1 // SPDX-License-Identifier: LGPL-2.1
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
13 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
14 /* Note that there are handle based routines which must be */
15 /* treated slightly differently for reconnection purposes since we never */
16 /* want to reuse a stale file handle and only the caller knows the file info */
19 #include <linux/kernel.h>
20 #include <linux/vfs.h>
21 #include <linux/task_io_accounting_ops.h>
22 #include <linux/uaccess.h>
23 #include <linux/uuid.h>
24 #include <linux/pagemap.h>
25 #include <linux/xattr.h>
28 #include "cifsproto.h"
29 #include "smb2proto.h"
30 #include "cifs_unicode.h"
31 #include "cifs_debug.h"
33 #include "smb2status.h"
36 #include "cifs_spnego.h"
37 #include "smbdirect.h"
39 #ifdef CONFIG_CIFS_DFS_UPCALL
40 #include "dfs_cache.h"
42 #include "cached_dir.h"
45 * The following table defines the expected "StructureSize" of SMB2 requests
46 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
48 * Note that commands are defined in smb2pdu.h in le16 but the array below is
49 * indexed by command in host byte order.
51 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
52 /* SMB2_NEGOTIATE */ 36,
53 /* SMB2_SESSION_SETUP */ 25,
55 /* SMB2_TREE_CONNECT */ 9,
56 /* SMB2_TREE_DISCONNECT */ 4,
66 /* SMB2_QUERY_DIRECTORY */ 33,
67 /* SMB2_CHANGE_NOTIFY */ 32,
68 /* SMB2_QUERY_INFO */ 41,
69 /* SMB2_SET_INFO */ 33,
70 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
73 int smb3_encryption_required(const struct cifs_tcon *tcon)
75 if (!tcon || !tcon->ses)
77 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
78 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
81 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
87 smb2_hdr_assemble(struct smb2_hdr *shdr, __le16 smb2_cmd,
88 const struct cifs_tcon *tcon,
89 struct TCP_Server_Info *server)
91 struct smb3_hdr_req *smb3_hdr;
93 shdr->ProtocolId = SMB2_PROTO_NUMBER;
94 shdr->StructureSize = cpu_to_le16(64);
95 shdr->Command = smb2_cmd;
98 /* After reconnect SMB3 must set ChannelSequence on subsequent reqs */
99 if (server->dialect >= SMB30_PROT_ID) {
100 smb3_hdr = (struct smb3_hdr_req *)shdr;
102 * if primary channel is not set yet, use default
103 * channel for chan sequence num
105 if (SERVER_IS_CHAN(server))
106 smb3_hdr->ChannelSequence =
107 cpu_to_le16(server->primary_server->channel_sequence_num);
109 smb3_hdr->ChannelSequence =
110 cpu_to_le16(server->channel_sequence_num);
112 spin_lock(&server->req_lock);
113 /* Request up to 10 credits but don't go over the limit. */
114 if (server->credits >= server->max_credits)
115 shdr->CreditRequest = cpu_to_le16(0);
117 shdr->CreditRequest = cpu_to_le16(
118 min_t(int, server->max_credits -
119 server->credits, 10));
120 spin_unlock(&server->req_lock);
122 shdr->CreditRequest = cpu_to_le16(2);
124 shdr->Id.SyncId.ProcessId = cpu_to_le32((__u16)current->tgid);
129 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
130 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
131 if (server && (server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
132 shdr->CreditCharge = cpu_to_le16(1);
133 /* else CreditCharge MBZ */
135 shdr->Id.SyncId.TreeId = cpu_to_le32(tcon->tid);
136 /* Uid is not converted */
138 shdr->SessionId = cpu_to_le64(tcon->ses->Suid);
141 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
142 * to pass the path on the Open SMB prefixed by \\server\share.
143 * Not sure when we would need to do the augmented path (if ever) and
144 * setting this flag breaks the SMB2 open operation since it is
145 * illegal to send an empty path name (without \\server\share prefix)
146 * when the DFS flag is set in the SMB open header. We could
147 * consider setting the flag on all operations other than open
148 * but it is safer to net set it for now.
150 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
151 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
153 if (server && server->sign && !smb3_encryption_required(tcon))
154 shdr->Flags |= SMB2_FLAGS_SIGNED;
159 /* helper function for code reuse */
161 cifs_chan_skip_or_disable(struct cifs_ses *ses,
162 struct TCP_Server_Info *server,
165 struct TCP_Server_Info *pserver;
166 unsigned int chan_index;
168 if (SERVER_IS_CHAN(server)) {
170 "server %s does not support multichannel anymore. Skip secondary channel\n",
171 ses->server->hostname);
173 spin_lock(&ses->chan_lock);
174 chan_index = cifs_ses_get_chan_index(ses, server);
175 if (chan_index == CIFS_INVAL_CHAN_INDEX) {
176 spin_unlock(&ses->chan_lock);
180 ses->chans[chan_index].server = NULL;
181 spin_unlock(&ses->chan_lock);
184 * the above reference of server by channel
185 * needs to be dropped without holding chan_lock
186 * as cifs_put_tcp_session takes a higher lock
187 * i.e. cifs_tcp_ses_lock
189 cifs_put_tcp_session(server, from_reconnect);
191 server->terminate = true;
192 cifs_signal_cifsd_for_reconnect(server, false);
194 /* mark primary server as needing reconnect */
195 pserver = server->primary_server;
196 cifs_signal_cifsd_for_reconnect(pserver, false);
202 "server does not support multichannel anymore. Disable all other channels\n");
203 cifs_disable_secondary_channels(ses);
210 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
211 struct TCP_Server_Info *server, bool from_reconnect)
214 struct nls_table *nls_codepage = NULL;
215 struct cifs_ses *ses;
219 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
220 * check for tcp and smb session status done differently
221 * for those three - in the calling routine.
227 * Need to also skip SMB2_IOCTL because it is used for checking nested dfs links in
228 * cifs_tree_connect().
230 if (smb2_command == SMB2_TREE_CONNECT || smb2_command == SMB2_IOCTL)
233 spin_lock(&tcon->tc_lock);
234 if (tcon->status == TID_EXITING) {
236 * only tree disconnect allowed when disconnecting ...
238 if (smb2_command != SMB2_TREE_DISCONNECT) {
239 spin_unlock(&tcon->tc_lock);
240 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
245 spin_unlock(&tcon->tc_lock);
250 spin_lock(&ses->ses_lock);
251 if (ses->ses_status == SES_EXITING) {
252 spin_unlock(&ses->ses_lock);
255 spin_unlock(&ses->ses_lock);
256 if (!ses->server || !server)
259 spin_lock(&server->srv_lock);
260 if (server->tcpStatus == CifsNeedReconnect) {
262 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
263 * here since they are implicitly done when session drops.
265 switch (smb2_command) {
267 * BB Should we keep oplock break and add flush to exceptions?
269 case SMB2_TREE_DISCONNECT:
272 case SMB2_OPLOCK_BREAK:
273 spin_unlock(&server->srv_lock);
278 /* if server is marked for termination, cifsd will cleanup */
279 if (server->terminate) {
280 spin_unlock(&server->srv_lock);
283 spin_unlock(&server->srv_lock);
286 rc = cifs_wait_for_server_reconnect(server, tcon->retry);
290 spin_lock(&ses->chan_lock);
291 if (!cifs_chan_needs_reconnect(ses, server) && !tcon->need_reconnect) {
292 spin_unlock(&ses->chan_lock);
295 spin_unlock(&ses->chan_lock);
296 cifs_dbg(FYI, "sess reconnect mask: 0x%lx, tcon reconnect: %d",
297 tcon->ses->chans_need_reconnect,
298 tcon->need_reconnect);
300 mutex_lock(&ses->session_mutex);
302 * if this is called by delayed work, and the channel has been disabled
303 * in parallel, the delayed work can continue to execute in parallel
304 * there's a chance that this channel may not exist anymore
306 spin_lock(&server->srv_lock);
307 if (server->tcpStatus == CifsExiting) {
308 spin_unlock(&server->srv_lock);
309 mutex_unlock(&ses->session_mutex);
315 * Recheck after acquire mutex. If another thread is negotiating
316 * and the server never sends an answer the socket will be closed
317 * and tcpStatus set to reconnect.
319 if (server->tcpStatus == CifsNeedReconnect) {
320 spin_unlock(&server->srv_lock);
321 mutex_unlock(&ses->session_mutex);
329 spin_unlock(&server->srv_lock);
331 nls_codepage = ses->local_nls;
334 * need to prevent multiple threads trying to simultaneously
335 * reconnect the same SMB session
337 spin_lock(&ses->ses_lock);
338 spin_lock(&ses->chan_lock);
339 if (!cifs_chan_needs_reconnect(ses, server) &&
340 ses->ses_status == SES_GOOD) {
341 spin_unlock(&ses->chan_lock);
342 spin_unlock(&ses->ses_lock);
343 /* this means that we only need to tree connect */
344 if (tcon->need_reconnect)
345 goto skip_sess_setup;
347 mutex_unlock(&ses->session_mutex);
350 spin_unlock(&ses->chan_lock);
351 spin_unlock(&ses->ses_lock);
353 rc = cifs_negotiate_protocol(0, ses, server);
356 * if server stopped supporting multichannel
357 * and the first channel reconnected, disable all the others.
359 if (ses->chan_count > 1 &&
360 !(server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
361 rc = cifs_chan_skip_or_disable(ses, server,
364 mutex_unlock(&ses->session_mutex);
369 rc = cifs_setup_session(0, ses, server, nls_codepage);
370 if ((rc == -EACCES) && !tcon->retry) {
371 mutex_unlock(&ses->session_mutex);
375 mutex_unlock(&ses->session_mutex);
379 mutex_unlock(&ses->session_mutex);
384 if (!tcon->need_reconnect) {
385 mutex_unlock(&ses->session_mutex);
388 cifs_mark_open_files_invalid(tcon);
389 if (tcon->use_persistent)
390 tcon->need_reopen_files = true;
392 rc = cifs_tree_connect(0, tcon, nls_codepage);
394 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
396 /* If sess reconnected but tcon didn't, something strange ... */
397 mutex_unlock(&ses->session_mutex);
398 cifs_dbg(VFS, "reconnect tcon failed rc = %d\n", rc);
403 (server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
404 mutex_unlock(&ses->session_mutex);
407 * query server network interfaces, in case they change
410 rc = SMB3_request_interfaces(xid, tcon, false);
413 if (rc == -EOPNOTSUPP) {
415 * some servers like Azure SMB server do not advertise
416 * that multichannel has been disabled with server
417 * capabilities, rather return STATUS_NOT_IMPLEMENTED.
418 * treat this as server not supporting multichannel
421 rc = cifs_chan_skip_or_disable(ses, server,
423 goto skip_add_channels;
425 cifs_dbg(FYI, "%s: failed to query server interfaces: %d\n",
428 if (ses->chan_max > ses->chan_count &&
430 !SERVER_IS_CHAN(server)) {
431 if (ses->chan_count == 1)
432 cifs_server_dbg(VFS, "supports multichannel now\n");
434 cifs_try_adding_channels(ses);
437 mutex_unlock(&ses->session_mutex);
441 if (smb2_command != SMB2_INTERNAL_CMD)
442 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
444 atomic_inc(&tconInfoReconnectCount);
447 * Check if handle based operation so we know whether we can continue
448 * or not without returning to caller to reset file handle.
451 * BB Is flush done by server on drop of tcp session? Should we special
452 * case it and skip above?
454 switch (smb2_command) {
459 case SMB2_QUERY_DIRECTORY:
460 case SMB2_CHANGE_NOTIFY:
461 case SMB2_QUERY_INFO:
470 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon,
471 struct TCP_Server_Info *server,
473 unsigned int *total_len)
475 struct smb2_pdu *spdu = buf;
476 /* lookup word count ie StructureSize from table */
477 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
480 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
481 * largest operations (Create)
485 smb2_hdr_assemble(&spdu->hdr, smb2_command, tcon, server);
486 spdu->StructureSize2 = cpu_to_le16(parmsize);
488 *total_len = parmsize + sizeof(struct smb2_hdr);
492 * Allocate and return pointer to an SMB request hdr, and set basic
493 * SMB information in the SMB header. If the return code is zero, this
494 * function must have filled in request_buf pointer.
496 static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
497 struct TCP_Server_Info *server,
498 void **request_buf, unsigned int *total_len)
500 /* BB eventually switch this to SMB2 specific small buf size */
501 switch (smb2_command) {
503 case SMB2_QUERY_INFO:
504 *request_buf = cifs_buf_get();
507 *request_buf = cifs_small_buf_get();
510 if (*request_buf == NULL) {
511 /* BB should we add a retry in here if not a writepage? */
515 fill_small_buf(smb2_command, tcon, server,
516 (struct smb2_hdr *)(*request_buf),
520 uint16_t com_code = le16_to_cpu(smb2_command);
521 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
522 cifs_stats_inc(&tcon->num_smbs_sent);
528 static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
529 struct TCP_Server_Info *server,
530 void **request_buf, unsigned int *total_len)
534 rc = smb2_reconnect(smb2_command, tcon, server, false);
538 return __smb2_plain_req_init(smb2_command, tcon, server, request_buf,
542 static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
543 struct TCP_Server_Info *server,
544 void **request_buf, unsigned int *total_len)
546 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
547 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
548 return __smb2_plain_req_init(SMB2_IOCTL, tcon, server,
549 request_buf, total_len);
551 return smb2_plain_req_init(SMB2_IOCTL, tcon, server,
552 request_buf, total_len);
555 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
558 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
560 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
561 pneg_ctxt->DataLength = cpu_to_le16(38);
562 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
563 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
564 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
565 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
569 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
571 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
572 pneg_ctxt->DataLength =
573 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
574 - sizeof(struct smb2_neg_context));
575 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
576 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
577 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
578 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
582 build_signing_ctxt(struct smb2_signing_capabilities *pneg_ctxt)
584 unsigned int ctxt_len = sizeof(struct smb2_signing_capabilities);
585 unsigned short num_algs = 1; /* number of signing algorithms sent */
587 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
589 * Context Data length must be rounded to multiple of 8 for some servers
591 pneg_ctxt->DataLength = cpu_to_le16(ALIGN(sizeof(struct smb2_signing_capabilities) -
592 sizeof(struct smb2_neg_context) +
593 (num_algs * sizeof(u16)), 8));
594 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(num_algs);
595 pneg_ctxt->SigningAlgorithms[0] = cpu_to_le16(SIGNING_ALG_AES_CMAC);
597 ctxt_len += sizeof(__le16) * num_algs;
598 ctxt_len = ALIGN(ctxt_len, 8);
600 /* TBD add SIGNING_ALG_AES_GMAC and/or SIGNING_ALG_HMAC_SHA256 */
604 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
606 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
607 if (require_gcm_256) {
608 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + 1 cipher */
609 pneg_ctxt->CipherCount = cpu_to_le16(1);
610 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES256_GCM;
611 } else if (enable_gcm_256) {
612 pneg_ctxt->DataLength = cpu_to_le16(8); /* Cipher Count + 3 ciphers */
613 pneg_ctxt->CipherCount = cpu_to_le16(3);
614 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
615 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES256_GCM;
616 pneg_ctxt->Ciphers[2] = SMB2_ENCRYPTION_AES128_CCM;
618 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + 2 ciphers */
619 pneg_ctxt->CipherCount = cpu_to_le16(2);
620 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
621 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
626 build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
628 struct nls_table *cp = load_nls_default();
630 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
632 /* copy up to max of first 100 bytes of server name to NetName field */
633 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
634 /* context size is DataLength + minimal smb2_neg_context */
635 return ALIGN(le16_to_cpu(pneg_ctxt->DataLength) + sizeof(struct smb2_neg_context), 8);
639 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
641 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
642 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
643 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
644 pneg_ctxt->Name[0] = 0x93;
645 pneg_ctxt->Name[1] = 0xAD;
646 pneg_ctxt->Name[2] = 0x25;
647 pneg_ctxt->Name[3] = 0x50;
648 pneg_ctxt->Name[4] = 0x9C;
649 pneg_ctxt->Name[5] = 0xB4;
650 pneg_ctxt->Name[6] = 0x11;
651 pneg_ctxt->Name[7] = 0xE7;
652 pneg_ctxt->Name[8] = 0xB4;
653 pneg_ctxt->Name[9] = 0x23;
654 pneg_ctxt->Name[10] = 0x83;
655 pneg_ctxt->Name[11] = 0xDE;
656 pneg_ctxt->Name[12] = 0x96;
657 pneg_ctxt->Name[13] = 0x8B;
658 pneg_ctxt->Name[14] = 0xCD;
659 pneg_ctxt->Name[15] = 0x7C;
663 assemble_neg_contexts(struct smb2_negotiate_req *req,
664 struct TCP_Server_Info *server, unsigned int *total_len)
666 unsigned int ctxt_len, neg_context_count;
667 struct TCP_Server_Info *pserver;
671 if (*total_len > 200) {
672 /* In case length corrupted don't want to overrun smb buffer */
673 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
678 * round up total_len of fixed part of SMB3 negotiate request to 8
679 * byte boundary before adding negotiate contexts
681 *total_len = ALIGN(*total_len, 8);
683 pneg_ctxt = (*total_len) + (char *)req;
684 req->NegotiateContextOffset = cpu_to_le32(*total_len);
686 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
687 ctxt_len = ALIGN(sizeof(struct smb2_preauth_neg_context), 8);
688 *total_len += ctxt_len;
689 pneg_ctxt += ctxt_len;
691 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
692 ctxt_len = ALIGN(sizeof(struct smb2_encryption_neg_context), 8);
693 *total_len += ctxt_len;
694 pneg_ctxt += ctxt_len;
697 * secondary channels don't have the hostname field populated
698 * use the hostname field in the primary channel instead
700 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
701 cifs_server_lock(pserver);
702 hostname = pserver->hostname;
703 if (hostname && (hostname[0] != 0)) {
704 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
706 *total_len += ctxt_len;
707 pneg_ctxt += ctxt_len;
708 neg_context_count = 3;
710 neg_context_count = 2;
711 cifs_server_unlock(pserver);
713 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
714 *total_len += sizeof(struct smb2_posix_neg_context);
715 pneg_ctxt += sizeof(struct smb2_posix_neg_context);
718 if (server->compress_algorithm) {
719 build_compression_ctxt((struct smb2_compression_capabilities_context *)
721 ctxt_len = ALIGN(sizeof(struct smb2_compression_capabilities_context), 8);
722 *total_len += ctxt_len;
723 pneg_ctxt += ctxt_len;
727 if (enable_negotiate_signing) {
728 ctxt_len = build_signing_ctxt((struct smb2_signing_capabilities *)
730 *total_len += ctxt_len;
731 pneg_ctxt += ctxt_len;
735 /* check for and add transport_capabilities and signing capabilities */
736 req->NegotiateContextCount = cpu_to_le16(neg_context_count);
740 /* If invalid preauth context warn but use what we requested, SHA-512 */
741 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
743 unsigned int len = le16_to_cpu(ctxt->DataLength);
746 * Caller checked that DataLength remains within SMB boundary. We still
747 * need to confirm that one HashAlgorithms member is accounted for.
749 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
750 pr_warn_once("server sent bad preauth context\n");
752 } else if (len < MIN_PREAUTH_CTXT_DATA_LEN + le16_to_cpu(ctxt->SaltLength)) {
753 pr_warn_once("server sent invalid SaltLength\n");
756 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
757 pr_warn_once("Invalid SMB3 hash algorithm count\n");
758 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
759 pr_warn_once("unknown SMB3 hash algorithm\n");
762 static void decode_compress_ctx(struct TCP_Server_Info *server,
763 struct smb2_compression_capabilities_context *ctxt)
765 unsigned int len = le16_to_cpu(ctxt->DataLength);
768 * Caller checked that DataLength remains within SMB boundary. We still
769 * need to confirm that one CompressionAlgorithms member is accounted
773 pr_warn_once("server sent bad compression cntxt\n");
776 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
777 pr_warn_once("Invalid SMB3 compress algorithm count\n");
780 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
781 pr_warn_once("unknown compression algorithm\n");
784 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
787 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
788 struct smb2_encryption_neg_context *ctxt)
790 unsigned int len = le16_to_cpu(ctxt->DataLength);
792 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
794 * Caller checked that DataLength remains within SMB boundary. We still
795 * need to confirm that one Cipher flexible array member is accounted
798 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
799 pr_warn_once("server sent bad crypto ctxt len\n");
803 if (le16_to_cpu(ctxt->CipherCount) != 1) {
804 pr_warn_once("Invalid SMB3.11 cipher count\n");
807 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
808 if (require_gcm_256) {
809 if (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM) {
810 cifs_dbg(VFS, "Server does not support requested encryption type (AES256 GCM)\n");
813 } else if (ctxt->Ciphers[0] == 0) {
815 * e.g. if server only supported AES256_CCM (very unlikely)
816 * or server supported no encryption types or had all disabled.
817 * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case
818 * in which mount requested encryption ("seal") checks later
819 * on during tree connection will return proper rc, but if
820 * seal not requested by client, since server is allowed to
821 * return 0 to indicate no supported cipher, we can't fail here
823 server->cipher_type = 0;
824 server->capabilities &= ~SMB2_GLOBAL_CAP_ENCRYPTION;
825 pr_warn_once("Server does not support requested encryption types\n");
827 } else if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
828 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM) &&
829 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM)) {
830 /* server returned a cipher we didn't ask for */
831 pr_warn_once("Invalid SMB3.11 cipher returned\n");
834 server->cipher_type = ctxt->Ciphers[0];
835 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
839 static void decode_signing_ctx(struct TCP_Server_Info *server,
840 struct smb2_signing_capabilities *pctxt)
842 unsigned int len = le16_to_cpu(pctxt->DataLength);
845 * Caller checked that DataLength remains within SMB boundary. We still
846 * need to confirm that one SigningAlgorithms flexible array member is
849 if ((len < 4) || (len > 16)) {
850 pr_warn_once("server sent bad signing negcontext\n");
853 if (le16_to_cpu(pctxt->SigningAlgorithmCount) != 1) {
854 pr_warn_once("Invalid signing algorithm count\n");
857 if (le16_to_cpu(pctxt->SigningAlgorithms[0]) > 2) {
858 pr_warn_once("unknown signing algorithm\n");
862 server->signing_negotiated = true;
863 server->signing_algorithm = le16_to_cpu(pctxt->SigningAlgorithms[0]);
864 cifs_dbg(FYI, "signing algorithm %d chosen\n",
865 server->signing_algorithm);
869 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
870 struct TCP_Server_Info *server,
871 unsigned int len_of_smb)
873 struct smb2_neg_context *pctx;
874 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
875 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
876 unsigned int len_of_ctxts, i;
879 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
880 if (len_of_smb <= offset) {
881 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
885 len_of_ctxts = len_of_smb - offset;
887 for (i = 0; i < ctxt_cnt; i++) {
889 /* check that offset is not beyond end of SMB */
890 if (len_of_ctxts < sizeof(struct smb2_neg_context))
893 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
894 clen = sizeof(struct smb2_neg_context)
895 + le16_to_cpu(pctx->DataLength);
897 * 2.2.4 SMB2 NEGOTIATE Response
898 * Subsequent negotiate contexts MUST appear at the first 8-byte
899 * aligned offset following the previous negotiate context.
901 if (i + 1 != ctxt_cnt)
902 clen = ALIGN(clen, 8);
903 if (clen > len_of_ctxts)
906 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
907 decode_preauth_context(
908 (struct smb2_preauth_neg_context *)pctx);
909 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
910 rc = decode_encrypt_ctx(server,
911 (struct smb2_encryption_neg_context *)pctx);
912 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
913 decode_compress_ctx(server,
914 (struct smb2_compression_capabilities_context *)pctx);
915 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
916 server->posix_ext_supported = true;
917 else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES)
918 decode_signing_ctx(server,
919 (struct smb2_signing_capabilities *)pctx);
921 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
922 le16_to_cpu(pctx->ContextType));
927 len_of_ctxts -= clen;
932 static struct create_posix *
933 create_posix_buf(umode_t mode)
935 struct create_posix *buf;
937 buf = kzalloc(sizeof(struct create_posix),
942 buf->ccontext.DataOffset =
943 cpu_to_le16(offsetof(struct create_posix, Mode));
944 buf->ccontext.DataLength = cpu_to_le32(4);
945 buf->ccontext.NameOffset =
946 cpu_to_le16(offsetof(struct create_posix, Name));
947 buf->ccontext.NameLength = cpu_to_le16(16);
949 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
960 buf->Name[10] = 0x83;
961 buf->Name[11] = 0xDE;
962 buf->Name[12] = 0x96;
963 buf->Name[13] = 0x8B;
964 buf->Name[14] = 0xCD;
965 buf->Name[15] = 0x7C;
966 buf->Mode = cpu_to_le32(mode);
967 cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
972 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
974 unsigned int num = *num_iovec;
976 iov[num].iov_base = create_posix_buf(mode);
977 if (mode == ACL_NO_MODE)
978 cifs_dbg(FYI, "%s: no mode\n", __func__);
979 if (iov[num].iov_base == NULL)
981 iov[num].iov_len = sizeof(struct create_posix);
982 *num_iovec = num + 1;
989 * SMB2 Worker functions follow:
991 * The general structure of the worker functions is:
992 * 1) Call smb2_init (assembles SMB2 header)
993 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
994 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
995 * 4) Decode SMB2 command specific fields in the fixed length area
996 * 5) Decode variable length data area (if any for this SMB2 command type)
997 * 6) Call free smb buffer
1003 SMB2_negotiate(const unsigned int xid,
1004 struct cifs_ses *ses,
1005 struct TCP_Server_Info *server)
1007 struct smb_rqst rqst;
1008 struct smb2_negotiate_req *req;
1009 struct smb2_negotiate_rsp *rsp;
1011 struct kvec rsp_iov;
1014 int blob_offset, blob_length;
1015 char *security_blob;
1016 int flags = CIFS_NEG_OP;
1017 unsigned int total_len;
1019 cifs_dbg(FYI, "Negotiate protocol\n");
1022 WARN(1, "%s: server is NULL!\n", __func__);
1026 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
1027 (void **) &req, &total_len);
1031 req->hdr.SessionId = 0;
1033 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
1034 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
1036 if (strcmp(server->vals->version_string,
1037 SMB3ANY_VERSION_STRING) == 0) {
1038 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1039 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1040 req->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1041 req->DialectCount = cpu_to_le16(3);
1043 } else if (strcmp(server->vals->version_string,
1044 SMBDEFAULT_VERSION_STRING) == 0) {
1045 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1046 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1047 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1048 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1049 req->DialectCount = cpu_to_le16(4);
1052 /* otherwise send specific dialect */
1053 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
1054 req->DialectCount = cpu_to_le16(1);
1058 /* only one of SMB2 signing flags may be set in SMB2 request */
1060 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1061 else if (global_secflags & CIFSSEC_MAY_SIGN)
1062 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1064 req->SecurityMode = 0;
1066 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
1067 if (ses->chan_max > 1)
1068 req->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
1070 /* ClientGUID must be zero for SMB2.02 dialect */
1071 if (server->vals->protocol_id == SMB20_PROT_ID)
1072 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
1074 memcpy(req->ClientGUID, server->client_guid,
1075 SMB2_CLIENT_GUID_SIZE);
1076 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
1077 (strcmp(server->vals->version_string,
1078 SMB3ANY_VERSION_STRING) == 0) ||
1079 (strcmp(server->vals->version_string,
1080 SMBDEFAULT_VERSION_STRING) == 0))
1081 assemble_neg_contexts(req, server, &total_len);
1083 iov[0].iov_base = (char *)req;
1084 iov[0].iov_len = total_len;
1086 memset(&rqst, 0, sizeof(struct smb_rqst));
1090 rc = cifs_send_recv(xid, ses, server,
1091 &rqst, &resp_buftype, flags, &rsp_iov);
1092 cifs_small_buf_release(req);
1093 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
1095 * No tcon so can't do
1096 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1098 if (rc == -EOPNOTSUPP) {
1099 cifs_server_dbg(VFS, "Dialect not supported by server. Consider specifying vers=1.0 or vers=2.0 on mount for accessing older servers\n");
1105 if (strcmp(server->vals->version_string,
1106 SMB3ANY_VERSION_STRING) == 0) {
1107 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
1108 cifs_server_dbg(VFS,
1109 "SMB2 dialect returned but not requested\n");
1111 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
1112 cifs_server_dbg(VFS,
1113 "SMB2.1 dialect returned but not requested\n");
1115 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1116 /* ops set to 3.0 by default for default so update */
1117 server->ops = &smb311_operations;
1118 server->vals = &smb311_values;
1120 } else if (strcmp(server->vals->version_string,
1121 SMBDEFAULT_VERSION_STRING) == 0) {
1122 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
1123 cifs_server_dbg(VFS,
1124 "SMB2 dialect returned but not requested\n");
1126 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
1127 /* ops set to 3.0 by default for default so update */
1128 server->ops = &smb21_operations;
1129 server->vals = &smb21_values;
1130 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1131 server->ops = &smb311_operations;
1132 server->vals = &smb311_values;
1134 } else if (le16_to_cpu(rsp->DialectRevision) !=
1135 server->vals->protocol_id) {
1136 /* if requested single dialect ensure returned dialect matched */
1137 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
1138 le16_to_cpu(rsp->DialectRevision));
1142 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
1144 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
1145 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
1146 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
1147 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
1148 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
1149 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
1150 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
1151 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
1152 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
1153 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
1155 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
1156 le16_to_cpu(rsp->DialectRevision));
1161 server->dialect = le16_to_cpu(rsp->DialectRevision);
1164 * Keep a copy of the hash after negprot. This hash will be
1165 * the starting hash value for all sessions made from this
1168 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
1169 SMB2_PREAUTH_HASH_SIZE);
1171 /* SMB2 only has an extended negflavor */
1172 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
1173 /* set it to the maximum buffer size value we can send with 1 credit */
1174 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
1175 SMB2_MAX_BUFFER_SIZE);
1176 server->max_read = le32_to_cpu(rsp->MaxReadSize);
1177 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
1178 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
1179 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
1180 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
1182 server->capabilities = le32_to_cpu(rsp->Capabilities);
1183 /* Internal types */
1184 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
1187 * SMB3.0 supports only 1 cipher and doesn't have a encryption neg context
1188 * Set the cipher type manually.
1190 if (server->dialect == SMB30_PROT_ID && (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1191 server->cipher_type = SMB2_ENCRYPTION_AES128_CCM;
1193 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
1194 (struct smb2_hdr *)rsp);
1196 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
1198 * ses->sectype = RawNTLMSSP;
1199 * but for time being this is our only auth choice so doesn't matter.
1200 * We just found a server which sets blob length to zero expecting raw.
1202 if (blob_length == 0) {
1203 cifs_dbg(FYI, "missing security blob on negprot\n");
1204 server->sec_ntlmssp = true;
1207 rc = cifs_enable_signing(server, ses->sign);
1211 rc = decode_negTokenInit(security_blob, blob_length, server);
1218 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1219 if (rsp->NegotiateContextCount)
1220 rc = smb311_decode_neg_context(rsp, server,
1223 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
1226 free_rsp_buf(resp_buftype, rsp);
1230 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1233 struct validate_negotiate_info_req *pneg_inbuf;
1234 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
1236 u32 inbuflen; /* max of 4 dialects */
1237 struct TCP_Server_Info *server = tcon->ses->server;
1239 cifs_dbg(FYI, "validate negotiate\n");
1241 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1242 if (server->dialect == SMB311_PROT_ID)
1246 * validation ioctl must be signed, so no point sending this if we
1247 * can not sign it (ie are not known user). Even if signing is not
1248 * required (enabled but not negotiated), in those cases we selectively
1249 * sign just this, the first and only signed request on a connection.
1250 * Having validation of negotiate info helps reduce attack vectors.
1252 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1253 return 0; /* validation requires signing */
1255 if (tcon->ses->user_name == NULL) {
1256 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1257 return 0; /* validation requires signing */
1260 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1261 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1263 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1267 pneg_inbuf->Capabilities =
1268 cpu_to_le32(server->vals->req_capabilities);
1269 if (tcon->ses->chan_max > 1)
1270 pneg_inbuf->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
1272 memcpy(pneg_inbuf->Guid, server->client_guid,
1273 SMB2_CLIENT_GUID_SIZE);
1275 if (tcon->ses->sign)
1276 pneg_inbuf->SecurityMode =
1277 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1278 else if (global_secflags & CIFSSEC_MAY_SIGN)
1279 pneg_inbuf->SecurityMode =
1280 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1282 pneg_inbuf->SecurityMode = 0;
1285 if (strcmp(server->vals->version_string,
1286 SMB3ANY_VERSION_STRING) == 0) {
1287 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1288 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1289 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1290 pneg_inbuf->DialectCount = cpu_to_le16(3);
1291 /* SMB 2.1 not included so subtract one dialect from len */
1292 inbuflen = sizeof(*pneg_inbuf) -
1293 (sizeof(pneg_inbuf->Dialects[0]));
1294 } else if (strcmp(server->vals->version_string,
1295 SMBDEFAULT_VERSION_STRING) == 0) {
1296 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1297 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1298 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1299 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1300 pneg_inbuf->DialectCount = cpu_to_le16(4);
1301 /* structure is big enough for 4 dialects */
1302 inbuflen = sizeof(*pneg_inbuf);
1304 /* otherwise specific dialect was requested */
1305 pneg_inbuf->Dialects[0] =
1306 cpu_to_le16(server->vals->protocol_id);
1307 pneg_inbuf->DialectCount = cpu_to_le16(1);
1308 /* structure is big enough for 4 dialects, sending only 1 */
1309 inbuflen = sizeof(*pneg_inbuf) -
1310 sizeof(pneg_inbuf->Dialects[0]) * 3;
1313 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1314 FSCTL_VALIDATE_NEGOTIATE_INFO,
1315 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1316 (char **)&pneg_rsp, &rsplen);
1317 if (rc == -EOPNOTSUPP) {
1319 * Old Windows versions or Netapp SMB server can return
1320 * not supported error. Client should accept it.
1322 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1324 goto out_free_inbuf;
1325 } else if (rc != 0) {
1326 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1329 goto out_free_inbuf;
1333 if (rsplen != sizeof(*pneg_rsp)) {
1334 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1337 /* relax check since Mac returns max bufsize allowed on ioctl */
1338 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1342 /* check validate negotiate info response matches what we got earlier */
1343 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1346 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1349 /* do not validate server guid because not saved at negprot time yet */
1351 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1352 SMB2_LARGE_FILES) != server->capabilities)
1355 /* validate negotiate successful */
1357 cifs_dbg(FYI, "validate negotiate info successful\n");
1361 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1370 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1372 switch (requested) {
1379 if (server->sec_ntlmssp &&
1380 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1382 if ((server->sec_kerberos || server->sec_mskerberos) &&
1383 (global_secflags & CIFSSEC_MAY_KRB5))
1391 struct SMB2_sess_data {
1393 struct cifs_ses *ses;
1394 struct TCP_Server_Info *server;
1395 struct nls_table *nls_cp;
1396 void (*func)(struct SMB2_sess_data *);
1398 u64 previous_session;
1400 /* we will send the SMB in three pieces:
1401 * a fixed length beginning part, an optional
1402 * SPNEGO blob (which can be zero length), and a
1403 * last part which will include the strings
1404 * and rest of bcc area. This allows us to avoid
1405 * a large buffer 17K allocation
1412 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1415 struct cifs_ses *ses = sess_data->ses;
1416 struct TCP_Server_Info *server = sess_data->server;
1417 struct smb2_sess_setup_req *req;
1418 unsigned int total_len;
1419 bool is_binding = false;
1421 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
1427 spin_lock(&ses->ses_lock);
1428 is_binding = (ses->ses_status == SES_GOOD);
1429 spin_unlock(&ses->ses_lock);
1432 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1433 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1434 req->PreviousSessionId = 0;
1435 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1436 cifs_dbg(FYI, "Binding to sess id: %llx\n", ses->Suid);
1438 /* First session, not a reauthenticate */
1439 req->hdr.SessionId = 0;
1441 * if reconnect, we need to send previous sess id
1444 req->PreviousSessionId = cpu_to_le64(sess_data->previous_session);
1445 req->Flags = 0; /* MBZ */
1446 cifs_dbg(FYI, "Fresh session. Previous: %llx\n",
1447 sess_data->previous_session);
1450 /* enough to enable echos and oplocks and one max size write */
1451 if (server->credits >= server->max_credits)
1452 req->hdr.CreditRequest = cpu_to_le16(0);
1454 req->hdr.CreditRequest = cpu_to_le16(
1455 min_t(int, server->max_credits -
1456 server->credits, 130));
1458 /* only one of SMB2 signing flags may be set in SMB2 request */
1460 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1461 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1462 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1464 req->SecurityMode = 0;
1466 #ifdef CONFIG_CIFS_DFS_UPCALL
1467 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1469 req->Capabilities = 0;
1470 #endif /* DFS_UPCALL */
1472 req->Channel = 0; /* MBZ */
1474 sess_data->iov[0].iov_base = (char *)req;
1476 sess_data->iov[0].iov_len = total_len - 1;
1478 * This variable will be used to clear the buffer
1479 * allocated above in case of any error in the calling function.
1481 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1487 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1489 struct kvec *iov = sess_data->iov;
1491 /* iov[1] is already freed by caller */
1492 if (sess_data->buf0_type != CIFS_NO_BUFFER && iov[0].iov_base)
1493 memzero_explicit(iov[0].iov_base, iov[0].iov_len);
1495 free_rsp_buf(sess_data->buf0_type, iov[0].iov_base);
1496 sess_data->buf0_type = CIFS_NO_BUFFER;
1500 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1503 struct smb_rqst rqst;
1504 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1505 struct kvec rsp_iov = { NULL, 0 };
1507 /* Testing shows that buffer offset must be at location of Buffer[0] */
1508 req->SecurityBufferOffset =
1509 cpu_to_le16(sizeof(struct smb2_sess_setup_req));
1510 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1512 memset(&rqst, 0, sizeof(struct smb_rqst));
1513 rqst.rq_iov = sess_data->iov;
1516 /* BB add code to build os and lm fields */
1517 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1520 &sess_data->buf0_type,
1521 CIFS_LOG_ERROR | CIFS_SESS_OP, &rsp_iov);
1522 cifs_small_buf_release(sess_data->iov[0].iov_base);
1523 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1529 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1532 struct cifs_ses *ses = sess_data->ses;
1533 struct TCP_Server_Info *server = sess_data->server;
1535 cifs_server_lock(server);
1536 if (server->ops->generate_signingkey) {
1537 rc = server->ops->generate_signingkey(ses, server);
1540 "SMB3 session key generation failed\n");
1541 cifs_server_unlock(server);
1545 if (!server->session_estab) {
1546 server->sequence_number = 0x2;
1547 server->session_estab = true;
1549 cifs_server_unlock(server);
1551 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1555 #ifdef CONFIG_CIFS_UPCALL
1557 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1560 struct cifs_ses *ses = sess_data->ses;
1561 struct TCP_Server_Info *server = sess_data->server;
1562 struct cifs_spnego_msg *msg;
1563 struct key *spnego_key = NULL;
1564 struct smb2_sess_setup_rsp *rsp = NULL;
1565 bool is_binding = false;
1567 rc = SMB2_sess_alloc_buffer(sess_data);
1571 spnego_key = cifs_get_spnego_key(ses, server);
1572 if (IS_ERR(spnego_key)) {
1573 rc = PTR_ERR(spnego_key);
1575 cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
1580 msg = spnego_key->payload.data[0];
1582 * check version field to make sure that cifs.upcall is
1583 * sending us a response in an expected form
1585 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1586 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1587 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1589 goto out_put_spnego_key;
1592 spin_lock(&ses->ses_lock);
1593 is_binding = (ses->ses_status == SES_GOOD);
1594 spin_unlock(&ses->ses_lock);
1596 /* keep session key if binding */
1598 kfree_sensitive(ses->auth_key.response);
1599 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1601 if (!ses->auth_key.response) {
1602 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1605 goto out_put_spnego_key;
1607 ses->auth_key.len = msg->sesskey_len;
1610 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1611 sess_data->iov[1].iov_len = msg->secblob_len;
1613 rc = SMB2_sess_sendreceive(sess_data);
1615 goto out_put_spnego_key;
1617 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1618 /* keep session id and flags if binding */
1620 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1621 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1624 rc = SMB2_sess_establish_session(sess_data);
1626 key_invalidate(spnego_key);
1627 key_put(spnego_key);
1629 kfree_sensitive(ses->auth_key.response);
1630 ses->auth_key.response = NULL;
1631 ses->auth_key.len = 0;
1634 sess_data->result = rc;
1635 sess_data->func = NULL;
1636 SMB2_sess_free_buffer(sess_data);
1640 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1642 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1643 sess_data->result = -EOPNOTSUPP;
1644 sess_data->func = NULL;
1649 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1652 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1655 struct cifs_ses *ses = sess_data->ses;
1656 struct TCP_Server_Info *server = sess_data->server;
1657 struct smb2_sess_setup_rsp *rsp = NULL;
1658 unsigned char *ntlmssp_blob = NULL;
1659 bool use_spnego = false; /* else use raw ntlmssp */
1660 u16 blob_length = 0;
1661 bool is_binding = false;
1664 * If memory allocation is successful, caller of this function
1667 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1668 if (!ses->ntlmssp) {
1672 ses->ntlmssp->sesskey_per_smbsess = true;
1674 rc = SMB2_sess_alloc_buffer(sess_data);
1678 rc = build_ntlmssp_smb3_negotiate_blob(&ntlmssp_blob,
1679 &blob_length, ses, server,
1685 /* BB eventually need to add this */
1686 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1690 sess_data->iov[1].iov_base = ntlmssp_blob;
1691 sess_data->iov[1].iov_len = blob_length;
1693 rc = SMB2_sess_sendreceive(sess_data);
1694 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1696 /* If true, rc here is expected and not an error */
1697 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1698 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1704 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1705 le16_to_cpu(rsp->SecurityBufferOffset)) {
1706 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1707 le16_to_cpu(rsp->SecurityBufferOffset));
1711 rc = decode_ntlmssp_challenge(rsp->Buffer,
1712 le16_to_cpu(rsp->SecurityBufferLength), ses);
1716 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1718 spin_lock(&ses->ses_lock);
1719 is_binding = (ses->ses_status == SES_GOOD);
1720 spin_unlock(&ses->ses_lock);
1722 /* keep existing ses id and flags if binding */
1724 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1725 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1729 kfree_sensitive(ntlmssp_blob);
1730 SMB2_sess_free_buffer(sess_data);
1732 sess_data->result = 0;
1733 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1737 kfree_sensitive(ses->ntlmssp);
1738 ses->ntlmssp = NULL;
1739 sess_data->result = rc;
1740 sess_data->func = NULL;
1744 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1747 struct cifs_ses *ses = sess_data->ses;
1748 struct TCP_Server_Info *server = sess_data->server;
1749 struct smb2_sess_setup_req *req;
1750 struct smb2_sess_setup_rsp *rsp = NULL;
1751 unsigned char *ntlmssp_blob = NULL;
1752 bool use_spnego = false; /* else use raw ntlmssp */
1753 u16 blob_length = 0;
1754 bool is_binding = false;
1756 rc = SMB2_sess_alloc_buffer(sess_data);
1760 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1761 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1763 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length,
1767 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1772 /* BB eventually need to add this */
1773 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1777 sess_data->iov[1].iov_base = ntlmssp_blob;
1778 sess_data->iov[1].iov_len = blob_length;
1780 rc = SMB2_sess_sendreceive(sess_data);
1784 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1786 spin_lock(&ses->ses_lock);
1787 is_binding = (ses->ses_status == SES_GOOD);
1788 spin_unlock(&ses->ses_lock);
1790 /* keep existing ses id and flags if binding */
1792 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1793 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1796 rc = SMB2_sess_establish_session(sess_data);
1797 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1798 if (ses->server->dialect < SMB30_PROT_ID) {
1799 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1801 * The session id is opaque in terms of endianness, so we can't
1802 * print it as a long long. we dump it as we got it on the wire
1804 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1806 cifs_dbg(VFS, "Session Key %*ph\n",
1807 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1808 cifs_dbg(VFS, "Signing Key %*ph\n",
1809 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1813 kfree_sensitive(ntlmssp_blob);
1814 SMB2_sess_free_buffer(sess_data);
1815 kfree_sensitive(ses->ntlmssp);
1816 ses->ntlmssp = NULL;
1817 sess_data->result = rc;
1818 sess_data->func = NULL;
1822 SMB2_select_sec(struct SMB2_sess_data *sess_data)
1825 struct cifs_ses *ses = sess_data->ses;
1826 struct TCP_Server_Info *server = sess_data->server;
1828 type = smb2_select_sectype(server, ses->sectype);
1829 cifs_dbg(FYI, "sess setup type %d\n", type);
1830 if (type == Unspecified) {
1831 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1837 sess_data->func = SMB2_auth_kerberos;
1840 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1843 cifs_dbg(VFS, "secType %d not supported!\n", type);
1851 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1852 struct TCP_Server_Info *server,
1853 const struct nls_table *nls_cp)
1856 struct SMB2_sess_data *sess_data;
1858 cifs_dbg(FYI, "Session Setup\n");
1861 WARN(1, "%s: server is NULL!\n", __func__);
1865 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1869 sess_data->xid = xid;
1870 sess_data->ses = ses;
1871 sess_data->server = server;
1872 sess_data->buf0_type = CIFS_NO_BUFFER;
1873 sess_data->nls_cp = (struct nls_table *) nls_cp;
1874 sess_data->previous_session = ses->Suid;
1876 rc = SMB2_select_sec(sess_data);
1881 * Initialize the session hash with the server one.
1883 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
1884 SMB2_PREAUTH_HASH_SIZE);
1886 while (sess_data->func)
1887 sess_data->func(sess_data);
1889 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1890 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1891 rc = sess_data->result;
1893 kfree_sensitive(sess_data);
1898 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1900 struct smb_rqst rqst;
1901 struct smb2_logoff_req *req; /* response is also trivial struct */
1903 struct TCP_Server_Info *server;
1905 unsigned int total_len;
1907 struct kvec rsp_iov;
1910 cifs_dbg(FYI, "disconnect session %p\n", ses);
1912 if (ses && (ses->server))
1913 server = ses->server;
1917 /* no need to send SMB logoff if uid already closed due to reconnect */
1918 spin_lock(&ses->chan_lock);
1919 if (CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
1920 spin_unlock(&ses->chan_lock);
1921 goto smb2_session_already_dead;
1923 spin_unlock(&ses->chan_lock);
1925 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
1926 (void **) &req, &total_len);
1930 /* since no tcon, smb2_init can not do this, so do here */
1931 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1933 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1934 flags |= CIFS_TRANSFORM_REQ;
1935 else if (server->sign)
1936 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1938 flags |= CIFS_NO_RSP_BUF;
1940 iov[0].iov_base = (char *)req;
1941 iov[0].iov_len = total_len;
1943 memset(&rqst, 0, sizeof(struct smb_rqst));
1947 rc = cifs_send_recv(xid, ses, ses->server,
1948 &rqst, &resp_buf_type, flags, &rsp_iov);
1949 cifs_small_buf_release(req);
1951 * No tcon so can't do
1952 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1955 smb2_session_already_dead:
1959 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1961 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1964 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1966 /* These are similar values to what Windows uses */
1967 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1969 tcon->max_chunks = 256;
1970 tcon->max_bytes_chunk = 1048576;
1971 tcon->max_bytes_copy = 16777216;
1975 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1976 struct cifs_tcon *tcon, const struct nls_table *cp)
1978 struct smb_rqst rqst;
1979 struct smb2_tree_connect_req *req;
1980 struct smb2_tree_connect_rsp *rsp = NULL;
1982 struct kvec rsp_iov = { NULL, 0 };
1986 __le16 *unc_path = NULL;
1988 unsigned int total_len;
1989 struct TCP_Server_Info *server;
1991 /* always use master channel */
1992 server = ses->server;
1994 cifs_dbg(FYI, "TCON\n");
1996 if (!server || !tree)
1999 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
2000 if (unc_path == NULL)
2003 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp);
2004 if (unc_path_len <= 0) {
2010 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
2012 atomic_set(&tcon->num_remote_opens, 0);
2013 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
2014 (void **) &req, &total_len);
2020 if (smb3_encryption_required(tcon))
2021 flags |= CIFS_TRANSFORM_REQ;
2023 iov[0].iov_base = (char *)req;
2025 iov[0].iov_len = total_len - 1;
2027 /* Testing shows that buffer offset must be at location of Buffer[0] */
2028 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req));
2029 req->PathLength = cpu_to_le16(unc_path_len);
2030 iov[1].iov_base = unc_path;
2031 iov[1].iov_len = unc_path_len;
2034 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
2035 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
2036 * (Samba servers don't always set the flag so also check if null user)
2038 if ((server->dialect == SMB311_PROT_ID) &&
2039 !smb3_encryption_required(tcon) &&
2040 !(ses->session_flags &
2041 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
2042 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
2043 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
2045 memset(&rqst, 0, sizeof(struct smb_rqst));
2049 /* Need 64 for max size write so ask for more in case not there yet */
2050 if (server->credits >= server->max_credits)
2051 req->hdr.CreditRequest = cpu_to_le16(0);
2053 req->hdr.CreditRequest = cpu_to_le16(
2054 min_t(int, server->max_credits -
2055 server->credits, 64));
2057 rc = cifs_send_recv(xid, ses, server,
2058 &rqst, &resp_buftype, flags, &rsp_iov);
2059 cifs_small_buf_release(req);
2060 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
2061 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
2062 if ((rc != 0) || (rsp == NULL)) {
2063 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
2064 tcon->need_reconnect = true;
2065 goto tcon_error_exit;
2068 switch (rsp->ShareType) {
2069 case SMB2_SHARE_TYPE_DISK:
2070 cifs_dbg(FYI, "connection to disk share\n");
2072 case SMB2_SHARE_TYPE_PIPE:
2074 cifs_dbg(FYI, "connection to pipe share\n");
2076 case SMB2_SHARE_TYPE_PRINT:
2078 cifs_dbg(FYI, "connection to printer\n");
2081 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
2083 goto tcon_error_exit;
2086 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
2087 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
2088 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
2089 tcon->tid = le32_to_cpu(rsp->hdr.Id.SyncId.TreeId);
2090 strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name));
2092 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
2093 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
2094 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
2097 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
2098 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
2100 init_copy_chunk_defaults(tcon);
2101 if (server->ops->validate_negotiate)
2102 rc = server->ops->validate_negotiate(xid, tcon);
2103 if (rc == 0) /* See MS-SMB2 2.2.10 and 3.2.5.5 */
2104 if (tcon->share_flags & SMB2_SHAREFLAG_ISOLATED_TRANSPORT)
2105 server->nosharesock = true;
2108 free_rsp_buf(resp_buftype, rsp);
2113 if (rsp && rsp->hdr.Status == STATUS_BAD_NETWORK_NAME)
2114 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
2119 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
2121 struct smb_rqst rqst;
2122 struct smb2_tree_disconnect_req *req; /* response is trivial */
2124 struct cifs_ses *ses = tcon->ses;
2126 unsigned int total_len;
2128 struct kvec rsp_iov;
2131 cifs_dbg(FYI, "Tree Disconnect\n");
2133 if (!ses || !(ses->server))
2136 trace_smb3_tdis_enter(xid, tcon->tid, ses->Suid, tcon->tree_name);
2137 spin_lock(&ses->chan_lock);
2138 if ((tcon->need_reconnect) ||
2139 (CIFS_ALL_CHANS_NEED_RECONNECT(tcon->ses))) {
2140 spin_unlock(&ses->chan_lock);
2143 spin_unlock(&ses->chan_lock);
2145 invalidate_all_cached_dirs(tcon);
2147 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, ses->server,
2153 if (smb3_encryption_required(tcon))
2154 flags |= CIFS_TRANSFORM_REQ;
2156 flags |= CIFS_NO_RSP_BUF;
2158 iov[0].iov_base = (char *)req;
2159 iov[0].iov_len = total_len;
2161 memset(&rqst, 0, sizeof(struct smb_rqst));
2165 rc = cifs_send_recv(xid, ses, ses->server,
2166 &rqst, &resp_buf_type, flags, &rsp_iov);
2167 cifs_small_buf_release(req);
2169 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
2170 trace_smb3_tdis_err(xid, tcon->tid, ses->Suid, rc);
2172 trace_smb3_tdis_done(xid, tcon->tid, ses->Suid);
2178 static struct create_durable *
2179 create_durable_buf(void)
2181 struct create_durable *buf;
2183 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2187 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2188 (struct create_durable, Data));
2189 buf->ccontext.DataLength = cpu_to_le32(16);
2190 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2191 (struct create_durable, Name));
2192 buf->ccontext.NameLength = cpu_to_le16(4);
2193 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
2201 static struct create_durable *
2202 create_reconnect_durable_buf(struct cifs_fid *fid)
2204 struct create_durable *buf;
2206 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2210 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2211 (struct create_durable, Data));
2212 buf->ccontext.DataLength = cpu_to_le32(16);
2213 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2214 (struct create_durable, Name));
2215 buf->ccontext.NameLength = cpu_to_le16(4);
2216 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
2217 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
2218 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
2227 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
2229 struct create_disk_id_rsp *pdisk_id = (struct create_disk_id_rsp *)cc;
2231 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
2232 pdisk_id->DiskFileId, pdisk_id->VolumeId);
2233 buf->IndexNumber = pdisk_id->DiskFileId;
2237 parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
2238 struct create_posix_rsp *posix)
2241 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
2242 u8 *end = beg + le32_to_cpu(cc->DataLength);
2245 memset(posix, 0, sizeof(*posix));
2247 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
2248 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
2249 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
2252 sid_len = posix_info_sid_size(sid, end);
2254 cifs_dbg(VFS, "bad owner sid in posix create response\n");
2257 memcpy(&posix->owner, sid, sid_len);
2259 sid = sid + sid_len;
2260 sid_len = posix_info_sid_size(sid, end);
2262 cifs_dbg(VFS, "bad group sid in posix create response\n");
2265 memcpy(&posix->group, sid, sid_len);
2267 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
2268 posix->nlink, posix->mode, posix->reparse_tag);
2271 int smb2_parse_contexts(struct TCP_Server_Info *server,
2272 struct kvec *rsp_iov,
2273 unsigned int *epoch,
2274 char *lease_key, __u8 *oplock,
2275 struct smb2_file_all_info *buf,
2276 struct create_posix_rsp *posix)
2278 struct smb2_create_rsp *rsp = rsp_iov->iov_base;
2279 struct create_context *cc;
2280 size_t rem, off, len;
2284 static const char smb3_create_tag_posix[] = {
2285 0x93, 0xAD, 0x25, 0x50, 0x9C,
2286 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
2287 0xDE, 0x96, 0x8B, 0xCD, 0x7C
2292 off = le32_to_cpu(rsp->CreateContextsOffset);
2293 rem = le32_to_cpu(rsp->CreateContextsLength);
2294 if (check_add_overflow(off, rem, &len) || len > rsp_iov->iov_len)
2296 cc = (struct create_context *)((u8 *)rsp + off);
2298 /* Initialize inode number to 0 in case no valid data in qfid context */
2300 buf->IndexNumber = 0;
2302 while (rem >= sizeof(*cc)) {
2303 doff = le16_to_cpu(cc->DataOffset);
2304 dlen = le32_to_cpu(cc->DataLength);
2305 if (check_add_overflow(doff, dlen, &len) || len > rem)
2308 noff = le16_to_cpu(cc->NameOffset);
2309 nlen = le16_to_cpu(cc->NameLength);
2310 if (noff + nlen > doff)
2313 name = (char *)cc + noff;
2316 if (!strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4)) {
2317 *oplock = server->ops->parse_lease_buf(cc, epoch,
2320 !strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4)) {
2321 parse_query_id_ctxt(cc, buf);
2325 if (posix && !memcmp(name, smb3_create_tag_posix, 16))
2326 parse_posix_ctxt(cc, buf, posix);
2329 cifs_dbg(FYI, "%s: unhandled context (nlen=%zu dlen=%zu)\n",
2330 __func__, nlen, dlen);
2331 if (IS_ENABLED(CONFIG_CIFS_DEBUG2))
2332 cifs_dump_mem("context data: ", cc, dlen);
2336 off = le32_to_cpu(cc->Next);
2339 if (check_sub_overflow(rem, off, &rem))
2341 cc = (struct create_context *)((u8 *)cc + off);
2344 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2345 *oplock = rsp->OplockLevel;
2351 add_lease_context(struct TCP_Server_Info *server,
2352 struct smb2_create_req *req,
2354 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
2356 unsigned int num = *num_iovec;
2358 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
2359 if (iov[num].iov_base == NULL)
2361 iov[num].iov_len = server->vals->create_lease_size;
2362 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2363 *num_iovec = num + 1;
2367 static struct create_durable_v2 *
2368 create_durable_v2_buf(struct cifs_open_parms *oparms)
2370 struct cifs_fid *pfid = oparms->fid;
2371 struct create_durable_v2 *buf;
2373 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2377 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2378 (struct create_durable_v2, dcontext));
2379 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2380 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2381 (struct create_durable_v2, Name));
2382 buf->ccontext.NameLength = cpu_to_le16(4);
2385 * NB: Handle timeout defaults to 0, which allows server to choose
2386 * (most servers default to 120 seconds) and most clients default to 0.
2387 * This can be overridden at mount ("handletimeout=") if the user wants
2388 * a different persistent (or resilient) handle timeout for all opens
2389 * on a particular SMB3 mount.
2391 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2392 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2393 generate_random_uuid(buf->dcontext.CreateGuid);
2394 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2396 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2404 static struct create_durable_handle_reconnect_v2 *
2405 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2407 struct create_durable_handle_reconnect_v2 *buf;
2409 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2414 buf->ccontext.DataOffset =
2415 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2417 buf->ccontext.DataLength =
2418 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2419 buf->ccontext.NameOffset =
2420 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2422 buf->ccontext.NameLength = cpu_to_le16(4);
2424 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2425 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2426 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2427 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2429 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2438 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2439 struct cifs_open_parms *oparms)
2441 unsigned int num = *num_iovec;
2443 iov[num].iov_base = create_durable_v2_buf(oparms);
2444 if (iov[num].iov_base == NULL)
2446 iov[num].iov_len = sizeof(struct create_durable_v2);
2447 *num_iovec = num + 1;
2452 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2453 struct cifs_open_parms *oparms)
2455 unsigned int num = *num_iovec;
2457 /* indicate that we don't need to relock the file */
2458 oparms->reconnect = false;
2460 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2461 if (iov[num].iov_base == NULL)
2463 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2464 *num_iovec = num + 1;
2469 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2470 struct cifs_open_parms *oparms, bool use_persistent)
2472 unsigned int num = *num_iovec;
2474 if (use_persistent) {
2475 if (oparms->reconnect)
2476 return add_durable_reconnect_v2_context(iov, num_iovec,
2479 return add_durable_v2_context(iov, num_iovec, oparms);
2482 if (oparms->reconnect) {
2483 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2484 /* indicate that we don't need to relock the file */
2485 oparms->reconnect = false;
2487 iov[num].iov_base = create_durable_buf();
2488 if (iov[num].iov_base == NULL)
2490 iov[num].iov_len = sizeof(struct create_durable);
2491 *num_iovec = num + 1;
2495 /* See MS-SMB2 2.2.13.2.7 */
2496 static struct crt_twarp_ctxt *
2497 create_twarp_buf(__u64 timewarp)
2499 struct crt_twarp_ctxt *buf;
2501 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2505 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2506 (struct crt_twarp_ctxt, Timestamp));
2507 buf->ccontext.DataLength = cpu_to_le32(8);
2508 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2509 (struct crt_twarp_ctxt, Name));
2510 buf->ccontext.NameLength = cpu_to_le16(4);
2511 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2516 buf->Timestamp = cpu_to_le64(timewarp);
2520 /* See MS-SMB2 2.2.13.2.7 */
2522 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2524 unsigned int num = *num_iovec;
2526 iov[num].iov_base = create_twarp_buf(timewarp);
2527 if (iov[num].iov_base == NULL)
2529 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2530 *num_iovec = num + 1;
2534 /* See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
2535 static void setup_owner_group_sids(char *buf)
2537 struct owner_group_sids *sids = (struct owner_group_sids *)buf;
2539 /* Populate the user ownership fields S-1-5-88-1 */
2540 sids->owner.Revision = 1;
2541 sids->owner.NumAuth = 3;
2542 sids->owner.Authority[5] = 5;
2543 sids->owner.SubAuthorities[0] = cpu_to_le32(88);
2544 sids->owner.SubAuthorities[1] = cpu_to_le32(1);
2545 sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val);
2547 /* Populate the group ownership fields S-1-5-88-2 */
2548 sids->group.Revision = 1;
2549 sids->group.NumAuth = 3;
2550 sids->group.Authority[5] = 5;
2551 sids->group.SubAuthorities[0] = cpu_to_le32(88);
2552 sids->group.SubAuthorities[1] = cpu_to_le32(2);
2553 sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val);
2555 cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val);
2558 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2559 static struct crt_sd_ctxt *
2560 create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
2562 struct crt_sd_ctxt *buf;
2564 unsigned int acelen, acl_size, ace_count;
2565 unsigned int owner_offset = 0;
2566 unsigned int group_offset = 0;
2567 struct smb3_acl acl = {};
2569 *len = round_up(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 4), 8);
2572 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
2573 *len += sizeof(struct owner_group_sids);
2576 buf = kzalloc(*len, GFP_KERNEL);
2580 ptr = (__u8 *)&buf[1];
2582 /* offset fields are from beginning of security descriptor not of create context */
2583 owner_offset = ptr - (__u8 *)&buf->sd;
2584 buf->sd.OffsetOwner = cpu_to_le32(owner_offset);
2585 group_offset = owner_offset + offsetof(struct owner_group_sids, group);
2586 buf->sd.OffsetGroup = cpu_to_le32(group_offset);
2588 setup_owner_group_sids(ptr);
2589 ptr += sizeof(struct owner_group_sids);
2591 buf->sd.OffsetOwner = 0;
2592 buf->sd.OffsetGroup = 0;
2595 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, sd));
2596 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name));
2597 buf->ccontext.NameLength = cpu_to_le16(4);
2598 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2603 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2606 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2607 * and "DP" ie the DACL is present
2609 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2611 /* offset owner, group and Sbz1 and SACL are all zero */
2612 buf->sd.OffsetDacl = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2613 /* Ship the ACL for now. we will copy it into buf later. */
2615 ptr += sizeof(struct smb3_acl);
2617 /* create one ACE to hold the mode embedded in reserved special SID */
2618 acelen = setup_special_mode_ACE((struct cifs_ace *)ptr, (__u64)mode);
2620 acl_size = acelen + sizeof(struct smb3_acl);
2624 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
2625 acelen = setup_special_user_owner_ACE((struct cifs_ace *)ptr);
2631 /* and one more ACE to allow access for authenticated users */
2632 acelen = setup_authusers_ACE((struct cifs_ace *)ptr);
2637 acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2638 acl.AclSize = cpu_to_le16(acl_size);
2639 acl.AceCount = cpu_to_le16(ace_count);
2640 /* acl.Sbz1 and Sbz2 MBZ so are not set here, but initialized above */
2641 memcpy(aclptr, &acl, sizeof(struct smb3_acl));
2643 buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2644 *len = round_up((unsigned int)(ptr - (__u8 *)buf), 8);
2650 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner)
2652 unsigned int num = *num_iovec;
2653 unsigned int len = 0;
2655 iov[num].iov_base = create_sd_buf(mode, set_owner, &len);
2656 if (iov[num].iov_base == NULL)
2658 iov[num].iov_len = len;
2659 *num_iovec = num + 1;
2663 static struct crt_query_id_ctxt *
2664 create_query_id_buf(void)
2666 struct crt_query_id_ctxt *buf;
2668 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2672 buf->ccontext.DataOffset = cpu_to_le16(0);
2673 buf->ccontext.DataLength = cpu_to_le32(0);
2674 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2675 (struct crt_query_id_ctxt, Name));
2676 buf->ccontext.NameLength = cpu_to_le16(4);
2677 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2685 /* See MS-SMB2 2.2.13.2.9 */
2687 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2689 unsigned int num = *num_iovec;
2691 iov[num].iov_base = create_query_id_buf();
2692 if (iov[num].iov_base == NULL)
2694 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2695 *num_iovec = num + 1;
2700 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2701 const char *treename, const __le16 *path)
2703 int treename_len, path_len;
2704 struct nls_table *cp;
2705 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2710 treename_len = strlen(treename);
2711 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2717 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2719 /* make room for one path separator only if @path isn't empty */
2720 *out_len = treename_len + (path[0] ? 1 : 0) + path_len;
2723 * final path needs to be 8-byte aligned as specified in
2724 * MS-SMB2 2.2.13 SMB2 CREATE Request.
2726 *out_size = round_up(*out_len * sizeof(__le16), 8);
2727 *out_path = kzalloc(*out_size + sizeof(__le16) /* null */, GFP_KERNEL);
2731 cp = load_nls_default();
2732 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2734 /* Do not append the separator if the path is empty */
2735 if (path[0] != cpu_to_le16(0x0000)) {
2736 UniStrcat((wchar_t *)*out_path, (wchar_t *)sep);
2737 UniStrcat((wchar_t *)*out_path, (wchar_t *)path);
2745 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2746 umode_t mode, struct cifs_tcon *tcon,
2747 const char *full_path,
2748 struct cifs_sb_info *cifs_sb)
2750 struct smb_rqst rqst;
2751 struct smb2_create_req *req;
2752 struct smb2_create_rsp *rsp = NULL;
2753 struct cifs_ses *ses = tcon->ses;
2754 struct kvec iov[3]; /* make sure at least one for each open context */
2755 struct kvec rsp_iov = {NULL, 0};
2758 __le16 *copy_path = NULL;
2761 unsigned int n_iov = 2;
2762 __u32 file_attributes = 0;
2763 char *pc_buf = NULL;
2765 unsigned int total_len;
2766 __le16 *utf16_path = NULL;
2767 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2769 cifs_dbg(FYI, "mkdir\n");
2771 /* resource #1: path allocation */
2772 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2776 if (!ses || !server) {
2781 /* resource #2: request */
2782 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2783 (void **) &req, &total_len);
2788 if (smb3_encryption_required(tcon))
2789 flags |= CIFS_TRANSFORM_REQ;
2791 req->ImpersonationLevel = IL_IMPERSONATION;
2792 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2793 /* File attributes ignored on open (used in create though) */
2794 req->FileAttributes = cpu_to_le32(file_attributes);
2795 req->ShareAccess = FILE_SHARE_ALL_LE;
2796 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2797 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2799 iov[0].iov_base = (char *)req;
2800 /* -1 since last byte is buf[0] which is sent below (path) */
2801 iov[0].iov_len = total_len - 1;
2803 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2805 /* [MS-SMB2] 2.2.13 NameOffset:
2806 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2807 * the SMB2 header, the file name includes a prefix that will
2808 * be processed during DFS name normalization as specified in
2809 * section 3.3.5.9. Otherwise, the file name is relative to
2810 * the share that is identified by the TreeId in the SMB2
2813 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2816 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2817 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2819 tcon->tree_name, utf16_path);
2823 req->NameLength = cpu_to_le16(name_len * 2);
2824 uni_path_len = copy_size;
2825 /* free before overwriting resource */
2827 utf16_path = copy_path;
2829 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2830 /* MUST set path len (NameLength) to 0 opening root of share */
2831 req->NameLength = cpu_to_le16(uni_path_len - 2);
2832 if (uni_path_len % 8 != 0) {
2833 copy_size = roundup(uni_path_len, 8);
2834 copy_path = kzalloc(copy_size, GFP_KERNEL);
2839 memcpy((char *)copy_path, (const char *)utf16_path,
2841 uni_path_len = copy_size;
2842 /* free before overwriting resource */
2844 utf16_path = copy_path;
2848 iov[1].iov_len = uni_path_len;
2849 iov[1].iov_base = utf16_path;
2850 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2852 if (tcon->posix_extensions) {
2853 /* resource #3: posix buf */
2854 rc = add_posix_context(iov, &n_iov, mode);
2857 req->CreateContextsOffset = cpu_to_le32(
2858 sizeof(struct smb2_create_req) +
2860 pc_buf = iov[n_iov-1].iov_base;
2864 memset(&rqst, 0, sizeof(struct smb_rqst));
2866 rqst.rq_nvec = n_iov;
2868 /* no need to inc num_remote_opens because we close it just below */
2869 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, full_path, CREATE_NOT_FILE,
2870 FILE_WRITE_ATTRIBUTES);
2871 /* resource #4: response buffer */
2872 rc = cifs_send_recv(xid, ses, server,
2873 &rqst, &resp_buftype, flags, &rsp_iov);
2875 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2876 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2878 FILE_WRITE_ATTRIBUTES, rc);
2879 goto err_free_rsp_buf;
2883 * Although unlikely to be possible for rsp to be null and rc not set,
2884 * adding check below is slightly safer long term (and quiets Coverity
2887 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2894 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
2895 CREATE_NOT_FILE, FILE_WRITE_ATTRIBUTES);
2897 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2899 /* Eventually save off posix specific response info and timestaps */
2902 free_rsp_buf(resp_buftype, rsp);
2905 cifs_small_buf_release(req);
2912 SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2913 struct smb_rqst *rqst, __u8 *oplock,
2914 struct cifs_open_parms *oparms, __le16 *path)
2916 struct smb2_create_req *req;
2917 unsigned int n_iov = 2;
2918 __u32 file_attributes = 0;
2921 unsigned int total_len;
2922 struct kvec *iov = rqst->rq_iov;
2926 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2927 (void **) &req, &total_len);
2931 iov[0].iov_base = (char *)req;
2932 /* -1 since last byte is buf[0] which is sent below (path) */
2933 iov[0].iov_len = total_len - 1;
2935 if (oparms->create_options & CREATE_OPTION_READONLY)
2936 file_attributes |= ATTR_READONLY;
2937 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2938 file_attributes |= ATTR_SYSTEM;
2940 req->ImpersonationLevel = IL_IMPERSONATION;
2941 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2942 /* File attributes ignored on open (used in create though) */
2943 req->FileAttributes = cpu_to_le32(file_attributes);
2944 req->ShareAccess = FILE_SHARE_ALL_LE;
2946 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2947 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2948 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2950 /* [MS-SMB2] 2.2.13 NameOffset:
2951 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2952 * the SMB2 header, the file name includes a prefix that will
2953 * be processed during DFS name normalization as specified in
2954 * section 3.3.5.9. Otherwise, the file name is relative to
2955 * the share that is identified by the TreeId in the SMB2
2958 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2961 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2962 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2964 tcon->tree_name, path);
2967 req->NameLength = cpu_to_le16(name_len * 2);
2968 uni_path_len = copy_size;
2971 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2972 /* MUST set path len (NameLength) to 0 opening root of share */
2973 req->NameLength = cpu_to_le16(uni_path_len - 2);
2974 copy_size = round_up(uni_path_len, 8);
2975 copy_path = kzalloc(copy_size, GFP_KERNEL);
2978 memcpy((char *)copy_path, (const char *)path,
2980 uni_path_len = copy_size;
2984 iov[1].iov_len = uni_path_len;
2985 iov[1].iov_base = path;
2987 if ((!server->oplocks) || (tcon->no_lease))
2988 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2990 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2991 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2992 req->RequestedOplockLevel = *oplock;
2993 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2994 (oparms->create_options & CREATE_NOT_FILE))
2995 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2997 rc = add_lease_context(server, req, iov, &n_iov,
2998 oparms->fid->lease_key, oplock);
3003 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3004 rc = add_durable_context(iov, &n_iov, oparms,
3005 tcon->use_persistent);
3010 if (tcon->posix_extensions) {
3011 rc = add_posix_context(iov, &n_iov, oparms->mode);
3016 if (tcon->snapshot_time) {
3017 cifs_dbg(FYI, "adding snapshot context\n");
3018 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
3023 if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) {
3027 if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
3028 (oparms->mode != ACL_NO_MODE))
3032 oparms->mode = ACL_NO_MODE;
3035 if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
3040 if (set_owner | set_mode) {
3041 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
3042 rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner);
3048 add_query_id_context(iov, &n_iov);
3052 * We have create contexts behind iov[1] (the file
3053 * name), point at them from the main create request
3055 req->CreateContextsOffset = cpu_to_le32(
3056 sizeof(struct smb2_create_req) +
3058 req->CreateContextsLength = 0;
3060 for (unsigned int i = 2; i < (n_iov-1); i++) {
3061 struct kvec *v = &iov[i];
3062 size_t len = v->iov_len;
3063 struct create_context *cctx =
3064 (struct create_context *)v->iov_base;
3066 cctx->Next = cpu_to_le32(len);
3067 le32_add_cpu(&req->CreateContextsLength, len);
3069 le32_add_cpu(&req->CreateContextsLength,
3070 iov[n_iov-1].iov_len);
3073 rqst->rq_nvec = n_iov;
3077 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
3078 * All other vectors are freed by kfree().
3081 SMB2_open_free(struct smb_rqst *rqst)
3085 if (rqst && rqst->rq_iov) {
3086 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
3087 for (i = 1; i < rqst->rq_nvec; i++)
3088 if (rqst->rq_iov[i].iov_base != smb2_padding)
3089 kfree(rqst->rq_iov[i].iov_base);
3094 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
3095 __u8 *oplock, struct smb2_file_all_info *buf,
3096 struct create_posix_rsp *posix,
3097 struct kvec *err_iov, int *buftype)
3099 struct smb_rqst rqst;
3100 struct smb2_create_rsp *rsp = NULL;
3101 struct cifs_tcon *tcon = oparms->tcon;
3102 struct cifs_ses *ses = tcon->ses;
3103 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3104 struct kvec iov[SMB2_CREATE_IOV_SIZE];
3105 struct kvec rsp_iov = {NULL, 0};
3106 int resp_buftype = CIFS_NO_BUFFER;
3110 cifs_dbg(FYI, "create/open\n");
3111 if (!ses || !server)
3114 if (smb3_encryption_required(tcon))
3115 flags |= CIFS_TRANSFORM_REQ;
3117 memset(&rqst, 0, sizeof(struct smb_rqst));
3118 memset(&iov, 0, sizeof(iov));
3120 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
3122 rc = SMB2_open_init(tcon, server,
3123 &rqst, oplock, oparms, path);
3127 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid, oparms->path,
3128 oparms->create_options, oparms->desired_access);
3130 rc = cifs_send_recv(xid, ses, server,
3131 &rqst, &resp_buftype, flags,
3133 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
3136 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
3137 if (err_iov && rsp) {
3139 *buftype = resp_buftype;
3140 resp_buftype = CIFS_NO_BUFFER;
3143 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
3144 oparms->create_options, oparms->desired_access, rc);
3145 if (rc == -EREMCHG) {
3146 pr_warn_once("server share %s deleted\n",
3148 tcon->need_reconnect = true;
3151 } else if (rsp == NULL) /* unlikely to happen, but safer to check */
3154 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
3155 oparms->create_options, oparms->desired_access);
3157 atomic_inc(&tcon->num_remote_opens);
3158 oparms->fid->persistent_fid = rsp->PersistentFileId;
3159 oparms->fid->volatile_fid = rsp->VolatileFileId;
3160 oparms->fid->access = oparms->desired_access;
3161 #ifdef CONFIG_CIFS_DEBUG2
3162 oparms->fid->mid = le64_to_cpu(rsp->hdr.MessageId);
3163 #endif /* CIFS_DEBUG2 */
3166 buf->CreationTime = rsp->CreationTime;
3167 buf->LastAccessTime = rsp->LastAccessTime;
3168 buf->LastWriteTime = rsp->LastWriteTime;
3169 buf->ChangeTime = rsp->ChangeTime;
3170 buf->AllocationSize = rsp->AllocationSize;
3171 buf->EndOfFile = rsp->EndofFile;
3172 buf->Attributes = rsp->FileAttributes;
3173 buf->NumberOfLinks = cpu_to_le32(1);
3174 buf->DeletePending = 0;
3178 rc = smb2_parse_contexts(server, &rsp_iov, &oparms->fid->epoch,
3179 oparms->fid->lease_key, oplock, buf, posix);
3181 SMB2_open_free(&rqst);
3182 free_rsp_buf(resp_buftype, rsp);
3187 SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3188 struct smb_rqst *rqst,
3189 u64 persistent_fid, u64 volatile_fid, u32 opcode,
3190 char *in_data, u32 indatalen,
3191 __u32 max_response_size)
3193 struct smb2_ioctl_req *req;
3194 struct kvec *iov = rqst->rq_iov;
3195 unsigned int total_len;
3199 rc = smb2_ioctl_req_init(opcode, tcon, server,
3200 (void **) &req, &total_len);
3206 * indatalen is usually small at a couple of bytes max, so
3207 * just allocate through generic pool
3209 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
3211 cifs_small_buf_release(req);
3216 req->CtlCode = cpu_to_le32(opcode);
3217 req->PersistentFileId = persistent_fid;
3218 req->VolatileFileId = volatile_fid;
3220 iov[0].iov_base = (char *)req;
3222 * If no input data, the size of ioctl struct in
3223 * protocol spec still includes a 1 byte data buffer,
3224 * but if input data passed to ioctl, we do not
3225 * want to double count this, so we do not send
3226 * the dummy one byte of data in iovec[0] if sending
3227 * input data (in iovec[1]).
3230 req->InputCount = cpu_to_le32(indatalen);
3231 /* do not set InputOffset if no input data */
3233 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
3235 iov[0].iov_len = total_len - 1;
3236 iov[1].iov_base = in_data_buf;
3237 iov[1].iov_len = indatalen;
3240 iov[0].iov_len = total_len;
3243 req->OutputOffset = 0;
3244 req->OutputCount = 0; /* MBZ */
3247 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
3248 * We Could increase default MaxOutputResponse, but that could require
3249 * more credits. Windows typically sets this smaller, but for some
3250 * ioctls it may be useful to allow server to send more. No point
3251 * limiting what the server can send as long as fits in one credit
3252 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
3253 * to increase this limit up in the future.
3254 * Note that for snapshot queries that servers like Azure expect that
3255 * the first query be minimal size (and just used to get the number/size
3256 * of previous versions) so response size must be specified as EXACTLY
3257 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
3258 * of eight bytes. Currently that is the only case where we set max
3259 * response size smaller.
3261 req->MaxOutputResponse = cpu_to_le32(max_response_size);
3262 req->hdr.CreditCharge =
3263 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
3264 SMB2_MAX_BUFFER_SIZE));
3265 /* always an FSCTL (for now) */
3266 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
3268 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
3269 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
3270 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
3276 SMB2_ioctl_free(struct smb_rqst *rqst)
3280 if (rqst && rqst->rq_iov) {
3281 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3282 for (i = 1; i < rqst->rq_nvec; i++)
3283 if (rqst->rq_iov[i].iov_base != smb2_padding)
3284 kfree(rqst->rq_iov[i].iov_base);
3290 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
3293 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3294 u64 volatile_fid, u32 opcode, char *in_data, u32 indatalen,
3295 u32 max_out_data_len, char **out_data,
3296 u32 *plen /* returned data len */)
3298 struct smb_rqst rqst;
3299 struct smb2_ioctl_rsp *rsp = NULL;
3300 struct cifs_ses *ses;
3301 struct TCP_Server_Info *server;
3302 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
3303 struct kvec rsp_iov = {NULL, 0};
3304 int resp_buftype = CIFS_NO_BUFFER;
3308 cifs_dbg(FYI, "SMB2 IOCTL\n");
3310 if (out_data != NULL)
3313 /* zero out returned data len, in case of error */
3324 server = cifs_pick_channel(ses);
3328 if (smb3_encryption_required(tcon))
3329 flags |= CIFS_TRANSFORM_REQ;
3331 memset(&rqst, 0, sizeof(struct smb_rqst));
3332 memset(&iov, 0, sizeof(iov));
3334 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
3336 rc = SMB2_ioctl_init(tcon, server,
3337 &rqst, persistent_fid, volatile_fid, opcode,
3338 in_data, indatalen, max_out_data_len);
3342 rc = cifs_send_recv(xid, ses, server,
3343 &rqst, &resp_buftype, flags,
3345 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
3348 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3349 ses->Suid, 0, opcode, rc);
3351 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
3352 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3354 } else if (rc == -EINVAL) {
3355 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3356 (opcode != FSCTL_SRV_COPYCHUNK)) {
3357 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3360 } else if (rc == -E2BIG) {
3361 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3362 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3367 /* check if caller wants to look at return data or just return rc */
3368 if ((plen == NULL) || (out_data == NULL))
3372 * Although unlikely to be possible for rsp to be null and rc not set,
3373 * adding check below is slightly safer long term (and quiets Coverity
3381 *plen = le32_to_cpu(rsp->OutputCount);
3383 /* We check for obvious errors in the output buffer length and offset */
3385 goto ioctl_exit; /* server returned no data */
3386 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
3387 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
3393 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
3394 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
3395 le32_to_cpu(rsp->OutputOffset));
3401 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3403 if (*out_data == NULL) {
3409 SMB2_ioctl_free(&rqst);
3410 free_rsp_buf(resp_buftype, rsp);
3415 * Individual callers to ioctl worker function follow
3419 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3420 u64 persistent_fid, u64 volatile_fid)
3423 struct compress_ioctl fsctl_input;
3424 char *ret_data = NULL;
3426 fsctl_input.CompressionState =
3427 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3429 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3430 FSCTL_SET_COMPRESSION,
3431 (char *)&fsctl_input /* data input */,
3432 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3433 &ret_data /* out data */, NULL);
3435 cifs_dbg(FYI, "set compression rc %d\n", rc);
3441 SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3442 struct smb_rqst *rqst,
3443 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
3445 struct smb2_close_req *req;
3446 struct kvec *iov = rqst->rq_iov;
3447 unsigned int total_len;
3450 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
3451 (void **) &req, &total_len);
3455 req->PersistentFileId = persistent_fid;
3456 req->VolatileFileId = volatile_fid;
3458 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3461 iov[0].iov_base = (char *)req;
3462 iov[0].iov_len = total_len;
3468 SMB2_close_free(struct smb_rqst *rqst)
3470 if (rqst && rqst->rq_iov)
3471 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3475 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3476 u64 persistent_fid, u64 volatile_fid,
3477 struct smb2_file_network_open_info *pbuf)
3479 struct smb_rqst rqst;
3480 struct smb2_close_rsp *rsp = NULL;
3481 struct cifs_ses *ses = tcon->ses;
3482 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3484 struct kvec rsp_iov;
3485 int resp_buftype = CIFS_NO_BUFFER;
3488 bool query_attrs = false;
3490 cifs_dbg(FYI, "Close\n");
3492 if (!ses || !server)
3495 if (smb3_encryption_required(tcon))
3496 flags |= CIFS_TRANSFORM_REQ;
3498 memset(&rqst, 0, sizeof(struct smb_rqst));
3499 memset(&iov, 0, sizeof(iov));
3503 /* check if need to ask server to return timestamps in close response */
3507 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3508 rc = SMB2_close_init(tcon, server,
3509 &rqst, persistent_fid, volatile_fid,
3514 rc = cifs_send_recv(xid, ses, server,
3515 &rqst, &resp_buftype, flags, &rsp_iov);
3516 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
3519 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
3520 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3524 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3527 memcpy(&pbuf->network_open_info,
3528 &rsp->network_open_info,
3529 sizeof(pbuf->network_open_info));
3532 atomic_dec(&tcon->num_remote_opens);
3534 SMB2_close_free(&rqst);
3535 free_rsp_buf(resp_buftype, rsp);
3537 /* retry close in a worker thread if this one is interrupted */
3538 if (is_interrupt_error(rc)) {
3541 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3544 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3545 persistent_fid, tmp_rc);
3551 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3552 u64 persistent_fid, u64 volatile_fid)
3554 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3558 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3559 struct kvec *iov, unsigned int min_buf_size)
3561 unsigned int smb_len = iov->iov_len;
3562 char *end_of_smb = smb_len + (char *)iov->iov_base;
3563 char *begin_of_buf = offset + (char *)iov->iov_base;
3564 char *end_of_buf = begin_of_buf + buffer_length;
3567 if (buffer_length < min_buf_size) {
3568 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3569 buffer_length, min_buf_size);
3573 /* check if beyond RFC1001 maximum length */
3574 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3575 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3576 buffer_length, smb_len);
3580 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3581 cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
3589 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3590 * Caller must free buffer.
3593 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3594 struct kvec *iov, unsigned int minbufsize,
3597 char *begin_of_buf = offset + (char *)iov->iov_base;
3603 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3607 memcpy(data, begin_of_buf, minbufsize);
3613 SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3614 struct smb_rqst *rqst,
3615 u64 persistent_fid, u64 volatile_fid,
3616 u8 info_class, u8 info_type, u32 additional_info,
3617 size_t output_len, size_t input_len, void *input)
3619 struct smb2_query_info_req *req;
3620 struct kvec *iov = rqst->rq_iov;
3621 unsigned int total_len;
3625 if (unlikely(check_add_overflow(input_len, sizeof(*req), &len) ||
3626 len > CIFSMaxBufSize))
3629 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
3630 (void **) &req, &total_len);
3634 req->InfoType = info_type;
3635 req->FileInfoClass = info_class;
3636 req->PersistentFileId = persistent_fid;
3637 req->VolatileFileId = volatile_fid;
3638 req->AdditionalInformation = cpu_to_le32(additional_info);
3640 req->OutputBufferLength = cpu_to_le32(output_len);
3642 req->InputBufferLength = cpu_to_le32(input_len);
3643 /* total_len for smb query request never close to le16 max */
3644 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3645 memcpy(req->Buffer, input, input_len);
3648 iov[0].iov_base = (char *)req;
3650 iov[0].iov_len = len;
3655 SMB2_query_info_free(struct smb_rqst *rqst)
3657 if (rqst && rqst->rq_iov)
3658 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
3662 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3663 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3664 u32 additional_info, size_t output_len, size_t min_len, void **data,
3667 struct smb_rqst rqst;
3668 struct smb2_query_info_rsp *rsp = NULL;
3670 struct kvec rsp_iov;
3672 int resp_buftype = CIFS_NO_BUFFER;
3673 struct cifs_ses *ses = tcon->ses;
3674 struct TCP_Server_Info *server;
3676 bool allocated = false;
3678 cifs_dbg(FYI, "Query Info\n");
3682 server = cifs_pick_channel(ses);
3686 if (smb3_encryption_required(tcon))
3687 flags |= CIFS_TRANSFORM_REQ;
3689 memset(&rqst, 0, sizeof(struct smb_rqst));
3690 memset(&iov, 0, sizeof(iov));
3694 rc = SMB2_query_info_init(tcon, server,
3695 &rqst, persistent_fid, volatile_fid,
3696 info_class, info_type, additional_info,
3697 output_len, 0, NULL);
3701 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3702 ses->Suid, info_class, (__u32)info_type);
3704 rc = cifs_send_recv(xid, ses, server,
3705 &rqst, &resp_buftype, flags, &rsp_iov);
3706 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3709 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3710 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3711 ses->Suid, info_class, (__u32)info_type, rc);
3715 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3716 ses->Suid, info_class, (__u32)info_type);
3719 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3721 *data = kmalloc(*dlen, GFP_KERNEL);
3724 "Error %d allocating memory for acl\n",
3734 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3735 le32_to_cpu(rsp->OutputBufferLength),
3736 &rsp_iov, dlen ? *dlen : min_len, *data);
3737 if (rc && allocated) {
3744 SMB2_query_info_free(&rqst);
3745 free_rsp_buf(resp_buftype, rsp);
3749 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3750 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3752 return query_info(xid, tcon, persistent_fid, volatile_fid,
3753 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3754 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3755 sizeof(struct smb2_file_all_info), (void **)&data,
3760 /* currently unused, as now we are doing compounding instead (see smb311_posix_query_path_info) */
3762 SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3763 u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen)
3765 size_t output_len = sizeof(struct smb311_posix_qinfo *) +
3766 (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2);
3769 return query_info(xid, tcon, persistent_fid, volatile_fid,
3770 SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0,
3771 output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen);
3772 /* Note caller must free "data" (passed in above). It may be allocated in query_info call */
3777 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3778 u64 persistent_fid, u64 volatile_fid,
3779 void **data, u32 *plen, u32 extra_info)
3781 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
3785 return query_info(xid, tcon, persistent_fid, volatile_fid,
3786 0, SMB2_O_INFO_SECURITY, additional_info,
3787 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3791 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3792 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3794 return query_info(xid, tcon, persistent_fid, volatile_fid,
3795 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3796 sizeof(struct smb2_file_internal_info),
3797 sizeof(struct smb2_file_internal_info),
3798 (void **)&uniqueid, NULL);
3802 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3803 * See MS-SMB2 2.2.35 and 2.2.36
3807 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3808 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3809 u64 persistent_fid, u64 volatile_fid,
3810 u32 completion_filter, bool watch_tree)
3812 struct smb2_change_notify_req *req;
3813 struct kvec *iov = rqst->rq_iov;
3814 unsigned int total_len;
3817 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
3818 (void **) &req, &total_len);
3822 req->PersistentFileId = persistent_fid;
3823 req->VolatileFileId = volatile_fid;
3824 /* See note 354 of MS-SMB2, 64K max */
3825 req->OutputBufferLength =
3826 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3827 req->CompletionFilter = cpu_to_le32(completion_filter);
3829 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3833 iov[0].iov_base = (char *)req;
3834 iov[0].iov_len = total_len;
3840 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3841 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3842 u32 completion_filter, u32 max_out_data_len, char **out_data,
3843 u32 *plen /* returned data len */)
3845 struct cifs_ses *ses = tcon->ses;
3846 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3847 struct smb_rqst rqst;
3848 struct smb2_change_notify_rsp *smb_rsp;
3850 struct kvec rsp_iov = {NULL, 0};
3851 int resp_buftype = CIFS_NO_BUFFER;
3855 cifs_dbg(FYI, "change notify\n");
3856 if (!ses || !server)
3859 if (smb3_encryption_required(tcon))
3860 flags |= CIFS_TRANSFORM_REQ;
3862 memset(&rqst, 0, sizeof(struct smb_rqst));
3863 memset(&iov, 0, sizeof(iov));
3870 rc = SMB2_notify_init(xid, &rqst, tcon, server,
3871 persistent_fid, volatile_fid,
3872 completion_filter, watch_tree);
3876 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3877 (u8)watch_tree, completion_filter);
3878 rc = cifs_send_recv(xid, ses, server,
3879 &rqst, &resp_buftype, flags, &rsp_iov);
3882 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3883 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3884 (u8)watch_tree, completion_filter, rc);
3886 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3887 ses->Suid, (u8)watch_tree, completion_filter);
3888 /* validate that notify information is plausible */
3889 if ((rsp_iov.iov_base == NULL) ||
3890 (rsp_iov.iov_len < sizeof(struct smb2_change_notify_rsp) + 1))
3893 smb_rsp = (struct smb2_change_notify_rsp *)rsp_iov.iov_base;
3895 smb2_validate_iov(le16_to_cpu(smb_rsp->OutputBufferOffset),
3896 le32_to_cpu(smb_rsp->OutputBufferLength), &rsp_iov,
3897 sizeof(struct file_notify_information));
3899 *out_data = kmemdup((char *)smb_rsp + le16_to_cpu(smb_rsp->OutputBufferOffset),
3900 le32_to_cpu(smb_rsp->OutputBufferLength), GFP_KERNEL);
3901 if (*out_data == NULL) {
3905 *plen = le32_to_cpu(smb_rsp->OutputBufferLength);
3910 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3911 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3918 * This is a no-op for now. We're not really interested in the reply, but
3919 * rather in the fact that the server sent one and that server->lstrp
3922 * FIXME: maybe we should consider checking that the reply matches request?
3925 smb2_echo_callback(struct mid_q_entry *mid)
3927 struct TCP_Server_Info *server = mid->callback_data;
3928 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3929 struct cifs_credits credits = { .value = 0, .instance = 0 };
3931 if (mid->mid_state == MID_RESPONSE_RECEIVED
3932 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3933 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
3934 credits.instance = server->reconnect_instance;
3938 add_credits(server, &credits, CIFS_ECHO_OP);
3941 void smb2_reconnect_server(struct work_struct *work)
3943 struct TCP_Server_Info *server = container_of(work,
3944 struct TCP_Server_Info, reconnect.work);
3945 struct TCP_Server_Info *pserver;
3946 struct cifs_ses *ses, *ses2;
3947 struct cifs_tcon *tcon, *tcon2;
3948 struct list_head tmp_list, tmp_ses_list;
3949 bool tcon_exist = false, ses_exist = false;
3950 bool tcon_selected = false;
3952 bool resched = false;
3954 /* first check if ref count has reached 0, if not inc ref count */
3955 spin_lock(&cifs_tcp_ses_lock);
3956 if (!server->srv_count) {
3957 spin_unlock(&cifs_tcp_ses_lock);
3960 server->srv_count++;
3961 spin_unlock(&cifs_tcp_ses_lock);
3963 /* If server is a channel, select the primary channel */
3964 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
3966 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3967 mutex_lock(&pserver->reconnect_mutex);
3969 /* if the server is marked for termination, drop the ref count here */
3970 if (server->terminate) {
3971 cifs_put_tcp_session(server, true);
3972 mutex_unlock(&pserver->reconnect_mutex);
3976 INIT_LIST_HEAD(&tmp_list);
3977 INIT_LIST_HEAD(&tmp_ses_list);
3978 cifs_dbg(FYI, "Reconnecting tcons and channels\n");
3980 spin_lock(&cifs_tcp_ses_lock);
3981 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
3982 spin_lock(&ses->ses_lock);
3983 if (ses->ses_status == SES_EXITING) {
3984 spin_unlock(&ses->ses_lock);
3987 spin_unlock(&ses->ses_lock);
3989 tcon_selected = false;
3991 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3992 if (tcon->need_reconnect || tcon->need_reopen_files) {
3994 list_add_tail(&tcon->rlist, &tmp_list);
3995 tcon_selected = tcon_exist = true;
3999 * IPC has the same lifetime as its session and uses its
4002 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
4003 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
4004 tcon_selected = tcon_exist = true;
4005 cifs_smb_ses_inc_refcount(ses);
4008 * handle the case where channel needs to reconnect
4009 * binding session, but tcon is healthy (some other channel
4012 spin_lock(&ses->chan_lock);
4013 if (!tcon_selected && cifs_chan_needs_reconnect(ses, server)) {
4014 list_add_tail(&ses->rlist, &tmp_ses_list);
4016 cifs_smb_ses_inc_refcount(ses);
4018 spin_unlock(&ses->chan_lock);
4020 spin_unlock(&cifs_tcp_ses_lock);
4022 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
4023 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server, true);
4025 cifs_reopen_persistent_handles(tcon);
4028 list_del_init(&tcon->rlist);
4030 cifs_put_smb_ses(tcon->ses);
4032 cifs_put_tcon(tcon);
4038 /* allocate a dummy tcon struct used for reconnect */
4039 tcon = tcon_info_alloc(false);
4042 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
4043 list_del_init(&ses->rlist);
4044 cifs_put_smb_ses(ses);
4049 tcon->status = TID_GOOD;
4050 tcon->retry = false;
4051 tcon->need_reconnect = false;
4053 /* now reconnect sessions for necessary channels */
4054 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
4056 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server, true);
4059 list_del_init(&ses->rlist);
4060 cifs_put_smb_ses(ses);
4065 cifs_dbg(FYI, "Reconnecting tcons and channels finished\n");
4067 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
4068 mutex_unlock(&pserver->reconnect_mutex);
4070 /* now we can safely release srv struct */
4071 cifs_put_tcp_session(server, true);
4075 SMB2_echo(struct TCP_Server_Info *server)
4077 struct smb2_echo_req *req;
4080 struct smb_rqst rqst = { .rq_iov = iov,
4082 unsigned int total_len;
4084 cifs_dbg(FYI, "In echo request for conn_id %lld\n", server->conn_id);
4086 spin_lock(&server->srv_lock);
4087 if (server->ops->need_neg &&
4088 server->ops->need_neg(server)) {
4089 spin_unlock(&server->srv_lock);
4090 /* No need to send echo on newly established connections */
4091 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
4094 spin_unlock(&server->srv_lock);
4096 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
4097 (void **)&req, &total_len);
4101 req->hdr.CreditRequest = cpu_to_le16(1);
4103 iov[0].iov_len = total_len;
4104 iov[0].iov_base = (char *)req;
4106 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
4107 server, CIFS_ECHO_OP, NULL);
4109 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
4111 cifs_small_buf_release(req);
4116 SMB2_flush_free(struct smb_rqst *rqst)
4118 if (rqst && rqst->rq_iov)
4119 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4123 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
4124 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
4125 u64 persistent_fid, u64 volatile_fid)
4127 struct smb2_flush_req *req;
4128 struct kvec *iov = rqst->rq_iov;
4129 unsigned int total_len;
4132 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
4133 (void **) &req, &total_len);
4137 req->PersistentFileId = persistent_fid;
4138 req->VolatileFileId = volatile_fid;
4140 iov[0].iov_base = (char *)req;
4141 iov[0].iov_len = total_len;
4147 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4150 struct cifs_ses *ses = tcon->ses;
4151 struct smb_rqst rqst;
4153 struct kvec rsp_iov = {NULL, 0};
4154 struct TCP_Server_Info *server = cifs_pick_channel(ses);
4155 int resp_buftype = CIFS_NO_BUFFER;
4159 cifs_dbg(FYI, "flush\n");
4160 if (!ses || !(ses->server))
4163 if (smb3_encryption_required(tcon))
4164 flags |= CIFS_TRANSFORM_REQ;
4166 memset(&rqst, 0, sizeof(struct smb_rqst));
4167 memset(&iov, 0, sizeof(iov));
4171 rc = SMB2_flush_init(xid, &rqst, tcon, server,
4172 persistent_fid, volatile_fid);
4176 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
4177 rc = cifs_send_recv(xid, ses, server,
4178 &rqst, &resp_buftype, flags, &rsp_iov);
4181 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
4182 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
4185 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
4189 SMB2_flush_free(&rqst);
4190 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4194 #ifdef CONFIG_CIFS_SMB_DIRECT
4195 static inline bool smb3_use_rdma_offload(struct cifs_io_parms *io_parms)
4197 struct TCP_Server_Info *server = io_parms->server;
4198 struct cifs_tcon *tcon = io_parms->tcon;
4200 /* we can only offload if we're connected */
4201 if (!server || !tcon)
4204 /* we can only offload on an rdma connection */
4205 if (!server->rdma || !server->smbd_conn)
4208 /* we don't support signed offload yet */
4212 /* we don't support encrypted offload yet */
4213 if (smb3_encryption_required(tcon))
4216 /* offload also has its overhead, so only do it if desired */
4217 if (io_parms->length < server->smbd_conn->rdma_readwrite_threshold)
4222 #endif /* CONFIG_CIFS_SMB_DIRECT */
4225 * To form a chain of read requests, any read requests after the first should
4226 * have the end_of_chain boolean set to true.
4229 smb2_new_read_req(void **buf, unsigned int *total_len,
4230 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
4231 unsigned int remaining_bytes, int request_type)
4234 struct smb2_read_req *req = NULL;
4235 struct smb2_hdr *shdr;
4236 struct TCP_Server_Info *server = io_parms->server;
4238 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
4239 (void **) &req, total_len);
4244 return -ECONNABORTED;
4247 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4249 req->PersistentFileId = io_parms->persistent_fid;
4250 req->VolatileFileId = io_parms->volatile_fid;
4251 req->ReadChannelInfoOffset = 0; /* reserved */
4252 req->ReadChannelInfoLength = 0; /* reserved */
4253 req->Channel = 0; /* reserved */
4254 req->MinimumCount = 0;
4255 req->Length = cpu_to_le32(io_parms->length);
4256 req->Offset = cpu_to_le64(io_parms->offset);
4258 trace_smb3_read_enter(0 /* xid */,
4259 io_parms->persistent_fid,
4260 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4261 io_parms->offset, io_parms->length);
4262 #ifdef CONFIG_CIFS_SMB_DIRECT
4264 * If we want to do a RDMA write, fill in and append
4265 * smbd_buffer_descriptor_v1 to the end of read request
4267 if (smb3_use_rdma_offload(io_parms)) {
4268 struct smbd_buffer_descriptor_v1 *v1;
4269 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4271 rdata->mr = smbd_register_mr(server->smbd_conn, &rdata->iter,
4272 true, need_invalidate);
4276 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4277 if (need_invalidate)
4278 req->Channel = SMB2_CHANNEL_RDMA_V1;
4279 req->ReadChannelInfoOffset =
4280 cpu_to_le16(offsetof(struct smb2_read_req, Buffer));
4281 req->ReadChannelInfoLength =
4282 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4283 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4284 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
4285 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
4286 v1->length = cpu_to_le32(rdata->mr->mr->length);
4288 *total_len += sizeof(*v1) - 1;
4291 if (request_type & CHAINED_REQUEST) {
4292 if (!(request_type & END_OF_CHAIN)) {
4293 /* next 8-byte aligned request */
4294 *total_len = ALIGN(*total_len, 8);
4295 shdr->NextCommand = cpu_to_le32(*total_len);
4296 } else /* END_OF_CHAIN */
4297 shdr->NextCommand = 0;
4298 if (request_type & RELATED_REQUEST) {
4299 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
4301 * Related requests use info from previous read request
4304 shdr->SessionId = cpu_to_le64(0xFFFFFFFFFFFFFFFF);
4305 shdr->Id.SyncId.TreeId = cpu_to_le32(0xFFFFFFFF);
4306 req->PersistentFileId = (u64)-1;
4307 req->VolatileFileId = (u64)-1;
4310 if (remaining_bytes > io_parms->length)
4311 req->RemainingBytes = cpu_to_le32(remaining_bytes);
4313 req->RemainingBytes = 0;
4320 smb2_readv_callback(struct mid_q_entry *mid)
4322 struct cifs_readdata *rdata = mid->callback_data;
4323 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
4324 struct TCP_Server_Info *server = rdata->server;
4325 struct smb2_hdr *shdr =
4326 (struct smb2_hdr *)rdata->iov[0].iov_base;
4327 struct cifs_credits credits = { .value = 0, .instance = 0 };
4328 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1], .rq_nvec = 1 };
4330 if (rdata->got_bytes) {
4331 rqst.rq_iter = rdata->iter;
4332 rqst.rq_iter_size = iov_iter_count(&rdata->iter);
4335 WARN_ONCE(rdata->server != mid->server,
4336 "rdata server %p != mid server %p",
4337 rdata->server, mid->server);
4339 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
4340 __func__, mid->mid, mid->mid_state, rdata->result,
4343 switch (mid->mid_state) {
4344 case MID_RESPONSE_RECEIVED:
4345 credits.value = le16_to_cpu(shdr->CreditRequest);
4346 credits.instance = server->reconnect_instance;
4347 /* result already set, check signature */
4348 if (server->sign && !mid->decrypted) {
4351 iov_iter_revert(&rqst.rq_iter, rdata->got_bytes);
4352 iov_iter_truncate(&rqst.rq_iter, rdata->got_bytes);
4353 rc = smb2_verify_signature(&rqst, server);
4355 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
4358 /* FIXME: should this be counted toward the initiating task? */
4359 task_io_account_read(rdata->got_bytes);
4360 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4362 case MID_REQUEST_SUBMITTED:
4363 case MID_RETRY_NEEDED:
4364 rdata->result = -EAGAIN;
4365 if (server->sign && rdata->got_bytes)
4366 /* reset bytes number since we can not check a sign */
4367 rdata->got_bytes = 0;
4368 /* FIXME: should this be counted toward the initiating task? */
4369 task_io_account_read(rdata->got_bytes);
4370 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4372 case MID_RESPONSE_MALFORMED:
4373 credits.value = le16_to_cpu(shdr->CreditRequest);
4374 credits.instance = server->reconnect_instance;
4377 rdata->result = -EIO;
4379 #ifdef CONFIG_CIFS_SMB_DIRECT
4381 * If this rdata has a memmory registered, the MR can be freed
4382 * MR needs to be freed as soon as I/O finishes to prevent deadlock
4383 * because they have limited number and are used for future I/Os
4386 smbd_deregister_mr(rdata->mr);
4390 if (rdata->result && rdata->result != -ENODATA) {
4391 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
4392 trace_smb3_read_err(0 /* xid */,
4393 rdata->cfile->fid.persistent_fid,
4394 tcon->tid, tcon->ses->Suid, rdata->offset,
4395 rdata->bytes, rdata->result);
4397 trace_smb3_read_done(0 /* xid */,
4398 rdata->cfile->fid.persistent_fid,
4399 tcon->tid, tcon->ses->Suid,
4400 rdata->offset, rdata->got_bytes);
4402 queue_work(cifsiod_wq, &rdata->work);
4404 add_credits(server, &credits, 0);
4407 /* smb2_async_readv - send an async read, and set up mid to handle result */
4409 smb2_async_readv(struct cifs_readdata *rdata)
4413 struct smb2_hdr *shdr;
4414 struct cifs_io_parms io_parms;
4415 struct smb_rqst rqst = { .rq_iov = rdata->iov,
4417 struct TCP_Server_Info *server;
4418 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
4419 unsigned int total_len;
4422 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
4423 __func__, rdata->offset, rdata->bytes);
4426 rdata->server = cifs_pick_channel(tcon->ses);
4428 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
4429 io_parms.server = server = rdata->server;
4430 io_parms.offset = rdata->offset;
4431 io_parms.length = rdata->bytes;
4432 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
4433 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
4434 io_parms.pid = rdata->pid;
4436 rc = smb2_new_read_req(
4437 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
4441 if (smb3_encryption_required(io_parms.tcon))
4442 flags |= CIFS_TRANSFORM_REQ;
4444 rdata->iov[0].iov_base = buf;
4445 rdata->iov[0].iov_len = total_len;
4447 shdr = (struct smb2_hdr *)buf;
4449 if (rdata->credits.value > 0) {
4450 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
4451 SMB2_MAX_BUFFER_SIZE));
4452 credit_request = le16_to_cpu(shdr->CreditCharge) + 8;
4453 if (server->credits >= server->max_credits)
4454 shdr->CreditRequest = cpu_to_le16(0);
4456 shdr->CreditRequest = cpu_to_le16(
4457 min_t(int, server->max_credits -
4458 server->credits, credit_request));
4460 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4462 goto async_readv_out;
4464 flags |= CIFS_HAS_CREDITS;
4467 kref_get(&rdata->refcount);
4468 rc = cifs_call_async(server, &rqst,
4469 cifs_readv_receive, smb2_readv_callback,
4470 smb3_handle_read_data, rdata, flags,
4473 kref_put(&rdata->refcount, cifs_readdata_release);
4474 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
4475 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
4477 io_parms.tcon->ses->Suid,
4478 io_parms.offset, io_parms.length, rc);
4482 cifs_small_buf_release(buf);
4487 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
4488 unsigned int *nbytes, char **buf, int *buf_type)
4490 struct smb_rqst rqst;
4491 int resp_buftype, rc;
4492 struct smb2_read_req *req = NULL;
4493 struct smb2_read_rsp *rsp = NULL;
4495 struct kvec rsp_iov;
4496 unsigned int total_len;
4497 int flags = CIFS_LOG_ERROR;
4498 struct cifs_ses *ses = io_parms->tcon->ses;
4500 if (!io_parms->server)
4501 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4504 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
4508 if (smb3_encryption_required(io_parms->tcon))
4509 flags |= CIFS_TRANSFORM_REQ;
4511 iov[0].iov_base = (char *)req;
4512 iov[0].iov_len = total_len;
4514 memset(&rqst, 0, sizeof(struct smb_rqst));
4518 rc = cifs_send_recv(xid, ses, io_parms->server,
4519 &rqst, &resp_buftype, flags, &rsp_iov);
4520 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
4523 if (rc != -ENODATA) {
4524 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4525 cifs_dbg(VFS, "Send error in read = %d\n", rc);
4526 trace_smb3_read_err(xid,
4527 req->PersistentFileId,
4528 io_parms->tcon->tid, ses->Suid,
4529 io_parms->offset, io_parms->length,
4532 trace_smb3_read_done(xid, req->PersistentFileId, io_parms->tcon->tid,
4533 ses->Suid, io_parms->offset, 0);
4534 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4535 cifs_small_buf_release(req);
4536 return rc == -ENODATA ? 0 : rc;
4538 trace_smb3_read_done(xid,
4539 req->PersistentFileId,
4540 io_parms->tcon->tid, ses->Suid,
4541 io_parms->offset, io_parms->length);
4543 cifs_small_buf_release(req);
4545 *nbytes = le32_to_cpu(rsp->DataLength);
4546 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4547 (*nbytes > io_parms->length)) {
4548 cifs_dbg(FYI, "bad length %d for count %d\n",
4549 *nbytes, io_parms->length);
4555 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
4556 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4557 } else if (resp_buftype != CIFS_NO_BUFFER) {
4558 *buf = rsp_iov.iov_base;
4559 if (resp_buftype == CIFS_SMALL_BUFFER)
4560 *buf_type = CIFS_SMALL_BUFFER;
4561 else if (resp_buftype == CIFS_LARGE_BUFFER)
4562 *buf_type = CIFS_LARGE_BUFFER;
4568 * Check the mid_state and signature on received buffer (if any), and queue the
4569 * workqueue completion task.
4572 smb2_writev_callback(struct mid_q_entry *mid)
4574 struct cifs_writedata *wdata = mid->callback_data;
4575 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4576 struct TCP_Server_Info *server = wdata->server;
4577 unsigned int written;
4578 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
4579 struct cifs_credits credits = { .value = 0, .instance = 0 };
4581 WARN_ONCE(wdata->server != mid->server,
4582 "wdata server %p != mid server %p",
4583 wdata->server, mid->server);
4585 switch (mid->mid_state) {
4586 case MID_RESPONSE_RECEIVED:
4587 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4588 credits.instance = server->reconnect_instance;
4589 wdata->result = smb2_check_receive(mid, server, 0);
4590 if (wdata->result != 0)
4593 written = le32_to_cpu(rsp->DataLength);
4595 * Mask off high 16 bits when bytes written as returned
4596 * by the server is greater than bytes requested by the
4597 * client. OS/2 servers are known to set incorrect
4600 if (written > wdata->bytes)
4603 if (written < wdata->bytes)
4604 wdata->result = -ENOSPC;
4606 wdata->bytes = written;
4608 case MID_REQUEST_SUBMITTED:
4609 case MID_RETRY_NEEDED:
4610 wdata->result = -EAGAIN;
4612 case MID_RESPONSE_MALFORMED:
4613 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4614 credits.instance = server->reconnect_instance;
4617 wdata->result = -EIO;
4620 #ifdef CONFIG_CIFS_SMB_DIRECT
4622 * If this wdata has a memory registered, the MR can be freed
4623 * The number of MRs available is limited, it's important to recover
4624 * used MR as soon as I/O is finished. Hold MR longer in the later
4625 * I/O process can possibly result in I/O deadlock due to lack of MR
4626 * to send request on I/O retry
4629 smbd_deregister_mr(wdata->mr);
4633 if (wdata->result) {
4634 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4635 trace_smb3_write_err(0 /* no xid */,
4636 wdata->cfile->fid.persistent_fid,
4637 tcon->tid, tcon->ses->Suid, wdata->offset,
4638 wdata->bytes, wdata->result);
4639 if (wdata->result == -ENOSPC)
4640 pr_warn_once("Out of space writing to %s\n",
4643 trace_smb3_write_done(0 /* no xid */,
4644 wdata->cfile->fid.persistent_fid,
4645 tcon->tid, tcon->ses->Suid,
4646 wdata->offset, wdata->bytes);
4648 queue_work(cifsiod_wq, &wdata->work);
4650 add_credits(server, &credits, 0);
4653 /* smb2_async_writev - send an async write, and set up mid to handle result */
4655 smb2_async_writev(struct cifs_writedata *wdata,
4656 void (*release)(struct kref *kref))
4658 int rc = -EACCES, flags = 0;
4659 struct smb2_write_req *req = NULL;
4660 struct smb2_hdr *shdr;
4661 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4662 struct TCP_Server_Info *server = wdata->server;
4664 struct smb_rqst rqst = { };
4665 unsigned int total_len;
4666 struct cifs_io_parms _io_parms;
4667 struct cifs_io_parms *io_parms = NULL;
4671 server = wdata->server = cifs_pick_channel(tcon->ses);
4674 * in future we may get cifs_io_parms passed in from the caller,
4675 * but for now we construct it here...
4677 _io_parms = (struct cifs_io_parms) {
4680 .offset = wdata->offset,
4681 .length = wdata->bytes,
4682 .persistent_fid = wdata->cfile->fid.persistent_fid,
4683 .volatile_fid = wdata->cfile->fid.volatile_fid,
4686 io_parms = &_io_parms;
4688 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
4689 (void **) &req, &total_len);
4693 if (smb3_encryption_required(tcon))
4694 flags |= CIFS_TRANSFORM_REQ;
4696 shdr = (struct smb2_hdr *)req;
4697 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4699 req->PersistentFileId = io_parms->persistent_fid;
4700 req->VolatileFileId = io_parms->volatile_fid;
4701 req->WriteChannelInfoOffset = 0;
4702 req->WriteChannelInfoLength = 0;
4703 req->Channel = SMB2_CHANNEL_NONE;
4704 req->Offset = cpu_to_le64(io_parms->offset);
4705 req->DataOffset = cpu_to_le16(
4706 offsetof(struct smb2_write_req, Buffer));
4707 req->RemainingBytes = 0;
4709 trace_smb3_write_enter(0 /* xid */,
4710 io_parms->persistent_fid,
4711 io_parms->tcon->tid,
4712 io_parms->tcon->ses->Suid,
4716 #ifdef CONFIG_CIFS_SMB_DIRECT
4718 * If we want to do a server RDMA read, fill in and append
4719 * smbd_buffer_descriptor_v1 to the end of write request
4721 if (smb3_use_rdma_offload(io_parms)) {
4722 struct smbd_buffer_descriptor_v1 *v1;
4723 size_t data_size = iov_iter_count(&wdata->iter);
4724 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4726 wdata->mr = smbd_register_mr(server->smbd_conn, &wdata->iter,
4727 false, need_invalidate);
4730 goto async_writev_out;
4733 req->DataOffset = 0;
4734 req->RemainingBytes = cpu_to_le32(data_size);
4735 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4736 if (need_invalidate)
4737 req->Channel = SMB2_CHANNEL_RDMA_V1;
4738 req->WriteChannelInfoOffset =
4739 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4740 req->WriteChannelInfoLength =
4741 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4742 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4743 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4744 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4745 v1->length = cpu_to_le32(wdata->mr->mr->length);
4748 iov[0].iov_len = total_len - 1;
4749 iov[0].iov_base = (char *)req;
4753 rqst.rq_iter = wdata->iter;
4754 rqst.rq_iter_size = iov_iter_count(&rqst.rq_iter);
4755 #ifdef CONFIG_CIFS_SMB_DIRECT
4757 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4759 cifs_dbg(FYI, "async write at %llu %u bytes iter=%zx\n",
4760 io_parms->offset, io_parms->length, iov_iter_count(&rqst.rq_iter));
4762 #ifdef CONFIG_CIFS_SMB_DIRECT
4763 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4765 req->Length = cpu_to_le32(io_parms->length);
4767 req->Length = cpu_to_le32(io_parms->length);
4770 if (wdata->credits.value > 0) {
4771 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4772 SMB2_MAX_BUFFER_SIZE));
4773 credit_request = le16_to_cpu(shdr->CreditCharge) + 8;
4774 if (server->credits >= server->max_credits)
4775 shdr->CreditRequest = cpu_to_le16(0);
4777 shdr->CreditRequest = cpu_to_le16(
4778 min_t(int, server->max_credits -
4779 server->credits, credit_request));
4781 rc = adjust_credits(server, &wdata->credits, io_parms->length);
4783 goto async_writev_out;
4785 flags |= CIFS_HAS_CREDITS;
4788 kref_get(&wdata->refcount);
4789 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4790 wdata, flags, &wdata->credits);
4793 trace_smb3_write_err(0 /* no xid */,
4794 io_parms->persistent_fid,
4795 io_parms->tcon->tid,
4796 io_parms->tcon->ses->Suid,
4800 kref_put(&wdata->refcount, release);
4801 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4805 cifs_small_buf_release(req);
4810 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4811 * The length field from io_parms must be at least 1 and indicates a number of
4812 * elements with data to write that begins with position 1 in iov array. All
4813 * data length is specified by count.
4816 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4817 unsigned int *nbytes, struct kvec *iov, int n_vec)
4819 struct smb_rqst rqst;
4821 struct smb2_write_req *req = NULL;
4822 struct smb2_write_rsp *rsp = NULL;
4824 struct kvec rsp_iov;
4826 unsigned int total_len;
4827 struct TCP_Server_Info *server;
4834 if (!io_parms->server)
4835 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4836 server = io_parms->server;
4838 return -ECONNABORTED;
4840 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
4841 (void **) &req, &total_len);
4845 if (smb3_encryption_required(io_parms->tcon))
4846 flags |= CIFS_TRANSFORM_REQ;
4848 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4850 req->PersistentFileId = io_parms->persistent_fid;
4851 req->VolatileFileId = io_parms->volatile_fid;
4852 req->WriteChannelInfoOffset = 0;
4853 req->WriteChannelInfoLength = 0;
4855 req->Length = cpu_to_le32(io_parms->length);
4856 req->Offset = cpu_to_le64(io_parms->offset);
4857 req->DataOffset = cpu_to_le16(
4858 offsetof(struct smb2_write_req, Buffer));
4859 req->RemainingBytes = 0;
4861 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4862 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4863 io_parms->offset, io_parms->length);
4865 iov[0].iov_base = (char *)req;
4867 iov[0].iov_len = total_len - 1;
4869 memset(&rqst, 0, sizeof(struct smb_rqst));
4871 rqst.rq_nvec = n_vec + 1;
4873 rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
4875 &resp_buftype, flags, &rsp_iov);
4876 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
4879 trace_smb3_write_err(xid,
4880 req->PersistentFileId,
4881 io_parms->tcon->tid,
4882 io_parms->tcon->ses->Suid,
4883 io_parms->offset, io_parms->length, rc);
4884 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
4885 cifs_dbg(VFS, "Send error in write = %d\n", rc);
4887 *nbytes = le32_to_cpu(rsp->DataLength);
4888 trace_smb3_write_done(xid,
4889 req->PersistentFileId,
4890 io_parms->tcon->tid,
4891 io_parms->tcon->ses->Suid,
4892 io_parms->offset, *nbytes);
4895 cifs_small_buf_release(req);
4896 free_rsp_buf(resp_buftype, rsp);
4900 int posix_info_sid_size(const void *beg, const void *end)
4908 subauth = *(u8 *)(beg+1);
4909 if (subauth < 1 || subauth > 15)
4912 total = 1 + 1 + 6 + 4*subauth;
4913 if (beg + total > end)
4919 int posix_info_parse(const void *beg, const void *end,
4920 struct smb2_posix_info_parsed *out)
4924 int owner_len, group_len;
4926 const void *owner_sid;
4927 const void *group_sid;
4930 /* if no end bound given, assume payload to be correct */
4932 const struct smb2_posix_info *p = beg;
4934 end = beg + le32_to_cpu(p->NextEntryOffset);
4935 /* last element will have a 0 offset, pick a sensible bound */
4940 /* check base buf */
4941 if (beg + sizeof(struct smb2_posix_info) > end)
4943 total_len = sizeof(struct smb2_posix_info);
4945 /* check owner sid */
4946 owner_sid = beg + total_len;
4947 owner_len = posix_info_sid_size(owner_sid, end);
4950 total_len += owner_len;
4952 /* check group sid */
4953 group_sid = beg + total_len;
4954 group_len = posix_info_sid_size(group_sid, end);
4957 total_len += group_len;
4959 /* check name len */
4960 if (beg + total_len + 4 > end)
4962 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
4963 if (name_len < 1 || name_len > 0xFFFF)
4968 name = beg + total_len;
4969 if (name + name_len > end)
4971 total_len += name_len;
4975 out->size = total_len;
4976 out->name_len = name_len;
4978 memcpy(&out->owner, owner_sid, owner_len);
4979 memcpy(&out->group, group_sid, group_len);
4984 static int posix_info_extra_size(const void *beg, const void *end)
4986 int len = posix_info_parse(beg, end, NULL);
4990 return len - sizeof(struct smb2_posix_info);
4994 num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
4998 unsigned int entrycount = 0;
4999 unsigned int next_offset = 0;
5001 FILE_DIRECTORY_INFO *dir_info;
5003 if (bufstart == NULL)
5006 entryptr = bufstart;
5009 if (entryptr + next_offset < entryptr ||
5010 entryptr + next_offset > end_of_buf ||
5011 entryptr + next_offset + size > end_of_buf) {
5012 cifs_dbg(VFS, "malformed search entry would overflow\n");
5016 entryptr = entryptr + next_offset;
5017 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
5019 if (infotype == SMB_FIND_FILE_POSIX_INFO)
5020 len = posix_info_extra_size(entryptr, end_of_buf);
5022 len = le32_to_cpu(dir_info->FileNameLength);
5025 entryptr + len < entryptr ||
5026 entryptr + len > end_of_buf ||
5027 entryptr + len + size > end_of_buf) {
5028 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
5033 *lastentry = entryptr;
5036 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
5047 int SMB2_query_directory_init(const unsigned int xid,
5048 struct cifs_tcon *tcon,
5049 struct TCP_Server_Info *server,
5050 struct smb_rqst *rqst,
5051 u64 persistent_fid, u64 volatile_fid,
5052 int index, int info_level)
5054 struct smb2_query_directory_req *req;
5055 unsigned char *bufptr;
5056 __le16 asteriks = cpu_to_le16('*');
5057 unsigned int output_size = CIFSMaxBufSize -
5058 MAX_SMB2_CREATE_RESPONSE_SIZE -
5059 MAX_SMB2_CLOSE_RESPONSE_SIZE;
5060 unsigned int total_len;
5061 struct kvec *iov = rqst->rq_iov;
5064 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
5065 (void **) &req, &total_len);
5069 switch (info_level) {
5070 case SMB_FIND_FILE_DIRECTORY_INFO:
5071 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
5073 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
5074 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
5076 case SMB_FIND_FILE_POSIX_INFO:
5077 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
5080 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
5085 req->FileIndex = cpu_to_le32(index);
5086 req->PersistentFileId = persistent_fid;
5087 req->VolatileFileId = volatile_fid;
5090 bufptr = req->Buffer;
5091 memcpy(bufptr, &asteriks, len);
5093 req->FileNameOffset =
5094 cpu_to_le16(sizeof(struct smb2_query_directory_req));
5095 req->FileNameLength = cpu_to_le16(len);
5097 * BB could be 30 bytes or so longer if we used SMB2 specific
5098 * buffer lengths, but this is safe and close enough.
5100 output_size = min_t(unsigned int, output_size, server->maxBuf);
5101 output_size = min_t(unsigned int, output_size, 2 << 15);
5102 req->OutputBufferLength = cpu_to_le32(output_size);
5104 iov[0].iov_base = (char *)req;
5106 iov[0].iov_len = total_len - 1;
5108 iov[1].iov_base = (char *)(req->Buffer);
5109 iov[1].iov_len = len;
5111 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
5112 tcon->ses->Suid, index, output_size);
5117 void SMB2_query_directory_free(struct smb_rqst *rqst)
5119 if (rqst && rqst->rq_iov) {
5120 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
5125 smb2_parse_query_directory(struct cifs_tcon *tcon,
5126 struct kvec *rsp_iov,
5128 struct cifs_search_info *srch_inf)
5130 struct smb2_query_directory_rsp *rsp;
5131 size_t info_buf_size;
5135 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
5137 switch (srch_inf->info_level) {
5138 case SMB_FIND_FILE_DIRECTORY_INFO:
5139 info_buf_size = sizeof(FILE_DIRECTORY_INFO);
5141 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
5142 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO);
5144 case SMB_FIND_FILE_POSIX_INFO:
5145 /* note that posix payload are variable size */
5146 info_buf_size = sizeof(struct smb2_posix_info);
5149 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
5150 srch_inf->info_level);
5154 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5155 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
5158 cifs_tcon_dbg(VFS, "bad info payload");
5162 srch_inf->unicode = true;
5164 if (srch_inf->ntwrk_buf_start) {
5165 if (srch_inf->smallBuf)
5166 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
5168 cifs_buf_release(srch_inf->ntwrk_buf_start);
5170 srch_inf->ntwrk_buf_start = (char *)rsp;
5171 srch_inf->srch_entries_start = srch_inf->last_entry =
5172 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
5173 end_of_smb = rsp_iov->iov_len + (char *)rsp;
5175 srch_inf->entries_in_buffer = num_entries(
5176 srch_inf->info_level,
5177 srch_inf->srch_entries_start,
5179 &srch_inf->last_entry,
5182 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
5183 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
5184 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
5185 srch_inf->srch_entries_start, srch_inf->last_entry);
5186 if (resp_buftype == CIFS_LARGE_BUFFER)
5187 srch_inf->smallBuf = false;
5188 else if (resp_buftype == CIFS_SMALL_BUFFER)
5189 srch_inf->smallBuf = true;
5191 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
5197 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
5198 u64 persistent_fid, u64 volatile_fid, int index,
5199 struct cifs_search_info *srch_inf)
5201 struct smb_rqst rqst;
5202 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
5203 struct smb2_query_directory_rsp *rsp = NULL;
5204 int resp_buftype = CIFS_NO_BUFFER;
5205 struct kvec rsp_iov;
5207 struct cifs_ses *ses = tcon->ses;
5208 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5211 if (!ses || !(ses->server))
5214 if (smb3_encryption_required(tcon))
5215 flags |= CIFS_TRANSFORM_REQ;
5217 memset(&rqst, 0, sizeof(struct smb_rqst));
5218 memset(&iov, 0, sizeof(iov));
5220 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
5222 rc = SMB2_query_directory_init(xid, tcon, server,
5223 &rqst, persistent_fid,
5224 volatile_fid, index,
5225 srch_inf->info_level);
5229 rc = cifs_send_recv(xid, ses, server,
5230 &rqst, &resp_buftype, flags, &rsp_iov);
5231 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
5234 if (rc == -ENODATA &&
5235 rsp->hdr.Status == STATUS_NO_MORE_FILES) {
5236 trace_smb3_query_dir_done(xid, persistent_fid,
5237 tcon->tid, tcon->ses->Suid, index, 0);
5238 srch_inf->endOfSearch = true;
5241 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5242 tcon->ses->Suid, index, 0, rc);
5243 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
5248 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
5251 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5252 tcon->ses->Suid, index, 0, rc);
5255 resp_buftype = CIFS_NO_BUFFER;
5257 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
5258 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
5261 SMB2_query_directory_free(&rqst);
5262 free_rsp_buf(resp_buftype, rsp);
5267 SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
5268 struct smb_rqst *rqst,
5269 u64 persistent_fid, u64 volatile_fid, u32 pid,
5270 u8 info_class, u8 info_type, u32 additional_info,
5271 void **data, unsigned int *size)
5273 struct smb2_set_info_req *req;
5274 struct kvec *iov = rqst->rq_iov;
5275 unsigned int i, total_len;
5278 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
5279 (void **) &req, &total_len);
5283 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
5284 req->InfoType = info_type;
5285 req->FileInfoClass = info_class;
5286 req->PersistentFileId = persistent_fid;
5287 req->VolatileFileId = volatile_fid;
5288 req->AdditionalInformation = cpu_to_le32(additional_info);
5290 req->BufferOffset = cpu_to_le16(sizeof(struct smb2_set_info_req));
5291 req->BufferLength = cpu_to_le32(*size);
5293 memcpy(req->Buffer, *data, *size);
5296 iov[0].iov_base = (char *)req;
5298 iov[0].iov_len = total_len - 1;
5300 for (i = 1; i < rqst->rq_nvec; i++) {
5301 le32_add_cpu(&req->BufferLength, size[i]);
5302 iov[i].iov_base = (char *)data[i];
5303 iov[i].iov_len = size[i];
5310 SMB2_set_info_free(struct smb_rqst *rqst)
5312 if (rqst && rqst->rq_iov)
5313 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
5317 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
5318 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
5319 u8 info_type, u32 additional_info, unsigned int num,
5320 void **data, unsigned int *size)
5322 struct smb_rqst rqst;
5323 struct smb2_set_info_rsp *rsp = NULL;
5325 struct kvec rsp_iov;
5328 struct cifs_ses *ses = tcon->ses;
5329 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5332 if (!ses || !server)
5338 if (smb3_encryption_required(tcon))
5339 flags |= CIFS_TRANSFORM_REQ;
5341 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
5345 memset(&rqst, 0, sizeof(struct smb_rqst));
5349 rc = SMB2_set_info_init(tcon, server,
5350 &rqst, persistent_fid, volatile_fid, pid,
5351 info_class, info_type, additional_info,
5359 rc = cifs_send_recv(xid, ses, server,
5360 &rqst, &resp_buftype, flags,
5362 SMB2_set_info_free(&rqst);
5363 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
5366 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
5367 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
5368 ses->Suid, info_class, (__u32)info_type, rc);
5371 free_rsp_buf(resp_buftype, rsp);
5377 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
5378 u64 volatile_fid, u32 pid, __le64 *eof)
5380 struct smb2_file_eof_info info;
5384 info.EndOfFile = *eof;
5387 size = sizeof(struct smb2_file_eof_info);
5389 trace_smb3_set_eof(xid, persistent_fid, tcon->tid, tcon->ses->Suid, le64_to_cpu(*eof));
5391 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5392 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
5393 0, 1, &data, &size);
5397 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
5398 u64 persistent_fid, u64 volatile_fid,
5399 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
5401 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5402 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
5403 1, (void **)&pnntsd, &pacllen);
5407 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
5408 u64 persistent_fid, u64 volatile_fid,
5409 struct smb2_file_full_ea_info *buf, int len)
5411 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5412 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
5413 0, 1, (void **)&buf, &len);
5417 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
5418 const u64 persistent_fid, const u64 volatile_fid,
5421 struct smb_rqst rqst;
5423 struct smb2_oplock_break *req = NULL;
5424 struct cifs_ses *ses = tcon->ses;
5425 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5426 int flags = CIFS_OBREAK_OP;
5427 unsigned int total_len;
5429 struct kvec rsp_iov;
5432 cifs_dbg(FYI, "SMB2_oplock_break\n");
5433 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5434 (void **) &req, &total_len);
5438 if (smb3_encryption_required(tcon))
5439 flags |= CIFS_TRANSFORM_REQ;
5441 req->VolatileFid = volatile_fid;
5442 req->PersistentFid = persistent_fid;
5443 req->OplockLevel = oplock_level;
5444 req->hdr.CreditRequest = cpu_to_le16(1);
5446 flags |= CIFS_NO_RSP_BUF;
5448 iov[0].iov_base = (char *)req;
5449 iov[0].iov_len = total_len;
5451 memset(&rqst, 0, sizeof(struct smb_rqst));
5455 rc = cifs_send_recv(xid, ses, server,
5456 &rqst, &resp_buf_type, flags, &rsp_iov);
5457 cifs_small_buf_release(req);
5460 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5461 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
5468 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
5469 struct kstatfs *kst)
5471 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
5472 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
5473 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
5474 kst->f_bfree = kst->f_bavail =
5475 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
5480 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
5481 struct kstatfs *kst)
5483 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
5484 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
5485 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
5486 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
5487 kst->f_bavail = kst->f_bfree;
5489 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
5490 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5491 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
5492 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5493 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
5499 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
5500 struct TCP_Server_Info *server,
5501 int level, int outbuf_len, u64 persistent_fid,
5505 struct smb2_query_info_req *req;
5506 unsigned int total_len;
5508 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
5510 if ((tcon->ses == NULL) || server == NULL)
5513 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
5514 (void **) &req, &total_len);
5518 req->InfoType = SMB2_O_INFO_FILESYSTEM;
5519 req->FileInfoClass = level;
5520 req->PersistentFileId = persistent_fid;
5521 req->VolatileFileId = volatile_fid;
5523 req->InputBufferOffset =
5524 cpu_to_le16(sizeof(struct smb2_query_info_req));
5525 req->OutputBufferLength = cpu_to_le32(
5526 outbuf_len + sizeof(struct smb2_query_info_rsp));
5528 iov->iov_base = (char *)req;
5529 iov->iov_len = total_len;
5533 static inline void free_qfs_info_req(struct kvec *iov)
5535 cifs_buf_release(iov->iov_base);
5539 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
5540 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5542 struct smb_rqst rqst;
5543 struct smb2_query_info_rsp *rsp = NULL;
5545 struct kvec rsp_iov;
5548 struct cifs_ses *ses = tcon->ses;
5549 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5550 FILE_SYSTEM_POSIX_INFO *info = NULL;
5553 rc = build_qfs_info_req(&iov, tcon, server,
5554 FS_POSIX_INFORMATION,
5555 sizeof(FILE_SYSTEM_POSIX_INFO),
5556 persistent_fid, volatile_fid);
5560 if (smb3_encryption_required(tcon))
5561 flags |= CIFS_TRANSFORM_REQ;
5563 memset(&rqst, 0, sizeof(struct smb_rqst));
5567 rc = cifs_send_recv(xid, ses, server,
5568 &rqst, &resp_buftype, flags, &rsp_iov);
5569 free_qfs_info_req(&iov);
5571 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5572 goto posix_qfsinf_exit;
5574 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5576 info = (FILE_SYSTEM_POSIX_INFO *)(
5577 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5578 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5579 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5580 sizeof(FILE_SYSTEM_POSIX_INFO));
5582 copy_posix_fs_info_to_kstatfs(info, fsdata);
5585 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5590 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5591 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5593 struct smb_rqst rqst;
5594 struct smb2_query_info_rsp *rsp = NULL;
5596 struct kvec rsp_iov;
5599 struct cifs_ses *ses = tcon->ses;
5600 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5601 struct smb2_fs_full_size_info *info = NULL;
5604 rc = build_qfs_info_req(&iov, tcon, server,
5605 FS_FULL_SIZE_INFORMATION,
5606 sizeof(struct smb2_fs_full_size_info),
5607 persistent_fid, volatile_fid);
5611 if (smb3_encryption_required(tcon))
5612 flags |= CIFS_TRANSFORM_REQ;
5614 memset(&rqst, 0, sizeof(struct smb_rqst));
5618 rc = cifs_send_recv(xid, ses, server,
5619 &rqst, &resp_buftype, flags, &rsp_iov);
5620 free_qfs_info_req(&iov);
5622 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5625 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5627 info = (struct smb2_fs_full_size_info *)(
5628 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5629 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5630 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5631 sizeof(struct smb2_fs_full_size_info));
5633 smb2_copy_fs_info_to_kstatfs(info, fsdata);
5636 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5641 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
5642 u64 persistent_fid, u64 volatile_fid, int level)
5644 struct smb_rqst rqst;
5645 struct smb2_query_info_rsp *rsp = NULL;
5647 struct kvec rsp_iov;
5649 int resp_buftype, max_len, min_len;
5650 struct cifs_ses *ses = tcon->ses;
5651 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5652 unsigned int rsp_len, offset;
5655 if (level == FS_DEVICE_INFORMATION) {
5656 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5657 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5658 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5659 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5660 min_len = MIN_FS_ATTR_INFO_SIZE;
5661 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5662 max_len = sizeof(struct smb3_fs_ss_info);
5663 min_len = sizeof(struct smb3_fs_ss_info);
5664 } else if (level == FS_VOLUME_INFORMATION) {
5665 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5666 min_len = sizeof(struct smb3_fs_vol_info);
5668 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
5672 rc = build_qfs_info_req(&iov, tcon, server,
5674 persistent_fid, volatile_fid);
5678 if (smb3_encryption_required(tcon))
5679 flags |= CIFS_TRANSFORM_REQ;
5681 memset(&rqst, 0, sizeof(struct smb_rqst));
5685 rc = cifs_send_recv(xid, ses, server,
5686 &rqst, &resp_buftype, flags, &rsp_iov);
5687 free_qfs_info_req(&iov);
5689 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5692 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5694 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5695 offset = le16_to_cpu(rsp->OutputBufferOffset);
5696 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
5700 if (level == FS_ATTRIBUTE_INFORMATION)
5701 memcpy(&tcon->fsAttrInfo, offset
5702 + (char *)rsp, min_t(unsigned int,
5704 else if (level == FS_DEVICE_INFORMATION)
5705 memcpy(&tcon->fsDevInfo, offset
5706 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
5707 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5708 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
5709 (offset + (char *)rsp);
5710 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5711 tcon->perf_sector_size =
5712 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
5713 } else if (level == FS_VOLUME_INFORMATION) {
5714 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5715 (offset + (char *)rsp);
5716 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5717 tcon->vol_create_time = vol_info->VolumeCreationTime;
5721 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5726 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5727 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5728 const __u32 num_lock, struct smb2_lock_element *buf)
5730 struct smb_rqst rqst;
5732 struct smb2_lock_req *req = NULL;
5734 struct kvec rsp_iov;
5737 int flags = CIFS_NO_RSP_BUF;
5738 unsigned int total_len;
5739 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5741 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
5743 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
5744 (void **) &req, &total_len);
5748 if (smb3_encryption_required(tcon))
5749 flags |= CIFS_TRANSFORM_REQ;
5751 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
5752 req->LockCount = cpu_to_le16(num_lock);
5754 req->PersistentFileId = persist_fid;
5755 req->VolatileFileId = volatile_fid;
5757 count = num_lock * sizeof(struct smb2_lock_element);
5759 iov[0].iov_base = (char *)req;
5760 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
5761 iov[1].iov_base = (char *)buf;
5762 iov[1].iov_len = count;
5764 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
5766 memset(&rqst, 0, sizeof(struct smb_rqst));
5770 rc = cifs_send_recv(xid, tcon->ses, server,
5771 &rqst, &resp_buf_type, flags,
5773 cifs_small_buf_release(req);
5775 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
5776 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
5777 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
5778 tcon->ses->Suid, rc);
5785 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
5786 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5787 const __u64 length, const __u64 offset, const __u32 lock_flags,
5790 struct smb2_lock_element lock;
5792 lock.Offset = cpu_to_le64(offset);
5793 lock.Length = cpu_to_le64(length);
5794 lock.Flags = cpu_to_le32(lock_flags);
5795 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5796 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5798 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5802 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5803 __u8 *lease_key, const __le32 lease_state)
5805 struct smb_rqst rqst;
5807 struct smb2_lease_ack *req = NULL;
5808 struct cifs_ses *ses = tcon->ses;
5809 int flags = CIFS_OBREAK_OP;
5810 unsigned int total_len;
5812 struct kvec rsp_iov;
5814 __u64 *please_key_high;
5815 __u64 *please_key_low;
5816 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5818 cifs_dbg(FYI, "SMB2_lease_break\n");
5819 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5820 (void **) &req, &total_len);
5824 if (smb3_encryption_required(tcon))
5825 flags |= CIFS_TRANSFORM_REQ;
5827 req->hdr.CreditRequest = cpu_to_le16(1);
5828 req->StructureSize = cpu_to_le16(36);
5831 memcpy(req->LeaseKey, lease_key, 16);
5832 req->LeaseState = lease_state;
5834 flags |= CIFS_NO_RSP_BUF;
5836 iov[0].iov_base = (char *)req;
5837 iov[0].iov_len = total_len;
5839 memset(&rqst, 0, sizeof(struct smb_rqst));
5843 rc = cifs_send_recv(xid, ses, server,
5844 &rqst, &resp_buf_type, flags, &rsp_iov);
5845 cifs_small_buf_release(req);
5847 please_key_low = (__u64 *)lease_key;
5848 please_key_high = (__u64 *)(lease_key+8);
5850 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5851 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5852 ses->Suid, *please_key_low, *please_key_high, rc);
5853 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
5855 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5856 ses->Suid, *please_key_low, *please_key_high);