4 * Copyright (C) International Business Machines Corp., 2009, 2013
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
9 * Contains the routines for constructing the SMB2 PDUs themselves
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
32 #include <linux/kernel.h>
33 #include <linux/vfs.h>
34 #include <linux/task_io_accounting_ops.h>
35 #include <linux/uaccess.h>
36 #include <linux/uuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/xattr.h>
42 #include "cifsproto.h"
43 #include "smb2proto.h"
44 #include "cifs_unicode.h"
45 #include "cifs_debug.h"
47 #include "smb2status.h"
50 #include "cifs_spnego.h"
51 #include "smbdirect.h"
53 #ifdef CONFIG_CIFS_DFS_UPCALL
54 #include "dfs_cache.h"
58 * The following table defines the expected "StructureSize" of SMB2 requests
59 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
61 * Note that commands are defined in smb2pdu.h in le16 but the array below is
62 * indexed by command in host byte order.
64 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
65 /* SMB2_NEGOTIATE */ 36,
66 /* SMB2_SESSION_SETUP */ 25,
68 /* SMB2_TREE_CONNECT */ 9,
69 /* SMB2_TREE_DISCONNECT */ 4,
79 /* SMB2_QUERY_DIRECTORY */ 33,
80 /* SMB2_CHANGE_NOTIFY */ 32,
81 /* SMB2_QUERY_INFO */ 41,
82 /* SMB2_SET_INFO */ 33,
83 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
86 int smb3_encryption_required(const struct cifs_tcon *tcon)
90 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
91 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
94 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
100 smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
101 const struct cifs_tcon *tcon)
103 shdr->ProtocolId = SMB2_PROTO_NUMBER;
104 shdr->StructureSize = cpu_to_le16(64);
105 shdr->Command = smb2_cmd;
106 if (tcon && tcon->ses && tcon->ses->server) {
107 struct TCP_Server_Info *server = tcon->ses->server;
109 spin_lock(&server->req_lock);
110 /* Request up to 10 credits but don't go over the limit. */
111 if (server->credits >= server->max_credits)
112 shdr->CreditRequest = cpu_to_le16(0);
114 shdr->CreditRequest = cpu_to_le16(
115 min_t(int, server->max_credits -
116 server->credits, 10));
117 spin_unlock(&server->req_lock);
119 shdr->CreditRequest = cpu_to_le16(2);
121 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
126 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
127 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
128 if ((tcon->ses) && (tcon->ses->server) &&
129 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
130 shdr->CreditCharge = cpu_to_le16(1);
131 /* else CreditCharge MBZ */
133 shdr->TreeId = tcon->tid;
134 /* Uid is not converted */
136 shdr->SessionId = tcon->ses->Suid;
139 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
140 * to pass the path on the Open SMB prefixed by \\server\share.
141 * Not sure when we would need to do the augmented path (if ever) and
142 * setting this flag breaks the SMB2 open operation since it is
143 * illegal to send an empty path name (without \\server\share prefix)
144 * when the DFS flag is set in the SMB open header. We could
145 * consider setting the flag on all operations other than open
146 * but it is safer to net set it for now.
148 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
149 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
151 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
152 !smb3_encryption_required(tcon))
153 shdr->Flags |= SMB2_FLAGS_SIGNED;
158 #ifdef CONFIG_CIFS_DFS_UPCALL
159 static int __smb2_reconnect(const struct nls_table *nlsc,
160 struct cifs_tcon *tcon)
163 struct dfs_cache_tgt_list tl;
164 struct dfs_cache_tgt_iterator *it = NULL;
166 const char *tcp_host;
168 const char *dfs_host;
171 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
176 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
177 tcon->ses->server->hostname);
178 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
182 if (!tcon->dfs_path) {
183 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
187 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl);
191 extract_unc_hostname(tcon->ses->server->hostname, &tcp_host,
194 for (it = dfs_cache_get_tgt_iterator(&tl); it;
195 it = dfs_cache_get_next_tgt(&tl, it)) {
196 const char *tgt = dfs_cache_get_tgt_name(it);
198 extract_unc_hostname(tgt, &dfs_host, &dfs_host_len);
200 if (dfs_host_len != tcp_host_len
201 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
202 cifs_dbg(FYI, "%s: skipping %.*s, doesn't match %.*s",
204 (int)dfs_host_len, dfs_host,
205 (int)tcp_host_len, tcp_host);
209 scnprintf(tree, MAX_TREE_SIZE, "\\%s", tgt);
211 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
220 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1,
225 dfs_cache_free_tgts(&tl);
231 static inline int __smb2_reconnect(const struct nls_table *nlsc,
232 struct cifs_tcon *tcon)
234 return SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
239 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
242 struct nls_table *nls_codepage;
243 struct cifs_ses *ses;
244 struct TCP_Server_Info *server;
248 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
249 * check for tcp and smb session status done differently
250 * for those three - in the calling routine.
255 if (smb2_command == SMB2_TREE_CONNECT)
258 if (tcon->tidStatus == CifsExiting) {
260 * only tree disconnect, open, and write,
261 * (and ulogoff which does not have tcon)
262 * are allowed as we start force umount.
264 if ((smb2_command != SMB2_WRITE) &&
265 (smb2_command != SMB2_CREATE) &&
266 (smb2_command != SMB2_TREE_DISCONNECT)) {
267 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
272 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
273 (!tcon->ses->server))
277 server = ses->server;
279 retries = server->nr_targets;
282 * Give demultiplex thread up to 10 seconds to each target available for
283 * reconnect -- should be greater than cifs socket timeout which is 7
286 while (server->tcpStatus == CifsNeedReconnect) {
288 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
289 * here since they are implicitly done when session drops.
291 switch (smb2_command) {
293 * BB Should we keep oplock break and add flush to exceptions?
295 case SMB2_TREE_DISCONNECT:
298 case SMB2_OPLOCK_BREAK:
302 rc = wait_event_interruptible_timeout(server->response_q,
303 (server->tcpStatus != CifsNeedReconnect),
306 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
307 " signal by the process\n", __func__);
311 /* are we still trying to reconnect? */
312 if (server->tcpStatus != CifsNeedReconnect)
319 * on "soft" mounts we wait once. Hard mounts keep
320 * retrying until process is killed or server comes
324 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
327 retries = server->nr_targets;
330 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
333 nls_codepage = load_nls_default();
336 * need to prevent multiple threads trying to simultaneously reconnect
337 * the same SMB session
339 mutex_lock(&tcon->ses->session_mutex);
342 * Recheck after acquire mutex. If another thread is negotiating
343 * and the server never sends an answer the socket will be closed
344 * and tcpStatus set to reconnect.
346 if (server->tcpStatus == CifsNeedReconnect) {
348 mutex_unlock(&tcon->ses->session_mutex);
352 rc = cifs_negotiate_protocol(0, tcon->ses);
353 if (!rc && tcon->ses->need_reconnect)
354 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
356 if (rc || !tcon->need_reconnect) {
357 mutex_unlock(&tcon->ses->session_mutex);
361 cifs_mark_open_files_invalid(tcon);
362 if (tcon->use_persistent)
363 tcon->need_reopen_files = true;
365 rc = __smb2_reconnect(nls_codepage, tcon);
366 mutex_unlock(&tcon->ses->session_mutex);
368 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
370 /* If sess reconnected but tcon didn't, something strange ... */
371 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
375 if (smb2_command != SMB2_INTERNAL_CMD)
376 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
378 atomic_inc(&tconInfoReconnectCount);
381 * Check if handle based operation so we know whether we can continue
382 * or not without returning to caller to reset file handle.
385 * BB Is flush done by server on drop of tcp session? Should we special
386 * case it and skip above?
388 switch (smb2_command) {
394 case SMB2_QUERY_DIRECTORY:
395 case SMB2_CHANGE_NOTIFY:
396 case SMB2_QUERY_INFO:
400 unload_nls(nls_codepage);
405 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
406 unsigned int *total_len)
408 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
409 /* lookup word count ie StructureSize from table */
410 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
413 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
414 * largest operations (Create)
418 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
419 spdu->StructureSize2 = cpu_to_le16(parmsize);
421 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
425 * Allocate and return pointer to an SMB request hdr, and set basic
426 * SMB information in the SMB header. If the return code is zero, this
427 * function must have filled in request_buf pointer.
430 smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
431 void **request_buf, unsigned int *total_len)
435 rc = smb2_reconnect(smb2_command, tcon);
439 /* BB eventually switch this to SMB2 specific small buf size */
440 if (smb2_command == SMB2_SET_INFO)
441 *request_buf = cifs_buf_get();
443 *request_buf = cifs_small_buf_get();
444 if (*request_buf == NULL) {
445 /* BB should we add a retry in here if not a writepage? */
449 fill_small_buf(smb2_command, tcon,
450 (struct smb2_sync_hdr *)(*request_buf),
454 uint16_t com_code = le16_to_cpu(smb2_command);
455 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
456 cifs_stats_inc(&tcon->num_smbs_sent);
463 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
464 #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
465 #define SMB2_POSIX_EXTENSIONS_AVAILABLE cpu_to_le16(0x100)
468 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
470 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
471 pneg_ctxt->DataLength = cpu_to_le16(38);
472 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
473 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
474 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
475 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
479 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
481 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
482 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + le16 cipher */
483 pneg_ctxt->CipherCount = cpu_to_le16(1);
484 /* pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;*/ /* not supported yet */
485 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_CCM;
489 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
491 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
492 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
493 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
494 pneg_ctxt->Name[0] = 0x93;
495 pneg_ctxt->Name[1] = 0xAD;
496 pneg_ctxt->Name[2] = 0x25;
497 pneg_ctxt->Name[3] = 0x50;
498 pneg_ctxt->Name[4] = 0x9C;
499 pneg_ctxt->Name[5] = 0xB4;
500 pneg_ctxt->Name[6] = 0x11;
501 pneg_ctxt->Name[7] = 0xE7;
502 pneg_ctxt->Name[8] = 0xB4;
503 pneg_ctxt->Name[9] = 0x23;
504 pneg_ctxt->Name[10] = 0x83;
505 pneg_ctxt->Name[11] = 0xDE;
506 pneg_ctxt->Name[12] = 0x96;
507 pneg_ctxt->Name[13] = 0x8B;
508 pneg_ctxt->Name[14] = 0xCD;
509 pneg_ctxt->Name[15] = 0x7C;
513 assemble_neg_contexts(struct smb2_negotiate_req *req,
514 unsigned int *total_len)
516 char *pneg_ctxt = (char *)req;
517 unsigned int ctxt_len;
519 if (*total_len > 200) {
520 /* In case length corrupted don't want to overrun smb buffer */
521 cifs_dbg(VFS, "Bad frame length assembling neg contexts\n");
526 * round up total_len of fixed part of SMB3 negotiate request to 8
527 * byte boundary before adding negotiate contexts
529 *total_len = roundup(*total_len, 8);
531 pneg_ctxt = (*total_len) + (char *)req;
532 req->NegotiateContextOffset = cpu_to_le32(*total_len);
534 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
535 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
536 *total_len += ctxt_len;
537 pneg_ctxt += ctxt_len;
539 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
540 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
541 *total_len += ctxt_len;
542 pneg_ctxt += ctxt_len;
544 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
545 *total_len += sizeof(struct smb2_posix_neg_context);
547 req->NegotiateContextCount = cpu_to_le16(3);
550 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
552 unsigned int len = le16_to_cpu(ctxt->DataLength);
554 /* If invalid preauth context warn but use what we requested, SHA-512 */
555 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
556 printk_once(KERN_WARNING "server sent bad preauth context\n");
559 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
560 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
561 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
562 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
565 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
566 struct smb2_encryption_neg_context *ctxt)
568 unsigned int len = le16_to_cpu(ctxt->DataLength);
570 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
571 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
572 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
576 if (le16_to_cpu(ctxt->CipherCount) != 1) {
577 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
580 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
581 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
582 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
583 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
586 server->cipher_type = ctxt->Ciphers[0];
587 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
591 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
592 struct TCP_Server_Info *server,
593 unsigned int len_of_smb)
595 struct smb2_neg_context *pctx;
596 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
597 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
598 unsigned int len_of_ctxts, i;
601 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
602 if (len_of_smb <= offset) {
603 cifs_dbg(VFS, "Invalid response: negotiate context offset\n");
607 len_of_ctxts = len_of_smb - offset;
609 for (i = 0; i < ctxt_cnt; i++) {
611 /* check that offset is not beyond end of SMB */
612 if (len_of_ctxts == 0)
615 if (len_of_ctxts < sizeof(struct smb2_neg_context))
618 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
619 clen = le16_to_cpu(pctx->DataLength);
620 if (clen > len_of_ctxts)
623 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
624 decode_preauth_context(
625 (struct smb2_preauth_neg_context *)pctx);
626 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
627 rc = decode_encrypt_ctx(server,
628 (struct smb2_encryption_neg_context *)pctx);
629 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
630 server->posix_ext_supported = true;
632 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
633 le16_to_cpu(pctx->ContextType));
637 /* offsets must be 8 byte aligned */
638 clen = (clen + 7) & ~0x7;
639 offset += clen + sizeof(struct smb2_neg_context);
640 len_of_ctxts -= clen;
645 static struct create_posix *
646 create_posix_buf(umode_t mode)
648 struct create_posix *buf;
650 buf = kzalloc(sizeof(struct create_posix),
655 buf->ccontext.DataOffset =
656 cpu_to_le16(offsetof(struct create_posix, Mode));
657 buf->ccontext.DataLength = cpu_to_le32(4);
658 buf->ccontext.NameOffset =
659 cpu_to_le16(offsetof(struct create_posix, Name));
660 buf->ccontext.NameLength = cpu_to_le16(16);
662 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
673 buf->Name[10] = 0x83;
674 buf->Name[11] = 0xDE;
675 buf->Name[12] = 0x96;
676 buf->Name[13] = 0x8B;
677 buf->Name[14] = 0xCD;
678 buf->Name[15] = 0x7C;
679 buf->Mode = cpu_to_le32(mode);
680 cifs_dbg(FYI, "mode on posix create 0%o", mode);
685 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
687 struct smb2_create_req *req = iov[0].iov_base;
688 unsigned int num = *num_iovec;
690 iov[num].iov_base = create_posix_buf(mode);
691 if (iov[num].iov_base == NULL)
693 iov[num].iov_len = sizeof(struct create_posix);
694 if (!req->CreateContextsOffset)
695 req->CreateContextsOffset = cpu_to_le32(
696 sizeof(struct smb2_create_req) +
697 iov[num - 1].iov_len);
698 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
699 *num_iovec = num + 1;
706 * SMB2 Worker functions follow:
708 * The general structure of the worker functions is:
709 * 1) Call smb2_init (assembles SMB2 header)
710 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
711 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
712 * 4) Decode SMB2 command specific fields in the fixed length area
713 * 5) Decode variable length data area (if any for this SMB2 command type)
714 * 6) Call free smb buffer
720 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
722 struct smb_rqst rqst;
723 struct smb2_negotiate_req *req;
724 struct smb2_negotiate_rsp *rsp;
729 struct TCP_Server_Info *server = ses->server;
730 int blob_offset, blob_length;
732 int flags = CIFS_NEG_OP;
733 unsigned int total_len;
735 cifs_dbg(FYI, "Negotiate protocol\n");
738 WARN(1, "%s: server is NULL!\n", __func__);
742 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
746 req->sync_hdr.SessionId = 0;
748 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
749 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
751 if (strcmp(ses->server->vals->version_string,
752 SMB3ANY_VERSION_STRING) == 0) {
753 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
754 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
755 req->DialectCount = cpu_to_le16(2);
757 } else if (strcmp(ses->server->vals->version_string,
758 SMBDEFAULT_VERSION_STRING) == 0) {
759 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
760 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
761 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
762 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
763 req->DialectCount = cpu_to_le16(4);
766 /* otherwise send specific dialect */
767 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
768 req->DialectCount = cpu_to_le16(1);
772 /* only one of SMB2 signing flags may be set in SMB2 request */
774 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
775 else if (global_secflags & CIFSSEC_MAY_SIGN)
776 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
778 req->SecurityMode = 0;
780 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
782 /* ClientGUID must be zero for SMB2.02 dialect */
783 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
784 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
786 memcpy(req->ClientGUID, server->client_guid,
787 SMB2_CLIENT_GUID_SIZE);
788 if ((ses->server->vals->protocol_id == SMB311_PROT_ID) ||
789 (strcmp(ses->server->vals->version_string,
790 SMBDEFAULT_VERSION_STRING) == 0))
791 assemble_neg_contexts(req, &total_len);
793 iov[0].iov_base = (char *)req;
794 iov[0].iov_len = total_len;
796 memset(&rqst, 0, sizeof(struct smb_rqst));
800 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
801 cifs_small_buf_release(req);
802 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
804 * No tcon so can't do
805 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
807 if (rc == -EOPNOTSUPP) {
808 cifs_dbg(VFS, "Dialect not supported by server. Consider "
809 "specifying vers=1.0 or vers=2.0 on mount for accessing"
815 if (strcmp(ses->server->vals->version_string,
816 SMB3ANY_VERSION_STRING) == 0) {
817 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
819 "SMB2 dialect returned but not requested\n");
821 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
823 "SMB2.1 dialect returned but not requested\n");
826 } else if (strcmp(ses->server->vals->version_string,
827 SMBDEFAULT_VERSION_STRING) == 0) {
828 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
830 "SMB2 dialect returned but not requested\n");
832 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
833 /* ops set to 3.0 by default for default so update */
834 ses->server->ops = &smb21_operations;
835 ses->server->vals = &smb21_values;
836 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
837 ses->server->ops = &smb311_operations;
838 ses->server->vals = &smb311_values;
840 } else if (le16_to_cpu(rsp->DialectRevision) !=
841 ses->server->vals->protocol_id) {
842 /* if requested single dialect ensure returned dialect matched */
843 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
844 le16_to_cpu(rsp->DialectRevision));
848 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
850 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
851 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
852 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
853 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
854 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
855 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
856 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
857 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
858 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
859 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
861 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
862 le16_to_cpu(rsp->DialectRevision));
866 server->dialect = le16_to_cpu(rsp->DialectRevision);
869 * Keep a copy of the hash after negprot. This hash will be
870 * the starting hash value for all sessions made from this
873 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
874 SMB2_PREAUTH_HASH_SIZE);
876 /* SMB2 only has an extended negflavor */
877 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
878 /* set it to the maximum buffer size value we can send with 1 credit */
879 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
880 SMB2_MAX_BUFFER_SIZE);
881 server->max_read = le32_to_cpu(rsp->MaxReadSize);
882 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
883 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
884 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
885 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
887 server->capabilities = le32_to_cpu(rsp->Capabilities);
889 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
891 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
892 (struct smb2_sync_hdr *)rsp);
894 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
896 * ses->sectype = RawNTLMSSP;
897 * but for time being this is our only auth choice so doesn't matter.
898 * We just found a server which sets blob length to zero expecting raw.
900 if (blob_length == 0) {
901 cifs_dbg(FYI, "missing security blob on negprot\n");
902 server->sec_ntlmssp = true;
905 rc = cifs_enable_signing(server, ses->sign);
909 rc = decode_negTokenInit(security_blob, blob_length, server);
916 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
917 if (rsp->NegotiateContextCount)
918 rc = smb311_decode_neg_context(rsp, server,
921 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
924 free_rsp_buf(resp_buftype, rsp);
928 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
931 struct validate_negotiate_info_req *pneg_inbuf;
932 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
934 u32 inbuflen; /* max of 4 dialects */
936 cifs_dbg(FYI, "validate negotiate\n");
938 /* In SMB3.11 preauth integrity supersedes validate negotiate */
939 if (tcon->ses->server->dialect == SMB311_PROT_ID)
943 * validation ioctl must be signed, so no point sending this if we
944 * can not sign it (ie are not known user). Even if signing is not
945 * required (enabled but not negotiated), in those cases we selectively
946 * sign just this, the first and only signed request on a connection.
947 * Having validation of negotiate info helps reduce attack vectors.
949 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
950 return 0; /* validation requires signing */
952 if (tcon->ses->user_name == NULL) {
953 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
954 return 0; /* validation requires signing */
957 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
958 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
960 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
964 pneg_inbuf->Capabilities =
965 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
966 memcpy(pneg_inbuf->Guid, tcon->ses->server->client_guid,
967 SMB2_CLIENT_GUID_SIZE);
970 pneg_inbuf->SecurityMode =
971 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
972 else if (global_secflags & CIFSSEC_MAY_SIGN)
973 pneg_inbuf->SecurityMode =
974 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
976 pneg_inbuf->SecurityMode = 0;
979 if (strcmp(tcon->ses->server->vals->version_string,
980 SMB3ANY_VERSION_STRING) == 0) {
981 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
982 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
983 pneg_inbuf->DialectCount = cpu_to_le16(2);
984 /* structure is big enough for 3 dialects, sending only 2 */
985 inbuflen = sizeof(*pneg_inbuf) -
986 (2 * sizeof(pneg_inbuf->Dialects[0]));
987 } else if (strcmp(tcon->ses->server->vals->version_string,
988 SMBDEFAULT_VERSION_STRING) == 0) {
989 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
990 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
991 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
992 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
993 pneg_inbuf->DialectCount = cpu_to_le16(4);
994 /* structure is big enough for 3 dialects */
995 inbuflen = sizeof(*pneg_inbuf);
997 /* otherwise specific dialect was requested */
998 pneg_inbuf->Dialects[0] =
999 cpu_to_le16(tcon->ses->server->vals->protocol_id);
1000 pneg_inbuf->DialectCount = cpu_to_le16(1);
1001 /* structure is big enough for 3 dialects, sending only 1 */
1002 inbuflen = sizeof(*pneg_inbuf) -
1003 sizeof(pneg_inbuf->Dialects[0]) * 2;
1006 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1007 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
1008 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1009 (char **)&pneg_rsp, &rsplen);
1010 if (rc == -EOPNOTSUPP) {
1012 * Old Windows versions or Netapp SMB server can return
1013 * not supported error. Client should accept it.
1015 cifs_dbg(VFS, "Server does not support validate negotiate\n");
1017 } else if (rc != 0) {
1018 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
1020 goto out_free_inbuf;
1024 if (rsplen != sizeof(*pneg_rsp)) {
1025 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
1028 /* relax check since Mac returns max bufsize allowed on ioctl */
1029 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1033 /* check validate negotiate info response matches what we got earlier */
1034 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
1037 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
1040 /* do not validate server guid because not saved at negprot time yet */
1042 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1043 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
1046 /* validate negotiate successful */
1048 cifs_dbg(FYI, "validate negotiate info successful\n");
1052 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1061 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1063 switch (requested) {
1070 if (server->sec_ntlmssp &&
1071 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1073 if ((server->sec_kerberos || server->sec_mskerberos) &&
1074 (global_secflags & CIFSSEC_MAY_KRB5))
1082 struct SMB2_sess_data {
1084 struct cifs_ses *ses;
1085 struct nls_table *nls_cp;
1086 void (*func)(struct SMB2_sess_data *);
1088 u64 previous_session;
1090 /* we will send the SMB in three pieces:
1091 * a fixed length beginning part, an optional
1092 * SPNEGO blob (which can be zero length), and a
1093 * last part which will include the strings
1094 * and rest of bcc area. This allows us to avoid
1095 * a large buffer 17K allocation
1102 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1105 struct cifs_ses *ses = sess_data->ses;
1106 struct smb2_sess_setup_req *req;
1107 struct TCP_Server_Info *server = ses->server;
1108 unsigned int total_len;
1110 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1115 /* First session, not a reauthenticate */
1116 req->sync_hdr.SessionId = 0;
1118 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
1119 req->PreviousSessionId = sess_data->previous_session;
1121 req->Flags = 0; /* MBZ */
1123 /* enough to enable echos and oplocks and one max size write */
1124 req->sync_hdr.CreditRequest = cpu_to_le16(130);
1126 /* only one of SMB2 signing flags may be set in SMB2 request */
1128 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1129 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1130 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1132 req->SecurityMode = 0;
1134 req->Capabilities = 0;
1135 req->Channel = 0; /* MBZ */
1137 sess_data->iov[0].iov_base = (char *)req;
1139 sess_data->iov[0].iov_len = total_len - 1;
1141 * This variable will be used to clear the buffer
1142 * allocated above in case of any error in the calling function.
1144 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1150 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1152 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1153 sess_data->buf0_type = CIFS_NO_BUFFER;
1157 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1160 struct smb_rqst rqst;
1161 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1162 struct kvec rsp_iov = { NULL, 0 };
1164 /* Testing shows that buffer offset must be at location of Buffer[0] */
1165 req->SecurityBufferOffset =
1166 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1167 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1169 memset(&rqst, 0, sizeof(struct smb_rqst));
1170 rqst.rq_iov = sess_data->iov;
1173 /* BB add code to build os and lm fields */
1174 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1176 &sess_data->buf0_type,
1177 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
1178 cifs_small_buf_release(sess_data->iov[0].iov_base);
1179 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1185 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1188 struct cifs_ses *ses = sess_data->ses;
1190 mutex_lock(&ses->server->srv_mutex);
1191 if (ses->server->ops->generate_signingkey) {
1192 rc = ses->server->ops->generate_signingkey(ses);
1195 "SMB3 session key generation failed\n");
1196 mutex_unlock(&ses->server->srv_mutex);
1200 if (!ses->server->session_estab) {
1201 ses->server->sequence_number = 0x2;
1202 ses->server->session_estab = true;
1204 mutex_unlock(&ses->server->srv_mutex);
1206 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1207 spin_lock(&GlobalMid_Lock);
1208 ses->status = CifsGood;
1209 ses->need_reconnect = false;
1210 spin_unlock(&GlobalMid_Lock);
1214 #ifdef CONFIG_CIFS_UPCALL
1216 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1219 struct cifs_ses *ses = sess_data->ses;
1220 struct cifs_spnego_msg *msg;
1221 struct key *spnego_key = NULL;
1222 struct smb2_sess_setup_rsp *rsp = NULL;
1224 rc = SMB2_sess_alloc_buffer(sess_data);
1228 spnego_key = cifs_get_spnego_key(ses);
1229 if (IS_ERR(spnego_key)) {
1230 rc = PTR_ERR(spnego_key);
1235 msg = spnego_key->payload.data[0];
1237 * check version field to make sure that cifs.upcall is
1238 * sending us a response in an expected form
1240 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1242 "bad cifs.upcall version. Expected %d got %d",
1243 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1245 goto out_put_spnego_key;
1248 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1250 if (!ses->auth_key.response) {
1252 "Kerberos can't allocate (%u bytes) memory",
1255 goto out_put_spnego_key;
1257 ses->auth_key.len = msg->sesskey_len;
1259 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1260 sess_data->iov[1].iov_len = msg->secblob_len;
1262 rc = SMB2_sess_sendreceive(sess_data);
1264 goto out_put_spnego_key;
1266 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1267 ses->Suid = rsp->sync_hdr.SessionId;
1269 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1271 rc = SMB2_sess_establish_session(sess_data);
1273 key_invalidate(spnego_key);
1274 key_put(spnego_key);
1276 sess_data->result = rc;
1277 sess_data->func = NULL;
1278 SMB2_sess_free_buffer(sess_data);
1282 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1284 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1285 sess_data->result = -EOPNOTSUPP;
1286 sess_data->func = NULL;
1291 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1294 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1297 struct cifs_ses *ses = sess_data->ses;
1298 struct smb2_sess_setup_rsp *rsp = NULL;
1299 char *ntlmssp_blob = NULL;
1300 bool use_spnego = false; /* else use raw ntlmssp */
1301 u16 blob_length = 0;
1304 * If memory allocation is successful, caller of this function
1307 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1308 if (!ses->ntlmssp) {
1312 ses->ntlmssp->sesskey_per_smbsess = true;
1314 rc = SMB2_sess_alloc_buffer(sess_data);
1318 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1320 if (ntlmssp_blob == NULL) {
1325 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1327 /* BB eventually need to add this */
1328 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1332 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1333 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1335 sess_data->iov[1].iov_base = ntlmssp_blob;
1336 sess_data->iov[1].iov_len = blob_length;
1338 rc = SMB2_sess_sendreceive(sess_data);
1339 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1341 /* If true, rc here is expected and not an error */
1342 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1343 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1349 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1350 le16_to_cpu(rsp->SecurityBufferOffset)) {
1351 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1352 le16_to_cpu(rsp->SecurityBufferOffset));
1356 rc = decode_ntlmssp_challenge(rsp->Buffer,
1357 le16_to_cpu(rsp->SecurityBufferLength), ses);
1361 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1364 ses->Suid = rsp->sync_hdr.SessionId;
1365 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1368 kfree(ntlmssp_blob);
1369 SMB2_sess_free_buffer(sess_data);
1371 sess_data->result = 0;
1372 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1376 kfree(ses->ntlmssp);
1377 ses->ntlmssp = NULL;
1378 sess_data->result = rc;
1379 sess_data->func = NULL;
1383 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1386 struct cifs_ses *ses = sess_data->ses;
1387 struct smb2_sess_setup_req *req;
1388 struct smb2_sess_setup_rsp *rsp = NULL;
1389 unsigned char *ntlmssp_blob = NULL;
1390 bool use_spnego = false; /* else use raw ntlmssp */
1391 u16 blob_length = 0;
1393 rc = SMB2_sess_alloc_buffer(sess_data);
1397 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1398 req->sync_hdr.SessionId = ses->Suid;
1400 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1403 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1408 /* BB eventually need to add this */
1409 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1413 sess_data->iov[1].iov_base = ntlmssp_blob;
1414 sess_data->iov[1].iov_len = blob_length;
1416 rc = SMB2_sess_sendreceive(sess_data);
1420 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1422 ses->Suid = rsp->sync_hdr.SessionId;
1423 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1425 rc = SMB2_sess_establish_session(sess_data);
1427 kfree(ntlmssp_blob);
1428 SMB2_sess_free_buffer(sess_data);
1429 kfree(ses->ntlmssp);
1430 ses->ntlmssp = NULL;
1431 sess_data->result = rc;
1432 sess_data->func = NULL;
1436 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1440 type = smb2_select_sectype(ses->server, ses->sectype);
1441 cifs_dbg(FYI, "sess setup type %d\n", type);
1442 if (type == Unspecified) {
1444 "Unable to select appropriate authentication method!");
1450 sess_data->func = SMB2_auth_kerberos;
1453 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1456 cifs_dbg(VFS, "secType %d not supported!\n", type);
1464 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1465 const struct nls_table *nls_cp)
1468 struct TCP_Server_Info *server = ses->server;
1469 struct SMB2_sess_data *sess_data;
1471 cifs_dbg(FYI, "Session Setup\n");
1474 WARN(1, "%s: server is NULL!\n", __func__);
1478 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1482 rc = SMB2_select_sec(ses, sess_data);
1485 sess_data->xid = xid;
1486 sess_data->ses = ses;
1487 sess_data->buf0_type = CIFS_NO_BUFFER;
1488 sess_data->nls_cp = (struct nls_table *) nls_cp;
1489 sess_data->previous_session = ses->Suid;
1492 * Initialize the session hash with the server one.
1494 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1495 SMB2_PREAUTH_HASH_SIZE);
1497 while (sess_data->func)
1498 sess_data->func(sess_data);
1500 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1501 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
1502 rc = sess_data->result;
1509 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1511 struct smb_rqst rqst;
1512 struct smb2_logoff_req *req; /* response is also trivial struct */
1514 struct TCP_Server_Info *server;
1516 unsigned int total_len;
1518 struct kvec rsp_iov;
1521 cifs_dbg(FYI, "disconnect session %p\n", ses);
1523 if (ses && (ses->server))
1524 server = ses->server;
1528 /* no need to send SMB logoff if uid already closed due to reconnect */
1529 if (ses->need_reconnect)
1530 goto smb2_session_already_dead;
1532 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
1536 /* since no tcon, smb2_init can not do this, so do here */
1537 req->sync_hdr.SessionId = ses->Suid;
1539 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1540 flags |= CIFS_TRANSFORM_REQ;
1541 else if (server->sign)
1542 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1544 flags |= CIFS_NO_RESP;
1546 iov[0].iov_base = (char *)req;
1547 iov[0].iov_len = total_len;
1549 memset(&rqst, 0, sizeof(struct smb_rqst));
1553 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1554 cifs_small_buf_release(req);
1556 * No tcon so can't do
1557 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1560 smb2_session_already_dead:
1564 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1566 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1569 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1571 /* These are similar values to what Windows uses */
1572 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1574 tcon->max_chunks = 256;
1575 tcon->max_bytes_chunk = 1048576;
1576 tcon->max_bytes_copy = 16777216;
1580 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1581 struct cifs_tcon *tcon, const struct nls_table *cp)
1583 struct smb_rqst rqst;
1584 struct smb2_tree_connect_req *req;
1585 struct smb2_tree_connect_rsp *rsp = NULL;
1587 struct kvec rsp_iov = { NULL, 0 };
1591 __le16 *unc_path = NULL;
1593 unsigned int total_len;
1595 cifs_dbg(FYI, "TCON\n");
1597 if (!(ses->server) || !tree)
1600 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1601 if (unc_path == NULL)
1604 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1606 if (unc_path_len < 2) {
1611 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1613 atomic_set(&tcon->num_remote_opens, 0);
1614 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1621 if (smb3_encryption_required(tcon))
1622 flags |= CIFS_TRANSFORM_REQ;
1624 iov[0].iov_base = (char *)req;
1626 iov[0].iov_len = total_len - 1;
1628 /* Testing shows that buffer offset must be at location of Buffer[0] */
1629 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1631 req->PathLength = cpu_to_le16(unc_path_len - 2);
1632 iov[1].iov_base = unc_path;
1633 iov[1].iov_len = unc_path_len;
1636 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1637 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1638 * (Samba servers don't always set the flag so also check if null user)
1640 if ((ses->server->dialect == SMB311_PROT_ID) &&
1641 !smb3_encryption_required(tcon) &&
1642 !(ses->session_flags &
1643 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1644 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1645 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1647 memset(&rqst, 0, sizeof(struct smb_rqst));
1651 /* Need 64 for max size write so ask for more in case not there yet */
1652 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1654 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
1655 cifs_small_buf_release(req);
1656 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1657 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1660 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1661 tcon->need_reconnect = true;
1663 goto tcon_error_exit;
1666 switch (rsp->ShareType) {
1667 case SMB2_SHARE_TYPE_DISK:
1668 cifs_dbg(FYI, "connection to disk share\n");
1670 case SMB2_SHARE_TYPE_PIPE:
1672 cifs_dbg(FYI, "connection to pipe share\n");
1674 case SMB2_SHARE_TYPE_PRINT:
1676 cifs_dbg(FYI, "connection to printer\n");
1679 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1681 goto tcon_error_exit;
1684 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1685 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1686 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1687 tcon->tidStatus = CifsGood;
1688 tcon->need_reconnect = false;
1689 tcon->tid = rsp->sync_hdr.TreeId;
1690 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1692 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1693 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1694 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1697 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1698 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1700 init_copy_chunk_defaults(tcon);
1701 if (tcon->ses->server->ops->validate_negotiate)
1702 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1705 free_rsp_buf(resp_buftype, rsp);
1710 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1711 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1717 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1719 struct smb_rqst rqst;
1720 struct smb2_tree_disconnect_req *req; /* response is trivial */
1722 struct cifs_ses *ses = tcon->ses;
1724 unsigned int total_len;
1726 struct kvec rsp_iov;
1729 cifs_dbg(FYI, "Tree Disconnect\n");
1731 if (!ses || !(ses->server))
1734 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1737 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1742 if (smb3_encryption_required(tcon))
1743 flags |= CIFS_TRANSFORM_REQ;
1745 flags |= CIFS_NO_RESP;
1747 iov[0].iov_base = (char *)req;
1748 iov[0].iov_len = total_len;
1750 memset(&rqst, 0, sizeof(struct smb_rqst));
1754 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1755 cifs_small_buf_release(req);
1757 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1763 static struct create_durable *
1764 create_durable_buf(void)
1766 struct create_durable *buf;
1768 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1772 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1773 (struct create_durable, Data));
1774 buf->ccontext.DataLength = cpu_to_le32(16);
1775 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1776 (struct create_durable, Name));
1777 buf->ccontext.NameLength = cpu_to_le16(4);
1778 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1786 static struct create_durable *
1787 create_reconnect_durable_buf(struct cifs_fid *fid)
1789 struct create_durable *buf;
1791 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1795 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1796 (struct create_durable, Data));
1797 buf->ccontext.DataLength = cpu_to_le32(16);
1798 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1799 (struct create_durable, Name));
1800 buf->ccontext.NameLength = cpu_to_le16(4);
1801 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1802 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1803 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1812 smb2_parse_lease_state(struct TCP_Server_Info *server,
1813 struct smb2_create_rsp *rsp,
1814 unsigned int *epoch, char *lease_key)
1817 struct create_context *cc;
1819 unsigned int remaining;
1822 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
1823 remaining = le32_to_cpu(rsp->CreateContextsLength);
1824 cc = (struct create_context *)data_offset;
1825 while (remaining >= sizeof(struct create_context)) {
1826 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1827 if (le16_to_cpu(cc->NameLength) == 4 &&
1828 strncmp(name, "RqLs", 4) == 0)
1829 return server->ops->parse_lease_buf(cc, epoch,
1832 next = le32_to_cpu(cc->Next);
1836 cc = (struct create_context *)((char *)cc + next);
1843 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1844 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
1846 struct smb2_create_req *req = iov[0].iov_base;
1847 unsigned int num = *num_iovec;
1849 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
1850 if (iov[num].iov_base == NULL)
1852 iov[num].iov_len = server->vals->create_lease_size;
1853 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1854 if (!req->CreateContextsOffset)
1855 req->CreateContextsOffset = cpu_to_le32(
1856 sizeof(struct smb2_create_req) +
1857 iov[num - 1].iov_len);
1858 le32_add_cpu(&req->CreateContextsLength,
1859 server->vals->create_lease_size);
1860 *num_iovec = num + 1;
1864 static struct create_durable_v2 *
1865 create_durable_v2_buf(struct cifs_open_parms *oparms)
1867 struct cifs_fid *pfid = oparms->fid;
1868 struct create_durable_v2 *buf;
1870 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1874 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1875 (struct create_durable_v2, dcontext));
1876 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1877 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1878 (struct create_durable_v2, Name));
1879 buf->ccontext.NameLength = cpu_to_le16(4);
1882 * NB: Handle timeout defaults to 0, which allows server to choose
1883 * (most servers default to 120 seconds) and most clients default to 0.
1884 * This can be overridden at mount ("handletimeout=") if the user wants
1885 * a different persistent (or resilient) handle timeout for all opens
1886 * opens on a particular SMB3 mount.
1888 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
1889 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1890 generate_random_uuid(buf->dcontext.CreateGuid);
1891 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1893 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1901 static struct create_durable_handle_reconnect_v2 *
1902 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1904 struct create_durable_handle_reconnect_v2 *buf;
1906 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1911 buf->ccontext.DataOffset =
1912 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1914 buf->ccontext.DataLength =
1915 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1916 buf->ccontext.NameOffset =
1917 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1919 buf->ccontext.NameLength = cpu_to_le16(4);
1921 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1922 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1923 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1924 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1926 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1935 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1936 struct cifs_open_parms *oparms)
1938 struct smb2_create_req *req = iov[0].iov_base;
1939 unsigned int num = *num_iovec;
1941 iov[num].iov_base = create_durable_v2_buf(oparms);
1942 if (iov[num].iov_base == NULL)
1944 iov[num].iov_len = sizeof(struct create_durable_v2);
1945 if (!req->CreateContextsOffset)
1946 req->CreateContextsOffset =
1947 cpu_to_le32(sizeof(struct smb2_create_req) +
1949 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1950 *num_iovec = num + 1;
1955 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1956 struct cifs_open_parms *oparms)
1958 struct smb2_create_req *req = iov[0].iov_base;
1959 unsigned int num = *num_iovec;
1961 /* indicate that we don't need to relock the file */
1962 oparms->reconnect = false;
1964 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1965 if (iov[num].iov_base == NULL)
1967 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1968 if (!req->CreateContextsOffset)
1969 req->CreateContextsOffset =
1970 cpu_to_le32(sizeof(struct smb2_create_req) +
1972 le32_add_cpu(&req->CreateContextsLength,
1973 sizeof(struct create_durable_handle_reconnect_v2));
1974 *num_iovec = num + 1;
1979 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1980 struct cifs_open_parms *oparms, bool use_persistent)
1982 struct smb2_create_req *req = iov[0].iov_base;
1983 unsigned int num = *num_iovec;
1985 if (use_persistent) {
1986 if (oparms->reconnect)
1987 return add_durable_reconnect_v2_context(iov, num_iovec,
1990 return add_durable_v2_context(iov, num_iovec, oparms);
1993 if (oparms->reconnect) {
1994 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1995 /* indicate that we don't need to relock the file */
1996 oparms->reconnect = false;
1998 iov[num].iov_base = create_durable_buf();
1999 if (iov[num].iov_base == NULL)
2001 iov[num].iov_len = sizeof(struct create_durable);
2002 if (!req->CreateContextsOffset)
2003 req->CreateContextsOffset =
2004 cpu_to_le32(sizeof(struct smb2_create_req) +
2006 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
2007 *num_iovec = num + 1;
2011 /* See MS-SMB2 2.2.13.2.7 */
2012 static struct crt_twarp_ctxt *
2013 create_twarp_buf(__u64 timewarp)
2015 struct crt_twarp_ctxt *buf;
2017 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2021 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2022 (struct crt_twarp_ctxt, Timestamp));
2023 buf->ccontext.DataLength = cpu_to_le32(8);
2024 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2025 (struct crt_twarp_ctxt, Name));
2026 buf->ccontext.NameLength = cpu_to_le16(4);
2027 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2032 buf->Timestamp = cpu_to_le64(timewarp);
2036 /* See MS-SMB2 2.2.13.2.7 */
2038 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2040 struct smb2_create_req *req = iov[0].iov_base;
2041 unsigned int num = *num_iovec;
2043 iov[num].iov_base = create_twarp_buf(timewarp);
2044 if (iov[num].iov_base == NULL)
2046 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2047 if (!req->CreateContextsOffset)
2048 req->CreateContextsOffset = cpu_to_le32(
2049 sizeof(struct smb2_create_req) +
2050 iov[num - 1].iov_len);
2051 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2052 *num_iovec = num + 1;
2057 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2058 const char *treename, const __le16 *path)
2060 int treename_len, path_len;
2061 struct nls_table *cp;
2062 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2067 treename_len = strlen(treename);
2068 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2074 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2077 * make room for one path separator between the treename and
2080 *out_len = treename_len + 1 + path_len;
2083 * final path needs to be null-terminated UTF16 with a
2087 *out_size = roundup((*out_len+1)*2, 8);
2088 *out_path = kzalloc(*out_size, GFP_KERNEL);
2092 cp = load_nls_default();
2093 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2094 UniStrcat(*out_path, sep);
2095 UniStrcat(*out_path, path);
2101 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2102 umode_t mode, struct cifs_tcon *tcon,
2103 const char *full_path,
2104 struct cifs_sb_info *cifs_sb)
2106 struct smb_rqst rqst;
2107 struct smb2_create_req *req;
2108 struct smb2_create_rsp *rsp = NULL;
2109 struct cifs_ses *ses = tcon->ses;
2110 struct kvec iov[3]; /* make sure at least one for each open context */
2111 struct kvec rsp_iov = {NULL, 0};
2114 __le16 *copy_path = NULL;
2117 unsigned int n_iov = 2;
2118 __u32 file_attributes = 0;
2119 char *pc_buf = NULL;
2121 unsigned int total_len;
2122 __le16 *utf16_path = NULL;
2124 cifs_dbg(FYI, "mkdir\n");
2126 /* resource #1: path allocation */
2127 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2131 if (!ses || !(ses->server)) {
2136 /* resource #2: request */
2137 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2142 if (smb3_encryption_required(tcon))
2143 flags |= CIFS_TRANSFORM_REQ;
2145 req->ImpersonationLevel = IL_IMPERSONATION;
2146 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2147 /* File attributes ignored on open (used in create though) */
2148 req->FileAttributes = cpu_to_le32(file_attributes);
2149 req->ShareAccess = FILE_SHARE_ALL_LE;
2150 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2151 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2153 iov[0].iov_base = (char *)req;
2154 /* -1 since last byte is buf[0] which is sent below (path) */
2155 iov[0].iov_len = total_len - 1;
2157 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2159 /* [MS-SMB2] 2.2.13 NameOffset:
2160 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2161 * the SMB2 header, the file name includes a prefix that will
2162 * be processed during DFS name normalization as specified in
2163 * section 3.3.5.9. Otherwise, the file name is relative to
2164 * the share that is identified by the TreeId in the SMB2
2167 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2170 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2171 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2173 tcon->treeName, utf16_path);
2177 req->NameLength = cpu_to_le16(name_len * 2);
2178 uni_path_len = copy_size;
2179 /* free before overwriting resource */
2181 utf16_path = copy_path;
2183 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2184 /* MUST set path len (NameLength) to 0 opening root of share */
2185 req->NameLength = cpu_to_le16(uni_path_len - 2);
2186 if (uni_path_len % 8 != 0) {
2187 copy_size = roundup(uni_path_len, 8);
2188 copy_path = kzalloc(copy_size, GFP_KERNEL);
2193 memcpy((char *)copy_path, (const char *)utf16_path,
2195 uni_path_len = copy_size;
2196 /* free before overwriting resource */
2198 utf16_path = copy_path;
2202 iov[1].iov_len = uni_path_len;
2203 iov[1].iov_base = utf16_path;
2204 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2206 if (tcon->posix_extensions) {
2207 /* resource #3: posix buf */
2208 rc = add_posix_context(iov, &n_iov, mode);
2211 pc_buf = iov[n_iov-1].iov_base;
2215 memset(&rqst, 0, sizeof(struct smb_rqst));
2217 rqst.rq_nvec = n_iov;
2219 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2220 FILE_WRITE_ATTRIBUTES);
2221 /* resource #4: response buffer */
2222 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2224 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2225 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2227 FILE_WRITE_ATTRIBUTES, rc);
2228 goto err_free_rsp_buf;
2231 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2232 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2233 ses->Suid, CREATE_NOT_FILE,
2234 FILE_WRITE_ATTRIBUTES);
2236 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2238 /* Eventually save off posix specific response info and timestaps */
2241 free_rsp_buf(resp_buftype, rsp);
2244 cifs_small_buf_release(req);
2251 SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2252 struct cifs_open_parms *oparms, __le16 *path)
2254 struct TCP_Server_Info *server = tcon->ses->server;
2255 struct smb2_create_req *req;
2256 unsigned int n_iov = 2;
2257 __u32 file_attributes = 0;
2260 unsigned int total_len;
2261 struct kvec *iov = rqst->rq_iov;
2265 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2269 iov[0].iov_base = (char *)req;
2270 /* -1 since last byte is buf[0] which is sent below (path) */
2271 iov[0].iov_len = total_len - 1;
2273 if (oparms->create_options & CREATE_OPTION_READONLY)
2274 file_attributes |= ATTR_READONLY;
2275 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2276 file_attributes |= ATTR_SYSTEM;
2278 req->ImpersonationLevel = IL_IMPERSONATION;
2279 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2280 /* File attributes ignored on open (used in create though) */
2281 req->FileAttributes = cpu_to_le32(file_attributes);
2282 req->ShareAccess = FILE_SHARE_ALL_LE;
2283 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2284 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2285 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2287 /* [MS-SMB2] 2.2.13 NameOffset:
2288 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2289 * the SMB2 header, the file name includes a prefix that will
2290 * be processed during DFS name normalization as specified in
2291 * section 3.3.5.9. Otherwise, the file name is relative to
2292 * the share that is identified by the TreeId in the SMB2
2295 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2298 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2299 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2301 tcon->treeName, path);
2304 req->NameLength = cpu_to_le16(name_len * 2);
2305 uni_path_len = copy_size;
2308 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2309 /* MUST set path len (NameLength) to 0 opening root of share */
2310 req->NameLength = cpu_to_le16(uni_path_len - 2);
2311 copy_size = uni_path_len;
2312 if (copy_size % 8 != 0)
2313 copy_size = roundup(copy_size, 8);
2314 copy_path = kzalloc(copy_size, GFP_KERNEL);
2317 memcpy((char *)copy_path, (const char *)path,
2319 uni_path_len = copy_size;
2323 iov[1].iov_len = uni_path_len;
2324 iov[1].iov_base = path;
2326 if (!server->oplocks)
2327 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2329 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2330 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2331 req->RequestedOplockLevel = *oplock;
2332 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2333 (oparms->create_options & CREATE_NOT_FILE))
2334 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2336 rc = add_lease_context(server, iov, &n_iov,
2337 oparms->fid->lease_key, oplock);
2342 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2343 /* need to set Next field of lease context if we request it */
2344 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2345 struct create_context *ccontext =
2346 (struct create_context *)iov[n_iov-1].iov_base;
2348 cpu_to_le32(server->vals->create_lease_size);
2351 rc = add_durable_context(iov, &n_iov, oparms,
2352 tcon->use_persistent);
2357 if (tcon->posix_extensions) {
2359 struct create_context *ccontext =
2360 (struct create_context *)iov[n_iov-1].iov_base;
2362 cpu_to_le32(iov[n_iov-1].iov_len);
2365 rc = add_posix_context(iov, &n_iov, oparms->mode);
2370 if (tcon->snapshot_time) {
2371 cifs_dbg(FYI, "adding snapshot context\n");
2373 struct create_context *ccontext =
2374 (struct create_context *)iov[n_iov-1].iov_base;
2376 cpu_to_le32(iov[n_iov-1].iov_len);
2379 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2385 rqst->rq_nvec = n_iov;
2389 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2390 * All other vectors are freed by kfree().
2393 SMB2_open_free(struct smb_rqst *rqst)
2397 if (rqst && rqst->rq_iov) {
2398 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2399 for (i = 1; i < rqst->rq_nvec; i++)
2400 if (rqst->rq_iov[i].iov_base != smb2_padding)
2401 kfree(rqst->rq_iov[i].iov_base);
2406 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2407 __u8 *oplock, struct smb2_file_all_info *buf,
2408 struct kvec *err_iov, int *buftype)
2410 struct smb_rqst rqst;
2411 struct smb2_create_rsp *rsp = NULL;
2412 struct TCP_Server_Info *server;
2413 struct cifs_tcon *tcon = oparms->tcon;
2414 struct cifs_ses *ses = tcon->ses;
2415 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2416 struct kvec rsp_iov = {NULL, 0};
2417 int resp_buftype = CIFS_NO_BUFFER;
2421 cifs_dbg(FYI, "create/open\n");
2422 if (ses && (ses->server))
2423 server = ses->server;
2427 if (smb3_encryption_required(tcon))
2428 flags |= CIFS_TRANSFORM_REQ;
2430 memset(&rqst, 0, sizeof(struct smb_rqst));
2431 memset(&iov, 0, sizeof(iov));
2433 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2435 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2439 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2440 oparms->create_options, oparms->desired_access);
2442 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2444 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2447 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2448 if (err_iov && rsp) {
2450 *buftype = resp_buftype;
2451 resp_buftype = CIFS_NO_BUFFER;
2454 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2455 oparms->create_options, oparms->desired_access, rc);
2458 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2459 ses->Suid, oparms->create_options,
2460 oparms->desired_access);
2462 atomic_inc(&tcon->num_remote_opens);
2463 oparms->fid->persistent_fid = rsp->PersistentFileId;
2464 oparms->fid->volatile_fid = rsp->VolatileFileId;
2465 #ifdef CONFIG_CIFS_DEBUG2
2466 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2467 #endif /* CIFS_DEBUG2 */
2470 memcpy(buf, &rsp->CreationTime, 32);
2471 buf->AllocationSize = rsp->AllocationSize;
2472 buf->EndOfFile = rsp->EndofFile;
2473 buf->Attributes = rsp->FileAttributes;
2474 buf->NumberOfLinks = cpu_to_le32(1);
2475 buf->DeletePending = 0;
2478 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
2479 *oplock = smb2_parse_lease_state(server, rsp,
2480 &oparms->fid->epoch,
2481 oparms->fid->lease_key);
2483 *oplock = rsp->OplockLevel;
2485 SMB2_open_free(&rqst);
2486 free_rsp_buf(resp_buftype, rsp);
2491 SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2492 u64 persistent_fid, u64 volatile_fid, u32 opcode,
2493 bool is_fsctl, char *in_data, u32 indatalen,
2494 __u32 max_response_size)
2496 struct smb2_ioctl_req *req;
2497 struct kvec *iov = rqst->rq_iov;
2498 unsigned int total_len;
2501 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
2505 req->CtlCode = cpu_to_le32(opcode);
2506 req->PersistentFileId = persistent_fid;
2507 req->VolatileFileId = volatile_fid;
2509 iov[0].iov_base = (char *)req;
2511 * If no input data, the size of ioctl struct in
2512 * protocol spec still includes a 1 byte data buffer,
2513 * but if input data passed to ioctl, we do not
2514 * want to double count this, so we do not send
2515 * the dummy one byte of data in iovec[0] if sending
2516 * input data (in iovec[1]).
2519 req->InputCount = cpu_to_le32(indatalen);
2520 /* do not set InputOffset if no input data */
2522 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
2524 iov[0].iov_len = total_len - 1;
2525 iov[1].iov_base = in_data;
2526 iov[1].iov_len = indatalen;
2529 iov[0].iov_len = total_len;
2532 req->OutputOffset = 0;
2533 req->OutputCount = 0; /* MBZ */
2536 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2537 * We Could increase default MaxOutputResponse, but that could require
2538 * more credits. Windows typically sets this smaller, but for some
2539 * ioctls it may be useful to allow server to send more. No point
2540 * limiting what the server can send as long as fits in one credit
2541 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2542 * to increase this limit up in the future.
2543 * Note that for snapshot queries that servers like Azure expect that
2544 * the first query be minimal size (and just used to get the number/size
2545 * of previous versions) so response size must be specified as EXACTLY
2546 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2547 * of eight bytes. Currently that is the only case where we set max
2548 * response size smaller.
2550 req->MaxOutputResponse = cpu_to_le32(max_response_size);
2553 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2557 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2558 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
2559 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
2565 SMB2_ioctl_free(struct smb_rqst *rqst)
2567 if (rqst && rqst->rq_iov)
2568 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2573 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2576 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2577 u64 volatile_fid, u32 opcode, bool is_fsctl,
2578 char *in_data, u32 indatalen, u32 max_out_data_len,
2579 char **out_data, u32 *plen /* returned data len */)
2581 struct smb_rqst rqst;
2582 struct smb2_ioctl_rsp *rsp = NULL;
2583 struct cifs_ses *ses;
2584 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
2585 struct kvec rsp_iov = {NULL, 0};
2586 int resp_buftype = CIFS_NO_BUFFER;
2590 cifs_dbg(FYI, "SMB2 IOCTL\n");
2592 if (out_data != NULL)
2595 /* zero out returned data len, in case of error */
2604 if (!ses || !(ses->server))
2607 if (smb3_encryption_required(tcon))
2608 flags |= CIFS_TRANSFORM_REQ;
2610 memset(&rqst, 0, sizeof(struct smb_rqst));
2611 memset(&iov, 0, sizeof(iov));
2613 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
2615 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
2616 is_fsctl, in_data, indatalen, max_out_data_len);
2620 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2622 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
2625 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2626 ses->Suid, 0, opcode, rc);
2628 if ((rc != 0) && (rc != -EINVAL)) {
2629 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2631 } else if (rc == -EINVAL) {
2632 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2633 (opcode != FSCTL_SRV_COPYCHUNK)) {
2634 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2639 /* check if caller wants to look at return data or just return rc */
2640 if ((plen == NULL) || (out_data == NULL))
2643 *plen = le32_to_cpu(rsp->OutputCount);
2645 /* We check for obvious errors in the output buffer length and offset */
2647 goto ioctl_exit; /* server returned no data */
2648 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
2649 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2655 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
2656 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2657 le32_to_cpu(rsp->OutputOffset));
2663 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2665 if (*out_data == NULL) {
2671 SMB2_ioctl_free(&rqst);
2672 free_rsp_buf(resp_buftype, rsp);
2677 * Individual callers to ioctl worker function follow
2681 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2682 u64 persistent_fid, u64 volatile_fid)
2685 struct compress_ioctl fsctl_input;
2686 char *ret_data = NULL;
2688 fsctl_input.CompressionState =
2689 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
2691 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2692 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2693 (char *)&fsctl_input /* data input */,
2694 2 /* in data len */, CIFSMaxBufSize /* max out data */,
2695 &ret_data /* out data */, NULL);
2697 cifs_dbg(FYI, "set compression rc %d\n", rc);
2703 SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2704 u64 persistent_fid, u64 volatile_fid)
2706 struct smb2_close_req *req;
2707 struct kvec *iov = rqst->rq_iov;
2708 unsigned int total_len;
2711 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2715 req->PersistentFileId = persistent_fid;
2716 req->VolatileFileId = volatile_fid;
2717 iov[0].iov_base = (char *)req;
2718 iov[0].iov_len = total_len;
2724 SMB2_close_free(struct smb_rqst *rqst)
2726 if (rqst && rqst->rq_iov)
2727 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2731 SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2732 u64 persistent_fid, u64 volatile_fid, int flags)
2734 struct smb_rqst rqst;
2735 struct smb2_close_rsp *rsp = NULL;
2736 struct cifs_ses *ses = tcon->ses;
2738 struct kvec rsp_iov;
2739 int resp_buftype = CIFS_NO_BUFFER;
2742 cifs_dbg(FYI, "Close\n");
2744 if (!ses || !(ses->server))
2747 if (smb3_encryption_required(tcon))
2748 flags |= CIFS_TRANSFORM_REQ;
2750 memset(&rqst, 0, sizeof(struct smb_rqst));
2751 memset(&iov, 0, sizeof(iov));
2755 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid);
2759 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2760 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2763 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
2764 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2769 atomic_dec(&tcon->num_remote_opens);
2771 /* BB FIXME - decode close response, update inode for caching */
2774 SMB2_close_free(&rqst);
2775 free_rsp_buf(resp_buftype, rsp);
2780 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2781 u64 persistent_fid, u64 volatile_fid)
2783 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2787 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
2788 struct kvec *iov, unsigned int min_buf_size)
2790 unsigned int smb_len = iov->iov_len;
2791 char *end_of_smb = smb_len + (char *)iov->iov_base;
2792 char *begin_of_buf = offset + (char *)iov->iov_base;
2793 char *end_of_buf = begin_of_buf + buffer_length;
2796 if (buffer_length < min_buf_size) {
2797 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2798 buffer_length, min_buf_size);
2802 /* check if beyond RFC1001 maximum length */
2803 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
2804 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2805 buffer_length, smb_len);
2809 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
2810 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
2818 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2819 * Caller must free buffer.
2822 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
2823 struct kvec *iov, unsigned int minbufsize,
2826 char *begin_of_buf = offset + (char *)iov->iov_base;
2832 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
2836 memcpy(data, begin_of_buf, buffer_length);
2842 SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2843 u64 persistent_fid, u64 volatile_fid,
2844 u8 info_class, u8 info_type, u32 additional_info,
2845 size_t output_len, size_t input_len, void *input)
2847 struct smb2_query_info_req *req;
2848 struct kvec *iov = rqst->rq_iov;
2849 unsigned int total_len;
2852 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2857 req->InfoType = info_type;
2858 req->FileInfoClass = info_class;
2859 req->PersistentFileId = persistent_fid;
2860 req->VolatileFileId = volatile_fid;
2861 req->AdditionalInformation = cpu_to_le32(additional_info);
2863 req->OutputBufferLength = cpu_to_le32(output_len);
2865 req->InputBufferLength = cpu_to_le32(input_len);
2866 /* total_len for smb query request never close to le16 max */
2867 req->InputBufferOffset = cpu_to_le16(total_len - 1);
2868 memcpy(req->Buffer, input, input_len);
2871 iov[0].iov_base = (char *)req;
2873 iov[0].iov_len = total_len - 1 + input_len;
2878 SMB2_query_info_free(struct smb_rqst *rqst)
2880 if (rqst && rqst->rq_iov)
2881 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2885 query_info(const unsigned int xid, struct cifs_tcon *tcon,
2886 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2887 u32 additional_info, size_t output_len, size_t min_len, void **data,
2890 struct smb_rqst rqst;
2891 struct smb2_query_info_rsp *rsp = NULL;
2893 struct kvec rsp_iov;
2895 int resp_buftype = CIFS_NO_BUFFER;
2896 struct cifs_ses *ses = tcon->ses;
2898 bool allocated = false;
2900 cifs_dbg(FYI, "Query Info\n");
2902 if (!ses || !(ses->server))
2905 if (smb3_encryption_required(tcon))
2906 flags |= CIFS_TRANSFORM_REQ;
2908 memset(&rqst, 0, sizeof(struct smb_rqst));
2909 memset(&iov, 0, sizeof(iov));
2913 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
2914 info_class, info_type, additional_info,
2915 output_len, 0, NULL);
2919 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
2920 ses->Suid, info_class, (__u32)info_type);
2922 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2923 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2926 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2927 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
2928 ses->Suid, info_class, (__u32)info_type, rc);
2932 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
2933 ses->Suid, info_class, (__u32)info_type);
2936 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2938 *data = kmalloc(*dlen, GFP_KERNEL);
2941 "Error %d allocating memory for acl\n",
2951 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
2952 le32_to_cpu(rsp->OutputBufferLength),
2953 &rsp_iov, min_len, *data);
2954 if (rc && allocated) {
2961 SMB2_query_info_free(&rqst);
2962 free_rsp_buf(resp_buftype, rsp);
2966 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2967 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
2969 return query_info(xid, tcon, persistent_fid, volatile_fid,
2970 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
2971 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
2972 sizeof(struct smb2_file_all_info), (void **)&data,
2977 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2978 u64 persistent_fid, u64 volatile_fid,
2979 void **data, u32 *plen)
2981 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2984 return query_info(xid, tcon, persistent_fid, volatile_fid,
2985 0, SMB2_O_INFO_SECURITY, additional_info,
2986 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
2990 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2991 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2993 return query_info(xid, tcon, persistent_fid, volatile_fid,
2994 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
2995 sizeof(struct smb2_file_internal_info),
2996 sizeof(struct smb2_file_internal_info),
2997 (void **)&uniqueid, NULL);
3001 * This is a no-op for now. We're not really interested in the reply, but
3002 * rather in the fact that the server sent one and that server->lstrp
3005 * FIXME: maybe we should consider checking that the reply matches request?
3008 smb2_echo_callback(struct mid_q_entry *mid)
3010 struct TCP_Server_Info *server = mid->callback_data;
3011 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3012 struct cifs_credits credits = { .value = 0, .instance = 0 };
3014 if (mid->mid_state == MID_RESPONSE_RECEIVED
3015 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3016 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3017 credits.instance = server->reconnect_instance;
3020 DeleteMidQEntry(mid);
3021 add_credits(server, &credits, CIFS_ECHO_OP);
3024 void smb2_reconnect_server(struct work_struct *work)
3026 struct TCP_Server_Info *server = container_of(work,
3027 struct TCP_Server_Info, reconnect.work);
3028 struct cifs_ses *ses;
3029 struct cifs_tcon *tcon, *tcon2;
3030 struct list_head tmp_list;
3031 int tcon_exist = false;
3033 int resched = false;
3036 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3037 mutex_lock(&server->reconnect_mutex);
3039 INIT_LIST_HEAD(&tmp_list);
3040 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3042 spin_lock(&cifs_tcp_ses_lock);
3043 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3044 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3045 if (tcon->need_reconnect || tcon->need_reopen_files) {
3047 list_add_tail(&tcon->rlist, &tmp_list);
3051 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3052 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3057 * Get the reference to server struct to be sure that the last call of
3058 * cifs_put_tcon() in the loop below won't release the server pointer.
3061 server->srv_count++;
3063 spin_unlock(&cifs_tcp_ses_lock);
3065 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3066 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3068 cifs_reopen_persistent_handles(tcon);
3071 list_del_init(&tcon->rlist);
3072 cifs_put_tcon(tcon);
3075 cifs_dbg(FYI, "Reconnecting tcons finished\n");
3077 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3078 mutex_unlock(&server->reconnect_mutex);
3080 /* now we can safely release srv struct */
3082 cifs_put_tcp_session(server, 1);
3086 SMB2_echo(struct TCP_Server_Info *server)
3088 struct smb2_echo_req *req;
3091 struct smb_rqst rqst = { .rq_iov = iov,
3093 unsigned int total_len;
3095 cifs_dbg(FYI, "In echo request\n");
3097 if (server->tcpStatus == CifsNeedNegotiate) {
3098 /* No need to send echo on newly established connections */
3099 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3103 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
3107 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3109 iov[0].iov_len = total_len;
3110 iov[0].iov_base = (char *)req;
3112 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3113 server, CIFS_ECHO_OP, NULL);
3115 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3117 cifs_small_buf_release(req);
3122 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3125 struct smb_rqst rqst;
3126 struct smb2_flush_req *req;
3127 struct cifs_ses *ses = tcon->ses;
3129 struct kvec rsp_iov;
3133 unsigned int total_len;
3135 cifs_dbg(FYI, "Flush\n");
3137 if (!ses || !(ses->server))
3140 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
3144 if (smb3_encryption_required(tcon))
3145 flags |= CIFS_TRANSFORM_REQ;
3147 req->PersistentFileId = persistent_fid;
3148 req->VolatileFileId = volatile_fid;
3150 iov[0].iov_base = (char *)req;
3151 iov[0].iov_len = total_len;
3153 memset(&rqst, 0, sizeof(struct smb_rqst));
3157 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3158 cifs_small_buf_release(req);
3161 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
3162 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3166 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3171 * To form a chain of read requests, any read requests after the first should
3172 * have the end_of_chain boolean set to true.
3175 smb2_new_read_req(void **buf, unsigned int *total_len,
3176 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3177 unsigned int remaining_bytes, int request_type)
3180 struct smb2_read_plain_req *req = NULL;
3181 struct smb2_sync_hdr *shdr;
3182 struct TCP_Server_Info *server;
3184 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3189 server = io_parms->tcon->ses->server;
3191 return -ECONNABORTED;
3193 shdr = &req->sync_hdr;
3194 shdr->ProcessId = cpu_to_le32(io_parms->pid);
3196 req->PersistentFileId = io_parms->persistent_fid;
3197 req->VolatileFileId = io_parms->volatile_fid;
3198 req->ReadChannelInfoOffset = 0; /* reserved */
3199 req->ReadChannelInfoLength = 0; /* reserved */
3200 req->Channel = 0; /* reserved */
3201 req->MinimumCount = 0;
3202 req->Length = cpu_to_le32(io_parms->length);
3203 req->Offset = cpu_to_le64(io_parms->offset);
3205 trace_smb3_read_enter(0 /* xid */,
3206 io_parms->persistent_fid,
3207 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3208 io_parms->offset, io_parms->length);
3209 #ifdef CONFIG_CIFS_SMB_DIRECT
3211 * If we want to do a RDMA write, fill in and append
3212 * smbd_buffer_descriptor_v1 to the end of read request
3214 if (server->rdma && rdata && !server->sign &&
3215 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
3217 struct smbd_buffer_descriptor_v1 *v1;
3218 bool need_invalidate =
3219 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3221 rdata->mr = smbd_register_mr(
3222 server->smbd_conn, rdata->pages,
3223 rdata->nr_pages, rdata->page_offset,
3224 rdata->tailsz, true, need_invalidate);
3228 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3229 if (need_invalidate)
3230 req->Channel = SMB2_CHANNEL_RDMA_V1;
3231 req->ReadChannelInfoOffset =
3232 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
3233 req->ReadChannelInfoLength =
3234 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3235 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3236 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3237 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3238 v1->length = cpu_to_le32(rdata->mr->mr->length);
3240 *total_len += sizeof(*v1) - 1;
3243 if (request_type & CHAINED_REQUEST) {
3244 if (!(request_type & END_OF_CHAIN)) {
3245 /* next 8-byte aligned request */
3246 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3247 shdr->NextCommand = cpu_to_le32(*total_len);
3248 } else /* END_OF_CHAIN */
3249 shdr->NextCommand = 0;
3250 if (request_type & RELATED_REQUEST) {
3251 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
3253 * Related requests use info from previous read request
3256 shdr->SessionId = 0xFFFFFFFF;
3257 shdr->TreeId = 0xFFFFFFFF;
3258 req->PersistentFileId = 0xFFFFFFFF;
3259 req->VolatileFileId = 0xFFFFFFFF;
3262 if (remaining_bytes > io_parms->length)
3263 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3265 req->RemainingBytes = 0;
3272 smb2_readv_callback(struct mid_q_entry *mid)
3274 struct cifs_readdata *rdata = mid->callback_data;
3275 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3276 struct TCP_Server_Info *server = tcon->ses->server;
3277 struct smb2_sync_hdr *shdr =
3278 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
3279 struct cifs_credits credits = { .value = 0, .instance = 0 };
3280 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3282 .rq_pages = rdata->pages,
3283 .rq_offset = rdata->page_offset,
3284 .rq_npages = rdata->nr_pages,
3285 .rq_pagesz = rdata->pagesz,
3286 .rq_tailsz = rdata->tailsz };
3288 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3289 __func__, mid->mid, mid->mid_state, rdata->result,
3292 switch (mid->mid_state) {
3293 case MID_RESPONSE_RECEIVED:
3294 credits.value = le16_to_cpu(shdr->CreditRequest);
3295 credits.instance = server->reconnect_instance;
3296 /* result already set, check signature */
3297 if (server->sign && !mid->decrypted) {
3300 rc = smb2_verify_signature(&rqst, server);
3302 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
3305 /* FIXME: should this be counted toward the initiating task? */
3306 task_io_account_read(rdata->got_bytes);
3307 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3309 case MID_REQUEST_SUBMITTED:
3310 case MID_RETRY_NEEDED:
3311 rdata->result = -EAGAIN;
3312 if (server->sign && rdata->got_bytes)
3313 /* reset bytes number since we can not check a sign */
3314 rdata->got_bytes = 0;
3315 /* FIXME: should this be counted toward the initiating task? */
3316 task_io_account_read(rdata->got_bytes);
3317 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3319 case MID_RESPONSE_MALFORMED:
3320 credits.value = le16_to_cpu(shdr->CreditRequest);
3321 credits.instance = server->reconnect_instance;
3324 rdata->result = -EIO;
3326 #ifdef CONFIG_CIFS_SMB_DIRECT
3328 * If this rdata has a memmory registered, the MR can be freed
3329 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3330 * because they have limited number and are used for future I/Os
3333 smbd_deregister_mr(rdata->mr);
3337 if (rdata->result && rdata->result != -ENODATA) {
3338 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
3339 trace_smb3_read_err(0 /* xid */,
3340 rdata->cfile->fid.persistent_fid,
3341 tcon->tid, tcon->ses->Suid, rdata->offset,
3342 rdata->bytes, rdata->result);
3344 trace_smb3_read_done(0 /* xid */,
3345 rdata->cfile->fid.persistent_fid,
3346 tcon->tid, tcon->ses->Suid,
3347 rdata->offset, rdata->got_bytes);
3349 queue_work(cifsiod_wq, &rdata->work);
3350 DeleteMidQEntry(mid);
3351 add_credits(server, &credits, 0);
3354 /* smb2_async_readv - send an async read, and set up mid to handle result */
3356 smb2_async_readv(struct cifs_readdata *rdata)
3360 struct smb2_sync_hdr *shdr;
3361 struct cifs_io_parms io_parms;
3362 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3364 struct TCP_Server_Info *server;
3365 unsigned int total_len;
3367 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3368 __func__, rdata->offset, rdata->bytes);
3370 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3371 io_parms.offset = rdata->offset;
3372 io_parms.length = rdata->bytes;
3373 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3374 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3375 io_parms.pid = rdata->pid;
3377 server = io_parms.tcon->ses->server;
3379 rc = smb2_new_read_req(
3380 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
3384 if (smb3_encryption_required(io_parms.tcon))
3385 flags |= CIFS_TRANSFORM_REQ;
3387 rdata->iov[0].iov_base = buf;
3388 rdata->iov[0].iov_len = total_len;
3390 shdr = (struct smb2_sync_hdr *)buf;
3392 if (rdata->credits.value > 0) {
3393 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
3394 SMB2_MAX_BUFFER_SIZE));
3395 shdr->CreditRequest =
3396 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
3398 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3400 goto async_readv_out;
3402 flags |= CIFS_HAS_CREDITS;
3405 kref_get(&rdata->refcount);
3406 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
3407 cifs_readv_receive, smb2_readv_callback,
3408 smb3_handle_read_data, rdata, flags,
3411 kref_put(&rdata->refcount, cifs_readdata_release);
3412 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
3413 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3415 io_parms.tcon->ses->Suid,
3416 io_parms.offset, io_parms.length, rc);
3420 cifs_small_buf_release(buf);
3425 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3426 unsigned int *nbytes, char **buf, int *buf_type)
3428 struct smb_rqst rqst;
3429 int resp_buftype, rc = -EACCES;
3430 struct smb2_read_plain_req *req = NULL;
3431 struct smb2_read_rsp *rsp = NULL;
3433 struct kvec rsp_iov;
3434 unsigned int total_len;
3435 int flags = CIFS_LOG_ERROR;
3436 struct cifs_ses *ses = io_parms->tcon->ses;
3439 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
3443 if (smb3_encryption_required(io_parms->tcon))
3444 flags |= CIFS_TRANSFORM_REQ;
3446 iov[0].iov_base = (char *)req;
3447 iov[0].iov_len = total_len;
3449 memset(&rqst, 0, sizeof(struct smb_rqst));
3453 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3454 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
3457 if (rc != -ENODATA) {
3458 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3459 cifs_dbg(VFS, "Send error in read = %d\n", rc);
3460 trace_smb3_read_err(xid, req->PersistentFileId,
3461 io_parms->tcon->tid, ses->Suid,
3462 io_parms->offset, io_parms->length,
3465 trace_smb3_read_done(xid, req->PersistentFileId,
3466 io_parms->tcon->tid, ses->Suid,
3467 io_parms->offset, 0);
3468 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3469 cifs_small_buf_release(req);
3470 return rc == -ENODATA ? 0 : rc;
3472 trace_smb3_read_done(xid, req->PersistentFileId,
3473 io_parms->tcon->tid, ses->Suid,
3474 io_parms->offset, io_parms->length);
3476 cifs_small_buf_release(req);
3478 *nbytes = le32_to_cpu(rsp->DataLength);
3479 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3480 (*nbytes > io_parms->length)) {
3481 cifs_dbg(FYI, "bad length %d for count %d\n",
3482 *nbytes, io_parms->length);
3488 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
3489 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3490 } else if (resp_buftype != CIFS_NO_BUFFER) {
3491 *buf = rsp_iov.iov_base;
3492 if (resp_buftype == CIFS_SMALL_BUFFER)
3493 *buf_type = CIFS_SMALL_BUFFER;
3494 else if (resp_buftype == CIFS_LARGE_BUFFER)
3495 *buf_type = CIFS_LARGE_BUFFER;
3501 * Check the mid_state and signature on received buffer (if any), and queue the
3502 * workqueue completion task.
3505 smb2_writev_callback(struct mid_q_entry *mid)
3507 struct cifs_writedata *wdata = mid->callback_data;
3508 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3509 struct TCP_Server_Info *server = tcon->ses->server;
3510 unsigned int written;
3511 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
3512 struct cifs_credits credits = { .value = 0, .instance = 0 };
3514 switch (mid->mid_state) {
3515 case MID_RESPONSE_RECEIVED:
3516 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3517 credits.instance = server->reconnect_instance;
3518 wdata->result = smb2_check_receive(mid, server, 0);
3519 if (wdata->result != 0)
3522 written = le32_to_cpu(rsp->DataLength);
3524 * Mask off high 16 bits when bytes written as returned
3525 * by the server is greater than bytes requested by the
3526 * client. OS/2 servers are known to set incorrect
3529 if (written > wdata->bytes)
3532 if (written < wdata->bytes)
3533 wdata->result = -ENOSPC;
3535 wdata->bytes = written;
3537 case MID_REQUEST_SUBMITTED:
3538 case MID_RETRY_NEEDED:
3539 wdata->result = -EAGAIN;
3541 case MID_RESPONSE_MALFORMED:
3542 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3543 credits.instance = server->reconnect_instance;
3546 wdata->result = -EIO;
3549 #ifdef CONFIG_CIFS_SMB_DIRECT
3551 * If this wdata has a memory registered, the MR can be freed
3552 * The number of MRs available is limited, it's important to recover
3553 * used MR as soon as I/O is finished. Hold MR longer in the later
3554 * I/O process can possibly result in I/O deadlock due to lack of MR
3555 * to send request on I/O retry
3558 smbd_deregister_mr(wdata->mr);
3562 if (wdata->result) {
3563 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3564 trace_smb3_write_err(0 /* no xid */,
3565 wdata->cfile->fid.persistent_fid,
3566 tcon->tid, tcon->ses->Suid, wdata->offset,
3567 wdata->bytes, wdata->result);
3569 trace_smb3_write_done(0 /* no xid */,
3570 wdata->cfile->fid.persistent_fid,
3571 tcon->tid, tcon->ses->Suid,
3572 wdata->offset, wdata->bytes);
3574 queue_work(cifsiod_wq, &wdata->work);
3575 DeleteMidQEntry(mid);
3576 add_credits(server, &credits, 0);
3579 /* smb2_async_writev - send an async write, and set up mid to handle result */
3581 smb2_async_writev(struct cifs_writedata *wdata,
3582 void (*release)(struct kref *kref))
3584 int rc = -EACCES, flags = 0;
3585 struct smb2_write_req *req = NULL;
3586 struct smb2_sync_hdr *shdr;
3587 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3588 struct TCP_Server_Info *server = tcon->ses->server;
3590 struct smb_rqst rqst = { };
3591 unsigned int total_len;
3593 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
3597 if (smb3_encryption_required(tcon))
3598 flags |= CIFS_TRANSFORM_REQ;
3600 shdr = (struct smb2_sync_hdr *)req;
3601 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
3603 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3604 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3605 req->WriteChannelInfoOffset = 0;
3606 req->WriteChannelInfoLength = 0;
3608 req->Offset = cpu_to_le64(wdata->offset);
3609 req->DataOffset = cpu_to_le16(
3610 offsetof(struct smb2_write_req, Buffer));
3611 req->RemainingBytes = 0;
3613 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
3614 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
3615 #ifdef CONFIG_CIFS_SMB_DIRECT
3617 * If we want to do a server RDMA read, fill in and append
3618 * smbd_buffer_descriptor_v1 to the end of write request
3620 if (server->rdma && !server->sign && wdata->bytes >=
3621 server->smbd_conn->rdma_readwrite_threshold) {
3623 struct smbd_buffer_descriptor_v1 *v1;
3624 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3626 wdata->mr = smbd_register_mr(
3627 server->smbd_conn, wdata->pages,
3628 wdata->nr_pages, wdata->page_offset,
3629 wdata->tailsz, false, need_invalidate);
3632 goto async_writev_out;
3635 req->DataOffset = 0;
3636 if (wdata->nr_pages > 1)
3637 req->RemainingBytes =
3639 (wdata->nr_pages - 1) * wdata->pagesz -
3640 wdata->page_offset + wdata->tailsz
3643 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
3644 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3645 if (need_invalidate)
3646 req->Channel = SMB2_CHANNEL_RDMA_V1;
3647 req->WriteChannelInfoOffset =
3648 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
3649 req->WriteChannelInfoLength =
3650 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3651 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3652 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3653 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3654 v1->length = cpu_to_le32(wdata->mr->mr->length);
3657 iov[0].iov_len = total_len - 1;
3658 iov[0].iov_base = (char *)req;
3662 rqst.rq_pages = wdata->pages;
3663 rqst.rq_offset = wdata->page_offset;
3664 rqst.rq_npages = wdata->nr_pages;
3665 rqst.rq_pagesz = wdata->pagesz;
3666 rqst.rq_tailsz = wdata->tailsz;
3667 #ifdef CONFIG_CIFS_SMB_DIRECT
3669 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
3673 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3674 wdata->offset, wdata->bytes);
3676 #ifdef CONFIG_CIFS_SMB_DIRECT
3677 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3679 req->Length = cpu_to_le32(wdata->bytes);
3681 req->Length = cpu_to_le32(wdata->bytes);
3684 if (wdata->credits.value > 0) {
3685 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
3686 SMB2_MAX_BUFFER_SIZE));
3687 shdr->CreditRequest =
3688 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
3690 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3692 goto async_writev_out;
3694 flags |= CIFS_HAS_CREDITS;
3697 kref_get(&wdata->refcount);
3698 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3699 wdata, flags, &wdata->credits);
3702 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
3703 tcon->tid, tcon->ses->Suid, wdata->offset,
3705 kref_put(&wdata->refcount, release);
3706 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3710 cifs_small_buf_release(req);
3715 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3716 * The length field from io_parms must be at least 1 and indicates a number of
3717 * elements with data to write that begins with position 1 in iov array. All
3718 * data length is specified by count.
3721 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3722 unsigned int *nbytes, struct kvec *iov, int n_vec)
3724 struct smb_rqst rqst;
3726 struct smb2_write_req *req = NULL;
3727 struct smb2_write_rsp *rsp = NULL;
3729 struct kvec rsp_iov;
3731 unsigned int total_len;
3738 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3743 if (io_parms->tcon->ses->server == NULL)
3744 return -ECONNABORTED;
3746 if (smb3_encryption_required(io_parms->tcon))
3747 flags |= CIFS_TRANSFORM_REQ;
3749 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
3751 req->PersistentFileId = io_parms->persistent_fid;
3752 req->VolatileFileId = io_parms->volatile_fid;
3753 req->WriteChannelInfoOffset = 0;
3754 req->WriteChannelInfoLength = 0;
3756 req->Length = cpu_to_le32(io_parms->length);
3757 req->Offset = cpu_to_le64(io_parms->offset);
3758 req->DataOffset = cpu_to_le16(
3759 offsetof(struct smb2_write_req, Buffer));
3760 req->RemainingBytes = 0;
3762 trace_smb3_write_enter(xid, io_parms->persistent_fid,
3763 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3764 io_parms->offset, io_parms->length);
3766 iov[0].iov_base = (char *)req;
3768 iov[0].iov_len = total_len - 1;
3770 memset(&rqst, 0, sizeof(struct smb_rqst));
3772 rqst.rq_nvec = n_vec + 1;
3774 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
3775 &resp_buftype, flags, &rsp_iov);
3776 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
3779 trace_smb3_write_err(xid, req->PersistentFileId,
3780 io_parms->tcon->tid,
3781 io_parms->tcon->ses->Suid,
3782 io_parms->offset, io_parms->length, rc);
3783 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
3784 cifs_dbg(VFS, "Send error in write = %d\n", rc);
3786 *nbytes = le32_to_cpu(rsp->DataLength);
3787 trace_smb3_write_done(xid, req->PersistentFileId,
3788 io_parms->tcon->tid,
3789 io_parms->tcon->ses->Suid,
3790 io_parms->offset, *nbytes);
3793 cifs_small_buf_release(req);
3794 free_rsp_buf(resp_buftype, rsp);
3799 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3802 unsigned int entrycount = 0;
3803 unsigned int next_offset = 0;
3805 FILE_DIRECTORY_INFO *dir_info;
3807 if (bufstart == NULL)
3810 entryptr = bufstart;
3813 if (entryptr + next_offset < entryptr ||
3814 entryptr + next_offset > end_of_buf ||
3815 entryptr + next_offset + size > end_of_buf) {
3816 cifs_dbg(VFS, "malformed search entry would overflow\n");
3820 entryptr = entryptr + next_offset;
3821 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
3823 len = le32_to_cpu(dir_info->FileNameLength);
3824 if (entryptr + len < entryptr ||
3825 entryptr + len > end_of_buf ||
3826 entryptr + len + size > end_of_buf) {
3827 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3832 *lastentry = entryptr;
3835 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
3847 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3848 u64 persistent_fid, u64 volatile_fid, int index,
3849 struct cifs_search_info *srch_inf)
3851 struct smb_rqst rqst;
3852 struct smb2_query_directory_req *req;
3853 struct smb2_query_directory_rsp *rsp = NULL;
3855 struct kvec rsp_iov;
3858 int resp_buftype = CIFS_NO_BUFFER;
3859 unsigned char *bufptr;
3860 struct TCP_Server_Info *server;
3861 struct cifs_ses *ses = tcon->ses;
3862 __le16 asteriks = cpu_to_le16('*');
3864 unsigned int output_size = CIFSMaxBufSize;
3865 size_t info_buf_size;
3867 unsigned int total_len;
3869 if (ses && (ses->server))
3870 server = ses->server;
3874 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3879 if (smb3_encryption_required(tcon))
3880 flags |= CIFS_TRANSFORM_REQ;
3882 switch (srch_inf->info_level) {
3883 case SMB_FIND_FILE_DIRECTORY_INFO:
3884 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3885 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3887 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3888 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3889 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3892 cifs_dbg(VFS, "info level %u isn't supported\n",
3893 srch_inf->info_level);
3898 req->FileIndex = cpu_to_le32(index);
3899 req->PersistentFileId = persistent_fid;
3900 req->VolatileFileId = volatile_fid;
3903 bufptr = req->Buffer;
3904 memcpy(bufptr, &asteriks, len);
3906 req->FileNameOffset =
3907 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
3908 req->FileNameLength = cpu_to_le16(len);
3910 * BB could be 30 bytes or so longer if we used SMB2 specific
3911 * buffer lengths, but this is safe and close enough.
3913 output_size = min_t(unsigned int, output_size, server->maxBuf);
3914 output_size = min_t(unsigned int, output_size, 2 << 15);
3915 req->OutputBufferLength = cpu_to_le32(output_size);
3917 iov[0].iov_base = (char *)req;
3919 iov[0].iov_len = total_len - 1;
3921 iov[1].iov_base = (char *)(req->Buffer);
3922 iov[1].iov_len = len;
3924 memset(&rqst, 0, sizeof(struct smb_rqst));
3928 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
3929 tcon->ses->Suid, index, output_size);
3931 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3932 cifs_small_buf_release(req);
3933 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
3936 if (rc == -ENODATA &&
3937 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
3938 trace_smb3_query_dir_done(xid, persistent_fid,
3939 tcon->tid, tcon->ses->Suid, index, 0);
3940 srch_inf->endOfSearch = true;
3943 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
3944 tcon->ses->Suid, index, 0, rc);
3945 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3950 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
3951 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
3954 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
3955 tcon->ses->Suid, index, 0, rc);
3959 srch_inf->unicode = true;
3961 if (srch_inf->ntwrk_buf_start) {
3962 if (srch_inf->smallBuf)
3963 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3965 cifs_buf_release(srch_inf->ntwrk_buf_start);
3967 srch_inf->ntwrk_buf_start = (char *)rsp;
3968 srch_inf->srch_entries_start = srch_inf->last_entry =
3969 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
3970 end_of_smb = rsp_iov.iov_len + (char *)rsp;
3971 srch_inf->entries_in_buffer =
3972 num_entries(srch_inf->srch_entries_start, end_of_smb,
3973 &srch_inf->last_entry, info_buf_size);
3974 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
3975 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3976 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3977 srch_inf->srch_entries_start, srch_inf->last_entry);
3978 if (resp_buftype == CIFS_LARGE_BUFFER)
3979 srch_inf->smallBuf = false;
3980 else if (resp_buftype == CIFS_SMALL_BUFFER)
3981 srch_inf->smallBuf = true;
3983 cifs_dbg(VFS, "illegal search buffer type\n");
3985 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
3986 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
3990 free_rsp_buf(resp_buftype, rsp);
3995 SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3996 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3997 u8 info_type, u32 additional_info,
3998 void **data, unsigned int *size)
4000 struct smb2_set_info_req *req;
4001 struct kvec *iov = rqst->rq_iov;
4002 unsigned int i, total_len;
4005 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
4009 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4010 req->InfoType = info_type;
4011 req->FileInfoClass = info_class;
4012 req->PersistentFileId = persistent_fid;
4013 req->VolatileFileId = volatile_fid;
4014 req->AdditionalInformation = cpu_to_le32(additional_info);
4017 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
4018 req->BufferLength = cpu_to_le32(*size);
4020 memcpy(req->Buffer, *data, *size);
4023 iov[0].iov_base = (char *)req;
4025 iov[0].iov_len = total_len - 1;
4027 for (i = 1; i < rqst->rq_nvec; i++) {
4028 le32_add_cpu(&req->BufferLength, size[i]);
4029 iov[i].iov_base = (char *)data[i];
4030 iov[i].iov_len = size[i];
4037 SMB2_set_info_free(struct smb_rqst *rqst)
4039 if (rqst && rqst->rq_iov)
4040 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
4044 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4045 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4046 u8 info_type, u32 additional_info, unsigned int num,
4047 void **data, unsigned int *size)
4049 struct smb_rqst rqst;
4050 struct smb2_set_info_rsp *rsp = NULL;
4052 struct kvec rsp_iov;
4055 struct cifs_ses *ses = tcon->ses;
4058 if (!ses || !(ses->server))
4064 if (smb3_encryption_required(tcon))
4065 flags |= CIFS_TRANSFORM_REQ;
4067 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4071 memset(&rqst, 0, sizeof(struct smb_rqst));
4075 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4076 info_class, info_type, additional_info,
4084 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
4086 SMB2_set_info_free(&rqst);
4087 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
4090 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
4091 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4092 ses->Suid, info_class, (__u32)info_type, rc);
4095 free_rsp_buf(resp_buftype, rsp);
4101 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4102 u64 volatile_fid, u32 pid, __le64 *eof)
4104 struct smb2_file_eof_info info;
4108 info.EndOfFile = *eof;
4111 size = sizeof(struct smb2_file_eof_info);
4113 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4114 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4115 0, 1, &data, &size);
4119 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4120 u64 persistent_fid, u64 volatile_fid,
4121 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4123 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4124 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4125 1, (void **)&pnntsd, &pacllen);
4129 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4130 u64 persistent_fid, u64 volatile_fid,
4131 struct smb2_file_full_ea_info *buf, int len)
4133 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4134 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4135 0, 1, (void **)&buf, &len);
4139 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4140 const u64 persistent_fid, const u64 volatile_fid,
4143 struct smb_rqst rqst;
4145 struct smb2_oplock_break *req = NULL;
4146 struct cifs_ses *ses = tcon->ses;
4147 int flags = CIFS_OBREAK_OP;
4148 unsigned int total_len;
4150 struct kvec rsp_iov;
4153 cifs_dbg(FYI, "SMB2_oplock_break\n");
4154 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4159 if (smb3_encryption_required(tcon))
4160 flags |= CIFS_TRANSFORM_REQ;
4162 req->VolatileFid = volatile_fid;
4163 req->PersistentFid = persistent_fid;
4164 req->OplockLevel = oplock_level;
4165 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4167 flags |= CIFS_NO_RESP;
4169 iov[0].iov_base = (char *)req;
4170 iov[0].iov_len = total_len;
4172 memset(&rqst, 0, sizeof(struct smb_rqst));
4176 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4177 cifs_small_buf_release(req);
4180 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4181 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
4188 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4189 struct kstatfs *kst)
4191 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4192 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4193 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
4194 kst->f_bfree = kst->f_bavail =
4195 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
4200 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4201 struct kstatfs *kst)
4203 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4204 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4205 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4206 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4207 kst->f_bavail = kst->f_bfree;
4209 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4210 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4211 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4212 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4213 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4219 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4220 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4223 struct smb2_query_info_req *req;
4224 unsigned int total_len;
4226 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
4228 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4231 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4236 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4237 req->FileInfoClass = level;
4238 req->PersistentFileId = persistent_fid;
4239 req->VolatileFileId = volatile_fid;
4241 req->InputBufferOffset =
4242 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
4243 req->OutputBufferLength = cpu_to_le32(
4244 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
4246 iov->iov_base = (char *)req;
4247 iov->iov_len = total_len;
4252 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4253 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4255 struct smb_rqst rqst;
4256 struct smb2_query_info_rsp *rsp = NULL;
4258 struct kvec rsp_iov;
4261 struct cifs_ses *ses = tcon->ses;
4262 FILE_SYSTEM_POSIX_INFO *info = NULL;
4265 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4266 sizeof(FILE_SYSTEM_POSIX_INFO),
4267 persistent_fid, volatile_fid);
4271 if (smb3_encryption_required(tcon))
4272 flags |= CIFS_TRANSFORM_REQ;
4274 memset(&rqst, 0, sizeof(struct smb_rqst));
4278 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4279 cifs_small_buf_release(iov.iov_base);
4281 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4282 goto posix_qfsinf_exit;
4284 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4286 info = (FILE_SYSTEM_POSIX_INFO *)(
4287 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4288 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4289 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4290 sizeof(FILE_SYSTEM_POSIX_INFO));
4292 copy_posix_fs_info_to_kstatfs(info, fsdata);
4295 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4300 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4301 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4303 struct smb_rqst rqst;
4304 struct smb2_query_info_rsp *rsp = NULL;
4306 struct kvec rsp_iov;
4309 struct cifs_ses *ses = tcon->ses;
4310 struct smb2_fs_full_size_info *info = NULL;
4313 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4314 sizeof(struct smb2_fs_full_size_info),
4315 persistent_fid, volatile_fid);
4319 if (smb3_encryption_required(tcon))
4320 flags |= CIFS_TRANSFORM_REQ;
4322 memset(&rqst, 0, sizeof(struct smb_rqst));
4326 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4327 cifs_small_buf_release(iov.iov_base);
4329 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4332 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4334 info = (struct smb2_fs_full_size_info *)(
4335 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4336 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4337 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4338 sizeof(struct smb2_fs_full_size_info));
4340 smb2_copy_fs_info_to_kstatfs(info, fsdata);
4343 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4348 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
4349 u64 persistent_fid, u64 volatile_fid, int level)
4351 struct smb_rqst rqst;
4352 struct smb2_query_info_rsp *rsp = NULL;
4354 struct kvec rsp_iov;
4356 int resp_buftype, max_len, min_len;
4357 struct cifs_ses *ses = tcon->ses;
4358 unsigned int rsp_len, offset;
4361 if (level == FS_DEVICE_INFORMATION) {
4362 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4363 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4364 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4365 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4366 min_len = MIN_FS_ATTR_INFO_SIZE;
4367 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4368 max_len = sizeof(struct smb3_fs_ss_info);
4369 min_len = sizeof(struct smb3_fs_ss_info);
4370 } else if (level == FS_VOLUME_INFORMATION) {
4371 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4372 min_len = sizeof(struct smb3_fs_vol_info);
4374 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
4378 rc = build_qfs_info_req(&iov, tcon, level, max_len,
4379 persistent_fid, volatile_fid);
4383 if (smb3_encryption_required(tcon))
4384 flags |= CIFS_TRANSFORM_REQ;
4386 memset(&rqst, 0, sizeof(struct smb_rqst));
4390 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4391 cifs_small_buf_release(iov.iov_base);
4393 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4396 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4398 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4399 offset = le16_to_cpu(rsp->OutputBufferOffset);
4400 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
4404 if (level == FS_ATTRIBUTE_INFORMATION)
4405 memcpy(&tcon->fsAttrInfo, offset
4406 + (char *)rsp, min_t(unsigned int,
4408 else if (level == FS_DEVICE_INFORMATION)
4409 memcpy(&tcon->fsDevInfo, offset
4410 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
4411 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4412 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
4413 (offset + (char *)rsp);
4414 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4415 tcon->perf_sector_size =
4416 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
4417 } else if (level == FS_VOLUME_INFORMATION) {
4418 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4419 (offset + (char *)rsp);
4420 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4421 tcon->vol_create_time = vol_info->VolumeCreationTime;
4425 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4430 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4431 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4432 const __u32 num_lock, struct smb2_lock_element *buf)
4434 struct smb_rqst rqst;
4436 struct smb2_lock_req *req = NULL;
4438 struct kvec rsp_iov;
4441 int flags = CIFS_NO_RESP;
4442 unsigned int total_len;
4444 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
4446 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
4450 if (smb3_encryption_required(tcon))
4451 flags |= CIFS_TRANSFORM_REQ;
4453 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4454 req->LockCount = cpu_to_le16(num_lock);
4456 req->PersistentFileId = persist_fid;
4457 req->VolatileFileId = volatile_fid;
4459 count = num_lock * sizeof(struct smb2_lock_element);
4461 iov[0].iov_base = (char *)req;
4462 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
4463 iov[1].iov_base = (char *)buf;
4464 iov[1].iov_len = count;
4466 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
4468 memset(&rqst, 0, sizeof(struct smb_rqst));
4472 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
4474 cifs_small_buf_release(req);
4476 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
4477 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
4478 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4479 tcon->ses->Suid, rc);
4486 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4487 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4488 const __u64 length, const __u64 offset, const __u32 lock_flags,
4491 struct smb2_lock_element lock;
4493 lock.Offset = cpu_to_le64(offset);
4494 lock.Length = cpu_to_le64(length);
4495 lock.Flags = cpu_to_le32(lock_flags);
4496 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
4497 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
4499 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
4503 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
4504 __u8 *lease_key, const __le32 lease_state)
4506 struct smb_rqst rqst;
4508 struct smb2_lease_ack *req = NULL;
4509 struct cifs_ses *ses = tcon->ses;
4510 int flags = CIFS_OBREAK_OP;
4511 unsigned int total_len;
4513 struct kvec rsp_iov;
4515 __u64 *please_key_high;
4516 __u64 *please_key_low;
4518 cifs_dbg(FYI, "SMB2_lease_break\n");
4519 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4524 if (smb3_encryption_required(tcon))
4525 flags |= CIFS_TRANSFORM_REQ;
4527 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4528 req->StructureSize = cpu_to_le16(36);
4531 memcpy(req->LeaseKey, lease_key, 16);
4532 req->LeaseState = lease_state;
4534 flags |= CIFS_NO_RESP;
4536 iov[0].iov_base = (char *)req;
4537 iov[0].iov_len = total_len;
4539 memset(&rqst, 0, sizeof(struct smb_rqst));
4543 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4544 cifs_small_buf_release(req);
4546 please_key_low = (__u64 *)lease_key;
4547 please_key_high = (__u64 *)(lease_key+8);
4549 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4550 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
4551 ses->Suid, *please_key_low, *please_key_high, rc);
4552 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
4554 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
4555 ses->Suid, *please_key_low, *please_key_high);