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 *tgt = dfs_cache_get_tgt_name(it);
198 extract_unc_hostname(tgt, &dfs_host, &dfs_host_len);
200 if (dfs_host_len != tcp_host_len
201 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
202 cifs_dbg(FYI, "%s: skipping %.*s, doesn't match %.*s",
204 (int)dfs_host_len, dfs_host,
205 (int)tcp_host_len, tcp_host);
209 scnprintf(tree, MAX_TREE_SIZE, "\\%s", tgt);
211 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
220 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1,
225 dfs_cache_free_tgts(&tl);
231 static inline int __smb2_reconnect(const struct nls_table *nlsc,
232 struct cifs_tcon *tcon)
234 return SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
239 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
242 struct nls_table *nls_codepage;
243 struct cifs_ses *ses;
244 struct TCP_Server_Info *server;
248 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
249 * check for tcp and smb session status done differently
250 * for those three - in the calling routine.
255 if (smb2_command == SMB2_TREE_CONNECT)
258 if (tcon->tidStatus == CifsExiting) {
260 * only tree disconnect, open, and write,
261 * (and ulogoff which does not have tcon)
262 * are allowed as we start force umount.
264 if ((smb2_command != SMB2_WRITE) &&
265 (smb2_command != SMB2_CREATE) &&
266 (smb2_command != SMB2_TREE_DISCONNECT)) {
267 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
272 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
273 (!tcon->ses->server))
277 server = ses->server;
279 retries = server->nr_targets;
282 * Give demultiplex thread up to 10 seconds to each target available for
283 * reconnect -- should be greater than cifs socket timeout which is 7
286 while (server->tcpStatus == CifsNeedReconnect) {
288 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
289 * here since they are implicitly done when session drops.
291 switch (smb2_command) {
293 * BB Should we keep oplock break and add flush to exceptions?
295 case SMB2_TREE_DISCONNECT:
298 case SMB2_OPLOCK_BREAK:
302 rc = wait_event_interruptible_timeout(server->response_q,
303 (server->tcpStatus != CifsNeedReconnect),
306 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
307 " signal by the process\n", __func__);
311 /* are we still trying to reconnect? */
312 if (server->tcpStatus != CifsNeedReconnect)
319 * on "soft" mounts we wait once. Hard mounts keep
320 * retrying until process is killed or server comes
324 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
327 retries = server->nr_targets;
330 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
333 nls_codepage = load_nls_default();
336 * need to prevent multiple threads trying to simultaneously reconnect
337 * the same SMB session
339 mutex_lock(&tcon->ses->session_mutex);
342 * Recheck after acquire mutex. If another thread is negotiating
343 * and the server never sends an answer the socket will be closed
344 * and tcpStatus set to reconnect.
346 if (server->tcpStatus == CifsNeedReconnect) {
348 mutex_unlock(&tcon->ses->session_mutex);
352 rc = cifs_negotiate_protocol(0, tcon->ses);
353 if (!rc && tcon->ses->need_reconnect)
354 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
356 if (rc || !tcon->need_reconnect) {
357 mutex_unlock(&tcon->ses->session_mutex);
361 cifs_mark_open_files_invalid(tcon);
362 if (tcon->use_persistent)
363 tcon->need_reopen_files = true;
365 rc = __smb2_reconnect(nls_codepage, tcon);
366 mutex_unlock(&tcon->ses->session_mutex);
368 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
370 /* If sess reconnected but tcon didn't, something strange ... */
371 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
375 if (smb2_command != SMB2_INTERNAL_CMD)
376 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
378 atomic_inc(&tconInfoReconnectCount);
381 * Check if handle based operation so we know whether we can continue
382 * or not without returning to caller to reset file handle.
385 * BB Is flush done by server on drop of tcp session? Should we special
386 * case it and skip above?
388 switch (smb2_command) {
394 case SMB2_QUERY_DIRECTORY:
395 case SMB2_CHANGE_NOTIFY:
396 case SMB2_QUERY_INFO:
400 unload_nls(nls_codepage);
405 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
406 unsigned int *total_len)
408 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
409 /* lookup word count ie StructureSize from table */
410 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
413 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
414 * largest operations (Create)
418 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
419 spdu->StructureSize2 = cpu_to_le16(parmsize);
421 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
425 * Allocate and return pointer to an SMB request hdr, and set basic
426 * SMB information in the SMB header. If the return code is zero, this
427 * function must have filled in request_buf pointer.
429 static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
430 void **request_buf, unsigned int *total_len)
432 /* BB eventually switch this to SMB2 specific small buf size */
433 if (smb2_command == SMB2_SET_INFO)
434 *request_buf = cifs_buf_get();
436 *request_buf = cifs_small_buf_get();
437 if (*request_buf == NULL) {
438 /* BB should we add a retry in here if not a writepage? */
442 fill_small_buf(smb2_command, tcon,
443 (struct smb2_sync_hdr *)(*request_buf),
447 uint16_t com_code = le16_to_cpu(smb2_command);
448 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
449 cifs_stats_inc(&tcon->num_smbs_sent);
455 static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
456 void **request_buf, unsigned int *total_len)
460 rc = smb2_reconnect(smb2_command, tcon);
464 return __smb2_plain_req_init(smb2_command, tcon, request_buf,
468 static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
469 void **request_buf, unsigned int *total_len)
471 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
472 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
473 return __smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf,
476 return smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf, total_len);
479 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
482 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
484 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
485 pneg_ctxt->DataLength = cpu_to_le16(38);
486 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
487 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
488 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
489 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
493 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
495 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
496 pneg_ctxt->DataLength =
497 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
498 - sizeof(struct smb2_neg_context));
499 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
500 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
501 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
502 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
506 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
508 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
509 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + two ciphers */
510 pneg_ctxt->CipherCount = cpu_to_le16(2);
511 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
512 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
516 build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
518 struct nls_table *cp = load_nls_default();
520 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
522 /* copy up to max of first 100 bytes of server name to NetName field */
523 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
524 /* context size is DataLength + minimal smb2_neg_context */
525 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
526 sizeof(struct smb2_neg_context), 8) * 8;
530 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
532 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
533 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
534 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
535 pneg_ctxt->Name[0] = 0x93;
536 pneg_ctxt->Name[1] = 0xAD;
537 pneg_ctxt->Name[2] = 0x25;
538 pneg_ctxt->Name[3] = 0x50;
539 pneg_ctxt->Name[4] = 0x9C;
540 pneg_ctxt->Name[5] = 0xB4;
541 pneg_ctxt->Name[6] = 0x11;
542 pneg_ctxt->Name[7] = 0xE7;
543 pneg_ctxt->Name[8] = 0xB4;
544 pneg_ctxt->Name[9] = 0x23;
545 pneg_ctxt->Name[10] = 0x83;
546 pneg_ctxt->Name[11] = 0xDE;
547 pneg_ctxt->Name[12] = 0x96;
548 pneg_ctxt->Name[13] = 0x8B;
549 pneg_ctxt->Name[14] = 0xCD;
550 pneg_ctxt->Name[15] = 0x7C;
554 assemble_neg_contexts(struct smb2_negotiate_req *req,
555 struct TCP_Server_Info *server, unsigned int *total_len)
558 unsigned int ctxt_len;
560 if (*total_len > 200) {
561 /* In case length corrupted don't want to overrun smb buffer */
562 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
567 * round up total_len of fixed part of SMB3 negotiate request to 8
568 * byte boundary before adding negotiate contexts
570 *total_len = roundup(*total_len, 8);
572 pneg_ctxt = (*total_len) + (char *)req;
573 req->NegotiateContextOffset = cpu_to_le32(*total_len);
575 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
576 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
577 *total_len += ctxt_len;
578 pneg_ctxt += ctxt_len;
580 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
581 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
582 *total_len += ctxt_len;
583 pneg_ctxt += ctxt_len;
585 if (server->compress_algorithm) {
586 build_compression_ctxt((struct smb2_compression_capabilities_context *)
588 ctxt_len = DIV_ROUND_UP(
589 sizeof(struct smb2_compression_capabilities_context),
591 *total_len += ctxt_len;
592 pneg_ctxt += ctxt_len;
593 req->NegotiateContextCount = cpu_to_le16(5);
595 req->NegotiateContextCount = cpu_to_le16(4);
597 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
599 *total_len += ctxt_len;
600 pneg_ctxt += ctxt_len;
602 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
603 *total_len += sizeof(struct smb2_posix_neg_context);
606 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
608 unsigned int len = le16_to_cpu(ctxt->DataLength);
610 /* If invalid preauth context warn but use what we requested, SHA-512 */
611 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
612 printk_once(KERN_WARNING "server sent bad preauth context\n");
615 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
616 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
617 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
618 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
621 static void decode_compress_ctx(struct TCP_Server_Info *server,
622 struct smb2_compression_capabilities_context *ctxt)
624 unsigned int len = le16_to_cpu(ctxt->DataLength);
626 /* sizeof compress context is a one element compression capbility struct */
628 printk_once(KERN_WARNING "server sent bad compression cntxt\n");
631 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
632 printk_once(KERN_WARNING "illegal SMB3 compress algorithm count\n");
635 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
636 printk_once(KERN_WARNING "unknown compression algorithm\n");
639 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
642 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
643 struct smb2_encryption_neg_context *ctxt)
645 unsigned int len = le16_to_cpu(ctxt->DataLength);
647 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
648 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
649 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
653 if (le16_to_cpu(ctxt->CipherCount) != 1) {
654 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
657 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
658 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
659 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
660 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
663 server->cipher_type = ctxt->Ciphers[0];
664 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
668 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
669 struct TCP_Server_Info *server,
670 unsigned int len_of_smb)
672 struct smb2_neg_context *pctx;
673 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
674 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
675 unsigned int len_of_ctxts, i;
678 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
679 if (len_of_smb <= offset) {
680 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
684 len_of_ctxts = len_of_smb - offset;
686 for (i = 0; i < ctxt_cnt; i++) {
688 /* check that offset is not beyond end of SMB */
689 if (len_of_ctxts == 0)
692 if (len_of_ctxts < sizeof(struct smb2_neg_context))
695 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
696 clen = le16_to_cpu(pctx->DataLength);
697 if (clen > len_of_ctxts)
700 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
701 decode_preauth_context(
702 (struct smb2_preauth_neg_context *)pctx);
703 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
704 rc = decode_encrypt_ctx(server,
705 (struct smb2_encryption_neg_context *)pctx);
706 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
707 decode_compress_ctx(server,
708 (struct smb2_compression_capabilities_context *)pctx);
709 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
710 server->posix_ext_supported = true;
712 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
713 le16_to_cpu(pctx->ContextType));
717 /* offsets must be 8 byte aligned */
718 clen = (clen + 7) & ~0x7;
719 offset += clen + sizeof(struct smb2_neg_context);
720 len_of_ctxts -= clen;
725 static struct create_posix *
726 create_posix_buf(umode_t mode)
728 struct create_posix *buf;
730 buf = kzalloc(sizeof(struct create_posix),
735 buf->ccontext.DataOffset =
736 cpu_to_le16(offsetof(struct create_posix, Mode));
737 buf->ccontext.DataLength = cpu_to_le32(4);
738 buf->ccontext.NameOffset =
739 cpu_to_le16(offsetof(struct create_posix, Name));
740 buf->ccontext.NameLength = cpu_to_le16(16);
742 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
753 buf->Name[10] = 0x83;
754 buf->Name[11] = 0xDE;
755 buf->Name[12] = 0x96;
756 buf->Name[13] = 0x8B;
757 buf->Name[14] = 0xCD;
758 buf->Name[15] = 0x7C;
759 buf->Mode = cpu_to_le32(mode);
760 cifs_dbg(FYI, "mode on posix create 0%o", mode);
765 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
767 struct smb2_create_req *req = iov[0].iov_base;
768 unsigned int num = *num_iovec;
770 iov[num].iov_base = create_posix_buf(mode);
771 if (mode == ACL_NO_MODE)
772 cifs_dbg(FYI, "illegal mode\n");
773 if (iov[num].iov_base == NULL)
775 iov[num].iov_len = sizeof(struct create_posix);
776 if (!req->CreateContextsOffset)
777 req->CreateContextsOffset = cpu_to_le32(
778 sizeof(struct smb2_create_req) +
779 iov[num - 1].iov_len);
780 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
781 *num_iovec = num + 1;
788 * SMB2 Worker functions follow:
790 * The general structure of the worker functions is:
791 * 1) Call smb2_init (assembles SMB2 header)
792 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
793 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
794 * 4) Decode SMB2 command specific fields in the fixed length area
795 * 5) Decode variable length data area (if any for this SMB2 command type)
796 * 6) Call free smb buffer
802 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
804 struct smb_rqst rqst;
805 struct smb2_negotiate_req *req;
806 struct smb2_negotiate_rsp *rsp;
811 struct TCP_Server_Info *server = cifs_ses_server(ses);
812 int blob_offset, blob_length;
814 int flags = CIFS_NEG_OP;
815 unsigned int total_len;
817 cifs_dbg(FYI, "Negotiate protocol\n");
820 WARN(1, "%s: server is NULL!\n", __func__);
824 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
828 req->sync_hdr.SessionId = 0;
830 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
831 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
833 if (strcmp(server->vals->version_string,
834 SMB3ANY_VERSION_STRING) == 0) {
835 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
836 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
837 req->DialectCount = cpu_to_le16(2);
839 } else if (strcmp(server->vals->version_string,
840 SMBDEFAULT_VERSION_STRING) == 0) {
841 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
842 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
843 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
844 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
845 req->DialectCount = cpu_to_le16(4);
848 /* otherwise send specific dialect */
849 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
850 req->DialectCount = cpu_to_le16(1);
854 /* only one of SMB2 signing flags may be set in SMB2 request */
856 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
857 else if (global_secflags & CIFSSEC_MAY_SIGN)
858 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
860 req->SecurityMode = 0;
862 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
864 /* ClientGUID must be zero for SMB2.02 dialect */
865 if (server->vals->protocol_id == SMB20_PROT_ID)
866 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
868 memcpy(req->ClientGUID, server->client_guid,
869 SMB2_CLIENT_GUID_SIZE);
870 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
871 (strcmp(server->vals->version_string,
872 SMBDEFAULT_VERSION_STRING) == 0))
873 assemble_neg_contexts(req, server, &total_len);
875 iov[0].iov_base = (char *)req;
876 iov[0].iov_len = total_len;
878 memset(&rqst, 0, sizeof(struct smb_rqst));
882 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
883 cifs_small_buf_release(req);
884 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
886 * No tcon so can't do
887 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
889 if (rc == -EOPNOTSUPP) {
890 cifs_server_dbg(VFS, "Dialect not supported by server. Consider "
891 "specifying vers=1.0 or vers=2.0 on mount for accessing"
897 if (strcmp(server->vals->version_string,
898 SMB3ANY_VERSION_STRING) == 0) {
899 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
901 "SMB2 dialect returned but not requested\n");
903 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
905 "SMB2.1 dialect returned but not requested\n");
908 } else if (strcmp(server->vals->version_string,
909 SMBDEFAULT_VERSION_STRING) == 0) {
910 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
912 "SMB2 dialect returned but not requested\n");
914 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
915 /* ops set to 3.0 by default for default so update */
916 server->ops = &smb21_operations;
917 server->vals = &smb21_values;
918 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
919 server->ops = &smb311_operations;
920 server->vals = &smb311_values;
922 } else if (le16_to_cpu(rsp->DialectRevision) !=
923 server->vals->protocol_id) {
924 /* if requested single dialect ensure returned dialect matched */
925 cifs_server_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
926 le16_to_cpu(rsp->DialectRevision));
930 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
932 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
933 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
934 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
935 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
936 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
937 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
938 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
939 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
940 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
941 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
943 cifs_server_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
944 le16_to_cpu(rsp->DialectRevision));
948 server->dialect = le16_to_cpu(rsp->DialectRevision);
951 * Keep a copy of the hash after negprot. This hash will be
952 * the starting hash value for all sessions made from this
955 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
956 SMB2_PREAUTH_HASH_SIZE);
958 /* SMB2 only has an extended negflavor */
959 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
960 /* set it to the maximum buffer size value we can send with 1 credit */
961 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
962 SMB2_MAX_BUFFER_SIZE);
963 server->max_read = le32_to_cpu(rsp->MaxReadSize);
964 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
965 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
966 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
967 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
969 server->capabilities = le32_to_cpu(rsp->Capabilities);
971 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
973 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
974 (struct smb2_sync_hdr *)rsp);
976 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
978 * ses->sectype = RawNTLMSSP;
979 * but for time being this is our only auth choice so doesn't matter.
980 * We just found a server which sets blob length to zero expecting raw.
982 if (blob_length == 0) {
983 cifs_dbg(FYI, "missing security blob on negprot\n");
984 server->sec_ntlmssp = true;
987 rc = cifs_enable_signing(server, ses->sign);
991 rc = decode_negTokenInit(security_blob, blob_length, server);
998 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
999 if (rsp->NegotiateContextCount)
1000 rc = smb311_decode_neg_context(rsp, server,
1003 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
1006 free_rsp_buf(resp_buftype, rsp);
1010 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1013 struct validate_negotiate_info_req *pneg_inbuf;
1014 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
1016 u32 inbuflen; /* max of 4 dialects */
1017 struct TCP_Server_Info *server = tcon->ses->server;
1019 cifs_dbg(FYI, "validate negotiate\n");
1021 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1022 if (server->dialect == SMB311_PROT_ID)
1026 * validation ioctl must be signed, so no point sending this if we
1027 * can not sign it (ie are not known user). Even if signing is not
1028 * required (enabled but not negotiated), in those cases we selectively
1029 * sign just this, the first and only signed request on a connection.
1030 * Having validation of negotiate info helps reduce attack vectors.
1032 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1033 return 0; /* validation requires signing */
1035 if (tcon->ses->user_name == NULL) {
1036 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1037 return 0; /* validation requires signing */
1040 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1041 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1043 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1047 pneg_inbuf->Capabilities =
1048 cpu_to_le32(server->vals->req_capabilities);
1049 memcpy(pneg_inbuf->Guid, server->client_guid,
1050 SMB2_CLIENT_GUID_SIZE);
1052 if (tcon->ses->sign)
1053 pneg_inbuf->SecurityMode =
1054 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1055 else if (global_secflags & CIFSSEC_MAY_SIGN)
1056 pneg_inbuf->SecurityMode =
1057 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1059 pneg_inbuf->SecurityMode = 0;
1062 if (strcmp(server->vals->version_string,
1063 SMB3ANY_VERSION_STRING) == 0) {
1064 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1065 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1066 pneg_inbuf->DialectCount = cpu_to_le16(2);
1067 /* structure is big enough for 3 dialects, sending only 2 */
1068 inbuflen = sizeof(*pneg_inbuf) -
1069 (2 * sizeof(pneg_inbuf->Dialects[0]));
1070 } else if (strcmp(server->vals->version_string,
1071 SMBDEFAULT_VERSION_STRING) == 0) {
1072 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1073 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1074 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1075 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1076 pneg_inbuf->DialectCount = cpu_to_le16(4);
1077 /* structure is big enough for 3 dialects */
1078 inbuflen = sizeof(*pneg_inbuf);
1080 /* otherwise specific dialect was requested */
1081 pneg_inbuf->Dialects[0] =
1082 cpu_to_le16(server->vals->protocol_id);
1083 pneg_inbuf->DialectCount = cpu_to_le16(1);
1084 /* structure is big enough for 3 dialects, sending only 1 */
1085 inbuflen = sizeof(*pneg_inbuf) -
1086 sizeof(pneg_inbuf->Dialects[0]) * 2;
1089 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1090 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
1091 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1092 (char **)&pneg_rsp, &rsplen);
1093 if (rc == -EOPNOTSUPP) {
1095 * Old Windows versions or Netapp SMB server can return
1096 * not supported error. Client should accept it.
1098 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1100 goto out_free_inbuf;
1101 } else if (rc != 0) {
1102 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
1104 goto out_free_inbuf;
1108 if (rsplen != sizeof(*pneg_rsp)) {
1109 cifs_tcon_dbg(VFS, "invalid protocol negotiate response size: %d\n",
1112 /* relax check since Mac returns max bufsize allowed on ioctl */
1113 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1117 /* check validate negotiate info response matches what we got earlier */
1118 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1121 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1124 /* do not validate server guid because not saved at negprot time yet */
1126 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1127 SMB2_LARGE_FILES) != server->capabilities)
1130 /* validate negotiate successful */
1132 cifs_dbg(FYI, "validate negotiate info successful\n");
1136 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1145 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1147 switch (requested) {
1154 if (server->sec_ntlmssp &&
1155 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1157 if ((server->sec_kerberos || server->sec_mskerberos) &&
1158 (global_secflags & CIFSSEC_MAY_KRB5))
1166 struct SMB2_sess_data {
1168 struct cifs_ses *ses;
1169 struct nls_table *nls_cp;
1170 void (*func)(struct SMB2_sess_data *);
1172 u64 previous_session;
1174 /* we will send the SMB in three pieces:
1175 * a fixed length beginning part, an optional
1176 * SPNEGO blob (which can be zero length), and a
1177 * last part which will include the strings
1178 * and rest of bcc area. This allows us to avoid
1179 * a large buffer 17K allocation
1186 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1189 struct cifs_ses *ses = sess_data->ses;
1190 struct smb2_sess_setup_req *req;
1191 struct TCP_Server_Info *server = cifs_ses_server(ses);
1192 unsigned int total_len;
1194 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1199 if (sess_data->ses->binding) {
1200 req->sync_hdr.SessionId = sess_data->ses->Suid;
1201 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1202 req->PreviousSessionId = 0;
1203 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1205 /* First session, not a reauthenticate */
1206 req->sync_hdr.SessionId = 0;
1208 * if reconnect, we need to send previous sess id
1211 req->PreviousSessionId = sess_data->previous_session;
1212 req->Flags = 0; /* MBZ */
1215 /* enough to enable echos and oplocks and one max size write */
1216 req->sync_hdr.CreditRequest = cpu_to_le16(130);
1218 /* only one of SMB2 signing flags may be set in SMB2 request */
1220 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1221 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1222 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1224 req->SecurityMode = 0;
1226 #ifdef CONFIG_CIFS_DFS_UPCALL
1227 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1229 req->Capabilities = 0;
1230 #endif /* DFS_UPCALL */
1232 req->Channel = 0; /* MBZ */
1234 sess_data->iov[0].iov_base = (char *)req;
1236 sess_data->iov[0].iov_len = total_len - 1;
1238 * This variable will be used to clear the buffer
1239 * allocated above in case of any error in the calling function.
1241 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1247 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1249 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1250 sess_data->buf0_type = CIFS_NO_BUFFER;
1254 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1257 struct smb_rqst rqst;
1258 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1259 struct kvec rsp_iov = { NULL, 0 };
1261 /* Testing shows that buffer offset must be at location of Buffer[0] */
1262 req->SecurityBufferOffset =
1263 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1264 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1266 memset(&rqst, 0, sizeof(struct smb_rqst));
1267 rqst.rq_iov = sess_data->iov;
1270 /* BB add code to build os and lm fields */
1271 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1273 &sess_data->buf0_type,
1274 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
1275 cifs_small_buf_release(sess_data->iov[0].iov_base);
1276 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1282 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1285 struct cifs_ses *ses = sess_data->ses;
1286 struct TCP_Server_Info *server = cifs_ses_server(ses);
1288 mutex_lock(&server->srv_mutex);
1289 if (server->ops->generate_signingkey) {
1290 rc = server->ops->generate_signingkey(ses);
1293 "SMB3 session key generation failed\n");
1294 mutex_unlock(&server->srv_mutex);
1298 if (!server->session_estab) {
1299 server->sequence_number = 0x2;
1300 server->session_estab = true;
1302 mutex_unlock(&server->srv_mutex);
1304 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1305 /* keep existing ses state if binding */
1306 if (!ses->binding) {
1307 spin_lock(&GlobalMid_Lock);
1308 ses->status = CifsGood;
1309 ses->need_reconnect = false;
1310 spin_unlock(&GlobalMid_Lock);
1316 #ifdef CONFIG_CIFS_UPCALL
1318 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1321 struct cifs_ses *ses = sess_data->ses;
1322 struct cifs_spnego_msg *msg;
1323 struct key *spnego_key = NULL;
1324 struct smb2_sess_setup_rsp *rsp = NULL;
1326 rc = SMB2_sess_alloc_buffer(sess_data);
1330 spnego_key = cifs_get_spnego_key(ses);
1331 if (IS_ERR(spnego_key)) {
1332 rc = PTR_ERR(spnego_key);
1337 msg = spnego_key->payload.data[0];
1339 * check version field to make sure that cifs.upcall is
1340 * sending us a response in an expected form
1342 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1344 "bad cifs.upcall version. Expected %d got %d",
1345 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1347 goto out_put_spnego_key;
1350 /* keep session key if binding */
1351 if (!ses->binding) {
1352 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1354 if (!ses->auth_key.response) {
1356 "Kerberos can't allocate (%u bytes) memory",
1359 goto out_put_spnego_key;
1361 ses->auth_key.len = msg->sesskey_len;
1364 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1365 sess_data->iov[1].iov_len = msg->secblob_len;
1367 rc = SMB2_sess_sendreceive(sess_data);
1369 goto out_put_spnego_key;
1371 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1372 /* keep session id and flags if binding */
1373 if (!ses->binding) {
1374 ses->Suid = rsp->sync_hdr.SessionId;
1375 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1378 rc = SMB2_sess_establish_session(sess_data);
1380 key_invalidate(spnego_key);
1381 key_put(spnego_key);
1383 sess_data->result = rc;
1384 sess_data->func = NULL;
1385 SMB2_sess_free_buffer(sess_data);
1389 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1391 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1392 sess_data->result = -EOPNOTSUPP;
1393 sess_data->func = NULL;
1398 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1401 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1404 struct cifs_ses *ses = sess_data->ses;
1405 struct smb2_sess_setup_rsp *rsp = NULL;
1406 char *ntlmssp_blob = NULL;
1407 bool use_spnego = false; /* else use raw ntlmssp */
1408 u16 blob_length = 0;
1411 * If memory allocation is successful, caller of this function
1414 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1415 if (!ses->ntlmssp) {
1419 ses->ntlmssp->sesskey_per_smbsess = true;
1421 rc = SMB2_sess_alloc_buffer(sess_data);
1425 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1427 if (ntlmssp_blob == NULL) {
1432 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1434 /* BB eventually need to add this */
1435 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1439 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1440 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1442 sess_data->iov[1].iov_base = ntlmssp_blob;
1443 sess_data->iov[1].iov_len = blob_length;
1445 rc = SMB2_sess_sendreceive(sess_data);
1446 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1448 /* If true, rc here is expected and not an error */
1449 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1450 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1456 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1457 le16_to_cpu(rsp->SecurityBufferOffset)) {
1458 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1459 le16_to_cpu(rsp->SecurityBufferOffset));
1463 rc = decode_ntlmssp_challenge(rsp->Buffer,
1464 le16_to_cpu(rsp->SecurityBufferLength), ses);
1468 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1470 /* keep existing ses id and flags if binding */
1471 if (!ses->binding) {
1472 ses->Suid = rsp->sync_hdr.SessionId;
1473 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1477 kfree(ntlmssp_blob);
1478 SMB2_sess_free_buffer(sess_data);
1480 sess_data->result = 0;
1481 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1485 kfree(ses->ntlmssp);
1486 ses->ntlmssp = NULL;
1487 sess_data->result = rc;
1488 sess_data->func = NULL;
1492 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1495 struct cifs_ses *ses = sess_data->ses;
1496 struct smb2_sess_setup_req *req;
1497 struct smb2_sess_setup_rsp *rsp = NULL;
1498 unsigned char *ntlmssp_blob = NULL;
1499 bool use_spnego = false; /* else use raw ntlmssp */
1500 u16 blob_length = 0;
1502 rc = SMB2_sess_alloc_buffer(sess_data);
1506 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1507 req->sync_hdr.SessionId = ses->Suid;
1509 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1512 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1517 /* BB eventually need to add this */
1518 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1522 sess_data->iov[1].iov_base = ntlmssp_blob;
1523 sess_data->iov[1].iov_len = blob_length;
1525 rc = SMB2_sess_sendreceive(sess_data);
1529 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1531 /* keep existing ses id and flags if binding */
1532 if (!ses->binding) {
1533 ses->Suid = rsp->sync_hdr.SessionId;
1534 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1537 rc = SMB2_sess_establish_session(sess_data);
1539 kfree(ntlmssp_blob);
1540 SMB2_sess_free_buffer(sess_data);
1541 kfree(ses->ntlmssp);
1542 ses->ntlmssp = NULL;
1543 sess_data->result = rc;
1544 sess_data->func = NULL;
1548 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1552 type = smb2_select_sectype(cifs_ses_server(ses), ses->sectype);
1553 cifs_dbg(FYI, "sess setup type %d\n", type);
1554 if (type == Unspecified) {
1556 "Unable to select appropriate authentication method!");
1562 sess_data->func = SMB2_auth_kerberos;
1565 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1568 cifs_dbg(VFS, "secType %d not supported!\n", type);
1576 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1577 const struct nls_table *nls_cp)
1580 struct TCP_Server_Info *server = cifs_ses_server(ses);
1581 struct SMB2_sess_data *sess_data;
1583 cifs_dbg(FYI, "Session Setup\n");
1586 WARN(1, "%s: server is NULL!\n", __func__);
1590 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1594 rc = SMB2_select_sec(ses, sess_data);
1597 sess_data->xid = xid;
1598 sess_data->ses = ses;
1599 sess_data->buf0_type = CIFS_NO_BUFFER;
1600 sess_data->nls_cp = (struct nls_table *) nls_cp;
1601 sess_data->previous_session = ses->Suid;
1604 * Initialize the session hash with the server one.
1606 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
1607 SMB2_PREAUTH_HASH_SIZE);
1609 while (sess_data->func)
1610 sess_data->func(sess_data);
1612 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1613 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1614 rc = sess_data->result;
1621 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1623 struct smb_rqst rqst;
1624 struct smb2_logoff_req *req; /* response is also trivial struct */
1626 struct TCP_Server_Info *server;
1628 unsigned int total_len;
1630 struct kvec rsp_iov;
1633 cifs_dbg(FYI, "disconnect session %p\n", ses);
1635 if (ses && (ses->server))
1636 server = ses->server;
1640 /* no need to send SMB logoff if uid already closed due to reconnect */
1641 if (ses->need_reconnect)
1642 goto smb2_session_already_dead;
1644 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
1648 /* since no tcon, smb2_init can not do this, so do here */
1649 req->sync_hdr.SessionId = ses->Suid;
1651 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1652 flags |= CIFS_TRANSFORM_REQ;
1653 else if (server->sign)
1654 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1656 flags |= CIFS_NO_RSP_BUF;
1658 iov[0].iov_base = (char *)req;
1659 iov[0].iov_len = total_len;
1661 memset(&rqst, 0, sizeof(struct smb_rqst));
1665 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1666 cifs_small_buf_release(req);
1668 * No tcon so can't do
1669 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1672 smb2_session_already_dead:
1676 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1678 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1681 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1683 /* These are similar values to what Windows uses */
1684 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1686 tcon->max_chunks = 256;
1687 tcon->max_bytes_chunk = 1048576;
1688 tcon->max_bytes_copy = 16777216;
1692 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1693 struct cifs_tcon *tcon, const struct nls_table *cp)
1695 struct smb_rqst rqst;
1696 struct smb2_tree_connect_req *req;
1697 struct smb2_tree_connect_rsp *rsp = NULL;
1699 struct kvec rsp_iov = { NULL, 0 };
1703 __le16 *unc_path = NULL;
1705 unsigned int total_len;
1706 struct TCP_Server_Info *server = ses->server;
1708 cifs_dbg(FYI, "TCON\n");
1710 if (!server || !tree)
1713 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1714 if (unc_path == NULL)
1717 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1719 if (unc_path_len < 2) {
1724 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1726 atomic_set(&tcon->num_remote_opens, 0);
1727 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1734 if (smb3_encryption_required(tcon))
1735 flags |= CIFS_TRANSFORM_REQ;
1737 iov[0].iov_base = (char *)req;
1739 iov[0].iov_len = total_len - 1;
1741 /* Testing shows that buffer offset must be at location of Buffer[0] */
1742 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1744 req->PathLength = cpu_to_le16(unc_path_len - 2);
1745 iov[1].iov_base = unc_path;
1746 iov[1].iov_len = unc_path_len;
1749 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1750 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1751 * (Samba servers don't always set the flag so also check if null user)
1753 if ((server->dialect == SMB311_PROT_ID) &&
1754 !smb3_encryption_required(tcon) &&
1755 !(ses->session_flags &
1756 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1757 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1758 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1760 memset(&rqst, 0, sizeof(struct smb_rqst));
1764 /* Need 64 for max size write so ask for more in case not there yet */
1765 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1767 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
1768 cifs_small_buf_release(req);
1769 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1770 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1773 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1774 tcon->need_reconnect = true;
1776 goto tcon_error_exit;
1779 switch (rsp->ShareType) {
1780 case SMB2_SHARE_TYPE_DISK:
1781 cifs_dbg(FYI, "connection to disk share\n");
1783 case SMB2_SHARE_TYPE_PIPE:
1785 cifs_dbg(FYI, "connection to pipe share\n");
1787 case SMB2_SHARE_TYPE_PRINT:
1789 cifs_dbg(FYI, "connection to printer\n");
1792 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1794 goto tcon_error_exit;
1797 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1798 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1799 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1800 tcon->tidStatus = CifsGood;
1801 tcon->need_reconnect = false;
1802 tcon->tid = rsp->sync_hdr.TreeId;
1803 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1805 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1806 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1807 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
1810 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1811 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
1813 init_copy_chunk_defaults(tcon);
1814 if (server->ops->validate_negotiate)
1815 rc = server->ops->validate_negotiate(xid, tcon);
1818 free_rsp_buf(resp_buftype, rsp);
1823 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1824 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1830 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1832 struct smb_rqst rqst;
1833 struct smb2_tree_disconnect_req *req; /* response is trivial */
1835 struct cifs_ses *ses = tcon->ses;
1837 unsigned int total_len;
1839 struct kvec rsp_iov;
1842 cifs_dbg(FYI, "Tree Disconnect\n");
1844 if (!ses || !(ses->server))
1847 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1850 close_shroot_lease(&tcon->crfid);
1852 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1857 if (smb3_encryption_required(tcon))
1858 flags |= CIFS_TRANSFORM_REQ;
1860 flags |= CIFS_NO_RSP_BUF;
1862 iov[0].iov_base = (char *)req;
1863 iov[0].iov_len = total_len;
1865 memset(&rqst, 0, sizeof(struct smb_rqst));
1869 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1870 cifs_small_buf_release(req);
1872 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1878 static struct create_durable *
1879 create_durable_buf(void)
1881 struct create_durable *buf;
1883 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1887 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1888 (struct create_durable, Data));
1889 buf->ccontext.DataLength = cpu_to_le32(16);
1890 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1891 (struct create_durable, Name));
1892 buf->ccontext.NameLength = cpu_to_le16(4);
1893 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1901 static struct create_durable *
1902 create_reconnect_durable_buf(struct cifs_fid *fid)
1904 struct create_durable *buf;
1906 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1910 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1911 (struct create_durable, Data));
1912 buf->ccontext.DataLength = cpu_to_le32(16);
1913 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1914 (struct create_durable, Name));
1915 buf->ccontext.NameLength = cpu_to_le16(4);
1916 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1917 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1918 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1927 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1929 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1931 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1932 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1933 buf->IndexNumber = pdisk_id->DiskFileId;
1937 smb2_parse_contexts(struct TCP_Server_Info *server,
1938 struct smb2_create_rsp *rsp,
1939 unsigned int *epoch, char *lease_key, __u8 *oplock,
1940 struct smb2_file_all_info *buf)
1943 struct create_context *cc;
1945 unsigned int remaining;
1949 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
1950 remaining = le32_to_cpu(rsp->CreateContextsLength);
1951 cc = (struct create_context *)data_offset;
1953 /* Initialize inode number to 0 in case no valid data in qfid context */
1955 buf->IndexNumber = 0;
1957 while (remaining >= sizeof(struct create_context)) {
1958 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1959 if (le16_to_cpu(cc->NameLength) == 4 &&
1960 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
1961 *oplock = server->ops->parse_lease_buf(cc, epoch,
1963 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
1964 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
1965 parse_query_id_ctxt(cc, buf);
1967 next = le32_to_cpu(cc->Next);
1971 cc = (struct create_context *)((char *)cc + next);
1974 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
1975 *oplock = rsp->OplockLevel;
1981 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1982 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
1984 struct smb2_create_req *req = iov[0].iov_base;
1985 unsigned int num = *num_iovec;
1987 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
1988 if (iov[num].iov_base == NULL)
1990 iov[num].iov_len = server->vals->create_lease_size;
1991 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1992 if (!req->CreateContextsOffset)
1993 req->CreateContextsOffset = cpu_to_le32(
1994 sizeof(struct smb2_create_req) +
1995 iov[num - 1].iov_len);
1996 le32_add_cpu(&req->CreateContextsLength,
1997 server->vals->create_lease_size);
1998 *num_iovec = num + 1;
2002 static struct create_durable_v2 *
2003 create_durable_v2_buf(struct cifs_open_parms *oparms)
2005 struct cifs_fid *pfid = oparms->fid;
2006 struct create_durable_v2 *buf;
2008 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2012 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2013 (struct create_durable_v2, dcontext));
2014 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2015 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2016 (struct create_durable_v2, Name));
2017 buf->ccontext.NameLength = cpu_to_le16(4);
2020 * NB: Handle timeout defaults to 0, which allows server to choose
2021 * (most servers default to 120 seconds) and most clients default to 0.
2022 * This can be overridden at mount ("handletimeout=") if the user wants
2023 * a different persistent (or resilient) handle timeout for all opens
2024 * opens on a particular SMB3 mount.
2026 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2027 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2028 generate_random_uuid(buf->dcontext.CreateGuid);
2029 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2031 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2039 static struct create_durable_handle_reconnect_v2 *
2040 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2042 struct create_durable_handle_reconnect_v2 *buf;
2044 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2049 buf->ccontext.DataOffset =
2050 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2052 buf->ccontext.DataLength =
2053 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2054 buf->ccontext.NameOffset =
2055 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2057 buf->ccontext.NameLength = cpu_to_le16(4);
2059 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2060 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2061 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2062 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2064 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2073 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2074 struct cifs_open_parms *oparms)
2076 struct smb2_create_req *req = iov[0].iov_base;
2077 unsigned int num = *num_iovec;
2079 iov[num].iov_base = create_durable_v2_buf(oparms);
2080 if (iov[num].iov_base == NULL)
2082 iov[num].iov_len = sizeof(struct create_durable_v2);
2083 if (!req->CreateContextsOffset)
2084 req->CreateContextsOffset =
2085 cpu_to_le32(sizeof(struct smb2_create_req) +
2087 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
2088 *num_iovec = num + 1;
2093 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2094 struct cifs_open_parms *oparms)
2096 struct smb2_create_req *req = iov[0].iov_base;
2097 unsigned int num = *num_iovec;
2099 /* indicate that we don't need to relock the file */
2100 oparms->reconnect = false;
2102 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2103 if (iov[num].iov_base == NULL)
2105 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2106 if (!req->CreateContextsOffset)
2107 req->CreateContextsOffset =
2108 cpu_to_le32(sizeof(struct smb2_create_req) +
2110 le32_add_cpu(&req->CreateContextsLength,
2111 sizeof(struct create_durable_handle_reconnect_v2));
2112 *num_iovec = num + 1;
2117 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2118 struct cifs_open_parms *oparms, bool use_persistent)
2120 struct smb2_create_req *req = iov[0].iov_base;
2121 unsigned int num = *num_iovec;
2123 if (use_persistent) {
2124 if (oparms->reconnect)
2125 return add_durable_reconnect_v2_context(iov, num_iovec,
2128 return add_durable_v2_context(iov, num_iovec, oparms);
2131 if (oparms->reconnect) {
2132 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2133 /* indicate that we don't need to relock the file */
2134 oparms->reconnect = false;
2136 iov[num].iov_base = create_durable_buf();
2137 if (iov[num].iov_base == NULL)
2139 iov[num].iov_len = sizeof(struct create_durable);
2140 if (!req->CreateContextsOffset)
2141 req->CreateContextsOffset =
2142 cpu_to_le32(sizeof(struct smb2_create_req) +
2144 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
2145 *num_iovec = num + 1;
2149 /* See MS-SMB2 2.2.13.2.7 */
2150 static struct crt_twarp_ctxt *
2151 create_twarp_buf(__u64 timewarp)
2153 struct crt_twarp_ctxt *buf;
2155 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2159 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2160 (struct crt_twarp_ctxt, Timestamp));
2161 buf->ccontext.DataLength = cpu_to_le32(8);
2162 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2163 (struct crt_twarp_ctxt, Name));
2164 buf->ccontext.NameLength = cpu_to_le16(4);
2165 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2170 buf->Timestamp = cpu_to_le64(timewarp);
2174 /* See MS-SMB2 2.2.13.2.7 */
2176 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2178 struct smb2_create_req *req = iov[0].iov_base;
2179 unsigned int num = *num_iovec;
2181 iov[num].iov_base = create_twarp_buf(timewarp);
2182 if (iov[num].iov_base == NULL)
2184 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2185 if (!req->CreateContextsOffset)
2186 req->CreateContextsOffset = cpu_to_le32(
2187 sizeof(struct smb2_create_req) +
2188 iov[num - 1].iov_len);
2189 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2190 *num_iovec = num + 1;
2194 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2195 static struct crt_sd_ctxt *
2196 create_sd_buf(umode_t mode, unsigned int *len)
2198 struct crt_sd_ctxt *buf;
2199 struct cifs_ace *pace;
2200 unsigned int sdlen, acelen;
2202 *len = roundup(sizeof(struct crt_sd_ctxt) + sizeof(struct cifs_ace) * 2,
2204 buf = kzalloc(*len, GFP_KERNEL);
2208 sdlen = sizeof(struct smb3_sd) + sizeof(struct smb3_acl) +
2209 2 * sizeof(struct cifs_ace);
2211 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2212 (struct crt_sd_ctxt, sd));
2213 buf->ccontext.DataLength = cpu_to_le32(sdlen);
2214 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2215 (struct crt_sd_ctxt, Name));
2216 buf->ccontext.NameLength = cpu_to_le16(4);
2217 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2222 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2224 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2225 * and "DP" ie the DACL is present
2227 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2229 /* offset owner, group and Sbz1 and SACL are all zero */
2230 buf->sd.OffsetDacl = cpu_to_le32(sizeof(struct smb3_sd));
2231 buf->acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2233 /* create one ACE to hold the mode embedded in reserved special SID */
2234 pace = (struct cifs_ace *)(sizeof(struct crt_sd_ctxt) + (char *)buf);
2235 acelen = setup_special_mode_ACE(pace, (__u64)mode);
2236 /* and one more ACE to allow access for authenticated users */
2237 pace = (struct cifs_ace *)(acelen + (sizeof(struct crt_sd_ctxt) +
2239 acelen += setup_authusers_ACE(pace);
2240 buf->acl.AclSize = cpu_to_le16(sizeof(struct cifs_acl) + acelen);
2241 buf->acl.AceCount = cpu_to_le16(2);
2246 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
2248 struct smb2_create_req *req = iov[0].iov_base;
2249 unsigned int num = *num_iovec;
2250 unsigned int len = 0;
2252 iov[num].iov_base = create_sd_buf(mode, &len);
2253 if (iov[num].iov_base == NULL)
2255 iov[num].iov_len = len;
2256 if (!req->CreateContextsOffset)
2257 req->CreateContextsOffset = cpu_to_le32(
2258 sizeof(struct smb2_create_req) +
2259 iov[num - 1].iov_len);
2260 le32_add_cpu(&req->CreateContextsLength, len);
2261 *num_iovec = num + 1;
2265 static struct crt_query_id_ctxt *
2266 create_query_id_buf(void)
2268 struct crt_query_id_ctxt *buf;
2270 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2274 buf->ccontext.DataOffset = cpu_to_le16(0);
2275 buf->ccontext.DataLength = cpu_to_le32(0);
2276 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2277 (struct crt_query_id_ctxt, Name));
2278 buf->ccontext.NameLength = cpu_to_le16(4);
2279 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2287 /* See MS-SMB2 2.2.13.2.9 */
2289 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2291 struct smb2_create_req *req = iov[0].iov_base;
2292 unsigned int num = *num_iovec;
2294 iov[num].iov_base = create_query_id_buf();
2295 if (iov[num].iov_base == NULL)
2297 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2298 if (!req->CreateContextsOffset)
2299 req->CreateContextsOffset = cpu_to_le32(
2300 sizeof(struct smb2_create_req) +
2301 iov[num - 1].iov_len);
2302 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2303 *num_iovec = num + 1;
2308 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2309 const char *treename, const __le16 *path)
2311 int treename_len, path_len;
2312 struct nls_table *cp;
2313 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2318 treename_len = strlen(treename);
2319 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2325 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2328 * make room for one path separator between the treename and
2331 *out_len = treename_len + 1 + path_len;
2334 * final path needs to be null-terminated UTF16 with a
2338 *out_size = roundup((*out_len+1)*2, 8);
2339 *out_path = kzalloc(*out_size, GFP_KERNEL);
2343 cp = load_nls_default();
2344 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2345 UniStrcat(*out_path, sep);
2346 UniStrcat(*out_path, path);
2352 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2353 umode_t mode, struct cifs_tcon *tcon,
2354 const char *full_path,
2355 struct cifs_sb_info *cifs_sb)
2357 struct smb_rqst rqst;
2358 struct smb2_create_req *req;
2359 struct smb2_create_rsp *rsp = NULL;
2360 struct cifs_ses *ses = tcon->ses;
2361 struct kvec iov[3]; /* make sure at least one for each open context */
2362 struct kvec rsp_iov = {NULL, 0};
2365 __le16 *copy_path = NULL;
2368 unsigned int n_iov = 2;
2369 __u32 file_attributes = 0;
2370 char *pc_buf = NULL;
2372 unsigned int total_len;
2373 __le16 *utf16_path = NULL;
2375 cifs_dbg(FYI, "mkdir\n");
2377 /* resource #1: path allocation */
2378 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2382 if (!ses || !(ses->server)) {
2387 /* resource #2: request */
2388 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2393 if (smb3_encryption_required(tcon))
2394 flags |= CIFS_TRANSFORM_REQ;
2396 req->ImpersonationLevel = IL_IMPERSONATION;
2397 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2398 /* File attributes ignored on open (used in create though) */
2399 req->FileAttributes = cpu_to_le32(file_attributes);
2400 req->ShareAccess = FILE_SHARE_ALL_LE;
2401 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2402 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2404 iov[0].iov_base = (char *)req;
2405 /* -1 since last byte is buf[0] which is sent below (path) */
2406 iov[0].iov_len = total_len - 1;
2408 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2410 /* [MS-SMB2] 2.2.13 NameOffset:
2411 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2412 * the SMB2 header, the file name includes a prefix that will
2413 * be processed during DFS name normalization as specified in
2414 * section 3.3.5.9. Otherwise, the file name is relative to
2415 * the share that is identified by the TreeId in the SMB2
2418 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2421 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2422 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2424 tcon->treeName, utf16_path);
2428 req->NameLength = cpu_to_le16(name_len * 2);
2429 uni_path_len = copy_size;
2430 /* free before overwriting resource */
2432 utf16_path = copy_path;
2434 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2435 /* MUST set path len (NameLength) to 0 opening root of share */
2436 req->NameLength = cpu_to_le16(uni_path_len - 2);
2437 if (uni_path_len % 8 != 0) {
2438 copy_size = roundup(uni_path_len, 8);
2439 copy_path = kzalloc(copy_size, GFP_KERNEL);
2444 memcpy((char *)copy_path, (const char *)utf16_path,
2446 uni_path_len = copy_size;
2447 /* free before overwriting resource */
2449 utf16_path = copy_path;
2453 iov[1].iov_len = uni_path_len;
2454 iov[1].iov_base = utf16_path;
2455 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2457 if (tcon->posix_extensions) {
2458 /* resource #3: posix buf */
2459 rc = add_posix_context(iov, &n_iov, mode);
2462 pc_buf = iov[n_iov-1].iov_base;
2466 memset(&rqst, 0, sizeof(struct smb_rqst));
2468 rqst.rq_nvec = n_iov;
2470 /* no need to inc num_remote_opens because we close it just below */
2471 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2472 FILE_WRITE_ATTRIBUTES);
2473 /* resource #4: response buffer */
2474 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2476 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2477 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2479 FILE_WRITE_ATTRIBUTES, rc);
2480 goto err_free_rsp_buf;
2483 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2484 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2485 ses->Suid, CREATE_NOT_FILE,
2486 FILE_WRITE_ATTRIBUTES);
2488 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2490 /* Eventually save off posix specific response info and timestaps */
2493 free_rsp_buf(resp_buftype, rsp);
2496 cifs_small_buf_release(req);
2503 SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2504 struct cifs_open_parms *oparms, __le16 *path)
2506 struct TCP_Server_Info *server = tcon->ses->server;
2507 struct smb2_create_req *req;
2508 unsigned int n_iov = 2;
2509 __u32 file_attributes = 0;
2512 unsigned int total_len;
2513 struct kvec *iov = rqst->rq_iov;
2517 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2521 iov[0].iov_base = (char *)req;
2522 /* -1 since last byte is buf[0] which is sent below (path) */
2523 iov[0].iov_len = total_len - 1;
2525 if (oparms->create_options & CREATE_OPTION_READONLY)
2526 file_attributes |= ATTR_READONLY;
2527 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2528 file_attributes |= ATTR_SYSTEM;
2530 req->ImpersonationLevel = IL_IMPERSONATION;
2531 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2532 /* File attributes ignored on open (used in create though) */
2533 req->FileAttributes = cpu_to_le32(file_attributes);
2534 req->ShareAccess = FILE_SHARE_ALL_LE;
2536 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2537 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2538 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2540 /* [MS-SMB2] 2.2.13 NameOffset:
2541 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2542 * the SMB2 header, the file name includes a prefix that will
2543 * be processed during DFS name normalization as specified in
2544 * section 3.3.5.9. Otherwise, the file name is relative to
2545 * the share that is identified by the TreeId in the SMB2
2548 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2551 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2552 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2554 tcon->treeName, path);
2557 req->NameLength = cpu_to_le16(name_len * 2);
2558 uni_path_len = copy_size;
2561 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2562 /* MUST set path len (NameLength) to 0 opening root of share */
2563 req->NameLength = cpu_to_le16(uni_path_len - 2);
2564 copy_size = uni_path_len;
2565 if (copy_size % 8 != 0)
2566 copy_size = roundup(copy_size, 8);
2567 copy_path = kzalloc(copy_size, GFP_KERNEL);
2570 memcpy((char *)copy_path, (const char *)path,
2572 uni_path_len = copy_size;
2576 iov[1].iov_len = uni_path_len;
2577 iov[1].iov_base = path;
2579 if ((!server->oplocks) || (tcon->no_lease))
2580 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2582 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2583 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2584 req->RequestedOplockLevel = *oplock;
2585 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2586 (oparms->create_options & CREATE_NOT_FILE))
2587 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2589 rc = add_lease_context(server, iov, &n_iov,
2590 oparms->fid->lease_key, oplock);
2595 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2596 /* need to set Next field of lease context if we request it */
2597 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2598 struct create_context *ccontext =
2599 (struct create_context *)iov[n_iov-1].iov_base;
2601 cpu_to_le32(server->vals->create_lease_size);
2604 rc = add_durable_context(iov, &n_iov, oparms,
2605 tcon->use_persistent);
2610 if (tcon->posix_extensions) {
2612 struct create_context *ccontext =
2613 (struct create_context *)iov[n_iov-1].iov_base;
2615 cpu_to_le32(iov[n_iov-1].iov_len);
2618 rc = add_posix_context(iov, &n_iov, oparms->mode);
2623 if (tcon->snapshot_time) {
2624 cifs_dbg(FYI, "adding snapshot context\n");
2626 struct create_context *ccontext =
2627 (struct create_context *)iov[n_iov-1].iov_base;
2629 cpu_to_le32(iov[n_iov-1].iov_len);
2632 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2637 if ((oparms->disposition != FILE_OPEN) &&
2638 (oparms->cifs_sb) &&
2639 (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
2640 (oparms->mode != ACL_NO_MODE)) {
2642 struct create_context *ccontext =
2643 (struct create_context *)iov[n_iov-1].iov_base;
2645 cpu_to_le32(iov[n_iov-1].iov_len);
2648 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2649 rc = add_sd_context(iov, &n_iov, oparms->mode);
2655 struct create_context *ccontext =
2656 (struct create_context *)iov[n_iov-1].iov_base;
2657 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2659 add_query_id_context(iov, &n_iov);
2661 rqst->rq_nvec = n_iov;
2665 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2666 * All other vectors are freed by kfree().
2669 SMB2_open_free(struct smb_rqst *rqst)
2673 if (rqst && rqst->rq_iov) {
2674 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2675 for (i = 1; i < rqst->rq_nvec; i++)
2676 if (rqst->rq_iov[i].iov_base != smb2_padding)
2677 kfree(rqst->rq_iov[i].iov_base);
2682 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2683 __u8 *oplock, struct smb2_file_all_info *buf,
2684 struct kvec *err_iov, int *buftype)
2686 struct smb_rqst rqst;
2687 struct smb2_create_rsp *rsp = NULL;
2688 struct TCP_Server_Info *server;
2689 struct cifs_tcon *tcon = oparms->tcon;
2690 struct cifs_ses *ses = tcon->ses;
2691 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2692 struct kvec rsp_iov = {NULL, 0};
2693 int resp_buftype = CIFS_NO_BUFFER;
2697 cifs_dbg(FYI, "create/open\n");
2698 if (ses && (ses->server))
2699 server = ses->server;
2703 if (smb3_encryption_required(tcon))
2704 flags |= CIFS_TRANSFORM_REQ;
2706 memset(&rqst, 0, sizeof(struct smb_rqst));
2707 memset(&iov, 0, sizeof(iov));
2709 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2711 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2715 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2716 oparms->create_options, oparms->desired_access);
2718 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2720 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2723 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2724 if (err_iov && rsp) {
2726 *buftype = resp_buftype;
2727 resp_buftype = CIFS_NO_BUFFER;
2730 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2731 oparms->create_options, oparms->desired_access, rc);
2732 if (rc == -EREMCHG) {
2733 printk_once(KERN_WARNING "server share %s deleted\n",
2735 tcon->need_reconnect = true;
2739 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2740 ses->Suid, oparms->create_options,
2741 oparms->desired_access);
2743 atomic_inc(&tcon->num_remote_opens);
2744 oparms->fid->persistent_fid = rsp->PersistentFileId;
2745 oparms->fid->volatile_fid = rsp->VolatileFileId;
2746 #ifdef CONFIG_CIFS_DEBUG2
2747 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2748 #endif /* CIFS_DEBUG2 */
2751 memcpy(buf, &rsp->CreationTime, 32);
2752 buf->AllocationSize = rsp->AllocationSize;
2753 buf->EndOfFile = rsp->EndofFile;
2754 buf->Attributes = rsp->FileAttributes;
2755 buf->NumberOfLinks = cpu_to_le32(1);
2756 buf->DeletePending = 0;
2760 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2761 oparms->fid->lease_key, oplock, buf);
2763 SMB2_open_free(&rqst);
2764 free_rsp_buf(resp_buftype, rsp);
2769 SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2770 u64 persistent_fid, u64 volatile_fid, u32 opcode,
2771 bool is_fsctl, char *in_data, u32 indatalen,
2772 __u32 max_response_size)
2774 struct smb2_ioctl_req *req;
2775 struct kvec *iov = rqst->rq_iov;
2776 unsigned int total_len;
2780 rc = smb2_ioctl_req_init(opcode, tcon, (void **) &req, &total_len);
2786 * indatalen is usually small at a couple of bytes max, so
2787 * just allocate through generic pool
2789 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
2791 cifs_small_buf_release(req);
2796 req->CtlCode = cpu_to_le32(opcode);
2797 req->PersistentFileId = persistent_fid;
2798 req->VolatileFileId = volatile_fid;
2800 iov[0].iov_base = (char *)req;
2802 * If no input data, the size of ioctl struct in
2803 * protocol spec still includes a 1 byte data buffer,
2804 * but if input data passed to ioctl, we do not
2805 * want to double count this, so we do not send
2806 * the dummy one byte of data in iovec[0] if sending
2807 * input data (in iovec[1]).
2810 req->InputCount = cpu_to_le32(indatalen);
2811 /* do not set InputOffset if no input data */
2813 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
2815 iov[0].iov_len = total_len - 1;
2816 iov[1].iov_base = in_data_buf;
2817 iov[1].iov_len = indatalen;
2820 iov[0].iov_len = total_len;
2823 req->OutputOffset = 0;
2824 req->OutputCount = 0; /* MBZ */
2827 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2828 * We Could increase default MaxOutputResponse, but that could require
2829 * more credits. Windows typically sets this smaller, but for some
2830 * ioctls it may be useful to allow server to send more. No point
2831 * limiting what the server can send as long as fits in one credit
2832 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2833 * to increase this limit up in the future.
2834 * Note that for snapshot queries that servers like Azure expect that
2835 * the first query be minimal size (and just used to get the number/size
2836 * of previous versions) so response size must be specified as EXACTLY
2837 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2838 * of eight bytes. Currently that is the only case where we set max
2839 * response size smaller.
2841 req->MaxOutputResponse = cpu_to_le32(max_response_size);
2844 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2848 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2849 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
2850 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
2856 SMB2_ioctl_free(struct smb_rqst *rqst)
2859 if (rqst && rqst->rq_iov) {
2860 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2861 for (i = 1; i < rqst->rq_nvec; i++)
2862 if (rqst->rq_iov[i].iov_base != smb2_padding)
2863 kfree(rqst->rq_iov[i].iov_base);
2869 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2872 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2873 u64 volatile_fid, u32 opcode, bool is_fsctl,
2874 char *in_data, u32 indatalen, u32 max_out_data_len,
2875 char **out_data, u32 *plen /* returned data len */)
2877 struct smb_rqst rqst;
2878 struct smb2_ioctl_rsp *rsp = NULL;
2879 struct cifs_ses *ses;
2880 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
2881 struct kvec rsp_iov = {NULL, 0};
2882 int resp_buftype = CIFS_NO_BUFFER;
2885 struct TCP_Server_Info *server;
2887 cifs_dbg(FYI, "SMB2 IOCTL\n");
2889 if (out_data != NULL)
2892 /* zero out returned data len, in case of error */
2903 server = ses->server;
2907 if (smb3_encryption_required(tcon))
2908 flags |= CIFS_TRANSFORM_REQ;
2910 memset(&rqst, 0, sizeof(struct smb_rqst));
2911 memset(&iov, 0, sizeof(iov));
2913 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
2915 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
2916 is_fsctl, in_data, indatalen, max_out_data_len);
2920 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2922 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
2925 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2926 ses->Suid, 0, opcode, rc);
2928 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
2929 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2931 } else if (rc == -EINVAL) {
2932 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2933 (opcode != FSCTL_SRV_COPYCHUNK)) {
2934 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2937 } else if (rc == -E2BIG) {
2938 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
2939 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2944 /* check if caller wants to look at return data or just return rc */
2945 if ((plen == NULL) || (out_data == NULL))
2948 *plen = le32_to_cpu(rsp->OutputCount);
2950 /* We check for obvious errors in the output buffer length and offset */
2952 goto ioctl_exit; /* server returned no data */
2953 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
2954 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2960 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
2961 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2962 le32_to_cpu(rsp->OutputOffset));
2968 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2970 if (*out_data == NULL) {
2976 SMB2_ioctl_free(&rqst);
2977 free_rsp_buf(resp_buftype, rsp);
2982 * Individual callers to ioctl worker function follow
2986 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2987 u64 persistent_fid, u64 volatile_fid)
2990 struct compress_ioctl fsctl_input;
2991 char *ret_data = NULL;
2993 fsctl_input.CompressionState =
2994 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
2996 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2997 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2998 (char *)&fsctl_input /* data input */,
2999 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3000 &ret_data /* out data */, NULL);
3002 cifs_dbg(FYI, "set compression rc %d\n", rc);
3008 SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3009 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
3011 struct smb2_close_req *req;
3012 struct kvec *iov = rqst->rq_iov;
3013 unsigned int total_len;
3016 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
3020 req->PersistentFileId = persistent_fid;
3021 req->VolatileFileId = volatile_fid;
3023 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3026 iov[0].iov_base = (char *)req;
3027 iov[0].iov_len = total_len;
3033 SMB2_close_free(struct smb_rqst *rqst)
3035 if (rqst && rqst->rq_iov)
3036 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3040 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3041 u64 persistent_fid, u64 volatile_fid,
3042 struct smb2_file_network_open_info *pbuf)
3044 struct smb_rqst rqst;
3045 struct smb2_close_rsp *rsp = NULL;
3046 struct cifs_ses *ses = tcon->ses;
3048 struct kvec rsp_iov;
3049 int resp_buftype = CIFS_NO_BUFFER;
3052 bool query_attrs = false;
3054 cifs_dbg(FYI, "Close\n");
3056 if (!ses || !(ses->server))
3059 if (smb3_encryption_required(tcon))
3060 flags |= CIFS_TRANSFORM_REQ;
3062 memset(&rqst, 0, sizeof(struct smb_rqst));
3063 memset(&iov, 0, sizeof(iov));
3067 /* check if need to ask server to return timestamps in close response */
3071 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3072 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid,
3077 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3078 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
3081 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
3082 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3086 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3089 * Note that have to subtract 4 since struct network_open_info
3090 * has a final 4 byte pad that close response does not have
3093 memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3096 atomic_dec(&tcon->num_remote_opens);
3098 SMB2_close_free(&rqst);
3099 free_rsp_buf(resp_buftype, rsp);
3101 /* retry close in a worker thread if this one is interrupted */
3105 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3108 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3109 persistent_fid, tmp_rc);
3115 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3116 u64 persistent_fid, u64 volatile_fid)
3118 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3122 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3123 struct kvec *iov, unsigned int min_buf_size)
3125 unsigned int smb_len = iov->iov_len;
3126 char *end_of_smb = smb_len + (char *)iov->iov_base;
3127 char *begin_of_buf = offset + (char *)iov->iov_base;
3128 char *end_of_buf = begin_of_buf + buffer_length;
3131 if (buffer_length < min_buf_size) {
3132 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3133 buffer_length, min_buf_size);
3137 /* check if beyond RFC1001 maximum length */
3138 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3139 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3140 buffer_length, smb_len);
3144 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3145 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
3153 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3154 * Caller must free buffer.
3157 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3158 struct kvec *iov, unsigned int minbufsize,
3161 char *begin_of_buf = offset + (char *)iov->iov_base;
3167 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3171 memcpy(data, begin_of_buf, buffer_length);
3177 SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3178 u64 persistent_fid, u64 volatile_fid,
3179 u8 info_class, u8 info_type, u32 additional_info,
3180 size_t output_len, size_t input_len, void *input)
3182 struct smb2_query_info_req *req;
3183 struct kvec *iov = rqst->rq_iov;
3184 unsigned int total_len;
3187 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3192 req->InfoType = info_type;
3193 req->FileInfoClass = info_class;
3194 req->PersistentFileId = persistent_fid;
3195 req->VolatileFileId = volatile_fid;
3196 req->AdditionalInformation = cpu_to_le32(additional_info);
3198 req->OutputBufferLength = cpu_to_le32(output_len);
3200 req->InputBufferLength = cpu_to_le32(input_len);
3201 /* total_len for smb query request never close to le16 max */
3202 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3203 memcpy(req->Buffer, input, input_len);
3206 iov[0].iov_base = (char *)req;
3208 iov[0].iov_len = total_len - 1 + input_len;
3213 SMB2_query_info_free(struct smb_rqst *rqst)
3215 if (rqst && rqst->rq_iov)
3216 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3220 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3221 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3222 u32 additional_info, size_t output_len, size_t min_len, void **data,
3225 struct smb_rqst rqst;
3226 struct smb2_query_info_rsp *rsp = NULL;
3228 struct kvec rsp_iov;
3230 int resp_buftype = CIFS_NO_BUFFER;
3231 struct cifs_ses *ses = tcon->ses;
3232 struct TCP_Server_Info *server;
3234 bool allocated = false;
3236 cifs_dbg(FYI, "Query Info\n");
3240 server = ses->server;
3244 if (smb3_encryption_required(tcon))
3245 flags |= CIFS_TRANSFORM_REQ;
3247 memset(&rqst, 0, sizeof(struct smb_rqst));
3248 memset(&iov, 0, sizeof(iov));
3252 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
3253 info_class, info_type, additional_info,
3254 output_len, 0, NULL);
3258 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3259 ses->Suid, info_class, (__u32)info_type);
3261 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3262 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3265 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3266 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3267 ses->Suid, info_class, (__u32)info_type, rc);
3271 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3272 ses->Suid, info_class, (__u32)info_type);
3275 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3277 *data = kmalloc(*dlen, GFP_KERNEL);
3280 "Error %d allocating memory for acl\n",
3290 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3291 le32_to_cpu(rsp->OutputBufferLength),
3292 &rsp_iov, min_len, *data);
3293 if (rc && allocated) {
3300 SMB2_query_info_free(&rqst);
3301 free_rsp_buf(resp_buftype, rsp);
3305 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3306 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3308 return query_info(xid, tcon, persistent_fid, volatile_fid,
3309 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3310 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3311 sizeof(struct smb2_file_all_info), (void **)&data,
3316 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3317 u64 persistent_fid, u64 volatile_fid,
3318 void **data, u32 *plen)
3320 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3323 return query_info(xid, tcon, persistent_fid, volatile_fid,
3324 0, SMB2_O_INFO_SECURITY, additional_info,
3325 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3329 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3330 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3332 return query_info(xid, tcon, persistent_fid, volatile_fid,
3333 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3334 sizeof(struct smb2_file_internal_info),
3335 sizeof(struct smb2_file_internal_info),
3336 (void **)&uniqueid, NULL);
3340 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3341 * See MS-SMB2 2.2.35 and 2.2.36
3345 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3346 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid,
3347 u32 completion_filter, bool watch_tree)
3349 struct smb2_change_notify_req *req;
3350 struct kvec *iov = rqst->rq_iov;
3351 unsigned int total_len;
3354 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, (void **) &req, &total_len);
3358 req->PersistentFileId = persistent_fid;
3359 req->VolatileFileId = volatile_fid;
3360 req->OutputBufferLength =
3361 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3362 req->CompletionFilter = cpu_to_le32(completion_filter);
3364 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3368 iov[0].iov_base = (char *)req;
3369 iov[0].iov_len = total_len;
3375 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3376 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3377 u32 completion_filter)
3379 struct cifs_ses *ses = tcon->ses;
3380 struct smb_rqst rqst;
3382 struct kvec rsp_iov = {NULL, 0};
3383 int resp_buftype = CIFS_NO_BUFFER;
3387 cifs_dbg(FYI, "change notify\n");
3388 if (!ses || !(ses->server))
3391 if (smb3_encryption_required(tcon))
3392 flags |= CIFS_TRANSFORM_REQ;
3394 memset(&rqst, 0, sizeof(struct smb_rqst));
3395 memset(&iov, 0, sizeof(iov));
3399 rc = SMB2_notify_init(xid, &rqst, tcon, persistent_fid, volatile_fid,
3400 completion_filter, watch_tree);
3404 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3405 (u8)watch_tree, completion_filter);
3406 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3409 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3410 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3411 (u8)watch_tree, completion_filter, rc);
3413 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3414 ses->Suid, (u8)watch_tree, completion_filter);
3418 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3419 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3426 * This is a no-op for now. We're not really interested in the reply, but
3427 * rather in the fact that the server sent one and that server->lstrp
3430 * FIXME: maybe we should consider checking that the reply matches request?
3433 smb2_echo_callback(struct mid_q_entry *mid)
3435 struct TCP_Server_Info *server = mid->callback_data;
3436 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3437 struct cifs_credits credits = { .value = 0, .instance = 0 };
3439 if (mid->mid_state == MID_RESPONSE_RECEIVED
3440 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3441 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3442 credits.instance = server->reconnect_instance;
3445 DeleteMidQEntry(mid);
3446 add_credits(server, &credits, CIFS_ECHO_OP);
3449 void smb2_reconnect_server(struct work_struct *work)
3451 struct TCP_Server_Info *server = container_of(work,
3452 struct TCP_Server_Info, reconnect.work);
3453 struct cifs_ses *ses;
3454 struct cifs_tcon *tcon, *tcon2;
3455 struct list_head tmp_list;
3456 int tcon_exist = false;
3458 int resched = false;
3461 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3462 mutex_lock(&server->reconnect_mutex);
3464 INIT_LIST_HEAD(&tmp_list);
3465 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3467 spin_lock(&cifs_tcp_ses_lock);
3468 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3469 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3470 if (tcon->need_reconnect || tcon->need_reopen_files) {
3472 list_add_tail(&tcon->rlist, &tmp_list);
3477 * IPC has the same lifetime as its session and uses its
3480 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3481 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3487 * Get the reference to server struct to be sure that the last call of
3488 * cifs_put_tcon() in the loop below won't release the server pointer.
3491 server->srv_count++;
3493 spin_unlock(&cifs_tcp_ses_lock);
3495 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3496 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3498 cifs_reopen_persistent_handles(tcon);
3501 list_del_init(&tcon->rlist);
3503 cifs_put_smb_ses(tcon->ses);
3505 cifs_put_tcon(tcon);
3508 cifs_dbg(FYI, "Reconnecting tcons finished\n");
3510 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3511 mutex_unlock(&server->reconnect_mutex);
3513 /* now we can safely release srv struct */
3515 cifs_put_tcp_session(server, 1);
3519 SMB2_echo(struct TCP_Server_Info *server)
3521 struct smb2_echo_req *req;
3524 struct smb_rqst rqst = { .rq_iov = iov,
3526 unsigned int total_len;
3528 cifs_dbg(FYI, "In echo request\n");
3530 if (server->tcpStatus == CifsNeedNegotiate) {
3531 /* No need to send echo on newly established connections */
3532 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3536 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
3540 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3542 iov[0].iov_len = total_len;
3543 iov[0].iov_base = (char *)req;
3545 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3546 server, CIFS_ECHO_OP, NULL);
3548 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3550 cifs_small_buf_release(req);
3555 SMB2_flush_free(struct smb_rqst *rqst)
3557 if (rqst && rqst->rq_iov)
3558 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3562 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3563 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid)
3565 struct smb2_flush_req *req;
3566 struct kvec *iov = rqst->rq_iov;
3567 unsigned int total_len;
3570 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
3574 req->PersistentFileId = persistent_fid;
3575 req->VolatileFileId = volatile_fid;
3577 iov[0].iov_base = (char *)req;
3578 iov[0].iov_len = total_len;
3584 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3587 struct cifs_ses *ses = tcon->ses;
3588 struct smb_rqst rqst;
3590 struct kvec rsp_iov = {NULL, 0};
3591 int resp_buftype = CIFS_NO_BUFFER;
3595 cifs_dbg(FYI, "flush\n");
3596 if (!ses || !(ses->server))
3599 if (smb3_encryption_required(tcon))
3600 flags |= CIFS_TRANSFORM_REQ;
3602 memset(&rqst, 0, sizeof(struct smb_rqst));
3603 memset(&iov, 0, sizeof(iov));
3607 rc = SMB2_flush_init(xid, &rqst, tcon, persistent_fid, volatile_fid);
3611 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3612 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3615 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
3616 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3619 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3623 SMB2_flush_free(&rqst);
3624 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3629 * To form a chain of read requests, any read requests after the first should
3630 * have the end_of_chain boolean set to true.
3633 smb2_new_read_req(void **buf, unsigned int *total_len,
3634 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3635 unsigned int remaining_bytes, int request_type)
3638 struct smb2_read_plain_req *req = NULL;
3639 struct smb2_sync_hdr *shdr;
3640 struct TCP_Server_Info *server;
3642 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3647 server = io_parms->tcon->ses->server;
3649 return -ECONNABORTED;
3651 shdr = &req->sync_hdr;
3652 shdr->ProcessId = cpu_to_le32(io_parms->pid);
3654 req->PersistentFileId = io_parms->persistent_fid;
3655 req->VolatileFileId = io_parms->volatile_fid;
3656 req->ReadChannelInfoOffset = 0; /* reserved */
3657 req->ReadChannelInfoLength = 0; /* reserved */
3658 req->Channel = 0; /* reserved */
3659 req->MinimumCount = 0;
3660 req->Length = cpu_to_le32(io_parms->length);
3661 req->Offset = cpu_to_le64(io_parms->offset);
3663 trace_smb3_read_enter(0 /* xid */,
3664 io_parms->persistent_fid,
3665 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3666 io_parms->offset, io_parms->length);
3667 #ifdef CONFIG_CIFS_SMB_DIRECT
3669 * If we want to do a RDMA write, fill in and append
3670 * smbd_buffer_descriptor_v1 to the end of read request
3672 if (server->rdma && rdata && !server->sign &&
3673 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
3675 struct smbd_buffer_descriptor_v1 *v1;
3676 bool need_invalidate =
3677 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3679 rdata->mr = smbd_register_mr(
3680 server->smbd_conn, rdata->pages,
3681 rdata->nr_pages, rdata->page_offset,
3682 rdata->tailsz, true, need_invalidate);
3686 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3687 if (need_invalidate)
3688 req->Channel = SMB2_CHANNEL_RDMA_V1;
3689 req->ReadChannelInfoOffset =
3690 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
3691 req->ReadChannelInfoLength =
3692 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3693 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3694 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3695 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3696 v1->length = cpu_to_le32(rdata->mr->mr->length);
3698 *total_len += sizeof(*v1) - 1;
3701 if (request_type & CHAINED_REQUEST) {
3702 if (!(request_type & END_OF_CHAIN)) {
3703 /* next 8-byte aligned request */
3704 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3705 shdr->NextCommand = cpu_to_le32(*total_len);
3706 } else /* END_OF_CHAIN */
3707 shdr->NextCommand = 0;
3708 if (request_type & RELATED_REQUEST) {
3709 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
3711 * Related requests use info from previous read request
3714 shdr->SessionId = 0xFFFFFFFF;
3715 shdr->TreeId = 0xFFFFFFFF;
3716 req->PersistentFileId = 0xFFFFFFFF;
3717 req->VolatileFileId = 0xFFFFFFFF;
3720 if (remaining_bytes > io_parms->length)
3721 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3723 req->RemainingBytes = 0;
3730 smb2_readv_callback(struct mid_q_entry *mid)
3732 struct cifs_readdata *rdata = mid->callback_data;
3733 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3734 struct TCP_Server_Info *server = tcon->ses->server;
3735 struct smb2_sync_hdr *shdr =
3736 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
3737 struct cifs_credits credits = { .value = 0, .instance = 0 };
3738 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
3740 .rq_pages = rdata->pages,
3741 .rq_offset = rdata->page_offset,
3742 .rq_npages = rdata->nr_pages,
3743 .rq_pagesz = rdata->pagesz,
3744 .rq_tailsz = rdata->tailsz };
3746 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3747 __func__, mid->mid, mid->mid_state, rdata->result,
3750 switch (mid->mid_state) {
3751 case MID_RESPONSE_RECEIVED:
3752 credits.value = le16_to_cpu(shdr->CreditRequest);
3753 credits.instance = server->reconnect_instance;
3754 /* result already set, check signature */
3755 if (server->sign && !mid->decrypted) {
3758 rc = smb2_verify_signature(&rqst, server);
3760 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
3763 /* FIXME: should this be counted toward the initiating task? */
3764 task_io_account_read(rdata->got_bytes);
3765 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3767 case MID_REQUEST_SUBMITTED:
3768 case MID_RETRY_NEEDED:
3769 rdata->result = -EAGAIN;
3770 if (server->sign && rdata->got_bytes)
3771 /* reset bytes number since we can not check a sign */
3772 rdata->got_bytes = 0;
3773 /* FIXME: should this be counted toward the initiating task? */
3774 task_io_account_read(rdata->got_bytes);
3775 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3777 case MID_RESPONSE_MALFORMED:
3778 credits.value = le16_to_cpu(shdr->CreditRequest);
3779 credits.instance = server->reconnect_instance;
3782 rdata->result = -EIO;
3784 #ifdef CONFIG_CIFS_SMB_DIRECT
3786 * If this rdata has a memmory registered, the MR can be freed
3787 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3788 * because they have limited number and are used for future I/Os
3791 smbd_deregister_mr(rdata->mr);
3795 if (rdata->result && rdata->result != -ENODATA) {
3796 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
3797 trace_smb3_read_err(0 /* xid */,
3798 rdata->cfile->fid.persistent_fid,
3799 tcon->tid, tcon->ses->Suid, rdata->offset,
3800 rdata->bytes, rdata->result);
3802 trace_smb3_read_done(0 /* xid */,
3803 rdata->cfile->fid.persistent_fid,
3804 tcon->tid, tcon->ses->Suid,
3805 rdata->offset, rdata->got_bytes);
3807 queue_work(cifsiod_wq, &rdata->work);
3808 DeleteMidQEntry(mid);
3809 add_credits(server, &credits, 0);
3812 /* smb2_async_readv - send an async read, and set up mid to handle result */
3814 smb2_async_readv(struct cifs_readdata *rdata)
3818 struct smb2_sync_hdr *shdr;
3819 struct cifs_io_parms io_parms;
3820 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3822 struct TCP_Server_Info *server;
3823 unsigned int total_len;
3825 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3826 __func__, rdata->offset, rdata->bytes);
3828 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3829 io_parms.offset = rdata->offset;
3830 io_parms.length = rdata->bytes;
3831 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3832 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3833 io_parms.pid = rdata->pid;
3835 server = io_parms.tcon->ses->server;
3837 rc = smb2_new_read_req(
3838 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
3842 if (smb3_encryption_required(io_parms.tcon))
3843 flags |= CIFS_TRANSFORM_REQ;
3845 rdata->iov[0].iov_base = buf;
3846 rdata->iov[0].iov_len = total_len;
3848 shdr = (struct smb2_sync_hdr *)buf;
3850 if (rdata->credits.value > 0) {
3851 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
3852 SMB2_MAX_BUFFER_SIZE));
3853 shdr->CreditRequest =
3854 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
3856 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3858 goto async_readv_out;
3860 flags |= CIFS_HAS_CREDITS;
3863 kref_get(&rdata->refcount);
3864 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
3865 cifs_readv_receive, smb2_readv_callback,
3866 smb3_handle_read_data, rdata, flags,
3869 kref_put(&rdata->refcount, cifs_readdata_release);
3870 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
3871 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3873 io_parms.tcon->ses->Suid,
3874 io_parms.offset, io_parms.length, rc);
3878 cifs_small_buf_release(buf);
3883 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3884 unsigned int *nbytes, char **buf, int *buf_type)
3886 struct smb_rqst rqst;
3887 int resp_buftype, rc;
3888 struct smb2_read_plain_req *req = NULL;
3889 struct smb2_read_rsp *rsp = NULL;
3891 struct kvec rsp_iov;
3892 unsigned int total_len;
3893 int flags = CIFS_LOG_ERROR;
3894 struct cifs_ses *ses = io_parms->tcon->ses;
3897 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
3901 if (smb3_encryption_required(io_parms->tcon))
3902 flags |= CIFS_TRANSFORM_REQ;
3904 iov[0].iov_base = (char *)req;
3905 iov[0].iov_len = total_len;
3907 memset(&rqst, 0, sizeof(struct smb_rqst));
3911 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3912 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
3915 if (rc != -ENODATA) {
3916 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3917 cifs_dbg(VFS, "Send error in read = %d\n", rc);
3918 trace_smb3_read_err(xid, req->PersistentFileId,
3919 io_parms->tcon->tid, ses->Suid,
3920 io_parms->offset, io_parms->length,
3923 trace_smb3_read_done(xid, req->PersistentFileId,
3924 io_parms->tcon->tid, ses->Suid,
3925 io_parms->offset, 0);
3926 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3927 cifs_small_buf_release(req);
3928 return rc == -ENODATA ? 0 : rc;
3930 trace_smb3_read_done(xid, req->PersistentFileId,
3931 io_parms->tcon->tid, ses->Suid,
3932 io_parms->offset, io_parms->length);
3934 cifs_small_buf_release(req);
3936 *nbytes = le32_to_cpu(rsp->DataLength);
3937 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3938 (*nbytes > io_parms->length)) {
3939 cifs_dbg(FYI, "bad length %d for count %d\n",
3940 *nbytes, io_parms->length);
3946 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
3947 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3948 } else if (resp_buftype != CIFS_NO_BUFFER) {
3949 *buf = rsp_iov.iov_base;
3950 if (resp_buftype == CIFS_SMALL_BUFFER)
3951 *buf_type = CIFS_SMALL_BUFFER;
3952 else if (resp_buftype == CIFS_LARGE_BUFFER)
3953 *buf_type = CIFS_LARGE_BUFFER;
3959 * Check the mid_state and signature on received buffer (if any), and queue the
3960 * workqueue completion task.
3963 smb2_writev_callback(struct mid_q_entry *mid)
3965 struct cifs_writedata *wdata = mid->callback_data;
3966 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3967 struct TCP_Server_Info *server = tcon->ses->server;
3968 unsigned int written;
3969 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
3970 struct cifs_credits credits = { .value = 0, .instance = 0 };
3972 switch (mid->mid_state) {
3973 case MID_RESPONSE_RECEIVED:
3974 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3975 credits.instance = server->reconnect_instance;
3976 wdata->result = smb2_check_receive(mid, server, 0);
3977 if (wdata->result != 0)
3980 written = le32_to_cpu(rsp->DataLength);
3982 * Mask off high 16 bits when bytes written as returned
3983 * by the server is greater than bytes requested by the
3984 * client. OS/2 servers are known to set incorrect
3987 if (written > wdata->bytes)
3990 if (written < wdata->bytes)
3991 wdata->result = -ENOSPC;
3993 wdata->bytes = written;
3995 case MID_REQUEST_SUBMITTED:
3996 case MID_RETRY_NEEDED:
3997 wdata->result = -EAGAIN;
3999 case MID_RESPONSE_MALFORMED:
4000 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4001 credits.instance = server->reconnect_instance;
4004 wdata->result = -EIO;
4007 #ifdef CONFIG_CIFS_SMB_DIRECT
4009 * If this wdata has a memory registered, the MR can be freed
4010 * The number of MRs available is limited, it's important to recover
4011 * used MR as soon as I/O is finished. Hold MR longer in the later
4012 * I/O process can possibly result in I/O deadlock due to lack of MR
4013 * to send request on I/O retry
4016 smbd_deregister_mr(wdata->mr);
4020 if (wdata->result) {
4021 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4022 trace_smb3_write_err(0 /* no xid */,
4023 wdata->cfile->fid.persistent_fid,
4024 tcon->tid, tcon->ses->Suid, wdata->offset,
4025 wdata->bytes, wdata->result);
4027 trace_smb3_write_done(0 /* no xid */,
4028 wdata->cfile->fid.persistent_fid,
4029 tcon->tid, tcon->ses->Suid,
4030 wdata->offset, wdata->bytes);
4032 queue_work(cifsiod_wq, &wdata->work);
4033 DeleteMidQEntry(mid);
4034 add_credits(server, &credits, 0);
4037 /* smb2_async_writev - send an async write, and set up mid to handle result */
4039 smb2_async_writev(struct cifs_writedata *wdata,
4040 void (*release)(struct kref *kref))
4042 int rc = -EACCES, flags = 0;
4043 struct smb2_write_req *req = NULL;
4044 struct smb2_sync_hdr *shdr;
4045 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4046 struct TCP_Server_Info *server = tcon->ses->server;
4048 struct smb_rqst rqst = { };
4049 unsigned int total_len;
4051 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
4055 if (smb3_encryption_required(tcon))
4056 flags |= CIFS_TRANSFORM_REQ;
4058 shdr = (struct smb2_sync_hdr *)req;
4059 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
4061 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
4062 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
4063 req->WriteChannelInfoOffset = 0;
4064 req->WriteChannelInfoLength = 0;
4066 req->Offset = cpu_to_le64(wdata->offset);
4067 req->DataOffset = cpu_to_le16(
4068 offsetof(struct smb2_write_req, Buffer));
4069 req->RemainingBytes = 0;
4071 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
4072 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
4073 #ifdef CONFIG_CIFS_SMB_DIRECT
4075 * If we want to do a server RDMA read, fill in and append
4076 * smbd_buffer_descriptor_v1 to the end of write request
4078 if (server->rdma && !server->sign && wdata->bytes >=
4079 server->smbd_conn->rdma_readwrite_threshold) {
4081 struct smbd_buffer_descriptor_v1 *v1;
4082 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4084 wdata->mr = smbd_register_mr(
4085 server->smbd_conn, wdata->pages,
4086 wdata->nr_pages, wdata->page_offset,
4087 wdata->tailsz, false, need_invalidate);
4090 goto async_writev_out;
4093 req->DataOffset = 0;
4094 if (wdata->nr_pages > 1)
4095 req->RemainingBytes =
4097 (wdata->nr_pages - 1) * wdata->pagesz -
4098 wdata->page_offset + wdata->tailsz
4101 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
4102 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4103 if (need_invalidate)
4104 req->Channel = SMB2_CHANNEL_RDMA_V1;
4105 req->WriteChannelInfoOffset =
4106 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4107 req->WriteChannelInfoLength =
4108 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4109 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4110 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4111 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4112 v1->length = cpu_to_le32(wdata->mr->mr->length);
4115 iov[0].iov_len = total_len - 1;
4116 iov[0].iov_base = (char *)req;
4120 rqst.rq_pages = wdata->pages;
4121 rqst.rq_offset = wdata->page_offset;
4122 rqst.rq_npages = wdata->nr_pages;
4123 rqst.rq_pagesz = wdata->pagesz;
4124 rqst.rq_tailsz = wdata->tailsz;
4125 #ifdef CONFIG_CIFS_SMB_DIRECT
4127 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4131 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4132 wdata->offset, wdata->bytes);
4134 #ifdef CONFIG_CIFS_SMB_DIRECT
4135 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4137 req->Length = cpu_to_le32(wdata->bytes);
4139 req->Length = cpu_to_le32(wdata->bytes);
4142 if (wdata->credits.value > 0) {
4143 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4144 SMB2_MAX_BUFFER_SIZE));
4145 shdr->CreditRequest =
4146 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
4148 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4150 goto async_writev_out;
4152 flags |= CIFS_HAS_CREDITS;
4155 kref_get(&wdata->refcount);
4156 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4157 wdata, flags, &wdata->credits);
4160 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
4161 tcon->tid, tcon->ses->Suid, wdata->offset,
4163 kref_put(&wdata->refcount, release);
4164 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4168 cifs_small_buf_release(req);
4173 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4174 * The length field from io_parms must be at least 1 and indicates a number of
4175 * elements with data to write that begins with position 1 in iov array. All
4176 * data length is specified by count.
4179 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4180 unsigned int *nbytes, struct kvec *iov, int n_vec)
4182 struct smb_rqst rqst;
4184 struct smb2_write_req *req = NULL;
4185 struct smb2_write_rsp *rsp = NULL;
4187 struct kvec rsp_iov;
4189 unsigned int total_len;
4196 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
4201 if (io_parms->tcon->ses->server == NULL)
4202 return -ECONNABORTED;
4204 if (smb3_encryption_required(io_parms->tcon))
4205 flags |= CIFS_TRANSFORM_REQ;
4207 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
4209 req->PersistentFileId = io_parms->persistent_fid;
4210 req->VolatileFileId = io_parms->volatile_fid;
4211 req->WriteChannelInfoOffset = 0;
4212 req->WriteChannelInfoLength = 0;
4214 req->Length = cpu_to_le32(io_parms->length);
4215 req->Offset = cpu_to_le64(io_parms->offset);
4216 req->DataOffset = cpu_to_le16(
4217 offsetof(struct smb2_write_req, Buffer));
4218 req->RemainingBytes = 0;
4220 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4221 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4222 io_parms->offset, io_parms->length);
4224 iov[0].iov_base = (char *)req;
4226 iov[0].iov_len = total_len - 1;
4228 memset(&rqst, 0, sizeof(struct smb_rqst));
4230 rqst.rq_nvec = n_vec + 1;
4232 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
4233 &resp_buftype, flags, &rsp_iov);
4234 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
4237 trace_smb3_write_err(xid, req->PersistentFileId,
4238 io_parms->tcon->tid,
4239 io_parms->tcon->ses->Suid,
4240 io_parms->offset, io_parms->length, rc);
4241 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
4242 cifs_dbg(VFS, "Send error in write = %d\n", rc);
4244 *nbytes = le32_to_cpu(rsp->DataLength);
4245 trace_smb3_write_done(xid, req->PersistentFileId,
4246 io_parms->tcon->tid,
4247 io_parms->tcon->ses->Suid,
4248 io_parms->offset, *nbytes);
4251 cifs_small_buf_release(req);
4252 free_rsp_buf(resp_buftype, rsp);
4257 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
4260 unsigned int entrycount = 0;
4261 unsigned int next_offset = 0;
4263 FILE_DIRECTORY_INFO *dir_info;
4265 if (bufstart == NULL)
4268 entryptr = bufstart;
4271 if (entryptr + next_offset < entryptr ||
4272 entryptr + next_offset > end_of_buf ||
4273 entryptr + next_offset + size > end_of_buf) {
4274 cifs_dbg(VFS, "malformed search entry would overflow\n");
4278 entryptr = entryptr + next_offset;
4279 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4281 len = le32_to_cpu(dir_info->FileNameLength);
4282 if (entryptr + len < entryptr ||
4283 entryptr + len > end_of_buf ||
4284 entryptr + len + size > end_of_buf) {
4285 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4290 *lastentry = entryptr;
4293 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
4304 int SMB2_query_directory_init(const unsigned int xid,
4305 struct cifs_tcon *tcon, struct smb_rqst *rqst,
4306 u64 persistent_fid, u64 volatile_fid,
4307 int index, int info_level)
4309 struct TCP_Server_Info *server = tcon->ses->server;
4310 struct smb2_query_directory_req *req;
4311 unsigned char *bufptr;
4312 __le16 asteriks = cpu_to_le16('*');
4313 unsigned int output_size = CIFSMaxBufSize -
4314 MAX_SMB2_CREATE_RESPONSE_SIZE -
4315 MAX_SMB2_CLOSE_RESPONSE_SIZE;
4316 unsigned int total_len;
4317 struct kvec *iov = rqst->rq_iov;
4320 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
4325 switch (info_level) {
4326 case SMB_FIND_FILE_DIRECTORY_INFO:
4327 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4329 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4330 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4333 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4338 req->FileIndex = cpu_to_le32(index);
4339 req->PersistentFileId = persistent_fid;
4340 req->VolatileFileId = volatile_fid;
4343 bufptr = req->Buffer;
4344 memcpy(bufptr, &asteriks, len);
4346 req->FileNameOffset =
4347 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
4348 req->FileNameLength = cpu_to_le16(len);
4350 * BB could be 30 bytes or so longer if we used SMB2 specific
4351 * buffer lengths, but this is safe and close enough.
4353 output_size = min_t(unsigned int, output_size, server->maxBuf);
4354 output_size = min_t(unsigned int, output_size, 2 << 15);
4355 req->OutputBufferLength = cpu_to_le32(output_size);
4357 iov[0].iov_base = (char *)req;
4359 iov[0].iov_len = total_len - 1;
4361 iov[1].iov_base = (char *)(req->Buffer);
4362 iov[1].iov_len = len;
4364 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4365 tcon->ses->Suid, index, output_size);
4370 void SMB2_query_directory_free(struct smb_rqst *rqst)
4372 if (rqst && rqst->rq_iov) {
4373 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4378 smb2_parse_query_directory(struct cifs_tcon *tcon,
4379 struct kvec *rsp_iov,
4381 struct cifs_search_info *srch_inf)
4383 struct smb2_query_directory_rsp *rsp;
4384 size_t info_buf_size;
4388 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
4390 switch (srch_inf->info_level) {
4391 case SMB_FIND_FILE_DIRECTORY_INFO:
4392 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4394 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4395 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4398 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4399 srch_inf->info_level);
4403 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4404 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
4409 srch_inf->unicode = true;
4411 if (srch_inf->ntwrk_buf_start) {
4412 if (srch_inf->smallBuf)
4413 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4415 cifs_buf_release(srch_inf->ntwrk_buf_start);
4417 srch_inf->ntwrk_buf_start = (char *)rsp;
4418 srch_inf->srch_entries_start = srch_inf->last_entry =
4419 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4420 end_of_smb = rsp_iov->iov_len + (char *)rsp;
4421 srch_inf->entries_in_buffer =
4422 num_entries(srch_inf->srch_entries_start, end_of_smb,
4423 &srch_inf->last_entry, info_buf_size);
4424 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4425 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4426 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4427 srch_inf->srch_entries_start, srch_inf->last_entry);
4428 if (resp_buftype == CIFS_LARGE_BUFFER)
4429 srch_inf->smallBuf = false;
4430 else if (resp_buftype == CIFS_SMALL_BUFFER)
4431 srch_inf->smallBuf = true;
4433 cifs_tcon_dbg(VFS, "illegal search buffer type\n");
4439 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4440 u64 persistent_fid, u64 volatile_fid, int index,
4441 struct cifs_search_info *srch_inf)
4443 struct smb_rqst rqst;
4444 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
4445 struct smb2_query_directory_rsp *rsp = NULL;
4446 int resp_buftype = CIFS_NO_BUFFER;
4447 struct kvec rsp_iov;
4449 struct cifs_ses *ses = tcon->ses;
4452 if (!ses || !(ses->server))
4455 if (smb3_encryption_required(tcon))
4456 flags |= CIFS_TRANSFORM_REQ;
4458 memset(&rqst, 0, sizeof(struct smb_rqst));
4459 memset(&iov, 0, sizeof(iov));
4461 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
4463 rc = SMB2_query_directory_init(xid, tcon, &rqst, persistent_fid,
4464 volatile_fid, index,
4465 srch_inf->info_level);
4469 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4470 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
4473 if (rc == -ENODATA &&
4474 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
4475 trace_smb3_query_dir_done(xid, persistent_fid,
4476 tcon->tid, tcon->ses->Suid, index, 0);
4477 srch_inf->endOfSearch = true;
4480 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4481 tcon->ses->Suid, index, 0, rc);
4482 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
4487 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
4490 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4491 tcon->ses->Suid, index, 0, rc);
4494 resp_buftype = CIFS_NO_BUFFER;
4496 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4497 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
4500 SMB2_query_directory_free(&rqst);
4501 free_rsp_buf(resp_buftype, rsp);
4506 SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
4507 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4508 u8 info_type, u32 additional_info,
4509 void **data, unsigned int *size)
4511 struct smb2_set_info_req *req;
4512 struct kvec *iov = rqst->rq_iov;
4513 unsigned int i, total_len;
4516 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
4520 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4521 req->InfoType = info_type;
4522 req->FileInfoClass = info_class;
4523 req->PersistentFileId = persistent_fid;
4524 req->VolatileFileId = volatile_fid;
4525 req->AdditionalInformation = cpu_to_le32(additional_info);
4528 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
4529 req->BufferLength = cpu_to_le32(*size);
4531 memcpy(req->Buffer, *data, *size);
4534 iov[0].iov_base = (char *)req;
4536 iov[0].iov_len = total_len - 1;
4538 for (i = 1; i < rqst->rq_nvec; i++) {
4539 le32_add_cpu(&req->BufferLength, size[i]);
4540 iov[i].iov_base = (char *)data[i];
4541 iov[i].iov_len = size[i];
4548 SMB2_set_info_free(struct smb_rqst *rqst)
4550 if (rqst && rqst->rq_iov)
4551 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
4555 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4556 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4557 u8 info_type, u32 additional_info, unsigned int num,
4558 void **data, unsigned int *size)
4560 struct smb_rqst rqst;
4561 struct smb2_set_info_rsp *rsp = NULL;
4563 struct kvec rsp_iov;
4566 struct cifs_ses *ses = tcon->ses;
4569 if (!ses || !(ses->server))
4575 if (smb3_encryption_required(tcon))
4576 flags |= CIFS_TRANSFORM_REQ;
4578 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4582 memset(&rqst, 0, sizeof(struct smb_rqst));
4586 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4587 info_class, info_type, additional_info,
4595 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
4597 SMB2_set_info_free(&rqst);
4598 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
4601 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
4602 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4603 ses->Suid, info_class, (__u32)info_type, rc);
4606 free_rsp_buf(resp_buftype, rsp);
4612 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4613 u64 volatile_fid, u32 pid, __le64 *eof)
4615 struct smb2_file_eof_info info;
4619 info.EndOfFile = *eof;
4622 size = sizeof(struct smb2_file_eof_info);
4624 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4625 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4626 0, 1, &data, &size);
4630 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4631 u64 persistent_fid, u64 volatile_fid,
4632 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4634 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4635 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4636 1, (void **)&pnntsd, &pacllen);
4640 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4641 u64 persistent_fid, u64 volatile_fid,
4642 struct smb2_file_full_ea_info *buf, int len)
4644 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4645 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4646 0, 1, (void **)&buf, &len);
4650 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4651 const u64 persistent_fid, const u64 volatile_fid,
4654 struct smb_rqst rqst;
4656 struct smb2_oplock_break *req = NULL;
4657 struct cifs_ses *ses = tcon->ses;
4658 int flags = CIFS_OBREAK_OP;
4659 unsigned int total_len;
4661 struct kvec rsp_iov;
4664 cifs_dbg(FYI, "SMB2_oplock_break\n");
4665 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4670 if (smb3_encryption_required(tcon))
4671 flags |= CIFS_TRANSFORM_REQ;
4673 req->VolatileFid = volatile_fid;
4674 req->PersistentFid = persistent_fid;
4675 req->OplockLevel = oplock_level;
4676 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4678 flags |= CIFS_NO_RSP_BUF;
4680 iov[0].iov_base = (char *)req;
4681 iov[0].iov_len = total_len;
4683 memset(&rqst, 0, sizeof(struct smb_rqst));
4687 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4688 cifs_small_buf_release(req);
4691 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4692 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
4699 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4700 struct kstatfs *kst)
4702 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4703 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4704 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
4705 kst->f_bfree = kst->f_bavail =
4706 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
4711 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4712 struct kstatfs *kst)
4714 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4715 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4716 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4717 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4718 kst->f_bavail = kst->f_bfree;
4720 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4721 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4722 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4723 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4724 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4730 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4731 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4734 struct smb2_query_info_req *req;
4735 unsigned int total_len;
4737 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
4739 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4742 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4747 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4748 req->FileInfoClass = level;
4749 req->PersistentFileId = persistent_fid;
4750 req->VolatileFileId = volatile_fid;
4752 req->InputBufferOffset =
4753 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
4754 req->OutputBufferLength = cpu_to_le32(
4755 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
4757 iov->iov_base = (char *)req;
4758 iov->iov_len = total_len;
4763 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4764 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4766 struct smb_rqst rqst;
4767 struct smb2_query_info_rsp *rsp = NULL;
4769 struct kvec rsp_iov;
4772 struct cifs_ses *ses = tcon->ses;
4773 FILE_SYSTEM_POSIX_INFO *info = NULL;
4776 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4777 sizeof(FILE_SYSTEM_POSIX_INFO),
4778 persistent_fid, volatile_fid);
4782 if (smb3_encryption_required(tcon))
4783 flags |= CIFS_TRANSFORM_REQ;
4785 memset(&rqst, 0, sizeof(struct smb_rqst));
4789 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4790 cifs_small_buf_release(iov.iov_base);
4792 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4793 goto posix_qfsinf_exit;
4795 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4797 info = (FILE_SYSTEM_POSIX_INFO *)(
4798 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4799 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4800 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4801 sizeof(FILE_SYSTEM_POSIX_INFO));
4803 copy_posix_fs_info_to_kstatfs(info, fsdata);
4806 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4811 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4812 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4814 struct smb_rqst rqst;
4815 struct smb2_query_info_rsp *rsp = NULL;
4817 struct kvec rsp_iov;
4820 struct cifs_ses *ses = tcon->ses;
4821 struct smb2_fs_full_size_info *info = NULL;
4824 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4825 sizeof(struct smb2_fs_full_size_info),
4826 persistent_fid, volatile_fid);
4830 if (smb3_encryption_required(tcon))
4831 flags |= CIFS_TRANSFORM_REQ;
4833 memset(&rqst, 0, sizeof(struct smb_rqst));
4837 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4838 cifs_small_buf_release(iov.iov_base);
4840 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4843 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4845 info = (struct smb2_fs_full_size_info *)(
4846 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4847 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4848 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4849 sizeof(struct smb2_fs_full_size_info));
4851 smb2_copy_fs_info_to_kstatfs(info, fsdata);
4854 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4859 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
4860 u64 persistent_fid, u64 volatile_fid, int level)
4862 struct smb_rqst rqst;
4863 struct smb2_query_info_rsp *rsp = NULL;
4865 struct kvec rsp_iov;
4867 int resp_buftype, max_len, min_len;
4868 struct cifs_ses *ses = tcon->ses;
4869 unsigned int rsp_len, offset;
4872 if (level == FS_DEVICE_INFORMATION) {
4873 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4874 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4875 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4876 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4877 min_len = MIN_FS_ATTR_INFO_SIZE;
4878 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4879 max_len = sizeof(struct smb3_fs_ss_info);
4880 min_len = sizeof(struct smb3_fs_ss_info);
4881 } else if (level == FS_VOLUME_INFORMATION) {
4882 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4883 min_len = sizeof(struct smb3_fs_vol_info);
4885 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
4889 rc = build_qfs_info_req(&iov, tcon, level, max_len,
4890 persistent_fid, volatile_fid);
4894 if (smb3_encryption_required(tcon))
4895 flags |= CIFS_TRANSFORM_REQ;
4897 memset(&rqst, 0, sizeof(struct smb_rqst));
4901 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4902 cifs_small_buf_release(iov.iov_base);
4904 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4907 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4909 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4910 offset = le16_to_cpu(rsp->OutputBufferOffset);
4911 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
4915 if (level == FS_ATTRIBUTE_INFORMATION)
4916 memcpy(&tcon->fsAttrInfo, offset
4917 + (char *)rsp, min_t(unsigned int,
4919 else if (level == FS_DEVICE_INFORMATION)
4920 memcpy(&tcon->fsDevInfo, offset
4921 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
4922 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4923 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
4924 (offset + (char *)rsp);
4925 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4926 tcon->perf_sector_size =
4927 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
4928 } else if (level == FS_VOLUME_INFORMATION) {
4929 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4930 (offset + (char *)rsp);
4931 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4932 tcon->vol_create_time = vol_info->VolumeCreationTime;
4936 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4941 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4942 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4943 const __u32 num_lock, struct smb2_lock_element *buf)
4945 struct smb_rqst rqst;
4947 struct smb2_lock_req *req = NULL;
4949 struct kvec rsp_iov;
4952 int flags = CIFS_NO_RSP_BUF;
4953 unsigned int total_len;
4955 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
4957 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
4961 if (smb3_encryption_required(tcon))
4962 flags |= CIFS_TRANSFORM_REQ;
4964 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4965 req->LockCount = cpu_to_le16(num_lock);
4967 req->PersistentFileId = persist_fid;
4968 req->VolatileFileId = volatile_fid;
4970 count = num_lock * sizeof(struct smb2_lock_element);
4972 iov[0].iov_base = (char *)req;
4973 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
4974 iov[1].iov_base = (char *)buf;
4975 iov[1].iov_len = count;
4977 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
4979 memset(&rqst, 0, sizeof(struct smb_rqst));
4983 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
4985 cifs_small_buf_release(req);
4987 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
4988 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
4989 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4990 tcon->ses->Suid, rc);
4997 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4998 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4999 const __u64 length, const __u64 offset, const __u32 lock_flags,
5002 struct smb2_lock_element lock;
5004 lock.Offset = cpu_to_le64(offset);
5005 lock.Length = cpu_to_le64(length);
5006 lock.Flags = cpu_to_le32(lock_flags);
5007 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5008 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5010 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5014 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5015 __u8 *lease_key, const __le32 lease_state)
5017 struct smb_rqst rqst;
5019 struct smb2_lease_ack *req = NULL;
5020 struct cifs_ses *ses = tcon->ses;
5021 int flags = CIFS_OBREAK_OP;
5022 unsigned int total_len;
5024 struct kvec rsp_iov;
5026 __u64 *please_key_high;
5027 __u64 *please_key_low;
5029 cifs_dbg(FYI, "SMB2_lease_break\n");
5030 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
5035 if (smb3_encryption_required(tcon))
5036 flags |= CIFS_TRANSFORM_REQ;
5038 req->sync_hdr.CreditRequest = cpu_to_le16(1);
5039 req->StructureSize = cpu_to_le16(36);
5042 memcpy(req->LeaseKey, lease_key, 16);
5043 req->LeaseState = lease_state;
5045 flags |= CIFS_NO_RSP_BUF;
5047 iov[0].iov_base = (char *)req;
5048 iov[0].iov_len = total_len;
5050 memset(&rqst, 0, sizeof(struct smb_rqst));
5054 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
5055 cifs_small_buf_release(req);
5057 please_key_low = (__u64 *)lease_key;
5058 please_key_high = (__u64 *)(lease_key+8);
5060 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5061 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5062 ses->Suid, *please_key_low, *please_key_high, rc);
5063 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
5065 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5066 ses->Suid, *please_key_low, *please_key_high);