4 * Copyright (C) International Business Machines Corp., 2009, 2013
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
9 * Contains the routines for constructing the SMB2 PDUs themselves
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_SALT_SIZE);
431 get_random_bytes(pneg_ctxt->Salt, SMB311_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");
570 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
571 pr_warn_once("Invalid SMB3 hash algorithm count\n");
572 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
573 pr_warn_once("unknown SMB3 hash algorithm\n");
576 static void decode_compress_ctx(struct TCP_Server_Info *server,
577 struct smb2_compression_capabilities_context *ctxt)
579 unsigned int len = le16_to_cpu(ctxt->DataLength);
581 /* sizeof compress context is a one element compression capbility struct */
583 pr_warn_once("server sent bad compression cntxt\n");
586 if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
587 pr_warn_once("Invalid SMB3 compress algorithm count\n");
590 if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
591 pr_warn_once("unknown compression algorithm\n");
594 server->compress_algorithm = ctxt->CompressionAlgorithms[0];
597 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
598 struct smb2_encryption_neg_context *ctxt)
600 unsigned int len = le16_to_cpu(ctxt->DataLength);
602 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
603 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
604 pr_warn_once("server sent bad crypto ctxt len\n");
608 if (le16_to_cpu(ctxt->CipherCount) != 1) {
609 pr_warn_once("Invalid SMB3.11 cipher count\n");
612 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
613 if (require_gcm_256) {
614 if (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM) {
615 cifs_dbg(VFS, "Server does not support requested encryption type (AES256 GCM)\n");
618 } else if (ctxt->Ciphers[0] == 0) {
620 * e.g. if server only supported AES256_CCM (very unlikely)
621 * or server supported no encryption types or had all disabled.
622 * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case
623 * in which mount requested encryption ("seal") checks later
624 * on during tree connection will return proper rc, but if
625 * seal not requested by client, since server is allowed to
626 * return 0 to indicate no supported cipher, we can't fail here
628 server->cipher_type = 0;
629 server->capabilities &= ~SMB2_GLOBAL_CAP_ENCRYPTION;
630 pr_warn_once("Server does not support requested encryption types\n");
632 } else if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
633 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM) &&
634 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM)) {
635 /* server returned a cipher we didn't ask for */
636 pr_warn_once("Invalid SMB3.11 cipher returned\n");
639 server->cipher_type = ctxt->Ciphers[0];
640 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
644 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
645 struct TCP_Server_Info *server,
646 unsigned int len_of_smb)
648 struct smb2_neg_context *pctx;
649 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
650 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
651 unsigned int len_of_ctxts, i;
654 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
655 if (len_of_smb <= offset) {
656 cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
660 len_of_ctxts = len_of_smb - offset;
662 for (i = 0; i < ctxt_cnt; i++) {
664 /* check that offset is not beyond end of SMB */
665 if (len_of_ctxts == 0)
668 if (len_of_ctxts < sizeof(struct smb2_neg_context))
671 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
672 clen = le16_to_cpu(pctx->DataLength);
673 if (clen > len_of_ctxts)
676 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
677 decode_preauth_context(
678 (struct smb2_preauth_neg_context *)pctx);
679 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
680 rc = decode_encrypt_ctx(server,
681 (struct smb2_encryption_neg_context *)pctx);
682 else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
683 decode_compress_ctx(server,
684 (struct smb2_compression_capabilities_context *)pctx);
685 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
686 server->posix_ext_supported = true;
688 cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
689 le16_to_cpu(pctx->ContextType));
693 /* offsets must be 8 byte aligned */
694 clen = (clen + 7) & ~0x7;
695 offset += clen + sizeof(struct smb2_neg_context);
696 len_of_ctxts -= clen;
701 static struct create_posix *
702 create_posix_buf(umode_t mode)
704 struct create_posix *buf;
706 buf = kzalloc(sizeof(struct create_posix),
711 buf->ccontext.DataOffset =
712 cpu_to_le16(offsetof(struct create_posix, Mode));
713 buf->ccontext.DataLength = cpu_to_le32(4);
714 buf->ccontext.NameOffset =
715 cpu_to_le16(offsetof(struct create_posix, Name));
716 buf->ccontext.NameLength = cpu_to_le16(16);
718 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
729 buf->Name[10] = 0x83;
730 buf->Name[11] = 0xDE;
731 buf->Name[12] = 0x96;
732 buf->Name[13] = 0x8B;
733 buf->Name[14] = 0xCD;
734 buf->Name[15] = 0x7C;
735 buf->Mode = cpu_to_le32(mode);
736 cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
741 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
743 struct smb2_create_req *req = iov[0].iov_base;
744 unsigned int num = *num_iovec;
746 iov[num].iov_base = create_posix_buf(mode);
747 if (mode == ACL_NO_MODE)
748 cifs_dbg(FYI, "Invalid mode\n");
749 if (iov[num].iov_base == NULL)
751 iov[num].iov_len = sizeof(struct create_posix);
752 if (!req->CreateContextsOffset)
753 req->CreateContextsOffset = cpu_to_le32(
754 sizeof(struct smb2_create_req) +
755 iov[num - 1].iov_len);
756 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
757 *num_iovec = num + 1;
764 * SMB2 Worker functions follow:
766 * The general structure of the worker functions is:
767 * 1) Call smb2_init (assembles SMB2 header)
768 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
769 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
770 * 4) Decode SMB2 command specific fields in the fixed length area
771 * 5) Decode variable length data area (if any for this SMB2 command type)
772 * 6) Call free smb buffer
778 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
780 struct smb_rqst rqst;
781 struct smb2_negotiate_req *req;
782 struct smb2_negotiate_rsp *rsp;
787 struct TCP_Server_Info *server = cifs_ses_server(ses);
788 int blob_offset, blob_length;
790 int flags = CIFS_NEG_OP;
791 unsigned int total_len;
793 cifs_dbg(FYI, "Negotiate protocol\n");
796 WARN(1, "%s: server is NULL!\n", __func__);
800 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
801 (void **) &req, &total_len);
805 req->sync_hdr.SessionId = 0;
807 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
808 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
810 if (strcmp(server->vals->version_string,
811 SMB3ANY_VERSION_STRING) == 0) {
812 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
813 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
814 req->DialectCount = cpu_to_le16(2);
816 } else if (strcmp(server->vals->version_string,
817 SMBDEFAULT_VERSION_STRING) == 0) {
818 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
819 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
820 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
821 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
822 req->DialectCount = cpu_to_le16(4);
825 /* otherwise send specific dialect */
826 req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
827 req->DialectCount = cpu_to_le16(1);
831 /* only one of SMB2 signing flags may be set in SMB2 request */
833 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
834 else if (global_secflags & CIFSSEC_MAY_SIGN)
835 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
837 req->SecurityMode = 0;
839 req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
841 /* ClientGUID must be zero for SMB2.02 dialect */
842 if (server->vals->protocol_id == SMB20_PROT_ID)
843 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
845 memcpy(req->ClientGUID, server->client_guid,
846 SMB2_CLIENT_GUID_SIZE);
847 if ((server->vals->protocol_id == SMB311_PROT_ID) ||
848 (strcmp(server->vals->version_string,
849 SMBDEFAULT_VERSION_STRING) == 0))
850 assemble_neg_contexts(req, server, &total_len);
852 iov[0].iov_base = (char *)req;
853 iov[0].iov_len = total_len;
855 memset(&rqst, 0, sizeof(struct smb_rqst));
859 rc = cifs_send_recv(xid, ses, server,
860 &rqst, &resp_buftype, flags, &rsp_iov);
861 cifs_small_buf_release(req);
862 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
864 * No tcon so can't do
865 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
867 if (rc == -EOPNOTSUPP) {
868 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");
873 if (strcmp(server->vals->version_string,
874 SMB3ANY_VERSION_STRING) == 0) {
875 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
877 "SMB2 dialect returned but not requested\n");
879 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
881 "SMB2.1 dialect returned but not requested\n");
884 } else if (strcmp(server->vals->version_string,
885 SMBDEFAULT_VERSION_STRING) == 0) {
886 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
888 "SMB2 dialect returned but not requested\n");
890 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
891 /* ops set to 3.0 by default for default so update */
892 server->ops = &smb21_operations;
893 server->vals = &smb21_values;
894 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
895 server->ops = &smb311_operations;
896 server->vals = &smb311_values;
898 } else if (le16_to_cpu(rsp->DialectRevision) !=
899 server->vals->protocol_id) {
900 /* if requested single dialect ensure returned dialect matched */
901 cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
902 le16_to_cpu(rsp->DialectRevision));
906 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
908 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
909 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
910 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
911 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
912 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
913 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
914 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
915 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
916 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
917 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
919 cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
920 le16_to_cpu(rsp->DialectRevision));
924 server->dialect = le16_to_cpu(rsp->DialectRevision);
927 * Keep a copy of the hash after negprot. This hash will be
928 * the starting hash value for all sessions made from this
931 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
932 SMB2_PREAUTH_HASH_SIZE);
934 /* SMB2 only has an extended negflavor */
935 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
936 /* set it to the maximum buffer size value we can send with 1 credit */
937 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
938 SMB2_MAX_BUFFER_SIZE);
939 server->max_read = le32_to_cpu(rsp->MaxReadSize);
940 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
941 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
942 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
943 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
945 server->capabilities = le32_to_cpu(rsp->Capabilities);
947 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
949 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
950 (struct smb2_sync_hdr *)rsp);
952 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
954 * ses->sectype = RawNTLMSSP;
955 * but for time being this is our only auth choice so doesn't matter.
956 * We just found a server which sets blob length to zero expecting raw.
958 if (blob_length == 0) {
959 cifs_dbg(FYI, "missing security blob on negprot\n");
960 server->sec_ntlmssp = true;
963 rc = cifs_enable_signing(server, ses->sign);
967 rc = decode_negTokenInit(security_blob, blob_length, server);
974 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
975 if (rsp->NegotiateContextCount)
976 rc = smb311_decode_neg_context(rsp, server,
979 cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
982 free_rsp_buf(resp_buftype, rsp);
986 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
989 struct validate_negotiate_info_req *pneg_inbuf;
990 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
992 u32 inbuflen; /* max of 4 dialects */
993 struct TCP_Server_Info *server = tcon->ses->server;
995 cifs_dbg(FYI, "validate negotiate\n");
997 /* In SMB3.11 preauth integrity supersedes validate negotiate */
998 if (server->dialect == SMB311_PROT_ID)
1002 * validation ioctl must be signed, so no point sending this if we
1003 * can not sign it (ie are not known user). Even if signing is not
1004 * required (enabled but not negotiated), in those cases we selectively
1005 * sign just this, the first and only signed request on a connection.
1006 * Having validation of negotiate info helps reduce attack vectors.
1008 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
1009 return 0; /* validation requires signing */
1011 if (tcon->ses->user_name == NULL) {
1012 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
1013 return 0; /* validation requires signing */
1016 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
1017 cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
1019 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
1023 pneg_inbuf->Capabilities =
1024 cpu_to_le32(server->vals->req_capabilities);
1025 memcpy(pneg_inbuf->Guid, server->client_guid,
1026 SMB2_CLIENT_GUID_SIZE);
1028 if (tcon->ses->sign)
1029 pneg_inbuf->SecurityMode =
1030 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
1031 else if (global_secflags & CIFSSEC_MAY_SIGN)
1032 pneg_inbuf->SecurityMode =
1033 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
1035 pneg_inbuf->SecurityMode = 0;
1038 if (strcmp(server->vals->version_string,
1039 SMB3ANY_VERSION_STRING) == 0) {
1040 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
1041 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
1042 pneg_inbuf->DialectCount = cpu_to_le16(2);
1043 /* structure is big enough for 3 dialects, sending only 2 */
1044 inbuflen = sizeof(*pneg_inbuf) -
1045 (2 * sizeof(pneg_inbuf->Dialects[0]));
1046 } else if (strcmp(server->vals->version_string,
1047 SMBDEFAULT_VERSION_STRING) == 0) {
1048 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
1049 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
1050 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
1051 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
1052 pneg_inbuf->DialectCount = cpu_to_le16(4);
1053 /* structure is big enough for 3 dialects */
1054 inbuflen = sizeof(*pneg_inbuf);
1056 /* otherwise specific dialect was requested */
1057 pneg_inbuf->Dialects[0] =
1058 cpu_to_le16(server->vals->protocol_id);
1059 pneg_inbuf->DialectCount = cpu_to_le16(1);
1060 /* structure is big enough for 3 dialects, sending only 1 */
1061 inbuflen = sizeof(*pneg_inbuf) -
1062 sizeof(pneg_inbuf->Dialects[0]) * 2;
1065 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1066 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
1067 (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
1068 (char **)&pneg_rsp, &rsplen);
1069 if (rc == -EOPNOTSUPP) {
1071 * Old Windows versions or Netapp SMB server can return
1072 * not supported error. Client should accept it.
1074 cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
1076 goto out_free_inbuf;
1077 } else if (rc != 0) {
1078 cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
1081 goto out_free_inbuf;
1085 if (rsplen != sizeof(*pneg_rsp)) {
1086 cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
1089 /* relax check since Mac returns max bufsize allowed on ioctl */
1090 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1094 /* check validate negotiate info response matches what we got earlier */
1095 if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
1098 if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
1101 /* do not validate server guid because not saved at negprot time yet */
1103 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1104 SMB2_LARGE_FILES) != server->capabilities)
1107 /* validate negotiate successful */
1109 cifs_dbg(FYI, "validate negotiate info successful\n");
1113 cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1122 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1124 switch (requested) {
1131 if (server->sec_ntlmssp &&
1132 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1134 if ((server->sec_kerberos || server->sec_mskerberos) &&
1135 (global_secflags & CIFSSEC_MAY_KRB5))
1143 struct SMB2_sess_data {
1145 struct cifs_ses *ses;
1146 struct nls_table *nls_cp;
1147 void (*func)(struct SMB2_sess_data *);
1149 u64 previous_session;
1151 /* we will send the SMB in three pieces:
1152 * a fixed length beginning part, an optional
1153 * SPNEGO blob (which can be zero length), and a
1154 * last part which will include the strings
1155 * and rest of bcc area. This allows us to avoid
1156 * a large buffer 17K allocation
1163 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1166 struct cifs_ses *ses = sess_data->ses;
1167 struct smb2_sess_setup_req *req;
1168 struct TCP_Server_Info *server = cifs_ses_server(ses);
1169 unsigned int total_len;
1171 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
1177 if (sess_data->ses->binding) {
1178 req->sync_hdr.SessionId = sess_data->ses->Suid;
1179 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1180 req->PreviousSessionId = 0;
1181 req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
1183 /* First session, not a reauthenticate */
1184 req->sync_hdr.SessionId = 0;
1186 * if reconnect, we need to send previous sess id
1189 req->PreviousSessionId = sess_data->previous_session;
1190 req->Flags = 0; /* MBZ */
1193 /* enough to enable echos and oplocks and one max size write */
1194 req->sync_hdr.CreditRequest = cpu_to_le16(130);
1196 /* only one of SMB2 signing flags may be set in SMB2 request */
1198 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1199 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1200 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1202 req->SecurityMode = 0;
1204 #ifdef CONFIG_CIFS_DFS_UPCALL
1205 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1207 req->Capabilities = 0;
1208 #endif /* DFS_UPCALL */
1210 req->Channel = 0; /* MBZ */
1212 sess_data->iov[0].iov_base = (char *)req;
1214 sess_data->iov[0].iov_len = total_len - 1;
1216 * This variable will be used to clear the buffer
1217 * allocated above in case of any error in the calling function.
1219 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1225 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1227 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1228 sess_data->buf0_type = CIFS_NO_BUFFER;
1232 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1235 struct smb_rqst rqst;
1236 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1237 struct kvec rsp_iov = { NULL, 0 };
1239 /* Testing shows that buffer offset must be at location of Buffer[0] */
1240 req->SecurityBufferOffset =
1241 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1242 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1244 memset(&rqst, 0, sizeof(struct smb_rqst));
1245 rqst.rq_iov = sess_data->iov;
1248 /* BB add code to build os and lm fields */
1249 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1250 cifs_ses_server(sess_data->ses),
1252 &sess_data->buf0_type,
1253 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
1254 cifs_small_buf_release(sess_data->iov[0].iov_base);
1255 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1261 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1264 struct cifs_ses *ses = sess_data->ses;
1265 struct TCP_Server_Info *server = cifs_ses_server(ses);
1267 mutex_lock(&server->srv_mutex);
1268 if (server->ops->generate_signingkey) {
1269 rc = server->ops->generate_signingkey(ses);
1272 "SMB3 session key generation failed\n");
1273 mutex_unlock(&server->srv_mutex);
1277 if (!server->session_estab) {
1278 server->sequence_number = 0x2;
1279 server->session_estab = true;
1281 mutex_unlock(&server->srv_mutex);
1283 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1284 /* keep existing ses state if binding */
1285 if (!ses->binding) {
1286 spin_lock(&GlobalMid_Lock);
1287 ses->status = CifsGood;
1288 ses->need_reconnect = false;
1289 spin_unlock(&GlobalMid_Lock);
1295 #ifdef CONFIG_CIFS_UPCALL
1297 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1300 struct cifs_ses *ses = sess_data->ses;
1301 struct cifs_spnego_msg *msg;
1302 struct key *spnego_key = NULL;
1303 struct smb2_sess_setup_rsp *rsp = NULL;
1305 rc = SMB2_sess_alloc_buffer(sess_data);
1309 spnego_key = cifs_get_spnego_key(ses);
1310 if (IS_ERR(spnego_key)) {
1311 rc = PTR_ERR(spnego_key);
1313 cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
1318 msg = spnego_key->payload.data[0];
1320 * check version field to make sure that cifs.upcall is
1321 * sending us a response in an expected form
1323 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1324 cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
1325 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1327 goto out_put_spnego_key;
1330 /* keep session key if binding */
1331 if (!ses->binding) {
1332 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1334 if (!ses->auth_key.response) {
1335 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1338 goto out_put_spnego_key;
1340 ses->auth_key.len = msg->sesskey_len;
1343 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1344 sess_data->iov[1].iov_len = msg->secblob_len;
1346 rc = SMB2_sess_sendreceive(sess_data);
1348 goto out_put_spnego_key;
1350 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1351 /* keep session id and flags if binding */
1352 if (!ses->binding) {
1353 ses->Suid = rsp->sync_hdr.SessionId;
1354 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1357 rc = SMB2_sess_establish_session(sess_data);
1359 key_invalidate(spnego_key);
1360 key_put(spnego_key);
1362 sess_data->result = rc;
1363 sess_data->func = NULL;
1364 SMB2_sess_free_buffer(sess_data);
1368 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1370 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1371 sess_data->result = -EOPNOTSUPP;
1372 sess_data->func = NULL;
1377 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1380 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1383 struct cifs_ses *ses = sess_data->ses;
1384 struct smb2_sess_setup_rsp *rsp = NULL;
1385 char *ntlmssp_blob = NULL;
1386 bool use_spnego = false; /* else use raw ntlmssp */
1387 u16 blob_length = 0;
1390 * If memory allocation is successful, caller of this function
1393 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1394 if (!ses->ntlmssp) {
1398 ses->ntlmssp->sesskey_per_smbsess = true;
1400 rc = SMB2_sess_alloc_buffer(sess_data);
1404 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1406 if (ntlmssp_blob == NULL) {
1411 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1413 /* BB eventually need to add this */
1414 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1418 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1419 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1421 sess_data->iov[1].iov_base = ntlmssp_blob;
1422 sess_data->iov[1].iov_len = blob_length;
1424 rc = SMB2_sess_sendreceive(sess_data);
1425 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1427 /* If true, rc here is expected and not an error */
1428 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1429 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1435 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1436 le16_to_cpu(rsp->SecurityBufferOffset)) {
1437 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1438 le16_to_cpu(rsp->SecurityBufferOffset));
1442 rc = decode_ntlmssp_challenge(rsp->Buffer,
1443 le16_to_cpu(rsp->SecurityBufferLength), ses);
1447 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1449 /* keep existing ses id and flags if binding */
1450 if (!ses->binding) {
1451 ses->Suid = rsp->sync_hdr.SessionId;
1452 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1456 kfree(ntlmssp_blob);
1457 SMB2_sess_free_buffer(sess_data);
1459 sess_data->result = 0;
1460 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1464 kfree(ses->ntlmssp);
1465 ses->ntlmssp = NULL;
1466 sess_data->result = rc;
1467 sess_data->func = NULL;
1471 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1474 struct cifs_ses *ses = sess_data->ses;
1475 struct smb2_sess_setup_req *req;
1476 struct smb2_sess_setup_rsp *rsp = NULL;
1477 unsigned char *ntlmssp_blob = NULL;
1478 bool use_spnego = false; /* else use raw ntlmssp */
1479 u16 blob_length = 0;
1481 rc = SMB2_sess_alloc_buffer(sess_data);
1485 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1486 req->sync_hdr.SessionId = ses->Suid;
1488 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1491 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1496 /* BB eventually need to add this */
1497 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1501 sess_data->iov[1].iov_base = ntlmssp_blob;
1502 sess_data->iov[1].iov_len = blob_length;
1504 rc = SMB2_sess_sendreceive(sess_data);
1508 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1510 /* keep existing ses id and flags if binding */
1511 if (!ses->binding) {
1512 ses->Suid = rsp->sync_hdr.SessionId;
1513 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1516 rc = SMB2_sess_establish_session(sess_data);
1517 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1518 if (ses->server->dialect < SMB30_PROT_ID) {
1519 cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
1521 * The session id is opaque in terms of endianness, so we can't
1522 * print it as a long long. we dump it as we got it on the wire
1524 cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
1526 cifs_dbg(VFS, "Session Key %*ph\n",
1527 SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
1528 cifs_dbg(VFS, "Signing Key %*ph\n",
1529 SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
1533 kfree(ntlmssp_blob);
1534 SMB2_sess_free_buffer(sess_data);
1535 kfree(ses->ntlmssp);
1536 ses->ntlmssp = NULL;
1537 sess_data->result = rc;
1538 sess_data->func = NULL;
1542 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1546 type = smb2_select_sectype(cifs_ses_server(ses), ses->sectype);
1547 cifs_dbg(FYI, "sess setup type %d\n", type);
1548 if (type == Unspecified) {
1549 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1555 sess_data->func = SMB2_auth_kerberos;
1558 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1561 cifs_dbg(VFS, "secType %d not supported!\n", type);
1569 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1570 const struct nls_table *nls_cp)
1573 struct TCP_Server_Info *server = cifs_ses_server(ses);
1574 struct SMB2_sess_data *sess_data;
1576 cifs_dbg(FYI, "Session Setup\n");
1579 WARN(1, "%s: server is NULL!\n", __func__);
1583 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1587 rc = SMB2_select_sec(ses, sess_data);
1590 sess_data->xid = xid;
1591 sess_data->ses = ses;
1592 sess_data->buf0_type = CIFS_NO_BUFFER;
1593 sess_data->nls_cp = (struct nls_table *) nls_cp;
1594 sess_data->previous_session = ses->Suid;
1597 * Initialize the session hash with the server one.
1599 memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
1600 SMB2_PREAUTH_HASH_SIZE);
1602 while (sess_data->func)
1603 sess_data->func(sess_data);
1605 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1606 cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
1607 rc = sess_data->result;
1614 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1616 struct smb_rqst rqst;
1617 struct smb2_logoff_req *req; /* response is also trivial struct */
1619 struct TCP_Server_Info *server;
1621 unsigned int total_len;
1623 struct kvec rsp_iov;
1626 cifs_dbg(FYI, "disconnect session %p\n", ses);
1628 if (ses && (ses->server))
1629 server = ses->server;
1633 /* no need to send SMB logoff if uid already closed due to reconnect */
1634 if (ses->need_reconnect)
1635 goto smb2_session_already_dead;
1637 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
1638 (void **) &req, &total_len);
1642 /* since no tcon, smb2_init can not do this, so do here */
1643 req->sync_hdr.SessionId = ses->Suid;
1645 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1646 flags |= CIFS_TRANSFORM_REQ;
1647 else if (server->sign)
1648 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1650 flags |= CIFS_NO_RSP_BUF;
1652 iov[0].iov_base = (char *)req;
1653 iov[0].iov_len = total_len;
1655 memset(&rqst, 0, sizeof(struct smb_rqst));
1659 rc = cifs_send_recv(xid, ses, ses->server,
1660 &rqst, &resp_buf_type, flags, &rsp_iov);
1661 cifs_small_buf_release(req);
1663 * No tcon so can't do
1664 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1667 smb2_session_already_dead:
1671 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1673 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1676 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1678 /* These are similar values to what Windows uses */
1679 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1681 tcon->max_chunks = 256;
1682 tcon->max_bytes_chunk = 1048576;
1683 tcon->max_bytes_copy = 16777216;
1687 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1688 struct cifs_tcon *tcon, const struct nls_table *cp)
1690 struct smb_rqst rqst;
1691 struct smb2_tree_connect_req *req;
1692 struct smb2_tree_connect_rsp *rsp = NULL;
1694 struct kvec rsp_iov = { NULL, 0 };
1698 __le16 *unc_path = NULL;
1700 unsigned int total_len;
1701 struct TCP_Server_Info *server;
1703 /* always use master channel */
1704 server = ses->server;
1706 cifs_dbg(FYI, "TCON\n");
1708 if (!server || !tree)
1711 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1712 if (unc_path == NULL)
1715 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1717 if (unc_path_len < 2) {
1722 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1724 atomic_set(&tcon->num_remote_opens, 0);
1725 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
1726 (void **) &req, &total_len);
1732 if (smb3_encryption_required(tcon))
1733 flags |= CIFS_TRANSFORM_REQ;
1735 iov[0].iov_base = (char *)req;
1737 iov[0].iov_len = total_len - 1;
1739 /* Testing shows that buffer offset must be at location of Buffer[0] */
1740 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1742 req->PathLength = cpu_to_le16(unc_path_len - 2);
1743 iov[1].iov_base = unc_path;
1744 iov[1].iov_len = unc_path_len;
1747 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1748 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1749 * (Samba servers don't always set the flag so also check if null user)
1751 if ((server->dialect == SMB311_PROT_ID) &&
1752 !smb3_encryption_required(tcon) &&
1753 !(ses->session_flags &
1754 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1755 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1756 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1758 memset(&rqst, 0, sizeof(struct smb_rqst));
1762 /* Need 64 for max size write so ask for more in case not there yet */
1763 req->sync_hdr.CreditRequest = cpu_to_le16(64);
1765 rc = cifs_send_recv(xid, ses, server,
1766 &rqst, &resp_buftype, flags, &rsp_iov);
1767 cifs_small_buf_release(req);
1768 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1769 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1772 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1773 tcon->need_reconnect = true;
1775 goto tcon_error_exit;
1778 switch (rsp->ShareType) {
1779 case SMB2_SHARE_TYPE_DISK:
1780 cifs_dbg(FYI, "connection to disk share\n");
1782 case SMB2_SHARE_TYPE_PIPE:
1784 cifs_dbg(FYI, "connection to pipe share\n");
1786 case SMB2_SHARE_TYPE_PRINT:
1788 cifs_dbg(FYI, "connection to printer\n");
1791 cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1793 goto tcon_error_exit;
1796 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1797 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1798 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1799 tcon->tidStatus = CifsGood;
1800 tcon->need_reconnect = false;
1801 tcon->tid = rsp->sync_hdr.TreeId;
1802 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1804 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1805 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1806 cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
1809 !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1810 cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
1812 init_copy_chunk_defaults(tcon);
1813 if (server->ops->validate_negotiate)
1814 rc = server->ops->validate_negotiate(xid, tcon);
1817 free_rsp_buf(resp_buftype, rsp);
1822 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1823 cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1829 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1831 struct smb_rqst rqst;
1832 struct smb2_tree_disconnect_req *req; /* response is trivial */
1834 struct cifs_ses *ses = tcon->ses;
1836 unsigned int total_len;
1838 struct kvec rsp_iov;
1841 cifs_dbg(FYI, "Tree Disconnect\n");
1843 if (!ses || !(ses->server))
1846 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1849 close_shroot_lease(&tcon->crfid);
1851 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, ses->server,
1857 if (smb3_encryption_required(tcon))
1858 flags |= CIFS_TRANSFORM_REQ;
1860 flags |= CIFS_NO_RSP_BUF;
1862 iov[0].iov_base = (char *)req;
1863 iov[0].iov_len = total_len;
1865 memset(&rqst, 0, sizeof(struct smb_rqst));
1869 rc = cifs_send_recv(xid, ses, ses->server,
1870 &rqst, &resp_buf_type, flags, &rsp_iov);
1871 cifs_small_buf_release(req);
1873 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1879 static struct create_durable *
1880 create_durable_buf(void)
1882 struct create_durable *buf;
1884 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1888 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1889 (struct create_durable, Data));
1890 buf->ccontext.DataLength = cpu_to_le32(16);
1891 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1892 (struct create_durable, Name));
1893 buf->ccontext.NameLength = cpu_to_le16(4);
1894 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1902 static struct create_durable *
1903 create_reconnect_durable_buf(struct cifs_fid *fid)
1905 struct create_durable *buf;
1907 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1911 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1912 (struct create_durable, Data));
1913 buf->ccontext.DataLength = cpu_to_le32(16);
1914 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1915 (struct create_durable, Name));
1916 buf->ccontext.NameLength = cpu_to_le16(4);
1917 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1918 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1919 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1928 parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
1930 struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
1932 cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
1933 pdisk_id->DiskFileId, pdisk_id->VolumeId);
1934 buf->IndexNumber = pdisk_id->DiskFileId;
1938 parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
1939 struct create_posix_rsp *posix)
1942 u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
1943 u8 *end = beg + le32_to_cpu(cc->DataLength);
1946 memset(posix, 0, sizeof(*posix));
1948 posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
1949 posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
1950 posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
1953 sid_len = posix_info_sid_size(sid, end);
1955 cifs_dbg(VFS, "bad owner sid in posix create response\n");
1958 memcpy(&posix->owner, sid, sid_len);
1960 sid = sid + sid_len;
1961 sid_len = posix_info_sid_size(sid, end);
1963 cifs_dbg(VFS, "bad group sid in posix create response\n");
1966 memcpy(&posix->group, sid, sid_len);
1968 cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
1969 posix->nlink, posix->mode, posix->reparse_tag);
1973 smb2_parse_contexts(struct TCP_Server_Info *server,
1974 struct smb2_create_rsp *rsp,
1975 unsigned int *epoch, char *lease_key, __u8 *oplock,
1976 struct smb2_file_all_info *buf,
1977 struct create_posix_rsp *posix)
1980 struct create_context *cc;
1982 unsigned int remaining;
1984 static const char smb3_create_tag_posix[] = {
1985 0x93, 0xAD, 0x25, 0x50, 0x9C,
1986 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
1987 0xDE, 0x96, 0x8B, 0xCD, 0x7C
1991 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
1992 remaining = le32_to_cpu(rsp->CreateContextsLength);
1993 cc = (struct create_context *)data_offset;
1995 /* Initialize inode number to 0 in case no valid data in qfid context */
1997 buf->IndexNumber = 0;
1999 while (remaining >= sizeof(struct create_context)) {
2000 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
2001 if (le16_to_cpu(cc->NameLength) == 4 &&
2002 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
2003 *oplock = server->ops->parse_lease_buf(cc, epoch,
2005 else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
2006 strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
2007 parse_query_id_ctxt(cc, buf);
2008 else if ((le16_to_cpu(cc->NameLength) == 16)) {
2010 memcmp(name, smb3_create_tag_posix, 16) == 0)
2011 parse_posix_ctxt(cc, buf, posix);
2014 cifs_dbg(FYI, "Context not matched with len %d\n",
2015 le16_to_cpu(cc->NameLength));
2016 cifs_dump_mem("Cctxt name: ", name, 4);
2019 next = le32_to_cpu(cc->Next);
2023 cc = (struct create_context *)((char *)cc + next);
2026 if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
2027 *oplock = rsp->OplockLevel;
2033 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
2034 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
2036 struct smb2_create_req *req = iov[0].iov_base;
2037 unsigned int num = *num_iovec;
2039 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
2040 if (iov[num].iov_base == NULL)
2042 iov[num].iov_len = server->vals->create_lease_size;
2043 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2044 if (!req->CreateContextsOffset)
2045 req->CreateContextsOffset = cpu_to_le32(
2046 sizeof(struct smb2_create_req) +
2047 iov[num - 1].iov_len);
2048 le32_add_cpu(&req->CreateContextsLength,
2049 server->vals->create_lease_size);
2050 *num_iovec = num + 1;
2054 static struct create_durable_v2 *
2055 create_durable_v2_buf(struct cifs_open_parms *oparms)
2057 struct cifs_fid *pfid = oparms->fid;
2058 struct create_durable_v2 *buf;
2060 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
2064 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2065 (struct create_durable_v2, dcontext));
2066 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
2067 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2068 (struct create_durable_v2, Name));
2069 buf->ccontext.NameLength = cpu_to_le16(4);
2072 * NB: Handle timeout defaults to 0, which allows server to choose
2073 * (most servers default to 120 seconds) and most clients default to 0.
2074 * This can be overridden at mount ("handletimeout=") if the user wants
2075 * a different persistent (or resilient) handle timeout for all opens
2076 * opens on a particular SMB3 mount.
2078 buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
2079 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2080 generate_random_uuid(buf->dcontext.CreateGuid);
2081 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
2083 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2091 static struct create_durable_handle_reconnect_v2 *
2092 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
2094 struct create_durable_handle_reconnect_v2 *buf;
2096 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
2101 buf->ccontext.DataOffset =
2102 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2104 buf->ccontext.DataLength =
2105 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
2106 buf->ccontext.NameOffset =
2107 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
2109 buf->ccontext.NameLength = cpu_to_le16(4);
2111 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
2112 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
2113 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
2114 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
2116 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2125 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
2126 struct cifs_open_parms *oparms)
2128 struct smb2_create_req *req = iov[0].iov_base;
2129 unsigned int num = *num_iovec;
2131 iov[num].iov_base = create_durable_v2_buf(oparms);
2132 if (iov[num].iov_base == NULL)
2134 iov[num].iov_len = sizeof(struct create_durable_v2);
2135 if (!req->CreateContextsOffset)
2136 req->CreateContextsOffset =
2137 cpu_to_le32(sizeof(struct smb2_create_req) +
2139 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
2140 *num_iovec = num + 1;
2145 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
2146 struct cifs_open_parms *oparms)
2148 struct smb2_create_req *req = iov[0].iov_base;
2149 unsigned int num = *num_iovec;
2151 /* indicate that we don't need to relock the file */
2152 oparms->reconnect = false;
2154 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
2155 if (iov[num].iov_base == NULL)
2157 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
2158 if (!req->CreateContextsOffset)
2159 req->CreateContextsOffset =
2160 cpu_to_le32(sizeof(struct smb2_create_req) +
2162 le32_add_cpu(&req->CreateContextsLength,
2163 sizeof(struct create_durable_handle_reconnect_v2));
2164 *num_iovec = num + 1;
2169 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
2170 struct cifs_open_parms *oparms, bool use_persistent)
2172 struct smb2_create_req *req = iov[0].iov_base;
2173 unsigned int num = *num_iovec;
2175 if (use_persistent) {
2176 if (oparms->reconnect)
2177 return add_durable_reconnect_v2_context(iov, num_iovec,
2180 return add_durable_v2_context(iov, num_iovec, oparms);
2183 if (oparms->reconnect) {
2184 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
2185 /* indicate that we don't need to relock the file */
2186 oparms->reconnect = false;
2188 iov[num].iov_base = create_durable_buf();
2189 if (iov[num].iov_base == NULL)
2191 iov[num].iov_len = sizeof(struct create_durable);
2192 if (!req->CreateContextsOffset)
2193 req->CreateContextsOffset =
2194 cpu_to_le32(sizeof(struct smb2_create_req) +
2196 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
2197 *num_iovec = num + 1;
2201 /* See MS-SMB2 2.2.13.2.7 */
2202 static struct crt_twarp_ctxt *
2203 create_twarp_buf(__u64 timewarp)
2205 struct crt_twarp_ctxt *buf;
2207 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
2211 buf->ccontext.DataOffset = cpu_to_le16(offsetof
2212 (struct crt_twarp_ctxt, Timestamp));
2213 buf->ccontext.DataLength = cpu_to_le32(8);
2214 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2215 (struct crt_twarp_ctxt, Name));
2216 buf->ccontext.NameLength = cpu_to_le16(4);
2217 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2222 buf->Timestamp = cpu_to_le64(timewarp);
2226 /* See MS-SMB2 2.2.13.2.7 */
2228 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
2230 struct smb2_create_req *req = iov[0].iov_base;
2231 unsigned int num = *num_iovec;
2233 iov[num].iov_base = create_twarp_buf(timewarp);
2234 if (iov[num].iov_base == NULL)
2236 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2237 if (!req->CreateContextsOffset)
2238 req->CreateContextsOffset = cpu_to_le32(
2239 sizeof(struct smb2_create_req) +
2240 iov[num - 1].iov_len);
2241 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2242 *num_iovec = num + 1;
2246 /* See See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
2247 static void setup_owner_group_sids(char *buf)
2249 struct owner_group_sids *sids = (struct owner_group_sids *)buf;
2251 /* Populate the user ownership fields S-1-5-88-1 */
2252 sids->owner.Revision = 1;
2253 sids->owner.NumAuth = 3;
2254 sids->owner.Authority[5] = 5;
2255 sids->owner.SubAuthorities[0] = cpu_to_le32(88);
2256 sids->owner.SubAuthorities[1] = cpu_to_le32(1);
2257 sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val);
2259 /* Populate the group ownership fields S-1-5-88-2 */
2260 sids->group.Revision = 1;
2261 sids->group.NumAuth = 3;
2262 sids->group.Authority[5] = 5;
2263 sids->group.SubAuthorities[0] = cpu_to_le32(88);
2264 sids->group.SubAuthorities[1] = cpu_to_le32(2);
2265 sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val);
2267 cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val);
2270 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2271 static struct crt_sd_ctxt *
2272 create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
2274 struct crt_sd_ctxt *buf;
2276 unsigned int acelen, acl_size, ace_count;
2277 unsigned int owner_offset = 0;
2278 unsigned int group_offset = 0;
2279 struct smb3_acl acl;
2281 *len = roundup(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 4), 8);
2284 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
2285 *len += sizeof(struct owner_group_sids);
2288 buf = kzalloc(*len, GFP_KERNEL);
2292 ptr = (__u8 *)&buf[1];
2294 /* offset fields are from beginning of security descriptor not of create context */
2295 owner_offset = ptr - (__u8 *)&buf->sd;
2296 buf->sd.OffsetOwner = cpu_to_le32(owner_offset);
2297 group_offset = owner_offset + offsetof(struct owner_group_sids, group);
2298 buf->sd.OffsetGroup = cpu_to_le32(group_offset);
2300 setup_owner_group_sids(ptr);
2301 ptr += sizeof(struct owner_group_sids);
2303 buf->sd.OffsetOwner = 0;
2304 buf->sd.OffsetGroup = 0;
2307 buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, sd));
2308 buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name));
2309 buf->ccontext.NameLength = cpu_to_le16(4);
2310 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2315 buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
2318 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2319 * and "DP" ie the DACL is present
2321 buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
2323 /* offset owner, group and Sbz1 and SACL are all zero */
2324 buf->sd.OffsetDacl = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2325 /* Ship the ACL for now. we will copy it into buf later. */
2327 ptr += sizeof(struct cifs_acl);
2329 /* create one ACE to hold the mode embedded in reserved special SID */
2330 acelen = setup_special_mode_ACE((struct cifs_ace *)ptr, (__u64)mode);
2332 acl_size = acelen + sizeof(struct smb3_acl);
2336 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
2337 acelen = setup_special_user_owner_ACE((struct cifs_ace *)ptr);
2343 /* and one more ACE to allow access for authenticated users */
2344 acelen = setup_authusers_ACE((struct cifs_ace *)ptr);
2349 acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
2350 acl.AclSize = cpu_to_le16(acl_size);
2351 acl.AceCount = cpu_to_le16(ace_count);
2352 memcpy(aclptr, &acl, sizeof(struct cifs_acl));
2354 buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd);
2355 *len = ptr - (__u8 *)buf;
2361 add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner)
2363 struct smb2_create_req *req = iov[0].iov_base;
2364 unsigned int num = *num_iovec;
2365 unsigned int len = 0;
2367 iov[num].iov_base = create_sd_buf(mode, set_owner, &len);
2368 if (iov[num].iov_base == NULL)
2370 iov[num].iov_len = len;
2371 if (!req->CreateContextsOffset)
2372 req->CreateContextsOffset = cpu_to_le32(
2373 sizeof(struct smb2_create_req) +
2374 iov[num - 1].iov_len);
2375 le32_add_cpu(&req->CreateContextsLength, len);
2376 *num_iovec = num + 1;
2380 static struct crt_query_id_ctxt *
2381 create_query_id_buf(void)
2383 struct crt_query_id_ctxt *buf;
2385 buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
2389 buf->ccontext.DataOffset = cpu_to_le16(0);
2390 buf->ccontext.DataLength = cpu_to_le32(0);
2391 buf->ccontext.NameOffset = cpu_to_le16(offsetof
2392 (struct crt_query_id_ctxt, Name));
2393 buf->ccontext.NameLength = cpu_to_le16(4);
2394 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2402 /* See MS-SMB2 2.2.13.2.9 */
2404 add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
2406 struct smb2_create_req *req = iov[0].iov_base;
2407 unsigned int num = *num_iovec;
2409 iov[num].iov_base = create_query_id_buf();
2410 if (iov[num].iov_base == NULL)
2412 iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
2413 if (!req->CreateContextsOffset)
2414 req->CreateContextsOffset = cpu_to_le32(
2415 sizeof(struct smb2_create_req) +
2416 iov[num - 1].iov_len);
2417 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
2418 *num_iovec = num + 1;
2423 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2424 const char *treename, const __le16 *path)
2426 int treename_len, path_len;
2427 struct nls_table *cp;
2428 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2433 treename_len = strlen(treename);
2434 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2440 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2443 * make room for one path separator between the treename and
2446 *out_len = treename_len + 1 + path_len;
2449 * final path needs to be null-terminated UTF16 with a
2453 *out_size = roundup((*out_len+1)*2, 8);
2454 *out_path = kzalloc(*out_size, GFP_KERNEL);
2458 cp = load_nls_default();
2459 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2460 UniStrcat(*out_path, sep);
2461 UniStrcat(*out_path, path);
2467 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2468 umode_t mode, struct cifs_tcon *tcon,
2469 const char *full_path,
2470 struct cifs_sb_info *cifs_sb)
2472 struct smb_rqst rqst;
2473 struct smb2_create_req *req;
2474 struct smb2_create_rsp *rsp = NULL;
2475 struct cifs_ses *ses = tcon->ses;
2476 struct kvec iov[3]; /* make sure at least one for each open context */
2477 struct kvec rsp_iov = {NULL, 0};
2480 __le16 *copy_path = NULL;
2483 unsigned int n_iov = 2;
2484 __u32 file_attributes = 0;
2485 char *pc_buf = NULL;
2487 unsigned int total_len;
2488 __le16 *utf16_path = NULL;
2489 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2491 cifs_dbg(FYI, "mkdir\n");
2493 /* resource #1: path allocation */
2494 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2498 if (!ses || !server) {
2503 /* resource #2: request */
2504 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2505 (void **) &req, &total_len);
2510 if (smb3_encryption_required(tcon))
2511 flags |= CIFS_TRANSFORM_REQ;
2513 req->ImpersonationLevel = IL_IMPERSONATION;
2514 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2515 /* File attributes ignored on open (used in create though) */
2516 req->FileAttributes = cpu_to_le32(file_attributes);
2517 req->ShareAccess = FILE_SHARE_ALL_LE;
2518 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2519 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2521 iov[0].iov_base = (char *)req;
2522 /* -1 since last byte is buf[0] which is sent below (path) */
2523 iov[0].iov_len = total_len - 1;
2525 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2527 /* [MS-SMB2] 2.2.13 NameOffset:
2528 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2529 * the SMB2 header, the file name includes a prefix that will
2530 * be processed during DFS name normalization as specified in
2531 * section 3.3.5.9. Otherwise, the file name is relative to
2532 * the share that is identified by the TreeId in the SMB2
2535 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2538 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2539 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2541 tcon->treeName, utf16_path);
2545 req->NameLength = cpu_to_le16(name_len * 2);
2546 uni_path_len = copy_size;
2547 /* free before overwriting resource */
2549 utf16_path = copy_path;
2551 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2552 /* MUST set path len (NameLength) to 0 opening root of share */
2553 req->NameLength = cpu_to_le16(uni_path_len - 2);
2554 if (uni_path_len % 8 != 0) {
2555 copy_size = roundup(uni_path_len, 8);
2556 copy_path = kzalloc(copy_size, GFP_KERNEL);
2561 memcpy((char *)copy_path, (const char *)utf16_path,
2563 uni_path_len = copy_size;
2564 /* free before overwriting resource */
2566 utf16_path = copy_path;
2570 iov[1].iov_len = uni_path_len;
2571 iov[1].iov_base = utf16_path;
2572 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2574 if (tcon->posix_extensions) {
2575 /* resource #3: posix buf */
2576 rc = add_posix_context(iov, &n_iov, mode);
2579 pc_buf = iov[n_iov-1].iov_base;
2583 memset(&rqst, 0, sizeof(struct smb_rqst));
2585 rqst.rq_nvec = n_iov;
2587 /* no need to inc num_remote_opens because we close it just below */
2588 trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, CREATE_NOT_FILE,
2589 FILE_WRITE_ATTRIBUTES);
2590 /* resource #4: response buffer */
2591 rc = cifs_send_recv(xid, ses, server,
2592 &rqst, &resp_buftype, flags, &rsp_iov);
2594 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2595 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2597 FILE_WRITE_ATTRIBUTES, rc);
2598 goto err_free_rsp_buf;
2601 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2602 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2603 ses->Suid, CREATE_NOT_FILE,
2604 FILE_WRITE_ATTRIBUTES);
2606 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2608 /* Eventually save off posix specific response info and timestaps */
2611 free_rsp_buf(resp_buftype, rsp);
2614 cifs_small_buf_release(req);
2621 SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2622 struct smb_rqst *rqst, __u8 *oplock,
2623 struct cifs_open_parms *oparms, __le16 *path)
2625 struct smb2_create_req *req;
2626 unsigned int n_iov = 2;
2627 __u32 file_attributes = 0;
2630 unsigned int total_len;
2631 struct kvec *iov = rqst->rq_iov;
2635 rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
2636 (void **) &req, &total_len);
2640 iov[0].iov_base = (char *)req;
2641 /* -1 since last byte is buf[0] which is sent below (path) */
2642 iov[0].iov_len = total_len - 1;
2644 if (oparms->create_options & CREATE_OPTION_READONLY)
2645 file_attributes |= ATTR_READONLY;
2646 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2647 file_attributes |= ATTR_SYSTEM;
2649 req->ImpersonationLevel = IL_IMPERSONATION;
2650 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2651 /* File attributes ignored on open (used in create though) */
2652 req->FileAttributes = cpu_to_le32(file_attributes);
2653 req->ShareAccess = FILE_SHARE_ALL_LE;
2655 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2656 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2657 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2659 /* [MS-SMB2] 2.2.13 NameOffset:
2660 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2661 * the SMB2 header, the file name includes a prefix that will
2662 * be processed during DFS name normalization as specified in
2663 * section 3.3.5.9. Otherwise, the file name is relative to
2664 * the share that is identified by the TreeId in the SMB2
2667 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2670 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2671 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2673 tcon->treeName, path);
2676 req->NameLength = cpu_to_le16(name_len * 2);
2677 uni_path_len = copy_size;
2680 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2681 /* MUST set path len (NameLength) to 0 opening root of share */
2682 req->NameLength = cpu_to_le16(uni_path_len - 2);
2683 copy_size = uni_path_len;
2684 if (copy_size % 8 != 0)
2685 copy_size = roundup(copy_size, 8);
2686 copy_path = kzalloc(copy_size, GFP_KERNEL);
2689 memcpy((char *)copy_path, (const char *)path,
2691 uni_path_len = copy_size;
2695 iov[1].iov_len = uni_path_len;
2696 iov[1].iov_base = path;
2698 if ((!server->oplocks) || (tcon->no_lease))
2699 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2701 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2702 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2703 req->RequestedOplockLevel = *oplock;
2704 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2705 (oparms->create_options & CREATE_NOT_FILE))
2706 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2708 rc = add_lease_context(server, iov, &n_iov,
2709 oparms->fid->lease_key, oplock);
2714 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2715 /* need to set Next field of lease context if we request it */
2716 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2717 struct create_context *ccontext =
2718 (struct create_context *)iov[n_iov-1].iov_base;
2720 cpu_to_le32(server->vals->create_lease_size);
2723 rc = add_durable_context(iov, &n_iov, oparms,
2724 tcon->use_persistent);
2729 if (tcon->posix_extensions) {
2731 struct create_context *ccontext =
2732 (struct create_context *)iov[n_iov-1].iov_base;
2734 cpu_to_le32(iov[n_iov-1].iov_len);
2737 rc = add_posix_context(iov, &n_iov, oparms->mode);
2742 if (tcon->snapshot_time) {
2743 cifs_dbg(FYI, "adding snapshot context\n");
2745 struct create_context *ccontext =
2746 (struct create_context *)iov[n_iov-1].iov_base;
2748 cpu_to_le32(iov[n_iov-1].iov_len);
2751 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2756 if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) {
2760 if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
2761 (oparms->mode != ACL_NO_MODE))
2765 oparms->mode = ACL_NO_MODE;
2768 if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
2773 if (set_owner | set_mode) {
2775 struct create_context *ccontext =
2776 (struct create_context *)iov[n_iov-1].iov_base;
2777 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2780 cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
2781 rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner);
2788 struct create_context *ccontext =
2789 (struct create_context *)iov[n_iov-1].iov_base;
2790 ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
2792 add_query_id_context(iov, &n_iov);
2794 rqst->rq_nvec = n_iov;
2798 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2799 * All other vectors are freed by kfree().
2802 SMB2_open_free(struct smb_rqst *rqst)
2806 if (rqst && rqst->rq_iov) {
2807 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2808 for (i = 1; i < rqst->rq_nvec; i++)
2809 if (rqst->rq_iov[i].iov_base != smb2_padding)
2810 kfree(rqst->rq_iov[i].iov_base);
2815 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2816 __u8 *oplock, struct smb2_file_all_info *buf,
2817 struct create_posix_rsp *posix,
2818 struct kvec *err_iov, int *buftype)
2820 struct smb_rqst rqst;
2821 struct smb2_create_rsp *rsp = NULL;
2822 struct cifs_tcon *tcon = oparms->tcon;
2823 struct cifs_ses *ses = tcon->ses;
2824 struct TCP_Server_Info *server = cifs_pick_channel(ses);
2825 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2826 struct kvec rsp_iov = {NULL, 0};
2827 int resp_buftype = CIFS_NO_BUFFER;
2831 cifs_dbg(FYI, "create/open\n");
2832 if (!ses || !server)
2835 if (smb3_encryption_required(tcon))
2836 flags |= CIFS_TRANSFORM_REQ;
2838 memset(&rqst, 0, sizeof(struct smb_rqst));
2839 memset(&iov, 0, sizeof(iov));
2841 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2843 rc = SMB2_open_init(tcon, server,
2844 &rqst, oplock, oparms, path);
2848 trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid,
2849 oparms->create_options, oparms->desired_access);
2851 rc = cifs_send_recv(xid, ses, server,
2852 &rqst, &resp_buftype, flags,
2854 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2857 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2858 if (err_iov && rsp) {
2860 *buftype = resp_buftype;
2861 resp_buftype = CIFS_NO_BUFFER;
2864 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2865 oparms->create_options, oparms->desired_access, rc);
2866 if (rc == -EREMCHG) {
2867 pr_warn_once("server share %s deleted\n",
2869 tcon->need_reconnect = true;
2873 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2874 ses->Suid, oparms->create_options,
2875 oparms->desired_access);
2877 atomic_inc(&tcon->num_remote_opens);
2878 oparms->fid->persistent_fid = rsp->PersistentFileId;
2879 oparms->fid->volatile_fid = rsp->VolatileFileId;
2880 oparms->fid->access = oparms->desired_access;
2881 #ifdef CONFIG_CIFS_DEBUG2
2882 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2883 #endif /* CIFS_DEBUG2 */
2886 memcpy(buf, &rsp->CreationTime, 32);
2887 buf->AllocationSize = rsp->AllocationSize;
2888 buf->EndOfFile = rsp->EndofFile;
2889 buf->Attributes = rsp->FileAttributes;
2890 buf->NumberOfLinks = cpu_to_le32(1);
2891 buf->DeletePending = 0;
2895 smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2896 oparms->fid->lease_key, oplock, buf, posix);
2898 SMB2_open_free(&rqst);
2899 free_rsp_buf(resp_buftype, rsp);
2904 SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
2905 struct smb_rqst *rqst,
2906 u64 persistent_fid, u64 volatile_fid, u32 opcode,
2907 bool is_fsctl, char *in_data, u32 indatalen,
2908 __u32 max_response_size)
2910 struct smb2_ioctl_req *req;
2911 struct kvec *iov = rqst->rq_iov;
2912 unsigned int total_len;
2916 rc = smb2_ioctl_req_init(opcode, tcon, server,
2917 (void **) &req, &total_len);
2923 * indatalen is usually small at a couple of bytes max, so
2924 * just allocate through generic pool
2926 in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
2928 cifs_small_buf_release(req);
2933 req->CtlCode = cpu_to_le32(opcode);
2934 req->PersistentFileId = persistent_fid;
2935 req->VolatileFileId = volatile_fid;
2937 iov[0].iov_base = (char *)req;
2939 * If no input data, the size of ioctl struct in
2940 * protocol spec still includes a 1 byte data buffer,
2941 * but if input data passed to ioctl, we do not
2942 * want to double count this, so we do not send
2943 * the dummy one byte of data in iovec[0] if sending
2944 * input data (in iovec[1]).
2947 req->InputCount = cpu_to_le32(indatalen);
2948 /* do not set InputOffset if no input data */
2950 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
2952 iov[0].iov_len = total_len - 1;
2953 iov[1].iov_base = in_data_buf;
2954 iov[1].iov_len = indatalen;
2957 iov[0].iov_len = total_len;
2960 req->OutputOffset = 0;
2961 req->OutputCount = 0; /* MBZ */
2964 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
2965 * We Could increase default MaxOutputResponse, but that could require
2966 * more credits. Windows typically sets this smaller, but for some
2967 * ioctls it may be useful to allow server to send more. No point
2968 * limiting what the server can send as long as fits in one credit
2969 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
2970 * to increase this limit up in the future.
2971 * Note that for snapshot queries that servers like Azure expect that
2972 * the first query be minimal size (and just used to get the number/size
2973 * of previous versions) so response size must be specified as EXACTLY
2974 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2975 * of eight bytes. Currently that is the only case where we set max
2976 * response size smaller.
2978 req->MaxOutputResponse = cpu_to_le32(max_response_size);
2979 req->sync_hdr.CreditCharge =
2980 cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
2981 SMB2_MAX_BUFFER_SIZE));
2983 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2987 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2988 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
2989 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
2995 SMB2_ioctl_free(struct smb_rqst *rqst)
2998 if (rqst && rqst->rq_iov) {
2999 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3000 for (i = 1; i < rqst->rq_nvec; i++)
3001 if (rqst->rq_iov[i].iov_base != smb2_padding)
3002 kfree(rqst->rq_iov[i].iov_base);
3008 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
3011 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3012 u64 volatile_fid, u32 opcode, bool is_fsctl,
3013 char *in_data, u32 indatalen, u32 max_out_data_len,
3014 char **out_data, u32 *plen /* returned data len */)
3016 struct smb_rqst rqst;
3017 struct smb2_ioctl_rsp *rsp = NULL;
3018 struct cifs_ses *ses;
3019 struct TCP_Server_Info *server;
3020 struct kvec iov[SMB2_IOCTL_IOV_SIZE];
3021 struct kvec rsp_iov = {NULL, 0};
3022 int resp_buftype = CIFS_NO_BUFFER;
3026 cifs_dbg(FYI, "SMB2 IOCTL\n");
3028 if (out_data != NULL)
3031 /* zero out returned data len, in case of error */
3042 server = cifs_pick_channel(ses);
3046 if (smb3_encryption_required(tcon))
3047 flags |= CIFS_TRANSFORM_REQ;
3049 memset(&rqst, 0, sizeof(struct smb_rqst));
3050 memset(&iov, 0, sizeof(iov));
3052 rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
3054 rc = SMB2_ioctl_init(tcon, server,
3055 &rqst, persistent_fid, volatile_fid, opcode,
3056 is_fsctl, in_data, indatalen, max_out_data_len);
3060 rc = cifs_send_recv(xid, ses, server,
3061 &rqst, &resp_buftype, flags,
3063 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
3066 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
3067 ses->Suid, 0, opcode, rc);
3069 if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
3070 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3072 } else if (rc == -EINVAL) {
3073 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
3074 (opcode != FSCTL_SRV_COPYCHUNK)) {
3075 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3078 } else if (rc == -E2BIG) {
3079 if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
3080 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
3085 /* check if caller wants to look at return data or just return rc */
3086 if ((plen == NULL) || (out_data == NULL))
3089 *plen = le32_to_cpu(rsp->OutputCount);
3091 /* We check for obvious errors in the output buffer length and offset */
3093 goto ioctl_exit; /* server returned no data */
3094 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
3095 cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
3101 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
3102 cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
3103 le32_to_cpu(rsp->OutputOffset));
3109 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
3111 if (*out_data == NULL) {
3117 SMB2_ioctl_free(&rqst);
3118 free_rsp_buf(resp_buftype, rsp);
3123 * Individual callers to ioctl worker function follow
3127 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3128 u64 persistent_fid, u64 volatile_fid)
3131 struct compress_ioctl fsctl_input;
3132 char *ret_data = NULL;
3134 fsctl_input.CompressionState =
3135 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3137 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
3138 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
3139 (char *)&fsctl_input /* data input */,
3140 2 /* in data len */, CIFSMaxBufSize /* max out data */,
3141 &ret_data /* out data */, NULL);
3143 cifs_dbg(FYI, "set compression rc %d\n", rc);
3149 SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3150 struct smb_rqst *rqst,
3151 u64 persistent_fid, u64 volatile_fid, bool query_attrs)
3153 struct smb2_close_req *req;
3154 struct kvec *iov = rqst->rq_iov;
3155 unsigned int total_len;
3158 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
3159 (void **) &req, &total_len);
3163 req->PersistentFileId = persistent_fid;
3164 req->VolatileFileId = volatile_fid;
3166 req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
3169 iov[0].iov_base = (char *)req;
3170 iov[0].iov_len = total_len;
3176 SMB2_close_free(struct smb_rqst *rqst)
3178 if (rqst && rqst->rq_iov)
3179 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3183 __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3184 u64 persistent_fid, u64 volatile_fid,
3185 struct smb2_file_network_open_info *pbuf)
3187 struct smb_rqst rqst;
3188 struct smb2_close_rsp *rsp = NULL;
3189 struct cifs_ses *ses = tcon->ses;
3190 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3192 struct kvec rsp_iov;
3193 int resp_buftype = CIFS_NO_BUFFER;
3196 bool query_attrs = false;
3198 cifs_dbg(FYI, "Close\n");
3200 if (!ses || !server)
3203 if (smb3_encryption_required(tcon))
3204 flags |= CIFS_TRANSFORM_REQ;
3206 memset(&rqst, 0, sizeof(struct smb_rqst));
3207 memset(&iov, 0, sizeof(iov));
3211 /* check if need to ask server to return timestamps in close response */
3215 trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3216 rc = SMB2_close_init(tcon, server,
3217 &rqst, persistent_fid, volatile_fid,
3222 rc = cifs_send_recv(xid, ses, server,
3223 &rqst, &resp_buftype, flags, &rsp_iov);
3224 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
3227 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
3228 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
3232 trace_smb3_close_done(xid, persistent_fid, tcon->tid,
3235 * Note that have to subtract 4 since struct network_open_info
3236 * has a final 4 byte pad that close response does not have
3239 memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
3242 atomic_dec(&tcon->num_remote_opens);
3244 SMB2_close_free(&rqst);
3245 free_rsp_buf(resp_buftype, rsp);
3247 /* retry close in a worker thread if this one is interrupted */
3251 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
3254 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
3255 persistent_fid, tmp_rc);
3261 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
3262 u64 persistent_fid, u64 volatile_fid)
3264 return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
3268 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
3269 struct kvec *iov, unsigned int min_buf_size)
3271 unsigned int smb_len = iov->iov_len;
3272 char *end_of_smb = smb_len + (char *)iov->iov_base;
3273 char *begin_of_buf = offset + (char *)iov->iov_base;
3274 char *end_of_buf = begin_of_buf + buffer_length;
3277 if (buffer_length < min_buf_size) {
3278 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
3279 buffer_length, min_buf_size);
3283 /* check if beyond RFC1001 maximum length */
3284 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
3285 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
3286 buffer_length, smb_len);
3290 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
3291 cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
3299 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3300 * Caller must free buffer.
3303 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
3304 struct kvec *iov, unsigned int minbufsize,
3307 char *begin_of_buf = offset + (char *)iov->iov_base;
3313 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
3317 memcpy(data, begin_of_buf, buffer_length);
3323 SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3324 struct smb_rqst *rqst,
3325 u64 persistent_fid, u64 volatile_fid,
3326 u8 info_class, u8 info_type, u32 additional_info,
3327 size_t output_len, size_t input_len, void *input)
3329 struct smb2_query_info_req *req;
3330 struct kvec *iov = rqst->rq_iov;
3331 unsigned int total_len;
3334 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
3335 (void **) &req, &total_len);
3339 req->InfoType = info_type;
3340 req->FileInfoClass = info_class;
3341 req->PersistentFileId = persistent_fid;
3342 req->VolatileFileId = volatile_fid;
3343 req->AdditionalInformation = cpu_to_le32(additional_info);
3345 req->OutputBufferLength = cpu_to_le32(output_len);
3347 req->InputBufferLength = cpu_to_le32(input_len);
3348 /* total_len for smb query request never close to le16 max */
3349 req->InputBufferOffset = cpu_to_le16(total_len - 1);
3350 memcpy(req->Buffer, input, input_len);
3353 iov[0].iov_base = (char *)req;
3355 iov[0].iov_len = total_len - 1 + input_len;
3360 SMB2_query_info_free(struct smb_rqst *rqst)
3362 if (rqst && rqst->rq_iov)
3363 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3367 query_info(const unsigned int xid, struct cifs_tcon *tcon,
3368 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
3369 u32 additional_info, size_t output_len, size_t min_len, void **data,
3372 struct smb_rqst rqst;
3373 struct smb2_query_info_rsp *rsp = NULL;
3375 struct kvec rsp_iov;
3377 int resp_buftype = CIFS_NO_BUFFER;
3378 struct cifs_ses *ses = tcon->ses;
3379 struct TCP_Server_Info *server;
3381 bool allocated = false;
3383 cifs_dbg(FYI, "Query Info\n");
3387 server = cifs_pick_channel(ses);
3391 if (smb3_encryption_required(tcon))
3392 flags |= CIFS_TRANSFORM_REQ;
3394 memset(&rqst, 0, sizeof(struct smb_rqst));
3395 memset(&iov, 0, sizeof(iov));
3399 rc = SMB2_query_info_init(tcon, server,
3400 &rqst, persistent_fid, volatile_fid,
3401 info_class, info_type, additional_info,
3402 output_len, 0, NULL);
3406 trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
3407 ses->Suid, info_class, (__u32)info_type);
3409 rc = cifs_send_recv(xid, ses, server,
3410 &rqst, &resp_buftype, flags, &rsp_iov);
3411 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3414 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3415 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
3416 ses->Suid, info_class, (__u32)info_type, rc);
3420 trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
3421 ses->Suid, info_class, (__u32)info_type);
3424 *dlen = le32_to_cpu(rsp->OutputBufferLength);
3426 *data = kmalloc(*dlen, GFP_KERNEL);
3429 "Error %d allocating memory for acl\n",
3439 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
3440 le32_to_cpu(rsp->OutputBufferLength),
3441 &rsp_iov, min_len, *data);
3442 if (rc && allocated) {
3449 SMB2_query_info_free(&rqst);
3450 free_rsp_buf(resp_buftype, rsp);
3454 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3455 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
3457 return query_info(xid, tcon, persistent_fid, volatile_fid,
3458 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
3459 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
3460 sizeof(struct smb2_file_all_info), (void **)&data,
3465 SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon,
3466 u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen)
3468 size_t output_len = sizeof(struct smb311_posix_qinfo *) +
3469 (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2);
3472 return query_info(xid, tcon, persistent_fid, volatile_fid,
3473 SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0,
3474 output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen);
3478 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
3479 u64 persistent_fid, u64 volatile_fid,
3480 void **data, u32 *plen)
3482 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
3485 return query_info(xid, tcon, persistent_fid, volatile_fid,
3486 0, SMB2_O_INFO_SECURITY, additional_info,
3487 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
3491 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
3492 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
3494 return query_info(xid, tcon, persistent_fid, volatile_fid,
3495 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
3496 sizeof(struct smb2_file_internal_info),
3497 sizeof(struct smb2_file_internal_info),
3498 (void **)&uniqueid, NULL);
3502 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3503 * See MS-SMB2 2.2.35 and 2.2.36
3507 SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
3508 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3509 u64 persistent_fid, u64 volatile_fid,
3510 u32 completion_filter, bool watch_tree)
3512 struct smb2_change_notify_req *req;
3513 struct kvec *iov = rqst->rq_iov;
3514 unsigned int total_len;
3517 rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
3518 (void **) &req, &total_len);
3522 req->PersistentFileId = persistent_fid;
3523 req->VolatileFileId = volatile_fid;
3524 /* See note 354 of MS-SMB2, 64K max */
3525 req->OutputBufferLength =
3526 cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
3527 req->CompletionFilter = cpu_to_le32(completion_filter);
3529 req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
3533 iov[0].iov_base = (char *)req;
3534 iov[0].iov_len = total_len;
3540 SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
3541 u64 persistent_fid, u64 volatile_fid, bool watch_tree,
3542 u32 completion_filter)
3544 struct cifs_ses *ses = tcon->ses;
3545 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3546 struct smb_rqst rqst;
3548 struct kvec rsp_iov = {NULL, 0};
3549 int resp_buftype = CIFS_NO_BUFFER;
3553 cifs_dbg(FYI, "change notify\n");
3554 if (!ses || !server)
3557 if (smb3_encryption_required(tcon))
3558 flags |= CIFS_TRANSFORM_REQ;
3560 memset(&rqst, 0, sizeof(struct smb_rqst));
3561 memset(&iov, 0, sizeof(iov));
3565 rc = SMB2_notify_init(xid, &rqst, tcon, server,
3566 persistent_fid, volatile_fid,
3567 completion_filter, watch_tree);
3571 trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
3572 (u8)watch_tree, completion_filter);
3573 rc = cifs_send_recv(xid, ses, server,
3574 &rqst, &resp_buftype, flags, &rsp_iov);
3577 cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
3578 trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
3579 (u8)watch_tree, completion_filter, rc);
3581 trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
3582 ses->Suid, (u8)watch_tree, completion_filter);
3586 cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
3587 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3594 * This is a no-op for now. We're not really interested in the reply, but
3595 * rather in the fact that the server sent one and that server->lstrp
3598 * FIXME: maybe we should consider checking that the reply matches request?
3601 smb2_echo_callback(struct mid_q_entry *mid)
3603 struct TCP_Server_Info *server = mid->callback_data;
3604 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
3605 struct cifs_credits credits = { .value = 0, .instance = 0 };
3607 if (mid->mid_state == MID_RESPONSE_RECEIVED
3608 || mid->mid_state == MID_RESPONSE_MALFORMED) {
3609 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3610 credits.instance = server->reconnect_instance;
3613 DeleteMidQEntry(mid);
3614 add_credits(server, &credits, CIFS_ECHO_OP);
3617 void smb2_reconnect_server(struct work_struct *work)
3619 struct TCP_Server_Info *server = container_of(work,
3620 struct TCP_Server_Info, reconnect.work);
3621 struct cifs_ses *ses;
3622 struct cifs_tcon *tcon, *tcon2;
3623 struct list_head tmp_list;
3624 int tcon_exist = false;
3626 int resched = false;
3629 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3630 mutex_lock(&server->reconnect_mutex);
3632 INIT_LIST_HEAD(&tmp_list);
3633 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
3635 spin_lock(&cifs_tcp_ses_lock);
3636 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3637 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
3638 if (tcon->need_reconnect || tcon->need_reopen_files) {
3640 list_add_tail(&tcon->rlist, &tmp_list);
3645 * IPC has the same lifetime as its session and uses its
3648 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
3649 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
3655 * Get the reference to server struct to be sure that the last call of
3656 * cifs_put_tcon() in the loop below won't release the server pointer.
3659 server->srv_count++;
3661 spin_unlock(&cifs_tcp_ses_lock);
3663 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
3664 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
3666 cifs_reopen_persistent_handles(tcon);
3669 list_del_init(&tcon->rlist);
3671 cifs_put_smb_ses(tcon->ses);
3673 cifs_put_tcon(tcon);
3676 cifs_dbg(FYI, "Reconnecting tcons finished\n");
3678 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
3679 mutex_unlock(&server->reconnect_mutex);
3681 /* now we can safely release srv struct */
3683 cifs_put_tcp_session(server, 1);
3687 SMB2_echo(struct TCP_Server_Info *server)
3689 struct smb2_echo_req *req;
3692 struct smb_rqst rqst = { .rq_iov = iov,
3694 unsigned int total_len;
3696 cifs_dbg(FYI, "In echo request\n");
3698 if (server->tcpStatus == CifsNeedNegotiate) {
3699 /* No need to send echo on newly established connections */
3700 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
3704 rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
3705 (void **)&req, &total_len);
3709 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3711 iov[0].iov_len = total_len;
3712 iov[0].iov_base = (char *)req;
3714 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3715 server, CIFS_ECHO_OP, NULL);
3717 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3719 cifs_small_buf_release(req);
3724 SMB2_flush_free(struct smb_rqst *rqst)
3726 if (rqst && rqst->rq_iov)
3727 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
3731 SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
3732 struct cifs_tcon *tcon, struct TCP_Server_Info *server,
3733 u64 persistent_fid, u64 volatile_fid)
3735 struct smb2_flush_req *req;
3736 struct kvec *iov = rqst->rq_iov;
3737 unsigned int total_len;
3740 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
3741 (void **) &req, &total_len);
3745 req->PersistentFileId = persistent_fid;
3746 req->VolatileFileId = volatile_fid;
3748 iov[0].iov_base = (char *)req;
3749 iov[0].iov_len = total_len;
3755 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3758 struct cifs_ses *ses = tcon->ses;
3759 struct smb_rqst rqst;
3761 struct kvec rsp_iov = {NULL, 0};
3762 struct TCP_Server_Info *server = cifs_pick_channel(ses);
3763 int resp_buftype = CIFS_NO_BUFFER;
3767 cifs_dbg(FYI, "flush\n");
3768 if (!ses || !(ses->server))
3771 if (smb3_encryption_required(tcon))
3772 flags |= CIFS_TRANSFORM_REQ;
3774 memset(&rqst, 0, sizeof(struct smb_rqst));
3775 memset(&iov, 0, sizeof(iov));
3779 rc = SMB2_flush_init(xid, &rqst, tcon, server,
3780 persistent_fid, volatile_fid);
3784 trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
3785 rc = cifs_send_recv(xid, ses, server,
3786 &rqst, &resp_buftype, flags, &rsp_iov);
3789 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
3790 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3793 trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
3797 SMB2_flush_free(&rqst);
3798 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3803 * To form a chain of read requests, any read requests after the first should
3804 * have the end_of_chain boolean set to true.
3807 smb2_new_read_req(void **buf, unsigned int *total_len,
3808 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3809 unsigned int remaining_bytes, int request_type)
3812 struct smb2_read_plain_req *req = NULL;
3813 struct smb2_sync_hdr *shdr;
3814 struct TCP_Server_Info *server = io_parms->server;
3816 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
3817 (void **) &req, total_len);
3822 return -ECONNABORTED;
3824 shdr = &req->sync_hdr;
3825 shdr->ProcessId = cpu_to_le32(io_parms->pid);
3827 req->PersistentFileId = io_parms->persistent_fid;
3828 req->VolatileFileId = io_parms->volatile_fid;
3829 req->ReadChannelInfoOffset = 0; /* reserved */
3830 req->ReadChannelInfoLength = 0; /* reserved */
3831 req->Channel = 0; /* reserved */
3832 req->MinimumCount = 0;
3833 req->Length = cpu_to_le32(io_parms->length);
3834 req->Offset = cpu_to_le64(io_parms->offset);
3836 trace_smb3_read_enter(0 /* xid */,
3837 io_parms->persistent_fid,
3838 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
3839 io_parms->offset, io_parms->length);
3840 #ifdef CONFIG_CIFS_SMB_DIRECT
3842 * If we want to do a RDMA write, fill in and append
3843 * smbd_buffer_descriptor_v1 to the end of read request
3845 if (server->rdma && rdata && !server->sign &&
3846 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
3848 struct smbd_buffer_descriptor_v1 *v1;
3849 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3851 rdata->mr = smbd_register_mr(
3852 server->smbd_conn, rdata->pages,
3853 rdata->nr_pages, rdata->page_offset,
3854 rdata->tailsz, true, need_invalidate);
3858 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3859 if (need_invalidate)
3860 req->Channel = SMB2_CHANNEL_RDMA_V1;
3861 req->ReadChannelInfoOffset =
3862 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
3863 req->ReadChannelInfoLength =
3864 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3865 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3866 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3867 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3868 v1->length = cpu_to_le32(rdata->mr->mr->length);
3870 *total_len += sizeof(*v1) - 1;
3873 if (request_type & CHAINED_REQUEST) {
3874 if (!(request_type & END_OF_CHAIN)) {
3875 /* next 8-byte aligned request */
3876 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3877 shdr->NextCommand = cpu_to_le32(*total_len);
3878 } else /* END_OF_CHAIN */
3879 shdr->NextCommand = 0;
3880 if (request_type & RELATED_REQUEST) {
3881 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
3883 * Related requests use info from previous read request
3886 shdr->SessionId = 0xFFFFFFFF;
3887 shdr->TreeId = 0xFFFFFFFF;
3888 req->PersistentFileId = 0xFFFFFFFF;
3889 req->VolatileFileId = 0xFFFFFFFF;
3892 if (remaining_bytes > io_parms->length)
3893 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3895 req->RemainingBytes = 0;
3902 smb2_readv_callback(struct mid_q_entry *mid)
3904 struct cifs_readdata *rdata = mid->callback_data;
3905 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3906 struct TCP_Server_Info *server = rdata->server;
3907 struct smb2_sync_hdr *shdr =
3908 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
3909 struct cifs_credits credits = { .value = 0, .instance = 0 };
3910 struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
3912 .rq_pages = rdata->pages,
3913 .rq_offset = rdata->page_offset,
3914 .rq_npages = rdata->nr_pages,
3915 .rq_pagesz = rdata->pagesz,
3916 .rq_tailsz = rdata->tailsz };
3918 WARN_ONCE(rdata->server != mid->server,
3919 "rdata server %p != mid server %p",
3920 rdata->server, mid->server);
3922 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3923 __func__, mid->mid, mid->mid_state, rdata->result,
3926 switch (mid->mid_state) {
3927 case MID_RESPONSE_RECEIVED:
3928 credits.value = le16_to_cpu(shdr->CreditRequest);
3929 credits.instance = server->reconnect_instance;
3930 /* result already set, check signature */
3931 if (server->sign && !mid->decrypted) {
3934 rc = smb2_verify_signature(&rqst, server);
3936 cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
3939 /* FIXME: should this be counted toward the initiating task? */
3940 task_io_account_read(rdata->got_bytes);
3941 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3943 case MID_REQUEST_SUBMITTED:
3944 case MID_RETRY_NEEDED:
3945 rdata->result = -EAGAIN;
3946 if (server->sign && rdata->got_bytes)
3947 /* reset bytes number since we can not check a sign */
3948 rdata->got_bytes = 0;
3949 /* FIXME: should this be counted toward the initiating task? */
3950 task_io_account_read(rdata->got_bytes);
3951 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3953 case MID_RESPONSE_MALFORMED:
3954 credits.value = le16_to_cpu(shdr->CreditRequest);
3955 credits.instance = server->reconnect_instance;
3958 rdata->result = -EIO;
3960 #ifdef CONFIG_CIFS_SMB_DIRECT
3962 * If this rdata has a memmory registered, the MR can be freed
3963 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3964 * because they have limited number and are used for future I/Os
3967 smbd_deregister_mr(rdata->mr);
3971 if (rdata->result && rdata->result != -ENODATA) {
3972 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
3973 trace_smb3_read_err(0 /* xid */,
3974 rdata->cfile->fid.persistent_fid,
3975 tcon->tid, tcon->ses->Suid, rdata->offset,
3976 rdata->bytes, rdata->result);
3978 trace_smb3_read_done(0 /* xid */,
3979 rdata->cfile->fid.persistent_fid,
3980 tcon->tid, tcon->ses->Suid,
3981 rdata->offset, rdata->got_bytes);
3983 queue_work(cifsiod_wq, &rdata->work);
3984 DeleteMidQEntry(mid);
3985 add_credits(server, &credits, 0);
3988 /* smb2_async_readv - send an async read, and set up mid to handle result */
3990 smb2_async_readv(struct cifs_readdata *rdata)
3994 struct smb2_sync_hdr *shdr;
3995 struct cifs_io_parms io_parms;
3996 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3998 struct TCP_Server_Info *server;
3999 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
4000 unsigned int total_len;
4002 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
4003 __func__, rdata->offset, rdata->bytes);
4006 rdata->server = cifs_pick_channel(tcon->ses);
4008 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
4009 io_parms.server = server = rdata->server;
4010 io_parms.offset = rdata->offset;
4011 io_parms.length = rdata->bytes;
4012 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
4013 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
4014 io_parms.pid = rdata->pid;
4016 rc = smb2_new_read_req(
4017 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
4021 if (smb3_encryption_required(io_parms.tcon))
4022 flags |= CIFS_TRANSFORM_REQ;
4024 rdata->iov[0].iov_base = buf;
4025 rdata->iov[0].iov_len = total_len;
4027 shdr = (struct smb2_sync_hdr *)buf;
4029 if (rdata->credits.value > 0) {
4030 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
4031 SMB2_MAX_BUFFER_SIZE));
4032 shdr->CreditRequest =
4033 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
4035 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4037 goto async_readv_out;
4039 flags |= CIFS_HAS_CREDITS;
4042 kref_get(&rdata->refcount);
4043 rc = cifs_call_async(server, &rqst,
4044 cifs_readv_receive, smb2_readv_callback,
4045 smb3_handle_read_data, rdata, flags,
4048 kref_put(&rdata->refcount, cifs_readdata_release);
4049 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
4050 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
4052 io_parms.tcon->ses->Suid,
4053 io_parms.offset, io_parms.length, rc);
4057 cifs_small_buf_release(buf);
4062 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
4063 unsigned int *nbytes, char **buf, int *buf_type)
4065 struct smb_rqst rqst;
4066 int resp_buftype, rc;
4067 struct smb2_read_plain_req *req = NULL;
4068 struct smb2_read_rsp *rsp = NULL;
4070 struct kvec rsp_iov;
4071 unsigned int total_len;
4072 int flags = CIFS_LOG_ERROR;
4073 struct cifs_ses *ses = io_parms->tcon->ses;
4075 if (!io_parms->server)
4076 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4079 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
4083 if (smb3_encryption_required(io_parms->tcon))
4084 flags |= CIFS_TRANSFORM_REQ;
4086 iov[0].iov_base = (char *)req;
4087 iov[0].iov_len = total_len;
4089 memset(&rqst, 0, sizeof(struct smb_rqst));
4093 rc = cifs_send_recv(xid, ses, io_parms->server,
4094 &rqst, &resp_buftype, flags, &rsp_iov);
4095 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
4098 if (rc != -ENODATA) {
4099 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
4100 cifs_dbg(VFS, "Send error in read = %d\n", rc);
4101 trace_smb3_read_err(xid, req->PersistentFileId,
4102 io_parms->tcon->tid, ses->Suid,
4103 io_parms->offset, io_parms->length,
4106 trace_smb3_read_done(xid, req->PersistentFileId,
4107 io_parms->tcon->tid, ses->Suid,
4108 io_parms->offset, 0);
4109 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4110 cifs_small_buf_release(req);
4111 return rc == -ENODATA ? 0 : rc;
4113 trace_smb3_read_done(xid, req->PersistentFileId,
4114 io_parms->tcon->tid, ses->Suid,
4115 io_parms->offset, io_parms->length);
4117 cifs_small_buf_release(req);
4119 *nbytes = le32_to_cpu(rsp->DataLength);
4120 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
4121 (*nbytes > io_parms->length)) {
4122 cifs_dbg(FYI, "bad length %d for count %d\n",
4123 *nbytes, io_parms->length);
4129 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
4130 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4131 } else if (resp_buftype != CIFS_NO_BUFFER) {
4132 *buf = rsp_iov.iov_base;
4133 if (resp_buftype == CIFS_SMALL_BUFFER)
4134 *buf_type = CIFS_SMALL_BUFFER;
4135 else if (resp_buftype == CIFS_LARGE_BUFFER)
4136 *buf_type = CIFS_LARGE_BUFFER;
4142 * Check the mid_state and signature on received buffer (if any), and queue the
4143 * workqueue completion task.
4146 smb2_writev_callback(struct mid_q_entry *mid)
4148 struct cifs_writedata *wdata = mid->callback_data;
4149 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4150 struct TCP_Server_Info *server = wdata->server;
4151 unsigned int written;
4152 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
4153 struct cifs_credits credits = { .value = 0, .instance = 0 };
4155 WARN_ONCE(wdata->server != mid->server,
4156 "wdata server %p != mid server %p",
4157 wdata->server, mid->server);
4159 switch (mid->mid_state) {
4160 case MID_RESPONSE_RECEIVED:
4161 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4162 credits.instance = server->reconnect_instance;
4163 wdata->result = smb2_check_receive(mid, server, 0);
4164 if (wdata->result != 0)
4167 written = le32_to_cpu(rsp->DataLength);
4169 * Mask off high 16 bits when bytes written as returned
4170 * by the server is greater than bytes requested by the
4171 * client. OS/2 servers are known to set incorrect
4174 if (written > wdata->bytes)
4177 if (written < wdata->bytes)
4178 wdata->result = -ENOSPC;
4180 wdata->bytes = written;
4182 case MID_REQUEST_SUBMITTED:
4183 case MID_RETRY_NEEDED:
4184 wdata->result = -EAGAIN;
4186 case MID_RESPONSE_MALFORMED:
4187 credits.value = le16_to_cpu(rsp->sync_hdr.CreditRequest);
4188 credits.instance = server->reconnect_instance;
4191 wdata->result = -EIO;
4194 #ifdef CONFIG_CIFS_SMB_DIRECT
4196 * If this wdata has a memory registered, the MR can be freed
4197 * The number of MRs available is limited, it's important to recover
4198 * used MR as soon as I/O is finished. Hold MR longer in the later
4199 * I/O process can possibly result in I/O deadlock due to lack of MR
4200 * to send request on I/O retry
4203 smbd_deregister_mr(wdata->mr);
4207 if (wdata->result) {
4208 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4209 trace_smb3_write_err(0 /* no xid */,
4210 wdata->cfile->fid.persistent_fid,
4211 tcon->tid, tcon->ses->Suid, wdata->offset,
4212 wdata->bytes, wdata->result);
4213 if (wdata->result == -ENOSPC)
4214 pr_warn_once("Out of space writing to %s\n",
4217 trace_smb3_write_done(0 /* no xid */,
4218 wdata->cfile->fid.persistent_fid,
4219 tcon->tid, tcon->ses->Suid,
4220 wdata->offset, wdata->bytes);
4222 queue_work(cifsiod_wq, &wdata->work);
4223 DeleteMidQEntry(mid);
4224 add_credits(server, &credits, 0);
4227 /* smb2_async_writev - send an async write, and set up mid to handle result */
4229 smb2_async_writev(struct cifs_writedata *wdata,
4230 void (*release)(struct kref *kref))
4232 int rc = -EACCES, flags = 0;
4233 struct smb2_write_req *req = NULL;
4234 struct smb2_sync_hdr *shdr;
4235 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
4236 struct TCP_Server_Info *server = wdata->server;
4238 struct smb_rqst rqst = { };
4239 unsigned int total_len;
4242 server = wdata->server = cifs_pick_channel(tcon->ses);
4244 rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
4245 (void **) &req, &total_len);
4249 if (smb3_encryption_required(tcon))
4250 flags |= CIFS_TRANSFORM_REQ;
4252 shdr = (struct smb2_sync_hdr *)req;
4253 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
4255 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
4256 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
4257 req->WriteChannelInfoOffset = 0;
4258 req->WriteChannelInfoLength = 0;
4260 req->Offset = cpu_to_le64(wdata->offset);
4261 req->DataOffset = cpu_to_le16(
4262 offsetof(struct smb2_write_req, Buffer));
4263 req->RemainingBytes = 0;
4265 trace_smb3_write_enter(0 /* xid */, wdata->cfile->fid.persistent_fid,
4266 tcon->tid, tcon->ses->Suid, wdata->offset, wdata->bytes);
4267 #ifdef CONFIG_CIFS_SMB_DIRECT
4269 * If we want to do a server RDMA read, fill in and append
4270 * smbd_buffer_descriptor_v1 to the end of write request
4272 if (server->rdma && !server->sign && wdata->bytes >=
4273 server->smbd_conn->rdma_readwrite_threshold) {
4275 struct smbd_buffer_descriptor_v1 *v1;
4276 bool need_invalidate = server->dialect == SMB30_PROT_ID;
4278 wdata->mr = smbd_register_mr(
4279 server->smbd_conn, wdata->pages,
4280 wdata->nr_pages, wdata->page_offset,
4281 wdata->tailsz, false, need_invalidate);
4284 goto async_writev_out;
4287 req->DataOffset = 0;
4288 if (wdata->nr_pages > 1)
4289 req->RemainingBytes =
4291 (wdata->nr_pages - 1) * wdata->pagesz -
4292 wdata->page_offset + wdata->tailsz
4295 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
4296 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
4297 if (need_invalidate)
4298 req->Channel = SMB2_CHANNEL_RDMA_V1;
4299 req->WriteChannelInfoOffset =
4300 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
4301 req->WriteChannelInfoLength =
4302 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
4303 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
4304 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
4305 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
4306 v1->length = cpu_to_le32(wdata->mr->mr->length);
4309 iov[0].iov_len = total_len - 1;
4310 iov[0].iov_base = (char *)req;
4314 rqst.rq_pages = wdata->pages;
4315 rqst.rq_offset = wdata->page_offset;
4316 rqst.rq_npages = wdata->nr_pages;
4317 rqst.rq_pagesz = wdata->pagesz;
4318 rqst.rq_tailsz = wdata->tailsz;
4319 #ifdef CONFIG_CIFS_SMB_DIRECT
4321 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
4325 cifs_dbg(FYI, "async write at %llu %u bytes\n",
4326 wdata->offset, wdata->bytes);
4328 #ifdef CONFIG_CIFS_SMB_DIRECT
4329 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4331 req->Length = cpu_to_le32(wdata->bytes);
4333 req->Length = cpu_to_le32(wdata->bytes);
4336 if (wdata->credits.value > 0) {
4337 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
4338 SMB2_MAX_BUFFER_SIZE));
4339 shdr->CreditRequest =
4340 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
4342 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
4344 goto async_writev_out;
4346 flags |= CIFS_HAS_CREDITS;
4349 kref_get(&wdata->refcount);
4350 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
4351 wdata, flags, &wdata->credits);
4354 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
4355 tcon->tid, tcon->ses->Suid, wdata->offset,
4357 kref_put(&wdata->refcount, release);
4358 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
4362 cifs_small_buf_release(req);
4367 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4368 * The length field from io_parms must be at least 1 and indicates a number of
4369 * elements with data to write that begins with position 1 in iov array. All
4370 * data length is specified by count.
4373 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
4374 unsigned int *nbytes, struct kvec *iov, int n_vec)
4376 struct smb_rqst rqst;
4378 struct smb2_write_req *req = NULL;
4379 struct smb2_write_rsp *rsp = NULL;
4381 struct kvec rsp_iov;
4383 unsigned int total_len;
4384 struct TCP_Server_Info *server;
4391 if (!io_parms->server)
4392 io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
4393 server = io_parms->server;
4395 return -ECONNABORTED;
4397 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
4398 (void **) &req, &total_len);
4402 if (smb3_encryption_required(io_parms->tcon))
4403 flags |= CIFS_TRANSFORM_REQ;
4405 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
4407 req->PersistentFileId = io_parms->persistent_fid;
4408 req->VolatileFileId = io_parms->volatile_fid;
4409 req->WriteChannelInfoOffset = 0;
4410 req->WriteChannelInfoLength = 0;
4412 req->Length = cpu_to_le32(io_parms->length);
4413 req->Offset = cpu_to_le64(io_parms->offset);
4414 req->DataOffset = cpu_to_le16(
4415 offsetof(struct smb2_write_req, Buffer));
4416 req->RemainingBytes = 0;
4418 trace_smb3_write_enter(xid, io_parms->persistent_fid,
4419 io_parms->tcon->tid, io_parms->tcon->ses->Suid,
4420 io_parms->offset, io_parms->length);
4422 iov[0].iov_base = (char *)req;
4424 iov[0].iov_len = total_len - 1;
4426 memset(&rqst, 0, sizeof(struct smb_rqst));
4428 rqst.rq_nvec = n_vec + 1;
4430 rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
4432 &resp_buftype, flags, &rsp_iov);
4433 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
4436 trace_smb3_write_err(xid, req->PersistentFileId,
4437 io_parms->tcon->tid,
4438 io_parms->tcon->ses->Suid,
4439 io_parms->offset, io_parms->length, rc);
4440 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
4441 cifs_dbg(VFS, "Send error in write = %d\n", rc);
4443 *nbytes = le32_to_cpu(rsp->DataLength);
4444 trace_smb3_write_done(xid, req->PersistentFileId,
4445 io_parms->tcon->tid,
4446 io_parms->tcon->ses->Suid,
4447 io_parms->offset, *nbytes);
4450 cifs_small_buf_release(req);
4451 free_rsp_buf(resp_buftype, rsp);
4455 int posix_info_sid_size(const void *beg, const void *end)
4463 subauth = *(u8 *)(beg+1);
4464 if (subauth < 1 || subauth > 15)
4467 total = 1 + 1 + 6 + 4*subauth;
4468 if (beg + total > end)
4474 int posix_info_parse(const void *beg, const void *end,
4475 struct smb2_posix_info_parsed *out)
4481 const void *owner_sid;
4482 const void *group_sid;
4485 /* if no end bound given, assume payload to be correct */
4487 const struct smb2_posix_info *p = beg;
4489 end = beg + le32_to_cpu(p->NextEntryOffset);
4490 /* last element will have a 0 offset, pick a sensible bound */
4495 /* check base buf */
4496 if (beg + sizeof(struct smb2_posix_info) > end)
4498 total_len = sizeof(struct smb2_posix_info);
4500 /* check owner sid */
4501 owner_sid = beg + total_len;
4502 sid_len = posix_info_sid_size(owner_sid, end);
4505 total_len += sid_len;
4507 /* check group sid */
4508 group_sid = beg + total_len;
4509 sid_len = posix_info_sid_size(group_sid, end);
4512 total_len += sid_len;
4514 /* check name len */
4515 if (beg + total_len + 4 > end)
4517 name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
4518 if (name_len < 1 || name_len > 0xFFFF)
4523 name = beg + total_len;
4524 if (name + name_len > end)
4526 total_len += name_len;
4530 out->size = total_len;
4531 out->name_len = name_len;
4533 memcpy(&out->owner, owner_sid,
4534 posix_info_sid_size(owner_sid, end));
4535 memcpy(&out->group, group_sid,
4536 posix_info_sid_size(group_sid, end));
4541 static int posix_info_extra_size(const void *beg, const void *end)
4543 int len = posix_info_parse(beg, end, NULL);
4547 return len - sizeof(struct smb2_posix_info);
4551 num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
4555 unsigned int entrycount = 0;
4556 unsigned int next_offset = 0;
4558 FILE_DIRECTORY_INFO *dir_info;
4560 if (bufstart == NULL)
4563 entryptr = bufstart;
4566 if (entryptr + next_offset < entryptr ||
4567 entryptr + next_offset > end_of_buf ||
4568 entryptr + next_offset + size > end_of_buf) {
4569 cifs_dbg(VFS, "malformed search entry would overflow\n");
4573 entryptr = entryptr + next_offset;
4574 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
4576 if (infotype == SMB_FIND_FILE_POSIX_INFO)
4577 len = posix_info_extra_size(entryptr, end_of_buf);
4579 len = le32_to_cpu(dir_info->FileNameLength);
4582 entryptr + len < entryptr ||
4583 entryptr + len > end_of_buf ||
4584 entryptr + len + size > end_of_buf) {
4585 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
4590 *lastentry = entryptr;
4593 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
4604 int SMB2_query_directory_init(const unsigned int xid,
4605 struct cifs_tcon *tcon,
4606 struct TCP_Server_Info *server,
4607 struct smb_rqst *rqst,
4608 u64 persistent_fid, u64 volatile_fid,
4609 int index, int info_level)
4611 struct smb2_query_directory_req *req;
4612 unsigned char *bufptr;
4613 __le16 asteriks = cpu_to_le16('*');
4614 unsigned int output_size = CIFSMaxBufSize -
4615 MAX_SMB2_CREATE_RESPONSE_SIZE -
4616 MAX_SMB2_CLOSE_RESPONSE_SIZE;
4617 unsigned int total_len;
4618 struct kvec *iov = rqst->rq_iov;
4621 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
4622 (void **) &req, &total_len);
4626 switch (info_level) {
4627 case SMB_FIND_FILE_DIRECTORY_INFO:
4628 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
4630 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4631 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
4633 case SMB_FIND_FILE_POSIX_INFO:
4634 req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
4637 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4642 req->FileIndex = cpu_to_le32(index);
4643 req->PersistentFileId = persistent_fid;
4644 req->VolatileFileId = volatile_fid;
4647 bufptr = req->Buffer;
4648 memcpy(bufptr, &asteriks, len);
4650 req->FileNameOffset =
4651 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
4652 req->FileNameLength = cpu_to_le16(len);
4654 * BB could be 30 bytes or so longer if we used SMB2 specific
4655 * buffer lengths, but this is safe and close enough.
4657 output_size = min_t(unsigned int, output_size, server->maxBuf);
4658 output_size = min_t(unsigned int, output_size, 2 << 15);
4659 req->OutputBufferLength = cpu_to_le32(output_size);
4661 iov[0].iov_base = (char *)req;
4663 iov[0].iov_len = total_len - 1;
4665 iov[1].iov_base = (char *)(req->Buffer);
4666 iov[1].iov_len = len;
4668 trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
4669 tcon->ses->Suid, index, output_size);
4674 void SMB2_query_directory_free(struct smb_rqst *rqst)
4676 if (rqst && rqst->rq_iov) {
4677 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
4682 smb2_parse_query_directory(struct cifs_tcon *tcon,
4683 struct kvec *rsp_iov,
4685 struct cifs_search_info *srch_inf)
4687 struct smb2_query_directory_rsp *rsp;
4688 size_t info_buf_size;
4692 rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
4694 switch (srch_inf->info_level) {
4695 case SMB_FIND_FILE_DIRECTORY_INFO:
4696 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
4698 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
4699 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
4701 case SMB_FIND_FILE_POSIX_INFO:
4702 /* note that posix payload are variable size */
4703 info_buf_size = sizeof(struct smb2_posix_info);
4706 cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
4707 srch_inf->info_level);
4711 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4712 le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
4715 cifs_tcon_dbg(VFS, "bad info payload");
4719 srch_inf->unicode = true;
4721 if (srch_inf->ntwrk_buf_start) {
4722 if (srch_inf->smallBuf)
4723 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
4725 cifs_buf_release(srch_inf->ntwrk_buf_start);
4727 srch_inf->ntwrk_buf_start = (char *)rsp;
4728 srch_inf->srch_entries_start = srch_inf->last_entry =
4729 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
4730 end_of_smb = rsp_iov->iov_len + (char *)rsp;
4732 srch_inf->entries_in_buffer = num_entries(
4733 srch_inf->info_level,
4734 srch_inf->srch_entries_start,
4736 &srch_inf->last_entry,
4739 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
4740 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
4741 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
4742 srch_inf->srch_entries_start, srch_inf->last_entry);
4743 if (resp_buftype == CIFS_LARGE_BUFFER)
4744 srch_inf->smallBuf = false;
4745 else if (resp_buftype == CIFS_SMALL_BUFFER)
4746 srch_inf->smallBuf = true;
4748 cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
4754 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
4755 u64 persistent_fid, u64 volatile_fid, int index,
4756 struct cifs_search_info *srch_inf)
4758 struct smb_rqst rqst;
4759 struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
4760 struct smb2_query_directory_rsp *rsp = NULL;
4761 int resp_buftype = CIFS_NO_BUFFER;
4762 struct kvec rsp_iov;
4764 struct cifs_ses *ses = tcon->ses;
4765 struct TCP_Server_Info *server = cifs_pick_channel(ses);
4768 if (!ses || !(ses->server))
4771 if (smb3_encryption_required(tcon))
4772 flags |= CIFS_TRANSFORM_REQ;
4774 memset(&rqst, 0, sizeof(struct smb_rqst));
4775 memset(&iov, 0, sizeof(iov));
4777 rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
4779 rc = SMB2_query_directory_init(xid, tcon, server,
4780 &rqst, persistent_fid,
4781 volatile_fid, index,
4782 srch_inf->info_level);
4786 rc = cifs_send_recv(xid, ses, server,
4787 &rqst, &resp_buftype, flags, &rsp_iov);
4788 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
4791 if (rc == -ENODATA &&
4792 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
4793 trace_smb3_query_dir_done(xid, persistent_fid,
4794 tcon->tid, tcon->ses->Suid, index, 0);
4795 srch_inf->endOfSearch = true;
4798 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4799 tcon->ses->Suid, index, 0, rc);
4800 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
4805 rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
4808 trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
4809 tcon->ses->Suid, index, 0, rc);
4812 resp_buftype = CIFS_NO_BUFFER;
4814 trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
4815 tcon->ses->Suid, index, srch_inf->entries_in_buffer);
4818 SMB2_query_directory_free(&rqst);
4819 free_rsp_buf(resp_buftype, rsp);
4824 SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
4825 struct smb_rqst *rqst,
4826 u64 persistent_fid, u64 volatile_fid, u32 pid,
4827 u8 info_class, u8 info_type, u32 additional_info,
4828 void **data, unsigned int *size)
4830 struct smb2_set_info_req *req;
4831 struct kvec *iov = rqst->rq_iov;
4832 unsigned int i, total_len;
4835 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
4836 (void **) &req, &total_len);
4840 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4841 req->InfoType = info_type;
4842 req->FileInfoClass = info_class;
4843 req->PersistentFileId = persistent_fid;
4844 req->VolatileFileId = volatile_fid;
4845 req->AdditionalInformation = cpu_to_le32(additional_info);
4848 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
4849 req->BufferLength = cpu_to_le32(*size);
4851 memcpy(req->Buffer, *data, *size);
4854 iov[0].iov_base = (char *)req;
4856 iov[0].iov_len = total_len - 1;
4858 for (i = 1; i < rqst->rq_nvec; i++) {
4859 le32_add_cpu(&req->BufferLength, size[i]);
4860 iov[i].iov_base = (char *)data[i];
4861 iov[i].iov_len = size[i];
4868 SMB2_set_info_free(struct smb_rqst *rqst)
4870 if (rqst && rqst->rq_iov)
4871 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
4875 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4876 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
4877 u8 info_type, u32 additional_info, unsigned int num,
4878 void **data, unsigned int *size)
4880 struct smb_rqst rqst;
4881 struct smb2_set_info_rsp *rsp = NULL;
4883 struct kvec rsp_iov;
4886 struct cifs_ses *ses = tcon->ses;
4887 struct TCP_Server_Info *server = cifs_pick_channel(ses);
4890 if (!ses || !server)
4896 if (smb3_encryption_required(tcon))
4897 flags |= CIFS_TRANSFORM_REQ;
4899 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
4903 memset(&rqst, 0, sizeof(struct smb_rqst));
4907 rc = SMB2_set_info_init(tcon, server,
4908 &rqst, persistent_fid, volatile_fid, pid,
4909 info_class, info_type, additional_info,
4917 rc = cifs_send_recv(xid, ses, server,
4918 &rqst, &resp_buftype, flags,
4920 SMB2_set_info_free(&rqst);
4921 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
4924 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
4925 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
4926 ses->Suid, info_class, (__u32)info_type, rc);
4929 free_rsp_buf(resp_buftype, rsp);
4935 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4936 u64 volatile_fid, u32 pid, __le64 *eof)
4938 struct smb2_file_eof_info info;
4942 info.EndOfFile = *eof;
4945 size = sizeof(struct smb2_file_eof_info);
4947 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4948 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4949 0, 1, &data, &size);
4953 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4954 u64 persistent_fid, u64 volatile_fid,
4955 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4957 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4958 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4959 1, (void **)&pnntsd, &pacllen);
4963 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4964 u64 persistent_fid, u64 volatile_fid,
4965 struct smb2_file_full_ea_info *buf, int len)
4967 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4968 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4969 0, 1, (void **)&buf, &len);
4973 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4974 const u64 persistent_fid, const u64 volatile_fid,
4977 struct smb_rqst rqst;
4979 struct smb2_oplock_break *req = NULL;
4980 struct cifs_ses *ses = tcon->ses;
4981 struct TCP_Server_Info *server = cifs_pick_channel(ses);
4982 int flags = CIFS_OBREAK_OP;
4983 unsigned int total_len;
4985 struct kvec rsp_iov;
4988 cifs_dbg(FYI, "SMB2_oplock_break\n");
4989 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
4990 (void **) &req, &total_len);
4994 if (smb3_encryption_required(tcon))
4995 flags |= CIFS_TRANSFORM_REQ;
4997 req->VolatileFid = volatile_fid;
4998 req->PersistentFid = persistent_fid;
4999 req->OplockLevel = oplock_level;
5000 req->sync_hdr.CreditRequest = cpu_to_le16(1);
5002 flags |= CIFS_NO_RSP_BUF;
5004 iov[0].iov_base = (char *)req;
5005 iov[0].iov_len = total_len;
5007 memset(&rqst, 0, sizeof(struct smb_rqst));
5011 rc = cifs_send_recv(xid, ses, server,
5012 &rqst, &resp_buf_type, flags, &rsp_iov);
5013 cifs_small_buf_release(req);
5016 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5017 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
5024 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
5025 struct kstatfs *kst)
5027 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
5028 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
5029 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
5030 kst->f_bfree = kst->f_bavail =
5031 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
5036 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
5037 struct kstatfs *kst)
5039 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
5040 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
5041 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
5042 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
5043 kst->f_bavail = kst->f_bfree;
5045 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
5046 if (response_data->TotalFileNodes != cpu_to_le64(-1))
5047 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
5048 if (response_data->FreeFileNodes != cpu_to_le64(-1))
5049 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
5055 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
5056 struct TCP_Server_Info *server,
5057 int level, int outbuf_len, u64 persistent_fid,
5061 struct smb2_query_info_req *req;
5062 unsigned int total_len;
5064 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
5066 if ((tcon->ses == NULL) || server == NULL)
5069 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
5070 (void **) &req, &total_len);
5074 req->InfoType = SMB2_O_INFO_FILESYSTEM;
5075 req->FileInfoClass = level;
5076 req->PersistentFileId = persistent_fid;
5077 req->VolatileFileId = volatile_fid;
5079 req->InputBufferOffset =
5080 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
5081 req->OutputBufferLength = cpu_to_le32(
5082 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
5084 iov->iov_base = (char *)req;
5085 iov->iov_len = total_len;
5090 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
5091 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5093 struct smb_rqst rqst;
5094 struct smb2_query_info_rsp *rsp = NULL;
5096 struct kvec rsp_iov;
5099 struct cifs_ses *ses = tcon->ses;
5100 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5101 FILE_SYSTEM_POSIX_INFO *info = NULL;
5104 rc = build_qfs_info_req(&iov, tcon, server,
5105 FS_POSIX_INFORMATION,
5106 sizeof(FILE_SYSTEM_POSIX_INFO),
5107 persistent_fid, volatile_fid);
5111 if (smb3_encryption_required(tcon))
5112 flags |= CIFS_TRANSFORM_REQ;
5114 memset(&rqst, 0, sizeof(struct smb_rqst));
5118 rc = cifs_send_recv(xid, ses, server,
5119 &rqst, &resp_buftype, flags, &rsp_iov);
5120 cifs_small_buf_release(iov.iov_base);
5122 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5123 goto posix_qfsinf_exit;
5125 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5127 info = (FILE_SYSTEM_POSIX_INFO *)(
5128 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5129 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5130 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5131 sizeof(FILE_SYSTEM_POSIX_INFO));
5133 copy_posix_fs_info_to_kstatfs(info, fsdata);
5136 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5141 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
5142 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
5144 struct smb_rqst rqst;
5145 struct smb2_query_info_rsp *rsp = NULL;
5147 struct kvec rsp_iov;
5150 struct cifs_ses *ses = tcon->ses;
5151 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5152 struct smb2_fs_full_size_info *info = NULL;
5155 rc = build_qfs_info_req(&iov, tcon, server,
5156 FS_FULL_SIZE_INFORMATION,
5157 sizeof(struct smb2_fs_full_size_info),
5158 persistent_fid, volatile_fid);
5162 if (smb3_encryption_required(tcon))
5163 flags |= CIFS_TRANSFORM_REQ;
5165 memset(&rqst, 0, sizeof(struct smb_rqst));
5169 rc = cifs_send_recv(xid, ses, server,
5170 &rqst, &resp_buftype, flags, &rsp_iov);
5171 cifs_small_buf_release(iov.iov_base);
5173 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5176 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5178 info = (struct smb2_fs_full_size_info *)(
5179 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
5180 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
5181 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
5182 sizeof(struct smb2_fs_full_size_info));
5184 smb2_copy_fs_info_to_kstatfs(info, fsdata);
5187 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5192 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
5193 u64 persistent_fid, u64 volatile_fid, int level)
5195 struct smb_rqst rqst;
5196 struct smb2_query_info_rsp *rsp = NULL;
5198 struct kvec rsp_iov;
5200 int resp_buftype, max_len, min_len;
5201 struct cifs_ses *ses = tcon->ses;
5202 struct TCP_Server_Info *server = cifs_pick_channel(ses);
5203 unsigned int rsp_len, offset;
5206 if (level == FS_DEVICE_INFORMATION) {
5207 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5208 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
5209 } else if (level == FS_ATTRIBUTE_INFORMATION) {
5210 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
5211 min_len = MIN_FS_ATTR_INFO_SIZE;
5212 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
5213 max_len = sizeof(struct smb3_fs_ss_info);
5214 min_len = sizeof(struct smb3_fs_ss_info);
5215 } else if (level == FS_VOLUME_INFORMATION) {
5216 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
5217 min_len = sizeof(struct smb3_fs_vol_info);
5219 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
5223 rc = build_qfs_info_req(&iov, tcon, server,
5225 persistent_fid, volatile_fid);
5229 if (smb3_encryption_required(tcon))
5230 flags |= CIFS_TRANSFORM_REQ;
5232 memset(&rqst, 0, sizeof(struct smb_rqst));
5236 rc = cifs_send_recv(xid, ses, server,
5237 &rqst, &resp_buftype, flags, &rsp_iov);
5238 cifs_small_buf_release(iov.iov_base);
5240 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
5243 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
5245 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
5246 offset = le16_to_cpu(rsp->OutputBufferOffset);
5247 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
5251 if (level == FS_ATTRIBUTE_INFORMATION)
5252 memcpy(&tcon->fsAttrInfo, offset
5253 + (char *)rsp, min_t(unsigned int,
5255 else if (level == FS_DEVICE_INFORMATION)
5256 memcpy(&tcon->fsDevInfo, offset
5257 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
5258 else if (level == FS_SECTOR_SIZE_INFORMATION) {
5259 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
5260 (offset + (char *)rsp);
5261 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
5262 tcon->perf_sector_size =
5263 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
5264 } else if (level == FS_VOLUME_INFORMATION) {
5265 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
5266 (offset + (char *)rsp);
5267 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
5268 tcon->vol_create_time = vol_info->VolumeCreationTime;
5272 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
5277 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
5278 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5279 const __u32 num_lock, struct smb2_lock_element *buf)
5281 struct smb_rqst rqst;
5283 struct smb2_lock_req *req = NULL;
5285 struct kvec rsp_iov;
5288 int flags = CIFS_NO_RSP_BUF;
5289 unsigned int total_len;
5290 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5292 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
5294 rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
5295 (void **) &req, &total_len);
5299 if (smb3_encryption_required(tcon))
5300 flags |= CIFS_TRANSFORM_REQ;
5302 req->sync_hdr.ProcessId = cpu_to_le32(pid);
5303 req->LockCount = cpu_to_le16(num_lock);
5305 req->PersistentFileId = persist_fid;
5306 req->VolatileFileId = volatile_fid;
5308 count = num_lock * sizeof(struct smb2_lock_element);
5310 iov[0].iov_base = (char *)req;
5311 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
5312 iov[1].iov_base = (char *)buf;
5313 iov[1].iov_len = count;
5315 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
5317 memset(&rqst, 0, sizeof(struct smb_rqst));
5321 rc = cifs_send_recv(xid, tcon->ses, server,
5322 &rqst, &resp_buf_type, flags,
5324 cifs_small_buf_release(req);
5326 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
5327 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
5328 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
5329 tcon->ses->Suid, rc);
5336 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
5337 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
5338 const __u64 length, const __u64 offset, const __u32 lock_flags,
5341 struct smb2_lock_element lock;
5343 lock.Offset = cpu_to_le64(offset);
5344 lock.Length = cpu_to_le64(length);
5345 lock.Flags = cpu_to_le32(lock_flags);
5346 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
5347 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
5349 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
5353 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
5354 __u8 *lease_key, const __le32 lease_state)
5356 struct smb_rqst rqst;
5358 struct smb2_lease_ack *req = NULL;
5359 struct cifs_ses *ses = tcon->ses;
5360 int flags = CIFS_OBREAK_OP;
5361 unsigned int total_len;
5363 struct kvec rsp_iov;
5365 __u64 *please_key_high;
5366 __u64 *please_key_low;
5367 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
5369 cifs_dbg(FYI, "SMB2_lease_break\n");
5370 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
5371 (void **) &req, &total_len);
5375 if (smb3_encryption_required(tcon))
5376 flags |= CIFS_TRANSFORM_REQ;
5378 req->sync_hdr.CreditRequest = cpu_to_le16(1);
5379 req->StructureSize = cpu_to_le16(36);
5382 memcpy(req->LeaseKey, lease_key, 16);
5383 req->LeaseState = lease_state;
5385 flags |= CIFS_NO_RSP_BUF;
5387 iov[0].iov_base = (char *)req;
5388 iov[0].iov_len = total_len;
5390 memset(&rqst, 0, sizeof(struct smb_rqst));
5394 rc = cifs_send_recv(xid, ses, server,
5395 &rqst, &resp_buf_type, flags, &rsp_iov);
5396 cifs_small_buf_release(req);
5398 please_key_low = (__u64 *)lease_key;
5399 please_key_high = (__u64 *)(lease_key+8);
5401 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
5402 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
5403 ses->Suid, *please_key_low, *please_key_high, rc);
5404 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
5406 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
5407 ses->Suid, *please_key_low, *please_key_high);