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"
51 #include "smbdirect.h"
53 #ifdef CONFIG_CIFS_DFS_UPCALL
54 #include "dfs_cache.h"
58 * The following table defines the expected "StructureSize" of SMB2 requests
59 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
61 * Note that commands are defined in smb2pdu.h in le16 but the array below is
62 * indexed by command in host byte order.
64 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
65 /* SMB2_NEGOTIATE */ 36,
66 /* SMB2_SESSION_SETUP */ 25,
68 /* SMB2_TREE_CONNECT */ 9,
69 /* SMB2_TREE_DISCONNECT */ 4,
79 /* SMB2_QUERY_DIRECTORY */ 33,
80 /* SMB2_CHANGE_NOTIFY */ 32,
81 /* SMB2_QUERY_INFO */ 41,
82 /* SMB2_SET_INFO */ 33,
83 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
86 int smb3_encryption_required(const struct cifs_tcon *tcon)
88 if (!tcon || !tcon->ses)
90 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
91 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
94 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
100 smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
101 const struct cifs_tcon *tcon,
102 struct TCP_Server_Info *server)
104 shdr->ProtocolId = SMB2_PROTO_NUMBER;
105 shdr->StructureSize = cpu_to_le16(64);
106 shdr->Command = smb2_cmd;
108 spin_lock(&server->req_lock);
109 /* Request up to 10 credits but don't go over the limit. */
110 if (server->credits >= server->max_credits)
111 shdr->CreditRequest = cpu_to_le16(0);
113 shdr->CreditRequest = cpu_to_le16(
114 min_t(int, server->max_credits -
115 server->credits, 10));
116 spin_unlock(&server->req_lock);
118 shdr->CreditRequest = cpu_to_le16(2);
120 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
125 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
126 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
127 if (server && (server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
128 shdr->CreditCharge = cpu_to_le16(1);
129 /* else CreditCharge MBZ */
131 shdr->TreeId = tcon->tid;
132 /* Uid is not converted */
134 shdr->SessionId = tcon->ses->Suid;
137 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
138 * to pass the path on the Open SMB prefixed by \\server\share.
139 * Not sure when we would need to do the augmented path (if ever) and
140 * setting this flag breaks the SMB2 open operation since it is
141 * illegal to send an empty path name (without \\server\share prefix)
142 * when the DFS flag is set in the SMB open header. We could
143 * consider setting the flag on all operations other than open
144 * but it is safer to net set it for now.
146 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
147 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
149 if (server && server->sign && !smb3_encryption_required(tcon))
150 shdr->Flags |= SMB2_FLAGS_SIGNED;
155 #ifdef CONFIG_CIFS_DFS_UPCALL
156 static int __smb2_reconnect(const struct nls_table *nlsc,
157 struct cifs_tcon *tcon)
160 struct TCP_Server_Info *server = tcon->ses->server;
161 struct dfs_cache_tgt_list tl;
162 struct dfs_cache_tgt_iterator *it = NULL;
164 const char *tcp_host;
166 const char *dfs_host;
169 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
173 if (!tcon->dfs_path) {
175 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
177 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
179 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon,
185 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl);
189 extract_unc_hostname(server->hostname, &tcp_host, &tcp_host_len);
191 for (it = dfs_cache_get_tgt_iterator(&tl); it;
192 it = dfs_cache_get_next_tgt(&tl, it)) {
193 const char *share, *prefix;
194 size_t share_len, prefix_len;
197 rc = dfs_cache_get_tgt_share(it, &share, &share_len, &prefix,
200 cifs_dbg(VFS, "%s: failed to parse target share %d\n",
205 extract_unc_hostname(share, &dfs_host, &dfs_host_len);
207 if (dfs_host_len != tcp_host_len
208 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
209 cifs_dbg(FYI, "%s: %.*s doesn't match %.*s\n",
211 (int)dfs_host_len, dfs_host,
212 (int)tcp_host_len, tcp_host);
214 rc = match_target_ip(server, dfs_host, dfs_host_len,
217 cifs_dbg(VFS, "%s: failed to match target ip: %d\n",
223 cifs_dbg(FYI, "%s: skipping target\n", __func__);
229 scnprintf(tree, MAX_TREE_SIZE, "\\\\%.*s\\IPC$",
230 (int)share_len, share);
231 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
233 scnprintf(tree, MAX_TREE_SIZE, "\\%.*s", (int)share_len,
235 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
237 rc = update_super_prepath(tcon, prefix,
248 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1,
253 dfs_cache_free_tgts(&tl);
259 static inline int __smb2_reconnect(const struct nls_table *nlsc,
260 struct cifs_tcon *tcon)
262 return SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
267 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
268 struct TCP_Server_Info *server)
271 struct nls_table *nls_codepage;
272 struct cifs_ses *ses;
276 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
277 * check for tcp and smb session status done differently
278 * for those three - in the calling routine.
283 if (smb2_command == SMB2_TREE_CONNECT)
286 if (tcon->tidStatus == CifsExiting) {
288 * only tree disconnect, open, and write,
289 * (and ulogoff which does not have tcon)
290 * are allowed as we start force umount.
292 if ((smb2_command != SMB2_WRITE) &&
293 (smb2_command != SMB2_CREATE) &&
294 (smb2_command != SMB2_TREE_DISCONNECT)) {
295 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
300 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
301 (!tcon->ses->server) || !server)
305 retries = server->nr_targets;
308 * Give demultiplex thread up to 10 seconds to each target available for
309 * reconnect -- should be greater than cifs socket timeout which is 7
312 while (server->tcpStatus == CifsNeedReconnect) {
314 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
315 * here since they are implicitly done when session drops.
317 switch (smb2_command) {
319 * BB Should we keep oplock break and add flush to exceptions?
321 case SMB2_TREE_DISCONNECT:
324 case SMB2_OPLOCK_BREAK:
328 rc = wait_event_interruptible_timeout(server->response_q,
329 (server->tcpStatus != CifsNeedReconnect),
332 cifs_dbg(FYI, "%s: aborting reconnect due to a received signal by the process\n",
337 /* are we still trying to reconnect? */
338 if (server->tcpStatus != CifsNeedReconnect)
341 if (retries && --retries)
345 * on "soft" mounts we wait once. Hard mounts keep
346 * retrying until process is killed or server comes
350 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
353 retries = server->nr_targets;
356 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
359 nls_codepage = load_nls_default();
362 * need to prevent multiple threads trying to simultaneously reconnect
363 * the same SMB session
365 mutex_lock(&tcon->ses->session_mutex);
368 * Recheck after acquire mutex. If another thread is negotiating
369 * and the server never sends an answer the socket will be closed
370 * and tcpStatus set to reconnect.
372 if (server->tcpStatus == CifsNeedReconnect) {
374 mutex_unlock(&tcon->ses->session_mutex);
379 * If we are reconnecting an extra channel, bind
381 if (server->is_channel) {
383 ses->binding_chan = cifs_ses_find_chan(ses, server);
386 rc = cifs_negotiate_protocol(0, tcon->ses);
387 if (!rc && tcon->ses->need_reconnect) {
388 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
389 if ((rc == -EACCES) && !tcon->retry) {
391 ses->binding = false;
392 ses->binding_chan = NULL;
393 mutex_unlock(&tcon->ses->session_mutex);
398 * End of channel binding
400 ses->binding = false;
401 ses->binding_chan = NULL;
403 if (rc || !tcon->need_reconnect) {
404 mutex_unlock(&tcon->ses->session_mutex);
408 cifs_mark_open_files_invalid(tcon);
409 if (tcon->use_persistent)
410 tcon->need_reopen_files = true;
412 rc = __smb2_reconnect(nls_codepage, tcon);
413 mutex_unlock(&tcon->ses->session_mutex);
415 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
417 /* If sess reconnected but tcon didn't, something strange ... */
418 pr_warn_once("reconnect tcon failed rc = %d\n", rc);
422 if (smb2_command != SMB2_INTERNAL_CMD)
423 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
425 atomic_inc(&tconInfoReconnectCount);
428 * Check if handle based operation so we know whether we can continue
429 * or not without returning to caller to reset file handle.
432 * BB Is flush done by server on drop of tcp session? Should we special
433 * case it and skip above?
435 switch (smb2_command) {
441 case SMB2_QUERY_DIRECTORY:
442 case SMB2_CHANGE_NOTIFY:
443 case SMB2_QUERY_INFO:
448 unload_nls(nls_codepage);
453 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon,
454 struct TCP_Server_Info *server,
456 unsigned int *total_len)
458 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
459 /* lookup word count ie StructureSize from table */
460 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
463 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
464 * largest operations (Create)
468 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon, server);
469 spdu->StructureSize2 = cpu_to_le16(parmsize);
471 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
475 * Allocate and return pointer to an SMB request hdr, and set basic
476 * SMB information in the SMB header. If the return code is zero, this
477 * function must have filled in request_buf pointer.
479 static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
480 struct TCP_Server_Info *server,
481 void **request_buf, unsigned int *total_len)
483 /* BB eventually switch this to SMB2 specific small buf size */
484 if (smb2_command == SMB2_SET_INFO)
485 *request_buf = cifs_buf_get();
487 *request_buf = cifs_small_buf_get();
488 if (*request_buf == NULL) {
489 /* BB should we add a retry in here if not a writepage? */
493 fill_small_buf(smb2_command, tcon, server,
494 (struct smb2_sync_hdr *)(*request_buf),
498 uint16_t com_code = le16_to_cpu(smb2_command);
499 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
500 cifs_stats_inc(&tcon->num_smbs_sent);
506 static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
507 struct TCP_Server_Info *server,
508 void **request_buf, unsigned int *total_len)
512 rc = smb2_reconnect(smb2_command, tcon, server);
516 return __smb2_plain_req_init(smb2_command, tcon, server, request_buf,
520 static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
521 struct TCP_Server_Info *server,
522 void **request_buf, unsigned int *total_len)
524 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
525 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
526 return __smb2_plain_req_init(SMB2_IOCTL, tcon, server,
527 request_buf, total_len);
529 return smb2_plain_req_init(SMB2_IOCTL, tcon, server,
530 request_buf, total_len);
533 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
536 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
538 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
539 pneg_ctxt->DataLength = cpu_to_le16(38);
540 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
541 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
542 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
543 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
547 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
549 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
550 pneg_ctxt->DataLength =
551 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
552 - sizeof(struct smb2_neg_context));
553 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
554 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
555 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
556 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
560 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
562 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
563 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + two ciphers */
564 pneg_ctxt->CipherCount = cpu_to_le16(2);
565 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
566 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
570 build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
572 struct nls_table *cp = load_nls_default();
574 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
576 /* copy up to max of first 100 bytes of server name to NetName field */
577 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
578 /* context size is DataLength + minimal smb2_neg_context */
579 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
580 sizeof(struct smb2_neg_context), 8) * 8;
584 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
586 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
587 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
588 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
589 pneg_ctxt->Name[0] = 0x93;
590 pneg_ctxt->Name[1] = 0xAD;
591 pneg_ctxt->Name[2] = 0x25;
592 pneg_ctxt->Name[3] = 0x50;
593 pneg_ctxt->Name[4] = 0x9C;
594 pneg_ctxt->Name[5] = 0xB4;
595 pneg_ctxt->Name[6] = 0x11;
596 pneg_ctxt->Name[7] = 0xE7;
597 pneg_ctxt->Name[8] = 0xB4;
598 pneg_ctxt->Name[9] = 0x23;
599 pneg_ctxt->Name[10] = 0x83;
600 pneg_ctxt->Name[11] = 0xDE;
601 pneg_ctxt->Name[12] = 0x96;
602 pneg_ctxt->Name[13] = 0x8B;
603 pneg_ctxt->Name[14] = 0xCD;
604 pneg_ctxt->Name[15] = 0x7C;
608 assemble_neg_contexts(struct smb2_negotiate_req *req,
609 struct TCP_Server_Info *server, unsigned int *total_len)
612 unsigned int ctxt_len;
614 if (*total_len > 200) {
615 /* In case length corrupted don't want to overrun smb buffer */
616 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
621 * round up total_len of fixed part of SMB3 negotiate request to 8
622 * byte boundary before adding negotiate contexts
624 *total_len = roundup(*total_len, 8);
626 pneg_ctxt = (*total_len) + (char *)req;
627 req->NegotiateContextOffset = cpu_to_le32(*total_len);
629 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
630 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
631 *total_len += ctxt_len;
632 pneg_ctxt += ctxt_len;
634 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
635 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
636 *total_len += ctxt_len;
637 pneg_ctxt += ctxt_len;
639 if (server->compress_algorithm) {
640 build_compression_ctxt((struct smb2_compression_capabilities_context *)
642 ctxt_len = DIV_ROUND_UP(
643 sizeof(struct smb2_compression_capabilities_context),
645 *total_len += ctxt_len;
646 pneg_ctxt += ctxt_len;
647 req->NegotiateContextCount = cpu_to_le16(5);
649 req->NegotiateContextCount = cpu_to_le16(4);
651 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
653 *total_len += ctxt_len;
654 pneg_ctxt += ctxt_len;
656 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
657 *total_len += sizeof(struct smb2_posix_neg_context);
660 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
662 unsigned int len = le16_to_cpu(ctxt->DataLength);
664 /* If invalid preauth context warn but use what we requested, SHA-512 */
665 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
666 pr_warn_once("server sent bad preauth context\n");
669 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
670 pr_warn_once("Invalid SMB3 hash algorithm count\n");
671 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
672 pr_warn_once("unknown SMB3 hash algorithm\n");
675 static void decode_compress_ctx(struct TCP_Server_Info *server,
676 struct smb2_compression_capabilities_context *ctxt)
678 unsigned int len = le16_to_cpu(ctxt->DataLength);
680 /* sizeof compress context is a one element compression capbility struct */
682 pr_warn_once("server sent bad compression cntxt\n");
685 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
686 pr_warn_once("Invalid SMB3 compress algorithm count\n");
689 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
690 pr_warn_once("unknown compression algorithm\n");
693 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
696 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
697 struct smb2_encryption_neg_context *ctxt)
699 unsigned int len = le16_to_cpu(ctxt->DataLength);
701 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
702 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
703 pr_warn_once("server sent bad crypto ctxt len\n");
707 if (le16_to_cpu(ctxt->CipherCount) != 1) {
708 pr_warn_once("Invalid SMB3.11 cipher count\n");
711 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
712 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
713 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
714 pr_warn_once("Invalid SMB3.11 cipher returned\n");
717 server->cipher_type = ctxt->Ciphers[0];
718 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
722 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
723 struct TCP_Server_Info *server,
724 unsigned int len_of_smb)
726 struct smb2_neg_context *pctx;
727 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
728 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
729 unsigned int len_of_ctxts, i;
732 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
733 if (len_of_smb <= offset) {
734 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
738 len_of_ctxts = len_of_smb - offset;
740 for (i = 0; i < ctxt_cnt; i++) {
742 /* check that offset is not beyond end of SMB */
743 if (len_of_ctxts == 0)
746 if (len_of_ctxts < sizeof(struct smb2_neg_context))
749 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
750 clen = le16_to_cpu(pctx->DataLength);
751 if (clen > len_of_ctxts)
754 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
755 decode_preauth_context(
756 (struct smb2_preauth_neg_context *)pctx);
757 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
758 rc = decode_encrypt_ctx(server,
759 (struct smb2_encryption_neg_context *)pctx);
760 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
761 decode_compress_ctx(server,
762 (struct smb2_compression_capabilities_context *)pctx);
763 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
764 server->posix_ext_supported = true;
766 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
767 le16_to_cpu(pctx->ContextType));
771 /* offsets must be 8 byte aligned */
772 clen = (clen + 7) & ~0x7;
773 offset += clen + sizeof(struct smb2_neg_context);
774 len_of_ctxts -= clen;
779 static struct create_posix *
780 create_posix_buf(umode_t mode)
782 struct create_posix *buf;
784 buf = kzalloc(sizeof(struct create_posix),
789 buf->ccontext.DataOffset =
790 cpu_to_le16(offsetof(struct create_posix, Mode));
791 buf->ccontext.DataLength = cpu_to_le32(4);
792 buf->ccontext.NameOffset =
793 cpu_to_le16(offsetof(struct create_posix, Name));
794 buf->ccontext.NameLength = cpu_to_le16(16);
796 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
807 buf->Name[10] = 0x83;
808 buf->Name[11] = 0xDE;
809 buf->Name[12] = 0x96;
810 buf->Name[13] = 0x8B;
811 buf->Name[14] = 0xCD;
812 buf->Name[15] = 0x7C;
813 buf->Mode = cpu_to_le32(mode);
814 cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
819 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
821 struct smb2_create_req *req = iov[0].iov_base;
822 unsigned int num = *num_iovec;
824 iov[num].iov_base = create_posix_buf(mode);
825 if (mode == ACL_NO_MODE)
826 cifs_dbg(FYI, "Invalid mode\n");
827 if (iov[num].iov_base == NULL)
829 iov[num].iov_len = sizeof(struct create_posix);
830 if (!req->CreateContextsOffset)
831 req->CreateContextsOffset = cpu_to_le32(
832 sizeof(struct smb2_create_req) +
833 iov[num - 1].iov_len);
834 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
835 *num_iovec = num + 1;
842 * SMB2 Worker functions follow:
844 * The general structure of the worker functions is:
845 * 1) Call smb2_init (assembles SMB2 header)
846 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
847 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
848 * 4) Decode SMB2 command specific fields in the fixed length area
849 * 5) Decode variable length data area (if any for this SMB2 command type)
850 * 6) Call free smb buffer
856 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
858 struct smb_rqst rqst;
859 struct smb2_negotiate_req *req;
860 struct smb2_negotiate_rsp *rsp;
865 struct TCP_Server_Info *server = cifs_ses_server(ses);
866 int blob_offset, blob_length;
868 int flags = CIFS_NEG_OP;
869 unsigned int total_len;
871 cifs_dbg(FYI, "Negotiate protocol\n");
874 WARN(1, "%s: server is NULL!\n", __func__);
878 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
879 (void **) &req, &total_len);
883 req->sync_hdr.SessionId = 0;
885 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
886 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
888 if (strcmp(server->vals->version_string,
889 SMB3ANY_VERSION_STRING) == 0) {
890 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
891 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
892 req->DialectCount = cpu_to_le16(2);
894 } else if (strcmp(server->vals->version_string,
895 SMBDEFAULT_VERSION_STRING) == 0) {
896 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
897 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
898 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
899 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
900 req->DialectCount = cpu_to_le16(4);
903 /* otherwise send specific dialect */
904 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
905 req->DialectCount = cpu_to_le16(1);
909 /* only one of SMB2 signing flags may be set in SMB2 request */
911 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
912 else if (global_secflags & CIFSSEC_MAY_SIGN)
913 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
915 req->SecurityMode = 0;
917 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
919 /* ClientGUID must be zero for SMB2.02 dialect */
920 if (server->vals->protocol_id == SMB20_PROT_ID)
921 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
923 memcpy(req->ClientGUID, server->client_guid,
924 SMB2_CLIENT_GUID_SIZE);
925 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
926 (strcmp(server->vals->version_string,
927 SMBDEFAULT_VERSION_STRING) == 0))
928 assemble_neg_contexts(req, server, &total_len);
930 iov[0].iov_base = (char *)req;
931 iov[0].iov_len = total_len;
933 memset(&rqst, 0, sizeof(struct smb_rqst));
937 rc = cifs_send_recv(xid, ses, server,
938 &rqst, &resp_buftype, flags, &rsp_iov);
939 cifs_small_buf_release(req);
940 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
942 * No tcon so can't do
943 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
945 if (rc == -EOPNOTSUPP) {
946 cifs_server_dbg(VFS, "Dialect not supported by server. Consider specifying vers=1.0 or vers=2.0 on mount for accessing older servers\n");
951 if (strcmp(server->vals->version_string,
952 SMB3ANY_VERSION_STRING) == 0) {
953 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
955 "SMB2 dialect returned but not requested\n");
957 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
959 "SMB2.1 dialect returned but not requested\n");
962 } else if (strcmp(server->vals->version_string,
963 SMBDEFAULT_VERSION_STRING) == 0) {
964 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
966 "SMB2 dialect returned but not requested\n");
968 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
969 /* ops set to 3.0 by default for default so update */
970 server->ops = &smb21_operations;
971 server->vals = &smb21_values;
972 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
973 server->ops = &smb311_operations;
974 server->vals = &smb311_values;
976 } else if (le16_to_cpu(rsp->DialectRevision) !=
977 server->vals->protocol_id) {
978 /* if requested single dialect ensure returned dialect matched */
979 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
980 le16_to_cpu(rsp->DialectRevision));
984 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
986 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
987 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
988 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
989 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
990 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
991 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
992 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
993 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
994 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
995 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
997 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
998 le16_to_cpu(rsp->DialectRevision));
1002 server->dialect = le16_to_cpu(rsp->DialectRevision);
1005 * Keep a copy of the hash after negprot. This hash will be
1006 * the starting hash value for all sessions made from this
1009 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
1010 SMB2_PREAUTH_HASH_SIZE);
1012 /* SMB2 only has an extended negflavor */
1013 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
1014 /* set it to the maximum buffer size value we can send with 1 credit */
1015 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
1016 SMB2_MAX_BUFFER_SIZE);
1017 server->max_read = le32_to_cpu(rsp->MaxReadSize);
1018 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
1019 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
1020 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
1021 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
1023 server->capabilities = le32_to_cpu(rsp->Capabilities);
1024 /* Internal types */
1025 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
1027 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
1028 (struct smb2_sync_hdr *)rsp);
1030 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
1032 * ses->sectype = RawNTLMSSP;
1033 * but for time being this is our only auth choice so doesn't matter.
1034 * We just found a server which sets blob length to zero expecting raw.
1036 if (blob_length == 0) {
1037 cifs_dbg(FYI, "missing security blob on negprot\n");
1038 server->sec_ntlmssp = true;
1041 rc = cifs_enable_signing(server, ses->sign);
1045 rc = decode_negTokenInit(security_blob, blob_length, server);
1052 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1053 if (rsp->NegotiateContextCount)
1054 rc = smb311_decode_neg_context(rsp, server,
1057 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
1060 free_rsp_buf(resp_buftype, rsp);
1064 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1067 struct validate_negotiate_info_req *pneg_inbuf;
1068 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
1070 u32 inbuflen; /* max of 4 dialects */
1071 struct TCP_Server_Info *server = tcon->ses->server;
1073 cifs_dbg(FYI, "validate negotiate\n");
1075 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1076 if (server->dialect == SMB311_PROT_ID)
1080 * validation ioctl must be signed, so no point sending this if we
1081 * can not sign it (ie are not known user). Even if signing is not
1082 * required (enabled but not negotiated), in those cases we selectively
1083 * sign just this, the first and only signed request on a connection.
1084 * Having validation of negotiate info helps reduce attack vectors.
1086 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1087 return 0; /* validation requires signing */
1089 if (tcon->ses->user_name == NULL) {
1090 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1091 return 0; /* validation requires signing */
1094 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1095 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1097 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1101 pneg_inbuf->Capabilities =
1102 cpu_to_le32(server->vals->req_capabilities);
1103 memcpy(pneg_inbuf->Guid, server->client_guid,
1104 SMB2_CLIENT_GUID_SIZE);
1106 if (tcon->ses->sign)
1107 pneg_inbuf->SecurityMode =
1108 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1109 else if (global_secflags & CIFSSEC_MAY_SIGN)
1110 pneg_inbuf->SecurityMode =
1111 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1113 pneg_inbuf->SecurityMode = 0;
1116 if (strcmp(server->vals->version_string,
1117 SMB3ANY_VERSION_STRING) == 0) {
1118 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1119 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1120 pneg_inbuf->DialectCount = cpu_to_le16(2);
1121 /* structure is big enough for 3 dialects, sending only 2 */
1122 inbuflen = sizeof(*pneg_inbuf) -
1123 (2 * sizeof(pneg_inbuf->Dialects[0]));
1124 } else if (strcmp(server->vals->version_string,
1125 SMBDEFAULT_VERSION_STRING) == 0) {
1126 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1127 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1128 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1129 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1130 pneg_inbuf->DialectCount = cpu_to_le16(4);
1131 /* structure is big enough for 3 dialects */
1132 inbuflen = sizeof(*pneg_inbuf);
1134 /* otherwise specific dialect was requested */
1135 pneg_inbuf->Dialects[0] =
1136 cpu_to_le16(server->vals->protocol_id);
1137 pneg_inbuf->DialectCount = cpu_to_le16(1);
1138 /* structure is big enough for 3 dialects, sending only 1 */
1139 inbuflen = sizeof(*pneg_inbuf) -
1140 sizeof(pneg_inbuf->Dialects[0]) * 2;
1143 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1144 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
1145 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1146 (char **)&pneg_rsp, &rsplen);
1147 if (rc == -EOPNOTSUPP) {
1149 * Old Windows versions or Netapp SMB server can return
1150 * not supported error. Client should accept it.
1152 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1154 goto out_free_inbuf;
1155 } else if (rc != 0) {
1156 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1159 goto out_free_inbuf;
1163 if (rsplen != sizeof(*pneg_rsp)) {
1164 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1167 /* relax check since Mac returns max bufsize allowed on ioctl */
1168 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1172 /* check validate negotiate info response matches what we got earlier */
1173 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1176 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1179 /* do not validate server guid because not saved at negprot time yet */
1181 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1182 SMB2_LARGE_FILES) != server->capabilities)
1185 /* validate negotiate successful */
1187 cifs_dbg(FYI, "validate negotiate info successful\n");
1191 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1200 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1202 switch (requested) {
1209 if (server->sec_ntlmssp &&
1210 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1212 if ((server->sec_kerberos || server->sec_mskerberos) &&
1213 (global_secflags & CIFSSEC_MAY_KRB5))
1221 struct SMB2_sess_data {
1223 struct cifs_ses *ses;
1224 struct nls_table *nls_cp;
1225 void (*func)(struct SMB2_sess_data *);
1227 u64 previous_session;
1229 /* we will send the SMB in three pieces:
1230 * a fixed length beginning part, an optional
1231 * SPNEGO blob (which can be zero length), and a
1232 * last part which will include the strings
1233 * and rest of bcc area. This allows us to avoid
1234 * a large buffer 17K allocation
1241 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1244 struct cifs_ses *ses = sess_data->ses;
1245 struct smb2_sess_setup_req *req;
1246 struct TCP_Server_Info *server = cifs_ses_server(ses);
1247 unsigned int total_len;
1249 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
1255 if (sess_data->ses->binding) {
1256 req->sync_hdr.SessionId = sess_data->ses->Suid;
1257 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1258 req->PreviousSessionId = 0;
1259 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1261 /* First session, not a reauthenticate */
1262 req->sync_hdr.SessionId = 0;
1264 * if reconnect, we need to send previous sess id
1267 req->PreviousSessionId = sess_data->previous_session;
1268 req->Flags = 0; /* MBZ */
1271 /* enough to enable echos and oplocks and one max size write */
1272 req->sync_hdr.CreditRequest = cpu_to_le16(130);
1274 /* only one of SMB2 signing flags may be set in SMB2 request */
1276 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1277 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1278 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1280 req->SecurityMode = 0;
1282 #ifdef CONFIG_CIFS_DFS_UPCALL
1283 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1285 req->Capabilities = 0;
1286 #endif /* DFS_UPCALL */
1288 req->Channel = 0; /* MBZ */
1290 sess_data->iov[0].iov_base = (char *)req;
1292 sess_data->iov[0].iov_len = total_len - 1;
1294 * This variable will be used to clear the buffer
1295 * allocated above in case of any error in the calling function.
1297 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1303 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1305 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1306 sess_data->buf0_type = CIFS_NO_BUFFER;
1310 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1313 struct smb_rqst rqst;
1314 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1315 struct kvec rsp_iov = { NULL, 0 };
1317 /* Testing shows that buffer offset must be at location of Buffer[0] */
1318 req->SecurityBufferOffset =
1319 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1320 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1322 memset(&rqst, 0, sizeof(struct smb_rqst));
1323 rqst.rq_iov = sess_data->iov;
1326 /* BB add code to build os and lm fields */
1327 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1328 cifs_ses_server(sess_data->ses),
1330 &sess_data->buf0_type,
1331 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
1332 cifs_small_buf_release(sess_data->iov[0].iov_base);
1333 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1339 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1342 struct cifs_ses *ses = sess_data->ses;
1343 struct TCP_Server_Info *server = cifs_ses_server(ses);
1345 mutex_lock(&server->srv_mutex);
1346 if (server->ops->generate_signingkey) {
1347 rc = server->ops->generate_signingkey(ses);
1350 "SMB3 session key generation failed\n");
1351 mutex_unlock(&server->srv_mutex);
1355 if (!server->session_estab) {
1356 server->sequence_number = 0x2;
1357 server->session_estab = true;
1359 mutex_unlock(&server->srv_mutex);
1361 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1362 /* keep existing ses state if binding */
1363 if (!ses->binding) {
1364 spin_lock(&GlobalMid_Lock);
1365 ses->status = CifsGood;
1366 ses->need_reconnect = false;
1367 spin_unlock(&GlobalMid_Lock);
1373 #ifdef CONFIG_CIFS_UPCALL
1375 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1378 struct cifs_ses *ses = sess_data->ses;
1379 struct cifs_spnego_msg *msg;
1380 struct key *spnego_key = NULL;
1381 struct smb2_sess_setup_rsp *rsp = NULL;
1383 rc = SMB2_sess_alloc_buffer(sess_data);
1387 spnego_key = cifs_get_spnego_key(ses);
1388 if (IS_ERR(spnego_key)) {
1389 rc = PTR_ERR(spnego_key);
1394 msg = spnego_key->payload.data[0];
1396 * check version field to make sure that cifs.upcall is
1397 * sending us a response in an expected form
1399 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1400 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1401 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1403 goto out_put_spnego_key;
1406 /* keep session key if binding */
1407 if (!ses->binding) {
1408 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1410 if (!ses->auth_key.response) {
1411 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1414 goto out_put_spnego_key;
1416 ses->auth_key.len = msg->sesskey_len;
1419 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1420 sess_data->iov[1].iov_len = msg->secblob_len;
1422 rc = SMB2_sess_sendreceive(sess_data);
1424 goto out_put_spnego_key;
1426 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1427 /* keep session id and flags if binding */
1428 if (!ses->binding) {
1429 ses->Suid = rsp->sync_hdr.SessionId;
1430 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1433 rc = SMB2_sess_establish_session(sess_data);
1435 key_invalidate(spnego_key);
1436 key_put(spnego_key);
1438 sess_data->result = rc;
1439 sess_data->func = NULL;
1440 SMB2_sess_free_buffer(sess_data);
1444 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1446 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1447 sess_data->result = -EOPNOTSUPP;
1448 sess_data->func = NULL;
1453 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1456 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1459 struct cifs_ses *ses = sess_data->ses;
1460 struct smb2_sess_setup_rsp *rsp = NULL;
1461 char *ntlmssp_blob = NULL;
1462 bool use_spnego = false; /* else use raw ntlmssp */
1463 u16 blob_length = 0;
1466 * If memory allocation is successful, caller of this function
1469 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1470 if (!ses->ntlmssp) {
1474 ses->ntlmssp->sesskey_per_smbsess = true;
1476 rc = SMB2_sess_alloc_buffer(sess_data);
1480 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1482 if (ntlmssp_blob == NULL) {
1487 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1489 /* BB eventually need to add this */
1490 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1494 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1495 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1497 sess_data->iov[1].iov_base = ntlmssp_blob;
1498 sess_data->iov[1].iov_len = blob_length;
1500 rc = SMB2_sess_sendreceive(sess_data);
1501 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1503 /* If true, rc here is expected and not an error */
1504 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1505 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1511 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1512 le16_to_cpu(rsp->SecurityBufferOffset)) {
1513 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1514 le16_to_cpu(rsp->SecurityBufferOffset));
1518 rc = decode_ntlmssp_challenge(rsp->Buffer,
1519 le16_to_cpu(rsp->SecurityBufferLength), ses);
1523 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1525 /* keep existing ses id and flags if binding */
1526 if (!ses->binding) {
1527 ses->Suid = rsp->sync_hdr.SessionId;
1528 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1532 kfree(ntlmssp_blob);
1533 SMB2_sess_free_buffer(sess_data);
1535 sess_data->result = 0;
1536 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1540 kfree(ses->ntlmssp);
1541 ses->ntlmssp = NULL;
1542 sess_data->result = rc;
1543 sess_data->func = NULL;
1547 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1550 struct cifs_ses *ses = sess_data->ses;
1551 struct smb2_sess_setup_req *req;
1552 struct smb2_sess_setup_rsp *rsp = NULL;
1553 unsigned char *ntlmssp_blob = NULL;
1554 bool use_spnego = false; /* else use raw ntlmssp */
1555 u16 blob_length = 0;
1557 rc = SMB2_sess_alloc_buffer(sess_data);
1561 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1562 req->sync_hdr.SessionId = ses->Suid;
1564 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1567 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1572 /* BB eventually need to add this */
1573 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1577 sess_data->iov[1].iov_base = ntlmssp_blob;
1578 sess_data->iov[1].iov_len = blob_length;
1580 rc = SMB2_sess_sendreceive(sess_data);
1584 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1586 /* keep existing ses id and flags if binding */
1587 if (!ses->binding) {
1588 ses->Suid = rsp->sync_hdr.SessionId;
1589 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1592 rc = SMB2_sess_establish_session(sess_data);
1593 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1594 if (ses->server->dialect < SMB30_PROT_ID) {
1595 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1597 * The session id is opaque in terms of endianness, so we can't
1598 * print it as a long long. we dump it as we got it on the wire
1600 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1602 cifs_dbg(VFS, "Session Key %*ph\n",
1603 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1604 cifs_dbg(VFS, "Signing Key %*ph\n",
1605 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1609 kfree(ntlmssp_blob);
1610 SMB2_sess_free_buffer(sess_data);
1611 kfree(ses->ntlmssp);
1612 ses->ntlmssp = NULL;
1613 sess_data->result = rc;
1614 sess_data->func = NULL;
1618 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1622 type = smb2_select_sectype(cifs_ses_server(ses), ses->sectype);
1623 cifs_dbg(FYI, "sess setup type %d\n", type);
1624 if (type == Unspecified) {
1625 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1631 sess_data->func = SMB2_auth_kerberos;
1634 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1637 cifs_dbg(VFS, "secType %d not supported!\n", type);
1645 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1646 const struct nls_table *nls_cp)
1649 struct TCP_Server_Info *server = cifs_ses_server(ses);
1650 struct SMB2_sess_data *sess_data;
1652 cifs_dbg(FYI, "Session Setup\n");
1655 WARN(1, "%s: server is NULL!\n", __func__);
1659 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1663 rc = SMB2_select_sec(ses, sess_data);
1666 sess_data->xid = xid;
1667 sess_data->ses = ses;
1668 sess_data->buf0_type = CIFS_NO_BUFFER;
1669 sess_data->nls_cp = (struct nls_table *) nls_cp;
1670 sess_data->previous_session = ses->Suid;
1673 * Initialize the session hash with the server one.
1675 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
1676 SMB2_PREAUTH_HASH_SIZE);
1678 while (sess_data->func)
1679 sess_data->func(sess_data);
1681 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1682 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1683 rc = sess_data->result;
1690 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1692 struct smb_rqst rqst;
1693 struct smb2_logoff_req *req; /* response is also trivial struct */
1695 struct TCP_Server_Info *server;
1697 unsigned int total_len;
1699 struct kvec rsp_iov;
1702 cifs_dbg(FYI, "disconnect session %p\n", ses);
1704 if (ses && (ses->server))
1705 server = ses->server;
1709 /* no need to send SMB logoff if uid already closed due to reconnect */
1710 if (ses->need_reconnect)
1711 goto smb2_session_already_dead;
1713 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
1714 (void **) &req, &total_len);
1718 /* since no tcon, smb2_init can not do this, so do here */
1719 req->sync_hdr.SessionId = ses->Suid;
1721 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1722 flags |= CIFS_TRANSFORM_REQ;
1723 else if (server->sign)
1724 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1726 flags |= CIFS_NO_RSP_BUF;
1728 iov[0].iov_base = (char *)req;
1729 iov[0].iov_len = total_len;
1731 memset(&rqst, 0, sizeof(struct smb_rqst));
1735 rc = cifs_send_recv(xid, ses, ses->server,
1736 &rqst, &resp_buf_type, flags, &rsp_iov);
1737 cifs_small_buf_release(req);
1739 * No tcon so can't do
1740 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1743 smb2_session_already_dead:
1747 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1749 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1752 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1754 /* These are similar values to what Windows uses */
1755 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1757 tcon->max_chunks = 256;
1758 tcon->max_bytes_chunk = 1048576;
1759 tcon->max_bytes_copy = 16777216;
1763 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1764 struct cifs_tcon *tcon, const struct nls_table *cp)
1766 struct smb_rqst rqst;
1767 struct smb2_tree_connect_req *req;
1768 struct smb2_tree_connect_rsp *rsp = NULL;
1770 struct kvec rsp_iov = { NULL, 0 };
1774 __le16 *unc_path = NULL;
1776 unsigned int total_len;
1777 struct TCP_Server_Info *server;
1779 /* always use master channel */
1780 server = ses->server;
1782 cifs_dbg(FYI, "TCON\n");
1784 if (!server || !tree)
1787 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1788 if (unc_path == NULL)
1791 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1793 if (unc_path_len < 2) {
1798 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1800 atomic_set(&tcon->num_remote_opens, 0);
1801 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
1802 (void **) &req, &total_len);
1808 if (smb3_encryption_required(tcon))
1809 flags |= CIFS_TRANSFORM_REQ;
1811 iov[0].iov_base = (char *)req;
1813 iov[0].iov_len = total_len - 1;
1815 /* Testing shows that buffer offset must be at location of Buffer[0] */
1816 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1818 req->PathLength = cpu_to_le16(unc_path_len - 2);
1819 iov[1].iov_base = unc_path;
1820 iov[1].iov_len = unc_path_len;
1823 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1824 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1825 * (Samba servers don't always set the flag so also check if null user)
1827 if ((server->dialect == SMB311_PROT_ID) &&
1828 !smb3_encryption_required(tcon) &&
1829 !(ses->session_flags &
1830 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1831 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1832 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1834 memset(&rqst, 0, sizeof(struct smb_rqst));
1838 /* Need 64 for max size write so ask for more in case not there yet */
1839 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1841 rc = cifs_send_recv(xid, ses, server,
1842 &rqst, &resp_buftype, flags, &rsp_iov);
1843 cifs_small_buf_release(req);
1844 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1845 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1848 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1849 tcon->need_reconnect = true;
1851 goto tcon_error_exit;
1854 switch (rsp->ShareType) {
1855 case SMB2_SHARE_TYPE_DISK:
1856 cifs_dbg(FYI, "connection to disk share\n");
1858 case SMB2_SHARE_TYPE_PIPE:
1860 cifs_dbg(FYI, "connection to pipe share\n");
1862 case SMB2_SHARE_TYPE_PRINT:
1864 cifs_dbg(FYI, "connection to printer\n");
1867 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1869 goto tcon_error_exit;
1872 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1873 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1874 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1875 tcon->tidStatus = CifsGood;
1876 tcon->need_reconnect = false;
1877 tcon->tid = rsp->sync_hdr.TreeId;
1878 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1880 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1881 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1882 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
1885 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1886 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
1888 init_copy_chunk_defaults(tcon);
1889 if (server->ops->validate_negotiate)
1890 rc = server->ops->validate_negotiate(xid, tcon);
1893 free_rsp_buf(resp_buftype, rsp);
1898 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1899 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1905 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1907 struct smb_rqst rqst;
1908 struct smb2_tree_disconnect_req *req; /* response is trivial */
1910 struct cifs_ses *ses = tcon->ses;
1912 unsigned int total_len;
1914 struct kvec rsp_iov;
1917 cifs_dbg(FYI, "Tree Disconnect\n");
1919 if (!ses || !(ses->server))
1922 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1925 close_shroot_lease(&tcon->crfid);
1927 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, ses->server,
1933 if (smb3_encryption_required(tcon))
1934 flags |= CIFS_TRANSFORM_REQ;
1936 flags |= CIFS_NO_RSP_BUF;
1938 iov[0].iov_base = (char *)req;
1939 iov[0].iov_len = total_len;
1941 memset(&rqst, 0, sizeof(struct smb_rqst));
1945 rc = cifs_send_recv(xid, ses, ses->server,
1946 &rqst, &resp_buf_type, flags, &rsp_iov);
1947 cifs_small_buf_release(req);
1949 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1955 static struct create_durable *
1956 create_durable_buf(void)
1958 struct create_durable *buf;
1960 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1964 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1965 (struct create_durable, Data));
1966 buf->ccontext.DataLength = cpu_to_le32(16);
1967 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1968 (struct create_durable, Name));
1969 buf->ccontext.NameLength = cpu_to_le16(4);
1970 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1978 static struct create_durable *
1979 create_reconnect_durable_buf(struct cifs_fid *fid)
1981 struct create_durable *buf;
1983 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1987 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1988 (struct create_durable, Data));
1989 buf->ccontext.DataLength = cpu_to_le32(16);
1990 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1991 (struct create_durable, Name));
1992 buf->ccontext.NameLength = cpu_to_le16(4);
1993 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1994 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1995 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
2004 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
2006 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
2008 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
2009 pdisk_id->DiskFileId, pdisk_id->VolumeId);
2010 buf->IndexNumber = pdisk_id->DiskFileId;
2014 parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
2015 struct create_posix_rsp *posix)
2018 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
2019 u8 *end = beg + le32_to_cpu(cc->DataLength);
2022 memset(posix, 0, sizeof(*posix));
2024 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
2025 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
2026 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
2029 sid_len = posix_info_sid_size(sid, end);
2031 cifs_dbg(VFS, "bad owner sid in posix create response\n");
2034 memcpy(&posix->owner, sid, sid_len);
2036 sid = sid + sid_len;
2037 sid_len = posix_info_sid_size(sid, end);
2039 cifs_dbg(VFS, "bad group sid in posix create response\n");
2042 memcpy(&posix->group, sid, sid_len);
2044 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
2045 posix->nlink, posix->mode, posix->reparse_tag);
2049 smb2_parse_contexts(struct TCP_Server_Info *server,
2050 struct smb2_create_rsp *rsp,
2051 unsigned int *epoch, char *lease_key, __u8 *oplock,
2052 struct smb2_file_all_info *buf,
2053 struct create_posix_rsp *posix)
2056 struct create_context *cc;
2058 unsigned int remaining;
2060 const char smb3_create_tag_posix[] = {0x93, 0xAD, 0x25, 0x50, 0x9C,
2061 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
2062 0xDE, 0x96, 0x8B, 0xCD, 0x7C};
2065 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
2066 remaining = le32_to_cpu(rsp->CreateContextsLength);
2067 cc = (struct create_context *)data_offset;
2069 /* Initialize inode number to 0 in case no valid data in qfid context */
2071 buf->IndexNumber = 0;
2073 while (remaining >= sizeof(struct create_context)) {
2074 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
2075 if (le16_to_cpu(cc->NameLength) == 4 &&
2076 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
2077 *oplock = server->ops->parse_lease_buf(cc, epoch,
2079 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
2080 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
2081 parse_query_id_ctxt(cc, buf);
2082 else if ((le16_to_cpu(cc->NameLength) == 16)) {
2084 memcmp(name, smb3_create_tag_posix, 16) == 0)
2085 parse_posix_ctxt(cc, buf, posix);
2088 cifs_dbg(FYI, "Context not matched with len %d\n",
2089 le16_to_cpu(cc->NameLength));
2090 cifs_dump_mem("Cctxt name: ", name, 4);
2093 next = le32_to_cpu(cc->Next);
2097 cc = (struct create_context *)((char *)cc + next);
2100 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2101 *oplock = rsp->OplockLevel;
2107 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
2108 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
2110 struct smb2_create_req *req = iov[0].iov_base;
2111 unsigned int num = *num_iovec;
2113 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
2114 if (iov[num].iov_base == NULL)
2116 iov[num].iov_len = server->vals->create_lease_size;
2117 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2118 if (!req->CreateContextsOffset)
2119 req->CreateContextsOffset = cpu_to_le32(
2120 sizeof(struct smb2_create_req) +
2121 iov[num - 1].iov_len);
2122 le32_add_cpu(&req->CreateContextsLength,
2123 server->vals->create_lease_size);
2124 *num_iovec = num + 1;
2128 static struct create_durable_v2 *
2129 create_durable_v2_buf(struct cifs_open_parms *oparms)
2131 struct cifs_fid *pfid = oparms->fid;
2132 struct create_durable_v2 *buf;
2134 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2138 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2139 (struct create_durable_v2, dcontext));
2140 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2141 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2142 (struct create_durable_v2, Name));
2143 buf->ccontext.NameLength = cpu_to_le16(4);
2146 * NB: Handle timeout defaults to 0, which allows server to choose
2147 * (most servers default to 120 seconds) and most clients default to 0.
2148 * This can be overridden at mount ("handletimeout=") if the user wants
2149 * a different persistent (or resilient) handle timeout for all opens
2150 * opens on a particular SMB3 mount.
2152 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2153 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2154 generate_random_uuid(buf->dcontext.CreateGuid);
2155 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2157 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2165 static struct create_durable_handle_reconnect_v2 *
2166 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2168 struct create_durable_handle_reconnect_v2 *buf;
2170 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2175 buf->ccontext.DataOffset =
2176 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2178 buf->ccontext.DataLength =
2179 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2180 buf->ccontext.NameOffset =
2181 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2183 buf->ccontext.NameLength = cpu_to_le16(4);
2185 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2186 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2187 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2188 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2190 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2199 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2200 struct cifs_open_parms *oparms)
2202 struct smb2_create_req *req = iov[0].iov_base;
2203 unsigned int num = *num_iovec;
2205 iov[num].iov_base = create_durable_v2_buf(oparms);
2206 if (iov[num].iov_base == NULL)
2208 iov[num].iov_len = sizeof(struct create_durable_v2);
2209 if (!req->CreateContextsOffset)
2210 req->CreateContextsOffset =
2211 cpu_to_le32(sizeof(struct smb2_create_req) +
2213 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
2214 *num_iovec = num + 1;
2219 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2220 struct cifs_open_parms *oparms)
2222 struct smb2_create_req *req = iov[0].iov_base;
2223 unsigned int num = *num_iovec;
2225 /* indicate that we don't need to relock the file */
2226 oparms->reconnect = false;
2228 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2229 if (iov[num].iov_base == NULL)
2231 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2232 if (!req->CreateContextsOffset)
2233 req->CreateContextsOffset =
2234 cpu_to_le32(sizeof(struct smb2_create_req) +
2236 le32_add_cpu(&req->CreateContextsLength,
2237 sizeof(struct create_durable_handle_reconnect_v2));
2238 *num_iovec = num + 1;
2243 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2244 struct cifs_open_parms *oparms, bool use_persistent)
2246 struct smb2_create_req *req = iov[0].iov_base;
2247 unsigned int num = *num_iovec;
2249 if (use_persistent) {
2250 if (oparms->reconnect)
2251 return add_durable_reconnect_v2_context(iov, num_iovec,
2254 return add_durable_v2_context(iov, num_iovec, oparms);
2257 if (oparms->reconnect) {
2258 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2259 /* indicate that we don't need to relock the file */
2260 oparms->reconnect = false;
2262 iov[num].iov_base = create_durable_buf();
2263 if (iov[num].iov_base == NULL)
2265 iov[num].iov_len = sizeof(struct create_durable);
2266 if (!req->CreateContextsOffset)
2267 req->CreateContextsOffset =
2268 cpu_to_le32(sizeof(struct smb2_create_req) +
2270 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
2271 *num_iovec = num + 1;
2275 /* See MS-SMB2 2.2.13.2.7 */
2276 static struct crt_twarp_ctxt *
2277 create_twarp_buf(__u64 timewarp)
2279 struct crt_twarp_ctxt *buf;
2281 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2285 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2286 (struct crt_twarp_ctxt, Timestamp));
2287 buf->ccontext.DataLength = cpu_to_le32(8);
2288 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2289 (struct crt_twarp_ctxt, Name));
2290 buf->ccontext.NameLength = cpu_to_le16(4);
2291 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2296 buf->Timestamp = cpu_to_le64(timewarp);
2300 /* See MS-SMB2 2.2.13.2.7 */
2302 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2304 struct smb2_create_req *req = iov[0].iov_base;
2305 unsigned int num = *num_iovec;
2307 iov[num].iov_base = create_twarp_buf(timewarp);
2308 if (iov[num].iov_base == NULL)
2310 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2311 if (!req->CreateContextsOffset)
2312 req->CreateContextsOffset = cpu_to_le32(
2313 sizeof(struct smb2_create_req) +
2314 iov[num - 1].iov_len);
2315 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2316 *num_iovec = num + 1;
2320 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2321 static struct crt_sd_ctxt *
2322 create_sd_buf(umode_t mode, unsigned int *len)
2324 struct crt_sd_ctxt *buf;
2325 struct cifs_ace *pace;
2326 unsigned int sdlen, acelen;
2328 *len = roundup(sizeof(struct crt_sd_ctxt) + sizeof(struct cifs_ace) * 2,
2330 buf = kzalloc(*len, GFP_KERNEL);
2334 sdlen = sizeof(struct smb3_sd) + sizeof(struct smb3_acl) +
2335 2 * sizeof(struct cifs_ace);
2337 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2338 (struct crt_sd_ctxt, sd));
2339 buf->ccontext.DataLength = cpu_to_le32(sdlen);
2340 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2341 (struct crt_sd_ctxt, Name));
2342 buf->ccontext.NameLength = cpu_to_le16(4);
2343 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2348 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2350 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2351 * and "DP" ie the DACL is present
2353 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2355 /* offset owner, group and Sbz1 and SACL are all zero */
2356 buf->sd.OffsetDacl = cpu_to_le32(sizeof(struct smb3_sd));
2357 buf->acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2359 /* create one ACE to hold the mode embedded in reserved special SID */
2360 pace = (struct cifs_ace *)(sizeof(struct crt_sd_ctxt) + (char *)buf);
2361 acelen = setup_special_mode_ACE(pace, (__u64)mode);
2362 /* and one more ACE to allow access for authenticated users */
2363 pace = (struct cifs_ace *)(acelen + (sizeof(struct crt_sd_ctxt) +
2365 acelen += setup_authusers_ACE(pace);
2366 buf->acl.AclSize = cpu_to_le16(sizeof(struct cifs_acl) + acelen);
2367 buf->acl.AceCount = cpu_to_le16(2);
2372 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
2374 struct smb2_create_req *req = iov[0].iov_base;
2375 unsigned int num = *num_iovec;
2376 unsigned int len = 0;
2378 iov[num].iov_base = create_sd_buf(mode, &len);
2379 if (iov[num].iov_base == NULL)
2381 iov[num].iov_len = len;
2382 if (!req->CreateContextsOffset)
2383 req->CreateContextsOffset = cpu_to_le32(
2384 sizeof(struct smb2_create_req) +
2385 iov[num - 1].iov_len);
2386 le32_add_cpu(&req->CreateContextsLength, len);
2387 *num_iovec = num + 1;
2391 static struct crt_query_id_ctxt *
2392 create_query_id_buf(void)
2394 struct crt_query_id_ctxt *buf;
2396 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2400 buf->ccontext.DataOffset = cpu_to_le16(0);
2401 buf->ccontext.DataLength = cpu_to_le32(0);
2402 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2403 (struct crt_query_id_ctxt, Name));
2404 buf->ccontext.NameLength = cpu_to_le16(4);
2405 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2413 /* See MS-SMB2 2.2.13.2.9 */
2415 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2417 struct smb2_create_req *req = iov[0].iov_base;
2418 unsigned int num = *num_iovec;
2420 iov[num].iov_base = create_query_id_buf();
2421 if (iov[num].iov_base == NULL)
2423 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2424 if (!req->CreateContextsOffset)
2425 req->CreateContextsOffset = cpu_to_le32(
2426 sizeof(struct smb2_create_req) +
2427 iov[num - 1].iov_len);
2428 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2429 *num_iovec = num + 1;
2434 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2435 const char *treename, const __le16 *path)
2437 int treename_len, path_len;
2438 struct nls_table *cp;
2439 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2444 treename_len = strlen(treename);
2445 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2451 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2454 * make room for one path separator between the treename and
2457 *out_len = treename_len + 1 + path_len;
2460 * final path needs to be null-terminated UTF16 with a
2464 *out_size = roundup((*out_len+1)*2, 8);
2465 *out_path = kzalloc(*out_size, GFP_KERNEL);
2469 cp = load_nls_default();
2470 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2471 UniStrcat(*out_path, sep);
2472 UniStrcat(*out_path, path);
2478 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2479 umode_t mode, struct cifs_tcon *tcon,
2480 const char *full_path,
2481 struct cifs_sb_info *cifs_sb)
2483 struct smb_rqst rqst;
2484 struct smb2_create_req *req;
2485 struct smb2_create_rsp *rsp = NULL;
2486 struct cifs_ses *ses = tcon->ses;
2487 struct kvec iov[3]; /* make sure at least one for each open context */
2488 struct kvec rsp_iov = {NULL, 0};
2491 __le16 *copy_path = NULL;
2494 unsigned int n_iov = 2;
2495 __u32 file_attributes = 0;
2496 char *pc_buf = NULL;
2498 unsigned int total_len;
2499 __le16 *utf16_path = NULL;
2500 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2502 cifs_dbg(FYI, "mkdir\n");
2504 /* resource #1: path allocation */
2505 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2509 if (!ses || !server) {
2514 /* resource #2: request */
2515 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2516 (void **) &req, &total_len);
2521 if (smb3_encryption_required(tcon))
2522 flags |= CIFS_TRANSFORM_REQ;
2524 req->ImpersonationLevel = IL_IMPERSONATION;
2525 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2526 /* File attributes ignored on open (used in create though) */
2527 req->FileAttributes = cpu_to_le32(file_attributes);
2528 req->ShareAccess = FILE_SHARE_ALL_LE;
2529 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2530 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2532 iov[0].iov_base = (char *)req;
2533 /* -1 since last byte is buf[0] which is sent below (path) */
2534 iov[0].iov_len = total_len - 1;
2536 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2538 /* [MS-SMB2] 2.2.13 NameOffset:
2539 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2540 * the SMB2 header, the file name includes a prefix that will
2541 * be processed during DFS name normalization as specified in
2542 * section 3.3.5.9. Otherwise, the file name is relative to
2543 * the share that is identified by the TreeId in the SMB2
2546 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2549 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2550 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2552 tcon->treeName, utf16_path);
2556 req->NameLength = cpu_to_le16(name_len * 2);
2557 uni_path_len = copy_size;
2558 /* free before overwriting resource */
2560 utf16_path = copy_path;
2562 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2563 /* MUST set path len (NameLength) to 0 opening root of share */
2564 req->NameLength = cpu_to_le16(uni_path_len - 2);
2565 if (uni_path_len % 8 != 0) {
2566 copy_size = roundup(uni_path_len, 8);
2567 copy_path = kzalloc(copy_size, GFP_KERNEL);
2572 memcpy((char *)copy_path, (const char *)utf16_path,
2574 uni_path_len = copy_size;
2575 /* free before overwriting resource */
2577 utf16_path = copy_path;
2581 iov[1].iov_len = uni_path_len;
2582 iov[1].iov_base = utf16_path;
2583 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2585 if (tcon->posix_extensions) {
2586 /* resource #3: posix buf */
2587 rc = add_posix_context(iov, &n_iov, mode);
2590 pc_buf = iov[n_iov-1].iov_base;
2594 memset(&rqst, 0, sizeof(struct smb_rqst));
2596 rqst.rq_nvec = n_iov;
2598 /* no need to inc num_remote_opens because we close it just below */
2599 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2600 FILE_WRITE_ATTRIBUTES);
2601 /* resource #4: response buffer */
2602 rc = cifs_send_recv(xid, ses, server,
2603 &rqst, &resp_buftype, flags, &rsp_iov);
2605 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2606 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2608 FILE_WRITE_ATTRIBUTES, rc);
2609 goto err_free_rsp_buf;
2612 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2613 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2614 ses->Suid, CREATE_NOT_FILE,
2615 FILE_WRITE_ATTRIBUTES);
2617 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2619 /* Eventually save off posix specific response info and timestaps */
2622 free_rsp_buf(resp_buftype, rsp);
2625 cifs_small_buf_release(req);
2632 SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2633 struct smb_rqst *rqst, __u8 *oplock,
2634 struct cifs_open_parms *oparms, __le16 *path)
2636 struct smb2_create_req *req;
2637 unsigned int n_iov = 2;
2638 __u32 file_attributes = 0;
2641 unsigned int total_len;
2642 struct kvec *iov = rqst->rq_iov;
2646 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2647 (void **) &req, &total_len);
2651 iov[0].iov_base = (char *)req;
2652 /* -1 since last byte is buf[0] which is sent below (path) */
2653 iov[0].iov_len = total_len - 1;
2655 if (oparms->create_options & CREATE_OPTION_READONLY)
2656 file_attributes |= ATTR_READONLY;
2657 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2658 file_attributes |= ATTR_SYSTEM;
2660 req->ImpersonationLevel = IL_IMPERSONATION;
2661 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2662 /* File attributes ignored on open (used in create though) */
2663 req->FileAttributes = cpu_to_le32(file_attributes);
2664 req->ShareAccess = FILE_SHARE_ALL_LE;
2666 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2667 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2668 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2670 /* [MS-SMB2] 2.2.13 NameOffset:
2671 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2672 * the SMB2 header, the file name includes a prefix that will
2673 * be processed during DFS name normalization as specified in
2674 * section 3.3.5.9. Otherwise, the file name is relative to
2675 * the share that is identified by the TreeId in the SMB2
2678 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2681 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2682 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2684 tcon->treeName, path);
2687 req->NameLength = cpu_to_le16(name_len * 2);
2688 uni_path_len = copy_size;
2691 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2692 /* MUST set path len (NameLength) to 0 opening root of share */
2693 req->NameLength = cpu_to_le16(uni_path_len - 2);
2694 copy_size = uni_path_len;
2695 if (copy_size % 8 != 0)
2696 copy_size = roundup(copy_size, 8);
2697 copy_path = kzalloc(copy_size, GFP_KERNEL);
2700 memcpy((char *)copy_path, (const char *)path,
2702 uni_path_len = copy_size;
2706 iov[1].iov_len = uni_path_len;
2707 iov[1].iov_base = path;
2709 if ((!server->oplocks) || (tcon->no_lease))
2710 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2712 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2713 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2714 req->RequestedOplockLevel = *oplock;
2715 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2716 (oparms->create_options & CREATE_NOT_FILE))
2717 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2719 rc = add_lease_context(server, iov, &n_iov,
2720 oparms->fid->lease_key, oplock);
2725 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2726 /* need to set Next field of lease context if we request it */
2727 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2728 struct create_context *ccontext =
2729 (struct create_context *)iov[n_iov-1].iov_base;
2731 cpu_to_le32(server->vals->create_lease_size);
2734 rc = add_durable_context(iov, &n_iov, oparms,
2735 tcon->use_persistent);
2740 if (tcon->posix_extensions) {
2742 struct create_context *ccontext =
2743 (struct create_context *)iov[n_iov-1].iov_base;
2745 cpu_to_le32(iov[n_iov-1].iov_len);
2748 rc = add_posix_context(iov, &n_iov, oparms->mode);
2753 if (tcon->snapshot_time) {
2754 cifs_dbg(FYI, "adding snapshot context\n");
2756 struct create_context *ccontext =
2757 (struct create_context *)iov[n_iov-1].iov_base;
2759 cpu_to_le32(iov[n_iov-1].iov_len);
2762 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2767 if ((oparms->disposition != FILE_OPEN) &&
2768 (oparms->cifs_sb) &&
2769 (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
2770 (oparms->mode != ACL_NO_MODE)) {
2772 struct create_context *ccontext =
2773 (struct create_context *)iov[n_iov-1].iov_base;
2775 cpu_to_le32(iov[n_iov-1].iov_len);
2778 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2779 rc = add_sd_context(iov, &n_iov, oparms->mode);
2785 struct create_context *ccontext =
2786 (struct create_context *)iov[n_iov-1].iov_base;
2787 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2789 add_query_id_context(iov, &n_iov);
2791 rqst->rq_nvec = n_iov;
2795 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2796 * All other vectors are freed by kfree().
2799 SMB2_open_free(struct smb_rqst *rqst)
2803 if (rqst && rqst->rq_iov) {
2804 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2805 for (i = 1; i < rqst->rq_nvec; i++)
2806 if (rqst->rq_iov[i].iov_base != smb2_padding)
2807 kfree(rqst->rq_iov[i].iov_base);
2812 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2813 __u8 *oplock, struct smb2_file_all_info *buf,
2814 struct create_posix_rsp *posix,
2815 struct kvec *err_iov, int *buftype)
2817 struct smb_rqst rqst;
2818 struct smb2_create_rsp *rsp = NULL;
2819 struct cifs_tcon *tcon = oparms->tcon;
2820 struct cifs_ses *ses = tcon->ses;
2821 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2822 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2823 struct kvec rsp_iov = {NULL, 0};
2824 int resp_buftype = CIFS_NO_BUFFER;
2828 cifs_dbg(FYI, "create/open\n");
2829 if (!ses || !server)
2832 if (smb3_encryption_required(tcon))
2833 flags |= CIFS_TRANSFORM_REQ;
2835 memset(&rqst, 0, sizeof(struct smb_rqst));
2836 memset(&iov, 0, sizeof(iov));
2838 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2840 rc = SMB2_open_init(tcon, server,
2841 &rqst, oplock, oparms, path);
2845 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2846 oparms->create_options, oparms->desired_access);
2848 rc = cifs_send_recv(xid, ses, server,
2849 &rqst, &resp_buftype, flags,
2851 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2854 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2855 if (err_iov && rsp) {
2857 *buftype = resp_buftype;
2858 resp_buftype = CIFS_NO_BUFFER;
2861 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2862 oparms->create_options, oparms->desired_access, rc);
2863 if (rc == -EREMCHG) {
2864 pr_warn_once("server share %s deleted\n",
2866 tcon->need_reconnect = true;
2870 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2871 ses->Suid, oparms->create_options,
2872 oparms->desired_access);
2874 atomic_inc(&tcon->num_remote_opens);
2875 oparms->fid->persistent_fid = rsp->PersistentFileId;
2876 oparms->fid->volatile_fid = rsp->VolatileFileId;
2877 oparms->fid->access = oparms->desired_access;
2878 #ifdef CONFIG_CIFS_DEBUG2
2879 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2880 #endif /* CIFS_DEBUG2 */
2883 memcpy(buf, &rsp->CreationTime, 32);
2884 buf->AllocationSize = rsp->AllocationSize;
2885 buf->EndOfFile = rsp->EndofFile;
2886 buf->Attributes = rsp->FileAttributes;
2887 buf->NumberOfLinks = cpu_to_le32(1);
2888 buf->DeletePending = 0;
2892 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2893 oparms->fid->lease_key, oplock, buf, posix);
2895 SMB2_open_free(&rqst);
2896 free_rsp_buf(resp_buftype, rsp);
2901 SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2902 struct smb_rqst *rqst,
2903 u64 persistent_fid, u64 volatile_fid, u32 opcode,
2904 bool is_fsctl, char *in_data, u32 indatalen,
2905 __u32 max_response_size)
2907 struct smb2_ioctl_req *req;
2908 struct kvec *iov = rqst->rq_iov;
2909 unsigned int total_len;
2913 rc = smb2_ioctl_req_init(opcode, tcon, server,
2914 (void **) &req, &total_len);
2920 * indatalen is usually small at a couple of bytes max, so
2921 * just allocate through generic pool
2923 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
2925 cifs_small_buf_release(req);
2930 req->CtlCode = cpu_to_le32(opcode);
2931 req->PersistentFileId = persistent_fid;
2932 req->VolatileFileId = volatile_fid;
2934 iov[0].iov_base = (char *)req;
2936 * If no input data, the size of ioctl struct in
2937 * protocol spec still includes a 1 byte data buffer,
2938 * but if input data passed to ioctl, we do not
2939 * want to double count this, so we do not send
2940 * the dummy one byte of data in iovec[0] if sending
2941 * input data (in iovec[1]).
2944 req->InputCount = cpu_to_le32(indatalen);
2945 /* do not set InputOffset if no input data */
2947 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
2949 iov[0].iov_len = total_len - 1;
2950 iov[1].iov_base = in_data_buf;
2951 iov[1].iov_len = indatalen;
2954 iov[0].iov_len = total_len;
2957 req->OutputOffset = 0;
2958 req->OutputCount = 0; /* MBZ */
2961 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2962 * We Could increase default MaxOutputResponse, but that could require
2963 * more credits. Windows typically sets this smaller, but for some
2964 * ioctls it may be useful to allow server to send more. No point
2965 * limiting what the server can send as long as fits in one credit
2966 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2967 * to increase this limit up in the future.
2968 * Note that for snapshot queries that servers like Azure expect that
2969 * the first query be minimal size (and just used to get the number/size
2970 * of previous versions) so response size must be specified as EXACTLY
2971 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2972 * of eight bytes. Currently that is the only case where we set max
2973 * response size smaller.
2975 req->MaxOutputResponse = cpu_to_le32(max_response_size);
2976 req->sync_hdr.CreditCharge =
2977 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
2978 SMB2_MAX_BUFFER_SIZE));
2980 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2984 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2985 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
2986 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
2992 SMB2_ioctl_free(struct smb_rqst *rqst)
2995 if (rqst && rqst->rq_iov) {
2996 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2997 for (i = 1; i < rqst->rq_nvec; i++)
2998 if (rqst->rq_iov[i].iov_base != smb2_padding)
2999 kfree(rqst->rq_iov[i].iov_base);
3005 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
3008 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3009 u64 volatile_fid, u32 opcode, bool is_fsctl,
3010 char *in_data, u32 indatalen, u32 max_out_data_len,
3011 char **out_data, u32 *plen /* returned data len */)
3013 struct smb_rqst rqst;
3014 struct smb2_ioctl_rsp *rsp = NULL;
3015 struct cifs_ses *ses;
3016 struct TCP_Server_Info *server;
3017 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
3018 struct kvec rsp_iov = {NULL, 0};
3019 int resp_buftype = CIFS_NO_BUFFER;
3023 cifs_dbg(FYI, "SMB2 IOCTL\n");
3025 if (out_data != NULL)
3028 /* zero out returned data len, in case of error */
3039 server = cifs_pick_channel(ses);
3043 if (smb3_encryption_required(tcon))
3044 flags |= CIFS_TRANSFORM_REQ;
3046 memset(&rqst, 0, sizeof(struct smb_rqst));
3047 memset(&iov, 0, sizeof(iov));
3049 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
3051 rc = SMB2_ioctl_init(tcon, server,
3052 &rqst, persistent_fid, volatile_fid, opcode,
3053 is_fsctl, in_data, indatalen, max_out_data_len);
3057 rc = cifs_send_recv(xid, ses, server,
3058 &rqst, &resp_buftype, flags,
3060 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
3063 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3064 ses->Suid, 0, opcode, rc);
3066 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
3067 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3069 } else if (rc == -EINVAL) {
3070 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3071 (opcode != FSCTL_SRV_COPYCHUNK)) {
3072 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3075 } else if (rc == -E2BIG) {
3076 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3077 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3082 /* check if caller wants to look at return data or just return rc */
3083 if ((plen == NULL) || (out_data == NULL))
3086 *plen = le32_to_cpu(rsp->OutputCount);
3088 /* We check for obvious errors in the output buffer length and offset */
3090 goto ioctl_exit; /* server returned no data */
3091 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
3092 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
3098 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
3099 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
3100 le32_to_cpu(rsp->OutputOffset));
3106 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3108 if (*out_data == NULL) {
3114 SMB2_ioctl_free(&rqst);
3115 free_rsp_buf(resp_buftype, rsp);
3120 * Individual callers to ioctl worker function follow
3124 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3125 u64 persistent_fid, u64 volatile_fid)
3128 struct compress_ioctl fsctl_input;
3129 char *ret_data = NULL;
3131 fsctl_input.CompressionState =
3132 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3134 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3135 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
3136 (char *)&fsctl_input /* data input */,
3137 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3138 &ret_data /* out data */, NULL);
3140 cifs_dbg(FYI, "set compression rc %d\n", rc);
3146 SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3147 struct smb_rqst *rqst,
3148 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
3150 struct smb2_close_req *req;
3151 struct kvec *iov = rqst->rq_iov;
3152 unsigned int total_len;
3155 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
3156 (void **) &req, &total_len);
3160 req->PersistentFileId = persistent_fid;
3161 req->VolatileFileId = volatile_fid;
3163 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3166 iov[0].iov_base = (char *)req;
3167 iov[0].iov_len = total_len;
3173 SMB2_close_free(struct smb_rqst *rqst)
3175 if (rqst && rqst->rq_iov)
3176 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3180 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3181 u64 persistent_fid, u64 volatile_fid,
3182 struct smb2_file_network_open_info *pbuf)
3184 struct smb_rqst rqst;
3185 struct smb2_close_rsp *rsp = NULL;
3186 struct cifs_ses *ses = tcon->ses;
3187 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3189 struct kvec rsp_iov;
3190 int resp_buftype = CIFS_NO_BUFFER;
3193 bool query_attrs = false;
3195 cifs_dbg(FYI, "Close\n");
3197 if (!ses || !server)
3200 if (smb3_encryption_required(tcon))
3201 flags |= CIFS_TRANSFORM_REQ;
3203 memset(&rqst, 0, sizeof(struct smb_rqst));
3204 memset(&iov, 0, sizeof(iov));
3208 /* check if need to ask server to return timestamps in close response */
3212 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3213 rc = SMB2_close_init(tcon, server,
3214 &rqst, persistent_fid, volatile_fid,
3219 rc = cifs_send_recv(xid, ses, server,
3220 &rqst, &resp_buftype, flags, &rsp_iov);
3221 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
3224 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
3225 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3229 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3232 * Note that have to subtract 4 since struct network_open_info
3233 * has a final 4 byte pad that close response does not have
3236 memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3239 atomic_dec(&tcon->num_remote_opens);
3241 SMB2_close_free(&rqst);
3242 free_rsp_buf(resp_buftype, rsp);
3244 /* retry close in a worker thread if this one is interrupted */
3248 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3251 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3252 persistent_fid, tmp_rc);
3258 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3259 u64 persistent_fid, u64 volatile_fid)
3261 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3265 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3266 struct kvec *iov, unsigned int min_buf_size)
3268 unsigned int smb_len = iov->iov_len;
3269 char *end_of_smb = smb_len + (char *)iov->iov_base;
3270 char *begin_of_buf = offset + (char *)iov->iov_base;
3271 char *end_of_buf = begin_of_buf + buffer_length;
3274 if (buffer_length < min_buf_size) {
3275 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3276 buffer_length, min_buf_size);
3280 /* check if beyond RFC1001 maximum length */
3281 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3282 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3283 buffer_length, smb_len);
3287 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3288 cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
3296 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3297 * Caller must free buffer.
3300 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3301 struct kvec *iov, unsigned int minbufsize,
3304 char *begin_of_buf = offset + (char *)iov->iov_base;
3310 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3314 memcpy(data, begin_of_buf, buffer_length);
3320 SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3321 struct smb_rqst *rqst,
3322 u64 persistent_fid, u64 volatile_fid,
3323 u8 info_class, u8 info_type, u32 additional_info,
3324 size_t output_len, size_t input_len, void *input)
3326 struct smb2_query_info_req *req;
3327 struct kvec *iov = rqst->rq_iov;
3328 unsigned int total_len;
3331 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
3332 (void **) &req, &total_len);
3336 req->InfoType = info_type;
3337 req->FileInfoClass = info_class;
3338 req->PersistentFileId = persistent_fid;
3339 req->VolatileFileId = volatile_fid;
3340 req->AdditionalInformation = cpu_to_le32(additional_info);
3342 req->OutputBufferLength = cpu_to_le32(output_len);
3344 req->InputBufferLength = cpu_to_le32(input_len);
3345 /* total_len for smb query request never close to le16 max */
3346 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3347 memcpy(req->Buffer, input, input_len);
3350 iov[0].iov_base = (char *)req;
3352 iov[0].iov_len = total_len - 1 + input_len;
3357 SMB2_query_info_free(struct smb_rqst *rqst)
3359 if (rqst && rqst->rq_iov)
3360 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3364 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3365 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3366 u32 additional_info, size_t output_len, size_t min_len, void **data,
3369 struct smb_rqst rqst;
3370 struct smb2_query_info_rsp *rsp = NULL;
3372 struct kvec rsp_iov;
3374 int resp_buftype = CIFS_NO_BUFFER;
3375 struct cifs_ses *ses = tcon->ses;
3376 struct TCP_Server_Info *server;
3378 bool allocated = false;
3380 cifs_dbg(FYI, "Query Info\n");
3384 server = cifs_pick_channel(ses);
3388 if (smb3_encryption_required(tcon))
3389 flags |= CIFS_TRANSFORM_REQ;
3391 memset(&rqst, 0, sizeof(struct smb_rqst));
3392 memset(&iov, 0, sizeof(iov));
3396 rc = SMB2_query_info_init(tcon, server,
3397 &rqst, persistent_fid, volatile_fid,
3398 info_class, info_type, additional_info,
3399 output_len, 0, NULL);
3403 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3404 ses->Suid, info_class, (__u32)info_type);
3406 rc = cifs_send_recv(xid, ses, server,
3407 &rqst, &resp_buftype, flags, &rsp_iov);
3408 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3411 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3412 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3413 ses->Suid, info_class, (__u32)info_type, rc);
3417 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3418 ses->Suid, info_class, (__u32)info_type);
3421 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3423 *data = kmalloc(*dlen, GFP_KERNEL);
3426 "Error %d allocating memory for acl\n",
3436 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3437 le32_to_cpu(rsp->OutputBufferLength),
3438 &rsp_iov, min_len, *data);
3439 if (rc && allocated) {
3446 SMB2_query_info_free(&rqst);
3447 free_rsp_buf(resp_buftype, rsp);
3451 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3452 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3454 return query_info(xid, tcon, persistent_fid, volatile_fid,
3455 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3456 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3457 sizeof(struct smb2_file_all_info), (void **)&data,
3462 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3463 u64 persistent_fid, u64 volatile_fid,
3464 void **data, u32 *plen)
3466 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3469 return query_info(xid, tcon, persistent_fid, volatile_fid,
3470 0, SMB2_O_INFO_SECURITY, additional_info,
3471 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3475 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3476 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3478 return query_info(xid, tcon, persistent_fid, volatile_fid,
3479 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3480 sizeof(struct smb2_file_internal_info),
3481 sizeof(struct smb2_file_internal_info),
3482 (void **)&uniqueid, NULL);
3486 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3487 * See MS-SMB2 2.2.35 and 2.2.36
3491 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3492 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3493 u64 persistent_fid, u64 volatile_fid,
3494 u32 completion_filter, bool watch_tree)
3496 struct smb2_change_notify_req *req;
3497 struct kvec *iov = rqst->rq_iov;
3498 unsigned int total_len;
3501 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
3502 (void **) &req, &total_len);
3506 req->PersistentFileId = persistent_fid;
3507 req->VolatileFileId = volatile_fid;
3508 /* See note 354 of MS-SMB2, 64K max */
3509 req->OutputBufferLength =
3510 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3511 req->CompletionFilter = cpu_to_le32(completion_filter);
3513 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3517 iov[0].iov_base = (char *)req;
3518 iov[0].iov_len = total_len;
3524 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3525 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3526 u32 completion_filter)
3528 struct cifs_ses *ses = tcon->ses;
3529 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3530 struct smb_rqst rqst;
3532 struct kvec rsp_iov = {NULL, 0};
3533 int resp_buftype = CIFS_NO_BUFFER;
3537 cifs_dbg(FYI, "change notify\n");
3538 if (!ses || !server)
3541 if (smb3_encryption_required(tcon))
3542 flags |= CIFS_TRANSFORM_REQ;
3544 memset(&rqst, 0, sizeof(struct smb_rqst));
3545 memset(&iov, 0, sizeof(iov));
3549 rc = SMB2_notify_init(xid, &rqst, tcon, server,
3550 persistent_fid, volatile_fid,
3551 completion_filter, watch_tree);
3555 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3556 (u8)watch_tree, completion_filter);
3557 rc = cifs_send_recv(xid, ses, server,
3558 &rqst, &resp_buftype, flags, &rsp_iov);
3561 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3562 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3563 (u8)watch_tree, completion_filter, rc);
3565 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3566 ses->Suid, (u8)watch_tree, completion_filter);
3570 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3571 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3578 * This is a no-op for now. We're not really interested in the reply, but
3579 * rather in the fact that the server sent one and that server->lstrp
3582 * FIXME: maybe we should consider checking that the reply matches request?
3585 smb2_echo_callback(struct mid_q_entry *mid)
3587 struct TCP_Server_Info *server = mid->callback_data;
3588 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3589 struct cifs_credits credits = { .value = 0, .instance = 0 };
3591 if (mid->mid_state == MID_RESPONSE_RECEIVED
3592 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3593 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3594 credits.instance = server->reconnect_instance;
3597 DeleteMidQEntry(mid);
3598 add_credits(server, &credits, CIFS_ECHO_OP);
3601 void smb2_reconnect_server(struct work_struct *work)
3603 struct TCP_Server_Info *server = container_of(work,
3604 struct TCP_Server_Info, reconnect.work);
3605 struct cifs_ses *ses;
3606 struct cifs_tcon *tcon, *tcon2;
3607 struct list_head tmp_list;
3608 int tcon_exist = false;
3610 int resched = false;
3613 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3614 mutex_lock(&server->reconnect_mutex);
3616 INIT_LIST_HEAD(&tmp_list);
3617 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3619 spin_lock(&cifs_tcp_ses_lock);
3620 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3621 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3622 if (tcon->need_reconnect || tcon->need_reopen_files) {
3624 list_add_tail(&tcon->rlist, &tmp_list);
3629 * IPC has the same lifetime as its session and uses its
3632 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3633 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3639 * Get the reference to server struct to be sure that the last call of
3640 * cifs_put_tcon() in the loop below won't release the server pointer.
3643 server->srv_count++;
3645 spin_unlock(&cifs_tcp_ses_lock);
3647 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3648 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
3650 cifs_reopen_persistent_handles(tcon);
3653 list_del_init(&tcon->rlist);
3655 cifs_put_smb_ses(tcon->ses);
3657 cifs_put_tcon(tcon);
3660 cifs_dbg(FYI, "Reconnecting tcons finished\n");
3662 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3663 mutex_unlock(&server->reconnect_mutex);
3665 /* now we can safely release srv struct */
3667 cifs_put_tcp_session(server, 1);
3671 SMB2_echo(struct TCP_Server_Info *server)
3673 struct smb2_echo_req *req;
3676 struct smb_rqst rqst = { .rq_iov = iov,
3678 unsigned int total_len;
3680 cifs_dbg(FYI, "In echo request\n");
3682 if (server->tcpStatus == CifsNeedNegotiate) {
3683 /* No need to send echo on newly established connections */
3684 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
3688 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
3689 (void **)&req, &total_len);
3693 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3695 iov[0].iov_len = total_len;
3696 iov[0].iov_base = (char *)req;
3698 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3699 server, CIFS_ECHO_OP, NULL);
3701 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3703 cifs_small_buf_release(req);
3708 SMB2_flush_free(struct smb_rqst *rqst)
3710 if (rqst && rqst->rq_iov)
3711 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3715 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3716 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3717 u64 persistent_fid, u64 volatile_fid)
3719 struct smb2_flush_req *req;
3720 struct kvec *iov = rqst->rq_iov;
3721 unsigned int total_len;
3724 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
3725 (void **) &req, &total_len);
3729 req->PersistentFileId = persistent_fid;
3730 req->VolatileFileId = volatile_fid;
3732 iov[0].iov_base = (char *)req;
3733 iov[0].iov_len = total_len;
3739 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3742 struct cifs_ses *ses = tcon->ses;
3743 struct smb_rqst rqst;
3745 struct kvec rsp_iov = {NULL, 0};
3746 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3747 int resp_buftype = CIFS_NO_BUFFER;
3751 cifs_dbg(FYI, "flush\n");
3752 if (!ses || !(ses->server))
3755 if (smb3_encryption_required(tcon))
3756 flags |= CIFS_TRANSFORM_REQ;
3758 memset(&rqst, 0, sizeof(struct smb_rqst));
3759 memset(&iov, 0, sizeof(iov));
3763 rc = SMB2_flush_init(xid, &rqst, tcon, server,
3764 persistent_fid, volatile_fid);
3768 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3769 rc = cifs_send_recv(xid, ses, server,
3770 &rqst, &resp_buftype, flags, &rsp_iov);
3773 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
3774 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3777 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3781 SMB2_flush_free(&rqst);
3782 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3787 * To form a chain of read requests, any read requests after the first should
3788 * have the end_of_chain boolean set to true.
3791 smb2_new_read_req(void **buf, unsigned int *total_len,
3792 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3793 unsigned int remaining_bytes, int request_type)
3796 struct smb2_read_plain_req *req = NULL;
3797 struct smb2_sync_hdr *shdr;
3798 struct TCP_Server_Info *server = io_parms->server;
3800 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
3801 (void **) &req, total_len);
3806 return -ECONNABORTED;
3808 shdr = &req->sync_hdr;
3809 shdr->ProcessId = cpu_to_le32(io_parms->pid);
3811 req->PersistentFileId = io_parms->persistent_fid;
3812 req->VolatileFileId = io_parms->volatile_fid;
3813 req->ReadChannelInfoOffset = 0; /* reserved */
3814 req->ReadChannelInfoLength = 0; /* reserved */
3815 req->Channel = 0; /* reserved */
3816 req->MinimumCount = 0;
3817 req->Length = cpu_to_le32(io_parms->length);
3818 req->Offset = cpu_to_le64(io_parms->offset);
3820 trace_smb3_read_enter(0 /* xid */,
3821 io_parms->persistent_fid,
3822 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3823 io_parms->offset, io_parms->length);
3824 #ifdef CONFIG_CIFS_SMB_DIRECT
3826 * If we want to do a RDMA write, fill in and append
3827 * smbd_buffer_descriptor_v1 to the end of read request
3829 if (server->rdma && rdata && !server->sign &&
3830 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
3832 struct smbd_buffer_descriptor_v1 *v1;
3833 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3835 rdata->mr = smbd_register_mr(
3836 server->smbd_conn, rdata->pages,
3837 rdata->nr_pages, rdata->page_offset,
3838 rdata->tailsz, true, need_invalidate);
3842 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3843 if (need_invalidate)
3844 req->Channel = SMB2_CHANNEL_RDMA_V1;
3845 req->ReadChannelInfoOffset =
3846 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
3847 req->ReadChannelInfoLength =
3848 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3849 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3850 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3851 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3852 v1->length = cpu_to_le32(rdata->mr->mr->length);
3854 *total_len += sizeof(*v1) - 1;
3857 if (request_type & CHAINED_REQUEST) {
3858 if (!(request_type & END_OF_CHAIN)) {
3859 /* next 8-byte aligned request */
3860 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3861 shdr->NextCommand = cpu_to_le32(*total_len);
3862 } else /* END_OF_CHAIN */
3863 shdr->NextCommand = 0;
3864 if (request_type & RELATED_REQUEST) {
3865 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
3867 * Related requests use info from previous read request
3870 shdr->SessionId = 0xFFFFFFFF;
3871 shdr->TreeId = 0xFFFFFFFF;
3872 req->PersistentFileId = 0xFFFFFFFF;
3873 req->VolatileFileId = 0xFFFFFFFF;
3876 if (remaining_bytes > io_parms->length)
3877 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3879 req->RemainingBytes = 0;
3886 smb2_readv_callback(struct mid_q_entry *mid)
3888 struct cifs_readdata *rdata = mid->callback_data;
3889 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3890 struct TCP_Server_Info *server = rdata->server;
3891 struct smb2_sync_hdr *shdr =
3892 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
3893 struct cifs_credits credits = { .value = 0, .instance = 0 };
3894 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
3896 .rq_pages = rdata->pages,
3897 .rq_offset = rdata->page_offset,
3898 .rq_npages = rdata->nr_pages,
3899 .rq_pagesz = rdata->pagesz,
3900 .rq_tailsz = rdata->tailsz };
3902 WARN_ONCE(rdata->server != mid->server,
3903 "rdata server %p != mid server %p",
3904 rdata->server, mid->server);
3906 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3907 __func__, mid->mid, mid->mid_state, rdata->result,
3910 switch (mid->mid_state) {
3911 case MID_RESPONSE_RECEIVED:
3912 credits.value = le16_to_cpu(shdr->CreditRequest);
3913 credits.instance = server->reconnect_instance;
3914 /* result already set, check signature */
3915 if (server->sign && !mid->decrypted) {
3918 rc = smb2_verify_signature(&rqst, server);
3920 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
3923 /* FIXME: should this be counted toward the initiating task? */
3924 task_io_account_read(rdata->got_bytes);
3925 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3927 case MID_REQUEST_SUBMITTED:
3928 case MID_RETRY_NEEDED:
3929 rdata->result = -EAGAIN;
3930 if (server->sign && rdata->got_bytes)
3931 /* reset bytes number since we can not check a sign */
3932 rdata->got_bytes = 0;
3933 /* FIXME: should this be counted toward the initiating task? */
3934 task_io_account_read(rdata->got_bytes);
3935 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3937 case MID_RESPONSE_MALFORMED:
3938 credits.value = le16_to_cpu(shdr->CreditRequest);
3939 credits.instance = server->reconnect_instance;
3942 rdata->result = -EIO;
3944 #ifdef CONFIG_CIFS_SMB_DIRECT
3946 * If this rdata has a memmory registered, the MR can be freed
3947 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3948 * because they have limited number and are used for future I/Os
3951 smbd_deregister_mr(rdata->mr);
3955 if (rdata->result && rdata->result != -ENODATA) {
3956 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
3957 trace_smb3_read_err(0 /* xid */,
3958 rdata->cfile->fid.persistent_fid,
3959 tcon->tid, tcon->ses->Suid, rdata->offset,
3960 rdata->bytes, rdata->result);
3962 trace_smb3_read_done(0 /* xid */,
3963 rdata->cfile->fid.persistent_fid,
3964 tcon->tid, tcon->ses->Suid,
3965 rdata->offset, rdata->got_bytes);
3967 queue_work(cifsiod_wq, &rdata->work);
3968 DeleteMidQEntry(mid);
3969 add_credits(server, &credits, 0);
3972 /* smb2_async_readv - send an async read, and set up mid to handle result */
3974 smb2_async_readv(struct cifs_readdata *rdata)
3978 struct smb2_sync_hdr *shdr;
3979 struct cifs_io_parms io_parms;
3980 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3982 struct TCP_Server_Info *server;
3983 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3984 unsigned int total_len;
3986 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3987 __func__, rdata->offset, rdata->bytes);
3990 rdata->server = cifs_pick_channel(tcon->ses);
3992 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3993 io_parms.server = server = rdata->server;
3994 io_parms.offset = rdata->offset;
3995 io_parms.length = rdata->bytes;
3996 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3997 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3998 io_parms.pid = rdata->pid;
4000 rc = smb2_new_read_req(
4001 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
4005 if (smb3_encryption_required(io_parms.tcon))
4006 flags |= CIFS_TRANSFORM_REQ;
4008 rdata->iov[0].iov_base = buf;
4009 rdata->iov[0].iov_len = total_len;
4011 shdr = (struct smb2_sync_hdr *)buf;
4013 if (rdata->credits.value > 0) {
4014 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
4015 SMB2_MAX_BUFFER_SIZE));
4016 shdr->CreditRequest =
4017 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
4019 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4021 goto async_readv_out;
4023 flags |= CIFS_HAS_CREDITS;
4026 kref_get(&rdata->refcount);
4027 rc = cifs_call_async(server, &rqst,
4028 cifs_readv_receive, smb2_readv_callback,
4029 smb3_handle_read_data, rdata, flags,
4032 kref_put(&rdata->refcount, cifs_readdata_release);
4033 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
4034 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
4036 io_parms.tcon->ses->Suid,
4037 io_parms.offset, io_parms.length, rc);
4041 cifs_small_buf_release(buf);
4046 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
4047 unsigned int *nbytes, char **buf, int *buf_type)
4049 struct smb_rqst rqst;
4050 int resp_buftype, rc;
4051 struct smb2_read_plain_req *req = NULL;
4052 struct smb2_read_rsp *rsp = NULL;
4054 struct kvec rsp_iov;
4055 unsigned int total_len;
4056 int flags = CIFS_LOG_ERROR;
4057 struct cifs_ses *ses = io_parms->tcon->ses;
4059 if (!io_parms->server)
4060 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4063 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
4067 if (smb3_encryption_required(io_parms->tcon))
4068 flags |= CIFS_TRANSFORM_REQ;
4070 iov[0].iov_base = (char *)req;
4071 iov[0].iov_len = total_len;
4073 memset(&rqst, 0, sizeof(struct smb_rqst));
4077 rc = cifs_send_recv(xid, ses, io_parms->server,
4078 &rqst, &resp_buftype, flags, &rsp_iov);
4079 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
4082 if (rc != -ENODATA) {
4083 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4084 cifs_dbg(VFS, "Send error in read = %d\n", rc);
4085 trace_smb3_read_err(xid, req->PersistentFileId,
4086 io_parms->tcon->tid, ses->Suid,
4087 io_parms->offset, io_parms->length,
4090 trace_smb3_read_done(xid, req->PersistentFileId,
4091 io_parms->tcon->tid, ses->Suid,
4092 io_parms->offset, 0);
4093 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4094 cifs_small_buf_release(req);
4095 return rc == -ENODATA ? 0 : rc;
4097 trace_smb3_read_done(xid, req->PersistentFileId,
4098 io_parms->tcon->tid, ses->Suid,
4099 io_parms->offset, io_parms->length);
4101 cifs_small_buf_release(req);
4103 *nbytes = le32_to_cpu(rsp->DataLength);
4104 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4105 (*nbytes > io_parms->length)) {
4106 cifs_dbg(FYI, "bad length %d for count %d\n",
4107 *nbytes, io_parms->length);
4113 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
4114 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4115 } else if (resp_buftype != CIFS_NO_BUFFER) {
4116 *buf = rsp_iov.iov_base;
4117 if (resp_buftype == CIFS_SMALL_BUFFER)
4118 *buf_type = CIFS_SMALL_BUFFER;
4119 else if (resp_buftype == CIFS_LARGE_BUFFER)
4120 *buf_type = CIFS_LARGE_BUFFER;
4126 * Check the mid_state and signature on received buffer (if any), and queue the
4127 * workqueue completion task.
4130 smb2_writev_callback(struct mid_q_entry *mid)
4132 struct cifs_writedata *wdata = mid->callback_data;
4133 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4134 struct TCP_Server_Info *server = wdata->server;
4135 unsigned int written;
4136 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
4137 struct cifs_credits credits = { .value = 0, .instance = 0 };
4139 WARN_ONCE(wdata->server != mid->server,
4140 "wdata server %p != mid server %p",
4141 wdata->server, mid->server);
4143 switch (mid->mid_state) {
4144 case MID_RESPONSE_RECEIVED:
4145 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4146 credits.instance = server->reconnect_instance;
4147 wdata->result = smb2_check_receive(mid, server, 0);
4148 if (wdata->result != 0)
4151 written = le32_to_cpu(rsp->DataLength);
4153 * Mask off high 16 bits when bytes written as returned
4154 * by the server is greater than bytes requested by the
4155 * client. OS/2 servers are known to set incorrect
4158 if (written > wdata->bytes)
4161 if (written < wdata->bytes)
4162 wdata->result = -ENOSPC;
4164 wdata->bytes = written;
4166 case MID_REQUEST_SUBMITTED:
4167 case MID_RETRY_NEEDED:
4168 wdata->result = -EAGAIN;
4170 case MID_RESPONSE_MALFORMED:
4171 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4172 credits.instance = server->reconnect_instance;
4175 wdata->result = -EIO;
4178 #ifdef CONFIG_CIFS_SMB_DIRECT
4180 * If this wdata has a memory registered, the MR can be freed
4181 * The number of MRs available is limited, it's important to recover
4182 * used MR as soon as I/O is finished. Hold MR longer in the later
4183 * I/O process can possibly result in I/O deadlock due to lack of MR
4184 * to send request on I/O retry
4187 smbd_deregister_mr(wdata->mr);
4191 if (wdata->result) {
4192 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4193 trace_smb3_write_err(0 /* no xid */,
4194 wdata->cfile->fid.persistent_fid,
4195 tcon->tid, tcon->ses->Suid, wdata->offset,
4196 wdata->bytes, wdata->result);
4197 if (wdata->result == -ENOSPC)
4198 pr_warn_once("Out of space writing to %s\n",
4201 trace_smb3_write_done(0 /* no xid */,
4202 wdata->cfile->fid.persistent_fid,
4203 tcon->tid, tcon->ses->Suid,
4204 wdata->offset, wdata->bytes);
4206 queue_work(cifsiod_wq, &wdata->work);
4207 DeleteMidQEntry(mid);
4208 add_credits(server, &credits, 0);
4211 /* smb2_async_writev - send an async write, and set up mid to handle result */
4213 smb2_async_writev(struct cifs_writedata *wdata,
4214 void (*release)(struct kref *kref))
4216 int rc = -EACCES, flags = 0;
4217 struct smb2_write_req *req = NULL;
4218 struct smb2_sync_hdr *shdr;
4219 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4220 struct TCP_Server_Info *server = wdata->server;
4222 struct smb_rqst rqst = { };
4223 unsigned int total_len;
4226 server = wdata->server = cifs_pick_channel(tcon->ses);
4228 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
4229 (void **) &req, &total_len);
4233 if (smb3_encryption_required(tcon))
4234 flags |= CIFS_TRANSFORM_REQ;
4236 shdr = (struct smb2_sync_hdr *)req;
4237 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
4239 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
4240 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
4241 req->WriteChannelInfoOffset = 0;
4242 req->WriteChannelInfoLength = 0;
4244 req->Offset = cpu_to_le64(wdata->offset);
4245 req->DataOffset = cpu_to_le16(
4246 offsetof(struct smb2_write_req, Buffer));
4247 req->RemainingBytes = 0;
4249 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
4250 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
4251 #ifdef CONFIG_CIFS_SMB_DIRECT
4253 * If we want to do a server RDMA read, fill in and append
4254 * smbd_buffer_descriptor_v1 to the end of write request
4256 if (server->rdma && !server->sign && wdata->bytes >=
4257 server->smbd_conn->rdma_readwrite_threshold) {
4259 struct smbd_buffer_descriptor_v1 *v1;
4260 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4262 wdata->mr = smbd_register_mr(
4263 server->smbd_conn, wdata->pages,
4264 wdata->nr_pages, wdata->page_offset,
4265 wdata->tailsz, false, need_invalidate);
4268 goto async_writev_out;
4271 req->DataOffset = 0;
4272 if (wdata->nr_pages > 1)
4273 req->RemainingBytes =
4275 (wdata->nr_pages - 1) * wdata->pagesz -
4276 wdata->page_offset + wdata->tailsz
4279 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
4280 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4281 if (need_invalidate)
4282 req->Channel = SMB2_CHANNEL_RDMA_V1;
4283 req->WriteChannelInfoOffset =
4284 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4285 req->WriteChannelInfoLength =
4286 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4287 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4288 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4289 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4290 v1->length = cpu_to_le32(wdata->mr->mr->length);
4293 iov[0].iov_len = total_len - 1;
4294 iov[0].iov_base = (char *)req;
4298 rqst.rq_pages = wdata->pages;
4299 rqst.rq_offset = wdata->page_offset;
4300 rqst.rq_npages = wdata->nr_pages;
4301 rqst.rq_pagesz = wdata->pagesz;
4302 rqst.rq_tailsz = wdata->tailsz;
4303 #ifdef CONFIG_CIFS_SMB_DIRECT
4305 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4309 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4310 wdata->offset, wdata->bytes);
4312 #ifdef CONFIG_CIFS_SMB_DIRECT
4313 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4315 req->Length = cpu_to_le32(wdata->bytes);
4317 req->Length = cpu_to_le32(wdata->bytes);
4320 if (wdata->credits.value > 0) {
4321 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4322 SMB2_MAX_BUFFER_SIZE));
4323 shdr->CreditRequest =
4324 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
4326 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4328 goto async_writev_out;
4330 flags |= CIFS_HAS_CREDITS;
4333 kref_get(&wdata->refcount);
4334 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4335 wdata, flags, &wdata->credits);
4338 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
4339 tcon->tid, tcon->ses->Suid, wdata->offset,
4341 kref_put(&wdata->refcount, release);
4342 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4346 cifs_small_buf_release(req);
4351 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4352 * The length field from io_parms must be at least 1 and indicates a number of
4353 * elements with data to write that begins with position 1 in iov array. All
4354 * data length is specified by count.
4357 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4358 unsigned int *nbytes, struct kvec *iov, int n_vec)
4360 struct smb_rqst rqst;
4362 struct smb2_write_req *req = NULL;
4363 struct smb2_write_rsp *rsp = NULL;
4365 struct kvec rsp_iov;
4367 unsigned int total_len;
4368 struct TCP_Server_Info *server;
4375 if (!io_parms->server)
4376 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4377 server = io_parms->server;
4379 return -ECONNABORTED;
4381 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
4382 (void **) &req, &total_len);
4386 if (smb3_encryption_required(io_parms->tcon))
4387 flags |= CIFS_TRANSFORM_REQ;
4389 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
4391 req->PersistentFileId = io_parms->persistent_fid;
4392 req->VolatileFileId = io_parms->volatile_fid;
4393 req->WriteChannelInfoOffset = 0;
4394 req->WriteChannelInfoLength = 0;
4396 req->Length = cpu_to_le32(io_parms->length);
4397 req->Offset = cpu_to_le64(io_parms->offset);
4398 req->DataOffset = cpu_to_le16(
4399 offsetof(struct smb2_write_req, Buffer));
4400 req->RemainingBytes = 0;
4402 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4403 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4404 io_parms->offset, io_parms->length);
4406 iov[0].iov_base = (char *)req;
4408 iov[0].iov_len = total_len - 1;
4410 memset(&rqst, 0, sizeof(struct smb_rqst));
4412 rqst.rq_nvec = n_vec + 1;
4414 rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
4416 &resp_buftype, flags, &rsp_iov);
4417 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
4420 trace_smb3_write_err(xid, req->PersistentFileId,
4421 io_parms->tcon->tid,
4422 io_parms->tcon->ses->Suid,
4423 io_parms->offset, io_parms->length, rc);
4424 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
4425 cifs_dbg(VFS, "Send error in write = %d\n", rc);
4427 *nbytes = le32_to_cpu(rsp->DataLength);
4428 trace_smb3_write_done(xid, req->PersistentFileId,
4429 io_parms->tcon->tid,
4430 io_parms->tcon->ses->Suid,
4431 io_parms->offset, *nbytes);
4434 cifs_small_buf_release(req);
4435 free_rsp_buf(resp_buftype, rsp);
4439 int posix_info_sid_size(const void *beg, const void *end)
4447 subauth = *(u8 *)(beg+1);
4448 if (subauth < 1 || subauth > 15)
4451 total = 1 + 1 + 6 + 4*subauth;
4452 if (beg + total > end)
4458 int posix_info_parse(const void *beg, const void *end,
4459 struct smb2_posix_info_parsed *out)
4465 const void *owner_sid;
4466 const void *group_sid;
4469 /* if no end bound given, assume payload to be correct */
4471 const struct smb2_posix_info *p = beg;
4473 end = beg + le32_to_cpu(p->NextEntryOffset);
4474 /* last element will have a 0 offset, pick a sensible bound */
4479 /* check base buf */
4480 if (beg + sizeof(struct smb2_posix_info) > end)
4482 total_len = sizeof(struct smb2_posix_info);
4484 /* check owner sid */
4485 owner_sid = beg + total_len;
4486 sid_len = posix_info_sid_size(owner_sid, end);
4489 total_len += sid_len;
4491 /* check group sid */
4492 group_sid = beg + total_len;
4493 sid_len = posix_info_sid_size(group_sid, end);
4496 total_len += sid_len;
4498 /* check name len */
4499 if (beg + total_len + 4 > end)
4501 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
4502 if (name_len < 1 || name_len > 0xFFFF)
4507 name = beg + total_len;
4508 if (name + name_len > end)
4510 total_len += name_len;
4514 out->size = total_len;
4515 out->name_len = name_len;
4517 memcpy(&out->owner, owner_sid,
4518 posix_info_sid_size(owner_sid, end));
4519 memcpy(&out->group, group_sid,
4520 posix_info_sid_size(group_sid, end));
4525 static int posix_info_extra_size(const void *beg, const void *end)
4527 int len = posix_info_parse(beg, end, NULL);
4531 return len - sizeof(struct smb2_posix_info);
4535 num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
4539 unsigned int entrycount = 0;
4540 unsigned int next_offset = 0;
4542 FILE_DIRECTORY_INFO *dir_info;
4544 if (bufstart == NULL)
4547 entryptr = bufstart;
4550 if (entryptr + next_offset < entryptr ||
4551 entryptr + next_offset > end_of_buf ||
4552 entryptr + next_offset + size > end_of_buf) {
4553 cifs_dbg(VFS, "malformed search entry would overflow\n");
4557 entryptr = entryptr + next_offset;
4558 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4560 if (infotype == SMB_FIND_FILE_POSIX_INFO)
4561 len = posix_info_extra_size(entryptr, end_of_buf);
4563 len = le32_to_cpu(dir_info->FileNameLength);
4566 entryptr + len < entryptr ||
4567 entryptr + len > end_of_buf ||
4568 entryptr + len + size > end_of_buf) {
4569 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4574 *lastentry = entryptr;
4577 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
4588 int SMB2_query_directory_init(const unsigned int xid,
4589 struct cifs_tcon *tcon,
4590 struct TCP_Server_Info *server,
4591 struct smb_rqst *rqst,
4592 u64 persistent_fid, u64 volatile_fid,
4593 int index, int info_level)
4595 struct smb2_query_directory_req *req;
4596 unsigned char *bufptr;
4597 __le16 asteriks = cpu_to_le16('*');
4598 unsigned int output_size = CIFSMaxBufSize -
4599 MAX_SMB2_CREATE_RESPONSE_SIZE -
4600 MAX_SMB2_CLOSE_RESPONSE_SIZE;
4601 unsigned int total_len;
4602 struct kvec *iov = rqst->rq_iov;
4605 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
4606 (void **) &req, &total_len);
4610 switch (info_level) {
4611 case SMB_FIND_FILE_DIRECTORY_INFO:
4612 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4614 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4615 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4617 case SMB_FIND_FILE_POSIX_INFO:
4618 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
4621 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4626 req->FileIndex = cpu_to_le32(index);
4627 req->PersistentFileId = persistent_fid;
4628 req->VolatileFileId = volatile_fid;
4631 bufptr = req->Buffer;
4632 memcpy(bufptr, &asteriks, len);
4634 req->FileNameOffset =
4635 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
4636 req->FileNameLength = cpu_to_le16(len);
4638 * BB could be 30 bytes or so longer if we used SMB2 specific
4639 * buffer lengths, but this is safe and close enough.
4641 output_size = min_t(unsigned int, output_size, server->maxBuf);
4642 output_size = min_t(unsigned int, output_size, 2 << 15);
4643 req->OutputBufferLength = cpu_to_le32(output_size);
4645 iov[0].iov_base = (char *)req;
4647 iov[0].iov_len = total_len - 1;
4649 iov[1].iov_base = (char *)(req->Buffer);
4650 iov[1].iov_len = len;
4652 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4653 tcon->ses->Suid, index, output_size);
4658 void SMB2_query_directory_free(struct smb_rqst *rqst)
4660 if (rqst && rqst->rq_iov) {
4661 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4666 smb2_parse_query_directory(struct cifs_tcon *tcon,
4667 struct kvec *rsp_iov,
4669 struct cifs_search_info *srch_inf)
4671 struct smb2_query_directory_rsp *rsp;
4672 size_t info_buf_size;
4676 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
4678 switch (srch_inf->info_level) {
4679 case SMB_FIND_FILE_DIRECTORY_INFO:
4680 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4682 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4683 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4685 case SMB_FIND_FILE_POSIX_INFO:
4686 /* note that posix payload are variable size */
4687 info_buf_size = sizeof(struct smb2_posix_info);
4690 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4691 srch_inf->info_level);
4695 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4696 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
4699 cifs_tcon_dbg(VFS, "bad info payload");
4703 srch_inf->unicode = true;
4705 if (srch_inf->ntwrk_buf_start) {
4706 if (srch_inf->smallBuf)
4707 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4709 cifs_buf_release(srch_inf->ntwrk_buf_start);
4711 srch_inf->ntwrk_buf_start = (char *)rsp;
4712 srch_inf->srch_entries_start = srch_inf->last_entry =
4713 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4714 end_of_smb = rsp_iov->iov_len + (char *)rsp;
4716 srch_inf->entries_in_buffer = num_entries(
4717 srch_inf->info_level,
4718 srch_inf->srch_entries_start,
4720 &srch_inf->last_entry,
4723 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4724 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4725 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4726 srch_inf->srch_entries_start, srch_inf->last_entry);
4727 if (resp_buftype == CIFS_LARGE_BUFFER)
4728 srch_inf->smallBuf = false;
4729 else if (resp_buftype == CIFS_SMALL_BUFFER)
4730 srch_inf->smallBuf = true;
4732 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
4738 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4739 u64 persistent_fid, u64 volatile_fid, int index,
4740 struct cifs_search_info *srch_inf)
4742 struct smb_rqst rqst;
4743 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
4744 struct smb2_query_directory_rsp *rsp = NULL;
4745 int resp_buftype = CIFS_NO_BUFFER;
4746 struct kvec rsp_iov;
4748 struct cifs_ses *ses = tcon->ses;
4749 struct TCP_Server_Info *server = cifs_pick_channel(ses);
4752 if (!ses || !(ses->server))
4755 if (smb3_encryption_required(tcon))
4756 flags |= CIFS_TRANSFORM_REQ;
4758 memset(&rqst, 0, sizeof(struct smb_rqst));
4759 memset(&iov, 0, sizeof(iov));
4761 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
4763 rc = SMB2_query_directory_init(xid, tcon, server,
4764 &rqst, persistent_fid,
4765 volatile_fid, index,
4766 srch_inf->info_level);
4770 rc = cifs_send_recv(xid, ses, server,
4771 &rqst, &resp_buftype, flags, &rsp_iov);
4772 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
4775 if (rc == -ENODATA &&
4776 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
4777 trace_smb3_query_dir_done(xid, persistent_fid,
4778 tcon->tid, tcon->ses->Suid, index, 0);
4779 srch_inf->endOfSearch = true;
4782 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4783 tcon->ses->Suid, index, 0, rc);
4784 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
4789 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
4792 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4793 tcon->ses->Suid, index, 0, rc);
4796 resp_buftype = CIFS_NO_BUFFER;
4798 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4799 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
4802 SMB2_query_directory_free(&rqst);
4803 free_rsp_buf(resp_buftype, rsp);
4808 SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
4809 struct smb_rqst *rqst,
4810 u64 persistent_fid, u64 volatile_fid, u32 pid,
4811 u8 info_class, u8 info_type, u32 additional_info,
4812 void **data, unsigned int *size)
4814 struct smb2_set_info_req *req;
4815 struct kvec *iov = rqst->rq_iov;
4816 unsigned int i, total_len;
4819 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
4820 (void **) &req, &total_len);
4824 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4825 req->InfoType = info_type;
4826 req->FileInfoClass = info_class;
4827 req->PersistentFileId = persistent_fid;
4828 req->VolatileFileId = volatile_fid;
4829 req->AdditionalInformation = cpu_to_le32(additional_info);
4832 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
4833 req->BufferLength = cpu_to_le32(*size);
4835 memcpy(req->Buffer, *data, *size);
4838 iov[0].iov_base = (char *)req;
4840 iov[0].iov_len = total_len - 1;
4842 for (i = 1; i < rqst->rq_nvec; i++) {
4843 le32_add_cpu(&req->BufferLength, size[i]);
4844 iov[i].iov_base = (char *)data[i];
4845 iov[i].iov_len = size[i];
4852 SMB2_set_info_free(struct smb_rqst *rqst)
4854 if (rqst && rqst->rq_iov)
4855 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
4859 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4860 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4861 u8 info_type, u32 additional_info, unsigned int num,
4862 void **data, unsigned int *size)
4864 struct smb_rqst rqst;
4865 struct smb2_set_info_rsp *rsp = NULL;
4867 struct kvec rsp_iov;
4870 struct cifs_ses *ses = tcon->ses;
4871 struct TCP_Server_Info *server = cifs_pick_channel(ses);
4874 if (!ses || !server)
4880 if (smb3_encryption_required(tcon))
4881 flags |= CIFS_TRANSFORM_REQ;
4883 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4887 memset(&rqst, 0, sizeof(struct smb_rqst));
4891 rc = SMB2_set_info_init(tcon, server,
4892 &rqst, persistent_fid, volatile_fid, pid,
4893 info_class, info_type, additional_info,
4901 rc = cifs_send_recv(xid, ses, server,
4902 &rqst, &resp_buftype, flags,
4904 SMB2_set_info_free(&rqst);
4905 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
4908 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
4909 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4910 ses->Suid, info_class, (__u32)info_type, rc);
4913 free_rsp_buf(resp_buftype, rsp);
4919 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4920 u64 volatile_fid, u32 pid, __le64 *eof)
4922 struct smb2_file_eof_info info;
4926 info.EndOfFile = *eof;
4929 size = sizeof(struct smb2_file_eof_info);
4931 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4932 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4933 0, 1, &data, &size);
4937 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4938 u64 persistent_fid, u64 volatile_fid,
4939 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4941 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4942 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4943 1, (void **)&pnntsd, &pacllen);
4947 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4948 u64 persistent_fid, u64 volatile_fid,
4949 struct smb2_file_full_ea_info *buf, int len)
4951 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4952 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4953 0, 1, (void **)&buf, &len);
4957 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4958 const u64 persistent_fid, const u64 volatile_fid,
4961 struct smb_rqst rqst;
4963 struct smb2_oplock_break *req = NULL;
4964 struct cifs_ses *ses = tcon->ses;
4965 struct TCP_Server_Info *server = cifs_pick_channel(ses);
4966 int flags = CIFS_OBREAK_OP;
4967 unsigned int total_len;
4969 struct kvec rsp_iov;
4972 cifs_dbg(FYI, "SMB2_oplock_break\n");
4973 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
4974 (void **) &req, &total_len);
4978 if (smb3_encryption_required(tcon))
4979 flags |= CIFS_TRANSFORM_REQ;
4981 req->VolatileFid = volatile_fid;
4982 req->PersistentFid = persistent_fid;
4983 req->OplockLevel = oplock_level;
4984 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4986 flags |= CIFS_NO_RSP_BUF;
4988 iov[0].iov_base = (char *)req;
4989 iov[0].iov_len = total_len;
4991 memset(&rqst, 0, sizeof(struct smb_rqst));
4995 rc = cifs_send_recv(xid, ses, server,
4996 &rqst, &resp_buf_type, flags, &rsp_iov);
4997 cifs_small_buf_release(req);
5000 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5001 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
5008 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
5009 struct kstatfs *kst)
5011 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
5012 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
5013 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
5014 kst->f_bfree = kst->f_bavail =
5015 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
5020 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
5021 struct kstatfs *kst)
5023 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
5024 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
5025 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
5026 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
5027 kst->f_bavail = kst->f_bfree;
5029 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
5030 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5031 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
5032 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5033 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
5039 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
5040 struct TCP_Server_Info *server,
5041 int level, int outbuf_len, u64 persistent_fid,
5045 struct smb2_query_info_req *req;
5046 unsigned int total_len;
5048 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
5050 if ((tcon->ses == NULL) || server == NULL)
5053 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
5054 (void **) &req, &total_len);
5058 req->InfoType = SMB2_O_INFO_FILESYSTEM;
5059 req->FileInfoClass = level;
5060 req->PersistentFileId = persistent_fid;
5061 req->VolatileFileId = volatile_fid;
5063 req->InputBufferOffset =
5064 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
5065 req->OutputBufferLength = cpu_to_le32(
5066 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
5068 iov->iov_base = (char *)req;
5069 iov->iov_len = total_len;
5074 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
5075 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5077 struct smb_rqst rqst;
5078 struct smb2_query_info_rsp *rsp = NULL;
5080 struct kvec rsp_iov;
5083 struct cifs_ses *ses = tcon->ses;
5084 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5085 FILE_SYSTEM_POSIX_INFO *info = NULL;
5088 rc = build_qfs_info_req(&iov, tcon, server,
5089 FS_POSIX_INFORMATION,
5090 sizeof(FILE_SYSTEM_POSIX_INFO),
5091 persistent_fid, volatile_fid);
5095 if (smb3_encryption_required(tcon))
5096 flags |= CIFS_TRANSFORM_REQ;
5098 memset(&rqst, 0, sizeof(struct smb_rqst));
5102 rc = cifs_send_recv(xid, ses, server,
5103 &rqst, &resp_buftype, flags, &rsp_iov);
5104 cifs_small_buf_release(iov.iov_base);
5106 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5107 goto posix_qfsinf_exit;
5109 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5111 info = (FILE_SYSTEM_POSIX_INFO *)(
5112 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5113 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5114 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5115 sizeof(FILE_SYSTEM_POSIX_INFO));
5117 copy_posix_fs_info_to_kstatfs(info, fsdata);
5120 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5125 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5126 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5128 struct smb_rqst rqst;
5129 struct smb2_query_info_rsp *rsp = NULL;
5131 struct kvec rsp_iov;
5134 struct cifs_ses *ses = tcon->ses;
5135 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5136 struct smb2_fs_full_size_info *info = NULL;
5139 rc = build_qfs_info_req(&iov, tcon, server,
5140 FS_FULL_SIZE_INFORMATION,
5141 sizeof(struct smb2_fs_full_size_info),
5142 persistent_fid, volatile_fid);
5146 if (smb3_encryption_required(tcon))
5147 flags |= CIFS_TRANSFORM_REQ;
5149 memset(&rqst, 0, sizeof(struct smb_rqst));
5153 rc = cifs_send_recv(xid, ses, server,
5154 &rqst, &resp_buftype, flags, &rsp_iov);
5155 cifs_small_buf_release(iov.iov_base);
5157 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5160 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5162 info = (struct smb2_fs_full_size_info *)(
5163 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5164 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5165 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5166 sizeof(struct smb2_fs_full_size_info));
5168 smb2_copy_fs_info_to_kstatfs(info, fsdata);
5171 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5176 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
5177 u64 persistent_fid, u64 volatile_fid, int level)
5179 struct smb_rqst rqst;
5180 struct smb2_query_info_rsp *rsp = NULL;
5182 struct kvec rsp_iov;
5184 int resp_buftype, max_len, min_len;
5185 struct cifs_ses *ses = tcon->ses;
5186 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5187 unsigned int rsp_len, offset;
5190 if (level == FS_DEVICE_INFORMATION) {
5191 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5192 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5193 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5194 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5195 min_len = MIN_FS_ATTR_INFO_SIZE;
5196 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5197 max_len = sizeof(struct smb3_fs_ss_info);
5198 min_len = sizeof(struct smb3_fs_ss_info);
5199 } else if (level == FS_VOLUME_INFORMATION) {
5200 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5201 min_len = sizeof(struct smb3_fs_vol_info);
5203 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
5207 rc = build_qfs_info_req(&iov, tcon, server,
5209 persistent_fid, volatile_fid);
5213 if (smb3_encryption_required(tcon))
5214 flags |= CIFS_TRANSFORM_REQ;
5216 memset(&rqst, 0, sizeof(struct smb_rqst));
5220 rc = cifs_send_recv(xid, ses, server,
5221 &rqst, &resp_buftype, flags, &rsp_iov);
5222 cifs_small_buf_release(iov.iov_base);
5224 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5227 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5229 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5230 offset = le16_to_cpu(rsp->OutputBufferOffset);
5231 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
5235 if (level == FS_ATTRIBUTE_INFORMATION)
5236 memcpy(&tcon->fsAttrInfo, offset
5237 + (char *)rsp, min_t(unsigned int,
5239 else if (level == FS_DEVICE_INFORMATION)
5240 memcpy(&tcon->fsDevInfo, offset
5241 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
5242 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5243 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
5244 (offset + (char *)rsp);
5245 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5246 tcon->perf_sector_size =
5247 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
5248 } else if (level == FS_VOLUME_INFORMATION) {
5249 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5250 (offset + (char *)rsp);
5251 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5252 tcon->vol_create_time = vol_info->VolumeCreationTime;
5256 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5261 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5262 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5263 const __u32 num_lock, struct smb2_lock_element *buf)
5265 struct smb_rqst rqst;
5267 struct smb2_lock_req *req = NULL;
5269 struct kvec rsp_iov;
5272 int flags = CIFS_NO_RSP_BUF;
5273 unsigned int total_len;
5274 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5276 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
5278 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
5279 (void **) &req, &total_len);
5283 if (smb3_encryption_required(tcon))
5284 flags |= CIFS_TRANSFORM_REQ;
5286 req->sync_hdr.ProcessId = cpu_to_le32(pid);
5287 req->LockCount = cpu_to_le16(num_lock);
5289 req->PersistentFileId = persist_fid;
5290 req->VolatileFileId = volatile_fid;
5292 count = num_lock * sizeof(struct smb2_lock_element);
5294 iov[0].iov_base = (char *)req;
5295 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
5296 iov[1].iov_base = (char *)buf;
5297 iov[1].iov_len = count;
5299 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
5301 memset(&rqst, 0, sizeof(struct smb_rqst));
5305 rc = cifs_send_recv(xid, tcon->ses, server,
5306 &rqst, &resp_buf_type, flags,
5308 cifs_small_buf_release(req);
5310 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
5311 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
5312 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
5313 tcon->ses->Suid, rc);
5320 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
5321 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5322 const __u64 length, const __u64 offset, const __u32 lock_flags,
5325 struct smb2_lock_element lock;
5327 lock.Offset = cpu_to_le64(offset);
5328 lock.Length = cpu_to_le64(length);
5329 lock.Flags = cpu_to_le32(lock_flags);
5330 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5331 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5333 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5337 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5338 __u8 *lease_key, const __le32 lease_state)
5340 struct smb_rqst rqst;
5342 struct smb2_lease_ack *req = NULL;
5343 struct cifs_ses *ses = tcon->ses;
5344 int flags = CIFS_OBREAK_OP;
5345 unsigned int total_len;
5347 struct kvec rsp_iov;
5349 __u64 *please_key_high;
5350 __u64 *please_key_low;
5351 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5353 cifs_dbg(FYI, "SMB2_lease_break\n");
5354 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5355 (void **) &req, &total_len);
5359 if (smb3_encryption_required(tcon))
5360 flags |= CIFS_TRANSFORM_REQ;
5362 req->sync_hdr.CreditRequest = cpu_to_le16(1);
5363 req->StructureSize = cpu_to_le16(36);
5366 memcpy(req->LeaseKey, lease_key, 16);
5367 req->LeaseState = lease_state;
5369 flags |= CIFS_NO_RSP_BUF;
5371 iov[0].iov_base = (char *)req;
5372 iov[0].iov_len = total_len;
5374 memset(&rqst, 0, sizeof(struct smb_rqst));
5378 rc = cifs_send_recv(xid, ses, server,
5379 &rqst, &resp_buf_type, flags, &rsp_iov);
5380 cifs_small_buf_release(req);
5382 please_key_low = (__u64 *)lease_key;
5383 please_key_high = (__u64 *)(lease_key+8);
5385 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5386 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5387 ses->Suid, *please_key_low, *please_key_high, rc);
5388 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
5390 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5391 ses->Suid, *please_key_low, *please_key_high);