1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2009, 2013
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
9 * Contains the routines for constructing the SMB2 PDUs themselves
13 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
14 /* Note that there are handle based routines which must be */
15 /* treated slightly differently for reconnection purposes since we never */
16 /* want to reuse a stale file handle and only the caller knows the file info */
19 #include <linux/kernel.h>
20 #include <linux/vfs.h>
21 #include <linux/task_io_accounting_ops.h>
22 #include <linux/uaccess.h>
23 #include <linux/uuid.h>
24 #include <linux/pagemap.h>
25 #include <linux/xattr.h>
28 #include "cifsproto.h"
29 #include "smb2proto.h"
30 #include "cifs_unicode.h"
31 #include "cifs_debug.h"
33 #include "smb2status.h"
36 #include "cifs_spnego.h"
37 #include "smbdirect.h"
39 #ifdef CONFIG_CIFS_DFS_UPCALL
40 #include "dfs_cache.h"
42 #include "cached_dir.h"
45 * The following table defines the expected "StructureSize" of SMB2 requests
46 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
48 * Note that commands are defined in smb2pdu.h in le16 but the array below is
49 * indexed by command in host byte order.
51 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
52 /* SMB2_NEGOTIATE */ 36,
53 /* SMB2_SESSION_SETUP */ 25,
55 /* SMB2_TREE_CONNECT */ 9,
56 /* SMB2_TREE_DISCONNECT */ 4,
66 /* SMB2_QUERY_DIRECTORY */ 33,
67 /* SMB2_CHANGE_NOTIFY */ 32,
68 /* SMB2_QUERY_INFO */ 41,
69 /* SMB2_SET_INFO */ 33,
70 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
73 int smb3_encryption_required(const struct cifs_tcon *tcon)
75 if (!tcon || !tcon->ses)
77 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
78 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
81 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
87 smb2_hdr_assemble(struct smb2_hdr *shdr, __le16 smb2_cmd,
88 const struct cifs_tcon *tcon,
89 struct TCP_Server_Info *server)
91 struct smb3_hdr_req *smb3_hdr;
92 shdr->ProtocolId = SMB2_PROTO_NUMBER;
93 shdr->StructureSize = cpu_to_le16(64);
94 shdr->Command = smb2_cmd;
95 if (server->dialect >= SMB30_PROT_ID) {
96 /* After reconnect SMB3 must set ChannelSequence on subsequent reqs */
97 smb3_hdr = (struct smb3_hdr_req *)shdr;
98 /* if primary channel is not set yet, use default channel for chan sequence num */
99 if (CIFS_SERVER_IS_CHAN(server))
100 smb3_hdr->ChannelSequence =
101 cpu_to_le16(server->primary_server->channel_sequence_num);
103 smb3_hdr->ChannelSequence = cpu_to_le16(server->channel_sequence_num);
106 spin_lock(&server->req_lock);
107 /* Request up to 10 credits but don't go over the limit. */
108 if (server->credits >= server->max_credits)
109 shdr->CreditRequest = cpu_to_le16(0);
111 shdr->CreditRequest = cpu_to_le16(
112 min_t(int, server->max_credits -
113 server->credits, 10));
114 spin_unlock(&server->req_lock);
116 shdr->CreditRequest = cpu_to_le16(2);
118 shdr->Id.SyncId.ProcessId = cpu_to_le32((__u16)current->tgid);
123 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
124 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
125 if (server && (server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
126 shdr->CreditCharge = cpu_to_le16(1);
127 /* else CreditCharge MBZ */
129 shdr->Id.SyncId.TreeId = cpu_to_le32(tcon->tid);
130 /* Uid is not converted */
132 shdr->SessionId = cpu_to_le64(tcon->ses->Suid);
135 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
136 * to pass the path on the Open SMB prefixed by \\server\share.
137 * Not sure when we would need to do the augmented path (if ever) and
138 * setting this flag breaks the SMB2 open operation since it is
139 * illegal to send an empty path name (without \\server\share prefix)
140 * when the DFS flag is set in the SMB open header. We could
141 * consider setting the flag on all operations other than open
142 * but it is safer to net set it for now.
144 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
145 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
147 if (server && server->sign && !smb3_encryption_required(tcon))
148 shdr->Flags |= SMB2_FLAGS_SIGNED;
154 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
155 struct TCP_Server_Info *server)
158 struct nls_table *nls_codepage = NULL;
159 struct cifs_ses *ses;
162 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
163 * check for tcp and smb session status done differently
164 * for those three - in the calling routine.
170 * Need to also skip SMB2_IOCTL because it is used for checking nested dfs links in
171 * cifs_tree_connect().
173 if (smb2_command == SMB2_TREE_CONNECT || smb2_command == SMB2_IOCTL)
176 spin_lock(&tcon->tc_lock);
177 if (tcon->status == TID_EXITING) {
179 * only tree disconnect allowed when disconnecting ...
181 if (smb2_command != SMB2_TREE_DISCONNECT) {
182 spin_unlock(&tcon->tc_lock);
183 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
188 spin_unlock(&tcon->tc_lock);
193 spin_lock(&ses->ses_lock);
194 if (ses->ses_status == SES_EXITING) {
195 spin_unlock(&ses->ses_lock);
198 spin_unlock(&ses->ses_lock);
199 if (!ses->server || !server)
202 spin_lock(&server->srv_lock);
203 if (server->tcpStatus == CifsNeedReconnect) {
205 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
206 * here since they are implicitly done when session drops.
208 switch (smb2_command) {
210 * BB Should we keep oplock break and add flush to exceptions?
212 case SMB2_TREE_DISCONNECT:
215 case SMB2_OPLOCK_BREAK:
216 spin_unlock(&server->srv_lock);
220 spin_unlock(&server->srv_lock);
223 rc = cifs_wait_for_server_reconnect(server, tcon->retry);
227 spin_lock(&ses->chan_lock);
228 if (!cifs_chan_needs_reconnect(ses, server) && !tcon->need_reconnect) {
229 spin_unlock(&ses->chan_lock);
232 spin_unlock(&ses->chan_lock);
233 cifs_dbg(FYI, "sess reconnect mask: 0x%lx, tcon reconnect: %d",
234 tcon->ses->chans_need_reconnect,
235 tcon->need_reconnect);
237 mutex_lock(&ses->session_mutex);
239 * Recheck after acquire mutex. If another thread is negotiating
240 * and the server never sends an answer the socket will be closed
241 * and tcpStatus set to reconnect.
243 spin_lock(&server->srv_lock);
244 if (server->tcpStatus == CifsNeedReconnect) {
245 spin_unlock(&server->srv_lock);
246 mutex_unlock(&ses->session_mutex);
254 spin_unlock(&server->srv_lock);
256 nls_codepage = load_nls_default();
259 * need to prevent multiple threads trying to simultaneously
260 * reconnect the same SMB session
262 spin_lock(&ses->ses_lock);
263 spin_lock(&ses->chan_lock);
264 if (!cifs_chan_needs_reconnect(ses, server) &&
265 ses->ses_status == SES_GOOD) {
266 spin_unlock(&ses->chan_lock);
267 spin_unlock(&ses->ses_lock);
268 /* this means that we only need to tree connect */
269 if (tcon->need_reconnect)
270 goto skip_sess_setup;
272 mutex_unlock(&ses->session_mutex);
275 spin_unlock(&ses->chan_lock);
276 spin_unlock(&ses->ses_lock);
278 rc = cifs_negotiate_protocol(0, ses, server);
280 rc = cifs_setup_session(0, ses, server, nls_codepage);
281 if ((rc == -EACCES) && !tcon->retry) {
282 mutex_unlock(&ses->session_mutex);
286 mutex_unlock(&ses->session_mutex);
290 mutex_unlock(&ses->session_mutex);
295 if (!tcon->need_reconnect) {
296 mutex_unlock(&ses->session_mutex);
299 cifs_mark_open_files_invalid(tcon);
300 if (tcon->use_persistent)
301 tcon->need_reopen_files = true;
303 rc = cifs_tree_connect(0, tcon, nls_codepage);
304 mutex_unlock(&ses->session_mutex);
306 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
308 /* If sess reconnected but tcon didn't, something strange ... */
309 cifs_dbg(VFS, "reconnect tcon failed rc = %d\n", rc);
313 if (smb2_command != SMB2_INTERNAL_CMD)
314 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
316 atomic_inc(&tconInfoReconnectCount);
319 * Check if handle based operation so we know whether we can continue
320 * or not without returning to caller to reset file handle.
323 * BB Is flush done by server on drop of tcp session? Should we special
324 * case it and skip above?
326 switch (smb2_command) {
332 case SMB2_QUERY_DIRECTORY:
333 case SMB2_CHANGE_NOTIFY:
334 case SMB2_QUERY_INFO:
339 unload_nls(nls_codepage);
344 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon,
345 struct TCP_Server_Info *server,
347 unsigned int *total_len)
349 struct smb2_pdu *spdu = buf;
350 /* lookup word count ie StructureSize from table */
351 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
354 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
355 * largest operations (Create)
359 smb2_hdr_assemble(&spdu->hdr, smb2_command, tcon, server);
360 spdu->StructureSize2 = cpu_to_le16(parmsize);
362 *total_len = parmsize + sizeof(struct smb2_hdr);
366 * Allocate and return pointer to an SMB request hdr, and set basic
367 * SMB information in the SMB header. If the return code is zero, this
368 * function must have filled in request_buf pointer.
370 static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
371 struct TCP_Server_Info *server,
372 void **request_buf, unsigned int *total_len)
374 /* BB eventually switch this to SMB2 specific small buf size */
375 switch (smb2_command) {
377 case SMB2_QUERY_INFO:
378 *request_buf = cifs_buf_get();
381 *request_buf = cifs_small_buf_get();
384 if (*request_buf == NULL) {
385 /* BB should we add a retry in here if not a writepage? */
389 fill_small_buf(smb2_command, tcon, server,
390 (struct smb2_hdr *)(*request_buf),
394 uint16_t com_code = le16_to_cpu(smb2_command);
395 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
396 cifs_stats_inc(&tcon->num_smbs_sent);
402 static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
403 struct TCP_Server_Info *server,
404 void **request_buf, unsigned int *total_len)
408 rc = smb2_reconnect(smb2_command, tcon, server);
412 return __smb2_plain_req_init(smb2_command, tcon, server, request_buf,
416 static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
417 struct TCP_Server_Info *server,
418 void **request_buf, unsigned int *total_len)
420 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
421 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
422 return __smb2_plain_req_init(SMB2_IOCTL, tcon, server,
423 request_buf, total_len);
425 return smb2_plain_req_init(SMB2_IOCTL, tcon, server,
426 request_buf, total_len);
429 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
432 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
434 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
435 pneg_ctxt->DataLength = cpu_to_le16(38);
436 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
437 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
438 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
439 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
443 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
445 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
446 pneg_ctxt->DataLength =
447 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
448 - sizeof(struct smb2_neg_context));
449 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
450 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
451 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
452 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
456 build_signing_ctxt(struct smb2_signing_capabilities *pneg_ctxt)
458 unsigned int ctxt_len = sizeof(struct smb2_signing_capabilities);
459 unsigned short num_algs = 1; /* number of signing algorithms sent */
461 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
463 * Context Data length must be rounded to multiple of 8 for some servers
465 pneg_ctxt->DataLength = cpu_to_le16(ALIGN(sizeof(struct smb2_signing_capabilities) -
466 sizeof(struct smb2_neg_context) +
467 (num_algs * sizeof(u16)), 8));
468 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(num_algs);
469 pneg_ctxt->SigningAlgorithms[0] = cpu_to_le16(SIGNING_ALG_AES_CMAC);
471 ctxt_len += sizeof(__le16) * num_algs;
472 ctxt_len = ALIGN(ctxt_len, 8);
474 /* TBD add SIGNING_ALG_AES_GMAC and/or SIGNING_ALG_HMAC_SHA256 */
478 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
480 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
481 if (require_gcm_256) {
482 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + 1 cipher */
483 pneg_ctxt->CipherCount = cpu_to_le16(1);
484 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES256_GCM;
485 } else if (enable_gcm_256) {
486 pneg_ctxt->DataLength = cpu_to_le16(8); /* Cipher Count + 3 ciphers */
487 pneg_ctxt->CipherCount = cpu_to_le16(3);
488 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
489 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES256_GCM;
490 pneg_ctxt->Ciphers[2] = SMB2_ENCRYPTION_AES128_CCM;
492 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + 2 ciphers */
493 pneg_ctxt->CipherCount = cpu_to_le16(2);
494 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
495 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
500 build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
502 struct nls_table *cp = load_nls_default();
504 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
506 /* copy up to max of first 100 bytes of server name to NetName field */
507 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
508 /* context size is DataLength + minimal smb2_neg_context */
509 return ALIGN(le16_to_cpu(pneg_ctxt->DataLength) + sizeof(struct smb2_neg_context), 8);
513 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
515 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
516 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
517 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
518 pneg_ctxt->Name[0] = 0x93;
519 pneg_ctxt->Name[1] = 0xAD;
520 pneg_ctxt->Name[2] = 0x25;
521 pneg_ctxt->Name[3] = 0x50;
522 pneg_ctxt->Name[4] = 0x9C;
523 pneg_ctxt->Name[5] = 0xB4;
524 pneg_ctxt->Name[6] = 0x11;
525 pneg_ctxt->Name[7] = 0xE7;
526 pneg_ctxt->Name[8] = 0xB4;
527 pneg_ctxt->Name[9] = 0x23;
528 pneg_ctxt->Name[10] = 0x83;
529 pneg_ctxt->Name[11] = 0xDE;
530 pneg_ctxt->Name[12] = 0x96;
531 pneg_ctxt->Name[13] = 0x8B;
532 pneg_ctxt->Name[14] = 0xCD;
533 pneg_ctxt->Name[15] = 0x7C;
537 assemble_neg_contexts(struct smb2_negotiate_req *req,
538 struct TCP_Server_Info *server, unsigned int *total_len)
540 unsigned int ctxt_len, neg_context_count;
541 struct TCP_Server_Info *pserver;
545 if (*total_len > 200) {
546 /* In case length corrupted don't want to overrun smb buffer */
547 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
552 * round up total_len of fixed part of SMB3 negotiate request to 8
553 * byte boundary before adding negotiate contexts
555 *total_len = ALIGN(*total_len, 8);
557 pneg_ctxt = (*total_len) + (char *)req;
558 req->NegotiateContextOffset = cpu_to_le32(*total_len);
560 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
561 ctxt_len = ALIGN(sizeof(struct smb2_preauth_neg_context), 8);
562 *total_len += ctxt_len;
563 pneg_ctxt += ctxt_len;
565 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
566 ctxt_len = ALIGN(sizeof(struct smb2_encryption_neg_context), 8);
567 *total_len += ctxt_len;
568 pneg_ctxt += ctxt_len;
571 * secondary channels don't have the hostname field populated
572 * use the hostname field in the primary channel instead
574 pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
575 cifs_server_lock(pserver);
576 hostname = pserver->hostname;
577 if (hostname && (hostname[0] != 0)) {
578 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
580 *total_len += ctxt_len;
581 pneg_ctxt += ctxt_len;
582 neg_context_count = 3;
584 neg_context_count = 2;
585 cifs_server_unlock(pserver);
587 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
588 *total_len += sizeof(struct smb2_posix_neg_context);
589 pneg_ctxt += sizeof(struct smb2_posix_neg_context);
592 if (server->compress_algorithm) {
593 build_compression_ctxt((struct smb2_compression_capabilities_context *)
595 ctxt_len = ALIGN(sizeof(struct smb2_compression_capabilities_context), 8);
596 *total_len += ctxt_len;
597 pneg_ctxt += ctxt_len;
601 if (enable_negotiate_signing) {
602 ctxt_len = build_signing_ctxt((struct smb2_signing_capabilities *)
604 *total_len += ctxt_len;
605 pneg_ctxt += ctxt_len;
609 /* check for and add transport_capabilities and signing capabilities */
610 req->NegotiateContextCount = cpu_to_le16(neg_context_count);
614 /* If invalid preauth context warn but use what we requested, SHA-512 */
615 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
617 unsigned int len = le16_to_cpu(ctxt->DataLength);
620 * Caller checked that DataLength remains within SMB boundary. We still
621 * need to confirm that one HashAlgorithms member is accounted for.
623 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
624 pr_warn_once("server sent bad preauth context\n");
626 } else if (len < MIN_PREAUTH_CTXT_DATA_LEN + le16_to_cpu(ctxt->SaltLength)) {
627 pr_warn_once("server sent invalid SaltLength\n");
630 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
631 pr_warn_once("Invalid SMB3 hash algorithm count\n");
632 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
633 pr_warn_once("unknown SMB3 hash algorithm\n");
636 static void decode_compress_ctx(struct TCP_Server_Info *server,
637 struct smb2_compression_capabilities_context *ctxt)
639 unsigned int len = le16_to_cpu(ctxt->DataLength);
642 * Caller checked that DataLength remains within SMB boundary. We still
643 * need to confirm that one CompressionAlgorithms member is accounted
647 pr_warn_once("server sent bad compression cntxt\n");
650 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
651 pr_warn_once("Invalid SMB3 compress algorithm count\n");
654 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
655 pr_warn_once("unknown compression algorithm\n");
658 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
661 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
662 struct smb2_encryption_neg_context *ctxt)
664 unsigned int len = le16_to_cpu(ctxt->DataLength);
666 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
668 * Caller checked that DataLength remains within SMB boundary. We still
669 * need to confirm that one Cipher flexible array member is accounted
672 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
673 pr_warn_once("server sent bad crypto ctxt len\n");
677 if (le16_to_cpu(ctxt->CipherCount) != 1) {
678 pr_warn_once("Invalid SMB3.11 cipher count\n");
681 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
682 if (require_gcm_256) {
683 if (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM) {
684 cifs_dbg(VFS, "Server does not support requested encryption type (AES256 GCM)\n");
687 } else if (ctxt->Ciphers[0] == 0) {
689 * e.g. if server only supported AES256_CCM (very unlikely)
690 * or server supported no encryption types or had all disabled.
691 * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case
692 * in which mount requested encryption ("seal") checks later
693 * on during tree connection will return proper rc, but if
694 * seal not requested by client, since server is allowed to
695 * return 0 to indicate no supported cipher, we can't fail here
697 server->cipher_type = 0;
698 server->capabilities &= ~SMB2_GLOBAL_CAP_ENCRYPTION;
699 pr_warn_once("Server does not support requested encryption types\n");
701 } else if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
702 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM) &&
703 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM)) {
704 /* server returned a cipher we didn't ask for */
705 pr_warn_once("Invalid SMB3.11 cipher returned\n");
708 server->cipher_type = ctxt->Ciphers[0];
709 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
713 static void decode_signing_ctx(struct TCP_Server_Info *server,
714 struct smb2_signing_capabilities *pctxt)
716 unsigned int len = le16_to_cpu(pctxt->DataLength);
719 * Caller checked that DataLength remains within SMB boundary. We still
720 * need to confirm that one SigningAlgorithms flexible array member is
723 if ((len < 4) || (len > 16)) {
724 pr_warn_once("server sent bad signing negcontext\n");
727 if (le16_to_cpu(pctxt->SigningAlgorithmCount) != 1) {
728 pr_warn_once("Invalid signing algorithm count\n");
731 if (le16_to_cpu(pctxt->SigningAlgorithms[0]) > 2) {
732 pr_warn_once("unknown signing algorithm\n");
736 server->signing_negotiated = true;
737 server->signing_algorithm = le16_to_cpu(pctxt->SigningAlgorithms[0]);
738 cifs_dbg(FYI, "signing algorithm %d chosen\n",
739 server->signing_algorithm);
743 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
744 struct TCP_Server_Info *server,
745 unsigned int len_of_smb)
747 struct smb2_neg_context *pctx;
748 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
749 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
750 unsigned int len_of_ctxts, i;
753 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
754 if (len_of_smb <= offset) {
755 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
759 len_of_ctxts = len_of_smb - offset;
761 for (i = 0; i < ctxt_cnt; i++) {
763 /* check that offset is not beyond end of SMB */
764 if (len_of_ctxts < sizeof(struct smb2_neg_context))
767 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
768 clen = sizeof(struct smb2_neg_context)
769 + le16_to_cpu(pctx->DataLength);
771 * 2.2.4 SMB2 NEGOTIATE Response
772 * Subsequent negotiate contexts MUST appear at the first 8-byte
773 * aligned offset following the previous negotiate context.
775 if (i + 1 != ctxt_cnt)
776 clen = ALIGN(clen, 8);
777 if (clen > len_of_ctxts)
780 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
781 decode_preauth_context(
782 (struct smb2_preauth_neg_context *)pctx);
783 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
784 rc = decode_encrypt_ctx(server,
785 (struct smb2_encryption_neg_context *)pctx);
786 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
787 decode_compress_ctx(server,
788 (struct smb2_compression_capabilities_context *)pctx);
789 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
790 server->posix_ext_supported = true;
791 else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES)
792 decode_signing_ctx(server,
793 (struct smb2_signing_capabilities *)pctx);
795 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
796 le16_to_cpu(pctx->ContextType));
801 len_of_ctxts -= clen;
806 static struct create_posix *
807 create_posix_buf(umode_t mode)
809 struct create_posix *buf;
811 buf = kzalloc(sizeof(struct create_posix),
816 buf->ccontext.DataOffset =
817 cpu_to_le16(offsetof(struct create_posix, Mode));
818 buf->ccontext.DataLength = cpu_to_le32(4);
819 buf->ccontext.NameOffset =
820 cpu_to_le16(offsetof(struct create_posix, Name));
821 buf->ccontext.NameLength = cpu_to_le16(16);
823 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
834 buf->Name[10] = 0x83;
835 buf->Name[11] = 0xDE;
836 buf->Name[12] = 0x96;
837 buf->Name[13] = 0x8B;
838 buf->Name[14] = 0xCD;
839 buf->Name[15] = 0x7C;
840 buf->Mode = cpu_to_le32(mode);
841 cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
846 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
848 struct smb2_create_req *req = iov[0].iov_base;
849 unsigned int num = *num_iovec;
851 iov[num].iov_base = create_posix_buf(mode);
852 if (mode == ACL_NO_MODE)
853 cifs_dbg(FYI, "Invalid mode\n");
854 if (iov[num].iov_base == NULL)
856 iov[num].iov_len = sizeof(struct create_posix);
857 if (!req->CreateContextsOffset)
858 req->CreateContextsOffset = cpu_to_le32(
859 sizeof(struct smb2_create_req) +
860 iov[num - 1].iov_len);
861 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
862 *num_iovec = num + 1;
869 * SMB2 Worker functions follow:
871 * The general structure of the worker functions is:
872 * 1) Call smb2_init (assembles SMB2 header)
873 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
874 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
875 * 4) Decode SMB2 command specific fields in the fixed length area
876 * 5) Decode variable length data area (if any for this SMB2 command type)
877 * 6) Call free smb buffer
883 SMB2_negotiate(const unsigned int xid,
884 struct cifs_ses *ses,
885 struct TCP_Server_Info *server)
887 struct smb_rqst rqst;
888 struct smb2_negotiate_req *req;
889 struct smb2_negotiate_rsp *rsp;
894 int blob_offset, blob_length;
896 int flags = CIFS_NEG_OP;
897 unsigned int total_len;
899 cifs_dbg(FYI, "Negotiate protocol\n");
902 WARN(1, "%s: server is NULL!\n", __func__);
906 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
907 (void **) &req, &total_len);
911 req->hdr.SessionId = 0;
913 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
914 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
916 if (strcmp(server->vals->version_string,
917 SMB3ANY_VERSION_STRING) == 0) {
918 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
919 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
920 req->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
921 req->DialectCount = cpu_to_le16(3);
923 } else if (strcmp(server->vals->version_string,
924 SMBDEFAULT_VERSION_STRING) == 0) {
925 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
926 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
927 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
928 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
929 req->DialectCount = cpu_to_le16(4);
932 /* otherwise send specific dialect */
933 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
934 req->DialectCount = cpu_to_le16(1);
938 /* only one of SMB2 signing flags may be set in SMB2 request */
940 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
941 else if (global_secflags & CIFSSEC_MAY_SIGN)
942 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
944 req->SecurityMode = 0;
946 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
947 if (ses->chan_max > 1)
948 req->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
950 /* ClientGUID must be zero for SMB2.02 dialect */
951 if (server->vals->protocol_id == SMB20_PROT_ID)
952 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
954 memcpy(req->ClientGUID, server->client_guid,
955 SMB2_CLIENT_GUID_SIZE);
956 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
957 (strcmp(server->vals->version_string,
958 SMB3ANY_VERSION_STRING) == 0) ||
959 (strcmp(server->vals->version_string,
960 SMBDEFAULT_VERSION_STRING) == 0))
961 assemble_neg_contexts(req, server, &total_len);
963 iov[0].iov_base = (char *)req;
964 iov[0].iov_len = total_len;
966 memset(&rqst, 0, sizeof(struct smb_rqst));
970 rc = cifs_send_recv(xid, ses, server,
971 &rqst, &resp_buftype, flags, &rsp_iov);
972 cifs_small_buf_release(req);
973 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
975 * No tcon so can't do
976 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
978 if (rc == -EOPNOTSUPP) {
979 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");
985 if (strcmp(server->vals->version_string,
986 SMB3ANY_VERSION_STRING) == 0) {
987 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
989 "SMB2 dialect returned but not requested\n");
991 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
993 "SMB2.1 dialect returned but not requested\n");
995 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
996 /* ops set to 3.0 by default for default so update */
997 server->ops = &smb311_operations;
998 server->vals = &smb311_values;
1000 } else if (strcmp(server->vals->version_string,
1001 SMBDEFAULT_VERSION_STRING) == 0) {
1002 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
1003 cifs_server_dbg(VFS,
1004 "SMB2 dialect returned but not requested\n");
1006 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
1007 /* ops set to 3.0 by default for default so update */
1008 server->ops = &smb21_operations;
1009 server->vals = &smb21_values;
1010 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1011 server->ops = &smb311_operations;
1012 server->vals = &smb311_values;
1014 } else if (le16_to_cpu(rsp->DialectRevision) !=
1015 server->vals->protocol_id) {
1016 /* if requested single dialect ensure returned dialect matched */
1017 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
1018 le16_to_cpu(rsp->DialectRevision));
1022 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
1024 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
1025 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
1026 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
1027 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
1028 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
1029 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
1030 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
1031 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
1032 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
1033 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
1035 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
1036 le16_to_cpu(rsp->DialectRevision));
1041 server->dialect = le16_to_cpu(rsp->DialectRevision);
1044 * Keep a copy of the hash after negprot. This hash will be
1045 * the starting hash value for all sessions made from this
1048 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
1049 SMB2_PREAUTH_HASH_SIZE);
1051 /* SMB2 only has an extended negflavor */
1052 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
1053 /* set it to the maximum buffer size value we can send with 1 credit */
1054 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
1055 SMB2_MAX_BUFFER_SIZE);
1056 server->max_read = le32_to_cpu(rsp->MaxReadSize);
1057 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
1058 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
1059 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
1060 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
1062 server->capabilities = le32_to_cpu(rsp->Capabilities);
1063 /* Internal types */
1064 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
1067 * SMB3.0 supports only 1 cipher and doesn't have a encryption neg context
1068 * Set the cipher type manually.
1070 if (server->dialect == SMB30_PROT_ID && (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1071 server->cipher_type = SMB2_ENCRYPTION_AES128_CCM;
1073 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
1074 (struct smb2_hdr *)rsp);
1076 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
1078 * ses->sectype = RawNTLMSSP;
1079 * but for time being this is our only auth choice so doesn't matter.
1080 * We just found a server which sets blob length to zero expecting raw.
1082 if (blob_length == 0) {
1083 cifs_dbg(FYI, "missing security blob on negprot\n");
1084 server->sec_ntlmssp = true;
1087 rc = cifs_enable_signing(server, ses->sign);
1091 rc = decode_negTokenInit(security_blob, blob_length, server);
1098 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1099 if (rsp->NegotiateContextCount)
1100 rc = smb311_decode_neg_context(rsp, server,
1103 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
1106 free_rsp_buf(resp_buftype, rsp);
1110 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1113 struct validate_negotiate_info_req *pneg_inbuf;
1114 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
1116 u32 inbuflen; /* max of 4 dialects */
1117 struct TCP_Server_Info *server = tcon->ses->server;
1119 cifs_dbg(FYI, "validate negotiate\n");
1121 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1122 if (server->dialect == SMB311_PROT_ID)
1126 * validation ioctl must be signed, so no point sending this if we
1127 * can not sign it (ie are not known user). Even if signing is not
1128 * required (enabled but not negotiated), in those cases we selectively
1129 * sign just this, the first and only signed request on a connection.
1130 * Having validation of negotiate info helps reduce attack vectors.
1132 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1133 return 0; /* validation requires signing */
1135 if (tcon->ses->user_name == NULL) {
1136 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1137 return 0; /* validation requires signing */
1140 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1141 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1143 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1147 pneg_inbuf->Capabilities =
1148 cpu_to_le32(server->vals->req_capabilities);
1149 if (tcon->ses->chan_max > 1)
1150 pneg_inbuf->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
1152 memcpy(pneg_inbuf->Guid, server->client_guid,
1153 SMB2_CLIENT_GUID_SIZE);
1155 if (tcon->ses->sign)
1156 pneg_inbuf->SecurityMode =
1157 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1158 else if (global_secflags & CIFSSEC_MAY_SIGN)
1159 pneg_inbuf->SecurityMode =
1160 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1162 pneg_inbuf->SecurityMode = 0;
1165 if (strcmp(server->vals->version_string,
1166 SMB3ANY_VERSION_STRING) == 0) {
1167 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1168 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1169 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1170 pneg_inbuf->DialectCount = cpu_to_le16(3);
1171 /* SMB 2.1 not included so subtract one dialect from len */
1172 inbuflen = sizeof(*pneg_inbuf) -
1173 (sizeof(pneg_inbuf->Dialects[0]));
1174 } else if (strcmp(server->vals->version_string,
1175 SMBDEFAULT_VERSION_STRING) == 0) {
1176 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1177 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1178 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1179 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1180 pneg_inbuf->DialectCount = cpu_to_le16(4);
1181 /* structure is big enough for 4 dialects */
1182 inbuflen = sizeof(*pneg_inbuf);
1184 /* otherwise specific dialect was requested */
1185 pneg_inbuf->Dialects[0] =
1186 cpu_to_le16(server->vals->protocol_id);
1187 pneg_inbuf->DialectCount = cpu_to_le16(1);
1188 /* structure is big enough for 4 dialects, sending only 1 */
1189 inbuflen = sizeof(*pneg_inbuf) -
1190 sizeof(pneg_inbuf->Dialects[0]) * 3;
1193 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1194 FSCTL_VALIDATE_NEGOTIATE_INFO,
1195 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1196 (char **)&pneg_rsp, &rsplen);
1197 if (rc == -EOPNOTSUPP) {
1199 * Old Windows versions or Netapp SMB server can return
1200 * not supported error. Client should accept it.
1202 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1204 goto out_free_inbuf;
1205 } else if (rc != 0) {
1206 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1209 goto out_free_inbuf;
1213 if (rsplen != sizeof(*pneg_rsp)) {
1214 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1217 /* relax check since Mac returns max bufsize allowed on ioctl */
1218 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1222 /* check validate negotiate info response matches what we got earlier */
1223 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1226 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1229 /* do not validate server guid because not saved at negprot time yet */
1231 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1232 SMB2_LARGE_FILES) != server->capabilities)
1235 /* validate negotiate successful */
1237 cifs_dbg(FYI, "validate negotiate info successful\n");
1241 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1250 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1252 switch (requested) {
1259 if (server->sec_ntlmssp &&
1260 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1262 if ((server->sec_kerberos || server->sec_mskerberos) &&
1263 (global_secflags & CIFSSEC_MAY_KRB5))
1271 struct SMB2_sess_data {
1273 struct cifs_ses *ses;
1274 struct TCP_Server_Info *server;
1275 struct nls_table *nls_cp;
1276 void (*func)(struct SMB2_sess_data *);
1278 u64 previous_session;
1280 /* we will send the SMB in three pieces:
1281 * a fixed length beginning part, an optional
1282 * SPNEGO blob (which can be zero length), and a
1283 * last part which will include the strings
1284 * and rest of bcc area. This allows us to avoid
1285 * a large buffer 17K allocation
1292 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1295 struct cifs_ses *ses = sess_data->ses;
1296 struct TCP_Server_Info *server = sess_data->server;
1297 struct smb2_sess_setup_req *req;
1298 unsigned int total_len;
1299 bool is_binding = false;
1301 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
1307 spin_lock(&ses->ses_lock);
1308 is_binding = (ses->ses_status == SES_GOOD);
1309 spin_unlock(&ses->ses_lock);
1312 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1313 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1314 req->PreviousSessionId = 0;
1315 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1316 cifs_dbg(FYI, "Binding to sess id: %llx\n", ses->Suid);
1318 /* First session, not a reauthenticate */
1319 req->hdr.SessionId = 0;
1321 * if reconnect, we need to send previous sess id
1324 req->PreviousSessionId = cpu_to_le64(sess_data->previous_session);
1325 req->Flags = 0; /* MBZ */
1326 cifs_dbg(FYI, "Fresh session. Previous: %llx\n",
1327 sess_data->previous_session);
1330 /* enough to enable echos and oplocks and one max size write */
1331 if (server->credits >= server->max_credits)
1332 req->hdr.CreditRequest = cpu_to_le16(0);
1334 req->hdr.CreditRequest = cpu_to_le16(
1335 min_t(int, server->max_credits -
1336 server->credits, 130));
1338 /* only one of SMB2 signing flags may be set in SMB2 request */
1340 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1341 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1342 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1344 req->SecurityMode = 0;
1346 #ifdef CONFIG_CIFS_DFS_UPCALL
1347 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1349 req->Capabilities = 0;
1350 #endif /* DFS_UPCALL */
1352 req->Channel = 0; /* MBZ */
1354 sess_data->iov[0].iov_base = (char *)req;
1356 sess_data->iov[0].iov_len = total_len - 1;
1358 * This variable will be used to clear the buffer
1359 * allocated above in case of any error in the calling function.
1361 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1367 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1369 struct kvec *iov = sess_data->iov;
1371 /* iov[1] is already freed by caller */
1372 if (sess_data->buf0_type != CIFS_NO_BUFFER && iov[0].iov_base)
1373 memzero_explicit(iov[0].iov_base, iov[0].iov_len);
1375 free_rsp_buf(sess_data->buf0_type, iov[0].iov_base);
1376 sess_data->buf0_type = CIFS_NO_BUFFER;
1380 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1383 struct smb_rqst rqst;
1384 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1385 struct kvec rsp_iov = { NULL, 0 };
1387 /* Testing shows that buffer offset must be at location of Buffer[0] */
1388 req->SecurityBufferOffset =
1389 cpu_to_le16(sizeof(struct smb2_sess_setup_req));
1390 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1392 memset(&rqst, 0, sizeof(struct smb_rqst));
1393 rqst.rq_iov = sess_data->iov;
1396 /* BB add code to build os and lm fields */
1397 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1400 &sess_data->buf0_type,
1401 CIFS_LOG_ERROR | CIFS_SESS_OP, &rsp_iov);
1402 cifs_small_buf_release(sess_data->iov[0].iov_base);
1403 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1409 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1412 struct cifs_ses *ses = sess_data->ses;
1413 struct TCP_Server_Info *server = sess_data->server;
1415 cifs_server_lock(server);
1416 if (server->ops->generate_signingkey) {
1417 rc = server->ops->generate_signingkey(ses, server);
1420 "SMB3 session key generation failed\n");
1421 cifs_server_unlock(server);
1425 if (!server->session_estab) {
1426 server->sequence_number = 0x2;
1427 server->session_estab = true;
1429 cifs_server_unlock(server);
1431 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1435 #ifdef CONFIG_CIFS_UPCALL
1437 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1440 struct cifs_ses *ses = sess_data->ses;
1441 struct TCP_Server_Info *server = sess_data->server;
1442 struct cifs_spnego_msg *msg;
1443 struct key *spnego_key = NULL;
1444 struct smb2_sess_setup_rsp *rsp = NULL;
1445 bool is_binding = false;
1447 rc = SMB2_sess_alloc_buffer(sess_data);
1451 spnego_key = cifs_get_spnego_key(ses, server);
1452 if (IS_ERR(spnego_key)) {
1453 rc = PTR_ERR(spnego_key);
1455 cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
1460 msg = spnego_key->payload.data[0];
1462 * check version field to make sure that cifs.upcall is
1463 * sending us a response in an expected form
1465 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1466 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1467 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1469 goto out_put_spnego_key;
1472 spin_lock(&ses->ses_lock);
1473 is_binding = (ses->ses_status == SES_GOOD);
1474 spin_unlock(&ses->ses_lock);
1476 /* keep session key if binding */
1478 kfree_sensitive(ses->auth_key.response);
1479 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1481 if (!ses->auth_key.response) {
1482 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1485 goto out_put_spnego_key;
1487 ses->auth_key.len = msg->sesskey_len;
1490 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1491 sess_data->iov[1].iov_len = msg->secblob_len;
1493 rc = SMB2_sess_sendreceive(sess_data);
1495 goto out_put_spnego_key;
1497 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1498 /* keep session id and flags if binding */
1500 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1501 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1504 rc = SMB2_sess_establish_session(sess_data);
1506 key_invalidate(spnego_key);
1507 key_put(spnego_key);
1509 kfree_sensitive(ses->auth_key.response);
1510 ses->auth_key.response = NULL;
1511 ses->auth_key.len = 0;
1514 sess_data->result = rc;
1515 sess_data->func = NULL;
1516 SMB2_sess_free_buffer(sess_data);
1520 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1522 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1523 sess_data->result = -EOPNOTSUPP;
1524 sess_data->func = NULL;
1529 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1532 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1535 struct cifs_ses *ses = sess_data->ses;
1536 struct TCP_Server_Info *server = sess_data->server;
1537 struct smb2_sess_setup_rsp *rsp = NULL;
1538 unsigned char *ntlmssp_blob = NULL;
1539 bool use_spnego = false; /* else use raw ntlmssp */
1540 u16 blob_length = 0;
1541 bool is_binding = false;
1544 * If memory allocation is successful, caller of this function
1547 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1548 if (!ses->ntlmssp) {
1552 ses->ntlmssp->sesskey_per_smbsess = true;
1554 rc = SMB2_sess_alloc_buffer(sess_data);
1558 rc = build_ntlmssp_smb3_negotiate_blob(&ntlmssp_blob,
1559 &blob_length, ses, server,
1565 /* BB eventually need to add this */
1566 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1570 sess_data->iov[1].iov_base = ntlmssp_blob;
1571 sess_data->iov[1].iov_len = blob_length;
1573 rc = SMB2_sess_sendreceive(sess_data);
1574 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1576 /* If true, rc here is expected and not an error */
1577 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1578 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1584 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1585 le16_to_cpu(rsp->SecurityBufferOffset)) {
1586 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1587 le16_to_cpu(rsp->SecurityBufferOffset));
1591 rc = decode_ntlmssp_challenge(rsp->Buffer,
1592 le16_to_cpu(rsp->SecurityBufferLength), ses);
1596 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1598 spin_lock(&ses->ses_lock);
1599 is_binding = (ses->ses_status == SES_GOOD);
1600 spin_unlock(&ses->ses_lock);
1602 /* keep existing ses id and flags if binding */
1604 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1605 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1609 kfree_sensitive(ntlmssp_blob);
1610 SMB2_sess_free_buffer(sess_data);
1612 sess_data->result = 0;
1613 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1617 kfree_sensitive(ses->ntlmssp);
1618 ses->ntlmssp = NULL;
1619 sess_data->result = rc;
1620 sess_data->func = NULL;
1624 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1627 struct cifs_ses *ses = sess_data->ses;
1628 struct TCP_Server_Info *server = sess_data->server;
1629 struct smb2_sess_setup_req *req;
1630 struct smb2_sess_setup_rsp *rsp = NULL;
1631 unsigned char *ntlmssp_blob = NULL;
1632 bool use_spnego = false; /* else use raw ntlmssp */
1633 u16 blob_length = 0;
1634 bool is_binding = false;
1636 rc = SMB2_sess_alloc_buffer(sess_data);
1640 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1641 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1643 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length,
1647 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1652 /* BB eventually need to add this */
1653 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1657 sess_data->iov[1].iov_base = ntlmssp_blob;
1658 sess_data->iov[1].iov_len = blob_length;
1660 rc = SMB2_sess_sendreceive(sess_data);
1664 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1666 spin_lock(&ses->ses_lock);
1667 is_binding = (ses->ses_status == SES_GOOD);
1668 spin_unlock(&ses->ses_lock);
1670 /* keep existing ses id and flags if binding */
1672 ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
1673 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1676 rc = SMB2_sess_establish_session(sess_data);
1677 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1678 if (ses->server->dialect < SMB30_PROT_ID) {
1679 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1681 * The session id is opaque in terms of endianness, so we can't
1682 * print it as a long long. we dump it as we got it on the wire
1684 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1686 cifs_dbg(VFS, "Session Key %*ph\n",
1687 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1688 cifs_dbg(VFS, "Signing Key %*ph\n",
1689 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1693 kfree_sensitive(ntlmssp_blob);
1694 SMB2_sess_free_buffer(sess_data);
1695 kfree_sensitive(ses->ntlmssp);
1696 ses->ntlmssp = NULL;
1697 sess_data->result = rc;
1698 sess_data->func = NULL;
1702 SMB2_select_sec(struct SMB2_sess_data *sess_data)
1705 struct cifs_ses *ses = sess_data->ses;
1706 struct TCP_Server_Info *server = sess_data->server;
1708 type = smb2_select_sectype(server, ses->sectype);
1709 cifs_dbg(FYI, "sess setup type %d\n", type);
1710 if (type == Unspecified) {
1711 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1717 sess_data->func = SMB2_auth_kerberos;
1720 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1723 cifs_dbg(VFS, "secType %d not supported!\n", type);
1731 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1732 struct TCP_Server_Info *server,
1733 const struct nls_table *nls_cp)
1736 struct SMB2_sess_data *sess_data;
1738 cifs_dbg(FYI, "Session Setup\n");
1741 WARN(1, "%s: server is NULL!\n", __func__);
1745 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1749 sess_data->xid = xid;
1750 sess_data->ses = ses;
1751 sess_data->server = server;
1752 sess_data->buf0_type = CIFS_NO_BUFFER;
1753 sess_data->nls_cp = (struct nls_table *) nls_cp;
1754 sess_data->previous_session = ses->Suid;
1756 rc = SMB2_select_sec(sess_data);
1761 * Initialize the session hash with the server one.
1763 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
1764 SMB2_PREAUTH_HASH_SIZE);
1766 while (sess_data->func)
1767 sess_data->func(sess_data);
1769 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1770 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1771 rc = sess_data->result;
1773 kfree_sensitive(sess_data);
1778 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1780 struct smb_rqst rqst;
1781 struct smb2_logoff_req *req; /* response is also trivial struct */
1783 struct TCP_Server_Info *server;
1785 unsigned int total_len;
1787 struct kvec rsp_iov;
1790 cifs_dbg(FYI, "disconnect session %p\n", ses);
1792 if (ses && (ses->server))
1793 server = ses->server;
1797 /* no need to send SMB logoff if uid already closed due to reconnect */
1798 spin_lock(&ses->chan_lock);
1799 if (CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
1800 spin_unlock(&ses->chan_lock);
1801 goto smb2_session_already_dead;
1803 spin_unlock(&ses->chan_lock);
1805 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
1806 (void **) &req, &total_len);
1810 /* since no tcon, smb2_init can not do this, so do here */
1811 req->hdr.SessionId = cpu_to_le64(ses->Suid);
1813 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1814 flags |= CIFS_TRANSFORM_REQ;
1815 else if (server->sign)
1816 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1818 flags |= CIFS_NO_RSP_BUF;
1820 iov[0].iov_base = (char *)req;
1821 iov[0].iov_len = total_len;
1823 memset(&rqst, 0, sizeof(struct smb_rqst));
1827 rc = cifs_send_recv(xid, ses, ses->server,
1828 &rqst, &resp_buf_type, flags, &rsp_iov);
1829 cifs_small_buf_release(req);
1831 * No tcon so can't do
1832 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1835 smb2_session_already_dead:
1839 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1841 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1844 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1846 /* These are similar values to what Windows uses */
1847 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1849 tcon->max_chunks = 256;
1850 tcon->max_bytes_chunk = 1048576;
1851 tcon->max_bytes_copy = 16777216;
1855 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1856 struct cifs_tcon *tcon, const struct nls_table *cp)
1858 struct smb_rqst rqst;
1859 struct smb2_tree_connect_req *req;
1860 struct smb2_tree_connect_rsp *rsp = NULL;
1862 struct kvec rsp_iov = { NULL, 0 };
1866 __le16 *unc_path = NULL;
1868 unsigned int total_len;
1869 struct TCP_Server_Info *server;
1871 /* always use master channel */
1872 server = ses->server;
1874 cifs_dbg(FYI, "TCON\n");
1876 if (!server || !tree)
1879 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1880 if (unc_path == NULL)
1883 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1885 if (unc_path_len < 2) {
1890 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1892 atomic_set(&tcon->num_remote_opens, 0);
1893 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
1894 (void **) &req, &total_len);
1900 if (smb3_encryption_required(tcon))
1901 flags |= CIFS_TRANSFORM_REQ;
1903 iov[0].iov_base = (char *)req;
1905 iov[0].iov_len = total_len - 1;
1907 /* Testing shows that buffer offset must be at location of Buffer[0] */
1908 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req));
1909 req->PathLength = cpu_to_le16(unc_path_len - 2);
1910 iov[1].iov_base = unc_path;
1911 iov[1].iov_len = unc_path_len;
1914 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1915 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1916 * (Samba servers don't always set the flag so also check if null user)
1918 if ((server->dialect == SMB311_PROT_ID) &&
1919 !smb3_encryption_required(tcon) &&
1920 !(ses->session_flags &
1921 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1922 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1923 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
1925 memset(&rqst, 0, sizeof(struct smb_rqst));
1929 /* Need 64 for max size write so ask for more in case not there yet */
1930 if (server->credits >= server->max_credits)
1931 req->hdr.CreditRequest = cpu_to_le16(0);
1933 req->hdr.CreditRequest = cpu_to_le16(
1934 min_t(int, server->max_credits -
1935 server->credits, 64));
1937 rc = cifs_send_recv(xid, ses, server,
1938 &rqst, &resp_buftype, flags, &rsp_iov);
1939 cifs_small_buf_release(req);
1940 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1941 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1942 if ((rc != 0) || (rsp == NULL)) {
1943 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1944 tcon->need_reconnect = true;
1945 goto tcon_error_exit;
1948 switch (rsp->ShareType) {
1949 case SMB2_SHARE_TYPE_DISK:
1950 cifs_dbg(FYI, "connection to disk share\n");
1952 case SMB2_SHARE_TYPE_PIPE:
1954 cifs_dbg(FYI, "connection to pipe share\n");
1956 case SMB2_SHARE_TYPE_PRINT:
1958 cifs_dbg(FYI, "connection to printer\n");
1961 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1963 goto tcon_error_exit;
1966 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1967 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1968 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1969 tcon->tid = le32_to_cpu(rsp->hdr.Id.SyncId.TreeId);
1970 strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name));
1972 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1973 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1974 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
1977 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1978 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
1980 init_copy_chunk_defaults(tcon);
1981 if (server->ops->validate_negotiate)
1982 rc = server->ops->validate_negotiate(xid, tcon);
1985 free_rsp_buf(resp_buftype, rsp);
1990 if (rsp && rsp->hdr.Status == STATUS_BAD_NETWORK_NAME)
1991 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1996 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1998 struct smb_rqst rqst;
1999 struct smb2_tree_disconnect_req *req; /* response is trivial */
2001 struct cifs_ses *ses = tcon->ses;
2003 unsigned int total_len;
2005 struct kvec rsp_iov;
2008 cifs_dbg(FYI, "Tree Disconnect\n");
2010 if (!ses || !(ses->server))
2013 trace_smb3_tdis_enter(xid, tcon->tid, ses->Suid, tcon->tree_name);
2014 spin_lock(&ses->chan_lock);
2015 if ((tcon->need_reconnect) ||
2016 (CIFS_ALL_CHANS_NEED_RECONNECT(tcon->ses))) {
2017 spin_unlock(&ses->chan_lock);
2020 spin_unlock(&ses->chan_lock);
2022 invalidate_all_cached_dirs(tcon);
2024 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, ses->server,
2030 if (smb3_encryption_required(tcon))
2031 flags |= CIFS_TRANSFORM_REQ;
2033 flags |= CIFS_NO_RSP_BUF;
2035 iov[0].iov_base = (char *)req;
2036 iov[0].iov_len = total_len;
2038 memset(&rqst, 0, sizeof(struct smb_rqst));
2042 rc = cifs_send_recv(xid, ses, ses->server,
2043 &rqst, &resp_buf_type, flags, &rsp_iov);
2044 cifs_small_buf_release(req);
2046 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
2047 trace_smb3_tdis_err(xid, tcon->tid, ses->Suid, rc);
2049 trace_smb3_tdis_done(xid, tcon->tid, ses->Suid);
2055 static struct create_durable *
2056 create_durable_buf(void)
2058 struct create_durable *buf;
2060 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2064 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2065 (struct create_durable, Data));
2066 buf->ccontext.DataLength = cpu_to_le32(16);
2067 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2068 (struct create_durable, Name));
2069 buf->ccontext.NameLength = cpu_to_le16(4);
2070 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
2078 static struct create_durable *
2079 create_reconnect_durable_buf(struct cifs_fid *fid)
2081 struct create_durable *buf;
2083 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
2087 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2088 (struct create_durable, Data));
2089 buf->ccontext.DataLength = cpu_to_le32(16);
2090 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2091 (struct create_durable, Name));
2092 buf->ccontext.NameLength = cpu_to_le16(4);
2093 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
2094 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
2095 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
2104 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
2106 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
2108 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
2109 pdisk_id->DiskFileId, pdisk_id->VolumeId);
2110 buf->IndexNumber = pdisk_id->DiskFileId;
2114 parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
2115 struct create_posix_rsp *posix)
2118 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
2119 u8 *end = beg + le32_to_cpu(cc->DataLength);
2122 memset(posix, 0, sizeof(*posix));
2124 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
2125 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
2126 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
2129 sid_len = posix_info_sid_size(sid, end);
2131 cifs_dbg(VFS, "bad owner sid in posix create response\n");
2134 memcpy(&posix->owner, sid, sid_len);
2136 sid = sid + sid_len;
2137 sid_len = posix_info_sid_size(sid, end);
2139 cifs_dbg(VFS, "bad group sid in posix create response\n");
2142 memcpy(&posix->group, sid, sid_len);
2144 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
2145 posix->nlink, posix->mode, posix->reparse_tag);
2148 int smb2_parse_contexts(struct TCP_Server_Info *server,
2149 struct kvec *rsp_iov,
2150 unsigned int *epoch,
2151 char *lease_key, __u8 *oplock,
2152 struct smb2_file_all_info *buf,
2153 struct create_posix_rsp *posix)
2155 struct smb2_create_rsp *rsp = rsp_iov->iov_base;
2156 struct create_context *cc;
2157 size_t rem, off, len;
2161 static const char smb3_create_tag_posix[] = {
2162 0x93, 0xAD, 0x25, 0x50, 0x9C,
2163 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
2164 0xDE, 0x96, 0x8B, 0xCD, 0x7C
2169 off = le32_to_cpu(rsp->CreateContextsOffset);
2170 rem = le32_to_cpu(rsp->CreateContextsLength);
2171 if (check_add_overflow(off, rem, &len) || len > rsp_iov->iov_len)
2173 cc = (struct create_context *)((u8 *)rsp + off);
2175 /* Initialize inode number to 0 in case no valid data in qfid context */
2177 buf->IndexNumber = 0;
2179 while (rem >= sizeof(*cc)) {
2180 doff = le16_to_cpu(cc->DataOffset);
2181 dlen = le32_to_cpu(cc->DataLength);
2182 if (check_add_overflow(doff, dlen, &len) || len > rem)
2185 noff = le16_to_cpu(cc->NameOffset);
2186 nlen = le16_to_cpu(cc->NameLength);
2187 if (noff + nlen > doff)
2190 name = (char *)cc + noff;
2193 if (!strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4)) {
2194 *oplock = server->ops->parse_lease_buf(cc, epoch,
2197 !strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4)) {
2198 parse_query_id_ctxt(cc, buf);
2202 if (posix && !memcmp(name, smb3_create_tag_posix, 16))
2203 parse_posix_ctxt(cc, buf, posix);
2206 cifs_dbg(FYI, "%s: unhandled context (nlen=%zu dlen=%zu)\n",
2207 __func__, nlen, dlen);
2208 if (IS_ENABLED(CONFIG_CIFS_DEBUG2))
2209 cifs_dump_mem("context data: ", cc, dlen);
2213 off = le32_to_cpu(cc->Next);
2216 if (check_sub_overflow(rem, off, &rem))
2218 cc = (struct create_context *)((u8 *)cc + off);
2221 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2222 *oplock = rsp->OplockLevel;
2228 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
2229 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
2231 struct smb2_create_req *req = iov[0].iov_base;
2232 unsigned int num = *num_iovec;
2234 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
2235 if (iov[num].iov_base == NULL)
2237 iov[num].iov_len = server->vals->create_lease_size;
2238 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2239 if (!req->CreateContextsOffset)
2240 req->CreateContextsOffset = cpu_to_le32(
2241 sizeof(struct smb2_create_req) +
2242 iov[num - 1].iov_len);
2243 le32_add_cpu(&req->CreateContextsLength,
2244 server->vals->create_lease_size);
2245 *num_iovec = num + 1;
2249 static struct create_durable_v2 *
2250 create_durable_v2_buf(struct cifs_open_parms *oparms)
2252 struct cifs_fid *pfid = oparms->fid;
2253 struct create_durable_v2 *buf;
2255 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2259 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2260 (struct create_durable_v2, dcontext));
2261 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2262 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2263 (struct create_durable_v2, Name));
2264 buf->ccontext.NameLength = cpu_to_le16(4);
2267 * NB: Handle timeout defaults to 0, which allows server to choose
2268 * (most servers default to 120 seconds) and most clients default to 0.
2269 * This can be overridden at mount ("handletimeout=") if the user wants
2270 * a different persistent (or resilient) handle timeout for all opens
2271 * opens on a particular SMB3 mount.
2273 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2274 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2275 generate_random_uuid(buf->dcontext.CreateGuid);
2276 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2278 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2286 static struct create_durable_handle_reconnect_v2 *
2287 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2289 struct create_durable_handle_reconnect_v2 *buf;
2291 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2296 buf->ccontext.DataOffset =
2297 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2299 buf->ccontext.DataLength =
2300 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2301 buf->ccontext.NameOffset =
2302 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2304 buf->ccontext.NameLength = cpu_to_le16(4);
2306 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2307 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2308 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2309 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2311 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2320 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2321 struct cifs_open_parms *oparms)
2323 struct smb2_create_req *req = iov[0].iov_base;
2324 unsigned int num = *num_iovec;
2326 iov[num].iov_base = create_durable_v2_buf(oparms);
2327 if (iov[num].iov_base == NULL)
2329 iov[num].iov_len = sizeof(struct create_durable_v2);
2330 if (!req->CreateContextsOffset)
2331 req->CreateContextsOffset =
2332 cpu_to_le32(sizeof(struct smb2_create_req) +
2334 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
2335 *num_iovec = num + 1;
2340 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2341 struct cifs_open_parms *oparms)
2343 struct smb2_create_req *req = iov[0].iov_base;
2344 unsigned int num = *num_iovec;
2346 /* indicate that we don't need to relock the file */
2347 oparms->reconnect = false;
2349 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2350 if (iov[num].iov_base == NULL)
2352 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2353 if (!req->CreateContextsOffset)
2354 req->CreateContextsOffset =
2355 cpu_to_le32(sizeof(struct smb2_create_req) +
2357 le32_add_cpu(&req->CreateContextsLength,
2358 sizeof(struct create_durable_handle_reconnect_v2));
2359 *num_iovec = num + 1;
2364 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2365 struct cifs_open_parms *oparms, bool use_persistent)
2367 struct smb2_create_req *req = iov[0].iov_base;
2368 unsigned int num = *num_iovec;
2370 if (use_persistent) {
2371 if (oparms->reconnect)
2372 return add_durable_reconnect_v2_context(iov, num_iovec,
2375 return add_durable_v2_context(iov, num_iovec, oparms);
2378 if (oparms->reconnect) {
2379 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2380 /* indicate that we don't need to relock the file */
2381 oparms->reconnect = false;
2383 iov[num].iov_base = create_durable_buf();
2384 if (iov[num].iov_base == NULL)
2386 iov[num].iov_len = sizeof(struct create_durable);
2387 if (!req->CreateContextsOffset)
2388 req->CreateContextsOffset =
2389 cpu_to_le32(sizeof(struct smb2_create_req) +
2391 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
2392 *num_iovec = num + 1;
2396 /* See MS-SMB2 2.2.13.2.7 */
2397 static struct crt_twarp_ctxt *
2398 create_twarp_buf(__u64 timewarp)
2400 struct crt_twarp_ctxt *buf;
2402 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2406 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2407 (struct crt_twarp_ctxt, Timestamp));
2408 buf->ccontext.DataLength = cpu_to_le32(8);
2409 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2410 (struct crt_twarp_ctxt, Name));
2411 buf->ccontext.NameLength = cpu_to_le16(4);
2412 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2417 buf->Timestamp = cpu_to_le64(timewarp);
2421 /* See MS-SMB2 2.2.13.2.7 */
2423 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2425 struct smb2_create_req *req = iov[0].iov_base;
2426 unsigned int num = *num_iovec;
2428 iov[num].iov_base = create_twarp_buf(timewarp);
2429 if (iov[num].iov_base == NULL)
2431 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2432 if (!req->CreateContextsOffset)
2433 req->CreateContextsOffset = cpu_to_le32(
2434 sizeof(struct smb2_create_req) +
2435 iov[num - 1].iov_len);
2436 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2437 *num_iovec = num + 1;
2441 /* See See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
2442 static void setup_owner_group_sids(char *buf)
2444 struct owner_group_sids *sids = (struct owner_group_sids *)buf;
2446 /* Populate the user ownership fields S-1-5-88-1 */
2447 sids->owner.Revision = 1;
2448 sids->owner.NumAuth = 3;
2449 sids->owner.Authority[5] = 5;
2450 sids->owner.SubAuthorities[0] = cpu_to_le32(88);
2451 sids->owner.SubAuthorities[1] = cpu_to_le32(1);
2452 sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val);
2454 /* Populate the group ownership fields S-1-5-88-2 */
2455 sids->group.Revision = 1;
2456 sids->group.NumAuth = 3;
2457 sids->group.Authority[5] = 5;
2458 sids->group.SubAuthorities[0] = cpu_to_le32(88);
2459 sids->group.SubAuthorities[1] = cpu_to_le32(2);
2460 sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val);
2462 cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val);
2465 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2466 static struct crt_sd_ctxt *
2467 create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
2469 struct crt_sd_ctxt *buf;
2471 unsigned int acelen, acl_size, ace_count;
2472 unsigned int owner_offset = 0;
2473 unsigned int group_offset = 0;
2474 struct smb3_acl acl = {};
2476 *len = round_up(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 4), 8);
2479 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
2480 *len += sizeof(struct owner_group_sids);
2483 buf = kzalloc(*len, GFP_KERNEL);
2487 ptr = (__u8 *)&buf[1];
2489 /* offset fields are from beginning of security descriptor not of create context */
2490 owner_offset = ptr - (__u8 *)&buf->sd;
2491 buf->sd.OffsetOwner = cpu_to_le32(owner_offset);
2492 group_offset = owner_offset + offsetof(struct owner_group_sids, group);
2493 buf->sd.OffsetGroup = cpu_to_le32(group_offset);
2495 setup_owner_group_sids(ptr);
2496 ptr += sizeof(struct owner_group_sids);
2498 buf->sd.OffsetOwner = 0;
2499 buf->sd.OffsetGroup = 0;
2502 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, sd));
2503 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name));
2504 buf->ccontext.NameLength = cpu_to_le16(4);
2505 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2510 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2513 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2514 * and "DP" ie the DACL is present
2516 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2518 /* offset owner, group and Sbz1 and SACL are all zero */
2519 buf->sd.OffsetDacl = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2520 /* Ship the ACL for now. we will copy it into buf later. */
2522 ptr += sizeof(struct smb3_acl);
2524 /* create one ACE to hold the mode embedded in reserved special SID */
2525 acelen = setup_special_mode_ACE((struct cifs_ace *)ptr, (__u64)mode);
2527 acl_size = acelen + sizeof(struct smb3_acl);
2531 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
2532 acelen = setup_special_user_owner_ACE((struct cifs_ace *)ptr);
2538 /* and one more ACE to allow access for authenticated users */
2539 acelen = setup_authusers_ACE((struct cifs_ace *)ptr);
2544 acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2545 acl.AclSize = cpu_to_le16(acl_size);
2546 acl.AceCount = cpu_to_le16(ace_count);
2547 /* acl.Sbz1 and Sbz2 MBZ so are not set here, but initialized above */
2548 memcpy(aclptr, &acl, sizeof(struct smb3_acl));
2550 buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2551 *len = round_up((unsigned int)(ptr - (__u8 *)buf), 8);
2557 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner)
2559 struct smb2_create_req *req = iov[0].iov_base;
2560 unsigned int num = *num_iovec;
2561 unsigned int len = 0;
2563 iov[num].iov_base = create_sd_buf(mode, set_owner, &len);
2564 if (iov[num].iov_base == NULL)
2566 iov[num].iov_len = len;
2567 if (!req->CreateContextsOffset)
2568 req->CreateContextsOffset = cpu_to_le32(
2569 sizeof(struct smb2_create_req) +
2570 iov[num - 1].iov_len);
2571 le32_add_cpu(&req->CreateContextsLength, len);
2572 *num_iovec = num + 1;
2576 static struct crt_query_id_ctxt *
2577 create_query_id_buf(void)
2579 struct crt_query_id_ctxt *buf;
2581 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2585 buf->ccontext.DataOffset = cpu_to_le16(0);
2586 buf->ccontext.DataLength = cpu_to_le32(0);
2587 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2588 (struct crt_query_id_ctxt, Name));
2589 buf->ccontext.NameLength = cpu_to_le16(4);
2590 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2598 /* See MS-SMB2 2.2.13.2.9 */
2600 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2602 struct smb2_create_req *req = iov[0].iov_base;
2603 unsigned int num = *num_iovec;
2605 iov[num].iov_base = create_query_id_buf();
2606 if (iov[num].iov_base == NULL)
2608 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2609 if (!req->CreateContextsOffset)
2610 req->CreateContextsOffset = cpu_to_le32(
2611 sizeof(struct smb2_create_req) +
2612 iov[num - 1].iov_len);
2613 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2614 *num_iovec = num + 1;
2619 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2620 const char *treename, const __le16 *path)
2622 int treename_len, path_len;
2623 struct nls_table *cp;
2624 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2629 treename_len = strlen(treename);
2630 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2636 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2638 /* make room for one path separator only if @path isn't empty */
2639 *out_len = treename_len + (path[0] ? 1 : 0) + path_len;
2642 * final path needs to be 8-byte aligned as specified in
2643 * MS-SMB2 2.2.13 SMB2 CREATE Request.
2645 *out_size = round_up(*out_len * sizeof(__le16), 8);
2646 *out_path = kzalloc(*out_size + sizeof(__le16) /* null */, GFP_KERNEL);
2650 cp = load_nls_default();
2651 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2653 /* Do not append the separator if the path is empty */
2654 if (path[0] != cpu_to_le16(0x0000)) {
2655 UniStrcat(*out_path, sep);
2656 UniStrcat(*out_path, path);
2664 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2665 umode_t mode, struct cifs_tcon *tcon,
2666 const char *full_path,
2667 struct cifs_sb_info *cifs_sb)
2669 struct smb_rqst rqst;
2670 struct smb2_create_req *req;
2671 struct smb2_create_rsp *rsp = NULL;
2672 struct cifs_ses *ses = tcon->ses;
2673 struct kvec iov[3]; /* make sure at least one for each open context */
2674 struct kvec rsp_iov = {NULL, 0};
2677 __le16 *copy_path = NULL;
2680 unsigned int n_iov = 2;
2681 __u32 file_attributes = 0;
2682 char *pc_buf = NULL;
2684 unsigned int total_len;
2685 __le16 *utf16_path = NULL;
2686 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2688 cifs_dbg(FYI, "mkdir\n");
2690 /* resource #1: path allocation */
2691 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2695 if (!ses || !server) {
2700 /* resource #2: request */
2701 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2702 (void **) &req, &total_len);
2707 if (smb3_encryption_required(tcon))
2708 flags |= CIFS_TRANSFORM_REQ;
2710 req->ImpersonationLevel = IL_IMPERSONATION;
2711 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2712 /* File attributes ignored on open (used in create though) */
2713 req->FileAttributes = cpu_to_le32(file_attributes);
2714 req->ShareAccess = FILE_SHARE_ALL_LE;
2715 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2716 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2718 iov[0].iov_base = (char *)req;
2719 /* -1 since last byte is buf[0] which is sent below (path) */
2720 iov[0].iov_len = total_len - 1;
2722 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2724 /* [MS-SMB2] 2.2.13 NameOffset:
2725 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2726 * the SMB2 header, the file name includes a prefix that will
2727 * be processed during DFS name normalization as specified in
2728 * section 3.3.5.9. Otherwise, the file name is relative to
2729 * the share that is identified by the TreeId in the SMB2
2732 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2735 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2736 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2738 tcon->tree_name, utf16_path);
2742 req->NameLength = cpu_to_le16(name_len * 2);
2743 uni_path_len = copy_size;
2744 /* free before overwriting resource */
2746 utf16_path = copy_path;
2748 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2749 /* MUST set path len (NameLength) to 0 opening root of share */
2750 req->NameLength = cpu_to_le16(uni_path_len - 2);
2751 if (uni_path_len % 8 != 0) {
2752 copy_size = roundup(uni_path_len, 8);
2753 copy_path = kzalloc(copy_size, GFP_KERNEL);
2758 memcpy((char *)copy_path, (const char *)utf16_path,
2760 uni_path_len = copy_size;
2761 /* free before overwriting resource */
2763 utf16_path = copy_path;
2767 iov[1].iov_len = uni_path_len;
2768 iov[1].iov_base = utf16_path;
2769 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2771 if (tcon->posix_extensions) {
2772 /* resource #3: posix buf */
2773 rc = add_posix_context(iov, &n_iov, mode);
2776 pc_buf = iov[n_iov-1].iov_base;
2780 memset(&rqst, 0, sizeof(struct smb_rqst));
2782 rqst.rq_nvec = n_iov;
2784 /* no need to inc num_remote_opens because we close it just below */
2785 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, full_path, CREATE_NOT_FILE,
2786 FILE_WRITE_ATTRIBUTES);
2787 /* resource #4: response buffer */
2788 rc = cifs_send_recv(xid, ses, server,
2789 &rqst, &resp_buftype, flags, &rsp_iov);
2791 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2792 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2794 FILE_WRITE_ATTRIBUTES, rc);
2795 goto err_free_rsp_buf;
2799 * Although unlikely to be possible for rsp to be null and rc not set,
2800 * adding check below is slightly safer long term (and quiets Coverity
2803 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2810 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
2811 CREATE_NOT_FILE, FILE_WRITE_ATTRIBUTES);
2813 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2815 /* Eventually save off posix specific response info and timestaps */
2818 free_rsp_buf(resp_buftype, rsp);
2821 cifs_small_buf_release(req);
2828 SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2829 struct smb_rqst *rqst, __u8 *oplock,
2830 struct cifs_open_parms *oparms, __le16 *path)
2832 struct smb2_create_req *req;
2833 unsigned int n_iov = 2;
2834 __u32 file_attributes = 0;
2837 unsigned int total_len;
2838 struct kvec *iov = rqst->rq_iov;
2842 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2843 (void **) &req, &total_len);
2847 iov[0].iov_base = (char *)req;
2848 /* -1 since last byte is buf[0] which is sent below (path) */
2849 iov[0].iov_len = total_len - 1;
2851 if (oparms->create_options & CREATE_OPTION_READONLY)
2852 file_attributes |= ATTR_READONLY;
2853 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2854 file_attributes |= ATTR_SYSTEM;
2856 req->ImpersonationLevel = IL_IMPERSONATION;
2857 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2858 /* File attributes ignored on open (used in create though) */
2859 req->FileAttributes = cpu_to_le32(file_attributes);
2860 req->ShareAccess = FILE_SHARE_ALL_LE;
2862 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2863 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2864 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2866 /* [MS-SMB2] 2.2.13 NameOffset:
2867 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2868 * the SMB2 header, the file name includes a prefix that will
2869 * be processed during DFS name normalization as specified in
2870 * section 3.3.5.9. Otherwise, the file name is relative to
2871 * the share that is identified by the TreeId in the SMB2
2874 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2877 req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2878 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2880 tcon->tree_name, path);
2883 req->NameLength = cpu_to_le16(name_len * 2);
2884 uni_path_len = copy_size;
2887 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2888 /* MUST set path len (NameLength) to 0 opening root of share */
2889 req->NameLength = cpu_to_le16(uni_path_len - 2);
2890 copy_size = round_up(uni_path_len, 8);
2891 copy_path = kzalloc(copy_size, GFP_KERNEL);
2894 memcpy((char *)copy_path, (const char *)path,
2896 uni_path_len = copy_size;
2900 iov[1].iov_len = uni_path_len;
2901 iov[1].iov_base = path;
2903 if ((!server->oplocks) || (tcon->no_lease))
2904 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2906 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2907 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2908 req->RequestedOplockLevel = *oplock;
2909 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2910 (oparms->create_options & CREATE_NOT_FILE))
2911 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2913 rc = add_lease_context(server, iov, &n_iov,
2914 oparms->fid->lease_key, oplock);
2919 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2920 /* need to set Next field of lease context if we request it */
2921 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2922 struct create_context *ccontext =
2923 (struct create_context *)iov[n_iov-1].iov_base;
2925 cpu_to_le32(server->vals->create_lease_size);
2928 rc = add_durable_context(iov, &n_iov, oparms,
2929 tcon->use_persistent);
2934 if (tcon->posix_extensions) {
2936 struct create_context *ccontext =
2937 (struct create_context *)iov[n_iov-1].iov_base;
2939 cpu_to_le32(iov[n_iov-1].iov_len);
2942 rc = add_posix_context(iov, &n_iov, oparms->mode);
2947 if (tcon->snapshot_time) {
2948 cifs_dbg(FYI, "adding snapshot context\n");
2950 struct create_context *ccontext =
2951 (struct create_context *)iov[n_iov-1].iov_base;
2953 cpu_to_le32(iov[n_iov-1].iov_len);
2956 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2961 if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) {
2965 if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
2966 (oparms->mode != ACL_NO_MODE))
2970 oparms->mode = ACL_NO_MODE;
2973 if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
2978 if (set_owner | set_mode) {
2980 struct create_context *ccontext =
2981 (struct create_context *)iov[n_iov-1].iov_base;
2982 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2985 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2986 rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner);
2993 struct create_context *ccontext =
2994 (struct create_context *)iov[n_iov-1].iov_base;
2995 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2997 add_query_id_context(iov, &n_iov);
2999 rqst->rq_nvec = n_iov;
3003 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
3004 * All other vectors are freed by kfree().
3007 SMB2_open_free(struct smb_rqst *rqst)
3011 if (rqst && rqst->rq_iov) {
3012 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
3013 for (i = 1; i < rqst->rq_nvec; i++)
3014 if (rqst->rq_iov[i].iov_base != smb2_padding)
3015 kfree(rqst->rq_iov[i].iov_base);
3020 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
3021 __u8 *oplock, struct smb2_file_all_info *buf,
3022 struct create_posix_rsp *posix,
3023 struct kvec *err_iov, int *buftype)
3025 struct smb_rqst rqst;
3026 struct smb2_create_rsp *rsp = NULL;
3027 struct cifs_tcon *tcon = oparms->tcon;
3028 struct cifs_ses *ses = tcon->ses;
3029 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3030 struct kvec iov[SMB2_CREATE_IOV_SIZE];
3031 struct kvec rsp_iov = {NULL, 0};
3032 int resp_buftype = CIFS_NO_BUFFER;
3036 cifs_dbg(FYI, "create/open\n");
3037 if (!ses || !server)
3040 if (smb3_encryption_required(tcon))
3041 flags |= CIFS_TRANSFORM_REQ;
3043 memset(&rqst, 0, sizeof(struct smb_rqst));
3044 memset(&iov, 0, sizeof(iov));
3046 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
3048 rc = SMB2_open_init(tcon, server,
3049 &rqst, oplock, oparms, path);
3053 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid, oparms->path,
3054 oparms->create_options, oparms->desired_access);
3056 rc = cifs_send_recv(xid, ses, server,
3057 &rqst, &resp_buftype, flags,
3059 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
3062 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
3063 if (err_iov && rsp) {
3065 *buftype = resp_buftype;
3066 resp_buftype = CIFS_NO_BUFFER;
3069 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
3070 oparms->create_options, oparms->desired_access, rc);
3071 if (rc == -EREMCHG) {
3072 pr_warn_once("server share %s deleted\n",
3074 tcon->need_reconnect = true;
3077 } else if (rsp == NULL) /* unlikely to happen, but safer to check */
3080 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
3081 oparms->create_options, oparms->desired_access);
3083 atomic_inc(&tcon->num_remote_opens);
3084 oparms->fid->persistent_fid = rsp->PersistentFileId;
3085 oparms->fid->volatile_fid = rsp->VolatileFileId;
3086 oparms->fid->access = oparms->desired_access;
3087 #ifdef CONFIG_CIFS_DEBUG2
3088 oparms->fid->mid = le64_to_cpu(rsp->hdr.MessageId);
3089 #endif /* CIFS_DEBUG2 */
3092 buf->CreationTime = rsp->CreationTime;
3093 buf->LastAccessTime = rsp->LastAccessTime;
3094 buf->LastWriteTime = rsp->LastWriteTime;
3095 buf->ChangeTime = rsp->ChangeTime;
3096 buf->AllocationSize = rsp->AllocationSize;
3097 buf->EndOfFile = rsp->EndofFile;
3098 buf->Attributes = rsp->FileAttributes;
3099 buf->NumberOfLinks = cpu_to_le32(1);
3100 buf->DeletePending = 0;
3104 rc = smb2_parse_contexts(server, &rsp_iov, &oparms->fid->epoch,
3105 oparms->fid->lease_key, oplock, buf, posix);
3107 SMB2_open_free(&rqst);
3108 free_rsp_buf(resp_buftype, rsp);
3113 SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3114 struct smb_rqst *rqst,
3115 u64 persistent_fid, u64 volatile_fid, u32 opcode,
3116 char *in_data, u32 indatalen,
3117 __u32 max_response_size)
3119 struct smb2_ioctl_req *req;
3120 struct kvec *iov = rqst->rq_iov;
3121 unsigned int total_len;
3125 rc = smb2_ioctl_req_init(opcode, tcon, server,
3126 (void **) &req, &total_len);
3132 * indatalen is usually small at a couple of bytes max, so
3133 * just allocate through generic pool
3135 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
3137 cifs_small_buf_release(req);
3142 req->CtlCode = cpu_to_le32(opcode);
3143 req->PersistentFileId = persistent_fid;
3144 req->VolatileFileId = volatile_fid;
3146 iov[0].iov_base = (char *)req;
3148 * If no input data, the size of ioctl struct in
3149 * protocol spec still includes a 1 byte data buffer,
3150 * but if input data passed to ioctl, we do not
3151 * want to double count this, so we do not send
3152 * the dummy one byte of data in iovec[0] if sending
3153 * input data (in iovec[1]).
3156 req->InputCount = cpu_to_le32(indatalen);
3157 /* do not set InputOffset if no input data */
3159 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
3161 iov[0].iov_len = total_len - 1;
3162 iov[1].iov_base = in_data_buf;
3163 iov[1].iov_len = indatalen;
3166 iov[0].iov_len = total_len;
3169 req->OutputOffset = 0;
3170 req->OutputCount = 0; /* MBZ */
3173 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
3174 * We Could increase default MaxOutputResponse, but that could require
3175 * more credits. Windows typically sets this smaller, but for some
3176 * ioctls it may be useful to allow server to send more. No point
3177 * limiting what the server can send as long as fits in one credit
3178 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
3179 * to increase this limit up in the future.
3180 * Note that for snapshot queries that servers like Azure expect that
3181 * the first query be minimal size (and just used to get the number/size
3182 * of previous versions) so response size must be specified as EXACTLY
3183 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
3184 * of eight bytes. Currently that is the only case where we set max
3185 * response size smaller.
3187 req->MaxOutputResponse = cpu_to_le32(max_response_size);
3188 req->hdr.CreditCharge =
3189 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
3190 SMB2_MAX_BUFFER_SIZE));
3191 /* always an FSCTL (for now) */
3192 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
3194 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
3195 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
3196 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
3202 SMB2_ioctl_free(struct smb_rqst *rqst)
3205 if (rqst && rqst->rq_iov) {
3206 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3207 for (i = 1; i < rqst->rq_nvec; i++)
3208 if (rqst->rq_iov[i].iov_base != smb2_padding)
3209 kfree(rqst->rq_iov[i].iov_base);
3215 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
3218 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3219 u64 volatile_fid, u32 opcode, char *in_data, u32 indatalen,
3220 u32 max_out_data_len, char **out_data,
3221 u32 *plen /* returned data len */)
3223 struct smb_rqst rqst;
3224 struct smb2_ioctl_rsp *rsp = NULL;
3225 struct cifs_ses *ses;
3226 struct TCP_Server_Info *server;
3227 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
3228 struct kvec rsp_iov = {NULL, 0};
3229 int resp_buftype = CIFS_NO_BUFFER;
3233 cifs_dbg(FYI, "SMB2 IOCTL\n");
3235 if (out_data != NULL)
3238 /* zero out returned data len, in case of error */
3249 server = cifs_pick_channel(ses);
3253 if (smb3_encryption_required(tcon))
3254 flags |= CIFS_TRANSFORM_REQ;
3256 memset(&rqst, 0, sizeof(struct smb_rqst));
3257 memset(&iov, 0, sizeof(iov));
3259 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
3261 rc = SMB2_ioctl_init(tcon, server,
3262 &rqst, persistent_fid, volatile_fid, opcode,
3263 in_data, indatalen, max_out_data_len);
3267 rc = cifs_send_recv(xid, ses, server,
3268 &rqst, &resp_buftype, flags,
3270 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
3273 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3274 ses->Suid, 0, opcode, rc);
3276 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
3277 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3279 } else if (rc == -EINVAL) {
3280 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3281 (opcode != FSCTL_SRV_COPYCHUNK)) {
3282 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3285 } else if (rc == -E2BIG) {
3286 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3287 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3292 /* check if caller wants to look at return data or just return rc */
3293 if ((plen == NULL) || (out_data == NULL))
3297 * Although unlikely to be possible for rsp to be null and rc not set,
3298 * adding check below is slightly safer long term (and quiets Coverity
3306 *plen = le32_to_cpu(rsp->OutputCount);
3308 /* We check for obvious errors in the output buffer length and offset */
3310 goto ioctl_exit; /* server returned no data */
3311 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
3312 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
3318 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
3319 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
3320 le32_to_cpu(rsp->OutputOffset));
3326 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3328 if (*out_data == NULL) {
3334 SMB2_ioctl_free(&rqst);
3335 free_rsp_buf(resp_buftype, rsp);
3340 * Individual callers to ioctl worker function follow
3344 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3345 u64 persistent_fid, u64 volatile_fid)
3348 struct compress_ioctl fsctl_input;
3349 char *ret_data = NULL;
3351 fsctl_input.CompressionState =
3352 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3354 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3355 FSCTL_SET_COMPRESSION,
3356 (char *)&fsctl_input /* data input */,
3357 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3358 &ret_data /* out data */, NULL);
3360 cifs_dbg(FYI, "set compression rc %d\n", rc);
3366 SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3367 struct smb_rqst *rqst,
3368 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
3370 struct smb2_close_req *req;
3371 struct kvec *iov = rqst->rq_iov;
3372 unsigned int total_len;
3375 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
3376 (void **) &req, &total_len);
3380 req->PersistentFileId = persistent_fid;
3381 req->VolatileFileId = volatile_fid;
3383 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3386 iov[0].iov_base = (char *)req;
3387 iov[0].iov_len = total_len;
3393 SMB2_close_free(struct smb_rqst *rqst)
3395 if (rqst && rqst->rq_iov)
3396 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3400 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3401 u64 persistent_fid, u64 volatile_fid,
3402 struct smb2_file_network_open_info *pbuf)
3404 struct smb_rqst rqst;
3405 struct smb2_close_rsp *rsp = NULL;
3406 struct cifs_ses *ses = tcon->ses;
3407 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3409 struct kvec rsp_iov;
3410 int resp_buftype = CIFS_NO_BUFFER;
3413 bool query_attrs = false;
3415 cifs_dbg(FYI, "Close\n");
3417 if (!ses || !server)
3420 if (smb3_encryption_required(tcon))
3421 flags |= CIFS_TRANSFORM_REQ;
3423 memset(&rqst, 0, sizeof(struct smb_rqst));
3424 memset(&iov, 0, sizeof(iov));
3428 /* check if need to ask server to return timestamps in close response */
3432 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3433 rc = SMB2_close_init(tcon, server,
3434 &rqst, persistent_fid, volatile_fid,
3439 rc = cifs_send_recv(xid, ses, server,
3440 &rqst, &resp_buftype, flags, &rsp_iov);
3441 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
3444 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
3445 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3449 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3452 memcpy(&pbuf->network_open_info,
3453 &rsp->network_open_info,
3454 sizeof(pbuf->network_open_info));
3455 atomic_dec(&tcon->num_remote_opens);
3459 SMB2_close_free(&rqst);
3460 free_rsp_buf(resp_buftype, rsp);
3462 /* retry close in a worker thread if this one is interrupted */
3463 if (is_interrupt_error(rc)) {
3466 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3469 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3470 persistent_fid, tmp_rc);
3476 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3477 u64 persistent_fid, u64 volatile_fid)
3479 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3483 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3484 struct kvec *iov, unsigned int min_buf_size)
3486 unsigned int smb_len = iov->iov_len;
3487 char *end_of_smb = smb_len + (char *)iov->iov_base;
3488 char *begin_of_buf = offset + (char *)iov->iov_base;
3489 char *end_of_buf = begin_of_buf + buffer_length;
3492 if (buffer_length < min_buf_size) {
3493 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3494 buffer_length, min_buf_size);
3498 /* check if beyond RFC1001 maximum length */
3499 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3500 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3501 buffer_length, smb_len);
3505 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3506 cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
3514 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3515 * Caller must free buffer.
3518 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3519 struct kvec *iov, unsigned int minbufsize,
3522 char *begin_of_buf = offset + (char *)iov->iov_base;
3528 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3532 memcpy(data, begin_of_buf, minbufsize);
3538 SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3539 struct smb_rqst *rqst,
3540 u64 persistent_fid, u64 volatile_fid,
3541 u8 info_class, u8 info_type, u32 additional_info,
3542 size_t output_len, size_t input_len, void *input)
3544 struct smb2_query_info_req *req;
3545 struct kvec *iov = rqst->rq_iov;
3546 unsigned int total_len;
3550 if (unlikely(check_add_overflow(input_len, sizeof(*req), &len) ||
3551 len > CIFSMaxBufSize))
3554 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
3555 (void **) &req, &total_len);
3559 req->InfoType = info_type;
3560 req->FileInfoClass = info_class;
3561 req->PersistentFileId = persistent_fid;
3562 req->VolatileFileId = volatile_fid;
3563 req->AdditionalInformation = cpu_to_le32(additional_info);
3565 req->OutputBufferLength = cpu_to_le32(output_len);
3567 req->InputBufferLength = cpu_to_le32(input_len);
3568 /* total_len for smb query request never close to le16 max */
3569 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3570 memcpy(req->Buffer, input, input_len);
3573 iov[0].iov_base = (char *)req;
3575 iov[0].iov_len = len;
3580 SMB2_query_info_free(struct smb_rqst *rqst)
3582 if (rqst && rqst->rq_iov)
3583 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
3587 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3588 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3589 u32 additional_info, size_t output_len, size_t min_len, void **data,
3592 struct smb_rqst rqst;
3593 struct smb2_query_info_rsp *rsp = NULL;
3595 struct kvec rsp_iov;
3597 int resp_buftype = CIFS_NO_BUFFER;
3598 struct cifs_ses *ses = tcon->ses;
3599 struct TCP_Server_Info *server;
3601 bool allocated = false;
3603 cifs_dbg(FYI, "Query Info\n");
3607 server = cifs_pick_channel(ses);
3611 if (smb3_encryption_required(tcon))
3612 flags |= CIFS_TRANSFORM_REQ;
3614 memset(&rqst, 0, sizeof(struct smb_rqst));
3615 memset(&iov, 0, sizeof(iov));
3619 rc = SMB2_query_info_init(tcon, server,
3620 &rqst, persistent_fid, volatile_fid,
3621 info_class, info_type, additional_info,
3622 output_len, 0, NULL);
3626 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3627 ses->Suid, info_class, (__u32)info_type);
3629 rc = cifs_send_recv(xid, ses, server,
3630 &rqst, &resp_buftype, flags, &rsp_iov);
3631 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3634 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3635 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3636 ses->Suid, info_class, (__u32)info_type, rc);
3640 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3641 ses->Suid, info_class, (__u32)info_type);
3644 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3646 *data = kmalloc(*dlen, GFP_KERNEL);
3649 "Error %d allocating memory for acl\n",
3659 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3660 le32_to_cpu(rsp->OutputBufferLength),
3661 &rsp_iov, dlen ? *dlen : min_len, *data);
3662 if (rc && allocated) {
3669 SMB2_query_info_free(&rqst);
3670 free_rsp_buf(resp_buftype, rsp);
3674 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3675 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3677 return query_info(xid, tcon, persistent_fid, volatile_fid,
3678 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3679 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3680 sizeof(struct smb2_file_all_info), (void **)&data,
3685 /* currently unused, as now we are doing compounding instead (see smb311_posix_query_path_info) */
3687 SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3688 u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen)
3690 size_t output_len = sizeof(struct smb311_posix_qinfo *) +
3691 (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2);
3694 return query_info(xid, tcon, persistent_fid, volatile_fid,
3695 SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0,
3696 output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen);
3697 /* Note caller must free "data" (passed in above). It may be allocated in query_info call */
3702 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3703 u64 persistent_fid, u64 volatile_fid,
3704 void **data, u32 *plen, u32 extra_info)
3706 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
3710 return query_info(xid, tcon, persistent_fid, volatile_fid,
3711 0, SMB2_O_INFO_SECURITY, additional_info,
3712 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3716 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3717 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3719 return query_info(xid, tcon, persistent_fid, volatile_fid,
3720 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3721 sizeof(struct smb2_file_internal_info),
3722 sizeof(struct smb2_file_internal_info),
3723 (void **)&uniqueid, NULL);
3727 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3728 * See MS-SMB2 2.2.35 and 2.2.36
3732 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3733 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3734 u64 persistent_fid, u64 volatile_fid,
3735 u32 completion_filter, bool watch_tree)
3737 struct smb2_change_notify_req *req;
3738 struct kvec *iov = rqst->rq_iov;
3739 unsigned int total_len;
3742 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
3743 (void **) &req, &total_len);
3747 req->PersistentFileId = persistent_fid;
3748 req->VolatileFileId = volatile_fid;
3749 /* See note 354 of MS-SMB2, 64K max */
3750 req->OutputBufferLength =
3751 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3752 req->CompletionFilter = cpu_to_le32(completion_filter);
3754 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3758 iov[0].iov_base = (char *)req;
3759 iov[0].iov_len = total_len;
3765 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3766 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3767 u32 completion_filter, u32 max_out_data_len, char **out_data,
3768 u32 *plen /* returned data len */)
3770 struct cifs_ses *ses = tcon->ses;
3771 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3772 struct smb_rqst rqst;
3773 struct smb2_change_notify_rsp *smb_rsp;
3775 struct kvec rsp_iov = {NULL, 0};
3776 int resp_buftype = CIFS_NO_BUFFER;
3780 cifs_dbg(FYI, "change notify\n");
3781 if (!ses || !server)
3784 if (smb3_encryption_required(tcon))
3785 flags |= CIFS_TRANSFORM_REQ;
3787 memset(&rqst, 0, sizeof(struct smb_rqst));
3788 memset(&iov, 0, sizeof(iov));
3795 rc = SMB2_notify_init(xid, &rqst, tcon, server,
3796 persistent_fid, volatile_fid,
3797 completion_filter, watch_tree);
3801 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3802 (u8)watch_tree, completion_filter);
3803 rc = cifs_send_recv(xid, ses, server,
3804 &rqst, &resp_buftype, flags, &rsp_iov);
3807 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3808 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3809 (u8)watch_tree, completion_filter, rc);
3811 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3812 ses->Suid, (u8)watch_tree, completion_filter);
3813 /* validate that notify information is plausible */
3814 if ((rsp_iov.iov_base == NULL) ||
3815 (rsp_iov.iov_len < sizeof(struct smb2_change_notify_rsp) + 1))
3818 smb_rsp = (struct smb2_change_notify_rsp *)rsp_iov.iov_base;
3820 smb2_validate_iov(le16_to_cpu(smb_rsp->OutputBufferOffset),
3821 le32_to_cpu(smb_rsp->OutputBufferLength), &rsp_iov,
3822 sizeof(struct file_notify_information));
3824 *out_data = kmemdup((char *)smb_rsp + le16_to_cpu(smb_rsp->OutputBufferOffset),
3825 le32_to_cpu(smb_rsp->OutputBufferLength), GFP_KERNEL);
3826 if (*out_data == NULL) {
3830 *plen = le32_to_cpu(smb_rsp->OutputBufferLength);
3835 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3836 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3843 * This is a no-op for now. We're not really interested in the reply, but
3844 * rather in the fact that the server sent one and that server->lstrp
3847 * FIXME: maybe we should consider checking that the reply matches request?
3850 smb2_echo_callback(struct mid_q_entry *mid)
3852 struct TCP_Server_Info *server = mid->callback_data;
3853 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3854 struct cifs_credits credits = { .value = 0, .instance = 0 };
3856 if (mid->mid_state == MID_RESPONSE_RECEIVED
3857 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3858 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
3859 credits.instance = server->reconnect_instance;
3863 add_credits(server, &credits, CIFS_ECHO_OP);
3866 void smb2_reconnect_server(struct work_struct *work)
3868 struct TCP_Server_Info *server = container_of(work,
3869 struct TCP_Server_Info, reconnect.work);
3870 struct TCP_Server_Info *pserver;
3871 struct cifs_ses *ses, *ses2;
3872 struct cifs_tcon *tcon, *tcon2;
3873 struct list_head tmp_list, tmp_ses_list;
3874 bool tcon_exist = false, ses_exist = false;
3875 bool tcon_selected = false;
3877 bool resched = false;
3879 /* If server is a channel, select the primary channel */
3880 pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
3882 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3883 mutex_lock(&pserver->reconnect_mutex);
3885 INIT_LIST_HEAD(&tmp_list);
3886 INIT_LIST_HEAD(&tmp_ses_list);
3887 cifs_dbg(FYI, "Reconnecting tcons and channels\n");
3889 spin_lock(&cifs_tcp_ses_lock);
3890 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
3891 spin_lock(&ses->ses_lock);
3892 if (ses->ses_status == SES_EXITING) {
3893 spin_unlock(&ses->ses_lock);
3896 spin_unlock(&ses->ses_lock);
3898 tcon_selected = false;
3900 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3901 if (tcon->need_reconnect || tcon->need_reopen_files) {
3903 list_add_tail(&tcon->rlist, &tmp_list);
3904 tcon_selected = tcon_exist = true;
3908 * IPC has the same lifetime as its session and uses its
3911 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3912 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3913 tcon_selected = tcon_exist = true;
3917 * handle the case where channel needs to reconnect
3918 * binding session, but tcon is healthy (some other channel
3921 spin_lock(&ses->chan_lock);
3922 if (!tcon_selected && cifs_chan_needs_reconnect(ses, server)) {
3923 list_add_tail(&ses->rlist, &tmp_ses_list);
3927 spin_unlock(&ses->chan_lock);
3930 * Get the reference to server struct to be sure that the last call of
3931 * cifs_put_tcon() in the loop below won't release the server pointer.
3933 if (tcon_exist || ses_exist)
3934 server->srv_count++;
3936 spin_unlock(&cifs_tcp_ses_lock);
3938 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3939 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
3941 cifs_reopen_persistent_handles(tcon);
3944 list_del_init(&tcon->rlist);
3946 cifs_put_smb_ses(tcon->ses);
3948 cifs_put_tcon(tcon);
3954 /* allocate a dummy tcon struct used for reconnect */
3955 tcon = tconInfoAlloc();
3958 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
3959 list_del_init(&ses->rlist);
3960 cifs_put_smb_ses(ses);
3965 tcon->status = TID_GOOD;
3966 tcon->retry = false;
3967 tcon->need_reconnect = false;
3969 /* now reconnect sessions for necessary channels */
3970 list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
3972 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
3975 list_del_init(&ses->rlist);
3976 cifs_put_smb_ses(ses);
3981 cifs_dbg(FYI, "Reconnecting tcons and channels finished\n");
3983 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3984 mutex_unlock(&pserver->reconnect_mutex);
3986 /* now we can safely release srv struct */
3987 if (tcon_exist || ses_exist)
3988 cifs_put_tcp_session(server, 1);
3992 SMB2_echo(struct TCP_Server_Info *server)
3994 struct smb2_echo_req *req;
3997 struct smb_rqst rqst = { .rq_iov = iov,
3999 unsigned int total_len;
4001 cifs_dbg(FYI, "In echo request for conn_id %lld\n", server->conn_id);
4003 spin_lock(&server->srv_lock);
4004 if (server->ops->need_neg &&
4005 server->ops->need_neg(server)) {
4006 spin_unlock(&server->srv_lock);
4007 /* No need to send echo on newly established connections */
4008 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
4011 spin_unlock(&server->srv_lock);
4013 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
4014 (void **)&req, &total_len);
4018 req->hdr.CreditRequest = cpu_to_le16(1);
4020 iov[0].iov_len = total_len;
4021 iov[0].iov_base = (char *)req;
4023 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
4024 server, CIFS_ECHO_OP, NULL);
4026 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
4028 cifs_small_buf_release(req);
4033 SMB2_flush_free(struct smb_rqst *rqst)
4035 if (rqst && rqst->rq_iov)
4036 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4040 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
4041 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
4042 u64 persistent_fid, u64 volatile_fid)
4044 struct smb2_flush_req *req;
4045 struct kvec *iov = rqst->rq_iov;
4046 unsigned int total_len;
4049 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
4050 (void **) &req, &total_len);
4054 req->PersistentFileId = persistent_fid;
4055 req->VolatileFileId = volatile_fid;
4057 iov[0].iov_base = (char *)req;
4058 iov[0].iov_len = total_len;
4064 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4067 struct cifs_ses *ses = tcon->ses;
4068 struct smb_rqst rqst;
4070 struct kvec rsp_iov = {NULL, 0};
4071 struct TCP_Server_Info *server = cifs_pick_channel(ses);
4072 int resp_buftype = CIFS_NO_BUFFER;
4076 cifs_dbg(FYI, "flush\n");
4077 if (!ses || !(ses->server))
4080 if (smb3_encryption_required(tcon))
4081 flags |= CIFS_TRANSFORM_REQ;
4083 memset(&rqst, 0, sizeof(struct smb_rqst));
4084 memset(&iov, 0, sizeof(iov));
4088 rc = SMB2_flush_init(xid, &rqst, tcon, server,
4089 persistent_fid, volatile_fid);
4093 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
4094 rc = cifs_send_recv(xid, ses, server,
4095 &rqst, &resp_buftype, flags, &rsp_iov);
4098 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
4099 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
4102 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
4106 SMB2_flush_free(&rqst);
4107 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4111 #ifdef CONFIG_CIFS_SMB_DIRECT
4112 static inline bool smb3_use_rdma_offload(struct cifs_io_parms *io_parms)
4114 struct TCP_Server_Info *server = io_parms->server;
4115 struct cifs_tcon *tcon = io_parms->tcon;
4117 /* we can only offload if we're connected */
4118 if (!server || !tcon)
4121 /* we can only offload on an rdma connection */
4122 if (!server->rdma || !server->smbd_conn)
4125 /* we don't support signed offload yet */
4129 /* we don't support encrypted offload yet */
4130 if (smb3_encryption_required(tcon))
4133 /* offload also has its overhead, so only do it if desired */
4134 if (io_parms->length < server->smbd_conn->rdma_readwrite_threshold)
4139 #endif /* CONFIG_CIFS_SMB_DIRECT */
4142 * To form a chain of read requests, any read requests after the first should
4143 * have the end_of_chain boolean set to true.
4146 smb2_new_read_req(void **buf, unsigned int *total_len,
4147 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
4148 unsigned int remaining_bytes, int request_type)
4151 struct smb2_read_req *req = NULL;
4152 struct smb2_hdr *shdr;
4153 struct TCP_Server_Info *server = io_parms->server;
4155 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
4156 (void **) &req, total_len);
4161 return -ECONNABORTED;
4164 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4166 req->PersistentFileId = io_parms->persistent_fid;
4167 req->VolatileFileId = io_parms->volatile_fid;
4168 req->ReadChannelInfoOffset = 0; /* reserved */
4169 req->ReadChannelInfoLength = 0; /* reserved */
4170 req->Channel = 0; /* reserved */
4171 req->MinimumCount = 0;
4172 req->Length = cpu_to_le32(io_parms->length);
4173 req->Offset = cpu_to_le64(io_parms->offset);
4175 trace_smb3_read_enter(0 /* xid */,
4176 io_parms->persistent_fid,
4177 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4178 io_parms->offset, io_parms->length);
4179 #ifdef CONFIG_CIFS_SMB_DIRECT
4181 * If we want to do a RDMA write, fill in and append
4182 * smbd_buffer_descriptor_v1 to the end of read request
4184 if (smb3_use_rdma_offload(io_parms)) {
4185 struct smbd_buffer_descriptor_v1 *v1;
4186 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4188 rdata->mr = smbd_register_mr(
4189 server->smbd_conn, rdata->pages,
4190 rdata->nr_pages, rdata->page_offset,
4191 rdata->tailsz, true, need_invalidate);
4195 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4196 if (need_invalidate)
4197 req->Channel = SMB2_CHANNEL_RDMA_V1;
4198 req->ReadChannelInfoOffset =
4199 cpu_to_le16(offsetof(struct smb2_read_req, Buffer));
4200 req->ReadChannelInfoLength =
4201 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4202 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4203 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
4204 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
4205 v1->length = cpu_to_le32(rdata->mr->mr->length);
4207 *total_len += sizeof(*v1) - 1;
4210 if (request_type & CHAINED_REQUEST) {
4211 if (!(request_type & END_OF_CHAIN)) {
4212 /* next 8-byte aligned request */
4213 *total_len = ALIGN(*total_len, 8);
4214 shdr->NextCommand = cpu_to_le32(*total_len);
4215 } else /* END_OF_CHAIN */
4216 shdr->NextCommand = 0;
4217 if (request_type & RELATED_REQUEST) {
4218 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
4220 * Related requests use info from previous read request
4223 shdr->SessionId = cpu_to_le64(0xFFFFFFFFFFFFFFFF);
4224 shdr->Id.SyncId.TreeId = cpu_to_le32(0xFFFFFFFF);
4225 req->PersistentFileId = (u64)-1;
4226 req->VolatileFileId = (u64)-1;
4229 if (remaining_bytes > io_parms->length)
4230 req->RemainingBytes = cpu_to_le32(remaining_bytes);
4232 req->RemainingBytes = 0;
4239 smb2_readv_callback(struct mid_q_entry *mid)
4241 struct cifs_readdata *rdata = mid->callback_data;
4242 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
4243 struct TCP_Server_Info *server = rdata->server;
4244 struct smb2_hdr *shdr =
4245 (struct smb2_hdr *)rdata->iov[0].iov_base;
4246 struct cifs_credits credits = { .value = 0, .instance = 0 };
4247 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
4250 if (rdata->got_bytes) {
4251 rqst.rq_pages = rdata->pages;
4252 rqst.rq_offset = rdata->page_offset;
4253 rqst.rq_npages = rdata->nr_pages;
4254 rqst.rq_pagesz = rdata->pagesz;
4255 rqst.rq_tailsz = rdata->tailsz;
4258 WARN_ONCE(rdata->server != mid->server,
4259 "rdata server %p != mid server %p",
4260 rdata->server, mid->server);
4262 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
4263 __func__, mid->mid, mid->mid_state, rdata->result,
4266 switch (mid->mid_state) {
4267 case MID_RESPONSE_RECEIVED:
4268 credits.value = le16_to_cpu(shdr->CreditRequest);
4269 credits.instance = server->reconnect_instance;
4270 /* result already set, check signature */
4271 if (server->sign && !mid->decrypted) {
4274 rc = smb2_verify_signature(&rqst, server);
4276 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
4279 /* FIXME: should this be counted toward the initiating task? */
4280 task_io_account_read(rdata->got_bytes);
4281 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4283 case MID_REQUEST_SUBMITTED:
4284 case MID_RETRY_NEEDED:
4285 rdata->result = -EAGAIN;
4286 if (server->sign && rdata->got_bytes)
4287 /* reset bytes number since we can not check a sign */
4288 rdata->got_bytes = 0;
4289 /* FIXME: should this be counted toward the initiating task? */
4290 task_io_account_read(rdata->got_bytes);
4291 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4293 case MID_RESPONSE_MALFORMED:
4294 credits.value = le16_to_cpu(shdr->CreditRequest);
4295 credits.instance = server->reconnect_instance;
4298 rdata->result = -EIO;
4300 #ifdef CONFIG_CIFS_SMB_DIRECT
4302 * If this rdata has a memmory registered, the MR can be freed
4303 * MR needs to be freed as soon as I/O finishes to prevent deadlock
4304 * because they have limited number and are used for future I/Os
4307 smbd_deregister_mr(rdata->mr);
4311 if (rdata->result && rdata->result != -ENODATA) {
4312 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
4313 trace_smb3_read_err(0 /* xid */,
4314 rdata->cfile->fid.persistent_fid,
4315 tcon->tid, tcon->ses->Suid, rdata->offset,
4316 rdata->bytes, rdata->result);
4318 trace_smb3_read_done(0 /* xid */,
4319 rdata->cfile->fid.persistent_fid,
4320 tcon->tid, tcon->ses->Suid,
4321 rdata->offset, rdata->got_bytes);
4323 queue_work(cifsiod_wq, &rdata->work);
4325 add_credits(server, &credits, 0);
4328 /* smb2_async_readv - send an async read, and set up mid to handle result */
4330 smb2_async_readv(struct cifs_readdata *rdata)
4334 struct smb2_hdr *shdr;
4335 struct cifs_io_parms io_parms;
4336 struct smb_rqst rqst = { .rq_iov = rdata->iov,
4338 struct TCP_Server_Info *server;
4339 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
4340 unsigned int total_len;
4343 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
4344 __func__, rdata->offset, rdata->bytes);
4347 rdata->server = cifs_pick_channel(tcon->ses);
4349 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
4350 io_parms.server = server = rdata->server;
4351 io_parms.offset = rdata->offset;
4352 io_parms.length = rdata->bytes;
4353 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
4354 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
4355 io_parms.pid = rdata->pid;
4357 rc = smb2_new_read_req(
4358 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
4362 if (smb3_encryption_required(io_parms.tcon))
4363 flags |= CIFS_TRANSFORM_REQ;
4365 rdata->iov[0].iov_base = buf;
4366 rdata->iov[0].iov_len = total_len;
4368 shdr = (struct smb2_hdr *)buf;
4370 if (rdata->credits.value > 0) {
4371 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
4372 SMB2_MAX_BUFFER_SIZE));
4373 credit_request = le16_to_cpu(shdr->CreditCharge) + 8;
4374 if (server->credits >= server->max_credits)
4375 shdr->CreditRequest = cpu_to_le16(0);
4377 shdr->CreditRequest = cpu_to_le16(
4378 min_t(int, server->max_credits -
4379 server->credits, credit_request));
4381 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4383 goto async_readv_out;
4385 flags |= CIFS_HAS_CREDITS;
4388 kref_get(&rdata->refcount);
4389 rc = cifs_call_async(server, &rqst,
4390 cifs_readv_receive, smb2_readv_callback,
4391 smb3_handle_read_data, rdata, flags,
4394 kref_put(&rdata->refcount, cifs_readdata_release);
4395 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
4396 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
4398 io_parms.tcon->ses->Suid,
4399 io_parms.offset, io_parms.length, rc);
4403 cifs_small_buf_release(buf);
4408 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
4409 unsigned int *nbytes, char **buf, int *buf_type)
4411 struct smb_rqst rqst;
4412 int resp_buftype, rc;
4413 struct smb2_read_req *req = NULL;
4414 struct smb2_read_rsp *rsp = NULL;
4416 struct kvec rsp_iov;
4417 unsigned int total_len;
4418 int flags = CIFS_LOG_ERROR;
4419 struct cifs_ses *ses = io_parms->tcon->ses;
4421 if (!io_parms->server)
4422 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4425 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
4429 if (smb3_encryption_required(io_parms->tcon))
4430 flags |= CIFS_TRANSFORM_REQ;
4432 iov[0].iov_base = (char *)req;
4433 iov[0].iov_len = total_len;
4435 memset(&rqst, 0, sizeof(struct smb_rqst));
4439 rc = cifs_send_recv(xid, ses, io_parms->server,
4440 &rqst, &resp_buftype, flags, &rsp_iov);
4441 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
4444 if (rc != -ENODATA) {
4445 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4446 cifs_dbg(VFS, "Send error in read = %d\n", rc);
4447 trace_smb3_read_err(xid,
4448 req->PersistentFileId,
4449 io_parms->tcon->tid, ses->Suid,
4450 io_parms->offset, io_parms->length,
4453 trace_smb3_read_done(xid, req->PersistentFileId, io_parms->tcon->tid,
4454 ses->Suid, io_parms->offset, 0);
4455 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4456 cifs_small_buf_release(req);
4457 return rc == -ENODATA ? 0 : rc;
4459 trace_smb3_read_done(xid,
4460 req->PersistentFileId,
4461 io_parms->tcon->tid, ses->Suid,
4462 io_parms->offset, io_parms->length);
4464 cifs_small_buf_release(req);
4466 *nbytes = le32_to_cpu(rsp->DataLength);
4467 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4468 (*nbytes > io_parms->length)) {
4469 cifs_dbg(FYI, "bad length %d for count %d\n",
4470 *nbytes, io_parms->length);
4476 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
4477 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4478 } else if (resp_buftype != CIFS_NO_BUFFER) {
4479 *buf = rsp_iov.iov_base;
4480 if (resp_buftype == CIFS_SMALL_BUFFER)
4481 *buf_type = CIFS_SMALL_BUFFER;
4482 else if (resp_buftype == CIFS_LARGE_BUFFER)
4483 *buf_type = CIFS_LARGE_BUFFER;
4489 * Check the mid_state and signature on received buffer (if any), and queue the
4490 * workqueue completion task.
4493 smb2_writev_callback(struct mid_q_entry *mid)
4495 struct cifs_writedata *wdata = mid->callback_data;
4496 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4497 struct TCP_Server_Info *server = wdata->server;
4498 unsigned int written;
4499 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
4500 struct cifs_credits credits = { .value = 0, .instance = 0 };
4502 WARN_ONCE(wdata->server != mid->server,
4503 "wdata server %p != mid server %p",
4504 wdata->server, mid->server);
4506 switch (mid->mid_state) {
4507 case MID_RESPONSE_RECEIVED:
4508 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4509 credits.instance = server->reconnect_instance;
4510 wdata->result = smb2_check_receive(mid, server, 0);
4511 if (wdata->result != 0)
4514 written = le32_to_cpu(rsp->DataLength);
4516 * Mask off high 16 bits when bytes written as returned
4517 * by the server is greater than bytes requested by the
4518 * client. OS/2 servers are known to set incorrect
4521 if (written > wdata->bytes)
4524 if (written < wdata->bytes)
4525 wdata->result = -ENOSPC;
4527 wdata->bytes = written;
4529 case MID_REQUEST_SUBMITTED:
4530 case MID_RETRY_NEEDED:
4531 wdata->result = -EAGAIN;
4533 case MID_RESPONSE_MALFORMED:
4534 credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
4535 credits.instance = server->reconnect_instance;
4538 wdata->result = -EIO;
4541 #ifdef CONFIG_CIFS_SMB_DIRECT
4543 * If this wdata has a memory registered, the MR can be freed
4544 * The number of MRs available is limited, it's important to recover
4545 * used MR as soon as I/O is finished. Hold MR longer in the later
4546 * I/O process can possibly result in I/O deadlock due to lack of MR
4547 * to send request on I/O retry
4550 smbd_deregister_mr(wdata->mr);
4554 if (wdata->result) {
4555 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4556 trace_smb3_write_err(0 /* no xid */,
4557 wdata->cfile->fid.persistent_fid,
4558 tcon->tid, tcon->ses->Suid, wdata->offset,
4559 wdata->bytes, wdata->result);
4560 if (wdata->result == -ENOSPC)
4561 pr_warn_once("Out of space writing to %s\n",
4564 trace_smb3_write_done(0 /* no xid */,
4565 wdata->cfile->fid.persistent_fid,
4566 tcon->tid, tcon->ses->Suid,
4567 wdata->offset, wdata->bytes);
4569 queue_work(cifsiod_wq, &wdata->work);
4571 add_credits(server, &credits, 0);
4574 /* smb2_async_writev - send an async write, and set up mid to handle result */
4576 smb2_async_writev(struct cifs_writedata *wdata,
4577 void (*release)(struct kref *kref))
4579 int rc = -EACCES, flags = 0;
4580 struct smb2_write_req *req = NULL;
4581 struct smb2_hdr *shdr;
4582 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4583 struct TCP_Server_Info *server = wdata->server;
4585 struct smb_rqst rqst = { };
4586 unsigned int total_len;
4587 struct cifs_io_parms _io_parms;
4588 struct cifs_io_parms *io_parms = NULL;
4592 server = wdata->server = cifs_pick_channel(tcon->ses);
4595 * in future we may get cifs_io_parms passed in from the caller,
4596 * but for now we construct it here...
4598 _io_parms = (struct cifs_io_parms) {
4601 .offset = wdata->offset,
4602 .length = wdata->bytes,
4603 .persistent_fid = wdata->cfile->fid.persistent_fid,
4604 .volatile_fid = wdata->cfile->fid.volatile_fid,
4607 io_parms = &_io_parms;
4609 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
4610 (void **) &req, &total_len);
4614 if (smb3_encryption_required(tcon))
4615 flags |= CIFS_TRANSFORM_REQ;
4617 shdr = (struct smb2_hdr *)req;
4618 shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4620 req->PersistentFileId = io_parms->persistent_fid;
4621 req->VolatileFileId = io_parms->volatile_fid;
4622 req->WriteChannelInfoOffset = 0;
4623 req->WriteChannelInfoLength = 0;
4625 req->Offset = cpu_to_le64(io_parms->offset);
4626 req->DataOffset = cpu_to_le16(
4627 offsetof(struct smb2_write_req, Buffer));
4628 req->RemainingBytes = 0;
4630 trace_smb3_write_enter(0 /* xid */,
4631 io_parms->persistent_fid,
4632 io_parms->tcon->tid,
4633 io_parms->tcon->ses->Suid,
4637 #ifdef CONFIG_CIFS_SMB_DIRECT
4639 * If we want to do a server RDMA read, fill in and append
4640 * smbd_buffer_descriptor_v1 to the end of write request
4642 if (smb3_use_rdma_offload(io_parms)) {
4643 struct smbd_buffer_descriptor_v1 *v1;
4644 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4646 wdata->mr = smbd_register_mr(
4647 server->smbd_conn, wdata->pages,
4648 wdata->nr_pages, wdata->page_offset,
4649 wdata->tailsz, false, need_invalidate);
4652 goto async_writev_out;
4655 req->DataOffset = 0;
4656 if (wdata->nr_pages > 1)
4657 req->RemainingBytes =
4659 (wdata->nr_pages - 1) * wdata->pagesz -
4660 wdata->page_offset + wdata->tailsz
4663 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
4664 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4665 if (need_invalidate)
4666 req->Channel = SMB2_CHANNEL_RDMA_V1;
4667 req->WriteChannelInfoOffset =
4668 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4669 req->WriteChannelInfoLength =
4670 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4671 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4672 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4673 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4674 v1->length = cpu_to_le32(wdata->mr->mr->length);
4677 iov[0].iov_len = total_len - 1;
4678 iov[0].iov_base = (char *)req;
4682 rqst.rq_pages = wdata->pages;
4683 rqst.rq_offset = wdata->page_offset;
4684 rqst.rq_npages = wdata->nr_pages;
4685 rqst.rq_pagesz = wdata->pagesz;
4686 rqst.rq_tailsz = wdata->tailsz;
4687 #ifdef CONFIG_CIFS_SMB_DIRECT
4689 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4693 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4694 io_parms->offset, io_parms->length);
4696 #ifdef CONFIG_CIFS_SMB_DIRECT
4697 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4699 req->Length = cpu_to_le32(io_parms->length);
4701 req->Length = cpu_to_le32(io_parms->length);
4704 if (wdata->credits.value > 0) {
4705 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4706 SMB2_MAX_BUFFER_SIZE));
4707 credit_request = le16_to_cpu(shdr->CreditCharge) + 8;
4708 if (server->credits >= server->max_credits)
4709 shdr->CreditRequest = cpu_to_le16(0);
4711 shdr->CreditRequest = cpu_to_le16(
4712 min_t(int, server->max_credits -
4713 server->credits, credit_request));
4715 rc = adjust_credits(server, &wdata->credits, io_parms->length);
4717 goto async_writev_out;
4719 flags |= CIFS_HAS_CREDITS;
4722 kref_get(&wdata->refcount);
4723 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4724 wdata, flags, &wdata->credits);
4727 trace_smb3_write_err(0 /* no xid */,
4728 io_parms->persistent_fid,
4729 io_parms->tcon->tid,
4730 io_parms->tcon->ses->Suid,
4734 kref_put(&wdata->refcount, release);
4735 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4739 cifs_small_buf_release(req);
4744 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4745 * The length field from io_parms must be at least 1 and indicates a number of
4746 * elements with data to write that begins with position 1 in iov array. All
4747 * data length is specified by count.
4750 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4751 unsigned int *nbytes, struct kvec *iov, int n_vec)
4753 struct smb_rqst rqst;
4755 struct smb2_write_req *req = NULL;
4756 struct smb2_write_rsp *rsp = NULL;
4758 struct kvec rsp_iov;
4760 unsigned int total_len;
4761 struct TCP_Server_Info *server;
4768 if (!io_parms->server)
4769 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4770 server = io_parms->server;
4772 return -ECONNABORTED;
4774 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
4775 (void **) &req, &total_len);
4779 if (smb3_encryption_required(io_parms->tcon))
4780 flags |= CIFS_TRANSFORM_REQ;
4782 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
4784 req->PersistentFileId = io_parms->persistent_fid;
4785 req->VolatileFileId = io_parms->volatile_fid;
4786 req->WriteChannelInfoOffset = 0;
4787 req->WriteChannelInfoLength = 0;
4789 req->Length = cpu_to_le32(io_parms->length);
4790 req->Offset = cpu_to_le64(io_parms->offset);
4791 req->DataOffset = cpu_to_le16(
4792 offsetof(struct smb2_write_req, Buffer));
4793 req->RemainingBytes = 0;
4795 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4796 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4797 io_parms->offset, io_parms->length);
4799 iov[0].iov_base = (char *)req;
4801 iov[0].iov_len = total_len - 1;
4803 memset(&rqst, 0, sizeof(struct smb_rqst));
4805 rqst.rq_nvec = n_vec + 1;
4807 rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
4809 &resp_buftype, flags, &rsp_iov);
4810 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
4813 trace_smb3_write_err(xid,
4814 req->PersistentFileId,
4815 io_parms->tcon->tid,
4816 io_parms->tcon->ses->Suid,
4817 io_parms->offset, io_parms->length, rc);
4818 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
4819 cifs_dbg(VFS, "Send error in write = %d\n", rc);
4821 *nbytes = le32_to_cpu(rsp->DataLength);
4822 trace_smb3_write_done(xid,
4823 req->PersistentFileId,
4824 io_parms->tcon->tid,
4825 io_parms->tcon->ses->Suid,
4826 io_parms->offset, *nbytes);
4829 cifs_small_buf_release(req);
4830 free_rsp_buf(resp_buftype, rsp);
4834 int posix_info_sid_size(const void *beg, const void *end)
4842 subauth = *(u8 *)(beg+1);
4843 if (subauth < 1 || subauth > 15)
4846 total = 1 + 1 + 6 + 4*subauth;
4847 if (beg + total > end)
4853 int posix_info_parse(const void *beg, const void *end,
4854 struct smb2_posix_info_parsed *out)
4858 int owner_len, group_len;
4860 const void *owner_sid;
4861 const void *group_sid;
4864 /* if no end bound given, assume payload to be correct */
4866 const struct smb2_posix_info *p = beg;
4868 end = beg + le32_to_cpu(p->NextEntryOffset);
4869 /* last element will have a 0 offset, pick a sensible bound */
4874 /* check base buf */
4875 if (beg + sizeof(struct smb2_posix_info) > end)
4877 total_len = sizeof(struct smb2_posix_info);
4879 /* check owner sid */
4880 owner_sid = beg + total_len;
4881 owner_len = posix_info_sid_size(owner_sid, end);
4884 total_len += owner_len;
4886 /* check group sid */
4887 group_sid = beg + total_len;
4888 group_len = posix_info_sid_size(group_sid, end);
4891 total_len += group_len;
4893 /* check name len */
4894 if (beg + total_len + 4 > end)
4896 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
4897 if (name_len < 1 || name_len > 0xFFFF)
4902 name = beg + total_len;
4903 if (name + name_len > end)
4905 total_len += name_len;
4909 out->size = total_len;
4910 out->name_len = name_len;
4912 memcpy(&out->owner, owner_sid, owner_len);
4913 memcpy(&out->group, group_sid, group_len);
4918 static int posix_info_extra_size(const void *beg, const void *end)
4920 int len = posix_info_parse(beg, end, NULL);
4924 return len - sizeof(struct smb2_posix_info);
4928 num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
4932 unsigned int entrycount = 0;
4933 unsigned int next_offset = 0;
4935 FILE_DIRECTORY_INFO *dir_info;
4937 if (bufstart == NULL)
4940 entryptr = bufstart;
4943 if (entryptr + next_offset < entryptr ||
4944 entryptr + next_offset > end_of_buf ||
4945 entryptr + next_offset + size > end_of_buf) {
4946 cifs_dbg(VFS, "malformed search entry would overflow\n");
4950 entryptr = entryptr + next_offset;
4951 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4953 if (infotype == SMB_FIND_FILE_POSIX_INFO)
4954 len = posix_info_extra_size(entryptr, end_of_buf);
4956 len = le32_to_cpu(dir_info->FileNameLength);
4959 entryptr + len < entryptr ||
4960 entryptr + len > end_of_buf ||
4961 entryptr + len + size > end_of_buf) {
4962 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4967 *lastentry = entryptr;
4970 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
4981 int SMB2_query_directory_init(const unsigned int xid,
4982 struct cifs_tcon *tcon,
4983 struct TCP_Server_Info *server,
4984 struct smb_rqst *rqst,
4985 u64 persistent_fid, u64 volatile_fid,
4986 int index, int info_level)
4988 struct smb2_query_directory_req *req;
4989 unsigned char *bufptr;
4990 __le16 asteriks = cpu_to_le16('*');
4991 unsigned int output_size = CIFSMaxBufSize -
4992 MAX_SMB2_CREATE_RESPONSE_SIZE -
4993 MAX_SMB2_CLOSE_RESPONSE_SIZE;
4994 unsigned int total_len;
4995 struct kvec *iov = rqst->rq_iov;
4998 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
4999 (void **) &req, &total_len);
5003 switch (info_level) {
5004 case SMB_FIND_FILE_DIRECTORY_INFO:
5005 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
5007 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
5008 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
5010 case SMB_FIND_FILE_POSIX_INFO:
5011 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
5013 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
5014 req->FileInformationClass = FILE_FULL_DIRECTORY_INFORMATION;
5017 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
5022 req->FileIndex = cpu_to_le32(index);
5023 req->PersistentFileId = persistent_fid;
5024 req->VolatileFileId = volatile_fid;
5027 bufptr = req->Buffer;
5028 memcpy(bufptr, &asteriks, len);
5030 req->FileNameOffset =
5031 cpu_to_le16(sizeof(struct smb2_query_directory_req));
5032 req->FileNameLength = cpu_to_le16(len);
5034 * BB could be 30 bytes or so longer if we used SMB2 specific
5035 * buffer lengths, but this is safe and close enough.
5037 output_size = min_t(unsigned int, output_size, server->maxBuf);
5038 output_size = min_t(unsigned int, output_size, 2 << 15);
5039 req->OutputBufferLength = cpu_to_le32(output_size);
5041 iov[0].iov_base = (char *)req;
5043 iov[0].iov_len = total_len - 1;
5045 iov[1].iov_base = (char *)(req->Buffer);
5046 iov[1].iov_len = len;
5048 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
5049 tcon->ses->Suid, index, output_size);
5054 void SMB2_query_directory_free(struct smb_rqst *rqst)
5056 if (rqst && rqst->rq_iov) {
5057 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
5062 smb2_parse_query_directory(struct cifs_tcon *tcon,
5063 struct kvec *rsp_iov,
5065 struct cifs_search_info *srch_inf)
5067 struct smb2_query_directory_rsp *rsp;
5068 size_t info_buf_size;
5072 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
5074 switch (srch_inf->info_level) {
5075 case SMB_FIND_FILE_DIRECTORY_INFO:
5076 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
5078 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
5079 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
5081 case SMB_FIND_FILE_POSIX_INFO:
5082 /* note that posix payload are variable size */
5083 info_buf_size = sizeof(struct smb2_posix_info);
5085 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
5086 info_buf_size = sizeof(FILE_FULL_DIRECTORY_INFO);
5089 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
5090 srch_inf->info_level);
5094 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5095 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
5098 cifs_tcon_dbg(VFS, "bad info payload");
5102 srch_inf->unicode = true;
5104 if (srch_inf->ntwrk_buf_start) {
5105 if (srch_inf->smallBuf)
5106 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
5108 cifs_buf_release(srch_inf->ntwrk_buf_start);
5110 srch_inf->ntwrk_buf_start = (char *)rsp;
5111 srch_inf->srch_entries_start = srch_inf->last_entry =
5112 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
5113 end_of_smb = rsp_iov->iov_len + (char *)rsp;
5115 srch_inf->entries_in_buffer = num_entries(
5116 srch_inf->info_level,
5117 srch_inf->srch_entries_start,
5119 &srch_inf->last_entry,
5122 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
5123 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
5124 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
5125 srch_inf->srch_entries_start, srch_inf->last_entry);
5126 if (resp_buftype == CIFS_LARGE_BUFFER)
5127 srch_inf->smallBuf = false;
5128 else if (resp_buftype == CIFS_SMALL_BUFFER)
5129 srch_inf->smallBuf = true;
5131 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
5137 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
5138 u64 persistent_fid, u64 volatile_fid, int index,
5139 struct cifs_search_info *srch_inf)
5141 struct smb_rqst rqst;
5142 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
5143 struct smb2_query_directory_rsp *rsp = NULL;
5144 int resp_buftype = CIFS_NO_BUFFER;
5145 struct kvec rsp_iov;
5147 struct cifs_ses *ses = tcon->ses;
5148 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5151 if (!ses || !(ses->server))
5154 if (smb3_encryption_required(tcon))
5155 flags |= CIFS_TRANSFORM_REQ;
5157 memset(&rqst, 0, sizeof(struct smb_rqst));
5158 memset(&iov, 0, sizeof(iov));
5160 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
5162 rc = SMB2_query_directory_init(xid, tcon, server,
5163 &rqst, persistent_fid,
5164 volatile_fid, index,
5165 srch_inf->info_level);
5169 rc = cifs_send_recv(xid, ses, server,
5170 &rqst, &resp_buftype, flags, &rsp_iov);
5171 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
5174 if (rc == -ENODATA &&
5175 rsp->hdr.Status == STATUS_NO_MORE_FILES) {
5176 trace_smb3_query_dir_done(xid, persistent_fid,
5177 tcon->tid, tcon->ses->Suid, index, 0);
5178 srch_inf->endOfSearch = true;
5181 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5182 tcon->ses->Suid, index, 0, rc);
5183 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
5188 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
5191 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
5192 tcon->ses->Suid, index, 0, rc);
5195 resp_buftype = CIFS_NO_BUFFER;
5197 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
5198 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
5201 SMB2_query_directory_free(&rqst);
5202 free_rsp_buf(resp_buftype, rsp);
5207 SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
5208 struct smb_rqst *rqst,
5209 u64 persistent_fid, u64 volatile_fid, u32 pid,
5210 u8 info_class, u8 info_type, u32 additional_info,
5211 void **data, unsigned int *size)
5213 struct smb2_set_info_req *req;
5214 struct kvec *iov = rqst->rq_iov;
5215 unsigned int i, total_len;
5218 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
5219 (void **) &req, &total_len);
5223 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
5224 req->InfoType = info_type;
5225 req->FileInfoClass = info_class;
5226 req->PersistentFileId = persistent_fid;
5227 req->VolatileFileId = volatile_fid;
5228 req->AdditionalInformation = cpu_to_le32(additional_info);
5230 req->BufferOffset = cpu_to_le16(sizeof(struct smb2_set_info_req));
5231 req->BufferLength = cpu_to_le32(*size);
5233 memcpy(req->Buffer, *data, *size);
5236 iov[0].iov_base = (char *)req;
5238 iov[0].iov_len = total_len - 1;
5240 for (i = 1; i < rqst->rq_nvec; i++) {
5241 le32_add_cpu(&req->BufferLength, size[i]);
5242 iov[i].iov_base = (char *)data[i];
5243 iov[i].iov_len = size[i];
5250 SMB2_set_info_free(struct smb_rqst *rqst)
5252 if (rqst && rqst->rq_iov)
5253 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
5257 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
5258 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
5259 u8 info_type, u32 additional_info, unsigned int num,
5260 void **data, unsigned int *size)
5262 struct smb_rqst rqst;
5263 struct smb2_set_info_rsp *rsp = NULL;
5265 struct kvec rsp_iov;
5268 struct cifs_ses *ses = tcon->ses;
5269 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5272 if (!ses || !server)
5278 if (smb3_encryption_required(tcon))
5279 flags |= CIFS_TRANSFORM_REQ;
5281 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
5285 memset(&rqst, 0, sizeof(struct smb_rqst));
5289 rc = SMB2_set_info_init(tcon, server,
5290 &rqst, persistent_fid, volatile_fid, pid,
5291 info_class, info_type, additional_info,
5299 rc = cifs_send_recv(xid, ses, server,
5300 &rqst, &resp_buftype, flags,
5302 SMB2_set_info_free(&rqst);
5303 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
5306 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
5307 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
5308 ses->Suid, info_class, (__u32)info_type, rc);
5311 free_rsp_buf(resp_buftype, rsp);
5317 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
5318 u64 volatile_fid, u32 pid, __le64 *eof)
5320 struct smb2_file_eof_info info;
5324 info.EndOfFile = *eof;
5327 size = sizeof(struct smb2_file_eof_info);
5329 trace_smb3_set_eof(xid, persistent_fid, tcon->tid, tcon->ses->Suid, le64_to_cpu(*eof));
5331 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5332 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
5333 0, 1, &data, &size);
5337 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
5338 u64 persistent_fid, u64 volatile_fid,
5339 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
5341 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5342 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
5343 1, (void **)&pnntsd, &pacllen);
5347 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
5348 u64 persistent_fid, u64 volatile_fid,
5349 struct smb2_file_full_ea_info *buf, int len)
5351 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5352 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
5353 0, 1, (void **)&buf, &len);
5357 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
5358 const u64 persistent_fid, const u64 volatile_fid,
5361 struct smb_rqst rqst;
5363 struct smb2_oplock_break *req = NULL;
5364 struct cifs_ses *ses = tcon->ses;
5365 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5366 int flags = CIFS_OBREAK_OP;
5367 unsigned int total_len;
5369 struct kvec rsp_iov;
5372 cifs_dbg(FYI, "SMB2_oplock_break\n");
5373 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5374 (void **) &req, &total_len);
5378 if (smb3_encryption_required(tcon))
5379 flags |= CIFS_TRANSFORM_REQ;
5381 req->VolatileFid = volatile_fid;
5382 req->PersistentFid = persistent_fid;
5383 req->OplockLevel = oplock_level;
5384 req->hdr.CreditRequest = cpu_to_le16(1);
5386 flags |= CIFS_NO_RSP_BUF;
5388 iov[0].iov_base = (char *)req;
5389 iov[0].iov_len = total_len;
5391 memset(&rqst, 0, sizeof(struct smb_rqst));
5395 rc = cifs_send_recv(xid, ses, server,
5396 &rqst, &resp_buf_type, flags, &rsp_iov);
5397 cifs_small_buf_release(req);
5400 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5401 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
5408 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
5409 struct kstatfs *kst)
5411 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
5412 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
5413 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
5414 kst->f_bfree = kst->f_bavail =
5415 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
5420 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
5421 struct kstatfs *kst)
5423 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
5424 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
5425 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
5426 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
5427 kst->f_bavail = kst->f_bfree;
5429 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
5430 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5431 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
5432 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5433 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
5439 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
5440 struct TCP_Server_Info *server,
5441 int level, int outbuf_len, u64 persistent_fid,
5445 struct smb2_query_info_req *req;
5446 unsigned int total_len;
5448 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
5450 if ((tcon->ses == NULL) || server == NULL)
5453 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
5454 (void **) &req, &total_len);
5458 req->InfoType = SMB2_O_INFO_FILESYSTEM;
5459 req->FileInfoClass = level;
5460 req->PersistentFileId = persistent_fid;
5461 req->VolatileFileId = volatile_fid;
5463 req->InputBufferOffset =
5464 cpu_to_le16(sizeof(struct smb2_query_info_req));
5465 req->OutputBufferLength = cpu_to_le32(
5466 outbuf_len + sizeof(struct smb2_query_info_rsp));
5468 iov->iov_base = (char *)req;
5469 iov->iov_len = total_len;
5473 static inline void free_qfs_info_req(struct kvec *iov)
5475 cifs_buf_release(iov->iov_base);
5479 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
5480 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5482 struct smb_rqst rqst;
5483 struct smb2_query_info_rsp *rsp = NULL;
5485 struct kvec rsp_iov;
5488 struct cifs_ses *ses = tcon->ses;
5489 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5490 FILE_SYSTEM_POSIX_INFO *info = NULL;
5493 rc = build_qfs_info_req(&iov, tcon, server,
5494 FS_POSIX_INFORMATION,
5495 sizeof(FILE_SYSTEM_POSIX_INFO),
5496 persistent_fid, volatile_fid);
5500 if (smb3_encryption_required(tcon))
5501 flags |= CIFS_TRANSFORM_REQ;
5503 memset(&rqst, 0, sizeof(struct smb_rqst));
5507 rc = cifs_send_recv(xid, ses, server,
5508 &rqst, &resp_buftype, flags, &rsp_iov);
5509 free_qfs_info_req(&iov);
5511 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5512 goto posix_qfsinf_exit;
5514 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5516 info = (FILE_SYSTEM_POSIX_INFO *)(
5517 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5518 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5519 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5520 sizeof(FILE_SYSTEM_POSIX_INFO));
5522 copy_posix_fs_info_to_kstatfs(info, fsdata);
5525 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5530 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5531 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5533 struct smb_rqst rqst;
5534 struct smb2_query_info_rsp *rsp = NULL;
5536 struct kvec rsp_iov;
5539 struct cifs_ses *ses = tcon->ses;
5540 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5541 struct smb2_fs_full_size_info *info = NULL;
5544 rc = build_qfs_info_req(&iov, tcon, server,
5545 FS_FULL_SIZE_INFORMATION,
5546 sizeof(struct smb2_fs_full_size_info),
5547 persistent_fid, volatile_fid);
5551 if (smb3_encryption_required(tcon))
5552 flags |= CIFS_TRANSFORM_REQ;
5554 memset(&rqst, 0, sizeof(struct smb_rqst));
5558 rc = cifs_send_recv(xid, ses, server,
5559 &rqst, &resp_buftype, flags, &rsp_iov);
5560 free_qfs_info_req(&iov);
5562 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5565 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5567 info = (struct smb2_fs_full_size_info *)(
5568 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5569 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5570 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5571 sizeof(struct smb2_fs_full_size_info));
5573 smb2_copy_fs_info_to_kstatfs(info, fsdata);
5576 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5581 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
5582 u64 persistent_fid, u64 volatile_fid, int level)
5584 struct smb_rqst rqst;
5585 struct smb2_query_info_rsp *rsp = NULL;
5587 struct kvec rsp_iov;
5589 int resp_buftype, max_len, min_len;
5590 struct cifs_ses *ses = tcon->ses;
5591 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5592 unsigned int rsp_len, offset;
5595 if (level == FS_DEVICE_INFORMATION) {
5596 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5597 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5598 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5599 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5600 min_len = MIN_FS_ATTR_INFO_SIZE;
5601 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5602 max_len = sizeof(struct smb3_fs_ss_info);
5603 min_len = sizeof(struct smb3_fs_ss_info);
5604 } else if (level == FS_VOLUME_INFORMATION) {
5605 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5606 min_len = sizeof(struct smb3_fs_vol_info);
5608 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
5612 rc = build_qfs_info_req(&iov, tcon, server,
5614 persistent_fid, volatile_fid);
5618 if (smb3_encryption_required(tcon))
5619 flags |= CIFS_TRANSFORM_REQ;
5621 memset(&rqst, 0, sizeof(struct smb_rqst));
5625 rc = cifs_send_recv(xid, ses, server,
5626 &rqst, &resp_buftype, flags, &rsp_iov);
5627 free_qfs_info_req(&iov);
5629 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5632 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5634 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5635 offset = le16_to_cpu(rsp->OutputBufferOffset);
5636 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
5640 if (level == FS_ATTRIBUTE_INFORMATION)
5641 memcpy(&tcon->fsAttrInfo, offset
5642 + (char *)rsp, min_t(unsigned int,
5644 else if (level == FS_DEVICE_INFORMATION)
5645 memcpy(&tcon->fsDevInfo, offset
5646 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
5647 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5648 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
5649 (offset + (char *)rsp);
5650 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5651 tcon->perf_sector_size =
5652 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
5653 } else if (level == FS_VOLUME_INFORMATION) {
5654 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5655 (offset + (char *)rsp);
5656 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5657 tcon->vol_create_time = vol_info->VolumeCreationTime;
5661 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5666 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5667 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5668 const __u32 num_lock, struct smb2_lock_element *buf)
5670 struct smb_rqst rqst;
5672 struct smb2_lock_req *req = NULL;
5674 struct kvec rsp_iov;
5677 int flags = CIFS_NO_RSP_BUF;
5678 unsigned int total_len;
5679 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5681 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
5683 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
5684 (void **) &req, &total_len);
5688 if (smb3_encryption_required(tcon))
5689 flags |= CIFS_TRANSFORM_REQ;
5691 req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
5692 req->LockCount = cpu_to_le16(num_lock);
5694 req->PersistentFileId = persist_fid;
5695 req->VolatileFileId = volatile_fid;
5697 count = num_lock * sizeof(struct smb2_lock_element);
5699 iov[0].iov_base = (char *)req;
5700 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
5701 iov[1].iov_base = (char *)buf;
5702 iov[1].iov_len = count;
5704 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
5706 memset(&rqst, 0, sizeof(struct smb_rqst));
5710 rc = cifs_send_recv(xid, tcon->ses, server,
5711 &rqst, &resp_buf_type, flags,
5713 cifs_small_buf_release(req);
5715 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
5716 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
5717 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
5718 tcon->ses->Suid, rc);
5725 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
5726 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5727 const __u64 length, const __u64 offset, const __u32 lock_flags,
5730 struct smb2_lock_element lock;
5732 lock.Offset = cpu_to_le64(offset);
5733 lock.Length = cpu_to_le64(length);
5734 lock.Flags = cpu_to_le32(lock_flags);
5735 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5736 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5738 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5742 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5743 __u8 *lease_key, const __le32 lease_state)
5745 struct smb_rqst rqst;
5747 struct smb2_lease_ack *req = NULL;
5748 struct cifs_ses *ses = tcon->ses;
5749 int flags = CIFS_OBREAK_OP;
5750 unsigned int total_len;
5752 struct kvec rsp_iov;
5754 __u64 *please_key_high;
5755 __u64 *please_key_low;
5756 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5758 cifs_dbg(FYI, "SMB2_lease_break\n");
5759 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5760 (void **) &req, &total_len);
5764 if (smb3_encryption_required(tcon))
5765 flags |= CIFS_TRANSFORM_REQ;
5767 req->hdr.CreditRequest = cpu_to_le16(1);
5768 req->StructureSize = cpu_to_le16(36);
5771 memcpy(req->LeaseKey, lease_key, 16);
5772 req->LeaseState = lease_state;
5774 flags |= CIFS_NO_RSP_BUF;
5776 iov[0].iov_base = (char *)req;
5777 iov[0].iov_len = total_len;
5779 memset(&rqst, 0, sizeof(struct smb_rqst));
5783 rc = cifs_send_recv(xid, ses, server,
5784 &rqst, &resp_buf_type, flags, &rsp_iov);
5785 cifs_small_buf_release(req);
5787 please_key_low = (__u64 *)lease_key;
5788 please_key_high = (__u64 *)(lease_key+8);
5790 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5791 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5792 ses->Suid, *please_key_low, *please_key_high, rc);
5793 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
5795 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5796 ses->Suid, *please_key_low, *please_key_high);