4 * Copyright (C) International Business Machines Corp., 2009, 2013
9 * Contains the routines for constructing the SMB2 PDUs themselves
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
32 #include <linux/kernel.h>
33 #include <linux/vfs.h>
34 #include <linux/task_io_accounting_ops.h>
35 #include <linux/uaccess.h>
36 #include <linux/uuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/xattr.h>
42 #include "cifsproto.h"
43 #include "smb2proto.h"
44 #include "cifs_unicode.h"
45 #include "cifs_debug.h"
47 #include "smb2status.h"
50 #include "cifs_spnego.h"
51 #include "smbdirect.h"
53 #ifdef CONFIG_CIFS_DFS_UPCALL
54 #include "dfs_cache.h"
58 * The following table defines the expected "StructureSize" of SMB2 requests
59 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
61 * Note that commands are defined in smb2pdu.h in le16 but the array below is
62 * indexed by command in host byte order.
64 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
65 /* SMB2_NEGOTIATE */ 36,
66 /* SMB2_SESSION_SETUP */ 25,
68 /* SMB2_TREE_CONNECT */ 9,
69 /* SMB2_TREE_DISCONNECT */ 4,
79 /* SMB2_QUERY_DIRECTORY */ 33,
80 /* SMB2_CHANGE_NOTIFY */ 32,
81 /* SMB2_QUERY_INFO */ 41,
82 /* SMB2_SET_INFO */ 33,
83 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
86 int smb3_encryption_required(const struct cifs_tcon *tcon)
88 if (!tcon || !tcon->ses)
90 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
91 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
94 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
100 smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
101 const struct cifs_tcon *tcon,
102 struct TCP_Server_Info *server)
104 shdr->ProtocolId = SMB2_PROTO_NUMBER;
105 shdr->StructureSize = cpu_to_le16(64);
106 shdr->Command = smb2_cmd;
108 spin_lock(&server->req_lock);
109 /* Request up to 10 credits but don't go over the limit. */
110 if (server->credits >= server->max_credits)
111 shdr->CreditRequest = cpu_to_le16(0);
113 shdr->CreditRequest = cpu_to_le16(
114 min_t(int, server->max_credits -
115 server->credits, 10));
116 spin_unlock(&server->req_lock);
118 shdr->CreditRequest = cpu_to_le16(2);
120 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
125 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
126 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
127 if (server && (server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
128 shdr->CreditCharge = cpu_to_le16(1);
129 /* else CreditCharge MBZ */
131 shdr->TreeId = tcon->tid;
132 /* Uid is not converted */
134 shdr->SessionId = tcon->ses->Suid;
137 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
138 * to pass the path on the Open SMB prefixed by \\server\share.
139 * Not sure when we would need to do the augmented path (if ever) and
140 * setting this flag breaks the SMB2 open operation since it is
141 * illegal to send an empty path name (without \\server\share prefix)
142 * when the DFS flag is set in the SMB open header. We could
143 * consider setting the flag on all operations other than open
144 * but it is safer to net set it for now.
146 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
147 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
149 if (server && server->sign && !smb3_encryption_required(tcon))
150 shdr->Flags |= SMB2_FLAGS_SIGNED;
156 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
157 struct TCP_Server_Info *server)
160 struct nls_table *nls_codepage;
161 struct cifs_ses *ses;
165 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
166 * check for tcp and smb session status done differently
167 * for those three - in the calling routine.
172 if (smb2_command == SMB2_TREE_CONNECT)
175 if (tcon->tidStatus == CifsExiting) {
177 * only tree disconnect, open, and write,
178 * (and ulogoff which does not have tcon)
179 * are allowed as we start force umount.
181 if ((smb2_command != SMB2_WRITE) &&
182 (smb2_command != SMB2_CREATE) &&
183 (smb2_command != SMB2_TREE_DISCONNECT)) {
184 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
189 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
190 (!tcon->ses->server) || !server)
194 retries = server->nr_targets;
197 * Give demultiplex thread up to 10 seconds to each target available for
198 * reconnect -- should be greater than cifs socket timeout which is 7
201 while (server->tcpStatus == CifsNeedReconnect) {
203 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
204 * here since they are implicitly done when session drops.
206 switch (smb2_command) {
208 * BB Should we keep oplock break and add flush to exceptions?
210 case SMB2_TREE_DISCONNECT:
213 case SMB2_OPLOCK_BREAK:
217 rc = wait_event_interruptible_timeout(server->response_q,
218 (server->tcpStatus != CifsNeedReconnect),
221 cifs_dbg(FYI, "%s: aborting reconnect due to a received signal by the process\n",
226 /* are we still trying to reconnect? */
227 if (server->tcpStatus != CifsNeedReconnect)
230 if (retries && --retries)
234 * on "soft" mounts we wait once. Hard mounts keep
235 * retrying until process is killed or server comes
239 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
242 retries = server->nr_targets;
245 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
248 nls_codepage = load_nls_default();
251 * need to prevent multiple threads trying to simultaneously reconnect
252 * the same SMB session
254 mutex_lock(&tcon->ses->session_mutex);
257 * Recheck after acquire mutex. If another thread is negotiating
258 * and the server never sends an answer the socket will be closed
259 * and tcpStatus set to reconnect.
261 if (server->tcpStatus == CifsNeedReconnect) {
263 mutex_unlock(&tcon->ses->session_mutex);
268 * If we are reconnecting an extra channel, bind
270 if (server->is_channel) {
272 ses->binding_chan = cifs_ses_find_chan(ses, server);
275 rc = cifs_negotiate_protocol(0, tcon->ses);
276 if (!rc && tcon->ses->need_reconnect) {
277 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
278 if ((rc == -EACCES) && !tcon->retry) {
280 ses->binding = false;
281 ses->binding_chan = NULL;
282 mutex_unlock(&tcon->ses->session_mutex);
287 * End of channel binding
289 ses->binding = false;
290 ses->binding_chan = NULL;
292 if (rc || !tcon->need_reconnect) {
293 mutex_unlock(&tcon->ses->session_mutex);
297 cifs_mark_open_files_invalid(tcon);
298 if (tcon->use_persistent)
299 tcon->need_reopen_files = true;
301 rc = cifs_tree_connect(0, tcon, nls_codepage);
302 mutex_unlock(&tcon->ses->session_mutex);
304 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
306 /* If sess reconnected but tcon didn't, something strange ... */
307 pr_warn_once("reconnect tcon failed rc = %d\n", rc);
311 if (smb2_command != SMB2_INTERNAL_CMD)
312 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
314 atomic_inc(&tconInfoReconnectCount);
317 * Check if handle based operation so we know whether we can continue
318 * or not without returning to caller to reset file handle.
321 * BB Is flush done by server on drop of tcp session? Should we special
322 * case it and skip above?
324 switch (smb2_command) {
330 case SMB2_QUERY_DIRECTORY:
331 case SMB2_CHANGE_NOTIFY:
332 case SMB2_QUERY_INFO:
337 unload_nls(nls_codepage);
342 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon,
343 struct TCP_Server_Info *server,
345 unsigned int *total_len)
347 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
348 /* lookup word count ie StructureSize from table */
349 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
352 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
353 * largest operations (Create)
357 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon, server);
358 spdu->StructureSize2 = cpu_to_le16(parmsize);
360 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
364 * Allocate and return pointer to an SMB request hdr, and set basic
365 * SMB information in the SMB header. If the return code is zero, this
366 * function must have filled in request_buf pointer.
368 static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
369 struct TCP_Server_Info *server,
370 void **request_buf, unsigned int *total_len)
372 /* BB eventually switch this to SMB2 specific small buf size */
373 if (smb2_command == SMB2_SET_INFO)
374 *request_buf = cifs_buf_get();
376 *request_buf = cifs_small_buf_get();
377 if (*request_buf == NULL) {
378 /* BB should we add a retry in here if not a writepage? */
382 fill_small_buf(smb2_command, tcon, server,
383 (struct smb2_sync_hdr *)(*request_buf),
387 uint16_t com_code = le16_to_cpu(smb2_command);
388 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
389 cifs_stats_inc(&tcon->num_smbs_sent);
395 static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
396 struct TCP_Server_Info *server,
397 void **request_buf, unsigned int *total_len)
401 rc = smb2_reconnect(smb2_command, tcon, server);
405 return __smb2_plain_req_init(smb2_command, tcon, server, request_buf,
409 static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
410 struct TCP_Server_Info *server,
411 void **request_buf, unsigned int *total_len)
413 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
414 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
415 return __smb2_plain_req_init(SMB2_IOCTL, tcon, server,
416 request_buf, total_len);
418 return smb2_plain_req_init(SMB2_IOCTL, tcon, server,
419 request_buf, total_len);
422 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
425 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
427 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
428 pneg_ctxt->DataLength = cpu_to_le16(38);
429 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
430 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_LINUX_CLIENT_SALT_SIZE);
431 get_random_bytes(pneg_ctxt->Salt, SMB311_LINUX_CLIENT_SALT_SIZE);
432 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
436 build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
438 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
439 pneg_ctxt->DataLength =
440 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
441 - sizeof(struct smb2_neg_context));
442 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
443 pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
444 pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
445 pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
449 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
451 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
452 if (require_gcm_256) {
453 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + 1 cipher */
454 pneg_ctxt->CipherCount = cpu_to_le16(1);
455 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES256_GCM;
456 } else if (enable_gcm_256) {
457 pneg_ctxt->DataLength = cpu_to_le16(8); /* Cipher Count + 3 ciphers */
458 pneg_ctxt->CipherCount = cpu_to_le16(3);
459 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
460 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES256_GCM;
461 pneg_ctxt->Ciphers[2] = SMB2_ENCRYPTION_AES128_CCM;
463 pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + 2 ciphers */
464 pneg_ctxt->CipherCount = cpu_to_le16(2);
465 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
466 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
471 build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
473 struct nls_table *cp = load_nls_default();
475 pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
477 /* copy up to max of first 100 bytes of server name to NetName field */
478 pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
479 /* context size is DataLength + minimal smb2_neg_context */
480 return DIV_ROUND_UP(le16_to_cpu(pneg_ctxt->DataLength) +
481 sizeof(struct smb2_neg_context), 8) * 8;
485 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
487 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
488 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
489 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
490 pneg_ctxt->Name[0] = 0x93;
491 pneg_ctxt->Name[1] = 0xAD;
492 pneg_ctxt->Name[2] = 0x25;
493 pneg_ctxt->Name[3] = 0x50;
494 pneg_ctxt->Name[4] = 0x9C;
495 pneg_ctxt->Name[5] = 0xB4;
496 pneg_ctxt->Name[6] = 0x11;
497 pneg_ctxt->Name[7] = 0xE7;
498 pneg_ctxt->Name[8] = 0xB4;
499 pneg_ctxt->Name[9] = 0x23;
500 pneg_ctxt->Name[10] = 0x83;
501 pneg_ctxt->Name[11] = 0xDE;
502 pneg_ctxt->Name[12] = 0x96;
503 pneg_ctxt->Name[13] = 0x8B;
504 pneg_ctxt->Name[14] = 0xCD;
505 pneg_ctxt->Name[15] = 0x7C;
509 assemble_neg_contexts(struct smb2_negotiate_req *req,
510 struct TCP_Server_Info *server, unsigned int *total_len)
513 unsigned int ctxt_len;
515 if (*total_len > 200) {
516 /* In case length corrupted don't want to overrun smb buffer */
517 cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
522 * round up total_len of fixed part of SMB3 negotiate request to 8
523 * byte boundary before adding negotiate contexts
525 *total_len = roundup(*total_len, 8);
527 pneg_ctxt = (*total_len) + (char *)req;
528 req->NegotiateContextOffset = cpu_to_le32(*total_len);
530 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
531 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
532 *total_len += ctxt_len;
533 pneg_ctxt += ctxt_len;
535 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
536 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
537 *total_len += ctxt_len;
538 pneg_ctxt += ctxt_len;
540 if (server->compress_algorithm) {
541 build_compression_ctxt((struct smb2_compression_capabilities_context *)
543 ctxt_len = DIV_ROUND_UP(
544 sizeof(struct smb2_compression_capabilities_context),
546 *total_len += ctxt_len;
547 pneg_ctxt += ctxt_len;
548 req->NegotiateContextCount = cpu_to_le16(5);
550 req->NegotiateContextCount = cpu_to_le16(4);
552 ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
554 *total_len += ctxt_len;
555 pneg_ctxt += ctxt_len;
557 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
558 *total_len += sizeof(struct smb2_posix_neg_context);
561 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
563 unsigned int len = le16_to_cpu(ctxt->DataLength);
565 /* If invalid preauth context warn but use what we requested, SHA-512 */
566 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
567 pr_warn_once("server sent bad preauth context\n");
569 } else if (len < MIN_PREAUTH_CTXT_DATA_LEN + le16_to_cpu(ctxt->SaltLength)) {
570 pr_warn_once("server sent invalid SaltLength\n");
573 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
574 pr_warn_once("Invalid SMB3 hash algorithm count\n");
575 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
576 pr_warn_once("unknown SMB3 hash algorithm\n");
579 static void decode_compress_ctx(struct TCP_Server_Info *server,
580 struct smb2_compression_capabilities_context *ctxt)
582 unsigned int len = le16_to_cpu(ctxt->DataLength);
584 /* sizeof compress context is a one element compression capbility struct */
586 pr_warn_once("server sent bad compression cntxt\n");
589 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
590 pr_warn_once("Invalid SMB3 compress algorithm count\n");
593 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
594 pr_warn_once("unknown compression algorithm\n");
597 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
600 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
601 struct smb2_encryption_neg_context *ctxt)
603 unsigned int len = le16_to_cpu(ctxt->DataLength);
605 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
606 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
607 pr_warn_once("server sent bad crypto ctxt len\n");
611 if (le16_to_cpu(ctxt->CipherCount) != 1) {
612 pr_warn_once("Invalid SMB3.11 cipher count\n");
615 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
616 if (require_gcm_256) {
617 if (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM) {
618 cifs_dbg(VFS, "Server does not support requested encryption type (AES256 GCM)\n");
621 } else if (ctxt->Ciphers[0] == 0) {
623 * e.g. if server only supported AES256_CCM (very unlikely)
624 * or server supported no encryption types or had all disabled.
625 * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case
626 * in which mount requested encryption ("seal") checks later
627 * on during tree connection will return proper rc, but if
628 * seal not requested by client, since server is allowed to
629 * return 0 to indicate no supported cipher, we can't fail here
631 server->cipher_type = 0;
632 server->capabilities &= ~SMB2_GLOBAL_CAP_ENCRYPTION;
633 pr_warn_once("Server does not support requested encryption types\n");
635 } else if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
636 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM) &&
637 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM)) {
638 /* server returned a cipher we didn't ask for */
639 pr_warn_once("Invalid SMB3.11 cipher returned\n");
642 server->cipher_type = ctxt->Ciphers[0];
643 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
647 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
648 struct TCP_Server_Info *server,
649 unsigned int len_of_smb)
651 struct smb2_neg_context *pctx;
652 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
653 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
654 unsigned int len_of_ctxts, i;
657 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
658 if (len_of_smb <= offset) {
659 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
663 len_of_ctxts = len_of_smb - offset;
665 for (i = 0; i < ctxt_cnt; i++) {
667 /* check that offset is not beyond end of SMB */
668 if (len_of_ctxts == 0)
671 if (len_of_ctxts < sizeof(struct smb2_neg_context))
674 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
675 clen = le16_to_cpu(pctx->DataLength);
676 if (clen > len_of_ctxts)
679 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
680 decode_preauth_context(
681 (struct smb2_preauth_neg_context *)pctx);
682 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
683 rc = decode_encrypt_ctx(server,
684 (struct smb2_encryption_neg_context *)pctx);
685 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
686 decode_compress_ctx(server,
687 (struct smb2_compression_capabilities_context *)pctx);
688 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
689 server->posix_ext_supported = true;
691 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
692 le16_to_cpu(pctx->ContextType));
696 /* offsets must be 8 byte aligned */
697 clen = (clen + 7) & ~0x7;
698 offset += clen + sizeof(struct smb2_neg_context);
699 len_of_ctxts -= clen;
704 static struct create_posix *
705 create_posix_buf(umode_t mode)
707 struct create_posix *buf;
709 buf = kzalloc(sizeof(struct create_posix),
714 buf->ccontext.DataOffset =
715 cpu_to_le16(offsetof(struct create_posix, Mode));
716 buf->ccontext.DataLength = cpu_to_le32(4);
717 buf->ccontext.NameOffset =
718 cpu_to_le16(offsetof(struct create_posix, Name));
719 buf->ccontext.NameLength = cpu_to_le16(16);
721 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
732 buf->Name[10] = 0x83;
733 buf->Name[11] = 0xDE;
734 buf->Name[12] = 0x96;
735 buf->Name[13] = 0x8B;
736 buf->Name[14] = 0xCD;
737 buf->Name[15] = 0x7C;
738 buf->Mode = cpu_to_le32(mode);
739 cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
744 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
746 struct smb2_create_req *req = iov[0].iov_base;
747 unsigned int num = *num_iovec;
749 iov[num].iov_base = create_posix_buf(mode);
750 if (mode == ACL_NO_MODE)
751 cifs_dbg(FYI, "Invalid mode\n");
752 if (iov[num].iov_base == NULL)
754 iov[num].iov_len = sizeof(struct create_posix);
755 if (!req->CreateContextsOffset)
756 req->CreateContextsOffset = cpu_to_le32(
757 sizeof(struct smb2_create_req) +
758 iov[num - 1].iov_len);
759 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
760 *num_iovec = num + 1;
767 * SMB2 Worker functions follow:
769 * The general structure of the worker functions is:
770 * 1) Call smb2_init (assembles SMB2 header)
771 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
772 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
773 * 4) Decode SMB2 command specific fields in the fixed length area
774 * 5) Decode variable length data area (if any for this SMB2 command type)
775 * 6) Call free smb buffer
781 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
783 struct smb_rqst rqst;
784 struct smb2_negotiate_req *req;
785 struct smb2_negotiate_rsp *rsp;
790 struct TCP_Server_Info *server = cifs_ses_server(ses);
791 int blob_offset, blob_length;
793 int flags = CIFS_NEG_OP;
794 unsigned int total_len;
796 cifs_dbg(FYI, "Negotiate protocol\n");
799 WARN(1, "%s: server is NULL!\n", __func__);
803 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
804 (void **) &req, &total_len);
808 req->sync_hdr.SessionId = 0;
810 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
811 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
813 if (strcmp(server->vals->version_string,
814 SMB3ANY_VERSION_STRING) == 0) {
815 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
816 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
817 req->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
818 req->DialectCount = cpu_to_le16(3);
820 } else if (strcmp(server->vals->version_string,
821 SMBDEFAULT_VERSION_STRING) == 0) {
822 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
823 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
824 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
825 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
826 req->DialectCount = cpu_to_le16(4);
829 /* otherwise send specific dialect */
830 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
831 req->DialectCount = cpu_to_le16(1);
835 /* only one of SMB2 signing flags may be set in SMB2 request */
837 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
838 else if (global_secflags & CIFSSEC_MAY_SIGN)
839 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
841 req->SecurityMode = 0;
843 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
845 /* ClientGUID must be zero for SMB2.02 dialect */
846 if (server->vals->protocol_id == SMB20_PROT_ID)
847 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
849 memcpy(req->ClientGUID, server->client_guid,
850 SMB2_CLIENT_GUID_SIZE);
851 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
852 (strcmp(server->vals->version_string,
853 SMB3ANY_VERSION_STRING) == 0) ||
854 (strcmp(server->vals->version_string,
855 SMBDEFAULT_VERSION_STRING) == 0))
856 assemble_neg_contexts(req, server, &total_len);
858 iov[0].iov_base = (char *)req;
859 iov[0].iov_len = total_len;
861 memset(&rqst, 0, sizeof(struct smb_rqst));
865 rc = cifs_send_recv(xid, ses, server,
866 &rqst, &resp_buftype, flags, &rsp_iov);
867 cifs_small_buf_release(req);
868 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
870 * No tcon so can't do
871 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
873 if (rc == -EOPNOTSUPP) {
874 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");
879 if (strcmp(server->vals->version_string,
880 SMB3ANY_VERSION_STRING) == 0) {
881 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
883 "SMB2 dialect returned but not requested\n");
885 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
887 "SMB2.1 dialect returned but not requested\n");
889 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
890 /* ops set to 3.0 by default for default so update */
891 server->ops = &smb311_operations;
892 server->vals = &smb311_values;
894 } else if (strcmp(server->vals->version_string,
895 SMBDEFAULT_VERSION_STRING) == 0) {
896 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
898 "SMB2 dialect returned but not requested\n");
900 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
901 /* ops set to 3.0 by default for default so update */
902 server->ops = &smb21_operations;
903 server->vals = &smb21_values;
904 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
905 server->ops = &smb311_operations;
906 server->vals = &smb311_values;
908 } else if (le16_to_cpu(rsp->DialectRevision) !=
909 server->vals->protocol_id) {
910 /* if requested single dialect ensure returned dialect matched */
911 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
912 le16_to_cpu(rsp->DialectRevision));
916 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
918 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
919 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
920 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
921 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
922 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
923 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
924 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
925 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
926 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
927 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
929 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
930 le16_to_cpu(rsp->DialectRevision));
934 server->dialect = le16_to_cpu(rsp->DialectRevision);
937 * Keep a copy of the hash after negprot. This hash will be
938 * the starting hash value for all sessions made from this
941 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
942 SMB2_PREAUTH_HASH_SIZE);
944 /* SMB2 only has an extended negflavor */
945 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
946 /* set it to the maximum buffer size value we can send with 1 credit */
947 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
948 SMB2_MAX_BUFFER_SIZE);
949 server->max_read = le32_to_cpu(rsp->MaxReadSize);
950 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
951 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
952 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
953 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
955 server->capabilities = le32_to_cpu(rsp->Capabilities);
957 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
959 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
960 (struct smb2_sync_hdr *)rsp);
962 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
964 * ses->sectype = RawNTLMSSP;
965 * but for time being this is our only auth choice so doesn't matter.
966 * We just found a server which sets blob length to zero expecting raw.
968 if (blob_length == 0) {
969 cifs_dbg(FYI, "missing security blob on negprot\n");
970 server->sec_ntlmssp = true;
973 rc = cifs_enable_signing(server, ses->sign);
977 rc = decode_negTokenInit(security_blob, blob_length, server);
984 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
985 if (rsp->NegotiateContextCount)
986 rc = smb311_decode_neg_context(rsp, server,
989 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
992 free_rsp_buf(resp_buftype, rsp);
996 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
999 struct validate_negotiate_info_req *pneg_inbuf;
1000 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
1002 u32 inbuflen; /* max of 4 dialects */
1003 struct TCP_Server_Info *server = tcon->ses->server;
1005 cifs_dbg(FYI, "validate negotiate\n");
1007 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1008 if (server->dialect == SMB311_PROT_ID)
1012 * validation ioctl must be signed, so no point sending this if we
1013 * can not sign it (ie are not known user). Even if signing is not
1014 * required (enabled but not negotiated), in those cases we selectively
1015 * sign just this, the first and only signed request on a connection.
1016 * Having validation of negotiate info helps reduce attack vectors.
1018 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1019 return 0; /* validation requires signing */
1021 if (tcon->ses->user_name == NULL) {
1022 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1023 return 0; /* validation requires signing */
1026 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1027 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1029 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1033 pneg_inbuf->Capabilities =
1034 cpu_to_le32(server->vals->req_capabilities);
1035 memcpy(pneg_inbuf->Guid, server->client_guid,
1036 SMB2_CLIENT_GUID_SIZE);
1038 if (tcon->ses->sign)
1039 pneg_inbuf->SecurityMode =
1040 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1041 else if (global_secflags & CIFSSEC_MAY_SIGN)
1042 pneg_inbuf->SecurityMode =
1043 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1045 pneg_inbuf->SecurityMode = 0;
1048 if (strcmp(server->vals->version_string,
1049 SMB3ANY_VERSION_STRING) == 0) {
1050 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1051 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1052 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
1053 pneg_inbuf->DialectCount = cpu_to_le16(3);
1054 /* SMB 2.1 not included so subtract one dialect from len */
1055 inbuflen = sizeof(*pneg_inbuf) -
1056 (sizeof(pneg_inbuf->Dialects[0]));
1057 } else if (strcmp(server->vals->version_string,
1058 SMBDEFAULT_VERSION_STRING) == 0) {
1059 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1060 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1061 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1062 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1063 pneg_inbuf->DialectCount = cpu_to_le16(4);
1064 /* structure is big enough for 4 dialects */
1065 inbuflen = sizeof(*pneg_inbuf);
1067 /* otherwise specific dialect was requested */
1068 pneg_inbuf->Dialects[0] =
1069 cpu_to_le16(server->vals->protocol_id);
1070 pneg_inbuf->DialectCount = cpu_to_le16(1);
1071 /* structure is big enough for 3 dialects, sending only 1 */
1072 inbuflen = sizeof(*pneg_inbuf) -
1073 sizeof(pneg_inbuf->Dialects[0]) * 2;
1076 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1077 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
1078 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1079 (char **)&pneg_rsp, &rsplen);
1080 if (rc == -EOPNOTSUPP) {
1082 * Old Windows versions or Netapp SMB server can return
1083 * not supported error. Client should accept it.
1085 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1087 goto out_free_inbuf;
1088 } else if (rc != 0) {
1089 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1092 goto out_free_inbuf;
1096 if (rsplen != sizeof(*pneg_rsp)) {
1097 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1100 /* relax check since Mac returns max bufsize allowed on ioctl */
1101 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1105 /* check validate negotiate info response matches what we got earlier */
1106 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1109 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1112 /* do not validate server guid because not saved at negprot time yet */
1114 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1115 SMB2_LARGE_FILES) != server->capabilities)
1118 /* validate negotiate successful */
1120 cifs_dbg(FYI, "validate negotiate info successful\n");
1124 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1133 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1135 switch (requested) {
1142 if (server->sec_ntlmssp &&
1143 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1145 if ((server->sec_kerberos || server->sec_mskerberos) &&
1146 (global_secflags & CIFSSEC_MAY_KRB5))
1154 struct SMB2_sess_data {
1156 struct cifs_ses *ses;
1157 struct nls_table *nls_cp;
1158 void (*func)(struct SMB2_sess_data *);
1160 u64 previous_session;
1162 /* we will send the SMB in three pieces:
1163 * a fixed length beginning part, an optional
1164 * SPNEGO blob (which can be zero length), and a
1165 * last part which will include the strings
1166 * and rest of bcc area. This allows us to avoid
1167 * a large buffer 17K allocation
1174 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1177 struct cifs_ses *ses = sess_data->ses;
1178 struct smb2_sess_setup_req *req;
1179 struct TCP_Server_Info *server = cifs_ses_server(ses);
1180 unsigned int total_len;
1182 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
1188 if (sess_data->ses->binding) {
1189 req->sync_hdr.SessionId = sess_data->ses->Suid;
1190 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1191 req->PreviousSessionId = 0;
1192 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1194 /* First session, not a reauthenticate */
1195 req->sync_hdr.SessionId = 0;
1197 * if reconnect, we need to send previous sess id
1200 req->PreviousSessionId = sess_data->previous_session;
1201 req->Flags = 0; /* MBZ */
1204 /* enough to enable echos and oplocks and one max size write */
1205 req->sync_hdr.CreditRequest = cpu_to_le16(130);
1207 /* only one of SMB2 signing flags may be set in SMB2 request */
1209 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1210 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1211 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1213 req->SecurityMode = 0;
1215 #ifdef CONFIG_CIFS_DFS_UPCALL
1216 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1218 req->Capabilities = 0;
1219 #endif /* DFS_UPCALL */
1221 req->Channel = 0; /* MBZ */
1223 sess_data->iov[0].iov_base = (char *)req;
1225 sess_data->iov[0].iov_len = total_len - 1;
1227 * This variable will be used to clear the buffer
1228 * allocated above in case of any error in the calling function.
1230 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1236 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1238 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1239 sess_data->buf0_type = CIFS_NO_BUFFER;
1243 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1246 struct smb_rqst rqst;
1247 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1248 struct kvec rsp_iov = { NULL, 0 };
1250 /* Testing shows that buffer offset must be at location of Buffer[0] */
1251 req->SecurityBufferOffset =
1252 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1253 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1255 memset(&rqst, 0, sizeof(struct smb_rqst));
1256 rqst.rq_iov = sess_data->iov;
1259 /* BB add code to build os and lm fields */
1260 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1261 cifs_ses_server(sess_data->ses),
1263 &sess_data->buf0_type,
1264 CIFS_LOG_ERROR | CIFS_SESS_OP, &rsp_iov);
1265 cifs_small_buf_release(sess_data->iov[0].iov_base);
1266 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1272 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1275 struct cifs_ses *ses = sess_data->ses;
1276 struct TCP_Server_Info *server = cifs_ses_server(ses);
1278 mutex_lock(&server->srv_mutex);
1279 if (server->ops->generate_signingkey) {
1280 rc = server->ops->generate_signingkey(ses);
1283 "SMB3 session key generation failed\n");
1284 mutex_unlock(&server->srv_mutex);
1288 if (!server->session_estab) {
1289 server->sequence_number = 0x2;
1290 server->session_estab = true;
1292 mutex_unlock(&server->srv_mutex);
1294 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1295 /* keep existing ses state if binding */
1296 if (!ses->binding) {
1297 spin_lock(&GlobalMid_Lock);
1298 ses->status = CifsGood;
1299 ses->need_reconnect = false;
1300 spin_unlock(&GlobalMid_Lock);
1306 #ifdef CONFIG_CIFS_UPCALL
1308 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1311 struct cifs_ses *ses = sess_data->ses;
1312 struct cifs_spnego_msg *msg;
1313 struct key *spnego_key = NULL;
1314 struct smb2_sess_setup_rsp *rsp = NULL;
1316 rc = SMB2_sess_alloc_buffer(sess_data);
1320 spnego_key = cifs_get_spnego_key(ses);
1321 if (IS_ERR(spnego_key)) {
1322 rc = PTR_ERR(spnego_key);
1324 cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
1329 msg = spnego_key->payload.data[0];
1331 * check version field to make sure that cifs.upcall is
1332 * sending us a response in an expected form
1334 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1335 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1336 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1338 goto out_put_spnego_key;
1341 /* keep session key if binding */
1342 if (!ses->binding) {
1343 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1345 if (!ses->auth_key.response) {
1346 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1349 goto out_put_spnego_key;
1351 ses->auth_key.len = msg->sesskey_len;
1354 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1355 sess_data->iov[1].iov_len = msg->secblob_len;
1357 rc = SMB2_sess_sendreceive(sess_data);
1359 goto out_put_spnego_key;
1361 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1362 /* keep session id and flags if binding */
1363 if (!ses->binding) {
1364 ses->Suid = rsp->sync_hdr.SessionId;
1365 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1368 rc = SMB2_sess_establish_session(sess_data);
1370 key_invalidate(spnego_key);
1371 key_put(spnego_key);
1373 sess_data->result = rc;
1374 sess_data->func = NULL;
1375 SMB2_sess_free_buffer(sess_data);
1379 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1381 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1382 sess_data->result = -EOPNOTSUPP;
1383 sess_data->func = NULL;
1388 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1391 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1394 struct cifs_ses *ses = sess_data->ses;
1395 struct smb2_sess_setup_rsp *rsp = NULL;
1396 char *ntlmssp_blob = NULL;
1397 bool use_spnego = false; /* else use raw ntlmssp */
1398 u16 blob_length = 0;
1401 * If memory allocation is successful, caller of this function
1404 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1405 if (!ses->ntlmssp) {
1409 ses->ntlmssp->sesskey_per_smbsess = true;
1411 rc = SMB2_sess_alloc_buffer(sess_data);
1415 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1417 if (ntlmssp_blob == NULL) {
1422 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1424 /* BB eventually need to add this */
1425 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1429 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1430 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1432 sess_data->iov[1].iov_base = ntlmssp_blob;
1433 sess_data->iov[1].iov_len = blob_length;
1435 rc = SMB2_sess_sendreceive(sess_data);
1436 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1438 /* If true, rc here is expected and not an error */
1439 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1440 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1446 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1447 le16_to_cpu(rsp->SecurityBufferOffset)) {
1448 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1449 le16_to_cpu(rsp->SecurityBufferOffset));
1453 rc = decode_ntlmssp_challenge(rsp->Buffer,
1454 le16_to_cpu(rsp->SecurityBufferLength), ses);
1458 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1460 /* keep existing ses id and flags if binding */
1461 if (!ses->binding) {
1462 ses->Suid = rsp->sync_hdr.SessionId;
1463 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1467 kfree(ntlmssp_blob);
1468 SMB2_sess_free_buffer(sess_data);
1470 sess_data->result = 0;
1471 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1475 kfree(ses->ntlmssp);
1476 ses->ntlmssp = NULL;
1477 sess_data->result = rc;
1478 sess_data->func = NULL;
1482 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1485 struct cifs_ses *ses = sess_data->ses;
1486 struct smb2_sess_setup_req *req;
1487 struct smb2_sess_setup_rsp *rsp = NULL;
1488 unsigned char *ntlmssp_blob = NULL;
1489 bool use_spnego = false; /* else use raw ntlmssp */
1490 u16 blob_length = 0;
1492 rc = SMB2_sess_alloc_buffer(sess_data);
1496 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1497 req->sync_hdr.SessionId = ses->Suid;
1499 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1502 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1507 /* BB eventually need to add this */
1508 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1512 sess_data->iov[1].iov_base = ntlmssp_blob;
1513 sess_data->iov[1].iov_len = blob_length;
1515 rc = SMB2_sess_sendreceive(sess_data);
1519 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1521 /* keep existing ses id and flags if binding */
1522 if (!ses->binding) {
1523 ses->Suid = rsp->sync_hdr.SessionId;
1524 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1527 rc = SMB2_sess_establish_session(sess_data);
1528 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1529 if (ses->server->dialect < SMB30_PROT_ID) {
1530 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1532 * The session id is opaque in terms of endianness, so we can't
1533 * print it as a long long. we dump it as we got it on the wire
1535 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1537 cifs_dbg(VFS, "Session Key %*ph\n",
1538 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1539 cifs_dbg(VFS, "Signing Key %*ph\n",
1540 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1544 kfree(ntlmssp_blob);
1545 SMB2_sess_free_buffer(sess_data);
1546 kfree(ses->ntlmssp);
1547 ses->ntlmssp = NULL;
1548 sess_data->result = rc;
1549 sess_data->func = NULL;
1553 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1557 type = smb2_select_sectype(cifs_ses_server(ses), ses->sectype);
1558 cifs_dbg(FYI, "sess setup type %d\n", type);
1559 if (type == Unspecified) {
1560 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1566 sess_data->func = SMB2_auth_kerberos;
1569 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1572 cifs_dbg(VFS, "secType %d not supported!\n", type);
1580 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1581 const struct nls_table *nls_cp)
1584 struct TCP_Server_Info *server = cifs_ses_server(ses);
1585 struct SMB2_sess_data *sess_data;
1587 cifs_dbg(FYI, "Session Setup\n");
1590 WARN(1, "%s: server is NULL!\n", __func__);
1594 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1598 rc = SMB2_select_sec(ses, sess_data);
1601 sess_data->xid = xid;
1602 sess_data->ses = ses;
1603 sess_data->buf0_type = CIFS_NO_BUFFER;
1604 sess_data->nls_cp = (struct nls_table *) nls_cp;
1605 sess_data->previous_session = ses->Suid;
1608 * Initialize the session hash with the server one.
1610 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
1611 SMB2_PREAUTH_HASH_SIZE);
1613 while (sess_data->func)
1614 sess_data->func(sess_data);
1616 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1617 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1618 rc = sess_data->result;
1625 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1627 struct smb_rqst rqst;
1628 struct smb2_logoff_req *req; /* response is also trivial struct */
1630 struct TCP_Server_Info *server;
1632 unsigned int total_len;
1634 struct kvec rsp_iov;
1637 cifs_dbg(FYI, "disconnect session %p\n", ses);
1639 if (ses && (ses->server))
1640 server = ses->server;
1644 /* no need to send SMB logoff if uid already closed due to reconnect */
1645 if (ses->need_reconnect)
1646 goto smb2_session_already_dead;
1648 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
1649 (void **) &req, &total_len);
1653 /* since no tcon, smb2_init can not do this, so do here */
1654 req->sync_hdr.SessionId = ses->Suid;
1656 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1657 flags |= CIFS_TRANSFORM_REQ;
1658 else if (server->sign)
1659 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1661 flags |= CIFS_NO_RSP_BUF;
1663 iov[0].iov_base = (char *)req;
1664 iov[0].iov_len = total_len;
1666 memset(&rqst, 0, sizeof(struct smb_rqst));
1670 rc = cifs_send_recv(xid, ses, ses->server,
1671 &rqst, &resp_buf_type, flags, &rsp_iov);
1672 cifs_small_buf_release(req);
1674 * No tcon so can't do
1675 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1678 smb2_session_already_dead:
1682 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1684 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1687 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1689 /* These are similar values to what Windows uses */
1690 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1692 tcon->max_chunks = 256;
1693 tcon->max_bytes_chunk = 1048576;
1694 tcon->max_bytes_copy = 16777216;
1698 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1699 struct cifs_tcon *tcon, const struct nls_table *cp)
1701 struct smb_rqst rqst;
1702 struct smb2_tree_connect_req *req;
1703 struct smb2_tree_connect_rsp *rsp = NULL;
1705 struct kvec rsp_iov = { NULL, 0 };
1709 __le16 *unc_path = NULL;
1711 unsigned int total_len;
1712 struct TCP_Server_Info *server;
1714 /* always use master channel */
1715 server = ses->server;
1717 cifs_dbg(FYI, "TCON\n");
1719 if (!server || !tree)
1722 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1723 if (unc_path == NULL)
1726 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1728 if (unc_path_len < 2) {
1733 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1735 atomic_set(&tcon->num_remote_opens, 0);
1736 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
1737 (void **) &req, &total_len);
1743 if (smb3_encryption_required(tcon))
1744 flags |= CIFS_TRANSFORM_REQ;
1746 iov[0].iov_base = (char *)req;
1748 iov[0].iov_len = total_len - 1;
1750 /* Testing shows that buffer offset must be at location of Buffer[0] */
1751 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1753 req->PathLength = cpu_to_le16(unc_path_len - 2);
1754 iov[1].iov_base = unc_path;
1755 iov[1].iov_len = unc_path_len;
1758 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1759 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1760 * (Samba servers don't always set the flag so also check if null user)
1762 if ((server->dialect == SMB311_PROT_ID) &&
1763 !smb3_encryption_required(tcon) &&
1764 !(ses->session_flags &
1765 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1766 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1767 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1769 memset(&rqst, 0, sizeof(struct smb_rqst));
1773 /* Need 64 for max size write so ask for more in case not there yet */
1774 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1776 rc = cifs_send_recv(xid, ses, server,
1777 &rqst, &resp_buftype, flags, &rsp_iov);
1778 cifs_small_buf_release(req);
1779 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1780 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1783 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1784 tcon->need_reconnect = true;
1786 goto tcon_error_exit;
1789 switch (rsp->ShareType) {
1790 case SMB2_SHARE_TYPE_DISK:
1791 cifs_dbg(FYI, "connection to disk share\n");
1793 case SMB2_SHARE_TYPE_PIPE:
1795 cifs_dbg(FYI, "connection to pipe share\n");
1797 case SMB2_SHARE_TYPE_PRINT:
1799 cifs_dbg(FYI, "connection to printer\n");
1802 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1804 goto tcon_error_exit;
1807 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1808 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1809 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1810 tcon->tidStatus = CifsGood;
1811 tcon->need_reconnect = false;
1812 tcon->tid = rsp->sync_hdr.TreeId;
1813 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1815 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1816 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1817 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
1820 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1821 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
1823 init_copy_chunk_defaults(tcon);
1824 if (server->ops->validate_negotiate)
1825 rc = server->ops->validate_negotiate(xid, tcon);
1828 free_rsp_buf(resp_buftype, rsp);
1833 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1834 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1840 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1842 struct smb_rqst rqst;
1843 struct smb2_tree_disconnect_req *req; /* response is trivial */
1845 struct cifs_ses *ses = tcon->ses;
1847 unsigned int total_len;
1849 struct kvec rsp_iov;
1852 cifs_dbg(FYI, "Tree Disconnect\n");
1854 if (!ses || !(ses->server))
1857 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1860 close_cached_dir_lease(&tcon->crfid);
1862 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, ses->server,
1868 if (smb3_encryption_required(tcon))
1869 flags |= CIFS_TRANSFORM_REQ;
1871 flags |= CIFS_NO_RSP_BUF;
1873 iov[0].iov_base = (char *)req;
1874 iov[0].iov_len = total_len;
1876 memset(&rqst, 0, sizeof(struct smb_rqst));
1880 rc = cifs_send_recv(xid, ses, ses->server,
1881 &rqst, &resp_buf_type, flags, &rsp_iov);
1882 cifs_small_buf_release(req);
1884 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1890 static struct create_durable *
1891 create_durable_buf(void)
1893 struct create_durable *buf;
1895 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1899 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1900 (struct create_durable, Data));
1901 buf->ccontext.DataLength = cpu_to_le32(16);
1902 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1903 (struct create_durable, Name));
1904 buf->ccontext.NameLength = cpu_to_le16(4);
1905 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1913 static struct create_durable *
1914 create_reconnect_durable_buf(struct cifs_fid *fid)
1916 struct create_durable *buf;
1918 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1922 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1923 (struct create_durable, Data));
1924 buf->ccontext.DataLength = cpu_to_le32(16);
1925 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1926 (struct create_durable, Name));
1927 buf->ccontext.NameLength = cpu_to_le16(4);
1928 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1929 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1930 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1939 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1941 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1943 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1944 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1945 buf->IndexNumber = pdisk_id->DiskFileId;
1949 parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
1950 struct create_posix_rsp *posix)
1953 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
1954 u8 *end = beg + le32_to_cpu(cc->DataLength);
1957 memset(posix, 0, sizeof(*posix));
1959 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
1960 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
1961 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
1964 sid_len = posix_info_sid_size(sid, end);
1966 cifs_dbg(VFS, "bad owner sid in posix create response\n");
1969 memcpy(&posix->owner, sid, sid_len);
1971 sid = sid + sid_len;
1972 sid_len = posix_info_sid_size(sid, end);
1974 cifs_dbg(VFS, "bad group sid in posix create response\n");
1977 memcpy(&posix->group, sid, sid_len);
1979 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
1980 posix->nlink, posix->mode, posix->reparse_tag);
1984 smb2_parse_contexts(struct TCP_Server_Info *server,
1985 struct smb2_create_rsp *rsp,
1986 unsigned int *epoch, char *lease_key, __u8 *oplock,
1987 struct smb2_file_all_info *buf,
1988 struct create_posix_rsp *posix)
1991 struct create_context *cc;
1993 unsigned int remaining;
1995 static const char smb3_create_tag_posix[] = {
1996 0x93, 0xAD, 0x25, 0x50, 0x9C,
1997 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
1998 0xDE, 0x96, 0x8B, 0xCD, 0x7C
2002 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
2003 remaining = le32_to_cpu(rsp->CreateContextsLength);
2004 cc = (struct create_context *)data_offset;
2006 /* Initialize inode number to 0 in case no valid data in qfid context */
2008 buf->IndexNumber = 0;
2010 while (remaining >= sizeof(struct create_context)) {
2011 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
2012 if (le16_to_cpu(cc->NameLength) == 4 &&
2013 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
2014 *oplock = server->ops->parse_lease_buf(cc, epoch,
2016 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
2017 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
2018 parse_query_id_ctxt(cc, buf);
2019 else if ((le16_to_cpu(cc->NameLength) == 16)) {
2021 memcmp(name, smb3_create_tag_posix, 16) == 0)
2022 parse_posix_ctxt(cc, buf, posix);
2025 cifs_dbg(FYI, "Context not matched with len %d\n",
2026 le16_to_cpu(cc->NameLength));
2027 cifs_dump_mem("Cctxt name: ", name, 4);
2030 next = le32_to_cpu(cc->Next);
2034 cc = (struct create_context *)((char *)cc + next);
2037 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2038 *oplock = rsp->OplockLevel;
2044 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
2045 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
2047 struct smb2_create_req *req = iov[0].iov_base;
2048 unsigned int num = *num_iovec;
2050 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
2051 if (iov[num].iov_base == NULL)
2053 iov[num].iov_len = server->vals->create_lease_size;
2054 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2055 if (!req->CreateContextsOffset)
2056 req->CreateContextsOffset = cpu_to_le32(
2057 sizeof(struct smb2_create_req) +
2058 iov[num - 1].iov_len);
2059 le32_add_cpu(&req->CreateContextsLength,
2060 server->vals->create_lease_size);
2061 *num_iovec = num + 1;
2065 static struct create_durable_v2 *
2066 create_durable_v2_buf(struct cifs_open_parms *oparms)
2068 struct cifs_fid *pfid = oparms->fid;
2069 struct create_durable_v2 *buf;
2071 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2075 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2076 (struct create_durable_v2, dcontext));
2077 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2078 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2079 (struct create_durable_v2, Name));
2080 buf->ccontext.NameLength = cpu_to_le16(4);
2083 * NB: Handle timeout defaults to 0, which allows server to choose
2084 * (most servers default to 120 seconds) and most clients default to 0.
2085 * This can be overridden at mount ("handletimeout=") if the user wants
2086 * a different persistent (or resilient) handle timeout for all opens
2087 * opens on a particular SMB3 mount.
2089 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2090 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2091 generate_random_uuid(buf->dcontext.CreateGuid);
2092 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2094 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2102 static struct create_durable_handle_reconnect_v2 *
2103 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2105 struct create_durable_handle_reconnect_v2 *buf;
2107 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2112 buf->ccontext.DataOffset =
2113 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2115 buf->ccontext.DataLength =
2116 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2117 buf->ccontext.NameOffset =
2118 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2120 buf->ccontext.NameLength = cpu_to_le16(4);
2122 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2123 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2124 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2125 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2127 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2136 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2137 struct cifs_open_parms *oparms)
2139 struct smb2_create_req *req = iov[0].iov_base;
2140 unsigned int num = *num_iovec;
2142 iov[num].iov_base = create_durable_v2_buf(oparms);
2143 if (iov[num].iov_base == NULL)
2145 iov[num].iov_len = sizeof(struct create_durable_v2);
2146 if (!req->CreateContextsOffset)
2147 req->CreateContextsOffset =
2148 cpu_to_le32(sizeof(struct smb2_create_req) +
2150 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
2151 *num_iovec = num + 1;
2156 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2157 struct cifs_open_parms *oparms)
2159 struct smb2_create_req *req = iov[0].iov_base;
2160 unsigned int num = *num_iovec;
2162 /* indicate that we don't need to relock the file */
2163 oparms->reconnect = false;
2165 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2166 if (iov[num].iov_base == NULL)
2168 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2169 if (!req->CreateContextsOffset)
2170 req->CreateContextsOffset =
2171 cpu_to_le32(sizeof(struct smb2_create_req) +
2173 le32_add_cpu(&req->CreateContextsLength,
2174 sizeof(struct create_durable_handle_reconnect_v2));
2175 *num_iovec = num + 1;
2180 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2181 struct cifs_open_parms *oparms, bool use_persistent)
2183 struct smb2_create_req *req = iov[0].iov_base;
2184 unsigned int num = *num_iovec;
2186 if (use_persistent) {
2187 if (oparms->reconnect)
2188 return add_durable_reconnect_v2_context(iov, num_iovec,
2191 return add_durable_v2_context(iov, num_iovec, oparms);
2194 if (oparms->reconnect) {
2195 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2196 /* indicate that we don't need to relock the file */
2197 oparms->reconnect = false;
2199 iov[num].iov_base = create_durable_buf();
2200 if (iov[num].iov_base == NULL)
2202 iov[num].iov_len = sizeof(struct create_durable);
2203 if (!req->CreateContextsOffset)
2204 req->CreateContextsOffset =
2205 cpu_to_le32(sizeof(struct smb2_create_req) +
2207 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
2208 *num_iovec = num + 1;
2212 /* See MS-SMB2 2.2.13.2.7 */
2213 static struct crt_twarp_ctxt *
2214 create_twarp_buf(__u64 timewarp)
2216 struct crt_twarp_ctxt *buf;
2218 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2222 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2223 (struct crt_twarp_ctxt, Timestamp));
2224 buf->ccontext.DataLength = cpu_to_le32(8);
2225 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2226 (struct crt_twarp_ctxt, Name));
2227 buf->ccontext.NameLength = cpu_to_le16(4);
2228 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2233 buf->Timestamp = cpu_to_le64(timewarp);
2237 /* See MS-SMB2 2.2.13.2.7 */
2239 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2241 struct smb2_create_req *req = iov[0].iov_base;
2242 unsigned int num = *num_iovec;
2244 iov[num].iov_base = create_twarp_buf(timewarp);
2245 if (iov[num].iov_base == NULL)
2247 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2248 if (!req->CreateContextsOffset)
2249 req->CreateContextsOffset = cpu_to_le32(
2250 sizeof(struct smb2_create_req) +
2251 iov[num - 1].iov_len);
2252 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2253 *num_iovec = num + 1;
2257 /* See See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
2258 static void setup_owner_group_sids(char *buf)
2260 struct owner_group_sids *sids = (struct owner_group_sids *)buf;
2262 /* Populate the user ownership fields S-1-5-88-1 */
2263 sids->owner.Revision = 1;
2264 sids->owner.NumAuth = 3;
2265 sids->owner.Authority[5] = 5;
2266 sids->owner.SubAuthorities[0] = cpu_to_le32(88);
2267 sids->owner.SubAuthorities[1] = cpu_to_le32(1);
2268 sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val);
2270 /* Populate the group ownership fields S-1-5-88-2 */
2271 sids->group.Revision = 1;
2272 sids->group.NumAuth = 3;
2273 sids->group.Authority[5] = 5;
2274 sids->group.SubAuthorities[0] = cpu_to_le32(88);
2275 sids->group.SubAuthorities[1] = cpu_to_le32(2);
2276 sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val);
2278 cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val);
2281 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2282 static struct crt_sd_ctxt *
2283 create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
2285 struct crt_sd_ctxt *buf;
2287 unsigned int acelen, acl_size, ace_count;
2288 unsigned int owner_offset = 0;
2289 unsigned int group_offset = 0;
2290 struct smb3_acl acl;
2292 *len = roundup(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 4), 8);
2295 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
2296 *len += sizeof(struct owner_group_sids);
2299 buf = kzalloc(*len, GFP_KERNEL);
2303 ptr = (__u8 *)&buf[1];
2305 /* offset fields are from beginning of security descriptor not of create context */
2306 owner_offset = ptr - (__u8 *)&buf->sd;
2307 buf->sd.OffsetOwner = cpu_to_le32(owner_offset);
2308 group_offset = owner_offset + offsetof(struct owner_group_sids, group);
2309 buf->sd.OffsetGroup = cpu_to_le32(group_offset);
2311 setup_owner_group_sids(ptr);
2312 ptr += sizeof(struct owner_group_sids);
2314 buf->sd.OffsetOwner = 0;
2315 buf->sd.OffsetGroup = 0;
2318 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, sd));
2319 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name));
2320 buf->ccontext.NameLength = cpu_to_le16(4);
2321 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2326 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2329 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2330 * and "DP" ie the DACL is present
2332 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2334 /* offset owner, group and Sbz1 and SACL are all zero */
2335 buf->sd.OffsetDacl = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2336 /* Ship the ACL for now. we will copy it into buf later. */
2338 ptr += sizeof(struct cifs_acl);
2340 /* create one ACE to hold the mode embedded in reserved special SID */
2341 acelen = setup_special_mode_ACE((struct cifs_ace *)ptr, (__u64)mode);
2343 acl_size = acelen + sizeof(struct smb3_acl);
2347 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
2348 acelen = setup_special_user_owner_ACE((struct cifs_ace *)ptr);
2354 /* and one more ACE to allow access for authenticated users */
2355 acelen = setup_authusers_ACE((struct cifs_ace *)ptr);
2360 acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2361 acl.AclSize = cpu_to_le16(acl_size);
2362 acl.AceCount = cpu_to_le16(ace_count);
2363 memcpy(aclptr, &acl, sizeof(struct cifs_acl));
2365 buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2366 *len = ptr - (__u8 *)buf;
2372 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner)
2374 struct smb2_create_req *req = iov[0].iov_base;
2375 unsigned int num = *num_iovec;
2376 unsigned int len = 0;
2378 iov[num].iov_base = create_sd_buf(mode, set_owner, &len);
2379 if (iov[num].iov_base == NULL)
2381 iov[num].iov_len = len;
2382 if (!req->CreateContextsOffset)
2383 req->CreateContextsOffset = cpu_to_le32(
2384 sizeof(struct smb2_create_req) +
2385 iov[num - 1].iov_len);
2386 le32_add_cpu(&req->CreateContextsLength, len);
2387 *num_iovec = num + 1;
2391 static struct crt_query_id_ctxt *
2392 create_query_id_buf(void)
2394 struct crt_query_id_ctxt *buf;
2396 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2400 buf->ccontext.DataOffset = cpu_to_le16(0);
2401 buf->ccontext.DataLength = cpu_to_le32(0);
2402 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2403 (struct crt_query_id_ctxt, Name));
2404 buf->ccontext.NameLength = cpu_to_le16(4);
2405 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2413 /* See MS-SMB2 2.2.13.2.9 */
2415 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2417 struct smb2_create_req *req = iov[0].iov_base;
2418 unsigned int num = *num_iovec;
2420 iov[num].iov_base = create_query_id_buf();
2421 if (iov[num].iov_base == NULL)
2423 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2424 if (!req->CreateContextsOffset)
2425 req->CreateContextsOffset = cpu_to_le32(
2426 sizeof(struct smb2_create_req) +
2427 iov[num - 1].iov_len);
2428 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2429 *num_iovec = num + 1;
2434 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2435 const char *treename, const __le16 *path)
2437 int treename_len, path_len;
2438 struct nls_table *cp;
2439 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2444 treename_len = strlen(treename);
2445 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2451 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2454 * make room for one path separator between the treename and
2457 *out_len = treename_len + 1 + path_len;
2460 * final path needs to be null-terminated UTF16 with a
2464 *out_size = roundup((*out_len+1)*2, 8);
2465 *out_path = kzalloc(*out_size, GFP_KERNEL);
2469 cp = load_nls_default();
2470 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2471 UniStrcat(*out_path, sep);
2472 UniStrcat(*out_path, path);
2478 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2479 umode_t mode, struct cifs_tcon *tcon,
2480 const char *full_path,
2481 struct cifs_sb_info *cifs_sb)
2483 struct smb_rqst rqst;
2484 struct smb2_create_req *req;
2485 struct smb2_create_rsp *rsp = NULL;
2486 struct cifs_ses *ses = tcon->ses;
2487 struct kvec iov[3]; /* make sure at least one for each open context */
2488 struct kvec rsp_iov = {NULL, 0};
2491 __le16 *copy_path = NULL;
2494 unsigned int n_iov = 2;
2495 __u32 file_attributes = 0;
2496 char *pc_buf = NULL;
2498 unsigned int total_len;
2499 __le16 *utf16_path = NULL;
2500 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2502 cifs_dbg(FYI, "mkdir\n");
2504 /* resource #1: path allocation */
2505 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2509 if (!ses || !server) {
2514 /* resource #2: request */
2515 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2516 (void **) &req, &total_len);
2521 if (smb3_encryption_required(tcon))
2522 flags |= CIFS_TRANSFORM_REQ;
2524 req->ImpersonationLevel = IL_IMPERSONATION;
2525 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2526 /* File attributes ignored on open (used in create though) */
2527 req->FileAttributes = cpu_to_le32(file_attributes);
2528 req->ShareAccess = FILE_SHARE_ALL_LE;
2529 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2530 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2532 iov[0].iov_base = (char *)req;
2533 /* -1 since last byte is buf[0] which is sent below (path) */
2534 iov[0].iov_len = total_len - 1;
2536 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2538 /* [MS-SMB2] 2.2.13 NameOffset:
2539 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2540 * the SMB2 header, the file name includes a prefix that will
2541 * be processed during DFS name normalization as specified in
2542 * section 3.3.5.9. Otherwise, the file name is relative to
2543 * the share that is identified by the TreeId in the SMB2
2546 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2549 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2550 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2552 tcon->treeName, utf16_path);
2556 req->NameLength = cpu_to_le16(name_len * 2);
2557 uni_path_len = copy_size;
2558 /* free before overwriting resource */
2560 utf16_path = copy_path;
2562 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2563 /* MUST set path len (NameLength) to 0 opening root of share */
2564 req->NameLength = cpu_to_le16(uni_path_len - 2);
2565 if (uni_path_len % 8 != 0) {
2566 copy_size = roundup(uni_path_len, 8);
2567 copy_path = kzalloc(copy_size, GFP_KERNEL);
2572 memcpy((char *)copy_path, (const char *)utf16_path,
2574 uni_path_len = copy_size;
2575 /* free before overwriting resource */
2577 utf16_path = copy_path;
2581 iov[1].iov_len = uni_path_len;
2582 iov[1].iov_base = utf16_path;
2583 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2585 if (tcon->posix_extensions) {
2586 /* resource #3: posix buf */
2587 rc = add_posix_context(iov, &n_iov, mode);
2590 pc_buf = iov[n_iov-1].iov_base;
2594 memset(&rqst, 0, sizeof(struct smb_rqst));
2596 rqst.rq_nvec = n_iov;
2598 /* no need to inc num_remote_opens because we close it just below */
2599 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2600 FILE_WRITE_ATTRIBUTES);
2601 /* resource #4: response buffer */
2602 rc = cifs_send_recv(xid, ses, server,
2603 &rqst, &resp_buftype, flags, &rsp_iov);
2605 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2606 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2608 FILE_WRITE_ATTRIBUTES, rc);
2609 goto err_free_rsp_buf;
2612 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2613 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2614 ses->Suid, CREATE_NOT_FILE,
2615 FILE_WRITE_ATTRIBUTES);
2617 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2619 /* Eventually save off posix specific response info and timestaps */
2622 free_rsp_buf(resp_buftype, rsp);
2625 cifs_small_buf_release(req);
2632 SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2633 struct smb_rqst *rqst, __u8 *oplock,
2634 struct cifs_open_parms *oparms, __le16 *path)
2636 struct smb2_create_req *req;
2637 unsigned int n_iov = 2;
2638 __u32 file_attributes = 0;
2641 unsigned int total_len;
2642 struct kvec *iov = rqst->rq_iov;
2646 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2647 (void **) &req, &total_len);
2651 iov[0].iov_base = (char *)req;
2652 /* -1 since last byte is buf[0] which is sent below (path) */
2653 iov[0].iov_len = total_len - 1;
2655 if (oparms->create_options & CREATE_OPTION_READONLY)
2656 file_attributes |= ATTR_READONLY;
2657 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2658 file_attributes |= ATTR_SYSTEM;
2660 req->ImpersonationLevel = IL_IMPERSONATION;
2661 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2662 /* File attributes ignored on open (used in create though) */
2663 req->FileAttributes = cpu_to_le32(file_attributes);
2664 req->ShareAccess = FILE_SHARE_ALL_LE;
2666 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2667 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2668 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2670 /* [MS-SMB2] 2.2.13 NameOffset:
2671 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2672 * the SMB2 header, the file name includes a prefix that will
2673 * be processed during DFS name normalization as specified in
2674 * section 3.3.5.9. Otherwise, the file name is relative to
2675 * the share that is identified by the TreeId in the SMB2
2678 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2681 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2682 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2684 tcon->treeName, path);
2687 req->NameLength = cpu_to_le16(name_len * 2);
2688 uni_path_len = copy_size;
2691 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2692 /* MUST set path len (NameLength) to 0 opening root of share */
2693 req->NameLength = cpu_to_le16(uni_path_len - 2);
2694 copy_size = uni_path_len;
2695 if (copy_size % 8 != 0)
2696 copy_size = roundup(copy_size, 8);
2697 copy_path = kzalloc(copy_size, GFP_KERNEL);
2700 memcpy((char *)copy_path, (const char *)path,
2702 uni_path_len = copy_size;
2706 iov[1].iov_len = uni_path_len;
2707 iov[1].iov_base = path;
2709 if ((!server->oplocks) || (tcon->no_lease))
2710 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2712 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2713 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2714 req->RequestedOplockLevel = *oplock;
2715 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2716 (oparms->create_options & CREATE_NOT_FILE))
2717 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2719 rc = add_lease_context(server, iov, &n_iov,
2720 oparms->fid->lease_key, oplock);
2725 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2726 /* need to set Next field of lease context if we request it */
2727 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2728 struct create_context *ccontext =
2729 (struct create_context *)iov[n_iov-1].iov_base;
2731 cpu_to_le32(server->vals->create_lease_size);
2734 rc = add_durable_context(iov, &n_iov, oparms,
2735 tcon->use_persistent);
2740 if (tcon->posix_extensions) {
2742 struct create_context *ccontext =
2743 (struct create_context *)iov[n_iov-1].iov_base;
2745 cpu_to_le32(iov[n_iov-1].iov_len);
2748 rc = add_posix_context(iov, &n_iov, oparms->mode);
2753 if (tcon->snapshot_time) {
2754 cifs_dbg(FYI, "adding snapshot context\n");
2756 struct create_context *ccontext =
2757 (struct create_context *)iov[n_iov-1].iov_base;
2759 cpu_to_le32(iov[n_iov-1].iov_len);
2762 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2767 if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) {
2771 if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
2772 (oparms->mode != ACL_NO_MODE))
2776 oparms->mode = ACL_NO_MODE;
2779 if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
2784 if (set_owner | set_mode) {
2786 struct create_context *ccontext =
2787 (struct create_context *)iov[n_iov-1].iov_base;
2788 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2791 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2792 rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner);
2799 struct create_context *ccontext =
2800 (struct create_context *)iov[n_iov-1].iov_base;
2801 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2803 add_query_id_context(iov, &n_iov);
2805 rqst->rq_nvec = n_iov;
2809 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2810 * All other vectors are freed by kfree().
2813 SMB2_open_free(struct smb_rqst *rqst)
2817 if (rqst && rqst->rq_iov) {
2818 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2819 for (i = 1; i < rqst->rq_nvec; i++)
2820 if (rqst->rq_iov[i].iov_base != smb2_padding)
2821 kfree(rqst->rq_iov[i].iov_base);
2826 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2827 __u8 *oplock, struct smb2_file_all_info *buf,
2828 struct create_posix_rsp *posix,
2829 struct kvec *err_iov, int *buftype)
2831 struct smb_rqst rqst;
2832 struct smb2_create_rsp *rsp = NULL;
2833 struct cifs_tcon *tcon = oparms->tcon;
2834 struct cifs_ses *ses = tcon->ses;
2835 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2836 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2837 struct kvec rsp_iov = {NULL, 0};
2838 int resp_buftype = CIFS_NO_BUFFER;
2842 cifs_dbg(FYI, "create/open\n");
2843 if (!ses || !server)
2846 if (smb3_encryption_required(tcon))
2847 flags |= CIFS_TRANSFORM_REQ;
2849 memset(&rqst, 0, sizeof(struct smb_rqst));
2850 memset(&iov, 0, sizeof(iov));
2852 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2854 rc = SMB2_open_init(tcon, server,
2855 &rqst, oplock, oparms, path);
2859 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2860 oparms->create_options, oparms->desired_access);
2862 rc = cifs_send_recv(xid, ses, server,
2863 &rqst, &resp_buftype, flags,
2865 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2868 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2869 if (err_iov && rsp) {
2871 *buftype = resp_buftype;
2872 resp_buftype = CIFS_NO_BUFFER;
2875 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2876 oparms->create_options, oparms->desired_access, rc);
2877 if (rc == -EREMCHG) {
2878 pr_warn_once("server share %s deleted\n",
2880 tcon->need_reconnect = true;
2884 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2885 ses->Suid, oparms->create_options,
2886 oparms->desired_access);
2888 atomic_inc(&tcon->num_remote_opens);
2889 oparms->fid->persistent_fid = rsp->PersistentFileId;
2890 oparms->fid->volatile_fid = rsp->VolatileFileId;
2891 oparms->fid->access = oparms->desired_access;
2892 #ifdef CONFIG_CIFS_DEBUG2
2893 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2894 #endif /* CIFS_DEBUG2 */
2897 memcpy(buf, &rsp->CreationTime, 32);
2898 buf->AllocationSize = rsp->AllocationSize;
2899 buf->EndOfFile = rsp->EndofFile;
2900 buf->Attributes = rsp->FileAttributes;
2901 buf->NumberOfLinks = cpu_to_le32(1);
2902 buf->DeletePending = 0;
2906 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2907 oparms->fid->lease_key, oplock, buf, posix);
2909 SMB2_open_free(&rqst);
2910 free_rsp_buf(resp_buftype, rsp);
2915 SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2916 struct smb_rqst *rqst,
2917 u64 persistent_fid, u64 volatile_fid, u32 opcode,
2918 bool is_fsctl, char *in_data, u32 indatalen,
2919 __u32 max_response_size)
2921 struct smb2_ioctl_req *req;
2922 struct kvec *iov = rqst->rq_iov;
2923 unsigned int total_len;
2927 rc = smb2_ioctl_req_init(opcode, tcon, server,
2928 (void **) &req, &total_len);
2934 * indatalen is usually small at a couple of bytes max, so
2935 * just allocate through generic pool
2937 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
2939 cifs_small_buf_release(req);
2944 req->CtlCode = cpu_to_le32(opcode);
2945 req->PersistentFileId = persistent_fid;
2946 req->VolatileFileId = volatile_fid;
2948 iov[0].iov_base = (char *)req;
2950 * If no input data, the size of ioctl struct in
2951 * protocol spec still includes a 1 byte data buffer,
2952 * but if input data passed to ioctl, we do not
2953 * want to double count this, so we do not send
2954 * the dummy one byte of data in iovec[0] if sending
2955 * input data (in iovec[1]).
2958 req->InputCount = cpu_to_le32(indatalen);
2959 /* do not set InputOffset if no input data */
2961 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
2963 iov[0].iov_len = total_len - 1;
2964 iov[1].iov_base = in_data_buf;
2965 iov[1].iov_len = indatalen;
2968 iov[0].iov_len = total_len;
2971 req->OutputOffset = 0;
2972 req->OutputCount = 0; /* MBZ */
2975 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2976 * We Could increase default MaxOutputResponse, but that could require
2977 * more credits. Windows typically sets this smaller, but for some
2978 * ioctls it may be useful to allow server to send more. No point
2979 * limiting what the server can send as long as fits in one credit
2980 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2981 * to increase this limit up in the future.
2982 * Note that for snapshot queries that servers like Azure expect that
2983 * the first query be minimal size (and just used to get the number/size
2984 * of previous versions) so response size must be specified as EXACTLY
2985 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2986 * of eight bytes. Currently that is the only case where we set max
2987 * response size smaller.
2989 req->MaxOutputResponse = cpu_to_le32(max_response_size);
2990 req->sync_hdr.CreditCharge =
2991 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
2992 SMB2_MAX_BUFFER_SIZE));
2994 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2998 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2999 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
3000 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
3006 SMB2_ioctl_free(struct smb_rqst *rqst)
3009 if (rqst && rqst->rq_iov) {
3010 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3011 for (i = 1; i < rqst->rq_nvec; i++)
3012 if (rqst->rq_iov[i].iov_base != smb2_padding)
3013 kfree(rqst->rq_iov[i].iov_base);
3019 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
3022 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3023 u64 volatile_fid, u32 opcode, bool is_fsctl,
3024 char *in_data, u32 indatalen, u32 max_out_data_len,
3025 char **out_data, u32 *plen /* returned data len */)
3027 struct smb_rqst rqst;
3028 struct smb2_ioctl_rsp *rsp = NULL;
3029 struct cifs_ses *ses;
3030 struct TCP_Server_Info *server;
3031 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
3032 struct kvec rsp_iov = {NULL, 0};
3033 int resp_buftype = CIFS_NO_BUFFER;
3037 cifs_dbg(FYI, "SMB2 IOCTL\n");
3039 if (out_data != NULL)
3042 /* zero out returned data len, in case of error */
3053 server = cifs_pick_channel(ses);
3057 if (smb3_encryption_required(tcon))
3058 flags |= CIFS_TRANSFORM_REQ;
3060 memset(&rqst, 0, sizeof(struct smb_rqst));
3061 memset(&iov, 0, sizeof(iov));
3063 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
3065 rc = SMB2_ioctl_init(tcon, server,
3066 &rqst, persistent_fid, volatile_fid, opcode,
3067 is_fsctl, in_data, indatalen, max_out_data_len);
3071 rc = cifs_send_recv(xid, ses, server,
3072 &rqst, &resp_buftype, flags,
3074 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
3077 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3078 ses->Suid, 0, opcode, rc);
3080 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
3081 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3083 } else if (rc == -EINVAL) {
3084 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3085 (opcode != FSCTL_SRV_COPYCHUNK)) {
3086 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3089 } else if (rc == -E2BIG) {
3090 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3091 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3096 /* check if caller wants to look at return data or just return rc */
3097 if ((plen == NULL) || (out_data == NULL))
3100 *plen = le32_to_cpu(rsp->OutputCount);
3102 /* We check for obvious errors in the output buffer length and offset */
3104 goto ioctl_exit; /* server returned no data */
3105 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
3106 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
3112 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
3113 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
3114 le32_to_cpu(rsp->OutputOffset));
3120 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3122 if (*out_data == NULL) {
3128 SMB2_ioctl_free(&rqst);
3129 free_rsp_buf(resp_buftype, rsp);
3134 * Individual callers to ioctl worker function follow
3138 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3139 u64 persistent_fid, u64 volatile_fid)
3142 struct compress_ioctl fsctl_input;
3143 char *ret_data = NULL;
3145 fsctl_input.CompressionState =
3146 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3148 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3149 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
3150 (char *)&fsctl_input /* data input */,
3151 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3152 &ret_data /* out data */, NULL);
3154 cifs_dbg(FYI, "set compression rc %d\n", rc);
3160 SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3161 struct smb_rqst *rqst,
3162 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
3164 struct smb2_close_req *req;
3165 struct kvec *iov = rqst->rq_iov;
3166 unsigned int total_len;
3169 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
3170 (void **) &req, &total_len);
3174 req->PersistentFileId = persistent_fid;
3175 req->VolatileFileId = volatile_fid;
3177 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3180 iov[0].iov_base = (char *)req;
3181 iov[0].iov_len = total_len;
3187 SMB2_close_free(struct smb_rqst *rqst)
3189 if (rqst && rqst->rq_iov)
3190 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3194 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3195 u64 persistent_fid, u64 volatile_fid,
3196 struct smb2_file_network_open_info *pbuf)
3198 struct smb_rqst rqst;
3199 struct smb2_close_rsp *rsp = NULL;
3200 struct cifs_ses *ses = tcon->ses;
3201 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3203 struct kvec rsp_iov;
3204 int resp_buftype = CIFS_NO_BUFFER;
3207 bool query_attrs = false;
3209 cifs_dbg(FYI, "Close\n");
3211 if (!ses || !server)
3214 if (smb3_encryption_required(tcon))
3215 flags |= CIFS_TRANSFORM_REQ;
3217 memset(&rqst, 0, sizeof(struct smb_rqst));
3218 memset(&iov, 0, sizeof(iov));
3222 /* check if need to ask server to return timestamps in close response */
3226 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3227 rc = SMB2_close_init(tcon, server,
3228 &rqst, persistent_fid, volatile_fid,
3233 rc = cifs_send_recv(xid, ses, server,
3234 &rqst, &resp_buftype, flags, &rsp_iov);
3235 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
3238 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
3239 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3243 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3246 * Note that have to subtract 4 since struct network_open_info
3247 * has a final 4 byte pad that close response does not have
3250 memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3253 atomic_dec(&tcon->num_remote_opens);
3255 SMB2_close_free(&rqst);
3256 free_rsp_buf(resp_buftype, rsp);
3258 /* retry close in a worker thread if this one is interrupted */
3259 if (is_interrupt_error(rc)) {
3262 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3265 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3266 persistent_fid, tmp_rc);
3272 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3273 u64 persistent_fid, u64 volatile_fid)
3275 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3279 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3280 struct kvec *iov, unsigned int min_buf_size)
3282 unsigned int smb_len = iov->iov_len;
3283 char *end_of_smb = smb_len + (char *)iov->iov_base;
3284 char *begin_of_buf = offset + (char *)iov->iov_base;
3285 char *end_of_buf = begin_of_buf + buffer_length;
3288 if (buffer_length < min_buf_size) {
3289 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3290 buffer_length, min_buf_size);
3294 /* check if beyond RFC1001 maximum length */
3295 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3296 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3297 buffer_length, smb_len);
3301 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3302 cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
3310 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3311 * Caller must free buffer.
3314 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3315 struct kvec *iov, unsigned int minbufsize,
3318 char *begin_of_buf = offset + (char *)iov->iov_base;
3324 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3328 memcpy(data, begin_of_buf, buffer_length);
3334 SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3335 struct smb_rqst *rqst,
3336 u64 persistent_fid, u64 volatile_fid,
3337 u8 info_class, u8 info_type, u32 additional_info,
3338 size_t output_len, size_t input_len, void *input)
3340 struct smb2_query_info_req *req;
3341 struct kvec *iov = rqst->rq_iov;
3342 unsigned int total_len;
3345 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
3346 (void **) &req, &total_len);
3350 req->InfoType = info_type;
3351 req->FileInfoClass = info_class;
3352 req->PersistentFileId = persistent_fid;
3353 req->VolatileFileId = volatile_fid;
3354 req->AdditionalInformation = cpu_to_le32(additional_info);
3356 req->OutputBufferLength = cpu_to_le32(output_len);
3358 req->InputBufferLength = cpu_to_le32(input_len);
3359 /* total_len for smb query request never close to le16 max */
3360 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3361 memcpy(req->Buffer, input, input_len);
3364 iov[0].iov_base = (char *)req;
3366 iov[0].iov_len = total_len - 1 + input_len;
3371 SMB2_query_info_free(struct smb_rqst *rqst)
3373 if (rqst && rqst->rq_iov)
3374 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3378 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3379 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3380 u32 additional_info, size_t output_len, size_t min_len, void **data,
3383 struct smb_rqst rqst;
3384 struct smb2_query_info_rsp *rsp = NULL;
3386 struct kvec rsp_iov;
3388 int resp_buftype = CIFS_NO_BUFFER;
3389 struct cifs_ses *ses = tcon->ses;
3390 struct TCP_Server_Info *server;
3392 bool allocated = false;
3394 cifs_dbg(FYI, "Query Info\n");
3398 server = cifs_pick_channel(ses);
3402 if (smb3_encryption_required(tcon))
3403 flags |= CIFS_TRANSFORM_REQ;
3405 memset(&rqst, 0, sizeof(struct smb_rqst));
3406 memset(&iov, 0, sizeof(iov));
3410 rc = SMB2_query_info_init(tcon, server,
3411 &rqst, persistent_fid, volatile_fid,
3412 info_class, info_type, additional_info,
3413 output_len, 0, NULL);
3417 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3418 ses->Suid, info_class, (__u32)info_type);
3420 rc = cifs_send_recv(xid, ses, server,
3421 &rqst, &resp_buftype, flags, &rsp_iov);
3422 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3425 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3426 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3427 ses->Suid, info_class, (__u32)info_type, rc);
3431 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3432 ses->Suid, info_class, (__u32)info_type);
3435 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3437 *data = kmalloc(*dlen, GFP_KERNEL);
3440 "Error %d allocating memory for acl\n",
3450 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3451 le32_to_cpu(rsp->OutputBufferLength),
3452 &rsp_iov, min_len, *data);
3453 if (rc && allocated) {
3460 SMB2_query_info_free(&rqst);
3461 free_rsp_buf(resp_buftype, rsp);
3465 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3466 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3468 return query_info(xid, tcon, persistent_fid, volatile_fid,
3469 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3470 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3471 sizeof(struct smb2_file_all_info), (void **)&data,
3476 SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3477 u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen)
3479 size_t output_len = sizeof(struct smb311_posix_qinfo *) +
3480 (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2);
3483 return query_info(xid, tcon, persistent_fid, volatile_fid,
3484 SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0,
3485 output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen);
3489 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3490 u64 persistent_fid, u64 volatile_fid,
3491 void **data, u32 *plen, u32 extra_info)
3493 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
3497 return query_info(xid, tcon, persistent_fid, volatile_fid,
3498 0, SMB2_O_INFO_SECURITY, additional_info,
3499 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3503 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3504 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3506 return query_info(xid, tcon, persistent_fid, volatile_fid,
3507 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3508 sizeof(struct smb2_file_internal_info),
3509 sizeof(struct smb2_file_internal_info),
3510 (void **)&uniqueid, NULL);
3514 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3515 * See MS-SMB2 2.2.35 and 2.2.36
3519 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3520 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3521 u64 persistent_fid, u64 volatile_fid,
3522 u32 completion_filter, bool watch_tree)
3524 struct smb2_change_notify_req *req;
3525 struct kvec *iov = rqst->rq_iov;
3526 unsigned int total_len;
3529 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
3530 (void **) &req, &total_len);
3534 req->PersistentFileId = persistent_fid;
3535 req->VolatileFileId = volatile_fid;
3536 /* See note 354 of MS-SMB2, 64K max */
3537 req->OutputBufferLength =
3538 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3539 req->CompletionFilter = cpu_to_le32(completion_filter);
3541 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3545 iov[0].iov_base = (char *)req;
3546 iov[0].iov_len = total_len;
3552 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3553 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3554 u32 completion_filter)
3556 struct cifs_ses *ses = tcon->ses;
3557 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3558 struct smb_rqst rqst;
3560 struct kvec rsp_iov = {NULL, 0};
3561 int resp_buftype = CIFS_NO_BUFFER;
3565 cifs_dbg(FYI, "change notify\n");
3566 if (!ses || !server)
3569 if (smb3_encryption_required(tcon))
3570 flags |= CIFS_TRANSFORM_REQ;
3572 memset(&rqst, 0, sizeof(struct smb_rqst));
3573 memset(&iov, 0, sizeof(iov));
3577 rc = SMB2_notify_init(xid, &rqst, tcon, server,
3578 persistent_fid, volatile_fid,
3579 completion_filter, watch_tree);
3583 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3584 (u8)watch_tree, completion_filter);
3585 rc = cifs_send_recv(xid, ses, server,
3586 &rqst, &resp_buftype, flags, &rsp_iov);
3589 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3590 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3591 (u8)watch_tree, completion_filter, rc);
3593 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3594 ses->Suid, (u8)watch_tree, completion_filter);
3598 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3599 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3606 * This is a no-op for now. We're not really interested in the reply, but
3607 * rather in the fact that the server sent one and that server->lstrp
3610 * FIXME: maybe we should consider checking that the reply matches request?
3613 smb2_echo_callback(struct mid_q_entry *mid)
3615 struct TCP_Server_Info *server = mid->callback_data;
3616 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3617 struct cifs_credits credits = { .value = 0, .instance = 0 };
3619 if (mid->mid_state == MID_RESPONSE_RECEIVED
3620 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3621 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3622 credits.instance = server->reconnect_instance;
3625 DeleteMidQEntry(mid);
3626 add_credits(server, &credits, CIFS_ECHO_OP);
3629 void smb2_reconnect_server(struct work_struct *work)
3631 struct TCP_Server_Info *server = container_of(work,
3632 struct TCP_Server_Info, reconnect.work);
3633 struct cifs_ses *ses;
3634 struct cifs_tcon *tcon, *tcon2;
3635 struct list_head tmp_list;
3636 int tcon_exist = false;
3638 int resched = false;
3641 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3642 mutex_lock(&server->reconnect_mutex);
3644 INIT_LIST_HEAD(&tmp_list);
3645 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3647 spin_lock(&cifs_tcp_ses_lock);
3648 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3649 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3650 if (tcon->need_reconnect || tcon->need_reopen_files) {
3652 list_add_tail(&tcon->rlist, &tmp_list);
3657 * IPC has the same lifetime as its session and uses its
3660 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3661 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3667 * Get the reference to server struct to be sure that the last call of
3668 * cifs_put_tcon() in the loop below won't release the server pointer.
3671 server->srv_count++;
3673 spin_unlock(&cifs_tcp_ses_lock);
3675 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3676 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
3678 cifs_reopen_persistent_handles(tcon);
3681 list_del_init(&tcon->rlist);
3683 cifs_put_smb_ses(tcon->ses);
3685 cifs_put_tcon(tcon);
3688 cifs_dbg(FYI, "Reconnecting tcons finished\n");
3690 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3691 mutex_unlock(&server->reconnect_mutex);
3693 /* now we can safely release srv struct */
3695 cifs_put_tcp_session(server, 1);
3699 SMB2_echo(struct TCP_Server_Info *server)
3701 struct smb2_echo_req *req;
3704 struct smb_rqst rqst = { .rq_iov = iov,
3706 unsigned int total_len;
3708 cifs_dbg(FYI, "In echo request\n");
3710 if (server->tcpStatus == CifsNeedNegotiate) {
3711 /* No need to send echo on newly established connections */
3712 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
3716 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
3717 (void **)&req, &total_len);
3721 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3723 iov[0].iov_len = total_len;
3724 iov[0].iov_base = (char *)req;
3726 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3727 server, CIFS_ECHO_OP, NULL);
3729 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3731 cifs_small_buf_release(req);
3736 SMB2_flush_free(struct smb_rqst *rqst)
3738 if (rqst && rqst->rq_iov)
3739 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3743 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3744 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3745 u64 persistent_fid, u64 volatile_fid)
3747 struct smb2_flush_req *req;
3748 struct kvec *iov = rqst->rq_iov;
3749 unsigned int total_len;
3752 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
3753 (void **) &req, &total_len);
3757 req->PersistentFileId = persistent_fid;
3758 req->VolatileFileId = volatile_fid;
3760 iov[0].iov_base = (char *)req;
3761 iov[0].iov_len = total_len;
3767 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3770 struct cifs_ses *ses = tcon->ses;
3771 struct smb_rqst rqst;
3773 struct kvec rsp_iov = {NULL, 0};
3774 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3775 int resp_buftype = CIFS_NO_BUFFER;
3779 cifs_dbg(FYI, "flush\n");
3780 if (!ses || !(ses->server))
3783 if (smb3_encryption_required(tcon))
3784 flags |= CIFS_TRANSFORM_REQ;
3786 memset(&rqst, 0, sizeof(struct smb_rqst));
3787 memset(&iov, 0, sizeof(iov));
3791 rc = SMB2_flush_init(xid, &rqst, tcon, server,
3792 persistent_fid, volatile_fid);
3796 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3797 rc = cifs_send_recv(xid, ses, server,
3798 &rqst, &resp_buftype, flags, &rsp_iov);
3801 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
3802 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3805 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3809 SMB2_flush_free(&rqst);
3810 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3815 * To form a chain of read requests, any read requests after the first should
3816 * have the end_of_chain boolean set to true.
3819 smb2_new_read_req(void **buf, unsigned int *total_len,
3820 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3821 unsigned int remaining_bytes, int request_type)
3824 struct smb2_read_plain_req *req = NULL;
3825 struct smb2_sync_hdr *shdr;
3826 struct TCP_Server_Info *server = io_parms->server;
3828 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
3829 (void **) &req, total_len);
3834 return -ECONNABORTED;
3836 shdr = &req->sync_hdr;
3837 shdr->ProcessId = cpu_to_le32(io_parms->pid);
3839 req->PersistentFileId = io_parms->persistent_fid;
3840 req->VolatileFileId = io_parms->volatile_fid;
3841 req->ReadChannelInfoOffset = 0; /* reserved */
3842 req->ReadChannelInfoLength = 0; /* reserved */
3843 req->Channel = 0; /* reserved */
3844 req->MinimumCount = 0;
3845 req->Length = cpu_to_le32(io_parms->length);
3846 req->Offset = cpu_to_le64(io_parms->offset);
3848 trace_smb3_read_enter(0 /* xid */,
3849 io_parms->persistent_fid,
3850 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3851 io_parms->offset, io_parms->length);
3852 #ifdef CONFIG_CIFS_SMB_DIRECT
3854 * If we want to do a RDMA write, fill in and append
3855 * smbd_buffer_descriptor_v1 to the end of read request
3857 if (server->rdma && rdata && !server->sign &&
3858 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
3860 struct smbd_buffer_descriptor_v1 *v1;
3861 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3863 rdata->mr = smbd_register_mr(
3864 server->smbd_conn, rdata->pages,
3865 rdata->nr_pages, rdata->page_offset,
3866 rdata->tailsz, true, need_invalidate);
3870 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3871 if (need_invalidate)
3872 req->Channel = SMB2_CHANNEL_RDMA_V1;
3873 req->ReadChannelInfoOffset =
3874 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
3875 req->ReadChannelInfoLength =
3876 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3877 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3878 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3879 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3880 v1->length = cpu_to_le32(rdata->mr->mr->length);
3882 *total_len += sizeof(*v1) - 1;
3885 if (request_type & CHAINED_REQUEST) {
3886 if (!(request_type & END_OF_CHAIN)) {
3887 /* next 8-byte aligned request */
3888 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3889 shdr->NextCommand = cpu_to_le32(*total_len);
3890 } else /* END_OF_CHAIN */
3891 shdr->NextCommand = 0;
3892 if (request_type & RELATED_REQUEST) {
3893 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
3895 * Related requests use info from previous read request
3898 shdr->SessionId = 0xFFFFFFFF;
3899 shdr->TreeId = 0xFFFFFFFF;
3900 req->PersistentFileId = 0xFFFFFFFF;
3901 req->VolatileFileId = 0xFFFFFFFF;
3904 if (remaining_bytes > io_parms->length)
3905 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3907 req->RemainingBytes = 0;
3914 smb2_readv_callback(struct mid_q_entry *mid)
3916 struct cifs_readdata *rdata = mid->callback_data;
3917 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3918 struct TCP_Server_Info *server = rdata->server;
3919 struct smb2_sync_hdr *shdr =
3920 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
3921 struct cifs_credits credits = { .value = 0, .instance = 0 };
3922 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
3924 .rq_pages = rdata->pages,
3925 .rq_offset = rdata->page_offset,
3926 .rq_npages = rdata->nr_pages,
3927 .rq_pagesz = rdata->pagesz,
3928 .rq_tailsz = rdata->tailsz };
3930 WARN_ONCE(rdata->server != mid->server,
3931 "rdata server %p != mid server %p",
3932 rdata->server, mid->server);
3934 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3935 __func__, mid->mid, mid->mid_state, rdata->result,
3938 switch (mid->mid_state) {
3939 case MID_RESPONSE_RECEIVED:
3940 credits.value = le16_to_cpu(shdr->CreditRequest);
3941 credits.instance = server->reconnect_instance;
3942 /* result already set, check signature */
3943 if (server->sign && !mid->decrypted) {
3946 rc = smb2_verify_signature(&rqst, server);
3948 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
3951 /* FIXME: should this be counted toward the initiating task? */
3952 task_io_account_read(rdata->got_bytes);
3953 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3955 case MID_REQUEST_SUBMITTED:
3956 case MID_RETRY_NEEDED:
3957 rdata->result = -EAGAIN;
3958 if (server->sign && rdata->got_bytes)
3959 /* reset bytes number since we can not check a sign */
3960 rdata->got_bytes = 0;
3961 /* FIXME: should this be counted toward the initiating task? */
3962 task_io_account_read(rdata->got_bytes);
3963 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3965 case MID_RESPONSE_MALFORMED:
3966 credits.value = le16_to_cpu(shdr->CreditRequest);
3967 credits.instance = server->reconnect_instance;
3970 rdata->result = -EIO;
3972 #ifdef CONFIG_CIFS_SMB_DIRECT
3974 * If this rdata has a memmory registered, the MR can be freed
3975 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3976 * because they have limited number and are used for future I/Os
3979 smbd_deregister_mr(rdata->mr);
3983 if (rdata->result && rdata->result != -ENODATA) {
3984 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
3985 trace_smb3_read_err(0 /* xid */,
3986 rdata->cfile->fid.persistent_fid,
3987 tcon->tid, tcon->ses->Suid, rdata->offset,
3988 rdata->bytes, rdata->result);
3990 trace_smb3_read_done(0 /* xid */,
3991 rdata->cfile->fid.persistent_fid,
3992 tcon->tid, tcon->ses->Suid,
3993 rdata->offset, rdata->got_bytes);
3995 queue_work(cifsiod_wq, &rdata->work);
3996 DeleteMidQEntry(mid);
3997 add_credits(server, &credits, 0);
4000 /* smb2_async_readv - send an async read, and set up mid to handle result */
4002 smb2_async_readv(struct cifs_readdata *rdata)
4006 struct smb2_sync_hdr *shdr;
4007 struct cifs_io_parms io_parms;
4008 struct smb_rqst rqst = { .rq_iov = rdata->iov,
4010 struct TCP_Server_Info *server;
4011 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
4012 unsigned int total_len;
4014 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
4015 __func__, rdata->offset, rdata->bytes);
4018 rdata->server = cifs_pick_channel(tcon->ses);
4020 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
4021 io_parms.server = server = rdata->server;
4022 io_parms.offset = rdata->offset;
4023 io_parms.length = rdata->bytes;
4024 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
4025 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
4026 io_parms.pid = rdata->pid;
4028 rc = smb2_new_read_req(
4029 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
4033 if (smb3_encryption_required(io_parms.tcon))
4034 flags |= CIFS_TRANSFORM_REQ;
4036 rdata->iov[0].iov_base = buf;
4037 rdata->iov[0].iov_len = total_len;
4039 shdr = (struct smb2_sync_hdr *)buf;
4041 if (rdata->credits.value > 0) {
4042 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
4043 SMB2_MAX_BUFFER_SIZE));
4044 shdr->CreditRequest = cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 8);
4046 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4048 goto async_readv_out;
4050 flags |= CIFS_HAS_CREDITS;
4053 kref_get(&rdata->refcount);
4054 rc = cifs_call_async(server, &rqst,
4055 cifs_readv_receive, smb2_readv_callback,
4056 smb3_handle_read_data, rdata, flags,
4059 kref_put(&rdata->refcount, cifs_readdata_release);
4060 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
4061 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
4063 io_parms.tcon->ses->Suid,
4064 io_parms.offset, io_parms.length, rc);
4068 cifs_small_buf_release(buf);
4073 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
4074 unsigned int *nbytes, char **buf, int *buf_type)
4076 struct smb_rqst rqst;
4077 int resp_buftype, rc;
4078 struct smb2_read_plain_req *req = NULL;
4079 struct smb2_read_rsp *rsp = NULL;
4081 struct kvec rsp_iov;
4082 unsigned int total_len;
4083 int flags = CIFS_LOG_ERROR;
4084 struct cifs_ses *ses = io_parms->tcon->ses;
4086 if (!io_parms->server)
4087 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4090 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
4094 if (smb3_encryption_required(io_parms->tcon))
4095 flags |= CIFS_TRANSFORM_REQ;
4097 iov[0].iov_base = (char *)req;
4098 iov[0].iov_len = total_len;
4100 memset(&rqst, 0, sizeof(struct smb_rqst));
4104 rc = cifs_send_recv(xid, ses, io_parms->server,
4105 &rqst, &resp_buftype, flags, &rsp_iov);
4106 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
4109 if (rc != -ENODATA) {
4110 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4111 cifs_dbg(VFS, "Send error in read = %d\n", rc);
4112 trace_smb3_read_err(xid, req->PersistentFileId,
4113 io_parms->tcon->tid, ses->Suid,
4114 io_parms->offset, io_parms->length,
4117 trace_smb3_read_done(xid, req->PersistentFileId,
4118 io_parms->tcon->tid, ses->Suid,
4119 io_parms->offset, 0);
4120 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4121 cifs_small_buf_release(req);
4122 return rc == -ENODATA ? 0 : rc;
4124 trace_smb3_read_done(xid, req->PersistentFileId,
4125 io_parms->tcon->tid, ses->Suid,
4126 io_parms->offset, io_parms->length);
4128 cifs_small_buf_release(req);
4130 *nbytes = le32_to_cpu(rsp->DataLength);
4131 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4132 (*nbytes > io_parms->length)) {
4133 cifs_dbg(FYI, "bad length %d for count %d\n",
4134 *nbytes, io_parms->length);
4140 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
4141 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4142 } else if (resp_buftype != CIFS_NO_BUFFER) {
4143 *buf = rsp_iov.iov_base;
4144 if (resp_buftype == CIFS_SMALL_BUFFER)
4145 *buf_type = CIFS_SMALL_BUFFER;
4146 else if (resp_buftype == CIFS_LARGE_BUFFER)
4147 *buf_type = CIFS_LARGE_BUFFER;
4153 * Check the mid_state and signature on received buffer (if any), and queue the
4154 * workqueue completion task.
4157 smb2_writev_callback(struct mid_q_entry *mid)
4159 struct cifs_writedata *wdata = mid->callback_data;
4160 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4161 struct TCP_Server_Info *server = wdata->server;
4162 unsigned int written;
4163 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
4164 struct cifs_credits credits = { .value = 0, .instance = 0 };
4166 WARN_ONCE(wdata->server != mid->server,
4167 "wdata server %p != mid server %p",
4168 wdata->server, mid->server);
4170 switch (mid->mid_state) {
4171 case MID_RESPONSE_RECEIVED:
4172 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4173 credits.instance = server->reconnect_instance;
4174 wdata->result = smb2_check_receive(mid, server, 0);
4175 if (wdata->result != 0)
4178 written = le32_to_cpu(rsp->DataLength);
4180 * Mask off high 16 bits when bytes written as returned
4181 * by the server is greater than bytes requested by the
4182 * client. OS/2 servers are known to set incorrect
4185 if (written > wdata->bytes)
4188 if (written < wdata->bytes)
4189 wdata->result = -ENOSPC;
4191 wdata->bytes = written;
4193 case MID_REQUEST_SUBMITTED:
4194 case MID_RETRY_NEEDED:
4195 wdata->result = -EAGAIN;
4197 case MID_RESPONSE_MALFORMED:
4198 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4199 credits.instance = server->reconnect_instance;
4202 wdata->result = -EIO;
4205 #ifdef CONFIG_CIFS_SMB_DIRECT
4207 * If this wdata has a memory registered, the MR can be freed
4208 * The number of MRs available is limited, it's important to recover
4209 * used MR as soon as I/O is finished. Hold MR longer in the later
4210 * I/O process can possibly result in I/O deadlock due to lack of MR
4211 * to send request on I/O retry
4214 smbd_deregister_mr(wdata->mr);
4218 if (wdata->result) {
4219 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4220 trace_smb3_write_err(0 /* no xid */,
4221 wdata->cfile->fid.persistent_fid,
4222 tcon->tid, tcon->ses->Suid, wdata->offset,
4223 wdata->bytes, wdata->result);
4224 if (wdata->result == -ENOSPC)
4225 pr_warn_once("Out of space writing to %s\n",
4228 trace_smb3_write_done(0 /* no xid */,
4229 wdata->cfile->fid.persistent_fid,
4230 tcon->tid, tcon->ses->Suid,
4231 wdata->offset, wdata->bytes);
4233 queue_work(cifsiod_wq, &wdata->work);
4234 DeleteMidQEntry(mid);
4235 add_credits(server, &credits, 0);
4238 /* smb2_async_writev - send an async write, and set up mid to handle result */
4240 smb2_async_writev(struct cifs_writedata *wdata,
4241 void (*release)(struct kref *kref))
4243 int rc = -EACCES, flags = 0;
4244 struct smb2_write_req *req = NULL;
4245 struct smb2_sync_hdr *shdr;
4246 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4247 struct TCP_Server_Info *server = wdata->server;
4249 struct smb_rqst rqst = { };
4250 unsigned int total_len;
4253 server = wdata->server = cifs_pick_channel(tcon->ses);
4255 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
4256 (void **) &req, &total_len);
4260 if (smb3_encryption_required(tcon))
4261 flags |= CIFS_TRANSFORM_REQ;
4263 shdr = (struct smb2_sync_hdr *)req;
4264 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
4266 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
4267 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
4268 req->WriteChannelInfoOffset = 0;
4269 req->WriteChannelInfoLength = 0;
4271 req->Offset = cpu_to_le64(wdata->offset);
4272 req->DataOffset = cpu_to_le16(
4273 offsetof(struct smb2_write_req, Buffer));
4274 req->RemainingBytes = 0;
4276 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
4277 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
4278 #ifdef CONFIG_CIFS_SMB_DIRECT
4280 * If we want to do a server RDMA read, fill in and append
4281 * smbd_buffer_descriptor_v1 to the end of write request
4283 if (server->rdma && !server->sign && wdata->bytes >=
4284 server->smbd_conn->rdma_readwrite_threshold) {
4286 struct smbd_buffer_descriptor_v1 *v1;
4287 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4289 wdata->mr = smbd_register_mr(
4290 server->smbd_conn, wdata->pages,
4291 wdata->nr_pages, wdata->page_offset,
4292 wdata->tailsz, false, need_invalidate);
4295 goto async_writev_out;
4298 req->DataOffset = 0;
4299 if (wdata->nr_pages > 1)
4300 req->RemainingBytes =
4302 (wdata->nr_pages - 1) * wdata->pagesz -
4303 wdata->page_offset + wdata->tailsz
4306 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
4307 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4308 if (need_invalidate)
4309 req->Channel = SMB2_CHANNEL_RDMA_V1;
4310 req->WriteChannelInfoOffset =
4311 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4312 req->WriteChannelInfoLength =
4313 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4314 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4315 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4316 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4317 v1->length = cpu_to_le32(wdata->mr->mr->length);
4320 iov[0].iov_len = total_len - 1;
4321 iov[0].iov_base = (char *)req;
4325 rqst.rq_pages = wdata->pages;
4326 rqst.rq_offset = wdata->page_offset;
4327 rqst.rq_npages = wdata->nr_pages;
4328 rqst.rq_pagesz = wdata->pagesz;
4329 rqst.rq_tailsz = wdata->tailsz;
4330 #ifdef CONFIG_CIFS_SMB_DIRECT
4332 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4336 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4337 wdata->offset, wdata->bytes);
4339 #ifdef CONFIG_CIFS_SMB_DIRECT
4340 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4342 req->Length = cpu_to_le32(wdata->bytes);
4344 req->Length = cpu_to_le32(wdata->bytes);
4347 if (wdata->credits.value > 0) {
4348 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4349 SMB2_MAX_BUFFER_SIZE));
4350 shdr->CreditRequest = cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 8);
4352 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4354 goto async_writev_out;
4356 flags |= CIFS_HAS_CREDITS;
4359 kref_get(&wdata->refcount);
4360 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4361 wdata, flags, &wdata->credits);
4364 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
4365 tcon->tid, tcon->ses->Suid, wdata->offset,
4367 kref_put(&wdata->refcount, release);
4368 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4372 cifs_small_buf_release(req);
4377 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4378 * The length field from io_parms must be at least 1 and indicates a number of
4379 * elements with data to write that begins with position 1 in iov array. All
4380 * data length is specified by count.
4383 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4384 unsigned int *nbytes, struct kvec *iov, int n_vec)
4386 struct smb_rqst rqst;
4388 struct smb2_write_req *req = NULL;
4389 struct smb2_write_rsp *rsp = NULL;
4391 struct kvec rsp_iov;
4393 unsigned int total_len;
4394 struct TCP_Server_Info *server;
4401 if (!io_parms->server)
4402 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4403 server = io_parms->server;
4405 return -ECONNABORTED;
4407 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
4408 (void **) &req, &total_len);
4412 if (smb3_encryption_required(io_parms->tcon))
4413 flags |= CIFS_TRANSFORM_REQ;
4415 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
4417 req->PersistentFileId = io_parms->persistent_fid;
4418 req->VolatileFileId = io_parms->volatile_fid;
4419 req->WriteChannelInfoOffset = 0;
4420 req->WriteChannelInfoLength = 0;
4422 req->Length = cpu_to_le32(io_parms->length);
4423 req->Offset = cpu_to_le64(io_parms->offset);
4424 req->DataOffset = cpu_to_le16(
4425 offsetof(struct smb2_write_req, Buffer));
4426 req->RemainingBytes = 0;
4428 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4429 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4430 io_parms->offset, io_parms->length);
4432 iov[0].iov_base = (char *)req;
4434 iov[0].iov_len = total_len - 1;
4436 memset(&rqst, 0, sizeof(struct smb_rqst));
4438 rqst.rq_nvec = n_vec + 1;
4440 rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
4442 &resp_buftype, flags, &rsp_iov);
4443 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
4446 trace_smb3_write_err(xid, req->PersistentFileId,
4447 io_parms->tcon->tid,
4448 io_parms->tcon->ses->Suid,
4449 io_parms->offset, io_parms->length, rc);
4450 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
4451 cifs_dbg(VFS, "Send error in write = %d\n", rc);
4453 *nbytes = le32_to_cpu(rsp->DataLength);
4454 trace_smb3_write_done(xid, req->PersistentFileId,
4455 io_parms->tcon->tid,
4456 io_parms->tcon->ses->Suid,
4457 io_parms->offset, *nbytes);
4460 cifs_small_buf_release(req);
4461 free_rsp_buf(resp_buftype, rsp);
4465 int posix_info_sid_size(const void *beg, const void *end)
4473 subauth = *(u8 *)(beg+1);
4474 if (subauth < 1 || subauth > 15)
4477 total = 1 + 1 + 6 + 4*subauth;
4478 if (beg + total > end)
4484 int posix_info_parse(const void *beg, const void *end,
4485 struct smb2_posix_info_parsed *out)
4491 const void *owner_sid;
4492 const void *group_sid;
4495 /* if no end bound given, assume payload to be correct */
4497 const struct smb2_posix_info *p = beg;
4499 end = beg + le32_to_cpu(p->NextEntryOffset);
4500 /* last element will have a 0 offset, pick a sensible bound */
4505 /* check base buf */
4506 if (beg + sizeof(struct smb2_posix_info) > end)
4508 total_len = sizeof(struct smb2_posix_info);
4510 /* check owner sid */
4511 owner_sid = beg + total_len;
4512 sid_len = posix_info_sid_size(owner_sid, end);
4515 total_len += sid_len;
4517 /* check group sid */
4518 group_sid = beg + total_len;
4519 sid_len = posix_info_sid_size(group_sid, end);
4522 total_len += sid_len;
4524 /* check name len */
4525 if (beg + total_len + 4 > end)
4527 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
4528 if (name_len < 1 || name_len > 0xFFFF)
4533 name = beg + total_len;
4534 if (name + name_len > end)
4536 total_len += name_len;
4540 out->size = total_len;
4541 out->name_len = name_len;
4543 memcpy(&out->owner, owner_sid,
4544 posix_info_sid_size(owner_sid, end));
4545 memcpy(&out->group, group_sid,
4546 posix_info_sid_size(group_sid, end));
4551 static int posix_info_extra_size(const void *beg, const void *end)
4553 int len = posix_info_parse(beg, end, NULL);
4557 return len - sizeof(struct smb2_posix_info);
4561 num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
4565 unsigned int entrycount = 0;
4566 unsigned int next_offset = 0;
4568 FILE_DIRECTORY_INFO *dir_info;
4570 if (bufstart == NULL)
4573 entryptr = bufstart;
4576 if (entryptr + next_offset < entryptr ||
4577 entryptr + next_offset > end_of_buf ||
4578 entryptr + next_offset + size > end_of_buf) {
4579 cifs_dbg(VFS, "malformed search entry would overflow\n");
4583 entryptr = entryptr + next_offset;
4584 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4586 if (infotype == SMB_FIND_FILE_POSIX_INFO)
4587 len = posix_info_extra_size(entryptr, end_of_buf);
4589 len = le32_to_cpu(dir_info->FileNameLength);
4592 entryptr + len < entryptr ||
4593 entryptr + len > end_of_buf ||
4594 entryptr + len + size > end_of_buf) {
4595 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4600 *lastentry = entryptr;
4603 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
4614 int SMB2_query_directory_init(const unsigned int xid,
4615 struct cifs_tcon *tcon,
4616 struct TCP_Server_Info *server,
4617 struct smb_rqst *rqst,
4618 u64 persistent_fid, u64 volatile_fid,
4619 int index, int info_level)
4621 struct smb2_query_directory_req *req;
4622 unsigned char *bufptr;
4623 __le16 asteriks = cpu_to_le16('*');
4624 unsigned int output_size = CIFSMaxBufSize -
4625 MAX_SMB2_CREATE_RESPONSE_SIZE -
4626 MAX_SMB2_CLOSE_RESPONSE_SIZE;
4627 unsigned int total_len;
4628 struct kvec *iov = rqst->rq_iov;
4631 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
4632 (void **) &req, &total_len);
4636 switch (info_level) {
4637 case SMB_FIND_FILE_DIRECTORY_INFO:
4638 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4640 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4641 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4643 case SMB_FIND_FILE_POSIX_INFO:
4644 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
4647 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4652 req->FileIndex = cpu_to_le32(index);
4653 req->PersistentFileId = persistent_fid;
4654 req->VolatileFileId = volatile_fid;
4657 bufptr = req->Buffer;
4658 memcpy(bufptr, &asteriks, len);
4660 req->FileNameOffset =
4661 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
4662 req->FileNameLength = cpu_to_le16(len);
4664 * BB could be 30 bytes or so longer if we used SMB2 specific
4665 * buffer lengths, but this is safe and close enough.
4667 output_size = min_t(unsigned int, output_size, server->maxBuf);
4668 output_size = min_t(unsigned int, output_size, 2 << 15);
4669 req->OutputBufferLength = cpu_to_le32(output_size);
4671 iov[0].iov_base = (char *)req;
4673 iov[0].iov_len = total_len - 1;
4675 iov[1].iov_base = (char *)(req->Buffer);
4676 iov[1].iov_len = len;
4678 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4679 tcon->ses->Suid, index, output_size);
4684 void SMB2_query_directory_free(struct smb_rqst *rqst)
4686 if (rqst && rqst->rq_iov) {
4687 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4692 smb2_parse_query_directory(struct cifs_tcon *tcon,
4693 struct kvec *rsp_iov,
4695 struct cifs_search_info *srch_inf)
4697 struct smb2_query_directory_rsp *rsp;
4698 size_t info_buf_size;
4702 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
4704 switch (srch_inf->info_level) {
4705 case SMB_FIND_FILE_DIRECTORY_INFO:
4706 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4708 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4709 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4711 case SMB_FIND_FILE_POSIX_INFO:
4712 /* note that posix payload are variable size */
4713 info_buf_size = sizeof(struct smb2_posix_info);
4716 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4717 srch_inf->info_level);
4721 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4722 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
4725 cifs_tcon_dbg(VFS, "bad info payload");
4729 srch_inf->unicode = true;
4731 if (srch_inf->ntwrk_buf_start) {
4732 if (srch_inf->smallBuf)
4733 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4735 cifs_buf_release(srch_inf->ntwrk_buf_start);
4737 srch_inf->ntwrk_buf_start = (char *)rsp;
4738 srch_inf->srch_entries_start = srch_inf->last_entry =
4739 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4740 end_of_smb = rsp_iov->iov_len + (char *)rsp;
4742 srch_inf->entries_in_buffer = num_entries(
4743 srch_inf->info_level,
4744 srch_inf->srch_entries_start,
4746 &srch_inf->last_entry,
4749 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4750 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4751 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4752 srch_inf->srch_entries_start, srch_inf->last_entry);
4753 if (resp_buftype == CIFS_LARGE_BUFFER)
4754 srch_inf->smallBuf = false;
4755 else if (resp_buftype == CIFS_SMALL_BUFFER)
4756 srch_inf->smallBuf = true;
4758 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
4764 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4765 u64 persistent_fid, u64 volatile_fid, int index,
4766 struct cifs_search_info *srch_inf)
4768 struct smb_rqst rqst;
4769 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
4770 struct smb2_query_directory_rsp *rsp = NULL;
4771 int resp_buftype = CIFS_NO_BUFFER;
4772 struct kvec rsp_iov;
4774 struct cifs_ses *ses = tcon->ses;
4775 struct TCP_Server_Info *server = cifs_pick_channel(ses);
4778 if (!ses || !(ses->server))
4781 if (smb3_encryption_required(tcon))
4782 flags |= CIFS_TRANSFORM_REQ;
4784 memset(&rqst, 0, sizeof(struct smb_rqst));
4785 memset(&iov, 0, sizeof(iov));
4787 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
4789 rc = SMB2_query_directory_init(xid, tcon, server,
4790 &rqst, persistent_fid,
4791 volatile_fid, index,
4792 srch_inf->info_level);
4796 rc = cifs_send_recv(xid, ses, server,
4797 &rqst, &resp_buftype, flags, &rsp_iov);
4798 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
4801 if (rc == -ENODATA &&
4802 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
4803 trace_smb3_query_dir_done(xid, persistent_fid,
4804 tcon->tid, tcon->ses->Suid, index, 0);
4805 srch_inf->endOfSearch = true;
4808 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4809 tcon->ses->Suid, index, 0, rc);
4810 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
4815 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
4818 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4819 tcon->ses->Suid, index, 0, rc);
4822 resp_buftype = CIFS_NO_BUFFER;
4824 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4825 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
4828 SMB2_query_directory_free(&rqst);
4829 free_rsp_buf(resp_buftype, rsp);
4834 SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
4835 struct smb_rqst *rqst,
4836 u64 persistent_fid, u64 volatile_fid, u32 pid,
4837 u8 info_class, u8 info_type, u32 additional_info,
4838 void **data, unsigned int *size)
4840 struct smb2_set_info_req *req;
4841 struct kvec *iov = rqst->rq_iov;
4842 unsigned int i, total_len;
4845 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
4846 (void **) &req, &total_len);
4850 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4851 req->InfoType = info_type;
4852 req->FileInfoClass = info_class;
4853 req->PersistentFileId = persistent_fid;
4854 req->VolatileFileId = volatile_fid;
4855 req->AdditionalInformation = cpu_to_le32(additional_info);
4858 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
4859 req->BufferLength = cpu_to_le32(*size);
4861 memcpy(req->Buffer, *data, *size);
4864 iov[0].iov_base = (char *)req;
4866 iov[0].iov_len = total_len - 1;
4868 for (i = 1; i < rqst->rq_nvec; i++) {
4869 le32_add_cpu(&req->BufferLength, size[i]);
4870 iov[i].iov_base = (char *)data[i];
4871 iov[i].iov_len = size[i];
4878 SMB2_set_info_free(struct smb_rqst *rqst)
4880 if (rqst && rqst->rq_iov)
4881 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
4885 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4886 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4887 u8 info_type, u32 additional_info, unsigned int num,
4888 void **data, unsigned int *size)
4890 struct smb_rqst rqst;
4891 struct smb2_set_info_rsp *rsp = NULL;
4893 struct kvec rsp_iov;
4896 struct cifs_ses *ses = tcon->ses;
4897 struct TCP_Server_Info *server = cifs_pick_channel(ses);
4900 if (!ses || !server)
4906 if (smb3_encryption_required(tcon))
4907 flags |= CIFS_TRANSFORM_REQ;
4909 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4913 memset(&rqst, 0, sizeof(struct smb_rqst));
4917 rc = SMB2_set_info_init(tcon, server,
4918 &rqst, persistent_fid, volatile_fid, pid,
4919 info_class, info_type, additional_info,
4927 rc = cifs_send_recv(xid, ses, server,
4928 &rqst, &resp_buftype, flags,
4930 SMB2_set_info_free(&rqst);
4931 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
4934 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
4935 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4936 ses->Suid, info_class, (__u32)info_type, rc);
4939 free_rsp_buf(resp_buftype, rsp);
4945 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4946 u64 volatile_fid, u32 pid, __le64 *eof)
4948 struct smb2_file_eof_info info;
4952 info.EndOfFile = *eof;
4955 size = sizeof(struct smb2_file_eof_info);
4957 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4958 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4959 0, 1, &data, &size);
4963 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4964 u64 persistent_fid, u64 volatile_fid,
4965 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4967 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4968 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4969 1, (void **)&pnntsd, &pacllen);
4973 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4974 u64 persistent_fid, u64 volatile_fid,
4975 struct smb2_file_full_ea_info *buf, int len)
4977 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4978 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4979 0, 1, (void **)&buf, &len);
4983 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4984 const u64 persistent_fid, const u64 volatile_fid,
4987 struct smb_rqst rqst;
4989 struct smb2_oplock_break *req = NULL;
4990 struct cifs_ses *ses = tcon->ses;
4991 struct TCP_Server_Info *server = cifs_pick_channel(ses);
4992 int flags = CIFS_OBREAK_OP;
4993 unsigned int total_len;
4995 struct kvec rsp_iov;
4998 cifs_dbg(FYI, "SMB2_oplock_break\n");
4999 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5000 (void **) &req, &total_len);
5004 if (smb3_encryption_required(tcon))
5005 flags |= CIFS_TRANSFORM_REQ;
5007 req->VolatileFid = volatile_fid;
5008 req->PersistentFid = persistent_fid;
5009 req->OplockLevel = oplock_level;
5010 req->sync_hdr.CreditRequest = cpu_to_le16(1);
5012 flags |= CIFS_NO_RSP_BUF;
5014 iov[0].iov_base = (char *)req;
5015 iov[0].iov_len = total_len;
5017 memset(&rqst, 0, sizeof(struct smb_rqst));
5021 rc = cifs_send_recv(xid, ses, server,
5022 &rqst, &resp_buf_type, flags, &rsp_iov);
5023 cifs_small_buf_release(req);
5026 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5027 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
5034 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
5035 struct kstatfs *kst)
5037 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
5038 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
5039 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
5040 kst->f_bfree = kst->f_bavail =
5041 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
5046 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
5047 struct kstatfs *kst)
5049 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
5050 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
5051 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
5052 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
5053 kst->f_bavail = kst->f_bfree;
5055 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
5056 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5057 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
5058 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5059 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
5065 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
5066 struct TCP_Server_Info *server,
5067 int level, int outbuf_len, u64 persistent_fid,
5071 struct smb2_query_info_req *req;
5072 unsigned int total_len;
5074 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
5076 if ((tcon->ses == NULL) || server == NULL)
5079 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
5080 (void **) &req, &total_len);
5084 req->InfoType = SMB2_O_INFO_FILESYSTEM;
5085 req->FileInfoClass = level;
5086 req->PersistentFileId = persistent_fid;
5087 req->VolatileFileId = volatile_fid;
5089 req->InputBufferOffset =
5090 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
5091 req->OutputBufferLength = cpu_to_le32(
5092 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
5094 iov->iov_base = (char *)req;
5095 iov->iov_len = total_len;
5100 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
5101 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5103 struct smb_rqst rqst;
5104 struct smb2_query_info_rsp *rsp = NULL;
5106 struct kvec rsp_iov;
5109 struct cifs_ses *ses = tcon->ses;
5110 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5111 FILE_SYSTEM_POSIX_INFO *info = NULL;
5114 rc = build_qfs_info_req(&iov, tcon, server,
5115 FS_POSIX_INFORMATION,
5116 sizeof(FILE_SYSTEM_POSIX_INFO),
5117 persistent_fid, volatile_fid);
5121 if (smb3_encryption_required(tcon))
5122 flags |= CIFS_TRANSFORM_REQ;
5124 memset(&rqst, 0, sizeof(struct smb_rqst));
5128 rc = cifs_send_recv(xid, ses, server,
5129 &rqst, &resp_buftype, flags, &rsp_iov);
5130 cifs_small_buf_release(iov.iov_base);
5132 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5133 goto posix_qfsinf_exit;
5135 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5137 info = (FILE_SYSTEM_POSIX_INFO *)(
5138 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5139 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5140 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5141 sizeof(FILE_SYSTEM_POSIX_INFO));
5143 copy_posix_fs_info_to_kstatfs(info, fsdata);
5146 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5151 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5152 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5154 struct smb_rqst rqst;
5155 struct smb2_query_info_rsp *rsp = NULL;
5157 struct kvec rsp_iov;
5160 struct cifs_ses *ses = tcon->ses;
5161 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5162 struct smb2_fs_full_size_info *info = NULL;
5165 rc = build_qfs_info_req(&iov, tcon, server,
5166 FS_FULL_SIZE_INFORMATION,
5167 sizeof(struct smb2_fs_full_size_info),
5168 persistent_fid, volatile_fid);
5172 if (smb3_encryption_required(tcon))
5173 flags |= CIFS_TRANSFORM_REQ;
5175 memset(&rqst, 0, sizeof(struct smb_rqst));
5179 rc = cifs_send_recv(xid, ses, server,
5180 &rqst, &resp_buftype, flags, &rsp_iov);
5181 cifs_small_buf_release(iov.iov_base);
5183 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5186 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5188 info = (struct smb2_fs_full_size_info *)(
5189 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5190 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5191 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5192 sizeof(struct smb2_fs_full_size_info));
5194 smb2_copy_fs_info_to_kstatfs(info, fsdata);
5197 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5202 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
5203 u64 persistent_fid, u64 volatile_fid, int level)
5205 struct smb_rqst rqst;
5206 struct smb2_query_info_rsp *rsp = NULL;
5208 struct kvec rsp_iov;
5210 int resp_buftype, max_len, min_len;
5211 struct cifs_ses *ses = tcon->ses;
5212 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5213 unsigned int rsp_len, offset;
5216 if (level == FS_DEVICE_INFORMATION) {
5217 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5218 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5219 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5220 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5221 min_len = MIN_FS_ATTR_INFO_SIZE;
5222 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5223 max_len = sizeof(struct smb3_fs_ss_info);
5224 min_len = sizeof(struct smb3_fs_ss_info);
5225 } else if (level == FS_VOLUME_INFORMATION) {
5226 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5227 min_len = sizeof(struct smb3_fs_vol_info);
5229 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
5233 rc = build_qfs_info_req(&iov, tcon, server,
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 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5256 offset = le16_to_cpu(rsp->OutputBufferOffset);
5257 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
5261 if (level == FS_ATTRIBUTE_INFORMATION)
5262 memcpy(&tcon->fsAttrInfo, offset
5263 + (char *)rsp, min_t(unsigned int,
5265 else if (level == FS_DEVICE_INFORMATION)
5266 memcpy(&tcon->fsDevInfo, offset
5267 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
5268 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5269 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
5270 (offset + (char *)rsp);
5271 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5272 tcon->perf_sector_size =
5273 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
5274 } else if (level == FS_VOLUME_INFORMATION) {
5275 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5276 (offset + (char *)rsp);
5277 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5278 tcon->vol_create_time = vol_info->VolumeCreationTime;
5282 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5287 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5288 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5289 const __u32 num_lock, struct smb2_lock_element *buf)
5291 struct smb_rqst rqst;
5293 struct smb2_lock_req *req = NULL;
5295 struct kvec rsp_iov;
5298 int flags = CIFS_NO_RSP_BUF;
5299 unsigned int total_len;
5300 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5302 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
5304 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
5305 (void **) &req, &total_len);
5309 if (smb3_encryption_required(tcon))
5310 flags |= CIFS_TRANSFORM_REQ;
5312 req->sync_hdr.ProcessId = cpu_to_le32(pid);
5313 req->LockCount = cpu_to_le16(num_lock);
5315 req->PersistentFileId = persist_fid;
5316 req->VolatileFileId = volatile_fid;
5318 count = num_lock * sizeof(struct smb2_lock_element);
5320 iov[0].iov_base = (char *)req;
5321 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
5322 iov[1].iov_base = (char *)buf;
5323 iov[1].iov_len = count;
5325 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
5327 memset(&rqst, 0, sizeof(struct smb_rqst));
5331 rc = cifs_send_recv(xid, tcon->ses, server,
5332 &rqst, &resp_buf_type, flags,
5334 cifs_small_buf_release(req);
5336 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
5337 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
5338 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
5339 tcon->ses->Suid, rc);
5346 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
5347 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5348 const __u64 length, const __u64 offset, const __u32 lock_flags,
5351 struct smb2_lock_element lock;
5353 lock.Offset = cpu_to_le64(offset);
5354 lock.Length = cpu_to_le64(length);
5355 lock.Flags = cpu_to_le32(lock_flags);
5356 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5357 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5359 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5363 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5364 __u8 *lease_key, const __le32 lease_state)
5366 struct smb_rqst rqst;
5368 struct smb2_lease_ack *req = NULL;
5369 struct cifs_ses *ses = tcon->ses;
5370 int flags = CIFS_OBREAK_OP;
5371 unsigned int total_len;
5373 struct kvec rsp_iov;
5375 __u64 *please_key_high;
5376 __u64 *please_key_low;
5377 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5379 cifs_dbg(FYI, "SMB2_lease_break\n");
5380 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5381 (void **) &req, &total_len);
5385 if (smb3_encryption_required(tcon))
5386 flags |= CIFS_TRANSFORM_REQ;
5388 req->sync_hdr.CreditRequest = cpu_to_le16(1);
5389 req->StructureSize = cpu_to_le16(36);
5392 memcpy(req->LeaseKey, lease_key, 16);
5393 req->LeaseState = lease_state;
5395 flags |= CIFS_NO_RSP_BUF;
5397 iov[0].iov_base = (char *)req;
5398 iov[0].iov_len = total_len;
5400 memset(&rqst, 0, sizeof(struct smb_rqst));
5404 rc = cifs_send_recv(xid, ses, server,
5405 &rqst, &resp_buf_type, flags, &rsp_iov);
5406 cifs_small_buf_release(req);
5408 please_key_low = (__u64 *)lease_key;
5409 please_key_high = (__u64 *)(lease_key+8);
5411 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5412 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5413 ses->Suid, *please_key_low, *please_key_high, rc);
5414 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
5416 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5417 ses->Suid, *please_key_low, *please_key_high);