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 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)
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 = SMB2_PROTO_NUMBER;
101 hdr->StructureSize = cpu_to_le16(64);
102 hdr->Command = smb2_cmd;
103 if (tcon && tcon->ses && tcon->ses->server) {
104 struct TCP_Server_Info *server = tcon->ses->server;
106 spin_lock(&server->req_lock);
107 /* Request up to 2 credits but don't go over the limit. */
108 if (server->credits >= SMB2_MAX_CREDITS_AVAILABLE)
109 hdr->CreditRequest = cpu_to_le16(0);
111 hdr->CreditRequest = cpu_to_le16(
112 min_t(int, SMB2_MAX_CREDITS_AVAILABLE -
113 server->credits, 2));
114 spin_unlock(&server->req_lock);
116 hdr->CreditRequest = cpu_to_le16(2);
118 hdr->ProcessId = cpu_to_le32((__u16)current->tgid);
123 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
124 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
125 if ((tcon->ses) && (tcon->ses->server) &&
126 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
127 hdr->CreditCharge = cpu_to_le16(1);
128 /* else CreditCharge MBZ */
130 hdr->TreeId = tcon->tid;
131 /* Uid is not converted */
133 hdr->SessionId = tcon->ses->Suid;
136 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
137 * to pass the path on the Open SMB prefixed by \\server\share.
138 * Not sure when we would need to do the augmented path (if ever) and
139 * setting this flag breaks the SMB2 open operation since it is
140 * illegal to send an empty path name (without \\server\share prefix)
141 * when the DFS flag is set in the SMB open header. We could
142 * consider setting the flag on all operations other than open
143 * but it is safer to net set it for now.
145 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
146 hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
148 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign)
149 hdr->Flags |= SMB2_FLAGS_SIGNED;
151 pdu->StructureSize2 = cpu_to_le16(parmsize);
156 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
159 struct nls_table *nls_codepage;
160 struct cifs_ses *ses;
161 struct TCP_Server_Info *server;
164 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
165 * check for tcp and smb session status done differently
166 * for those three - in the calling routine.
171 if (smb2_command == SMB2_TREE_CONNECT)
174 if (tcon->tidStatus == CifsExiting) {
176 * only tree disconnect, open, and write,
177 * (and ulogoff which does not have tcon)
178 * are allowed as we start force umount.
180 if ((smb2_command != SMB2_WRITE) &&
181 (smb2_command != SMB2_CREATE) &&
182 (smb2_command != SMB2_TREE_DISCONNECT)) {
183 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
188 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
189 (!tcon->ses->server))
193 server = ses->server;
196 * Give demultiplex thread up to 10 seconds to reconnect, should be
197 * greater than cifs socket timeout which is 7 seconds
199 while (server->tcpStatus == CifsNeedReconnect) {
201 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
202 * here since they are implicitly done when session drops.
204 switch (smb2_command) {
206 * BB Should we keep oplock break and add flush to exceptions?
208 case SMB2_TREE_DISCONNECT:
211 case SMB2_OPLOCK_BREAK:
215 wait_event_interruptible_timeout(server->response_q,
216 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
218 /* are we still trying to reconnect? */
219 if (server->tcpStatus != CifsNeedReconnect)
223 * on "soft" mounts we wait once. Hard mounts keep
224 * retrying until process is killed or server comes
228 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
233 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
236 nls_codepage = load_nls_default();
239 * need to prevent multiple threads trying to simultaneously reconnect
240 * the same SMB session
242 mutex_lock(&tcon->ses->session_mutex);
243 rc = cifs_negotiate_protocol(0, tcon->ses);
244 if (!rc && tcon->ses->need_reconnect)
245 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
247 if (rc || !tcon->need_reconnect) {
248 mutex_unlock(&tcon->ses->session_mutex);
252 cifs_mark_open_files_invalid(tcon);
253 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
254 mutex_unlock(&tcon->ses->session_mutex);
255 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
258 atomic_inc(&tconInfoReconnectCount);
261 * Check if handle based operation so we know whether we can continue
262 * or not without returning to caller to reset file handle.
265 * BB Is flush done by server on drop of tcp session? Should we special
266 * case it and skip above?
268 switch (smb2_command) {
274 case SMB2_QUERY_DIRECTORY:
275 case SMB2_CHANGE_NOTIFY:
276 case SMB2_QUERY_INFO:
280 unload_nls(nls_codepage);
285 * Allocate and return pointer to an SMB request hdr, and set basic
286 * SMB information in the SMB header. If the return code is zero, this
287 * function must have filled in request_buf pointer.
290 small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
295 rc = smb2_reconnect(smb2_command, tcon);
299 /* BB eventually switch this to SMB2 specific small buf size */
300 *request_buf = cifs_small_buf_get();
301 if (*request_buf == NULL) {
302 /* BB should we add a retry in here if not a writepage? */
306 smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
309 #ifdef CONFIG_CIFS_STATS2
310 uint16_t com_code = le16_to_cpu(smb2_command);
311 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
313 cifs_stats_inc(&tcon->num_smbs_sent);
319 #ifdef CONFIG_CIFS_SMB311
320 /* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
321 #define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
324 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
325 #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
328 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
330 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
331 pneg_ctxt->DataLength = cpu_to_le16(38);
332 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
333 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
334 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
335 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
339 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
341 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
342 pneg_ctxt->DataLength = cpu_to_le16(6);
343 pneg_ctxt->CipherCount = cpu_to_le16(2);
344 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
345 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
349 assemble_neg_contexts(struct smb2_negotiate_req *req)
352 /* +4 is to account for the RFC1001 len field */
353 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
355 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
356 /* Add 2 to size to round to 8 byte boundary */
357 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
358 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
359 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
360 req->NegotiateContextCount = cpu_to_le16(2);
361 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2
362 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
365 static void assemble_neg_contexts(struct smb2_negotiate_req *req)
374 * SMB2 Worker functions follow:
376 * The general structure of the worker functions is:
377 * 1) Call smb2_init (assembles SMB2 header)
378 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
379 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
380 * 4) Decode SMB2 command specific fields in the fixed length area
381 * 5) Decode variable length data area (if any for this SMB2 command type)
382 * 6) Call free smb buffer
388 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
390 struct smb2_negotiate_req *req;
391 struct smb2_negotiate_rsp *rsp;
395 struct TCP_Server_Info *server = ses->server;
396 int blob_offset, blob_length;
398 int flags = CIFS_NEG_OP;
400 cifs_dbg(FYI, "Negotiate protocol\n");
403 WARN(1, "%s: server is NULL!\n", __func__);
407 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
411 req->hdr.SessionId = 0;
413 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
415 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
416 inc_rfc1001_len(req, 2);
418 /* only one of SMB2 signing flags may be set in SMB2 request */
420 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
421 else if (global_secflags & CIFSSEC_MAY_SIGN)
422 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
424 req->SecurityMode = 0;
426 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
428 /* ClientGUID must be zero for SMB2.02 dialect */
429 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
430 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
432 memcpy(req->ClientGUID, server->client_guid,
433 SMB2_CLIENT_GUID_SIZE);
434 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
435 assemble_neg_contexts(req);
437 iov[0].iov_base = (char *)req;
438 /* 4 for rfc1002 length field */
439 iov[0].iov_len = get_rfc1002_length(req) + 4;
441 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
443 rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
445 * No tcon so can't do
446 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
451 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
453 /* BB we may eventually want to match the negotiated vs. requested
454 dialect, even though we are only requesting one at a time */
455 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
456 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
457 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
458 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
459 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
460 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
461 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
462 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
463 #ifdef CONFIG_CIFS_SMB311
464 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
465 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
468 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
469 le16_to_cpu(rsp->DialectRevision));
473 server->dialect = le16_to_cpu(rsp->DialectRevision);
475 /* SMB2 only has an extended negflavor */
476 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
477 /* set it to the maximum buffer size value we can send with 1 credit */
478 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
479 SMB2_MAX_BUFFER_SIZE);
480 server->max_read = le32_to_cpu(rsp->MaxReadSize);
481 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
482 /* BB Do we need to validate the SecurityMode? */
483 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
484 server->capabilities = le32_to_cpu(rsp->Capabilities);
486 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
488 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
491 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
493 * ses->sectype = RawNTLMSSP;
494 * but for time being this is our only auth choice so doesn't matter.
495 * We just found a server which sets blob length to zero expecting raw.
497 if (blob_length == 0)
498 cifs_dbg(FYI, "missing security blob on negprot\n");
500 rc = cifs_enable_signing(server, ses->sign);
504 rc = decode_negTokenInit(security_blob, blob_length, server);
511 free_rsp_buf(resp_buftype, rsp);
515 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
518 struct validate_negotiate_info_req vneg_inbuf;
519 struct validate_negotiate_info_rsp *pneg_rsp;
522 cifs_dbg(FYI, "validate negotiate\n");
525 * validation ioctl must be signed, so no point sending this if we
526 * can not sign it. We could eventually change this to selectively
527 * sign just this, the first and only signed request on a connection.
528 * This is good enough for now since a user who wants better security
529 * would also enable signing on the mount. Having validation of
530 * negotiate info for signed connections helps reduce attack vectors
532 if (tcon->ses->server->sign == false)
533 return 0; /* validation requires signing */
535 vneg_inbuf.Capabilities =
536 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
537 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
538 SMB2_CLIENT_GUID_SIZE);
541 vneg_inbuf.SecurityMode =
542 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
543 else if (global_secflags & CIFSSEC_MAY_SIGN)
544 vneg_inbuf.SecurityMode =
545 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
547 vneg_inbuf.SecurityMode = 0;
549 vneg_inbuf.DialectCount = cpu_to_le16(1);
550 vneg_inbuf.Dialects[0] =
551 cpu_to_le16(tcon->ses->server->vals->protocol_id);
553 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
554 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
555 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
556 (char **)&pneg_rsp, &rsplen);
559 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
563 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
564 cifs_dbg(VFS, "invalid size of protocol negotiate response\n");
568 /* check validate negotiate info response matches what we got earlier */
569 if (pneg_rsp->Dialect !=
570 cpu_to_le16(tcon->ses->server->vals->protocol_id))
573 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
576 /* do not validate server guid because not saved at negprot time yet */
578 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
579 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
582 /* validate negotiate successful */
583 cifs_dbg(FYI, "validate negotiate info successful\n");
587 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
592 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
593 const struct nls_table *nls_cp)
595 struct smb2_sess_setup_req *req;
596 struct smb2_sess_setup_rsp *rsp = NULL;
599 int resp_buftype = CIFS_NO_BUFFER;
600 __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */
601 struct TCP_Server_Info *server = ses->server;
603 struct key *spnego_key = NULL;
604 char *security_blob = NULL;
605 unsigned char *ntlmssp_blob = NULL;
606 bool use_spnego = false; /* else use raw ntlmssp */
607 u64 previous_session = ses->Suid;
609 cifs_dbg(FYI, "Session Setup\n");
612 WARN(1, "%s: server is NULL!\n", __func__);
617 * If we are here due to reconnect, free per-smb session key
618 * in case signing was required.
620 kfree(ses->auth_key.response);
621 ses->auth_key.response = NULL;
624 * If memory allocation is successful, caller of this function
627 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
630 ses->ntlmssp->sesskey_per_smbsess = true;
632 /* FIXME: allow for other auth types besides NTLMSSP (e.g. krb5) */
633 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
634 ses->sectype = RawNTLMSSP;
636 ssetup_ntlmssp_authenticate:
637 if (phase == NtLmChallenge)
638 phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
640 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
644 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
646 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
647 req->PreviousSessionId = previous_session;
649 req->Flags = 0; /* MBZ */
650 /* to enable echos and oplocks */
651 req->hdr.CreditRequest = cpu_to_le16(3);
653 /* only one of SMB2 signing flags may be set in SMB2 request */
655 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
656 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
657 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
659 req->SecurityMode = 0;
661 req->Capabilities = 0;
662 req->Channel = 0; /* MBZ */
664 iov[0].iov_base = (char *)req;
665 /* 4 for rfc1002 length field and 1 for pad */
666 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
668 if (ses->sectype == Kerberos) {
669 #ifdef CONFIG_CIFS_UPCALL
670 struct cifs_spnego_msg *msg;
672 spnego_key = cifs_get_spnego_key(ses);
673 if (IS_ERR(spnego_key)) {
674 rc = PTR_ERR(spnego_key);
679 msg = spnego_key->payload.data[0];
681 * check version field to make sure that cifs.upcall is
682 * sending us a response in an expected form
684 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
686 "bad cifs.upcall version. Expected %d got %d",
687 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
691 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
693 if (!ses->auth_key.response) {
695 "Kerberos can't allocate (%u bytes) memory",
700 ses->auth_key.len = msg->sesskey_len;
701 blob_length = msg->secblob_len;
702 iov[1].iov_base = msg->data + msg->sesskey_len;
703 iov[1].iov_len = blob_length;
707 #endif /* CONFIG_CIFS_UPCALL */
708 } else if (phase == NtLmNegotiate) { /* if not krb5 must be ntlmssp */
709 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
711 if (ntlmssp_blob == NULL) {
715 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
717 /* blob_length = build_spnego_ntlmssp_blob(
719 sizeof(struct _NEGOTIATE_MESSAGE),
721 /* BB eventually need to add this */
722 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
727 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
728 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
729 security_blob = ntlmssp_blob;
731 iov[1].iov_base = security_blob;
732 iov[1].iov_len = blob_length;
733 } else if (phase == NtLmAuthenticate) {
734 req->hdr.SessionId = ses->Suid;
735 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
738 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n",
740 goto ssetup_exit; /* BB double check error handling */
743 /* blob_length = build_spnego_ntlmssp_blob(
747 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
752 security_blob = ntlmssp_blob;
754 iov[1].iov_base = security_blob;
755 iov[1].iov_len = blob_length;
757 cifs_dbg(VFS, "illegal ntlmssp phase\n");
762 /* Testing shows that buffer offset must be at location of Buffer[0] */
763 req->SecurityBufferOffset =
764 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
765 1 /* pad */ - 4 /* rfc1001 len */);
766 req->SecurityBufferLength = cpu_to_le16(blob_length);
768 inc_rfc1001_len(req, blob_length - 1 /* pad */);
770 /* BB add code to build os and lm fields */
772 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype,
773 CIFS_LOG_ERROR | CIFS_NEG_OP);
775 kfree(security_blob);
776 rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
777 ses->Suid = rsp->hdr.SessionId;
778 if (resp_buftype != CIFS_NO_BUFFER &&
779 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
780 if (phase != NtLmNegotiate) {
781 cifs_dbg(VFS, "Unexpected more processing error\n");
784 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
785 le16_to_cpu(rsp->SecurityBufferOffset)) {
786 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
787 le16_to_cpu(rsp->SecurityBufferOffset));
792 /* NTLMSSP Negotiate sent now processing challenge (response) */
793 phase = NtLmChallenge; /* process ntlmssp challenge */
794 rc = 0; /* MORE_PROCESSING is not an error here but expected */
795 rc = decode_ntlmssp_challenge(rsp->Buffer,
796 le16_to_cpu(rsp->SecurityBufferLength), ses);
800 * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
801 * but at least the raw NTLMSSP case works.
804 * No tcon so can't do
805 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
810 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
811 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
812 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
814 free_rsp_buf(resp_buftype, rsp);
816 /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
817 if ((phase == NtLmChallenge) && (rc == 0))
818 goto ssetup_ntlmssp_authenticate;
821 mutex_lock(&server->srv_mutex);
822 if (server->sign && server->ops->generate_signingkey) {
823 rc = server->ops->generate_signingkey(ses);
824 kfree(ses->auth_key.response);
825 ses->auth_key.response = NULL;
828 "SMB3 session key generation failed\n");
829 mutex_unlock(&server->srv_mutex);
833 if (!server->session_estab) {
834 server->sequence_number = 0x2;
835 server->session_estab = true;
837 mutex_unlock(&server->srv_mutex);
839 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
840 spin_lock(&GlobalMid_Lock);
841 ses->status = CifsGood;
842 ses->need_reconnect = false;
843 spin_unlock(&GlobalMid_Lock);
848 kfree(ses->auth_key.response);
849 ses->auth_key.response = NULL;
852 key_invalidate(spnego_key);
861 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
863 struct smb2_logoff_req *req; /* response is also trivial struct */
865 struct TCP_Server_Info *server;
867 cifs_dbg(FYI, "disconnect session %p\n", ses);
869 if (ses && (ses->server))
870 server = ses->server;
874 /* no need to send SMB logoff if uid already closed due to reconnect */
875 if (ses->need_reconnect)
876 goto smb2_session_already_dead;
878 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
882 /* since no tcon, smb2_init can not do this, so do here */
883 req->hdr.SessionId = ses->Suid;
885 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
887 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
889 * No tcon so can't do
890 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
893 smb2_session_already_dead:
897 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
899 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
902 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
904 /* These are similar values to what Windows uses */
905 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
907 tcon->max_chunks = 256;
908 tcon->max_bytes_chunk = 1048576;
909 tcon->max_bytes_copy = 16777216;
913 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
914 struct cifs_tcon *tcon, const struct nls_table *cp)
916 struct smb2_tree_connect_req *req;
917 struct smb2_tree_connect_rsp *rsp = NULL;
922 struct TCP_Server_Info *server;
923 __le16 *unc_path = NULL;
925 cifs_dbg(FYI, "TCON\n");
927 if ((ses->server) && tree)
928 server = ses->server;
932 if (tcon && tcon->bad_network_name)
935 if ((tcon && tcon->seal) &&
936 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
937 cifs_dbg(VFS, "encryption requested but no server support");
941 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
942 if (unc_path == NULL)
945 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
947 if (unc_path_len < 2) {
952 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
959 /* since no tcon, smb2_init can not do this, so do here */
960 req->hdr.SessionId = ses->Suid;
961 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
962 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
965 iov[0].iov_base = (char *)req;
966 /* 4 for rfc1002 length field and 1 for pad */
967 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
969 /* Testing shows that buffer offset must be at location of Buffer[0] */
970 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
971 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
972 req->PathLength = cpu_to_le16(unc_path_len - 2);
973 iov[1].iov_base = unc_path;
974 iov[1].iov_len = unc_path_len;
976 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
978 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
979 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
983 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
984 tcon->need_reconnect = true;
986 goto tcon_error_exit;
990 ses->ipc_tid = rsp->hdr.TreeId;
994 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
995 cifs_dbg(FYI, "connection to disk share\n");
996 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
998 cifs_dbg(FYI, "connection to pipe share\n");
999 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1001 cifs_dbg(FYI, "connection to printer\n");
1003 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1005 goto tcon_error_exit;
1008 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1009 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1010 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1011 tcon->tidStatus = CifsGood;
1012 tcon->need_reconnect = false;
1013 tcon->tid = rsp->hdr.TreeId;
1014 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1016 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1017 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1018 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1019 init_copy_chunk_defaults(tcon);
1020 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1021 cifs_dbg(VFS, "Encrypted shares not supported");
1022 if (tcon->ses->server->ops->validate_negotiate)
1023 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1025 free_rsp_buf(resp_buftype, rsp);
1030 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
1031 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1033 tcon->bad_network_name = true;
1039 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1041 struct smb2_tree_disconnect_req *req; /* response is trivial */
1043 struct TCP_Server_Info *server;
1044 struct cifs_ses *ses = tcon->ses;
1046 cifs_dbg(FYI, "Tree Disconnect\n");
1048 if (ses && (ses->server))
1049 server = ses->server;
1053 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1056 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1060 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
1062 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1068 static struct create_durable *
1069 create_durable_buf(void)
1071 struct create_durable *buf;
1073 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1077 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1078 (struct create_durable, Data));
1079 buf->ccontext.DataLength = cpu_to_le32(16);
1080 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1081 (struct create_durable, Name));
1082 buf->ccontext.NameLength = cpu_to_le16(4);
1083 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1091 static struct create_durable *
1092 create_reconnect_durable_buf(struct cifs_fid *fid)
1094 struct create_durable *buf;
1096 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1100 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1101 (struct create_durable, Data));
1102 buf->ccontext.DataLength = cpu_to_le32(16);
1103 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1104 (struct create_durable, Name));
1105 buf->ccontext.NameLength = cpu_to_le16(4);
1106 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1107 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1108 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1117 parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1118 unsigned int *epoch)
1121 struct create_context *cc;
1123 unsigned int remaining;
1126 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
1127 remaining = le32_to_cpu(rsp->CreateContextsLength);
1128 cc = (struct create_context *)data_offset;
1129 while (remaining >= sizeof(struct create_context)) {
1130 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1131 if (le16_to_cpu(cc->NameLength) == 4 &&
1132 strncmp(name, "RqLs", 4) == 0)
1133 return server->ops->parse_lease_buf(cc, epoch);
1135 next = le32_to_cpu(cc->Next);
1139 cc = (struct create_context *)((char *)cc + next);
1146 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1147 unsigned int *num_iovec, __u8 *oplock)
1149 struct smb2_create_req *req = iov[0].iov_base;
1150 unsigned int num = *num_iovec;
1152 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
1153 if (iov[num].iov_base == NULL)
1155 iov[num].iov_len = server->vals->create_lease_size;
1156 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1157 if (!req->CreateContextsOffset)
1158 req->CreateContextsOffset = cpu_to_le32(
1159 sizeof(struct smb2_create_req) - 4 +
1160 iov[num - 1].iov_len);
1161 le32_add_cpu(&req->CreateContextsLength,
1162 server->vals->create_lease_size);
1163 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
1164 *num_iovec = num + 1;
1168 static struct create_durable_v2 *
1169 create_durable_v2_buf(struct cifs_fid *pfid)
1171 struct create_durable_v2 *buf;
1173 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1177 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1178 (struct create_durable_v2, dcontext));
1179 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1180 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1181 (struct create_durable_v2, Name));
1182 buf->ccontext.NameLength = cpu_to_le16(4);
1184 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1185 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1186 generate_random_uuid(buf->dcontext.CreateGuid);
1187 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1189 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1197 static struct create_durable_handle_reconnect_v2 *
1198 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1200 struct create_durable_handle_reconnect_v2 *buf;
1202 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1207 buf->ccontext.DataOffset =
1208 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1210 buf->ccontext.DataLength =
1211 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1212 buf->ccontext.NameOffset =
1213 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1215 buf->ccontext.NameLength = cpu_to_le16(4);
1217 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1218 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1219 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1220 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1222 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1231 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1232 struct cifs_open_parms *oparms)
1234 struct smb2_create_req *req = iov[0].iov_base;
1235 unsigned int num = *num_iovec;
1237 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1238 if (iov[num].iov_base == NULL)
1240 iov[num].iov_len = sizeof(struct create_durable_v2);
1241 if (!req->CreateContextsOffset)
1242 req->CreateContextsOffset =
1243 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1245 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1246 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1247 *num_iovec = num + 1;
1252 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1253 struct cifs_open_parms *oparms)
1255 struct smb2_create_req *req = iov[0].iov_base;
1256 unsigned int num = *num_iovec;
1258 /* indicate that we don't need to relock the file */
1259 oparms->reconnect = false;
1261 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1262 if (iov[num].iov_base == NULL)
1264 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1265 if (!req->CreateContextsOffset)
1266 req->CreateContextsOffset =
1267 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1269 le32_add_cpu(&req->CreateContextsLength,
1270 sizeof(struct create_durable_handle_reconnect_v2));
1271 inc_rfc1001_len(&req->hdr,
1272 sizeof(struct create_durable_handle_reconnect_v2));
1273 *num_iovec = num + 1;
1278 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1279 struct cifs_open_parms *oparms, bool use_persistent)
1281 struct smb2_create_req *req = iov[0].iov_base;
1282 unsigned int num = *num_iovec;
1284 if (use_persistent) {
1285 if (oparms->reconnect)
1286 return add_durable_reconnect_v2_context(iov, num_iovec,
1289 return add_durable_v2_context(iov, num_iovec, oparms);
1292 if (oparms->reconnect) {
1293 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1294 /* indicate that we don't need to relock the file */
1295 oparms->reconnect = false;
1297 iov[num].iov_base = create_durable_buf();
1298 if (iov[num].iov_base == NULL)
1300 iov[num].iov_len = sizeof(struct create_durable);
1301 if (!req->CreateContextsOffset)
1302 req->CreateContextsOffset =
1303 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1305 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1306 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1307 *num_iovec = num + 1;
1312 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
1313 __u8 *oplock, struct smb2_file_all_info *buf,
1314 struct smb2_err_rsp **err_buf)
1316 struct smb2_create_req *req;
1317 struct smb2_create_rsp *rsp;
1318 struct TCP_Server_Info *server;
1319 struct cifs_tcon *tcon = oparms->tcon;
1320 struct cifs_ses *ses = tcon->ses;
1324 __le16 *copy_path = NULL;
1327 unsigned int num_iovecs = 2;
1328 __u32 file_attributes = 0;
1329 char *dhc_buf = NULL, *lc_buf = NULL;
1331 cifs_dbg(FYI, "create/open\n");
1333 if (ses && (ses->server))
1334 server = ses->server;
1338 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1342 if (oparms->create_options & CREATE_OPTION_READONLY)
1343 file_attributes |= ATTR_READONLY;
1344 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1345 file_attributes |= ATTR_SYSTEM;
1347 req->ImpersonationLevel = IL_IMPERSONATION;
1348 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
1349 /* File attributes ignored on open (used in create though) */
1350 req->FileAttributes = cpu_to_le32(file_attributes);
1351 req->ShareAccess = FILE_SHARE_ALL_LE;
1352 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1353 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
1354 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1355 /* do not count rfc1001 len field */
1356 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1358 iov[0].iov_base = (char *)req;
1359 /* 4 for rfc1002 length field */
1360 iov[0].iov_len = get_rfc1002_length(req) + 4;
1362 /* MUST set path len (NameLength) to 0 opening root of share */
1363 req->NameLength = cpu_to_le16(uni_path_len - 2);
1364 /* -1 since last byte is buf[0] which is sent below (path) */
1366 if (uni_path_len % 8 != 0) {
1367 copy_size = uni_path_len / 8 * 8;
1368 if (copy_size < uni_path_len)
1371 copy_path = kzalloc(copy_size, GFP_KERNEL);
1374 memcpy((char *)copy_path, (const char *)path,
1376 uni_path_len = copy_size;
1380 iov[1].iov_len = uni_path_len;
1381 iov[1].iov_base = path;
1382 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1383 inc_rfc1001_len(req, uni_path_len - 1);
1385 if (!server->oplocks)
1386 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1388 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1389 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1390 req->RequestedOplockLevel = *oplock;
1392 rc = add_lease_context(server, iov, &num_iovecs, oplock);
1394 cifs_small_buf_release(req);
1398 lc_buf = iov[num_iovecs-1].iov_base;
1401 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1402 /* need to set Next field of lease context if we request it */
1403 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
1404 struct create_context *ccontext =
1405 (struct create_context *)iov[num_iovecs-1].iov_base;
1407 cpu_to_le32(server->vals->create_lease_size);
1410 rc = add_durable_context(iov, &num_iovecs, oparms,
1411 tcon->use_persistent);
1413 cifs_small_buf_release(req);
1418 dhc_buf = iov[num_iovecs-1].iov_base;
1421 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1422 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1425 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
1427 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1432 oparms->fid->persistent_fid = rsp->PersistentFileId;
1433 oparms->fid->volatile_fid = rsp->VolatileFileId;
1436 memcpy(buf, &rsp->CreationTime, 32);
1437 buf->AllocationSize = rsp->AllocationSize;
1438 buf->EndOfFile = rsp->EndofFile;
1439 buf->Attributes = rsp->FileAttributes;
1440 buf->NumberOfLinks = cpu_to_le32(1);
1441 buf->DeletePending = 0;
1444 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
1445 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
1447 *oplock = rsp->OplockLevel;
1452 free_rsp_buf(resp_buftype, rsp);
1457 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1460 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1461 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1462 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1464 struct smb2_ioctl_req *req;
1465 struct smb2_ioctl_rsp *rsp;
1466 struct TCP_Server_Info *server;
1467 struct cifs_ses *ses;
1473 cifs_dbg(FYI, "SMB2 IOCTL\n");
1475 if (out_data != NULL)
1478 /* zero out returned data len, in case of error */
1487 if (ses && (ses->server))
1488 server = ses->server;
1492 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1496 req->CtlCode = cpu_to_le32(opcode);
1497 req->PersistentFileId = persistent_fid;
1498 req->VolatileFileId = volatile_fid;
1501 req->InputCount = cpu_to_le32(indatalen);
1502 /* do not set InputOffset if no input data */
1504 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1505 iov[1].iov_base = in_data;
1506 iov[1].iov_len = indatalen;
1511 req->OutputOffset = 0;
1512 req->OutputCount = 0; /* MBZ */
1515 * Could increase MaxOutputResponse, but that would require more
1516 * than one credit. Windows typically sets this smaller, but for some
1517 * ioctls it may be useful to allow server to send more. No point
1518 * limiting what the server can send as long as fits in one credit
1520 req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1523 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1527 iov[0].iov_base = (char *)req;
1530 * If no input data, the size of ioctl struct in
1531 * protocol spec still includes a 1 byte data buffer,
1532 * but if input data passed to ioctl, we do not
1533 * want to double count this, so we do not send
1534 * the dummy one byte of data in iovec[0] if sending
1535 * input data (in iovec[1]). We also must add 4 bytes
1536 * in first iovec to allow for rfc1002 length field.
1540 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1541 inc_rfc1001_len(req, indatalen - 1);
1543 iov[0].iov_len = get_rfc1002_length(req) + 4;
1546 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1547 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1549 if ((rc != 0) && (rc != -EINVAL)) {
1550 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1552 } else if (rc == -EINVAL) {
1553 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1554 (opcode != FSCTL_SRV_COPYCHUNK)) {
1555 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1560 /* check if caller wants to look at return data or just return rc */
1561 if ((plen == NULL) || (out_data == NULL))
1564 *plen = le32_to_cpu(rsp->OutputCount);
1566 /* We check for obvious errors in the output buffer length and offset */
1568 goto ioctl_exit; /* server returned no data */
1569 else if (*plen > 0xFF00) {
1570 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1576 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1577 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1578 le32_to_cpu(rsp->OutputOffset));
1584 *out_data = kmalloc(*plen, GFP_KERNEL);
1585 if (*out_data == NULL) {
1591 (char *)&rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
1594 free_rsp_buf(resp_buftype, rsp);
1599 * Individual callers to ioctl worker function follow
1603 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1604 u64 persistent_fid, u64 volatile_fid)
1607 struct compress_ioctl fsctl_input;
1608 char *ret_data = NULL;
1610 fsctl_input.CompressionState =
1611 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
1613 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1614 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1615 (char *)&fsctl_input /* data input */,
1616 2 /* in data len */, &ret_data /* out data */, NULL);
1618 cifs_dbg(FYI, "set compression rc %d\n", rc);
1624 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1625 u64 persistent_fid, u64 volatile_fid)
1627 struct smb2_close_req *req;
1628 struct smb2_close_rsp *rsp;
1629 struct TCP_Server_Info *server;
1630 struct cifs_ses *ses = tcon->ses;
1635 cifs_dbg(FYI, "Close\n");
1637 if (ses && (ses->server))
1638 server = ses->server;
1642 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1646 req->PersistentFileId = persistent_fid;
1647 req->VolatileFileId = volatile_fid;
1649 iov[0].iov_base = (char *)req;
1650 /* 4 for rfc1002 length field */
1651 iov[0].iov_len = get_rfc1002_length(req) + 4;
1653 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1654 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1657 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
1661 /* BB FIXME - decode close response, update inode for caching */
1664 free_rsp_buf(resp_buftype, rsp);
1669 validate_buf(unsigned int offset, unsigned int buffer_length,
1670 struct smb2_hdr *hdr, unsigned int min_buf_size)
1673 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1674 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1675 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1676 char *end_of_buf = begin_of_buf + buffer_length;
1679 if (buffer_length < min_buf_size) {
1680 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1681 buffer_length, min_buf_size);
1685 /* check if beyond RFC1001 maximum length */
1686 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
1687 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1688 buffer_length, smb_len);
1692 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
1693 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
1701 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1702 * Caller must free buffer.
1705 validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1706 struct smb2_hdr *hdr, unsigned int minbufsize,
1710 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1716 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1720 memcpy(data, begin_of_buf, buffer_length);
1726 query_info(const unsigned int xid, struct cifs_tcon *tcon,
1727 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1728 size_t output_len, size_t min_len, void *data)
1730 struct smb2_query_info_req *req;
1731 struct smb2_query_info_rsp *rsp = NULL;
1735 struct TCP_Server_Info *server;
1736 struct cifs_ses *ses = tcon->ses;
1738 cifs_dbg(FYI, "Query Info\n");
1740 if (ses && (ses->server))
1741 server = ses->server;
1745 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1749 req->InfoType = SMB2_O_INFO_FILE;
1750 req->FileInfoClass = info_class;
1751 req->PersistentFileId = persistent_fid;
1752 req->VolatileFileId = volatile_fid;
1753 /* 4 for rfc1002 length field and 1 for Buffer */
1754 req->InputBufferOffset =
1755 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
1756 req->OutputBufferLength = cpu_to_le32(output_len);
1758 iov[0].iov_base = (char *)req;
1759 /* 4 for rfc1002 length field */
1760 iov[0].iov_len = get_rfc1002_length(req) + 4;
1762 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1763 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1766 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1770 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1771 le32_to_cpu(rsp->OutputBufferLength),
1772 &rsp->hdr, min_len, data);
1775 free_rsp_buf(resp_buftype, rsp);
1780 SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1781 u64 persistent_fid, u64 volatile_fid,
1782 struct smb2_file_all_info *data)
1784 return query_info(xid, tcon, persistent_fid, volatile_fid,
1785 FILE_ALL_INFORMATION,
1786 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
1787 sizeof(struct smb2_file_all_info), data);
1791 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1792 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1794 return query_info(xid, tcon, persistent_fid, volatile_fid,
1795 FILE_INTERNAL_INFORMATION,
1796 sizeof(struct smb2_file_internal_info),
1797 sizeof(struct smb2_file_internal_info), uniqueid);
1801 * This is a no-op for now. We're not really interested in the reply, but
1802 * rather in the fact that the server sent one and that server->lstrp
1805 * FIXME: maybe we should consider checking that the reply matches request?
1808 smb2_echo_callback(struct mid_q_entry *mid)
1810 struct TCP_Server_Info *server = mid->callback_data;
1811 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1812 unsigned int credits_received = 1;
1814 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1815 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1817 mutex_lock(&server->srv_mutex);
1818 DeleteMidQEntry(mid);
1819 mutex_unlock(&server->srv_mutex);
1820 add_credits(server, credits_received, CIFS_ECHO_OP);
1824 SMB2_echo(struct TCP_Server_Info *server)
1826 struct smb2_echo_req *req;
1829 struct smb_rqst rqst = { .rq_iov = &iov,
1832 cifs_dbg(FYI, "In echo request\n");
1834 if (server->tcpStatus == CifsNeedNegotiate) {
1835 struct list_head *tmp, *tmp2;
1836 struct cifs_ses *ses;
1837 struct cifs_tcon *tcon;
1839 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
1840 spin_lock(&cifs_tcp_ses_lock);
1841 list_for_each(tmp, &server->smb_ses_list) {
1842 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
1843 list_for_each(tmp2, &ses->tcon_list) {
1844 tcon = list_entry(tmp2, struct cifs_tcon,
1846 /* add check for persistent handle reconnect */
1847 if (tcon && tcon->need_reconnect) {
1848 spin_unlock(&cifs_tcp_ses_lock);
1849 rc = smb2_reconnect(SMB2_ECHO, tcon);
1850 spin_lock(&cifs_tcp_ses_lock);
1854 spin_unlock(&cifs_tcp_ses_lock);
1857 /* if no session, renegotiate failed above */
1858 if (server->tcpStatus == CifsNeedNegotiate)
1861 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
1865 req->hdr.CreditRequest = cpu_to_le16(1);
1867 iov.iov_base = (char *)req;
1868 /* 4 for rfc1002 length field */
1869 iov.iov_len = get_rfc1002_length(req) + 4;
1871 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
1874 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
1876 cifs_small_buf_release(req);
1881 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1884 struct smb2_flush_req *req;
1885 struct TCP_Server_Info *server;
1886 struct cifs_ses *ses = tcon->ses;
1891 cifs_dbg(FYI, "Flush\n");
1893 if (ses && (ses->server))
1894 server = ses->server;
1898 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
1902 req->PersistentFileId = persistent_fid;
1903 req->VolatileFileId = volatile_fid;
1905 iov[0].iov_base = (char *)req;
1906 /* 4 for rfc1002 length field */
1907 iov[0].iov_len = get_rfc1002_length(req) + 4;
1909 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1912 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
1914 free_rsp_buf(resp_buftype, iov[0].iov_base);
1919 * To form a chain of read requests, any read requests after the first should
1920 * have the end_of_chain boolean set to true.
1923 smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
1924 unsigned int remaining_bytes, int request_type)
1927 struct smb2_read_req *req = NULL;
1929 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
1932 if (io_parms->tcon->ses->server == NULL)
1933 return -ECONNABORTED;
1935 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
1937 req->PersistentFileId = io_parms->persistent_fid;
1938 req->VolatileFileId = io_parms->volatile_fid;
1939 req->ReadChannelInfoOffset = 0; /* reserved */
1940 req->ReadChannelInfoLength = 0; /* reserved */
1941 req->Channel = 0; /* reserved */
1942 req->MinimumCount = 0;
1943 req->Length = cpu_to_le32(io_parms->length);
1944 req->Offset = cpu_to_le64(io_parms->offset);
1946 if (request_type & CHAINED_REQUEST) {
1947 if (!(request_type & END_OF_CHAIN)) {
1948 /* 4 for rfc1002 length field */
1949 req->hdr.NextCommand =
1950 cpu_to_le32(get_rfc1002_length(req) + 4);
1951 } else /* END_OF_CHAIN */
1952 req->hdr.NextCommand = 0;
1953 if (request_type & RELATED_REQUEST) {
1954 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
1956 * Related requests use info from previous read request
1959 req->hdr.SessionId = 0xFFFFFFFF;
1960 req->hdr.TreeId = 0xFFFFFFFF;
1961 req->PersistentFileId = 0xFFFFFFFF;
1962 req->VolatileFileId = 0xFFFFFFFF;
1965 if (remaining_bytes > io_parms->length)
1966 req->RemainingBytes = cpu_to_le32(remaining_bytes);
1968 req->RemainingBytes = 0;
1970 iov[0].iov_base = (char *)req;
1971 /* 4 for rfc1002 length field */
1972 iov[0].iov_len = get_rfc1002_length(req) + 4;
1977 smb2_readv_callback(struct mid_q_entry *mid)
1979 struct cifs_readdata *rdata = mid->callback_data;
1980 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1981 struct TCP_Server_Info *server = tcon->ses->server;
1982 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
1983 unsigned int credits_received = 1;
1984 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
1986 .rq_pages = rdata->pages,
1987 .rq_npages = rdata->nr_pages,
1988 .rq_pagesz = rdata->pagesz,
1989 .rq_tailsz = rdata->tailsz };
1991 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1992 __func__, mid->mid, mid->mid_state, rdata->result,
1995 switch (mid->mid_state) {
1996 case MID_RESPONSE_RECEIVED:
1997 credits_received = le16_to_cpu(buf->CreditRequest);
1998 /* result already set, check signature */
2002 rc = smb2_verify_signature(&rqst, server);
2004 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2007 /* FIXME: should this be counted toward the initiating task? */
2008 task_io_account_read(rdata->got_bytes);
2009 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2011 case MID_REQUEST_SUBMITTED:
2012 case MID_RETRY_NEEDED:
2013 rdata->result = -EAGAIN;
2014 if (server->sign && rdata->got_bytes)
2015 /* reset bytes number since we can not check a sign */
2016 rdata->got_bytes = 0;
2017 /* FIXME: should this be counted toward the initiating task? */
2018 task_io_account_read(rdata->got_bytes);
2019 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2022 if (rdata->result != -ENODATA)
2023 rdata->result = -EIO;
2027 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2029 queue_work(cifsiod_wq, &rdata->work);
2030 mutex_lock(&server->srv_mutex);
2031 DeleteMidQEntry(mid);
2032 mutex_unlock(&server->srv_mutex);
2033 add_credits(server, credits_received, 0);
2036 /* smb2_async_readv - send an async write, and set up mid to handle result */
2038 smb2_async_readv(struct cifs_readdata *rdata)
2041 struct smb2_hdr *buf;
2042 struct cifs_io_parms io_parms;
2043 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
2045 struct TCP_Server_Info *server;
2047 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2048 __func__, rdata->offset, rdata->bytes);
2050 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2051 io_parms.offset = rdata->offset;
2052 io_parms.length = rdata->bytes;
2053 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2054 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2055 io_parms.pid = rdata->pid;
2057 server = io_parms.tcon->ses->server;
2059 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
2061 if (rc == -EAGAIN && rdata->credits) {
2062 /* credits was reset by reconnect */
2064 /* reduce in_flight value since we won't send the req */
2065 spin_lock(&server->req_lock);
2066 server->in_flight--;
2067 spin_unlock(&server->req_lock);
2072 buf = (struct smb2_hdr *)rdata->iov.iov_base;
2073 /* 4 for rfc1002 length field */
2074 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
2076 if (rdata->credits) {
2077 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2078 SMB2_MAX_BUFFER_SIZE));
2079 buf->CreditRequest = buf->CreditCharge;
2080 spin_lock(&server->req_lock);
2081 server->credits += rdata->credits -
2082 le16_to_cpu(buf->CreditCharge);
2083 spin_unlock(&server->req_lock);
2084 wake_up(&server->request_q);
2085 flags = CIFS_HAS_CREDITS;
2088 kref_get(&rdata->refcount);
2089 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
2090 cifs_readv_receive, smb2_readv_callback,
2093 kref_put(&rdata->refcount, cifs_readdata_release);
2094 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2097 cifs_small_buf_release(buf);
2102 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2103 unsigned int *nbytes, char **buf, int *buf_type)
2105 int resp_buftype, rc = -EACCES;
2106 struct smb2_read_rsp *rsp = NULL;
2110 rc = smb2_new_read_req(iov, io_parms, 0, 0);
2114 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2115 &resp_buftype, CIFS_LOG_ERROR);
2117 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
2119 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
2120 free_rsp_buf(resp_buftype, iov[0].iov_base);
2125 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2126 cifs_dbg(VFS, "Send error in read = %d\n", rc);
2128 *nbytes = le32_to_cpu(rsp->DataLength);
2129 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2130 (*nbytes > io_parms->length)) {
2131 cifs_dbg(FYI, "bad length %d for count %d\n",
2132 *nbytes, io_parms->length);
2139 memcpy(*buf, (char *)&rsp->hdr.ProtocolId + rsp->DataOffset,
2141 free_rsp_buf(resp_buftype, iov[0].iov_base);
2142 } else if (resp_buftype != CIFS_NO_BUFFER) {
2143 *buf = iov[0].iov_base;
2144 if (resp_buftype == CIFS_SMALL_BUFFER)
2145 *buf_type = CIFS_SMALL_BUFFER;
2146 else if (resp_buftype == CIFS_LARGE_BUFFER)
2147 *buf_type = CIFS_LARGE_BUFFER;
2153 * Check the mid_state and signature on received buffer (if any), and queue the
2154 * workqueue completion task.
2157 smb2_writev_callback(struct mid_q_entry *mid)
2159 struct cifs_writedata *wdata = mid->callback_data;
2160 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2161 struct TCP_Server_Info *server = tcon->ses->server;
2162 unsigned int written;
2163 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2164 unsigned int credits_received = 1;
2166 switch (mid->mid_state) {
2167 case MID_RESPONSE_RECEIVED:
2168 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
2169 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2170 if (wdata->result != 0)
2173 written = le32_to_cpu(rsp->DataLength);
2175 * Mask off high 16 bits when bytes written as returned
2176 * by the server is greater than bytes requested by the
2177 * client. OS/2 servers are known to set incorrect
2180 if (written > wdata->bytes)
2183 if (written < wdata->bytes)
2184 wdata->result = -ENOSPC;
2186 wdata->bytes = written;
2188 case MID_REQUEST_SUBMITTED:
2189 case MID_RETRY_NEEDED:
2190 wdata->result = -EAGAIN;
2193 wdata->result = -EIO;
2198 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2200 queue_work(cifsiod_wq, &wdata->work);
2201 mutex_lock(&server->srv_mutex);
2202 DeleteMidQEntry(mid);
2203 mutex_unlock(&server->srv_mutex);
2204 add_credits(tcon->ses->server, credits_received, 0);
2207 /* smb2_async_writev - send an async write, and set up mid to handle result */
2209 smb2_async_writev(struct cifs_writedata *wdata,
2210 void (*release)(struct kref *kref))
2212 int rc = -EACCES, flags = 0;
2213 struct smb2_write_req *req = NULL;
2214 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2215 struct TCP_Server_Info *server = tcon->ses->server;
2217 struct smb_rqst rqst;
2219 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
2221 if (rc == -EAGAIN && wdata->credits) {
2222 /* credits was reset by reconnect */
2224 /* reduce in_flight value since we won't send the req */
2225 spin_lock(&server->req_lock);
2226 server->in_flight--;
2227 spin_unlock(&server->req_lock);
2229 goto async_writev_out;
2232 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2234 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2235 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2236 req->WriteChannelInfoOffset = 0;
2237 req->WriteChannelInfoLength = 0;
2239 req->Offset = cpu_to_le64(wdata->offset);
2240 /* 4 for rfc1002 length field */
2241 req->DataOffset = cpu_to_le16(
2242 offsetof(struct smb2_write_req, Buffer) - 4);
2243 req->RemainingBytes = 0;
2245 /* 4 for rfc1002 length field and 1 for Buffer */
2246 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2251 rqst.rq_pages = wdata->pages;
2252 rqst.rq_npages = wdata->nr_pages;
2253 rqst.rq_pagesz = wdata->pagesz;
2254 rqst.rq_tailsz = wdata->tailsz;
2256 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2257 wdata->offset, wdata->bytes);
2259 req->Length = cpu_to_le32(wdata->bytes);
2261 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2263 if (wdata->credits) {
2264 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2265 SMB2_MAX_BUFFER_SIZE));
2266 req->hdr.CreditRequest = req->hdr.CreditCharge;
2267 spin_lock(&server->req_lock);
2268 server->credits += wdata->credits -
2269 le16_to_cpu(req->hdr.CreditCharge);
2270 spin_unlock(&server->req_lock);
2271 wake_up(&server->request_q);
2272 flags = CIFS_HAS_CREDITS;
2275 kref_get(&wdata->refcount);
2276 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2280 kref_put(&wdata->refcount, release);
2281 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2285 cifs_small_buf_release(req);
2290 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2291 * The length field from io_parms must be at least 1 and indicates a number of
2292 * elements with data to write that begins with position 1 in iov array. All
2293 * data length is specified by count.
2296 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2297 unsigned int *nbytes, struct kvec *iov, int n_vec)
2300 struct smb2_write_req *req = NULL;
2301 struct smb2_write_rsp *rsp = NULL;
2308 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2312 if (io_parms->tcon->ses->server == NULL)
2313 return -ECONNABORTED;
2315 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2317 req->PersistentFileId = io_parms->persistent_fid;
2318 req->VolatileFileId = io_parms->volatile_fid;
2319 req->WriteChannelInfoOffset = 0;
2320 req->WriteChannelInfoLength = 0;
2322 req->Length = cpu_to_le32(io_parms->length);
2323 req->Offset = cpu_to_le64(io_parms->offset);
2324 /* 4 for rfc1002 length field */
2325 req->DataOffset = cpu_to_le16(
2326 offsetof(struct smb2_write_req, Buffer) - 4);
2327 req->RemainingBytes = 0;
2329 iov[0].iov_base = (char *)req;
2330 /* 4 for rfc1002 length field and 1 for Buffer */
2331 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2333 /* length of entire message including data to be written */
2334 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2336 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2338 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
2341 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
2342 cifs_dbg(VFS, "Send error in write = %d\n", rc);
2344 *nbytes = le32_to_cpu(rsp->DataLength);
2346 free_rsp_buf(resp_buftype, rsp);
2351 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2354 unsigned int entrycount = 0;
2355 unsigned int next_offset = 0;
2356 FILE_DIRECTORY_INFO *entryptr;
2358 if (bufstart == NULL)
2361 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2364 entryptr = (FILE_DIRECTORY_INFO *)
2365 ((char *)entryptr + next_offset);
2367 if ((char *)entryptr + size > end_of_buf) {
2368 cifs_dbg(VFS, "malformed search entry would overflow\n");
2372 len = le32_to_cpu(entryptr->FileNameLength);
2373 if ((char *)entryptr + len + size > end_of_buf) {
2374 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2379 *lastentry = (char *)entryptr;
2382 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2394 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2395 u64 persistent_fid, u64 volatile_fid, int index,
2396 struct cifs_search_info *srch_inf)
2398 struct smb2_query_directory_req *req;
2399 struct smb2_query_directory_rsp *rsp = NULL;
2403 int resp_buftype = CIFS_NO_BUFFER;
2404 unsigned char *bufptr;
2405 struct TCP_Server_Info *server;
2406 struct cifs_ses *ses = tcon->ses;
2407 __le16 asteriks = cpu_to_le16('*');
2409 unsigned int output_size = CIFSMaxBufSize;
2410 size_t info_buf_size;
2412 if (ses && (ses->server))
2413 server = ses->server;
2417 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2421 switch (srch_inf->info_level) {
2422 case SMB_FIND_FILE_DIRECTORY_INFO:
2423 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2424 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2426 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2427 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2428 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2431 cifs_dbg(VFS, "info level %u isn't supported\n",
2432 srch_inf->info_level);
2437 req->FileIndex = cpu_to_le32(index);
2438 req->PersistentFileId = persistent_fid;
2439 req->VolatileFileId = volatile_fid;
2442 bufptr = req->Buffer;
2443 memcpy(bufptr, &asteriks, len);
2445 req->FileNameOffset =
2446 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2447 req->FileNameLength = cpu_to_le16(len);
2449 * BB could be 30 bytes or so longer if we used SMB2 specific
2450 * buffer lengths, but this is safe and close enough.
2452 output_size = min_t(unsigned int, output_size, server->maxBuf);
2453 output_size = min_t(unsigned int, output_size, 2 << 15);
2454 req->OutputBufferLength = cpu_to_le32(output_size);
2456 iov[0].iov_base = (char *)req;
2457 /* 4 for RFC1001 length and 1 for Buffer */
2458 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2460 iov[1].iov_base = (char *)(req->Buffer);
2461 iov[1].iov_len = len;
2463 inc_rfc1001_len(req, len - 1 /* Buffer */);
2465 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
2466 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2469 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2470 srch_inf->endOfSearch = true;
2473 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2477 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2478 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2483 srch_inf->unicode = true;
2485 if (srch_inf->ntwrk_buf_start) {
2486 if (srch_inf->smallBuf)
2487 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2489 cifs_buf_release(srch_inf->ntwrk_buf_start);
2491 srch_inf->ntwrk_buf_start = (char *)rsp;
2492 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2493 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2494 /* 4 for rfc1002 length field */
2495 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2496 srch_inf->entries_in_buffer =
2497 num_entries(srch_inf->srch_entries_start, end_of_smb,
2498 &srch_inf->last_entry, info_buf_size);
2499 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
2500 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2501 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2502 srch_inf->srch_entries_start, srch_inf->last_entry);
2503 if (resp_buftype == CIFS_LARGE_BUFFER)
2504 srch_inf->smallBuf = false;
2505 else if (resp_buftype == CIFS_SMALL_BUFFER)
2506 srch_inf->smallBuf = true;
2508 cifs_dbg(VFS, "illegal search buffer type\n");
2513 free_rsp_buf(resp_buftype, rsp);
2518 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2519 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
2520 unsigned int num, void **data, unsigned int *size)
2522 struct smb2_set_info_req *req;
2523 struct smb2_set_info_rsp *rsp = NULL;
2528 struct TCP_Server_Info *server;
2529 struct cifs_ses *ses = tcon->ses;
2531 if (ses && (ses->server))
2532 server = ses->server;
2539 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2543 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2549 req->hdr.ProcessId = cpu_to_le32(pid);
2551 req->InfoType = SMB2_O_INFO_FILE;
2552 req->FileInfoClass = info_class;
2553 req->PersistentFileId = persistent_fid;
2554 req->VolatileFileId = volatile_fid;
2556 /* 4 for RFC1001 length and 1 for Buffer */
2558 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2559 req->BufferLength = cpu_to_le32(*size);
2561 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2563 memcpy(req->Buffer, *data, *size);
2565 iov[0].iov_base = (char *)req;
2566 /* 4 for RFC1001 length */
2567 iov[0].iov_len = get_rfc1002_length(req) + 4;
2569 for (i = 1; i < num; i++) {
2570 inc_rfc1001_len(req, size[i]);
2571 le32_add_cpu(&req->BufferLength, size[i]);
2572 iov[i].iov_base = (char *)data[i];
2573 iov[i].iov_len = size[i];
2576 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2577 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2580 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
2582 free_rsp_buf(resp_buftype, rsp);
2588 SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2589 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2591 struct smb2_file_rename_info info;
2593 unsigned int size[2];
2595 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2597 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2601 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2602 /* 0 = fail if target already exists */
2603 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2604 info.FileNameLength = cpu_to_le32(len);
2607 size[0] = sizeof(struct smb2_file_rename_info);
2609 data[1] = target_file;
2610 size[1] = len + 2 /* null */;
2612 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2613 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2620 SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2621 u64 persistent_fid, u64 volatile_fid)
2623 __u8 delete_pending = 1;
2627 data = &delete_pending;
2628 size = 1; /* sizeof __u8 */
2630 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2631 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2636 SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2637 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2639 struct smb2_file_link_info info;
2641 unsigned int size[2];
2643 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2645 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2649 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2650 /* 0 = fail if link already exists */
2651 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2652 info.FileNameLength = cpu_to_le32(len);
2655 size[0] = sizeof(struct smb2_file_link_info);
2657 data[1] = target_file;
2658 size[1] = len + 2 /* null */;
2660 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2661 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
2667 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2668 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
2670 struct smb2_file_eof_info info;
2674 info.EndOfFile = *eof;
2677 size = sizeof(struct smb2_file_eof_info);
2680 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2681 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2683 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2684 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
2688 SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2689 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2692 size = sizeof(FILE_BASIC_INFO);
2693 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2694 current->tgid, FILE_BASIC_INFORMATION, 1,
2695 (void **)&buf, &size);
2699 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2700 const u64 persistent_fid, const u64 volatile_fid,
2704 struct smb2_oplock_break *req = NULL;
2706 cifs_dbg(FYI, "SMB2_oplock_break\n");
2707 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2712 req->VolatileFid = volatile_fid;
2713 req->PersistentFid = persistent_fid;
2714 req->OplockLevel = oplock_level;
2715 req->hdr.CreditRequest = cpu_to_le16(1);
2717 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2718 /* SMB2 buffer freed by function above */
2721 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
2722 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
2729 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2730 struct kstatfs *kst)
2732 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2733 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2734 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2735 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
2736 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2741 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2742 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2745 struct smb2_query_info_req *req;
2747 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
2749 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2752 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2756 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2757 req->FileInfoClass = level;
2758 req->PersistentFileId = persistent_fid;
2759 req->VolatileFileId = volatile_fid;
2760 /* 4 for rfc1002 length field and 1 for pad */
2761 req->InputBufferOffset =
2762 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2763 req->OutputBufferLength = cpu_to_le32(
2764 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2766 iov->iov_base = (char *)req;
2767 /* 4 for rfc1002 length field */
2768 iov->iov_len = get_rfc1002_length(req) + 4;
2773 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2774 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2776 struct smb2_query_info_rsp *rsp = NULL;
2780 struct cifs_ses *ses = tcon->ses;
2781 struct smb2_fs_full_size_info *info = NULL;
2783 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2784 sizeof(struct smb2_fs_full_size_info),
2785 persistent_fid, volatile_fid);
2789 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2791 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2794 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2796 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
2797 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
2798 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2799 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2800 sizeof(struct smb2_fs_full_size_info));
2802 copy_fs_info_to_kstatfs(info, fsdata);
2805 free_rsp_buf(resp_buftype, iov.iov_base);
2810 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
2811 u64 persistent_fid, u64 volatile_fid, int level)
2813 struct smb2_query_info_rsp *rsp = NULL;
2816 int resp_buftype, max_len, min_len;
2817 struct cifs_ses *ses = tcon->ses;
2818 unsigned int rsp_len, offset;
2820 if (level == FS_DEVICE_INFORMATION) {
2821 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2822 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2823 } else if (level == FS_ATTRIBUTE_INFORMATION) {
2824 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
2825 min_len = MIN_FS_ATTR_INFO_SIZE;
2826 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
2827 max_len = sizeof(struct smb3_fs_ss_info);
2828 min_len = sizeof(struct smb3_fs_ss_info);
2830 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
2834 rc = build_qfs_info_req(&iov, tcon, level, max_len,
2835 persistent_fid, volatile_fid);
2839 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2841 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2844 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2846 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
2847 offset = le16_to_cpu(rsp->OutputBufferOffset);
2848 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
2852 if (level == FS_ATTRIBUTE_INFORMATION)
2853 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
2854 + (char *)&rsp->hdr, min_t(unsigned int,
2856 else if (level == FS_DEVICE_INFORMATION)
2857 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
2858 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
2859 else if (level == FS_SECTOR_SIZE_INFORMATION) {
2860 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
2861 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
2862 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
2863 tcon->perf_sector_size =
2864 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
2868 free_rsp_buf(resp_buftype, iov.iov_base);
2873 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2874 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2875 const __u32 num_lock, struct smb2_lock_element *buf)
2878 struct smb2_lock_req *req = NULL;
2883 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
2885 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
2889 req->hdr.ProcessId = cpu_to_le32(pid);
2890 req->LockCount = cpu_to_le16(num_lock);
2892 req->PersistentFileId = persist_fid;
2893 req->VolatileFileId = volatile_fid;
2895 count = num_lock * sizeof(struct smb2_lock_element);
2896 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
2898 iov[0].iov_base = (char *)req;
2899 /* 4 for rfc1002 length field and count for all locks */
2900 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
2901 iov[1].iov_base = (char *)buf;
2902 iov[1].iov_len = count;
2904 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2905 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2907 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
2908 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
2915 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
2916 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2917 const __u64 length, const __u64 offset, const __u32 lock_flags,
2920 struct smb2_lock_element lock;
2922 lock.Offset = cpu_to_le64(offset);
2923 lock.Length = cpu_to_le64(length);
2924 lock.Flags = cpu_to_le32(lock_flags);
2925 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
2926 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
2928 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
2932 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
2933 __u8 *lease_key, const __le32 lease_state)
2936 struct smb2_lease_ack *req = NULL;
2938 cifs_dbg(FYI, "SMB2_lease_break\n");
2939 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2944 req->hdr.CreditRequest = cpu_to_le16(1);
2945 req->StructureSize = cpu_to_le16(36);
2946 inc_rfc1001_len(req, 12);
2948 memcpy(req->LeaseKey, lease_key, 16);
2949 req->LeaseState = lease_state;
2951 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2952 /* SMB2 buffer freed by function above */
2955 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
2956 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);