4 * Copyright (C) International Business Machines Corp., 2009, 2013
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
9 * Contains the routines for constructing the SMB2 PDUs themselves
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
32 #include <linux/kernel.h>
33 #include <linux/vfs.h>
34 #include <linux/task_io_accounting_ops.h>
35 #include <linux/uaccess.h>
36 #include <linux/uuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/xattr.h>
42 #include "cifsproto.h"
43 #include "smb2proto.h"
44 #include "cifs_unicode.h"
45 #include "cifs_debug.h"
47 #include "smb2status.h"
50 #include "cifs_spnego.h"
51 #include "smbdirect.h"
53 #ifdef CONFIG_CIFS_DFS_UPCALL
54 #include "dfs_cache.h"
58 * The following table defines the expected "StructureSize" of SMB2 requests
59 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
61 * Note that commands are defined in smb2pdu.h in le16 but the array below is
62 * indexed by command in host byte order.
64 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
65 /* SMB2_NEGOTIATE */ 36,
66 /* SMB2_SESSION_SETUP */ 25,
68 /* SMB2_TREE_CONNECT */ 9,
69 /* SMB2_TREE_DISCONNECT */ 4,
79 /* SMB2_QUERY_DIRECTORY */ 33,
80 /* SMB2_CHANGE_NOTIFY */ 32,
81 /* SMB2_QUERY_INFO */ 41,
82 /* SMB2_SET_INFO */ 33,
83 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
86 int smb3_encryption_required(const struct cifs_tcon *tcon)
90 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
91 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
94 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
100 smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
101 const struct cifs_tcon *tcon)
103 shdr->ProtocolId = SMB2_PROTO_NUMBER;
104 shdr->StructureSize = cpu_to_le16(64);
105 shdr->Command = smb2_cmd;
106 if (tcon && tcon->ses && tcon->ses->server) {
107 struct TCP_Server_Info *server = tcon->ses->server;
109 spin_lock(&server->req_lock);
110 /* Request up to 10 credits but don't go over the limit. */
111 if (server->credits >= server->max_credits)
112 shdr->CreditRequest = cpu_to_le16(0);
114 shdr->CreditRequest = cpu_to_le16(
115 min_t(int, server->max_credits -
116 server->credits, 10));
117 spin_unlock(&server->req_lock);
119 shdr->CreditRequest = cpu_to_le16(2);
121 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
126 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
127 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
128 if ((tcon->ses) && (tcon->ses->server) &&
129 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
130 shdr->CreditCharge = cpu_to_le16(1);
131 /* else CreditCharge MBZ */
133 shdr->TreeId = tcon->tid;
134 /* Uid is not converted */
136 shdr->SessionId = tcon->ses->Suid;
139 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
140 * to pass the path on the Open SMB prefixed by \\server\share.
141 * Not sure when we would need to do the augmented path (if ever) and
142 * setting this flag breaks the SMB2 open operation since it is
143 * illegal to send an empty path name (without \\server\share prefix)
144 * when the DFS flag is set in the SMB open header. We could
145 * consider setting the flag on all operations other than open
146 * but it is safer to net set it for now.
148 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
149 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
151 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
152 !smb3_encryption_required(tcon))
153 shdr->Flags |= SMB2_FLAGS_SIGNED;
158 #ifdef CONFIG_CIFS_DFS_UPCALL
159 static int __smb2_reconnect(const struct nls_table *nlsc,
160 struct cifs_tcon *tcon)
163 struct dfs_cache_tgt_list tl;
164 struct dfs_cache_tgt_iterator *it = NULL;
166 const char *tcp_host;
168 const char *dfs_host;
171 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
176 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
177 tcon->ses->server->hostname);
178 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
182 if (!tcon->dfs_path) {
183 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
187 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl);
191 extract_unc_hostname(tcon->ses->server->hostname, &tcp_host,
194 for (it = dfs_cache_get_tgt_iterator(&tl); it;
195 it = dfs_cache_get_next_tgt(&tl, it)) {
196 const char *tgt = dfs_cache_get_tgt_name(it);
198 extract_unc_hostname(tgt, &dfs_host, &dfs_host_len);
200 if (dfs_host_len != tcp_host_len
201 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
202 cifs_dbg(FYI, "%s: skipping %.*s, doesn't match %.*s",
204 (int)dfs_host_len, dfs_host,
205 (int)tcp_host_len, tcp_host);
209 scnprintf(tree, MAX_TREE_SIZE, "\\%s", tgt);
211 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
220 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1,
225 dfs_cache_free_tgts(&tl);
231 static inline int __smb2_reconnect(const struct nls_table *nlsc,
232 struct cifs_tcon *tcon)
234 return SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
239 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
242 struct nls_table *nls_codepage;
243 struct cifs_ses *ses;
244 struct TCP_Server_Info *server;
248 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
249 * check for tcp and smb session status done differently
250 * for those three - in the calling routine.
255 if (smb2_command == SMB2_TREE_CONNECT)
258 if (tcon->tidStatus == CifsExiting) {
260 * only tree disconnect, open, and write,
261 * (and ulogoff which does not have tcon)
262 * are allowed as we start force umount.
264 if ((smb2_command != SMB2_WRITE) &&
265 (smb2_command != SMB2_CREATE) &&
266 (smb2_command != SMB2_TREE_DISCONNECT)) {
267 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
272 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
273 (!tcon->ses->server))
277 server = ses->server;
279 retries = server->nr_targets;
282 * Give demultiplex thread up to 10 seconds to each target available for
283 * reconnect -- should be greater than cifs socket timeout which is 7
286 while (server->tcpStatus == CifsNeedReconnect) {
288 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
289 * here since they are implicitly done when session drops.
291 switch (smb2_command) {
293 * BB Should we keep oplock break and add flush to exceptions?
295 case SMB2_TREE_DISCONNECT:
298 case SMB2_OPLOCK_BREAK:
302 rc = wait_event_interruptible_timeout(server->response_q,
303 (server->tcpStatus != CifsNeedReconnect),
306 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
307 " signal by the process\n", __func__);
311 /* are we still trying to reconnect? */
312 if (server->tcpStatus != CifsNeedReconnect)
315 if (retries && --retries)
319 * on "soft" mounts we wait once. Hard mounts keep
320 * retrying until process is killed or server comes
324 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
327 retries = server->nr_targets;
330 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
333 nls_codepage = load_nls_default();
336 * need to prevent multiple threads trying to simultaneously reconnect
337 * the same SMB session
339 mutex_lock(&tcon->ses->session_mutex);
342 * Recheck after acquire mutex. If another thread is negotiating
343 * and the server never sends an answer the socket will be closed
344 * and tcpStatus set to reconnect.
346 if (server->tcpStatus == CifsNeedReconnect) {
348 mutex_unlock(&tcon->ses->session_mutex);
352 rc = cifs_negotiate_protocol(0, tcon->ses);
353 if (!rc && tcon->ses->need_reconnect) {
354 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
355 if ((rc == -EACCES) && !tcon->retry) {
357 mutex_unlock(&tcon->ses->session_mutex);
361 if (rc || !tcon->need_reconnect) {
362 mutex_unlock(&tcon->ses->session_mutex);
366 cifs_mark_open_files_invalid(tcon);
367 if (tcon->use_persistent)
368 tcon->need_reopen_files = true;
370 rc = __smb2_reconnect(nls_codepage, tcon);
371 mutex_unlock(&tcon->ses->session_mutex);
373 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
375 /* If sess reconnected but tcon didn't, something strange ... */
376 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
380 if (smb2_command != SMB2_INTERNAL_CMD)
381 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
383 atomic_inc(&tconInfoReconnectCount);
386 * Check if handle based operation so we know whether we can continue
387 * or not without returning to caller to reset file handle.
390 * BB Is flush done by server on drop of tcp session? Should we special
391 * case it and skip above?
393 switch (smb2_command) {
399 case SMB2_QUERY_DIRECTORY:
400 case SMB2_CHANGE_NOTIFY:
401 case SMB2_QUERY_INFO:
406 unload_nls(nls_codepage);
411 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
412 unsigned int *total_len)
414 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
415 /* lookup word count ie StructureSize from table */
416 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
419 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
420 * largest operations (Create)
424 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
425 spdu->StructureSize2 = cpu_to_le16(parmsize);
427 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
431 * Allocate and return pointer to an SMB request hdr, and set basic
432 * SMB information in the SMB header. If the return code is zero, this
433 * function must have filled in request_buf pointer.
435 static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
436 void **request_buf, unsigned int *total_len)
438 /* BB eventually switch this to SMB2 specific small buf size */
439 if (smb2_command == SMB2_SET_INFO)
440 *request_buf = cifs_buf_get();
442 *request_buf = cifs_small_buf_get();
443 if (*request_buf == NULL) {
444 /* BB should we add a retry in here if not a writepage? */
448 fill_small_buf(smb2_command, tcon,
449 (struct smb2_sync_hdr *)(*request_buf),
453 uint16_t com_code = le16_to_cpu(smb2_command);
454 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
455 cifs_stats_inc(&tcon->num_smbs_sent);
461 static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
462 void **request_buf, unsigned int *total_len)
466 rc = smb2_reconnect(smb2_command, tcon);
470 return __smb2_plain_req_init(smb2_command, tcon, request_buf,
474 static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
475 void **request_buf, unsigned int *total_len)
477 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
478 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
479 return __smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf,
482 return smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf, total_len);
485 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
488 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
490 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
491 pneg_ctxt->DataLength = cpu_to_le16(38);
492 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
493 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
494 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
495 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
499 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
501 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
502 pneg_ctxt->DataLength =
503 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
504 - sizeof(struct smb2_neg_context));
505 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
506 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
507 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
508 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
512 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
514 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
515 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + two ciphers */
516 pneg_ctxt->CipherCount = cpu_to_le16(2);
517 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
518 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
522 build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
524 struct nls_table *cp = load_nls_default();
526 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
528 /* copy up to max of first 100 bytes of server name to NetName field */
529 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
530 /* context size is DataLength + minimal smb2_neg_context */
531 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
532 sizeof(struct smb2_neg_context), 8) * 8;
536 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
538 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
539 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
540 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
541 pneg_ctxt->Name[0] = 0x93;
542 pneg_ctxt->Name[1] = 0xAD;
543 pneg_ctxt->Name[2] = 0x25;
544 pneg_ctxt->Name[3] = 0x50;
545 pneg_ctxt->Name[4] = 0x9C;
546 pneg_ctxt->Name[5] = 0xB4;
547 pneg_ctxt->Name[6] = 0x11;
548 pneg_ctxt->Name[7] = 0xE7;
549 pneg_ctxt->Name[8] = 0xB4;
550 pneg_ctxt->Name[9] = 0x23;
551 pneg_ctxt->Name[10] = 0x83;
552 pneg_ctxt->Name[11] = 0xDE;
553 pneg_ctxt->Name[12] = 0x96;
554 pneg_ctxt->Name[13] = 0x8B;
555 pneg_ctxt->Name[14] = 0xCD;
556 pneg_ctxt->Name[15] = 0x7C;
560 assemble_neg_contexts(struct smb2_negotiate_req *req,
561 struct TCP_Server_Info *server, unsigned int *total_len)
563 char *pneg_ctxt = (char *)req;
564 unsigned int ctxt_len;
566 if (*total_len > 200) {
567 /* In case length corrupted don't want to overrun smb buffer */
568 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
573 * round up total_len of fixed part of SMB3 negotiate request to 8
574 * byte boundary before adding negotiate contexts
576 *total_len = roundup(*total_len, 8);
578 pneg_ctxt = (*total_len) + (char *)req;
579 req->NegotiateContextOffset = cpu_to_le32(*total_len);
581 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
582 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
583 *total_len += ctxt_len;
584 pneg_ctxt += ctxt_len;
586 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
587 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
588 *total_len += ctxt_len;
589 pneg_ctxt += ctxt_len;
591 if (server->compress_algorithm) {
592 build_compression_ctxt((struct smb2_compression_capabilities_context *)
594 ctxt_len = DIV_ROUND_UP(
595 sizeof(struct smb2_compression_capabilities_context),
597 *total_len += ctxt_len;
598 pneg_ctxt += ctxt_len;
599 req->NegotiateContextCount = cpu_to_le16(5);
601 req->NegotiateContextCount = cpu_to_le16(4);
603 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
605 *total_len += ctxt_len;
606 pneg_ctxt += ctxt_len;
608 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
609 *total_len += sizeof(struct smb2_posix_neg_context);
612 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
614 unsigned int len = le16_to_cpu(ctxt->DataLength);
616 /* If invalid preauth context warn but use what we requested, SHA-512 */
617 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
618 printk_once(KERN_WARNING "server sent bad preauth context\n");
621 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
622 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
623 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
624 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
627 static void decode_compress_ctx(struct TCP_Server_Info *server,
628 struct smb2_compression_capabilities_context *ctxt)
630 unsigned int len = le16_to_cpu(ctxt->DataLength);
632 /* sizeof compress context is a one element compression capbility struct */
634 printk_once(KERN_WARNING "server sent bad compression cntxt\n");
637 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
638 printk_once(KERN_WARNING "illegal SMB3 compress algorithm count\n");
641 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
642 printk_once(KERN_WARNING "unknown compression algorithm\n");
645 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
648 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
649 struct smb2_encryption_neg_context *ctxt)
651 unsigned int len = le16_to_cpu(ctxt->DataLength);
653 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
654 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
655 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
659 if (le16_to_cpu(ctxt->CipherCount) != 1) {
660 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
663 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
664 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
665 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
666 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
669 server->cipher_type = ctxt->Ciphers[0];
670 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
674 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
675 struct TCP_Server_Info *server,
676 unsigned int len_of_smb)
678 struct smb2_neg_context *pctx;
679 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
680 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
681 unsigned int len_of_ctxts, i;
684 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
685 if (len_of_smb <= offset) {
686 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
690 len_of_ctxts = len_of_smb - offset;
692 for (i = 0; i < ctxt_cnt; i++) {
694 /* check that offset is not beyond end of SMB */
695 if (len_of_ctxts == 0)
698 if (len_of_ctxts < sizeof(struct smb2_neg_context))
701 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
702 clen = le16_to_cpu(pctx->DataLength);
703 if (clen > len_of_ctxts)
706 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
707 decode_preauth_context(
708 (struct smb2_preauth_neg_context *)pctx);
709 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
710 rc = decode_encrypt_ctx(server,
711 (struct smb2_encryption_neg_context *)pctx);
712 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
713 decode_compress_ctx(server,
714 (struct smb2_compression_capabilities_context *)pctx);
715 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
716 server->posix_ext_supported = true;
718 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
719 le16_to_cpu(pctx->ContextType));
723 /* offsets must be 8 byte aligned */
724 clen = (clen + 7) & ~0x7;
725 offset += clen + sizeof(struct smb2_neg_context);
726 len_of_ctxts -= clen;
731 static struct create_posix *
732 create_posix_buf(umode_t mode)
734 struct create_posix *buf;
736 buf = kzalloc(sizeof(struct create_posix),
741 buf->ccontext.DataOffset =
742 cpu_to_le16(offsetof(struct create_posix, Mode));
743 buf->ccontext.DataLength = cpu_to_le32(4);
744 buf->ccontext.NameOffset =
745 cpu_to_le16(offsetof(struct create_posix, Name));
746 buf->ccontext.NameLength = cpu_to_le16(16);
748 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
759 buf->Name[10] = 0x83;
760 buf->Name[11] = 0xDE;
761 buf->Name[12] = 0x96;
762 buf->Name[13] = 0x8B;
763 buf->Name[14] = 0xCD;
764 buf->Name[15] = 0x7C;
765 buf->Mode = cpu_to_le32(mode);
766 cifs_dbg(FYI, "mode on posix create 0%o", mode);
771 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
773 struct smb2_create_req *req = iov[0].iov_base;
774 unsigned int num = *num_iovec;
776 iov[num].iov_base = create_posix_buf(mode);
777 if (mode == ACL_NO_MODE)
778 cifs_dbg(FYI, "illegal mode\n");
779 if (iov[num].iov_base == NULL)
781 iov[num].iov_len = sizeof(struct create_posix);
782 if (!req->CreateContextsOffset)
783 req->CreateContextsOffset = cpu_to_le32(
784 sizeof(struct smb2_create_req) +
785 iov[num - 1].iov_len);
786 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
787 *num_iovec = num + 1;
794 * SMB2 Worker functions follow:
796 * The general structure of the worker functions is:
797 * 1) Call smb2_init (assembles SMB2 header)
798 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
799 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
800 * 4) Decode SMB2 command specific fields in the fixed length area
801 * 5) Decode variable length data area (if any for this SMB2 command type)
802 * 6) Call free smb buffer
808 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
810 struct smb_rqst rqst;
811 struct smb2_negotiate_req *req;
812 struct smb2_negotiate_rsp *rsp;
817 struct TCP_Server_Info *server = ses->server;
818 int blob_offset, blob_length;
820 int flags = CIFS_NEG_OP;
821 unsigned int total_len;
823 cifs_dbg(FYI, "Negotiate protocol\n");
826 WARN(1, "%s: server is NULL!\n", __func__);
830 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
834 req->sync_hdr.SessionId = 0;
836 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
837 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
839 if (strcmp(ses->server->vals->version_string,
840 SMB3ANY_VERSION_STRING) == 0) {
841 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
842 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
843 req->DialectCount = cpu_to_le16(2);
845 } else if (strcmp(server->vals->version_string,
846 SMBDEFAULT_VERSION_STRING) == 0) {
847 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
848 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
849 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
850 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
851 req->DialectCount = cpu_to_le16(4);
854 /* otherwise send specific dialect */
855 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
856 req->DialectCount = cpu_to_le16(1);
860 /* only one of SMB2 signing flags may be set in SMB2 request */
862 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
863 else if (global_secflags & CIFSSEC_MAY_SIGN)
864 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
866 req->SecurityMode = 0;
868 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
870 /* ClientGUID must be zero for SMB2.02 dialect */
871 if (server->vals->protocol_id == SMB20_PROT_ID)
872 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
874 memcpy(req->ClientGUID, server->client_guid,
875 SMB2_CLIENT_GUID_SIZE);
876 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
877 (strcmp(server->vals->version_string,
878 SMBDEFAULT_VERSION_STRING) == 0))
879 assemble_neg_contexts(req, server, &total_len);
881 iov[0].iov_base = (char *)req;
882 iov[0].iov_len = total_len;
884 memset(&rqst, 0, sizeof(struct smb_rqst));
888 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
889 cifs_small_buf_release(req);
890 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
892 * No tcon so can't do
893 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
895 if (rc == -EOPNOTSUPP) {
896 cifs_server_dbg(VFS, "Dialect not supported by server. Consider "
897 "specifying vers=1.0 or vers=2.0 on mount for accessing"
903 if (strcmp(server->vals->version_string,
904 SMB3ANY_VERSION_STRING) == 0) {
905 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
907 "SMB2 dialect returned but not requested\n");
909 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
911 "SMB2.1 dialect returned but not requested\n");
914 } else if (strcmp(server->vals->version_string,
915 SMBDEFAULT_VERSION_STRING) == 0) {
916 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
918 "SMB2 dialect returned but not requested\n");
920 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
921 /* ops set to 3.0 by default for default so update */
922 server->ops = &smb21_operations;
923 server->vals = &smb21_values;
924 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
925 server->ops = &smb311_operations;
926 server->vals = &smb311_values;
928 } else if (le16_to_cpu(rsp->DialectRevision) !=
929 server->vals->protocol_id) {
930 /* if requested single dialect ensure returned dialect matched */
931 cifs_server_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
932 le16_to_cpu(rsp->DialectRevision));
936 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
938 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
939 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
940 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
941 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
942 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
943 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
944 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
945 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
946 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
947 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
949 cifs_server_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
950 le16_to_cpu(rsp->DialectRevision));
954 server->dialect = le16_to_cpu(rsp->DialectRevision);
957 * Keep a copy of the hash after negprot. This hash will be
958 * the starting hash value for all sessions made from this
961 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
962 SMB2_PREAUTH_HASH_SIZE);
964 /* SMB2 only has an extended negflavor */
965 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
966 /* set it to the maximum buffer size value we can send with 1 credit */
967 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
968 SMB2_MAX_BUFFER_SIZE);
969 server->max_read = le32_to_cpu(rsp->MaxReadSize);
970 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
971 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
972 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
973 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
975 server->capabilities = le32_to_cpu(rsp->Capabilities);
977 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
979 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
980 (struct smb2_sync_hdr *)rsp);
982 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
984 * ses->sectype = RawNTLMSSP;
985 * but for time being this is our only auth choice so doesn't matter.
986 * We just found a server which sets blob length to zero expecting raw.
988 if (blob_length == 0) {
989 cifs_dbg(FYI, "missing security blob on negprot\n");
990 server->sec_ntlmssp = true;
993 rc = cifs_enable_signing(server, ses->sign);
997 rc = decode_negTokenInit(security_blob, blob_length, server);
1004 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1005 if (rsp->NegotiateContextCount)
1006 rc = smb311_decode_neg_context(rsp, server,
1009 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
1012 free_rsp_buf(resp_buftype, rsp);
1016 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1019 struct validate_negotiate_info_req *pneg_inbuf;
1020 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
1022 u32 inbuflen; /* max of 4 dialects */
1023 struct TCP_Server_Info *server = tcon->ses->server;
1025 cifs_dbg(FYI, "validate negotiate\n");
1027 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1028 if (server->dialect == SMB311_PROT_ID)
1032 * validation ioctl must be signed, so no point sending this if we
1033 * can not sign it (ie are not known user). Even if signing is not
1034 * required (enabled but not negotiated), in those cases we selectively
1035 * sign just this, the first and only signed request on a connection.
1036 * Having validation of negotiate info helps reduce attack vectors.
1038 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1039 return 0; /* validation requires signing */
1041 if (tcon->ses->user_name == NULL) {
1042 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1043 return 0; /* validation requires signing */
1046 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1047 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1049 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1053 pneg_inbuf->Capabilities =
1054 cpu_to_le32(server->vals->req_capabilities);
1055 memcpy(pneg_inbuf->Guid, server->client_guid,
1056 SMB2_CLIENT_GUID_SIZE);
1058 if (tcon->ses->sign)
1059 pneg_inbuf->SecurityMode =
1060 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1061 else if (global_secflags & CIFSSEC_MAY_SIGN)
1062 pneg_inbuf->SecurityMode =
1063 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1065 pneg_inbuf->SecurityMode = 0;
1068 if (strcmp(server->vals->version_string,
1069 SMB3ANY_VERSION_STRING) == 0) {
1070 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1071 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1072 pneg_inbuf->DialectCount = cpu_to_le16(2);
1073 /* structure is big enough for 3 dialects, sending only 2 */
1074 inbuflen = sizeof(*pneg_inbuf) -
1075 (2 * sizeof(pneg_inbuf->Dialects[0]));
1076 } else if (strcmp(server->vals->version_string,
1077 SMBDEFAULT_VERSION_STRING) == 0) {
1078 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1079 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1080 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1081 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1082 pneg_inbuf->DialectCount = cpu_to_le16(4);
1083 /* structure is big enough for 3 dialects */
1084 inbuflen = sizeof(*pneg_inbuf);
1086 /* otherwise specific dialect was requested */
1087 pneg_inbuf->Dialects[0] =
1088 cpu_to_le16(server->vals->protocol_id);
1089 pneg_inbuf->DialectCount = cpu_to_le16(1);
1090 /* structure is big enough for 3 dialects, sending only 1 */
1091 inbuflen = sizeof(*pneg_inbuf) -
1092 sizeof(pneg_inbuf->Dialects[0]) * 2;
1095 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1096 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
1097 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1098 (char **)&pneg_rsp, &rsplen);
1099 if (rc == -EOPNOTSUPP) {
1101 * Old Windows versions or Netapp SMB server can return
1102 * not supported error. Client should accept it.
1104 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1106 goto out_free_inbuf;
1107 } else if (rc != 0) {
1108 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
1110 goto out_free_inbuf;
1114 if (rsplen != sizeof(*pneg_rsp)) {
1115 cifs_tcon_dbg(VFS, "invalid protocol negotiate response size: %d\n",
1118 /* relax check since Mac returns max bufsize allowed on ioctl */
1119 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1123 /* check validate negotiate info response matches what we got earlier */
1124 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1127 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1130 /* do not validate server guid because not saved at negprot time yet */
1132 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1133 SMB2_LARGE_FILES) != server->capabilities)
1136 /* validate negotiate successful */
1138 cifs_dbg(FYI, "validate negotiate info successful\n");
1142 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1151 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1153 switch (requested) {
1160 if (server->sec_ntlmssp &&
1161 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1163 if ((server->sec_kerberos || server->sec_mskerberos) &&
1164 (global_secflags & CIFSSEC_MAY_KRB5))
1172 struct SMB2_sess_data {
1174 struct cifs_ses *ses;
1175 struct nls_table *nls_cp;
1176 void (*func)(struct SMB2_sess_data *);
1178 u64 previous_session;
1180 /* we will send the SMB in three pieces:
1181 * a fixed length beginning part, an optional
1182 * SPNEGO blob (which can be zero length), and a
1183 * last part which will include the strings
1184 * and rest of bcc area. This allows us to avoid
1185 * a large buffer 17K allocation
1192 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1195 struct cifs_ses *ses = sess_data->ses;
1196 struct smb2_sess_setup_req *req;
1197 struct TCP_Server_Info *server = ses->server;
1198 unsigned int total_len;
1200 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1205 /* First session, not a reauthenticate */
1206 req->sync_hdr.SessionId = 0;
1208 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
1209 req->PreviousSessionId = sess_data->previous_session;
1211 req->Flags = 0; /* MBZ */
1213 /* enough to enable echos and oplocks and one max size write */
1214 req->sync_hdr.CreditRequest = cpu_to_le16(130);
1216 /* only one of SMB2 signing flags may be set in SMB2 request */
1218 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1219 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1220 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1222 req->SecurityMode = 0;
1224 #ifdef CONFIG_CIFS_DFS_UPCALL
1225 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1227 req->Capabilities = 0;
1228 #endif /* DFS_UPCALL */
1230 req->Channel = 0; /* MBZ */
1232 sess_data->iov[0].iov_base = (char *)req;
1234 sess_data->iov[0].iov_len = total_len - 1;
1236 * This variable will be used to clear the buffer
1237 * allocated above in case of any error in the calling function.
1239 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1245 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1247 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1248 sess_data->buf0_type = CIFS_NO_BUFFER;
1252 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1255 struct smb_rqst rqst;
1256 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1257 struct kvec rsp_iov = { NULL, 0 };
1259 /* Testing shows that buffer offset must be at location of Buffer[0] */
1260 req->SecurityBufferOffset =
1261 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1262 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1264 memset(&rqst, 0, sizeof(struct smb_rqst));
1265 rqst.rq_iov = sess_data->iov;
1268 /* BB add code to build os and lm fields */
1269 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1271 &sess_data->buf0_type,
1272 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
1273 cifs_small_buf_release(sess_data->iov[0].iov_base);
1274 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1280 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1283 struct cifs_ses *ses = sess_data->ses;
1285 mutex_lock(&ses->server->srv_mutex);
1286 if (ses->server->ops->generate_signingkey) {
1287 rc = ses->server->ops->generate_signingkey(ses);
1290 "SMB3 session key generation failed\n");
1291 mutex_unlock(&ses->server->srv_mutex);
1295 if (!ses->server->session_estab) {
1296 ses->server->sequence_number = 0x2;
1297 ses->server->session_estab = true;
1299 mutex_unlock(&ses->server->srv_mutex);
1301 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1302 spin_lock(&GlobalMid_Lock);
1303 ses->status = CifsGood;
1304 ses->need_reconnect = false;
1305 spin_unlock(&GlobalMid_Lock);
1309 #ifdef CONFIG_CIFS_UPCALL
1311 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1314 struct cifs_ses *ses = sess_data->ses;
1315 struct cifs_spnego_msg *msg;
1316 struct key *spnego_key = NULL;
1317 struct smb2_sess_setup_rsp *rsp = NULL;
1319 rc = SMB2_sess_alloc_buffer(sess_data);
1323 spnego_key = cifs_get_spnego_key(ses);
1324 if (IS_ERR(spnego_key)) {
1325 rc = PTR_ERR(spnego_key);
1330 msg = spnego_key->payload.data[0];
1332 * check version field to make sure that cifs.upcall is
1333 * sending us a response in an expected form
1335 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1337 "bad cifs.upcall version. Expected %d got %d",
1338 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1340 goto out_put_spnego_key;
1343 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1345 if (!ses->auth_key.response) {
1347 "Kerberos can't allocate (%u bytes) memory",
1350 goto out_put_spnego_key;
1352 ses->auth_key.len = msg->sesskey_len;
1354 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1355 sess_data->iov[1].iov_len = msg->secblob_len;
1357 rc = SMB2_sess_sendreceive(sess_data);
1359 goto out_put_spnego_key;
1361 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1362 ses->Suid = rsp->sync_hdr.SessionId;
1364 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1366 rc = SMB2_sess_establish_session(sess_data);
1368 key_invalidate(spnego_key);
1369 key_put(spnego_key);
1371 sess_data->result = rc;
1372 sess_data->func = NULL;
1373 SMB2_sess_free_buffer(sess_data);
1377 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1379 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1380 sess_data->result = -EOPNOTSUPP;
1381 sess_data->func = NULL;
1386 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1389 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1392 struct cifs_ses *ses = sess_data->ses;
1393 struct smb2_sess_setup_rsp *rsp = NULL;
1394 char *ntlmssp_blob = NULL;
1395 bool use_spnego = false; /* else use raw ntlmssp */
1396 u16 blob_length = 0;
1399 * If memory allocation is successful, caller of this function
1402 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1403 if (!ses->ntlmssp) {
1407 ses->ntlmssp->sesskey_per_smbsess = true;
1409 rc = SMB2_sess_alloc_buffer(sess_data);
1413 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1415 if (ntlmssp_blob == NULL) {
1420 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1422 /* BB eventually need to add this */
1423 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1427 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1428 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1430 sess_data->iov[1].iov_base = ntlmssp_blob;
1431 sess_data->iov[1].iov_len = blob_length;
1433 rc = SMB2_sess_sendreceive(sess_data);
1434 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1436 /* If true, rc here is expected and not an error */
1437 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1438 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1444 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1445 le16_to_cpu(rsp->SecurityBufferOffset)) {
1446 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1447 le16_to_cpu(rsp->SecurityBufferOffset));
1451 rc = decode_ntlmssp_challenge(rsp->Buffer,
1452 le16_to_cpu(rsp->SecurityBufferLength), ses);
1456 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1459 ses->Suid = rsp->sync_hdr.SessionId;
1460 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1463 kfree(ntlmssp_blob);
1464 SMB2_sess_free_buffer(sess_data);
1466 sess_data->result = 0;
1467 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1471 kfree(ses->ntlmssp);
1472 ses->ntlmssp = NULL;
1473 sess_data->result = rc;
1474 sess_data->func = NULL;
1478 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1481 struct cifs_ses *ses = sess_data->ses;
1482 struct smb2_sess_setup_req *req;
1483 struct smb2_sess_setup_rsp *rsp = NULL;
1484 unsigned char *ntlmssp_blob = NULL;
1485 bool use_spnego = false; /* else use raw ntlmssp */
1486 u16 blob_length = 0;
1488 rc = SMB2_sess_alloc_buffer(sess_data);
1492 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1493 req->sync_hdr.SessionId = ses->Suid;
1495 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1498 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1503 /* BB eventually need to add this */
1504 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1508 sess_data->iov[1].iov_base = ntlmssp_blob;
1509 sess_data->iov[1].iov_len = blob_length;
1511 rc = SMB2_sess_sendreceive(sess_data);
1515 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1517 ses->Suid = rsp->sync_hdr.SessionId;
1518 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1520 rc = SMB2_sess_establish_session(sess_data);
1522 kfree(ntlmssp_blob);
1523 SMB2_sess_free_buffer(sess_data);
1524 kfree(ses->ntlmssp);
1525 ses->ntlmssp = NULL;
1526 sess_data->result = rc;
1527 sess_data->func = NULL;
1531 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1535 type = smb2_select_sectype(ses->server, ses->sectype);
1536 cifs_dbg(FYI, "sess setup type %d\n", type);
1537 if (type == Unspecified) {
1539 "Unable to select appropriate authentication method!");
1545 sess_data->func = SMB2_auth_kerberos;
1548 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1551 cifs_dbg(VFS, "secType %d not supported!\n", type);
1559 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1560 const struct nls_table *nls_cp)
1563 struct TCP_Server_Info *server = ses->server;
1564 struct SMB2_sess_data *sess_data;
1566 cifs_dbg(FYI, "Session Setup\n");
1569 WARN(1, "%s: server is NULL!\n", __func__);
1573 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1577 rc = SMB2_select_sec(ses, sess_data);
1580 sess_data->xid = xid;
1581 sess_data->ses = ses;
1582 sess_data->buf0_type = CIFS_NO_BUFFER;
1583 sess_data->nls_cp = (struct nls_table *) nls_cp;
1584 sess_data->previous_session = ses->Suid;
1587 * Initialize the session hash with the server one.
1589 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1590 SMB2_PREAUTH_HASH_SIZE);
1592 while (sess_data->func)
1593 sess_data->func(sess_data);
1595 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1596 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1597 rc = sess_data->result;
1604 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1606 struct smb_rqst rqst;
1607 struct smb2_logoff_req *req; /* response is also trivial struct */
1609 struct TCP_Server_Info *server;
1611 unsigned int total_len;
1613 struct kvec rsp_iov;
1616 cifs_dbg(FYI, "disconnect session %p\n", ses);
1618 if (ses && (ses->server))
1619 server = ses->server;
1623 /* no need to send SMB logoff if uid already closed due to reconnect */
1624 if (ses->need_reconnect)
1625 goto smb2_session_already_dead;
1627 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
1631 /* since no tcon, smb2_init can not do this, so do here */
1632 req->sync_hdr.SessionId = ses->Suid;
1634 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1635 flags |= CIFS_TRANSFORM_REQ;
1636 else if (server->sign)
1637 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1639 flags |= CIFS_NO_RSP_BUF;
1641 iov[0].iov_base = (char *)req;
1642 iov[0].iov_len = total_len;
1644 memset(&rqst, 0, sizeof(struct smb_rqst));
1648 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1649 cifs_small_buf_release(req);
1651 * No tcon so can't do
1652 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1655 smb2_session_already_dead:
1659 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1661 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1664 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1666 /* These are similar values to what Windows uses */
1667 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1669 tcon->max_chunks = 256;
1670 tcon->max_bytes_chunk = 1048576;
1671 tcon->max_bytes_copy = 16777216;
1675 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1676 struct cifs_tcon *tcon, const struct nls_table *cp)
1678 struct smb_rqst rqst;
1679 struct smb2_tree_connect_req *req;
1680 struct smb2_tree_connect_rsp *rsp = NULL;
1682 struct kvec rsp_iov = { NULL, 0 };
1686 __le16 *unc_path = NULL;
1688 unsigned int total_len;
1689 struct TCP_Server_Info *server = ses->server;
1691 cifs_dbg(FYI, "TCON\n");
1693 if (!server || !tree)
1696 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1697 if (unc_path == NULL)
1700 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1702 if (unc_path_len < 2) {
1707 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1709 atomic_set(&tcon->num_remote_opens, 0);
1710 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1717 if (smb3_encryption_required(tcon))
1718 flags |= CIFS_TRANSFORM_REQ;
1720 iov[0].iov_base = (char *)req;
1722 iov[0].iov_len = total_len - 1;
1724 /* Testing shows that buffer offset must be at location of Buffer[0] */
1725 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1727 req->PathLength = cpu_to_le16(unc_path_len - 2);
1728 iov[1].iov_base = unc_path;
1729 iov[1].iov_len = unc_path_len;
1732 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1733 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1734 * (Samba servers don't always set the flag so also check if null user)
1736 if ((server->dialect == SMB311_PROT_ID) &&
1737 !smb3_encryption_required(tcon) &&
1738 !(ses->session_flags &
1739 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1740 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1741 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1743 memset(&rqst, 0, sizeof(struct smb_rqst));
1747 /* Need 64 for max size write so ask for more in case not there yet */
1748 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1750 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
1751 cifs_small_buf_release(req);
1752 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1753 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1756 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1757 tcon->need_reconnect = true;
1759 goto tcon_error_exit;
1762 switch (rsp->ShareType) {
1763 case SMB2_SHARE_TYPE_DISK:
1764 cifs_dbg(FYI, "connection to disk share\n");
1766 case SMB2_SHARE_TYPE_PIPE:
1768 cifs_dbg(FYI, "connection to pipe share\n");
1770 case SMB2_SHARE_TYPE_PRINT:
1772 cifs_dbg(FYI, "connection to printer\n");
1775 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1777 goto tcon_error_exit;
1780 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1781 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1782 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1783 tcon->tidStatus = CifsGood;
1784 tcon->need_reconnect = false;
1785 tcon->tid = rsp->sync_hdr.TreeId;
1786 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1788 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1789 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1790 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
1793 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1794 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
1796 init_copy_chunk_defaults(tcon);
1797 if (server->ops->validate_negotiate)
1798 rc = server->ops->validate_negotiate(xid, tcon);
1801 free_rsp_buf(resp_buftype, rsp);
1806 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1807 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1813 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1815 struct smb_rqst rqst;
1816 struct smb2_tree_disconnect_req *req; /* response is trivial */
1818 struct cifs_ses *ses = tcon->ses;
1820 unsigned int total_len;
1822 struct kvec rsp_iov;
1825 cifs_dbg(FYI, "Tree Disconnect\n");
1827 if (!ses || !(ses->server))
1830 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1833 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1838 if (smb3_encryption_required(tcon))
1839 flags |= CIFS_TRANSFORM_REQ;
1841 flags |= CIFS_NO_RSP_BUF;
1843 iov[0].iov_base = (char *)req;
1844 iov[0].iov_len = total_len;
1846 memset(&rqst, 0, sizeof(struct smb_rqst));
1850 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1851 cifs_small_buf_release(req);
1853 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1859 static struct create_durable *
1860 create_durable_buf(void)
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 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1882 static struct create_durable *
1883 create_reconnect_durable_buf(struct cifs_fid *fid)
1885 struct create_durable *buf;
1887 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1891 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1892 (struct create_durable, Data));
1893 buf->ccontext.DataLength = cpu_to_le32(16);
1894 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1895 (struct create_durable, Name));
1896 buf->ccontext.NameLength = cpu_to_le16(4);
1897 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1898 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1899 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1908 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1910 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1912 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1913 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1914 buf->IndexNumber = pdisk_id->DiskFileId;
1918 smb2_parse_contexts(struct TCP_Server_Info *server,
1919 struct smb2_create_rsp *rsp,
1920 unsigned int *epoch, char *lease_key, __u8 *oplock,
1921 struct smb2_file_all_info *buf)
1924 struct create_context *cc;
1926 unsigned int remaining;
1930 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
1931 remaining = le32_to_cpu(rsp->CreateContextsLength);
1932 cc = (struct create_context *)data_offset;
1934 /* Initialize inode number to 0 in case no valid data in qfid context */
1936 buf->IndexNumber = 0;
1938 while (remaining >= sizeof(struct create_context)) {
1939 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1940 if (le16_to_cpu(cc->NameLength) == 4 &&
1941 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
1942 *oplock = server->ops->parse_lease_buf(cc, epoch,
1944 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
1945 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
1946 parse_query_id_ctxt(cc, buf);
1948 next = le32_to_cpu(cc->Next);
1952 cc = (struct create_context *)((char *)cc + next);
1955 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
1956 *oplock = rsp->OplockLevel;
1962 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1963 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
1965 struct smb2_create_req *req = iov[0].iov_base;
1966 unsigned int num = *num_iovec;
1968 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
1969 if (iov[num].iov_base == NULL)
1971 iov[num].iov_len = server->vals->create_lease_size;
1972 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1973 if (!req->CreateContextsOffset)
1974 req->CreateContextsOffset = cpu_to_le32(
1975 sizeof(struct smb2_create_req) +
1976 iov[num - 1].iov_len);
1977 le32_add_cpu(&req->CreateContextsLength,
1978 server->vals->create_lease_size);
1979 *num_iovec = num + 1;
1983 static struct create_durable_v2 *
1984 create_durable_v2_buf(struct cifs_open_parms *oparms)
1986 struct cifs_fid *pfid = oparms->fid;
1987 struct create_durable_v2 *buf;
1989 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1993 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1994 (struct create_durable_v2, dcontext));
1995 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1996 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1997 (struct create_durable_v2, Name));
1998 buf->ccontext.NameLength = cpu_to_le16(4);
2001 * NB: Handle timeout defaults to 0, which allows server to choose
2002 * (most servers default to 120 seconds) and most clients default to 0.
2003 * This can be overridden at mount ("handletimeout=") if the user wants
2004 * a different persistent (or resilient) handle timeout for all opens
2005 * opens on a particular SMB3 mount.
2007 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2008 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2009 generate_random_uuid(buf->dcontext.CreateGuid);
2010 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2012 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2020 static struct create_durable_handle_reconnect_v2 *
2021 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2023 struct create_durable_handle_reconnect_v2 *buf;
2025 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2030 buf->ccontext.DataOffset =
2031 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2033 buf->ccontext.DataLength =
2034 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2035 buf->ccontext.NameOffset =
2036 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2038 buf->ccontext.NameLength = cpu_to_le16(4);
2040 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2041 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2042 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2043 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2045 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2054 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2055 struct cifs_open_parms *oparms)
2057 struct smb2_create_req *req = iov[0].iov_base;
2058 unsigned int num = *num_iovec;
2060 iov[num].iov_base = create_durable_v2_buf(oparms);
2061 if (iov[num].iov_base == NULL)
2063 iov[num].iov_len = sizeof(struct create_durable_v2);
2064 if (!req->CreateContextsOffset)
2065 req->CreateContextsOffset =
2066 cpu_to_le32(sizeof(struct smb2_create_req) +
2068 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
2069 *num_iovec = num + 1;
2074 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2075 struct cifs_open_parms *oparms)
2077 struct smb2_create_req *req = iov[0].iov_base;
2078 unsigned int num = *num_iovec;
2080 /* indicate that we don't need to relock the file */
2081 oparms->reconnect = false;
2083 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2084 if (iov[num].iov_base == NULL)
2086 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2087 if (!req->CreateContextsOffset)
2088 req->CreateContextsOffset =
2089 cpu_to_le32(sizeof(struct smb2_create_req) +
2091 le32_add_cpu(&req->CreateContextsLength,
2092 sizeof(struct create_durable_handle_reconnect_v2));
2093 *num_iovec = num + 1;
2098 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2099 struct cifs_open_parms *oparms, bool use_persistent)
2101 struct smb2_create_req *req = iov[0].iov_base;
2102 unsigned int num = *num_iovec;
2104 if (use_persistent) {
2105 if (oparms->reconnect)
2106 return add_durable_reconnect_v2_context(iov, num_iovec,
2109 return add_durable_v2_context(iov, num_iovec, oparms);
2112 if (oparms->reconnect) {
2113 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2114 /* indicate that we don't need to relock the file */
2115 oparms->reconnect = false;
2117 iov[num].iov_base = create_durable_buf();
2118 if (iov[num].iov_base == NULL)
2120 iov[num].iov_len = sizeof(struct create_durable);
2121 if (!req->CreateContextsOffset)
2122 req->CreateContextsOffset =
2123 cpu_to_le32(sizeof(struct smb2_create_req) +
2125 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
2126 *num_iovec = num + 1;
2130 /* See MS-SMB2 2.2.13.2.7 */
2131 static struct crt_twarp_ctxt *
2132 create_twarp_buf(__u64 timewarp)
2134 struct crt_twarp_ctxt *buf;
2136 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2140 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2141 (struct crt_twarp_ctxt, Timestamp));
2142 buf->ccontext.DataLength = cpu_to_le32(8);
2143 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2144 (struct crt_twarp_ctxt, Name));
2145 buf->ccontext.NameLength = cpu_to_le16(4);
2146 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2151 buf->Timestamp = cpu_to_le64(timewarp);
2155 /* See MS-SMB2 2.2.13.2.7 */
2157 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2159 struct smb2_create_req *req = iov[0].iov_base;
2160 unsigned int num = *num_iovec;
2162 iov[num].iov_base = create_twarp_buf(timewarp);
2163 if (iov[num].iov_base == NULL)
2165 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2166 if (!req->CreateContextsOffset)
2167 req->CreateContextsOffset = cpu_to_le32(
2168 sizeof(struct smb2_create_req) +
2169 iov[num - 1].iov_len);
2170 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2171 *num_iovec = num + 1;
2175 static struct crt_query_id_ctxt *
2176 create_query_id_buf(void)
2178 struct crt_query_id_ctxt *buf;
2180 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2184 buf->ccontext.DataOffset = cpu_to_le16(0);
2185 buf->ccontext.DataLength = cpu_to_le32(0);
2186 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2187 (struct crt_query_id_ctxt, Name));
2188 buf->ccontext.NameLength = cpu_to_le16(4);
2189 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2197 /* See MS-SMB2 2.2.13.2.9 */
2199 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2201 struct smb2_create_req *req = iov[0].iov_base;
2202 unsigned int num = *num_iovec;
2204 iov[num].iov_base = create_query_id_buf();
2205 if (iov[num].iov_base == NULL)
2207 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2208 if (!req->CreateContextsOffset)
2209 req->CreateContextsOffset = cpu_to_le32(
2210 sizeof(struct smb2_create_req) +
2211 iov[num - 1].iov_len);
2212 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2213 *num_iovec = num + 1;
2218 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2219 const char *treename, const __le16 *path)
2221 int treename_len, path_len;
2222 struct nls_table *cp;
2223 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2228 treename_len = strlen(treename);
2229 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2235 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2238 * make room for one path separator between the treename and
2241 *out_len = treename_len + 1 + path_len;
2244 * final path needs to be null-terminated UTF16 with a
2248 *out_size = roundup((*out_len+1)*2, 8);
2249 *out_path = kzalloc(*out_size, GFP_KERNEL);
2253 cp = load_nls_default();
2254 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2255 UniStrcat(*out_path, sep);
2256 UniStrcat(*out_path, path);
2262 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2263 umode_t mode, struct cifs_tcon *tcon,
2264 const char *full_path,
2265 struct cifs_sb_info *cifs_sb)
2267 struct smb_rqst rqst;
2268 struct smb2_create_req *req;
2269 struct smb2_create_rsp *rsp = NULL;
2270 struct cifs_ses *ses = tcon->ses;
2271 struct kvec iov[3]; /* make sure at least one for each open context */
2272 struct kvec rsp_iov = {NULL, 0};
2275 __le16 *copy_path = NULL;
2278 unsigned int n_iov = 2;
2279 __u32 file_attributes = 0;
2280 char *pc_buf = NULL;
2282 unsigned int total_len;
2283 __le16 *utf16_path = NULL;
2285 cifs_dbg(FYI, "mkdir\n");
2287 /* resource #1: path allocation */
2288 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2292 if (!ses || !(ses->server)) {
2297 /* resource #2: request */
2298 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2303 if (smb3_encryption_required(tcon))
2304 flags |= CIFS_TRANSFORM_REQ;
2306 req->ImpersonationLevel = IL_IMPERSONATION;
2307 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2308 /* File attributes ignored on open (used in create though) */
2309 req->FileAttributes = cpu_to_le32(file_attributes);
2310 req->ShareAccess = FILE_SHARE_ALL_LE;
2311 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2312 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2314 iov[0].iov_base = (char *)req;
2315 /* -1 since last byte is buf[0] which is sent below (path) */
2316 iov[0].iov_len = total_len - 1;
2318 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2320 /* [MS-SMB2] 2.2.13 NameOffset:
2321 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2322 * the SMB2 header, the file name includes a prefix that will
2323 * be processed during DFS name normalization as specified in
2324 * section 3.3.5.9. Otherwise, the file name is relative to
2325 * the share that is identified by the TreeId in the SMB2
2328 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2331 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2332 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2334 tcon->treeName, utf16_path);
2338 req->NameLength = cpu_to_le16(name_len * 2);
2339 uni_path_len = copy_size;
2340 /* free before overwriting resource */
2342 utf16_path = copy_path;
2344 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2345 /* MUST set path len (NameLength) to 0 opening root of share */
2346 req->NameLength = cpu_to_le16(uni_path_len - 2);
2347 if (uni_path_len % 8 != 0) {
2348 copy_size = roundup(uni_path_len, 8);
2349 copy_path = kzalloc(copy_size, GFP_KERNEL);
2354 memcpy((char *)copy_path, (const char *)utf16_path,
2356 uni_path_len = copy_size;
2357 /* free before overwriting resource */
2359 utf16_path = copy_path;
2363 iov[1].iov_len = uni_path_len;
2364 iov[1].iov_base = utf16_path;
2365 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2367 if (tcon->posix_extensions) {
2368 /* resource #3: posix buf */
2369 rc = add_posix_context(iov, &n_iov, mode);
2372 pc_buf = iov[n_iov-1].iov_base;
2376 memset(&rqst, 0, sizeof(struct smb_rqst));
2378 rqst.rq_nvec = n_iov;
2380 /* no need to inc num_remote_opens because we close it just below */
2381 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2382 FILE_WRITE_ATTRIBUTES);
2383 /* resource #4: response buffer */
2384 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2386 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2387 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2389 FILE_WRITE_ATTRIBUTES, rc);
2390 goto err_free_rsp_buf;
2393 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2394 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2395 ses->Suid, CREATE_NOT_FILE,
2396 FILE_WRITE_ATTRIBUTES);
2398 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2400 /* Eventually save off posix specific response info and timestaps */
2403 free_rsp_buf(resp_buftype, rsp);
2406 cifs_small_buf_release(req);
2413 SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2414 struct cifs_open_parms *oparms, __le16 *path)
2416 struct TCP_Server_Info *server = tcon->ses->server;
2417 struct smb2_create_req *req;
2418 unsigned int n_iov = 2;
2419 __u32 file_attributes = 0;
2422 unsigned int total_len;
2423 struct kvec *iov = rqst->rq_iov;
2427 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2431 iov[0].iov_base = (char *)req;
2432 /* -1 since last byte is buf[0] which is sent below (path) */
2433 iov[0].iov_len = total_len - 1;
2435 if (oparms->create_options & CREATE_OPTION_READONLY)
2436 file_attributes |= ATTR_READONLY;
2437 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2438 file_attributes |= ATTR_SYSTEM;
2440 req->ImpersonationLevel = IL_IMPERSONATION;
2441 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2442 /* File attributes ignored on open (used in create though) */
2443 req->FileAttributes = cpu_to_le32(file_attributes);
2444 req->ShareAccess = FILE_SHARE_ALL_LE;
2446 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2447 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2448 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2450 /* [MS-SMB2] 2.2.13 NameOffset:
2451 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2452 * the SMB2 header, the file name includes a prefix that will
2453 * be processed during DFS name normalization as specified in
2454 * section 3.3.5.9. Otherwise, the file name is relative to
2455 * the share that is identified by the TreeId in the SMB2
2458 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2461 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2462 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2464 tcon->treeName, path);
2467 req->NameLength = cpu_to_le16(name_len * 2);
2468 uni_path_len = copy_size;
2471 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2472 /* MUST set path len (NameLength) to 0 opening root of share */
2473 req->NameLength = cpu_to_le16(uni_path_len - 2);
2474 copy_size = uni_path_len;
2475 if (copy_size % 8 != 0)
2476 copy_size = roundup(copy_size, 8);
2477 copy_path = kzalloc(copy_size, GFP_KERNEL);
2480 memcpy((char *)copy_path, (const char *)path,
2482 uni_path_len = copy_size;
2486 iov[1].iov_len = uni_path_len;
2487 iov[1].iov_base = path;
2489 if ((!server->oplocks) || (tcon->no_lease))
2490 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2492 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2493 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2494 req->RequestedOplockLevel = *oplock;
2495 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2496 (oparms->create_options & CREATE_NOT_FILE))
2497 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2499 rc = add_lease_context(server, iov, &n_iov,
2500 oparms->fid->lease_key, oplock);
2505 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2506 /* need to set Next field of lease context if we request it */
2507 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2508 struct create_context *ccontext =
2509 (struct create_context *)iov[n_iov-1].iov_base;
2511 cpu_to_le32(server->vals->create_lease_size);
2514 rc = add_durable_context(iov, &n_iov, oparms,
2515 tcon->use_persistent);
2520 if (tcon->posix_extensions) {
2522 struct create_context *ccontext =
2523 (struct create_context *)iov[n_iov-1].iov_base;
2525 cpu_to_le32(iov[n_iov-1].iov_len);
2528 rc = add_posix_context(iov, &n_iov, oparms->mode);
2533 if (tcon->snapshot_time) {
2534 cifs_dbg(FYI, "adding snapshot context\n");
2536 struct create_context *ccontext =
2537 (struct create_context *)iov[n_iov-1].iov_base;
2539 cpu_to_le32(iov[n_iov-1].iov_len);
2542 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2547 if ((oparms->disposition == FILE_CREATE) &&
2548 (oparms->mode != ACL_NO_MODE)) {
2550 struct create_context *ccontext =
2551 (struct create_context *)iov[n_iov-1].iov_base;
2553 cpu_to_le32(iov[n_iov-1].iov_len);
2556 /* rc = add_sd_context(iov, &n_iov, oparms->mode); */
2562 struct create_context *ccontext =
2563 (struct create_context *)iov[n_iov-1].iov_base;
2564 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2566 add_query_id_context(iov, &n_iov);
2568 rqst->rq_nvec = n_iov;
2572 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2573 * All other vectors are freed by kfree().
2576 SMB2_open_free(struct smb_rqst *rqst)
2580 if (rqst && rqst->rq_iov) {
2581 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2582 for (i = 1; i < rqst->rq_nvec; i++)
2583 if (rqst->rq_iov[i].iov_base != smb2_padding)
2584 kfree(rqst->rq_iov[i].iov_base);
2589 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2590 __u8 *oplock, struct smb2_file_all_info *buf,
2591 struct kvec *err_iov, int *buftype)
2593 struct smb_rqst rqst;
2594 struct smb2_create_rsp *rsp = NULL;
2595 struct TCP_Server_Info *server;
2596 struct cifs_tcon *tcon = oparms->tcon;
2597 struct cifs_ses *ses = tcon->ses;
2598 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2599 struct kvec rsp_iov = {NULL, 0};
2600 int resp_buftype = CIFS_NO_BUFFER;
2604 cifs_dbg(FYI, "create/open\n");
2605 if (ses && (ses->server))
2606 server = ses->server;
2610 if (smb3_encryption_required(tcon))
2611 flags |= CIFS_TRANSFORM_REQ;
2613 memset(&rqst, 0, sizeof(struct smb_rqst));
2614 memset(&iov, 0, sizeof(iov));
2616 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2618 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2622 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2623 oparms->create_options, oparms->desired_access);
2625 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2627 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2630 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2631 if (err_iov && rsp) {
2633 *buftype = resp_buftype;
2634 resp_buftype = CIFS_NO_BUFFER;
2637 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2638 oparms->create_options, oparms->desired_access, rc);
2639 if (rc == -EREMCHG) {
2640 printk_once(KERN_WARNING "server share %s deleted\n",
2642 tcon->need_reconnect = true;
2646 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2647 ses->Suid, oparms->create_options,
2648 oparms->desired_access);
2650 atomic_inc(&tcon->num_remote_opens);
2651 oparms->fid->persistent_fid = rsp->PersistentFileId;
2652 oparms->fid->volatile_fid = rsp->VolatileFileId;
2653 oparms->fid->access = oparms->desired_access;
2654 #ifdef CONFIG_CIFS_DEBUG2
2655 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2656 #endif /* CIFS_DEBUG2 */
2659 memcpy(buf, &rsp->CreationTime, 32);
2660 buf->AllocationSize = rsp->AllocationSize;
2661 buf->EndOfFile = rsp->EndofFile;
2662 buf->Attributes = rsp->FileAttributes;
2663 buf->NumberOfLinks = cpu_to_le32(1);
2664 buf->DeletePending = 0;
2668 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2669 oparms->fid->lease_key, oplock, buf);
2671 SMB2_open_free(&rqst);
2672 free_rsp_buf(resp_buftype, rsp);
2677 SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2678 u64 persistent_fid, u64 volatile_fid, u32 opcode,
2679 bool is_fsctl, char *in_data, u32 indatalen,
2680 __u32 max_response_size)
2682 struct smb2_ioctl_req *req;
2683 struct kvec *iov = rqst->rq_iov;
2684 unsigned int total_len;
2688 rc = smb2_ioctl_req_init(opcode, tcon, (void **) &req, &total_len);
2694 * indatalen is usually small at a couple of bytes max, so
2695 * just allocate through generic pool
2697 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
2699 cifs_small_buf_release(req);
2704 req->CtlCode = cpu_to_le32(opcode);
2705 req->PersistentFileId = persistent_fid;
2706 req->VolatileFileId = volatile_fid;
2708 iov[0].iov_base = (char *)req;
2710 * If no input data, the size of ioctl struct in
2711 * protocol spec still includes a 1 byte data buffer,
2712 * but if input data passed to ioctl, we do not
2713 * want to double count this, so we do not send
2714 * the dummy one byte of data in iovec[0] if sending
2715 * input data (in iovec[1]).
2718 req->InputCount = cpu_to_le32(indatalen);
2719 /* do not set InputOffset if no input data */
2721 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
2723 iov[0].iov_len = total_len - 1;
2724 iov[1].iov_base = in_data_buf;
2725 iov[1].iov_len = indatalen;
2728 iov[0].iov_len = total_len;
2731 req->OutputOffset = 0;
2732 req->OutputCount = 0; /* MBZ */
2735 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2736 * We Could increase default MaxOutputResponse, but that could require
2737 * more credits. Windows typically sets this smaller, but for some
2738 * ioctls it may be useful to allow server to send more. No point
2739 * limiting what the server can send as long as fits in one credit
2740 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2741 * to increase this limit up in the future.
2742 * Note that for snapshot queries that servers like Azure expect that
2743 * the first query be minimal size (and just used to get the number/size
2744 * of previous versions) so response size must be specified as EXACTLY
2745 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2746 * of eight bytes. Currently that is the only case where we set max
2747 * response size smaller.
2749 req->MaxOutputResponse = cpu_to_le32(max_response_size);
2752 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2756 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2757 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
2758 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
2764 SMB2_ioctl_free(struct smb_rqst *rqst)
2767 if (rqst && rqst->rq_iov) {
2768 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2769 for (i = 1; i < rqst->rq_nvec; i++)
2770 if (rqst->rq_iov[i].iov_base != smb2_padding)
2771 kfree(rqst->rq_iov[i].iov_base);
2777 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2780 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2781 u64 volatile_fid, u32 opcode, bool is_fsctl,
2782 char *in_data, u32 indatalen, u32 max_out_data_len,
2783 char **out_data, u32 *plen /* returned data len */)
2785 struct smb_rqst rqst;
2786 struct smb2_ioctl_rsp *rsp = NULL;
2787 struct cifs_ses *ses;
2788 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
2789 struct kvec rsp_iov = {NULL, 0};
2790 int resp_buftype = CIFS_NO_BUFFER;
2793 struct TCP_Server_Info *server;
2795 cifs_dbg(FYI, "SMB2 IOCTL\n");
2797 if (out_data != NULL)
2800 /* zero out returned data len, in case of error */
2811 server = ses->server;
2815 if (smb3_encryption_required(tcon))
2816 flags |= CIFS_TRANSFORM_REQ;
2818 memset(&rqst, 0, sizeof(struct smb_rqst));
2819 memset(&iov, 0, sizeof(iov));
2821 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
2823 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
2824 is_fsctl, in_data, indatalen, max_out_data_len);
2828 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2830 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
2833 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2834 ses->Suid, 0, opcode, rc);
2836 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
2837 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2839 } else if (rc == -EINVAL) {
2840 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2841 (opcode != FSCTL_SRV_COPYCHUNK)) {
2842 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2845 } else if (rc == -E2BIG) {
2846 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
2847 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2852 /* check if caller wants to look at return data or just return rc */
2853 if ((plen == NULL) || (out_data == NULL))
2856 *plen = le32_to_cpu(rsp->OutputCount);
2858 /* We check for obvious errors in the output buffer length and offset */
2860 goto ioctl_exit; /* server returned no data */
2861 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
2862 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2868 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
2869 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2870 le32_to_cpu(rsp->OutputOffset));
2876 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2878 if (*out_data == NULL) {
2884 SMB2_ioctl_free(&rqst);
2885 free_rsp_buf(resp_buftype, rsp);
2890 * Individual callers to ioctl worker function follow
2894 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2895 u64 persistent_fid, u64 volatile_fid)
2898 struct compress_ioctl fsctl_input;
2899 char *ret_data = NULL;
2901 fsctl_input.CompressionState =
2902 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
2904 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2905 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2906 (char *)&fsctl_input /* data input */,
2907 2 /* in data len */, CIFSMaxBufSize /* max out data */,
2908 &ret_data /* out data */, NULL);
2910 cifs_dbg(FYI, "set compression rc %d\n", rc);
2916 SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2917 u64 persistent_fid, u64 volatile_fid)
2919 struct smb2_close_req *req;
2920 struct kvec *iov = rqst->rq_iov;
2921 unsigned int total_len;
2924 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2928 req->PersistentFileId = persistent_fid;
2929 req->VolatileFileId = volatile_fid;
2930 iov[0].iov_base = (char *)req;
2931 iov[0].iov_len = total_len;
2937 SMB2_close_free(struct smb_rqst *rqst)
2939 if (rqst && rqst->rq_iov)
2940 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2944 SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2945 u64 persistent_fid, u64 volatile_fid, int flags)
2947 struct smb_rqst rqst;
2948 struct smb2_close_rsp *rsp = NULL;
2949 struct cifs_ses *ses = tcon->ses;
2951 struct kvec rsp_iov;
2952 int resp_buftype = CIFS_NO_BUFFER;
2955 cifs_dbg(FYI, "Close\n");
2957 if (!ses || !(ses->server))
2960 if (smb3_encryption_required(tcon))
2961 flags |= CIFS_TRANSFORM_REQ;
2963 memset(&rqst, 0, sizeof(struct smb_rqst));
2964 memset(&iov, 0, sizeof(iov));
2968 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
2969 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid);
2973 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2974 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2977 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
2978 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2982 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
2985 atomic_dec(&tcon->num_remote_opens);
2987 /* BB FIXME - decode close response, update inode for caching */
2990 SMB2_close_free(&rqst);
2991 free_rsp_buf(resp_buftype, rsp);
2996 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2997 u64 persistent_fid, u64 volatile_fid)
3002 rc = SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
3004 /* retry close in a worker thread if this one is interrupted */
3006 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3009 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3010 persistent_fid, tmp_rc);
3017 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3018 struct kvec *iov, unsigned int min_buf_size)
3020 unsigned int smb_len = iov->iov_len;
3021 char *end_of_smb = smb_len + (char *)iov->iov_base;
3022 char *begin_of_buf = offset + (char *)iov->iov_base;
3023 char *end_of_buf = begin_of_buf + buffer_length;
3026 if (buffer_length < min_buf_size) {
3027 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3028 buffer_length, min_buf_size);
3032 /* check if beyond RFC1001 maximum length */
3033 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3034 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3035 buffer_length, smb_len);
3039 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3040 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
3048 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3049 * Caller must free buffer.
3052 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3053 struct kvec *iov, unsigned int minbufsize,
3056 char *begin_of_buf = offset + (char *)iov->iov_base;
3062 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3066 memcpy(data, begin_of_buf, buffer_length);
3072 SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3073 u64 persistent_fid, u64 volatile_fid,
3074 u8 info_class, u8 info_type, u32 additional_info,
3075 size_t output_len, size_t input_len, void *input)
3077 struct smb2_query_info_req *req;
3078 struct kvec *iov = rqst->rq_iov;
3079 unsigned int total_len;
3082 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3087 req->InfoType = info_type;
3088 req->FileInfoClass = info_class;
3089 req->PersistentFileId = persistent_fid;
3090 req->VolatileFileId = volatile_fid;
3091 req->AdditionalInformation = cpu_to_le32(additional_info);
3093 req->OutputBufferLength = cpu_to_le32(output_len);
3095 req->InputBufferLength = cpu_to_le32(input_len);
3096 /* total_len for smb query request never close to le16 max */
3097 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3098 memcpy(req->Buffer, input, input_len);
3101 iov[0].iov_base = (char *)req;
3103 iov[0].iov_len = total_len - 1 + input_len;
3108 SMB2_query_info_free(struct smb_rqst *rqst)
3110 if (rqst && rqst->rq_iov)
3111 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3115 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3116 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3117 u32 additional_info, size_t output_len, size_t min_len, void **data,
3120 struct smb_rqst rqst;
3121 struct smb2_query_info_rsp *rsp = NULL;
3123 struct kvec rsp_iov;
3125 int resp_buftype = CIFS_NO_BUFFER;
3126 struct cifs_ses *ses = tcon->ses;
3127 struct TCP_Server_Info *server;
3129 bool allocated = false;
3131 cifs_dbg(FYI, "Query Info\n");
3135 server = ses->server;
3139 if (smb3_encryption_required(tcon))
3140 flags |= CIFS_TRANSFORM_REQ;
3142 memset(&rqst, 0, sizeof(struct smb_rqst));
3143 memset(&iov, 0, sizeof(iov));
3147 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
3148 info_class, info_type, additional_info,
3149 output_len, 0, NULL);
3153 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3154 ses->Suid, info_class, (__u32)info_type);
3156 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3157 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3160 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3161 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3162 ses->Suid, info_class, (__u32)info_type, rc);
3166 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3167 ses->Suid, info_class, (__u32)info_type);
3170 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3172 *data = kmalloc(*dlen, GFP_KERNEL);
3175 "Error %d allocating memory for acl\n",
3185 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3186 le32_to_cpu(rsp->OutputBufferLength),
3187 &rsp_iov, min_len, *data);
3188 if (rc && allocated) {
3195 SMB2_query_info_free(&rqst);
3196 free_rsp_buf(resp_buftype, rsp);
3200 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3201 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3203 return query_info(xid, tcon, persistent_fid, volatile_fid,
3204 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3205 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3206 sizeof(struct smb2_file_all_info), (void **)&data,
3211 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3212 u64 persistent_fid, u64 volatile_fid,
3213 void **data, u32 *plen)
3215 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3218 return query_info(xid, tcon, persistent_fid, volatile_fid,
3219 0, SMB2_O_INFO_SECURITY, additional_info,
3220 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3224 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3225 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3227 return query_info(xid, tcon, persistent_fid, volatile_fid,
3228 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3229 sizeof(struct smb2_file_internal_info),
3230 sizeof(struct smb2_file_internal_info),
3231 (void **)&uniqueid, NULL);
3235 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3236 * See MS-SMB2 2.2.35 and 2.2.36
3240 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3241 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid,
3242 u32 completion_filter, bool watch_tree)
3244 struct smb2_change_notify_req *req;
3245 struct kvec *iov = rqst->rq_iov;
3246 unsigned int total_len;
3249 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, (void **) &req, &total_len);
3253 req->PersistentFileId = persistent_fid;
3254 req->VolatileFileId = volatile_fid;
3255 req->OutputBufferLength =
3256 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3257 req->CompletionFilter = cpu_to_le32(completion_filter);
3259 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3263 iov[0].iov_base = (char *)req;
3264 iov[0].iov_len = total_len;
3270 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3271 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3272 u32 completion_filter)
3274 struct cifs_ses *ses = tcon->ses;
3275 struct smb_rqst rqst;
3277 struct kvec rsp_iov = {NULL, 0};
3278 int resp_buftype = CIFS_NO_BUFFER;
3282 cifs_dbg(FYI, "change notify\n");
3283 if (!ses || !(ses->server))
3286 if (smb3_encryption_required(tcon))
3287 flags |= CIFS_TRANSFORM_REQ;
3289 memset(&rqst, 0, sizeof(struct smb_rqst));
3290 memset(&iov, 0, sizeof(iov));
3294 rc = SMB2_notify_init(xid, &rqst, tcon, persistent_fid, volatile_fid,
3295 completion_filter, watch_tree);
3299 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3300 (u8)watch_tree, completion_filter);
3301 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3304 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3305 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3306 (u8)watch_tree, completion_filter, rc);
3308 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3309 ses->Suid, (u8)watch_tree, completion_filter);
3313 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3314 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3321 * This is a no-op for now. We're not really interested in the reply, but
3322 * rather in the fact that the server sent one and that server->lstrp
3325 * FIXME: maybe we should consider checking that the reply matches request?
3328 smb2_echo_callback(struct mid_q_entry *mid)
3330 struct TCP_Server_Info *server = mid->callback_data;
3331 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3332 struct cifs_credits credits = { .value = 0, .instance = 0 };
3334 if (mid->mid_state == MID_RESPONSE_RECEIVED
3335 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3336 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3337 credits.instance = server->reconnect_instance;
3340 DeleteMidQEntry(mid);
3341 add_credits(server, &credits, CIFS_ECHO_OP);
3344 void smb2_reconnect_server(struct work_struct *work)
3346 struct TCP_Server_Info *server = container_of(work,
3347 struct TCP_Server_Info, reconnect.work);
3348 struct cifs_ses *ses;
3349 struct cifs_tcon *tcon, *tcon2;
3350 struct list_head tmp_list;
3351 int tcon_exist = false;
3353 int resched = false;
3356 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3357 mutex_lock(&server->reconnect_mutex);
3359 INIT_LIST_HEAD(&tmp_list);
3360 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3362 spin_lock(&cifs_tcp_ses_lock);
3363 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3364 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3365 if (tcon->need_reconnect || tcon->need_reopen_files) {
3367 list_add_tail(&tcon->rlist, &tmp_list);
3372 * IPC has the same lifetime as its session and uses its
3375 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3376 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3382 * Get the reference to server struct to be sure that the last call of
3383 * cifs_put_tcon() in the loop below won't release the server pointer.
3386 server->srv_count++;
3388 spin_unlock(&cifs_tcp_ses_lock);
3390 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3391 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3393 cifs_reopen_persistent_handles(tcon);
3396 list_del_init(&tcon->rlist);
3398 cifs_put_smb_ses(tcon->ses);
3400 cifs_put_tcon(tcon);
3403 cifs_dbg(FYI, "Reconnecting tcons finished\n");
3405 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3406 mutex_unlock(&server->reconnect_mutex);
3408 /* now we can safely release srv struct */
3410 cifs_put_tcp_session(server, 1);
3414 SMB2_echo(struct TCP_Server_Info *server)
3416 struct smb2_echo_req *req;
3419 struct smb_rqst rqst = { .rq_iov = iov,
3421 unsigned int total_len;
3423 cifs_dbg(FYI, "In echo request\n");
3425 if (server->tcpStatus == CifsNeedNegotiate) {
3426 /* No need to send echo on newly established connections */
3427 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3431 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
3435 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3437 iov[0].iov_len = total_len;
3438 iov[0].iov_base = (char *)req;
3440 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3441 server, CIFS_ECHO_OP, NULL);
3443 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3445 cifs_small_buf_release(req);
3450 SMB2_flush_free(struct smb_rqst *rqst)
3452 if (rqst && rqst->rq_iov)
3453 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3457 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3458 struct cifs_tcon *tcon, u64 persistent_fid, u64 volatile_fid)
3460 struct smb2_flush_req *req;
3461 struct kvec *iov = rqst->rq_iov;
3462 unsigned int total_len;
3465 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
3469 req->PersistentFileId = persistent_fid;
3470 req->VolatileFileId = volatile_fid;
3472 iov[0].iov_base = (char *)req;
3473 iov[0].iov_len = total_len;
3479 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3482 struct cifs_ses *ses = tcon->ses;
3483 struct smb_rqst rqst;
3485 struct kvec rsp_iov = {NULL, 0};
3486 int resp_buftype = CIFS_NO_BUFFER;
3490 cifs_dbg(FYI, "flush\n");
3491 if (!ses || !(ses->server))
3494 if (smb3_encryption_required(tcon))
3495 flags |= CIFS_TRANSFORM_REQ;
3497 memset(&rqst, 0, sizeof(struct smb_rqst));
3498 memset(&iov, 0, sizeof(iov));
3502 rc = SMB2_flush_init(xid, &rqst, tcon, persistent_fid, volatile_fid);
3506 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3507 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3510 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
3511 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3514 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3518 SMB2_flush_free(&rqst);
3519 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3524 * To form a chain of read requests, any read requests after the first should
3525 * have the end_of_chain boolean set to true.
3528 smb2_new_read_req(void **buf, unsigned int *total_len,
3529 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3530 unsigned int remaining_bytes, int request_type)
3533 struct smb2_read_plain_req *req = NULL;
3534 struct smb2_sync_hdr *shdr;
3535 struct TCP_Server_Info *server;
3537 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3542 server = io_parms->tcon->ses->server;
3544 return -ECONNABORTED;
3546 shdr = &req->sync_hdr;
3547 shdr->ProcessId = cpu_to_le32(io_parms->pid);
3549 req->PersistentFileId = io_parms->persistent_fid;
3550 req->VolatileFileId = io_parms->volatile_fid;
3551 req->ReadChannelInfoOffset = 0; /* reserved */
3552 req->ReadChannelInfoLength = 0; /* reserved */
3553 req->Channel = 0; /* reserved */
3554 req->MinimumCount = 0;
3555 req->Length = cpu_to_le32(io_parms->length);
3556 req->Offset = cpu_to_le64(io_parms->offset);
3558 trace_smb3_read_enter(0 /* xid */,
3559 io_parms->persistent_fid,
3560 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3561 io_parms->offset, io_parms->length);
3562 #ifdef CONFIG_CIFS_SMB_DIRECT
3564 * If we want to do a RDMA write, fill in and append
3565 * smbd_buffer_descriptor_v1 to the end of read request
3567 if (server->rdma && rdata && !server->sign &&
3568 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
3570 struct smbd_buffer_descriptor_v1 *v1;
3571 bool need_invalidate =
3572 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3574 rdata->mr = smbd_register_mr(
3575 server->smbd_conn, rdata->pages,
3576 rdata->nr_pages, rdata->page_offset,
3577 rdata->tailsz, true, need_invalidate);
3581 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3582 if (need_invalidate)
3583 req->Channel = SMB2_CHANNEL_RDMA_V1;
3584 req->ReadChannelInfoOffset =
3585 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
3586 req->ReadChannelInfoLength =
3587 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3588 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3589 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3590 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3591 v1->length = cpu_to_le32(rdata->mr->mr->length);
3593 *total_len += sizeof(*v1) - 1;
3596 if (request_type & CHAINED_REQUEST) {
3597 if (!(request_type & END_OF_CHAIN)) {
3598 /* next 8-byte aligned request */
3599 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3600 shdr->NextCommand = cpu_to_le32(*total_len);
3601 } else /* END_OF_CHAIN */
3602 shdr->NextCommand = 0;
3603 if (request_type & RELATED_REQUEST) {
3604 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
3606 * Related requests use info from previous read request
3609 shdr->SessionId = 0xFFFFFFFF;
3610 shdr->TreeId = 0xFFFFFFFF;
3611 req->PersistentFileId = 0xFFFFFFFF;
3612 req->VolatileFileId = 0xFFFFFFFF;
3615 if (remaining_bytes > io_parms->length)
3616 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3618 req->RemainingBytes = 0;
3625 smb2_readv_callback(struct mid_q_entry *mid)
3627 struct cifs_readdata *rdata = mid->callback_data;
3628 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3629 struct TCP_Server_Info *server = tcon->ses->server;
3630 struct smb2_sync_hdr *shdr =
3631 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
3632 struct cifs_credits credits = { .value = 0, .instance = 0 };
3633 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
3635 .rq_pages = rdata->pages,
3636 .rq_offset = rdata->page_offset,
3637 .rq_npages = rdata->nr_pages,
3638 .rq_pagesz = rdata->pagesz,
3639 .rq_tailsz = rdata->tailsz };
3641 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3642 __func__, mid->mid, mid->mid_state, rdata->result,
3645 switch (mid->mid_state) {
3646 case MID_RESPONSE_RECEIVED:
3647 credits.value = le16_to_cpu(shdr->CreditRequest);
3648 credits.instance = server->reconnect_instance;
3649 /* result already set, check signature */
3650 if (server->sign && !mid->decrypted) {
3653 rc = smb2_verify_signature(&rqst, server);
3655 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
3658 /* FIXME: should this be counted toward the initiating task? */
3659 task_io_account_read(rdata->got_bytes);
3660 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3662 case MID_REQUEST_SUBMITTED:
3663 case MID_RETRY_NEEDED:
3664 rdata->result = -EAGAIN;
3665 if (server->sign && rdata->got_bytes)
3666 /* reset bytes number since we can not check a sign */
3667 rdata->got_bytes = 0;
3668 /* FIXME: should this be counted toward the initiating task? */
3669 task_io_account_read(rdata->got_bytes);
3670 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3672 case MID_RESPONSE_MALFORMED:
3673 credits.value = le16_to_cpu(shdr->CreditRequest);
3674 credits.instance = server->reconnect_instance;
3677 rdata->result = -EIO;
3679 #ifdef CONFIG_CIFS_SMB_DIRECT
3681 * If this rdata has a memmory registered, the MR can be freed
3682 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3683 * because they have limited number and are used for future I/Os
3686 smbd_deregister_mr(rdata->mr);
3690 if (rdata->result && rdata->result != -ENODATA) {
3691 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
3692 trace_smb3_read_err(0 /* xid */,
3693 rdata->cfile->fid.persistent_fid,
3694 tcon->tid, tcon->ses->Suid, rdata->offset,
3695 rdata->bytes, rdata->result);
3697 trace_smb3_read_done(0 /* xid */,
3698 rdata->cfile->fid.persistent_fid,
3699 tcon->tid, tcon->ses->Suid,
3700 rdata->offset, rdata->got_bytes);
3702 queue_work(cifsiod_wq, &rdata->work);
3703 DeleteMidQEntry(mid);
3704 add_credits(server, &credits, 0);
3707 /* smb2_async_readv - send an async read, and set up mid to handle result */
3709 smb2_async_readv(struct cifs_readdata *rdata)
3713 struct smb2_sync_hdr *shdr;
3714 struct cifs_io_parms io_parms;
3715 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3717 struct TCP_Server_Info *server;
3718 unsigned int total_len;
3720 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3721 __func__, rdata->offset, rdata->bytes);
3723 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3724 io_parms.offset = rdata->offset;
3725 io_parms.length = rdata->bytes;
3726 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3727 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3728 io_parms.pid = rdata->pid;
3730 server = io_parms.tcon->ses->server;
3732 rc = smb2_new_read_req(
3733 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
3737 if (smb3_encryption_required(io_parms.tcon))
3738 flags |= CIFS_TRANSFORM_REQ;
3740 rdata->iov[0].iov_base = buf;
3741 rdata->iov[0].iov_len = total_len;
3743 shdr = (struct smb2_sync_hdr *)buf;
3745 if (rdata->credits.value > 0) {
3746 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
3747 SMB2_MAX_BUFFER_SIZE));
3748 shdr->CreditRequest =
3749 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
3751 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3753 goto async_readv_out;
3755 flags |= CIFS_HAS_CREDITS;
3758 kref_get(&rdata->refcount);
3759 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
3760 cifs_readv_receive, smb2_readv_callback,
3761 smb3_handle_read_data, rdata, flags,
3764 kref_put(&rdata->refcount, cifs_readdata_release);
3765 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
3766 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3768 io_parms.tcon->ses->Suid,
3769 io_parms.offset, io_parms.length, rc);
3773 cifs_small_buf_release(buf);
3778 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3779 unsigned int *nbytes, char **buf, int *buf_type)
3781 struct smb_rqst rqst;
3782 int resp_buftype, rc;
3783 struct smb2_read_plain_req *req = NULL;
3784 struct smb2_read_rsp *rsp = NULL;
3786 struct kvec rsp_iov;
3787 unsigned int total_len;
3788 int flags = CIFS_LOG_ERROR;
3789 struct cifs_ses *ses = io_parms->tcon->ses;
3792 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
3796 if (smb3_encryption_required(io_parms->tcon))
3797 flags |= CIFS_TRANSFORM_REQ;
3799 iov[0].iov_base = (char *)req;
3800 iov[0].iov_len = total_len;
3802 memset(&rqst, 0, sizeof(struct smb_rqst));
3806 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3807 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
3810 if (rc != -ENODATA) {
3811 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3812 cifs_dbg(VFS, "Send error in read = %d\n", rc);
3813 trace_smb3_read_err(xid, req->PersistentFileId,
3814 io_parms->tcon->tid, ses->Suid,
3815 io_parms->offset, io_parms->length,
3818 trace_smb3_read_done(xid, req->PersistentFileId,
3819 io_parms->tcon->tid, ses->Suid,
3820 io_parms->offset, 0);
3821 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3822 cifs_small_buf_release(req);
3823 return rc == -ENODATA ? 0 : rc;
3825 trace_smb3_read_done(xid, req->PersistentFileId,
3826 io_parms->tcon->tid, ses->Suid,
3827 io_parms->offset, io_parms->length);
3829 cifs_small_buf_release(req);
3831 *nbytes = le32_to_cpu(rsp->DataLength);
3832 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3833 (*nbytes > io_parms->length)) {
3834 cifs_dbg(FYI, "bad length %d for count %d\n",
3835 *nbytes, io_parms->length);
3841 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
3842 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3843 } else if (resp_buftype != CIFS_NO_BUFFER) {
3844 *buf = rsp_iov.iov_base;
3845 if (resp_buftype == CIFS_SMALL_BUFFER)
3846 *buf_type = CIFS_SMALL_BUFFER;
3847 else if (resp_buftype == CIFS_LARGE_BUFFER)
3848 *buf_type = CIFS_LARGE_BUFFER;
3854 * Check the mid_state and signature on received buffer (if any), and queue the
3855 * workqueue completion task.
3858 smb2_writev_callback(struct mid_q_entry *mid)
3860 struct cifs_writedata *wdata = mid->callback_data;
3861 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3862 struct TCP_Server_Info *server = tcon->ses->server;
3863 unsigned int written;
3864 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
3865 struct cifs_credits credits = { .value = 0, .instance = 0 };
3867 switch (mid->mid_state) {
3868 case MID_RESPONSE_RECEIVED:
3869 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3870 credits.instance = server->reconnect_instance;
3871 wdata->result = smb2_check_receive(mid, server, 0);
3872 if (wdata->result != 0)
3875 written = le32_to_cpu(rsp->DataLength);
3877 * Mask off high 16 bits when bytes written as returned
3878 * by the server is greater than bytes requested by the
3879 * client. OS/2 servers are known to set incorrect
3882 if (written > wdata->bytes)
3885 if (written < wdata->bytes)
3886 wdata->result = -ENOSPC;
3888 wdata->bytes = written;
3890 case MID_REQUEST_SUBMITTED:
3891 case MID_RETRY_NEEDED:
3892 wdata->result = -EAGAIN;
3894 case MID_RESPONSE_MALFORMED:
3895 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3896 credits.instance = server->reconnect_instance;
3899 wdata->result = -EIO;
3902 #ifdef CONFIG_CIFS_SMB_DIRECT
3904 * If this wdata has a memory registered, the MR can be freed
3905 * The number of MRs available is limited, it's important to recover
3906 * used MR as soon as I/O is finished. Hold MR longer in the later
3907 * I/O process can possibly result in I/O deadlock due to lack of MR
3908 * to send request on I/O retry
3911 smbd_deregister_mr(wdata->mr);
3915 if (wdata->result) {
3916 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3917 trace_smb3_write_err(0 /* no xid */,
3918 wdata->cfile->fid.persistent_fid,
3919 tcon->tid, tcon->ses->Suid, wdata->offset,
3920 wdata->bytes, wdata->result);
3921 if (wdata->result == -ENOSPC)
3922 printk_once(KERN_WARNING "Out of space writing to %s\n",
3925 trace_smb3_write_done(0 /* no xid */,
3926 wdata->cfile->fid.persistent_fid,
3927 tcon->tid, tcon->ses->Suid,
3928 wdata->offset, wdata->bytes);
3930 queue_work(cifsiod_wq, &wdata->work);
3931 DeleteMidQEntry(mid);
3932 add_credits(server, &credits, 0);
3935 /* smb2_async_writev - send an async write, and set up mid to handle result */
3937 smb2_async_writev(struct cifs_writedata *wdata,
3938 void (*release)(struct kref *kref))
3940 int rc = -EACCES, flags = 0;
3941 struct smb2_write_req *req = NULL;
3942 struct smb2_sync_hdr *shdr;
3943 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3944 struct TCP_Server_Info *server = tcon->ses->server;
3946 struct smb_rqst rqst = { };
3947 unsigned int total_len;
3949 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
3953 if (smb3_encryption_required(tcon))
3954 flags |= CIFS_TRANSFORM_REQ;
3956 shdr = (struct smb2_sync_hdr *)req;
3957 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
3959 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3960 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3961 req->WriteChannelInfoOffset = 0;
3962 req->WriteChannelInfoLength = 0;
3964 req->Offset = cpu_to_le64(wdata->offset);
3965 req->DataOffset = cpu_to_le16(
3966 offsetof(struct smb2_write_req, Buffer));
3967 req->RemainingBytes = 0;
3969 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
3970 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
3971 #ifdef CONFIG_CIFS_SMB_DIRECT
3973 * If we want to do a server RDMA read, fill in and append
3974 * smbd_buffer_descriptor_v1 to the end of write request
3976 if (server->rdma && !server->sign && wdata->bytes >=
3977 server->smbd_conn->rdma_readwrite_threshold) {
3979 struct smbd_buffer_descriptor_v1 *v1;
3980 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3982 wdata->mr = smbd_register_mr(
3983 server->smbd_conn, wdata->pages,
3984 wdata->nr_pages, wdata->page_offset,
3985 wdata->tailsz, false, need_invalidate);
3988 goto async_writev_out;
3991 req->DataOffset = 0;
3992 if (wdata->nr_pages > 1)
3993 req->RemainingBytes =
3995 (wdata->nr_pages - 1) * wdata->pagesz -
3996 wdata->page_offset + wdata->tailsz
3999 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
4000 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4001 if (need_invalidate)
4002 req->Channel = SMB2_CHANNEL_RDMA_V1;
4003 req->WriteChannelInfoOffset =
4004 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4005 req->WriteChannelInfoLength =
4006 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4007 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4008 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4009 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4010 v1->length = cpu_to_le32(wdata->mr->mr->length);
4013 iov[0].iov_len = total_len - 1;
4014 iov[0].iov_base = (char *)req;
4018 rqst.rq_pages = wdata->pages;
4019 rqst.rq_offset = wdata->page_offset;
4020 rqst.rq_npages = wdata->nr_pages;
4021 rqst.rq_pagesz = wdata->pagesz;
4022 rqst.rq_tailsz = wdata->tailsz;
4023 #ifdef CONFIG_CIFS_SMB_DIRECT
4025 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4029 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4030 wdata->offset, wdata->bytes);
4032 #ifdef CONFIG_CIFS_SMB_DIRECT
4033 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4035 req->Length = cpu_to_le32(wdata->bytes);
4037 req->Length = cpu_to_le32(wdata->bytes);
4040 if (wdata->credits.value > 0) {
4041 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4042 SMB2_MAX_BUFFER_SIZE));
4043 shdr->CreditRequest =
4044 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
4046 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4048 goto async_writev_out;
4050 flags |= CIFS_HAS_CREDITS;
4053 kref_get(&wdata->refcount);
4054 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4055 wdata, flags, &wdata->credits);
4058 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
4059 tcon->tid, tcon->ses->Suid, wdata->offset,
4061 kref_put(&wdata->refcount, release);
4062 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4066 cifs_small_buf_release(req);
4071 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4072 * The length field from io_parms must be at least 1 and indicates a number of
4073 * elements with data to write that begins with position 1 in iov array. All
4074 * data length is specified by count.
4077 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4078 unsigned int *nbytes, struct kvec *iov, int n_vec)
4080 struct smb_rqst rqst;
4082 struct smb2_write_req *req = NULL;
4083 struct smb2_write_rsp *rsp = NULL;
4085 struct kvec rsp_iov;
4087 unsigned int total_len;
4094 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
4099 if (io_parms->tcon->ses->server == NULL)
4100 return -ECONNABORTED;
4102 if (smb3_encryption_required(io_parms->tcon))
4103 flags |= CIFS_TRANSFORM_REQ;
4105 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
4107 req->PersistentFileId = io_parms->persistent_fid;
4108 req->VolatileFileId = io_parms->volatile_fid;
4109 req->WriteChannelInfoOffset = 0;
4110 req->WriteChannelInfoLength = 0;
4112 req->Length = cpu_to_le32(io_parms->length);
4113 req->Offset = cpu_to_le64(io_parms->offset);
4114 req->DataOffset = cpu_to_le16(
4115 offsetof(struct smb2_write_req, Buffer));
4116 req->RemainingBytes = 0;
4118 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4119 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4120 io_parms->offset, io_parms->length);
4122 iov[0].iov_base = (char *)req;
4124 iov[0].iov_len = total_len - 1;
4126 memset(&rqst, 0, sizeof(struct smb_rqst));
4128 rqst.rq_nvec = n_vec + 1;
4130 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
4131 &resp_buftype, flags, &rsp_iov);
4132 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
4135 trace_smb3_write_err(xid, req->PersistentFileId,
4136 io_parms->tcon->tid,
4137 io_parms->tcon->ses->Suid,
4138 io_parms->offset, io_parms->length, rc);
4139 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
4140 cifs_dbg(VFS, "Send error in write = %d\n", rc);
4142 *nbytes = le32_to_cpu(rsp->DataLength);
4143 trace_smb3_write_done(xid, req->PersistentFileId,
4144 io_parms->tcon->tid,
4145 io_parms->tcon->ses->Suid,
4146 io_parms->offset, *nbytes);
4149 cifs_small_buf_release(req);
4150 free_rsp_buf(resp_buftype, rsp);
4155 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
4158 unsigned int entrycount = 0;
4159 unsigned int next_offset = 0;
4161 FILE_DIRECTORY_INFO *dir_info;
4163 if (bufstart == NULL)
4166 entryptr = bufstart;
4169 if (entryptr + next_offset < entryptr ||
4170 entryptr + next_offset > end_of_buf ||
4171 entryptr + next_offset + size > end_of_buf) {
4172 cifs_dbg(VFS, "malformed search entry would overflow\n");
4176 entryptr = entryptr + next_offset;
4177 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4179 len = le32_to_cpu(dir_info->FileNameLength);
4180 if (entryptr + len < entryptr ||
4181 entryptr + len > end_of_buf ||
4182 entryptr + len + size > end_of_buf) {
4183 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4188 *lastentry = entryptr;
4191 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
4203 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4204 u64 persistent_fid, u64 volatile_fid, int index,
4205 struct cifs_search_info *srch_inf)
4207 struct smb_rqst rqst;
4208 struct smb2_query_directory_req *req;
4209 struct smb2_query_directory_rsp *rsp = NULL;
4211 struct kvec rsp_iov;
4214 int resp_buftype = CIFS_NO_BUFFER;
4215 unsigned char *bufptr;
4216 struct TCP_Server_Info *server;
4217 struct cifs_ses *ses = tcon->ses;
4218 __le16 asteriks = cpu_to_le16('*');
4220 unsigned int output_size = CIFSMaxBufSize;
4221 size_t info_buf_size;
4223 unsigned int total_len;
4225 if (ses && (ses->server))
4226 server = ses->server;
4230 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
4235 if (smb3_encryption_required(tcon))
4236 flags |= CIFS_TRANSFORM_REQ;
4238 switch (srch_inf->info_level) {
4239 case SMB_FIND_FILE_DIRECTORY_INFO:
4240 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4241 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4243 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4244 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4245 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4248 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4249 srch_inf->info_level);
4254 req->FileIndex = cpu_to_le32(index);
4255 req->PersistentFileId = persistent_fid;
4256 req->VolatileFileId = volatile_fid;
4259 bufptr = req->Buffer;
4260 memcpy(bufptr, &asteriks, len);
4262 req->FileNameOffset =
4263 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
4264 req->FileNameLength = cpu_to_le16(len);
4266 * BB could be 30 bytes or so longer if we used SMB2 specific
4267 * buffer lengths, but this is safe and close enough.
4269 output_size = min_t(unsigned int, output_size, server->maxBuf);
4270 output_size = min_t(unsigned int, output_size, 2 << 15);
4271 req->OutputBufferLength = cpu_to_le32(output_size);
4273 iov[0].iov_base = (char *)req;
4275 iov[0].iov_len = total_len - 1;
4277 iov[1].iov_base = (char *)(req->Buffer);
4278 iov[1].iov_len = len;
4280 memset(&rqst, 0, sizeof(struct smb_rqst));
4284 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4285 tcon->ses->Suid, index, output_size);
4287 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4288 cifs_small_buf_release(req);
4289 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
4292 if (rc == -ENODATA &&
4293 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
4294 trace_smb3_query_dir_done(xid, persistent_fid,
4295 tcon->tid, tcon->ses->Suid, index, 0);
4296 srch_inf->endOfSearch = true;
4299 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4300 tcon->ses->Suid, index, 0, rc);
4301 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
4306 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4307 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4310 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4311 tcon->ses->Suid, index, 0, rc);
4315 srch_inf->unicode = true;
4317 if (srch_inf->ntwrk_buf_start) {
4318 if (srch_inf->smallBuf)
4319 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4321 cifs_buf_release(srch_inf->ntwrk_buf_start);
4323 srch_inf->ntwrk_buf_start = (char *)rsp;
4324 srch_inf->srch_entries_start = srch_inf->last_entry =
4325 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4326 end_of_smb = rsp_iov.iov_len + (char *)rsp;
4327 srch_inf->entries_in_buffer =
4328 num_entries(srch_inf->srch_entries_start, end_of_smb,
4329 &srch_inf->last_entry, info_buf_size);
4330 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4331 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4332 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4333 srch_inf->srch_entries_start, srch_inf->last_entry);
4334 if (resp_buftype == CIFS_LARGE_BUFFER)
4335 srch_inf->smallBuf = false;
4336 else if (resp_buftype == CIFS_SMALL_BUFFER)
4337 srch_inf->smallBuf = true;
4339 cifs_tcon_dbg(VFS, "illegal search buffer type\n");
4341 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4342 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
4346 free_rsp_buf(resp_buftype, rsp);
4351 SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
4352 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4353 u8 info_type, u32 additional_info,
4354 void **data, unsigned int *size)
4356 struct smb2_set_info_req *req;
4357 struct kvec *iov = rqst->rq_iov;
4358 unsigned int i, total_len;
4361 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
4365 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4366 req->InfoType = info_type;
4367 req->FileInfoClass = info_class;
4368 req->PersistentFileId = persistent_fid;
4369 req->VolatileFileId = volatile_fid;
4370 req->AdditionalInformation = cpu_to_le32(additional_info);
4373 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
4374 req->BufferLength = cpu_to_le32(*size);
4376 memcpy(req->Buffer, *data, *size);
4379 iov[0].iov_base = (char *)req;
4381 iov[0].iov_len = total_len - 1;
4383 for (i = 1; i < rqst->rq_nvec; i++) {
4384 le32_add_cpu(&req->BufferLength, size[i]);
4385 iov[i].iov_base = (char *)data[i];
4386 iov[i].iov_len = size[i];
4393 SMB2_set_info_free(struct smb_rqst *rqst)
4395 if (rqst && rqst->rq_iov)
4396 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
4400 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4401 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4402 u8 info_type, u32 additional_info, unsigned int num,
4403 void **data, unsigned int *size)
4405 struct smb_rqst rqst;
4406 struct smb2_set_info_rsp *rsp = NULL;
4408 struct kvec rsp_iov;
4411 struct cifs_ses *ses = tcon->ses;
4414 if (!ses || !(ses->server))
4420 if (smb3_encryption_required(tcon))
4421 flags |= CIFS_TRANSFORM_REQ;
4423 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4427 memset(&rqst, 0, sizeof(struct smb_rqst));
4431 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4432 info_class, info_type, additional_info,
4440 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
4442 SMB2_set_info_free(&rqst);
4443 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
4446 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
4447 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4448 ses->Suid, info_class, (__u32)info_type, rc);
4451 free_rsp_buf(resp_buftype, rsp);
4457 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4458 u64 volatile_fid, u32 pid, __le64 *eof)
4460 struct smb2_file_eof_info info;
4464 info.EndOfFile = *eof;
4467 size = sizeof(struct smb2_file_eof_info);
4469 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4470 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4471 0, 1, &data, &size);
4475 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4476 u64 persistent_fid, u64 volatile_fid,
4477 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4479 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4480 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4481 1, (void **)&pnntsd, &pacllen);
4485 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4486 u64 persistent_fid, u64 volatile_fid,
4487 struct smb2_file_full_ea_info *buf, int len)
4489 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4490 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4491 0, 1, (void **)&buf, &len);
4495 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4496 const u64 persistent_fid, const u64 volatile_fid,
4499 struct smb_rqst rqst;
4501 struct smb2_oplock_break *req = NULL;
4502 struct cifs_ses *ses = tcon->ses;
4503 int flags = CIFS_OBREAK_OP;
4504 unsigned int total_len;
4506 struct kvec rsp_iov;
4509 cifs_dbg(FYI, "SMB2_oplock_break\n");
4510 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4515 if (smb3_encryption_required(tcon))
4516 flags |= CIFS_TRANSFORM_REQ;
4518 req->VolatileFid = volatile_fid;
4519 req->PersistentFid = persistent_fid;
4520 req->OplockLevel = oplock_level;
4521 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4523 flags |= CIFS_NO_RSP_BUF;
4525 iov[0].iov_base = (char *)req;
4526 iov[0].iov_len = total_len;
4528 memset(&rqst, 0, sizeof(struct smb_rqst));
4532 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4533 cifs_small_buf_release(req);
4536 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4537 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
4544 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4545 struct kstatfs *kst)
4547 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4548 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4549 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
4550 kst->f_bfree = kst->f_bavail =
4551 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
4556 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4557 struct kstatfs *kst)
4559 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4560 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4561 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4562 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4563 kst->f_bavail = kst->f_bfree;
4565 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4566 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4567 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4568 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4569 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4575 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4576 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4579 struct smb2_query_info_req *req;
4580 unsigned int total_len;
4582 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
4584 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4587 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4592 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4593 req->FileInfoClass = level;
4594 req->PersistentFileId = persistent_fid;
4595 req->VolatileFileId = volatile_fid;
4597 req->InputBufferOffset =
4598 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
4599 req->OutputBufferLength = cpu_to_le32(
4600 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
4602 iov->iov_base = (char *)req;
4603 iov->iov_len = total_len;
4608 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4609 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4611 struct smb_rqst rqst;
4612 struct smb2_query_info_rsp *rsp = NULL;
4614 struct kvec rsp_iov;
4617 struct cifs_ses *ses = tcon->ses;
4618 FILE_SYSTEM_POSIX_INFO *info = NULL;
4621 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4622 sizeof(FILE_SYSTEM_POSIX_INFO),
4623 persistent_fid, volatile_fid);
4627 if (smb3_encryption_required(tcon))
4628 flags |= CIFS_TRANSFORM_REQ;
4630 memset(&rqst, 0, sizeof(struct smb_rqst));
4634 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4635 cifs_small_buf_release(iov.iov_base);
4637 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4638 goto posix_qfsinf_exit;
4640 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4642 info = (FILE_SYSTEM_POSIX_INFO *)(
4643 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4644 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4645 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4646 sizeof(FILE_SYSTEM_POSIX_INFO));
4648 copy_posix_fs_info_to_kstatfs(info, fsdata);
4651 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4656 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4657 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4659 struct smb_rqst rqst;
4660 struct smb2_query_info_rsp *rsp = NULL;
4662 struct kvec rsp_iov;
4665 struct cifs_ses *ses = tcon->ses;
4666 struct smb2_fs_full_size_info *info = NULL;
4669 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4670 sizeof(struct smb2_fs_full_size_info),
4671 persistent_fid, volatile_fid);
4675 if (smb3_encryption_required(tcon))
4676 flags |= CIFS_TRANSFORM_REQ;
4678 memset(&rqst, 0, sizeof(struct smb_rqst));
4682 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4683 cifs_small_buf_release(iov.iov_base);
4685 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4688 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4690 info = (struct smb2_fs_full_size_info *)(
4691 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4692 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4693 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4694 sizeof(struct smb2_fs_full_size_info));
4696 smb2_copy_fs_info_to_kstatfs(info, fsdata);
4699 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4704 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
4705 u64 persistent_fid, u64 volatile_fid, int level)
4707 struct smb_rqst rqst;
4708 struct smb2_query_info_rsp *rsp = NULL;
4710 struct kvec rsp_iov;
4712 int resp_buftype, max_len, min_len;
4713 struct cifs_ses *ses = tcon->ses;
4714 unsigned int rsp_len, offset;
4717 if (level == FS_DEVICE_INFORMATION) {
4718 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4719 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4720 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4721 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4722 min_len = MIN_FS_ATTR_INFO_SIZE;
4723 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4724 max_len = sizeof(struct smb3_fs_ss_info);
4725 min_len = sizeof(struct smb3_fs_ss_info);
4726 } else if (level == FS_VOLUME_INFORMATION) {
4727 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4728 min_len = sizeof(struct smb3_fs_vol_info);
4730 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
4734 rc = build_qfs_info_req(&iov, tcon, level, max_len,
4735 persistent_fid, volatile_fid);
4739 if (smb3_encryption_required(tcon))
4740 flags |= CIFS_TRANSFORM_REQ;
4742 memset(&rqst, 0, sizeof(struct smb_rqst));
4746 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4747 cifs_small_buf_release(iov.iov_base);
4749 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4752 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4754 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4755 offset = le16_to_cpu(rsp->OutputBufferOffset);
4756 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
4760 if (level == FS_ATTRIBUTE_INFORMATION)
4761 memcpy(&tcon->fsAttrInfo, offset
4762 + (char *)rsp, min_t(unsigned int,
4764 else if (level == FS_DEVICE_INFORMATION)
4765 memcpy(&tcon->fsDevInfo, offset
4766 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
4767 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4768 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
4769 (offset + (char *)rsp);
4770 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4771 tcon->perf_sector_size =
4772 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
4773 } else if (level == FS_VOLUME_INFORMATION) {
4774 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4775 (offset + (char *)rsp);
4776 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4777 tcon->vol_create_time = vol_info->VolumeCreationTime;
4781 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4786 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4787 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4788 const __u32 num_lock, struct smb2_lock_element *buf)
4790 struct smb_rqst rqst;
4792 struct smb2_lock_req *req = NULL;
4794 struct kvec rsp_iov;
4797 int flags = CIFS_NO_RSP_BUF;
4798 unsigned int total_len;
4800 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
4802 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
4806 if (smb3_encryption_required(tcon))
4807 flags |= CIFS_TRANSFORM_REQ;
4809 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4810 req->LockCount = cpu_to_le16(num_lock);
4812 req->PersistentFileId = persist_fid;
4813 req->VolatileFileId = volatile_fid;
4815 count = num_lock * sizeof(struct smb2_lock_element);
4817 iov[0].iov_base = (char *)req;
4818 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
4819 iov[1].iov_base = (char *)buf;
4820 iov[1].iov_len = count;
4822 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
4824 memset(&rqst, 0, sizeof(struct smb_rqst));
4828 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
4830 cifs_small_buf_release(req);
4832 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
4833 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
4834 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4835 tcon->ses->Suid, rc);
4842 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4843 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4844 const __u64 length, const __u64 offset, const __u32 lock_flags,
4847 struct smb2_lock_element lock;
4849 lock.Offset = cpu_to_le64(offset);
4850 lock.Length = cpu_to_le64(length);
4851 lock.Flags = cpu_to_le32(lock_flags);
4852 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
4853 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
4855 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
4859 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
4860 __u8 *lease_key, const __le32 lease_state)
4862 struct smb_rqst rqst;
4864 struct smb2_lease_ack *req = NULL;
4865 struct cifs_ses *ses = tcon->ses;
4866 int flags = CIFS_OBREAK_OP;
4867 unsigned int total_len;
4869 struct kvec rsp_iov;
4871 __u64 *please_key_high;
4872 __u64 *please_key_low;
4874 cifs_dbg(FYI, "SMB2_lease_break\n");
4875 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4880 if (smb3_encryption_required(tcon))
4881 flags |= CIFS_TRANSFORM_REQ;
4883 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4884 req->StructureSize = cpu_to_le16(36);
4887 memcpy(req->LeaseKey, lease_key, 16);
4888 req->LeaseState = lease_state;
4890 flags |= CIFS_NO_RSP_BUF;
4892 iov[0].iov_base = (char *)req;
4893 iov[0].iov_len = total_len;
4895 memset(&rqst, 0, sizeof(struct smb_rqst));
4899 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4900 cifs_small_buf_release(req);
4902 please_key_low = (__u64 *)lease_key;
4903 please_key_high = (__u64 *)(lease_key+8);
4905 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4906 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
4907 ses->Suid, *please_key_low, *please_key_high, rc);
4908 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
4910 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
4911 ses->Suid, *please_key_low, *please_key_high);