1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2009, 2013
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>
26 #include <linux/netfs.h>
27 #include <trace/events/netfs.h>
30 #include "cifsproto.h"
31 #include "smb2proto.h"
32 #include "cifs_unicode.h"
33 #include "cifs_debug.h"
35 #include "../common/smb2status.h"
38 #include "cifs_spnego.h"
39 #include "smbdirect.h"
41 #ifdef CONFIG_CIFS_DFS_UPCALL
42 #include "dfs_cache.h"
44 #include "cached_dir.h"
48 * The following table defines the expected "StructureSize" of SMB2 requests
49 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
51 * Note that commands are defined in smb2pdu.h in le16 but the array below is
52 * indexed by command in host byte order.
54 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
55 /* SMB2_NEGOTIATE */ 36,
56 /* SMB2_SESSION_SETUP */ 25,
58 /* SMB2_TREE_CONNECT */ 9,
59 /* SMB2_TREE_DISCONNECT */ 4,
69 /* SMB2_QUERY_DIRECTORY */ 33,
70 /* SMB2_CHANGE_NOTIFY */ 32,
71 /* SMB2_QUERY_INFO */ 41,
72 /* SMB2_SET_INFO */ 33,
73 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
76 int smb3_encryption_required(const struct cifs_tcon *tcon)
78 if (!tcon || !tcon->ses)
80 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
81 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
84 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
86 if (((global_secflags & CIFSSEC_MUST_SEAL) == CIFSSEC_MUST_SEAL) &&
87 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
93 smb2_hdr_assemble(struct smb2_hdr *shdr, __le16 smb2_cmd,
94 const struct cifs_tcon *tcon,
95 struct TCP_Server_Info *server)
97 struct smb3_hdr_req *smb3_hdr;
99 shdr->ProtocolId = SMB2_PROTO_NUMBER;
100 shdr->StructureSize = cpu_to_le16(64);
101 shdr->Command = smb2_cmd;
104 /* After reconnect SMB3 must set ChannelSequence on subsequent reqs */
105 if (server->dialect >= SMB30_PROT_ID) {
106 smb3_hdr = (struct smb3_hdr_req *)shdr;
108 * if primary channel is not set yet, use default
109 * channel for chan sequence num
111 if (SERVER_IS_CHAN(server))
112 smb3_hdr->ChannelSequence =
113 cpu_to_le16(server->primary_server->channel_sequence_num);
115 smb3_hdr->ChannelSequence =
116 cpu_to_le16(server->channel_sequence_num);
118 spin_lock(&server->req_lock);
119 /* Request up to 10 credits but don't go over the limit. */
120 if (server->credits >= server->max_credits)
121 shdr->CreditRequest = cpu_to_le16(0);
123 shdr->CreditRequest = cpu_to_le16(
124 min_t(int, server->max_credits -
125 server->credits, 10));
126 spin_unlock(&server->req_lock);
128 shdr->CreditRequest = cpu_to_le16(2);
130 shdr->Id.SyncId.ProcessId = cpu_to_le32((__u16)current->tgid);
135 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
136 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
137 if (server && (server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
138 shdr->CreditCharge = cpu_to_le16(1);
139 /* else CreditCharge MBZ */
141 shdr->Id.SyncId.TreeId = cpu_to_le32(tcon->tid);
142 /* Uid is not converted */
144 shdr->SessionId = cpu_to_le64(tcon->ses->Suid);
147 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
148 * to pass the path on the Open SMB prefixed by \\server\share.
149 * Not sure when we would need to do the augmented path (if ever) and
150 * setting this flag breaks the SMB2 open operation since it is
151 * illegal to send an empty path name (without \\server\share prefix)
152 * when the DFS flag is set in the SMB open header. We could
153 * consider setting the flag on all operations other than open
154 * but it is safer to net set it for now.
156 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
157 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
159 if (server && server->sign && !smb3_encryption_required(tcon))
160 shdr->Flags |= SMB2_FLAGS_SIGNED;
165 /* helper function for code reuse */
167 cifs_chan_skip_or_disable(struct cifs_ses *ses,
168 struct TCP_Server_Info *server,
171 struct TCP_Server_Info *pserver;
172 unsigned int chan_index;
174 if (SERVER_IS_CHAN(server)) {
176 "server %s does not support multichannel anymore. Skip secondary channel\n",
177 ses->server->hostname);
179 spin_lock(&ses->chan_lock);
180 chan_index = cifs_ses_get_chan_index(ses, server);
181 if (chan_index == CIFS_INVAL_CHAN_INDEX) {
182 spin_unlock(&ses->chan_lock);
186 ses->chans[chan_index].server = NULL;
187 server->terminate = true;
188 spin_unlock(&ses->chan_lock);
191 * the above reference of server by channel
192 * needs to be dropped without holding chan_lock
193 * as cifs_put_tcp_session takes a higher lock
194 * i.e. cifs_tcp_ses_lock
196 cifs_put_tcp_session(server, from_reconnect);
198 cifs_signal_cifsd_for_reconnect(server, false);
200 /* mark primary server as needing reconnect */
201 pserver = server->primary_server;
202 cifs_signal_cifsd_for_reconnect(pserver, false);
208 "server does not support multichannel anymore. Disable all other channels\n");
209 cifs_disable_secondary_channels(ses);
216 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
217 struct TCP_Server_Info *server, bool from_reconnect)
219 struct cifs_ses *ses;
224 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
225 * check for tcp and smb session status done differently
226 * for those three - in the calling routine.
231 if (smb2_command == SMB2_TREE_CONNECT)
234 spin_lock(&tcon->tc_lock);
235 if (tcon->status == TID_EXITING) {
237 * only tree disconnect allowed when disconnecting ...
239 if (smb2_command != SMB2_TREE_DISCONNECT) {
240 spin_unlock(&tcon->tc_lock);
241 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
246 spin_unlock(&tcon->tc_lock);
251 spin_lock(&ses->ses_lock);
252 if (ses->ses_status == SES_EXITING) {
253 spin_unlock(&ses->ses_lock);
256 spin_unlock(&ses->ses_lock);
257 if (!ses->server || !server)
260 spin_lock(&server->srv_lock);
261 if (server->tcpStatus == CifsNeedReconnect) {
263 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
264 * here since they are implicitly done when session drops.
266 switch (smb2_command) {
268 * BB Should we keep oplock break and add flush to exceptions?
270 case SMB2_TREE_DISCONNECT:
273 case SMB2_OPLOCK_BREAK:
274 spin_unlock(&server->srv_lock);
279 /* if server is marked for termination, cifsd will cleanup */
280 if (server->terminate) {
281 spin_unlock(&server->srv_lock);
284 spin_unlock(&server->srv_lock);
287 rc = cifs_wait_for_server_reconnect(server, tcon->retry);
291 spin_lock(&ses->chan_lock);
292 if (!cifs_chan_needs_reconnect(ses, server) && !tcon->need_reconnect) {
293 spin_unlock(&ses->chan_lock);
296 spin_unlock(&ses->chan_lock);
297 cifs_dbg(FYI, "sess reconnect mask: 0x%lx, tcon reconnect: %d",
298 tcon->ses->chans_need_reconnect,
299 tcon->need_reconnect);
301 mutex_lock(&ses->session_mutex);
303 * if this is called by delayed work, and the channel has been disabled
304 * in parallel, the delayed work can continue to execute in parallel
305 * there's a chance that this channel may not exist anymore
307 spin_lock(&server->srv_lock);
308 if (server->tcpStatus == CifsExiting) {
309 spin_unlock(&server->srv_lock);
310 mutex_unlock(&ses->session_mutex);
316 * Recheck after acquire mutex. If another thread is negotiating
317 * and the server never sends an answer the socket will be closed
318 * and tcpStatus set to reconnect.
320 if (server->tcpStatus == CifsNeedReconnect) {
321 spin_unlock(&server->srv_lock);
322 mutex_unlock(&ses->session_mutex);
330 spin_unlock(&server->srv_lock);
333 * need to prevent multiple threads trying to simultaneously
334 * reconnect the same SMB session
336 spin_lock(&ses->ses_lock);
337 spin_lock(&ses->chan_lock);
338 if (!cifs_chan_needs_reconnect(ses, server) &&
339 ses->ses_status == SES_GOOD) {
340 spin_unlock(&ses->chan_lock);
341 spin_unlock(&ses->ses_lock);
342 /* this means that we only need to tree connect */
343 if (tcon->need_reconnect)
344 goto skip_sess_setup;
346 mutex_unlock(&ses->session_mutex);
349 spin_unlock(&ses->chan_lock);
350 spin_unlock(&ses->ses_lock);
352 rc = cifs_negotiate_protocol(0, ses, server);
355 * if server stopped supporting multichannel
356 * and the first channel reconnected, disable all the others.
358 if (ses->chan_count > 1 &&
359 !(server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
360 rc = cifs_chan_skip_or_disable(ses, server,
363 mutex_unlock(&ses->session_mutex);
368 rc = cifs_setup_session(0, ses, server, ses->local_nls);
369 if ((rc == -EACCES) || (rc == -EKEYEXPIRED) || (rc == -EKEYREVOKED)) {
371 * Try alternate password for next reconnect (key rotation
372 * could be enabled on the server e.g.) if an alternate
373 * password is available and the current password is expired,
374 * but do not swap on non pwd related errors like host down
377 swap(ses->password2, ses->password);
380 if ((rc == -EACCES) && !tcon->retry) {
381 mutex_unlock(&ses->session_mutex);
385 mutex_unlock(&ses->session_mutex);
389 mutex_unlock(&ses->session_mutex);
394 if (!tcon->need_reconnect) {
395 mutex_unlock(&ses->session_mutex);
398 cifs_mark_open_files_invalid(tcon);
399 if (tcon->use_persistent)
400 tcon->need_reopen_files = true;
402 rc = cifs_tree_connect(0, tcon);
404 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
406 /* If sess reconnected but tcon didn't, something strange ... */
407 mutex_unlock(&ses->session_mutex);
408 cifs_dbg(VFS, "reconnect tcon failed rc = %d\n", rc);
412 spin_lock(&ses->ses_lock);
413 if (ses->flags & CIFS_SES_FLAG_SCALE_CHANNELS) {
414 spin_unlock(&ses->ses_lock);
415 mutex_unlock(&ses->session_mutex);
416 goto skip_add_channels;
418 ses->flags |= CIFS_SES_FLAG_SCALE_CHANNELS;
419 spin_unlock(&ses->ses_lock);
422 (server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL) &&
423 server->ops->query_server_interfaces) {
424 mutex_unlock(&ses->session_mutex);
427 * query server network interfaces, in case they change
430 rc = server->ops->query_server_interfaces(xid, tcon, false);
433 if (rc == -EOPNOTSUPP && ses->chan_count > 1) {
435 * some servers like Azure SMB server do not advertise
436 * that multichannel has been disabled with server
437 * capabilities, rather return STATUS_NOT_IMPLEMENTED.
438 * treat this as server not supporting multichannel
441 rc = cifs_chan_skip_or_disable(ses, server,
443 goto skip_add_channels;
445 cifs_dbg(FYI, "%s: failed to query server interfaces: %d\n",
448 if (ses->chan_max > ses->chan_count &&
450 !SERVER_IS_CHAN(server)) {
451 if (ses->chan_count == 1) {
452 cifs_server_dbg(VFS, "supports multichannel now\n");
453 queue_delayed_work(cifsiod_wq, &tcon->query_interfaces,
454 (SMB_INTERFACE_POLL_INTERVAL * HZ));
457 cifs_try_adding_channels(ses);
460 mutex_unlock(&ses->session_mutex);
464 spin_lock(&ses->ses_lock);
465 ses->flags &= ~CIFS_SES_FLAG_SCALE_CHANNELS;
466 spin_unlock(&ses->ses_lock);
468 if (smb2_command != SMB2_INTERNAL_CMD)
469 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
471 atomic_inc(&tconInfoReconnectCount);
474 * Check if handle based operation so we know whether we can continue
475 * or not without returning to caller to reset file handle.
478 * BB Is flush done by server on drop of tcp session? Should we special
479 * case it and skip above?
481 switch (smb2_command) {
486 case SMB2_QUERY_DIRECTORY:
487 case SMB2_CHANGE_NOTIFY:
488 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 ||
1229 server->dialect == SMB302_PROT_ID) &&
1230 (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1231 server->cipher_type = SMB2_ENCRYPTION_AES128_CCM;
1233 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
1234 (struct smb2_hdr *)rsp);
1236 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
1238 * ses->sectype = RawNTLMSSP;
1239 * but for time being this is our only auth choice so doesn't matter.
1240 * We just found a server which sets blob length to zero expecting raw.
1242 if (blob_length == 0) {
1243 cifs_dbg(FYI, "missing security blob on negprot\n");
1244 server->sec_ntlmssp = true;
1247 rc = cifs_enable_signing(server, ses->sign);
1251 rc = decode_negTokenInit(security_blob, blob_length, server);
1258 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1259 if (rsp->NegotiateContextCount)
1260 rc = smb311_decode_neg_context(rsp, server,
1263 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
1266 if (server->cipher_type && !rc) {
1267 if (!SERVER_IS_CHAN(server)) {
1268 rc = smb3_crypto_aead_allocate(server);
1270 /* For channels, just reuse the primary server crypto secmech. */
1271 server->secmech.enc = server->primary_server->secmech.enc;
1272 server->secmech.dec = server->primary_server->secmech.dec;
1276 free_rsp_buf(resp_buftype, rsp);
1280 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1283 struct validate_negotiate_info_req *pneg_inbuf;
1284 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
1286 u32 inbuflen; /* max of 4 dialects */
1287 struct TCP_Server_Info *server = tcon->ses->server;
1289 cifs_dbg(FYI, "validate negotiate\n");
1291 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1292 if (server->dialect == SMB311_PROT_ID)
1296 * validation ioctl must be signed, so no point sending this if we
1297 * can not sign it (ie are not known user). Even if signing is not
1298 * required (enabled but not negotiated), in those cases we selectively
1299 * sign just this, the first and only signed request on a connection.
1300 * Having validation of negotiate info helps reduce attack vectors.
1302 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1303 return 0; /* validation requires signing */
1305 if (tcon->ses->user_name == NULL) {
1306 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1307 return 0; /* validation requires signing */
1310 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1311 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1313 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1317 pneg_inbuf->Capabilities =
1318 cpu_to_le32(server->vals->req_capabilities);
1319 if (tcon->ses->chan_max > 1)
1320 pneg_inbuf->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
1322 memcpy(pneg_inbuf->Guid, server->client_guid,
1323 SMB2_CLIENT_GUID_SIZE);
1325 if (tcon->ses->sign)
1326 pneg_inbuf->SecurityMode =
1327 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1328 else if (global_secflags & CIFSSEC_MAY_SIGN)
1329 pneg_inbuf->SecurityMode =
1330 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1332 pneg_inbuf->SecurityMode = 0;
1335 if (strcmp(server->vals->version_string,
1336 SMB3ANY_VERSION_STRING) == 0) {
1337 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1338 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1339 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1340 pneg_inbuf->DialectCount = cpu_to_le16(3);
1341 /* SMB 2.1 not included so subtract one dialect from len */
1342 inbuflen = sizeof(*pneg_inbuf) -
1343 (sizeof(pneg_inbuf->Dialects[0]));
1344 } else if (strcmp(server->vals->version_string,
1345 SMBDEFAULT_VERSION_STRING) == 0) {
1346 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1347 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1348 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1349 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1350 pneg_inbuf->DialectCount = cpu_to_le16(4);
1351 /* structure is big enough for 4 dialects */
1352 inbuflen = sizeof(*pneg_inbuf);
1354 /* otherwise specific dialect was requested */
1355 pneg_inbuf->Dialects[0] =
1356 cpu_to_le16(server->vals->protocol_id);
1357 pneg_inbuf->DialectCount = cpu_to_le16(1);
1358 /* structure is big enough for 4 dialects, sending only 1 */
1359 inbuflen = sizeof(*pneg_inbuf) -
1360 sizeof(pneg_inbuf->Dialects[0]) * 3;
1363 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1364 FSCTL_VALIDATE_NEGOTIATE_INFO,
1365 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1366 (char **)&pneg_rsp, &rsplen);
1367 if (rc == -EOPNOTSUPP) {
1369 * Old Windows versions or Netapp SMB server can return
1370 * not supported error. Client should accept it.
1372 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1374 goto out_free_inbuf;
1375 } else if (rc != 0) {
1376 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1379 goto out_free_inbuf;
1383 if (rsplen != sizeof(*pneg_rsp)) {
1384 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1387 /* relax check since Mac returns max bufsize allowed on ioctl */
1388 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1392 /* check validate negotiate info response matches what we got earlier */
1393 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1396 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1399 /* do not validate server guid because not saved at negprot time yet */
1401 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1402 SMB2_LARGE_FILES) != server->capabilities)
1405 /* validate negotiate successful */
1407 cifs_dbg(FYI, "validate negotiate info successful\n");
1411 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1420 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1422 switch (requested) {
1429 if (server->sec_ntlmssp &&
1430 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1432 if ((server->sec_kerberos || server->sec_mskerberos) &&
1433 (global_secflags & CIFSSEC_MAY_KRB5))
1441 struct SMB2_sess_data {
1443 struct cifs_ses *ses;
1444 struct TCP_Server_Info *server;
1445 struct nls_table *nls_cp;
1446 void (*func)(struct SMB2_sess_data *);
1448 u64 previous_session;
1450 /* we will send the SMB in three pieces:
1451 * a fixed length beginning part, an optional
1452 * SPNEGO blob (which can be zero length), and a
1453 * last part which will include the strings
1454 * and rest of bcc area. This allows us to avoid
1455 * a large buffer 17K allocation
1462 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1465 struct cifs_ses *ses = sess_data->ses;
1466 struct TCP_Server_Info *server = sess_data->server;
1467 struct smb2_sess_setup_req *req;
1468 unsigned int total_len;
1469 bool is_binding = false;
1471 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
1477 spin_lock(&ses->ses_lock);
1478 is_binding = (ses->ses_status == SES_GOOD);
1479 spin_unlock(&ses->ses_lock);
1482 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1483 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1484 req->PreviousSessionId = 0;
1485 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1486 cifs_dbg(FYI, "Binding to sess id: %llx\n", ses->Suid);
1488 /* First session, not a reauthenticate */
1489 req->hdr.SessionId = 0;
1491 * if reconnect, we need to send previous sess id
1494 req->PreviousSessionId = cpu_to_le64(sess_data->previous_session);
1495 req->Flags = 0; /* MBZ */
1496 cifs_dbg(FYI, "Fresh session. Previous: %llx\n",
1497 sess_data->previous_session);
1500 /* enough to enable echos and oplocks and one max size write */
1501 if (server->credits >= server->max_credits)
1502 req->hdr.CreditRequest = cpu_to_le16(0);
1504 req->hdr.CreditRequest = cpu_to_le16(
1505 min_t(int, server->max_credits -
1506 server->credits, 130));
1508 /* only one of SMB2 signing flags may be set in SMB2 request */
1510 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1511 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1512 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1514 req->SecurityMode = 0;
1516 #ifdef CONFIG_CIFS_DFS_UPCALL
1517 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1519 req->Capabilities = 0;
1520 #endif /* DFS_UPCALL */
1522 req->Channel = 0; /* MBZ */
1524 sess_data->iov[0].iov_base = (char *)req;
1526 sess_data->iov[0].iov_len = total_len - 1;
1528 * This variable will be used to clear the buffer
1529 * allocated above in case of any error in the calling function.
1531 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1537 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1539 struct kvec *iov = sess_data->iov;
1541 /* iov[1] is already freed by caller */
1542 if (sess_data->buf0_type != CIFS_NO_BUFFER && iov[0].iov_base)
1543 memzero_explicit(iov[0].iov_base, iov[0].iov_len);
1545 free_rsp_buf(sess_data->buf0_type, iov[0].iov_base);
1546 sess_data->buf0_type = CIFS_NO_BUFFER;
1550 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1553 struct smb_rqst rqst;
1554 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1555 struct kvec rsp_iov = { NULL, 0 };
1557 /* Testing shows that buffer offset must be at location of Buffer[0] */
1558 req->SecurityBufferOffset =
1559 cpu_to_le16(sizeof(struct smb2_sess_setup_req));
1560 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1562 memset(&rqst, 0, sizeof(struct smb_rqst));
1563 rqst.rq_iov = sess_data->iov;
1566 /* BB add code to build os and lm fields */
1567 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1570 &sess_data->buf0_type,
1571 CIFS_LOG_ERROR | CIFS_SESS_OP, &rsp_iov);
1572 cifs_small_buf_release(sess_data->iov[0].iov_base);
1574 sess_data->ses->expired_pwd = false;
1575 else if ((rc == -EACCES) || (rc == -EKEYEXPIRED) || (rc == -EKEYREVOKED)) {
1576 if (sess_data->ses->expired_pwd == false)
1577 trace_smb3_key_expired(sess_data->server->hostname,
1578 sess_data->ses->user_name,
1579 sess_data->server->conn_id,
1580 &sess_data->server->dstaddr, rc);
1581 sess_data->ses->expired_pwd = true;
1584 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1590 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1593 struct cifs_ses *ses = sess_data->ses;
1594 struct TCP_Server_Info *server = sess_data->server;
1596 cifs_server_lock(server);
1597 if (server->ops->generate_signingkey) {
1598 rc = server->ops->generate_signingkey(ses, server);
1601 "SMB3 session key generation failed\n");
1602 cifs_server_unlock(server);
1606 if (!server->session_estab) {
1607 server->sequence_number = 0x2;
1608 server->session_estab = true;
1610 cifs_server_unlock(server);
1612 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1616 #ifdef CONFIG_CIFS_UPCALL
1618 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1621 struct cifs_ses *ses = sess_data->ses;
1622 struct TCP_Server_Info *server = sess_data->server;
1623 struct cifs_spnego_msg *msg;
1624 struct key *spnego_key = NULL;
1625 struct smb2_sess_setup_rsp *rsp = NULL;
1626 bool is_binding = false;
1628 rc = SMB2_sess_alloc_buffer(sess_data);
1632 spnego_key = cifs_get_spnego_key(ses, server);
1633 if (IS_ERR(spnego_key)) {
1634 rc = PTR_ERR(spnego_key);
1636 cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
1641 msg = spnego_key->payload.data[0];
1643 * check version field to make sure that cifs.upcall is
1644 * sending us a response in an expected form
1646 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1647 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1648 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1650 goto out_put_spnego_key;
1653 spin_lock(&ses->ses_lock);
1654 is_binding = (ses->ses_status == SES_GOOD);
1655 spin_unlock(&ses->ses_lock);
1657 /* keep session key if binding */
1659 kfree_sensitive(ses->auth_key.response);
1660 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1662 if (!ses->auth_key.response) {
1663 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1666 goto out_put_spnego_key;
1668 ses->auth_key.len = msg->sesskey_len;
1671 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1672 sess_data->iov[1].iov_len = msg->secblob_len;
1674 rc = SMB2_sess_sendreceive(sess_data);
1676 goto out_put_spnego_key;
1678 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1679 /* keep session id and flags if binding */
1681 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1682 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1685 rc = SMB2_sess_establish_session(sess_data);
1687 key_invalidate(spnego_key);
1688 key_put(spnego_key);
1690 kfree_sensitive(ses->auth_key.response);
1691 ses->auth_key.response = NULL;
1692 ses->auth_key.len = 0;
1695 sess_data->result = rc;
1696 sess_data->func = NULL;
1697 SMB2_sess_free_buffer(sess_data);
1701 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1703 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1704 sess_data->result = -EOPNOTSUPP;
1705 sess_data->func = NULL;
1710 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1713 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1716 struct cifs_ses *ses = sess_data->ses;
1717 struct TCP_Server_Info *server = sess_data->server;
1718 struct smb2_sess_setup_rsp *rsp = NULL;
1719 unsigned char *ntlmssp_blob = NULL;
1720 bool use_spnego = false; /* else use raw ntlmssp */
1721 u16 blob_length = 0;
1722 bool is_binding = false;
1725 * If memory allocation is successful, caller of this function
1728 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1729 if (!ses->ntlmssp) {
1733 ses->ntlmssp->sesskey_per_smbsess = true;
1735 rc = SMB2_sess_alloc_buffer(sess_data);
1739 rc = build_ntlmssp_smb3_negotiate_blob(&ntlmssp_blob,
1740 &blob_length, ses, server,
1746 /* BB eventually need to add this */
1747 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1751 sess_data->iov[1].iov_base = ntlmssp_blob;
1752 sess_data->iov[1].iov_len = blob_length;
1754 rc = SMB2_sess_sendreceive(sess_data);
1755 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1757 /* If true, rc here is expected and not an error */
1758 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1759 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1765 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1766 le16_to_cpu(rsp->SecurityBufferOffset)) {
1767 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1768 le16_to_cpu(rsp->SecurityBufferOffset));
1772 rc = decode_ntlmssp_challenge(rsp->Buffer,
1773 le16_to_cpu(rsp->SecurityBufferLength), ses);
1777 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1779 spin_lock(&ses->ses_lock);
1780 is_binding = (ses->ses_status == SES_GOOD);
1781 spin_unlock(&ses->ses_lock);
1783 /* keep existing ses id and flags if binding */
1785 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1786 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1790 kfree_sensitive(ntlmssp_blob);
1791 SMB2_sess_free_buffer(sess_data);
1793 sess_data->result = 0;
1794 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1798 kfree_sensitive(ses->ntlmssp);
1799 ses->ntlmssp = NULL;
1800 sess_data->result = rc;
1801 sess_data->func = NULL;
1805 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1808 struct cifs_ses *ses = sess_data->ses;
1809 struct TCP_Server_Info *server = sess_data->server;
1810 struct smb2_sess_setup_req *req;
1811 struct smb2_sess_setup_rsp *rsp = NULL;
1812 unsigned char *ntlmssp_blob = NULL;
1813 bool use_spnego = false; /* else use raw ntlmssp */
1814 u16 blob_length = 0;
1815 bool is_binding = false;
1817 rc = SMB2_sess_alloc_buffer(sess_data);
1821 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1822 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1824 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length,
1828 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1833 /* BB eventually need to add this */
1834 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1838 sess_data->iov[1].iov_base = ntlmssp_blob;
1839 sess_data->iov[1].iov_len = blob_length;
1841 rc = SMB2_sess_sendreceive(sess_data);
1845 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1847 spin_lock(&ses->ses_lock);
1848 is_binding = (ses->ses_status == SES_GOOD);
1849 spin_unlock(&ses->ses_lock);
1851 /* keep existing ses id and flags if binding */
1853 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1854 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1857 rc = SMB2_sess_establish_session(sess_data);
1858 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1859 if (ses->server->dialect < SMB30_PROT_ID) {
1860 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1862 * The session id is opaque in terms of endianness, so we can't
1863 * print it as a long long. we dump it as we got it on the wire
1865 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1867 cifs_dbg(VFS, "Session Key %*ph\n",
1868 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1869 cifs_dbg(VFS, "Signing Key %*ph\n",
1870 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1874 kfree_sensitive(ntlmssp_blob);
1875 SMB2_sess_free_buffer(sess_data);
1876 kfree_sensitive(ses->ntlmssp);
1877 ses->ntlmssp = NULL;
1878 sess_data->result = rc;
1879 sess_data->func = NULL;
1883 SMB2_select_sec(struct SMB2_sess_data *sess_data)
1886 struct cifs_ses *ses = sess_data->ses;
1887 struct TCP_Server_Info *server = sess_data->server;
1889 type = smb2_select_sectype(server, ses->sectype);
1890 cifs_dbg(FYI, "sess setup type %d\n", type);
1891 if (type == Unspecified) {
1892 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1898 sess_data->func = SMB2_auth_kerberos;
1901 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1904 cifs_dbg(VFS, "secType %d not supported!\n", type);
1912 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1913 struct TCP_Server_Info *server,
1914 const struct nls_table *nls_cp)
1917 struct SMB2_sess_data *sess_data;
1919 cifs_dbg(FYI, "Session Setup\n");
1922 WARN(1, "%s: server is NULL!\n", __func__);
1926 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1930 sess_data->xid = xid;
1931 sess_data->ses = ses;
1932 sess_data->server = server;
1933 sess_data->buf0_type = CIFS_NO_BUFFER;
1934 sess_data->nls_cp = (struct nls_table *) nls_cp;
1935 sess_data->previous_session = ses->Suid;
1937 rc = SMB2_select_sec(sess_data);
1942 * Initialize the session hash with the server one.
1944 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
1945 SMB2_PREAUTH_HASH_SIZE);
1947 while (sess_data->func)
1948 sess_data->func(sess_data);
1950 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1951 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1952 rc = sess_data->result;
1954 kfree_sensitive(sess_data);
1959 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1961 struct smb_rqst rqst;
1962 struct smb2_logoff_req *req; /* response is also trivial struct */
1964 struct TCP_Server_Info *server;
1966 unsigned int total_len;
1968 struct kvec rsp_iov;
1971 cifs_dbg(FYI, "disconnect session %p\n", ses);
1973 if (ses && (ses->server))
1974 server = ses->server;
1978 /* no need to send SMB logoff if uid already closed due to reconnect */
1979 spin_lock(&ses->chan_lock);
1980 if (CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
1981 spin_unlock(&ses->chan_lock);
1982 goto smb2_session_already_dead;
1984 spin_unlock(&ses->chan_lock);
1986 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
1987 (void **) &req, &total_len);
1991 /* since no tcon, smb2_init can not do this, so do here */
1992 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1994 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1995 flags |= CIFS_TRANSFORM_REQ;
1996 else if (server->sign)
1997 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1999 flags |= CIFS_NO_RSP_BUF;
2001 iov[0].iov_base = (char *)req;
2002 iov[0].iov_len = total_len;
2004 memset(&rqst, 0, sizeof(struct smb_rqst));
2008 rc = cifs_send_recv(xid, ses, ses->server,
2009 &rqst, &resp_buf_type, flags, &rsp_iov);
2010 cifs_small_buf_release(req);
2012 * No tcon so can't do
2013 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
2016 smb2_session_already_dead:
2020 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
2022 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
2025 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
2027 /* These are similar values to what Windows uses */
2028 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
2030 tcon->max_chunks = 256;
2031 tcon->max_bytes_chunk = 1048576;
2032 tcon->max_bytes_copy = 16777216;
2036 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
2037 struct cifs_tcon *tcon, const struct nls_table *cp)
2039 struct smb_rqst rqst;
2040 struct smb2_tree_connect_req *req;
2041 struct smb2_tree_connect_rsp *rsp = NULL;
2043 struct kvec rsp_iov = { NULL, 0 };
2047 __le16 *unc_path = NULL;
2049 unsigned int total_len;
2050 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2052 cifs_dbg(FYI, "TCON\n");
2054 if (!server || !tree)
2057 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
2058 if (unc_path == NULL)
2061 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp);
2062 if (unc_path_len <= 0) {
2068 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
2070 atomic_set(&tcon->num_remote_opens, 0);
2071 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
2072 (void **) &req, &total_len);
2078 if (smb3_encryption_required(tcon))
2079 flags |= CIFS_TRANSFORM_REQ;
2081 iov[0].iov_base = (char *)req;
2083 iov[0].iov_len = total_len - 1;
2085 /* Testing shows that buffer offset must be at location of Buffer[0] */
2086 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req));
2087 req->PathLength = cpu_to_le16(unc_path_len);
2088 iov[1].iov_base = unc_path;
2089 iov[1].iov_len = unc_path_len;
2092 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
2093 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
2094 * (Samba servers don't always set the flag so also check if null user)
2096 if ((server->dialect == SMB311_PROT_ID) &&
2097 !smb3_encryption_required(tcon) &&
2098 !(ses->session_flags &
2099 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
2100 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
2101 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
2103 memset(&rqst, 0, sizeof(struct smb_rqst));
2107 /* Need 64 for max size write so ask for more in case not there yet */
2108 if (server->credits >= server->max_credits)
2109 req->hdr.CreditRequest = cpu_to_le16(0);
2111 req->hdr.CreditRequest = cpu_to_le16(
2112 min_t(int, server->max_credits -
2113 server->credits, 64));
2115 rc = cifs_send_recv(xid, ses, server,
2116 &rqst, &resp_buftype, flags, &rsp_iov);
2117 cifs_small_buf_release(req);
2118 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
2119 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
2120 if ((rc != 0) || (rsp == NULL)) {
2121 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
2122 tcon->need_reconnect = true;
2123 goto tcon_error_exit;
2126 switch (rsp->ShareType) {
2127 case SMB2_SHARE_TYPE_DISK:
2128 cifs_dbg(FYI, "connection to disk share\n");
2130 case SMB2_SHARE_TYPE_PIPE:
2132 cifs_dbg(FYI, "connection to pipe share\n");
2134 case SMB2_SHARE_TYPE_PRINT:
2136 cifs_dbg(FYI, "connection to printer\n");
2139 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
2141 goto tcon_error_exit;
2144 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
2145 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
2146 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
2147 tcon->tid = le32_to_cpu(rsp->hdr.Id.SyncId.TreeId);
2148 strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name));
2150 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
2151 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
2152 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
2155 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
2156 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
2158 init_copy_chunk_defaults(tcon);
2159 if (server->ops->validate_negotiate)
2160 rc = server->ops->validate_negotiate(xid, tcon);
2161 if (rc == 0) /* See MS-SMB2 2.2.10 and 3.2.5.5 */
2162 if (tcon->share_flags & SMB2_SHAREFLAG_ISOLATED_TRANSPORT)
2163 server->nosharesock = true;
2166 free_rsp_buf(resp_buftype, rsp);
2171 if (rsp && rsp->hdr.Status == STATUS_BAD_NETWORK_NAME)
2172 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
2177 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
2179 struct smb_rqst rqst;
2180 struct smb2_tree_disconnect_req *req; /* response is trivial */
2182 struct cifs_ses *ses = tcon->ses;
2183 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2185 unsigned int total_len;
2187 struct kvec rsp_iov;
2190 cifs_dbg(FYI, "Tree Disconnect\n");
2192 if (!ses || !(ses->server))
2195 trace_smb3_tdis_enter(xid, tcon->tid, ses->Suid, tcon->tree_name);
2196 spin_lock(&ses->chan_lock);
2197 if ((tcon->need_reconnect) ||
2198 (CIFS_ALL_CHANS_NEED_RECONNECT(tcon->ses))) {
2199 spin_unlock(&ses->chan_lock);
2202 spin_unlock(&ses->chan_lock);
2204 invalidate_all_cached_dirs(tcon);
2206 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, server,
2212 if (smb3_encryption_required(tcon))
2213 flags |= CIFS_TRANSFORM_REQ;
2215 flags |= CIFS_NO_RSP_BUF;
2217 iov[0].iov_base = (char *)req;
2218 iov[0].iov_len = total_len;
2220 memset(&rqst, 0, sizeof(struct smb_rqst));
2224 rc = cifs_send_recv(xid, ses, server,
2225 &rqst, &resp_buf_type, flags, &rsp_iov);
2226 cifs_small_buf_release(req);
2228 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
2229 trace_smb3_tdis_err(xid, tcon->tid, ses->Suid, rc);
2231 trace_smb3_tdis_done(xid, tcon->tid, ses->Suid);
2237 static struct create_durable *
2238 create_durable_buf(void)
2240 struct create_durable *buf;
2242 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2246 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2247 (struct create_durable, Data));
2248 buf->ccontext.DataLength = cpu_to_le32(16);
2249 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2250 (struct create_durable, Name));
2251 buf->ccontext.NameLength = cpu_to_le16(4);
2252 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
2260 static struct create_durable *
2261 create_reconnect_durable_buf(struct cifs_fid *fid)
2263 struct create_durable *buf;
2265 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2269 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2270 (struct create_durable, Data));
2271 buf->ccontext.DataLength = cpu_to_le32(16);
2272 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2273 (struct create_durable, Name));
2274 buf->ccontext.NameLength = cpu_to_le16(4);
2275 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
2276 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
2277 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
2286 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
2288 struct create_disk_id_rsp *pdisk_id = (struct create_disk_id_rsp *)cc;
2290 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
2291 pdisk_id->DiskFileId, pdisk_id->VolumeId);
2292 buf->IndexNumber = pdisk_id->DiskFileId;
2296 parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
2297 struct create_posix_rsp *posix)
2300 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
2301 u8 *end = beg + le32_to_cpu(cc->DataLength);
2304 memset(posix, 0, sizeof(*posix));
2306 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
2307 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
2308 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
2311 sid_len = posix_info_sid_size(sid, end);
2313 cifs_dbg(VFS, "bad owner sid in posix create response\n");
2316 memcpy(&posix->owner, sid, sid_len);
2318 sid = sid + sid_len;
2319 sid_len = posix_info_sid_size(sid, end);
2321 cifs_dbg(VFS, "bad group sid in posix create response\n");
2324 memcpy(&posix->group, sid, sid_len);
2326 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
2327 posix->nlink, posix->mode, posix->reparse_tag);
2330 int smb2_parse_contexts(struct TCP_Server_Info *server,
2331 struct kvec *rsp_iov,
2332 unsigned int *epoch,
2333 char *lease_key, __u8 *oplock,
2334 struct smb2_file_all_info *buf,
2335 struct create_posix_rsp *posix)
2337 struct smb2_create_rsp *rsp = rsp_iov->iov_base;
2338 struct create_context *cc;
2339 size_t rem, off, len;
2343 static const char smb3_create_tag_posix[] = {
2344 0x93, 0xAD, 0x25, 0x50, 0x9C,
2345 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
2346 0xDE, 0x96, 0x8B, 0xCD, 0x7C
2351 off = le32_to_cpu(rsp->CreateContextsOffset);
2352 rem = le32_to_cpu(rsp->CreateContextsLength);
2353 if (check_add_overflow(off, rem, &len) || len > rsp_iov->iov_len)
2355 cc = (struct create_context *)((u8 *)rsp + off);
2357 /* Initialize inode number to 0 in case no valid data in qfid context */
2359 buf->IndexNumber = 0;
2361 while (rem >= sizeof(*cc)) {
2362 doff = le16_to_cpu(cc->DataOffset);
2363 dlen = le32_to_cpu(cc->DataLength);
2364 if (check_add_overflow(doff, dlen, &len) || len > rem)
2367 noff = le16_to_cpu(cc->NameOffset);
2368 nlen = le16_to_cpu(cc->NameLength);
2369 if (noff + nlen > doff)
2372 name = (char *)cc + noff;
2375 if (!strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4)) {
2376 *oplock = server->ops->parse_lease_buf(cc, epoch,
2379 !strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4)) {
2380 parse_query_id_ctxt(cc, buf);
2384 if (posix && !memcmp(name, smb3_create_tag_posix, 16))
2385 parse_posix_ctxt(cc, buf, posix);
2388 cifs_dbg(FYI, "%s: unhandled context (nlen=%zu dlen=%zu)\n",
2389 __func__, nlen, dlen);
2390 if (IS_ENABLED(CONFIG_CIFS_DEBUG2))
2391 cifs_dump_mem("context data: ", cc, dlen);
2395 off = le32_to_cpu(cc->Next);
2398 if (check_sub_overflow(rem, off, &rem))
2400 cc = (struct create_context *)((u8 *)cc + off);
2403 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2404 *oplock = rsp->OplockLevel;
2410 add_lease_context(struct TCP_Server_Info *server,
2411 struct smb2_create_req *req,
2413 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
2415 unsigned int num = *num_iovec;
2417 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
2418 if (iov[num].iov_base == NULL)
2420 iov[num].iov_len = server->vals->create_lease_size;
2421 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2422 *num_iovec = num + 1;
2426 static struct create_durable_v2 *
2427 create_durable_v2_buf(struct cifs_open_parms *oparms)
2429 struct cifs_fid *pfid = oparms->fid;
2430 struct create_durable_v2 *buf;
2432 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2436 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2437 (struct create_durable_v2, dcontext));
2438 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2439 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2440 (struct create_durable_v2, Name));
2441 buf->ccontext.NameLength = cpu_to_le16(4);
2444 * NB: Handle timeout defaults to 0, which allows server to choose
2445 * (most servers default to 120 seconds) and most clients default to 0.
2446 * This can be overridden at mount ("handletimeout=") if the user wants
2447 * a different persistent (or resilient) handle timeout for all opens
2448 * on a particular SMB3 mount.
2450 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2451 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2453 /* for replay, we should not overwrite the existing create guid */
2454 if (!oparms->replay) {
2455 generate_random_uuid(buf->dcontext.CreateGuid);
2456 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2458 memcpy(buf->dcontext.CreateGuid, pfid->create_guid, 16);
2460 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2468 static struct create_durable_handle_reconnect_v2 *
2469 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2471 struct create_durable_handle_reconnect_v2 *buf;
2473 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2478 buf->ccontext.DataOffset =
2479 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2481 buf->ccontext.DataLength =
2482 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2483 buf->ccontext.NameOffset =
2484 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2486 buf->ccontext.NameLength = cpu_to_le16(4);
2488 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2489 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2490 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2491 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2493 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2502 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2503 struct cifs_open_parms *oparms)
2505 unsigned int num = *num_iovec;
2507 iov[num].iov_base = create_durable_v2_buf(oparms);
2508 if (iov[num].iov_base == NULL)
2510 iov[num].iov_len = sizeof(struct create_durable_v2);
2511 *num_iovec = num + 1;
2516 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2517 struct cifs_open_parms *oparms)
2519 unsigned int num = *num_iovec;
2521 /* indicate that we don't need to relock the file */
2522 oparms->reconnect = false;
2524 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2525 if (iov[num].iov_base == NULL)
2527 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2528 *num_iovec = num + 1;
2533 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2534 struct cifs_open_parms *oparms, bool use_persistent)
2536 unsigned int num = *num_iovec;
2538 if (use_persistent) {
2539 if (oparms->reconnect)
2540 return add_durable_reconnect_v2_context(iov, num_iovec,
2543 return add_durable_v2_context(iov, num_iovec, oparms);
2546 if (oparms->reconnect) {
2547 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2548 /* indicate that we don't need to relock the file */
2549 oparms->reconnect = false;
2551 iov[num].iov_base = create_durable_buf();
2552 if (iov[num].iov_base == NULL)
2554 iov[num].iov_len = sizeof(struct create_durable);
2555 *num_iovec = num + 1;
2559 /* See MS-SMB2 2.2.13.2.7 */
2560 static struct crt_twarp_ctxt *
2561 create_twarp_buf(__u64 timewarp)
2563 struct crt_twarp_ctxt *buf;
2565 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2569 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2570 (struct crt_twarp_ctxt, Timestamp));
2571 buf->ccontext.DataLength = cpu_to_le32(8);
2572 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2573 (struct crt_twarp_ctxt, Name));
2574 buf->ccontext.NameLength = cpu_to_le16(4);
2575 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2580 buf->Timestamp = cpu_to_le64(timewarp);
2584 /* See MS-SMB2 2.2.13.2.7 */
2586 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2588 unsigned int num = *num_iovec;
2590 iov[num].iov_base = create_twarp_buf(timewarp);
2591 if (iov[num].iov_base == NULL)
2593 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2594 *num_iovec = num + 1;
2598 /* See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
2599 static void setup_owner_group_sids(char *buf)
2601 struct owner_group_sids *sids = (struct owner_group_sids *)buf;
2603 /* Populate the user ownership fields S-1-5-88-1 */
2604 sids->owner.Revision = 1;
2605 sids->owner.NumAuth = 3;
2606 sids->owner.Authority[5] = 5;
2607 sids->owner.SubAuthorities[0] = cpu_to_le32(88);
2608 sids->owner.SubAuthorities[1] = cpu_to_le32(1);
2609 sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val);
2611 /* Populate the group ownership fields S-1-5-88-2 */
2612 sids->group.Revision = 1;
2613 sids->group.NumAuth = 3;
2614 sids->group.Authority[5] = 5;
2615 sids->group.SubAuthorities[0] = cpu_to_le32(88);
2616 sids->group.SubAuthorities[1] = cpu_to_le32(2);
2617 sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val);
2619 cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val);
2622 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2623 static struct crt_sd_ctxt *
2624 create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
2626 struct crt_sd_ctxt *buf;
2628 unsigned int acelen, acl_size, ace_count;
2629 unsigned int owner_offset = 0;
2630 unsigned int group_offset = 0;
2631 struct smb3_acl acl = {};
2633 *len = round_up(sizeof(struct crt_sd_ctxt) + (sizeof(struct smb_ace) * 4), 8);
2636 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
2637 *len += sizeof(struct owner_group_sids);
2640 buf = kzalloc(*len, GFP_KERNEL);
2644 ptr = (__u8 *)&buf[1];
2646 /* offset fields are from beginning of security descriptor not of create context */
2647 owner_offset = ptr - (__u8 *)&buf->sd;
2648 buf->sd.OffsetOwner = cpu_to_le32(owner_offset);
2649 group_offset = owner_offset + offsetof(struct owner_group_sids, group);
2650 buf->sd.OffsetGroup = cpu_to_le32(group_offset);
2652 setup_owner_group_sids(ptr);
2653 ptr += sizeof(struct owner_group_sids);
2655 buf->sd.OffsetOwner = 0;
2656 buf->sd.OffsetGroup = 0;
2659 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, sd));
2660 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name));
2661 buf->ccontext.NameLength = cpu_to_le16(4);
2662 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2667 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2670 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2671 * and "DP" ie the DACL is present
2673 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2675 /* offset owner, group and Sbz1 and SACL are all zero */
2676 buf->sd.OffsetDacl = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2677 /* Ship the ACL for now. we will copy it into buf later. */
2679 ptr += sizeof(struct smb3_acl);
2681 /* create one ACE to hold the mode embedded in reserved special SID */
2682 acelen = setup_special_mode_ACE((struct smb_ace *)ptr, false, (__u64)mode);
2684 acl_size = acelen + sizeof(struct smb3_acl);
2688 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
2689 acelen = setup_special_user_owner_ACE((struct smb_ace *)ptr);
2695 /* and one more ACE to allow access for authenticated users */
2696 acelen = setup_authusers_ACE((struct smb_ace *)ptr);
2701 acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2702 acl.AclSize = cpu_to_le16(acl_size);
2703 acl.AceCount = cpu_to_le16(ace_count);
2704 /* acl.Sbz1 and Sbz2 MBZ so are not set here, but initialized above */
2705 memcpy(aclptr, &acl, sizeof(struct smb3_acl));
2707 buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2708 *len = round_up((unsigned int)(ptr - (__u8 *)buf), 8);
2714 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner)
2716 unsigned int num = *num_iovec;
2717 unsigned int len = 0;
2719 iov[num].iov_base = create_sd_buf(mode, set_owner, &len);
2720 if (iov[num].iov_base == NULL)
2722 iov[num].iov_len = len;
2723 *num_iovec = num + 1;
2727 static struct crt_query_id_ctxt *
2728 create_query_id_buf(void)
2730 struct crt_query_id_ctxt *buf;
2732 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2736 buf->ccontext.DataOffset = cpu_to_le16(0);
2737 buf->ccontext.DataLength = cpu_to_le32(0);
2738 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2739 (struct crt_query_id_ctxt, Name));
2740 buf->ccontext.NameLength = cpu_to_le16(4);
2741 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2749 /* See MS-SMB2 2.2.13.2.9 */
2751 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2753 unsigned int num = *num_iovec;
2755 iov[num].iov_base = create_query_id_buf();
2756 if (iov[num].iov_base == NULL)
2758 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2759 *num_iovec = num + 1;
2763 static void add_ea_context(struct cifs_open_parms *oparms,
2764 struct kvec *rq_iov, unsigned int *num_iovs)
2766 struct kvec *iov = oparms->ea_cctx;
2768 if (iov && iov->iov_base && iov->iov_len) {
2769 rq_iov[(*num_iovs)++] = *iov;
2770 memset(iov, 0, sizeof(*iov));
2775 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2776 const char *treename, const __le16 *path)
2778 int treename_len, path_len;
2779 struct nls_table *cp;
2780 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2785 treename_len = strlen(treename);
2786 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2792 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2794 /* make room for one path separator only if @path isn't empty */
2795 *out_len = treename_len + (path[0] ? 1 : 0) + path_len;
2798 * final path needs to be 8-byte aligned as specified in
2799 * MS-SMB2 2.2.13 SMB2 CREATE Request.
2801 *out_size = round_up(*out_len * sizeof(__le16), 8);
2802 *out_path = kzalloc(*out_size + sizeof(__le16) /* null */, GFP_KERNEL);
2806 cp = load_nls_default();
2807 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2809 /* Do not append the separator if the path is empty */
2810 if (path[0] != cpu_to_le16(0x0000)) {
2811 UniStrcat((wchar_t *)*out_path, (wchar_t *)sep);
2812 UniStrcat((wchar_t *)*out_path, (wchar_t *)path);
2820 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2821 umode_t mode, struct cifs_tcon *tcon,
2822 const char *full_path,
2823 struct cifs_sb_info *cifs_sb)
2825 struct smb_rqst rqst;
2826 struct smb2_create_req *req;
2827 struct smb2_create_rsp *rsp = NULL;
2828 struct cifs_ses *ses = tcon->ses;
2829 struct kvec iov[3]; /* make sure at least one for each open context */
2830 struct kvec rsp_iov = {NULL, 0};
2833 __le16 *copy_path = NULL;
2836 unsigned int n_iov = 2;
2837 __u32 file_attributes = 0;
2838 char *pc_buf = NULL;
2840 unsigned int total_len;
2841 __le16 *utf16_path = NULL;
2842 struct TCP_Server_Info *server;
2843 int retries = 0, cur_sleep = 1;
2846 /* reinitialize for possible replay */
2849 server = cifs_pick_channel(ses);
2851 cifs_dbg(FYI, "mkdir\n");
2853 /* resource #1: path allocation */
2854 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2858 if (!ses || !server) {
2863 /* resource #2: request */
2864 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2865 (void **) &req, &total_len);
2870 if (smb3_encryption_required(tcon))
2871 flags |= CIFS_TRANSFORM_REQ;
2873 req->ImpersonationLevel = IL_IMPERSONATION;
2874 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2875 /* File attributes ignored on open (used in create though) */
2876 req->FileAttributes = cpu_to_le32(file_attributes);
2877 req->ShareAccess = FILE_SHARE_ALL_LE;
2878 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2879 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2881 iov[0].iov_base = (char *)req;
2882 /* -1 since last byte is buf[0] which is sent below (path) */
2883 iov[0].iov_len = total_len - 1;
2885 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2887 /* [MS-SMB2] 2.2.13 NameOffset:
2888 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2889 * the SMB2 header, the file name includes a prefix that will
2890 * be processed during DFS name normalization as specified in
2891 * section 3.3.5.9. Otherwise, the file name is relative to
2892 * the share that is identified by the TreeId in the SMB2
2895 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2898 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2899 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2901 tcon->tree_name, utf16_path);
2905 req->NameLength = cpu_to_le16(name_len * 2);
2906 uni_path_len = copy_size;
2907 /* free before overwriting resource */
2909 utf16_path = copy_path;
2911 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2912 /* MUST set path len (NameLength) to 0 opening root of share */
2913 req->NameLength = cpu_to_le16(uni_path_len - 2);
2914 if (uni_path_len % 8 != 0) {
2915 copy_size = roundup(uni_path_len, 8);
2916 copy_path = kzalloc(copy_size, GFP_KERNEL);
2921 memcpy((char *)copy_path, (const char *)utf16_path,
2923 uni_path_len = copy_size;
2924 /* free before overwriting resource */
2926 utf16_path = copy_path;
2930 iov[1].iov_len = uni_path_len;
2931 iov[1].iov_base = utf16_path;
2932 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2934 if (tcon->posix_extensions) {
2935 /* resource #3: posix buf */
2936 rc = add_posix_context(iov, &n_iov, mode);
2939 req->CreateContextsOffset = cpu_to_le32(
2940 sizeof(struct smb2_create_req) +
2942 pc_buf = iov[n_iov-1].iov_base;
2946 memset(&rqst, 0, sizeof(struct smb_rqst));
2948 rqst.rq_nvec = n_iov;
2950 /* no need to inc num_remote_opens because we close it just below */
2951 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, full_path, CREATE_NOT_FILE,
2952 FILE_WRITE_ATTRIBUTES);
2955 smb2_set_replay(server, &rqst);
2957 /* resource #4: response buffer */
2958 rc = cifs_send_recv(xid, ses, server,
2959 &rqst, &resp_buftype, flags, &rsp_iov);
2961 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2962 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2964 FILE_WRITE_ATTRIBUTES, rc);
2965 goto err_free_rsp_buf;
2969 * Although unlikely to be possible for rsp to be null and rc not set,
2970 * adding check below is slightly safer long term (and quiets Coverity
2973 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2980 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
2981 CREATE_NOT_FILE, FILE_WRITE_ATTRIBUTES);
2983 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2985 /* Eventually save off posix specific response info and timestamps */
2988 free_rsp_buf(resp_buftype, rsp);
2991 cifs_small_buf_release(req);
2995 if (is_replayable_error(rc) &&
2996 smb2_should_replay(tcon, &retries, &cur_sleep))
3003 SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3004 struct smb_rqst *rqst, __u8 *oplock,
3005 struct cifs_open_parms *oparms, __le16 *path)
3007 struct smb2_create_req *req;
3008 unsigned int n_iov = 2;
3009 __u32 file_attributes = 0;
3012 unsigned int total_len;
3013 struct kvec *iov = rqst->rq_iov;
3017 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
3018 (void **) &req, &total_len);
3022 iov[0].iov_base = (char *)req;
3023 /* -1 since last byte is buf[0] which is sent below (path) */
3024 iov[0].iov_len = total_len - 1;
3026 if (oparms->create_options & CREATE_OPTION_READONLY)
3027 file_attributes |= ATTR_READONLY;
3028 if (oparms->create_options & CREATE_OPTION_SPECIAL)
3029 file_attributes |= ATTR_SYSTEM;
3031 req->ImpersonationLevel = IL_IMPERSONATION;
3032 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
3033 /* File attributes ignored on open (used in create though) */
3034 req->FileAttributes = cpu_to_le32(file_attributes);
3035 req->ShareAccess = FILE_SHARE_ALL_LE;
3037 req->CreateDisposition = cpu_to_le32(oparms->disposition);
3038 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
3039 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
3041 /* [MS-SMB2] 2.2.13 NameOffset:
3042 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
3043 * the SMB2 header, the file name includes a prefix that will
3044 * be processed during DFS name normalization as specified in
3045 * section 3.3.5.9. Otherwise, the file name is relative to
3046 * the share that is identified by the TreeId in the SMB2
3049 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
3052 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
3053 rc = alloc_path_with_tree_prefix(©_path, ©_size,
3055 tcon->tree_name, path);
3058 req->NameLength = cpu_to_le16(name_len * 2);
3059 uni_path_len = copy_size;
3062 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
3063 /* MUST set path len (NameLength) to 0 opening root of share */
3064 req->NameLength = cpu_to_le16(uni_path_len - 2);
3065 copy_size = round_up(uni_path_len, 8);
3066 copy_path = kzalloc(copy_size, GFP_KERNEL);
3069 memcpy((char *)copy_path, (const char *)path,
3071 uni_path_len = copy_size;
3075 iov[1].iov_len = uni_path_len;
3076 iov[1].iov_base = path;
3078 if ((!server->oplocks) || (tcon->no_lease))
3079 *oplock = SMB2_OPLOCK_LEVEL_NONE;
3081 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
3082 *oplock == SMB2_OPLOCK_LEVEL_NONE)
3083 req->RequestedOplockLevel = *oplock;
3084 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
3085 (oparms->create_options & CREATE_NOT_FILE))
3086 req->RequestedOplockLevel = *oplock; /* no srv lease support */
3088 rc = add_lease_context(server, req, iov, &n_iov,
3089 oparms->fid->lease_key, oplock);
3094 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3095 rc = add_durable_context(iov, &n_iov, oparms,
3096 tcon->use_persistent);
3101 if (tcon->posix_extensions) {
3102 rc = add_posix_context(iov, &n_iov, oparms->mode);
3107 if (tcon->snapshot_time) {
3108 cifs_dbg(FYI, "adding snapshot context\n");
3109 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
3114 if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) {
3118 if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
3119 (oparms->mode != ACL_NO_MODE))
3123 oparms->mode = ACL_NO_MODE;
3126 if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
3131 if (set_owner | set_mode) {
3132 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
3133 rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner);
3139 add_query_id_context(iov, &n_iov);
3140 add_ea_context(oparms, iov, &n_iov);
3144 * We have create contexts behind iov[1] (the file
3145 * name), point at them from the main create request
3147 req->CreateContextsOffset = cpu_to_le32(
3148 sizeof(struct smb2_create_req) +
3150 req->CreateContextsLength = 0;
3152 for (unsigned int i = 2; i < (n_iov-1); i++) {
3153 struct kvec *v = &iov[i];
3154 size_t len = v->iov_len;
3155 struct create_context *cctx =
3156 (struct create_context *)v->iov_base;
3158 cctx->Next = cpu_to_le32(len);
3159 le32_add_cpu(&req->CreateContextsLength, len);
3161 le32_add_cpu(&req->CreateContextsLength,
3162 iov[n_iov-1].iov_len);
3165 rqst->rq_nvec = n_iov;
3169 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
3170 * All other vectors are freed by kfree().
3173 SMB2_open_free(struct smb_rqst *rqst)
3177 if (rqst && rqst->rq_iov) {
3178 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
3179 for (i = 1; i < rqst->rq_nvec; i++)
3180 if (rqst->rq_iov[i].iov_base != smb2_padding)
3181 kfree(rqst->rq_iov[i].iov_base);
3186 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
3187 __u8 *oplock, struct smb2_file_all_info *buf,
3188 struct create_posix_rsp *posix,
3189 struct kvec *err_iov, int *buftype)
3191 struct smb_rqst rqst;
3192 struct smb2_create_rsp *rsp = NULL;
3193 struct cifs_tcon *tcon = oparms->tcon;
3194 struct cifs_ses *ses = tcon->ses;
3195 struct TCP_Server_Info *server;
3196 struct kvec iov[SMB2_CREATE_IOV_SIZE];
3197 struct kvec rsp_iov = {NULL, 0};
3198 int resp_buftype = CIFS_NO_BUFFER;
3201 int retries = 0, cur_sleep = 1;
3204 /* reinitialize for possible replay */
3206 server = cifs_pick_channel(ses);
3207 oparms->replay = !!(retries);
3209 cifs_dbg(FYI, "create/open\n");
3210 if (!ses || !server)
3213 if (smb3_encryption_required(tcon))
3214 flags |= CIFS_TRANSFORM_REQ;
3216 memset(&rqst, 0, sizeof(struct smb_rqst));
3217 memset(&iov, 0, sizeof(iov));
3219 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
3221 rc = SMB2_open_init(tcon, server,
3222 &rqst, oplock, oparms, path);
3226 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid, oparms->path,
3227 oparms->create_options, oparms->desired_access);
3230 smb2_set_replay(server, &rqst);
3232 rc = cifs_send_recv(xid, ses, server,
3233 &rqst, &resp_buftype, flags,
3235 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
3238 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
3239 if (err_iov && rsp) {
3241 *buftype = resp_buftype;
3242 resp_buftype = CIFS_NO_BUFFER;
3245 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
3246 oparms->create_options, oparms->desired_access, rc);
3247 if (rc == -EREMCHG) {
3248 pr_warn_once("server share %s deleted\n",
3250 tcon->need_reconnect = true;
3253 } else if (rsp == NULL) /* unlikely to happen, but safer to check */
3256 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
3257 oparms->create_options, oparms->desired_access);
3259 atomic_inc(&tcon->num_remote_opens);
3260 oparms->fid->persistent_fid = rsp->PersistentFileId;
3261 oparms->fid->volatile_fid = rsp->VolatileFileId;
3262 oparms->fid->access = oparms->desired_access;
3263 #ifdef CONFIG_CIFS_DEBUG2
3264 oparms->fid->mid = le64_to_cpu(rsp->hdr.MessageId);
3265 #endif /* CIFS_DEBUG2 */
3268 buf->CreationTime = rsp->CreationTime;
3269 buf->LastAccessTime = rsp->LastAccessTime;
3270 buf->LastWriteTime = rsp->LastWriteTime;
3271 buf->ChangeTime = rsp->ChangeTime;
3272 buf->AllocationSize = rsp->AllocationSize;
3273 buf->EndOfFile = rsp->EndofFile;
3274 buf->Attributes = rsp->FileAttributes;
3275 buf->NumberOfLinks = cpu_to_le32(1);
3276 buf->DeletePending = 0;
3280 rc = smb2_parse_contexts(server, &rsp_iov, &oparms->fid->epoch,
3281 oparms->fid->lease_key, oplock, buf, posix);
3283 SMB2_open_free(&rqst);
3284 free_rsp_buf(resp_buftype, rsp);
3286 if (is_replayable_error(rc) &&
3287 smb2_should_replay(tcon, &retries, &cur_sleep))
3294 SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3295 struct smb_rqst *rqst,
3296 u64 persistent_fid, u64 volatile_fid, u32 opcode,
3297 char *in_data, u32 indatalen,
3298 __u32 max_response_size)
3300 struct smb2_ioctl_req *req;
3301 struct kvec *iov = rqst->rq_iov;
3302 unsigned int total_len;
3306 rc = smb2_ioctl_req_init(opcode, tcon, server,
3307 (void **) &req, &total_len);
3313 * indatalen is usually small at a couple of bytes max, so
3314 * just allocate through generic pool
3316 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
3318 cifs_small_buf_release(req);
3323 req->CtlCode = cpu_to_le32(opcode);
3324 req->PersistentFileId = persistent_fid;
3325 req->VolatileFileId = volatile_fid;
3327 iov[0].iov_base = (char *)req;
3329 * If no input data, the size of ioctl struct in
3330 * protocol spec still includes a 1 byte data buffer,
3331 * but if input data passed to ioctl, we do not
3332 * want to double count this, so we do not send
3333 * the dummy one byte of data in iovec[0] if sending
3334 * input data (in iovec[1]).
3337 req->InputCount = cpu_to_le32(indatalen);
3338 /* do not set InputOffset if no input data */
3340 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
3342 iov[0].iov_len = total_len - 1;
3343 iov[1].iov_base = in_data_buf;
3344 iov[1].iov_len = indatalen;
3347 iov[0].iov_len = total_len;
3350 req->OutputOffset = 0;
3351 req->OutputCount = 0; /* MBZ */
3354 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
3355 * We Could increase default MaxOutputResponse, but that could require
3356 * more credits. Windows typically sets this smaller, but for some
3357 * ioctls it may be useful to allow server to send more. No point
3358 * limiting what the server can send as long as fits in one credit
3359 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
3360 * to increase this limit up in the future.
3361 * Note that for snapshot queries that servers like Azure expect that
3362 * the first query be minimal size (and just used to get the number/size
3363 * of previous versions) so response size must be specified as EXACTLY
3364 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
3365 * of eight bytes. Currently that is the only case where we set max
3366 * response size smaller.
3368 req->MaxOutputResponse = cpu_to_le32(max_response_size);
3369 req->hdr.CreditCharge =
3370 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
3371 SMB2_MAX_BUFFER_SIZE));
3372 /* always an FSCTL (for now) */
3373 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
3375 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
3376 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
3377 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
3383 SMB2_ioctl_free(struct smb_rqst *rqst)
3387 if (rqst && rqst->rq_iov) {
3388 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3389 for (i = 1; i < rqst->rq_nvec; i++)
3390 if (rqst->rq_iov[i].iov_base != smb2_padding)
3391 kfree(rqst->rq_iov[i].iov_base);
3397 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
3400 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3401 u64 volatile_fid, u32 opcode, char *in_data, u32 indatalen,
3402 u32 max_out_data_len, char **out_data,
3403 u32 *plen /* returned data len */)
3405 struct smb_rqst rqst;
3406 struct smb2_ioctl_rsp *rsp = NULL;
3407 struct cifs_ses *ses;
3408 struct TCP_Server_Info *server;
3409 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
3410 struct kvec rsp_iov = {NULL, 0};
3411 int resp_buftype = CIFS_NO_BUFFER;
3414 int retries = 0, cur_sleep = 1;
3424 /* reinitialize for possible replay */
3426 server = cifs_pick_channel(ses);
3431 cifs_dbg(FYI, "SMB2 IOCTL\n");
3433 if (out_data != NULL)
3436 /* zero out returned data len, in case of error */
3440 if (smb3_encryption_required(tcon))
3441 flags |= CIFS_TRANSFORM_REQ;
3443 memset(&rqst, 0, sizeof(struct smb_rqst));
3444 memset(&iov, 0, sizeof(iov));
3446 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
3448 rc = SMB2_ioctl_init(tcon, server,
3449 &rqst, persistent_fid, volatile_fid, opcode,
3450 in_data, indatalen, max_out_data_len);
3455 smb2_set_replay(server, &rqst);
3457 rc = cifs_send_recv(xid, ses, server,
3458 &rqst, &resp_buftype, flags,
3460 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
3463 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3464 ses->Suid, 0, opcode, rc);
3466 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
3467 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3469 } else if (rc == -EINVAL) {
3470 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3471 (opcode != FSCTL_SRV_COPYCHUNK)) {
3472 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3475 } else if (rc == -E2BIG) {
3476 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3477 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3482 /* check if caller wants to look at return data or just return rc */
3483 if ((plen == NULL) || (out_data == NULL))
3487 * Although unlikely to be possible for rsp to be null and rc not set,
3488 * adding check below is slightly safer long term (and quiets Coverity
3496 *plen = le32_to_cpu(rsp->OutputCount);
3498 /* We check for obvious errors in the output buffer length and offset */
3500 goto ioctl_exit; /* server returned no data */
3501 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
3502 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
3508 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
3509 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
3510 le32_to_cpu(rsp->OutputOffset));
3516 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3518 if (*out_data == NULL) {
3524 SMB2_ioctl_free(&rqst);
3525 free_rsp_buf(resp_buftype, rsp);
3527 if (is_replayable_error(rc) &&
3528 smb2_should_replay(tcon, &retries, &cur_sleep))
3535 * Individual callers to ioctl worker function follow
3539 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3540 u64 persistent_fid, u64 volatile_fid)
3543 struct compress_ioctl fsctl_input;
3544 char *ret_data = NULL;
3546 fsctl_input.CompressionState =
3547 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3549 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3550 FSCTL_SET_COMPRESSION,
3551 (char *)&fsctl_input /* data input */,
3552 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3553 &ret_data /* out data */, NULL);
3555 cifs_dbg(FYI, "set compression rc %d\n", rc);
3561 SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3562 struct smb_rqst *rqst,
3563 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
3565 struct smb2_close_req *req;
3566 struct kvec *iov = rqst->rq_iov;
3567 unsigned int total_len;
3570 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
3571 (void **) &req, &total_len);
3575 req->PersistentFileId = persistent_fid;
3576 req->VolatileFileId = volatile_fid;
3578 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3581 iov[0].iov_base = (char *)req;
3582 iov[0].iov_len = total_len;
3588 SMB2_close_free(struct smb_rqst *rqst)
3590 if (rqst && rqst->rq_iov)
3591 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3595 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3596 u64 persistent_fid, u64 volatile_fid,
3597 struct smb2_file_network_open_info *pbuf)
3599 struct smb_rqst rqst;
3600 struct smb2_close_rsp *rsp = NULL;
3601 struct cifs_ses *ses = tcon->ses;
3602 struct TCP_Server_Info *server;
3604 struct kvec rsp_iov;
3605 int resp_buftype = CIFS_NO_BUFFER;
3608 bool query_attrs = false;
3609 int retries = 0, cur_sleep = 1;
3612 /* reinitialize for possible replay */
3614 query_attrs = false;
3615 server = cifs_pick_channel(ses);
3617 cifs_dbg(FYI, "Close\n");
3619 if (!ses || !server)
3622 if (smb3_encryption_required(tcon))
3623 flags |= CIFS_TRANSFORM_REQ;
3625 memset(&rqst, 0, sizeof(struct smb_rqst));
3626 memset(&iov, 0, sizeof(iov));
3630 /* check if need to ask server to return timestamps in close response */
3634 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3635 rc = SMB2_close_init(tcon, server,
3636 &rqst, persistent_fid, volatile_fid,
3642 smb2_set_replay(server, &rqst);
3644 rc = cifs_send_recv(xid, ses, server,
3645 &rqst, &resp_buftype, flags, &rsp_iov);
3646 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
3649 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
3650 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3654 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3657 memcpy(&pbuf->network_open_info,
3658 &rsp->network_open_info,
3659 sizeof(pbuf->network_open_info));
3660 atomic_dec(&tcon->num_remote_opens);
3664 SMB2_close_free(&rqst);
3665 free_rsp_buf(resp_buftype, rsp);
3667 /* retry close in a worker thread if this one is interrupted */
3668 if (is_interrupt_error(rc)) {
3671 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3674 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3675 persistent_fid, tmp_rc);
3678 if (is_replayable_error(rc) &&
3679 smb2_should_replay(tcon, &retries, &cur_sleep))
3686 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3687 u64 persistent_fid, u64 volatile_fid)
3689 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3693 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3694 struct kvec *iov, unsigned int min_buf_size)
3696 unsigned int smb_len = iov->iov_len;
3697 char *end_of_smb = smb_len + (char *)iov->iov_base;
3698 char *begin_of_buf = offset + (char *)iov->iov_base;
3699 char *end_of_buf = begin_of_buf + buffer_length;
3702 if (buffer_length < min_buf_size) {
3703 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3704 buffer_length, min_buf_size);
3708 /* check if beyond RFC1001 maximum length */
3709 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3710 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3711 buffer_length, smb_len);
3715 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3716 cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
3724 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3725 * Caller must free buffer.
3728 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3729 struct kvec *iov, unsigned int minbufsize,
3732 char *begin_of_buf = offset + (char *)iov->iov_base;
3738 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3742 memcpy(data, begin_of_buf, minbufsize);
3748 SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3749 struct smb_rqst *rqst,
3750 u64 persistent_fid, u64 volatile_fid,
3751 u8 info_class, u8 info_type, u32 additional_info,
3752 size_t output_len, size_t input_len, void *input)
3754 struct smb2_query_info_req *req;
3755 struct kvec *iov = rqst->rq_iov;
3756 unsigned int total_len;
3760 if (unlikely(check_add_overflow(input_len, sizeof(*req), &len) ||
3761 len > CIFSMaxBufSize))
3764 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
3765 (void **) &req, &total_len);
3769 req->InfoType = info_type;
3770 req->FileInfoClass = info_class;
3771 req->PersistentFileId = persistent_fid;
3772 req->VolatileFileId = volatile_fid;
3773 req->AdditionalInformation = cpu_to_le32(additional_info);
3775 req->OutputBufferLength = cpu_to_le32(output_len);
3777 req->InputBufferLength = cpu_to_le32(input_len);
3778 /* total_len for smb query request never close to le16 max */
3779 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3780 memcpy(req->Buffer, input, input_len);
3783 iov[0].iov_base = (char *)req;
3785 iov[0].iov_len = len;
3790 SMB2_query_info_free(struct smb_rqst *rqst)
3792 if (rqst && rqst->rq_iov)
3793 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
3797 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3798 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3799 u32 additional_info, size_t output_len, size_t min_len, void **data,
3802 struct smb_rqst rqst;
3803 struct smb2_query_info_rsp *rsp = NULL;
3805 struct kvec rsp_iov;
3807 int resp_buftype = CIFS_NO_BUFFER;
3808 struct cifs_ses *ses = tcon->ses;
3809 struct TCP_Server_Info *server;
3811 bool allocated = false;
3812 int retries = 0, cur_sleep = 1;
3814 cifs_dbg(FYI, "Query Info\n");
3820 /* reinitialize for possible replay */
3823 server = cifs_pick_channel(ses);
3828 if (smb3_encryption_required(tcon))
3829 flags |= CIFS_TRANSFORM_REQ;
3831 memset(&rqst, 0, sizeof(struct smb_rqst));
3832 memset(&iov, 0, sizeof(iov));
3836 rc = SMB2_query_info_init(tcon, server,
3837 &rqst, persistent_fid, volatile_fid,
3838 info_class, info_type, additional_info,
3839 output_len, 0, NULL);
3843 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3844 ses->Suid, info_class, (__u32)info_type);
3847 smb2_set_replay(server, &rqst);
3849 rc = cifs_send_recv(xid, ses, server,
3850 &rqst, &resp_buftype, flags, &rsp_iov);
3851 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3854 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3855 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3856 ses->Suid, info_class, (__u32)info_type, rc);
3860 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3861 ses->Suid, info_class, (__u32)info_type);
3864 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3866 *data = kmalloc(*dlen, GFP_KERNEL);
3869 "Error %d allocating memory for acl\n",
3879 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3880 le32_to_cpu(rsp->OutputBufferLength),
3881 &rsp_iov, dlen ? *dlen : min_len, *data);
3882 if (rc && allocated) {
3889 SMB2_query_info_free(&rqst);
3890 free_rsp_buf(resp_buftype, rsp);
3892 if (is_replayable_error(rc) &&
3893 smb2_should_replay(tcon, &retries, &cur_sleep))
3899 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3900 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3902 return query_info(xid, tcon, persistent_fid, volatile_fid,
3903 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3904 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3905 sizeof(struct smb2_file_all_info), (void **)&data,
3910 /* currently unused, as now we are doing compounding instead (see smb311_posix_query_path_info) */
3912 SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3913 u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen)
3915 size_t output_len = sizeof(struct smb311_posix_qinfo *) +
3916 (sizeof(struct smb_sid) * 2) + (PATH_MAX * 2);
3919 return query_info(xid, tcon, persistent_fid, volatile_fid,
3920 SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0,
3921 output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen);
3922 /* Note caller must free "data" (passed in above). It may be allocated in query_info call */
3927 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3928 u64 persistent_fid, u64 volatile_fid,
3929 void **data, u32 *plen, u32 extra_info)
3931 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
3935 return query_info(xid, tcon, persistent_fid, volatile_fid,
3936 0, SMB2_O_INFO_SECURITY, additional_info,
3937 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3941 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3942 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3944 return query_info(xid, tcon, persistent_fid, volatile_fid,
3945 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3946 sizeof(struct smb2_file_internal_info),
3947 sizeof(struct smb2_file_internal_info),
3948 (void **)&uniqueid, NULL);
3952 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3953 * See MS-SMB2 2.2.35 and 2.2.36
3957 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3958 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3959 u64 persistent_fid, u64 volatile_fid,
3960 u32 completion_filter, bool watch_tree)
3962 struct smb2_change_notify_req *req;
3963 struct kvec *iov = rqst->rq_iov;
3964 unsigned int total_len;
3967 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
3968 (void **) &req, &total_len);
3972 req->PersistentFileId = persistent_fid;
3973 req->VolatileFileId = volatile_fid;
3974 /* See note 354 of MS-SMB2, 64K max */
3975 req->OutputBufferLength =
3976 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3977 req->CompletionFilter = cpu_to_le32(completion_filter);
3979 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3983 iov[0].iov_base = (char *)req;
3984 iov[0].iov_len = total_len;
3990 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3991 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3992 u32 completion_filter, u32 max_out_data_len, char **out_data,
3993 u32 *plen /* returned data len */)
3995 struct cifs_ses *ses = tcon->ses;
3996 struct TCP_Server_Info *server;
3997 struct smb_rqst rqst;
3998 struct smb2_change_notify_rsp *smb_rsp;
4000 struct kvec rsp_iov = {NULL, 0};
4001 int resp_buftype = CIFS_NO_BUFFER;
4004 int retries = 0, cur_sleep = 1;
4007 /* reinitialize for possible replay */
4009 server = cifs_pick_channel(ses);
4011 cifs_dbg(FYI, "change notify\n");
4012 if (!ses || !server)
4015 if (smb3_encryption_required(tcon))
4016 flags |= CIFS_TRANSFORM_REQ;
4018 memset(&rqst, 0, sizeof(struct smb_rqst));
4019 memset(&iov, 0, sizeof(iov));
4026 rc = SMB2_notify_init(xid, &rqst, tcon, server,
4027 persistent_fid, volatile_fid,
4028 completion_filter, watch_tree);
4032 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
4033 (u8)watch_tree, completion_filter);
4036 smb2_set_replay(server, &rqst);
4038 rc = cifs_send_recv(xid, ses, server,
4039 &rqst, &resp_buftype, flags, &rsp_iov);
4042 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
4043 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
4044 (u8)watch_tree, completion_filter, rc);
4046 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
4047 ses->Suid, (u8)watch_tree, completion_filter);
4048 /* validate that notify information is plausible */
4049 if ((rsp_iov.iov_base == NULL) ||
4050 (rsp_iov.iov_len < sizeof(struct smb2_change_notify_rsp) + 1))
4053 smb_rsp = (struct smb2_change_notify_rsp *)rsp_iov.iov_base;
4055 smb2_validate_iov(le16_to_cpu(smb_rsp->OutputBufferOffset),
4056 le32_to_cpu(smb_rsp->OutputBufferLength), &rsp_iov,
4057 sizeof(struct file_notify_information));
4059 *out_data = kmemdup((char *)smb_rsp + le16_to_cpu(smb_rsp->OutputBufferOffset),
4060 le32_to_cpu(smb_rsp->OutputBufferLength), GFP_KERNEL);
4061 if (*out_data == NULL) {
4065 *plen = le32_to_cpu(smb_rsp->OutputBufferLength);
4070 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
4071 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4073 if (is_replayable_error(rc) &&
4074 smb2_should_replay(tcon, &retries, &cur_sleep))
4083 * This is a no-op for now. We're not really interested in the reply, but
4084 * rather in the fact that the server sent one and that server->lstrp
4087 * FIXME: maybe we should consider checking that the reply matches request?
4090 smb2_echo_callback(struct mid_q_entry *mid)
4092 struct TCP_Server_Info *server = mid->callback_data;
4093 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
4094 struct cifs_credits credits = { .value = 0, .instance = 0 };
4096 if (mid->mid_state == MID_RESPONSE_RECEIVED
4097 || mid->mid_state == MID_RESPONSE_MALFORMED) {
4098 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4099 credits.instance = server->reconnect_instance;
4103 add_credits(server, &credits, CIFS_ECHO_OP);
4106 void smb2_reconnect_server(struct work_struct *work)
4108 struct TCP_Server_Info *server = container_of(work,
4109 struct TCP_Server_Info, reconnect.work);
4110 struct TCP_Server_Info *pserver;
4111 struct cifs_ses *ses, *ses2;
4112 struct cifs_tcon *tcon, *tcon2;
4113 struct list_head tmp_list, tmp_ses_list;
4114 bool ses_exist = false;
4115 bool tcon_selected = false;
4117 bool resched = false;
4119 /* first check if ref count has reached 0, if not inc ref count */
4120 spin_lock(&cifs_tcp_ses_lock);
4121 if (!server->srv_count) {
4122 spin_unlock(&cifs_tcp_ses_lock);
4125 server->srv_count++;
4126 spin_unlock(&cifs_tcp_ses_lock);
4128 /* If server is a channel, select the primary channel */
4129 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
4131 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
4132 mutex_lock(&pserver->reconnect_mutex);
4134 /* if the server is marked for termination, drop the ref count here */
4135 if (server->terminate) {
4136 cifs_put_tcp_session(server, true);
4137 mutex_unlock(&pserver->reconnect_mutex);
4141 INIT_LIST_HEAD(&tmp_list);
4142 INIT_LIST_HEAD(&tmp_ses_list);
4143 cifs_dbg(FYI, "Reconnecting tcons and channels\n");
4145 spin_lock(&cifs_tcp_ses_lock);
4146 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
4147 spin_lock(&ses->ses_lock);
4148 if (ses->ses_status == SES_EXITING) {
4149 spin_unlock(&ses->ses_lock);
4152 spin_unlock(&ses->ses_lock);
4154 tcon_selected = false;
4156 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
4157 if (tcon->need_reconnect || tcon->need_reopen_files) {
4159 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
4160 netfs_trace_tcon_ref_get_reconnect_server);
4161 list_add_tail(&tcon->rlist, &tmp_list);
4162 tcon_selected = true;
4166 * IPC has the same lifetime as its session and uses its
4169 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
4170 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
4171 tcon_selected = true;
4172 cifs_smb_ses_inc_refcount(ses);
4175 * handle the case where channel needs to reconnect
4176 * binding session, but tcon is healthy (some other channel
4179 spin_lock(&ses->chan_lock);
4180 if (!tcon_selected && cifs_chan_needs_reconnect(ses, server)) {
4181 list_add_tail(&ses->rlist, &tmp_ses_list);
4183 cifs_smb_ses_inc_refcount(ses);
4185 spin_unlock(&ses->chan_lock);
4187 spin_unlock(&cifs_tcp_ses_lock);
4189 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
4190 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server, true);
4192 cifs_reopen_persistent_handles(tcon);
4195 list_del_init(&tcon->rlist);
4197 cifs_put_smb_ses(tcon->ses);
4199 cifs_put_tcon(tcon, netfs_trace_tcon_ref_put_reconnect_server);
4205 /* allocate a dummy tcon struct used for reconnect */
4206 tcon = tcon_info_alloc(false, netfs_trace_tcon_ref_new_reconnect_server);
4209 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
4210 list_del_init(&ses->rlist);
4211 cifs_put_smb_ses(ses);
4216 tcon->status = TID_GOOD;
4217 tcon->retry = false;
4218 tcon->need_reconnect = false;
4220 /* now reconnect sessions for necessary channels */
4221 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
4223 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server, true);
4226 list_del_init(&ses->rlist);
4227 cifs_put_smb_ses(ses);
4229 tconInfoFree(tcon, netfs_trace_tcon_ref_free_reconnect_server);
4232 cifs_dbg(FYI, "Reconnecting tcons and channels finished\n");
4234 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
4235 mutex_unlock(&pserver->reconnect_mutex);
4237 /* now we can safely release srv struct */
4238 cifs_put_tcp_session(server, true);
4242 SMB2_echo(struct TCP_Server_Info *server)
4244 struct smb2_echo_req *req;
4247 struct smb_rqst rqst = { .rq_iov = iov,
4249 unsigned int total_len;
4251 cifs_dbg(FYI, "In echo request for conn_id %lld\n", server->conn_id);
4253 spin_lock(&server->srv_lock);
4254 if (server->ops->need_neg &&
4255 server->ops->need_neg(server)) {
4256 spin_unlock(&server->srv_lock);
4257 /* No need to send echo on newly established connections */
4258 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
4261 spin_unlock(&server->srv_lock);
4263 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
4264 (void **)&req, &total_len);
4268 req->hdr.CreditRequest = cpu_to_le16(1);
4270 iov[0].iov_len = total_len;
4271 iov[0].iov_base = (char *)req;
4273 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
4274 server, CIFS_ECHO_OP, NULL);
4276 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
4278 cifs_small_buf_release(req);
4283 SMB2_flush_free(struct smb_rqst *rqst)
4285 if (rqst && rqst->rq_iov)
4286 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4290 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
4291 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
4292 u64 persistent_fid, u64 volatile_fid)
4294 struct smb2_flush_req *req;
4295 struct kvec *iov = rqst->rq_iov;
4296 unsigned int total_len;
4299 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
4300 (void **) &req, &total_len);
4304 req->PersistentFileId = persistent_fid;
4305 req->VolatileFileId = volatile_fid;
4307 iov[0].iov_base = (char *)req;
4308 iov[0].iov_len = total_len;
4314 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4317 struct cifs_ses *ses = tcon->ses;
4318 struct smb_rqst rqst;
4320 struct kvec rsp_iov = {NULL, 0};
4321 struct TCP_Server_Info *server;
4322 int resp_buftype = CIFS_NO_BUFFER;
4325 int retries = 0, cur_sleep = 1;
4328 /* reinitialize for possible replay */
4330 server = cifs_pick_channel(ses);
4332 cifs_dbg(FYI, "flush\n");
4333 if (!ses || !(ses->server))
4336 if (smb3_encryption_required(tcon))
4337 flags |= CIFS_TRANSFORM_REQ;
4339 memset(&rqst, 0, sizeof(struct smb_rqst));
4340 memset(&iov, 0, sizeof(iov));
4344 rc = SMB2_flush_init(xid, &rqst, tcon, server,
4345 persistent_fid, volatile_fid);
4349 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
4352 smb2_set_replay(server, &rqst);
4354 rc = cifs_send_recv(xid, ses, server,
4355 &rqst, &resp_buftype, flags, &rsp_iov);
4358 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
4359 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
4362 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
4366 SMB2_flush_free(&rqst);
4367 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4369 if (is_replayable_error(rc) &&
4370 smb2_should_replay(tcon, &retries, &cur_sleep))
4376 #ifdef CONFIG_CIFS_SMB_DIRECT
4377 static inline bool smb3_use_rdma_offload(struct cifs_io_parms *io_parms)
4379 struct TCP_Server_Info *server = io_parms->server;
4380 struct cifs_tcon *tcon = io_parms->tcon;
4382 /* we can only offload if we're connected */
4383 if (!server || !tcon)
4386 /* we can only offload on an rdma connection */
4387 if (!server->rdma || !server->smbd_conn)
4390 /* we don't support signed offload yet */
4394 /* we don't support encrypted offload yet */
4395 if (smb3_encryption_required(tcon))
4398 /* offload also has its overhead, so only do it if desired */
4399 if (io_parms->length < server->smbd_conn->rdma_readwrite_threshold)
4404 #endif /* CONFIG_CIFS_SMB_DIRECT */
4407 * To form a chain of read requests, any read requests after the first should
4408 * have the end_of_chain boolean set to true.
4411 smb2_new_read_req(void **buf, unsigned int *total_len,
4412 struct cifs_io_parms *io_parms, struct cifs_io_subrequest *rdata,
4413 unsigned int remaining_bytes, int request_type)
4416 struct smb2_read_req *req = NULL;
4417 struct smb2_hdr *shdr;
4418 struct TCP_Server_Info *server = io_parms->server;
4420 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
4421 (void **) &req, total_len);
4426 return -ECONNABORTED;
4429 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4431 req->PersistentFileId = io_parms->persistent_fid;
4432 req->VolatileFileId = io_parms->volatile_fid;
4433 req->ReadChannelInfoOffset = 0; /* reserved */
4434 req->ReadChannelInfoLength = 0; /* reserved */
4435 req->Channel = 0; /* reserved */
4436 req->MinimumCount = 0;
4437 req->Length = cpu_to_le32(io_parms->length);
4438 req->Offset = cpu_to_le64(io_parms->offset);
4440 trace_smb3_read_enter(rdata ? rdata->rreq->debug_id : 0,
4441 rdata ? rdata->subreq.debug_index : 0,
4442 rdata ? rdata->xid : 0,
4443 io_parms->persistent_fid,
4444 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4445 io_parms->offset, io_parms->length);
4446 #ifdef CONFIG_CIFS_SMB_DIRECT
4448 * If we want to do a RDMA write, fill in and append
4449 * smbd_buffer_descriptor_v1 to the end of read request
4451 if (rdata && smb3_use_rdma_offload(io_parms)) {
4452 struct smbd_buffer_descriptor_v1 *v1;
4453 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4455 rdata->mr = smbd_register_mr(server->smbd_conn, &rdata->subreq.io_iter,
4456 true, need_invalidate);
4460 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4461 if (need_invalidate)
4462 req->Channel = SMB2_CHANNEL_RDMA_V1;
4463 req->ReadChannelInfoOffset =
4464 cpu_to_le16(offsetof(struct smb2_read_req, Buffer));
4465 req->ReadChannelInfoLength =
4466 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4467 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4468 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
4469 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
4470 v1->length = cpu_to_le32(rdata->mr->mr->length);
4472 *total_len += sizeof(*v1) - 1;
4475 if (request_type & CHAINED_REQUEST) {
4476 if (!(request_type & END_OF_CHAIN)) {
4477 /* next 8-byte aligned request */
4478 *total_len = ALIGN(*total_len, 8);
4479 shdr->NextCommand = cpu_to_le32(*total_len);
4480 } else /* END_OF_CHAIN */
4481 shdr->NextCommand = 0;
4482 if (request_type & RELATED_REQUEST) {
4483 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
4485 * Related requests use info from previous read request
4488 shdr->SessionId = cpu_to_le64(0xFFFFFFFFFFFFFFFF);
4489 shdr->Id.SyncId.TreeId = cpu_to_le32(0xFFFFFFFF);
4490 req->PersistentFileId = (u64)-1;
4491 req->VolatileFileId = (u64)-1;
4494 if (remaining_bytes > io_parms->length)
4495 req->RemainingBytes = cpu_to_le32(remaining_bytes);
4497 req->RemainingBytes = 0;
4503 static void smb2_readv_worker(struct work_struct *work)
4505 struct cifs_io_subrequest *rdata =
4506 container_of(work, struct cifs_io_subrequest, subreq.work);
4508 netfs_read_subreq_terminated(&rdata->subreq, rdata->result, false);
4512 smb2_readv_callback(struct mid_q_entry *mid)
4514 struct cifs_io_subrequest *rdata = mid->callback_data;
4515 struct netfs_inode *ictx = netfs_inode(rdata->rreq->inode);
4516 struct cifs_tcon *tcon = tlink_tcon(rdata->req->cfile->tlink);
4517 struct TCP_Server_Info *server = rdata->server;
4518 struct smb2_hdr *shdr =
4519 (struct smb2_hdr *)rdata->iov[0].iov_base;
4520 struct cifs_credits credits = {
4523 .rreq_debug_id = rdata->rreq->debug_id,
4524 .rreq_debug_index = rdata->subreq.debug_index,
4526 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1], .rq_nvec = 1 };
4527 unsigned int rreq_debug_id = rdata->rreq->debug_id;
4528 unsigned int subreq_debug_index = rdata->subreq.debug_index;
4530 if (rdata->got_bytes) {
4531 rqst.rq_iter = rdata->subreq.io_iter;
4534 WARN_ONCE(rdata->server != mid->server,
4535 "rdata server %p != mid server %p",
4536 rdata->server, mid->server);
4538 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%zu/%zu\n",
4539 __func__, mid->mid, mid->mid_state, rdata->result,
4540 rdata->got_bytes, rdata->subreq.len - rdata->subreq.transferred);
4542 switch (mid->mid_state) {
4543 case MID_RESPONSE_RECEIVED:
4544 credits.value = le16_to_cpu(shdr->CreditRequest);
4545 credits.instance = server->reconnect_instance;
4546 /* result already set, check signature */
4547 if (server->sign && !mid->decrypted) {
4550 iov_iter_truncate(&rqst.rq_iter, rdata->got_bytes);
4551 rc = smb2_verify_signature(&rqst, server);
4553 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
4556 /* FIXME: should this be counted toward the initiating task? */
4557 task_io_account_read(rdata->got_bytes);
4558 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4560 case MID_REQUEST_SUBMITTED:
4561 case MID_RETRY_NEEDED:
4562 __set_bit(NETFS_SREQ_NEED_RETRY, &rdata->subreq.flags);
4563 rdata->result = -EAGAIN;
4564 if (server->sign && rdata->got_bytes)
4565 /* reset bytes number since we can not check a sign */
4566 rdata->got_bytes = 0;
4567 /* FIXME: should this be counted toward the initiating task? */
4568 task_io_account_read(rdata->got_bytes);
4569 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4571 case MID_RESPONSE_MALFORMED:
4572 credits.value = le16_to_cpu(shdr->CreditRequest);
4573 credits.instance = server->reconnect_instance;
4576 rdata->result = -EIO;
4578 #ifdef CONFIG_CIFS_SMB_DIRECT
4580 * If this rdata has a memory registered, the MR can be freed
4581 * MR needs to be freed as soon as I/O finishes to prevent deadlock
4582 * because they have limited number and are used for future I/Os
4585 smbd_deregister_mr(rdata->mr);
4589 if (rdata->result && rdata->result != -ENODATA) {
4590 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
4591 trace_smb3_read_err(rdata->rreq->debug_id,
4592 rdata->subreq.debug_index,
4594 rdata->req->cfile->fid.persistent_fid,
4595 tcon->tid, tcon->ses->Suid,
4596 rdata->subreq.start + rdata->subreq.transferred,
4597 rdata->subreq.len - rdata->subreq.transferred,
4600 trace_smb3_read_done(rdata->rreq->debug_id,
4601 rdata->subreq.debug_index,
4603 rdata->req->cfile->fid.persistent_fid,
4604 tcon->tid, tcon->ses->Suid,
4605 rdata->subreq.start + rdata->subreq.transferred,
4608 if (rdata->result == -ENODATA) {
4609 __set_bit(NETFS_SREQ_HIT_EOF, &rdata->subreq.flags);
4612 size_t trans = rdata->subreq.transferred + rdata->got_bytes;
4613 if (trans < rdata->subreq.len &&
4614 rdata->subreq.start + trans == ictx->remote_i_size) {
4615 __set_bit(NETFS_SREQ_HIT_EOF, &rdata->subreq.flags);
4618 __set_bit(NETFS_SREQ_MADE_PROGRESS, &rdata->subreq.flags);
4620 trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, rdata->credits.value,
4621 server->credits, server->in_flight,
4622 0, cifs_trace_rw_credits_read_response_clear);
4623 rdata->credits.value = 0;
4624 rdata->subreq.transferred += rdata->got_bytes;
4625 trace_netfs_sreq(&rdata->subreq, netfs_sreq_trace_io_progress);
4626 INIT_WORK(&rdata->subreq.work, smb2_readv_worker);
4627 queue_work(cifsiod_wq, &rdata->subreq.work);
4629 trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, 0,
4630 server->credits, server->in_flight,
4631 credits.value, cifs_trace_rw_credits_read_response_add);
4632 add_credits(server, &credits, 0);
4635 /* smb2_async_readv - send an async read, and set up mid to handle result */
4637 smb2_async_readv(struct cifs_io_subrequest *rdata)
4641 struct netfs_io_subrequest *subreq = &rdata->subreq;
4642 struct smb2_hdr *shdr;
4643 struct cifs_io_parms io_parms;
4644 struct smb_rqst rqst = { .rq_iov = rdata->iov,
4646 struct TCP_Server_Info *server;
4647 struct cifs_tcon *tcon = tlink_tcon(rdata->req->cfile->tlink);
4648 unsigned int total_len;
4651 cifs_dbg(FYI, "%s: offset=%llu bytes=%zu\n",
4652 __func__, subreq->start, subreq->len);
4655 rdata->server = cifs_pick_channel(tcon->ses);
4657 io_parms.tcon = tlink_tcon(rdata->req->cfile->tlink);
4658 io_parms.server = server = rdata->server;
4659 io_parms.offset = subreq->start + subreq->transferred;
4660 io_parms.length = subreq->len - subreq->transferred;
4661 io_parms.persistent_fid = rdata->req->cfile->fid.persistent_fid;
4662 io_parms.volatile_fid = rdata->req->cfile->fid.volatile_fid;
4663 io_parms.pid = rdata->req->pid;
4665 rc = smb2_new_read_req(
4666 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
4670 if (smb3_encryption_required(io_parms.tcon))
4671 flags |= CIFS_TRANSFORM_REQ;
4673 rdata->iov[0].iov_base = buf;
4674 rdata->iov[0].iov_len = total_len;
4675 rdata->got_bytes = 0;
4678 shdr = (struct smb2_hdr *)buf;
4680 if (rdata->credits.value > 0) {
4681 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(io_parms.length,
4682 SMB2_MAX_BUFFER_SIZE));
4683 credit_request = le16_to_cpu(shdr->CreditCharge) + 8;
4684 if (server->credits >= server->max_credits)
4685 shdr->CreditRequest = cpu_to_le16(0);
4687 shdr->CreditRequest = cpu_to_le16(
4688 min_t(int, server->max_credits -
4689 server->credits, credit_request));
4691 rc = adjust_credits(server, rdata, cifs_trace_rw_credits_call_readv_adjust);
4693 goto async_readv_out;
4695 flags |= CIFS_HAS_CREDITS;
4698 rc = cifs_call_async(server, &rqst,
4699 cifs_readv_receive, smb2_readv_callback,
4700 smb3_handle_read_data, rdata, flags,
4703 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
4704 trace_smb3_read_err(rdata->rreq->debug_id,
4705 subreq->debug_index,
4706 rdata->xid, io_parms.persistent_fid,
4708 io_parms.tcon->ses->Suid,
4710 subreq->len - subreq->transferred, rc);
4714 cifs_small_buf_release(buf);
4719 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
4720 unsigned int *nbytes, char **buf, int *buf_type)
4722 struct smb_rqst rqst;
4723 int resp_buftype, rc;
4724 struct smb2_read_req *req = NULL;
4725 struct smb2_read_rsp *rsp = NULL;
4727 struct kvec rsp_iov;
4728 unsigned int total_len;
4729 int flags = CIFS_LOG_ERROR;
4730 struct cifs_ses *ses = io_parms->tcon->ses;
4732 if (!io_parms->server)
4733 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4736 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
4740 if (smb3_encryption_required(io_parms->tcon))
4741 flags |= CIFS_TRANSFORM_REQ;
4743 iov[0].iov_base = (char *)req;
4744 iov[0].iov_len = total_len;
4746 memset(&rqst, 0, sizeof(struct smb_rqst));
4750 rc = cifs_send_recv(xid, ses, io_parms->server,
4751 &rqst, &resp_buftype, flags, &rsp_iov);
4752 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
4755 if (rc != -ENODATA) {
4756 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4757 cifs_dbg(VFS, "Send error in read = %d\n", rc);
4758 trace_smb3_read_err(0, 0, xid,
4759 req->PersistentFileId,
4760 io_parms->tcon->tid, ses->Suid,
4761 io_parms->offset, io_parms->length,
4764 trace_smb3_read_done(0, 0, xid,
4765 req->PersistentFileId, io_parms->tcon->tid,
4766 ses->Suid, io_parms->offset, 0);
4767 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4768 cifs_small_buf_release(req);
4769 return rc == -ENODATA ? 0 : rc;
4771 trace_smb3_read_done(0, 0, xid,
4772 req->PersistentFileId,
4773 io_parms->tcon->tid, ses->Suid,
4774 io_parms->offset, io_parms->length);
4776 cifs_small_buf_release(req);
4778 *nbytes = le32_to_cpu(rsp->DataLength);
4779 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4780 (*nbytes > io_parms->length)) {
4781 cifs_dbg(FYI, "bad length %d for count %d\n",
4782 *nbytes, io_parms->length);
4788 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
4789 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4790 } else if (resp_buftype != CIFS_NO_BUFFER) {
4791 *buf = rsp_iov.iov_base;
4792 if (resp_buftype == CIFS_SMALL_BUFFER)
4793 *buf_type = CIFS_SMALL_BUFFER;
4794 else if (resp_buftype == CIFS_LARGE_BUFFER)
4795 *buf_type = CIFS_LARGE_BUFFER;
4801 * Check the mid_state and signature on received buffer (if any), and queue the
4802 * workqueue completion task.
4805 smb2_writev_callback(struct mid_q_entry *mid)
4807 struct cifs_io_subrequest *wdata = mid->callback_data;
4808 struct cifs_tcon *tcon = tlink_tcon(wdata->req->cfile->tlink);
4809 struct TCP_Server_Info *server = wdata->server;
4810 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
4811 struct cifs_credits credits = {
4814 .rreq_debug_id = wdata->rreq->debug_id,
4815 .rreq_debug_index = wdata->subreq.debug_index,
4817 unsigned int rreq_debug_id = wdata->rreq->debug_id;
4818 unsigned int subreq_debug_index = wdata->subreq.debug_index;
4822 WARN_ONCE(wdata->server != mid->server,
4823 "wdata server %p != mid server %p",
4824 wdata->server, mid->server);
4826 switch (mid->mid_state) {
4827 case MID_RESPONSE_RECEIVED:
4828 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4829 credits.instance = server->reconnect_instance;
4830 result = smb2_check_receive(mid, server, 0);
4834 written = le32_to_cpu(rsp->DataLength);
4836 * Mask off high 16 bits when bytes written as returned
4837 * by the server is greater than bytes requested by the
4838 * client. OS/2 servers are known to set incorrect
4841 if (written > wdata->subreq.len)
4844 cifs_stats_bytes_written(tcon, written);
4846 if (written < wdata->subreq.len) {
4847 wdata->result = -ENOSPC;
4848 } else if (written > 0) {
4849 wdata->subreq.len = written;
4850 __set_bit(NETFS_SREQ_MADE_PROGRESS, &wdata->subreq.flags);
4853 case MID_REQUEST_SUBMITTED:
4854 case MID_RETRY_NEEDED:
4857 case MID_RESPONSE_MALFORMED:
4858 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4859 credits.instance = server->reconnect_instance;
4865 #ifdef CONFIG_CIFS_SMB_DIRECT
4867 * If this wdata has a memory registered, the MR can be freed
4868 * The number of MRs available is limited, it's important to recover
4869 * used MR as soon as I/O is finished. Hold MR longer in the later
4870 * I/O process can possibly result in I/O deadlock due to lack of MR
4871 * to send request on I/O retry
4874 smbd_deregister_mr(wdata->mr);
4879 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4880 trace_smb3_write_err(wdata->rreq->debug_id,
4881 wdata->subreq.debug_index,
4883 wdata->req->cfile->fid.persistent_fid,
4884 tcon->tid, tcon->ses->Suid, wdata->subreq.start,
4885 wdata->subreq.len, wdata->result);
4886 if (wdata->result == -ENOSPC)
4887 pr_warn_once("Out of space writing to %s\n",
4890 trace_smb3_write_done(wdata->rreq->debug_id,
4891 wdata->subreq.debug_index,
4893 wdata->req->cfile->fid.persistent_fid,
4894 tcon->tid, tcon->ses->Suid,
4895 wdata->subreq.start, wdata->subreq.len);
4897 trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, wdata->credits.value,
4898 server->credits, server->in_flight,
4899 0, cifs_trace_rw_credits_write_response_clear);
4900 wdata->credits.value = 0;
4901 trace_netfs_sreq(&wdata->subreq, netfs_sreq_trace_io_progress);
4902 cifs_write_subrequest_terminated(wdata, result ?: written, true);
4904 trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, 0,
4905 server->credits, server->in_flight,
4906 credits.value, cifs_trace_rw_credits_write_response_add);
4907 add_credits(server, &credits, 0);
4910 /* smb2_async_writev - send an async write, and set up mid to handle result */
4912 smb2_async_writev(struct cifs_io_subrequest *wdata)
4914 int rc = -EACCES, flags = 0;
4915 struct smb2_write_req *req = NULL;
4916 struct smb2_hdr *shdr;
4917 struct cifs_tcon *tcon = tlink_tcon(wdata->req->cfile->tlink);
4918 struct TCP_Server_Info *server = wdata->server;
4920 struct smb_rqst rqst = { };
4921 unsigned int total_len, xid = wdata->xid;
4922 struct cifs_io_parms _io_parms;
4923 struct cifs_io_parms *io_parms = NULL;
4927 * in future we may get cifs_io_parms passed in from the caller,
4928 * but for now we construct it here...
4930 _io_parms = (struct cifs_io_parms) {
4933 .offset = wdata->subreq.start,
4934 .length = wdata->subreq.len,
4935 .persistent_fid = wdata->req->cfile->fid.persistent_fid,
4936 .volatile_fid = wdata->req->cfile->fid.volatile_fid,
4937 .pid = wdata->req->pid,
4939 io_parms = &_io_parms;
4941 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
4942 (void **) &req, &total_len);
4947 rqst.rq_iter = wdata->subreq.io_iter;
4949 rqst.rq_iov[0].iov_len = total_len - 1;
4950 rqst.rq_iov[0].iov_base = (char *)req;
4953 if (smb3_encryption_required(tcon))
4954 flags |= CIFS_TRANSFORM_REQ;
4956 shdr = (struct smb2_hdr *)req;
4957 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4959 req->PersistentFileId = io_parms->persistent_fid;
4960 req->VolatileFileId = io_parms->volatile_fid;
4961 req->WriteChannelInfoOffset = 0;
4962 req->WriteChannelInfoLength = 0;
4963 req->Channel = SMB2_CHANNEL_NONE;
4964 req->Length = cpu_to_le32(io_parms->length);
4965 req->Offset = cpu_to_le64(io_parms->offset);
4966 req->DataOffset = cpu_to_le16(
4967 offsetof(struct smb2_write_req, Buffer));
4968 req->RemainingBytes = 0;
4970 trace_smb3_write_enter(wdata->rreq->debug_id,
4971 wdata->subreq.debug_index,
4973 io_parms->persistent_fid,
4974 io_parms->tcon->tid,
4975 io_parms->tcon->ses->Suid,
4979 #ifdef CONFIG_CIFS_SMB_DIRECT
4981 * If we want to do a server RDMA read, fill in and append
4982 * smbd_buffer_descriptor_v1 to the end of write request
4984 if (smb3_use_rdma_offload(io_parms)) {
4985 struct smbd_buffer_descriptor_v1 *v1;
4986 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4988 wdata->mr = smbd_register_mr(server->smbd_conn, &wdata->subreq.io_iter,
4989 false, need_invalidate);
4992 goto async_writev_out;
4994 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4995 req->RemainingBytes = req->Length;
4997 req->DataOffset = 0;
4998 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4999 if (need_invalidate)
5000 req->Channel = SMB2_CHANNEL_RDMA_V1;
5001 req->WriteChannelInfoOffset =
5002 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
5003 req->WriteChannelInfoLength =
5004 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
5005 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
5006 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
5007 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
5008 v1->length = cpu_to_le32(wdata->mr->mr->length);
5010 rqst.rq_iov[0].iov_len += sizeof(*v1);
5013 * We keep wdata->subreq.io_iter,
5014 * but we have to truncate rqst.rq_iter
5016 iov_iter_truncate(&rqst.rq_iter, 0);
5020 if (wdata->subreq.retry_count > 0)
5021 smb2_set_replay(server, &rqst);
5023 cifs_dbg(FYI, "async write at %llu %u bytes iter=%zx\n",
5024 io_parms->offset, io_parms->length, iov_iter_count(&wdata->subreq.io_iter));
5026 if (wdata->credits.value > 0) {
5027 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->subreq.len,
5028 SMB2_MAX_BUFFER_SIZE));
5029 credit_request = le16_to_cpu(shdr->CreditCharge) + 8;
5030 if (server->credits >= server->max_credits)
5031 shdr->CreditRequest = cpu_to_le16(0);
5033 shdr->CreditRequest = cpu_to_le16(
5034 min_t(int, server->max_credits -
5035 server->credits, credit_request));
5037 rc = adjust_credits(server, wdata, cifs_trace_rw_credits_call_writev_adjust);
5039 goto async_writev_out;
5041 flags |= CIFS_HAS_CREDITS;
5044 /* XXX: compression + encryption is unsupported for now */
5045 if (((flags & CIFS_TRANSFORM_REQ) != CIFS_TRANSFORM_REQ) && should_compress(tcon, &rqst))
5046 flags |= CIFS_COMPRESS_REQ;
5048 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
5049 wdata, flags, &wdata->credits);
5050 /* Can't touch wdata if rc == 0 */
5052 trace_smb3_write_err(wdata->rreq->debug_id,
5053 wdata->subreq.debug_index,
5055 io_parms->persistent_fid,
5056 io_parms->tcon->tid,
5057 io_parms->tcon->ses->Suid,
5061 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
5065 cifs_small_buf_release(req);
5068 trace_smb3_rw_credits(wdata->rreq->debug_id,
5069 wdata->subreq.debug_index,
5070 wdata->credits.value,
5071 server->credits, server->in_flight,
5072 -(int)wdata->credits.value,
5073 cifs_trace_rw_credits_write_response_clear);
5074 add_credits_and_wake_if(wdata->server, &wdata->credits, 0);
5075 cifs_write_subrequest_terminated(wdata, rc, true);
5080 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
5081 * The length field from io_parms must be at least 1 and indicates a number of
5082 * elements with data to write that begins with position 1 in iov array. All
5083 * data length is specified by count.
5086 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
5087 unsigned int *nbytes, struct kvec *iov, int n_vec)
5089 struct smb_rqst rqst;
5091 struct smb2_write_req *req = NULL;
5092 struct smb2_write_rsp *rsp = NULL;
5094 struct kvec rsp_iov;
5096 unsigned int total_len;
5097 struct TCP_Server_Info *server;
5098 int retries = 0, cur_sleep = 1;
5101 /* reinitialize for possible replay */
5104 if (!io_parms->server)
5105 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
5106 server = io_parms->server;
5108 return -ECONNABORTED;
5113 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
5114 (void **) &req, &total_len);
5118 if (smb3_encryption_required(io_parms->tcon))
5119 flags |= CIFS_TRANSFORM_REQ;
5121 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
5123 req->PersistentFileId = io_parms->persistent_fid;
5124 req->VolatileFileId = io_parms->volatile_fid;
5125 req->WriteChannelInfoOffset = 0;
5126 req->WriteChannelInfoLength = 0;
5128 req->Length = cpu_to_le32(io_parms->length);
5129 req->Offset = cpu_to_le64(io_parms->offset);
5130 req->DataOffset = cpu_to_le16(
5131 offsetof(struct smb2_write_req, Buffer));
5132 req->RemainingBytes = 0;
5134 trace_smb3_write_enter(0, 0, xid, io_parms->persistent_fid,
5135 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
5136 io_parms->offset, io_parms->length);
5138 iov[0].iov_base = (char *)req;
5140 iov[0].iov_len = total_len - 1;
5142 memset(&rqst, 0, sizeof(struct smb_rqst));
5144 rqst.rq_nvec = n_vec + 1;
5147 smb2_set_replay(server, &rqst);
5149 rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
5151 &resp_buftype, flags, &rsp_iov);
5152 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
5155 trace_smb3_write_err(0, 0, xid,
5156 req->PersistentFileId,
5157 io_parms->tcon->tid,
5158 io_parms->tcon->ses->Suid,
5159 io_parms->offset, io_parms->length, rc);
5160 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
5161 cifs_dbg(VFS, "Send error in write = %d\n", rc);
5163 *nbytes = le32_to_cpu(rsp->DataLength);
5164 cifs_stats_bytes_written(io_parms->tcon, *nbytes);
5165 trace_smb3_write_done(0, 0, xid,
5166 req->PersistentFileId,
5167 io_parms->tcon->tid,
5168 io_parms->tcon->ses->Suid,
5169 io_parms->offset, *nbytes);
5172 cifs_small_buf_release(req);
5173 free_rsp_buf(resp_buftype, rsp);
5175 if (is_replayable_error(rc) &&
5176 smb2_should_replay(io_parms->tcon, &retries, &cur_sleep))
5182 int posix_info_sid_size(const void *beg, const void *end)
5190 subauth = *(u8 *)(beg+1);
5191 if (subauth < 1 || subauth > 15)
5194 total = 1 + 1 + 6 + 4*subauth;
5195 if (beg + total > end)
5201 int posix_info_parse(const void *beg, const void *end,
5202 struct smb2_posix_info_parsed *out)
5206 int owner_len, group_len;
5208 const void *owner_sid;
5209 const void *group_sid;
5212 /* if no end bound given, assume payload to be correct */
5214 const struct smb2_posix_info *p = beg;
5216 end = beg + le32_to_cpu(p->NextEntryOffset);
5217 /* last element will have a 0 offset, pick a sensible bound */
5222 /* check base buf */
5223 if (beg + sizeof(struct smb2_posix_info) > end)
5225 total_len = sizeof(struct smb2_posix_info);
5227 /* check owner sid */
5228 owner_sid = beg + total_len;
5229 owner_len = posix_info_sid_size(owner_sid, end);
5232 total_len += owner_len;
5234 /* check group sid */
5235 group_sid = beg + total_len;
5236 group_len = posix_info_sid_size(group_sid, end);
5239 total_len += group_len;
5241 /* check name len */
5242 if (beg + total_len + 4 > end)
5244 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
5245 if (name_len < 1 || name_len > 0xFFFF)
5250 name = beg + total_len;
5251 if (name + name_len > end)
5253 total_len += name_len;
5257 out->size = total_len;
5258 out->name_len = name_len;
5260 memcpy(&out->owner, owner_sid, owner_len);
5261 memcpy(&out->group, group_sid, group_len);
5266 static int posix_info_extra_size(const void *beg, const void *end)
5268 int len = posix_info_parse(beg, end, NULL);
5272 return len - sizeof(struct smb2_posix_info);
5276 num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
5280 unsigned int entrycount = 0;
5281 unsigned int next_offset = 0;
5283 FILE_DIRECTORY_INFO *dir_info;
5285 if (bufstart == NULL)
5288 entryptr = bufstart;
5291 if (entryptr + next_offset < entryptr ||
5292 entryptr + next_offset > end_of_buf ||
5293 entryptr + next_offset + size > end_of_buf) {
5294 cifs_dbg(VFS, "malformed search entry would overflow\n");
5298 entryptr = entryptr + next_offset;
5299 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
5301 if (infotype == SMB_FIND_FILE_POSIX_INFO)
5302 len = posix_info_extra_size(entryptr, end_of_buf);
5304 len = le32_to_cpu(dir_info->FileNameLength);
5307 entryptr + len < entryptr ||
5308 entryptr + len > end_of_buf ||
5309 entryptr + len + size > end_of_buf) {
5310 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
5315 *lastentry = entryptr;
5318 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
5329 int SMB2_query_directory_init(const unsigned int xid,
5330 struct cifs_tcon *tcon,
5331 struct TCP_Server_Info *server,
5332 struct smb_rqst *rqst,
5333 u64 persistent_fid, u64 volatile_fid,
5334 int index, int info_level)
5336 struct smb2_query_directory_req *req;
5337 unsigned char *bufptr;
5338 __le16 asteriks = cpu_to_le16('*');
5339 unsigned int output_size = CIFSMaxBufSize -
5340 MAX_SMB2_CREATE_RESPONSE_SIZE -
5341 MAX_SMB2_CLOSE_RESPONSE_SIZE;
5342 unsigned int total_len;
5343 struct kvec *iov = rqst->rq_iov;
5346 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
5347 (void **) &req, &total_len);
5351 switch (info_level) {
5352 case SMB_FIND_FILE_DIRECTORY_INFO:
5353 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
5355 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
5356 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
5358 case SMB_FIND_FILE_POSIX_INFO:
5359 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
5361 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
5362 req->FileInformationClass = FILE_FULL_DIRECTORY_INFORMATION;
5365 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
5370 req->FileIndex = cpu_to_le32(index);
5371 req->PersistentFileId = persistent_fid;
5372 req->VolatileFileId = volatile_fid;
5375 bufptr = req->Buffer;
5376 memcpy(bufptr, &asteriks, len);
5378 req->FileNameOffset =
5379 cpu_to_le16(sizeof(struct smb2_query_directory_req));
5380 req->FileNameLength = cpu_to_le16(len);
5382 * BB could be 30 bytes or so longer if we used SMB2 specific
5383 * buffer lengths, but this is safe and close enough.
5385 output_size = min_t(unsigned int, output_size, server->maxBuf);
5386 output_size = min_t(unsigned int, output_size, 2 << 15);
5387 req->OutputBufferLength = cpu_to_le32(output_size);
5389 iov[0].iov_base = (char *)req;
5391 iov[0].iov_len = total_len - 1;
5393 iov[1].iov_base = (char *)(req->Buffer);
5394 iov[1].iov_len = len;
5396 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
5397 tcon->ses->Suid, index, output_size);
5402 void SMB2_query_directory_free(struct smb_rqst *rqst)
5404 if (rqst && rqst->rq_iov) {
5405 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
5410 smb2_parse_query_directory(struct cifs_tcon *tcon,
5411 struct kvec *rsp_iov,
5413 struct cifs_search_info *srch_inf)
5415 struct smb2_query_directory_rsp *rsp;
5416 size_t info_buf_size;
5420 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
5422 switch (srch_inf->info_level) {
5423 case SMB_FIND_FILE_DIRECTORY_INFO:
5424 info_buf_size = sizeof(FILE_DIRECTORY_INFO);
5426 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
5427 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO);
5429 case SMB_FIND_FILE_POSIX_INFO:
5430 /* note that posix payload are variable size */
5431 info_buf_size = sizeof(struct smb2_posix_info);
5433 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
5434 info_buf_size = sizeof(FILE_FULL_DIRECTORY_INFO);
5437 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
5438 srch_inf->info_level);
5442 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5443 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
5446 cifs_tcon_dbg(VFS, "bad info payload");
5450 srch_inf->unicode = true;
5452 if (srch_inf->ntwrk_buf_start) {
5453 if (srch_inf->smallBuf)
5454 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
5456 cifs_buf_release(srch_inf->ntwrk_buf_start);
5458 srch_inf->ntwrk_buf_start = (char *)rsp;
5459 srch_inf->srch_entries_start = srch_inf->last_entry =
5460 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
5461 end_of_smb = rsp_iov->iov_len + (char *)rsp;
5463 srch_inf->entries_in_buffer = num_entries(
5464 srch_inf->info_level,
5465 srch_inf->srch_entries_start,
5467 &srch_inf->last_entry,
5470 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
5471 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
5472 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
5473 srch_inf->srch_entries_start, srch_inf->last_entry);
5474 if (resp_buftype == CIFS_LARGE_BUFFER)
5475 srch_inf->smallBuf = false;
5476 else if (resp_buftype == CIFS_SMALL_BUFFER)
5477 srch_inf->smallBuf = true;
5479 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
5485 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
5486 u64 persistent_fid, u64 volatile_fid, int index,
5487 struct cifs_search_info *srch_inf)
5489 struct smb_rqst rqst;
5490 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
5491 struct smb2_query_directory_rsp *rsp = NULL;
5492 int resp_buftype = CIFS_NO_BUFFER;
5493 struct kvec rsp_iov;
5495 struct cifs_ses *ses = tcon->ses;
5496 struct TCP_Server_Info *server;
5498 int retries = 0, cur_sleep = 1;
5501 /* reinitialize for possible replay */
5503 server = cifs_pick_channel(ses);
5505 if (!ses || !(ses->server))
5508 if (smb3_encryption_required(tcon))
5509 flags |= CIFS_TRANSFORM_REQ;
5511 memset(&rqst, 0, sizeof(struct smb_rqst));
5512 memset(&iov, 0, sizeof(iov));
5514 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
5516 rc = SMB2_query_directory_init(xid, tcon, server,
5517 &rqst, persistent_fid,
5518 volatile_fid, index,
5519 srch_inf->info_level);
5524 smb2_set_replay(server, &rqst);
5526 rc = cifs_send_recv(xid, ses, server,
5527 &rqst, &resp_buftype, flags, &rsp_iov);
5528 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
5531 if (rc == -ENODATA &&
5532 rsp->hdr.Status == STATUS_NO_MORE_FILES) {
5533 trace_smb3_query_dir_done(xid, persistent_fid,
5534 tcon->tid, tcon->ses->Suid, index, 0);
5535 srch_inf->endOfSearch = true;
5538 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5539 tcon->ses->Suid, index, 0, rc);
5540 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
5545 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
5548 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5549 tcon->ses->Suid, index, 0, rc);
5552 resp_buftype = CIFS_NO_BUFFER;
5554 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
5555 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
5558 SMB2_query_directory_free(&rqst);
5559 free_rsp_buf(resp_buftype, rsp);
5561 if (is_replayable_error(rc) &&
5562 smb2_should_replay(tcon, &retries, &cur_sleep))
5569 SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
5570 struct smb_rqst *rqst,
5571 u64 persistent_fid, u64 volatile_fid, u32 pid,
5572 u8 info_class, u8 info_type, u32 additional_info,
5573 void **data, unsigned int *size)
5575 struct smb2_set_info_req *req;
5576 struct kvec *iov = rqst->rq_iov;
5577 unsigned int i, total_len;
5580 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
5581 (void **) &req, &total_len);
5585 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
5586 req->InfoType = info_type;
5587 req->FileInfoClass = info_class;
5588 req->PersistentFileId = persistent_fid;
5589 req->VolatileFileId = volatile_fid;
5590 req->AdditionalInformation = cpu_to_le32(additional_info);
5592 req->BufferOffset = cpu_to_le16(sizeof(struct smb2_set_info_req));
5593 req->BufferLength = cpu_to_le32(*size);
5595 memcpy(req->Buffer, *data, *size);
5598 iov[0].iov_base = (char *)req;
5600 iov[0].iov_len = total_len - 1;
5602 for (i = 1; i < rqst->rq_nvec; i++) {
5603 le32_add_cpu(&req->BufferLength, size[i]);
5604 iov[i].iov_base = (char *)data[i];
5605 iov[i].iov_len = size[i];
5612 SMB2_set_info_free(struct smb_rqst *rqst)
5614 if (rqst && rqst->rq_iov)
5615 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
5619 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
5620 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
5621 u8 info_type, u32 additional_info, unsigned int num,
5622 void **data, unsigned int *size)
5624 struct smb_rqst rqst;
5625 struct smb2_set_info_rsp *rsp = NULL;
5627 struct kvec rsp_iov;
5630 struct cifs_ses *ses = tcon->ses;
5631 struct TCP_Server_Info *server;
5633 int retries = 0, cur_sleep = 1;
5636 /* reinitialize for possible replay */
5638 server = cifs_pick_channel(ses);
5640 if (!ses || !server)
5646 if (smb3_encryption_required(tcon))
5647 flags |= CIFS_TRANSFORM_REQ;
5649 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
5653 memset(&rqst, 0, sizeof(struct smb_rqst));
5657 rc = SMB2_set_info_init(tcon, server,
5658 &rqst, persistent_fid, volatile_fid, pid,
5659 info_class, info_type, additional_info,
5667 smb2_set_replay(server, &rqst);
5669 rc = cifs_send_recv(xid, ses, server,
5670 &rqst, &resp_buftype, flags,
5672 SMB2_set_info_free(&rqst);
5673 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
5676 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
5677 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
5678 ses->Suid, info_class, (__u32)info_type, rc);
5681 free_rsp_buf(resp_buftype, rsp);
5684 if (is_replayable_error(rc) &&
5685 smb2_should_replay(tcon, &retries, &cur_sleep))
5692 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
5693 u64 volatile_fid, u32 pid, loff_t new_eof)
5695 struct smb2_file_eof_info info;
5699 info.EndOfFile = cpu_to_le64(new_eof);
5702 size = sizeof(struct smb2_file_eof_info);
5704 trace_smb3_set_eof(xid, persistent_fid, tcon->tid, tcon->ses->Suid, new_eof);
5706 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5707 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
5708 0, 1, &data, &size);
5712 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
5713 u64 persistent_fid, u64 volatile_fid,
5714 struct smb_ntsd *pnntsd, int pacllen, int aclflag)
5716 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5717 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
5718 1, (void **)&pnntsd, &pacllen);
5722 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
5723 u64 persistent_fid, u64 volatile_fid,
5724 struct smb2_file_full_ea_info *buf, int len)
5726 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5727 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
5728 0, 1, (void **)&buf, &len);
5732 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
5733 const u64 persistent_fid, const u64 volatile_fid,
5736 struct smb_rqst rqst;
5738 struct smb2_oplock_break *req = NULL;
5739 struct cifs_ses *ses = tcon->ses;
5740 struct TCP_Server_Info *server;
5741 int flags = CIFS_OBREAK_OP;
5742 unsigned int total_len;
5744 struct kvec rsp_iov;
5746 int retries = 0, cur_sleep = 1;
5749 /* reinitialize for possible replay */
5750 flags = CIFS_OBREAK_OP;
5751 server = cifs_pick_channel(ses);
5753 cifs_dbg(FYI, "SMB2_oplock_break\n");
5754 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5755 (void **) &req, &total_len);
5759 if (smb3_encryption_required(tcon))
5760 flags |= CIFS_TRANSFORM_REQ;
5762 req->VolatileFid = volatile_fid;
5763 req->PersistentFid = persistent_fid;
5764 req->OplockLevel = oplock_level;
5765 req->hdr.CreditRequest = cpu_to_le16(1);
5767 flags |= CIFS_NO_RSP_BUF;
5769 iov[0].iov_base = (char *)req;
5770 iov[0].iov_len = total_len;
5772 memset(&rqst, 0, sizeof(struct smb_rqst));
5777 smb2_set_replay(server, &rqst);
5779 rc = cifs_send_recv(xid, ses, server,
5780 &rqst, &resp_buf_type, flags, &rsp_iov);
5781 cifs_small_buf_release(req);
5783 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5784 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
5787 if (is_replayable_error(rc) &&
5788 smb2_should_replay(tcon, &retries, &cur_sleep))
5795 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
5796 struct kstatfs *kst)
5798 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
5799 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
5800 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
5801 kst->f_bfree = kst->f_bavail =
5802 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
5807 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
5808 struct kstatfs *kst)
5810 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
5811 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
5812 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
5813 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
5814 kst->f_bavail = kst->f_bfree;
5816 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
5817 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5818 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
5819 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5820 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
5826 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
5827 struct TCP_Server_Info *server,
5828 int level, int outbuf_len, u64 persistent_fid,
5832 struct smb2_query_info_req *req;
5833 unsigned int total_len;
5835 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
5837 if ((tcon->ses == NULL) || server == NULL)
5840 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
5841 (void **) &req, &total_len);
5845 req->InfoType = SMB2_O_INFO_FILESYSTEM;
5846 req->FileInfoClass = level;
5847 req->PersistentFileId = persistent_fid;
5848 req->VolatileFileId = volatile_fid;
5850 req->InputBufferOffset =
5851 cpu_to_le16(sizeof(struct smb2_query_info_req));
5852 req->OutputBufferLength = cpu_to_le32(
5853 outbuf_len + sizeof(struct smb2_query_info_rsp));
5855 iov->iov_base = (char *)req;
5856 iov->iov_len = total_len;
5860 static inline void free_qfs_info_req(struct kvec *iov)
5862 cifs_buf_release(iov->iov_base);
5866 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
5867 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5869 struct smb_rqst rqst;
5870 struct smb2_query_info_rsp *rsp = NULL;
5872 struct kvec rsp_iov;
5875 struct cifs_ses *ses = tcon->ses;
5876 struct TCP_Server_Info *server;
5877 FILE_SYSTEM_POSIX_INFO *info = NULL;
5879 int retries = 0, cur_sleep = 1;
5882 /* reinitialize for possible replay */
5884 server = cifs_pick_channel(ses);
5886 rc = build_qfs_info_req(&iov, tcon, server,
5887 FS_POSIX_INFORMATION,
5888 sizeof(FILE_SYSTEM_POSIX_INFO),
5889 persistent_fid, volatile_fid);
5893 if (smb3_encryption_required(tcon))
5894 flags |= CIFS_TRANSFORM_REQ;
5896 memset(&rqst, 0, sizeof(struct smb_rqst));
5901 smb2_set_replay(server, &rqst);
5903 rc = cifs_send_recv(xid, ses, server,
5904 &rqst, &resp_buftype, flags, &rsp_iov);
5905 free_qfs_info_req(&iov);
5907 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5908 goto posix_qfsinf_exit;
5910 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5912 info = (FILE_SYSTEM_POSIX_INFO *)(
5913 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5914 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5915 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5916 sizeof(FILE_SYSTEM_POSIX_INFO));
5918 copy_posix_fs_info_to_kstatfs(info, fsdata);
5921 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5923 if (is_replayable_error(rc) &&
5924 smb2_should_replay(tcon, &retries, &cur_sleep))
5931 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5932 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5934 struct smb_rqst rqst;
5935 struct smb2_query_info_rsp *rsp = NULL;
5937 struct kvec rsp_iov;
5940 struct cifs_ses *ses = tcon->ses;
5941 struct TCP_Server_Info *server;
5942 struct smb2_fs_full_size_info *info = NULL;
5944 int retries = 0, cur_sleep = 1;
5947 /* reinitialize for possible replay */
5949 server = cifs_pick_channel(ses);
5951 rc = build_qfs_info_req(&iov, tcon, server,
5952 FS_FULL_SIZE_INFORMATION,
5953 sizeof(struct smb2_fs_full_size_info),
5954 persistent_fid, volatile_fid);
5958 if (smb3_encryption_required(tcon))
5959 flags |= CIFS_TRANSFORM_REQ;
5961 memset(&rqst, 0, sizeof(struct smb_rqst));
5966 smb2_set_replay(server, &rqst);
5968 rc = cifs_send_recv(xid, ses, server,
5969 &rqst, &resp_buftype, flags, &rsp_iov);
5970 free_qfs_info_req(&iov);
5972 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5975 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5977 info = (struct smb2_fs_full_size_info *)(
5978 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5979 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5980 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5981 sizeof(struct smb2_fs_full_size_info));
5983 smb2_copy_fs_info_to_kstatfs(info, fsdata);
5986 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5988 if (is_replayable_error(rc) &&
5989 smb2_should_replay(tcon, &retries, &cur_sleep))
5996 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
5997 u64 persistent_fid, u64 volatile_fid, int level)
5999 struct smb_rqst rqst;
6000 struct smb2_query_info_rsp *rsp = NULL;
6002 struct kvec rsp_iov;
6004 int resp_buftype, max_len, min_len;
6005 struct cifs_ses *ses = tcon->ses;
6006 struct TCP_Server_Info *server;
6007 unsigned int rsp_len, offset;
6009 int retries = 0, cur_sleep = 1;
6012 /* reinitialize for possible replay */
6014 server = cifs_pick_channel(ses);
6016 if (level == FS_DEVICE_INFORMATION) {
6017 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
6018 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
6019 } else if (level == FS_ATTRIBUTE_INFORMATION) {
6020 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
6021 min_len = MIN_FS_ATTR_INFO_SIZE;
6022 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
6023 max_len = sizeof(struct smb3_fs_ss_info);
6024 min_len = sizeof(struct smb3_fs_ss_info);
6025 } else if (level == FS_VOLUME_INFORMATION) {
6026 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
6027 min_len = sizeof(struct smb3_fs_vol_info);
6029 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
6033 rc = build_qfs_info_req(&iov, tcon, server,
6035 persistent_fid, volatile_fid);
6039 if (smb3_encryption_required(tcon))
6040 flags |= CIFS_TRANSFORM_REQ;
6042 memset(&rqst, 0, sizeof(struct smb_rqst));
6047 smb2_set_replay(server, &rqst);
6049 rc = cifs_send_recv(xid, ses, server,
6050 &rqst, &resp_buftype, flags, &rsp_iov);
6051 free_qfs_info_req(&iov);
6053 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
6056 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
6058 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
6059 offset = le16_to_cpu(rsp->OutputBufferOffset);
6060 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
6064 if (level == FS_ATTRIBUTE_INFORMATION)
6065 memcpy(&tcon->fsAttrInfo, offset
6066 + (char *)rsp, min_t(unsigned int,
6068 else if (level == FS_DEVICE_INFORMATION)
6069 memcpy(&tcon->fsDevInfo, offset
6070 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
6071 else if (level == FS_SECTOR_SIZE_INFORMATION) {
6072 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
6073 (offset + (char *)rsp);
6074 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
6075 tcon->perf_sector_size =
6076 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
6077 } else if (level == FS_VOLUME_INFORMATION) {
6078 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
6079 (offset + (char *)rsp);
6080 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
6081 tcon->vol_create_time = vol_info->VolumeCreationTime;
6085 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
6087 if (is_replayable_error(rc) &&
6088 smb2_should_replay(tcon, &retries, &cur_sleep))
6095 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
6096 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
6097 const __u32 num_lock, struct smb2_lock_element *buf)
6099 struct smb_rqst rqst;
6101 struct smb2_lock_req *req = NULL;
6103 struct kvec rsp_iov;
6106 int flags = CIFS_NO_RSP_BUF;
6107 unsigned int total_len;
6108 struct TCP_Server_Info *server;
6109 int retries = 0, cur_sleep = 1;
6112 /* reinitialize for possible replay */
6113 flags = CIFS_NO_RSP_BUF;
6114 server = cifs_pick_channel(tcon->ses);
6116 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
6118 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
6119 (void **) &req, &total_len);
6123 if (smb3_encryption_required(tcon))
6124 flags |= CIFS_TRANSFORM_REQ;
6126 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
6127 req->LockCount = cpu_to_le16(num_lock);
6129 req->PersistentFileId = persist_fid;
6130 req->VolatileFileId = volatile_fid;
6132 count = num_lock * sizeof(struct smb2_lock_element);
6134 iov[0].iov_base = (char *)req;
6135 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
6136 iov[1].iov_base = (char *)buf;
6137 iov[1].iov_len = count;
6139 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
6141 memset(&rqst, 0, sizeof(struct smb_rqst));
6146 smb2_set_replay(server, &rqst);
6148 rc = cifs_send_recv(xid, tcon->ses, server,
6149 &rqst, &resp_buf_type, flags,
6151 cifs_small_buf_release(req);
6153 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
6154 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
6155 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
6156 tcon->ses->Suid, rc);
6159 if (is_replayable_error(rc) &&
6160 smb2_should_replay(tcon, &retries, &cur_sleep))
6167 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
6168 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
6169 const __u64 length, const __u64 offset, const __u32 lock_flags,
6172 struct smb2_lock_element lock;
6174 lock.Offset = cpu_to_le64(offset);
6175 lock.Length = cpu_to_le64(length);
6176 lock.Flags = cpu_to_le32(lock_flags);
6177 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
6178 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
6180 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
6184 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
6185 __u8 *lease_key, const __le32 lease_state)
6187 struct smb_rqst rqst;
6189 struct smb2_lease_ack *req = NULL;
6190 struct cifs_ses *ses = tcon->ses;
6191 int flags = CIFS_OBREAK_OP;
6192 unsigned int total_len;
6194 struct kvec rsp_iov;
6196 __u64 *please_key_high;
6197 __u64 *please_key_low;
6198 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
6200 cifs_dbg(FYI, "SMB2_lease_break\n");
6201 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
6202 (void **) &req, &total_len);
6206 if (smb3_encryption_required(tcon))
6207 flags |= CIFS_TRANSFORM_REQ;
6209 req->hdr.CreditRequest = cpu_to_le16(1);
6210 req->StructureSize = cpu_to_le16(36);
6213 memcpy(req->LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
6214 req->LeaseState = lease_state;
6216 flags |= CIFS_NO_RSP_BUF;
6218 iov[0].iov_base = (char *)req;
6219 iov[0].iov_len = total_len;
6221 memset(&rqst, 0, sizeof(struct smb_rqst));
6225 rc = cifs_send_recv(xid, ses, server,
6226 &rqst, &resp_buf_type, flags, &rsp_iov);
6227 cifs_small_buf_release(req);
6229 please_key_low = (__u64 *)lease_key;
6230 please_key_high = (__u64 *)(lease_key+8);
6232 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
6233 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
6234 ses->Suid, *please_key_low, *please_key_high, rc);
6235 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
6237 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
6238 ses->Suid, *please_key_low, *please_key_high);