4 * Copyright (C) International Business Machines Corp., 2009, 2013
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
9 * Contains the routines for constructing the SMB2 PDUs themselves
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
32 #include <linux/kernel.h>
33 #include <linux/vfs.h>
34 #include <linux/task_io_accounting_ops.h>
35 #include <linux/uaccess.h>
36 #include <linux/uuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/xattr.h>
42 #include "cifsproto.h"
43 #include "smb2proto.h"
44 #include "cifs_unicode.h"
45 #include "cifs_debug.h"
47 #include "smb2status.h"
50 #include "cifs_spnego.h"
51 #include "smbdirect.h"
54 * The following table defines the expected "StructureSize" of SMB2 requests
55 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
57 * Note that commands are defined in smb2pdu.h in le16 but the array below is
58 * indexed by command in host byte order.
60 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
61 /* SMB2_NEGOTIATE */ 36,
62 /* SMB2_SESSION_SETUP */ 25,
64 /* SMB2_TREE_CONNECT */ 9,
65 /* SMB2_TREE_DISCONNECT */ 4,
75 /* SMB2_QUERY_DIRECTORY */ 33,
76 /* SMB2_CHANGE_NOTIFY */ 32,
77 /* SMB2_QUERY_INFO */ 41,
78 /* SMB2_SET_INFO */ 33,
79 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
82 static int encryption_required(const struct cifs_tcon *tcon)
86 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
87 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
90 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
96 smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
97 const struct cifs_tcon *tcon)
99 shdr->ProtocolId = SMB2_PROTO_NUMBER;
100 shdr->StructureSize = cpu_to_le16(64);
101 shdr->Command = smb2_cmd;
102 if (tcon && tcon->ses && tcon->ses->server) {
103 struct TCP_Server_Info *server = tcon->ses->server;
105 spin_lock(&server->req_lock);
106 /* Request up to 2 credits but don't go over the limit. */
107 if (server->credits >= server->max_credits)
108 shdr->CreditRequest = cpu_to_le16(0);
110 shdr->CreditRequest = cpu_to_le16(
111 min_t(int, server->max_credits -
112 server->credits, 2));
113 spin_unlock(&server->req_lock);
115 shdr->CreditRequest = cpu_to_le16(2);
117 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
122 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
123 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
124 if ((tcon->ses) && (tcon->ses->server) &&
125 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
126 shdr->CreditCharge = cpu_to_le16(1);
127 /* else CreditCharge MBZ */
129 shdr->TreeId = tcon->tid;
130 /* Uid is not converted */
132 shdr->SessionId = tcon->ses->Suid;
135 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
136 * to pass the path on the Open SMB prefixed by \\server\share.
137 * Not sure when we would need to do the augmented path (if ever) and
138 * setting this flag breaks the SMB2 open operation since it is
139 * illegal to send an empty path name (without \\server\share prefix)
140 * when the DFS flag is set in the SMB open header. We could
141 * consider setting the flag on all operations other than open
142 * but it is safer to net set it for now.
144 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
145 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
147 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
148 !encryption_required(tcon))
149 shdr->Flags |= SMB2_FLAGS_SIGNED;
155 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
158 struct nls_table *nls_codepage;
159 struct cifs_ses *ses;
160 struct TCP_Server_Info *server;
163 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
164 * check for tcp and smb session status done differently
165 * for those three - in the calling routine.
170 if (smb2_command == SMB2_TREE_CONNECT)
173 if (tcon->tidStatus == CifsExiting) {
175 * only tree disconnect, open, and write,
176 * (and ulogoff which does not have tcon)
177 * are allowed as we start force umount.
179 if ((smb2_command != SMB2_WRITE) &&
180 (smb2_command != SMB2_CREATE) &&
181 (smb2_command != SMB2_TREE_DISCONNECT)) {
182 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
187 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
188 (!tcon->ses->server))
192 server = ses->server;
195 * Give demultiplex thread up to 10 seconds to reconnect, should be
196 * greater than cifs socket timeout which is 7 seconds
198 while (server->tcpStatus == CifsNeedReconnect) {
200 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
201 * here since they are implicitly done when session drops.
203 switch (smb2_command) {
205 * BB Should we keep oplock break and add flush to exceptions?
207 case SMB2_TREE_DISCONNECT:
210 case SMB2_OPLOCK_BREAK:
214 wait_event_interruptible_timeout(server->response_q,
215 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
217 /* are we still trying to reconnect? */
218 if (server->tcpStatus != CifsNeedReconnect)
222 * on "soft" mounts we wait once. Hard mounts keep
223 * retrying until process is killed or server comes
227 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
232 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
235 nls_codepage = load_nls_default();
238 * need to prevent multiple threads trying to simultaneously reconnect
239 * the same SMB session
241 mutex_lock(&tcon->ses->session_mutex);
244 * Recheck after acquire mutex. If another thread is negotiating
245 * and the server never sends an answer the socket will be closed
246 * and tcpStatus set to reconnect.
248 if (server->tcpStatus == CifsNeedReconnect) {
250 mutex_unlock(&tcon->ses->session_mutex);
254 rc = cifs_negotiate_protocol(0, tcon->ses);
255 if (!rc && tcon->ses->need_reconnect)
256 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
258 if (rc || !tcon->need_reconnect) {
259 mutex_unlock(&tcon->ses->session_mutex);
263 cifs_mark_open_files_invalid(tcon);
264 if (tcon->use_persistent)
265 tcon->need_reopen_files = true;
267 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
268 mutex_unlock(&tcon->ses->session_mutex);
270 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
272 /* If sess reconnected but tcon didn't, something strange ... */
273 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
277 if (smb2_command != SMB2_INTERNAL_CMD)
278 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
280 atomic_inc(&tconInfoReconnectCount);
283 * Check if handle based operation so we know whether we can continue
284 * or not without returning to caller to reset file handle.
287 * BB Is flush done by server on drop of tcp session? Should we special
288 * case it and skip above?
290 switch (smb2_command) {
296 case SMB2_QUERY_DIRECTORY:
297 case SMB2_CHANGE_NOTIFY:
298 case SMB2_QUERY_INFO:
302 unload_nls(nls_codepage);
307 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
308 unsigned int *total_len)
310 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
311 /* lookup word count ie StructureSize from table */
312 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
315 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
316 * largest operations (Create)
320 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
321 spdu->StructureSize2 = cpu_to_le16(parmsize);
323 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
327 * Allocate and return pointer to an SMB request hdr, and set basic
328 * SMB information in the SMB header. If the return code is zero, this
329 * function must have filled in request_buf pointer.
332 smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
333 void **request_buf, unsigned int *total_len)
337 rc = smb2_reconnect(smb2_command, tcon);
341 /* BB eventually switch this to SMB2 specific small buf size */
342 *request_buf = cifs_small_buf_get();
343 if (*request_buf == NULL) {
344 /* BB should we add a retry in here if not a writepage? */
348 fill_small_buf(smb2_command, tcon,
349 (struct smb2_sync_hdr *)(*request_buf),
353 #ifdef CONFIG_CIFS_STATS2
354 uint16_t com_code = le16_to_cpu(smb2_command);
355 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
357 cifs_stats_inc(&tcon->num_smbs_sent);
363 #ifdef CONFIG_CIFS_SMB311
364 /* offset is sizeof smb2_negotiate_req but rounded up to 8 bytes */
365 #define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) */
368 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
369 #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
372 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
374 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
375 pneg_ctxt->DataLength = cpu_to_le16(38);
376 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
377 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
378 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
379 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
383 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
385 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
386 pneg_ctxt->DataLength = cpu_to_le16(6);
387 pneg_ctxt->CipherCount = cpu_to_le16(2);
388 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
389 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
393 assemble_neg_contexts(struct smb2_negotiate_req *req,
394 unsigned int *total_len)
396 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT;
398 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
399 /* Add 2 to size to round to 8 byte boundary */
401 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
402 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
403 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
404 req->NegotiateContextCount = cpu_to_le16(2);
406 *total_len += 4 + sizeof(struct smb2_preauth_neg_context)
407 + sizeof(struct smb2_encryption_neg_context);
410 static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
412 unsigned int len = le16_to_cpu(ctxt->DataLength);
414 /* If invalid preauth context warn but use what we requested, SHA-512 */
415 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
416 printk_once(KERN_WARNING "server sent bad preauth context\n");
419 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
420 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
421 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
422 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
425 static int decode_encrypt_ctx(struct TCP_Server_Info *server,
426 struct smb2_encryption_neg_context *ctxt)
428 unsigned int len = le16_to_cpu(ctxt->DataLength);
430 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
431 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
432 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
436 if (le16_to_cpu(ctxt->CipherCount) != 1) {
437 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
440 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
441 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
442 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
443 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
446 server->cipher_type = ctxt->Ciphers[0];
450 static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
451 struct TCP_Server_Info *server)
453 struct smb2_neg_context *pctx;
454 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
455 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
456 unsigned int len_of_smb = be32_to_cpu(rsp->hdr.smb2_buf_length);
457 unsigned int len_of_ctxts, i;
460 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
461 if (len_of_smb <= offset) {
462 cifs_dbg(VFS, "Invalid response: negotiate context offset\n");
466 len_of_ctxts = len_of_smb - offset;
468 for (i = 0; i < ctxt_cnt; i++) {
470 /* check that offset is not beyond end of SMB */
471 if (len_of_ctxts == 0)
474 if (len_of_ctxts < sizeof(struct smb2_neg_context))
477 pctx = (struct smb2_neg_context *)(offset + 4 + (char *)rsp);
478 clen = le16_to_cpu(pctx->DataLength);
479 if (clen > len_of_ctxts)
482 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
483 decode_preauth_context(
484 (struct smb2_preauth_neg_context *)pctx);
485 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
486 rc = decode_encrypt_ctx(server,
487 (struct smb2_encryption_neg_context *)pctx);
489 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
490 le16_to_cpu(pctx->ContextType));
494 /* offsets must be 8 byte aligned */
495 clen = (clen + 7) & ~0x7;
496 offset += clen + sizeof(struct smb2_neg_context);
497 len_of_ctxts -= clen;
503 static void assemble_neg_contexts(struct smb2_negotiate_req *req,
504 unsigned int *total_len)
512 * SMB2 Worker functions follow:
514 * The general structure of the worker functions is:
515 * 1) Call smb2_init (assembles SMB2 header)
516 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
517 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
518 * 4) Decode SMB2 command specific fields in the fixed length area
519 * 5) Decode variable length data area (if any for this SMB2 command type)
520 * 6) Call free smb buffer
526 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
528 struct smb2_negotiate_req *req;
529 struct smb2_negotiate_rsp *rsp;
534 struct TCP_Server_Info *server = ses->server;
535 int blob_offset, blob_length;
537 int flags = CIFS_NEG_OP;
538 unsigned int total_len;
540 cifs_dbg(FYI, "Negotiate protocol\n");
543 WARN(1, "%s: server is NULL!\n", __func__);
547 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
551 req->sync_hdr.SessionId = 0;
552 #ifdef CONFIG_CIFS_SMB311
553 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
554 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
557 if (strcmp(ses->server->vals->version_string,
558 SMB3ANY_VERSION_STRING) == 0) {
559 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
560 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
561 req->DialectCount = cpu_to_le16(2);
563 } else if (strcmp(ses->server->vals->version_string,
564 SMBDEFAULT_VERSION_STRING) == 0) {
565 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
566 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
567 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
568 req->DialectCount = cpu_to_le16(3);
571 /* otherwise send specific dialect */
572 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
573 req->DialectCount = cpu_to_le16(1);
577 /* only one of SMB2 signing flags may be set in SMB2 request */
579 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
580 else if (global_secflags & CIFSSEC_MAY_SIGN)
581 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
583 req->SecurityMode = 0;
585 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
587 /* ClientGUID must be zero for SMB2.02 dialect */
588 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
589 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
591 memcpy(req->ClientGUID, server->client_guid,
592 SMB2_CLIENT_GUID_SIZE);
593 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
594 assemble_neg_contexts(req, &total_len);
596 iov[0].iov_base = (char *)req;
597 iov[0].iov_len = total_len;
599 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
600 cifs_small_buf_release(req);
601 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
603 * No tcon so can't do
604 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
606 if (rc == -EOPNOTSUPP) {
607 cifs_dbg(VFS, "Dialect not supported by server. Consider "
608 "specifying vers=1.0 or vers=2.0 on mount for accessing"
614 if (strcmp(ses->server->vals->version_string,
615 SMB3ANY_VERSION_STRING) == 0) {
616 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
618 "SMB2 dialect returned but not requested\n");
620 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
622 "SMB2.1 dialect returned but not requested\n");
625 } else if (strcmp(ses->server->vals->version_string,
626 SMBDEFAULT_VERSION_STRING) == 0) {
627 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
629 "SMB2 dialect returned but not requested\n");
631 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
632 /* ops set to 3.0 by default for default so update */
633 ses->server->ops = &smb21_operations;
635 } else if (le16_to_cpu(rsp->DialectRevision) !=
636 ses->server->vals->protocol_id) {
637 /* if requested single dialect ensure returned dialect matched */
638 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
639 le16_to_cpu(rsp->DialectRevision));
643 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
645 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
646 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
647 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
648 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
649 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
650 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
651 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
652 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
653 #ifdef CONFIG_CIFS_SMB311
654 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
655 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
658 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
659 le16_to_cpu(rsp->DialectRevision));
663 server->dialect = le16_to_cpu(rsp->DialectRevision);
665 /* BB: add check that dialect was valid given dialect(s) we asked for */
667 #ifdef CONFIG_CIFS_SMB311
669 * Keep a copy of the hash after negprot. This hash will be
670 * the starting hash value for all sessions made from this
673 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
674 SMB2_PREAUTH_HASH_SIZE);
676 /* SMB2 only has an extended negflavor */
677 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
678 /* set it to the maximum buffer size value we can send with 1 credit */
679 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
680 SMB2_MAX_BUFFER_SIZE);
681 server->max_read = le32_to_cpu(rsp->MaxReadSize);
682 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
683 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
684 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
685 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
687 server->capabilities = le32_to_cpu(rsp->Capabilities);
689 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
691 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
694 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
696 * ses->sectype = RawNTLMSSP;
697 * but for time being this is our only auth choice so doesn't matter.
698 * We just found a server which sets blob length to zero expecting raw.
700 if (blob_length == 0) {
701 cifs_dbg(FYI, "missing security blob on negprot\n");
702 server->sec_ntlmssp = true;
705 rc = cifs_enable_signing(server, ses->sign);
709 rc = decode_negTokenInit(security_blob, blob_length, server);
716 #ifdef CONFIG_CIFS_SMB311
717 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
718 if (rsp->NegotiateContextCount)
719 rc = smb311_decode_neg_context(rsp, server);
721 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
723 #endif /* CONFIG_CIFS_SMB311 */
725 free_rsp_buf(resp_buftype, rsp);
729 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
732 struct validate_negotiate_info_req vneg_inbuf;
733 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
735 u32 inbuflen; /* max of 4 dialects */
737 cifs_dbg(FYI, "validate negotiate\n");
739 #ifdef CONFIG_CIFS_SMB_DIRECT
740 if (tcon->ses->server->rdma)
744 /* In SMB3.11 preauth integrity supersedes validate negotiate */
745 if (tcon->ses->server->dialect == SMB311_PROT_ID)
749 * validation ioctl must be signed, so no point sending this if we
750 * can not sign it (ie are not known user). Even if signing is not
751 * required (enabled but not negotiated), in those cases we selectively
752 * sign just this, the first and only signed request on a connection.
753 * Having validation of negotiate info helps reduce attack vectors.
755 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
756 return 0; /* validation requires signing */
758 if (tcon->ses->user_name == NULL) {
759 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
760 return 0; /* validation requires signing */
763 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
764 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
766 vneg_inbuf.Capabilities =
767 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
768 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
769 SMB2_CLIENT_GUID_SIZE);
772 vneg_inbuf.SecurityMode =
773 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
774 else if (global_secflags & CIFSSEC_MAY_SIGN)
775 vneg_inbuf.SecurityMode =
776 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
778 vneg_inbuf.SecurityMode = 0;
781 if (strcmp(tcon->ses->server->vals->version_string,
782 SMB3ANY_VERSION_STRING) == 0) {
783 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
784 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
785 vneg_inbuf.DialectCount = cpu_to_le16(2);
786 /* structure is big enough for 3 dialects, sending only 2 */
787 inbuflen = sizeof(struct validate_negotiate_info_req) - 2;
788 } else if (strcmp(tcon->ses->server->vals->version_string,
789 SMBDEFAULT_VERSION_STRING) == 0) {
790 vneg_inbuf.Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
791 vneg_inbuf.Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
792 vneg_inbuf.Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
793 vneg_inbuf.DialectCount = cpu_to_le16(3);
794 /* structure is big enough for 3 dialects */
795 inbuflen = sizeof(struct validate_negotiate_info_req);
797 /* otherwise specific dialect was requested */
798 vneg_inbuf.Dialects[0] =
799 cpu_to_le16(tcon->ses->server->vals->protocol_id);
800 vneg_inbuf.DialectCount = cpu_to_le16(1);
801 /* structure is big enough for 3 dialects, sending only 1 */
802 inbuflen = sizeof(struct validate_negotiate_info_req) - 4;
805 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
806 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
807 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
808 (char **)&pneg_rsp, &rsplen);
811 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
815 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
816 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
819 /* relax check since Mac returns max bufsize allowed on ioctl */
820 if ((rsplen > CIFSMaxBufSize)
821 || (rsplen < sizeof(struct validate_negotiate_info_rsp)))
825 /* check validate negotiate info response matches what we got earlier */
826 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
829 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
832 /* do not validate server guid because not saved at negprot time yet */
834 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
835 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
838 /* validate negotiate successful */
839 cifs_dbg(FYI, "validate negotiate info successful\n");
844 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
851 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
860 if (server->sec_ntlmssp &&
861 (global_secflags & CIFSSEC_MAY_NTLMSSP))
863 if ((server->sec_kerberos || server->sec_mskerberos) &&
864 (global_secflags & CIFSSEC_MAY_KRB5))
872 struct SMB2_sess_data {
874 struct cifs_ses *ses;
875 struct nls_table *nls_cp;
876 void (*func)(struct SMB2_sess_data *);
878 u64 previous_session;
880 /* we will send the SMB in three pieces:
881 * a fixed length beginning part, an optional
882 * SPNEGO blob (which can be zero length), and a
883 * last part which will include the strings
884 * and rest of bcc area. This allows us to avoid
885 * a large buffer 17K allocation
892 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
895 struct cifs_ses *ses = sess_data->ses;
896 struct smb2_sess_setup_req *req;
897 struct TCP_Server_Info *server = ses->server;
898 unsigned int total_len;
900 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
905 /* First session, not a reauthenticate */
906 req->sync_hdr.SessionId = 0;
908 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
909 req->PreviousSessionId = sess_data->previous_session;
911 req->Flags = 0; /* MBZ */
912 /* to enable echos and oplocks */
913 req->sync_hdr.CreditRequest = cpu_to_le16(3);
915 /* only one of SMB2 signing flags may be set in SMB2 request */
917 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
918 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
919 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
921 req->SecurityMode = 0;
923 req->Capabilities = 0;
924 req->Channel = 0; /* MBZ */
926 sess_data->iov[0].iov_base = (char *)req;
928 sess_data->iov[0].iov_len = total_len - 1;
930 * This variable will be used to clear the buffer
931 * allocated above in case of any error in the calling function.
933 sess_data->buf0_type = CIFS_SMALL_BUFFER;
939 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
941 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
942 sess_data->buf0_type = CIFS_NO_BUFFER;
946 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
949 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
950 struct kvec rsp_iov = { NULL, 0 };
952 /* Testing shows that buffer offset must be at location of Buffer[0] */
953 req->SecurityBufferOffset =
954 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
955 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
957 /* BB add code to build os and lm fields */
959 rc = smb2_send_recv(sess_data->xid, sess_data->ses,
961 &sess_data->buf0_type,
962 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
963 cifs_small_buf_release(sess_data->iov[0].iov_base);
964 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
970 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
973 struct cifs_ses *ses = sess_data->ses;
975 mutex_lock(&ses->server->srv_mutex);
976 if (ses->server->ops->generate_signingkey) {
977 rc = ses->server->ops->generate_signingkey(ses);
980 "SMB3 session key generation failed\n");
981 mutex_unlock(&ses->server->srv_mutex);
985 if (!ses->server->session_estab) {
986 ses->server->sequence_number = 0x2;
987 ses->server->session_estab = true;
989 mutex_unlock(&ses->server->srv_mutex);
991 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
992 spin_lock(&GlobalMid_Lock);
993 ses->status = CifsGood;
994 ses->need_reconnect = false;
995 spin_unlock(&GlobalMid_Lock);
999 #ifdef CONFIG_CIFS_UPCALL
1001 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1004 struct cifs_ses *ses = sess_data->ses;
1005 struct cifs_spnego_msg *msg;
1006 struct key *spnego_key = NULL;
1007 struct smb2_sess_setup_rsp *rsp = NULL;
1009 rc = SMB2_sess_alloc_buffer(sess_data);
1013 spnego_key = cifs_get_spnego_key(ses);
1014 if (IS_ERR(spnego_key)) {
1015 rc = PTR_ERR(spnego_key);
1020 msg = spnego_key->payload.data[0];
1022 * check version field to make sure that cifs.upcall is
1023 * sending us a response in an expected form
1025 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1027 "bad cifs.upcall version. Expected %d got %d",
1028 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1030 goto out_put_spnego_key;
1033 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1035 if (!ses->auth_key.response) {
1037 "Kerberos can't allocate (%u bytes) memory",
1040 goto out_put_spnego_key;
1042 ses->auth_key.len = msg->sesskey_len;
1044 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1045 sess_data->iov[1].iov_len = msg->secblob_len;
1047 rc = SMB2_sess_sendreceive(sess_data);
1049 goto out_put_spnego_key;
1051 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1052 ses->Suid = rsp->hdr.sync_hdr.SessionId;
1054 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1056 rc = SMB2_sess_establish_session(sess_data);
1058 key_invalidate(spnego_key);
1059 key_put(spnego_key);
1061 sess_data->result = rc;
1062 sess_data->func = NULL;
1063 SMB2_sess_free_buffer(sess_data);
1067 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1069 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1070 sess_data->result = -EOPNOTSUPP;
1071 sess_data->func = NULL;
1076 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1079 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1082 struct cifs_ses *ses = sess_data->ses;
1083 struct smb2_sess_setup_rsp *rsp = NULL;
1084 char *ntlmssp_blob = NULL;
1085 bool use_spnego = false; /* else use raw ntlmssp */
1086 u16 blob_length = 0;
1089 * If memory allocation is successful, caller of this function
1092 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1093 if (!ses->ntlmssp) {
1097 ses->ntlmssp->sesskey_per_smbsess = true;
1099 rc = SMB2_sess_alloc_buffer(sess_data);
1103 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1105 if (ntlmssp_blob == NULL) {
1110 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1112 /* BB eventually need to add this */
1113 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1117 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1118 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1120 sess_data->iov[1].iov_base = ntlmssp_blob;
1121 sess_data->iov[1].iov_len = blob_length;
1123 rc = SMB2_sess_sendreceive(sess_data);
1124 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1126 /* If true, rc here is expected and not an error */
1127 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1128 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1134 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
1135 le16_to_cpu(rsp->SecurityBufferOffset)) {
1136 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1137 le16_to_cpu(rsp->SecurityBufferOffset));
1141 rc = decode_ntlmssp_challenge(rsp->Buffer,
1142 le16_to_cpu(rsp->SecurityBufferLength), ses);
1146 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1149 ses->Suid = rsp->hdr.sync_hdr.SessionId;
1150 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1153 kfree(ntlmssp_blob);
1154 SMB2_sess_free_buffer(sess_data);
1156 sess_data->result = 0;
1157 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1161 kfree(ses->ntlmssp);
1162 ses->ntlmssp = NULL;
1163 sess_data->result = rc;
1164 sess_data->func = NULL;
1168 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1171 struct cifs_ses *ses = sess_data->ses;
1172 struct smb2_sess_setup_req *req;
1173 struct smb2_sess_setup_rsp *rsp = NULL;
1174 unsigned char *ntlmssp_blob = NULL;
1175 bool use_spnego = false; /* else use raw ntlmssp */
1176 u16 blob_length = 0;
1178 rc = SMB2_sess_alloc_buffer(sess_data);
1182 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1183 req->sync_hdr.SessionId = ses->Suid;
1185 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1188 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1193 /* BB eventually need to add this */
1194 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1198 sess_data->iov[1].iov_base = ntlmssp_blob;
1199 sess_data->iov[1].iov_len = blob_length;
1201 rc = SMB2_sess_sendreceive(sess_data);
1205 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1207 ses->Suid = rsp->hdr.sync_hdr.SessionId;
1208 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1210 rc = SMB2_sess_establish_session(sess_data);
1212 kfree(ntlmssp_blob);
1213 SMB2_sess_free_buffer(sess_data);
1214 kfree(ses->ntlmssp);
1215 ses->ntlmssp = NULL;
1216 sess_data->result = rc;
1217 sess_data->func = NULL;
1221 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1225 type = smb2_select_sectype(ses->server, ses->sectype);
1226 cifs_dbg(FYI, "sess setup type %d\n", type);
1227 if (type == Unspecified) {
1229 "Unable to select appropriate authentication method!");
1235 sess_data->func = SMB2_auth_kerberos;
1238 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1241 cifs_dbg(VFS, "secType %d not supported!\n", type);
1249 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1250 const struct nls_table *nls_cp)
1253 struct TCP_Server_Info *server = ses->server;
1254 struct SMB2_sess_data *sess_data;
1256 cifs_dbg(FYI, "Session Setup\n");
1259 WARN(1, "%s: server is NULL!\n", __func__);
1263 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1267 rc = SMB2_select_sec(ses, sess_data);
1270 sess_data->xid = xid;
1271 sess_data->ses = ses;
1272 sess_data->buf0_type = CIFS_NO_BUFFER;
1273 sess_data->nls_cp = (struct nls_table *) nls_cp;
1275 #ifdef CONFIG_CIFS_SMB311
1277 * Initialize the session hash with the server one.
1279 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1280 SMB2_PREAUTH_HASH_SIZE);
1283 while (sess_data->func)
1284 sess_data->func(sess_data);
1286 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1287 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
1288 rc = sess_data->result;
1295 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1297 struct smb2_logoff_req *req; /* response is also trivial struct */
1299 struct TCP_Server_Info *server;
1301 unsigned int total_len;
1303 struct kvec rsp_iov;
1306 cifs_dbg(FYI, "disconnect session %p\n", ses);
1308 if (ses && (ses->server))
1309 server = ses->server;
1313 /* no need to send SMB logoff if uid already closed due to reconnect */
1314 if (ses->need_reconnect)
1315 goto smb2_session_already_dead;
1317 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
1321 /* since no tcon, smb2_init can not do this, so do here */
1322 req->sync_hdr.SessionId = ses->Suid;
1324 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1325 flags |= CIFS_TRANSFORM_REQ;
1326 else if (server->sign)
1327 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1329 flags |= CIFS_NO_RESP;
1331 iov[0].iov_base = (char *)req;
1332 iov[0].iov_len = total_len;
1334 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
1335 cifs_small_buf_release(req);
1337 * No tcon so can't do
1338 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1341 smb2_session_already_dead:
1345 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1347 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1350 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1352 /* These are similar values to what Windows uses */
1353 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1355 tcon->max_chunks = 256;
1356 tcon->max_bytes_chunk = 1048576;
1357 tcon->max_bytes_copy = 16777216;
1361 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1362 struct cifs_tcon *tcon, const struct nls_table *cp)
1364 struct smb2_tree_connect_req *req;
1365 struct smb2_tree_connect_rsp *rsp = NULL;
1367 struct kvec rsp_iov = { NULL, 0 };
1371 __le16 *unc_path = NULL;
1373 unsigned int total_len;
1375 cifs_dbg(FYI, "TCON\n");
1377 if (!(ses->server) || !tree)
1380 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1381 if (unc_path == NULL)
1384 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1386 if (unc_path_len < 2) {
1391 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1394 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1401 if (encryption_required(tcon))
1402 flags |= CIFS_TRANSFORM_REQ;
1404 iov[0].iov_base = (char *)req;
1406 iov[0].iov_len = total_len - 1;
1408 /* Testing shows that buffer offset must be at location of Buffer[0] */
1409 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1411 req->PathLength = cpu_to_le16(unc_path_len - 2);
1412 iov[1].iov_base = unc_path;
1413 iov[1].iov_len = unc_path_len;
1415 /* 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1 */
1416 if ((ses->server->dialect == SMB311_PROT_ID) &&
1417 !encryption_required(tcon))
1418 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1420 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
1421 cifs_small_buf_release(req);
1422 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1426 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1427 tcon->need_reconnect = true;
1429 goto tcon_error_exit;
1432 switch (rsp->ShareType) {
1433 case SMB2_SHARE_TYPE_DISK:
1434 cifs_dbg(FYI, "connection to disk share\n");
1436 case SMB2_SHARE_TYPE_PIPE:
1438 cifs_dbg(FYI, "connection to pipe share\n");
1440 case SMB2_SHARE_TYPE_PRINT:
1442 cifs_dbg(FYI, "connection to printer\n");
1445 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1447 goto tcon_error_exit;
1450 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1451 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1452 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1453 tcon->tidStatus = CifsGood;
1454 tcon->need_reconnect = false;
1455 tcon->tid = rsp->hdr.sync_hdr.TreeId;
1456 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1458 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1459 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1460 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1463 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1464 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1466 init_copy_chunk_defaults(tcon);
1467 if (tcon->ses->server->ops->validate_negotiate)
1468 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1470 free_rsp_buf(resp_buftype, rsp);
1475 if (rsp && rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1476 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1482 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1484 struct smb2_tree_disconnect_req *req; /* response is trivial */
1486 struct cifs_ses *ses = tcon->ses;
1488 unsigned int total_len;
1490 struct kvec rsp_iov;
1493 cifs_dbg(FYI, "Tree Disconnect\n");
1495 if (!ses || !(ses->server))
1498 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1501 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1506 if (encryption_required(tcon))
1507 flags |= CIFS_TRANSFORM_REQ;
1509 flags |= CIFS_NO_RESP;
1511 iov[0].iov_base = (char *)req;
1512 iov[0].iov_len = total_len;
1514 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
1515 cifs_small_buf_release(req);
1517 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1523 static struct create_durable *
1524 create_durable_buf(void)
1526 struct create_durable *buf;
1528 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1532 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1533 (struct create_durable, Data));
1534 buf->ccontext.DataLength = cpu_to_le32(16);
1535 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1536 (struct create_durable, Name));
1537 buf->ccontext.NameLength = cpu_to_le16(4);
1538 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1546 static struct create_durable *
1547 create_reconnect_durable_buf(struct cifs_fid *fid)
1549 struct create_durable *buf;
1551 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1555 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1556 (struct create_durable, Data));
1557 buf->ccontext.DataLength = cpu_to_le32(16);
1558 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1559 (struct create_durable, Name));
1560 buf->ccontext.NameLength = cpu_to_le16(4);
1561 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1562 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1563 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1572 parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1573 unsigned int *epoch)
1576 struct create_context *cc;
1578 unsigned int remaining;
1581 data_offset = (char *)rsp + server->vals->header_preamble_size + le32_to_cpu(rsp->CreateContextsOffset);
1582 remaining = le32_to_cpu(rsp->CreateContextsLength);
1583 cc = (struct create_context *)data_offset;
1584 while (remaining >= sizeof(struct create_context)) {
1585 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1586 if (le16_to_cpu(cc->NameLength) == 4 &&
1587 strncmp(name, "RqLs", 4) == 0)
1588 return server->ops->parse_lease_buf(cc, epoch);
1590 next = le32_to_cpu(cc->Next);
1594 cc = (struct create_context *)((char *)cc + next);
1601 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1602 unsigned int *num_iovec, __u8 *oplock)
1604 struct smb2_create_req *req = iov[0].iov_base;
1605 unsigned int num = *num_iovec;
1607 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
1608 if (iov[num].iov_base == NULL)
1610 iov[num].iov_len = server->vals->create_lease_size;
1611 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1612 if (!req->CreateContextsOffset)
1613 req->CreateContextsOffset = cpu_to_le32(
1614 sizeof(struct smb2_create_req) +
1615 iov[num - 1].iov_len);
1616 le32_add_cpu(&req->CreateContextsLength,
1617 server->vals->create_lease_size);
1618 *num_iovec = num + 1;
1622 static struct create_durable_v2 *
1623 create_durable_v2_buf(struct cifs_fid *pfid)
1625 struct create_durable_v2 *buf;
1627 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1631 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1632 (struct create_durable_v2, dcontext));
1633 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1634 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1635 (struct create_durable_v2, Name));
1636 buf->ccontext.NameLength = cpu_to_le16(4);
1638 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1639 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1640 generate_random_uuid(buf->dcontext.CreateGuid);
1641 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1643 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1651 static struct create_durable_handle_reconnect_v2 *
1652 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1654 struct create_durable_handle_reconnect_v2 *buf;
1656 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1661 buf->ccontext.DataOffset =
1662 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1664 buf->ccontext.DataLength =
1665 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1666 buf->ccontext.NameOffset =
1667 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1669 buf->ccontext.NameLength = cpu_to_le16(4);
1671 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1672 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1673 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1674 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1676 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1685 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1686 struct cifs_open_parms *oparms)
1688 struct smb2_create_req *req = iov[0].iov_base;
1689 unsigned int num = *num_iovec;
1691 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1692 if (iov[num].iov_base == NULL)
1694 iov[num].iov_len = sizeof(struct create_durable_v2);
1695 if (!req->CreateContextsOffset)
1696 req->CreateContextsOffset =
1697 cpu_to_le32(sizeof(struct smb2_create_req) +
1699 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1700 *num_iovec = num + 1;
1705 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1706 struct cifs_open_parms *oparms)
1708 struct smb2_create_req *req = iov[0].iov_base;
1709 unsigned int num = *num_iovec;
1711 /* indicate that we don't need to relock the file */
1712 oparms->reconnect = false;
1714 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1715 if (iov[num].iov_base == NULL)
1717 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1718 if (!req->CreateContextsOffset)
1719 req->CreateContextsOffset =
1720 cpu_to_le32(sizeof(struct smb2_create_req) +
1722 le32_add_cpu(&req->CreateContextsLength,
1723 sizeof(struct create_durable_handle_reconnect_v2));
1724 *num_iovec = num + 1;
1729 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1730 struct cifs_open_parms *oparms, bool use_persistent)
1732 struct smb2_create_req *req = iov[0].iov_base;
1733 unsigned int num = *num_iovec;
1735 if (use_persistent) {
1736 if (oparms->reconnect)
1737 return add_durable_reconnect_v2_context(iov, num_iovec,
1740 return add_durable_v2_context(iov, num_iovec, oparms);
1743 if (oparms->reconnect) {
1744 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1745 /* indicate that we don't need to relock the file */
1746 oparms->reconnect = false;
1748 iov[num].iov_base = create_durable_buf();
1749 if (iov[num].iov_base == NULL)
1751 iov[num].iov_len = sizeof(struct create_durable);
1752 if (!req->CreateContextsOffset)
1753 req->CreateContextsOffset =
1754 cpu_to_le32(sizeof(struct smb2_create_req) +
1756 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1757 *num_iovec = num + 1;
1762 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1763 const char *treename, const __le16 *path)
1765 int treename_len, path_len;
1766 struct nls_table *cp;
1767 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1772 treename_len = strlen(treename);
1773 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1779 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1782 * make room for one path separator between the treename and
1785 *out_len = treename_len + 1 + path_len;
1788 * final path needs to be null-terminated UTF16 with a
1792 *out_size = roundup((*out_len+1)*2, 8);
1793 *out_path = kzalloc(*out_size, GFP_KERNEL);
1797 cp = load_nls_default();
1798 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1799 UniStrcat(*out_path, sep);
1800 UniStrcat(*out_path, path);
1807 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
1808 __u8 *oplock, struct smb2_file_all_info *buf,
1809 struct smb2_err_rsp **err_buf)
1811 struct smb2_create_req *req;
1812 struct smb2_create_rsp *rsp;
1813 struct TCP_Server_Info *server;
1814 struct cifs_tcon *tcon = oparms->tcon;
1815 struct cifs_ses *ses = tcon->ses;
1817 struct kvec rsp_iov = {NULL, 0};
1820 __le16 *copy_path = NULL;
1823 unsigned int n_iov = 2;
1824 __u32 file_attributes = 0;
1825 char *dhc_buf = NULL, *lc_buf = NULL;
1827 unsigned int total_len;
1829 cifs_dbg(FYI, "create/open\n");
1831 if (ses && (ses->server))
1832 server = ses->server;
1836 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
1841 if (encryption_required(tcon))
1842 flags |= CIFS_TRANSFORM_REQ;
1844 if (oparms->create_options & CREATE_OPTION_READONLY)
1845 file_attributes |= ATTR_READONLY;
1846 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1847 file_attributes |= ATTR_SYSTEM;
1849 req->ImpersonationLevel = IL_IMPERSONATION;
1850 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
1851 /* File attributes ignored on open (used in create though) */
1852 req->FileAttributes = cpu_to_le32(file_attributes);
1853 req->ShareAccess = FILE_SHARE_ALL_LE;
1854 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1855 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
1857 iov[0].iov_base = (char *)req;
1858 /* -1 since last byte is buf[0] which is sent below (path) */
1859 iov[0].iov_len = total_len - 1;
1861 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
1863 /* [MS-SMB2] 2.2.13 NameOffset:
1864 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1865 * the SMB2 header, the file name includes a prefix that will
1866 * be processed during DFS name normalization as specified in
1867 * section 3.3.5.9. Otherwise, the file name is relative to
1868 * the share that is identified by the TreeId in the SMB2
1871 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1874 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
1875 rc = alloc_path_with_tree_prefix(©_path, ©_size,
1877 tcon->treeName, path);
1879 cifs_small_buf_release(req);
1882 req->NameLength = cpu_to_le16(name_len * 2);
1883 uni_path_len = copy_size;
1886 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1887 /* MUST set path len (NameLength) to 0 opening root of share */
1888 req->NameLength = cpu_to_le16(uni_path_len - 2);
1889 if (uni_path_len % 8 != 0) {
1890 copy_size = roundup(uni_path_len, 8);
1891 copy_path = kzalloc(copy_size, GFP_KERNEL);
1893 cifs_small_buf_release(req);
1896 memcpy((char *)copy_path, (const char *)path,
1898 uni_path_len = copy_size;
1903 iov[1].iov_len = uni_path_len;
1904 iov[1].iov_base = path;
1906 if (!server->oplocks)
1907 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1909 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1910 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1911 req->RequestedOplockLevel = *oplock;
1913 rc = add_lease_context(server, iov, &n_iov, oplock);
1915 cifs_small_buf_release(req);
1919 lc_buf = iov[n_iov-1].iov_base;
1922 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1923 /* need to set Next field of lease context if we request it */
1924 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
1925 struct create_context *ccontext =
1926 (struct create_context *)iov[n_iov-1].iov_base;
1928 cpu_to_le32(server->vals->create_lease_size);
1931 rc = add_durable_context(iov, &n_iov, oparms,
1932 tcon->use_persistent);
1934 cifs_small_buf_release(req);
1939 dhc_buf = iov[n_iov-1].iov_base;
1942 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
1944 cifs_small_buf_release(req);
1945 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
1948 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
1950 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1955 oparms->fid->persistent_fid = rsp->PersistentFileId;
1956 oparms->fid->volatile_fid = rsp->VolatileFileId;
1959 memcpy(buf, &rsp->CreationTime, 32);
1960 buf->AllocationSize = rsp->AllocationSize;
1961 buf->EndOfFile = rsp->EndofFile;
1962 buf->Attributes = rsp->FileAttributes;
1963 buf->NumberOfLinks = cpu_to_le32(1);
1964 buf->DeletePending = 0;
1967 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
1968 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
1970 *oplock = rsp->OplockLevel;
1975 free_rsp_buf(resp_buftype, rsp);
1980 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1983 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1984 u64 volatile_fid, u32 opcode, bool is_fsctl,
1985 char *in_data, u32 indatalen,
1986 char **out_data, u32 *plen /* returned data len */)
1988 struct smb2_ioctl_req *req;
1989 struct smb2_ioctl_rsp *rsp;
1990 struct smb2_sync_hdr *shdr;
1991 struct cifs_ses *ses;
1993 struct kvec rsp_iov;
1998 unsigned int total_len;
2000 cifs_dbg(FYI, "SMB2 IOCTL\n");
2002 if (out_data != NULL)
2005 /* zero out returned data len, in case of error */
2014 if (!ses || !(ses->server))
2017 rc = smb2_plain_req_init(SMB2_IOCTL, tcon, (void **) &req, &total_len);
2021 if (encryption_required(tcon))
2022 flags |= CIFS_TRANSFORM_REQ;
2024 req->CtlCode = cpu_to_le32(opcode);
2025 req->PersistentFileId = persistent_fid;
2026 req->VolatileFileId = volatile_fid;
2029 req->InputCount = cpu_to_le32(indatalen);
2030 /* do not set InputOffset if no input data */
2032 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
2033 iov[1].iov_base = in_data;
2034 iov[1].iov_len = indatalen;
2039 req->OutputOffset = 0;
2040 req->OutputCount = 0; /* MBZ */
2043 * Could increase MaxOutputResponse, but that would require more
2044 * than one credit. Windows typically sets this smaller, but for some
2045 * ioctls it may be useful to allow server to send more. No point
2046 * limiting what the server can send as long as fits in one credit
2047 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
2048 * (by default, note that it can be overridden to make max larger)
2049 * in responses (except for read responses which can be bigger.
2050 * We may want to bump this limit up
2052 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
2055 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2059 iov[0].iov_base = (char *)req;
2062 * If no input data, the size of ioctl struct in
2063 * protocol spec still includes a 1 byte data buffer,
2064 * but if input data passed to ioctl, we do not
2065 * want to double count this, so we do not send
2066 * the dummy one byte of data in iovec[0] if sending
2067 * input data (in iovec[1]).
2071 iov[0].iov_len = total_len - 1;
2073 iov[0].iov_len = total_len;
2075 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2076 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
2077 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
2079 rc = smb2_send_recv(xid, ses, iov, n_iov, &resp_buftype, flags,
2081 cifs_small_buf_release(req);
2082 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
2084 if ((rc != 0) && (rc != -EINVAL)) {
2085 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2087 } else if (rc == -EINVAL) {
2088 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2089 (opcode != FSCTL_SRV_COPYCHUNK)) {
2090 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2095 /* check if caller wants to look at return data or just return rc */
2096 if ((plen == NULL) || (out_data == NULL))
2099 *plen = le32_to_cpu(rsp->OutputCount);
2101 /* We check for obvious errors in the output buffer length and offset */
2103 goto ioctl_exit; /* server returned no data */
2104 else if (*plen > 0xFF00) {
2105 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2111 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
2112 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2113 le32_to_cpu(rsp->OutputOffset));
2119 *out_data = kmalloc(*plen, GFP_KERNEL);
2120 if (*out_data == NULL) {
2125 shdr = get_sync_hdr(rsp);
2126 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
2128 free_rsp_buf(resp_buftype, rsp);
2133 * Individual callers to ioctl worker function follow
2137 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2138 u64 persistent_fid, u64 volatile_fid)
2141 struct compress_ioctl fsctl_input;
2142 char *ret_data = NULL;
2144 fsctl_input.CompressionState =
2145 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
2147 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2148 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2149 (char *)&fsctl_input /* data input */,
2150 2 /* in data len */, &ret_data /* out data */, NULL);
2152 cifs_dbg(FYI, "set compression rc %d\n", rc);
2158 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2159 u64 persistent_fid, u64 volatile_fid)
2161 struct smb2_close_req *req;
2162 struct smb2_close_rsp *rsp;
2163 struct cifs_ses *ses = tcon->ses;
2165 struct kvec rsp_iov;
2169 unsigned int total_len;
2171 cifs_dbg(FYI, "Close\n");
2173 if (!ses || !(ses->server))
2176 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2180 if (encryption_required(tcon))
2181 flags |= CIFS_TRANSFORM_REQ;
2183 req->PersistentFileId = persistent_fid;
2184 req->VolatileFileId = volatile_fid;
2186 iov[0].iov_base = (char *)req;
2187 iov[0].iov_len = total_len;
2189 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2190 cifs_small_buf_release(req);
2191 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2194 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
2198 /* BB FIXME - decode close response, update inode for caching */
2201 free_rsp_buf(resp_buftype, rsp);
2206 validate_buf(unsigned int offset, unsigned int buffer_length,
2207 struct smb2_hdr *hdr, unsigned int min_buf_size)
2210 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2211 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2212 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2213 char *end_of_buf = begin_of_buf + buffer_length;
2216 if (buffer_length < min_buf_size) {
2217 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2218 buffer_length, min_buf_size);
2222 /* check if beyond RFC1001 maximum length */
2223 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
2224 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2225 buffer_length, smb_len);
2229 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
2230 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
2238 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2239 * Caller must free buffer.
2242 validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2243 struct smb2_hdr *hdr, unsigned int minbufsize,
2247 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2253 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2257 memcpy(data, begin_of_buf, buffer_length);
2263 query_info(const unsigned int xid, struct cifs_tcon *tcon,
2264 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2265 u32 additional_info, size_t output_len, size_t min_len, void **data,
2268 struct smb2_query_info_req *req;
2269 struct smb2_query_info_rsp *rsp = NULL;
2271 struct kvec rsp_iov;
2274 struct cifs_ses *ses = tcon->ses;
2276 unsigned int total_len;
2278 cifs_dbg(FYI, "Query Info\n");
2280 if (!ses || !(ses->server))
2283 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2288 if (encryption_required(tcon))
2289 flags |= CIFS_TRANSFORM_REQ;
2291 req->InfoType = info_type;
2292 req->FileInfoClass = info_class;
2293 req->PersistentFileId = persistent_fid;
2294 req->VolatileFileId = volatile_fid;
2295 req->AdditionalInformation = cpu_to_le32(additional_info);
2298 * We do not use the input buffer (do not send extra byte)
2300 req->InputBufferOffset = 0;
2302 req->OutputBufferLength = cpu_to_le32(output_len);
2304 iov[0].iov_base = (char *)req;
2306 iov[0].iov_len = total_len - 1;
2308 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2309 cifs_small_buf_release(req);
2310 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2313 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2318 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2320 *data = kmalloc(*dlen, GFP_KERNEL);
2323 "Error %d allocating memory for acl\n",
2331 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2332 le32_to_cpu(rsp->OutputBufferLength),
2333 &rsp->hdr, min_len, *data);
2336 free_rsp_buf(resp_buftype, rsp);
2340 int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
2341 u64 persistent_fid, u64 volatile_fid,
2342 int ea_buf_size, struct smb2_file_full_ea_info *data)
2344 return query_info(xid, tcon, persistent_fid, volatile_fid,
2345 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
2347 sizeof(struct smb2_file_full_ea_info),
2352 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2353 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
2355 return query_info(xid, tcon, persistent_fid, volatile_fid,
2356 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
2357 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
2358 sizeof(struct smb2_file_all_info), (void **)&data,
2363 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2364 u64 persistent_fid, u64 volatile_fid,
2365 void **data, u32 *plen)
2367 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2370 return query_info(xid, tcon, persistent_fid, volatile_fid,
2371 0, SMB2_O_INFO_SECURITY, additional_info,
2372 SMB2_MAX_BUFFER_SIZE,
2373 sizeof(struct smb2_file_all_info), data, plen);
2377 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2378 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2380 return query_info(xid, tcon, persistent_fid, volatile_fid,
2381 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
2382 sizeof(struct smb2_file_internal_info),
2383 sizeof(struct smb2_file_internal_info),
2384 (void **)&uniqueid, NULL);
2388 * This is a no-op for now. We're not really interested in the reply, but
2389 * rather in the fact that the server sent one and that server->lstrp
2392 * FIXME: maybe we should consider checking that the reply matches request?
2395 smb2_echo_callback(struct mid_q_entry *mid)
2397 struct TCP_Server_Info *server = mid->callback_data;
2398 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
2399 unsigned int credits_received = 1;
2401 if (mid->mid_state == MID_RESPONSE_RECEIVED)
2402 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
2404 DeleteMidQEntry(mid);
2405 add_credits(server, credits_received, CIFS_ECHO_OP);
2408 void smb2_reconnect_server(struct work_struct *work)
2410 struct TCP_Server_Info *server = container_of(work,
2411 struct TCP_Server_Info, reconnect.work);
2412 struct cifs_ses *ses;
2413 struct cifs_tcon *tcon, *tcon2;
2414 struct list_head tmp_list;
2415 int tcon_exist = false;
2417 int resched = false;
2420 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2421 mutex_lock(&server->reconnect_mutex);
2423 INIT_LIST_HEAD(&tmp_list);
2424 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2426 spin_lock(&cifs_tcp_ses_lock);
2427 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2428 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2429 if (tcon->need_reconnect || tcon->need_reopen_files) {
2431 list_add_tail(&tcon->rlist, &tmp_list);
2435 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2436 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2441 * Get the reference to server struct to be sure that the last call of
2442 * cifs_put_tcon() in the loop below won't release the server pointer.
2445 server->srv_count++;
2447 spin_unlock(&cifs_tcp_ses_lock);
2449 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
2450 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2452 cifs_reopen_persistent_handles(tcon);
2455 list_del_init(&tcon->rlist);
2456 cifs_put_tcon(tcon);
2459 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2461 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
2462 mutex_unlock(&server->reconnect_mutex);
2464 /* now we can safely release srv struct */
2466 cifs_put_tcp_session(server, 1);
2470 SMB2_echo(struct TCP_Server_Info *server)
2472 struct smb2_echo_req *req;
2475 struct smb_rqst rqst = { .rq_iov = iov,
2477 unsigned int total_len;
2478 __be32 rfc1002_marker;
2480 cifs_dbg(FYI, "In echo request\n");
2482 if (server->tcpStatus == CifsNeedNegotiate) {
2483 /* No need to send echo on newly established connections */
2484 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2488 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
2492 req->sync_hdr.CreditRequest = cpu_to_le16(1);
2495 rfc1002_marker = cpu_to_be32(total_len);
2496 iov[0].iov_base = &rfc1002_marker;
2497 iov[1].iov_len = total_len;
2498 iov[1].iov_base = (char *)req;
2500 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2501 server, CIFS_ECHO_OP);
2503 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
2505 cifs_small_buf_release(req);
2510 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2513 struct smb2_flush_req *req;
2514 struct cifs_ses *ses = tcon->ses;
2516 struct kvec rsp_iov;
2520 unsigned int total_len;
2522 cifs_dbg(FYI, "Flush\n");
2524 if (!ses || !(ses->server))
2527 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
2531 if (encryption_required(tcon))
2532 flags |= CIFS_TRANSFORM_REQ;
2534 req->PersistentFileId = persistent_fid;
2535 req->VolatileFileId = volatile_fid;
2537 iov[0].iov_base = (char *)req;
2538 iov[0].iov_len = total_len;
2540 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2541 cifs_small_buf_release(req);
2544 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2546 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2551 * To form a chain of read requests, any read requests after the first should
2552 * have the end_of_chain boolean set to true.
2555 smb2_new_read_req(void **buf, unsigned int *total_len,
2556 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
2557 unsigned int remaining_bytes, int request_type)
2560 struct smb2_read_plain_req *req = NULL;
2561 struct smb2_sync_hdr *shdr;
2562 struct TCP_Server_Info *server;
2564 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2569 server = io_parms->tcon->ses->server;
2571 return -ECONNABORTED;
2573 shdr = &req->sync_hdr;
2574 shdr->ProcessId = cpu_to_le32(io_parms->pid);
2576 req->PersistentFileId = io_parms->persistent_fid;
2577 req->VolatileFileId = io_parms->volatile_fid;
2578 req->ReadChannelInfoOffset = 0; /* reserved */
2579 req->ReadChannelInfoLength = 0; /* reserved */
2580 req->Channel = 0; /* reserved */
2581 req->MinimumCount = 0;
2582 req->Length = cpu_to_le32(io_parms->length);
2583 req->Offset = cpu_to_le64(io_parms->offset);
2584 #ifdef CONFIG_CIFS_SMB_DIRECT
2586 * If we want to do a RDMA write, fill in and append
2587 * smbd_buffer_descriptor_v1 to the end of read request
2589 if (server->rdma && rdata &&
2590 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
2592 struct smbd_buffer_descriptor_v1 *v1;
2593 bool need_invalidate =
2594 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
2596 rdata->mr = smbd_register_mr(
2597 server->smbd_conn, rdata->pages,
2598 rdata->nr_pages, rdata->tailsz,
2599 true, need_invalidate);
2603 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2604 if (need_invalidate)
2605 req->Channel = SMB2_CHANNEL_RDMA_V1;
2606 req->ReadChannelInfoOffset =
2607 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
2608 req->ReadChannelInfoLength =
2609 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
2610 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
2611 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
2612 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
2613 v1->length = cpu_to_le32(rdata->mr->mr->length);
2615 *total_len += sizeof(*v1) - 1;
2618 if (request_type & CHAINED_REQUEST) {
2619 if (!(request_type & END_OF_CHAIN)) {
2620 /* next 8-byte aligned request */
2621 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2622 shdr->NextCommand = cpu_to_le32(*total_len);
2623 } else /* END_OF_CHAIN */
2624 shdr->NextCommand = 0;
2625 if (request_type & RELATED_REQUEST) {
2626 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2628 * Related requests use info from previous read request
2631 shdr->SessionId = 0xFFFFFFFF;
2632 shdr->TreeId = 0xFFFFFFFF;
2633 req->PersistentFileId = 0xFFFFFFFF;
2634 req->VolatileFileId = 0xFFFFFFFF;
2637 if (remaining_bytes > io_parms->length)
2638 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2640 req->RemainingBytes = 0;
2647 smb2_readv_callback(struct mid_q_entry *mid)
2649 struct cifs_readdata *rdata = mid->callback_data;
2650 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2651 struct TCP_Server_Info *server = tcon->ses->server;
2652 struct smb2_sync_hdr *shdr =
2653 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
2654 unsigned int credits_received = 1;
2655 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2657 .rq_pages = rdata->pages,
2658 .rq_npages = rdata->nr_pages,
2659 .rq_pagesz = rdata->pagesz,
2660 .rq_tailsz = rdata->tailsz };
2662 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2663 __func__, mid->mid, mid->mid_state, rdata->result,
2666 switch (mid->mid_state) {
2667 case MID_RESPONSE_RECEIVED:
2668 credits_received = le16_to_cpu(shdr->CreditRequest);
2669 /* result already set, check signature */
2670 if (server->sign && !mid->decrypted) {
2673 rc = smb2_verify_signature(&rqst, server);
2675 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2678 /* FIXME: should this be counted toward the initiating task? */
2679 task_io_account_read(rdata->got_bytes);
2680 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2682 case MID_REQUEST_SUBMITTED:
2683 case MID_RETRY_NEEDED:
2684 rdata->result = -EAGAIN;
2685 if (server->sign && rdata->got_bytes)
2686 /* reset bytes number since we can not check a sign */
2687 rdata->got_bytes = 0;
2688 /* FIXME: should this be counted toward the initiating task? */
2689 task_io_account_read(rdata->got_bytes);
2690 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2693 if (rdata->result != -ENODATA)
2694 rdata->result = -EIO;
2696 #ifdef CONFIG_CIFS_SMB_DIRECT
2698 * If this rdata has a memmory registered, the MR can be freed
2699 * MR needs to be freed as soon as I/O finishes to prevent deadlock
2700 * because they have limited number and are used for future I/Os
2703 smbd_deregister_mr(rdata->mr);
2708 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2710 queue_work(cifsiod_wq, &rdata->work);
2711 DeleteMidQEntry(mid);
2712 add_credits(server, credits_received, 0);
2715 /* smb2_async_readv - send an async read, and set up mid to handle result */
2717 smb2_async_readv(struct cifs_readdata *rdata)
2721 struct smb2_sync_hdr *shdr;
2722 struct cifs_io_parms io_parms;
2723 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2725 struct TCP_Server_Info *server;
2726 unsigned int total_len;
2729 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2730 __func__, rdata->offset, rdata->bytes);
2732 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2733 io_parms.offset = rdata->offset;
2734 io_parms.length = rdata->bytes;
2735 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2736 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2737 io_parms.pid = rdata->pid;
2739 server = io_parms.tcon->ses->server;
2741 rc = smb2_new_read_req(
2742 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
2744 if (rc == -EAGAIN && rdata->credits) {
2745 /* credits was reset by reconnect */
2747 /* reduce in_flight value since we won't send the req */
2748 spin_lock(&server->req_lock);
2749 server->in_flight--;
2750 spin_unlock(&server->req_lock);
2755 if (encryption_required(io_parms.tcon))
2756 flags |= CIFS_TRANSFORM_REQ;
2758 req_len = cpu_to_be32(total_len);
2760 rdata->iov[0].iov_base = &req_len;
2761 rdata->iov[0].iov_len = sizeof(__be32);
2762 rdata->iov[1].iov_base = buf;
2763 rdata->iov[1].iov_len = total_len;
2765 shdr = (struct smb2_sync_hdr *)buf;
2767 if (rdata->credits) {
2768 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2769 SMB2_MAX_BUFFER_SIZE));
2770 shdr->CreditRequest = shdr->CreditCharge;
2771 spin_lock(&server->req_lock);
2772 server->credits += rdata->credits -
2773 le16_to_cpu(shdr->CreditCharge);
2774 spin_unlock(&server->req_lock);
2775 wake_up(&server->request_q);
2776 flags |= CIFS_HAS_CREDITS;
2779 kref_get(&rdata->refcount);
2780 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
2781 cifs_readv_receive, smb2_readv_callback,
2782 smb3_handle_read_data, rdata, flags);
2784 kref_put(&rdata->refcount, cifs_readdata_release);
2785 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2788 cifs_small_buf_release(buf);
2793 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2794 unsigned int *nbytes, char **buf, int *buf_type)
2796 int resp_buftype, rc = -EACCES;
2797 struct smb2_read_plain_req *req = NULL;
2798 struct smb2_read_rsp *rsp = NULL;
2799 struct smb2_sync_hdr *shdr;
2801 struct kvec rsp_iov;
2802 unsigned int total_len;
2803 int flags = CIFS_LOG_ERROR;
2804 struct cifs_ses *ses = io_parms->tcon->ses;
2807 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
2811 if (encryption_required(io_parms->tcon))
2812 flags |= CIFS_TRANSFORM_REQ;
2814 iov[0].iov_base = (char *)req;
2815 iov[0].iov_len = total_len;
2817 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2818 cifs_small_buf_release(req);
2820 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
2823 if (rc != -ENODATA) {
2824 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2825 cifs_dbg(VFS, "Send error in read = %d\n", rc);
2827 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2828 return rc == -ENODATA ? 0 : rc;
2831 *nbytes = le32_to_cpu(rsp->DataLength);
2832 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2833 (*nbytes > io_parms->length)) {
2834 cifs_dbg(FYI, "bad length %d for count %d\n",
2835 *nbytes, io_parms->length);
2840 shdr = get_sync_hdr(rsp);
2843 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
2844 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2845 } else if (resp_buftype != CIFS_NO_BUFFER) {
2846 *buf = rsp_iov.iov_base;
2847 if (resp_buftype == CIFS_SMALL_BUFFER)
2848 *buf_type = CIFS_SMALL_BUFFER;
2849 else if (resp_buftype == CIFS_LARGE_BUFFER)
2850 *buf_type = CIFS_LARGE_BUFFER;
2856 * Check the mid_state and signature on received buffer (if any), and queue the
2857 * workqueue completion task.
2860 smb2_writev_callback(struct mid_q_entry *mid)
2862 struct cifs_writedata *wdata = mid->callback_data;
2863 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2864 unsigned int written;
2865 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2866 unsigned int credits_received = 1;
2868 switch (mid->mid_state) {
2869 case MID_RESPONSE_RECEIVED:
2870 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
2871 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2872 if (wdata->result != 0)
2875 written = le32_to_cpu(rsp->DataLength);
2877 * Mask off high 16 bits when bytes written as returned
2878 * by the server is greater than bytes requested by the
2879 * client. OS/2 servers are known to set incorrect
2882 if (written > wdata->bytes)
2885 if (written < wdata->bytes)
2886 wdata->result = -ENOSPC;
2888 wdata->bytes = written;
2890 case MID_REQUEST_SUBMITTED:
2891 case MID_RETRY_NEEDED:
2892 wdata->result = -EAGAIN;
2895 wdata->result = -EIO;
2898 #ifdef CONFIG_CIFS_SMB_DIRECT
2900 * If this wdata has a memory registered, the MR can be freed
2901 * The number of MRs available is limited, it's important to recover
2902 * used MR as soon as I/O is finished. Hold MR longer in the later
2903 * I/O process can possibly result in I/O deadlock due to lack of MR
2904 * to send request on I/O retry
2907 smbd_deregister_mr(wdata->mr);
2912 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2914 queue_work(cifsiod_wq, &wdata->work);
2915 DeleteMidQEntry(mid);
2916 add_credits(tcon->ses->server, credits_received, 0);
2919 /* smb2_async_writev - send an async write, and set up mid to handle result */
2921 smb2_async_writev(struct cifs_writedata *wdata,
2922 void (*release)(struct kref *kref))
2924 int rc = -EACCES, flags = 0;
2925 struct smb2_write_req *req = NULL;
2926 struct smb2_sync_hdr *shdr;
2927 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2928 struct TCP_Server_Info *server = tcon->ses->server;
2930 struct smb_rqst rqst = { };
2931 unsigned int total_len;
2932 __be32 rfc1002_marker;
2934 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
2936 if (rc == -EAGAIN && wdata->credits) {
2937 /* credits was reset by reconnect */
2939 /* reduce in_flight value since we won't send the req */
2940 spin_lock(&server->req_lock);
2941 server->in_flight--;
2942 spin_unlock(&server->req_lock);
2944 goto async_writev_out;
2947 if (encryption_required(tcon))
2948 flags |= CIFS_TRANSFORM_REQ;
2950 shdr = (struct smb2_sync_hdr *)req;
2951 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
2953 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2954 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2955 req->WriteChannelInfoOffset = 0;
2956 req->WriteChannelInfoLength = 0;
2958 req->Offset = cpu_to_le64(wdata->offset);
2959 req->DataOffset = cpu_to_le16(
2960 offsetof(struct smb2_write_req, Buffer));
2961 req->RemainingBytes = 0;
2962 #ifdef CONFIG_CIFS_SMB_DIRECT
2964 * If we want to do a server RDMA read, fill in and append
2965 * smbd_buffer_descriptor_v1 to the end of write request
2967 if (server->rdma && wdata->bytes >=
2968 server->smbd_conn->rdma_readwrite_threshold) {
2970 struct smbd_buffer_descriptor_v1 *v1;
2971 bool need_invalidate = server->dialect == SMB30_PROT_ID;
2973 wdata->mr = smbd_register_mr(
2974 server->smbd_conn, wdata->pages,
2975 wdata->nr_pages, wdata->tailsz,
2976 false, need_invalidate);
2979 goto async_writev_out;
2982 req->DataOffset = 0;
2983 req->RemainingBytes =
2984 cpu_to_le32((wdata->nr_pages-1)*PAGE_SIZE + wdata->tailsz);
2985 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
2986 if (need_invalidate)
2987 req->Channel = SMB2_CHANNEL_RDMA_V1;
2988 req->WriteChannelInfoOffset =
2989 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
2990 req->WriteChannelInfoLength =
2991 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
2992 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
2993 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
2994 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
2995 v1->length = cpu_to_le32(wdata->mr->mr->length);
2998 /* 4 for rfc1002 length field and 1 for Buffer */
3000 rfc1002_marker = cpu_to_be32(total_len - 1 + wdata->bytes);
3001 iov[0].iov_base = &rfc1002_marker;
3002 iov[1].iov_len = total_len - 1;
3003 iov[1].iov_base = (char *)req;
3007 rqst.rq_pages = wdata->pages;
3008 rqst.rq_npages = wdata->nr_pages;
3009 rqst.rq_pagesz = wdata->pagesz;
3010 rqst.rq_tailsz = wdata->tailsz;
3011 #ifdef CONFIG_CIFS_SMB_DIRECT
3013 iov[1].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
3017 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3018 wdata->offset, wdata->bytes);
3020 #ifdef CONFIG_CIFS_SMB_DIRECT
3021 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3023 req->Length = cpu_to_le32(wdata->bytes);
3025 req->Length = cpu_to_le32(wdata->bytes);
3028 if (wdata->credits) {
3029 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
3030 SMB2_MAX_BUFFER_SIZE));
3031 shdr->CreditRequest = shdr->CreditCharge;
3032 spin_lock(&server->req_lock);
3033 server->credits += wdata->credits -
3034 le16_to_cpu(shdr->CreditCharge);
3035 spin_unlock(&server->req_lock);
3036 wake_up(&server->request_q);
3037 flags |= CIFS_HAS_CREDITS;
3040 kref_get(&wdata->refcount);
3041 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3045 kref_put(&wdata->refcount, release);
3046 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3050 cifs_small_buf_release(req);
3055 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3056 * The length field from io_parms must be at least 1 and indicates a number of
3057 * elements with data to write that begins with position 1 in iov array. All
3058 * data length is specified by count.
3061 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3062 unsigned int *nbytes, struct kvec *iov, int n_vec)
3065 struct smb2_write_req *req = NULL;
3066 struct smb2_write_rsp *rsp = NULL;
3068 struct kvec rsp_iov;
3070 unsigned int total_len;
3077 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3082 if (io_parms->tcon->ses->server == NULL)
3083 return -ECONNABORTED;
3085 if (encryption_required(io_parms->tcon))
3086 flags |= CIFS_TRANSFORM_REQ;
3088 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
3090 req->PersistentFileId = io_parms->persistent_fid;
3091 req->VolatileFileId = io_parms->volatile_fid;
3092 req->WriteChannelInfoOffset = 0;
3093 req->WriteChannelInfoLength = 0;
3095 req->Length = cpu_to_le32(io_parms->length);
3096 req->Offset = cpu_to_le64(io_parms->offset);
3097 req->DataOffset = cpu_to_le16(
3098 offsetof(struct smb2_write_req, Buffer));
3099 req->RemainingBytes = 0;
3101 iov[0].iov_base = (char *)req;
3103 iov[0].iov_len = total_len - 1;
3105 rc = smb2_send_recv(xid, io_parms->tcon->ses, iov, n_vec + 1,
3106 &resp_buftype, flags, &rsp_iov);
3107 cifs_small_buf_release(req);
3108 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
3111 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
3112 cifs_dbg(VFS, "Send error in write = %d\n", rc);
3114 *nbytes = le32_to_cpu(rsp->DataLength);
3116 free_rsp_buf(resp_buftype, rsp);
3121 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3124 unsigned int entrycount = 0;
3125 unsigned int next_offset = 0;
3126 FILE_DIRECTORY_INFO *entryptr;
3128 if (bufstart == NULL)
3131 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
3134 entryptr = (FILE_DIRECTORY_INFO *)
3135 ((char *)entryptr + next_offset);
3137 if ((char *)entryptr + size > end_of_buf) {
3138 cifs_dbg(VFS, "malformed search entry would overflow\n");
3142 len = le32_to_cpu(entryptr->FileNameLength);
3143 if ((char *)entryptr + len + size > end_of_buf) {
3144 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3149 *lastentry = (char *)entryptr;
3152 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
3164 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3165 u64 persistent_fid, u64 volatile_fid, int index,
3166 struct cifs_search_info *srch_inf)
3168 struct smb2_query_directory_req *req;
3169 struct smb2_query_directory_rsp *rsp = NULL;
3171 struct kvec rsp_iov;
3174 int resp_buftype = CIFS_NO_BUFFER;
3175 unsigned char *bufptr;
3176 struct TCP_Server_Info *server;
3177 struct cifs_ses *ses = tcon->ses;
3178 __le16 asteriks = cpu_to_le16('*');
3180 unsigned int output_size = CIFSMaxBufSize;
3181 size_t info_buf_size;
3183 unsigned int total_len;
3185 if (ses && (ses->server))
3186 server = ses->server;
3190 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3195 if (encryption_required(tcon))
3196 flags |= CIFS_TRANSFORM_REQ;
3198 switch (srch_inf->info_level) {
3199 case SMB_FIND_FILE_DIRECTORY_INFO:
3200 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3201 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3203 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3204 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3205 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3208 cifs_dbg(VFS, "info level %u isn't supported\n",
3209 srch_inf->info_level);
3214 req->FileIndex = cpu_to_le32(index);
3215 req->PersistentFileId = persistent_fid;
3216 req->VolatileFileId = volatile_fid;
3219 bufptr = req->Buffer;
3220 memcpy(bufptr, &asteriks, len);
3222 req->FileNameOffset =
3223 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
3224 req->FileNameLength = cpu_to_le16(len);
3226 * BB could be 30 bytes or so longer if we used SMB2 specific
3227 * buffer lengths, but this is safe and close enough.
3229 output_size = min_t(unsigned int, output_size, server->maxBuf);
3230 output_size = min_t(unsigned int, output_size, 2 << 15);
3231 req->OutputBufferLength = cpu_to_le32(output_size);
3233 iov[0].iov_base = (char *)req;
3235 iov[0].iov_len = total_len - 1;
3237 iov[1].iov_base = (char *)(req->Buffer);
3238 iov[1].iov_len = len;
3240 rc = smb2_send_recv(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
3241 cifs_small_buf_release(req);
3242 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
3245 if (rc == -ENODATA &&
3246 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
3247 srch_inf->endOfSearch = true;
3250 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3254 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3255 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3260 srch_inf->unicode = true;
3262 if (srch_inf->ntwrk_buf_start) {
3263 if (srch_inf->smallBuf)
3264 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3266 cifs_buf_release(srch_inf->ntwrk_buf_start);
3268 srch_inf->ntwrk_buf_start = (char *)rsp;
3269 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3270 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3271 /* 4 for rfc1002 length field */
3272 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3273 srch_inf->entries_in_buffer =
3274 num_entries(srch_inf->srch_entries_start, end_of_smb,
3275 &srch_inf->last_entry, info_buf_size);
3276 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
3277 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3278 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3279 srch_inf->srch_entries_start, srch_inf->last_entry);
3280 if (resp_buftype == CIFS_LARGE_BUFFER)
3281 srch_inf->smallBuf = false;
3282 else if (resp_buftype == CIFS_SMALL_BUFFER)
3283 srch_inf->smallBuf = true;
3285 cifs_dbg(VFS, "illegal search buffer type\n");
3290 free_rsp_buf(resp_buftype, rsp);
3295 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3296 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3297 u8 info_type, u32 additional_info, unsigned int num,
3298 void **data, unsigned int *size)
3300 struct smb2_set_info_req *req;
3301 struct smb2_set_info_rsp *rsp = NULL;
3303 struct kvec rsp_iov;
3307 struct cifs_ses *ses = tcon->ses;
3309 unsigned int total_len;
3311 if (!ses || !(ses->server))
3317 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3321 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
3327 if (encryption_required(tcon))
3328 flags |= CIFS_TRANSFORM_REQ;
3330 req->sync_hdr.ProcessId = cpu_to_le32(pid);
3332 req->InfoType = info_type;
3333 req->FileInfoClass = info_class;
3334 req->PersistentFileId = persistent_fid;
3335 req->VolatileFileId = volatile_fid;
3336 req->AdditionalInformation = cpu_to_le32(additional_info);
3339 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
3340 req->BufferLength = cpu_to_le32(*size);
3342 memcpy(req->Buffer, *data, *size);
3345 iov[0].iov_base = (char *)req;
3347 iov[0].iov_len = total_len - 1;
3349 for (i = 1; i < num; i++) {
3350 le32_add_cpu(&req->BufferLength, size[i]);
3351 iov[i].iov_base = (char *)data[i];
3352 iov[i].iov_len = size[i];
3355 rc = smb2_send_recv(xid, ses, iov, num, &resp_buftype, flags,
3357 cifs_small_buf_release(req);
3358 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
3361 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
3363 free_rsp_buf(resp_buftype, rsp);
3369 SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3370 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3372 struct smb2_file_rename_info info;
3374 unsigned int size[2];
3376 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3378 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3382 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3383 /* 0 = fail if target already exists */
3384 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3385 info.FileNameLength = cpu_to_le32(len);
3388 size[0] = sizeof(struct smb2_file_rename_info);
3390 data[1] = target_file;
3391 size[1] = len + 2 /* null */;
3393 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
3394 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3401 SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3402 u64 persistent_fid, u64 volatile_fid)
3404 __u8 delete_pending = 1;
3408 data = &delete_pending;
3409 size = 1; /* sizeof __u8 */
3411 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3412 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3413 0, 1, &data, &size);
3417 SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3418 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3420 struct smb2_file_link_info info;
3422 unsigned int size[2];
3424 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3426 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3430 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3431 /* 0 = fail if link already exists */
3432 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3433 info.FileNameLength = cpu_to_le32(len);
3436 size[0] = sizeof(struct smb2_file_link_info);
3438 data[1] = target_file;
3439 size[1] = len + 2 /* null */;
3441 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
3442 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3449 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3450 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
3452 struct smb2_file_eof_info info;
3456 info.EndOfFile = *eof;
3459 size = sizeof(struct smb2_file_eof_info);
3462 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3463 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3464 0, 1, &data, &size);
3466 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3467 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3468 0, 1, &data, &size);
3472 SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3473 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3476 size = sizeof(FILE_BASIC_INFO);
3477 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3478 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3479 0, 1, (void **)&buf, &size);
3483 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3484 u64 persistent_fid, u64 volatile_fid,
3485 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3487 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3488 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3489 1, (void **)&pnntsd, &pacllen);
3493 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3494 u64 persistent_fid, u64 volatile_fid,
3495 struct smb2_file_full_ea_info *buf, int len)
3497 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3498 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3499 0, 1, (void **)&buf, &len);
3503 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3504 const u64 persistent_fid, const u64 volatile_fid,
3508 struct smb2_oplock_break_req *req = NULL;
3509 struct cifs_ses *ses = tcon->ses;
3510 int flags = CIFS_OBREAK_OP;
3511 unsigned int total_len;
3513 struct kvec rsp_iov;
3516 cifs_dbg(FYI, "SMB2_oplock_break\n");
3517 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3522 if (encryption_required(tcon))
3523 flags |= CIFS_TRANSFORM_REQ;
3525 req->VolatileFid = volatile_fid;
3526 req->PersistentFid = persistent_fid;
3527 req->OplockLevel = oplock_level;
3528 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3530 flags |= CIFS_NO_RESP;
3532 iov[0].iov_base = (char *)req;
3533 iov[0].iov_len = total_len;
3535 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
3536 cifs_small_buf_release(req);
3539 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3540 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
3547 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3548 struct kstatfs *kst)
3550 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3551 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3552 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
3553 kst->f_bfree = kst->f_bavail =
3554 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
3559 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3560 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3562 struct TCP_Server_Info *server;
3564 struct smb2_query_info_req *req;
3565 unsigned int total_len;
3567 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
3569 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3572 server = tcon->ses->server;
3574 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
3579 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3580 req->FileInfoClass = level;
3581 req->PersistentFileId = persistent_fid;
3582 req->VolatileFileId = volatile_fid;
3584 req->InputBufferOffset =
3585 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
3586 req->OutputBufferLength = cpu_to_le32(
3587 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - server->vals->header_preamble_size);
3589 iov->iov_base = (char *)req;
3590 iov->iov_len = total_len;
3595 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3596 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3598 struct smb2_query_info_rsp *rsp = NULL;
3600 struct kvec rsp_iov;
3603 struct cifs_ses *ses = tcon->ses;
3604 struct TCP_Server_Info *server = ses->server;
3605 struct smb2_fs_full_size_info *info = NULL;
3608 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3609 sizeof(struct smb2_fs_full_size_info),
3610 persistent_fid, volatile_fid);
3614 if (encryption_required(tcon))
3615 flags |= CIFS_TRANSFORM_REQ;
3617 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
3618 cifs_small_buf_release(iov.iov_base);
3620 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3623 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3625 info = (struct smb2_fs_full_size_info *)(server->vals->header_preamble_size +
3626 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3627 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3628 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3629 sizeof(struct smb2_fs_full_size_info));
3631 copy_fs_info_to_kstatfs(info, fsdata);
3634 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3639 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
3640 u64 persistent_fid, u64 volatile_fid, int level)
3642 struct smb2_query_info_rsp *rsp = NULL;
3644 struct kvec rsp_iov;
3646 int resp_buftype, max_len, min_len;
3647 struct cifs_ses *ses = tcon->ses;
3648 struct TCP_Server_Info *server = ses->server;
3649 unsigned int rsp_len, offset;
3652 if (level == FS_DEVICE_INFORMATION) {
3653 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3654 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3655 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3656 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3657 min_len = MIN_FS_ATTR_INFO_SIZE;
3658 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3659 max_len = sizeof(struct smb3_fs_ss_info);
3660 min_len = sizeof(struct smb3_fs_ss_info);
3662 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
3666 rc = build_qfs_info_req(&iov, tcon, level, max_len,
3667 persistent_fid, volatile_fid);
3671 if (encryption_required(tcon))
3672 flags |= CIFS_TRANSFORM_REQ;
3674 rc = smb2_send_recv(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
3675 cifs_small_buf_release(iov.iov_base);
3677 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3680 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3682 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3683 offset = le16_to_cpu(rsp->OutputBufferOffset);
3684 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3688 if (level == FS_ATTRIBUTE_INFORMATION)
3689 memcpy(&tcon->fsAttrInfo, server->vals->header_preamble_size + offset
3690 + (char *)&rsp->hdr, min_t(unsigned int,
3692 else if (level == FS_DEVICE_INFORMATION)
3693 memcpy(&tcon->fsDevInfo, server->vals->header_preamble_size + offset
3694 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
3695 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3696 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3697 (server->vals->header_preamble_size + offset + (char *)&rsp->hdr);
3698 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3699 tcon->perf_sector_size =
3700 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3704 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3709 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3710 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3711 const __u32 num_lock, struct smb2_lock_element *buf)
3714 struct smb2_lock_req *req = NULL;
3716 struct kvec rsp_iov;
3719 int flags = CIFS_NO_RESP;
3720 unsigned int total_len;
3722 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
3724 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
3728 if (encryption_required(tcon))
3729 flags |= CIFS_TRANSFORM_REQ;
3731 req->sync_hdr.ProcessId = cpu_to_le32(pid);
3732 req->LockCount = cpu_to_le16(num_lock);
3734 req->PersistentFileId = persist_fid;
3735 req->VolatileFileId = volatile_fid;
3737 count = num_lock * sizeof(struct smb2_lock_element);
3739 iov[0].iov_base = (char *)req;
3740 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
3741 iov[1].iov_base = (char *)buf;
3742 iov[1].iov_len = count;
3744 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3745 rc = smb2_send_recv(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3747 cifs_small_buf_release(req);
3749 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
3750 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3757 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3758 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3759 const __u64 length, const __u64 offset, const __u32 lock_flags,
3762 struct smb2_lock_element lock;
3764 lock.Offset = cpu_to_le64(offset);
3765 lock.Length = cpu_to_le64(length);
3766 lock.Flags = cpu_to_le32(lock_flags);
3767 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3768 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3770 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3774 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3775 __u8 *lease_key, const __le32 lease_state)
3778 struct smb2_lease_ack *req = NULL;
3779 struct cifs_ses *ses = tcon->ses;
3780 int flags = CIFS_OBREAK_OP;
3781 unsigned int total_len;
3783 struct kvec rsp_iov;
3786 cifs_dbg(FYI, "SMB2_lease_break\n");
3787 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
3792 if (encryption_required(tcon))
3793 flags |= CIFS_TRANSFORM_REQ;
3795 req->sync_hdr.CreditRequest = cpu_to_le16(1);
3796 req->StructureSize = cpu_to_le16(36);
3799 memcpy(req->LeaseKey, lease_key, 16);
3800 req->LeaseState = lease_state;
3802 flags |= CIFS_NO_RESP;
3804 iov[0].iov_base = (char *)req;
3805 iov[0].iov_len = total_len;
3807 rc = smb2_send_recv(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
3808 cifs_small_buf_release(req);
3811 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3812 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);