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 "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"
47 * The following table defines the expected "StructureSize" of SMB2 requests
48 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
50 * Note that commands are defined in smb2pdu.h in le16 but the array below is
51 * indexed by command in host byte order.
53 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
54 /* SMB2_NEGOTIATE */ 36,
55 /* SMB2_SESSION_SETUP */ 25,
57 /* SMB2_TREE_CONNECT */ 9,
58 /* SMB2_TREE_DISCONNECT */ 4,
68 /* SMB2_QUERY_DIRECTORY */ 33,
69 /* SMB2_CHANGE_NOTIFY */ 32,
70 /* SMB2_QUERY_INFO */ 41,
71 /* SMB2_SET_INFO */ 33,
72 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
75 int smb3_encryption_required(const struct cifs_tcon *tcon)
77 if (!tcon || !tcon->ses)
79 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
80 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
83 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
89 smb2_hdr_assemble(struct smb2_hdr *shdr, __le16 smb2_cmd,
90 const struct cifs_tcon *tcon,
91 struct TCP_Server_Info *server)
93 struct smb3_hdr_req *smb3_hdr;
95 shdr->ProtocolId = SMB2_PROTO_NUMBER;
96 shdr->StructureSize = cpu_to_le16(64);
97 shdr->Command = smb2_cmd;
100 /* After reconnect SMB3 must set ChannelSequence on subsequent reqs */
101 if (server->dialect >= SMB30_PROT_ID) {
102 smb3_hdr = (struct smb3_hdr_req *)shdr;
104 * if primary channel is not set yet, use default
105 * channel for chan sequence num
107 if (SERVER_IS_CHAN(server))
108 smb3_hdr->ChannelSequence =
109 cpu_to_le16(server->primary_server->channel_sequence_num);
111 smb3_hdr->ChannelSequence =
112 cpu_to_le16(server->channel_sequence_num);
114 spin_lock(&server->req_lock);
115 /* Request up to 10 credits but don't go over the limit. */
116 if (server->credits >= server->max_credits)
117 shdr->CreditRequest = cpu_to_le16(0);
119 shdr->CreditRequest = cpu_to_le16(
120 min_t(int, server->max_credits -
121 server->credits, 10));
122 spin_unlock(&server->req_lock);
124 shdr->CreditRequest = cpu_to_le16(2);
126 shdr->Id.SyncId.ProcessId = cpu_to_le32((__u16)current->tgid);
131 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
132 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
133 if (server && (server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
134 shdr->CreditCharge = cpu_to_le16(1);
135 /* else CreditCharge MBZ */
137 shdr->Id.SyncId.TreeId = cpu_to_le32(tcon->tid);
138 /* Uid is not converted */
140 shdr->SessionId = cpu_to_le64(tcon->ses->Suid);
143 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
144 * to pass the path on the Open SMB prefixed by \\server\share.
145 * Not sure when we would need to do the augmented path (if ever) and
146 * setting this flag breaks the SMB2 open operation since it is
147 * illegal to send an empty path name (without \\server\share prefix)
148 * when the DFS flag is set in the SMB open header. We could
149 * consider setting the flag on all operations other than open
150 * but it is safer to net set it for now.
152 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
153 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
155 if (server && server->sign && !smb3_encryption_required(tcon))
156 shdr->Flags |= SMB2_FLAGS_SIGNED;
161 /* helper function for code reuse */
163 cifs_chan_skip_or_disable(struct cifs_ses *ses,
164 struct TCP_Server_Info *server,
167 struct TCP_Server_Info *pserver;
168 unsigned int chan_index;
170 if (SERVER_IS_CHAN(server)) {
172 "server %s does not support multichannel anymore. Skip secondary channel\n",
173 ses->server->hostname);
175 spin_lock(&ses->chan_lock);
176 chan_index = cifs_ses_get_chan_index(ses, server);
177 if (chan_index == CIFS_INVAL_CHAN_INDEX) {
178 spin_unlock(&ses->chan_lock);
182 ses->chans[chan_index].server = NULL;
183 server->terminate = true;
184 spin_unlock(&ses->chan_lock);
187 * the above reference of server by channel
188 * needs to be dropped without holding chan_lock
189 * as cifs_put_tcp_session takes a higher lock
190 * i.e. cifs_tcp_ses_lock
192 cifs_put_tcp_session(server, from_reconnect);
194 cifs_signal_cifsd_for_reconnect(server, false);
196 /* mark primary server as needing reconnect */
197 pserver = server->primary_server;
198 cifs_signal_cifsd_for_reconnect(pserver, false);
204 "server does not support multichannel anymore. Disable all other channels\n");
205 cifs_disable_secondary_channels(ses);
212 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
213 struct TCP_Server_Info *server, bool from_reconnect)
216 struct nls_table *nls_codepage = NULL;
217 struct cifs_ses *ses;
221 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
222 * check for tcp and smb session status done differently
223 * for those three - in the calling routine.
229 * Need to also skip SMB2_IOCTL because it is used for checking nested dfs links in
230 * cifs_tree_connect().
232 if (smb2_command == SMB2_TREE_CONNECT || smb2_command == SMB2_IOCTL)
235 spin_lock(&tcon->tc_lock);
236 if (tcon->status == TID_EXITING) {
238 * only tree disconnect allowed when disconnecting ...
240 if (smb2_command != SMB2_TREE_DISCONNECT) {
241 spin_unlock(&tcon->tc_lock);
242 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
247 spin_unlock(&tcon->tc_lock);
252 spin_lock(&ses->ses_lock);
253 if (ses->ses_status == SES_EXITING) {
254 spin_unlock(&ses->ses_lock);
257 spin_unlock(&ses->ses_lock);
258 if (!ses->server || !server)
261 spin_lock(&server->srv_lock);
262 if (server->tcpStatus == CifsNeedReconnect) {
264 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
265 * here since they are implicitly done when session drops.
267 switch (smb2_command) {
269 * BB Should we keep oplock break and add flush to exceptions?
271 case SMB2_TREE_DISCONNECT:
274 case SMB2_OPLOCK_BREAK:
275 spin_unlock(&server->srv_lock);
280 /* if server is marked for termination, cifsd will cleanup */
281 if (server->terminate) {
282 spin_unlock(&server->srv_lock);
285 spin_unlock(&server->srv_lock);
288 rc = cifs_wait_for_server_reconnect(server, tcon->retry);
292 spin_lock(&ses->chan_lock);
293 if (!cifs_chan_needs_reconnect(ses, server) && !tcon->need_reconnect) {
294 spin_unlock(&ses->chan_lock);
297 spin_unlock(&ses->chan_lock);
298 cifs_dbg(FYI, "sess reconnect mask: 0x%lx, tcon reconnect: %d",
299 tcon->ses->chans_need_reconnect,
300 tcon->need_reconnect);
302 mutex_lock(&ses->session_mutex);
304 * if this is called by delayed work, and the channel has been disabled
305 * in parallel, the delayed work can continue to execute in parallel
306 * there's a chance that this channel may not exist anymore
308 spin_lock(&server->srv_lock);
309 if (server->tcpStatus == CifsExiting) {
310 spin_unlock(&server->srv_lock);
311 mutex_unlock(&ses->session_mutex);
317 * Recheck after acquire mutex. If another thread is negotiating
318 * and the server never sends an answer the socket will be closed
319 * and tcpStatus set to reconnect.
321 if (server->tcpStatus == CifsNeedReconnect) {
322 spin_unlock(&server->srv_lock);
323 mutex_unlock(&ses->session_mutex);
331 spin_unlock(&server->srv_lock);
333 nls_codepage = ses->local_nls;
336 * need to prevent multiple threads trying to simultaneously
337 * reconnect the same SMB session
339 spin_lock(&ses->ses_lock);
340 spin_lock(&ses->chan_lock);
341 if (!cifs_chan_needs_reconnect(ses, server) &&
342 ses->ses_status == SES_GOOD) {
343 spin_unlock(&ses->chan_lock);
344 spin_unlock(&ses->ses_lock);
345 /* this means that we only need to tree connect */
346 if (tcon->need_reconnect)
347 goto skip_sess_setup;
349 mutex_unlock(&ses->session_mutex);
352 spin_unlock(&ses->chan_lock);
353 spin_unlock(&ses->ses_lock);
355 rc = cifs_negotiate_protocol(0, ses, server);
358 * if server stopped supporting multichannel
359 * and the first channel reconnected, disable all the others.
361 if (ses->chan_count > 1 &&
362 !(server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
363 rc = cifs_chan_skip_or_disable(ses, server,
366 mutex_unlock(&ses->session_mutex);
371 rc = cifs_setup_session(0, ses, server, nls_codepage);
372 if ((rc == -EACCES) || (rc == -EKEYEXPIRED) || (rc == -EKEYREVOKED)) {
374 * Try alternate password for next reconnect (key rotation
375 * could be enabled on the server e.g.) if an alternate
376 * password is available and the current password is expired,
377 * but do not swap on non pwd related errors like host down
380 swap(ses->password2, ses->password);
383 if ((rc == -EACCES) && !tcon->retry) {
384 mutex_unlock(&ses->session_mutex);
388 mutex_unlock(&ses->session_mutex);
392 mutex_unlock(&ses->session_mutex);
397 if (!tcon->need_reconnect) {
398 mutex_unlock(&ses->session_mutex);
401 cifs_mark_open_files_invalid(tcon);
402 if (tcon->use_persistent)
403 tcon->need_reopen_files = true;
405 rc = cifs_tree_connect(0, tcon, nls_codepage);
407 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
409 /* If sess reconnected but tcon didn't, something strange ... */
410 mutex_unlock(&ses->session_mutex);
411 cifs_dbg(VFS, "reconnect tcon failed rc = %d\n", rc);
415 spin_lock(&ses->ses_lock);
416 if (ses->flags & CIFS_SES_FLAG_SCALE_CHANNELS) {
417 spin_unlock(&ses->ses_lock);
418 mutex_unlock(&ses->session_mutex);
419 goto skip_add_channels;
421 ses->flags |= CIFS_SES_FLAG_SCALE_CHANNELS;
422 spin_unlock(&ses->ses_lock);
425 (server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL) &&
426 server->ops->query_server_interfaces) {
427 mutex_unlock(&ses->session_mutex);
430 * query server network interfaces, in case they change
433 rc = server->ops->query_server_interfaces(xid, tcon, false);
436 if (rc == -EOPNOTSUPP && ses->chan_count > 1) {
438 * some servers like Azure SMB server do not advertise
439 * that multichannel has been disabled with server
440 * capabilities, rather return STATUS_NOT_IMPLEMENTED.
441 * treat this as server not supporting multichannel
444 rc = cifs_chan_skip_or_disable(ses, server,
446 goto skip_add_channels;
448 cifs_dbg(FYI, "%s: failed to query server interfaces: %d\n",
451 if (ses->chan_max > ses->chan_count &&
453 !SERVER_IS_CHAN(server)) {
454 if (ses->chan_count == 1) {
455 cifs_server_dbg(VFS, "supports multichannel now\n");
456 queue_delayed_work(cifsiod_wq, &tcon->query_interfaces,
457 (SMB_INTERFACE_POLL_INTERVAL * HZ));
460 cifs_try_adding_channels(ses);
463 mutex_unlock(&ses->session_mutex);
467 spin_lock(&ses->ses_lock);
468 ses->flags &= ~CIFS_SES_FLAG_SCALE_CHANNELS;
469 spin_unlock(&ses->ses_lock);
471 if (smb2_command != SMB2_INTERNAL_CMD)
472 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
474 atomic_inc(&tconInfoReconnectCount);
477 * Check if handle based operation so we know whether we can continue
478 * or not without returning to caller to reset file handle.
481 * BB Is flush done by server on drop of tcp session? Should we special
482 * case it and skip above?
484 switch (smb2_command) {
489 case SMB2_QUERY_DIRECTORY:
490 case SMB2_CHANGE_NOTIFY:
491 case SMB2_QUERY_INFO:
500 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon,
501 struct TCP_Server_Info *server,
503 unsigned int *total_len)
505 struct smb2_pdu *spdu = buf;
506 /* lookup word count ie StructureSize from table */
507 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
510 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
511 * largest operations (Create)
515 smb2_hdr_assemble(&spdu->hdr, smb2_command, tcon, server);
516 spdu->StructureSize2 = cpu_to_le16(parmsize);
518 *total_len = parmsize + sizeof(struct smb2_hdr);
522 * Allocate and return pointer to an SMB request hdr, and set basic
523 * SMB information in the SMB header. If the return code is zero, this
524 * function must have filled in request_buf pointer.
526 static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
527 struct TCP_Server_Info *server,
528 void **request_buf, unsigned int *total_len)
530 /* BB eventually switch this to SMB2 specific small buf size */
531 switch (smb2_command) {
533 case SMB2_QUERY_INFO:
534 *request_buf = cifs_buf_get();
537 *request_buf = cifs_small_buf_get();
540 if (*request_buf == NULL) {
541 /* BB should we add a retry in here if not a writepage? */
545 fill_small_buf(smb2_command, tcon, server,
546 (struct smb2_hdr *)(*request_buf),
550 uint16_t com_code = le16_to_cpu(smb2_command);
551 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
552 cifs_stats_inc(&tcon->num_smbs_sent);
558 static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
559 struct TCP_Server_Info *server,
560 void **request_buf, unsigned int *total_len)
564 rc = smb2_reconnect(smb2_command, tcon, server, false);
568 return __smb2_plain_req_init(smb2_command, tcon, server, request_buf,
572 static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
573 struct TCP_Server_Info *server,
574 void **request_buf, unsigned int *total_len)
576 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
577 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
578 return __smb2_plain_req_init(SMB2_IOCTL, tcon, server,
579 request_buf, total_len);
581 return smb2_plain_req_init(SMB2_IOCTL, tcon, server,
582 request_buf, total_len);
585 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
588 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
590 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
591 pneg_ctxt->DataLength = cpu_to_le16(38);
592 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
593 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
594 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
595 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
599 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
601 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
602 pneg_ctxt->DataLength =
603 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
604 - sizeof(struct smb2_neg_context));
605 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
606 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
607 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
608 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
612 build_signing_ctxt(struct smb2_signing_capabilities *pneg_ctxt)
614 unsigned int ctxt_len = sizeof(struct smb2_signing_capabilities);
615 unsigned short num_algs = 1; /* number of signing algorithms sent */
617 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
619 * Context Data length must be rounded to multiple of 8 for some servers
621 pneg_ctxt->DataLength = cpu_to_le16(ALIGN(sizeof(struct smb2_signing_capabilities) -
622 sizeof(struct smb2_neg_context) +
623 (num_algs * sizeof(u16)), 8));
624 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(num_algs);
625 pneg_ctxt->SigningAlgorithms[0] = cpu_to_le16(SIGNING_ALG_AES_CMAC);
627 ctxt_len += sizeof(__le16) * num_algs;
628 ctxt_len = ALIGN(ctxt_len, 8);
630 /* TBD add SIGNING_ALG_AES_GMAC and/or SIGNING_ALG_HMAC_SHA256 */
634 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
636 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
637 if (require_gcm_256) {
638 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + 1 cipher */
639 pneg_ctxt->CipherCount = cpu_to_le16(1);
640 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES256_GCM;
641 } else if (enable_gcm_256) {
642 pneg_ctxt->DataLength = cpu_to_le16(8); /* Cipher Count + 3 ciphers */
643 pneg_ctxt->CipherCount = cpu_to_le16(3);
644 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
645 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES256_GCM;
646 pneg_ctxt->Ciphers[2] = SMB2_ENCRYPTION_AES128_CCM;
648 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + 2 ciphers */
649 pneg_ctxt->CipherCount = cpu_to_le16(2);
650 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
651 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
656 build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
658 struct nls_table *cp = load_nls_default();
660 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
662 /* copy up to max of first 100 bytes of server name to NetName field */
663 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
664 /* context size is DataLength + minimal smb2_neg_context */
665 return ALIGN(le16_to_cpu(pneg_ctxt->DataLength) + sizeof(struct smb2_neg_context), 8);
669 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
671 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
672 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
673 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
674 pneg_ctxt->Name[0] = 0x93;
675 pneg_ctxt->Name[1] = 0xAD;
676 pneg_ctxt->Name[2] = 0x25;
677 pneg_ctxt->Name[3] = 0x50;
678 pneg_ctxt->Name[4] = 0x9C;
679 pneg_ctxt->Name[5] = 0xB4;
680 pneg_ctxt->Name[6] = 0x11;
681 pneg_ctxt->Name[7] = 0xE7;
682 pneg_ctxt->Name[8] = 0xB4;
683 pneg_ctxt->Name[9] = 0x23;
684 pneg_ctxt->Name[10] = 0x83;
685 pneg_ctxt->Name[11] = 0xDE;
686 pneg_ctxt->Name[12] = 0x96;
687 pneg_ctxt->Name[13] = 0x8B;
688 pneg_ctxt->Name[14] = 0xCD;
689 pneg_ctxt->Name[15] = 0x7C;
693 assemble_neg_contexts(struct smb2_negotiate_req *req,
694 struct TCP_Server_Info *server, unsigned int *total_len)
696 unsigned int ctxt_len, neg_context_count;
697 struct TCP_Server_Info *pserver;
701 if (*total_len > 200) {
702 /* In case length corrupted don't want to overrun smb buffer */
703 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
708 * round up total_len of fixed part of SMB3 negotiate request to 8
709 * byte boundary before adding negotiate contexts
711 *total_len = ALIGN(*total_len, 8);
713 pneg_ctxt = (*total_len) + (char *)req;
714 req->NegotiateContextOffset = cpu_to_le32(*total_len);
716 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
717 ctxt_len = ALIGN(sizeof(struct smb2_preauth_neg_context), 8);
718 *total_len += ctxt_len;
719 pneg_ctxt += ctxt_len;
721 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
722 ctxt_len = ALIGN(sizeof(struct smb2_encryption_neg_context), 8);
723 *total_len += ctxt_len;
724 pneg_ctxt += ctxt_len;
727 * secondary channels don't have the hostname field populated
728 * use the hostname field in the primary channel instead
730 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
731 cifs_server_lock(pserver);
732 hostname = pserver->hostname;
733 if (hostname && (hostname[0] != 0)) {
734 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
736 *total_len += ctxt_len;
737 pneg_ctxt += ctxt_len;
738 neg_context_count = 3;
740 neg_context_count = 2;
741 cifs_server_unlock(pserver);
743 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
744 *total_len += sizeof(struct smb2_posix_neg_context);
745 pneg_ctxt += sizeof(struct smb2_posix_neg_context);
748 if (server->compression.requested) {
749 build_compression_ctxt((struct smb2_compression_capabilities_context *)
751 ctxt_len = ALIGN(sizeof(struct smb2_compression_capabilities_context), 8);
752 *total_len += ctxt_len;
753 pneg_ctxt += ctxt_len;
757 if (enable_negotiate_signing) {
758 ctxt_len = build_signing_ctxt((struct smb2_signing_capabilities *)
760 *total_len += ctxt_len;
761 pneg_ctxt += ctxt_len;
765 /* check for and add transport_capabilities and signing capabilities */
766 req->NegotiateContextCount = cpu_to_le16(neg_context_count);
770 /* If invalid preauth context warn but use what we requested, SHA-512 */
771 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
773 unsigned int len = le16_to_cpu(ctxt->DataLength);
776 * Caller checked that DataLength remains within SMB boundary. We still
777 * need to confirm that one HashAlgorithms member is accounted for.
779 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
780 pr_warn_once("server sent bad preauth context\n");
782 } else if (len < MIN_PREAUTH_CTXT_DATA_LEN + le16_to_cpu(ctxt->SaltLength)) {
783 pr_warn_once("server sent invalid SaltLength\n");
786 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
787 pr_warn_once("Invalid SMB3 hash algorithm count\n");
788 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
789 pr_warn_once("unknown SMB3 hash algorithm\n");
792 static void decode_compress_ctx(struct TCP_Server_Info *server,
793 struct smb2_compression_capabilities_context *ctxt)
795 unsigned int len = le16_to_cpu(ctxt->DataLength);
798 server->compression.enabled = false;
801 * Caller checked that DataLength remains within SMB boundary. We still
802 * need to confirm that one CompressionAlgorithms member is accounted
806 pr_warn_once("server sent bad compression cntxt\n");
810 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
811 pr_warn_once("invalid SMB3 compress algorithm count\n");
815 alg = ctxt->CompressionAlgorithms[0];
817 /* 'NONE' (0) compressor type is never negotiated */
818 if (alg == 0 || le16_to_cpu(alg) > 3) {
819 pr_warn_once("invalid compression algorithm '%u'\n", alg);
823 server->compression.alg = alg;
824 server->compression.enabled = true;
827 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
828 struct smb2_encryption_neg_context *ctxt)
830 unsigned int len = le16_to_cpu(ctxt->DataLength);
832 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
834 * Caller checked that DataLength remains within SMB boundary. We still
835 * need to confirm that one Cipher flexible array member is accounted
838 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
839 pr_warn_once("server sent bad crypto ctxt len\n");
843 if (le16_to_cpu(ctxt->CipherCount) != 1) {
844 pr_warn_once("Invalid SMB3.11 cipher count\n");
847 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
848 if (require_gcm_256) {
849 if (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM) {
850 cifs_dbg(VFS, "Server does not support requested encryption type (AES256 GCM)\n");
853 } else if (ctxt->Ciphers[0] == 0) {
855 * e.g. if server only supported AES256_CCM (very unlikely)
856 * or server supported no encryption types or had all disabled.
857 * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case
858 * in which mount requested encryption ("seal") checks later
859 * on during tree connection will return proper rc, but if
860 * seal not requested by client, since server is allowed to
861 * return 0 to indicate no supported cipher, we can't fail here
863 server->cipher_type = 0;
864 server->capabilities &= ~SMB2_GLOBAL_CAP_ENCRYPTION;
865 pr_warn_once("Server does not support requested encryption types\n");
867 } else if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
868 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM) &&
869 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM)) {
870 /* server returned a cipher we didn't ask for */
871 pr_warn_once("Invalid SMB3.11 cipher returned\n");
874 server->cipher_type = ctxt->Ciphers[0];
875 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
879 static void decode_signing_ctx(struct TCP_Server_Info *server,
880 struct smb2_signing_capabilities *pctxt)
882 unsigned int len = le16_to_cpu(pctxt->DataLength);
885 * Caller checked that DataLength remains within SMB boundary. We still
886 * need to confirm that one SigningAlgorithms flexible array member is
889 if ((len < 4) || (len > 16)) {
890 pr_warn_once("server sent bad signing negcontext\n");
893 if (le16_to_cpu(pctxt->SigningAlgorithmCount) != 1) {
894 pr_warn_once("Invalid signing algorithm count\n");
897 if (le16_to_cpu(pctxt->SigningAlgorithms[0]) > 2) {
898 pr_warn_once("unknown signing algorithm\n");
902 server->signing_negotiated = true;
903 server->signing_algorithm = le16_to_cpu(pctxt->SigningAlgorithms[0]);
904 cifs_dbg(FYI, "signing algorithm %d chosen\n",
905 server->signing_algorithm);
909 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
910 struct TCP_Server_Info *server,
911 unsigned int len_of_smb)
913 struct smb2_neg_context *pctx;
914 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
915 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
916 unsigned int len_of_ctxts, i;
919 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
920 if (len_of_smb <= offset) {
921 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
925 len_of_ctxts = len_of_smb - offset;
927 for (i = 0; i < ctxt_cnt; i++) {
929 /* check that offset is not beyond end of SMB */
930 if (len_of_ctxts < sizeof(struct smb2_neg_context))
933 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
934 clen = sizeof(struct smb2_neg_context)
935 + le16_to_cpu(pctx->DataLength);
937 * 2.2.4 SMB2 NEGOTIATE Response
938 * Subsequent negotiate contexts MUST appear at the first 8-byte
939 * aligned offset following the previous negotiate context.
941 if (i + 1 != ctxt_cnt)
942 clen = ALIGN(clen, 8);
943 if (clen > len_of_ctxts)
946 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
947 decode_preauth_context(
948 (struct smb2_preauth_neg_context *)pctx);
949 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
950 rc = decode_encrypt_ctx(server,
951 (struct smb2_encryption_neg_context *)pctx);
952 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
953 decode_compress_ctx(server,
954 (struct smb2_compression_capabilities_context *)pctx);
955 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
956 server->posix_ext_supported = true;
957 else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES)
958 decode_signing_ctx(server,
959 (struct smb2_signing_capabilities *)pctx);
961 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
962 le16_to_cpu(pctx->ContextType));
967 len_of_ctxts -= clen;
972 static struct create_posix *
973 create_posix_buf(umode_t mode)
975 struct create_posix *buf;
977 buf = kzalloc(sizeof(struct create_posix),
982 buf->ccontext.DataOffset =
983 cpu_to_le16(offsetof(struct create_posix, Mode));
984 buf->ccontext.DataLength = cpu_to_le32(4);
985 buf->ccontext.NameOffset =
986 cpu_to_le16(offsetof(struct create_posix, Name));
987 buf->ccontext.NameLength = cpu_to_le16(16);
989 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
1000 buf->Name[10] = 0x83;
1001 buf->Name[11] = 0xDE;
1002 buf->Name[12] = 0x96;
1003 buf->Name[13] = 0x8B;
1004 buf->Name[14] = 0xCD;
1005 buf->Name[15] = 0x7C;
1006 buf->Mode = cpu_to_le32(mode);
1007 cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
1012 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
1014 unsigned int num = *num_iovec;
1016 iov[num].iov_base = create_posix_buf(mode);
1017 if (mode == ACL_NO_MODE)
1018 cifs_dbg(FYI, "%s: no mode\n", __func__);
1019 if (iov[num].iov_base == NULL)
1021 iov[num].iov_len = sizeof(struct create_posix);
1022 *num_iovec = num + 1;
1029 * SMB2 Worker functions follow:
1031 * The general structure of the worker functions is:
1032 * 1) Call smb2_init (assembles SMB2 header)
1033 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
1034 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
1035 * 4) Decode SMB2 command specific fields in the fixed length area
1036 * 5) Decode variable length data area (if any for this SMB2 command type)
1037 * 6) Call free smb buffer
1043 SMB2_negotiate(const unsigned int xid,
1044 struct cifs_ses *ses,
1045 struct TCP_Server_Info *server)
1047 struct smb_rqst rqst;
1048 struct smb2_negotiate_req *req;
1049 struct smb2_negotiate_rsp *rsp;
1051 struct kvec rsp_iov;
1054 int blob_offset, blob_length;
1055 char *security_blob;
1056 int flags = CIFS_NEG_OP;
1057 unsigned int total_len;
1059 cifs_dbg(FYI, "Negotiate protocol\n");
1062 WARN(1, "%s: server is NULL!\n", __func__);
1066 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
1067 (void **) &req, &total_len);
1071 req->hdr.SessionId = 0;
1073 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
1074 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
1076 if (strcmp(server->vals->version_string,
1077 SMB3ANY_VERSION_STRING) == 0) {
1078 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1079 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1080 req->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1081 req->DialectCount = cpu_to_le16(3);
1083 } else if (strcmp(server->vals->version_string,
1084 SMBDEFAULT_VERSION_STRING) == 0) {
1085 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1086 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1087 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1088 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1089 req->DialectCount = cpu_to_le16(4);
1092 /* otherwise send specific dialect */
1093 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
1094 req->DialectCount = cpu_to_le16(1);
1098 /* only one of SMB2 signing flags may be set in SMB2 request */
1100 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1101 else if (global_secflags & CIFSSEC_MAY_SIGN)
1102 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1104 req->SecurityMode = 0;
1106 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
1107 if (ses->chan_max > 1)
1108 req->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
1110 /* ClientGUID must be zero for SMB2.02 dialect */
1111 if (server->vals->protocol_id == SMB20_PROT_ID)
1112 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
1114 memcpy(req->ClientGUID, server->client_guid,
1115 SMB2_CLIENT_GUID_SIZE);
1116 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
1117 (strcmp(server->vals->version_string,
1118 SMB3ANY_VERSION_STRING) == 0) ||
1119 (strcmp(server->vals->version_string,
1120 SMBDEFAULT_VERSION_STRING) == 0))
1121 assemble_neg_contexts(req, server, &total_len);
1123 iov[0].iov_base = (char *)req;
1124 iov[0].iov_len = total_len;
1126 memset(&rqst, 0, sizeof(struct smb_rqst));
1130 rc = cifs_send_recv(xid, ses, server,
1131 &rqst, &resp_buftype, flags, &rsp_iov);
1132 cifs_small_buf_release(req);
1133 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
1135 * No tcon so can't do
1136 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1138 if (rc == -EOPNOTSUPP) {
1139 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");
1145 if (strcmp(server->vals->version_string,
1146 SMB3ANY_VERSION_STRING) == 0) {
1147 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
1148 cifs_server_dbg(VFS,
1149 "SMB2 dialect returned but not requested\n");
1151 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
1152 cifs_server_dbg(VFS,
1153 "SMB2.1 dialect returned but not requested\n");
1155 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1156 /* ops set to 3.0 by default for default so update */
1157 server->ops = &smb311_operations;
1158 server->vals = &smb311_values;
1160 } else if (strcmp(server->vals->version_string,
1161 SMBDEFAULT_VERSION_STRING) == 0) {
1162 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
1163 cifs_server_dbg(VFS,
1164 "SMB2 dialect returned but not requested\n");
1166 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
1167 /* ops set to 3.0 by default for default so update */
1168 server->ops = &smb21_operations;
1169 server->vals = &smb21_values;
1170 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1171 server->ops = &smb311_operations;
1172 server->vals = &smb311_values;
1174 } else if (le16_to_cpu(rsp->DialectRevision) !=
1175 server->vals->protocol_id) {
1176 /* if requested single dialect ensure returned dialect matched */
1177 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
1178 le16_to_cpu(rsp->DialectRevision));
1182 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
1184 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
1185 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
1186 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
1187 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
1188 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
1189 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
1190 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
1191 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
1192 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
1193 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
1195 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
1196 le16_to_cpu(rsp->DialectRevision));
1201 server->dialect = le16_to_cpu(rsp->DialectRevision);
1204 * Keep a copy of the hash after negprot. This hash will be
1205 * the starting hash value for all sessions made from this
1208 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
1209 SMB2_PREAUTH_HASH_SIZE);
1211 /* SMB2 only has an extended negflavor */
1212 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
1213 /* set it to the maximum buffer size value we can send with 1 credit */
1214 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
1215 SMB2_MAX_BUFFER_SIZE);
1216 server->max_read = le32_to_cpu(rsp->MaxReadSize);
1217 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
1218 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
1219 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
1220 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
1222 server->capabilities = le32_to_cpu(rsp->Capabilities);
1223 /* Internal types */
1224 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
1227 * SMB3.0 supports only 1 cipher and doesn't have a encryption neg context
1228 * Set the cipher type manually.
1230 if (server->dialect == SMB30_PROT_ID && (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 free_rsp_buf(resp_buftype, rsp);
1270 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1273 struct validate_negotiate_info_req *pneg_inbuf;
1274 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
1276 u32 inbuflen; /* max of 4 dialects */
1277 struct TCP_Server_Info *server = tcon->ses->server;
1279 cifs_dbg(FYI, "validate negotiate\n");
1281 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1282 if (server->dialect == SMB311_PROT_ID)
1286 * validation ioctl must be signed, so no point sending this if we
1287 * can not sign it (ie are not known user). Even if signing is not
1288 * required (enabled but not negotiated), in those cases we selectively
1289 * sign just this, the first and only signed request on a connection.
1290 * Having validation of negotiate info helps reduce attack vectors.
1292 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1293 return 0; /* validation requires signing */
1295 if (tcon->ses->user_name == NULL) {
1296 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1297 return 0; /* validation requires signing */
1300 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1301 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1303 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1307 pneg_inbuf->Capabilities =
1308 cpu_to_le32(server->vals->req_capabilities);
1309 if (tcon->ses->chan_max > 1)
1310 pneg_inbuf->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
1312 memcpy(pneg_inbuf->Guid, server->client_guid,
1313 SMB2_CLIENT_GUID_SIZE);
1315 if (tcon->ses->sign)
1316 pneg_inbuf->SecurityMode =
1317 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1318 else if (global_secflags & CIFSSEC_MAY_SIGN)
1319 pneg_inbuf->SecurityMode =
1320 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1322 pneg_inbuf->SecurityMode = 0;
1325 if (strcmp(server->vals->version_string,
1326 SMB3ANY_VERSION_STRING) == 0) {
1327 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1328 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1329 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1330 pneg_inbuf->DialectCount = cpu_to_le16(3);
1331 /* SMB 2.1 not included so subtract one dialect from len */
1332 inbuflen = sizeof(*pneg_inbuf) -
1333 (sizeof(pneg_inbuf->Dialects[0]));
1334 } else if (strcmp(server->vals->version_string,
1335 SMBDEFAULT_VERSION_STRING) == 0) {
1336 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1337 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1338 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1339 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1340 pneg_inbuf->DialectCount = cpu_to_le16(4);
1341 /* structure is big enough for 4 dialects */
1342 inbuflen = sizeof(*pneg_inbuf);
1344 /* otherwise specific dialect was requested */
1345 pneg_inbuf->Dialects[0] =
1346 cpu_to_le16(server->vals->protocol_id);
1347 pneg_inbuf->DialectCount = cpu_to_le16(1);
1348 /* structure is big enough for 4 dialects, sending only 1 */
1349 inbuflen = sizeof(*pneg_inbuf) -
1350 sizeof(pneg_inbuf->Dialects[0]) * 3;
1353 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1354 FSCTL_VALIDATE_NEGOTIATE_INFO,
1355 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1356 (char **)&pneg_rsp, &rsplen);
1357 if (rc == -EOPNOTSUPP) {
1359 * Old Windows versions or Netapp SMB server can return
1360 * not supported error. Client should accept it.
1362 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1364 goto out_free_inbuf;
1365 } else if (rc != 0) {
1366 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1369 goto out_free_inbuf;
1373 if (rsplen != sizeof(*pneg_rsp)) {
1374 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1377 /* relax check since Mac returns max bufsize allowed on ioctl */
1378 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1382 /* check validate negotiate info response matches what we got earlier */
1383 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1386 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1389 /* do not validate server guid because not saved at negprot time yet */
1391 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1392 SMB2_LARGE_FILES) != server->capabilities)
1395 /* validate negotiate successful */
1397 cifs_dbg(FYI, "validate negotiate info successful\n");
1401 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1410 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1412 switch (requested) {
1419 if (server->sec_ntlmssp &&
1420 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1422 if ((server->sec_kerberos || server->sec_mskerberos) &&
1423 (global_secflags & CIFSSEC_MAY_KRB5))
1431 struct SMB2_sess_data {
1433 struct cifs_ses *ses;
1434 struct TCP_Server_Info *server;
1435 struct nls_table *nls_cp;
1436 void (*func)(struct SMB2_sess_data *);
1438 u64 previous_session;
1440 /* we will send the SMB in three pieces:
1441 * a fixed length beginning part, an optional
1442 * SPNEGO blob (which can be zero length), and a
1443 * last part which will include the strings
1444 * and rest of bcc area. This allows us to avoid
1445 * a large buffer 17K allocation
1452 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1455 struct cifs_ses *ses = sess_data->ses;
1456 struct TCP_Server_Info *server = sess_data->server;
1457 struct smb2_sess_setup_req *req;
1458 unsigned int total_len;
1459 bool is_binding = false;
1461 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
1467 spin_lock(&ses->ses_lock);
1468 is_binding = (ses->ses_status == SES_GOOD);
1469 spin_unlock(&ses->ses_lock);
1472 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1473 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1474 req->PreviousSessionId = 0;
1475 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1476 cifs_dbg(FYI, "Binding to sess id: %llx\n", ses->Suid);
1478 /* First session, not a reauthenticate */
1479 req->hdr.SessionId = 0;
1481 * if reconnect, we need to send previous sess id
1484 req->PreviousSessionId = cpu_to_le64(sess_data->previous_session);
1485 req->Flags = 0; /* MBZ */
1486 cifs_dbg(FYI, "Fresh session. Previous: %llx\n",
1487 sess_data->previous_session);
1490 /* enough to enable echos and oplocks and one max size write */
1491 if (server->credits >= server->max_credits)
1492 req->hdr.CreditRequest = cpu_to_le16(0);
1494 req->hdr.CreditRequest = cpu_to_le16(
1495 min_t(int, server->max_credits -
1496 server->credits, 130));
1498 /* only one of SMB2 signing flags may be set in SMB2 request */
1500 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1501 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1502 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1504 req->SecurityMode = 0;
1506 #ifdef CONFIG_CIFS_DFS_UPCALL
1507 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1509 req->Capabilities = 0;
1510 #endif /* DFS_UPCALL */
1512 req->Channel = 0; /* MBZ */
1514 sess_data->iov[0].iov_base = (char *)req;
1516 sess_data->iov[0].iov_len = total_len - 1;
1518 * This variable will be used to clear the buffer
1519 * allocated above in case of any error in the calling function.
1521 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1527 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1529 struct kvec *iov = sess_data->iov;
1531 /* iov[1] is already freed by caller */
1532 if (sess_data->buf0_type != CIFS_NO_BUFFER && iov[0].iov_base)
1533 memzero_explicit(iov[0].iov_base, iov[0].iov_len);
1535 free_rsp_buf(sess_data->buf0_type, iov[0].iov_base);
1536 sess_data->buf0_type = CIFS_NO_BUFFER;
1540 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1543 struct smb_rqst rqst;
1544 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1545 struct kvec rsp_iov = { NULL, 0 };
1547 /* Testing shows that buffer offset must be at location of Buffer[0] */
1548 req->SecurityBufferOffset =
1549 cpu_to_le16(sizeof(struct smb2_sess_setup_req));
1550 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1552 memset(&rqst, 0, sizeof(struct smb_rqst));
1553 rqst.rq_iov = sess_data->iov;
1556 /* BB add code to build os and lm fields */
1557 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1560 &sess_data->buf0_type,
1561 CIFS_LOG_ERROR | CIFS_SESS_OP, &rsp_iov);
1562 cifs_small_buf_release(sess_data->iov[0].iov_base);
1564 sess_data->ses->expired_pwd = false;
1565 else if ((rc == -EACCES) || (rc == -EKEYEXPIRED) || (rc == -EKEYREVOKED))
1566 sess_data->ses->expired_pwd = true;
1568 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1574 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1577 struct cifs_ses *ses = sess_data->ses;
1578 struct TCP_Server_Info *server = sess_data->server;
1580 cifs_server_lock(server);
1581 if (server->ops->generate_signingkey) {
1582 rc = server->ops->generate_signingkey(ses, server);
1585 "SMB3 session key generation failed\n");
1586 cifs_server_unlock(server);
1590 if (!server->session_estab) {
1591 server->sequence_number = 0x2;
1592 server->session_estab = true;
1594 cifs_server_unlock(server);
1596 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1600 #ifdef CONFIG_CIFS_UPCALL
1602 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1605 struct cifs_ses *ses = sess_data->ses;
1606 struct TCP_Server_Info *server = sess_data->server;
1607 struct cifs_spnego_msg *msg;
1608 struct key *spnego_key = NULL;
1609 struct smb2_sess_setup_rsp *rsp = NULL;
1610 bool is_binding = false;
1612 rc = SMB2_sess_alloc_buffer(sess_data);
1616 spnego_key = cifs_get_spnego_key(ses, server);
1617 if (IS_ERR(spnego_key)) {
1618 rc = PTR_ERR(spnego_key);
1620 cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
1625 msg = spnego_key->payload.data[0];
1627 * check version field to make sure that cifs.upcall is
1628 * sending us a response in an expected form
1630 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1631 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1632 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1634 goto out_put_spnego_key;
1637 spin_lock(&ses->ses_lock);
1638 is_binding = (ses->ses_status == SES_GOOD);
1639 spin_unlock(&ses->ses_lock);
1641 /* keep session key if binding */
1643 kfree_sensitive(ses->auth_key.response);
1644 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1646 if (!ses->auth_key.response) {
1647 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1650 goto out_put_spnego_key;
1652 ses->auth_key.len = msg->sesskey_len;
1655 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1656 sess_data->iov[1].iov_len = msg->secblob_len;
1658 rc = SMB2_sess_sendreceive(sess_data);
1660 goto out_put_spnego_key;
1662 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1663 /* keep session id and flags if binding */
1665 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1666 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1669 rc = SMB2_sess_establish_session(sess_data);
1671 key_invalidate(spnego_key);
1672 key_put(spnego_key);
1674 kfree_sensitive(ses->auth_key.response);
1675 ses->auth_key.response = NULL;
1676 ses->auth_key.len = 0;
1679 sess_data->result = rc;
1680 sess_data->func = NULL;
1681 SMB2_sess_free_buffer(sess_data);
1685 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1687 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1688 sess_data->result = -EOPNOTSUPP;
1689 sess_data->func = NULL;
1694 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1697 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1700 struct cifs_ses *ses = sess_data->ses;
1701 struct TCP_Server_Info *server = sess_data->server;
1702 struct smb2_sess_setup_rsp *rsp = NULL;
1703 unsigned char *ntlmssp_blob = NULL;
1704 bool use_spnego = false; /* else use raw ntlmssp */
1705 u16 blob_length = 0;
1706 bool is_binding = false;
1709 * If memory allocation is successful, caller of this function
1712 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1713 if (!ses->ntlmssp) {
1717 ses->ntlmssp->sesskey_per_smbsess = true;
1719 rc = SMB2_sess_alloc_buffer(sess_data);
1723 rc = build_ntlmssp_smb3_negotiate_blob(&ntlmssp_blob,
1724 &blob_length, ses, server,
1730 /* BB eventually need to add this */
1731 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1735 sess_data->iov[1].iov_base = ntlmssp_blob;
1736 sess_data->iov[1].iov_len = blob_length;
1738 rc = SMB2_sess_sendreceive(sess_data);
1739 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1741 /* If true, rc here is expected and not an error */
1742 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1743 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1749 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1750 le16_to_cpu(rsp->SecurityBufferOffset)) {
1751 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1752 le16_to_cpu(rsp->SecurityBufferOffset));
1756 rc = decode_ntlmssp_challenge(rsp->Buffer,
1757 le16_to_cpu(rsp->SecurityBufferLength), ses);
1761 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1763 spin_lock(&ses->ses_lock);
1764 is_binding = (ses->ses_status == SES_GOOD);
1765 spin_unlock(&ses->ses_lock);
1767 /* keep existing ses id and flags if binding */
1769 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1770 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1774 kfree_sensitive(ntlmssp_blob);
1775 SMB2_sess_free_buffer(sess_data);
1777 sess_data->result = 0;
1778 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1782 kfree_sensitive(ses->ntlmssp);
1783 ses->ntlmssp = NULL;
1784 sess_data->result = rc;
1785 sess_data->func = NULL;
1789 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1792 struct cifs_ses *ses = sess_data->ses;
1793 struct TCP_Server_Info *server = sess_data->server;
1794 struct smb2_sess_setup_req *req;
1795 struct smb2_sess_setup_rsp *rsp = NULL;
1796 unsigned char *ntlmssp_blob = NULL;
1797 bool use_spnego = false; /* else use raw ntlmssp */
1798 u16 blob_length = 0;
1799 bool is_binding = false;
1801 rc = SMB2_sess_alloc_buffer(sess_data);
1805 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1806 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1808 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length,
1812 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1817 /* BB eventually need to add this */
1818 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1822 sess_data->iov[1].iov_base = ntlmssp_blob;
1823 sess_data->iov[1].iov_len = blob_length;
1825 rc = SMB2_sess_sendreceive(sess_data);
1829 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1831 spin_lock(&ses->ses_lock);
1832 is_binding = (ses->ses_status == SES_GOOD);
1833 spin_unlock(&ses->ses_lock);
1835 /* keep existing ses id and flags if binding */
1837 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1838 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1841 rc = SMB2_sess_establish_session(sess_data);
1842 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1843 if (ses->server->dialect < SMB30_PROT_ID) {
1844 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1846 * The session id is opaque in terms of endianness, so we can't
1847 * print it as a long long. we dump it as we got it on the wire
1849 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1851 cifs_dbg(VFS, "Session Key %*ph\n",
1852 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1853 cifs_dbg(VFS, "Signing Key %*ph\n",
1854 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1858 kfree_sensitive(ntlmssp_blob);
1859 SMB2_sess_free_buffer(sess_data);
1860 kfree_sensitive(ses->ntlmssp);
1861 ses->ntlmssp = NULL;
1862 sess_data->result = rc;
1863 sess_data->func = NULL;
1867 SMB2_select_sec(struct SMB2_sess_data *sess_data)
1870 struct cifs_ses *ses = sess_data->ses;
1871 struct TCP_Server_Info *server = sess_data->server;
1873 type = smb2_select_sectype(server, ses->sectype);
1874 cifs_dbg(FYI, "sess setup type %d\n", type);
1875 if (type == Unspecified) {
1876 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1882 sess_data->func = SMB2_auth_kerberos;
1885 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1888 cifs_dbg(VFS, "secType %d not supported!\n", type);
1896 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1897 struct TCP_Server_Info *server,
1898 const struct nls_table *nls_cp)
1901 struct SMB2_sess_data *sess_data;
1903 cifs_dbg(FYI, "Session Setup\n");
1906 WARN(1, "%s: server is NULL!\n", __func__);
1910 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1914 sess_data->xid = xid;
1915 sess_data->ses = ses;
1916 sess_data->server = server;
1917 sess_data->buf0_type = CIFS_NO_BUFFER;
1918 sess_data->nls_cp = (struct nls_table *) nls_cp;
1919 sess_data->previous_session = ses->Suid;
1921 rc = SMB2_select_sec(sess_data);
1926 * Initialize the session hash with the server one.
1928 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
1929 SMB2_PREAUTH_HASH_SIZE);
1931 while (sess_data->func)
1932 sess_data->func(sess_data);
1934 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1935 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1936 rc = sess_data->result;
1938 kfree_sensitive(sess_data);
1943 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1945 struct smb_rqst rqst;
1946 struct smb2_logoff_req *req; /* response is also trivial struct */
1948 struct TCP_Server_Info *server;
1950 unsigned int total_len;
1952 struct kvec rsp_iov;
1955 cifs_dbg(FYI, "disconnect session %p\n", ses);
1957 if (ses && (ses->server))
1958 server = ses->server;
1962 /* no need to send SMB logoff if uid already closed due to reconnect */
1963 spin_lock(&ses->chan_lock);
1964 if (CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
1965 spin_unlock(&ses->chan_lock);
1966 goto smb2_session_already_dead;
1968 spin_unlock(&ses->chan_lock);
1970 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
1971 (void **) &req, &total_len);
1975 /* since no tcon, smb2_init can not do this, so do here */
1976 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1978 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1979 flags |= CIFS_TRANSFORM_REQ;
1980 else if (server->sign)
1981 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1983 flags |= CIFS_NO_RSP_BUF;
1985 iov[0].iov_base = (char *)req;
1986 iov[0].iov_len = total_len;
1988 memset(&rqst, 0, sizeof(struct smb_rqst));
1992 rc = cifs_send_recv(xid, ses, ses->server,
1993 &rqst, &resp_buf_type, flags, &rsp_iov);
1994 cifs_small_buf_release(req);
1996 * No tcon so can't do
1997 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
2000 smb2_session_already_dead:
2004 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
2006 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
2009 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
2011 /* These are similar values to what Windows uses */
2012 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
2014 tcon->max_chunks = 256;
2015 tcon->max_bytes_chunk = 1048576;
2016 tcon->max_bytes_copy = 16777216;
2020 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
2021 struct cifs_tcon *tcon, const struct nls_table *cp)
2023 struct smb_rqst rqst;
2024 struct smb2_tree_connect_req *req;
2025 struct smb2_tree_connect_rsp *rsp = NULL;
2027 struct kvec rsp_iov = { NULL, 0 };
2031 __le16 *unc_path = NULL;
2033 unsigned int total_len;
2034 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2036 cifs_dbg(FYI, "TCON\n");
2038 if (!server || !tree)
2041 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
2042 if (unc_path == NULL)
2045 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp);
2046 if (unc_path_len <= 0) {
2052 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
2054 atomic_set(&tcon->num_remote_opens, 0);
2055 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
2056 (void **) &req, &total_len);
2062 if (smb3_encryption_required(tcon))
2063 flags |= CIFS_TRANSFORM_REQ;
2065 iov[0].iov_base = (char *)req;
2067 iov[0].iov_len = total_len - 1;
2069 /* Testing shows that buffer offset must be at location of Buffer[0] */
2070 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req));
2071 req->PathLength = cpu_to_le16(unc_path_len);
2072 iov[1].iov_base = unc_path;
2073 iov[1].iov_len = unc_path_len;
2076 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
2077 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
2078 * (Samba servers don't always set the flag so also check if null user)
2080 if ((server->dialect == SMB311_PROT_ID) &&
2081 !smb3_encryption_required(tcon) &&
2082 !(ses->session_flags &
2083 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
2084 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
2085 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
2087 memset(&rqst, 0, sizeof(struct smb_rqst));
2091 /* Need 64 for max size write so ask for more in case not there yet */
2092 if (server->credits >= server->max_credits)
2093 req->hdr.CreditRequest = cpu_to_le16(0);
2095 req->hdr.CreditRequest = cpu_to_le16(
2096 min_t(int, server->max_credits -
2097 server->credits, 64));
2099 rc = cifs_send_recv(xid, ses, server,
2100 &rqst, &resp_buftype, flags, &rsp_iov);
2101 cifs_small_buf_release(req);
2102 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
2103 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
2104 if ((rc != 0) || (rsp == NULL)) {
2105 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
2106 tcon->need_reconnect = true;
2107 goto tcon_error_exit;
2110 switch (rsp->ShareType) {
2111 case SMB2_SHARE_TYPE_DISK:
2112 cifs_dbg(FYI, "connection to disk share\n");
2114 case SMB2_SHARE_TYPE_PIPE:
2116 cifs_dbg(FYI, "connection to pipe share\n");
2118 case SMB2_SHARE_TYPE_PRINT:
2120 cifs_dbg(FYI, "connection to printer\n");
2123 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
2125 goto tcon_error_exit;
2128 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
2129 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
2130 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
2131 tcon->tid = le32_to_cpu(rsp->hdr.Id.SyncId.TreeId);
2132 strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name));
2134 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
2135 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
2136 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
2139 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
2140 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
2142 init_copy_chunk_defaults(tcon);
2143 if (server->ops->validate_negotiate)
2144 rc = server->ops->validate_negotiate(xid, tcon);
2145 if (rc == 0) /* See MS-SMB2 2.2.10 and 3.2.5.5 */
2146 if (tcon->share_flags & SMB2_SHAREFLAG_ISOLATED_TRANSPORT)
2147 server->nosharesock = true;
2150 free_rsp_buf(resp_buftype, rsp);
2155 if (rsp && rsp->hdr.Status == STATUS_BAD_NETWORK_NAME)
2156 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
2161 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
2163 struct smb_rqst rqst;
2164 struct smb2_tree_disconnect_req *req; /* response is trivial */
2166 struct cifs_ses *ses = tcon->ses;
2167 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2169 unsigned int total_len;
2171 struct kvec rsp_iov;
2174 cifs_dbg(FYI, "Tree Disconnect\n");
2176 if (!ses || !(ses->server))
2179 trace_smb3_tdis_enter(xid, tcon->tid, ses->Suid, tcon->tree_name);
2180 spin_lock(&ses->chan_lock);
2181 if ((tcon->need_reconnect) ||
2182 (CIFS_ALL_CHANS_NEED_RECONNECT(tcon->ses))) {
2183 spin_unlock(&ses->chan_lock);
2186 spin_unlock(&ses->chan_lock);
2188 invalidate_all_cached_dirs(tcon);
2190 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, server,
2196 if (smb3_encryption_required(tcon))
2197 flags |= CIFS_TRANSFORM_REQ;
2199 flags |= CIFS_NO_RSP_BUF;
2201 iov[0].iov_base = (char *)req;
2202 iov[0].iov_len = total_len;
2204 memset(&rqst, 0, sizeof(struct smb_rqst));
2208 rc = cifs_send_recv(xid, ses, server,
2209 &rqst, &resp_buf_type, flags, &rsp_iov);
2210 cifs_small_buf_release(req);
2212 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
2213 trace_smb3_tdis_err(xid, tcon->tid, ses->Suid, rc);
2215 trace_smb3_tdis_done(xid, tcon->tid, ses->Suid);
2221 static struct create_durable *
2222 create_durable_buf(void)
2224 struct create_durable *buf;
2226 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2230 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2231 (struct create_durable, Data));
2232 buf->ccontext.DataLength = cpu_to_le32(16);
2233 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2234 (struct create_durable, Name));
2235 buf->ccontext.NameLength = cpu_to_le16(4);
2236 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
2244 static struct create_durable *
2245 create_reconnect_durable_buf(struct cifs_fid *fid)
2247 struct create_durable *buf;
2249 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2253 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2254 (struct create_durable, Data));
2255 buf->ccontext.DataLength = cpu_to_le32(16);
2256 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2257 (struct create_durable, Name));
2258 buf->ccontext.NameLength = cpu_to_le16(4);
2259 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
2260 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
2261 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
2270 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
2272 struct create_disk_id_rsp *pdisk_id = (struct create_disk_id_rsp *)cc;
2274 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
2275 pdisk_id->DiskFileId, pdisk_id->VolumeId);
2276 buf->IndexNumber = pdisk_id->DiskFileId;
2280 parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
2281 struct create_posix_rsp *posix)
2284 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
2285 u8 *end = beg + le32_to_cpu(cc->DataLength);
2288 memset(posix, 0, sizeof(*posix));
2290 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
2291 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
2292 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
2295 sid_len = posix_info_sid_size(sid, end);
2297 cifs_dbg(VFS, "bad owner sid in posix create response\n");
2300 memcpy(&posix->owner, sid, sid_len);
2302 sid = sid + sid_len;
2303 sid_len = posix_info_sid_size(sid, end);
2305 cifs_dbg(VFS, "bad group sid in posix create response\n");
2308 memcpy(&posix->group, sid, sid_len);
2310 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
2311 posix->nlink, posix->mode, posix->reparse_tag);
2314 int smb2_parse_contexts(struct TCP_Server_Info *server,
2315 struct kvec *rsp_iov,
2316 unsigned int *epoch,
2317 char *lease_key, __u8 *oplock,
2318 struct smb2_file_all_info *buf,
2319 struct create_posix_rsp *posix)
2321 struct smb2_create_rsp *rsp = rsp_iov->iov_base;
2322 struct create_context *cc;
2323 size_t rem, off, len;
2327 static const char smb3_create_tag_posix[] = {
2328 0x93, 0xAD, 0x25, 0x50, 0x9C,
2329 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
2330 0xDE, 0x96, 0x8B, 0xCD, 0x7C
2335 off = le32_to_cpu(rsp->CreateContextsOffset);
2336 rem = le32_to_cpu(rsp->CreateContextsLength);
2337 if (check_add_overflow(off, rem, &len) || len > rsp_iov->iov_len)
2339 cc = (struct create_context *)((u8 *)rsp + off);
2341 /* Initialize inode number to 0 in case no valid data in qfid context */
2343 buf->IndexNumber = 0;
2345 while (rem >= sizeof(*cc)) {
2346 doff = le16_to_cpu(cc->DataOffset);
2347 dlen = le32_to_cpu(cc->DataLength);
2348 if (check_add_overflow(doff, dlen, &len) || len > rem)
2351 noff = le16_to_cpu(cc->NameOffset);
2352 nlen = le16_to_cpu(cc->NameLength);
2353 if (noff + nlen > doff)
2356 name = (char *)cc + noff;
2359 if (!strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4)) {
2360 *oplock = server->ops->parse_lease_buf(cc, epoch,
2363 !strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4)) {
2364 parse_query_id_ctxt(cc, buf);
2368 if (posix && !memcmp(name, smb3_create_tag_posix, 16))
2369 parse_posix_ctxt(cc, buf, posix);
2372 cifs_dbg(FYI, "%s: unhandled context (nlen=%zu dlen=%zu)\n",
2373 __func__, nlen, dlen);
2374 if (IS_ENABLED(CONFIG_CIFS_DEBUG2))
2375 cifs_dump_mem("context data: ", cc, dlen);
2379 off = le32_to_cpu(cc->Next);
2382 if (check_sub_overflow(rem, off, &rem))
2384 cc = (struct create_context *)((u8 *)cc + off);
2387 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2388 *oplock = rsp->OplockLevel;
2394 add_lease_context(struct TCP_Server_Info *server,
2395 struct smb2_create_req *req,
2397 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
2399 unsigned int num = *num_iovec;
2401 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
2402 if (iov[num].iov_base == NULL)
2404 iov[num].iov_len = server->vals->create_lease_size;
2405 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2406 *num_iovec = num + 1;
2410 static struct create_durable_v2 *
2411 create_durable_v2_buf(struct cifs_open_parms *oparms)
2413 struct cifs_fid *pfid = oparms->fid;
2414 struct create_durable_v2 *buf;
2416 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2420 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2421 (struct create_durable_v2, dcontext));
2422 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2423 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2424 (struct create_durable_v2, Name));
2425 buf->ccontext.NameLength = cpu_to_le16(4);
2428 * NB: Handle timeout defaults to 0, which allows server to choose
2429 * (most servers default to 120 seconds) and most clients default to 0.
2430 * This can be overridden at mount ("handletimeout=") if the user wants
2431 * a different persistent (or resilient) handle timeout for all opens
2432 * on a particular SMB3 mount.
2434 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2435 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2437 /* for replay, we should not overwrite the existing create guid */
2438 if (!oparms->replay) {
2439 generate_random_uuid(buf->dcontext.CreateGuid);
2440 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2442 memcpy(buf->dcontext.CreateGuid, pfid->create_guid, 16);
2444 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2452 static struct create_durable_handle_reconnect_v2 *
2453 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2455 struct create_durable_handle_reconnect_v2 *buf;
2457 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2462 buf->ccontext.DataOffset =
2463 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2465 buf->ccontext.DataLength =
2466 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2467 buf->ccontext.NameOffset =
2468 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2470 buf->ccontext.NameLength = cpu_to_le16(4);
2472 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2473 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2474 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2475 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2477 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2486 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2487 struct cifs_open_parms *oparms)
2489 unsigned int num = *num_iovec;
2491 iov[num].iov_base = create_durable_v2_buf(oparms);
2492 if (iov[num].iov_base == NULL)
2494 iov[num].iov_len = sizeof(struct create_durable_v2);
2495 *num_iovec = num + 1;
2500 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2501 struct cifs_open_parms *oparms)
2503 unsigned int num = *num_iovec;
2505 /* indicate that we don't need to relock the file */
2506 oparms->reconnect = false;
2508 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2509 if (iov[num].iov_base == NULL)
2511 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2512 *num_iovec = num + 1;
2517 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2518 struct cifs_open_parms *oparms, bool use_persistent)
2520 unsigned int num = *num_iovec;
2522 if (use_persistent) {
2523 if (oparms->reconnect)
2524 return add_durable_reconnect_v2_context(iov, num_iovec,
2527 return add_durable_v2_context(iov, num_iovec, oparms);
2530 if (oparms->reconnect) {
2531 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2532 /* indicate that we don't need to relock the file */
2533 oparms->reconnect = false;
2535 iov[num].iov_base = create_durable_buf();
2536 if (iov[num].iov_base == NULL)
2538 iov[num].iov_len = sizeof(struct create_durable);
2539 *num_iovec = num + 1;
2543 /* See MS-SMB2 2.2.13.2.7 */
2544 static struct crt_twarp_ctxt *
2545 create_twarp_buf(__u64 timewarp)
2547 struct crt_twarp_ctxt *buf;
2549 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2553 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2554 (struct crt_twarp_ctxt, Timestamp));
2555 buf->ccontext.DataLength = cpu_to_le32(8);
2556 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2557 (struct crt_twarp_ctxt, Name));
2558 buf->ccontext.NameLength = cpu_to_le16(4);
2559 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2564 buf->Timestamp = cpu_to_le64(timewarp);
2568 /* See MS-SMB2 2.2.13.2.7 */
2570 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2572 unsigned int num = *num_iovec;
2574 iov[num].iov_base = create_twarp_buf(timewarp);
2575 if (iov[num].iov_base == NULL)
2577 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2578 *num_iovec = num + 1;
2582 /* See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
2583 static void setup_owner_group_sids(char *buf)
2585 struct owner_group_sids *sids = (struct owner_group_sids *)buf;
2587 /* Populate the user ownership fields S-1-5-88-1 */
2588 sids->owner.Revision = 1;
2589 sids->owner.NumAuth = 3;
2590 sids->owner.Authority[5] = 5;
2591 sids->owner.SubAuthorities[0] = cpu_to_le32(88);
2592 sids->owner.SubAuthorities[1] = cpu_to_le32(1);
2593 sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val);
2595 /* Populate the group ownership fields S-1-5-88-2 */
2596 sids->group.Revision = 1;
2597 sids->group.NumAuth = 3;
2598 sids->group.Authority[5] = 5;
2599 sids->group.SubAuthorities[0] = cpu_to_le32(88);
2600 sids->group.SubAuthorities[1] = cpu_to_le32(2);
2601 sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val);
2603 cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val);
2606 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2607 static struct crt_sd_ctxt *
2608 create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
2610 struct crt_sd_ctxt *buf;
2612 unsigned int acelen, acl_size, ace_count;
2613 unsigned int owner_offset = 0;
2614 unsigned int group_offset = 0;
2615 struct smb3_acl acl = {};
2617 *len = round_up(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 4), 8);
2620 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
2621 *len += sizeof(struct owner_group_sids);
2624 buf = kzalloc(*len, GFP_KERNEL);
2628 ptr = (__u8 *)&buf[1];
2630 /* offset fields are from beginning of security descriptor not of create context */
2631 owner_offset = ptr - (__u8 *)&buf->sd;
2632 buf->sd.OffsetOwner = cpu_to_le32(owner_offset);
2633 group_offset = owner_offset + offsetof(struct owner_group_sids, group);
2634 buf->sd.OffsetGroup = cpu_to_le32(group_offset);
2636 setup_owner_group_sids(ptr);
2637 ptr += sizeof(struct owner_group_sids);
2639 buf->sd.OffsetOwner = 0;
2640 buf->sd.OffsetGroup = 0;
2643 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, sd));
2644 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name));
2645 buf->ccontext.NameLength = cpu_to_le16(4);
2646 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2651 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2654 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2655 * and "DP" ie the DACL is present
2657 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2659 /* offset owner, group and Sbz1 and SACL are all zero */
2660 buf->sd.OffsetDacl = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2661 /* Ship the ACL for now. we will copy it into buf later. */
2663 ptr += sizeof(struct smb3_acl);
2665 /* create one ACE to hold the mode embedded in reserved special SID */
2666 acelen = setup_special_mode_ACE((struct cifs_ace *)ptr, (__u64)mode);
2668 acl_size = acelen + sizeof(struct smb3_acl);
2672 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
2673 acelen = setup_special_user_owner_ACE((struct cifs_ace *)ptr);
2679 /* and one more ACE to allow access for authenticated users */
2680 acelen = setup_authusers_ACE((struct cifs_ace *)ptr);
2685 acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2686 acl.AclSize = cpu_to_le16(acl_size);
2687 acl.AceCount = cpu_to_le16(ace_count);
2688 /* acl.Sbz1 and Sbz2 MBZ so are not set here, but initialized above */
2689 memcpy(aclptr, &acl, sizeof(struct smb3_acl));
2691 buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2692 *len = round_up((unsigned int)(ptr - (__u8 *)buf), 8);
2698 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner)
2700 unsigned int num = *num_iovec;
2701 unsigned int len = 0;
2703 iov[num].iov_base = create_sd_buf(mode, set_owner, &len);
2704 if (iov[num].iov_base == NULL)
2706 iov[num].iov_len = len;
2707 *num_iovec = num + 1;
2711 static struct crt_query_id_ctxt *
2712 create_query_id_buf(void)
2714 struct crt_query_id_ctxt *buf;
2716 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2720 buf->ccontext.DataOffset = cpu_to_le16(0);
2721 buf->ccontext.DataLength = cpu_to_le32(0);
2722 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2723 (struct crt_query_id_ctxt, Name));
2724 buf->ccontext.NameLength = cpu_to_le16(4);
2725 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2733 /* See MS-SMB2 2.2.13.2.9 */
2735 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2737 unsigned int num = *num_iovec;
2739 iov[num].iov_base = create_query_id_buf();
2740 if (iov[num].iov_base == NULL)
2742 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2743 *num_iovec = num + 1;
2747 static void add_ea_context(struct cifs_open_parms *oparms,
2748 struct kvec *rq_iov, unsigned int *num_iovs)
2750 struct kvec *iov = oparms->ea_cctx;
2752 if (iov && iov->iov_base && iov->iov_len) {
2753 rq_iov[(*num_iovs)++] = *iov;
2754 memset(iov, 0, sizeof(*iov));
2759 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2760 const char *treename, const __le16 *path)
2762 int treename_len, path_len;
2763 struct nls_table *cp;
2764 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2769 treename_len = strlen(treename);
2770 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2776 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2778 /* make room for one path separator only if @path isn't empty */
2779 *out_len = treename_len + (path[0] ? 1 : 0) + path_len;
2782 * final path needs to be 8-byte aligned as specified in
2783 * MS-SMB2 2.2.13 SMB2 CREATE Request.
2785 *out_size = round_up(*out_len * sizeof(__le16), 8);
2786 *out_path = kzalloc(*out_size + sizeof(__le16) /* null */, GFP_KERNEL);
2790 cp = load_nls_default();
2791 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2793 /* Do not append the separator if the path is empty */
2794 if (path[0] != cpu_to_le16(0x0000)) {
2795 UniStrcat((wchar_t *)*out_path, (wchar_t *)sep);
2796 UniStrcat((wchar_t *)*out_path, (wchar_t *)path);
2804 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2805 umode_t mode, struct cifs_tcon *tcon,
2806 const char *full_path,
2807 struct cifs_sb_info *cifs_sb)
2809 struct smb_rqst rqst;
2810 struct smb2_create_req *req;
2811 struct smb2_create_rsp *rsp = NULL;
2812 struct cifs_ses *ses = tcon->ses;
2813 struct kvec iov[3]; /* make sure at least one for each open context */
2814 struct kvec rsp_iov = {NULL, 0};
2817 __le16 *copy_path = NULL;
2820 unsigned int n_iov = 2;
2821 __u32 file_attributes = 0;
2822 char *pc_buf = NULL;
2824 unsigned int total_len;
2825 __le16 *utf16_path = NULL;
2826 struct TCP_Server_Info *server;
2827 int retries = 0, cur_sleep = 1;
2830 /* reinitialize for possible replay */
2833 server = cifs_pick_channel(ses);
2835 cifs_dbg(FYI, "mkdir\n");
2837 /* resource #1: path allocation */
2838 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2842 if (!ses || !server) {
2847 /* resource #2: request */
2848 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2849 (void **) &req, &total_len);
2854 if (smb3_encryption_required(tcon))
2855 flags |= CIFS_TRANSFORM_REQ;
2857 req->ImpersonationLevel = IL_IMPERSONATION;
2858 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2859 /* File attributes ignored on open (used in create though) */
2860 req->FileAttributes = cpu_to_le32(file_attributes);
2861 req->ShareAccess = FILE_SHARE_ALL_LE;
2862 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2863 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2865 iov[0].iov_base = (char *)req;
2866 /* -1 since last byte is buf[0] which is sent below (path) */
2867 iov[0].iov_len = total_len - 1;
2869 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2871 /* [MS-SMB2] 2.2.13 NameOffset:
2872 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2873 * the SMB2 header, the file name includes a prefix that will
2874 * be processed during DFS name normalization as specified in
2875 * section 3.3.5.9. Otherwise, the file name is relative to
2876 * the share that is identified by the TreeId in the SMB2
2879 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2882 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2883 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2885 tcon->tree_name, utf16_path);
2889 req->NameLength = cpu_to_le16(name_len * 2);
2890 uni_path_len = copy_size;
2891 /* free before overwriting resource */
2893 utf16_path = copy_path;
2895 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2896 /* MUST set path len (NameLength) to 0 opening root of share */
2897 req->NameLength = cpu_to_le16(uni_path_len - 2);
2898 if (uni_path_len % 8 != 0) {
2899 copy_size = roundup(uni_path_len, 8);
2900 copy_path = kzalloc(copy_size, GFP_KERNEL);
2905 memcpy((char *)copy_path, (const char *)utf16_path,
2907 uni_path_len = copy_size;
2908 /* free before overwriting resource */
2910 utf16_path = copy_path;
2914 iov[1].iov_len = uni_path_len;
2915 iov[1].iov_base = utf16_path;
2916 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2918 if (tcon->posix_extensions) {
2919 /* resource #3: posix buf */
2920 rc = add_posix_context(iov, &n_iov, mode);
2923 req->CreateContextsOffset = cpu_to_le32(
2924 sizeof(struct smb2_create_req) +
2926 pc_buf = iov[n_iov-1].iov_base;
2930 memset(&rqst, 0, sizeof(struct smb_rqst));
2932 rqst.rq_nvec = n_iov;
2934 /* no need to inc num_remote_opens because we close it just below */
2935 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, full_path, CREATE_NOT_FILE,
2936 FILE_WRITE_ATTRIBUTES);
2939 smb2_set_replay(server, &rqst);
2941 /* resource #4: response buffer */
2942 rc = cifs_send_recv(xid, ses, server,
2943 &rqst, &resp_buftype, flags, &rsp_iov);
2945 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2946 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2948 FILE_WRITE_ATTRIBUTES, rc);
2949 goto err_free_rsp_buf;
2953 * Although unlikely to be possible for rsp to be null and rc not set,
2954 * adding check below is slightly safer long term (and quiets Coverity
2957 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2964 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
2965 CREATE_NOT_FILE, FILE_WRITE_ATTRIBUTES);
2967 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2969 /* Eventually save off posix specific response info and timestaps */
2972 free_rsp_buf(resp_buftype, rsp);
2975 cifs_small_buf_release(req);
2979 if (is_replayable_error(rc) &&
2980 smb2_should_replay(tcon, &retries, &cur_sleep))
2987 SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2988 struct smb_rqst *rqst, __u8 *oplock,
2989 struct cifs_open_parms *oparms, __le16 *path)
2991 struct smb2_create_req *req;
2992 unsigned int n_iov = 2;
2993 __u32 file_attributes = 0;
2996 unsigned int total_len;
2997 struct kvec *iov = rqst->rq_iov;
3001 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
3002 (void **) &req, &total_len);
3006 iov[0].iov_base = (char *)req;
3007 /* -1 since last byte is buf[0] which is sent below (path) */
3008 iov[0].iov_len = total_len - 1;
3010 if (oparms->create_options & CREATE_OPTION_READONLY)
3011 file_attributes |= ATTR_READONLY;
3012 if (oparms->create_options & CREATE_OPTION_SPECIAL)
3013 file_attributes |= ATTR_SYSTEM;
3015 req->ImpersonationLevel = IL_IMPERSONATION;
3016 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
3017 /* File attributes ignored on open (used in create though) */
3018 req->FileAttributes = cpu_to_le32(file_attributes);
3019 req->ShareAccess = FILE_SHARE_ALL_LE;
3021 req->CreateDisposition = cpu_to_le32(oparms->disposition);
3022 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
3023 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
3025 /* [MS-SMB2] 2.2.13 NameOffset:
3026 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
3027 * the SMB2 header, the file name includes a prefix that will
3028 * be processed during DFS name normalization as specified in
3029 * section 3.3.5.9. Otherwise, the file name is relative to
3030 * the share that is identified by the TreeId in the SMB2
3033 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
3036 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
3037 rc = alloc_path_with_tree_prefix(©_path, ©_size,
3039 tcon->tree_name, path);
3042 req->NameLength = cpu_to_le16(name_len * 2);
3043 uni_path_len = copy_size;
3046 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
3047 /* MUST set path len (NameLength) to 0 opening root of share */
3048 req->NameLength = cpu_to_le16(uni_path_len - 2);
3049 copy_size = round_up(uni_path_len, 8);
3050 copy_path = kzalloc(copy_size, GFP_KERNEL);
3053 memcpy((char *)copy_path, (const char *)path,
3055 uni_path_len = copy_size;
3059 iov[1].iov_len = uni_path_len;
3060 iov[1].iov_base = path;
3062 if ((!server->oplocks) || (tcon->no_lease))
3063 *oplock = SMB2_OPLOCK_LEVEL_NONE;
3065 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
3066 *oplock == SMB2_OPLOCK_LEVEL_NONE)
3067 req->RequestedOplockLevel = *oplock;
3068 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
3069 (oparms->create_options & CREATE_NOT_FILE))
3070 req->RequestedOplockLevel = *oplock; /* no srv lease support */
3072 rc = add_lease_context(server, req, iov, &n_iov,
3073 oparms->fid->lease_key, oplock);
3078 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3079 rc = add_durable_context(iov, &n_iov, oparms,
3080 tcon->use_persistent);
3085 if (tcon->posix_extensions) {
3086 rc = add_posix_context(iov, &n_iov, oparms->mode);
3091 if (tcon->snapshot_time) {
3092 cifs_dbg(FYI, "adding snapshot context\n");
3093 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
3098 if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) {
3102 if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
3103 (oparms->mode != ACL_NO_MODE))
3107 oparms->mode = ACL_NO_MODE;
3110 if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
3115 if (set_owner | set_mode) {
3116 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
3117 rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner);
3123 add_query_id_context(iov, &n_iov);
3124 add_ea_context(oparms, iov, &n_iov);
3128 * We have create contexts behind iov[1] (the file
3129 * name), point at them from the main create request
3131 req->CreateContextsOffset = cpu_to_le32(
3132 sizeof(struct smb2_create_req) +
3134 req->CreateContextsLength = 0;
3136 for (unsigned int i = 2; i < (n_iov-1); i++) {
3137 struct kvec *v = &iov[i];
3138 size_t len = v->iov_len;
3139 struct create_context *cctx =
3140 (struct create_context *)v->iov_base;
3142 cctx->Next = cpu_to_le32(len);
3143 le32_add_cpu(&req->CreateContextsLength, len);
3145 le32_add_cpu(&req->CreateContextsLength,
3146 iov[n_iov-1].iov_len);
3149 rqst->rq_nvec = n_iov;
3153 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
3154 * All other vectors are freed by kfree().
3157 SMB2_open_free(struct smb_rqst *rqst)
3161 if (rqst && rqst->rq_iov) {
3162 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
3163 for (i = 1; i < rqst->rq_nvec; i++)
3164 if (rqst->rq_iov[i].iov_base != smb2_padding)
3165 kfree(rqst->rq_iov[i].iov_base);
3170 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
3171 __u8 *oplock, struct smb2_file_all_info *buf,
3172 struct create_posix_rsp *posix,
3173 struct kvec *err_iov, int *buftype)
3175 struct smb_rqst rqst;
3176 struct smb2_create_rsp *rsp = NULL;
3177 struct cifs_tcon *tcon = oparms->tcon;
3178 struct cifs_ses *ses = tcon->ses;
3179 struct TCP_Server_Info *server;
3180 struct kvec iov[SMB2_CREATE_IOV_SIZE];
3181 struct kvec rsp_iov = {NULL, 0};
3182 int resp_buftype = CIFS_NO_BUFFER;
3185 int retries = 0, cur_sleep = 1;
3188 /* reinitialize for possible replay */
3190 server = cifs_pick_channel(ses);
3191 oparms->replay = !!(retries);
3193 cifs_dbg(FYI, "create/open\n");
3194 if (!ses || !server)
3197 if (smb3_encryption_required(tcon))
3198 flags |= CIFS_TRANSFORM_REQ;
3200 memset(&rqst, 0, sizeof(struct smb_rqst));
3201 memset(&iov, 0, sizeof(iov));
3203 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
3205 rc = SMB2_open_init(tcon, server,
3206 &rqst, oplock, oparms, path);
3210 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid, oparms->path,
3211 oparms->create_options, oparms->desired_access);
3214 smb2_set_replay(server, &rqst);
3216 rc = cifs_send_recv(xid, ses, server,
3217 &rqst, &resp_buftype, flags,
3219 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
3222 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
3223 if (err_iov && rsp) {
3225 *buftype = resp_buftype;
3226 resp_buftype = CIFS_NO_BUFFER;
3229 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
3230 oparms->create_options, oparms->desired_access, rc);
3231 if (rc == -EREMCHG) {
3232 pr_warn_once("server share %s deleted\n",
3234 tcon->need_reconnect = true;
3237 } else if (rsp == NULL) /* unlikely to happen, but safer to check */
3240 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
3241 oparms->create_options, oparms->desired_access);
3243 atomic_inc(&tcon->num_remote_opens);
3244 oparms->fid->persistent_fid = rsp->PersistentFileId;
3245 oparms->fid->volatile_fid = rsp->VolatileFileId;
3246 oparms->fid->access = oparms->desired_access;
3247 #ifdef CONFIG_CIFS_DEBUG2
3248 oparms->fid->mid = le64_to_cpu(rsp->hdr.MessageId);
3249 #endif /* CIFS_DEBUG2 */
3252 buf->CreationTime = rsp->CreationTime;
3253 buf->LastAccessTime = rsp->LastAccessTime;
3254 buf->LastWriteTime = rsp->LastWriteTime;
3255 buf->ChangeTime = rsp->ChangeTime;
3256 buf->AllocationSize = rsp->AllocationSize;
3257 buf->EndOfFile = rsp->EndofFile;
3258 buf->Attributes = rsp->FileAttributes;
3259 buf->NumberOfLinks = cpu_to_le32(1);
3260 buf->DeletePending = 0;
3264 rc = smb2_parse_contexts(server, &rsp_iov, &oparms->fid->epoch,
3265 oparms->fid->lease_key, oplock, buf, posix);
3267 SMB2_open_free(&rqst);
3268 free_rsp_buf(resp_buftype, rsp);
3270 if (is_replayable_error(rc) &&
3271 smb2_should_replay(tcon, &retries, &cur_sleep))
3278 SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3279 struct smb_rqst *rqst,
3280 u64 persistent_fid, u64 volatile_fid, u32 opcode,
3281 char *in_data, u32 indatalen,
3282 __u32 max_response_size)
3284 struct smb2_ioctl_req *req;
3285 struct kvec *iov = rqst->rq_iov;
3286 unsigned int total_len;
3290 rc = smb2_ioctl_req_init(opcode, tcon, server,
3291 (void **) &req, &total_len);
3297 * indatalen is usually small at a couple of bytes max, so
3298 * just allocate through generic pool
3300 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
3302 cifs_small_buf_release(req);
3307 req->CtlCode = cpu_to_le32(opcode);
3308 req->PersistentFileId = persistent_fid;
3309 req->VolatileFileId = volatile_fid;
3311 iov[0].iov_base = (char *)req;
3313 * If no input data, the size of ioctl struct in
3314 * protocol spec still includes a 1 byte data buffer,
3315 * but if input data passed to ioctl, we do not
3316 * want to double count this, so we do not send
3317 * the dummy one byte of data in iovec[0] if sending
3318 * input data (in iovec[1]).
3321 req->InputCount = cpu_to_le32(indatalen);
3322 /* do not set InputOffset if no input data */
3324 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
3326 iov[0].iov_len = total_len - 1;
3327 iov[1].iov_base = in_data_buf;
3328 iov[1].iov_len = indatalen;
3331 iov[0].iov_len = total_len;
3334 req->OutputOffset = 0;
3335 req->OutputCount = 0; /* MBZ */
3338 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
3339 * We Could increase default MaxOutputResponse, but that could require
3340 * more credits. Windows typically sets this smaller, but for some
3341 * ioctls it may be useful to allow server to send more. No point
3342 * limiting what the server can send as long as fits in one credit
3343 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
3344 * to increase this limit up in the future.
3345 * Note that for snapshot queries that servers like Azure expect that
3346 * the first query be minimal size (and just used to get the number/size
3347 * of previous versions) so response size must be specified as EXACTLY
3348 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
3349 * of eight bytes. Currently that is the only case where we set max
3350 * response size smaller.
3352 req->MaxOutputResponse = cpu_to_le32(max_response_size);
3353 req->hdr.CreditCharge =
3354 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
3355 SMB2_MAX_BUFFER_SIZE));
3356 /* always an FSCTL (for now) */
3357 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
3359 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
3360 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
3361 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
3367 SMB2_ioctl_free(struct smb_rqst *rqst)
3371 if (rqst && rqst->rq_iov) {
3372 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3373 for (i = 1; i < rqst->rq_nvec; i++)
3374 if (rqst->rq_iov[i].iov_base != smb2_padding)
3375 kfree(rqst->rq_iov[i].iov_base);
3381 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
3384 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3385 u64 volatile_fid, u32 opcode, char *in_data, u32 indatalen,
3386 u32 max_out_data_len, char **out_data,
3387 u32 *plen /* returned data len */)
3389 struct smb_rqst rqst;
3390 struct smb2_ioctl_rsp *rsp = NULL;
3391 struct cifs_ses *ses;
3392 struct TCP_Server_Info *server;
3393 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
3394 struct kvec rsp_iov = {NULL, 0};
3395 int resp_buftype = CIFS_NO_BUFFER;
3398 int retries = 0, cur_sleep = 1;
3408 /* reinitialize for possible replay */
3410 server = cifs_pick_channel(ses);
3415 cifs_dbg(FYI, "SMB2 IOCTL\n");
3417 if (out_data != NULL)
3420 /* zero out returned data len, in case of error */
3424 if (smb3_encryption_required(tcon))
3425 flags |= CIFS_TRANSFORM_REQ;
3427 memset(&rqst, 0, sizeof(struct smb_rqst));
3428 memset(&iov, 0, sizeof(iov));
3430 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
3432 rc = SMB2_ioctl_init(tcon, server,
3433 &rqst, persistent_fid, volatile_fid, opcode,
3434 in_data, indatalen, max_out_data_len);
3439 smb2_set_replay(server, &rqst);
3441 rc = cifs_send_recv(xid, ses, server,
3442 &rqst, &resp_buftype, flags,
3444 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
3447 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3448 ses->Suid, 0, opcode, rc);
3450 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
3451 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3453 } else if (rc == -EINVAL) {
3454 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3455 (opcode != FSCTL_SRV_COPYCHUNK)) {
3456 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3459 } else if (rc == -E2BIG) {
3460 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3461 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3466 /* check if caller wants to look at return data or just return rc */
3467 if ((plen == NULL) || (out_data == NULL))
3471 * Although unlikely to be possible for rsp to be null and rc not set,
3472 * adding check below is slightly safer long term (and quiets Coverity
3480 *plen = le32_to_cpu(rsp->OutputCount);
3482 /* We check for obvious errors in the output buffer length and offset */
3484 goto ioctl_exit; /* server returned no data */
3485 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
3486 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
3492 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
3493 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
3494 le32_to_cpu(rsp->OutputOffset));
3500 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3502 if (*out_data == NULL) {
3508 SMB2_ioctl_free(&rqst);
3509 free_rsp_buf(resp_buftype, rsp);
3511 if (is_replayable_error(rc) &&
3512 smb2_should_replay(tcon, &retries, &cur_sleep))
3519 * Individual callers to ioctl worker function follow
3523 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3524 u64 persistent_fid, u64 volatile_fid)
3527 struct compress_ioctl fsctl_input;
3528 char *ret_data = NULL;
3530 fsctl_input.CompressionState =
3531 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3533 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3534 FSCTL_SET_COMPRESSION,
3535 (char *)&fsctl_input /* data input */,
3536 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3537 &ret_data /* out data */, NULL);
3539 cifs_dbg(FYI, "set compression rc %d\n", rc);
3545 SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3546 struct smb_rqst *rqst,
3547 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
3549 struct smb2_close_req *req;
3550 struct kvec *iov = rqst->rq_iov;
3551 unsigned int total_len;
3554 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
3555 (void **) &req, &total_len);
3559 req->PersistentFileId = persistent_fid;
3560 req->VolatileFileId = volatile_fid;
3562 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3565 iov[0].iov_base = (char *)req;
3566 iov[0].iov_len = total_len;
3572 SMB2_close_free(struct smb_rqst *rqst)
3574 if (rqst && rqst->rq_iov)
3575 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3579 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3580 u64 persistent_fid, u64 volatile_fid,
3581 struct smb2_file_network_open_info *pbuf)
3583 struct smb_rqst rqst;
3584 struct smb2_close_rsp *rsp = NULL;
3585 struct cifs_ses *ses = tcon->ses;
3586 struct TCP_Server_Info *server;
3588 struct kvec rsp_iov;
3589 int resp_buftype = CIFS_NO_BUFFER;
3592 bool query_attrs = false;
3593 int retries = 0, cur_sleep = 1;
3596 /* reinitialize for possible replay */
3598 query_attrs = false;
3599 server = cifs_pick_channel(ses);
3601 cifs_dbg(FYI, "Close\n");
3603 if (!ses || !server)
3606 if (smb3_encryption_required(tcon))
3607 flags |= CIFS_TRANSFORM_REQ;
3609 memset(&rqst, 0, sizeof(struct smb_rqst));
3610 memset(&iov, 0, sizeof(iov));
3614 /* check if need to ask server to return timestamps in close response */
3618 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3619 rc = SMB2_close_init(tcon, server,
3620 &rqst, persistent_fid, volatile_fid,
3626 smb2_set_replay(server, &rqst);
3628 rc = cifs_send_recv(xid, ses, server,
3629 &rqst, &resp_buftype, flags, &rsp_iov);
3630 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
3633 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
3634 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3638 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3641 memcpy(&pbuf->network_open_info,
3642 &rsp->network_open_info,
3643 sizeof(pbuf->network_open_info));
3644 atomic_dec(&tcon->num_remote_opens);
3648 SMB2_close_free(&rqst);
3649 free_rsp_buf(resp_buftype, rsp);
3651 /* retry close in a worker thread if this one is interrupted */
3652 if (is_interrupt_error(rc)) {
3655 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3658 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3659 persistent_fid, tmp_rc);
3662 if (is_replayable_error(rc) &&
3663 smb2_should_replay(tcon, &retries, &cur_sleep))
3670 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3671 u64 persistent_fid, u64 volatile_fid)
3673 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3677 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3678 struct kvec *iov, unsigned int min_buf_size)
3680 unsigned int smb_len = iov->iov_len;
3681 char *end_of_smb = smb_len + (char *)iov->iov_base;
3682 char *begin_of_buf = offset + (char *)iov->iov_base;
3683 char *end_of_buf = begin_of_buf + buffer_length;
3686 if (buffer_length < min_buf_size) {
3687 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3688 buffer_length, min_buf_size);
3692 /* check if beyond RFC1001 maximum length */
3693 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3694 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3695 buffer_length, smb_len);
3699 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3700 cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
3708 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3709 * Caller must free buffer.
3712 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3713 struct kvec *iov, unsigned int minbufsize,
3716 char *begin_of_buf = offset + (char *)iov->iov_base;
3722 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3726 memcpy(data, begin_of_buf, minbufsize);
3732 SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3733 struct smb_rqst *rqst,
3734 u64 persistent_fid, u64 volatile_fid,
3735 u8 info_class, u8 info_type, u32 additional_info,
3736 size_t output_len, size_t input_len, void *input)
3738 struct smb2_query_info_req *req;
3739 struct kvec *iov = rqst->rq_iov;
3740 unsigned int total_len;
3744 if (unlikely(check_add_overflow(input_len, sizeof(*req), &len) ||
3745 len > CIFSMaxBufSize))
3748 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
3749 (void **) &req, &total_len);
3753 req->InfoType = info_type;
3754 req->FileInfoClass = info_class;
3755 req->PersistentFileId = persistent_fid;
3756 req->VolatileFileId = volatile_fid;
3757 req->AdditionalInformation = cpu_to_le32(additional_info);
3759 req->OutputBufferLength = cpu_to_le32(output_len);
3761 req->InputBufferLength = cpu_to_le32(input_len);
3762 /* total_len for smb query request never close to le16 max */
3763 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3764 memcpy(req->Buffer, input, input_len);
3767 iov[0].iov_base = (char *)req;
3769 iov[0].iov_len = len;
3774 SMB2_query_info_free(struct smb_rqst *rqst)
3776 if (rqst && rqst->rq_iov)
3777 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
3781 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3782 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3783 u32 additional_info, size_t output_len, size_t min_len, void **data,
3786 struct smb_rqst rqst;
3787 struct smb2_query_info_rsp *rsp = NULL;
3789 struct kvec rsp_iov;
3791 int resp_buftype = CIFS_NO_BUFFER;
3792 struct cifs_ses *ses = tcon->ses;
3793 struct TCP_Server_Info *server;
3795 bool allocated = false;
3796 int retries = 0, cur_sleep = 1;
3798 cifs_dbg(FYI, "Query Info\n");
3804 /* reinitialize for possible replay */
3807 server = cifs_pick_channel(ses);
3812 if (smb3_encryption_required(tcon))
3813 flags |= CIFS_TRANSFORM_REQ;
3815 memset(&rqst, 0, sizeof(struct smb_rqst));
3816 memset(&iov, 0, sizeof(iov));
3820 rc = SMB2_query_info_init(tcon, server,
3821 &rqst, persistent_fid, volatile_fid,
3822 info_class, info_type, additional_info,
3823 output_len, 0, NULL);
3827 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3828 ses->Suid, info_class, (__u32)info_type);
3831 smb2_set_replay(server, &rqst);
3833 rc = cifs_send_recv(xid, ses, server,
3834 &rqst, &resp_buftype, flags, &rsp_iov);
3835 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3838 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3839 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3840 ses->Suid, info_class, (__u32)info_type, rc);
3844 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3845 ses->Suid, info_class, (__u32)info_type);
3848 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3850 *data = kmalloc(*dlen, GFP_KERNEL);
3853 "Error %d allocating memory for acl\n",
3863 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3864 le32_to_cpu(rsp->OutputBufferLength),
3865 &rsp_iov, dlen ? *dlen : min_len, *data);
3866 if (rc && allocated) {
3873 SMB2_query_info_free(&rqst);
3874 free_rsp_buf(resp_buftype, rsp);
3876 if (is_replayable_error(rc) &&
3877 smb2_should_replay(tcon, &retries, &cur_sleep))
3883 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3884 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3886 return query_info(xid, tcon, persistent_fid, volatile_fid,
3887 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3888 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3889 sizeof(struct smb2_file_all_info), (void **)&data,
3894 /* currently unused, as now we are doing compounding instead (see smb311_posix_query_path_info) */
3896 SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3897 u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen)
3899 size_t output_len = sizeof(struct smb311_posix_qinfo *) +
3900 (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2);
3903 return query_info(xid, tcon, persistent_fid, volatile_fid,
3904 SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0,
3905 output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen);
3906 /* Note caller must free "data" (passed in above). It may be allocated in query_info call */
3911 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3912 u64 persistent_fid, u64 volatile_fid,
3913 void **data, u32 *plen, u32 extra_info)
3915 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
3919 return query_info(xid, tcon, persistent_fid, volatile_fid,
3920 0, SMB2_O_INFO_SECURITY, additional_info,
3921 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3925 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3926 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3928 return query_info(xid, tcon, persistent_fid, volatile_fid,
3929 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3930 sizeof(struct smb2_file_internal_info),
3931 sizeof(struct smb2_file_internal_info),
3932 (void **)&uniqueid, NULL);
3936 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3937 * See MS-SMB2 2.2.35 and 2.2.36
3941 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3942 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3943 u64 persistent_fid, u64 volatile_fid,
3944 u32 completion_filter, bool watch_tree)
3946 struct smb2_change_notify_req *req;
3947 struct kvec *iov = rqst->rq_iov;
3948 unsigned int total_len;
3951 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
3952 (void **) &req, &total_len);
3956 req->PersistentFileId = persistent_fid;
3957 req->VolatileFileId = volatile_fid;
3958 /* See note 354 of MS-SMB2, 64K max */
3959 req->OutputBufferLength =
3960 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3961 req->CompletionFilter = cpu_to_le32(completion_filter);
3963 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3967 iov[0].iov_base = (char *)req;
3968 iov[0].iov_len = total_len;
3974 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3975 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3976 u32 completion_filter, u32 max_out_data_len, char **out_data,
3977 u32 *plen /* returned data len */)
3979 struct cifs_ses *ses = tcon->ses;
3980 struct TCP_Server_Info *server;
3981 struct smb_rqst rqst;
3982 struct smb2_change_notify_rsp *smb_rsp;
3984 struct kvec rsp_iov = {NULL, 0};
3985 int resp_buftype = CIFS_NO_BUFFER;
3988 int retries = 0, cur_sleep = 1;
3991 /* reinitialize for possible replay */
3993 server = cifs_pick_channel(ses);
3995 cifs_dbg(FYI, "change notify\n");
3996 if (!ses || !server)
3999 if (smb3_encryption_required(tcon))
4000 flags |= CIFS_TRANSFORM_REQ;
4002 memset(&rqst, 0, sizeof(struct smb_rqst));
4003 memset(&iov, 0, sizeof(iov));
4010 rc = SMB2_notify_init(xid, &rqst, tcon, server,
4011 persistent_fid, volatile_fid,
4012 completion_filter, watch_tree);
4016 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
4017 (u8)watch_tree, completion_filter);
4020 smb2_set_replay(server, &rqst);
4022 rc = cifs_send_recv(xid, ses, server,
4023 &rqst, &resp_buftype, flags, &rsp_iov);
4026 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
4027 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
4028 (u8)watch_tree, completion_filter, rc);
4030 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
4031 ses->Suid, (u8)watch_tree, completion_filter);
4032 /* validate that notify information is plausible */
4033 if ((rsp_iov.iov_base == NULL) ||
4034 (rsp_iov.iov_len < sizeof(struct smb2_change_notify_rsp) + 1))
4037 smb_rsp = (struct smb2_change_notify_rsp *)rsp_iov.iov_base;
4039 smb2_validate_iov(le16_to_cpu(smb_rsp->OutputBufferOffset),
4040 le32_to_cpu(smb_rsp->OutputBufferLength), &rsp_iov,
4041 sizeof(struct file_notify_information));
4043 *out_data = kmemdup((char *)smb_rsp + le16_to_cpu(smb_rsp->OutputBufferOffset),
4044 le32_to_cpu(smb_rsp->OutputBufferLength), GFP_KERNEL);
4045 if (*out_data == NULL) {
4049 *plen = le32_to_cpu(smb_rsp->OutputBufferLength);
4054 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
4055 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4057 if (is_replayable_error(rc) &&
4058 smb2_should_replay(tcon, &retries, &cur_sleep))
4067 * This is a no-op for now. We're not really interested in the reply, but
4068 * rather in the fact that the server sent one and that server->lstrp
4071 * FIXME: maybe we should consider checking that the reply matches request?
4074 smb2_echo_callback(struct mid_q_entry *mid)
4076 struct TCP_Server_Info *server = mid->callback_data;
4077 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
4078 struct cifs_credits credits = { .value = 0, .instance = 0 };
4080 if (mid->mid_state == MID_RESPONSE_RECEIVED
4081 || mid->mid_state == MID_RESPONSE_MALFORMED) {
4082 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4083 credits.instance = server->reconnect_instance;
4087 add_credits(server, &credits, CIFS_ECHO_OP);
4090 void smb2_reconnect_server(struct work_struct *work)
4092 struct TCP_Server_Info *server = container_of(work,
4093 struct TCP_Server_Info, reconnect.work);
4094 struct TCP_Server_Info *pserver;
4095 struct cifs_ses *ses, *ses2;
4096 struct cifs_tcon *tcon, *tcon2;
4097 struct list_head tmp_list, tmp_ses_list;
4098 bool ses_exist = false;
4099 bool tcon_selected = false;
4101 bool resched = false;
4103 /* first check if ref count has reached 0, if not inc ref count */
4104 spin_lock(&cifs_tcp_ses_lock);
4105 if (!server->srv_count) {
4106 spin_unlock(&cifs_tcp_ses_lock);
4109 server->srv_count++;
4110 spin_unlock(&cifs_tcp_ses_lock);
4112 /* If server is a channel, select the primary channel */
4113 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
4115 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
4116 mutex_lock(&pserver->reconnect_mutex);
4118 /* if the server is marked for termination, drop the ref count here */
4119 if (server->terminate) {
4120 cifs_put_tcp_session(server, true);
4121 mutex_unlock(&pserver->reconnect_mutex);
4125 INIT_LIST_HEAD(&tmp_list);
4126 INIT_LIST_HEAD(&tmp_ses_list);
4127 cifs_dbg(FYI, "Reconnecting tcons and channels\n");
4129 spin_lock(&cifs_tcp_ses_lock);
4130 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
4131 spin_lock(&ses->ses_lock);
4132 if (ses->ses_status == SES_EXITING) {
4133 spin_unlock(&ses->ses_lock);
4136 spin_unlock(&ses->ses_lock);
4138 tcon_selected = false;
4140 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
4141 if (tcon->need_reconnect || tcon->need_reopen_files) {
4143 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
4144 netfs_trace_tcon_ref_get_reconnect_server);
4145 list_add_tail(&tcon->rlist, &tmp_list);
4146 tcon_selected = true;
4150 * IPC has the same lifetime as its session and uses its
4153 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
4154 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
4155 tcon_selected = true;
4156 cifs_smb_ses_inc_refcount(ses);
4159 * handle the case where channel needs to reconnect
4160 * binding session, but tcon is healthy (some other channel
4163 spin_lock(&ses->chan_lock);
4164 if (!tcon_selected && cifs_chan_needs_reconnect(ses, server)) {
4165 list_add_tail(&ses->rlist, &tmp_ses_list);
4167 cifs_smb_ses_inc_refcount(ses);
4169 spin_unlock(&ses->chan_lock);
4171 spin_unlock(&cifs_tcp_ses_lock);
4173 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
4174 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server, true);
4176 cifs_reopen_persistent_handles(tcon);
4179 list_del_init(&tcon->rlist);
4181 cifs_put_smb_ses(tcon->ses);
4183 cifs_put_tcon(tcon, netfs_trace_tcon_ref_put_reconnect_server);
4189 /* allocate a dummy tcon struct used for reconnect */
4190 tcon = tcon_info_alloc(false, netfs_trace_tcon_ref_new_reconnect_server);
4193 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
4194 list_del_init(&ses->rlist);
4195 cifs_put_smb_ses(ses);
4200 tcon->status = TID_GOOD;
4201 tcon->retry = false;
4202 tcon->need_reconnect = false;
4204 /* now reconnect sessions for necessary channels */
4205 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
4207 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server, true);
4210 list_del_init(&ses->rlist);
4211 cifs_put_smb_ses(ses);
4213 tconInfoFree(tcon, netfs_trace_tcon_ref_free_reconnect_server);
4216 cifs_dbg(FYI, "Reconnecting tcons and channels finished\n");
4218 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
4219 mutex_unlock(&pserver->reconnect_mutex);
4221 /* now we can safely release srv struct */
4222 cifs_put_tcp_session(server, true);
4226 SMB2_echo(struct TCP_Server_Info *server)
4228 struct smb2_echo_req *req;
4231 struct smb_rqst rqst = { .rq_iov = iov,
4233 unsigned int total_len;
4235 cifs_dbg(FYI, "In echo request for conn_id %lld\n", server->conn_id);
4237 spin_lock(&server->srv_lock);
4238 if (server->ops->need_neg &&
4239 server->ops->need_neg(server)) {
4240 spin_unlock(&server->srv_lock);
4241 /* No need to send echo on newly established connections */
4242 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
4245 spin_unlock(&server->srv_lock);
4247 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
4248 (void **)&req, &total_len);
4252 req->hdr.CreditRequest = cpu_to_le16(1);
4254 iov[0].iov_len = total_len;
4255 iov[0].iov_base = (char *)req;
4257 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
4258 server, CIFS_ECHO_OP, NULL);
4260 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
4262 cifs_small_buf_release(req);
4267 SMB2_flush_free(struct smb_rqst *rqst)
4269 if (rqst && rqst->rq_iov)
4270 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4274 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
4275 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
4276 u64 persistent_fid, u64 volatile_fid)
4278 struct smb2_flush_req *req;
4279 struct kvec *iov = rqst->rq_iov;
4280 unsigned int total_len;
4283 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
4284 (void **) &req, &total_len);
4288 req->PersistentFileId = persistent_fid;
4289 req->VolatileFileId = volatile_fid;
4291 iov[0].iov_base = (char *)req;
4292 iov[0].iov_len = total_len;
4298 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4301 struct cifs_ses *ses = tcon->ses;
4302 struct smb_rqst rqst;
4304 struct kvec rsp_iov = {NULL, 0};
4305 struct TCP_Server_Info *server;
4306 int resp_buftype = CIFS_NO_BUFFER;
4309 int retries = 0, cur_sleep = 1;
4312 /* reinitialize for possible replay */
4314 server = cifs_pick_channel(ses);
4316 cifs_dbg(FYI, "flush\n");
4317 if (!ses || !(ses->server))
4320 if (smb3_encryption_required(tcon))
4321 flags |= CIFS_TRANSFORM_REQ;
4323 memset(&rqst, 0, sizeof(struct smb_rqst));
4324 memset(&iov, 0, sizeof(iov));
4328 rc = SMB2_flush_init(xid, &rqst, tcon, server,
4329 persistent_fid, volatile_fid);
4333 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
4336 smb2_set_replay(server, &rqst);
4338 rc = cifs_send_recv(xid, ses, server,
4339 &rqst, &resp_buftype, flags, &rsp_iov);
4342 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
4343 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
4346 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
4350 SMB2_flush_free(&rqst);
4351 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4353 if (is_replayable_error(rc) &&
4354 smb2_should_replay(tcon, &retries, &cur_sleep))
4360 #ifdef CONFIG_CIFS_SMB_DIRECT
4361 static inline bool smb3_use_rdma_offload(struct cifs_io_parms *io_parms)
4363 struct TCP_Server_Info *server = io_parms->server;
4364 struct cifs_tcon *tcon = io_parms->tcon;
4366 /* we can only offload if we're connected */
4367 if (!server || !tcon)
4370 /* we can only offload on an rdma connection */
4371 if (!server->rdma || !server->smbd_conn)
4374 /* we don't support signed offload yet */
4378 /* we don't support encrypted offload yet */
4379 if (smb3_encryption_required(tcon))
4382 /* offload also has its overhead, so only do it if desired */
4383 if (io_parms->length < server->smbd_conn->rdma_readwrite_threshold)
4388 #endif /* CONFIG_CIFS_SMB_DIRECT */
4391 * To form a chain of read requests, any read requests after the first should
4392 * have the end_of_chain boolean set to true.
4395 smb2_new_read_req(void **buf, unsigned int *total_len,
4396 struct cifs_io_parms *io_parms, struct cifs_io_subrequest *rdata,
4397 unsigned int remaining_bytes, int request_type)
4400 struct smb2_read_req *req = NULL;
4401 struct smb2_hdr *shdr;
4402 struct TCP_Server_Info *server = io_parms->server;
4404 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
4405 (void **) &req, total_len);
4410 return -ECONNABORTED;
4413 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4415 req->PersistentFileId = io_parms->persistent_fid;
4416 req->VolatileFileId = io_parms->volatile_fid;
4417 req->ReadChannelInfoOffset = 0; /* reserved */
4418 req->ReadChannelInfoLength = 0; /* reserved */
4419 req->Channel = 0; /* reserved */
4420 req->MinimumCount = 0;
4421 req->Length = cpu_to_le32(io_parms->length);
4422 req->Offset = cpu_to_le64(io_parms->offset);
4424 trace_smb3_read_enter(rdata ? rdata->rreq->debug_id : 0,
4425 rdata ? rdata->subreq.debug_index : 0,
4426 rdata ? rdata->xid : 0,
4427 io_parms->persistent_fid,
4428 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4429 io_parms->offset, io_parms->length);
4430 #ifdef CONFIG_CIFS_SMB_DIRECT
4432 * If we want to do a RDMA write, fill in and append
4433 * smbd_buffer_descriptor_v1 to the end of read request
4435 if (smb3_use_rdma_offload(io_parms)) {
4436 struct smbd_buffer_descriptor_v1 *v1;
4437 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4439 rdata->mr = smbd_register_mr(server->smbd_conn, &rdata->subreq.io_iter,
4440 true, need_invalidate);
4444 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4445 if (need_invalidate)
4446 req->Channel = SMB2_CHANNEL_RDMA_V1;
4447 req->ReadChannelInfoOffset =
4448 cpu_to_le16(offsetof(struct smb2_read_req, Buffer));
4449 req->ReadChannelInfoLength =
4450 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4451 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4452 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
4453 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
4454 v1->length = cpu_to_le32(rdata->mr->mr->length);
4456 *total_len += sizeof(*v1) - 1;
4459 if (request_type & CHAINED_REQUEST) {
4460 if (!(request_type & END_OF_CHAIN)) {
4461 /* next 8-byte aligned request */
4462 *total_len = ALIGN(*total_len, 8);
4463 shdr->NextCommand = cpu_to_le32(*total_len);
4464 } else /* END_OF_CHAIN */
4465 shdr->NextCommand = 0;
4466 if (request_type & RELATED_REQUEST) {
4467 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
4469 * Related requests use info from previous read request
4472 shdr->SessionId = cpu_to_le64(0xFFFFFFFFFFFFFFFF);
4473 shdr->Id.SyncId.TreeId = cpu_to_le32(0xFFFFFFFF);
4474 req->PersistentFileId = (u64)-1;
4475 req->VolatileFileId = (u64)-1;
4478 if (remaining_bytes > io_parms->length)
4479 req->RemainingBytes = cpu_to_le32(remaining_bytes);
4481 req->RemainingBytes = 0;
4487 static void smb2_readv_worker(struct work_struct *work)
4489 struct cifs_io_subrequest *rdata =
4490 container_of(work, struct cifs_io_subrequest, subreq.work);
4492 netfs_subreq_terminated(&rdata->subreq,
4493 (rdata->result == 0 || rdata->result == -EAGAIN) ?
4494 rdata->got_bytes : rdata->result, true);
4498 smb2_readv_callback(struct mid_q_entry *mid)
4500 struct cifs_io_subrequest *rdata = mid->callback_data;
4501 struct cifs_tcon *tcon = tlink_tcon(rdata->req->cfile->tlink);
4502 struct TCP_Server_Info *server = rdata->server;
4503 struct smb2_hdr *shdr =
4504 (struct smb2_hdr *)rdata->iov[0].iov_base;
4505 struct cifs_credits credits = {
4508 .rreq_debug_id = rdata->rreq->debug_id,
4509 .rreq_debug_index = rdata->subreq.debug_index,
4511 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1], .rq_nvec = 1 };
4512 unsigned int rreq_debug_id = rdata->rreq->debug_id;
4513 unsigned int subreq_debug_index = rdata->subreq.debug_index;
4515 if (rdata->got_bytes) {
4516 rqst.rq_iter = rdata->subreq.io_iter;
4517 rqst.rq_iter_size = iov_iter_count(&rdata->subreq.io_iter);
4520 WARN_ONCE(rdata->server != mid->server,
4521 "rdata server %p != mid server %p",
4522 rdata->server, mid->server);
4524 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%zu\n",
4525 __func__, mid->mid, mid->mid_state, rdata->result,
4528 switch (mid->mid_state) {
4529 case MID_RESPONSE_RECEIVED:
4530 credits.value = le16_to_cpu(shdr->CreditRequest);
4531 credits.instance = server->reconnect_instance;
4532 /* result already set, check signature */
4533 if (server->sign && !mid->decrypted) {
4536 iov_iter_truncate(&rqst.rq_iter, rdata->got_bytes);
4537 rc = smb2_verify_signature(&rqst, server);
4539 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
4542 /* FIXME: should this be counted toward the initiating task? */
4543 task_io_account_read(rdata->got_bytes);
4544 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4546 case MID_REQUEST_SUBMITTED:
4547 case MID_RETRY_NEEDED:
4548 rdata->result = -EAGAIN;
4549 if (server->sign && rdata->got_bytes)
4550 /* reset bytes number since we can not check a sign */
4551 rdata->got_bytes = 0;
4552 /* FIXME: should this be counted toward the initiating task? */
4553 task_io_account_read(rdata->got_bytes);
4554 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4556 case MID_RESPONSE_MALFORMED:
4557 credits.value = le16_to_cpu(shdr->CreditRequest);
4558 credits.instance = server->reconnect_instance;
4561 rdata->result = -EIO;
4563 #ifdef CONFIG_CIFS_SMB_DIRECT
4565 * If this rdata has a memmory registered, the MR can be freed
4566 * MR needs to be freed as soon as I/O finishes to prevent deadlock
4567 * because they have limited number and are used for future I/Os
4570 smbd_deregister_mr(rdata->mr);
4574 if (rdata->result && rdata->result != -ENODATA) {
4575 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
4576 trace_smb3_read_err(rdata->rreq->debug_id,
4577 rdata->subreq.debug_index,
4579 rdata->req->cfile->fid.persistent_fid,
4580 tcon->tid, tcon->ses->Suid, rdata->subreq.start,
4581 rdata->subreq.len, rdata->result);
4583 trace_smb3_read_done(rdata->rreq->debug_id,
4584 rdata->subreq.debug_index,
4586 rdata->req->cfile->fid.persistent_fid,
4587 tcon->tid, tcon->ses->Suid,
4588 rdata->subreq.start, rdata->got_bytes);
4590 if (rdata->result == -ENODATA) {
4591 /* We may have got an EOF error because fallocate
4592 * failed to enlarge the file.
4594 if (rdata->subreq.start < rdata->subreq.rreq->i_size)
4597 trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, rdata->credits.value,
4598 server->credits, server->in_flight,
4599 0, cifs_trace_rw_credits_read_response_clear);
4600 rdata->credits.value = 0;
4601 INIT_WORK(&rdata->subreq.work, smb2_readv_worker);
4602 queue_work(cifsiod_wq, &rdata->subreq.work);
4604 trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, 0,
4605 server->credits, server->in_flight,
4606 credits.value, cifs_trace_rw_credits_read_response_add);
4607 add_credits(server, &credits, 0);
4610 /* smb2_async_readv - send an async read, and set up mid to handle result */
4612 smb2_async_readv(struct cifs_io_subrequest *rdata)
4616 struct smb2_hdr *shdr;
4617 struct cifs_io_parms io_parms;
4618 struct smb_rqst rqst = { .rq_iov = rdata->iov,
4620 struct TCP_Server_Info *server;
4621 struct cifs_tcon *tcon = tlink_tcon(rdata->req->cfile->tlink);
4622 unsigned int total_len;
4625 cifs_dbg(FYI, "%s: offset=%llu bytes=%zu\n",
4626 __func__, rdata->subreq.start, rdata->subreq.len);
4629 rdata->server = cifs_pick_channel(tcon->ses);
4631 io_parms.tcon = tlink_tcon(rdata->req->cfile->tlink);
4632 io_parms.server = server = rdata->server;
4633 io_parms.offset = rdata->subreq.start;
4634 io_parms.length = rdata->subreq.len;
4635 io_parms.persistent_fid = rdata->req->cfile->fid.persistent_fid;
4636 io_parms.volatile_fid = rdata->req->cfile->fid.volatile_fid;
4637 io_parms.pid = rdata->req->pid;
4639 rc = smb2_new_read_req(
4640 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
4644 if (smb3_encryption_required(io_parms.tcon))
4645 flags |= CIFS_TRANSFORM_REQ;
4647 rdata->iov[0].iov_base = buf;
4648 rdata->iov[0].iov_len = total_len;
4650 shdr = (struct smb2_hdr *)buf;
4652 if (rdata->credits.value > 0) {
4653 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->subreq.len,
4654 SMB2_MAX_BUFFER_SIZE));
4655 credit_request = le16_to_cpu(shdr->CreditCharge) + 8;
4656 if (server->credits >= server->max_credits)
4657 shdr->CreditRequest = cpu_to_le16(0);
4659 shdr->CreditRequest = cpu_to_le16(
4660 min_t(int, server->max_credits -
4661 server->credits, credit_request));
4663 rc = adjust_credits(server, rdata, cifs_trace_rw_credits_call_readv_adjust);
4665 goto async_readv_out;
4667 flags |= CIFS_HAS_CREDITS;
4670 rc = cifs_call_async(server, &rqst,
4671 cifs_readv_receive, smb2_readv_callback,
4672 smb3_handle_read_data, rdata, flags,
4675 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
4676 trace_smb3_read_err(rdata->rreq->debug_id,
4677 rdata->subreq.debug_index,
4678 rdata->xid, io_parms.persistent_fid,
4680 io_parms.tcon->ses->Suid,
4681 io_parms.offset, io_parms.length, rc);
4685 cifs_small_buf_release(buf);
4690 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
4691 unsigned int *nbytes, char **buf, int *buf_type)
4693 struct smb_rqst rqst;
4694 int resp_buftype, rc;
4695 struct smb2_read_req *req = NULL;
4696 struct smb2_read_rsp *rsp = NULL;
4698 struct kvec rsp_iov;
4699 unsigned int total_len;
4700 int flags = CIFS_LOG_ERROR;
4701 struct cifs_ses *ses = io_parms->tcon->ses;
4703 if (!io_parms->server)
4704 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4707 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
4711 if (smb3_encryption_required(io_parms->tcon))
4712 flags |= CIFS_TRANSFORM_REQ;
4714 iov[0].iov_base = (char *)req;
4715 iov[0].iov_len = total_len;
4717 memset(&rqst, 0, sizeof(struct smb_rqst));
4721 rc = cifs_send_recv(xid, ses, io_parms->server,
4722 &rqst, &resp_buftype, flags, &rsp_iov);
4723 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
4726 if (rc != -ENODATA) {
4727 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4728 cifs_dbg(VFS, "Send error in read = %d\n", rc);
4729 trace_smb3_read_err(0, 0, xid,
4730 req->PersistentFileId,
4731 io_parms->tcon->tid, ses->Suid,
4732 io_parms->offset, io_parms->length,
4735 trace_smb3_read_done(0, 0, xid,
4736 req->PersistentFileId, io_parms->tcon->tid,
4737 ses->Suid, io_parms->offset, 0);
4738 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4739 cifs_small_buf_release(req);
4740 return rc == -ENODATA ? 0 : rc;
4742 trace_smb3_read_done(0, 0, xid,
4743 req->PersistentFileId,
4744 io_parms->tcon->tid, ses->Suid,
4745 io_parms->offset, io_parms->length);
4747 cifs_small_buf_release(req);
4749 *nbytes = le32_to_cpu(rsp->DataLength);
4750 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4751 (*nbytes > io_parms->length)) {
4752 cifs_dbg(FYI, "bad length %d for count %d\n",
4753 *nbytes, io_parms->length);
4759 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
4760 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4761 } else if (resp_buftype != CIFS_NO_BUFFER) {
4762 *buf = rsp_iov.iov_base;
4763 if (resp_buftype == CIFS_SMALL_BUFFER)
4764 *buf_type = CIFS_SMALL_BUFFER;
4765 else if (resp_buftype == CIFS_LARGE_BUFFER)
4766 *buf_type = CIFS_LARGE_BUFFER;
4772 * Check the mid_state and signature on received buffer (if any), and queue the
4773 * workqueue completion task.
4776 smb2_writev_callback(struct mid_q_entry *mid)
4778 struct cifs_io_subrequest *wdata = mid->callback_data;
4779 struct cifs_tcon *tcon = tlink_tcon(wdata->req->cfile->tlink);
4780 struct TCP_Server_Info *server = wdata->server;
4781 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
4782 struct cifs_credits credits = {
4785 .rreq_debug_id = wdata->rreq->debug_id,
4786 .rreq_debug_index = wdata->subreq.debug_index,
4788 unsigned int rreq_debug_id = wdata->rreq->debug_id;
4789 unsigned int subreq_debug_index = wdata->subreq.debug_index;
4793 WARN_ONCE(wdata->server != mid->server,
4794 "wdata server %p != mid server %p",
4795 wdata->server, mid->server);
4797 switch (mid->mid_state) {
4798 case MID_RESPONSE_RECEIVED:
4799 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4800 credits.instance = server->reconnect_instance;
4801 result = smb2_check_receive(mid, server, 0);
4805 written = le32_to_cpu(rsp->DataLength);
4807 * Mask off high 16 bits when bytes written as returned
4808 * by the server is greater than bytes requested by the
4809 * client. OS/2 servers are known to set incorrect
4812 if (written > wdata->subreq.len)
4815 if (written < wdata->subreq.len)
4816 wdata->result = -ENOSPC;
4818 wdata->subreq.len = written;
4820 case MID_REQUEST_SUBMITTED:
4821 case MID_RETRY_NEEDED:
4824 case MID_RESPONSE_MALFORMED:
4825 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4826 credits.instance = server->reconnect_instance;
4832 #ifdef CONFIG_CIFS_SMB_DIRECT
4834 * If this wdata has a memory registered, the MR can be freed
4835 * The number of MRs available is limited, it's important to recover
4836 * used MR as soon as I/O is finished. Hold MR longer in the later
4837 * I/O process can possibly result in I/O deadlock due to lack of MR
4838 * to send request on I/O retry
4841 smbd_deregister_mr(wdata->mr);
4846 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4847 trace_smb3_write_err(wdata->xid,
4848 wdata->req->cfile->fid.persistent_fid,
4849 tcon->tid, tcon->ses->Suid, wdata->subreq.start,
4850 wdata->subreq.len, wdata->result);
4851 if (wdata->result == -ENOSPC)
4852 pr_warn_once("Out of space writing to %s\n",
4855 trace_smb3_write_done(0 /* no xid */,
4856 wdata->req->cfile->fid.persistent_fid,
4857 tcon->tid, tcon->ses->Suid,
4858 wdata->subreq.start, wdata->subreq.len);
4860 trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, wdata->credits.value,
4861 server->credits, server->in_flight,
4862 0, cifs_trace_rw_credits_write_response_clear);
4863 wdata->credits.value = 0;
4864 cifs_write_subrequest_terminated(wdata, result ?: written, true);
4866 trace_smb3_rw_credits(rreq_debug_id, subreq_debug_index, 0,
4867 server->credits, server->in_flight,
4868 credits.value, cifs_trace_rw_credits_write_response_add);
4869 add_credits(server, &credits, 0);
4872 /* smb2_async_writev - send an async write, and set up mid to handle result */
4874 smb2_async_writev(struct cifs_io_subrequest *wdata)
4876 int rc = -EACCES, flags = 0;
4877 struct smb2_write_req *req = NULL;
4878 struct smb2_hdr *shdr;
4879 struct cifs_tcon *tcon = tlink_tcon(wdata->req->cfile->tlink);
4880 struct TCP_Server_Info *server = wdata->server;
4882 struct smb_rqst rqst = { };
4883 unsigned int total_len, xid = wdata->xid;
4884 struct cifs_io_parms _io_parms;
4885 struct cifs_io_parms *io_parms = NULL;
4889 * in future we may get cifs_io_parms passed in from the caller,
4890 * but for now we construct it here...
4892 _io_parms = (struct cifs_io_parms) {
4895 .offset = wdata->subreq.start,
4896 .length = wdata->subreq.len,
4897 .persistent_fid = wdata->req->cfile->fid.persistent_fid,
4898 .volatile_fid = wdata->req->cfile->fid.volatile_fid,
4899 .pid = wdata->req->pid,
4901 io_parms = &_io_parms;
4903 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
4904 (void **) &req, &total_len);
4908 if (smb3_encryption_required(tcon))
4909 flags |= CIFS_TRANSFORM_REQ;
4911 shdr = (struct smb2_hdr *)req;
4912 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4914 req->PersistentFileId = io_parms->persistent_fid;
4915 req->VolatileFileId = io_parms->volatile_fid;
4916 req->WriteChannelInfoOffset = 0;
4917 req->WriteChannelInfoLength = 0;
4918 req->Channel = SMB2_CHANNEL_NONE;
4919 req->Offset = cpu_to_le64(io_parms->offset);
4920 req->DataOffset = cpu_to_le16(
4921 offsetof(struct smb2_write_req, Buffer));
4922 req->RemainingBytes = 0;
4924 trace_smb3_write_enter(wdata->xid,
4925 io_parms->persistent_fid,
4926 io_parms->tcon->tid,
4927 io_parms->tcon->ses->Suid,
4931 #ifdef CONFIG_CIFS_SMB_DIRECT
4933 * If we want to do a server RDMA read, fill in and append
4934 * smbd_buffer_descriptor_v1 to the end of write request
4936 if (smb3_use_rdma_offload(io_parms)) {
4937 struct smbd_buffer_descriptor_v1 *v1;
4938 size_t data_size = iov_iter_count(&wdata->subreq.io_iter);
4939 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4941 wdata->mr = smbd_register_mr(server->smbd_conn, &wdata->subreq.io_iter,
4942 false, need_invalidate);
4945 goto async_writev_out;
4948 req->DataOffset = 0;
4949 req->RemainingBytes = cpu_to_le32(data_size);
4950 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4951 if (need_invalidate)
4952 req->Channel = SMB2_CHANNEL_RDMA_V1;
4953 req->WriteChannelInfoOffset =
4954 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4955 req->WriteChannelInfoLength =
4956 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4957 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4958 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4959 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4960 v1->length = cpu_to_le32(wdata->mr->mr->length);
4963 iov[0].iov_len = total_len - 1;
4964 iov[0].iov_base = (char *)req;
4968 rqst.rq_iter = wdata->subreq.io_iter;
4969 rqst.rq_iter_size = iov_iter_count(&rqst.rq_iter);
4970 if (test_bit(NETFS_SREQ_RETRYING, &wdata->subreq.flags))
4971 smb2_set_replay(server, &rqst);
4972 #ifdef CONFIG_CIFS_SMB_DIRECT
4974 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4976 cifs_dbg(FYI, "async write at %llu %u bytes iter=%zx\n",
4977 io_parms->offset, io_parms->length, iov_iter_count(&rqst.rq_iter));
4979 #ifdef CONFIG_CIFS_SMB_DIRECT
4980 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4982 req->Length = cpu_to_le32(io_parms->length);
4984 req->Length = cpu_to_le32(io_parms->length);
4987 if (wdata->credits.value > 0) {
4988 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->subreq.len,
4989 SMB2_MAX_BUFFER_SIZE));
4990 credit_request = le16_to_cpu(shdr->CreditCharge) + 8;
4991 if (server->credits >= server->max_credits)
4992 shdr->CreditRequest = cpu_to_le16(0);
4994 shdr->CreditRequest = cpu_to_le16(
4995 min_t(int, server->max_credits -
4996 server->credits, credit_request));
4998 rc = adjust_credits(server, wdata, cifs_trace_rw_credits_call_writev_adjust);
5000 goto async_writev_out;
5002 flags |= CIFS_HAS_CREDITS;
5005 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
5006 wdata, flags, &wdata->credits);
5007 /* Can't touch wdata if rc == 0 */
5009 trace_smb3_write_err(xid,
5010 io_parms->persistent_fid,
5011 io_parms->tcon->tid,
5012 io_parms->tcon->ses->Suid,
5016 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
5020 cifs_small_buf_release(req);
5023 trace_smb3_rw_credits(wdata->rreq->debug_id,
5024 wdata->subreq.debug_index,
5025 wdata->credits.value,
5026 server->credits, server->in_flight,
5027 -(int)wdata->credits.value,
5028 cifs_trace_rw_credits_write_response_clear);
5029 add_credits_and_wake_if(wdata->server, &wdata->credits, 0);
5030 cifs_write_subrequest_terminated(wdata, rc, true);
5035 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
5036 * The length field from io_parms must be at least 1 and indicates a number of
5037 * elements with data to write that begins with position 1 in iov array. All
5038 * data length is specified by count.
5041 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
5042 unsigned int *nbytes, struct kvec *iov, int n_vec)
5044 struct smb_rqst rqst;
5046 struct smb2_write_req *req = NULL;
5047 struct smb2_write_rsp *rsp = NULL;
5049 struct kvec rsp_iov;
5051 unsigned int total_len;
5052 struct TCP_Server_Info *server;
5053 int retries = 0, cur_sleep = 1;
5056 /* reinitialize for possible replay */
5059 if (!io_parms->server)
5060 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
5061 server = io_parms->server;
5063 return -ECONNABORTED;
5068 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
5069 (void **) &req, &total_len);
5073 if (smb3_encryption_required(io_parms->tcon))
5074 flags |= CIFS_TRANSFORM_REQ;
5076 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
5078 req->PersistentFileId = io_parms->persistent_fid;
5079 req->VolatileFileId = io_parms->volatile_fid;
5080 req->WriteChannelInfoOffset = 0;
5081 req->WriteChannelInfoLength = 0;
5083 req->Length = cpu_to_le32(io_parms->length);
5084 req->Offset = cpu_to_le64(io_parms->offset);
5085 req->DataOffset = cpu_to_le16(
5086 offsetof(struct smb2_write_req, Buffer));
5087 req->RemainingBytes = 0;
5089 trace_smb3_write_enter(xid, io_parms->persistent_fid,
5090 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
5091 io_parms->offset, io_parms->length);
5093 iov[0].iov_base = (char *)req;
5095 iov[0].iov_len = total_len - 1;
5097 memset(&rqst, 0, sizeof(struct smb_rqst));
5099 rqst.rq_nvec = n_vec + 1;
5102 smb2_set_replay(server, &rqst);
5104 rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
5106 &resp_buftype, flags, &rsp_iov);
5107 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
5110 trace_smb3_write_err(xid,
5111 req->PersistentFileId,
5112 io_parms->tcon->tid,
5113 io_parms->tcon->ses->Suid,
5114 io_parms->offset, io_parms->length, rc);
5115 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
5116 cifs_dbg(VFS, "Send error in write = %d\n", rc);
5118 *nbytes = le32_to_cpu(rsp->DataLength);
5119 trace_smb3_write_done(xid,
5120 req->PersistentFileId,
5121 io_parms->tcon->tid,
5122 io_parms->tcon->ses->Suid,
5123 io_parms->offset, *nbytes);
5126 cifs_small_buf_release(req);
5127 free_rsp_buf(resp_buftype, rsp);
5129 if (is_replayable_error(rc) &&
5130 smb2_should_replay(io_parms->tcon, &retries, &cur_sleep))
5136 int posix_info_sid_size(const void *beg, const void *end)
5144 subauth = *(u8 *)(beg+1);
5145 if (subauth < 1 || subauth > 15)
5148 total = 1 + 1 + 6 + 4*subauth;
5149 if (beg + total > end)
5155 int posix_info_parse(const void *beg, const void *end,
5156 struct smb2_posix_info_parsed *out)
5160 int owner_len, group_len;
5162 const void *owner_sid;
5163 const void *group_sid;
5166 /* if no end bound given, assume payload to be correct */
5168 const struct smb2_posix_info *p = beg;
5170 end = beg + le32_to_cpu(p->NextEntryOffset);
5171 /* last element will have a 0 offset, pick a sensible bound */
5176 /* check base buf */
5177 if (beg + sizeof(struct smb2_posix_info) > end)
5179 total_len = sizeof(struct smb2_posix_info);
5181 /* check owner sid */
5182 owner_sid = beg + total_len;
5183 owner_len = posix_info_sid_size(owner_sid, end);
5186 total_len += owner_len;
5188 /* check group sid */
5189 group_sid = beg + total_len;
5190 group_len = posix_info_sid_size(group_sid, end);
5193 total_len += group_len;
5195 /* check name len */
5196 if (beg + total_len + 4 > end)
5198 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
5199 if (name_len < 1 || name_len > 0xFFFF)
5204 name = beg + total_len;
5205 if (name + name_len > end)
5207 total_len += name_len;
5211 out->size = total_len;
5212 out->name_len = name_len;
5214 memcpy(&out->owner, owner_sid, owner_len);
5215 memcpy(&out->group, group_sid, group_len);
5220 static int posix_info_extra_size(const void *beg, const void *end)
5222 int len = posix_info_parse(beg, end, NULL);
5226 return len - sizeof(struct smb2_posix_info);
5230 num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
5234 unsigned int entrycount = 0;
5235 unsigned int next_offset = 0;
5237 FILE_DIRECTORY_INFO *dir_info;
5239 if (bufstart == NULL)
5242 entryptr = bufstart;
5245 if (entryptr + next_offset < entryptr ||
5246 entryptr + next_offset > end_of_buf ||
5247 entryptr + next_offset + size > end_of_buf) {
5248 cifs_dbg(VFS, "malformed search entry would overflow\n");
5252 entryptr = entryptr + next_offset;
5253 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
5255 if (infotype == SMB_FIND_FILE_POSIX_INFO)
5256 len = posix_info_extra_size(entryptr, end_of_buf);
5258 len = le32_to_cpu(dir_info->FileNameLength);
5261 entryptr + len < entryptr ||
5262 entryptr + len > end_of_buf ||
5263 entryptr + len + size > end_of_buf) {
5264 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
5269 *lastentry = entryptr;
5272 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
5283 int SMB2_query_directory_init(const unsigned int xid,
5284 struct cifs_tcon *tcon,
5285 struct TCP_Server_Info *server,
5286 struct smb_rqst *rqst,
5287 u64 persistent_fid, u64 volatile_fid,
5288 int index, int info_level)
5290 struct smb2_query_directory_req *req;
5291 unsigned char *bufptr;
5292 __le16 asteriks = cpu_to_le16('*');
5293 unsigned int output_size = CIFSMaxBufSize -
5294 MAX_SMB2_CREATE_RESPONSE_SIZE -
5295 MAX_SMB2_CLOSE_RESPONSE_SIZE;
5296 unsigned int total_len;
5297 struct kvec *iov = rqst->rq_iov;
5300 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
5301 (void **) &req, &total_len);
5305 switch (info_level) {
5306 case SMB_FIND_FILE_DIRECTORY_INFO:
5307 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
5309 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
5310 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
5312 case SMB_FIND_FILE_POSIX_INFO:
5313 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
5315 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
5316 req->FileInformationClass = FILE_FULL_DIRECTORY_INFORMATION;
5319 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
5324 req->FileIndex = cpu_to_le32(index);
5325 req->PersistentFileId = persistent_fid;
5326 req->VolatileFileId = volatile_fid;
5329 bufptr = req->Buffer;
5330 memcpy(bufptr, &asteriks, len);
5332 req->FileNameOffset =
5333 cpu_to_le16(sizeof(struct smb2_query_directory_req));
5334 req->FileNameLength = cpu_to_le16(len);
5336 * BB could be 30 bytes or so longer if we used SMB2 specific
5337 * buffer lengths, but this is safe and close enough.
5339 output_size = min_t(unsigned int, output_size, server->maxBuf);
5340 output_size = min_t(unsigned int, output_size, 2 << 15);
5341 req->OutputBufferLength = cpu_to_le32(output_size);
5343 iov[0].iov_base = (char *)req;
5345 iov[0].iov_len = total_len - 1;
5347 iov[1].iov_base = (char *)(req->Buffer);
5348 iov[1].iov_len = len;
5350 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
5351 tcon->ses->Suid, index, output_size);
5356 void SMB2_query_directory_free(struct smb_rqst *rqst)
5358 if (rqst && rqst->rq_iov) {
5359 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
5364 smb2_parse_query_directory(struct cifs_tcon *tcon,
5365 struct kvec *rsp_iov,
5367 struct cifs_search_info *srch_inf)
5369 struct smb2_query_directory_rsp *rsp;
5370 size_t info_buf_size;
5374 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
5376 switch (srch_inf->info_level) {
5377 case SMB_FIND_FILE_DIRECTORY_INFO:
5378 info_buf_size = sizeof(FILE_DIRECTORY_INFO);
5380 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
5381 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO);
5383 case SMB_FIND_FILE_POSIX_INFO:
5384 /* note that posix payload are variable size */
5385 info_buf_size = sizeof(struct smb2_posix_info);
5387 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
5388 info_buf_size = sizeof(FILE_FULL_DIRECTORY_INFO);
5391 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
5392 srch_inf->info_level);
5396 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5397 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
5400 cifs_tcon_dbg(VFS, "bad info payload");
5404 srch_inf->unicode = true;
5406 if (srch_inf->ntwrk_buf_start) {
5407 if (srch_inf->smallBuf)
5408 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
5410 cifs_buf_release(srch_inf->ntwrk_buf_start);
5412 srch_inf->ntwrk_buf_start = (char *)rsp;
5413 srch_inf->srch_entries_start = srch_inf->last_entry =
5414 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
5415 end_of_smb = rsp_iov->iov_len + (char *)rsp;
5417 srch_inf->entries_in_buffer = num_entries(
5418 srch_inf->info_level,
5419 srch_inf->srch_entries_start,
5421 &srch_inf->last_entry,
5424 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
5425 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
5426 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
5427 srch_inf->srch_entries_start, srch_inf->last_entry);
5428 if (resp_buftype == CIFS_LARGE_BUFFER)
5429 srch_inf->smallBuf = false;
5430 else if (resp_buftype == CIFS_SMALL_BUFFER)
5431 srch_inf->smallBuf = true;
5433 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
5439 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
5440 u64 persistent_fid, u64 volatile_fid, int index,
5441 struct cifs_search_info *srch_inf)
5443 struct smb_rqst rqst;
5444 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
5445 struct smb2_query_directory_rsp *rsp = NULL;
5446 int resp_buftype = CIFS_NO_BUFFER;
5447 struct kvec rsp_iov;
5449 struct cifs_ses *ses = tcon->ses;
5450 struct TCP_Server_Info *server;
5452 int retries = 0, cur_sleep = 1;
5455 /* reinitialize for possible replay */
5457 server = cifs_pick_channel(ses);
5459 if (!ses || !(ses->server))
5462 if (smb3_encryption_required(tcon))
5463 flags |= CIFS_TRANSFORM_REQ;
5465 memset(&rqst, 0, sizeof(struct smb_rqst));
5466 memset(&iov, 0, sizeof(iov));
5468 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
5470 rc = SMB2_query_directory_init(xid, tcon, server,
5471 &rqst, persistent_fid,
5472 volatile_fid, index,
5473 srch_inf->info_level);
5478 smb2_set_replay(server, &rqst);
5480 rc = cifs_send_recv(xid, ses, server,
5481 &rqst, &resp_buftype, flags, &rsp_iov);
5482 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
5485 if (rc == -ENODATA &&
5486 rsp->hdr.Status == STATUS_NO_MORE_FILES) {
5487 trace_smb3_query_dir_done(xid, persistent_fid,
5488 tcon->tid, tcon->ses->Suid, index, 0);
5489 srch_inf->endOfSearch = true;
5492 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5493 tcon->ses->Suid, index, 0, rc);
5494 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
5499 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
5502 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5503 tcon->ses->Suid, index, 0, rc);
5506 resp_buftype = CIFS_NO_BUFFER;
5508 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
5509 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
5512 SMB2_query_directory_free(&rqst);
5513 free_rsp_buf(resp_buftype, rsp);
5515 if (is_replayable_error(rc) &&
5516 smb2_should_replay(tcon, &retries, &cur_sleep))
5523 SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
5524 struct smb_rqst *rqst,
5525 u64 persistent_fid, u64 volatile_fid, u32 pid,
5526 u8 info_class, u8 info_type, u32 additional_info,
5527 void **data, unsigned int *size)
5529 struct smb2_set_info_req *req;
5530 struct kvec *iov = rqst->rq_iov;
5531 unsigned int i, total_len;
5534 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
5535 (void **) &req, &total_len);
5539 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
5540 req->InfoType = info_type;
5541 req->FileInfoClass = info_class;
5542 req->PersistentFileId = persistent_fid;
5543 req->VolatileFileId = volatile_fid;
5544 req->AdditionalInformation = cpu_to_le32(additional_info);
5546 req->BufferOffset = cpu_to_le16(sizeof(struct smb2_set_info_req));
5547 req->BufferLength = cpu_to_le32(*size);
5549 memcpy(req->Buffer, *data, *size);
5552 iov[0].iov_base = (char *)req;
5554 iov[0].iov_len = total_len - 1;
5556 for (i = 1; i < rqst->rq_nvec; i++) {
5557 le32_add_cpu(&req->BufferLength, size[i]);
5558 iov[i].iov_base = (char *)data[i];
5559 iov[i].iov_len = size[i];
5566 SMB2_set_info_free(struct smb_rqst *rqst)
5568 if (rqst && rqst->rq_iov)
5569 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
5573 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
5574 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
5575 u8 info_type, u32 additional_info, unsigned int num,
5576 void **data, unsigned int *size)
5578 struct smb_rqst rqst;
5579 struct smb2_set_info_rsp *rsp = NULL;
5581 struct kvec rsp_iov;
5584 struct cifs_ses *ses = tcon->ses;
5585 struct TCP_Server_Info *server;
5587 int retries = 0, cur_sleep = 1;
5590 /* reinitialize for possible replay */
5592 server = cifs_pick_channel(ses);
5594 if (!ses || !server)
5600 if (smb3_encryption_required(tcon))
5601 flags |= CIFS_TRANSFORM_REQ;
5603 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
5607 memset(&rqst, 0, sizeof(struct smb_rqst));
5611 rc = SMB2_set_info_init(tcon, server,
5612 &rqst, persistent_fid, volatile_fid, pid,
5613 info_class, info_type, additional_info,
5621 smb2_set_replay(server, &rqst);
5623 rc = cifs_send_recv(xid, ses, server,
5624 &rqst, &resp_buftype, flags,
5626 SMB2_set_info_free(&rqst);
5627 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
5630 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
5631 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
5632 ses->Suid, info_class, (__u32)info_type, rc);
5635 free_rsp_buf(resp_buftype, rsp);
5638 if (is_replayable_error(rc) &&
5639 smb2_should_replay(tcon, &retries, &cur_sleep))
5646 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
5647 u64 volatile_fid, u32 pid, loff_t new_eof)
5649 struct smb2_file_eof_info info;
5653 info.EndOfFile = cpu_to_le64(new_eof);
5656 size = sizeof(struct smb2_file_eof_info);
5658 trace_smb3_set_eof(xid, persistent_fid, tcon->tid, tcon->ses->Suid, new_eof);
5660 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5661 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
5662 0, 1, &data, &size);
5666 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
5667 u64 persistent_fid, u64 volatile_fid,
5668 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
5670 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5671 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
5672 1, (void **)&pnntsd, &pacllen);
5676 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
5677 u64 persistent_fid, u64 volatile_fid,
5678 struct smb2_file_full_ea_info *buf, int len)
5680 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5681 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
5682 0, 1, (void **)&buf, &len);
5686 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
5687 const u64 persistent_fid, const u64 volatile_fid,
5690 struct smb_rqst rqst;
5692 struct smb2_oplock_break *req = NULL;
5693 struct cifs_ses *ses = tcon->ses;
5694 struct TCP_Server_Info *server;
5695 int flags = CIFS_OBREAK_OP;
5696 unsigned int total_len;
5698 struct kvec rsp_iov;
5700 int retries = 0, cur_sleep = 1;
5703 /* reinitialize for possible replay */
5704 flags = CIFS_OBREAK_OP;
5705 server = cifs_pick_channel(ses);
5707 cifs_dbg(FYI, "SMB2_oplock_break\n");
5708 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5709 (void **) &req, &total_len);
5713 if (smb3_encryption_required(tcon))
5714 flags |= CIFS_TRANSFORM_REQ;
5716 req->VolatileFid = volatile_fid;
5717 req->PersistentFid = persistent_fid;
5718 req->OplockLevel = oplock_level;
5719 req->hdr.CreditRequest = cpu_to_le16(1);
5721 flags |= CIFS_NO_RSP_BUF;
5723 iov[0].iov_base = (char *)req;
5724 iov[0].iov_len = total_len;
5726 memset(&rqst, 0, sizeof(struct smb_rqst));
5731 smb2_set_replay(server, &rqst);
5733 rc = cifs_send_recv(xid, ses, server,
5734 &rqst, &resp_buf_type, flags, &rsp_iov);
5735 cifs_small_buf_release(req);
5737 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5738 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
5741 if (is_replayable_error(rc) &&
5742 smb2_should_replay(tcon, &retries, &cur_sleep))
5749 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
5750 struct kstatfs *kst)
5752 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
5753 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
5754 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
5755 kst->f_bfree = kst->f_bavail =
5756 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
5761 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
5762 struct kstatfs *kst)
5764 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
5765 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
5766 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
5767 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
5768 kst->f_bavail = kst->f_bfree;
5770 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
5771 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5772 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
5773 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5774 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
5780 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
5781 struct TCP_Server_Info *server,
5782 int level, int outbuf_len, u64 persistent_fid,
5786 struct smb2_query_info_req *req;
5787 unsigned int total_len;
5789 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
5791 if ((tcon->ses == NULL) || server == NULL)
5794 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
5795 (void **) &req, &total_len);
5799 req->InfoType = SMB2_O_INFO_FILESYSTEM;
5800 req->FileInfoClass = level;
5801 req->PersistentFileId = persistent_fid;
5802 req->VolatileFileId = volatile_fid;
5804 req->InputBufferOffset =
5805 cpu_to_le16(sizeof(struct smb2_query_info_req));
5806 req->OutputBufferLength = cpu_to_le32(
5807 outbuf_len + sizeof(struct smb2_query_info_rsp));
5809 iov->iov_base = (char *)req;
5810 iov->iov_len = total_len;
5814 static inline void free_qfs_info_req(struct kvec *iov)
5816 cifs_buf_release(iov->iov_base);
5820 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
5821 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5823 struct smb_rqst rqst;
5824 struct smb2_query_info_rsp *rsp = NULL;
5826 struct kvec rsp_iov;
5829 struct cifs_ses *ses = tcon->ses;
5830 struct TCP_Server_Info *server;
5831 FILE_SYSTEM_POSIX_INFO *info = NULL;
5833 int retries = 0, cur_sleep = 1;
5836 /* reinitialize for possible replay */
5838 server = cifs_pick_channel(ses);
5840 rc = build_qfs_info_req(&iov, tcon, server,
5841 FS_POSIX_INFORMATION,
5842 sizeof(FILE_SYSTEM_POSIX_INFO),
5843 persistent_fid, volatile_fid);
5847 if (smb3_encryption_required(tcon))
5848 flags |= CIFS_TRANSFORM_REQ;
5850 memset(&rqst, 0, sizeof(struct smb_rqst));
5855 smb2_set_replay(server, &rqst);
5857 rc = cifs_send_recv(xid, ses, server,
5858 &rqst, &resp_buftype, flags, &rsp_iov);
5859 free_qfs_info_req(&iov);
5861 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5862 goto posix_qfsinf_exit;
5864 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5866 info = (FILE_SYSTEM_POSIX_INFO *)(
5867 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5868 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5869 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5870 sizeof(FILE_SYSTEM_POSIX_INFO));
5872 copy_posix_fs_info_to_kstatfs(info, fsdata);
5875 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5877 if (is_replayable_error(rc) &&
5878 smb2_should_replay(tcon, &retries, &cur_sleep))
5885 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5886 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5888 struct smb_rqst rqst;
5889 struct smb2_query_info_rsp *rsp = NULL;
5891 struct kvec rsp_iov;
5894 struct cifs_ses *ses = tcon->ses;
5895 struct TCP_Server_Info *server;
5896 struct smb2_fs_full_size_info *info = NULL;
5898 int retries = 0, cur_sleep = 1;
5901 /* reinitialize for possible replay */
5903 server = cifs_pick_channel(ses);
5905 rc = build_qfs_info_req(&iov, tcon, server,
5906 FS_FULL_SIZE_INFORMATION,
5907 sizeof(struct smb2_fs_full_size_info),
5908 persistent_fid, volatile_fid);
5912 if (smb3_encryption_required(tcon))
5913 flags |= CIFS_TRANSFORM_REQ;
5915 memset(&rqst, 0, sizeof(struct smb_rqst));
5920 smb2_set_replay(server, &rqst);
5922 rc = cifs_send_recv(xid, ses, server,
5923 &rqst, &resp_buftype, flags, &rsp_iov);
5924 free_qfs_info_req(&iov);
5926 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5929 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5931 info = (struct smb2_fs_full_size_info *)(
5932 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5933 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5934 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5935 sizeof(struct smb2_fs_full_size_info));
5937 smb2_copy_fs_info_to_kstatfs(info, fsdata);
5940 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5942 if (is_replayable_error(rc) &&
5943 smb2_should_replay(tcon, &retries, &cur_sleep))
5950 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
5951 u64 persistent_fid, u64 volatile_fid, int level)
5953 struct smb_rqst rqst;
5954 struct smb2_query_info_rsp *rsp = NULL;
5956 struct kvec rsp_iov;
5958 int resp_buftype, max_len, min_len;
5959 struct cifs_ses *ses = tcon->ses;
5960 struct TCP_Server_Info *server;
5961 unsigned int rsp_len, offset;
5963 int retries = 0, cur_sleep = 1;
5966 /* reinitialize for possible replay */
5968 server = cifs_pick_channel(ses);
5970 if (level == FS_DEVICE_INFORMATION) {
5971 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5972 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5973 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5974 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5975 min_len = MIN_FS_ATTR_INFO_SIZE;
5976 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5977 max_len = sizeof(struct smb3_fs_ss_info);
5978 min_len = sizeof(struct smb3_fs_ss_info);
5979 } else if (level == FS_VOLUME_INFORMATION) {
5980 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5981 min_len = sizeof(struct smb3_fs_vol_info);
5983 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
5987 rc = build_qfs_info_req(&iov, tcon, server,
5989 persistent_fid, volatile_fid);
5993 if (smb3_encryption_required(tcon))
5994 flags |= CIFS_TRANSFORM_REQ;
5996 memset(&rqst, 0, sizeof(struct smb_rqst));
6001 smb2_set_replay(server, &rqst);
6003 rc = cifs_send_recv(xid, ses, server,
6004 &rqst, &resp_buftype, flags, &rsp_iov);
6005 free_qfs_info_req(&iov);
6007 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
6010 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
6012 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
6013 offset = le16_to_cpu(rsp->OutputBufferOffset);
6014 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
6018 if (level == FS_ATTRIBUTE_INFORMATION)
6019 memcpy(&tcon->fsAttrInfo, offset
6020 + (char *)rsp, min_t(unsigned int,
6022 else if (level == FS_DEVICE_INFORMATION)
6023 memcpy(&tcon->fsDevInfo, offset
6024 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
6025 else if (level == FS_SECTOR_SIZE_INFORMATION) {
6026 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
6027 (offset + (char *)rsp);
6028 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
6029 tcon->perf_sector_size =
6030 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
6031 } else if (level == FS_VOLUME_INFORMATION) {
6032 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
6033 (offset + (char *)rsp);
6034 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
6035 tcon->vol_create_time = vol_info->VolumeCreationTime;
6039 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
6041 if (is_replayable_error(rc) &&
6042 smb2_should_replay(tcon, &retries, &cur_sleep))
6049 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
6050 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
6051 const __u32 num_lock, struct smb2_lock_element *buf)
6053 struct smb_rqst rqst;
6055 struct smb2_lock_req *req = NULL;
6057 struct kvec rsp_iov;
6060 int flags = CIFS_NO_RSP_BUF;
6061 unsigned int total_len;
6062 struct TCP_Server_Info *server;
6063 int retries = 0, cur_sleep = 1;
6066 /* reinitialize for possible replay */
6067 flags = CIFS_NO_RSP_BUF;
6068 server = cifs_pick_channel(tcon->ses);
6070 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
6072 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
6073 (void **) &req, &total_len);
6077 if (smb3_encryption_required(tcon))
6078 flags |= CIFS_TRANSFORM_REQ;
6080 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
6081 req->LockCount = cpu_to_le16(num_lock);
6083 req->PersistentFileId = persist_fid;
6084 req->VolatileFileId = volatile_fid;
6086 count = num_lock * sizeof(struct smb2_lock_element);
6088 iov[0].iov_base = (char *)req;
6089 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
6090 iov[1].iov_base = (char *)buf;
6091 iov[1].iov_len = count;
6093 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
6095 memset(&rqst, 0, sizeof(struct smb_rqst));
6100 smb2_set_replay(server, &rqst);
6102 rc = cifs_send_recv(xid, tcon->ses, server,
6103 &rqst, &resp_buf_type, flags,
6105 cifs_small_buf_release(req);
6107 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
6108 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
6109 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
6110 tcon->ses->Suid, rc);
6113 if (is_replayable_error(rc) &&
6114 smb2_should_replay(tcon, &retries, &cur_sleep))
6121 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
6122 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
6123 const __u64 length, const __u64 offset, const __u32 lock_flags,
6126 struct smb2_lock_element lock;
6128 lock.Offset = cpu_to_le64(offset);
6129 lock.Length = cpu_to_le64(length);
6130 lock.Flags = cpu_to_le32(lock_flags);
6131 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
6132 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
6134 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
6138 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
6139 __u8 *lease_key, const __le32 lease_state)
6141 struct smb_rqst rqst;
6143 struct smb2_lease_ack *req = NULL;
6144 struct cifs_ses *ses = tcon->ses;
6145 int flags = CIFS_OBREAK_OP;
6146 unsigned int total_len;
6148 struct kvec rsp_iov;
6150 __u64 *please_key_high;
6151 __u64 *please_key_low;
6152 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
6154 cifs_dbg(FYI, "SMB2_lease_break\n");
6155 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
6156 (void **) &req, &total_len);
6160 if (smb3_encryption_required(tcon))
6161 flags |= CIFS_TRANSFORM_REQ;
6163 req->hdr.CreditRequest = cpu_to_le16(1);
6164 req->StructureSize = cpu_to_le16(36);
6167 memcpy(req->LeaseKey, lease_key, 16);
6168 req->LeaseState = lease_state;
6170 flags |= CIFS_NO_RSP_BUF;
6172 iov[0].iov_base = (char *)req;
6173 iov[0].iov_len = total_len;
6175 memset(&rqst, 0, sizeof(struct smb_rqst));
6179 rc = cifs_send_recv(xid, ses, server,
6180 &rqst, &resp_buf_type, flags, &rsp_iov);
6181 cifs_small_buf_release(req);
6183 please_key_low = (__u64 *)lease_key;
6184 please_key_high = (__u64 *)(lease_key+8);
6186 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
6187 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
6188 ses->Suid, *please_key_low, *please_key_high, rc);
6189 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
6191 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
6192 ses->Suid, *please_key_low, *please_key_high);