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[0] = 0xFE;
101 hdr->ProtocolId[1] = 'S';
102 hdr->ProtocolId[2] = 'M';
103 hdr->ProtocolId[3] = 'B';
104 hdr->StructureSize = cpu_to_le16(64);
105 hdr->Command = smb2_cmd;
106 if (tcon && tcon->ses && tcon->ses->server) {
107 struct TCP_Server_Info *server = tcon->ses->server;
109 spin_lock(&server->req_lock);
110 /* Request up to 2 credits but don't go over the limit. */
111 if (server->credits >= SMB2_MAX_CREDITS_AVAILABLE)
112 hdr->CreditRequest = cpu_to_le16(0);
114 hdr->CreditRequest = cpu_to_le16(
115 min_t(int, SMB2_MAX_CREDITS_AVAILABLE -
116 server->credits, 2));
117 spin_unlock(&server->req_lock);
119 hdr->CreditRequest = cpu_to_le16(2);
121 hdr->ProcessId = cpu_to_le32((__u16)current->tgid);
126 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
127 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
128 if ((tcon->ses) && (tcon->ses->server) &&
129 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
130 hdr->CreditCharge = cpu_to_le16(1);
131 /* else CreditCharge MBZ */
133 hdr->TreeId = tcon->tid;
134 /* Uid is not converted */
136 hdr->SessionId = tcon->ses->Suid;
139 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
140 * to pass the path on the Open SMB prefixed by \\server\share.
141 * Not sure when we would need to do the augmented path (if ever) and
142 * setting this flag breaks the SMB2 open operation since it is
143 * illegal to send an empty path name (without \\server\share prefix)
144 * when the DFS flag is set in the SMB open header. We could
145 * consider setting the flag on all operations other than open
146 * but it is safer to net set it for now.
148 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
149 hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
151 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign)
152 hdr->Flags |= SMB2_FLAGS_SIGNED;
154 pdu->StructureSize2 = cpu_to_le16(parmsize);
159 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
162 struct nls_table *nls_codepage;
163 struct cifs_ses *ses;
164 struct TCP_Server_Info *server;
167 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
168 * check for tcp and smb session status done differently
169 * for those three - in the calling routine.
174 if (smb2_command == SMB2_TREE_CONNECT)
177 if (tcon->tidStatus == CifsExiting) {
179 * only tree disconnect, open, and write,
180 * (and ulogoff which does not have tcon)
181 * are allowed as we start force umount.
183 if ((smb2_command != SMB2_WRITE) &&
184 (smb2_command != SMB2_CREATE) &&
185 (smb2_command != SMB2_TREE_DISCONNECT)) {
186 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
191 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
192 (!tcon->ses->server))
196 server = ses->server;
199 * Give demultiplex thread up to 10 seconds to reconnect, should be
200 * greater than cifs socket timeout which is 7 seconds
202 while (server->tcpStatus == CifsNeedReconnect) {
204 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
205 * here since they are implicitly done when session drops.
207 switch (smb2_command) {
209 * BB Should we keep oplock break and add flush to exceptions?
211 case SMB2_TREE_DISCONNECT:
214 case SMB2_OPLOCK_BREAK:
218 wait_event_interruptible_timeout(server->response_q,
219 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
221 /* are we still trying to reconnect? */
222 if (server->tcpStatus != CifsNeedReconnect)
226 * on "soft" mounts we wait once. Hard mounts keep
227 * retrying until process is killed or server comes
231 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
236 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
239 nls_codepage = load_nls_default();
242 * need to prevent multiple threads trying to simultaneously reconnect
243 * the same SMB session
245 mutex_lock(&tcon->ses->session_mutex);
246 rc = cifs_negotiate_protocol(0, tcon->ses);
247 if (!rc && tcon->ses->need_reconnect)
248 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
250 if (rc || !tcon->need_reconnect) {
251 mutex_unlock(&tcon->ses->session_mutex);
255 cifs_mark_open_files_invalid(tcon);
256 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
257 mutex_unlock(&tcon->ses->session_mutex);
258 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
261 atomic_inc(&tconInfoReconnectCount);
264 * Check if handle based operation so we know whether we can continue
265 * or not without returning to caller to reset file handle.
268 * BB Is flush done by server on drop of tcp session? Should we special
269 * case it and skip above?
271 switch (smb2_command) {
277 case SMB2_QUERY_DIRECTORY:
278 case SMB2_CHANGE_NOTIFY:
279 case SMB2_QUERY_INFO:
283 unload_nls(nls_codepage);
288 * Allocate and return pointer to an SMB request hdr, and set basic
289 * SMB information in the SMB header. If the return code is zero, this
290 * function must have filled in request_buf pointer.
293 small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
298 rc = smb2_reconnect(smb2_command, tcon);
302 /* BB eventually switch this to SMB2 specific small buf size */
303 *request_buf = cifs_small_buf_get();
304 if (*request_buf == NULL) {
305 /* BB should we add a retry in here if not a writepage? */
309 smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
312 #ifdef CONFIG_CIFS_STATS2
313 uint16_t com_code = le16_to_cpu(smb2_command);
314 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
316 cifs_stats_inc(&tcon->num_smbs_sent);
322 #ifdef CONFIG_CIFS_SMB311
323 /* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
324 #define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
327 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
328 #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
331 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
333 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
334 pneg_ctxt->DataLength = cpu_to_le16(38);
335 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
336 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
337 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
338 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
342 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
344 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
345 pneg_ctxt->DataLength = cpu_to_le16(6);
346 pneg_ctxt->CipherCount = cpu_to_le16(2);
347 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
348 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
352 assemble_neg_contexts(struct smb2_negotiate_req *req)
355 /* +4 is to account for the RFC1001 len field */
356 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
358 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
359 /* Add 2 to size to round to 8 byte boundary */
360 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
361 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
362 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
363 req->NegotiateContextCount = cpu_to_le16(2);
364 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2
365 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
368 static void assemble_neg_contexts(struct smb2_negotiate_req *req)
377 * SMB2 Worker functions follow:
379 * The general structure of the worker functions is:
380 * 1) Call smb2_init (assembles SMB2 header)
381 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
382 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
383 * 4) Decode SMB2 command specific fields in the fixed length area
384 * 5) Decode variable length data area (if any for this SMB2 command type)
385 * 6) Call free smb buffer
391 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
393 struct smb2_negotiate_req *req;
394 struct smb2_negotiate_rsp *rsp;
398 struct TCP_Server_Info *server = ses->server;
399 int blob_offset, blob_length;
401 int flags = CIFS_NEG_OP;
403 cifs_dbg(FYI, "Negotiate protocol\n");
406 WARN(1, "%s: server is NULL!\n", __func__);
410 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
414 req->hdr.SessionId = 0;
416 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
418 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
419 inc_rfc1001_len(req, 2);
421 /* only one of SMB2 signing flags may be set in SMB2 request */
423 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
424 else if (global_secflags & CIFSSEC_MAY_SIGN)
425 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
427 req->SecurityMode = 0;
429 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
431 /* ClientGUID must be zero for SMB2.02 dialect */
432 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
433 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
435 memcpy(req->ClientGUID, server->client_guid,
436 SMB2_CLIENT_GUID_SIZE);
437 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
438 assemble_neg_contexts(req);
440 iov[0].iov_base = (char *)req;
441 /* 4 for rfc1002 length field */
442 iov[0].iov_len = get_rfc1002_length(req) + 4;
444 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
446 rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
448 * No tcon so can't do
449 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
454 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
456 /* BB we may eventually want to match the negotiated vs. requested
457 dialect, even though we are only requesting one at a time */
458 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
459 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
460 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
461 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
462 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
463 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
464 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
465 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
466 #ifdef CONFIG_CIFS_SMB311
467 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
468 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
471 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
472 le16_to_cpu(rsp->DialectRevision));
476 server->dialect = le16_to_cpu(rsp->DialectRevision);
478 /* SMB2 only has an extended negflavor */
479 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
480 /* set it to the maximum buffer size value we can send with 1 credit */
481 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
482 SMB2_MAX_BUFFER_SIZE);
483 server->max_read = le32_to_cpu(rsp->MaxReadSize);
484 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
485 /* BB Do we need to validate the SecurityMode? */
486 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
487 server->capabilities = le32_to_cpu(rsp->Capabilities);
489 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
491 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
494 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
496 * ses->sectype = RawNTLMSSP;
497 * but for time being this is our only auth choice so doesn't matter.
498 * We just found a server which sets blob length to zero expecting raw.
500 if (blob_length == 0)
501 cifs_dbg(FYI, "missing security blob on negprot\n");
503 rc = cifs_enable_signing(server, ses->sign);
507 rc = decode_negTokenInit(security_blob, blob_length, server);
514 free_rsp_buf(resp_buftype, rsp);
518 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
521 struct validate_negotiate_info_req vneg_inbuf;
522 struct validate_negotiate_info_rsp *pneg_rsp;
525 cifs_dbg(FYI, "validate negotiate\n");
528 * validation ioctl must be signed, so no point sending this if we
529 * can not sign it. We could eventually change this to selectively
530 * sign just this, the first and only signed request on a connection.
531 * This is good enough for now since a user who wants better security
532 * would also enable signing on the mount. Having validation of
533 * negotiate info for signed connections helps reduce attack vectors
535 if (tcon->ses->server->sign == false)
536 return 0; /* validation requires signing */
538 vneg_inbuf.Capabilities =
539 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
540 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
541 SMB2_CLIENT_GUID_SIZE);
544 vneg_inbuf.SecurityMode =
545 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
546 else if (global_secflags & CIFSSEC_MAY_SIGN)
547 vneg_inbuf.SecurityMode =
548 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
550 vneg_inbuf.SecurityMode = 0;
552 vneg_inbuf.DialectCount = cpu_to_le16(1);
553 vneg_inbuf.Dialects[0] =
554 cpu_to_le16(tcon->ses->server->vals->protocol_id);
556 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
557 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
558 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
559 (char **)&pneg_rsp, &rsplen);
562 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
566 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
567 cifs_dbg(VFS, "invalid size of protocol negotiate response\n");
571 /* check validate negotiate info response matches what we got earlier */
572 if (pneg_rsp->Dialect !=
573 cpu_to_le16(tcon->ses->server->vals->protocol_id))
576 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
579 /* do not validate server guid because not saved at negprot time yet */
581 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
582 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
585 /* validate negotiate successful */
586 cifs_dbg(FYI, "validate negotiate info successful\n");
590 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
595 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
596 const struct nls_table *nls_cp)
598 struct smb2_sess_setup_req *req;
599 struct smb2_sess_setup_rsp *rsp = NULL;
602 int resp_buftype = CIFS_NO_BUFFER;
603 __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */
604 struct TCP_Server_Info *server = ses->server;
606 struct key *spnego_key = NULL;
607 char *security_blob = NULL;
608 unsigned char *ntlmssp_blob = NULL;
609 bool use_spnego = false; /* else use raw ntlmssp */
610 u64 previous_session = ses->Suid;
612 cifs_dbg(FYI, "Session Setup\n");
615 WARN(1, "%s: server is NULL!\n", __func__);
620 * If we are here due to reconnect, free per-smb session key
621 * in case signing was required.
623 kfree(ses->auth_key.response);
624 ses->auth_key.response = NULL;
627 * If memory allocation is successful, caller of this function
630 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
633 ses->ntlmssp->sesskey_per_smbsess = true;
635 /* FIXME: allow for other auth types besides NTLMSSP (e.g. krb5) */
636 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
637 ses->sectype = RawNTLMSSP;
639 ssetup_ntlmssp_authenticate:
640 if (phase == NtLmChallenge)
641 phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
643 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
647 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
649 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
650 req->PreviousSessionId = previous_session;
652 req->Flags = 0; /* MBZ */
653 /* to enable echos and oplocks */
654 req->hdr.CreditRequest = cpu_to_le16(3);
656 /* only one of SMB2 signing flags may be set in SMB2 request */
658 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
659 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
660 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
662 req->SecurityMode = 0;
664 req->Capabilities = 0;
665 req->Channel = 0; /* MBZ */
667 iov[0].iov_base = (char *)req;
668 /* 4 for rfc1002 length field and 1 for pad */
669 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
671 if (ses->sectype == Kerberos) {
672 #ifdef CONFIG_CIFS_UPCALL
673 struct cifs_spnego_msg *msg;
675 spnego_key = cifs_get_spnego_key(ses);
676 if (IS_ERR(spnego_key)) {
677 rc = PTR_ERR(spnego_key);
682 msg = spnego_key->payload.data[0];
684 * check version field to make sure that cifs.upcall is
685 * sending us a response in an expected form
687 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
689 "bad cifs.upcall version. Expected %d got %d",
690 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
694 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
696 if (!ses->auth_key.response) {
698 "Kerberos can't allocate (%u bytes) memory",
703 ses->auth_key.len = msg->sesskey_len;
704 blob_length = msg->secblob_len;
705 iov[1].iov_base = msg->data + msg->sesskey_len;
706 iov[1].iov_len = blob_length;
710 #endif /* CONFIG_CIFS_UPCALL */
711 } else if (phase == NtLmNegotiate) { /* if not krb5 must be ntlmssp */
712 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
714 if (ntlmssp_blob == NULL) {
718 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
720 /* blob_length = build_spnego_ntlmssp_blob(
722 sizeof(struct _NEGOTIATE_MESSAGE),
724 /* BB eventually need to add this */
725 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
730 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
731 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
732 security_blob = ntlmssp_blob;
734 iov[1].iov_base = security_blob;
735 iov[1].iov_len = blob_length;
736 } else if (phase == NtLmAuthenticate) {
737 req->hdr.SessionId = ses->Suid;
738 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
741 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n",
743 goto ssetup_exit; /* BB double check error handling */
746 /* blob_length = build_spnego_ntlmssp_blob(
750 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
755 security_blob = ntlmssp_blob;
757 iov[1].iov_base = security_blob;
758 iov[1].iov_len = blob_length;
760 cifs_dbg(VFS, "illegal ntlmssp phase\n");
765 /* Testing shows that buffer offset must be at location of Buffer[0] */
766 req->SecurityBufferOffset =
767 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
768 1 /* pad */ - 4 /* rfc1001 len */);
769 req->SecurityBufferLength = cpu_to_le16(blob_length);
771 inc_rfc1001_len(req, blob_length - 1 /* pad */);
773 /* BB add code to build os and lm fields */
775 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype,
776 CIFS_LOG_ERROR | CIFS_NEG_OP);
778 kfree(security_blob);
779 rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
780 ses->Suid = rsp->hdr.SessionId;
781 if (resp_buftype != CIFS_NO_BUFFER &&
782 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
783 if (phase != NtLmNegotiate) {
784 cifs_dbg(VFS, "Unexpected more processing error\n");
787 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
788 le16_to_cpu(rsp->SecurityBufferOffset)) {
789 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
790 le16_to_cpu(rsp->SecurityBufferOffset));
795 /* NTLMSSP Negotiate sent now processing challenge (response) */
796 phase = NtLmChallenge; /* process ntlmssp challenge */
797 rc = 0; /* MORE_PROCESSING is not an error here but expected */
798 rc = decode_ntlmssp_challenge(rsp->Buffer,
799 le16_to_cpu(rsp->SecurityBufferLength), ses);
803 * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
804 * but at least the raw NTLMSSP case works.
807 * No tcon so can't do
808 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
813 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
814 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
815 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
817 free_rsp_buf(resp_buftype, rsp);
819 /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
820 if ((phase == NtLmChallenge) && (rc == 0))
821 goto ssetup_ntlmssp_authenticate;
824 mutex_lock(&server->srv_mutex);
825 if (server->sign && server->ops->generate_signingkey) {
826 rc = server->ops->generate_signingkey(ses);
827 kfree(ses->auth_key.response);
828 ses->auth_key.response = NULL;
831 "SMB3 session key generation failed\n");
832 mutex_unlock(&server->srv_mutex);
836 if (!server->session_estab) {
837 server->sequence_number = 0x2;
838 server->session_estab = true;
840 mutex_unlock(&server->srv_mutex);
842 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
843 spin_lock(&GlobalMid_Lock);
844 ses->status = CifsGood;
845 ses->need_reconnect = false;
846 spin_unlock(&GlobalMid_Lock);
851 kfree(ses->auth_key.response);
852 ses->auth_key.response = NULL;
855 key_invalidate(spnego_key);
864 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
866 struct smb2_logoff_req *req; /* response is also trivial struct */
868 struct TCP_Server_Info *server;
870 cifs_dbg(FYI, "disconnect session %p\n", ses);
872 if (ses && (ses->server))
873 server = ses->server;
877 /* no need to send SMB logoff if uid already closed due to reconnect */
878 if (ses->need_reconnect)
879 goto smb2_session_already_dead;
881 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
885 /* since no tcon, smb2_init can not do this, so do here */
886 req->hdr.SessionId = ses->Suid;
888 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
890 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
892 * No tcon so can't do
893 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
896 smb2_session_already_dead:
900 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
902 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
905 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
907 /* These are similar values to what Windows uses */
908 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
910 tcon->max_chunks = 256;
911 tcon->max_bytes_chunk = 1048576;
912 tcon->max_bytes_copy = 16777216;
916 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
917 struct cifs_tcon *tcon, const struct nls_table *cp)
919 struct smb2_tree_connect_req *req;
920 struct smb2_tree_connect_rsp *rsp = NULL;
925 struct TCP_Server_Info *server;
926 __le16 *unc_path = NULL;
928 cifs_dbg(FYI, "TCON\n");
930 if ((ses->server) && tree)
931 server = ses->server;
935 if (tcon && tcon->bad_network_name)
938 if ((tcon && tcon->seal) &&
939 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
940 cifs_dbg(VFS, "encryption requested but no server support");
944 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
945 if (unc_path == NULL)
948 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
950 if (unc_path_len < 2) {
955 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
959 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
966 /* since no tcon, smb2_init can not do this, so do here */
967 req->hdr.SessionId = ses->Suid;
968 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
969 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
972 iov[0].iov_base = (char *)req;
973 /* 4 for rfc1002 length field and 1 for pad */
974 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
976 /* Testing shows that buffer offset must be at location of Buffer[0] */
977 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
978 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
979 req->PathLength = cpu_to_le16(unc_path_len - 2);
980 iov[1].iov_base = unc_path;
981 iov[1].iov_len = unc_path_len;
983 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
985 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
986 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
990 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
991 tcon->need_reconnect = true;
993 goto tcon_error_exit;
997 ses->ipc_tid = rsp->hdr.TreeId;
1001 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
1002 cifs_dbg(FYI, "connection to disk share\n");
1003 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
1005 cifs_dbg(FYI, "connection to pipe share\n");
1006 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1008 cifs_dbg(FYI, "connection to printer\n");
1010 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1012 goto tcon_error_exit;
1015 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1016 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1017 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1018 tcon->tidStatus = CifsGood;
1019 tcon->need_reconnect = false;
1020 tcon->tid = rsp->hdr.TreeId;
1021 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1023 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1024 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1025 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1026 init_copy_chunk_defaults(tcon);
1027 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1028 cifs_dbg(VFS, "Encrypted shares not supported");
1029 if (tcon->ses->server->ops->validate_negotiate)
1030 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1032 free_rsp_buf(resp_buftype, rsp);
1037 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
1038 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1040 tcon->bad_network_name = true;
1046 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1048 struct smb2_tree_disconnect_req *req; /* response is trivial */
1050 struct TCP_Server_Info *server;
1051 struct cifs_ses *ses = tcon->ses;
1053 cifs_dbg(FYI, "Tree Disconnect\n");
1055 if (ses && (ses->server))
1056 server = ses->server;
1060 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1063 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1067 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
1069 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1075 static struct create_durable *
1076 create_durable_buf(void)
1078 struct create_durable *buf;
1080 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1084 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1085 (struct create_durable, Data));
1086 buf->ccontext.DataLength = cpu_to_le32(16);
1087 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1088 (struct create_durable, Name));
1089 buf->ccontext.NameLength = cpu_to_le16(4);
1090 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1098 static struct create_durable *
1099 create_reconnect_durable_buf(struct cifs_fid *fid)
1101 struct create_durable *buf;
1103 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1107 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1108 (struct create_durable, Data));
1109 buf->ccontext.DataLength = cpu_to_le32(16);
1110 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1111 (struct create_durable, Name));
1112 buf->ccontext.NameLength = cpu_to_le16(4);
1113 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1114 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1115 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1124 parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1125 unsigned int *epoch)
1128 struct create_context *cc;
1130 unsigned int remaining;
1133 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
1134 remaining = le32_to_cpu(rsp->CreateContextsLength);
1135 cc = (struct create_context *)data_offset;
1136 while (remaining >= sizeof(struct create_context)) {
1137 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1138 if (le16_to_cpu(cc->NameLength) == 4 &&
1139 strncmp(name, "RqLs", 4) == 0)
1140 return server->ops->parse_lease_buf(cc, epoch);
1142 next = le32_to_cpu(cc->Next);
1146 cc = (struct create_context *)((char *)cc + next);
1153 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1154 unsigned int *num_iovec, __u8 *oplock)
1156 struct smb2_create_req *req = iov[0].iov_base;
1157 unsigned int num = *num_iovec;
1159 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
1160 if (iov[num].iov_base == NULL)
1162 iov[num].iov_len = server->vals->create_lease_size;
1163 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1164 if (!req->CreateContextsOffset)
1165 req->CreateContextsOffset = cpu_to_le32(
1166 sizeof(struct smb2_create_req) - 4 +
1167 iov[num - 1].iov_len);
1168 le32_add_cpu(&req->CreateContextsLength,
1169 server->vals->create_lease_size);
1170 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
1171 *num_iovec = num + 1;
1175 static struct create_durable_v2 *
1176 create_durable_v2_buf(struct cifs_fid *pfid)
1178 struct create_durable_v2 *buf;
1180 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1184 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1185 (struct create_durable_v2, dcontext));
1186 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1187 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1188 (struct create_durable_v2, Name));
1189 buf->ccontext.NameLength = cpu_to_le16(4);
1191 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1192 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1193 generate_random_uuid(buf->dcontext.CreateGuid);
1194 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1196 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1204 static struct create_durable_handle_reconnect_v2 *
1205 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1207 struct create_durable_handle_reconnect_v2 *buf;
1209 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1214 buf->ccontext.DataOffset =
1215 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1217 buf->ccontext.DataLength =
1218 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1219 buf->ccontext.NameOffset =
1220 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1222 buf->ccontext.NameLength = cpu_to_le16(4);
1224 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1225 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1226 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1227 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1229 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1238 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1239 struct cifs_open_parms *oparms)
1241 struct smb2_create_req *req = iov[0].iov_base;
1242 unsigned int num = *num_iovec;
1244 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1245 if (iov[num].iov_base == NULL)
1247 iov[num].iov_len = sizeof(struct create_durable_v2);
1248 if (!req->CreateContextsOffset)
1249 req->CreateContextsOffset =
1250 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1252 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1253 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1254 *num_iovec = num + 1;
1259 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1260 struct cifs_open_parms *oparms)
1262 struct smb2_create_req *req = iov[0].iov_base;
1263 unsigned int num = *num_iovec;
1265 /* indicate that we don't need to relock the file */
1266 oparms->reconnect = false;
1268 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1269 if (iov[num].iov_base == NULL)
1271 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1272 if (!req->CreateContextsOffset)
1273 req->CreateContextsOffset =
1274 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1276 le32_add_cpu(&req->CreateContextsLength,
1277 sizeof(struct create_durable_handle_reconnect_v2));
1278 inc_rfc1001_len(&req->hdr,
1279 sizeof(struct create_durable_handle_reconnect_v2));
1280 *num_iovec = num + 1;
1285 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1286 struct cifs_open_parms *oparms, bool use_persistent)
1288 struct smb2_create_req *req = iov[0].iov_base;
1289 unsigned int num = *num_iovec;
1291 if (use_persistent) {
1292 if (oparms->reconnect)
1293 return add_durable_reconnect_v2_context(iov, num_iovec,
1296 return add_durable_v2_context(iov, num_iovec, oparms);
1299 if (oparms->reconnect) {
1300 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1301 /* indicate that we don't need to relock the file */
1302 oparms->reconnect = false;
1304 iov[num].iov_base = create_durable_buf();
1305 if (iov[num].iov_base == NULL)
1307 iov[num].iov_len = sizeof(struct create_durable);
1308 if (!req->CreateContextsOffset)
1309 req->CreateContextsOffset =
1310 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1312 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1313 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1314 *num_iovec = num + 1;
1319 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
1320 __u8 *oplock, struct smb2_file_all_info *buf,
1321 struct smb2_err_rsp **err_buf)
1323 struct smb2_create_req *req;
1324 struct smb2_create_rsp *rsp;
1325 struct TCP_Server_Info *server;
1326 struct cifs_tcon *tcon = oparms->tcon;
1327 struct cifs_ses *ses = tcon->ses;
1331 __le16 *copy_path = NULL;
1334 unsigned int num_iovecs = 2;
1335 __u32 file_attributes = 0;
1336 char *dhc_buf = NULL, *lc_buf = NULL;
1338 cifs_dbg(FYI, "create/open\n");
1340 if (ses && (ses->server))
1341 server = ses->server;
1345 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1349 if (oparms->create_options & CREATE_OPTION_READONLY)
1350 file_attributes |= ATTR_READONLY;
1351 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1352 file_attributes |= ATTR_SYSTEM;
1354 req->ImpersonationLevel = IL_IMPERSONATION;
1355 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
1356 /* File attributes ignored on open (used in create though) */
1357 req->FileAttributes = cpu_to_le32(file_attributes);
1358 req->ShareAccess = FILE_SHARE_ALL_LE;
1359 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1360 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
1361 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1362 /* do not count rfc1001 len field */
1363 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1365 iov[0].iov_base = (char *)req;
1366 /* 4 for rfc1002 length field */
1367 iov[0].iov_len = get_rfc1002_length(req) + 4;
1369 /* MUST set path len (NameLength) to 0 opening root of share */
1370 req->NameLength = cpu_to_le16(uni_path_len - 2);
1371 /* -1 since last byte is buf[0] which is sent below (path) */
1373 if (uni_path_len % 8 != 0) {
1374 copy_size = uni_path_len / 8 * 8;
1375 if (copy_size < uni_path_len)
1378 copy_path = kzalloc(copy_size, GFP_KERNEL);
1381 memcpy((char *)copy_path, (const char *)path,
1383 uni_path_len = copy_size;
1387 iov[1].iov_len = uni_path_len;
1388 iov[1].iov_base = path;
1389 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1390 inc_rfc1001_len(req, uni_path_len - 1);
1392 if (!server->oplocks)
1393 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1395 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1396 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1397 req->RequestedOplockLevel = *oplock;
1399 rc = add_lease_context(server, iov, &num_iovecs, oplock);
1401 cifs_small_buf_release(req);
1405 lc_buf = iov[num_iovecs-1].iov_base;
1408 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1409 /* need to set Next field of lease context if we request it */
1410 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
1411 struct create_context *ccontext =
1412 (struct create_context *)iov[num_iovecs-1].iov_base;
1414 cpu_to_le32(server->vals->create_lease_size);
1417 rc = add_durable_context(iov, &num_iovecs, oparms,
1418 tcon->use_persistent);
1420 cifs_small_buf_release(req);
1425 dhc_buf = iov[num_iovecs-1].iov_base;
1428 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1429 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1432 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
1434 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1439 oparms->fid->persistent_fid = rsp->PersistentFileId;
1440 oparms->fid->volatile_fid = rsp->VolatileFileId;
1443 memcpy(buf, &rsp->CreationTime, 32);
1444 buf->AllocationSize = rsp->AllocationSize;
1445 buf->EndOfFile = rsp->EndofFile;
1446 buf->Attributes = rsp->FileAttributes;
1447 buf->NumberOfLinks = cpu_to_le32(1);
1448 buf->DeletePending = 0;
1451 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
1452 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
1454 *oplock = rsp->OplockLevel;
1459 free_rsp_buf(resp_buftype, rsp);
1464 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1467 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1468 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1469 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1471 struct smb2_ioctl_req *req;
1472 struct smb2_ioctl_rsp *rsp;
1473 struct TCP_Server_Info *server;
1474 struct cifs_ses *ses;
1480 cifs_dbg(FYI, "SMB2 IOCTL\n");
1482 if (out_data != NULL)
1485 /* zero out returned data len, in case of error */
1494 if (ses && (ses->server))
1495 server = ses->server;
1499 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1503 req->CtlCode = cpu_to_le32(opcode);
1504 req->PersistentFileId = persistent_fid;
1505 req->VolatileFileId = volatile_fid;
1508 req->InputCount = cpu_to_le32(indatalen);
1509 /* do not set InputOffset if no input data */
1511 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1512 iov[1].iov_base = in_data;
1513 iov[1].iov_len = indatalen;
1518 req->OutputOffset = 0;
1519 req->OutputCount = 0; /* MBZ */
1522 * Could increase MaxOutputResponse, but that would require more
1523 * than one credit. Windows typically sets this smaller, but for some
1524 * ioctls it may be useful to allow server to send more. No point
1525 * limiting what the server can send as long as fits in one credit
1527 req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1530 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1534 iov[0].iov_base = (char *)req;
1537 * If no input data, the size of ioctl struct in
1538 * protocol spec still includes a 1 byte data buffer,
1539 * but if input data passed to ioctl, we do not
1540 * want to double count this, so we do not send
1541 * the dummy one byte of data in iovec[0] if sending
1542 * input data (in iovec[1]). We also must add 4 bytes
1543 * in first iovec to allow for rfc1002 length field.
1547 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1548 inc_rfc1001_len(req, indatalen - 1);
1550 iov[0].iov_len = get_rfc1002_length(req) + 4;
1553 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1554 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1556 if ((rc != 0) && (rc != -EINVAL)) {
1557 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1559 } else if (rc == -EINVAL) {
1560 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1561 (opcode != FSCTL_SRV_COPYCHUNK)) {
1562 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1567 /* check if caller wants to look at return data or just return rc */
1568 if ((plen == NULL) || (out_data == NULL))
1571 *plen = le32_to_cpu(rsp->OutputCount);
1573 /* We check for obvious errors in the output buffer length and offset */
1575 goto ioctl_exit; /* server returned no data */
1576 else if (*plen > 0xFF00) {
1577 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1583 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1584 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1585 le32_to_cpu(rsp->OutputOffset));
1591 *out_data = kmalloc(*plen, GFP_KERNEL);
1592 if (*out_data == NULL) {
1597 memcpy(*out_data, rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
1600 free_rsp_buf(resp_buftype, rsp);
1605 * Individual callers to ioctl worker function follow
1609 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1610 u64 persistent_fid, u64 volatile_fid)
1613 struct compress_ioctl fsctl_input;
1614 char *ret_data = NULL;
1616 fsctl_input.CompressionState =
1617 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
1619 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1620 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1621 (char *)&fsctl_input /* data input */,
1622 2 /* in data len */, &ret_data /* out data */, NULL);
1624 cifs_dbg(FYI, "set compression rc %d\n", rc);
1630 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1631 u64 persistent_fid, u64 volatile_fid)
1633 struct smb2_close_req *req;
1634 struct smb2_close_rsp *rsp;
1635 struct TCP_Server_Info *server;
1636 struct cifs_ses *ses = tcon->ses;
1641 cifs_dbg(FYI, "Close\n");
1643 if (ses && (ses->server))
1644 server = ses->server;
1648 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1652 req->PersistentFileId = persistent_fid;
1653 req->VolatileFileId = volatile_fid;
1655 iov[0].iov_base = (char *)req;
1656 /* 4 for rfc1002 length field */
1657 iov[0].iov_len = get_rfc1002_length(req) + 4;
1659 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1660 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1663 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
1667 /* BB FIXME - decode close response, update inode for caching */
1670 free_rsp_buf(resp_buftype, rsp);
1675 validate_buf(unsigned int offset, unsigned int buffer_length,
1676 struct smb2_hdr *hdr, unsigned int min_buf_size)
1679 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1680 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1681 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1682 char *end_of_buf = begin_of_buf + buffer_length;
1685 if (buffer_length < min_buf_size) {
1686 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1687 buffer_length, min_buf_size);
1691 /* check if beyond RFC1001 maximum length */
1692 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
1693 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1694 buffer_length, smb_len);
1698 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
1699 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
1707 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1708 * Caller must free buffer.
1711 validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1712 struct smb2_hdr *hdr, unsigned int minbufsize,
1716 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1722 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1726 memcpy(data, begin_of_buf, buffer_length);
1732 query_info(const unsigned int xid, struct cifs_tcon *tcon,
1733 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1734 size_t output_len, size_t min_len, void *data)
1736 struct smb2_query_info_req *req;
1737 struct smb2_query_info_rsp *rsp = NULL;
1741 struct TCP_Server_Info *server;
1742 struct cifs_ses *ses = tcon->ses;
1744 cifs_dbg(FYI, "Query Info\n");
1746 if (ses && (ses->server))
1747 server = ses->server;
1751 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1755 req->InfoType = SMB2_O_INFO_FILE;
1756 req->FileInfoClass = info_class;
1757 req->PersistentFileId = persistent_fid;
1758 req->VolatileFileId = volatile_fid;
1759 /* 4 for rfc1002 length field and 1 for Buffer */
1760 req->InputBufferOffset =
1761 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
1762 req->OutputBufferLength = cpu_to_le32(output_len);
1764 iov[0].iov_base = (char *)req;
1765 /* 4 for rfc1002 length field */
1766 iov[0].iov_len = get_rfc1002_length(req) + 4;
1768 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1769 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1772 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1776 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1777 le32_to_cpu(rsp->OutputBufferLength),
1778 &rsp->hdr, min_len, data);
1781 free_rsp_buf(resp_buftype, rsp);
1786 SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1787 u64 persistent_fid, u64 volatile_fid,
1788 struct smb2_file_all_info *data)
1790 return query_info(xid, tcon, persistent_fid, volatile_fid,
1791 FILE_ALL_INFORMATION,
1792 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
1793 sizeof(struct smb2_file_all_info), data);
1797 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1798 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1800 return query_info(xid, tcon, persistent_fid, volatile_fid,
1801 FILE_INTERNAL_INFORMATION,
1802 sizeof(struct smb2_file_internal_info),
1803 sizeof(struct smb2_file_internal_info), uniqueid);
1807 * This is a no-op for now. We're not really interested in the reply, but
1808 * rather in the fact that the server sent one and that server->lstrp
1811 * FIXME: maybe we should consider checking that the reply matches request?
1814 smb2_echo_callback(struct mid_q_entry *mid)
1816 struct TCP_Server_Info *server = mid->callback_data;
1817 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1818 unsigned int credits_received = 1;
1820 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1821 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1823 mutex_lock(&server->srv_mutex);
1824 DeleteMidQEntry(mid);
1825 mutex_unlock(&server->srv_mutex);
1826 add_credits(server, credits_received, CIFS_ECHO_OP);
1829 void smb2_reconnect_server(struct work_struct *work)
1831 struct TCP_Server_Info *server = container_of(work,
1832 struct TCP_Server_Info, reconnect.work);
1833 struct cifs_ses *ses;
1834 struct cifs_tcon *tcon, *tcon2;
1835 struct list_head tmp_list;
1836 int tcon_exist = false;
1838 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
1839 mutex_lock(&server->reconnect_mutex);
1841 INIT_LIST_HEAD(&tmp_list);
1842 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
1844 spin_lock(&cifs_tcp_ses_lock);
1845 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1846 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
1847 if (tcon->need_reconnect) {
1849 list_add_tail(&tcon->rlist, &tmp_list);
1855 * Get the reference to server struct to be sure that the last call of
1856 * cifs_put_tcon() in the loop below won't release the server pointer.
1859 server->srv_count++;
1861 spin_unlock(&cifs_tcp_ses_lock);
1863 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
1864 smb2_reconnect(SMB2_ECHO, tcon);
1865 list_del_init(&tcon->rlist);
1866 cifs_put_tcon(tcon);
1869 cifs_dbg(FYI, "Reconnecting tcons finished\n");
1870 mutex_unlock(&server->reconnect_mutex);
1872 /* now we can safely release srv struct */
1874 cifs_put_tcp_session(server, 1);
1878 SMB2_echo(struct TCP_Server_Info *server)
1880 struct smb2_echo_req *req;
1883 struct smb_rqst rqst = { .rq_iov = &iov,
1886 cifs_dbg(FYI, "In echo request\n");
1888 if (server->tcpStatus == CifsNeedNegotiate) {
1889 /* No need to send echo on newly established connections */
1890 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
1894 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
1898 req->hdr.CreditRequest = cpu_to_le16(1);
1900 iov.iov_base = (char *)req;
1901 /* 4 for rfc1002 length field */
1902 iov.iov_len = get_rfc1002_length(req) + 4;
1904 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
1907 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
1909 cifs_small_buf_release(req);
1914 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1917 struct smb2_flush_req *req;
1918 struct TCP_Server_Info *server;
1919 struct cifs_ses *ses = tcon->ses;
1924 cifs_dbg(FYI, "Flush\n");
1926 if (ses && (ses->server))
1927 server = ses->server;
1931 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
1935 req->PersistentFileId = persistent_fid;
1936 req->VolatileFileId = volatile_fid;
1938 iov[0].iov_base = (char *)req;
1939 /* 4 for rfc1002 length field */
1940 iov[0].iov_len = get_rfc1002_length(req) + 4;
1942 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1945 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
1947 free_rsp_buf(resp_buftype, iov[0].iov_base);
1952 * To form a chain of read requests, any read requests after the first should
1953 * have the end_of_chain boolean set to true.
1956 smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
1957 unsigned int remaining_bytes, int request_type)
1960 struct smb2_read_req *req = NULL;
1962 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
1965 if (io_parms->tcon->ses->server == NULL)
1966 return -ECONNABORTED;
1968 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
1970 req->PersistentFileId = io_parms->persistent_fid;
1971 req->VolatileFileId = io_parms->volatile_fid;
1972 req->ReadChannelInfoOffset = 0; /* reserved */
1973 req->ReadChannelInfoLength = 0; /* reserved */
1974 req->Channel = 0; /* reserved */
1975 req->MinimumCount = 0;
1976 req->Length = cpu_to_le32(io_parms->length);
1977 req->Offset = cpu_to_le64(io_parms->offset);
1979 if (request_type & CHAINED_REQUEST) {
1980 if (!(request_type & END_OF_CHAIN)) {
1981 /* 4 for rfc1002 length field */
1982 req->hdr.NextCommand =
1983 cpu_to_le32(get_rfc1002_length(req) + 4);
1984 } else /* END_OF_CHAIN */
1985 req->hdr.NextCommand = 0;
1986 if (request_type & RELATED_REQUEST) {
1987 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
1989 * Related requests use info from previous read request
1992 req->hdr.SessionId = 0xFFFFFFFF;
1993 req->hdr.TreeId = 0xFFFFFFFF;
1994 req->PersistentFileId = 0xFFFFFFFF;
1995 req->VolatileFileId = 0xFFFFFFFF;
1998 if (remaining_bytes > io_parms->length)
1999 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2001 req->RemainingBytes = 0;
2003 iov[0].iov_base = (char *)req;
2004 /* 4 for rfc1002 length field */
2005 iov[0].iov_len = get_rfc1002_length(req) + 4;
2010 smb2_readv_callback(struct mid_q_entry *mid)
2012 struct cifs_readdata *rdata = mid->callback_data;
2013 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2014 struct TCP_Server_Info *server = tcon->ses->server;
2015 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
2016 unsigned int credits_received = 1;
2017 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
2019 .rq_pages = rdata->pages,
2020 .rq_npages = rdata->nr_pages,
2021 .rq_pagesz = rdata->pagesz,
2022 .rq_tailsz = rdata->tailsz };
2024 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2025 __func__, mid->mid, mid->mid_state, rdata->result,
2028 switch (mid->mid_state) {
2029 case MID_RESPONSE_RECEIVED:
2030 credits_received = le16_to_cpu(buf->CreditRequest);
2031 /* result already set, check signature */
2035 rc = smb2_verify_signature(&rqst, server);
2037 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2040 /* FIXME: should this be counted toward the initiating task? */
2041 task_io_account_read(rdata->got_bytes);
2042 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2044 case MID_REQUEST_SUBMITTED:
2045 case MID_RETRY_NEEDED:
2046 rdata->result = -EAGAIN;
2047 if (server->sign && rdata->got_bytes)
2048 /* reset bytes number since we can not check a sign */
2049 rdata->got_bytes = 0;
2050 /* FIXME: should this be counted toward the initiating task? */
2051 task_io_account_read(rdata->got_bytes);
2052 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2055 if (rdata->result != -ENODATA)
2056 rdata->result = -EIO;
2060 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2062 queue_work(cifsiod_wq, &rdata->work);
2063 mutex_lock(&server->srv_mutex);
2064 DeleteMidQEntry(mid);
2065 mutex_unlock(&server->srv_mutex);
2066 add_credits(server, credits_received, 0);
2069 /* smb2_async_readv - send an async write, and set up mid to handle result */
2071 smb2_async_readv(struct cifs_readdata *rdata)
2074 struct smb2_hdr *buf;
2075 struct cifs_io_parms io_parms;
2076 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
2078 struct TCP_Server_Info *server;
2080 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2081 __func__, rdata->offset, rdata->bytes);
2083 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2084 io_parms.offset = rdata->offset;
2085 io_parms.length = rdata->bytes;
2086 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2087 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2088 io_parms.pid = rdata->pid;
2090 server = io_parms.tcon->ses->server;
2092 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
2094 if (rc == -EAGAIN && rdata->credits) {
2095 /* credits was reset by reconnect */
2097 /* reduce in_flight value since we won't send the req */
2098 spin_lock(&server->req_lock);
2099 server->in_flight--;
2100 spin_unlock(&server->req_lock);
2105 buf = (struct smb2_hdr *)rdata->iov.iov_base;
2106 /* 4 for rfc1002 length field */
2107 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
2109 if (rdata->credits) {
2110 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2111 SMB2_MAX_BUFFER_SIZE));
2112 buf->CreditRequest = buf->CreditCharge;
2113 spin_lock(&server->req_lock);
2114 server->credits += rdata->credits -
2115 le16_to_cpu(buf->CreditCharge);
2116 spin_unlock(&server->req_lock);
2117 wake_up(&server->request_q);
2118 flags = CIFS_HAS_CREDITS;
2121 kref_get(&rdata->refcount);
2122 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
2123 cifs_readv_receive, smb2_readv_callback,
2126 kref_put(&rdata->refcount, cifs_readdata_release);
2127 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2130 cifs_small_buf_release(buf);
2135 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2136 unsigned int *nbytes, char **buf, int *buf_type)
2138 int resp_buftype, rc = -EACCES;
2139 struct smb2_read_rsp *rsp = NULL;
2143 rc = smb2_new_read_req(iov, io_parms, 0, 0);
2147 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2148 &resp_buftype, CIFS_LOG_ERROR);
2150 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
2152 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
2153 free_rsp_buf(resp_buftype, iov[0].iov_base);
2158 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2159 cifs_dbg(VFS, "Send error in read = %d\n", rc);
2161 *nbytes = le32_to_cpu(rsp->DataLength);
2162 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2163 (*nbytes > io_parms->length)) {
2164 cifs_dbg(FYI, "bad length %d for count %d\n",
2165 *nbytes, io_parms->length);
2172 memcpy(*buf, (char *)rsp->hdr.ProtocolId + rsp->DataOffset,
2174 free_rsp_buf(resp_buftype, iov[0].iov_base);
2175 } else if (resp_buftype != CIFS_NO_BUFFER) {
2176 *buf = iov[0].iov_base;
2177 if (resp_buftype == CIFS_SMALL_BUFFER)
2178 *buf_type = CIFS_SMALL_BUFFER;
2179 else if (resp_buftype == CIFS_LARGE_BUFFER)
2180 *buf_type = CIFS_LARGE_BUFFER;
2186 * Check the mid_state and signature on received buffer (if any), and queue the
2187 * workqueue completion task.
2190 smb2_writev_callback(struct mid_q_entry *mid)
2192 struct cifs_writedata *wdata = mid->callback_data;
2193 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2194 struct TCP_Server_Info *server = tcon->ses->server;
2195 unsigned int written;
2196 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2197 unsigned int credits_received = 1;
2199 switch (mid->mid_state) {
2200 case MID_RESPONSE_RECEIVED:
2201 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
2202 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2203 if (wdata->result != 0)
2206 written = le32_to_cpu(rsp->DataLength);
2208 * Mask off high 16 bits when bytes written as returned
2209 * by the server is greater than bytes requested by the
2210 * client. OS/2 servers are known to set incorrect
2213 if (written > wdata->bytes)
2216 if (written < wdata->bytes)
2217 wdata->result = -ENOSPC;
2219 wdata->bytes = written;
2221 case MID_REQUEST_SUBMITTED:
2222 case MID_RETRY_NEEDED:
2223 wdata->result = -EAGAIN;
2226 wdata->result = -EIO;
2231 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2233 queue_work(cifsiod_wq, &wdata->work);
2234 mutex_lock(&server->srv_mutex);
2235 DeleteMidQEntry(mid);
2236 mutex_unlock(&server->srv_mutex);
2237 add_credits(tcon->ses->server, credits_received, 0);
2240 /* smb2_async_writev - send an async write, and set up mid to handle result */
2242 smb2_async_writev(struct cifs_writedata *wdata,
2243 void (*release)(struct kref *kref))
2245 int rc = -EACCES, flags = 0;
2246 struct smb2_write_req *req = NULL;
2247 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2248 struct TCP_Server_Info *server = tcon->ses->server;
2250 struct smb_rqst rqst;
2252 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
2254 if (rc == -EAGAIN && wdata->credits) {
2255 /* credits was reset by reconnect */
2257 /* reduce in_flight value since we won't send the req */
2258 spin_lock(&server->req_lock);
2259 server->in_flight--;
2260 spin_unlock(&server->req_lock);
2262 goto async_writev_out;
2265 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2267 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2268 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2269 req->WriteChannelInfoOffset = 0;
2270 req->WriteChannelInfoLength = 0;
2272 req->Offset = cpu_to_le64(wdata->offset);
2273 /* 4 for rfc1002 length field */
2274 req->DataOffset = cpu_to_le16(
2275 offsetof(struct smb2_write_req, Buffer) - 4);
2276 req->RemainingBytes = 0;
2278 /* 4 for rfc1002 length field and 1 for Buffer */
2279 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2284 rqst.rq_pages = wdata->pages;
2285 rqst.rq_npages = wdata->nr_pages;
2286 rqst.rq_pagesz = wdata->pagesz;
2287 rqst.rq_tailsz = wdata->tailsz;
2289 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2290 wdata->offset, wdata->bytes);
2292 req->Length = cpu_to_le32(wdata->bytes);
2294 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2296 if (wdata->credits) {
2297 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2298 SMB2_MAX_BUFFER_SIZE));
2299 req->hdr.CreditRequest = req->hdr.CreditCharge;
2300 spin_lock(&server->req_lock);
2301 server->credits += wdata->credits -
2302 le16_to_cpu(req->hdr.CreditCharge);
2303 spin_unlock(&server->req_lock);
2304 wake_up(&server->request_q);
2305 flags = CIFS_HAS_CREDITS;
2308 kref_get(&wdata->refcount);
2309 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2313 kref_put(&wdata->refcount, release);
2314 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2318 cifs_small_buf_release(req);
2323 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2324 * The length field from io_parms must be at least 1 and indicates a number of
2325 * elements with data to write that begins with position 1 in iov array. All
2326 * data length is specified by count.
2329 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2330 unsigned int *nbytes, struct kvec *iov, int n_vec)
2333 struct smb2_write_req *req = NULL;
2334 struct smb2_write_rsp *rsp = NULL;
2341 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2345 if (io_parms->tcon->ses->server == NULL)
2346 return -ECONNABORTED;
2348 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2350 req->PersistentFileId = io_parms->persistent_fid;
2351 req->VolatileFileId = io_parms->volatile_fid;
2352 req->WriteChannelInfoOffset = 0;
2353 req->WriteChannelInfoLength = 0;
2355 req->Length = cpu_to_le32(io_parms->length);
2356 req->Offset = cpu_to_le64(io_parms->offset);
2357 /* 4 for rfc1002 length field */
2358 req->DataOffset = cpu_to_le16(
2359 offsetof(struct smb2_write_req, Buffer) - 4);
2360 req->RemainingBytes = 0;
2362 iov[0].iov_base = (char *)req;
2363 /* 4 for rfc1002 length field and 1 for Buffer */
2364 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2366 /* length of entire message including data to be written */
2367 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2369 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2371 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
2374 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
2375 cifs_dbg(VFS, "Send error in write = %d\n", rc);
2377 *nbytes = le32_to_cpu(rsp->DataLength);
2379 free_rsp_buf(resp_buftype, rsp);
2384 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2387 unsigned int entrycount = 0;
2388 unsigned int next_offset = 0;
2389 FILE_DIRECTORY_INFO *entryptr;
2391 if (bufstart == NULL)
2394 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2397 entryptr = (FILE_DIRECTORY_INFO *)
2398 ((char *)entryptr + next_offset);
2400 if ((char *)entryptr + size > end_of_buf) {
2401 cifs_dbg(VFS, "malformed search entry would overflow\n");
2405 len = le32_to_cpu(entryptr->FileNameLength);
2406 if ((char *)entryptr + len + size > end_of_buf) {
2407 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2412 *lastentry = (char *)entryptr;
2415 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2427 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2428 u64 persistent_fid, u64 volatile_fid, int index,
2429 struct cifs_search_info *srch_inf)
2431 struct smb2_query_directory_req *req;
2432 struct smb2_query_directory_rsp *rsp = NULL;
2436 int resp_buftype = CIFS_NO_BUFFER;
2437 unsigned char *bufptr;
2438 struct TCP_Server_Info *server;
2439 struct cifs_ses *ses = tcon->ses;
2440 __le16 asteriks = cpu_to_le16('*');
2442 unsigned int output_size = CIFSMaxBufSize;
2443 size_t info_buf_size;
2445 if (ses && (ses->server))
2446 server = ses->server;
2450 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2454 switch (srch_inf->info_level) {
2455 case SMB_FIND_FILE_DIRECTORY_INFO:
2456 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2457 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2459 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2460 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2461 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2464 cifs_dbg(VFS, "info level %u isn't supported\n",
2465 srch_inf->info_level);
2470 req->FileIndex = cpu_to_le32(index);
2471 req->PersistentFileId = persistent_fid;
2472 req->VolatileFileId = volatile_fid;
2475 bufptr = req->Buffer;
2476 memcpy(bufptr, &asteriks, len);
2478 req->FileNameOffset =
2479 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2480 req->FileNameLength = cpu_to_le16(len);
2482 * BB could be 30 bytes or so longer if we used SMB2 specific
2483 * buffer lengths, but this is safe and close enough.
2485 output_size = min_t(unsigned int, output_size, server->maxBuf);
2486 output_size = min_t(unsigned int, output_size, 2 << 15);
2487 req->OutputBufferLength = cpu_to_le32(output_size);
2489 iov[0].iov_base = (char *)req;
2490 /* 4 for RFC1001 length and 1 for Buffer */
2491 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2493 iov[1].iov_base = (char *)(req->Buffer);
2494 iov[1].iov_len = len;
2496 inc_rfc1001_len(req, len - 1 /* Buffer */);
2498 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
2499 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2502 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2503 srch_inf->endOfSearch = true;
2506 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2510 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2511 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2516 srch_inf->unicode = true;
2518 if (srch_inf->ntwrk_buf_start) {
2519 if (srch_inf->smallBuf)
2520 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2522 cifs_buf_release(srch_inf->ntwrk_buf_start);
2524 srch_inf->ntwrk_buf_start = (char *)rsp;
2525 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2526 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2527 /* 4 for rfc1002 length field */
2528 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2529 srch_inf->entries_in_buffer =
2530 num_entries(srch_inf->srch_entries_start, end_of_smb,
2531 &srch_inf->last_entry, info_buf_size);
2532 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
2533 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2534 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2535 srch_inf->srch_entries_start, srch_inf->last_entry);
2536 if (resp_buftype == CIFS_LARGE_BUFFER)
2537 srch_inf->smallBuf = false;
2538 else if (resp_buftype == CIFS_SMALL_BUFFER)
2539 srch_inf->smallBuf = true;
2541 cifs_dbg(VFS, "illegal search buffer type\n");
2546 free_rsp_buf(resp_buftype, rsp);
2551 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2552 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
2553 unsigned int num, void **data, unsigned int *size)
2555 struct smb2_set_info_req *req;
2556 struct smb2_set_info_rsp *rsp = NULL;
2561 struct TCP_Server_Info *server;
2562 struct cifs_ses *ses = tcon->ses;
2564 if (ses && (ses->server))
2565 server = ses->server;
2572 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2576 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2582 req->hdr.ProcessId = cpu_to_le32(pid);
2584 req->InfoType = SMB2_O_INFO_FILE;
2585 req->FileInfoClass = info_class;
2586 req->PersistentFileId = persistent_fid;
2587 req->VolatileFileId = volatile_fid;
2589 /* 4 for RFC1001 length and 1 for Buffer */
2591 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2592 req->BufferLength = cpu_to_le32(*size);
2594 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2596 memcpy(req->Buffer, *data, *size);
2598 iov[0].iov_base = (char *)req;
2599 /* 4 for RFC1001 length */
2600 iov[0].iov_len = get_rfc1002_length(req) + 4;
2602 for (i = 1; i < num; i++) {
2603 inc_rfc1001_len(req, size[i]);
2604 le32_add_cpu(&req->BufferLength, size[i]);
2605 iov[i].iov_base = (char *)data[i];
2606 iov[i].iov_len = size[i];
2609 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2610 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2613 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
2615 free_rsp_buf(resp_buftype, rsp);
2621 SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2622 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2624 struct smb2_file_rename_info info;
2626 unsigned int size[2];
2628 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2630 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2634 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2635 /* 0 = fail if target already exists */
2636 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2637 info.FileNameLength = cpu_to_le32(len);
2640 size[0] = sizeof(struct smb2_file_rename_info);
2642 data[1] = target_file;
2643 size[1] = len + 2 /* null */;
2645 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2646 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2653 SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2654 u64 persistent_fid, u64 volatile_fid)
2656 __u8 delete_pending = 1;
2660 data = &delete_pending;
2661 size = 1; /* sizeof __u8 */
2663 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2664 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2669 SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2670 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2672 struct smb2_file_link_info info;
2674 unsigned int size[2];
2676 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2678 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2682 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2683 /* 0 = fail if link already exists */
2684 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2685 info.FileNameLength = cpu_to_le32(len);
2688 size[0] = sizeof(struct smb2_file_link_info);
2690 data[1] = target_file;
2691 size[1] = len + 2 /* null */;
2693 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2694 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
2700 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2701 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
2703 struct smb2_file_eof_info info;
2707 info.EndOfFile = *eof;
2710 size = sizeof(struct smb2_file_eof_info);
2713 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2714 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2716 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2717 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
2721 SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2722 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2725 size = sizeof(FILE_BASIC_INFO);
2726 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2727 current->tgid, FILE_BASIC_INFORMATION, 1,
2728 (void **)&buf, &size);
2732 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2733 const u64 persistent_fid, const u64 volatile_fid,
2737 struct smb2_oplock_break *req = NULL;
2739 cifs_dbg(FYI, "SMB2_oplock_break\n");
2740 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2745 req->VolatileFid = volatile_fid;
2746 req->PersistentFid = persistent_fid;
2747 req->OplockLevel = oplock_level;
2748 req->hdr.CreditRequest = cpu_to_le16(1);
2750 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2751 /* SMB2 buffer freed by function above */
2754 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
2755 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
2762 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2763 struct kstatfs *kst)
2765 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2766 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2767 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2768 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
2769 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2774 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2775 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2778 struct smb2_query_info_req *req;
2780 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
2782 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2785 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2789 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2790 req->FileInfoClass = level;
2791 req->PersistentFileId = persistent_fid;
2792 req->VolatileFileId = volatile_fid;
2793 /* 4 for rfc1002 length field and 1 for pad */
2794 req->InputBufferOffset =
2795 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2796 req->OutputBufferLength = cpu_to_le32(
2797 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2799 iov->iov_base = (char *)req;
2800 /* 4 for rfc1002 length field */
2801 iov->iov_len = get_rfc1002_length(req) + 4;
2806 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2807 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2809 struct smb2_query_info_rsp *rsp = NULL;
2813 struct cifs_ses *ses = tcon->ses;
2814 struct smb2_fs_full_size_info *info = NULL;
2816 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2817 sizeof(struct smb2_fs_full_size_info),
2818 persistent_fid, volatile_fid);
2822 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2824 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2827 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2829 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
2830 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
2831 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2832 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2833 sizeof(struct smb2_fs_full_size_info));
2835 copy_fs_info_to_kstatfs(info, fsdata);
2838 free_rsp_buf(resp_buftype, iov.iov_base);
2843 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
2844 u64 persistent_fid, u64 volatile_fid, int level)
2846 struct smb2_query_info_rsp *rsp = NULL;
2849 int resp_buftype, max_len, min_len;
2850 struct cifs_ses *ses = tcon->ses;
2851 unsigned int rsp_len, offset;
2853 if (level == FS_DEVICE_INFORMATION) {
2854 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2855 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2856 } else if (level == FS_ATTRIBUTE_INFORMATION) {
2857 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
2858 min_len = MIN_FS_ATTR_INFO_SIZE;
2859 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
2860 max_len = sizeof(struct smb3_fs_ss_info);
2861 min_len = sizeof(struct smb3_fs_ss_info);
2863 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
2867 rc = build_qfs_info_req(&iov, tcon, level, max_len,
2868 persistent_fid, volatile_fid);
2872 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2874 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2877 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2879 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
2880 offset = le16_to_cpu(rsp->OutputBufferOffset);
2881 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
2885 if (level == FS_ATTRIBUTE_INFORMATION)
2886 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
2887 + (char *)&rsp->hdr, min_t(unsigned int,
2889 else if (level == FS_DEVICE_INFORMATION)
2890 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
2891 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
2892 else if (level == FS_SECTOR_SIZE_INFORMATION) {
2893 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
2894 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
2895 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
2896 tcon->perf_sector_size =
2897 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
2901 free_rsp_buf(resp_buftype, iov.iov_base);
2906 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2907 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2908 const __u32 num_lock, struct smb2_lock_element *buf)
2911 struct smb2_lock_req *req = NULL;
2916 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
2918 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
2922 req->hdr.ProcessId = cpu_to_le32(pid);
2923 req->LockCount = cpu_to_le16(num_lock);
2925 req->PersistentFileId = persist_fid;
2926 req->VolatileFileId = volatile_fid;
2928 count = num_lock * sizeof(struct smb2_lock_element);
2929 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
2931 iov[0].iov_base = (char *)req;
2932 /* 4 for rfc1002 length field and count for all locks */
2933 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
2934 iov[1].iov_base = (char *)buf;
2935 iov[1].iov_len = count;
2937 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2938 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2940 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
2941 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
2948 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
2949 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2950 const __u64 length, const __u64 offset, const __u32 lock_flags,
2953 struct smb2_lock_element lock;
2955 lock.Offset = cpu_to_le64(offset);
2956 lock.Length = cpu_to_le64(length);
2957 lock.Flags = cpu_to_le32(lock_flags);
2958 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
2959 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
2961 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
2965 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
2966 __u8 *lease_key, const __le32 lease_state)
2969 struct smb2_lease_ack *req = NULL;
2971 cifs_dbg(FYI, "SMB2_lease_break\n");
2972 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2977 req->hdr.CreditRequest = cpu_to_le16(1);
2978 req->StructureSize = cpu_to_le16(36);
2979 inc_rfc1001_len(req, 12);
2981 memcpy(req->LeaseKey, lease_key, 16);
2982 req->LeaseState = lease_state;
2984 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2985 /* SMB2 buffer freed by function above */
2988 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
2989 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);