ocfs2: fix locking for res->tracking and dlm->tracking_list
[linux/fpc-iii.git] / fs / cifs / smb2pdu.c
blobf7111bb88ec1beb1e051cfa5b7dad0c7dbf77c6a
1 /*
2 * fs/cifs/smb2pdu.c
4 * Copyright (C) International Business Machines Corp., 2009, 2013
5 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
9 * Contains the routines for constructing the SMB2 PDUs themselves
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
31 #include <linux/fs.h>
32 #include <linux/kernel.h>
33 #include <linux/vfs.h>
34 #include <linux/task_io_accounting_ops.h>
35 #include <linux/uaccess.h>
36 #include <linux/pagemap.h>
37 #include <linux/xattr.h>
38 #include "smb2pdu.h"
39 #include "cifsglob.h"
40 #include "cifsacl.h"
41 #include "cifsproto.h"
42 #include "smb2proto.h"
43 #include "cifs_unicode.h"
44 #include "cifs_debug.h"
45 #include "ntlmssp.h"
46 #include "smb2status.h"
47 #include "smb2glob.h"
48 #include "cifspdu.h"
49 #include "cifs_spnego.h"
52 * The following table defines the expected "StructureSize" of SMB2 requests
53 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
55 * Note that commands are defined in smb2pdu.h in le16 but the array below is
56 * indexed by command in host byte order.
58 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
59 /* SMB2_NEGOTIATE */ 36,
60 /* SMB2_SESSION_SETUP */ 25,
61 /* SMB2_LOGOFF */ 4,
62 /* SMB2_TREE_CONNECT */ 9,
63 /* SMB2_TREE_DISCONNECT */ 4,
64 /* SMB2_CREATE */ 57,
65 /* SMB2_CLOSE */ 24,
66 /* SMB2_FLUSH */ 24,
67 /* SMB2_READ */ 49,
68 /* SMB2_WRITE */ 49,
69 /* SMB2_LOCK */ 48,
70 /* SMB2_IOCTL */ 57,
71 /* SMB2_CANCEL */ 4,
72 /* SMB2_ECHO */ 4,
73 /* SMB2_QUERY_DIRECTORY */ 33,
74 /* SMB2_CHANGE_NOTIFY */ 32,
75 /* SMB2_QUERY_INFO */ 41,
76 /* SMB2_SET_INFO */ 33,
77 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
81 static void
82 smb2_hdr_assemble(struct smb2_hdr *hdr, __le16 smb2_cmd /* command */ ,
83 const struct cifs_tcon *tcon)
85 struct smb2_pdu *pdu = (struct smb2_pdu *)hdr;
86 char *temp = (char *)hdr;
87 /* lookup word count ie StructureSize from table */
88 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_cmd)];
91 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
92 * largest operations (Create)
94 memset(temp, 0, 256);
96 /* Note this is only network field converted to big endian */
97 hdr->smb2_buf_length = cpu_to_be32(parmsize + sizeof(struct smb2_hdr)
98 - 4 /* RFC 1001 length field itself not counted */);
100 hdr->ProtocolId[0] = 0xFE;
101 hdr->ProtocolId[1] = 'S';
102 hdr->ProtocolId[2] = 'M';
103 hdr->ProtocolId[3] = 'B';
104 hdr->StructureSize = cpu_to_le16(64);
105 hdr->Command = smb2_cmd;
106 if (tcon && tcon->ses && tcon->ses->server) {
107 struct TCP_Server_Info *server = tcon->ses->server;
109 spin_lock(&server->req_lock);
110 /* Request up to 2 credits but don't go over the limit. */
111 if (server->credits >= SMB2_MAX_CREDITS_AVAILABLE)
112 hdr->CreditRequest = cpu_to_le16(0);
113 else
114 hdr->CreditRequest = cpu_to_le16(
115 min_t(int, SMB2_MAX_CREDITS_AVAILABLE -
116 server->credits, 2));
117 spin_unlock(&server->req_lock);
118 } else {
119 hdr->CreditRequest = cpu_to_le16(2);
121 hdr->ProcessId = cpu_to_le32((__u16)current->tgid);
123 if (!tcon)
124 goto out;
126 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
127 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
128 if ((tcon->ses) && (tcon->ses->server) &&
129 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
130 hdr->CreditCharge = cpu_to_le16(1);
131 /* else CreditCharge MBZ */
133 hdr->TreeId = tcon->tid;
134 /* Uid is not converted */
135 if (tcon->ses)
136 hdr->SessionId = tcon->ses->Suid;
139 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
140 * to pass the path on the Open SMB prefixed by \\server\share.
141 * Not sure when we would need to do the augmented path (if ever) and
142 * setting this flag breaks the SMB2 open operation since it is
143 * illegal to send an empty path name (without \\server\share prefix)
144 * when the DFS flag is set in the SMB open header. We could
145 * consider setting the flag on all operations other than open
146 * but it is safer to net set it for now.
148 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
149 hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
151 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign)
152 hdr->Flags |= SMB2_FLAGS_SIGNED;
153 out:
154 pdu->StructureSize2 = cpu_to_le16(parmsize);
155 return;
158 static int
159 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
161 int rc;
162 struct nls_table *nls_codepage;
163 struct cifs_ses *ses;
164 struct TCP_Server_Info *server;
167 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
168 * check for tcp and smb session status done differently
169 * for those three - in the calling routine.
171 if (tcon == NULL)
172 return 0;
174 if (smb2_command == SMB2_TREE_CONNECT)
175 return 0;
177 if (tcon->tidStatus == CifsExiting) {
179 * only tree disconnect, open, and write,
180 * (and ulogoff which does not have tcon)
181 * are allowed as we start force umount.
183 if ((smb2_command != SMB2_WRITE) &&
184 (smb2_command != SMB2_CREATE) &&
185 (smb2_command != SMB2_TREE_DISCONNECT)) {
186 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
187 smb2_command);
188 return -ENODEV;
191 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
192 (!tcon->ses->server))
193 return -EIO;
195 ses = tcon->ses;
196 server = ses->server;
199 * Give demultiplex thread up to 10 seconds to reconnect, should be
200 * greater than cifs socket timeout which is 7 seconds
202 while (server->tcpStatus == CifsNeedReconnect) {
204 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
205 * here since they are implicitly done when session drops.
207 switch (smb2_command) {
209 * BB Should we keep oplock break and add flush to exceptions?
211 case SMB2_TREE_DISCONNECT:
212 case SMB2_CANCEL:
213 case SMB2_CLOSE:
214 case SMB2_OPLOCK_BREAK:
215 return -EAGAIN;
218 rc = wait_event_interruptible_timeout(server->response_q,
219 (server->tcpStatus != CifsNeedReconnect),
220 10 * HZ);
221 if (rc < 0) {
222 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
223 " signal by the process\n", __func__);
224 return -ERESTARTSYS;
227 /* are we still trying to reconnect? */
228 if (server->tcpStatus != CifsNeedReconnect)
229 break;
232 * on "soft" mounts we wait once. Hard mounts keep
233 * retrying until process is killed or server comes
234 * back on-line
236 if (!tcon->retry) {
237 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
238 return -EHOSTDOWN;
242 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
243 return 0;
245 nls_codepage = load_nls_default();
248 * need to prevent multiple threads trying to simultaneously reconnect
249 * the same SMB session
251 mutex_lock(&tcon->ses->session_mutex);
252 rc = cifs_negotiate_protocol(0, tcon->ses);
253 if (!rc && tcon->ses->need_reconnect)
254 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
256 if (rc || !tcon->need_reconnect) {
257 mutex_unlock(&tcon->ses->session_mutex);
258 goto out;
261 cifs_mark_open_files_invalid(tcon);
262 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
263 mutex_unlock(&tcon->ses->session_mutex);
264 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
265 if (rc)
266 goto out;
267 atomic_inc(&tconInfoReconnectCount);
268 out:
270 * Check if handle based operation so we know whether we can continue
271 * or not without returning to caller to reset file handle.
274 * BB Is flush done by server on drop of tcp session? Should we special
275 * case it and skip above?
277 switch (smb2_command) {
278 case SMB2_FLUSH:
279 case SMB2_READ:
280 case SMB2_WRITE:
281 case SMB2_LOCK:
282 case SMB2_IOCTL:
283 case SMB2_QUERY_DIRECTORY:
284 case SMB2_CHANGE_NOTIFY:
285 case SMB2_QUERY_INFO:
286 case SMB2_SET_INFO:
287 rc = -EAGAIN;
289 unload_nls(nls_codepage);
290 return rc;
294 * Allocate and return pointer to an SMB request hdr, and set basic
295 * SMB information in the SMB header. If the return code is zero, this
296 * function must have filled in request_buf pointer.
298 static int
299 small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
300 void **request_buf)
302 int rc = 0;
304 rc = smb2_reconnect(smb2_command, tcon);
305 if (rc)
306 return rc;
308 /* BB eventually switch this to SMB2 specific small buf size */
309 *request_buf = cifs_small_buf_get();
310 if (*request_buf == NULL) {
311 /* BB should we add a retry in here if not a writepage? */
312 return -ENOMEM;
315 smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
317 if (tcon != NULL) {
318 #ifdef CONFIG_CIFS_STATS
319 uint16_t com_code = le16_to_cpu(smb2_command);
320 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
321 #endif
322 cifs_stats_inc(&tcon->num_smbs_sent);
325 return rc;
328 #ifdef CONFIG_CIFS_SMB311
329 /* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
330 #define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
333 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
334 #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
336 static void
337 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
339 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
340 pneg_ctxt->DataLength = cpu_to_le16(38);
341 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
342 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
343 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
344 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
347 static void
348 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
350 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
351 pneg_ctxt->DataLength = cpu_to_le16(6);
352 pneg_ctxt->CipherCount = cpu_to_le16(2);
353 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
354 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
357 static void
358 assemble_neg_contexts(struct smb2_negotiate_req *req)
361 /* +4 is to account for the RFC1001 len field */
362 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
364 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
365 /* Add 2 to size to round to 8 byte boundary */
366 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
367 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
368 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
369 req->NegotiateContextCount = cpu_to_le16(2);
370 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context)
371 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
373 #else
374 static void assemble_neg_contexts(struct smb2_negotiate_req *req)
376 return;
378 #endif /* SMB311 */
383 * SMB2 Worker functions follow:
385 * The general structure of the worker functions is:
386 * 1) Call smb2_init (assembles SMB2 header)
387 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
388 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
389 * 4) Decode SMB2 command specific fields in the fixed length area
390 * 5) Decode variable length data area (if any for this SMB2 command type)
391 * 6) Call free smb buffer
392 * 7) return
397 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
399 struct smb2_negotiate_req *req;
400 struct smb2_negotiate_rsp *rsp;
401 struct kvec iov[1];
402 int rc = 0;
403 int resp_buftype;
404 struct TCP_Server_Info *server = ses->server;
405 int blob_offset, blob_length;
406 char *security_blob;
407 int flags = CIFS_NEG_OP;
409 cifs_dbg(FYI, "Negotiate protocol\n");
411 if (!server) {
412 WARN(1, "%s: server is NULL!\n", __func__);
413 return -EIO;
416 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
417 if (rc)
418 return rc;
420 req->hdr.SessionId = 0;
422 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
424 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
425 inc_rfc1001_len(req, 2);
427 /* only one of SMB2 signing flags may be set in SMB2 request */
428 if (ses->sign)
429 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
430 else if (global_secflags & CIFSSEC_MAY_SIGN)
431 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
432 else
433 req->SecurityMode = 0;
435 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
437 /* ClientGUID must be zero for SMB2.02 dialect */
438 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
439 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
440 else {
441 memcpy(req->ClientGUID, server->client_guid,
442 SMB2_CLIENT_GUID_SIZE);
443 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
444 assemble_neg_contexts(req);
446 iov[0].iov_base = (char *)req;
447 /* 4 for rfc1002 length field */
448 iov[0].iov_len = get_rfc1002_length(req) + 4;
450 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
452 rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
454 * No tcon so can't do
455 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
457 if (rc != 0)
458 goto neg_exit;
460 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
462 /* BB we may eventually want to match the negotiated vs. requested
463 dialect, even though we are only requesting one at a time */
464 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
465 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
466 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
467 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
468 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
469 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
470 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
471 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
472 #ifdef CONFIG_CIFS_SMB311
473 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
474 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
475 #endif /* SMB311 */
476 else {
477 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
478 le16_to_cpu(rsp->DialectRevision));
479 rc = -EIO;
480 goto neg_exit;
482 server->dialect = le16_to_cpu(rsp->DialectRevision);
484 /* SMB2 only has an extended negflavor */
485 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
486 /* set it to the maximum buffer size value we can send with 1 credit */
487 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
488 SMB2_MAX_BUFFER_SIZE);
489 server->max_read = le32_to_cpu(rsp->MaxReadSize);
490 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
491 /* BB Do we need to validate the SecurityMode? */
492 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
493 server->capabilities = le32_to_cpu(rsp->Capabilities);
494 /* Internal types */
495 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
497 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
498 &rsp->hdr);
500 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
501 * for us will be
502 * ses->sectype = RawNTLMSSP;
503 * but for time being this is our only auth choice so doesn't matter.
504 * We just found a server which sets blob length to zero expecting raw.
506 if (blob_length == 0)
507 cifs_dbg(FYI, "missing security blob on negprot\n");
509 rc = cifs_enable_signing(server, ses->sign);
510 if (rc)
511 goto neg_exit;
512 if (blob_length) {
513 rc = decode_negTokenInit(security_blob, blob_length, server);
514 if (rc == 1)
515 rc = 0;
516 else if (rc == 0)
517 rc = -EIO;
519 neg_exit:
520 free_rsp_buf(resp_buftype, rsp);
521 return rc;
524 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
526 int rc = 0;
527 struct validate_negotiate_info_req vneg_inbuf;
528 struct validate_negotiate_info_rsp *pneg_rsp;
529 u32 rsplen;
531 cifs_dbg(FYI, "validate negotiate\n");
534 * validation ioctl must be signed, so no point sending this if we
535 * can not sign it (ie are not known user). Even if signing is not
536 * required (enabled but not negotiated), in those cases we selectively
537 * sign just this, the first and only signed request on a connection.
538 * Having validation of negotiate info helps reduce attack vectors.
540 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
541 return 0; /* validation requires signing */
543 if (tcon->ses->user_name == NULL) {
544 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
545 return 0; /* validation requires signing */
548 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
549 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
551 vneg_inbuf.Capabilities =
552 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
553 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
554 SMB2_CLIENT_GUID_SIZE);
556 if (tcon->ses->sign)
557 vneg_inbuf.SecurityMode =
558 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
559 else if (global_secflags & CIFSSEC_MAY_SIGN)
560 vneg_inbuf.SecurityMode =
561 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
562 else
563 vneg_inbuf.SecurityMode = 0;
565 vneg_inbuf.DialectCount = cpu_to_le16(1);
566 vneg_inbuf.Dialects[0] =
567 cpu_to_le16(tcon->ses->server->vals->protocol_id);
569 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
570 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
571 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
572 (char **)&pneg_rsp, &rsplen);
574 if (rc != 0) {
575 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
576 return -EIO;
579 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
580 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
581 rsplen);
583 /* relax check since Mac returns max bufsize allowed on ioctl */
584 if (rsplen > CIFSMaxBufSize)
585 return -EIO;
588 /* check validate negotiate info response matches what we got earlier */
589 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
590 goto vneg_out;
592 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
593 goto vneg_out;
595 /* do not validate server guid because not saved at negprot time yet */
597 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
598 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
599 goto vneg_out;
601 /* validate negotiate successful */
602 cifs_dbg(FYI, "validate negotiate info successful\n");
603 return 0;
605 vneg_out:
606 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
607 return -EIO;
611 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
612 const struct nls_table *nls_cp)
614 struct smb2_sess_setup_req *req;
615 struct smb2_sess_setup_rsp *rsp = NULL;
616 struct kvec iov[2];
617 int rc = 0;
618 int resp_buftype = CIFS_NO_BUFFER;
619 __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */
620 struct TCP_Server_Info *server = ses->server;
621 u16 blob_length = 0;
622 struct key *spnego_key = NULL;
623 char *security_blob = NULL;
624 unsigned char *ntlmssp_blob = NULL;
625 bool use_spnego = false; /* else use raw ntlmssp */
626 u64 previous_session = ses->Suid;
628 cifs_dbg(FYI, "Session Setup\n");
630 if (!server) {
631 WARN(1, "%s: server is NULL!\n", __func__);
632 return -EIO;
636 * If we are here due to reconnect, free per-smb session key
637 * in case signing was required.
639 kfree(ses->auth_key.response);
640 ses->auth_key.response = NULL;
643 * If memory allocation is successful, caller of this function
644 * frees it.
646 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
647 if (!ses->ntlmssp)
648 return -ENOMEM;
649 ses->ntlmssp->sesskey_per_smbsess = true;
651 /* FIXME: allow for other auth types besides NTLMSSP (e.g. krb5) */
652 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
653 ses->sectype = RawNTLMSSP;
655 ssetup_ntlmssp_authenticate:
656 if (phase == NtLmChallenge)
657 phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
659 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
660 if (rc)
661 return rc;
663 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
665 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
666 req->PreviousSessionId = previous_session;
668 req->Flags = 0; /* MBZ */
669 /* to enable echos and oplocks */
670 req->hdr.CreditRequest = cpu_to_le16(3);
672 /* only one of SMB2 signing flags may be set in SMB2 request */
673 if (server->sign)
674 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
675 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
676 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
677 else
678 req->SecurityMode = 0;
680 req->Capabilities = 0;
681 req->Channel = 0; /* MBZ */
683 iov[0].iov_base = (char *)req;
684 /* 4 for rfc1002 length field and 1 for pad */
685 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
687 if (ses->sectype == Kerberos) {
688 #ifdef CONFIG_CIFS_UPCALL
689 struct cifs_spnego_msg *msg;
691 spnego_key = cifs_get_spnego_key(ses);
692 if (IS_ERR(spnego_key)) {
693 rc = PTR_ERR(spnego_key);
694 spnego_key = NULL;
695 goto ssetup_exit;
698 msg = spnego_key->payload.data[0];
700 * check version field to make sure that cifs.upcall is
701 * sending us a response in an expected form
703 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
704 cifs_dbg(VFS,
705 "bad cifs.upcall version. Expected %d got %d",
706 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
707 rc = -EKEYREJECTED;
708 goto ssetup_exit;
710 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
711 GFP_KERNEL);
712 if (!ses->auth_key.response) {
713 cifs_dbg(VFS,
714 "Kerberos can't allocate (%u bytes) memory",
715 msg->sesskey_len);
716 rc = -ENOMEM;
717 goto ssetup_exit;
719 ses->auth_key.len = msg->sesskey_len;
720 blob_length = msg->secblob_len;
721 iov[1].iov_base = msg->data + msg->sesskey_len;
722 iov[1].iov_len = blob_length;
723 #else
724 rc = -EOPNOTSUPP;
725 goto ssetup_exit;
726 #endif /* CONFIG_CIFS_UPCALL */
727 } else if (phase == NtLmNegotiate) { /* if not krb5 must be ntlmssp */
728 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
729 GFP_KERNEL);
730 if (ntlmssp_blob == NULL) {
731 rc = -ENOMEM;
732 goto ssetup_exit;
734 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
735 if (use_spnego) {
736 /* blob_length = build_spnego_ntlmssp_blob(
737 &security_blob,
738 sizeof(struct _NEGOTIATE_MESSAGE),
739 ntlmssp_blob); */
740 /* BB eventually need to add this */
741 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
742 rc = -EOPNOTSUPP;
743 kfree(ntlmssp_blob);
744 goto ssetup_exit;
745 } else {
746 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
747 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
748 security_blob = ntlmssp_blob;
750 iov[1].iov_base = security_blob;
751 iov[1].iov_len = blob_length;
752 } else if (phase == NtLmAuthenticate) {
753 req->hdr.SessionId = ses->Suid;
754 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
755 nls_cp);
756 if (rc) {
757 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n",
758 rc);
759 goto ssetup_exit; /* BB double check error handling */
761 if (use_spnego) {
762 /* blob_length = build_spnego_ntlmssp_blob(
763 &security_blob,
764 blob_length,
765 ntlmssp_blob); */
766 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
767 rc = -EOPNOTSUPP;
768 kfree(ntlmssp_blob);
769 goto ssetup_exit;
770 } else {
771 security_blob = ntlmssp_blob;
773 iov[1].iov_base = security_blob;
774 iov[1].iov_len = blob_length;
775 } else {
776 cifs_dbg(VFS, "illegal ntlmssp phase\n");
777 rc = -EIO;
778 goto ssetup_exit;
781 /* Testing shows that buffer offset must be at location of Buffer[0] */
782 req->SecurityBufferOffset =
783 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
784 1 /* pad */ - 4 /* rfc1001 len */);
785 req->SecurityBufferLength = cpu_to_le16(blob_length);
787 inc_rfc1001_len(req, blob_length - 1 /* pad */);
789 /* BB add code to build os and lm fields */
791 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype,
792 CIFS_LOG_ERROR | CIFS_NEG_OP);
794 kfree(security_blob);
795 rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
796 ses->Suid = rsp->hdr.SessionId;
797 if (resp_buftype != CIFS_NO_BUFFER &&
798 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
799 if (phase != NtLmNegotiate) {
800 cifs_dbg(VFS, "Unexpected more processing error\n");
801 goto ssetup_exit;
803 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
804 le16_to_cpu(rsp->SecurityBufferOffset)) {
805 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
806 le16_to_cpu(rsp->SecurityBufferOffset));
807 rc = -EIO;
808 goto ssetup_exit;
811 /* NTLMSSP Negotiate sent now processing challenge (response) */
812 phase = NtLmChallenge; /* process ntlmssp challenge */
813 rc = 0; /* MORE_PROCESSING is not an error here but expected */
814 rc = decode_ntlmssp_challenge(rsp->Buffer,
815 le16_to_cpu(rsp->SecurityBufferLength), ses);
819 * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
820 * but at least the raw NTLMSSP case works.
823 * No tcon so can't do
824 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
826 if (rc != 0)
827 goto ssetup_exit;
829 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
830 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
831 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
832 ssetup_exit:
833 free_rsp_buf(resp_buftype, rsp);
835 /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
836 if ((phase == NtLmChallenge) && (rc == 0))
837 goto ssetup_ntlmssp_authenticate;
839 if (!rc) {
840 mutex_lock(&server->srv_mutex);
841 if (server->ops->generate_signingkey) {
842 rc = server->ops->generate_signingkey(ses);
843 if (rc) {
844 cifs_dbg(FYI,
845 "SMB3 session key generation failed\n");
846 mutex_unlock(&server->srv_mutex);
847 goto keygen_exit;
850 if (!server->session_estab) {
851 server->sequence_number = 0x2;
852 server->session_estab = true;
854 mutex_unlock(&server->srv_mutex);
856 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
857 spin_lock(&GlobalMid_Lock);
858 ses->status = CifsGood;
859 ses->need_reconnect = false;
860 spin_unlock(&GlobalMid_Lock);
863 keygen_exit:
864 if (spnego_key) {
865 key_invalidate(spnego_key);
866 key_put(spnego_key);
868 kfree(ses->ntlmssp);
870 return rc;
874 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
876 struct smb2_logoff_req *req; /* response is also trivial struct */
877 int rc = 0;
878 struct TCP_Server_Info *server;
880 cifs_dbg(FYI, "disconnect session %p\n", ses);
882 if (ses && (ses->server))
883 server = ses->server;
884 else
885 return -EIO;
887 /* no need to send SMB logoff if uid already closed due to reconnect */
888 if (ses->need_reconnect)
889 goto smb2_session_already_dead;
891 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
892 if (rc)
893 return rc;
895 /* since no tcon, smb2_init can not do this, so do here */
896 req->hdr.SessionId = ses->Suid;
897 if (server->sign)
898 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
900 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
902 * No tcon so can't do
903 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
906 smb2_session_already_dead:
907 return rc;
910 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
912 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
915 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
917 /* These are similar values to what Windows uses */
918 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
920 tcon->max_chunks = 256;
921 tcon->max_bytes_chunk = 1048576;
922 tcon->max_bytes_copy = 16777216;
926 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
927 struct cifs_tcon *tcon, const struct nls_table *cp)
929 struct smb2_tree_connect_req *req;
930 struct smb2_tree_connect_rsp *rsp = NULL;
931 struct kvec iov[2];
932 int rc = 0;
933 int resp_buftype;
934 int unc_path_len;
935 struct TCP_Server_Info *server;
936 __le16 *unc_path = NULL;
938 cifs_dbg(FYI, "TCON\n");
940 if ((ses->server) && tree)
941 server = ses->server;
942 else
943 return -EIO;
945 if ((tcon && tcon->seal) &&
946 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
947 cifs_dbg(VFS, "encryption requested but no server support");
948 return -EOPNOTSUPP;
951 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
952 if (unc_path == NULL)
953 return -ENOMEM;
955 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
956 unc_path_len *= 2;
957 if (unc_path_len < 2) {
958 kfree(unc_path);
959 return -EINVAL;
962 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
963 if (tcon)
964 tcon->tid = 0;
966 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
967 if (rc) {
968 kfree(unc_path);
969 return rc;
972 if (tcon == NULL) {
973 /* since no tcon, smb2_init can not do this, so do here */
974 req->hdr.SessionId = ses->Suid;
975 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
976 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
979 iov[0].iov_base = (char *)req;
980 /* 4 for rfc1002 length field and 1 for pad */
981 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
983 /* Testing shows that buffer offset must be at location of Buffer[0] */
984 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
985 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
986 req->PathLength = cpu_to_le16(unc_path_len - 2);
987 iov[1].iov_base = unc_path;
988 iov[1].iov_len = unc_path_len;
990 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
992 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
993 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
995 if (rc != 0) {
996 if (tcon) {
997 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
998 tcon->need_reconnect = true;
1000 goto tcon_error_exit;
1003 if (tcon == NULL) {
1004 ses->ipc_tid = rsp->hdr.TreeId;
1005 goto tcon_exit;
1008 switch (rsp->ShareType) {
1009 case SMB2_SHARE_TYPE_DISK:
1010 cifs_dbg(FYI, "connection to disk share\n");
1011 break;
1012 case SMB2_SHARE_TYPE_PIPE:
1013 tcon->ipc = true;
1014 cifs_dbg(FYI, "connection to pipe share\n");
1015 break;
1016 case SMB2_SHARE_TYPE_PRINT:
1017 tcon->ipc = true;
1018 cifs_dbg(FYI, "connection to printer\n");
1019 break;
1020 default:
1021 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1022 rc = -EOPNOTSUPP;
1023 goto tcon_error_exit;
1026 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1027 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1028 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1029 tcon->tidStatus = CifsGood;
1030 tcon->need_reconnect = false;
1031 tcon->tid = rsp->hdr.TreeId;
1032 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1034 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1035 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1036 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1037 init_copy_chunk_defaults(tcon);
1038 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1039 cifs_dbg(VFS, "Encrypted shares not supported");
1040 if (tcon->ses->server->ops->validate_negotiate)
1041 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1042 tcon_exit:
1043 free_rsp_buf(resp_buftype, rsp);
1044 kfree(unc_path);
1045 return rc;
1047 tcon_error_exit:
1048 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
1049 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1051 goto tcon_exit;
1055 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1057 struct smb2_tree_disconnect_req *req; /* response is trivial */
1058 int rc = 0;
1059 struct TCP_Server_Info *server;
1060 struct cifs_ses *ses = tcon->ses;
1062 cifs_dbg(FYI, "Tree Disconnect\n");
1064 if (ses && (ses->server))
1065 server = ses->server;
1066 else
1067 return -EIO;
1069 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1070 return 0;
1072 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1073 if (rc)
1074 return rc;
1076 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
1077 if (rc)
1078 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1080 return rc;
1084 static struct create_durable *
1085 create_durable_buf(void)
1087 struct create_durable *buf;
1089 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1090 if (!buf)
1091 return NULL;
1093 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1094 (struct create_durable, Data));
1095 buf->ccontext.DataLength = cpu_to_le32(16);
1096 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1097 (struct create_durable, Name));
1098 buf->ccontext.NameLength = cpu_to_le16(4);
1099 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1100 buf->Name[0] = 'D';
1101 buf->Name[1] = 'H';
1102 buf->Name[2] = 'n';
1103 buf->Name[3] = 'Q';
1104 return buf;
1107 static struct create_durable *
1108 create_reconnect_durable_buf(struct cifs_fid *fid)
1110 struct create_durable *buf;
1112 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1113 if (!buf)
1114 return NULL;
1116 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1117 (struct create_durable, Data));
1118 buf->ccontext.DataLength = cpu_to_le32(16);
1119 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1120 (struct create_durable, Name));
1121 buf->ccontext.NameLength = cpu_to_le16(4);
1122 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1123 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1124 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1125 buf->Name[0] = 'D';
1126 buf->Name[1] = 'H';
1127 buf->Name[2] = 'n';
1128 buf->Name[3] = 'C';
1129 return buf;
1132 static __u8
1133 parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1134 unsigned int *epoch)
1136 char *data_offset;
1137 struct create_context *cc;
1138 unsigned int next;
1139 unsigned int remaining;
1140 char *name;
1142 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
1143 remaining = le32_to_cpu(rsp->CreateContextsLength);
1144 cc = (struct create_context *)data_offset;
1145 while (remaining >= sizeof(struct create_context)) {
1146 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1147 if (le16_to_cpu(cc->NameLength) == 4 &&
1148 strncmp(name, "RqLs", 4) == 0)
1149 return server->ops->parse_lease_buf(cc, epoch);
1151 next = le32_to_cpu(cc->Next);
1152 if (!next)
1153 break;
1154 remaining -= next;
1155 cc = (struct create_context *)((char *)cc + next);
1158 return 0;
1161 static int
1162 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1163 unsigned int *num_iovec, __u8 *oplock)
1165 struct smb2_create_req *req = iov[0].iov_base;
1166 unsigned int num = *num_iovec;
1168 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
1169 if (iov[num].iov_base == NULL)
1170 return -ENOMEM;
1171 iov[num].iov_len = server->vals->create_lease_size;
1172 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1173 if (!req->CreateContextsOffset)
1174 req->CreateContextsOffset = cpu_to_le32(
1175 sizeof(struct smb2_create_req) - 4 +
1176 iov[num - 1].iov_len);
1177 le32_add_cpu(&req->CreateContextsLength,
1178 server->vals->create_lease_size);
1179 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
1180 *num_iovec = num + 1;
1181 return 0;
1184 static struct create_durable_v2 *
1185 create_durable_v2_buf(struct cifs_fid *pfid)
1187 struct create_durable_v2 *buf;
1189 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1190 if (!buf)
1191 return NULL;
1193 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1194 (struct create_durable_v2, dcontext));
1195 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1196 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1197 (struct create_durable_v2, Name));
1198 buf->ccontext.NameLength = cpu_to_le16(4);
1200 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1201 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1202 generate_random_uuid(buf->dcontext.CreateGuid);
1203 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1205 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1206 buf->Name[0] = 'D';
1207 buf->Name[1] = 'H';
1208 buf->Name[2] = '2';
1209 buf->Name[3] = 'Q';
1210 return buf;
1213 static struct create_durable_handle_reconnect_v2 *
1214 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1216 struct create_durable_handle_reconnect_v2 *buf;
1218 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1219 GFP_KERNEL);
1220 if (!buf)
1221 return NULL;
1223 buf->ccontext.DataOffset =
1224 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1225 dcontext));
1226 buf->ccontext.DataLength =
1227 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1228 buf->ccontext.NameOffset =
1229 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1230 Name));
1231 buf->ccontext.NameLength = cpu_to_le16(4);
1233 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1234 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1235 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1236 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1238 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1239 buf->Name[0] = 'D';
1240 buf->Name[1] = 'H';
1241 buf->Name[2] = '2';
1242 buf->Name[3] = 'C';
1243 return buf;
1246 static int
1247 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1248 struct cifs_open_parms *oparms)
1250 struct smb2_create_req *req = iov[0].iov_base;
1251 unsigned int num = *num_iovec;
1253 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1254 if (iov[num].iov_base == NULL)
1255 return -ENOMEM;
1256 iov[num].iov_len = sizeof(struct create_durable_v2);
1257 if (!req->CreateContextsOffset)
1258 req->CreateContextsOffset =
1259 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1260 iov[1].iov_len);
1261 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1262 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1263 *num_iovec = num + 1;
1264 return 0;
1267 static int
1268 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1269 struct cifs_open_parms *oparms)
1271 struct smb2_create_req *req = iov[0].iov_base;
1272 unsigned int num = *num_iovec;
1274 /* indicate that we don't need to relock the file */
1275 oparms->reconnect = false;
1277 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1278 if (iov[num].iov_base == NULL)
1279 return -ENOMEM;
1280 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1281 if (!req->CreateContextsOffset)
1282 req->CreateContextsOffset =
1283 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1284 iov[1].iov_len);
1285 le32_add_cpu(&req->CreateContextsLength,
1286 sizeof(struct create_durable_handle_reconnect_v2));
1287 inc_rfc1001_len(&req->hdr,
1288 sizeof(struct create_durable_handle_reconnect_v2));
1289 *num_iovec = num + 1;
1290 return 0;
1293 static int
1294 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1295 struct cifs_open_parms *oparms, bool use_persistent)
1297 struct smb2_create_req *req = iov[0].iov_base;
1298 unsigned int num = *num_iovec;
1300 if (use_persistent) {
1301 if (oparms->reconnect)
1302 return add_durable_reconnect_v2_context(iov, num_iovec,
1303 oparms);
1304 else
1305 return add_durable_v2_context(iov, num_iovec, oparms);
1308 if (oparms->reconnect) {
1309 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1310 /* indicate that we don't need to relock the file */
1311 oparms->reconnect = false;
1312 } else
1313 iov[num].iov_base = create_durable_buf();
1314 if (iov[num].iov_base == NULL)
1315 return -ENOMEM;
1316 iov[num].iov_len = sizeof(struct create_durable);
1317 if (!req->CreateContextsOffset)
1318 req->CreateContextsOffset =
1319 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1320 iov[1].iov_len);
1321 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1322 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1323 *num_iovec = num + 1;
1324 return 0;
1328 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
1329 __u8 *oplock, struct smb2_file_all_info *buf,
1330 struct smb2_err_rsp **err_buf)
1332 struct smb2_create_req *req;
1333 struct smb2_create_rsp *rsp;
1334 struct TCP_Server_Info *server;
1335 struct cifs_tcon *tcon = oparms->tcon;
1336 struct cifs_ses *ses = tcon->ses;
1337 struct kvec iov[4];
1338 int resp_buftype;
1339 int uni_path_len;
1340 __le16 *copy_path = NULL;
1341 int copy_size;
1342 int rc = 0;
1343 unsigned int num_iovecs = 2;
1344 __u32 file_attributes = 0;
1345 char *dhc_buf = NULL, *lc_buf = NULL;
1347 cifs_dbg(FYI, "create/open\n");
1349 if (ses && (ses->server))
1350 server = ses->server;
1351 else
1352 return -EIO;
1354 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1355 if (rc)
1356 return rc;
1358 if (oparms->create_options & CREATE_OPTION_READONLY)
1359 file_attributes |= ATTR_READONLY;
1360 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1361 file_attributes |= ATTR_SYSTEM;
1363 req->ImpersonationLevel = IL_IMPERSONATION;
1364 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
1365 /* File attributes ignored on open (used in create though) */
1366 req->FileAttributes = cpu_to_le32(file_attributes);
1367 req->ShareAccess = FILE_SHARE_ALL_LE;
1368 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1369 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
1370 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1371 /* do not count rfc1001 len field */
1372 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1374 iov[0].iov_base = (char *)req;
1375 /* 4 for rfc1002 length field */
1376 iov[0].iov_len = get_rfc1002_length(req) + 4;
1378 /* MUST set path len (NameLength) to 0 opening root of share */
1379 req->NameLength = cpu_to_le16(uni_path_len - 2);
1380 /* -1 since last byte is buf[0] which is sent below (path) */
1381 iov[0].iov_len--;
1382 if (uni_path_len % 8 != 0) {
1383 copy_size = uni_path_len / 8 * 8;
1384 if (copy_size < uni_path_len)
1385 copy_size += 8;
1387 copy_path = kzalloc(copy_size, GFP_KERNEL);
1388 if (!copy_path)
1389 return -ENOMEM;
1390 memcpy((char *)copy_path, (const char *)path,
1391 uni_path_len);
1392 uni_path_len = copy_size;
1393 path = copy_path;
1396 iov[1].iov_len = uni_path_len;
1397 iov[1].iov_base = path;
1398 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1399 inc_rfc1001_len(req, uni_path_len - 1);
1401 if (!server->oplocks)
1402 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1404 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1405 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1406 req->RequestedOplockLevel = *oplock;
1407 else {
1408 rc = add_lease_context(server, iov, &num_iovecs, oplock);
1409 if (rc) {
1410 cifs_small_buf_release(req);
1411 kfree(copy_path);
1412 return rc;
1414 lc_buf = iov[num_iovecs-1].iov_base;
1417 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1418 /* need to set Next field of lease context if we request it */
1419 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
1420 struct create_context *ccontext =
1421 (struct create_context *)iov[num_iovecs-1].iov_base;
1422 ccontext->Next =
1423 cpu_to_le32(server->vals->create_lease_size);
1426 rc = add_durable_context(iov, &num_iovecs, oparms,
1427 tcon->use_persistent);
1428 if (rc) {
1429 cifs_small_buf_release(req);
1430 kfree(copy_path);
1431 kfree(lc_buf);
1432 return rc;
1434 dhc_buf = iov[num_iovecs-1].iov_base;
1437 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1438 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1440 if (rc != 0) {
1441 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
1442 if (err_buf)
1443 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1444 GFP_KERNEL);
1445 goto creat_exit;
1448 oparms->fid->persistent_fid = rsp->PersistentFileId;
1449 oparms->fid->volatile_fid = rsp->VolatileFileId;
1451 if (buf) {
1452 memcpy(buf, &rsp->CreationTime, 32);
1453 buf->AllocationSize = rsp->AllocationSize;
1454 buf->EndOfFile = rsp->EndofFile;
1455 buf->Attributes = rsp->FileAttributes;
1456 buf->NumberOfLinks = cpu_to_le32(1);
1457 buf->DeletePending = 0;
1460 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
1461 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
1462 else
1463 *oplock = rsp->OplockLevel;
1464 creat_exit:
1465 kfree(copy_path);
1466 kfree(lc_buf);
1467 kfree(dhc_buf);
1468 free_rsp_buf(resp_buftype, rsp);
1469 return rc;
1473 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1476 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1477 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1478 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1480 struct smb2_ioctl_req *req;
1481 struct smb2_ioctl_rsp *rsp;
1482 struct TCP_Server_Info *server;
1483 struct cifs_ses *ses;
1484 struct kvec iov[2];
1485 int resp_buftype;
1486 int num_iovecs;
1487 int rc = 0;
1489 cifs_dbg(FYI, "SMB2 IOCTL\n");
1491 if (out_data != NULL)
1492 *out_data = NULL;
1494 /* zero out returned data len, in case of error */
1495 if (plen)
1496 *plen = 0;
1498 if (tcon)
1499 ses = tcon->ses;
1500 else
1501 return -EIO;
1503 if (ses && (ses->server))
1504 server = ses->server;
1505 else
1506 return -EIO;
1508 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1509 if (rc)
1510 return rc;
1512 req->CtlCode = cpu_to_le32(opcode);
1513 req->PersistentFileId = persistent_fid;
1514 req->VolatileFileId = volatile_fid;
1516 if (indatalen) {
1517 req->InputCount = cpu_to_le32(indatalen);
1518 /* do not set InputOffset if no input data */
1519 req->InputOffset =
1520 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1521 iov[1].iov_base = in_data;
1522 iov[1].iov_len = indatalen;
1523 num_iovecs = 2;
1524 } else
1525 num_iovecs = 1;
1527 req->OutputOffset = 0;
1528 req->OutputCount = 0; /* MBZ */
1531 * Could increase MaxOutputResponse, but that would require more
1532 * than one credit. Windows typically sets this smaller, but for some
1533 * ioctls it may be useful to allow server to send more. No point
1534 * limiting what the server can send as long as fits in one credit
1535 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1536 * (by default, note that it can be overridden to make max larger)
1537 * in responses (except for read responses which can be bigger.
1538 * We may want to bump this limit up
1540 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
1542 if (is_fsctl)
1543 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1544 else
1545 req->Flags = 0;
1547 iov[0].iov_base = (char *)req;
1550 * If no input data, the size of ioctl struct in
1551 * protocol spec still includes a 1 byte data buffer,
1552 * but if input data passed to ioctl, we do not
1553 * want to double count this, so we do not send
1554 * the dummy one byte of data in iovec[0] if sending
1555 * input data (in iovec[1]). We also must add 4 bytes
1556 * in first iovec to allow for rfc1002 length field.
1559 if (indatalen) {
1560 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1561 inc_rfc1001_len(req, indatalen - 1);
1562 } else
1563 iov[0].iov_len = get_rfc1002_length(req) + 4;
1565 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
1566 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
1567 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1569 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1570 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1572 if ((rc != 0) && (rc != -EINVAL)) {
1573 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1574 goto ioctl_exit;
1575 } else if (rc == -EINVAL) {
1576 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1577 (opcode != FSCTL_SRV_COPYCHUNK)) {
1578 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1579 goto ioctl_exit;
1583 /* check if caller wants to look at return data or just return rc */
1584 if ((plen == NULL) || (out_data == NULL))
1585 goto ioctl_exit;
1587 *plen = le32_to_cpu(rsp->OutputCount);
1589 /* We check for obvious errors in the output buffer length and offset */
1590 if (*plen == 0)
1591 goto ioctl_exit; /* server returned no data */
1592 else if (*plen > 0xFF00) {
1593 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1594 *plen = 0;
1595 rc = -EIO;
1596 goto ioctl_exit;
1599 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1600 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1601 le32_to_cpu(rsp->OutputOffset));
1602 *plen = 0;
1603 rc = -EIO;
1604 goto ioctl_exit;
1607 *out_data = kmalloc(*plen, GFP_KERNEL);
1608 if (*out_data == NULL) {
1609 rc = -ENOMEM;
1610 goto ioctl_exit;
1613 memcpy(*out_data, rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
1614 *plen);
1615 ioctl_exit:
1616 free_rsp_buf(resp_buftype, rsp);
1617 return rc;
1621 * Individual callers to ioctl worker function follow
1625 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1626 u64 persistent_fid, u64 volatile_fid)
1628 int rc;
1629 struct compress_ioctl fsctl_input;
1630 char *ret_data = NULL;
1632 fsctl_input.CompressionState =
1633 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
1635 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1636 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1637 (char *)&fsctl_input /* data input */,
1638 2 /* in data len */, &ret_data /* out data */, NULL);
1640 cifs_dbg(FYI, "set compression rc %d\n", rc);
1642 return rc;
1646 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1647 u64 persistent_fid, u64 volatile_fid)
1649 struct smb2_close_req *req;
1650 struct smb2_close_rsp *rsp;
1651 struct TCP_Server_Info *server;
1652 struct cifs_ses *ses = tcon->ses;
1653 struct kvec iov[1];
1654 int resp_buftype;
1655 int rc = 0;
1657 cifs_dbg(FYI, "Close\n");
1659 if (ses && (ses->server))
1660 server = ses->server;
1661 else
1662 return -EIO;
1664 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1665 if (rc)
1666 return rc;
1668 req->PersistentFileId = persistent_fid;
1669 req->VolatileFileId = volatile_fid;
1671 iov[0].iov_base = (char *)req;
1672 /* 4 for rfc1002 length field */
1673 iov[0].iov_len = get_rfc1002_length(req) + 4;
1675 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1676 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1678 if (rc != 0) {
1679 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
1680 goto close_exit;
1683 /* BB FIXME - decode close response, update inode for caching */
1685 close_exit:
1686 free_rsp_buf(resp_buftype, rsp);
1687 return rc;
1690 static int
1691 validate_buf(unsigned int offset, unsigned int buffer_length,
1692 struct smb2_hdr *hdr, unsigned int min_buf_size)
1695 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1696 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1697 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1698 char *end_of_buf = begin_of_buf + buffer_length;
1701 if (buffer_length < min_buf_size) {
1702 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1703 buffer_length, min_buf_size);
1704 return -EINVAL;
1707 /* check if beyond RFC1001 maximum length */
1708 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
1709 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1710 buffer_length, smb_len);
1711 return -EINVAL;
1714 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
1715 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
1716 return -EINVAL;
1719 return 0;
1723 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1724 * Caller must free buffer.
1726 static int
1727 validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1728 struct smb2_hdr *hdr, unsigned int minbufsize,
1729 char *data)
1732 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1733 int rc;
1735 if (!data)
1736 return -EINVAL;
1738 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1739 if (rc)
1740 return rc;
1742 memcpy(data, begin_of_buf, buffer_length);
1744 return 0;
1747 static int
1748 query_info(const unsigned int xid, struct cifs_tcon *tcon,
1749 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1750 size_t output_len, size_t min_len, void *data)
1752 struct smb2_query_info_req *req;
1753 struct smb2_query_info_rsp *rsp = NULL;
1754 struct kvec iov[2];
1755 int rc = 0;
1756 int resp_buftype;
1757 struct TCP_Server_Info *server;
1758 struct cifs_ses *ses = tcon->ses;
1760 cifs_dbg(FYI, "Query Info\n");
1762 if (ses && (ses->server))
1763 server = ses->server;
1764 else
1765 return -EIO;
1767 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1768 if (rc)
1769 return rc;
1771 req->InfoType = SMB2_O_INFO_FILE;
1772 req->FileInfoClass = info_class;
1773 req->PersistentFileId = persistent_fid;
1774 req->VolatileFileId = volatile_fid;
1775 /* 4 for rfc1002 length field and 1 for Buffer */
1776 req->InputBufferOffset =
1777 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
1778 req->OutputBufferLength = cpu_to_le32(output_len);
1780 iov[0].iov_base = (char *)req;
1781 /* 4 for rfc1002 length field */
1782 iov[0].iov_len = get_rfc1002_length(req) + 4;
1784 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1785 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1787 if (rc) {
1788 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1789 goto qinf_exit;
1792 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1793 le32_to_cpu(rsp->OutputBufferLength),
1794 &rsp->hdr, min_len, data);
1796 qinf_exit:
1797 free_rsp_buf(resp_buftype, rsp);
1798 return rc;
1802 SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1803 u64 persistent_fid, u64 volatile_fid,
1804 struct smb2_file_all_info *data)
1806 return query_info(xid, tcon, persistent_fid, volatile_fid,
1807 FILE_ALL_INFORMATION,
1808 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
1809 sizeof(struct smb2_file_all_info), data);
1813 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1814 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1816 return query_info(xid, tcon, persistent_fid, volatile_fid,
1817 FILE_INTERNAL_INFORMATION,
1818 sizeof(struct smb2_file_internal_info),
1819 sizeof(struct smb2_file_internal_info), uniqueid);
1823 * This is a no-op for now. We're not really interested in the reply, but
1824 * rather in the fact that the server sent one and that server->lstrp
1825 * gets updated.
1827 * FIXME: maybe we should consider checking that the reply matches request?
1829 static void
1830 smb2_echo_callback(struct mid_q_entry *mid)
1832 struct TCP_Server_Info *server = mid->callback_data;
1833 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1834 unsigned int credits_received = 1;
1836 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1837 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1839 mutex_lock(&server->srv_mutex);
1840 DeleteMidQEntry(mid);
1841 mutex_unlock(&server->srv_mutex);
1842 add_credits(server, credits_received, CIFS_ECHO_OP);
1845 void smb2_reconnect_server(struct work_struct *work)
1847 struct TCP_Server_Info *server = container_of(work,
1848 struct TCP_Server_Info, reconnect.work);
1849 struct cifs_ses *ses;
1850 struct cifs_tcon *tcon, *tcon2;
1851 struct list_head tmp_list;
1852 int tcon_exist = false;
1854 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
1855 mutex_lock(&server->reconnect_mutex);
1857 INIT_LIST_HEAD(&tmp_list);
1858 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
1860 spin_lock(&cifs_tcp_ses_lock);
1861 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1862 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1863 if (tcon->need_reconnect) {
1864 tcon->tc_count++;
1865 list_add_tail(&tcon->rlist, &tmp_list);
1866 tcon_exist = true;
1871 * Get the reference to server struct to be sure that the last call of
1872 * cifs_put_tcon() in the loop below won't release the server pointer.
1874 if (tcon_exist)
1875 server->srv_count++;
1877 spin_unlock(&cifs_tcp_ses_lock);
1879 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
1880 smb2_reconnect(SMB2_ECHO, tcon);
1881 list_del_init(&tcon->rlist);
1882 cifs_put_tcon(tcon);
1885 cifs_dbg(FYI, "Reconnecting tcons finished\n");
1886 mutex_unlock(&server->reconnect_mutex);
1888 /* now we can safely release srv struct */
1889 if (tcon_exist)
1890 cifs_put_tcp_session(server, 1);
1894 SMB2_echo(struct TCP_Server_Info *server)
1896 struct smb2_echo_req *req;
1897 int rc = 0;
1898 struct kvec iov;
1899 struct smb_rqst rqst = { .rq_iov = &iov,
1900 .rq_nvec = 1 };
1902 cifs_dbg(FYI, "In echo request\n");
1904 if (server->tcpStatus == CifsNeedNegotiate) {
1905 /* No need to send echo on newly established connections */
1906 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
1907 return rc;
1910 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
1911 if (rc)
1912 return rc;
1914 req->hdr.CreditRequest = cpu_to_le16(1);
1916 iov.iov_base = (char *)req;
1917 /* 4 for rfc1002 length field */
1918 iov.iov_len = get_rfc1002_length(req) + 4;
1920 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
1921 CIFS_ECHO_OP);
1922 if (rc)
1923 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
1925 cifs_small_buf_release(req);
1926 return rc;
1930 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1931 u64 volatile_fid)
1933 struct smb2_flush_req *req;
1934 struct TCP_Server_Info *server;
1935 struct cifs_ses *ses = tcon->ses;
1936 struct kvec iov[1];
1937 int resp_buftype;
1938 int rc = 0;
1940 cifs_dbg(FYI, "Flush\n");
1942 if (ses && (ses->server))
1943 server = ses->server;
1944 else
1945 return -EIO;
1947 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
1948 if (rc)
1949 return rc;
1951 req->PersistentFileId = persistent_fid;
1952 req->VolatileFileId = volatile_fid;
1954 iov[0].iov_base = (char *)req;
1955 /* 4 for rfc1002 length field */
1956 iov[0].iov_len = get_rfc1002_length(req) + 4;
1958 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1960 if (rc != 0)
1961 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
1963 free_rsp_buf(resp_buftype, iov[0].iov_base);
1964 return rc;
1968 * To form a chain of read requests, any read requests after the first should
1969 * have the end_of_chain boolean set to true.
1971 static int
1972 smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
1973 unsigned int remaining_bytes, int request_type)
1975 int rc = -EACCES;
1976 struct smb2_read_req *req = NULL;
1978 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
1979 if (rc)
1980 return rc;
1981 if (io_parms->tcon->ses->server == NULL)
1982 return -ECONNABORTED;
1984 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
1986 req->PersistentFileId = io_parms->persistent_fid;
1987 req->VolatileFileId = io_parms->volatile_fid;
1988 req->ReadChannelInfoOffset = 0; /* reserved */
1989 req->ReadChannelInfoLength = 0; /* reserved */
1990 req->Channel = 0; /* reserved */
1991 req->MinimumCount = 0;
1992 req->Length = cpu_to_le32(io_parms->length);
1993 req->Offset = cpu_to_le64(io_parms->offset);
1995 if (request_type & CHAINED_REQUEST) {
1996 if (!(request_type & END_OF_CHAIN)) {
1997 /* 4 for rfc1002 length field */
1998 req->hdr.NextCommand =
1999 cpu_to_le32(get_rfc1002_length(req) + 4);
2000 } else /* END_OF_CHAIN */
2001 req->hdr.NextCommand = 0;
2002 if (request_type & RELATED_REQUEST) {
2003 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2005 * Related requests use info from previous read request
2006 * in chain.
2008 req->hdr.SessionId = 0xFFFFFFFF;
2009 req->hdr.TreeId = 0xFFFFFFFF;
2010 req->PersistentFileId = 0xFFFFFFFF;
2011 req->VolatileFileId = 0xFFFFFFFF;
2014 if (remaining_bytes > io_parms->length)
2015 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2016 else
2017 req->RemainingBytes = 0;
2019 iov[0].iov_base = (char *)req;
2020 /* 4 for rfc1002 length field */
2021 iov[0].iov_len = get_rfc1002_length(req) + 4;
2022 return rc;
2025 static void
2026 smb2_readv_callback(struct mid_q_entry *mid)
2028 struct cifs_readdata *rdata = mid->callback_data;
2029 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2030 struct TCP_Server_Info *server = tcon->ses->server;
2031 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
2032 unsigned int credits_received = 1;
2033 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
2034 .rq_nvec = 1,
2035 .rq_pages = rdata->pages,
2036 .rq_npages = rdata->nr_pages,
2037 .rq_pagesz = rdata->pagesz,
2038 .rq_tailsz = rdata->tailsz };
2040 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2041 __func__, mid->mid, mid->mid_state, rdata->result,
2042 rdata->bytes);
2044 switch (mid->mid_state) {
2045 case MID_RESPONSE_RECEIVED:
2046 credits_received = le16_to_cpu(buf->CreditRequest);
2047 /* result already set, check signature */
2048 if (server->sign) {
2049 int rc;
2051 rc = smb2_verify_signature(&rqst, server);
2052 if (rc)
2053 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2054 rc);
2056 /* FIXME: should this be counted toward the initiating task? */
2057 task_io_account_read(rdata->got_bytes);
2058 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2059 break;
2060 case MID_REQUEST_SUBMITTED:
2061 case MID_RETRY_NEEDED:
2062 rdata->result = -EAGAIN;
2063 if (server->sign && rdata->got_bytes)
2064 /* reset bytes number since we can not check a sign */
2065 rdata->got_bytes = 0;
2066 /* FIXME: should this be counted toward the initiating task? */
2067 task_io_account_read(rdata->got_bytes);
2068 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2069 break;
2070 default:
2071 if (rdata->result != -ENODATA)
2072 rdata->result = -EIO;
2075 if (rdata->result)
2076 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2078 queue_work(cifsiod_wq, &rdata->work);
2079 mutex_lock(&server->srv_mutex);
2080 DeleteMidQEntry(mid);
2081 mutex_unlock(&server->srv_mutex);
2082 add_credits(server, credits_received, 0);
2085 /* smb2_async_readv - send an async write, and set up mid to handle result */
2087 smb2_async_readv(struct cifs_readdata *rdata)
2089 int rc, flags = 0;
2090 struct smb2_hdr *buf;
2091 struct cifs_io_parms io_parms;
2092 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
2093 .rq_nvec = 1 };
2094 struct TCP_Server_Info *server;
2096 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2097 __func__, rdata->offset, rdata->bytes);
2099 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2100 io_parms.offset = rdata->offset;
2101 io_parms.length = rdata->bytes;
2102 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2103 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2104 io_parms.pid = rdata->pid;
2106 server = io_parms.tcon->ses->server;
2108 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
2109 if (rc) {
2110 if (rc == -EAGAIN && rdata->credits) {
2111 /* credits was reset by reconnect */
2112 rdata->credits = 0;
2113 /* reduce in_flight value since we won't send the req */
2114 spin_lock(&server->req_lock);
2115 server->in_flight--;
2116 spin_unlock(&server->req_lock);
2118 return rc;
2121 buf = (struct smb2_hdr *)rdata->iov.iov_base;
2122 /* 4 for rfc1002 length field */
2123 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
2125 if (rdata->credits) {
2126 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2127 SMB2_MAX_BUFFER_SIZE));
2128 buf->CreditRequest = buf->CreditCharge;
2129 spin_lock(&server->req_lock);
2130 server->credits += rdata->credits -
2131 le16_to_cpu(buf->CreditCharge);
2132 spin_unlock(&server->req_lock);
2133 wake_up(&server->request_q);
2134 flags = CIFS_HAS_CREDITS;
2137 kref_get(&rdata->refcount);
2138 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
2139 cifs_readv_receive, smb2_readv_callback,
2140 rdata, flags);
2141 if (rc) {
2142 kref_put(&rdata->refcount, cifs_readdata_release);
2143 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2146 cifs_small_buf_release(buf);
2147 return rc;
2151 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2152 unsigned int *nbytes, char **buf, int *buf_type)
2154 int resp_buftype, rc = -EACCES;
2155 struct smb2_read_rsp *rsp = NULL;
2156 struct kvec iov[1];
2158 *nbytes = 0;
2159 rc = smb2_new_read_req(iov, io_parms, 0, 0);
2160 if (rc)
2161 return rc;
2163 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2164 &resp_buftype, CIFS_LOG_ERROR);
2166 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
2168 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
2169 free_rsp_buf(resp_buftype, iov[0].iov_base);
2170 return 0;
2173 if (rc) {
2174 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2175 cifs_dbg(VFS, "Send error in read = %d\n", rc);
2176 } else {
2177 *nbytes = le32_to_cpu(rsp->DataLength);
2178 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2179 (*nbytes > io_parms->length)) {
2180 cifs_dbg(FYI, "bad length %d for count %d\n",
2181 *nbytes, io_parms->length);
2182 rc = -EIO;
2183 *nbytes = 0;
2187 if (*buf) {
2188 memcpy(*buf, (char *)rsp->hdr.ProtocolId + rsp->DataOffset,
2189 *nbytes);
2190 free_rsp_buf(resp_buftype, iov[0].iov_base);
2191 } else if (resp_buftype != CIFS_NO_BUFFER) {
2192 *buf = iov[0].iov_base;
2193 if (resp_buftype == CIFS_SMALL_BUFFER)
2194 *buf_type = CIFS_SMALL_BUFFER;
2195 else if (resp_buftype == CIFS_LARGE_BUFFER)
2196 *buf_type = CIFS_LARGE_BUFFER;
2198 return rc;
2202 * Check the mid_state and signature on received buffer (if any), and queue the
2203 * workqueue completion task.
2205 static void
2206 smb2_writev_callback(struct mid_q_entry *mid)
2208 struct cifs_writedata *wdata = mid->callback_data;
2209 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2210 struct TCP_Server_Info *server = tcon->ses->server;
2211 unsigned int written;
2212 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2213 unsigned int credits_received = 1;
2215 switch (mid->mid_state) {
2216 case MID_RESPONSE_RECEIVED:
2217 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
2218 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2219 if (wdata->result != 0)
2220 break;
2222 written = le32_to_cpu(rsp->DataLength);
2224 * Mask off high 16 bits when bytes written as returned
2225 * by the server is greater than bytes requested by the
2226 * client. OS/2 servers are known to set incorrect
2227 * CountHigh values.
2229 if (written > wdata->bytes)
2230 written &= 0xFFFF;
2232 if (written < wdata->bytes)
2233 wdata->result = -ENOSPC;
2234 else
2235 wdata->bytes = written;
2236 break;
2237 case MID_REQUEST_SUBMITTED:
2238 case MID_RETRY_NEEDED:
2239 wdata->result = -EAGAIN;
2240 break;
2241 default:
2242 wdata->result = -EIO;
2243 break;
2246 if (wdata->result)
2247 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2249 queue_work(cifsiod_wq, &wdata->work);
2250 mutex_lock(&server->srv_mutex);
2251 DeleteMidQEntry(mid);
2252 mutex_unlock(&server->srv_mutex);
2253 add_credits(tcon->ses->server, credits_received, 0);
2256 /* smb2_async_writev - send an async write, and set up mid to handle result */
2258 smb2_async_writev(struct cifs_writedata *wdata,
2259 void (*release)(struct kref *kref))
2261 int rc = -EACCES, flags = 0;
2262 struct smb2_write_req *req = NULL;
2263 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2264 struct TCP_Server_Info *server = tcon->ses->server;
2265 struct kvec iov;
2266 struct smb_rqst rqst;
2268 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
2269 if (rc) {
2270 if (rc == -EAGAIN && wdata->credits) {
2271 /* credits was reset by reconnect */
2272 wdata->credits = 0;
2273 /* reduce in_flight value since we won't send the req */
2274 spin_lock(&server->req_lock);
2275 server->in_flight--;
2276 spin_unlock(&server->req_lock);
2278 goto async_writev_out;
2281 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2283 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2284 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2285 req->WriteChannelInfoOffset = 0;
2286 req->WriteChannelInfoLength = 0;
2287 req->Channel = 0;
2288 req->Offset = cpu_to_le64(wdata->offset);
2289 /* 4 for rfc1002 length field */
2290 req->DataOffset = cpu_to_le16(
2291 offsetof(struct smb2_write_req, Buffer) - 4);
2292 req->RemainingBytes = 0;
2294 /* 4 for rfc1002 length field and 1 for Buffer */
2295 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2296 iov.iov_base = req;
2298 rqst.rq_iov = &iov;
2299 rqst.rq_nvec = 1;
2300 rqst.rq_pages = wdata->pages;
2301 rqst.rq_npages = wdata->nr_pages;
2302 rqst.rq_pagesz = wdata->pagesz;
2303 rqst.rq_tailsz = wdata->tailsz;
2305 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2306 wdata->offset, wdata->bytes);
2308 req->Length = cpu_to_le32(wdata->bytes);
2310 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2312 if (wdata->credits) {
2313 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2314 SMB2_MAX_BUFFER_SIZE));
2315 req->hdr.CreditRequest = req->hdr.CreditCharge;
2316 spin_lock(&server->req_lock);
2317 server->credits += wdata->credits -
2318 le16_to_cpu(req->hdr.CreditCharge);
2319 spin_unlock(&server->req_lock);
2320 wake_up(&server->request_q);
2321 flags = CIFS_HAS_CREDITS;
2324 kref_get(&wdata->refcount);
2325 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2326 flags);
2328 if (rc) {
2329 kref_put(&wdata->refcount, release);
2330 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2333 async_writev_out:
2334 cifs_small_buf_release(req);
2335 return rc;
2339 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2340 * The length field from io_parms must be at least 1 and indicates a number of
2341 * elements with data to write that begins with position 1 in iov array. All
2342 * data length is specified by count.
2345 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2346 unsigned int *nbytes, struct kvec *iov, int n_vec)
2348 int rc = 0;
2349 struct smb2_write_req *req = NULL;
2350 struct smb2_write_rsp *rsp = NULL;
2351 int resp_buftype;
2352 *nbytes = 0;
2354 if (n_vec < 1)
2355 return rc;
2357 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2358 if (rc)
2359 return rc;
2361 if (io_parms->tcon->ses->server == NULL)
2362 return -ECONNABORTED;
2364 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2366 req->PersistentFileId = io_parms->persistent_fid;
2367 req->VolatileFileId = io_parms->volatile_fid;
2368 req->WriteChannelInfoOffset = 0;
2369 req->WriteChannelInfoLength = 0;
2370 req->Channel = 0;
2371 req->Length = cpu_to_le32(io_parms->length);
2372 req->Offset = cpu_to_le64(io_parms->offset);
2373 /* 4 for rfc1002 length field */
2374 req->DataOffset = cpu_to_le16(
2375 offsetof(struct smb2_write_req, Buffer) - 4);
2376 req->RemainingBytes = 0;
2378 iov[0].iov_base = (char *)req;
2379 /* 4 for rfc1002 length field and 1 for Buffer */
2380 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2382 /* length of entire message including data to be written */
2383 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2385 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2386 &resp_buftype, 0);
2387 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
2389 if (rc) {
2390 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
2391 cifs_dbg(VFS, "Send error in write = %d\n", rc);
2392 } else
2393 *nbytes = le32_to_cpu(rsp->DataLength);
2395 free_rsp_buf(resp_buftype, rsp);
2396 return rc;
2399 static unsigned int
2400 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2402 int len;
2403 unsigned int entrycount = 0;
2404 unsigned int next_offset = 0;
2405 char *entryptr;
2406 FILE_DIRECTORY_INFO *dir_info;
2408 if (bufstart == NULL)
2409 return 0;
2411 entryptr = bufstart;
2413 while (1) {
2414 if (entryptr + next_offset < entryptr ||
2415 entryptr + next_offset > end_of_buf ||
2416 entryptr + next_offset + size > end_of_buf) {
2417 cifs_dbg(VFS, "malformed search entry would overflow\n");
2418 break;
2421 entryptr = entryptr + next_offset;
2422 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
2424 len = le32_to_cpu(dir_info->FileNameLength);
2425 if (entryptr + len < entryptr ||
2426 entryptr + len > end_of_buf ||
2427 entryptr + len + size > end_of_buf) {
2428 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2429 end_of_buf);
2430 break;
2433 *lastentry = entryptr;
2434 entrycount++;
2436 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
2437 if (!next_offset)
2438 break;
2441 return entrycount;
2445 * Readdir/FindFirst
2448 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2449 u64 persistent_fid, u64 volatile_fid, int index,
2450 struct cifs_search_info *srch_inf)
2452 struct smb2_query_directory_req *req;
2453 struct smb2_query_directory_rsp *rsp = NULL;
2454 struct kvec iov[2];
2455 int rc = 0;
2456 int len;
2457 int resp_buftype = CIFS_NO_BUFFER;
2458 unsigned char *bufptr;
2459 struct TCP_Server_Info *server;
2460 struct cifs_ses *ses = tcon->ses;
2461 __le16 asteriks = cpu_to_le16('*');
2462 char *end_of_smb;
2463 unsigned int output_size = CIFSMaxBufSize;
2464 size_t info_buf_size;
2466 if (ses && (ses->server))
2467 server = ses->server;
2468 else
2469 return -EIO;
2471 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2472 if (rc)
2473 return rc;
2475 switch (srch_inf->info_level) {
2476 case SMB_FIND_FILE_DIRECTORY_INFO:
2477 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2478 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2479 break;
2480 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2481 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2482 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2483 break;
2484 default:
2485 cifs_dbg(VFS, "info level %u isn't supported\n",
2486 srch_inf->info_level);
2487 rc = -EINVAL;
2488 goto qdir_exit;
2491 req->FileIndex = cpu_to_le32(index);
2492 req->PersistentFileId = persistent_fid;
2493 req->VolatileFileId = volatile_fid;
2495 len = 0x2;
2496 bufptr = req->Buffer;
2497 memcpy(bufptr, &asteriks, len);
2499 req->FileNameOffset =
2500 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2501 req->FileNameLength = cpu_to_le16(len);
2503 * BB could be 30 bytes or so longer if we used SMB2 specific
2504 * buffer lengths, but this is safe and close enough.
2506 output_size = min_t(unsigned int, output_size, server->maxBuf);
2507 output_size = min_t(unsigned int, output_size, 2 << 15);
2508 req->OutputBufferLength = cpu_to_le32(output_size);
2510 iov[0].iov_base = (char *)req;
2511 /* 4 for RFC1001 length and 1 for Buffer */
2512 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2514 iov[1].iov_base = (char *)(req->Buffer);
2515 iov[1].iov_len = len;
2517 inc_rfc1001_len(req, len - 1 /* Buffer */);
2519 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
2520 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2522 if (rc) {
2523 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2524 srch_inf->endOfSearch = true;
2525 rc = 0;
2527 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2528 goto qdir_exit;
2531 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2532 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2533 info_buf_size);
2534 if (rc)
2535 goto qdir_exit;
2537 srch_inf->unicode = true;
2539 if (srch_inf->ntwrk_buf_start) {
2540 if (srch_inf->smallBuf)
2541 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2542 else
2543 cifs_buf_release(srch_inf->ntwrk_buf_start);
2545 srch_inf->ntwrk_buf_start = (char *)rsp;
2546 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2547 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2548 /* 4 for rfc1002 length field */
2549 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2550 srch_inf->entries_in_buffer =
2551 num_entries(srch_inf->srch_entries_start, end_of_smb,
2552 &srch_inf->last_entry, info_buf_size);
2553 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
2554 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2555 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2556 srch_inf->srch_entries_start, srch_inf->last_entry);
2557 if (resp_buftype == CIFS_LARGE_BUFFER)
2558 srch_inf->smallBuf = false;
2559 else if (resp_buftype == CIFS_SMALL_BUFFER)
2560 srch_inf->smallBuf = true;
2561 else
2562 cifs_dbg(VFS, "illegal search buffer type\n");
2564 return rc;
2566 qdir_exit:
2567 free_rsp_buf(resp_buftype, rsp);
2568 return rc;
2571 static int
2572 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2573 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
2574 unsigned int num, void **data, unsigned int *size)
2576 struct smb2_set_info_req *req;
2577 struct smb2_set_info_rsp *rsp = NULL;
2578 struct kvec *iov;
2579 int rc = 0;
2580 int resp_buftype;
2581 unsigned int i;
2582 struct TCP_Server_Info *server;
2583 struct cifs_ses *ses = tcon->ses;
2585 if (ses && (ses->server))
2586 server = ses->server;
2587 else
2588 return -EIO;
2590 if (!num)
2591 return -EINVAL;
2593 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2594 if (!iov)
2595 return -ENOMEM;
2597 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2598 if (rc) {
2599 kfree(iov);
2600 return rc;
2603 req->hdr.ProcessId = cpu_to_le32(pid);
2605 req->InfoType = SMB2_O_INFO_FILE;
2606 req->FileInfoClass = info_class;
2607 req->PersistentFileId = persistent_fid;
2608 req->VolatileFileId = volatile_fid;
2610 /* 4 for RFC1001 length and 1 for Buffer */
2611 req->BufferOffset =
2612 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2613 req->BufferLength = cpu_to_le32(*size);
2615 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2617 memcpy(req->Buffer, *data, *size);
2619 iov[0].iov_base = (char *)req;
2620 /* 4 for RFC1001 length */
2621 iov[0].iov_len = get_rfc1002_length(req) + 4;
2623 for (i = 1; i < num; i++) {
2624 inc_rfc1001_len(req, size[i]);
2625 le32_add_cpu(&req->BufferLength, size[i]);
2626 iov[i].iov_base = (char *)data[i];
2627 iov[i].iov_len = size[i];
2630 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2631 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2633 if (rc != 0)
2634 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
2636 free_rsp_buf(resp_buftype, rsp);
2637 kfree(iov);
2638 return rc;
2642 SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2643 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2645 struct smb2_file_rename_info info;
2646 void **data;
2647 unsigned int size[2];
2648 int rc;
2649 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2651 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2652 if (!data)
2653 return -ENOMEM;
2655 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2656 /* 0 = fail if target already exists */
2657 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2658 info.FileNameLength = cpu_to_le32(len);
2660 data[0] = &info;
2661 size[0] = sizeof(struct smb2_file_rename_info);
2663 data[1] = target_file;
2664 size[1] = len + 2 /* null */;
2666 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2667 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2668 size);
2669 kfree(data);
2670 return rc;
2674 SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2675 u64 persistent_fid, u64 volatile_fid)
2677 __u8 delete_pending = 1;
2678 void *data;
2679 unsigned int size;
2681 data = &delete_pending;
2682 size = 1; /* sizeof __u8 */
2684 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2685 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2686 &size);
2690 SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2691 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2693 struct smb2_file_link_info info;
2694 void **data;
2695 unsigned int size[2];
2696 int rc;
2697 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2699 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2700 if (!data)
2701 return -ENOMEM;
2703 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2704 /* 0 = fail if link already exists */
2705 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2706 info.FileNameLength = cpu_to_le32(len);
2708 data[0] = &info;
2709 size[0] = sizeof(struct smb2_file_link_info);
2711 data[1] = target_file;
2712 size[1] = len + 2 /* null */;
2714 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2715 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
2716 kfree(data);
2717 return rc;
2721 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2722 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
2724 struct smb2_file_eof_info info;
2725 void *data;
2726 unsigned int size;
2728 info.EndOfFile = *eof;
2730 data = &info;
2731 size = sizeof(struct smb2_file_eof_info);
2733 if (is_falloc)
2734 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2735 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2736 else
2737 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2738 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
2742 SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2743 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2745 unsigned int size;
2746 size = sizeof(FILE_BASIC_INFO);
2747 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2748 current->tgid, FILE_BASIC_INFORMATION, 1,
2749 (void **)&buf, &size);
2753 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2754 const u64 persistent_fid, const u64 volatile_fid,
2755 __u8 oplock_level)
2757 int rc;
2758 struct smb2_oplock_break *req = NULL;
2760 cifs_dbg(FYI, "SMB2_oplock_break\n");
2761 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2763 if (rc)
2764 return rc;
2766 req->VolatileFid = volatile_fid;
2767 req->PersistentFid = persistent_fid;
2768 req->OplockLevel = oplock_level;
2769 req->hdr.CreditRequest = cpu_to_le16(1);
2771 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2772 /* SMB2 buffer freed by function above */
2774 if (rc) {
2775 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
2776 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
2779 return rc;
2782 static void
2783 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2784 struct kstatfs *kst)
2786 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2787 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2788 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2789 kst->f_bfree = kst->f_bavail =
2790 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2791 return;
2794 static int
2795 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2796 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2798 int rc;
2799 struct smb2_query_info_req *req;
2801 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
2803 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2804 return -EIO;
2806 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2807 if (rc)
2808 return rc;
2810 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2811 req->FileInfoClass = level;
2812 req->PersistentFileId = persistent_fid;
2813 req->VolatileFileId = volatile_fid;
2814 /* 4 for rfc1002 length field and 1 for pad */
2815 req->InputBufferOffset =
2816 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2817 req->OutputBufferLength = cpu_to_le32(
2818 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2820 iov->iov_base = (char *)req;
2821 /* 4 for rfc1002 length field */
2822 iov->iov_len = get_rfc1002_length(req) + 4;
2823 return 0;
2827 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2828 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2830 struct smb2_query_info_rsp *rsp = NULL;
2831 struct kvec iov;
2832 int rc = 0;
2833 int resp_buftype;
2834 struct cifs_ses *ses = tcon->ses;
2835 struct smb2_fs_full_size_info *info = NULL;
2837 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2838 sizeof(struct smb2_fs_full_size_info),
2839 persistent_fid, volatile_fid);
2840 if (rc)
2841 return rc;
2843 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2844 if (rc) {
2845 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2846 goto qfsinf_exit;
2848 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2850 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
2851 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
2852 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2853 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2854 sizeof(struct smb2_fs_full_size_info));
2855 if (!rc)
2856 copy_fs_info_to_kstatfs(info, fsdata);
2858 qfsinf_exit:
2859 free_rsp_buf(resp_buftype, iov.iov_base);
2860 return rc;
2864 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
2865 u64 persistent_fid, u64 volatile_fid, int level)
2867 struct smb2_query_info_rsp *rsp = NULL;
2868 struct kvec iov;
2869 int rc = 0;
2870 int resp_buftype, max_len, min_len;
2871 struct cifs_ses *ses = tcon->ses;
2872 unsigned int rsp_len, offset;
2874 if (level == FS_DEVICE_INFORMATION) {
2875 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2876 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2877 } else if (level == FS_ATTRIBUTE_INFORMATION) {
2878 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
2879 min_len = MIN_FS_ATTR_INFO_SIZE;
2880 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
2881 max_len = sizeof(struct smb3_fs_ss_info);
2882 min_len = sizeof(struct smb3_fs_ss_info);
2883 } else {
2884 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
2885 return -EINVAL;
2888 rc = build_qfs_info_req(&iov, tcon, level, max_len,
2889 persistent_fid, volatile_fid);
2890 if (rc)
2891 return rc;
2893 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2894 if (rc) {
2895 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2896 goto qfsattr_exit;
2898 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2900 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
2901 offset = le16_to_cpu(rsp->OutputBufferOffset);
2902 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
2903 if (rc)
2904 goto qfsattr_exit;
2906 if (level == FS_ATTRIBUTE_INFORMATION)
2907 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
2908 + (char *)&rsp->hdr, min_t(unsigned int,
2909 rsp_len, max_len));
2910 else if (level == FS_DEVICE_INFORMATION)
2911 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
2912 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
2913 else if (level == FS_SECTOR_SIZE_INFORMATION) {
2914 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
2915 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
2916 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
2917 tcon->perf_sector_size =
2918 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
2921 qfsattr_exit:
2922 free_rsp_buf(resp_buftype, iov.iov_base);
2923 return rc;
2927 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2928 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2929 const __u32 num_lock, struct smb2_lock_element *buf)
2931 int rc = 0;
2932 struct smb2_lock_req *req = NULL;
2933 struct kvec iov[2];
2934 int resp_buf_type;
2935 unsigned int count;
2937 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
2939 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
2940 if (rc)
2941 return rc;
2943 req->hdr.ProcessId = cpu_to_le32(pid);
2944 req->LockCount = cpu_to_le16(num_lock);
2946 req->PersistentFileId = persist_fid;
2947 req->VolatileFileId = volatile_fid;
2949 count = num_lock * sizeof(struct smb2_lock_element);
2950 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
2952 iov[0].iov_base = (char *)req;
2953 /* 4 for rfc1002 length field and count for all locks */
2954 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
2955 iov[1].iov_base = (char *)buf;
2956 iov[1].iov_len = count;
2958 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2959 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2960 if (rc) {
2961 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
2962 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
2965 return rc;
2969 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
2970 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2971 const __u64 length, const __u64 offset, const __u32 lock_flags,
2972 const bool wait)
2974 struct smb2_lock_element lock;
2976 lock.Offset = cpu_to_le64(offset);
2977 lock.Length = cpu_to_le64(length);
2978 lock.Flags = cpu_to_le32(lock_flags);
2979 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
2980 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
2982 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
2986 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
2987 __u8 *lease_key, const __le32 lease_state)
2989 int rc;
2990 struct smb2_lease_ack *req = NULL;
2992 cifs_dbg(FYI, "SMB2_lease_break\n");
2993 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2995 if (rc)
2996 return rc;
2998 req->hdr.CreditRequest = cpu_to_le16(1);
2999 req->StructureSize = cpu_to_le16(36);
3000 inc_rfc1001_len(req, 12);
3002 memcpy(req->LeaseKey, lease_key, 16);
3003 req->LeaseState = lease_state;
3005 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
3006 /* SMB2 buffer freed by function above */
3008 if (rc) {
3009 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3010 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
3013 return rc;