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);
462 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
465 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
467 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
468 pneg_ctxt->DataLength = cpu_to_le16(38);
469 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
470 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
471 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
472 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
476 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
478 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
479 pneg_ctxt->DataLength =
480 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
481 - sizeof(struct smb2_neg_context));
482 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
483 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
484 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
485 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
489 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
491 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
492 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + le16 cipher */
493 pneg_ctxt->CipherCount = cpu_to_le16(1);
494 /* pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;*/ /* not supported yet */
495 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_CCM;
499 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
501 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
502 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
503 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
504 pneg_ctxt->Name[0] = 0x93;
505 pneg_ctxt->Name[1] = 0xAD;
506 pneg_ctxt->Name[2] = 0x25;
507 pneg_ctxt->Name[3] = 0x50;
508 pneg_ctxt->Name[4] = 0x9C;
509 pneg_ctxt->Name[5] = 0xB4;
510 pneg_ctxt->Name[6] = 0x11;
511 pneg_ctxt->Name[7] = 0xE7;
512 pneg_ctxt->Name[8] = 0xB4;
513 pneg_ctxt->Name[9] = 0x23;
514 pneg_ctxt->Name[10] = 0x83;
515 pneg_ctxt->Name[11] = 0xDE;
516 pneg_ctxt->Name[12] = 0x96;
517 pneg_ctxt->Name[13] = 0x8B;
518 pneg_ctxt->Name[14] = 0xCD;
519 pneg_ctxt->Name[15] = 0x7C;
523 assemble_neg_contexts(struct smb2_negotiate_req *req,
524 unsigned int *total_len)
526 char *pneg_ctxt = (char *)req;
527 unsigned int ctxt_len;
529 if (*total_len > 200) {
530 /* In case length corrupted don't want to overrun smb buffer */
531 cifs_dbg(VFS, "Bad frame length assembling neg contexts\n");
536 * round up total_len of fixed part of SMB3 negotiate request to 8
537 * byte boundary before adding negotiate contexts
539 *total_len = roundup(*total_len, 8);
541 pneg_ctxt = (*total_len) + (char *)req;
542 req->NegotiateContextOffset = cpu_to_le32(*total_len);
544 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
545 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
546 *total_len += ctxt_len;
547 pneg_ctxt += ctxt_len;
549 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
550 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
551 *total_len += ctxt_len;
552 pneg_ctxt += ctxt_len;
554 build_compression_ctxt((struct smb2_compression_capabilities_context *)
556 ctxt_len = DIV_ROUND_UP(
557 sizeof(struct smb2_compression_capabilities_context), 8) * 8;
558 *total_len += ctxt_len;
559 pneg_ctxt += ctxt_len;
561 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
562 *total_len += sizeof(struct smb2_posix_neg_context);
564 req->NegotiateContextCount = cpu_to_le16(4);
567 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
569 unsigned int len = le16_to_cpu(ctxt->DataLength);
571 /* If invalid preauth context warn but use what we requested, SHA-512 */
572 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
573 printk_once(KERN_WARNING "server sent bad preauth context\n");
576 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
577 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
578 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
579 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
582 static void decode_compress_ctx(struct TCP_Server_Info *server,
583 struct smb2_compression_capabilities_context *ctxt)
585 unsigned int len = le16_to_cpu(ctxt->DataLength);
587 /* sizeof compress context is a one element compression capbility struct */
589 printk_once(KERN_WARNING "server sent bad compression cntxt\n");
592 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
593 printk_once(KERN_WARNING "illegal SMB3 compress algorithm count\n");
596 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
597 printk_once(KERN_WARNING "unknown compression algorithm\n");
600 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
603 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
604 struct smb2_encryption_neg_context *ctxt)
606 unsigned int len = le16_to_cpu(ctxt->DataLength);
608 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
609 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
610 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
614 if (le16_to_cpu(ctxt->CipherCount) != 1) {
615 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
618 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
619 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
620 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
621 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
624 server->cipher_type = ctxt->Ciphers[0];
625 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
629 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
630 struct TCP_Server_Info *server,
631 unsigned int len_of_smb)
633 struct smb2_neg_context *pctx;
634 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
635 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
636 unsigned int len_of_ctxts, i;
639 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
640 if (len_of_smb <= offset) {
641 cifs_dbg(VFS, "Invalid response: negotiate context offset\n");
645 len_of_ctxts = len_of_smb - offset;
647 for (i = 0; i < ctxt_cnt; i++) {
649 /* check that offset is not beyond end of SMB */
650 if (len_of_ctxts == 0)
653 if (len_of_ctxts < sizeof(struct smb2_neg_context))
656 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
657 clen = le16_to_cpu(pctx->DataLength);
658 if (clen > len_of_ctxts)
661 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
662 decode_preauth_context(
663 (struct smb2_preauth_neg_context *)pctx);
664 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
665 rc = decode_encrypt_ctx(server,
666 (struct smb2_encryption_neg_context *)pctx);
667 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
668 decode_compress_ctx(server,
669 (struct smb2_compression_capabilities_context *)pctx);
670 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
671 server->posix_ext_supported = true;
673 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
674 le16_to_cpu(pctx->ContextType));
678 /* offsets must be 8 byte aligned */
679 clen = (clen + 7) & ~0x7;
680 offset += clen + sizeof(struct smb2_neg_context);
681 len_of_ctxts -= clen;
686 static struct create_posix *
687 create_posix_buf(umode_t mode)
689 struct create_posix *buf;
691 buf = kzalloc(sizeof(struct create_posix),
696 buf->ccontext.DataOffset =
697 cpu_to_le16(offsetof(struct create_posix, Mode));
698 buf->ccontext.DataLength = cpu_to_le32(4);
699 buf->ccontext.NameOffset =
700 cpu_to_le16(offsetof(struct create_posix, Name));
701 buf->ccontext.NameLength = cpu_to_le16(16);
703 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
714 buf->Name[10] = 0x83;
715 buf->Name[11] = 0xDE;
716 buf->Name[12] = 0x96;
717 buf->Name[13] = 0x8B;
718 buf->Name[14] = 0xCD;
719 buf->Name[15] = 0x7C;
720 buf->Mode = cpu_to_le32(mode);
721 cifs_dbg(FYI, "mode on posix create 0%o", mode);
726 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
728 struct smb2_create_req *req = iov[0].iov_base;
729 unsigned int num = *num_iovec;
731 iov[num].iov_base = create_posix_buf(mode);
732 if (iov[num].iov_base == NULL)
734 iov[num].iov_len = sizeof(struct create_posix);
735 if (!req->CreateContextsOffset)
736 req->CreateContextsOffset = cpu_to_le32(
737 sizeof(struct smb2_create_req) +
738 iov[num - 1].iov_len);
739 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
740 *num_iovec = num + 1;
747 * SMB2 Worker functions follow:
749 * The general structure of the worker functions is:
750 * 1) Call smb2_init (assembles SMB2 header)
751 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
752 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
753 * 4) Decode SMB2 command specific fields in the fixed length area
754 * 5) Decode variable length data area (if any for this SMB2 command type)
755 * 6) Call free smb buffer
761 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
763 struct smb_rqst rqst;
764 struct smb2_negotiate_req *req;
765 struct smb2_negotiate_rsp *rsp;
770 struct TCP_Server_Info *server = ses->server;
771 int blob_offset, blob_length;
773 int flags = CIFS_NEG_OP;
774 unsigned int total_len;
776 cifs_dbg(FYI, "Negotiate protocol\n");
779 WARN(1, "%s: server is NULL!\n", __func__);
783 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
787 req->sync_hdr.SessionId = 0;
789 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
790 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
792 if (strcmp(ses->server->vals->version_string,
793 SMB3ANY_VERSION_STRING) == 0) {
794 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
795 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
796 req->DialectCount = cpu_to_le16(2);
798 } else if (strcmp(ses->server->vals->version_string,
799 SMBDEFAULT_VERSION_STRING) == 0) {
800 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
801 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
802 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
803 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
804 req->DialectCount = cpu_to_le16(4);
807 /* otherwise send specific dialect */
808 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
809 req->DialectCount = cpu_to_le16(1);
813 /* only one of SMB2 signing flags may be set in SMB2 request */
815 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
816 else if (global_secflags & CIFSSEC_MAY_SIGN)
817 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
819 req->SecurityMode = 0;
821 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
823 /* ClientGUID must be zero for SMB2.02 dialect */
824 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
825 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
827 memcpy(req->ClientGUID, server->client_guid,
828 SMB2_CLIENT_GUID_SIZE);
829 if ((ses->server->vals->protocol_id == SMB311_PROT_ID) ||
830 (strcmp(ses->server->vals->version_string,
831 SMBDEFAULT_VERSION_STRING) == 0))
832 assemble_neg_contexts(req, &total_len);
834 iov[0].iov_base = (char *)req;
835 iov[0].iov_len = total_len;
837 memset(&rqst, 0, sizeof(struct smb_rqst));
841 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
842 cifs_small_buf_release(req);
843 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
845 * No tcon so can't do
846 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
848 if (rc == -EOPNOTSUPP) {
849 cifs_dbg(VFS, "Dialect not supported by server. Consider "
850 "specifying vers=1.0 or vers=2.0 on mount for accessing"
856 if (strcmp(ses->server->vals->version_string,
857 SMB3ANY_VERSION_STRING) == 0) {
858 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
860 "SMB2 dialect returned but not requested\n");
862 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
864 "SMB2.1 dialect returned but not requested\n");
867 } else if (strcmp(ses->server->vals->version_string,
868 SMBDEFAULT_VERSION_STRING) == 0) {
869 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
871 "SMB2 dialect returned but not requested\n");
873 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
874 /* ops set to 3.0 by default for default so update */
875 ses->server->ops = &smb21_operations;
876 ses->server->vals = &smb21_values;
877 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
878 ses->server->ops = &smb311_operations;
879 ses->server->vals = &smb311_values;
881 } else if (le16_to_cpu(rsp->DialectRevision) !=
882 ses->server->vals->protocol_id) {
883 /* if requested single dialect ensure returned dialect matched */
884 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
885 le16_to_cpu(rsp->DialectRevision));
889 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
891 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
892 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
893 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
894 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
895 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
896 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
897 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
898 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
899 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
900 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
902 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
903 le16_to_cpu(rsp->DialectRevision));
907 server->dialect = le16_to_cpu(rsp->DialectRevision);
910 * Keep a copy of the hash after negprot. This hash will be
911 * the starting hash value for all sessions made from this
914 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
915 SMB2_PREAUTH_HASH_SIZE);
917 /* SMB2 only has an extended negflavor */
918 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
919 /* set it to the maximum buffer size value we can send with 1 credit */
920 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
921 SMB2_MAX_BUFFER_SIZE);
922 server->max_read = le32_to_cpu(rsp->MaxReadSize);
923 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
924 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
925 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
926 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
928 server->capabilities = le32_to_cpu(rsp->Capabilities);
930 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
932 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
933 (struct smb2_sync_hdr *)rsp);
935 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
937 * ses->sectype = RawNTLMSSP;
938 * but for time being this is our only auth choice so doesn't matter.
939 * We just found a server which sets blob length to zero expecting raw.
941 if (blob_length == 0) {
942 cifs_dbg(FYI, "missing security blob on negprot\n");
943 server->sec_ntlmssp = true;
946 rc = cifs_enable_signing(server, ses->sign);
950 rc = decode_negTokenInit(security_blob, blob_length, server);
957 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
958 if (rsp->NegotiateContextCount)
959 rc = smb311_decode_neg_context(rsp, server,
962 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
965 free_rsp_buf(resp_buftype, rsp);
969 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
972 struct validate_negotiate_info_req *pneg_inbuf;
973 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
975 u32 inbuflen; /* max of 4 dialects */
977 cifs_dbg(FYI, "validate negotiate\n");
979 /* In SMB3.11 preauth integrity supersedes validate negotiate */
980 if (tcon->ses->server->dialect == SMB311_PROT_ID)
984 * validation ioctl must be signed, so no point sending this if we
985 * can not sign it (ie are not known user). Even if signing is not
986 * required (enabled but not negotiated), in those cases we selectively
987 * sign just this, the first and only signed request on a connection.
988 * Having validation of negotiate info helps reduce attack vectors.
990 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
991 return 0; /* validation requires signing */
993 if (tcon->ses->user_name == NULL) {
994 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
995 return 0; /* validation requires signing */
998 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
999 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1001 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1005 pneg_inbuf->Capabilities =
1006 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
1007 memcpy(pneg_inbuf->Guid, tcon->ses->server->client_guid,
1008 SMB2_CLIENT_GUID_SIZE);
1010 if (tcon->ses->sign)
1011 pneg_inbuf->SecurityMode =
1012 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1013 else if (global_secflags & CIFSSEC_MAY_SIGN)
1014 pneg_inbuf->SecurityMode =
1015 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1017 pneg_inbuf->SecurityMode = 0;
1020 if (strcmp(tcon->ses->server->vals->version_string,
1021 SMB3ANY_VERSION_STRING) == 0) {
1022 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1023 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1024 pneg_inbuf->DialectCount = cpu_to_le16(2);
1025 /* structure is big enough for 3 dialects, sending only 2 */
1026 inbuflen = sizeof(*pneg_inbuf) -
1027 (2 * sizeof(pneg_inbuf->Dialects[0]));
1028 } else if (strcmp(tcon->ses->server->vals->version_string,
1029 SMBDEFAULT_VERSION_STRING) == 0) {
1030 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1031 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1032 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1033 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1034 pneg_inbuf->DialectCount = cpu_to_le16(4);
1035 /* structure is big enough for 3 dialects */
1036 inbuflen = sizeof(*pneg_inbuf);
1038 /* otherwise specific dialect was requested */
1039 pneg_inbuf->Dialects[0] =
1040 cpu_to_le16(tcon->ses->server->vals->protocol_id);
1041 pneg_inbuf->DialectCount = cpu_to_le16(1);
1042 /* structure is big enough for 3 dialects, sending only 1 */
1043 inbuflen = sizeof(*pneg_inbuf) -
1044 sizeof(pneg_inbuf->Dialects[0]) * 2;
1047 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1048 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
1049 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1050 (char **)&pneg_rsp, &rsplen);
1051 if (rc == -EOPNOTSUPP) {
1053 * Old Windows versions or Netapp SMB server can return
1054 * not supported error. Client should accept it.
1056 cifs_dbg(VFS, "Server does not support validate negotiate\n");
1058 } else if (rc != 0) {
1059 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
1061 goto out_free_inbuf;
1065 if (rsplen != sizeof(*pneg_rsp)) {
1066 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
1069 /* relax check since Mac returns max bufsize allowed on ioctl */
1070 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1074 /* check validate negotiate info response matches what we got earlier */
1075 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
1078 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
1081 /* do not validate server guid because not saved at negprot time yet */
1083 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1084 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
1087 /* validate negotiate successful */
1089 cifs_dbg(FYI, "validate negotiate info successful\n");
1093 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1102 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1104 switch (requested) {
1111 if (server->sec_ntlmssp &&
1112 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1114 if ((server->sec_kerberos || server->sec_mskerberos) &&
1115 (global_secflags & CIFSSEC_MAY_KRB5))
1123 struct SMB2_sess_data {
1125 struct cifs_ses *ses;
1126 struct nls_table *nls_cp;
1127 void (*func)(struct SMB2_sess_data *);
1129 u64 previous_session;
1131 /* we will send the SMB in three pieces:
1132 * a fixed length beginning part, an optional
1133 * SPNEGO blob (which can be zero length), and a
1134 * last part which will include the strings
1135 * and rest of bcc area. This allows us to avoid
1136 * a large buffer 17K allocation
1143 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1146 struct cifs_ses *ses = sess_data->ses;
1147 struct smb2_sess_setup_req *req;
1148 struct TCP_Server_Info *server = ses->server;
1149 unsigned int total_len;
1151 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1156 /* First session, not a reauthenticate */
1157 req->sync_hdr.SessionId = 0;
1159 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
1160 req->PreviousSessionId = sess_data->previous_session;
1162 req->Flags = 0; /* MBZ */
1164 /* enough to enable echos and oplocks and one max size write */
1165 req->sync_hdr.CreditRequest = cpu_to_le16(130);
1167 /* only one of SMB2 signing flags may be set in SMB2 request */
1169 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1170 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1171 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1173 req->SecurityMode = 0;
1175 req->Capabilities = 0;
1176 req->Channel = 0; /* MBZ */
1178 sess_data->iov[0].iov_base = (char *)req;
1180 sess_data->iov[0].iov_len = total_len - 1;
1182 * This variable will be used to clear the buffer
1183 * allocated above in case of any error in the calling function.
1185 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1191 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1193 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1194 sess_data->buf0_type = CIFS_NO_BUFFER;
1198 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1201 struct smb_rqst rqst;
1202 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1203 struct kvec rsp_iov = { NULL, 0 };
1205 /* Testing shows that buffer offset must be at location of Buffer[0] */
1206 req->SecurityBufferOffset =
1207 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1208 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1210 memset(&rqst, 0, sizeof(struct smb_rqst));
1211 rqst.rq_iov = sess_data->iov;
1214 /* BB add code to build os and lm fields */
1215 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1217 &sess_data->buf0_type,
1218 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
1219 cifs_small_buf_release(sess_data->iov[0].iov_base);
1220 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1226 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1229 struct cifs_ses *ses = sess_data->ses;
1231 mutex_lock(&ses->server->srv_mutex);
1232 if (ses->server->ops->generate_signingkey) {
1233 rc = ses->server->ops->generate_signingkey(ses);
1236 "SMB3 session key generation failed\n");
1237 mutex_unlock(&ses->server->srv_mutex);
1241 if (!ses->server->session_estab) {
1242 ses->server->sequence_number = 0x2;
1243 ses->server->session_estab = true;
1245 mutex_unlock(&ses->server->srv_mutex);
1247 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1248 spin_lock(&GlobalMid_Lock);
1249 ses->status = CifsGood;
1250 ses->need_reconnect = false;
1251 spin_unlock(&GlobalMid_Lock);
1255 #ifdef CONFIG_CIFS_UPCALL
1257 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1260 struct cifs_ses *ses = sess_data->ses;
1261 struct cifs_spnego_msg *msg;
1262 struct key *spnego_key = NULL;
1263 struct smb2_sess_setup_rsp *rsp = NULL;
1265 rc = SMB2_sess_alloc_buffer(sess_data);
1269 spnego_key = cifs_get_spnego_key(ses);
1270 if (IS_ERR(spnego_key)) {
1271 rc = PTR_ERR(spnego_key);
1276 msg = spnego_key->payload.data[0];
1278 * check version field to make sure that cifs.upcall is
1279 * sending us a response in an expected form
1281 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1283 "bad cifs.upcall version. Expected %d got %d",
1284 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1286 goto out_put_spnego_key;
1289 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1291 if (!ses->auth_key.response) {
1293 "Kerberos can't allocate (%u bytes) memory",
1296 goto out_put_spnego_key;
1298 ses->auth_key.len = msg->sesskey_len;
1300 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1301 sess_data->iov[1].iov_len = msg->secblob_len;
1303 rc = SMB2_sess_sendreceive(sess_data);
1305 goto out_put_spnego_key;
1307 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1308 ses->Suid = rsp->sync_hdr.SessionId;
1310 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1312 rc = SMB2_sess_establish_session(sess_data);
1314 key_invalidate(spnego_key);
1315 key_put(spnego_key);
1317 sess_data->result = rc;
1318 sess_data->func = NULL;
1319 SMB2_sess_free_buffer(sess_data);
1323 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1325 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1326 sess_data->result = -EOPNOTSUPP;
1327 sess_data->func = NULL;
1332 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1335 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1338 struct cifs_ses *ses = sess_data->ses;
1339 struct smb2_sess_setup_rsp *rsp = NULL;
1340 char *ntlmssp_blob = NULL;
1341 bool use_spnego = false; /* else use raw ntlmssp */
1342 u16 blob_length = 0;
1345 * If memory allocation is successful, caller of this function
1348 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1349 if (!ses->ntlmssp) {
1353 ses->ntlmssp->sesskey_per_smbsess = true;
1355 rc = SMB2_sess_alloc_buffer(sess_data);
1359 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1361 if (ntlmssp_blob == NULL) {
1366 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1368 /* BB eventually need to add this */
1369 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1373 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1374 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1376 sess_data->iov[1].iov_base = ntlmssp_blob;
1377 sess_data->iov[1].iov_len = blob_length;
1379 rc = SMB2_sess_sendreceive(sess_data);
1380 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1382 /* If true, rc here is expected and not an error */
1383 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1384 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1390 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1391 le16_to_cpu(rsp->SecurityBufferOffset)) {
1392 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1393 le16_to_cpu(rsp->SecurityBufferOffset));
1397 rc = decode_ntlmssp_challenge(rsp->Buffer,
1398 le16_to_cpu(rsp->SecurityBufferLength), ses);
1402 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1405 ses->Suid = rsp->sync_hdr.SessionId;
1406 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1409 kfree(ntlmssp_blob);
1410 SMB2_sess_free_buffer(sess_data);
1412 sess_data->result = 0;
1413 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1417 kfree(ses->ntlmssp);
1418 ses->ntlmssp = NULL;
1419 sess_data->result = rc;
1420 sess_data->func = NULL;
1424 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1427 struct cifs_ses *ses = sess_data->ses;
1428 struct smb2_sess_setup_req *req;
1429 struct smb2_sess_setup_rsp *rsp = NULL;
1430 unsigned char *ntlmssp_blob = NULL;
1431 bool use_spnego = false; /* else use raw ntlmssp */
1432 u16 blob_length = 0;
1434 rc = SMB2_sess_alloc_buffer(sess_data);
1438 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1439 req->sync_hdr.SessionId = ses->Suid;
1441 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1444 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1449 /* BB eventually need to add this */
1450 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1454 sess_data->iov[1].iov_base = ntlmssp_blob;
1455 sess_data->iov[1].iov_len = blob_length;
1457 rc = SMB2_sess_sendreceive(sess_data);
1461 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1463 ses->Suid = rsp->sync_hdr.SessionId;
1464 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1466 rc = SMB2_sess_establish_session(sess_data);
1468 kfree(ntlmssp_blob);
1469 SMB2_sess_free_buffer(sess_data);
1470 kfree(ses->ntlmssp);
1471 ses->ntlmssp = NULL;
1472 sess_data->result = rc;
1473 sess_data->func = NULL;
1477 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1481 type = smb2_select_sectype(ses->server, ses->sectype);
1482 cifs_dbg(FYI, "sess setup type %d\n", type);
1483 if (type == Unspecified) {
1485 "Unable to select appropriate authentication method!");
1491 sess_data->func = SMB2_auth_kerberos;
1494 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1497 cifs_dbg(VFS, "secType %d not supported!\n", type);
1505 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1506 const struct nls_table *nls_cp)
1509 struct TCP_Server_Info *server = ses->server;
1510 struct SMB2_sess_data *sess_data;
1512 cifs_dbg(FYI, "Session Setup\n");
1515 WARN(1, "%s: server is NULL!\n", __func__);
1519 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1523 rc = SMB2_select_sec(ses, sess_data);
1526 sess_data->xid = xid;
1527 sess_data->ses = ses;
1528 sess_data->buf0_type = CIFS_NO_BUFFER;
1529 sess_data->nls_cp = (struct nls_table *) nls_cp;
1530 sess_data->previous_session = ses->Suid;
1533 * Initialize the session hash with the server one.
1535 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1536 SMB2_PREAUTH_HASH_SIZE);
1538 while (sess_data->func)
1539 sess_data->func(sess_data);
1541 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1542 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
1543 rc = sess_data->result;
1550 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1552 struct smb_rqst rqst;
1553 struct smb2_logoff_req *req; /* response is also trivial struct */
1555 struct TCP_Server_Info *server;
1557 unsigned int total_len;
1559 struct kvec rsp_iov;
1562 cifs_dbg(FYI, "disconnect session %p\n", ses);
1564 if (ses && (ses->server))
1565 server = ses->server;
1569 /* no need to send SMB logoff if uid already closed due to reconnect */
1570 if (ses->need_reconnect)
1571 goto smb2_session_already_dead;
1573 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
1577 /* since no tcon, smb2_init can not do this, so do here */
1578 req->sync_hdr.SessionId = ses->Suid;
1580 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1581 flags |= CIFS_TRANSFORM_REQ;
1582 else if (server->sign)
1583 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1585 flags |= CIFS_NO_RSP_BUF;
1587 iov[0].iov_base = (char *)req;
1588 iov[0].iov_len = total_len;
1590 memset(&rqst, 0, sizeof(struct smb_rqst));
1594 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1595 cifs_small_buf_release(req);
1597 * No tcon so can't do
1598 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1601 smb2_session_already_dead:
1605 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1607 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1610 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1612 /* These are similar values to what Windows uses */
1613 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1615 tcon->max_chunks = 256;
1616 tcon->max_bytes_chunk = 1048576;
1617 tcon->max_bytes_copy = 16777216;
1621 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1622 struct cifs_tcon *tcon, const struct nls_table *cp)
1624 struct smb_rqst rqst;
1625 struct smb2_tree_connect_req *req;
1626 struct smb2_tree_connect_rsp *rsp = NULL;
1628 struct kvec rsp_iov = { NULL, 0 };
1632 __le16 *unc_path = NULL;
1634 unsigned int total_len;
1636 cifs_dbg(FYI, "TCON\n");
1638 if (!(ses->server) || !tree)
1641 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1642 if (unc_path == NULL)
1645 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1647 if (unc_path_len < 2) {
1652 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1654 atomic_set(&tcon->num_remote_opens, 0);
1655 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1662 if (smb3_encryption_required(tcon))
1663 flags |= CIFS_TRANSFORM_REQ;
1665 iov[0].iov_base = (char *)req;
1667 iov[0].iov_len = total_len - 1;
1669 /* Testing shows that buffer offset must be at location of Buffer[0] */
1670 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1672 req->PathLength = cpu_to_le16(unc_path_len - 2);
1673 iov[1].iov_base = unc_path;
1674 iov[1].iov_len = unc_path_len;
1677 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1678 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1679 * (Samba servers don't always set the flag so also check if null user)
1681 if ((ses->server->dialect == SMB311_PROT_ID) &&
1682 !smb3_encryption_required(tcon) &&
1683 !(ses->session_flags &
1684 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1685 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1686 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1688 memset(&rqst, 0, sizeof(struct smb_rqst));
1692 /* Need 64 for max size write so ask for more in case not there yet */
1693 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1695 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
1696 cifs_small_buf_release(req);
1697 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1698 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1701 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1702 tcon->need_reconnect = true;
1704 goto tcon_error_exit;
1707 switch (rsp->ShareType) {
1708 case SMB2_SHARE_TYPE_DISK:
1709 cifs_dbg(FYI, "connection to disk share\n");
1711 case SMB2_SHARE_TYPE_PIPE:
1713 cifs_dbg(FYI, "connection to pipe share\n");
1715 case SMB2_SHARE_TYPE_PRINT:
1717 cifs_dbg(FYI, "connection to printer\n");
1720 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1722 goto tcon_error_exit;
1725 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1726 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1727 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1728 tcon->tidStatus = CifsGood;
1729 tcon->need_reconnect = false;
1730 tcon->tid = rsp->sync_hdr.TreeId;
1731 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1733 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1734 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1735 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1738 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1739 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1741 init_copy_chunk_defaults(tcon);
1742 if (tcon->ses->server->ops->validate_negotiate)
1743 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1746 free_rsp_buf(resp_buftype, rsp);
1751 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1752 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1758 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1760 struct smb_rqst rqst;
1761 struct smb2_tree_disconnect_req *req; /* response is trivial */
1763 struct cifs_ses *ses = tcon->ses;
1765 unsigned int total_len;
1767 struct kvec rsp_iov;
1770 cifs_dbg(FYI, "Tree Disconnect\n");
1772 if (!ses || !(ses->server))
1775 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1778 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1783 if (smb3_encryption_required(tcon))
1784 flags |= CIFS_TRANSFORM_REQ;
1786 flags |= CIFS_NO_RSP_BUF;
1788 iov[0].iov_base = (char *)req;
1789 iov[0].iov_len = total_len;
1791 memset(&rqst, 0, sizeof(struct smb_rqst));
1795 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1796 cifs_small_buf_release(req);
1798 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1804 static struct create_durable *
1805 create_durable_buf(void)
1807 struct create_durable *buf;
1809 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1813 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1814 (struct create_durable, Data));
1815 buf->ccontext.DataLength = cpu_to_le32(16);
1816 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1817 (struct create_durable, Name));
1818 buf->ccontext.NameLength = cpu_to_le16(4);
1819 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1827 static struct create_durable *
1828 create_reconnect_durable_buf(struct cifs_fid *fid)
1830 struct create_durable *buf;
1832 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1836 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1837 (struct create_durable, Data));
1838 buf->ccontext.DataLength = cpu_to_le32(16);
1839 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1840 (struct create_durable, Name));
1841 buf->ccontext.NameLength = cpu_to_le16(4);
1842 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1843 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1844 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1853 smb2_parse_lease_state(struct TCP_Server_Info *server,
1854 struct smb2_create_rsp *rsp,
1855 unsigned int *epoch, char *lease_key)
1858 struct create_context *cc;
1860 unsigned int remaining;
1863 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
1864 remaining = le32_to_cpu(rsp->CreateContextsLength);
1865 cc = (struct create_context *)data_offset;
1866 while (remaining >= sizeof(struct create_context)) {
1867 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1868 if (le16_to_cpu(cc->NameLength) == 4 &&
1869 strncmp(name, "RqLs", 4) == 0)
1870 return server->ops->parse_lease_buf(cc, epoch,
1873 next = le32_to_cpu(cc->Next);
1877 cc = (struct create_context *)((char *)cc + next);
1884 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1885 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
1887 struct smb2_create_req *req = iov[0].iov_base;
1888 unsigned int num = *num_iovec;
1890 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
1891 if (iov[num].iov_base == NULL)
1893 iov[num].iov_len = server->vals->create_lease_size;
1894 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1895 if (!req->CreateContextsOffset)
1896 req->CreateContextsOffset = cpu_to_le32(
1897 sizeof(struct smb2_create_req) +
1898 iov[num - 1].iov_len);
1899 le32_add_cpu(&req->CreateContextsLength,
1900 server->vals->create_lease_size);
1901 *num_iovec = num + 1;
1905 static struct create_durable_v2 *
1906 create_durable_v2_buf(struct cifs_open_parms *oparms)
1908 struct cifs_fid *pfid = oparms->fid;
1909 struct create_durable_v2 *buf;
1911 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1915 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1916 (struct create_durable_v2, dcontext));
1917 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1918 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1919 (struct create_durable_v2, Name));
1920 buf->ccontext.NameLength = cpu_to_le16(4);
1923 * NB: Handle timeout defaults to 0, which allows server to choose
1924 * (most servers default to 120 seconds) and most clients default to 0.
1925 * This can be overridden at mount ("handletimeout=") if the user wants
1926 * a different persistent (or resilient) handle timeout for all opens
1927 * opens on a particular SMB3 mount.
1929 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
1930 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1931 generate_random_uuid(buf->dcontext.CreateGuid);
1932 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1934 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1942 static struct create_durable_handle_reconnect_v2 *
1943 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1945 struct create_durable_handle_reconnect_v2 *buf;
1947 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1952 buf->ccontext.DataOffset =
1953 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1955 buf->ccontext.DataLength =
1956 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1957 buf->ccontext.NameOffset =
1958 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1960 buf->ccontext.NameLength = cpu_to_le16(4);
1962 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1963 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1964 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1965 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1967 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1976 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1977 struct cifs_open_parms *oparms)
1979 struct smb2_create_req *req = iov[0].iov_base;
1980 unsigned int num = *num_iovec;
1982 iov[num].iov_base = create_durable_v2_buf(oparms);
1983 if (iov[num].iov_base == NULL)
1985 iov[num].iov_len = sizeof(struct create_durable_v2);
1986 if (!req->CreateContextsOffset)
1987 req->CreateContextsOffset =
1988 cpu_to_le32(sizeof(struct smb2_create_req) +
1990 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1991 *num_iovec = num + 1;
1996 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1997 struct cifs_open_parms *oparms)
1999 struct smb2_create_req *req = iov[0].iov_base;
2000 unsigned int num = *num_iovec;
2002 /* indicate that we don't need to relock the file */
2003 oparms->reconnect = false;
2005 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2006 if (iov[num].iov_base == NULL)
2008 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2009 if (!req->CreateContextsOffset)
2010 req->CreateContextsOffset =
2011 cpu_to_le32(sizeof(struct smb2_create_req) +
2013 le32_add_cpu(&req->CreateContextsLength,
2014 sizeof(struct create_durable_handle_reconnect_v2));
2015 *num_iovec = num + 1;
2020 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2021 struct cifs_open_parms *oparms, bool use_persistent)
2023 struct smb2_create_req *req = iov[0].iov_base;
2024 unsigned int num = *num_iovec;
2026 if (use_persistent) {
2027 if (oparms->reconnect)
2028 return add_durable_reconnect_v2_context(iov, num_iovec,
2031 return add_durable_v2_context(iov, num_iovec, oparms);
2034 if (oparms->reconnect) {
2035 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2036 /* indicate that we don't need to relock the file */
2037 oparms->reconnect = false;
2039 iov[num].iov_base = create_durable_buf();
2040 if (iov[num].iov_base == NULL)
2042 iov[num].iov_len = sizeof(struct create_durable);
2043 if (!req->CreateContextsOffset)
2044 req->CreateContextsOffset =
2045 cpu_to_le32(sizeof(struct smb2_create_req) +
2047 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
2048 *num_iovec = num + 1;
2052 /* See MS-SMB2 2.2.13.2.7 */
2053 static struct crt_twarp_ctxt *
2054 create_twarp_buf(__u64 timewarp)
2056 struct crt_twarp_ctxt *buf;
2058 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2062 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2063 (struct crt_twarp_ctxt, Timestamp));
2064 buf->ccontext.DataLength = cpu_to_le32(8);
2065 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2066 (struct crt_twarp_ctxt, Name));
2067 buf->ccontext.NameLength = cpu_to_le16(4);
2068 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2073 buf->Timestamp = cpu_to_le64(timewarp);
2077 /* See MS-SMB2 2.2.13.2.7 */
2079 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2081 struct smb2_create_req *req = iov[0].iov_base;
2082 unsigned int num = *num_iovec;
2084 iov[num].iov_base = create_twarp_buf(timewarp);
2085 if (iov[num].iov_base == NULL)
2087 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2088 if (!req->CreateContextsOffset)
2089 req->CreateContextsOffset = cpu_to_le32(
2090 sizeof(struct smb2_create_req) +
2091 iov[num - 1].iov_len);
2092 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2093 *num_iovec = num + 1;
2098 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2099 const char *treename, const __le16 *path)
2101 int treename_len, path_len;
2102 struct nls_table *cp;
2103 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2108 treename_len = strlen(treename);
2109 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2115 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2118 * make room for one path separator between the treename and
2121 *out_len = treename_len + 1 + path_len;
2124 * final path needs to be null-terminated UTF16 with a
2128 *out_size = roundup((*out_len+1)*2, 8);
2129 *out_path = kzalloc(*out_size, GFP_KERNEL);
2133 cp = load_nls_default();
2134 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2135 UniStrcat(*out_path, sep);
2136 UniStrcat(*out_path, path);
2142 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2143 umode_t mode, struct cifs_tcon *tcon,
2144 const char *full_path,
2145 struct cifs_sb_info *cifs_sb)
2147 struct smb_rqst rqst;
2148 struct smb2_create_req *req;
2149 struct smb2_create_rsp *rsp = NULL;
2150 struct cifs_ses *ses = tcon->ses;
2151 struct kvec iov[3]; /* make sure at least one for each open context */
2152 struct kvec rsp_iov = {NULL, 0};
2155 __le16 *copy_path = NULL;
2158 unsigned int n_iov = 2;
2159 __u32 file_attributes = 0;
2160 char *pc_buf = NULL;
2162 unsigned int total_len;
2163 __le16 *utf16_path = NULL;
2165 cifs_dbg(FYI, "mkdir\n");
2167 /* resource #1: path allocation */
2168 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2172 if (!ses || !(ses->server)) {
2177 /* resource #2: request */
2178 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2183 if (smb3_encryption_required(tcon))
2184 flags |= CIFS_TRANSFORM_REQ;
2186 req->ImpersonationLevel = IL_IMPERSONATION;
2187 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2188 /* File attributes ignored on open (used in create though) */
2189 req->FileAttributes = cpu_to_le32(file_attributes);
2190 req->ShareAccess = FILE_SHARE_ALL_LE;
2191 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2192 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2194 iov[0].iov_base = (char *)req;
2195 /* -1 since last byte is buf[0] which is sent below (path) */
2196 iov[0].iov_len = total_len - 1;
2198 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2200 /* [MS-SMB2] 2.2.13 NameOffset:
2201 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2202 * the SMB2 header, the file name includes a prefix that will
2203 * be processed during DFS name normalization as specified in
2204 * section 3.3.5.9. Otherwise, the file name is relative to
2205 * the share that is identified by the TreeId in the SMB2
2208 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2211 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2212 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2214 tcon->treeName, utf16_path);
2218 req->NameLength = cpu_to_le16(name_len * 2);
2219 uni_path_len = copy_size;
2220 /* free before overwriting resource */
2222 utf16_path = copy_path;
2224 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2225 /* MUST set path len (NameLength) to 0 opening root of share */
2226 req->NameLength = cpu_to_le16(uni_path_len - 2);
2227 if (uni_path_len % 8 != 0) {
2228 copy_size = roundup(uni_path_len, 8);
2229 copy_path = kzalloc(copy_size, GFP_KERNEL);
2234 memcpy((char *)copy_path, (const char *)utf16_path,
2236 uni_path_len = copy_size;
2237 /* free before overwriting resource */
2239 utf16_path = copy_path;
2243 iov[1].iov_len = uni_path_len;
2244 iov[1].iov_base = utf16_path;
2245 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2247 if (tcon->posix_extensions) {
2248 /* resource #3: posix buf */
2249 rc = add_posix_context(iov, &n_iov, mode);
2252 pc_buf = iov[n_iov-1].iov_base;
2256 memset(&rqst, 0, sizeof(struct smb_rqst));
2258 rqst.rq_nvec = n_iov;
2260 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2261 FILE_WRITE_ATTRIBUTES);
2262 /* resource #4: response buffer */
2263 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2265 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2266 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2268 FILE_WRITE_ATTRIBUTES, rc);
2269 goto err_free_rsp_buf;
2272 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2273 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2274 ses->Suid, CREATE_NOT_FILE,
2275 FILE_WRITE_ATTRIBUTES);
2277 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2279 /* Eventually save off posix specific response info and timestaps */
2282 free_rsp_buf(resp_buftype, rsp);
2285 cifs_small_buf_release(req);
2292 SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2293 struct cifs_open_parms *oparms, __le16 *path)
2295 struct TCP_Server_Info *server = tcon->ses->server;
2296 struct smb2_create_req *req;
2297 unsigned int n_iov = 2;
2298 __u32 file_attributes = 0;
2301 unsigned int total_len;
2302 struct kvec *iov = rqst->rq_iov;
2306 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2310 iov[0].iov_base = (char *)req;
2311 /* -1 since last byte is buf[0] which is sent below (path) */
2312 iov[0].iov_len = total_len - 1;
2314 if (oparms->create_options & CREATE_OPTION_READONLY)
2315 file_attributes |= ATTR_READONLY;
2316 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2317 file_attributes |= ATTR_SYSTEM;
2319 req->ImpersonationLevel = IL_IMPERSONATION;
2320 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2321 /* File attributes ignored on open (used in create though) */
2322 req->FileAttributes = cpu_to_le32(file_attributes);
2323 req->ShareAccess = FILE_SHARE_ALL_LE;
2324 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2325 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2326 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2328 /* [MS-SMB2] 2.2.13 NameOffset:
2329 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2330 * the SMB2 header, the file name includes a prefix that will
2331 * be processed during DFS name normalization as specified in
2332 * section 3.3.5.9. Otherwise, the file name is relative to
2333 * the share that is identified by the TreeId in the SMB2
2336 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2339 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2340 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2342 tcon->treeName, path);
2345 req->NameLength = cpu_to_le16(name_len * 2);
2346 uni_path_len = copy_size;
2349 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2350 /* MUST set path len (NameLength) to 0 opening root of share */
2351 req->NameLength = cpu_to_le16(uni_path_len - 2);
2352 copy_size = uni_path_len;
2353 if (copy_size % 8 != 0)
2354 copy_size = roundup(copy_size, 8);
2355 copy_path = kzalloc(copy_size, GFP_KERNEL);
2358 memcpy((char *)copy_path, (const char *)path,
2360 uni_path_len = copy_size;
2364 iov[1].iov_len = uni_path_len;
2365 iov[1].iov_base = path;
2367 if (!server->oplocks)
2368 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2370 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2371 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2372 req->RequestedOplockLevel = *oplock;
2373 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2374 (oparms->create_options & CREATE_NOT_FILE))
2375 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2377 rc = add_lease_context(server, iov, &n_iov,
2378 oparms->fid->lease_key, oplock);
2383 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2384 /* need to set Next field of lease context if we request it */
2385 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2386 struct create_context *ccontext =
2387 (struct create_context *)iov[n_iov-1].iov_base;
2389 cpu_to_le32(server->vals->create_lease_size);
2392 rc = add_durable_context(iov, &n_iov, oparms,
2393 tcon->use_persistent);
2398 if (tcon->posix_extensions) {
2400 struct create_context *ccontext =
2401 (struct create_context *)iov[n_iov-1].iov_base;
2403 cpu_to_le32(iov[n_iov-1].iov_len);
2406 rc = add_posix_context(iov, &n_iov, oparms->mode);
2411 if (tcon->snapshot_time) {
2412 cifs_dbg(FYI, "adding snapshot context\n");
2414 struct create_context *ccontext =
2415 (struct create_context *)iov[n_iov-1].iov_base;
2417 cpu_to_le32(iov[n_iov-1].iov_len);
2420 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2426 rqst->rq_nvec = n_iov;
2430 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2431 * All other vectors are freed by kfree().
2434 SMB2_open_free(struct smb_rqst *rqst)
2438 if (rqst && rqst->rq_iov) {
2439 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2440 for (i = 1; i < rqst->rq_nvec; i++)
2441 if (rqst->rq_iov[i].iov_base != smb2_padding)
2442 kfree(rqst->rq_iov[i].iov_base);
2447 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2448 __u8 *oplock, struct smb2_file_all_info *buf,
2449 struct kvec *err_iov, int *buftype)
2451 struct smb_rqst rqst;
2452 struct smb2_create_rsp *rsp = NULL;
2453 struct TCP_Server_Info *server;
2454 struct cifs_tcon *tcon = oparms->tcon;
2455 struct cifs_ses *ses = tcon->ses;
2456 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2457 struct kvec rsp_iov = {NULL, 0};
2458 int resp_buftype = CIFS_NO_BUFFER;
2462 cifs_dbg(FYI, "create/open\n");
2463 if (ses && (ses->server))
2464 server = ses->server;
2468 if (smb3_encryption_required(tcon))
2469 flags |= CIFS_TRANSFORM_REQ;
2471 memset(&rqst, 0, sizeof(struct smb_rqst));
2472 memset(&iov, 0, sizeof(iov));
2474 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2476 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2480 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2481 oparms->create_options, oparms->desired_access);
2483 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2485 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2488 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2489 if (err_iov && rsp) {
2491 *buftype = resp_buftype;
2492 resp_buftype = CIFS_NO_BUFFER;
2495 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2496 oparms->create_options, oparms->desired_access, rc);
2499 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2500 ses->Suid, oparms->create_options,
2501 oparms->desired_access);
2503 atomic_inc(&tcon->num_remote_opens);
2504 oparms->fid->persistent_fid = rsp->PersistentFileId;
2505 oparms->fid->volatile_fid = rsp->VolatileFileId;
2506 #ifdef CONFIG_CIFS_DEBUG2
2507 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2508 #endif /* CIFS_DEBUG2 */
2511 memcpy(buf, &rsp->CreationTime, 32);
2512 buf->AllocationSize = rsp->AllocationSize;
2513 buf->EndOfFile = rsp->EndofFile;
2514 buf->Attributes = rsp->FileAttributes;
2515 buf->NumberOfLinks = cpu_to_le32(1);
2516 buf->DeletePending = 0;
2519 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
2520 *oplock = smb2_parse_lease_state(server, rsp,
2521 &oparms->fid->epoch,
2522 oparms->fid->lease_key);
2524 *oplock = rsp->OplockLevel;
2526 SMB2_open_free(&rqst);
2527 free_rsp_buf(resp_buftype, rsp);
2532 SMB2_ioctl_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2533 u64 persistent_fid, u64 volatile_fid, u32 opcode,
2534 bool is_fsctl, char *in_data, u32 indatalen,
2535 __u32 max_response_size)
2537 struct smb2_ioctl_req *req;
2538 struct kvec *iov = rqst->rq_iov;
2539 unsigned int total_len;
2543 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
2549 * indatalen is usually small at a couple of bytes max, so
2550 * just allocate through generic pool
2552 in_data_buf = kmalloc(indatalen, GFP_NOFS);
2554 cifs_small_buf_release(req);
2557 memcpy(in_data_buf, in_data, indatalen);
2560 req->CtlCode = cpu_to_le32(opcode);
2561 req->PersistentFileId = persistent_fid;
2562 req->VolatileFileId = volatile_fid;
2564 iov[0].iov_base = (char *)req;
2566 * If no input data, the size of ioctl struct in
2567 * protocol spec still includes a 1 byte data buffer,
2568 * but if input data passed to ioctl, we do not
2569 * want to double count this, so we do not send
2570 * the dummy one byte of data in iovec[0] if sending
2571 * input data (in iovec[1]).
2574 req->InputCount = cpu_to_le32(indatalen);
2575 /* do not set InputOffset if no input data */
2577 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
2579 iov[0].iov_len = total_len - 1;
2580 iov[1].iov_base = in_data_buf;
2581 iov[1].iov_len = indatalen;
2584 iov[0].iov_len = total_len;
2587 req->OutputOffset = 0;
2588 req->OutputCount = 0; /* MBZ */
2591 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2592 * We Could increase default MaxOutputResponse, but that could require
2593 * more credits. Windows typically sets this smaller, but for some
2594 * ioctls it may be useful to allow server to send more. No point
2595 * limiting what the server can send as long as fits in one credit
2596 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2597 * to increase this limit up in the future.
2598 * Note that for snapshot queries that servers like Azure expect that
2599 * the first query be minimal size (and just used to get the number/size
2600 * of previous versions) so response size must be specified as EXACTLY
2601 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2602 * of eight bytes. Currently that is the only case where we set max
2603 * response size smaller.
2605 req->MaxOutputResponse = cpu_to_le32(max_response_size);
2608 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2612 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2613 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
2614 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
2620 SMB2_ioctl_free(struct smb_rqst *rqst)
2622 if (rqst && rqst->rq_iov) {
2623 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2624 if (rqst->rq_iov[1].iov_len)
2625 kfree(rqst->rq_iov[1].iov_base);
2631 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2634 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2635 u64 volatile_fid, u32 opcode, bool is_fsctl,
2636 char *in_data, u32 indatalen, u32 max_out_data_len,
2637 char **out_data, u32 *plen /* returned data len */)
2639 struct smb_rqst rqst;
2640 struct smb2_ioctl_rsp *rsp = NULL;
2641 struct cifs_ses *ses;
2642 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
2643 struct kvec rsp_iov = {NULL, 0};
2644 int resp_buftype = CIFS_NO_BUFFER;
2648 cifs_dbg(FYI, "SMB2 IOCTL\n");
2650 if (out_data != NULL)
2653 /* zero out returned data len, in case of error */
2662 if (!ses || !(ses->server))
2665 if (smb3_encryption_required(tcon))
2666 flags |= CIFS_TRANSFORM_REQ;
2668 memset(&rqst, 0, sizeof(struct smb_rqst));
2669 memset(&iov, 0, sizeof(iov));
2671 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
2673 rc = SMB2_ioctl_init(tcon, &rqst, persistent_fid, volatile_fid, opcode,
2674 is_fsctl, in_data, indatalen, max_out_data_len);
2678 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2680 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
2683 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2684 ses->Suid, 0, opcode, rc);
2686 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
2687 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2689 } else if (rc == -EINVAL) {
2690 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2691 (opcode != FSCTL_SRV_COPYCHUNK)) {
2692 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2695 } else if (rc == -E2BIG) {
2696 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
2697 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2702 /* check if caller wants to look at return data or just return rc */
2703 if ((plen == NULL) || (out_data == NULL))
2706 *plen = le32_to_cpu(rsp->OutputCount);
2708 /* We check for obvious errors in the output buffer length and offset */
2710 goto ioctl_exit; /* server returned no data */
2711 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
2712 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2718 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
2719 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2720 le32_to_cpu(rsp->OutputOffset));
2726 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2728 if (*out_data == NULL) {
2734 SMB2_ioctl_free(&rqst);
2735 free_rsp_buf(resp_buftype, rsp);
2740 * Individual callers to ioctl worker function follow
2744 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2745 u64 persistent_fid, u64 volatile_fid)
2748 struct compress_ioctl fsctl_input;
2749 char *ret_data = NULL;
2751 fsctl_input.CompressionState =
2752 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
2754 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2755 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2756 (char *)&fsctl_input /* data input */,
2757 2 /* in data len */, CIFSMaxBufSize /* max out data */,
2758 &ret_data /* out data */, NULL);
2760 cifs_dbg(FYI, "set compression rc %d\n", rc);
2766 SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2767 u64 persistent_fid, u64 volatile_fid)
2769 struct smb2_close_req *req;
2770 struct kvec *iov = rqst->rq_iov;
2771 unsigned int total_len;
2774 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2778 req->PersistentFileId = persistent_fid;
2779 req->VolatileFileId = volatile_fid;
2780 iov[0].iov_base = (char *)req;
2781 iov[0].iov_len = total_len;
2787 SMB2_close_free(struct smb_rqst *rqst)
2789 if (rqst && rqst->rq_iov)
2790 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2794 SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2795 u64 persistent_fid, u64 volatile_fid, int flags)
2797 struct smb_rqst rqst;
2798 struct smb2_close_rsp *rsp = NULL;
2799 struct cifs_ses *ses = tcon->ses;
2801 struct kvec rsp_iov;
2802 int resp_buftype = CIFS_NO_BUFFER;
2805 cifs_dbg(FYI, "Close\n");
2807 if (!ses || !(ses->server))
2810 if (smb3_encryption_required(tcon))
2811 flags |= CIFS_TRANSFORM_REQ;
2813 memset(&rqst, 0, sizeof(struct smb_rqst));
2814 memset(&iov, 0, sizeof(iov));
2818 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid);
2822 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2823 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2826 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
2827 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2832 atomic_dec(&tcon->num_remote_opens);
2834 /* BB FIXME - decode close response, update inode for caching */
2837 SMB2_close_free(&rqst);
2838 free_rsp_buf(resp_buftype, rsp);
2843 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2844 u64 persistent_fid, u64 volatile_fid)
2846 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2850 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
2851 struct kvec *iov, unsigned int min_buf_size)
2853 unsigned int smb_len = iov->iov_len;
2854 char *end_of_smb = smb_len + (char *)iov->iov_base;
2855 char *begin_of_buf = offset + (char *)iov->iov_base;
2856 char *end_of_buf = begin_of_buf + buffer_length;
2859 if (buffer_length < min_buf_size) {
2860 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2861 buffer_length, min_buf_size);
2865 /* check if beyond RFC1001 maximum length */
2866 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
2867 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2868 buffer_length, smb_len);
2872 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
2873 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
2881 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2882 * Caller must free buffer.
2885 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
2886 struct kvec *iov, unsigned int minbufsize,
2889 char *begin_of_buf = offset + (char *)iov->iov_base;
2895 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
2899 memcpy(data, begin_of_buf, buffer_length);
2905 SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2906 u64 persistent_fid, u64 volatile_fid,
2907 u8 info_class, u8 info_type, u32 additional_info,
2908 size_t output_len, size_t input_len, void *input)
2910 struct smb2_query_info_req *req;
2911 struct kvec *iov = rqst->rq_iov;
2912 unsigned int total_len;
2915 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2920 req->InfoType = info_type;
2921 req->FileInfoClass = info_class;
2922 req->PersistentFileId = persistent_fid;
2923 req->VolatileFileId = volatile_fid;
2924 req->AdditionalInformation = cpu_to_le32(additional_info);
2926 req->OutputBufferLength = cpu_to_le32(output_len);
2928 req->InputBufferLength = cpu_to_le32(input_len);
2929 /* total_len for smb query request never close to le16 max */
2930 req->InputBufferOffset = cpu_to_le16(total_len - 1);
2931 memcpy(req->Buffer, input, input_len);
2934 iov[0].iov_base = (char *)req;
2936 iov[0].iov_len = total_len - 1 + input_len;
2941 SMB2_query_info_free(struct smb_rqst *rqst)
2943 if (rqst && rqst->rq_iov)
2944 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2948 query_info(const unsigned int xid, struct cifs_tcon *tcon,
2949 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2950 u32 additional_info, size_t output_len, size_t min_len, void **data,
2953 struct smb_rqst rqst;
2954 struct smb2_query_info_rsp *rsp = NULL;
2956 struct kvec rsp_iov;
2958 int resp_buftype = CIFS_NO_BUFFER;
2959 struct cifs_ses *ses = tcon->ses;
2961 bool allocated = false;
2963 cifs_dbg(FYI, "Query Info\n");
2965 if (!ses || !(ses->server))
2968 if (smb3_encryption_required(tcon))
2969 flags |= CIFS_TRANSFORM_REQ;
2971 memset(&rqst, 0, sizeof(struct smb_rqst));
2972 memset(&iov, 0, sizeof(iov));
2976 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
2977 info_class, info_type, additional_info,
2978 output_len, 0, NULL);
2982 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
2983 ses->Suid, info_class, (__u32)info_type);
2985 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2986 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2989 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2990 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
2991 ses->Suid, info_class, (__u32)info_type, rc);
2995 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
2996 ses->Suid, info_class, (__u32)info_type);
2999 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3001 *data = kmalloc(*dlen, GFP_KERNEL);
3004 "Error %d allocating memory for acl\n",
3014 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3015 le32_to_cpu(rsp->OutputBufferLength),
3016 &rsp_iov, min_len, *data);
3017 if (rc && allocated) {
3024 SMB2_query_info_free(&rqst);
3025 free_rsp_buf(resp_buftype, rsp);
3029 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3030 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3032 return query_info(xid, tcon, persistent_fid, volatile_fid,
3033 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3034 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3035 sizeof(struct smb2_file_all_info), (void **)&data,
3040 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3041 u64 persistent_fid, u64 volatile_fid,
3042 void **data, u32 *plen)
3044 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3047 return query_info(xid, tcon, persistent_fid, volatile_fid,
3048 0, SMB2_O_INFO_SECURITY, additional_info,
3049 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3053 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3054 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3056 return query_info(xid, tcon, persistent_fid, volatile_fid,
3057 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3058 sizeof(struct smb2_file_internal_info),
3059 sizeof(struct smb2_file_internal_info),
3060 (void **)&uniqueid, NULL);
3064 * This is a no-op for now. We're not really interested in the reply, but
3065 * rather in the fact that the server sent one and that server->lstrp
3068 * FIXME: maybe we should consider checking that the reply matches request?
3071 smb2_echo_callback(struct mid_q_entry *mid)
3073 struct TCP_Server_Info *server = mid->callback_data;
3074 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3075 struct cifs_credits credits = { .value = 0, .instance = 0 };
3077 if (mid->mid_state == MID_RESPONSE_RECEIVED
3078 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3079 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3080 credits.instance = server->reconnect_instance;
3083 DeleteMidQEntry(mid);
3084 add_credits(server, &credits, CIFS_ECHO_OP);
3087 void smb2_reconnect_server(struct work_struct *work)
3089 struct TCP_Server_Info *server = container_of(work,
3090 struct TCP_Server_Info, reconnect.work);
3091 struct cifs_ses *ses;
3092 struct cifs_tcon *tcon, *tcon2;
3093 struct list_head tmp_list;
3094 int tcon_exist = false;
3096 int resched = false;
3099 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3100 mutex_lock(&server->reconnect_mutex);
3102 INIT_LIST_HEAD(&tmp_list);
3103 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3105 spin_lock(&cifs_tcp_ses_lock);
3106 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3107 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3108 if (tcon->need_reconnect || tcon->need_reopen_files) {
3110 list_add_tail(&tcon->rlist, &tmp_list);
3114 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3115 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3120 * Get the reference to server struct to be sure that the last call of
3121 * cifs_put_tcon() in the loop below won't release the server pointer.
3124 server->srv_count++;
3126 spin_unlock(&cifs_tcp_ses_lock);
3128 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3129 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
3131 cifs_reopen_persistent_handles(tcon);
3134 list_del_init(&tcon->rlist);
3135 cifs_put_tcon(tcon);
3138 cifs_dbg(FYI, "Reconnecting tcons finished\n");
3140 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3141 mutex_unlock(&server->reconnect_mutex);
3143 /* now we can safely release srv struct */
3145 cifs_put_tcp_session(server, 1);
3149 SMB2_echo(struct TCP_Server_Info *server)
3151 struct smb2_echo_req *req;
3154 struct smb_rqst rqst = { .rq_iov = iov,
3156 unsigned int total_len;
3158 cifs_dbg(FYI, "In echo request\n");
3160 if (server->tcpStatus == CifsNeedNegotiate) {
3161 /* No need to send echo on newly established connections */
3162 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3166 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
3170 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3172 iov[0].iov_len = total_len;
3173 iov[0].iov_base = (char *)req;
3175 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3176 server, CIFS_ECHO_OP, NULL);
3178 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3180 cifs_small_buf_release(req);
3185 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3188 struct smb_rqst rqst;
3189 struct smb2_flush_req *req;
3190 struct cifs_ses *ses = tcon->ses;
3192 struct kvec rsp_iov;
3196 unsigned int total_len;
3198 cifs_dbg(FYI, "Flush\n");
3200 if (!ses || !(ses->server))
3203 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
3207 if (smb3_encryption_required(tcon))
3208 flags |= CIFS_TRANSFORM_REQ;
3210 req->PersistentFileId = persistent_fid;
3211 req->VolatileFileId = volatile_fid;
3213 iov[0].iov_base = (char *)req;
3214 iov[0].iov_len = total_len;
3216 memset(&rqst, 0, sizeof(struct smb_rqst));
3220 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3221 cifs_small_buf_release(req);
3224 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
3225 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3229 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3234 * To form a chain of read requests, any read requests after the first should
3235 * have the end_of_chain boolean set to true.
3238 smb2_new_read_req(void **buf, unsigned int *total_len,
3239 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3240 unsigned int remaining_bytes, int request_type)
3243 struct smb2_read_plain_req *req = NULL;
3244 struct smb2_sync_hdr *shdr;
3245 struct TCP_Server_Info *server;
3247 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3252 server = io_parms->tcon->ses->server;
3254 return -ECONNABORTED;
3256 shdr = &req->sync_hdr;
3257 shdr->ProcessId = cpu_to_le32(io_parms->pid);
3259 req->PersistentFileId = io_parms->persistent_fid;
3260 req->VolatileFileId = io_parms->volatile_fid;
3261 req->ReadChannelInfoOffset = 0; /* reserved */
3262 req->ReadChannelInfoLength = 0; /* reserved */
3263 req->Channel = 0; /* reserved */
3264 req->MinimumCount = 0;
3265 req->Length = cpu_to_le32(io_parms->length);
3266 req->Offset = cpu_to_le64(io_parms->offset);
3268 trace_smb3_read_enter(0 /* xid */,
3269 io_parms->persistent_fid,
3270 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3271 io_parms->offset, io_parms->length);
3272 #ifdef CONFIG_CIFS_SMB_DIRECT
3274 * If we want to do a RDMA write, fill in and append
3275 * smbd_buffer_descriptor_v1 to the end of read request
3277 if (server->rdma && rdata && !server->sign &&
3278 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
3280 struct smbd_buffer_descriptor_v1 *v1;
3281 bool need_invalidate =
3282 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3284 rdata->mr = smbd_register_mr(
3285 server->smbd_conn, rdata->pages,
3286 rdata->nr_pages, rdata->page_offset,
3287 rdata->tailsz, true, need_invalidate);
3291 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3292 if (need_invalidate)
3293 req->Channel = SMB2_CHANNEL_RDMA_V1;
3294 req->ReadChannelInfoOffset =
3295 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
3296 req->ReadChannelInfoLength =
3297 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3298 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3299 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3300 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3301 v1->length = cpu_to_le32(rdata->mr->mr->length);
3303 *total_len += sizeof(*v1) - 1;
3306 if (request_type & CHAINED_REQUEST) {
3307 if (!(request_type & END_OF_CHAIN)) {
3308 /* next 8-byte aligned request */
3309 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3310 shdr->NextCommand = cpu_to_le32(*total_len);
3311 } else /* END_OF_CHAIN */
3312 shdr->NextCommand = 0;
3313 if (request_type & RELATED_REQUEST) {
3314 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
3316 * Related requests use info from previous read request
3319 shdr->SessionId = 0xFFFFFFFF;
3320 shdr->TreeId = 0xFFFFFFFF;
3321 req->PersistentFileId = 0xFFFFFFFF;
3322 req->VolatileFileId = 0xFFFFFFFF;
3325 if (remaining_bytes > io_parms->length)
3326 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3328 req->RemainingBytes = 0;
3335 smb2_readv_callback(struct mid_q_entry *mid)
3337 struct cifs_readdata *rdata = mid->callback_data;
3338 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3339 struct TCP_Server_Info *server = tcon->ses->server;
3340 struct smb2_sync_hdr *shdr =
3341 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
3342 struct cifs_credits credits = { .value = 0, .instance = 0 };
3343 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3345 .rq_pages = rdata->pages,
3346 .rq_offset = rdata->page_offset,
3347 .rq_npages = rdata->nr_pages,
3348 .rq_pagesz = rdata->pagesz,
3349 .rq_tailsz = rdata->tailsz };
3351 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3352 __func__, mid->mid, mid->mid_state, rdata->result,
3355 switch (mid->mid_state) {
3356 case MID_RESPONSE_RECEIVED:
3357 credits.value = le16_to_cpu(shdr->CreditRequest);
3358 credits.instance = server->reconnect_instance;
3359 /* result already set, check signature */
3360 if (server->sign && !mid->decrypted) {
3363 rc = smb2_verify_signature(&rqst, server);
3365 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
3368 /* FIXME: should this be counted toward the initiating task? */
3369 task_io_account_read(rdata->got_bytes);
3370 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3372 case MID_REQUEST_SUBMITTED:
3373 case MID_RETRY_NEEDED:
3374 rdata->result = -EAGAIN;
3375 if (server->sign && rdata->got_bytes)
3376 /* reset bytes number since we can not check a sign */
3377 rdata->got_bytes = 0;
3378 /* FIXME: should this be counted toward the initiating task? */
3379 task_io_account_read(rdata->got_bytes);
3380 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3382 case MID_RESPONSE_MALFORMED:
3383 credits.value = le16_to_cpu(shdr->CreditRequest);
3384 credits.instance = server->reconnect_instance;
3387 rdata->result = -EIO;
3389 #ifdef CONFIG_CIFS_SMB_DIRECT
3391 * If this rdata has a memmory registered, the MR can be freed
3392 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3393 * because they have limited number and are used for future I/Os
3396 smbd_deregister_mr(rdata->mr);
3400 if (rdata->result && rdata->result != -ENODATA) {
3401 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
3402 trace_smb3_read_err(0 /* xid */,
3403 rdata->cfile->fid.persistent_fid,
3404 tcon->tid, tcon->ses->Suid, rdata->offset,
3405 rdata->bytes, rdata->result);
3407 trace_smb3_read_done(0 /* xid */,
3408 rdata->cfile->fid.persistent_fid,
3409 tcon->tid, tcon->ses->Suid,
3410 rdata->offset, rdata->got_bytes);
3412 queue_work(cifsiod_wq, &rdata->work);
3413 DeleteMidQEntry(mid);
3414 add_credits(server, &credits, 0);
3417 /* smb2_async_readv - send an async read, and set up mid to handle result */
3419 smb2_async_readv(struct cifs_readdata *rdata)
3423 struct smb2_sync_hdr *shdr;
3424 struct cifs_io_parms io_parms;
3425 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3427 struct TCP_Server_Info *server;
3428 unsigned int total_len;
3430 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3431 __func__, rdata->offset, rdata->bytes);
3433 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3434 io_parms.offset = rdata->offset;
3435 io_parms.length = rdata->bytes;
3436 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3437 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3438 io_parms.pid = rdata->pid;
3440 server = io_parms.tcon->ses->server;
3442 rc = smb2_new_read_req(
3443 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
3447 if (smb3_encryption_required(io_parms.tcon))
3448 flags |= CIFS_TRANSFORM_REQ;
3450 rdata->iov[0].iov_base = buf;
3451 rdata->iov[0].iov_len = total_len;
3453 shdr = (struct smb2_sync_hdr *)buf;
3455 if (rdata->credits.value > 0) {
3456 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
3457 SMB2_MAX_BUFFER_SIZE));
3458 shdr->CreditRequest =
3459 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
3461 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3463 goto async_readv_out;
3465 flags |= CIFS_HAS_CREDITS;
3468 kref_get(&rdata->refcount);
3469 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
3470 cifs_readv_receive, smb2_readv_callback,
3471 smb3_handle_read_data, rdata, flags,
3474 kref_put(&rdata->refcount, cifs_readdata_release);
3475 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
3476 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3478 io_parms.tcon->ses->Suid,
3479 io_parms.offset, io_parms.length, rc);
3483 cifs_small_buf_release(buf);
3488 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3489 unsigned int *nbytes, char **buf, int *buf_type)
3491 struct smb_rqst rqst;
3492 int resp_buftype, rc = -EACCES;
3493 struct smb2_read_plain_req *req = NULL;
3494 struct smb2_read_rsp *rsp = NULL;
3496 struct kvec rsp_iov;
3497 unsigned int total_len;
3498 int flags = CIFS_LOG_ERROR;
3499 struct cifs_ses *ses = io_parms->tcon->ses;
3502 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
3506 if (smb3_encryption_required(io_parms->tcon))
3507 flags |= CIFS_TRANSFORM_REQ;
3509 iov[0].iov_base = (char *)req;
3510 iov[0].iov_len = total_len;
3512 memset(&rqst, 0, sizeof(struct smb_rqst));
3516 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3517 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
3520 if (rc != -ENODATA) {
3521 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3522 cifs_dbg(VFS, "Send error in read = %d\n", rc);
3523 trace_smb3_read_err(xid, req->PersistentFileId,
3524 io_parms->tcon->tid, ses->Suid,
3525 io_parms->offset, io_parms->length,
3528 trace_smb3_read_done(xid, req->PersistentFileId,
3529 io_parms->tcon->tid, ses->Suid,
3530 io_parms->offset, 0);
3531 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3532 cifs_small_buf_release(req);
3533 return rc == -ENODATA ? 0 : rc;
3535 trace_smb3_read_done(xid, req->PersistentFileId,
3536 io_parms->tcon->tid, ses->Suid,
3537 io_parms->offset, io_parms->length);
3539 cifs_small_buf_release(req);
3541 *nbytes = le32_to_cpu(rsp->DataLength);
3542 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3543 (*nbytes > io_parms->length)) {
3544 cifs_dbg(FYI, "bad length %d for count %d\n",
3545 *nbytes, io_parms->length);
3551 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
3552 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3553 } else if (resp_buftype != CIFS_NO_BUFFER) {
3554 *buf = rsp_iov.iov_base;
3555 if (resp_buftype == CIFS_SMALL_BUFFER)
3556 *buf_type = CIFS_SMALL_BUFFER;
3557 else if (resp_buftype == CIFS_LARGE_BUFFER)
3558 *buf_type = CIFS_LARGE_BUFFER;
3564 * Check the mid_state and signature on received buffer (if any), and queue the
3565 * workqueue completion task.
3568 smb2_writev_callback(struct mid_q_entry *mid)
3570 struct cifs_writedata *wdata = mid->callback_data;
3571 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3572 struct TCP_Server_Info *server = tcon->ses->server;
3573 unsigned int written;
3574 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
3575 struct cifs_credits credits = { .value = 0, .instance = 0 };
3577 switch (mid->mid_state) {
3578 case MID_RESPONSE_RECEIVED:
3579 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3580 credits.instance = server->reconnect_instance;
3581 wdata->result = smb2_check_receive(mid, server, 0);
3582 if (wdata->result != 0)
3585 written = le32_to_cpu(rsp->DataLength);
3587 * Mask off high 16 bits when bytes written as returned
3588 * by the server is greater than bytes requested by the
3589 * client. OS/2 servers are known to set incorrect
3592 if (written > wdata->bytes)
3595 if (written < wdata->bytes)
3596 wdata->result = -ENOSPC;
3598 wdata->bytes = written;
3600 case MID_REQUEST_SUBMITTED:
3601 case MID_RETRY_NEEDED:
3602 wdata->result = -EAGAIN;
3604 case MID_RESPONSE_MALFORMED:
3605 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3606 credits.instance = server->reconnect_instance;
3609 wdata->result = -EIO;
3612 #ifdef CONFIG_CIFS_SMB_DIRECT
3614 * If this wdata has a memory registered, the MR can be freed
3615 * The number of MRs available is limited, it's important to recover
3616 * used MR as soon as I/O is finished. Hold MR longer in the later
3617 * I/O process can possibly result in I/O deadlock due to lack of MR
3618 * to send request on I/O retry
3621 smbd_deregister_mr(wdata->mr);
3625 if (wdata->result) {
3626 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3627 trace_smb3_write_err(0 /* no xid */,
3628 wdata->cfile->fid.persistent_fid,
3629 tcon->tid, tcon->ses->Suid, wdata->offset,
3630 wdata->bytes, wdata->result);
3632 trace_smb3_write_done(0 /* no xid */,
3633 wdata->cfile->fid.persistent_fid,
3634 tcon->tid, tcon->ses->Suid,
3635 wdata->offset, wdata->bytes);
3637 queue_work(cifsiod_wq, &wdata->work);
3638 DeleteMidQEntry(mid);
3639 add_credits(server, &credits, 0);
3642 /* smb2_async_writev - send an async write, and set up mid to handle result */
3644 smb2_async_writev(struct cifs_writedata *wdata,
3645 void (*release)(struct kref *kref))
3647 int rc = -EACCES, flags = 0;
3648 struct smb2_write_req *req = NULL;
3649 struct smb2_sync_hdr *shdr;
3650 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3651 struct TCP_Server_Info *server = tcon->ses->server;
3653 struct smb_rqst rqst = { };
3654 unsigned int total_len;
3656 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
3660 if (smb3_encryption_required(tcon))
3661 flags |= CIFS_TRANSFORM_REQ;
3663 shdr = (struct smb2_sync_hdr *)req;
3664 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
3666 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3667 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3668 req->WriteChannelInfoOffset = 0;
3669 req->WriteChannelInfoLength = 0;
3671 req->Offset = cpu_to_le64(wdata->offset);
3672 req->DataOffset = cpu_to_le16(
3673 offsetof(struct smb2_write_req, Buffer));
3674 req->RemainingBytes = 0;
3676 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
3677 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
3678 #ifdef CONFIG_CIFS_SMB_DIRECT
3680 * If we want to do a server RDMA read, fill in and append
3681 * smbd_buffer_descriptor_v1 to the end of write request
3683 if (server->rdma && !server->sign && wdata->bytes >=
3684 server->smbd_conn->rdma_readwrite_threshold) {
3686 struct smbd_buffer_descriptor_v1 *v1;
3687 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3689 wdata->mr = smbd_register_mr(
3690 server->smbd_conn, wdata->pages,
3691 wdata->nr_pages, wdata->page_offset,
3692 wdata->tailsz, false, need_invalidate);
3695 goto async_writev_out;
3698 req->DataOffset = 0;
3699 if (wdata->nr_pages > 1)
3700 req->RemainingBytes =
3702 (wdata->nr_pages - 1) * wdata->pagesz -
3703 wdata->page_offset + wdata->tailsz
3706 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
3707 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3708 if (need_invalidate)
3709 req->Channel = SMB2_CHANNEL_RDMA_V1;
3710 req->WriteChannelInfoOffset =
3711 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
3712 req->WriteChannelInfoLength =
3713 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3714 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3715 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3716 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3717 v1->length = cpu_to_le32(wdata->mr->mr->length);
3720 iov[0].iov_len = total_len - 1;
3721 iov[0].iov_base = (char *)req;
3725 rqst.rq_pages = wdata->pages;
3726 rqst.rq_offset = wdata->page_offset;
3727 rqst.rq_npages = wdata->nr_pages;
3728 rqst.rq_pagesz = wdata->pagesz;
3729 rqst.rq_tailsz = wdata->tailsz;
3730 #ifdef CONFIG_CIFS_SMB_DIRECT
3732 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
3736 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3737 wdata->offset, wdata->bytes);
3739 #ifdef CONFIG_CIFS_SMB_DIRECT
3740 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3742 req->Length = cpu_to_le32(wdata->bytes);
3744 req->Length = cpu_to_le32(wdata->bytes);
3747 if (wdata->credits.value > 0) {
3748 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
3749 SMB2_MAX_BUFFER_SIZE));
3750 shdr->CreditRequest =
3751 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
3753 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3755 goto async_writev_out;
3757 flags |= CIFS_HAS_CREDITS;
3760 kref_get(&wdata->refcount);
3761 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3762 wdata, flags, &wdata->credits);
3765 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
3766 tcon->tid, tcon->ses->Suid, wdata->offset,
3768 kref_put(&wdata->refcount, release);
3769 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3773 cifs_small_buf_release(req);
3778 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3779 * The length field from io_parms must be at least 1 and indicates a number of
3780 * elements with data to write that begins with position 1 in iov array. All
3781 * data length is specified by count.
3784 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3785 unsigned int *nbytes, struct kvec *iov, int n_vec)
3787 struct smb_rqst rqst;
3789 struct smb2_write_req *req = NULL;
3790 struct smb2_write_rsp *rsp = NULL;
3792 struct kvec rsp_iov;
3794 unsigned int total_len;
3801 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3806 if (io_parms->tcon->ses->server == NULL)
3807 return -ECONNABORTED;
3809 if (smb3_encryption_required(io_parms->tcon))
3810 flags |= CIFS_TRANSFORM_REQ;
3812 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
3814 req->PersistentFileId = io_parms->persistent_fid;
3815 req->VolatileFileId = io_parms->volatile_fid;
3816 req->WriteChannelInfoOffset = 0;
3817 req->WriteChannelInfoLength = 0;
3819 req->Length = cpu_to_le32(io_parms->length);
3820 req->Offset = cpu_to_le64(io_parms->offset);
3821 req->DataOffset = cpu_to_le16(
3822 offsetof(struct smb2_write_req, Buffer));
3823 req->RemainingBytes = 0;
3825 trace_smb3_write_enter(xid, io_parms->persistent_fid,
3826 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3827 io_parms->offset, io_parms->length);
3829 iov[0].iov_base = (char *)req;
3831 iov[0].iov_len = total_len - 1;
3833 memset(&rqst, 0, sizeof(struct smb_rqst));
3835 rqst.rq_nvec = n_vec + 1;
3837 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
3838 &resp_buftype, flags, &rsp_iov);
3839 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
3842 trace_smb3_write_err(xid, req->PersistentFileId,
3843 io_parms->tcon->tid,
3844 io_parms->tcon->ses->Suid,
3845 io_parms->offset, io_parms->length, rc);
3846 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
3847 cifs_dbg(VFS, "Send error in write = %d\n", rc);
3849 *nbytes = le32_to_cpu(rsp->DataLength);
3850 trace_smb3_write_done(xid, req->PersistentFileId,
3851 io_parms->tcon->tid,
3852 io_parms->tcon->ses->Suid,
3853 io_parms->offset, *nbytes);
3856 cifs_small_buf_release(req);
3857 free_rsp_buf(resp_buftype, rsp);
3862 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3865 unsigned int entrycount = 0;
3866 unsigned int next_offset = 0;
3868 FILE_DIRECTORY_INFO *dir_info;
3870 if (bufstart == NULL)
3873 entryptr = bufstart;
3876 if (entryptr + next_offset < entryptr ||
3877 entryptr + next_offset > end_of_buf ||
3878 entryptr + next_offset + size > end_of_buf) {
3879 cifs_dbg(VFS, "malformed search entry would overflow\n");
3883 entryptr = entryptr + next_offset;
3884 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
3886 len = le32_to_cpu(dir_info->FileNameLength);
3887 if (entryptr + len < entryptr ||
3888 entryptr + len > end_of_buf ||
3889 entryptr + len + size > end_of_buf) {
3890 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3895 *lastentry = entryptr;
3898 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
3910 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3911 u64 persistent_fid, u64 volatile_fid, int index,
3912 struct cifs_search_info *srch_inf)
3914 struct smb_rqst rqst;
3915 struct smb2_query_directory_req *req;
3916 struct smb2_query_directory_rsp *rsp = NULL;
3918 struct kvec rsp_iov;
3921 int resp_buftype = CIFS_NO_BUFFER;
3922 unsigned char *bufptr;
3923 struct TCP_Server_Info *server;
3924 struct cifs_ses *ses = tcon->ses;
3925 __le16 asteriks = cpu_to_le16('*');
3927 unsigned int output_size = CIFSMaxBufSize;
3928 size_t info_buf_size;
3930 unsigned int total_len;
3932 if (ses && (ses->server))
3933 server = ses->server;
3937 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3942 if (smb3_encryption_required(tcon))
3943 flags |= CIFS_TRANSFORM_REQ;
3945 switch (srch_inf->info_level) {
3946 case SMB_FIND_FILE_DIRECTORY_INFO:
3947 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3948 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3950 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3951 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3952 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3955 cifs_dbg(VFS, "info level %u isn't supported\n",
3956 srch_inf->info_level);
3961 req->FileIndex = cpu_to_le32(index);
3962 req->PersistentFileId = persistent_fid;
3963 req->VolatileFileId = volatile_fid;
3966 bufptr = req->Buffer;
3967 memcpy(bufptr, &asteriks, len);
3969 req->FileNameOffset =
3970 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
3971 req->FileNameLength = cpu_to_le16(len);
3973 * BB could be 30 bytes or so longer if we used SMB2 specific
3974 * buffer lengths, but this is safe and close enough.
3976 output_size = min_t(unsigned int, output_size, server->maxBuf);
3977 output_size = min_t(unsigned int, output_size, 2 << 15);
3978 req->OutputBufferLength = cpu_to_le32(output_size);
3980 iov[0].iov_base = (char *)req;
3982 iov[0].iov_len = total_len - 1;
3984 iov[1].iov_base = (char *)(req->Buffer);
3985 iov[1].iov_len = len;
3987 memset(&rqst, 0, sizeof(struct smb_rqst));
3991 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
3992 tcon->ses->Suid, index, output_size);
3994 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3995 cifs_small_buf_release(req);
3996 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
3999 if (rc == -ENODATA &&
4000 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
4001 trace_smb3_query_dir_done(xid, persistent_fid,
4002 tcon->tid, tcon->ses->Suid, index, 0);
4003 srch_inf->endOfSearch = true;
4006 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4007 tcon->ses->Suid, index, 0, rc);
4008 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
4013 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4014 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4017 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4018 tcon->ses->Suid, index, 0, rc);
4022 srch_inf->unicode = true;
4024 if (srch_inf->ntwrk_buf_start) {
4025 if (srch_inf->smallBuf)
4026 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4028 cifs_buf_release(srch_inf->ntwrk_buf_start);
4030 srch_inf->ntwrk_buf_start = (char *)rsp;
4031 srch_inf->srch_entries_start = srch_inf->last_entry =
4032 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4033 end_of_smb = rsp_iov.iov_len + (char *)rsp;
4034 srch_inf->entries_in_buffer =
4035 num_entries(srch_inf->srch_entries_start, end_of_smb,
4036 &srch_inf->last_entry, info_buf_size);
4037 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4038 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4039 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4040 srch_inf->srch_entries_start, srch_inf->last_entry);
4041 if (resp_buftype == CIFS_LARGE_BUFFER)
4042 srch_inf->smallBuf = false;
4043 else if (resp_buftype == CIFS_SMALL_BUFFER)
4044 srch_inf->smallBuf = true;
4046 cifs_dbg(VFS, "illegal search buffer type\n");
4048 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4049 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
4053 free_rsp_buf(resp_buftype, rsp);
4058 SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
4059 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4060 u8 info_type, u32 additional_info,
4061 void **data, unsigned int *size)
4063 struct smb2_set_info_req *req;
4064 struct kvec *iov = rqst->rq_iov;
4065 unsigned int i, total_len;
4068 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
4072 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4073 req->InfoType = info_type;
4074 req->FileInfoClass = info_class;
4075 req->PersistentFileId = persistent_fid;
4076 req->VolatileFileId = volatile_fid;
4077 req->AdditionalInformation = cpu_to_le32(additional_info);
4080 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
4081 req->BufferLength = cpu_to_le32(*size);
4083 memcpy(req->Buffer, *data, *size);
4086 iov[0].iov_base = (char *)req;
4088 iov[0].iov_len = total_len - 1;
4090 for (i = 1; i < rqst->rq_nvec; i++) {
4091 le32_add_cpu(&req->BufferLength, size[i]);
4092 iov[i].iov_base = (char *)data[i];
4093 iov[i].iov_len = size[i];
4100 SMB2_set_info_free(struct smb_rqst *rqst)
4102 if (rqst && rqst->rq_iov)
4103 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
4107 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4108 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4109 u8 info_type, u32 additional_info, unsigned int num,
4110 void **data, unsigned int *size)
4112 struct smb_rqst rqst;
4113 struct smb2_set_info_rsp *rsp = NULL;
4115 struct kvec rsp_iov;
4118 struct cifs_ses *ses = tcon->ses;
4121 if (!ses || !(ses->server))
4127 if (smb3_encryption_required(tcon))
4128 flags |= CIFS_TRANSFORM_REQ;
4130 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4134 memset(&rqst, 0, sizeof(struct smb_rqst));
4138 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
4139 info_class, info_type, additional_info,
4147 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
4149 SMB2_set_info_free(&rqst);
4150 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
4153 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
4154 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4155 ses->Suid, info_class, (__u32)info_type, rc);
4158 free_rsp_buf(resp_buftype, rsp);
4164 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4165 u64 volatile_fid, u32 pid, __le64 *eof)
4167 struct smb2_file_eof_info info;
4171 info.EndOfFile = *eof;
4174 size = sizeof(struct smb2_file_eof_info);
4176 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4177 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4178 0, 1, &data, &size);
4182 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4183 u64 persistent_fid, u64 volatile_fid,
4184 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4186 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4187 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4188 1, (void **)&pnntsd, &pacllen);
4192 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4193 u64 persistent_fid, u64 volatile_fid,
4194 struct smb2_file_full_ea_info *buf, int len)
4196 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4197 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4198 0, 1, (void **)&buf, &len);
4202 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4203 const u64 persistent_fid, const u64 volatile_fid,
4206 struct smb_rqst rqst;
4208 struct smb2_oplock_break *req = NULL;
4209 struct cifs_ses *ses = tcon->ses;
4210 int flags = CIFS_OBREAK_OP;
4211 unsigned int total_len;
4213 struct kvec rsp_iov;
4216 cifs_dbg(FYI, "SMB2_oplock_break\n");
4217 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4222 if (smb3_encryption_required(tcon))
4223 flags |= CIFS_TRANSFORM_REQ;
4225 req->VolatileFid = volatile_fid;
4226 req->PersistentFid = persistent_fid;
4227 req->OplockLevel = oplock_level;
4228 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4230 flags |= CIFS_NO_RSP_BUF;
4232 iov[0].iov_base = (char *)req;
4233 iov[0].iov_len = total_len;
4235 memset(&rqst, 0, sizeof(struct smb_rqst));
4239 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4240 cifs_small_buf_release(req);
4243 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4244 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
4251 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4252 struct kstatfs *kst)
4254 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4255 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4256 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
4257 kst->f_bfree = kst->f_bavail =
4258 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
4263 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4264 struct kstatfs *kst)
4266 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4267 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4268 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4269 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4270 kst->f_bavail = kst->f_bfree;
4272 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4273 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4274 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4275 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4276 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4282 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4283 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4286 struct smb2_query_info_req *req;
4287 unsigned int total_len;
4289 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
4291 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4294 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4299 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4300 req->FileInfoClass = level;
4301 req->PersistentFileId = persistent_fid;
4302 req->VolatileFileId = volatile_fid;
4304 req->InputBufferOffset =
4305 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
4306 req->OutputBufferLength = cpu_to_le32(
4307 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
4309 iov->iov_base = (char *)req;
4310 iov->iov_len = total_len;
4315 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4316 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4318 struct smb_rqst rqst;
4319 struct smb2_query_info_rsp *rsp = NULL;
4321 struct kvec rsp_iov;
4324 struct cifs_ses *ses = tcon->ses;
4325 FILE_SYSTEM_POSIX_INFO *info = NULL;
4328 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4329 sizeof(FILE_SYSTEM_POSIX_INFO),
4330 persistent_fid, volatile_fid);
4334 if (smb3_encryption_required(tcon))
4335 flags |= CIFS_TRANSFORM_REQ;
4337 memset(&rqst, 0, sizeof(struct smb_rqst));
4341 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4342 cifs_small_buf_release(iov.iov_base);
4344 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4345 goto posix_qfsinf_exit;
4347 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4349 info = (FILE_SYSTEM_POSIX_INFO *)(
4350 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4351 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4352 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4353 sizeof(FILE_SYSTEM_POSIX_INFO));
4355 copy_posix_fs_info_to_kstatfs(info, fsdata);
4358 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4363 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4364 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4366 struct smb_rqst rqst;
4367 struct smb2_query_info_rsp *rsp = NULL;
4369 struct kvec rsp_iov;
4372 struct cifs_ses *ses = tcon->ses;
4373 struct smb2_fs_full_size_info *info = NULL;
4376 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4377 sizeof(struct smb2_fs_full_size_info),
4378 persistent_fid, volatile_fid);
4382 if (smb3_encryption_required(tcon))
4383 flags |= CIFS_TRANSFORM_REQ;
4385 memset(&rqst, 0, sizeof(struct smb_rqst));
4389 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4390 cifs_small_buf_release(iov.iov_base);
4392 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4395 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4397 info = (struct smb2_fs_full_size_info *)(
4398 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4399 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4400 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4401 sizeof(struct smb2_fs_full_size_info));
4403 smb2_copy_fs_info_to_kstatfs(info, fsdata);
4406 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4411 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
4412 u64 persistent_fid, u64 volatile_fid, int level)
4414 struct smb_rqst rqst;
4415 struct smb2_query_info_rsp *rsp = NULL;
4417 struct kvec rsp_iov;
4419 int resp_buftype, max_len, min_len;
4420 struct cifs_ses *ses = tcon->ses;
4421 unsigned int rsp_len, offset;
4424 if (level == FS_DEVICE_INFORMATION) {
4425 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4426 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4427 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4428 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4429 min_len = MIN_FS_ATTR_INFO_SIZE;
4430 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4431 max_len = sizeof(struct smb3_fs_ss_info);
4432 min_len = sizeof(struct smb3_fs_ss_info);
4433 } else if (level == FS_VOLUME_INFORMATION) {
4434 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4435 min_len = sizeof(struct smb3_fs_vol_info);
4437 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
4441 rc = build_qfs_info_req(&iov, tcon, level, max_len,
4442 persistent_fid, volatile_fid);
4446 if (smb3_encryption_required(tcon))
4447 flags |= CIFS_TRANSFORM_REQ;
4449 memset(&rqst, 0, sizeof(struct smb_rqst));
4453 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4454 cifs_small_buf_release(iov.iov_base);
4456 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4459 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4461 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4462 offset = le16_to_cpu(rsp->OutputBufferOffset);
4463 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
4467 if (level == FS_ATTRIBUTE_INFORMATION)
4468 memcpy(&tcon->fsAttrInfo, offset
4469 + (char *)rsp, min_t(unsigned int,
4471 else if (level == FS_DEVICE_INFORMATION)
4472 memcpy(&tcon->fsDevInfo, offset
4473 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
4474 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4475 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
4476 (offset + (char *)rsp);
4477 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4478 tcon->perf_sector_size =
4479 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
4480 } else if (level == FS_VOLUME_INFORMATION) {
4481 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4482 (offset + (char *)rsp);
4483 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4484 tcon->vol_create_time = vol_info->VolumeCreationTime;
4488 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4493 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4494 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4495 const __u32 num_lock, struct smb2_lock_element *buf)
4497 struct smb_rqst rqst;
4499 struct smb2_lock_req *req = NULL;
4501 struct kvec rsp_iov;
4504 int flags = CIFS_NO_RSP_BUF;
4505 unsigned int total_len;
4507 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
4509 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
4513 if (smb3_encryption_required(tcon))
4514 flags |= CIFS_TRANSFORM_REQ;
4516 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4517 req->LockCount = cpu_to_le16(num_lock);
4519 req->PersistentFileId = persist_fid;
4520 req->VolatileFileId = volatile_fid;
4522 count = num_lock * sizeof(struct smb2_lock_element);
4524 iov[0].iov_base = (char *)req;
4525 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
4526 iov[1].iov_base = (char *)buf;
4527 iov[1].iov_len = count;
4529 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
4531 memset(&rqst, 0, sizeof(struct smb_rqst));
4535 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
4537 cifs_small_buf_release(req);
4539 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
4540 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
4541 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4542 tcon->ses->Suid, rc);
4549 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4550 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4551 const __u64 length, const __u64 offset, const __u32 lock_flags,
4554 struct smb2_lock_element lock;
4556 lock.Offset = cpu_to_le64(offset);
4557 lock.Length = cpu_to_le64(length);
4558 lock.Flags = cpu_to_le32(lock_flags);
4559 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
4560 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
4562 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
4566 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
4567 __u8 *lease_key, const __le32 lease_state)
4569 struct smb_rqst rqst;
4571 struct smb2_lease_ack *req = NULL;
4572 struct cifs_ses *ses = tcon->ses;
4573 int flags = CIFS_OBREAK_OP;
4574 unsigned int total_len;
4576 struct kvec rsp_iov;
4578 __u64 *please_key_high;
4579 __u64 *please_key_low;
4581 cifs_dbg(FYI, "SMB2_lease_break\n");
4582 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4587 if (smb3_encryption_required(tcon))
4588 flags |= CIFS_TRANSFORM_REQ;
4590 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4591 req->StructureSize = cpu_to_le16(36);
4594 memcpy(req->LeaseKey, lease_key, 16);
4595 req->LeaseState = lease_state;
4597 flags |= CIFS_NO_RSP_BUF;
4599 iov[0].iov_base = (char *)req;
4600 iov[0].iov_len = total_len;
4602 memset(&rqst, 0, sizeof(struct smb_rqst));
4606 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4607 cifs_small_buf_release(req);
4609 please_key_low = (__u64 *)lease_key;
4610 please_key_high = (__u64 *)(lease_key+8);
4612 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4613 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
4614 ses->Suid, *please_key_low, *please_key_high, rc);
4615 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
4617 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
4618 ses->Suid, *please_key_low, *please_key_high);