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 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
836 ses->server->ops = &smb311_operations;
837 } else if (le16_to_cpu(rsp->DialectRevision) !=
838 ses->server->vals->protocol_id) {
839 /* if requested single dialect ensure returned dialect matched */
840 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
841 le16_to_cpu(rsp->DialectRevision));
845 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
847 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
848 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
849 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
850 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
851 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
852 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
853 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
854 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
855 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
856 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
858 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
859 le16_to_cpu(rsp->DialectRevision));
863 server->dialect = le16_to_cpu(rsp->DialectRevision);
866 * Keep a copy of the hash after negprot. This hash will be
867 * the starting hash value for all sessions made from this
870 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
871 SMB2_PREAUTH_HASH_SIZE);
873 /* SMB2 only has an extended negflavor */
874 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
875 /* set it to the maximum buffer size value we can send with 1 credit */
876 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
877 SMB2_MAX_BUFFER_SIZE);
878 server->max_read = le32_to_cpu(rsp->MaxReadSize);
879 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
880 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
881 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
882 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
884 server->capabilities = le32_to_cpu(rsp->Capabilities);
886 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
888 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
889 (struct smb2_sync_hdr *)rsp);
891 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
893 * ses->sectype = RawNTLMSSP;
894 * but for time being this is our only auth choice so doesn't matter.
895 * We just found a server which sets blob length to zero expecting raw.
897 if (blob_length == 0) {
898 cifs_dbg(FYI, "missing security blob on negprot\n");
899 server->sec_ntlmssp = true;
902 rc = cifs_enable_signing(server, ses->sign);
906 rc = decode_negTokenInit(security_blob, blob_length, server);
913 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
914 if (rsp->NegotiateContextCount)
915 rc = smb311_decode_neg_context(rsp, server,
918 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
921 free_rsp_buf(resp_buftype, rsp);
925 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
928 struct validate_negotiate_info_req *pneg_inbuf;
929 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
931 u32 inbuflen; /* max of 4 dialects */
933 cifs_dbg(FYI, "validate negotiate\n");
935 /* In SMB3.11 preauth integrity supersedes validate negotiate */
936 if (tcon->ses->server->dialect == SMB311_PROT_ID)
940 * validation ioctl must be signed, so no point sending this if we
941 * can not sign it (ie are not known user). Even if signing is not
942 * required (enabled but not negotiated), in those cases we selectively
943 * sign just this, the first and only signed request on a connection.
944 * Having validation of negotiate info helps reduce attack vectors.
946 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
947 return 0; /* validation requires signing */
949 if (tcon->ses->user_name == NULL) {
950 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
951 return 0; /* validation requires signing */
954 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
955 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
957 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
961 pneg_inbuf->Capabilities =
962 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
963 memcpy(pneg_inbuf->Guid, tcon->ses->server->client_guid,
964 SMB2_CLIENT_GUID_SIZE);
967 pneg_inbuf->SecurityMode =
968 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
969 else if (global_secflags & CIFSSEC_MAY_SIGN)
970 pneg_inbuf->SecurityMode =
971 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
973 pneg_inbuf->SecurityMode = 0;
976 if (strcmp(tcon->ses->server->vals->version_string,
977 SMB3ANY_VERSION_STRING) == 0) {
978 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
979 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
980 pneg_inbuf->DialectCount = cpu_to_le16(2);
981 /* structure is big enough for 3 dialects, sending only 2 */
982 inbuflen = sizeof(*pneg_inbuf) -
983 (2 * sizeof(pneg_inbuf->Dialects[0]));
984 } else if (strcmp(tcon->ses->server->vals->version_string,
985 SMBDEFAULT_VERSION_STRING) == 0) {
986 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
987 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
988 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
989 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
990 pneg_inbuf->DialectCount = cpu_to_le16(4);
991 /* structure is big enough for 3 dialects */
992 inbuflen = sizeof(*pneg_inbuf);
994 /* otherwise specific dialect was requested */
995 pneg_inbuf->Dialects[0] =
996 cpu_to_le16(tcon->ses->server->vals->protocol_id);
997 pneg_inbuf->DialectCount = cpu_to_le16(1);
998 /* structure is big enough for 3 dialects, sending only 1 */
999 inbuflen = sizeof(*pneg_inbuf) -
1000 sizeof(pneg_inbuf->Dialects[0]) * 2;
1003 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1004 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
1005 (char *)pneg_inbuf, inbuflen, (char **)&pneg_rsp, &rsplen);
1006 if (rc == -EOPNOTSUPP) {
1008 * Old Windows versions or Netapp SMB server can return
1009 * not supported error. Client should accept it.
1011 cifs_dbg(VFS, "Server does not support validate negotiate\n");
1013 } else if (rc != 0) {
1014 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
1016 goto out_free_inbuf;
1020 if (rsplen != sizeof(*pneg_rsp)) {
1021 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
1024 /* relax check since Mac returns max bufsize allowed on ioctl */
1025 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1029 /* check validate negotiate info response matches what we got earlier */
1030 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
1033 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
1036 /* do not validate server guid because not saved at negprot time yet */
1038 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1039 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
1042 /* validate negotiate successful */
1044 cifs_dbg(FYI, "validate negotiate info successful\n");
1048 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1057 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1059 switch (requested) {
1066 if (server->sec_ntlmssp &&
1067 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1069 if ((server->sec_kerberos || server->sec_mskerberos) &&
1070 (global_secflags & CIFSSEC_MAY_KRB5))
1078 struct SMB2_sess_data {
1080 struct cifs_ses *ses;
1081 struct nls_table *nls_cp;
1082 void (*func)(struct SMB2_sess_data *);
1084 u64 previous_session;
1086 /* we will send the SMB in three pieces:
1087 * a fixed length beginning part, an optional
1088 * SPNEGO blob (which can be zero length), and a
1089 * last part which will include the strings
1090 * and rest of bcc area. This allows us to avoid
1091 * a large buffer 17K allocation
1098 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1101 struct cifs_ses *ses = sess_data->ses;
1102 struct smb2_sess_setup_req *req;
1103 struct TCP_Server_Info *server = ses->server;
1104 unsigned int total_len;
1106 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1111 /* First session, not a reauthenticate */
1112 req->sync_hdr.SessionId = 0;
1114 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
1115 req->PreviousSessionId = sess_data->previous_session;
1117 req->Flags = 0; /* MBZ */
1119 /* enough to enable echos and oplocks and one max size write */
1120 req->sync_hdr.CreditRequest = cpu_to_le16(130);
1122 /* only one of SMB2 signing flags may be set in SMB2 request */
1124 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1125 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1126 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1128 req->SecurityMode = 0;
1130 req->Capabilities = 0;
1131 req->Channel = 0; /* MBZ */
1133 sess_data->iov[0].iov_base = (char *)req;
1135 sess_data->iov[0].iov_len = total_len - 1;
1137 * This variable will be used to clear the buffer
1138 * allocated above in case of any error in the calling function.
1140 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1146 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1148 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1149 sess_data->buf0_type = CIFS_NO_BUFFER;
1153 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1156 struct smb_rqst rqst;
1157 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1158 struct kvec rsp_iov = { NULL, 0 };
1160 /* Testing shows that buffer offset must be at location of Buffer[0] */
1161 req->SecurityBufferOffset =
1162 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1163 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1165 memset(&rqst, 0, sizeof(struct smb_rqst));
1166 rqst.rq_iov = sess_data->iov;
1169 /* BB add code to build os and lm fields */
1170 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1172 &sess_data->buf0_type,
1173 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
1174 cifs_small_buf_release(sess_data->iov[0].iov_base);
1175 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1181 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1184 struct cifs_ses *ses = sess_data->ses;
1186 mutex_lock(&ses->server->srv_mutex);
1187 if (ses->server->ops->generate_signingkey) {
1188 rc = ses->server->ops->generate_signingkey(ses);
1191 "SMB3 session key generation failed\n");
1192 mutex_unlock(&ses->server->srv_mutex);
1196 if (!ses->server->session_estab) {
1197 ses->server->sequence_number = 0x2;
1198 ses->server->session_estab = true;
1200 mutex_unlock(&ses->server->srv_mutex);
1202 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1203 spin_lock(&GlobalMid_Lock);
1204 ses->status = CifsGood;
1205 ses->need_reconnect = false;
1206 spin_unlock(&GlobalMid_Lock);
1210 #ifdef CONFIG_CIFS_UPCALL
1212 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1215 struct cifs_ses *ses = sess_data->ses;
1216 struct cifs_spnego_msg *msg;
1217 struct key *spnego_key = NULL;
1218 struct smb2_sess_setup_rsp *rsp = NULL;
1220 rc = SMB2_sess_alloc_buffer(sess_data);
1224 spnego_key = cifs_get_spnego_key(ses);
1225 if (IS_ERR(spnego_key)) {
1226 rc = PTR_ERR(spnego_key);
1231 msg = spnego_key->payload.data[0];
1233 * check version field to make sure that cifs.upcall is
1234 * sending us a response in an expected form
1236 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1238 "bad cifs.upcall version. Expected %d got %d",
1239 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1241 goto out_put_spnego_key;
1244 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1246 if (!ses->auth_key.response) {
1248 "Kerberos can't allocate (%u bytes) memory",
1251 goto out_put_spnego_key;
1253 ses->auth_key.len = msg->sesskey_len;
1255 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1256 sess_data->iov[1].iov_len = msg->secblob_len;
1258 rc = SMB2_sess_sendreceive(sess_data);
1260 goto out_put_spnego_key;
1262 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1263 ses->Suid = rsp->sync_hdr.SessionId;
1265 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1267 rc = SMB2_sess_establish_session(sess_data);
1269 key_invalidate(spnego_key);
1270 key_put(spnego_key);
1272 sess_data->result = rc;
1273 sess_data->func = NULL;
1274 SMB2_sess_free_buffer(sess_data);
1278 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1280 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1281 sess_data->result = -EOPNOTSUPP;
1282 sess_data->func = NULL;
1287 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1290 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1293 struct cifs_ses *ses = sess_data->ses;
1294 struct smb2_sess_setup_rsp *rsp = NULL;
1295 char *ntlmssp_blob = NULL;
1296 bool use_spnego = false; /* else use raw ntlmssp */
1297 u16 blob_length = 0;
1300 * If memory allocation is successful, caller of this function
1303 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1304 if (!ses->ntlmssp) {
1308 ses->ntlmssp->sesskey_per_smbsess = true;
1310 rc = SMB2_sess_alloc_buffer(sess_data);
1314 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1316 if (ntlmssp_blob == NULL) {
1321 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1323 /* BB eventually need to add this */
1324 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1328 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1329 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1331 sess_data->iov[1].iov_base = ntlmssp_blob;
1332 sess_data->iov[1].iov_len = blob_length;
1334 rc = SMB2_sess_sendreceive(sess_data);
1335 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1337 /* If true, rc here is expected and not an error */
1338 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1339 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1345 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1346 le16_to_cpu(rsp->SecurityBufferOffset)) {
1347 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1348 le16_to_cpu(rsp->SecurityBufferOffset));
1352 rc = decode_ntlmssp_challenge(rsp->Buffer,
1353 le16_to_cpu(rsp->SecurityBufferLength), ses);
1357 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1360 ses->Suid = rsp->sync_hdr.SessionId;
1361 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1364 kfree(ntlmssp_blob);
1365 SMB2_sess_free_buffer(sess_data);
1367 sess_data->result = 0;
1368 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1372 kfree(ses->ntlmssp);
1373 ses->ntlmssp = NULL;
1374 sess_data->result = rc;
1375 sess_data->func = NULL;
1379 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1382 struct cifs_ses *ses = sess_data->ses;
1383 struct smb2_sess_setup_req *req;
1384 struct smb2_sess_setup_rsp *rsp = NULL;
1385 unsigned char *ntlmssp_blob = NULL;
1386 bool use_spnego = false; /* else use raw ntlmssp */
1387 u16 blob_length = 0;
1389 rc = SMB2_sess_alloc_buffer(sess_data);
1393 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1394 req->sync_hdr.SessionId = ses->Suid;
1396 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1399 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1404 /* BB eventually need to add this */
1405 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1409 sess_data->iov[1].iov_base = ntlmssp_blob;
1410 sess_data->iov[1].iov_len = blob_length;
1412 rc = SMB2_sess_sendreceive(sess_data);
1416 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1418 ses->Suid = rsp->sync_hdr.SessionId;
1419 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1421 rc = SMB2_sess_establish_session(sess_data);
1423 kfree(ntlmssp_blob);
1424 SMB2_sess_free_buffer(sess_data);
1425 kfree(ses->ntlmssp);
1426 ses->ntlmssp = NULL;
1427 sess_data->result = rc;
1428 sess_data->func = NULL;
1432 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1436 type = smb2_select_sectype(ses->server, ses->sectype);
1437 cifs_dbg(FYI, "sess setup type %d\n", type);
1438 if (type == Unspecified) {
1440 "Unable to select appropriate authentication method!");
1446 sess_data->func = SMB2_auth_kerberos;
1449 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1452 cifs_dbg(VFS, "secType %d not supported!\n", type);
1460 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1461 const struct nls_table *nls_cp)
1464 struct TCP_Server_Info *server = ses->server;
1465 struct SMB2_sess_data *sess_data;
1467 cifs_dbg(FYI, "Session Setup\n");
1470 WARN(1, "%s: server is NULL!\n", __func__);
1474 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1478 rc = SMB2_select_sec(ses, sess_data);
1481 sess_data->xid = xid;
1482 sess_data->ses = ses;
1483 sess_data->buf0_type = CIFS_NO_BUFFER;
1484 sess_data->nls_cp = (struct nls_table *) nls_cp;
1485 sess_data->previous_session = ses->Suid;
1488 * Initialize the session hash with the server one.
1490 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1491 SMB2_PREAUTH_HASH_SIZE);
1493 while (sess_data->func)
1494 sess_data->func(sess_data);
1496 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1497 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
1498 rc = sess_data->result;
1505 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1507 struct smb_rqst rqst;
1508 struct smb2_logoff_req *req; /* response is also trivial struct */
1510 struct TCP_Server_Info *server;
1512 unsigned int total_len;
1514 struct kvec rsp_iov;
1517 cifs_dbg(FYI, "disconnect session %p\n", ses);
1519 if (ses && (ses->server))
1520 server = ses->server;
1524 /* no need to send SMB logoff if uid already closed due to reconnect */
1525 if (ses->need_reconnect)
1526 goto smb2_session_already_dead;
1528 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
1532 /* since no tcon, smb2_init can not do this, so do here */
1533 req->sync_hdr.SessionId = ses->Suid;
1535 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1536 flags |= CIFS_TRANSFORM_REQ;
1537 else if (server->sign)
1538 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1540 flags |= CIFS_NO_RESP;
1542 iov[0].iov_base = (char *)req;
1543 iov[0].iov_len = total_len;
1545 memset(&rqst, 0, sizeof(struct smb_rqst));
1549 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1550 cifs_small_buf_release(req);
1552 * No tcon so can't do
1553 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1556 smb2_session_already_dead:
1560 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1562 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1565 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1567 /* These are similar values to what Windows uses */
1568 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1570 tcon->max_chunks = 256;
1571 tcon->max_bytes_chunk = 1048576;
1572 tcon->max_bytes_copy = 16777216;
1576 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1577 struct cifs_tcon *tcon, const struct nls_table *cp)
1579 struct smb_rqst rqst;
1580 struct smb2_tree_connect_req *req;
1581 struct smb2_tree_connect_rsp *rsp = NULL;
1583 struct kvec rsp_iov = { NULL, 0 };
1587 __le16 *unc_path = NULL;
1589 unsigned int total_len;
1591 cifs_dbg(FYI, "TCON\n");
1593 if (!(ses->server) || !tree)
1596 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1597 if (unc_path == NULL)
1600 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1602 if (unc_path_len < 2) {
1607 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1609 atomic_set(&tcon->num_remote_opens, 0);
1610 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1617 if (smb3_encryption_required(tcon))
1618 flags |= CIFS_TRANSFORM_REQ;
1620 iov[0].iov_base = (char *)req;
1622 iov[0].iov_len = total_len - 1;
1624 /* Testing shows that buffer offset must be at location of Buffer[0] */
1625 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1627 req->PathLength = cpu_to_le16(unc_path_len - 2);
1628 iov[1].iov_base = unc_path;
1629 iov[1].iov_len = unc_path_len;
1631 /* 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 */
1632 if ((ses->server->dialect == SMB311_PROT_ID) &&
1633 !smb3_encryption_required(tcon))
1634 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1636 memset(&rqst, 0, sizeof(struct smb_rqst));
1640 /* Need 64 for max size write so ask for more in case not there yet */
1641 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1643 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
1644 cifs_small_buf_release(req);
1645 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1646 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1649 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1650 tcon->need_reconnect = true;
1652 goto tcon_error_exit;
1655 switch (rsp->ShareType) {
1656 case SMB2_SHARE_TYPE_DISK:
1657 cifs_dbg(FYI, "connection to disk share\n");
1659 case SMB2_SHARE_TYPE_PIPE:
1661 cifs_dbg(FYI, "connection to pipe share\n");
1663 case SMB2_SHARE_TYPE_PRINT:
1665 cifs_dbg(FYI, "connection to printer\n");
1668 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1670 goto tcon_error_exit;
1673 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1674 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1675 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1676 tcon->tidStatus = CifsGood;
1677 tcon->need_reconnect = false;
1678 tcon->tid = rsp->sync_hdr.TreeId;
1679 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1681 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1682 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1683 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1686 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1687 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1689 init_copy_chunk_defaults(tcon);
1690 if (tcon->ses->server->ops->validate_negotiate)
1691 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1694 free_rsp_buf(resp_buftype, rsp);
1699 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1700 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1706 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1708 struct smb_rqst rqst;
1709 struct smb2_tree_disconnect_req *req; /* response is trivial */
1711 struct cifs_ses *ses = tcon->ses;
1713 unsigned int total_len;
1715 struct kvec rsp_iov;
1718 cifs_dbg(FYI, "Tree Disconnect\n");
1720 if (!ses || !(ses->server))
1723 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1726 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1731 if (smb3_encryption_required(tcon))
1732 flags |= CIFS_TRANSFORM_REQ;
1734 flags |= CIFS_NO_RESP;
1736 iov[0].iov_base = (char *)req;
1737 iov[0].iov_len = total_len;
1739 memset(&rqst, 0, sizeof(struct smb_rqst));
1743 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1744 cifs_small_buf_release(req);
1746 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1752 static struct create_durable *
1753 create_durable_buf(void)
1755 struct create_durable *buf;
1757 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1761 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1762 (struct create_durable, Data));
1763 buf->ccontext.DataLength = cpu_to_le32(16);
1764 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1765 (struct create_durable, Name));
1766 buf->ccontext.NameLength = cpu_to_le16(4);
1767 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1775 static struct create_durable *
1776 create_reconnect_durable_buf(struct cifs_fid *fid)
1778 struct create_durable *buf;
1780 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1784 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1785 (struct create_durable, Data));
1786 buf->ccontext.DataLength = cpu_to_le32(16);
1787 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1788 (struct create_durable, Name));
1789 buf->ccontext.NameLength = cpu_to_le16(4);
1790 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1791 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1792 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1801 smb2_parse_lease_state(struct TCP_Server_Info *server,
1802 struct smb2_create_rsp *rsp,
1803 unsigned int *epoch, char *lease_key)
1806 struct create_context *cc;
1808 unsigned int remaining;
1811 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
1812 remaining = le32_to_cpu(rsp->CreateContextsLength);
1813 cc = (struct create_context *)data_offset;
1814 while (remaining >= sizeof(struct create_context)) {
1815 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1816 if (le16_to_cpu(cc->NameLength) == 4 &&
1817 strncmp(name, "RqLs", 4) == 0)
1818 return server->ops->parse_lease_buf(cc, epoch,
1821 next = le32_to_cpu(cc->Next);
1825 cc = (struct create_context *)((char *)cc + next);
1832 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1833 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
1835 struct smb2_create_req *req = iov[0].iov_base;
1836 unsigned int num = *num_iovec;
1838 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
1839 if (iov[num].iov_base == NULL)
1841 iov[num].iov_len = server->vals->create_lease_size;
1842 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1843 if (!req->CreateContextsOffset)
1844 req->CreateContextsOffset = cpu_to_le32(
1845 sizeof(struct smb2_create_req) +
1846 iov[num - 1].iov_len);
1847 le32_add_cpu(&req->CreateContextsLength,
1848 server->vals->create_lease_size);
1849 *num_iovec = num + 1;
1853 static struct create_durable_v2 *
1854 create_durable_v2_buf(struct cifs_fid *pfid)
1856 struct create_durable_v2 *buf;
1858 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1862 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1863 (struct create_durable_v2, dcontext));
1864 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1865 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1866 (struct create_durable_v2, Name));
1867 buf->ccontext.NameLength = cpu_to_le16(4);
1869 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1870 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1871 generate_random_uuid(buf->dcontext.CreateGuid);
1872 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1874 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1882 static struct create_durable_handle_reconnect_v2 *
1883 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1885 struct create_durable_handle_reconnect_v2 *buf;
1887 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1892 buf->ccontext.DataOffset =
1893 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1895 buf->ccontext.DataLength =
1896 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1897 buf->ccontext.NameOffset =
1898 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1900 buf->ccontext.NameLength = cpu_to_le16(4);
1902 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1903 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1904 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1905 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1907 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1916 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1917 struct cifs_open_parms *oparms)
1919 struct smb2_create_req *req = iov[0].iov_base;
1920 unsigned int num = *num_iovec;
1922 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1923 if (iov[num].iov_base == NULL)
1925 iov[num].iov_len = sizeof(struct create_durable_v2);
1926 if (!req->CreateContextsOffset)
1927 req->CreateContextsOffset =
1928 cpu_to_le32(sizeof(struct smb2_create_req) +
1930 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1931 *num_iovec = num + 1;
1936 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1937 struct cifs_open_parms *oparms)
1939 struct smb2_create_req *req = iov[0].iov_base;
1940 unsigned int num = *num_iovec;
1942 /* indicate that we don't need to relock the file */
1943 oparms->reconnect = false;
1945 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1946 if (iov[num].iov_base == NULL)
1948 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1949 if (!req->CreateContextsOffset)
1950 req->CreateContextsOffset =
1951 cpu_to_le32(sizeof(struct smb2_create_req) +
1953 le32_add_cpu(&req->CreateContextsLength,
1954 sizeof(struct create_durable_handle_reconnect_v2));
1955 *num_iovec = num + 1;
1960 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1961 struct cifs_open_parms *oparms, bool use_persistent)
1963 struct smb2_create_req *req = iov[0].iov_base;
1964 unsigned int num = *num_iovec;
1966 if (use_persistent) {
1967 if (oparms->reconnect)
1968 return add_durable_reconnect_v2_context(iov, num_iovec,
1971 return add_durable_v2_context(iov, num_iovec, oparms);
1974 if (oparms->reconnect) {
1975 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1976 /* indicate that we don't need to relock the file */
1977 oparms->reconnect = false;
1979 iov[num].iov_base = create_durable_buf();
1980 if (iov[num].iov_base == NULL)
1982 iov[num].iov_len = sizeof(struct create_durable);
1983 if (!req->CreateContextsOffset)
1984 req->CreateContextsOffset =
1985 cpu_to_le32(sizeof(struct smb2_create_req) +
1987 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1988 *num_iovec = num + 1;
1992 /* See MS-SMB2 2.2.13.2.7 */
1993 static struct crt_twarp_ctxt *
1994 create_twarp_buf(__u64 timewarp)
1996 struct crt_twarp_ctxt *buf;
1998 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2002 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2003 (struct crt_twarp_ctxt, Timestamp));
2004 buf->ccontext.DataLength = cpu_to_le32(8);
2005 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2006 (struct crt_twarp_ctxt, Name));
2007 buf->ccontext.NameLength = cpu_to_le16(4);
2008 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2013 buf->Timestamp = cpu_to_le64(timewarp);
2017 /* See MS-SMB2 2.2.13.2.7 */
2019 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2021 struct smb2_create_req *req = iov[0].iov_base;
2022 unsigned int num = *num_iovec;
2024 iov[num].iov_base = create_twarp_buf(timewarp);
2025 if (iov[num].iov_base == NULL)
2027 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2028 if (!req->CreateContextsOffset)
2029 req->CreateContextsOffset = cpu_to_le32(
2030 sizeof(struct smb2_create_req) +
2031 iov[num - 1].iov_len);
2032 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2033 *num_iovec = num + 1;
2038 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2039 const char *treename, const __le16 *path)
2041 int treename_len, path_len;
2042 struct nls_table *cp;
2043 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2048 treename_len = strlen(treename);
2049 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2055 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2058 * make room for one path separator between the treename and
2061 *out_len = treename_len + 1 + path_len;
2064 * final path needs to be null-terminated UTF16 with a
2068 *out_size = roundup((*out_len+1)*2, 8);
2069 *out_path = kzalloc(*out_size, GFP_KERNEL);
2073 cp = load_nls_default();
2074 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2075 UniStrcat(*out_path, sep);
2076 UniStrcat(*out_path, path);
2082 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2083 umode_t mode, struct cifs_tcon *tcon,
2084 const char *full_path,
2085 struct cifs_sb_info *cifs_sb)
2087 struct smb_rqst rqst;
2088 struct smb2_create_req *req;
2089 struct smb2_create_rsp *rsp = NULL;
2090 struct cifs_ses *ses = tcon->ses;
2091 struct kvec iov[3]; /* make sure at least one for each open context */
2092 struct kvec rsp_iov = {NULL, 0};
2095 __le16 *copy_path = NULL;
2098 unsigned int n_iov = 2;
2099 __u32 file_attributes = 0;
2100 char *pc_buf = NULL;
2102 unsigned int total_len;
2103 __le16 *utf16_path = NULL;
2105 cifs_dbg(FYI, "mkdir\n");
2107 /* resource #1: path allocation */
2108 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2112 if (!ses || !(ses->server)) {
2117 /* resource #2: request */
2118 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2123 if (smb3_encryption_required(tcon))
2124 flags |= CIFS_TRANSFORM_REQ;
2126 req->ImpersonationLevel = IL_IMPERSONATION;
2127 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2128 /* File attributes ignored on open (used in create though) */
2129 req->FileAttributes = cpu_to_le32(file_attributes);
2130 req->ShareAccess = FILE_SHARE_ALL_LE;
2131 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2132 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2134 iov[0].iov_base = (char *)req;
2135 /* -1 since last byte is buf[0] which is sent below (path) */
2136 iov[0].iov_len = total_len - 1;
2138 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2140 /* [MS-SMB2] 2.2.13 NameOffset:
2141 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2142 * the SMB2 header, the file name includes a prefix that will
2143 * be processed during DFS name normalization as specified in
2144 * section 3.3.5.9. Otherwise, the file name is relative to
2145 * the share that is identified by the TreeId in the SMB2
2148 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2151 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2152 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2154 tcon->treeName, utf16_path);
2158 req->NameLength = cpu_to_le16(name_len * 2);
2159 uni_path_len = copy_size;
2160 /* free before overwriting resource */
2162 utf16_path = copy_path;
2164 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2165 /* MUST set path len (NameLength) to 0 opening root of share */
2166 req->NameLength = cpu_to_le16(uni_path_len - 2);
2167 if (uni_path_len % 8 != 0) {
2168 copy_size = roundup(uni_path_len, 8);
2169 copy_path = kzalloc(copy_size, GFP_KERNEL);
2174 memcpy((char *)copy_path, (const char *)utf16_path,
2176 uni_path_len = copy_size;
2177 /* free before overwriting resource */
2179 utf16_path = copy_path;
2183 iov[1].iov_len = uni_path_len;
2184 iov[1].iov_base = utf16_path;
2185 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2187 if (tcon->posix_extensions) {
2188 /* resource #3: posix buf */
2189 rc = add_posix_context(iov, &n_iov, mode);
2192 pc_buf = iov[n_iov-1].iov_base;
2196 memset(&rqst, 0, sizeof(struct smb_rqst));
2198 rqst.rq_nvec = n_iov;
2200 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2201 FILE_WRITE_ATTRIBUTES);
2202 /* resource #4: response buffer */
2203 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2205 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2206 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2208 FILE_WRITE_ATTRIBUTES, rc);
2209 goto err_free_rsp_buf;
2212 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2213 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2214 ses->Suid, CREATE_NOT_FILE,
2215 FILE_WRITE_ATTRIBUTES);
2217 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2219 /* Eventually save off posix specific response info and timestaps */
2222 free_rsp_buf(resp_buftype, rsp);
2225 cifs_small_buf_release(req);
2232 SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2233 struct cifs_open_parms *oparms, __le16 *path)
2235 struct TCP_Server_Info *server = tcon->ses->server;
2236 struct smb2_create_req *req;
2237 unsigned int n_iov = 2;
2238 __u32 file_attributes = 0;
2241 unsigned int total_len;
2242 struct kvec *iov = rqst->rq_iov;
2246 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2250 iov[0].iov_base = (char *)req;
2251 /* -1 since last byte is buf[0] which is sent below (path) */
2252 iov[0].iov_len = total_len - 1;
2254 if (oparms->create_options & CREATE_OPTION_READONLY)
2255 file_attributes |= ATTR_READONLY;
2256 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2257 file_attributes |= ATTR_SYSTEM;
2259 req->ImpersonationLevel = IL_IMPERSONATION;
2260 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2261 /* File attributes ignored on open (used in create though) */
2262 req->FileAttributes = cpu_to_le32(file_attributes);
2263 req->ShareAccess = FILE_SHARE_ALL_LE;
2264 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2265 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2266 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2268 /* [MS-SMB2] 2.2.13 NameOffset:
2269 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2270 * the SMB2 header, the file name includes a prefix that will
2271 * be processed during DFS name normalization as specified in
2272 * section 3.3.5.9. Otherwise, the file name is relative to
2273 * the share that is identified by the TreeId in the SMB2
2276 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2279 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2280 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2282 tcon->treeName, path);
2285 req->NameLength = cpu_to_le16(name_len * 2);
2286 uni_path_len = copy_size;
2289 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2290 /* MUST set path len (NameLength) to 0 opening root of share */
2291 req->NameLength = cpu_to_le16(uni_path_len - 2);
2292 copy_size = uni_path_len;
2293 if (copy_size % 8 != 0)
2294 copy_size = roundup(copy_size, 8);
2295 copy_path = kzalloc(copy_size, GFP_KERNEL);
2298 memcpy((char *)copy_path, (const char *)path,
2300 uni_path_len = copy_size;
2304 iov[1].iov_len = uni_path_len;
2305 iov[1].iov_base = path;
2307 if (!server->oplocks)
2308 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2310 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2311 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2312 req->RequestedOplockLevel = *oplock;
2313 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2314 (oparms->create_options & CREATE_NOT_FILE))
2315 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2317 rc = add_lease_context(server, iov, &n_iov,
2318 oparms->fid->lease_key, oplock);
2323 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2324 /* need to set Next field of lease context if we request it */
2325 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2326 struct create_context *ccontext =
2327 (struct create_context *)iov[n_iov-1].iov_base;
2329 cpu_to_le32(server->vals->create_lease_size);
2332 rc = add_durable_context(iov, &n_iov, oparms,
2333 tcon->use_persistent);
2338 if (tcon->posix_extensions) {
2340 struct create_context *ccontext =
2341 (struct create_context *)iov[n_iov-1].iov_base;
2343 cpu_to_le32(iov[n_iov-1].iov_len);
2346 rc = add_posix_context(iov, &n_iov, oparms->mode);
2351 if (tcon->snapshot_time) {
2352 cifs_dbg(FYI, "adding snapshot context\n");
2354 struct create_context *ccontext =
2355 (struct create_context *)iov[n_iov-1].iov_base;
2357 cpu_to_le32(iov[n_iov-1].iov_len);
2360 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2366 rqst->rq_nvec = n_iov;
2370 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2371 * All other vectors are freed by kfree().
2374 SMB2_open_free(struct smb_rqst *rqst)
2378 if (rqst && rqst->rq_iov) {
2379 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2380 for (i = 1; i < rqst->rq_nvec; i++)
2381 if (rqst->rq_iov[i].iov_base != smb2_padding)
2382 kfree(rqst->rq_iov[i].iov_base);
2387 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2388 __u8 *oplock, struct smb2_file_all_info *buf,
2389 struct kvec *err_iov, int *buftype)
2391 struct smb_rqst rqst;
2392 struct smb2_create_rsp *rsp = NULL;
2393 struct TCP_Server_Info *server;
2394 struct cifs_tcon *tcon = oparms->tcon;
2395 struct cifs_ses *ses = tcon->ses;
2396 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2397 struct kvec rsp_iov = {NULL, 0};
2398 int resp_buftype = CIFS_NO_BUFFER;
2402 cifs_dbg(FYI, "create/open\n");
2403 if (ses && (ses->server))
2404 server = ses->server;
2408 if (smb3_encryption_required(tcon))
2409 flags |= CIFS_TRANSFORM_REQ;
2411 memset(&rqst, 0, sizeof(struct smb_rqst));
2412 memset(&iov, 0, sizeof(iov));
2414 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2416 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2420 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2421 oparms->create_options, oparms->desired_access);
2423 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2425 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2428 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2429 if (err_iov && rsp) {
2431 *buftype = resp_buftype;
2432 resp_buftype = CIFS_NO_BUFFER;
2435 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2436 oparms->create_options, oparms->desired_access, rc);
2439 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2440 ses->Suid, oparms->create_options,
2441 oparms->desired_access);
2443 atomic_inc(&tcon->num_remote_opens);
2444 oparms->fid->persistent_fid = rsp->PersistentFileId;
2445 oparms->fid->volatile_fid = rsp->VolatileFileId;
2446 #ifdef CONFIG_CIFS_DEBUG2
2447 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2448 #endif /* CIFS_DEBUG2 */
2451 memcpy(buf, &rsp->CreationTime, 32);
2452 buf->AllocationSize = rsp->AllocationSize;
2453 buf->EndOfFile = rsp->EndofFile;
2454 buf->Attributes = rsp->FileAttributes;
2455 buf->NumberOfLinks = cpu_to_le32(1);
2456 buf->DeletePending = 0;
2459 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
2460 *oplock = smb2_parse_lease_state(server, rsp,
2461 &oparms->fid->epoch,
2462 oparms->fid->lease_key);
2464 *oplock = rsp->OplockLevel;
2466 SMB2_open_free(&rqst);
2467 free_rsp_buf(resp_buftype, rsp);
2472 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2475 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2476 u64 volatile_fid, u32 opcode, bool is_fsctl,
2477 char *in_data, u32 indatalen,
2478 char **out_data, u32 *plen /* returned data len */)
2480 struct smb_rqst rqst;
2481 struct smb2_ioctl_req *req;
2482 struct smb2_ioctl_rsp *rsp;
2483 struct cifs_ses *ses;
2485 struct kvec rsp_iov;
2490 unsigned int total_len;
2492 cifs_dbg(FYI, "SMB2 IOCTL\n");
2494 if (out_data != NULL)
2497 /* zero out returned data len, in case of error */
2506 if (!ses || !(ses->server))
2509 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
2513 if (smb3_encryption_required(tcon))
2514 flags |= CIFS_TRANSFORM_REQ;
2516 req->CtlCode = cpu_to_le32(opcode);
2517 req->PersistentFileId = persistent_fid;
2518 req->VolatileFileId = volatile_fid;
2521 req->InputCount = cpu_to_le32(indatalen);
2522 /* do not set InputOffset if no input data */
2524 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
2525 iov[1].iov_base = in_data;
2526 iov[1].iov_len = indatalen;
2531 req->OutputOffset = 0;
2532 req->OutputCount = 0; /* MBZ */
2535 * Could increase MaxOutputResponse, but that would require more
2536 * than one credit. Windows typically sets this smaller, but for some
2537 * ioctls it may be useful to allow server to send more. No point
2538 * limiting what the server can send as long as fits in one credit
2539 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
2540 * (by default, note that it can be overridden to make max larger)
2541 * in responses (except for read responses which can be bigger.
2542 * We may want to bump this limit up
2544 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
2547 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2551 iov[0].iov_base = (char *)req;
2554 * If no input data, the size of ioctl struct in
2555 * protocol spec still includes a 1 byte data buffer,
2556 * but if input data passed to ioctl, we do not
2557 * want to double count this, so we do not send
2558 * the dummy one byte of data in iovec[0] if sending
2559 * input data (in iovec[1]).
2563 iov[0].iov_len = total_len - 1;
2565 iov[0].iov_len = total_len;
2567 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2568 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
2569 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
2571 memset(&rqst, 0, sizeof(struct smb_rqst));
2573 rqst.rq_nvec = n_iov;
2575 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2577 cifs_small_buf_release(req);
2578 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
2581 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2582 ses->Suid, 0, opcode, rc);
2584 if ((rc != 0) && (rc != -EINVAL)) {
2585 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2587 } else if (rc == -EINVAL) {
2588 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2589 (opcode != FSCTL_SRV_COPYCHUNK)) {
2590 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2595 /* check if caller wants to look at return data or just return rc */
2596 if ((plen == NULL) || (out_data == NULL))
2599 *plen = le32_to_cpu(rsp->OutputCount);
2601 /* We check for obvious errors in the output buffer length and offset */
2603 goto ioctl_exit; /* server returned no data */
2604 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
2605 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2611 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
2612 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2613 le32_to_cpu(rsp->OutputOffset));
2619 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2621 if (*out_data == NULL) {
2627 free_rsp_buf(resp_buftype, rsp);
2632 * Individual callers to ioctl worker function follow
2636 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2637 u64 persistent_fid, u64 volatile_fid)
2640 struct compress_ioctl fsctl_input;
2641 char *ret_data = NULL;
2643 fsctl_input.CompressionState =
2644 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
2646 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2647 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2648 (char *)&fsctl_input /* data input */,
2649 2 /* in data len */, &ret_data /* out data */, NULL);
2651 cifs_dbg(FYI, "set compression rc %d\n", rc);
2657 SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2658 u64 persistent_fid, u64 volatile_fid)
2660 struct smb2_close_req *req;
2661 struct kvec *iov = rqst->rq_iov;
2662 unsigned int total_len;
2665 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2669 req->PersistentFileId = persistent_fid;
2670 req->VolatileFileId = volatile_fid;
2671 iov[0].iov_base = (char *)req;
2672 iov[0].iov_len = total_len;
2678 SMB2_close_free(struct smb_rqst *rqst)
2680 if (rqst && rqst->rq_iov)
2681 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2685 SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2686 u64 persistent_fid, u64 volatile_fid, int flags)
2688 struct smb_rqst rqst;
2689 struct smb2_close_rsp *rsp = NULL;
2690 struct cifs_ses *ses = tcon->ses;
2692 struct kvec rsp_iov;
2693 int resp_buftype = CIFS_NO_BUFFER;
2696 cifs_dbg(FYI, "Close\n");
2698 if (!ses || !(ses->server))
2701 if (smb3_encryption_required(tcon))
2702 flags |= CIFS_TRANSFORM_REQ;
2704 memset(&rqst, 0, sizeof(struct smb_rqst));
2705 memset(&iov, 0, sizeof(iov));
2709 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid);
2713 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2714 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2717 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
2718 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2723 atomic_dec(&tcon->num_remote_opens);
2725 /* BB FIXME - decode close response, update inode for caching */
2728 SMB2_close_free(&rqst);
2729 free_rsp_buf(resp_buftype, rsp);
2734 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2735 u64 persistent_fid, u64 volatile_fid)
2737 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2741 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
2742 struct kvec *iov, unsigned int min_buf_size)
2744 unsigned int smb_len = iov->iov_len;
2745 char *end_of_smb = smb_len + (char *)iov->iov_base;
2746 char *begin_of_buf = offset + (char *)iov->iov_base;
2747 char *end_of_buf = begin_of_buf + buffer_length;
2750 if (buffer_length < min_buf_size) {
2751 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2752 buffer_length, min_buf_size);
2756 /* check if beyond RFC1001 maximum length */
2757 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
2758 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2759 buffer_length, smb_len);
2763 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
2764 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
2772 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2773 * Caller must free buffer.
2776 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
2777 struct kvec *iov, unsigned int minbufsize,
2780 char *begin_of_buf = offset + (char *)iov->iov_base;
2786 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
2790 memcpy(data, begin_of_buf, buffer_length);
2796 SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2797 u64 persistent_fid, u64 volatile_fid,
2798 u8 info_class, u8 info_type, u32 additional_info,
2799 size_t output_len, size_t input_len, void *input)
2801 struct smb2_query_info_req *req;
2802 struct kvec *iov = rqst->rq_iov;
2803 unsigned int total_len;
2806 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2811 req->InfoType = info_type;
2812 req->FileInfoClass = info_class;
2813 req->PersistentFileId = persistent_fid;
2814 req->VolatileFileId = volatile_fid;
2815 req->AdditionalInformation = cpu_to_le32(additional_info);
2817 req->OutputBufferLength = cpu_to_le32(output_len);
2819 req->InputBufferLength = cpu_to_le32(input_len);
2820 /* total_len for smb query request never close to le16 max */
2821 req->InputBufferOffset = cpu_to_le16(total_len - 1);
2822 memcpy(req->Buffer, input, input_len);
2825 iov[0].iov_base = (char *)req;
2827 iov[0].iov_len = total_len - 1 + input_len;
2832 SMB2_query_info_free(struct smb_rqst *rqst)
2834 if (rqst && rqst->rq_iov)
2835 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2839 query_info(const unsigned int xid, struct cifs_tcon *tcon,
2840 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2841 u32 additional_info, size_t output_len, size_t min_len, void **data,
2844 struct smb_rqst rqst;
2845 struct smb2_query_info_rsp *rsp = NULL;
2847 struct kvec rsp_iov;
2849 int resp_buftype = CIFS_NO_BUFFER;
2850 struct cifs_ses *ses = tcon->ses;
2852 bool allocated = false;
2854 cifs_dbg(FYI, "Query Info\n");
2856 if (!ses || !(ses->server))
2859 if (smb3_encryption_required(tcon))
2860 flags |= CIFS_TRANSFORM_REQ;
2862 memset(&rqst, 0, sizeof(struct smb_rqst));
2863 memset(&iov, 0, sizeof(iov));
2867 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
2868 info_class, info_type, additional_info,
2869 output_len, 0, NULL);
2873 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
2874 ses->Suid, info_class, (__u32)info_type);
2876 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2877 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2880 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2881 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
2882 ses->Suid, info_class, (__u32)info_type, rc);
2886 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
2887 ses->Suid, info_class, (__u32)info_type);
2890 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2892 *data = kmalloc(*dlen, GFP_KERNEL);
2895 "Error %d allocating memory for acl\n",
2905 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
2906 le32_to_cpu(rsp->OutputBufferLength),
2907 &rsp_iov, min_len, *data);
2908 if (rc && allocated) {
2915 SMB2_query_info_free(&rqst);
2916 free_rsp_buf(resp_buftype, rsp);
2920 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2921 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
2923 return query_info(xid, tcon, persistent_fid, volatile_fid,
2924 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
2925 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
2926 sizeof(struct smb2_file_all_info), (void **)&data,
2931 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2932 u64 persistent_fid, u64 volatile_fid,
2933 void **data, u32 *plen)
2935 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2938 return query_info(xid, tcon, persistent_fid, volatile_fid,
2939 0, SMB2_O_INFO_SECURITY, additional_info,
2940 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
2944 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2945 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2947 return query_info(xid, tcon, persistent_fid, volatile_fid,
2948 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
2949 sizeof(struct smb2_file_internal_info),
2950 sizeof(struct smb2_file_internal_info),
2951 (void **)&uniqueid, NULL);
2955 * This is a no-op for now. We're not really interested in the reply, but
2956 * rather in the fact that the server sent one and that server->lstrp
2959 * FIXME: maybe we should consider checking that the reply matches request?
2962 smb2_echo_callback(struct mid_q_entry *mid)
2964 struct TCP_Server_Info *server = mid->callback_data;
2965 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
2966 struct cifs_credits credits = { .value = 0, .instance = 0 };
2968 if (mid->mid_state == MID_RESPONSE_RECEIVED
2969 || mid->mid_state == MID_RESPONSE_MALFORMED) {
2970 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
2971 credits.instance = server->reconnect_instance;
2974 DeleteMidQEntry(mid);
2975 add_credits(server, &credits, CIFS_ECHO_OP);
2978 void smb2_reconnect_server(struct work_struct *work)
2980 struct TCP_Server_Info *server = container_of(work,
2981 struct TCP_Server_Info, reconnect.work);
2982 struct cifs_ses *ses;
2983 struct cifs_tcon *tcon, *tcon2;
2984 struct list_head tmp_list;
2985 int tcon_exist = false;
2987 int resched = false;
2990 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2991 mutex_lock(&server->reconnect_mutex);
2993 INIT_LIST_HEAD(&tmp_list);
2994 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2996 spin_lock(&cifs_tcp_ses_lock);
2997 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2998 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2999 if (tcon->need_reconnect || tcon->need_reopen_files) {
3001 list_add_tail(&tcon->rlist, &tmp_list);
3005 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3006 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3011 * Get the reference to server struct to be sure that the last call of
3012 * cifs_put_tcon() in the loop below won't release the server pointer.
3015 server->srv_count++;
3017 spin_unlock(&cifs_tcp_ses_lock);
3019 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3020 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3022 cifs_reopen_persistent_handles(tcon);
3025 list_del_init(&tcon->rlist);
3026 cifs_put_tcon(tcon);
3029 cifs_dbg(FYI, "Reconnecting tcons finished\n");
3031 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3032 mutex_unlock(&server->reconnect_mutex);
3034 /* now we can safely release srv struct */
3036 cifs_put_tcp_session(server, 1);
3040 SMB2_echo(struct TCP_Server_Info *server)
3042 struct smb2_echo_req *req;
3045 struct smb_rqst rqst = { .rq_iov = iov,
3047 unsigned int total_len;
3049 cifs_dbg(FYI, "In echo request\n");
3051 if (server->tcpStatus == CifsNeedNegotiate) {
3052 /* No need to send echo on newly established connections */
3053 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3057 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
3061 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3063 iov[0].iov_len = total_len;
3064 iov[0].iov_base = (char *)req;
3066 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3067 server, CIFS_ECHO_OP, NULL);
3069 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3071 cifs_small_buf_release(req);
3076 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3079 struct smb_rqst rqst;
3080 struct smb2_flush_req *req;
3081 struct cifs_ses *ses = tcon->ses;
3083 struct kvec rsp_iov;
3087 unsigned int total_len;
3089 cifs_dbg(FYI, "Flush\n");
3091 if (!ses || !(ses->server))
3094 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
3098 if (smb3_encryption_required(tcon))
3099 flags |= CIFS_TRANSFORM_REQ;
3101 req->PersistentFileId = persistent_fid;
3102 req->VolatileFileId = volatile_fid;
3104 iov[0].iov_base = (char *)req;
3105 iov[0].iov_len = total_len;
3107 memset(&rqst, 0, sizeof(struct smb_rqst));
3111 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3112 cifs_small_buf_release(req);
3115 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
3116 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3120 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3125 * To form a chain of read requests, any read requests after the first should
3126 * have the end_of_chain boolean set to true.
3129 smb2_new_read_req(void **buf, unsigned int *total_len,
3130 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3131 unsigned int remaining_bytes, int request_type)
3134 struct smb2_read_plain_req *req = NULL;
3135 struct smb2_sync_hdr *shdr;
3136 struct TCP_Server_Info *server;
3138 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3143 server = io_parms->tcon->ses->server;
3145 return -ECONNABORTED;
3147 shdr = &req->sync_hdr;
3148 shdr->ProcessId = cpu_to_le32(io_parms->pid);
3150 req->PersistentFileId = io_parms->persistent_fid;
3151 req->VolatileFileId = io_parms->volatile_fid;
3152 req->ReadChannelInfoOffset = 0; /* reserved */
3153 req->ReadChannelInfoLength = 0; /* reserved */
3154 req->Channel = 0; /* reserved */
3155 req->MinimumCount = 0;
3156 req->Length = cpu_to_le32(io_parms->length);
3157 req->Offset = cpu_to_le64(io_parms->offset);
3159 trace_smb3_read_enter(0 /* xid */,
3160 io_parms->persistent_fid,
3161 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3162 io_parms->offset, io_parms->length);
3163 #ifdef CONFIG_CIFS_SMB_DIRECT
3165 * If we want to do a RDMA write, fill in and append
3166 * smbd_buffer_descriptor_v1 to the end of read request
3168 if (server->rdma && rdata && !server->sign &&
3169 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
3171 struct smbd_buffer_descriptor_v1 *v1;
3172 bool need_invalidate =
3173 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3175 rdata->mr = smbd_register_mr(
3176 server->smbd_conn, rdata->pages,
3177 rdata->nr_pages, rdata->page_offset,
3178 rdata->tailsz, true, need_invalidate);
3182 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3183 if (need_invalidate)
3184 req->Channel = SMB2_CHANNEL_RDMA_V1;
3185 req->ReadChannelInfoOffset =
3186 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
3187 req->ReadChannelInfoLength =
3188 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3189 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3190 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3191 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3192 v1->length = cpu_to_le32(rdata->mr->mr->length);
3194 *total_len += sizeof(*v1) - 1;
3197 if (request_type & CHAINED_REQUEST) {
3198 if (!(request_type & END_OF_CHAIN)) {
3199 /* next 8-byte aligned request */
3200 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3201 shdr->NextCommand = cpu_to_le32(*total_len);
3202 } else /* END_OF_CHAIN */
3203 shdr->NextCommand = 0;
3204 if (request_type & RELATED_REQUEST) {
3205 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
3207 * Related requests use info from previous read request
3210 shdr->SessionId = 0xFFFFFFFF;
3211 shdr->TreeId = 0xFFFFFFFF;
3212 req->PersistentFileId = 0xFFFFFFFF;
3213 req->VolatileFileId = 0xFFFFFFFF;
3216 if (remaining_bytes > io_parms->length)
3217 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3219 req->RemainingBytes = 0;
3226 smb2_readv_callback(struct mid_q_entry *mid)
3228 struct cifs_readdata *rdata = mid->callback_data;
3229 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3230 struct TCP_Server_Info *server = tcon->ses->server;
3231 struct smb2_sync_hdr *shdr =
3232 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
3233 struct cifs_credits credits = { .value = 0, .instance = 0 };
3234 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3236 .rq_pages = rdata->pages,
3237 .rq_offset = rdata->page_offset,
3238 .rq_npages = rdata->nr_pages,
3239 .rq_pagesz = rdata->pagesz,
3240 .rq_tailsz = rdata->tailsz };
3242 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3243 __func__, mid->mid, mid->mid_state, rdata->result,
3246 switch (mid->mid_state) {
3247 case MID_RESPONSE_RECEIVED:
3248 credits.value = le16_to_cpu(shdr->CreditRequest);
3249 credits.instance = server->reconnect_instance;
3250 /* result already set, check signature */
3251 if (server->sign && !mid->decrypted) {
3254 rc = smb2_verify_signature(&rqst, server);
3256 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
3259 /* FIXME: should this be counted toward the initiating task? */
3260 task_io_account_read(rdata->got_bytes);
3261 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3263 case MID_REQUEST_SUBMITTED:
3264 case MID_RETRY_NEEDED:
3265 rdata->result = -EAGAIN;
3266 if (server->sign && rdata->got_bytes)
3267 /* reset bytes number since we can not check a sign */
3268 rdata->got_bytes = 0;
3269 /* FIXME: should this be counted toward the initiating task? */
3270 task_io_account_read(rdata->got_bytes);
3271 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3273 case MID_RESPONSE_MALFORMED:
3274 credits.value = le16_to_cpu(shdr->CreditRequest);
3275 credits.instance = server->reconnect_instance;
3278 rdata->result = -EIO;
3280 #ifdef CONFIG_CIFS_SMB_DIRECT
3282 * If this rdata has a memmory registered, the MR can be freed
3283 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3284 * because they have limited number and are used for future I/Os
3287 smbd_deregister_mr(rdata->mr);
3291 if (rdata->result && rdata->result != -ENODATA) {
3292 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
3293 trace_smb3_read_err(0 /* xid */,
3294 rdata->cfile->fid.persistent_fid,
3295 tcon->tid, tcon->ses->Suid, rdata->offset,
3296 rdata->bytes, rdata->result);
3298 trace_smb3_read_done(0 /* xid */,
3299 rdata->cfile->fid.persistent_fid,
3300 tcon->tid, tcon->ses->Suid,
3301 rdata->offset, rdata->got_bytes);
3303 queue_work(cifsiod_wq, &rdata->work);
3304 DeleteMidQEntry(mid);
3305 add_credits(server, &credits, 0);
3308 /* smb2_async_readv - send an async read, and set up mid to handle result */
3310 smb2_async_readv(struct cifs_readdata *rdata)
3314 struct smb2_sync_hdr *shdr;
3315 struct cifs_io_parms io_parms;
3316 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3318 struct TCP_Server_Info *server;
3319 unsigned int total_len;
3321 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3322 __func__, rdata->offset, rdata->bytes);
3324 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3325 io_parms.offset = rdata->offset;
3326 io_parms.length = rdata->bytes;
3327 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3328 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3329 io_parms.pid = rdata->pid;
3331 server = io_parms.tcon->ses->server;
3333 rc = smb2_new_read_req(
3334 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
3338 if (smb3_encryption_required(io_parms.tcon))
3339 flags |= CIFS_TRANSFORM_REQ;
3341 rdata->iov[0].iov_base = buf;
3342 rdata->iov[0].iov_len = total_len;
3344 shdr = (struct smb2_sync_hdr *)buf;
3346 if (rdata->credits.value > 0) {
3347 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
3348 SMB2_MAX_BUFFER_SIZE));
3349 shdr->CreditRequest =
3350 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
3352 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3354 goto async_readv_out;
3356 flags |= CIFS_HAS_CREDITS;
3359 kref_get(&rdata->refcount);
3360 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
3361 cifs_readv_receive, smb2_readv_callback,
3362 smb3_handle_read_data, rdata, flags,
3365 kref_put(&rdata->refcount, cifs_readdata_release);
3366 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
3367 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3369 io_parms.tcon->ses->Suid,
3370 io_parms.offset, io_parms.length, rc);
3374 cifs_small_buf_release(buf);
3379 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3380 unsigned int *nbytes, char **buf, int *buf_type)
3382 struct smb_rqst rqst;
3383 int resp_buftype, rc = -EACCES;
3384 struct smb2_read_plain_req *req = NULL;
3385 struct smb2_read_rsp *rsp = NULL;
3387 struct kvec rsp_iov;
3388 unsigned int total_len;
3389 int flags = CIFS_LOG_ERROR;
3390 struct cifs_ses *ses = io_parms->tcon->ses;
3393 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
3397 if (smb3_encryption_required(io_parms->tcon))
3398 flags |= CIFS_TRANSFORM_REQ;
3400 iov[0].iov_base = (char *)req;
3401 iov[0].iov_len = total_len;
3403 memset(&rqst, 0, sizeof(struct smb_rqst));
3407 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3408 cifs_small_buf_release(req);
3410 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
3413 if (rc != -ENODATA) {
3414 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3415 cifs_dbg(VFS, "Send error in read = %d\n", rc);
3416 trace_smb3_read_err(xid, req->PersistentFileId,
3417 io_parms->tcon->tid, ses->Suid,
3418 io_parms->offset, io_parms->length,
3421 trace_smb3_read_done(xid, req->PersistentFileId,
3422 io_parms->tcon->tid, ses->Suid,
3423 io_parms->offset, 0);
3424 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3425 return rc == -ENODATA ? 0 : rc;
3427 trace_smb3_read_done(xid, req->PersistentFileId,
3428 io_parms->tcon->tid, ses->Suid,
3429 io_parms->offset, io_parms->length);
3431 *nbytes = le32_to_cpu(rsp->DataLength);
3432 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3433 (*nbytes > io_parms->length)) {
3434 cifs_dbg(FYI, "bad length %d for count %d\n",
3435 *nbytes, io_parms->length);
3441 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
3442 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3443 } else if (resp_buftype != CIFS_NO_BUFFER) {
3444 *buf = rsp_iov.iov_base;
3445 if (resp_buftype == CIFS_SMALL_BUFFER)
3446 *buf_type = CIFS_SMALL_BUFFER;
3447 else if (resp_buftype == CIFS_LARGE_BUFFER)
3448 *buf_type = CIFS_LARGE_BUFFER;
3454 * Check the mid_state and signature on received buffer (if any), and queue the
3455 * workqueue completion task.
3458 smb2_writev_callback(struct mid_q_entry *mid)
3460 struct cifs_writedata *wdata = mid->callback_data;
3461 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3462 struct TCP_Server_Info *server = tcon->ses->server;
3463 unsigned int written;
3464 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
3465 struct cifs_credits credits = { .value = 0, .instance = 0 };
3467 switch (mid->mid_state) {
3468 case MID_RESPONSE_RECEIVED:
3469 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3470 credits.instance = server->reconnect_instance;
3471 wdata->result = smb2_check_receive(mid, server, 0);
3472 if (wdata->result != 0)
3475 written = le32_to_cpu(rsp->DataLength);
3477 * Mask off high 16 bits when bytes written as returned
3478 * by the server is greater than bytes requested by the
3479 * client. OS/2 servers are known to set incorrect
3482 if (written > wdata->bytes)
3485 if (written < wdata->bytes)
3486 wdata->result = -ENOSPC;
3488 wdata->bytes = written;
3490 case MID_REQUEST_SUBMITTED:
3491 case MID_RETRY_NEEDED:
3492 wdata->result = -EAGAIN;
3494 case MID_RESPONSE_MALFORMED:
3495 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3496 credits.instance = server->reconnect_instance;
3499 wdata->result = -EIO;
3502 #ifdef CONFIG_CIFS_SMB_DIRECT
3504 * If this wdata has a memory registered, the MR can be freed
3505 * The number of MRs available is limited, it's important to recover
3506 * used MR as soon as I/O is finished. Hold MR longer in the later
3507 * I/O process can possibly result in I/O deadlock due to lack of MR
3508 * to send request on I/O retry
3511 smbd_deregister_mr(wdata->mr);
3515 if (wdata->result) {
3516 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3517 trace_smb3_write_err(0 /* no xid */,
3518 wdata->cfile->fid.persistent_fid,
3519 tcon->tid, tcon->ses->Suid, wdata->offset,
3520 wdata->bytes, wdata->result);
3522 trace_smb3_write_done(0 /* no xid */,
3523 wdata->cfile->fid.persistent_fid,
3524 tcon->tid, tcon->ses->Suid,
3525 wdata->offset, wdata->bytes);
3527 queue_work(cifsiod_wq, &wdata->work);
3528 DeleteMidQEntry(mid);
3529 add_credits(server, &credits, 0);
3532 /* smb2_async_writev - send an async write, and set up mid to handle result */
3534 smb2_async_writev(struct cifs_writedata *wdata,
3535 void (*release)(struct kref *kref))
3537 int rc = -EACCES, flags = 0;
3538 struct smb2_write_req *req = NULL;
3539 struct smb2_sync_hdr *shdr;
3540 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3541 struct TCP_Server_Info *server = tcon->ses->server;
3543 struct smb_rqst rqst = { };
3544 unsigned int total_len;
3546 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
3550 if (smb3_encryption_required(tcon))
3551 flags |= CIFS_TRANSFORM_REQ;
3553 shdr = (struct smb2_sync_hdr *)req;
3554 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
3556 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3557 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3558 req->WriteChannelInfoOffset = 0;
3559 req->WriteChannelInfoLength = 0;
3561 req->Offset = cpu_to_le64(wdata->offset);
3562 req->DataOffset = cpu_to_le16(
3563 offsetof(struct smb2_write_req, Buffer));
3564 req->RemainingBytes = 0;
3566 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
3567 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
3568 #ifdef CONFIG_CIFS_SMB_DIRECT
3570 * If we want to do a server RDMA read, fill in and append
3571 * smbd_buffer_descriptor_v1 to the end of write request
3573 if (server->rdma && !server->sign && wdata->bytes >=
3574 server->smbd_conn->rdma_readwrite_threshold) {
3576 struct smbd_buffer_descriptor_v1 *v1;
3577 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3579 wdata->mr = smbd_register_mr(
3580 server->smbd_conn, wdata->pages,
3581 wdata->nr_pages, wdata->page_offset,
3582 wdata->tailsz, false, need_invalidate);
3585 goto async_writev_out;
3588 req->DataOffset = 0;
3589 if (wdata->nr_pages > 1)
3590 req->RemainingBytes =
3592 (wdata->nr_pages - 1) * wdata->pagesz -
3593 wdata->page_offset + wdata->tailsz
3596 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
3597 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3598 if (need_invalidate)
3599 req->Channel = SMB2_CHANNEL_RDMA_V1;
3600 req->WriteChannelInfoOffset =
3601 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
3602 req->WriteChannelInfoLength =
3603 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3604 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3605 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3606 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3607 v1->length = cpu_to_le32(wdata->mr->mr->length);
3610 iov[0].iov_len = total_len - 1;
3611 iov[0].iov_base = (char *)req;
3615 rqst.rq_pages = wdata->pages;
3616 rqst.rq_offset = wdata->page_offset;
3617 rqst.rq_npages = wdata->nr_pages;
3618 rqst.rq_pagesz = wdata->pagesz;
3619 rqst.rq_tailsz = wdata->tailsz;
3620 #ifdef CONFIG_CIFS_SMB_DIRECT
3622 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
3626 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3627 wdata->offset, wdata->bytes);
3629 #ifdef CONFIG_CIFS_SMB_DIRECT
3630 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3632 req->Length = cpu_to_le32(wdata->bytes);
3634 req->Length = cpu_to_le32(wdata->bytes);
3637 if (wdata->credits.value > 0) {
3638 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
3639 SMB2_MAX_BUFFER_SIZE));
3640 shdr->CreditRequest =
3641 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
3643 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3645 goto async_writev_out;
3647 flags |= CIFS_HAS_CREDITS;
3650 kref_get(&wdata->refcount);
3651 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3652 wdata, flags, &wdata->credits);
3655 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
3656 tcon->tid, tcon->ses->Suid, wdata->offset,
3658 kref_put(&wdata->refcount, release);
3659 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3663 cifs_small_buf_release(req);
3668 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3669 * The length field from io_parms must be at least 1 and indicates a number of
3670 * elements with data to write that begins with position 1 in iov array. All
3671 * data length is specified by count.
3674 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3675 unsigned int *nbytes, struct kvec *iov, int n_vec)
3677 struct smb_rqst rqst;
3679 struct smb2_write_req *req = NULL;
3680 struct smb2_write_rsp *rsp = NULL;
3682 struct kvec rsp_iov;
3684 unsigned int total_len;
3691 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3696 if (io_parms->tcon->ses->server == NULL)
3697 return -ECONNABORTED;
3699 if (smb3_encryption_required(io_parms->tcon))
3700 flags |= CIFS_TRANSFORM_REQ;
3702 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
3704 req->PersistentFileId = io_parms->persistent_fid;
3705 req->VolatileFileId = io_parms->volatile_fid;
3706 req->WriteChannelInfoOffset = 0;
3707 req->WriteChannelInfoLength = 0;
3709 req->Length = cpu_to_le32(io_parms->length);
3710 req->Offset = cpu_to_le64(io_parms->offset);
3711 req->DataOffset = cpu_to_le16(
3712 offsetof(struct smb2_write_req, Buffer));
3713 req->RemainingBytes = 0;
3715 trace_smb3_write_enter(xid, io_parms->persistent_fid,
3716 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3717 io_parms->offset, io_parms->length);
3719 iov[0].iov_base = (char *)req;
3721 iov[0].iov_len = total_len - 1;
3723 memset(&rqst, 0, sizeof(struct smb_rqst));
3725 rqst.rq_nvec = n_vec + 1;
3727 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
3728 &resp_buftype, flags, &rsp_iov);
3729 cifs_small_buf_release(req);
3730 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
3733 trace_smb3_write_err(xid, req->PersistentFileId,
3734 io_parms->tcon->tid,
3735 io_parms->tcon->ses->Suid,
3736 io_parms->offset, io_parms->length, rc);
3737 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
3738 cifs_dbg(VFS, "Send error in write = %d\n", rc);
3740 *nbytes = le32_to_cpu(rsp->DataLength);
3741 trace_smb3_write_done(xid, req->PersistentFileId,
3742 io_parms->tcon->tid,
3743 io_parms->tcon->ses->Suid,
3744 io_parms->offset, *nbytes);
3747 free_rsp_buf(resp_buftype, rsp);
3752 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3755 unsigned int entrycount = 0;
3756 unsigned int next_offset = 0;
3758 FILE_DIRECTORY_INFO *dir_info;
3760 if (bufstart == NULL)
3763 entryptr = bufstart;
3766 if (entryptr + next_offset < entryptr ||
3767 entryptr + next_offset > end_of_buf ||
3768 entryptr + next_offset + size > end_of_buf) {
3769 cifs_dbg(VFS, "malformed search entry would overflow\n");
3773 entryptr = entryptr + next_offset;
3774 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
3776 len = le32_to_cpu(dir_info->FileNameLength);
3777 if (entryptr + len < entryptr ||
3778 entryptr + len > end_of_buf ||
3779 entryptr + len + size > end_of_buf) {
3780 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3785 *lastentry = entryptr;
3788 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
3800 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3801 u64 persistent_fid, u64 volatile_fid, int index,
3802 struct cifs_search_info *srch_inf)
3804 struct smb_rqst rqst;
3805 struct smb2_query_directory_req *req;
3806 struct smb2_query_directory_rsp *rsp = NULL;
3808 struct kvec rsp_iov;
3811 int resp_buftype = CIFS_NO_BUFFER;
3812 unsigned char *bufptr;
3813 struct TCP_Server_Info *server;
3814 struct cifs_ses *ses = tcon->ses;
3815 __le16 asteriks = cpu_to_le16('*');
3817 unsigned int output_size = CIFSMaxBufSize;
3818 size_t info_buf_size;
3820 unsigned int total_len;
3822 if (ses && (ses->server))
3823 server = ses->server;
3827 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3832 if (smb3_encryption_required(tcon))
3833 flags |= CIFS_TRANSFORM_REQ;
3835 switch (srch_inf->info_level) {
3836 case SMB_FIND_FILE_DIRECTORY_INFO:
3837 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3838 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3840 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3841 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3842 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3845 cifs_dbg(VFS, "info level %u isn't supported\n",
3846 srch_inf->info_level);
3851 req->FileIndex = cpu_to_le32(index);
3852 req->PersistentFileId = persistent_fid;
3853 req->VolatileFileId = volatile_fid;
3856 bufptr = req->Buffer;
3857 memcpy(bufptr, &asteriks, len);
3859 req->FileNameOffset =
3860 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
3861 req->FileNameLength = cpu_to_le16(len);
3863 * BB could be 30 bytes or so longer if we used SMB2 specific
3864 * buffer lengths, but this is safe and close enough.
3866 output_size = min_t(unsigned int, output_size, server->maxBuf);
3867 output_size = min_t(unsigned int, output_size, 2 << 15);
3868 req->OutputBufferLength = cpu_to_le32(output_size);
3870 iov[0].iov_base = (char *)req;
3872 iov[0].iov_len = total_len - 1;
3874 iov[1].iov_base = (char *)(req->Buffer);
3875 iov[1].iov_len = len;
3877 memset(&rqst, 0, sizeof(struct smb_rqst));
3881 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
3882 tcon->ses->Suid, index, output_size);
3884 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3885 cifs_small_buf_release(req);
3886 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
3889 if (rc == -ENODATA &&
3890 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
3891 trace_smb3_query_dir_done(xid, persistent_fid,
3892 tcon->tid, tcon->ses->Suid, index, 0);
3893 srch_inf->endOfSearch = true;
3896 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
3897 tcon->ses->Suid, index, 0, rc);
3898 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3903 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
3904 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
3907 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
3908 tcon->ses->Suid, index, 0, rc);
3912 srch_inf->unicode = true;
3914 if (srch_inf->ntwrk_buf_start) {
3915 if (srch_inf->smallBuf)
3916 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3918 cifs_buf_release(srch_inf->ntwrk_buf_start);
3920 srch_inf->ntwrk_buf_start = (char *)rsp;
3921 srch_inf->srch_entries_start = srch_inf->last_entry =
3922 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
3923 end_of_smb = rsp_iov.iov_len + (char *)rsp;
3924 srch_inf->entries_in_buffer =
3925 num_entries(srch_inf->srch_entries_start, end_of_smb,
3926 &srch_inf->last_entry, info_buf_size);
3927 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
3928 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3929 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3930 srch_inf->srch_entries_start, srch_inf->last_entry);
3931 if (resp_buftype == CIFS_LARGE_BUFFER)
3932 srch_inf->smallBuf = false;
3933 else if (resp_buftype == CIFS_SMALL_BUFFER)
3934 srch_inf->smallBuf = true;
3936 cifs_dbg(VFS, "illegal search buffer type\n");
3938 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
3939 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
3943 free_rsp_buf(resp_buftype, rsp);
3948 SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3949 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3950 u8 info_type, u32 additional_info,
3951 void **data, unsigned int *size)
3953 struct smb2_set_info_req *req;
3954 struct kvec *iov = rqst->rq_iov;
3955 unsigned int i, total_len;
3958 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
3962 req->sync_hdr.ProcessId = cpu_to_le32(pid);
3963 req->InfoType = info_type;
3964 req->FileInfoClass = info_class;
3965 req->PersistentFileId = persistent_fid;
3966 req->VolatileFileId = volatile_fid;
3967 req->AdditionalInformation = cpu_to_le32(additional_info);
3970 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
3971 req->BufferLength = cpu_to_le32(*size);
3973 memcpy(req->Buffer, *data, *size);
3976 iov[0].iov_base = (char *)req;
3978 iov[0].iov_len = total_len - 1;
3980 for (i = 1; i < rqst->rq_nvec; i++) {
3981 le32_add_cpu(&req->BufferLength, size[i]);
3982 iov[i].iov_base = (char *)data[i];
3983 iov[i].iov_len = size[i];
3990 SMB2_set_info_free(struct smb_rqst *rqst)
3992 if (rqst && rqst->rq_iov)
3993 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
3997 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3998 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3999 u8 info_type, u32 additional_info, unsigned int num,
4000 void **data, unsigned int *size)
4002 struct smb_rqst rqst;
4003 struct smb2_set_info_rsp *rsp = NULL;
4005 struct kvec rsp_iov;
4008 struct cifs_ses *ses = tcon->ses;
4011 if (!ses || !(ses->server))
4017 if (smb3_encryption_required(tcon))
4018 flags |= CIFS_TRANSFORM_REQ;
4020 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4024 memset(&rqst, 0, sizeof(struct smb_rqst));
4028 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4029 info_class, info_type, additional_info,
4037 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
4039 SMB2_set_info_free(&rqst);
4040 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
4043 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
4044 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4045 ses->Suid, info_class, (__u32)info_type, rc);
4048 free_rsp_buf(resp_buftype, rsp);
4054 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4055 u64 volatile_fid, u32 pid, __le64 *eof)
4057 struct smb2_file_eof_info info;
4061 info.EndOfFile = *eof;
4064 size = sizeof(struct smb2_file_eof_info);
4066 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4067 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4068 0, 1, &data, &size);
4072 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4073 u64 persistent_fid, u64 volatile_fid,
4074 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4076 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4077 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4078 1, (void **)&pnntsd, &pacllen);
4082 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4083 u64 persistent_fid, u64 volatile_fid,
4084 struct smb2_file_full_ea_info *buf, int len)
4086 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4087 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4088 0, 1, (void **)&buf, &len);
4092 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4093 const u64 persistent_fid, const u64 volatile_fid,
4096 struct smb_rqst rqst;
4098 struct smb2_oplock_break *req = NULL;
4099 struct cifs_ses *ses = tcon->ses;
4100 int flags = CIFS_OBREAK_OP;
4101 unsigned int total_len;
4103 struct kvec rsp_iov;
4106 cifs_dbg(FYI, "SMB2_oplock_break\n");
4107 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4112 if (smb3_encryption_required(tcon))
4113 flags |= CIFS_TRANSFORM_REQ;
4115 req->VolatileFid = volatile_fid;
4116 req->PersistentFid = persistent_fid;
4117 req->OplockLevel = oplock_level;
4118 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4120 flags |= CIFS_NO_RESP;
4122 iov[0].iov_base = (char *)req;
4123 iov[0].iov_len = total_len;
4125 memset(&rqst, 0, sizeof(struct smb_rqst));
4129 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4130 cifs_small_buf_release(req);
4133 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4134 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
4141 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4142 struct kstatfs *kst)
4144 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4145 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4146 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
4147 kst->f_bfree = kst->f_bavail =
4148 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
4153 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4154 struct kstatfs *kst)
4156 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4157 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4158 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4159 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4160 kst->f_bavail = kst->f_bfree;
4162 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4163 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4164 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4165 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4166 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4172 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4173 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4176 struct smb2_query_info_req *req;
4177 unsigned int total_len;
4179 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
4181 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4184 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4189 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4190 req->FileInfoClass = level;
4191 req->PersistentFileId = persistent_fid;
4192 req->VolatileFileId = volatile_fid;
4194 req->InputBufferOffset =
4195 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
4196 req->OutputBufferLength = cpu_to_le32(
4197 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
4199 iov->iov_base = (char *)req;
4200 iov->iov_len = total_len;
4205 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4206 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4208 struct smb_rqst rqst;
4209 struct smb2_query_info_rsp *rsp = NULL;
4211 struct kvec rsp_iov;
4214 struct cifs_ses *ses = tcon->ses;
4215 FILE_SYSTEM_POSIX_INFO *info = NULL;
4218 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4219 sizeof(FILE_SYSTEM_POSIX_INFO),
4220 persistent_fid, volatile_fid);
4224 if (smb3_encryption_required(tcon))
4225 flags |= CIFS_TRANSFORM_REQ;
4227 memset(&rqst, 0, sizeof(struct smb_rqst));
4231 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4232 cifs_small_buf_release(iov.iov_base);
4234 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4235 goto posix_qfsinf_exit;
4237 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4239 info = (FILE_SYSTEM_POSIX_INFO *)(
4240 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4241 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4242 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4243 sizeof(FILE_SYSTEM_POSIX_INFO));
4245 copy_posix_fs_info_to_kstatfs(info, fsdata);
4248 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4253 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4254 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4256 struct smb_rqst rqst;
4257 struct smb2_query_info_rsp *rsp = NULL;
4259 struct kvec rsp_iov;
4262 struct cifs_ses *ses = tcon->ses;
4263 struct smb2_fs_full_size_info *info = NULL;
4266 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4267 sizeof(struct smb2_fs_full_size_info),
4268 persistent_fid, volatile_fid);
4272 if (smb3_encryption_required(tcon))
4273 flags |= CIFS_TRANSFORM_REQ;
4275 memset(&rqst, 0, sizeof(struct smb_rqst));
4279 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4280 cifs_small_buf_release(iov.iov_base);
4282 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4285 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4287 info = (struct smb2_fs_full_size_info *)(
4288 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4289 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4290 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4291 sizeof(struct smb2_fs_full_size_info));
4293 smb2_copy_fs_info_to_kstatfs(info, fsdata);
4296 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4301 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
4302 u64 persistent_fid, u64 volatile_fid, int level)
4304 struct smb_rqst rqst;
4305 struct smb2_query_info_rsp *rsp = NULL;
4307 struct kvec rsp_iov;
4309 int resp_buftype, max_len, min_len;
4310 struct cifs_ses *ses = tcon->ses;
4311 unsigned int rsp_len, offset;
4314 if (level == FS_DEVICE_INFORMATION) {
4315 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4316 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4317 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4318 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4319 min_len = MIN_FS_ATTR_INFO_SIZE;
4320 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4321 max_len = sizeof(struct smb3_fs_ss_info);
4322 min_len = sizeof(struct smb3_fs_ss_info);
4323 } else if (level == FS_VOLUME_INFORMATION) {
4324 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4325 min_len = sizeof(struct smb3_fs_vol_info);
4327 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
4331 rc = build_qfs_info_req(&iov, tcon, level, max_len,
4332 persistent_fid, volatile_fid);
4336 if (smb3_encryption_required(tcon))
4337 flags |= CIFS_TRANSFORM_REQ;
4339 memset(&rqst, 0, sizeof(struct smb_rqst));
4343 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4344 cifs_small_buf_release(iov.iov_base);
4346 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4349 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4351 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4352 offset = le16_to_cpu(rsp->OutputBufferOffset);
4353 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
4357 if (level == FS_ATTRIBUTE_INFORMATION)
4358 memcpy(&tcon->fsAttrInfo, offset
4359 + (char *)rsp, min_t(unsigned int,
4361 else if (level == FS_DEVICE_INFORMATION)
4362 memcpy(&tcon->fsDevInfo, offset
4363 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
4364 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4365 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
4366 (offset + (char *)rsp);
4367 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4368 tcon->perf_sector_size =
4369 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
4370 } else if (level == FS_VOLUME_INFORMATION) {
4371 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4372 (offset + (char *)rsp);
4373 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4374 tcon->vol_create_time = vol_info->VolumeCreationTime;
4378 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4383 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4384 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4385 const __u32 num_lock, struct smb2_lock_element *buf)
4387 struct smb_rqst rqst;
4389 struct smb2_lock_req *req = NULL;
4391 struct kvec rsp_iov;
4394 int flags = CIFS_NO_RESP;
4395 unsigned int total_len;
4397 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
4399 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
4403 if (smb3_encryption_required(tcon))
4404 flags |= CIFS_TRANSFORM_REQ;
4406 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4407 req->LockCount = cpu_to_le16(num_lock);
4409 req->PersistentFileId = persist_fid;
4410 req->VolatileFileId = volatile_fid;
4412 count = num_lock * sizeof(struct smb2_lock_element);
4414 iov[0].iov_base = (char *)req;
4415 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
4416 iov[1].iov_base = (char *)buf;
4417 iov[1].iov_len = count;
4419 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
4421 memset(&rqst, 0, sizeof(struct smb_rqst));
4425 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
4427 cifs_small_buf_release(req);
4429 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
4430 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
4431 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4432 tcon->ses->Suid, rc);
4439 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4440 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4441 const __u64 length, const __u64 offset, const __u32 lock_flags,
4444 struct smb2_lock_element lock;
4446 lock.Offset = cpu_to_le64(offset);
4447 lock.Length = cpu_to_le64(length);
4448 lock.Flags = cpu_to_le32(lock_flags);
4449 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
4450 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
4452 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
4456 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
4457 __u8 *lease_key, const __le32 lease_state)
4459 struct smb_rqst rqst;
4461 struct smb2_lease_ack *req = NULL;
4462 struct cifs_ses *ses = tcon->ses;
4463 int flags = CIFS_OBREAK_OP;
4464 unsigned int total_len;
4466 struct kvec rsp_iov;
4468 __u64 *please_key_high;
4469 __u64 *please_key_low;
4471 cifs_dbg(FYI, "SMB2_lease_break\n");
4472 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4477 if (smb3_encryption_required(tcon))
4478 flags |= CIFS_TRANSFORM_REQ;
4480 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4481 req->StructureSize = cpu_to_le16(36);
4484 memcpy(req->LeaseKey, lease_key, 16);
4485 req->LeaseState = lease_state;
4487 flags |= CIFS_NO_RESP;
4489 iov[0].iov_base = (char *)req;
4490 iov[0].iov_len = total_len;
4492 memset(&rqst, 0, sizeof(struct smb_rqst));
4496 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4497 cifs_small_buf_release(req);
4499 please_key_low = (__u64 *)lease_key;
4500 please_key_high = (__u64 *)(lease_key+8);
4502 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4503 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
4504 ses->Suid, *please_key_low, *please_key_high, rc);
4505 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
4507 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
4508 ses->Suid, *please_key_low, *please_key_high);