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 server->terminate = true;
182 spin_unlock(&ses->chan_lock);
185 * the above reference of server by channel
186 * needs to be dropped without holding chan_lock
187 * as cifs_put_tcp_session takes a higher lock
188 * i.e. cifs_tcp_ses_lock
190 cifs_put_tcp_session(server, from_reconnect);
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);
402 spin_lock(&ses->ses_lock);
403 if (ses->flags & CIFS_SES_FLAG_SCALE_CHANNELS) {
404 spin_unlock(&ses->ses_lock);
405 mutex_unlock(&ses->session_mutex);
406 goto skip_add_channels;
408 ses->flags |= CIFS_SES_FLAG_SCALE_CHANNELS;
409 spin_unlock(&ses->ses_lock);
412 (server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL) &&
413 server->ops->query_server_interfaces) {
414 mutex_unlock(&ses->session_mutex);
417 * query server network interfaces, in case they change
420 rc = server->ops->query_server_interfaces(xid, tcon, false);
423 if (rc == -EOPNOTSUPP && ses->chan_count > 1) {
425 * some servers like Azure SMB server do not advertise
426 * that multichannel has been disabled with server
427 * capabilities, rather return STATUS_NOT_IMPLEMENTED.
428 * treat this as server not supporting multichannel
431 rc = cifs_chan_skip_or_disable(ses, server,
433 goto skip_add_channels;
435 cifs_dbg(FYI, "%s: failed to query server interfaces: %d\n",
438 if (ses->chan_max > ses->chan_count &&
440 !SERVER_IS_CHAN(server)) {
441 if (ses->chan_count == 1) {
442 cifs_server_dbg(VFS, "supports multichannel now\n");
443 queue_delayed_work(cifsiod_wq, &tcon->query_interfaces,
444 (SMB_INTERFACE_POLL_INTERVAL * HZ));
447 cifs_try_adding_channels(ses);
450 mutex_unlock(&ses->session_mutex);
454 spin_lock(&ses->ses_lock);
455 ses->flags &= ~CIFS_SES_FLAG_SCALE_CHANNELS;
456 spin_unlock(&ses->ses_lock);
458 if (smb2_command != SMB2_INTERNAL_CMD)
459 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
461 atomic_inc(&tconInfoReconnectCount);
464 * Check if handle based operation so we know whether we can continue
465 * or not without returning to caller to reset file handle.
468 * BB Is flush done by server on drop of tcp session? Should we special
469 * case it and skip above?
471 switch (smb2_command) {
476 case SMB2_QUERY_DIRECTORY:
477 case SMB2_CHANGE_NOTIFY:
478 case SMB2_QUERY_INFO:
487 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon,
488 struct TCP_Server_Info *server,
490 unsigned int *total_len)
492 struct smb2_pdu *spdu = buf;
493 /* lookup word count ie StructureSize from table */
494 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
497 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
498 * largest operations (Create)
502 smb2_hdr_assemble(&spdu->hdr, smb2_command, tcon, server);
503 spdu->StructureSize2 = cpu_to_le16(parmsize);
505 *total_len = parmsize + sizeof(struct smb2_hdr);
509 * Allocate and return pointer to an SMB request hdr, and set basic
510 * SMB information in the SMB header. If the return code is zero, this
511 * function must have filled in request_buf pointer.
513 static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
514 struct TCP_Server_Info *server,
515 void **request_buf, unsigned int *total_len)
517 /* BB eventually switch this to SMB2 specific small buf size */
518 switch (smb2_command) {
520 case SMB2_QUERY_INFO:
521 *request_buf = cifs_buf_get();
524 *request_buf = cifs_small_buf_get();
527 if (*request_buf == NULL) {
528 /* BB should we add a retry in here if not a writepage? */
532 fill_small_buf(smb2_command, tcon, server,
533 (struct smb2_hdr *)(*request_buf),
537 uint16_t com_code = le16_to_cpu(smb2_command);
538 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
539 cifs_stats_inc(&tcon->num_smbs_sent);
545 static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
546 struct TCP_Server_Info *server,
547 void **request_buf, unsigned int *total_len)
551 rc = smb2_reconnect(smb2_command, tcon, server, false);
555 return __smb2_plain_req_init(smb2_command, tcon, server, request_buf,
559 static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
560 struct TCP_Server_Info *server,
561 void **request_buf, unsigned int *total_len)
563 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
564 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
565 return __smb2_plain_req_init(SMB2_IOCTL, tcon, server,
566 request_buf, total_len);
568 return smb2_plain_req_init(SMB2_IOCTL, tcon, server,
569 request_buf, total_len);
572 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
575 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
577 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
578 pneg_ctxt->DataLength = cpu_to_le16(38);
579 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
580 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
581 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
582 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
586 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
588 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
589 pneg_ctxt->DataLength =
590 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
591 - sizeof(struct smb2_neg_context));
592 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
593 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
594 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
595 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
599 build_signing_ctxt(struct smb2_signing_capabilities *pneg_ctxt)
601 unsigned int ctxt_len = sizeof(struct smb2_signing_capabilities);
602 unsigned short num_algs = 1; /* number of signing algorithms sent */
604 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
606 * Context Data length must be rounded to multiple of 8 for some servers
608 pneg_ctxt->DataLength = cpu_to_le16(ALIGN(sizeof(struct smb2_signing_capabilities) -
609 sizeof(struct smb2_neg_context) +
610 (num_algs * sizeof(u16)), 8));
611 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(num_algs);
612 pneg_ctxt->SigningAlgorithms[0] = cpu_to_le16(SIGNING_ALG_AES_CMAC);
614 ctxt_len += sizeof(__le16) * num_algs;
615 ctxt_len = ALIGN(ctxt_len, 8);
617 /* TBD add SIGNING_ALG_AES_GMAC and/or SIGNING_ALG_HMAC_SHA256 */
621 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
623 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
624 if (require_gcm_256) {
625 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + 1 cipher */
626 pneg_ctxt->CipherCount = cpu_to_le16(1);
627 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES256_GCM;
628 } else if (enable_gcm_256) {
629 pneg_ctxt->DataLength = cpu_to_le16(8); /* Cipher Count + 3 ciphers */
630 pneg_ctxt->CipherCount = cpu_to_le16(3);
631 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
632 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES256_GCM;
633 pneg_ctxt->Ciphers[2] = SMB2_ENCRYPTION_AES128_CCM;
635 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + 2 ciphers */
636 pneg_ctxt->CipherCount = cpu_to_le16(2);
637 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
638 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
643 build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
645 struct nls_table *cp = load_nls_default();
647 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
649 /* copy up to max of first 100 bytes of server name to NetName field */
650 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
651 /* context size is DataLength + minimal smb2_neg_context */
652 return ALIGN(le16_to_cpu(pneg_ctxt->DataLength) + sizeof(struct smb2_neg_context), 8);
656 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
658 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
659 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
660 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
661 pneg_ctxt->Name[0] = 0x93;
662 pneg_ctxt->Name[1] = 0xAD;
663 pneg_ctxt->Name[2] = 0x25;
664 pneg_ctxt->Name[3] = 0x50;
665 pneg_ctxt->Name[4] = 0x9C;
666 pneg_ctxt->Name[5] = 0xB4;
667 pneg_ctxt->Name[6] = 0x11;
668 pneg_ctxt->Name[7] = 0xE7;
669 pneg_ctxt->Name[8] = 0xB4;
670 pneg_ctxt->Name[9] = 0x23;
671 pneg_ctxt->Name[10] = 0x83;
672 pneg_ctxt->Name[11] = 0xDE;
673 pneg_ctxt->Name[12] = 0x96;
674 pneg_ctxt->Name[13] = 0x8B;
675 pneg_ctxt->Name[14] = 0xCD;
676 pneg_ctxt->Name[15] = 0x7C;
680 assemble_neg_contexts(struct smb2_negotiate_req *req,
681 struct TCP_Server_Info *server, unsigned int *total_len)
683 unsigned int ctxt_len, neg_context_count;
684 struct TCP_Server_Info *pserver;
688 if (*total_len > 200) {
689 /* In case length corrupted don't want to overrun smb buffer */
690 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
695 * round up total_len of fixed part of SMB3 negotiate request to 8
696 * byte boundary before adding negotiate contexts
698 *total_len = ALIGN(*total_len, 8);
700 pneg_ctxt = (*total_len) + (char *)req;
701 req->NegotiateContextOffset = cpu_to_le32(*total_len);
703 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
704 ctxt_len = ALIGN(sizeof(struct smb2_preauth_neg_context), 8);
705 *total_len += ctxt_len;
706 pneg_ctxt += ctxt_len;
708 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
709 ctxt_len = ALIGN(sizeof(struct smb2_encryption_neg_context), 8);
710 *total_len += ctxt_len;
711 pneg_ctxt += ctxt_len;
714 * secondary channels don't have the hostname field populated
715 * use the hostname field in the primary channel instead
717 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
718 cifs_server_lock(pserver);
719 hostname = pserver->hostname;
720 if (hostname && (hostname[0] != 0)) {
721 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
723 *total_len += ctxt_len;
724 pneg_ctxt += ctxt_len;
725 neg_context_count = 3;
727 neg_context_count = 2;
728 cifs_server_unlock(pserver);
730 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
731 *total_len += sizeof(struct smb2_posix_neg_context);
732 pneg_ctxt += sizeof(struct smb2_posix_neg_context);
735 if (server->compress_algorithm) {
736 build_compression_ctxt((struct smb2_compression_capabilities_context *)
738 ctxt_len = ALIGN(sizeof(struct smb2_compression_capabilities_context), 8);
739 *total_len += ctxt_len;
740 pneg_ctxt += ctxt_len;
744 if (enable_negotiate_signing) {
745 ctxt_len = build_signing_ctxt((struct smb2_signing_capabilities *)
747 *total_len += ctxt_len;
748 pneg_ctxt += ctxt_len;
752 /* check for and add transport_capabilities and signing capabilities */
753 req->NegotiateContextCount = cpu_to_le16(neg_context_count);
757 /* If invalid preauth context warn but use what we requested, SHA-512 */
758 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
760 unsigned int len = le16_to_cpu(ctxt->DataLength);
763 * Caller checked that DataLength remains within SMB boundary. We still
764 * need to confirm that one HashAlgorithms member is accounted for.
766 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
767 pr_warn_once("server sent bad preauth context\n");
769 } else if (len < MIN_PREAUTH_CTXT_DATA_LEN + le16_to_cpu(ctxt->SaltLength)) {
770 pr_warn_once("server sent invalid SaltLength\n");
773 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
774 pr_warn_once("Invalid SMB3 hash algorithm count\n");
775 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
776 pr_warn_once("unknown SMB3 hash algorithm\n");
779 static void decode_compress_ctx(struct TCP_Server_Info *server,
780 struct smb2_compression_capabilities_context *ctxt)
782 unsigned int len = le16_to_cpu(ctxt->DataLength);
785 * Caller checked that DataLength remains within SMB boundary. We still
786 * need to confirm that one CompressionAlgorithms member is accounted
790 pr_warn_once("server sent bad compression cntxt\n");
793 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
794 pr_warn_once("Invalid SMB3 compress algorithm count\n");
797 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
798 pr_warn_once("unknown compression algorithm\n");
801 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
804 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
805 struct smb2_encryption_neg_context *ctxt)
807 unsigned int len = le16_to_cpu(ctxt->DataLength);
809 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
811 * Caller checked that DataLength remains within SMB boundary. We still
812 * need to confirm that one Cipher flexible array member is accounted
815 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
816 pr_warn_once("server sent bad crypto ctxt len\n");
820 if (le16_to_cpu(ctxt->CipherCount) != 1) {
821 pr_warn_once("Invalid SMB3.11 cipher count\n");
824 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
825 if (require_gcm_256) {
826 if (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM) {
827 cifs_dbg(VFS, "Server does not support requested encryption type (AES256 GCM)\n");
830 } else if (ctxt->Ciphers[0] == 0) {
832 * e.g. if server only supported AES256_CCM (very unlikely)
833 * or server supported no encryption types or had all disabled.
834 * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case
835 * in which mount requested encryption ("seal") checks later
836 * on during tree connection will return proper rc, but if
837 * seal not requested by client, since server is allowed to
838 * return 0 to indicate no supported cipher, we can't fail here
840 server->cipher_type = 0;
841 server->capabilities &= ~SMB2_GLOBAL_CAP_ENCRYPTION;
842 pr_warn_once("Server does not support requested encryption types\n");
844 } else if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
845 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM) &&
846 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM)) {
847 /* server returned a cipher we didn't ask for */
848 pr_warn_once("Invalid SMB3.11 cipher returned\n");
851 server->cipher_type = ctxt->Ciphers[0];
852 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
856 static void decode_signing_ctx(struct TCP_Server_Info *server,
857 struct smb2_signing_capabilities *pctxt)
859 unsigned int len = le16_to_cpu(pctxt->DataLength);
862 * Caller checked that DataLength remains within SMB boundary. We still
863 * need to confirm that one SigningAlgorithms flexible array member is
866 if ((len < 4) || (len > 16)) {
867 pr_warn_once("server sent bad signing negcontext\n");
870 if (le16_to_cpu(pctxt->SigningAlgorithmCount) != 1) {
871 pr_warn_once("Invalid signing algorithm count\n");
874 if (le16_to_cpu(pctxt->SigningAlgorithms[0]) > 2) {
875 pr_warn_once("unknown signing algorithm\n");
879 server->signing_negotiated = true;
880 server->signing_algorithm = le16_to_cpu(pctxt->SigningAlgorithms[0]);
881 cifs_dbg(FYI, "signing algorithm %d chosen\n",
882 server->signing_algorithm);
886 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
887 struct TCP_Server_Info *server,
888 unsigned int len_of_smb)
890 struct smb2_neg_context *pctx;
891 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
892 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
893 unsigned int len_of_ctxts, i;
896 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
897 if (len_of_smb <= offset) {
898 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
902 len_of_ctxts = len_of_smb - offset;
904 for (i = 0; i < ctxt_cnt; i++) {
906 /* check that offset is not beyond end of SMB */
907 if (len_of_ctxts < sizeof(struct smb2_neg_context))
910 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
911 clen = sizeof(struct smb2_neg_context)
912 + le16_to_cpu(pctx->DataLength);
914 * 2.2.4 SMB2 NEGOTIATE Response
915 * Subsequent negotiate contexts MUST appear at the first 8-byte
916 * aligned offset following the previous negotiate context.
918 if (i + 1 != ctxt_cnt)
919 clen = ALIGN(clen, 8);
920 if (clen > len_of_ctxts)
923 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
924 decode_preauth_context(
925 (struct smb2_preauth_neg_context *)pctx);
926 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
927 rc = decode_encrypt_ctx(server,
928 (struct smb2_encryption_neg_context *)pctx);
929 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
930 decode_compress_ctx(server,
931 (struct smb2_compression_capabilities_context *)pctx);
932 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
933 server->posix_ext_supported = true;
934 else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES)
935 decode_signing_ctx(server,
936 (struct smb2_signing_capabilities *)pctx);
938 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
939 le16_to_cpu(pctx->ContextType));
944 len_of_ctxts -= clen;
949 static struct create_posix *
950 create_posix_buf(umode_t mode)
952 struct create_posix *buf;
954 buf = kzalloc(sizeof(struct create_posix),
959 buf->ccontext.DataOffset =
960 cpu_to_le16(offsetof(struct create_posix, Mode));
961 buf->ccontext.DataLength = cpu_to_le32(4);
962 buf->ccontext.NameOffset =
963 cpu_to_le16(offsetof(struct create_posix, Name));
964 buf->ccontext.NameLength = cpu_to_le16(16);
966 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
977 buf->Name[10] = 0x83;
978 buf->Name[11] = 0xDE;
979 buf->Name[12] = 0x96;
980 buf->Name[13] = 0x8B;
981 buf->Name[14] = 0xCD;
982 buf->Name[15] = 0x7C;
983 buf->Mode = cpu_to_le32(mode);
984 cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
989 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
991 unsigned int num = *num_iovec;
993 iov[num].iov_base = create_posix_buf(mode);
994 if (mode == ACL_NO_MODE)
995 cifs_dbg(FYI, "%s: no mode\n", __func__);
996 if (iov[num].iov_base == NULL)
998 iov[num].iov_len = sizeof(struct create_posix);
999 *num_iovec = num + 1;
1006 * SMB2 Worker functions follow:
1008 * The general structure of the worker functions is:
1009 * 1) Call smb2_init (assembles SMB2 header)
1010 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
1011 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
1012 * 4) Decode SMB2 command specific fields in the fixed length area
1013 * 5) Decode variable length data area (if any for this SMB2 command type)
1014 * 6) Call free smb buffer
1020 SMB2_negotiate(const unsigned int xid,
1021 struct cifs_ses *ses,
1022 struct TCP_Server_Info *server)
1024 struct smb_rqst rqst;
1025 struct smb2_negotiate_req *req;
1026 struct smb2_negotiate_rsp *rsp;
1028 struct kvec rsp_iov;
1031 int blob_offset, blob_length;
1032 char *security_blob;
1033 int flags = CIFS_NEG_OP;
1034 unsigned int total_len;
1036 cifs_dbg(FYI, "Negotiate protocol\n");
1039 WARN(1, "%s: server is NULL!\n", __func__);
1043 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
1044 (void **) &req, &total_len);
1048 req->hdr.SessionId = 0;
1050 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
1051 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
1053 if (strcmp(server->vals->version_string,
1054 SMB3ANY_VERSION_STRING) == 0) {
1055 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1056 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1057 req->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1058 req->DialectCount = cpu_to_le16(3);
1060 } else if (strcmp(server->vals->version_string,
1061 SMBDEFAULT_VERSION_STRING) == 0) {
1062 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1063 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1064 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1065 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1066 req->DialectCount = cpu_to_le16(4);
1069 /* otherwise send specific dialect */
1070 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
1071 req->DialectCount = cpu_to_le16(1);
1075 /* only one of SMB2 signing flags may be set in SMB2 request */
1077 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1078 else if (global_secflags & CIFSSEC_MAY_SIGN)
1079 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1081 req->SecurityMode = 0;
1083 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
1084 if (ses->chan_max > 1)
1085 req->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
1087 /* ClientGUID must be zero for SMB2.02 dialect */
1088 if (server->vals->protocol_id == SMB20_PROT_ID)
1089 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
1091 memcpy(req->ClientGUID, server->client_guid,
1092 SMB2_CLIENT_GUID_SIZE);
1093 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
1094 (strcmp(server->vals->version_string,
1095 SMB3ANY_VERSION_STRING) == 0) ||
1096 (strcmp(server->vals->version_string,
1097 SMBDEFAULT_VERSION_STRING) == 0))
1098 assemble_neg_contexts(req, server, &total_len);
1100 iov[0].iov_base = (char *)req;
1101 iov[0].iov_len = total_len;
1103 memset(&rqst, 0, sizeof(struct smb_rqst));
1107 rc = cifs_send_recv(xid, ses, server,
1108 &rqst, &resp_buftype, flags, &rsp_iov);
1109 cifs_small_buf_release(req);
1110 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
1112 * No tcon so can't do
1113 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1115 if (rc == -EOPNOTSUPP) {
1116 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");
1122 if (strcmp(server->vals->version_string,
1123 SMB3ANY_VERSION_STRING) == 0) {
1124 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
1125 cifs_server_dbg(VFS,
1126 "SMB2 dialect returned but not requested\n");
1128 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
1129 cifs_server_dbg(VFS,
1130 "SMB2.1 dialect returned but not requested\n");
1132 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1133 /* ops set to 3.0 by default for default so update */
1134 server->ops = &smb311_operations;
1135 server->vals = &smb311_values;
1137 } else if (strcmp(server->vals->version_string,
1138 SMBDEFAULT_VERSION_STRING) == 0) {
1139 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
1140 cifs_server_dbg(VFS,
1141 "SMB2 dialect returned but not requested\n");
1143 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
1144 /* ops set to 3.0 by default for default so update */
1145 server->ops = &smb21_operations;
1146 server->vals = &smb21_values;
1147 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1148 server->ops = &smb311_operations;
1149 server->vals = &smb311_values;
1151 } else if (le16_to_cpu(rsp->DialectRevision) !=
1152 server->vals->protocol_id) {
1153 /* if requested single dialect ensure returned dialect matched */
1154 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
1155 le16_to_cpu(rsp->DialectRevision));
1159 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
1161 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
1162 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
1163 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
1164 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
1165 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
1166 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
1167 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
1168 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
1169 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
1170 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
1172 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
1173 le16_to_cpu(rsp->DialectRevision));
1178 server->dialect = le16_to_cpu(rsp->DialectRevision);
1181 * Keep a copy of the hash after negprot. This hash will be
1182 * the starting hash value for all sessions made from this
1185 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
1186 SMB2_PREAUTH_HASH_SIZE);
1188 /* SMB2 only has an extended negflavor */
1189 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
1190 /* set it to the maximum buffer size value we can send with 1 credit */
1191 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
1192 SMB2_MAX_BUFFER_SIZE);
1193 server->max_read = le32_to_cpu(rsp->MaxReadSize);
1194 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
1195 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
1196 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
1197 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
1199 server->capabilities = le32_to_cpu(rsp->Capabilities);
1200 /* Internal types */
1201 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
1204 * SMB3.0 supports only 1 cipher and doesn't have a encryption neg context
1205 * Set the cipher type manually.
1207 if (server->dialect == SMB30_PROT_ID && (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1208 server->cipher_type = SMB2_ENCRYPTION_AES128_CCM;
1210 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
1211 (struct smb2_hdr *)rsp);
1213 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
1215 * ses->sectype = RawNTLMSSP;
1216 * but for time being this is our only auth choice so doesn't matter.
1217 * We just found a server which sets blob length to zero expecting raw.
1219 if (blob_length == 0) {
1220 cifs_dbg(FYI, "missing security blob on negprot\n");
1221 server->sec_ntlmssp = true;
1224 rc = cifs_enable_signing(server, ses->sign);
1228 rc = decode_negTokenInit(security_blob, blob_length, server);
1235 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1236 if (rsp->NegotiateContextCount)
1237 rc = smb311_decode_neg_context(rsp, server,
1240 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
1243 free_rsp_buf(resp_buftype, rsp);
1247 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1250 struct validate_negotiate_info_req *pneg_inbuf;
1251 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
1253 u32 inbuflen; /* max of 4 dialects */
1254 struct TCP_Server_Info *server = tcon->ses->server;
1256 cifs_dbg(FYI, "validate negotiate\n");
1258 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1259 if (server->dialect == SMB311_PROT_ID)
1263 * validation ioctl must be signed, so no point sending this if we
1264 * can not sign it (ie are not known user). Even if signing is not
1265 * required (enabled but not negotiated), in those cases we selectively
1266 * sign just this, the first and only signed request on a connection.
1267 * Having validation of negotiate info helps reduce attack vectors.
1269 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1270 return 0; /* validation requires signing */
1272 if (tcon->ses->user_name == NULL) {
1273 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1274 return 0; /* validation requires signing */
1277 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1278 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1280 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1284 pneg_inbuf->Capabilities =
1285 cpu_to_le32(server->vals->req_capabilities);
1286 if (tcon->ses->chan_max > 1)
1287 pneg_inbuf->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
1289 memcpy(pneg_inbuf->Guid, server->client_guid,
1290 SMB2_CLIENT_GUID_SIZE);
1292 if (tcon->ses->sign)
1293 pneg_inbuf->SecurityMode =
1294 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1295 else if (global_secflags & CIFSSEC_MAY_SIGN)
1296 pneg_inbuf->SecurityMode =
1297 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1299 pneg_inbuf->SecurityMode = 0;
1302 if (strcmp(server->vals->version_string,
1303 SMB3ANY_VERSION_STRING) == 0) {
1304 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1305 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1306 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1307 pneg_inbuf->DialectCount = cpu_to_le16(3);
1308 /* SMB 2.1 not included so subtract one dialect from len */
1309 inbuflen = sizeof(*pneg_inbuf) -
1310 (sizeof(pneg_inbuf->Dialects[0]));
1311 } else if (strcmp(server->vals->version_string,
1312 SMBDEFAULT_VERSION_STRING) == 0) {
1313 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1314 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1315 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1316 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1317 pneg_inbuf->DialectCount = cpu_to_le16(4);
1318 /* structure is big enough for 4 dialects */
1319 inbuflen = sizeof(*pneg_inbuf);
1321 /* otherwise specific dialect was requested */
1322 pneg_inbuf->Dialects[0] =
1323 cpu_to_le16(server->vals->protocol_id);
1324 pneg_inbuf->DialectCount = cpu_to_le16(1);
1325 /* structure is big enough for 4 dialects, sending only 1 */
1326 inbuflen = sizeof(*pneg_inbuf) -
1327 sizeof(pneg_inbuf->Dialects[0]) * 3;
1330 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1331 FSCTL_VALIDATE_NEGOTIATE_INFO,
1332 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1333 (char **)&pneg_rsp, &rsplen);
1334 if (rc == -EOPNOTSUPP) {
1336 * Old Windows versions or Netapp SMB server can return
1337 * not supported error. Client should accept it.
1339 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1341 goto out_free_inbuf;
1342 } else if (rc != 0) {
1343 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1346 goto out_free_inbuf;
1350 if (rsplen != sizeof(*pneg_rsp)) {
1351 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1354 /* relax check since Mac returns max bufsize allowed on ioctl */
1355 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1359 /* check validate negotiate info response matches what we got earlier */
1360 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1363 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1366 /* do not validate server guid because not saved at negprot time yet */
1368 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1369 SMB2_LARGE_FILES) != server->capabilities)
1372 /* validate negotiate successful */
1374 cifs_dbg(FYI, "validate negotiate info successful\n");
1378 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1387 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1389 switch (requested) {
1396 if (server->sec_ntlmssp &&
1397 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1399 if ((server->sec_kerberos || server->sec_mskerberos) &&
1400 (global_secflags & CIFSSEC_MAY_KRB5))
1408 struct SMB2_sess_data {
1410 struct cifs_ses *ses;
1411 struct TCP_Server_Info *server;
1412 struct nls_table *nls_cp;
1413 void (*func)(struct SMB2_sess_data *);
1415 u64 previous_session;
1417 /* we will send the SMB in three pieces:
1418 * a fixed length beginning part, an optional
1419 * SPNEGO blob (which can be zero length), and a
1420 * last part which will include the strings
1421 * and rest of bcc area. This allows us to avoid
1422 * a large buffer 17K allocation
1429 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1432 struct cifs_ses *ses = sess_data->ses;
1433 struct TCP_Server_Info *server = sess_data->server;
1434 struct smb2_sess_setup_req *req;
1435 unsigned int total_len;
1436 bool is_binding = false;
1438 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
1444 spin_lock(&ses->ses_lock);
1445 is_binding = (ses->ses_status == SES_GOOD);
1446 spin_unlock(&ses->ses_lock);
1449 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1450 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1451 req->PreviousSessionId = 0;
1452 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1453 cifs_dbg(FYI, "Binding to sess id: %llx\n", ses->Suid);
1455 /* First session, not a reauthenticate */
1456 req->hdr.SessionId = 0;
1458 * if reconnect, we need to send previous sess id
1461 req->PreviousSessionId = cpu_to_le64(sess_data->previous_session);
1462 req->Flags = 0; /* MBZ */
1463 cifs_dbg(FYI, "Fresh session. Previous: %llx\n",
1464 sess_data->previous_session);
1467 /* enough to enable echos and oplocks and one max size write */
1468 if (server->credits >= server->max_credits)
1469 req->hdr.CreditRequest = cpu_to_le16(0);
1471 req->hdr.CreditRequest = cpu_to_le16(
1472 min_t(int, server->max_credits -
1473 server->credits, 130));
1475 /* only one of SMB2 signing flags may be set in SMB2 request */
1477 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1478 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1479 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1481 req->SecurityMode = 0;
1483 #ifdef CONFIG_CIFS_DFS_UPCALL
1484 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1486 req->Capabilities = 0;
1487 #endif /* DFS_UPCALL */
1489 req->Channel = 0; /* MBZ */
1491 sess_data->iov[0].iov_base = (char *)req;
1493 sess_data->iov[0].iov_len = total_len - 1;
1495 * This variable will be used to clear the buffer
1496 * allocated above in case of any error in the calling function.
1498 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1504 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1506 struct kvec *iov = sess_data->iov;
1508 /* iov[1] is already freed by caller */
1509 if (sess_data->buf0_type != CIFS_NO_BUFFER && iov[0].iov_base)
1510 memzero_explicit(iov[0].iov_base, iov[0].iov_len);
1512 free_rsp_buf(sess_data->buf0_type, iov[0].iov_base);
1513 sess_data->buf0_type = CIFS_NO_BUFFER;
1517 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1520 struct smb_rqst rqst;
1521 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1522 struct kvec rsp_iov = { NULL, 0 };
1524 /* Testing shows that buffer offset must be at location of Buffer[0] */
1525 req->SecurityBufferOffset =
1526 cpu_to_le16(sizeof(struct smb2_sess_setup_req));
1527 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1529 memset(&rqst, 0, sizeof(struct smb_rqst));
1530 rqst.rq_iov = sess_data->iov;
1533 /* BB add code to build os and lm fields */
1534 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1537 &sess_data->buf0_type,
1538 CIFS_LOG_ERROR | CIFS_SESS_OP, &rsp_iov);
1539 cifs_small_buf_release(sess_data->iov[0].iov_base);
1541 sess_data->ses->expired_pwd = false;
1542 else if ((rc == -EACCES) || (rc == -EKEYEXPIRED) || (rc == -EKEYREVOKED))
1543 sess_data->ses->expired_pwd = true;
1545 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1551 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1554 struct cifs_ses *ses = sess_data->ses;
1555 struct TCP_Server_Info *server = sess_data->server;
1557 cifs_server_lock(server);
1558 if (server->ops->generate_signingkey) {
1559 rc = server->ops->generate_signingkey(ses, server);
1562 "SMB3 session key generation failed\n");
1563 cifs_server_unlock(server);
1567 if (!server->session_estab) {
1568 server->sequence_number = 0x2;
1569 server->session_estab = true;
1571 cifs_server_unlock(server);
1573 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1577 #ifdef CONFIG_CIFS_UPCALL
1579 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1582 struct cifs_ses *ses = sess_data->ses;
1583 struct TCP_Server_Info *server = sess_data->server;
1584 struct cifs_spnego_msg *msg;
1585 struct key *spnego_key = NULL;
1586 struct smb2_sess_setup_rsp *rsp = NULL;
1587 bool is_binding = false;
1589 rc = SMB2_sess_alloc_buffer(sess_data);
1593 spnego_key = cifs_get_spnego_key(ses, server);
1594 if (IS_ERR(spnego_key)) {
1595 rc = PTR_ERR(spnego_key);
1597 cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
1602 msg = spnego_key->payload.data[0];
1604 * check version field to make sure that cifs.upcall is
1605 * sending us a response in an expected form
1607 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1608 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1609 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1611 goto out_put_spnego_key;
1614 spin_lock(&ses->ses_lock);
1615 is_binding = (ses->ses_status == SES_GOOD);
1616 spin_unlock(&ses->ses_lock);
1618 /* keep session key if binding */
1620 kfree_sensitive(ses->auth_key.response);
1621 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1623 if (!ses->auth_key.response) {
1624 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1627 goto out_put_spnego_key;
1629 ses->auth_key.len = msg->sesskey_len;
1632 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1633 sess_data->iov[1].iov_len = msg->secblob_len;
1635 rc = SMB2_sess_sendreceive(sess_data);
1637 goto out_put_spnego_key;
1639 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1640 /* keep session id and flags if binding */
1642 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1643 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1646 rc = SMB2_sess_establish_session(sess_data);
1648 key_invalidate(spnego_key);
1649 key_put(spnego_key);
1651 kfree_sensitive(ses->auth_key.response);
1652 ses->auth_key.response = NULL;
1653 ses->auth_key.len = 0;
1656 sess_data->result = rc;
1657 sess_data->func = NULL;
1658 SMB2_sess_free_buffer(sess_data);
1662 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1664 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1665 sess_data->result = -EOPNOTSUPP;
1666 sess_data->func = NULL;
1671 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1674 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1677 struct cifs_ses *ses = sess_data->ses;
1678 struct TCP_Server_Info *server = sess_data->server;
1679 struct smb2_sess_setup_rsp *rsp = NULL;
1680 unsigned char *ntlmssp_blob = NULL;
1681 bool use_spnego = false; /* else use raw ntlmssp */
1682 u16 blob_length = 0;
1683 bool is_binding = false;
1686 * If memory allocation is successful, caller of this function
1689 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1690 if (!ses->ntlmssp) {
1694 ses->ntlmssp->sesskey_per_smbsess = true;
1696 rc = SMB2_sess_alloc_buffer(sess_data);
1700 rc = build_ntlmssp_smb3_negotiate_blob(&ntlmssp_blob,
1701 &blob_length, ses, server,
1707 /* BB eventually need to add this */
1708 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1712 sess_data->iov[1].iov_base = ntlmssp_blob;
1713 sess_data->iov[1].iov_len = blob_length;
1715 rc = SMB2_sess_sendreceive(sess_data);
1716 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1718 /* If true, rc here is expected and not an error */
1719 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1720 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1726 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1727 le16_to_cpu(rsp->SecurityBufferOffset)) {
1728 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1729 le16_to_cpu(rsp->SecurityBufferOffset));
1733 rc = decode_ntlmssp_challenge(rsp->Buffer,
1734 le16_to_cpu(rsp->SecurityBufferLength), ses);
1738 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1740 spin_lock(&ses->ses_lock);
1741 is_binding = (ses->ses_status == SES_GOOD);
1742 spin_unlock(&ses->ses_lock);
1744 /* keep existing ses id and flags if binding */
1746 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1747 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1751 kfree_sensitive(ntlmssp_blob);
1752 SMB2_sess_free_buffer(sess_data);
1754 sess_data->result = 0;
1755 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1759 kfree_sensitive(ses->ntlmssp);
1760 ses->ntlmssp = NULL;
1761 sess_data->result = rc;
1762 sess_data->func = NULL;
1766 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1769 struct cifs_ses *ses = sess_data->ses;
1770 struct TCP_Server_Info *server = sess_data->server;
1771 struct smb2_sess_setup_req *req;
1772 struct smb2_sess_setup_rsp *rsp = NULL;
1773 unsigned char *ntlmssp_blob = NULL;
1774 bool use_spnego = false; /* else use raw ntlmssp */
1775 u16 blob_length = 0;
1776 bool is_binding = false;
1778 rc = SMB2_sess_alloc_buffer(sess_data);
1782 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1783 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1785 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length,
1789 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1794 /* BB eventually need to add this */
1795 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1799 sess_data->iov[1].iov_base = ntlmssp_blob;
1800 sess_data->iov[1].iov_len = blob_length;
1802 rc = SMB2_sess_sendreceive(sess_data);
1806 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1808 spin_lock(&ses->ses_lock);
1809 is_binding = (ses->ses_status == SES_GOOD);
1810 spin_unlock(&ses->ses_lock);
1812 /* keep existing ses id and flags if binding */
1814 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1815 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1818 rc = SMB2_sess_establish_session(sess_data);
1819 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1820 if (ses->server->dialect < SMB30_PROT_ID) {
1821 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1823 * The session id is opaque in terms of endianness, so we can't
1824 * print it as a long long. we dump it as we got it on the wire
1826 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1828 cifs_dbg(VFS, "Session Key %*ph\n",
1829 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1830 cifs_dbg(VFS, "Signing Key %*ph\n",
1831 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1835 kfree_sensitive(ntlmssp_blob);
1836 SMB2_sess_free_buffer(sess_data);
1837 kfree_sensitive(ses->ntlmssp);
1838 ses->ntlmssp = NULL;
1839 sess_data->result = rc;
1840 sess_data->func = NULL;
1844 SMB2_select_sec(struct SMB2_sess_data *sess_data)
1847 struct cifs_ses *ses = sess_data->ses;
1848 struct TCP_Server_Info *server = sess_data->server;
1850 type = smb2_select_sectype(server, ses->sectype);
1851 cifs_dbg(FYI, "sess setup type %d\n", type);
1852 if (type == Unspecified) {
1853 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1859 sess_data->func = SMB2_auth_kerberos;
1862 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1865 cifs_dbg(VFS, "secType %d not supported!\n", type);
1873 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1874 struct TCP_Server_Info *server,
1875 const struct nls_table *nls_cp)
1878 struct SMB2_sess_data *sess_data;
1880 cifs_dbg(FYI, "Session Setup\n");
1883 WARN(1, "%s: server is NULL!\n", __func__);
1887 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1891 sess_data->xid = xid;
1892 sess_data->ses = ses;
1893 sess_data->server = server;
1894 sess_data->buf0_type = CIFS_NO_BUFFER;
1895 sess_data->nls_cp = (struct nls_table *) nls_cp;
1896 sess_data->previous_session = ses->Suid;
1898 rc = SMB2_select_sec(sess_data);
1903 * Initialize the session hash with the server one.
1905 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
1906 SMB2_PREAUTH_HASH_SIZE);
1908 while (sess_data->func)
1909 sess_data->func(sess_data);
1911 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1912 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1913 rc = sess_data->result;
1915 kfree_sensitive(sess_data);
1920 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1922 struct smb_rqst rqst;
1923 struct smb2_logoff_req *req; /* response is also trivial struct */
1925 struct TCP_Server_Info *server;
1927 unsigned int total_len;
1929 struct kvec rsp_iov;
1932 cifs_dbg(FYI, "disconnect session %p\n", ses);
1934 if (ses && (ses->server))
1935 server = ses->server;
1939 /* no need to send SMB logoff if uid already closed due to reconnect */
1940 spin_lock(&ses->chan_lock);
1941 if (CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
1942 spin_unlock(&ses->chan_lock);
1943 goto smb2_session_already_dead;
1945 spin_unlock(&ses->chan_lock);
1947 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
1948 (void **) &req, &total_len);
1952 /* since no tcon, smb2_init can not do this, so do here */
1953 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1955 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1956 flags |= CIFS_TRANSFORM_REQ;
1957 else if (server->sign)
1958 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1960 flags |= CIFS_NO_RSP_BUF;
1962 iov[0].iov_base = (char *)req;
1963 iov[0].iov_len = total_len;
1965 memset(&rqst, 0, sizeof(struct smb_rqst));
1969 rc = cifs_send_recv(xid, ses, ses->server,
1970 &rqst, &resp_buf_type, flags, &rsp_iov);
1971 cifs_small_buf_release(req);
1973 * No tcon so can't do
1974 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1977 smb2_session_already_dead:
1981 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1983 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1986 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1988 /* These are similar values to what Windows uses */
1989 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1991 tcon->max_chunks = 256;
1992 tcon->max_bytes_chunk = 1048576;
1993 tcon->max_bytes_copy = 16777216;
1997 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1998 struct cifs_tcon *tcon, const struct nls_table *cp)
2000 struct smb_rqst rqst;
2001 struct smb2_tree_connect_req *req;
2002 struct smb2_tree_connect_rsp *rsp = NULL;
2004 struct kvec rsp_iov = { NULL, 0 };
2008 __le16 *unc_path = NULL;
2010 unsigned int total_len;
2011 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2013 cifs_dbg(FYI, "TCON\n");
2015 if (!server || !tree)
2018 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
2019 if (unc_path == NULL)
2022 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp);
2023 if (unc_path_len <= 0) {
2029 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
2031 atomic_set(&tcon->num_remote_opens, 0);
2032 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
2033 (void **) &req, &total_len);
2039 if (smb3_encryption_required(tcon))
2040 flags |= CIFS_TRANSFORM_REQ;
2042 iov[0].iov_base = (char *)req;
2044 iov[0].iov_len = total_len - 1;
2046 /* Testing shows that buffer offset must be at location of Buffer[0] */
2047 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req));
2048 req->PathLength = cpu_to_le16(unc_path_len);
2049 iov[1].iov_base = unc_path;
2050 iov[1].iov_len = unc_path_len;
2053 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
2054 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
2055 * (Samba servers don't always set the flag so also check if null user)
2057 if ((server->dialect == SMB311_PROT_ID) &&
2058 !smb3_encryption_required(tcon) &&
2059 !(ses->session_flags &
2060 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
2061 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
2062 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
2064 memset(&rqst, 0, sizeof(struct smb_rqst));
2068 /* Need 64 for max size write so ask for more in case not there yet */
2069 if (server->credits >= server->max_credits)
2070 req->hdr.CreditRequest = cpu_to_le16(0);
2072 req->hdr.CreditRequest = cpu_to_le16(
2073 min_t(int, server->max_credits -
2074 server->credits, 64));
2076 rc = cifs_send_recv(xid, ses, server,
2077 &rqst, &resp_buftype, flags, &rsp_iov);
2078 cifs_small_buf_release(req);
2079 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
2080 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
2081 if ((rc != 0) || (rsp == NULL)) {
2082 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
2083 tcon->need_reconnect = true;
2084 goto tcon_error_exit;
2087 switch (rsp->ShareType) {
2088 case SMB2_SHARE_TYPE_DISK:
2089 cifs_dbg(FYI, "connection to disk share\n");
2091 case SMB2_SHARE_TYPE_PIPE:
2093 cifs_dbg(FYI, "connection to pipe share\n");
2095 case SMB2_SHARE_TYPE_PRINT:
2097 cifs_dbg(FYI, "connection to printer\n");
2100 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
2102 goto tcon_error_exit;
2105 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
2106 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
2107 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
2108 tcon->tid = le32_to_cpu(rsp->hdr.Id.SyncId.TreeId);
2109 strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name));
2111 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
2112 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
2113 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
2116 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
2117 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
2119 init_copy_chunk_defaults(tcon);
2120 if (server->ops->validate_negotiate)
2121 rc = server->ops->validate_negotiate(xid, tcon);
2122 if (rc == 0) /* See MS-SMB2 2.2.10 and 3.2.5.5 */
2123 if (tcon->share_flags & SMB2_SHAREFLAG_ISOLATED_TRANSPORT)
2124 server->nosharesock = true;
2127 free_rsp_buf(resp_buftype, rsp);
2132 if (rsp && rsp->hdr.Status == STATUS_BAD_NETWORK_NAME)
2133 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
2138 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
2140 struct smb_rqst rqst;
2141 struct smb2_tree_disconnect_req *req; /* response is trivial */
2143 struct cifs_ses *ses = tcon->ses;
2144 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2146 unsigned int total_len;
2148 struct kvec rsp_iov;
2151 cifs_dbg(FYI, "Tree Disconnect\n");
2153 if (!ses || !(ses->server))
2156 trace_smb3_tdis_enter(xid, tcon->tid, ses->Suid, tcon->tree_name);
2157 spin_lock(&ses->chan_lock);
2158 if ((tcon->need_reconnect) ||
2159 (CIFS_ALL_CHANS_NEED_RECONNECT(tcon->ses))) {
2160 spin_unlock(&ses->chan_lock);
2163 spin_unlock(&ses->chan_lock);
2165 invalidate_all_cached_dirs(tcon);
2167 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, server,
2173 if (smb3_encryption_required(tcon))
2174 flags |= CIFS_TRANSFORM_REQ;
2176 flags |= CIFS_NO_RSP_BUF;
2178 iov[0].iov_base = (char *)req;
2179 iov[0].iov_len = total_len;
2181 memset(&rqst, 0, sizeof(struct smb_rqst));
2185 rc = cifs_send_recv(xid, ses, server,
2186 &rqst, &resp_buf_type, flags, &rsp_iov);
2187 cifs_small_buf_release(req);
2189 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
2190 trace_smb3_tdis_err(xid, tcon->tid, ses->Suid, rc);
2192 trace_smb3_tdis_done(xid, tcon->tid, ses->Suid);
2198 static struct create_durable *
2199 create_durable_buf(void)
2201 struct create_durable *buf;
2203 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2207 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2208 (struct create_durable, Data));
2209 buf->ccontext.DataLength = cpu_to_le32(16);
2210 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2211 (struct create_durable, Name));
2212 buf->ccontext.NameLength = cpu_to_le16(4);
2213 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
2221 static struct create_durable *
2222 create_reconnect_durable_buf(struct cifs_fid *fid)
2224 struct create_durable *buf;
2226 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2230 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2231 (struct create_durable, Data));
2232 buf->ccontext.DataLength = cpu_to_le32(16);
2233 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2234 (struct create_durable, Name));
2235 buf->ccontext.NameLength = cpu_to_le16(4);
2236 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
2237 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
2238 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
2247 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
2249 struct create_disk_id_rsp *pdisk_id = (struct create_disk_id_rsp *)cc;
2251 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
2252 pdisk_id->DiskFileId, pdisk_id->VolumeId);
2253 buf->IndexNumber = pdisk_id->DiskFileId;
2257 parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
2258 struct create_posix_rsp *posix)
2261 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
2262 u8 *end = beg + le32_to_cpu(cc->DataLength);
2265 memset(posix, 0, sizeof(*posix));
2267 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
2268 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
2269 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
2272 sid_len = posix_info_sid_size(sid, end);
2274 cifs_dbg(VFS, "bad owner sid in posix create response\n");
2277 memcpy(&posix->owner, sid, sid_len);
2279 sid = sid + sid_len;
2280 sid_len = posix_info_sid_size(sid, end);
2282 cifs_dbg(VFS, "bad group sid in posix create response\n");
2285 memcpy(&posix->group, sid, sid_len);
2287 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
2288 posix->nlink, posix->mode, posix->reparse_tag);
2291 int smb2_parse_contexts(struct TCP_Server_Info *server,
2292 struct kvec *rsp_iov,
2293 unsigned int *epoch,
2294 char *lease_key, __u8 *oplock,
2295 struct smb2_file_all_info *buf,
2296 struct create_posix_rsp *posix)
2298 struct smb2_create_rsp *rsp = rsp_iov->iov_base;
2299 struct create_context *cc;
2300 size_t rem, off, len;
2304 static const char smb3_create_tag_posix[] = {
2305 0x93, 0xAD, 0x25, 0x50, 0x9C,
2306 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
2307 0xDE, 0x96, 0x8B, 0xCD, 0x7C
2312 off = le32_to_cpu(rsp->CreateContextsOffset);
2313 rem = le32_to_cpu(rsp->CreateContextsLength);
2314 if (check_add_overflow(off, rem, &len) || len > rsp_iov->iov_len)
2316 cc = (struct create_context *)((u8 *)rsp + off);
2318 /* Initialize inode number to 0 in case no valid data in qfid context */
2320 buf->IndexNumber = 0;
2322 while (rem >= sizeof(*cc)) {
2323 doff = le16_to_cpu(cc->DataOffset);
2324 dlen = le32_to_cpu(cc->DataLength);
2325 if (check_add_overflow(doff, dlen, &len) || len > rem)
2328 noff = le16_to_cpu(cc->NameOffset);
2329 nlen = le16_to_cpu(cc->NameLength);
2330 if (noff + nlen > doff)
2333 name = (char *)cc + noff;
2336 if (!strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4)) {
2337 *oplock = server->ops->parse_lease_buf(cc, epoch,
2340 !strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4)) {
2341 parse_query_id_ctxt(cc, buf);
2345 if (posix && !memcmp(name, smb3_create_tag_posix, 16))
2346 parse_posix_ctxt(cc, buf, posix);
2349 cifs_dbg(FYI, "%s: unhandled context (nlen=%zu dlen=%zu)\n",
2350 __func__, nlen, dlen);
2351 if (IS_ENABLED(CONFIG_CIFS_DEBUG2))
2352 cifs_dump_mem("context data: ", cc, dlen);
2356 off = le32_to_cpu(cc->Next);
2359 if (check_sub_overflow(rem, off, &rem))
2361 cc = (struct create_context *)((u8 *)cc + off);
2364 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2365 *oplock = rsp->OplockLevel;
2371 add_lease_context(struct TCP_Server_Info *server,
2372 struct smb2_create_req *req,
2374 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
2376 unsigned int num = *num_iovec;
2378 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
2379 if (iov[num].iov_base == NULL)
2381 iov[num].iov_len = server->vals->create_lease_size;
2382 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2383 *num_iovec = num + 1;
2387 static struct create_durable_v2 *
2388 create_durable_v2_buf(struct cifs_open_parms *oparms)
2390 struct cifs_fid *pfid = oparms->fid;
2391 struct create_durable_v2 *buf;
2393 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2397 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2398 (struct create_durable_v2, dcontext));
2399 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2400 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2401 (struct create_durable_v2, Name));
2402 buf->ccontext.NameLength = cpu_to_le16(4);
2405 * NB: Handle timeout defaults to 0, which allows server to choose
2406 * (most servers default to 120 seconds) and most clients default to 0.
2407 * This can be overridden at mount ("handletimeout=") if the user wants
2408 * a different persistent (or resilient) handle timeout for all opens
2409 * on a particular SMB3 mount.
2411 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2412 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2414 /* for replay, we should not overwrite the existing create guid */
2415 if (!oparms->replay) {
2416 generate_random_uuid(buf->dcontext.CreateGuid);
2417 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2419 memcpy(buf->dcontext.CreateGuid, pfid->create_guid, 16);
2421 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2429 static struct create_durable_handle_reconnect_v2 *
2430 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2432 struct create_durable_handle_reconnect_v2 *buf;
2434 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2439 buf->ccontext.DataOffset =
2440 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2442 buf->ccontext.DataLength =
2443 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2444 buf->ccontext.NameOffset =
2445 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2447 buf->ccontext.NameLength = cpu_to_le16(4);
2449 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2450 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2451 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2452 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2454 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2463 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2464 struct cifs_open_parms *oparms)
2466 unsigned int num = *num_iovec;
2468 iov[num].iov_base = create_durable_v2_buf(oparms);
2469 if (iov[num].iov_base == NULL)
2471 iov[num].iov_len = sizeof(struct create_durable_v2);
2472 *num_iovec = num + 1;
2477 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2478 struct cifs_open_parms *oparms)
2480 unsigned int num = *num_iovec;
2482 /* indicate that we don't need to relock the file */
2483 oparms->reconnect = false;
2485 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2486 if (iov[num].iov_base == NULL)
2488 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2489 *num_iovec = num + 1;
2494 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2495 struct cifs_open_parms *oparms, bool use_persistent)
2497 unsigned int num = *num_iovec;
2499 if (use_persistent) {
2500 if (oparms->reconnect)
2501 return add_durable_reconnect_v2_context(iov, num_iovec,
2504 return add_durable_v2_context(iov, num_iovec, oparms);
2507 if (oparms->reconnect) {
2508 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2509 /* indicate that we don't need to relock the file */
2510 oparms->reconnect = false;
2512 iov[num].iov_base = create_durable_buf();
2513 if (iov[num].iov_base == NULL)
2515 iov[num].iov_len = sizeof(struct create_durable);
2516 *num_iovec = num + 1;
2520 /* See MS-SMB2 2.2.13.2.7 */
2521 static struct crt_twarp_ctxt *
2522 create_twarp_buf(__u64 timewarp)
2524 struct crt_twarp_ctxt *buf;
2526 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2530 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2531 (struct crt_twarp_ctxt, Timestamp));
2532 buf->ccontext.DataLength = cpu_to_le32(8);
2533 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2534 (struct crt_twarp_ctxt, Name));
2535 buf->ccontext.NameLength = cpu_to_le16(4);
2536 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2541 buf->Timestamp = cpu_to_le64(timewarp);
2545 /* See MS-SMB2 2.2.13.2.7 */
2547 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2549 unsigned int num = *num_iovec;
2551 iov[num].iov_base = create_twarp_buf(timewarp);
2552 if (iov[num].iov_base == NULL)
2554 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2555 *num_iovec = num + 1;
2559 /* See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
2560 static void setup_owner_group_sids(char *buf)
2562 struct owner_group_sids *sids = (struct owner_group_sids *)buf;
2564 /* Populate the user ownership fields S-1-5-88-1 */
2565 sids->owner.Revision = 1;
2566 sids->owner.NumAuth = 3;
2567 sids->owner.Authority[5] = 5;
2568 sids->owner.SubAuthorities[0] = cpu_to_le32(88);
2569 sids->owner.SubAuthorities[1] = cpu_to_le32(1);
2570 sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val);
2572 /* Populate the group ownership fields S-1-5-88-2 */
2573 sids->group.Revision = 1;
2574 sids->group.NumAuth = 3;
2575 sids->group.Authority[5] = 5;
2576 sids->group.SubAuthorities[0] = cpu_to_le32(88);
2577 sids->group.SubAuthorities[1] = cpu_to_le32(2);
2578 sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val);
2580 cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val);
2583 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2584 static struct crt_sd_ctxt *
2585 create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
2587 struct crt_sd_ctxt *buf;
2589 unsigned int acelen, acl_size, ace_count;
2590 unsigned int owner_offset = 0;
2591 unsigned int group_offset = 0;
2592 struct smb3_acl acl = {};
2594 *len = round_up(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 4), 8);
2597 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
2598 *len += sizeof(struct owner_group_sids);
2601 buf = kzalloc(*len, GFP_KERNEL);
2605 ptr = (__u8 *)&buf[1];
2607 /* offset fields are from beginning of security descriptor not of create context */
2608 owner_offset = ptr - (__u8 *)&buf->sd;
2609 buf->sd.OffsetOwner = cpu_to_le32(owner_offset);
2610 group_offset = owner_offset + offsetof(struct owner_group_sids, group);
2611 buf->sd.OffsetGroup = cpu_to_le32(group_offset);
2613 setup_owner_group_sids(ptr);
2614 ptr += sizeof(struct owner_group_sids);
2616 buf->sd.OffsetOwner = 0;
2617 buf->sd.OffsetGroup = 0;
2620 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, sd));
2621 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name));
2622 buf->ccontext.NameLength = cpu_to_le16(4);
2623 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2628 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2631 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2632 * and "DP" ie the DACL is present
2634 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2636 /* offset owner, group and Sbz1 and SACL are all zero */
2637 buf->sd.OffsetDacl = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2638 /* Ship the ACL for now. we will copy it into buf later. */
2640 ptr += sizeof(struct smb3_acl);
2642 /* create one ACE to hold the mode embedded in reserved special SID */
2643 acelen = setup_special_mode_ACE((struct cifs_ace *)ptr, (__u64)mode);
2645 acl_size = acelen + sizeof(struct smb3_acl);
2649 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
2650 acelen = setup_special_user_owner_ACE((struct cifs_ace *)ptr);
2656 /* and one more ACE to allow access for authenticated users */
2657 acelen = setup_authusers_ACE((struct cifs_ace *)ptr);
2662 acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2663 acl.AclSize = cpu_to_le16(acl_size);
2664 acl.AceCount = cpu_to_le16(ace_count);
2665 /* acl.Sbz1 and Sbz2 MBZ so are not set here, but initialized above */
2666 memcpy(aclptr, &acl, sizeof(struct smb3_acl));
2668 buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2669 *len = round_up((unsigned int)(ptr - (__u8 *)buf), 8);
2675 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner)
2677 unsigned int num = *num_iovec;
2678 unsigned int len = 0;
2680 iov[num].iov_base = create_sd_buf(mode, set_owner, &len);
2681 if (iov[num].iov_base == NULL)
2683 iov[num].iov_len = len;
2684 *num_iovec = num + 1;
2688 static struct crt_query_id_ctxt *
2689 create_query_id_buf(void)
2691 struct crt_query_id_ctxt *buf;
2693 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2697 buf->ccontext.DataOffset = cpu_to_le16(0);
2698 buf->ccontext.DataLength = cpu_to_le32(0);
2699 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2700 (struct crt_query_id_ctxt, Name));
2701 buf->ccontext.NameLength = cpu_to_le16(4);
2702 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2710 /* See MS-SMB2 2.2.13.2.9 */
2712 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2714 unsigned int num = *num_iovec;
2716 iov[num].iov_base = create_query_id_buf();
2717 if (iov[num].iov_base == NULL)
2719 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2720 *num_iovec = num + 1;
2725 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2726 const char *treename, const __le16 *path)
2728 int treename_len, path_len;
2729 struct nls_table *cp;
2730 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2735 treename_len = strlen(treename);
2736 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2742 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2744 /* make room for one path separator only if @path isn't empty */
2745 *out_len = treename_len + (path[0] ? 1 : 0) + path_len;
2748 * final path needs to be 8-byte aligned as specified in
2749 * MS-SMB2 2.2.13 SMB2 CREATE Request.
2751 *out_size = round_up(*out_len * sizeof(__le16), 8);
2752 *out_path = kzalloc(*out_size + sizeof(__le16) /* null */, GFP_KERNEL);
2756 cp = load_nls_default();
2757 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2759 /* Do not append the separator if the path is empty */
2760 if (path[0] != cpu_to_le16(0x0000)) {
2761 UniStrcat((wchar_t *)*out_path, (wchar_t *)sep);
2762 UniStrcat((wchar_t *)*out_path, (wchar_t *)path);
2770 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2771 umode_t mode, struct cifs_tcon *tcon,
2772 const char *full_path,
2773 struct cifs_sb_info *cifs_sb)
2775 struct smb_rqst rqst;
2776 struct smb2_create_req *req;
2777 struct smb2_create_rsp *rsp = NULL;
2778 struct cifs_ses *ses = tcon->ses;
2779 struct kvec iov[3]; /* make sure at least one for each open context */
2780 struct kvec rsp_iov = {NULL, 0};
2783 __le16 *copy_path = NULL;
2786 unsigned int n_iov = 2;
2787 __u32 file_attributes = 0;
2788 char *pc_buf = NULL;
2790 unsigned int total_len;
2791 __le16 *utf16_path = NULL;
2792 struct TCP_Server_Info *server;
2793 int retries = 0, cur_sleep = 1;
2796 /* reinitialize for possible replay */
2799 server = cifs_pick_channel(ses);
2801 cifs_dbg(FYI, "mkdir\n");
2803 /* resource #1: path allocation */
2804 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2808 if (!ses || !server) {
2813 /* resource #2: request */
2814 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2815 (void **) &req, &total_len);
2820 if (smb3_encryption_required(tcon))
2821 flags |= CIFS_TRANSFORM_REQ;
2823 req->ImpersonationLevel = IL_IMPERSONATION;
2824 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2825 /* File attributes ignored on open (used in create though) */
2826 req->FileAttributes = cpu_to_le32(file_attributes);
2827 req->ShareAccess = FILE_SHARE_ALL_LE;
2828 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2829 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2831 iov[0].iov_base = (char *)req;
2832 /* -1 since last byte is buf[0] which is sent below (path) */
2833 iov[0].iov_len = total_len - 1;
2835 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2837 /* [MS-SMB2] 2.2.13 NameOffset:
2838 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2839 * the SMB2 header, the file name includes a prefix that will
2840 * be processed during DFS name normalization as specified in
2841 * section 3.3.5.9. Otherwise, the file name is relative to
2842 * the share that is identified by the TreeId in the SMB2
2845 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2848 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2849 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2851 tcon->tree_name, utf16_path);
2855 req->NameLength = cpu_to_le16(name_len * 2);
2856 uni_path_len = copy_size;
2857 /* free before overwriting resource */
2859 utf16_path = copy_path;
2861 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2862 /* MUST set path len (NameLength) to 0 opening root of share */
2863 req->NameLength = cpu_to_le16(uni_path_len - 2);
2864 if (uni_path_len % 8 != 0) {
2865 copy_size = roundup(uni_path_len, 8);
2866 copy_path = kzalloc(copy_size, GFP_KERNEL);
2871 memcpy((char *)copy_path, (const char *)utf16_path,
2873 uni_path_len = copy_size;
2874 /* free before overwriting resource */
2876 utf16_path = copy_path;
2880 iov[1].iov_len = uni_path_len;
2881 iov[1].iov_base = utf16_path;
2882 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2884 if (tcon->posix_extensions) {
2885 /* resource #3: posix buf */
2886 rc = add_posix_context(iov, &n_iov, mode);
2889 req->CreateContextsOffset = cpu_to_le32(
2890 sizeof(struct smb2_create_req) +
2892 pc_buf = iov[n_iov-1].iov_base;
2896 memset(&rqst, 0, sizeof(struct smb_rqst));
2898 rqst.rq_nvec = n_iov;
2900 /* no need to inc num_remote_opens because we close it just below */
2901 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, full_path, CREATE_NOT_FILE,
2902 FILE_WRITE_ATTRIBUTES);
2905 smb2_set_replay(server, &rqst);
2907 /* resource #4: response buffer */
2908 rc = cifs_send_recv(xid, ses, server,
2909 &rqst, &resp_buftype, flags, &rsp_iov);
2911 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2912 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2914 FILE_WRITE_ATTRIBUTES, rc);
2915 goto err_free_rsp_buf;
2919 * Although unlikely to be possible for rsp to be null and rc not set,
2920 * adding check below is slightly safer long term (and quiets Coverity
2923 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2930 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
2931 CREATE_NOT_FILE, FILE_WRITE_ATTRIBUTES);
2933 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2935 /* Eventually save off posix specific response info and timestaps */
2938 free_rsp_buf(resp_buftype, rsp);
2941 cifs_small_buf_release(req);
2945 if (is_replayable_error(rc) &&
2946 smb2_should_replay(tcon, &retries, &cur_sleep))
2953 SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2954 struct smb_rqst *rqst, __u8 *oplock,
2955 struct cifs_open_parms *oparms, __le16 *path)
2957 struct smb2_create_req *req;
2958 unsigned int n_iov = 2;
2959 __u32 file_attributes = 0;
2962 unsigned int total_len;
2963 struct kvec *iov = rqst->rq_iov;
2967 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2968 (void **) &req, &total_len);
2972 iov[0].iov_base = (char *)req;
2973 /* -1 since last byte is buf[0] which is sent below (path) */
2974 iov[0].iov_len = total_len - 1;
2976 if (oparms->create_options & CREATE_OPTION_READONLY)
2977 file_attributes |= ATTR_READONLY;
2978 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2979 file_attributes |= ATTR_SYSTEM;
2981 req->ImpersonationLevel = IL_IMPERSONATION;
2982 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2983 /* File attributes ignored on open (used in create though) */
2984 req->FileAttributes = cpu_to_le32(file_attributes);
2985 req->ShareAccess = FILE_SHARE_ALL_LE;
2987 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2988 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2989 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2991 /* [MS-SMB2] 2.2.13 NameOffset:
2992 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2993 * the SMB2 header, the file name includes a prefix that will
2994 * be processed during DFS name normalization as specified in
2995 * section 3.3.5.9. Otherwise, the file name is relative to
2996 * the share that is identified by the TreeId in the SMB2
2999 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
3002 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
3003 rc = alloc_path_with_tree_prefix(©_path, ©_size,
3005 tcon->tree_name, path);
3008 req->NameLength = cpu_to_le16(name_len * 2);
3009 uni_path_len = copy_size;
3012 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
3013 /* MUST set path len (NameLength) to 0 opening root of share */
3014 req->NameLength = cpu_to_le16(uni_path_len - 2);
3015 copy_size = round_up(uni_path_len, 8);
3016 copy_path = kzalloc(copy_size, GFP_KERNEL);
3019 memcpy((char *)copy_path, (const char *)path,
3021 uni_path_len = copy_size;
3025 iov[1].iov_len = uni_path_len;
3026 iov[1].iov_base = path;
3028 if ((!server->oplocks) || (tcon->no_lease))
3029 *oplock = SMB2_OPLOCK_LEVEL_NONE;
3031 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
3032 *oplock == SMB2_OPLOCK_LEVEL_NONE)
3033 req->RequestedOplockLevel = *oplock;
3034 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
3035 (oparms->create_options & CREATE_NOT_FILE))
3036 req->RequestedOplockLevel = *oplock; /* no srv lease support */
3038 rc = add_lease_context(server, req, iov, &n_iov,
3039 oparms->fid->lease_key, oplock);
3044 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3045 rc = add_durable_context(iov, &n_iov, oparms,
3046 tcon->use_persistent);
3051 if (tcon->posix_extensions) {
3052 rc = add_posix_context(iov, &n_iov, oparms->mode);
3057 if (tcon->snapshot_time) {
3058 cifs_dbg(FYI, "adding snapshot context\n");
3059 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
3064 if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) {
3068 if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
3069 (oparms->mode != ACL_NO_MODE))
3073 oparms->mode = ACL_NO_MODE;
3076 if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
3081 if (set_owner | set_mode) {
3082 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
3083 rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner);
3089 add_query_id_context(iov, &n_iov);
3093 * We have create contexts behind iov[1] (the file
3094 * name), point at them from the main create request
3096 req->CreateContextsOffset = cpu_to_le32(
3097 sizeof(struct smb2_create_req) +
3099 req->CreateContextsLength = 0;
3101 for (unsigned int i = 2; i < (n_iov-1); i++) {
3102 struct kvec *v = &iov[i];
3103 size_t len = v->iov_len;
3104 struct create_context *cctx =
3105 (struct create_context *)v->iov_base;
3107 cctx->Next = cpu_to_le32(len);
3108 le32_add_cpu(&req->CreateContextsLength, len);
3110 le32_add_cpu(&req->CreateContextsLength,
3111 iov[n_iov-1].iov_len);
3114 rqst->rq_nvec = n_iov;
3118 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
3119 * All other vectors are freed by kfree().
3122 SMB2_open_free(struct smb_rqst *rqst)
3126 if (rqst && rqst->rq_iov) {
3127 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
3128 for (i = 1; i < rqst->rq_nvec; i++)
3129 if (rqst->rq_iov[i].iov_base != smb2_padding)
3130 kfree(rqst->rq_iov[i].iov_base);
3135 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
3136 __u8 *oplock, struct smb2_file_all_info *buf,
3137 struct create_posix_rsp *posix,
3138 struct kvec *err_iov, int *buftype)
3140 struct smb_rqst rqst;
3141 struct smb2_create_rsp *rsp = NULL;
3142 struct cifs_tcon *tcon = oparms->tcon;
3143 struct cifs_ses *ses = tcon->ses;
3144 struct TCP_Server_Info *server;
3145 struct kvec iov[SMB2_CREATE_IOV_SIZE];
3146 struct kvec rsp_iov = {NULL, 0};
3147 int resp_buftype = CIFS_NO_BUFFER;
3150 int retries = 0, cur_sleep = 1;
3153 /* reinitialize for possible replay */
3155 server = cifs_pick_channel(ses);
3156 oparms->replay = !!(retries);
3158 cifs_dbg(FYI, "create/open\n");
3159 if (!ses || !server)
3162 if (smb3_encryption_required(tcon))
3163 flags |= CIFS_TRANSFORM_REQ;
3165 memset(&rqst, 0, sizeof(struct smb_rqst));
3166 memset(&iov, 0, sizeof(iov));
3168 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
3170 rc = SMB2_open_init(tcon, server,
3171 &rqst, oplock, oparms, path);
3175 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid, oparms->path,
3176 oparms->create_options, oparms->desired_access);
3179 smb2_set_replay(server, &rqst);
3181 rc = cifs_send_recv(xid, ses, server,
3182 &rqst, &resp_buftype, flags,
3184 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
3187 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
3188 if (err_iov && rsp) {
3190 *buftype = resp_buftype;
3191 resp_buftype = CIFS_NO_BUFFER;
3194 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
3195 oparms->create_options, oparms->desired_access, rc);
3196 if (rc == -EREMCHG) {
3197 pr_warn_once("server share %s deleted\n",
3199 tcon->need_reconnect = true;
3202 } else if (rsp == NULL) /* unlikely to happen, but safer to check */
3205 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
3206 oparms->create_options, oparms->desired_access);
3208 atomic_inc(&tcon->num_remote_opens);
3209 oparms->fid->persistent_fid = rsp->PersistentFileId;
3210 oparms->fid->volatile_fid = rsp->VolatileFileId;
3211 oparms->fid->access = oparms->desired_access;
3212 #ifdef CONFIG_CIFS_DEBUG2
3213 oparms->fid->mid = le64_to_cpu(rsp->hdr.MessageId);
3214 #endif /* CIFS_DEBUG2 */
3217 buf->CreationTime = rsp->CreationTime;
3218 buf->LastAccessTime = rsp->LastAccessTime;
3219 buf->LastWriteTime = rsp->LastWriteTime;
3220 buf->ChangeTime = rsp->ChangeTime;
3221 buf->AllocationSize = rsp->AllocationSize;
3222 buf->EndOfFile = rsp->EndofFile;
3223 buf->Attributes = rsp->FileAttributes;
3224 buf->NumberOfLinks = cpu_to_le32(1);
3225 buf->DeletePending = 0;
3229 rc = smb2_parse_contexts(server, &rsp_iov, &oparms->fid->epoch,
3230 oparms->fid->lease_key, oplock, buf, posix);
3232 SMB2_open_free(&rqst);
3233 free_rsp_buf(resp_buftype, rsp);
3235 if (is_replayable_error(rc) &&
3236 smb2_should_replay(tcon, &retries, &cur_sleep))
3243 SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3244 struct smb_rqst *rqst,
3245 u64 persistent_fid, u64 volatile_fid, u32 opcode,
3246 char *in_data, u32 indatalen,
3247 __u32 max_response_size)
3249 struct smb2_ioctl_req *req;
3250 struct kvec *iov = rqst->rq_iov;
3251 unsigned int total_len;
3255 rc = smb2_ioctl_req_init(opcode, tcon, server,
3256 (void **) &req, &total_len);
3262 * indatalen is usually small at a couple of bytes max, so
3263 * just allocate through generic pool
3265 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
3267 cifs_small_buf_release(req);
3272 req->CtlCode = cpu_to_le32(opcode);
3273 req->PersistentFileId = persistent_fid;
3274 req->VolatileFileId = volatile_fid;
3276 iov[0].iov_base = (char *)req;
3278 * If no input data, the size of ioctl struct in
3279 * protocol spec still includes a 1 byte data buffer,
3280 * but if input data passed to ioctl, we do not
3281 * want to double count this, so we do not send
3282 * the dummy one byte of data in iovec[0] if sending
3283 * input data (in iovec[1]).
3286 req->InputCount = cpu_to_le32(indatalen);
3287 /* do not set InputOffset if no input data */
3289 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
3291 iov[0].iov_len = total_len - 1;
3292 iov[1].iov_base = in_data_buf;
3293 iov[1].iov_len = indatalen;
3296 iov[0].iov_len = total_len;
3299 req->OutputOffset = 0;
3300 req->OutputCount = 0; /* MBZ */
3303 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
3304 * We Could increase default MaxOutputResponse, but that could require
3305 * more credits. Windows typically sets this smaller, but for some
3306 * ioctls it may be useful to allow server to send more. No point
3307 * limiting what the server can send as long as fits in one credit
3308 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
3309 * to increase this limit up in the future.
3310 * Note that for snapshot queries that servers like Azure expect that
3311 * the first query be minimal size (and just used to get the number/size
3312 * of previous versions) so response size must be specified as EXACTLY
3313 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
3314 * of eight bytes. Currently that is the only case where we set max
3315 * response size smaller.
3317 req->MaxOutputResponse = cpu_to_le32(max_response_size);
3318 req->hdr.CreditCharge =
3319 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
3320 SMB2_MAX_BUFFER_SIZE));
3321 /* always an FSCTL (for now) */
3322 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
3324 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
3325 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
3326 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
3332 SMB2_ioctl_free(struct smb_rqst *rqst)
3336 if (rqst && rqst->rq_iov) {
3337 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3338 for (i = 1; i < rqst->rq_nvec; i++)
3339 if (rqst->rq_iov[i].iov_base != smb2_padding)
3340 kfree(rqst->rq_iov[i].iov_base);
3346 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
3349 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3350 u64 volatile_fid, u32 opcode, char *in_data, u32 indatalen,
3351 u32 max_out_data_len, char **out_data,
3352 u32 *plen /* returned data len */)
3354 struct smb_rqst rqst;
3355 struct smb2_ioctl_rsp *rsp = NULL;
3356 struct cifs_ses *ses;
3357 struct TCP_Server_Info *server;
3358 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
3359 struct kvec rsp_iov = {NULL, 0};
3360 int resp_buftype = CIFS_NO_BUFFER;
3363 int retries = 0, cur_sleep = 1;
3373 /* reinitialize for possible replay */
3375 server = cifs_pick_channel(ses);
3380 cifs_dbg(FYI, "SMB2 IOCTL\n");
3382 if (out_data != NULL)
3385 /* zero out returned data len, in case of error */
3389 if (smb3_encryption_required(tcon))
3390 flags |= CIFS_TRANSFORM_REQ;
3392 memset(&rqst, 0, sizeof(struct smb_rqst));
3393 memset(&iov, 0, sizeof(iov));
3395 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
3397 rc = SMB2_ioctl_init(tcon, server,
3398 &rqst, persistent_fid, volatile_fid, opcode,
3399 in_data, indatalen, max_out_data_len);
3404 smb2_set_replay(server, &rqst);
3406 rc = cifs_send_recv(xid, ses, server,
3407 &rqst, &resp_buftype, flags,
3409 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
3412 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3413 ses->Suid, 0, opcode, rc);
3415 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
3416 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3418 } else if (rc == -EINVAL) {
3419 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3420 (opcode != FSCTL_SRV_COPYCHUNK)) {
3421 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3424 } else if (rc == -E2BIG) {
3425 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3426 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3431 /* check if caller wants to look at return data or just return rc */
3432 if ((plen == NULL) || (out_data == NULL))
3436 * Although unlikely to be possible for rsp to be null and rc not set,
3437 * adding check below is slightly safer long term (and quiets Coverity
3445 *plen = le32_to_cpu(rsp->OutputCount);
3447 /* We check for obvious errors in the output buffer length and offset */
3449 goto ioctl_exit; /* server returned no data */
3450 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
3451 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
3457 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
3458 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
3459 le32_to_cpu(rsp->OutputOffset));
3465 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3467 if (*out_data == NULL) {
3473 SMB2_ioctl_free(&rqst);
3474 free_rsp_buf(resp_buftype, rsp);
3476 if (is_replayable_error(rc) &&
3477 smb2_should_replay(tcon, &retries, &cur_sleep))
3484 * Individual callers to ioctl worker function follow
3488 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3489 u64 persistent_fid, u64 volatile_fid)
3492 struct compress_ioctl fsctl_input;
3493 char *ret_data = NULL;
3495 fsctl_input.CompressionState =
3496 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3498 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3499 FSCTL_SET_COMPRESSION,
3500 (char *)&fsctl_input /* data input */,
3501 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3502 &ret_data /* out data */, NULL);
3504 cifs_dbg(FYI, "set compression rc %d\n", rc);
3510 SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3511 struct smb_rqst *rqst,
3512 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
3514 struct smb2_close_req *req;
3515 struct kvec *iov = rqst->rq_iov;
3516 unsigned int total_len;
3519 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
3520 (void **) &req, &total_len);
3524 req->PersistentFileId = persistent_fid;
3525 req->VolatileFileId = volatile_fid;
3527 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3530 iov[0].iov_base = (char *)req;
3531 iov[0].iov_len = total_len;
3537 SMB2_close_free(struct smb_rqst *rqst)
3539 if (rqst && rqst->rq_iov)
3540 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3544 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3545 u64 persistent_fid, u64 volatile_fid,
3546 struct smb2_file_network_open_info *pbuf)
3548 struct smb_rqst rqst;
3549 struct smb2_close_rsp *rsp = NULL;
3550 struct cifs_ses *ses = tcon->ses;
3551 struct TCP_Server_Info *server;
3553 struct kvec rsp_iov;
3554 int resp_buftype = CIFS_NO_BUFFER;
3557 bool query_attrs = false;
3558 int retries = 0, cur_sleep = 1;
3561 /* reinitialize for possible replay */
3563 query_attrs = false;
3564 server = cifs_pick_channel(ses);
3566 cifs_dbg(FYI, "Close\n");
3568 if (!ses || !server)
3571 if (smb3_encryption_required(tcon))
3572 flags |= CIFS_TRANSFORM_REQ;
3574 memset(&rqst, 0, sizeof(struct smb_rqst));
3575 memset(&iov, 0, sizeof(iov));
3579 /* check if need to ask server to return timestamps in close response */
3583 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3584 rc = SMB2_close_init(tcon, server,
3585 &rqst, persistent_fid, volatile_fid,
3591 smb2_set_replay(server, &rqst);
3593 rc = cifs_send_recv(xid, ses, server,
3594 &rqst, &resp_buftype, flags, &rsp_iov);
3595 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
3598 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
3599 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3603 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3606 memcpy(&pbuf->network_open_info,
3607 &rsp->network_open_info,
3608 sizeof(pbuf->network_open_info));
3609 atomic_dec(&tcon->num_remote_opens);
3613 SMB2_close_free(&rqst);
3614 free_rsp_buf(resp_buftype, rsp);
3616 /* retry close in a worker thread if this one is interrupted */
3617 if (is_interrupt_error(rc)) {
3620 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3623 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3624 persistent_fid, tmp_rc);
3627 if (is_replayable_error(rc) &&
3628 smb2_should_replay(tcon, &retries, &cur_sleep))
3635 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3636 u64 persistent_fid, u64 volatile_fid)
3638 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3642 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3643 struct kvec *iov, unsigned int min_buf_size)
3645 unsigned int smb_len = iov->iov_len;
3646 char *end_of_smb = smb_len + (char *)iov->iov_base;
3647 char *begin_of_buf = offset + (char *)iov->iov_base;
3648 char *end_of_buf = begin_of_buf + buffer_length;
3651 if (buffer_length < min_buf_size) {
3652 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3653 buffer_length, min_buf_size);
3657 /* check if beyond RFC1001 maximum length */
3658 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3659 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3660 buffer_length, smb_len);
3664 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3665 cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
3673 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3674 * Caller must free buffer.
3677 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3678 struct kvec *iov, unsigned int minbufsize,
3681 char *begin_of_buf = offset + (char *)iov->iov_base;
3687 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3691 memcpy(data, begin_of_buf, minbufsize);
3697 SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3698 struct smb_rqst *rqst,
3699 u64 persistent_fid, u64 volatile_fid,
3700 u8 info_class, u8 info_type, u32 additional_info,
3701 size_t output_len, size_t input_len, void *input)
3703 struct smb2_query_info_req *req;
3704 struct kvec *iov = rqst->rq_iov;
3705 unsigned int total_len;
3709 if (unlikely(check_add_overflow(input_len, sizeof(*req), &len) ||
3710 len > CIFSMaxBufSize))
3713 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
3714 (void **) &req, &total_len);
3718 req->InfoType = info_type;
3719 req->FileInfoClass = info_class;
3720 req->PersistentFileId = persistent_fid;
3721 req->VolatileFileId = volatile_fid;
3722 req->AdditionalInformation = cpu_to_le32(additional_info);
3724 req->OutputBufferLength = cpu_to_le32(output_len);
3726 req->InputBufferLength = cpu_to_le32(input_len);
3727 /* total_len for smb query request never close to le16 max */
3728 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3729 memcpy(req->Buffer, input, input_len);
3732 iov[0].iov_base = (char *)req;
3734 iov[0].iov_len = len;
3739 SMB2_query_info_free(struct smb_rqst *rqst)
3741 if (rqst && rqst->rq_iov)
3742 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
3746 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3747 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3748 u32 additional_info, size_t output_len, size_t min_len, void **data,
3751 struct smb_rqst rqst;
3752 struct smb2_query_info_rsp *rsp = NULL;
3754 struct kvec rsp_iov;
3756 int resp_buftype = CIFS_NO_BUFFER;
3757 struct cifs_ses *ses = tcon->ses;
3758 struct TCP_Server_Info *server;
3760 bool allocated = false;
3761 int retries = 0, cur_sleep = 1;
3763 cifs_dbg(FYI, "Query Info\n");
3769 /* reinitialize for possible replay */
3772 server = cifs_pick_channel(ses);
3777 if (smb3_encryption_required(tcon))
3778 flags |= CIFS_TRANSFORM_REQ;
3780 memset(&rqst, 0, sizeof(struct smb_rqst));
3781 memset(&iov, 0, sizeof(iov));
3785 rc = SMB2_query_info_init(tcon, server,
3786 &rqst, persistent_fid, volatile_fid,
3787 info_class, info_type, additional_info,
3788 output_len, 0, NULL);
3792 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3793 ses->Suid, info_class, (__u32)info_type);
3796 smb2_set_replay(server, &rqst);
3798 rc = cifs_send_recv(xid, ses, server,
3799 &rqst, &resp_buftype, flags, &rsp_iov);
3800 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3803 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3804 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3805 ses->Suid, info_class, (__u32)info_type, rc);
3809 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3810 ses->Suid, info_class, (__u32)info_type);
3813 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3815 *data = kmalloc(*dlen, GFP_KERNEL);
3818 "Error %d allocating memory for acl\n",
3828 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3829 le32_to_cpu(rsp->OutputBufferLength),
3830 &rsp_iov, dlen ? *dlen : min_len, *data);
3831 if (rc && allocated) {
3838 SMB2_query_info_free(&rqst);
3839 free_rsp_buf(resp_buftype, rsp);
3841 if (is_replayable_error(rc) &&
3842 smb2_should_replay(tcon, &retries, &cur_sleep))
3848 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3849 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3851 return query_info(xid, tcon, persistent_fid, volatile_fid,
3852 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3853 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3854 sizeof(struct smb2_file_all_info), (void **)&data,
3859 /* currently unused, as now we are doing compounding instead (see smb311_posix_query_path_info) */
3861 SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3862 u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen)
3864 size_t output_len = sizeof(struct smb311_posix_qinfo *) +
3865 (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2);
3868 return query_info(xid, tcon, persistent_fid, volatile_fid,
3869 SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0,
3870 output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen);
3871 /* Note caller must free "data" (passed in above). It may be allocated in query_info call */
3876 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3877 u64 persistent_fid, u64 volatile_fid,
3878 void **data, u32 *plen, u32 extra_info)
3880 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
3884 return query_info(xid, tcon, persistent_fid, volatile_fid,
3885 0, SMB2_O_INFO_SECURITY, additional_info,
3886 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3890 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3891 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3893 return query_info(xid, tcon, persistent_fid, volatile_fid,
3894 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3895 sizeof(struct smb2_file_internal_info),
3896 sizeof(struct smb2_file_internal_info),
3897 (void **)&uniqueid, NULL);
3901 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3902 * See MS-SMB2 2.2.35 and 2.2.36
3906 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3907 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3908 u64 persistent_fid, u64 volatile_fid,
3909 u32 completion_filter, bool watch_tree)
3911 struct smb2_change_notify_req *req;
3912 struct kvec *iov = rqst->rq_iov;
3913 unsigned int total_len;
3916 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
3917 (void **) &req, &total_len);
3921 req->PersistentFileId = persistent_fid;
3922 req->VolatileFileId = volatile_fid;
3923 /* See note 354 of MS-SMB2, 64K max */
3924 req->OutputBufferLength =
3925 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3926 req->CompletionFilter = cpu_to_le32(completion_filter);
3928 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3932 iov[0].iov_base = (char *)req;
3933 iov[0].iov_len = total_len;
3939 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3940 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3941 u32 completion_filter, u32 max_out_data_len, char **out_data,
3942 u32 *plen /* returned data len */)
3944 struct cifs_ses *ses = tcon->ses;
3945 struct TCP_Server_Info *server;
3946 struct smb_rqst rqst;
3947 struct smb2_change_notify_rsp *smb_rsp;
3949 struct kvec rsp_iov = {NULL, 0};
3950 int resp_buftype = CIFS_NO_BUFFER;
3953 int retries = 0, cur_sleep = 1;
3956 /* reinitialize for possible replay */
3958 server = cifs_pick_channel(ses);
3960 cifs_dbg(FYI, "change notify\n");
3961 if (!ses || !server)
3964 if (smb3_encryption_required(tcon))
3965 flags |= CIFS_TRANSFORM_REQ;
3967 memset(&rqst, 0, sizeof(struct smb_rqst));
3968 memset(&iov, 0, sizeof(iov));
3975 rc = SMB2_notify_init(xid, &rqst, tcon, server,
3976 persistent_fid, volatile_fid,
3977 completion_filter, watch_tree);
3981 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3982 (u8)watch_tree, completion_filter);
3985 smb2_set_replay(server, &rqst);
3987 rc = cifs_send_recv(xid, ses, server,
3988 &rqst, &resp_buftype, flags, &rsp_iov);
3991 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3992 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3993 (u8)watch_tree, completion_filter, rc);
3995 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3996 ses->Suid, (u8)watch_tree, completion_filter);
3997 /* validate that notify information is plausible */
3998 if ((rsp_iov.iov_base == NULL) ||
3999 (rsp_iov.iov_len < sizeof(struct smb2_change_notify_rsp) + 1))
4002 smb_rsp = (struct smb2_change_notify_rsp *)rsp_iov.iov_base;
4004 smb2_validate_iov(le16_to_cpu(smb_rsp->OutputBufferOffset),
4005 le32_to_cpu(smb_rsp->OutputBufferLength), &rsp_iov,
4006 sizeof(struct file_notify_information));
4008 *out_data = kmemdup((char *)smb_rsp + le16_to_cpu(smb_rsp->OutputBufferOffset),
4009 le32_to_cpu(smb_rsp->OutputBufferLength), GFP_KERNEL);
4010 if (*out_data == NULL) {
4014 *plen = le32_to_cpu(smb_rsp->OutputBufferLength);
4019 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
4020 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4022 if (is_replayable_error(rc) &&
4023 smb2_should_replay(tcon, &retries, &cur_sleep))
4032 * This is a no-op for now. We're not really interested in the reply, but
4033 * rather in the fact that the server sent one and that server->lstrp
4036 * FIXME: maybe we should consider checking that the reply matches request?
4039 smb2_echo_callback(struct mid_q_entry *mid)
4041 struct TCP_Server_Info *server = mid->callback_data;
4042 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
4043 struct cifs_credits credits = { .value = 0, .instance = 0 };
4045 if (mid->mid_state == MID_RESPONSE_RECEIVED
4046 || mid->mid_state == MID_RESPONSE_MALFORMED) {
4047 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4048 credits.instance = server->reconnect_instance;
4052 add_credits(server, &credits, CIFS_ECHO_OP);
4055 void smb2_reconnect_server(struct work_struct *work)
4057 struct TCP_Server_Info *server = container_of(work,
4058 struct TCP_Server_Info, reconnect.work);
4059 struct TCP_Server_Info *pserver;
4060 struct cifs_ses *ses, *ses2;
4061 struct cifs_tcon *tcon, *tcon2;
4062 struct list_head tmp_list, tmp_ses_list;
4063 bool ses_exist = false;
4064 bool tcon_selected = false;
4066 bool resched = false;
4068 /* first check if ref count has reached 0, if not inc ref count */
4069 spin_lock(&cifs_tcp_ses_lock);
4070 if (!server->srv_count) {
4071 spin_unlock(&cifs_tcp_ses_lock);
4074 server->srv_count++;
4075 spin_unlock(&cifs_tcp_ses_lock);
4077 /* If server is a channel, select the primary channel */
4078 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
4080 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
4081 mutex_lock(&pserver->reconnect_mutex);
4083 /* if the server is marked for termination, drop the ref count here */
4084 if (server->terminate) {
4085 cifs_put_tcp_session(server, true);
4086 mutex_unlock(&pserver->reconnect_mutex);
4090 INIT_LIST_HEAD(&tmp_list);
4091 INIT_LIST_HEAD(&tmp_ses_list);
4092 cifs_dbg(FYI, "Reconnecting tcons and channels\n");
4094 spin_lock(&cifs_tcp_ses_lock);
4095 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
4096 spin_lock(&ses->ses_lock);
4097 if (ses->ses_status == SES_EXITING) {
4098 spin_unlock(&ses->ses_lock);
4101 spin_unlock(&ses->ses_lock);
4103 tcon_selected = false;
4105 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
4106 if (tcon->need_reconnect || tcon->need_reopen_files) {
4108 list_add_tail(&tcon->rlist, &tmp_list);
4109 tcon_selected = true;
4113 * IPC has the same lifetime as its session and uses its
4116 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
4117 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
4118 tcon_selected = true;
4119 cifs_smb_ses_inc_refcount(ses);
4122 * handle the case where channel needs to reconnect
4123 * binding session, but tcon is healthy (some other channel
4126 spin_lock(&ses->chan_lock);
4127 if (!tcon_selected && cifs_chan_needs_reconnect(ses, server)) {
4128 list_add_tail(&ses->rlist, &tmp_ses_list);
4130 cifs_smb_ses_inc_refcount(ses);
4132 spin_unlock(&ses->chan_lock);
4134 spin_unlock(&cifs_tcp_ses_lock);
4136 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
4137 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server, true);
4139 cifs_reopen_persistent_handles(tcon);
4142 list_del_init(&tcon->rlist);
4144 cifs_put_smb_ses(tcon->ses);
4146 cifs_put_tcon(tcon);
4152 /* allocate a dummy tcon struct used for reconnect */
4153 tcon = tcon_info_alloc(false);
4156 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
4157 list_del_init(&ses->rlist);
4158 cifs_put_smb_ses(ses);
4163 tcon->status = TID_GOOD;
4164 tcon->retry = false;
4165 tcon->need_reconnect = false;
4167 /* now reconnect sessions for necessary channels */
4168 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
4170 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server, true);
4173 list_del_init(&ses->rlist);
4174 cifs_put_smb_ses(ses);
4179 cifs_dbg(FYI, "Reconnecting tcons and channels finished\n");
4181 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
4182 mutex_unlock(&pserver->reconnect_mutex);
4184 /* now we can safely release srv struct */
4185 cifs_put_tcp_session(server, true);
4189 SMB2_echo(struct TCP_Server_Info *server)
4191 struct smb2_echo_req *req;
4194 struct smb_rqst rqst = { .rq_iov = iov,
4196 unsigned int total_len;
4198 cifs_dbg(FYI, "In echo request for conn_id %lld\n", server->conn_id);
4200 spin_lock(&server->srv_lock);
4201 if (server->ops->need_neg &&
4202 server->ops->need_neg(server)) {
4203 spin_unlock(&server->srv_lock);
4204 /* No need to send echo on newly established connections */
4205 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
4208 spin_unlock(&server->srv_lock);
4210 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
4211 (void **)&req, &total_len);
4215 req->hdr.CreditRequest = cpu_to_le16(1);
4217 iov[0].iov_len = total_len;
4218 iov[0].iov_base = (char *)req;
4220 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
4221 server, CIFS_ECHO_OP, NULL);
4223 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
4225 cifs_small_buf_release(req);
4230 SMB2_flush_free(struct smb_rqst *rqst)
4232 if (rqst && rqst->rq_iov)
4233 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4237 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
4238 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
4239 u64 persistent_fid, u64 volatile_fid)
4241 struct smb2_flush_req *req;
4242 struct kvec *iov = rqst->rq_iov;
4243 unsigned int total_len;
4246 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
4247 (void **) &req, &total_len);
4251 req->PersistentFileId = persistent_fid;
4252 req->VolatileFileId = volatile_fid;
4254 iov[0].iov_base = (char *)req;
4255 iov[0].iov_len = total_len;
4261 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4264 struct cifs_ses *ses = tcon->ses;
4265 struct smb_rqst rqst;
4267 struct kvec rsp_iov = {NULL, 0};
4268 struct TCP_Server_Info *server;
4269 int resp_buftype = CIFS_NO_BUFFER;
4272 int retries = 0, cur_sleep = 1;
4275 /* reinitialize for possible replay */
4277 server = cifs_pick_channel(ses);
4279 cifs_dbg(FYI, "flush\n");
4280 if (!ses || !(ses->server))
4283 if (smb3_encryption_required(tcon))
4284 flags |= CIFS_TRANSFORM_REQ;
4286 memset(&rqst, 0, sizeof(struct smb_rqst));
4287 memset(&iov, 0, sizeof(iov));
4291 rc = SMB2_flush_init(xid, &rqst, tcon, server,
4292 persistent_fid, volatile_fid);
4296 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
4299 smb2_set_replay(server, &rqst);
4301 rc = cifs_send_recv(xid, ses, server,
4302 &rqst, &resp_buftype, flags, &rsp_iov);
4305 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
4306 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
4309 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
4313 SMB2_flush_free(&rqst);
4314 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4316 if (is_replayable_error(rc) &&
4317 smb2_should_replay(tcon, &retries, &cur_sleep))
4323 #ifdef CONFIG_CIFS_SMB_DIRECT
4324 static inline bool smb3_use_rdma_offload(struct cifs_io_parms *io_parms)
4326 struct TCP_Server_Info *server = io_parms->server;
4327 struct cifs_tcon *tcon = io_parms->tcon;
4329 /* we can only offload if we're connected */
4330 if (!server || !tcon)
4333 /* we can only offload on an rdma connection */
4334 if (!server->rdma || !server->smbd_conn)
4337 /* we don't support signed offload yet */
4341 /* we don't support encrypted offload yet */
4342 if (smb3_encryption_required(tcon))
4345 /* offload also has its overhead, so only do it if desired */
4346 if (io_parms->length < server->smbd_conn->rdma_readwrite_threshold)
4351 #endif /* CONFIG_CIFS_SMB_DIRECT */
4354 * To form a chain of read requests, any read requests after the first should
4355 * have the end_of_chain boolean set to true.
4358 smb2_new_read_req(void **buf, unsigned int *total_len,
4359 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
4360 unsigned int remaining_bytes, int request_type)
4363 struct smb2_read_req *req = NULL;
4364 struct smb2_hdr *shdr;
4365 struct TCP_Server_Info *server = io_parms->server;
4367 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
4368 (void **) &req, total_len);
4373 return -ECONNABORTED;
4376 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4378 req->PersistentFileId = io_parms->persistent_fid;
4379 req->VolatileFileId = io_parms->volatile_fid;
4380 req->ReadChannelInfoOffset = 0; /* reserved */
4381 req->ReadChannelInfoLength = 0; /* reserved */
4382 req->Channel = 0; /* reserved */
4383 req->MinimumCount = 0;
4384 req->Length = cpu_to_le32(io_parms->length);
4385 req->Offset = cpu_to_le64(io_parms->offset);
4387 trace_smb3_read_enter(0 /* xid */,
4388 io_parms->persistent_fid,
4389 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4390 io_parms->offset, io_parms->length);
4391 #ifdef CONFIG_CIFS_SMB_DIRECT
4393 * If we want to do a RDMA write, fill in and append
4394 * smbd_buffer_descriptor_v1 to the end of read request
4396 if (smb3_use_rdma_offload(io_parms)) {
4397 struct smbd_buffer_descriptor_v1 *v1;
4398 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4400 rdata->mr = smbd_register_mr(server->smbd_conn, &rdata->iter,
4401 true, need_invalidate);
4405 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4406 if (need_invalidate)
4407 req->Channel = SMB2_CHANNEL_RDMA_V1;
4408 req->ReadChannelInfoOffset =
4409 cpu_to_le16(offsetof(struct smb2_read_req, Buffer));
4410 req->ReadChannelInfoLength =
4411 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4412 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4413 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
4414 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
4415 v1->length = cpu_to_le32(rdata->mr->mr->length);
4417 *total_len += sizeof(*v1) - 1;
4420 if (request_type & CHAINED_REQUEST) {
4421 if (!(request_type & END_OF_CHAIN)) {
4422 /* next 8-byte aligned request */
4423 *total_len = ALIGN(*total_len, 8);
4424 shdr->NextCommand = cpu_to_le32(*total_len);
4425 } else /* END_OF_CHAIN */
4426 shdr->NextCommand = 0;
4427 if (request_type & RELATED_REQUEST) {
4428 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
4430 * Related requests use info from previous read request
4433 shdr->SessionId = cpu_to_le64(0xFFFFFFFFFFFFFFFF);
4434 shdr->Id.SyncId.TreeId = cpu_to_le32(0xFFFFFFFF);
4435 req->PersistentFileId = (u64)-1;
4436 req->VolatileFileId = (u64)-1;
4439 if (remaining_bytes > io_parms->length)
4440 req->RemainingBytes = cpu_to_le32(remaining_bytes);
4442 req->RemainingBytes = 0;
4449 smb2_readv_callback(struct mid_q_entry *mid)
4451 struct cifs_readdata *rdata = mid->callback_data;
4452 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
4453 struct TCP_Server_Info *server = rdata->server;
4454 struct smb2_hdr *shdr =
4455 (struct smb2_hdr *)rdata->iov[0].iov_base;
4456 struct cifs_credits credits = { .value = 0, .instance = 0 };
4457 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1], .rq_nvec = 1 };
4459 if (rdata->got_bytes) {
4460 rqst.rq_iter = rdata->iter;
4461 rqst.rq_iter_size = iov_iter_count(&rdata->iter);
4464 WARN_ONCE(rdata->server != mid->server,
4465 "rdata server %p != mid server %p",
4466 rdata->server, mid->server);
4468 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
4469 __func__, mid->mid, mid->mid_state, rdata->result,
4472 switch (mid->mid_state) {
4473 case MID_RESPONSE_RECEIVED:
4474 credits.value = le16_to_cpu(shdr->CreditRequest);
4475 credits.instance = server->reconnect_instance;
4476 /* result already set, check signature */
4477 if (server->sign && !mid->decrypted) {
4480 iov_iter_revert(&rqst.rq_iter, rdata->got_bytes);
4481 iov_iter_truncate(&rqst.rq_iter, rdata->got_bytes);
4482 rc = smb2_verify_signature(&rqst, server);
4484 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
4487 /* FIXME: should this be counted toward the initiating task? */
4488 task_io_account_read(rdata->got_bytes);
4489 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4491 case MID_REQUEST_SUBMITTED:
4492 case MID_RETRY_NEEDED:
4493 rdata->result = -EAGAIN;
4494 if (server->sign && rdata->got_bytes)
4495 /* reset bytes number since we can not check a sign */
4496 rdata->got_bytes = 0;
4497 /* FIXME: should this be counted toward the initiating task? */
4498 task_io_account_read(rdata->got_bytes);
4499 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4501 case MID_RESPONSE_MALFORMED:
4502 credits.value = le16_to_cpu(shdr->CreditRequest);
4503 credits.instance = server->reconnect_instance;
4506 rdata->result = -EIO;
4508 #ifdef CONFIG_CIFS_SMB_DIRECT
4510 * If this rdata has a memmory registered, the MR can be freed
4511 * MR needs to be freed as soon as I/O finishes to prevent deadlock
4512 * because they have limited number and are used for future I/Os
4515 smbd_deregister_mr(rdata->mr);
4519 if (rdata->result && rdata->result != -ENODATA) {
4520 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
4521 trace_smb3_read_err(0 /* xid */,
4522 rdata->cfile->fid.persistent_fid,
4523 tcon->tid, tcon->ses->Suid, rdata->offset,
4524 rdata->bytes, rdata->result);
4526 trace_smb3_read_done(0 /* xid */,
4527 rdata->cfile->fid.persistent_fid,
4528 tcon->tid, tcon->ses->Suid,
4529 rdata->offset, rdata->got_bytes);
4531 queue_work(cifsiod_wq, &rdata->work);
4533 add_credits(server, &credits, 0);
4536 /* smb2_async_readv - send an async read, and set up mid to handle result */
4538 smb2_async_readv(struct cifs_readdata *rdata)
4542 struct smb2_hdr *shdr;
4543 struct cifs_io_parms io_parms;
4544 struct smb_rqst rqst = { .rq_iov = rdata->iov,
4546 struct TCP_Server_Info *server;
4547 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
4548 unsigned int total_len;
4551 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
4552 __func__, rdata->offset, rdata->bytes);
4555 rdata->server = cifs_pick_channel(tcon->ses);
4557 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
4558 io_parms.server = server = rdata->server;
4559 io_parms.offset = rdata->offset;
4560 io_parms.length = rdata->bytes;
4561 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
4562 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
4563 io_parms.pid = rdata->pid;
4565 rc = smb2_new_read_req(
4566 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
4570 if (smb3_encryption_required(io_parms.tcon))
4571 flags |= CIFS_TRANSFORM_REQ;
4573 rdata->iov[0].iov_base = buf;
4574 rdata->iov[0].iov_len = total_len;
4576 shdr = (struct smb2_hdr *)buf;
4578 if (rdata->credits.value > 0) {
4579 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
4580 SMB2_MAX_BUFFER_SIZE));
4581 credit_request = le16_to_cpu(shdr->CreditCharge) + 8;
4582 if (server->credits >= server->max_credits)
4583 shdr->CreditRequest = cpu_to_le16(0);
4585 shdr->CreditRequest = cpu_to_le16(
4586 min_t(int, server->max_credits -
4587 server->credits, credit_request));
4589 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4591 goto async_readv_out;
4593 flags |= CIFS_HAS_CREDITS;
4596 kref_get(&rdata->refcount);
4597 rc = cifs_call_async(server, &rqst,
4598 cifs_readv_receive, smb2_readv_callback,
4599 smb3_handle_read_data, rdata, flags,
4602 kref_put(&rdata->refcount, cifs_readdata_release);
4603 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
4604 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
4606 io_parms.tcon->ses->Suid,
4607 io_parms.offset, io_parms.length, rc);
4611 cifs_small_buf_release(buf);
4616 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
4617 unsigned int *nbytes, char **buf, int *buf_type)
4619 struct smb_rqst rqst;
4620 int resp_buftype, rc;
4621 struct smb2_read_req *req = NULL;
4622 struct smb2_read_rsp *rsp = NULL;
4624 struct kvec rsp_iov;
4625 unsigned int total_len;
4626 int flags = CIFS_LOG_ERROR;
4627 struct cifs_ses *ses = io_parms->tcon->ses;
4629 if (!io_parms->server)
4630 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4633 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
4637 if (smb3_encryption_required(io_parms->tcon))
4638 flags |= CIFS_TRANSFORM_REQ;
4640 iov[0].iov_base = (char *)req;
4641 iov[0].iov_len = total_len;
4643 memset(&rqst, 0, sizeof(struct smb_rqst));
4647 rc = cifs_send_recv(xid, ses, io_parms->server,
4648 &rqst, &resp_buftype, flags, &rsp_iov);
4649 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
4652 if (rc != -ENODATA) {
4653 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4654 cifs_dbg(VFS, "Send error in read = %d\n", rc);
4655 trace_smb3_read_err(xid,
4656 req->PersistentFileId,
4657 io_parms->tcon->tid, ses->Suid,
4658 io_parms->offset, io_parms->length,
4661 trace_smb3_read_done(xid, req->PersistentFileId, io_parms->tcon->tid,
4662 ses->Suid, io_parms->offset, 0);
4663 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4664 cifs_small_buf_release(req);
4665 return rc == -ENODATA ? 0 : rc;
4667 trace_smb3_read_done(xid,
4668 req->PersistentFileId,
4669 io_parms->tcon->tid, ses->Suid,
4670 io_parms->offset, io_parms->length);
4672 cifs_small_buf_release(req);
4674 *nbytes = le32_to_cpu(rsp->DataLength);
4675 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4676 (*nbytes > io_parms->length)) {
4677 cifs_dbg(FYI, "bad length %d for count %d\n",
4678 *nbytes, io_parms->length);
4684 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
4685 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4686 } else if (resp_buftype != CIFS_NO_BUFFER) {
4687 *buf = rsp_iov.iov_base;
4688 if (resp_buftype == CIFS_SMALL_BUFFER)
4689 *buf_type = CIFS_SMALL_BUFFER;
4690 else if (resp_buftype == CIFS_LARGE_BUFFER)
4691 *buf_type = CIFS_LARGE_BUFFER;
4697 * Check the mid_state and signature on received buffer (if any), and queue the
4698 * workqueue completion task.
4701 smb2_writev_callback(struct mid_q_entry *mid)
4703 struct cifs_writedata *wdata = mid->callback_data;
4704 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4705 struct TCP_Server_Info *server = wdata->server;
4706 unsigned int written;
4707 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
4708 struct cifs_credits credits = { .value = 0, .instance = 0 };
4710 WARN_ONCE(wdata->server != mid->server,
4711 "wdata server %p != mid server %p",
4712 wdata->server, mid->server);
4714 switch (mid->mid_state) {
4715 case MID_RESPONSE_RECEIVED:
4716 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4717 credits.instance = server->reconnect_instance;
4718 wdata->result = smb2_check_receive(mid, server, 0);
4719 if (wdata->result != 0)
4722 written = le32_to_cpu(rsp->DataLength);
4724 * Mask off high 16 bits when bytes written as returned
4725 * by the server is greater than bytes requested by the
4726 * client. OS/2 servers are known to set incorrect
4729 if (written > wdata->bytes)
4732 if (written < wdata->bytes)
4733 wdata->result = -ENOSPC;
4735 wdata->bytes = written;
4737 case MID_REQUEST_SUBMITTED:
4738 case MID_RETRY_NEEDED:
4739 wdata->result = -EAGAIN;
4741 case MID_RESPONSE_MALFORMED:
4742 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4743 credits.instance = server->reconnect_instance;
4746 wdata->result = -EIO;
4749 #ifdef CONFIG_CIFS_SMB_DIRECT
4751 * If this wdata has a memory registered, the MR can be freed
4752 * The number of MRs available is limited, it's important to recover
4753 * used MR as soon as I/O is finished. Hold MR longer in the later
4754 * I/O process can possibly result in I/O deadlock due to lack of MR
4755 * to send request on I/O retry
4758 smbd_deregister_mr(wdata->mr);
4762 if (wdata->result) {
4763 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4764 trace_smb3_write_err(0 /* no xid */,
4765 wdata->cfile->fid.persistent_fid,
4766 tcon->tid, tcon->ses->Suid, wdata->offset,
4767 wdata->bytes, wdata->result);
4768 if (wdata->result == -ENOSPC)
4769 pr_warn_once("Out of space writing to %s\n",
4772 trace_smb3_write_done(0 /* no xid */,
4773 wdata->cfile->fid.persistent_fid,
4774 tcon->tid, tcon->ses->Suid,
4775 wdata->offset, wdata->bytes);
4777 queue_work(cifsiod_wq, &wdata->work);
4779 add_credits(server, &credits, 0);
4782 /* smb2_async_writev - send an async write, and set up mid to handle result */
4784 smb2_async_writev(struct cifs_writedata *wdata,
4785 void (*release)(struct kref *kref))
4787 int rc = -EACCES, flags = 0;
4788 struct smb2_write_req *req = NULL;
4789 struct smb2_hdr *shdr;
4790 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4791 struct TCP_Server_Info *server = wdata->server;
4793 struct smb_rqst rqst = { };
4794 unsigned int total_len;
4795 struct cifs_io_parms _io_parms;
4796 struct cifs_io_parms *io_parms = NULL;
4799 if (!wdata->server || wdata->replay)
4800 server = wdata->server = cifs_pick_channel(tcon->ses);
4803 * in future we may get cifs_io_parms passed in from the caller,
4804 * but for now we construct it here...
4806 _io_parms = (struct cifs_io_parms) {
4809 .offset = wdata->offset,
4810 .length = wdata->bytes,
4811 .persistent_fid = wdata->cfile->fid.persistent_fid,
4812 .volatile_fid = wdata->cfile->fid.volatile_fid,
4815 io_parms = &_io_parms;
4817 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
4818 (void **) &req, &total_len);
4822 if (smb3_encryption_required(tcon))
4823 flags |= CIFS_TRANSFORM_REQ;
4825 shdr = (struct smb2_hdr *)req;
4826 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4828 req->PersistentFileId = io_parms->persistent_fid;
4829 req->VolatileFileId = io_parms->volatile_fid;
4830 req->WriteChannelInfoOffset = 0;
4831 req->WriteChannelInfoLength = 0;
4832 req->Channel = SMB2_CHANNEL_NONE;
4833 req->Offset = cpu_to_le64(io_parms->offset);
4834 req->DataOffset = cpu_to_le16(
4835 offsetof(struct smb2_write_req, Buffer));
4836 req->RemainingBytes = 0;
4838 trace_smb3_write_enter(0 /* xid */,
4839 io_parms->persistent_fid,
4840 io_parms->tcon->tid,
4841 io_parms->tcon->ses->Suid,
4845 #ifdef CONFIG_CIFS_SMB_DIRECT
4847 * If we want to do a server RDMA read, fill in and append
4848 * smbd_buffer_descriptor_v1 to the end of write request
4850 if (smb3_use_rdma_offload(io_parms)) {
4851 struct smbd_buffer_descriptor_v1 *v1;
4852 size_t data_size = iov_iter_count(&wdata->iter);
4853 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4855 wdata->mr = smbd_register_mr(server->smbd_conn, &wdata->iter,
4856 false, need_invalidate);
4859 goto async_writev_out;
4862 req->DataOffset = 0;
4863 req->RemainingBytes = cpu_to_le32(data_size);
4864 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4865 if (need_invalidate)
4866 req->Channel = SMB2_CHANNEL_RDMA_V1;
4867 req->WriteChannelInfoOffset =
4868 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4869 req->WriteChannelInfoLength =
4870 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4871 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4872 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4873 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4874 v1->length = cpu_to_le32(wdata->mr->mr->length);
4877 iov[0].iov_len = total_len - 1;
4878 iov[0].iov_base = (char *)req;
4882 rqst.rq_iter = wdata->iter;
4883 rqst.rq_iter_size = iov_iter_count(&rqst.rq_iter);
4885 smb2_set_replay(server, &rqst);
4886 #ifdef CONFIG_CIFS_SMB_DIRECT
4888 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4890 cifs_dbg(FYI, "async write at %llu %u bytes iter=%zx\n",
4891 io_parms->offset, io_parms->length, iov_iter_count(&rqst.rq_iter));
4893 #ifdef CONFIG_CIFS_SMB_DIRECT
4894 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4896 req->Length = cpu_to_le32(io_parms->length);
4898 req->Length = cpu_to_le32(io_parms->length);
4901 if (wdata->credits.value > 0) {
4902 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4903 SMB2_MAX_BUFFER_SIZE));
4904 credit_request = le16_to_cpu(shdr->CreditCharge) + 8;
4905 if (server->credits >= server->max_credits)
4906 shdr->CreditRequest = cpu_to_le16(0);
4908 shdr->CreditRequest = cpu_to_le16(
4909 min_t(int, server->max_credits -
4910 server->credits, credit_request));
4912 rc = adjust_credits(server, &wdata->credits, io_parms->length);
4914 goto async_writev_out;
4916 flags |= CIFS_HAS_CREDITS;
4919 kref_get(&wdata->refcount);
4920 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4921 wdata, flags, &wdata->credits);
4924 trace_smb3_write_err(0 /* no xid */,
4925 io_parms->persistent_fid,
4926 io_parms->tcon->tid,
4927 io_parms->tcon->ses->Suid,
4931 kref_put(&wdata->refcount, release);
4932 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4936 cifs_small_buf_release(req);
4941 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4942 * The length field from io_parms must be at least 1 and indicates a number of
4943 * elements with data to write that begins with position 1 in iov array. All
4944 * data length is specified by count.
4947 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4948 unsigned int *nbytes, struct kvec *iov, int n_vec)
4950 struct smb_rqst rqst;
4952 struct smb2_write_req *req = NULL;
4953 struct smb2_write_rsp *rsp = NULL;
4955 struct kvec rsp_iov;
4957 unsigned int total_len;
4958 struct TCP_Server_Info *server;
4959 int retries = 0, cur_sleep = 1;
4962 /* reinitialize for possible replay */
4965 if (!io_parms->server)
4966 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4967 server = io_parms->server;
4969 return -ECONNABORTED;
4974 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
4975 (void **) &req, &total_len);
4979 if (smb3_encryption_required(io_parms->tcon))
4980 flags |= CIFS_TRANSFORM_REQ;
4982 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4984 req->PersistentFileId = io_parms->persistent_fid;
4985 req->VolatileFileId = io_parms->volatile_fid;
4986 req->WriteChannelInfoOffset = 0;
4987 req->WriteChannelInfoLength = 0;
4989 req->Length = cpu_to_le32(io_parms->length);
4990 req->Offset = cpu_to_le64(io_parms->offset);
4991 req->DataOffset = cpu_to_le16(
4992 offsetof(struct smb2_write_req, Buffer));
4993 req->RemainingBytes = 0;
4995 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4996 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4997 io_parms->offset, io_parms->length);
4999 iov[0].iov_base = (char *)req;
5001 iov[0].iov_len = total_len - 1;
5003 memset(&rqst, 0, sizeof(struct smb_rqst));
5005 rqst.rq_nvec = n_vec + 1;
5008 smb2_set_replay(server, &rqst);
5010 rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
5012 &resp_buftype, flags, &rsp_iov);
5013 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
5016 trace_smb3_write_err(xid,
5017 req->PersistentFileId,
5018 io_parms->tcon->tid,
5019 io_parms->tcon->ses->Suid,
5020 io_parms->offset, io_parms->length, rc);
5021 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
5022 cifs_dbg(VFS, "Send error in write = %d\n", rc);
5024 *nbytes = le32_to_cpu(rsp->DataLength);
5025 trace_smb3_write_done(xid,
5026 req->PersistentFileId,
5027 io_parms->tcon->tid,
5028 io_parms->tcon->ses->Suid,
5029 io_parms->offset, *nbytes);
5032 cifs_small_buf_release(req);
5033 free_rsp_buf(resp_buftype, rsp);
5035 if (is_replayable_error(rc) &&
5036 smb2_should_replay(io_parms->tcon, &retries, &cur_sleep))
5042 int posix_info_sid_size(const void *beg, const void *end)
5050 subauth = *(u8 *)(beg+1);
5051 if (subauth < 1 || subauth > 15)
5054 total = 1 + 1 + 6 + 4*subauth;
5055 if (beg + total > end)
5061 int posix_info_parse(const void *beg, const void *end,
5062 struct smb2_posix_info_parsed *out)
5066 int owner_len, group_len;
5068 const void *owner_sid;
5069 const void *group_sid;
5072 /* if no end bound given, assume payload to be correct */
5074 const struct smb2_posix_info *p = beg;
5076 end = beg + le32_to_cpu(p->NextEntryOffset);
5077 /* last element will have a 0 offset, pick a sensible bound */
5082 /* check base buf */
5083 if (beg + sizeof(struct smb2_posix_info) > end)
5085 total_len = sizeof(struct smb2_posix_info);
5087 /* check owner sid */
5088 owner_sid = beg + total_len;
5089 owner_len = posix_info_sid_size(owner_sid, end);
5092 total_len += owner_len;
5094 /* check group sid */
5095 group_sid = beg + total_len;
5096 group_len = posix_info_sid_size(group_sid, end);
5099 total_len += group_len;
5101 /* check name len */
5102 if (beg + total_len + 4 > end)
5104 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
5105 if (name_len < 1 || name_len > 0xFFFF)
5110 name = beg + total_len;
5111 if (name + name_len > end)
5113 total_len += name_len;
5117 out->size = total_len;
5118 out->name_len = name_len;
5120 memcpy(&out->owner, owner_sid, owner_len);
5121 memcpy(&out->group, group_sid, group_len);
5126 static int posix_info_extra_size(const void *beg, const void *end)
5128 int len = posix_info_parse(beg, end, NULL);
5132 return len - sizeof(struct smb2_posix_info);
5136 num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
5140 unsigned int entrycount = 0;
5141 unsigned int next_offset = 0;
5143 FILE_DIRECTORY_INFO *dir_info;
5145 if (bufstart == NULL)
5148 entryptr = bufstart;
5151 if (entryptr + next_offset < entryptr ||
5152 entryptr + next_offset > end_of_buf ||
5153 entryptr + next_offset + size > end_of_buf) {
5154 cifs_dbg(VFS, "malformed search entry would overflow\n");
5158 entryptr = entryptr + next_offset;
5159 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
5161 if (infotype == SMB_FIND_FILE_POSIX_INFO)
5162 len = posix_info_extra_size(entryptr, end_of_buf);
5164 len = le32_to_cpu(dir_info->FileNameLength);
5167 entryptr + len < entryptr ||
5168 entryptr + len > end_of_buf ||
5169 entryptr + len + size > end_of_buf) {
5170 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
5175 *lastentry = entryptr;
5178 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
5189 int SMB2_query_directory_init(const unsigned int xid,
5190 struct cifs_tcon *tcon,
5191 struct TCP_Server_Info *server,
5192 struct smb_rqst *rqst,
5193 u64 persistent_fid, u64 volatile_fid,
5194 int index, int info_level)
5196 struct smb2_query_directory_req *req;
5197 unsigned char *bufptr;
5198 __le16 asteriks = cpu_to_le16('*');
5199 unsigned int output_size = CIFSMaxBufSize -
5200 MAX_SMB2_CREATE_RESPONSE_SIZE -
5201 MAX_SMB2_CLOSE_RESPONSE_SIZE;
5202 unsigned int total_len;
5203 struct kvec *iov = rqst->rq_iov;
5206 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
5207 (void **) &req, &total_len);
5211 switch (info_level) {
5212 case SMB_FIND_FILE_DIRECTORY_INFO:
5213 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
5215 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
5216 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
5218 case SMB_FIND_FILE_POSIX_INFO:
5219 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
5221 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
5222 req->FileInformationClass = FILE_FULL_DIRECTORY_INFORMATION;
5225 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
5230 req->FileIndex = cpu_to_le32(index);
5231 req->PersistentFileId = persistent_fid;
5232 req->VolatileFileId = volatile_fid;
5235 bufptr = req->Buffer;
5236 memcpy(bufptr, &asteriks, len);
5238 req->FileNameOffset =
5239 cpu_to_le16(sizeof(struct smb2_query_directory_req));
5240 req->FileNameLength = cpu_to_le16(len);
5242 * BB could be 30 bytes or so longer if we used SMB2 specific
5243 * buffer lengths, but this is safe and close enough.
5245 output_size = min_t(unsigned int, output_size, server->maxBuf);
5246 output_size = min_t(unsigned int, output_size, 2 << 15);
5247 req->OutputBufferLength = cpu_to_le32(output_size);
5249 iov[0].iov_base = (char *)req;
5251 iov[0].iov_len = total_len - 1;
5253 iov[1].iov_base = (char *)(req->Buffer);
5254 iov[1].iov_len = len;
5256 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
5257 tcon->ses->Suid, index, output_size);
5262 void SMB2_query_directory_free(struct smb_rqst *rqst)
5264 if (rqst && rqst->rq_iov) {
5265 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
5270 smb2_parse_query_directory(struct cifs_tcon *tcon,
5271 struct kvec *rsp_iov,
5273 struct cifs_search_info *srch_inf)
5275 struct smb2_query_directory_rsp *rsp;
5276 size_t info_buf_size;
5280 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
5282 switch (srch_inf->info_level) {
5283 case SMB_FIND_FILE_DIRECTORY_INFO:
5284 info_buf_size = sizeof(FILE_DIRECTORY_INFO);
5286 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
5287 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO);
5289 case SMB_FIND_FILE_POSIX_INFO:
5290 /* note that posix payload are variable size */
5291 info_buf_size = sizeof(struct smb2_posix_info);
5293 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
5294 info_buf_size = sizeof(FILE_FULL_DIRECTORY_INFO);
5297 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
5298 srch_inf->info_level);
5302 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5303 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
5306 cifs_tcon_dbg(VFS, "bad info payload");
5310 srch_inf->unicode = true;
5312 if (srch_inf->ntwrk_buf_start) {
5313 if (srch_inf->smallBuf)
5314 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
5316 cifs_buf_release(srch_inf->ntwrk_buf_start);
5318 srch_inf->ntwrk_buf_start = (char *)rsp;
5319 srch_inf->srch_entries_start = srch_inf->last_entry =
5320 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
5321 end_of_smb = rsp_iov->iov_len + (char *)rsp;
5323 srch_inf->entries_in_buffer = num_entries(
5324 srch_inf->info_level,
5325 srch_inf->srch_entries_start,
5327 &srch_inf->last_entry,
5330 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
5331 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
5332 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
5333 srch_inf->srch_entries_start, srch_inf->last_entry);
5334 if (resp_buftype == CIFS_LARGE_BUFFER)
5335 srch_inf->smallBuf = false;
5336 else if (resp_buftype == CIFS_SMALL_BUFFER)
5337 srch_inf->smallBuf = true;
5339 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
5345 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
5346 u64 persistent_fid, u64 volatile_fid, int index,
5347 struct cifs_search_info *srch_inf)
5349 struct smb_rqst rqst;
5350 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
5351 struct smb2_query_directory_rsp *rsp = NULL;
5352 int resp_buftype = CIFS_NO_BUFFER;
5353 struct kvec rsp_iov;
5355 struct cifs_ses *ses = tcon->ses;
5356 struct TCP_Server_Info *server;
5358 int retries = 0, cur_sleep = 1;
5361 /* reinitialize for possible replay */
5363 server = cifs_pick_channel(ses);
5365 if (!ses || !(ses->server))
5368 if (smb3_encryption_required(tcon))
5369 flags |= CIFS_TRANSFORM_REQ;
5371 memset(&rqst, 0, sizeof(struct smb_rqst));
5372 memset(&iov, 0, sizeof(iov));
5374 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
5376 rc = SMB2_query_directory_init(xid, tcon, server,
5377 &rqst, persistent_fid,
5378 volatile_fid, index,
5379 srch_inf->info_level);
5384 smb2_set_replay(server, &rqst);
5386 rc = cifs_send_recv(xid, ses, server,
5387 &rqst, &resp_buftype, flags, &rsp_iov);
5388 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
5391 if (rc == -ENODATA &&
5392 rsp->hdr.Status == STATUS_NO_MORE_FILES) {
5393 trace_smb3_query_dir_done(xid, persistent_fid,
5394 tcon->tid, tcon->ses->Suid, index, 0);
5395 srch_inf->endOfSearch = true;
5398 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5399 tcon->ses->Suid, index, 0, rc);
5400 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
5405 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
5408 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5409 tcon->ses->Suid, index, 0, rc);
5412 resp_buftype = CIFS_NO_BUFFER;
5414 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
5415 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
5418 SMB2_query_directory_free(&rqst);
5419 free_rsp_buf(resp_buftype, rsp);
5421 if (is_replayable_error(rc) &&
5422 smb2_should_replay(tcon, &retries, &cur_sleep))
5429 SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
5430 struct smb_rqst *rqst,
5431 u64 persistent_fid, u64 volatile_fid, u32 pid,
5432 u8 info_class, u8 info_type, u32 additional_info,
5433 void **data, unsigned int *size)
5435 struct smb2_set_info_req *req;
5436 struct kvec *iov = rqst->rq_iov;
5437 unsigned int i, total_len;
5440 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
5441 (void **) &req, &total_len);
5445 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
5446 req->InfoType = info_type;
5447 req->FileInfoClass = info_class;
5448 req->PersistentFileId = persistent_fid;
5449 req->VolatileFileId = volatile_fid;
5450 req->AdditionalInformation = cpu_to_le32(additional_info);
5452 req->BufferOffset = cpu_to_le16(sizeof(struct smb2_set_info_req));
5453 req->BufferLength = cpu_to_le32(*size);
5455 memcpy(req->Buffer, *data, *size);
5458 iov[0].iov_base = (char *)req;
5460 iov[0].iov_len = total_len - 1;
5462 for (i = 1; i < rqst->rq_nvec; i++) {
5463 le32_add_cpu(&req->BufferLength, size[i]);
5464 iov[i].iov_base = (char *)data[i];
5465 iov[i].iov_len = size[i];
5472 SMB2_set_info_free(struct smb_rqst *rqst)
5474 if (rqst && rqst->rq_iov)
5475 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
5479 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
5480 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
5481 u8 info_type, u32 additional_info, unsigned int num,
5482 void **data, unsigned int *size)
5484 struct smb_rqst rqst;
5485 struct smb2_set_info_rsp *rsp = NULL;
5487 struct kvec rsp_iov;
5490 struct cifs_ses *ses = tcon->ses;
5491 struct TCP_Server_Info *server;
5493 int retries = 0, cur_sleep = 1;
5496 /* reinitialize for possible replay */
5498 server = cifs_pick_channel(ses);
5500 if (!ses || !server)
5506 if (smb3_encryption_required(tcon))
5507 flags |= CIFS_TRANSFORM_REQ;
5509 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
5513 memset(&rqst, 0, sizeof(struct smb_rqst));
5517 rc = SMB2_set_info_init(tcon, server,
5518 &rqst, persistent_fid, volatile_fid, pid,
5519 info_class, info_type, additional_info,
5527 smb2_set_replay(server, &rqst);
5529 rc = cifs_send_recv(xid, ses, server,
5530 &rqst, &resp_buftype, flags,
5532 SMB2_set_info_free(&rqst);
5533 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
5536 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
5537 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
5538 ses->Suid, info_class, (__u32)info_type, rc);
5541 free_rsp_buf(resp_buftype, rsp);
5544 if (is_replayable_error(rc) &&
5545 smb2_should_replay(tcon, &retries, &cur_sleep))
5552 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
5553 u64 volatile_fid, u32 pid, loff_t new_eof)
5555 struct smb2_file_eof_info info;
5559 info.EndOfFile = cpu_to_le64(new_eof);
5562 size = sizeof(struct smb2_file_eof_info);
5564 trace_smb3_set_eof(xid, persistent_fid, tcon->tid, tcon->ses->Suid, new_eof);
5566 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5567 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
5568 0, 1, &data, &size);
5572 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
5573 u64 persistent_fid, u64 volatile_fid,
5574 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
5576 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5577 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
5578 1, (void **)&pnntsd, &pacllen);
5582 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
5583 u64 persistent_fid, u64 volatile_fid,
5584 struct smb2_file_full_ea_info *buf, int len)
5586 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5587 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
5588 0, 1, (void **)&buf, &len);
5592 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
5593 const u64 persistent_fid, const u64 volatile_fid,
5596 struct smb_rqst rqst;
5598 struct smb2_oplock_break *req = NULL;
5599 struct cifs_ses *ses = tcon->ses;
5600 struct TCP_Server_Info *server;
5601 int flags = CIFS_OBREAK_OP;
5602 unsigned int total_len;
5604 struct kvec rsp_iov;
5606 int retries = 0, cur_sleep = 1;
5609 /* reinitialize for possible replay */
5610 flags = CIFS_OBREAK_OP;
5611 server = cifs_pick_channel(ses);
5613 cifs_dbg(FYI, "SMB2_oplock_break\n");
5614 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5615 (void **) &req, &total_len);
5619 if (smb3_encryption_required(tcon))
5620 flags |= CIFS_TRANSFORM_REQ;
5622 req->VolatileFid = volatile_fid;
5623 req->PersistentFid = persistent_fid;
5624 req->OplockLevel = oplock_level;
5625 req->hdr.CreditRequest = cpu_to_le16(1);
5627 flags |= CIFS_NO_RSP_BUF;
5629 iov[0].iov_base = (char *)req;
5630 iov[0].iov_len = total_len;
5632 memset(&rqst, 0, sizeof(struct smb_rqst));
5637 smb2_set_replay(server, &rqst);
5639 rc = cifs_send_recv(xid, ses, server,
5640 &rqst, &resp_buf_type, flags, &rsp_iov);
5641 cifs_small_buf_release(req);
5643 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5644 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
5647 if (is_replayable_error(rc) &&
5648 smb2_should_replay(tcon, &retries, &cur_sleep))
5655 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
5656 struct kstatfs *kst)
5658 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
5659 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
5660 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
5661 kst->f_bfree = kst->f_bavail =
5662 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
5667 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
5668 struct kstatfs *kst)
5670 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
5671 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
5672 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
5673 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
5674 kst->f_bavail = kst->f_bfree;
5676 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
5677 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5678 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
5679 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5680 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
5686 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
5687 struct TCP_Server_Info *server,
5688 int level, int outbuf_len, u64 persistent_fid,
5692 struct smb2_query_info_req *req;
5693 unsigned int total_len;
5695 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
5697 if ((tcon->ses == NULL) || server == NULL)
5700 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
5701 (void **) &req, &total_len);
5705 req->InfoType = SMB2_O_INFO_FILESYSTEM;
5706 req->FileInfoClass = level;
5707 req->PersistentFileId = persistent_fid;
5708 req->VolatileFileId = volatile_fid;
5710 req->InputBufferOffset =
5711 cpu_to_le16(sizeof(struct smb2_query_info_req));
5712 req->OutputBufferLength = cpu_to_le32(
5713 outbuf_len + sizeof(struct smb2_query_info_rsp));
5715 iov->iov_base = (char *)req;
5716 iov->iov_len = total_len;
5720 static inline void free_qfs_info_req(struct kvec *iov)
5722 cifs_buf_release(iov->iov_base);
5726 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
5727 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5729 struct smb_rqst rqst;
5730 struct smb2_query_info_rsp *rsp = NULL;
5732 struct kvec rsp_iov;
5735 struct cifs_ses *ses = tcon->ses;
5736 struct TCP_Server_Info *server;
5737 FILE_SYSTEM_POSIX_INFO *info = NULL;
5739 int retries = 0, cur_sleep = 1;
5742 /* reinitialize for possible replay */
5744 server = cifs_pick_channel(ses);
5746 rc = build_qfs_info_req(&iov, tcon, server,
5747 FS_POSIX_INFORMATION,
5748 sizeof(FILE_SYSTEM_POSIX_INFO),
5749 persistent_fid, volatile_fid);
5753 if (smb3_encryption_required(tcon))
5754 flags |= CIFS_TRANSFORM_REQ;
5756 memset(&rqst, 0, sizeof(struct smb_rqst));
5761 smb2_set_replay(server, &rqst);
5763 rc = cifs_send_recv(xid, ses, server,
5764 &rqst, &resp_buftype, flags, &rsp_iov);
5765 free_qfs_info_req(&iov);
5767 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5768 goto posix_qfsinf_exit;
5770 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5772 info = (FILE_SYSTEM_POSIX_INFO *)(
5773 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5774 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5775 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5776 sizeof(FILE_SYSTEM_POSIX_INFO));
5778 copy_posix_fs_info_to_kstatfs(info, fsdata);
5781 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5783 if (is_replayable_error(rc) &&
5784 smb2_should_replay(tcon, &retries, &cur_sleep))
5791 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5792 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5794 struct smb_rqst rqst;
5795 struct smb2_query_info_rsp *rsp = NULL;
5797 struct kvec rsp_iov;
5800 struct cifs_ses *ses = tcon->ses;
5801 struct TCP_Server_Info *server;
5802 struct smb2_fs_full_size_info *info = NULL;
5804 int retries = 0, cur_sleep = 1;
5807 /* reinitialize for possible replay */
5809 server = cifs_pick_channel(ses);
5811 rc = build_qfs_info_req(&iov, tcon, server,
5812 FS_FULL_SIZE_INFORMATION,
5813 sizeof(struct smb2_fs_full_size_info),
5814 persistent_fid, volatile_fid);
5818 if (smb3_encryption_required(tcon))
5819 flags |= CIFS_TRANSFORM_REQ;
5821 memset(&rqst, 0, sizeof(struct smb_rqst));
5826 smb2_set_replay(server, &rqst);
5828 rc = cifs_send_recv(xid, ses, server,
5829 &rqst, &resp_buftype, flags, &rsp_iov);
5830 free_qfs_info_req(&iov);
5832 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5835 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5837 info = (struct smb2_fs_full_size_info *)(
5838 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5839 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5840 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5841 sizeof(struct smb2_fs_full_size_info));
5843 smb2_copy_fs_info_to_kstatfs(info, fsdata);
5846 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5848 if (is_replayable_error(rc) &&
5849 smb2_should_replay(tcon, &retries, &cur_sleep))
5856 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
5857 u64 persistent_fid, u64 volatile_fid, int level)
5859 struct smb_rqst rqst;
5860 struct smb2_query_info_rsp *rsp = NULL;
5862 struct kvec rsp_iov;
5864 int resp_buftype, max_len, min_len;
5865 struct cifs_ses *ses = tcon->ses;
5866 struct TCP_Server_Info *server;
5867 unsigned int rsp_len, offset;
5869 int retries = 0, cur_sleep = 1;
5872 /* reinitialize for possible replay */
5874 server = cifs_pick_channel(ses);
5876 if (level == FS_DEVICE_INFORMATION) {
5877 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5878 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5879 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5880 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5881 min_len = MIN_FS_ATTR_INFO_SIZE;
5882 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5883 max_len = sizeof(struct smb3_fs_ss_info);
5884 min_len = sizeof(struct smb3_fs_ss_info);
5885 } else if (level == FS_VOLUME_INFORMATION) {
5886 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5887 min_len = sizeof(struct smb3_fs_vol_info);
5889 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
5893 rc = build_qfs_info_req(&iov, tcon, server,
5895 persistent_fid, volatile_fid);
5899 if (smb3_encryption_required(tcon))
5900 flags |= CIFS_TRANSFORM_REQ;
5902 memset(&rqst, 0, sizeof(struct smb_rqst));
5907 smb2_set_replay(server, &rqst);
5909 rc = cifs_send_recv(xid, ses, server,
5910 &rqst, &resp_buftype, flags, &rsp_iov);
5911 free_qfs_info_req(&iov);
5913 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5916 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5918 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5919 offset = le16_to_cpu(rsp->OutputBufferOffset);
5920 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
5924 if (level == FS_ATTRIBUTE_INFORMATION)
5925 memcpy(&tcon->fsAttrInfo, offset
5926 + (char *)rsp, min_t(unsigned int,
5928 else if (level == FS_DEVICE_INFORMATION)
5929 memcpy(&tcon->fsDevInfo, offset
5930 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
5931 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5932 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
5933 (offset + (char *)rsp);
5934 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5935 tcon->perf_sector_size =
5936 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
5937 } else if (level == FS_VOLUME_INFORMATION) {
5938 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5939 (offset + (char *)rsp);
5940 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5941 tcon->vol_create_time = vol_info->VolumeCreationTime;
5945 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5947 if (is_replayable_error(rc) &&
5948 smb2_should_replay(tcon, &retries, &cur_sleep))
5955 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5956 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5957 const __u32 num_lock, struct smb2_lock_element *buf)
5959 struct smb_rqst rqst;
5961 struct smb2_lock_req *req = NULL;
5963 struct kvec rsp_iov;
5966 int flags = CIFS_NO_RSP_BUF;
5967 unsigned int total_len;
5968 struct TCP_Server_Info *server;
5969 int retries = 0, cur_sleep = 1;
5972 /* reinitialize for possible replay */
5973 flags = CIFS_NO_RSP_BUF;
5974 server = cifs_pick_channel(tcon->ses);
5976 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
5978 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
5979 (void **) &req, &total_len);
5983 if (smb3_encryption_required(tcon))
5984 flags |= CIFS_TRANSFORM_REQ;
5986 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
5987 req->LockCount = cpu_to_le16(num_lock);
5989 req->PersistentFileId = persist_fid;
5990 req->VolatileFileId = volatile_fid;
5992 count = num_lock * sizeof(struct smb2_lock_element);
5994 iov[0].iov_base = (char *)req;
5995 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
5996 iov[1].iov_base = (char *)buf;
5997 iov[1].iov_len = count;
5999 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
6001 memset(&rqst, 0, sizeof(struct smb_rqst));
6006 smb2_set_replay(server, &rqst);
6008 rc = cifs_send_recv(xid, tcon->ses, server,
6009 &rqst, &resp_buf_type, flags,
6011 cifs_small_buf_release(req);
6013 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
6014 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
6015 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
6016 tcon->ses->Suid, rc);
6019 if (is_replayable_error(rc) &&
6020 smb2_should_replay(tcon, &retries, &cur_sleep))
6027 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
6028 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
6029 const __u64 length, const __u64 offset, const __u32 lock_flags,
6032 struct smb2_lock_element lock;
6034 lock.Offset = cpu_to_le64(offset);
6035 lock.Length = cpu_to_le64(length);
6036 lock.Flags = cpu_to_le32(lock_flags);
6037 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
6038 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
6040 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
6044 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
6045 __u8 *lease_key, const __le32 lease_state)
6047 struct smb_rqst rqst;
6049 struct smb2_lease_ack *req = NULL;
6050 struct cifs_ses *ses = tcon->ses;
6051 int flags = CIFS_OBREAK_OP;
6052 unsigned int total_len;
6054 struct kvec rsp_iov;
6056 __u64 *please_key_high;
6057 __u64 *please_key_low;
6058 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
6060 cifs_dbg(FYI, "SMB2_lease_break\n");
6061 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
6062 (void **) &req, &total_len);
6066 if (smb3_encryption_required(tcon))
6067 flags |= CIFS_TRANSFORM_REQ;
6069 req->hdr.CreditRequest = cpu_to_le16(1);
6070 req->StructureSize = cpu_to_le16(36);
6073 memcpy(req->LeaseKey, lease_key, 16);
6074 req->LeaseState = lease_state;
6076 flags |= CIFS_NO_RSP_BUF;
6078 iov[0].iov_base = (char *)req;
6079 iov[0].iov_len = total_len;
6081 memset(&rqst, 0, sizeof(struct smb_rqst));
6085 rc = cifs_send_recv(xid, ses, server,
6086 &rqst, &resp_buf_type, flags, &rsp_iov);
6087 cifs_small_buf_release(req);
6089 please_key_low = (__u64 *)lease_key;
6090 please_key_high = (__u64 *)(lease_key+8);
6092 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
6093 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
6094 ses->Suid, *please_key_low, *please_key_high, rc);
6095 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
6097 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
6098 ses->Suid, *please_key_low, *please_key_high);