4 * Copyright (C) International Business Machines Corp., 2009, 2013
9 * Contains the routines for constructing the SMB2 PDUs themselves
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
32 #include <linux/kernel.h>
33 #include <linux/vfs.h>
34 #include <linux/task_io_accounting_ops.h>
35 #include <linux/uaccess.h>
36 #include <linux/uuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/xattr.h>
42 #include "cifsproto.h"
43 #include "smb2proto.h"
44 #include "cifs_unicode.h"
45 #include "cifs_debug.h"
47 #include "smb2status.h"
50 #include "cifs_spnego.h"
51 #include "smbdirect.h"
53 #ifdef CONFIG_CIFS_DFS_UPCALL
54 #include "dfs_cache.h"
58 * The following table defines the expected "StructureSize" of SMB2 requests
59 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
61 * Note that commands are defined in smb2pdu.h in le16 but the array below is
62 * indexed by command in host byte order.
64 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
65 /* SMB2_NEGOTIATE */ 36,
66 /* SMB2_SESSION_SETUP */ 25,
68 /* SMB2_TREE_CONNECT */ 9,
69 /* SMB2_TREE_DISCONNECT */ 4,
79 /* SMB2_QUERY_DIRECTORY */ 33,
80 /* SMB2_CHANGE_NOTIFY */ 32,
81 /* SMB2_QUERY_INFO */ 41,
82 /* SMB2_SET_INFO */ 33,
83 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
86 int smb3_encryption_required(const struct cifs_tcon *tcon)
90 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
91 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
94 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
100 smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
101 const struct cifs_tcon *tcon)
103 shdr->ProtocolId = SMB2_PROTO_NUMBER;
104 shdr->StructureSize = cpu_to_le16(64);
105 shdr->Command = smb2_cmd;
106 if (tcon && tcon->ses && tcon->ses->server) {
107 struct TCP_Server_Info *server = tcon->ses->server;
109 spin_lock(&server->req_lock);
110 /* Request up to 10 credits but don't go over the limit. */
111 if (server->credits >= server->max_credits)
112 shdr->CreditRequest = cpu_to_le16(0);
114 shdr->CreditRequest = cpu_to_le16(
115 min_t(int, server->max_credits -
116 server->credits, 10));
117 spin_unlock(&server->req_lock);
119 shdr->CreditRequest = cpu_to_le16(2);
121 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
126 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
127 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
128 if ((tcon->ses) && (tcon->ses->server) &&
129 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
130 shdr->CreditCharge = cpu_to_le16(1);
131 /* else CreditCharge MBZ */
133 shdr->TreeId = tcon->tid;
134 /* Uid is not converted */
136 shdr->SessionId = tcon->ses->Suid;
139 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
140 * to pass the path on the Open SMB prefixed by \\server\share.
141 * Not sure when we would need to do the augmented path (if ever) and
142 * setting this flag breaks the SMB2 open operation since it is
143 * illegal to send an empty path name (without \\server\share prefix)
144 * when the DFS flag is set in the SMB open header. We could
145 * consider setting the flag on all operations other than open
146 * but it is safer to net set it for now.
148 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
149 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
151 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
152 !smb3_encryption_required(tcon))
153 shdr->Flags |= SMB2_FLAGS_SIGNED;
158 #ifdef CONFIG_CIFS_DFS_UPCALL
159 static int __smb2_reconnect(const struct nls_table *nlsc,
160 struct cifs_tcon *tcon)
163 struct dfs_cache_tgt_list tl;
164 struct dfs_cache_tgt_iterator *it = NULL;
166 const char *tcp_host;
168 const char *dfs_host;
171 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
176 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
177 tcon->ses->server->hostname);
178 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
182 if (!tcon->dfs_path) {
183 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
187 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl);
191 extract_unc_hostname(tcon->ses->server->hostname, &tcp_host,
194 for (it = dfs_cache_get_tgt_iterator(&tl); it;
195 it = dfs_cache_get_next_tgt(&tl, it)) {
196 const char *tgt = dfs_cache_get_tgt_name(it);
198 extract_unc_hostname(tgt, &dfs_host, &dfs_host_len);
200 if (dfs_host_len != tcp_host_len
201 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
202 cifs_dbg(FYI, "%s: skipping %.*s, doesn't match %.*s",
204 (int)dfs_host_len, dfs_host,
205 (int)tcp_host_len, tcp_host);
209 scnprintf(tree, MAX_TREE_SIZE, "\\%s", tgt);
211 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
220 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1,
225 dfs_cache_free_tgts(&tl);
231 static inline int __smb2_reconnect(const struct nls_table *nlsc,
232 struct cifs_tcon *tcon)
234 return SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
239 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
242 struct nls_table *nls_codepage;
243 struct cifs_ses *ses;
244 struct TCP_Server_Info *server;
248 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
249 * check for tcp and smb session status done differently
250 * for those three - in the calling routine.
255 if (smb2_command == SMB2_TREE_CONNECT)
258 if (tcon->tidStatus == CifsExiting) {
260 * only tree disconnect, open, and write,
261 * (and ulogoff which does not have tcon)
262 * are allowed as we start force umount.
264 if ((smb2_command != SMB2_WRITE) &&
265 (smb2_command != SMB2_CREATE) &&
266 (smb2_command != SMB2_TREE_DISCONNECT)) {
267 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
272 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
273 (!tcon->ses->server))
277 server = ses->server;
279 retries = server->nr_targets;
282 * Give demultiplex thread up to 10 seconds to each target available for
283 * reconnect -- should be greater than cifs socket timeout which is 7
286 while (server->tcpStatus == CifsNeedReconnect) {
288 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
289 * here since they are implicitly done when session drops.
291 switch (smb2_command) {
293 * BB Should we keep oplock break and add flush to exceptions?
295 case SMB2_TREE_DISCONNECT:
298 case SMB2_OPLOCK_BREAK:
302 rc = wait_event_interruptible_timeout(server->response_q,
303 (server->tcpStatus != CifsNeedReconnect),
306 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
307 " signal by the process\n", __func__);
311 /* are we still trying to reconnect? */
312 if (server->tcpStatus != CifsNeedReconnect)
319 * on "soft" mounts we wait once. Hard mounts keep
320 * retrying until process is killed or server comes
324 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
327 retries = server->nr_targets;
330 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
333 nls_codepage = load_nls_default();
336 * need to prevent multiple threads trying to simultaneously reconnect
337 * the same SMB session
339 mutex_lock(&tcon->ses->session_mutex);
342 * Recheck after acquire mutex. If another thread is negotiating
343 * and the server never sends an answer the socket will be closed
344 * and tcpStatus set to reconnect.
346 if (server->tcpStatus == CifsNeedReconnect) {
348 mutex_unlock(&tcon->ses->session_mutex);
352 rc = cifs_negotiate_protocol(0, tcon->ses);
353 if (!rc && tcon->ses->need_reconnect)
354 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
356 if (rc || !tcon->need_reconnect) {
357 mutex_unlock(&tcon->ses->session_mutex);
361 cifs_mark_open_files_invalid(tcon);
362 if (tcon->use_persistent)
363 tcon->need_reopen_files = true;
365 rc = __smb2_reconnect(nls_codepage, tcon);
366 mutex_unlock(&tcon->ses->session_mutex);
368 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
370 /* If sess reconnected but tcon didn't, something strange ... */
371 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
375 if (smb2_command != SMB2_INTERNAL_CMD)
376 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
378 atomic_inc(&tconInfoReconnectCount);
381 * Check if handle based operation so we know whether we can continue
382 * or not without returning to caller to reset file handle.
385 * BB Is flush done by server on drop of tcp session? Should we special
386 * case it and skip above?
388 switch (smb2_command) {
394 case SMB2_QUERY_DIRECTORY:
395 case SMB2_CHANGE_NOTIFY:
396 case SMB2_QUERY_INFO:
400 unload_nls(nls_codepage);
405 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
406 unsigned int *total_len)
408 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
409 /* lookup word count ie StructureSize from table */
410 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
413 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
414 * largest operations (Create)
418 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
419 spdu->StructureSize2 = cpu_to_le16(parmsize);
421 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
425 * Allocate and return pointer to an SMB request hdr, and set basic
426 * SMB information in the SMB header. If the return code is zero, this
427 * function must have filled in request_buf pointer.
430 smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
431 void **request_buf, unsigned int *total_len)
435 rc = smb2_reconnect(smb2_command, tcon);
439 /* BB eventually switch this to SMB2 specific small buf size */
440 if (smb2_command == SMB2_SET_INFO)
441 *request_buf = cifs_buf_get();
443 *request_buf = cifs_small_buf_get();
444 if (*request_buf == NULL) {
445 /* BB should we add a retry in here if not a writepage? */
449 fill_small_buf(smb2_command, tcon,
450 (struct smb2_sync_hdr *)(*request_buf),
454 uint16_t com_code = le16_to_cpu(smb2_command);
455 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
456 cifs_stats_inc(&tcon->num_smbs_sent);
462 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
465 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
467 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
468 pneg_ctxt->DataLength = cpu_to_le16(38);
469 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
470 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
471 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
472 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
476 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
478 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
479 pneg_ctxt->DataLength =
480 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
481 - sizeof(struct smb2_neg_context));
482 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
483 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
484 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
485 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
489 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
491 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
492 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + two ciphers */
493 pneg_ctxt->CipherCount = cpu_to_le16(2);
494 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
495 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
499 build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
501 struct nls_table *cp = load_nls_default();
503 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
505 /* copy up to max of first 100 bytes of server name to NetName field */
506 pneg_ctxt->DataLength = cpu_to_le16(2 +
507 (2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp)));
508 /* context size is DataLength + minimal smb2_neg_context */
509 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
510 sizeof(struct smb2_neg_context), 8) * 8;
514 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
516 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
517 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
518 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
519 pneg_ctxt->Name[0] = 0x93;
520 pneg_ctxt->Name[1] = 0xAD;
521 pneg_ctxt->Name[2] = 0x25;
522 pneg_ctxt->Name[3] = 0x50;
523 pneg_ctxt->Name[4] = 0x9C;
524 pneg_ctxt->Name[5] = 0xB4;
525 pneg_ctxt->Name[6] = 0x11;
526 pneg_ctxt->Name[7] = 0xE7;
527 pneg_ctxt->Name[8] = 0xB4;
528 pneg_ctxt->Name[9] = 0x23;
529 pneg_ctxt->Name[10] = 0x83;
530 pneg_ctxt->Name[11] = 0xDE;
531 pneg_ctxt->Name[12] = 0x96;
532 pneg_ctxt->Name[13] = 0x8B;
533 pneg_ctxt->Name[14] = 0xCD;
534 pneg_ctxt->Name[15] = 0x7C;
538 assemble_neg_contexts(struct smb2_negotiate_req *req,
539 struct TCP_Server_Info *server, unsigned int *total_len)
541 char *pneg_ctxt = (char *)req;
542 unsigned int ctxt_len;
544 if (*total_len > 200) {
545 /* In case length corrupted don't want to overrun smb buffer */
546 cifs_dbg(VFS, "Bad frame length assembling neg contexts\n");
551 * round up total_len of fixed part of SMB3 negotiate request to 8
552 * byte boundary before adding negotiate contexts
554 *total_len = roundup(*total_len, 8);
556 pneg_ctxt = (*total_len) + (char *)req;
557 req->NegotiateContextOffset = cpu_to_le32(*total_len);
559 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
560 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
561 *total_len += ctxt_len;
562 pneg_ctxt += ctxt_len;
564 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
565 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
566 *total_len += ctxt_len;
567 pneg_ctxt += ctxt_len;
569 if (server->compress_algorithm) {
570 build_compression_ctxt((struct smb2_compression_capabilities_context *)
572 ctxt_len = DIV_ROUND_UP(
573 sizeof(struct smb2_compression_capabilities_context),
575 *total_len += ctxt_len;
576 pneg_ctxt += ctxt_len;
577 req->NegotiateContextCount = cpu_to_le16(5);
579 req->NegotiateContextCount = cpu_to_le16(4);
581 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
583 *total_len += ctxt_len;
584 pneg_ctxt += ctxt_len;
586 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
587 *total_len += sizeof(struct smb2_posix_neg_context);
590 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
592 unsigned int len = le16_to_cpu(ctxt->DataLength);
594 /* If invalid preauth context warn but use what we requested, SHA-512 */
595 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
596 printk_once(KERN_WARNING "server sent bad preauth context\n");
599 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
600 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
601 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
602 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
605 static void decode_compress_ctx(struct TCP_Server_Info *server,
606 struct smb2_compression_capabilities_context *ctxt)
608 unsigned int len = le16_to_cpu(ctxt->DataLength);
610 /* sizeof compress context is a one element compression capbility struct */
612 printk_once(KERN_WARNING "server sent bad compression cntxt\n");
615 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
616 printk_once(KERN_WARNING "illegal SMB3 compress algorithm count\n");
619 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
620 printk_once(KERN_WARNING "unknown compression algorithm\n");
623 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
626 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
627 struct smb2_encryption_neg_context *ctxt)
629 unsigned int len = le16_to_cpu(ctxt->DataLength);
631 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
632 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
633 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
637 if (le16_to_cpu(ctxt->CipherCount) != 1) {
638 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
641 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
642 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
643 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
644 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
647 server->cipher_type = ctxt->Ciphers[0];
648 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
652 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
653 struct TCP_Server_Info *server,
654 unsigned int len_of_smb)
656 struct smb2_neg_context *pctx;
657 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
658 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
659 unsigned int len_of_ctxts, i;
662 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
663 if (len_of_smb <= offset) {
664 cifs_dbg(VFS, "Invalid response: negotiate context offset\n");
668 len_of_ctxts = len_of_smb - offset;
670 for (i = 0; i < ctxt_cnt; i++) {
672 /* check that offset is not beyond end of SMB */
673 if (len_of_ctxts == 0)
676 if (len_of_ctxts < sizeof(struct smb2_neg_context))
679 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
680 clen = le16_to_cpu(pctx->DataLength);
681 if (clen > len_of_ctxts)
684 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
685 decode_preauth_context(
686 (struct smb2_preauth_neg_context *)pctx);
687 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
688 rc = decode_encrypt_ctx(server,
689 (struct smb2_encryption_neg_context *)pctx);
690 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
691 decode_compress_ctx(server,
692 (struct smb2_compression_capabilities_context *)pctx);
693 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
694 server->posix_ext_supported = true;
696 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
697 le16_to_cpu(pctx->ContextType));
701 /* offsets must be 8 byte aligned */
702 clen = (clen + 7) & ~0x7;
703 offset += clen + sizeof(struct smb2_neg_context);
704 len_of_ctxts -= clen;
709 static struct create_posix *
710 create_posix_buf(umode_t mode)
712 struct create_posix *buf;
714 buf = kzalloc(sizeof(struct create_posix),
719 buf->ccontext.DataOffset =
720 cpu_to_le16(offsetof(struct create_posix, Mode));
721 buf->ccontext.DataLength = cpu_to_le32(4);
722 buf->ccontext.NameOffset =
723 cpu_to_le16(offsetof(struct create_posix, Name));
724 buf->ccontext.NameLength = cpu_to_le16(16);
726 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
737 buf->Name[10] = 0x83;
738 buf->Name[11] = 0xDE;
739 buf->Name[12] = 0x96;
740 buf->Name[13] = 0x8B;
741 buf->Name[14] = 0xCD;
742 buf->Name[15] = 0x7C;
743 buf->Mode = cpu_to_le32(mode);
744 cifs_dbg(FYI, "mode on posix create 0%o", mode);
749 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
751 struct smb2_create_req *req = iov[0].iov_base;
752 unsigned int num = *num_iovec;
754 iov[num].iov_base = create_posix_buf(mode);
755 if (iov[num].iov_base == NULL)
757 iov[num].iov_len = sizeof(struct create_posix);
758 if (!req->CreateContextsOffset)
759 req->CreateContextsOffset = cpu_to_le32(
760 sizeof(struct smb2_create_req) +
761 iov[num - 1].iov_len);
762 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
763 *num_iovec = num + 1;
770 * SMB2 Worker functions follow:
772 * The general structure of the worker functions is:
773 * 1) Call smb2_init (assembles SMB2 header)
774 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
775 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
776 * 4) Decode SMB2 command specific fields in the fixed length area
777 * 5) Decode variable length data area (if any for this SMB2 command type)
778 * 6) Call free smb buffer
784 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
786 struct smb_rqst rqst;
787 struct smb2_negotiate_req *req;
788 struct smb2_negotiate_rsp *rsp;
793 struct TCP_Server_Info *server = ses->server;
794 int blob_offset, blob_length;
796 int flags = CIFS_NEG_OP;
797 unsigned int total_len;
799 cifs_dbg(FYI, "Negotiate protocol\n");
802 WARN(1, "%s: server is NULL!\n", __func__);
806 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
810 req->sync_hdr.SessionId = 0;
812 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
813 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
815 if (strcmp(ses->server->vals->version_string,
816 SMB3ANY_VERSION_STRING) == 0) {
817 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
818 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
819 req->DialectCount = cpu_to_le16(2);
821 } else if (strcmp(ses->server->vals->version_string,
822 SMBDEFAULT_VERSION_STRING) == 0) {
823 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
824 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
825 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
826 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
827 req->DialectCount = cpu_to_le16(4);
830 /* otherwise send specific dialect */
831 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
832 req->DialectCount = cpu_to_le16(1);
836 /* only one of SMB2 signing flags may be set in SMB2 request */
838 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
839 else if (global_secflags & CIFSSEC_MAY_SIGN)
840 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
842 req->SecurityMode = 0;
844 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
846 /* ClientGUID must be zero for SMB2.02 dialect */
847 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
848 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
850 memcpy(req->ClientGUID, server->client_guid,
851 SMB2_CLIENT_GUID_SIZE);
852 if ((ses->server->vals->protocol_id == SMB311_PROT_ID) ||
853 (strcmp(ses->server->vals->version_string,
854 SMBDEFAULT_VERSION_STRING) == 0))
855 assemble_neg_contexts(req, server, &total_len);
857 iov[0].iov_base = (char *)req;
858 iov[0].iov_len = total_len;
860 memset(&rqst, 0, sizeof(struct smb_rqst));
864 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
865 cifs_small_buf_release(req);
866 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
868 * No tcon so can't do
869 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
871 if (rc == -EOPNOTSUPP) {
872 cifs_dbg(VFS, "Dialect not supported by server. Consider "
873 "specifying vers=1.0 or vers=2.0 on mount for accessing"
879 if (strcmp(ses->server->vals->version_string,
880 SMB3ANY_VERSION_STRING) == 0) {
881 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
883 "SMB2 dialect returned but not requested\n");
885 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
887 "SMB2.1 dialect returned but not requested\n");
890 } else if (strcmp(ses->server->vals->version_string,
891 SMBDEFAULT_VERSION_STRING) == 0) {
892 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
894 "SMB2 dialect returned but not requested\n");
896 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
897 /* ops set to 3.0 by default for default so update */
898 ses->server->ops = &smb21_operations;
899 ses->server->vals = &smb21_values;
900 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
901 ses->server->ops = &smb311_operations;
902 ses->server->vals = &smb311_values;
904 } else if (le16_to_cpu(rsp->DialectRevision) !=
905 ses->server->vals->protocol_id) {
906 /* if requested single dialect ensure returned dialect matched */
907 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
908 le16_to_cpu(rsp->DialectRevision));
912 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
914 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
915 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
916 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
917 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
918 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
919 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
920 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
921 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
922 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
923 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
925 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
926 le16_to_cpu(rsp->DialectRevision));
930 server->dialect = le16_to_cpu(rsp->DialectRevision);
933 * Keep a copy of the hash after negprot. This hash will be
934 * the starting hash value for all sessions made from this
937 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
938 SMB2_PREAUTH_HASH_SIZE);
940 /* SMB2 only has an extended negflavor */
941 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
942 /* set it to the maximum buffer size value we can send with 1 credit */
943 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
944 SMB2_MAX_BUFFER_SIZE);
945 server->max_read = le32_to_cpu(rsp->MaxReadSize);
946 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
947 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
948 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
949 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
951 server->capabilities = le32_to_cpu(rsp->Capabilities);
953 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
955 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
956 (struct smb2_sync_hdr *)rsp);
958 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
960 * ses->sectype = RawNTLMSSP;
961 * but for time being this is our only auth choice so doesn't matter.
962 * We just found a server which sets blob length to zero expecting raw.
964 if (blob_length == 0) {
965 cifs_dbg(FYI, "missing security blob on negprot\n");
966 server->sec_ntlmssp = true;
969 rc = cifs_enable_signing(server, ses->sign);
973 rc = decode_negTokenInit(security_blob, blob_length, server);
980 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
981 if (rsp->NegotiateContextCount)
982 rc = smb311_decode_neg_context(rsp, server,
985 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
988 free_rsp_buf(resp_buftype, rsp);
992 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
995 struct validate_negotiate_info_req *pneg_inbuf;
996 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
998 u32 inbuflen; /* max of 4 dialects */
1000 cifs_dbg(FYI, "validate negotiate\n");
1002 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1003 if (tcon->ses->server->dialect == SMB311_PROT_ID)
1007 * validation ioctl must be signed, so no point sending this if we
1008 * can not sign it (ie are not known user). Even if signing is not
1009 * required (enabled but not negotiated), in those cases we selectively
1010 * sign just this, the first and only signed request on a connection.
1011 * Having validation of negotiate info helps reduce attack vectors.
1013 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1014 return 0; /* validation requires signing */
1016 if (tcon->ses->user_name == NULL) {
1017 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1018 return 0; /* validation requires signing */
1021 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1022 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1024 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1028 pneg_inbuf->Capabilities =
1029 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
1030 memcpy(pneg_inbuf->Guid, tcon->ses->server->client_guid,
1031 SMB2_CLIENT_GUID_SIZE);
1033 if (tcon->ses->sign)
1034 pneg_inbuf->SecurityMode =
1035 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1036 else if (global_secflags & CIFSSEC_MAY_SIGN)
1037 pneg_inbuf->SecurityMode =
1038 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1040 pneg_inbuf->SecurityMode = 0;
1043 if (strcmp(tcon->ses->server->vals->version_string,
1044 SMB3ANY_VERSION_STRING) == 0) {
1045 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1046 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1047 pneg_inbuf->DialectCount = cpu_to_le16(2);
1048 /* structure is big enough for 3 dialects, sending only 2 */
1049 inbuflen = sizeof(*pneg_inbuf) -
1050 (2 * sizeof(pneg_inbuf->Dialects[0]));
1051 } else if (strcmp(tcon->ses->server->vals->version_string,
1052 SMBDEFAULT_VERSION_STRING) == 0) {
1053 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1054 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1055 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1056 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1057 pneg_inbuf->DialectCount = cpu_to_le16(4);
1058 /* structure is big enough for 3 dialects */
1059 inbuflen = sizeof(*pneg_inbuf);
1061 /* otherwise specific dialect was requested */
1062 pneg_inbuf->Dialects[0] =
1063 cpu_to_le16(tcon->ses->server->vals->protocol_id);
1064 pneg_inbuf->DialectCount = cpu_to_le16(1);
1065 /* structure is big enough for 3 dialects, sending only 1 */
1066 inbuflen = sizeof(*pneg_inbuf) -
1067 sizeof(pneg_inbuf->Dialects[0]) * 2;
1070 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1071 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
1072 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1073 (char **)&pneg_rsp, &rsplen);
1074 if (rc == -EOPNOTSUPP) {
1076 * Old Windows versions or Netapp SMB server can return
1077 * not supported error. Client should accept it.
1079 cifs_dbg(VFS, "Server does not support validate negotiate\n");
1081 goto out_free_inbuf;
1082 } else if (rc != 0) {
1083 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
1085 goto out_free_inbuf;
1089 if (rsplen != sizeof(*pneg_rsp)) {
1090 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
1093 /* relax check since Mac returns max bufsize allowed on ioctl */
1094 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1098 /* check validate negotiate info response matches what we got earlier */
1099 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
1102 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
1105 /* do not validate server guid because not saved at negprot time yet */
1107 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1108 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
1111 /* validate negotiate successful */
1113 cifs_dbg(FYI, "validate negotiate info successful\n");
1117 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1126 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1128 switch (requested) {
1135 if (server->sec_ntlmssp &&
1136 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1138 if ((server->sec_kerberos || server->sec_mskerberos) &&
1139 (global_secflags & CIFSSEC_MAY_KRB5))
1147 struct SMB2_sess_data {
1149 struct cifs_ses *ses;
1150 struct nls_table *nls_cp;
1151 void (*func)(struct SMB2_sess_data *);
1153 u64 previous_session;
1155 /* we will send the SMB in three pieces:
1156 * a fixed length beginning part, an optional
1157 * SPNEGO blob (which can be zero length), and a
1158 * last part which will include the strings
1159 * and rest of bcc area. This allows us to avoid
1160 * a large buffer 17K allocation
1167 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1170 struct cifs_ses *ses = sess_data->ses;
1171 struct smb2_sess_setup_req *req;
1172 struct TCP_Server_Info *server = ses->server;
1173 unsigned int total_len;
1175 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1180 /* First session, not a reauthenticate */
1181 req->sync_hdr.SessionId = 0;
1183 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
1184 req->PreviousSessionId = sess_data->previous_session;
1186 req->Flags = 0; /* MBZ */
1188 /* enough to enable echos and oplocks and one max size write */
1189 req->sync_hdr.CreditRequest = cpu_to_le16(130);
1191 /* only one of SMB2 signing flags may be set in SMB2 request */
1193 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1194 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1195 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1197 req->SecurityMode = 0;
1199 req->Capabilities = 0;
1200 req->Channel = 0; /* MBZ */
1202 sess_data->iov[0].iov_base = (char *)req;
1204 sess_data->iov[0].iov_len = total_len - 1;
1206 * This variable will be used to clear the buffer
1207 * allocated above in case of any error in the calling function.
1209 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1215 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1217 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1218 sess_data->buf0_type = CIFS_NO_BUFFER;
1222 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1225 struct smb_rqst rqst;
1226 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1227 struct kvec rsp_iov = { NULL, 0 };
1229 /* Testing shows that buffer offset must be at location of Buffer[0] */
1230 req->SecurityBufferOffset =
1231 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1232 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1234 memset(&rqst, 0, sizeof(struct smb_rqst));
1235 rqst.rq_iov = sess_data->iov;
1238 /* BB add code to build os and lm fields */
1239 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1241 &sess_data->buf0_type,
1242 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
1243 cifs_small_buf_release(sess_data->iov[0].iov_base);
1244 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1250 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1253 struct cifs_ses *ses = sess_data->ses;
1255 mutex_lock(&ses->server->srv_mutex);
1256 if (ses->server->ops->generate_signingkey) {
1257 rc = ses->server->ops->generate_signingkey(ses);
1260 "SMB3 session key generation failed\n");
1261 mutex_unlock(&ses->server->srv_mutex);
1265 if (!ses->server->session_estab) {
1266 ses->server->sequence_number = 0x2;
1267 ses->server->session_estab = true;
1269 mutex_unlock(&ses->server->srv_mutex);
1271 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1272 spin_lock(&GlobalMid_Lock);
1273 ses->status = CifsGood;
1274 ses->need_reconnect = false;
1275 spin_unlock(&GlobalMid_Lock);
1279 #ifdef CONFIG_CIFS_UPCALL
1281 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1284 struct cifs_ses *ses = sess_data->ses;
1285 struct cifs_spnego_msg *msg;
1286 struct key *spnego_key = NULL;
1287 struct smb2_sess_setup_rsp *rsp = NULL;
1289 rc = SMB2_sess_alloc_buffer(sess_data);
1293 spnego_key = cifs_get_spnego_key(ses);
1294 if (IS_ERR(spnego_key)) {
1295 rc = PTR_ERR(spnego_key);
1300 msg = spnego_key->payload.data[0];
1302 * check version field to make sure that cifs.upcall is
1303 * sending us a response in an expected form
1305 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1307 "bad cifs.upcall version. Expected %d got %d",
1308 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1310 goto out_put_spnego_key;
1313 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1315 if (!ses->auth_key.response) {
1317 "Kerberos can't allocate (%u bytes) memory",
1320 goto out_put_spnego_key;
1322 ses->auth_key.len = msg->sesskey_len;
1324 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1325 sess_data->iov[1].iov_len = msg->secblob_len;
1327 rc = SMB2_sess_sendreceive(sess_data);
1329 goto out_put_spnego_key;
1331 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1332 ses->Suid = rsp->sync_hdr.SessionId;
1334 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1336 rc = SMB2_sess_establish_session(sess_data);
1338 key_invalidate(spnego_key);
1339 key_put(spnego_key);
1341 sess_data->result = rc;
1342 sess_data->func = NULL;
1343 SMB2_sess_free_buffer(sess_data);
1347 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1349 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1350 sess_data->result = -EOPNOTSUPP;
1351 sess_data->func = NULL;
1356 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1359 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1362 struct cifs_ses *ses = sess_data->ses;
1363 struct smb2_sess_setup_rsp *rsp = NULL;
1364 char *ntlmssp_blob = NULL;
1365 bool use_spnego = false; /* else use raw ntlmssp */
1366 u16 blob_length = 0;
1369 * If memory allocation is successful, caller of this function
1372 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1373 if (!ses->ntlmssp) {
1377 ses->ntlmssp->sesskey_per_smbsess = true;
1379 rc = SMB2_sess_alloc_buffer(sess_data);
1383 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1385 if (ntlmssp_blob == NULL) {
1390 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1392 /* BB eventually need to add this */
1393 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1397 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1398 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1400 sess_data->iov[1].iov_base = ntlmssp_blob;
1401 sess_data->iov[1].iov_len = blob_length;
1403 rc = SMB2_sess_sendreceive(sess_data);
1404 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1406 /* If true, rc here is expected and not an error */
1407 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1408 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1414 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1415 le16_to_cpu(rsp->SecurityBufferOffset)) {
1416 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1417 le16_to_cpu(rsp->SecurityBufferOffset));
1421 rc = decode_ntlmssp_challenge(rsp->Buffer,
1422 le16_to_cpu(rsp->SecurityBufferLength), ses);
1426 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1429 ses->Suid = rsp->sync_hdr.SessionId;
1430 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1433 kfree(ntlmssp_blob);
1434 SMB2_sess_free_buffer(sess_data);
1436 sess_data->result = 0;
1437 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1441 kfree(ses->ntlmssp);
1442 ses->ntlmssp = NULL;
1443 sess_data->result = rc;
1444 sess_data->func = NULL;
1448 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1451 struct cifs_ses *ses = sess_data->ses;
1452 struct smb2_sess_setup_req *req;
1453 struct smb2_sess_setup_rsp *rsp = NULL;
1454 unsigned char *ntlmssp_blob = NULL;
1455 bool use_spnego = false; /* else use raw ntlmssp */
1456 u16 blob_length = 0;
1458 rc = SMB2_sess_alloc_buffer(sess_data);
1462 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1463 req->sync_hdr.SessionId = ses->Suid;
1465 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1468 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1473 /* BB eventually need to add this */
1474 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1478 sess_data->iov[1].iov_base = ntlmssp_blob;
1479 sess_data->iov[1].iov_len = blob_length;
1481 rc = SMB2_sess_sendreceive(sess_data);
1485 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1487 ses->Suid = rsp->sync_hdr.SessionId;
1488 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1490 rc = SMB2_sess_establish_session(sess_data);
1492 kfree(ntlmssp_blob);
1493 SMB2_sess_free_buffer(sess_data);
1494 kfree(ses->ntlmssp);
1495 ses->ntlmssp = NULL;
1496 sess_data->result = rc;
1497 sess_data->func = NULL;
1501 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1505 type = smb2_select_sectype(ses->server, ses->sectype);
1506 cifs_dbg(FYI, "sess setup type %d\n", type);
1507 if (type == Unspecified) {
1509 "Unable to select appropriate authentication method!");
1515 sess_data->func = SMB2_auth_kerberos;
1518 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1521 cifs_dbg(VFS, "secType %d not supported!\n", type);
1529 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1530 const struct nls_table *nls_cp)
1533 struct TCP_Server_Info *server = ses->server;
1534 struct SMB2_sess_data *sess_data;
1536 cifs_dbg(FYI, "Session Setup\n");
1539 WARN(1, "%s: server is NULL!\n", __func__);
1543 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1547 rc = SMB2_select_sec(ses, sess_data);
1550 sess_data->xid = xid;
1551 sess_data->ses = ses;
1552 sess_data->buf0_type = CIFS_NO_BUFFER;
1553 sess_data->nls_cp = (struct nls_table *) nls_cp;
1554 sess_data->previous_session = ses->Suid;
1557 * Initialize the session hash with the server one.
1559 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1560 SMB2_PREAUTH_HASH_SIZE);
1562 while (sess_data->func)
1563 sess_data->func(sess_data);
1565 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1566 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
1567 rc = sess_data->result;
1574 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1576 struct smb_rqst rqst;
1577 struct smb2_logoff_req *req; /* response is also trivial struct */
1579 struct TCP_Server_Info *server;
1581 unsigned int total_len;
1583 struct kvec rsp_iov;
1586 cifs_dbg(FYI, "disconnect session %p\n", ses);
1588 if (ses && (ses->server))
1589 server = ses->server;
1593 /* no need to send SMB logoff if uid already closed due to reconnect */
1594 if (ses->need_reconnect)
1595 goto smb2_session_already_dead;
1597 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
1601 /* since no tcon, smb2_init can not do this, so do here */
1602 req->sync_hdr.SessionId = ses->Suid;
1604 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1605 flags |= CIFS_TRANSFORM_REQ;
1606 else if (server->sign)
1607 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1609 flags |= CIFS_NO_RSP_BUF;
1611 iov[0].iov_base = (char *)req;
1612 iov[0].iov_len = total_len;
1614 memset(&rqst, 0, sizeof(struct smb_rqst));
1618 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1619 cifs_small_buf_release(req);
1621 * No tcon so can't do
1622 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1625 smb2_session_already_dead:
1629 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1631 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1634 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1636 /* These are similar values to what Windows uses */
1637 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1639 tcon->max_chunks = 256;
1640 tcon->max_bytes_chunk = 1048576;
1641 tcon->max_bytes_copy = 16777216;
1645 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1646 struct cifs_tcon *tcon, const struct nls_table *cp)
1648 struct smb_rqst rqst;
1649 struct smb2_tree_connect_req *req;
1650 struct smb2_tree_connect_rsp *rsp = NULL;
1652 struct kvec rsp_iov = { NULL, 0 };
1656 __le16 *unc_path = NULL;
1658 unsigned int total_len;
1660 cifs_dbg(FYI, "TCON\n");
1662 if (!(ses->server) || !tree)
1665 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1666 if (unc_path == NULL)
1669 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1671 if (unc_path_len < 2) {
1676 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1678 atomic_set(&tcon->num_remote_opens, 0);
1679 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1686 if (smb3_encryption_required(tcon))
1687 flags |= CIFS_TRANSFORM_REQ;
1689 iov[0].iov_base = (char *)req;
1691 iov[0].iov_len = total_len - 1;
1693 /* Testing shows that buffer offset must be at location of Buffer[0] */
1694 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1696 req->PathLength = cpu_to_le16(unc_path_len - 2);
1697 iov[1].iov_base = unc_path;
1698 iov[1].iov_len = unc_path_len;
1701 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1702 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1703 * (Samba servers don't always set the flag so also check if null user)
1705 if ((ses->server->dialect == SMB311_PROT_ID) &&
1706 !smb3_encryption_required(tcon) &&
1707 !(ses->session_flags &
1708 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1709 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1710 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1712 memset(&rqst, 0, sizeof(struct smb_rqst));
1716 /* Need 64 for max size write so ask for more in case not there yet */
1717 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1719 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
1720 cifs_small_buf_release(req);
1721 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1722 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1725 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1726 tcon->need_reconnect = true;
1728 goto tcon_error_exit;
1731 switch (rsp->ShareType) {
1732 case SMB2_SHARE_TYPE_DISK:
1733 cifs_dbg(FYI, "connection to disk share\n");
1735 case SMB2_SHARE_TYPE_PIPE:
1737 cifs_dbg(FYI, "connection to pipe share\n");
1739 case SMB2_SHARE_TYPE_PRINT:
1741 cifs_dbg(FYI, "connection to printer\n");
1744 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1746 goto tcon_error_exit;
1749 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1750 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1751 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1752 tcon->tidStatus = CifsGood;
1753 tcon->need_reconnect = false;
1754 tcon->tid = rsp->sync_hdr.TreeId;
1755 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1757 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1758 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1759 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1762 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1763 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1765 init_copy_chunk_defaults(tcon);
1766 if (tcon->ses->server->ops->validate_negotiate)
1767 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1770 free_rsp_buf(resp_buftype, rsp);
1775 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1776 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1782 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1784 struct smb_rqst rqst;
1785 struct smb2_tree_disconnect_req *req; /* response is trivial */
1787 struct cifs_ses *ses = tcon->ses;
1789 unsigned int total_len;
1791 struct kvec rsp_iov;
1794 cifs_dbg(FYI, "Tree Disconnect\n");
1796 if (!ses || !(ses->server))
1799 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1802 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1807 if (smb3_encryption_required(tcon))
1808 flags |= CIFS_TRANSFORM_REQ;
1810 flags |= CIFS_NO_RSP_BUF;
1812 iov[0].iov_base = (char *)req;
1813 iov[0].iov_len = total_len;
1815 memset(&rqst, 0, sizeof(struct smb_rqst));
1819 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1820 cifs_small_buf_release(req);
1822 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1828 static struct create_durable *
1829 create_durable_buf(void)
1831 struct create_durable *buf;
1833 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1837 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1838 (struct create_durable, Data));
1839 buf->ccontext.DataLength = cpu_to_le32(16);
1840 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1841 (struct create_durable, Name));
1842 buf->ccontext.NameLength = cpu_to_le16(4);
1843 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1851 static struct create_durable *
1852 create_reconnect_durable_buf(struct cifs_fid *fid)
1854 struct create_durable *buf;
1856 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1860 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1861 (struct create_durable, Data));
1862 buf->ccontext.DataLength = cpu_to_le32(16);
1863 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1864 (struct create_durable, Name));
1865 buf->ccontext.NameLength = cpu_to_le16(4);
1866 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1867 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1868 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1877 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1879 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1881 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1882 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1883 buf->IndexNumber = pdisk_id->DiskFileId;
1887 smb2_parse_contexts(struct TCP_Server_Info *server,
1888 struct smb2_create_rsp *rsp,
1889 unsigned int *epoch, char *lease_key, __u8 *oplock,
1890 struct smb2_file_all_info *buf)
1893 struct create_context *cc;
1895 unsigned int remaining;
1899 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
1900 remaining = le32_to_cpu(rsp->CreateContextsLength);
1901 cc = (struct create_context *)data_offset;
1903 /* Initialize inode number to 0 in case no valid data in qfid context */
1905 buf->IndexNumber = 0;
1907 while (remaining >= sizeof(struct create_context)) {
1908 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1909 if (le16_to_cpu(cc->NameLength) == 4 &&
1910 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
1911 *oplock = server->ops->parse_lease_buf(cc, epoch,
1913 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
1914 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
1915 parse_query_id_ctxt(cc, buf);
1917 next = le32_to_cpu(cc->Next);
1921 cc = (struct create_context *)((char *)cc + next);
1924 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
1925 *oplock = rsp->OplockLevel;
1931 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1932 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
1934 struct smb2_create_req *req = iov[0].iov_base;
1935 unsigned int num = *num_iovec;
1937 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
1938 if (iov[num].iov_base == NULL)
1940 iov[num].iov_len = server->vals->create_lease_size;
1941 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1942 if (!req->CreateContextsOffset)
1943 req->CreateContextsOffset = cpu_to_le32(
1944 sizeof(struct smb2_create_req) +
1945 iov[num - 1].iov_len);
1946 le32_add_cpu(&req->CreateContextsLength,
1947 server->vals->create_lease_size);
1948 *num_iovec = num + 1;
1952 static struct create_durable_v2 *
1953 create_durable_v2_buf(struct cifs_open_parms *oparms)
1955 struct cifs_fid *pfid = oparms->fid;
1956 struct create_durable_v2 *buf;
1958 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1962 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1963 (struct create_durable_v2, dcontext));
1964 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1965 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1966 (struct create_durable_v2, Name));
1967 buf->ccontext.NameLength = cpu_to_le16(4);
1970 * NB: Handle timeout defaults to 0, which allows server to choose
1971 * (most servers default to 120 seconds) and most clients default to 0.
1972 * This can be overridden at mount ("handletimeout=") if the user wants
1973 * a different persistent (or resilient) handle timeout for all opens
1974 * opens on a particular SMB3 mount.
1976 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
1977 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1978 generate_random_uuid(buf->dcontext.CreateGuid);
1979 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1981 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1989 static struct create_durable_handle_reconnect_v2 *
1990 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1992 struct create_durable_handle_reconnect_v2 *buf;
1994 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1999 buf->ccontext.DataOffset =
2000 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2002 buf->ccontext.DataLength =
2003 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2004 buf->ccontext.NameOffset =
2005 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2007 buf->ccontext.NameLength = cpu_to_le16(4);
2009 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2010 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2011 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2012 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2014 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2023 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2024 struct cifs_open_parms *oparms)
2026 struct smb2_create_req *req = iov[0].iov_base;
2027 unsigned int num = *num_iovec;
2029 iov[num].iov_base = create_durable_v2_buf(oparms);
2030 if (iov[num].iov_base == NULL)
2032 iov[num].iov_len = sizeof(struct create_durable_v2);
2033 if (!req->CreateContextsOffset)
2034 req->CreateContextsOffset =
2035 cpu_to_le32(sizeof(struct smb2_create_req) +
2037 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
2038 *num_iovec = num + 1;
2043 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2044 struct cifs_open_parms *oparms)
2046 struct smb2_create_req *req = iov[0].iov_base;
2047 unsigned int num = *num_iovec;
2049 /* indicate that we don't need to relock the file */
2050 oparms->reconnect = false;
2052 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2053 if (iov[num].iov_base == NULL)
2055 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2056 if (!req->CreateContextsOffset)
2057 req->CreateContextsOffset =
2058 cpu_to_le32(sizeof(struct smb2_create_req) +
2060 le32_add_cpu(&req->CreateContextsLength,
2061 sizeof(struct create_durable_handle_reconnect_v2));
2062 *num_iovec = num + 1;
2067 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2068 struct cifs_open_parms *oparms, bool use_persistent)
2070 struct smb2_create_req *req = iov[0].iov_base;
2071 unsigned int num = *num_iovec;
2073 if (use_persistent) {
2074 if (oparms->reconnect)
2075 return add_durable_reconnect_v2_context(iov, num_iovec,
2078 return add_durable_v2_context(iov, num_iovec, oparms);
2081 if (oparms->reconnect) {
2082 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2083 /* indicate that we don't need to relock the file */
2084 oparms->reconnect = false;
2086 iov[num].iov_base = create_durable_buf();
2087 if (iov[num].iov_base == NULL)
2089 iov[num].iov_len = sizeof(struct create_durable);
2090 if (!req->CreateContextsOffset)
2091 req->CreateContextsOffset =
2092 cpu_to_le32(sizeof(struct smb2_create_req) +
2094 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
2095 *num_iovec = num + 1;
2099 /* See MS-SMB2 2.2.13.2.7 */
2100 static struct crt_twarp_ctxt *
2101 create_twarp_buf(__u64 timewarp)
2103 struct crt_twarp_ctxt *buf;
2105 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2109 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2110 (struct crt_twarp_ctxt, Timestamp));
2111 buf->ccontext.DataLength = cpu_to_le32(8);
2112 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2113 (struct crt_twarp_ctxt, Name));
2114 buf->ccontext.NameLength = cpu_to_le16(4);
2115 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2120 buf->Timestamp = cpu_to_le64(timewarp);
2124 /* See MS-SMB2 2.2.13.2.7 */
2126 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2128 struct smb2_create_req *req = iov[0].iov_base;
2129 unsigned int num = *num_iovec;
2131 iov[num].iov_base = create_twarp_buf(timewarp);
2132 if (iov[num].iov_base == NULL)
2134 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2135 if (!req->CreateContextsOffset)
2136 req->CreateContextsOffset = cpu_to_le32(
2137 sizeof(struct smb2_create_req) +
2138 iov[num - 1].iov_len);
2139 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2140 *num_iovec = num + 1;
2144 static struct crt_query_id_ctxt *
2145 create_query_id_buf(void)
2147 struct crt_query_id_ctxt *buf;
2149 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2153 buf->ccontext.DataOffset = cpu_to_le16(0);
2154 buf->ccontext.DataLength = cpu_to_le32(0);
2155 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2156 (struct crt_query_id_ctxt, Name));
2157 buf->ccontext.NameLength = cpu_to_le16(4);
2158 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2166 /* See MS-SMB2 2.2.13.2.9 */
2168 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2170 struct smb2_create_req *req = iov[0].iov_base;
2171 unsigned int num = *num_iovec;
2173 iov[num].iov_base = create_query_id_buf();
2174 if (iov[num].iov_base == NULL)
2176 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2177 if (!req->CreateContextsOffset)
2178 req->CreateContextsOffset = cpu_to_le32(
2179 sizeof(struct smb2_create_req) +
2180 iov[num - 1].iov_len);
2181 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2182 *num_iovec = num + 1;
2187 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2188 const char *treename, const __le16 *path)
2190 int treename_len, path_len;
2191 struct nls_table *cp;
2192 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2197 treename_len = strlen(treename);
2198 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2204 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2207 * make room for one path separator between the treename and
2210 *out_len = treename_len + 1 + path_len;
2213 * final path needs to be null-terminated UTF16 with a
2217 *out_size = roundup((*out_len+1)*2, 8);
2218 *out_path = kzalloc(*out_size, GFP_KERNEL);
2222 cp = load_nls_default();
2223 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2224 UniStrcat(*out_path, sep);
2225 UniStrcat(*out_path, path);
2231 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2232 umode_t mode, struct cifs_tcon *tcon,
2233 const char *full_path,
2234 struct cifs_sb_info *cifs_sb)
2236 struct smb_rqst rqst;
2237 struct smb2_create_req *req;
2238 struct smb2_create_rsp *rsp = NULL;
2239 struct cifs_ses *ses = tcon->ses;
2240 struct kvec iov[3]; /* make sure at least one for each open context */
2241 struct kvec rsp_iov = {NULL, 0};
2244 __le16 *copy_path = NULL;
2247 unsigned int n_iov = 2;
2248 __u32 file_attributes = 0;
2249 char *pc_buf = NULL;
2251 unsigned int total_len;
2252 __le16 *utf16_path = NULL;
2254 cifs_dbg(FYI, "mkdir\n");
2256 /* resource #1: path allocation */
2257 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2261 if (!ses || !(ses->server)) {
2266 /* resource #2: request */
2267 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2272 if (smb3_encryption_required(tcon))
2273 flags |= CIFS_TRANSFORM_REQ;
2275 req->ImpersonationLevel = IL_IMPERSONATION;
2276 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2277 /* File attributes ignored on open (used in create though) */
2278 req->FileAttributes = cpu_to_le32(file_attributes);
2279 req->ShareAccess = FILE_SHARE_ALL_LE;
2280 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2281 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2283 iov[0].iov_base = (char *)req;
2284 /* -1 since last byte is buf[0] which is sent below (path) */
2285 iov[0].iov_len = total_len - 1;
2287 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2289 /* [MS-SMB2] 2.2.13 NameOffset:
2290 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2291 * the SMB2 header, the file name includes a prefix that will
2292 * be processed during DFS name normalization as specified in
2293 * section 3.3.5.9. Otherwise, the file name is relative to
2294 * the share that is identified by the TreeId in the SMB2
2297 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2300 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2301 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2303 tcon->treeName, utf16_path);
2307 req->NameLength = cpu_to_le16(name_len * 2);
2308 uni_path_len = copy_size;
2309 /* free before overwriting resource */
2311 utf16_path = copy_path;
2313 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2314 /* MUST set path len (NameLength) to 0 opening root of share */
2315 req->NameLength = cpu_to_le16(uni_path_len - 2);
2316 if (uni_path_len % 8 != 0) {
2317 copy_size = roundup(uni_path_len, 8);
2318 copy_path = kzalloc(copy_size, GFP_KERNEL);
2323 memcpy((char *)copy_path, (const char *)utf16_path,
2325 uni_path_len = copy_size;
2326 /* free before overwriting resource */
2328 utf16_path = copy_path;
2332 iov[1].iov_len = uni_path_len;
2333 iov[1].iov_base = utf16_path;
2334 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2336 if (tcon->posix_extensions) {
2337 /* resource #3: posix buf */
2338 rc = add_posix_context(iov, &n_iov, mode);
2341 pc_buf = iov[n_iov-1].iov_base;
2345 memset(&rqst, 0, sizeof(struct smb_rqst));
2347 rqst.rq_nvec = n_iov;
2349 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2350 FILE_WRITE_ATTRIBUTES);
2351 /* resource #4: response buffer */
2352 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2354 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2355 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2357 FILE_WRITE_ATTRIBUTES, rc);
2358 goto err_free_rsp_buf;
2361 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2362 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2363 ses->Suid, CREATE_NOT_FILE,
2364 FILE_WRITE_ATTRIBUTES);
2366 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2368 /* Eventually save off posix specific response info and timestaps */
2371 free_rsp_buf(resp_buftype, rsp);
2374 cifs_small_buf_release(req);
2381 SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2382 struct cifs_open_parms *oparms, __le16 *path)
2384 struct TCP_Server_Info *server = tcon->ses->server;
2385 struct smb2_create_req *req;
2386 unsigned int n_iov = 2;
2387 __u32 file_attributes = 0;
2390 unsigned int total_len;
2391 struct kvec *iov = rqst->rq_iov;
2395 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2399 iov[0].iov_base = (char *)req;
2400 /* -1 since last byte is buf[0] which is sent below (path) */
2401 iov[0].iov_len = total_len - 1;
2403 if (oparms->create_options & CREATE_OPTION_READONLY)
2404 file_attributes |= ATTR_READONLY;
2405 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2406 file_attributes |= ATTR_SYSTEM;
2408 req->ImpersonationLevel = IL_IMPERSONATION;
2409 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2410 /* File attributes ignored on open (used in create though) */
2411 req->FileAttributes = cpu_to_le32(file_attributes);
2412 req->ShareAccess = FILE_SHARE_ALL_LE;
2413 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2414 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2415 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2417 /* [MS-SMB2] 2.2.13 NameOffset:
2418 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2419 * the SMB2 header, the file name includes a prefix that will
2420 * be processed during DFS name normalization as specified in
2421 * section 3.3.5.9. Otherwise, the file name is relative to
2422 * the share that is identified by the TreeId in the SMB2
2425 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2428 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2429 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2431 tcon->treeName, path);
2434 req->NameLength = cpu_to_le16(name_len * 2);
2435 uni_path_len = copy_size;
2438 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2439 /* MUST set path len (NameLength) to 0 opening root of share */
2440 req->NameLength = cpu_to_le16(uni_path_len - 2);
2441 copy_size = uni_path_len;
2442 if (copy_size % 8 != 0)
2443 copy_size = roundup(copy_size, 8);
2444 copy_path = kzalloc(copy_size, GFP_KERNEL);
2447 memcpy((char *)copy_path, (const char *)path,
2449 uni_path_len = copy_size;
2453 iov[1].iov_len = uni_path_len;
2454 iov[1].iov_base = path;
2456 if (!server->oplocks)
2457 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2459 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2460 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2461 req->RequestedOplockLevel = *oplock;
2462 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2463 (oparms->create_options & CREATE_NOT_FILE))
2464 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2466 rc = add_lease_context(server, iov, &n_iov,
2467 oparms->fid->lease_key, oplock);
2472 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2473 /* need to set Next field of lease context if we request it */
2474 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2475 struct create_context *ccontext =
2476 (struct create_context *)iov[n_iov-1].iov_base;
2478 cpu_to_le32(server->vals->create_lease_size);
2481 rc = add_durable_context(iov, &n_iov, oparms,
2482 tcon->use_persistent);
2487 if (tcon->posix_extensions) {
2489 struct create_context *ccontext =
2490 (struct create_context *)iov[n_iov-1].iov_base;
2492 cpu_to_le32(iov[n_iov-1].iov_len);
2495 rc = add_posix_context(iov, &n_iov, oparms->mode);
2500 if (tcon->snapshot_time) {
2501 cifs_dbg(FYI, "adding snapshot context\n");
2503 struct create_context *ccontext =
2504 (struct create_context *)iov[n_iov-1].iov_base;
2506 cpu_to_le32(iov[n_iov-1].iov_len);
2509 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2515 struct create_context *ccontext =
2516 (struct create_context *)iov[n_iov-1].iov_base;
2517 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2519 add_query_id_context(iov, &n_iov);
2521 rqst->rq_nvec = n_iov;
2525 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2526 * All other vectors are freed by kfree().
2529 SMB2_open_free(struct smb_rqst *rqst)
2533 if (rqst && rqst->rq_iov) {
2534 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2535 for (i = 1; i < rqst->rq_nvec; i++)
2536 if (rqst->rq_iov[i].iov_base != smb2_padding)
2537 kfree(rqst->rq_iov[i].iov_base);
2542 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2543 __u8 *oplock, struct smb2_file_all_info *buf,
2544 struct kvec *err_iov, int *buftype)
2546 struct smb_rqst rqst;
2547 struct smb2_create_rsp *rsp = NULL;
2548 struct TCP_Server_Info *server;
2549 struct cifs_tcon *tcon = oparms->tcon;
2550 struct cifs_ses *ses = tcon->ses;
2551 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2552 struct kvec rsp_iov = {NULL, 0};
2553 int resp_buftype = CIFS_NO_BUFFER;
2557 cifs_dbg(FYI, "create/open\n");
2558 if (ses && (ses->server))
2559 server = ses->server;
2563 if (smb3_encryption_required(tcon))
2564 flags |= CIFS_TRANSFORM_REQ;
2566 memset(&rqst, 0, sizeof(struct smb_rqst));
2567 memset(&iov, 0, sizeof(iov));
2569 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2571 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2575 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2576 oparms->create_options, oparms->desired_access);
2578 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2580 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2583 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2584 if (err_iov && rsp) {
2586 *buftype = resp_buftype;
2587 resp_buftype = CIFS_NO_BUFFER;
2590 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2591 oparms->create_options, oparms->desired_access, rc);
2594 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2595 ses->Suid, oparms->create_options,
2596 oparms->desired_access);
2598 atomic_inc(&tcon->num_remote_opens);
2599 oparms->fid->persistent_fid = rsp->PersistentFileId;
2600 oparms->fid->volatile_fid = rsp->VolatileFileId;
2601 #ifdef CONFIG_CIFS_DEBUG2
2602 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2603 #endif /* CIFS_DEBUG2 */
2606 memcpy(buf, &rsp->CreationTime, 32);
2607 buf->AllocationSize = rsp->AllocationSize;
2608 buf->EndOfFile = rsp->EndofFile;
2609 buf->Attributes = rsp->FileAttributes;
2610 buf->NumberOfLinks = cpu_to_le32(1);
2611 buf->DeletePending = 0;
2615 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2616 oparms->fid->lease_key, oplock, buf);
2618 SMB2_open_free(&rqst);
2619 free_rsp_buf(resp_buftype, rsp);
2624 SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2625 u64 persistent_fid, u64 volatile_fid, u32 opcode,
2626 bool is_fsctl, char *in_data, u32 indatalen,
2627 __u32 max_response_size)
2629 struct smb2_ioctl_req *req;
2630 struct kvec *iov = rqst->rq_iov;
2631 unsigned int total_len;
2635 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
2641 * indatalen is usually small at a couple of bytes max, so
2642 * just allocate through generic pool
2644 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
2646 cifs_small_buf_release(req);
2651 req->CtlCode = cpu_to_le32(opcode);
2652 req->PersistentFileId = persistent_fid;
2653 req->VolatileFileId = volatile_fid;
2655 iov[0].iov_base = (char *)req;
2657 * If no input data, the size of ioctl struct in
2658 * protocol spec still includes a 1 byte data buffer,
2659 * but if input data passed to ioctl, we do not
2660 * want to double count this, so we do not send
2661 * the dummy one byte of data in iovec[0] if sending
2662 * input data (in iovec[1]).
2665 req->InputCount = cpu_to_le32(indatalen);
2666 /* do not set InputOffset if no input data */
2668 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
2670 iov[0].iov_len = total_len - 1;
2671 iov[1].iov_base = in_data_buf;
2672 iov[1].iov_len = indatalen;
2675 iov[0].iov_len = total_len;
2678 req->OutputOffset = 0;
2679 req->OutputCount = 0; /* MBZ */
2682 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2683 * We Could increase default MaxOutputResponse, but that could require
2684 * more credits. Windows typically sets this smaller, but for some
2685 * ioctls it may be useful to allow server to send more. No point
2686 * limiting what the server can send as long as fits in one credit
2687 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2688 * to increase this limit up in the future.
2689 * Note that for snapshot queries that servers like Azure expect that
2690 * the first query be minimal size (and just used to get the number/size
2691 * of previous versions) so response size must be specified as EXACTLY
2692 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2693 * of eight bytes. Currently that is the only case where we set max
2694 * response size smaller.
2696 req->MaxOutputResponse = cpu_to_le32(max_response_size);
2699 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2703 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2704 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
2705 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
2711 SMB2_ioctl_free(struct smb_rqst *rqst)
2714 if (rqst && rqst->rq_iov) {
2715 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2716 for (i = 1; i < rqst->rq_nvec; i++)
2717 if (rqst->rq_iov[i].iov_base != smb2_padding)
2718 kfree(rqst->rq_iov[i].iov_base);
2724 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2727 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2728 u64 volatile_fid, u32 opcode, bool is_fsctl,
2729 char *in_data, u32 indatalen, u32 max_out_data_len,
2730 char **out_data, u32 *plen /* returned data len */)
2732 struct smb_rqst rqst;
2733 struct smb2_ioctl_rsp *rsp = NULL;
2734 struct cifs_ses *ses;
2735 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
2736 struct kvec rsp_iov = {NULL, 0};
2737 int resp_buftype = CIFS_NO_BUFFER;
2741 cifs_dbg(FYI, "SMB2 IOCTL\n");
2743 if (out_data != NULL)
2746 /* zero out returned data len, in case of error */
2755 if (!ses || !(ses->server))
2758 if (smb3_encryption_required(tcon))
2759 flags |= CIFS_TRANSFORM_REQ;
2761 memset(&rqst, 0, sizeof(struct smb_rqst));
2762 memset(&iov, 0, sizeof(iov));
2764 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
2766 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
2767 is_fsctl, in_data, indatalen, max_out_data_len);
2771 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2773 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
2776 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2777 ses->Suid, 0, opcode, rc);
2779 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
2780 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2782 } else if (rc == -EINVAL) {
2783 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2784 (opcode != FSCTL_SRV_COPYCHUNK)) {
2785 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2788 } else if (rc == -E2BIG) {
2789 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
2790 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2795 /* check if caller wants to look at return data or just return rc */
2796 if ((plen == NULL) || (out_data == NULL))
2799 *plen = le32_to_cpu(rsp->OutputCount);
2801 /* We check for obvious errors in the output buffer length and offset */
2803 goto ioctl_exit; /* server returned no data */
2804 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
2805 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2811 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
2812 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2813 le32_to_cpu(rsp->OutputOffset));
2819 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2821 if (*out_data == NULL) {
2827 SMB2_ioctl_free(&rqst);
2828 free_rsp_buf(resp_buftype, rsp);
2833 * Individual callers to ioctl worker function follow
2837 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2838 u64 persistent_fid, u64 volatile_fid)
2841 struct compress_ioctl fsctl_input;
2842 char *ret_data = NULL;
2844 fsctl_input.CompressionState =
2845 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
2847 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2848 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2849 (char *)&fsctl_input /* data input */,
2850 2 /* in data len */, CIFSMaxBufSize /* max out data */,
2851 &ret_data /* out data */, NULL);
2853 cifs_dbg(FYI, "set compression rc %d\n", rc);
2859 SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2860 u64 persistent_fid, u64 volatile_fid)
2862 struct smb2_close_req *req;
2863 struct kvec *iov = rqst->rq_iov;
2864 unsigned int total_len;
2867 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2871 req->PersistentFileId = persistent_fid;
2872 req->VolatileFileId = volatile_fid;
2873 iov[0].iov_base = (char *)req;
2874 iov[0].iov_len = total_len;
2880 SMB2_close_free(struct smb_rqst *rqst)
2882 if (rqst && rqst->rq_iov)
2883 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2887 SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2888 u64 persistent_fid, u64 volatile_fid, int flags)
2890 struct smb_rqst rqst;
2891 struct smb2_close_rsp *rsp = NULL;
2892 struct cifs_ses *ses = tcon->ses;
2894 struct kvec rsp_iov;
2895 int resp_buftype = CIFS_NO_BUFFER;
2898 cifs_dbg(FYI, "Close\n");
2900 if (!ses || !(ses->server))
2903 if (smb3_encryption_required(tcon))
2904 flags |= CIFS_TRANSFORM_REQ;
2906 memset(&rqst, 0, sizeof(struct smb_rqst));
2907 memset(&iov, 0, sizeof(iov));
2911 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid);
2915 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2916 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2919 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
2920 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2925 atomic_dec(&tcon->num_remote_opens);
2927 /* BB FIXME - decode close response, update inode for caching */
2930 SMB2_close_free(&rqst);
2931 free_rsp_buf(resp_buftype, rsp);
2936 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2937 u64 persistent_fid, u64 volatile_fid)
2939 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2943 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
2944 struct kvec *iov, unsigned int min_buf_size)
2946 unsigned int smb_len = iov->iov_len;
2947 char *end_of_smb = smb_len + (char *)iov->iov_base;
2948 char *begin_of_buf = offset + (char *)iov->iov_base;
2949 char *end_of_buf = begin_of_buf + buffer_length;
2952 if (buffer_length < min_buf_size) {
2953 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2954 buffer_length, min_buf_size);
2958 /* check if beyond RFC1001 maximum length */
2959 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
2960 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2961 buffer_length, smb_len);
2965 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
2966 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
2974 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2975 * Caller must free buffer.
2978 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
2979 struct kvec *iov, unsigned int minbufsize,
2982 char *begin_of_buf = offset + (char *)iov->iov_base;
2988 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
2992 memcpy(data, begin_of_buf, buffer_length);
2998 SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2999 u64 persistent_fid, u64 volatile_fid,
3000 u8 info_class, u8 info_type, u32 additional_info,
3001 size_t output_len, size_t input_len, void *input)
3003 struct smb2_query_info_req *req;
3004 struct kvec *iov = rqst->rq_iov;
3005 unsigned int total_len;
3008 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3013 req->InfoType = info_type;
3014 req->FileInfoClass = info_class;
3015 req->PersistentFileId = persistent_fid;
3016 req->VolatileFileId = volatile_fid;
3017 req->AdditionalInformation = cpu_to_le32(additional_info);
3019 req->OutputBufferLength = cpu_to_le32(output_len);
3021 req->InputBufferLength = cpu_to_le32(input_len);
3022 /* total_len for smb query request never close to le16 max */
3023 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3024 memcpy(req->Buffer, input, input_len);
3027 iov[0].iov_base = (char *)req;
3029 iov[0].iov_len = total_len - 1 + input_len;
3034 SMB2_query_info_free(struct smb_rqst *rqst)
3036 if (rqst && rqst->rq_iov)
3037 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3041 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3042 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3043 u32 additional_info, size_t output_len, size_t min_len, void **data,
3046 struct smb_rqst rqst;
3047 struct smb2_query_info_rsp *rsp = NULL;
3049 struct kvec rsp_iov;
3051 int resp_buftype = CIFS_NO_BUFFER;
3052 struct cifs_ses *ses = tcon->ses;
3054 bool allocated = false;
3056 cifs_dbg(FYI, "Query Info\n");
3058 if (!ses || !(ses->server))
3061 if (smb3_encryption_required(tcon))
3062 flags |= CIFS_TRANSFORM_REQ;
3064 memset(&rqst, 0, sizeof(struct smb_rqst));
3065 memset(&iov, 0, sizeof(iov));
3069 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
3070 info_class, info_type, additional_info,
3071 output_len, 0, NULL);
3075 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3076 ses->Suid, info_class, (__u32)info_type);
3078 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3079 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3082 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3083 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3084 ses->Suid, info_class, (__u32)info_type, rc);
3088 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3089 ses->Suid, info_class, (__u32)info_type);
3092 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3094 *data = kmalloc(*dlen, GFP_KERNEL);
3097 "Error %d allocating memory for acl\n",
3107 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3108 le32_to_cpu(rsp->OutputBufferLength),
3109 &rsp_iov, min_len, *data);
3110 if (rc && allocated) {
3117 SMB2_query_info_free(&rqst);
3118 free_rsp_buf(resp_buftype, rsp);
3122 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3123 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3125 return query_info(xid, tcon, persistent_fid, volatile_fid,
3126 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3127 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3128 sizeof(struct smb2_file_all_info), (void **)&data,
3133 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3134 u64 persistent_fid, u64 volatile_fid,
3135 void **data, u32 *plen)
3137 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3140 return query_info(xid, tcon, persistent_fid, volatile_fid,
3141 0, SMB2_O_INFO_SECURITY, additional_info,
3142 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3146 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3147 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3149 return query_info(xid, tcon, persistent_fid, volatile_fid,
3150 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3151 sizeof(struct smb2_file_internal_info),
3152 sizeof(struct smb2_file_internal_info),
3153 (void **)&uniqueid, NULL);
3157 * This is a no-op for now. We're not really interested in the reply, but
3158 * rather in the fact that the server sent one and that server->lstrp
3161 * FIXME: maybe we should consider checking that the reply matches request?
3164 smb2_echo_callback(struct mid_q_entry *mid)
3166 struct TCP_Server_Info *server = mid->callback_data;
3167 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3168 struct cifs_credits credits = { .value = 0, .instance = 0 };
3170 if (mid->mid_state == MID_RESPONSE_RECEIVED
3171 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3172 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3173 credits.instance = server->reconnect_instance;
3176 DeleteMidQEntry(mid);
3177 add_credits(server, &credits, CIFS_ECHO_OP);
3180 void smb2_reconnect_server(struct work_struct *work)
3182 struct TCP_Server_Info *server = container_of(work,
3183 struct TCP_Server_Info, reconnect.work);
3184 struct cifs_ses *ses;
3185 struct cifs_tcon *tcon, *tcon2;
3186 struct list_head tmp_list;
3187 int tcon_exist = false;
3189 int resched = false;
3192 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3193 mutex_lock(&server->reconnect_mutex);
3195 INIT_LIST_HEAD(&tmp_list);
3196 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3198 spin_lock(&cifs_tcp_ses_lock);
3199 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3200 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3201 if (tcon->need_reconnect || tcon->need_reopen_files) {
3203 list_add_tail(&tcon->rlist, &tmp_list);
3208 * IPC has the same lifetime as its session and uses its
3211 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3212 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3218 * Get the reference to server struct to be sure that the last call of
3219 * cifs_put_tcon() in the loop below won't release the server pointer.
3222 server->srv_count++;
3224 spin_unlock(&cifs_tcp_ses_lock);
3226 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3227 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3229 cifs_reopen_persistent_handles(tcon);
3232 list_del_init(&tcon->rlist);
3234 cifs_put_smb_ses(tcon->ses);
3236 cifs_put_tcon(tcon);
3239 cifs_dbg(FYI, "Reconnecting tcons finished\n");
3241 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3242 mutex_unlock(&server->reconnect_mutex);
3244 /* now we can safely release srv struct */
3246 cifs_put_tcp_session(server, 1);
3250 SMB2_echo(struct TCP_Server_Info *server)
3252 struct smb2_echo_req *req;
3255 struct smb_rqst rqst = { .rq_iov = iov,
3257 unsigned int total_len;
3259 cifs_dbg(FYI, "In echo request\n");
3261 if (server->tcpStatus == CifsNeedNegotiate) {
3262 /* No need to send echo on newly established connections */
3263 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3267 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
3271 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3273 iov[0].iov_len = total_len;
3274 iov[0].iov_base = (char *)req;
3276 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3277 server, CIFS_ECHO_OP, NULL);
3279 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3281 cifs_small_buf_release(req);
3286 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3289 struct smb_rqst rqst;
3290 struct smb2_flush_req *req;
3291 struct cifs_ses *ses = tcon->ses;
3293 struct kvec rsp_iov;
3297 unsigned int total_len;
3299 cifs_dbg(FYI, "Flush\n");
3301 if (!ses || !(ses->server))
3304 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
3308 if (smb3_encryption_required(tcon))
3309 flags |= CIFS_TRANSFORM_REQ;
3311 req->PersistentFileId = persistent_fid;
3312 req->VolatileFileId = volatile_fid;
3314 iov[0].iov_base = (char *)req;
3315 iov[0].iov_len = total_len;
3317 memset(&rqst, 0, sizeof(struct smb_rqst));
3321 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3322 cifs_small_buf_release(req);
3325 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
3326 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3330 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3335 * To form a chain of read requests, any read requests after the first should
3336 * have the end_of_chain boolean set to true.
3339 smb2_new_read_req(void **buf, unsigned int *total_len,
3340 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3341 unsigned int remaining_bytes, int request_type)
3344 struct smb2_read_plain_req *req = NULL;
3345 struct smb2_sync_hdr *shdr;
3346 struct TCP_Server_Info *server;
3348 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3353 server = io_parms->tcon->ses->server;
3355 return -ECONNABORTED;
3357 shdr = &req->sync_hdr;
3358 shdr->ProcessId = cpu_to_le32(io_parms->pid);
3360 req->PersistentFileId = io_parms->persistent_fid;
3361 req->VolatileFileId = io_parms->volatile_fid;
3362 req->ReadChannelInfoOffset = 0; /* reserved */
3363 req->ReadChannelInfoLength = 0; /* reserved */
3364 req->Channel = 0; /* reserved */
3365 req->MinimumCount = 0;
3366 req->Length = cpu_to_le32(io_parms->length);
3367 req->Offset = cpu_to_le64(io_parms->offset);
3369 trace_smb3_read_enter(0 /* xid */,
3370 io_parms->persistent_fid,
3371 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3372 io_parms->offset, io_parms->length);
3373 #ifdef CONFIG_CIFS_SMB_DIRECT
3375 * If we want to do a RDMA write, fill in and append
3376 * smbd_buffer_descriptor_v1 to the end of read request
3378 if (server->rdma && rdata && !server->sign &&
3379 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
3381 struct smbd_buffer_descriptor_v1 *v1;
3382 bool need_invalidate =
3383 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3385 rdata->mr = smbd_register_mr(
3386 server->smbd_conn, rdata->pages,
3387 rdata->nr_pages, rdata->page_offset,
3388 rdata->tailsz, true, need_invalidate);
3392 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3393 if (need_invalidate)
3394 req->Channel = SMB2_CHANNEL_RDMA_V1;
3395 req->ReadChannelInfoOffset =
3396 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
3397 req->ReadChannelInfoLength =
3398 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3399 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3400 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3401 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3402 v1->length = cpu_to_le32(rdata->mr->mr->length);
3404 *total_len += sizeof(*v1) - 1;
3407 if (request_type & CHAINED_REQUEST) {
3408 if (!(request_type & END_OF_CHAIN)) {
3409 /* next 8-byte aligned request */
3410 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3411 shdr->NextCommand = cpu_to_le32(*total_len);
3412 } else /* END_OF_CHAIN */
3413 shdr->NextCommand = 0;
3414 if (request_type & RELATED_REQUEST) {
3415 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
3417 * Related requests use info from previous read request
3420 shdr->SessionId = 0xFFFFFFFF;
3421 shdr->TreeId = 0xFFFFFFFF;
3422 req->PersistentFileId = 0xFFFFFFFF;
3423 req->VolatileFileId = 0xFFFFFFFF;
3426 if (remaining_bytes > io_parms->length)
3427 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3429 req->RemainingBytes = 0;
3436 smb2_readv_callback(struct mid_q_entry *mid)
3438 struct cifs_readdata *rdata = mid->callback_data;
3439 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3440 struct TCP_Server_Info *server = tcon->ses->server;
3441 struct smb2_sync_hdr *shdr =
3442 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
3443 struct cifs_credits credits = { .value = 0, .instance = 0 };
3444 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3446 .rq_pages = rdata->pages,
3447 .rq_offset = rdata->page_offset,
3448 .rq_npages = rdata->nr_pages,
3449 .rq_pagesz = rdata->pagesz,
3450 .rq_tailsz = rdata->tailsz };
3452 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3453 __func__, mid->mid, mid->mid_state, rdata->result,
3456 switch (mid->mid_state) {
3457 case MID_RESPONSE_RECEIVED:
3458 credits.value = le16_to_cpu(shdr->CreditRequest);
3459 credits.instance = server->reconnect_instance;
3460 /* result already set, check signature */
3461 if (server->sign && !mid->decrypted) {
3464 rc = smb2_verify_signature(&rqst, server);
3466 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
3469 /* FIXME: should this be counted toward the initiating task? */
3470 task_io_account_read(rdata->got_bytes);
3471 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3473 case MID_REQUEST_SUBMITTED:
3474 case MID_RETRY_NEEDED:
3475 rdata->result = -EAGAIN;
3476 if (server->sign && rdata->got_bytes)
3477 /* reset bytes number since we can not check a sign */
3478 rdata->got_bytes = 0;
3479 /* FIXME: should this be counted toward the initiating task? */
3480 task_io_account_read(rdata->got_bytes);
3481 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3483 case MID_RESPONSE_MALFORMED:
3484 credits.value = le16_to_cpu(shdr->CreditRequest);
3485 credits.instance = server->reconnect_instance;
3488 rdata->result = -EIO;
3490 #ifdef CONFIG_CIFS_SMB_DIRECT
3492 * If this rdata has a memmory registered, the MR can be freed
3493 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3494 * because they have limited number and are used for future I/Os
3497 smbd_deregister_mr(rdata->mr);
3501 if (rdata->result && rdata->result != -ENODATA) {
3502 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
3503 trace_smb3_read_err(0 /* xid */,
3504 rdata->cfile->fid.persistent_fid,
3505 tcon->tid, tcon->ses->Suid, rdata->offset,
3506 rdata->bytes, rdata->result);
3508 trace_smb3_read_done(0 /* xid */,
3509 rdata->cfile->fid.persistent_fid,
3510 tcon->tid, tcon->ses->Suid,
3511 rdata->offset, rdata->got_bytes);
3513 queue_work(cifsiod_wq, &rdata->work);
3514 DeleteMidQEntry(mid);
3515 add_credits(server, &credits, 0);
3518 /* smb2_async_readv - send an async read, and set up mid to handle result */
3520 smb2_async_readv(struct cifs_readdata *rdata)
3524 struct smb2_sync_hdr *shdr;
3525 struct cifs_io_parms io_parms;
3526 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3528 struct TCP_Server_Info *server;
3529 unsigned int total_len;
3531 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3532 __func__, rdata->offset, rdata->bytes);
3534 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3535 io_parms.offset = rdata->offset;
3536 io_parms.length = rdata->bytes;
3537 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3538 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3539 io_parms.pid = rdata->pid;
3541 server = io_parms.tcon->ses->server;
3543 rc = smb2_new_read_req(
3544 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
3548 if (smb3_encryption_required(io_parms.tcon))
3549 flags |= CIFS_TRANSFORM_REQ;
3551 rdata->iov[0].iov_base = buf;
3552 rdata->iov[0].iov_len = total_len;
3554 shdr = (struct smb2_sync_hdr *)buf;
3556 if (rdata->credits.value > 0) {
3557 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
3558 SMB2_MAX_BUFFER_SIZE));
3559 shdr->CreditRequest =
3560 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
3562 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3564 goto async_readv_out;
3566 flags |= CIFS_HAS_CREDITS;
3569 kref_get(&rdata->refcount);
3570 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
3571 cifs_readv_receive, smb2_readv_callback,
3572 smb3_handle_read_data, rdata, flags,
3575 kref_put(&rdata->refcount, cifs_readdata_release);
3576 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
3577 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3579 io_parms.tcon->ses->Suid,
3580 io_parms.offset, io_parms.length, rc);
3584 cifs_small_buf_release(buf);
3589 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3590 unsigned int *nbytes, char **buf, int *buf_type)
3592 struct smb_rqst rqst;
3593 int resp_buftype, rc = -EACCES;
3594 struct smb2_read_plain_req *req = NULL;
3595 struct smb2_read_rsp *rsp = NULL;
3597 struct kvec rsp_iov;
3598 unsigned int total_len;
3599 int flags = CIFS_LOG_ERROR;
3600 struct cifs_ses *ses = io_parms->tcon->ses;
3603 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
3607 if (smb3_encryption_required(io_parms->tcon))
3608 flags |= CIFS_TRANSFORM_REQ;
3610 iov[0].iov_base = (char *)req;
3611 iov[0].iov_len = total_len;
3613 memset(&rqst, 0, sizeof(struct smb_rqst));
3617 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3618 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
3621 if (rc != -ENODATA) {
3622 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3623 cifs_dbg(VFS, "Send error in read = %d\n", rc);
3624 trace_smb3_read_err(xid, req->PersistentFileId,
3625 io_parms->tcon->tid, ses->Suid,
3626 io_parms->offset, io_parms->length,
3629 trace_smb3_read_done(xid, req->PersistentFileId,
3630 io_parms->tcon->tid, ses->Suid,
3631 io_parms->offset, 0);
3632 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3633 cifs_small_buf_release(req);
3634 return rc == -ENODATA ? 0 : rc;
3636 trace_smb3_read_done(xid, req->PersistentFileId,
3637 io_parms->tcon->tid, ses->Suid,
3638 io_parms->offset, io_parms->length);
3640 cifs_small_buf_release(req);
3642 *nbytes = le32_to_cpu(rsp->DataLength);
3643 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3644 (*nbytes > io_parms->length)) {
3645 cifs_dbg(FYI, "bad length %d for count %d\n",
3646 *nbytes, io_parms->length);
3652 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
3653 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3654 } else if (resp_buftype != CIFS_NO_BUFFER) {
3655 *buf = rsp_iov.iov_base;
3656 if (resp_buftype == CIFS_SMALL_BUFFER)
3657 *buf_type = CIFS_SMALL_BUFFER;
3658 else if (resp_buftype == CIFS_LARGE_BUFFER)
3659 *buf_type = CIFS_LARGE_BUFFER;
3665 * Check the mid_state and signature on received buffer (if any), and queue the
3666 * workqueue completion task.
3669 smb2_writev_callback(struct mid_q_entry *mid)
3671 struct cifs_writedata *wdata = mid->callback_data;
3672 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3673 struct TCP_Server_Info *server = tcon->ses->server;
3674 unsigned int written;
3675 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
3676 struct cifs_credits credits = { .value = 0, .instance = 0 };
3678 switch (mid->mid_state) {
3679 case MID_RESPONSE_RECEIVED:
3680 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3681 credits.instance = server->reconnect_instance;
3682 wdata->result = smb2_check_receive(mid, server, 0);
3683 if (wdata->result != 0)
3686 written = le32_to_cpu(rsp->DataLength);
3688 * Mask off high 16 bits when bytes written as returned
3689 * by the server is greater than bytes requested by the
3690 * client. OS/2 servers are known to set incorrect
3693 if (written > wdata->bytes)
3696 if (written < wdata->bytes)
3697 wdata->result = -ENOSPC;
3699 wdata->bytes = written;
3701 case MID_REQUEST_SUBMITTED:
3702 case MID_RETRY_NEEDED:
3703 wdata->result = -EAGAIN;
3705 case MID_RESPONSE_MALFORMED:
3706 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3707 credits.instance = server->reconnect_instance;
3710 wdata->result = -EIO;
3713 #ifdef CONFIG_CIFS_SMB_DIRECT
3715 * If this wdata has a memory registered, the MR can be freed
3716 * The number of MRs available is limited, it's important to recover
3717 * used MR as soon as I/O is finished. Hold MR longer in the later
3718 * I/O process can possibly result in I/O deadlock due to lack of MR
3719 * to send request on I/O retry
3722 smbd_deregister_mr(wdata->mr);
3726 if (wdata->result) {
3727 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3728 trace_smb3_write_err(0 /* no xid */,
3729 wdata->cfile->fid.persistent_fid,
3730 tcon->tid, tcon->ses->Suid, wdata->offset,
3731 wdata->bytes, wdata->result);
3733 trace_smb3_write_done(0 /* no xid */,
3734 wdata->cfile->fid.persistent_fid,
3735 tcon->tid, tcon->ses->Suid,
3736 wdata->offset, wdata->bytes);
3738 queue_work(cifsiod_wq, &wdata->work);
3739 DeleteMidQEntry(mid);
3740 add_credits(server, &credits, 0);
3743 /* smb2_async_writev - send an async write, and set up mid to handle result */
3745 smb2_async_writev(struct cifs_writedata *wdata,
3746 void (*release)(struct kref *kref))
3748 int rc = -EACCES, flags = 0;
3749 struct smb2_write_req *req = NULL;
3750 struct smb2_sync_hdr *shdr;
3751 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3752 struct TCP_Server_Info *server = tcon->ses->server;
3754 struct smb_rqst rqst = { };
3755 unsigned int total_len;
3757 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
3761 if (smb3_encryption_required(tcon))
3762 flags |= CIFS_TRANSFORM_REQ;
3764 shdr = (struct smb2_sync_hdr *)req;
3765 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
3767 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3768 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3769 req->WriteChannelInfoOffset = 0;
3770 req->WriteChannelInfoLength = 0;
3772 req->Offset = cpu_to_le64(wdata->offset);
3773 req->DataOffset = cpu_to_le16(
3774 offsetof(struct smb2_write_req, Buffer));
3775 req->RemainingBytes = 0;
3777 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
3778 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
3779 #ifdef CONFIG_CIFS_SMB_DIRECT
3781 * If we want to do a server RDMA read, fill in and append
3782 * smbd_buffer_descriptor_v1 to the end of write request
3784 if (server->rdma && !server->sign && wdata->bytes >=
3785 server->smbd_conn->rdma_readwrite_threshold) {
3787 struct smbd_buffer_descriptor_v1 *v1;
3788 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3790 wdata->mr = smbd_register_mr(
3791 server->smbd_conn, wdata->pages,
3792 wdata->nr_pages, wdata->page_offset,
3793 wdata->tailsz, false, need_invalidate);
3796 goto async_writev_out;
3799 req->DataOffset = 0;
3800 if (wdata->nr_pages > 1)
3801 req->RemainingBytes =
3803 (wdata->nr_pages - 1) * wdata->pagesz -
3804 wdata->page_offset + wdata->tailsz
3807 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
3808 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3809 if (need_invalidate)
3810 req->Channel = SMB2_CHANNEL_RDMA_V1;
3811 req->WriteChannelInfoOffset =
3812 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
3813 req->WriteChannelInfoLength =
3814 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3815 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3816 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3817 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3818 v1->length = cpu_to_le32(wdata->mr->mr->length);
3821 iov[0].iov_len = total_len - 1;
3822 iov[0].iov_base = (char *)req;
3826 rqst.rq_pages = wdata->pages;
3827 rqst.rq_offset = wdata->page_offset;
3828 rqst.rq_npages = wdata->nr_pages;
3829 rqst.rq_pagesz = wdata->pagesz;
3830 rqst.rq_tailsz = wdata->tailsz;
3831 #ifdef CONFIG_CIFS_SMB_DIRECT
3833 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
3837 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3838 wdata->offset, wdata->bytes);
3840 #ifdef CONFIG_CIFS_SMB_DIRECT
3841 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3843 req->Length = cpu_to_le32(wdata->bytes);
3845 req->Length = cpu_to_le32(wdata->bytes);
3848 if (wdata->credits.value > 0) {
3849 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
3850 SMB2_MAX_BUFFER_SIZE));
3851 shdr->CreditRequest =
3852 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
3854 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3856 goto async_writev_out;
3858 flags |= CIFS_HAS_CREDITS;
3861 kref_get(&wdata->refcount);
3862 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3863 wdata, flags, &wdata->credits);
3866 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
3867 tcon->tid, tcon->ses->Suid, wdata->offset,
3869 kref_put(&wdata->refcount, release);
3870 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3874 cifs_small_buf_release(req);
3879 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3880 * The length field from io_parms must be at least 1 and indicates a number of
3881 * elements with data to write that begins with position 1 in iov array. All
3882 * data length is specified by count.
3885 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3886 unsigned int *nbytes, struct kvec *iov, int n_vec)
3888 struct smb_rqst rqst;
3890 struct smb2_write_req *req = NULL;
3891 struct smb2_write_rsp *rsp = NULL;
3893 struct kvec rsp_iov;
3895 unsigned int total_len;
3902 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3907 if (io_parms->tcon->ses->server == NULL)
3908 return -ECONNABORTED;
3910 if (smb3_encryption_required(io_parms->tcon))
3911 flags |= CIFS_TRANSFORM_REQ;
3913 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
3915 req->PersistentFileId = io_parms->persistent_fid;
3916 req->VolatileFileId = io_parms->volatile_fid;
3917 req->WriteChannelInfoOffset = 0;
3918 req->WriteChannelInfoLength = 0;
3920 req->Length = cpu_to_le32(io_parms->length);
3921 req->Offset = cpu_to_le64(io_parms->offset);
3922 req->DataOffset = cpu_to_le16(
3923 offsetof(struct smb2_write_req, Buffer));
3924 req->RemainingBytes = 0;
3926 trace_smb3_write_enter(xid, io_parms->persistent_fid,
3927 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3928 io_parms->offset, io_parms->length);
3930 iov[0].iov_base = (char *)req;
3932 iov[0].iov_len = total_len - 1;
3934 memset(&rqst, 0, sizeof(struct smb_rqst));
3936 rqst.rq_nvec = n_vec + 1;
3938 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
3939 &resp_buftype, flags, &rsp_iov);
3940 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
3943 trace_smb3_write_err(xid, req->PersistentFileId,
3944 io_parms->tcon->tid,
3945 io_parms->tcon->ses->Suid,
3946 io_parms->offset, io_parms->length, rc);
3947 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
3948 cifs_dbg(VFS, "Send error in write = %d\n", rc);
3950 *nbytes = le32_to_cpu(rsp->DataLength);
3951 trace_smb3_write_done(xid, req->PersistentFileId,
3952 io_parms->tcon->tid,
3953 io_parms->tcon->ses->Suid,
3954 io_parms->offset, *nbytes);
3957 cifs_small_buf_release(req);
3958 free_rsp_buf(resp_buftype, rsp);
3963 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3966 unsigned int entrycount = 0;
3967 unsigned int next_offset = 0;
3969 FILE_DIRECTORY_INFO *dir_info;
3971 if (bufstart == NULL)
3974 entryptr = bufstart;
3977 if (entryptr + next_offset < entryptr ||
3978 entryptr + next_offset > end_of_buf ||
3979 entryptr + next_offset + size > end_of_buf) {
3980 cifs_dbg(VFS, "malformed search entry would overflow\n");
3984 entryptr = entryptr + next_offset;
3985 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
3987 len = le32_to_cpu(dir_info->FileNameLength);
3988 if (entryptr + len < entryptr ||
3989 entryptr + len > end_of_buf ||
3990 entryptr + len + size > end_of_buf) {
3991 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3996 *lastentry = entryptr;
3999 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
4011 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4012 u64 persistent_fid, u64 volatile_fid, int index,
4013 struct cifs_search_info *srch_inf)
4015 struct smb_rqst rqst;
4016 struct smb2_query_directory_req *req;
4017 struct smb2_query_directory_rsp *rsp = NULL;
4019 struct kvec rsp_iov;
4022 int resp_buftype = CIFS_NO_BUFFER;
4023 unsigned char *bufptr;
4024 struct TCP_Server_Info *server;
4025 struct cifs_ses *ses = tcon->ses;
4026 __le16 asteriks = cpu_to_le16('*');
4028 unsigned int output_size = CIFSMaxBufSize;
4029 size_t info_buf_size;
4031 unsigned int total_len;
4033 if (ses && (ses->server))
4034 server = ses->server;
4038 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
4043 if (smb3_encryption_required(tcon))
4044 flags |= CIFS_TRANSFORM_REQ;
4046 switch (srch_inf->info_level) {
4047 case SMB_FIND_FILE_DIRECTORY_INFO:
4048 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4049 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4051 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4052 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4053 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4056 cifs_dbg(VFS, "info level %u isn't supported\n",
4057 srch_inf->info_level);
4062 req->FileIndex = cpu_to_le32(index);
4063 req->PersistentFileId = persistent_fid;
4064 req->VolatileFileId = volatile_fid;
4067 bufptr = req->Buffer;
4068 memcpy(bufptr, &asteriks, len);
4070 req->FileNameOffset =
4071 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
4072 req->FileNameLength = cpu_to_le16(len);
4074 * BB could be 30 bytes or so longer if we used SMB2 specific
4075 * buffer lengths, but this is safe and close enough.
4077 output_size = min_t(unsigned int, output_size, server->maxBuf);
4078 output_size = min_t(unsigned int, output_size, 2 << 15);
4079 req->OutputBufferLength = cpu_to_le32(output_size);
4081 iov[0].iov_base = (char *)req;
4083 iov[0].iov_len = total_len - 1;
4085 iov[1].iov_base = (char *)(req->Buffer);
4086 iov[1].iov_len = len;
4088 memset(&rqst, 0, sizeof(struct smb_rqst));
4092 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4093 tcon->ses->Suid, index, output_size);
4095 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4096 cifs_small_buf_release(req);
4097 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
4100 if (rc == -ENODATA &&
4101 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
4102 trace_smb3_query_dir_done(xid, persistent_fid,
4103 tcon->tid, tcon->ses->Suid, index, 0);
4104 srch_inf->endOfSearch = true;
4107 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4108 tcon->ses->Suid, index, 0, rc);
4109 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
4114 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4115 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4118 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4119 tcon->ses->Suid, index, 0, rc);
4123 srch_inf->unicode = true;
4125 if (srch_inf->ntwrk_buf_start) {
4126 if (srch_inf->smallBuf)
4127 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4129 cifs_buf_release(srch_inf->ntwrk_buf_start);
4131 srch_inf->ntwrk_buf_start = (char *)rsp;
4132 srch_inf->srch_entries_start = srch_inf->last_entry =
4133 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4134 end_of_smb = rsp_iov.iov_len + (char *)rsp;
4135 srch_inf->entries_in_buffer =
4136 num_entries(srch_inf->srch_entries_start, end_of_smb,
4137 &srch_inf->last_entry, info_buf_size);
4138 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4139 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4140 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4141 srch_inf->srch_entries_start, srch_inf->last_entry);
4142 if (resp_buftype == CIFS_LARGE_BUFFER)
4143 srch_inf->smallBuf = false;
4144 else if (resp_buftype == CIFS_SMALL_BUFFER)
4145 srch_inf->smallBuf = true;
4147 cifs_dbg(VFS, "illegal search buffer type\n");
4149 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4150 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
4154 free_rsp_buf(resp_buftype, rsp);
4159 SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
4160 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4161 u8 info_type, u32 additional_info,
4162 void **data, unsigned int *size)
4164 struct smb2_set_info_req *req;
4165 struct kvec *iov = rqst->rq_iov;
4166 unsigned int i, total_len;
4169 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
4173 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4174 req->InfoType = info_type;
4175 req->FileInfoClass = info_class;
4176 req->PersistentFileId = persistent_fid;
4177 req->VolatileFileId = volatile_fid;
4178 req->AdditionalInformation = cpu_to_le32(additional_info);
4181 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
4182 req->BufferLength = cpu_to_le32(*size);
4184 memcpy(req->Buffer, *data, *size);
4187 iov[0].iov_base = (char *)req;
4189 iov[0].iov_len = total_len - 1;
4191 for (i = 1; i < rqst->rq_nvec; i++) {
4192 le32_add_cpu(&req->BufferLength, size[i]);
4193 iov[i].iov_base = (char *)data[i];
4194 iov[i].iov_len = size[i];
4201 SMB2_set_info_free(struct smb_rqst *rqst)
4203 if (rqst && rqst->rq_iov)
4204 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
4208 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4209 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4210 u8 info_type, u32 additional_info, unsigned int num,
4211 void **data, unsigned int *size)
4213 struct smb_rqst rqst;
4214 struct smb2_set_info_rsp *rsp = NULL;
4216 struct kvec rsp_iov;
4219 struct cifs_ses *ses = tcon->ses;
4222 if (!ses || !(ses->server))
4228 if (smb3_encryption_required(tcon))
4229 flags |= CIFS_TRANSFORM_REQ;
4231 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4235 memset(&rqst, 0, sizeof(struct smb_rqst));
4239 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4240 info_class, info_type, additional_info,
4248 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
4250 SMB2_set_info_free(&rqst);
4251 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
4254 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
4255 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4256 ses->Suid, info_class, (__u32)info_type, rc);
4259 free_rsp_buf(resp_buftype, rsp);
4265 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4266 u64 volatile_fid, u32 pid, __le64 *eof)
4268 struct smb2_file_eof_info info;
4272 info.EndOfFile = *eof;
4275 size = sizeof(struct smb2_file_eof_info);
4277 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4278 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4279 0, 1, &data, &size);
4283 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4284 u64 persistent_fid, u64 volatile_fid,
4285 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4287 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4288 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4289 1, (void **)&pnntsd, &pacllen);
4293 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4294 u64 persistent_fid, u64 volatile_fid,
4295 struct smb2_file_full_ea_info *buf, int len)
4297 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4298 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4299 0, 1, (void **)&buf, &len);
4303 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4304 const u64 persistent_fid, const u64 volatile_fid,
4307 struct smb_rqst rqst;
4309 struct smb2_oplock_break *req = NULL;
4310 struct cifs_ses *ses = tcon->ses;
4311 int flags = CIFS_OBREAK_OP;
4312 unsigned int total_len;
4314 struct kvec rsp_iov;
4317 cifs_dbg(FYI, "SMB2_oplock_break\n");
4318 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4323 if (smb3_encryption_required(tcon))
4324 flags |= CIFS_TRANSFORM_REQ;
4326 req->VolatileFid = volatile_fid;
4327 req->PersistentFid = persistent_fid;
4328 req->OplockLevel = oplock_level;
4329 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4331 flags |= CIFS_NO_RSP_BUF;
4333 iov[0].iov_base = (char *)req;
4334 iov[0].iov_len = total_len;
4336 memset(&rqst, 0, sizeof(struct smb_rqst));
4340 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4341 cifs_small_buf_release(req);
4344 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4345 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
4352 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4353 struct kstatfs *kst)
4355 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4356 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4357 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
4358 kst->f_bfree = kst->f_bavail =
4359 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
4364 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4365 struct kstatfs *kst)
4367 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4368 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4369 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4370 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4371 kst->f_bavail = kst->f_bfree;
4373 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4374 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4375 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4376 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4377 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4383 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4384 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4387 struct smb2_query_info_req *req;
4388 unsigned int total_len;
4390 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
4392 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4395 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4400 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4401 req->FileInfoClass = level;
4402 req->PersistentFileId = persistent_fid;
4403 req->VolatileFileId = volatile_fid;
4405 req->InputBufferOffset =
4406 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
4407 req->OutputBufferLength = cpu_to_le32(
4408 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
4410 iov->iov_base = (char *)req;
4411 iov->iov_len = total_len;
4416 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4417 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4419 struct smb_rqst rqst;
4420 struct smb2_query_info_rsp *rsp = NULL;
4422 struct kvec rsp_iov;
4425 struct cifs_ses *ses = tcon->ses;
4426 FILE_SYSTEM_POSIX_INFO *info = NULL;
4429 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4430 sizeof(FILE_SYSTEM_POSIX_INFO),
4431 persistent_fid, volatile_fid);
4435 if (smb3_encryption_required(tcon))
4436 flags |= CIFS_TRANSFORM_REQ;
4438 memset(&rqst, 0, sizeof(struct smb_rqst));
4442 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4443 cifs_small_buf_release(iov.iov_base);
4445 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4446 goto posix_qfsinf_exit;
4448 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4450 info = (FILE_SYSTEM_POSIX_INFO *)(
4451 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4452 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4453 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4454 sizeof(FILE_SYSTEM_POSIX_INFO));
4456 copy_posix_fs_info_to_kstatfs(info, fsdata);
4459 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4464 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4465 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4467 struct smb_rqst rqst;
4468 struct smb2_query_info_rsp *rsp = NULL;
4470 struct kvec rsp_iov;
4473 struct cifs_ses *ses = tcon->ses;
4474 struct smb2_fs_full_size_info *info = NULL;
4477 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4478 sizeof(struct smb2_fs_full_size_info),
4479 persistent_fid, volatile_fid);
4483 if (smb3_encryption_required(tcon))
4484 flags |= CIFS_TRANSFORM_REQ;
4486 memset(&rqst, 0, sizeof(struct smb_rqst));
4490 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4491 cifs_small_buf_release(iov.iov_base);
4493 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4496 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4498 info = (struct smb2_fs_full_size_info *)(
4499 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4500 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4501 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4502 sizeof(struct smb2_fs_full_size_info));
4504 smb2_copy_fs_info_to_kstatfs(info, fsdata);
4507 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4512 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
4513 u64 persistent_fid, u64 volatile_fid, int level)
4515 struct smb_rqst rqst;
4516 struct smb2_query_info_rsp *rsp = NULL;
4518 struct kvec rsp_iov;
4520 int resp_buftype, max_len, min_len;
4521 struct cifs_ses *ses = tcon->ses;
4522 unsigned int rsp_len, offset;
4525 if (level == FS_DEVICE_INFORMATION) {
4526 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4527 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4528 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4529 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4530 min_len = MIN_FS_ATTR_INFO_SIZE;
4531 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4532 max_len = sizeof(struct smb3_fs_ss_info);
4533 min_len = sizeof(struct smb3_fs_ss_info);
4534 } else if (level == FS_VOLUME_INFORMATION) {
4535 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4536 min_len = sizeof(struct smb3_fs_vol_info);
4538 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
4542 rc = build_qfs_info_req(&iov, tcon, level, max_len,
4543 persistent_fid, volatile_fid);
4547 if (smb3_encryption_required(tcon))
4548 flags |= CIFS_TRANSFORM_REQ;
4550 memset(&rqst, 0, sizeof(struct smb_rqst));
4554 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4555 cifs_small_buf_release(iov.iov_base);
4557 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4560 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4562 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4563 offset = le16_to_cpu(rsp->OutputBufferOffset);
4564 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
4568 if (level == FS_ATTRIBUTE_INFORMATION)
4569 memcpy(&tcon->fsAttrInfo, offset
4570 + (char *)rsp, min_t(unsigned int,
4572 else if (level == FS_DEVICE_INFORMATION)
4573 memcpy(&tcon->fsDevInfo, offset
4574 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
4575 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4576 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
4577 (offset + (char *)rsp);
4578 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4579 tcon->perf_sector_size =
4580 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
4581 } else if (level == FS_VOLUME_INFORMATION) {
4582 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4583 (offset + (char *)rsp);
4584 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4585 tcon->vol_create_time = vol_info->VolumeCreationTime;
4589 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4594 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4595 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4596 const __u32 num_lock, struct smb2_lock_element *buf)
4598 struct smb_rqst rqst;
4600 struct smb2_lock_req *req = NULL;
4602 struct kvec rsp_iov;
4605 int flags = CIFS_NO_RSP_BUF;
4606 unsigned int total_len;
4608 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
4610 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
4614 if (smb3_encryption_required(tcon))
4615 flags |= CIFS_TRANSFORM_REQ;
4617 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4618 req->LockCount = cpu_to_le16(num_lock);
4620 req->PersistentFileId = persist_fid;
4621 req->VolatileFileId = volatile_fid;
4623 count = num_lock * sizeof(struct smb2_lock_element);
4625 iov[0].iov_base = (char *)req;
4626 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
4627 iov[1].iov_base = (char *)buf;
4628 iov[1].iov_len = count;
4630 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
4632 memset(&rqst, 0, sizeof(struct smb_rqst));
4636 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
4638 cifs_small_buf_release(req);
4640 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
4641 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
4642 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4643 tcon->ses->Suid, rc);
4650 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4651 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4652 const __u64 length, const __u64 offset, const __u32 lock_flags,
4655 struct smb2_lock_element lock;
4657 lock.Offset = cpu_to_le64(offset);
4658 lock.Length = cpu_to_le64(length);
4659 lock.Flags = cpu_to_le32(lock_flags);
4660 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
4661 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
4663 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
4667 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
4668 __u8 *lease_key, const __le32 lease_state)
4670 struct smb_rqst rqst;
4672 struct smb2_lease_ack *req = NULL;
4673 struct cifs_ses *ses = tcon->ses;
4674 int flags = CIFS_OBREAK_OP;
4675 unsigned int total_len;
4677 struct kvec rsp_iov;
4679 __u64 *please_key_high;
4680 __u64 *please_key_low;
4682 cifs_dbg(FYI, "SMB2_lease_break\n");
4683 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4688 if (smb3_encryption_required(tcon))
4689 flags |= CIFS_TRANSFORM_REQ;
4691 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4692 req->StructureSize = cpu_to_le16(36);
4695 memcpy(req->LeaseKey, lease_key, 16);
4696 req->LeaseState = lease_state;
4698 flags |= CIFS_NO_RSP_BUF;
4700 iov[0].iov_base = (char *)req;
4701 iov[0].iov_len = total_len;
4703 memset(&rqst, 0, sizeof(struct smb_rqst));
4707 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4708 cifs_small_buf_release(req);
4710 please_key_low = (__u64 *)lease_key;
4711 please_key_high = (__u64 *)(lease_key+8);
4713 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4714 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
4715 ses->Suid, *please_key_low, *please_key_high, rc);
4716 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
4718 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
4719 ses->Suid, *please_key_low, *please_key_high);