4 * Copyright (C) International Business Machines Corp., 2009, 2013
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)
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)
103 shdr->ProtocolId = SMB2_PROTO_NUMBER;
104 shdr->StructureSize = cpu_to_le16(64);
105 shdr->Command = smb2_cmd;
106 if (tcon && tcon->ses && tcon->ses->server) {
107 struct TCP_Server_Info *server = tcon->ses->server;
109 spin_lock(&server->req_lock);
110 /* Request up to 10 credits but don't go over the limit. */
111 if (server->credits >= server->max_credits)
112 shdr->CreditRequest = cpu_to_le16(0);
114 shdr->CreditRequest = cpu_to_le16(
115 min_t(int, server->max_credits -
116 server->credits, 10));
117 spin_unlock(&server->req_lock);
119 shdr->CreditRequest = cpu_to_le16(2);
121 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
126 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
127 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
128 if ((tcon->ses) && (tcon->ses->server) &&
129 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
130 shdr->CreditCharge = cpu_to_le16(1);
131 /* else CreditCharge MBZ */
133 shdr->TreeId = tcon->tid;
134 /* Uid is not converted */
136 shdr->SessionId = tcon->ses->Suid;
139 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
140 * to pass the path on the Open SMB prefixed by \\server\share.
141 * Not sure when we would need to do the augmented path (if ever) and
142 * setting this flag breaks the SMB2 open operation since it is
143 * illegal to send an empty path name (without \\server\share prefix)
144 * when the DFS flag is set in the SMB open header. We could
145 * consider setting the flag on all operations other than open
146 * but it is safer to net set it for now.
148 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
149 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
151 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
152 !smb3_encryption_required(tcon))
153 shdr->Flags |= SMB2_FLAGS_SIGNED;
158 #ifdef CONFIG_CIFS_DFS_UPCALL
159 static int __smb2_reconnect(const struct nls_table *nlsc,
160 struct cifs_tcon *tcon)
163 struct dfs_cache_tgt_list tl;
164 struct dfs_cache_tgt_iterator *it = NULL;
166 const char *tcp_host;
168 const char *dfs_host;
171 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
176 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
177 tcon->ses->server->hostname);
178 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
182 if (!tcon->dfs_path) {
183 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
187 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl);
191 extract_unc_hostname(tcon->ses->server->hostname, &tcp_host,
194 for (it = dfs_cache_get_tgt_iterator(&tl); it;
195 it = dfs_cache_get_next_tgt(&tl, it)) {
196 const char *share, *prefix;
197 size_t share_len, prefix_len;
199 rc = dfs_cache_get_tgt_share(it, &share, &share_len, &prefix,
202 cifs_dbg(VFS, "%s: failed to parse target share %d\n",
207 extract_unc_hostname(share, &dfs_host, &dfs_host_len);
209 if (dfs_host_len != tcp_host_len
210 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
211 cifs_dbg(FYI, "%s: skipping %.*s, doesn't match %.*s",
213 (int)dfs_host_len, dfs_host,
214 (int)tcp_host_len, tcp_host);
218 scnprintf(tree, MAX_TREE_SIZE, "\\%.*s", (int)share_len, share);
220 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
222 rc = update_super_prepath(tcon, prefix, prefix_len);
231 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1,
236 dfs_cache_free_tgts(&tl);
242 static inline int __smb2_reconnect(const struct nls_table *nlsc,
243 struct cifs_tcon *tcon)
245 return SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
250 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
253 struct nls_table *nls_codepage;
254 struct cifs_ses *ses;
255 struct TCP_Server_Info *server;
259 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
260 * check for tcp and smb session status done differently
261 * for those three - in the calling routine.
266 if (smb2_command == SMB2_TREE_CONNECT)
269 if (tcon->tidStatus == CifsExiting) {
271 * only tree disconnect, open, and write,
272 * (and ulogoff which does not have tcon)
273 * are allowed as we start force umount.
275 if ((smb2_command != SMB2_WRITE) &&
276 (smb2_command != SMB2_CREATE) &&
277 (smb2_command != SMB2_TREE_DISCONNECT)) {
278 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
283 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
284 (!tcon->ses->server))
288 server = ses->server;
290 retries = server->nr_targets;
293 * Give demultiplex thread up to 10 seconds to each target available for
294 * reconnect -- should be greater than cifs socket timeout which is 7
297 while (server->tcpStatus == CifsNeedReconnect) {
299 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
300 * here since they are implicitly done when session drops.
302 switch (smb2_command) {
304 * BB Should we keep oplock break and add flush to exceptions?
306 case SMB2_TREE_DISCONNECT:
309 case SMB2_OPLOCK_BREAK:
313 rc = wait_event_interruptible_timeout(server->response_q,
314 (server->tcpStatus != CifsNeedReconnect),
317 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
318 " signal by the process\n", __func__);
322 /* are we still trying to reconnect? */
323 if (server->tcpStatus != CifsNeedReconnect)
326 if (retries && --retries)
330 * on "soft" mounts we wait once. Hard mounts keep
331 * retrying until process is killed or server comes
335 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
338 retries = server->nr_targets;
341 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
344 nls_codepage = load_nls_default();
347 * need to prevent multiple threads trying to simultaneously reconnect
348 * the same SMB session
350 mutex_lock(&tcon->ses->session_mutex);
353 * Recheck after acquire mutex. If another thread is negotiating
354 * and the server never sends an answer the socket will be closed
355 * and tcpStatus set to reconnect.
357 if (server->tcpStatus == CifsNeedReconnect) {
359 mutex_unlock(&tcon->ses->session_mutex);
363 rc = cifs_negotiate_protocol(0, tcon->ses);
364 if (!rc && tcon->ses->need_reconnect) {
365 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
366 if ((rc == -EACCES) && !tcon->retry) {
368 mutex_unlock(&tcon->ses->session_mutex);
372 if (rc || !tcon->need_reconnect) {
373 mutex_unlock(&tcon->ses->session_mutex);
377 cifs_mark_open_files_invalid(tcon);
378 if (tcon->use_persistent)
379 tcon->need_reopen_files = true;
381 rc = __smb2_reconnect(nls_codepage, tcon);
382 mutex_unlock(&tcon->ses->session_mutex);
384 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
386 /* If sess reconnected but tcon didn't, something strange ... */
387 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
391 if (smb2_command != SMB2_INTERNAL_CMD)
392 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
394 atomic_inc(&tconInfoReconnectCount);
397 * Check if handle based operation so we know whether we can continue
398 * or not without returning to caller to reset file handle.
401 * BB Is flush done by server on drop of tcp session? Should we special
402 * case it and skip above?
404 switch (smb2_command) {
410 case SMB2_QUERY_DIRECTORY:
411 case SMB2_CHANGE_NOTIFY:
412 case SMB2_QUERY_INFO:
417 unload_nls(nls_codepage);
422 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
423 unsigned int *total_len)
425 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
426 /* lookup word count ie StructureSize from table */
427 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
430 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
431 * largest operations (Create)
435 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
436 spdu->StructureSize2 = cpu_to_le16(parmsize);
438 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
442 * Allocate and return pointer to an SMB request hdr, and set basic
443 * SMB information in the SMB header. If the return code is zero, this
444 * function must have filled in request_buf pointer.
446 static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
447 void **request_buf, unsigned int *total_len)
449 /* BB eventually switch this to SMB2 specific small buf size */
450 if (smb2_command == SMB2_SET_INFO)
451 *request_buf = cifs_buf_get();
453 *request_buf = cifs_small_buf_get();
454 if (*request_buf == NULL) {
455 /* BB should we add a retry in here if not a writepage? */
459 fill_small_buf(smb2_command, tcon,
460 (struct smb2_sync_hdr *)(*request_buf),
464 uint16_t com_code = le16_to_cpu(smb2_command);
465 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
466 cifs_stats_inc(&tcon->num_smbs_sent);
472 static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
473 void **request_buf, unsigned int *total_len)
477 rc = smb2_reconnect(smb2_command, tcon);
481 return __smb2_plain_req_init(smb2_command, tcon, request_buf,
485 static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
486 void **request_buf, unsigned int *total_len)
488 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
489 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
490 return __smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf,
493 return smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf, total_len);
496 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
499 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
501 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
502 pneg_ctxt->DataLength = cpu_to_le16(38);
503 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
504 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
505 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
506 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
510 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
512 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
513 pneg_ctxt->DataLength =
514 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
515 - sizeof(struct smb2_neg_context));
516 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
517 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
518 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
519 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
523 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
525 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
526 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + two ciphers */
527 pneg_ctxt->CipherCount = cpu_to_le16(2);
528 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
529 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
533 build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
535 struct nls_table *cp = load_nls_default();
537 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
539 /* copy up to max of first 100 bytes of server name to NetName field */
540 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
541 /* context size is DataLength + minimal smb2_neg_context */
542 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
543 sizeof(struct smb2_neg_context), 8) * 8;
547 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
549 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
550 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
551 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
552 pneg_ctxt->Name[0] = 0x93;
553 pneg_ctxt->Name[1] = 0xAD;
554 pneg_ctxt->Name[2] = 0x25;
555 pneg_ctxt->Name[3] = 0x50;
556 pneg_ctxt->Name[4] = 0x9C;
557 pneg_ctxt->Name[5] = 0xB4;
558 pneg_ctxt->Name[6] = 0x11;
559 pneg_ctxt->Name[7] = 0xE7;
560 pneg_ctxt->Name[8] = 0xB4;
561 pneg_ctxt->Name[9] = 0x23;
562 pneg_ctxt->Name[10] = 0x83;
563 pneg_ctxt->Name[11] = 0xDE;
564 pneg_ctxt->Name[12] = 0x96;
565 pneg_ctxt->Name[13] = 0x8B;
566 pneg_ctxt->Name[14] = 0xCD;
567 pneg_ctxt->Name[15] = 0x7C;
571 assemble_neg_contexts(struct smb2_negotiate_req *req,
572 struct TCP_Server_Info *server, unsigned int *total_len)
575 unsigned int ctxt_len;
577 if (*total_len > 200) {
578 /* In case length corrupted don't want to overrun smb buffer */
579 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
584 * round up total_len of fixed part of SMB3 negotiate request to 8
585 * byte boundary before adding negotiate contexts
587 *total_len = roundup(*total_len, 8);
589 pneg_ctxt = (*total_len) + (char *)req;
590 req->NegotiateContextOffset = cpu_to_le32(*total_len);
592 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
593 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
594 *total_len += ctxt_len;
595 pneg_ctxt += ctxt_len;
597 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
598 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
599 *total_len += ctxt_len;
600 pneg_ctxt += ctxt_len;
602 if (server->compress_algorithm) {
603 build_compression_ctxt((struct smb2_compression_capabilities_context *)
605 ctxt_len = DIV_ROUND_UP(
606 sizeof(struct smb2_compression_capabilities_context),
608 *total_len += ctxt_len;
609 pneg_ctxt += ctxt_len;
610 req->NegotiateContextCount = cpu_to_le16(5);
612 req->NegotiateContextCount = cpu_to_le16(4);
614 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
616 *total_len += ctxt_len;
617 pneg_ctxt += ctxt_len;
619 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
620 *total_len += sizeof(struct smb2_posix_neg_context);
623 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
625 unsigned int len = le16_to_cpu(ctxt->DataLength);
627 /* If invalid preauth context warn but use what we requested, SHA-512 */
628 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
629 printk_once(KERN_WARNING "server sent bad preauth context\n");
632 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
633 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
634 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
635 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
638 static void decode_compress_ctx(struct TCP_Server_Info *server,
639 struct smb2_compression_capabilities_context *ctxt)
641 unsigned int len = le16_to_cpu(ctxt->DataLength);
643 /* sizeof compress context is a one element compression capbility struct */
645 printk_once(KERN_WARNING "server sent bad compression cntxt\n");
648 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
649 printk_once(KERN_WARNING "illegal SMB3 compress algorithm count\n");
652 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
653 printk_once(KERN_WARNING "unknown compression algorithm\n");
656 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
659 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
660 struct smb2_encryption_neg_context *ctxt)
662 unsigned int len = le16_to_cpu(ctxt->DataLength);
664 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
665 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
666 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
670 if (le16_to_cpu(ctxt->CipherCount) != 1) {
671 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
674 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
675 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
676 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
677 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
680 server->cipher_type = ctxt->Ciphers[0];
681 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
685 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
686 struct TCP_Server_Info *server,
687 unsigned int len_of_smb)
689 struct smb2_neg_context *pctx;
690 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
691 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
692 unsigned int len_of_ctxts, i;
695 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
696 if (len_of_smb <= offset) {
697 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
701 len_of_ctxts = len_of_smb - offset;
703 for (i = 0; i < ctxt_cnt; i++) {
705 /* check that offset is not beyond end of SMB */
706 if (len_of_ctxts == 0)
709 if (len_of_ctxts < sizeof(struct smb2_neg_context))
712 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
713 clen = le16_to_cpu(pctx->DataLength);
714 if (clen > len_of_ctxts)
717 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
718 decode_preauth_context(
719 (struct smb2_preauth_neg_context *)pctx);
720 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
721 rc = decode_encrypt_ctx(server,
722 (struct smb2_encryption_neg_context *)pctx);
723 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
724 decode_compress_ctx(server,
725 (struct smb2_compression_capabilities_context *)pctx);
726 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
727 server->posix_ext_supported = true;
729 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
730 le16_to_cpu(pctx->ContextType));
734 /* offsets must be 8 byte aligned */
735 clen = (clen + 7) & ~0x7;
736 offset += clen + sizeof(struct smb2_neg_context);
737 len_of_ctxts -= clen;
742 static struct create_posix *
743 create_posix_buf(umode_t mode)
745 struct create_posix *buf;
747 buf = kzalloc(sizeof(struct create_posix),
752 buf->ccontext.DataOffset =
753 cpu_to_le16(offsetof(struct create_posix, Mode));
754 buf->ccontext.DataLength = cpu_to_le32(4);
755 buf->ccontext.NameOffset =
756 cpu_to_le16(offsetof(struct create_posix, Name));
757 buf->ccontext.NameLength = cpu_to_le16(16);
759 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
770 buf->Name[10] = 0x83;
771 buf->Name[11] = 0xDE;
772 buf->Name[12] = 0x96;
773 buf->Name[13] = 0x8B;
774 buf->Name[14] = 0xCD;
775 buf->Name[15] = 0x7C;
776 buf->Mode = cpu_to_le32(mode);
777 cifs_dbg(FYI, "mode on posix create 0%o", mode);
782 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
784 struct smb2_create_req *req = iov[0].iov_base;
785 unsigned int num = *num_iovec;
787 iov[num].iov_base = create_posix_buf(mode);
788 if (mode == ACL_NO_MODE)
789 cifs_dbg(FYI, "illegal mode\n");
790 if (iov[num].iov_base == NULL)
792 iov[num].iov_len = sizeof(struct create_posix);
793 if (!req->CreateContextsOffset)
794 req->CreateContextsOffset = cpu_to_le32(
795 sizeof(struct smb2_create_req) +
796 iov[num - 1].iov_len);
797 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
798 *num_iovec = num + 1;
805 * SMB2 Worker functions follow:
807 * The general structure of the worker functions is:
808 * 1) Call smb2_init (assembles SMB2 header)
809 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
810 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
811 * 4) Decode SMB2 command specific fields in the fixed length area
812 * 5) Decode variable length data area (if any for this SMB2 command type)
813 * 6) Call free smb buffer
819 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
821 struct smb_rqst rqst;
822 struct smb2_negotiate_req *req;
823 struct smb2_negotiate_rsp *rsp;
828 struct TCP_Server_Info *server = cifs_ses_server(ses);
829 int blob_offset, blob_length;
831 int flags = CIFS_NEG_OP;
832 unsigned int total_len;
834 cifs_dbg(FYI, "Negotiate protocol\n");
837 WARN(1, "%s: server is NULL!\n", __func__);
841 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
845 req->sync_hdr.SessionId = 0;
847 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
848 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
850 if (strcmp(server->vals->version_string,
851 SMB3ANY_VERSION_STRING) == 0) {
852 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
853 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
854 req->DialectCount = cpu_to_le16(2);
856 } else if (strcmp(server->vals->version_string,
857 SMBDEFAULT_VERSION_STRING) == 0) {
858 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
859 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
860 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
861 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
862 req->DialectCount = cpu_to_le16(4);
865 /* otherwise send specific dialect */
866 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
867 req->DialectCount = cpu_to_le16(1);
871 /* only one of SMB2 signing flags may be set in SMB2 request */
873 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
874 else if (global_secflags & CIFSSEC_MAY_SIGN)
875 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
877 req->SecurityMode = 0;
879 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
881 /* ClientGUID must be zero for SMB2.02 dialect */
882 if (server->vals->protocol_id == SMB20_PROT_ID)
883 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
885 memcpy(req->ClientGUID, server->client_guid,
886 SMB2_CLIENT_GUID_SIZE);
887 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
888 (strcmp(server->vals->version_string,
889 SMBDEFAULT_VERSION_STRING) == 0))
890 assemble_neg_contexts(req, server, &total_len);
892 iov[0].iov_base = (char *)req;
893 iov[0].iov_len = total_len;
895 memset(&rqst, 0, sizeof(struct smb_rqst));
899 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
900 cifs_small_buf_release(req);
901 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
903 * No tcon so can't do
904 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
906 if (rc == -EOPNOTSUPP) {
907 cifs_server_dbg(VFS, "Dialect not supported by server. Consider "
908 "specifying vers=1.0 or vers=2.0 on mount for accessing"
914 if (strcmp(server->vals->version_string,
915 SMB3ANY_VERSION_STRING) == 0) {
916 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
918 "SMB2 dialect returned but not requested\n");
920 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
922 "SMB2.1 dialect returned but not requested\n");
925 } else if (strcmp(server->vals->version_string,
926 SMBDEFAULT_VERSION_STRING) == 0) {
927 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
929 "SMB2 dialect returned but not requested\n");
931 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
932 /* ops set to 3.0 by default for default so update */
933 server->ops = &smb21_operations;
934 server->vals = &smb21_values;
935 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
936 server->ops = &smb311_operations;
937 server->vals = &smb311_values;
939 } else if (le16_to_cpu(rsp->DialectRevision) !=
940 server->vals->protocol_id) {
941 /* if requested single dialect ensure returned dialect matched */
942 cifs_server_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
943 le16_to_cpu(rsp->DialectRevision));
947 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
949 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
950 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
951 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
952 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
953 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
954 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
955 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
956 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
957 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
958 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
960 cifs_server_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
961 le16_to_cpu(rsp->DialectRevision));
965 server->dialect = le16_to_cpu(rsp->DialectRevision);
968 * Keep a copy of the hash after negprot. This hash will be
969 * the starting hash value for all sessions made from this
972 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
973 SMB2_PREAUTH_HASH_SIZE);
975 /* SMB2 only has an extended negflavor */
976 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
977 /* set it to the maximum buffer size value we can send with 1 credit */
978 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
979 SMB2_MAX_BUFFER_SIZE);
980 server->max_read = le32_to_cpu(rsp->MaxReadSize);
981 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
982 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
983 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
984 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
986 server->capabilities = le32_to_cpu(rsp->Capabilities);
988 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
990 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
991 (struct smb2_sync_hdr *)rsp);
993 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
995 * ses->sectype = RawNTLMSSP;
996 * but for time being this is our only auth choice so doesn't matter.
997 * We just found a server which sets blob length to zero expecting raw.
999 if (blob_length == 0) {
1000 cifs_dbg(FYI, "missing security blob on negprot\n");
1001 server->sec_ntlmssp = true;
1004 rc = cifs_enable_signing(server, ses->sign);
1008 rc = decode_negTokenInit(security_blob, blob_length, server);
1015 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1016 if (rsp->NegotiateContextCount)
1017 rc = smb311_decode_neg_context(rsp, server,
1020 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
1023 free_rsp_buf(resp_buftype, rsp);
1027 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1030 struct validate_negotiate_info_req *pneg_inbuf;
1031 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
1033 u32 inbuflen; /* max of 4 dialects */
1034 struct TCP_Server_Info *server = tcon->ses->server;
1036 cifs_dbg(FYI, "validate negotiate\n");
1038 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1039 if (server->dialect == SMB311_PROT_ID)
1043 * validation ioctl must be signed, so no point sending this if we
1044 * can not sign it (ie are not known user). Even if signing is not
1045 * required (enabled but not negotiated), in those cases we selectively
1046 * sign just this, the first and only signed request on a connection.
1047 * Having validation of negotiate info helps reduce attack vectors.
1049 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1050 return 0; /* validation requires signing */
1052 if (tcon->ses->user_name == NULL) {
1053 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1054 return 0; /* validation requires signing */
1057 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1058 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1060 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1064 pneg_inbuf->Capabilities =
1065 cpu_to_le32(server->vals->req_capabilities);
1066 memcpy(pneg_inbuf->Guid, server->client_guid,
1067 SMB2_CLIENT_GUID_SIZE);
1069 if (tcon->ses->sign)
1070 pneg_inbuf->SecurityMode =
1071 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1072 else if (global_secflags & CIFSSEC_MAY_SIGN)
1073 pneg_inbuf->SecurityMode =
1074 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1076 pneg_inbuf->SecurityMode = 0;
1079 if (strcmp(server->vals->version_string,
1080 SMB3ANY_VERSION_STRING) == 0) {
1081 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1082 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1083 pneg_inbuf->DialectCount = cpu_to_le16(2);
1084 /* structure is big enough for 3 dialects, sending only 2 */
1085 inbuflen = sizeof(*pneg_inbuf) -
1086 (2 * sizeof(pneg_inbuf->Dialects[0]));
1087 } else if (strcmp(server->vals->version_string,
1088 SMBDEFAULT_VERSION_STRING) == 0) {
1089 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1090 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1091 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1092 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1093 pneg_inbuf->DialectCount = cpu_to_le16(4);
1094 /* structure is big enough for 3 dialects */
1095 inbuflen = sizeof(*pneg_inbuf);
1097 /* otherwise specific dialect was requested */
1098 pneg_inbuf->Dialects[0] =
1099 cpu_to_le16(server->vals->protocol_id);
1100 pneg_inbuf->DialectCount = cpu_to_le16(1);
1101 /* structure is big enough for 3 dialects, sending only 1 */
1102 inbuflen = sizeof(*pneg_inbuf) -
1103 sizeof(pneg_inbuf->Dialects[0]) * 2;
1106 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1107 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
1108 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1109 (char **)&pneg_rsp, &rsplen);
1110 if (rc == -EOPNOTSUPP) {
1112 * Old Windows versions or Netapp SMB server can return
1113 * not supported error. Client should accept it.
1115 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1117 goto out_free_inbuf;
1118 } else if (rc != 0) {
1119 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
1121 goto out_free_inbuf;
1125 if (rsplen != sizeof(*pneg_rsp)) {
1126 cifs_tcon_dbg(VFS, "invalid protocol negotiate response size: %d\n",
1129 /* relax check since Mac returns max bufsize allowed on ioctl */
1130 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1134 /* check validate negotiate info response matches what we got earlier */
1135 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1138 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1141 /* do not validate server guid because not saved at negprot time yet */
1143 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1144 SMB2_LARGE_FILES) != server->capabilities)
1147 /* validate negotiate successful */
1149 cifs_dbg(FYI, "validate negotiate info successful\n");
1153 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1162 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1164 switch (requested) {
1171 if (server->sec_ntlmssp &&
1172 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1174 if ((server->sec_kerberos || server->sec_mskerberos) &&
1175 (global_secflags & CIFSSEC_MAY_KRB5))
1183 struct SMB2_sess_data {
1185 struct cifs_ses *ses;
1186 struct nls_table *nls_cp;
1187 void (*func)(struct SMB2_sess_data *);
1189 u64 previous_session;
1191 /* we will send the SMB in three pieces:
1192 * a fixed length beginning part, an optional
1193 * SPNEGO blob (which can be zero length), and a
1194 * last part which will include the strings
1195 * and rest of bcc area. This allows us to avoid
1196 * a large buffer 17K allocation
1203 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1206 struct cifs_ses *ses = sess_data->ses;
1207 struct smb2_sess_setup_req *req;
1208 struct TCP_Server_Info *server = cifs_ses_server(ses);
1209 unsigned int total_len;
1211 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1216 if (sess_data->ses->binding) {
1217 req->sync_hdr.SessionId = sess_data->ses->Suid;
1218 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1219 req->PreviousSessionId = 0;
1220 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1222 /* First session, not a reauthenticate */
1223 req->sync_hdr.SessionId = 0;
1225 * if reconnect, we need to send previous sess id
1228 req->PreviousSessionId = sess_data->previous_session;
1229 req->Flags = 0; /* MBZ */
1232 /* enough to enable echos and oplocks and one max size write */
1233 req->sync_hdr.CreditRequest = cpu_to_le16(130);
1235 /* only one of SMB2 signing flags may be set in SMB2 request */
1237 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1238 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1239 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1241 req->SecurityMode = 0;
1243 #ifdef CONFIG_CIFS_DFS_UPCALL
1244 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1246 req->Capabilities = 0;
1247 #endif /* DFS_UPCALL */
1249 req->Channel = 0; /* MBZ */
1251 sess_data->iov[0].iov_base = (char *)req;
1253 sess_data->iov[0].iov_len = total_len - 1;
1255 * This variable will be used to clear the buffer
1256 * allocated above in case of any error in the calling function.
1258 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1264 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1266 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1267 sess_data->buf0_type = CIFS_NO_BUFFER;
1271 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1274 struct smb_rqst rqst;
1275 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1276 struct kvec rsp_iov = { NULL, 0 };
1278 /* Testing shows that buffer offset must be at location of Buffer[0] */
1279 req->SecurityBufferOffset =
1280 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1281 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1283 memset(&rqst, 0, sizeof(struct smb_rqst));
1284 rqst.rq_iov = sess_data->iov;
1287 /* BB add code to build os and lm fields */
1288 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1290 &sess_data->buf0_type,
1291 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
1292 cifs_small_buf_release(sess_data->iov[0].iov_base);
1293 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1299 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1302 struct cifs_ses *ses = sess_data->ses;
1303 struct TCP_Server_Info *server = cifs_ses_server(ses);
1305 mutex_lock(&server->srv_mutex);
1306 if (server->ops->generate_signingkey) {
1307 rc = server->ops->generate_signingkey(ses);
1310 "SMB3 session key generation failed\n");
1311 mutex_unlock(&server->srv_mutex);
1315 if (!server->session_estab) {
1316 server->sequence_number = 0x2;
1317 server->session_estab = true;
1319 mutex_unlock(&server->srv_mutex);
1321 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1322 /* keep existing ses state if binding */
1323 if (!ses->binding) {
1324 spin_lock(&GlobalMid_Lock);
1325 ses->status = CifsGood;
1326 ses->need_reconnect = false;
1327 spin_unlock(&GlobalMid_Lock);
1333 #ifdef CONFIG_CIFS_UPCALL
1335 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1338 struct cifs_ses *ses = sess_data->ses;
1339 struct cifs_spnego_msg *msg;
1340 struct key *spnego_key = NULL;
1341 struct smb2_sess_setup_rsp *rsp = NULL;
1343 rc = SMB2_sess_alloc_buffer(sess_data);
1347 spnego_key = cifs_get_spnego_key(ses);
1348 if (IS_ERR(spnego_key)) {
1349 rc = PTR_ERR(spnego_key);
1354 msg = spnego_key->payload.data[0];
1356 * check version field to make sure that cifs.upcall is
1357 * sending us a response in an expected form
1359 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1361 "bad cifs.upcall version. Expected %d got %d",
1362 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1364 goto out_put_spnego_key;
1367 /* keep session key if binding */
1368 if (!ses->binding) {
1369 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1371 if (!ses->auth_key.response) {
1373 "Kerberos can't allocate (%u bytes) memory",
1376 goto out_put_spnego_key;
1378 ses->auth_key.len = msg->sesskey_len;
1381 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1382 sess_data->iov[1].iov_len = msg->secblob_len;
1384 rc = SMB2_sess_sendreceive(sess_data);
1386 goto out_put_spnego_key;
1388 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1389 /* keep session id and flags if binding */
1390 if (!ses->binding) {
1391 ses->Suid = rsp->sync_hdr.SessionId;
1392 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1395 rc = SMB2_sess_establish_session(sess_data);
1397 key_invalidate(spnego_key);
1398 key_put(spnego_key);
1400 sess_data->result = rc;
1401 sess_data->func = NULL;
1402 SMB2_sess_free_buffer(sess_data);
1406 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1408 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1409 sess_data->result = -EOPNOTSUPP;
1410 sess_data->func = NULL;
1415 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1418 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1421 struct cifs_ses *ses = sess_data->ses;
1422 struct smb2_sess_setup_rsp *rsp = NULL;
1423 char *ntlmssp_blob = NULL;
1424 bool use_spnego = false; /* else use raw ntlmssp */
1425 u16 blob_length = 0;
1428 * If memory allocation is successful, caller of this function
1431 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1432 if (!ses->ntlmssp) {
1436 ses->ntlmssp->sesskey_per_smbsess = true;
1438 rc = SMB2_sess_alloc_buffer(sess_data);
1442 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1444 if (ntlmssp_blob == NULL) {
1449 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1451 /* BB eventually need to add this */
1452 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1456 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1457 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1459 sess_data->iov[1].iov_base = ntlmssp_blob;
1460 sess_data->iov[1].iov_len = blob_length;
1462 rc = SMB2_sess_sendreceive(sess_data);
1463 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1465 /* If true, rc here is expected and not an error */
1466 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1467 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1473 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1474 le16_to_cpu(rsp->SecurityBufferOffset)) {
1475 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1476 le16_to_cpu(rsp->SecurityBufferOffset));
1480 rc = decode_ntlmssp_challenge(rsp->Buffer,
1481 le16_to_cpu(rsp->SecurityBufferLength), ses);
1485 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1487 /* keep existing ses id and flags if binding */
1488 if (!ses->binding) {
1489 ses->Suid = rsp->sync_hdr.SessionId;
1490 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1494 kfree(ntlmssp_blob);
1495 SMB2_sess_free_buffer(sess_data);
1497 sess_data->result = 0;
1498 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1502 kfree(ses->ntlmssp);
1503 ses->ntlmssp = NULL;
1504 sess_data->result = rc;
1505 sess_data->func = NULL;
1509 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1512 struct cifs_ses *ses = sess_data->ses;
1513 struct smb2_sess_setup_req *req;
1514 struct smb2_sess_setup_rsp *rsp = NULL;
1515 unsigned char *ntlmssp_blob = NULL;
1516 bool use_spnego = false; /* else use raw ntlmssp */
1517 u16 blob_length = 0;
1519 rc = SMB2_sess_alloc_buffer(sess_data);
1523 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1524 req->sync_hdr.SessionId = ses->Suid;
1526 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1529 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1534 /* BB eventually need to add this */
1535 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1539 sess_data->iov[1].iov_base = ntlmssp_blob;
1540 sess_data->iov[1].iov_len = blob_length;
1542 rc = SMB2_sess_sendreceive(sess_data);
1546 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1548 /* keep existing ses id and flags if binding */
1549 if (!ses->binding) {
1550 ses->Suid = rsp->sync_hdr.SessionId;
1551 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1554 rc = SMB2_sess_establish_session(sess_data);
1555 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1556 if (ses->server->dialect < SMB30_PROT_ID) {
1557 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1559 * The session id is opaque in terms of endianness, so we can't
1560 * print it as a long long. we dump it as we got it on the wire
1562 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1564 cifs_dbg(VFS, "Session Key %*ph\n",
1565 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1566 cifs_dbg(VFS, "Signing Key %*ph\n",
1567 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1571 kfree(ntlmssp_blob);
1572 SMB2_sess_free_buffer(sess_data);
1573 kfree(ses->ntlmssp);
1574 ses->ntlmssp = NULL;
1575 sess_data->result = rc;
1576 sess_data->func = NULL;
1580 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1584 type = smb2_select_sectype(cifs_ses_server(ses), ses->sectype);
1585 cifs_dbg(FYI, "sess setup type %d\n", type);
1586 if (type == Unspecified) {
1588 "Unable to select appropriate authentication method!");
1594 sess_data->func = SMB2_auth_kerberos;
1597 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1600 cifs_dbg(VFS, "secType %d not supported!\n", type);
1608 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1609 const struct nls_table *nls_cp)
1612 struct TCP_Server_Info *server = cifs_ses_server(ses);
1613 struct SMB2_sess_data *sess_data;
1615 cifs_dbg(FYI, "Session Setup\n");
1618 WARN(1, "%s: server is NULL!\n", __func__);
1622 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1626 rc = SMB2_select_sec(ses, sess_data);
1629 sess_data->xid = xid;
1630 sess_data->ses = ses;
1631 sess_data->buf0_type = CIFS_NO_BUFFER;
1632 sess_data->nls_cp = (struct nls_table *) nls_cp;
1633 sess_data->previous_session = ses->Suid;
1636 * Initialize the session hash with the server one.
1638 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
1639 SMB2_PREAUTH_HASH_SIZE);
1641 while (sess_data->func)
1642 sess_data->func(sess_data);
1644 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1645 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1646 rc = sess_data->result;
1653 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1655 struct smb_rqst rqst;
1656 struct smb2_logoff_req *req; /* response is also trivial struct */
1658 struct TCP_Server_Info *server;
1660 unsigned int total_len;
1662 struct kvec rsp_iov;
1665 cifs_dbg(FYI, "disconnect session %p\n", ses);
1667 if (ses && (ses->server))
1668 server = ses->server;
1672 /* no need to send SMB logoff if uid already closed due to reconnect */
1673 if (ses->need_reconnect)
1674 goto smb2_session_already_dead;
1676 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
1680 /* since no tcon, smb2_init can not do this, so do here */
1681 req->sync_hdr.SessionId = ses->Suid;
1683 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1684 flags |= CIFS_TRANSFORM_REQ;
1685 else if (server->sign)
1686 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1688 flags |= CIFS_NO_RSP_BUF;
1690 iov[0].iov_base = (char *)req;
1691 iov[0].iov_len = total_len;
1693 memset(&rqst, 0, sizeof(struct smb_rqst));
1697 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1698 cifs_small_buf_release(req);
1700 * No tcon so can't do
1701 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1704 smb2_session_already_dead:
1708 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1710 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1713 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1715 /* These are similar values to what Windows uses */
1716 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1718 tcon->max_chunks = 256;
1719 tcon->max_bytes_chunk = 1048576;
1720 tcon->max_bytes_copy = 16777216;
1724 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1725 struct cifs_tcon *tcon, const struct nls_table *cp)
1727 struct smb_rqst rqst;
1728 struct smb2_tree_connect_req *req;
1729 struct smb2_tree_connect_rsp *rsp = NULL;
1731 struct kvec rsp_iov = { NULL, 0 };
1735 __le16 *unc_path = NULL;
1737 unsigned int total_len;
1738 struct TCP_Server_Info *server = ses->server;
1740 cifs_dbg(FYI, "TCON\n");
1742 if (!server || !tree)
1745 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1746 if (unc_path == NULL)
1749 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1751 if (unc_path_len < 2) {
1756 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1758 atomic_set(&tcon->num_remote_opens, 0);
1759 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1766 if (smb3_encryption_required(tcon))
1767 flags |= CIFS_TRANSFORM_REQ;
1769 iov[0].iov_base = (char *)req;
1771 iov[0].iov_len = total_len - 1;
1773 /* Testing shows that buffer offset must be at location of Buffer[0] */
1774 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1776 req->PathLength = cpu_to_le16(unc_path_len - 2);
1777 iov[1].iov_base = unc_path;
1778 iov[1].iov_len = unc_path_len;
1781 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1782 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1783 * (Samba servers don't always set the flag so also check if null user)
1785 if ((server->dialect == SMB311_PROT_ID) &&
1786 !smb3_encryption_required(tcon) &&
1787 !(ses->session_flags &
1788 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1789 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1790 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1792 memset(&rqst, 0, sizeof(struct smb_rqst));
1796 /* Need 64 for max size write so ask for more in case not there yet */
1797 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1799 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
1800 cifs_small_buf_release(req);
1801 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1802 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1805 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1806 tcon->need_reconnect = true;
1808 goto tcon_error_exit;
1811 switch (rsp->ShareType) {
1812 case SMB2_SHARE_TYPE_DISK:
1813 cifs_dbg(FYI, "connection to disk share\n");
1815 case SMB2_SHARE_TYPE_PIPE:
1817 cifs_dbg(FYI, "connection to pipe share\n");
1819 case SMB2_SHARE_TYPE_PRINT:
1821 cifs_dbg(FYI, "connection to printer\n");
1824 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1826 goto tcon_error_exit;
1829 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1830 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1831 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1832 tcon->tidStatus = CifsGood;
1833 tcon->need_reconnect = false;
1834 tcon->tid = rsp->sync_hdr.TreeId;
1835 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1837 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1838 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1839 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
1842 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1843 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
1845 init_copy_chunk_defaults(tcon);
1846 if (server->ops->validate_negotiate)
1847 rc = server->ops->validate_negotiate(xid, tcon);
1850 free_rsp_buf(resp_buftype, rsp);
1855 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1856 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1862 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1864 struct smb_rqst rqst;
1865 struct smb2_tree_disconnect_req *req; /* response is trivial */
1867 struct cifs_ses *ses = tcon->ses;
1869 unsigned int total_len;
1871 struct kvec rsp_iov;
1874 cifs_dbg(FYI, "Tree Disconnect\n");
1876 if (!ses || !(ses->server))
1879 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1882 close_shroot_lease(&tcon->crfid);
1884 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1889 if (smb3_encryption_required(tcon))
1890 flags |= CIFS_TRANSFORM_REQ;
1892 flags |= CIFS_NO_RSP_BUF;
1894 iov[0].iov_base = (char *)req;
1895 iov[0].iov_len = total_len;
1897 memset(&rqst, 0, sizeof(struct smb_rqst));
1901 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1902 cifs_small_buf_release(req);
1904 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1910 static struct create_durable *
1911 create_durable_buf(void)
1913 struct create_durable *buf;
1915 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1919 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1920 (struct create_durable, Data));
1921 buf->ccontext.DataLength = cpu_to_le32(16);
1922 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1923 (struct create_durable, Name));
1924 buf->ccontext.NameLength = cpu_to_le16(4);
1925 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1933 static struct create_durable *
1934 create_reconnect_durable_buf(struct cifs_fid *fid)
1936 struct create_durable *buf;
1938 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1942 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1943 (struct create_durable, Data));
1944 buf->ccontext.DataLength = cpu_to_le32(16);
1945 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1946 (struct create_durable, Name));
1947 buf->ccontext.NameLength = cpu_to_le16(4);
1948 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1949 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1950 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1959 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1961 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1963 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1964 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1965 buf->IndexNumber = pdisk_id->DiskFileId;
1969 parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
1970 struct create_posix_rsp *posix)
1973 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
1974 u8 *end = beg + le32_to_cpu(cc->DataLength);
1977 memset(posix, 0, sizeof(*posix));
1979 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
1980 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
1981 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
1984 sid_len = posix_info_sid_size(sid, end);
1986 cifs_dbg(VFS, "bad owner sid in posix create response\n");
1989 memcpy(&posix->owner, sid, sid_len);
1991 sid = sid + sid_len;
1992 sid_len = posix_info_sid_size(sid, end);
1994 cifs_dbg(VFS, "bad group sid in posix create response\n");
1997 memcpy(&posix->group, sid, sid_len);
1999 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
2000 posix->nlink, posix->mode, posix->reparse_tag);
2004 smb2_parse_contexts(struct TCP_Server_Info *server,
2005 struct smb2_create_rsp *rsp,
2006 unsigned int *epoch, char *lease_key, __u8 *oplock,
2007 struct smb2_file_all_info *buf,
2008 struct create_posix_rsp *posix)
2011 struct create_context *cc;
2013 unsigned int remaining;
2015 const char smb3_create_tag_posix[] = {0x93, 0xAD, 0x25, 0x50, 0x9C,
2016 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
2017 0xDE, 0x96, 0x8B, 0xCD, 0x7C};
2020 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
2021 remaining = le32_to_cpu(rsp->CreateContextsLength);
2022 cc = (struct create_context *)data_offset;
2024 /* Initialize inode number to 0 in case no valid data in qfid context */
2026 buf->IndexNumber = 0;
2028 while (remaining >= sizeof(struct create_context)) {
2029 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
2030 if (le16_to_cpu(cc->NameLength) == 4 &&
2031 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
2032 *oplock = server->ops->parse_lease_buf(cc, epoch,
2034 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
2035 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
2036 parse_query_id_ctxt(cc, buf);
2037 else if ((le16_to_cpu(cc->NameLength) == 16)) {
2039 memcmp(name, smb3_create_tag_posix, 16) == 0)
2040 parse_posix_ctxt(cc, buf, posix);
2043 cifs_dbg(FYI, "Context not matched with len %d\n",
2044 le16_to_cpu(cc->NameLength));
2045 cifs_dump_mem("Cctxt name: ", name, 4);
2048 next = le32_to_cpu(cc->Next);
2052 cc = (struct create_context *)((char *)cc + next);
2055 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2056 *oplock = rsp->OplockLevel;
2062 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
2063 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
2065 struct smb2_create_req *req = iov[0].iov_base;
2066 unsigned int num = *num_iovec;
2068 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
2069 if (iov[num].iov_base == NULL)
2071 iov[num].iov_len = server->vals->create_lease_size;
2072 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2073 if (!req->CreateContextsOffset)
2074 req->CreateContextsOffset = cpu_to_le32(
2075 sizeof(struct smb2_create_req) +
2076 iov[num - 1].iov_len);
2077 le32_add_cpu(&req->CreateContextsLength,
2078 server->vals->create_lease_size);
2079 *num_iovec = num + 1;
2083 static struct create_durable_v2 *
2084 create_durable_v2_buf(struct cifs_open_parms *oparms)
2086 struct cifs_fid *pfid = oparms->fid;
2087 struct create_durable_v2 *buf;
2089 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2093 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2094 (struct create_durable_v2, dcontext));
2095 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2096 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2097 (struct create_durable_v2, Name));
2098 buf->ccontext.NameLength = cpu_to_le16(4);
2101 * NB: Handle timeout defaults to 0, which allows server to choose
2102 * (most servers default to 120 seconds) and most clients default to 0.
2103 * This can be overridden at mount ("handletimeout=") if the user wants
2104 * a different persistent (or resilient) handle timeout for all opens
2105 * opens on a particular SMB3 mount.
2107 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2108 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2109 generate_random_uuid(buf->dcontext.CreateGuid);
2110 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2112 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2120 static struct create_durable_handle_reconnect_v2 *
2121 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2123 struct create_durable_handle_reconnect_v2 *buf;
2125 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2130 buf->ccontext.DataOffset =
2131 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2133 buf->ccontext.DataLength =
2134 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2135 buf->ccontext.NameOffset =
2136 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2138 buf->ccontext.NameLength = cpu_to_le16(4);
2140 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2141 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2142 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2143 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2145 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2154 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2155 struct cifs_open_parms *oparms)
2157 struct smb2_create_req *req = iov[0].iov_base;
2158 unsigned int num = *num_iovec;
2160 iov[num].iov_base = create_durable_v2_buf(oparms);
2161 if (iov[num].iov_base == NULL)
2163 iov[num].iov_len = sizeof(struct create_durable_v2);
2164 if (!req->CreateContextsOffset)
2165 req->CreateContextsOffset =
2166 cpu_to_le32(sizeof(struct smb2_create_req) +
2168 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
2169 *num_iovec = num + 1;
2174 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2175 struct cifs_open_parms *oparms)
2177 struct smb2_create_req *req = iov[0].iov_base;
2178 unsigned int num = *num_iovec;
2180 /* indicate that we don't need to relock the file */
2181 oparms->reconnect = false;
2183 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2184 if (iov[num].iov_base == NULL)
2186 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2187 if (!req->CreateContextsOffset)
2188 req->CreateContextsOffset =
2189 cpu_to_le32(sizeof(struct smb2_create_req) +
2191 le32_add_cpu(&req->CreateContextsLength,
2192 sizeof(struct create_durable_handle_reconnect_v2));
2193 *num_iovec = num + 1;
2198 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2199 struct cifs_open_parms *oparms, bool use_persistent)
2201 struct smb2_create_req *req = iov[0].iov_base;
2202 unsigned int num = *num_iovec;
2204 if (use_persistent) {
2205 if (oparms->reconnect)
2206 return add_durable_reconnect_v2_context(iov, num_iovec,
2209 return add_durable_v2_context(iov, num_iovec, oparms);
2212 if (oparms->reconnect) {
2213 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2214 /* indicate that we don't need to relock the file */
2215 oparms->reconnect = false;
2217 iov[num].iov_base = create_durable_buf();
2218 if (iov[num].iov_base == NULL)
2220 iov[num].iov_len = sizeof(struct create_durable);
2221 if (!req->CreateContextsOffset)
2222 req->CreateContextsOffset =
2223 cpu_to_le32(sizeof(struct smb2_create_req) +
2225 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
2226 *num_iovec = num + 1;
2230 /* See MS-SMB2 2.2.13.2.7 */
2231 static struct crt_twarp_ctxt *
2232 create_twarp_buf(__u64 timewarp)
2234 struct crt_twarp_ctxt *buf;
2236 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2240 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2241 (struct crt_twarp_ctxt, Timestamp));
2242 buf->ccontext.DataLength = cpu_to_le32(8);
2243 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2244 (struct crt_twarp_ctxt, Name));
2245 buf->ccontext.NameLength = cpu_to_le16(4);
2246 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2251 buf->Timestamp = cpu_to_le64(timewarp);
2255 /* See MS-SMB2 2.2.13.2.7 */
2257 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2259 struct smb2_create_req *req = iov[0].iov_base;
2260 unsigned int num = *num_iovec;
2262 iov[num].iov_base = create_twarp_buf(timewarp);
2263 if (iov[num].iov_base == NULL)
2265 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2266 if (!req->CreateContextsOffset)
2267 req->CreateContextsOffset = cpu_to_le32(
2268 sizeof(struct smb2_create_req) +
2269 iov[num - 1].iov_len);
2270 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2271 *num_iovec = num + 1;
2275 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2276 static struct crt_sd_ctxt *
2277 create_sd_buf(umode_t mode, unsigned int *len)
2279 struct crt_sd_ctxt *buf;
2280 struct cifs_ace *pace;
2281 unsigned int sdlen, acelen;
2283 *len = roundup(sizeof(struct crt_sd_ctxt) + sizeof(struct cifs_ace) * 2,
2285 buf = kzalloc(*len, GFP_KERNEL);
2289 sdlen = sizeof(struct smb3_sd) + sizeof(struct smb3_acl) +
2290 2 * sizeof(struct cifs_ace);
2292 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2293 (struct crt_sd_ctxt, sd));
2294 buf->ccontext.DataLength = cpu_to_le32(sdlen);
2295 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2296 (struct crt_sd_ctxt, Name));
2297 buf->ccontext.NameLength = cpu_to_le16(4);
2298 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2303 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2305 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2306 * and "DP" ie the DACL is present
2308 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2310 /* offset owner, group and Sbz1 and SACL are all zero */
2311 buf->sd.OffsetDacl = cpu_to_le32(sizeof(struct smb3_sd));
2312 buf->acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2314 /* create one ACE to hold the mode embedded in reserved special SID */
2315 pace = (struct cifs_ace *)(sizeof(struct crt_sd_ctxt) + (char *)buf);
2316 acelen = setup_special_mode_ACE(pace, (__u64)mode);
2317 /* and one more ACE to allow access for authenticated users */
2318 pace = (struct cifs_ace *)(acelen + (sizeof(struct crt_sd_ctxt) +
2320 acelen += setup_authusers_ACE(pace);
2321 buf->acl.AclSize = cpu_to_le16(sizeof(struct cifs_acl) + acelen);
2322 buf->acl.AceCount = cpu_to_le16(2);
2327 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
2329 struct smb2_create_req *req = iov[0].iov_base;
2330 unsigned int num = *num_iovec;
2331 unsigned int len = 0;
2333 iov[num].iov_base = create_sd_buf(mode, &len);
2334 if (iov[num].iov_base == NULL)
2336 iov[num].iov_len = len;
2337 if (!req->CreateContextsOffset)
2338 req->CreateContextsOffset = cpu_to_le32(
2339 sizeof(struct smb2_create_req) +
2340 iov[num - 1].iov_len);
2341 le32_add_cpu(&req->CreateContextsLength, len);
2342 *num_iovec = num + 1;
2346 static struct crt_query_id_ctxt *
2347 create_query_id_buf(void)
2349 struct crt_query_id_ctxt *buf;
2351 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2355 buf->ccontext.DataOffset = cpu_to_le16(0);
2356 buf->ccontext.DataLength = cpu_to_le32(0);
2357 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2358 (struct crt_query_id_ctxt, Name));
2359 buf->ccontext.NameLength = cpu_to_le16(4);
2360 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2368 /* See MS-SMB2 2.2.13.2.9 */
2370 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2372 struct smb2_create_req *req = iov[0].iov_base;
2373 unsigned int num = *num_iovec;
2375 iov[num].iov_base = create_query_id_buf();
2376 if (iov[num].iov_base == NULL)
2378 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2379 if (!req->CreateContextsOffset)
2380 req->CreateContextsOffset = cpu_to_le32(
2381 sizeof(struct smb2_create_req) +
2382 iov[num - 1].iov_len);
2383 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2384 *num_iovec = num + 1;
2389 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2390 const char *treename, const __le16 *path)
2392 int treename_len, path_len;
2393 struct nls_table *cp;
2394 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2399 treename_len = strlen(treename);
2400 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2406 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2409 * make room for one path separator between the treename and
2412 *out_len = treename_len + 1 + path_len;
2415 * final path needs to be null-terminated UTF16 with a
2419 *out_size = roundup((*out_len+1)*2, 8);
2420 *out_path = kzalloc(*out_size, GFP_KERNEL);
2424 cp = load_nls_default();
2425 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2426 UniStrcat(*out_path, sep);
2427 UniStrcat(*out_path, path);
2433 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2434 umode_t mode, struct cifs_tcon *tcon,
2435 const char *full_path,
2436 struct cifs_sb_info *cifs_sb)
2438 struct smb_rqst rqst;
2439 struct smb2_create_req *req;
2440 struct smb2_create_rsp *rsp = NULL;
2441 struct cifs_ses *ses = tcon->ses;
2442 struct kvec iov[3]; /* make sure at least one for each open context */
2443 struct kvec rsp_iov = {NULL, 0};
2446 __le16 *copy_path = NULL;
2449 unsigned int n_iov = 2;
2450 __u32 file_attributes = 0;
2451 char *pc_buf = NULL;
2453 unsigned int total_len;
2454 __le16 *utf16_path = NULL;
2456 cifs_dbg(FYI, "mkdir\n");
2458 /* resource #1: path allocation */
2459 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2463 if (!ses || !(ses->server)) {
2468 /* resource #2: request */
2469 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2474 if (smb3_encryption_required(tcon))
2475 flags |= CIFS_TRANSFORM_REQ;
2477 req->ImpersonationLevel = IL_IMPERSONATION;
2478 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2479 /* File attributes ignored on open (used in create though) */
2480 req->FileAttributes = cpu_to_le32(file_attributes);
2481 req->ShareAccess = FILE_SHARE_ALL_LE;
2482 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2483 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2485 iov[0].iov_base = (char *)req;
2486 /* -1 since last byte is buf[0] which is sent below (path) */
2487 iov[0].iov_len = total_len - 1;
2489 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2491 /* [MS-SMB2] 2.2.13 NameOffset:
2492 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2493 * the SMB2 header, the file name includes a prefix that will
2494 * be processed during DFS name normalization as specified in
2495 * section 3.3.5.9. Otherwise, the file name is relative to
2496 * the share that is identified by the TreeId in the SMB2
2499 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2502 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2503 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2505 tcon->treeName, utf16_path);
2509 req->NameLength = cpu_to_le16(name_len * 2);
2510 uni_path_len = copy_size;
2511 /* free before overwriting resource */
2513 utf16_path = copy_path;
2515 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2516 /* MUST set path len (NameLength) to 0 opening root of share */
2517 req->NameLength = cpu_to_le16(uni_path_len - 2);
2518 if (uni_path_len % 8 != 0) {
2519 copy_size = roundup(uni_path_len, 8);
2520 copy_path = kzalloc(copy_size, GFP_KERNEL);
2525 memcpy((char *)copy_path, (const char *)utf16_path,
2527 uni_path_len = copy_size;
2528 /* free before overwriting resource */
2530 utf16_path = copy_path;
2534 iov[1].iov_len = uni_path_len;
2535 iov[1].iov_base = utf16_path;
2536 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2538 if (tcon->posix_extensions) {
2539 /* resource #3: posix buf */
2540 rc = add_posix_context(iov, &n_iov, mode);
2543 pc_buf = iov[n_iov-1].iov_base;
2547 memset(&rqst, 0, sizeof(struct smb_rqst));
2549 rqst.rq_nvec = n_iov;
2551 /* no need to inc num_remote_opens because we close it just below */
2552 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2553 FILE_WRITE_ATTRIBUTES);
2554 /* resource #4: response buffer */
2555 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2557 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2558 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2560 FILE_WRITE_ATTRIBUTES, rc);
2561 goto err_free_rsp_buf;
2564 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2565 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2566 ses->Suid, CREATE_NOT_FILE,
2567 FILE_WRITE_ATTRIBUTES);
2569 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2571 /* Eventually save off posix specific response info and timestaps */
2574 free_rsp_buf(resp_buftype, rsp);
2577 cifs_small_buf_release(req);
2584 SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2585 struct cifs_open_parms *oparms, __le16 *path)
2587 struct TCP_Server_Info *server = tcon->ses->server;
2588 struct smb2_create_req *req;
2589 unsigned int n_iov = 2;
2590 __u32 file_attributes = 0;
2593 unsigned int total_len;
2594 struct kvec *iov = rqst->rq_iov;
2598 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2602 iov[0].iov_base = (char *)req;
2603 /* -1 since last byte is buf[0] which is sent below (path) */
2604 iov[0].iov_len = total_len - 1;
2606 if (oparms->create_options & CREATE_OPTION_READONLY)
2607 file_attributes |= ATTR_READONLY;
2608 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2609 file_attributes |= ATTR_SYSTEM;
2611 req->ImpersonationLevel = IL_IMPERSONATION;
2612 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2613 /* File attributes ignored on open (used in create though) */
2614 req->FileAttributes = cpu_to_le32(file_attributes);
2615 req->ShareAccess = FILE_SHARE_ALL_LE;
2617 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2618 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2619 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2621 /* [MS-SMB2] 2.2.13 NameOffset:
2622 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2623 * the SMB2 header, the file name includes a prefix that will
2624 * be processed during DFS name normalization as specified in
2625 * section 3.3.5.9. Otherwise, the file name is relative to
2626 * the share that is identified by the TreeId in the SMB2
2629 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2632 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2633 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2635 tcon->treeName, path);
2638 req->NameLength = cpu_to_le16(name_len * 2);
2639 uni_path_len = copy_size;
2642 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2643 /* MUST set path len (NameLength) to 0 opening root of share */
2644 req->NameLength = cpu_to_le16(uni_path_len - 2);
2645 copy_size = uni_path_len;
2646 if (copy_size % 8 != 0)
2647 copy_size = roundup(copy_size, 8);
2648 copy_path = kzalloc(copy_size, GFP_KERNEL);
2651 memcpy((char *)copy_path, (const char *)path,
2653 uni_path_len = copy_size;
2657 iov[1].iov_len = uni_path_len;
2658 iov[1].iov_base = path;
2660 if ((!server->oplocks) || (tcon->no_lease))
2661 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2663 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2664 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2665 req->RequestedOplockLevel = *oplock;
2666 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2667 (oparms->create_options & CREATE_NOT_FILE))
2668 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2670 rc = add_lease_context(server, iov, &n_iov,
2671 oparms->fid->lease_key, oplock);
2676 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2677 /* need to set Next field of lease context if we request it */
2678 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2679 struct create_context *ccontext =
2680 (struct create_context *)iov[n_iov-1].iov_base;
2682 cpu_to_le32(server->vals->create_lease_size);
2685 rc = add_durable_context(iov, &n_iov, oparms,
2686 tcon->use_persistent);
2691 if (tcon->posix_extensions) {
2693 struct create_context *ccontext =
2694 (struct create_context *)iov[n_iov-1].iov_base;
2696 cpu_to_le32(iov[n_iov-1].iov_len);
2699 rc = add_posix_context(iov, &n_iov, oparms->mode);
2704 if (tcon->snapshot_time) {
2705 cifs_dbg(FYI, "adding snapshot context\n");
2707 struct create_context *ccontext =
2708 (struct create_context *)iov[n_iov-1].iov_base;
2710 cpu_to_le32(iov[n_iov-1].iov_len);
2713 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2718 if ((oparms->disposition != FILE_OPEN) &&
2719 (oparms->cifs_sb) &&
2720 (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
2721 (oparms->mode != ACL_NO_MODE)) {
2723 struct create_context *ccontext =
2724 (struct create_context *)iov[n_iov-1].iov_base;
2726 cpu_to_le32(iov[n_iov-1].iov_len);
2729 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2730 rc = add_sd_context(iov, &n_iov, oparms->mode);
2736 struct create_context *ccontext =
2737 (struct create_context *)iov[n_iov-1].iov_base;
2738 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2740 add_query_id_context(iov, &n_iov);
2742 rqst->rq_nvec = n_iov;
2746 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2747 * All other vectors are freed by kfree().
2750 SMB2_open_free(struct smb_rqst *rqst)
2754 if (rqst && rqst->rq_iov) {
2755 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2756 for (i = 1; i < rqst->rq_nvec; i++)
2757 if (rqst->rq_iov[i].iov_base != smb2_padding)
2758 kfree(rqst->rq_iov[i].iov_base);
2763 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2764 __u8 *oplock, struct smb2_file_all_info *buf,
2765 struct create_posix_rsp *posix,
2766 struct kvec *err_iov, int *buftype)
2768 struct smb_rqst rqst;
2769 struct smb2_create_rsp *rsp = NULL;
2770 struct TCP_Server_Info *server;
2771 struct cifs_tcon *tcon = oparms->tcon;
2772 struct cifs_ses *ses = tcon->ses;
2773 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2774 struct kvec rsp_iov = {NULL, 0};
2775 int resp_buftype = CIFS_NO_BUFFER;
2779 cifs_dbg(FYI, "create/open\n");
2780 if (ses && (ses->server))
2781 server = ses->server;
2785 if (smb3_encryption_required(tcon))
2786 flags |= CIFS_TRANSFORM_REQ;
2788 memset(&rqst, 0, sizeof(struct smb_rqst));
2789 memset(&iov, 0, sizeof(iov));
2791 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2793 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2797 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2798 oparms->create_options, oparms->desired_access);
2800 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2802 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2805 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2806 if (err_iov && rsp) {
2808 *buftype = resp_buftype;
2809 resp_buftype = CIFS_NO_BUFFER;
2812 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2813 oparms->create_options, oparms->desired_access, rc);
2814 if (rc == -EREMCHG) {
2815 printk_once(KERN_WARNING "server share %s deleted\n",
2817 tcon->need_reconnect = true;
2821 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2822 ses->Suid, oparms->create_options,
2823 oparms->desired_access);
2825 atomic_inc(&tcon->num_remote_opens);
2826 oparms->fid->persistent_fid = rsp->PersistentFileId;
2827 oparms->fid->volatile_fid = rsp->VolatileFileId;
2828 oparms->fid->access = oparms->desired_access;
2829 #ifdef CONFIG_CIFS_DEBUG2
2830 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2831 #endif /* CIFS_DEBUG2 */
2834 memcpy(buf, &rsp->CreationTime, 32);
2835 buf->AllocationSize = rsp->AllocationSize;
2836 buf->EndOfFile = rsp->EndofFile;
2837 buf->Attributes = rsp->FileAttributes;
2838 buf->NumberOfLinks = cpu_to_le32(1);
2839 buf->DeletePending = 0;
2843 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2844 oparms->fid->lease_key, oplock, buf, posix);
2846 SMB2_open_free(&rqst);
2847 free_rsp_buf(resp_buftype, rsp);
2852 SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2853 u64 persistent_fid, u64 volatile_fid, u32 opcode,
2854 bool is_fsctl, char *in_data, u32 indatalen,
2855 __u32 max_response_size)
2857 struct smb2_ioctl_req *req;
2858 struct kvec *iov = rqst->rq_iov;
2859 unsigned int total_len;
2863 rc = smb2_ioctl_req_init(opcode, tcon, (void **) &req, &total_len);
2869 * indatalen is usually small at a couple of bytes max, so
2870 * just allocate through generic pool
2872 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
2874 cifs_small_buf_release(req);
2879 req->CtlCode = cpu_to_le32(opcode);
2880 req->PersistentFileId = persistent_fid;
2881 req->VolatileFileId = volatile_fid;
2883 iov[0].iov_base = (char *)req;
2885 * If no input data, the size of ioctl struct in
2886 * protocol spec still includes a 1 byte data buffer,
2887 * but if input data passed to ioctl, we do not
2888 * want to double count this, so we do not send
2889 * the dummy one byte of data in iovec[0] if sending
2890 * input data (in iovec[1]).
2893 req->InputCount = cpu_to_le32(indatalen);
2894 /* do not set InputOffset if no input data */
2896 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
2898 iov[0].iov_len = total_len - 1;
2899 iov[1].iov_base = in_data_buf;
2900 iov[1].iov_len = indatalen;
2903 iov[0].iov_len = total_len;
2906 req->OutputOffset = 0;
2907 req->OutputCount = 0; /* MBZ */
2910 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2911 * We Could increase default MaxOutputResponse, but that could require
2912 * more credits. Windows typically sets this smaller, but for some
2913 * ioctls it may be useful to allow server to send more. No point
2914 * limiting what the server can send as long as fits in one credit
2915 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2916 * to increase this limit up in the future.
2917 * Note that for snapshot queries that servers like Azure expect that
2918 * the first query be minimal size (and just used to get the number/size
2919 * of previous versions) so response size must be specified as EXACTLY
2920 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2921 * of eight bytes. Currently that is the only case where we set max
2922 * response size smaller.
2924 req->MaxOutputResponse = cpu_to_le32(max_response_size);
2927 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2931 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2932 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
2933 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
2939 SMB2_ioctl_free(struct smb_rqst *rqst)
2942 if (rqst && rqst->rq_iov) {
2943 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2944 for (i = 1; i < rqst->rq_nvec; i++)
2945 if (rqst->rq_iov[i].iov_base != smb2_padding)
2946 kfree(rqst->rq_iov[i].iov_base);
2952 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2955 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2956 u64 volatile_fid, u32 opcode, bool is_fsctl,
2957 char *in_data, u32 indatalen, u32 max_out_data_len,
2958 char **out_data, u32 *plen /* returned data len */)
2960 struct smb_rqst rqst;
2961 struct smb2_ioctl_rsp *rsp = NULL;
2962 struct cifs_ses *ses;
2963 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
2964 struct kvec rsp_iov = {NULL, 0};
2965 int resp_buftype = CIFS_NO_BUFFER;
2968 struct TCP_Server_Info *server;
2970 cifs_dbg(FYI, "SMB2 IOCTL\n");
2972 if (out_data != NULL)
2975 /* zero out returned data len, in case of error */
2986 server = ses->server;
2990 if (smb3_encryption_required(tcon))
2991 flags |= CIFS_TRANSFORM_REQ;
2993 memset(&rqst, 0, sizeof(struct smb_rqst));
2994 memset(&iov, 0, sizeof(iov));
2996 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
2998 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
2999 is_fsctl, in_data, indatalen, max_out_data_len);
3003 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
3005 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
3008 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3009 ses->Suid, 0, opcode, rc);
3011 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
3012 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3014 } else if (rc == -EINVAL) {
3015 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3016 (opcode != FSCTL_SRV_COPYCHUNK)) {
3017 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3020 } else if (rc == -E2BIG) {
3021 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3022 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3027 /* check if caller wants to look at return data or just return rc */
3028 if ((plen == NULL) || (out_data == NULL))
3031 *plen = le32_to_cpu(rsp->OutputCount);
3033 /* We check for obvious errors in the output buffer length and offset */
3035 goto ioctl_exit; /* server returned no data */
3036 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
3037 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
3043 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
3044 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
3045 le32_to_cpu(rsp->OutputOffset));
3051 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3053 if (*out_data == NULL) {
3059 SMB2_ioctl_free(&rqst);
3060 free_rsp_buf(resp_buftype, rsp);
3065 * Individual callers to ioctl worker function follow
3069 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3070 u64 persistent_fid, u64 volatile_fid)
3073 struct compress_ioctl fsctl_input;
3074 char *ret_data = NULL;
3076 fsctl_input.CompressionState =
3077 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3079 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3080 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
3081 (char *)&fsctl_input /* data input */,
3082 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3083 &ret_data /* out data */, NULL);
3085 cifs_dbg(FYI, "set compression rc %d\n", rc);
3091 SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3092 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
3094 struct smb2_close_req *req;
3095 struct kvec *iov = rqst->rq_iov;
3096 unsigned int total_len;
3099 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
3103 req->PersistentFileId = persistent_fid;
3104 req->VolatileFileId = volatile_fid;
3106 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3109 iov[0].iov_base = (char *)req;
3110 iov[0].iov_len = total_len;
3116 SMB2_close_free(struct smb_rqst *rqst)
3118 if (rqst && rqst->rq_iov)
3119 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3123 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3124 u64 persistent_fid, u64 volatile_fid,
3125 struct smb2_file_network_open_info *pbuf)
3127 struct smb_rqst rqst;
3128 struct smb2_close_rsp *rsp = NULL;
3129 struct cifs_ses *ses = tcon->ses;
3131 struct kvec rsp_iov;
3132 int resp_buftype = CIFS_NO_BUFFER;
3135 bool query_attrs = false;
3137 cifs_dbg(FYI, "Close\n");
3139 if (!ses || !(ses->server))
3142 if (smb3_encryption_required(tcon))
3143 flags |= CIFS_TRANSFORM_REQ;
3145 memset(&rqst, 0, sizeof(struct smb_rqst));
3146 memset(&iov, 0, sizeof(iov));
3150 /* check if need to ask server to return timestamps in close response */
3154 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3155 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid,
3160 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3161 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
3164 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
3165 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3169 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3172 * Note that have to subtract 4 since struct network_open_info
3173 * has a final 4 byte pad that close response does not have
3176 memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3179 atomic_dec(&tcon->num_remote_opens);
3181 SMB2_close_free(&rqst);
3182 free_rsp_buf(resp_buftype, rsp);
3184 /* retry close in a worker thread if this one is interrupted */
3188 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3191 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3192 persistent_fid, tmp_rc);
3198 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3199 u64 persistent_fid, u64 volatile_fid)
3201 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3205 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3206 struct kvec *iov, unsigned int min_buf_size)
3208 unsigned int smb_len = iov->iov_len;
3209 char *end_of_smb = smb_len + (char *)iov->iov_base;
3210 char *begin_of_buf = offset + (char *)iov->iov_base;
3211 char *end_of_buf = begin_of_buf + buffer_length;
3214 if (buffer_length < min_buf_size) {
3215 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3216 buffer_length, min_buf_size);
3220 /* check if beyond RFC1001 maximum length */
3221 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3222 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3223 buffer_length, smb_len);
3227 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3228 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
3236 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3237 * Caller must free buffer.
3240 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3241 struct kvec *iov, unsigned int minbufsize,
3244 char *begin_of_buf = offset + (char *)iov->iov_base;
3250 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3254 memcpy(data, begin_of_buf, buffer_length);
3260 SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3261 u64 persistent_fid, u64 volatile_fid,
3262 u8 info_class, u8 info_type, u32 additional_info,
3263 size_t output_len, size_t input_len, void *input)
3265 struct smb2_query_info_req *req;
3266 struct kvec *iov = rqst->rq_iov;
3267 unsigned int total_len;
3270 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3275 req->InfoType = info_type;
3276 req->FileInfoClass = info_class;
3277 req->PersistentFileId = persistent_fid;
3278 req->VolatileFileId = volatile_fid;
3279 req->AdditionalInformation = cpu_to_le32(additional_info);
3281 req->OutputBufferLength = cpu_to_le32(output_len);
3283 req->InputBufferLength = cpu_to_le32(input_len);
3284 /* total_len for smb query request never close to le16 max */
3285 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3286 memcpy(req->Buffer, input, input_len);
3289 iov[0].iov_base = (char *)req;
3291 iov[0].iov_len = total_len - 1 + input_len;
3296 SMB2_query_info_free(struct smb_rqst *rqst)
3298 if (rqst && rqst->rq_iov)
3299 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3303 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3304 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3305 u32 additional_info, size_t output_len, size_t min_len, void **data,
3308 struct smb_rqst rqst;
3309 struct smb2_query_info_rsp *rsp = NULL;
3311 struct kvec rsp_iov;
3313 int resp_buftype = CIFS_NO_BUFFER;
3314 struct cifs_ses *ses = tcon->ses;
3315 struct TCP_Server_Info *server;
3317 bool allocated = false;
3319 cifs_dbg(FYI, "Query Info\n");
3323 server = ses->server;
3327 if (smb3_encryption_required(tcon))
3328 flags |= CIFS_TRANSFORM_REQ;
3330 memset(&rqst, 0, sizeof(struct smb_rqst));
3331 memset(&iov, 0, sizeof(iov));
3335 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
3336 info_class, info_type, additional_info,
3337 output_len, 0, NULL);
3341 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3342 ses->Suid, info_class, (__u32)info_type);
3344 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3345 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3348 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3349 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3350 ses->Suid, info_class, (__u32)info_type, rc);
3354 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3355 ses->Suid, info_class, (__u32)info_type);
3358 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3360 *data = kmalloc(*dlen, GFP_KERNEL);
3363 "Error %d allocating memory for acl\n",
3373 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3374 le32_to_cpu(rsp->OutputBufferLength),
3375 &rsp_iov, min_len, *data);
3376 if (rc && allocated) {
3383 SMB2_query_info_free(&rqst);
3384 free_rsp_buf(resp_buftype, rsp);
3388 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3389 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3391 return query_info(xid, tcon, persistent_fid, volatile_fid,
3392 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3393 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3394 sizeof(struct smb2_file_all_info), (void **)&data,
3399 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3400 u64 persistent_fid, u64 volatile_fid,
3401 void **data, u32 *plen)
3403 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3406 return query_info(xid, tcon, persistent_fid, volatile_fid,
3407 0, SMB2_O_INFO_SECURITY, additional_info,
3408 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3412 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3413 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3415 return query_info(xid, tcon, persistent_fid, volatile_fid,
3416 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3417 sizeof(struct smb2_file_internal_info),
3418 sizeof(struct smb2_file_internal_info),
3419 (void **)&uniqueid, NULL);
3423 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3424 * See MS-SMB2 2.2.35 and 2.2.36
3428 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3429 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid,
3430 u32 completion_filter, bool watch_tree)
3432 struct smb2_change_notify_req *req;
3433 struct kvec *iov = rqst->rq_iov;
3434 unsigned int total_len;
3437 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, (void **) &req, &total_len);
3441 req->PersistentFileId = persistent_fid;
3442 req->VolatileFileId = volatile_fid;
3443 /* See note 354 of MS-SMB2, 64K max */
3444 req->OutputBufferLength =
3445 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3446 req->CompletionFilter = cpu_to_le32(completion_filter);
3448 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3452 iov[0].iov_base = (char *)req;
3453 iov[0].iov_len = total_len;
3459 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3460 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3461 u32 completion_filter)
3463 struct cifs_ses *ses = tcon->ses;
3464 struct smb_rqst rqst;
3466 struct kvec rsp_iov = {NULL, 0};
3467 int resp_buftype = CIFS_NO_BUFFER;
3471 cifs_dbg(FYI, "change notify\n");
3472 if (!ses || !(ses->server))
3475 if (smb3_encryption_required(tcon))
3476 flags |= CIFS_TRANSFORM_REQ;
3478 memset(&rqst, 0, sizeof(struct smb_rqst));
3479 memset(&iov, 0, sizeof(iov));
3483 rc = SMB2_notify_init(xid, &rqst, tcon, persistent_fid, volatile_fid,
3484 completion_filter, watch_tree);
3488 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3489 (u8)watch_tree, completion_filter);
3490 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3493 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3494 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3495 (u8)watch_tree, completion_filter, rc);
3497 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3498 ses->Suid, (u8)watch_tree, completion_filter);
3502 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3503 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3510 * This is a no-op for now. We're not really interested in the reply, but
3511 * rather in the fact that the server sent one and that server->lstrp
3514 * FIXME: maybe we should consider checking that the reply matches request?
3517 smb2_echo_callback(struct mid_q_entry *mid)
3519 struct TCP_Server_Info *server = mid->callback_data;
3520 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3521 struct cifs_credits credits = { .value = 0, .instance = 0 };
3523 if (mid->mid_state == MID_RESPONSE_RECEIVED
3524 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3525 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3526 credits.instance = server->reconnect_instance;
3529 DeleteMidQEntry(mid);
3530 add_credits(server, &credits, CIFS_ECHO_OP);
3533 void smb2_reconnect_server(struct work_struct *work)
3535 struct TCP_Server_Info *server = container_of(work,
3536 struct TCP_Server_Info, reconnect.work);
3537 struct cifs_ses *ses;
3538 struct cifs_tcon *tcon, *tcon2;
3539 struct list_head tmp_list;
3540 int tcon_exist = false;
3542 int resched = false;
3545 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3546 mutex_lock(&server->reconnect_mutex);
3548 INIT_LIST_HEAD(&tmp_list);
3549 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3551 spin_lock(&cifs_tcp_ses_lock);
3552 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3553 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3554 if (tcon->need_reconnect || tcon->need_reopen_files) {
3556 list_add_tail(&tcon->rlist, &tmp_list);
3561 * IPC has the same lifetime as its session and uses its
3564 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3565 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3571 * Get the reference to server struct to be sure that the last call of
3572 * cifs_put_tcon() in the loop below won't release the server pointer.
3575 server->srv_count++;
3577 spin_unlock(&cifs_tcp_ses_lock);
3579 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3580 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3582 cifs_reopen_persistent_handles(tcon);
3585 list_del_init(&tcon->rlist);
3587 cifs_put_smb_ses(tcon->ses);
3589 cifs_put_tcon(tcon);
3592 cifs_dbg(FYI, "Reconnecting tcons finished\n");
3594 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3595 mutex_unlock(&server->reconnect_mutex);
3597 /* now we can safely release srv struct */
3599 cifs_put_tcp_session(server, 1);
3603 SMB2_echo(struct TCP_Server_Info *server)
3605 struct smb2_echo_req *req;
3608 struct smb_rqst rqst = { .rq_iov = iov,
3610 unsigned int total_len;
3612 cifs_dbg(FYI, "In echo request\n");
3614 if (server->tcpStatus == CifsNeedNegotiate) {
3615 /* No need to send echo on newly established connections */
3616 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
3620 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
3624 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3626 iov[0].iov_len = total_len;
3627 iov[0].iov_base = (char *)req;
3629 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3630 server, CIFS_ECHO_OP, NULL);
3632 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3634 cifs_small_buf_release(req);
3639 SMB2_flush_free(struct smb_rqst *rqst)
3641 if (rqst && rqst->rq_iov)
3642 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3646 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3647 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid)
3649 struct smb2_flush_req *req;
3650 struct kvec *iov = rqst->rq_iov;
3651 unsigned int total_len;
3654 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
3658 req->PersistentFileId = persistent_fid;
3659 req->VolatileFileId = volatile_fid;
3661 iov[0].iov_base = (char *)req;
3662 iov[0].iov_len = total_len;
3668 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3671 struct cifs_ses *ses = tcon->ses;
3672 struct smb_rqst rqst;
3674 struct kvec rsp_iov = {NULL, 0};
3675 int resp_buftype = CIFS_NO_BUFFER;
3679 cifs_dbg(FYI, "flush\n");
3680 if (!ses || !(ses->server))
3683 if (smb3_encryption_required(tcon))
3684 flags |= CIFS_TRANSFORM_REQ;
3686 memset(&rqst, 0, sizeof(struct smb_rqst));
3687 memset(&iov, 0, sizeof(iov));
3691 rc = SMB2_flush_init(xid, &rqst, tcon, persistent_fid, volatile_fid);
3695 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3696 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3699 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
3700 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3703 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3707 SMB2_flush_free(&rqst);
3708 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3713 * To form a chain of read requests, any read requests after the first should
3714 * have the end_of_chain boolean set to true.
3717 smb2_new_read_req(void **buf, unsigned int *total_len,
3718 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3719 unsigned int remaining_bytes, int request_type)
3722 struct smb2_read_plain_req *req = NULL;
3723 struct smb2_sync_hdr *shdr;
3724 struct TCP_Server_Info *server;
3726 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3731 server = io_parms->tcon->ses->server;
3733 return -ECONNABORTED;
3735 shdr = &req->sync_hdr;
3736 shdr->ProcessId = cpu_to_le32(io_parms->pid);
3738 req->PersistentFileId = io_parms->persistent_fid;
3739 req->VolatileFileId = io_parms->volatile_fid;
3740 req->ReadChannelInfoOffset = 0; /* reserved */
3741 req->ReadChannelInfoLength = 0; /* reserved */
3742 req->Channel = 0; /* reserved */
3743 req->MinimumCount = 0;
3744 req->Length = cpu_to_le32(io_parms->length);
3745 req->Offset = cpu_to_le64(io_parms->offset);
3747 trace_smb3_read_enter(0 /* xid */,
3748 io_parms->persistent_fid,
3749 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3750 io_parms->offset, io_parms->length);
3751 #ifdef CONFIG_CIFS_SMB_DIRECT
3753 * If we want to do a RDMA write, fill in and append
3754 * smbd_buffer_descriptor_v1 to the end of read request
3756 if (server->rdma && rdata && !server->sign &&
3757 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
3759 struct smbd_buffer_descriptor_v1 *v1;
3760 bool need_invalidate =
3761 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3763 rdata->mr = smbd_register_mr(
3764 server->smbd_conn, rdata->pages,
3765 rdata->nr_pages, rdata->page_offset,
3766 rdata->tailsz, true, need_invalidate);
3770 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3771 if (need_invalidate)
3772 req->Channel = SMB2_CHANNEL_RDMA_V1;
3773 req->ReadChannelInfoOffset =
3774 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
3775 req->ReadChannelInfoLength =
3776 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3777 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3778 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3779 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3780 v1->length = cpu_to_le32(rdata->mr->mr->length);
3782 *total_len += sizeof(*v1) - 1;
3785 if (request_type & CHAINED_REQUEST) {
3786 if (!(request_type & END_OF_CHAIN)) {
3787 /* next 8-byte aligned request */
3788 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3789 shdr->NextCommand = cpu_to_le32(*total_len);
3790 } else /* END_OF_CHAIN */
3791 shdr->NextCommand = 0;
3792 if (request_type & RELATED_REQUEST) {
3793 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
3795 * Related requests use info from previous read request
3798 shdr->SessionId = 0xFFFFFFFF;
3799 shdr->TreeId = 0xFFFFFFFF;
3800 req->PersistentFileId = 0xFFFFFFFF;
3801 req->VolatileFileId = 0xFFFFFFFF;
3804 if (remaining_bytes > io_parms->length)
3805 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3807 req->RemainingBytes = 0;
3814 smb2_readv_callback(struct mid_q_entry *mid)
3816 struct cifs_readdata *rdata = mid->callback_data;
3817 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3818 struct TCP_Server_Info *server = tcon->ses->server;
3819 struct smb2_sync_hdr *shdr =
3820 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
3821 struct cifs_credits credits = { .value = 0, .instance = 0 };
3822 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
3824 .rq_pages = rdata->pages,
3825 .rq_offset = rdata->page_offset,
3826 .rq_npages = rdata->nr_pages,
3827 .rq_pagesz = rdata->pagesz,
3828 .rq_tailsz = rdata->tailsz };
3830 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3831 __func__, mid->mid, mid->mid_state, rdata->result,
3834 switch (mid->mid_state) {
3835 case MID_RESPONSE_RECEIVED:
3836 credits.value = le16_to_cpu(shdr->CreditRequest);
3837 credits.instance = server->reconnect_instance;
3838 /* result already set, check signature */
3839 if (server->sign && !mid->decrypted) {
3842 rc = smb2_verify_signature(&rqst, server);
3844 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
3847 /* FIXME: should this be counted toward the initiating task? */
3848 task_io_account_read(rdata->got_bytes);
3849 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3851 case MID_REQUEST_SUBMITTED:
3852 case MID_RETRY_NEEDED:
3853 rdata->result = -EAGAIN;
3854 if (server->sign && rdata->got_bytes)
3855 /* reset bytes number since we can not check a sign */
3856 rdata->got_bytes = 0;
3857 /* FIXME: should this be counted toward the initiating task? */
3858 task_io_account_read(rdata->got_bytes);
3859 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3861 case MID_RESPONSE_MALFORMED:
3862 credits.value = le16_to_cpu(shdr->CreditRequest);
3863 credits.instance = server->reconnect_instance;
3866 rdata->result = -EIO;
3868 #ifdef CONFIG_CIFS_SMB_DIRECT
3870 * If this rdata has a memmory registered, the MR can be freed
3871 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3872 * because they have limited number and are used for future I/Os
3875 smbd_deregister_mr(rdata->mr);
3879 if (rdata->result && rdata->result != -ENODATA) {
3880 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
3881 trace_smb3_read_err(0 /* xid */,
3882 rdata->cfile->fid.persistent_fid,
3883 tcon->tid, tcon->ses->Suid, rdata->offset,
3884 rdata->bytes, rdata->result);
3886 trace_smb3_read_done(0 /* xid */,
3887 rdata->cfile->fid.persistent_fid,
3888 tcon->tid, tcon->ses->Suid,
3889 rdata->offset, rdata->got_bytes);
3891 queue_work(cifsiod_wq, &rdata->work);
3892 DeleteMidQEntry(mid);
3893 add_credits(server, &credits, 0);
3896 /* smb2_async_readv - send an async read, and set up mid to handle result */
3898 smb2_async_readv(struct cifs_readdata *rdata)
3902 struct smb2_sync_hdr *shdr;
3903 struct cifs_io_parms io_parms;
3904 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3906 struct TCP_Server_Info *server;
3907 unsigned int total_len;
3909 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3910 __func__, rdata->offset, rdata->bytes);
3912 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3913 io_parms.offset = rdata->offset;
3914 io_parms.length = rdata->bytes;
3915 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3916 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3917 io_parms.pid = rdata->pid;
3919 server = io_parms.tcon->ses->server;
3921 rc = smb2_new_read_req(
3922 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
3926 if (smb3_encryption_required(io_parms.tcon))
3927 flags |= CIFS_TRANSFORM_REQ;
3929 rdata->iov[0].iov_base = buf;
3930 rdata->iov[0].iov_len = total_len;
3932 shdr = (struct smb2_sync_hdr *)buf;
3934 if (rdata->credits.value > 0) {
3935 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
3936 SMB2_MAX_BUFFER_SIZE));
3937 shdr->CreditRequest =
3938 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
3940 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3942 goto async_readv_out;
3944 flags |= CIFS_HAS_CREDITS;
3947 kref_get(&rdata->refcount);
3948 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
3949 cifs_readv_receive, smb2_readv_callback,
3950 smb3_handle_read_data, rdata, flags,
3953 kref_put(&rdata->refcount, cifs_readdata_release);
3954 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
3955 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3957 io_parms.tcon->ses->Suid,
3958 io_parms.offset, io_parms.length, rc);
3962 cifs_small_buf_release(buf);
3967 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3968 unsigned int *nbytes, char **buf, int *buf_type)
3970 struct smb_rqst rqst;
3971 int resp_buftype, rc;
3972 struct smb2_read_plain_req *req = NULL;
3973 struct smb2_read_rsp *rsp = NULL;
3975 struct kvec rsp_iov;
3976 unsigned int total_len;
3977 int flags = CIFS_LOG_ERROR;
3978 struct cifs_ses *ses = io_parms->tcon->ses;
3981 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
3985 if (smb3_encryption_required(io_parms->tcon))
3986 flags |= CIFS_TRANSFORM_REQ;
3988 iov[0].iov_base = (char *)req;
3989 iov[0].iov_len = total_len;
3991 memset(&rqst, 0, sizeof(struct smb_rqst));
3995 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3996 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
3999 if (rc != -ENODATA) {
4000 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4001 cifs_dbg(VFS, "Send error in read = %d\n", rc);
4002 trace_smb3_read_err(xid, req->PersistentFileId,
4003 io_parms->tcon->tid, ses->Suid,
4004 io_parms->offset, io_parms->length,
4007 trace_smb3_read_done(xid, req->PersistentFileId,
4008 io_parms->tcon->tid, ses->Suid,
4009 io_parms->offset, 0);
4010 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4011 cifs_small_buf_release(req);
4012 return rc == -ENODATA ? 0 : rc;
4014 trace_smb3_read_done(xid, req->PersistentFileId,
4015 io_parms->tcon->tid, ses->Suid,
4016 io_parms->offset, io_parms->length);
4018 cifs_small_buf_release(req);
4020 *nbytes = le32_to_cpu(rsp->DataLength);
4021 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4022 (*nbytes > io_parms->length)) {
4023 cifs_dbg(FYI, "bad length %d for count %d\n",
4024 *nbytes, io_parms->length);
4030 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
4031 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4032 } else if (resp_buftype != CIFS_NO_BUFFER) {
4033 *buf = rsp_iov.iov_base;
4034 if (resp_buftype == CIFS_SMALL_BUFFER)
4035 *buf_type = CIFS_SMALL_BUFFER;
4036 else if (resp_buftype == CIFS_LARGE_BUFFER)
4037 *buf_type = CIFS_LARGE_BUFFER;
4043 * Check the mid_state and signature on received buffer (if any), and queue the
4044 * workqueue completion task.
4047 smb2_writev_callback(struct mid_q_entry *mid)
4049 struct cifs_writedata *wdata = mid->callback_data;
4050 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4051 struct TCP_Server_Info *server = tcon->ses->server;
4052 unsigned int written;
4053 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
4054 struct cifs_credits credits = { .value = 0, .instance = 0 };
4056 switch (mid->mid_state) {
4057 case MID_RESPONSE_RECEIVED:
4058 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4059 credits.instance = server->reconnect_instance;
4060 wdata->result = smb2_check_receive(mid, server, 0);
4061 if (wdata->result != 0)
4064 written = le32_to_cpu(rsp->DataLength);
4066 * Mask off high 16 bits when bytes written as returned
4067 * by the server is greater than bytes requested by the
4068 * client. OS/2 servers are known to set incorrect
4071 if (written > wdata->bytes)
4074 if (written < wdata->bytes)
4075 wdata->result = -ENOSPC;
4077 wdata->bytes = written;
4079 case MID_REQUEST_SUBMITTED:
4080 case MID_RETRY_NEEDED:
4081 wdata->result = -EAGAIN;
4083 case MID_RESPONSE_MALFORMED:
4084 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4085 credits.instance = server->reconnect_instance;
4088 wdata->result = -EIO;
4091 #ifdef CONFIG_CIFS_SMB_DIRECT
4093 * If this wdata has a memory registered, the MR can be freed
4094 * The number of MRs available is limited, it's important to recover
4095 * used MR as soon as I/O is finished. Hold MR longer in the later
4096 * I/O process can possibly result in I/O deadlock due to lack of MR
4097 * to send request on I/O retry
4100 smbd_deregister_mr(wdata->mr);
4104 if (wdata->result) {
4105 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4106 trace_smb3_write_err(0 /* no xid */,
4107 wdata->cfile->fid.persistent_fid,
4108 tcon->tid, tcon->ses->Suid, wdata->offset,
4109 wdata->bytes, wdata->result);
4110 if (wdata->result == -ENOSPC)
4111 printk_once(KERN_WARNING "Out of space writing to %s\n",
4114 trace_smb3_write_done(0 /* no xid */,
4115 wdata->cfile->fid.persistent_fid,
4116 tcon->tid, tcon->ses->Suid,
4117 wdata->offset, wdata->bytes);
4119 queue_work(cifsiod_wq, &wdata->work);
4120 DeleteMidQEntry(mid);
4121 add_credits(server, &credits, 0);
4124 /* smb2_async_writev - send an async write, and set up mid to handle result */
4126 smb2_async_writev(struct cifs_writedata *wdata,
4127 void (*release)(struct kref *kref))
4129 int rc = -EACCES, flags = 0;
4130 struct smb2_write_req *req = NULL;
4131 struct smb2_sync_hdr *shdr;
4132 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4133 struct TCP_Server_Info *server = tcon->ses->server;
4135 struct smb_rqst rqst = { };
4136 unsigned int total_len;
4138 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
4142 if (smb3_encryption_required(tcon))
4143 flags |= CIFS_TRANSFORM_REQ;
4145 shdr = (struct smb2_sync_hdr *)req;
4146 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
4148 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
4149 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
4150 req->WriteChannelInfoOffset = 0;
4151 req->WriteChannelInfoLength = 0;
4153 req->Offset = cpu_to_le64(wdata->offset);
4154 req->DataOffset = cpu_to_le16(
4155 offsetof(struct smb2_write_req, Buffer));
4156 req->RemainingBytes = 0;
4158 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
4159 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
4160 #ifdef CONFIG_CIFS_SMB_DIRECT
4162 * If we want to do a server RDMA read, fill in and append
4163 * smbd_buffer_descriptor_v1 to the end of write request
4165 if (server->rdma && !server->sign && wdata->bytes >=
4166 server->smbd_conn->rdma_readwrite_threshold) {
4168 struct smbd_buffer_descriptor_v1 *v1;
4169 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4171 wdata->mr = smbd_register_mr(
4172 server->smbd_conn, wdata->pages,
4173 wdata->nr_pages, wdata->page_offset,
4174 wdata->tailsz, false, need_invalidate);
4177 goto async_writev_out;
4180 req->DataOffset = 0;
4181 if (wdata->nr_pages > 1)
4182 req->RemainingBytes =
4184 (wdata->nr_pages - 1) * wdata->pagesz -
4185 wdata->page_offset + wdata->tailsz
4188 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
4189 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4190 if (need_invalidate)
4191 req->Channel = SMB2_CHANNEL_RDMA_V1;
4192 req->WriteChannelInfoOffset =
4193 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4194 req->WriteChannelInfoLength =
4195 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4196 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4197 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4198 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4199 v1->length = cpu_to_le32(wdata->mr->mr->length);
4202 iov[0].iov_len = total_len - 1;
4203 iov[0].iov_base = (char *)req;
4207 rqst.rq_pages = wdata->pages;
4208 rqst.rq_offset = wdata->page_offset;
4209 rqst.rq_npages = wdata->nr_pages;
4210 rqst.rq_pagesz = wdata->pagesz;
4211 rqst.rq_tailsz = wdata->tailsz;
4212 #ifdef CONFIG_CIFS_SMB_DIRECT
4214 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4218 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4219 wdata->offset, wdata->bytes);
4221 #ifdef CONFIG_CIFS_SMB_DIRECT
4222 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4224 req->Length = cpu_to_le32(wdata->bytes);
4226 req->Length = cpu_to_le32(wdata->bytes);
4229 if (wdata->credits.value > 0) {
4230 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4231 SMB2_MAX_BUFFER_SIZE));
4232 shdr->CreditRequest =
4233 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
4235 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4237 goto async_writev_out;
4239 flags |= CIFS_HAS_CREDITS;
4242 kref_get(&wdata->refcount);
4243 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4244 wdata, flags, &wdata->credits);
4247 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
4248 tcon->tid, tcon->ses->Suid, wdata->offset,
4250 kref_put(&wdata->refcount, release);
4251 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4255 cifs_small_buf_release(req);
4260 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4261 * The length field from io_parms must be at least 1 and indicates a number of
4262 * elements with data to write that begins with position 1 in iov array. All
4263 * data length is specified by count.
4266 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4267 unsigned int *nbytes, struct kvec *iov, int n_vec)
4269 struct smb_rqst rqst;
4271 struct smb2_write_req *req = NULL;
4272 struct smb2_write_rsp *rsp = NULL;
4274 struct kvec rsp_iov;
4276 unsigned int total_len;
4283 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
4288 if (io_parms->tcon->ses->server == NULL)
4289 return -ECONNABORTED;
4291 if (smb3_encryption_required(io_parms->tcon))
4292 flags |= CIFS_TRANSFORM_REQ;
4294 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
4296 req->PersistentFileId = io_parms->persistent_fid;
4297 req->VolatileFileId = io_parms->volatile_fid;
4298 req->WriteChannelInfoOffset = 0;
4299 req->WriteChannelInfoLength = 0;
4301 req->Length = cpu_to_le32(io_parms->length);
4302 req->Offset = cpu_to_le64(io_parms->offset);
4303 req->DataOffset = cpu_to_le16(
4304 offsetof(struct smb2_write_req, Buffer));
4305 req->RemainingBytes = 0;
4307 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4308 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4309 io_parms->offset, io_parms->length);
4311 iov[0].iov_base = (char *)req;
4313 iov[0].iov_len = total_len - 1;
4315 memset(&rqst, 0, sizeof(struct smb_rqst));
4317 rqst.rq_nvec = n_vec + 1;
4319 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
4320 &resp_buftype, flags, &rsp_iov);
4321 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
4324 trace_smb3_write_err(xid, req->PersistentFileId,
4325 io_parms->tcon->tid,
4326 io_parms->tcon->ses->Suid,
4327 io_parms->offset, io_parms->length, rc);
4328 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
4329 cifs_dbg(VFS, "Send error in write = %d\n", rc);
4331 *nbytes = le32_to_cpu(rsp->DataLength);
4332 trace_smb3_write_done(xid, req->PersistentFileId,
4333 io_parms->tcon->tid,
4334 io_parms->tcon->ses->Suid,
4335 io_parms->offset, *nbytes);
4338 cifs_small_buf_release(req);
4339 free_rsp_buf(resp_buftype, rsp);
4343 int posix_info_sid_size(const void *beg, const void *end)
4351 subauth = *(u8 *)(beg+1);
4352 if (subauth < 1 || subauth > 15)
4355 total = 1 + 1 + 6 + 4*subauth;
4356 if (beg + total > end)
4362 int posix_info_parse(const void *beg, const void *end,
4363 struct smb2_posix_info_parsed *out)
4369 const void *owner_sid;
4370 const void *group_sid;
4373 /* if no end bound given, assume payload to be correct */
4375 const struct smb2_posix_info *p = beg;
4377 end = beg + le32_to_cpu(p->NextEntryOffset);
4378 /* last element will have a 0 offset, pick a sensible bound */
4383 /* check base buf */
4384 if (beg + sizeof(struct smb2_posix_info) > end)
4386 total_len = sizeof(struct smb2_posix_info);
4388 /* check owner sid */
4389 owner_sid = beg + total_len;
4390 sid_len = posix_info_sid_size(owner_sid, end);
4393 total_len += sid_len;
4395 /* check group sid */
4396 group_sid = beg + total_len;
4397 sid_len = posix_info_sid_size(group_sid, end);
4400 total_len += sid_len;
4402 /* check name len */
4403 if (beg + total_len + 4 > end)
4405 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
4406 if (name_len < 1 || name_len > 0xFFFF)
4411 name = beg + total_len;
4412 if (name + name_len > end)
4414 total_len += name_len;
4418 out->size = total_len;
4419 out->name_len = name_len;
4421 memcpy(&out->owner, owner_sid,
4422 posix_info_sid_size(owner_sid, end));
4423 memcpy(&out->group, group_sid,
4424 posix_info_sid_size(group_sid, end));
4429 static int posix_info_extra_size(const void *beg, const void *end)
4431 int len = posix_info_parse(beg, end, NULL);
4435 return len - sizeof(struct smb2_posix_info);
4439 num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
4443 unsigned int entrycount = 0;
4444 unsigned int next_offset = 0;
4446 FILE_DIRECTORY_INFO *dir_info;
4448 if (bufstart == NULL)
4451 entryptr = bufstart;
4454 if (entryptr + next_offset < entryptr ||
4455 entryptr + next_offset > end_of_buf ||
4456 entryptr + next_offset + size > end_of_buf) {
4457 cifs_dbg(VFS, "malformed search entry would overflow\n");
4461 entryptr = entryptr + next_offset;
4462 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4464 if (infotype == SMB_FIND_FILE_POSIX_INFO)
4465 len = posix_info_extra_size(entryptr, end_of_buf);
4467 len = le32_to_cpu(dir_info->FileNameLength);
4470 entryptr + len < entryptr ||
4471 entryptr + len > end_of_buf ||
4472 entryptr + len + size > end_of_buf) {
4473 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4478 *lastentry = entryptr;
4481 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
4492 int SMB2_query_directory_init(const unsigned int xid,
4493 struct cifs_tcon *tcon, struct smb_rqst *rqst,
4494 u64 persistent_fid, u64 volatile_fid,
4495 int index, int info_level)
4497 struct TCP_Server_Info *server = tcon->ses->server;
4498 struct smb2_query_directory_req *req;
4499 unsigned char *bufptr;
4500 __le16 asteriks = cpu_to_le16('*');
4501 unsigned int output_size = CIFSMaxBufSize -
4502 MAX_SMB2_CREATE_RESPONSE_SIZE -
4503 MAX_SMB2_CLOSE_RESPONSE_SIZE;
4504 unsigned int total_len;
4505 struct kvec *iov = rqst->rq_iov;
4508 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
4513 switch (info_level) {
4514 case SMB_FIND_FILE_DIRECTORY_INFO:
4515 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4517 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4518 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4520 case SMB_FIND_FILE_POSIX_INFO:
4521 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
4524 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4529 req->FileIndex = cpu_to_le32(index);
4530 req->PersistentFileId = persistent_fid;
4531 req->VolatileFileId = volatile_fid;
4534 bufptr = req->Buffer;
4535 memcpy(bufptr, &asteriks, len);
4537 req->FileNameOffset =
4538 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
4539 req->FileNameLength = cpu_to_le16(len);
4541 * BB could be 30 bytes or so longer if we used SMB2 specific
4542 * buffer lengths, but this is safe and close enough.
4544 output_size = min_t(unsigned int, output_size, server->maxBuf);
4545 output_size = min_t(unsigned int, output_size, 2 << 15);
4546 req->OutputBufferLength = cpu_to_le32(output_size);
4548 iov[0].iov_base = (char *)req;
4550 iov[0].iov_len = total_len - 1;
4552 iov[1].iov_base = (char *)(req->Buffer);
4553 iov[1].iov_len = len;
4555 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4556 tcon->ses->Suid, index, output_size);
4561 void SMB2_query_directory_free(struct smb_rqst *rqst)
4563 if (rqst && rqst->rq_iov) {
4564 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4569 smb2_parse_query_directory(struct cifs_tcon *tcon,
4570 struct kvec *rsp_iov,
4572 struct cifs_search_info *srch_inf)
4574 struct smb2_query_directory_rsp *rsp;
4575 size_t info_buf_size;
4579 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
4581 switch (srch_inf->info_level) {
4582 case SMB_FIND_FILE_DIRECTORY_INFO:
4583 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4585 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4586 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4588 case SMB_FIND_FILE_POSIX_INFO:
4589 /* note that posix payload are variable size */
4590 info_buf_size = sizeof(struct smb2_posix_info);
4593 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4594 srch_inf->info_level);
4598 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4599 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
4602 cifs_tcon_dbg(VFS, "bad info payload");
4606 srch_inf->unicode = true;
4608 if (srch_inf->ntwrk_buf_start) {
4609 if (srch_inf->smallBuf)
4610 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4612 cifs_buf_release(srch_inf->ntwrk_buf_start);
4614 srch_inf->ntwrk_buf_start = (char *)rsp;
4615 srch_inf->srch_entries_start = srch_inf->last_entry =
4616 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4617 end_of_smb = rsp_iov->iov_len + (char *)rsp;
4619 srch_inf->entries_in_buffer = num_entries(
4620 srch_inf->info_level,
4621 srch_inf->srch_entries_start,
4623 &srch_inf->last_entry,
4626 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4627 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4628 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4629 srch_inf->srch_entries_start, srch_inf->last_entry);
4630 if (resp_buftype == CIFS_LARGE_BUFFER)
4631 srch_inf->smallBuf = false;
4632 else if (resp_buftype == CIFS_SMALL_BUFFER)
4633 srch_inf->smallBuf = true;
4635 cifs_tcon_dbg(VFS, "illegal search buffer type\n");
4641 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4642 u64 persistent_fid, u64 volatile_fid, int index,
4643 struct cifs_search_info *srch_inf)
4645 struct smb_rqst rqst;
4646 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
4647 struct smb2_query_directory_rsp *rsp = NULL;
4648 int resp_buftype = CIFS_NO_BUFFER;
4649 struct kvec rsp_iov;
4651 struct cifs_ses *ses = tcon->ses;
4654 if (!ses || !(ses->server))
4657 if (smb3_encryption_required(tcon))
4658 flags |= CIFS_TRANSFORM_REQ;
4660 memset(&rqst, 0, sizeof(struct smb_rqst));
4661 memset(&iov, 0, sizeof(iov));
4663 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
4665 rc = SMB2_query_directory_init(xid, tcon, &rqst, persistent_fid,
4666 volatile_fid, index,
4667 srch_inf->info_level);
4671 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4672 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
4675 if (rc == -ENODATA &&
4676 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
4677 trace_smb3_query_dir_done(xid, persistent_fid,
4678 tcon->tid, tcon->ses->Suid, index, 0);
4679 srch_inf->endOfSearch = true;
4682 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4683 tcon->ses->Suid, index, 0, rc);
4684 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
4689 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
4692 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4693 tcon->ses->Suid, index, 0, rc);
4696 resp_buftype = CIFS_NO_BUFFER;
4698 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4699 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
4702 SMB2_query_directory_free(&rqst);
4703 free_rsp_buf(resp_buftype, rsp);
4708 SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
4709 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4710 u8 info_type, u32 additional_info,
4711 void **data, unsigned int *size)
4713 struct smb2_set_info_req *req;
4714 struct kvec *iov = rqst->rq_iov;
4715 unsigned int i, total_len;
4718 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
4722 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4723 req->InfoType = info_type;
4724 req->FileInfoClass = info_class;
4725 req->PersistentFileId = persistent_fid;
4726 req->VolatileFileId = volatile_fid;
4727 req->AdditionalInformation = cpu_to_le32(additional_info);
4730 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
4731 req->BufferLength = cpu_to_le32(*size);
4733 memcpy(req->Buffer, *data, *size);
4736 iov[0].iov_base = (char *)req;
4738 iov[0].iov_len = total_len - 1;
4740 for (i = 1; i < rqst->rq_nvec; i++) {
4741 le32_add_cpu(&req->BufferLength, size[i]);
4742 iov[i].iov_base = (char *)data[i];
4743 iov[i].iov_len = size[i];
4750 SMB2_set_info_free(struct smb_rqst *rqst)
4752 if (rqst && rqst->rq_iov)
4753 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
4757 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4758 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4759 u8 info_type, u32 additional_info, unsigned int num,
4760 void **data, unsigned int *size)
4762 struct smb_rqst rqst;
4763 struct smb2_set_info_rsp *rsp = NULL;
4765 struct kvec rsp_iov;
4768 struct cifs_ses *ses = tcon->ses;
4771 if (!ses || !(ses->server))
4777 if (smb3_encryption_required(tcon))
4778 flags |= CIFS_TRANSFORM_REQ;
4780 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4784 memset(&rqst, 0, sizeof(struct smb_rqst));
4788 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4789 info_class, info_type, additional_info,
4797 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
4799 SMB2_set_info_free(&rqst);
4800 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
4803 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
4804 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4805 ses->Suid, info_class, (__u32)info_type, rc);
4808 free_rsp_buf(resp_buftype, rsp);
4814 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4815 u64 volatile_fid, u32 pid, __le64 *eof)
4817 struct smb2_file_eof_info info;
4821 info.EndOfFile = *eof;
4824 size = sizeof(struct smb2_file_eof_info);
4826 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4827 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4828 0, 1, &data, &size);
4832 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4833 u64 persistent_fid, u64 volatile_fid,
4834 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4836 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4837 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4838 1, (void **)&pnntsd, &pacllen);
4842 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4843 u64 persistent_fid, u64 volatile_fid,
4844 struct smb2_file_full_ea_info *buf, int len)
4846 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4847 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4848 0, 1, (void **)&buf, &len);
4852 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4853 const u64 persistent_fid, const u64 volatile_fid,
4856 struct smb_rqst rqst;
4858 struct smb2_oplock_break *req = NULL;
4859 struct cifs_ses *ses = tcon->ses;
4860 int flags = CIFS_OBREAK_OP;
4861 unsigned int total_len;
4863 struct kvec rsp_iov;
4866 cifs_dbg(FYI, "SMB2_oplock_break\n");
4867 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4872 if (smb3_encryption_required(tcon))
4873 flags |= CIFS_TRANSFORM_REQ;
4875 req->VolatileFid = volatile_fid;
4876 req->PersistentFid = persistent_fid;
4877 req->OplockLevel = oplock_level;
4878 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4880 flags |= CIFS_NO_RSP_BUF;
4882 iov[0].iov_base = (char *)req;
4883 iov[0].iov_len = total_len;
4885 memset(&rqst, 0, sizeof(struct smb_rqst));
4889 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4890 cifs_small_buf_release(req);
4893 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4894 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
4901 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4902 struct kstatfs *kst)
4904 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4905 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4906 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
4907 kst->f_bfree = kst->f_bavail =
4908 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
4913 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4914 struct kstatfs *kst)
4916 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4917 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4918 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4919 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4920 kst->f_bavail = kst->f_bfree;
4922 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4923 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4924 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4925 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4926 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4932 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4933 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4936 struct smb2_query_info_req *req;
4937 unsigned int total_len;
4939 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
4941 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4944 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4949 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4950 req->FileInfoClass = level;
4951 req->PersistentFileId = persistent_fid;
4952 req->VolatileFileId = volatile_fid;
4954 req->InputBufferOffset =
4955 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
4956 req->OutputBufferLength = cpu_to_le32(
4957 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
4959 iov->iov_base = (char *)req;
4960 iov->iov_len = total_len;
4965 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4966 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4968 struct smb_rqst rqst;
4969 struct smb2_query_info_rsp *rsp = NULL;
4971 struct kvec rsp_iov;
4974 struct cifs_ses *ses = tcon->ses;
4975 FILE_SYSTEM_POSIX_INFO *info = NULL;
4978 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4979 sizeof(FILE_SYSTEM_POSIX_INFO),
4980 persistent_fid, volatile_fid);
4984 if (smb3_encryption_required(tcon))
4985 flags |= CIFS_TRANSFORM_REQ;
4987 memset(&rqst, 0, sizeof(struct smb_rqst));
4991 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4992 cifs_small_buf_release(iov.iov_base);
4994 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4995 goto posix_qfsinf_exit;
4997 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4999 info = (FILE_SYSTEM_POSIX_INFO *)(
5000 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5001 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5002 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5003 sizeof(FILE_SYSTEM_POSIX_INFO));
5005 copy_posix_fs_info_to_kstatfs(info, fsdata);
5008 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5013 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5014 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5016 struct smb_rqst rqst;
5017 struct smb2_query_info_rsp *rsp = NULL;
5019 struct kvec rsp_iov;
5022 struct cifs_ses *ses = tcon->ses;
5023 struct smb2_fs_full_size_info *info = NULL;
5026 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
5027 sizeof(struct smb2_fs_full_size_info),
5028 persistent_fid, volatile_fid);
5032 if (smb3_encryption_required(tcon))
5033 flags |= CIFS_TRANSFORM_REQ;
5035 memset(&rqst, 0, sizeof(struct smb_rqst));
5039 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
5040 cifs_small_buf_release(iov.iov_base);
5042 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5045 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5047 info = (struct smb2_fs_full_size_info *)(
5048 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5049 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5050 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5051 sizeof(struct smb2_fs_full_size_info));
5053 smb2_copy_fs_info_to_kstatfs(info, fsdata);
5056 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5061 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
5062 u64 persistent_fid, u64 volatile_fid, int level)
5064 struct smb_rqst rqst;
5065 struct smb2_query_info_rsp *rsp = NULL;
5067 struct kvec rsp_iov;
5069 int resp_buftype, max_len, min_len;
5070 struct cifs_ses *ses = tcon->ses;
5071 unsigned int rsp_len, offset;
5074 if (level == FS_DEVICE_INFORMATION) {
5075 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5076 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5077 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5078 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5079 min_len = MIN_FS_ATTR_INFO_SIZE;
5080 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5081 max_len = sizeof(struct smb3_fs_ss_info);
5082 min_len = sizeof(struct smb3_fs_ss_info);
5083 } else if (level == FS_VOLUME_INFORMATION) {
5084 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5085 min_len = sizeof(struct smb3_fs_vol_info);
5087 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
5091 rc = build_qfs_info_req(&iov, tcon, level, max_len,
5092 persistent_fid, volatile_fid);
5096 if (smb3_encryption_required(tcon))
5097 flags |= CIFS_TRANSFORM_REQ;
5099 memset(&rqst, 0, sizeof(struct smb_rqst));
5103 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
5104 cifs_small_buf_release(iov.iov_base);
5106 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5109 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5111 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5112 offset = le16_to_cpu(rsp->OutputBufferOffset);
5113 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
5117 if (level == FS_ATTRIBUTE_INFORMATION)
5118 memcpy(&tcon->fsAttrInfo, offset
5119 + (char *)rsp, min_t(unsigned int,
5121 else if (level == FS_DEVICE_INFORMATION)
5122 memcpy(&tcon->fsDevInfo, offset
5123 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
5124 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5125 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
5126 (offset + (char *)rsp);
5127 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5128 tcon->perf_sector_size =
5129 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
5130 } else if (level == FS_VOLUME_INFORMATION) {
5131 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5132 (offset + (char *)rsp);
5133 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5134 tcon->vol_create_time = vol_info->VolumeCreationTime;
5138 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5143 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5144 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5145 const __u32 num_lock, struct smb2_lock_element *buf)
5147 struct smb_rqst rqst;
5149 struct smb2_lock_req *req = NULL;
5151 struct kvec rsp_iov;
5154 int flags = CIFS_NO_RSP_BUF;
5155 unsigned int total_len;
5157 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
5159 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
5163 if (smb3_encryption_required(tcon))
5164 flags |= CIFS_TRANSFORM_REQ;
5166 req->sync_hdr.ProcessId = cpu_to_le32(pid);
5167 req->LockCount = cpu_to_le16(num_lock);
5169 req->PersistentFileId = persist_fid;
5170 req->VolatileFileId = volatile_fid;
5172 count = num_lock * sizeof(struct smb2_lock_element);
5174 iov[0].iov_base = (char *)req;
5175 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
5176 iov[1].iov_base = (char *)buf;
5177 iov[1].iov_len = count;
5179 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
5181 memset(&rqst, 0, sizeof(struct smb_rqst));
5185 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
5187 cifs_small_buf_release(req);
5189 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
5190 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
5191 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
5192 tcon->ses->Suid, rc);
5199 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
5200 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5201 const __u64 length, const __u64 offset, const __u32 lock_flags,
5204 struct smb2_lock_element lock;
5206 lock.Offset = cpu_to_le64(offset);
5207 lock.Length = cpu_to_le64(length);
5208 lock.Flags = cpu_to_le32(lock_flags);
5209 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5210 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5212 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5216 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5217 __u8 *lease_key, const __le32 lease_state)
5219 struct smb_rqst rqst;
5221 struct smb2_lease_ack *req = NULL;
5222 struct cifs_ses *ses = tcon->ses;
5223 int flags = CIFS_OBREAK_OP;
5224 unsigned int total_len;
5226 struct kvec rsp_iov;
5228 __u64 *please_key_high;
5229 __u64 *please_key_low;
5231 cifs_dbg(FYI, "SMB2_lease_break\n");
5232 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
5237 if (smb3_encryption_required(tcon))
5238 flags |= CIFS_TRANSFORM_REQ;
5240 req->sync_hdr.CreditRequest = cpu_to_le16(1);
5241 req->StructureSize = cpu_to_le16(36);
5244 memcpy(req->LeaseKey, lease_key, 16);
5245 req->LeaseState = lease_state;
5247 flags |= CIFS_NO_RSP_BUF;
5249 iov[0].iov_base = (char *)req;
5250 iov[0].iov_len = total_len;
5252 memset(&rqst, 0, sizeof(struct smb_rqst));
5256 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
5257 cifs_small_buf_release(req);
5259 please_key_low = (__u64 *)lease_key;
5260 please_key_high = (__u64 *)(lease_key+8);
5262 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5263 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5264 ses->Suid, *please_key_low, *please_key_high, rc);
5265 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
5267 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5268 ses->Suid, *please_key_low, *please_key_high);