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 >= server->max_credits)
109 hdr->CreditRequest = cpu_to_le16(0);
111 hdr->CreditRequest = cpu_to_le16(
112 min_t(int, server->max_credits -
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);
254 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
255 mutex_unlock(&tcon->ses->session_mutex);
257 if (tcon->use_persistent)
258 cifs_reopen_persistent_handles(tcon);
260 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
263 atomic_inc(&tconInfoReconnectCount);
266 * Check if handle based operation so we know whether we can continue
267 * or not without returning to caller to reset file handle.
270 * BB Is flush done by server on drop of tcp session? Should we special
271 * case it and skip above?
273 switch (smb2_command) {
279 case SMB2_QUERY_DIRECTORY:
280 case SMB2_CHANGE_NOTIFY:
281 case SMB2_QUERY_INFO:
285 unload_nls(nls_codepage);
290 * Allocate and return pointer to an SMB request hdr, and set basic
291 * SMB information in the SMB header. If the return code is zero, this
292 * function must have filled in request_buf pointer.
295 small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
300 rc = smb2_reconnect(smb2_command, tcon);
304 /* BB eventually switch this to SMB2 specific small buf size */
305 *request_buf = cifs_small_buf_get();
306 if (*request_buf == NULL) {
307 /* BB should we add a retry in here if not a writepage? */
311 smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
314 #ifdef CONFIG_CIFS_STATS2
315 uint16_t com_code = le16_to_cpu(smb2_command);
316 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
318 cifs_stats_inc(&tcon->num_smbs_sent);
324 #ifdef CONFIG_CIFS_SMB311
325 /* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
326 #define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
329 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
330 #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
333 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
335 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
336 pneg_ctxt->DataLength = cpu_to_le16(38);
337 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
338 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
339 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
340 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
344 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
346 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
347 pneg_ctxt->DataLength = cpu_to_le16(6);
348 pneg_ctxt->CipherCount = cpu_to_le16(2);
349 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
350 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
354 assemble_neg_contexts(struct smb2_negotiate_req *req)
357 /* +4 is to account for the RFC1001 len field */
358 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
360 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
361 /* Add 2 to size to round to 8 byte boundary */
362 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
363 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
364 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
365 req->NegotiateContextCount = cpu_to_le16(2);
366 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2
367 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
370 static void assemble_neg_contexts(struct smb2_negotiate_req *req)
379 * SMB2 Worker functions follow:
381 * The general structure of the worker functions is:
382 * 1) Call smb2_init (assembles SMB2 header)
383 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
384 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
385 * 4) Decode SMB2 command specific fields in the fixed length area
386 * 5) Decode variable length data area (if any for this SMB2 command type)
387 * 6) Call free smb buffer
393 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
395 struct smb2_negotiate_req *req;
396 struct smb2_negotiate_rsp *rsp;
400 struct TCP_Server_Info *server = ses->server;
401 int blob_offset, blob_length;
403 int flags = CIFS_NEG_OP;
405 cifs_dbg(FYI, "Negotiate protocol\n");
408 WARN(1, "%s: server is NULL!\n", __func__);
412 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
416 req->hdr.SessionId = 0;
418 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
420 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
421 inc_rfc1001_len(req, 2);
423 /* only one of SMB2 signing flags may be set in SMB2 request */
425 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
426 else if (global_secflags & CIFSSEC_MAY_SIGN)
427 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
429 req->SecurityMode = 0;
431 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
433 /* ClientGUID must be zero for SMB2.02 dialect */
434 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
435 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
437 memcpy(req->ClientGUID, server->client_guid,
438 SMB2_CLIENT_GUID_SIZE);
439 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
440 assemble_neg_contexts(req);
442 iov[0].iov_base = (char *)req;
443 /* 4 for rfc1002 length field */
444 iov[0].iov_len = get_rfc1002_length(req) + 4;
446 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
448 rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
450 * No tcon so can't do
451 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
456 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
458 /* BB we may eventually want to match the negotiated vs. requested
459 dialect, even though we are only requesting one at a time */
460 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
461 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
462 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
463 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
464 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
465 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
466 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
467 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
468 #ifdef CONFIG_CIFS_SMB311
469 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
470 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
473 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
474 le16_to_cpu(rsp->DialectRevision));
478 server->dialect = le16_to_cpu(rsp->DialectRevision);
480 /* SMB2 only has an extended negflavor */
481 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
482 /* set it to the maximum buffer size value we can send with 1 credit */
483 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
484 SMB2_MAX_BUFFER_SIZE);
485 server->max_read = le32_to_cpu(rsp->MaxReadSize);
486 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
487 /* BB Do we need to validate the SecurityMode? */
488 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
489 server->capabilities = le32_to_cpu(rsp->Capabilities);
491 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
493 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
496 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
498 * ses->sectype = RawNTLMSSP;
499 * but for time being this is our only auth choice so doesn't matter.
500 * We just found a server which sets blob length to zero expecting raw.
502 if (blob_length == 0)
503 cifs_dbg(FYI, "missing security blob on negprot\n");
505 rc = cifs_enable_signing(server, ses->sign);
509 rc = decode_negTokenInit(security_blob, blob_length, server);
516 free_rsp_buf(resp_buftype, rsp);
520 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
523 struct validate_negotiate_info_req vneg_inbuf;
524 struct validate_negotiate_info_rsp *pneg_rsp;
527 cifs_dbg(FYI, "validate negotiate\n");
530 * validation ioctl must be signed, so no point sending this if we
531 * can not sign it. We could eventually change this to selectively
532 * sign just this, the first and only signed request on a connection.
533 * This is good enough for now since a user who wants better security
534 * would also enable signing on the mount. Having validation of
535 * negotiate info for signed connections helps reduce attack vectors
537 if (tcon->ses->server->sign == false)
538 return 0; /* validation requires signing */
540 vneg_inbuf.Capabilities =
541 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
542 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
543 SMB2_CLIENT_GUID_SIZE);
546 vneg_inbuf.SecurityMode =
547 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
548 else if (global_secflags & CIFSSEC_MAY_SIGN)
549 vneg_inbuf.SecurityMode =
550 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
552 vneg_inbuf.SecurityMode = 0;
554 vneg_inbuf.DialectCount = cpu_to_le16(1);
555 vneg_inbuf.Dialects[0] =
556 cpu_to_le16(tcon->ses->server->vals->protocol_id);
558 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
559 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
560 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
561 (char **)&pneg_rsp, &rsplen);
564 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
568 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
569 cifs_dbg(VFS, "invalid size of protocol negotiate response\n");
573 /* check validate negotiate info response matches what we got earlier */
574 if (pneg_rsp->Dialect !=
575 cpu_to_le16(tcon->ses->server->vals->protocol_id))
578 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
581 /* do not validate server guid because not saved at negprot time yet */
583 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
584 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
587 /* validate negotiate successful */
588 cifs_dbg(FYI, "validate negotiate info successful\n");
592 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
596 struct SMB2_sess_data {
598 struct cifs_ses *ses;
599 struct nls_table *nls_cp;
600 void (*func)(struct SMB2_sess_data *);
602 u64 previous_session;
604 /* we will send the SMB in three pieces:
605 * a fixed length beginning part, an optional
606 * SPNEGO blob (which can be zero length), and a
607 * last part which will include the strings
608 * and rest of bcc area. This allows us to avoid
609 * a large buffer 17K allocation
616 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
619 struct cifs_ses *ses = sess_data->ses;
620 struct smb2_sess_setup_req *req;
621 struct TCP_Server_Info *server = ses->server;
623 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
627 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
629 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
630 req->PreviousSessionId = sess_data->previous_session;
632 req->Flags = 0; /* MBZ */
633 /* to enable echos and oplocks */
634 req->hdr.CreditRequest = cpu_to_le16(3);
636 /* only one of SMB2 signing flags may be set in SMB2 request */
638 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
639 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
640 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
642 req->SecurityMode = 0;
644 req->Capabilities = 0;
645 req->Channel = 0; /* MBZ */
647 sess_data->iov[0].iov_base = (char *)req;
648 /* 4 for rfc1002 length field and 1 for pad */
649 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
651 * This variable will be used to clear the buffer
652 * allocated above in case of any error in the calling function.
654 sess_data->buf0_type = CIFS_SMALL_BUFFER;
660 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
662 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
663 sess_data->buf0_type = CIFS_NO_BUFFER;
667 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
670 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
672 /* Testing shows that buffer offset must be at location of Buffer[0] */
673 req->SecurityBufferOffset =
674 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
675 1 /* pad */ - 4 /* rfc1001 len */);
676 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
678 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
680 /* BB add code to build os and lm fields */
682 rc = SendReceive2(sess_data->xid, sess_data->ses,
684 &sess_data->buf0_type,
685 CIFS_LOG_ERROR | CIFS_NEG_OP);
691 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
694 struct cifs_ses *ses = sess_data->ses;
696 mutex_lock(&ses->server->srv_mutex);
697 if (ses->server->sign && ses->server->ops->generate_signingkey) {
698 rc = ses->server->ops->generate_signingkey(ses);
699 kfree(ses->auth_key.response);
700 ses->auth_key.response = NULL;
703 "SMB3 session key generation failed\n");
704 mutex_unlock(&ses->server->srv_mutex);
708 if (!ses->server->session_estab) {
709 ses->server->sequence_number = 0x2;
710 ses->server->session_estab = true;
712 mutex_unlock(&ses->server->srv_mutex);
714 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
715 spin_lock(&GlobalMid_Lock);
716 ses->status = CifsGood;
717 ses->need_reconnect = false;
718 spin_unlock(&GlobalMid_Lock);
721 if (!ses->server->sign) {
722 kfree(ses->auth_key.response);
723 ses->auth_key.response = NULL;
728 #ifdef CONFIG_CIFS_UPCALL
730 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
733 struct cifs_ses *ses = sess_data->ses;
734 struct cifs_spnego_msg *msg;
735 struct key *spnego_key = NULL;
736 struct smb2_sess_setup_rsp *rsp = NULL;
738 rc = SMB2_sess_alloc_buffer(sess_data);
742 spnego_key = cifs_get_spnego_key(ses);
743 if (IS_ERR(spnego_key)) {
744 rc = PTR_ERR(spnego_key);
749 msg = spnego_key->payload.data[0];
751 * check version field to make sure that cifs.upcall is
752 * sending us a response in an expected form
754 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
756 "bad cifs.upcall version. Expected %d got %d",
757 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
759 goto out_put_spnego_key;
762 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
764 if (!ses->auth_key.response) {
766 "Kerberos can't allocate (%u bytes) memory",
769 goto out_put_spnego_key;
771 ses->auth_key.len = msg->sesskey_len;
773 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
774 sess_data->iov[1].iov_len = msg->secblob_len;
776 rc = SMB2_sess_sendreceive(sess_data);
778 goto out_put_spnego_key;
780 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
781 ses->Suid = rsp->hdr.SessionId;
783 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
784 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
785 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
787 rc = SMB2_sess_establish_session(sess_data);
789 key_invalidate(spnego_key);
792 sess_data->result = rc;
793 sess_data->func = NULL;
794 SMB2_sess_free_buffer(sess_data);
798 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
800 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
801 sess_data->result = -EOPNOTSUPP;
802 sess_data->func = NULL;
807 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
810 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
813 struct cifs_ses *ses = sess_data->ses;
814 struct smb2_sess_setup_rsp *rsp = NULL;
815 char *ntlmssp_blob = NULL;
816 bool use_spnego = false; /* else use raw ntlmssp */
820 * If memory allocation is successful, caller of this function
823 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
828 ses->ntlmssp->sesskey_per_smbsess = true;
830 rc = SMB2_sess_alloc_buffer(sess_data);
834 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
836 if (ntlmssp_blob == NULL) {
841 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
843 /* BB eventually need to add this */
844 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
848 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
849 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
851 sess_data->iov[1].iov_base = ntlmssp_blob;
852 sess_data->iov[1].iov_len = blob_length;
854 rc = SMB2_sess_sendreceive(sess_data);
855 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
857 /* If true, rc here is expected and not an error */
858 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
859 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
865 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
866 le16_to_cpu(rsp->SecurityBufferOffset)) {
867 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
868 le16_to_cpu(rsp->SecurityBufferOffset));
872 rc = decode_ntlmssp_challenge(rsp->Buffer,
873 le16_to_cpu(rsp->SecurityBufferLength), ses);
877 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
880 ses->Suid = rsp->hdr.SessionId;
881 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
882 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
883 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
887 SMB2_sess_free_buffer(sess_data);
889 sess_data->result = 0;
890 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
896 sess_data->result = rc;
897 sess_data->func = NULL;
901 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
904 struct cifs_ses *ses = sess_data->ses;
905 struct smb2_sess_setup_req *req;
906 struct smb2_sess_setup_rsp *rsp = NULL;
907 unsigned char *ntlmssp_blob = NULL;
908 bool use_spnego = false; /* else use raw ntlmssp */
911 rc = SMB2_sess_alloc_buffer(sess_data);
915 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
916 req->hdr.SessionId = ses->Suid;
918 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
921 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
926 /* BB eventually need to add this */
927 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
931 sess_data->iov[1].iov_base = ntlmssp_blob;
932 sess_data->iov[1].iov_len = blob_length;
934 rc = SMB2_sess_sendreceive(sess_data);
938 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
940 ses->Suid = rsp->hdr.SessionId;
941 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
942 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
943 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
945 rc = SMB2_sess_establish_session(sess_data);
948 SMB2_sess_free_buffer(sess_data);
951 sess_data->result = rc;
952 sess_data->func = NULL;
956 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
958 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
959 ses->sectype = RawNTLMSSP;
961 switch (ses->sectype) {
963 sess_data->func = SMB2_auth_kerberos;
966 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
969 cifs_dbg(VFS, "secType %d not supported!\n", ses->sectype);
977 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
978 const struct nls_table *nls_cp)
981 struct TCP_Server_Info *server = ses->server;
982 struct SMB2_sess_data *sess_data;
984 cifs_dbg(FYI, "Session Setup\n");
987 WARN(1, "%s: server is NULL!\n", __func__);
991 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
995 rc = SMB2_select_sec(ses, sess_data);
998 sess_data->xid = xid;
999 sess_data->ses = ses;
1000 sess_data->buf0_type = CIFS_NO_BUFFER;
1001 sess_data->nls_cp = (struct nls_table *) nls_cp;
1003 while (sess_data->func)
1004 sess_data->func(sess_data);
1006 rc = sess_data->result;
1013 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1015 struct smb2_logoff_req *req; /* response is also trivial struct */
1017 struct TCP_Server_Info *server;
1019 cifs_dbg(FYI, "disconnect session %p\n", ses);
1021 if (ses && (ses->server))
1022 server = ses->server;
1026 /* no need to send SMB logoff if uid already closed due to reconnect */
1027 if (ses->need_reconnect)
1028 goto smb2_session_already_dead;
1030 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1034 /* since no tcon, smb2_init can not do this, so do here */
1035 req->hdr.SessionId = ses->Suid;
1037 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1039 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
1041 * No tcon so can't do
1042 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1045 smb2_session_already_dead:
1049 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1051 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1054 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1056 /* These are similar values to what Windows uses */
1057 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1059 tcon->max_chunks = 256;
1060 tcon->max_bytes_chunk = 1048576;
1061 tcon->max_bytes_copy = 16777216;
1065 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1066 struct cifs_tcon *tcon, const struct nls_table *cp)
1068 struct smb2_tree_connect_req *req;
1069 struct smb2_tree_connect_rsp *rsp = NULL;
1074 struct TCP_Server_Info *server;
1075 __le16 *unc_path = NULL;
1077 cifs_dbg(FYI, "TCON\n");
1079 if ((ses->server) && tree)
1080 server = ses->server;
1084 if (tcon && tcon->bad_network_name)
1087 if ((tcon && tcon->seal) &&
1088 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
1089 cifs_dbg(VFS, "encryption requested but no server support");
1093 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1094 if (unc_path == NULL)
1097 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1099 if (unc_path_len < 2) {
1104 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1111 /* since no tcon, smb2_init can not do this, so do here */
1112 req->hdr.SessionId = ses->Suid;
1113 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
1114 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
1117 iov[0].iov_base = (char *)req;
1118 /* 4 for rfc1002 length field and 1 for pad */
1119 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1121 /* Testing shows that buffer offset must be at location of Buffer[0] */
1122 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1123 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1124 req->PathLength = cpu_to_le16(unc_path_len - 2);
1125 iov[1].iov_base = unc_path;
1126 iov[1].iov_len = unc_path_len;
1128 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1130 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
1131 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
1135 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1136 tcon->need_reconnect = true;
1138 goto tcon_error_exit;
1142 ses->ipc_tid = rsp->hdr.TreeId;
1146 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
1147 cifs_dbg(FYI, "connection to disk share\n");
1148 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
1150 cifs_dbg(FYI, "connection to pipe share\n");
1151 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1153 cifs_dbg(FYI, "connection to printer\n");
1155 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1157 goto tcon_error_exit;
1160 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1161 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1162 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1163 tcon->tidStatus = CifsGood;
1164 tcon->need_reconnect = false;
1165 tcon->tid = rsp->hdr.TreeId;
1166 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1168 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1169 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1170 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1171 init_copy_chunk_defaults(tcon);
1172 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1173 cifs_dbg(VFS, "Encrypted shares not supported");
1174 if (tcon->ses->server->ops->validate_negotiate)
1175 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1177 free_rsp_buf(resp_buftype, rsp);
1182 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
1183 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1185 tcon->bad_network_name = true;
1191 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1193 struct smb2_tree_disconnect_req *req; /* response is trivial */
1195 struct TCP_Server_Info *server;
1196 struct cifs_ses *ses = tcon->ses;
1198 cifs_dbg(FYI, "Tree Disconnect\n");
1200 if (ses && (ses->server))
1201 server = ses->server;
1205 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1208 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1212 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
1214 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1220 static struct create_durable *
1221 create_durable_buf(void)
1223 struct create_durable *buf;
1225 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1229 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1230 (struct create_durable, Data));
1231 buf->ccontext.DataLength = cpu_to_le32(16);
1232 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1233 (struct create_durable, Name));
1234 buf->ccontext.NameLength = cpu_to_le16(4);
1235 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1243 static struct create_durable *
1244 create_reconnect_durable_buf(struct cifs_fid *fid)
1246 struct create_durable *buf;
1248 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1252 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1253 (struct create_durable, Data));
1254 buf->ccontext.DataLength = cpu_to_le32(16);
1255 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1256 (struct create_durable, Name));
1257 buf->ccontext.NameLength = cpu_to_le16(4);
1258 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1259 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1260 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1269 parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1270 unsigned int *epoch)
1273 struct create_context *cc;
1275 unsigned int remaining;
1278 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
1279 remaining = le32_to_cpu(rsp->CreateContextsLength);
1280 cc = (struct create_context *)data_offset;
1281 while (remaining >= sizeof(struct create_context)) {
1282 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1283 if (le16_to_cpu(cc->NameLength) == 4 &&
1284 strncmp(name, "RqLs", 4) == 0)
1285 return server->ops->parse_lease_buf(cc, epoch);
1287 next = le32_to_cpu(cc->Next);
1291 cc = (struct create_context *)((char *)cc + next);
1298 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1299 unsigned int *num_iovec, __u8 *oplock)
1301 struct smb2_create_req *req = iov[0].iov_base;
1302 unsigned int num = *num_iovec;
1304 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
1305 if (iov[num].iov_base == NULL)
1307 iov[num].iov_len = server->vals->create_lease_size;
1308 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1309 if (!req->CreateContextsOffset)
1310 req->CreateContextsOffset = cpu_to_le32(
1311 sizeof(struct smb2_create_req) - 4 +
1312 iov[num - 1].iov_len);
1313 le32_add_cpu(&req->CreateContextsLength,
1314 server->vals->create_lease_size);
1315 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
1316 *num_iovec = num + 1;
1320 static struct create_durable_v2 *
1321 create_durable_v2_buf(struct cifs_fid *pfid)
1323 struct create_durable_v2 *buf;
1325 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1329 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1330 (struct create_durable_v2, dcontext));
1331 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1332 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1333 (struct create_durable_v2, Name));
1334 buf->ccontext.NameLength = cpu_to_le16(4);
1336 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1337 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1338 generate_random_uuid(buf->dcontext.CreateGuid);
1339 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1341 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1349 static struct create_durable_handle_reconnect_v2 *
1350 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1352 struct create_durable_handle_reconnect_v2 *buf;
1354 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1359 buf->ccontext.DataOffset =
1360 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1362 buf->ccontext.DataLength =
1363 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1364 buf->ccontext.NameOffset =
1365 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1367 buf->ccontext.NameLength = cpu_to_le16(4);
1369 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1370 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1371 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1372 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1374 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1383 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1384 struct cifs_open_parms *oparms)
1386 struct smb2_create_req *req = iov[0].iov_base;
1387 unsigned int num = *num_iovec;
1389 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1390 if (iov[num].iov_base == NULL)
1392 iov[num].iov_len = sizeof(struct create_durable_v2);
1393 if (!req->CreateContextsOffset)
1394 req->CreateContextsOffset =
1395 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1397 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1398 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1399 *num_iovec = num + 1;
1404 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1405 struct cifs_open_parms *oparms)
1407 struct smb2_create_req *req = iov[0].iov_base;
1408 unsigned int num = *num_iovec;
1410 /* indicate that we don't need to relock the file */
1411 oparms->reconnect = false;
1413 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1414 if (iov[num].iov_base == NULL)
1416 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1417 if (!req->CreateContextsOffset)
1418 req->CreateContextsOffset =
1419 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1421 le32_add_cpu(&req->CreateContextsLength,
1422 sizeof(struct create_durable_handle_reconnect_v2));
1423 inc_rfc1001_len(&req->hdr,
1424 sizeof(struct create_durable_handle_reconnect_v2));
1425 *num_iovec = num + 1;
1430 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1431 struct cifs_open_parms *oparms, bool use_persistent)
1433 struct smb2_create_req *req = iov[0].iov_base;
1434 unsigned int num = *num_iovec;
1436 if (use_persistent) {
1437 if (oparms->reconnect)
1438 return add_durable_reconnect_v2_context(iov, num_iovec,
1441 return add_durable_v2_context(iov, num_iovec, oparms);
1444 if (oparms->reconnect) {
1445 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1446 /* indicate that we don't need to relock the file */
1447 oparms->reconnect = false;
1449 iov[num].iov_base = create_durable_buf();
1450 if (iov[num].iov_base == NULL)
1452 iov[num].iov_len = sizeof(struct create_durable);
1453 if (!req->CreateContextsOffset)
1454 req->CreateContextsOffset =
1455 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1457 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1458 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1459 *num_iovec = num + 1;
1464 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
1465 __u8 *oplock, struct smb2_file_all_info *buf,
1466 struct smb2_err_rsp **err_buf)
1468 struct smb2_create_req *req;
1469 struct smb2_create_rsp *rsp;
1470 struct TCP_Server_Info *server;
1471 struct cifs_tcon *tcon = oparms->tcon;
1472 struct cifs_ses *ses = tcon->ses;
1476 __le16 *copy_path = NULL;
1479 unsigned int num_iovecs = 2;
1480 __u32 file_attributes = 0;
1481 char *dhc_buf = NULL, *lc_buf = NULL;
1483 cifs_dbg(FYI, "create/open\n");
1485 if (ses && (ses->server))
1486 server = ses->server;
1490 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1494 if (oparms->create_options & CREATE_OPTION_READONLY)
1495 file_attributes |= ATTR_READONLY;
1496 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1497 file_attributes |= ATTR_SYSTEM;
1499 req->ImpersonationLevel = IL_IMPERSONATION;
1500 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
1501 /* File attributes ignored on open (used in create though) */
1502 req->FileAttributes = cpu_to_le32(file_attributes);
1503 req->ShareAccess = FILE_SHARE_ALL_LE;
1504 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1505 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
1506 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1507 /* do not count rfc1001 len field */
1508 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1510 iov[0].iov_base = (char *)req;
1511 /* 4 for rfc1002 length field */
1512 iov[0].iov_len = get_rfc1002_length(req) + 4;
1514 /* MUST set path len (NameLength) to 0 opening root of share */
1515 req->NameLength = cpu_to_le16(uni_path_len - 2);
1516 /* -1 since last byte is buf[0] which is sent below (path) */
1518 if (uni_path_len % 8 != 0) {
1519 copy_size = uni_path_len / 8 * 8;
1520 if (copy_size < uni_path_len)
1523 copy_path = kzalloc(copy_size, GFP_KERNEL);
1526 memcpy((char *)copy_path, (const char *)path,
1528 uni_path_len = copy_size;
1532 iov[1].iov_len = uni_path_len;
1533 iov[1].iov_base = path;
1534 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1535 inc_rfc1001_len(req, uni_path_len - 1);
1537 if (!server->oplocks)
1538 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1540 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1541 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1542 req->RequestedOplockLevel = *oplock;
1544 rc = add_lease_context(server, iov, &num_iovecs, oplock);
1546 cifs_small_buf_release(req);
1550 lc_buf = iov[num_iovecs-1].iov_base;
1553 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1554 /* need to set Next field of lease context if we request it */
1555 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
1556 struct create_context *ccontext =
1557 (struct create_context *)iov[num_iovecs-1].iov_base;
1559 cpu_to_le32(server->vals->create_lease_size);
1562 rc = add_durable_context(iov, &num_iovecs, oparms,
1563 tcon->use_persistent);
1565 cifs_small_buf_release(req);
1570 dhc_buf = iov[num_iovecs-1].iov_base;
1573 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1574 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1577 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
1579 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1584 oparms->fid->persistent_fid = rsp->PersistentFileId;
1585 oparms->fid->volatile_fid = rsp->VolatileFileId;
1588 memcpy(buf, &rsp->CreationTime, 32);
1589 buf->AllocationSize = rsp->AllocationSize;
1590 buf->EndOfFile = rsp->EndofFile;
1591 buf->Attributes = rsp->FileAttributes;
1592 buf->NumberOfLinks = cpu_to_le32(1);
1593 buf->DeletePending = 0;
1596 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
1597 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
1599 *oplock = rsp->OplockLevel;
1604 free_rsp_buf(resp_buftype, rsp);
1609 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1612 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1613 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1614 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1616 struct smb2_ioctl_req *req;
1617 struct smb2_ioctl_rsp *rsp;
1618 struct TCP_Server_Info *server;
1619 struct cifs_ses *ses;
1625 cifs_dbg(FYI, "SMB2 IOCTL\n");
1627 if (out_data != NULL)
1630 /* zero out returned data len, in case of error */
1639 if (ses && (ses->server))
1640 server = ses->server;
1644 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1648 req->CtlCode = cpu_to_le32(opcode);
1649 req->PersistentFileId = persistent_fid;
1650 req->VolatileFileId = volatile_fid;
1653 req->InputCount = cpu_to_le32(indatalen);
1654 /* do not set InputOffset if no input data */
1656 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1657 iov[1].iov_base = in_data;
1658 iov[1].iov_len = indatalen;
1663 req->OutputOffset = 0;
1664 req->OutputCount = 0; /* MBZ */
1667 * Could increase MaxOutputResponse, but that would require more
1668 * than one credit. Windows typically sets this smaller, but for some
1669 * ioctls it may be useful to allow server to send more. No point
1670 * limiting what the server can send as long as fits in one credit
1672 req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1675 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1679 iov[0].iov_base = (char *)req;
1682 * If no input data, the size of ioctl struct in
1683 * protocol spec still includes a 1 byte data buffer,
1684 * but if input data passed to ioctl, we do not
1685 * want to double count this, so we do not send
1686 * the dummy one byte of data in iovec[0] if sending
1687 * input data (in iovec[1]). We also must add 4 bytes
1688 * in first iovec to allow for rfc1002 length field.
1692 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1693 inc_rfc1001_len(req, indatalen - 1);
1695 iov[0].iov_len = get_rfc1002_length(req) + 4;
1698 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1699 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1701 if ((rc != 0) && (rc != -EINVAL)) {
1702 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1704 } else if (rc == -EINVAL) {
1705 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1706 (opcode != FSCTL_SRV_COPYCHUNK)) {
1707 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1712 /* check if caller wants to look at return data or just return rc */
1713 if ((plen == NULL) || (out_data == NULL))
1716 *plen = le32_to_cpu(rsp->OutputCount);
1718 /* We check for obvious errors in the output buffer length and offset */
1720 goto ioctl_exit; /* server returned no data */
1721 else if (*plen > 0xFF00) {
1722 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1728 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1729 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1730 le32_to_cpu(rsp->OutputOffset));
1736 *out_data = kmalloc(*plen, GFP_KERNEL);
1737 if (*out_data == NULL) {
1743 (char *)&rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
1746 free_rsp_buf(resp_buftype, rsp);
1751 * Individual callers to ioctl worker function follow
1755 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1756 u64 persistent_fid, u64 volatile_fid)
1759 struct compress_ioctl fsctl_input;
1760 char *ret_data = NULL;
1762 fsctl_input.CompressionState =
1763 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
1765 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1766 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1767 (char *)&fsctl_input /* data input */,
1768 2 /* in data len */, &ret_data /* out data */, NULL);
1770 cifs_dbg(FYI, "set compression rc %d\n", rc);
1776 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1777 u64 persistent_fid, u64 volatile_fid)
1779 struct smb2_close_req *req;
1780 struct smb2_close_rsp *rsp;
1781 struct TCP_Server_Info *server;
1782 struct cifs_ses *ses = tcon->ses;
1787 cifs_dbg(FYI, "Close\n");
1789 if (ses && (ses->server))
1790 server = ses->server;
1794 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1798 req->PersistentFileId = persistent_fid;
1799 req->VolatileFileId = volatile_fid;
1801 iov[0].iov_base = (char *)req;
1802 /* 4 for rfc1002 length field */
1803 iov[0].iov_len = get_rfc1002_length(req) + 4;
1805 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1806 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1809 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
1813 /* BB FIXME - decode close response, update inode for caching */
1816 free_rsp_buf(resp_buftype, rsp);
1821 validate_buf(unsigned int offset, unsigned int buffer_length,
1822 struct smb2_hdr *hdr, unsigned int min_buf_size)
1825 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1826 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1827 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1828 char *end_of_buf = begin_of_buf + buffer_length;
1831 if (buffer_length < min_buf_size) {
1832 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1833 buffer_length, min_buf_size);
1837 /* check if beyond RFC1001 maximum length */
1838 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
1839 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1840 buffer_length, smb_len);
1844 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
1845 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
1853 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1854 * Caller must free buffer.
1857 validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1858 struct smb2_hdr *hdr, unsigned int minbufsize,
1862 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1868 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1872 memcpy(data, begin_of_buf, buffer_length);
1878 query_info(const unsigned int xid, struct cifs_tcon *tcon,
1879 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1880 size_t output_len, size_t min_len, void *data)
1882 struct smb2_query_info_req *req;
1883 struct smb2_query_info_rsp *rsp = NULL;
1887 struct TCP_Server_Info *server;
1888 struct cifs_ses *ses = tcon->ses;
1890 cifs_dbg(FYI, "Query Info\n");
1892 if (ses && (ses->server))
1893 server = ses->server;
1897 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1901 req->InfoType = SMB2_O_INFO_FILE;
1902 req->FileInfoClass = info_class;
1903 req->PersistentFileId = persistent_fid;
1904 req->VolatileFileId = volatile_fid;
1905 /* 4 for rfc1002 length field and 1 for Buffer */
1906 req->InputBufferOffset =
1907 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
1908 req->OutputBufferLength = cpu_to_le32(output_len);
1910 iov[0].iov_base = (char *)req;
1911 /* 4 for rfc1002 length field */
1912 iov[0].iov_len = get_rfc1002_length(req) + 4;
1914 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1915 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1918 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1922 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1923 le32_to_cpu(rsp->OutputBufferLength),
1924 &rsp->hdr, min_len, data);
1927 free_rsp_buf(resp_buftype, rsp);
1932 SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1933 u64 persistent_fid, u64 volatile_fid,
1934 struct smb2_file_all_info *data)
1936 return query_info(xid, tcon, persistent_fid, volatile_fid,
1937 FILE_ALL_INFORMATION,
1938 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
1939 sizeof(struct smb2_file_all_info), data);
1943 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1944 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1946 return query_info(xid, tcon, persistent_fid, volatile_fid,
1947 FILE_INTERNAL_INFORMATION,
1948 sizeof(struct smb2_file_internal_info),
1949 sizeof(struct smb2_file_internal_info), uniqueid);
1953 * This is a no-op for now. We're not really interested in the reply, but
1954 * rather in the fact that the server sent one and that server->lstrp
1957 * FIXME: maybe we should consider checking that the reply matches request?
1960 smb2_echo_callback(struct mid_q_entry *mid)
1962 struct TCP_Server_Info *server = mid->callback_data;
1963 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1964 unsigned int credits_received = 1;
1966 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1967 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1969 mutex_lock(&server->srv_mutex);
1970 DeleteMidQEntry(mid);
1971 mutex_unlock(&server->srv_mutex);
1972 add_credits(server, credits_received, CIFS_ECHO_OP);
1976 SMB2_echo(struct TCP_Server_Info *server)
1978 struct smb2_echo_req *req;
1981 struct smb_rqst rqst = { .rq_iov = &iov,
1984 cifs_dbg(FYI, "In echo request\n");
1986 if (server->tcpStatus == CifsNeedNegotiate) {
1987 struct list_head *tmp, *tmp2;
1988 struct cifs_ses *ses;
1989 struct cifs_tcon *tcon;
1991 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
1992 spin_lock(&cifs_tcp_ses_lock);
1993 list_for_each(tmp, &server->smb_ses_list) {
1994 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
1995 list_for_each(tmp2, &ses->tcon_list) {
1996 tcon = list_entry(tmp2, struct cifs_tcon,
1998 /* add check for persistent handle reconnect */
1999 if (tcon && tcon->need_reconnect) {
2000 spin_unlock(&cifs_tcp_ses_lock);
2001 rc = smb2_reconnect(SMB2_ECHO, tcon);
2002 spin_lock(&cifs_tcp_ses_lock);
2006 spin_unlock(&cifs_tcp_ses_lock);
2009 /* if no session, renegotiate failed above */
2010 if (server->tcpStatus == CifsNeedNegotiate)
2013 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2017 req->hdr.CreditRequest = cpu_to_le16(1);
2019 iov.iov_base = (char *)req;
2020 /* 4 for rfc1002 length field */
2021 iov.iov_len = get_rfc1002_length(req) + 4;
2023 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
2026 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
2028 cifs_small_buf_release(req);
2033 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2036 struct smb2_flush_req *req;
2037 struct TCP_Server_Info *server;
2038 struct cifs_ses *ses = tcon->ses;
2043 cifs_dbg(FYI, "Flush\n");
2045 if (ses && (ses->server))
2046 server = ses->server;
2050 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2054 req->PersistentFileId = persistent_fid;
2055 req->VolatileFileId = volatile_fid;
2057 iov[0].iov_base = (char *)req;
2058 /* 4 for rfc1002 length field */
2059 iov[0].iov_len = get_rfc1002_length(req) + 4;
2061 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
2064 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2066 free_rsp_buf(resp_buftype, iov[0].iov_base);
2071 * To form a chain of read requests, any read requests after the first should
2072 * have the end_of_chain boolean set to true.
2075 smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
2076 unsigned int remaining_bytes, int request_type)
2079 struct smb2_read_req *req = NULL;
2081 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
2084 if (io_parms->tcon->ses->server == NULL)
2085 return -ECONNABORTED;
2087 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2089 req->PersistentFileId = io_parms->persistent_fid;
2090 req->VolatileFileId = io_parms->volatile_fid;
2091 req->ReadChannelInfoOffset = 0; /* reserved */
2092 req->ReadChannelInfoLength = 0; /* reserved */
2093 req->Channel = 0; /* reserved */
2094 req->MinimumCount = 0;
2095 req->Length = cpu_to_le32(io_parms->length);
2096 req->Offset = cpu_to_le64(io_parms->offset);
2098 if (request_type & CHAINED_REQUEST) {
2099 if (!(request_type & END_OF_CHAIN)) {
2100 /* 4 for rfc1002 length field */
2101 req->hdr.NextCommand =
2102 cpu_to_le32(get_rfc1002_length(req) + 4);
2103 } else /* END_OF_CHAIN */
2104 req->hdr.NextCommand = 0;
2105 if (request_type & RELATED_REQUEST) {
2106 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2108 * Related requests use info from previous read request
2111 req->hdr.SessionId = 0xFFFFFFFF;
2112 req->hdr.TreeId = 0xFFFFFFFF;
2113 req->PersistentFileId = 0xFFFFFFFF;
2114 req->VolatileFileId = 0xFFFFFFFF;
2117 if (remaining_bytes > io_parms->length)
2118 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2120 req->RemainingBytes = 0;
2122 iov[0].iov_base = (char *)req;
2123 /* 4 for rfc1002 length field */
2124 iov[0].iov_len = get_rfc1002_length(req) + 4;
2129 smb2_readv_callback(struct mid_q_entry *mid)
2131 struct cifs_readdata *rdata = mid->callback_data;
2132 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2133 struct TCP_Server_Info *server = tcon->ses->server;
2134 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
2135 unsigned int credits_received = 1;
2136 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
2138 .rq_pages = rdata->pages,
2139 .rq_npages = rdata->nr_pages,
2140 .rq_pagesz = rdata->pagesz,
2141 .rq_tailsz = rdata->tailsz };
2143 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2144 __func__, mid->mid, mid->mid_state, rdata->result,
2147 switch (mid->mid_state) {
2148 case MID_RESPONSE_RECEIVED:
2149 credits_received = le16_to_cpu(buf->CreditRequest);
2150 /* result already set, check signature */
2154 rc = smb2_verify_signature(&rqst, server);
2156 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2159 /* FIXME: should this be counted toward the initiating task? */
2160 task_io_account_read(rdata->got_bytes);
2161 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2163 case MID_REQUEST_SUBMITTED:
2164 case MID_RETRY_NEEDED:
2165 rdata->result = -EAGAIN;
2166 if (server->sign && rdata->got_bytes)
2167 /* reset bytes number since we can not check a sign */
2168 rdata->got_bytes = 0;
2169 /* FIXME: should this be counted toward the initiating task? */
2170 task_io_account_read(rdata->got_bytes);
2171 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2174 if (rdata->result != -ENODATA)
2175 rdata->result = -EIO;
2179 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2181 queue_work(cifsiod_wq, &rdata->work);
2182 mutex_lock(&server->srv_mutex);
2183 DeleteMidQEntry(mid);
2184 mutex_unlock(&server->srv_mutex);
2185 add_credits(server, credits_received, 0);
2188 /* smb2_async_readv - send an async write, and set up mid to handle result */
2190 smb2_async_readv(struct cifs_readdata *rdata)
2193 struct smb2_hdr *buf;
2194 struct cifs_io_parms io_parms;
2195 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
2197 struct TCP_Server_Info *server;
2199 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2200 __func__, rdata->offset, rdata->bytes);
2202 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2203 io_parms.offset = rdata->offset;
2204 io_parms.length = rdata->bytes;
2205 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2206 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2207 io_parms.pid = rdata->pid;
2209 server = io_parms.tcon->ses->server;
2211 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
2213 if (rc == -EAGAIN && rdata->credits) {
2214 /* credits was reset by reconnect */
2216 /* reduce in_flight value since we won't send the req */
2217 spin_lock(&server->req_lock);
2218 server->in_flight--;
2219 spin_unlock(&server->req_lock);
2224 buf = (struct smb2_hdr *)rdata->iov.iov_base;
2225 /* 4 for rfc1002 length field */
2226 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
2228 if (rdata->credits) {
2229 buf->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2230 SMB2_MAX_BUFFER_SIZE));
2231 buf->CreditRequest = buf->CreditCharge;
2232 spin_lock(&server->req_lock);
2233 server->credits += rdata->credits -
2234 le16_to_cpu(buf->CreditCharge);
2235 spin_unlock(&server->req_lock);
2236 wake_up(&server->request_q);
2237 flags = CIFS_HAS_CREDITS;
2240 kref_get(&rdata->refcount);
2241 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
2242 cifs_readv_receive, smb2_readv_callback,
2245 kref_put(&rdata->refcount, cifs_readdata_release);
2246 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2249 cifs_small_buf_release(buf);
2254 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2255 unsigned int *nbytes, char **buf, int *buf_type)
2257 int resp_buftype, rc = -EACCES;
2258 struct smb2_read_rsp *rsp = NULL;
2262 rc = smb2_new_read_req(iov, io_parms, 0, 0);
2266 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2267 &resp_buftype, CIFS_LOG_ERROR);
2269 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
2271 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
2272 free_rsp_buf(resp_buftype, iov[0].iov_base);
2277 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2278 cifs_dbg(VFS, "Send error in read = %d\n", rc);
2280 *nbytes = le32_to_cpu(rsp->DataLength);
2281 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2282 (*nbytes > io_parms->length)) {
2283 cifs_dbg(FYI, "bad length %d for count %d\n",
2284 *nbytes, io_parms->length);
2291 memcpy(*buf, (char *)&rsp->hdr.ProtocolId + rsp->DataOffset,
2293 free_rsp_buf(resp_buftype, iov[0].iov_base);
2294 } else if (resp_buftype != CIFS_NO_BUFFER) {
2295 *buf = iov[0].iov_base;
2296 if (resp_buftype == CIFS_SMALL_BUFFER)
2297 *buf_type = CIFS_SMALL_BUFFER;
2298 else if (resp_buftype == CIFS_LARGE_BUFFER)
2299 *buf_type = CIFS_LARGE_BUFFER;
2305 * Check the mid_state and signature on received buffer (if any), and queue the
2306 * workqueue completion task.
2309 smb2_writev_callback(struct mid_q_entry *mid)
2311 struct cifs_writedata *wdata = mid->callback_data;
2312 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2313 struct TCP_Server_Info *server = tcon->ses->server;
2314 unsigned int written;
2315 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2316 unsigned int credits_received = 1;
2318 switch (mid->mid_state) {
2319 case MID_RESPONSE_RECEIVED:
2320 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
2321 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2322 if (wdata->result != 0)
2325 written = le32_to_cpu(rsp->DataLength);
2327 * Mask off high 16 bits when bytes written as returned
2328 * by the server is greater than bytes requested by the
2329 * client. OS/2 servers are known to set incorrect
2332 if (written > wdata->bytes)
2335 if (written < wdata->bytes)
2336 wdata->result = -ENOSPC;
2338 wdata->bytes = written;
2340 case MID_REQUEST_SUBMITTED:
2341 case MID_RETRY_NEEDED:
2342 wdata->result = -EAGAIN;
2345 wdata->result = -EIO;
2350 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2352 queue_work(cifsiod_wq, &wdata->work);
2353 mutex_lock(&server->srv_mutex);
2354 DeleteMidQEntry(mid);
2355 mutex_unlock(&server->srv_mutex);
2356 add_credits(tcon->ses->server, credits_received, 0);
2359 /* smb2_async_writev - send an async write, and set up mid to handle result */
2361 smb2_async_writev(struct cifs_writedata *wdata,
2362 void (*release)(struct kref *kref))
2364 int rc = -EACCES, flags = 0;
2365 struct smb2_write_req *req = NULL;
2366 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2367 struct TCP_Server_Info *server = tcon->ses->server;
2369 struct smb_rqst rqst;
2371 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
2373 if (rc == -EAGAIN && wdata->credits) {
2374 /* credits was reset by reconnect */
2376 /* reduce in_flight value since we won't send the req */
2377 spin_lock(&server->req_lock);
2378 server->in_flight--;
2379 spin_unlock(&server->req_lock);
2381 goto async_writev_out;
2384 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
2386 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2387 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2388 req->WriteChannelInfoOffset = 0;
2389 req->WriteChannelInfoLength = 0;
2391 req->Offset = cpu_to_le64(wdata->offset);
2392 /* 4 for rfc1002 length field */
2393 req->DataOffset = cpu_to_le16(
2394 offsetof(struct smb2_write_req, Buffer) - 4);
2395 req->RemainingBytes = 0;
2397 /* 4 for rfc1002 length field and 1 for Buffer */
2398 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
2403 rqst.rq_pages = wdata->pages;
2404 rqst.rq_npages = wdata->nr_pages;
2405 rqst.rq_pagesz = wdata->pagesz;
2406 rqst.rq_tailsz = wdata->tailsz;
2408 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2409 wdata->offset, wdata->bytes);
2411 req->Length = cpu_to_le32(wdata->bytes);
2413 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2415 if (wdata->credits) {
2416 req->hdr.CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2417 SMB2_MAX_BUFFER_SIZE));
2418 req->hdr.CreditRequest = req->hdr.CreditCharge;
2419 spin_lock(&server->req_lock);
2420 server->credits += wdata->credits -
2421 le16_to_cpu(req->hdr.CreditCharge);
2422 spin_unlock(&server->req_lock);
2423 wake_up(&server->request_q);
2424 flags = CIFS_HAS_CREDITS;
2427 kref_get(&wdata->refcount);
2428 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2432 kref_put(&wdata->refcount, release);
2433 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2437 cifs_small_buf_release(req);
2442 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2443 * The length field from io_parms must be at least 1 and indicates a number of
2444 * elements with data to write that begins with position 1 in iov array. All
2445 * data length is specified by count.
2448 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2449 unsigned int *nbytes, struct kvec *iov, int n_vec)
2452 struct smb2_write_req *req = NULL;
2453 struct smb2_write_rsp *rsp = NULL;
2460 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2464 if (io_parms->tcon->ses->server == NULL)
2465 return -ECONNABORTED;
2467 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
2469 req->PersistentFileId = io_parms->persistent_fid;
2470 req->VolatileFileId = io_parms->volatile_fid;
2471 req->WriteChannelInfoOffset = 0;
2472 req->WriteChannelInfoLength = 0;
2474 req->Length = cpu_to_le32(io_parms->length);
2475 req->Offset = cpu_to_le64(io_parms->offset);
2476 /* 4 for rfc1002 length field */
2477 req->DataOffset = cpu_to_le16(
2478 offsetof(struct smb2_write_req, Buffer) - 4);
2479 req->RemainingBytes = 0;
2481 iov[0].iov_base = (char *)req;
2482 /* 4 for rfc1002 length field and 1 for Buffer */
2483 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2485 /* length of entire message including data to be written */
2486 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2488 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2490 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
2493 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
2494 cifs_dbg(VFS, "Send error in write = %d\n", rc);
2496 *nbytes = le32_to_cpu(rsp->DataLength);
2498 free_rsp_buf(resp_buftype, rsp);
2503 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2506 unsigned int entrycount = 0;
2507 unsigned int next_offset = 0;
2508 FILE_DIRECTORY_INFO *entryptr;
2510 if (bufstart == NULL)
2513 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2516 entryptr = (FILE_DIRECTORY_INFO *)
2517 ((char *)entryptr + next_offset);
2519 if ((char *)entryptr + size > end_of_buf) {
2520 cifs_dbg(VFS, "malformed search entry would overflow\n");
2524 len = le32_to_cpu(entryptr->FileNameLength);
2525 if ((char *)entryptr + len + size > end_of_buf) {
2526 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2531 *lastentry = (char *)entryptr;
2534 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2546 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2547 u64 persistent_fid, u64 volatile_fid, int index,
2548 struct cifs_search_info *srch_inf)
2550 struct smb2_query_directory_req *req;
2551 struct smb2_query_directory_rsp *rsp = NULL;
2555 int resp_buftype = CIFS_NO_BUFFER;
2556 unsigned char *bufptr;
2557 struct TCP_Server_Info *server;
2558 struct cifs_ses *ses = tcon->ses;
2559 __le16 asteriks = cpu_to_le16('*');
2561 unsigned int output_size = CIFSMaxBufSize;
2562 size_t info_buf_size;
2564 if (ses && (ses->server))
2565 server = ses->server;
2569 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2573 switch (srch_inf->info_level) {
2574 case SMB_FIND_FILE_DIRECTORY_INFO:
2575 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2576 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2578 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2579 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2580 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2583 cifs_dbg(VFS, "info level %u isn't supported\n",
2584 srch_inf->info_level);
2589 req->FileIndex = cpu_to_le32(index);
2590 req->PersistentFileId = persistent_fid;
2591 req->VolatileFileId = volatile_fid;
2594 bufptr = req->Buffer;
2595 memcpy(bufptr, &asteriks, len);
2597 req->FileNameOffset =
2598 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2599 req->FileNameLength = cpu_to_le16(len);
2601 * BB could be 30 bytes or so longer if we used SMB2 specific
2602 * buffer lengths, but this is safe and close enough.
2604 output_size = min_t(unsigned int, output_size, server->maxBuf);
2605 output_size = min_t(unsigned int, output_size, 2 << 15);
2606 req->OutputBufferLength = cpu_to_le32(output_size);
2608 iov[0].iov_base = (char *)req;
2609 /* 4 for RFC1001 length and 1 for Buffer */
2610 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2612 iov[1].iov_base = (char *)(req->Buffer);
2613 iov[1].iov_len = len;
2615 inc_rfc1001_len(req, len - 1 /* Buffer */);
2617 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
2618 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2621 if (rc == -ENODATA && rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2622 srch_inf->endOfSearch = true;
2625 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2629 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2630 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2635 srch_inf->unicode = true;
2637 if (srch_inf->ntwrk_buf_start) {
2638 if (srch_inf->smallBuf)
2639 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2641 cifs_buf_release(srch_inf->ntwrk_buf_start);
2643 srch_inf->ntwrk_buf_start = (char *)rsp;
2644 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2645 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2646 /* 4 for rfc1002 length field */
2647 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2648 srch_inf->entries_in_buffer =
2649 num_entries(srch_inf->srch_entries_start, end_of_smb,
2650 &srch_inf->last_entry, info_buf_size);
2651 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
2652 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2653 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2654 srch_inf->srch_entries_start, srch_inf->last_entry);
2655 if (resp_buftype == CIFS_LARGE_BUFFER)
2656 srch_inf->smallBuf = false;
2657 else if (resp_buftype == CIFS_SMALL_BUFFER)
2658 srch_inf->smallBuf = true;
2660 cifs_dbg(VFS, "illegal search buffer type\n");
2665 free_rsp_buf(resp_buftype, rsp);
2670 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2671 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
2672 unsigned int num, void **data, unsigned int *size)
2674 struct smb2_set_info_req *req;
2675 struct smb2_set_info_rsp *rsp = NULL;
2680 struct TCP_Server_Info *server;
2681 struct cifs_ses *ses = tcon->ses;
2683 if (ses && (ses->server))
2684 server = ses->server;
2691 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2695 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2701 req->hdr.ProcessId = cpu_to_le32(pid);
2703 req->InfoType = SMB2_O_INFO_FILE;
2704 req->FileInfoClass = info_class;
2705 req->PersistentFileId = persistent_fid;
2706 req->VolatileFileId = volatile_fid;
2708 /* 4 for RFC1001 length and 1 for Buffer */
2710 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2711 req->BufferLength = cpu_to_le32(*size);
2713 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2715 memcpy(req->Buffer, *data, *size);
2717 iov[0].iov_base = (char *)req;
2718 /* 4 for RFC1001 length */
2719 iov[0].iov_len = get_rfc1002_length(req) + 4;
2721 for (i = 1; i < num; i++) {
2722 inc_rfc1001_len(req, size[i]);
2723 le32_add_cpu(&req->BufferLength, size[i]);
2724 iov[i].iov_base = (char *)data[i];
2725 iov[i].iov_len = size[i];
2728 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2729 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2732 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
2734 free_rsp_buf(resp_buftype, rsp);
2740 SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2741 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2743 struct smb2_file_rename_info info;
2745 unsigned int size[2];
2747 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2749 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2753 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2754 /* 0 = fail if target already exists */
2755 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2756 info.FileNameLength = cpu_to_le32(len);
2759 size[0] = sizeof(struct smb2_file_rename_info);
2761 data[1] = target_file;
2762 size[1] = len + 2 /* null */;
2764 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2765 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2772 SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2773 u64 persistent_fid, u64 volatile_fid)
2775 __u8 delete_pending = 1;
2779 data = &delete_pending;
2780 size = 1; /* sizeof __u8 */
2782 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2783 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2788 SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2789 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2791 struct smb2_file_link_info info;
2793 unsigned int size[2];
2795 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2797 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2801 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2802 /* 0 = fail if link already exists */
2803 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2804 info.FileNameLength = cpu_to_le32(len);
2807 size[0] = sizeof(struct smb2_file_link_info);
2809 data[1] = target_file;
2810 size[1] = len + 2 /* null */;
2812 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2813 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
2819 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2820 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
2822 struct smb2_file_eof_info info;
2826 info.EndOfFile = *eof;
2829 size = sizeof(struct smb2_file_eof_info);
2832 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2833 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2835 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2836 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
2840 SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2841 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2844 size = sizeof(FILE_BASIC_INFO);
2845 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2846 current->tgid, FILE_BASIC_INFORMATION, 1,
2847 (void **)&buf, &size);
2851 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2852 const u64 persistent_fid, const u64 volatile_fid,
2856 struct smb2_oplock_break *req = NULL;
2858 cifs_dbg(FYI, "SMB2_oplock_break\n");
2859 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2864 req->VolatileFid = volatile_fid;
2865 req->PersistentFid = persistent_fid;
2866 req->OplockLevel = oplock_level;
2867 req->hdr.CreditRequest = cpu_to_le16(1);
2869 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2870 /* SMB2 buffer freed by function above */
2873 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
2874 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
2881 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2882 struct kstatfs *kst)
2884 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2885 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2886 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2887 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
2888 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2893 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2894 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2897 struct smb2_query_info_req *req;
2899 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
2901 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2904 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2908 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2909 req->FileInfoClass = level;
2910 req->PersistentFileId = persistent_fid;
2911 req->VolatileFileId = volatile_fid;
2912 /* 4 for rfc1002 length field and 1 for pad */
2913 req->InputBufferOffset =
2914 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2915 req->OutputBufferLength = cpu_to_le32(
2916 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2918 iov->iov_base = (char *)req;
2919 /* 4 for rfc1002 length field */
2920 iov->iov_len = get_rfc1002_length(req) + 4;
2925 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2926 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2928 struct smb2_query_info_rsp *rsp = NULL;
2932 struct cifs_ses *ses = tcon->ses;
2933 struct smb2_fs_full_size_info *info = NULL;
2935 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2936 sizeof(struct smb2_fs_full_size_info),
2937 persistent_fid, volatile_fid);
2941 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2943 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2946 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2948 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
2949 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
2950 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2951 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2952 sizeof(struct smb2_fs_full_size_info));
2954 copy_fs_info_to_kstatfs(info, fsdata);
2957 free_rsp_buf(resp_buftype, iov.iov_base);
2962 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
2963 u64 persistent_fid, u64 volatile_fid, int level)
2965 struct smb2_query_info_rsp *rsp = NULL;
2968 int resp_buftype, max_len, min_len;
2969 struct cifs_ses *ses = tcon->ses;
2970 unsigned int rsp_len, offset;
2972 if (level == FS_DEVICE_INFORMATION) {
2973 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2974 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
2975 } else if (level == FS_ATTRIBUTE_INFORMATION) {
2976 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
2977 min_len = MIN_FS_ATTR_INFO_SIZE;
2978 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
2979 max_len = sizeof(struct smb3_fs_ss_info);
2980 min_len = sizeof(struct smb3_fs_ss_info);
2982 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
2986 rc = build_qfs_info_req(&iov, tcon, level, max_len,
2987 persistent_fid, volatile_fid);
2991 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2993 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2996 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2998 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
2999 offset = le16_to_cpu(rsp->OutputBufferOffset);
3000 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3004 if (level == FS_ATTRIBUTE_INFORMATION)
3005 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3006 + (char *)&rsp->hdr, min_t(unsigned int,
3008 else if (level == FS_DEVICE_INFORMATION)
3009 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3010 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
3011 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3012 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3013 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3014 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3015 tcon->perf_sector_size =
3016 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3020 free_rsp_buf(resp_buftype, iov.iov_base);
3025 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3026 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3027 const __u32 num_lock, struct smb2_lock_element *buf)
3030 struct smb2_lock_req *req = NULL;
3035 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
3037 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3041 req->hdr.ProcessId = cpu_to_le32(pid);
3042 req->LockCount = cpu_to_le16(num_lock);
3044 req->PersistentFileId = persist_fid;
3045 req->VolatileFileId = volatile_fid;
3047 count = num_lock * sizeof(struct smb2_lock_element);
3048 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3050 iov[0].iov_base = (char *)req;
3051 /* 4 for rfc1002 length field and count for all locks */
3052 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3053 iov[1].iov_base = (char *)buf;
3054 iov[1].iov_len = count;
3056 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3057 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
3059 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
3060 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3067 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3068 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3069 const __u64 length, const __u64 offset, const __u32 lock_flags,
3072 struct smb2_lock_element lock;
3074 lock.Offset = cpu_to_le64(offset);
3075 lock.Length = cpu_to_le64(length);
3076 lock.Flags = cpu_to_le32(lock_flags);
3077 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3078 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3080 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3084 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3085 __u8 *lease_key, const __le32 lease_state)
3088 struct smb2_lease_ack *req = NULL;
3090 cifs_dbg(FYI, "SMB2_lease_break\n");
3091 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3096 req->hdr.CreditRequest = cpu_to_le16(1);
3097 req->StructureSize = cpu_to_le16(36);
3098 inc_rfc1001_len(req, 12);
3100 memcpy(req->LeaseKey, lease_key, 16);
3101 req->LeaseState = lease_state;
3103 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
3104 /* SMB2 buffer freed by function above */
3107 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3108 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);