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>
29 #include "cifsproto.h"
30 #include "smb2proto.h"
31 #include "cifs_unicode.h"
32 #include "cifs_debug.h"
34 #include "smb2status.h"
37 #include "cifs_spnego.h"
38 #include "smbdirect.h"
40 #ifdef CONFIG_CIFS_DFS_UPCALL
41 #include "dfs_cache.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_sync_hdr *shdr, __le16 smb2_cmd,
88 const struct cifs_tcon *tcon,
89 struct TCP_Server_Info *server)
91 shdr->ProtocolId = SMB2_PROTO_NUMBER;
92 shdr->StructureSize = cpu_to_le16(64);
93 shdr->Command = smb2_cmd;
95 spin_lock(&server->req_lock);
96 /* Request up to 10 credits but don't go over the limit. */
97 if (server->credits >= server->max_credits)
98 shdr->CreditRequest = cpu_to_le16(0);
100 shdr->CreditRequest = cpu_to_le16(
101 min_t(int, server->max_credits -
102 server->credits, 10));
103 spin_unlock(&server->req_lock);
105 shdr->CreditRequest = cpu_to_le16(2);
107 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
112 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
113 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
114 if (server && (server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
115 shdr->CreditCharge = cpu_to_le16(1);
116 /* else CreditCharge MBZ */
118 shdr->TreeId = tcon->tid;
119 /* Uid is not converted */
121 shdr->SessionId = tcon->ses->Suid;
124 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
125 * to pass the path on the Open SMB prefixed by \\server\share.
126 * Not sure when we would need to do the augmented path (if ever) and
127 * setting this flag breaks the SMB2 open operation since it is
128 * illegal to send an empty path name (without \\server\share prefix)
129 * when the DFS flag is set in the SMB open header. We could
130 * consider setting the flag on all operations other than open
131 * but it is safer to net set it for now.
133 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
134 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
136 if (server && server->sign && !smb3_encryption_required(tcon))
137 shdr->Flags |= SMB2_FLAGS_SIGNED;
143 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
144 struct TCP_Server_Info *server)
147 struct nls_table *nls_codepage;
148 struct cifs_ses *ses;
152 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
153 * check for tcp and smb session status done differently
154 * for those three - in the calling routine.
159 if (smb2_command == SMB2_TREE_CONNECT)
162 if (tcon->tidStatus == CifsExiting) {
164 * only tree disconnect, open, and write,
165 * (and ulogoff which does not have tcon)
166 * are allowed as we start force umount.
168 if ((smb2_command != SMB2_WRITE) &&
169 (smb2_command != SMB2_CREATE) &&
170 (smb2_command != SMB2_TREE_DISCONNECT)) {
171 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
176 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
177 (!tcon->ses->server) || !server)
181 retries = server->nr_targets;
184 * Give demultiplex thread up to 10 seconds to each target available for
185 * reconnect -- should be greater than cifs socket timeout which is 7
188 while (server->tcpStatus == CifsNeedReconnect) {
190 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
191 * here since they are implicitly done when session drops.
193 switch (smb2_command) {
195 * BB Should we keep oplock break and add flush to exceptions?
197 case SMB2_TREE_DISCONNECT:
200 case SMB2_OPLOCK_BREAK:
204 rc = wait_event_interruptible_timeout(server->response_q,
205 (server->tcpStatus != CifsNeedReconnect),
208 cifs_dbg(FYI, "%s: aborting reconnect due to a received signal by the process\n",
213 /* are we still trying to reconnect? */
214 if (server->tcpStatus != CifsNeedReconnect)
217 if (retries && --retries)
221 * on "soft" mounts we wait once. Hard mounts keep
222 * retrying until process is killed or server comes
226 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
229 retries = server->nr_targets;
232 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
235 nls_codepage = load_nls_default();
238 * need to prevent multiple threads trying to simultaneously reconnect
239 * the same SMB session
241 mutex_lock(&tcon->ses->session_mutex);
244 * Recheck after acquire mutex. If another thread is negotiating
245 * and the server never sends an answer the socket will be closed
246 * and tcpStatus set to reconnect.
248 if (server->tcpStatus == CifsNeedReconnect) {
250 mutex_unlock(&tcon->ses->session_mutex);
255 * If we are reconnecting an extra channel, bind
257 if (server->is_channel) {
259 ses->binding_chan = cifs_ses_find_chan(ses, server);
262 rc = cifs_negotiate_protocol(0, tcon->ses);
263 if (!rc && tcon->ses->need_reconnect) {
264 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
265 if ((rc == -EACCES) && !tcon->retry) {
267 ses->binding = false;
268 ses->binding_chan = NULL;
269 mutex_unlock(&tcon->ses->session_mutex);
274 * End of channel binding
276 ses->binding = false;
277 ses->binding_chan = NULL;
279 if (rc || !tcon->need_reconnect) {
280 mutex_unlock(&tcon->ses->session_mutex);
284 cifs_mark_open_files_invalid(tcon);
285 if (tcon->use_persistent)
286 tcon->need_reopen_files = true;
288 rc = cifs_tree_connect(0, tcon, nls_codepage);
289 mutex_unlock(&tcon->ses->session_mutex);
291 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
293 /* If sess reconnected but tcon didn't, something strange ... */
294 pr_warn_once("reconnect tcon failed rc = %d\n", rc);
298 if (smb2_command != SMB2_INTERNAL_CMD)
299 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
301 atomic_inc(&tconInfoReconnectCount);
304 * Check if handle based operation so we know whether we can continue
305 * or not without returning to caller to reset file handle.
308 * BB Is flush done by server on drop of tcp session? Should we special
309 * case it and skip above?
311 switch (smb2_command) {
317 case SMB2_QUERY_DIRECTORY:
318 case SMB2_CHANGE_NOTIFY:
319 case SMB2_QUERY_INFO:
324 unload_nls(nls_codepage);
329 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon,
330 struct TCP_Server_Info *server,
332 unsigned int *total_len)
334 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
335 /* lookup word count ie StructureSize from table */
336 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
339 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
340 * largest operations (Create)
344 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon, server);
345 spdu->StructureSize2 = cpu_to_le16(parmsize);
347 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
351 * Allocate and return pointer to an SMB request hdr, and set basic
352 * SMB information in the SMB header. If the return code is zero, this
353 * function must have filled in request_buf pointer.
355 static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
356 struct TCP_Server_Info *server,
357 void **request_buf, unsigned int *total_len)
359 /* BB eventually switch this to SMB2 specific small buf size */
360 if (smb2_command == SMB2_SET_INFO)
361 *request_buf = cifs_buf_get();
363 *request_buf = cifs_small_buf_get();
364 if (*request_buf == NULL) {
365 /* BB should we add a retry in here if not a writepage? */
369 fill_small_buf(smb2_command, tcon, server,
370 (struct smb2_sync_hdr *)(*request_buf),
374 uint16_t com_code = le16_to_cpu(smb2_command);
375 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
376 cifs_stats_inc(&tcon->num_smbs_sent);
382 static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
383 struct TCP_Server_Info *server,
384 void **request_buf, unsigned int *total_len)
388 rc = smb2_reconnect(smb2_command, tcon, server);
392 return __smb2_plain_req_init(smb2_command, tcon, server, request_buf,
396 static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
397 struct TCP_Server_Info *server,
398 void **request_buf, unsigned int *total_len)
400 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
401 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
402 return __smb2_plain_req_init(SMB2_IOCTL, tcon, server,
403 request_buf, total_len);
405 return smb2_plain_req_init(SMB2_IOCTL, tcon, server,
406 request_buf, total_len);
409 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
412 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
414 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
415 pneg_ctxt->DataLength = cpu_to_le16(38);
416 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
417 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_LINUX_CLIENT_SALT_SIZE);
418 get_random_bytes(pneg_ctxt->Salt, SMB311_LINUX_CLIENT_SALT_SIZE);
419 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
423 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
425 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
426 pneg_ctxt->DataLength =
427 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
428 - sizeof(struct smb2_neg_context));
429 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
430 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
431 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
432 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
436 build_signing_ctxt(struct smb2_signing_capabilities *pneg_ctxt)
438 unsigned int ctxt_len = sizeof(struct smb2_signing_capabilities);
439 unsigned short num_algs = 1; /* number of signing algorithms sent */
441 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
443 * Context Data length must be rounded to multiple of 8 for some servers
445 pneg_ctxt->DataLength = cpu_to_le16(DIV_ROUND_UP(
446 sizeof(struct smb2_signing_capabilities) -
447 sizeof(struct smb2_neg_context) +
448 (num_algs * 2 /* sizeof u16 */), 8) * 8);
449 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(num_algs);
450 pneg_ctxt->SigningAlgorithms[0] = cpu_to_le16(SIGNING_ALG_AES_CMAC);
452 ctxt_len += 2 /* sizeof le16 */ * num_algs;
453 ctxt_len = DIV_ROUND_UP(ctxt_len, 8) * 8;
455 /* TBD add SIGNING_ALG_AES_GMAC and/or SIGNING_ALG_HMAC_SHA256 */
459 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
461 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
462 if (require_gcm_256) {
463 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + 1 cipher */
464 pneg_ctxt->CipherCount = cpu_to_le16(1);
465 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES256_GCM;
466 } else if (enable_gcm_256) {
467 pneg_ctxt->DataLength = cpu_to_le16(8); /* Cipher Count + 3 ciphers */
468 pneg_ctxt->CipherCount = cpu_to_le16(3);
469 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
470 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES256_GCM;
471 pneg_ctxt->Ciphers[2] = SMB2_ENCRYPTION_AES128_CCM;
473 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + 2 ciphers */
474 pneg_ctxt->CipherCount = cpu_to_le16(2);
475 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
476 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
481 build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
483 struct nls_table *cp = load_nls_default();
485 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
487 /* copy up to max of first 100 bytes of server name to NetName field */
488 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
489 /* context size is DataLength + minimal smb2_neg_context */
490 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
491 sizeof(struct smb2_neg_context), 8) * 8;
495 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
497 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
498 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
499 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
500 pneg_ctxt->Name[0] = 0x93;
501 pneg_ctxt->Name[1] = 0xAD;
502 pneg_ctxt->Name[2] = 0x25;
503 pneg_ctxt->Name[3] = 0x50;
504 pneg_ctxt->Name[4] = 0x9C;
505 pneg_ctxt->Name[5] = 0xB4;
506 pneg_ctxt->Name[6] = 0x11;
507 pneg_ctxt->Name[7] = 0xE7;
508 pneg_ctxt->Name[8] = 0xB4;
509 pneg_ctxt->Name[9] = 0x23;
510 pneg_ctxt->Name[10] = 0x83;
511 pneg_ctxt->Name[11] = 0xDE;
512 pneg_ctxt->Name[12] = 0x96;
513 pneg_ctxt->Name[13] = 0x8B;
514 pneg_ctxt->Name[14] = 0xCD;
515 pneg_ctxt->Name[15] = 0x7C;
519 assemble_neg_contexts(struct smb2_negotiate_req *req,
520 struct TCP_Server_Info *server, unsigned int *total_len)
523 unsigned int ctxt_len, neg_context_count;
525 if (*total_len > 200) {
526 /* In case length corrupted don't want to overrun smb buffer */
527 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
532 * round up total_len of fixed part of SMB3 negotiate request to 8
533 * byte boundary before adding negotiate contexts
535 *total_len = roundup(*total_len, 8);
537 pneg_ctxt = (*total_len) + (char *)req;
538 req->NegotiateContextOffset = cpu_to_le32(*total_len);
540 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
541 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
542 *total_len += ctxt_len;
543 pneg_ctxt += ctxt_len;
545 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
546 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
547 *total_len += ctxt_len;
548 pneg_ctxt += ctxt_len;
550 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
552 *total_len += ctxt_len;
553 pneg_ctxt += ctxt_len;
555 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
556 *total_len += sizeof(struct smb2_posix_neg_context);
557 pneg_ctxt += sizeof(struct smb2_posix_neg_context);
559 neg_context_count = 4;
561 if (server->compress_algorithm) {
562 build_compression_ctxt((struct smb2_compression_capabilities_context *)
564 ctxt_len = DIV_ROUND_UP(
565 sizeof(struct smb2_compression_capabilities_context),
567 *total_len += ctxt_len;
568 pneg_ctxt += ctxt_len;
572 if (enable_negotiate_signing) {
573 ctxt_len = build_signing_ctxt((struct smb2_signing_capabilities *)
575 *total_len += ctxt_len;
576 pneg_ctxt += ctxt_len;
580 /* check for and add transport_capabilities and signing capabilities */
581 req->NegotiateContextCount = cpu_to_le16(neg_context_count);
585 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
587 unsigned int len = le16_to_cpu(ctxt->DataLength);
589 /* If invalid preauth context warn but use what we requested, SHA-512 */
590 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
591 pr_warn_once("server sent bad preauth context\n");
593 } else if (len < MIN_PREAUTH_CTXT_DATA_LEN + le16_to_cpu(ctxt->SaltLength)) {
594 pr_warn_once("server sent invalid SaltLength\n");
597 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
598 pr_warn_once("Invalid SMB3 hash algorithm count\n");
599 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
600 pr_warn_once("unknown SMB3 hash algorithm\n");
603 static void decode_compress_ctx(struct TCP_Server_Info *server,
604 struct smb2_compression_capabilities_context *ctxt)
606 unsigned int len = le16_to_cpu(ctxt->DataLength);
608 /* sizeof compress context is a one element compression capbility struct */
610 pr_warn_once("server sent bad compression cntxt\n");
613 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
614 pr_warn_once("Invalid SMB3 compress algorithm count\n");
617 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
618 pr_warn_once("unknown compression algorithm\n");
621 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
624 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
625 struct smb2_encryption_neg_context *ctxt)
627 unsigned int len = le16_to_cpu(ctxt->DataLength);
629 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
630 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
631 pr_warn_once("server sent bad crypto ctxt len\n");
635 if (le16_to_cpu(ctxt->CipherCount) != 1) {
636 pr_warn_once("Invalid SMB3.11 cipher count\n");
639 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
640 if (require_gcm_256) {
641 if (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM) {
642 cifs_dbg(VFS, "Server does not support requested encryption type (AES256 GCM)\n");
645 } else if (ctxt->Ciphers[0] == 0) {
647 * e.g. if server only supported AES256_CCM (very unlikely)
648 * or server supported no encryption types or had all disabled.
649 * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case
650 * in which mount requested encryption ("seal") checks later
651 * on during tree connection will return proper rc, but if
652 * seal not requested by client, since server is allowed to
653 * return 0 to indicate no supported cipher, we can't fail here
655 server->cipher_type = 0;
656 server->capabilities &= ~SMB2_GLOBAL_CAP_ENCRYPTION;
657 pr_warn_once("Server does not support requested encryption types\n");
659 } else if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
660 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM) &&
661 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM)) {
662 /* server returned a cipher we didn't ask for */
663 pr_warn_once("Invalid SMB3.11 cipher returned\n");
666 server->cipher_type = ctxt->Ciphers[0];
667 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
671 static void decode_signing_ctx(struct TCP_Server_Info *server,
672 struct smb2_signing_capabilities *pctxt)
674 unsigned int len = le16_to_cpu(pctxt->DataLength);
676 if ((len < 4) || (len > 16)) {
677 pr_warn_once("server sent bad signing negcontext\n");
680 if (le16_to_cpu(pctxt->SigningAlgorithmCount) != 1) {
681 pr_warn_once("Invalid signing algorithm count\n");
684 if (le16_to_cpu(pctxt->SigningAlgorithms[0]) > 2) {
685 pr_warn_once("unknown signing algorithm\n");
689 server->signing_negotiated = true;
690 server->signing_algorithm = le16_to_cpu(pctxt->SigningAlgorithms[0]);
691 cifs_dbg(FYI, "signing algorithm %d chosen\n",
692 server->signing_algorithm);
696 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
697 struct TCP_Server_Info *server,
698 unsigned int len_of_smb)
700 struct smb2_neg_context *pctx;
701 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
702 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
703 unsigned int len_of_ctxts, i;
706 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
707 if (len_of_smb <= offset) {
708 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
712 len_of_ctxts = len_of_smb - offset;
714 for (i = 0; i < ctxt_cnt; i++) {
716 /* check that offset is not beyond end of SMB */
717 if (len_of_ctxts == 0)
720 if (len_of_ctxts < sizeof(struct smb2_neg_context))
723 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
724 clen = le16_to_cpu(pctx->DataLength);
725 if (clen > len_of_ctxts)
728 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
729 decode_preauth_context(
730 (struct smb2_preauth_neg_context *)pctx);
731 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
732 rc = decode_encrypt_ctx(server,
733 (struct smb2_encryption_neg_context *)pctx);
734 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
735 decode_compress_ctx(server,
736 (struct smb2_compression_capabilities_context *)pctx);
737 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
738 server->posix_ext_supported = true;
739 else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES)
740 decode_signing_ctx(server,
741 (struct smb2_signing_capabilities *)pctx);
743 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
744 le16_to_cpu(pctx->ContextType));
748 /* offsets must be 8 byte aligned */
749 clen = (clen + 7) & ~0x7;
750 offset += clen + sizeof(struct smb2_neg_context);
751 len_of_ctxts -= clen;
756 static struct create_posix *
757 create_posix_buf(umode_t mode)
759 struct create_posix *buf;
761 buf = kzalloc(sizeof(struct create_posix),
766 buf->ccontext.DataOffset =
767 cpu_to_le16(offsetof(struct create_posix, Mode));
768 buf->ccontext.DataLength = cpu_to_le32(4);
769 buf->ccontext.NameOffset =
770 cpu_to_le16(offsetof(struct create_posix, Name));
771 buf->ccontext.NameLength = cpu_to_le16(16);
773 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
784 buf->Name[10] = 0x83;
785 buf->Name[11] = 0xDE;
786 buf->Name[12] = 0x96;
787 buf->Name[13] = 0x8B;
788 buf->Name[14] = 0xCD;
789 buf->Name[15] = 0x7C;
790 buf->Mode = cpu_to_le32(mode);
791 cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
796 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
798 struct smb2_create_req *req = iov[0].iov_base;
799 unsigned int num = *num_iovec;
801 iov[num].iov_base = create_posix_buf(mode);
802 if (mode == ACL_NO_MODE)
803 cifs_dbg(FYI, "Invalid mode\n");
804 if (iov[num].iov_base == NULL)
806 iov[num].iov_len = sizeof(struct create_posix);
807 if (!req->CreateContextsOffset)
808 req->CreateContextsOffset = cpu_to_le32(
809 sizeof(struct smb2_create_req) +
810 iov[num - 1].iov_len);
811 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
812 *num_iovec = num + 1;
819 * SMB2 Worker functions follow:
821 * The general structure of the worker functions is:
822 * 1) Call smb2_init (assembles SMB2 header)
823 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
824 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
825 * 4) Decode SMB2 command specific fields in the fixed length area
826 * 5) Decode variable length data area (if any for this SMB2 command type)
827 * 6) Call free smb buffer
833 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
835 struct smb_rqst rqst;
836 struct smb2_negotiate_req *req;
837 struct smb2_negotiate_rsp *rsp;
842 struct TCP_Server_Info *server = cifs_ses_server(ses);
843 int blob_offset, blob_length;
845 int flags = CIFS_NEG_OP;
846 unsigned int total_len;
848 cifs_dbg(FYI, "Negotiate protocol\n");
851 WARN(1, "%s: server is NULL!\n", __func__);
855 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
856 (void **) &req, &total_len);
860 req->sync_hdr.SessionId = 0;
862 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
863 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
865 if (strcmp(server->vals->version_string,
866 SMB3ANY_VERSION_STRING) == 0) {
867 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
868 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
869 req->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
870 req->DialectCount = cpu_to_le16(3);
872 } else if (strcmp(server->vals->version_string,
873 SMBDEFAULT_VERSION_STRING) == 0) {
874 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
875 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
876 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
877 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
878 req->DialectCount = cpu_to_le16(4);
881 /* otherwise send specific dialect */
882 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
883 req->DialectCount = cpu_to_le16(1);
887 /* only one of SMB2 signing flags may be set in SMB2 request */
889 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
890 else if (global_secflags & CIFSSEC_MAY_SIGN)
891 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
893 req->SecurityMode = 0;
895 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
896 if (ses->chan_max > 1)
897 req->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
899 /* ClientGUID must be zero for SMB2.02 dialect */
900 if (server->vals->protocol_id == SMB20_PROT_ID)
901 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
903 memcpy(req->ClientGUID, server->client_guid,
904 SMB2_CLIENT_GUID_SIZE);
905 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
906 (strcmp(server->vals->version_string,
907 SMB3ANY_VERSION_STRING) == 0) ||
908 (strcmp(server->vals->version_string,
909 SMBDEFAULT_VERSION_STRING) == 0))
910 assemble_neg_contexts(req, server, &total_len);
912 iov[0].iov_base = (char *)req;
913 iov[0].iov_len = total_len;
915 memset(&rqst, 0, sizeof(struct smb_rqst));
919 rc = cifs_send_recv(xid, ses, server,
920 &rqst, &resp_buftype, flags, &rsp_iov);
921 cifs_small_buf_release(req);
922 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
924 * No tcon so can't do
925 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
927 if (rc == -EOPNOTSUPP) {
928 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");
933 if (strcmp(server->vals->version_string,
934 SMB3ANY_VERSION_STRING) == 0) {
935 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
937 "SMB2 dialect returned but not requested\n");
939 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
941 "SMB2.1 dialect returned but not requested\n");
943 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
944 /* ops set to 3.0 by default for default so update */
945 server->ops = &smb311_operations;
946 server->vals = &smb311_values;
948 } else if (strcmp(server->vals->version_string,
949 SMBDEFAULT_VERSION_STRING) == 0) {
950 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
952 "SMB2 dialect returned but not requested\n");
954 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
955 /* ops set to 3.0 by default for default so update */
956 server->ops = &smb21_operations;
957 server->vals = &smb21_values;
958 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
959 server->ops = &smb311_operations;
960 server->vals = &smb311_values;
962 } else if (le16_to_cpu(rsp->DialectRevision) !=
963 server->vals->protocol_id) {
964 /* if requested single dialect ensure returned dialect matched */
965 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
966 le16_to_cpu(rsp->DialectRevision));
970 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
972 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
973 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
974 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
975 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
976 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
977 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
978 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
979 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
980 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
981 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
983 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
984 le16_to_cpu(rsp->DialectRevision));
988 server->dialect = le16_to_cpu(rsp->DialectRevision);
991 * Keep a copy of the hash after negprot. This hash will be
992 * the starting hash value for all sessions made from this
995 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
996 SMB2_PREAUTH_HASH_SIZE);
998 /* SMB2 only has an extended negflavor */
999 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
1000 /* set it to the maximum buffer size value we can send with 1 credit */
1001 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
1002 SMB2_MAX_BUFFER_SIZE);
1003 server->max_read = le32_to_cpu(rsp->MaxReadSize);
1004 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
1005 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
1006 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
1007 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
1009 server->capabilities = le32_to_cpu(rsp->Capabilities);
1010 /* Internal types */
1011 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
1014 * SMB3.0 supports only 1 cipher and doesn't have a encryption neg context
1015 * Set the cipher type manually.
1017 if (server->dialect == SMB30_PROT_ID && (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1018 server->cipher_type = SMB2_ENCRYPTION_AES128_CCM;
1020 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
1021 (struct smb2_sync_hdr *)rsp);
1023 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
1025 * ses->sectype = RawNTLMSSP;
1026 * but for time being this is our only auth choice so doesn't matter.
1027 * We just found a server which sets blob length to zero expecting raw.
1029 if (blob_length == 0) {
1030 cifs_dbg(FYI, "missing security blob on negprot\n");
1031 server->sec_ntlmssp = true;
1034 rc = cifs_enable_signing(server, ses->sign);
1038 rc = decode_negTokenInit(security_blob, blob_length, server);
1045 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
1046 if (rsp->NegotiateContextCount)
1047 rc = smb311_decode_neg_context(rsp, server,
1050 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
1053 free_rsp_buf(resp_buftype, rsp);
1057 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
1060 struct validate_negotiate_info_req *pneg_inbuf;
1061 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
1063 u32 inbuflen; /* max of 4 dialects */
1064 struct TCP_Server_Info *server = tcon->ses->server;
1066 cifs_dbg(FYI, "validate negotiate\n");
1068 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1069 if (server->dialect == SMB311_PROT_ID)
1073 * validation ioctl must be signed, so no point sending this if we
1074 * can not sign it (ie are not known user). Even if signing is not
1075 * required (enabled but not negotiated), in those cases we selectively
1076 * sign just this, the first and only signed request on a connection.
1077 * Having validation of negotiate info helps reduce attack vectors.
1079 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1080 return 0; /* validation requires signing */
1082 if (tcon->ses->user_name == NULL) {
1083 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1084 return 0; /* validation requires signing */
1087 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1088 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1090 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1094 pneg_inbuf->Capabilities =
1095 cpu_to_le32(server->vals->req_capabilities);
1096 if (tcon->ses->chan_max > 1)
1097 pneg_inbuf->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
1099 memcpy(pneg_inbuf->Guid, server->client_guid,
1100 SMB2_CLIENT_GUID_SIZE);
1102 if (tcon->ses->sign)
1103 pneg_inbuf->SecurityMode =
1104 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1105 else if (global_secflags & CIFSSEC_MAY_SIGN)
1106 pneg_inbuf->SecurityMode =
1107 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1109 pneg_inbuf->SecurityMode = 0;
1112 if (strcmp(server->vals->version_string,
1113 SMB3ANY_VERSION_STRING) == 0) {
1114 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1115 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1116 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1117 pneg_inbuf->DialectCount = cpu_to_le16(3);
1118 /* SMB 2.1 not included so subtract one dialect from len */
1119 inbuflen = sizeof(*pneg_inbuf) -
1120 (sizeof(pneg_inbuf->Dialects[0]));
1121 } else if (strcmp(server->vals->version_string,
1122 SMBDEFAULT_VERSION_STRING) == 0) {
1123 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1124 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1125 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1126 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1127 pneg_inbuf->DialectCount = cpu_to_le16(4);
1128 /* structure is big enough for 4 dialects */
1129 inbuflen = sizeof(*pneg_inbuf);
1131 /* otherwise specific dialect was requested */
1132 pneg_inbuf->Dialects[0] =
1133 cpu_to_le16(server->vals->protocol_id);
1134 pneg_inbuf->DialectCount = cpu_to_le16(1);
1135 /* structure is big enough for 3 dialects, sending only 1 */
1136 inbuflen = sizeof(*pneg_inbuf) -
1137 sizeof(pneg_inbuf->Dialects[0]) * 2;
1140 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1141 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
1142 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1143 (char **)&pneg_rsp, &rsplen);
1144 if (rc == -EOPNOTSUPP) {
1146 * Old Windows versions or Netapp SMB server can return
1147 * not supported error. Client should accept it.
1149 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1151 goto out_free_inbuf;
1152 } else if (rc != 0) {
1153 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1156 goto out_free_inbuf;
1160 if (rsplen != sizeof(*pneg_rsp)) {
1161 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1164 /* relax check since Mac returns max bufsize allowed on ioctl */
1165 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1169 /* check validate negotiate info response matches what we got earlier */
1170 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1173 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1176 /* do not validate server guid because not saved at negprot time yet */
1178 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1179 SMB2_LARGE_FILES) != server->capabilities)
1182 /* validate negotiate successful */
1184 cifs_dbg(FYI, "validate negotiate info successful\n");
1188 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1197 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1199 switch (requested) {
1206 if (server->sec_ntlmssp &&
1207 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1209 if ((server->sec_kerberos || server->sec_mskerberos) &&
1210 (global_secflags & CIFSSEC_MAY_KRB5))
1218 struct SMB2_sess_data {
1220 struct cifs_ses *ses;
1221 struct nls_table *nls_cp;
1222 void (*func)(struct SMB2_sess_data *);
1224 u64 previous_session;
1226 /* we will send the SMB in three pieces:
1227 * a fixed length beginning part, an optional
1228 * SPNEGO blob (which can be zero length), and a
1229 * last part which will include the strings
1230 * and rest of bcc area. This allows us to avoid
1231 * a large buffer 17K allocation
1238 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1241 struct cifs_ses *ses = sess_data->ses;
1242 struct smb2_sess_setup_req *req;
1243 struct TCP_Server_Info *server = cifs_ses_server(ses);
1244 unsigned int total_len;
1246 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
1252 if (sess_data->ses->binding) {
1253 req->sync_hdr.SessionId = sess_data->ses->Suid;
1254 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1255 req->PreviousSessionId = 0;
1256 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1258 /* First session, not a reauthenticate */
1259 req->sync_hdr.SessionId = 0;
1261 * if reconnect, we need to send previous sess id
1264 req->PreviousSessionId = sess_data->previous_session;
1265 req->Flags = 0; /* MBZ */
1268 /* enough to enable echos and oplocks and one max size write */
1269 req->sync_hdr.CreditRequest = cpu_to_le16(130);
1271 /* only one of SMB2 signing flags may be set in SMB2 request */
1273 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1274 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1275 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1277 req->SecurityMode = 0;
1279 #ifdef CONFIG_CIFS_DFS_UPCALL
1280 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1282 req->Capabilities = 0;
1283 #endif /* DFS_UPCALL */
1285 req->Channel = 0; /* MBZ */
1287 sess_data->iov[0].iov_base = (char *)req;
1289 sess_data->iov[0].iov_len = total_len - 1;
1291 * This variable will be used to clear the buffer
1292 * allocated above in case of any error in the calling function.
1294 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1300 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1302 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1303 sess_data->buf0_type = CIFS_NO_BUFFER;
1307 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1310 struct smb_rqst rqst;
1311 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1312 struct kvec rsp_iov = { NULL, 0 };
1314 /* Testing shows that buffer offset must be at location of Buffer[0] */
1315 req->SecurityBufferOffset =
1316 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1317 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1319 memset(&rqst, 0, sizeof(struct smb_rqst));
1320 rqst.rq_iov = sess_data->iov;
1323 /* BB add code to build os and lm fields */
1324 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1325 cifs_ses_server(sess_data->ses),
1327 &sess_data->buf0_type,
1328 CIFS_LOG_ERROR | CIFS_SESS_OP, &rsp_iov);
1329 cifs_small_buf_release(sess_data->iov[0].iov_base);
1330 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1336 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1339 struct cifs_ses *ses = sess_data->ses;
1340 struct TCP_Server_Info *server = cifs_ses_server(ses);
1342 mutex_lock(&server->srv_mutex);
1343 if (server->ops->generate_signingkey) {
1344 rc = server->ops->generate_signingkey(ses);
1347 "SMB3 session key generation failed\n");
1348 mutex_unlock(&server->srv_mutex);
1352 if (!server->session_estab) {
1353 server->sequence_number = 0x2;
1354 server->session_estab = true;
1356 mutex_unlock(&server->srv_mutex);
1358 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1359 /* keep existing ses state if binding */
1360 if (!ses->binding) {
1361 spin_lock(&GlobalMid_Lock);
1362 ses->status = CifsGood;
1363 ses->need_reconnect = false;
1364 spin_unlock(&GlobalMid_Lock);
1370 #ifdef CONFIG_CIFS_UPCALL
1372 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1375 struct cifs_ses *ses = sess_data->ses;
1376 struct cifs_spnego_msg *msg;
1377 struct key *spnego_key = NULL;
1378 struct smb2_sess_setup_rsp *rsp = NULL;
1380 rc = SMB2_sess_alloc_buffer(sess_data);
1384 spnego_key = cifs_get_spnego_key(ses);
1385 if (IS_ERR(spnego_key)) {
1386 rc = PTR_ERR(spnego_key);
1388 cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
1393 msg = spnego_key->payload.data[0];
1395 * check version field to make sure that cifs.upcall is
1396 * sending us a response in an expected form
1398 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1399 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1400 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1402 goto out_put_spnego_key;
1405 /* keep session key if binding */
1406 if (!ses->binding) {
1407 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1409 if (!ses->auth_key.response) {
1410 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1413 goto out_put_spnego_key;
1415 ses->auth_key.len = msg->sesskey_len;
1418 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1419 sess_data->iov[1].iov_len = msg->secblob_len;
1421 rc = SMB2_sess_sendreceive(sess_data);
1423 goto out_put_spnego_key;
1425 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1426 /* keep session id and flags if binding */
1427 if (!ses->binding) {
1428 ses->Suid = rsp->sync_hdr.SessionId;
1429 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1432 rc = SMB2_sess_establish_session(sess_data);
1434 key_invalidate(spnego_key);
1435 key_put(spnego_key);
1437 sess_data->result = rc;
1438 sess_data->func = NULL;
1439 SMB2_sess_free_buffer(sess_data);
1443 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1445 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1446 sess_data->result = -EOPNOTSUPP;
1447 sess_data->func = NULL;
1452 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1455 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1458 struct cifs_ses *ses = sess_data->ses;
1459 struct smb2_sess_setup_rsp *rsp = NULL;
1460 char *ntlmssp_blob = NULL;
1461 bool use_spnego = false; /* else use raw ntlmssp */
1462 u16 blob_length = 0;
1465 * If memory allocation is successful, caller of this function
1468 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1469 if (!ses->ntlmssp) {
1473 ses->ntlmssp->sesskey_per_smbsess = true;
1475 rc = SMB2_sess_alloc_buffer(sess_data);
1479 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1481 if (ntlmssp_blob == NULL) {
1486 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1488 /* BB eventually need to add this */
1489 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1493 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1494 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1496 sess_data->iov[1].iov_base = ntlmssp_blob;
1497 sess_data->iov[1].iov_len = blob_length;
1499 rc = SMB2_sess_sendreceive(sess_data);
1500 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1502 /* If true, rc here is expected and not an error */
1503 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1504 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1510 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1511 le16_to_cpu(rsp->SecurityBufferOffset)) {
1512 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1513 le16_to_cpu(rsp->SecurityBufferOffset));
1517 rc = decode_ntlmssp_challenge(rsp->Buffer,
1518 le16_to_cpu(rsp->SecurityBufferLength), ses);
1522 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1524 /* keep existing ses id and flags if binding */
1525 if (!ses->binding) {
1526 ses->Suid = rsp->sync_hdr.SessionId;
1527 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1531 kfree(ntlmssp_blob);
1532 SMB2_sess_free_buffer(sess_data);
1534 sess_data->result = 0;
1535 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1539 kfree(ses->ntlmssp);
1540 ses->ntlmssp = NULL;
1541 sess_data->result = rc;
1542 sess_data->func = NULL;
1546 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1549 struct cifs_ses *ses = sess_data->ses;
1550 struct smb2_sess_setup_req *req;
1551 struct smb2_sess_setup_rsp *rsp = NULL;
1552 unsigned char *ntlmssp_blob = NULL;
1553 bool use_spnego = false; /* else use raw ntlmssp */
1554 u16 blob_length = 0;
1556 rc = SMB2_sess_alloc_buffer(sess_data);
1560 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1561 req->sync_hdr.SessionId = ses->Suid;
1563 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1566 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1571 /* BB eventually need to add this */
1572 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1576 sess_data->iov[1].iov_base = ntlmssp_blob;
1577 sess_data->iov[1].iov_len = blob_length;
1579 rc = SMB2_sess_sendreceive(sess_data);
1583 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1585 /* keep existing ses id and flags if binding */
1586 if (!ses->binding) {
1587 ses->Suid = rsp->sync_hdr.SessionId;
1588 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1591 rc = SMB2_sess_establish_session(sess_data);
1592 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1593 if (ses->server->dialect < SMB30_PROT_ID) {
1594 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1596 * The session id is opaque in terms of endianness, so we can't
1597 * print it as a long long. we dump it as we got it on the wire
1599 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1601 cifs_dbg(VFS, "Session Key %*ph\n",
1602 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1603 cifs_dbg(VFS, "Signing Key %*ph\n",
1604 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1608 kfree(ntlmssp_blob);
1609 SMB2_sess_free_buffer(sess_data);
1610 kfree(ses->ntlmssp);
1611 ses->ntlmssp = NULL;
1612 sess_data->result = rc;
1613 sess_data->func = NULL;
1617 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1621 type = smb2_select_sectype(cifs_ses_server(ses), ses->sectype);
1622 cifs_dbg(FYI, "sess setup type %d\n", type);
1623 if (type == Unspecified) {
1624 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1630 sess_data->func = SMB2_auth_kerberos;
1633 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1636 cifs_dbg(VFS, "secType %d not supported!\n", type);
1644 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1645 const struct nls_table *nls_cp)
1648 struct TCP_Server_Info *server = cifs_ses_server(ses);
1649 struct SMB2_sess_data *sess_data;
1651 cifs_dbg(FYI, "Session Setup\n");
1654 WARN(1, "%s: server is NULL!\n", __func__);
1658 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1662 rc = SMB2_select_sec(ses, sess_data);
1665 sess_data->xid = xid;
1666 sess_data->ses = ses;
1667 sess_data->buf0_type = CIFS_NO_BUFFER;
1668 sess_data->nls_cp = (struct nls_table *) nls_cp;
1669 sess_data->previous_session = ses->Suid;
1672 * Initialize the session hash with the server one.
1674 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
1675 SMB2_PREAUTH_HASH_SIZE);
1677 while (sess_data->func)
1678 sess_data->func(sess_data);
1680 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1681 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1682 rc = sess_data->result;
1689 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1691 struct smb_rqst rqst;
1692 struct smb2_logoff_req *req; /* response is also trivial struct */
1694 struct TCP_Server_Info *server;
1696 unsigned int total_len;
1698 struct kvec rsp_iov;
1701 cifs_dbg(FYI, "disconnect session %p\n", ses);
1703 if (ses && (ses->server))
1704 server = ses->server;
1708 /* no need to send SMB logoff if uid already closed due to reconnect */
1709 if (ses->need_reconnect)
1710 goto smb2_session_already_dead;
1712 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
1713 (void **) &req, &total_len);
1717 /* since no tcon, smb2_init can not do this, so do here */
1718 req->sync_hdr.SessionId = ses->Suid;
1720 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1721 flags |= CIFS_TRANSFORM_REQ;
1722 else if (server->sign)
1723 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1725 flags |= CIFS_NO_RSP_BUF;
1727 iov[0].iov_base = (char *)req;
1728 iov[0].iov_len = total_len;
1730 memset(&rqst, 0, sizeof(struct smb_rqst));
1734 rc = cifs_send_recv(xid, ses, ses->server,
1735 &rqst, &resp_buf_type, flags, &rsp_iov);
1736 cifs_small_buf_release(req);
1738 * No tcon so can't do
1739 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1742 smb2_session_already_dead:
1746 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1748 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1751 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1753 /* These are similar values to what Windows uses */
1754 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1756 tcon->max_chunks = 256;
1757 tcon->max_bytes_chunk = 1048576;
1758 tcon->max_bytes_copy = 16777216;
1762 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1763 struct cifs_tcon *tcon, const struct nls_table *cp)
1765 struct smb_rqst rqst;
1766 struct smb2_tree_connect_req *req;
1767 struct smb2_tree_connect_rsp *rsp = NULL;
1769 struct kvec rsp_iov = { NULL, 0 };
1773 __le16 *unc_path = NULL;
1775 unsigned int total_len;
1776 struct TCP_Server_Info *server;
1778 /* always use master channel */
1779 server = ses->server;
1781 cifs_dbg(FYI, "TCON\n");
1783 if (!server || !tree)
1786 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1787 if (unc_path == NULL)
1790 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1792 if (unc_path_len < 2) {
1797 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1799 atomic_set(&tcon->num_remote_opens, 0);
1800 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
1801 (void **) &req, &total_len);
1807 if (smb3_encryption_required(tcon))
1808 flags |= CIFS_TRANSFORM_REQ;
1810 iov[0].iov_base = (char *)req;
1812 iov[0].iov_len = total_len - 1;
1814 /* Testing shows that buffer offset must be at location of Buffer[0] */
1815 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1817 req->PathLength = cpu_to_le16(unc_path_len - 2);
1818 iov[1].iov_base = unc_path;
1819 iov[1].iov_len = unc_path_len;
1822 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1823 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1824 * (Samba servers don't always set the flag so also check if null user)
1826 if ((server->dialect == SMB311_PROT_ID) &&
1827 !smb3_encryption_required(tcon) &&
1828 !(ses->session_flags &
1829 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1830 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1831 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1833 memset(&rqst, 0, sizeof(struct smb_rqst));
1837 /* Need 64 for max size write so ask for more in case not there yet */
1838 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1840 rc = cifs_send_recv(xid, ses, server,
1841 &rqst, &resp_buftype, flags, &rsp_iov);
1842 cifs_small_buf_release(req);
1843 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1844 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1846 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1847 tcon->need_reconnect = true;
1848 goto tcon_error_exit;
1851 switch (rsp->ShareType) {
1852 case SMB2_SHARE_TYPE_DISK:
1853 cifs_dbg(FYI, "connection to disk share\n");
1855 case SMB2_SHARE_TYPE_PIPE:
1857 cifs_dbg(FYI, "connection to pipe share\n");
1859 case SMB2_SHARE_TYPE_PRINT:
1861 cifs_dbg(FYI, "connection to printer\n");
1864 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1866 goto tcon_error_exit;
1869 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1870 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1871 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1872 tcon->tidStatus = CifsGood;
1873 tcon->need_reconnect = false;
1874 tcon->tid = rsp->sync_hdr.TreeId;
1875 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1877 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1878 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1879 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
1882 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1883 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
1885 init_copy_chunk_defaults(tcon);
1886 if (server->ops->validate_negotiate)
1887 rc = server->ops->validate_negotiate(xid, tcon);
1890 free_rsp_buf(resp_buftype, rsp);
1895 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1896 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1902 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1904 struct smb_rqst rqst;
1905 struct smb2_tree_disconnect_req *req; /* response is trivial */
1907 struct cifs_ses *ses = tcon->ses;
1909 unsigned int total_len;
1911 struct kvec rsp_iov;
1914 cifs_dbg(FYI, "Tree Disconnect\n");
1916 if (!ses || !(ses->server))
1919 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1922 close_cached_dir_lease(&tcon->crfid);
1924 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, ses->server,
1930 if (smb3_encryption_required(tcon))
1931 flags |= CIFS_TRANSFORM_REQ;
1933 flags |= CIFS_NO_RSP_BUF;
1935 iov[0].iov_base = (char *)req;
1936 iov[0].iov_len = total_len;
1938 memset(&rqst, 0, sizeof(struct smb_rqst));
1942 rc = cifs_send_recv(xid, ses, ses->server,
1943 &rqst, &resp_buf_type, flags, &rsp_iov);
1944 cifs_small_buf_release(req);
1946 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1952 static struct create_durable *
1953 create_durable_buf(void)
1955 struct create_durable *buf;
1957 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1961 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1962 (struct create_durable, Data));
1963 buf->ccontext.DataLength = cpu_to_le32(16);
1964 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1965 (struct create_durable, Name));
1966 buf->ccontext.NameLength = cpu_to_le16(4);
1967 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1975 static struct create_durable *
1976 create_reconnect_durable_buf(struct cifs_fid *fid)
1978 struct create_durable *buf;
1980 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1984 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1985 (struct create_durable, Data));
1986 buf->ccontext.DataLength = cpu_to_le32(16);
1987 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1988 (struct create_durable, Name));
1989 buf->ccontext.NameLength = cpu_to_le16(4);
1990 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1991 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1992 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
2001 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
2003 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
2005 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
2006 pdisk_id->DiskFileId, pdisk_id->VolumeId);
2007 buf->IndexNumber = pdisk_id->DiskFileId;
2011 parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
2012 struct create_posix_rsp *posix)
2015 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
2016 u8 *end = beg + le32_to_cpu(cc->DataLength);
2019 memset(posix, 0, sizeof(*posix));
2021 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
2022 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
2023 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
2026 sid_len = posix_info_sid_size(sid, end);
2028 cifs_dbg(VFS, "bad owner sid in posix create response\n");
2031 memcpy(&posix->owner, sid, sid_len);
2033 sid = sid + sid_len;
2034 sid_len = posix_info_sid_size(sid, end);
2036 cifs_dbg(VFS, "bad group sid in posix create response\n");
2039 memcpy(&posix->group, sid, sid_len);
2041 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
2042 posix->nlink, posix->mode, posix->reparse_tag);
2046 smb2_parse_contexts(struct TCP_Server_Info *server,
2047 struct smb2_create_rsp *rsp,
2048 unsigned int *epoch, char *lease_key, __u8 *oplock,
2049 struct smb2_file_all_info *buf,
2050 struct create_posix_rsp *posix)
2053 struct create_context *cc;
2055 unsigned int remaining;
2057 static const char smb3_create_tag_posix[] = {
2058 0x93, 0xAD, 0x25, 0x50, 0x9C,
2059 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
2060 0xDE, 0x96, 0x8B, 0xCD, 0x7C
2064 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
2065 remaining = le32_to_cpu(rsp->CreateContextsLength);
2066 cc = (struct create_context *)data_offset;
2068 /* Initialize inode number to 0 in case no valid data in qfid context */
2070 buf->IndexNumber = 0;
2072 while (remaining >= sizeof(struct create_context)) {
2073 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
2074 if (le16_to_cpu(cc->NameLength) == 4 &&
2075 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
2076 *oplock = server->ops->parse_lease_buf(cc, epoch,
2078 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
2079 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
2080 parse_query_id_ctxt(cc, buf);
2081 else if ((le16_to_cpu(cc->NameLength) == 16)) {
2083 memcmp(name, smb3_create_tag_posix, 16) == 0)
2084 parse_posix_ctxt(cc, buf, posix);
2087 cifs_dbg(FYI, "Context not matched with len %d\n",
2088 le16_to_cpu(cc->NameLength));
2089 cifs_dump_mem("Cctxt name: ", name, 4);
2092 next = le32_to_cpu(cc->Next);
2096 cc = (struct create_context *)((char *)cc + next);
2099 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2100 *oplock = rsp->OplockLevel;
2106 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
2107 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
2109 struct smb2_create_req *req = iov[0].iov_base;
2110 unsigned int num = *num_iovec;
2112 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
2113 if (iov[num].iov_base == NULL)
2115 iov[num].iov_len = server->vals->create_lease_size;
2116 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2117 if (!req->CreateContextsOffset)
2118 req->CreateContextsOffset = cpu_to_le32(
2119 sizeof(struct smb2_create_req) +
2120 iov[num - 1].iov_len);
2121 le32_add_cpu(&req->CreateContextsLength,
2122 server->vals->create_lease_size);
2123 *num_iovec = num + 1;
2127 static struct create_durable_v2 *
2128 create_durable_v2_buf(struct cifs_open_parms *oparms)
2130 struct cifs_fid *pfid = oparms->fid;
2131 struct create_durable_v2 *buf;
2133 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2137 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2138 (struct create_durable_v2, dcontext));
2139 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2140 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2141 (struct create_durable_v2, Name));
2142 buf->ccontext.NameLength = cpu_to_le16(4);
2145 * NB: Handle timeout defaults to 0, which allows server to choose
2146 * (most servers default to 120 seconds) and most clients default to 0.
2147 * This can be overridden at mount ("handletimeout=") if the user wants
2148 * a different persistent (or resilient) handle timeout for all opens
2149 * opens on a particular SMB3 mount.
2151 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2152 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2153 generate_random_uuid(buf->dcontext.CreateGuid);
2154 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2156 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2164 static struct create_durable_handle_reconnect_v2 *
2165 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2167 struct create_durable_handle_reconnect_v2 *buf;
2169 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2174 buf->ccontext.DataOffset =
2175 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2177 buf->ccontext.DataLength =
2178 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2179 buf->ccontext.NameOffset =
2180 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2182 buf->ccontext.NameLength = cpu_to_le16(4);
2184 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2185 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2186 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2187 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2189 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2198 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2199 struct cifs_open_parms *oparms)
2201 struct smb2_create_req *req = iov[0].iov_base;
2202 unsigned int num = *num_iovec;
2204 iov[num].iov_base = create_durable_v2_buf(oparms);
2205 if (iov[num].iov_base == NULL)
2207 iov[num].iov_len = sizeof(struct create_durable_v2);
2208 if (!req->CreateContextsOffset)
2209 req->CreateContextsOffset =
2210 cpu_to_le32(sizeof(struct smb2_create_req) +
2212 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
2213 *num_iovec = num + 1;
2218 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2219 struct cifs_open_parms *oparms)
2221 struct smb2_create_req *req = iov[0].iov_base;
2222 unsigned int num = *num_iovec;
2224 /* indicate that we don't need to relock the file */
2225 oparms->reconnect = false;
2227 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2228 if (iov[num].iov_base == NULL)
2230 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2231 if (!req->CreateContextsOffset)
2232 req->CreateContextsOffset =
2233 cpu_to_le32(sizeof(struct smb2_create_req) +
2235 le32_add_cpu(&req->CreateContextsLength,
2236 sizeof(struct create_durable_handle_reconnect_v2));
2237 *num_iovec = num + 1;
2242 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2243 struct cifs_open_parms *oparms, bool use_persistent)
2245 struct smb2_create_req *req = iov[0].iov_base;
2246 unsigned int num = *num_iovec;
2248 if (use_persistent) {
2249 if (oparms->reconnect)
2250 return add_durable_reconnect_v2_context(iov, num_iovec,
2253 return add_durable_v2_context(iov, num_iovec, oparms);
2256 if (oparms->reconnect) {
2257 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2258 /* indicate that we don't need to relock the file */
2259 oparms->reconnect = false;
2261 iov[num].iov_base = create_durable_buf();
2262 if (iov[num].iov_base == NULL)
2264 iov[num].iov_len = sizeof(struct create_durable);
2265 if (!req->CreateContextsOffset)
2266 req->CreateContextsOffset =
2267 cpu_to_le32(sizeof(struct smb2_create_req) +
2269 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
2270 *num_iovec = num + 1;
2274 /* See MS-SMB2 2.2.13.2.7 */
2275 static struct crt_twarp_ctxt *
2276 create_twarp_buf(__u64 timewarp)
2278 struct crt_twarp_ctxt *buf;
2280 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2284 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2285 (struct crt_twarp_ctxt, Timestamp));
2286 buf->ccontext.DataLength = cpu_to_le32(8);
2287 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2288 (struct crt_twarp_ctxt, Name));
2289 buf->ccontext.NameLength = cpu_to_le16(4);
2290 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2295 buf->Timestamp = cpu_to_le64(timewarp);
2299 /* See MS-SMB2 2.2.13.2.7 */
2301 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2303 struct smb2_create_req *req = iov[0].iov_base;
2304 unsigned int num = *num_iovec;
2306 iov[num].iov_base = create_twarp_buf(timewarp);
2307 if (iov[num].iov_base == NULL)
2309 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2310 if (!req->CreateContextsOffset)
2311 req->CreateContextsOffset = cpu_to_le32(
2312 sizeof(struct smb2_create_req) +
2313 iov[num - 1].iov_len);
2314 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2315 *num_iovec = num + 1;
2319 /* See See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
2320 static void setup_owner_group_sids(char *buf)
2322 struct owner_group_sids *sids = (struct owner_group_sids *)buf;
2324 /* Populate the user ownership fields S-1-5-88-1 */
2325 sids->owner.Revision = 1;
2326 sids->owner.NumAuth = 3;
2327 sids->owner.Authority[5] = 5;
2328 sids->owner.SubAuthorities[0] = cpu_to_le32(88);
2329 sids->owner.SubAuthorities[1] = cpu_to_le32(1);
2330 sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val);
2332 /* Populate the group ownership fields S-1-5-88-2 */
2333 sids->group.Revision = 1;
2334 sids->group.NumAuth = 3;
2335 sids->group.Authority[5] = 5;
2336 sids->group.SubAuthorities[0] = cpu_to_le32(88);
2337 sids->group.SubAuthorities[1] = cpu_to_le32(2);
2338 sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val);
2340 cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val);
2343 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2344 static struct crt_sd_ctxt *
2345 create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
2347 struct crt_sd_ctxt *buf;
2349 unsigned int acelen, acl_size, ace_count;
2350 unsigned int owner_offset = 0;
2351 unsigned int group_offset = 0;
2352 struct smb3_acl acl;
2354 *len = roundup(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 4), 8);
2357 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
2358 *len += sizeof(struct owner_group_sids);
2361 buf = kzalloc(*len, GFP_KERNEL);
2365 ptr = (__u8 *)&buf[1];
2367 /* offset fields are from beginning of security descriptor not of create context */
2368 owner_offset = ptr - (__u8 *)&buf->sd;
2369 buf->sd.OffsetOwner = cpu_to_le32(owner_offset);
2370 group_offset = owner_offset + offsetof(struct owner_group_sids, group);
2371 buf->sd.OffsetGroup = cpu_to_le32(group_offset);
2373 setup_owner_group_sids(ptr);
2374 ptr += sizeof(struct owner_group_sids);
2376 buf->sd.OffsetOwner = 0;
2377 buf->sd.OffsetGroup = 0;
2380 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, sd));
2381 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name));
2382 buf->ccontext.NameLength = cpu_to_le16(4);
2383 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2388 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2391 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2392 * and "DP" ie the DACL is present
2394 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2396 /* offset owner, group and Sbz1 and SACL are all zero */
2397 buf->sd.OffsetDacl = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2398 /* Ship the ACL for now. we will copy it into buf later. */
2400 ptr += sizeof(struct smb3_acl);
2402 /* create one ACE to hold the mode embedded in reserved special SID */
2403 acelen = setup_special_mode_ACE((struct cifs_ace *)ptr, (__u64)mode);
2405 acl_size = acelen + sizeof(struct smb3_acl);
2409 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
2410 acelen = setup_special_user_owner_ACE((struct cifs_ace *)ptr);
2416 /* and one more ACE to allow access for authenticated users */
2417 acelen = setup_authusers_ACE((struct cifs_ace *)ptr);
2422 acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2423 acl.AclSize = cpu_to_le16(acl_size);
2424 acl.AceCount = cpu_to_le16(ace_count);
2425 memcpy(aclptr, &acl, sizeof(struct smb3_acl));
2427 buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2428 *len = roundup(ptr - (__u8 *)buf, 8);
2434 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner)
2436 struct smb2_create_req *req = iov[0].iov_base;
2437 unsigned int num = *num_iovec;
2438 unsigned int len = 0;
2440 iov[num].iov_base = create_sd_buf(mode, set_owner, &len);
2441 if (iov[num].iov_base == NULL)
2443 iov[num].iov_len = len;
2444 if (!req->CreateContextsOffset)
2445 req->CreateContextsOffset = cpu_to_le32(
2446 sizeof(struct smb2_create_req) +
2447 iov[num - 1].iov_len);
2448 le32_add_cpu(&req->CreateContextsLength, len);
2449 *num_iovec = num + 1;
2453 static struct crt_query_id_ctxt *
2454 create_query_id_buf(void)
2456 struct crt_query_id_ctxt *buf;
2458 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2462 buf->ccontext.DataOffset = cpu_to_le16(0);
2463 buf->ccontext.DataLength = cpu_to_le32(0);
2464 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2465 (struct crt_query_id_ctxt, Name));
2466 buf->ccontext.NameLength = cpu_to_le16(4);
2467 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2475 /* See MS-SMB2 2.2.13.2.9 */
2477 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2479 struct smb2_create_req *req = iov[0].iov_base;
2480 unsigned int num = *num_iovec;
2482 iov[num].iov_base = create_query_id_buf();
2483 if (iov[num].iov_base == NULL)
2485 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2486 if (!req->CreateContextsOffset)
2487 req->CreateContextsOffset = cpu_to_le32(
2488 sizeof(struct smb2_create_req) +
2489 iov[num - 1].iov_len);
2490 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2491 *num_iovec = num + 1;
2496 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2497 const char *treename, const __le16 *path)
2499 int treename_len, path_len;
2500 struct nls_table *cp;
2501 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2506 treename_len = strlen(treename);
2507 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2513 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2516 * make room for one path separator between the treename and
2519 *out_len = treename_len + 1 + path_len;
2522 * final path needs to be null-terminated UTF16 with a
2526 *out_size = roundup((*out_len+1)*2, 8);
2527 *out_path = kzalloc(*out_size, GFP_KERNEL);
2531 cp = load_nls_default();
2532 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2533 UniStrcat(*out_path, sep);
2534 UniStrcat(*out_path, path);
2540 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2541 umode_t mode, struct cifs_tcon *tcon,
2542 const char *full_path,
2543 struct cifs_sb_info *cifs_sb)
2545 struct smb_rqst rqst;
2546 struct smb2_create_req *req;
2547 struct smb2_create_rsp *rsp = NULL;
2548 struct cifs_ses *ses = tcon->ses;
2549 struct kvec iov[3]; /* make sure at least one for each open context */
2550 struct kvec rsp_iov = {NULL, 0};
2553 __le16 *copy_path = NULL;
2556 unsigned int n_iov = 2;
2557 __u32 file_attributes = 0;
2558 char *pc_buf = NULL;
2560 unsigned int total_len;
2561 __le16 *utf16_path = NULL;
2562 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2564 cifs_dbg(FYI, "mkdir\n");
2566 /* resource #1: path allocation */
2567 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2571 if (!ses || !server) {
2576 /* resource #2: request */
2577 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2578 (void **) &req, &total_len);
2583 if (smb3_encryption_required(tcon))
2584 flags |= CIFS_TRANSFORM_REQ;
2586 req->ImpersonationLevel = IL_IMPERSONATION;
2587 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2588 /* File attributes ignored on open (used in create though) */
2589 req->FileAttributes = cpu_to_le32(file_attributes);
2590 req->ShareAccess = FILE_SHARE_ALL_LE;
2591 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2592 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2594 iov[0].iov_base = (char *)req;
2595 /* -1 since last byte is buf[0] which is sent below (path) */
2596 iov[0].iov_len = total_len - 1;
2598 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2600 /* [MS-SMB2] 2.2.13 NameOffset:
2601 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2602 * the SMB2 header, the file name includes a prefix that will
2603 * be processed during DFS name normalization as specified in
2604 * section 3.3.5.9. Otherwise, the file name is relative to
2605 * the share that is identified by the TreeId in the SMB2
2608 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2611 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2612 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2614 tcon->treeName, utf16_path);
2618 req->NameLength = cpu_to_le16(name_len * 2);
2619 uni_path_len = copy_size;
2620 /* free before overwriting resource */
2622 utf16_path = copy_path;
2624 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2625 /* MUST set path len (NameLength) to 0 opening root of share */
2626 req->NameLength = cpu_to_le16(uni_path_len - 2);
2627 if (uni_path_len % 8 != 0) {
2628 copy_size = roundup(uni_path_len, 8);
2629 copy_path = kzalloc(copy_size, GFP_KERNEL);
2634 memcpy((char *)copy_path, (const char *)utf16_path,
2636 uni_path_len = copy_size;
2637 /* free before overwriting resource */
2639 utf16_path = copy_path;
2643 iov[1].iov_len = uni_path_len;
2644 iov[1].iov_base = utf16_path;
2645 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2647 if (tcon->posix_extensions) {
2648 /* resource #3: posix buf */
2649 rc = add_posix_context(iov, &n_iov, mode);
2652 pc_buf = iov[n_iov-1].iov_base;
2656 memset(&rqst, 0, sizeof(struct smb_rqst));
2658 rqst.rq_nvec = n_iov;
2660 /* no need to inc num_remote_opens because we close it just below */
2661 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2662 FILE_WRITE_ATTRIBUTES);
2663 /* resource #4: response buffer */
2664 rc = cifs_send_recv(xid, ses, server,
2665 &rqst, &resp_buftype, flags, &rsp_iov);
2667 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2668 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2670 FILE_WRITE_ATTRIBUTES, rc);
2671 goto err_free_rsp_buf;
2674 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2675 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2676 ses->Suid, CREATE_NOT_FILE,
2677 FILE_WRITE_ATTRIBUTES);
2679 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2681 /* Eventually save off posix specific response info and timestaps */
2684 free_rsp_buf(resp_buftype, rsp);
2687 cifs_small_buf_release(req);
2694 SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2695 struct smb_rqst *rqst, __u8 *oplock,
2696 struct cifs_open_parms *oparms, __le16 *path)
2698 struct smb2_create_req *req;
2699 unsigned int n_iov = 2;
2700 __u32 file_attributes = 0;
2703 unsigned int total_len;
2704 struct kvec *iov = rqst->rq_iov;
2708 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2709 (void **) &req, &total_len);
2713 iov[0].iov_base = (char *)req;
2714 /* -1 since last byte is buf[0] which is sent below (path) */
2715 iov[0].iov_len = total_len - 1;
2717 if (oparms->create_options & CREATE_OPTION_READONLY)
2718 file_attributes |= ATTR_READONLY;
2719 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2720 file_attributes |= ATTR_SYSTEM;
2722 req->ImpersonationLevel = IL_IMPERSONATION;
2723 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2724 /* File attributes ignored on open (used in create though) */
2725 req->FileAttributes = cpu_to_le32(file_attributes);
2726 req->ShareAccess = FILE_SHARE_ALL_LE;
2728 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2729 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2730 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2732 /* [MS-SMB2] 2.2.13 NameOffset:
2733 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2734 * the SMB2 header, the file name includes a prefix that will
2735 * be processed during DFS name normalization as specified in
2736 * section 3.3.5.9. Otherwise, the file name is relative to
2737 * the share that is identified by the TreeId in the SMB2
2740 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2743 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2744 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2746 tcon->treeName, path);
2749 req->NameLength = cpu_to_le16(name_len * 2);
2750 uni_path_len = copy_size;
2753 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2754 /* MUST set path len (NameLength) to 0 opening root of share */
2755 req->NameLength = cpu_to_le16(uni_path_len - 2);
2756 copy_size = uni_path_len;
2757 if (copy_size % 8 != 0)
2758 copy_size = roundup(copy_size, 8);
2759 copy_path = kzalloc(copy_size, GFP_KERNEL);
2762 memcpy((char *)copy_path, (const char *)path,
2764 uni_path_len = copy_size;
2768 iov[1].iov_len = uni_path_len;
2769 iov[1].iov_base = path;
2771 if ((!server->oplocks) || (tcon->no_lease))
2772 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2774 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2775 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2776 req->RequestedOplockLevel = *oplock;
2777 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2778 (oparms->create_options & CREATE_NOT_FILE))
2779 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2781 rc = add_lease_context(server, iov, &n_iov,
2782 oparms->fid->lease_key, oplock);
2787 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2788 /* need to set Next field of lease context if we request it */
2789 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2790 struct create_context *ccontext =
2791 (struct create_context *)iov[n_iov-1].iov_base;
2793 cpu_to_le32(server->vals->create_lease_size);
2796 rc = add_durable_context(iov, &n_iov, oparms,
2797 tcon->use_persistent);
2802 if (tcon->posix_extensions) {
2804 struct create_context *ccontext =
2805 (struct create_context *)iov[n_iov-1].iov_base;
2807 cpu_to_le32(iov[n_iov-1].iov_len);
2810 rc = add_posix_context(iov, &n_iov, oparms->mode);
2815 if (tcon->snapshot_time) {
2816 cifs_dbg(FYI, "adding snapshot context\n");
2818 struct create_context *ccontext =
2819 (struct create_context *)iov[n_iov-1].iov_base;
2821 cpu_to_le32(iov[n_iov-1].iov_len);
2824 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2829 if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) {
2833 if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
2834 (oparms->mode != ACL_NO_MODE))
2838 oparms->mode = ACL_NO_MODE;
2841 if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
2846 if (set_owner | set_mode) {
2848 struct create_context *ccontext =
2849 (struct create_context *)iov[n_iov-1].iov_base;
2850 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2853 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2854 rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner);
2861 struct create_context *ccontext =
2862 (struct create_context *)iov[n_iov-1].iov_base;
2863 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2865 add_query_id_context(iov, &n_iov);
2867 rqst->rq_nvec = n_iov;
2871 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2872 * All other vectors are freed by kfree().
2875 SMB2_open_free(struct smb_rqst *rqst)
2879 if (rqst && rqst->rq_iov) {
2880 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2881 for (i = 1; i < rqst->rq_nvec; i++)
2882 if (rqst->rq_iov[i].iov_base != smb2_padding)
2883 kfree(rqst->rq_iov[i].iov_base);
2888 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2889 __u8 *oplock, struct smb2_file_all_info *buf,
2890 struct create_posix_rsp *posix,
2891 struct kvec *err_iov, int *buftype)
2893 struct smb_rqst rqst;
2894 struct smb2_create_rsp *rsp = NULL;
2895 struct cifs_tcon *tcon = oparms->tcon;
2896 struct cifs_ses *ses = tcon->ses;
2897 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2898 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2899 struct kvec rsp_iov = {NULL, 0};
2900 int resp_buftype = CIFS_NO_BUFFER;
2904 cifs_dbg(FYI, "create/open\n");
2905 if (!ses || !server)
2908 if (smb3_encryption_required(tcon))
2909 flags |= CIFS_TRANSFORM_REQ;
2911 memset(&rqst, 0, sizeof(struct smb_rqst));
2912 memset(&iov, 0, sizeof(iov));
2914 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2916 rc = SMB2_open_init(tcon, server,
2917 &rqst, oplock, oparms, path);
2921 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2922 oparms->create_options, oparms->desired_access);
2924 rc = cifs_send_recv(xid, ses, server,
2925 &rqst, &resp_buftype, flags,
2927 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2930 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2931 if (err_iov && rsp) {
2933 *buftype = resp_buftype;
2934 resp_buftype = CIFS_NO_BUFFER;
2937 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2938 oparms->create_options, oparms->desired_access, rc);
2939 if (rc == -EREMCHG) {
2940 pr_warn_once("server share %s deleted\n",
2942 tcon->need_reconnect = true;
2946 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2947 ses->Suid, oparms->create_options,
2948 oparms->desired_access);
2950 atomic_inc(&tcon->num_remote_opens);
2951 oparms->fid->persistent_fid = rsp->PersistentFileId;
2952 oparms->fid->volatile_fid = rsp->VolatileFileId;
2953 oparms->fid->access = oparms->desired_access;
2954 #ifdef CONFIG_CIFS_DEBUG2
2955 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2956 #endif /* CIFS_DEBUG2 */
2959 buf->CreationTime = rsp->CreationTime;
2960 buf->LastAccessTime = rsp->LastAccessTime;
2961 buf->LastWriteTime = rsp->LastWriteTime;
2962 buf->ChangeTime = rsp->ChangeTime;
2963 buf->AllocationSize = rsp->AllocationSize;
2964 buf->EndOfFile = rsp->EndofFile;
2965 buf->Attributes = rsp->FileAttributes;
2966 buf->NumberOfLinks = cpu_to_le32(1);
2967 buf->DeletePending = 0;
2971 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2972 oparms->fid->lease_key, oplock, buf, posix);
2974 SMB2_open_free(&rqst);
2975 free_rsp_buf(resp_buftype, rsp);
2980 SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2981 struct smb_rqst *rqst,
2982 u64 persistent_fid, u64 volatile_fid, u32 opcode,
2983 bool is_fsctl, char *in_data, u32 indatalen,
2984 __u32 max_response_size)
2986 struct smb2_ioctl_req *req;
2987 struct kvec *iov = rqst->rq_iov;
2988 unsigned int total_len;
2992 rc = smb2_ioctl_req_init(opcode, tcon, server,
2993 (void **) &req, &total_len);
2999 * indatalen is usually small at a couple of bytes max, so
3000 * just allocate through generic pool
3002 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
3004 cifs_small_buf_release(req);
3009 req->CtlCode = cpu_to_le32(opcode);
3010 req->PersistentFileId = persistent_fid;
3011 req->VolatileFileId = volatile_fid;
3013 iov[0].iov_base = (char *)req;
3015 * If no input data, the size of ioctl struct in
3016 * protocol spec still includes a 1 byte data buffer,
3017 * but if input data passed to ioctl, we do not
3018 * want to double count this, so we do not send
3019 * the dummy one byte of data in iovec[0] if sending
3020 * input data (in iovec[1]).
3023 req->InputCount = cpu_to_le32(indatalen);
3024 /* do not set InputOffset if no input data */
3026 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
3028 iov[0].iov_len = total_len - 1;
3029 iov[1].iov_base = in_data_buf;
3030 iov[1].iov_len = indatalen;
3033 iov[0].iov_len = total_len;
3036 req->OutputOffset = 0;
3037 req->OutputCount = 0; /* MBZ */
3040 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
3041 * We Could increase default MaxOutputResponse, but that could require
3042 * more credits. Windows typically sets this smaller, but for some
3043 * ioctls it may be useful to allow server to send more. No point
3044 * limiting what the server can send as long as fits in one credit
3045 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
3046 * to increase this limit up in the future.
3047 * Note that for snapshot queries that servers like Azure expect that
3048 * the first query be minimal size (and just used to get the number/size
3049 * of previous versions) so response size must be specified as EXACTLY
3050 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
3051 * of eight bytes. Currently that is the only case where we set max
3052 * response size smaller.
3054 req->MaxOutputResponse = cpu_to_le32(max_response_size);
3055 req->sync_hdr.CreditCharge =
3056 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
3057 SMB2_MAX_BUFFER_SIZE));
3059 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
3063 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
3064 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
3065 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
3071 SMB2_ioctl_free(struct smb_rqst *rqst)
3074 if (rqst && rqst->rq_iov) {
3075 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3076 for (i = 1; i < rqst->rq_nvec; i++)
3077 if (rqst->rq_iov[i].iov_base != smb2_padding)
3078 kfree(rqst->rq_iov[i].iov_base);
3084 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
3087 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3088 u64 volatile_fid, u32 opcode, bool is_fsctl,
3089 char *in_data, u32 indatalen, u32 max_out_data_len,
3090 char **out_data, u32 *plen /* returned data len */)
3092 struct smb_rqst rqst;
3093 struct smb2_ioctl_rsp *rsp = NULL;
3094 struct cifs_ses *ses;
3095 struct TCP_Server_Info *server;
3096 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
3097 struct kvec rsp_iov = {NULL, 0};
3098 int resp_buftype = CIFS_NO_BUFFER;
3102 cifs_dbg(FYI, "SMB2 IOCTL\n");
3104 if (out_data != NULL)
3107 /* zero out returned data len, in case of error */
3118 server = cifs_pick_channel(ses);
3122 if (smb3_encryption_required(tcon))
3123 flags |= CIFS_TRANSFORM_REQ;
3125 memset(&rqst, 0, sizeof(struct smb_rqst));
3126 memset(&iov, 0, sizeof(iov));
3128 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
3130 rc = SMB2_ioctl_init(tcon, server,
3131 &rqst, persistent_fid, volatile_fid, opcode,
3132 is_fsctl, in_data, indatalen, max_out_data_len);
3136 rc = cifs_send_recv(xid, ses, server,
3137 &rqst, &resp_buftype, flags,
3139 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
3142 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3143 ses->Suid, 0, opcode, rc);
3145 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
3146 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3148 } else if (rc == -EINVAL) {
3149 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3150 (opcode != FSCTL_SRV_COPYCHUNK)) {
3151 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3154 } else if (rc == -E2BIG) {
3155 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3156 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3161 /* check if caller wants to look at return data or just return rc */
3162 if ((plen == NULL) || (out_data == NULL))
3165 *plen = le32_to_cpu(rsp->OutputCount);
3167 /* We check for obvious errors in the output buffer length and offset */
3169 goto ioctl_exit; /* server returned no data */
3170 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
3171 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
3177 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
3178 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
3179 le32_to_cpu(rsp->OutputOffset));
3185 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3187 if (*out_data == NULL) {
3193 SMB2_ioctl_free(&rqst);
3194 free_rsp_buf(resp_buftype, rsp);
3199 * Individual callers to ioctl worker function follow
3203 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3204 u64 persistent_fid, u64 volatile_fid)
3207 struct compress_ioctl fsctl_input;
3208 char *ret_data = NULL;
3210 fsctl_input.CompressionState =
3211 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3213 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3214 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
3215 (char *)&fsctl_input /* data input */,
3216 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3217 &ret_data /* out data */, NULL);
3219 cifs_dbg(FYI, "set compression rc %d\n", rc);
3225 SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3226 struct smb_rqst *rqst,
3227 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
3229 struct smb2_close_req *req;
3230 struct kvec *iov = rqst->rq_iov;
3231 unsigned int total_len;
3234 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
3235 (void **) &req, &total_len);
3239 req->PersistentFileId = persistent_fid;
3240 req->VolatileFileId = volatile_fid;
3242 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3245 iov[0].iov_base = (char *)req;
3246 iov[0].iov_len = total_len;
3252 SMB2_close_free(struct smb_rqst *rqst)
3254 if (rqst && rqst->rq_iov)
3255 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3259 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3260 u64 persistent_fid, u64 volatile_fid,
3261 struct smb2_file_network_open_info *pbuf)
3263 struct smb_rqst rqst;
3264 struct smb2_close_rsp *rsp = NULL;
3265 struct cifs_ses *ses = tcon->ses;
3266 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3268 struct kvec rsp_iov;
3269 int resp_buftype = CIFS_NO_BUFFER;
3272 bool query_attrs = false;
3274 cifs_dbg(FYI, "Close\n");
3276 if (!ses || !server)
3279 if (smb3_encryption_required(tcon))
3280 flags |= CIFS_TRANSFORM_REQ;
3282 memset(&rqst, 0, sizeof(struct smb_rqst));
3283 memset(&iov, 0, sizeof(iov));
3287 /* check if need to ask server to return timestamps in close response */
3291 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3292 rc = SMB2_close_init(tcon, server,
3293 &rqst, persistent_fid, volatile_fid,
3298 rc = cifs_send_recv(xid, ses, server,
3299 &rqst, &resp_buftype, flags, &rsp_iov);
3300 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
3303 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
3304 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3308 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3311 * Note that have to subtract 4 since struct network_open_info
3312 * has a final 4 byte pad that close response does not have
3315 memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3318 atomic_dec(&tcon->num_remote_opens);
3320 SMB2_close_free(&rqst);
3321 free_rsp_buf(resp_buftype, rsp);
3323 /* retry close in a worker thread if this one is interrupted */
3324 if (is_interrupt_error(rc)) {
3327 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3330 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3331 persistent_fid, tmp_rc);
3337 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3338 u64 persistent_fid, u64 volatile_fid)
3340 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3344 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3345 struct kvec *iov, unsigned int min_buf_size)
3347 unsigned int smb_len = iov->iov_len;
3348 char *end_of_smb = smb_len + (char *)iov->iov_base;
3349 char *begin_of_buf = offset + (char *)iov->iov_base;
3350 char *end_of_buf = begin_of_buf + buffer_length;
3353 if (buffer_length < min_buf_size) {
3354 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3355 buffer_length, min_buf_size);
3359 /* check if beyond RFC1001 maximum length */
3360 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3361 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3362 buffer_length, smb_len);
3366 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3367 cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
3375 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3376 * Caller must free buffer.
3379 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3380 struct kvec *iov, unsigned int minbufsize,
3383 char *begin_of_buf = offset + (char *)iov->iov_base;
3389 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3393 memcpy(data, begin_of_buf, buffer_length);
3399 SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3400 struct smb_rqst *rqst,
3401 u64 persistent_fid, u64 volatile_fid,
3402 u8 info_class, u8 info_type, u32 additional_info,
3403 size_t output_len, size_t input_len, void *input)
3405 struct smb2_query_info_req *req;
3406 struct kvec *iov = rqst->rq_iov;
3407 unsigned int total_len;
3410 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
3411 (void **) &req, &total_len);
3415 req->InfoType = info_type;
3416 req->FileInfoClass = info_class;
3417 req->PersistentFileId = persistent_fid;
3418 req->VolatileFileId = volatile_fid;
3419 req->AdditionalInformation = cpu_to_le32(additional_info);
3421 req->OutputBufferLength = cpu_to_le32(output_len);
3423 req->InputBufferLength = cpu_to_le32(input_len);
3424 /* total_len for smb query request never close to le16 max */
3425 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3426 memcpy(req->Buffer, input, input_len);
3429 iov[0].iov_base = (char *)req;
3431 iov[0].iov_len = total_len - 1 + input_len;
3436 SMB2_query_info_free(struct smb_rqst *rqst)
3438 if (rqst && rqst->rq_iov)
3439 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3443 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3444 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3445 u32 additional_info, size_t output_len, size_t min_len, void **data,
3448 struct smb_rqst rqst;
3449 struct smb2_query_info_rsp *rsp = NULL;
3451 struct kvec rsp_iov;
3453 int resp_buftype = CIFS_NO_BUFFER;
3454 struct cifs_ses *ses = tcon->ses;
3455 struct TCP_Server_Info *server;
3457 bool allocated = false;
3459 cifs_dbg(FYI, "Query Info\n");
3463 server = cifs_pick_channel(ses);
3467 if (smb3_encryption_required(tcon))
3468 flags |= CIFS_TRANSFORM_REQ;
3470 memset(&rqst, 0, sizeof(struct smb_rqst));
3471 memset(&iov, 0, sizeof(iov));
3475 rc = SMB2_query_info_init(tcon, server,
3476 &rqst, persistent_fid, volatile_fid,
3477 info_class, info_type, additional_info,
3478 output_len, 0, NULL);
3482 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3483 ses->Suid, info_class, (__u32)info_type);
3485 rc = cifs_send_recv(xid, ses, server,
3486 &rqst, &resp_buftype, flags, &rsp_iov);
3487 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3490 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3491 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3492 ses->Suid, info_class, (__u32)info_type, rc);
3496 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3497 ses->Suid, info_class, (__u32)info_type);
3500 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3502 *data = kmalloc(*dlen, GFP_KERNEL);
3505 "Error %d allocating memory for acl\n",
3515 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3516 le32_to_cpu(rsp->OutputBufferLength),
3517 &rsp_iov, min_len, *data);
3518 if (rc && allocated) {
3525 SMB2_query_info_free(&rqst);
3526 free_rsp_buf(resp_buftype, rsp);
3530 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3531 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3533 return query_info(xid, tcon, persistent_fid, volatile_fid,
3534 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3535 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3536 sizeof(struct smb2_file_all_info), (void **)&data,
3541 /* currently unused, as now we are doing compounding instead (see smb311_posix_query_path_info) */
3543 SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3544 u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen)
3546 size_t output_len = sizeof(struct smb311_posix_qinfo *) +
3547 (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2);
3550 return query_info(xid, tcon, persistent_fid, volatile_fid,
3551 SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0,
3552 output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen);
3553 /* Note caller must free "data" (passed in above). It may be allocated in query_info call */
3558 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3559 u64 persistent_fid, u64 volatile_fid,
3560 void **data, u32 *plen, u32 extra_info)
3562 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
3566 return query_info(xid, tcon, persistent_fid, volatile_fid,
3567 0, SMB2_O_INFO_SECURITY, additional_info,
3568 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3572 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3573 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3575 return query_info(xid, tcon, persistent_fid, volatile_fid,
3576 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3577 sizeof(struct smb2_file_internal_info),
3578 sizeof(struct smb2_file_internal_info),
3579 (void **)&uniqueid, NULL);
3583 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3584 * See MS-SMB2 2.2.35 and 2.2.36
3588 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3589 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3590 u64 persistent_fid, u64 volatile_fid,
3591 u32 completion_filter, bool watch_tree)
3593 struct smb2_change_notify_req *req;
3594 struct kvec *iov = rqst->rq_iov;
3595 unsigned int total_len;
3598 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
3599 (void **) &req, &total_len);
3603 req->PersistentFileId = persistent_fid;
3604 req->VolatileFileId = volatile_fid;
3605 /* See note 354 of MS-SMB2, 64K max */
3606 req->OutputBufferLength =
3607 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3608 req->CompletionFilter = cpu_to_le32(completion_filter);
3610 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3614 iov[0].iov_base = (char *)req;
3615 iov[0].iov_len = total_len;
3621 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3622 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3623 u32 completion_filter)
3625 struct cifs_ses *ses = tcon->ses;
3626 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3627 struct smb_rqst rqst;
3629 struct kvec rsp_iov = {NULL, 0};
3630 int resp_buftype = CIFS_NO_BUFFER;
3634 cifs_dbg(FYI, "change notify\n");
3635 if (!ses || !server)
3638 if (smb3_encryption_required(tcon))
3639 flags |= CIFS_TRANSFORM_REQ;
3641 memset(&rqst, 0, sizeof(struct smb_rqst));
3642 memset(&iov, 0, sizeof(iov));
3646 rc = SMB2_notify_init(xid, &rqst, tcon, server,
3647 persistent_fid, volatile_fid,
3648 completion_filter, watch_tree);
3652 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3653 (u8)watch_tree, completion_filter);
3654 rc = cifs_send_recv(xid, ses, server,
3655 &rqst, &resp_buftype, flags, &rsp_iov);
3658 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3659 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3660 (u8)watch_tree, completion_filter, rc);
3662 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3663 ses->Suid, (u8)watch_tree, completion_filter);
3667 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3668 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3675 * This is a no-op for now. We're not really interested in the reply, but
3676 * rather in the fact that the server sent one and that server->lstrp
3679 * FIXME: maybe we should consider checking that the reply matches request?
3682 smb2_echo_callback(struct mid_q_entry *mid)
3684 struct TCP_Server_Info *server = mid->callback_data;
3685 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3686 struct cifs_credits credits = { .value = 0, .instance = 0 };
3688 if (mid->mid_state == MID_RESPONSE_RECEIVED
3689 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3690 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3691 credits.instance = server->reconnect_instance;
3694 DeleteMidQEntry(mid);
3695 add_credits(server, &credits, CIFS_ECHO_OP);
3698 void smb2_reconnect_server(struct work_struct *work)
3700 struct TCP_Server_Info *server = container_of(work,
3701 struct TCP_Server_Info, reconnect.work);
3702 struct cifs_ses *ses;
3703 struct cifs_tcon *tcon, *tcon2;
3704 struct list_head tmp_list;
3705 int tcon_exist = false;
3707 int resched = false;
3710 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3711 mutex_lock(&server->reconnect_mutex);
3713 INIT_LIST_HEAD(&tmp_list);
3714 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3716 spin_lock(&cifs_tcp_ses_lock);
3717 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3718 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3719 if (tcon->need_reconnect || tcon->need_reopen_files) {
3721 list_add_tail(&tcon->rlist, &tmp_list);
3726 * IPC has the same lifetime as its session and uses its
3729 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3730 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3736 * Get the reference to server struct to be sure that the last call of
3737 * cifs_put_tcon() in the loop below won't release the server pointer.
3740 server->srv_count++;
3742 spin_unlock(&cifs_tcp_ses_lock);
3744 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3745 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
3747 cifs_reopen_persistent_handles(tcon);
3750 list_del_init(&tcon->rlist);
3752 cifs_put_smb_ses(tcon->ses);
3754 cifs_put_tcon(tcon);
3757 cifs_dbg(FYI, "Reconnecting tcons finished\n");
3759 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3760 mutex_unlock(&server->reconnect_mutex);
3762 /* now we can safely release srv struct */
3764 cifs_put_tcp_session(server, 1);
3768 SMB2_echo(struct TCP_Server_Info *server)
3770 struct smb2_echo_req *req;
3773 struct smb_rqst rqst = { .rq_iov = iov,
3775 unsigned int total_len;
3777 cifs_dbg(FYI, "In echo request\n");
3779 if (server->tcpStatus == CifsNeedNegotiate) {
3780 /* No need to send echo on newly established connections */
3781 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
3785 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
3786 (void **)&req, &total_len);
3790 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3792 iov[0].iov_len = total_len;
3793 iov[0].iov_base = (char *)req;
3795 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3796 server, CIFS_ECHO_OP, NULL);
3798 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3800 cifs_small_buf_release(req);
3805 SMB2_flush_free(struct smb_rqst *rqst)
3807 if (rqst && rqst->rq_iov)
3808 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3812 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3813 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3814 u64 persistent_fid, u64 volatile_fid)
3816 struct smb2_flush_req *req;
3817 struct kvec *iov = rqst->rq_iov;
3818 unsigned int total_len;
3821 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
3822 (void **) &req, &total_len);
3826 req->PersistentFileId = persistent_fid;
3827 req->VolatileFileId = volatile_fid;
3829 iov[0].iov_base = (char *)req;
3830 iov[0].iov_len = total_len;
3836 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3839 struct cifs_ses *ses = tcon->ses;
3840 struct smb_rqst rqst;
3842 struct kvec rsp_iov = {NULL, 0};
3843 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3844 int resp_buftype = CIFS_NO_BUFFER;
3848 cifs_dbg(FYI, "flush\n");
3849 if (!ses || !(ses->server))
3852 if (smb3_encryption_required(tcon))
3853 flags |= CIFS_TRANSFORM_REQ;
3855 memset(&rqst, 0, sizeof(struct smb_rqst));
3856 memset(&iov, 0, sizeof(iov));
3860 rc = SMB2_flush_init(xid, &rqst, tcon, server,
3861 persistent_fid, volatile_fid);
3865 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3866 rc = cifs_send_recv(xid, ses, server,
3867 &rqst, &resp_buftype, flags, &rsp_iov);
3870 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
3871 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3874 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3878 SMB2_flush_free(&rqst);
3879 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3884 * To form a chain of read requests, any read requests after the first should
3885 * have the end_of_chain boolean set to true.
3888 smb2_new_read_req(void **buf, unsigned int *total_len,
3889 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3890 unsigned int remaining_bytes, int request_type)
3893 struct smb2_read_plain_req *req = NULL;
3894 struct smb2_sync_hdr *shdr;
3895 struct TCP_Server_Info *server = io_parms->server;
3897 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
3898 (void **) &req, total_len);
3903 return -ECONNABORTED;
3905 shdr = &req->sync_hdr;
3906 shdr->ProcessId = cpu_to_le32(io_parms->pid);
3908 req->PersistentFileId = io_parms->persistent_fid;
3909 req->VolatileFileId = io_parms->volatile_fid;
3910 req->ReadChannelInfoOffset = 0; /* reserved */
3911 req->ReadChannelInfoLength = 0; /* reserved */
3912 req->Channel = 0; /* reserved */
3913 req->MinimumCount = 0;
3914 req->Length = cpu_to_le32(io_parms->length);
3915 req->Offset = cpu_to_le64(io_parms->offset);
3917 trace_smb3_read_enter(0 /* xid */,
3918 io_parms->persistent_fid,
3919 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3920 io_parms->offset, io_parms->length);
3921 #ifdef CONFIG_CIFS_SMB_DIRECT
3923 * If we want to do a RDMA write, fill in and append
3924 * smbd_buffer_descriptor_v1 to the end of read request
3926 if (server->rdma && rdata && !server->sign &&
3927 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
3929 struct smbd_buffer_descriptor_v1 *v1;
3930 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3932 rdata->mr = smbd_register_mr(
3933 server->smbd_conn, rdata->pages,
3934 rdata->nr_pages, rdata->page_offset,
3935 rdata->tailsz, true, need_invalidate);
3939 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3940 if (need_invalidate)
3941 req->Channel = SMB2_CHANNEL_RDMA_V1;
3942 req->ReadChannelInfoOffset =
3943 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
3944 req->ReadChannelInfoLength =
3945 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3946 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3947 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3948 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3949 v1->length = cpu_to_le32(rdata->mr->mr->length);
3951 *total_len += sizeof(*v1) - 1;
3954 if (request_type & CHAINED_REQUEST) {
3955 if (!(request_type & END_OF_CHAIN)) {
3956 /* next 8-byte aligned request */
3957 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3958 shdr->NextCommand = cpu_to_le32(*total_len);
3959 } else /* END_OF_CHAIN */
3960 shdr->NextCommand = 0;
3961 if (request_type & RELATED_REQUEST) {
3962 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
3964 * Related requests use info from previous read request
3967 shdr->SessionId = 0xFFFFFFFFFFFFFFFF;
3968 shdr->TreeId = 0xFFFFFFFF;
3969 req->PersistentFileId = 0xFFFFFFFFFFFFFFFF;
3970 req->VolatileFileId = 0xFFFFFFFFFFFFFFFF;
3973 if (remaining_bytes > io_parms->length)
3974 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3976 req->RemainingBytes = 0;
3983 smb2_readv_callback(struct mid_q_entry *mid)
3985 struct cifs_readdata *rdata = mid->callback_data;
3986 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3987 struct TCP_Server_Info *server = rdata->server;
3988 struct smb2_sync_hdr *shdr =
3989 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
3990 struct cifs_credits credits = { .value = 0, .instance = 0 };
3991 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
3993 .rq_pages = rdata->pages,
3994 .rq_offset = rdata->page_offset,
3995 .rq_npages = rdata->nr_pages,
3996 .rq_pagesz = rdata->pagesz,
3997 .rq_tailsz = rdata->tailsz };
3999 WARN_ONCE(rdata->server != mid->server,
4000 "rdata server %p != mid server %p",
4001 rdata->server, mid->server);
4003 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
4004 __func__, mid->mid, mid->mid_state, rdata->result,
4007 switch (mid->mid_state) {
4008 case MID_RESPONSE_RECEIVED:
4009 credits.value = le16_to_cpu(shdr->CreditRequest);
4010 credits.instance = server->reconnect_instance;
4011 /* result already set, check signature */
4012 if (server->sign && !mid->decrypted) {
4015 rc = smb2_verify_signature(&rqst, server);
4017 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
4020 /* FIXME: should this be counted toward the initiating task? */
4021 task_io_account_read(rdata->got_bytes);
4022 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4024 case MID_REQUEST_SUBMITTED:
4025 case MID_RETRY_NEEDED:
4026 rdata->result = -EAGAIN;
4027 if (server->sign && rdata->got_bytes)
4028 /* reset bytes number since we can not check a sign */
4029 rdata->got_bytes = 0;
4030 /* FIXME: should this be counted toward the initiating task? */
4031 task_io_account_read(rdata->got_bytes);
4032 cifs_stats_bytes_read(tcon, rdata->got_bytes);
4034 case MID_RESPONSE_MALFORMED:
4035 credits.value = le16_to_cpu(shdr->CreditRequest);
4036 credits.instance = server->reconnect_instance;
4039 rdata->result = -EIO;
4041 #ifdef CONFIG_CIFS_SMB_DIRECT
4043 * If this rdata has a memmory registered, the MR can be freed
4044 * MR needs to be freed as soon as I/O finishes to prevent deadlock
4045 * because they have limited number and are used for future I/Os
4048 smbd_deregister_mr(rdata->mr);
4052 if (rdata->result && rdata->result != -ENODATA) {
4053 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
4054 trace_smb3_read_err(0 /* xid */,
4055 rdata->cfile->fid.persistent_fid,
4056 tcon->tid, tcon->ses->Suid, rdata->offset,
4057 rdata->bytes, rdata->result);
4059 trace_smb3_read_done(0 /* xid */,
4060 rdata->cfile->fid.persistent_fid,
4061 tcon->tid, tcon->ses->Suid,
4062 rdata->offset, rdata->got_bytes);
4064 queue_work(cifsiod_wq, &rdata->work);
4065 DeleteMidQEntry(mid);
4066 add_credits(server, &credits, 0);
4069 /* smb2_async_readv - send an async read, and set up mid to handle result */
4071 smb2_async_readv(struct cifs_readdata *rdata)
4075 struct smb2_sync_hdr *shdr;
4076 struct cifs_io_parms io_parms;
4077 struct smb_rqst rqst = { .rq_iov = rdata->iov,
4079 struct TCP_Server_Info *server;
4080 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
4081 unsigned int total_len;
4083 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
4084 __func__, rdata->offset, rdata->bytes);
4087 rdata->server = cifs_pick_channel(tcon->ses);
4089 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
4090 io_parms.server = server = rdata->server;
4091 io_parms.offset = rdata->offset;
4092 io_parms.length = rdata->bytes;
4093 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
4094 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
4095 io_parms.pid = rdata->pid;
4097 rc = smb2_new_read_req(
4098 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
4102 if (smb3_encryption_required(io_parms.tcon))
4103 flags |= CIFS_TRANSFORM_REQ;
4105 rdata->iov[0].iov_base = buf;
4106 rdata->iov[0].iov_len = total_len;
4108 shdr = (struct smb2_sync_hdr *)buf;
4110 if (rdata->credits.value > 0) {
4111 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
4112 SMB2_MAX_BUFFER_SIZE));
4113 shdr->CreditRequest = cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 8);
4115 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4117 goto async_readv_out;
4119 flags |= CIFS_HAS_CREDITS;
4122 kref_get(&rdata->refcount);
4123 rc = cifs_call_async(server, &rqst,
4124 cifs_readv_receive, smb2_readv_callback,
4125 smb3_handle_read_data, rdata, flags,
4128 kref_put(&rdata->refcount, cifs_readdata_release);
4129 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
4130 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
4132 io_parms.tcon->ses->Suid,
4133 io_parms.offset, io_parms.length, rc);
4137 cifs_small_buf_release(buf);
4142 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
4143 unsigned int *nbytes, char **buf, int *buf_type)
4145 struct smb_rqst rqst;
4146 int resp_buftype, rc;
4147 struct smb2_read_plain_req *req = NULL;
4148 struct smb2_read_rsp *rsp = NULL;
4150 struct kvec rsp_iov;
4151 unsigned int total_len;
4152 int flags = CIFS_LOG_ERROR;
4153 struct cifs_ses *ses = io_parms->tcon->ses;
4155 if (!io_parms->server)
4156 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4159 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
4163 if (smb3_encryption_required(io_parms->tcon))
4164 flags |= CIFS_TRANSFORM_REQ;
4166 iov[0].iov_base = (char *)req;
4167 iov[0].iov_len = total_len;
4169 memset(&rqst, 0, sizeof(struct smb_rqst));
4173 rc = cifs_send_recv(xid, ses, io_parms->server,
4174 &rqst, &resp_buftype, flags, &rsp_iov);
4175 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
4178 if (rc != -ENODATA) {
4179 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4180 cifs_dbg(VFS, "Send error in read = %d\n", rc);
4181 trace_smb3_read_err(xid, req->PersistentFileId,
4182 io_parms->tcon->tid, ses->Suid,
4183 io_parms->offset, io_parms->length,
4186 trace_smb3_read_done(xid, req->PersistentFileId,
4187 io_parms->tcon->tid, ses->Suid,
4188 io_parms->offset, 0);
4189 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4190 cifs_small_buf_release(req);
4191 return rc == -ENODATA ? 0 : rc;
4193 trace_smb3_read_done(xid, req->PersistentFileId,
4194 io_parms->tcon->tid, ses->Suid,
4195 io_parms->offset, io_parms->length);
4197 cifs_small_buf_release(req);
4199 *nbytes = le32_to_cpu(rsp->DataLength);
4200 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4201 (*nbytes > io_parms->length)) {
4202 cifs_dbg(FYI, "bad length %d for count %d\n",
4203 *nbytes, io_parms->length);
4209 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
4210 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4211 } else if (resp_buftype != CIFS_NO_BUFFER) {
4212 *buf = rsp_iov.iov_base;
4213 if (resp_buftype == CIFS_SMALL_BUFFER)
4214 *buf_type = CIFS_SMALL_BUFFER;
4215 else if (resp_buftype == CIFS_LARGE_BUFFER)
4216 *buf_type = CIFS_LARGE_BUFFER;
4222 * Check the mid_state and signature on received buffer (if any), and queue the
4223 * workqueue completion task.
4226 smb2_writev_callback(struct mid_q_entry *mid)
4228 struct cifs_writedata *wdata = mid->callback_data;
4229 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4230 struct TCP_Server_Info *server = wdata->server;
4231 unsigned int written;
4232 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
4233 struct cifs_credits credits = { .value = 0, .instance = 0 };
4235 WARN_ONCE(wdata->server != mid->server,
4236 "wdata server %p != mid server %p",
4237 wdata->server, mid->server);
4239 switch (mid->mid_state) {
4240 case MID_RESPONSE_RECEIVED:
4241 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4242 credits.instance = server->reconnect_instance;
4243 wdata->result = smb2_check_receive(mid, server, 0);
4244 if (wdata->result != 0)
4247 written = le32_to_cpu(rsp->DataLength);
4249 * Mask off high 16 bits when bytes written as returned
4250 * by the server is greater than bytes requested by the
4251 * client. OS/2 servers are known to set incorrect
4254 if (written > wdata->bytes)
4257 if (written < wdata->bytes)
4258 wdata->result = -ENOSPC;
4260 wdata->bytes = written;
4262 case MID_REQUEST_SUBMITTED:
4263 case MID_RETRY_NEEDED:
4264 wdata->result = -EAGAIN;
4266 case MID_RESPONSE_MALFORMED:
4267 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4268 credits.instance = server->reconnect_instance;
4271 wdata->result = -EIO;
4274 #ifdef CONFIG_CIFS_SMB_DIRECT
4276 * If this wdata has a memory registered, the MR can be freed
4277 * The number of MRs available is limited, it's important to recover
4278 * used MR as soon as I/O is finished. Hold MR longer in the later
4279 * I/O process can possibly result in I/O deadlock due to lack of MR
4280 * to send request on I/O retry
4283 smbd_deregister_mr(wdata->mr);
4287 if (wdata->result) {
4288 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4289 trace_smb3_write_err(0 /* no xid */,
4290 wdata->cfile->fid.persistent_fid,
4291 tcon->tid, tcon->ses->Suid, wdata->offset,
4292 wdata->bytes, wdata->result);
4293 if (wdata->result == -ENOSPC)
4294 pr_warn_once("Out of space writing to %s\n",
4297 trace_smb3_write_done(0 /* no xid */,
4298 wdata->cfile->fid.persistent_fid,
4299 tcon->tid, tcon->ses->Suid,
4300 wdata->offset, wdata->bytes);
4302 queue_work(cifsiod_wq, &wdata->work);
4303 DeleteMidQEntry(mid);
4304 add_credits(server, &credits, 0);
4307 /* smb2_async_writev - send an async write, and set up mid to handle result */
4309 smb2_async_writev(struct cifs_writedata *wdata,
4310 void (*release)(struct kref *kref))
4312 int rc = -EACCES, flags = 0;
4313 struct smb2_write_req *req = NULL;
4314 struct smb2_sync_hdr *shdr;
4315 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4316 struct TCP_Server_Info *server = wdata->server;
4318 struct smb_rqst rqst = { };
4319 unsigned int total_len;
4322 server = wdata->server = cifs_pick_channel(tcon->ses);
4324 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
4325 (void **) &req, &total_len);
4329 if (smb3_encryption_required(tcon))
4330 flags |= CIFS_TRANSFORM_REQ;
4332 shdr = (struct smb2_sync_hdr *)req;
4333 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
4335 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
4336 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
4337 req->WriteChannelInfoOffset = 0;
4338 req->WriteChannelInfoLength = 0;
4340 req->Offset = cpu_to_le64(wdata->offset);
4341 req->DataOffset = cpu_to_le16(
4342 offsetof(struct smb2_write_req, Buffer));
4343 req->RemainingBytes = 0;
4345 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
4346 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
4347 #ifdef CONFIG_CIFS_SMB_DIRECT
4349 * If we want to do a server RDMA read, fill in and append
4350 * smbd_buffer_descriptor_v1 to the end of write request
4352 if (server->rdma && !server->sign && wdata->bytes >=
4353 server->smbd_conn->rdma_readwrite_threshold) {
4355 struct smbd_buffer_descriptor_v1 *v1;
4356 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4358 wdata->mr = smbd_register_mr(
4359 server->smbd_conn, wdata->pages,
4360 wdata->nr_pages, wdata->page_offset,
4361 wdata->tailsz, false, need_invalidate);
4364 goto async_writev_out;
4367 req->DataOffset = 0;
4368 if (wdata->nr_pages > 1)
4369 req->RemainingBytes =
4371 (wdata->nr_pages - 1) * wdata->pagesz -
4372 wdata->page_offset + wdata->tailsz
4375 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
4376 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4377 if (need_invalidate)
4378 req->Channel = SMB2_CHANNEL_RDMA_V1;
4379 req->WriteChannelInfoOffset =
4380 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4381 req->WriteChannelInfoLength =
4382 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4383 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4384 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4385 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4386 v1->length = cpu_to_le32(wdata->mr->mr->length);
4389 iov[0].iov_len = total_len - 1;
4390 iov[0].iov_base = (char *)req;
4394 rqst.rq_pages = wdata->pages;
4395 rqst.rq_offset = wdata->page_offset;
4396 rqst.rq_npages = wdata->nr_pages;
4397 rqst.rq_pagesz = wdata->pagesz;
4398 rqst.rq_tailsz = wdata->tailsz;
4399 #ifdef CONFIG_CIFS_SMB_DIRECT
4401 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4405 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4406 wdata->offset, wdata->bytes);
4408 #ifdef CONFIG_CIFS_SMB_DIRECT
4409 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4411 req->Length = cpu_to_le32(wdata->bytes);
4413 req->Length = cpu_to_le32(wdata->bytes);
4416 if (wdata->credits.value > 0) {
4417 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4418 SMB2_MAX_BUFFER_SIZE));
4419 shdr->CreditRequest = cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 8);
4421 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4423 goto async_writev_out;
4425 flags |= CIFS_HAS_CREDITS;
4428 kref_get(&wdata->refcount);
4429 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4430 wdata, flags, &wdata->credits);
4433 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
4434 tcon->tid, tcon->ses->Suid, wdata->offset,
4436 kref_put(&wdata->refcount, release);
4437 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4441 cifs_small_buf_release(req);
4446 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4447 * The length field from io_parms must be at least 1 and indicates a number of
4448 * elements with data to write that begins with position 1 in iov array. All
4449 * data length is specified by count.
4452 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4453 unsigned int *nbytes, struct kvec *iov, int n_vec)
4455 struct smb_rqst rqst;
4457 struct smb2_write_req *req = NULL;
4458 struct smb2_write_rsp *rsp = NULL;
4460 struct kvec rsp_iov;
4462 unsigned int total_len;
4463 struct TCP_Server_Info *server;
4470 if (!io_parms->server)
4471 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4472 server = io_parms->server;
4474 return -ECONNABORTED;
4476 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
4477 (void **) &req, &total_len);
4481 if (smb3_encryption_required(io_parms->tcon))
4482 flags |= CIFS_TRANSFORM_REQ;
4484 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
4486 req->PersistentFileId = io_parms->persistent_fid;
4487 req->VolatileFileId = io_parms->volatile_fid;
4488 req->WriteChannelInfoOffset = 0;
4489 req->WriteChannelInfoLength = 0;
4491 req->Length = cpu_to_le32(io_parms->length);
4492 req->Offset = cpu_to_le64(io_parms->offset);
4493 req->DataOffset = cpu_to_le16(
4494 offsetof(struct smb2_write_req, Buffer));
4495 req->RemainingBytes = 0;
4497 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4498 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4499 io_parms->offset, io_parms->length);
4501 iov[0].iov_base = (char *)req;
4503 iov[0].iov_len = total_len - 1;
4505 memset(&rqst, 0, sizeof(struct smb_rqst));
4507 rqst.rq_nvec = n_vec + 1;
4509 rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
4511 &resp_buftype, flags, &rsp_iov);
4512 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
4515 trace_smb3_write_err(xid, req->PersistentFileId,
4516 io_parms->tcon->tid,
4517 io_parms->tcon->ses->Suid,
4518 io_parms->offset, io_parms->length, rc);
4519 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
4520 cifs_dbg(VFS, "Send error in write = %d\n", rc);
4522 *nbytes = le32_to_cpu(rsp->DataLength);
4523 trace_smb3_write_done(xid, req->PersistentFileId,
4524 io_parms->tcon->tid,
4525 io_parms->tcon->ses->Suid,
4526 io_parms->offset, *nbytes);
4529 cifs_small_buf_release(req);
4530 free_rsp_buf(resp_buftype, rsp);
4534 int posix_info_sid_size(const void *beg, const void *end)
4542 subauth = *(u8 *)(beg+1);
4543 if (subauth < 1 || subauth > 15)
4546 total = 1 + 1 + 6 + 4*subauth;
4547 if (beg + total > end)
4553 int posix_info_parse(const void *beg, const void *end,
4554 struct smb2_posix_info_parsed *out)
4558 int owner_len, group_len;
4560 const void *owner_sid;
4561 const void *group_sid;
4564 /* if no end bound given, assume payload to be correct */
4566 const struct smb2_posix_info *p = beg;
4568 end = beg + le32_to_cpu(p->NextEntryOffset);
4569 /* last element will have a 0 offset, pick a sensible bound */
4574 /* check base buf */
4575 if (beg + sizeof(struct smb2_posix_info) > end)
4577 total_len = sizeof(struct smb2_posix_info);
4579 /* check owner sid */
4580 owner_sid = beg + total_len;
4581 owner_len = posix_info_sid_size(owner_sid, end);
4584 total_len += owner_len;
4586 /* check group sid */
4587 group_sid = beg + total_len;
4588 group_len = posix_info_sid_size(group_sid, end);
4591 total_len += group_len;
4593 /* check name len */
4594 if (beg + total_len + 4 > end)
4596 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
4597 if (name_len < 1 || name_len > 0xFFFF)
4602 name = beg + total_len;
4603 if (name + name_len > end)
4605 total_len += name_len;
4609 out->size = total_len;
4610 out->name_len = name_len;
4612 memcpy(&out->owner, owner_sid, owner_len);
4613 memcpy(&out->group, group_sid, group_len);
4618 static int posix_info_extra_size(const void *beg, const void *end)
4620 int len = posix_info_parse(beg, end, NULL);
4624 return len - sizeof(struct smb2_posix_info);
4628 num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
4632 unsigned int entrycount = 0;
4633 unsigned int next_offset = 0;
4635 FILE_DIRECTORY_INFO *dir_info;
4637 if (bufstart == NULL)
4640 entryptr = bufstart;
4643 if (entryptr + next_offset < entryptr ||
4644 entryptr + next_offset > end_of_buf ||
4645 entryptr + next_offset + size > end_of_buf) {
4646 cifs_dbg(VFS, "malformed search entry would overflow\n");
4650 entryptr = entryptr + next_offset;
4651 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4653 if (infotype == SMB_FIND_FILE_POSIX_INFO)
4654 len = posix_info_extra_size(entryptr, end_of_buf);
4656 len = le32_to_cpu(dir_info->FileNameLength);
4659 entryptr + len < entryptr ||
4660 entryptr + len > end_of_buf ||
4661 entryptr + len + size > end_of_buf) {
4662 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4667 *lastentry = entryptr;
4670 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
4681 int SMB2_query_directory_init(const unsigned int xid,
4682 struct cifs_tcon *tcon,
4683 struct TCP_Server_Info *server,
4684 struct smb_rqst *rqst,
4685 u64 persistent_fid, u64 volatile_fid,
4686 int index, int info_level)
4688 struct smb2_query_directory_req *req;
4689 unsigned char *bufptr;
4690 __le16 asteriks = cpu_to_le16('*');
4691 unsigned int output_size = CIFSMaxBufSize -
4692 MAX_SMB2_CREATE_RESPONSE_SIZE -
4693 MAX_SMB2_CLOSE_RESPONSE_SIZE;
4694 unsigned int total_len;
4695 struct kvec *iov = rqst->rq_iov;
4698 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
4699 (void **) &req, &total_len);
4703 switch (info_level) {
4704 case SMB_FIND_FILE_DIRECTORY_INFO:
4705 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4707 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4708 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4710 case SMB_FIND_FILE_POSIX_INFO:
4711 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
4714 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4719 req->FileIndex = cpu_to_le32(index);
4720 req->PersistentFileId = persistent_fid;
4721 req->VolatileFileId = volatile_fid;
4724 bufptr = req->Buffer;
4725 memcpy(bufptr, &asteriks, len);
4727 req->FileNameOffset =
4728 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
4729 req->FileNameLength = cpu_to_le16(len);
4731 * BB could be 30 bytes or so longer if we used SMB2 specific
4732 * buffer lengths, but this is safe and close enough.
4734 output_size = min_t(unsigned int, output_size, server->maxBuf);
4735 output_size = min_t(unsigned int, output_size, 2 << 15);
4736 req->OutputBufferLength = cpu_to_le32(output_size);
4738 iov[0].iov_base = (char *)req;
4740 iov[0].iov_len = total_len - 1;
4742 iov[1].iov_base = (char *)(req->Buffer);
4743 iov[1].iov_len = len;
4745 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4746 tcon->ses->Suid, index, output_size);
4751 void SMB2_query_directory_free(struct smb_rqst *rqst)
4753 if (rqst && rqst->rq_iov) {
4754 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4759 smb2_parse_query_directory(struct cifs_tcon *tcon,
4760 struct kvec *rsp_iov,
4762 struct cifs_search_info *srch_inf)
4764 struct smb2_query_directory_rsp *rsp;
4765 size_t info_buf_size;
4769 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
4771 switch (srch_inf->info_level) {
4772 case SMB_FIND_FILE_DIRECTORY_INFO:
4773 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4775 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4776 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4778 case SMB_FIND_FILE_POSIX_INFO:
4779 /* note that posix payload are variable size */
4780 info_buf_size = sizeof(struct smb2_posix_info);
4783 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4784 srch_inf->info_level);
4788 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4789 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
4792 cifs_tcon_dbg(VFS, "bad info payload");
4796 srch_inf->unicode = true;
4798 if (srch_inf->ntwrk_buf_start) {
4799 if (srch_inf->smallBuf)
4800 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4802 cifs_buf_release(srch_inf->ntwrk_buf_start);
4804 srch_inf->ntwrk_buf_start = (char *)rsp;
4805 srch_inf->srch_entries_start = srch_inf->last_entry =
4806 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4807 end_of_smb = rsp_iov->iov_len + (char *)rsp;
4809 srch_inf->entries_in_buffer = num_entries(
4810 srch_inf->info_level,
4811 srch_inf->srch_entries_start,
4813 &srch_inf->last_entry,
4816 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4817 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4818 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4819 srch_inf->srch_entries_start, srch_inf->last_entry);
4820 if (resp_buftype == CIFS_LARGE_BUFFER)
4821 srch_inf->smallBuf = false;
4822 else if (resp_buftype == CIFS_SMALL_BUFFER)
4823 srch_inf->smallBuf = true;
4825 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
4831 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4832 u64 persistent_fid, u64 volatile_fid, int index,
4833 struct cifs_search_info *srch_inf)
4835 struct smb_rqst rqst;
4836 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
4837 struct smb2_query_directory_rsp *rsp = NULL;
4838 int resp_buftype = CIFS_NO_BUFFER;
4839 struct kvec rsp_iov;
4841 struct cifs_ses *ses = tcon->ses;
4842 struct TCP_Server_Info *server = cifs_pick_channel(ses);
4845 if (!ses || !(ses->server))
4848 if (smb3_encryption_required(tcon))
4849 flags |= CIFS_TRANSFORM_REQ;
4851 memset(&rqst, 0, sizeof(struct smb_rqst));
4852 memset(&iov, 0, sizeof(iov));
4854 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
4856 rc = SMB2_query_directory_init(xid, tcon, server,
4857 &rqst, persistent_fid,
4858 volatile_fid, index,
4859 srch_inf->info_level);
4863 rc = cifs_send_recv(xid, ses, server,
4864 &rqst, &resp_buftype, flags, &rsp_iov);
4865 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
4868 if (rc == -ENODATA &&
4869 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
4870 trace_smb3_query_dir_done(xid, persistent_fid,
4871 tcon->tid, tcon->ses->Suid, index, 0);
4872 srch_inf->endOfSearch = true;
4875 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4876 tcon->ses->Suid, index, 0, rc);
4877 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
4882 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
4885 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4886 tcon->ses->Suid, index, 0, rc);
4889 resp_buftype = CIFS_NO_BUFFER;
4891 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4892 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
4895 SMB2_query_directory_free(&rqst);
4896 free_rsp_buf(resp_buftype, rsp);
4901 SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
4902 struct smb_rqst *rqst,
4903 u64 persistent_fid, u64 volatile_fid, u32 pid,
4904 u8 info_class, u8 info_type, u32 additional_info,
4905 void **data, unsigned int *size)
4907 struct smb2_set_info_req *req;
4908 struct kvec *iov = rqst->rq_iov;
4909 unsigned int i, total_len;
4912 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
4913 (void **) &req, &total_len);
4917 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4918 req->InfoType = info_type;
4919 req->FileInfoClass = info_class;
4920 req->PersistentFileId = persistent_fid;
4921 req->VolatileFileId = volatile_fid;
4922 req->AdditionalInformation = cpu_to_le32(additional_info);
4925 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
4926 req->BufferLength = cpu_to_le32(*size);
4928 memcpy(req->Buffer, *data, *size);
4931 iov[0].iov_base = (char *)req;
4933 iov[0].iov_len = total_len - 1;
4935 for (i = 1; i < rqst->rq_nvec; i++) {
4936 le32_add_cpu(&req->BufferLength, size[i]);
4937 iov[i].iov_base = (char *)data[i];
4938 iov[i].iov_len = size[i];
4945 SMB2_set_info_free(struct smb_rqst *rqst)
4947 if (rqst && rqst->rq_iov)
4948 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
4952 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4953 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4954 u8 info_type, u32 additional_info, unsigned int num,
4955 void **data, unsigned int *size)
4957 struct smb_rqst rqst;
4958 struct smb2_set_info_rsp *rsp = NULL;
4960 struct kvec rsp_iov;
4963 struct cifs_ses *ses = tcon->ses;
4964 struct TCP_Server_Info *server = cifs_pick_channel(ses);
4967 if (!ses || !server)
4973 if (smb3_encryption_required(tcon))
4974 flags |= CIFS_TRANSFORM_REQ;
4976 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4980 memset(&rqst, 0, sizeof(struct smb_rqst));
4984 rc = SMB2_set_info_init(tcon, server,
4985 &rqst, persistent_fid, volatile_fid, pid,
4986 info_class, info_type, additional_info,
4994 rc = cifs_send_recv(xid, ses, server,
4995 &rqst, &resp_buftype, flags,
4997 SMB2_set_info_free(&rqst);
4998 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
5001 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
5002 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
5003 ses->Suid, info_class, (__u32)info_type, rc);
5006 free_rsp_buf(resp_buftype, rsp);
5012 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
5013 u64 volatile_fid, u32 pid, __le64 *eof)
5015 struct smb2_file_eof_info info;
5019 info.EndOfFile = *eof;
5022 size = sizeof(struct smb2_file_eof_info);
5024 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5025 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
5026 0, 1, &data, &size);
5030 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
5031 u64 persistent_fid, u64 volatile_fid,
5032 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
5034 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5035 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
5036 1, (void **)&pnntsd, &pacllen);
5040 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
5041 u64 persistent_fid, u64 volatile_fid,
5042 struct smb2_file_full_ea_info *buf, int len)
5044 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
5045 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
5046 0, 1, (void **)&buf, &len);
5050 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
5051 const u64 persistent_fid, const u64 volatile_fid,
5054 struct smb_rqst rqst;
5056 struct smb2_oplock_break *req = NULL;
5057 struct cifs_ses *ses = tcon->ses;
5058 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5059 int flags = CIFS_OBREAK_OP;
5060 unsigned int total_len;
5062 struct kvec rsp_iov;
5065 cifs_dbg(FYI, "SMB2_oplock_break\n");
5066 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5067 (void **) &req, &total_len);
5071 if (smb3_encryption_required(tcon))
5072 flags |= CIFS_TRANSFORM_REQ;
5074 req->VolatileFid = volatile_fid;
5075 req->PersistentFid = persistent_fid;
5076 req->OplockLevel = oplock_level;
5077 req->sync_hdr.CreditRequest = cpu_to_le16(1);
5079 flags |= CIFS_NO_RSP_BUF;
5081 iov[0].iov_base = (char *)req;
5082 iov[0].iov_len = total_len;
5084 memset(&rqst, 0, sizeof(struct smb_rqst));
5088 rc = cifs_send_recv(xid, ses, server,
5089 &rqst, &resp_buf_type, flags, &rsp_iov);
5090 cifs_small_buf_release(req);
5093 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5094 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
5101 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
5102 struct kstatfs *kst)
5104 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
5105 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
5106 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
5107 kst->f_bfree = kst->f_bavail =
5108 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
5113 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
5114 struct kstatfs *kst)
5116 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
5117 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
5118 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
5119 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
5120 kst->f_bavail = kst->f_bfree;
5122 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
5123 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5124 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
5125 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5126 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
5132 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
5133 struct TCP_Server_Info *server,
5134 int level, int outbuf_len, u64 persistent_fid,
5138 struct smb2_query_info_req *req;
5139 unsigned int total_len;
5141 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
5143 if ((tcon->ses == NULL) || server == NULL)
5146 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
5147 (void **) &req, &total_len);
5151 req->InfoType = SMB2_O_INFO_FILESYSTEM;
5152 req->FileInfoClass = level;
5153 req->PersistentFileId = persistent_fid;
5154 req->VolatileFileId = volatile_fid;
5156 req->InputBufferOffset =
5157 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
5158 req->OutputBufferLength = cpu_to_le32(
5159 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
5161 iov->iov_base = (char *)req;
5162 iov->iov_len = total_len;
5167 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
5168 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5170 struct smb_rqst rqst;
5171 struct smb2_query_info_rsp *rsp = NULL;
5173 struct kvec rsp_iov;
5176 struct cifs_ses *ses = tcon->ses;
5177 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5178 FILE_SYSTEM_POSIX_INFO *info = NULL;
5181 rc = build_qfs_info_req(&iov, tcon, server,
5182 FS_POSIX_INFORMATION,
5183 sizeof(FILE_SYSTEM_POSIX_INFO),
5184 persistent_fid, volatile_fid);
5188 if (smb3_encryption_required(tcon))
5189 flags |= CIFS_TRANSFORM_REQ;
5191 memset(&rqst, 0, sizeof(struct smb_rqst));
5195 rc = cifs_send_recv(xid, ses, server,
5196 &rqst, &resp_buftype, flags, &rsp_iov);
5197 cifs_small_buf_release(iov.iov_base);
5199 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5200 goto posix_qfsinf_exit;
5202 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5204 info = (FILE_SYSTEM_POSIX_INFO *)(
5205 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5206 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5207 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5208 sizeof(FILE_SYSTEM_POSIX_INFO));
5210 copy_posix_fs_info_to_kstatfs(info, fsdata);
5213 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5218 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5219 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5221 struct smb_rqst rqst;
5222 struct smb2_query_info_rsp *rsp = NULL;
5224 struct kvec rsp_iov;
5227 struct cifs_ses *ses = tcon->ses;
5228 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5229 struct smb2_fs_full_size_info *info = NULL;
5232 rc = build_qfs_info_req(&iov, tcon, server,
5233 FS_FULL_SIZE_INFORMATION,
5234 sizeof(struct smb2_fs_full_size_info),
5235 persistent_fid, volatile_fid);
5239 if (smb3_encryption_required(tcon))
5240 flags |= CIFS_TRANSFORM_REQ;
5242 memset(&rqst, 0, sizeof(struct smb_rqst));
5246 rc = cifs_send_recv(xid, ses, server,
5247 &rqst, &resp_buftype, flags, &rsp_iov);
5248 cifs_small_buf_release(iov.iov_base);
5250 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5253 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5255 info = (struct smb2_fs_full_size_info *)(
5256 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5257 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5258 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5259 sizeof(struct smb2_fs_full_size_info));
5261 smb2_copy_fs_info_to_kstatfs(info, fsdata);
5264 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5269 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
5270 u64 persistent_fid, u64 volatile_fid, int level)
5272 struct smb_rqst rqst;
5273 struct smb2_query_info_rsp *rsp = NULL;
5275 struct kvec rsp_iov;
5277 int resp_buftype, max_len, min_len;
5278 struct cifs_ses *ses = tcon->ses;
5279 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5280 unsigned int rsp_len, offset;
5283 if (level == FS_DEVICE_INFORMATION) {
5284 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5285 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5286 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5287 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5288 min_len = MIN_FS_ATTR_INFO_SIZE;
5289 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5290 max_len = sizeof(struct smb3_fs_ss_info);
5291 min_len = sizeof(struct smb3_fs_ss_info);
5292 } else if (level == FS_VOLUME_INFORMATION) {
5293 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5294 min_len = sizeof(struct smb3_fs_vol_info);
5296 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
5300 rc = build_qfs_info_req(&iov, tcon, server,
5302 persistent_fid, volatile_fid);
5306 if (smb3_encryption_required(tcon))
5307 flags |= CIFS_TRANSFORM_REQ;
5309 memset(&rqst, 0, sizeof(struct smb_rqst));
5313 rc = cifs_send_recv(xid, ses, server,
5314 &rqst, &resp_buftype, flags, &rsp_iov);
5315 cifs_small_buf_release(iov.iov_base);
5317 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5320 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5322 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5323 offset = le16_to_cpu(rsp->OutputBufferOffset);
5324 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
5328 if (level == FS_ATTRIBUTE_INFORMATION)
5329 memcpy(&tcon->fsAttrInfo, offset
5330 + (char *)rsp, min_t(unsigned int,
5332 else if (level == FS_DEVICE_INFORMATION)
5333 memcpy(&tcon->fsDevInfo, offset
5334 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
5335 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5336 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
5337 (offset + (char *)rsp);
5338 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5339 tcon->perf_sector_size =
5340 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
5341 } else if (level == FS_VOLUME_INFORMATION) {
5342 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5343 (offset + (char *)rsp);
5344 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5345 tcon->vol_create_time = vol_info->VolumeCreationTime;
5349 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5354 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5355 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5356 const __u32 num_lock, struct smb2_lock_element *buf)
5358 struct smb_rqst rqst;
5360 struct smb2_lock_req *req = NULL;
5362 struct kvec rsp_iov;
5365 int flags = CIFS_NO_RSP_BUF;
5366 unsigned int total_len;
5367 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5369 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
5371 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
5372 (void **) &req, &total_len);
5376 if (smb3_encryption_required(tcon))
5377 flags |= CIFS_TRANSFORM_REQ;
5379 req->sync_hdr.ProcessId = cpu_to_le32(pid);
5380 req->LockCount = cpu_to_le16(num_lock);
5382 req->PersistentFileId = persist_fid;
5383 req->VolatileFileId = volatile_fid;
5385 count = num_lock * sizeof(struct smb2_lock_element);
5387 iov[0].iov_base = (char *)req;
5388 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
5389 iov[1].iov_base = (char *)buf;
5390 iov[1].iov_len = count;
5392 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
5394 memset(&rqst, 0, sizeof(struct smb_rqst));
5398 rc = cifs_send_recv(xid, tcon->ses, server,
5399 &rqst, &resp_buf_type, flags,
5401 cifs_small_buf_release(req);
5403 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
5404 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
5405 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
5406 tcon->ses->Suid, rc);
5413 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
5414 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5415 const __u64 length, const __u64 offset, const __u32 lock_flags,
5418 struct smb2_lock_element lock;
5420 lock.Offset = cpu_to_le64(offset);
5421 lock.Length = cpu_to_le64(length);
5422 lock.Flags = cpu_to_le32(lock_flags);
5423 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5424 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5426 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5430 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5431 __u8 *lease_key, const __le32 lease_state)
5433 struct smb_rqst rqst;
5435 struct smb2_lease_ack *req = NULL;
5436 struct cifs_ses *ses = tcon->ses;
5437 int flags = CIFS_OBREAK_OP;
5438 unsigned int total_len;
5440 struct kvec rsp_iov;
5442 __u64 *please_key_high;
5443 __u64 *please_key_low;
5444 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5446 cifs_dbg(FYI, "SMB2_lease_break\n");
5447 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5448 (void **) &req, &total_len);
5452 if (smb3_encryption_required(tcon))
5453 flags |= CIFS_TRANSFORM_REQ;
5455 req->sync_hdr.CreditRequest = cpu_to_le16(1);
5456 req->StructureSize = cpu_to_le16(36);
5459 memcpy(req->LeaseKey, lease_key, 16);
5460 req->LeaseState = lease_state;
5462 flags |= CIFS_NO_RSP_BUF;
5464 iov[0].iov_base = (char *)req;
5465 iov[0].iov_len = total_len;
5467 memset(&rqst, 0, sizeof(struct smb_rqst));
5471 rc = cifs_send_recv(xid, ses, server,
5472 &rqst, &resp_buf_type, flags, &rsp_iov);
5473 cifs_small_buf_release(req);
5475 please_key_low = (__u64 *)lease_key;
5476 please_key_high = (__u64 *)(lease_key+8);
5478 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5479 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5480 ses->Suid, *please_key_low, *please_key_high, rc);
5481 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
5483 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5484 ses->Suid, *please_key_low, *please_key_high);