4 * Copyright (C) International Business Machines Corp., 2009, 2013
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
9 * Contains the routines for constructing the SMB2 PDUs themselves
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
32 #include <linux/kernel.h>
33 #include <linux/vfs.h>
34 #include <linux/task_io_accounting_ops.h>
35 #include <linux/uaccess.h>
36 #include <linux/uuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/xattr.h>
42 #include "cifsproto.h"
43 #include "smb2proto.h"
44 #include "cifs_unicode.h"
45 #include "cifs_debug.h"
47 #include "smb2status.h"
50 #include "cifs_spnego.h"
51 #include "smbdirect.h"
53 #ifdef CONFIG_CIFS_DFS_UPCALL
54 #include "dfs_cache.h"
58 * The following table defines the expected "StructureSize" of SMB2 requests
59 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
61 * Note that commands are defined in smb2pdu.h in le16 but the array below is
62 * indexed by command in host byte order.
64 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
65 /* SMB2_NEGOTIATE */ 36,
66 /* SMB2_SESSION_SETUP */ 25,
68 /* SMB2_TREE_CONNECT */ 9,
69 /* SMB2_TREE_DISCONNECT */ 4,
79 /* SMB2_QUERY_DIRECTORY */ 33,
80 /* SMB2_CHANGE_NOTIFY */ 32,
81 /* SMB2_QUERY_INFO */ 41,
82 /* SMB2_SET_INFO */ 33,
83 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
86 int smb3_encryption_required(const struct cifs_tcon *tcon)
90 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
91 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
94 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
100 smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
101 const struct cifs_tcon *tcon)
103 shdr->ProtocolId = SMB2_PROTO_NUMBER;
104 shdr->StructureSize = cpu_to_le16(64);
105 shdr->Command = smb2_cmd;
106 if (tcon && tcon->ses && tcon->ses->server) {
107 struct TCP_Server_Info *server = tcon->ses->server;
109 spin_lock(&server->req_lock);
110 /* Request up to 10 credits but don't go over the limit. */
111 if (server->credits >= server->max_credits)
112 shdr->CreditRequest = cpu_to_le16(0);
114 shdr->CreditRequest = cpu_to_le16(
115 min_t(int, server->max_credits -
116 server->credits, 10));
117 spin_unlock(&server->req_lock);
119 shdr->CreditRequest = cpu_to_le16(2);
121 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
126 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
127 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
128 if ((tcon->ses) && (tcon->ses->server) &&
129 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
130 shdr->CreditCharge = cpu_to_le16(1);
131 /* else CreditCharge MBZ */
133 shdr->TreeId = tcon->tid;
134 /* Uid is not converted */
136 shdr->SessionId = tcon->ses->Suid;
139 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
140 * to pass the path on the Open SMB prefixed by \\server\share.
141 * Not sure when we would need to do the augmented path (if ever) and
142 * setting this flag breaks the SMB2 open operation since it is
143 * illegal to send an empty path name (without \\server\share prefix)
144 * when the DFS flag is set in the SMB open header. We could
145 * consider setting the flag on all operations other than open
146 * but it is safer to net set it for now.
148 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
149 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
151 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
152 !smb3_encryption_required(tcon))
153 shdr->Flags |= SMB2_FLAGS_SIGNED;
158 #ifdef CONFIG_CIFS_DFS_UPCALL
159 static int __smb2_reconnect(const struct nls_table *nlsc,
160 struct cifs_tcon *tcon)
163 struct dfs_cache_tgt_list tl;
164 struct dfs_cache_tgt_iterator *it = NULL;
166 const char *tcp_host;
168 const char *dfs_host;
171 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
176 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
177 tcon->ses->server->hostname);
178 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
182 if (!tcon->dfs_path) {
183 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
187 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl);
191 extract_unc_hostname(tcon->ses->server->hostname, &tcp_host,
194 for (it = dfs_cache_get_tgt_iterator(&tl); it;
195 it = dfs_cache_get_next_tgt(&tl, it)) {
196 const char *tgt = dfs_cache_get_tgt_name(it);
198 extract_unc_hostname(tgt, &dfs_host, &dfs_host_len);
200 if (dfs_host_len != tcp_host_len
201 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
202 cifs_dbg(FYI, "%s: skipping %.*s, doesn't match %.*s",
204 (int)dfs_host_len, dfs_host,
205 (int)tcp_host_len, tcp_host);
209 scnprintf(tree, MAX_TREE_SIZE, "\\%s", tgt);
211 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
220 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1,
225 dfs_cache_free_tgts(&tl);
231 static inline int __smb2_reconnect(const struct nls_table *nlsc,
232 struct cifs_tcon *tcon)
234 return SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
239 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
242 struct nls_table *nls_codepage;
243 struct cifs_ses *ses;
244 struct TCP_Server_Info *server;
248 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
249 * check for tcp and smb session status done differently
250 * for those three - in the calling routine.
255 if (smb2_command == SMB2_TREE_CONNECT || smb2_command == SMB2_IOCTL)
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 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
507 /* context size is DataLength + minimal smb2_neg_context */
508 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
509 sizeof(struct smb2_neg_context), 8) * 8;
513 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
515 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
516 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
517 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
518 pneg_ctxt->Name[0] = 0x93;
519 pneg_ctxt->Name[1] = 0xAD;
520 pneg_ctxt->Name[2] = 0x25;
521 pneg_ctxt->Name[3] = 0x50;
522 pneg_ctxt->Name[4] = 0x9C;
523 pneg_ctxt->Name[5] = 0xB4;
524 pneg_ctxt->Name[6] = 0x11;
525 pneg_ctxt->Name[7] = 0xE7;
526 pneg_ctxt->Name[8] = 0xB4;
527 pneg_ctxt->Name[9] = 0x23;
528 pneg_ctxt->Name[10] = 0x83;
529 pneg_ctxt->Name[11] = 0xDE;
530 pneg_ctxt->Name[12] = 0x96;
531 pneg_ctxt->Name[13] = 0x8B;
532 pneg_ctxt->Name[14] = 0xCD;
533 pneg_ctxt->Name[15] = 0x7C;
537 assemble_neg_contexts(struct smb2_negotiate_req *req,
538 struct TCP_Server_Info *server, unsigned int *total_len)
540 char *pneg_ctxt = (char *)req;
541 unsigned int ctxt_len;
543 if (*total_len > 200) {
544 /* In case length corrupted don't want to overrun smb buffer */
545 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
550 * round up total_len of fixed part of SMB3 negotiate request to 8
551 * byte boundary before adding negotiate contexts
553 *total_len = roundup(*total_len, 8);
555 pneg_ctxt = (*total_len) + (char *)req;
556 req->NegotiateContextOffset = cpu_to_le32(*total_len);
558 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
559 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
560 *total_len += ctxt_len;
561 pneg_ctxt += ctxt_len;
563 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
564 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
565 *total_len += ctxt_len;
566 pneg_ctxt += ctxt_len;
568 if (server->compress_algorithm) {
569 build_compression_ctxt((struct smb2_compression_capabilities_context *)
571 ctxt_len = DIV_ROUND_UP(
572 sizeof(struct smb2_compression_capabilities_context),
574 *total_len += ctxt_len;
575 pneg_ctxt += ctxt_len;
576 req->NegotiateContextCount = cpu_to_le16(5);
578 req->NegotiateContextCount = cpu_to_le16(4);
580 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
582 *total_len += ctxt_len;
583 pneg_ctxt += ctxt_len;
585 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
586 *total_len += sizeof(struct smb2_posix_neg_context);
589 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
591 unsigned int len = le16_to_cpu(ctxt->DataLength);
593 /* If invalid preauth context warn but use what we requested, SHA-512 */
594 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
595 printk_once(KERN_WARNING "server sent bad preauth context\n");
598 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
599 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
600 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
601 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
604 static void decode_compress_ctx(struct TCP_Server_Info *server,
605 struct smb2_compression_capabilities_context *ctxt)
607 unsigned int len = le16_to_cpu(ctxt->DataLength);
609 /* sizeof compress context is a one element compression capbility struct */
611 printk_once(KERN_WARNING "server sent bad compression cntxt\n");
614 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
615 printk_once(KERN_WARNING "illegal SMB3 compress algorithm count\n");
618 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
619 printk_once(KERN_WARNING "unknown compression algorithm\n");
622 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
625 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
626 struct smb2_encryption_neg_context *ctxt)
628 unsigned int len = le16_to_cpu(ctxt->DataLength);
630 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
631 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
632 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
636 if (le16_to_cpu(ctxt->CipherCount) != 1) {
637 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
640 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
641 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
642 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
643 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
646 server->cipher_type = ctxt->Ciphers[0];
647 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
651 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
652 struct TCP_Server_Info *server,
653 unsigned int len_of_smb)
655 struct smb2_neg_context *pctx;
656 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
657 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
658 unsigned int len_of_ctxts, i;
661 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
662 if (len_of_smb <= offset) {
663 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
667 len_of_ctxts = len_of_smb - offset;
669 for (i = 0; i < ctxt_cnt; i++) {
671 /* check that offset is not beyond end of SMB */
672 if (len_of_ctxts == 0)
675 if (len_of_ctxts < sizeof(struct smb2_neg_context))
678 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
679 clen = le16_to_cpu(pctx->DataLength);
680 if (clen > len_of_ctxts)
683 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
684 decode_preauth_context(
685 (struct smb2_preauth_neg_context *)pctx);
686 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
687 rc = decode_encrypt_ctx(server,
688 (struct smb2_encryption_neg_context *)pctx);
689 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
690 decode_compress_ctx(server,
691 (struct smb2_compression_capabilities_context *)pctx);
692 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
693 server->posix_ext_supported = true;
695 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
696 le16_to_cpu(pctx->ContextType));
700 /* offsets must be 8 byte aligned */
701 clen = (clen + 7) & ~0x7;
702 offset += clen + sizeof(struct smb2_neg_context);
703 len_of_ctxts -= clen;
708 static struct create_posix *
709 create_posix_buf(umode_t mode)
711 struct create_posix *buf;
713 buf = kzalloc(sizeof(struct create_posix),
718 buf->ccontext.DataOffset =
719 cpu_to_le16(offsetof(struct create_posix, Mode));
720 buf->ccontext.DataLength = cpu_to_le32(4);
721 buf->ccontext.NameOffset =
722 cpu_to_le16(offsetof(struct create_posix, Name));
723 buf->ccontext.NameLength = cpu_to_le16(16);
725 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
736 buf->Name[10] = 0x83;
737 buf->Name[11] = 0xDE;
738 buf->Name[12] = 0x96;
739 buf->Name[13] = 0x8B;
740 buf->Name[14] = 0xCD;
741 buf->Name[15] = 0x7C;
742 buf->Mode = cpu_to_le32(mode);
743 cifs_dbg(FYI, "mode on posix create 0%o", mode);
748 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
750 struct smb2_create_req *req = iov[0].iov_base;
751 unsigned int num = *num_iovec;
753 iov[num].iov_base = create_posix_buf(mode);
755 cifs_dbg(VFS, "illegal mode\n"); /* BB REMOVEME */
756 if (iov[num].iov_base == NULL)
758 iov[num].iov_len = sizeof(struct create_posix);
759 if (!req->CreateContextsOffset)
760 req->CreateContextsOffset = cpu_to_le32(
761 sizeof(struct smb2_create_req) +
762 iov[num - 1].iov_len);
763 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
764 *num_iovec = num + 1;
771 * SMB2 Worker functions follow:
773 * The general structure of the worker functions is:
774 * 1) Call smb2_init (assembles SMB2 header)
775 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
776 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
777 * 4) Decode SMB2 command specific fields in the fixed length area
778 * 5) Decode variable length data area (if any for this SMB2 command type)
779 * 6) Call free smb buffer
785 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
787 struct smb_rqst rqst;
788 struct smb2_negotiate_req *req;
789 struct smb2_negotiate_rsp *rsp;
794 struct TCP_Server_Info *server = ses->server;
795 int blob_offset, blob_length;
797 int flags = CIFS_NEG_OP;
798 unsigned int total_len;
800 cifs_dbg(FYI, "Negotiate protocol\n");
803 WARN(1, "%s: server is NULL!\n", __func__);
807 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
811 req->sync_hdr.SessionId = 0;
813 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
814 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
816 if (strcmp(ses->server->vals->version_string,
817 SMB3ANY_VERSION_STRING) == 0) {
818 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
819 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
820 req->DialectCount = cpu_to_le16(2);
822 } else if (strcmp(server->vals->version_string,
823 SMBDEFAULT_VERSION_STRING) == 0) {
824 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
825 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
826 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
827 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
828 req->DialectCount = cpu_to_le16(4);
831 /* otherwise send specific dialect */
832 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
833 req->DialectCount = cpu_to_le16(1);
837 /* only one of SMB2 signing flags may be set in SMB2 request */
839 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
840 else if (global_secflags & CIFSSEC_MAY_SIGN)
841 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
843 req->SecurityMode = 0;
845 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
847 /* ClientGUID must be zero for SMB2.02 dialect */
848 if (server->vals->protocol_id == SMB20_PROT_ID)
849 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
851 memcpy(req->ClientGUID, server->client_guid,
852 SMB2_CLIENT_GUID_SIZE);
853 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
854 (strcmp(server->vals->version_string,
855 SMBDEFAULT_VERSION_STRING) == 0))
856 assemble_neg_contexts(req, server, &total_len);
858 iov[0].iov_base = (char *)req;
859 iov[0].iov_len = total_len;
861 memset(&rqst, 0, sizeof(struct smb_rqst));
865 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
866 cifs_small_buf_release(req);
867 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
869 * No tcon so can't do
870 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
872 if (rc == -EOPNOTSUPP) {
873 cifs_server_dbg(VFS, "Dialect not supported by server. Consider "
874 "specifying vers=1.0 or vers=2.0 on mount for accessing"
880 if (strcmp(server->vals->version_string,
881 SMB3ANY_VERSION_STRING) == 0) {
882 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
884 "SMB2 dialect returned but not requested\n");
886 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
888 "SMB2.1 dialect returned but not requested\n");
891 } else if (strcmp(server->vals->version_string,
892 SMBDEFAULT_VERSION_STRING) == 0) {
893 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
895 "SMB2 dialect returned but not requested\n");
897 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
898 /* ops set to 3.0 by default for default so update */
899 server->ops = &smb21_operations;
900 server->vals = &smb21_values;
901 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
902 server->ops = &smb311_operations;
903 server->vals = &smb311_values;
905 } else if (le16_to_cpu(rsp->DialectRevision) !=
906 server->vals->protocol_id) {
907 /* if requested single dialect ensure returned dialect matched */
908 cifs_server_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
909 le16_to_cpu(rsp->DialectRevision));
913 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
915 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
916 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
917 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
918 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
919 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
920 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
921 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
922 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
923 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
924 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
926 cifs_server_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
927 le16_to_cpu(rsp->DialectRevision));
931 server->dialect = le16_to_cpu(rsp->DialectRevision);
934 * Keep a copy of the hash after negprot. This hash will be
935 * the starting hash value for all sessions made from this
938 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
939 SMB2_PREAUTH_HASH_SIZE);
941 /* SMB2 only has an extended negflavor */
942 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
943 /* set it to the maximum buffer size value we can send with 1 credit */
944 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
945 SMB2_MAX_BUFFER_SIZE);
946 server->max_read = le32_to_cpu(rsp->MaxReadSize);
947 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
948 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
949 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
950 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
952 server->capabilities = le32_to_cpu(rsp->Capabilities);
954 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
956 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
957 (struct smb2_sync_hdr *)rsp);
959 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
961 * ses->sectype = RawNTLMSSP;
962 * but for time being this is our only auth choice so doesn't matter.
963 * We just found a server which sets blob length to zero expecting raw.
965 if (blob_length == 0) {
966 cifs_dbg(FYI, "missing security blob on negprot\n");
967 server->sec_ntlmssp = true;
970 rc = cifs_enable_signing(server, ses->sign);
974 rc = decode_negTokenInit(security_blob, blob_length, server);
981 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
982 if (rsp->NegotiateContextCount)
983 rc = smb311_decode_neg_context(rsp, server,
986 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
989 free_rsp_buf(resp_buftype, rsp);
993 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
996 struct validate_negotiate_info_req *pneg_inbuf;
997 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
999 u32 inbuflen; /* max of 4 dialects */
1000 struct TCP_Server_Info *server = tcon->ses->server;
1002 cifs_dbg(FYI, "validate negotiate\n");
1004 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1005 if (server->dialect == SMB311_PROT_ID)
1009 * validation ioctl must be signed, so no point sending this if we
1010 * can not sign it (ie are not known user). Even if signing is not
1011 * required (enabled but not negotiated), in those cases we selectively
1012 * sign just this, the first and only signed request on a connection.
1013 * Having validation of negotiate info helps reduce attack vectors.
1015 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1016 return 0; /* validation requires signing */
1018 if (tcon->ses->user_name == NULL) {
1019 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1020 return 0; /* validation requires signing */
1023 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1024 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1026 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1030 pneg_inbuf->Capabilities =
1031 cpu_to_le32(server->vals->req_capabilities);
1032 memcpy(pneg_inbuf->Guid, server->client_guid,
1033 SMB2_CLIENT_GUID_SIZE);
1035 if (tcon->ses->sign)
1036 pneg_inbuf->SecurityMode =
1037 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1038 else if (global_secflags & CIFSSEC_MAY_SIGN)
1039 pneg_inbuf->SecurityMode =
1040 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1042 pneg_inbuf->SecurityMode = 0;
1045 if (strcmp(server->vals->version_string,
1046 SMB3ANY_VERSION_STRING) == 0) {
1047 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1048 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1049 pneg_inbuf->DialectCount = cpu_to_le16(2);
1050 /* structure is big enough for 3 dialects, sending only 2 */
1051 inbuflen = sizeof(*pneg_inbuf) -
1052 (2 * sizeof(pneg_inbuf->Dialects[0]));
1053 } else if (strcmp(server->vals->version_string,
1054 SMBDEFAULT_VERSION_STRING) == 0) {
1055 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1056 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1057 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1058 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1059 pneg_inbuf->DialectCount = cpu_to_le16(4);
1060 /* structure is big enough for 3 dialects */
1061 inbuflen = sizeof(*pneg_inbuf);
1063 /* otherwise specific dialect was requested */
1064 pneg_inbuf->Dialects[0] =
1065 cpu_to_le16(server->vals->protocol_id);
1066 pneg_inbuf->DialectCount = cpu_to_le16(1);
1067 /* structure is big enough for 3 dialects, sending only 1 */
1068 inbuflen = sizeof(*pneg_inbuf) -
1069 sizeof(pneg_inbuf->Dialects[0]) * 2;
1072 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1073 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
1074 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1075 (char **)&pneg_rsp, &rsplen);
1076 if (rc == -EOPNOTSUPP) {
1078 * Old Windows versions or Netapp SMB server can return
1079 * not supported error. Client should accept it.
1081 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1083 goto out_free_inbuf;
1084 } else if (rc != 0) {
1085 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
1087 goto out_free_inbuf;
1091 if (rsplen != sizeof(*pneg_rsp)) {
1092 cifs_tcon_dbg(VFS, "invalid protocol negotiate response size: %d\n",
1095 /* relax check since Mac returns max bufsize allowed on ioctl */
1096 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1100 /* check validate negotiate info response matches what we got earlier */
1101 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1104 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1107 /* do not validate server guid because not saved at negprot time yet */
1109 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1110 SMB2_LARGE_FILES) != server->capabilities)
1113 /* validate negotiate successful */
1115 cifs_dbg(FYI, "validate negotiate info successful\n");
1119 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1128 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1130 switch (requested) {
1137 if (server->sec_ntlmssp &&
1138 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1140 if ((server->sec_kerberos || server->sec_mskerberos) &&
1141 (global_secflags & CIFSSEC_MAY_KRB5))
1149 struct SMB2_sess_data {
1151 struct cifs_ses *ses;
1152 struct nls_table *nls_cp;
1153 void (*func)(struct SMB2_sess_data *);
1155 u64 previous_session;
1157 /* we will send the SMB in three pieces:
1158 * a fixed length beginning part, an optional
1159 * SPNEGO blob (which can be zero length), and a
1160 * last part which will include the strings
1161 * and rest of bcc area. This allows us to avoid
1162 * a large buffer 17K allocation
1169 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1172 struct cifs_ses *ses = sess_data->ses;
1173 struct smb2_sess_setup_req *req;
1174 struct TCP_Server_Info *server = ses->server;
1175 unsigned int total_len;
1177 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1182 /* First session, not a reauthenticate */
1183 req->sync_hdr.SessionId = 0;
1185 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
1186 req->PreviousSessionId = sess_data->previous_session;
1188 req->Flags = 0; /* MBZ */
1190 /* enough to enable echos and oplocks and one max size write */
1191 req->sync_hdr.CreditRequest = cpu_to_le16(130);
1193 /* only one of SMB2 signing flags may be set in SMB2 request */
1195 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1196 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1197 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1199 req->SecurityMode = 0;
1201 #ifdef CONFIG_CIFS_DFS_UPCALL
1202 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1204 req->Capabilities = 0;
1205 #endif /* DFS_UPCALL */
1207 req->Channel = 0; /* MBZ */
1209 sess_data->iov[0].iov_base = (char *)req;
1211 sess_data->iov[0].iov_len = total_len - 1;
1213 * This variable will be used to clear the buffer
1214 * allocated above in case of any error in the calling function.
1216 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1222 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1224 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1225 sess_data->buf0_type = CIFS_NO_BUFFER;
1229 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1232 struct smb_rqst rqst;
1233 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1234 struct kvec rsp_iov = { NULL, 0 };
1236 /* Testing shows that buffer offset must be at location of Buffer[0] */
1237 req->SecurityBufferOffset =
1238 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1239 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1241 memset(&rqst, 0, sizeof(struct smb_rqst));
1242 rqst.rq_iov = sess_data->iov;
1245 /* BB add code to build os and lm fields */
1246 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1248 &sess_data->buf0_type,
1249 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
1250 cifs_small_buf_release(sess_data->iov[0].iov_base);
1251 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1257 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1260 struct cifs_ses *ses = sess_data->ses;
1262 mutex_lock(&ses->server->srv_mutex);
1263 if (ses->server->ops->generate_signingkey) {
1264 rc = ses->server->ops->generate_signingkey(ses);
1267 "SMB3 session key generation failed\n");
1268 mutex_unlock(&ses->server->srv_mutex);
1272 if (!ses->server->session_estab) {
1273 ses->server->sequence_number = 0x2;
1274 ses->server->session_estab = true;
1276 mutex_unlock(&ses->server->srv_mutex);
1278 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1279 spin_lock(&GlobalMid_Lock);
1280 ses->status = CifsGood;
1281 ses->need_reconnect = false;
1282 spin_unlock(&GlobalMid_Lock);
1286 #ifdef CONFIG_CIFS_UPCALL
1288 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1291 struct cifs_ses *ses = sess_data->ses;
1292 struct cifs_spnego_msg *msg;
1293 struct key *spnego_key = NULL;
1294 struct smb2_sess_setup_rsp *rsp = NULL;
1296 rc = SMB2_sess_alloc_buffer(sess_data);
1300 spnego_key = cifs_get_spnego_key(ses);
1301 if (IS_ERR(spnego_key)) {
1302 rc = PTR_ERR(spnego_key);
1307 msg = spnego_key->payload.data[0];
1309 * check version field to make sure that cifs.upcall is
1310 * sending us a response in an expected form
1312 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1314 "bad cifs.upcall version. Expected %d got %d",
1315 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1317 goto out_put_spnego_key;
1320 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1322 if (!ses->auth_key.response) {
1324 "Kerberos can't allocate (%u bytes) memory",
1327 goto out_put_spnego_key;
1329 ses->auth_key.len = msg->sesskey_len;
1331 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1332 sess_data->iov[1].iov_len = msg->secblob_len;
1334 rc = SMB2_sess_sendreceive(sess_data);
1336 goto out_put_spnego_key;
1338 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1339 ses->Suid = rsp->sync_hdr.SessionId;
1341 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1343 rc = SMB2_sess_establish_session(sess_data);
1345 key_invalidate(spnego_key);
1346 key_put(spnego_key);
1348 sess_data->result = rc;
1349 sess_data->func = NULL;
1350 SMB2_sess_free_buffer(sess_data);
1354 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1356 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1357 sess_data->result = -EOPNOTSUPP;
1358 sess_data->func = NULL;
1363 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1366 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1369 struct cifs_ses *ses = sess_data->ses;
1370 struct smb2_sess_setup_rsp *rsp = NULL;
1371 char *ntlmssp_blob = NULL;
1372 bool use_spnego = false; /* else use raw ntlmssp */
1373 u16 blob_length = 0;
1376 * If memory allocation is successful, caller of this function
1379 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1380 if (!ses->ntlmssp) {
1384 ses->ntlmssp->sesskey_per_smbsess = true;
1386 rc = SMB2_sess_alloc_buffer(sess_data);
1390 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1392 if (ntlmssp_blob == NULL) {
1397 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1399 /* BB eventually need to add this */
1400 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1404 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1405 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1407 sess_data->iov[1].iov_base = ntlmssp_blob;
1408 sess_data->iov[1].iov_len = blob_length;
1410 rc = SMB2_sess_sendreceive(sess_data);
1411 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1413 /* If true, rc here is expected and not an error */
1414 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1415 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1421 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1422 le16_to_cpu(rsp->SecurityBufferOffset)) {
1423 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1424 le16_to_cpu(rsp->SecurityBufferOffset));
1428 rc = decode_ntlmssp_challenge(rsp->Buffer,
1429 le16_to_cpu(rsp->SecurityBufferLength), ses);
1433 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1436 ses->Suid = rsp->sync_hdr.SessionId;
1437 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1440 kfree(ntlmssp_blob);
1441 SMB2_sess_free_buffer(sess_data);
1443 sess_data->result = 0;
1444 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1448 kfree(ses->ntlmssp);
1449 ses->ntlmssp = NULL;
1450 sess_data->result = rc;
1451 sess_data->func = NULL;
1455 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1458 struct cifs_ses *ses = sess_data->ses;
1459 struct smb2_sess_setup_req *req;
1460 struct smb2_sess_setup_rsp *rsp = NULL;
1461 unsigned char *ntlmssp_blob = NULL;
1462 bool use_spnego = false; /* else use raw ntlmssp */
1463 u16 blob_length = 0;
1465 rc = SMB2_sess_alloc_buffer(sess_data);
1469 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1470 req->sync_hdr.SessionId = ses->Suid;
1472 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1475 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1480 /* BB eventually need to add this */
1481 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1485 sess_data->iov[1].iov_base = ntlmssp_blob;
1486 sess_data->iov[1].iov_len = blob_length;
1488 rc = SMB2_sess_sendreceive(sess_data);
1492 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1494 ses->Suid = rsp->sync_hdr.SessionId;
1495 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1497 rc = SMB2_sess_establish_session(sess_data);
1499 kfree(ntlmssp_blob);
1500 SMB2_sess_free_buffer(sess_data);
1501 kfree(ses->ntlmssp);
1502 ses->ntlmssp = NULL;
1503 sess_data->result = rc;
1504 sess_data->func = NULL;
1508 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1512 type = smb2_select_sectype(ses->server, ses->sectype);
1513 cifs_dbg(FYI, "sess setup type %d\n", type);
1514 if (type == Unspecified) {
1516 "Unable to select appropriate authentication method!");
1522 sess_data->func = SMB2_auth_kerberos;
1525 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1528 cifs_dbg(VFS, "secType %d not supported!\n", type);
1536 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1537 const struct nls_table *nls_cp)
1540 struct TCP_Server_Info *server = ses->server;
1541 struct SMB2_sess_data *sess_data;
1543 cifs_dbg(FYI, "Session Setup\n");
1546 WARN(1, "%s: server is NULL!\n", __func__);
1550 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1554 rc = SMB2_select_sec(ses, sess_data);
1557 sess_data->xid = xid;
1558 sess_data->ses = ses;
1559 sess_data->buf0_type = CIFS_NO_BUFFER;
1560 sess_data->nls_cp = (struct nls_table *) nls_cp;
1561 sess_data->previous_session = ses->Suid;
1564 * Initialize the session hash with the server one.
1566 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1567 SMB2_PREAUTH_HASH_SIZE);
1569 while (sess_data->func)
1570 sess_data->func(sess_data);
1572 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1573 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1574 rc = sess_data->result;
1581 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1583 struct smb_rqst rqst;
1584 struct smb2_logoff_req *req; /* response is also trivial struct */
1586 struct TCP_Server_Info *server;
1588 unsigned int total_len;
1590 struct kvec rsp_iov;
1593 cifs_dbg(FYI, "disconnect session %p\n", ses);
1595 if (ses && (ses->server))
1596 server = ses->server;
1600 /* no need to send SMB logoff if uid already closed due to reconnect */
1601 if (ses->need_reconnect)
1602 goto smb2_session_already_dead;
1604 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
1608 /* since no tcon, smb2_init can not do this, so do here */
1609 req->sync_hdr.SessionId = ses->Suid;
1611 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1612 flags |= CIFS_TRANSFORM_REQ;
1613 else if (server->sign)
1614 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1616 flags |= CIFS_NO_RSP_BUF;
1618 iov[0].iov_base = (char *)req;
1619 iov[0].iov_len = total_len;
1621 memset(&rqst, 0, sizeof(struct smb_rqst));
1625 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1626 cifs_small_buf_release(req);
1628 * No tcon so can't do
1629 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1632 smb2_session_already_dead:
1636 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1638 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1641 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1643 /* These are similar values to what Windows uses */
1644 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1646 tcon->max_chunks = 256;
1647 tcon->max_bytes_chunk = 1048576;
1648 tcon->max_bytes_copy = 16777216;
1652 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1653 struct cifs_tcon *tcon, const struct nls_table *cp)
1655 struct smb_rqst rqst;
1656 struct smb2_tree_connect_req *req;
1657 struct smb2_tree_connect_rsp *rsp = NULL;
1659 struct kvec rsp_iov = { NULL, 0 };
1663 __le16 *unc_path = NULL;
1665 unsigned int total_len;
1666 struct TCP_Server_Info *server = ses->server;
1668 cifs_dbg(FYI, "TCON\n");
1670 if (!server || !tree)
1673 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1674 if (unc_path == NULL)
1677 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1679 if (unc_path_len < 2) {
1684 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1686 atomic_set(&tcon->num_remote_opens, 0);
1687 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1694 if (smb3_encryption_required(tcon))
1695 flags |= CIFS_TRANSFORM_REQ;
1697 iov[0].iov_base = (char *)req;
1699 iov[0].iov_len = total_len - 1;
1701 /* Testing shows that buffer offset must be at location of Buffer[0] */
1702 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1704 req->PathLength = cpu_to_le16(unc_path_len - 2);
1705 iov[1].iov_base = unc_path;
1706 iov[1].iov_len = unc_path_len;
1709 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1710 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1711 * (Samba servers don't always set the flag so also check if null user)
1713 if ((server->dialect == SMB311_PROT_ID) &&
1714 !smb3_encryption_required(tcon) &&
1715 !(ses->session_flags &
1716 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1717 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1718 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1720 memset(&rqst, 0, sizeof(struct smb_rqst));
1724 /* Need 64 for max size write so ask for more in case not there yet */
1725 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1727 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
1728 cifs_small_buf_release(req);
1729 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1730 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1733 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1734 tcon->need_reconnect = true;
1736 goto tcon_error_exit;
1739 switch (rsp->ShareType) {
1740 case SMB2_SHARE_TYPE_DISK:
1741 cifs_dbg(FYI, "connection to disk share\n");
1743 case SMB2_SHARE_TYPE_PIPE:
1745 cifs_dbg(FYI, "connection to pipe share\n");
1747 case SMB2_SHARE_TYPE_PRINT:
1749 cifs_dbg(FYI, "connection to printer\n");
1752 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1754 goto tcon_error_exit;
1757 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1758 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1759 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1760 tcon->tidStatus = CifsGood;
1761 tcon->need_reconnect = false;
1762 tcon->tid = rsp->sync_hdr.TreeId;
1763 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1765 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1766 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1767 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
1770 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1771 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
1773 init_copy_chunk_defaults(tcon);
1774 if (server->ops->validate_negotiate)
1775 rc = server->ops->validate_negotiate(xid, tcon);
1778 free_rsp_buf(resp_buftype, rsp);
1783 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1784 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1790 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1792 struct smb_rqst rqst;
1793 struct smb2_tree_disconnect_req *req; /* response is trivial */
1795 struct cifs_ses *ses = tcon->ses;
1797 unsigned int total_len;
1799 struct kvec rsp_iov;
1802 cifs_dbg(FYI, "Tree Disconnect\n");
1804 if (!ses || !(ses->server))
1807 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1810 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1815 if (smb3_encryption_required(tcon))
1816 flags |= CIFS_TRANSFORM_REQ;
1818 flags |= CIFS_NO_RSP_BUF;
1820 iov[0].iov_base = (char *)req;
1821 iov[0].iov_len = total_len;
1823 memset(&rqst, 0, sizeof(struct smb_rqst));
1827 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1828 cifs_small_buf_release(req);
1830 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1836 static struct create_durable *
1837 create_durable_buf(void)
1839 struct create_durable *buf;
1841 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1845 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1846 (struct create_durable, Data));
1847 buf->ccontext.DataLength = cpu_to_le32(16);
1848 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1849 (struct create_durable, Name));
1850 buf->ccontext.NameLength = cpu_to_le16(4);
1851 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1859 static struct create_durable *
1860 create_reconnect_durable_buf(struct cifs_fid *fid)
1862 struct create_durable *buf;
1864 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1868 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1869 (struct create_durable, Data));
1870 buf->ccontext.DataLength = cpu_to_le32(16);
1871 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1872 (struct create_durable, Name));
1873 buf->ccontext.NameLength = cpu_to_le16(4);
1874 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1875 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1876 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1885 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1887 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1889 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1890 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1891 buf->IndexNumber = pdisk_id->DiskFileId;
1895 smb2_parse_contexts(struct TCP_Server_Info *server,
1896 struct smb2_create_rsp *rsp,
1897 unsigned int *epoch, char *lease_key, __u8 *oplock,
1898 struct smb2_file_all_info *buf)
1901 struct create_context *cc;
1903 unsigned int remaining;
1907 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
1908 remaining = le32_to_cpu(rsp->CreateContextsLength);
1909 cc = (struct create_context *)data_offset;
1911 /* Initialize inode number to 0 in case no valid data in qfid context */
1913 buf->IndexNumber = 0;
1915 while (remaining >= sizeof(struct create_context)) {
1916 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1917 if (le16_to_cpu(cc->NameLength) == 4 &&
1918 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
1919 *oplock = server->ops->parse_lease_buf(cc, epoch,
1921 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
1922 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
1923 parse_query_id_ctxt(cc, buf);
1925 next = le32_to_cpu(cc->Next);
1929 cc = (struct create_context *)((char *)cc + next);
1932 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
1933 *oplock = rsp->OplockLevel;
1939 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1940 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
1942 struct smb2_create_req *req = iov[0].iov_base;
1943 unsigned int num = *num_iovec;
1945 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
1946 if (iov[num].iov_base == NULL)
1948 iov[num].iov_len = server->vals->create_lease_size;
1949 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1950 if (!req->CreateContextsOffset)
1951 req->CreateContextsOffset = cpu_to_le32(
1952 sizeof(struct smb2_create_req) +
1953 iov[num - 1].iov_len);
1954 le32_add_cpu(&req->CreateContextsLength,
1955 server->vals->create_lease_size);
1956 *num_iovec = num + 1;
1960 static struct create_durable_v2 *
1961 create_durable_v2_buf(struct cifs_open_parms *oparms)
1963 struct cifs_fid *pfid = oparms->fid;
1964 struct create_durable_v2 *buf;
1966 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1970 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1971 (struct create_durable_v2, dcontext));
1972 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1973 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1974 (struct create_durable_v2, Name));
1975 buf->ccontext.NameLength = cpu_to_le16(4);
1978 * NB: Handle timeout defaults to 0, which allows server to choose
1979 * (most servers default to 120 seconds) and most clients default to 0.
1980 * This can be overridden at mount ("handletimeout=") if the user wants
1981 * a different persistent (or resilient) handle timeout for all opens
1982 * opens on a particular SMB3 mount.
1984 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
1985 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1986 generate_random_uuid(buf->dcontext.CreateGuid);
1987 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1989 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1997 static struct create_durable_handle_reconnect_v2 *
1998 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2000 struct create_durable_handle_reconnect_v2 *buf;
2002 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2007 buf->ccontext.DataOffset =
2008 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2010 buf->ccontext.DataLength =
2011 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2012 buf->ccontext.NameOffset =
2013 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2015 buf->ccontext.NameLength = cpu_to_le16(4);
2017 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2018 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2019 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2020 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2022 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2031 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2032 struct cifs_open_parms *oparms)
2034 struct smb2_create_req *req = iov[0].iov_base;
2035 unsigned int num = *num_iovec;
2037 iov[num].iov_base = create_durable_v2_buf(oparms);
2038 if (iov[num].iov_base == NULL)
2040 iov[num].iov_len = sizeof(struct create_durable_v2);
2041 if (!req->CreateContextsOffset)
2042 req->CreateContextsOffset =
2043 cpu_to_le32(sizeof(struct smb2_create_req) +
2045 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
2046 *num_iovec = num + 1;
2051 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2052 struct cifs_open_parms *oparms)
2054 struct smb2_create_req *req = iov[0].iov_base;
2055 unsigned int num = *num_iovec;
2057 /* indicate that we don't need to relock the file */
2058 oparms->reconnect = false;
2060 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2061 if (iov[num].iov_base == NULL)
2063 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2064 if (!req->CreateContextsOffset)
2065 req->CreateContextsOffset =
2066 cpu_to_le32(sizeof(struct smb2_create_req) +
2068 le32_add_cpu(&req->CreateContextsLength,
2069 sizeof(struct create_durable_handle_reconnect_v2));
2070 *num_iovec = num + 1;
2075 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2076 struct cifs_open_parms *oparms, bool use_persistent)
2078 struct smb2_create_req *req = iov[0].iov_base;
2079 unsigned int num = *num_iovec;
2081 if (use_persistent) {
2082 if (oparms->reconnect)
2083 return add_durable_reconnect_v2_context(iov, num_iovec,
2086 return add_durable_v2_context(iov, num_iovec, oparms);
2089 if (oparms->reconnect) {
2090 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2091 /* indicate that we don't need to relock the file */
2092 oparms->reconnect = false;
2094 iov[num].iov_base = create_durable_buf();
2095 if (iov[num].iov_base == NULL)
2097 iov[num].iov_len = sizeof(struct create_durable);
2098 if (!req->CreateContextsOffset)
2099 req->CreateContextsOffset =
2100 cpu_to_le32(sizeof(struct smb2_create_req) +
2102 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
2103 *num_iovec = num + 1;
2107 /* See MS-SMB2 2.2.13.2.7 */
2108 static struct crt_twarp_ctxt *
2109 create_twarp_buf(__u64 timewarp)
2111 struct crt_twarp_ctxt *buf;
2113 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2117 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2118 (struct crt_twarp_ctxt, Timestamp));
2119 buf->ccontext.DataLength = cpu_to_le32(8);
2120 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2121 (struct crt_twarp_ctxt, Name));
2122 buf->ccontext.NameLength = cpu_to_le16(4);
2123 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2128 buf->Timestamp = cpu_to_le64(timewarp);
2132 /* See MS-SMB2 2.2.13.2.7 */
2134 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2136 struct smb2_create_req *req = iov[0].iov_base;
2137 unsigned int num = *num_iovec;
2139 iov[num].iov_base = create_twarp_buf(timewarp);
2140 if (iov[num].iov_base == NULL)
2142 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2143 if (!req->CreateContextsOffset)
2144 req->CreateContextsOffset = cpu_to_le32(
2145 sizeof(struct smb2_create_req) +
2146 iov[num - 1].iov_len);
2147 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2148 *num_iovec = num + 1;
2152 static struct crt_query_id_ctxt *
2153 create_query_id_buf(void)
2155 struct crt_query_id_ctxt *buf;
2157 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2161 buf->ccontext.DataOffset = cpu_to_le16(0);
2162 buf->ccontext.DataLength = cpu_to_le32(0);
2163 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2164 (struct crt_query_id_ctxt, Name));
2165 buf->ccontext.NameLength = cpu_to_le16(4);
2166 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2174 /* See MS-SMB2 2.2.13.2.9 */
2176 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2178 struct smb2_create_req *req = iov[0].iov_base;
2179 unsigned int num = *num_iovec;
2181 iov[num].iov_base = create_query_id_buf();
2182 if (iov[num].iov_base == NULL)
2184 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2185 if (!req->CreateContextsOffset)
2186 req->CreateContextsOffset = cpu_to_le32(
2187 sizeof(struct smb2_create_req) +
2188 iov[num - 1].iov_len);
2189 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2190 *num_iovec = num + 1;
2195 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2196 const char *treename, const __le16 *path)
2198 int treename_len, path_len;
2199 struct nls_table *cp;
2200 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2205 treename_len = strlen(treename);
2206 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2212 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2215 * make room for one path separator between the treename and
2218 *out_len = treename_len + 1 + path_len;
2221 * final path needs to be null-terminated UTF16 with a
2225 *out_size = roundup((*out_len+1)*2, 8);
2226 *out_path = kzalloc(*out_size, GFP_KERNEL);
2230 cp = load_nls_default();
2231 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2232 UniStrcat(*out_path, sep);
2233 UniStrcat(*out_path, path);
2239 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2240 umode_t mode, struct cifs_tcon *tcon,
2241 const char *full_path,
2242 struct cifs_sb_info *cifs_sb)
2244 struct smb_rqst rqst;
2245 struct smb2_create_req *req;
2246 struct smb2_create_rsp *rsp = NULL;
2247 struct cifs_ses *ses = tcon->ses;
2248 struct kvec iov[3]; /* make sure at least one for each open context */
2249 struct kvec rsp_iov = {NULL, 0};
2252 __le16 *copy_path = NULL;
2255 unsigned int n_iov = 2;
2256 __u32 file_attributes = 0;
2257 char *pc_buf = NULL;
2259 unsigned int total_len;
2260 __le16 *utf16_path = NULL;
2262 cifs_dbg(FYI, "mkdir\n");
2264 /* resource #1: path allocation */
2265 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2269 if (!ses || !(ses->server)) {
2274 /* resource #2: request */
2275 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2280 if (smb3_encryption_required(tcon))
2281 flags |= CIFS_TRANSFORM_REQ;
2283 req->ImpersonationLevel = IL_IMPERSONATION;
2284 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2285 /* File attributes ignored on open (used in create though) */
2286 req->FileAttributes = cpu_to_le32(file_attributes);
2287 req->ShareAccess = FILE_SHARE_ALL_LE;
2288 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2289 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2291 iov[0].iov_base = (char *)req;
2292 /* -1 since last byte is buf[0] which is sent below (path) */
2293 iov[0].iov_len = total_len - 1;
2295 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2297 /* [MS-SMB2] 2.2.13 NameOffset:
2298 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2299 * the SMB2 header, the file name includes a prefix that will
2300 * be processed during DFS name normalization as specified in
2301 * section 3.3.5.9. Otherwise, the file name is relative to
2302 * the share that is identified by the TreeId in the SMB2
2305 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2308 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2309 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2311 tcon->treeName, utf16_path);
2315 req->NameLength = cpu_to_le16(name_len * 2);
2316 uni_path_len = copy_size;
2317 /* free before overwriting resource */
2319 utf16_path = copy_path;
2321 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2322 /* MUST set path len (NameLength) to 0 opening root of share */
2323 req->NameLength = cpu_to_le16(uni_path_len - 2);
2324 if (uni_path_len % 8 != 0) {
2325 copy_size = roundup(uni_path_len, 8);
2326 copy_path = kzalloc(copy_size, GFP_KERNEL);
2331 memcpy((char *)copy_path, (const char *)utf16_path,
2333 uni_path_len = copy_size;
2334 /* free before overwriting resource */
2336 utf16_path = copy_path;
2340 iov[1].iov_len = uni_path_len;
2341 iov[1].iov_base = utf16_path;
2342 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2344 if (tcon->posix_extensions) {
2345 /* resource #3: posix buf */
2346 rc = add_posix_context(iov, &n_iov, mode);
2349 pc_buf = iov[n_iov-1].iov_base;
2353 memset(&rqst, 0, sizeof(struct smb_rqst));
2355 rqst.rq_nvec = n_iov;
2357 /* no need to inc num_remote_opens because we close it just below */
2358 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2359 FILE_WRITE_ATTRIBUTES);
2360 /* resource #4: response buffer */
2361 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2363 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2364 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2366 FILE_WRITE_ATTRIBUTES, rc);
2367 goto err_free_rsp_buf;
2370 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2371 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2372 ses->Suid, CREATE_NOT_FILE,
2373 FILE_WRITE_ATTRIBUTES);
2375 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2377 /* Eventually save off posix specific response info and timestaps */
2380 free_rsp_buf(resp_buftype, rsp);
2383 cifs_small_buf_release(req);
2390 SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2391 struct cifs_open_parms *oparms, __le16 *path)
2393 struct TCP_Server_Info *server = tcon->ses->server;
2394 struct smb2_create_req *req;
2395 unsigned int n_iov = 2;
2396 __u32 file_attributes = 0;
2399 unsigned int total_len;
2400 struct kvec *iov = rqst->rq_iov;
2404 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2408 iov[0].iov_base = (char *)req;
2409 /* -1 since last byte is buf[0] which is sent below (path) */
2410 iov[0].iov_len = total_len - 1;
2412 if (oparms->create_options & CREATE_OPTION_READONLY)
2413 file_attributes |= ATTR_READONLY;
2414 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2415 file_attributes |= ATTR_SYSTEM;
2417 req->ImpersonationLevel = IL_IMPERSONATION;
2418 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2419 /* File attributes ignored on open (used in create though) */
2420 req->FileAttributes = cpu_to_le32(file_attributes);
2421 req->ShareAccess = FILE_SHARE_ALL_LE;
2423 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2424 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2425 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2427 /* [MS-SMB2] 2.2.13 NameOffset:
2428 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2429 * the SMB2 header, the file name includes a prefix that will
2430 * be processed during DFS name normalization as specified in
2431 * section 3.3.5.9. Otherwise, the file name is relative to
2432 * the share that is identified by the TreeId in the SMB2
2435 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2438 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2439 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2441 tcon->treeName, path);
2444 req->NameLength = cpu_to_le16(name_len * 2);
2445 uni_path_len = copy_size;
2448 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2449 /* MUST set path len (NameLength) to 0 opening root of share */
2450 req->NameLength = cpu_to_le16(uni_path_len - 2);
2451 copy_size = uni_path_len;
2452 if (copy_size % 8 != 0)
2453 copy_size = roundup(copy_size, 8);
2454 copy_path = kzalloc(copy_size, GFP_KERNEL);
2457 memcpy((char *)copy_path, (const char *)path,
2459 uni_path_len = copy_size;
2463 iov[1].iov_len = uni_path_len;
2464 iov[1].iov_base = path;
2466 if ((!server->oplocks) || (tcon->no_lease))
2467 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2469 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2470 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2471 req->RequestedOplockLevel = *oplock;
2472 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2473 (oparms->create_options & CREATE_NOT_FILE))
2474 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2476 rc = add_lease_context(server, iov, &n_iov,
2477 oparms->fid->lease_key, oplock);
2482 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2483 /* need to set Next field of lease context if we request it */
2484 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2485 struct create_context *ccontext =
2486 (struct create_context *)iov[n_iov-1].iov_base;
2488 cpu_to_le32(server->vals->create_lease_size);
2491 rc = add_durable_context(iov, &n_iov, oparms,
2492 tcon->use_persistent);
2497 if (tcon->posix_extensions) {
2499 struct create_context *ccontext =
2500 (struct create_context *)iov[n_iov-1].iov_base;
2502 cpu_to_le32(iov[n_iov-1].iov_len);
2505 rc = add_posix_context(iov, &n_iov, oparms->mode);
2510 if (tcon->snapshot_time) {
2511 cifs_dbg(FYI, "adding snapshot context\n");
2513 struct create_context *ccontext =
2514 (struct create_context *)iov[n_iov-1].iov_base;
2516 cpu_to_le32(iov[n_iov-1].iov_len);
2519 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2524 /* TODO: add handling for the mode on create */
2525 if (oparms->disposition == FILE_CREATE)
2526 cifs_dbg(VFS, "mode is 0x%x\n", oparms->mode); /* BB REMOVEME */
2528 if ((oparms->disposition == FILE_CREATE) && (oparms->mode != -1)) {
2530 struct create_context *ccontext =
2531 (struct create_context *)iov[n_iov-1].iov_base;
2533 cpu_to_le32(iov[n_iov-1].iov_len);
2536 /* rc = add_sd_context(iov, &n_iov, oparms->mode); */
2542 struct create_context *ccontext =
2543 (struct create_context *)iov[n_iov-1].iov_base;
2544 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2546 add_query_id_context(iov, &n_iov);
2548 rqst->rq_nvec = n_iov;
2552 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2553 * All other vectors are freed by kfree().
2556 SMB2_open_free(struct smb_rqst *rqst)
2560 if (rqst && rqst->rq_iov) {
2561 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2562 for (i = 1; i < rqst->rq_nvec; i++)
2563 if (rqst->rq_iov[i].iov_base != smb2_padding)
2564 kfree(rqst->rq_iov[i].iov_base);
2569 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2570 __u8 *oplock, struct smb2_file_all_info *buf,
2571 struct kvec *err_iov, int *buftype)
2573 struct smb_rqst rqst;
2574 struct smb2_create_rsp *rsp = NULL;
2575 struct TCP_Server_Info *server;
2576 struct cifs_tcon *tcon = oparms->tcon;
2577 struct cifs_ses *ses = tcon->ses;
2578 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2579 struct kvec rsp_iov = {NULL, 0};
2580 int resp_buftype = CIFS_NO_BUFFER;
2584 cifs_dbg(FYI, "create/open\n");
2585 if (ses && (ses->server))
2586 server = ses->server;
2590 if (smb3_encryption_required(tcon))
2591 flags |= CIFS_TRANSFORM_REQ;
2593 memset(&rqst, 0, sizeof(struct smb_rqst));
2594 memset(&iov, 0, sizeof(iov));
2596 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2598 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2602 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2603 oparms->create_options, oparms->desired_access);
2605 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2607 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2610 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2611 if (err_iov && rsp) {
2613 *buftype = resp_buftype;
2614 resp_buftype = CIFS_NO_BUFFER;
2617 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2618 oparms->create_options, oparms->desired_access, rc);
2619 if (rc == -EREMCHG) {
2620 printk_once(KERN_WARNING "server share %s deleted\n",
2622 tcon->need_reconnect = true;
2626 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2627 ses->Suid, oparms->create_options,
2628 oparms->desired_access);
2630 atomic_inc(&tcon->num_remote_opens);
2631 oparms->fid->persistent_fid = rsp->PersistentFileId;
2632 oparms->fid->volatile_fid = rsp->VolatileFileId;
2633 #ifdef CONFIG_CIFS_DEBUG2
2634 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2635 #endif /* CIFS_DEBUG2 */
2638 memcpy(buf, &rsp->CreationTime, 32);
2639 buf->AllocationSize = rsp->AllocationSize;
2640 buf->EndOfFile = rsp->EndofFile;
2641 buf->Attributes = rsp->FileAttributes;
2642 buf->NumberOfLinks = cpu_to_le32(1);
2643 buf->DeletePending = 0;
2647 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2648 oparms->fid->lease_key, oplock, buf);
2650 SMB2_open_free(&rqst);
2651 free_rsp_buf(resp_buftype, rsp);
2656 SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2657 u64 persistent_fid, u64 volatile_fid, u32 opcode,
2658 bool is_fsctl, char *in_data, u32 indatalen,
2659 __u32 max_response_size)
2661 struct smb2_ioctl_req *req;
2662 struct kvec *iov = rqst->rq_iov;
2663 unsigned int total_len;
2667 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
2673 * indatalen is usually small at a couple of bytes max, so
2674 * just allocate through generic pool
2676 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
2678 cifs_small_buf_release(req);
2683 req->CtlCode = cpu_to_le32(opcode);
2684 req->PersistentFileId = persistent_fid;
2685 req->VolatileFileId = volatile_fid;
2687 iov[0].iov_base = (char *)req;
2689 * If no input data, the size of ioctl struct in
2690 * protocol spec still includes a 1 byte data buffer,
2691 * but if input data passed to ioctl, we do not
2692 * want to double count this, so we do not send
2693 * the dummy one byte of data in iovec[0] if sending
2694 * input data (in iovec[1]).
2697 req->InputCount = cpu_to_le32(indatalen);
2698 /* do not set InputOffset if no input data */
2700 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
2702 iov[0].iov_len = total_len - 1;
2703 iov[1].iov_base = in_data_buf;
2704 iov[1].iov_len = indatalen;
2707 iov[0].iov_len = total_len;
2710 req->OutputOffset = 0;
2711 req->OutputCount = 0; /* MBZ */
2714 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2715 * We Could increase default MaxOutputResponse, but that could require
2716 * more credits. Windows typically sets this smaller, but for some
2717 * ioctls it may be useful to allow server to send more. No point
2718 * limiting what the server can send as long as fits in one credit
2719 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2720 * to increase this limit up in the future.
2721 * Note that for snapshot queries that servers like Azure expect that
2722 * the first query be minimal size (and just used to get the number/size
2723 * of previous versions) so response size must be specified as EXACTLY
2724 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2725 * of eight bytes. Currently that is the only case where we set max
2726 * response size smaller.
2728 req->MaxOutputResponse = cpu_to_le32(max_response_size);
2731 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2735 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2736 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
2737 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
2743 SMB2_ioctl_free(struct smb_rqst *rqst)
2746 if (rqst && rqst->rq_iov) {
2747 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2748 for (i = 1; i < rqst->rq_nvec; i++)
2749 if (rqst->rq_iov[i].iov_base != smb2_padding)
2750 kfree(rqst->rq_iov[i].iov_base);
2756 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2759 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2760 u64 volatile_fid, u32 opcode, bool is_fsctl,
2761 char *in_data, u32 indatalen, u32 max_out_data_len,
2762 char **out_data, u32 *plen /* returned data len */)
2764 struct smb_rqst rqst;
2765 struct smb2_ioctl_rsp *rsp = NULL;
2766 struct cifs_ses *ses;
2767 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
2768 struct kvec rsp_iov = {NULL, 0};
2769 int resp_buftype = CIFS_NO_BUFFER;
2772 struct TCP_Server_Info *server;
2774 cifs_dbg(FYI, "SMB2 IOCTL\n");
2776 if (out_data != NULL)
2779 /* zero out returned data len, in case of error */
2790 server = ses->server;
2794 if (smb3_encryption_required(tcon))
2795 flags |= CIFS_TRANSFORM_REQ;
2797 memset(&rqst, 0, sizeof(struct smb_rqst));
2798 memset(&iov, 0, sizeof(iov));
2800 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
2802 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
2803 is_fsctl, in_data, indatalen, max_out_data_len);
2807 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2809 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
2812 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2813 ses->Suid, 0, opcode, rc);
2815 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
2816 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2818 } else if (rc == -EINVAL) {
2819 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2820 (opcode != FSCTL_SRV_COPYCHUNK)) {
2821 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2824 } else if (rc == -E2BIG) {
2825 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
2826 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2831 /* check if caller wants to look at return data or just return rc */
2832 if ((plen == NULL) || (out_data == NULL))
2835 *plen = le32_to_cpu(rsp->OutputCount);
2837 /* We check for obvious errors in the output buffer length and offset */
2839 goto ioctl_exit; /* server returned no data */
2840 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
2841 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2847 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
2848 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2849 le32_to_cpu(rsp->OutputOffset));
2855 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2857 if (*out_data == NULL) {
2863 SMB2_ioctl_free(&rqst);
2864 free_rsp_buf(resp_buftype, rsp);
2869 * Individual callers to ioctl worker function follow
2873 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2874 u64 persistent_fid, u64 volatile_fid)
2877 struct compress_ioctl fsctl_input;
2878 char *ret_data = NULL;
2880 fsctl_input.CompressionState =
2881 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
2883 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2884 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2885 (char *)&fsctl_input /* data input */,
2886 2 /* in data len */, CIFSMaxBufSize /* max out data */,
2887 &ret_data /* out data */, NULL);
2889 cifs_dbg(FYI, "set compression rc %d\n", rc);
2895 SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2896 u64 persistent_fid, u64 volatile_fid)
2898 struct smb2_close_req *req;
2899 struct kvec *iov = rqst->rq_iov;
2900 unsigned int total_len;
2903 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2907 req->PersistentFileId = persistent_fid;
2908 req->VolatileFileId = volatile_fid;
2909 iov[0].iov_base = (char *)req;
2910 iov[0].iov_len = total_len;
2916 SMB2_close_free(struct smb_rqst *rqst)
2918 if (rqst && rqst->rq_iov)
2919 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2923 SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2924 u64 persistent_fid, u64 volatile_fid, int flags)
2926 struct smb_rqst rqst;
2927 struct smb2_close_rsp *rsp = NULL;
2928 struct cifs_ses *ses = tcon->ses;
2930 struct kvec rsp_iov;
2931 int resp_buftype = CIFS_NO_BUFFER;
2934 cifs_dbg(FYI, "Close\n");
2936 if (!ses || !(ses->server))
2939 if (smb3_encryption_required(tcon))
2940 flags |= CIFS_TRANSFORM_REQ;
2942 memset(&rqst, 0, sizeof(struct smb_rqst));
2943 memset(&iov, 0, sizeof(iov));
2947 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
2948 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid);
2952 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2953 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2956 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
2957 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2961 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
2964 atomic_dec(&tcon->num_remote_opens);
2966 /* BB FIXME - decode close response, update inode for caching */
2969 SMB2_close_free(&rqst);
2970 free_rsp_buf(resp_buftype, rsp);
2975 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2976 u64 persistent_fid, u64 volatile_fid)
2978 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2982 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
2983 struct kvec *iov, unsigned int min_buf_size)
2985 unsigned int smb_len = iov->iov_len;
2986 char *end_of_smb = smb_len + (char *)iov->iov_base;
2987 char *begin_of_buf = offset + (char *)iov->iov_base;
2988 char *end_of_buf = begin_of_buf + buffer_length;
2991 if (buffer_length < min_buf_size) {
2992 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2993 buffer_length, min_buf_size);
2997 /* check if beyond RFC1001 maximum length */
2998 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
2999 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3000 buffer_length, smb_len);
3004 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3005 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
3013 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3014 * Caller must free buffer.
3017 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3018 struct kvec *iov, unsigned int minbufsize,
3021 char *begin_of_buf = offset + (char *)iov->iov_base;
3027 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3031 memcpy(data, begin_of_buf, buffer_length);
3037 SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3038 u64 persistent_fid, u64 volatile_fid,
3039 u8 info_class, u8 info_type, u32 additional_info,
3040 size_t output_len, size_t input_len, void *input)
3042 struct smb2_query_info_req *req;
3043 struct kvec *iov = rqst->rq_iov;
3044 unsigned int total_len;
3047 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3052 req->InfoType = info_type;
3053 req->FileInfoClass = info_class;
3054 req->PersistentFileId = persistent_fid;
3055 req->VolatileFileId = volatile_fid;
3056 req->AdditionalInformation = cpu_to_le32(additional_info);
3058 req->OutputBufferLength = cpu_to_le32(output_len);
3060 req->InputBufferLength = cpu_to_le32(input_len);
3061 /* total_len for smb query request never close to le16 max */
3062 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3063 memcpy(req->Buffer, input, input_len);
3066 iov[0].iov_base = (char *)req;
3068 iov[0].iov_len = total_len - 1 + input_len;
3073 SMB2_query_info_free(struct smb_rqst *rqst)
3075 if (rqst && rqst->rq_iov)
3076 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3080 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3081 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3082 u32 additional_info, size_t output_len, size_t min_len, void **data,
3085 struct smb_rqst rqst;
3086 struct smb2_query_info_rsp *rsp = NULL;
3088 struct kvec rsp_iov;
3090 int resp_buftype = CIFS_NO_BUFFER;
3091 struct cifs_ses *ses = tcon->ses;
3092 struct TCP_Server_Info *server;
3094 bool allocated = false;
3096 cifs_dbg(FYI, "Query Info\n");
3100 server = ses->server;
3104 if (smb3_encryption_required(tcon))
3105 flags |= CIFS_TRANSFORM_REQ;
3107 memset(&rqst, 0, sizeof(struct smb_rqst));
3108 memset(&iov, 0, sizeof(iov));
3112 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
3113 info_class, info_type, additional_info,
3114 output_len, 0, NULL);
3118 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3119 ses->Suid, info_class, (__u32)info_type);
3121 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3122 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3125 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3126 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3127 ses->Suid, info_class, (__u32)info_type, rc);
3131 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3132 ses->Suid, info_class, (__u32)info_type);
3135 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3137 *data = kmalloc(*dlen, GFP_KERNEL);
3140 "Error %d allocating memory for acl\n",
3150 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3151 le32_to_cpu(rsp->OutputBufferLength),
3152 &rsp_iov, min_len, *data);
3153 if (rc && allocated) {
3160 SMB2_query_info_free(&rqst);
3161 free_rsp_buf(resp_buftype, rsp);
3165 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3166 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3168 return query_info(xid, tcon, persistent_fid, volatile_fid,
3169 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3170 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3171 sizeof(struct smb2_file_all_info), (void **)&data,
3176 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3177 u64 persistent_fid, u64 volatile_fid,
3178 void **data, u32 *plen)
3180 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3183 return query_info(xid, tcon, persistent_fid, volatile_fid,
3184 0, SMB2_O_INFO_SECURITY, additional_info,
3185 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3189 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3190 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3192 return query_info(xid, tcon, persistent_fid, volatile_fid,
3193 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3194 sizeof(struct smb2_file_internal_info),
3195 sizeof(struct smb2_file_internal_info),
3196 (void **)&uniqueid, NULL);
3200 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3201 * See MS-SMB2 2.2.35 and 2.2.36
3205 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3206 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid,
3207 u32 completion_filter, bool watch_tree)
3209 struct smb2_change_notify_req *req;
3210 struct kvec *iov = rqst->rq_iov;
3211 unsigned int total_len;
3214 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, (void **) &req, &total_len);
3218 req->PersistentFileId = persistent_fid;
3219 req->VolatileFileId = volatile_fid;
3220 req->OutputBufferLength = SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE;
3221 req->CompletionFilter = cpu_to_le32(completion_filter);
3223 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3227 iov[0].iov_base = (char *)req;
3228 iov[0].iov_len = total_len;
3234 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3235 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3236 u32 completion_filter)
3238 struct cifs_ses *ses = tcon->ses;
3239 struct smb_rqst rqst;
3241 struct kvec rsp_iov = {NULL, 0};
3242 int resp_buftype = CIFS_NO_BUFFER;
3246 cifs_dbg(FYI, "change notify\n");
3247 if (!ses || !(ses->server))
3250 if (smb3_encryption_required(tcon))
3251 flags |= CIFS_TRANSFORM_REQ;
3253 memset(&rqst, 0, sizeof(struct smb_rqst));
3254 memset(&iov, 0, sizeof(iov));
3258 rc = SMB2_notify_init(xid, &rqst, tcon, persistent_fid, volatile_fid,
3259 completion_filter, watch_tree);
3263 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3264 (u8)watch_tree, completion_filter);
3265 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3268 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3269 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3270 (u8)watch_tree, completion_filter, rc);
3272 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3273 ses->Suid, (u8)watch_tree, completion_filter);
3277 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3278 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3285 * This is a no-op for now. We're not really interested in the reply, but
3286 * rather in the fact that the server sent one and that server->lstrp
3289 * FIXME: maybe we should consider checking that the reply matches request?
3292 smb2_echo_callback(struct mid_q_entry *mid)
3294 struct TCP_Server_Info *server = mid->callback_data;
3295 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3296 struct cifs_credits credits = { .value = 0, .instance = 0 };
3298 if (mid->mid_state == MID_RESPONSE_RECEIVED
3299 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3300 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3301 credits.instance = server->reconnect_instance;
3304 DeleteMidQEntry(mid);
3305 add_credits(server, &credits, CIFS_ECHO_OP);
3308 void smb2_reconnect_server(struct work_struct *work)
3310 struct TCP_Server_Info *server = container_of(work,
3311 struct TCP_Server_Info, reconnect.work);
3312 struct cifs_ses *ses;
3313 struct cifs_tcon *tcon, *tcon2;
3314 struct list_head tmp_list;
3315 int tcon_exist = false;
3317 int resched = false;
3320 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3321 mutex_lock(&server->reconnect_mutex);
3323 INIT_LIST_HEAD(&tmp_list);
3324 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3326 spin_lock(&cifs_tcp_ses_lock);
3327 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3328 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3329 if (tcon->need_reconnect || tcon->need_reopen_files) {
3331 list_add_tail(&tcon->rlist, &tmp_list);
3336 * IPC has the same lifetime as its session and uses its
3339 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3340 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3346 * Get the reference to server struct to be sure that the last call of
3347 * cifs_put_tcon() in the loop below won't release the server pointer.
3350 server->srv_count++;
3352 spin_unlock(&cifs_tcp_ses_lock);
3354 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3355 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3357 cifs_reopen_persistent_handles(tcon);
3360 list_del_init(&tcon->rlist);
3362 cifs_put_smb_ses(tcon->ses);
3364 cifs_put_tcon(tcon);
3367 cifs_dbg(FYI, "Reconnecting tcons finished\n");
3369 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3370 mutex_unlock(&server->reconnect_mutex);
3372 /* now we can safely release srv struct */
3374 cifs_put_tcp_session(server, 1);
3378 SMB2_echo(struct TCP_Server_Info *server)
3380 struct smb2_echo_req *req;
3383 struct smb_rqst rqst = { .rq_iov = iov,
3385 unsigned int total_len;
3387 cifs_dbg(FYI, "In echo request\n");
3389 if (server->tcpStatus == CifsNeedNegotiate) {
3390 /* No need to send echo on newly established connections */
3391 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3395 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
3399 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3401 iov[0].iov_len = total_len;
3402 iov[0].iov_base = (char *)req;
3404 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3405 server, CIFS_ECHO_OP, NULL);
3407 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3409 cifs_small_buf_release(req);
3414 SMB2_flush_free(struct smb_rqst *rqst)
3416 if (rqst && rqst->rq_iov)
3417 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3421 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3422 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid)
3424 struct smb2_flush_req *req;
3425 struct kvec *iov = rqst->rq_iov;
3426 unsigned int total_len;
3429 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
3433 req->PersistentFileId = persistent_fid;
3434 req->VolatileFileId = volatile_fid;
3436 iov[0].iov_base = (char *)req;
3437 iov[0].iov_len = total_len;
3443 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3446 struct cifs_ses *ses = tcon->ses;
3447 struct smb_rqst rqst;
3449 struct kvec rsp_iov = {NULL, 0};
3450 int resp_buftype = CIFS_NO_BUFFER;
3454 cifs_dbg(FYI, "flush\n");
3455 if (!ses || !(ses->server))
3458 if (smb3_encryption_required(tcon))
3459 flags |= CIFS_TRANSFORM_REQ;
3461 memset(&rqst, 0, sizeof(struct smb_rqst));
3462 memset(&iov, 0, sizeof(iov));
3466 rc = SMB2_flush_init(xid, &rqst, tcon, persistent_fid, volatile_fid);
3470 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3471 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3474 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
3475 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3478 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3482 SMB2_flush_free(&rqst);
3483 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3488 * To form a chain of read requests, any read requests after the first should
3489 * have the end_of_chain boolean set to true.
3492 smb2_new_read_req(void **buf, unsigned int *total_len,
3493 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3494 unsigned int remaining_bytes, int request_type)
3497 struct smb2_read_plain_req *req = NULL;
3498 struct smb2_sync_hdr *shdr;
3499 struct TCP_Server_Info *server;
3501 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3506 server = io_parms->tcon->ses->server;
3508 return -ECONNABORTED;
3510 shdr = &req->sync_hdr;
3511 shdr->ProcessId = cpu_to_le32(io_parms->pid);
3513 req->PersistentFileId = io_parms->persistent_fid;
3514 req->VolatileFileId = io_parms->volatile_fid;
3515 req->ReadChannelInfoOffset = 0; /* reserved */
3516 req->ReadChannelInfoLength = 0; /* reserved */
3517 req->Channel = 0; /* reserved */
3518 req->MinimumCount = 0;
3519 req->Length = cpu_to_le32(io_parms->length);
3520 req->Offset = cpu_to_le64(io_parms->offset);
3522 trace_smb3_read_enter(0 /* xid */,
3523 io_parms->persistent_fid,
3524 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3525 io_parms->offset, io_parms->length);
3526 #ifdef CONFIG_CIFS_SMB_DIRECT
3528 * If we want to do a RDMA write, fill in and append
3529 * smbd_buffer_descriptor_v1 to the end of read request
3531 if (server->rdma && rdata && !server->sign &&
3532 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
3534 struct smbd_buffer_descriptor_v1 *v1;
3535 bool need_invalidate =
3536 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3538 rdata->mr = smbd_register_mr(
3539 server->smbd_conn, rdata->pages,
3540 rdata->nr_pages, rdata->page_offset,
3541 rdata->tailsz, true, need_invalidate);
3545 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3546 if (need_invalidate)
3547 req->Channel = SMB2_CHANNEL_RDMA_V1;
3548 req->ReadChannelInfoOffset =
3549 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
3550 req->ReadChannelInfoLength =
3551 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3552 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3553 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3554 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3555 v1->length = cpu_to_le32(rdata->mr->mr->length);
3557 *total_len += sizeof(*v1) - 1;
3560 if (request_type & CHAINED_REQUEST) {
3561 if (!(request_type & END_OF_CHAIN)) {
3562 /* next 8-byte aligned request */
3563 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3564 shdr->NextCommand = cpu_to_le32(*total_len);
3565 } else /* END_OF_CHAIN */
3566 shdr->NextCommand = 0;
3567 if (request_type & RELATED_REQUEST) {
3568 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
3570 * Related requests use info from previous read request
3573 shdr->SessionId = 0xFFFFFFFF;
3574 shdr->TreeId = 0xFFFFFFFF;
3575 req->PersistentFileId = 0xFFFFFFFF;
3576 req->VolatileFileId = 0xFFFFFFFF;
3579 if (remaining_bytes > io_parms->length)
3580 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3582 req->RemainingBytes = 0;
3589 smb2_readv_callback(struct mid_q_entry *mid)
3591 struct cifs_readdata *rdata = mid->callback_data;
3592 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3593 struct TCP_Server_Info *server = tcon->ses->server;
3594 struct smb2_sync_hdr *shdr =
3595 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
3596 struct cifs_credits credits = { .value = 0, .instance = 0 };
3597 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
3599 .rq_pages = rdata->pages,
3600 .rq_offset = rdata->page_offset,
3601 .rq_npages = rdata->nr_pages,
3602 .rq_pagesz = rdata->pagesz,
3603 .rq_tailsz = rdata->tailsz };
3605 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3606 __func__, mid->mid, mid->mid_state, rdata->result,
3609 switch (mid->mid_state) {
3610 case MID_RESPONSE_RECEIVED:
3611 credits.value = le16_to_cpu(shdr->CreditRequest);
3612 credits.instance = server->reconnect_instance;
3613 /* result already set, check signature */
3614 if (server->sign && !mid->decrypted) {
3617 rc = smb2_verify_signature(&rqst, server);
3619 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
3622 /* FIXME: should this be counted toward the initiating task? */
3623 task_io_account_read(rdata->got_bytes);
3624 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3626 case MID_REQUEST_SUBMITTED:
3627 case MID_RETRY_NEEDED:
3628 rdata->result = -EAGAIN;
3629 if (server->sign && rdata->got_bytes)
3630 /* reset bytes number since we can not check a sign */
3631 rdata->got_bytes = 0;
3632 /* FIXME: should this be counted toward the initiating task? */
3633 task_io_account_read(rdata->got_bytes);
3634 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3636 case MID_RESPONSE_MALFORMED:
3637 credits.value = le16_to_cpu(shdr->CreditRequest);
3638 credits.instance = server->reconnect_instance;
3641 rdata->result = -EIO;
3643 #ifdef CONFIG_CIFS_SMB_DIRECT
3645 * If this rdata has a memmory registered, the MR can be freed
3646 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3647 * because they have limited number and are used for future I/Os
3650 smbd_deregister_mr(rdata->mr);
3654 if (rdata->result && rdata->result != -ENODATA) {
3655 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
3656 trace_smb3_read_err(0 /* xid */,
3657 rdata->cfile->fid.persistent_fid,
3658 tcon->tid, tcon->ses->Suid, rdata->offset,
3659 rdata->bytes, rdata->result);
3661 trace_smb3_read_done(0 /* xid */,
3662 rdata->cfile->fid.persistent_fid,
3663 tcon->tid, tcon->ses->Suid,
3664 rdata->offset, rdata->got_bytes);
3666 queue_work(cifsiod_wq, &rdata->work);
3667 DeleteMidQEntry(mid);
3668 add_credits(server, &credits, 0);
3671 /* smb2_async_readv - send an async read, and set up mid to handle result */
3673 smb2_async_readv(struct cifs_readdata *rdata)
3677 struct smb2_sync_hdr *shdr;
3678 struct cifs_io_parms io_parms;
3679 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3681 struct TCP_Server_Info *server;
3682 unsigned int total_len;
3684 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3685 __func__, rdata->offset, rdata->bytes);
3687 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3688 io_parms.offset = rdata->offset;
3689 io_parms.length = rdata->bytes;
3690 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3691 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3692 io_parms.pid = rdata->pid;
3694 server = io_parms.tcon->ses->server;
3696 rc = smb2_new_read_req(
3697 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
3701 if (smb3_encryption_required(io_parms.tcon))
3702 flags |= CIFS_TRANSFORM_REQ;
3704 rdata->iov[0].iov_base = buf;
3705 rdata->iov[0].iov_len = total_len;
3707 shdr = (struct smb2_sync_hdr *)buf;
3709 if (rdata->credits.value > 0) {
3710 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
3711 SMB2_MAX_BUFFER_SIZE));
3712 shdr->CreditRequest =
3713 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
3715 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3717 goto async_readv_out;
3719 flags |= CIFS_HAS_CREDITS;
3722 kref_get(&rdata->refcount);
3723 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
3724 cifs_readv_receive, smb2_readv_callback,
3725 smb3_handle_read_data, rdata, flags,
3728 kref_put(&rdata->refcount, cifs_readdata_release);
3729 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
3730 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3732 io_parms.tcon->ses->Suid,
3733 io_parms.offset, io_parms.length, rc);
3737 cifs_small_buf_release(buf);
3742 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3743 unsigned int *nbytes, char **buf, int *buf_type)
3745 struct smb_rqst rqst;
3746 int resp_buftype, rc;
3747 struct smb2_read_plain_req *req = NULL;
3748 struct smb2_read_rsp *rsp = NULL;
3750 struct kvec rsp_iov;
3751 unsigned int total_len;
3752 int flags = CIFS_LOG_ERROR;
3753 struct cifs_ses *ses = io_parms->tcon->ses;
3756 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
3760 if (smb3_encryption_required(io_parms->tcon))
3761 flags |= CIFS_TRANSFORM_REQ;
3763 iov[0].iov_base = (char *)req;
3764 iov[0].iov_len = total_len;
3766 memset(&rqst, 0, sizeof(struct smb_rqst));
3770 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3771 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
3774 if (rc != -ENODATA) {
3775 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3776 cifs_dbg(VFS, "Send error in read = %d\n", rc);
3777 trace_smb3_read_err(xid, req->PersistentFileId,
3778 io_parms->tcon->tid, ses->Suid,
3779 io_parms->offset, io_parms->length,
3782 trace_smb3_read_done(xid, req->PersistentFileId,
3783 io_parms->tcon->tid, ses->Suid,
3784 io_parms->offset, 0);
3785 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3786 cifs_small_buf_release(req);
3787 return rc == -ENODATA ? 0 : rc;
3789 trace_smb3_read_done(xid, req->PersistentFileId,
3790 io_parms->tcon->tid, ses->Suid,
3791 io_parms->offset, io_parms->length);
3793 cifs_small_buf_release(req);
3795 *nbytes = le32_to_cpu(rsp->DataLength);
3796 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3797 (*nbytes > io_parms->length)) {
3798 cifs_dbg(FYI, "bad length %d for count %d\n",
3799 *nbytes, io_parms->length);
3805 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
3806 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3807 } else if (resp_buftype != CIFS_NO_BUFFER) {
3808 *buf = rsp_iov.iov_base;
3809 if (resp_buftype == CIFS_SMALL_BUFFER)
3810 *buf_type = CIFS_SMALL_BUFFER;
3811 else if (resp_buftype == CIFS_LARGE_BUFFER)
3812 *buf_type = CIFS_LARGE_BUFFER;
3818 * Check the mid_state and signature on received buffer (if any), and queue the
3819 * workqueue completion task.
3822 smb2_writev_callback(struct mid_q_entry *mid)
3824 struct cifs_writedata *wdata = mid->callback_data;
3825 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3826 struct TCP_Server_Info *server = tcon->ses->server;
3827 unsigned int written;
3828 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
3829 struct cifs_credits credits = { .value = 0, .instance = 0 };
3831 switch (mid->mid_state) {
3832 case MID_RESPONSE_RECEIVED:
3833 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3834 credits.instance = server->reconnect_instance;
3835 wdata->result = smb2_check_receive(mid, server, 0);
3836 if (wdata->result != 0)
3839 written = le32_to_cpu(rsp->DataLength);
3841 * Mask off high 16 bits when bytes written as returned
3842 * by the server is greater than bytes requested by the
3843 * client. OS/2 servers are known to set incorrect
3846 if (written > wdata->bytes)
3849 if (written < wdata->bytes)
3850 wdata->result = -ENOSPC;
3852 wdata->bytes = written;
3854 case MID_REQUEST_SUBMITTED:
3855 case MID_RETRY_NEEDED:
3856 wdata->result = -EAGAIN;
3858 case MID_RESPONSE_MALFORMED:
3859 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3860 credits.instance = server->reconnect_instance;
3863 wdata->result = -EIO;
3866 #ifdef CONFIG_CIFS_SMB_DIRECT
3868 * If this wdata has a memory registered, the MR can be freed
3869 * The number of MRs available is limited, it's important to recover
3870 * used MR as soon as I/O is finished. Hold MR longer in the later
3871 * I/O process can possibly result in I/O deadlock due to lack of MR
3872 * to send request on I/O retry
3875 smbd_deregister_mr(wdata->mr);
3879 if (wdata->result) {
3880 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3881 trace_smb3_write_err(0 /* no xid */,
3882 wdata->cfile->fid.persistent_fid,
3883 tcon->tid, tcon->ses->Suid, wdata->offset,
3884 wdata->bytes, wdata->result);
3886 trace_smb3_write_done(0 /* no xid */,
3887 wdata->cfile->fid.persistent_fid,
3888 tcon->tid, tcon->ses->Suid,
3889 wdata->offset, wdata->bytes);
3891 queue_work(cifsiod_wq, &wdata->work);
3892 DeleteMidQEntry(mid);
3893 add_credits(server, &credits, 0);
3896 /* smb2_async_writev - send an async write, and set up mid to handle result */
3898 smb2_async_writev(struct cifs_writedata *wdata,
3899 void (*release)(struct kref *kref))
3901 int rc = -EACCES, flags = 0;
3902 struct smb2_write_req *req = NULL;
3903 struct smb2_sync_hdr *shdr;
3904 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3905 struct TCP_Server_Info *server = tcon->ses->server;
3907 struct smb_rqst rqst = { };
3908 unsigned int total_len;
3910 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
3914 if (smb3_encryption_required(tcon))
3915 flags |= CIFS_TRANSFORM_REQ;
3917 shdr = (struct smb2_sync_hdr *)req;
3918 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
3920 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3921 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3922 req->WriteChannelInfoOffset = 0;
3923 req->WriteChannelInfoLength = 0;
3925 req->Offset = cpu_to_le64(wdata->offset);
3926 req->DataOffset = cpu_to_le16(
3927 offsetof(struct smb2_write_req, Buffer));
3928 req->RemainingBytes = 0;
3930 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
3931 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
3932 #ifdef CONFIG_CIFS_SMB_DIRECT
3934 * If we want to do a server RDMA read, fill in and append
3935 * smbd_buffer_descriptor_v1 to the end of write request
3937 if (server->rdma && !server->sign && wdata->bytes >=
3938 server->smbd_conn->rdma_readwrite_threshold) {
3940 struct smbd_buffer_descriptor_v1 *v1;
3941 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3943 wdata->mr = smbd_register_mr(
3944 server->smbd_conn, wdata->pages,
3945 wdata->nr_pages, wdata->page_offset,
3946 wdata->tailsz, false, need_invalidate);
3949 goto async_writev_out;
3952 req->DataOffset = 0;
3953 if (wdata->nr_pages > 1)
3954 req->RemainingBytes =
3956 (wdata->nr_pages - 1) * wdata->pagesz -
3957 wdata->page_offset + wdata->tailsz
3960 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
3961 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3962 if (need_invalidate)
3963 req->Channel = SMB2_CHANNEL_RDMA_V1;
3964 req->WriteChannelInfoOffset =
3965 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
3966 req->WriteChannelInfoLength =
3967 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3968 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3969 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3970 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3971 v1->length = cpu_to_le32(wdata->mr->mr->length);
3974 iov[0].iov_len = total_len - 1;
3975 iov[0].iov_base = (char *)req;
3979 rqst.rq_pages = wdata->pages;
3980 rqst.rq_offset = wdata->page_offset;
3981 rqst.rq_npages = wdata->nr_pages;
3982 rqst.rq_pagesz = wdata->pagesz;
3983 rqst.rq_tailsz = wdata->tailsz;
3984 #ifdef CONFIG_CIFS_SMB_DIRECT
3986 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
3990 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3991 wdata->offset, wdata->bytes);
3993 #ifdef CONFIG_CIFS_SMB_DIRECT
3994 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3996 req->Length = cpu_to_le32(wdata->bytes);
3998 req->Length = cpu_to_le32(wdata->bytes);
4001 if (wdata->credits.value > 0) {
4002 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4003 SMB2_MAX_BUFFER_SIZE));
4004 shdr->CreditRequest =
4005 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
4007 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4009 goto async_writev_out;
4011 flags |= CIFS_HAS_CREDITS;
4014 kref_get(&wdata->refcount);
4015 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4016 wdata, flags, &wdata->credits);
4019 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
4020 tcon->tid, tcon->ses->Suid, wdata->offset,
4022 kref_put(&wdata->refcount, release);
4023 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4027 cifs_small_buf_release(req);
4032 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4033 * The length field from io_parms must be at least 1 and indicates a number of
4034 * elements with data to write that begins with position 1 in iov array. All
4035 * data length is specified by count.
4038 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4039 unsigned int *nbytes, struct kvec *iov, int n_vec)
4041 struct smb_rqst rqst;
4043 struct smb2_write_req *req = NULL;
4044 struct smb2_write_rsp *rsp = NULL;
4046 struct kvec rsp_iov;
4048 unsigned int total_len;
4055 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
4060 if (io_parms->tcon->ses->server == NULL)
4061 return -ECONNABORTED;
4063 if (smb3_encryption_required(io_parms->tcon))
4064 flags |= CIFS_TRANSFORM_REQ;
4066 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
4068 req->PersistentFileId = io_parms->persistent_fid;
4069 req->VolatileFileId = io_parms->volatile_fid;
4070 req->WriteChannelInfoOffset = 0;
4071 req->WriteChannelInfoLength = 0;
4073 req->Length = cpu_to_le32(io_parms->length);
4074 req->Offset = cpu_to_le64(io_parms->offset);
4075 req->DataOffset = cpu_to_le16(
4076 offsetof(struct smb2_write_req, Buffer));
4077 req->RemainingBytes = 0;
4079 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4080 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4081 io_parms->offset, io_parms->length);
4083 iov[0].iov_base = (char *)req;
4085 iov[0].iov_len = total_len - 1;
4087 memset(&rqst, 0, sizeof(struct smb_rqst));
4089 rqst.rq_nvec = n_vec + 1;
4091 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
4092 &resp_buftype, flags, &rsp_iov);
4093 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
4096 trace_smb3_write_err(xid, req->PersistentFileId,
4097 io_parms->tcon->tid,
4098 io_parms->tcon->ses->Suid,
4099 io_parms->offset, io_parms->length, rc);
4100 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
4101 cifs_dbg(VFS, "Send error in write = %d\n", rc);
4103 *nbytes = le32_to_cpu(rsp->DataLength);
4104 trace_smb3_write_done(xid, req->PersistentFileId,
4105 io_parms->tcon->tid,
4106 io_parms->tcon->ses->Suid,
4107 io_parms->offset, *nbytes);
4110 cifs_small_buf_release(req);
4111 free_rsp_buf(resp_buftype, rsp);
4116 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
4119 unsigned int entrycount = 0;
4120 unsigned int next_offset = 0;
4122 FILE_DIRECTORY_INFO *dir_info;
4124 if (bufstart == NULL)
4127 entryptr = bufstart;
4130 if (entryptr + next_offset < entryptr ||
4131 entryptr + next_offset > end_of_buf ||
4132 entryptr + next_offset + size > end_of_buf) {
4133 cifs_dbg(VFS, "malformed search entry would overflow\n");
4137 entryptr = entryptr + next_offset;
4138 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4140 len = le32_to_cpu(dir_info->FileNameLength);
4141 if (entryptr + len < entryptr ||
4142 entryptr + len > end_of_buf ||
4143 entryptr + len + size > end_of_buf) {
4144 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4149 *lastentry = entryptr;
4152 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
4164 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4165 u64 persistent_fid, u64 volatile_fid, int index,
4166 struct cifs_search_info *srch_inf)
4168 struct smb_rqst rqst;
4169 struct smb2_query_directory_req *req;
4170 struct smb2_query_directory_rsp *rsp = NULL;
4172 struct kvec rsp_iov;
4175 int resp_buftype = CIFS_NO_BUFFER;
4176 unsigned char *bufptr;
4177 struct TCP_Server_Info *server;
4178 struct cifs_ses *ses = tcon->ses;
4179 __le16 asteriks = cpu_to_le16('*');
4181 unsigned int output_size = CIFSMaxBufSize;
4182 size_t info_buf_size;
4184 unsigned int total_len;
4186 if (ses && (ses->server))
4187 server = ses->server;
4191 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
4196 if (smb3_encryption_required(tcon))
4197 flags |= CIFS_TRANSFORM_REQ;
4199 switch (srch_inf->info_level) {
4200 case SMB_FIND_FILE_DIRECTORY_INFO:
4201 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4202 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4204 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4205 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4206 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4209 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4210 srch_inf->info_level);
4215 req->FileIndex = cpu_to_le32(index);
4216 req->PersistentFileId = persistent_fid;
4217 req->VolatileFileId = volatile_fid;
4220 bufptr = req->Buffer;
4221 memcpy(bufptr, &asteriks, len);
4223 req->FileNameOffset =
4224 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
4225 req->FileNameLength = cpu_to_le16(len);
4227 * BB could be 30 bytes or so longer if we used SMB2 specific
4228 * buffer lengths, but this is safe and close enough.
4230 output_size = min_t(unsigned int, output_size, server->maxBuf);
4231 output_size = min_t(unsigned int, output_size, 2 << 15);
4232 req->OutputBufferLength = cpu_to_le32(output_size);
4234 iov[0].iov_base = (char *)req;
4236 iov[0].iov_len = total_len - 1;
4238 iov[1].iov_base = (char *)(req->Buffer);
4239 iov[1].iov_len = len;
4241 memset(&rqst, 0, sizeof(struct smb_rqst));
4245 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4246 tcon->ses->Suid, index, output_size);
4248 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4249 cifs_small_buf_release(req);
4250 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
4253 if (rc == -ENODATA &&
4254 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
4255 trace_smb3_query_dir_done(xid, persistent_fid,
4256 tcon->tid, tcon->ses->Suid, index, 0);
4257 srch_inf->endOfSearch = true;
4260 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4261 tcon->ses->Suid, index, 0, rc);
4262 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
4267 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4268 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4271 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4272 tcon->ses->Suid, index, 0, rc);
4276 srch_inf->unicode = true;
4278 if (srch_inf->ntwrk_buf_start) {
4279 if (srch_inf->smallBuf)
4280 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4282 cifs_buf_release(srch_inf->ntwrk_buf_start);
4284 srch_inf->ntwrk_buf_start = (char *)rsp;
4285 srch_inf->srch_entries_start = srch_inf->last_entry =
4286 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4287 end_of_smb = rsp_iov.iov_len + (char *)rsp;
4288 srch_inf->entries_in_buffer =
4289 num_entries(srch_inf->srch_entries_start, end_of_smb,
4290 &srch_inf->last_entry, info_buf_size);
4291 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4292 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4293 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4294 srch_inf->srch_entries_start, srch_inf->last_entry);
4295 if (resp_buftype == CIFS_LARGE_BUFFER)
4296 srch_inf->smallBuf = false;
4297 else if (resp_buftype == CIFS_SMALL_BUFFER)
4298 srch_inf->smallBuf = true;
4300 cifs_tcon_dbg(VFS, "illegal search buffer type\n");
4302 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4303 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
4307 free_rsp_buf(resp_buftype, rsp);
4312 SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
4313 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4314 u8 info_type, u32 additional_info,
4315 void **data, unsigned int *size)
4317 struct smb2_set_info_req *req;
4318 struct kvec *iov = rqst->rq_iov;
4319 unsigned int i, total_len;
4322 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
4326 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4327 req->InfoType = info_type;
4328 req->FileInfoClass = info_class;
4329 req->PersistentFileId = persistent_fid;
4330 req->VolatileFileId = volatile_fid;
4331 req->AdditionalInformation = cpu_to_le32(additional_info);
4334 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
4335 req->BufferLength = cpu_to_le32(*size);
4337 memcpy(req->Buffer, *data, *size);
4340 iov[0].iov_base = (char *)req;
4342 iov[0].iov_len = total_len - 1;
4344 for (i = 1; i < rqst->rq_nvec; i++) {
4345 le32_add_cpu(&req->BufferLength, size[i]);
4346 iov[i].iov_base = (char *)data[i];
4347 iov[i].iov_len = size[i];
4354 SMB2_set_info_free(struct smb_rqst *rqst)
4356 if (rqst && rqst->rq_iov)
4357 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
4361 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4362 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4363 u8 info_type, u32 additional_info, unsigned int num,
4364 void **data, unsigned int *size)
4366 struct smb_rqst rqst;
4367 struct smb2_set_info_rsp *rsp = NULL;
4369 struct kvec rsp_iov;
4372 struct cifs_ses *ses = tcon->ses;
4375 if (!ses || !(ses->server))
4381 if (smb3_encryption_required(tcon))
4382 flags |= CIFS_TRANSFORM_REQ;
4384 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4388 memset(&rqst, 0, sizeof(struct smb_rqst));
4392 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4393 info_class, info_type, additional_info,
4401 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
4403 SMB2_set_info_free(&rqst);
4404 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
4407 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
4408 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4409 ses->Suid, info_class, (__u32)info_type, rc);
4412 free_rsp_buf(resp_buftype, rsp);
4418 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4419 u64 volatile_fid, u32 pid, __le64 *eof)
4421 struct smb2_file_eof_info info;
4425 info.EndOfFile = *eof;
4428 size = sizeof(struct smb2_file_eof_info);
4430 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4431 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4432 0, 1, &data, &size);
4436 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4437 u64 persistent_fid, u64 volatile_fid,
4438 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4440 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4441 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4442 1, (void **)&pnntsd, &pacllen);
4446 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4447 u64 persistent_fid, u64 volatile_fid,
4448 struct smb2_file_full_ea_info *buf, int len)
4450 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4451 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4452 0, 1, (void **)&buf, &len);
4456 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4457 const u64 persistent_fid, const u64 volatile_fid,
4460 struct smb_rqst rqst;
4462 struct smb2_oplock_break *req = NULL;
4463 struct cifs_ses *ses = tcon->ses;
4464 int flags = CIFS_OBREAK_OP;
4465 unsigned int total_len;
4467 struct kvec rsp_iov;
4470 cifs_dbg(FYI, "SMB2_oplock_break\n");
4471 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4476 if (smb3_encryption_required(tcon))
4477 flags |= CIFS_TRANSFORM_REQ;
4479 req->VolatileFid = volatile_fid;
4480 req->PersistentFid = persistent_fid;
4481 req->OplockLevel = oplock_level;
4482 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4484 flags |= CIFS_NO_RSP_BUF;
4486 iov[0].iov_base = (char *)req;
4487 iov[0].iov_len = total_len;
4489 memset(&rqst, 0, sizeof(struct smb_rqst));
4493 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4494 cifs_small_buf_release(req);
4497 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4498 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
4505 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4506 struct kstatfs *kst)
4508 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4509 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4510 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
4511 kst->f_bfree = kst->f_bavail =
4512 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
4517 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4518 struct kstatfs *kst)
4520 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4521 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4522 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4523 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4524 kst->f_bavail = kst->f_bfree;
4526 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4527 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4528 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4529 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4530 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4536 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4537 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4540 struct smb2_query_info_req *req;
4541 unsigned int total_len;
4543 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
4545 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4548 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4553 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4554 req->FileInfoClass = level;
4555 req->PersistentFileId = persistent_fid;
4556 req->VolatileFileId = volatile_fid;
4558 req->InputBufferOffset =
4559 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
4560 req->OutputBufferLength = cpu_to_le32(
4561 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
4563 iov->iov_base = (char *)req;
4564 iov->iov_len = total_len;
4569 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4570 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4572 struct smb_rqst rqst;
4573 struct smb2_query_info_rsp *rsp = NULL;
4575 struct kvec rsp_iov;
4578 struct cifs_ses *ses = tcon->ses;
4579 FILE_SYSTEM_POSIX_INFO *info = NULL;
4582 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4583 sizeof(FILE_SYSTEM_POSIX_INFO),
4584 persistent_fid, volatile_fid);
4588 if (smb3_encryption_required(tcon))
4589 flags |= CIFS_TRANSFORM_REQ;
4591 memset(&rqst, 0, sizeof(struct smb_rqst));
4595 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4596 cifs_small_buf_release(iov.iov_base);
4598 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4599 goto posix_qfsinf_exit;
4601 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4603 info = (FILE_SYSTEM_POSIX_INFO *)(
4604 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4605 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4606 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4607 sizeof(FILE_SYSTEM_POSIX_INFO));
4609 copy_posix_fs_info_to_kstatfs(info, fsdata);
4612 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4617 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4618 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4620 struct smb_rqst rqst;
4621 struct smb2_query_info_rsp *rsp = NULL;
4623 struct kvec rsp_iov;
4626 struct cifs_ses *ses = tcon->ses;
4627 struct smb2_fs_full_size_info *info = NULL;
4630 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4631 sizeof(struct smb2_fs_full_size_info),
4632 persistent_fid, volatile_fid);
4636 if (smb3_encryption_required(tcon))
4637 flags |= CIFS_TRANSFORM_REQ;
4639 memset(&rqst, 0, sizeof(struct smb_rqst));
4643 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4644 cifs_small_buf_release(iov.iov_base);
4646 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4649 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4651 info = (struct smb2_fs_full_size_info *)(
4652 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4653 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4654 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4655 sizeof(struct smb2_fs_full_size_info));
4657 smb2_copy_fs_info_to_kstatfs(info, fsdata);
4660 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4665 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
4666 u64 persistent_fid, u64 volatile_fid, int level)
4668 struct smb_rqst rqst;
4669 struct smb2_query_info_rsp *rsp = NULL;
4671 struct kvec rsp_iov;
4673 int resp_buftype, max_len, min_len;
4674 struct cifs_ses *ses = tcon->ses;
4675 unsigned int rsp_len, offset;
4678 if (level == FS_DEVICE_INFORMATION) {
4679 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4680 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4681 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4682 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4683 min_len = MIN_FS_ATTR_INFO_SIZE;
4684 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4685 max_len = sizeof(struct smb3_fs_ss_info);
4686 min_len = sizeof(struct smb3_fs_ss_info);
4687 } else if (level == FS_VOLUME_INFORMATION) {
4688 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4689 min_len = sizeof(struct smb3_fs_vol_info);
4691 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
4695 rc = build_qfs_info_req(&iov, tcon, level, max_len,
4696 persistent_fid, volatile_fid);
4700 if (smb3_encryption_required(tcon))
4701 flags |= CIFS_TRANSFORM_REQ;
4703 memset(&rqst, 0, sizeof(struct smb_rqst));
4707 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4708 cifs_small_buf_release(iov.iov_base);
4710 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4713 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4715 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4716 offset = le16_to_cpu(rsp->OutputBufferOffset);
4717 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
4721 if (level == FS_ATTRIBUTE_INFORMATION)
4722 memcpy(&tcon->fsAttrInfo, offset
4723 + (char *)rsp, min_t(unsigned int,
4725 else if (level == FS_DEVICE_INFORMATION)
4726 memcpy(&tcon->fsDevInfo, offset
4727 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
4728 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4729 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
4730 (offset + (char *)rsp);
4731 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4732 tcon->perf_sector_size =
4733 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
4734 } else if (level == FS_VOLUME_INFORMATION) {
4735 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4736 (offset + (char *)rsp);
4737 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4738 tcon->vol_create_time = vol_info->VolumeCreationTime;
4742 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4747 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4748 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4749 const __u32 num_lock, struct smb2_lock_element *buf)
4751 struct smb_rqst rqst;
4753 struct smb2_lock_req *req = NULL;
4755 struct kvec rsp_iov;
4758 int flags = CIFS_NO_RSP_BUF;
4759 unsigned int total_len;
4761 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
4763 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
4767 if (smb3_encryption_required(tcon))
4768 flags |= CIFS_TRANSFORM_REQ;
4770 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4771 req->LockCount = cpu_to_le16(num_lock);
4773 req->PersistentFileId = persist_fid;
4774 req->VolatileFileId = volatile_fid;
4776 count = num_lock * sizeof(struct smb2_lock_element);
4778 iov[0].iov_base = (char *)req;
4779 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
4780 iov[1].iov_base = (char *)buf;
4781 iov[1].iov_len = count;
4783 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
4785 memset(&rqst, 0, sizeof(struct smb_rqst));
4789 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
4791 cifs_small_buf_release(req);
4793 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
4794 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
4795 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4796 tcon->ses->Suid, rc);
4803 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4804 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4805 const __u64 length, const __u64 offset, const __u32 lock_flags,
4808 struct smb2_lock_element lock;
4810 lock.Offset = cpu_to_le64(offset);
4811 lock.Length = cpu_to_le64(length);
4812 lock.Flags = cpu_to_le32(lock_flags);
4813 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
4814 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
4816 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
4820 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
4821 __u8 *lease_key, const __le32 lease_state)
4823 struct smb_rqst rqst;
4825 struct smb2_lease_ack *req = NULL;
4826 struct cifs_ses *ses = tcon->ses;
4827 int flags = CIFS_OBREAK_OP;
4828 unsigned int total_len;
4830 struct kvec rsp_iov;
4832 __u64 *please_key_high;
4833 __u64 *please_key_low;
4835 cifs_dbg(FYI, "SMB2_lease_break\n");
4836 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4841 if (smb3_encryption_required(tcon))
4842 flags |= CIFS_TRANSFORM_REQ;
4844 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4845 req->StructureSize = cpu_to_le16(36);
4848 memcpy(req->LeaseKey, lease_key, 16);
4849 req->LeaseState = lease_state;
4851 flags |= CIFS_NO_RSP_BUF;
4853 iov[0].iov_base = (char *)req;
4854 iov[0].iov_len = total_len;
4856 memset(&rqst, 0, sizeof(struct smb_rqst));
4860 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4861 cifs_small_buf_release(req);
4863 please_key_low = (__u64 *)lease_key;
4864 please_key_high = (__u64 *)(lease_key+8);
4866 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4867 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
4868 ses->Suid, *please_key_low, *please_key_high, rc);
4869 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
4871 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
4872 ses->Suid, *please_key_low, *please_key_high);