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/uuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/xattr.h>
42 #include "cifsproto.h"
43 #include "smb2proto.h"
44 #include "cifs_unicode.h"
45 #include "cifs_debug.h"
47 #include "smb2status.h"
50 #include "cifs_spnego.h"
53 * The following table defines the expected "StructureSize" of SMB2 requests
54 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
56 * Note that commands are defined in smb2pdu.h in le16 but the array below is
57 * indexed by command in host byte order.
59 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
60 /* SMB2_NEGOTIATE */ 36,
61 /* SMB2_SESSION_SETUP */ 25,
63 /* SMB2_TREE_CONNECT */ 9,
64 /* SMB2_TREE_DISCONNECT */ 4,
74 /* SMB2_QUERY_DIRECTORY */ 33,
75 /* SMB2_CHANGE_NOTIFY */ 32,
76 /* SMB2_QUERY_INFO */ 41,
77 /* SMB2_SET_INFO */ 33,
78 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
81 static int encryption_required(const struct cifs_tcon *tcon)
85 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
86 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
89 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
95 smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
96 const struct cifs_tcon *tcon)
98 shdr->ProtocolId = SMB2_PROTO_NUMBER;
99 shdr->StructureSize = cpu_to_le16(64);
100 shdr->Command = smb2_cmd;
101 if (tcon && tcon->ses && tcon->ses->server) {
102 struct TCP_Server_Info *server = tcon->ses->server;
104 spin_lock(&server->req_lock);
105 /* Request up to 2 credits but don't go over the limit. */
106 if (server->credits >= server->max_credits)
107 shdr->CreditRequest = cpu_to_le16(0);
109 shdr->CreditRequest = cpu_to_le16(
110 min_t(int, server->max_credits -
111 server->credits, 2));
112 spin_unlock(&server->req_lock);
114 shdr->CreditRequest = cpu_to_le16(2);
116 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
121 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
122 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
123 if ((tcon->ses) && (tcon->ses->server) &&
124 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
125 shdr->CreditCharge = cpu_to_le16(1);
126 /* else CreditCharge MBZ */
128 shdr->TreeId = tcon->tid;
129 /* Uid is not converted */
131 shdr->SessionId = tcon->ses->Suid;
134 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
135 * to pass the path on the Open SMB prefixed by \\server\share.
136 * Not sure when we would need to do the augmented path (if ever) and
137 * setting this flag breaks the SMB2 open operation since it is
138 * illegal to send an empty path name (without \\server\share prefix)
139 * when the DFS flag is set in the SMB open header. We could
140 * consider setting the flag on all operations other than open
141 * but it is safer to net set it for now.
143 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
144 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
146 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
147 !encryption_required(tcon))
148 shdr->Flags |= SMB2_FLAGS_SIGNED;
154 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
157 struct nls_table *nls_codepage;
158 struct cifs_ses *ses;
159 struct TCP_Server_Info *server;
162 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
163 * check for tcp and smb session status done differently
164 * for those three - in the calling routine.
169 if (smb2_command == SMB2_TREE_CONNECT)
172 if (tcon->tidStatus == CifsExiting) {
174 * only tree disconnect, open, and write,
175 * (and ulogoff which does not have tcon)
176 * are allowed as we start force umount.
178 if ((smb2_command != SMB2_WRITE) &&
179 (smb2_command != SMB2_CREATE) &&
180 (smb2_command != SMB2_TREE_DISCONNECT)) {
181 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
186 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
187 (!tcon->ses->server))
191 server = ses->server;
194 * Give demultiplex thread up to 10 seconds to reconnect, should be
195 * greater than cifs socket timeout which is 7 seconds
197 while (server->tcpStatus == CifsNeedReconnect) {
199 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
200 * here since they are implicitly done when session drops.
202 switch (smb2_command) {
204 * BB Should we keep oplock break and add flush to exceptions?
206 case SMB2_TREE_DISCONNECT:
209 case SMB2_OPLOCK_BREAK:
213 wait_event_interruptible_timeout(server->response_q,
214 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
216 /* are we still trying to reconnect? */
217 if (server->tcpStatus != CifsNeedReconnect)
221 * on "soft" mounts we wait once. Hard mounts keep
222 * retrying until process is killed or server comes
226 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
231 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
234 nls_codepage = load_nls_default();
237 * need to prevent multiple threads trying to simultaneously reconnect
238 * the same SMB session
240 mutex_lock(&tcon->ses->session_mutex);
241 rc = cifs_negotiate_protocol(0, tcon->ses);
242 if (!rc && tcon->ses->need_reconnect)
243 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
245 if (rc || !tcon->need_reconnect) {
246 mutex_unlock(&tcon->ses->session_mutex);
250 cifs_mark_open_files_invalid(tcon);
251 if (tcon->use_persistent)
252 tcon->need_reopen_files = true;
254 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
255 mutex_unlock(&tcon->ses->session_mutex);
257 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
261 if (smb2_command != SMB2_INTERNAL_CMD)
262 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
264 atomic_inc(&tconInfoReconnectCount);
267 * Check if handle based operation so we know whether we can continue
268 * or not without returning to caller to reset file handle.
271 * BB Is flush done by server on drop of tcp session? Should we special
272 * case it and skip above?
274 switch (smb2_command) {
280 case SMB2_QUERY_DIRECTORY:
281 case SMB2_CHANGE_NOTIFY:
282 case SMB2_QUERY_INFO:
286 unload_nls(nls_codepage);
291 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
292 unsigned int *total_len)
294 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
295 /* lookup word count ie StructureSize from table */
296 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
299 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
300 * largest operations (Create)
304 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
305 spdu->StructureSize2 = cpu_to_le16(parmsize);
307 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
310 /* init request without RFC1001 length at the beginning */
312 smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
313 void **request_buf, unsigned int *total_len)
316 struct smb2_sync_hdr *shdr;
318 rc = smb2_reconnect(smb2_command, tcon);
322 /* BB eventually switch this to SMB2 specific small buf size */
323 *request_buf = cifs_small_buf_get();
324 if (*request_buf == NULL) {
325 /* BB should we add a retry in here if not a writepage? */
329 shdr = (struct smb2_sync_hdr *)(*request_buf);
331 fill_small_buf(smb2_command, tcon, shdr, total_len);
334 #ifdef CONFIG_CIFS_STATS2
335 uint16_t com_code = le16_to_cpu(smb2_command);
337 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
339 cifs_stats_inc(&tcon->num_smbs_sent);
346 * Allocate and return pointer to an SMB request hdr, and set basic
347 * SMB information in the SMB header. If the return code is zero, this
348 * function must have filled in request_buf pointer. The returned buffer
349 * has RFC1001 length at the beginning.
352 small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
356 unsigned int total_len;
357 struct smb2_pdu *pdu;
359 rc = smb2_reconnect(smb2_command, tcon);
363 /* BB eventually switch this to SMB2 specific small buf size */
364 *request_buf = cifs_small_buf_get();
365 if (*request_buf == NULL) {
366 /* BB should we add a retry in here if not a writepage? */
370 pdu = (struct smb2_pdu *)(*request_buf);
372 fill_small_buf(smb2_command, tcon, get_sync_hdr(pdu), &total_len);
374 /* Note this is only network field converted to big endian */
375 pdu->hdr.smb2_buf_length = cpu_to_be32(total_len);
378 #ifdef CONFIG_CIFS_STATS2
379 uint16_t com_code = le16_to_cpu(smb2_command);
380 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
382 cifs_stats_inc(&tcon->num_smbs_sent);
388 #ifdef CONFIG_CIFS_SMB311
389 /* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
390 #define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
393 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
394 #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
397 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
399 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
400 pneg_ctxt->DataLength = cpu_to_le16(38);
401 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
402 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
403 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
404 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
408 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
410 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
411 pneg_ctxt->DataLength = cpu_to_le16(6);
412 pneg_ctxt->CipherCount = cpu_to_le16(2);
413 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
414 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
418 assemble_neg_contexts(struct smb2_negotiate_req *req)
421 /* +4 is to account for the RFC1001 len field */
422 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
424 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
425 /* Add 2 to size to round to 8 byte boundary */
426 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
427 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
428 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
429 req->NegotiateContextCount = cpu_to_le16(2);
430 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2
431 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
434 static void assemble_neg_contexts(struct smb2_negotiate_req *req)
442 * SMB2 Worker functions follow:
444 * The general structure of the worker functions is:
445 * 1) Call smb2_init (assembles SMB2 header)
446 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
447 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
448 * 4) Decode SMB2 command specific fields in the fixed length area
449 * 5) Decode variable length data area (if any for this SMB2 command type)
450 * 6) Call free smb buffer
456 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
458 struct smb2_negotiate_req *req;
459 struct smb2_negotiate_rsp *rsp;
464 struct TCP_Server_Info *server = ses->server;
465 int blob_offset, blob_length;
467 int flags = CIFS_NEG_OP;
469 cifs_dbg(FYI, "Negotiate protocol\n");
472 WARN(1, "%s: server is NULL!\n", __func__);
476 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
480 req->hdr.sync_hdr.SessionId = 0;
482 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
484 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
485 inc_rfc1001_len(req, 2);
487 /* only one of SMB2 signing flags may be set in SMB2 request */
489 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
490 else if (global_secflags & CIFSSEC_MAY_SIGN)
491 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
493 req->SecurityMode = 0;
495 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
497 /* ClientGUID must be zero for SMB2.02 dialect */
498 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
499 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
501 memcpy(req->ClientGUID, server->client_guid,
502 SMB2_CLIENT_GUID_SIZE);
503 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
504 assemble_neg_contexts(req);
506 iov[0].iov_base = (char *)req;
507 /* 4 for rfc1002 length field */
508 iov[0].iov_len = get_rfc1002_length(req) + 4;
510 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
511 cifs_small_buf_release(req);
512 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
514 * No tcon so can't do
515 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
520 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
522 /* BB we may eventually want to match the negotiated vs. requested
523 dialect, even though we are only requesting one at a time */
524 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
525 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
526 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
527 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
528 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
529 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
530 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
531 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
532 #ifdef CONFIG_CIFS_SMB311
533 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
534 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
537 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
538 le16_to_cpu(rsp->DialectRevision));
542 server->dialect = le16_to_cpu(rsp->DialectRevision);
544 /* SMB2 only has an extended negflavor */
545 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
546 /* set it to the maximum buffer size value we can send with 1 credit */
547 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
548 SMB2_MAX_BUFFER_SIZE);
549 server->max_read = le32_to_cpu(rsp->MaxReadSize);
550 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
551 /* BB Do we need to validate the SecurityMode? */
552 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
553 server->capabilities = le32_to_cpu(rsp->Capabilities);
555 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
557 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
560 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
562 * ses->sectype = RawNTLMSSP;
563 * but for time being this is our only auth choice so doesn't matter.
564 * We just found a server which sets blob length to zero expecting raw.
566 if (blob_length == 0) {
567 cifs_dbg(FYI, "missing security blob on negprot\n");
568 server->sec_ntlmssp = true;
571 rc = cifs_enable_signing(server, ses->sign);
575 rc = decode_negTokenInit(security_blob, blob_length, server);
582 free_rsp_buf(resp_buftype, rsp);
586 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
589 struct validate_negotiate_info_req vneg_inbuf;
590 struct validate_negotiate_info_rsp *pneg_rsp;
593 cifs_dbg(FYI, "validate negotiate\n");
596 * validation ioctl must be signed, so no point sending this if we
597 * can not sign it. We could eventually change this to selectively
598 * sign just this, the first and only signed request on a connection.
599 * This is good enough for now since a user who wants better security
600 * would also enable signing on the mount. Having validation of
601 * negotiate info for signed connections helps reduce attack vectors
603 if (tcon->ses->server->sign == false)
604 return 0; /* validation requires signing */
606 vneg_inbuf.Capabilities =
607 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
608 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
609 SMB2_CLIENT_GUID_SIZE);
612 vneg_inbuf.SecurityMode =
613 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
614 else if (global_secflags & CIFSSEC_MAY_SIGN)
615 vneg_inbuf.SecurityMode =
616 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
618 vneg_inbuf.SecurityMode = 0;
620 vneg_inbuf.DialectCount = cpu_to_le16(1);
621 vneg_inbuf.Dialects[0] =
622 cpu_to_le16(tcon->ses->server->vals->protocol_id);
624 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
625 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
627 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
628 (char **)&pneg_rsp, &rsplen);
631 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
635 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
636 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
639 /* relax check since Mac returns max bufsize allowed on ioctl */
640 if (rsplen > CIFSMaxBufSize)
644 /* check validate negotiate info response matches what we got earlier */
645 if (pneg_rsp->Dialect !=
646 cpu_to_le16(tcon->ses->server->vals->protocol_id))
649 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
652 /* do not validate server guid because not saved at negprot time yet */
654 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
655 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
658 /* validate negotiate successful */
659 cifs_dbg(FYI, "validate negotiate info successful\n");
663 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
668 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
677 if (server->sec_ntlmssp &&
678 (global_secflags & CIFSSEC_MAY_NTLMSSP))
680 if ((server->sec_kerberos || server->sec_mskerberos) &&
681 (global_secflags & CIFSSEC_MAY_KRB5))
689 struct SMB2_sess_data {
691 struct cifs_ses *ses;
692 struct nls_table *nls_cp;
693 void (*func)(struct SMB2_sess_data *);
695 u64 previous_session;
697 /* we will send the SMB in three pieces:
698 * a fixed length beginning part, an optional
699 * SPNEGO blob (which can be zero length), and a
700 * last part which will include the strings
701 * and rest of bcc area. This allows us to avoid
702 * a large buffer 17K allocation
709 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
712 struct cifs_ses *ses = sess_data->ses;
713 struct smb2_sess_setup_req *req;
714 struct TCP_Server_Info *server = ses->server;
716 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
720 /* First session, not a reauthenticate */
721 req->hdr.sync_hdr.SessionId = 0;
723 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
724 req->PreviousSessionId = sess_data->previous_session;
726 req->Flags = 0; /* MBZ */
727 /* to enable echos and oplocks */
728 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(3);
730 /* only one of SMB2 signing flags may be set in SMB2 request */
732 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
733 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
734 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
736 req->SecurityMode = 0;
738 req->Capabilities = 0;
739 req->Channel = 0; /* MBZ */
741 sess_data->iov[0].iov_base = (char *)req;
742 /* 4 for rfc1002 length field and 1 for pad */
743 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
745 * This variable will be used to clear the buffer
746 * allocated above in case of any error in the calling function.
748 sess_data->buf0_type = CIFS_SMALL_BUFFER;
754 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
756 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
757 sess_data->buf0_type = CIFS_NO_BUFFER;
761 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
764 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
765 struct kvec rsp_iov = { NULL, 0 };
767 /* Testing shows that buffer offset must be at location of Buffer[0] */
768 req->SecurityBufferOffset =
769 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
770 1 /* pad */ - 4 /* rfc1001 len */);
771 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
773 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
775 /* BB add code to build os and lm fields */
777 rc = SendReceive2(sess_data->xid, sess_data->ses,
779 &sess_data->buf0_type,
780 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
781 cifs_small_buf_release(sess_data->iov[0].iov_base);
782 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
788 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
791 struct cifs_ses *ses = sess_data->ses;
793 mutex_lock(&ses->server->srv_mutex);
794 if (ses->server->ops->generate_signingkey) {
795 rc = ses->server->ops->generate_signingkey(ses);
798 "SMB3 session key generation failed\n");
799 mutex_unlock(&ses->server->srv_mutex);
803 if (!ses->server->session_estab) {
804 ses->server->sequence_number = 0x2;
805 ses->server->session_estab = true;
807 mutex_unlock(&ses->server->srv_mutex);
809 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
810 spin_lock(&GlobalMid_Lock);
811 ses->status = CifsGood;
812 ses->need_reconnect = false;
813 spin_unlock(&GlobalMid_Lock);
817 #ifdef CONFIG_CIFS_UPCALL
819 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
822 struct cifs_ses *ses = sess_data->ses;
823 struct cifs_spnego_msg *msg;
824 struct key *spnego_key = NULL;
825 struct smb2_sess_setup_rsp *rsp = NULL;
827 rc = SMB2_sess_alloc_buffer(sess_data);
831 spnego_key = cifs_get_spnego_key(ses);
832 if (IS_ERR(spnego_key)) {
833 rc = PTR_ERR(spnego_key);
838 msg = spnego_key->payload.data[0];
840 * check version field to make sure that cifs.upcall is
841 * sending us a response in an expected form
843 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
845 "bad cifs.upcall version. Expected %d got %d",
846 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
848 goto out_put_spnego_key;
851 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
853 if (!ses->auth_key.response) {
855 "Kerberos can't allocate (%u bytes) memory",
858 goto out_put_spnego_key;
860 ses->auth_key.len = msg->sesskey_len;
862 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
863 sess_data->iov[1].iov_len = msg->secblob_len;
865 rc = SMB2_sess_sendreceive(sess_data);
867 goto out_put_spnego_key;
869 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
870 ses->Suid = rsp->hdr.sync_hdr.SessionId;
872 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
874 rc = SMB2_sess_establish_session(sess_data);
876 key_invalidate(spnego_key);
879 sess_data->result = rc;
880 sess_data->func = NULL;
881 SMB2_sess_free_buffer(sess_data);
885 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
887 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
888 sess_data->result = -EOPNOTSUPP;
889 sess_data->func = NULL;
894 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
897 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
900 struct cifs_ses *ses = sess_data->ses;
901 struct smb2_sess_setup_rsp *rsp = NULL;
902 char *ntlmssp_blob = NULL;
903 bool use_spnego = false; /* else use raw ntlmssp */
907 * If memory allocation is successful, caller of this function
910 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
915 ses->ntlmssp->sesskey_per_smbsess = true;
917 rc = SMB2_sess_alloc_buffer(sess_data);
921 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
923 if (ntlmssp_blob == NULL) {
928 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
930 /* BB eventually need to add this */
931 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
935 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
936 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
938 sess_data->iov[1].iov_base = ntlmssp_blob;
939 sess_data->iov[1].iov_len = blob_length;
941 rc = SMB2_sess_sendreceive(sess_data);
942 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
944 /* If true, rc here is expected and not an error */
945 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
946 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
952 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
953 le16_to_cpu(rsp->SecurityBufferOffset)) {
954 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
955 le16_to_cpu(rsp->SecurityBufferOffset));
959 rc = decode_ntlmssp_challenge(rsp->Buffer,
960 le16_to_cpu(rsp->SecurityBufferLength), ses);
964 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
967 ses->Suid = rsp->hdr.sync_hdr.SessionId;
968 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
972 SMB2_sess_free_buffer(sess_data);
974 sess_data->result = 0;
975 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
981 sess_data->result = rc;
982 sess_data->func = NULL;
986 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
989 struct cifs_ses *ses = sess_data->ses;
990 struct smb2_sess_setup_req *req;
991 struct smb2_sess_setup_rsp *rsp = NULL;
992 unsigned char *ntlmssp_blob = NULL;
993 bool use_spnego = false; /* else use raw ntlmssp */
996 rc = SMB2_sess_alloc_buffer(sess_data);
1000 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1001 req->hdr.sync_hdr.SessionId = ses->Suid;
1003 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1006 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1011 /* BB eventually need to add this */
1012 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1016 sess_data->iov[1].iov_base = ntlmssp_blob;
1017 sess_data->iov[1].iov_len = blob_length;
1019 rc = SMB2_sess_sendreceive(sess_data);
1023 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1025 ses->Suid = rsp->hdr.sync_hdr.SessionId;
1026 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1028 rc = SMB2_sess_establish_session(sess_data);
1030 kfree(ntlmssp_blob);
1031 SMB2_sess_free_buffer(sess_data);
1032 kfree(ses->ntlmssp);
1033 ses->ntlmssp = NULL;
1034 sess_data->result = rc;
1035 sess_data->func = NULL;
1039 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1043 type = smb2_select_sectype(ses->server, ses->sectype);
1044 cifs_dbg(FYI, "sess setup type %d\n", type);
1045 if (type == Unspecified) {
1047 "Unable to select appropriate authentication method!");
1053 sess_data->func = SMB2_auth_kerberos;
1056 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1059 cifs_dbg(VFS, "secType %d not supported!\n", type);
1067 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1068 const struct nls_table *nls_cp)
1071 struct TCP_Server_Info *server = ses->server;
1072 struct SMB2_sess_data *sess_data;
1074 cifs_dbg(FYI, "Session Setup\n");
1077 WARN(1, "%s: server is NULL!\n", __func__);
1081 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1085 rc = SMB2_select_sec(ses, sess_data);
1088 sess_data->xid = xid;
1089 sess_data->ses = ses;
1090 sess_data->buf0_type = CIFS_NO_BUFFER;
1091 sess_data->nls_cp = (struct nls_table *) nls_cp;
1093 while (sess_data->func)
1094 sess_data->func(sess_data);
1096 rc = sess_data->result;
1103 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1105 struct smb2_logoff_req *req; /* response is also trivial struct */
1107 struct TCP_Server_Info *server;
1110 cifs_dbg(FYI, "disconnect session %p\n", ses);
1112 if (ses && (ses->server))
1113 server = ses->server;
1117 /* no need to send SMB logoff if uid already closed due to reconnect */
1118 if (ses->need_reconnect)
1119 goto smb2_session_already_dead;
1121 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1125 /* since no tcon, smb2_init can not do this, so do here */
1126 req->hdr.sync_hdr.SessionId = ses->Suid;
1128 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1129 flags |= CIFS_TRANSFORM_REQ;
1130 else if (server->sign)
1131 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1133 rc = SendReceiveNoRsp(xid, ses, (char *) req, flags);
1134 cifs_small_buf_release(req);
1136 * No tcon so can't do
1137 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1140 smb2_session_already_dead:
1144 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1146 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1149 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1151 /* These are similar values to what Windows uses */
1152 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1154 tcon->max_chunks = 256;
1155 tcon->max_bytes_chunk = 1048576;
1156 tcon->max_bytes_copy = 16777216;
1160 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1161 struct cifs_tcon *tcon, const struct nls_table *cp)
1163 struct smb2_tree_connect_req *req;
1164 struct smb2_tree_connect_rsp *rsp = NULL;
1166 struct kvec rsp_iov;
1170 struct TCP_Server_Info *server;
1171 __le16 *unc_path = NULL;
1174 cifs_dbg(FYI, "TCON\n");
1176 if ((ses->server) && tree)
1177 server = ses->server;
1181 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1182 if (unc_path == NULL)
1185 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1187 if (unc_path_len < 2) {
1192 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1196 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1203 if ((ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA))
1204 flags |= CIFS_TRANSFORM_REQ;
1206 /* since no tcon, smb2_init can not do this, so do here */
1207 req->hdr.sync_hdr.SessionId = ses->Suid;
1208 if (ses->server->sign)
1209 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1210 } else if (encryption_required(tcon))
1211 flags |= CIFS_TRANSFORM_REQ;
1213 iov[0].iov_base = (char *)req;
1214 /* 4 for rfc1002 length field and 1 for pad */
1215 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1217 /* Testing shows that buffer offset must be at location of Buffer[0] */
1218 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1219 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1220 req->PathLength = cpu_to_le16(unc_path_len - 2);
1221 iov[1].iov_base = unc_path;
1222 iov[1].iov_len = unc_path_len;
1224 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1226 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
1227 cifs_small_buf_release(req);
1228 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1232 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1233 tcon->need_reconnect = true;
1235 goto tcon_error_exit;
1239 ses->ipc_tid = rsp->hdr.sync_hdr.TreeId;
1243 switch (rsp->ShareType) {
1244 case SMB2_SHARE_TYPE_DISK:
1245 cifs_dbg(FYI, "connection to disk share\n");
1247 case SMB2_SHARE_TYPE_PIPE:
1249 cifs_dbg(FYI, "connection to pipe share\n");
1251 case SMB2_SHARE_TYPE_PRINT:
1253 cifs_dbg(FYI, "connection to printer\n");
1256 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1258 goto tcon_error_exit;
1261 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1262 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1263 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1264 tcon->tidStatus = CifsGood;
1265 tcon->need_reconnect = false;
1266 tcon->tid = rsp->hdr.sync_hdr.TreeId;
1267 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1269 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1270 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1271 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1274 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1275 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1277 init_copy_chunk_defaults(tcon);
1278 if (tcon->ses->server->ops->validate_negotiate)
1279 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1281 free_rsp_buf(resp_buftype, rsp);
1286 if (rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1287 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1293 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1295 struct smb2_tree_disconnect_req *req; /* response is trivial */
1297 struct TCP_Server_Info *server;
1298 struct cifs_ses *ses = tcon->ses;
1301 cifs_dbg(FYI, "Tree Disconnect\n");
1303 if (ses && (ses->server))
1304 server = ses->server;
1308 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1311 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1315 if (encryption_required(tcon))
1316 flags |= CIFS_TRANSFORM_REQ;
1318 rc = SendReceiveNoRsp(xid, ses, (char *)req, flags);
1319 cifs_small_buf_release(req);
1321 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1327 static struct create_durable *
1328 create_durable_buf(void)
1330 struct create_durable *buf;
1332 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1336 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1337 (struct create_durable, Data));
1338 buf->ccontext.DataLength = cpu_to_le32(16);
1339 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1340 (struct create_durable, Name));
1341 buf->ccontext.NameLength = cpu_to_le16(4);
1342 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1350 static struct create_durable *
1351 create_reconnect_durable_buf(struct cifs_fid *fid)
1353 struct create_durable *buf;
1355 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1359 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1360 (struct create_durable, Data));
1361 buf->ccontext.DataLength = cpu_to_le32(16);
1362 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1363 (struct create_durable, Name));
1364 buf->ccontext.NameLength = cpu_to_le16(4);
1365 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1366 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1367 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1376 parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1377 unsigned int *epoch)
1380 struct create_context *cc;
1382 unsigned int remaining;
1385 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
1386 remaining = le32_to_cpu(rsp->CreateContextsLength);
1387 cc = (struct create_context *)data_offset;
1388 while (remaining >= sizeof(struct create_context)) {
1389 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1390 if (le16_to_cpu(cc->NameLength) == 4 &&
1391 strncmp(name, "RqLs", 4) == 0)
1392 return server->ops->parse_lease_buf(cc, epoch);
1394 next = le32_to_cpu(cc->Next);
1398 cc = (struct create_context *)((char *)cc + next);
1405 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1406 unsigned int *num_iovec, __u8 *oplock)
1408 struct smb2_create_req *req = iov[0].iov_base;
1409 unsigned int num = *num_iovec;
1411 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
1412 if (iov[num].iov_base == NULL)
1414 iov[num].iov_len = server->vals->create_lease_size;
1415 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1416 if (!req->CreateContextsOffset)
1417 req->CreateContextsOffset = cpu_to_le32(
1418 sizeof(struct smb2_create_req) - 4 +
1419 iov[num - 1].iov_len);
1420 le32_add_cpu(&req->CreateContextsLength,
1421 server->vals->create_lease_size);
1422 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
1423 *num_iovec = num + 1;
1427 static struct create_durable_v2 *
1428 create_durable_v2_buf(struct cifs_fid *pfid)
1430 struct create_durable_v2 *buf;
1432 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1436 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1437 (struct create_durable_v2, dcontext));
1438 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1439 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1440 (struct create_durable_v2, Name));
1441 buf->ccontext.NameLength = cpu_to_le16(4);
1443 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1444 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1445 generate_random_uuid(buf->dcontext.CreateGuid);
1446 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1448 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1456 static struct create_durable_handle_reconnect_v2 *
1457 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1459 struct create_durable_handle_reconnect_v2 *buf;
1461 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1466 buf->ccontext.DataOffset =
1467 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1469 buf->ccontext.DataLength =
1470 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1471 buf->ccontext.NameOffset =
1472 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1474 buf->ccontext.NameLength = cpu_to_le16(4);
1476 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1477 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1478 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1479 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1481 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1490 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1491 struct cifs_open_parms *oparms)
1493 struct smb2_create_req *req = iov[0].iov_base;
1494 unsigned int num = *num_iovec;
1496 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1497 if (iov[num].iov_base == NULL)
1499 iov[num].iov_len = sizeof(struct create_durable_v2);
1500 if (!req->CreateContextsOffset)
1501 req->CreateContextsOffset =
1502 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1504 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1505 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1506 *num_iovec = num + 1;
1511 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1512 struct cifs_open_parms *oparms)
1514 struct smb2_create_req *req = iov[0].iov_base;
1515 unsigned int num = *num_iovec;
1517 /* indicate that we don't need to relock the file */
1518 oparms->reconnect = false;
1520 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1521 if (iov[num].iov_base == NULL)
1523 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1524 if (!req->CreateContextsOffset)
1525 req->CreateContextsOffset =
1526 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1528 le32_add_cpu(&req->CreateContextsLength,
1529 sizeof(struct create_durable_handle_reconnect_v2));
1530 inc_rfc1001_len(&req->hdr,
1531 sizeof(struct create_durable_handle_reconnect_v2));
1532 *num_iovec = num + 1;
1537 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1538 struct cifs_open_parms *oparms, bool use_persistent)
1540 struct smb2_create_req *req = iov[0].iov_base;
1541 unsigned int num = *num_iovec;
1543 if (use_persistent) {
1544 if (oparms->reconnect)
1545 return add_durable_reconnect_v2_context(iov, num_iovec,
1548 return add_durable_v2_context(iov, num_iovec, oparms);
1551 if (oparms->reconnect) {
1552 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1553 /* indicate that we don't need to relock the file */
1554 oparms->reconnect = false;
1556 iov[num].iov_base = create_durable_buf();
1557 if (iov[num].iov_base == NULL)
1559 iov[num].iov_len = sizeof(struct create_durable);
1560 if (!req->CreateContextsOffset)
1561 req->CreateContextsOffset =
1562 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1564 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1565 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1566 *num_iovec = num + 1;
1571 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1572 const char *treename, const __le16 *path)
1574 int treename_len, path_len;
1575 struct nls_table *cp;
1576 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1581 treename_len = strlen(treename);
1582 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1588 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1591 * make room for one path separator between the treename and
1594 *out_len = treename_len + 1 + path_len;
1597 * final path needs to be null-terminated UTF16 with a
1601 *out_size = roundup((*out_len+1)*2, 8);
1602 *out_path = kzalloc(*out_size, GFP_KERNEL);
1606 cp = load_nls_default();
1607 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1608 UniStrcat(*out_path, sep);
1609 UniStrcat(*out_path, path);
1616 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
1617 __u8 *oplock, struct smb2_file_all_info *buf,
1618 struct smb2_err_rsp **err_buf)
1620 struct smb2_create_req *req;
1621 struct smb2_create_rsp *rsp;
1622 struct TCP_Server_Info *server;
1623 struct cifs_tcon *tcon = oparms->tcon;
1624 struct cifs_ses *ses = tcon->ses;
1626 struct kvec rsp_iov;
1629 __le16 *copy_path = NULL;
1632 unsigned int n_iov = 2;
1633 __u32 file_attributes = 0;
1634 char *dhc_buf = NULL, *lc_buf = NULL;
1637 cifs_dbg(FYI, "create/open\n");
1639 if (ses && (ses->server))
1640 server = ses->server;
1644 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1648 if (encryption_required(tcon))
1649 flags |= CIFS_TRANSFORM_REQ;
1651 if (oparms->create_options & CREATE_OPTION_READONLY)
1652 file_attributes |= ATTR_READONLY;
1653 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1654 file_attributes |= ATTR_SYSTEM;
1656 req->ImpersonationLevel = IL_IMPERSONATION;
1657 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
1658 /* File attributes ignored on open (used in create though) */
1659 req->FileAttributes = cpu_to_le32(file_attributes);
1660 req->ShareAccess = FILE_SHARE_ALL_LE;
1661 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1662 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
1664 iov[0].iov_base = (char *)req;
1665 /* 4 for rfc1002 length field */
1666 iov[0].iov_len = get_rfc1002_length(req) + 4;
1667 /* -1 since last byte is buf[0] which is sent below (path) */
1670 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1672 /* [MS-SMB2] 2.2.13 NameOffset:
1673 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1674 * the SMB2 header, the file name includes a prefix that will
1675 * be processed during DFS name normalization as specified in
1676 * section 3.3.5.9. Otherwise, the file name is relative to
1677 * the share that is identified by the TreeId in the SMB2
1680 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1683 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
1684 rc = alloc_path_with_tree_prefix(©_path, ©_size,
1686 tcon->treeName, path);
1689 req->NameLength = cpu_to_le16(name_len * 2);
1690 uni_path_len = copy_size;
1693 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1694 /* MUST set path len (NameLength) to 0 opening root of share */
1695 req->NameLength = cpu_to_le16(uni_path_len - 2);
1696 if (uni_path_len % 8 != 0) {
1697 copy_size = roundup(uni_path_len, 8);
1698 copy_path = kzalloc(copy_size, GFP_KERNEL);
1701 memcpy((char *)copy_path, (const char *)path,
1703 uni_path_len = copy_size;
1708 iov[1].iov_len = uni_path_len;
1709 iov[1].iov_base = path;
1710 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1711 inc_rfc1001_len(req, uni_path_len - 1);
1713 if (!server->oplocks)
1714 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1716 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1717 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1718 req->RequestedOplockLevel = *oplock;
1720 rc = add_lease_context(server, iov, &n_iov, oplock);
1722 cifs_small_buf_release(req);
1726 lc_buf = iov[n_iov-1].iov_base;
1729 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1730 /* need to set Next field of lease context if we request it */
1731 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
1732 struct create_context *ccontext =
1733 (struct create_context *)iov[n_iov-1].iov_base;
1735 cpu_to_le32(server->vals->create_lease_size);
1738 rc = add_durable_context(iov, &n_iov, oparms,
1739 tcon->use_persistent);
1741 cifs_small_buf_release(req);
1746 dhc_buf = iov[n_iov-1].iov_base;
1749 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
1750 cifs_small_buf_release(req);
1751 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
1754 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
1756 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1761 oparms->fid->persistent_fid = rsp->PersistentFileId;
1762 oparms->fid->volatile_fid = rsp->VolatileFileId;
1765 memcpy(buf, &rsp->CreationTime, 32);
1766 buf->AllocationSize = rsp->AllocationSize;
1767 buf->EndOfFile = rsp->EndofFile;
1768 buf->Attributes = rsp->FileAttributes;
1769 buf->NumberOfLinks = cpu_to_le32(1);
1770 buf->DeletePending = 0;
1773 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
1774 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
1776 *oplock = rsp->OplockLevel;
1781 free_rsp_buf(resp_buftype, rsp);
1786 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1789 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1790 u64 volatile_fid, u32 opcode, bool is_fsctl, bool use_ipc,
1791 char *in_data, u32 indatalen,
1792 char **out_data, u32 *plen /* returned data len */)
1794 struct smb2_ioctl_req *req;
1795 struct smb2_ioctl_rsp *rsp;
1796 struct smb2_sync_hdr *shdr;
1797 struct TCP_Server_Info *server;
1798 struct cifs_ses *ses;
1800 struct kvec rsp_iov;
1806 cifs_dbg(FYI, "SMB2 IOCTL\n");
1808 if (out_data != NULL)
1811 /* zero out returned data len, in case of error */
1820 if (ses && (ses->server))
1821 server = ses->server;
1825 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1830 if (ses->ipc_tid == 0) {
1831 cifs_small_buf_release(req);
1835 cifs_dbg(FYI, "replacing tid 0x%x with IPC tid 0x%x\n",
1836 req->hdr.sync_hdr.TreeId, ses->ipc_tid);
1837 req->hdr.sync_hdr.TreeId = ses->ipc_tid;
1839 if (encryption_required(tcon))
1840 flags |= CIFS_TRANSFORM_REQ;
1842 req->CtlCode = cpu_to_le32(opcode);
1843 req->PersistentFileId = persistent_fid;
1844 req->VolatileFileId = volatile_fid;
1847 req->InputCount = cpu_to_le32(indatalen);
1848 /* do not set InputOffset if no input data */
1850 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1851 iov[1].iov_base = in_data;
1852 iov[1].iov_len = indatalen;
1857 req->OutputOffset = 0;
1858 req->OutputCount = 0; /* MBZ */
1861 * Could increase MaxOutputResponse, but that would require more
1862 * than one credit. Windows typically sets this smaller, but for some
1863 * ioctls it may be useful to allow server to send more. No point
1864 * limiting what the server can send as long as fits in one credit
1865 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1866 * (by default, note that it can be overridden to make max larger)
1867 * in responses (except for read responses which can be bigger.
1868 * We may want to bump this limit up
1870 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
1873 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1877 iov[0].iov_base = (char *)req;
1880 * If no input data, the size of ioctl struct in
1881 * protocol spec still includes a 1 byte data buffer,
1882 * but if input data passed to ioctl, we do not
1883 * want to double count this, so we do not send
1884 * the dummy one byte of data in iovec[0] if sending
1885 * input data (in iovec[1]). We also must add 4 bytes
1886 * in first iovec to allow for rfc1002 length field.
1890 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1891 inc_rfc1001_len(req, indatalen - 1);
1893 iov[0].iov_len = get_rfc1002_length(req) + 4;
1896 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
1897 cifs_small_buf_release(req);
1898 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
1900 if ((rc != 0) && (rc != -EINVAL)) {
1901 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1903 } else if (rc == -EINVAL) {
1904 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1905 (opcode != FSCTL_SRV_COPYCHUNK)) {
1906 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1911 /* check if caller wants to look at return data or just return rc */
1912 if ((plen == NULL) || (out_data == NULL))
1915 *plen = le32_to_cpu(rsp->OutputCount);
1917 /* We check for obvious errors in the output buffer length and offset */
1919 goto ioctl_exit; /* server returned no data */
1920 else if (*plen > 0xFF00) {
1921 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1927 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1928 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1929 le32_to_cpu(rsp->OutputOffset));
1935 *out_data = kmalloc(*plen, GFP_KERNEL);
1936 if (*out_data == NULL) {
1941 shdr = get_sync_hdr(rsp);
1942 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
1944 free_rsp_buf(resp_buftype, rsp);
1949 * Individual callers to ioctl worker function follow
1953 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1954 u64 persistent_fid, u64 volatile_fid)
1957 struct compress_ioctl fsctl_input;
1958 char *ret_data = NULL;
1960 fsctl_input.CompressionState =
1961 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
1963 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1964 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1965 false /* use_ipc */,
1966 (char *)&fsctl_input /* data input */,
1967 2 /* in data len */, &ret_data /* out data */, NULL);
1969 cifs_dbg(FYI, "set compression rc %d\n", rc);
1975 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1976 u64 persistent_fid, u64 volatile_fid)
1978 struct smb2_close_req *req;
1979 struct smb2_close_rsp *rsp;
1980 struct TCP_Server_Info *server;
1981 struct cifs_ses *ses = tcon->ses;
1983 struct kvec rsp_iov;
1988 cifs_dbg(FYI, "Close\n");
1990 if (ses && (ses->server))
1991 server = ses->server;
1995 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1999 if (encryption_required(tcon))
2000 flags |= CIFS_TRANSFORM_REQ;
2002 req->PersistentFileId = persistent_fid;
2003 req->VolatileFileId = volatile_fid;
2005 iov[0].iov_base = (char *)req;
2006 /* 4 for rfc1002 length field */
2007 iov[0].iov_len = get_rfc1002_length(req) + 4;
2009 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2010 cifs_small_buf_release(req);
2011 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2014 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
2018 /* BB FIXME - decode close response, update inode for caching */
2021 free_rsp_buf(resp_buftype, rsp);
2026 validate_buf(unsigned int offset, unsigned int buffer_length,
2027 struct smb2_hdr *hdr, unsigned int min_buf_size)
2030 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2031 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2032 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2033 char *end_of_buf = begin_of_buf + buffer_length;
2036 if (buffer_length < min_buf_size) {
2037 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2038 buffer_length, min_buf_size);
2042 /* check if beyond RFC1001 maximum length */
2043 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
2044 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2045 buffer_length, smb_len);
2049 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
2050 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
2058 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2059 * Caller must free buffer.
2062 validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2063 struct smb2_hdr *hdr, unsigned int minbufsize,
2067 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2073 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2077 memcpy(data, begin_of_buf, buffer_length);
2083 query_info(const unsigned int xid, struct cifs_tcon *tcon,
2084 u64 persistent_fid, u64 volatile_fid, u8 info_class,
2085 size_t output_len, size_t min_len, void *data)
2087 struct smb2_query_info_req *req;
2088 struct smb2_query_info_rsp *rsp = NULL;
2090 struct kvec rsp_iov;
2093 struct TCP_Server_Info *server;
2094 struct cifs_ses *ses = tcon->ses;
2097 cifs_dbg(FYI, "Query Info\n");
2099 if (ses && (ses->server))
2100 server = ses->server;
2104 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2108 if (encryption_required(tcon))
2109 flags |= CIFS_TRANSFORM_REQ;
2111 req->InfoType = SMB2_O_INFO_FILE;
2112 req->FileInfoClass = info_class;
2113 req->PersistentFileId = persistent_fid;
2114 req->VolatileFileId = volatile_fid;
2115 /* 4 for rfc1002 length field and 1 for Buffer */
2116 req->InputBufferOffset =
2117 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2118 req->OutputBufferLength = cpu_to_le32(output_len);
2120 iov[0].iov_base = (char *)req;
2121 /* 4 for rfc1002 length field */
2122 iov[0].iov_len = get_rfc1002_length(req) + 4;
2124 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2125 cifs_small_buf_release(req);
2126 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2129 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2133 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2134 le32_to_cpu(rsp->OutputBufferLength),
2135 &rsp->hdr, min_len, data);
2138 free_rsp_buf(resp_buftype, rsp);
2143 SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2144 u64 persistent_fid, u64 volatile_fid,
2145 struct smb2_file_all_info *data)
2147 return query_info(xid, tcon, persistent_fid, volatile_fid,
2148 FILE_ALL_INFORMATION,
2149 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
2150 sizeof(struct smb2_file_all_info), data);
2154 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2155 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2157 return query_info(xid, tcon, persistent_fid, volatile_fid,
2158 FILE_INTERNAL_INFORMATION,
2159 sizeof(struct smb2_file_internal_info),
2160 sizeof(struct smb2_file_internal_info), uniqueid);
2164 * This is a no-op for now. We're not really interested in the reply, but
2165 * rather in the fact that the server sent one and that server->lstrp
2168 * FIXME: maybe we should consider checking that the reply matches request?
2171 smb2_echo_callback(struct mid_q_entry *mid)
2173 struct TCP_Server_Info *server = mid->callback_data;
2174 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
2175 unsigned int credits_received = 1;
2177 if (mid->mid_state == MID_RESPONSE_RECEIVED)
2178 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
2180 DeleteMidQEntry(mid);
2181 add_credits(server, credits_received, CIFS_ECHO_OP);
2184 void smb2_reconnect_server(struct work_struct *work)
2186 struct TCP_Server_Info *server = container_of(work,
2187 struct TCP_Server_Info, reconnect.work);
2188 struct cifs_ses *ses;
2189 struct cifs_tcon *tcon, *tcon2;
2190 struct list_head tmp_list;
2191 int tcon_exist = false;
2193 int resched = false;
2196 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2197 mutex_lock(&server->reconnect_mutex);
2199 INIT_LIST_HEAD(&tmp_list);
2200 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2202 spin_lock(&cifs_tcp_ses_lock);
2203 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2204 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2205 if (tcon->need_reconnect || tcon->need_reopen_files) {
2207 list_add_tail(&tcon->rlist, &tmp_list);
2213 * Get the reference to server struct to be sure that the last call of
2214 * cifs_put_tcon() in the loop below won't release the server pointer.
2217 server->srv_count++;
2219 spin_unlock(&cifs_tcp_ses_lock);
2221 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
2222 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2224 cifs_reopen_persistent_handles(tcon);
2227 list_del_init(&tcon->rlist);
2228 cifs_put_tcon(tcon);
2231 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2233 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
2234 mutex_unlock(&server->reconnect_mutex);
2236 /* now we can safely release srv struct */
2238 cifs_put_tcp_session(server, 1);
2242 SMB2_echo(struct TCP_Server_Info *server)
2244 struct smb2_echo_req *req;
2247 struct smb_rqst rqst = { .rq_iov = iov,
2250 cifs_dbg(FYI, "In echo request\n");
2252 if (server->tcpStatus == CifsNeedNegotiate) {
2253 /* No need to send echo on newly established connections */
2254 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2258 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2262 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
2264 /* 4 for rfc1002 length field */
2266 iov[0].iov_base = (char *)req;
2267 iov[1].iov_len = get_rfc1002_length(req);
2268 iov[1].iov_base = (char *)req + 4;
2270 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2271 server, CIFS_ECHO_OP);
2273 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
2275 cifs_small_buf_release(req);
2280 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2283 struct smb2_flush_req *req;
2284 struct TCP_Server_Info *server;
2285 struct cifs_ses *ses = tcon->ses;
2287 struct kvec rsp_iov;
2292 cifs_dbg(FYI, "Flush\n");
2294 if (ses && (ses->server))
2295 server = ses->server;
2299 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2303 if (encryption_required(tcon))
2304 flags |= CIFS_TRANSFORM_REQ;
2306 req->PersistentFileId = persistent_fid;
2307 req->VolatileFileId = volatile_fid;
2309 iov[0].iov_base = (char *)req;
2310 /* 4 for rfc1002 length field */
2311 iov[0].iov_len = get_rfc1002_length(req) + 4;
2313 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2314 cifs_small_buf_release(req);
2317 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2319 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2324 * To form a chain of read requests, any read requests after the first should
2325 * have the end_of_chain boolean set to true.
2328 smb2_new_read_req(void **buf, unsigned int *total_len,
2329 struct cifs_io_parms *io_parms, unsigned int remaining_bytes,
2333 struct smb2_read_plain_req *req = NULL;
2334 struct smb2_sync_hdr *shdr;
2336 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2340 if (io_parms->tcon->ses->server == NULL)
2341 return -ECONNABORTED;
2343 shdr = &req->sync_hdr;
2344 shdr->ProcessId = cpu_to_le32(io_parms->pid);
2346 req->PersistentFileId = io_parms->persistent_fid;
2347 req->VolatileFileId = io_parms->volatile_fid;
2348 req->ReadChannelInfoOffset = 0; /* reserved */
2349 req->ReadChannelInfoLength = 0; /* reserved */
2350 req->Channel = 0; /* reserved */
2351 req->MinimumCount = 0;
2352 req->Length = cpu_to_le32(io_parms->length);
2353 req->Offset = cpu_to_le64(io_parms->offset);
2355 if (request_type & CHAINED_REQUEST) {
2356 if (!(request_type & END_OF_CHAIN)) {
2357 /* next 8-byte aligned request */
2358 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2359 shdr->NextCommand = cpu_to_le32(*total_len);
2360 } else /* END_OF_CHAIN */
2361 shdr->NextCommand = 0;
2362 if (request_type & RELATED_REQUEST) {
2363 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2365 * Related requests use info from previous read request
2368 shdr->SessionId = 0xFFFFFFFF;
2369 shdr->TreeId = 0xFFFFFFFF;
2370 req->PersistentFileId = 0xFFFFFFFF;
2371 req->VolatileFileId = 0xFFFFFFFF;
2374 if (remaining_bytes > io_parms->length)
2375 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2377 req->RemainingBytes = 0;
2384 smb2_readv_callback(struct mid_q_entry *mid)
2386 struct cifs_readdata *rdata = mid->callback_data;
2387 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2388 struct TCP_Server_Info *server = tcon->ses->server;
2389 struct smb2_sync_hdr *shdr =
2390 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
2391 unsigned int credits_received = 1;
2392 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2394 .rq_pages = rdata->pages,
2395 .rq_npages = rdata->nr_pages,
2396 .rq_pagesz = rdata->pagesz,
2397 .rq_tailsz = rdata->tailsz };
2399 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2400 __func__, mid->mid, mid->mid_state, rdata->result,
2403 switch (mid->mid_state) {
2404 case MID_RESPONSE_RECEIVED:
2405 credits_received = le16_to_cpu(shdr->CreditRequest);
2406 /* result already set, check signature */
2407 if (server->sign && !mid->decrypted) {
2410 rc = smb2_verify_signature(&rqst, server);
2412 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2415 /* FIXME: should this be counted toward the initiating task? */
2416 task_io_account_read(rdata->got_bytes);
2417 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2419 case MID_REQUEST_SUBMITTED:
2420 case MID_RETRY_NEEDED:
2421 rdata->result = -EAGAIN;
2422 if (server->sign && rdata->got_bytes)
2423 /* reset bytes number since we can not check a sign */
2424 rdata->got_bytes = 0;
2425 /* FIXME: should this be counted toward the initiating task? */
2426 task_io_account_read(rdata->got_bytes);
2427 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2430 if (rdata->result != -ENODATA)
2431 rdata->result = -EIO;
2435 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2437 queue_work(cifsiod_wq, &rdata->work);
2438 DeleteMidQEntry(mid);
2439 add_credits(server, credits_received, 0);
2442 /* smb2_async_readv - send an async read, and set up mid to handle result */
2444 smb2_async_readv(struct cifs_readdata *rdata)
2448 struct smb2_sync_hdr *shdr;
2449 struct cifs_io_parms io_parms;
2450 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2452 struct TCP_Server_Info *server;
2453 unsigned int total_len;
2456 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2457 __func__, rdata->offset, rdata->bytes);
2459 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2460 io_parms.offset = rdata->offset;
2461 io_parms.length = rdata->bytes;
2462 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2463 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2464 io_parms.pid = rdata->pid;
2466 server = io_parms.tcon->ses->server;
2468 rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0);
2470 if (rc == -EAGAIN && rdata->credits) {
2471 /* credits was reset by reconnect */
2473 /* reduce in_flight value since we won't send the req */
2474 spin_lock(&server->req_lock);
2475 server->in_flight--;
2476 spin_unlock(&server->req_lock);
2481 if (encryption_required(io_parms.tcon))
2482 flags |= CIFS_TRANSFORM_REQ;
2484 req_len = cpu_to_be32(total_len);
2486 rdata->iov[0].iov_base = &req_len;
2487 rdata->iov[0].iov_len = sizeof(__be32);
2488 rdata->iov[1].iov_base = buf;
2489 rdata->iov[1].iov_len = total_len;
2491 shdr = (struct smb2_sync_hdr *)buf;
2493 if (rdata->credits) {
2494 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2495 SMB2_MAX_BUFFER_SIZE));
2496 shdr->CreditRequest = shdr->CreditCharge;
2497 spin_lock(&server->req_lock);
2498 server->credits += rdata->credits -
2499 le16_to_cpu(shdr->CreditCharge);
2500 spin_unlock(&server->req_lock);
2501 wake_up(&server->request_q);
2502 flags |= CIFS_HAS_CREDITS;
2505 kref_get(&rdata->refcount);
2506 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
2507 cifs_readv_receive, smb2_readv_callback,
2508 smb3_handle_read_data, rdata, flags);
2510 kref_put(&rdata->refcount, cifs_readdata_release);
2511 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2514 cifs_small_buf_release(buf);
2519 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2520 unsigned int *nbytes, char **buf, int *buf_type)
2522 int resp_buftype, rc = -EACCES;
2523 struct smb2_read_plain_req *req = NULL;
2524 struct smb2_read_rsp *rsp = NULL;
2525 struct smb2_sync_hdr *shdr;
2527 struct kvec rsp_iov;
2528 unsigned int total_len;
2530 struct smb_rqst rqst = { .rq_iov = iov,
2532 int flags = CIFS_LOG_ERROR;
2533 struct cifs_ses *ses = io_parms->tcon->ses;
2536 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0);
2540 if (encryption_required(io_parms->tcon))
2541 flags |= CIFS_TRANSFORM_REQ;
2543 req_len = cpu_to_be32(total_len);
2545 iov[0].iov_base = &req_len;
2546 iov[0].iov_len = sizeof(__be32);
2547 iov[1].iov_base = req;
2548 iov[1].iov_len = total_len;
2550 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2551 cifs_small_buf_release(req);
2553 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
2554 shdr = get_sync_hdr(rsp);
2556 if (shdr->Status == STATUS_END_OF_FILE) {
2557 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2562 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2563 cifs_dbg(VFS, "Send error in read = %d\n", rc);
2565 *nbytes = le32_to_cpu(rsp->DataLength);
2566 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2567 (*nbytes > io_parms->length)) {
2568 cifs_dbg(FYI, "bad length %d for count %d\n",
2569 *nbytes, io_parms->length);
2576 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
2577 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2578 } else if (resp_buftype != CIFS_NO_BUFFER) {
2579 *buf = rsp_iov.iov_base;
2580 if (resp_buftype == CIFS_SMALL_BUFFER)
2581 *buf_type = CIFS_SMALL_BUFFER;
2582 else if (resp_buftype == CIFS_LARGE_BUFFER)
2583 *buf_type = CIFS_LARGE_BUFFER;
2589 * Check the mid_state and signature on received buffer (if any), and queue the
2590 * workqueue completion task.
2593 smb2_writev_callback(struct mid_q_entry *mid)
2595 struct cifs_writedata *wdata = mid->callback_data;
2596 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2597 unsigned int written;
2598 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2599 unsigned int credits_received = 1;
2601 switch (mid->mid_state) {
2602 case MID_RESPONSE_RECEIVED:
2603 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
2604 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2605 if (wdata->result != 0)
2608 written = le32_to_cpu(rsp->DataLength);
2610 * Mask off high 16 bits when bytes written as returned
2611 * by the server is greater than bytes requested by the
2612 * client. OS/2 servers are known to set incorrect
2615 if (written > wdata->bytes)
2618 if (written < wdata->bytes)
2619 wdata->result = -ENOSPC;
2621 wdata->bytes = written;
2623 case MID_REQUEST_SUBMITTED:
2624 case MID_RETRY_NEEDED:
2625 wdata->result = -EAGAIN;
2628 wdata->result = -EIO;
2633 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2635 queue_work(cifsiod_wq, &wdata->work);
2636 DeleteMidQEntry(mid);
2637 add_credits(tcon->ses->server, credits_received, 0);
2640 /* smb2_async_writev - send an async write, and set up mid to handle result */
2642 smb2_async_writev(struct cifs_writedata *wdata,
2643 void (*release)(struct kref *kref))
2645 int rc = -EACCES, flags = 0;
2646 struct smb2_write_req *req = NULL;
2647 struct smb2_sync_hdr *shdr;
2648 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2649 struct TCP_Server_Info *server = tcon->ses->server;
2651 struct smb_rqst rqst = { };
2653 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
2655 if (rc == -EAGAIN && wdata->credits) {
2656 /* credits was reset by reconnect */
2658 /* reduce in_flight value since we won't send the req */
2659 spin_lock(&server->req_lock);
2660 server->in_flight--;
2661 spin_unlock(&server->req_lock);
2663 goto async_writev_out;
2666 if (encryption_required(tcon))
2667 flags |= CIFS_TRANSFORM_REQ;
2669 shdr = get_sync_hdr(req);
2670 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
2672 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2673 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2674 req->WriteChannelInfoOffset = 0;
2675 req->WriteChannelInfoLength = 0;
2677 req->Offset = cpu_to_le64(wdata->offset);
2678 /* 4 for rfc1002 length field */
2679 req->DataOffset = cpu_to_le16(
2680 offsetof(struct smb2_write_req, Buffer) - 4);
2681 req->RemainingBytes = 0;
2683 /* 4 for rfc1002 length field and 1 for Buffer */
2685 iov[0].iov_base = req;
2686 iov[1].iov_len = get_rfc1002_length(req) - 1;
2687 iov[1].iov_base = (char *)req + 4;
2691 rqst.rq_pages = wdata->pages;
2692 rqst.rq_npages = wdata->nr_pages;
2693 rqst.rq_pagesz = wdata->pagesz;
2694 rqst.rq_tailsz = wdata->tailsz;
2696 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2697 wdata->offset, wdata->bytes);
2699 req->Length = cpu_to_le32(wdata->bytes);
2701 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2703 if (wdata->credits) {
2704 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2705 SMB2_MAX_BUFFER_SIZE));
2706 shdr->CreditRequest = shdr->CreditCharge;
2707 spin_lock(&server->req_lock);
2708 server->credits += wdata->credits -
2709 le16_to_cpu(shdr->CreditCharge);
2710 spin_unlock(&server->req_lock);
2711 wake_up(&server->request_q);
2712 flags |= CIFS_HAS_CREDITS;
2715 kref_get(&wdata->refcount);
2716 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2720 kref_put(&wdata->refcount, release);
2721 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2725 cifs_small_buf_release(req);
2730 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2731 * The length field from io_parms must be at least 1 and indicates a number of
2732 * elements with data to write that begins with position 1 in iov array. All
2733 * data length is specified by count.
2736 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2737 unsigned int *nbytes, struct kvec *iov, int n_vec)
2740 struct smb2_write_req *req = NULL;
2741 struct smb2_write_rsp *rsp = NULL;
2743 struct kvec rsp_iov;
2751 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2755 if (io_parms->tcon->ses->server == NULL)
2756 return -ECONNABORTED;
2758 if (encryption_required(io_parms->tcon))
2759 flags |= CIFS_TRANSFORM_REQ;
2761 req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
2763 req->PersistentFileId = io_parms->persistent_fid;
2764 req->VolatileFileId = io_parms->volatile_fid;
2765 req->WriteChannelInfoOffset = 0;
2766 req->WriteChannelInfoLength = 0;
2768 req->Length = cpu_to_le32(io_parms->length);
2769 req->Offset = cpu_to_le64(io_parms->offset);
2770 /* 4 for rfc1002 length field */
2771 req->DataOffset = cpu_to_le16(
2772 offsetof(struct smb2_write_req, Buffer) - 4);
2773 req->RemainingBytes = 0;
2775 iov[0].iov_base = (char *)req;
2776 /* 4 for rfc1002 length field and 1 for Buffer */
2777 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2779 /* length of entire message including data to be written */
2780 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2782 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2783 &resp_buftype, flags, &rsp_iov);
2784 cifs_small_buf_release(req);
2785 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
2788 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
2789 cifs_dbg(VFS, "Send error in write = %d\n", rc);
2791 *nbytes = le32_to_cpu(rsp->DataLength);
2793 free_rsp_buf(resp_buftype, rsp);
2798 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2801 unsigned int entrycount = 0;
2802 unsigned int next_offset = 0;
2803 FILE_DIRECTORY_INFO *entryptr;
2805 if (bufstart == NULL)
2808 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2811 entryptr = (FILE_DIRECTORY_INFO *)
2812 ((char *)entryptr + next_offset);
2814 if ((char *)entryptr + size > end_of_buf) {
2815 cifs_dbg(VFS, "malformed search entry would overflow\n");
2819 len = le32_to_cpu(entryptr->FileNameLength);
2820 if ((char *)entryptr + len + size > end_of_buf) {
2821 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2826 *lastentry = (char *)entryptr;
2829 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2841 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2842 u64 persistent_fid, u64 volatile_fid, int index,
2843 struct cifs_search_info *srch_inf)
2845 struct smb2_query_directory_req *req;
2846 struct smb2_query_directory_rsp *rsp = NULL;
2848 struct kvec rsp_iov;
2851 int resp_buftype = CIFS_NO_BUFFER;
2852 unsigned char *bufptr;
2853 struct TCP_Server_Info *server;
2854 struct cifs_ses *ses = tcon->ses;
2855 __le16 asteriks = cpu_to_le16('*');
2857 unsigned int output_size = CIFSMaxBufSize;
2858 size_t info_buf_size;
2861 if (ses && (ses->server))
2862 server = ses->server;
2866 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2870 if (encryption_required(tcon))
2871 flags |= CIFS_TRANSFORM_REQ;
2873 switch (srch_inf->info_level) {
2874 case SMB_FIND_FILE_DIRECTORY_INFO:
2875 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2876 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2878 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2879 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2880 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2883 cifs_dbg(VFS, "info level %u isn't supported\n",
2884 srch_inf->info_level);
2889 req->FileIndex = cpu_to_le32(index);
2890 req->PersistentFileId = persistent_fid;
2891 req->VolatileFileId = volatile_fid;
2894 bufptr = req->Buffer;
2895 memcpy(bufptr, &asteriks, len);
2897 req->FileNameOffset =
2898 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2899 req->FileNameLength = cpu_to_le16(len);
2901 * BB could be 30 bytes or so longer if we used SMB2 specific
2902 * buffer lengths, but this is safe and close enough.
2904 output_size = min_t(unsigned int, output_size, server->maxBuf);
2905 output_size = min_t(unsigned int, output_size, 2 << 15);
2906 req->OutputBufferLength = cpu_to_le32(output_size);
2908 iov[0].iov_base = (char *)req;
2909 /* 4 for RFC1001 length and 1 for Buffer */
2910 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2912 iov[1].iov_base = (char *)(req->Buffer);
2913 iov[1].iov_len = len;
2915 inc_rfc1001_len(req, len - 1 /* Buffer */);
2917 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
2918 cifs_small_buf_release(req);
2919 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
2922 if (rc == -ENODATA &&
2923 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
2924 srch_inf->endOfSearch = true;
2927 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2931 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2932 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2937 srch_inf->unicode = true;
2939 if (srch_inf->ntwrk_buf_start) {
2940 if (srch_inf->smallBuf)
2941 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2943 cifs_buf_release(srch_inf->ntwrk_buf_start);
2945 srch_inf->ntwrk_buf_start = (char *)rsp;
2946 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2947 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2948 /* 4 for rfc1002 length field */
2949 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2950 srch_inf->entries_in_buffer =
2951 num_entries(srch_inf->srch_entries_start, end_of_smb,
2952 &srch_inf->last_entry, info_buf_size);
2953 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
2954 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2955 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2956 srch_inf->srch_entries_start, srch_inf->last_entry);
2957 if (resp_buftype == CIFS_LARGE_BUFFER)
2958 srch_inf->smallBuf = false;
2959 else if (resp_buftype == CIFS_SMALL_BUFFER)
2960 srch_inf->smallBuf = true;
2962 cifs_dbg(VFS, "illegal search buffer type\n");
2967 free_rsp_buf(resp_buftype, rsp);
2972 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2973 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
2974 unsigned int num, void **data, unsigned int *size)
2976 struct smb2_set_info_req *req;
2977 struct smb2_set_info_rsp *rsp = NULL;
2979 struct kvec rsp_iov;
2983 struct TCP_Server_Info *server;
2984 struct cifs_ses *ses = tcon->ses;
2987 if (ses && (ses->server))
2988 server = ses->server;
2995 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2999 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
3005 if (encryption_required(tcon))
3006 flags |= CIFS_TRANSFORM_REQ;
3008 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
3010 req->InfoType = SMB2_O_INFO_FILE;
3011 req->FileInfoClass = info_class;
3012 req->PersistentFileId = persistent_fid;
3013 req->VolatileFileId = volatile_fid;
3015 /* 4 for RFC1001 length and 1 for Buffer */
3017 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
3018 req->BufferLength = cpu_to_le32(*size);
3020 inc_rfc1001_len(req, *size - 1 /* Buffer */);
3022 memcpy(req->Buffer, *data, *size);
3024 iov[0].iov_base = (char *)req;
3025 /* 4 for RFC1001 length */
3026 iov[0].iov_len = get_rfc1002_length(req) + 4;
3028 for (i = 1; i < num; i++) {
3029 inc_rfc1001_len(req, size[i]);
3030 le32_add_cpu(&req->BufferLength, size[i]);
3031 iov[i].iov_base = (char *)data[i];
3032 iov[i].iov_len = size[i];
3035 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, flags, &rsp_iov);
3036 cifs_small_buf_release(req);
3037 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
3040 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
3042 free_rsp_buf(resp_buftype, rsp);
3048 SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3049 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3051 struct smb2_file_rename_info info;
3053 unsigned int size[2];
3055 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3057 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3061 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3062 /* 0 = fail if target already exists */
3063 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3064 info.FileNameLength = cpu_to_le32(len);
3067 size[0] = sizeof(struct smb2_file_rename_info);
3069 data[1] = target_file;
3070 size[1] = len + 2 /* null */;
3072 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
3073 current->tgid, FILE_RENAME_INFORMATION, 2, data,
3080 SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3081 u64 persistent_fid, u64 volatile_fid)
3083 __u8 delete_pending = 1;
3087 data = &delete_pending;
3088 size = 1; /* sizeof __u8 */
3090 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3091 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
3096 SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3097 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3099 struct smb2_file_link_info info;
3101 unsigned int size[2];
3103 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3105 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3109 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3110 /* 0 = fail if link already exists */
3111 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3112 info.FileNameLength = cpu_to_le32(len);
3115 size[0] = sizeof(struct smb2_file_link_info);
3117 data[1] = target_file;
3118 size[1] = len + 2 /* null */;
3120 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
3121 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
3127 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3128 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
3130 struct smb2_file_eof_info info;
3134 info.EndOfFile = *eof;
3137 size = sizeof(struct smb2_file_eof_info);
3140 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3141 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
3143 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3144 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
3148 SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3149 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3152 size = sizeof(FILE_BASIC_INFO);
3153 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3154 current->tgid, FILE_BASIC_INFORMATION, 1,
3155 (void **)&buf, &size);
3159 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3160 const u64 persistent_fid, const u64 volatile_fid,
3164 struct smb2_oplock_break *req = NULL;
3165 int flags = CIFS_OBREAK_OP;
3167 cifs_dbg(FYI, "SMB2_oplock_break\n");
3168 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3172 if (encryption_required(tcon))
3173 flags |= CIFS_TRANSFORM_REQ;
3175 req->VolatileFid = volatile_fid;
3176 req->PersistentFid = persistent_fid;
3177 req->OplockLevel = oplock_level;
3178 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
3180 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
3181 cifs_small_buf_release(req);
3184 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3185 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
3192 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3193 struct kstatfs *kst)
3195 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3196 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3197 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
3198 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
3199 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
3204 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3205 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3208 struct smb2_query_info_req *req;
3210 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
3212 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3215 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
3219 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3220 req->FileInfoClass = level;
3221 req->PersistentFileId = persistent_fid;
3222 req->VolatileFileId = volatile_fid;
3223 /* 4 for rfc1002 length field and 1 for pad */
3224 req->InputBufferOffset =
3225 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
3226 req->OutputBufferLength = cpu_to_le32(
3227 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3229 iov->iov_base = (char *)req;
3230 /* 4 for rfc1002 length field */
3231 iov->iov_len = get_rfc1002_length(req) + 4;
3236 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3237 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3239 struct smb2_query_info_rsp *rsp = NULL;
3241 struct kvec rsp_iov;
3244 struct cifs_ses *ses = tcon->ses;
3245 struct smb2_fs_full_size_info *info = NULL;
3248 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3249 sizeof(struct smb2_fs_full_size_info),
3250 persistent_fid, volatile_fid);
3254 if (encryption_required(tcon))
3255 flags |= CIFS_TRANSFORM_REQ;
3257 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
3258 cifs_small_buf_release(iov.iov_base);
3260 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3263 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3265 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3266 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3267 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3268 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3269 sizeof(struct smb2_fs_full_size_info));
3271 copy_fs_info_to_kstatfs(info, fsdata);
3274 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3279 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
3280 u64 persistent_fid, u64 volatile_fid, int level)
3282 struct smb2_query_info_rsp *rsp = NULL;
3284 struct kvec rsp_iov;
3286 int resp_buftype, max_len, min_len;
3287 struct cifs_ses *ses = tcon->ses;
3288 unsigned int rsp_len, offset;
3291 if (level == FS_DEVICE_INFORMATION) {
3292 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3293 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3294 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3295 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3296 min_len = MIN_FS_ATTR_INFO_SIZE;
3297 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3298 max_len = sizeof(struct smb3_fs_ss_info);
3299 min_len = sizeof(struct smb3_fs_ss_info);
3301 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
3305 rc = build_qfs_info_req(&iov, tcon, level, max_len,
3306 persistent_fid, volatile_fid);
3310 if (encryption_required(tcon))
3311 flags |= CIFS_TRANSFORM_REQ;
3313 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
3314 cifs_small_buf_release(iov.iov_base);
3316 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3319 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3321 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3322 offset = le16_to_cpu(rsp->OutputBufferOffset);
3323 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3327 if (level == FS_ATTRIBUTE_INFORMATION)
3328 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3329 + (char *)&rsp->hdr, min_t(unsigned int,
3331 else if (level == FS_DEVICE_INFORMATION)
3332 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3333 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
3334 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3335 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3336 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3337 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3338 tcon->perf_sector_size =
3339 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3343 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3348 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3349 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3350 const __u32 num_lock, struct smb2_lock_element *buf)
3353 struct smb2_lock_req *req = NULL;
3355 struct kvec rsp_iov;
3358 int flags = CIFS_NO_RESP;
3360 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
3362 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3366 if (encryption_required(tcon))
3367 flags |= CIFS_TRANSFORM_REQ;
3369 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
3370 req->LockCount = cpu_to_le16(num_lock);
3372 req->PersistentFileId = persist_fid;
3373 req->VolatileFileId = volatile_fid;
3375 count = num_lock * sizeof(struct smb2_lock_element);
3376 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3378 iov[0].iov_base = (char *)req;
3379 /* 4 for rfc1002 length field and count for all locks */
3380 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3381 iov[1].iov_base = (char *)buf;
3382 iov[1].iov_len = count;
3384 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3385 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3387 cifs_small_buf_release(req);
3389 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
3390 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3397 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3398 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3399 const __u64 length, const __u64 offset, const __u32 lock_flags,
3402 struct smb2_lock_element lock;
3404 lock.Offset = cpu_to_le64(offset);
3405 lock.Length = cpu_to_le64(length);
3406 lock.Flags = cpu_to_le32(lock_flags);
3407 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3408 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3410 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3414 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3415 __u8 *lease_key, const __le32 lease_state)
3418 struct smb2_lease_ack *req = NULL;
3419 int flags = CIFS_OBREAK_OP;
3421 cifs_dbg(FYI, "SMB2_lease_break\n");
3422 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3426 if (encryption_required(tcon))
3427 flags |= CIFS_TRANSFORM_REQ;
3429 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
3430 req->StructureSize = cpu_to_le16(36);
3431 inc_rfc1001_len(req, 12);
3433 memcpy(req->LeaseKey, lease_key, 16);
3434 req->LeaseState = lease_state;
3436 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
3437 cifs_small_buf_release(req);
3440 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3441 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);