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)
315 if (retries && --retries)
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);
355 if ((rc == -EACCES) && !tcon->retry) {
357 mutex_unlock(&tcon->ses->session_mutex);
361 if (rc || !tcon->need_reconnect) {
362 mutex_unlock(&tcon->ses->session_mutex);
366 cifs_mark_open_files_invalid(tcon);
367 if (tcon->use_persistent)
368 tcon->need_reopen_files = true;
370 rc = __smb2_reconnect(nls_codepage, tcon);
371 mutex_unlock(&tcon->ses->session_mutex);
373 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
375 /* If sess reconnected but tcon didn't, something strange ... */
376 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
380 if (smb2_command != SMB2_INTERNAL_CMD)
381 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
383 atomic_inc(&tconInfoReconnectCount);
386 * Check if handle based operation so we know whether we can continue
387 * or not without returning to caller to reset file handle.
390 * BB Is flush done by server on drop of tcp session? Should we special
391 * case it and skip above?
393 switch (smb2_command) {
399 case SMB2_QUERY_DIRECTORY:
400 case SMB2_CHANGE_NOTIFY:
401 case SMB2_QUERY_INFO:
406 unload_nls(nls_codepage);
411 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
412 unsigned int *total_len)
414 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
415 /* lookup word count ie StructureSize from table */
416 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
419 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
420 * largest operations (Create)
424 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
425 spdu->StructureSize2 = cpu_to_le16(parmsize);
427 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
431 * Allocate and return pointer to an SMB request hdr, and set basic
432 * SMB information in the SMB header. If the return code is zero, this
433 * function must have filled in request_buf pointer.
435 static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
436 void **request_buf, unsigned int *total_len)
438 /* BB eventually switch this to SMB2 specific small buf size */
439 if (smb2_command == SMB2_SET_INFO)
440 *request_buf = cifs_buf_get();
442 *request_buf = cifs_small_buf_get();
443 if (*request_buf == NULL) {
444 /* BB should we add a retry in here if not a writepage? */
448 fill_small_buf(smb2_command, tcon,
449 (struct smb2_sync_hdr *)(*request_buf),
453 uint16_t com_code = le16_to_cpu(smb2_command);
454 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
455 cifs_stats_inc(&tcon->num_smbs_sent);
461 static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
462 void **request_buf, unsigned int *total_len)
466 rc = smb2_reconnect(smb2_command, tcon);
470 return __smb2_plain_req_init(smb2_command, tcon, request_buf,
474 static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
475 void **request_buf, unsigned int *total_len)
477 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
478 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
479 return __smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf,
482 return smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf, total_len);
485 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
488 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
490 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
491 pneg_ctxt->DataLength = cpu_to_le16(38);
492 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
493 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
494 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
495 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
499 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
501 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
502 pneg_ctxt->DataLength =
503 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
504 - sizeof(struct smb2_neg_context));
505 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
506 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
507 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
508 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
512 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
514 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
515 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + two ciphers */
516 pneg_ctxt->CipherCount = cpu_to_le16(2);
517 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
518 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
522 build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
524 struct nls_table *cp = load_nls_default();
526 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
528 /* copy up to max of first 100 bytes of server name to NetName field */
529 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
530 /* context size is DataLength + minimal smb2_neg_context */
531 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
532 sizeof(struct smb2_neg_context), 8) * 8;
536 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
538 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
539 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
540 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
541 pneg_ctxt->Name[0] = 0x93;
542 pneg_ctxt->Name[1] = 0xAD;
543 pneg_ctxt->Name[2] = 0x25;
544 pneg_ctxt->Name[3] = 0x50;
545 pneg_ctxt->Name[4] = 0x9C;
546 pneg_ctxt->Name[5] = 0xB4;
547 pneg_ctxt->Name[6] = 0x11;
548 pneg_ctxt->Name[7] = 0xE7;
549 pneg_ctxt->Name[8] = 0xB4;
550 pneg_ctxt->Name[9] = 0x23;
551 pneg_ctxt->Name[10] = 0x83;
552 pneg_ctxt->Name[11] = 0xDE;
553 pneg_ctxt->Name[12] = 0x96;
554 pneg_ctxt->Name[13] = 0x8B;
555 pneg_ctxt->Name[14] = 0xCD;
556 pneg_ctxt->Name[15] = 0x7C;
560 assemble_neg_contexts(struct smb2_negotiate_req *req,
561 struct TCP_Server_Info *server, unsigned int *total_len)
564 unsigned int ctxt_len;
566 if (*total_len > 200) {
567 /* In case length corrupted don't want to overrun smb buffer */
568 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
573 * round up total_len of fixed part of SMB3 negotiate request to 8
574 * byte boundary before adding negotiate contexts
576 *total_len = roundup(*total_len, 8);
578 pneg_ctxt = (*total_len) + (char *)req;
579 req->NegotiateContextOffset = cpu_to_le32(*total_len);
581 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
582 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
583 *total_len += ctxt_len;
584 pneg_ctxt += ctxt_len;
586 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
587 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
588 *total_len += ctxt_len;
589 pneg_ctxt += ctxt_len;
591 if (server->compress_algorithm) {
592 build_compression_ctxt((struct smb2_compression_capabilities_context *)
594 ctxt_len = DIV_ROUND_UP(
595 sizeof(struct smb2_compression_capabilities_context),
597 *total_len += ctxt_len;
598 pneg_ctxt += ctxt_len;
599 req->NegotiateContextCount = cpu_to_le16(5);
601 req->NegotiateContextCount = cpu_to_le16(4);
603 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
605 *total_len += ctxt_len;
606 pneg_ctxt += ctxt_len;
608 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
609 *total_len += sizeof(struct smb2_posix_neg_context);
612 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
614 unsigned int len = le16_to_cpu(ctxt->DataLength);
616 /* If invalid preauth context warn but use what we requested, SHA-512 */
617 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
618 printk_once(KERN_WARNING "server sent bad preauth context\n");
621 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
622 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
623 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
624 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
627 static void decode_compress_ctx(struct TCP_Server_Info *server,
628 struct smb2_compression_capabilities_context *ctxt)
630 unsigned int len = le16_to_cpu(ctxt->DataLength);
632 /* sizeof compress context is a one element compression capbility struct */
634 printk_once(KERN_WARNING "server sent bad compression cntxt\n");
637 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
638 printk_once(KERN_WARNING "illegal SMB3 compress algorithm count\n");
641 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
642 printk_once(KERN_WARNING "unknown compression algorithm\n");
645 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
648 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
649 struct smb2_encryption_neg_context *ctxt)
651 unsigned int len = le16_to_cpu(ctxt->DataLength);
653 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
654 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
655 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
659 if (le16_to_cpu(ctxt->CipherCount) != 1) {
660 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
663 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
664 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
665 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
666 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
669 server->cipher_type = ctxt->Ciphers[0];
670 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
674 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
675 struct TCP_Server_Info *server,
676 unsigned int len_of_smb)
678 struct smb2_neg_context *pctx;
679 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
680 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
681 unsigned int len_of_ctxts, i;
684 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
685 if (len_of_smb <= offset) {
686 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
690 len_of_ctxts = len_of_smb - offset;
692 for (i = 0; i < ctxt_cnt; i++) {
694 /* check that offset is not beyond end of SMB */
695 if (len_of_ctxts == 0)
698 if (len_of_ctxts < sizeof(struct smb2_neg_context))
701 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
702 clen = le16_to_cpu(pctx->DataLength);
703 if (clen > len_of_ctxts)
706 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
707 decode_preauth_context(
708 (struct smb2_preauth_neg_context *)pctx);
709 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
710 rc = decode_encrypt_ctx(server,
711 (struct smb2_encryption_neg_context *)pctx);
712 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
713 decode_compress_ctx(server,
714 (struct smb2_compression_capabilities_context *)pctx);
715 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
716 server->posix_ext_supported = true;
718 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
719 le16_to_cpu(pctx->ContextType));
723 /* offsets must be 8 byte aligned */
724 clen = (clen + 7) & ~0x7;
725 offset += clen + sizeof(struct smb2_neg_context);
726 len_of_ctxts -= clen;
731 static struct create_posix *
732 create_posix_buf(umode_t mode)
734 struct create_posix *buf;
736 buf = kzalloc(sizeof(struct create_posix),
741 buf->ccontext.DataOffset =
742 cpu_to_le16(offsetof(struct create_posix, Mode));
743 buf->ccontext.DataLength = cpu_to_le32(4);
744 buf->ccontext.NameOffset =
745 cpu_to_le16(offsetof(struct create_posix, Name));
746 buf->ccontext.NameLength = cpu_to_le16(16);
748 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
759 buf->Name[10] = 0x83;
760 buf->Name[11] = 0xDE;
761 buf->Name[12] = 0x96;
762 buf->Name[13] = 0x8B;
763 buf->Name[14] = 0xCD;
764 buf->Name[15] = 0x7C;
765 buf->Mode = cpu_to_le32(mode);
766 cifs_dbg(FYI, "mode on posix create 0%o", mode);
771 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
773 struct smb2_create_req *req = iov[0].iov_base;
774 unsigned int num = *num_iovec;
776 iov[num].iov_base = create_posix_buf(mode);
777 if (mode == ACL_NO_MODE)
778 cifs_dbg(FYI, "illegal mode\n");
779 if (iov[num].iov_base == NULL)
781 iov[num].iov_len = sizeof(struct create_posix);
782 if (!req->CreateContextsOffset)
783 req->CreateContextsOffset = cpu_to_le32(
784 sizeof(struct smb2_create_req) +
785 iov[num - 1].iov_len);
786 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
787 *num_iovec = num + 1;
794 * SMB2 Worker functions follow:
796 * The general structure of the worker functions is:
797 * 1) Call smb2_init (assembles SMB2 header)
798 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
799 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
800 * 4) Decode SMB2 command specific fields in the fixed length area
801 * 5) Decode variable length data area (if any for this SMB2 command type)
802 * 6) Call free smb buffer
808 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
810 struct smb_rqst rqst;
811 struct smb2_negotiate_req *req;
812 struct smb2_negotiate_rsp *rsp;
817 struct TCP_Server_Info *server = cifs_ses_server(ses);
818 int blob_offset, blob_length;
820 int flags = CIFS_NEG_OP;
821 unsigned int total_len;
823 cifs_dbg(FYI, "Negotiate protocol\n");
826 WARN(1, "%s: server is NULL!\n", __func__);
830 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
834 req->sync_hdr.SessionId = 0;
836 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
837 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
839 if (strcmp(server->vals->version_string,
840 SMB3ANY_VERSION_STRING) == 0) {
841 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
842 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
843 req->DialectCount = cpu_to_le16(2);
845 } else if (strcmp(server->vals->version_string,
846 SMBDEFAULT_VERSION_STRING) == 0) {
847 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
848 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
849 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
850 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
851 req->DialectCount = cpu_to_le16(4);
854 /* otherwise send specific dialect */
855 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
856 req->DialectCount = cpu_to_le16(1);
860 /* only one of SMB2 signing flags may be set in SMB2 request */
862 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
863 else if (global_secflags & CIFSSEC_MAY_SIGN)
864 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
866 req->SecurityMode = 0;
868 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
870 /* ClientGUID must be zero for SMB2.02 dialect */
871 if (server->vals->protocol_id == SMB20_PROT_ID)
872 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
874 memcpy(req->ClientGUID, server->client_guid,
875 SMB2_CLIENT_GUID_SIZE);
876 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
877 (strcmp(server->vals->version_string,
878 SMBDEFAULT_VERSION_STRING) == 0))
879 assemble_neg_contexts(req, server, &total_len);
881 iov[0].iov_base = (char *)req;
882 iov[0].iov_len = total_len;
884 memset(&rqst, 0, sizeof(struct smb_rqst));
888 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
889 cifs_small_buf_release(req);
890 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
892 * No tcon so can't do
893 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
895 if (rc == -EOPNOTSUPP) {
896 cifs_server_dbg(VFS, "Dialect not supported by server. Consider "
897 "specifying vers=1.0 or vers=2.0 on mount for accessing"
903 if (strcmp(server->vals->version_string,
904 SMB3ANY_VERSION_STRING) == 0) {
905 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
907 "SMB2 dialect returned but not requested\n");
909 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
911 "SMB2.1 dialect returned but not requested\n");
914 } else if (strcmp(server->vals->version_string,
915 SMBDEFAULT_VERSION_STRING) == 0) {
916 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
918 "SMB2 dialect returned but not requested\n");
920 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
921 /* ops set to 3.0 by default for default so update */
922 server->ops = &smb21_operations;
923 server->vals = &smb21_values;
924 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
925 server->ops = &smb311_operations;
926 server->vals = &smb311_values;
928 } else if (le16_to_cpu(rsp->DialectRevision) !=
929 server->vals->protocol_id) {
930 /* if requested single dialect ensure returned dialect matched */
931 cifs_server_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
932 le16_to_cpu(rsp->DialectRevision));
936 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
938 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
939 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
940 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
941 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
942 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
943 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
944 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
945 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
946 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
947 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
949 cifs_server_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
950 le16_to_cpu(rsp->DialectRevision));
954 server->dialect = le16_to_cpu(rsp->DialectRevision);
957 * Keep a copy of the hash after negprot. This hash will be
958 * the starting hash value for all sessions made from this
961 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
962 SMB2_PREAUTH_HASH_SIZE);
964 /* SMB2 only has an extended negflavor */
965 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
966 /* set it to the maximum buffer size value we can send with 1 credit */
967 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
968 SMB2_MAX_BUFFER_SIZE);
969 server->max_read = le32_to_cpu(rsp->MaxReadSize);
970 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
971 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
972 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
973 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
975 server->capabilities = le32_to_cpu(rsp->Capabilities);
977 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
979 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
980 (struct smb2_sync_hdr *)rsp);
982 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
984 * ses->sectype = RawNTLMSSP;
985 * but for time being this is our only auth choice so doesn't matter.
986 * We just found a server which sets blob length to zero expecting raw.
988 if (blob_length == 0) {
989 cifs_dbg(FYI, "missing security blob on negprot\n");
990 server->sec_ntlmssp = true;
993 rc = cifs_enable_signing(server, ses->sign);
997 rc = decode_negTokenInit(security_blob, blob_length, server);
1004 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1005 if (rsp->NegotiateContextCount)
1006 rc = smb311_decode_neg_context(rsp, server,
1009 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
1012 free_rsp_buf(resp_buftype, rsp);
1016 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1019 struct validate_negotiate_info_req *pneg_inbuf;
1020 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
1022 u32 inbuflen; /* max of 4 dialects */
1023 struct TCP_Server_Info *server = tcon->ses->server;
1025 cifs_dbg(FYI, "validate negotiate\n");
1027 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1028 if (server->dialect == SMB311_PROT_ID)
1032 * validation ioctl must be signed, so no point sending this if we
1033 * can not sign it (ie are not known user). Even if signing is not
1034 * required (enabled but not negotiated), in those cases we selectively
1035 * sign just this, the first and only signed request on a connection.
1036 * Having validation of negotiate info helps reduce attack vectors.
1038 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1039 return 0; /* validation requires signing */
1041 if (tcon->ses->user_name == NULL) {
1042 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1043 return 0; /* validation requires signing */
1046 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1047 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1049 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1053 pneg_inbuf->Capabilities =
1054 cpu_to_le32(server->vals->req_capabilities);
1055 memcpy(pneg_inbuf->Guid, server->client_guid,
1056 SMB2_CLIENT_GUID_SIZE);
1058 if (tcon->ses->sign)
1059 pneg_inbuf->SecurityMode =
1060 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1061 else if (global_secflags & CIFSSEC_MAY_SIGN)
1062 pneg_inbuf->SecurityMode =
1063 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1065 pneg_inbuf->SecurityMode = 0;
1068 if (strcmp(server->vals->version_string,
1069 SMB3ANY_VERSION_STRING) == 0) {
1070 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1071 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1072 pneg_inbuf->DialectCount = cpu_to_le16(2);
1073 /* structure is big enough for 3 dialects, sending only 2 */
1074 inbuflen = sizeof(*pneg_inbuf) -
1075 (2 * sizeof(pneg_inbuf->Dialects[0]));
1076 } else if (strcmp(server->vals->version_string,
1077 SMBDEFAULT_VERSION_STRING) == 0) {
1078 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1079 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1080 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1081 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1082 pneg_inbuf->DialectCount = cpu_to_le16(4);
1083 /* structure is big enough for 3 dialects */
1084 inbuflen = sizeof(*pneg_inbuf);
1086 /* otherwise specific dialect was requested */
1087 pneg_inbuf->Dialects[0] =
1088 cpu_to_le16(server->vals->protocol_id);
1089 pneg_inbuf->DialectCount = cpu_to_le16(1);
1090 /* structure is big enough for 3 dialects, sending only 1 */
1091 inbuflen = sizeof(*pneg_inbuf) -
1092 sizeof(pneg_inbuf->Dialects[0]) * 2;
1095 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1096 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
1097 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1098 (char **)&pneg_rsp, &rsplen);
1099 if (rc == -EOPNOTSUPP) {
1101 * Old Windows versions or Netapp SMB server can return
1102 * not supported error. Client should accept it.
1104 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1106 goto out_free_inbuf;
1107 } else if (rc != 0) {
1108 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
1110 goto out_free_inbuf;
1114 if (rsplen != sizeof(*pneg_rsp)) {
1115 cifs_tcon_dbg(VFS, "invalid protocol negotiate response size: %d\n",
1118 /* relax check since Mac returns max bufsize allowed on ioctl */
1119 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1123 /* check validate negotiate info response matches what we got earlier */
1124 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1127 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1130 /* do not validate server guid because not saved at negprot time yet */
1132 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1133 SMB2_LARGE_FILES) != server->capabilities)
1136 /* validate negotiate successful */
1138 cifs_dbg(FYI, "validate negotiate info successful\n");
1142 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1151 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1153 switch (requested) {
1160 if (server->sec_ntlmssp &&
1161 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1163 if ((server->sec_kerberos || server->sec_mskerberos) &&
1164 (global_secflags & CIFSSEC_MAY_KRB5))
1172 struct SMB2_sess_data {
1174 struct cifs_ses *ses;
1175 struct nls_table *nls_cp;
1176 void (*func)(struct SMB2_sess_data *);
1178 u64 previous_session;
1180 /* we will send the SMB in three pieces:
1181 * a fixed length beginning part, an optional
1182 * SPNEGO blob (which can be zero length), and a
1183 * last part which will include the strings
1184 * and rest of bcc area. This allows us to avoid
1185 * a large buffer 17K allocation
1192 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1195 struct cifs_ses *ses = sess_data->ses;
1196 struct smb2_sess_setup_req *req;
1197 struct TCP_Server_Info *server = cifs_ses_server(ses);
1198 unsigned int total_len;
1200 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1205 if (sess_data->ses->binding) {
1206 req->sync_hdr.SessionId = sess_data->ses->Suid;
1207 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1208 req->PreviousSessionId = 0;
1209 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1211 /* First session, not a reauthenticate */
1212 req->sync_hdr.SessionId = 0;
1214 * if reconnect, we need to send previous sess id
1217 req->PreviousSessionId = sess_data->previous_session;
1218 req->Flags = 0; /* MBZ */
1221 /* enough to enable echos and oplocks and one max size write */
1222 req->sync_hdr.CreditRequest = cpu_to_le16(130);
1224 /* only one of SMB2 signing flags may be set in SMB2 request */
1226 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1227 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1228 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1230 req->SecurityMode = 0;
1232 #ifdef CONFIG_CIFS_DFS_UPCALL
1233 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1235 req->Capabilities = 0;
1236 #endif /* DFS_UPCALL */
1238 req->Channel = 0; /* MBZ */
1240 sess_data->iov[0].iov_base = (char *)req;
1242 sess_data->iov[0].iov_len = total_len - 1;
1244 * This variable will be used to clear the buffer
1245 * allocated above in case of any error in the calling function.
1247 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1253 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1255 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1256 sess_data->buf0_type = CIFS_NO_BUFFER;
1260 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1263 struct smb_rqst rqst;
1264 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1265 struct kvec rsp_iov = { NULL, 0 };
1267 /* Testing shows that buffer offset must be at location of Buffer[0] */
1268 req->SecurityBufferOffset =
1269 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1270 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1272 memset(&rqst, 0, sizeof(struct smb_rqst));
1273 rqst.rq_iov = sess_data->iov;
1276 /* BB add code to build os and lm fields */
1277 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1279 &sess_data->buf0_type,
1280 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
1281 cifs_small_buf_release(sess_data->iov[0].iov_base);
1282 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1288 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1291 struct cifs_ses *ses = sess_data->ses;
1292 struct TCP_Server_Info *server = cifs_ses_server(ses);
1294 mutex_lock(&server->srv_mutex);
1295 if (server->ops->generate_signingkey) {
1296 rc = server->ops->generate_signingkey(ses);
1299 "SMB3 session key generation failed\n");
1300 mutex_unlock(&server->srv_mutex);
1304 if (!server->session_estab) {
1305 server->sequence_number = 0x2;
1306 server->session_estab = true;
1308 mutex_unlock(&server->srv_mutex);
1310 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1311 /* keep existing ses state if binding */
1312 if (!ses->binding) {
1313 spin_lock(&GlobalMid_Lock);
1314 ses->status = CifsGood;
1315 ses->need_reconnect = false;
1316 spin_unlock(&GlobalMid_Lock);
1322 #ifdef CONFIG_CIFS_UPCALL
1324 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1327 struct cifs_ses *ses = sess_data->ses;
1328 struct cifs_spnego_msg *msg;
1329 struct key *spnego_key = NULL;
1330 struct smb2_sess_setup_rsp *rsp = NULL;
1332 rc = SMB2_sess_alloc_buffer(sess_data);
1336 spnego_key = cifs_get_spnego_key(ses);
1337 if (IS_ERR(spnego_key)) {
1338 rc = PTR_ERR(spnego_key);
1343 msg = spnego_key->payload.data[0];
1345 * check version field to make sure that cifs.upcall is
1346 * sending us a response in an expected form
1348 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1350 "bad cifs.upcall version. Expected %d got %d",
1351 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1353 goto out_put_spnego_key;
1356 /* keep session key if binding */
1357 if (!ses->binding) {
1358 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1360 if (!ses->auth_key.response) {
1362 "Kerberos can't allocate (%u bytes) memory",
1365 goto out_put_spnego_key;
1367 ses->auth_key.len = msg->sesskey_len;
1370 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1371 sess_data->iov[1].iov_len = msg->secblob_len;
1373 rc = SMB2_sess_sendreceive(sess_data);
1375 goto out_put_spnego_key;
1377 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1378 /* keep session id and flags if binding */
1379 if (!ses->binding) {
1380 ses->Suid = rsp->sync_hdr.SessionId;
1381 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1384 rc = SMB2_sess_establish_session(sess_data);
1386 key_invalidate(spnego_key);
1387 key_put(spnego_key);
1389 sess_data->result = rc;
1390 sess_data->func = NULL;
1391 SMB2_sess_free_buffer(sess_data);
1395 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1397 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1398 sess_data->result = -EOPNOTSUPP;
1399 sess_data->func = NULL;
1404 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1407 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1410 struct cifs_ses *ses = sess_data->ses;
1411 struct smb2_sess_setup_rsp *rsp = NULL;
1412 char *ntlmssp_blob = NULL;
1413 bool use_spnego = false; /* else use raw ntlmssp */
1414 u16 blob_length = 0;
1417 * If memory allocation is successful, caller of this function
1420 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1421 if (!ses->ntlmssp) {
1425 ses->ntlmssp->sesskey_per_smbsess = true;
1427 rc = SMB2_sess_alloc_buffer(sess_data);
1431 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1433 if (ntlmssp_blob == NULL) {
1438 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1440 /* BB eventually need to add this */
1441 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1445 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1446 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1448 sess_data->iov[1].iov_base = ntlmssp_blob;
1449 sess_data->iov[1].iov_len = blob_length;
1451 rc = SMB2_sess_sendreceive(sess_data);
1452 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1454 /* If true, rc here is expected and not an error */
1455 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1456 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1462 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1463 le16_to_cpu(rsp->SecurityBufferOffset)) {
1464 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1465 le16_to_cpu(rsp->SecurityBufferOffset));
1469 rc = decode_ntlmssp_challenge(rsp->Buffer,
1470 le16_to_cpu(rsp->SecurityBufferLength), ses);
1474 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1476 /* keep existing ses id and flags if binding */
1477 if (!ses->binding) {
1478 ses->Suid = rsp->sync_hdr.SessionId;
1479 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1483 kfree(ntlmssp_blob);
1484 SMB2_sess_free_buffer(sess_data);
1486 sess_data->result = 0;
1487 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1491 kfree(ses->ntlmssp);
1492 ses->ntlmssp = NULL;
1493 sess_data->result = rc;
1494 sess_data->func = NULL;
1498 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1501 struct cifs_ses *ses = sess_data->ses;
1502 struct smb2_sess_setup_req *req;
1503 struct smb2_sess_setup_rsp *rsp = NULL;
1504 unsigned char *ntlmssp_blob = NULL;
1505 bool use_spnego = false; /* else use raw ntlmssp */
1506 u16 blob_length = 0;
1508 rc = SMB2_sess_alloc_buffer(sess_data);
1512 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1513 req->sync_hdr.SessionId = ses->Suid;
1515 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1518 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1523 /* BB eventually need to add this */
1524 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1528 sess_data->iov[1].iov_base = ntlmssp_blob;
1529 sess_data->iov[1].iov_len = blob_length;
1531 rc = SMB2_sess_sendreceive(sess_data);
1535 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1537 /* keep existing ses id and flags if binding */
1538 if (!ses->binding) {
1539 ses->Suid = rsp->sync_hdr.SessionId;
1540 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1543 rc = SMB2_sess_establish_session(sess_data);
1545 kfree(ntlmssp_blob);
1546 SMB2_sess_free_buffer(sess_data);
1547 kfree(ses->ntlmssp);
1548 ses->ntlmssp = NULL;
1549 sess_data->result = rc;
1550 sess_data->func = NULL;
1554 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1558 type = smb2_select_sectype(cifs_ses_server(ses), ses->sectype);
1559 cifs_dbg(FYI, "sess setup type %d\n", type);
1560 if (type == Unspecified) {
1562 "Unable to select appropriate authentication method!");
1568 sess_data->func = SMB2_auth_kerberos;
1571 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1574 cifs_dbg(VFS, "secType %d not supported!\n", type);
1582 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1583 const struct nls_table *nls_cp)
1586 struct TCP_Server_Info *server = cifs_ses_server(ses);
1587 struct SMB2_sess_data *sess_data;
1589 cifs_dbg(FYI, "Session Setup\n");
1592 WARN(1, "%s: server is NULL!\n", __func__);
1596 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1600 rc = SMB2_select_sec(ses, sess_data);
1603 sess_data->xid = xid;
1604 sess_data->ses = ses;
1605 sess_data->buf0_type = CIFS_NO_BUFFER;
1606 sess_data->nls_cp = (struct nls_table *) nls_cp;
1607 sess_data->previous_session = ses->Suid;
1610 * Initialize the session hash with the server one.
1612 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
1613 SMB2_PREAUTH_HASH_SIZE);
1615 while (sess_data->func)
1616 sess_data->func(sess_data);
1618 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1619 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1620 rc = sess_data->result;
1627 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1629 struct smb_rqst rqst;
1630 struct smb2_logoff_req *req; /* response is also trivial struct */
1632 struct TCP_Server_Info *server;
1634 unsigned int total_len;
1636 struct kvec rsp_iov;
1639 cifs_dbg(FYI, "disconnect session %p\n", ses);
1641 if (ses && (ses->server))
1642 server = ses->server;
1646 /* no need to send SMB logoff if uid already closed due to reconnect */
1647 if (ses->need_reconnect)
1648 goto smb2_session_already_dead;
1650 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
1654 /* since no tcon, smb2_init can not do this, so do here */
1655 req->sync_hdr.SessionId = ses->Suid;
1657 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1658 flags |= CIFS_TRANSFORM_REQ;
1659 else if (server->sign)
1660 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1662 flags |= CIFS_NO_RSP_BUF;
1664 iov[0].iov_base = (char *)req;
1665 iov[0].iov_len = total_len;
1667 memset(&rqst, 0, sizeof(struct smb_rqst));
1671 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1672 cifs_small_buf_release(req);
1674 * No tcon so can't do
1675 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1678 smb2_session_already_dead:
1682 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1684 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1687 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1689 /* These are similar values to what Windows uses */
1690 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1692 tcon->max_chunks = 256;
1693 tcon->max_bytes_chunk = 1048576;
1694 tcon->max_bytes_copy = 16777216;
1698 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1699 struct cifs_tcon *tcon, const struct nls_table *cp)
1701 struct smb_rqst rqst;
1702 struct smb2_tree_connect_req *req;
1703 struct smb2_tree_connect_rsp *rsp = NULL;
1705 struct kvec rsp_iov = { NULL, 0 };
1709 __le16 *unc_path = NULL;
1711 unsigned int total_len;
1712 struct TCP_Server_Info *server = ses->server;
1714 cifs_dbg(FYI, "TCON\n");
1716 if (!server || !tree)
1719 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1720 if (unc_path == NULL)
1723 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1725 if (unc_path_len < 2) {
1730 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1732 atomic_set(&tcon->num_remote_opens, 0);
1733 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1740 if (smb3_encryption_required(tcon))
1741 flags |= CIFS_TRANSFORM_REQ;
1743 iov[0].iov_base = (char *)req;
1745 iov[0].iov_len = total_len - 1;
1747 /* Testing shows that buffer offset must be at location of Buffer[0] */
1748 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1750 req->PathLength = cpu_to_le16(unc_path_len - 2);
1751 iov[1].iov_base = unc_path;
1752 iov[1].iov_len = unc_path_len;
1755 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1756 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1757 * (Samba servers don't always set the flag so also check if null user)
1759 if ((server->dialect == SMB311_PROT_ID) &&
1760 !smb3_encryption_required(tcon) &&
1761 !(ses->session_flags &
1762 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1763 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1764 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1766 memset(&rqst, 0, sizeof(struct smb_rqst));
1770 /* Need 64 for max size write so ask for more in case not there yet */
1771 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1773 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
1774 cifs_small_buf_release(req);
1775 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1776 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1779 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1780 tcon->need_reconnect = true;
1782 goto tcon_error_exit;
1785 switch (rsp->ShareType) {
1786 case SMB2_SHARE_TYPE_DISK:
1787 cifs_dbg(FYI, "connection to disk share\n");
1789 case SMB2_SHARE_TYPE_PIPE:
1791 cifs_dbg(FYI, "connection to pipe share\n");
1793 case SMB2_SHARE_TYPE_PRINT:
1795 cifs_dbg(FYI, "connection to printer\n");
1798 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1800 goto tcon_error_exit;
1803 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1804 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1805 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1806 tcon->tidStatus = CifsGood;
1807 tcon->need_reconnect = false;
1808 tcon->tid = rsp->sync_hdr.TreeId;
1809 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1811 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1812 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1813 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
1816 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1817 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
1819 init_copy_chunk_defaults(tcon);
1820 if (server->ops->validate_negotiate)
1821 rc = server->ops->validate_negotiate(xid, tcon);
1824 free_rsp_buf(resp_buftype, rsp);
1829 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1830 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1836 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1838 struct smb_rqst rqst;
1839 struct smb2_tree_disconnect_req *req; /* response is trivial */
1841 struct cifs_ses *ses = tcon->ses;
1843 unsigned int total_len;
1845 struct kvec rsp_iov;
1848 cifs_dbg(FYI, "Tree Disconnect\n");
1850 if (!ses || !(ses->server))
1853 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1856 close_shroot_lease(&tcon->crfid);
1858 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1863 if (smb3_encryption_required(tcon))
1864 flags |= CIFS_TRANSFORM_REQ;
1866 flags |= CIFS_NO_RSP_BUF;
1868 iov[0].iov_base = (char *)req;
1869 iov[0].iov_len = total_len;
1871 memset(&rqst, 0, sizeof(struct smb_rqst));
1875 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1876 cifs_small_buf_release(req);
1878 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1884 static struct create_durable *
1885 create_durable_buf(void)
1887 struct create_durable *buf;
1889 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1893 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1894 (struct create_durable, Data));
1895 buf->ccontext.DataLength = cpu_to_le32(16);
1896 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1897 (struct create_durable, Name));
1898 buf->ccontext.NameLength = cpu_to_le16(4);
1899 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1907 static struct create_durable *
1908 create_reconnect_durable_buf(struct cifs_fid *fid)
1910 struct create_durable *buf;
1912 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1916 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1917 (struct create_durable, Data));
1918 buf->ccontext.DataLength = cpu_to_le32(16);
1919 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1920 (struct create_durable, Name));
1921 buf->ccontext.NameLength = cpu_to_le16(4);
1922 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1923 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1924 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1933 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1935 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1937 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1938 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1939 buf->IndexNumber = pdisk_id->DiskFileId;
1943 smb2_parse_contexts(struct TCP_Server_Info *server,
1944 struct smb2_create_rsp *rsp,
1945 unsigned int *epoch, char *lease_key, __u8 *oplock,
1946 struct smb2_file_all_info *buf)
1949 struct create_context *cc;
1951 unsigned int remaining;
1955 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
1956 remaining = le32_to_cpu(rsp->CreateContextsLength);
1957 cc = (struct create_context *)data_offset;
1959 /* Initialize inode number to 0 in case no valid data in qfid context */
1961 buf->IndexNumber = 0;
1963 while (remaining >= sizeof(struct create_context)) {
1964 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1965 if (le16_to_cpu(cc->NameLength) == 4 &&
1966 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
1967 *oplock = server->ops->parse_lease_buf(cc, epoch,
1969 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
1970 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
1971 parse_query_id_ctxt(cc, buf);
1973 next = le32_to_cpu(cc->Next);
1977 cc = (struct create_context *)((char *)cc + next);
1980 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
1981 *oplock = rsp->OplockLevel;
1987 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1988 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
1990 struct smb2_create_req *req = iov[0].iov_base;
1991 unsigned int num = *num_iovec;
1993 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
1994 if (iov[num].iov_base == NULL)
1996 iov[num].iov_len = server->vals->create_lease_size;
1997 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1998 if (!req->CreateContextsOffset)
1999 req->CreateContextsOffset = cpu_to_le32(
2000 sizeof(struct smb2_create_req) +
2001 iov[num - 1].iov_len);
2002 le32_add_cpu(&req->CreateContextsLength,
2003 server->vals->create_lease_size);
2004 *num_iovec = num + 1;
2008 static struct create_durable_v2 *
2009 create_durable_v2_buf(struct cifs_open_parms *oparms)
2011 struct cifs_fid *pfid = oparms->fid;
2012 struct create_durable_v2 *buf;
2014 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2018 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2019 (struct create_durable_v2, dcontext));
2020 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2021 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2022 (struct create_durable_v2, Name));
2023 buf->ccontext.NameLength = cpu_to_le16(4);
2026 * NB: Handle timeout defaults to 0, which allows server to choose
2027 * (most servers default to 120 seconds) and most clients default to 0.
2028 * This can be overridden at mount ("handletimeout=") if the user wants
2029 * a different persistent (or resilient) handle timeout for all opens
2030 * opens on a particular SMB3 mount.
2032 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2033 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2034 generate_random_uuid(buf->dcontext.CreateGuid);
2035 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2037 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2045 static struct create_durable_handle_reconnect_v2 *
2046 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2048 struct create_durable_handle_reconnect_v2 *buf;
2050 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2055 buf->ccontext.DataOffset =
2056 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2058 buf->ccontext.DataLength =
2059 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2060 buf->ccontext.NameOffset =
2061 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2063 buf->ccontext.NameLength = cpu_to_le16(4);
2065 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2066 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2067 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2068 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2070 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2079 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2080 struct cifs_open_parms *oparms)
2082 struct smb2_create_req *req = iov[0].iov_base;
2083 unsigned int num = *num_iovec;
2085 iov[num].iov_base = create_durable_v2_buf(oparms);
2086 if (iov[num].iov_base == NULL)
2088 iov[num].iov_len = sizeof(struct create_durable_v2);
2089 if (!req->CreateContextsOffset)
2090 req->CreateContextsOffset =
2091 cpu_to_le32(sizeof(struct smb2_create_req) +
2093 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
2094 *num_iovec = num + 1;
2099 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2100 struct cifs_open_parms *oparms)
2102 struct smb2_create_req *req = iov[0].iov_base;
2103 unsigned int num = *num_iovec;
2105 /* indicate that we don't need to relock the file */
2106 oparms->reconnect = false;
2108 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2109 if (iov[num].iov_base == NULL)
2111 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2112 if (!req->CreateContextsOffset)
2113 req->CreateContextsOffset =
2114 cpu_to_le32(sizeof(struct smb2_create_req) +
2116 le32_add_cpu(&req->CreateContextsLength,
2117 sizeof(struct create_durable_handle_reconnect_v2));
2118 *num_iovec = num + 1;
2123 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2124 struct cifs_open_parms *oparms, bool use_persistent)
2126 struct smb2_create_req *req = iov[0].iov_base;
2127 unsigned int num = *num_iovec;
2129 if (use_persistent) {
2130 if (oparms->reconnect)
2131 return add_durable_reconnect_v2_context(iov, num_iovec,
2134 return add_durable_v2_context(iov, num_iovec, oparms);
2137 if (oparms->reconnect) {
2138 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2139 /* indicate that we don't need to relock the file */
2140 oparms->reconnect = false;
2142 iov[num].iov_base = create_durable_buf();
2143 if (iov[num].iov_base == NULL)
2145 iov[num].iov_len = sizeof(struct create_durable);
2146 if (!req->CreateContextsOffset)
2147 req->CreateContextsOffset =
2148 cpu_to_le32(sizeof(struct smb2_create_req) +
2150 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
2151 *num_iovec = num + 1;
2155 /* See MS-SMB2 2.2.13.2.7 */
2156 static struct crt_twarp_ctxt *
2157 create_twarp_buf(__u64 timewarp)
2159 struct crt_twarp_ctxt *buf;
2161 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2165 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2166 (struct crt_twarp_ctxt, Timestamp));
2167 buf->ccontext.DataLength = cpu_to_le32(8);
2168 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2169 (struct crt_twarp_ctxt, Name));
2170 buf->ccontext.NameLength = cpu_to_le16(4);
2171 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2176 buf->Timestamp = cpu_to_le64(timewarp);
2180 /* See MS-SMB2 2.2.13.2.7 */
2182 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2184 struct smb2_create_req *req = iov[0].iov_base;
2185 unsigned int num = *num_iovec;
2187 iov[num].iov_base = create_twarp_buf(timewarp);
2188 if (iov[num].iov_base == NULL)
2190 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2191 if (!req->CreateContextsOffset)
2192 req->CreateContextsOffset = cpu_to_le32(
2193 sizeof(struct smb2_create_req) +
2194 iov[num - 1].iov_len);
2195 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2196 *num_iovec = num + 1;
2200 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2201 static struct crt_sd_ctxt *
2202 create_sd_buf(umode_t mode, unsigned int *len)
2204 struct crt_sd_ctxt *buf;
2205 struct cifs_ace *pace;
2206 unsigned int sdlen, acelen;
2208 *len = roundup(sizeof(struct crt_sd_ctxt) + sizeof(struct cifs_ace) * 2,
2210 buf = kzalloc(*len, GFP_KERNEL);
2214 sdlen = sizeof(struct smb3_sd) + sizeof(struct smb3_acl) +
2215 2 * sizeof(struct cifs_ace);
2217 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2218 (struct crt_sd_ctxt, sd));
2219 buf->ccontext.DataLength = cpu_to_le32(sdlen);
2220 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2221 (struct crt_sd_ctxt, Name));
2222 buf->ccontext.NameLength = cpu_to_le16(4);
2223 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2228 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2230 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2231 * and "DP" ie the DACL is present
2233 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2235 /* offset owner, group and Sbz1 and SACL are all zero */
2236 buf->sd.OffsetDacl = cpu_to_le32(sizeof(struct smb3_sd));
2237 buf->acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2239 /* create one ACE to hold the mode embedded in reserved special SID */
2240 pace = (struct cifs_ace *)(sizeof(struct crt_sd_ctxt) + (char *)buf);
2241 acelen = setup_special_mode_ACE(pace, (__u64)mode);
2242 /* and one more ACE to allow access for authenticated users */
2243 pace = (struct cifs_ace *)(acelen + (sizeof(struct crt_sd_ctxt) +
2245 acelen += setup_authusers_ACE(pace);
2246 buf->acl.AclSize = cpu_to_le16(sizeof(struct cifs_acl) + acelen);
2247 buf->acl.AceCount = cpu_to_le16(2);
2252 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
2254 struct smb2_create_req *req = iov[0].iov_base;
2255 unsigned int num = *num_iovec;
2256 unsigned int len = 0;
2258 iov[num].iov_base = create_sd_buf(mode, &len);
2259 if (iov[num].iov_base == NULL)
2261 iov[num].iov_len = len;
2262 if (!req->CreateContextsOffset)
2263 req->CreateContextsOffset = cpu_to_le32(
2264 sizeof(struct smb2_create_req) +
2265 iov[num - 1].iov_len);
2266 le32_add_cpu(&req->CreateContextsLength, len);
2267 *num_iovec = num + 1;
2271 static struct crt_query_id_ctxt *
2272 create_query_id_buf(void)
2274 struct crt_query_id_ctxt *buf;
2276 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2280 buf->ccontext.DataOffset = cpu_to_le16(0);
2281 buf->ccontext.DataLength = cpu_to_le32(0);
2282 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2283 (struct crt_query_id_ctxt, Name));
2284 buf->ccontext.NameLength = cpu_to_le16(4);
2285 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2293 /* See MS-SMB2 2.2.13.2.9 */
2295 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2297 struct smb2_create_req *req = iov[0].iov_base;
2298 unsigned int num = *num_iovec;
2300 iov[num].iov_base = create_query_id_buf();
2301 if (iov[num].iov_base == NULL)
2303 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2304 if (!req->CreateContextsOffset)
2305 req->CreateContextsOffset = cpu_to_le32(
2306 sizeof(struct smb2_create_req) +
2307 iov[num - 1].iov_len);
2308 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2309 *num_iovec = num + 1;
2314 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2315 const char *treename, const __le16 *path)
2317 int treename_len, path_len;
2318 struct nls_table *cp;
2319 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2324 treename_len = strlen(treename);
2325 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2331 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2334 * make room for one path separator between the treename and
2337 *out_len = treename_len + 1 + path_len;
2340 * final path needs to be null-terminated UTF16 with a
2344 *out_size = roundup((*out_len+1)*2, 8);
2345 *out_path = kzalloc(*out_size, GFP_KERNEL);
2349 cp = load_nls_default();
2350 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2351 UniStrcat(*out_path, sep);
2352 UniStrcat(*out_path, path);
2358 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2359 umode_t mode, struct cifs_tcon *tcon,
2360 const char *full_path,
2361 struct cifs_sb_info *cifs_sb)
2363 struct smb_rqst rqst;
2364 struct smb2_create_req *req;
2365 struct smb2_create_rsp *rsp = NULL;
2366 struct cifs_ses *ses = tcon->ses;
2367 struct kvec iov[3]; /* make sure at least one for each open context */
2368 struct kvec rsp_iov = {NULL, 0};
2371 __le16 *copy_path = NULL;
2374 unsigned int n_iov = 2;
2375 __u32 file_attributes = 0;
2376 char *pc_buf = NULL;
2378 unsigned int total_len;
2379 __le16 *utf16_path = NULL;
2381 cifs_dbg(FYI, "mkdir\n");
2383 /* resource #1: path allocation */
2384 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2388 if (!ses || !(ses->server)) {
2393 /* resource #2: request */
2394 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2399 if (smb3_encryption_required(tcon))
2400 flags |= CIFS_TRANSFORM_REQ;
2402 req->ImpersonationLevel = IL_IMPERSONATION;
2403 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2404 /* File attributes ignored on open (used in create though) */
2405 req->FileAttributes = cpu_to_le32(file_attributes);
2406 req->ShareAccess = FILE_SHARE_ALL_LE;
2407 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2408 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2410 iov[0].iov_base = (char *)req;
2411 /* -1 since last byte is buf[0] which is sent below (path) */
2412 iov[0].iov_len = total_len - 1;
2414 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2416 /* [MS-SMB2] 2.2.13 NameOffset:
2417 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2418 * the SMB2 header, the file name includes a prefix that will
2419 * be processed during DFS name normalization as specified in
2420 * section 3.3.5.9. Otherwise, the file name is relative to
2421 * the share that is identified by the TreeId in the SMB2
2424 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2427 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2428 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2430 tcon->treeName, utf16_path);
2434 req->NameLength = cpu_to_le16(name_len * 2);
2435 uni_path_len = copy_size;
2436 /* free before overwriting resource */
2438 utf16_path = copy_path;
2440 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2441 /* MUST set path len (NameLength) to 0 opening root of share */
2442 req->NameLength = cpu_to_le16(uni_path_len - 2);
2443 if (uni_path_len % 8 != 0) {
2444 copy_size = roundup(uni_path_len, 8);
2445 copy_path = kzalloc(copy_size, GFP_KERNEL);
2450 memcpy((char *)copy_path, (const char *)utf16_path,
2452 uni_path_len = copy_size;
2453 /* free before overwriting resource */
2455 utf16_path = copy_path;
2459 iov[1].iov_len = uni_path_len;
2460 iov[1].iov_base = utf16_path;
2461 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2463 if (tcon->posix_extensions) {
2464 /* resource #3: posix buf */
2465 rc = add_posix_context(iov, &n_iov, mode);
2468 pc_buf = iov[n_iov-1].iov_base;
2472 memset(&rqst, 0, sizeof(struct smb_rqst));
2474 rqst.rq_nvec = n_iov;
2476 /* no need to inc num_remote_opens because we close it just below */
2477 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2478 FILE_WRITE_ATTRIBUTES);
2479 /* resource #4: response buffer */
2480 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2482 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2483 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2485 FILE_WRITE_ATTRIBUTES, rc);
2486 goto err_free_rsp_buf;
2489 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2490 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2491 ses->Suid, CREATE_NOT_FILE,
2492 FILE_WRITE_ATTRIBUTES);
2494 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2496 /* Eventually save off posix specific response info and timestaps */
2499 free_rsp_buf(resp_buftype, rsp);
2502 cifs_small_buf_release(req);
2509 SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2510 struct cifs_open_parms *oparms, __le16 *path)
2512 struct TCP_Server_Info *server = tcon->ses->server;
2513 struct smb2_create_req *req;
2514 unsigned int n_iov = 2;
2515 __u32 file_attributes = 0;
2518 unsigned int total_len;
2519 struct kvec *iov = rqst->rq_iov;
2523 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2527 iov[0].iov_base = (char *)req;
2528 /* -1 since last byte is buf[0] which is sent below (path) */
2529 iov[0].iov_len = total_len - 1;
2531 if (oparms->create_options & CREATE_OPTION_READONLY)
2532 file_attributes |= ATTR_READONLY;
2533 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2534 file_attributes |= ATTR_SYSTEM;
2536 req->ImpersonationLevel = IL_IMPERSONATION;
2537 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2538 /* File attributes ignored on open (used in create though) */
2539 req->FileAttributes = cpu_to_le32(file_attributes);
2540 req->ShareAccess = FILE_SHARE_ALL_LE;
2542 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2543 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2544 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2546 /* [MS-SMB2] 2.2.13 NameOffset:
2547 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2548 * the SMB2 header, the file name includes a prefix that will
2549 * be processed during DFS name normalization as specified in
2550 * section 3.3.5.9. Otherwise, the file name is relative to
2551 * the share that is identified by the TreeId in the SMB2
2554 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2557 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2558 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2560 tcon->treeName, path);
2563 req->NameLength = cpu_to_le16(name_len * 2);
2564 uni_path_len = copy_size;
2567 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2568 /* MUST set path len (NameLength) to 0 opening root of share */
2569 req->NameLength = cpu_to_le16(uni_path_len - 2);
2570 copy_size = uni_path_len;
2571 if (copy_size % 8 != 0)
2572 copy_size = roundup(copy_size, 8);
2573 copy_path = kzalloc(copy_size, GFP_KERNEL);
2576 memcpy((char *)copy_path, (const char *)path,
2578 uni_path_len = copy_size;
2582 iov[1].iov_len = uni_path_len;
2583 iov[1].iov_base = path;
2585 if ((!server->oplocks) || (tcon->no_lease))
2586 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2588 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2589 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2590 req->RequestedOplockLevel = *oplock;
2591 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2592 (oparms->create_options & CREATE_NOT_FILE))
2593 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2595 rc = add_lease_context(server, iov, &n_iov,
2596 oparms->fid->lease_key, oplock);
2601 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2602 /* need to set Next field of lease context if we request it */
2603 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2604 struct create_context *ccontext =
2605 (struct create_context *)iov[n_iov-1].iov_base;
2607 cpu_to_le32(server->vals->create_lease_size);
2610 rc = add_durable_context(iov, &n_iov, oparms,
2611 tcon->use_persistent);
2616 if (tcon->posix_extensions) {
2618 struct create_context *ccontext =
2619 (struct create_context *)iov[n_iov-1].iov_base;
2621 cpu_to_le32(iov[n_iov-1].iov_len);
2624 rc = add_posix_context(iov, &n_iov, oparms->mode);
2629 if (tcon->snapshot_time) {
2630 cifs_dbg(FYI, "adding snapshot context\n");
2632 struct create_context *ccontext =
2633 (struct create_context *)iov[n_iov-1].iov_base;
2635 cpu_to_le32(iov[n_iov-1].iov_len);
2638 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2643 if ((oparms->disposition != FILE_OPEN) &&
2644 (oparms->cifs_sb) &&
2645 (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
2646 (oparms->mode != ACL_NO_MODE)) {
2648 struct create_context *ccontext =
2649 (struct create_context *)iov[n_iov-1].iov_base;
2651 cpu_to_le32(iov[n_iov-1].iov_len);
2654 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2655 rc = add_sd_context(iov, &n_iov, oparms->mode);
2661 struct create_context *ccontext =
2662 (struct create_context *)iov[n_iov-1].iov_base;
2663 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2665 add_query_id_context(iov, &n_iov);
2667 rqst->rq_nvec = n_iov;
2671 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2672 * All other vectors are freed by kfree().
2675 SMB2_open_free(struct smb_rqst *rqst)
2679 if (rqst && rqst->rq_iov) {
2680 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2681 for (i = 1; i < rqst->rq_nvec; i++)
2682 if (rqst->rq_iov[i].iov_base != smb2_padding)
2683 kfree(rqst->rq_iov[i].iov_base);
2688 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2689 __u8 *oplock, struct smb2_file_all_info *buf,
2690 struct kvec *err_iov, int *buftype)
2692 struct smb_rqst rqst;
2693 struct smb2_create_rsp *rsp = NULL;
2694 struct TCP_Server_Info *server;
2695 struct cifs_tcon *tcon = oparms->tcon;
2696 struct cifs_ses *ses = tcon->ses;
2697 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2698 struct kvec rsp_iov = {NULL, 0};
2699 int resp_buftype = CIFS_NO_BUFFER;
2703 cifs_dbg(FYI, "create/open\n");
2704 if (ses && (ses->server))
2705 server = ses->server;
2709 if (smb3_encryption_required(tcon))
2710 flags |= CIFS_TRANSFORM_REQ;
2712 memset(&rqst, 0, sizeof(struct smb_rqst));
2713 memset(&iov, 0, sizeof(iov));
2715 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2717 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2721 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2722 oparms->create_options, oparms->desired_access);
2724 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2726 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2729 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2730 if (err_iov && rsp) {
2732 *buftype = resp_buftype;
2733 resp_buftype = CIFS_NO_BUFFER;
2736 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2737 oparms->create_options, oparms->desired_access, rc);
2738 if (rc == -EREMCHG) {
2739 printk_once(KERN_WARNING "server share %s deleted\n",
2741 tcon->need_reconnect = true;
2745 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2746 ses->Suid, oparms->create_options,
2747 oparms->desired_access);
2749 atomic_inc(&tcon->num_remote_opens);
2750 oparms->fid->persistent_fid = rsp->PersistentFileId;
2751 oparms->fid->volatile_fid = rsp->VolatileFileId;
2752 #ifdef CONFIG_CIFS_DEBUG2
2753 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2754 #endif /* CIFS_DEBUG2 */
2757 memcpy(buf, &rsp->CreationTime, 32);
2758 buf->AllocationSize = rsp->AllocationSize;
2759 buf->EndOfFile = rsp->EndofFile;
2760 buf->Attributes = rsp->FileAttributes;
2761 buf->NumberOfLinks = cpu_to_le32(1);
2762 buf->DeletePending = 0;
2766 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2767 oparms->fid->lease_key, oplock, buf);
2769 SMB2_open_free(&rqst);
2770 free_rsp_buf(resp_buftype, rsp);
2775 SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2776 u64 persistent_fid, u64 volatile_fid, u32 opcode,
2777 bool is_fsctl, char *in_data, u32 indatalen,
2778 __u32 max_response_size)
2780 struct smb2_ioctl_req *req;
2781 struct kvec *iov = rqst->rq_iov;
2782 unsigned int total_len;
2786 rc = smb2_ioctl_req_init(opcode, tcon, (void **) &req, &total_len);
2792 * indatalen is usually small at a couple of bytes max, so
2793 * just allocate through generic pool
2795 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
2797 cifs_small_buf_release(req);
2802 req->CtlCode = cpu_to_le32(opcode);
2803 req->PersistentFileId = persistent_fid;
2804 req->VolatileFileId = volatile_fid;
2806 iov[0].iov_base = (char *)req;
2808 * If no input data, the size of ioctl struct in
2809 * protocol spec still includes a 1 byte data buffer,
2810 * but if input data passed to ioctl, we do not
2811 * want to double count this, so we do not send
2812 * the dummy one byte of data in iovec[0] if sending
2813 * input data (in iovec[1]).
2816 req->InputCount = cpu_to_le32(indatalen);
2817 /* do not set InputOffset if no input data */
2819 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
2821 iov[0].iov_len = total_len - 1;
2822 iov[1].iov_base = in_data_buf;
2823 iov[1].iov_len = indatalen;
2826 iov[0].iov_len = total_len;
2829 req->OutputOffset = 0;
2830 req->OutputCount = 0; /* MBZ */
2833 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2834 * We Could increase default MaxOutputResponse, but that could require
2835 * more credits. Windows typically sets this smaller, but for some
2836 * ioctls it may be useful to allow server to send more. No point
2837 * limiting what the server can send as long as fits in one credit
2838 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2839 * to increase this limit up in the future.
2840 * Note that for snapshot queries that servers like Azure expect that
2841 * the first query be minimal size (and just used to get the number/size
2842 * of previous versions) so response size must be specified as EXACTLY
2843 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2844 * of eight bytes. Currently that is the only case where we set max
2845 * response size smaller.
2847 req->MaxOutputResponse = cpu_to_le32(max_response_size);
2850 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2854 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2855 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
2856 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
2862 SMB2_ioctl_free(struct smb_rqst *rqst)
2865 if (rqst && rqst->rq_iov) {
2866 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2867 for (i = 1; i < rqst->rq_nvec; i++)
2868 if (rqst->rq_iov[i].iov_base != smb2_padding)
2869 kfree(rqst->rq_iov[i].iov_base);
2875 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2878 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2879 u64 volatile_fid, u32 opcode, bool is_fsctl,
2880 char *in_data, u32 indatalen, u32 max_out_data_len,
2881 char **out_data, u32 *plen /* returned data len */)
2883 struct smb_rqst rqst;
2884 struct smb2_ioctl_rsp *rsp = NULL;
2885 struct cifs_ses *ses;
2886 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
2887 struct kvec rsp_iov = {NULL, 0};
2888 int resp_buftype = CIFS_NO_BUFFER;
2891 struct TCP_Server_Info *server;
2893 cifs_dbg(FYI, "SMB2 IOCTL\n");
2895 if (out_data != NULL)
2898 /* zero out returned data len, in case of error */
2909 server = ses->server;
2913 if (smb3_encryption_required(tcon))
2914 flags |= CIFS_TRANSFORM_REQ;
2916 memset(&rqst, 0, sizeof(struct smb_rqst));
2917 memset(&iov, 0, sizeof(iov));
2919 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
2921 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
2922 is_fsctl, in_data, indatalen, max_out_data_len);
2926 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2928 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
2931 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2932 ses->Suid, 0, opcode, rc);
2934 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
2935 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2937 } else if (rc == -EINVAL) {
2938 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2939 (opcode != FSCTL_SRV_COPYCHUNK)) {
2940 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2943 } else if (rc == -E2BIG) {
2944 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
2945 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2950 /* check if caller wants to look at return data or just return rc */
2951 if ((plen == NULL) || (out_data == NULL))
2954 *plen = le32_to_cpu(rsp->OutputCount);
2956 /* We check for obvious errors in the output buffer length and offset */
2958 goto ioctl_exit; /* server returned no data */
2959 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
2960 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2966 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
2967 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2968 le32_to_cpu(rsp->OutputOffset));
2974 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2976 if (*out_data == NULL) {
2982 SMB2_ioctl_free(&rqst);
2983 free_rsp_buf(resp_buftype, rsp);
2988 * Individual callers to ioctl worker function follow
2992 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2993 u64 persistent_fid, u64 volatile_fid)
2996 struct compress_ioctl fsctl_input;
2997 char *ret_data = NULL;
2999 fsctl_input.CompressionState =
3000 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3002 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3003 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
3004 (char *)&fsctl_input /* data input */,
3005 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3006 &ret_data /* out data */, NULL);
3008 cifs_dbg(FYI, "set compression rc %d\n", rc);
3014 SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3015 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
3017 struct smb2_close_req *req;
3018 struct kvec *iov = rqst->rq_iov;
3019 unsigned int total_len;
3022 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
3026 req->PersistentFileId = persistent_fid;
3027 req->VolatileFileId = volatile_fid;
3029 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3032 iov[0].iov_base = (char *)req;
3033 iov[0].iov_len = total_len;
3039 SMB2_close_free(struct smb_rqst *rqst)
3041 if (rqst && rqst->rq_iov)
3042 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3046 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3047 u64 persistent_fid, u64 volatile_fid,
3048 struct smb2_file_network_open_info *pbuf)
3050 struct smb_rqst rqst;
3051 struct smb2_close_rsp *rsp = NULL;
3052 struct cifs_ses *ses = tcon->ses;
3054 struct kvec rsp_iov;
3055 int resp_buftype = CIFS_NO_BUFFER;
3058 bool query_attrs = false;
3060 cifs_dbg(FYI, "Close\n");
3062 if (!ses || !(ses->server))
3065 if (smb3_encryption_required(tcon))
3066 flags |= CIFS_TRANSFORM_REQ;
3068 memset(&rqst, 0, sizeof(struct smb_rqst));
3069 memset(&iov, 0, sizeof(iov));
3073 /* check if need to ask server to return timestamps in close response */
3077 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3078 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid,
3083 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3084 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
3087 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
3088 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3092 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3095 * Note that have to subtract 4 since struct network_open_info
3096 * has a final 4 byte pad that close response does not have
3099 memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3102 atomic_dec(&tcon->num_remote_opens);
3104 SMB2_close_free(&rqst);
3105 free_rsp_buf(resp_buftype, rsp);
3107 /* retry close in a worker thread if this one is interrupted */
3111 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3114 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3115 persistent_fid, tmp_rc);
3121 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3122 u64 persistent_fid, u64 volatile_fid)
3124 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3128 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3129 struct kvec *iov, unsigned int min_buf_size)
3131 unsigned int smb_len = iov->iov_len;
3132 char *end_of_smb = smb_len + (char *)iov->iov_base;
3133 char *begin_of_buf = offset + (char *)iov->iov_base;
3134 char *end_of_buf = begin_of_buf + buffer_length;
3137 if (buffer_length < min_buf_size) {
3138 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3139 buffer_length, min_buf_size);
3143 /* check if beyond RFC1001 maximum length */
3144 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3145 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3146 buffer_length, smb_len);
3150 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3151 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
3159 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3160 * Caller must free buffer.
3163 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3164 struct kvec *iov, unsigned int minbufsize,
3167 char *begin_of_buf = offset + (char *)iov->iov_base;
3173 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3177 memcpy(data, begin_of_buf, buffer_length);
3183 SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3184 u64 persistent_fid, u64 volatile_fid,
3185 u8 info_class, u8 info_type, u32 additional_info,
3186 size_t output_len, size_t input_len, void *input)
3188 struct smb2_query_info_req *req;
3189 struct kvec *iov = rqst->rq_iov;
3190 unsigned int total_len;
3193 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3198 req->InfoType = info_type;
3199 req->FileInfoClass = info_class;
3200 req->PersistentFileId = persistent_fid;
3201 req->VolatileFileId = volatile_fid;
3202 req->AdditionalInformation = cpu_to_le32(additional_info);
3204 req->OutputBufferLength = cpu_to_le32(output_len);
3206 req->InputBufferLength = cpu_to_le32(input_len);
3207 /* total_len for smb query request never close to le16 max */
3208 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3209 memcpy(req->Buffer, input, input_len);
3212 iov[0].iov_base = (char *)req;
3214 iov[0].iov_len = total_len - 1 + input_len;
3219 SMB2_query_info_free(struct smb_rqst *rqst)
3221 if (rqst && rqst->rq_iov)
3222 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3226 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3227 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3228 u32 additional_info, size_t output_len, size_t min_len, void **data,
3231 struct smb_rqst rqst;
3232 struct smb2_query_info_rsp *rsp = NULL;
3234 struct kvec rsp_iov;
3236 int resp_buftype = CIFS_NO_BUFFER;
3237 struct cifs_ses *ses = tcon->ses;
3238 struct TCP_Server_Info *server;
3240 bool allocated = false;
3242 cifs_dbg(FYI, "Query Info\n");
3246 server = ses->server;
3250 if (smb3_encryption_required(tcon))
3251 flags |= CIFS_TRANSFORM_REQ;
3253 memset(&rqst, 0, sizeof(struct smb_rqst));
3254 memset(&iov, 0, sizeof(iov));
3258 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
3259 info_class, info_type, additional_info,
3260 output_len, 0, NULL);
3264 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3265 ses->Suid, info_class, (__u32)info_type);
3267 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3268 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3271 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3272 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3273 ses->Suid, info_class, (__u32)info_type, rc);
3277 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3278 ses->Suid, info_class, (__u32)info_type);
3281 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3283 *data = kmalloc(*dlen, GFP_KERNEL);
3286 "Error %d allocating memory for acl\n",
3296 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3297 le32_to_cpu(rsp->OutputBufferLength),
3298 &rsp_iov, min_len, *data);
3299 if (rc && allocated) {
3306 SMB2_query_info_free(&rqst);
3307 free_rsp_buf(resp_buftype, rsp);
3311 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3312 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3314 return query_info(xid, tcon, persistent_fid, volatile_fid,
3315 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3316 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3317 sizeof(struct smb2_file_all_info), (void **)&data,
3322 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3323 u64 persistent_fid, u64 volatile_fid,
3324 void **data, u32 *plen)
3326 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3329 return query_info(xid, tcon, persistent_fid, volatile_fid,
3330 0, SMB2_O_INFO_SECURITY, additional_info,
3331 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3335 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3336 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3338 return query_info(xid, tcon, persistent_fid, volatile_fid,
3339 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3340 sizeof(struct smb2_file_internal_info),
3341 sizeof(struct smb2_file_internal_info),
3342 (void **)&uniqueid, NULL);
3346 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3347 * See MS-SMB2 2.2.35 and 2.2.36
3351 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3352 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid,
3353 u32 completion_filter, bool watch_tree)
3355 struct smb2_change_notify_req *req;
3356 struct kvec *iov = rqst->rq_iov;
3357 unsigned int total_len;
3360 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, (void **) &req, &total_len);
3364 req->PersistentFileId = persistent_fid;
3365 req->VolatileFileId = volatile_fid;
3366 req->OutputBufferLength =
3367 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3368 req->CompletionFilter = cpu_to_le32(completion_filter);
3370 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3374 iov[0].iov_base = (char *)req;
3375 iov[0].iov_len = total_len;
3381 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3382 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3383 u32 completion_filter)
3385 struct cifs_ses *ses = tcon->ses;
3386 struct smb_rqst rqst;
3388 struct kvec rsp_iov = {NULL, 0};
3389 int resp_buftype = CIFS_NO_BUFFER;
3393 cifs_dbg(FYI, "change notify\n");
3394 if (!ses || !(ses->server))
3397 if (smb3_encryption_required(tcon))
3398 flags |= CIFS_TRANSFORM_REQ;
3400 memset(&rqst, 0, sizeof(struct smb_rqst));
3401 memset(&iov, 0, sizeof(iov));
3405 rc = SMB2_notify_init(xid, &rqst, tcon, persistent_fid, volatile_fid,
3406 completion_filter, watch_tree);
3410 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3411 (u8)watch_tree, completion_filter);
3412 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3415 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3416 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3417 (u8)watch_tree, completion_filter, rc);
3419 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3420 ses->Suid, (u8)watch_tree, completion_filter);
3424 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3425 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3432 * This is a no-op for now. We're not really interested in the reply, but
3433 * rather in the fact that the server sent one and that server->lstrp
3436 * FIXME: maybe we should consider checking that the reply matches request?
3439 smb2_echo_callback(struct mid_q_entry *mid)
3441 struct TCP_Server_Info *server = mid->callback_data;
3442 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3443 struct cifs_credits credits = { .value = 0, .instance = 0 };
3445 if (mid->mid_state == MID_RESPONSE_RECEIVED
3446 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3447 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3448 credits.instance = server->reconnect_instance;
3451 DeleteMidQEntry(mid);
3452 add_credits(server, &credits, CIFS_ECHO_OP);
3455 void smb2_reconnect_server(struct work_struct *work)
3457 struct TCP_Server_Info *server = container_of(work,
3458 struct TCP_Server_Info, reconnect.work);
3459 struct cifs_ses *ses;
3460 struct cifs_tcon *tcon, *tcon2;
3461 struct list_head tmp_list;
3462 int tcon_exist = false;
3464 int resched = false;
3467 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3468 mutex_lock(&server->reconnect_mutex);
3470 INIT_LIST_HEAD(&tmp_list);
3471 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3473 spin_lock(&cifs_tcp_ses_lock);
3474 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3475 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3476 if (tcon->need_reconnect || tcon->need_reopen_files) {
3478 list_add_tail(&tcon->rlist, &tmp_list);
3483 * IPC has the same lifetime as its session and uses its
3486 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3487 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3493 * Get the reference to server struct to be sure that the last call of
3494 * cifs_put_tcon() in the loop below won't release the server pointer.
3497 server->srv_count++;
3499 spin_unlock(&cifs_tcp_ses_lock);
3501 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3502 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3504 cifs_reopen_persistent_handles(tcon);
3507 list_del_init(&tcon->rlist);
3509 cifs_put_smb_ses(tcon->ses);
3511 cifs_put_tcon(tcon);
3514 cifs_dbg(FYI, "Reconnecting tcons finished\n");
3516 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3517 mutex_unlock(&server->reconnect_mutex);
3519 /* now we can safely release srv struct */
3521 cifs_put_tcp_session(server, 1);
3525 SMB2_echo(struct TCP_Server_Info *server)
3527 struct smb2_echo_req *req;
3530 struct smb_rqst rqst = { .rq_iov = iov,
3532 unsigned int total_len;
3534 cifs_dbg(FYI, "In echo request\n");
3536 if (server->tcpStatus == CifsNeedNegotiate) {
3537 /* No need to send echo on newly established connections */
3538 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3542 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
3546 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3548 iov[0].iov_len = total_len;
3549 iov[0].iov_base = (char *)req;
3551 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3552 server, CIFS_ECHO_OP, NULL);
3554 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3556 cifs_small_buf_release(req);
3561 SMB2_flush_free(struct smb_rqst *rqst)
3563 if (rqst && rqst->rq_iov)
3564 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3568 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3569 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid)
3571 struct smb2_flush_req *req;
3572 struct kvec *iov = rqst->rq_iov;
3573 unsigned int total_len;
3576 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
3580 req->PersistentFileId = persistent_fid;
3581 req->VolatileFileId = volatile_fid;
3583 iov[0].iov_base = (char *)req;
3584 iov[0].iov_len = total_len;
3590 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3593 struct cifs_ses *ses = tcon->ses;
3594 struct smb_rqst rqst;
3596 struct kvec rsp_iov = {NULL, 0};
3597 int resp_buftype = CIFS_NO_BUFFER;
3601 cifs_dbg(FYI, "flush\n");
3602 if (!ses || !(ses->server))
3605 if (smb3_encryption_required(tcon))
3606 flags |= CIFS_TRANSFORM_REQ;
3608 memset(&rqst, 0, sizeof(struct smb_rqst));
3609 memset(&iov, 0, sizeof(iov));
3613 rc = SMB2_flush_init(xid, &rqst, tcon, persistent_fid, volatile_fid);
3617 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3618 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3621 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
3622 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3625 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3629 SMB2_flush_free(&rqst);
3630 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3635 * To form a chain of read requests, any read requests after the first should
3636 * have the end_of_chain boolean set to true.
3639 smb2_new_read_req(void **buf, unsigned int *total_len,
3640 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3641 unsigned int remaining_bytes, int request_type)
3644 struct smb2_read_plain_req *req = NULL;
3645 struct smb2_sync_hdr *shdr;
3646 struct TCP_Server_Info *server;
3648 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3653 server = io_parms->tcon->ses->server;
3655 return -ECONNABORTED;
3657 shdr = &req->sync_hdr;
3658 shdr->ProcessId = cpu_to_le32(io_parms->pid);
3660 req->PersistentFileId = io_parms->persistent_fid;
3661 req->VolatileFileId = io_parms->volatile_fid;
3662 req->ReadChannelInfoOffset = 0; /* reserved */
3663 req->ReadChannelInfoLength = 0; /* reserved */
3664 req->Channel = 0; /* reserved */
3665 req->MinimumCount = 0;
3666 req->Length = cpu_to_le32(io_parms->length);
3667 req->Offset = cpu_to_le64(io_parms->offset);
3669 trace_smb3_read_enter(0 /* xid */,
3670 io_parms->persistent_fid,
3671 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3672 io_parms->offset, io_parms->length);
3673 #ifdef CONFIG_CIFS_SMB_DIRECT
3675 * If we want to do a RDMA write, fill in and append
3676 * smbd_buffer_descriptor_v1 to the end of read request
3678 if (server->rdma && rdata && !server->sign &&
3679 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
3681 struct smbd_buffer_descriptor_v1 *v1;
3682 bool need_invalidate =
3683 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3685 rdata->mr = smbd_register_mr(
3686 server->smbd_conn, rdata->pages,
3687 rdata->nr_pages, rdata->page_offset,
3688 rdata->tailsz, true, need_invalidate);
3692 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3693 if (need_invalidate)
3694 req->Channel = SMB2_CHANNEL_RDMA_V1;
3695 req->ReadChannelInfoOffset =
3696 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
3697 req->ReadChannelInfoLength =
3698 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3699 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3700 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3701 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3702 v1->length = cpu_to_le32(rdata->mr->mr->length);
3704 *total_len += sizeof(*v1) - 1;
3707 if (request_type & CHAINED_REQUEST) {
3708 if (!(request_type & END_OF_CHAIN)) {
3709 /* next 8-byte aligned request */
3710 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3711 shdr->NextCommand = cpu_to_le32(*total_len);
3712 } else /* END_OF_CHAIN */
3713 shdr->NextCommand = 0;
3714 if (request_type & RELATED_REQUEST) {
3715 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
3717 * Related requests use info from previous read request
3720 shdr->SessionId = 0xFFFFFFFF;
3721 shdr->TreeId = 0xFFFFFFFF;
3722 req->PersistentFileId = 0xFFFFFFFF;
3723 req->VolatileFileId = 0xFFFFFFFF;
3726 if (remaining_bytes > io_parms->length)
3727 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3729 req->RemainingBytes = 0;
3736 smb2_readv_callback(struct mid_q_entry *mid)
3738 struct cifs_readdata *rdata = mid->callback_data;
3739 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3740 struct TCP_Server_Info *server = tcon->ses->server;
3741 struct smb2_sync_hdr *shdr =
3742 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
3743 struct cifs_credits credits = { .value = 0, .instance = 0 };
3744 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
3746 .rq_pages = rdata->pages,
3747 .rq_offset = rdata->page_offset,
3748 .rq_npages = rdata->nr_pages,
3749 .rq_pagesz = rdata->pagesz,
3750 .rq_tailsz = rdata->tailsz };
3752 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3753 __func__, mid->mid, mid->mid_state, rdata->result,
3756 switch (mid->mid_state) {
3757 case MID_RESPONSE_RECEIVED:
3758 credits.value = le16_to_cpu(shdr->CreditRequest);
3759 credits.instance = server->reconnect_instance;
3760 /* result already set, check signature */
3761 if (server->sign && !mid->decrypted) {
3764 rc = smb2_verify_signature(&rqst, server);
3766 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
3769 /* FIXME: should this be counted toward the initiating task? */
3770 task_io_account_read(rdata->got_bytes);
3771 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3773 case MID_REQUEST_SUBMITTED:
3774 case MID_RETRY_NEEDED:
3775 rdata->result = -EAGAIN;
3776 if (server->sign && rdata->got_bytes)
3777 /* reset bytes number since we can not check a sign */
3778 rdata->got_bytes = 0;
3779 /* FIXME: should this be counted toward the initiating task? */
3780 task_io_account_read(rdata->got_bytes);
3781 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3783 case MID_RESPONSE_MALFORMED:
3784 credits.value = le16_to_cpu(shdr->CreditRequest);
3785 credits.instance = server->reconnect_instance;
3788 rdata->result = -EIO;
3790 #ifdef CONFIG_CIFS_SMB_DIRECT
3792 * If this rdata has a memmory registered, the MR can be freed
3793 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3794 * because they have limited number and are used for future I/Os
3797 smbd_deregister_mr(rdata->mr);
3801 if (rdata->result && rdata->result != -ENODATA) {
3802 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
3803 trace_smb3_read_err(0 /* xid */,
3804 rdata->cfile->fid.persistent_fid,
3805 tcon->tid, tcon->ses->Suid, rdata->offset,
3806 rdata->bytes, rdata->result);
3808 trace_smb3_read_done(0 /* xid */,
3809 rdata->cfile->fid.persistent_fid,
3810 tcon->tid, tcon->ses->Suid,
3811 rdata->offset, rdata->got_bytes);
3813 queue_work(cifsiod_wq, &rdata->work);
3814 DeleteMidQEntry(mid);
3815 add_credits(server, &credits, 0);
3818 /* smb2_async_readv - send an async read, and set up mid to handle result */
3820 smb2_async_readv(struct cifs_readdata *rdata)
3824 struct smb2_sync_hdr *shdr;
3825 struct cifs_io_parms io_parms;
3826 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3828 struct TCP_Server_Info *server;
3829 unsigned int total_len;
3831 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3832 __func__, rdata->offset, rdata->bytes);
3834 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3835 io_parms.offset = rdata->offset;
3836 io_parms.length = rdata->bytes;
3837 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3838 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3839 io_parms.pid = rdata->pid;
3841 server = io_parms.tcon->ses->server;
3843 rc = smb2_new_read_req(
3844 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
3848 if (smb3_encryption_required(io_parms.tcon))
3849 flags |= CIFS_TRANSFORM_REQ;
3851 rdata->iov[0].iov_base = buf;
3852 rdata->iov[0].iov_len = total_len;
3854 shdr = (struct smb2_sync_hdr *)buf;
3856 if (rdata->credits.value > 0) {
3857 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
3858 SMB2_MAX_BUFFER_SIZE));
3859 shdr->CreditRequest =
3860 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
3862 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3864 goto async_readv_out;
3866 flags |= CIFS_HAS_CREDITS;
3869 kref_get(&rdata->refcount);
3870 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
3871 cifs_readv_receive, smb2_readv_callback,
3872 smb3_handle_read_data, rdata, flags,
3875 kref_put(&rdata->refcount, cifs_readdata_release);
3876 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
3877 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3879 io_parms.tcon->ses->Suid,
3880 io_parms.offset, io_parms.length, rc);
3884 cifs_small_buf_release(buf);
3889 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3890 unsigned int *nbytes, char **buf, int *buf_type)
3892 struct smb_rqst rqst;
3893 int resp_buftype, rc;
3894 struct smb2_read_plain_req *req = NULL;
3895 struct smb2_read_rsp *rsp = NULL;
3897 struct kvec rsp_iov;
3898 unsigned int total_len;
3899 int flags = CIFS_LOG_ERROR;
3900 struct cifs_ses *ses = io_parms->tcon->ses;
3903 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
3907 if (smb3_encryption_required(io_parms->tcon))
3908 flags |= CIFS_TRANSFORM_REQ;
3910 iov[0].iov_base = (char *)req;
3911 iov[0].iov_len = total_len;
3913 memset(&rqst, 0, sizeof(struct smb_rqst));
3917 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3918 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
3921 if (rc != -ENODATA) {
3922 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3923 cifs_dbg(VFS, "Send error in read = %d\n", rc);
3924 trace_smb3_read_err(xid, req->PersistentFileId,
3925 io_parms->tcon->tid, ses->Suid,
3926 io_parms->offset, io_parms->length,
3929 trace_smb3_read_done(xid, req->PersistentFileId,
3930 io_parms->tcon->tid, ses->Suid,
3931 io_parms->offset, 0);
3932 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3933 cifs_small_buf_release(req);
3934 return rc == -ENODATA ? 0 : rc;
3936 trace_smb3_read_done(xid, req->PersistentFileId,
3937 io_parms->tcon->tid, ses->Suid,
3938 io_parms->offset, io_parms->length);
3940 cifs_small_buf_release(req);
3942 *nbytes = le32_to_cpu(rsp->DataLength);
3943 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3944 (*nbytes > io_parms->length)) {
3945 cifs_dbg(FYI, "bad length %d for count %d\n",
3946 *nbytes, io_parms->length);
3952 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
3953 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3954 } else if (resp_buftype != CIFS_NO_BUFFER) {
3955 *buf = rsp_iov.iov_base;
3956 if (resp_buftype == CIFS_SMALL_BUFFER)
3957 *buf_type = CIFS_SMALL_BUFFER;
3958 else if (resp_buftype == CIFS_LARGE_BUFFER)
3959 *buf_type = CIFS_LARGE_BUFFER;
3965 * Check the mid_state and signature on received buffer (if any), and queue the
3966 * workqueue completion task.
3969 smb2_writev_callback(struct mid_q_entry *mid)
3971 struct cifs_writedata *wdata = mid->callback_data;
3972 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3973 struct TCP_Server_Info *server = tcon->ses->server;
3974 unsigned int written;
3975 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
3976 struct cifs_credits credits = { .value = 0, .instance = 0 };
3978 switch (mid->mid_state) {
3979 case MID_RESPONSE_RECEIVED:
3980 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3981 credits.instance = server->reconnect_instance;
3982 wdata->result = smb2_check_receive(mid, server, 0);
3983 if (wdata->result != 0)
3986 written = le32_to_cpu(rsp->DataLength);
3988 * Mask off high 16 bits when bytes written as returned
3989 * by the server is greater than bytes requested by the
3990 * client. OS/2 servers are known to set incorrect
3993 if (written > wdata->bytes)
3996 if (written < wdata->bytes)
3997 wdata->result = -ENOSPC;
3999 wdata->bytes = written;
4001 case MID_REQUEST_SUBMITTED:
4002 case MID_RETRY_NEEDED:
4003 wdata->result = -EAGAIN;
4005 case MID_RESPONSE_MALFORMED:
4006 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4007 credits.instance = server->reconnect_instance;
4010 wdata->result = -EIO;
4013 #ifdef CONFIG_CIFS_SMB_DIRECT
4015 * If this wdata has a memory registered, the MR can be freed
4016 * The number of MRs available is limited, it's important to recover
4017 * used MR as soon as I/O is finished. Hold MR longer in the later
4018 * I/O process can possibly result in I/O deadlock due to lack of MR
4019 * to send request on I/O retry
4022 smbd_deregister_mr(wdata->mr);
4026 if (wdata->result) {
4027 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4028 trace_smb3_write_err(0 /* no xid */,
4029 wdata->cfile->fid.persistent_fid,
4030 tcon->tid, tcon->ses->Suid, wdata->offset,
4031 wdata->bytes, wdata->result);
4032 if (wdata->result == -ENOSPC)
4033 printk_once(KERN_WARNING "Out of space writing to %s\n",
4036 trace_smb3_write_done(0 /* no xid */,
4037 wdata->cfile->fid.persistent_fid,
4038 tcon->tid, tcon->ses->Suid,
4039 wdata->offset, wdata->bytes);
4041 queue_work(cifsiod_wq, &wdata->work);
4042 DeleteMidQEntry(mid);
4043 add_credits(server, &credits, 0);
4046 /* smb2_async_writev - send an async write, and set up mid to handle result */
4048 smb2_async_writev(struct cifs_writedata *wdata,
4049 void (*release)(struct kref *kref))
4051 int rc = -EACCES, flags = 0;
4052 struct smb2_write_req *req = NULL;
4053 struct smb2_sync_hdr *shdr;
4054 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4055 struct TCP_Server_Info *server = tcon->ses->server;
4057 struct smb_rqst rqst = { };
4058 unsigned int total_len;
4060 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
4064 if (smb3_encryption_required(tcon))
4065 flags |= CIFS_TRANSFORM_REQ;
4067 shdr = (struct smb2_sync_hdr *)req;
4068 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
4070 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
4071 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
4072 req->WriteChannelInfoOffset = 0;
4073 req->WriteChannelInfoLength = 0;
4075 req->Offset = cpu_to_le64(wdata->offset);
4076 req->DataOffset = cpu_to_le16(
4077 offsetof(struct smb2_write_req, Buffer));
4078 req->RemainingBytes = 0;
4080 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
4081 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
4082 #ifdef CONFIG_CIFS_SMB_DIRECT
4084 * If we want to do a server RDMA read, fill in and append
4085 * smbd_buffer_descriptor_v1 to the end of write request
4087 if (server->rdma && !server->sign && wdata->bytes >=
4088 server->smbd_conn->rdma_readwrite_threshold) {
4090 struct smbd_buffer_descriptor_v1 *v1;
4091 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4093 wdata->mr = smbd_register_mr(
4094 server->smbd_conn, wdata->pages,
4095 wdata->nr_pages, wdata->page_offset,
4096 wdata->tailsz, false, need_invalidate);
4099 goto async_writev_out;
4102 req->DataOffset = 0;
4103 if (wdata->nr_pages > 1)
4104 req->RemainingBytes =
4106 (wdata->nr_pages - 1) * wdata->pagesz -
4107 wdata->page_offset + wdata->tailsz
4110 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
4111 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4112 if (need_invalidate)
4113 req->Channel = SMB2_CHANNEL_RDMA_V1;
4114 req->WriteChannelInfoOffset =
4115 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4116 req->WriteChannelInfoLength =
4117 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4118 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4119 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4120 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4121 v1->length = cpu_to_le32(wdata->mr->mr->length);
4124 iov[0].iov_len = total_len - 1;
4125 iov[0].iov_base = (char *)req;
4129 rqst.rq_pages = wdata->pages;
4130 rqst.rq_offset = wdata->page_offset;
4131 rqst.rq_npages = wdata->nr_pages;
4132 rqst.rq_pagesz = wdata->pagesz;
4133 rqst.rq_tailsz = wdata->tailsz;
4134 #ifdef CONFIG_CIFS_SMB_DIRECT
4136 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4140 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4141 wdata->offset, wdata->bytes);
4143 #ifdef CONFIG_CIFS_SMB_DIRECT
4144 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4146 req->Length = cpu_to_le32(wdata->bytes);
4148 req->Length = cpu_to_le32(wdata->bytes);
4151 if (wdata->credits.value > 0) {
4152 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4153 SMB2_MAX_BUFFER_SIZE));
4154 shdr->CreditRequest =
4155 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
4157 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4159 goto async_writev_out;
4161 flags |= CIFS_HAS_CREDITS;
4164 kref_get(&wdata->refcount);
4165 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4166 wdata, flags, &wdata->credits);
4169 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
4170 tcon->tid, tcon->ses->Suid, wdata->offset,
4172 kref_put(&wdata->refcount, release);
4173 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4177 cifs_small_buf_release(req);
4182 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4183 * The length field from io_parms must be at least 1 and indicates a number of
4184 * elements with data to write that begins with position 1 in iov array. All
4185 * data length is specified by count.
4188 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4189 unsigned int *nbytes, struct kvec *iov, int n_vec)
4191 struct smb_rqst rqst;
4193 struct smb2_write_req *req = NULL;
4194 struct smb2_write_rsp *rsp = NULL;
4196 struct kvec rsp_iov;
4198 unsigned int total_len;
4205 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
4210 if (io_parms->tcon->ses->server == NULL)
4211 return -ECONNABORTED;
4213 if (smb3_encryption_required(io_parms->tcon))
4214 flags |= CIFS_TRANSFORM_REQ;
4216 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
4218 req->PersistentFileId = io_parms->persistent_fid;
4219 req->VolatileFileId = io_parms->volatile_fid;
4220 req->WriteChannelInfoOffset = 0;
4221 req->WriteChannelInfoLength = 0;
4223 req->Length = cpu_to_le32(io_parms->length);
4224 req->Offset = cpu_to_le64(io_parms->offset);
4225 req->DataOffset = cpu_to_le16(
4226 offsetof(struct smb2_write_req, Buffer));
4227 req->RemainingBytes = 0;
4229 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4230 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4231 io_parms->offset, io_parms->length);
4233 iov[0].iov_base = (char *)req;
4235 iov[0].iov_len = total_len - 1;
4237 memset(&rqst, 0, sizeof(struct smb_rqst));
4239 rqst.rq_nvec = n_vec + 1;
4241 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
4242 &resp_buftype, flags, &rsp_iov);
4243 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
4246 trace_smb3_write_err(xid, req->PersistentFileId,
4247 io_parms->tcon->tid,
4248 io_parms->tcon->ses->Suid,
4249 io_parms->offset, io_parms->length, rc);
4250 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
4251 cifs_dbg(VFS, "Send error in write = %d\n", rc);
4253 *nbytes = le32_to_cpu(rsp->DataLength);
4254 trace_smb3_write_done(xid, req->PersistentFileId,
4255 io_parms->tcon->tid,
4256 io_parms->tcon->ses->Suid,
4257 io_parms->offset, *nbytes);
4260 cifs_small_buf_release(req);
4261 free_rsp_buf(resp_buftype, rsp);
4266 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
4269 unsigned int entrycount = 0;
4270 unsigned int next_offset = 0;
4272 FILE_DIRECTORY_INFO *dir_info;
4274 if (bufstart == NULL)
4277 entryptr = bufstart;
4280 if (entryptr + next_offset < entryptr ||
4281 entryptr + next_offset > end_of_buf ||
4282 entryptr + next_offset + size > end_of_buf) {
4283 cifs_dbg(VFS, "malformed search entry would overflow\n");
4287 entryptr = entryptr + next_offset;
4288 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4290 len = le32_to_cpu(dir_info->FileNameLength);
4291 if (entryptr + len < entryptr ||
4292 entryptr + len > end_of_buf ||
4293 entryptr + len + size > end_of_buf) {
4294 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4299 *lastentry = entryptr;
4302 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
4313 int SMB2_query_directory_init(const unsigned int xid,
4314 struct cifs_tcon *tcon, struct smb_rqst *rqst,
4315 u64 persistent_fid, u64 volatile_fid,
4316 int index, int info_level)
4318 struct TCP_Server_Info *server = tcon->ses->server;
4319 struct smb2_query_directory_req *req;
4320 unsigned char *bufptr;
4321 __le16 asteriks = cpu_to_le16('*');
4322 unsigned int output_size = CIFSMaxBufSize -
4323 MAX_SMB2_CREATE_RESPONSE_SIZE -
4324 MAX_SMB2_CLOSE_RESPONSE_SIZE;
4325 unsigned int total_len;
4326 struct kvec *iov = rqst->rq_iov;
4329 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
4334 switch (info_level) {
4335 case SMB_FIND_FILE_DIRECTORY_INFO:
4336 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4338 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4339 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4342 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4347 req->FileIndex = cpu_to_le32(index);
4348 req->PersistentFileId = persistent_fid;
4349 req->VolatileFileId = volatile_fid;
4352 bufptr = req->Buffer;
4353 memcpy(bufptr, &asteriks, len);
4355 req->FileNameOffset =
4356 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
4357 req->FileNameLength = cpu_to_le16(len);
4359 * BB could be 30 bytes or so longer if we used SMB2 specific
4360 * buffer lengths, but this is safe and close enough.
4362 output_size = min_t(unsigned int, output_size, server->maxBuf);
4363 output_size = min_t(unsigned int, output_size, 2 << 15);
4364 req->OutputBufferLength = cpu_to_le32(output_size);
4366 iov[0].iov_base = (char *)req;
4368 iov[0].iov_len = total_len - 1;
4370 iov[1].iov_base = (char *)(req->Buffer);
4371 iov[1].iov_len = len;
4373 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4374 tcon->ses->Suid, index, output_size);
4379 void SMB2_query_directory_free(struct smb_rqst *rqst)
4381 if (rqst && rqst->rq_iov) {
4382 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4387 smb2_parse_query_directory(struct cifs_tcon *tcon,
4388 struct kvec *rsp_iov,
4390 struct cifs_search_info *srch_inf)
4392 struct smb2_query_directory_rsp *rsp;
4393 size_t info_buf_size;
4397 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
4399 switch (srch_inf->info_level) {
4400 case SMB_FIND_FILE_DIRECTORY_INFO:
4401 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4403 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4404 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4407 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4408 srch_inf->info_level);
4412 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4413 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
4418 srch_inf->unicode = true;
4420 if (srch_inf->ntwrk_buf_start) {
4421 if (srch_inf->smallBuf)
4422 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4424 cifs_buf_release(srch_inf->ntwrk_buf_start);
4426 srch_inf->ntwrk_buf_start = (char *)rsp;
4427 srch_inf->srch_entries_start = srch_inf->last_entry =
4428 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4429 end_of_smb = rsp_iov->iov_len + (char *)rsp;
4430 srch_inf->entries_in_buffer =
4431 num_entries(srch_inf->srch_entries_start, end_of_smb,
4432 &srch_inf->last_entry, info_buf_size);
4433 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4434 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4435 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4436 srch_inf->srch_entries_start, srch_inf->last_entry);
4437 if (resp_buftype == CIFS_LARGE_BUFFER)
4438 srch_inf->smallBuf = false;
4439 else if (resp_buftype == CIFS_SMALL_BUFFER)
4440 srch_inf->smallBuf = true;
4442 cifs_tcon_dbg(VFS, "illegal search buffer type\n");
4448 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4449 u64 persistent_fid, u64 volatile_fid, int index,
4450 struct cifs_search_info *srch_inf)
4452 struct smb_rqst rqst;
4453 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
4454 struct smb2_query_directory_rsp *rsp = NULL;
4455 int resp_buftype = CIFS_NO_BUFFER;
4456 struct kvec rsp_iov;
4458 struct cifs_ses *ses = tcon->ses;
4461 if (!ses || !(ses->server))
4464 if (smb3_encryption_required(tcon))
4465 flags |= CIFS_TRANSFORM_REQ;
4467 memset(&rqst, 0, sizeof(struct smb_rqst));
4468 memset(&iov, 0, sizeof(iov));
4470 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
4472 rc = SMB2_query_directory_init(xid, tcon, &rqst, persistent_fid,
4473 volatile_fid, index,
4474 srch_inf->info_level);
4478 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4479 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
4482 if (rc == -ENODATA &&
4483 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
4484 trace_smb3_query_dir_done(xid, persistent_fid,
4485 tcon->tid, tcon->ses->Suid, index, 0);
4486 srch_inf->endOfSearch = true;
4489 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4490 tcon->ses->Suid, index, 0, rc);
4491 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
4496 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
4499 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4500 tcon->ses->Suid, index, 0, rc);
4503 resp_buftype = CIFS_NO_BUFFER;
4505 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4506 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
4509 SMB2_query_directory_free(&rqst);
4510 free_rsp_buf(resp_buftype, rsp);
4515 SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
4516 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4517 u8 info_type, u32 additional_info,
4518 void **data, unsigned int *size)
4520 struct smb2_set_info_req *req;
4521 struct kvec *iov = rqst->rq_iov;
4522 unsigned int i, total_len;
4525 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
4529 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4530 req->InfoType = info_type;
4531 req->FileInfoClass = info_class;
4532 req->PersistentFileId = persistent_fid;
4533 req->VolatileFileId = volatile_fid;
4534 req->AdditionalInformation = cpu_to_le32(additional_info);
4537 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
4538 req->BufferLength = cpu_to_le32(*size);
4540 memcpy(req->Buffer, *data, *size);
4543 iov[0].iov_base = (char *)req;
4545 iov[0].iov_len = total_len - 1;
4547 for (i = 1; i < rqst->rq_nvec; i++) {
4548 le32_add_cpu(&req->BufferLength, size[i]);
4549 iov[i].iov_base = (char *)data[i];
4550 iov[i].iov_len = size[i];
4557 SMB2_set_info_free(struct smb_rqst *rqst)
4559 if (rqst && rqst->rq_iov)
4560 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
4564 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4565 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4566 u8 info_type, u32 additional_info, unsigned int num,
4567 void **data, unsigned int *size)
4569 struct smb_rqst rqst;
4570 struct smb2_set_info_rsp *rsp = NULL;
4572 struct kvec rsp_iov;
4575 struct cifs_ses *ses = tcon->ses;
4578 if (!ses || !(ses->server))
4584 if (smb3_encryption_required(tcon))
4585 flags |= CIFS_TRANSFORM_REQ;
4587 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4591 memset(&rqst, 0, sizeof(struct smb_rqst));
4595 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4596 info_class, info_type, additional_info,
4604 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
4606 SMB2_set_info_free(&rqst);
4607 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
4610 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
4611 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4612 ses->Suid, info_class, (__u32)info_type, rc);
4615 free_rsp_buf(resp_buftype, rsp);
4621 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4622 u64 volatile_fid, u32 pid, __le64 *eof)
4624 struct smb2_file_eof_info info;
4628 info.EndOfFile = *eof;
4631 size = sizeof(struct smb2_file_eof_info);
4633 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4634 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4635 0, 1, &data, &size);
4639 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4640 u64 persistent_fid, u64 volatile_fid,
4641 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4643 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4644 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4645 1, (void **)&pnntsd, &pacllen);
4649 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4650 u64 persistent_fid, u64 volatile_fid,
4651 struct smb2_file_full_ea_info *buf, int len)
4653 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4654 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4655 0, 1, (void **)&buf, &len);
4659 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4660 const u64 persistent_fid, const u64 volatile_fid,
4663 struct smb_rqst rqst;
4665 struct smb2_oplock_break *req = NULL;
4666 struct cifs_ses *ses = tcon->ses;
4667 int flags = CIFS_OBREAK_OP;
4668 unsigned int total_len;
4670 struct kvec rsp_iov;
4673 cifs_dbg(FYI, "SMB2_oplock_break\n");
4674 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4679 if (smb3_encryption_required(tcon))
4680 flags |= CIFS_TRANSFORM_REQ;
4682 req->VolatileFid = volatile_fid;
4683 req->PersistentFid = persistent_fid;
4684 req->OplockLevel = oplock_level;
4685 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4687 flags |= CIFS_NO_RSP_BUF;
4689 iov[0].iov_base = (char *)req;
4690 iov[0].iov_len = total_len;
4692 memset(&rqst, 0, sizeof(struct smb_rqst));
4696 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4697 cifs_small_buf_release(req);
4700 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4701 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
4708 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4709 struct kstatfs *kst)
4711 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4712 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4713 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
4714 kst->f_bfree = kst->f_bavail =
4715 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
4720 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4721 struct kstatfs *kst)
4723 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4724 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4725 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4726 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4727 kst->f_bavail = kst->f_bfree;
4729 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4730 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4731 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4732 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4733 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4739 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4740 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4743 struct smb2_query_info_req *req;
4744 unsigned int total_len;
4746 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
4748 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4751 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4756 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4757 req->FileInfoClass = level;
4758 req->PersistentFileId = persistent_fid;
4759 req->VolatileFileId = volatile_fid;
4761 req->InputBufferOffset =
4762 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
4763 req->OutputBufferLength = cpu_to_le32(
4764 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
4766 iov->iov_base = (char *)req;
4767 iov->iov_len = total_len;
4772 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4773 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4775 struct smb_rqst rqst;
4776 struct smb2_query_info_rsp *rsp = NULL;
4778 struct kvec rsp_iov;
4781 struct cifs_ses *ses = tcon->ses;
4782 FILE_SYSTEM_POSIX_INFO *info = NULL;
4785 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4786 sizeof(FILE_SYSTEM_POSIX_INFO),
4787 persistent_fid, volatile_fid);
4791 if (smb3_encryption_required(tcon))
4792 flags |= CIFS_TRANSFORM_REQ;
4794 memset(&rqst, 0, sizeof(struct smb_rqst));
4798 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4799 cifs_small_buf_release(iov.iov_base);
4801 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4802 goto posix_qfsinf_exit;
4804 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4806 info = (FILE_SYSTEM_POSIX_INFO *)(
4807 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4808 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4809 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4810 sizeof(FILE_SYSTEM_POSIX_INFO));
4812 copy_posix_fs_info_to_kstatfs(info, fsdata);
4815 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4820 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4821 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4823 struct smb_rqst rqst;
4824 struct smb2_query_info_rsp *rsp = NULL;
4826 struct kvec rsp_iov;
4829 struct cifs_ses *ses = tcon->ses;
4830 struct smb2_fs_full_size_info *info = NULL;
4833 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4834 sizeof(struct smb2_fs_full_size_info),
4835 persistent_fid, volatile_fid);
4839 if (smb3_encryption_required(tcon))
4840 flags |= CIFS_TRANSFORM_REQ;
4842 memset(&rqst, 0, sizeof(struct smb_rqst));
4846 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4847 cifs_small_buf_release(iov.iov_base);
4849 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4852 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4854 info = (struct smb2_fs_full_size_info *)(
4855 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4856 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4857 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4858 sizeof(struct smb2_fs_full_size_info));
4860 smb2_copy_fs_info_to_kstatfs(info, fsdata);
4863 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4868 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
4869 u64 persistent_fid, u64 volatile_fid, int level)
4871 struct smb_rqst rqst;
4872 struct smb2_query_info_rsp *rsp = NULL;
4874 struct kvec rsp_iov;
4876 int resp_buftype, max_len, min_len;
4877 struct cifs_ses *ses = tcon->ses;
4878 unsigned int rsp_len, offset;
4881 if (level == FS_DEVICE_INFORMATION) {
4882 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4883 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4884 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4885 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4886 min_len = MIN_FS_ATTR_INFO_SIZE;
4887 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4888 max_len = sizeof(struct smb3_fs_ss_info);
4889 min_len = sizeof(struct smb3_fs_ss_info);
4890 } else if (level == FS_VOLUME_INFORMATION) {
4891 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4892 min_len = sizeof(struct smb3_fs_vol_info);
4894 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
4898 rc = build_qfs_info_req(&iov, tcon, level, max_len,
4899 persistent_fid, volatile_fid);
4903 if (smb3_encryption_required(tcon))
4904 flags |= CIFS_TRANSFORM_REQ;
4906 memset(&rqst, 0, sizeof(struct smb_rqst));
4910 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4911 cifs_small_buf_release(iov.iov_base);
4913 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4916 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4918 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4919 offset = le16_to_cpu(rsp->OutputBufferOffset);
4920 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
4924 if (level == FS_ATTRIBUTE_INFORMATION)
4925 memcpy(&tcon->fsAttrInfo, offset
4926 + (char *)rsp, min_t(unsigned int,
4928 else if (level == FS_DEVICE_INFORMATION)
4929 memcpy(&tcon->fsDevInfo, offset
4930 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
4931 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4932 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
4933 (offset + (char *)rsp);
4934 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4935 tcon->perf_sector_size =
4936 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
4937 } else if (level == FS_VOLUME_INFORMATION) {
4938 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4939 (offset + (char *)rsp);
4940 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4941 tcon->vol_create_time = vol_info->VolumeCreationTime;
4945 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4950 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4951 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4952 const __u32 num_lock, struct smb2_lock_element *buf)
4954 struct smb_rqst rqst;
4956 struct smb2_lock_req *req = NULL;
4958 struct kvec rsp_iov;
4961 int flags = CIFS_NO_RSP_BUF;
4962 unsigned int total_len;
4964 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
4966 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
4970 if (smb3_encryption_required(tcon))
4971 flags |= CIFS_TRANSFORM_REQ;
4973 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4974 req->LockCount = cpu_to_le16(num_lock);
4976 req->PersistentFileId = persist_fid;
4977 req->VolatileFileId = volatile_fid;
4979 count = num_lock * sizeof(struct smb2_lock_element);
4981 iov[0].iov_base = (char *)req;
4982 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
4983 iov[1].iov_base = (char *)buf;
4984 iov[1].iov_len = count;
4986 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
4988 memset(&rqst, 0, sizeof(struct smb_rqst));
4992 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
4994 cifs_small_buf_release(req);
4996 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
4997 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
4998 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4999 tcon->ses->Suid, rc);
5006 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
5007 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5008 const __u64 length, const __u64 offset, const __u32 lock_flags,
5011 struct smb2_lock_element lock;
5013 lock.Offset = cpu_to_le64(offset);
5014 lock.Length = cpu_to_le64(length);
5015 lock.Flags = cpu_to_le32(lock_flags);
5016 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5017 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5019 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5023 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5024 __u8 *lease_key, const __le32 lease_state)
5026 struct smb_rqst rqst;
5028 struct smb2_lease_ack *req = NULL;
5029 struct cifs_ses *ses = tcon->ses;
5030 int flags = CIFS_OBREAK_OP;
5031 unsigned int total_len;
5033 struct kvec rsp_iov;
5035 __u64 *please_key_high;
5036 __u64 *please_key_low;
5038 cifs_dbg(FYI, "SMB2_lease_break\n");
5039 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
5044 if (smb3_encryption_required(tcon))
5045 flags |= CIFS_TRANSFORM_REQ;
5047 req->sync_hdr.CreditRequest = cpu_to_le16(1);
5048 req->StructureSize = cpu_to_le16(36);
5051 memcpy(req->LeaseKey, lease_key, 16);
5052 req->LeaseState = lease_state;
5054 flags |= CIFS_NO_RSP_BUF;
5056 iov[0].iov_base = (char *)req;
5057 iov[0].iov_len = total_len;
5059 memset(&rqst, 0, sizeof(struct smb_rqst));
5063 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
5064 cifs_small_buf_release(req);
5066 please_key_low = (__u64 *)lease_key;
5067 please_key_high = (__u64 *)(lease_key+8);
5069 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5070 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5071 ses->Suid, *please_key_low, *please_key_high, rc);
5072 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
5074 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5075 ses->Suid, *please_key_low, *please_key_high);