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) || (rc == -EKEYEXPIRED) || (rc == -EKEYREVOKED)) {
372 * Try alternate password for next reconnect (key rotation
373 * could be enabled on the server e.g.) if an alternate
374 * password is available and the current password is expired,
375 * but do not swap on non pwd related errors like host down
378 swap(ses->password2, ses->password);
381 if ((rc == -EACCES) && !tcon->retry) {
382 mutex_unlock(&ses->session_mutex);
386 mutex_unlock(&ses->session_mutex);
390 mutex_unlock(&ses->session_mutex);
395 if (!tcon->need_reconnect) {
396 mutex_unlock(&ses->session_mutex);
399 cifs_mark_open_files_invalid(tcon);
400 if (tcon->use_persistent)
401 tcon->need_reopen_files = true;
403 rc = cifs_tree_connect(0, tcon, nls_codepage);
405 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
407 /* If sess reconnected but tcon didn't, something strange ... */
408 mutex_unlock(&ses->session_mutex);
409 cifs_dbg(VFS, "reconnect tcon failed rc = %d\n", rc);
413 spin_lock(&ses->ses_lock);
414 if (ses->flags & CIFS_SES_FLAG_SCALE_CHANNELS) {
415 spin_unlock(&ses->ses_lock);
416 mutex_unlock(&ses->session_mutex);
417 goto skip_add_channels;
419 ses->flags |= CIFS_SES_FLAG_SCALE_CHANNELS;
420 spin_unlock(&ses->ses_lock);
423 (server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL) &&
424 server->ops->query_server_interfaces) {
425 mutex_unlock(&ses->session_mutex);
428 * query server network interfaces, in case they change
431 rc = server->ops->query_server_interfaces(xid, tcon, false);
434 if (rc == -EOPNOTSUPP && ses->chan_count > 1) {
436 * some servers like Azure SMB server do not advertise
437 * that multichannel has been disabled with server
438 * capabilities, rather return STATUS_NOT_IMPLEMENTED.
439 * treat this as server not supporting multichannel
442 rc = cifs_chan_skip_or_disable(ses, server,
444 goto skip_add_channels;
446 cifs_dbg(FYI, "%s: failed to query server interfaces: %d\n",
449 if (ses->chan_max > ses->chan_count &&
451 !SERVER_IS_CHAN(server)) {
452 if (ses->chan_count == 1) {
453 cifs_server_dbg(VFS, "supports multichannel now\n");
454 queue_delayed_work(cifsiod_wq, &tcon->query_interfaces,
455 (SMB_INTERFACE_POLL_INTERVAL * HZ));
458 cifs_try_adding_channels(ses);
461 mutex_unlock(&ses->session_mutex);
465 spin_lock(&ses->ses_lock);
466 ses->flags &= ~CIFS_SES_FLAG_SCALE_CHANNELS;
467 spin_unlock(&ses->ses_lock);
469 if (smb2_command != SMB2_INTERNAL_CMD)
470 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
472 atomic_inc(&tconInfoReconnectCount);
475 * Check if handle based operation so we know whether we can continue
476 * or not without returning to caller to reset file handle.
479 * BB Is flush done by server on drop of tcp session? Should we special
480 * case it and skip above?
482 switch (smb2_command) {
487 case SMB2_QUERY_DIRECTORY:
488 case SMB2_CHANGE_NOTIFY:
489 case SMB2_QUERY_INFO:
498 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon,
499 struct TCP_Server_Info *server,
501 unsigned int *total_len)
503 struct smb2_pdu *spdu = buf;
504 /* lookup word count ie StructureSize from table */
505 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
508 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
509 * largest operations (Create)
513 smb2_hdr_assemble(&spdu->hdr, smb2_command, tcon, server);
514 spdu->StructureSize2 = cpu_to_le16(parmsize);
516 *total_len = parmsize + sizeof(struct smb2_hdr);
520 * Allocate and return pointer to an SMB request hdr, and set basic
521 * SMB information in the SMB header. If the return code is zero, this
522 * function must have filled in request_buf pointer.
524 static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
525 struct TCP_Server_Info *server,
526 void **request_buf, unsigned int *total_len)
528 /* BB eventually switch this to SMB2 specific small buf size */
529 switch (smb2_command) {
531 case SMB2_QUERY_INFO:
532 *request_buf = cifs_buf_get();
535 *request_buf = cifs_small_buf_get();
538 if (*request_buf == NULL) {
539 /* BB should we add a retry in here if not a writepage? */
543 fill_small_buf(smb2_command, tcon, server,
544 (struct smb2_hdr *)(*request_buf),
548 uint16_t com_code = le16_to_cpu(smb2_command);
549 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
550 cifs_stats_inc(&tcon->num_smbs_sent);
556 static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
557 struct TCP_Server_Info *server,
558 void **request_buf, unsigned int *total_len)
562 rc = smb2_reconnect(smb2_command, tcon, server, false);
566 return __smb2_plain_req_init(smb2_command, tcon, server, request_buf,
570 static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
571 struct TCP_Server_Info *server,
572 void **request_buf, unsigned int *total_len)
574 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
575 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
576 return __smb2_plain_req_init(SMB2_IOCTL, tcon, server,
577 request_buf, total_len);
579 return smb2_plain_req_init(SMB2_IOCTL, tcon, server,
580 request_buf, total_len);
583 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
586 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
588 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
589 pneg_ctxt->DataLength = cpu_to_le16(38);
590 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
591 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
592 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
593 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
597 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
599 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
600 pneg_ctxt->DataLength =
601 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
602 - sizeof(struct smb2_neg_context));
603 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
604 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
605 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
606 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
610 build_signing_ctxt(struct smb2_signing_capabilities *pneg_ctxt)
612 unsigned int ctxt_len = sizeof(struct smb2_signing_capabilities);
613 unsigned short num_algs = 1; /* number of signing algorithms sent */
615 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
617 * Context Data length must be rounded to multiple of 8 for some servers
619 pneg_ctxt->DataLength = cpu_to_le16(ALIGN(sizeof(struct smb2_signing_capabilities) -
620 sizeof(struct smb2_neg_context) +
621 (num_algs * sizeof(u16)), 8));
622 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(num_algs);
623 pneg_ctxt->SigningAlgorithms[0] = cpu_to_le16(SIGNING_ALG_AES_CMAC);
625 ctxt_len += sizeof(__le16) * num_algs;
626 ctxt_len = ALIGN(ctxt_len, 8);
628 /* TBD add SIGNING_ALG_AES_GMAC and/or SIGNING_ALG_HMAC_SHA256 */
632 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
634 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
635 if (require_gcm_256) {
636 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + 1 cipher */
637 pneg_ctxt->CipherCount = cpu_to_le16(1);
638 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES256_GCM;
639 } else if (enable_gcm_256) {
640 pneg_ctxt->DataLength = cpu_to_le16(8); /* Cipher Count + 3 ciphers */
641 pneg_ctxt->CipherCount = cpu_to_le16(3);
642 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
643 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES256_GCM;
644 pneg_ctxt->Ciphers[2] = SMB2_ENCRYPTION_AES128_CCM;
646 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + 2 ciphers */
647 pneg_ctxt->CipherCount = cpu_to_le16(2);
648 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
649 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
654 build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
656 struct nls_table *cp = load_nls_default();
658 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
660 /* copy up to max of first 100 bytes of server name to NetName field */
661 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
662 /* context size is DataLength + minimal smb2_neg_context */
663 return ALIGN(le16_to_cpu(pneg_ctxt->DataLength) + sizeof(struct smb2_neg_context), 8);
667 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
669 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
670 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
671 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
672 pneg_ctxt->Name[0] = 0x93;
673 pneg_ctxt->Name[1] = 0xAD;
674 pneg_ctxt->Name[2] = 0x25;
675 pneg_ctxt->Name[3] = 0x50;
676 pneg_ctxt->Name[4] = 0x9C;
677 pneg_ctxt->Name[5] = 0xB4;
678 pneg_ctxt->Name[6] = 0x11;
679 pneg_ctxt->Name[7] = 0xE7;
680 pneg_ctxt->Name[8] = 0xB4;
681 pneg_ctxt->Name[9] = 0x23;
682 pneg_ctxt->Name[10] = 0x83;
683 pneg_ctxt->Name[11] = 0xDE;
684 pneg_ctxt->Name[12] = 0x96;
685 pneg_ctxt->Name[13] = 0x8B;
686 pneg_ctxt->Name[14] = 0xCD;
687 pneg_ctxt->Name[15] = 0x7C;
691 assemble_neg_contexts(struct smb2_negotiate_req *req,
692 struct TCP_Server_Info *server, unsigned int *total_len)
694 unsigned int ctxt_len, neg_context_count;
695 struct TCP_Server_Info *pserver;
699 if (*total_len > 200) {
700 /* In case length corrupted don't want to overrun smb buffer */
701 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
706 * round up total_len of fixed part of SMB3 negotiate request to 8
707 * byte boundary before adding negotiate contexts
709 *total_len = ALIGN(*total_len, 8);
711 pneg_ctxt = (*total_len) + (char *)req;
712 req->NegotiateContextOffset = cpu_to_le32(*total_len);
714 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
715 ctxt_len = ALIGN(sizeof(struct smb2_preauth_neg_context), 8);
716 *total_len += ctxt_len;
717 pneg_ctxt += ctxt_len;
719 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
720 ctxt_len = ALIGN(sizeof(struct smb2_encryption_neg_context), 8);
721 *total_len += ctxt_len;
722 pneg_ctxt += ctxt_len;
725 * secondary channels don't have the hostname field populated
726 * use the hostname field in the primary channel instead
728 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
729 cifs_server_lock(pserver);
730 hostname = pserver->hostname;
731 if (hostname && (hostname[0] != 0)) {
732 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
734 *total_len += ctxt_len;
735 pneg_ctxt += ctxt_len;
736 neg_context_count = 3;
738 neg_context_count = 2;
739 cifs_server_unlock(pserver);
741 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
742 *total_len += sizeof(struct smb2_posix_neg_context);
743 pneg_ctxt += sizeof(struct smb2_posix_neg_context);
746 if (server->compression.requested) {
747 build_compression_ctxt((struct smb2_compression_capabilities_context *)
749 ctxt_len = ALIGN(sizeof(struct smb2_compression_capabilities_context), 8);
750 *total_len += ctxt_len;
751 pneg_ctxt += ctxt_len;
755 if (enable_negotiate_signing) {
756 ctxt_len = build_signing_ctxt((struct smb2_signing_capabilities *)
758 *total_len += ctxt_len;
759 pneg_ctxt += ctxt_len;
763 /* check for and add transport_capabilities and signing capabilities */
764 req->NegotiateContextCount = cpu_to_le16(neg_context_count);
768 /* If invalid preauth context warn but use what we requested, SHA-512 */
769 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
771 unsigned int len = le16_to_cpu(ctxt->DataLength);
774 * Caller checked that DataLength remains within SMB boundary. We still
775 * need to confirm that one HashAlgorithms member is accounted for.
777 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
778 pr_warn_once("server sent bad preauth context\n");
780 } else if (len < MIN_PREAUTH_CTXT_DATA_LEN + le16_to_cpu(ctxt->SaltLength)) {
781 pr_warn_once("server sent invalid SaltLength\n");
784 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
785 pr_warn_once("Invalid SMB3 hash algorithm count\n");
786 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
787 pr_warn_once("unknown SMB3 hash algorithm\n");
790 static void decode_compress_ctx(struct TCP_Server_Info *server,
791 struct smb2_compression_capabilities_context *ctxt)
793 unsigned int len = le16_to_cpu(ctxt->DataLength);
796 server->compression.enabled = false;
799 * Caller checked that DataLength remains within SMB boundary. We still
800 * need to confirm that one CompressionAlgorithms member is accounted
804 pr_warn_once("server sent bad compression cntxt\n");
808 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
809 pr_warn_once("invalid SMB3 compress algorithm count\n");
813 alg = ctxt->CompressionAlgorithms[0];
815 /* 'NONE' (0) compressor type is never negotiated */
816 if (alg == 0 || le16_to_cpu(alg) > 3) {
817 pr_warn_once("invalid compression algorithm '%u'\n", alg);
821 server->compression.alg = alg;
822 server->compression.enabled = true;
825 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
826 struct smb2_encryption_neg_context *ctxt)
828 unsigned int len = le16_to_cpu(ctxt->DataLength);
830 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
832 * Caller checked that DataLength remains within SMB boundary. We still
833 * need to confirm that one Cipher flexible array member is accounted
836 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
837 pr_warn_once("server sent bad crypto ctxt len\n");
841 if (le16_to_cpu(ctxt->CipherCount) != 1) {
842 pr_warn_once("Invalid SMB3.11 cipher count\n");
845 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
846 if (require_gcm_256) {
847 if (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM) {
848 cifs_dbg(VFS, "Server does not support requested encryption type (AES256 GCM)\n");
851 } else if (ctxt->Ciphers[0] == 0) {
853 * e.g. if server only supported AES256_CCM (very unlikely)
854 * or server supported no encryption types or had all disabled.
855 * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case
856 * in which mount requested encryption ("seal") checks later
857 * on during tree connection will return proper rc, but if
858 * seal not requested by client, since server is allowed to
859 * return 0 to indicate no supported cipher, we can't fail here
861 server->cipher_type = 0;
862 server->capabilities &= ~SMB2_GLOBAL_CAP_ENCRYPTION;
863 pr_warn_once("Server does not support requested encryption types\n");
865 } else if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
866 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM) &&
867 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM)) {
868 /* server returned a cipher we didn't ask for */
869 pr_warn_once("Invalid SMB3.11 cipher returned\n");
872 server->cipher_type = ctxt->Ciphers[0];
873 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
877 static void decode_signing_ctx(struct TCP_Server_Info *server,
878 struct smb2_signing_capabilities *pctxt)
880 unsigned int len = le16_to_cpu(pctxt->DataLength);
883 * Caller checked that DataLength remains within SMB boundary. We still
884 * need to confirm that one SigningAlgorithms flexible array member is
887 if ((len < 4) || (len > 16)) {
888 pr_warn_once("server sent bad signing negcontext\n");
891 if (le16_to_cpu(pctxt->SigningAlgorithmCount) != 1) {
892 pr_warn_once("Invalid signing algorithm count\n");
895 if (le16_to_cpu(pctxt->SigningAlgorithms[0]) > 2) {
896 pr_warn_once("unknown signing algorithm\n");
900 server->signing_negotiated = true;
901 server->signing_algorithm = le16_to_cpu(pctxt->SigningAlgorithms[0]);
902 cifs_dbg(FYI, "signing algorithm %d chosen\n",
903 server->signing_algorithm);
907 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
908 struct TCP_Server_Info *server,
909 unsigned int len_of_smb)
911 struct smb2_neg_context *pctx;
912 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
913 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
914 unsigned int len_of_ctxts, i;
917 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
918 if (len_of_smb <= offset) {
919 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
923 len_of_ctxts = len_of_smb - offset;
925 for (i = 0; i < ctxt_cnt; i++) {
927 /* check that offset is not beyond end of SMB */
928 if (len_of_ctxts < sizeof(struct smb2_neg_context))
931 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
932 clen = sizeof(struct smb2_neg_context)
933 + le16_to_cpu(pctx->DataLength);
935 * 2.2.4 SMB2 NEGOTIATE Response
936 * Subsequent negotiate contexts MUST appear at the first 8-byte
937 * aligned offset following the previous negotiate context.
939 if (i + 1 != ctxt_cnt)
940 clen = ALIGN(clen, 8);
941 if (clen > len_of_ctxts)
944 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
945 decode_preauth_context(
946 (struct smb2_preauth_neg_context *)pctx);
947 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
948 rc = decode_encrypt_ctx(server,
949 (struct smb2_encryption_neg_context *)pctx);
950 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
951 decode_compress_ctx(server,
952 (struct smb2_compression_capabilities_context *)pctx);
953 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
954 server->posix_ext_supported = true;
955 else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES)
956 decode_signing_ctx(server,
957 (struct smb2_signing_capabilities *)pctx);
959 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
960 le16_to_cpu(pctx->ContextType));
965 len_of_ctxts -= clen;
970 static struct create_posix *
971 create_posix_buf(umode_t mode)
973 struct create_posix *buf;
975 buf = kzalloc(sizeof(struct create_posix),
980 buf->ccontext.DataOffset =
981 cpu_to_le16(offsetof(struct create_posix, Mode));
982 buf->ccontext.DataLength = cpu_to_le32(4);
983 buf->ccontext.NameOffset =
984 cpu_to_le16(offsetof(struct create_posix, Name));
985 buf->ccontext.NameLength = cpu_to_le16(16);
987 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
998 buf->Name[10] = 0x83;
999 buf->Name[11] = 0xDE;
1000 buf->Name[12] = 0x96;
1001 buf->Name[13] = 0x8B;
1002 buf->Name[14] = 0xCD;
1003 buf->Name[15] = 0x7C;
1004 buf->Mode = cpu_to_le32(mode);
1005 cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
1010 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
1012 unsigned int num = *num_iovec;
1014 iov[num].iov_base = create_posix_buf(mode);
1015 if (mode == ACL_NO_MODE)
1016 cifs_dbg(FYI, "%s: no mode\n", __func__);
1017 if (iov[num].iov_base == NULL)
1019 iov[num].iov_len = sizeof(struct create_posix);
1020 *num_iovec = num + 1;
1027 * SMB2 Worker functions follow:
1029 * The general structure of the worker functions is:
1030 * 1) Call smb2_init (assembles SMB2 header)
1031 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
1032 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
1033 * 4) Decode SMB2 command specific fields in the fixed length area
1034 * 5) Decode variable length data area (if any for this SMB2 command type)
1035 * 6) Call free smb buffer
1041 SMB2_negotiate(const unsigned int xid,
1042 struct cifs_ses *ses,
1043 struct TCP_Server_Info *server)
1045 struct smb_rqst rqst;
1046 struct smb2_negotiate_req *req;
1047 struct smb2_negotiate_rsp *rsp;
1049 struct kvec rsp_iov;
1052 int blob_offset, blob_length;
1053 char *security_blob;
1054 int flags = CIFS_NEG_OP;
1055 unsigned int total_len;
1057 cifs_dbg(FYI, "Negotiate protocol\n");
1060 WARN(1, "%s: server is NULL!\n", __func__);
1064 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
1065 (void **) &req, &total_len);
1069 req->hdr.SessionId = 0;
1071 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
1072 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
1074 if (strcmp(server->vals->version_string,
1075 SMB3ANY_VERSION_STRING) == 0) {
1076 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1077 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1078 req->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1079 req->DialectCount = cpu_to_le16(3);
1081 } else if (strcmp(server->vals->version_string,
1082 SMBDEFAULT_VERSION_STRING) == 0) {
1083 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1084 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1085 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1086 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1087 req->DialectCount = cpu_to_le16(4);
1090 /* otherwise send specific dialect */
1091 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
1092 req->DialectCount = cpu_to_le16(1);
1096 /* only one of SMB2 signing flags may be set in SMB2 request */
1098 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1099 else if (global_secflags & CIFSSEC_MAY_SIGN)
1100 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1102 req->SecurityMode = 0;
1104 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
1105 if (ses->chan_max > 1)
1106 req->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
1108 /* ClientGUID must be zero for SMB2.02 dialect */
1109 if (server->vals->protocol_id == SMB20_PROT_ID)
1110 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
1112 memcpy(req->ClientGUID, server->client_guid,
1113 SMB2_CLIENT_GUID_SIZE);
1114 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
1115 (strcmp(server->vals->version_string,
1116 SMB3ANY_VERSION_STRING) == 0) ||
1117 (strcmp(server->vals->version_string,
1118 SMBDEFAULT_VERSION_STRING) == 0))
1119 assemble_neg_contexts(req, server, &total_len);
1121 iov[0].iov_base = (char *)req;
1122 iov[0].iov_len = total_len;
1124 memset(&rqst, 0, sizeof(struct smb_rqst));
1128 rc = cifs_send_recv(xid, ses, server,
1129 &rqst, &resp_buftype, flags, &rsp_iov);
1130 cifs_small_buf_release(req);
1131 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
1133 * No tcon so can't do
1134 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1136 if (rc == -EOPNOTSUPP) {
1137 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");
1143 if (strcmp(server->vals->version_string,
1144 SMB3ANY_VERSION_STRING) == 0) {
1145 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
1146 cifs_server_dbg(VFS,
1147 "SMB2 dialect returned but not requested\n");
1149 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
1150 cifs_server_dbg(VFS,
1151 "SMB2.1 dialect returned but not requested\n");
1153 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1154 /* ops set to 3.0 by default for default so update */
1155 server->ops = &smb311_operations;
1156 server->vals = &smb311_values;
1158 } else if (strcmp(server->vals->version_string,
1159 SMBDEFAULT_VERSION_STRING) == 0) {
1160 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
1161 cifs_server_dbg(VFS,
1162 "SMB2 dialect returned but not requested\n");
1164 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
1165 /* ops set to 3.0 by default for default so update */
1166 server->ops = &smb21_operations;
1167 server->vals = &smb21_values;
1168 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1169 server->ops = &smb311_operations;
1170 server->vals = &smb311_values;
1172 } else if (le16_to_cpu(rsp->DialectRevision) !=
1173 server->vals->protocol_id) {
1174 /* if requested single dialect ensure returned dialect matched */
1175 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
1176 le16_to_cpu(rsp->DialectRevision));
1180 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
1182 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
1183 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
1184 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
1185 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
1186 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
1187 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
1188 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
1189 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
1190 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
1191 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
1193 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
1194 le16_to_cpu(rsp->DialectRevision));
1199 server->dialect = le16_to_cpu(rsp->DialectRevision);
1202 * Keep a copy of the hash after negprot. This hash will be
1203 * the starting hash value for all sessions made from this
1206 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
1207 SMB2_PREAUTH_HASH_SIZE);
1209 /* SMB2 only has an extended negflavor */
1210 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
1211 /* set it to the maximum buffer size value we can send with 1 credit */
1212 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
1213 SMB2_MAX_BUFFER_SIZE);
1214 server->max_read = le32_to_cpu(rsp->MaxReadSize);
1215 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
1216 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
1217 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
1218 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
1220 server->capabilities = le32_to_cpu(rsp->Capabilities);
1221 /* Internal types */
1222 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
1225 * SMB3.0 supports only 1 cipher and doesn't have a encryption neg context
1226 * Set the cipher type manually.
1228 if (server->dialect == SMB30_PROT_ID && (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1229 server->cipher_type = SMB2_ENCRYPTION_AES128_CCM;
1231 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
1232 (struct smb2_hdr *)rsp);
1234 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
1236 * ses->sectype = RawNTLMSSP;
1237 * but for time being this is our only auth choice so doesn't matter.
1238 * We just found a server which sets blob length to zero expecting raw.
1240 if (blob_length == 0) {
1241 cifs_dbg(FYI, "missing security blob on negprot\n");
1242 server->sec_ntlmssp = true;
1245 rc = cifs_enable_signing(server, ses->sign);
1249 rc = decode_negTokenInit(security_blob, blob_length, server);
1256 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1257 if (rsp->NegotiateContextCount)
1258 rc = smb311_decode_neg_context(rsp, server,
1261 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
1264 free_rsp_buf(resp_buftype, rsp);
1268 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1271 struct validate_negotiate_info_req *pneg_inbuf;
1272 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
1274 u32 inbuflen; /* max of 4 dialects */
1275 struct TCP_Server_Info *server = tcon->ses->server;
1277 cifs_dbg(FYI, "validate negotiate\n");
1279 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1280 if (server->dialect == SMB311_PROT_ID)
1284 * validation ioctl must be signed, so no point sending this if we
1285 * can not sign it (ie are not known user). Even if signing is not
1286 * required (enabled but not negotiated), in those cases we selectively
1287 * sign just this, the first and only signed request on a connection.
1288 * Having validation of negotiate info helps reduce attack vectors.
1290 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1291 return 0; /* validation requires signing */
1293 if (tcon->ses->user_name == NULL) {
1294 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1295 return 0; /* validation requires signing */
1298 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1299 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1301 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1305 pneg_inbuf->Capabilities =
1306 cpu_to_le32(server->vals->req_capabilities);
1307 if (tcon->ses->chan_max > 1)
1308 pneg_inbuf->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
1310 memcpy(pneg_inbuf->Guid, server->client_guid,
1311 SMB2_CLIENT_GUID_SIZE);
1313 if (tcon->ses->sign)
1314 pneg_inbuf->SecurityMode =
1315 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1316 else if (global_secflags & CIFSSEC_MAY_SIGN)
1317 pneg_inbuf->SecurityMode =
1318 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1320 pneg_inbuf->SecurityMode = 0;
1323 if (strcmp(server->vals->version_string,
1324 SMB3ANY_VERSION_STRING) == 0) {
1325 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1326 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1327 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1328 pneg_inbuf->DialectCount = cpu_to_le16(3);
1329 /* SMB 2.1 not included so subtract one dialect from len */
1330 inbuflen = sizeof(*pneg_inbuf) -
1331 (sizeof(pneg_inbuf->Dialects[0]));
1332 } else if (strcmp(server->vals->version_string,
1333 SMBDEFAULT_VERSION_STRING) == 0) {
1334 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1335 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1336 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1337 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1338 pneg_inbuf->DialectCount = cpu_to_le16(4);
1339 /* structure is big enough for 4 dialects */
1340 inbuflen = sizeof(*pneg_inbuf);
1342 /* otherwise specific dialect was requested */
1343 pneg_inbuf->Dialects[0] =
1344 cpu_to_le16(server->vals->protocol_id);
1345 pneg_inbuf->DialectCount = cpu_to_le16(1);
1346 /* structure is big enough for 4 dialects, sending only 1 */
1347 inbuflen = sizeof(*pneg_inbuf) -
1348 sizeof(pneg_inbuf->Dialects[0]) * 3;
1351 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1352 FSCTL_VALIDATE_NEGOTIATE_INFO,
1353 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1354 (char **)&pneg_rsp, &rsplen);
1355 if (rc == -EOPNOTSUPP) {
1357 * Old Windows versions or Netapp SMB server can return
1358 * not supported error. Client should accept it.
1360 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1362 goto out_free_inbuf;
1363 } else if (rc != 0) {
1364 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1367 goto out_free_inbuf;
1371 if (rsplen != sizeof(*pneg_rsp)) {
1372 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1375 /* relax check since Mac returns max bufsize allowed on ioctl */
1376 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1380 /* check validate negotiate info response matches what we got earlier */
1381 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1384 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1387 /* do not validate server guid because not saved at negprot time yet */
1389 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1390 SMB2_LARGE_FILES) != server->capabilities)
1393 /* validate negotiate successful */
1395 cifs_dbg(FYI, "validate negotiate info successful\n");
1399 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1408 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1410 switch (requested) {
1417 if (server->sec_ntlmssp &&
1418 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1420 if ((server->sec_kerberos || server->sec_mskerberos) &&
1421 (global_secflags & CIFSSEC_MAY_KRB5))
1429 struct SMB2_sess_data {
1431 struct cifs_ses *ses;
1432 struct TCP_Server_Info *server;
1433 struct nls_table *nls_cp;
1434 void (*func)(struct SMB2_sess_data *);
1436 u64 previous_session;
1438 /* we will send the SMB in three pieces:
1439 * a fixed length beginning part, an optional
1440 * SPNEGO blob (which can be zero length), and a
1441 * last part which will include the strings
1442 * and rest of bcc area. This allows us to avoid
1443 * a large buffer 17K allocation
1450 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1453 struct cifs_ses *ses = sess_data->ses;
1454 struct TCP_Server_Info *server = sess_data->server;
1455 struct smb2_sess_setup_req *req;
1456 unsigned int total_len;
1457 bool is_binding = false;
1459 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
1465 spin_lock(&ses->ses_lock);
1466 is_binding = (ses->ses_status == SES_GOOD);
1467 spin_unlock(&ses->ses_lock);
1470 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1471 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1472 req->PreviousSessionId = 0;
1473 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1474 cifs_dbg(FYI, "Binding to sess id: %llx\n", ses->Suid);
1476 /* First session, not a reauthenticate */
1477 req->hdr.SessionId = 0;
1479 * if reconnect, we need to send previous sess id
1482 req->PreviousSessionId = cpu_to_le64(sess_data->previous_session);
1483 req->Flags = 0; /* MBZ */
1484 cifs_dbg(FYI, "Fresh session. Previous: %llx\n",
1485 sess_data->previous_session);
1488 /* enough to enable echos and oplocks and one max size write */
1489 if (server->credits >= server->max_credits)
1490 req->hdr.CreditRequest = cpu_to_le16(0);
1492 req->hdr.CreditRequest = cpu_to_le16(
1493 min_t(int, server->max_credits -
1494 server->credits, 130));
1496 /* only one of SMB2 signing flags may be set in SMB2 request */
1498 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1499 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1500 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1502 req->SecurityMode = 0;
1504 #ifdef CONFIG_CIFS_DFS_UPCALL
1505 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1507 req->Capabilities = 0;
1508 #endif /* DFS_UPCALL */
1510 req->Channel = 0; /* MBZ */
1512 sess_data->iov[0].iov_base = (char *)req;
1514 sess_data->iov[0].iov_len = total_len - 1;
1516 * This variable will be used to clear the buffer
1517 * allocated above in case of any error in the calling function.
1519 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1525 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1527 struct kvec *iov = sess_data->iov;
1529 /* iov[1] is already freed by caller */
1530 if (sess_data->buf0_type != CIFS_NO_BUFFER && iov[0].iov_base)
1531 memzero_explicit(iov[0].iov_base, iov[0].iov_len);
1533 free_rsp_buf(sess_data->buf0_type, iov[0].iov_base);
1534 sess_data->buf0_type = CIFS_NO_BUFFER;
1538 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1541 struct smb_rqst rqst;
1542 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1543 struct kvec rsp_iov = { NULL, 0 };
1545 /* Testing shows that buffer offset must be at location of Buffer[0] */
1546 req->SecurityBufferOffset =
1547 cpu_to_le16(sizeof(struct smb2_sess_setup_req));
1548 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1550 memset(&rqst, 0, sizeof(struct smb_rqst));
1551 rqst.rq_iov = sess_data->iov;
1554 /* BB add code to build os and lm fields */
1555 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1558 &sess_data->buf0_type,
1559 CIFS_LOG_ERROR | CIFS_SESS_OP, &rsp_iov);
1560 cifs_small_buf_release(sess_data->iov[0].iov_base);
1562 sess_data->ses->expired_pwd = false;
1563 else if ((rc == -EACCES) || (rc == -EKEYEXPIRED) || (rc == -EKEYREVOKED))
1564 sess_data->ses->expired_pwd = true;
1566 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1572 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1575 struct cifs_ses *ses = sess_data->ses;
1576 struct TCP_Server_Info *server = sess_data->server;
1578 cifs_server_lock(server);
1579 if (server->ops->generate_signingkey) {
1580 rc = server->ops->generate_signingkey(ses, server);
1583 "SMB3 session key generation failed\n");
1584 cifs_server_unlock(server);
1588 if (!server->session_estab) {
1589 server->sequence_number = 0x2;
1590 server->session_estab = true;
1592 cifs_server_unlock(server);
1594 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1598 #ifdef CONFIG_CIFS_UPCALL
1600 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1603 struct cifs_ses *ses = sess_data->ses;
1604 struct TCP_Server_Info *server = sess_data->server;
1605 struct cifs_spnego_msg *msg;
1606 struct key *spnego_key = NULL;
1607 struct smb2_sess_setup_rsp *rsp = NULL;
1608 bool is_binding = false;
1610 rc = SMB2_sess_alloc_buffer(sess_data);
1614 spnego_key = cifs_get_spnego_key(ses, server);
1615 if (IS_ERR(spnego_key)) {
1616 rc = PTR_ERR(spnego_key);
1618 cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
1623 msg = spnego_key->payload.data[0];
1625 * check version field to make sure that cifs.upcall is
1626 * sending us a response in an expected form
1628 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1629 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1630 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1632 goto out_put_spnego_key;
1635 spin_lock(&ses->ses_lock);
1636 is_binding = (ses->ses_status == SES_GOOD);
1637 spin_unlock(&ses->ses_lock);
1639 /* keep session key if binding */
1641 kfree_sensitive(ses->auth_key.response);
1642 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1644 if (!ses->auth_key.response) {
1645 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1648 goto out_put_spnego_key;
1650 ses->auth_key.len = msg->sesskey_len;
1653 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1654 sess_data->iov[1].iov_len = msg->secblob_len;
1656 rc = SMB2_sess_sendreceive(sess_data);
1658 goto out_put_spnego_key;
1660 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1661 /* keep session id and flags if binding */
1663 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1664 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1667 rc = SMB2_sess_establish_session(sess_data);
1669 key_invalidate(spnego_key);
1670 key_put(spnego_key);
1672 kfree_sensitive(ses->auth_key.response);
1673 ses->auth_key.response = NULL;
1674 ses->auth_key.len = 0;
1677 sess_data->result = rc;
1678 sess_data->func = NULL;
1679 SMB2_sess_free_buffer(sess_data);
1683 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1685 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1686 sess_data->result = -EOPNOTSUPP;
1687 sess_data->func = NULL;
1692 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1695 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1698 struct cifs_ses *ses = sess_data->ses;
1699 struct TCP_Server_Info *server = sess_data->server;
1700 struct smb2_sess_setup_rsp *rsp = NULL;
1701 unsigned char *ntlmssp_blob = NULL;
1702 bool use_spnego = false; /* else use raw ntlmssp */
1703 u16 blob_length = 0;
1704 bool is_binding = false;
1707 * If memory allocation is successful, caller of this function
1710 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1711 if (!ses->ntlmssp) {
1715 ses->ntlmssp->sesskey_per_smbsess = true;
1717 rc = SMB2_sess_alloc_buffer(sess_data);
1721 rc = build_ntlmssp_smb3_negotiate_blob(&ntlmssp_blob,
1722 &blob_length, ses, server,
1728 /* BB eventually need to add this */
1729 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1733 sess_data->iov[1].iov_base = ntlmssp_blob;
1734 sess_data->iov[1].iov_len = blob_length;
1736 rc = SMB2_sess_sendreceive(sess_data);
1737 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1739 /* If true, rc here is expected and not an error */
1740 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1741 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1747 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1748 le16_to_cpu(rsp->SecurityBufferOffset)) {
1749 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1750 le16_to_cpu(rsp->SecurityBufferOffset));
1754 rc = decode_ntlmssp_challenge(rsp->Buffer,
1755 le16_to_cpu(rsp->SecurityBufferLength), ses);
1759 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1761 spin_lock(&ses->ses_lock);
1762 is_binding = (ses->ses_status == SES_GOOD);
1763 spin_unlock(&ses->ses_lock);
1765 /* keep existing ses id and flags if binding */
1767 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1768 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1772 kfree_sensitive(ntlmssp_blob);
1773 SMB2_sess_free_buffer(sess_data);
1775 sess_data->result = 0;
1776 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1780 kfree_sensitive(ses->ntlmssp);
1781 ses->ntlmssp = NULL;
1782 sess_data->result = rc;
1783 sess_data->func = NULL;
1787 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1790 struct cifs_ses *ses = sess_data->ses;
1791 struct TCP_Server_Info *server = sess_data->server;
1792 struct smb2_sess_setup_req *req;
1793 struct smb2_sess_setup_rsp *rsp = NULL;
1794 unsigned char *ntlmssp_blob = NULL;
1795 bool use_spnego = false; /* else use raw ntlmssp */
1796 u16 blob_length = 0;
1797 bool is_binding = false;
1799 rc = SMB2_sess_alloc_buffer(sess_data);
1803 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1804 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1806 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length,
1810 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1815 /* BB eventually need to add this */
1816 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1820 sess_data->iov[1].iov_base = ntlmssp_blob;
1821 sess_data->iov[1].iov_len = blob_length;
1823 rc = SMB2_sess_sendreceive(sess_data);
1827 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1829 spin_lock(&ses->ses_lock);
1830 is_binding = (ses->ses_status == SES_GOOD);
1831 spin_unlock(&ses->ses_lock);
1833 /* keep existing ses id and flags if binding */
1835 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1836 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1839 rc = SMB2_sess_establish_session(sess_data);
1840 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1841 if (ses->server->dialect < SMB30_PROT_ID) {
1842 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1844 * The session id is opaque in terms of endianness, so we can't
1845 * print it as a long long. we dump it as we got it on the wire
1847 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1849 cifs_dbg(VFS, "Session Key %*ph\n",
1850 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1851 cifs_dbg(VFS, "Signing Key %*ph\n",
1852 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1856 kfree_sensitive(ntlmssp_blob);
1857 SMB2_sess_free_buffer(sess_data);
1858 kfree_sensitive(ses->ntlmssp);
1859 ses->ntlmssp = NULL;
1860 sess_data->result = rc;
1861 sess_data->func = NULL;
1865 SMB2_select_sec(struct SMB2_sess_data *sess_data)
1868 struct cifs_ses *ses = sess_data->ses;
1869 struct TCP_Server_Info *server = sess_data->server;
1871 type = smb2_select_sectype(server, ses->sectype);
1872 cifs_dbg(FYI, "sess setup type %d\n", type);
1873 if (type == Unspecified) {
1874 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1880 sess_data->func = SMB2_auth_kerberos;
1883 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1886 cifs_dbg(VFS, "secType %d not supported!\n", type);
1894 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1895 struct TCP_Server_Info *server,
1896 const struct nls_table *nls_cp)
1899 struct SMB2_sess_data *sess_data;
1901 cifs_dbg(FYI, "Session Setup\n");
1904 WARN(1, "%s: server is NULL!\n", __func__);
1908 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1912 sess_data->xid = xid;
1913 sess_data->ses = ses;
1914 sess_data->server = server;
1915 sess_data->buf0_type = CIFS_NO_BUFFER;
1916 sess_data->nls_cp = (struct nls_table *) nls_cp;
1917 sess_data->previous_session = ses->Suid;
1919 rc = SMB2_select_sec(sess_data);
1924 * Initialize the session hash with the server one.
1926 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
1927 SMB2_PREAUTH_HASH_SIZE);
1929 while (sess_data->func)
1930 sess_data->func(sess_data);
1932 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1933 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1934 rc = sess_data->result;
1936 kfree_sensitive(sess_data);
1941 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1943 struct smb_rqst rqst;
1944 struct smb2_logoff_req *req; /* response is also trivial struct */
1946 struct TCP_Server_Info *server;
1948 unsigned int total_len;
1950 struct kvec rsp_iov;
1953 cifs_dbg(FYI, "disconnect session %p\n", ses);
1955 if (ses && (ses->server))
1956 server = ses->server;
1960 /* no need to send SMB logoff if uid already closed due to reconnect */
1961 spin_lock(&ses->chan_lock);
1962 if (CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
1963 spin_unlock(&ses->chan_lock);
1964 goto smb2_session_already_dead;
1966 spin_unlock(&ses->chan_lock);
1968 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
1969 (void **) &req, &total_len);
1973 /* since no tcon, smb2_init can not do this, so do here */
1974 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1976 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1977 flags |= CIFS_TRANSFORM_REQ;
1978 else if (server->sign)
1979 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1981 flags |= CIFS_NO_RSP_BUF;
1983 iov[0].iov_base = (char *)req;
1984 iov[0].iov_len = total_len;
1986 memset(&rqst, 0, sizeof(struct smb_rqst));
1990 rc = cifs_send_recv(xid, ses, ses->server,
1991 &rqst, &resp_buf_type, flags, &rsp_iov);
1992 cifs_small_buf_release(req);
1994 * No tcon so can't do
1995 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1998 smb2_session_already_dead:
2002 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
2004 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
2007 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
2009 /* These are similar values to what Windows uses */
2010 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
2012 tcon->max_chunks = 256;
2013 tcon->max_bytes_chunk = 1048576;
2014 tcon->max_bytes_copy = 16777216;
2018 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
2019 struct cifs_tcon *tcon, const struct nls_table *cp)
2021 struct smb_rqst rqst;
2022 struct smb2_tree_connect_req *req;
2023 struct smb2_tree_connect_rsp *rsp = NULL;
2025 struct kvec rsp_iov = { NULL, 0 };
2029 __le16 *unc_path = NULL;
2031 unsigned int total_len;
2032 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2034 cifs_dbg(FYI, "TCON\n");
2036 if (!server || !tree)
2039 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
2040 if (unc_path == NULL)
2043 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp);
2044 if (unc_path_len <= 0) {
2050 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
2052 atomic_set(&tcon->num_remote_opens, 0);
2053 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
2054 (void **) &req, &total_len);
2060 if (smb3_encryption_required(tcon))
2061 flags |= CIFS_TRANSFORM_REQ;
2063 iov[0].iov_base = (char *)req;
2065 iov[0].iov_len = total_len - 1;
2067 /* Testing shows that buffer offset must be at location of Buffer[0] */
2068 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req));
2069 req->PathLength = cpu_to_le16(unc_path_len);
2070 iov[1].iov_base = unc_path;
2071 iov[1].iov_len = unc_path_len;
2074 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
2075 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
2076 * (Samba servers don't always set the flag so also check if null user)
2078 if ((server->dialect == SMB311_PROT_ID) &&
2079 !smb3_encryption_required(tcon) &&
2080 !(ses->session_flags &
2081 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
2082 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
2083 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
2085 memset(&rqst, 0, sizeof(struct smb_rqst));
2089 /* Need 64 for max size write so ask for more in case not there yet */
2090 if (server->credits >= server->max_credits)
2091 req->hdr.CreditRequest = cpu_to_le16(0);
2093 req->hdr.CreditRequest = cpu_to_le16(
2094 min_t(int, server->max_credits -
2095 server->credits, 64));
2097 rc = cifs_send_recv(xid, ses, server,
2098 &rqst, &resp_buftype, flags, &rsp_iov);
2099 cifs_small_buf_release(req);
2100 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
2101 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
2102 if ((rc != 0) || (rsp == NULL)) {
2103 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
2104 tcon->need_reconnect = true;
2105 goto tcon_error_exit;
2108 switch (rsp->ShareType) {
2109 case SMB2_SHARE_TYPE_DISK:
2110 cifs_dbg(FYI, "connection to disk share\n");
2112 case SMB2_SHARE_TYPE_PIPE:
2114 cifs_dbg(FYI, "connection to pipe share\n");
2116 case SMB2_SHARE_TYPE_PRINT:
2118 cifs_dbg(FYI, "connection to printer\n");
2121 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
2123 goto tcon_error_exit;
2126 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
2127 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
2128 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
2129 tcon->tid = le32_to_cpu(rsp->hdr.Id.SyncId.TreeId);
2130 strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name));
2132 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
2133 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
2134 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
2137 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
2138 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
2140 init_copy_chunk_defaults(tcon);
2141 if (server->ops->validate_negotiate)
2142 rc = server->ops->validate_negotiate(xid, tcon);
2143 if (rc == 0) /* See MS-SMB2 2.2.10 and 3.2.5.5 */
2144 if (tcon->share_flags & SMB2_SHAREFLAG_ISOLATED_TRANSPORT)
2145 server->nosharesock = true;
2148 free_rsp_buf(resp_buftype, rsp);
2153 if (rsp && rsp->hdr.Status == STATUS_BAD_NETWORK_NAME)
2154 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
2159 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
2161 struct smb_rqst rqst;
2162 struct smb2_tree_disconnect_req *req; /* response is trivial */
2164 struct cifs_ses *ses = tcon->ses;
2165 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2167 unsigned int total_len;
2169 struct kvec rsp_iov;
2172 cifs_dbg(FYI, "Tree Disconnect\n");
2174 if (!ses || !(ses->server))
2177 trace_smb3_tdis_enter(xid, tcon->tid, ses->Suid, tcon->tree_name);
2178 spin_lock(&ses->chan_lock);
2179 if ((tcon->need_reconnect) ||
2180 (CIFS_ALL_CHANS_NEED_RECONNECT(tcon->ses))) {
2181 spin_unlock(&ses->chan_lock);
2184 spin_unlock(&ses->chan_lock);
2186 invalidate_all_cached_dirs(tcon);
2188 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, server,
2194 if (smb3_encryption_required(tcon))
2195 flags |= CIFS_TRANSFORM_REQ;
2197 flags |= CIFS_NO_RSP_BUF;
2199 iov[0].iov_base = (char *)req;
2200 iov[0].iov_len = total_len;
2202 memset(&rqst, 0, sizeof(struct smb_rqst));
2206 rc = cifs_send_recv(xid, ses, server,
2207 &rqst, &resp_buf_type, flags, &rsp_iov);
2208 cifs_small_buf_release(req);
2210 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
2211 trace_smb3_tdis_err(xid, tcon->tid, ses->Suid, rc);
2213 trace_smb3_tdis_done(xid, tcon->tid, ses->Suid);
2219 static struct create_durable *
2220 create_durable_buf(void)
2222 struct create_durable *buf;
2224 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2228 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2229 (struct create_durable, Data));
2230 buf->ccontext.DataLength = cpu_to_le32(16);
2231 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2232 (struct create_durable, Name));
2233 buf->ccontext.NameLength = cpu_to_le16(4);
2234 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
2242 static struct create_durable *
2243 create_reconnect_durable_buf(struct cifs_fid *fid)
2245 struct create_durable *buf;
2247 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2251 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2252 (struct create_durable, Data));
2253 buf->ccontext.DataLength = cpu_to_le32(16);
2254 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2255 (struct create_durable, Name));
2256 buf->ccontext.NameLength = cpu_to_le16(4);
2257 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
2258 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
2259 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
2268 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
2270 struct create_disk_id_rsp *pdisk_id = (struct create_disk_id_rsp *)cc;
2272 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
2273 pdisk_id->DiskFileId, pdisk_id->VolumeId);
2274 buf->IndexNumber = pdisk_id->DiskFileId;
2278 parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
2279 struct create_posix_rsp *posix)
2282 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
2283 u8 *end = beg + le32_to_cpu(cc->DataLength);
2286 memset(posix, 0, sizeof(*posix));
2288 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
2289 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
2290 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
2293 sid_len = posix_info_sid_size(sid, end);
2295 cifs_dbg(VFS, "bad owner sid in posix create response\n");
2298 memcpy(&posix->owner, sid, sid_len);
2300 sid = sid + sid_len;
2301 sid_len = posix_info_sid_size(sid, end);
2303 cifs_dbg(VFS, "bad group sid in posix create response\n");
2306 memcpy(&posix->group, sid, sid_len);
2308 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
2309 posix->nlink, posix->mode, posix->reparse_tag);
2312 int smb2_parse_contexts(struct TCP_Server_Info *server,
2313 struct kvec *rsp_iov,
2314 unsigned int *epoch,
2315 char *lease_key, __u8 *oplock,
2316 struct smb2_file_all_info *buf,
2317 struct create_posix_rsp *posix)
2319 struct smb2_create_rsp *rsp = rsp_iov->iov_base;
2320 struct create_context *cc;
2321 size_t rem, off, len;
2325 static const char smb3_create_tag_posix[] = {
2326 0x93, 0xAD, 0x25, 0x50, 0x9C,
2327 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
2328 0xDE, 0x96, 0x8B, 0xCD, 0x7C
2333 off = le32_to_cpu(rsp->CreateContextsOffset);
2334 rem = le32_to_cpu(rsp->CreateContextsLength);
2335 if (check_add_overflow(off, rem, &len) || len > rsp_iov->iov_len)
2337 cc = (struct create_context *)((u8 *)rsp + off);
2339 /* Initialize inode number to 0 in case no valid data in qfid context */
2341 buf->IndexNumber = 0;
2343 while (rem >= sizeof(*cc)) {
2344 doff = le16_to_cpu(cc->DataOffset);
2345 dlen = le32_to_cpu(cc->DataLength);
2346 if (check_add_overflow(doff, dlen, &len) || len > rem)
2349 noff = le16_to_cpu(cc->NameOffset);
2350 nlen = le16_to_cpu(cc->NameLength);
2351 if (noff + nlen > doff)
2354 name = (char *)cc + noff;
2357 if (!strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4)) {
2358 *oplock = server->ops->parse_lease_buf(cc, epoch,
2361 !strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4)) {
2362 parse_query_id_ctxt(cc, buf);
2366 if (posix && !memcmp(name, smb3_create_tag_posix, 16))
2367 parse_posix_ctxt(cc, buf, posix);
2370 cifs_dbg(FYI, "%s: unhandled context (nlen=%zu dlen=%zu)\n",
2371 __func__, nlen, dlen);
2372 if (IS_ENABLED(CONFIG_CIFS_DEBUG2))
2373 cifs_dump_mem("context data: ", cc, dlen);
2377 off = le32_to_cpu(cc->Next);
2380 if (check_sub_overflow(rem, off, &rem))
2382 cc = (struct create_context *)((u8 *)cc + off);
2385 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2386 *oplock = rsp->OplockLevel;
2392 add_lease_context(struct TCP_Server_Info *server,
2393 struct smb2_create_req *req,
2395 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
2397 unsigned int num = *num_iovec;
2399 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
2400 if (iov[num].iov_base == NULL)
2402 iov[num].iov_len = server->vals->create_lease_size;
2403 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2404 *num_iovec = num + 1;
2408 static struct create_durable_v2 *
2409 create_durable_v2_buf(struct cifs_open_parms *oparms)
2411 struct cifs_fid *pfid = oparms->fid;
2412 struct create_durable_v2 *buf;
2414 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2418 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2419 (struct create_durable_v2, dcontext));
2420 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2421 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2422 (struct create_durable_v2, Name));
2423 buf->ccontext.NameLength = cpu_to_le16(4);
2426 * NB: Handle timeout defaults to 0, which allows server to choose
2427 * (most servers default to 120 seconds) and most clients default to 0.
2428 * This can be overridden at mount ("handletimeout=") if the user wants
2429 * a different persistent (or resilient) handle timeout for all opens
2430 * on a particular SMB3 mount.
2432 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2433 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2435 /* for replay, we should not overwrite the existing create guid */
2436 if (!oparms->replay) {
2437 generate_random_uuid(buf->dcontext.CreateGuid);
2438 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2440 memcpy(buf->dcontext.CreateGuid, pfid->create_guid, 16);
2442 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2450 static struct create_durable_handle_reconnect_v2 *
2451 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2453 struct create_durable_handle_reconnect_v2 *buf;
2455 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2460 buf->ccontext.DataOffset =
2461 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2463 buf->ccontext.DataLength =
2464 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2465 buf->ccontext.NameOffset =
2466 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2468 buf->ccontext.NameLength = cpu_to_le16(4);
2470 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2471 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2472 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2473 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2475 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2484 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2485 struct cifs_open_parms *oparms)
2487 unsigned int num = *num_iovec;
2489 iov[num].iov_base = create_durable_v2_buf(oparms);
2490 if (iov[num].iov_base == NULL)
2492 iov[num].iov_len = sizeof(struct create_durable_v2);
2493 *num_iovec = num + 1;
2498 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2499 struct cifs_open_parms *oparms)
2501 unsigned int num = *num_iovec;
2503 /* indicate that we don't need to relock the file */
2504 oparms->reconnect = false;
2506 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2507 if (iov[num].iov_base == NULL)
2509 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2510 *num_iovec = num + 1;
2515 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2516 struct cifs_open_parms *oparms, bool use_persistent)
2518 unsigned int num = *num_iovec;
2520 if (use_persistent) {
2521 if (oparms->reconnect)
2522 return add_durable_reconnect_v2_context(iov, num_iovec,
2525 return add_durable_v2_context(iov, num_iovec, oparms);
2528 if (oparms->reconnect) {
2529 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2530 /* indicate that we don't need to relock the file */
2531 oparms->reconnect = false;
2533 iov[num].iov_base = create_durable_buf();
2534 if (iov[num].iov_base == NULL)
2536 iov[num].iov_len = sizeof(struct create_durable);
2537 *num_iovec = num + 1;
2541 /* See MS-SMB2 2.2.13.2.7 */
2542 static struct crt_twarp_ctxt *
2543 create_twarp_buf(__u64 timewarp)
2545 struct crt_twarp_ctxt *buf;
2547 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2551 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2552 (struct crt_twarp_ctxt, Timestamp));
2553 buf->ccontext.DataLength = cpu_to_le32(8);
2554 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2555 (struct crt_twarp_ctxt, Name));
2556 buf->ccontext.NameLength = cpu_to_le16(4);
2557 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2562 buf->Timestamp = cpu_to_le64(timewarp);
2566 /* See MS-SMB2 2.2.13.2.7 */
2568 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2570 unsigned int num = *num_iovec;
2572 iov[num].iov_base = create_twarp_buf(timewarp);
2573 if (iov[num].iov_base == NULL)
2575 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2576 *num_iovec = num + 1;
2580 /* See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
2581 static void setup_owner_group_sids(char *buf)
2583 struct owner_group_sids *sids = (struct owner_group_sids *)buf;
2585 /* Populate the user ownership fields S-1-5-88-1 */
2586 sids->owner.Revision = 1;
2587 sids->owner.NumAuth = 3;
2588 sids->owner.Authority[5] = 5;
2589 sids->owner.SubAuthorities[0] = cpu_to_le32(88);
2590 sids->owner.SubAuthorities[1] = cpu_to_le32(1);
2591 sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val);
2593 /* Populate the group ownership fields S-1-5-88-2 */
2594 sids->group.Revision = 1;
2595 sids->group.NumAuth = 3;
2596 sids->group.Authority[5] = 5;
2597 sids->group.SubAuthorities[0] = cpu_to_le32(88);
2598 sids->group.SubAuthorities[1] = cpu_to_le32(2);
2599 sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val);
2601 cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val);
2604 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2605 static struct crt_sd_ctxt *
2606 create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
2608 struct crt_sd_ctxt *buf;
2610 unsigned int acelen, acl_size, ace_count;
2611 unsigned int owner_offset = 0;
2612 unsigned int group_offset = 0;
2613 struct smb3_acl acl = {};
2615 *len = round_up(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 4), 8);
2618 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
2619 *len += sizeof(struct owner_group_sids);
2622 buf = kzalloc(*len, GFP_KERNEL);
2626 ptr = (__u8 *)&buf[1];
2628 /* offset fields are from beginning of security descriptor not of create context */
2629 owner_offset = ptr - (__u8 *)&buf->sd;
2630 buf->sd.OffsetOwner = cpu_to_le32(owner_offset);
2631 group_offset = owner_offset + offsetof(struct owner_group_sids, group);
2632 buf->sd.OffsetGroup = cpu_to_le32(group_offset);
2634 setup_owner_group_sids(ptr);
2635 ptr += sizeof(struct owner_group_sids);
2637 buf->sd.OffsetOwner = 0;
2638 buf->sd.OffsetGroup = 0;
2641 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, sd));
2642 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name));
2643 buf->ccontext.NameLength = cpu_to_le16(4);
2644 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2649 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2652 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2653 * and "DP" ie the DACL is present
2655 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2657 /* offset owner, group and Sbz1 and SACL are all zero */
2658 buf->sd.OffsetDacl = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2659 /* Ship the ACL for now. we will copy it into buf later. */
2661 ptr += sizeof(struct smb3_acl);
2663 /* create one ACE to hold the mode embedded in reserved special SID */
2664 acelen = setup_special_mode_ACE((struct cifs_ace *)ptr, (__u64)mode);
2666 acl_size = acelen + sizeof(struct smb3_acl);
2670 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
2671 acelen = setup_special_user_owner_ACE((struct cifs_ace *)ptr);
2677 /* and one more ACE to allow access for authenticated users */
2678 acelen = setup_authusers_ACE((struct cifs_ace *)ptr);
2683 acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2684 acl.AclSize = cpu_to_le16(acl_size);
2685 acl.AceCount = cpu_to_le16(ace_count);
2686 /* acl.Sbz1 and Sbz2 MBZ so are not set here, but initialized above */
2687 memcpy(aclptr, &acl, sizeof(struct smb3_acl));
2689 buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2690 *len = round_up((unsigned int)(ptr - (__u8 *)buf), 8);
2696 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner)
2698 unsigned int num = *num_iovec;
2699 unsigned int len = 0;
2701 iov[num].iov_base = create_sd_buf(mode, set_owner, &len);
2702 if (iov[num].iov_base == NULL)
2704 iov[num].iov_len = len;
2705 *num_iovec = num + 1;
2709 static struct crt_query_id_ctxt *
2710 create_query_id_buf(void)
2712 struct crt_query_id_ctxt *buf;
2714 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2718 buf->ccontext.DataOffset = cpu_to_le16(0);
2719 buf->ccontext.DataLength = cpu_to_le32(0);
2720 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2721 (struct crt_query_id_ctxt, Name));
2722 buf->ccontext.NameLength = cpu_to_le16(4);
2723 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2731 /* See MS-SMB2 2.2.13.2.9 */
2733 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2735 unsigned int num = *num_iovec;
2737 iov[num].iov_base = create_query_id_buf();
2738 if (iov[num].iov_base == NULL)
2740 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2741 *num_iovec = num + 1;
2745 static void add_ea_context(struct cifs_open_parms *oparms,
2746 struct kvec *rq_iov, unsigned int *num_iovs)
2748 struct kvec *iov = oparms->ea_cctx;
2750 if (iov && iov->iov_base && iov->iov_len) {
2751 rq_iov[(*num_iovs)++] = *iov;
2752 memset(iov, 0, sizeof(*iov));
2757 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2758 const char *treename, const __le16 *path)
2760 int treename_len, path_len;
2761 struct nls_table *cp;
2762 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2767 treename_len = strlen(treename);
2768 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2774 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2776 /* make room for one path separator only if @path isn't empty */
2777 *out_len = treename_len + (path[0] ? 1 : 0) + path_len;
2780 * final path needs to be 8-byte aligned as specified in
2781 * MS-SMB2 2.2.13 SMB2 CREATE Request.
2783 *out_size = round_up(*out_len * sizeof(__le16), 8);
2784 *out_path = kzalloc(*out_size + sizeof(__le16) /* null */, GFP_KERNEL);
2788 cp = load_nls_default();
2789 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2791 /* Do not append the separator if the path is empty */
2792 if (path[0] != cpu_to_le16(0x0000)) {
2793 UniStrcat((wchar_t *)*out_path, (wchar_t *)sep);
2794 UniStrcat((wchar_t *)*out_path, (wchar_t *)path);
2802 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2803 umode_t mode, struct cifs_tcon *tcon,
2804 const char *full_path,
2805 struct cifs_sb_info *cifs_sb)
2807 struct smb_rqst rqst;
2808 struct smb2_create_req *req;
2809 struct smb2_create_rsp *rsp = NULL;
2810 struct cifs_ses *ses = tcon->ses;
2811 struct kvec iov[3]; /* make sure at least one for each open context */
2812 struct kvec rsp_iov = {NULL, 0};
2815 __le16 *copy_path = NULL;
2818 unsigned int n_iov = 2;
2819 __u32 file_attributes = 0;
2820 char *pc_buf = NULL;
2822 unsigned int total_len;
2823 __le16 *utf16_path = NULL;
2824 struct TCP_Server_Info *server;
2825 int retries = 0, cur_sleep = 1;
2828 /* reinitialize for possible replay */
2831 server = cifs_pick_channel(ses);
2833 cifs_dbg(FYI, "mkdir\n");
2835 /* resource #1: path allocation */
2836 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2840 if (!ses || !server) {
2845 /* resource #2: request */
2846 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2847 (void **) &req, &total_len);
2852 if (smb3_encryption_required(tcon))
2853 flags |= CIFS_TRANSFORM_REQ;
2855 req->ImpersonationLevel = IL_IMPERSONATION;
2856 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2857 /* File attributes ignored on open (used in create though) */
2858 req->FileAttributes = cpu_to_le32(file_attributes);
2859 req->ShareAccess = FILE_SHARE_ALL_LE;
2860 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2861 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2863 iov[0].iov_base = (char *)req;
2864 /* -1 since last byte is buf[0] which is sent below (path) */
2865 iov[0].iov_len = total_len - 1;
2867 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2869 /* [MS-SMB2] 2.2.13 NameOffset:
2870 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2871 * the SMB2 header, the file name includes a prefix that will
2872 * be processed during DFS name normalization as specified in
2873 * section 3.3.5.9. Otherwise, the file name is relative to
2874 * the share that is identified by the TreeId in the SMB2
2877 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2880 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2881 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2883 tcon->tree_name, utf16_path);
2887 req->NameLength = cpu_to_le16(name_len * 2);
2888 uni_path_len = copy_size;
2889 /* free before overwriting resource */
2891 utf16_path = copy_path;
2893 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2894 /* MUST set path len (NameLength) to 0 opening root of share */
2895 req->NameLength = cpu_to_le16(uni_path_len - 2);
2896 if (uni_path_len % 8 != 0) {
2897 copy_size = roundup(uni_path_len, 8);
2898 copy_path = kzalloc(copy_size, GFP_KERNEL);
2903 memcpy((char *)copy_path, (const char *)utf16_path,
2905 uni_path_len = copy_size;
2906 /* free before overwriting resource */
2908 utf16_path = copy_path;
2912 iov[1].iov_len = uni_path_len;
2913 iov[1].iov_base = utf16_path;
2914 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2916 if (tcon->posix_extensions) {
2917 /* resource #3: posix buf */
2918 rc = add_posix_context(iov, &n_iov, mode);
2921 req->CreateContextsOffset = cpu_to_le32(
2922 sizeof(struct smb2_create_req) +
2924 pc_buf = iov[n_iov-1].iov_base;
2928 memset(&rqst, 0, sizeof(struct smb_rqst));
2930 rqst.rq_nvec = n_iov;
2932 /* no need to inc num_remote_opens because we close it just below */
2933 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, full_path, CREATE_NOT_FILE,
2934 FILE_WRITE_ATTRIBUTES);
2937 smb2_set_replay(server, &rqst);
2939 /* resource #4: response buffer */
2940 rc = cifs_send_recv(xid, ses, server,
2941 &rqst, &resp_buftype, flags, &rsp_iov);
2943 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2944 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2946 FILE_WRITE_ATTRIBUTES, rc);
2947 goto err_free_rsp_buf;
2951 * Although unlikely to be possible for rsp to be null and rc not set,
2952 * adding check below is slightly safer long term (and quiets Coverity
2955 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2962 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
2963 CREATE_NOT_FILE, FILE_WRITE_ATTRIBUTES);
2965 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2967 /* Eventually save off posix specific response info and timestaps */
2970 free_rsp_buf(resp_buftype, rsp);
2973 cifs_small_buf_release(req);
2977 if (is_replayable_error(rc) &&
2978 smb2_should_replay(tcon, &retries, &cur_sleep))
2985 SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2986 struct smb_rqst *rqst, __u8 *oplock,
2987 struct cifs_open_parms *oparms, __le16 *path)
2989 struct smb2_create_req *req;
2990 unsigned int n_iov = 2;
2991 __u32 file_attributes = 0;
2994 unsigned int total_len;
2995 struct kvec *iov = rqst->rq_iov;
2999 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
3000 (void **) &req, &total_len);
3004 iov[0].iov_base = (char *)req;
3005 /* -1 since last byte is buf[0] which is sent below (path) */
3006 iov[0].iov_len = total_len - 1;
3008 if (oparms->create_options & CREATE_OPTION_READONLY)
3009 file_attributes |= ATTR_READONLY;
3010 if (oparms->create_options & CREATE_OPTION_SPECIAL)
3011 file_attributes |= ATTR_SYSTEM;
3013 req->ImpersonationLevel = IL_IMPERSONATION;
3014 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
3015 /* File attributes ignored on open (used in create though) */
3016 req->FileAttributes = cpu_to_le32(file_attributes);
3017 req->ShareAccess = FILE_SHARE_ALL_LE;
3019 req->CreateDisposition = cpu_to_le32(oparms->disposition);
3020 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
3021 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
3023 /* [MS-SMB2] 2.2.13 NameOffset:
3024 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
3025 * the SMB2 header, the file name includes a prefix that will
3026 * be processed during DFS name normalization as specified in
3027 * section 3.3.5.9. Otherwise, the file name is relative to
3028 * the share that is identified by the TreeId in the SMB2
3031 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
3034 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
3035 rc = alloc_path_with_tree_prefix(©_path, ©_size,
3037 tcon->tree_name, path);
3040 req->NameLength = cpu_to_le16(name_len * 2);
3041 uni_path_len = copy_size;
3044 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
3045 /* MUST set path len (NameLength) to 0 opening root of share */
3046 req->NameLength = cpu_to_le16(uni_path_len - 2);
3047 copy_size = round_up(uni_path_len, 8);
3048 copy_path = kzalloc(copy_size, GFP_KERNEL);
3051 memcpy((char *)copy_path, (const char *)path,
3053 uni_path_len = copy_size;
3057 iov[1].iov_len = uni_path_len;
3058 iov[1].iov_base = path;
3060 if ((!server->oplocks) || (tcon->no_lease))
3061 *oplock = SMB2_OPLOCK_LEVEL_NONE;
3063 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
3064 *oplock == SMB2_OPLOCK_LEVEL_NONE)
3065 req->RequestedOplockLevel = *oplock;
3066 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
3067 (oparms->create_options & CREATE_NOT_FILE))
3068 req->RequestedOplockLevel = *oplock; /* no srv lease support */
3070 rc = add_lease_context(server, req, iov, &n_iov,
3071 oparms->fid->lease_key, oplock);
3076 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3077 rc = add_durable_context(iov, &n_iov, oparms,
3078 tcon->use_persistent);
3083 if (tcon->posix_extensions) {
3084 rc = add_posix_context(iov, &n_iov, oparms->mode);
3089 if (tcon->snapshot_time) {
3090 cifs_dbg(FYI, "adding snapshot context\n");
3091 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
3096 if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) {
3100 if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
3101 (oparms->mode != ACL_NO_MODE))
3105 oparms->mode = ACL_NO_MODE;
3108 if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
3113 if (set_owner | set_mode) {
3114 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
3115 rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner);
3121 add_query_id_context(iov, &n_iov);
3122 add_ea_context(oparms, iov, &n_iov);
3126 * We have create contexts behind iov[1] (the file
3127 * name), point at them from the main create request
3129 req->CreateContextsOffset = cpu_to_le32(
3130 sizeof(struct smb2_create_req) +
3132 req->CreateContextsLength = 0;
3134 for (unsigned int i = 2; i < (n_iov-1); i++) {
3135 struct kvec *v = &iov[i];
3136 size_t len = v->iov_len;
3137 struct create_context *cctx =
3138 (struct create_context *)v->iov_base;
3140 cctx->Next = cpu_to_le32(len);
3141 le32_add_cpu(&req->CreateContextsLength, len);
3143 le32_add_cpu(&req->CreateContextsLength,
3144 iov[n_iov-1].iov_len);
3147 rqst->rq_nvec = n_iov;
3151 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
3152 * All other vectors are freed by kfree().
3155 SMB2_open_free(struct smb_rqst *rqst)
3159 if (rqst && rqst->rq_iov) {
3160 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
3161 for (i = 1; i < rqst->rq_nvec; i++)
3162 if (rqst->rq_iov[i].iov_base != smb2_padding)
3163 kfree(rqst->rq_iov[i].iov_base);
3168 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
3169 __u8 *oplock, struct smb2_file_all_info *buf,
3170 struct create_posix_rsp *posix,
3171 struct kvec *err_iov, int *buftype)
3173 struct smb_rqst rqst;
3174 struct smb2_create_rsp *rsp = NULL;
3175 struct cifs_tcon *tcon = oparms->tcon;
3176 struct cifs_ses *ses = tcon->ses;
3177 struct TCP_Server_Info *server;
3178 struct kvec iov[SMB2_CREATE_IOV_SIZE];
3179 struct kvec rsp_iov = {NULL, 0};
3180 int resp_buftype = CIFS_NO_BUFFER;
3183 int retries = 0, cur_sleep = 1;
3186 /* reinitialize for possible replay */
3188 server = cifs_pick_channel(ses);
3189 oparms->replay = !!(retries);
3191 cifs_dbg(FYI, "create/open\n");
3192 if (!ses || !server)
3195 if (smb3_encryption_required(tcon))
3196 flags |= CIFS_TRANSFORM_REQ;
3198 memset(&rqst, 0, sizeof(struct smb_rqst));
3199 memset(&iov, 0, sizeof(iov));
3201 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
3203 rc = SMB2_open_init(tcon, server,
3204 &rqst, oplock, oparms, path);
3208 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid, oparms->path,
3209 oparms->create_options, oparms->desired_access);
3212 smb2_set_replay(server, &rqst);
3214 rc = cifs_send_recv(xid, ses, server,
3215 &rqst, &resp_buftype, flags,
3217 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
3220 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
3221 if (err_iov && rsp) {
3223 *buftype = resp_buftype;
3224 resp_buftype = CIFS_NO_BUFFER;
3227 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
3228 oparms->create_options, oparms->desired_access, rc);
3229 if (rc == -EREMCHG) {
3230 pr_warn_once("server share %s deleted\n",
3232 tcon->need_reconnect = true;
3235 } else if (rsp == NULL) /* unlikely to happen, but safer to check */
3238 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
3239 oparms->create_options, oparms->desired_access);
3241 atomic_inc(&tcon->num_remote_opens);
3242 oparms->fid->persistent_fid = rsp->PersistentFileId;
3243 oparms->fid->volatile_fid = rsp->VolatileFileId;
3244 oparms->fid->access = oparms->desired_access;
3245 #ifdef CONFIG_CIFS_DEBUG2
3246 oparms->fid->mid = le64_to_cpu(rsp->hdr.MessageId);
3247 #endif /* CIFS_DEBUG2 */
3250 buf->CreationTime = rsp->CreationTime;
3251 buf->LastAccessTime = rsp->LastAccessTime;
3252 buf->LastWriteTime = rsp->LastWriteTime;
3253 buf->ChangeTime = rsp->ChangeTime;
3254 buf->AllocationSize = rsp->AllocationSize;
3255 buf->EndOfFile = rsp->EndofFile;
3256 buf->Attributes = rsp->FileAttributes;
3257 buf->NumberOfLinks = cpu_to_le32(1);
3258 buf->DeletePending = 0;
3262 rc = smb2_parse_contexts(server, &rsp_iov, &oparms->fid->epoch,
3263 oparms->fid->lease_key, oplock, buf, posix);
3265 SMB2_open_free(&rqst);
3266 free_rsp_buf(resp_buftype, rsp);
3268 if (is_replayable_error(rc) &&
3269 smb2_should_replay(tcon, &retries, &cur_sleep))
3276 SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3277 struct smb_rqst *rqst,
3278 u64 persistent_fid, u64 volatile_fid, u32 opcode,
3279 char *in_data, u32 indatalen,
3280 __u32 max_response_size)
3282 struct smb2_ioctl_req *req;
3283 struct kvec *iov = rqst->rq_iov;
3284 unsigned int total_len;
3288 rc = smb2_ioctl_req_init(opcode, tcon, server,
3289 (void **) &req, &total_len);
3295 * indatalen is usually small at a couple of bytes max, so
3296 * just allocate through generic pool
3298 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
3300 cifs_small_buf_release(req);
3305 req->CtlCode = cpu_to_le32(opcode);
3306 req->PersistentFileId = persistent_fid;
3307 req->VolatileFileId = volatile_fid;
3309 iov[0].iov_base = (char *)req;
3311 * If no input data, the size of ioctl struct in
3312 * protocol spec still includes a 1 byte data buffer,
3313 * but if input data passed to ioctl, we do not
3314 * want to double count this, so we do not send
3315 * the dummy one byte of data in iovec[0] if sending
3316 * input data (in iovec[1]).
3319 req->InputCount = cpu_to_le32(indatalen);
3320 /* do not set InputOffset if no input data */
3322 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
3324 iov[0].iov_len = total_len - 1;
3325 iov[1].iov_base = in_data_buf;
3326 iov[1].iov_len = indatalen;
3329 iov[0].iov_len = total_len;
3332 req->OutputOffset = 0;
3333 req->OutputCount = 0; /* MBZ */
3336 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
3337 * We Could increase default MaxOutputResponse, but that could require
3338 * more credits. Windows typically sets this smaller, but for some
3339 * ioctls it may be useful to allow server to send more. No point
3340 * limiting what the server can send as long as fits in one credit
3341 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
3342 * to increase this limit up in the future.
3343 * Note that for snapshot queries that servers like Azure expect that
3344 * the first query be minimal size (and just used to get the number/size
3345 * of previous versions) so response size must be specified as EXACTLY
3346 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
3347 * of eight bytes. Currently that is the only case where we set max
3348 * response size smaller.
3350 req->MaxOutputResponse = cpu_to_le32(max_response_size);
3351 req->hdr.CreditCharge =
3352 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
3353 SMB2_MAX_BUFFER_SIZE));
3354 /* always an FSCTL (for now) */
3355 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
3357 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
3358 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
3359 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
3365 SMB2_ioctl_free(struct smb_rqst *rqst)
3369 if (rqst && rqst->rq_iov) {
3370 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3371 for (i = 1; i < rqst->rq_nvec; i++)
3372 if (rqst->rq_iov[i].iov_base != smb2_padding)
3373 kfree(rqst->rq_iov[i].iov_base);
3379 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
3382 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3383 u64 volatile_fid, u32 opcode, char *in_data, u32 indatalen,
3384 u32 max_out_data_len, char **out_data,
3385 u32 *plen /* returned data len */)
3387 struct smb_rqst rqst;
3388 struct smb2_ioctl_rsp *rsp = NULL;
3389 struct cifs_ses *ses;
3390 struct TCP_Server_Info *server;
3391 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
3392 struct kvec rsp_iov = {NULL, 0};
3393 int resp_buftype = CIFS_NO_BUFFER;
3396 int retries = 0, cur_sleep = 1;
3406 /* reinitialize for possible replay */
3408 server = cifs_pick_channel(ses);
3413 cifs_dbg(FYI, "SMB2 IOCTL\n");
3415 if (out_data != NULL)
3418 /* zero out returned data len, in case of error */
3422 if (smb3_encryption_required(tcon))
3423 flags |= CIFS_TRANSFORM_REQ;
3425 memset(&rqst, 0, sizeof(struct smb_rqst));
3426 memset(&iov, 0, sizeof(iov));
3428 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
3430 rc = SMB2_ioctl_init(tcon, server,
3431 &rqst, persistent_fid, volatile_fid, opcode,
3432 in_data, indatalen, max_out_data_len);
3437 smb2_set_replay(server, &rqst);
3439 rc = cifs_send_recv(xid, ses, server,
3440 &rqst, &resp_buftype, flags,
3442 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
3445 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3446 ses->Suid, 0, opcode, rc);
3448 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
3449 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3451 } else if (rc == -EINVAL) {
3452 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3453 (opcode != FSCTL_SRV_COPYCHUNK)) {
3454 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3457 } else if (rc == -E2BIG) {
3458 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3459 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3464 /* check if caller wants to look at return data or just return rc */
3465 if ((plen == NULL) || (out_data == NULL))
3469 * Although unlikely to be possible for rsp to be null and rc not set,
3470 * adding check below is slightly safer long term (and quiets Coverity
3478 *plen = le32_to_cpu(rsp->OutputCount);
3480 /* We check for obvious errors in the output buffer length and offset */
3482 goto ioctl_exit; /* server returned no data */
3483 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
3484 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
3490 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
3491 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
3492 le32_to_cpu(rsp->OutputOffset));
3498 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3500 if (*out_data == NULL) {
3506 SMB2_ioctl_free(&rqst);
3507 free_rsp_buf(resp_buftype, rsp);
3509 if (is_replayable_error(rc) &&
3510 smb2_should_replay(tcon, &retries, &cur_sleep))
3517 * Individual callers to ioctl worker function follow
3521 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3522 u64 persistent_fid, u64 volatile_fid)
3525 struct compress_ioctl fsctl_input;
3526 char *ret_data = NULL;
3528 fsctl_input.CompressionState =
3529 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3531 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3532 FSCTL_SET_COMPRESSION,
3533 (char *)&fsctl_input /* data input */,
3534 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3535 &ret_data /* out data */, NULL);
3537 cifs_dbg(FYI, "set compression rc %d\n", rc);
3543 SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3544 struct smb_rqst *rqst,
3545 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
3547 struct smb2_close_req *req;
3548 struct kvec *iov = rqst->rq_iov;
3549 unsigned int total_len;
3552 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
3553 (void **) &req, &total_len);
3557 req->PersistentFileId = persistent_fid;
3558 req->VolatileFileId = volatile_fid;
3560 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3563 iov[0].iov_base = (char *)req;
3564 iov[0].iov_len = total_len;
3570 SMB2_close_free(struct smb_rqst *rqst)
3572 if (rqst && rqst->rq_iov)
3573 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3577 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3578 u64 persistent_fid, u64 volatile_fid,
3579 struct smb2_file_network_open_info *pbuf)
3581 struct smb_rqst rqst;
3582 struct smb2_close_rsp *rsp = NULL;
3583 struct cifs_ses *ses = tcon->ses;
3584 struct TCP_Server_Info *server;
3586 struct kvec rsp_iov;
3587 int resp_buftype = CIFS_NO_BUFFER;
3590 bool query_attrs = false;
3591 int retries = 0, cur_sleep = 1;
3594 /* reinitialize for possible replay */
3596 query_attrs = false;
3597 server = cifs_pick_channel(ses);
3599 cifs_dbg(FYI, "Close\n");
3601 if (!ses || !server)
3604 if (smb3_encryption_required(tcon))
3605 flags |= CIFS_TRANSFORM_REQ;
3607 memset(&rqst, 0, sizeof(struct smb_rqst));
3608 memset(&iov, 0, sizeof(iov));
3612 /* check if need to ask server to return timestamps in close response */
3616 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3617 rc = SMB2_close_init(tcon, server,
3618 &rqst, persistent_fid, volatile_fid,
3624 smb2_set_replay(server, &rqst);
3626 rc = cifs_send_recv(xid, ses, server,
3627 &rqst, &resp_buftype, flags, &rsp_iov);
3628 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
3631 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
3632 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3636 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3639 memcpy(&pbuf->network_open_info,
3640 &rsp->network_open_info,
3641 sizeof(pbuf->network_open_info));
3642 atomic_dec(&tcon->num_remote_opens);
3646 SMB2_close_free(&rqst);
3647 free_rsp_buf(resp_buftype, rsp);
3649 /* retry close in a worker thread if this one is interrupted */
3650 if (is_interrupt_error(rc)) {
3653 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3656 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3657 persistent_fid, tmp_rc);
3660 if (is_replayable_error(rc) &&
3661 smb2_should_replay(tcon, &retries, &cur_sleep))
3668 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3669 u64 persistent_fid, u64 volatile_fid)
3671 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3675 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3676 struct kvec *iov, unsigned int min_buf_size)
3678 unsigned int smb_len = iov->iov_len;
3679 char *end_of_smb = smb_len + (char *)iov->iov_base;
3680 char *begin_of_buf = offset + (char *)iov->iov_base;
3681 char *end_of_buf = begin_of_buf + buffer_length;
3684 if (buffer_length < min_buf_size) {
3685 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3686 buffer_length, min_buf_size);
3690 /* check if beyond RFC1001 maximum length */
3691 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3692 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3693 buffer_length, smb_len);
3697 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3698 cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
3706 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3707 * Caller must free buffer.
3710 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3711 struct kvec *iov, unsigned int minbufsize,
3714 char *begin_of_buf = offset + (char *)iov->iov_base;
3720 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3724 memcpy(data, begin_of_buf, minbufsize);
3730 SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3731 struct smb_rqst *rqst,
3732 u64 persistent_fid, u64 volatile_fid,
3733 u8 info_class, u8 info_type, u32 additional_info,
3734 size_t output_len, size_t input_len, void *input)
3736 struct smb2_query_info_req *req;
3737 struct kvec *iov = rqst->rq_iov;
3738 unsigned int total_len;
3742 if (unlikely(check_add_overflow(input_len, sizeof(*req), &len) ||
3743 len > CIFSMaxBufSize))
3746 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
3747 (void **) &req, &total_len);
3751 req->InfoType = info_type;
3752 req->FileInfoClass = info_class;
3753 req->PersistentFileId = persistent_fid;
3754 req->VolatileFileId = volatile_fid;
3755 req->AdditionalInformation = cpu_to_le32(additional_info);
3757 req->OutputBufferLength = cpu_to_le32(output_len);
3759 req->InputBufferLength = cpu_to_le32(input_len);
3760 /* total_len for smb query request never close to le16 max */
3761 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3762 memcpy(req->Buffer, input, input_len);
3765 iov[0].iov_base = (char *)req;
3767 iov[0].iov_len = len;
3772 SMB2_query_info_free(struct smb_rqst *rqst)
3774 if (rqst && rqst->rq_iov)
3775 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
3779 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3780 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3781 u32 additional_info, size_t output_len, size_t min_len, void **data,
3784 struct smb_rqst rqst;
3785 struct smb2_query_info_rsp *rsp = NULL;
3787 struct kvec rsp_iov;
3789 int resp_buftype = CIFS_NO_BUFFER;
3790 struct cifs_ses *ses = tcon->ses;
3791 struct TCP_Server_Info *server;
3793 bool allocated = false;
3794 int retries = 0, cur_sleep = 1;
3796 cifs_dbg(FYI, "Query Info\n");
3802 /* reinitialize for possible replay */
3805 server = cifs_pick_channel(ses);
3810 if (smb3_encryption_required(tcon))
3811 flags |= CIFS_TRANSFORM_REQ;
3813 memset(&rqst, 0, sizeof(struct smb_rqst));
3814 memset(&iov, 0, sizeof(iov));
3818 rc = SMB2_query_info_init(tcon, server,
3819 &rqst, persistent_fid, volatile_fid,
3820 info_class, info_type, additional_info,
3821 output_len, 0, NULL);
3825 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3826 ses->Suid, info_class, (__u32)info_type);
3829 smb2_set_replay(server, &rqst);
3831 rc = cifs_send_recv(xid, ses, server,
3832 &rqst, &resp_buftype, flags, &rsp_iov);
3833 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3836 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3837 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3838 ses->Suid, info_class, (__u32)info_type, rc);
3842 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3843 ses->Suid, info_class, (__u32)info_type);
3846 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3848 *data = kmalloc(*dlen, GFP_KERNEL);
3851 "Error %d allocating memory for acl\n",
3861 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3862 le32_to_cpu(rsp->OutputBufferLength),
3863 &rsp_iov, dlen ? *dlen : min_len, *data);
3864 if (rc && allocated) {
3871 SMB2_query_info_free(&rqst);
3872 free_rsp_buf(resp_buftype, rsp);
3874 if (is_replayable_error(rc) &&
3875 smb2_should_replay(tcon, &retries, &cur_sleep))
3881 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3882 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3884 return query_info(xid, tcon, persistent_fid, volatile_fid,
3885 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3886 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3887 sizeof(struct smb2_file_all_info), (void **)&data,
3892 /* currently unused, as now we are doing compounding instead (see smb311_posix_query_path_info) */
3894 SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3895 u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen)
3897 size_t output_len = sizeof(struct smb311_posix_qinfo *) +
3898 (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2);
3901 return query_info(xid, tcon, persistent_fid, volatile_fid,
3902 SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0,
3903 output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen);
3904 /* Note caller must free "data" (passed in above). It may be allocated in query_info call */
3909 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3910 u64 persistent_fid, u64 volatile_fid,
3911 void **data, u32 *plen, u32 extra_info)
3913 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
3917 return query_info(xid, tcon, persistent_fid, volatile_fid,
3918 0, SMB2_O_INFO_SECURITY, additional_info,
3919 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3923 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3924 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3926 return query_info(xid, tcon, persistent_fid, volatile_fid,
3927 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3928 sizeof(struct smb2_file_internal_info),
3929 sizeof(struct smb2_file_internal_info),
3930 (void **)&uniqueid, NULL);
3934 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3935 * See MS-SMB2 2.2.35 and 2.2.36
3939 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3940 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3941 u64 persistent_fid, u64 volatile_fid,
3942 u32 completion_filter, bool watch_tree)
3944 struct smb2_change_notify_req *req;
3945 struct kvec *iov = rqst->rq_iov;
3946 unsigned int total_len;
3949 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
3950 (void **) &req, &total_len);
3954 req->PersistentFileId = persistent_fid;
3955 req->VolatileFileId = volatile_fid;
3956 /* See note 354 of MS-SMB2, 64K max */
3957 req->OutputBufferLength =
3958 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3959 req->CompletionFilter = cpu_to_le32(completion_filter);
3961 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3965 iov[0].iov_base = (char *)req;
3966 iov[0].iov_len = total_len;
3972 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3973 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3974 u32 completion_filter, u32 max_out_data_len, char **out_data,
3975 u32 *plen /* returned data len */)
3977 struct cifs_ses *ses = tcon->ses;
3978 struct TCP_Server_Info *server;
3979 struct smb_rqst rqst;
3980 struct smb2_change_notify_rsp *smb_rsp;
3982 struct kvec rsp_iov = {NULL, 0};
3983 int resp_buftype = CIFS_NO_BUFFER;
3986 int retries = 0, cur_sleep = 1;
3989 /* reinitialize for possible replay */
3991 server = cifs_pick_channel(ses);
3993 cifs_dbg(FYI, "change notify\n");
3994 if (!ses || !server)
3997 if (smb3_encryption_required(tcon))
3998 flags |= CIFS_TRANSFORM_REQ;
4000 memset(&rqst, 0, sizeof(struct smb_rqst));
4001 memset(&iov, 0, sizeof(iov));
4008 rc = SMB2_notify_init(xid, &rqst, tcon, server,
4009 persistent_fid, volatile_fid,
4010 completion_filter, watch_tree);
4014 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
4015 (u8)watch_tree, completion_filter);
4018 smb2_set_replay(server, &rqst);
4020 rc = cifs_send_recv(xid, ses, server,
4021 &rqst, &resp_buftype, flags, &rsp_iov);
4024 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
4025 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
4026 (u8)watch_tree, completion_filter, rc);
4028 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
4029 ses->Suid, (u8)watch_tree, completion_filter);
4030 /* validate that notify information is plausible */
4031 if ((rsp_iov.iov_base == NULL) ||
4032 (rsp_iov.iov_len < sizeof(struct smb2_change_notify_rsp) + 1))
4035 smb_rsp = (struct smb2_change_notify_rsp *)rsp_iov.iov_base;
4037 smb2_validate_iov(le16_to_cpu(smb_rsp->OutputBufferOffset),
4038 le32_to_cpu(smb_rsp->OutputBufferLength), &rsp_iov,
4039 sizeof(struct file_notify_information));
4041 *out_data = kmemdup((char *)smb_rsp + le16_to_cpu(smb_rsp->OutputBufferOffset),
4042 le32_to_cpu(smb_rsp->OutputBufferLength), GFP_KERNEL);
4043 if (*out_data == NULL) {
4047 *plen = le32_to_cpu(smb_rsp->OutputBufferLength);
4052 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
4053 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4055 if (is_replayable_error(rc) &&
4056 smb2_should_replay(tcon, &retries, &cur_sleep))
4065 * This is a no-op for now. We're not really interested in the reply, but
4066 * rather in the fact that the server sent one and that server->lstrp
4069 * FIXME: maybe we should consider checking that the reply matches request?
4072 smb2_echo_callback(struct mid_q_entry *mid)
4074 struct TCP_Server_Info *server = mid->callback_data;
4075 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
4076 struct cifs_credits credits = { .value = 0, .instance = 0 };
4078 if (mid->mid_state == MID_RESPONSE_RECEIVED
4079 || mid->mid_state == MID_RESPONSE_MALFORMED) {
4080 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4081 credits.instance = server->reconnect_instance;
4085 add_credits(server, &credits, CIFS_ECHO_OP);
4088 void smb2_reconnect_server(struct work_struct *work)
4090 struct TCP_Server_Info *server = container_of(work,
4091 struct TCP_Server_Info, reconnect.work);
4092 struct TCP_Server_Info *pserver;
4093 struct cifs_ses *ses, *ses2;
4094 struct cifs_tcon *tcon, *tcon2;
4095 struct list_head tmp_list, tmp_ses_list;
4096 bool ses_exist = false;
4097 bool tcon_selected = false;
4099 bool resched = false;
4101 /* first check if ref count has reached 0, if not inc ref count */
4102 spin_lock(&cifs_tcp_ses_lock);
4103 if (!server->srv_count) {
4104 spin_unlock(&cifs_tcp_ses_lock);
4107 server->srv_count++;
4108 spin_unlock(&cifs_tcp_ses_lock);
4110 /* If server is a channel, select the primary channel */
4111 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
4113 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
4114 mutex_lock(&pserver->reconnect_mutex);
4116 /* if the server is marked for termination, drop the ref count here */
4117 if (server->terminate) {
4118 cifs_put_tcp_session(server, true);
4119 mutex_unlock(&pserver->reconnect_mutex);
4123 INIT_LIST_HEAD(&tmp_list);
4124 INIT_LIST_HEAD(&tmp_ses_list);
4125 cifs_dbg(FYI, "Reconnecting tcons and channels\n");
4127 spin_lock(&cifs_tcp_ses_lock);
4128 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
4129 spin_lock(&ses->ses_lock);
4130 if (ses->ses_status == SES_EXITING) {
4131 spin_unlock(&ses->ses_lock);
4134 spin_unlock(&ses->ses_lock);
4136 tcon_selected = false;
4138 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
4139 if (tcon->need_reconnect || tcon->need_reopen_files) {
4141 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
4142 netfs_trace_tcon_ref_get_reconnect_server);
4143 list_add_tail(&tcon->rlist, &tmp_list);
4144 tcon_selected = true;
4148 * IPC has the same lifetime as its session and uses its
4151 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
4152 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
4153 tcon_selected = true;
4154 cifs_smb_ses_inc_refcount(ses);
4157 * handle the case where channel needs to reconnect
4158 * binding session, but tcon is healthy (some other channel
4161 spin_lock(&ses->chan_lock);
4162 if (!tcon_selected && cifs_chan_needs_reconnect(ses, server)) {
4163 list_add_tail(&ses->rlist, &tmp_ses_list);
4165 cifs_smb_ses_inc_refcount(ses);
4167 spin_unlock(&ses->chan_lock);
4169 spin_unlock(&cifs_tcp_ses_lock);
4171 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
4172 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server, true);
4174 cifs_reopen_persistent_handles(tcon);
4177 list_del_init(&tcon->rlist);
4179 cifs_put_smb_ses(tcon->ses);
4181 cifs_put_tcon(tcon, netfs_trace_tcon_ref_put_reconnect_server);
4187 /* allocate a dummy tcon struct used for reconnect */
4188 tcon = tcon_info_alloc(false, netfs_trace_tcon_ref_new_reconnect_server);
4191 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
4192 list_del_init(&ses->rlist);
4193 cifs_put_smb_ses(ses);
4198 tcon->status = TID_GOOD;
4199 tcon->retry = false;
4200 tcon->need_reconnect = false;
4202 /* now reconnect sessions for necessary channels */
4203 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
4205 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server, true);
4208 list_del_init(&ses->rlist);
4209 cifs_put_smb_ses(ses);
4211 tconInfoFree(tcon, netfs_trace_tcon_ref_free_reconnect_server);
4214 cifs_dbg(FYI, "Reconnecting tcons and channels finished\n");
4216 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
4217 mutex_unlock(&pserver->reconnect_mutex);
4219 /* now we can safely release srv struct */
4220 cifs_put_tcp_session(server, true);
4224 SMB2_echo(struct TCP_Server_Info *server)
4226 struct smb2_echo_req *req;
4229 struct smb_rqst rqst = { .rq_iov = iov,
4231 unsigned int total_len;
4233 cifs_dbg(FYI, "In echo request for conn_id %lld\n", server->conn_id);
4235 spin_lock(&server->srv_lock);
4236 if (server->ops->need_neg &&
4237 server->ops->need_neg(server)) {
4238 spin_unlock(&server->srv_lock);
4239 /* No need to send echo on newly established connections */
4240 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
4243 spin_unlock(&server->srv_lock);
4245 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
4246 (void **)&req, &total_len);
4250 req->hdr.CreditRequest = cpu_to_le16(1);
4252 iov[0].iov_len = total_len;
4253 iov[0].iov_base = (char *)req;
4255 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
4256 server, CIFS_ECHO_OP, NULL);
4258 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
4260 cifs_small_buf_release(req);
4265 SMB2_flush_free(struct smb_rqst *rqst)
4267 if (rqst && rqst->rq_iov)
4268 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4272 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
4273 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
4274 u64 persistent_fid, u64 volatile_fid)
4276 struct smb2_flush_req *req;
4277 struct kvec *iov = rqst->rq_iov;
4278 unsigned int total_len;
4281 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
4282 (void **) &req, &total_len);
4286 req->PersistentFileId = persistent_fid;
4287 req->VolatileFileId = volatile_fid;
4289 iov[0].iov_base = (char *)req;
4290 iov[0].iov_len = total_len;
4296 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4299 struct cifs_ses *ses = tcon->ses;
4300 struct smb_rqst rqst;
4302 struct kvec rsp_iov = {NULL, 0};
4303 struct TCP_Server_Info *server;
4304 int resp_buftype = CIFS_NO_BUFFER;
4307 int retries = 0, cur_sleep = 1;
4310 /* reinitialize for possible replay */
4312 server = cifs_pick_channel(ses);
4314 cifs_dbg(FYI, "flush\n");
4315 if (!ses || !(ses->server))
4318 if (smb3_encryption_required(tcon))
4319 flags |= CIFS_TRANSFORM_REQ;
4321 memset(&rqst, 0, sizeof(struct smb_rqst));
4322 memset(&iov, 0, sizeof(iov));
4326 rc = SMB2_flush_init(xid, &rqst, tcon, server,
4327 persistent_fid, volatile_fid);
4331 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
4334 smb2_set_replay(server, &rqst);
4336 rc = cifs_send_recv(xid, ses, server,
4337 &rqst, &resp_buftype, flags, &rsp_iov);
4340 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
4341 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
4344 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
4348 SMB2_flush_free(&rqst);
4349 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4351 if (is_replayable_error(rc) &&
4352 smb2_should_replay(tcon, &retries, &cur_sleep))
4358 #ifdef CONFIG_CIFS_SMB_DIRECT
4359 static inline bool smb3_use_rdma_offload(struct cifs_io_parms *io_parms)
4361 struct TCP_Server_Info *server = io_parms->server;
4362 struct cifs_tcon *tcon = io_parms->tcon;
4364 /* we can only offload if we're connected */
4365 if (!server || !tcon)
4368 /* we can only offload on an rdma connection */
4369 if (!server->rdma || !server->smbd_conn)
4372 /* we don't support signed offload yet */
4376 /* we don't support encrypted offload yet */
4377 if (smb3_encryption_required(tcon))
4380 /* offload also has its overhead, so only do it if desired */
4381 if (io_parms->length < server->smbd_conn->rdma_readwrite_threshold)
4386 #endif /* CONFIG_CIFS_SMB_DIRECT */
4389 * To form a chain of read requests, any read requests after the first should
4390 * have the end_of_chain boolean set to true.
4393 smb2_new_read_req(void **buf, unsigned int *total_len,
4394 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
4395 unsigned int remaining_bytes, int request_type)
4398 struct smb2_read_req *req = NULL;
4399 struct smb2_hdr *shdr;
4400 struct TCP_Server_Info *server = io_parms->server;
4402 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
4403 (void **) &req, total_len);
4408 return -ECONNABORTED;
4411 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4413 req->PersistentFileId = io_parms->persistent_fid;
4414 req->VolatileFileId = io_parms->volatile_fid;
4415 req->ReadChannelInfoOffset = 0; /* reserved */
4416 req->ReadChannelInfoLength = 0; /* reserved */
4417 req->Channel = 0; /* reserved */
4418 req->MinimumCount = 0;
4419 req->Length = cpu_to_le32(io_parms->length);
4420 req->Offset = cpu_to_le64(io_parms->offset);
4422 trace_smb3_read_enter(0 /* xid */,
4423 io_parms->persistent_fid,
4424 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4425 io_parms->offset, io_parms->length);
4426 #ifdef CONFIG_CIFS_SMB_DIRECT
4428 * If we want to do a RDMA write, fill in and append
4429 * smbd_buffer_descriptor_v1 to the end of read request
4431 if (smb3_use_rdma_offload(io_parms)) {
4432 struct smbd_buffer_descriptor_v1 *v1;
4433 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4435 rdata->mr = smbd_register_mr(server->smbd_conn, &rdata->iter,
4436 true, need_invalidate);
4440 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4441 if (need_invalidate)
4442 req->Channel = SMB2_CHANNEL_RDMA_V1;
4443 req->ReadChannelInfoOffset =
4444 cpu_to_le16(offsetof(struct smb2_read_req, Buffer));
4445 req->ReadChannelInfoLength =
4446 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4447 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4448 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
4449 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
4450 v1->length = cpu_to_le32(rdata->mr->mr->length);
4452 *total_len += sizeof(*v1) - 1;
4455 if (request_type & CHAINED_REQUEST) {
4456 if (!(request_type & END_OF_CHAIN)) {
4457 /* next 8-byte aligned request */
4458 *total_len = ALIGN(*total_len, 8);
4459 shdr->NextCommand = cpu_to_le32(*total_len);
4460 } else /* END_OF_CHAIN */
4461 shdr->NextCommand = 0;
4462 if (request_type & RELATED_REQUEST) {
4463 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
4465 * Related requests use info from previous read request
4468 shdr->SessionId = cpu_to_le64(0xFFFFFFFFFFFFFFFF);
4469 shdr->Id.SyncId.TreeId = cpu_to_le32(0xFFFFFFFF);
4470 req->PersistentFileId = (u64)-1;
4471 req->VolatileFileId = (u64)-1;
4474 if (remaining_bytes > io_parms->length)
4475 req->RemainingBytes = cpu_to_le32(remaining_bytes);
4477 req->RemainingBytes = 0;
4484 smb2_readv_callback(struct mid_q_entry *mid)
4486 struct cifs_readdata *rdata = mid->callback_data;
4487 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
4488 struct TCP_Server_Info *server = rdata->server;
4489 struct smb2_hdr *shdr =
4490 (struct smb2_hdr *)rdata->iov[0].iov_base;
4491 struct cifs_credits credits = { .value = 0, .instance = 0 };
4492 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1], .rq_nvec = 1 };
4494 if (rdata->got_bytes) {
4495 rqst.rq_iter = rdata->iter;
4496 rqst.rq_iter_size = iov_iter_count(&rdata->iter);
4499 WARN_ONCE(rdata->server != mid->server,
4500 "rdata server %p != mid server %p",
4501 rdata->server, mid->server);
4503 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
4504 __func__, mid->mid, mid->mid_state, rdata->result,
4507 switch (mid->mid_state) {
4508 case MID_RESPONSE_RECEIVED:
4509 credits.value = le16_to_cpu(shdr->CreditRequest);
4510 credits.instance = server->reconnect_instance;
4511 /* result already set, check signature */
4512 if (server->sign && !mid->decrypted) {
4515 iov_iter_revert(&rqst.rq_iter, rdata->got_bytes);
4516 iov_iter_truncate(&rqst.rq_iter, rdata->got_bytes);
4517 rc = smb2_verify_signature(&rqst, server);
4519 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
4522 /* FIXME: should this be counted toward the initiating task? */
4523 task_io_account_read(rdata->got_bytes);
4524 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4526 case MID_REQUEST_SUBMITTED:
4527 case MID_RETRY_NEEDED:
4528 rdata->result = -EAGAIN;
4529 if (server->sign && rdata->got_bytes)
4530 /* reset bytes number since we can not check a sign */
4531 rdata->got_bytes = 0;
4532 /* FIXME: should this be counted toward the initiating task? */
4533 task_io_account_read(rdata->got_bytes);
4534 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4536 case MID_RESPONSE_MALFORMED:
4537 credits.value = le16_to_cpu(shdr->CreditRequest);
4538 credits.instance = server->reconnect_instance;
4541 rdata->result = -EIO;
4543 #ifdef CONFIG_CIFS_SMB_DIRECT
4545 * If this rdata has a memmory registered, the MR can be freed
4546 * MR needs to be freed as soon as I/O finishes to prevent deadlock
4547 * because they have limited number and are used for future I/Os
4550 smbd_deregister_mr(rdata->mr);
4554 if (rdata->result && rdata->result != -ENODATA) {
4555 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
4556 trace_smb3_read_err(0 /* xid */,
4557 rdata->cfile->fid.persistent_fid,
4558 tcon->tid, tcon->ses->Suid, rdata->offset,
4559 rdata->bytes, rdata->result);
4561 trace_smb3_read_done(0 /* xid */,
4562 rdata->cfile->fid.persistent_fid,
4563 tcon->tid, tcon->ses->Suid,
4564 rdata->offset, rdata->got_bytes);
4566 queue_work(cifsiod_wq, &rdata->work);
4568 add_credits(server, &credits, 0);
4571 /* smb2_async_readv - send an async read, and set up mid to handle result */
4573 smb2_async_readv(struct cifs_readdata *rdata)
4577 struct smb2_hdr *shdr;
4578 struct cifs_io_parms io_parms;
4579 struct smb_rqst rqst = { .rq_iov = rdata->iov,
4581 struct TCP_Server_Info *server;
4582 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
4583 unsigned int total_len;
4586 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
4587 __func__, rdata->offset, rdata->bytes);
4590 rdata->server = cifs_pick_channel(tcon->ses);
4592 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
4593 io_parms.server = server = rdata->server;
4594 io_parms.offset = rdata->offset;
4595 io_parms.length = rdata->bytes;
4596 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
4597 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
4598 io_parms.pid = rdata->pid;
4600 rc = smb2_new_read_req(
4601 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
4605 if (smb3_encryption_required(io_parms.tcon))
4606 flags |= CIFS_TRANSFORM_REQ;
4608 rdata->iov[0].iov_base = buf;
4609 rdata->iov[0].iov_len = total_len;
4611 shdr = (struct smb2_hdr *)buf;
4613 if (rdata->credits.value > 0) {
4614 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
4615 SMB2_MAX_BUFFER_SIZE));
4616 credit_request = le16_to_cpu(shdr->CreditCharge) + 8;
4617 if (server->credits >= server->max_credits)
4618 shdr->CreditRequest = cpu_to_le16(0);
4620 shdr->CreditRequest = cpu_to_le16(
4621 min_t(int, server->max_credits -
4622 server->credits, credit_request));
4624 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4626 goto async_readv_out;
4628 flags |= CIFS_HAS_CREDITS;
4631 kref_get(&rdata->refcount);
4632 rc = cifs_call_async(server, &rqst,
4633 cifs_readv_receive, smb2_readv_callback,
4634 smb3_handle_read_data, rdata, flags,
4637 kref_put(&rdata->refcount, cifs_readdata_release);
4638 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
4639 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
4641 io_parms.tcon->ses->Suid,
4642 io_parms.offset, io_parms.length, rc);
4646 cifs_small_buf_release(buf);
4651 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
4652 unsigned int *nbytes, char **buf, int *buf_type)
4654 struct smb_rqst rqst;
4655 int resp_buftype, rc;
4656 struct smb2_read_req *req = NULL;
4657 struct smb2_read_rsp *rsp = NULL;
4659 struct kvec rsp_iov;
4660 unsigned int total_len;
4661 int flags = CIFS_LOG_ERROR;
4662 struct cifs_ses *ses = io_parms->tcon->ses;
4664 if (!io_parms->server)
4665 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4668 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
4672 if (smb3_encryption_required(io_parms->tcon))
4673 flags |= CIFS_TRANSFORM_REQ;
4675 iov[0].iov_base = (char *)req;
4676 iov[0].iov_len = total_len;
4678 memset(&rqst, 0, sizeof(struct smb_rqst));
4682 rc = cifs_send_recv(xid, ses, io_parms->server,
4683 &rqst, &resp_buftype, flags, &rsp_iov);
4684 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
4687 if (rc != -ENODATA) {
4688 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4689 cifs_dbg(VFS, "Send error in read = %d\n", rc);
4690 trace_smb3_read_err(xid,
4691 req->PersistentFileId,
4692 io_parms->tcon->tid, ses->Suid,
4693 io_parms->offset, io_parms->length,
4696 trace_smb3_read_done(xid, req->PersistentFileId, io_parms->tcon->tid,
4697 ses->Suid, io_parms->offset, 0);
4698 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4699 cifs_small_buf_release(req);
4700 return rc == -ENODATA ? 0 : rc;
4702 trace_smb3_read_done(xid,
4703 req->PersistentFileId,
4704 io_parms->tcon->tid, ses->Suid,
4705 io_parms->offset, io_parms->length);
4707 cifs_small_buf_release(req);
4709 *nbytes = le32_to_cpu(rsp->DataLength);
4710 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4711 (*nbytes > io_parms->length)) {
4712 cifs_dbg(FYI, "bad length %d for count %d\n",
4713 *nbytes, io_parms->length);
4719 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
4720 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4721 } else if (resp_buftype != CIFS_NO_BUFFER) {
4722 *buf = rsp_iov.iov_base;
4723 if (resp_buftype == CIFS_SMALL_BUFFER)
4724 *buf_type = CIFS_SMALL_BUFFER;
4725 else if (resp_buftype == CIFS_LARGE_BUFFER)
4726 *buf_type = CIFS_LARGE_BUFFER;
4732 * Check the mid_state and signature on received buffer (if any), and queue the
4733 * workqueue completion task.
4736 smb2_writev_callback(struct mid_q_entry *mid)
4738 struct cifs_writedata *wdata = mid->callback_data;
4739 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4740 struct TCP_Server_Info *server = wdata->server;
4741 unsigned int written;
4742 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
4743 struct cifs_credits credits = { .value = 0, .instance = 0 };
4745 WARN_ONCE(wdata->server != mid->server,
4746 "wdata server %p != mid server %p",
4747 wdata->server, mid->server);
4749 switch (mid->mid_state) {
4750 case MID_RESPONSE_RECEIVED:
4751 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4752 credits.instance = server->reconnect_instance;
4753 wdata->result = smb2_check_receive(mid, server, 0);
4754 if (wdata->result != 0)
4757 written = le32_to_cpu(rsp->DataLength);
4759 * Mask off high 16 bits when bytes written as returned
4760 * by the server is greater than bytes requested by the
4761 * client. OS/2 servers are known to set incorrect
4764 if (written > wdata->bytes)
4767 if (written < wdata->bytes)
4768 wdata->result = -ENOSPC;
4770 wdata->bytes = written;
4772 case MID_REQUEST_SUBMITTED:
4773 case MID_RETRY_NEEDED:
4774 wdata->result = -EAGAIN;
4776 case MID_RESPONSE_MALFORMED:
4777 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4778 credits.instance = server->reconnect_instance;
4781 wdata->result = -EIO;
4784 #ifdef CONFIG_CIFS_SMB_DIRECT
4786 * If this wdata has a memory registered, the MR can be freed
4787 * The number of MRs available is limited, it's important to recover
4788 * used MR as soon as I/O is finished. Hold MR longer in the later
4789 * I/O process can possibly result in I/O deadlock due to lack of MR
4790 * to send request on I/O retry
4793 smbd_deregister_mr(wdata->mr);
4797 if (wdata->result) {
4798 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4799 trace_smb3_write_err(0 /* no xid */,
4800 wdata->cfile->fid.persistent_fid,
4801 tcon->tid, tcon->ses->Suid, wdata->offset,
4802 wdata->bytes, wdata->result);
4803 if (wdata->result == -ENOSPC)
4804 pr_warn_once("Out of space writing to %s\n",
4807 trace_smb3_write_done(0 /* no xid */,
4808 wdata->cfile->fid.persistent_fid,
4809 tcon->tid, tcon->ses->Suid,
4810 wdata->offset, wdata->bytes);
4812 queue_work(cifsiod_wq, &wdata->work);
4814 add_credits(server, &credits, 0);
4817 /* smb2_async_writev - send an async write, and set up mid to handle result */
4819 smb2_async_writev(struct cifs_writedata *wdata,
4820 void (*release)(struct kref *kref))
4822 int rc = -EACCES, flags = 0;
4823 struct smb2_write_req *req = NULL;
4824 struct smb2_hdr *shdr;
4825 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4826 struct TCP_Server_Info *server = wdata->server;
4828 struct smb_rqst rqst = { };
4829 unsigned int total_len;
4830 struct cifs_io_parms _io_parms;
4831 struct cifs_io_parms *io_parms = NULL;
4834 if (!wdata->server || wdata->replay)
4835 server = wdata->server = cifs_pick_channel(tcon->ses);
4838 * in future we may get cifs_io_parms passed in from the caller,
4839 * but for now we construct it here...
4841 _io_parms = (struct cifs_io_parms) {
4844 .offset = wdata->offset,
4845 .length = wdata->bytes,
4846 .persistent_fid = wdata->cfile->fid.persistent_fid,
4847 .volatile_fid = wdata->cfile->fid.volatile_fid,
4850 io_parms = &_io_parms;
4852 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
4853 (void **) &req, &total_len);
4857 if (smb3_encryption_required(tcon))
4858 flags |= CIFS_TRANSFORM_REQ;
4860 shdr = (struct smb2_hdr *)req;
4861 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4863 req->PersistentFileId = io_parms->persistent_fid;
4864 req->VolatileFileId = io_parms->volatile_fid;
4865 req->WriteChannelInfoOffset = 0;
4866 req->WriteChannelInfoLength = 0;
4867 req->Channel = SMB2_CHANNEL_NONE;
4868 req->Offset = cpu_to_le64(io_parms->offset);
4869 req->DataOffset = cpu_to_le16(
4870 offsetof(struct smb2_write_req, Buffer));
4871 req->RemainingBytes = 0;
4873 trace_smb3_write_enter(0 /* xid */,
4874 io_parms->persistent_fid,
4875 io_parms->tcon->tid,
4876 io_parms->tcon->ses->Suid,
4880 #ifdef CONFIG_CIFS_SMB_DIRECT
4882 * If we want to do a server RDMA read, fill in and append
4883 * smbd_buffer_descriptor_v1 to the end of write request
4885 if (smb3_use_rdma_offload(io_parms)) {
4886 struct smbd_buffer_descriptor_v1 *v1;
4887 size_t data_size = iov_iter_count(&wdata->iter);
4888 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4890 wdata->mr = smbd_register_mr(server->smbd_conn, &wdata->iter,
4891 false, need_invalidate);
4894 goto async_writev_out;
4897 req->DataOffset = 0;
4898 req->RemainingBytes = cpu_to_le32(data_size);
4899 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4900 if (need_invalidate)
4901 req->Channel = SMB2_CHANNEL_RDMA_V1;
4902 req->WriteChannelInfoOffset =
4903 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4904 req->WriteChannelInfoLength =
4905 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4906 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4907 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4908 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4909 v1->length = cpu_to_le32(wdata->mr->mr->length);
4912 iov[0].iov_len = total_len - 1;
4913 iov[0].iov_base = (char *)req;
4917 rqst.rq_iter = wdata->iter;
4918 rqst.rq_iter_size = iov_iter_count(&rqst.rq_iter);
4920 smb2_set_replay(server, &rqst);
4921 #ifdef CONFIG_CIFS_SMB_DIRECT
4923 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4925 cifs_dbg(FYI, "async write at %llu %u bytes iter=%zx\n",
4926 io_parms->offset, io_parms->length, iov_iter_count(&rqst.rq_iter));
4928 #ifdef CONFIG_CIFS_SMB_DIRECT
4929 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4931 req->Length = cpu_to_le32(io_parms->length);
4933 req->Length = cpu_to_le32(io_parms->length);
4936 if (wdata->credits.value > 0) {
4937 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4938 SMB2_MAX_BUFFER_SIZE));
4939 credit_request = le16_to_cpu(shdr->CreditCharge) + 8;
4940 if (server->credits >= server->max_credits)
4941 shdr->CreditRequest = cpu_to_le16(0);
4943 shdr->CreditRequest = cpu_to_le16(
4944 min_t(int, server->max_credits -
4945 server->credits, credit_request));
4947 rc = adjust_credits(server, &wdata->credits, io_parms->length);
4949 goto async_writev_out;
4951 flags |= CIFS_HAS_CREDITS;
4954 kref_get(&wdata->refcount);
4955 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4956 wdata, flags, &wdata->credits);
4959 trace_smb3_write_err(0 /* no xid */,
4960 io_parms->persistent_fid,
4961 io_parms->tcon->tid,
4962 io_parms->tcon->ses->Suid,
4966 kref_put(&wdata->refcount, release);
4967 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4971 cifs_small_buf_release(req);
4976 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4977 * The length field from io_parms must be at least 1 and indicates a number of
4978 * elements with data to write that begins with position 1 in iov array. All
4979 * data length is specified by count.
4982 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4983 unsigned int *nbytes, struct kvec *iov, int n_vec)
4985 struct smb_rqst rqst;
4987 struct smb2_write_req *req = NULL;
4988 struct smb2_write_rsp *rsp = NULL;
4990 struct kvec rsp_iov;
4992 unsigned int total_len;
4993 struct TCP_Server_Info *server;
4994 int retries = 0, cur_sleep = 1;
4997 /* reinitialize for possible replay */
5000 if (!io_parms->server)
5001 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
5002 server = io_parms->server;
5004 return -ECONNABORTED;
5009 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
5010 (void **) &req, &total_len);
5014 if (smb3_encryption_required(io_parms->tcon))
5015 flags |= CIFS_TRANSFORM_REQ;
5017 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
5019 req->PersistentFileId = io_parms->persistent_fid;
5020 req->VolatileFileId = io_parms->volatile_fid;
5021 req->WriteChannelInfoOffset = 0;
5022 req->WriteChannelInfoLength = 0;
5024 req->Length = cpu_to_le32(io_parms->length);
5025 req->Offset = cpu_to_le64(io_parms->offset);
5026 req->DataOffset = cpu_to_le16(
5027 offsetof(struct smb2_write_req, Buffer));
5028 req->RemainingBytes = 0;
5030 trace_smb3_write_enter(xid, io_parms->persistent_fid,
5031 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
5032 io_parms->offset, io_parms->length);
5034 iov[0].iov_base = (char *)req;
5036 iov[0].iov_len = total_len - 1;
5038 memset(&rqst, 0, sizeof(struct smb_rqst));
5040 rqst.rq_nvec = n_vec + 1;
5043 smb2_set_replay(server, &rqst);
5045 rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
5047 &resp_buftype, flags, &rsp_iov);
5048 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
5051 trace_smb3_write_err(xid,
5052 req->PersistentFileId,
5053 io_parms->tcon->tid,
5054 io_parms->tcon->ses->Suid,
5055 io_parms->offset, io_parms->length, rc);
5056 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
5057 cifs_dbg(VFS, "Send error in write = %d\n", rc);
5059 *nbytes = le32_to_cpu(rsp->DataLength);
5060 trace_smb3_write_done(xid,
5061 req->PersistentFileId,
5062 io_parms->tcon->tid,
5063 io_parms->tcon->ses->Suid,
5064 io_parms->offset, *nbytes);
5067 cifs_small_buf_release(req);
5068 free_rsp_buf(resp_buftype, rsp);
5070 if (is_replayable_error(rc) &&
5071 smb2_should_replay(io_parms->tcon, &retries, &cur_sleep))
5077 int posix_info_sid_size(const void *beg, const void *end)
5085 subauth = *(u8 *)(beg+1);
5086 if (subauth < 1 || subauth > 15)
5089 total = 1 + 1 + 6 + 4*subauth;
5090 if (beg + total > end)
5096 int posix_info_parse(const void *beg, const void *end,
5097 struct smb2_posix_info_parsed *out)
5101 int owner_len, group_len;
5103 const void *owner_sid;
5104 const void *group_sid;
5107 /* if no end bound given, assume payload to be correct */
5109 const struct smb2_posix_info *p = beg;
5111 end = beg + le32_to_cpu(p->NextEntryOffset);
5112 /* last element will have a 0 offset, pick a sensible bound */
5117 /* check base buf */
5118 if (beg + sizeof(struct smb2_posix_info) > end)
5120 total_len = sizeof(struct smb2_posix_info);
5122 /* check owner sid */
5123 owner_sid = beg + total_len;
5124 owner_len = posix_info_sid_size(owner_sid, end);
5127 total_len += owner_len;
5129 /* check group sid */
5130 group_sid = beg + total_len;
5131 group_len = posix_info_sid_size(group_sid, end);
5134 total_len += group_len;
5136 /* check name len */
5137 if (beg + total_len + 4 > end)
5139 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
5140 if (name_len < 1 || name_len > 0xFFFF)
5145 name = beg + total_len;
5146 if (name + name_len > end)
5148 total_len += name_len;
5152 out->size = total_len;
5153 out->name_len = name_len;
5155 memcpy(&out->owner, owner_sid, owner_len);
5156 memcpy(&out->group, group_sid, group_len);
5161 static int posix_info_extra_size(const void *beg, const void *end)
5163 int len = posix_info_parse(beg, end, NULL);
5167 return len - sizeof(struct smb2_posix_info);
5171 num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
5175 unsigned int entrycount = 0;
5176 unsigned int next_offset = 0;
5178 FILE_DIRECTORY_INFO *dir_info;
5180 if (bufstart == NULL)
5183 entryptr = bufstart;
5186 if (entryptr + next_offset < entryptr ||
5187 entryptr + next_offset > end_of_buf ||
5188 entryptr + next_offset + size > end_of_buf) {
5189 cifs_dbg(VFS, "malformed search entry would overflow\n");
5193 entryptr = entryptr + next_offset;
5194 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
5196 if (infotype == SMB_FIND_FILE_POSIX_INFO)
5197 len = posix_info_extra_size(entryptr, end_of_buf);
5199 len = le32_to_cpu(dir_info->FileNameLength);
5202 entryptr + len < entryptr ||
5203 entryptr + len > end_of_buf ||
5204 entryptr + len + size > end_of_buf) {
5205 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
5210 *lastentry = entryptr;
5213 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
5224 int SMB2_query_directory_init(const unsigned int xid,
5225 struct cifs_tcon *tcon,
5226 struct TCP_Server_Info *server,
5227 struct smb_rqst *rqst,
5228 u64 persistent_fid, u64 volatile_fid,
5229 int index, int info_level)
5231 struct smb2_query_directory_req *req;
5232 unsigned char *bufptr;
5233 __le16 asteriks = cpu_to_le16('*');
5234 unsigned int output_size = CIFSMaxBufSize -
5235 MAX_SMB2_CREATE_RESPONSE_SIZE -
5236 MAX_SMB2_CLOSE_RESPONSE_SIZE;
5237 unsigned int total_len;
5238 struct kvec *iov = rqst->rq_iov;
5241 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
5242 (void **) &req, &total_len);
5246 switch (info_level) {
5247 case SMB_FIND_FILE_DIRECTORY_INFO:
5248 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
5250 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
5251 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
5253 case SMB_FIND_FILE_POSIX_INFO:
5254 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
5256 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
5257 req->FileInformationClass = FILE_FULL_DIRECTORY_INFORMATION;
5260 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
5265 req->FileIndex = cpu_to_le32(index);
5266 req->PersistentFileId = persistent_fid;
5267 req->VolatileFileId = volatile_fid;
5270 bufptr = req->Buffer;
5271 memcpy(bufptr, &asteriks, len);
5273 req->FileNameOffset =
5274 cpu_to_le16(sizeof(struct smb2_query_directory_req));
5275 req->FileNameLength = cpu_to_le16(len);
5277 * BB could be 30 bytes or so longer if we used SMB2 specific
5278 * buffer lengths, but this is safe and close enough.
5280 output_size = min_t(unsigned int, output_size, server->maxBuf);
5281 output_size = min_t(unsigned int, output_size, 2 << 15);
5282 req->OutputBufferLength = cpu_to_le32(output_size);
5284 iov[0].iov_base = (char *)req;
5286 iov[0].iov_len = total_len - 1;
5288 iov[1].iov_base = (char *)(req->Buffer);
5289 iov[1].iov_len = len;
5291 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
5292 tcon->ses->Suid, index, output_size);
5297 void SMB2_query_directory_free(struct smb_rqst *rqst)
5299 if (rqst && rqst->rq_iov) {
5300 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
5305 smb2_parse_query_directory(struct cifs_tcon *tcon,
5306 struct kvec *rsp_iov,
5308 struct cifs_search_info *srch_inf)
5310 struct smb2_query_directory_rsp *rsp;
5311 size_t info_buf_size;
5315 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
5317 switch (srch_inf->info_level) {
5318 case SMB_FIND_FILE_DIRECTORY_INFO:
5319 info_buf_size = sizeof(FILE_DIRECTORY_INFO);
5321 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
5322 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO);
5324 case SMB_FIND_FILE_POSIX_INFO:
5325 /* note that posix payload are variable size */
5326 info_buf_size = sizeof(struct smb2_posix_info);
5328 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
5329 info_buf_size = sizeof(FILE_FULL_DIRECTORY_INFO);
5332 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
5333 srch_inf->info_level);
5337 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5338 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
5341 cifs_tcon_dbg(VFS, "bad info payload");
5345 srch_inf->unicode = true;
5347 if (srch_inf->ntwrk_buf_start) {
5348 if (srch_inf->smallBuf)
5349 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
5351 cifs_buf_release(srch_inf->ntwrk_buf_start);
5353 srch_inf->ntwrk_buf_start = (char *)rsp;
5354 srch_inf->srch_entries_start = srch_inf->last_entry =
5355 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
5356 end_of_smb = rsp_iov->iov_len + (char *)rsp;
5358 srch_inf->entries_in_buffer = num_entries(
5359 srch_inf->info_level,
5360 srch_inf->srch_entries_start,
5362 &srch_inf->last_entry,
5365 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
5366 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
5367 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
5368 srch_inf->srch_entries_start, srch_inf->last_entry);
5369 if (resp_buftype == CIFS_LARGE_BUFFER)
5370 srch_inf->smallBuf = false;
5371 else if (resp_buftype == CIFS_SMALL_BUFFER)
5372 srch_inf->smallBuf = true;
5374 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
5380 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
5381 u64 persistent_fid, u64 volatile_fid, int index,
5382 struct cifs_search_info *srch_inf)
5384 struct smb_rqst rqst;
5385 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
5386 struct smb2_query_directory_rsp *rsp = NULL;
5387 int resp_buftype = CIFS_NO_BUFFER;
5388 struct kvec rsp_iov;
5390 struct cifs_ses *ses = tcon->ses;
5391 struct TCP_Server_Info *server;
5393 int retries = 0, cur_sleep = 1;
5396 /* reinitialize for possible replay */
5398 server = cifs_pick_channel(ses);
5400 if (!ses || !(ses->server))
5403 if (smb3_encryption_required(tcon))
5404 flags |= CIFS_TRANSFORM_REQ;
5406 memset(&rqst, 0, sizeof(struct smb_rqst));
5407 memset(&iov, 0, sizeof(iov));
5409 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
5411 rc = SMB2_query_directory_init(xid, tcon, server,
5412 &rqst, persistent_fid,
5413 volatile_fid, index,
5414 srch_inf->info_level);
5419 smb2_set_replay(server, &rqst);
5421 rc = cifs_send_recv(xid, ses, server,
5422 &rqst, &resp_buftype, flags, &rsp_iov);
5423 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
5426 if (rc == -ENODATA &&
5427 rsp->hdr.Status == STATUS_NO_MORE_FILES) {
5428 trace_smb3_query_dir_done(xid, persistent_fid,
5429 tcon->tid, tcon->ses->Suid, index, 0);
5430 srch_inf->endOfSearch = true;
5433 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5434 tcon->ses->Suid, index, 0, rc);
5435 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
5440 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
5443 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5444 tcon->ses->Suid, index, 0, rc);
5447 resp_buftype = CIFS_NO_BUFFER;
5449 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
5450 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
5453 SMB2_query_directory_free(&rqst);
5454 free_rsp_buf(resp_buftype, rsp);
5456 if (is_replayable_error(rc) &&
5457 smb2_should_replay(tcon, &retries, &cur_sleep))
5464 SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
5465 struct smb_rqst *rqst,
5466 u64 persistent_fid, u64 volatile_fid, u32 pid,
5467 u8 info_class, u8 info_type, u32 additional_info,
5468 void **data, unsigned int *size)
5470 struct smb2_set_info_req *req;
5471 struct kvec *iov = rqst->rq_iov;
5472 unsigned int i, total_len;
5475 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
5476 (void **) &req, &total_len);
5480 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
5481 req->InfoType = info_type;
5482 req->FileInfoClass = info_class;
5483 req->PersistentFileId = persistent_fid;
5484 req->VolatileFileId = volatile_fid;
5485 req->AdditionalInformation = cpu_to_le32(additional_info);
5487 req->BufferOffset = cpu_to_le16(sizeof(struct smb2_set_info_req));
5488 req->BufferLength = cpu_to_le32(*size);
5490 memcpy(req->Buffer, *data, *size);
5493 iov[0].iov_base = (char *)req;
5495 iov[0].iov_len = total_len - 1;
5497 for (i = 1; i < rqst->rq_nvec; i++) {
5498 le32_add_cpu(&req->BufferLength, size[i]);
5499 iov[i].iov_base = (char *)data[i];
5500 iov[i].iov_len = size[i];
5507 SMB2_set_info_free(struct smb_rqst *rqst)
5509 if (rqst && rqst->rq_iov)
5510 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
5514 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
5515 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
5516 u8 info_type, u32 additional_info, unsigned int num,
5517 void **data, unsigned int *size)
5519 struct smb_rqst rqst;
5520 struct smb2_set_info_rsp *rsp = NULL;
5522 struct kvec rsp_iov;
5525 struct cifs_ses *ses = tcon->ses;
5526 struct TCP_Server_Info *server;
5528 int retries = 0, cur_sleep = 1;
5531 /* reinitialize for possible replay */
5533 server = cifs_pick_channel(ses);
5535 if (!ses || !server)
5541 if (smb3_encryption_required(tcon))
5542 flags |= CIFS_TRANSFORM_REQ;
5544 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
5548 memset(&rqst, 0, sizeof(struct smb_rqst));
5552 rc = SMB2_set_info_init(tcon, server,
5553 &rqst, persistent_fid, volatile_fid, pid,
5554 info_class, info_type, additional_info,
5562 smb2_set_replay(server, &rqst);
5564 rc = cifs_send_recv(xid, ses, server,
5565 &rqst, &resp_buftype, flags,
5567 SMB2_set_info_free(&rqst);
5568 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
5571 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
5572 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
5573 ses->Suid, info_class, (__u32)info_type, rc);
5576 free_rsp_buf(resp_buftype, rsp);
5579 if (is_replayable_error(rc) &&
5580 smb2_should_replay(tcon, &retries, &cur_sleep))
5587 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
5588 u64 volatile_fid, u32 pid, loff_t new_eof)
5590 struct smb2_file_eof_info info;
5594 info.EndOfFile = cpu_to_le64(new_eof);
5597 size = sizeof(struct smb2_file_eof_info);
5599 trace_smb3_set_eof(xid, persistent_fid, tcon->tid, tcon->ses->Suid, new_eof);
5601 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5602 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
5603 0, 1, &data, &size);
5607 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
5608 u64 persistent_fid, u64 volatile_fid,
5609 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
5611 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5612 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
5613 1, (void **)&pnntsd, &pacllen);
5617 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
5618 u64 persistent_fid, u64 volatile_fid,
5619 struct smb2_file_full_ea_info *buf, int len)
5621 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5622 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
5623 0, 1, (void **)&buf, &len);
5627 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
5628 const u64 persistent_fid, const u64 volatile_fid,
5631 struct smb_rqst rqst;
5633 struct smb2_oplock_break *req = NULL;
5634 struct cifs_ses *ses = tcon->ses;
5635 struct TCP_Server_Info *server;
5636 int flags = CIFS_OBREAK_OP;
5637 unsigned int total_len;
5639 struct kvec rsp_iov;
5641 int retries = 0, cur_sleep = 1;
5644 /* reinitialize for possible replay */
5645 flags = CIFS_OBREAK_OP;
5646 server = cifs_pick_channel(ses);
5648 cifs_dbg(FYI, "SMB2_oplock_break\n");
5649 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5650 (void **) &req, &total_len);
5654 if (smb3_encryption_required(tcon))
5655 flags |= CIFS_TRANSFORM_REQ;
5657 req->VolatileFid = volatile_fid;
5658 req->PersistentFid = persistent_fid;
5659 req->OplockLevel = oplock_level;
5660 req->hdr.CreditRequest = cpu_to_le16(1);
5662 flags |= CIFS_NO_RSP_BUF;
5664 iov[0].iov_base = (char *)req;
5665 iov[0].iov_len = total_len;
5667 memset(&rqst, 0, sizeof(struct smb_rqst));
5672 smb2_set_replay(server, &rqst);
5674 rc = cifs_send_recv(xid, ses, server,
5675 &rqst, &resp_buf_type, flags, &rsp_iov);
5676 cifs_small_buf_release(req);
5678 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5679 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
5682 if (is_replayable_error(rc) &&
5683 smb2_should_replay(tcon, &retries, &cur_sleep))
5690 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
5691 struct kstatfs *kst)
5693 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
5694 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
5695 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
5696 kst->f_bfree = kst->f_bavail =
5697 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
5702 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
5703 struct kstatfs *kst)
5705 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
5706 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
5707 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
5708 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
5709 kst->f_bavail = kst->f_bfree;
5711 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
5712 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5713 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
5714 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5715 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
5721 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
5722 struct TCP_Server_Info *server,
5723 int level, int outbuf_len, u64 persistent_fid,
5727 struct smb2_query_info_req *req;
5728 unsigned int total_len;
5730 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
5732 if ((tcon->ses == NULL) || server == NULL)
5735 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
5736 (void **) &req, &total_len);
5740 req->InfoType = SMB2_O_INFO_FILESYSTEM;
5741 req->FileInfoClass = level;
5742 req->PersistentFileId = persistent_fid;
5743 req->VolatileFileId = volatile_fid;
5745 req->InputBufferOffset =
5746 cpu_to_le16(sizeof(struct smb2_query_info_req));
5747 req->OutputBufferLength = cpu_to_le32(
5748 outbuf_len + sizeof(struct smb2_query_info_rsp));
5750 iov->iov_base = (char *)req;
5751 iov->iov_len = total_len;
5755 static inline void free_qfs_info_req(struct kvec *iov)
5757 cifs_buf_release(iov->iov_base);
5761 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
5762 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5764 struct smb_rqst rqst;
5765 struct smb2_query_info_rsp *rsp = NULL;
5767 struct kvec rsp_iov;
5770 struct cifs_ses *ses = tcon->ses;
5771 struct TCP_Server_Info *server;
5772 FILE_SYSTEM_POSIX_INFO *info = NULL;
5774 int retries = 0, cur_sleep = 1;
5777 /* reinitialize for possible replay */
5779 server = cifs_pick_channel(ses);
5781 rc = build_qfs_info_req(&iov, tcon, server,
5782 FS_POSIX_INFORMATION,
5783 sizeof(FILE_SYSTEM_POSIX_INFO),
5784 persistent_fid, volatile_fid);
5788 if (smb3_encryption_required(tcon))
5789 flags |= CIFS_TRANSFORM_REQ;
5791 memset(&rqst, 0, sizeof(struct smb_rqst));
5796 smb2_set_replay(server, &rqst);
5798 rc = cifs_send_recv(xid, ses, server,
5799 &rqst, &resp_buftype, flags, &rsp_iov);
5800 free_qfs_info_req(&iov);
5802 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5803 goto posix_qfsinf_exit;
5805 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5807 info = (FILE_SYSTEM_POSIX_INFO *)(
5808 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5809 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5810 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5811 sizeof(FILE_SYSTEM_POSIX_INFO));
5813 copy_posix_fs_info_to_kstatfs(info, fsdata);
5816 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5818 if (is_replayable_error(rc) &&
5819 smb2_should_replay(tcon, &retries, &cur_sleep))
5826 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5827 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5829 struct smb_rqst rqst;
5830 struct smb2_query_info_rsp *rsp = NULL;
5832 struct kvec rsp_iov;
5835 struct cifs_ses *ses = tcon->ses;
5836 struct TCP_Server_Info *server;
5837 struct smb2_fs_full_size_info *info = NULL;
5839 int retries = 0, cur_sleep = 1;
5842 /* reinitialize for possible replay */
5844 server = cifs_pick_channel(ses);
5846 rc = build_qfs_info_req(&iov, tcon, server,
5847 FS_FULL_SIZE_INFORMATION,
5848 sizeof(struct smb2_fs_full_size_info),
5849 persistent_fid, volatile_fid);
5853 if (smb3_encryption_required(tcon))
5854 flags |= CIFS_TRANSFORM_REQ;
5856 memset(&rqst, 0, sizeof(struct smb_rqst));
5861 smb2_set_replay(server, &rqst);
5863 rc = cifs_send_recv(xid, ses, server,
5864 &rqst, &resp_buftype, flags, &rsp_iov);
5865 free_qfs_info_req(&iov);
5867 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5870 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5872 info = (struct smb2_fs_full_size_info *)(
5873 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5874 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5875 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5876 sizeof(struct smb2_fs_full_size_info));
5878 smb2_copy_fs_info_to_kstatfs(info, fsdata);
5881 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5883 if (is_replayable_error(rc) &&
5884 smb2_should_replay(tcon, &retries, &cur_sleep))
5891 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
5892 u64 persistent_fid, u64 volatile_fid, int level)
5894 struct smb_rqst rqst;
5895 struct smb2_query_info_rsp *rsp = NULL;
5897 struct kvec rsp_iov;
5899 int resp_buftype, max_len, min_len;
5900 struct cifs_ses *ses = tcon->ses;
5901 struct TCP_Server_Info *server;
5902 unsigned int rsp_len, offset;
5904 int retries = 0, cur_sleep = 1;
5907 /* reinitialize for possible replay */
5909 server = cifs_pick_channel(ses);
5911 if (level == FS_DEVICE_INFORMATION) {
5912 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5913 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5914 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5915 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5916 min_len = MIN_FS_ATTR_INFO_SIZE;
5917 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5918 max_len = sizeof(struct smb3_fs_ss_info);
5919 min_len = sizeof(struct smb3_fs_ss_info);
5920 } else if (level == FS_VOLUME_INFORMATION) {
5921 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5922 min_len = sizeof(struct smb3_fs_vol_info);
5924 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
5928 rc = build_qfs_info_req(&iov, tcon, server,
5930 persistent_fid, volatile_fid);
5934 if (smb3_encryption_required(tcon))
5935 flags |= CIFS_TRANSFORM_REQ;
5937 memset(&rqst, 0, sizeof(struct smb_rqst));
5942 smb2_set_replay(server, &rqst);
5944 rc = cifs_send_recv(xid, ses, server,
5945 &rqst, &resp_buftype, flags, &rsp_iov);
5946 free_qfs_info_req(&iov);
5948 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5951 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5953 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5954 offset = le16_to_cpu(rsp->OutputBufferOffset);
5955 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
5959 if (level == FS_ATTRIBUTE_INFORMATION)
5960 memcpy(&tcon->fsAttrInfo, offset
5961 + (char *)rsp, min_t(unsigned int,
5963 else if (level == FS_DEVICE_INFORMATION)
5964 memcpy(&tcon->fsDevInfo, offset
5965 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
5966 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5967 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
5968 (offset + (char *)rsp);
5969 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5970 tcon->perf_sector_size =
5971 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
5972 } else if (level == FS_VOLUME_INFORMATION) {
5973 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5974 (offset + (char *)rsp);
5975 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5976 tcon->vol_create_time = vol_info->VolumeCreationTime;
5980 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5982 if (is_replayable_error(rc) &&
5983 smb2_should_replay(tcon, &retries, &cur_sleep))
5990 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5991 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5992 const __u32 num_lock, struct smb2_lock_element *buf)
5994 struct smb_rqst rqst;
5996 struct smb2_lock_req *req = NULL;
5998 struct kvec rsp_iov;
6001 int flags = CIFS_NO_RSP_BUF;
6002 unsigned int total_len;
6003 struct TCP_Server_Info *server;
6004 int retries = 0, cur_sleep = 1;
6007 /* reinitialize for possible replay */
6008 flags = CIFS_NO_RSP_BUF;
6009 server = cifs_pick_channel(tcon->ses);
6011 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
6013 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
6014 (void **) &req, &total_len);
6018 if (smb3_encryption_required(tcon))
6019 flags |= CIFS_TRANSFORM_REQ;
6021 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
6022 req->LockCount = cpu_to_le16(num_lock);
6024 req->PersistentFileId = persist_fid;
6025 req->VolatileFileId = volatile_fid;
6027 count = num_lock * sizeof(struct smb2_lock_element);
6029 iov[0].iov_base = (char *)req;
6030 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
6031 iov[1].iov_base = (char *)buf;
6032 iov[1].iov_len = count;
6034 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
6036 memset(&rqst, 0, sizeof(struct smb_rqst));
6041 smb2_set_replay(server, &rqst);
6043 rc = cifs_send_recv(xid, tcon->ses, server,
6044 &rqst, &resp_buf_type, flags,
6046 cifs_small_buf_release(req);
6048 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
6049 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
6050 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
6051 tcon->ses->Suid, rc);
6054 if (is_replayable_error(rc) &&
6055 smb2_should_replay(tcon, &retries, &cur_sleep))
6062 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
6063 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
6064 const __u64 length, const __u64 offset, const __u32 lock_flags,
6067 struct smb2_lock_element lock;
6069 lock.Offset = cpu_to_le64(offset);
6070 lock.Length = cpu_to_le64(length);
6071 lock.Flags = cpu_to_le32(lock_flags);
6072 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
6073 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
6075 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
6079 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
6080 __u8 *lease_key, const __le32 lease_state)
6082 struct smb_rqst rqst;
6084 struct smb2_lease_ack *req = NULL;
6085 struct cifs_ses *ses = tcon->ses;
6086 int flags = CIFS_OBREAK_OP;
6087 unsigned int total_len;
6089 struct kvec rsp_iov;
6091 __u64 *please_key_high;
6092 __u64 *please_key_low;
6093 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
6095 cifs_dbg(FYI, "SMB2_lease_break\n");
6096 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
6097 (void **) &req, &total_len);
6101 if (smb3_encryption_required(tcon))
6102 flags |= CIFS_TRANSFORM_REQ;
6104 req->hdr.CreditRequest = cpu_to_le16(1);
6105 req->StructureSize = cpu_to_le16(36);
6108 memcpy(req->LeaseKey, lease_key, 16);
6109 req->LeaseState = lease_state;
6111 flags |= CIFS_NO_RSP_BUF;
6113 iov[0].iov_base = (char *)req;
6114 iov[0].iov_len = total_len;
6116 memset(&rqst, 0, sizeof(struct smb_rqst));
6120 rc = cifs_send_recv(xid, ses, server,
6121 &rqst, &resp_buf_type, flags, &rsp_iov);
6122 cifs_small_buf_release(req);
6124 please_key_low = (__u64 *)lease_key;
6125 please_key_high = (__u64 *)(lease_key+8);
6127 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
6128 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
6129 ses->Suid, *please_key_low, *please_key_high, rc);
6130 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
6132 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
6133 ses->Suid, *please_key_low, *please_key_high);