4 * Copyright (C) International Business Machines Corp., 2009, 2013
9 * Contains the routines for constructing the SMB2 PDUs themselves
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
32 #include <linux/kernel.h>
33 #include <linux/vfs.h>
34 #include <linux/task_io_accounting_ops.h>
35 #include <linux/uaccess.h>
36 #include <linux/uuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/xattr.h>
42 #include "cifsproto.h"
43 #include "smb2proto.h"
44 #include "cifs_unicode.h"
45 #include "cifs_debug.h"
47 #include "smb2status.h"
50 #include "cifs_spnego.h"
51 #include "smbdirect.h"
53 #ifdef CONFIG_CIFS_DFS_UPCALL
54 #include "dfs_cache.h"
58 * The following table defines the expected "StructureSize" of SMB2 requests
59 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
61 * Note that commands are defined in smb2pdu.h in le16 but the array below is
62 * indexed by command in host byte order.
64 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
65 /* SMB2_NEGOTIATE */ 36,
66 /* SMB2_SESSION_SETUP */ 25,
68 /* SMB2_TREE_CONNECT */ 9,
69 /* SMB2_TREE_DISCONNECT */ 4,
79 /* SMB2_QUERY_DIRECTORY */ 33,
80 /* SMB2_CHANGE_NOTIFY */ 32,
81 /* SMB2_QUERY_INFO */ 41,
82 /* SMB2_SET_INFO */ 33,
83 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
86 int smb3_encryption_required(const struct cifs_tcon *tcon)
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)
103 shdr->ProtocolId = SMB2_PROTO_NUMBER;
104 shdr->StructureSize = cpu_to_le16(64);
105 shdr->Command = smb2_cmd;
106 if (tcon && tcon->ses && tcon->ses->server) {
107 struct TCP_Server_Info *server = tcon->ses->server;
109 spin_lock(&server->req_lock);
110 /* Request up to 2 credits but don't go over the limit. */
111 if (server->credits >= server->max_credits)
112 shdr->CreditRequest = cpu_to_le16(0);
114 shdr->CreditRequest = cpu_to_le16(
115 min_t(int, server->max_credits -
116 server->credits, 2));
117 spin_unlock(&server->req_lock);
119 shdr->CreditRequest = cpu_to_le16(2);
121 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
126 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
127 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
128 if ((tcon->ses) && (tcon->ses->server) &&
129 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
130 shdr->CreditCharge = cpu_to_le16(1);
131 /* else CreditCharge MBZ */
133 shdr->TreeId = tcon->tid;
134 /* Uid is not converted */
136 shdr->SessionId = tcon->ses->Suid;
139 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
140 * to pass the path on the Open SMB prefixed by \\server\share.
141 * Not sure when we would need to do the augmented path (if ever) and
142 * setting this flag breaks the SMB2 open operation since it is
143 * illegal to send an empty path name (without \\server\share prefix)
144 * when the DFS flag is set in the SMB open header. We could
145 * consider setting the flag on all operations other than open
146 * but it is safer to net set it for now.
148 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
149 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
151 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
152 !smb3_encryption_required(tcon))
153 shdr->Flags |= SMB2_FLAGS_SIGNED;
158 #ifdef CONFIG_CIFS_DFS_UPCALL
159 static int __smb2_reconnect(const struct nls_table *nlsc,
160 struct cifs_tcon *tcon)
163 struct dfs_cache_tgt_list tl;
164 struct dfs_cache_tgt_iterator *it = NULL;
166 const char *tcp_host;
168 const char *dfs_host;
171 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
176 snprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
177 tcon->ses->server->hostname);
178 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
182 if (!tcon->dfs_path) {
183 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
187 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl);
191 extract_unc_hostname(tcon->ses->server->hostname, &tcp_host,
194 for (it = dfs_cache_get_tgt_iterator(&tl); it;
195 it = dfs_cache_get_next_tgt(&tl, it)) {
196 const char *tgt = dfs_cache_get_tgt_name(it);
198 extract_unc_hostname(tgt, &dfs_host, &dfs_host_len);
200 if (dfs_host_len != tcp_host_len
201 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
202 cifs_dbg(FYI, "%s: skipping %.*s, doesn't match %.*s",
204 (int)dfs_host_len, dfs_host,
205 (int)tcp_host_len, tcp_host);
209 snprintf(tree, MAX_TREE_SIZE, "\\%s", tgt);
211 rc = SMB2_tcon(0, tcon->ses, tree, tcon, nlsc);
220 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1,
225 dfs_cache_free_tgts(&tl);
231 static inline int __smb2_reconnect(const struct nls_table *nlsc,
232 struct cifs_tcon *tcon)
234 return SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nlsc);
239 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
242 struct nls_table *nls_codepage;
243 struct cifs_ses *ses;
244 struct TCP_Server_Info *server;
248 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
249 * check for tcp and smb session status done differently
250 * for those three - in the calling routine.
255 if (smb2_command == SMB2_TREE_CONNECT)
258 if (tcon->tidStatus == CifsExiting) {
260 * only tree disconnect, open, and write,
261 * (and ulogoff which does not have tcon)
262 * are allowed as we start force umount.
264 if ((smb2_command != SMB2_WRITE) &&
265 (smb2_command != SMB2_CREATE) &&
266 (smb2_command != SMB2_TREE_DISCONNECT)) {
267 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
272 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
273 (!tcon->ses->server))
277 server = ses->server;
279 retries = server->nr_targets;
282 * Give demultiplex thread up to 10 seconds to each target available for
283 * reconnect -- should be greater than cifs socket timeout which is 7
286 while (server->tcpStatus == CifsNeedReconnect) {
288 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
289 * here since they are implicitly done when session drops.
291 switch (smb2_command) {
293 * BB Should we keep oplock break and add flush to exceptions?
295 case SMB2_TREE_DISCONNECT:
298 case SMB2_OPLOCK_BREAK:
302 rc = wait_event_interruptible_timeout(server->response_q,
303 (server->tcpStatus != CifsNeedReconnect),
306 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
307 " signal by the process\n", __func__);
311 /* are we still trying to reconnect? */
312 if (server->tcpStatus != CifsNeedReconnect)
319 * on "soft" mounts we wait once. Hard mounts keep
320 * retrying until process is killed or server comes
324 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
327 retries = server->nr_targets;
330 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
333 nls_codepage = load_nls_default();
336 * need to prevent multiple threads trying to simultaneously reconnect
337 * the same SMB session
339 mutex_lock(&tcon->ses->session_mutex);
342 * Recheck after acquire mutex. If another thread is negotiating
343 * and the server never sends an answer the socket will be closed
344 * and tcpStatus set to reconnect.
346 if (server->tcpStatus == CifsNeedReconnect) {
348 mutex_unlock(&tcon->ses->session_mutex);
352 rc = cifs_negotiate_protocol(0, tcon->ses);
353 if (!rc && tcon->ses->need_reconnect)
354 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
356 if (rc || !tcon->need_reconnect) {
357 mutex_unlock(&tcon->ses->session_mutex);
361 cifs_mark_open_files_invalid(tcon);
362 if (tcon->use_persistent)
363 tcon->need_reopen_files = true;
365 rc = __smb2_reconnect(nls_codepage, tcon);
366 mutex_unlock(&tcon->ses->session_mutex);
368 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
370 /* If sess reconnected but tcon didn't, something strange ... */
371 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
375 if (smb2_command != SMB2_INTERNAL_CMD)
376 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
378 atomic_inc(&tconInfoReconnectCount);
381 * Check if handle based operation so we know whether we can continue
382 * or not without returning to caller to reset file handle.
385 * BB Is flush done by server on drop of tcp session? Should we special
386 * case it and skip above?
388 switch (smb2_command) {
394 case SMB2_QUERY_DIRECTORY:
395 case SMB2_CHANGE_NOTIFY:
396 case SMB2_QUERY_INFO:
400 unload_nls(nls_codepage);
405 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
406 unsigned int *total_len)
408 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
409 /* lookup word count ie StructureSize from table */
410 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
413 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
414 * largest operations (Create)
418 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
419 spdu->StructureSize2 = cpu_to_le16(parmsize);
421 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
425 * Allocate and return pointer to an SMB request hdr, and set basic
426 * SMB information in the SMB header. If the return code is zero, this
427 * function must have filled in request_buf pointer.
430 smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
431 void **request_buf, unsigned int *total_len)
435 rc = smb2_reconnect(smb2_command, tcon);
439 /* BB eventually switch this to SMB2 specific small buf size */
440 if (smb2_command == SMB2_SET_INFO)
441 *request_buf = cifs_buf_get();
443 *request_buf = cifs_small_buf_get();
444 if (*request_buf == NULL) {
445 /* BB should we add a retry in here if not a writepage? */
449 fill_small_buf(smb2_command, tcon,
450 (struct smb2_sync_hdr *)(*request_buf),
454 uint16_t com_code = le16_to_cpu(smb2_command);
455 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
456 cifs_stats_inc(&tcon->num_smbs_sent);
463 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
464 #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
465 #define SMB2_POSIX_EXTENSIONS_AVAILABLE cpu_to_le16(0x100)
468 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
470 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
471 pneg_ctxt->DataLength = cpu_to_le16(38);
472 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
473 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
474 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
475 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
479 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
481 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
482 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + le16 cipher */
483 pneg_ctxt->CipherCount = cpu_to_le16(1);
484 /* pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;*/ /* not supported yet */
485 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_CCM;
489 build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
491 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
492 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
496 assemble_neg_contexts(struct smb2_negotiate_req *req,
497 unsigned int *total_len)
499 char *pneg_ctxt = (char *)req;
500 unsigned int ctxt_len;
502 if (*total_len > 200) {
503 /* In case length corrupted don't want to overrun smb buffer */
504 cifs_dbg(VFS, "Bad frame length assembling neg contexts\n");
509 * round up total_len of fixed part of SMB3 negotiate request to 8
510 * byte boundary before adding negotiate contexts
512 *total_len = roundup(*total_len, 8);
514 pneg_ctxt = (*total_len) + (char *)req;
515 req->NegotiateContextOffset = cpu_to_le32(*total_len);
517 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
518 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
519 *total_len += ctxt_len;
520 pneg_ctxt += ctxt_len;
522 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
523 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
524 *total_len += ctxt_len;
525 pneg_ctxt += ctxt_len;
527 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
528 *total_len += sizeof(struct smb2_posix_neg_context);
530 req->NegotiateContextCount = cpu_to_le16(3);
533 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
535 unsigned int len = le16_to_cpu(ctxt->DataLength);
537 /* If invalid preauth context warn but use what we requested, SHA-512 */
538 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
539 printk_once(KERN_WARNING "server sent bad preauth context\n");
542 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
543 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
544 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
545 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
548 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
549 struct smb2_encryption_neg_context *ctxt)
551 unsigned int len = le16_to_cpu(ctxt->DataLength);
553 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
554 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
555 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
559 if (le16_to_cpu(ctxt->CipherCount) != 1) {
560 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
563 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
564 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
565 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
566 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
569 server->cipher_type = ctxt->Ciphers[0];
570 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
574 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
575 struct TCP_Server_Info *server,
576 unsigned int len_of_smb)
578 struct smb2_neg_context *pctx;
579 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
580 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
581 unsigned int len_of_ctxts, i;
584 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
585 if (len_of_smb <= offset) {
586 cifs_dbg(VFS, "Invalid response: negotiate context offset\n");
590 len_of_ctxts = len_of_smb - offset;
592 for (i = 0; i < ctxt_cnt; i++) {
594 /* check that offset is not beyond end of SMB */
595 if (len_of_ctxts == 0)
598 if (len_of_ctxts < sizeof(struct smb2_neg_context))
601 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
602 clen = le16_to_cpu(pctx->DataLength);
603 if (clen > len_of_ctxts)
606 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
607 decode_preauth_context(
608 (struct smb2_preauth_neg_context *)pctx);
609 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
610 rc = decode_encrypt_ctx(server,
611 (struct smb2_encryption_neg_context *)pctx);
612 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
613 server->posix_ext_supported = true;
615 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
616 le16_to_cpu(pctx->ContextType));
620 /* offsets must be 8 byte aligned */
621 clen = (clen + 7) & ~0x7;
622 offset += clen + sizeof(struct smb2_neg_context);
623 len_of_ctxts -= clen;
628 static struct create_posix *
629 create_posix_buf(umode_t mode)
631 struct create_posix *buf;
633 buf = kzalloc(sizeof(struct create_posix),
638 buf->ccontext.DataOffset =
639 cpu_to_le16(offsetof(struct create_posix, Mode));
640 buf->ccontext.DataLength = cpu_to_le32(4);
641 buf->ccontext.NameOffset =
642 cpu_to_le16(offsetof(struct create_posix, Name));
643 buf->ccontext.NameLength = cpu_to_le16(16);
645 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
656 buf->Name[10] = 0x83;
657 buf->Name[11] = 0xDE;
658 buf->Name[12] = 0x96;
659 buf->Name[13] = 0x8B;
660 buf->Name[14] = 0xCD;
661 buf->Name[15] = 0x7C;
662 buf->Mode = cpu_to_le32(mode);
663 cifs_dbg(FYI, "mode on posix create 0%o", mode);
668 add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
670 struct smb2_create_req *req = iov[0].iov_base;
671 unsigned int num = *num_iovec;
673 iov[num].iov_base = create_posix_buf(mode);
674 if (iov[num].iov_base == NULL)
676 iov[num].iov_len = sizeof(struct create_posix);
677 if (!req->CreateContextsOffset)
678 req->CreateContextsOffset = cpu_to_le32(
679 sizeof(struct smb2_create_req) +
680 iov[num - 1].iov_len);
681 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
682 *num_iovec = num + 1;
689 * SMB2 Worker functions follow:
691 * The general structure of the worker functions is:
692 * 1) Call smb2_init (assembles SMB2 header)
693 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
694 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
695 * 4) Decode SMB2 command specific fields in the fixed length area
696 * 5) Decode variable length data area (if any for this SMB2 command type)
697 * 6) Call free smb buffer
703 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
705 struct smb_rqst rqst;
706 struct smb2_negotiate_req *req;
707 struct smb2_negotiate_rsp *rsp;
712 struct TCP_Server_Info *server = ses->server;
713 int blob_offset, blob_length;
715 int flags = CIFS_NEG_OP;
716 unsigned int total_len;
718 cifs_dbg(FYI, "Negotiate protocol\n");
721 WARN(1, "%s: server is NULL!\n", __func__);
725 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
729 req->sync_hdr.SessionId = 0;
731 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
732 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
734 if (strcmp(ses->server->vals->version_string,
735 SMB3ANY_VERSION_STRING) == 0) {
736 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
737 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
738 req->DialectCount = cpu_to_le16(2);
740 } else if (strcmp(ses->server->vals->version_string,
741 SMBDEFAULT_VERSION_STRING) == 0) {
742 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
743 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
744 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
745 req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
746 req->DialectCount = cpu_to_le16(4);
749 /* otherwise send specific dialect */
750 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
751 req->DialectCount = cpu_to_le16(1);
755 /* only one of SMB2 signing flags may be set in SMB2 request */
757 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
758 else if (global_secflags & CIFSSEC_MAY_SIGN)
759 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
761 req->SecurityMode = 0;
763 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
765 /* ClientGUID must be zero for SMB2.02 dialect */
766 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
767 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
769 memcpy(req->ClientGUID, server->client_guid,
770 SMB2_CLIENT_GUID_SIZE);
771 if ((ses->server->vals->protocol_id == SMB311_PROT_ID) ||
772 (strcmp(ses->server->vals->version_string,
773 SMBDEFAULT_VERSION_STRING) == 0))
774 assemble_neg_contexts(req, &total_len);
776 iov[0].iov_base = (char *)req;
777 iov[0].iov_len = total_len;
779 memset(&rqst, 0, sizeof(struct smb_rqst));
783 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
784 cifs_small_buf_release(req);
785 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
787 * No tcon so can't do
788 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
790 if (rc == -EOPNOTSUPP) {
791 cifs_dbg(VFS, "Dialect not supported by server. Consider "
792 "specifying vers=1.0 or vers=2.0 on mount for accessing"
798 if (strcmp(ses->server->vals->version_string,
799 SMB3ANY_VERSION_STRING) == 0) {
800 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
802 "SMB2 dialect returned but not requested\n");
804 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
806 "SMB2.1 dialect returned but not requested\n");
809 } else if (strcmp(ses->server->vals->version_string,
810 SMBDEFAULT_VERSION_STRING) == 0) {
811 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
813 "SMB2 dialect returned but not requested\n");
815 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
816 /* ops set to 3.0 by default for default so update */
817 ses->server->ops = &smb21_operations;
818 } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
819 ses->server->ops = &smb311_operations;
820 } else if (le16_to_cpu(rsp->DialectRevision) !=
821 ses->server->vals->protocol_id) {
822 /* if requested single dialect ensure returned dialect matched */
823 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
824 le16_to_cpu(rsp->DialectRevision));
828 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
830 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
831 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
832 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
833 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
834 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
835 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
836 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
837 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
838 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
839 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
841 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
842 le16_to_cpu(rsp->DialectRevision));
846 server->dialect = le16_to_cpu(rsp->DialectRevision);
849 * Keep a copy of the hash after negprot. This hash will be
850 * the starting hash value for all sessions made from this
853 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
854 SMB2_PREAUTH_HASH_SIZE);
856 /* SMB2 only has an extended negflavor */
857 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
858 /* set it to the maximum buffer size value we can send with 1 credit */
859 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
860 SMB2_MAX_BUFFER_SIZE);
861 server->max_read = le32_to_cpu(rsp->MaxReadSize);
862 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
863 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
864 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
865 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
867 server->capabilities = le32_to_cpu(rsp->Capabilities);
869 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
871 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
872 (struct smb2_sync_hdr *)rsp);
874 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
876 * ses->sectype = RawNTLMSSP;
877 * but for time being this is our only auth choice so doesn't matter.
878 * We just found a server which sets blob length to zero expecting raw.
880 if (blob_length == 0) {
881 cifs_dbg(FYI, "missing security blob on negprot\n");
882 server->sec_ntlmssp = true;
885 rc = cifs_enable_signing(server, ses->sign);
889 rc = decode_negTokenInit(security_blob, blob_length, server);
896 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
897 if (rsp->NegotiateContextCount)
898 rc = smb311_decode_neg_context(rsp, server,
901 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
904 free_rsp_buf(resp_buftype, rsp);
908 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
911 struct validate_negotiate_info_req *pneg_inbuf;
912 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
914 u32 inbuflen; /* max of 4 dialects */
916 cifs_dbg(FYI, "validate negotiate\n");
918 /* In SMB3.11 preauth integrity supersedes validate negotiate */
919 if (tcon->ses->server->dialect == SMB311_PROT_ID)
923 * validation ioctl must be signed, so no point sending this if we
924 * can not sign it (ie are not known user). Even if signing is not
925 * required (enabled but not negotiated), in those cases we selectively
926 * sign just this, the first and only signed request on a connection.
927 * Having validation of negotiate info helps reduce attack vectors.
929 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
930 return 0; /* validation requires signing */
932 if (tcon->ses->user_name == NULL) {
933 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
934 return 0; /* validation requires signing */
937 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
938 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
940 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
944 pneg_inbuf->Capabilities =
945 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
946 memcpy(pneg_inbuf->Guid, tcon->ses->server->client_guid,
947 SMB2_CLIENT_GUID_SIZE);
950 pneg_inbuf->SecurityMode =
951 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
952 else if (global_secflags & CIFSSEC_MAY_SIGN)
953 pneg_inbuf->SecurityMode =
954 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
956 pneg_inbuf->SecurityMode = 0;
959 if (strcmp(tcon->ses->server->vals->version_string,
960 SMB3ANY_VERSION_STRING) == 0) {
961 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
962 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
963 pneg_inbuf->DialectCount = cpu_to_le16(2);
964 /* structure is big enough for 3 dialects, sending only 2 */
965 inbuflen = sizeof(*pneg_inbuf) -
966 (2 * sizeof(pneg_inbuf->Dialects[0]));
967 } else if (strcmp(tcon->ses->server->vals->version_string,
968 SMBDEFAULT_VERSION_STRING) == 0) {
969 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
970 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
971 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
972 pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
973 pneg_inbuf->DialectCount = cpu_to_le16(4);
974 /* structure is big enough for 3 dialects */
975 inbuflen = sizeof(*pneg_inbuf);
977 /* otherwise specific dialect was requested */
978 pneg_inbuf->Dialects[0] =
979 cpu_to_le16(tcon->ses->server->vals->protocol_id);
980 pneg_inbuf->DialectCount = cpu_to_le16(1);
981 /* structure is big enough for 3 dialects, sending only 1 */
982 inbuflen = sizeof(*pneg_inbuf) -
983 sizeof(pneg_inbuf->Dialects[0]) * 2;
986 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
987 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
988 (char *)pneg_inbuf, inbuflen, (char **)&pneg_rsp, &rsplen);
991 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
997 if (rsplen != sizeof(*pneg_rsp)) {
998 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
1001 /* relax check since Mac returns max bufsize allowed on ioctl */
1002 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
1006 /* check validate negotiate info response matches what we got earlier */
1007 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
1010 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
1013 /* do not validate server guid because not saved at negprot time yet */
1015 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
1016 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
1019 /* validate negotiate successful */
1021 cifs_dbg(FYI, "validate negotiate info successful\n");
1025 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
1034 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1036 switch (requested) {
1043 if (server->sec_ntlmssp &&
1044 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1046 if ((server->sec_kerberos || server->sec_mskerberos) &&
1047 (global_secflags & CIFSSEC_MAY_KRB5))
1055 struct SMB2_sess_data {
1057 struct cifs_ses *ses;
1058 struct nls_table *nls_cp;
1059 void (*func)(struct SMB2_sess_data *);
1061 u64 previous_session;
1063 /* we will send the SMB in three pieces:
1064 * a fixed length beginning part, an optional
1065 * SPNEGO blob (which can be zero length), and a
1066 * last part which will include the strings
1067 * and rest of bcc area. This allows us to avoid
1068 * a large buffer 17K allocation
1075 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
1078 struct cifs_ses *ses = sess_data->ses;
1079 struct smb2_sess_setup_req *req;
1080 struct TCP_Server_Info *server = ses->server;
1081 unsigned int total_len;
1083 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1088 /* First session, not a reauthenticate */
1089 req->sync_hdr.SessionId = 0;
1091 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
1092 req->PreviousSessionId = sess_data->previous_session;
1094 req->Flags = 0; /* MBZ */
1096 /* enough to enable echos and oplocks and one max size write */
1097 req->sync_hdr.CreditRequest = cpu_to_le16(130);
1099 /* only one of SMB2 signing flags may be set in SMB2 request */
1101 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1102 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1103 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1105 req->SecurityMode = 0;
1107 req->Capabilities = 0;
1108 req->Channel = 0; /* MBZ */
1110 sess_data->iov[0].iov_base = (char *)req;
1112 sess_data->iov[0].iov_len = total_len - 1;
1114 * This variable will be used to clear the buffer
1115 * allocated above in case of any error in the calling function.
1117 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1123 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1125 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1126 sess_data->buf0_type = CIFS_NO_BUFFER;
1130 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1133 struct smb_rqst rqst;
1134 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1135 struct kvec rsp_iov = { NULL, 0 };
1137 /* Testing shows that buffer offset must be at location of Buffer[0] */
1138 req->SecurityBufferOffset =
1139 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1140 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1142 memset(&rqst, 0, sizeof(struct smb_rqst));
1143 rqst.rq_iov = sess_data->iov;
1146 /* BB add code to build os and lm fields */
1147 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1149 &sess_data->buf0_type,
1150 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
1151 cifs_small_buf_release(sess_data->iov[0].iov_base);
1152 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1158 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1161 struct cifs_ses *ses = sess_data->ses;
1163 mutex_lock(&ses->server->srv_mutex);
1164 if (ses->server->ops->generate_signingkey) {
1165 rc = ses->server->ops->generate_signingkey(ses);
1168 "SMB3 session key generation failed\n");
1169 mutex_unlock(&ses->server->srv_mutex);
1173 if (!ses->server->session_estab) {
1174 ses->server->sequence_number = 0x2;
1175 ses->server->session_estab = true;
1177 mutex_unlock(&ses->server->srv_mutex);
1179 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1180 spin_lock(&GlobalMid_Lock);
1181 ses->status = CifsGood;
1182 ses->need_reconnect = false;
1183 spin_unlock(&GlobalMid_Lock);
1187 #ifdef CONFIG_CIFS_UPCALL
1189 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1192 struct cifs_ses *ses = sess_data->ses;
1193 struct cifs_spnego_msg *msg;
1194 struct key *spnego_key = NULL;
1195 struct smb2_sess_setup_rsp *rsp = NULL;
1197 rc = SMB2_sess_alloc_buffer(sess_data);
1201 spnego_key = cifs_get_spnego_key(ses);
1202 if (IS_ERR(spnego_key)) {
1203 rc = PTR_ERR(spnego_key);
1208 msg = spnego_key->payload.data[0];
1210 * check version field to make sure that cifs.upcall is
1211 * sending us a response in an expected form
1213 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1215 "bad cifs.upcall version. Expected %d got %d",
1216 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1218 goto out_put_spnego_key;
1221 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1223 if (!ses->auth_key.response) {
1225 "Kerberos can't allocate (%u bytes) memory",
1228 goto out_put_spnego_key;
1230 ses->auth_key.len = msg->sesskey_len;
1232 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1233 sess_data->iov[1].iov_len = msg->secblob_len;
1235 rc = SMB2_sess_sendreceive(sess_data);
1237 goto out_put_spnego_key;
1239 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1240 ses->Suid = rsp->sync_hdr.SessionId;
1242 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1244 rc = SMB2_sess_establish_session(sess_data);
1246 key_invalidate(spnego_key);
1247 key_put(spnego_key);
1249 sess_data->result = rc;
1250 sess_data->func = NULL;
1251 SMB2_sess_free_buffer(sess_data);
1255 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1257 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1258 sess_data->result = -EOPNOTSUPP;
1259 sess_data->func = NULL;
1264 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1267 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1270 struct cifs_ses *ses = sess_data->ses;
1271 struct smb2_sess_setup_rsp *rsp = NULL;
1272 char *ntlmssp_blob = NULL;
1273 bool use_spnego = false; /* else use raw ntlmssp */
1274 u16 blob_length = 0;
1277 * If memory allocation is successful, caller of this function
1280 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1281 if (!ses->ntlmssp) {
1285 ses->ntlmssp->sesskey_per_smbsess = true;
1287 rc = SMB2_sess_alloc_buffer(sess_data);
1291 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1293 if (ntlmssp_blob == NULL) {
1298 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1300 /* BB eventually need to add this */
1301 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1305 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1306 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1308 sess_data->iov[1].iov_base = ntlmssp_blob;
1309 sess_data->iov[1].iov_len = blob_length;
1311 rc = SMB2_sess_sendreceive(sess_data);
1312 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1314 /* If true, rc here is expected and not an error */
1315 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1316 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1322 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1323 le16_to_cpu(rsp->SecurityBufferOffset)) {
1324 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1325 le16_to_cpu(rsp->SecurityBufferOffset));
1329 rc = decode_ntlmssp_challenge(rsp->Buffer,
1330 le16_to_cpu(rsp->SecurityBufferLength), ses);
1334 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1337 ses->Suid = rsp->sync_hdr.SessionId;
1338 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1341 kfree(ntlmssp_blob);
1342 SMB2_sess_free_buffer(sess_data);
1344 sess_data->result = 0;
1345 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1349 kfree(ses->ntlmssp);
1350 ses->ntlmssp = NULL;
1351 sess_data->result = rc;
1352 sess_data->func = NULL;
1356 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1359 struct cifs_ses *ses = sess_data->ses;
1360 struct smb2_sess_setup_req *req;
1361 struct smb2_sess_setup_rsp *rsp = NULL;
1362 unsigned char *ntlmssp_blob = NULL;
1363 bool use_spnego = false; /* else use raw ntlmssp */
1364 u16 blob_length = 0;
1366 rc = SMB2_sess_alloc_buffer(sess_data);
1370 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1371 req->sync_hdr.SessionId = ses->Suid;
1373 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1376 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1381 /* BB eventually need to add this */
1382 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1386 sess_data->iov[1].iov_base = ntlmssp_blob;
1387 sess_data->iov[1].iov_len = blob_length;
1389 rc = SMB2_sess_sendreceive(sess_data);
1393 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1395 ses->Suid = rsp->sync_hdr.SessionId;
1396 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1398 rc = SMB2_sess_establish_session(sess_data);
1400 kfree(ntlmssp_blob);
1401 SMB2_sess_free_buffer(sess_data);
1402 kfree(ses->ntlmssp);
1403 ses->ntlmssp = NULL;
1404 sess_data->result = rc;
1405 sess_data->func = NULL;
1409 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1413 type = smb2_select_sectype(ses->server, ses->sectype);
1414 cifs_dbg(FYI, "sess setup type %d\n", type);
1415 if (type == Unspecified) {
1417 "Unable to select appropriate authentication method!");
1423 sess_data->func = SMB2_auth_kerberos;
1426 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1429 cifs_dbg(VFS, "secType %d not supported!\n", type);
1437 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1438 const struct nls_table *nls_cp)
1441 struct TCP_Server_Info *server = ses->server;
1442 struct SMB2_sess_data *sess_data;
1444 cifs_dbg(FYI, "Session Setup\n");
1447 WARN(1, "%s: server is NULL!\n", __func__);
1451 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1455 rc = SMB2_select_sec(ses, sess_data);
1458 sess_data->xid = xid;
1459 sess_data->ses = ses;
1460 sess_data->buf0_type = CIFS_NO_BUFFER;
1461 sess_data->nls_cp = (struct nls_table *) nls_cp;
1462 sess_data->previous_session = ses->Suid;
1465 * Initialize the session hash with the server one.
1467 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1468 SMB2_PREAUTH_HASH_SIZE);
1470 while (sess_data->func)
1471 sess_data->func(sess_data);
1473 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1474 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
1475 rc = sess_data->result;
1482 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1484 struct smb_rqst rqst;
1485 struct smb2_logoff_req *req; /* response is also trivial struct */
1487 struct TCP_Server_Info *server;
1489 unsigned int total_len;
1491 struct kvec rsp_iov;
1494 cifs_dbg(FYI, "disconnect session %p\n", ses);
1496 if (ses && (ses->server))
1497 server = ses->server;
1501 /* no need to send SMB logoff if uid already closed due to reconnect */
1502 if (ses->need_reconnect)
1503 goto smb2_session_already_dead;
1505 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
1509 /* since no tcon, smb2_init can not do this, so do here */
1510 req->sync_hdr.SessionId = ses->Suid;
1512 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1513 flags |= CIFS_TRANSFORM_REQ;
1514 else if (server->sign)
1515 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1517 flags |= CIFS_NO_RESP;
1519 iov[0].iov_base = (char *)req;
1520 iov[0].iov_len = total_len;
1522 memset(&rqst, 0, sizeof(struct smb_rqst));
1526 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1527 cifs_small_buf_release(req);
1529 * No tcon so can't do
1530 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1533 smb2_session_already_dead:
1537 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1539 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1542 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1544 /* These are similar values to what Windows uses */
1545 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1547 tcon->max_chunks = 256;
1548 tcon->max_bytes_chunk = 1048576;
1549 tcon->max_bytes_copy = 16777216;
1553 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1554 struct cifs_tcon *tcon, const struct nls_table *cp)
1556 struct smb_rqst rqst;
1557 struct smb2_tree_connect_req *req;
1558 struct smb2_tree_connect_rsp *rsp = NULL;
1560 struct kvec rsp_iov = { NULL, 0 };
1564 __le16 *unc_path = NULL;
1566 unsigned int total_len;
1568 cifs_dbg(FYI, "TCON\n");
1570 if (!(ses->server) || !tree)
1573 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1574 if (unc_path == NULL)
1577 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1579 if (unc_path_len < 2) {
1584 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1586 atomic_set(&tcon->num_remote_opens, 0);
1587 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1594 if (smb3_encryption_required(tcon))
1595 flags |= CIFS_TRANSFORM_REQ;
1597 iov[0].iov_base = (char *)req;
1599 iov[0].iov_len = total_len - 1;
1601 /* Testing shows that buffer offset must be at location of Buffer[0] */
1602 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1604 req->PathLength = cpu_to_le16(unc_path_len - 2);
1605 iov[1].iov_base = unc_path;
1606 iov[1].iov_len = unc_path_len;
1608 /* 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 */
1609 if ((ses->server->dialect == SMB311_PROT_ID) &&
1610 !smb3_encryption_required(tcon))
1611 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1613 memset(&rqst, 0, sizeof(struct smb_rqst));
1617 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
1618 cifs_small_buf_release(req);
1619 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1620 trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
1623 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1624 tcon->need_reconnect = true;
1626 goto tcon_error_exit;
1629 switch (rsp->ShareType) {
1630 case SMB2_SHARE_TYPE_DISK:
1631 cifs_dbg(FYI, "connection to disk share\n");
1633 case SMB2_SHARE_TYPE_PIPE:
1635 cifs_dbg(FYI, "connection to pipe share\n");
1637 case SMB2_SHARE_TYPE_PRINT:
1639 cifs_dbg(FYI, "connection to printer\n");
1642 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1644 goto tcon_error_exit;
1647 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1648 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1649 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1650 tcon->tidStatus = CifsGood;
1651 tcon->need_reconnect = false;
1652 tcon->tid = rsp->sync_hdr.TreeId;
1653 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1655 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1656 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1657 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1660 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1661 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1663 init_copy_chunk_defaults(tcon);
1664 if (tcon->ses->server->ops->validate_negotiate)
1665 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1668 free_rsp_buf(resp_buftype, rsp);
1673 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1674 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1680 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1682 struct smb_rqst rqst;
1683 struct smb2_tree_disconnect_req *req; /* response is trivial */
1685 struct cifs_ses *ses = tcon->ses;
1687 unsigned int total_len;
1689 struct kvec rsp_iov;
1692 cifs_dbg(FYI, "Tree Disconnect\n");
1694 if (!ses || !(ses->server))
1697 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1700 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1705 if (smb3_encryption_required(tcon))
1706 flags |= CIFS_TRANSFORM_REQ;
1708 flags |= CIFS_NO_RESP;
1710 iov[0].iov_base = (char *)req;
1711 iov[0].iov_len = total_len;
1713 memset(&rqst, 0, sizeof(struct smb_rqst));
1717 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1718 cifs_small_buf_release(req);
1720 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1726 static struct create_durable *
1727 create_durable_buf(void)
1729 struct create_durable *buf;
1731 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1735 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1736 (struct create_durable, Data));
1737 buf->ccontext.DataLength = cpu_to_le32(16);
1738 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1739 (struct create_durable, Name));
1740 buf->ccontext.NameLength = cpu_to_le16(4);
1741 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1749 static struct create_durable *
1750 create_reconnect_durable_buf(struct cifs_fid *fid)
1752 struct create_durable *buf;
1754 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1758 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1759 (struct create_durable, Data));
1760 buf->ccontext.DataLength = cpu_to_le32(16);
1761 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1762 (struct create_durable, Name));
1763 buf->ccontext.NameLength = cpu_to_le16(4);
1764 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1765 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1766 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1775 parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1776 unsigned int *epoch, char *lease_key)
1779 struct create_context *cc;
1781 unsigned int remaining;
1784 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
1785 remaining = le32_to_cpu(rsp->CreateContextsLength);
1786 cc = (struct create_context *)data_offset;
1787 while (remaining >= sizeof(struct create_context)) {
1788 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1789 if (le16_to_cpu(cc->NameLength) == 4 &&
1790 strncmp(name, "RqLs", 4) == 0)
1791 return server->ops->parse_lease_buf(cc, epoch,
1794 next = le32_to_cpu(cc->Next);
1798 cc = (struct create_context *)((char *)cc + next);
1805 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1806 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
1808 struct smb2_create_req *req = iov[0].iov_base;
1809 unsigned int num = *num_iovec;
1811 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
1812 if (iov[num].iov_base == NULL)
1814 iov[num].iov_len = server->vals->create_lease_size;
1815 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1816 if (!req->CreateContextsOffset)
1817 req->CreateContextsOffset = cpu_to_le32(
1818 sizeof(struct smb2_create_req) +
1819 iov[num - 1].iov_len);
1820 le32_add_cpu(&req->CreateContextsLength,
1821 server->vals->create_lease_size);
1822 *num_iovec = num + 1;
1826 static struct create_durable_v2 *
1827 create_durable_v2_buf(struct cifs_fid *pfid)
1829 struct create_durable_v2 *buf;
1831 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1835 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1836 (struct create_durable_v2, dcontext));
1837 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1838 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1839 (struct create_durable_v2, Name));
1840 buf->ccontext.NameLength = cpu_to_le16(4);
1842 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1843 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1844 generate_random_uuid(buf->dcontext.CreateGuid);
1845 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1847 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1855 static struct create_durable_handle_reconnect_v2 *
1856 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1858 struct create_durable_handle_reconnect_v2 *buf;
1860 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1865 buf->ccontext.DataOffset =
1866 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1868 buf->ccontext.DataLength =
1869 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1870 buf->ccontext.NameOffset =
1871 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1873 buf->ccontext.NameLength = cpu_to_le16(4);
1875 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1876 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1877 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1878 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1880 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1889 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1890 struct cifs_open_parms *oparms)
1892 struct smb2_create_req *req = iov[0].iov_base;
1893 unsigned int num = *num_iovec;
1895 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1896 if (iov[num].iov_base == NULL)
1898 iov[num].iov_len = sizeof(struct create_durable_v2);
1899 if (!req->CreateContextsOffset)
1900 req->CreateContextsOffset =
1901 cpu_to_le32(sizeof(struct smb2_create_req) +
1903 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1904 *num_iovec = num + 1;
1909 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1910 struct cifs_open_parms *oparms)
1912 struct smb2_create_req *req = iov[0].iov_base;
1913 unsigned int num = *num_iovec;
1915 /* indicate that we don't need to relock the file */
1916 oparms->reconnect = false;
1918 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1919 if (iov[num].iov_base == NULL)
1921 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1922 if (!req->CreateContextsOffset)
1923 req->CreateContextsOffset =
1924 cpu_to_le32(sizeof(struct smb2_create_req) +
1926 le32_add_cpu(&req->CreateContextsLength,
1927 sizeof(struct create_durable_handle_reconnect_v2));
1928 *num_iovec = num + 1;
1933 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1934 struct cifs_open_parms *oparms, bool use_persistent)
1936 struct smb2_create_req *req = iov[0].iov_base;
1937 unsigned int num = *num_iovec;
1939 if (use_persistent) {
1940 if (oparms->reconnect)
1941 return add_durable_reconnect_v2_context(iov, num_iovec,
1944 return add_durable_v2_context(iov, num_iovec, oparms);
1947 if (oparms->reconnect) {
1948 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1949 /* indicate that we don't need to relock the file */
1950 oparms->reconnect = false;
1952 iov[num].iov_base = create_durable_buf();
1953 if (iov[num].iov_base == NULL)
1955 iov[num].iov_len = sizeof(struct create_durable);
1956 if (!req->CreateContextsOffset)
1957 req->CreateContextsOffset =
1958 cpu_to_le32(sizeof(struct smb2_create_req) +
1960 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1961 *num_iovec = num + 1;
1965 /* See MS-SMB2 2.2.13.2.7 */
1966 static struct crt_twarp_ctxt *
1967 create_twarp_buf(__u64 timewarp)
1969 struct crt_twarp_ctxt *buf;
1971 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
1975 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1976 (struct crt_twarp_ctxt, Timestamp));
1977 buf->ccontext.DataLength = cpu_to_le32(8);
1978 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1979 (struct crt_twarp_ctxt, Name));
1980 buf->ccontext.NameLength = cpu_to_le16(4);
1981 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
1986 buf->Timestamp = cpu_to_le64(timewarp);
1990 /* See MS-SMB2 2.2.13.2.7 */
1992 add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
1994 struct smb2_create_req *req = iov[0].iov_base;
1995 unsigned int num = *num_iovec;
1997 iov[num].iov_base = create_twarp_buf(timewarp);
1998 if (iov[num].iov_base == NULL)
2000 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
2001 if (!req->CreateContextsOffset)
2002 req->CreateContextsOffset = cpu_to_le32(
2003 sizeof(struct smb2_create_req) +
2004 iov[num - 1].iov_len);
2005 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
2006 *num_iovec = num + 1;
2011 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
2012 const char *treename, const __le16 *path)
2014 int treename_len, path_len;
2015 struct nls_table *cp;
2016 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2021 treename_len = strlen(treename);
2022 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
2028 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
2031 * make room for one path separator between the treename and
2034 *out_len = treename_len + 1 + path_len;
2037 * final path needs to be null-terminated UTF16 with a
2041 *out_size = roundup((*out_len+1)*2, 8);
2042 *out_path = kzalloc(*out_size, GFP_KERNEL);
2046 cp = load_nls_default();
2047 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
2048 UniStrcat(*out_path, sep);
2049 UniStrcat(*out_path, path);
2055 int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
2056 umode_t mode, struct cifs_tcon *tcon,
2057 const char *full_path,
2058 struct cifs_sb_info *cifs_sb)
2060 struct smb_rqst rqst;
2061 struct smb2_create_req *req;
2062 struct smb2_create_rsp *rsp = NULL;
2063 struct cifs_ses *ses = tcon->ses;
2064 struct kvec iov[3]; /* make sure at least one for each open context */
2065 struct kvec rsp_iov = {NULL, 0};
2068 __le16 *copy_path = NULL;
2071 unsigned int n_iov = 2;
2072 __u32 file_attributes = 0;
2073 char *pc_buf = NULL;
2075 unsigned int total_len;
2076 __le16 *utf16_path = NULL;
2078 cifs_dbg(FYI, "mkdir\n");
2080 /* resource #1: path allocation */
2081 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2085 if (!ses || !(ses->server)) {
2090 /* resource #2: request */
2091 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2096 if (smb3_encryption_required(tcon))
2097 flags |= CIFS_TRANSFORM_REQ;
2099 req->ImpersonationLevel = IL_IMPERSONATION;
2100 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2101 /* File attributes ignored on open (used in create though) */
2102 req->FileAttributes = cpu_to_le32(file_attributes);
2103 req->ShareAccess = FILE_SHARE_ALL_LE;
2104 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2105 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2107 iov[0].iov_base = (char *)req;
2108 /* -1 since last byte is buf[0] which is sent below (path) */
2109 iov[0].iov_len = total_len - 1;
2111 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2113 /* [MS-SMB2] 2.2.13 NameOffset:
2114 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2115 * the SMB2 header, the file name includes a prefix that will
2116 * be processed during DFS name normalization as specified in
2117 * section 3.3.5.9. Otherwise, the file name is relative to
2118 * the share that is identified by the TreeId in the SMB2
2121 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2124 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2125 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2127 tcon->treeName, utf16_path);
2131 req->NameLength = cpu_to_le16(name_len * 2);
2132 uni_path_len = copy_size;
2133 /* free before overwriting resource */
2135 utf16_path = copy_path;
2137 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2138 /* MUST set path len (NameLength) to 0 opening root of share */
2139 req->NameLength = cpu_to_le16(uni_path_len - 2);
2140 if (uni_path_len % 8 != 0) {
2141 copy_size = roundup(uni_path_len, 8);
2142 copy_path = kzalloc(copy_size, GFP_KERNEL);
2147 memcpy((char *)copy_path, (const char *)utf16_path,
2149 uni_path_len = copy_size;
2150 /* free before overwriting resource */
2152 utf16_path = copy_path;
2156 iov[1].iov_len = uni_path_len;
2157 iov[1].iov_base = utf16_path;
2158 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2160 if (tcon->posix_extensions) {
2161 /* resource #3: posix buf */
2162 rc = add_posix_context(iov, &n_iov, mode);
2165 pc_buf = iov[n_iov-1].iov_base;
2169 memset(&rqst, 0, sizeof(struct smb_rqst));
2171 rqst.rq_nvec = n_iov;
2173 /* resource #4: response buffer */
2174 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2176 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2177 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2179 FILE_WRITE_ATTRIBUTES, rc);
2180 goto err_free_rsp_buf;
2183 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2184 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2185 ses->Suid, CREATE_NOT_FILE,
2186 FILE_WRITE_ATTRIBUTES);
2188 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2190 /* Eventually save off posix specific response info and timestaps */
2193 free_rsp_buf(resp_buftype, rsp);
2196 cifs_small_buf_release(req);
2203 SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2204 struct cifs_open_parms *oparms, __le16 *path)
2206 struct TCP_Server_Info *server = tcon->ses->server;
2207 struct smb2_create_req *req;
2208 unsigned int n_iov = 2;
2209 __u32 file_attributes = 0;
2212 unsigned int total_len;
2213 struct kvec *iov = rqst->rq_iov;
2217 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2221 iov[0].iov_base = (char *)req;
2222 /* -1 since last byte is buf[0] which is sent below (path) */
2223 iov[0].iov_len = total_len - 1;
2225 if (oparms->create_options & CREATE_OPTION_READONLY)
2226 file_attributes |= ATTR_READONLY;
2227 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2228 file_attributes |= ATTR_SYSTEM;
2230 req->ImpersonationLevel = IL_IMPERSONATION;
2231 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2232 /* File attributes ignored on open (used in create though) */
2233 req->FileAttributes = cpu_to_le32(file_attributes);
2234 req->ShareAccess = FILE_SHARE_ALL_LE;
2235 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2236 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2237 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2239 /* [MS-SMB2] 2.2.13 NameOffset:
2240 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2241 * the SMB2 header, the file name includes a prefix that will
2242 * be processed during DFS name normalization as specified in
2243 * section 3.3.5.9. Otherwise, the file name is relative to
2244 * the share that is identified by the TreeId in the SMB2
2247 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2250 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2251 rc = alloc_path_with_tree_prefix(©_path, ©_size,
2253 tcon->treeName, path);
2256 req->NameLength = cpu_to_le16(name_len * 2);
2257 uni_path_len = copy_size;
2260 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2261 /* MUST set path len (NameLength) to 0 opening root of share */
2262 req->NameLength = cpu_to_le16(uni_path_len - 2);
2263 copy_size = uni_path_len;
2264 if (copy_size % 8 != 0)
2265 copy_size = roundup(copy_size, 8);
2266 copy_path = kzalloc(copy_size, GFP_KERNEL);
2269 memcpy((char *)copy_path, (const char *)path,
2271 uni_path_len = copy_size;
2275 iov[1].iov_len = uni_path_len;
2276 iov[1].iov_base = path;
2278 if (!server->oplocks)
2279 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2281 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2282 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2283 req->RequestedOplockLevel = *oplock;
2284 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2285 (oparms->create_options & CREATE_NOT_FILE))
2286 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2288 rc = add_lease_context(server, iov, &n_iov,
2289 oparms->fid->lease_key, oplock);
2294 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2295 /* need to set Next field of lease context if we request it */
2296 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2297 struct create_context *ccontext =
2298 (struct create_context *)iov[n_iov-1].iov_base;
2300 cpu_to_le32(server->vals->create_lease_size);
2303 rc = add_durable_context(iov, &n_iov, oparms,
2304 tcon->use_persistent);
2309 if (tcon->posix_extensions) {
2311 struct create_context *ccontext =
2312 (struct create_context *)iov[n_iov-1].iov_base;
2314 cpu_to_le32(iov[n_iov-1].iov_len);
2317 rc = add_posix_context(iov, &n_iov, oparms->mode);
2322 if (tcon->snapshot_time) {
2323 cifs_dbg(FYI, "adding snapshot context\n");
2325 struct create_context *ccontext =
2326 (struct create_context *)iov[n_iov-1].iov_base;
2328 cpu_to_le32(iov[n_iov-1].iov_len);
2331 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2337 rqst->rq_nvec = n_iov;
2341 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
2342 * All other vectors are freed by kfree().
2345 SMB2_open_free(struct smb_rqst *rqst)
2349 if (rqst && rqst->rq_iov) {
2350 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2351 for (i = 1; i < rqst->rq_nvec; i++)
2352 if (rqst->rq_iov[i].iov_base != smb2_padding)
2353 kfree(rqst->rq_iov[i].iov_base);
2358 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2359 __u8 *oplock, struct smb2_file_all_info *buf,
2360 struct kvec *err_iov, int *buftype)
2362 struct smb_rqst rqst;
2363 struct smb2_create_rsp *rsp = NULL;
2364 struct TCP_Server_Info *server;
2365 struct cifs_tcon *tcon = oparms->tcon;
2366 struct cifs_ses *ses = tcon->ses;
2367 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2368 struct kvec rsp_iov = {NULL, 0};
2369 int resp_buftype = CIFS_NO_BUFFER;
2373 cifs_dbg(FYI, "create/open\n");
2374 if (ses && (ses->server))
2375 server = ses->server;
2379 if (smb3_encryption_required(tcon))
2380 flags |= CIFS_TRANSFORM_REQ;
2382 memset(&rqst, 0, sizeof(struct smb_rqst));
2383 memset(&iov, 0, sizeof(iov));
2385 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2387 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2391 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2393 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2396 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2397 if (err_iov && rsp) {
2399 *buftype = resp_buftype;
2400 resp_buftype = CIFS_NO_BUFFER;
2403 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2404 oparms->create_options, oparms->desired_access, rc);
2407 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2408 ses->Suid, oparms->create_options,
2409 oparms->desired_access);
2411 atomic_inc(&tcon->num_remote_opens);
2412 oparms->fid->persistent_fid = rsp->PersistentFileId;
2413 oparms->fid->volatile_fid = rsp->VolatileFileId;
2414 #ifdef CONFIG_CIFS_DEBUG2
2415 oparms->fid->mid = le64_to_cpu(rsp->sync_hdr.MessageId);
2416 #endif /* CIFS_DEBUG2 */
2419 memcpy(buf, &rsp->CreationTime, 32);
2420 buf->AllocationSize = rsp->AllocationSize;
2421 buf->EndOfFile = rsp->EndofFile;
2422 buf->Attributes = rsp->FileAttributes;
2423 buf->NumberOfLinks = cpu_to_le32(1);
2424 buf->DeletePending = 0;
2427 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
2428 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch,
2429 oparms->fid->lease_key);
2431 *oplock = rsp->OplockLevel;
2433 SMB2_open_free(&rqst);
2434 free_rsp_buf(resp_buftype, rsp);
2439 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2442 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2443 u64 volatile_fid, u32 opcode, bool is_fsctl,
2444 char *in_data, u32 indatalen,
2445 char **out_data, u32 *plen /* returned data len */)
2447 struct smb_rqst rqst;
2448 struct smb2_ioctl_req *req;
2449 struct smb2_ioctl_rsp *rsp;
2450 struct cifs_ses *ses;
2452 struct kvec rsp_iov;
2457 unsigned int total_len;
2459 cifs_dbg(FYI, "SMB2 IOCTL\n");
2461 if (out_data != NULL)
2464 /* zero out returned data len, in case of error */
2473 if (!ses || !(ses->server))
2476 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
2480 if (smb3_encryption_required(tcon))
2481 flags |= CIFS_TRANSFORM_REQ;
2483 req->CtlCode = cpu_to_le32(opcode);
2484 req->PersistentFileId = persistent_fid;
2485 req->VolatileFileId = volatile_fid;
2488 req->InputCount = cpu_to_le32(indatalen);
2489 /* do not set InputOffset if no input data */
2491 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
2492 iov[1].iov_base = in_data;
2493 iov[1].iov_len = indatalen;
2498 req->OutputOffset = 0;
2499 req->OutputCount = 0; /* MBZ */
2502 * Could increase MaxOutputResponse, but that would require more
2503 * than one credit. Windows typically sets this smaller, but for some
2504 * ioctls it may be useful to allow server to send more. No point
2505 * limiting what the server can send as long as fits in one credit
2506 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
2507 * (by default, note that it can be overridden to make max larger)
2508 * in responses (except for read responses which can be bigger.
2509 * We may want to bump this limit up
2511 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
2514 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2518 iov[0].iov_base = (char *)req;
2521 * If no input data, the size of ioctl struct in
2522 * protocol spec still includes a 1 byte data buffer,
2523 * but if input data passed to ioctl, we do not
2524 * want to double count this, so we do not send
2525 * the dummy one byte of data in iovec[0] if sending
2526 * input data (in iovec[1]).
2530 iov[0].iov_len = total_len - 1;
2532 iov[0].iov_len = total_len;
2534 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2535 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
2536 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
2538 memset(&rqst, 0, sizeof(struct smb_rqst));
2540 rqst.rq_nvec = n_iov;
2542 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2544 cifs_small_buf_release(req);
2545 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
2548 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2549 ses->Suid, 0, opcode, rc);
2551 if ((rc != 0) && (rc != -EINVAL)) {
2552 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2554 } else if (rc == -EINVAL) {
2555 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2556 (opcode != FSCTL_SRV_COPYCHUNK)) {
2557 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2562 /* check if caller wants to look at return data or just return rc */
2563 if ((plen == NULL) || (out_data == NULL))
2566 *plen = le32_to_cpu(rsp->OutputCount);
2568 /* We check for obvious errors in the output buffer length and offset */
2570 goto ioctl_exit; /* server returned no data */
2571 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
2572 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2578 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
2579 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2580 le32_to_cpu(rsp->OutputOffset));
2586 *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
2588 if (*out_data == NULL) {
2594 free_rsp_buf(resp_buftype, rsp);
2599 * Individual callers to ioctl worker function follow
2603 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2604 u64 persistent_fid, u64 volatile_fid)
2607 struct compress_ioctl fsctl_input;
2608 char *ret_data = NULL;
2610 fsctl_input.CompressionState =
2611 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
2613 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2614 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2615 (char *)&fsctl_input /* data input */,
2616 2 /* in data len */, &ret_data /* out data */, NULL);
2618 cifs_dbg(FYI, "set compression rc %d\n", rc);
2624 SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2625 u64 persistent_fid, u64 volatile_fid)
2627 struct smb2_close_req *req;
2628 struct kvec *iov = rqst->rq_iov;
2629 unsigned int total_len;
2632 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2636 req->PersistentFileId = persistent_fid;
2637 req->VolatileFileId = volatile_fid;
2638 iov[0].iov_base = (char *)req;
2639 iov[0].iov_len = total_len;
2645 SMB2_close_free(struct smb_rqst *rqst)
2647 if (rqst && rqst->rq_iov)
2648 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2652 SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2653 u64 persistent_fid, u64 volatile_fid, int flags)
2655 struct smb_rqst rqst;
2656 struct smb2_close_rsp *rsp = NULL;
2657 struct cifs_ses *ses = tcon->ses;
2659 struct kvec rsp_iov;
2660 int resp_buftype = CIFS_NO_BUFFER;
2663 cifs_dbg(FYI, "Close\n");
2665 if (!ses || !(ses->server))
2668 if (smb3_encryption_required(tcon))
2669 flags |= CIFS_TRANSFORM_REQ;
2671 memset(&rqst, 0, sizeof(struct smb_rqst));
2672 memset(&iov, 0, sizeof(iov));
2676 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid);
2680 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2681 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2684 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
2685 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2690 atomic_dec(&tcon->num_remote_opens);
2692 /* BB FIXME - decode close response, update inode for caching */
2695 SMB2_close_free(&rqst);
2696 free_rsp_buf(resp_buftype, rsp);
2701 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2702 u64 persistent_fid, u64 volatile_fid)
2704 return SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2708 smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
2709 struct kvec *iov, unsigned int min_buf_size)
2711 unsigned int smb_len = iov->iov_len;
2712 char *end_of_smb = smb_len + (char *)iov->iov_base;
2713 char *begin_of_buf = offset + (char *)iov->iov_base;
2714 char *end_of_buf = begin_of_buf + buffer_length;
2717 if (buffer_length < min_buf_size) {
2718 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2719 buffer_length, min_buf_size);
2723 /* check if beyond RFC1001 maximum length */
2724 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
2725 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2726 buffer_length, smb_len);
2730 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
2731 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
2739 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2740 * Caller must free buffer.
2743 smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
2744 struct kvec *iov, unsigned int minbufsize,
2747 char *begin_of_buf = offset + (char *)iov->iov_base;
2753 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
2757 memcpy(data, begin_of_buf, buffer_length);
2763 SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2764 u64 persistent_fid, u64 volatile_fid,
2765 u8 info_class, u8 info_type, u32 additional_info,
2766 size_t output_len, size_t input_len, void *input)
2768 struct smb2_query_info_req *req;
2769 struct kvec *iov = rqst->rq_iov;
2770 unsigned int total_len;
2773 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2778 req->InfoType = info_type;
2779 req->FileInfoClass = info_class;
2780 req->PersistentFileId = persistent_fid;
2781 req->VolatileFileId = volatile_fid;
2782 req->AdditionalInformation = cpu_to_le32(additional_info);
2784 req->OutputBufferLength = cpu_to_le32(output_len);
2786 req->InputBufferLength = cpu_to_le32(input_len);
2787 /* total_len for smb query request never close to le16 max */
2788 req->InputBufferOffset = cpu_to_le16(total_len - 1);
2789 memcpy(req->Buffer, input, input_len);
2792 iov[0].iov_base = (char *)req;
2794 iov[0].iov_len = total_len - 1 + input_len;
2799 SMB2_query_info_free(struct smb_rqst *rqst)
2801 if (rqst && rqst->rq_iov)
2802 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2806 query_info(const unsigned int xid, struct cifs_tcon *tcon,
2807 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2808 u32 additional_info, size_t output_len, size_t min_len, void **data,
2811 struct smb_rqst rqst;
2812 struct smb2_query_info_rsp *rsp = NULL;
2814 struct kvec rsp_iov;
2816 int resp_buftype = CIFS_NO_BUFFER;
2817 struct cifs_ses *ses = tcon->ses;
2819 bool allocated = false;
2821 cifs_dbg(FYI, "Query Info\n");
2823 if (!ses || !(ses->server))
2826 if (smb3_encryption_required(tcon))
2827 flags |= CIFS_TRANSFORM_REQ;
2829 memset(&rqst, 0, sizeof(struct smb_rqst));
2830 memset(&iov, 0, sizeof(iov));
2834 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
2835 info_class, info_type, additional_info,
2836 output_len, 0, NULL);
2840 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2841 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2844 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2845 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
2846 ses->Suid, info_class, (__u32)info_type, rc);
2851 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2853 *data = kmalloc(*dlen, GFP_KERNEL);
2856 "Error %d allocating memory for acl\n",
2866 rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
2867 le32_to_cpu(rsp->OutputBufferLength),
2868 &rsp_iov, min_len, *data);
2869 if (rc && allocated) {
2876 SMB2_query_info_free(&rqst);
2877 free_rsp_buf(resp_buftype, rsp);
2881 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2882 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
2884 return query_info(xid, tcon, persistent_fid, volatile_fid,
2885 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
2886 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
2887 sizeof(struct smb2_file_all_info), (void **)&data,
2892 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2893 u64 persistent_fid, u64 volatile_fid,
2894 void **data, u32 *plen)
2896 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2899 return query_info(xid, tcon, persistent_fid, volatile_fid,
2900 0, SMB2_O_INFO_SECURITY, additional_info,
2901 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
2905 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2906 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2908 return query_info(xid, tcon, persistent_fid, volatile_fid,
2909 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
2910 sizeof(struct smb2_file_internal_info),
2911 sizeof(struct smb2_file_internal_info),
2912 (void **)&uniqueid, NULL);
2916 * This is a no-op for now. We're not really interested in the reply, but
2917 * rather in the fact that the server sent one and that server->lstrp
2920 * FIXME: maybe we should consider checking that the reply matches request?
2923 smb2_echo_callback(struct mid_q_entry *mid)
2925 struct TCP_Server_Info *server = mid->callback_data;
2926 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
2927 unsigned int credits_received = 0;
2929 if (mid->mid_state == MID_RESPONSE_RECEIVED
2930 || mid->mid_state == MID_RESPONSE_MALFORMED)
2931 credits_received = le16_to_cpu(rsp->sync_hdr.CreditRequest);
2933 DeleteMidQEntry(mid);
2934 add_credits(server, credits_received, CIFS_ECHO_OP);
2937 void smb2_reconnect_server(struct work_struct *work)
2939 struct TCP_Server_Info *server = container_of(work,
2940 struct TCP_Server_Info, reconnect.work);
2941 struct cifs_ses *ses;
2942 struct cifs_tcon *tcon, *tcon2;
2943 struct list_head tmp_list;
2944 int tcon_exist = false;
2946 int resched = false;
2949 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2950 mutex_lock(&server->reconnect_mutex);
2952 INIT_LIST_HEAD(&tmp_list);
2953 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2955 spin_lock(&cifs_tcp_ses_lock);
2956 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2957 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2958 if (tcon->need_reconnect || tcon->need_reopen_files) {
2960 list_add_tail(&tcon->rlist, &tmp_list);
2964 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2965 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2970 * Get the reference to server struct to be sure that the last call of
2971 * cifs_put_tcon() in the loop below won't release the server pointer.
2974 server->srv_count++;
2976 spin_unlock(&cifs_tcp_ses_lock);
2978 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
2979 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2981 cifs_reopen_persistent_handles(tcon);
2984 list_del_init(&tcon->rlist);
2985 cifs_put_tcon(tcon);
2988 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2990 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
2991 mutex_unlock(&server->reconnect_mutex);
2993 /* now we can safely release srv struct */
2995 cifs_put_tcp_session(server, 1);
2999 SMB2_echo(struct TCP_Server_Info *server)
3001 struct smb2_echo_req *req;
3004 struct smb_rqst rqst = { .rq_iov = iov,
3006 unsigned int total_len;
3008 cifs_dbg(FYI, "In echo request\n");
3010 if (server->tcpStatus == CifsNeedNegotiate) {
3011 /* No need to send echo on newly established connections */
3012 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
3016 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
3020 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3022 iov[0].iov_len = total_len;
3023 iov[0].iov_base = (char *)req;
3025 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
3026 server, CIFS_ECHO_OP);
3028 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
3030 cifs_small_buf_release(req);
3035 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3038 struct smb_rqst rqst;
3039 struct smb2_flush_req *req;
3040 struct cifs_ses *ses = tcon->ses;
3042 struct kvec rsp_iov;
3046 unsigned int total_len;
3048 cifs_dbg(FYI, "Flush\n");
3050 if (!ses || !(ses->server))
3053 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
3057 if (smb3_encryption_required(tcon))
3058 flags |= CIFS_TRANSFORM_REQ;
3060 req->PersistentFileId = persistent_fid;
3061 req->VolatileFileId = volatile_fid;
3063 iov[0].iov_base = (char *)req;
3064 iov[0].iov_len = total_len;
3066 memset(&rqst, 0, sizeof(struct smb_rqst));
3070 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3071 cifs_small_buf_release(req);
3074 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
3075 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3079 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3084 * To form a chain of read requests, any read requests after the first should
3085 * have the end_of_chain boolean set to true.
3088 smb2_new_read_req(void **buf, unsigned int *total_len,
3089 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3090 unsigned int remaining_bytes, int request_type)
3093 struct smb2_read_plain_req *req = NULL;
3094 struct smb2_sync_hdr *shdr;
3095 struct TCP_Server_Info *server;
3097 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3102 server = io_parms->tcon->ses->server;
3104 return -ECONNABORTED;
3106 shdr = &req->sync_hdr;
3107 shdr->ProcessId = cpu_to_le32(io_parms->pid);
3109 req->PersistentFileId = io_parms->persistent_fid;
3110 req->VolatileFileId = io_parms->volatile_fid;
3111 req->ReadChannelInfoOffset = 0; /* reserved */
3112 req->ReadChannelInfoLength = 0; /* reserved */
3113 req->Channel = 0; /* reserved */
3114 req->MinimumCount = 0;
3115 req->Length = cpu_to_le32(io_parms->length);
3116 req->Offset = cpu_to_le64(io_parms->offset);
3117 #ifdef CONFIG_CIFS_SMB_DIRECT
3119 * If we want to do a RDMA write, fill in and append
3120 * smbd_buffer_descriptor_v1 to the end of read request
3122 if (server->rdma && rdata && !server->sign &&
3123 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
3125 struct smbd_buffer_descriptor_v1 *v1;
3126 bool need_invalidate =
3127 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3129 rdata->mr = smbd_register_mr(
3130 server->smbd_conn, rdata->pages,
3131 rdata->nr_pages, rdata->page_offset,
3132 rdata->tailsz, true, need_invalidate);
3136 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3137 if (need_invalidate)
3138 req->Channel = SMB2_CHANNEL_RDMA_V1;
3139 req->ReadChannelInfoOffset =
3140 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
3141 req->ReadChannelInfoLength =
3142 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3143 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3144 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3145 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3146 v1->length = cpu_to_le32(rdata->mr->mr->length);
3148 *total_len += sizeof(*v1) - 1;
3151 if (request_type & CHAINED_REQUEST) {
3152 if (!(request_type & END_OF_CHAIN)) {
3153 /* next 8-byte aligned request */
3154 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3155 shdr->NextCommand = cpu_to_le32(*total_len);
3156 } else /* END_OF_CHAIN */
3157 shdr->NextCommand = 0;
3158 if (request_type & RELATED_REQUEST) {
3159 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
3161 * Related requests use info from previous read request
3164 shdr->SessionId = 0xFFFFFFFF;
3165 shdr->TreeId = 0xFFFFFFFF;
3166 req->PersistentFileId = 0xFFFFFFFF;
3167 req->VolatileFileId = 0xFFFFFFFF;
3170 if (remaining_bytes > io_parms->length)
3171 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3173 req->RemainingBytes = 0;
3180 smb2_readv_callback(struct mid_q_entry *mid)
3182 struct cifs_readdata *rdata = mid->callback_data;
3183 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3184 struct TCP_Server_Info *server = tcon->ses->server;
3185 struct smb2_sync_hdr *shdr =
3186 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
3187 unsigned int credits_received = 0;
3188 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3190 .rq_pages = rdata->pages,
3191 .rq_offset = rdata->page_offset,
3192 .rq_npages = rdata->nr_pages,
3193 .rq_pagesz = rdata->pagesz,
3194 .rq_tailsz = rdata->tailsz };
3196 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3197 __func__, mid->mid, mid->mid_state, rdata->result,
3200 switch (mid->mid_state) {
3201 case MID_RESPONSE_RECEIVED:
3202 credits_received = le16_to_cpu(shdr->CreditRequest);
3203 /* result already set, check signature */
3204 if (server->sign && !mid->decrypted) {
3207 rc = smb2_verify_signature(&rqst, server);
3209 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
3212 /* FIXME: should this be counted toward the initiating task? */
3213 task_io_account_read(rdata->got_bytes);
3214 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3216 case MID_REQUEST_SUBMITTED:
3217 case MID_RETRY_NEEDED:
3218 rdata->result = -EAGAIN;
3219 if (server->sign && rdata->got_bytes)
3220 /* reset bytes number since we can not check a sign */
3221 rdata->got_bytes = 0;
3222 /* FIXME: should this be counted toward the initiating task? */
3223 task_io_account_read(rdata->got_bytes);
3224 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3226 case MID_RESPONSE_MALFORMED:
3227 credits_received = le16_to_cpu(shdr->CreditRequest);
3230 if (rdata->result != -ENODATA)
3231 rdata->result = -EIO;
3233 #ifdef CONFIG_CIFS_SMB_DIRECT
3235 * If this rdata has a memmory registered, the MR can be freed
3236 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3237 * because they have limited number and are used for future I/Os
3240 smbd_deregister_mr(rdata->mr);
3244 if (rdata->result && rdata->result != -ENODATA) {
3245 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
3246 trace_smb3_read_err(0 /* xid */,
3247 rdata->cfile->fid.persistent_fid,
3248 tcon->tid, tcon->ses->Suid, rdata->offset,
3249 rdata->bytes, rdata->result);
3251 trace_smb3_read_done(0 /* xid */,
3252 rdata->cfile->fid.persistent_fid,
3253 tcon->tid, tcon->ses->Suid,
3254 rdata->offset, rdata->got_bytes);
3256 queue_work(cifsiod_wq, &rdata->work);
3257 DeleteMidQEntry(mid);
3258 add_credits(server, credits_received, 0);
3261 /* smb2_async_readv - send an async read, and set up mid to handle result */
3263 smb2_async_readv(struct cifs_readdata *rdata)
3267 struct smb2_sync_hdr *shdr;
3268 struct cifs_io_parms io_parms;
3269 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3271 struct TCP_Server_Info *server;
3272 unsigned int total_len;
3274 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3275 __func__, rdata->offset, rdata->bytes);
3277 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3278 io_parms.offset = rdata->offset;
3279 io_parms.length = rdata->bytes;
3280 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3281 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3282 io_parms.pid = rdata->pid;
3284 server = io_parms.tcon->ses->server;
3286 rc = smb2_new_read_req(
3287 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
3289 if (rc == -EAGAIN && rdata->credits) {
3290 /* credits was reset by reconnect */
3292 /* reduce in_flight value since we won't send the req */
3293 spin_lock(&server->req_lock);
3294 server->in_flight--;
3295 spin_unlock(&server->req_lock);
3300 if (smb3_encryption_required(io_parms.tcon))
3301 flags |= CIFS_TRANSFORM_REQ;
3303 rdata->iov[0].iov_base = buf;
3304 rdata->iov[0].iov_len = total_len;
3306 shdr = (struct smb2_sync_hdr *)buf;
3308 if (rdata->credits) {
3309 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
3310 SMB2_MAX_BUFFER_SIZE));
3311 shdr->CreditRequest =
3312 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
3313 spin_lock(&server->req_lock);
3314 server->credits += rdata->credits -
3315 le16_to_cpu(shdr->CreditCharge);
3316 spin_unlock(&server->req_lock);
3317 wake_up(&server->request_q);
3318 rdata->credits = le16_to_cpu(shdr->CreditCharge);
3319 flags |= CIFS_HAS_CREDITS;
3322 kref_get(&rdata->refcount);
3323 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
3324 cifs_readv_receive, smb2_readv_callback,
3325 smb3_handle_read_data, rdata, flags);
3327 kref_put(&rdata->refcount, cifs_readdata_release);
3328 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
3329 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3331 io_parms.tcon->ses->Suid,
3332 io_parms.offset, io_parms.length, rc);
3335 cifs_small_buf_release(buf);
3340 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3341 unsigned int *nbytes, char **buf, int *buf_type)
3343 struct smb_rqst rqst;
3344 int resp_buftype, rc = -EACCES;
3345 struct smb2_read_plain_req *req = NULL;
3346 struct smb2_read_rsp *rsp = NULL;
3348 struct kvec rsp_iov;
3349 unsigned int total_len;
3350 int flags = CIFS_LOG_ERROR;
3351 struct cifs_ses *ses = io_parms->tcon->ses;
3354 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
3358 if (smb3_encryption_required(io_parms->tcon))
3359 flags |= CIFS_TRANSFORM_REQ;
3361 iov[0].iov_base = (char *)req;
3362 iov[0].iov_len = total_len;
3364 memset(&rqst, 0, sizeof(struct smb_rqst));
3368 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3369 cifs_small_buf_release(req);
3371 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
3374 if (rc != -ENODATA) {
3375 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3376 cifs_dbg(VFS, "Send error in read = %d\n", rc);
3377 trace_smb3_read_err(xid, req->PersistentFileId,
3378 io_parms->tcon->tid, ses->Suid,
3379 io_parms->offset, io_parms->length,
3382 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3383 return rc == -ENODATA ? 0 : rc;
3385 trace_smb3_read_done(xid, req->PersistentFileId,
3386 io_parms->tcon->tid, ses->Suid,
3387 io_parms->offset, io_parms->length);
3389 *nbytes = le32_to_cpu(rsp->DataLength);
3390 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3391 (*nbytes > io_parms->length)) {
3392 cifs_dbg(FYI, "bad length %d for count %d\n",
3393 *nbytes, io_parms->length);
3399 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
3400 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3401 } else if (resp_buftype != CIFS_NO_BUFFER) {
3402 *buf = rsp_iov.iov_base;
3403 if (resp_buftype == CIFS_SMALL_BUFFER)
3404 *buf_type = CIFS_SMALL_BUFFER;
3405 else if (resp_buftype == CIFS_LARGE_BUFFER)
3406 *buf_type = CIFS_LARGE_BUFFER;
3412 * Check the mid_state and signature on received buffer (if any), and queue the
3413 * workqueue completion task.
3416 smb2_writev_callback(struct mid_q_entry *mid)
3418 struct cifs_writedata *wdata = mid->callback_data;
3419 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3420 unsigned int written;
3421 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
3422 unsigned int credits_received = 0;
3424 switch (mid->mid_state) {
3425 case MID_RESPONSE_RECEIVED:
3426 credits_received = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3427 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
3428 if (wdata->result != 0)
3431 written = le32_to_cpu(rsp->DataLength);
3433 * Mask off high 16 bits when bytes written as returned
3434 * by the server is greater than bytes requested by the
3435 * client. OS/2 servers are known to set incorrect
3438 if (written > wdata->bytes)
3441 if (written < wdata->bytes)
3442 wdata->result = -ENOSPC;
3444 wdata->bytes = written;
3446 case MID_REQUEST_SUBMITTED:
3447 case MID_RETRY_NEEDED:
3448 wdata->result = -EAGAIN;
3450 case MID_RESPONSE_MALFORMED:
3451 credits_received = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3454 wdata->result = -EIO;
3457 #ifdef CONFIG_CIFS_SMB_DIRECT
3459 * If this wdata has a memory registered, the MR can be freed
3460 * The number of MRs available is limited, it's important to recover
3461 * used MR as soon as I/O is finished. Hold MR longer in the later
3462 * I/O process can possibly result in I/O deadlock due to lack of MR
3463 * to send request on I/O retry
3466 smbd_deregister_mr(wdata->mr);
3470 if (wdata->result) {
3471 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3472 trace_smb3_write_err(0 /* no xid */,
3473 wdata->cfile->fid.persistent_fid,
3474 tcon->tid, tcon->ses->Suid, wdata->offset,
3475 wdata->bytes, wdata->result);
3477 trace_smb3_write_done(0 /* no xid */,
3478 wdata->cfile->fid.persistent_fid,
3479 tcon->tid, tcon->ses->Suid,
3480 wdata->offset, wdata->bytes);
3482 queue_work(cifsiod_wq, &wdata->work);
3483 DeleteMidQEntry(mid);
3484 add_credits(tcon->ses->server, credits_received, 0);
3487 /* smb2_async_writev - send an async write, and set up mid to handle result */
3489 smb2_async_writev(struct cifs_writedata *wdata,
3490 void (*release)(struct kref *kref))
3492 int rc = -EACCES, flags = 0;
3493 struct smb2_write_req *req = NULL;
3494 struct smb2_sync_hdr *shdr;
3495 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3496 struct TCP_Server_Info *server = tcon->ses->server;
3498 struct smb_rqst rqst = { };
3499 unsigned int total_len;
3501 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
3503 if (rc == -EAGAIN && wdata->credits) {
3504 /* credits was reset by reconnect */
3506 /* reduce in_flight value since we won't send the req */
3507 spin_lock(&server->req_lock);
3508 server->in_flight--;
3509 spin_unlock(&server->req_lock);
3511 goto async_writev_out;
3514 if (smb3_encryption_required(tcon))
3515 flags |= CIFS_TRANSFORM_REQ;
3517 shdr = (struct smb2_sync_hdr *)req;
3518 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
3520 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3521 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3522 req->WriteChannelInfoOffset = 0;
3523 req->WriteChannelInfoLength = 0;
3525 req->Offset = cpu_to_le64(wdata->offset);
3526 req->DataOffset = cpu_to_le16(
3527 offsetof(struct smb2_write_req, Buffer));
3528 req->RemainingBytes = 0;
3529 #ifdef CONFIG_CIFS_SMB_DIRECT
3531 * If we want to do a server RDMA read, fill in and append
3532 * smbd_buffer_descriptor_v1 to the end of write request
3534 if (server->rdma && !server->sign && wdata->bytes >=
3535 server->smbd_conn->rdma_readwrite_threshold) {
3537 struct smbd_buffer_descriptor_v1 *v1;
3538 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3540 wdata->mr = smbd_register_mr(
3541 server->smbd_conn, wdata->pages,
3542 wdata->nr_pages, wdata->page_offset,
3543 wdata->tailsz, false, need_invalidate);
3546 goto async_writev_out;
3549 req->DataOffset = 0;
3550 if (wdata->nr_pages > 1)
3551 req->RemainingBytes =
3553 (wdata->nr_pages - 1) * wdata->pagesz -
3554 wdata->page_offset + wdata->tailsz
3557 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
3558 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3559 if (need_invalidate)
3560 req->Channel = SMB2_CHANNEL_RDMA_V1;
3561 req->WriteChannelInfoOffset =
3562 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
3563 req->WriteChannelInfoLength =
3564 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3565 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3566 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3567 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3568 v1->length = cpu_to_le32(wdata->mr->mr->length);
3571 iov[0].iov_len = total_len - 1;
3572 iov[0].iov_base = (char *)req;
3576 rqst.rq_pages = wdata->pages;
3577 rqst.rq_offset = wdata->page_offset;
3578 rqst.rq_npages = wdata->nr_pages;
3579 rqst.rq_pagesz = wdata->pagesz;
3580 rqst.rq_tailsz = wdata->tailsz;
3581 #ifdef CONFIG_CIFS_SMB_DIRECT
3583 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
3587 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3588 wdata->offset, wdata->bytes);
3590 #ifdef CONFIG_CIFS_SMB_DIRECT
3591 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3593 req->Length = cpu_to_le32(wdata->bytes);
3595 req->Length = cpu_to_le32(wdata->bytes);
3598 if (wdata->credits) {
3599 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
3600 SMB2_MAX_BUFFER_SIZE));
3601 shdr->CreditRequest =
3602 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
3603 spin_lock(&server->req_lock);
3604 server->credits += wdata->credits -
3605 le16_to_cpu(shdr->CreditCharge);
3606 spin_unlock(&server->req_lock);
3607 wake_up(&server->request_q);
3608 wdata->credits = le16_to_cpu(shdr->CreditCharge);
3609 flags |= CIFS_HAS_CREDITS;
3612 kref_get(&wdata->refcount);
3613 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3617 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
3618 tcon->tid, tcon->ses->Suid, wdata->offset,
3620 kref_put(&wdata->refcount, release);
3621 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3625 cifs_small_buf_release(req);
3630 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3631 * The length field from io_parms must be at least 1 and indicates a number of
3632 * elements with data to write that begins with position 1 in iov array. All
3633 * data length is specified by count.
3636 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3637 unsigned int *nbytes, struct kvec *iov, int n_vec)
3639 struct smb_rqst rqst;
3641 struct smb2_write_req *req = NULL;
3642 struct smb2_write_rsp *rsp = NULL;
3644 struct kvec rsp_iov;
3646 unsigned int total_len;
3653 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3658 if (io_parms->tcon->ses->server == NULL)
3659 return -ECONNABORTED;
3661 if (smb3_encryption_required(io_parms->tcon))
3662 flags |= CIFS_TRANSFORM_REQ;
3664 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
3666 req->PersistentFileId = io_parms->persistent_fid;
3667 req->VolatileFileId = io_parms->volatile_fid;
3668 req->WriteChannelInfoOffset = 0;
3669 req->WriteChannelInfoLength = 0;
3671 req->Length = cpu_to_le32(io_parms->length);
3672 req->Offset = cpu_to_le64(io_parms->offset);
3673 req->DataOffset = cpu_to_le16(
3674 offsetof(struct smb2_write_req, Buffer));
3675 req->RemainingBytes = 0;
3677 iov[0].iov_base = (char *)req;
3679 iov[0].iov_len = total_len - 1;
3681 memset(&rqst, 0, sizeof(struct smb_rqst));
3683 rqst.rq_nvec = n_vec + 1;
3685 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
3686 &resp_buftype, flags, &rsp_iov);
3687 cifs_small_buf_release(req);
3688 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
3691 trace_smb3_write_err(xid, req->PersistentFileId,
3692 io_parms->tcon->tid,
3693 io_parms->tcon->ses->Suid,
3694 io_parms->offset, io_parms->length, rc);
3695 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
3696 cifs_dbg(VFS, "Send error in write = %d\n", rc);
3698 *nbytes = le32_to_cpu(rsp->DataLength);
3699 trace_smb3_write_done(xid, req->PersistentFileId,
3700 io_parms->tcon->tid,
3701 io_parms->tcon->ses->Suid,
3702 io_parms->offset, *nbytes);
3705 free_rsp_buf(resp_buftype, rsp);
3710 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3713 unsigned int entrycount = 0;
3714 unsigned int next_offset = 0;
3716 FILE_DIRECTORY_INFO *dir_info;
3718 if (bufstart == NULL)
3721 entryptr = bufstart;
3724 if (entryptr + next_offset < entryptr ||
3725 entryptr + next_offset > end_of_buf ||
3726 entryptr + next_offset + size > end_of_buf) {
3727 cifs_dbg(VFS, "malformed search entry would overflow\n");
3731 entryptr = entryptr + next_offset;
3732 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
3734 len = le32_to_cpu(dir_info->FileNameLength);
3735 if (entryptr + len < entryptr ||
3736 entryptr + len > end_of_buf ||
3737 entryptr + len + size > end_of_buf) {
3738 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3743 *lastentry = entryptr;
3746 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
3758 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3759 u64 persistent_fid, u64 volatile_fid, int index,
3760 struct cifs_search_info *srch_inf)
3762 struct smb_rqst rqst;
3763 struct smb2_query_directory_req *req;
3764 struct smb2_query_directory_rsp *rsp = NULL;
3766 struct kvec rsp_iov;
3769 int resp_buftype = CIFS_NO_BUFFER;
3770 unsigned char *bufptr;
3771 struct TCP_Server_Info *server;
3772 struct cifs_ses *ses = tcon->ses;
3773 __le16 asteriks = cpu_to_le16('*');
3775 unsigned int output_size = CIFSMaxBufSize;
3776 size_t info_buf_size;
3778 unsigned int total_len;
3780 if (ses && (ses->server))
3781 server = ses->server;
3785 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3790 if (smb3_encryption_required(tcon))
3791 flags |= CIFS_TRANSFORM_REQ;
3793 switch (srch_inf->info_level) {
3794 case SMB_FIND_FILE_DIRECTORY_INFO:
3795 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3796 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3798 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3799 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3800 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3803 cifs_dbg(VFS, "info level %u isn't supported\n",
3804 srch_inf->info_level);
3809 req->FileIndex = cpu_to_le32(index);
3810 req->PersistentFileId = persistent_fid;
3811 req->VolatileFileId = volatile_fid;
3814 bufptr = req->Buffer;
3815 memcpy(bufptr, &asteriks, len);
3817 req->FileNameOffset =
3818 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
3819 req->FileNameLength = cpu_to_le16(len);
3821 * BB could be 30 bytes or so longer if we used SMB2 specific
3822 * buffer lengths, but this is safe and close enough.
3824 output_size = min_t(unsigned int, output_size, server->maxBuf);
3825 output_size = min_t(unsigned int, output_size, 2 << 15);
3826 req->OutputBufferLength = cpu_to_le32(output_size);
3828 iov[0].iov_base = (char *)req;
3830 iov[0].iov_len = total_len - 1;
3832 iov[1].iov_base = (char *)(req->Buffer);
3833 iov[1].iov_len = len;
3835 memset(&rqst, 0, sizeof(struct smb_rqst));
3839 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3840 cifs_small_buf_release(req);
3841 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
3844 if (rc == -ENODATA &&
3845 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
3846 srch_inf->endOfSearch = true;
3849 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3853 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
3854 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
3859 srch_inf->unicode = true;
3861 if (srch_inf->ntwrk_buf_start) {
3862 if (srch_inf->smallBuf)
3863 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3865 cifs_buf_release(srch_inf->ntwrk_buf_start);
3867 srch_inf->ntwrk_buf_start = (char *)rsp;
3868 srch_inf->srch_entries_start = srch_inf->last_entry =
3869 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
3870 end_of_smb = rsp_iov.iov_len + (char *)rsp;
3871 srch_inf->entries_in_buffer =
3872 num_entries(srch_inf->srch_entries_start, end_of_smb,
3873 &srch_inf->last_entry, info_buf_size);
3874 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
3875 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3876 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3877 srch_inf->srch_entries_start, srch_inf->last_entry);
3878 if (resp_buftype == CIFS_LARGE_BUFFER)
3879 srch_inf->smallBuf = false;
3880 else if (resp_buftype == CIFS_SMALL_BUFFER)
3881 srch_inf->smallBuf = true;
3883 cifs_dbg(VFS, "illegal search buffer type\n");
3888 free_rsp_buf(resp_buftype, rsp);
3893 SMB2_set_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
3894 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3895 u8 info_type, u32 additional_info,
3896 void **data, unsigned int *size)
3898 struct smb2_set_info_req *req;
3899 struct kvec *iov = rqst->rq_iov;
3900 unsigned int i, total_len;
3903 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
3907 req->sync_hdr.ProcessId = cpu_to_le32(pid);
3908 req->InfoType = info_type;
3909 req->FileInfoClass = info_class;
3910 req->PersistentFileId = persistent_fid;
3911 req->VolatileFileId = volatile_fid;
3912 req->AdditionalInformation = cpu_to_le32(additional_info);
3915 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
3916 req->BufferLength = cpu_to_le32(*size);
3918 memcpy(req->Buffer, *data, *size);
3921 iov[0].iov_base = (char *)req;
3923 iov[0].iov_len = total_len - 1;
3925 for (i = 1; i < rqst->rq_nvec; i++) {
3926 le32_add_cpu(&req->BufferLength, size[i]);
3927 iov[i].iov_base = (char *)data[i];
3928 iov[i].iov_len = size[i];
3935 SMB2_set_info_free(struct smb_rqst *rqst)
3937 if (rqst && rqst->rq_iov)
3938 cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
3942 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3943 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3944 u8 info_type, u32 additional_info, unsigned int num,
3945 void **data, unsigned int *size)
3947 struct smb_rqst rqst;
3948 struct smb2_set_info_rsp *rsp = NULL;
3950 struct kvec rsp_iov;
3953 struct cifs_ses *ses = tcon->ses;
3956 if (!ses || !(ses->server))
3962 if (smb3_encryption_required(tcon))
3963 flags |= CIFS_TRANSFORM_REQ;
3965 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
3969 memset(&rqst, 0, sizeof(struct smb_rqst));
3973 rc = SMB2_set_info_init(tcon, &rqst, persistent_fid, volatile_fid, pid,
3974 info_class, info_type, additional_info,
3982 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
3984 SMB2_set_info_free(&rqst);
3985 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
3988 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
3989 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
3990 ses->Suid, info_class, (__u32)info_type, rc);
3993 free_rsp_buf(resp_buftype, rsp);
3999 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4000 u64 volatile_fid, u32 pid, __le64 *eof)
4002 struct smb2_file_eof_info info;
4006 info.EndOfFile = *eof;
4009 size = sizeof(struct smb2_file_eof_info);
4011 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4012 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4013 0, 1, &data, &size);
4017 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4018 u64 persistent_fid, u64 volatile_fid,
4019 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4021 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4022 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4023 1, (void **)&pnntsd, &pacllen);
4027 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4028 u64 persistent_fid, u64 volatile_fid,
4029 struct smb2_file_full_ea_info *buf, int len)
4031 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4032 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4033 0, 1, (void **)&buf, &len);
4037 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4038 const u64 persistent_fid, const u64 volatile_fid,
4041 struct smb_rqst rqst;
4043 struct smb2_oplock_break *req = NULL;
4044 struct cifs_ses *ses = tcon->ses;
4045 int flags = CIFS_OBREAK_OP;
4046 unsigned int total_len;
4048 struct kvec rsp_iov;
4051 cifs_dbg(FYI, "SMB2_oplock_break\n");
4052 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4057 if (smb3_encryption_required(tcon))
4058 flags |= CIFS_TRANSFORM_REQ;
4060 req->VolatileFid = volatile_fid;
4061 req->PersistentFid = persistent_fid;
4062 req->OplockLevel = oplock_level;
4063 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4065 flags |= CIFS_NO_RESP;
4067 iov[0].iov_base = (char *)req;
4068 iov[0].iov_len = total_len;
4070 memset(&rqst, 0, sizeof(struct smb_rqst));
4074 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4075 cifs_small_buf_release(req);
4078 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4079 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
4086 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4087 struct kstatfs *kst)
4089 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4090 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4091 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
4092 kst->f_bfree = kst->f_bavail =
4093 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
4098 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4099 struct kstatfs *kst)
4101 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4102 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4103 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4104 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4105 kst->f_bavail = kst->f_bfree;
4107 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4108 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4109 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4110 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4111 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4117 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4118 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4121 struct smb2_query_info_req *req;
4122 unsigned int total_len;
4124 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
4126 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4129 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4134 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4135 req->FileInfoClass = level;
4136 req->PersistentFileId = persistent_fid;
4137 req->VolatileFileId = volatile_fid;
4139 req->InputBufferOffset =
4140 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
4141 req->OutputBufferLength = cpu_to_le32(
4142 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
4144 iov->iov_base = (char *)req;
4145 iov->iov_len = total_len;
4150 SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4151 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4153 struct smb_rqst rqst;
4154 struct smb2_query_info_rsp *rsp = NULL;
4156 struct kvec rsp_iov;
4159 struct cifs_ses *ses = tcon->ses;
4160 FILE_SYSTEM_POSIX_INFO *info = NULL;
4163 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4164 sizeof(FILE_SYSTEM_POSIX_INFO),
4165 persistent_fid, volatile_fid);
4169 if (smb3_encryption_required(tcon))
4170 flags |= CIFS_TRANSFORM_REQ;
4172 memset(&rqst, 0, sizeof(struct smb_rqst));
4176 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4177 cifs_small_buf_release(iov.iov_base);
4179 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4180 goto posix_qfsinf_exit;
4182 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4184 info = (FILE_SYSTEM_POSIX_INFO *)(
4185 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4186 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4187 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4188 sizeof(FILE_SYSTEM_POSIX_INFO));
4190 copy_posix_fs_info_to_kstatfs(info, fsdata);
4193 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4198 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4199 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4201 struct smb_rqst rqst;
4202 struct smb2_query_info_rsp *rsp = NULL;
4204 struct kvec rsp_iov;
4207 struct cifs_ses *ses = tcon->ses;
4208 struct smb2_fs_full_size_info *info = NULL;
4211 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4212 sizeof(struct smb2_fs_full_size_info),
4213 persistent_fid, volatile_fid);
4217 if (smb3_encryption_required(tcon))
4218 flags |= CIFS_TRANSFORM_REQ;
4220 memset(&rqst, 0, sizeof(struct smb_rqst));
4224 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4225 cifs_small_buf_release(iov.iov_base);
4227 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4230 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4232 info = (struct smb2_fs_full_size_info *)(
4233 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4234 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4235 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4236 sizeof(struct smb2_fs_full_size_info));
4238 smb2_copy_fs_info_to_kstatfs(info, fsdata);
4241 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4246 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
4247 u64 persistent_fid, u64 volatile_fid, int level)
4249 struct smb_rqst rqst;
4250 struct smb2_query_info_rsp *rsp = NULL;
4252 struct kvec rsp_iov;
4254 int resp_buftype, max_len, min_len;
4255 struct cifs_ses *ses = tcon->ses;
4256 unsigned int rsp_len, offset;
4259 if (level == FS_DEVICE_INFORMATION) {
4260 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4261 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4262 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4263 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4264 min_len = MIN_FS_ATTR_INFO_SIZE;
4265 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4266 max_len = sizeof(struct smb3_fs_ss_info);
4267 min_len = sizeof(struct smb3_fs_ss_info);
4268 } else if (level == FS_VOLUME_INFORMATION) {
4269 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4270 min_len = sizeof(struct smb3_fs_vol_info);
4272 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
4276 rc = build_qfs_info_req(&iov, tcon, level, max_len,
4277 persistent_fid, volatile_fid);
4281 if (smb3_encryption_required(tcon))
4282 flags |= CIFS_TRANSFORM_REQ;
4284 memset(&rqst, 0, sizeof(struct smb_rqst));
4288 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4289 cifs_small_buf_release(iov.iov_base);
4291 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4294 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4296 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4297 offset = le16_to_cpu(rsp->OutputBufferOffset);
4298 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
4302 if (level == FS_ATTRIBUTE_INFORMATION)
4303 memcpy(&tcon->fsAttrInfo, offset
4304 + (char *)rsp, min_t(unsigned int,
4306 else if (level == FS_DEVICE_INFORMATION)
4307 memcpy(&tcon->fsDevInfo, offset
4308 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
4309 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4310 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
4311 (offset + (char *)rsp);
4312 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4313 tcon->perf_sector_size =
4314 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
4315 } else if (level == FS_VOLUME_INFORMATION) {
4316 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4317 (offset + (char *)rsp);
4318 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4319 tcon->vol_create_time = vol_info->VolumeCreationTime;
4323 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4328 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4329 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4330 const __u32 num_lock, struct smb2_lock_element *buf)
4332 struct smb_rqst rqst;
4334 struct smb2_lock_req *req = NULL;
4336 struct kvec rsp_iov;
4339 int flags = CIFS_NO_RESP;
4340 unsigned int total_len;
4342 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
4344 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
4348 if (smb3_encryption_required(tcon))
4349 flags |= CIFS_TRANSFORM_REQ;
4351 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4352 req->LockCount = cpu_to_le16(num_lock);
4354 req->PersistentFileId = persist_fid;
4355 req->VolatileFileId = volatile_fid;
4357 count = num_lock * sizeof(struct smb2_lock_element);
4359 iov[0].iov_base = (char *)req;
4360 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
4361 iov[1].iov_base = (char *)buf;
4362 iov[1].iov_len = count;
4364 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
4366 memset(&rqst, 0, sizeof(struct smb_rqst));
4370 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
4372 cifs_small_buf_release(req);
4374 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
4375 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
4376 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4377 tcon->ses->Suid, rc);
4384 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4385 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4386 const __u64 length, const __u64 offset, const __u32 lock_flags,
4389 struct smb2_lock_element lock;
4391 lock.Offset = cpu_to_le64(offset);
4392 lock.Length = cpu_to_le64(length);
4393 lock.Flags = cpu_to_le32(lock_flags);
4394 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
4395 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
4397 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
4401 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
4402 __u8 *lease_key, const __le32 lease_state)
4404 struct smb_rqst rqst;
4406 struct smb2_lease_ack *req = NULL;
4407 struct cifs_ses *ses = tcon->ses;
4408 int flags = CIFS_OBREAK_OP;
4409 unsigned int total_len;
4411 struct kvec rsp_iov;
4413 __u64 *please_key_high;
4414 __u64 *please_key_low;
4416 cifs_dbg(FYI, "SMB2_lease_break\n");
4417 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4422 if (smb3_encryption_required(tcon))
4423 flags |= CIFS_TRANSFORM_REQ;
4425 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4426 req->StructureSize = cpu_to_le16(36);
4429 memcpy(req->LeaseKey, lease_key, 16);
4430 req->LeaseState = lease_state;
4432 flags |= CIFS_NO_RESP;
4434 iov[0].iov_base = (char *)req;
4435 iov[0].iov_len = total_len;
4437 memset(&rqst, 0, sizeof(struct smb_rqst));
4441 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4442 cifs_small_buf_release(req);
4444 please_key_low = (__u64 *)lease_key;
4445 please_key_high = (__u64 *)(lease_key+8);
4447 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4448 trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
4449 ses->Suid, *please_key_low, *please_key_high, rc);
4450 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
4452 trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
4453 ses->Suid, *please_key_low, *please_key_high);