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 */
80 static int encryption_required(const struct cifs_tcon *tcon)
84 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
85 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
88 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
94 smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
95 const struct cifs_tcon *tcon)
97 shdr->ProtocolId = SMB2_PROTO_NUMBER;
98 shdr->StructureSize = cpu_to_le16(64);
99 shdr->Command = smb2_cmd;
100 if (tcon && tcon->ses && tcon->ses->server) {
101 struct TCP_Server_Info *server = tcon->ses->server;
103 spin_lock(&server->req_lock);
104 /* Request up to 2 credits but don't go over the limit. */
105 if (server->credits >= server->max_credits)
106 shdr->CreditRequest = cpu_to_le16(0);
108 shdr->CreditRequest = cpu_to_le16(
109 min_t(int, server->max_credits -
110 server->credits, 2));
111 spin_unlock(&server->req_lock);
113 shdr->CreditRequest = cpu_to_le16(2);
115 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
120 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
121 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
122 if ((tcon->ses) && (tcon->ses->server) &&
123 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
124 shdr->CreditCharge = cpu_to_le16(1);
125 /* else CreditCharge MBZ */
127 shdr->TreeId = tcon->tid;
128 /* Uid is not converted */
130 shdr->SessionId = tcon->ses->Suid;
133 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
134 * to pass the path on the Open SMB prefixed by \\server\share.
135 * Not sure when we would need to do the augmented path (if ever) and
136 * setting this flag breaks the SMB2 open operation since it is
137 * illegal to send an empty path name (without \\server\share prefix)
138 * when the DFS flag is set in the SMB open header. We could
139 * consider setting the flag on all operations other than open
140 * but it is safer to net set it for now.
142 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
143 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
145 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
146 !encryption_required(tcon))
147 shdr->Flags |= SMB2_FLAGS_SIGNED;
153 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
156 struct nls_table *nls_codepage;
157 struct cifs_ses *ses;
158 struct TCP_Server_Info *server;
161 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
162 * check for tcp and smb session status done differently
163 * for those three - in the calling routine.
168 if (smb2_command == SMB2_TREE_CONNECT)
171 if (tcon->tidStatus == CifsExiting) {
173 * only tree disconnect, open, and write,
174 * (and ulogoff which does not have tcon)
175 * are allowed as we start force umount.
177 if ((smb2_command != SMB2_WRITE) &&
178 (smb2_command != SMB2_CREATE) &&
179 (smb2_command != SMB2_TREE_DISCONNECT)) {
180 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
185 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
186 (!tcon->ses->server))
190 server = ses->server;
193 * Give demultiplex thread up to 10 seconds to reconnect, should be
194 * greater than cifs socket timeout which is 7 seconds
196 while (server->tcpStatus == CifsNeedReconnect) {
198 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
199 * here since they are implicitly done when session drops.
201 switch (smb2_command) {
203 * BB Should we keep oplock break and add flush to exceptions?
205 case SMB2_TREE_DISCONNECT:
208 case SMB2_OPLOCK_BREAK:
212 wait_event_interruptible_timeout(server->response_q,
213 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
215 /* are we still trying to reconnect? */
216 if (server->tcpStatus != CifsNeedReconnect)
220 * on "soft" mounts we wait once. Hard mounts keep
221 * retrying until process is killed or server comes
225 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
230 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
233 nls_codepage = load_nls_default();
236 * need to prevent multiple threads trying to simultaneously reconnect
237 * the same SMB session
239 mutex_lock(&tcon->ses->session_mutex);
240 rc = cifs_negotiate_protocol(0, tcon->ses);
241 if (!rc && tcon->ses->need_reconnect)
242 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
244 if (rc || !tcon->need_reconnect) {
245 mutex_unlock(&tcon->ses->session_mutex);
249 cifs_mark_open_files_invalid(tcon);
250 if (tcon->use_persistent)
251 tcon->need_reopen_files = true;
253 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
254 mutex_unlock(&tcon->ses->session_mutex);
256 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
260 if (smb2_command != SMB2_INTERNAL_CMD)
261 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
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 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
291 unsigned int *total_len)
293 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
294 /* lookup word count ie StructureSize from table */
295 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
298 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
299 * largest operations (Create)
303 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
304 spdu->StructureSize2 = cpu_to_le16(parmsize);
306 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
309 /* init request without RFC1001 length at the beginning */
311 smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
312 void **request_buf, unsigned int *total_len)
315 struct smb2_sync_hdr *shdr;
317 rc = smb2_reconnect(smb2_command, tcon);
321 /* BB eventually switch this to SMB2 specific small buf size */
322 *request_buf = cifs_small_buf_get();
323 if (*request_buf == NULL) {
324 /* BB should we add a retry in here if not a writepage? */
328 shdr = (struct smb2_sync_hdr *)(*request_buf);
330 fill_small_buf(smb2_command, tcon, shdr, total_len);
333 #ifdef CONFIG_CIFS_STATS2
334 uint16_t com_code = le16_to_cpu(smb2_command);
336 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
338 cifs_stats_inc(&tcon->num_smbs_sent);
345 * Allocate and return pointer to an SMB request hdr, and set basic
346 * SMB information in the SMB header. If the return code is zero, this
347 * function must have filled in request_buf pointer. The returned buffer
348 * has RFC1001 length at the beginning.
351 small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
355 unsigned int total_len;
356 struct smb2_pdu *pdu;
358 rc = smb2_reconnect(smb2_command, tcon);
362 /* BB eventually switch this to SMB2 specific small buf size */
363 *request_buf = cifs_small_buf_get();
364 if (*request_buf == NULL) {
365 /* BB should we add a retry in here if not a writepage? */
369 pdu = (struct smb2_pdu *)(*request_buf);
371 fill_small_buf(smb2_command, tcon, get_sync_hdr(pdu), &total_len);
373 /* Note this is only network field converted to big endian */
374 pdu->hdr.smb2_buf_length = cpu_to_be32(total_len);
377 #ifdef CONFIG_CIFS_STATS2
378 uint16_t com_code = le16_to_cpu(smb2_command);
379 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
381 cifs_stats_inc(&tcon->num_smbs_sent);
387 #ifdef CONFIG_CIFS_SMB311
388 /* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
389 #define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
392 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
393 #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
396 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
398 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
399 pneg_ctxt->DataLength = cpu_to_le16(38);
400 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
401 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
402 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
403 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
407 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
409 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
410 pneg_ctxt->DataLength = cpu_to_le16(6);
411 pneg_ctxt->CipherCount = cpu_to_le16(2);
412 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
413 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
417 assemble_neg_contexts(struct smb2_negotiate_req *req)
420 /* +4 is to account for the RFC1001 len field */
421 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
423 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
424 /* Add 2 to size to round to 8 byte boundary */
425 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
426 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
427 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
428 req->NegotiateContextCount = cpu_to_le16(2);
429 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2
430 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
433 static void assemble_neg_contexts(struct smb2_negotiate_req *req)
441 * SMB2 Worker functions follow:
443 * The general structure of the worker functions is:
444 * 1) Call smb2_init (assembles SMB2 header)
445 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
446 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
447 * 4) Decode SMB2 command specific fields in the fixed length area
448 * 5) Decode variable length data area (if any for this SMB2 command type)
449 * 6) Call free smb buffer
455 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
457 struct smb2_negotiate_req *req;
458 struct smb2_negotiate_rsp *rsp;
463 struct TCP_Server_Info *server = ses->server;
464 int blob_offset, blob_length;
466 int flags = CIFS_NEG_OP;
468 cifs_dbg(FYI, "Negotiate protocol\n");
471 WARN(1, "%s: server is NULL!\n", __func__);
475 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
479 req->hdr.sync_hdr.SessionId = 0;
481 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
483 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
484 inc_rfc1001_len(req, 2);
486 /* only one of SMB2 signing flags may be set in SMB2 request */
488 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
489 else if (global_secflags & CIFSSEC_MAY_SIGN)
490 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
492 req->SecurityMode = 0;
494 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
496 /* ClientGUID must be zero for SMB2.02 dialect */
497 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
498 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
500 memcpy(req->ClientGUID, server->client_guid,
501 SMB2_CLIENT_GUID_SIZE);
502 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
503 assemble_neg_contexts(req);
505 iov[0].iov_base = (char *)req;
506 /* 4 for rfc1002 length field */
507 iov[0].iov_len = get_rfc1002_length(req) + 4;
509 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
510 cifs_small_buf_release(req);
511 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
513 * No tcon so can't do
514 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
519 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
521 /* BB we may eventually want to match the negotiated vs. requested
522 dialect, even though we are only requesting one at a time */
523 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
524 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
525 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
526 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
527 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
528 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
529 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
530 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
531 #ifdef CONFIG_CIFS_SMB311
532 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
533 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
536 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
537 le16_to_cpu(rsp->DialectRevision));
541 server->dialect = le16_to_cpu(rsp->DialectRevision);
543 /* SMB2 only has an extended negflavor */
544 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
545 /* set it to the maximum buffer size value we can send with 1 credit */
546 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
547 SMB2_MAX_BUFFER_SIZE);
548 server->max_read = le32_to_cpu(rsp->MaxReadSize);
549 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
550 /* BB Do we need to validate the SecurityMode? */
551 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
552 server->capabilities = le32_to_cpu(rsp->Capabilities);
554 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
556 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
559 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
561 * ses->sectype = RawNTLMSSP;
562 * but for time being this is our only auth choice so doesn't matter.
563 * We just found a server which sets blob length to zero expecting raw.
565 if (blob_length == 0)
566 cifs_dbg(FYI, "missing security blob on negprot\n");
568 rc = cifs_enable_signing(server, ses->sign);
572 rc = decode_negTokenInit(security_blob, blob_length, server);
579 free_rsp_buf(resp_buftype, rsp);
583 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
586 struct validate_negotiate_info_req vneg_inbuf;
587 struct validate_negotiate_info_rsp *pneg_rsp;
590 cifs_dbg(FYI, "validate negotiate\n");
593 * validation ioctl must be signed, so no point sending this if we
594 * can not sign it. We could eventually change this to selectively
595 * sign just this, the first and only signed request on a connection.
596 * This is good enough for now since a user who wants better security
597 * would also enable signing on the mount. Having validation of
598 * negotiate info for signed connections helps reduce attack vectors
600 if (tcon->ses->server->sign == false)
601 return 0; /* validation requires signing */
603 vneg_inbuf.Capabilities =
604 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
605 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
606 SMB2_CLIENT_GUID_SIZE);
609 vneg_inbuf.SecurityMode =
610 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
611 else if (global_secflags & CIFSSEC_MAY_SIGN)
612 vneg_inbuf.SecurityMode =
613 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
615 vneg_inbuf.SecurityMode = 0;
617 vneg_inbuf.DialectCount = cpu_to_le16(1);
618 vneg_inbuf.Dialects[0] =
619 cpu_to_le16(tcon->ses->server->vals->protocol_id);
621 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
622 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
624 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
625 (char **)&pneg_rsp, &rsplen);
628 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
632 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
633 cifs_dbg(VFS, "invalid size of protocol negotiate response\n");
637 /* check validate negotiate info response matches what we got earlier */
638 if (pneg_rsp->Dialect !=
639 cpu_to_le16(tcon->ses->server->vals->protocol_id))
642 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
645 /* do not validate server guid because not saved at negprot time yet */
647 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
648 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
651 /* validate negotiate successful */
652 cifs_dbg(FYI, "validate negotiate info successful\n");
656 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
661 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
670 if (server->sec_ntlmssp &&
671 (global_secflags & CIFSSEC_MAY_NTLMSSP))
673 if ((server->sec_kerberos || server->sec_mskerberos) &&
674 (global_secflags & CIFSSEC_MAY_KRB5))
682 struct SMB2_sess_data {
684 struct cifs_ses *ses;
685 struct nls_table *nls_cp;
686 void (*func)(struct SMB2_sess_data *);
688 u64 previous_session;
690 /* we will send the SMB in three pieces:
691 * a fixed length beginning part, an optional
692 * SPNEGO blob (which can be zero length), and a
693 * last part which will include the strings
694 * and rest of bcc area. This allows us to avoid
695 * a large buffer 17K allocation
702 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
705 struct cifs_ses *ses = sess_data->ses;
706 struct smb2_sess_setup_req *req;
707 struct TCP_Server_Info *server = ses->server;
709 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
713 /* First session, not a reauthenticate */
714 req->hdr.sync_hdr.SessionId = 0;
716 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
717 req->PreviousSessionId = sess_data->previous_session;
719 req->Flags = 0; /* MBZ */
720 /* to enable echos and oplocks */
721 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(3);
723 /* only one of SMB2 signing flags may be set in SMB2 request */
725 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
726 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
727 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
729 req->SecurityMode = 0;
731 req->Capabilities = 0;
732 req->Channel = 0; /* MBZ */
734 sess_data->iov[0].iov_base = (char *)req;
735 /* 4 for rfc1002 length field and 1 for pad */
736 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
738 * This variable will be used to clear the buffer
739 * allocated above in case of any error in the calling function.
741 sess_data->buf0_type = CIFS_SMALL_BUFFER;
747 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
749 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
750 sess_data->buf0_type = CIFS_NO_BUFFER;
754 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
757 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
758 struct kvec rsp_iov = { NULL, 0 };
760 /* Testing shows that buffer offset must be at location of Buffer[0] */
761 req->SecurityBufferOffset =
762 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
763 1 /* pad */ - 4 /* rfc1001 len */);
764 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
766 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
768 /* BB add code to build os and lm fields */
770 rc = SendReceive2(sess_data->xid, sess_data->ses,
772 &sess_data->buf0_type,
773 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
774 cifs_small_buf_release(sess_data->iov[0].iov_base);
775 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
781 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
784 struct cifs_ses *ses = sess_data->ses;
786 mutex_lock(&ses->server->srv_mutex);
787 if (ses->server->ops->generate_signingkey) {
788 rc = ses->server->ops->generate_signingkey(ses);
791 "SMB3 session key generation failed\n");
792 mutex_unlock(&ses->server->srv_mutex);
796 if (!ses->server->session_estab) {
797 ses->server->sequence_number = 0x2;
798 ses->server->session_estab = true;
800 mutex_unlock(&ses->server->srv_mutex);
802 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
803 spin_lock(&GlobalMid_Lock);
804 ses->status = CifsGood;
805 ses->need_reconnect = false;
806 spin_unlock(&GlobalMid_Lock);
810 #ifdef CONFIG_CIFS_UPCALL
812 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
815 struct cifs_ses *ses = sess_data->ses;
816 struct cifs_spnego_msg *msg;
817 struct key *spnego_key = NULL;
818 struct smb2_sess_setup_rsp *rsp = NULL;
820 rc = SMB2_sess_alloc_buffer(sess_data);
824 spnego_key = cifs_get_spnego_key(ses);
825 if (IS_ERR(spnego_key)) {
826 rc = PTR_ERR(spnego_key);
831 msg = spnego_key->payload.data[0];
833 * check version field to make sure that cifs.upcall is
834 * sending us a response in an expected form
836 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
838 "bad cifs.upcall version. Expected %d got %d",
839 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
841 goto out_put_spnego_key;
844 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
846 if (!ses->auth_key.response) {
848 "Kerberos can't allocate (%u bytes) memory",
851 goto out_put_spnego_key;
853 ses->auth_key.len = msg->sesskey_len;
855 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
856 sess_data->iov[1].iov_len = msg->secblob_len;
858 rc = SMB2_sess_sendreceive(sess_data);
860 goto out_put_spnego_key;
862 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
863 ses->Suid = rsp->hdr.sync_hdr.SessionId;
865 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
867 rc = SMB2_sess_establish_session(sess_data);
869 key_invalidate(spnego_key);
872 sess_data->result = rc;
873 sess_data->func = NULL;
874 SMB2_sess_free_buffer(sess_data);
878 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
880 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
881 sess_data->result = -EOPNOTSUPP;
882 sess_data->func = NULL;
887 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
890 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
893 struct cifs_ses *ses = sess_data->ses;
894 struct smb2_sess_setup_rsp *rsp = NULL;
895 char *ntlmssp_blob = NULL;
896 bool use_spnego = false; /* else use raw ntlmssp */
900 * If memory allocation is successful, caller of this function
903 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
908 ses->ntlmssp->sesskey_per_smbsess = true;
910 rc = SMB2_sess_alloc_buffer(sess_data);
914 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
916 if (ntlmssp_blob == NULL) {
921 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
923 /* BB eventually need to add this */
924 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
928 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
929 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
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);
935 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
937 /* If true, rc here is expected and not an error */
938 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
939 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
945 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
946 le16_to_cpu(rsp->SecurityBufferOffset)) {
947 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
948 le16_to_cpu(rsp->SecurityBufferOffset));
952 rc = decode_ntlmssp_challenge(rsp->Buffer,
953 le16_to_cpu(rsp->SecurityBufferLength), ses);
957 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
960 ses->Suid = rsp->hdr.sync_hdr.SessionId;
961 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
965 SMB2_sess_free_buffer(sess_data);
967 sess_data->result = 0;
968 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
974 sess_data->result = rc;
975 sess_data->func = NULL;
979 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
982 struct cifs_ses *ses = sess_data->ses;
983 struct smb2_sess_setup_req *req;
984 struct smb2_sess_setup_rsp *rsp = NULL;
985 unsigned char *ntlmssp_blob = NULL;
986 bool use_spnego = false; /* else use raw ntlmssp */
989 rc = SMB2_sess_alloc_buffer(sess_data);
993 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
994 req->hdr.sync_hdr.SessionId = ses->Suid;
996 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
999 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1004 /* BB eventually need to add this */
1005 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1009 sess_data->iov[1].iov_base = ntlmssp_blob;
1010 sess_data->iov[1].iov_len = blob_length;
1012 rc = SMB2_sess_sendreceive(sess_data);
1016 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1018 ses->Suid = rsp->hdr.sync_hdr.SessionId;
1019 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1021 rc = SMB2_sess_establish_session(sess_data);
1023 kfree(ntlmssp_blob);
1024 SMB2_sess_free_buffer(sess_data);
1025 kfree(ses->ntlmssp);
1026 ses->ntlmssp = NULL;
1027 sess_data->result = rc;
1028 sess_data->func = NULL;
1032 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1036 type = smb2_select_sectype(ses->server, ses->sectype);
1037 cifs_dbg(FYI, "sess setup type %d\n", type);
1038 if (type == Unspecified) {
1040 "Unable to select appropriate authentication method!");
1046 sess_data->func = SMB2_auth_kerberos;
1049 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1052 cifs_dbg(VFS, "secType %d not supported!\n", type);
1060 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1061 const struct nls_table *nls_cp)
1064 struct TCP_Server_Info *server = ses->server;
1065 struct SMB2_sess_data *sess_data;
1067 cifs_dbg(FYI, "Session Setup\n");
1070 WARN(1, "%s: server is NULL!\n", __func__);
1074 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1078 rc = SMB2_select_sec(ses, sess_data);
1081 sess_data->xid = xid;
1082 sess_data->ses = ses;
1083 sess_data->buf0_type = CIFS_NO_BUFFER;
1084 sess_data->nls_cp = (struct nls_table *) nls_cp;
1086 while (sess_data->func)
1087 sess_data->func(sess_data);
1089 rc = sess_data->result;
1096 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1098 struct smb2_logoff_req *req; /* response is also trivial struct */
1100 struct TCP_Server_Info *server;
1103 cifs_dbg(FYI, "disconnect session %p\n", ses);
1105 if (ses && (ses->server))
1106 server = ses->server;
1110 /* no need to send SMB logoff if uid already closed due to reconnect */
1111 if (ses->need_reconnect)
1112 goto smb2_session_already_dead;
1114 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1118 /* since no tcon, smb2_init can not do this, so do here */
1119 req->hdr.sync_hdr.SessionId = ses->Suid;
1121 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1122 flags |= CIFS_TRANSFORM_REQ;
1123 else if (server->sign)
1124 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1126 rc = SendReceiveNoRsp(xid, ses, (char *) req, flags);
1127 cifs_small_buf_release(req);
1129 * No tcon so can't do
1130 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1133 smb2_session_already_dead:
1137 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1139 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1142 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1144 /* These are similar values to what Windows uses */
1145 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1147 tcon->max_chunks = 256;
1148 tcon->max_bytes_chunk = 1048576;
1149 tcon->max_bytes_copy = 16777216;
1153 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1154 struct cifs_tcon *tcon, const struct nls_table *cp)
1156 struct smb2_tree_connect_req *req;
1157 struct smb2_tree_connect_rsp *rsp = NULL;
1159 struct kvec rsp_iov;
1163 struct TCP_Server_Info *server;
1164 __le16 *unc_path = NULL;
1167 cifs_dbg(FYI, "TCON\n");
1169 if ((ses->server) && tree)
1170 server = ses->server;
1174 if (tcon && tcon->bad_network_name)
1177 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1178 if (unc_path == NULL)
1181 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1183 if (unc_path_len < 2) {
1188 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1192 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1199 if ((ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA))
1200 flags |= CIFS_TRANSFORM_REQ;
1202 /* since no tcon, smb2_init can not do this, so do here */
1203 req->hdr.sync_hdr.SessionId = ses->Suid;
1204 if (ses->server->sign)
1205 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1206 } else if (encryption_required(tcon))
1207 flags |= CIFS_TRANSFORM_REQ;
1209 iov[0].iov_base = (char *)req;
1210 /* 4 for rfc1002 length field and 1 for pad */
1211 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1213 /* Testing shows that buffer offset must be at location of Buffer[0] */
1214 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1215 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1216 req->PathLength = cpu_to_le16(unc_path_len - 2);
1217 iov[1].iov_base = unc_path;
1218 iov[1].iov_len = unc_path_len;
1220 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1222 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
1223 cifs_small_buf_release(req);
1224 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1228 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1229 tcon->need_reconnect = true;
1231 goto tcon_error_exit;
1235 ses->ipc_tid = rsp->hdr.sync_hdr.TreeId;
1239 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
1240 cifs_dbg(FYI, "connection to disk share\n");
1241 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
1243 cifs_dbg(FYI, "connection to pipe share\n");
1244 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1246 cifs_dbg(FYI, "connection to printer\n");
1248 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1250 goto tcon_error_exit;
1253 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1254 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1255 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1256 tcon->tidStatus = CifsGood;
1257 tcon->need_reconnect = false;
1258 tcon->tid = rsp->hdr.sync_hdr.TreeId;
1259 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1261 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1262 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1263 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1266 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1267 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1269 init_copy_chunk_defaults(tcon);
1270 if (tcon->ses->server->ops->validate_negotiate)
1271 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1273 free_rsp_buf(resp_buftype, rsp);
1278 if (rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1279 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1281 tcon->bad_network_name = true;
1287 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1289 struct smb2_tree_disconnect_req *req; /* response is trivial */
1291 struct TCP_Server_Info *server;
1292 struct cifs_ses *ses = tcon->ses;
1295 cifs_dbg(FYI, "Tree Disconnect\n");
1297 if (ses && (ses->server))
1298 server = ses->server;
1302 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1305 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1309 if (encryption_required(tcon))
1310 flags |= CIFS_TRANSFORM_REQ;
1312 rc = SendReceiveNoRsp(xid, ses, (char *)req, flags);
1313 cifs_small_buf_release(req);
1315 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1321 static struct create_durable *
1322 create_durable_buf(void)
1324 struct create_durable *buf;
1326 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1330 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1331 (struct create_durable, Data));
1332 buf->ccontext.DataLength = cpu_to_le32(16);
1333 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1334 (struct create_durable, Name));
1335 buf->ccontext.NameLength = cpu_to_le16(4);
1336 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1344 static struct create_durable *
1345 create_reconnect_durable_buf(struct cifs_fid *fid)
1347 struct create_durable *buf;
1349 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1353 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1354 (struct create_durable, Data));
1355 buf->ccontext.DataLength = cpu_to_le32(16);
1356 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1357 (struct create_durable, Name));
1358 buf->ccontext.NameLength = cpu_to_le16(4);
1359 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1360 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1361 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1370 parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1371 unsigned int *epoch)
1374 struct create_context *cc;
1376 unsigned int remaining;
1379 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
1380 remaining = le32_to_cpu(rsp->CreateContextsLength);
1381 cc = (struct create_context *)data_offset;
1382 while (remaining >= sizeof(struct create_context)) {
1383 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1384 if (le16_to_cpu(cc->NameLength) == 4 &&
1385 strncmp(name, "RqLs", 4) == 0)
1386 return server->ops->parse_lease_buf(cc, epoch);
1388 next = le32_to_cpu(cc->Next);
1392 cc = (struct create_context *)((char *)cc + next);
1399 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1400 unsigned int *num_iovec, __u8 *oplock)
1402 struct smb2_create_req *req = iov[0].iov_base;
1403 unsigned int num = *num_iovec;
1405 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
1406 if (iov[num].iov_base == NULL)
1408 iov[num].iov_len = server->vals->create_lease_size;
1409 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1410 if (!req->CreateContextsOffset)
1411 req->CreateContextsOffset = cpu_to_le32(
1412 sizeof(struct smb2_create_req) - 4 +
1413 iov[num - 1].iov_len);
1414 le32_add_cpu(&req->CreateContextsLength,
1415 server->vals->create_lease_size);
1416 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
1417 *num_iovec = num + 1;
1421 static struct create_durable_v2 *
1422 create_durable_v2_buf(struct cifs_fid *pfid)
1424 struct create_durable_v2 *buf;
1426 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1430 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1431 (struct create_durable_v2, dcontext));
1432 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1433 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1434 (struct create_durable_v2, Name));
1435 buf->ccontext.NameLength = cpu_to_le16(4);
1437 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1438 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1439 generate_random_uuid(buf->dcontext.CreateGuid);
1440 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1442 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1450 static struct create_durable_handle_reconnect_v2 *
1451 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1453 struct create_durable_handle_reconnect_v2 *buf;
1455 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1460 buf->ccontext.DataOffset =
1461 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1463 buf->ccontext.DataLength =
1464 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1465 buf->ccontext.NameOffset =
1466 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1468 buf->ccontext.NameLength = cpu_to_le16(4);
1470 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1471 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1472 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1473 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1475 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1484 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1485 struct cifs_open_parms *oparms)
1487 struct smb2_create_req *req = iov[0].iov_base;
1488 unsigned int num = *num_iovec;
1490 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1491 if (iov[num].iov_base == NULL)
1493 iov[num].iov_len = sizeof(struct create_durable_v2);
1494 if (!req->CreateContextsOffset)
1495 req->CreateContextsOffset =
1496 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1498 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1499 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1500 *num_iovec = num + 1;
1505 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1506 struct cifs_open_parms *oparms)
1508 struct smb2_create_req *req = iov[0].iov_base;
1509 unsigned int num = *num_iovec;
1511 /* indicate that we don't need to relock the file */
1512 oparms->reconnect = false;
1514 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1515 if (iov[num].iov_base == NULL)
1517 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1518 if (!req->CreateContextsOffset)
1519 req->CreateContextsOffset =
1520 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1522 le32_add_cpu(&req->CreateContextsLength,
1523 sizeof(struct create_durable_handle_reconnect_v2));
1524 inc_rfc1001_len(&req->hdr,
1525 sizeof(struct create_durable_handle_reconnect_v2));
1526 *num_iovec = num + 1;
1531 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1532 struct cifs_open_parms *oparms, bool use_persistent)
1534 struct smb2_create_req *req = iov[0].iov_base;
1535 unsigned int num = *num_iovec;
1537 if (use_persistent) {
1538 if (oparms->reconnect)
1539 return add_durable_reconnect_v2_context(iov, num_iovec,
1542 return add_durable_v2_context(iov, num_iovec, oparms);
1545 if (oparms->reconnect) {
1546 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1547 /* indicate that we don't need to relock the file */
1548 oparms->reconnect = false;
1550 iov[num].iov_base = create_durable_buf();
1551 if (iov[num].iov_base == NULL)
1553 iov[num].iov_len = sizeof(struct create_durable);
1554 if (!req->CreateContextsOffset)
1555 req->CreateContextsOffset =
1556 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1558 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1559 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1560 *num_iovec = num + 1;
1565 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1566 const char *treename, const __le16 *path)
1568 int treename_len, path_len;
1569 struct nls_table *cp;
1570 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1575 treename_len = strlen(treename);
1576 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1582 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1585 * make room for one path separator between the treename and
1588 *out_len = treename_len + 1 + path_len;
1591 * final path needs to be null-terminated UTF16 with a
1595 *out_size = roundup((*out_len+1)*2, 8);
1596 *out_path = kzalloc(*out_size, GFP_KERNEL);
1600 cp = load_nls_default();
1601 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1602 UniStrcat(*out_path, sep);
1603 UniStrcat(*out_path, path);
1610 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
1611 __u8 *oplock, struct smb2_file_all_info *buf,
1612 struct smb2_err_rsp **err_buf)
1614 struct smb2_create_req *req;
1615 struct smb2_create_rsp *rsp;
1616 struct TCP_Server_Info *server;
1617 struct cifs_tcon *tcon = oparms->tcon;
1618 struct cifs_ses *ses = tcon->ses;
1620 struct kvec rsp_iov;
1623 __le16 *copy_path = NULL;
1626 unsigned int n_iov = 2;
1627 __u32 file_attributes = 0;
1628 char *dhc_buf = NULL, *lc_buf = NULL;
1631 cifs_dbg(FYI, "create/open\n");
1633 if (ses && (ses->server))
1634 server = ses->server;
1638 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1642 if (encryption_required(tcon))
1643 flags |= CIFS_TRANSFORM_REQ;
1645 if (oparms->create_options & CREATE_OPTION_READONLY)
1646 file_attributes |= ATTR_READONLY;
1647 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1648 file_attributes |= ATTR_SYSTEM;
1650 req->ImpersonationLevel = IL_IMPERSONATION;
1651 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
1652 /* File attributes ignored on open (used in create though) */
1653 req->FileAttributes = cpu_to_le32(file_attributes);
1654 req->ShareAccess = FILE_SHARE_ALL_LE;
1655 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1656 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
1658 iov[0].iov_base = (char *)req;
1659 /* 4 for rfc1002 length field */
1660 iov[0].iov_len = get_rfc1002_length(req) + 4;
1661 /* -1 since last byte is buf[0] which is sent below (path) */
1664 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1666 /* [MS-SMB2] 2.2.13 NameOffset:
1667 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1668 * the SMB2 header, the file name includes a prefix that will
1669 * be processed during DFS name normalization as specified in
1670 * section 3.3.5.9. Otherwise, the file name is relative to
1671 * the share that is identified by the TreeId in the SMB2
1674 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1677 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
1678 rc = alloc_path_with_tree_prefix(©_path, ©_size,
1680 tcon->treeName, path);
1683 req->NameLength = cpu_to_le16(name_len * 2);
1684 uni_path_len = copy_size;
1687 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1688 /* MUST set path len (NameLength) to 0 opening root of share */
1689 req->NameLength = cpu_to_le16(uni_path_len - 2);
1690 if (uni_path_len % 8 != 0) {
1691 copy_size = roundup(uni_path_len, 8);
1692 copy_path = kzalloc(copy_size, GFP_KERNEL);
1695 memcpy((char *)copy_path, (const char *)path,
1697 uni_path_len = copy_size;
1702 iov[1].iov_len = uni_path_len;
1703 iov[1].iov_base = path;
1704 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1705 inc_rfc1001_len(req, uni_path_len - 1);
1707 if (!server->oplocks)
1708 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1710 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1711 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1712 req->RequestedOplockLevel = *oplock;
1714 rc = add_lease_context(server, iov, &n_iov, oplock);
1716 cifs_small_buf_release(req);
1720 lc_buf = iov[n_iov-1].iov_base;
1723 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1724 /* need to set Next field of lease context if we request it */
1725 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
1726 struct create_context *ccontext =
1727 (struct create_context *)iov[n_iov-1].iov_base;
1729 cpu_to_le32(server->vals->create_lease_size);
1732 rc = add_durable_context(iov, &n_iov, oparms,
1733 tcon->use_persistent);
1735 cifs_small_buf_release(req);
1740 dhc_buf = iov[n_iov-1].iov_base;
1743 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
1744 cifs_small_buf_release(req);
1745 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
1748 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
1750 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1755 oparms->fid->persistent_fid = rsp->PersistentFileId;
1756 oparms->fid->volatile_fid = rsp->VolatileFileId;
1759 memcpy(buf, &rsp->CreationTime, 32);
1760 buf->AllocationSize = rsp->AllocationSize;
1761 buf->EndOfFile = rsp->EndofFile;
1762 buf->Attributes = rsp->FileAttributes;
1763 buf->NumberOfLinks = cpu_to_le32(1);
1764 buf->DeletePending = 0;
1767 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
1768 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
1770 *oplock = rsp->OplockLevel;
1775 free_rsp_buf(resp_buftype, rsp);
1780 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1783 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1784 u64 volatile_fid, u32 opcode, bool is_fsctl, bool use_ipc,
1785 char *in_data, u32 indatalen,
1786 char **out_data, u32 *plen /* returned data len */)
1788 struct smb2_ioctl_req *req;
1789 struct smb2_ioctl_rsp *rsp;
1790 struct smb2_sync_hdr *shdr;
1791 struct TCP_Server_Info *server;
1792 struct cifs_ses *ses;
1794 struct kvec rsp_iov;
1800 cifs_dbg(FYI, "SMB2 IOCTL\n");
1802 if (out_data != NULL)
1805 /* zero out returned data len, in case of error */
1814 if (ses && (ses->server))
1815 server = ses->server;
1819 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1824 if (ses->ipc_tid == 0) {
1825 cifs_small_buf_release(req);
1829 cifs_dbg(FYI, "replacing tid 0x%x with IPC tid 0x%x\n",
1830 req->hdr.sync_hdr.TreeId, ses->ipc_tid);
1831 req->hdr.sync_hdr.TreeId = ses->ipc_tid;
1833 if (encryption_required(tcon))
1834 flags |= CIFS_TRANSFORM_REQ;
1836 req->CtlCode = cpu_to_le32(opcode);
1837 req->PersistentFileId = persistent_fid;
1838 req->VolatileFileId = volatile_fid;
1841 req->InputCount = cpu_to_le32(indatalen);
1842 /* do not set InputOffset if no input data */
1844 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1845 iov[1].iov_base = in_data;
1846 iov[1].iov_len = indatalen;
1851 req->OutputOffset = 0;
1852 req->OutputCount = 0; /* MBZ */
1855 * Could increase MaxOutputResponse, but that would require more
1856 * than one credit. Windows typically sets this smaller, but for some
1857 * ioctls it may be useful to allow server to send more. No point
1858 * limiting what the server can send as long as fits in one credit
1860 req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1863 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1867 iov[0].iov_base = (char *)req;
1870 * If no input data, the size of ioctl struct in
1871 * protocol spec still includes a 1 byte data buffer,
1872 * but if input data passed to ioctl, we do not
1873 * want to double count this, so we do not send
1874 * the dummy one byte of data in iovec[0] if sending
1875 * input data (in iovec[1]). We also must add 4 bytes
1876 * in first iovec to allow for rfc1002 length field.
1880 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1881 inc_rfc1001_len(req, indatalen - 1);
1883 iov[0].iov_len = get_rfc1002_length(req) + 4;
1886 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
1887 cifs_small_buf_release(req);
1888 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
1890 if ((rc != 0) && (rc != -EINVAL)) {
1891 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1893 } else if (rc == -EINVAL) {
1894 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1895 (opcode != FSCTL_SRV_COPYCHUNK)) {
1896 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1901 /* check if caller wants to look at return data or just return rc */
1902 if ((plen == NULL) || (out_data == NULL))
1905 *plen = le32_to_cpu(rsp->OutputCount);
1907 /* We check for obvious errors in the output buffer length and offset */
1909 goto ioctl_exit; /* server returned no data */
1910 else if (*plen > 0xFF00) {
1911 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1917 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1918 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1919 le32_to_cpu(rsp->OutputOffset));
1925 *out_data = kmalloc(*plen, GFP_KERNEL);
1926 if (*out_data == NULL) {
1931 shdr = get_sync_hdr(rsp);
1932 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
1934 free_rsp_buf(resp_buftype, rsp);
1939 * Individual callers to ioctl worker function follow
1943 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1944 u64 persistent_fid, u64 volatile_fid)
1947 struct compress_ioctl fsctl_input;
1948 char *ret_data = NULL;
1950 fsctl_input.CompressionState =
1951 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
1953 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1954 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1955 false /* use_ipc */,
1956 (char *)&fsctl_input /* data input */,
1957 2 /* in data len */, &ret_data /* out data */, NULL);
1959 cifs_dbg(FYI, "set compression rc %d\n", rc);
1965 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1966 u64 persistent_fid, u64 volatile_fid)
1968 struct smb2_close_req *req;
1969 struct smb2_close_rsp *rsp;
1970 struct TCP_Server_Info *server;
1971 struct cifs_ses *ses = tcon->ses;
1973 struct kvec rsp_iov;
1978 cifs_dbg(FYI, "Close\n");
1980 if (ses && (ses->server))
1981 server = ses->server;
1985 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1989 if (encryption_required(tcon))
1990 flags |= CIFS_TRANSFORM_REQ;
1992 req->PersistentFileId = persistent_fid;
1993 req->VolatileFileId = volatile_fid;
1995 iov[0].iov_base = (char *)req;
1996 /* 4 for rfc1002 length field */
1997 iov[0].iov_len = get_rfc1002_length(req) + 4;
1999 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2000 cifs_small_buf_release(req);
2001 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2004 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
2008 /* BB FIXME - decode close response, update inode for caching */
2011 free_rsp_buf(resp_buftype, rsp);
2016 validate_buf(unsigned int offset, unsigned int buffer_length,
2017 struct smb2_hdr *hdr, unsigned int min_buf_size)
2020 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2021 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2022 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2023 char *end_of_buf = begin_of_buf + buffer_length;
2026 if (buffer_length < min_buf_size) {
2027 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2028 buffer_length, min_buf_size);
2032 /* check if beyond RFC1001 maximum length */
2033 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
2034 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2035 buffer_length, smb_len);
2039 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
2040 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
2048 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2049 * Caller must free buffer.
2052 validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2053 struct smb2_hdr *hdr, unsigned int minbufsize,
2057 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2063 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2067 memcpy(data, begin_of_buf, buffer_length);
2073 query_info(const unsigned int xid, struct cifs_tcon *tcon,
2074 u64 persistent_fid, u64 volatile_fid, u8 info_class,
2075 size_t output_len, size_t min_len, void *data)
2077 struct smb2_query_info_req *req;
2078 struct smb2_query_info_rsp *rsp = NULL;
2080 struct kvec rsp_iov;
2083 struct TCP_Server_Info *server;
2084 struct cifs_ses *ses = tcon->ses;
2087 cifs_dbg(FYI, "Query Info\n");
2089 if (ses && (ses->server))
2090 server = ses->server;
2094 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2098 if (encryption_required(tcon))
2099 flags |= CIFS_TRANSFORM_REQ;
2101 req->InfoType = SMB2_O_INFO_FILE;
2102 req->FileInfoClass = info_class;
2103 req->PersistentFileId = persistent_fid;
2104 req->VolatileFileId = volatile_fid;
2105 /* 4 for rfc1002 length field and 1 for Buffer */
2106 req->InputBufferOffset =
2107 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2108 req->OutputBufferLength = cpu_to_le32(output_len);
2110 iov[0].iov_base = (char *)req;
2111 /* 4 for rfc1002 length field */
2112 iov[0].iov_len = get_rfc1002_length(req) + 4;
2114 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2115 cifs_small_buf_release(req);
2116 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2119 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2123 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2124 le32_to_cpu(rsp->OutputBufferLength),
2125 &rsp->hdr, min_len, data);
2128 free_rsp_buf(resp_buftype, rsp);
2133 SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2134 u64 persistent_fid, u64 volatile_fid,
2135 struct smb2_file_all_info *data)
2137 return query_info(xid, tcon, persistent_fid, volatile_fid,
2138 FILE_ALL_INFORMATION,
2139 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
2140 sizeof(struct smb2_file_all_info), data);
2144 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2145 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2147 return query_info(xid, tcon, persistent_fid, volatile_fid,
2148 FILE_INTERNAL_INFORMATION,
2149 sizeof(struct smb2_file_internal_info),
2150 sizeof(struct smb2_file_internal_info), uniqueid);
2154 * This is a no-op for now. We're not really interested in the reply, but
2155 * rather in the fact that the server sent one and that server->lstrp
2158 * FIXME: maybe we should consider checking that the reply matches request?
2161 smb2_echo_callback(struct mid_q_entry *mid)
2163 struct TCP_Server_Info *server = mid->callback_data;
2164 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
2165 unsigned int credits_received = 1;
2167 if (mid->mid_state == MID_RESPONSE_RECEIVED)
2168 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
2170 mutex_lock(&server->srv_mutex);
2171 DeleteMidQEntry(mid);
2172 mutex_unlock(&server->srv_mutex);
2173 add_credits(server, credits_received, CIFS_ECHO_OP);
2176 void smb2_reconnect_server(struct work_struct *work)
2178 struct TCP_Server_Info *server = container_of(work,
2179 struct TCP_Server_Info, reconnect.work);
2180 struct cifs_ses *ses;
2181 struct cifs_tcon *tcon, *tcon2;
2182 struct list_head tmp_list;
2183 int tcon_exist = false;
2185 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2186 mutex_lock(&server->reconnect_mutex);
2188 INIT_LIST_HEAD(&tmp_list);
2189 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2191 spin_lock(&cifs_tcp_ses_lock);
2192 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2193 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2194 if (tcon->need_reconnect || tcon->need_reopen_files) {
2196 list_add_tail(&tcon->rlist, &tmp_list);
2202 * Get the reference to server struct to be sure that the last call of
2203 * cifs_put_tcon() in the loop below won't release the server pointer.
2206 server->srv_count++;
2208 spin_unlock(&cifs_tcp_ses_lock);
2210 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
2211 if (!smb2_reconnect(SMB2_INTERNAL_CMD, tcon))
2212 cifs_reopen_persistent_handles(tcon);
2213 list_del_init(&tcon->rlist);
2214 cifs_put_tcon(tcon);
2217 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2218 mutex_unlock(&server->reconnect_mutex);
2220 /* now we can safely release srv struct */
2222 cifs_put_tcp_session(server, 1);
2226 SMB2_echo(struct TCP_Server_Info *server)
2228 struct smb2_echo_req *req;
2231 struct smb_rqst rqst = { .rq_iov = iov,
2234 cifs_dbg(FYI, "In echo request\n");
2236 if (server->tcpStatus == CifsNeedNegotiate) {
2237 /* No need to send echo on newly established connections */
2238 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2242 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2246 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
2248 /* 4 for rfc1002 length field */
2250 iov[0].iov_base = (char *)req;
2251 iov[1].iov_len = get_rfc1002_length(req);
2252 iov[1].iov_base = (char *)req + 4;
2254 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2255 server, CIFS_ECHO_OP);
2257 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
2259 cifs_small_buf_release(req);
2264 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2267 struct smb2_flush_req *req;
2268 struct TCP_Server_Info *server;
2269 struct cifs_ses *ses = tcon->ses;
2271 struct kvec rsp_iov;
2276 cifs_dbg(FYI, "Flush\n");
2278 if (ses && (ses->server))
2279 server = ses->server;
2283 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2287 if (encryption_required(tcon))
2288 flags |= CIFS_TRANSFORM_REQ;
2290 req->PersistentFileId = persistent_fid;
2291 req->VolatileFileId = volatile_fid;
2293 iov[0].iov_base = (char *)req;
2294 /* 4 for rfc1002 length field */
2295 iov[0].iov_len = get_rfc1002_length(req) + 4;
2297 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2298 cifs_small_buf_release(req);
2301 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2303 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2308 * To form a chain of read requests, any read requests after the first should
2309 * have the end_of_chain boolean set to true.
2312 smb2_new_read_req(void **buf, unsigned int *total_len,
2313 struct cifs_io_parms *io_parms, unsigned int remaining_bytes,
2317 struct smb2_read_plain_req *req = NULL;
2318 struct smb2_sync_hdr *shdr;
2320 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2324 if (io_parms->tcon->ses->server == NULL)
2325 return -ECONNABORTED;
2327 shdr = &req->sync_hdr;
2328 shdr->ProcessId = cpu_to_le32(io_parms->pid);
2330 req->PersistentFileId = io_parms->persistent_fid;
2331 req->VolatileFileId = io_parms->volatile_fid;
2332 req->ReadChannelInfoOffset = 0; /* reserved */
2333 req->ReadChannelInfoLength = 0; /* reserved */
2334 req->Channel = 0; /* reserved */
2335 req->MinimumCount = 0;
2336 req->Length = cpu_to_le32(io_parms->length);
2337 req->Offset = cpu_to_le64(io_parms->offset);
2339 if (request_type & CHAINED_REQUEST) {
2340 if (!(request_type & END_OF_CHAIN)) {
2341 /* next 8-byte aligned request */
2342 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2343 shdr->NextCommand = cpu_to_le32(*total_len);
2344 } else /* END_OF_CHAIN */
2345 shdr->NextCommand = 0;
2346 if (request_type & RELATED_REQUEST) {
2347 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2349 * Related requests use info from previous read request
2352 shdr->SessionId = 0xFFFFFFFF;
2353 shdr->TreeId = 0xFFFFFFFF;
2354 req->PersistentFileId = 0xFFFFFFFF;
2355 req->VolatileFileId = 0xFFFFFFFF;
2358 if (remaining_bytes > io_parms->length)
2359 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2361 req->RemainingBytes = 0;
2368 smb2_readv_callback(struct mid_q_entry *mid)
2370 struct cifs_readdata *rdata = mid->callback_data;
2371 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2372 struct TCP_Server_Info *server = tcon->ses->server;
2373 struct smb2_sync_hdr *shdr =
2374 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
2375 unsigned int credits_received = 1;
2376 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2378 .rq_pages = rdata->pages,
2379 .rq_npages = rdata->nr_pages,
2380 .rq_pagesz = rdata->pagesz,
2381 .rq_tailsz = rdata->tailsz };
2383 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2384 __func__, mid->mid, mid->mid_state, rdata->result,
2387 switch (mid->mid_state) {
2388 case MID_RESPONSE_RECEIVED:
2389 credits_received = le16_to_cpu(shdr->CreditRequest);
2390 /* result already set, check signature */
2391 if (server->sign && !mid->decrypted) {
2394 rc = smb2_verify_signature(&rqst, server);
2396 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2399 /* FIXME: should this be counted toward the initiating task? */
2400 task_io_account_read(rdata->got_bytes);
2401 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2403 case MID_REQUEST_SUBMITTED:
2404 case MID_RETRY_NEEDED:
2405 rdata->result = -EAGAIN;
2406 if (server->sign && rdata->got_bytes)
2407 /* reset bytes number since we can not check a sign */
2408 rdata->got_bytes = 0;
2409 /* FIXME: should this be counted toward the initiating task? */
2410 task_io_account_read(rdata->got_bytes);
2411 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2414 if (rdata->result != -ENODATA)
2415 rdata->result = -EIO;
2419 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2421 queue_work(cifsiod_wq, &rdata->work);
2422 mutex_lock(&server->srv_mutex);
2423 DeleteMidQEntry(mid);
2424 mutex_unlock(&server->srv_mutex);
2425 add_credits(server, credits_received, 0);
2428 /* smb2_async_readv - send an async read, and set up mid to handle result */
2430 smb2_async_readv(struct cifs_readdata *rdata)
2434 struct smb2_sync_hdr *shdr;
2435 struct cifs_io_parms io_parms;
2436 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2438 struct TCP_Server_Info *server;
2439 unsigned int total_len;
2442 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2443 __func__, rdata->offset, rdata->bytes);
2445 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2446 io_parms.offset = rdata->offset;
2447 io_parms.length = rdata->bytes;
2448 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2449 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2450 io_parms.pid = rdata->pid;
2452 server = io_parms.tcon->ses->server;
2454 rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0);
2456 if (rc == -EAGAIN && rdata->credits) {
2457 /* credits was reset by reconnect */
2459 /* reduce in_flight value since we won't send the req */
2460 spin_lock(&server->req_lock);
2461 server->in_flight--;
2462 spin_unlock(&server->req_lock);
2467 if (encryption_required(io_parms.tcon))
2468 flags |= CIFS_TRANSFORM_REQ;
2470 req_len = cpu_to_be32(total_len);
2472 rdata->iov[0].iov_base = &req_len;
2473 rdata->iov[0].iov_len = sizeof(__be32);
2474 rdata->iov[1].iov_base = buf;
2475 rdata->iov[1].iov_len = total_len;
2477 shdr = (struct smb2_sync_hdr *)buf;
2479 if (rdata->credits) {
2480 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2481 SMB2_MAX_BUFFER_SIZE));
2482 shdr->CreditRequest = shdr->CreditCharge;
2483 spin_lock(&server->req_lock);
2484 server->credits += rdata->credits -
2485 le16_to_cpu(shdr->CreditCharge);
2486 spin_unlock(&server->req_lock);
2487 wake_up(&server->request_q);
2488 flags |= CIFS_HAS_CREDITS;
2491 kref_get(&rdata->refcount);
2492 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
2493 cifs_readv_receive, smb2_readv_callback,
2494 smb3_handle_read_data, rdata, flags);
2496 kref_put(&rdata->refcount, cifs_readdata_release);
2497 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2500 cifs_small_buf_release(buf);
2505 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2506 unsigned int *nbytes, char **buf, int *buf_type)
2508 int resp_buftype, rc = -EACCES;
2509 struct smb2_read_plain_req *req = NULL;
2510 struct smb2_read_rsp *rsp = NULL;
2511 struct smb2_sync_hdr *shdr;
2513 struct kvec rsp_iov;
2514 unsigned int total_len;
2516 struct smb_rqst rqst = { .rq_iov = iov,
2518 int flags = CIFS_LOG_ERROR;
2519 struct cifs_ses *ses = io_parms->tcon->ses;
2522 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0);
2526 if (encryption_required(io_parms->tcon))
2527 flags |= CIFS_TRANSFORM_REQ;
2529 req_len = cpu_to_be32(total_len);
2531 iov[0].iov_base = &req_len;
2532 iov[0].iov_len = sizeof(__be32);
2533 iov[1].iov_base = req;
2534 iov[1].iov_len = total_len;
2536 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2537 cifs_small_buf_release(req);
2539 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
2540 shdr = get_sync_hdr(rsp);
2542 if (shdr->Status == STATUS_END_OF_FILE) {
2543 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2548 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2549 cifs_dbg(VFS, "Send error in read = %d\n", rc);
2551 *nbytes = le32_to_cpu(rsp->DataLength);
2552 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2553 (*nbytes > io_parms->length)) {
2554 cifs_dbg(FYI, "bad length %d for count %d\n",
2555 *nbytes, io_parms->length);
2562 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
2563 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2564 } else if (resp_buftype != CIFS_NO_BUFFER) {
2565 *buf = rsp_iov.iov_base;
2566 if (resp_buftype == CIFS_SMALL_BUFFER)
2567 *buf_type = CIFS_SMALL_BUFFER;
2568 else if (resp_buftype == CIFS_LARGE_BUFFER)
2569 *buf_type = CIFS_LARGE_BUFFER;
2575 * Check the mid_state and signature on received buffer (if any), and queue the
2576 * workqueue completion task.
2579 smb2_writev_callback(struct mid_q_entry *mid)
2581 struct cifs_writedata *wdata = mid->callback_data;
2582 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2583 struct TCP_Server_Info *server = tcon->ses->server;
2584 unsigned int written;
2585 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2586 unsigned int credits_received = 1;
2588 switch (mid->mid_state) {
2589 case MID_RESPONSE_RECEIVED:
2590 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
2591 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2592 if (wdata->result != 0)
2595 written = le32_to_cpu(rsp->DataLength);
2597 * Mask off high 16 bits when bytes written as returned
2598 * by the server is greater than bytes requested by the
2599 * client. OS/2 servers are known to set incorrect
2602 if (written > wdata->bytes)
2605 if (written < wdata->bytes)
2606 wdata->result = -ENOSPC;
2608 wdata->bytes = written;
2610 case MID_REQUEST_SUBMITTED:
2611 case MID_RETRY_NEEDED:
2612 wdata->result = -EAGAIN;
2615 wdata->result = -EIO;
2620 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2622 queue_work(cifsiod_wq, &wdata->work);
2623 mutex_lock(&server->srv_mutex);
2624 DeleteMidQEntry(mid);
2625 mutex_unlock(&server->srv_mutex);
2626 add_credits(tcon->ses->server, credits_received, 0);
2629 /* smb2_async_writev - send an async write, and set up mid to handle result */
2631 smb2_async_writev(struct cifs_writedata *wdata,
2632 void (*release)(struct kref *kref))
2634 int rc = -EACCES, flags = 0;
2635 struct smb2_write_req *req = NULL;
2636 struct smb2_sync_hdr *shdr;
2637 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2638 struct TCP_Server_Info *server = tcon->ses->server;
2640 struct smb_rqst rqst = { };
2642 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
2644 if (rc == -EAGAIN && wdata->credits) {
2645 /* credits was reset by reconnect */
2647 /* reduce in_flight value since we won't send the req */
2648 spin_lock(&server->req_lock);
2649 server->in_flight--;
2650 spin_unlock(&server->req_lock);
2652 goto async_writev_out;
2655 if (encryption_required(tcon))
2656 flags |= CIFS_TRANSFORM_REQ;
2658 shdr = get_sync_hdr(req);
2659 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
2661 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2662 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2663 req->WriteChannelInfoOffset = 0;
2664 req->WriteChannelInfoLength = 0;
2666 req->Offset = cpu_to_le64(wdata->offset);
2667 /* 4 for rfc1002 length field */
2668 req->DataOffset = cpu_to_le16(
2669 offsetof(struct smb2_write_req, Buffer) - 4);
2670 req->RemainingBytes = 0;
2672 /* 4 for rfc1002 length field and 1 for Buffer */
2674 iov[0].iov_base = req;
2675 iov[1].iov_len = get_rfc1002_length(req) - 1;
2676 iov[1].iov_base = (char *)req + 4;
2680 rqst.rq_pages = wdata->pages;
2681 rqst.rq_npages = wdata->nr_pages;
2682 rqst.rq_pagesz = wdata->pagesz;
2683 rqst.rq_tailsz = wdata->tailsz;
2685 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2686 wdata->offset, wdata->bytes);
2688 req->Length = cpu_to_le32(wdata->bytes);
2690 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2692 if (wdata->credits) {
2693 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2694 SMB2_MAX_BUFFER_SIZE));
2695 shdr->CreditRequest = shdr->CreditCharge;
2696 spin_lock(&server->req_lock);
2697 server->credits += wdata->credits -
2698 le16_to_cpu(shdr->CreditCharge);
2699 spin_unlock(&server->req_lock);
2700 wake_up(&server->request_q);
2701 flags |= CIFS_HAS_CREDITS;
2704 kref_get(&wdata->refcount);
2705 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2709 kref_put(&wdata->refcount, release);
2710 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2714 cifs_small_buf_release(req);
2719 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2720 * The length field from io_parms must be at least 1 and indicates a number of
2721 * elements with data to write that begins with position 1 in iov array. All
2722 * data length is specified by count.
2725 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2726 unsigned int *nbytes, struct kvec *iov, int n_vec)
2729 struct smb2_write_req *req = NULL;
2730 struct smb2_write_rsp *rsp = NULL;
2732 struct kvec rsp_iov;
2740 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2744 if (io_parms->tcon->ses->server == NULL)
2745 return -ECONNABORTED;
2747 if (encryption_required(io_parms->tcon))
2748 flags |= CIFS_TRANSFORM_REQ;
2750 req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
2752 req->PersistentFileId = io_parms->persistent_fid;
2753 req->VolatileFileId = io_parms->volatile_fid;
2754 req->WriteChannelInfoOffset = 0;
2755 req->WriteChannelInfoLength = 0;
2757 req->Length = cpu_to_le32(io_parms->length);
2758 req->Offset = cpu_to_le64(io_parms->offset);
2759 /* 4 for rfc1002 length field */
2760 req->DataOffset = cpu_to_le16(
2761 offsetof(struct smb2_write_req, Buffer) - 4);
2762 req->RemainingBytes = 0;
2764 iov[0].iov_base = (char *)req;
2765 /* 4 for rfc1002 length field and 1 for Buffer */
2766 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2768 /* length of entire message including data to be written */
2769 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2771 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2772 &resp_buftype, flags, &rsp_iov);
2773 cifs_small_buf_release(req);
2774 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
2777 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
2778 cifs_dbg(VFS, "Send error in write = %d\n", rc);
2780 *nbytes = le32_to_cpu(rsp->DataLength);
2782 free_rsp_buf(resp_buftype, rsp);
2787 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2790 unsigned int entrycount = 0;
2791 unsigned int next_offset = 0;
2792 FILE_DIRECTORY_INFO *entryptr;
2794 if (bufstart == NULL)
2797 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2800 entryptr = (FILE_DIRECTORY_INFO *)
2801 ((char *)entryptr + next_offset);
2803 if ((char *)entryptr + size > end_of_buf) {
2804 cifs_dbg(VFS, "malformed search entry would overflow\n");
2808 len = le32_to_cpu(entryptr->FileNameLength);
2809 if ((char *)entryptr + len + size > end_of_buf) {
2810 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2815 *lastentry = (char *)entryptr;
2818 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2830 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2831 u64 persistent_fid, u64 volatile_fid, int index,
2832 struct cifs_search_info *srch_inf)
2834 struct smb2_query_directory_req *req;
2835 struct smb2_query_directory_rsp *rsp = NULL;
2837 struct kvec rsp_iov;
2840 int resp_buftype = CIFS_NO_BUFFER;
2841 unsigned char *bufptr;
2842 struct TCP_Server_Info *server;
2843 struct cifs_ses *ses = tcon->ses;
2844 __le16 asteriks = cpu_to_le16('*');
2846 unsigned int output_size = CIFSMaxBufSize;
2847 size_t info_buf_size;
2850 if (ses && (ses->server))
2851 server = ses->server;
2855 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2859 if (encryption_required(tcon))
2860 flags |= CIFS_TRANSFORM_REQ;
2862 switch (srch_inf->info_level) {
2863 case SMB_FIND_FILE_DIRECTORY_INFO:
2864 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2865 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2867 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2868 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2869 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2872 cifs_dbg(VFS, "info level %u isn't supported\n",
2873 srch_inf->info_level);
2878 req->FileIndex = cpu_to_le32(index);
2879 req->PersistentFileId = persistent_fid;
2880 req->VolatileFileId = volatile_fid;
2883 bufptr = req->Buffer;
2884 memcpy(bufptr, &asteriks, len);
2886 req->FileNameOffset =
2887 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2888 req->FileNameLength = cpu_to_le16(len);
2890 * BB could be 30 bytes or so longer if we used SMB2 specific
2891 * buffer lengths, but this is safe and close enough.
2893 output_size = min_t(unsigned int, output_size, server->maxBuf);
2894 output_size = min_t(unsigned int, output_size, 2 << 15);
2895 req->OutputBufferLength = cpu_to_le32(output_size);
2897 iov[0].iov_base = (char *)req;
2898 /* 4 for RFC1001 length and 1 for Buffer */
2899 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2901 iov[1].iov_base = (char *)(req->Buffer);
2902 iov[1].iov_len = len;
2904 inc_rfc1001_len(req, len - 1 /* Buffer */);
2906 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
2907 cifs_small_buf_release(req);
2908 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
2911 if (rc == -ENODATA &&
2912 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
2913 srch_inf->endOfSearch = true;
2916 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2920 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2921 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2926 srch_inf->unicode = true;
2928 if (srch_inf->ntwrk_buf_start) {
2929 if (srch_inf->smallBuf)
2930 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2932 cifs_buf_release(srch_inf->ntwrk_buf_start);
2934 srch_inf->ntwrk_buf_start = (char *)rsp;
2935 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2936 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2937 /* 4 for rfc1002 length field */
2938 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2939 srch_inf->entries_in_buffer =
2940 num_entries(srch_inf->srch_entries_start, end_of_smb,
2941 &srch_inf->last_entry, info_buf_size);
2942 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
2943 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2944 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2945 srch_inf->srch_entries_start, srch_inf->last_entry);
2946 if (resp_buftype == CIFS_LARGE_BUFFER)
2947 srch_inf->smallBuf = false;
2948 else if (resp_buftype == CIFS_SMALL_BUFFER)
2949 srch_inf->smallBuf = true;
2951 cifs_dbg(VFS, "illegal search buffer type\n");
2956 free_rsp_buf(resp_buftype, rsp);
2961 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2962 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
2963 unsigned int num, void **data, unsigned int *size)
2965 struct smb2_set_info_req *req;
2966 struct smb2_set_info_rsp *rsp = NULL;
2968 struct kvec rsp_iov;
2972 struct TCP_Server_Info *server;
2973 struct cifs_ses *ses = tcon->ses;
2976 if (ses && (ses->server))
2977 server = ses->server;
2984 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2988 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2994 if (encryption_required(tcon))
2995 flags |= CIFS_TRANSFORM_REQ;
2997 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
2999 req->InfoType = SMB2_O_INFO_FILE;
3000 req->FileInfoClass = info_class;
3001 req->PersistentFileId = persistent_fid;
3002 req->VolatileFileId = volatile_fid;
3004 /* 4 for RFC1001 length and 1 for Buffer */
3006 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
3007 req->BufferLength = cpu_to_le32(*size);
3009 inc_rfc1001_len(req, *size - 1 /* Buffer */);
3011 memcpy(req->Buffer, *data, *size);
3013 iov[0].iov_base = (char *)req;
3014 /* 4 for RFC1001 length */
3015 iov[0].iov_len = get_rfc1002_length(req) + 4;
3017 for (i = 1; i < num; i++) {
3018 inc_rfc1001_len(req, size[i]);
3019 le32_add_cpu(&req->BufferLength, size[i]);
3020 iov[i].iov_base = (char *)data[i];
3021 iov[i].iov_len = size[i];
3024 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, flags, &rsp_iov);
3025 cifs_small_buf_release(req);
3026 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
3029 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
3031 free_rsp_buf(resp_buftype, rsp);
3037 SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3038 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3040 struct smb2_file_rename_info info;
3042 unsigned int size[2];
3044 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3046 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3050 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3051 /* 0 = fail if target already exists */
3052 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3053 info.FileNameLength = cpu_to_le32(len);
3056 size[0] = sizeof(struct smb2_file_rename_info);
3058 data[1] = target_file;
3059 size[1] = len + 2 /* null */;
3061 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
3062 current->tgid, FILE_RENAME_INFORMATION, 2, data,
3069 SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3070 u64 persistent_fid, u64 volatile_fid)
3072 __u8 delete_pending = 1;
3076 data = &delete_pending;
3077 size = 1; /* sizeof __u8 */
3079 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3080 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
3085 SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3086 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3088 struct smb2_file_link_info info;
3090 unsigned int size[2];
3092 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3094 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3098 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3099 /* 0 = fail if link already exists */
3100 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3101 info.FileNameLength = cpu_to_le32(len);
3104 size[0] = sizeof(struct smb2_file_link_info);
3106 data[1] = target_file;
3107 size[1] = len + 2 /* null */;
3109 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
3110 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
3116 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3117 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
3119 struct smb2_file_eof_info info;
3123 info.EndOfFile = *eof;
3126 size = sizeof(struct smb2_file_eof_info);
3129 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3130 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
3132 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3133 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
3137 SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3138 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3141 size = sizeof(FILE_BASIC_INFO);
3142 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3143 current->tgid, FILE_BASIC_INFORMATION, 1,
3144 (void **)&buf, &size);
3148 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3149 const u64 persistent_fid, const u64 volatile_fid,
3153 struct smb2_oplock_break *req = NULL;
3154 int flags = CIFS_OBREAK_OP;
3156 cifs_dbg(FYI, "SMB2_oplock_break\n");
3157 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3161 if (encryption_required(tcon))
3162 flags |= CIFS_TRANSFORM_REQ;
3164 req->VolatileFid = volatile_fid;
3165 req->PersistentFid = persistent_fid;
3166 req->OplockLevel = oplock_level;
3167 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
3169 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
3170 cifs_small_buf_release(req);
3173 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3174 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
3181 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3182 struct kstatfs *kst)
3184 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3185 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3186 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
3187 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
3188 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
3193 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3194 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3197 struct smb2_query_info_req *req;
3199 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
3201 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3204 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
3208 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3209 req->FileInfoClass = level;
3210 req->PersistentFileId = persistent_fid;
3211 req->VolatileFileId = volatile_fid;
3212 /* 4 for rfc1002 length field and 1 for pad */
3213 req->InputBufferOffset =
3214 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
3215 req->OutputBufferLength = cpu_to_le32(
3216 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3218 iov->iov_base = (char *)req;
3219 /* 4 for rfc1002 length field */
3220 iov->iov_len = get_rfc1002_length(req) + 4;
3225 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3226 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3228 struct smb2_query_info_rsp *rsp = NULL;
3230 struct kvec rsp_iov;
3233 struct cifs_ses *ses = tcon->ses;
3234 struct smb2_fs_full_size_info *info = NULL;
3237 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3238 sizeof(struct smb2_fs_full_size_info),
3239 persistent_fid, volatile_fid);
3243 if (encryption_required(tcon))
3244 flags |= CIFS_TRANSFORM_REQ;
3246 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
3247 cifs_small_buf_release(iov.iov_base);
3249 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3252 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3254 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3255 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3256 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3257 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3258 sizeof(struct smb2_fs_full_size_info));
3260 copy_fs_info_to_kstatfs(info, fsdata);
3263 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3268 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
3269 u64 persistent_fid, u64 volatile_fid, int level)
3271 struct smb2_query_info_rsp *rsp = NULL;
3273 struct kvec rsp_iov;
3275 int resp_buftype, max_len, min_len;
3276 struct cifs_ses *ses = tcon->ses;
3277 unsigned int rsp_len, offset;
3280 if (level == FS_DEVICE_INFORMATION) {
3281 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3282 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3283 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3284 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3285 min_len = MIN_FS_ATTR_INFO_SIZE;
3286 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3287 max_len = sizeof(struct smb3_fs_ss_info);
3288 min_len = sizeof(struct smb3_fs_ss_info);
3290 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
3294 rc = build_qfs_info_req(&iov, tcon, level, max_len,
3295 persistent_fid, volatile_fid);
3299 if (encryption_required(tcon))
3300 flags |= CIFS_TRANSFORM_REQ;
3302 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
3303 cifs_small_buf_release(iov.iov_base);
3305 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3308 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3310 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3311 offset = le16_to_cpu(rsp->OutputBufferOffset);
3312 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3316 if (level == FS_ATTRIBUTE_INFORMATION)
3317 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3318 + (char *)&rsp->hdr, min_t(unsigned int,
3320 else if (level == FS_DEVICE_INFORMATION)
3321 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3322 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
3323 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3324 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3325 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3326 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3327 tcon->perf_sector_size =
3328 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3332 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3337 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3338 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3339 const __u32 num_lock, struct smb2_lock_element *buf)
3342 struct smb2_lock_req *req = NULL;
3344 struct kvec rsp_iov;
3347 int flags = CIFS_NO_RESP;
3349 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
3351 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3355 if (encryption_required(tcon))
3356 flags |= CIFS_TRANSFORM_REQ;
3358 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
3359 req->LockCount = cpu_to_le16(num_lock);
3361 req->PersistentFileId = persist_fid;
3362 req->VolatileFileId = volatile_fid;
3364 count = num_lock * sizeof(struct smb2_lock_element);
3365 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3367 iov[0].iov_base = (char *)req;
3368 /* 4 for rfc1002 length field and count for all locks */
3369 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3370 iov[1].iov_base = (char *)buf;
3371 iov[1].iov_len = count;
3373 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3374 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3376 cifs_small_buf_release(req);
3378 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
3379 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3386 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3387 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3388 const __u64 length, const __u64 offset, const __u32 lock_flags,
3391 struct smb2_lock_element lock;
3393 lock.Offset = cpu_to_le64(offset);
3394 lock.Length = cpu_to_le64(length);
3395 lock.Flags = cpu_to_le32(lock_flags);
3396 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3397 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3399 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3403 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3404 __u8 *lease_key, const __le32 lease_state)
3407 struct smb2_lease_ack *req = NULL;
3408 int flags = CIFS_OBREAK_OP;
3410 cifs_dbg(FYI, "SMB2_lease_break\n");
3411 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3415 if (encryption_required(tcon))
3416 flags |= CIFS_TRANSFORM_REQ;
3418 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
3419 req->StructureSize = cpu_to_le16(36);
3420 inc_rfc1001_len(req, 12);
3422 memcpy(req->LeaseKey, lease_key, 16);
3423 req->LeaseState = lease_state;
3425 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
3426 cifs_small_buf_release(req);
3429 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3430 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);