4 * Copyright (C) International Business Machines Corp., 2009, 2013
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 */
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>
41 #include "cifsproto.h"
42 #include "smb2proto.h"
43 #include "cifs_unicode.h"
44 #include "cifs_debug.h"
46 #include "smb2status.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,
62 /* SMB2_TREE_CONNECT */ 9,
63 /* SMB2_TREE_DISCONNECT */ 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 */
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 struct smb2_sync_hdr *shdr = get_sync_hdr(hdr);
87 char *temp = (char *)hdr;
88 /* lookup word count ie StructureSize from table */
89 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_cmd)];
92 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
93 * largest operations (Create)
97 /* Note this is only network field converted to big endian */
98 hdr->smb2_buf_length =
99 cpu_to_be32(parmsize + sizeof(struct smb2_sync_hdr));
101 shdr->ProtocolId = SMB2_PROTO_NUMBER;
102 shdr->StructureSize = cpu_to_le16(64);
103 shdr->Command = smb2_cmd;
104 if (tcon && tcon->ses && tcon->ses->server) {
105 struct TCP_Server_Info *server = tcon->ses->server;
107 spin_lock(&server->req_lock);
108 /* Request up to 2 credits but don't go over the limit. */
109 if (server->credits >= server->max_credits)
110 shdr->CreditRequest = cpu_to_le16(0);
112 shdr->CreditRequest = cpu_to_le16(
113 min_t(int, server->max_credits -
114 server->credits, 2));
115 spin_unlock(&server->req_lock);
117 shdr->CreditRequest = cpu_to_le16(2);
119 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
124 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
125 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
126 if ((tcon->ses) && (tcon->ses->server) &&
127 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
128 shdr->CreditCharge = cpu_to_le16(1);
129 /* else CreditCharge MBZ */
131 shdr->TreeId = tcon->tid;
132 /* Uid is not converted */
134 shdr->SessionId = tcon->ses->Suid;
137 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
138 * to pass the path on the Open SMB prefixed by \\server\share.
139 * Not sure when we would need to do the augmented path (if ever) and
140 * setting this flag breaks the SMB2 open operation since it is
141 * illegal to send an empty path name (without \\server\share prefix)
142 * when the DFS flag is set in the SMB open header. We could
143 * consider setting the flag on all operations other than open
144 * but it is safer to net set it for now.
146 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
147 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
149 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign)
150 shdr->Flags |= SMB2_FLAGS_SIGNED;
152 pdu->StructureSize2 = cpu_to_le16(parmsize);
157 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
160 struct nls_table *nls_codepage;
161 struct cifs_ses *ses;
162 struct TCP_Server_Info *server;
165 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
166 * check for tcp and smb session status done differently
167 * for those three - in the calling routine.
172 if (smb2_command == SMB2_TREE_CONNECT)
175 if (tcon->tidStatus == CifsExiting) {
177 * only tree disconnect, open, and write,
178 * (and ulogoff which does not have tcon)
179 * are allowed as we start force umount.
181 if ((smb2_command != SMB2_WRITE) &&
182 (smb2_command != SMB2_CREATE) &&
183 (smb2_command != SMB2_TREE_DISCONNECT)) {
184 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
189 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
190 (!tcon->ses->server))
194 server = ses->server;
197 * Give demultiplex thread up to 10 seconds to reconnect, should be
198 * greater than cifs socket timeout which is 7 seconds
200 while (server->tcpStatus == CifsNeedReconnect) {
202 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
203 * here since they are implicitly done when session drops.
205 switch (smb2_command) {
207 * BB Should we keep oplock break and add flush to exceptions?
209 case SMB2_TREE_DISCONNECT:
212 case SMB2_OPLOCK_BREAK:
216 wait_event_interruptible_timeout(server->response_q,
217 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
219 /* are we still trying to reconnect? */
220 if (server->tcpStatus != CifsNeedReconnect)
224 * on "soft" mounts we wait once. Hard mounts keep
225 * retrying until process is killed or server comes
229 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
234 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
237 nls_codepage = load_nls_default();
240 * need to prevent multiple threads trying to simultaneously reconnect
241 * the same SMB session
243 mutex_lock(&tcon->ses->session_mutex);
244 rc = cifs_negotiate_protocol(0, tcon->ses);
245 if (!rc && tcon->ses->need_reconnect)
246 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
248 if (rc || !tcon->need_reconnect) {
249 mutex_unlock(&tcon->ses->session_mutex);
253 cifs_mark_open_files_invalid(tcon);
254 if (tcon->use_persistent)
255 tcon->need_reopen_files = true;
257 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
258 mutex_unlock(&tcon->ses->session_mutex);
260 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
264 if (smb2_command != SMB2_INTERNAL_CMD)
265 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
267 atomic_inc(&tconInfoReconnectCount);
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) {
283 case SMB2_QUERY_DIRECTORY:
284 case SMB2_CHANGE_NOTIFY:
285 case SMB2_QUERY_INFO:
289 unload_nls(nls_codepage);
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.
299 small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
304 rc = smb2_reconnect(smb2_command, tcon);
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? */
315 smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
318 #ifdef CONFIG_CIFS_STATS2
319 uint16_t com_code = le16_to_cpu(smb2_command);
320 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
322 cifs_stats_inc(&tcon->num_smbs_sent);
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)
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;
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;
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) + 2
371 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
374 static void assemble_neg_contexts(struct smb2_negotiate_req *req)
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
397 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
399 struct smb2_negotiate_req *req;
400 struct smb2_negotiate_rsp *rsp;
405 struct TCP_Server_Info *server = ses->server;
406 int blob_offset, blob_length;
408 int flags = CIFS_NEG_OP;
410 cifs_dbg(FYI, "Negotiate protocol\n");
413 WARN(1, "%s: server is NULL!\n", __func__);
417 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
421 req->hdr.sync_hdr.SessionId = 0;
423 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
425 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
426 inc_rfc1001_len(req, 2);
428 /* only one of SMB2 signing flags may be set in SMB2 request */
430 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
431 else if (global_secflags & CIFSSEC_MAY_SIGN)
432 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
434 req->SecurityMode = 0;
436 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
438 /* ClientGUID must be zero for SMB2.02 dialect */
439 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
440 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
442 memcpy(req->ClientGUID, server->client_guid,
443 SMB2_CLIENT_GUID_SIZE);
444 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
445 assemble_neg_contexts(req);
447 iov[0].iov_base = (char *)req;
448 /* 4 for rfc1002 length field */
449 iov[0].iov_len = get_rfc1002_length(req) + 4;
451 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
452 cifs_small_buf_release(req);
453 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
455 * No tcon so can't do
456 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
461 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
463 /* BB we may eventually want to match the negotiated vs. requested
464 dialect, even though we are only requesting one at a time */
465 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
466 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
467 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
468 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
469 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
470 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
471 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
472 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
473 #ifdef CONFIG_CIFS_SMB311
474 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
475 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
478 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
479 le16_to_cpu(rsp->DialectRevision));
483 server->dialect = le16_to_cpu(rsp->DialectRevision);
485 /* SMB2 only has an extended negflavor */
486 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
487 /* set it to the maximum buffer size value we can send with 1 credit */
488 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
489 SMB2_MAX_BUFFER_SIZE);
490 server->max_read = le32_to_cpu(rsp->MaxReadSize);
491 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
492 /* BB Do we need to validate the SecurityMode? */
493 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
494 server->capabilities = le32_to_cpu(rsp->Capabilities);
496 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
498 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
501 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
503 * ses->sectype = RawNTLMSSP;
504 * but for time being this is our only auth choice so doesn't matter.
505 * We just found a server which sets blob length to zero expecting raw.
507 if (blob_length == 0)
508 cifs_dbg(FYI, "missing security blob on negprot\n");
510 rc = cifs_enable_signing(server, ses->sign);
514 rc = decode_negTokenInit(security_blob, blob_length, server);
521 free_rsp_buf(resp_buftype, rsp);
525 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
528 struct validate_negotiate_info_req vneg_inbuf;
529 struct validate_negotiate_info_rsp *pneg_rsp;
532 cifs_dbg(FYI, "validate negotiate\n");
535 * validation ioctl must be signed, so no point sending this if we
536 * can not sign it. We could eventually change this to selectively
537 * sign just this, the first and only signed request on a connection.
538 * This is good enough for now since a user who wants better security
539 * would also enable signing on the mount. Having validation of
540 * negotiate info for signed connections helps reduce attack vectors
542 if (tcon->ses->server->sign == false)
543 return 0; /* validation requires signing */
545 vneg_inbuf.Capabilities =
546 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
547 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
548 SMB2_CLIENT_GUID_SIZE);
551 vneg_inbuf.SecurityMode =
552 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
553 else if (global_secflags & CIFSSEC_MAY_SIGN)
554 vneg_inbuf.SecurityMode =
555 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
557 vneg_inbuf.SecurityMode = 0;
559 vneg_inbuf.DialectCount = cpu_to_le16(1);
560 vneg_inbuf.Dialects[0] =
561 cpu_to_le16(tcon->ses->server->vals->protocol_id);
563 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
564 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
565 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
566 (char **)&pneg_rsp, &rsplen);
569 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
573 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
574 cifs_dbg(VFS, "invalid size of protocol negotiate response\n");
578 /* check validate negotiate info response matches what we got earlier */
579 if (pneg_rsp->Dialect !=
580 cpu_to_le16(tcon->ses->server->vals->protocol_id))
583 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
586 /* do not validate server guid because not saved at negprot time yet */
588 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
589 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
592 /* validate negotiate successful */
593 cifs_dbg(FYI, "validate negotiate info successful\n");
597 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
601 struct SMB2_sess_data {
603 struct cifs_ses *ses;
604 struct nls_table *nls_cp;
605 void (*func)(struct SMB2_sess_data *);
607 u64 previous_session;
609 /* we will send the SMB in three pieces:
610 * a fixed length beginning part, an optional
611 * SPNEGO blob (which can be zero length), and a
612 * last part which will include the strings
613 * and rest of bcc area. This allows us to avoid
614 * a large buffer 17K allocation
621 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
624 struct cifs_ses *ses = sess_data->ses;
625 struct smb2_sess_setup_req *req;
626 struct TCP_Server_Info *server = ses->server;
628 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
632 /* First session, not a reauthenticate */
633 req->hdr.sync_hdr.SessionId = 0;
635 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
636 req->PreviousSessionId = sess_data->previous_session;
638 req->Flags = 0; /* MBZ */
639 /* to enable echos and oplocks */
640 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(3);
642 /* only one of SMB2 signing flags may be set in SMB2 request */
644 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
645 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
646 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
648 req->SecurityMode = 0;
650 req->Capabilities = 0;
651 req->Channel = 0; /* MBZ */
653 sess_data->iov[0].iov_base = (char *)req;
654 /* 4 for rfc1002 length field and 1 for pad */
655 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
657 * This variable will be used to clear the buffer
658 * allocated above in case of any error in the calling function.
660 sess_data->buf0_type = CIFS_SMALL_BUFFER;
666 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
668 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
669 sess_data->buf0_type = CIFS_NO_BUFFER;
673 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
676 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
677 struct kvec rsp_iov = { NULL, 0 };
679 /* Testing shows that buffer offset must be at location of Buffer[0] */
680 req->SecurityBufferOffset =
681 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
682 1 /* pad */ - 4 /* rfc1001 len */);
683 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
685 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
687 /* BB add code to build os and lm fields */
689 rc = SendReceive2(sess_data->xid, sess_data->ses,
691 &sess_data->buf0_type,
692 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
693 cifs_small_buf_release(sess_data->iov[0].iov_base);
694 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
700 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
703 struct cifs_ses *ses = sess_data->ses;
705 mutex_lock(&ses->server->srv_mutex);
706 if (ses->server->sign && ses->server->ops->generate_signingkey) {
707 rc = ses->server->ops->generate_signingkey(ses);
708 kfree(ses->auth_key.response);
709 ses->auth_key.response = NULL;
712 "SMB3 session key generation failed\n");
713 mutex_unlock(&ses->server->srv_mutex);
717 if (!ses->server->session_estab) {
718 ses->server->sequence_number = 0x2;
719 ses->server->session_estab = true;
721 mutex_unlock(&ses->server->srv_mutex);
723 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
724 spin_lock(&GlobalMid_Lock);
725 ses->status = CifsGood;
726 ses->need_reconnect = false;
727 spin_unlock(&GlobalMid_Lock);
730 if (!ses->server->sign) {
731 kfree(ses->auth_key.response);
732 ses->auth_key.response = NULL;
737 #ifdef CONFIG_CIFS_UPCALL
739 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
742 struct cifs_ses *ses = sess_data->ses;
743 struct cifs_spnego_msg *msg;
744 struct key *spnego_key = NULL;
745 struct smb2_sess_setup_rsp *rsp = NULL;
747 rc = SMB2_sess_alloc_buffer(sess_data);
751 spnego_key = cifs_get_spnego_key(ses);
752 if (IS_ERR(spnego_key)) {
753 rc = PTR_ERR(spnego_key);
758 msg = spnego_key->payload.data[0];
760 * check version field to make sure that cifs.upcall is
761 * sending us a response in an expected form
763 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
765 "bad cifs.upcall version. Expected %d got %d",
766 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
768 goto out_put_spnego_key;
771 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
773 if (!ses->auth_key.response) {
775 "Kerberos can't allocate (%u bytes) memory",
778 goto out_put_spnego_key;
780 ses->auth_key.len = msg->sesskey_len;
782 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
783 sess_data->iov[1].iov_len = msg->secblob_len;
785 rc = SMB2_sess_sendreceive(sess_data);
787 goto out_put_spnego_key;
789 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
790 ses->Suid = rsp->hdr.sync_hdr.SessionId;
792 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
793 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
794 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
796 rc = SMB2_sess_establish_session(sess_data);
798 key_invalidate(spnego_key);
801 sess_data->result = rc;
802 sess_data->func = NULL;
803 SMB2_sess_free_buffer(sess_data);
807 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
809 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
810 sess_data->result = -EOPNOTSUPP;
811 sess_data->func = NULL;
816 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
819 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
822 struct cifs_ses *ses = sess_data->ses;
823 struct smb2_sess_setup_rsp *rsp = NULL;
824 char *ntlmssp_blob = NULL;
825 bool use_spnego = false; /* else use raw ntlmssp */
829 * If memory allocation is successful, caller of this function
832 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
837 ses->ntlmssp->sesskey_per_smbsess = true;
839 rc = SMB2_sess_alloc_buffer(sess_data);
843 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
845 if (ntlmssp_blob == NULL) {
850 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
852 /* BB eventually need to add this */
853 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
857 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
858 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
860 sess_data->iov[1].iov_base = ntlmssp_blob;
861 sess_data->iov[1].iov_len = blob_length;
863 rc = SMB2_sess_sendreceive(sess_data);
864 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
866 /* If true, rc here is expected and not an error */
867 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
868 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
874 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
875 le16_to_cpu(rsp->SecurityBufferOffset)) {
876 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
877 le16_to_cpu(rsp->SecurityBufferOffset));
881 rc = decode_ntlmssp_challenge(rsp->Buffer,
882 le16_to_cpu(rsp->SecurityBufferLength), ses);
886 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
889 ses->Suid = rsp->hdr.sync_hdr.SessionId;
890 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
891 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
892 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
896 SMB2_sess_free_buffer(sess_data);
898 sess_data->result = 0;
899 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
905 sess_data->result = rc;
906 sess_data->func = NULL;
910 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
913 struct cifs_ses *ses = sess_data->ses;
914 struct smb2_sess_setup_req *req;
915 struct smb2_sess_setup_rsp *rsp = NULL;
916 unsigned char *ntlmssp_blob = NULL;
917 bool use_spnego = false; /* else use raw ntlmssp */
920 rc = SMB2_sess_alloc_buffer(sess_data);
924 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
925 req->hdr.sync_hdr.SessionId = ses->Suid;
927 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
930 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
935 /* BB eventually need to add this */
936 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
940 sess_data->iov[1].iov_base = ntlmssp_blob;
941 sess_data->iov[1].iov_len = blob_length;
943 rc = SMB2_sess_sendreceive(sess_data);
947 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
949 ses->Suid = rsp->hdr.sync_hdr.SessionId;
950 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
951 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
952 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
954 rc = SMB2_sess_establish_session(sess_data);
957 SMB2_sess_free_buffer(sess_data);
960 sess_data->result = rc;
961 sess_data->func = NULL;
965 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
967 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
968 ses->sectype = RawNTLMSSP;
970 switch (ses->sectype) {
972 sess_data->func = SMB2_auth_kerberos;
975 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
978 cifs_dbg(VFS, "secType %d not supported!\n", ses->sectype);
986 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
987 const struct nls_table *nls_cp)
990 struct TCP_Server_Info *server = ses->server;
991 struct SMB2_sess_data *sess_data;
993 cifs_dbg(FYI, "Session Setup\n");
996 WARN(1, "%s: server is NULL!\n", __func__);
1000 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1004 rc = SMB2_select_sec(ses, sess_data);
1007 sess_data->xid = xid;
1008 sess_data->ses = ses;
1009 sess_data->buf0_type = CIFS_NO_BUFFER;
1010 sess_data->nls_cp = (struct nls_table *) nls_cp;
1012 while (sess_data->func)
1013 sess_data->func(sess_data);
1015 rc = sess_data->result;
1022 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1024 struct smb2_logoff_req *req; /* response is also trivial struct */
1026 struct TCP_Server_Info *server;
1028 cifs_dbg(FYI, "disconnect session %p\n", ses);
1030 if (ses && (ses->server))
1031 server = ses->server;
1035 /* no need to send SMB logoff if uid already closed due to reconnect */
1036 if (ses->need_reconnect)
1037 goto smb2_session_already_dead;
1039 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1043 /* since no tcon, smb2_init can not do this, so do here */
1044 req->hdr.sync_hdr.SessionId = ses->Suid;
1046 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1048 rc = SendReceiveNoRsp(xid, ses, (char *) req, 0);
1049 cifs_small_buf_release(req);
1051 * No tcon so can't do
1052 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1055 smb2_session_already_dead:
1059 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1061 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1064 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1066 /* These are similar values to what Windows uses */
1067 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1069 tcon->max_chunks = 256;
1070 tcon->max_bytes_chunk = 1048576;
1071 tcon->max_bytes_copy = 16777216;
1075 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1076 struct cifs_tcon *tcon, const struct nls_table *cp)
1078 struct smb2_tree_connect_req *req;
1079 struct smb2_tree_connect_rsp *rsp = NULL;
1081 struct kvec rsp_iov;
1085 struct TCP_Server_Info *server;
1086 __le16 *unc_path = NULL;
1088 cifs_dbg(FYI, "TCON\n");
1090 if ((ses->server) && tree)
1091 server = ses->server;
1095 if (tcon && tcon->bad_network_name)
1098 if ((tcon && tcon->seal) &&
1099 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
1100 cifs_dbg(VFS, "encryption requested but no server support");
1104 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1105 if (unc_path == NULL)
1108 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1110 if (unc_path_len < 2) {
1115 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1122 /* since no tcon, smb2_init can not do this, so do here */
1123 req->hdr.sync_hdr.SessionId = ses->Suid;
1124 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
1125 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
1128 iov[0].iov_base = (char *)req;
1129 /* 4 for rfc1002 length field and 1 for pad */
1130 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1132 /* Testing shows that buffer offset must be at location of Buffer[0] */
1133 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1134 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1135 req->PathLength = cpu_to_le16(unc_path_len - 2);
1136 iov[1].iov_base = unc_path;
1137 iov[1].iov_len = unc_path_len;
1139 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1141 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0, &rsp_iov);
1142 cifs_small_buf_release(req);
1143 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1147 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1148 tcon->need_reconnect = true;
1150 goto tcon_error_exit;
1154 ses->ipc_tid = rsp->hdr.sync_hdr.TreeId;
1158 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
1159 cifs_dbg(FYI, "connection to disk share\n");
1160 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
1162 cifs_dbg(FYI, "connection to pipe share\n");
1163 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1165 cifs_dbg(FYI, "connection to printer\n");
1167 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1169 goto tcon_error_exit;
1172 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1173 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1174 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1175 tcon->tidStatus = CifsGood;
1176 tcon->need_reconnect = false;
1177 tcon->tid = rsp->hdr.sync_hdr.TreeId;
1178 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1180 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1181 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1182 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1183 init_copy_chunk_defaults(tcon);
1184 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1185 cifs_dbg(VFS, "Encrypted shares not supported");
1186 if (tcon->ses->server->ops->validate_negotiate)
1187 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1189 free_rsp_buf(resp_buftype, rsp);
1194 if (rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1195 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1197 tcon->bad_network_name = true;
1203 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1205 struct smb2_tree_disconnect_req *req; /* response is trivial */
1207 struct TCP_Server_Info *server;
1208 struct cifs_ses *ses = tcon->ses;
1210 cifs_dbg(FYI, "Tree Disconnect\n");
1212 if (ses && (ses->server))
1213 server = ses->server;
1217 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1220 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1224 rc = SendReceiveNoRsp(xid, ses, (char *)req, 0);
1225 cifs_small_buf_release(req);
1227 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1233 static struct create_durable *
1234 create_durable_buf(void)
1236 struct create_durable *buf;
1238 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1242 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1243 (struct create_durable, Data));
1244 buf->ccontext.DataLength = cpu_to_le32(16);
1245 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1246 (struct create_durable, Name));
1247 buf->ccontext.NameLength = cpu_to_le16(4);
1248 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1256 static struct create_durable *
1257 create_reconnect_durable_buf(struct cifs_fid *fid)
1259 struct create_durable *buf;
1261 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1265 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1266 (struct create_durable, Data));
1267 buf->ccontext.DataLength = cpu_to_le32(16);
1268 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1269 (struct create_durable, Name));
1270 buf->ccontext.NameLength = cpu_to_le16(4);
1271 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1272 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1273 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1282 parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1283 unsigned int *epoch)
1286 struct create_context *cc;
1288 unsigned int remaining;
1291 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
1292 remaining = le32_to_cpu(rsp->CreateContextsLength);
1293 cc = (struct create_context *)data_offset;
1294 while (remaining >= sizeof(struct create_context)) {
1295 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1296 if (le16_to_cpu(cc->NameLength) == 4 &&
1297 strncmp(name, "RqLs", 4) == 0)
1298 return server->ops->parse_lease_buf(cc, epoch);
1300 next = le32_to_cpu(cc->Next);
1304 cc = (struct create_context *)((char *)cc + next);
1311 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1312 unsigned int *num_iovec, __u8 *oplock)
1314 struct smb2_create_req *req = iov[0].iov_base;
1315 unsigned int num = *num_iovec;
1317 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
1318 if (iov[num].iov_base == NULL)
1320 iov[num].iov_len = server->vals->create_lease_size;
1321 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1322 if (!req->CreateContextsOffset)
1323 req->CreateContextsOffset = cpu_to_le32(
1324 sizeof(struct smb2_create_req) - 4 +
1325 iov[num - 1].iov_len);
1326 le32_add_cpu(&req->CreateContextsLength,
1327 server->vals->create_lease_size);
1328 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
1329 *num_iovec = num + 1;
1333 static struct create_durable_v2 *
1334 create_durable_v2_buf(struct cifs_fid *pfid)
1336 struct create_durable_v2 *buf;
1338 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1342 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1343 (struct create_durable_v2, dcontext));
1344 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1345 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1346 (struct create_durable_v2, Name));
1347 buf->ccontext.NameLength = cpu_to_le16(4);
1349 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1350 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1351 generate_random_uuid(buf->dcontext.CreateGuid);
1352 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1354 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1362 static struct create_durable_handle_reconnect_v2 *
1363 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1365 struct create_durable_handle_reconnect_v2 *buf;
1367 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1372 buf->ccontext.DataOffset =
1373 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1375 buf->ccontext.DataLength =
1376 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1377 buf->ccontext.NameOffset =
1378 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1380 buf->ccontext.NameLength = cpu_to_le16(4);
1382 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1383 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1384 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1385 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1387 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1396 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1397 struct cifs_open_parms *oparms)
1399 struct smb2_create_req *req = iov[0].iov_base;
1400 unsigned int num = *num_iovec;
1402 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1403 if (iov[num].iov_base == NULL)
1405 iov[num].iov_len = sizeof(struct create_durable_v2);
1406 if (!req->CreateContextsOffset)
1407 req->CreateContextsOffset =
1408 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1410 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1411 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1412 *num_iovec = num + 1;
1417 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1418 struct cifs_open_parms *oparms)
1420 struct smb2_create_req *req = iov[0].iov_base;
1421 unsigned int num = *num_iovec;
1423 /* indicate that we don't need to relock the file */
1424 oparms->reconnect = false;
1426 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1427 if (iov[num].iov_base == NULL)
1429 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1430 if (!req->CreateContextsOffset)
1431 req->CreateContextsOffset =
1432 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1434 le32_add_cpu(&req->CreateContextsLength,
1435 sizeof(struct create_durable_handle_reconnect_v2));
1436 inc_rfc1001_len(&req->hdr,
1437 sizeof(struct create_durable_handle_reconnect_v2));
1438 *num_iovec = num + 1;
1443 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1444 struct cifs_open_parms *oparms, bool use_persistent)
1446 struct smb2_create_req *req = iov[0].iov_base;
1447 unsigned int num = *num_iovec;
1449 if (use_persistent) {
1450 if (oparms->reconnect)
1451 return add_durable_reconnect_v2_context(iov, num_iovec,
1454 return add_durable_v2_context(iov, num_iovec, oparms);
1457 if (oparms->reconnect) {
1458 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1459 /* indicate that we don't need to relock the file */
1460 oparms->reconnect = false;
1462 iov[num].iov_base = create_durable_buf();
1463 if (iov[num].iov_base == NULL)
1465 iov[num].iov_len = sizeof(struct create_durable);
1466 if (!req->CreateContextsOffset)
1467 req->CreateContextsOffset =
1468 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1470 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1471 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1472 *num_iovec = num + 1;
1477 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
1478 __u8 *oplock, struct smb2_file_all_info *buf,
1479 struct smb2_err_rsp **err_buf)
1481 struct smb2_create_req *req;
1482 struct smb2_create_rsp *rsp;
1483 struct TCP_Server_Info *server;
1484 struct cifs_tcon *tcon = oparms->tcon;
1485 struct cifs_ses *ses = tcon->ses;
1487 struct kvec rsp_iov;
1490 __le16 *copy_path = NULL;
1493 unsigned int n_iov = 2;
1494 __u32 file_attributes = 0;
1495 char *dhc_buf = NULL, *lc_buf = NULL;
1497 cifs_dbg(FYI, "create/open\n");
1499 if (ses && (ses->server))
1500 server = ses->server;
1504 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1508 if (oparms->create_options & CREATE_OPTION_READONLY)
1509 file_attributes |= ATTR_READONLY;
1510 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1511 file_attributes |= ATTR_SYSTEM;
1513 req->ImpersonationLevel = IL_IMPERSONATION;
1514 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
1515 /* File attributes ignored on open (used in create though) */
1516 req->FileAttributes = cpu_to_le32(file_attributes);
1517 req->ShareAccess = FILE_SHARE_ALL_LE;
1518 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1519 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
1520 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1521 /* do not count rfc1001 len field */
1522 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1524 iov[0].iov_base = (char *)req;
1525 /* 4 for rfc1002 length field */
1526 iov[0].iov_len = get_rfc1002_length(req) + 4;
1528 /* MUST set path len (NameLength) to 0 opening root of share */
1529 req->NameLength = cpu_to_le16(uni_path_len - 2);
1530 /* -1 since last byte is buf[0] which is sent below (path) */
1532 if (uni_path_len % 8 != 0) {
1533 copy_size = uni_path_len / 8 * 8;
1534 if (copy_size < uni_path_len)
1537 copy_path = kzalloc(copy_size, GFP_KERNEL);
1540 memcpy((char *)copy_path, (const char *)path,
1542 uni_path_len = copy_size;
1546 iov[1].iov_len = uni_path_len;
1547 iov[1].iov_base = path;
1548 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1549 inc_rfc1001_len(req, uni_path_len - 1);
1551 if (!server->oplocks)
1552 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1554 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1555 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1556 req->RequestedOplockLevel = *oplock;
1558 rc = add_lease_context(server, iov, &n_iov, oplock);
1560 cifs_small_buf_release(req);
1564 lc_buf = iov[n_iov-1].iov_base;
1567 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1568 /* need to set Next field of lease context if we request it */
1569 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
1570 struct create_context *ccontext =
1571 (struct create_context *)iov[n_iov-1].iov_base;
1573 cpu_to_le32(server->vals->create_lease_size);
1576 rc = add_durable_context(iov, &n_iov, oparms,
1577 tcon->use_persistent);
1579 cifs_small_buf_release(req);
1584 dhc_buf = iov[n_iov-1].iov_base;
1587 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, 0, &rsp_iov);
1588 cifs_small_buf_release(req);
1589 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
1592 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
1594 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1599 oparms->fid->persistent_fid = rsp->PersistentFileId;
1600 oparms->fid->volatile_fid = rsp->VolatileFileId;
1603 memcpy(buf, &rsp->CreationTime, 32);
1604 buf->AllocationSize = rsp->AllocationSize;
1605 buf->EndOfFile = rsp->EndofFile;
1606 buf->Attributes = rsp->FileAttributes;
1607 buf->NumberOfLinks = cpu_to_le32(1);
1608 buf->DeletePending = 0;
1611 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
1612 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
1614 *oplock = rsp->OplockLevel;
1619 free_rsp_buf(resp_buftype, rsp);
1624 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1627 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1628 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1629 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1631 struct smb2_ioctl_req *req;
1632 struct smb2_ioctl_rsp *rsp;
1633 struct smb2_sync_hdr *shdr;
1634 struct TCP_Server_Info *server;
1635 struct cifs_ses *ses;
1637 struct kvec rsp_iov;
1642 cifs_dbg(FYI, "SMB2 IOCTL\n");
1644 if (out_data != NULL)
1647 /* zero out returned data len, in case of error */
1656 if (ses && (ses->server))
1657 server = ses->server;
1661 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1665 req->CtlCode = cpu_to_le32(opcode);
1666 req->PersistentFileId = persistent_fid;
1667 req->VolatileFileId = volatile_fid;
1670 req->InputCount = cpu_to_le32(indatalen);
1671 /* do not set InputOffset if no input data */
1673 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1674 iov[1].iov_base = in_data;
1675 iov[1].iov_len = indatalen;
1680 req->OutputOffset = 0;
1681 req->OutputCount = 0; /* MBZ */
1684 * Could increase MaxOutputResponse, but that would require more
1685 * than one credit. Windows typically sets this smaller, but for some
1686 * ioctls it may be useful to allow server to send more. No point
1687 * limiting what the server can send as long as fits in one credit
1689 req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1692 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1696 iov[0].iov_base = (char *)req;
1699 * If no input data, the size of ioctl struct in
1700 * protocol spec still includes a 1 byte data buffer,
1701 * but if input data passed to ioctl, we do not
1702 * want to double count this, so we do not send
1703 * the dummy one byte of data in iovec[0] if sending
1704 * input data (in iovec[1]). We also must add 4 bytes
1705 * in first iovec to allow for rfc1002 length field.
1709 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1710 inc_rfc1001_len(req, indatalen - 1);
1712 iov[0].iov_len = get_rfc1002_length(req) + 4;
1715 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, 0, &rsp_iov);
1716 cifs_small_buf_release(req);
1717 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
1719 if ((rc != 0) && (rc != -EINVAL)) {
1720 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1722 } else if (rc == -EINVAL) {
1723 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1724 (opcode != FSCTL_SRV_COPYCHUNK)) {
1725 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1730 /* check if caller wants to look at return data or just return rc */
1731 if ((plen == NULL) || (out_data == NULL))
1734 *plen = le32_to_cpu(rsp->OutputCount);
1736 /* We check for obvious errors in the output buffer length and offset */
1738 goto ioctl_exit; /* server returned no data */
1739 else if (*plen > 0xFF00) {
1740 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1746 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1747 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1748 le32_to_cpu(rsp->OutputOffset));
1754 *out_data = kmalloc(*plen, GFP_KERNEL);
1755 if (*out_data == NULL) {
1760 shdr = get_sync_hdr(rsp);
1761 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
1763 free_rsp_buf(resp_buftype, rsp);
1768 * Individual callers to ioctl worker function follow
1772 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1773 u64 persistent_fid, u64 volatile_fid)
1776 struct compress_ioctl fsctl_input;
1777 char *ret_data = NULL;
1779 fsctl_input.CompressionState =
1780 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
1782 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1783 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1784 (char *)&fsctl_input /* data input */,
1785 2 /* in data len */, &ret_data /* out data */, NULL);
1787 cifs_dbg(FYI, "set compression rc %d\n", rc);
1793 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1794 u64 persistent_fid, u64 volatile_fid)
1796 struct smb2_close_req *req;
1797 struct smb2_close_rsp *rsp;
1798 struct TCP_Server_Info *server;
1799 struct cifs_ses *ses = tcon->ses;
1801 struct kvec rsp_iov;
1805 cifs_dbg(FYI, "Close\n");
1807 if (ses && (ses->server))
1808 server = ses->server;
1812 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1816 req->PersistentFileId = persistent_fid;
1817 req->VolatileFileId = volatile_fid;
1819 iov[0].iov_base = (char *)req;
1820 /* 4 for rfc1002 length field */
1821 iov[0].iov_len = get_rfc1002_length(req) + 4;
1823 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0, &rsp_iov);
1824 cifs_small_buf_release(req);
1825 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
1828 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
1832 /* BB FIXME - decode close response, update inode for caching */
1835 free_rsp_buf(resp_buftype, rsp);
1840 validate_buf(unsigned int offset, unsigned int buffer_length,
1841 struct smb2_hdr *hdr, unsigned int min_buf_size)
1844 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1845 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1846 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1847 char *end_of_buf = begin_of_buf + buffer_length;
1850 if (buffer_length < min_buf_size) {
1851 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1852 buffer_length, min_buf_size);
1856 /* check if beyond RFC1001 maximum length */
1857 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
1858 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1859 buffer_length, smb_len);
1863 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
1864 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
1872 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1873 * Caller must free buffer.
1876 validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1877 struct smb2_hdr *hdr, unsigned int minbufsize,
1881 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1887 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1891 memcpy(data, begin_of_buf, buffer_length);
1897 query_info(const unsigned int xid, struct cifs_tcon *tcon,
1898 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1899 size_t output_len, size_t min_len, void *data)
1901 struct smb2_query_info_req *req;
1902 struct smb2_query_info_rsp *rsp = NULL;
1904 struct kvec rsp_iov;
1907 struct TCP_Server_Info *server;
1908 struct cifs_ses *ses = tcon->ses;
1910 cifs_dbg(FYI, "Query Info\n");
1912 if (ses && (ses->server))
1913 server = ses->server;
1917 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1921 req->InfoType = SMB2_O_INFO_FILE;
1922 req->FileInfoClass = info_class;
1923 req->PersistentFileId = persistent_fid;
1924 req->VolatileFileId = volatile_fid;
1925 /* 4 for rfc1002 length field and 1 for Buffer */
1926 req->InputBufferOffset =
1927 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
1928 req->OutputBufferLength = cpu_to_le32(output_len);
1930 iov[0].iov_base = (char *)req;
1931 /* 4 for rfc1002 length field */
1932 iov[0].iov_len = get_rfc1002_length(req) + 4;
1934 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0, &rsp_iov);
1935 cifs_small_buf_release(req);
1936 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1939 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1943 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1944 le32_to_cpu(rsp->OutputBufferLength),
1945 &rsp->hdr, min_len, data);
1948 free_rsp_buf(resp_buftype, rsp);
1953 SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1954 u64 persistent_fid, u64 volatile_fid,
1955 struct smb2_file_all_info *data)
1957 return query_info(xid, tcon, persistent_fid, volatile_fid,
1958 FILE_ALL_INFORMATION,
1959 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
1960 sizeof(struct smb2_file_all_info), data);
1964 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1965 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1967 return query_info(xid, tcon, persistent_fid, volatile_fid,
1968 FILE_INTERNAL_INFORMATION,
1969 sizeof(struct smb2_file_internal_info),
1970 sizeof(struct smb2_file_internal_info), uniqueid);
1974 * This is a no-op for now. We're not really interested in the reply, but
1975 * rather in the fact that the server sent one and that server->lstrp
1978 * FIXME: maybe we should consider checking that the reply matches request?
1981 smb2_echo_callback(struct mid_q_entry *mid)
1983 struct TCP_Server_Info *server = mid->callback_data;
1984 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
1985 unsigned int credits_received = 1;
1987 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1988 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
1990 mutex_lock(&server->srv_mutex);
1991 DeleteMidQEntry(mid);
1992 mutex_unlock(&server->srv_mutex);
1993 add_credits(server, credits_received, CIFS_ECHO_OP);
1996 void smb2_reconnect_server(struct work_struct *work)
1998 struct TCP_Server_Info *server = container_of(work,
1999 struct TCP_Server_Info, reconnect.work);
2000 struct cifs_ses *ses;
2001 struct cifs_tcon *tcon, *tcon2;
2002 struct list_head tmp_list;
2003 int tcon_exist = false;
2005 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2006 mutex_lock(&server->reconnect_mutex);
2008 INIT_LIST_HEAD(&tmp_list);
2009 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2011 spin_lock(&cifs_tcp_ses_lock);
2012 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2013 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2014 if (tcon->need_reconnect || tcon->need_reopen_files) {
2016 list_add_tail(&tcon->rlist, &tmp_list);
2022 * Get the reference to server struct to be sure that the last call of
2023 * cifs_put_tcon() in the loop below won't release the server pointer.
2026 server->srv_count++;
2028 spin_unlock(&cifs_tcp_ses_lock);
2030 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
2031 if (!smb2_reconnect(SMB2_INTERNAL_CMD, tcon))
2032 cifs_reopen_persistent_handles(tcon);
2033 list_del_init(&tcon->rlist);
2034 cifs_put_tcon(tcon);
2037 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2038 mutex_unlock(&server->reconnect_mutex);
2040 /* now we can safely release srv struct */
2042 cifs_put_tcp_session(server, 1);
2046 SMB2_echo(struct TCP_Server_Info *server)
2048 struct smb2_echo_req *req;
2051 struct smb_rqst rqst = { .rq_iov = &iov,
2054 cifs_dbg(FYI, "In echo request\n");
2056 if (server->tcpStatus == CifsNeedNegotiate) {
2057 /* No need to send echo on newly established connections */
2058 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2062 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2066 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
2068 iov.iov_base = (char *)req;
2069 /* 4 for rfc1002 length field */
2070 iov.iov_len = get_rfc1002_length(req) + 4;
2072 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
2075 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
2077 cifs_small_buf_release(req);
2082 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2085 struct smb2_flush_req *req;
2086 struct TCP_Server_Info *server;
2087 struct cifs_ses *ses = tcon->ses;
2089 struct kvec rsp_iov;
2093 cifs_dbg(FYI, "Flush\n");
2095 if (ses && (ses->server))
2096 server = ses->server;
2100 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2104 req->PersistentFileId = persistent_fid;
2105 req->VolatileFileId = volatile_fid;
2107 iov[0].iov_base = (char *)req;
2108 /* 4 for rfc1002 length field */
2109 iov[0].iov_len = get_rfc1002_length(req) + 4;
2111 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0, &rsp_iov);
2112 cifs_small_buf_release(req);
2115 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2117 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2122 * To form a chain of read requests, any read requests after the first should
2123 * have the end_of_chain boolean set to true.
2126 smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
2127 unsigned int remaining_bytes, int request_type)
2130 struct smb2_read_req *req = NULL;
2131 struct smb2_sync_hdr *shdr;
2133 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
2136 if (io_parms->tcon->ses->server == NULL)
2137 return -ECONNABORTED;
2139 shdr = get_sync_hdr(req);
2140 shdr->ProcessId = cpu_to_le32(io_parms->pid);
2142 req->PersistentFileId = io_parms->persistent_fid;
2143 req->VolatileFileId = io_parms->volatile_fid;
2144 req->ReadChannelInfoOffset = 0; /* reserved */
2145 req->ReadChannelInfoLength = 0; /* reserved */
2146 req->Channel = 0; /* reserved */
2147 req->MinimumCount = 0;
2148 req->Length = cpu_to_le32(io_parms->length);
2149 req->Offset = cpu_to_le64(io_parms->offset);
2151 if (request_type & CHAINED_REQUEST) {
2152 if (!(request_type & END_OF_CHAIN)) {
2153 /* 4 for rfc1002 length field */
2155 cpu_to_le32(get_rfc1002_length(req) + 4);
2156 } else /* END_OF_CHAIN */
2157 shdr->NextCommand = 0;
2158 if (request_type & RELATED_REQUEST) {
2159 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2161 * Related requests use info from previous read request
2164 shdr->SessionId = 0xFFFFFFFF;
2165 shdr->TreeId = 0xFFFFFFFF;
2166 req->PersistentFileId = 0xFFFFFFFF;
2167 req->VolatileFileId = 0xFFFFFFFF;
2170 if (remaining_bytes > io_parms->length)
2171 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2173 req->RemainingBytes = 0;
2175 iov[0].iov_base = (char *)req;
2176 /* 4 for rfc1002 length field */
2177 iov[0].iov_len = get_rfc1002_length(req) + 4;
2182 smb2_readv_callback(struct mid_q_entry *mid)
2184 struct cifs_readdata *rdata = mid->callback_data;
2185 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2186 struct TCP_Server_Info *server = tcon->ses->server;
2187 struct smb2_sync_hdr *shdr = get_sync_hdr(rdata->iov.iov_base);
2188 unsigned int credits_received = 1;
2189 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
2191 .rq_pages = rdata->pages,
2192 .rq_npages = rdata->nr_pages,
2193 .rq_pagesz = rdata->pagesz,
2194 .rq_tailsz = rdata->tailsz };
2196 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2197 __func__, mid->mid, mid->mid_state, rdata->result,
2200 switch (mid->mid_state) {
2201 case MID_RESPONSE_RECEIVED:
2202 credits_received = le16_to_cpu(shdr->CreditRequest);
2203 /* result already set, check signature */
2207 rc = smb2_verify_signature(&rqst, server);
2209 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2212 /* FIXME: should this be counted toward the initiating task? */
2213 task_io_account_read(rdata->got_bytes);
2214 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2216 case MID_REQUEST_SUBMITTED:
2217 case MID_RETRY_NEEDED:
2218 rdata->result = -EAGAIN;
2219 if (server->sign && rdata->got_bytes)
2220 /* reset bytes number since we can not check a sign */
2221 rdata->got_bytes = 0;
2222 /* FIXME: should this be counted toward the initiating task? */
2223 task_io_account_read(rdata->got_bytes);
2224 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2227 if (rdata->result != -ENODATA)
2228 rdata->result = -EIO;
2232 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2234 queue_work(cifsiod_wq, &rdata->work);
2235 mutex_lock(&server->srv_mutex);
2236 DeleteMidQEntry(mid);
2237 mutex_unlock(&server->srv_mutex);
2238 add_credits(server, credits_received, 0);
2241 /* smb2_async_readv - send an async write, and set up mid to handle result */
2243 smb2_async_readv(struct cifs_readdata *rdata)
2247 struct smb2_sync_hdr *shdr;
2248 struct cifs_io_parms io_parms;
2249 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
2251 struct TCP_Server_Info *server;
2253 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2254 __func__, rdata->offset, rdata->bytes);
2256 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2257 io_parms.offset = rdata->offset;
2258 io_parms.length = rdata->bytes;
2259 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2260 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2261 io_parms.pid = rdata->pid;
2263 server = io_parms.tcon->ses->server;
2265 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
2267 if (rc == -EAGAIN && rdata->credits) {
2268 /* credits was reset by reconnect */
2270 /* reduce in_flight value since we won't send the req */
2271 spin_lock(&server->req_lock);
2272 server->in_flight--;
2273 spin_unlock(&server->req_lock);
2278 buf = rdata->iov.iov_base;
2279 shdr = get_sync_hdr(buf);
2280 /* 4 for rfc1002 length field */
2281 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
2283 if (rdata->credits) {
2284 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2285 SMB2_MAX_BUFFER_SIZE));
2286 shdr->CreditRequest = shdr->CreditCharge;
2287 spin_lock(&server->req_lock);
2288 server->credits += rdata->credits -
2289 le16_to_cpu(shdr->CreditCharge);
2290 spin_unlock(&server->req_lock);
2291 wake_up(&server->request_q);
2292 flags = CIFS_HAS_CREDITS;
2295 kref_get(&rdata->refcount);
2296 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
2297 cifs_readv_receive, smb2_readv_callback,
2300 kref_put(&rdata->refcount, cifs_readdata_release);
2301 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2304 cifs_small_buf_release(buf);
2309 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2310 unsigned int *nbytes, char **buf, int *buf_type)
2312 int resp_buftype, rc = -EACCES;
2313 struct smb2_read_rsp *rsp = NULL;
2314 struct smb2_sync_hdr *shdr;
2316 struct kvec rsp_iov;
2319 rc = smb2_new_read_req(iov, io_parms, 0, 0);
2323 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2324 &resp_buftype, CIFS_LOG_ERROR, &rsp_iov);
2325 cifs_small_buf_release(iov[0].iov_base);
2327 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
2328 shdr = get_sync_hdr(rsp);
2330 if (shdr->Status == STATUS_END_OF_FILE) {
2331 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2336 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2337 cifs_dbg(VFS, "Send error in read = %d\n", rc);
2339 *nbytes = le32_to_cpu(rsp->DataLength);
2340 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2341 (*nbytes > io_parms->length)) {
2342 cifs_dbg(FYI, "bad length %d for count %d\n",
2343 *nbytes, io_parms->length);
2350 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
2351 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2352 } else if (resp_buftype != CIFS_NO_BUFFER) {
2353 *buf = rsp_iov.iov_base;
2354 if (resp_buftype == CIFS_SMALL_BUFFER)
2355 *buf_type = CIFS_SMALL_BUFFER;
2356 else if (resp_buftype == CIFS_LARGE_BUFFER)
2357 *buf_type = CIFS_LARGE_BUFFER;
2363 * Check the mid_state and signature on received buffer (if any), and queue the
2364 * workqueue completion task.
2367 smb2_writev_callback(struct mid_q_entry *mid)
2369 struct cifs_writedata *wdata = mid->callback_data;
2370 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2371 struct TCP_Server_Info *server = tcon->ses->server;
2372 unsigned int written;
2373 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2374 unsigned int credits_received = 1;
2376 switch (mid->mid_state) {
2377 case MID_RESPONSE_RECEIVED:
2378 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
2379 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2380 if (wdata->result != 0)
2383 written = le32_to_cpu(rsp->DataLength);
2385 * Mask off high 16 bits when bytes written as returned
2386 * by the server is greater than bytes requested by the
2387 * client. OS/2 servers are known to set incorrect
2390 if (written > wdata->bytes)
2393 if (written < wdata->bytes)
2394 wdata->result = -ENOSPC;
2396 wdata->bytes = written;
2398 case MID_REQUEST_SUBMITTED:
2399 case MID_RETRY_NEEDED:
2400 wdata->result = -EAGAIN;
2403 wdata->result = -EIO;
2408 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2410 queue_work(cifsiod_wq, &wdata->work);
2411 mutex_lock(&server->srv_mutex);
2412 DeleteMidQEntry(mid);
2413 mutex_unlock(&server->srv_mutex);
2414 add_credits(tcon->ses->server, credits_received, 0);
2417 /* smb2_async_writev - send an async write, and set up mid to handle result */
2419 smb2_async_writev(struct cifs_writedata *wdata,
2420 void (*release)(struct kref *kref))
2422 int rc = -EACCES, flags = 0;
2423 struct smb2_write_req *req = NULL;
2424 struct smb2_sync_hdr *shdr;
2425 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2426 struct TCP_Server_Info *server = tcon->ses->server;
2428 struct smb_rqst rqst;
2430 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
2432 if (rc == -EAGAIN && wdata->credits) {
2433 /* credits was reset by reconnect */
2435 /* reduce in_flight value since we won't send the req */
2436 spin_lock(&server->req_lock);
2437 server->in_flight--;
2438 spin_unlock(&server->req_lock);
2440 goto async_writev_out;
2443 shdr = get_sync_hdr(req);
2444 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
2446 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2447 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2448 req->WriteChannelInfoOffset = 0;
2449 req->WriteChannelInfoLength = 0;
2451 req->Offset = cpu_to_le64(wdata->offset);
2452 /* 4 for rfc1002 length field */
2453 req->DataOffset = cpu_to_le16(
2454 offsetof(struct smb2_write_req, Buffer) - 4);
2455 req->RemainingBytes = 0;
2457 /* 4 for rfc1002 length field and 1 for Buffer */
2458 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2463 rqst.rq_pages = wdata->pages;
2464 rqst.rq_npages = wdata->nr_pages;
2465 rqst.rq_pagesz = wdata->pagesz;
2466 rqst.rq_tailsz = wdata->tailsz;
2468 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2469 wdata->offset, wdata->bytes);
2471 req->Length = cpu_to_le32(wdata->bytes);
2473 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2475 if (wdata->credits) {
2476 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2477 SMB2_MAX_BUFFER_SIZE));
2478 shdr->CreditRequest = shdr->CreditCharge;
2479 spin_lock(&server->req_lock);
2480 server->credits += wdata->credits -
2481 le16_to_cpu(shdr->CreditCharge);
2482 spin_unlock(&server->req_lock);
2483 wake_up(&server->request_q);
2484 flags = CIFS_HAS_CREDITS;
2487 kref_get(&wdata->refcount);
2488 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2492 kref_put(&wdata->refcount, release);
2493 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2497 cifs_small_buf_release(req);
2502 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2503 * The length field from io_parms must be at least 1 and indicates a number of
2504 * elements with data to write that begins with position 1 in iov array. All
2505 * data length is specified by count.
2508 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2509 unsigned int *nbytes, struct kvec *iov, int n_vec)
2512 struct smb2_write_req *req = NULL;
2513 struct smb2_write_rsp *rsp = NULL;
2515 struct kvec rsp_iov;
2522 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2526 if (io_parms->tcon->ses->server == NULL)
2527 return -ECONNABORTED;
2529 req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
2531 req->PersistentFileId = io_parms->persistent_fid;
2532 req->VolatileFileId = io_parms->volatile_fid;
2533 req->WriteChannelInfoOffset = 0;
2534 req->WriteChannelInfoLength = 0;
2536 req->Length = cpu_to_le32(io_parms->length);
2537 req->Offset = cpu_to_le64(io_parms->offset);
2538 /* 4 for rfc1002 length field */
2539 req->DataOffset = cpu_to_le16(
2540 offsetof(struct smb2_write_req, Buffer) - 4);
2541 req->RemainingBytes = 0;
2543 iov[0].iov_base = (char *)req;
2544 /* 4 for rfc1002 length field and 1 for Buffer */
2545 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2547 /* length of entire message including data to be written */
2548 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2550 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2551 &resp_buftype, 0, &rsp_iov);
2552 cifs_small_buf_release(req);
2553 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
2556 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
2557 cifs_dbg(VFS, "Send error in write = %d\n", rc);
2559 *nbytes = le32_to_cpu(rsp->DataLength);
2561 free_rsp_buf(resp_buftype, rsp);
2566 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2569 unsigned int entrycount = 0;
2570 unsigned int next_offset = 0;
2571 FILE_DIRECTORY_INFO *entryptr;
2573 if (bufstart == NULL)
2576 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2579 entryptr = (FILE_DIRECTORY_INFO *)
2580 ((char *)entryptr + next_offset);
2582 if ((char *)entryptr + size > end_of_buf) {
2583 cifs_dbg(VFS, "malformed search entry would overflow\n");
2587 len = le32_to_cpu(entryptr->FileNameLength);
2588 if ((char *)entryptr + len + size > end_of_buf) {
2589 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2594 *lastentry = (char *)entryptr;
2597 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2609 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2610 u64 persistent_fid, u64 volatile_fid, int index,
2611 struct cifs_search_info *srch_inf)
2613 struct smb2_query_directory_req *req;
2614 struct smb2_query_directory_rsp *rsp = NULL;
2616 struct kvec rsp_iov;
2619 int resp_buftype = CIFS_NO_BUFFER;
2620 unsigned char *bufptr;
2621 struct TCP_Server_Info *server;
2622 struct cifs_ses *ses = tcon->ses;
2623 __le16 asteriks = cpu_to_le16('*');
2625 unsigned int output_size = CIFSMaxBufSize;
2626 size_t info_buf_size;
2628 if (ses && (ses->server))
2629 server = ses->server;
2633 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2637 switch (srch_inf->info_level) {
2638 case SMB_FIND_FILE_DIRECTORY_INFO:
2639 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2640 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2642 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2643 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2644 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2647 cifs_dbg(VFS, "info level %u isn't supported\n",
2648 srch_inf->info_level);
2653 req->FileIndex = cpu_to_le32(index);
2654 req->PersistentFileId = persistent_fid;
2655 req->VolatileFileId = volatile_fid;
2658 bufptr = req->Buffer;
2659 memcpy(bufptr, &asteriks, len);
2661 req->FileNameOffset =
2662 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2663 req->FileNameLength = cpu_to_le16(len);
2665 * BB could be 30 bytes or so longer if we used SMB2 specific
2666 * buffer lengths, but this is safe and close enough.
2668 output_size = min_t(unsigned int, output_size, server->maxBuf);
2669 output_size = min_t(unsigned int, output_size, 2 << 15);
2670 req->OutputBufferLength = cpu_to_le32(output_size);
2672 iov[0].iov_base = (char *)req;
2673 /* 4 for RFC1001 length and 1 for Buffer */
2674 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2676 iov[1].iov_base = (char *)(req->Buffer);
2677 iov[1].iov_len = len;
2679 inc_rfc1001_len(req, len - 1 /* Buffer */);
2681 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0, &rsp_iov);
2682 cifs_small_buf_release(req);
2683 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
2686 if (rc == -ENODATA &&
2687 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
2688 srch_inf->endOfSearch = true;
2691 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2695 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2696 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2701 srch_inf->unicode = true;
2703 if (srch_inf->ntwrk_buf_start) {
2704 if (srch_inf->smallBuf)
2705 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2707 cifs_buf_release(srch_inf->ntwrk_buf_start);
2709 srch_inf->ntwrk_buf_start = (char *)rsp;
2710 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2711 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2712 /* 4 for rfc1002 length field */
2713 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2714 srch_inf->entries_in_buffer =
2715 num_entries(srch_inf->srch_entries_start, end_of_smb,
2716 &srch_inf->last_entry, info_buf_size);
2717 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
2718 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2719 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2720 srch_inf->srch_entries_start, srch_inf->last_entry);
2721 if (resp_buftype == CIFS_LARGE_BUFFER)
2722 srch_inf->smallBuf = false;
2723 else if (resp_buftype == CIFS_SMALL_BUFFER)
2724 srch_inf->smallBuf = true;
2726 cifs_dbg(VFS, "illegal search buffer type\n");
2731 free_rsp_buf(resp_buftype, rsp);
2736 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2737 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
2738 unsigned int num, void **data, unsigned int *size)
2740 struct smb2_set_info_req *req;
2741 struct smb2_set_info_rsp *rsp = NULL;
2743 struct kvec rsp_iov;
2747 struct TCP_Server_Info *server;
2748 struct cifs_ses *ses = tcon->ses;
2750 if (ses && (ses->server))
2751 server = ses->server;
2758 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2762 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2768 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
2770 req->InfoType = SMB2_O_INFO_FILE;
2771 req->FileInfoClass = info_class;
2772 req->PersistentFileId = persistent_fid;
2773 req->VolatileFileId = volatile_fid;
2775 /* 4 for RFC1001 length and 1 for Buffer */
2777 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2778 req->BufferLength = cpu_to_le32(*size);
2780 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2782 memcpy(req->Buffer, *data, *size);
2784 iov[0].iov_base = (char *)req;
2785 /* 4 for RFC1001 length */
2786 iov[0].iov_len = get_rfc1002_length(req) + 4;
2788 for (i = 1; i < num; i++) {
2789 inc_rfc1001_len(req, size[i]);
2790 le32_add_cpu(&req->BufferLength, size[i]);
2791 iov[i].iov_base = (char *)data[i];
2792 iov[i].iov_len = size[i];
2795 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0, &rsp_iov);
2796 cifs_small_buf_release(req);
2797 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
2800 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
2802 free_rsp_buf(resp_buftype, rsp);
2808 SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2809 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2811 struct smb2_file_rename_info info;
2813 unsigned int size[2];
2815 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2817 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2821 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2822 /* 0 = fail if target already exists */
2823 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2824 info.FileNameLength = cpu_to_le32(len);
2827 size[0] = sizeof(struct smb2_file_rename_info);
2829 data[1] = target_file;
2830 size[1] = len + 2 /* null */;
2832 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2833 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2840 SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2841 u64 persistent_fid, u64 volatile_fid)
2843 __u8 delete_pending = 1;
2847 data = &delete_pending;
2848 size = 1; /* sizeof __u8 */
2850 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2851 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2856 SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2857 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2859 struct smb2_file_link_info info;
2861 unsigned int size[2];
2863 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2865 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2869 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2870 /* 0 = fail if link already exists */
2871 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2872 info.FileNameLength = cpu_to_le32(len);
2875 size[0] = sizeof(struct smb2_file_link_info);
2877 data[1] = target_file;
2878 size[1] = len + 2 /* null */;
2880 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2881 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
2887 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2888 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
2890 struct smb2_file_eof_info info;
2894 info.EndOfFile = *eof;
2897 size = sizeof(struct smb2_file_eof_info);
2900 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2901 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2903 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2904 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
2908 SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2909 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2912 size = sizeof(FILE_BASIC_INFO);
2913 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2914 current->tgid, FILE_BASIC_INFORMATION, 1,
2915 (void **)&buf, &size);
2919 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2920 const u64 persistent_fid, const u64 volatile_fid,
2924 struct smb2_oplock_break *req = NULL;
2926 cifs_dbg(FYI, "SMB2_oplock_break\n");
2927 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2932 req->VolatileFid = volatile_fid;
2933 req->PersistentFid = persistent_fid;
2934 req->OplockLevel = oplock_level;
2935 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
2937 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2938 cifs_small_buf_release(req);
2941 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
2942 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
2949 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2950 struct kstatfs *kst)
2952 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2953 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2954 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2955 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
2956 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2961 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2962 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2965 struct smb2_query_info_req *req;
2967 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
2969 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2972 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2976 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2977 req->FileInfoClass = level;
2978 req->PersistentFileId = persistent_fid;
2979 req->VolatileFileId = volatile_fid;
2980 /* 4 for rfc1002 length field and 1 for pad */
2981 req->InputBufferOffset =
2982 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2983 req->OutputBufferLength = cpu_to_le32(
2984 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2986 iov->iov_base = (char *)req;
2987 /* 4 for rfc1002 length field */
2988 iov->iov_len = get_rfc1002_length(req) + 4;
2993 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2994 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2996 struct smb2_query_info_rsp *rsp = NULL;
2998 struct kvec rsp_iov;
3001 struct cifs_ses *ses = tcon->ses;
3002 struct smb2_fs_full_size_info *info = NULL;
3004 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3005 sizeof(struct smb2_fs_full_size_info),
3006 persistent_fid, volatile_fid);
3010 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0, &rsp_iov);
3011 cifs_small_buf_release(iov.iov_base);
3013 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3016 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3018 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3019 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3020 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3021 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3022 sizeof(struct smb2_fs_full_size_info));
3024 copy_fs_info_to_kstatfs(info, fsdata);
3027 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3032 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
3033 u64 persistent_fid, u64 volatile_fid, int level)
3035 struct smb2_query_info_rsp *rsp = NULL;
3037 struct kvec rsp_iov;
3039 int resp_buftype, max_len, min_len;
3040 struct cifs_ses *ses = tcon->ses;
3041 unsigned int rsp_len, offset;
3043 if (level == FS_DEVICE_INFORMATION) {
3044 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3045 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3046 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3047 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3048 min_len = MIN_FS_ATTR_INFO_SIZE;
3049 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3050 max_len = sizeof(struct smb3_fs_ss_info);
3051 min_len = sizeof(struct smb3_fs_ss_info);
3053 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
3057 rc = build_qfs_info_req(&iov, tcon, level, max_len,
3058 persistent_fid, volatile_fid);
3062 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0, &rsp_iov);
3063 cifs_small_buf_release(iov.iov_base);
3065 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3068 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3070 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3071 offset = le16_to_cpu(rsp->OutputBufferOffset);
3072 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3076 if (level == FS_ATTRIBUTE_INFORMATION)
3077 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3078 + (char *)&rsp->hdr, min_t(unsigned int,
3080 else if (level == FS_DEVICE_INFORMATION)
3081 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3082 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
3083 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3084 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3085 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3086 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3087 tcon->perf_sector_size =
3088 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3092 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3097 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3098 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3099 const __u32 num_lock, struct smb2_lock_element *buf)
3102 struct smb2_lock_req *req = NULL;
3104 struct kvec rsp_iov;
3108 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
3110 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3114 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
3115 req->LockCount = cpu_to_le16(num_lock);
3117 req->PersistentFileId = persist_fid;
3118 req->VolatileFileId = volatile_fid;
3120 count = num_lock * sizeof(struct smb2_lock_element);
3121 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3123 iov[0].iov_base = (char *)req;
3124 /* 4 for rfc1002 length field and count for all locks */
3125 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3126 iov[1].iov_base = (char *)buf;
3127 iov[1].iov_len = count;
3129 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3130 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP,
3132 cifs_small_buf_release(req);
3134 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
3135 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3142 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3143 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3144 const __u64 length, const __u64 offset, const __u32 lock_flags,
3147 struct smb2_lock_element lock;
3149 lock.Offset = cpu_to_le64(offset);
3150 lock.Length = cpu_to_le64(length);
3151 lock.Flags = cpu_to_le32(lock_flags);
3152 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3153 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3155 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3159 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3160 __u8 *lease_key, const __le32 lease_state)
3163 struct smb2_lease_ack *req = NULL;
3165 cifs_dbg(FYI, "SMB2_lease_break\n");
3166 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3171 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
3172 req->StructureSize = cpu_to_le16(36);
3173 inc_rfc1001_len(req, 12);
3175 memcpy(req->LeaseKey, lease_key, 16);
3176 req->LeaseState = lease_state;
3178 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
3179 cifs_small_buf_release(req);
3182 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3183 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);