4 * Copyright (C) International Business Machines Corp., 2009, 2013
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
9 * Contains the routines for constructing the SMB2 PDUs themselves
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
32 #include <linux/kernel.h>
33 #include <linux/vfs.h>
34 #include <linux/task_io_accounting_ops.h>
35 #include <linux/uaccess.h>
36 #include <linux/uuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/xattr.h>
42 #include "cifsproto.h"
43 #include "smb2proto.h"
44 #include "cifs_unicode.h"
45 #include "cifs_debug.h"
47 #include "smb2status.h"
50 #include "cifs_spnego.h"
51 #include "smbdirect.h"
53 #ifdef CONFIG_CIFS_DFS_UPCALL
54 #include "dfs_cache.h"
58 * The following table defines the expected "StructureSize" of SMB2 requests
59 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
61 * Note that commands are defined in smb2pdu.h in le16 but the array below is
62 * indexed by command in host byte order.
64 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
65 /* SMB2_NEGOTIATE */ 36,
66 /* SMB2_SESSION_SETUP */ 25,
68 /* SMB2_TREE_CONNECT */ 9,
69 /* SMB2_TREE_DISCONNECT */ 4,
79 /* SMB2_QUERY_DIRECTORY */ 33,
80 /* SMB2_CHANGE_NOTIFY */ 32,
81 /* SMB2_QUERY_INFO */ 41,
82 /* SMB2_SET_INFO */ 33,
83 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
86 int smb3_encryption_required(const struct cifs_tcon *tcon)
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);
1556 kfree(ntlmssp_blob);
1557 SMB2_sess_free_buffer(sess_data);
1558 kfree(ses->ntlmssp);
1559 ses->ntlmssp = NULL;
1560 sess_data->result = rc;
1561 sess_data->func = NULL;
1565 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1569 type = smb2_select_sectype(cifs_ses_server(ses), ses->sectype);
1570 cifs_dbg(FYI, "sess setup type %d\n", type);
1571 if (type == Unspecified) {
1573 "Unable to select appropriate authentication method!");
1579 sess_data->func = SMB2_auth_kerberos;
1582 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1585 cifs_dbg(VFS, "secType %d not supported!\n", type);
1593 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1594 const struct nls_table *nls_cp)
1597 struct TCP_Server_Info *server = cifs_ses_server(ses);
1598 struct SMB2_sess_data *sess_data;
1600 cifs_dbg(FYI, "Session Setup\n");
1603 WARN(1, "%s: server is NULL!\n", __func__);
1607 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1611 rc = SMB2_select_sec(ses, sess_data);
1614 sess_data->xid = xid;
1615 sess_data->ses = ses;
1616 sess_data->buf0_type = CIFS_NO_BUFFER;
1617 sess_data->nls_cp = (struct nls_table *) nls_cp;
1618 sess_data->previous_session = ses->Suid;
1621 * Initialize the session hash with the server one.
1623 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
1624 SMB2_PREAUTH_HASH_SIZE);
1626 while (sess_data->func)
1627 sess_data->func(sess_data);
1629 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1630 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1631 rc = sess_data->result;
1638 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1640 struct smb_rqst rqst;
1641 struct smb2_logoff_req *req; /* response is also trivial struct */
1643 struct TCP_Server_Info *server;
1645 unsigned int total_len;
1647 struct kvec rsp_iov;
1650 cifs_dbg(FYI, "disconnect session %p\n", ses);
1652 if (ses && (ses->server))
1653 server = ses->server;
1657 /* no need to send SMB logoff if uid already closed due to reconnect */
1658 if (ses->need_reconnect)
1659 goto smb2_session_already_dead;
1661 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
1665 /* since no tcon, smb2_init can not do this, so do here */
1666 req->sync_hdr.SessionId = ses->Suid;
1668 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1669 flags |= CIFS_TRANSFORM_REQ;
1670 else if (server->sign)
1671 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1673 flags |= CIFS_NO_RSP_BUF;
1675 iov[0].iov_base = (char *)req;
1676 iov[0].iov_len = total_len;
1678 memset(&rqst, 0, sizeof(struct smb_rqst));
1682 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1683 cifs_small_buf_release(req);
1685 * No tcon so can't do
1686 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1689 smb2_session_already_dead:
1693 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1695 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1698 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1700 /* These are similar values to what Windows uses */
1701 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1703 tcon->max_chunks = 256;
1704 tcon->max_bytes_chunk = 1048576;
1705 tcon->max_bytes_copy = 16777216;
1709 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1710 struct cifs_tcon *tcon, const struct nls_table *cp)
1712 struct smb_rqst rqst;
1713 struct smb2_tree_connect_req *req;
1714 struct smb2_tree_connect_rsp *rsp = NULL;
1716 struct kvec rsp_iov = { NULL, 0 };
1720 __le16 *unc_path = NULL;
1722 unsigned int total_len;
1723 struct TCP_Server_Info *server = ses->server;
1725 cifs_dbg(FYI, "TCON\n");
1727 if (!server || !tree)
1730 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1731 if (unc_path == NULL)
1734 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1736 if (unc_path_len < 2) {
1741 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1743 atomic_set(&tcon->num_remote_opens, 0);
1744 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1751 if (smb3_encryption_required(tcon))
1752 flags |= CIFS_TRANSFORM_REQ;
1754 iov[0].iov_base = (char *)req;
1756 iov[0].iov_len = total_len - 1;
1758 /* Testing shows that buffer offset must be at location of Buffer[0] */
1759 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1761 req->PathLength = cpu_to_le16(unc_path_len - 2);
1762 iov[1].iov_base = unc_path;
1763 iov[1].iov_len = unc_path_len;
1766 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1767 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1768 * (Samba servers don't always set the flag so also check if null user)
1770 if ((server->dialect == SMB311_PROT_ID) &&
1771 !smb3_encryption_required(tcon) &&
1772 !(ses->session_flags &
1773 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1774 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1775 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1777 memset(&rqst, 0, sizeof(struct smb_rqst));
1781 /* Need 64 for max size write so ask for more in case not there yet */
1782 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1784 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
1785 cifs_small_buf_release(req);
1786 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1787 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1790 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1791 tcon->need_reconnect = true;
1793 goto tcon_error_exit;
1796 switch (rsp->ShareType) {
1797 case SMB2_SHARE_TYPE_DISK:
1798 cifs_dbg(FYI, "connection to disk share\n");
1800 case SMB2_SHARE_TYPE_PIPE:
1802 cifs_dbg(FYI, "connection to pipe share\n");
1804 case SMB2_SHARE_TYPE_PRINT:
1806 cifs_dbg(FYI, "connection to printer\n");
1809 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1811 goto tcon_error_exit;
1814 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1815 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1816 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1817 tcon->tidStatus = CifsGood;
1818 tcon->need_reconnect = false;
1819 tcon->tid = rsp->sync_hdr.TreeId;
1820 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1822 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1823 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1824 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
1827 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1828 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
1830 init_copy_chunk_defaults(tcon);
1831 if (server->ops->validate_negotiate)
1832 rc = server->ops->validate_negotiate(xid, tcon);
1835 free_rsp_buf(resp_buftype, rsp);
1840 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1841 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1847 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1849 struct smb_rqst rqst;
1850 struct smb2_tree_disconnect_req *req; /* response is trivial */
1852 struct cifs_ses *ses = tcon->ses;
1854 unsigned int total_len;
1856 struct kvec rsp_iov;
1859 cifs_dbg(FYI, "Tree Disconnect\n");
1861 if (!ses || !(ses->server))
1864 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1867 close_shroot_lease(&tcon->crfid);
1869 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1874 if (smb3_encryption_required(tcon))
1875 flags |= CIFS_TRANSFORM_REQ;
1877 flags |= CIFS_NO_RSP_BUF;
1879 iov[0].iov_base = (char *)req;
1880 iov[0].iov_len = total_len;
1882 memset(&rqst, 0, sizeof(struct smb_rqst));
1886 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1887 cifs_small_buf_release(req);
1889 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1895 static struct create_durable *
1896 create_durable_buf(void)
1898 struct create_durable *buf;
1900 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1904 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1905 (struct create_durable, Data));
1906 buf->ccontext.DataLength = cpu_to_le32(16);
1907 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1908 (struct create_durable, Name));
1909 buf->ccontext.NameLength = cpu_to_le16(4);
1910 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1918 static struct create_durable *
1919 create_reconnect_durable_buf(struct cifs_fid *fid)
1921 struct create_durable *buf;
1923 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1927 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1928 (struct create_durable, Data));
1929 buf->ccontext.DataLength = cpu_to_le32(16);
1930 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1931 (struct create_durable, Name));
1932 buf->ccontext.NameLength = cpu_to_le16(4);
1933 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1934 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1935 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1944 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1946 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1948 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1949 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1950 buf->IndexNumber = pdisk_id->DiskFileId;
1954 parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
1955 struct create_posix_rsp *posix)
1958 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
1959 u8 *end = beg + le32_to_cpu(cc->DataLength);
1962 memset(posix, 0, sizeof(*posix));
1964 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
1965 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
1966 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
1969 sid_len = posix_info_sid_size(sid, end);
1971 cifs_dbg(VFS, "bad owner sid in posix create response\n");
1974 memcpy(&posix->owner, sid, sid_len);
1976 sid = sid + sid_len;
1977 sid_len = posix_info_sid_size(sid, end);
1979 cifs_dbg(VFS, "bad group sid in posix create response\n");
1982 memcpy(&posix->group, sid, sid_len);
1984 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
1985 posix->nlink, posix->mode, posix->reparse_tag);
1989 smb2_parse_contexts(struct TCP_Server_Info *server,
1990 struct smb2_create_rsp *rsp,
1991 unsigned int *epoch, char *lease_key, __u8 *oplock,
1992 struct smb2_file_all_info *buf,
1993 struct create_posix_rsp *posix)
1996 struct create_context *cc;
1998 unsigned int remaining;
2000 const char smb3_create_tag_posix[] = {0x93, 0xAD, 0x25, 0x50, 0x9C,
2001 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
2002 0xDE, 0x96, 0x8B, 0xCD, 0x7C};
2005 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
2006 remaining = le32_to_cpu(rsp->CreateContextsLength);
2007 cc = (struct create_context *)data_offset;
2009 /* Initialize inode number to 0 in case no valid data in qfid context */
2011 buf->IndexNumber = 0;
2013 while (remaining >= sizeof(struct create_context)) {
2014 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
2015 if (le16_to_cpu(cc->NameLength) == 4 &&
2016 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
2017 *oplock = server->ops->parse_lease_buf(cc, epoch,
2019 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
2020 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
2021 parse_query_id_ctxt(cc, buf);
2022 else if ((le16_to_cpu(cc->NameLength) == 16)) {
2024 memcmp(name, smb3_create_tag_posix, 16) == 0)
2025 parse_posix_ctxt(cc, buf, posix);
2028 cifs_dbg(FYI, "Context not matched with len %d\n",
2029 le16_to_cpu(cc->NameLength));
2030 cifs_dump_mem("Cctxt name: ", name, 4);
2033 next = le32_to_cpu(cc->Next);
2037 cc = (struct create_context *)((char *)cc + next);
2040 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2041 *oplock = rsp->OplockLevel;
2047 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
2048 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
2050 struct smb2_create_req *req = iov[0].iov_base;
2051 unsigned int num = *num_iovec;
2053 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
2054 if (iov[num].iov_base == NULL)
2056 iov[num].iov_len = server->vals->create_lease_size;
2057 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2058 if (!req->CreateContextsOffset)
2059 req->CreateContextsOffset = cpu_to_le32(
2060 sizeof(struct smb2_create_req) +
2061 iov[num - 1].iov_len);
2062 le32_add_cpu(&req->CreateContextsLength,
2063 server->vals->create_lease_size);
2064 *num_iovec = num + 1;
2068 static struct create_durable_v2 *
2069 create_durable_v2_buf(struct cifs_open_parms *oparms)
2071 struct cifs_fid *pfid = oparms->fid;
2072 struct create_durable_v2 *buf;
2074 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2078 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2079 (struct create_durable_v2, dcontext));
2080 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2081 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2082 (struct create_durable_v2, Name));
2083 buf->ccontext.NameLength = cpu_to_le16(4);
2086 * NB: Handle timeout defaults to 0, which allows server to choose
2087 * (most servers default to 120 seconds) and most clients default to 0.
2088 * This can be overridden at mount ("handletimeout=") if the user wants
2089 * a different persistent (or resilient) handle timeout for all opens
2090 * opens on a particular SMB3 mount.
2092 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2093 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2094 generate_random_uuid(buf->dcontext.CreateGuid);
2095 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2097 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2105 static struct create_durable_handle_reconnect_v2 *
2106 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2108 struct create_durable_handle_reconnect_v2 *buf;
2110 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2115 buf->ccontext.DataOffset =
2116 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2118 buf->ccontext.DataLength =
2119 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2120 buf->ccontext.NameOffset =
2121 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2123 buf->ccontext.NameLength = cpu_to_le16(4);
2125 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2126 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2127 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2128 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2130 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2139 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2140 struct cifs_open_parms *oparms)
2142 struct smb2_create_req *req = iov[0].iov_base;
2143 unsigned int num = *num_iovec;
2145 iov[num].iov_base = create_durable_v2_buf(oparms);
2146 if (iov[num].iov_base == NULL)
2148 iov[num].iov_len = sizeof(struct create_durable_v2);
2149 if (!req->CreateContextsOffset)
2150 req->CreateContextsOffset =
2151 cpu_to_le32(sizeof(struct smb2_create_req) +
2153 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
2154 *num_iovec = num + 1;
2159 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2160 struct cifs_open_parms *oparms)
2162 struct smb2_create_req *req = iov[0].iov_base;
2163 unsigned int num = *num_iovec;
2165 /* indicate that we don't need to relock the file */
2166 oparms->reconnect = false;
2168 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2169 if (iov[num].iov_base == NULL)
2171 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2172 if (!req->CreateContextsOffset)
2173 req->CreateContextsOffset =
2174 cpu_to_le32(sizeof(struct smb2_create_req) +
2176 le32_add_cpu(&req->CreateContextsLength,
2177 sizeof(struct create_durable_handle_reconnect_v2));
2178 *num_iovec = num + 1;
2183 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2184 struct cifs_open_parms *oparms, bool use_persistent)
2186 struct smb2_create_req *req = iov[0].iov_base;
2187 unsigned int num = *num_iovec;
2189 if (use_persistent) {
2190 if (oparms->reconnect)
2191 return add_durable_reconnect_v2_context(iov, num_iovec,
2194 return add_durable_v2_context(iov, num_iovec, oparms);
2197 if (oparms->reconnect) {
2198 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2199 /* indicate that we don't need to relock the file */
2200 oparms->reconnect = false;
2202 iov[num].iov_base = create_durable_buf();
2203 if (iov[num].iov_base == NULL)
2205 iov[num].iov_len = sizeof(struct create_durable);
2206 if (!req->CreateContextsOffset)
2207 req->CreateContextsOffset =
2208 cpu_to_le32(sizeof(struct smb2_create_req) +
2210 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
2211 *num_iovec = num + 1;
2215 /* See MS-SMB2 2.2.13.2.7 */
2216 static struct crt_twarp_ctxt *
2217 create_twarp_buf(__u64 timewarp)
2219 struct crt_twarp_ctxt *buf;
2221 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2225 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2226 (struct crt_twarp_ctxt, Timestamp));
2227 buf->ccontext.DataLength = cpu_to_le32(8);
2228 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2229 (struct crt_twarp_ctxt, Name));
2230 buf->ccontext.NameLength = cpu_to_le16(4);
2231 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2236 buf->Timestamp = cpu_to_le64(timewarp);
2240 /* See MS-SMB2 2.2.13.2.7 */
2242 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2244 struct smb2_create_req *req = iov[0].iov_base;
2245 unsigned int num = *num_iovec;
2247 iov[num].iov_base = create_twarp_buf(timewarp);
2248 if (iov[num].iov_base == NULL)
2250 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2251 if (!req->CreateContextsOffset)
2252 req->CreateContextsOffset = cpu_to_le32(
2253 sizeof(struct smb2_create_req) +
2254 iov[num - 1].iov_len);
2255 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2256 *num_iovec = num + 1;
2260 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2261 static struct crt_sd_ctxt *
2262 create_sd_buf(umode_t mode, unsigned int *len)
2264 struct crt_sd_ctxt *buf;
2265 struct cifs_ace *pace;
2266 unsigned int sdlen, acelen;
2268 *len = roundup(sizeof(struct crt_sd_ctxt) + sizeof(struct cifs_ace) * 2,
2270 buf = kzalloc(*len, GFP_KERNEL);
2274 sdlen = sizeof(struct smb3_sd) + sizeof(struct smb3_acl) +
2275 2 * sizeof(struct cifs_ace);
2277 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2278 (struct crt_sd_ctxt, sd));
2279 buf->ccontext.DataLength = cpu_to_le32(sdlen);
2280 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2281 (struct crt_sd_ctxt, Name));
2282 buf->ccontext.NameLength = cpu_to_le16(4);
2283 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2288 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2290 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2291 * and "DP" ie the DACL is present
2293 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2295 /* offset owner, group and Sbz1 and SACL are all zero */
2296 buf->sd.OffsetDacl = cpu_to_le32(sizeof(struct smb3_sd));
2297 buf->acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2299 /* create one ACE to hold the mode embedded in reserved special SID */
2300 pace = (struct cifs_ace *)(sizeof(struct crt_sd_ctxt) + (char *)buf);
2301 acelen = setup_special_mode_ACE(pace, (__u64)mode);
2302 /* and one more ACE to allow access for authenticated users */
2303 pace = (struct cifs_ace *)(acelen + (sizeof(struct crt_sd_ctxt) +
2305 acelen += setup_authusers_ACE(pace);
2306 buf->acl.AclSize = cpu_to_le16(sizeof(struct cifs_acl) + acelen);
2307 buf->acl.AceCount = cpu_to_le16(2);
2312 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
2314 struct smb2_create_req *req = iov[0].iov_base;
2315 unsigned int num = *num_iovec;
2316 unsigned int len = 0;
2318 iov[num].iov_base = create_sd_buf(mode, &len);
2319 if (iov[num].iov_base == NULL)
2321 iov[num].iov_len = len;
2322 if (!req->CreateContextsOffset)
2323 req->CreateContextsOffset = cpu_to_le32(
2324 sizeof(struct smb2_create_req) +
2325 iov[num - 1].iov_len);
2326 le32_add_cpu(&req->CreateContextsLength, len);
2327 *num_iovec = num + 1;
2331 static struct crt_query_id_ctxt *
2332 create_query_id_buf(void)
2334 struct crt_query_id_ctxt *buf;
2336 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2340 buf->ccontext.DataOffset = cpu_to_le16(0);
2341 buf->ccontext.DataLength = cpu_to_le32(0);
2342 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2343 (struct crt_query_id_ctxt, Name));
2344 buf->ccontext.NameLength = cpu_to_le16(4);
2345 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2353 /* See MS-SMB2 2.2.13.2.9 */
2355 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2357 struct smb2_create_req *req = iov[0].iov_base;
2358 unsigned int num = *num_iovec;
2360 iov[num].iov_base = create_query_id_buf();
2361 if (iov[num].iov_base == NULL)
2363 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2364 if (!req->CreateContextsOffset)
2365 req->CreateContextsOffset = cpu_to_le32(
2366 sizeof(struct smb2_create_req) +
2367 iov[num - 1].iov_len);
2368 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2369 *num_iovec = num + 1;
2374 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2375 const char *treename, const __le16 *path)
2377 int treename_len, path_len;
2378 struct nls_table *cp;
2379 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2384 treename_len = strlen(treename);
2385 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2391 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2394 * make room for one path separator between the treename and
2397 *out_len = treename_len + 1 + path_len;
2400 * final path needs to be null-terminated UTF16 with a
2404 *out_size = roundup((*out_len+1)*2, 8);
2405 *out_path = kzalloc(*out_size, GFP_KERNEL);
2409 cp = load_nls_default();
2410 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2411 UniStrcat(*out_path, sep);
2412 UniStrcat(*out_path, path);
2418 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2419 umode_t mode, struct cifs_tcon *tcon,
2420 const char *full_path,
2421 struct cifs_sb_info *cifs_sb)
2423 struct smb_rqst rqst;
2424 struct smb2_create_req *req;
2425 struct smb2_create_rsp *rsp = NULL;
2426 struct cifs_ses *ses = tcon->ses;
2427 struct kvec iov[3]; /* make sure at least one for each open context */
2428 struct kvec rsp_iov = {NULL, 0};
2431 __le16 *copy_path = NULL;
2434 unsigned int n_iov = 2;
2435 __u32 file_attributes = 0;
2436 char *pc_buf = NULL;
2438 unsigned int total_len;
2439 __le16 *utf16_path = NULL;
2441 cifs_dbg(FYI, "mkdir\n");
2443 /* resource #1: path allocation */
2444 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2448 if (!ses || !(ses->server)) {
2453 /* resource #2: request */
2454 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2459 if (smb3_encryption_required(tcon))
2460 flags |= CIFS_TRANSFORM_REQ;
2462 req->ImpersonationLevel = IL_IMPERSONATION;
2463 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2464 /* File attributes ignored on open (used in create though) */
2465 req->FileAttributes = cpu_to_le32(file_attributes);
2466 req->ShareAccess = FILE_SHARE_ALL_LE;
2467 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2468 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2470 iov[0].iov_base = (char *)req;
2471 /* -1 since last byte is buf[0] which is sent below (path) */
2472 iov[0].iov_len = total_len - 1;
2474 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2476 /* [MS-SMB2] 2.2.13 NameOffset:
2477 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2478 * the SMB2 header, the file name includes a prefix that will
2479 * be processed during DFS name normalization as specified in
2480 * section 3.3.5.9. Otherwise, the file name is relative to
2481 * the share that is identified by the TreeId in the SMB2
2484 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2487 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2488 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2490 tcon->treeName, utf16_path);
2494 req->NameLength = cpu_to_le16(name_len * 2);
2495 uni_path_len = copy_size;
2496 /* free before overwriting resource */
2498 utf16_path = copy_path;
2500 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2501 /* MUST set path len (NameLength) to 0 opening root of share */
2502 req->NameLength = cpu_to_le16(uni_path_len - 2);
2503 if (uni_path_len % 8 != 0) {
2504 copy_size = roundup(uni_path_len, 8);
2505 copy_path = kzalloc(copy_size, GFP_KERNEL);
2510 memcpy((char *)copy_path, (const char *)utf16_path,
2512 uni_path_len = copy_size;
2513 /* free before overwriting resource */
2515 utf16_path = copy_path;
2519 iov[1].iov_len = uni_path_len;
2520 iov[1].iov_base = utf16_path;
2521 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2523 if (tcon->posix_extensions) {
2524 /* resource #3: posix buf */
2525 rc = add_posix_context(iov, &n_iov, mode);
2528 pc_buf = iov[n_iov-1].iov_base;
2532 memset(&rqst, 0, sizeof(struct smb_rqst));
2534 rqst.rq_nvec = n_iov;
2536 /* no need to inc num_remote_opens because we close it just below */
2537 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2538 FILE_WRITE_ATTRIBUTES);
2539 /* resource #4: response buffer */
2540 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2542 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2543 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2545 FILE_WRITE_ATTRIBUTES, rc);
2546 goto err_free_rsp_buf;
2549 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2550 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2551 ses->Suid, CREATE_NOT_FILE,
2552 FILE_WRITE_ATTRIBUTES);
2554 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2556 /* Eventually save off posix specific response info and timestaps */
2559 free_rsp_buf(resp_buftype, rsp);
2562 cifs_small_buf_release(req);
2569 SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2570 struct cifs_open_parms *oparms, __le16 *path)
2572 struct TCP_Server_Info *server = tcon->ses->server;
2573 struct smb2_create_req *req;
2574 unsigned int n_iov = 2;
2575 __u32 file_attributes = 0;
2578 unsigned int total_len;
2579 struct kvec *iov = rqst->rq_iov;
2583 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2587 iov[0].iov_base = (char *)req;
2588 /* -1 since last byte is buf[0] which is sent below (path) */
2589 iov[0].iov_len = total_len - 1;
2591 if (oparms->create_options & CREATE_OPTION_READONLY)
2592 file_attributes |= ATTR_READONLY;
2593 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2594 file_attributes |= ATTR_SYSTEM;
2596 req->ImpersonationLevel = IL_IMPERSONATION;
2597 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2598 /* File attributes ignored on open (used in create though) */
2599 req->FileAttributes = cpu_to_le32(file_attributes);
2600 req->ShareAccess = FILE_SHARE_ALL_LE;
2602 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2603 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2604 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2606 /* [MS-SMB2] 2.2.13 NameOffset:
2607 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2608 * the SMB2 header, the file name includes a prefix that will
2609 * be processed during DFS name normalization as specified in
2610 * section 3.3.5.9. Otherwise, the file name is relative to
2611 * the share that is identified by the TreeId in the SMB2
2614 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2617 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2618 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2620 tcon->treeName, path);
2623 req->NameLength = cpu_to_le16(name_len * 2);
2624 uni_path_len = copy_size;
2627 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2628 /* MUST set path len (NameLength) to 0 opening root of share */
2629 req->NameLength = cpu_to_le16(uni_path_len - 2);
2630 copy_size = uni_path_len;
2631 if (copy_size % 8 != 0)
2632 copy_size = roundup(copy_size, 8);
2633 copy_path = kzalloc(copy_size, GFP_KERNEL);
2636 memcpy((char *)copy_path, (const char *)path,
2638 uni_path_len = copy_size;
2642 iov[1].iov_len = uni_path_len;
2643 iov[1].iov_base = path;
2645 if ((!server->oplocks) || (tcon->no_lease))
2646 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2648 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2649 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2650 req->RequestedOplockLevel = *oplock;
2651 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2652 (oparms->create_options & CREATE_NOT_FILE))
2653 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2655 rc = add_lease_context(server, iov, &n_iov,
2656 oparms->fid->lease_key, oplock);
2661 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2662 /* need to set Next field of lease context if we request it */
2663 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2664 struct create_context *ccontext =
2665 (struct create_context *)iov[n_iov-1].iov_base;
2667 cpu_to_le32(server->vals->create_lease_size);
2670 rc = add_durable_context(iov, &n_iov, oparms,
2671 tcon->use_persistent);
2676 if (tcon->posix_extensions) {
2678 struct create_context *ccontext =
2679 (struct create_context *)iov[n_iov-1].iov_base;
2681 cpu_to_le32(iov[n_iov-1].iov_len);
2684 rc = add_posix_context(iov, &n_iov, oparms->mode);
2689 if (tcon->snapshot_time) {
2690 cifs_dbg(FYI, "adding snapshot context\n");
2692 struct create_context *ccontext =
2693 (struct create_context *)iov[n_iov-1].iov_base;
2695 cpu_to_le32(iov[n_iov-1].iov_len);
2698 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2703 if ((oparms->disposition != FILE_OPEN) &&
2704 (oparms->cifs_sb) &&
2705 (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
2706 (oparms->mode != ACL_NO_MODE)) {
2708 struct create_context *ccontext =
2709 (struct create_context *)iov[n_iov-1].iov_base;
2711 cpu_to_le32(iov[n_iov-1].iov_len);
2714 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2715 rc = add_sd_context(iov, &n_iov, oparms->mode);
2721 struct create_context *ccontext =
2722 (struct create_context *)iov[n_iov-1].iov_base;
2723 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2725 add_query_id_context(iov, &n_iov);
2727 rqst->rq_nvec = n_iov;
2731 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2732 * All other vectors are freed by kfree().
2735 SMB2_open_free(struct smb_rqst *rqst)
2739 if (rqst && rqst->rq_iov) {
2740 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2741 for (i = 1; i < rqst->rq_nvec; i++)
2742 if (rqst->rq_iov[i].iov_base != smb2_padding)
2743 kfree(rqst->rq_iov[i].iov_base);
2748 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2749 __u8 *oplock, struct smb2_file_all_info *buf,
2750 struct create_posix_rsp *posix,
2751 struct kvec *err_iov, int *buftype)
2753 struct smb_rqst rqst;
2754 struct smb2_create_rsp *rsp = NULL;
2755 struct TCP_Server_Info *server;
2756 struct cifs_tcon *tcon = oparms->tcon;
2757 struct cifs_ses *ses = tcon->ses;
2758 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2759 struct kvec rsp_iov = {NULL, 0};
2760 int resp_buftype = CIFS_NO_BUFFER;
2764 cifs_dbg(FYI, "create/open\n");
2765 if (ses && (ses->server))
2766 server = ses->server;
2770 if (smb3_encryption_required(tcon))
2771 flags |= CIFS_TRANSFORM_REQ;
2773 memset(&rqst, 0, sizeof(struct smb_rqst));
2774 memset(&iov, 0, sizeof(iov));
2776 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2778 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2782 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2783 oparms->create_options, oparms->desired_access);
2785 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2787 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2790 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2791 if (err_iov && rsp) {
2793 *buftype = resp_buftype;
2794 resp_buftype = CIFS_NO_BUFFER;
2797 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2798 oparms->create_options, oparms->desired_access, rc);
2799 if (rc == -EREMCHG) {
2800 printk_once(KERN_WARNING "server share %s deleted\n",
2802 tcon->need_reconnect = true;
2806 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2807 ses->Suid, oparms->create_options,
2808 oparms->desired_access);
2810 atomic_inc(&tcon->num_remote_opens);
2811 oparms->fid->persistent_fid = rsp->PersistentFileId;
2812 oparms->fid->volatile_fid = rsp->VolatileFileId;
2813 oparms->fid->access = oparms->desired_access;
2814 #ifdef CONFIG_CIFS_DEBUG2
2815 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2816 #endif /* CIFS_DEBUG2 */
2819 memcpy(buf, &rsp->CreationTime, 32);
2820 buf->AllocationSize = rsp->AllocationSize;
2821 buf->EndOfFile = rsp->EndofFile;
2822 buf->Attributes = rsp->FileAttributes;
2823 buf->NumberOfLinks = cpu_to_le32(1);
2824 buf->DeletePending = 0;
2828 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2829 oparms->fid->lease_key, oplock, buf, posix);
2831 SMB2_open_free(&rqst);
2832 free_rsp_buf(resp_buftype, rsp);
2837 SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2838 u64 persistent_fid, u64 volatile_fid, u32 opcode,
2839 bool is_fsctl, char *in_data, u32 indatalen,
2840 __u32 max_response_size)
2842 struct smb2_ioctl_req *req;
2843 struct kvec *iov = rqst->rq_iov;
2844 unsigned int total_len;
2848 rc = smb2_ioctl_req_init(opcode, tcon, (void **) &req, &total_len);
2854 * indatalen is usually small at a couple of bytes max, so
2855 * just allocate through generic pool
2857 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
2859 cifs_small_buf_release(req);
2864 req->CtlCode = cpu_to_le32(opcode);
2865 req->PersistentFileId = persistent_fid;
2866 req->VolatileFileId = volatile_fid;
2868 iov[0].iov_base = (char *)req;
2870 * If no input data, the size of ioctl struct in
2871 * protocol spec still includes a 1 byte data buffer,
2872 * but if input data passed to ioctl, we do not
2873 * want to double count this, so we do not send
2874 * the dummy one byte of data in iovec[0] if sending
2875 * input data (in iovec[1]).
2878 req->InputCount = cpu_to_le32(indatalen);
2879 /* do not set InputOffset if no input data */
2881 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
2883 iov[0].iov_len = total_len - 1;
2884 iov[1].iov_base = in_data_buf;
2885 iov[1].iov_len = indatalen;
2888 iov[0].iov_len = total_len;
2891 req->OutputOffset = 0;
2892 req->OutputCount = 0; /* MBZ */
2895 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2896 * We Could increase default MaxOutputResponse, but that could require
2897 * more credits. Windows typically sets this smaller, but for some
2898 * ioctls it may be useful to allow server to send more. No point
2899 * limiting what the server can send as long as fits in one credit
2900 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2901 * to increase this limit up in the future.
2902 * Note that for snapshot queries that servers like Azure expect that
2903 * the first query be minimal size (and just used to get the number/size
2904 * of previous versions) so response size must be specified as EXACTLY
2905 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2906 * of eight bytes. Currently that is the only case where we set max
2907 * response size smaller.
2909 req->MaxOutputResponse = cpu_to_le32(max_response_size);
2912 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2916 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2917 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
2918 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
2924 SMB2_ioctl_free(struct smb_rqst *rqst)
2927 if (rqst && rqst->rq_iov) {
2928 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2929 for (i = 1; i < rqst->rq_nvec; i++)
2930 if (rqst->rq_iov[i].iov_base != smb2_padding)
2931 kfree(rqst->rq_iov[i].iov_base);
2937 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2940 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2941 u64 volatile_fid, u32 opcode, bool is_fsctl,
2942 char *in_data, u32 indatalen, u32 max_out_data_len,
2943 char **out_data, u32 *plen /* returned data len */)
2945 struct smb_rqst rqst;
2946 struct smb2_ioctl_rsp *rsp = NULL;
2947 struct cifs_ses *ses;
2948 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
2949 struct kvec rsp_iov = {NULL, 0};
2950 int resp_buftype = CIFS_NO_BUFFER;
2953 struct TCP_Server_Info *server;
2955 cifs_dbg(FYI, "SMB2 IOCTL\n");
2957 if (out_data != NULL)
2960 /* zero out returned data len, in case of error */
2971 server = ses->server;
2975 if (smb3_encryption_required(tcon))
2976 flags |= CIFS_TRANSFORM_REQ;
2978 memset(&rqst, 0, sizeof(struct smb_rqst));
2979 memset(&iov, 0, sizeof(iov));
2981 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
2983 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
2984 is_fsctl, in_data, indatalen, max_out_data_len);
2988 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2990 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
2993 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2994 ses->Suid, 0, opcode, rc);
2996 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
2997 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2999 } else if (rc == -EINVAL) {
3000 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3001 (opcode != FSCTL_SRV_COPYCHUNK)) {
3002 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3005 } else if (rc == -E2BIG) {
3006 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3007 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3012 /* check if caller wants to look at return data or just return rc */
3013 if ((plen == NULL) || (out_data == NULL))
3016 *plen = le32_to_cpu(rsp->OutputCount);
3018 /* We check for obvious errors in the output buffer length and offset */
3020 goto ioctl_exit; /* server returned no data */
3021 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
3022 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
3028 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
3029 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
3030 le32_to_cpu(rsp->OutputOffset));
3036 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3038 if (*out_data == NULL) {
3044 SMB2_ioctl_free(&rqst);
3045 free_rsp_buf(resp_buftype, rsp);
3050 * Individual callers to ioctl worker function follow
3054 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3055 u64 persistent_fid, u64 volatile_fid)
3058 struct compress_ioctl fsctl_input;
3059 char *ret_data = NULL;
3061 fsctl_input.CompressionState =
3062 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3064 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3065 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
3066 (char *)&fsctl_input /* data input */,
3067 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3068 &ret_data /* out data */, NULL);
3070 cifs_dbg(FYI, "set compression rc %d\n", rc);
3076 SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3077 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
3079 struct smb2_close_req *req;
3080 struct kvec *iov = rqst->rq_iov;
3081 unsigned int total_len;
3084 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
3088 req->PersistentFileId = persistent_fid;
3089 req->VolatileFileId = volatile_fid;
3091 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3094 iov[0].iov_base = (char *)req;
3095 iov[0].iov_len = total_len;
3101 SMB2_close_free(struct smb_rqst *rqst)
3103 if (rqst && rqst->rq_iov)
3104 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3108 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3109 u64 persistent_fid, u64 volatile_fid,
3110 struct smb2_file_network_open_info *pbuf)
3112 struct smb_rqst rqst;
3113 struct smb2_close_rsp *rsp = NULL;
3114 struct cifs_ses *ses = tcon->ses;
3116 struct kvec rsp_iov;
3117 int resp_buftype = CIFS_NO_BUFFER;
3120 bool query_attrs = false;
3122 cifs_dbg(FYI, "Close\n");
3124 if (!ses || !(ses->server))
3127 if (smb3_encryption_required(tcon))
3128 flags |= CIFS_TRANSFORM_REQ;
3130 memset(&rqst, 0, sizeof(struct smb_rqst));
3131 memset(&iov, 0, sizeof(iov));
3135 /* check if need to ask server to return timestamps in close response */
3139 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3140 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid,
3145 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3146 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
3149 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
3150 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3154 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3157 * Note that have to subtract 4 since struct network_open_info
3158 * has a final 4 byte pad that close response does not have
3161 memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3164 atomic_dec(&tcon->num_remote_opens);
3166 SMB2_close_free(&rqst);
3167 free_rsp_buf(resp_buftype, rsp);
3169 /* retry close in a worker thread if this one is interrupted */
3173 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3176 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3177 persistent_fid, tmp_rc);
3183 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3184 u64 persistent_fid, u64 volatile_fid)
3186 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3190 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3191 struct kvec *iov, unsigned int min_buf_size)
3193 unsigned int smb_len = iov->iov_len;
3194 char *end_of_smb = smb_len + (char *)iov->iov_base;
3195 char *begin_of_buf = offset + (char *)iov->iov_base;
3196 char *end_of_buf = begin_of_buf + buffer_length;
3199 if (buffer_length < min_buf_size) {
3200 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3201 buffer_length, min_buf_size);
3205 /* check if beyond RFC1001 maximum length */
3206 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3207 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3208 buffer_length, smb_len);
3212 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3213 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
3221 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3222 * Caller must free buffer.
3225 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3226 struct kvec *iov, unsigned int minbufsize,
3229 char *begin_of_buf = offset + (char *)iov->iov_base;
3235 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3239 memcpy(data, begin_of_buf, buffer_length);
3245 SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3246 u64 persistent_fid, u64 volatile_fid,
3247 u8 info_class, u8 info_type, u32 additional_info,
3248 size_t output_len, size_t input_len, void *input)
3250 struct smb2_query_info_req *req;
3251 struct kvec *iov = rqst->rq_iov;
3252 unsigned int total_len;
3255 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3260 req->InfoType = info_type;
3261 req->FileInfoClass = info_class;
3262 req->PersistentFileId = persistent_fid;
3263 req->VolatileFileId = volatile_fid;
3264 req->AdditionalInformation = cpu_to_le32(additional_info);
3266 req->OutputBufferLength = cpu_to_le32(output_len);
3268 req->InputBufferLength = cpu_to_le32(input_len);
3269 /* total_len for smb query request never close to le16 max */
3270 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3271 memcpy(req->Buffer, input, input_len);
3274 iov[0].iov_base = (char *)req;
3276 iov[0].iov_len = total_len - 1 + input_len;
3281 SMB2_query_info_free(struct smb_rqst *rqst)
3283 if (rqst && rqst->rq_iov)
3284 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3288 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3289 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3290 u32 additional_info, size_t output_len, size_t min_len, void **data,
3293 struct smb_rqst rqst;
3294 struct smb2_query_info_rsp *rsp = NULL;
3296 struct kvec rsp_iov;
3298 int resp_buftype = CIFS_NO_BUFFER;
3299 struct cifs_ses *ses = tcon->ses;
3300 struct TCP_Server_Info *server;
3302 bool allocated = false;
3304 cifs_dbg(FYI, "Query Info\n");
3308 server = ses->server;
3312 if (smb3_encryption_required(tcon))
3313 flags |= CIFS_TRANSFORM_REQ;
3315 memset(&rqst, 0, sizeof(struct smb_rqst));
3316 memset(&iov, 0, sizeof(iov));
3320 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
3321 info_class, info_type, additional_info,
3322 output_len, 0, NULL);
3326 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3327 ses->Suid, info_class, (__u32)info_type);
3329 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3330 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3333 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3334 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3335 ses->Suid, info_class, (__u32)info_type, rc);
3339 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3340 ses->Suid, info_class, (__u32)info_type);
3343 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3345 *data = kmalloc(*dlen, GFP_KERNEL);
3348 "Error %d allocating memory for acl\n",
3358 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3359 le32_to_cpu(rsp->OutputBufferLength),
3360 &rsp_iov, min_len, *data);
3361 if (rc && allocated) {
3368 SMB2_query_info_free(&rqst);
3369 free_rsp_buf(resp_buftype, rsp);
3373 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3374 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3376 return query_info(xid, tcon, persistent_fid, volatile_fid,
3377 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3378 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3379 sizeof(struct smb2_file_all_info), (void **)&data,
3384 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3385 u64 persistent_fid, u64 volatile_fid,
3386 void **data, u32 *plen)
3388 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3391 return query_info(xid, tcon, persistent_fid, volatile_fid,
3392 0, SMB2_O_INFO_SECURITY, additional_info,
3393 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3397 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3398 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3400 return query_info(xid, tcon, persistent_fid, volatile_fid,
3401 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3402 sizeof(struct smb2_file_internal_info),
3403 sizeof(struct smb2_file_internal_info),
3404 (void **)&uniqueid, NULL);
3408 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3409 * See MS-SMB2 2.2.35 and 2.2.36
3413 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3414 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid,
3415 u32 completion_filter, bool watch_tree)
3417 struct smb2_change_notify_req *req;
3418 struct kvec *iov = rqst->rq_iov;
3419 unsigned int total_len;
3422 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, (void **) &req, &total_len);
3426 req->PersistentFileId = persistent_fid;
3427 req->VolatileFileId = volatile_fid;
3428 /* See note 354 of MS-SMB2, 64K max */
3429 req->OutputBufferLength =
3430 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3431 req->CompletionFilter = cpu_to_le32(completion_filter);
3433 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3437 iov[0].iov_base = (char *)req;
3438 iov[0].iov_len = total_len;
3444 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3445 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3446 u32 completion_filter)
3448 struct cifs_ses *ses = tcon->ses;
3449 struct smb_rqst rqst;
3451 struct kvec rsp_iov = {NULL, 0};
3452 int resp_buftype = CIFS_NO_BUFFER;
3456 cifs_dbg(FYI, "change notify\n");
3457 if (!ses || !(ses->server))
3460 if (smb3_encryption_required(tcon))
3461 flags |= CIFS_TRANSFORM_REQ;
3463 memset(&rqst, 0, sizeof(struct smb_rqst));
3464 memset(&iov, 0, sizeof(iov));
3468 rc = SMB2_notify_init(xid, &rqst, tcon, persistent_fid, volatile_fid,
3469 completion_filter, watch_tree);
3473 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3474 (u8)watch_tree, completion_filter);
3475 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3478 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3479 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3480 (u8)watch_tree, completion_filter, rc);
3482 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3483 ses->Suid, (u8)watch_tree, completion_filter);
3487 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3488 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3495 * This is a no-op for now. We're not really interested in the reply, but
3496 * rather in the fact that the server sent one and that server->lstrp
3499 * FIXME: maybe we should consider checking that the reply matches request?
3502 smb2_echo_callback(struct mid_q_entry *mid)
3504 struct TCP_Server_Info *server = mid->callback_data;
3505 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3506 struct cifs_credits credits = { .value = 0, .instance = 0 };
3508 if (mid->mid_state == MID_RESPONSE_RECEIVED
3509 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3510 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3511 credits.instance = server->reconnect_instance;
3514 DeleteMidQEntry(mid);
3515 add_credits(server, &credits, CIFS_ECHO_OP);
3518 void smb2_reconnect_server(struct work_struct *work)
3520 struct TCP_Server_Info *server = container_of(work,
3521 struct TCP_Server_Info, reconnect.work);
3522 struct cifs_ses *ses;
3523 struct cifs_tcon *tcon, *tcon2;
3524 struct list_head tmp_list;
3525 int tcon_exist = false;
3527 int resched = false;
3530 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3531 mutex_lock(&server->reconnect_mutex);
3533 INIT_LIST_HEAD(&tmp_list);
3534 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3536 spin_lock(&cifs_tcp_ses_lock);
3537 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3538 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3539 if (tcon->need_reconnect || tcon->need_reopen_files) {
3541 list_add_tail(&tcon->rlist, &tmp_list);
3546 * IPC has the same lifetime as its session and uses its
3549 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3550 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3556 * Get the reference to server struct to be sure that the last call of
3557 * cifs_put_tcon() in the loop below won't release the server pointer.
3560 server->srv_count++;
3562 spin_unlock(&cifs_tcp_ses_lock);
3564 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3565 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3567 cifs_reopen_persistent_handles(tcon);
3570 list_del_init(&tcon->rlist);
3572 cifs_put_smb_ses(tcon->ses);
3574 cifs_put_tcon(tcon);
3577 cifs_dbg(FYI, "Reconnecting tcons finished\n");
3579 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3580 mutex_unlock(&server->reconnect_mutex);
3582 /* now we can safely release srv struct */
3584 cifs_put_tcp_session(server, 1);
3588 SMB2_echo(struct TCP_Server_Info *server)
3590 struct smb2_echo_req *req;
3593 struct smb_rqst rqst = { .rq_iov = iov,
3595 unsigned int total_len;
3597 cifs_dbg(FYI, "In echo request\n");
3599 if (server->tcpStatus == CifsNeedNegotiate) {
3600 /* No need to send echo on newly established connections */
3601 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
3605 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
3609 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3611 iov[0].iov_len = total_len;
3612 iov[0].iov_base = (char *)req;
3614 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3615 server, CIFS_ECHO_OP, NULL);
3617 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3619 cifs_small_buf_release(req);
3624 SMB2_flush_free(struct smb_rqst *rqst)
3626 if (rqst && rqst->rq_iov)
3627 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3631 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3632 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid)
3634 struct smb2_flush_req *req;
3635 struct kvec *iov = rqst->rq_iov;
3636 unsigned int total_len;
3639 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
3643 req->PersistentFileId = persistent_fid;
3644 req->VolatileFileId = volatile_fid;
3646 iov[0].iov_base = (char *)req;
3647 iov[0].iov_len = total_len;
3653 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3656 struct cifs_ses *ses = tcon->ses;
3657 struct smb_rqst rqst;
3659 struct kvec rsp_iov = {NULL, 0};
3660 int resp_buftype = CIFS_NO_BUFFER;
3664 cifs_dbg(FYI, "flush\n");
3665 if (!ses || !(ses->server))
3668 if (smb3_encryption_required(tcon))
3669 flags |= CIFS_TRANSFORM_REQ;
3671 memset(&rqst, 0, sizeof(struct smb_rqst));
3672 memset(&iov, 0, sizeof(iov));
3676 rc = SMB2_flush_init(xid, &rqst, tcon, persistent_fid, volatile_fid);
3680 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3681 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3684 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
3685 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3688 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3692 SMB2_flush_free(&rqst);
3693 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3698 * To form a chain of read requests, any read requests after the first should
3699 * have the end_of_chain boolean set to true.
3702 smb2_new_read_req(void **buf, unsigned int *total_len,
3703 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3704 unsigned int remaining_bytes, int request_type)
3707 struct smb2_read_plain_req *req = NULL;
3708 struct smb2_sync_hdr *shdr;
3709 struct TCP_Server_Info *server;
3711 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3716 server = io_parms->tcon->ses->server;
3718 return -ECONNABORTED;
3720 shdr = &req->sync_hdr;
3721 shdr->ProcessId = cpu_to_le32(io_parms->pid);
3723 req->PersistentFileId = io_parms->persistent_fid;
3724 req->VolatileFileId = io_parms->volatile_fid;
3725 req->ReadChannelInfoOffset = 0; /* reserved */
3726 req->ReadChannelInfoLength = 0; /* reserved */
3727 req->Channel = 0; /* reserved */
3728 req->MinimumCount = 0;
3729 req->Length = cpu_to_le32(io_parms->length);
3730 req->Offset = cpu_to_le64(io_parms->offset);
3732 trace_smb3_read_enter(0 /* xid */,
3733 io_parms->persistent_fid,
3734 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3735 io_parms->offset, io_parms->length);
3736 #ifdef CONFIG_CIFS_SMB_DIRECT
3738 * If we want to do a RDMA write, fill in and append
3739 * smbd_buffer_descriptor_v1 to the end of read request
3741 if (server->rdma && rdata && !server->sign &&
3742 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
3744 struct smbd_buffer_descriptor_v1 *v1;
3745 bool need_invalidate =
3746 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3748 rdata->mr = smbd_register_mr(
3749 server->smbd_conn, rdata->pages,
3750 rdata->nr_pages, rdata->page_offset,
3751 rdata->tailsz, true, need_invalidate);
3755 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3756 if (need_invalidate)
3757 req->Channel = SMB2_CHANNEL_RDMA_V1;
3758 req->ReadChannelInfoOffset =
3759 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
3760 req->ReadChannelInfoLength =
3761 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3762 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3763 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3764 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3765 v1->length = cpu_to_le32(rdata->mr->mr->length);
3767 *total_len += sizeof(*v1) - 1;
3770 if (request_type & CHAINED_REQUEST) {
3771 if (!(request_type & END_OF_CHAIN)) {
3772 /* next 8-byte aligned request */
3773 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3774 shdr->NextCommand = cpu_to_le32(*total_len);
3775 } else /* END_OF_CHAIN */
3776 shdr->NextCommand = 0;
3777 if (request_type & RELATED_REQUEST) {
3778 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
3780 * Related requests use info from previous read request
3783 shdr->SessionId = 0xFFFFFFFF;
3784 shdr->TreeId = 0xFFFFFFFF;
3785 req->PersistentFileId = 0xFFFFFFFF;
3786 req->VolatileFileId = 0xFFFFFFFF;
3789 if (remaining_bytes > io_parms->length)
3790 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3792 req->RemainingBytes = 0;
3799 smb2_readv_callback(struct mid_q_entry *mid)
3801 struct cifs_readdata *rdata = mid->callback_data;
3802 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3803 struct TCP_Server_Info *server = tcon->ses->server;
3804 struct smb2_sync_hdr *shdr =
3805 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
3806 struct cifs_credits credits = { .value = 0, .instance = 0 };
3807 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
3809 .rq_pages = rdata->pages,
3810 .rq_offset = rdata->page_offset,
3811 .rq_npages = rdata->nr_pages,
3812 .rq_pagesz = rdata->pagesz,
3813 .rq_tailsz = rdata->tailsz };
3815 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3816 __func__, mid->mid, mid->mid_state, rdata->result,
3819 switch (mid->mid_state) {
3820 case MID_RESPONSE_RECEIVED:
3821 credits.value = le16_to_cpu(shdr->CreditRequest);
3822 credits.instance = server->reconnect_instance;
3823 /* result already set, check signature */
3824 if (server->sign && !mid->decrypted) {
3827 rc = smb2_verify_signature(&rqst, server);
3829 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
3832 /* FIXME: should this be counted toward the initiating task? */
3833 task_io_account_read(rdata->got_bytes);
3834 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3836 case MID_REQUEST_SUBMITTED:
3837 case MID_RETRY_NEEDED:
3838 rdata->result = -EAGAIN;
3839 if (server->sign && rdata->got_bytes)
3840 /* reset bytes number since we can not check a sign */
3841 rdata->got_bytes = 0;
3842 /* FIXME: should this be counted toward the initiating task? */
3843 task_io_account_read(rdata->got_bytes);
3844 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3846 case MID_RESPONSE_MALFORMED:
3847 credits.value = le16_to_cpu(shdr->CreditRequest);
3848 credits.instance = server->reconnect_instance;
3851 rdata->result = -EIO;
3853 #ifdef CONFIG_CIFS_SMB_DIRECT
3855 * If this rdata has a memmory registered, the MR can be freed
3856 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3857 * because they have limited number and are used for future I/Os
3860 smbd_deregister_mr(rdata->mr);
3864 if (rdata->result && rdata->result != -ENODATA) {
3865 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
3866 trace_smb3_read_err(0 /* xid */,
3867 rdata->cfile->fid.persistent_fid,
3868 tcon->tid, tcon->ses->Suid, rdata->offset,
3869 rdata->bytes, rdata->result);
3871 trace_smb3_read_done(0 /* xid */,
3872 rdata->cfile->fid.persistent_fid,
3873 tcon->tid, tcon->ses->Suid,
3874 rdata->offset, rdata->got_bytes);
3876 queue_work(cifsiod_wq, &rdata->work);
3877 DeleteMidQEntry(mid);
3878 add_credits(server, &credits, 0);
3881 /* smb2_async_readv - send an async read, and set up mid to handle result */
3883 smb2_async_readv(struct cifs_readdata *rdata)
3887 struct smb2_sync_hdr *shdr;
3888 struct cifs_io_parms io_parms;
3889 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3891 struct TCP_Server_Info *server;
3892 unsigned int total_len;
3894 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3895 __func__, rdata->offset, rdata->bytes);
3897 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3898 io_parms.offset = rdata->offset;
3899 io_parms.length = rdata->bytes;
3900 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3901 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3902 io_parms.pid = rdata->pid;
3904 server = io_parms.tcon->ses->server;
3906 rc = smb2_new_read_req(
3907 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
3911 if (smb3_encryption_required(io_parms.tcon))
3912 flags |= CIFS_TRANSFORM_REQ;
3914 rdata->iov[0].iov_base = buf;
3915 rdata->iov[0].iov_len = total_len;
3917 shdr = (struct smb2_sync_hdr *)buf;
3919 if (rdata->credits.value > 0) {
3920 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
3921 SMB2_MAX_BUFFER_SIZE));
3922 shdr->CreditRequest =
3923 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
3925 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3927 goto async_readv_out;
3929 flags |= CIFS_HAS_CREDITS;
3932 kref_get(&rdata->refcount);
3933 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
3934 cifs_readv_receive, smb2_readv_callback,
3935 smb3_handle_read_data, rdata, flags,
3938 kref_put(&rdata->refcount, cifs_readdata_release);
3939 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
3940 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3942 io_parms.tcon->ses->Suid,
3943 io_parms.offset, io_parms.length, rc);
3947 cifs_small_buf_release(buf);
3952 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3953 unsigned int *nbytes, char **buf, int *buf_type)
3955 struct smb_rqst rqst;
3956 int resp_buftype, rc;
3957 struct smb2_read_plain_req *req = NULL;
3958 struct smb2_read_rsp *rsp = NULL;
3960 struct kvec rsp_iov;
3961 unsigned int total_len;
3962 int flags = CIFS_LOG_ERROR;
3963 struct cifs_ses *ses = io_parms->tcon->ses;
3966 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
3970 if (smb3_encryption_required(io_parms->tcon))
3971 flags |= CIFS_TRANSFORM_REQ;
3973 iov[0].iov_base = (char *)req;
3974 iov[0].iov_len = total_len;
3976 memset(&rqst, 0, sizeof(struct smb_rqst));
3980 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3981 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
3984 if (rc != -ENODATA) {
3985 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3986 cifs_dbg(VFS, "Send error in read = %d\n", rc);
3987 trace_smb3_read_err(xid, req->PersistentFileId,
3988 io_parms->tcon->tid, ses->Suid,
3989 io_parms->offset, io_parms->length,
3992 trace_smb3_read_done(xid, req->PersistentFileId,
3993 io_parms->tcon->tid, ses->Suid,
3994 io_parms->offset, 0);
3995 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3996 cifs_small_buf_release(req);
3997 return rc == -ENODATA ? 0 : rc;
3999 trace_smb3_read_done(xid, req->PersistentFileId,
4000 io_parms->tcon->tid, ses->Suid,
4001 io_parms->offset, io_parms->length);
4003 cifs_small_buf_release(req);
4005 *nbytes = le32_to_cpu(rsp->DataLength);
4006 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4007 (*nbytes > io_parms->length)) {
4008 cifs_dbg(FYI, "bad length %d for count %d\n",
4009 *nbytes, io_parms->length);
4015 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
4016 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4017 } else if (resp_buftype != CIFS_NO_BUFFER) {
4018 *buf = rsp_iov.iov_base;
4019 if (resp_buftype == CIFS_SMALL_BUFFER)
4020 *buf_type = CIFS_SMALL_BUFFER;
4021 else if (resp_buftype == CIFS_LARGE_BUFFER)
4022 *buf_type = CIFS_LARGE_BUFFER;
4028 * Check the mid_state and signature on received buffer (if any), and queue the
4029 * workqueue completion task.
4032 smb2_writev_callback(struct mid_q_entry *mid)
4034 struct cifs_writedata *wdata = mid->callback_data;
4035 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4036 struct TCP_Server_Info *server = tcon->ses->server;
4037 unsigned int written;
4038 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
4039 struct cifs_credits credits = { .value = 0, .instance = 0 };
4041 switch (mid->mid_state) {
4042 case MID_RESPONSE_RECEIVED:
4043 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4044 credits.instance = server->reconnect_instance;
4045 wdata->result = smb2_check_receive(mid, server, 0);
4046 if (wdata->result != 0)
4049 written = le32_to_cpu(rsp->DataLength);
4051 * Mask off high 16 bits when bytes written as returned
4052 * by the server is greater than bytes requested by the
4053 * client. OS/2 servers are known to set incorrect
4056 if (written > wdata->bytes)
4059 if (written < wdata->bytes)
4060 wdata->result = -ENOSPC;
4062 wdata->bytes = written;
4064 case MID_REQUEST_SUBMITTED:
4065 case MID_RETRY_NEEDED:
4066 wdata->result = -EAGAIN;
4068 case MID_RESPONSE_MALFORMED:
4069 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4070 credits.instance = server->reconnect_instance;
4073 wdata->result = -EIO;
4076 #ifdef CONFIG_CIFS_SMB_DIRECT
4078 * If this wdata has a memory registered, the MR can be freed
4079 * The number of MRs available is limited, it's important to recover
4080 * used MR as soon as I/O is finished. Hold MR longer in the later
4081 * I/O process can possibly result in I/O deadlock due to lack of MR
4082 * to send request on I/O retry
4085 smbd_deregister_mr(wdata->mr);
4089 if (wdata->result) {
4090 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4091 trace_smb3_write_err(0 /* no xid */,
4092 wdata->cfile->fid.persistent_fid,
4093 tcon->tid, tcon->ses->Suid, wdata->offset,
4094 wdata->bytes, wdata->result);
4095 if (wdata->result == -ENOSPC)
4096 printk_once(KERN_WARNING "Out of space writing to %s\n",
4099 trace_smb3_write_done(0 /* no xid */,
4100 wdata->cfile->fid.persistent_fid,
4101 tcon->tid, tcon->ses->Suid,
4102 wdata->offset, wdata->bytes);
4104 queue_work(cifsiod_wq, &wdata->work);
4105 DeleteMidQEntry(mid);
4106 add_credits(server, &credits, 0);
4109 /* smb2_async_writev - send an async write, and set up mid to handle result */
4111 smb2_async_writev(struct cifs_writedata *wdata,
4112 void (*release)(struct kref *kref))
4114 int rc = -EACCES, flags = 0;
4115 struct smb2_write_req *req = NULL;
4116 struct smb2_sync_hdr *shdr;
4117 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4118 struct TCP_Server_Info *server = tcon->ses->server;
4120 struct smb_rqst rqst = { };
4121 unsigned int total_len;
4123 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
4127 if (smb3_encryption_required(tcon))
4128 flags |= CIFS_TRANSFORM_REQ;
4130 shdr = (struct smb2_sync_hdr *)req;
4131 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
4133 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
4134 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
4135 req->WriteChannelInfoOffset = 0;
4136 req->WriteChannelInfoLength = 0;
4138 req->Offset = cpu_to_le64(wdata->offset);
4139 req->DataOffset = cpu_to_le16(
4140 offsetof(struct smb2_write_req, Buffer));
4141 req->RemainingBytes = 0;
4143 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
4144 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
4145 #ifdef CONFIG_CIFS_SMB_DIRECT
4147 * If we want to do a server RDMA read, fill in and append
4148 * smbd_buffer_descriptor_v1 to the end of write request
4150 if (server->rdma && !server->sign && wdata->bytes >=
4151 server->smbd_conn->rdma_readwrite_threshold) {
4153 struct smbd_buffer_descriptor_v1 *v1;
4154 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4156 wdata->mr = smbd_register_mr(
4157 server->smbd_conn, wdata->pages,
4158 wdata->nr_pages, wdata->page_offset,
4159 wdata->tailsz, false, need_invalidate);
4162 goto async_writev_out;
4165 req->DataOffset = 0;
4166 if (wdata->nr_pages > 1)
4167 req->RemainingBytes =
4169 (wdata->nr_pages - 1) * wdata->pagesz -
4170 wdata->page_offset + wdata->tailsz
4173 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
4174 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4175 if (need_invalidate)
4176 req->Channel = SMB2_CHANNEL_RDMA_V1;
4177 req->WriteChannelInfoOffset =
4178 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4179 req->WriteChannelInfoLength =
4180 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4181 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4182 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4183 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4184 v1->length = cpu_to_le32(wdata->mr->mr->length);
4187 iov[0].iov_len = total_len - 1;
4188 iov[0].iov_base = (char *)req;
4192 rqst.rq_pages = wdata->pages;
4193 rqst.rq_offset = wdata->page_offset;
4194 rqst.rq_npages = wdata->nr_pages;
4195 rqst.rq_pagesz = wdata->pagesz;
4196 rqst.rq_tailsz = wdata->tailsz;
4197 #ifdef CONFIG_CIFS_SMB_DIRECT
4199 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4203 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4204 wdata->offset, wdata->bytes);
4206 #ifdef CONFIG_CIFS_SMB_DIRECT
4207 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4209 req->Length = cpu_to_le32(wdata->bytes);
4211 req->Length = cpu_to_le32(wdata->bytes);
4214 if (wdata->credits.value > 0) {
4215 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4216 SMB2_MAX_BUFFER_SIZE));
4217 shdr->CreditRequest =
4218 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
4220 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4222 goto async_writev_out;
4224 flags |= CIFS_HAS_CREDITS;
4227 kref_get(&wdata->refcount);
4228 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4229 wdata, flags, &wdata->credits);
4232 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
4233 tcon->tid, tcon->ses->Suid, wdata->offset,
4235 kref_put(&wdata->refcount, release);
4236 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4240 cifs_small_buf_release(req);
4245 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4246 * The length field from io_parms must be at least 1 and indicates a number of
4247 * elements with data to write that begins with position 1 in iov array. All
4248 * data length is specified by count.
4251 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4252 unsigned int *nbytes, struct kvec *iov, int n_vec)
4254 struct smb_rqst rqst;
4256 struct smb2_write_req *req = NULL;
4257 struct smb2_write_rsp *rsp = NULL;
4259 struct kvec rsp_iov;
4261 unsigned int total_len;
4268 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
4273 if (io_parms->tcon->ses->server == NULL)
4274 return -ECONNABORTED;
4276 if (smb3_encryption_required(io_parms->tcon))
4277 flags |= CIFS_TRANSFORM_REQ;
4279 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
4281 req->PersistentFileId = io_parms->persistent_fid;
4282 req->VolatileFileId = io_parms->volatile_fid;
4283 req->WriteChannelInfoOffset = 0;
4284 req->WriteChannelInfoLength = 0;
4286 req->Length = cpu_to_le32(io_parms->length);
4287 req->Offset = cpu_to_le64(io_parms->offset);
4288 req->DataOffset = cpu_to_le16(
4289 offsetof(struct smb2_write_req, Buffer));
4290 req->RemainingBytes = 0;
4292 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4293 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4294 io_parms->offset, io_parms->length);
4296 iov[0].iov_base = (char *)req;
4298 iov[0].iov_len = total_len - 1;
4300 memset(&rqst, 0, sizeof(struct smb_rqst));
4302 rqst.rq_nvec = n_vec + 1;
4304 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
4305 &resp_buftype, flags, &rsp_iov);
4306 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
4309 trace_smb3_write_err(xid, req->PersistentFileId,
4310 io_parms->tcon->tid,
4311 io_parms->tcon->ses->Suid,
4312 io_parms->offset, io_parms->length, rc);
4313 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
4314 cifs_dbg(VFS, "Send error in write = %d\n", rc);
4316 *nbytes = le32_to_cpu(rsp->DataLength);
4317 trace_smb3_write_done(xid, req->PersistentFileId,
4318 io_parms->tcon->tid,
4319 io_parms->tcon->ses->Suid,
4320 io_parms->offset, *nbytes);
4323 cifs_small_buf_release(req);
4324 free_rsp_buf(resp_buftype, rsp);
4328 int posix_info_sid_size(const void *beg, const void *end)
4336 subauth = *(u8 *)(beg+1);
4337 if (subauth < 1 || subauth > 15)
4340 total = 1 + 1 + 6 + 4*subauth;
4341 if (beg + total > end)
4347 int posix_info_parse(const void *beg, const void *end,
4348 struct smb2_posix_info_parsed *out)
4354 const void *owner_sid;
4355 const void *group_sid;
4358 /* if no end bound given, assume payload to be correct */
4360 const struct smb2_posix_info *p = beg;
4362 end = beg + le32_to_cpu(p->NextEntryOffset);
4363 /* last element will have a 0 offset, pick a sensible bound */
4368 /* check base buf */
4369 if (beg + sizeof(struct smb2_posix_info) > end)
4371 total_len = sizeof(struct smb2_posix_info);
4373 /* check owner sid */
4374 owner_sid = beg + total_len;
4375 sid_len = posix_info_sid_size(owner_sid, end);
4378 total_len += sid_len;
4380 /* check group sid */
4381 group_sid = beg + total_len;
4382 sid_len = posix_info_sid_size(group_sid, end);
4385 total_len += sid_len;
4387 /* check name len */
4388 if (beg + total_len + 4 > end)
4390 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
4391 if (name_len < 1 || name_len > 0xFFFF)
4396 name = beg + total_len;
4397 if (name + name_len > end)
4399 total_len += name_len;
4403 out->size = total_len;
4404 out->name_len = name_len;
4406 memcpy(&out->owner, owner_sid,
4407 posix_info_sid_size(owner_sid, end));
4408 memcpy(&out->group, group_sid,
4409 posix_info_sid_size(group_sid, end));
4414 static int posix_info_extra_size(const void *beg, const void *end)
4416 int len = posix_info_parse(beg, end, NULL);
4420 return len - sizeof(struct smb2_posix_info);
4424 num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
4428 unsigned int entrycount = 0;
4429 unsigned int next_offset = 0;
4431 FILE_DIRECTORY_INFO *dir_info;
4433 if (bufstart == NULL)
4436 entryptr = bufstart;
4439 if (entryptr + next_offset < entryptr ||
4440 entryptr + next_offset > end_of_buf ||
4441 entryptr + next_offset + size > end_of_buf) {
4442 cifs_dbg(VFS, "malformed search entry would overflow\n");
4446 entryptr = entryptr + next_offset;
4447 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4449 if (infotype == SMB_FIND_FILE_POSIX_INFO)
4450 len = posix_info_extra_size(entryptr, end_of_buf);
4452 len = le32_to_cpu(dir_info->FileNameLength);
4455 entryptr + len < entryptr ||
4456 entryptr + len > end_of_buf ||
4457 entryptr + len + size > end_of_buf) {
4458 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4463 *lastentry = entryptr;
4466 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
4477 int SMB2_query_directory_init(const unsigned int xid,
4478 struct cifs_tcon *tcon, struct smb_rqst *rqst,
4479 u64 persistent_fid, u64 volatile_fid,
4480 int index, int info_level)
4482 struct TCP_Server_Info *server = tcon->ses->server;
4483 struct smb2_query_directory_req *req;
4484 unsigned char *bufptr;
4485 __le16 asteriks = cpu_to_le16('*');
4486 unsigned int output_size = CIFSMaxBufSize -
4487 MAX_SMB2_CREATE_RESPONSE_SIZE -
4488 MAX_SMB2_CLOSE_RESPONSE_SIZE;
4489 unsigned int total_len;
4490 struct kvec *iov = rqst->rq_iov;
4493 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
4498 switch (info_level) {
4499 case SMB_FIND_FILE_DIRECTORY_INFO:
4500 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4502 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4503 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4505 case SMB_FIND_FILE_POSIX_INFO:
4506 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
4509 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4514 req->FileIndex = cpu_to_le32(index);
4515 req->PersistentFileId = persistent_fid;
4516 req->VolatileFileId = volatile_fid;
4519 bufptr = req->Buffer;
4520 memcpy(bufptr, &asteriks, len);
4522 req->FileNameOffset =
4523 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
4524 req->FileNameLength = cpu_to_le16(len);
4526 * BB could be 30 bytes or so longer if we used SMB2 specific
4527 * buffer lengths, but this is safe and close enough.
4529 output_size = min_t(unsigned int, output_size, server->maxBuf);
4530 output_size = min_t(unsigned int, output_size, 2 << 15);
4531 req->OutputBufferLength = cpu_to_le32(output_size);
4533 iov[0].iov_base = (char *)req;
4535 iov[0].iov_len = total_len - 1;
4537 iov[1].iov_base = (char *)(req->Buffer);
4538 iov[1].iov_len = len;
4540 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4541 tcon->ses->Suid, index, output_size);
4546 void SMB2_query_directory_free(struct smb_rqst *rqst)
4548 if (rqst && rqst->rq_iov) {
4549 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4554 smb2_parse_query_directory(struct cifs_tcon *tcon,
4555 struct kvec *rsp_iov,
4557 struct cifs_search_info *srch_inf)
4559 struct smb2_query_directory_rsp *rsp;
4560 size_t info_buf_size;
4564 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
4566 switch (srch_inf->info_level) {
4567 case SMB_FIND_FILE_DIRECTORY_INFO:
4568 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4570 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4571 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4573 case SMB_FIND_FILE_POSIX_INFO:
4574 /* note that posix payload are variable size */
4575 info_buf_size = sizeof(struct smb2_posix_info);
4578 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4579 srch_inf->info_level);
4583 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4584 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
4587 cifs_tcon_dbg(VFS, "bad info payload");
4591 srch_inf->unicode = true;
4593 if (srch_inf->ntwrk_buf_start) {
4594 if (srch_inf->smallBuf)
4595 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4597 cifs_buf_release(srch_inf->ntwrk_buf_start);
4599 srch_inf->ntwrk_buf_start = (char *)rsp;
4600 srch_inf->srch_entries_start = srch_inf->last_entry =
4601 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4602 end_of_smb = rsp_iov->iov_len + (char *)rsp;
4604 srch_inf->entries_in_buffer = num_entries(
4605 srch_inf->info_level,
4606 srch_inf->srch_entries_start,
4608 &srch_inf->last_entry,
4611 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4612 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4613 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4614 srch_inf->srch_entries_start, srch_inf->last_entry);
4615 if (resp_buftype == CIFS_LARGE_BUFFER)
4616 srch_inf->smallBuf = false;
4617 else if (resp_buftype == CIFS_SMALL_BUFFER)
4618 srch_inf->smallBuf = true;
4620 cifs_tcon_dbg(VFS, "illegal search buffer type\n");
4626 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4627 u64 persistent_fid, u64 volatile_fid, int index,
4628 struct cifs_search_info *srch_inf)
4630 struct smb_rqst rqst;
4631 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
4632 struct smb2_query_directory_rsp *rsp = NULL;
4633 int resp_buftype = CIFS_NO_BUFFER;
4634 struct kvec rsp_iov;
4636 struct cifs_ses *ses = tcon->ses;
4639 if (!ses || !(ses->server))
4642 if (smb3_encryption_required(tcon))
4643 flags |= CIFS_TRANSFORM_REQ;
4645 memset(&rqst, 0, sizeof(struct smb_rqst));
4646 memset(&iov, 0, sizeof(iov));
4648 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
4650 rc = SMB2_query_directory_init(xid, tcon, &rqst, persistent_fid,
4651 volatile_fid, index,
4652 srch_inf->info_level);
4656 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4657 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
4660 if (rc == -ENODATA &&
4661 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
4662 trace_smb3_query_dir_done(xid, persistent_fid,
4663 tcon->tid, tcon->ses->Suid, index, 0);
4664 srch_inf->endOfSearch = true;
4667 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4668 tcon->ses->Suid, index, 0, rc);
4669 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
4674 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
4677 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4678 tcon->ses->Suid, index, 0, rc);
4681 resp_buftype = CIFS_NO_BUFFER;
4683 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4684 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
4687 SMB2_query_directory_free(&rqst);
4688 free_rsp_buf(resp_buftype, rsp);
4693 SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
4694 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4695 u8 info_type, u32 additional_info,
4696 void **data, unsigned int *size)
4698 struct smb2_set_info_req *req;
4699 struct kvec *iov = rqst->rq_iov;
4700 unsigned int i, total_len;
4703 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
4707 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4708 req->InfoType = info_type;
4709 req->FileInfoClass = info_class;
4710 req->PersistentFileId = persistent_fid;
4711 req->VolatileFileId = volatile_fid;
4712 req->AdditionalInformation = cpu_to_le32(additional_info);
4715 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
4716 req->BufferLength = cpu_to_le32(*size);
4718 memcpy(req->Buffer, *data, *size);
4721 iov[0].iov_base = (char *)req;
4723 iov[0].iov_len = total_len - 1;
4725 for (i = 1; i < rqst->rq_nvec; i++) {
4726 le32_add_cpu(&req->BufferLength, size[i]);
4727 iov[i].iov_base = (char *)data[i];
4728 iov[i].iov_len = size[i];
4735 SMB2_set_info_free(struct smb_rqst *rqst)
4737 if (rqst && rqst->rq_iov)
4738 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
4742 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4743 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4744 u8 info_type, u32 additional_info, unsigned int num,
4745 void **data, unsigned int *size)
4747 struct smb_rqst rqst;
4748 struct smb2_set_info_rsp *rsp = NULL;
4750 struct kvec rsp_iov;
4753 struct cifs_ses *ses = tcon->ses;
4756 if (!ses || !(ses->server))
4762 if (smb3_encryption_required(tcon))
4763 flags |= CIFS_TRANSFORM_REQ;
4765 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4769 memset(&rqst, 0, sizeof(struct smb_rqst));
4773 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4774 info_class, info_type, additional_info,
4782 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
4784 SMB2_set_info_free(&rqst);
4785 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
4788 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
4789 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4790 ses->Suid, info_class, (__u32)info_type, rc);
4793 free_rsp_buf(resp_buftype, rsp);
4799 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4800 u64 volatile_fid, u32 pid, __le64 *eof)
4802 struct smb2_file_eof_info info;
4806 info.EndOfFile = *eof;
4809 size = sizeof(struct smb2_file_eof_info);
4811 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4812 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4813 0, 1, &data, &size);
4817 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4818 u64 persistent_fid, u64 volatile_fid,
4819 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4821 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4822 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4823 1, (void **)&pnntsd, &pacllen);
4827 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4828 u64 persistent_fid, u64 volatile_fid,
4829 struct smb2_file_full_ea_info *buf, int len)
4831 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4832 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4833 0, 1, (void **)&buf, &len);
4837 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4838 const u64 persistent_fid, const u64 volatile_fid,
4841 struct smb_rqst rqst;
4843 struct smb2_oplock_break *req = NULL;
4844 struct cifs_ses *ses = tcon->ses;
4845 int flags = CIFS_OBREAK_OP;
4846 unsigned int total_len;
4848 struct kvec rsp_iov;
4851 cifs_dbg(FYI, "SMB2_oplock_break\n");
4852 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4857 if (smb3_encryption_required(tcon))
4858 flags |= CIFS_TRANSFORM_REQ;
4860 req->VolatileFid = volatile_fid;
4861 req->PersistentFid = persistent_fid;
4862 req->OplockLevel = oplock_level;
4863 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4865 flags |= CIFS_NO_RSP_BUF;
4867 iov[0].iov_base = (char *)req;
4868 iov[0].iov_len = total_len;
4870 memset(&rqst, 0, sizeof(struct smb_rqst));
4874 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4875 cifs_small_buf_release(req);
4878 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4879 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
4886 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4887 struct kstatfs *kst)
4889 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4890 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4891 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
4892 kst->f_bfree = kst->f_bavail =
4893 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
4898 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4899 struct kstatfs *kst)
4901 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4902 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4903 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4904 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4905 kst->f_bavail = kst->f_bfree;
4907 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4908 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4909 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4910 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4911 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4917 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4918 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4921 struct smb2_query_info_req *req;
4922 unsigned int total_len;
4924 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
4926 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4929 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4934 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4935 req->FileInfoClass = level;
4936 req->PersistentFileId = persistent_fid;
4937 req->VolatileFileId = volatile_fid;
4939 req->InputBufferOffset =
4940 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
4941 req->OutputBufferLength = cpu_to_le32(
4942 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
4944 iov->iov_base = (char *)req;
4945 iov->iov_len = total_len;
4950 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4951 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4953 struct smb_rqst rqst;
4954 struct smb2_query_info_rsp *rsp = NULL;
4956 struct kvec rsp_iov;
4959 struct cifs_ses *ses = tcon->ses;
4960 FILE_SYSTEM_POSIX_INFO *info = NULL;
4963 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4964 sizeof(FILE_SYSTEM_POSIX_INFO),
4965 persistent_fid, volatile_fid);
4969 if (smb3_encryption_required(tcon))
4970 flags |= CIFS_TRANSFORM_REQ;
4972 memset(&rqst, 0, sizeof(struct smb_rqst));
4976 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4977 cifs_small_buf_release(iov.iov_base);
4979 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4980 goto posix_qfsinf_exit;
4982 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4984 info = (FILE_SYSTEM_POSIX_INFO *)(
4985 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4986 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4987 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4988 sizeof(FILE_SYSTEM_POSIX_INFO));
4990 copy_posix_fs_info_to_kstatfs(info, fsdata);
4993 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4998 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4999 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5001 struct smb_rqst rqst;
5002 struct smb2_query_info_rsp *rsp = NULL;
5004 struct kvec rsp_iov;
5007 struct cifs_ses *ses = tcon->ses;
5008 struct smb2_fs_full_size_info *info = NULL;
5011 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
5012 sizeof(struct smb2_fs_full_size_info),
5013 persistent_fid, volatile_fid);
5017 if (smb3_encryption_required(tcon))
5018 flags |= CIFS_TRANSFORM_REQ;
5020 memset(&rqst, 0, sizeof(struct smb_rqst));
5024 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
5025 cifs_small_buf_release(iov.iov_base);
5027 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5030 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5032 info = (struct smb2_fs_full_size_info *)(
5033 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5034 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5035 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5036 sizeof(struct smb2_fs_full_size_info));
5038 smb2_copy_fs_info_to_kstatfs(info, fsdata);
5041 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5046 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
5047 u64 persistent_fid, u64 volatile_fid, int level)
5049 struct smb_rqst rqst;
5050 struct smb2_query_info_rsp *rsp = NULL;
5052 struct kvec rsp_iov;
5054 int resp_buftype, max_len, min_len;
5055 struct cifs_ses *ses = tcon->ses;
5056 unsigned int rsp_len, offset;
5059 if (level == FS_DEVICE_INFORMATION) {
5060 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5061 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5062 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5063 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5064 min_len = MIN_FS_ATTR_INFO_SIZE;
5065 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5066 max_len = sizeof(struct smb3_fs_ss_info);
5067 min_len = sizeof(struct smb3_fs_ss_info);
5068 } else if (level == FS_VOLUME_INFORMATION) {
5069 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5070 min_len = sizeof(struct smb3_fs_vol_info);
5072 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
5076 rc = build_qfs_info_req(&iov, tcon, level, max_len,
5077 persistent_fid, volatile_fid);
5081 if (smb3_encryption_required(tcon))
5082 flags |= CIFS_TRANSFORM_REQ;
5084 memset(&rqst, 0, sizeof(struct smb_rqst));
5088 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
5089 cifs_small_buf_release(iov.iov_base);
5091 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5094 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5096 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5097 offset = le16_to_cpu(rsp->OutputBufferOffset);
5098 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
5102 if (level == FS_ATTRIBUTE_INFORMATION)
5103 memcpy(&tcon->fsAttrInfo, offset
5104 + (char *)rsp, min_t(unsigned int,
5106 else if (level == FS_DEVICE_INFORMATION)
5107 memcpy(&tcon->fsDevInfo, offset
5108 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
5109 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5110 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
5111 (offset + (char *)rsp);
5112 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5113 tcon->perf_sector_size =
5114 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
5115 } else if (level == FS_VOLUME_INFORMATION) {
5116 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5117 (offset + (char *)rsp);
5118 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5119 tcon->vol_create_time = vol_info->VolumeCreationTime;
5123 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5128 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5129 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5130 const __u32 num_lock, struct smb2_lock_element *buf)
5132 struct smb_rqst rqst;
5134 struct smb2_lock_req *req = NULL;
5136 struct kvec rsp_iov;
5139 int flags = CIFS_NO_RSP_BUF;
5140 unsigned int total_len;
5142 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
5144 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
5148 if (smb3_encryption_required(tcon))
5149 flags |= CIFS_TRANSFORM_REQ;
5151 req->sync_hdr.ProcessId = cpu_to_le32(pid);
5152 req->LockCount = cpu_to_le16(num_lock);
5154 req->PersistentFileId = persist_fid;
5155 req->VolatileFileId = volatile_fid;
5157 count = num_lock * sizeof(struct smb2_lock_element);
5159 iov[0].iov_base = (char *)req;
5160 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
5161 iov[1].iov_base = (char *)buf;
5162 iov[1].iov_len = count;
5164 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
5166 memset(&rqst, 0, sizeof(struct smb_rqst));
5170 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
5172 cifs_small_buf_release(req);
5174 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
5175 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
5176 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
5177 tcon->ses->Suid, rc);
5184 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
5185 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5186 const __u64 length, const __u64 offset, const __u32 lock_flags,
5189 struct smb2_lock_element lock;
5191 lock.Offset = cpu_to_le64(offset);
5192 lock.Length = cpu_to_le64(length);
5193 lock.Flags = cpu_to_le32(lock_flags);
5194 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5195 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5197 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5201 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5202 __u8 *lease_key, const __le32 lease_state)
5204 struct smb_rqst rqst;
5206 struct smb2_lease_ack *req = NULL;
5207 struct cifs_ses *ses = tcon->ses;
5208 int flags = CIFS_OBREAK_OP;
5209 unsigned int total_len;
5211 struct kvec rsp_iov;
5213 __u64 *please_key_high;
5214 __u64 *please_key_low;
5216 cifs_dbg(FYI, "SMB2_lease_break\n");
5217 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
5222 if (smb3_encryption_required(tcon))
5223 flags |= CIFS_TRANSFORM_REQ;
5225 req->sync_hdr.CreditRequest = cpu_to_le16(1);
5226 req->StructureSize = cpu_to_le16(36);
5229 memcpy(req->LeaseKey, lease_key, 16);
5230 req->LeaseState = lease_state;
5232 flags |= CIFS_NO_RSP_BUF;
5234 iov[0].iov_base = (char *)req;
5235 iov[0].iov_len = total_len;
5237 memset(&rqst, 0, sizeof(struct smb_rqst));
5241 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
5242 cifs_small_buf_release(req);
5244 please_key_low = (__u64 *)lease_key;
5245 please_key_high = (__u64 *)(lease_key+8);
5247 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5248 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5249 ses->Suid, *please_key_low, *please_key_high, rc);
5250 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
5252 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5253 ses->Suid, *please_key_low, *please_key_high);