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/pagemap.h>
37 #include <linux/xattr.h>
41 #include "cifsproto.h"
42 #include "smb2proto.h"
43 #include "cifs_unicode.h"
44 #include "cifs_debug.h"
46 #include "smb2status.h"
49 #include "cifs_spnego.h"
52 * The following table defines the expected "StructureSize" of SMB2 requests
53 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
55 * Note that commands are defined in smb2pdu.h in le16 but the array below is
56 * indexed by command in host byte order.
58 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
59 /* SMB2_NEGOTIATE */ 36,
60 /* SMB2_SESSION_SETUP */ 25,
62 /* SMB2_TREE_CONNECT */ 9,
63 /* SMB2_TREE_DISCONNECT */ 4,
73 /* SMB2_QUERY_DIRECTORY */ 33,
74 /* SMB2_CHANGE_NOTIFY */ 32,
75 /* SMB2_QUERY_INFO */ 41,
76 /* SMB2_SET_INFO */ 33,
77 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
82 smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
83 const struct cifs_tcon *tcon)
85 shdr->ProtocolId = SMB2_PROTO_NUMBER;
86 shdr->StructureSize = cpu_to_le16(64);
87 shdr->Command = smb2_cmd;
88 if (tcon && tcon->ses && tcon->ses->server) {
89 struct TCP_Server_Info *server = tcon->ses->server;
91 spin_lock(&server->req_lock);
92 /* Request up to 2 credits but don't go over the limit. */
93 if (server->credits >= server->max_credits)
94 shdr->CreditRequest = cpu_to_le16(0);
96 shdr->CreditRequest = cpu_to_le16(
97 min_t(int, server->max_credits -
99 spin_unlock(&server->req_lock);
101 shdr->CreditRequest = cpu_to_le16(2);
103 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
108 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
109 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
110 if ((tcon->ses) && (tcon->ses->server) &&
111 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
112 shdr->CreditCharge = cpu_to_le16(1);
113 /* else CreditCharge MBZ */
115 shdr->TreeId = tcon->tid;
116 /* Uid is not converted */
118 shdr->SessionId = tcon->ses->Suid;
121 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
122 * to pass the path on the Open SMB prefixed by \\server\share.
123 * Not sure when we would need to do the augmented path (if ever) and
124 * setting this flag breaks the SMB2 open operation since it is
125 * illegal to send an empty path name (without \\server\share prefix)
126 * when the DFS flag is set in the SMB open header. We could
127 * consider setting the flag on all operations other than open
128 * but it is safer to net set it for now.
130 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
131 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
133 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign)
134 shdr->Flags |= SMB2_FLAGS_SIGNED;
140 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
143 struct nls_table *nls_codepage;
144 struct cifs_ses *ses;
145 struct TCP_Server_Info *server;
148 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
149 * check for tcp and smb session status done differently
150 * for those three - in the calling routine.
155 if (smb2_command == SMB2_TREE_CONNECT)
158 if (tcon->tidStatus == CifsExiting) {
160 * only tree disconnect, open, and write,
161 * (and ulogoff which does not have tcon)
162 * are allowed as we start force umount.
164 if ((smb2_command != SMB2_WRITE) &&
165 (smb2_command != SMB2_CREATE) &&
166 (smb2_command != SMB2_TREE_DISCONNECT)) {
167 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
172 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
173 (!tcon->ses->server))
177 server = ses->server;
180 * Give demultiplex thread up to 10 seconds to reconnect, should be
181 * greater than cifs socket timeout which is 7 seconds
183 while (server->tcpStatus == CifsNeedReconnect) {
185 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
186 * here since they are implicitly done when session drops.
188 switch (smb2_command) {
190 * BB Should we keep oplock break and add flush to exceptions?
192 case SMB2_TREE_DISCONNECT:
195 case SMB2_OPLOCK_BREAK:
199 wait_event_interruptible_timeout(server->response_q,
200 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
202 /* are we still trying to reconnect? */
203 if (server->tcpStatus != CifsNeedReconnect)
207 * on "soft" mounts we wait once. Hard mounts keep
208 * retrying until process is killed or server comes
212 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
217 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
220 nls_codepage = load_nls_default();
223 * need to prevent multiple threads trying to simultaneously reconnect
224 * the same SMB session
226 mutex_lock(&tcon->ses->session_mutex);
227 rc = cifs_negotiate_protocol(0, tcon->ses);
228 if (!rc && tcon->ses->need_reconnect)
229 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
231 if (rc || !tcon->need_reconnect) {
232 mutex_unlock(&tcon->ses->session_mutex);
236 cifs_mark_open_files_invalid(tcon);
237 if (tcon->use_persistent)
238 tcon->need_reopen_files = true;
240 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
241 mutex_unlock(&tcon->ses->session_mutex);
243 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
247 if (smb2_command != SMB2_INTERNAL_CMD)
248 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
250 atomic_inc(&tconInfoReconnectCount);
253 * Check if handle based operation so we know whether we can continue
254 * or not without returning to caller to reset file handle.
257 * BB Is flush done by server on drop of tcp session? Should we special
258 * case it and skip above?
260 switch (smb2_command) {
266 case SMB2_QUERY_DIRECTORY:
267 case SMB2_CHANGE_NOTIFY:
268 case SMB2_QUERY_INFO:
272 unload_nls(nls_codepage);
277 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
278 unsigned int *total_len)
280 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
281 /* lookup word count ie StructureSize from table */
282 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
285 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
286 * largest operations (Create)
290 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
291 spdu->StructureSize2 = cpu_to_le16(parmsize);
293 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
297 * Allocate and return pointer to an SMB request hdr, and set basic
298 * SMB information in the SMB header. If the return code is zero, this
299 * function must have filled in request_buf pointer.
302 small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
306 unsigned int total_len;
307 struct smb2_pdu *pdu;
309 rc = smb2_reconnect(smb2_command, tcon);
313 /* BB eventually switch this to SMB2 specific small buf size */
314 *request_buf = cifs_small_buf_get();
315 if (*request_buf == NULL) {
316 /* BB should we add a retry in here if not a writepage? */
320 pdu = (struct smb2_pdu *)(*request_buf);
322 fill_small_buf(smb2_command, tcon, get_sync_hdr(pdu), &total_len);
324 /* Note this is only network field converted to big endian */
325 pdu->hdr.smb2_buf_length = cpu_to_be32(total_len);
328 #ifdef CONFIG_CIFS_STATS2
329 uint16_t com_code = le16_to_cpu(smb2_command);
330 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
332 cifs_stats_inc(&tcon->num_smbs_sent);
338 #ifdef CONFIG_CIFS_SMB311
339 /* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
340 #define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
343 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
344 #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
347 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
349 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
350 pneg_ctxt->DataLength = cpu_to_le16(38);
351 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
352 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
353 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
354 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
358 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
360 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
361 pneg_ctxt->DataLength = cpu_to_le16(6);
362 pneg_ctxt->CipherCount = cpu_to_le16(2);
363 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
364 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
368 assemble_neg_contexts(struct smb2_negotiate_req *req)
371 /* +4 is to account for the RFC1001 len field */
372 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
374 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
375 /* Add 2 to size to round to 8 byte boundary */
376 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
377 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
378 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
379 req->NegotiateContextCount = cpu_to_le16(2);
380 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2
381 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
384 static void assemble_neg_contexts(struct smb2_negotiate_req *req)
393 * SMB2 Worker functions follow:
395 * The general structure of the worker functions is:
396 * 1) Call smb2_init (assembles SMB2 header)
397 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
398 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
399 * 4) Decode SMB2 command specific fields in the fixed length area
400 * 5) Decode variable length data area (if any for this SMB2 command type)
401 * 6) Call free smb buffer
407 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
409 struct smb2_negotiate_req *req;
410 struct smb2_negotiate_rsp *rsp;
415 struct TCP_Server_Info *server = ses->server;
416 int blob_offset, blob_length;
418 int flags = CIFS_NEG_OP;
420 cifs_dbg(FYI, "Negotiate protocol\n");
423 WARN(1, "%s: server is NULL!\n", __func__);
427 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
431 req->hdr.sync_hdr.SessionId = 0;
433 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
435 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
436 inc_rfc1001_len(req, 2);
438 /* only one of SMB2 signing flags may be set in SMB2 request */
440 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
441 else if (global_secflags & CIFSSEC_MAY_SIGN)
442 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
444 req->SecurityMode = 0;
446 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
448 /* ClientGUID must be zero for SMB2.02 dialect */
449 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
450 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
452 memcpy(req->ClientGUID, server->client_guid,
453 SMB2_CLIENT_GUID_SIZE);
454 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
455 assemble_neg_contexts(req);
457 iov[0].iov_base = (char *)req;
458 /* 4 for rfc1002 length field */
459 iov[0].iov_len = get_rfc1002_length(req) + 4;
461 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
462 cifs_small_buf_release(req);
463 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
465 * No tcon so can't do
466 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
471 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
473 /* BB we may eventually want to match the negotiated vs. requested
474 dialect, even though we are only requesting one at a time */
475 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
476 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
477 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
478 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
479 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
480 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
481 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
482 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
483 #ifdef CONFIG_CIFS_SMB311
484 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
485 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
488 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
489 le16_to_cpu(rsp->DialectRevision));
493 server->dialect = le16_to_cpu(rsp->DialectRevision);
495 /* SMB2 only has an extended negflavor */
496 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
497 /* set it to the maximum buffer size value we can send with 1 credit */
498 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
499 SMB2_MAX_BUFFER_SIZE);
500 server->max_read = le32_to_cpu(rsp->MaxReadSize);
501 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
502 /* BB Do we need to validate the SecurityMode? */
503 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
504 server->capabilities = le32_to_cpu(rsp->Capabilities);
506 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
508 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
511 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
513 * ses->sectype = RawNTLMSSP;
514 * but for time being this is our only auth choice so doesn't matter.
515 * We just found a server which sets blob length to zero expecting raw.
517 if (blob_length == 0)
518 cifs_dbg(FYI, "missing security blob on negprot\n");
520 rc = cifs_enable_signing(server, ses->sign);
524 rc = decode_negTokenInit(security_blob, blob_length, server);
531 free_rsp_buf(resp_buftype, rsp);
535 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
538 struct validate_negotiate_info_req vneg_inbuf;
539 struct validate_negotiate_info_rsp *pneg_rsp;
542 cifs_dbg(FYI, "validate negotiate\n");
545 * validation ioctl must be signed, so no point sending this if we
546 * can not sign it. We could eventually change this to selectively
547 * sign just this, the first and only signed request on a connection.
548 * This is good enough for now since a user who wants better security
549 * would also enable signing on the mount. Having validation of
550 * negotiate info for signed connections helps reduce attack vectors
552 if (tcon->ses->server->sign == false)
553 return 0; /* validation requires signing */
555 vneg_inbuf.Capabilities =
556 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
557 memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid,
558 SMB2_CLIENT_GUID_SIZE);
561 vneg_inbuf.SecurityMode =
562 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
563 else if (global_secflags & CIFSSEC_MAY_SIGN)
564 vneg_inbuf.SecurityMode =
565 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
567 vneg_inbuf.SecurityMode = 0;
569 vneg_inbuf.DialectCount = cpu_to_le16(1);
570 vneg_inbuf.Dialects[0] =
571 cpu_to_le16(tcon->ses->server->vals->protocol_id);
573 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
574 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
575 (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req),
576 (char **)&pneg_rsp, &rsplen);
579 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
583 if (rsplen != sizeof(struct validate_negotiate_info_rsp)) {
584 cifs_dbg(VFS, "invalid size of protocol negotiate response\n");
588 /* check validate negotiate info response matches what we got earlier */
589 if (pneg_rsp->Dialect !=
590 cpu_to_le16(tcon->ses->server->vals->protocol_id))
593 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
596 /* do not validate server guid because not saved at negprot time yet */
598 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
599 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
602 /* validate negotiate successful */
603 cifs_dbg(FYI, "validate negotiate info successful\n");
607 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
611 struct SMB2_sess_data {
613 struct cifs_ses *ses;
614 struct nls_table *nls_cp;
615 void (*func)(struct SMB2_sess_data *);
617 u64 previous_session;
619 /* we will send the SMB in three pieces:
620 * a fixed length beginning part, an optional
621 * SPNEGO blob (which can be zero length), and a
622 * last part which will include the strings
623 * and rest of bcc area. This allows us to avoid
624 * a large buffer 17K allocation
631 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
634 struct cifs_ses *ses = sess_data->ses;
635 struct smb2_sess_setup_req *req;
636 struct TCP_Server_Info *server = ses->server;
638 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
642 /* First session, not a reauthenticate */
643 req->hdr.sync_hdr.SessionId = 0;
645 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
646 req->PreviousSessionId = sess_data->previous_session;
648 req->Flags = 0; /* MBZ */
649 /* to enable echos and oplocks */
650 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(3);
652 /* only one of SMB2 signing flags may be set in SMB2 request */
654 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
655 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
656 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
658 req->SecurityMode = 0;
660 req->Capabilities = 0;
661 req->Channel = 0; /* MBZ */
663 sess_data->iov[0].iov_base = (char *)req;
664 /* 4 for rfc1002 length field and 1 for pad */
665 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
667 * This variable will be used to clear the buffer
668 * allocated above in case of any error in the calling function.
670 sess_data->buf0_type = CIFS_SMALL_BUFFER;
676 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
678 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
679 sess_data->buf0_type = CIFS_NO_BUFFER;
683 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
686 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
687 struct kvec rsp_iov = { NULL, 0 };
689 /* Testing shows that buffer offset must be at location of Buffer[0] */
690 req->SecurityBufferOffset =
691 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
692 1 /* pad */ - 4 /* rfc1001 len */);
693 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
695 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
697 /* BB add code to build os and lm fields */
699 rc = SendReceive2(sess_data->xid, sess_data->ses,
701 &sess_data->buf0_type,
702 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
703 cifs_small_buf_release(sess_data->iov[0].iov_base);
704 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
710 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
713 struct cifs_ses *ses = sess_data->ses;
715 mutex_lock(&ses->server->srv_mutex);
716 if (ses->server->sign && ses->server->ops->generate_signingkey) {
717 rc = ses->server->ops->generate_signingkey(ses);
718 kfree(ses->auth_key.response);
719 ses->auth_key.response = NULL;
722 "SMB3 session key generation failed\n");
723 mutex_unlock(&ses->server->srv_mutex);
727 if (!ses->server->session_estab) {
728 ses->server->sequence_number = 0x2;
729 ses->server->session_estab = true;
731 mutex_unlock(&ses->server->srv_mutex);
733 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
734 spin_lock(&GlobalMid_Lock);
735 ses->status = CifsGood;
736 ses->need_reconnect = false;
737 spin_unlock(&GlobalMid_Lock);
740 if (!ses->server->sign) {
741 kfree(ses->auth_key.response);
742 ses->auth_key.response = NULL;
747 #ifdef CONFIG_CIFS_UPCALL
749 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
752 struct cifs_ses *ses = sess_data->ses;
753 struct cifs_spnego_msg *msg;
754 struct key *spnego_key = NULL;
755 struct smb2_sess_setup_rsp *rsp = NULL;
757 rc = SMB2_sess_alloc_buffer(sess_data);
761 spnego_key = cifs_get_spnego_key(ses);
762 if (IS_ERR(spnego_key)) {
763 rc = PTR_ERR(spnego_key);
768 msg = spnego_key->payload.data[0];
770 * check version field to make sure that cifs.upcall is
771 * sending us a response in an expected form
773 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
775 "bad cifs.upcall version. Expected %d got %d",
776 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
778 goto out_put_spnego_key;
781 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
783 if (!ses->auth_key.response) {
785 "Kerberos can't allocate (%u bytes) memory",
788 goto out_put_spnego_key;
790 ses->auth_key.len = msg->sesskey_len;
792 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
793 sess_data->iov[1].iov_len = msg->secblob_len;
795 rc = SMB2_sess_sendreceive(sess_data);
797 goto out_put_spnego_key;
799 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
800 ses->Suid = rsp->hdr.sync_hdr.SessionId;
802 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
803 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
804 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
806 rc = SMB2_sess_establish_session(sess_data);
808 key_invalidate(spnego_key);
811 sess_data->result = rc;
812 sess_data->func = NULL;
813 SMB2_sess_free_buffer(sess_data);
817 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
819 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
820 sess_data->result = -EOPNOTSUPP;
821 sess_data->func = NULL;
826 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
829 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
832 struct cifs_ses *ses = sess_data->ses;
833 struct smb2_sess_setup_rsp *rsp = NULL;
834 char *ntlmssp_blob = NULL;
835 bool use_spnego = false; /* else use raw ntlmssp */
839 * If memory allocation is successful, caller of this function
842 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
847 ses->ntlmssp->sesskey_per_smbsess = true;
849 rc = SMB2_sess_alloc_buffer(sess_data);
853 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
855 if (ntlmssp_blob == NULL) {
860 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
862 /* BB eventually need to add this */
863 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
867 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
868 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
870 sess_data->iov[1].iov_base = ntlmssp_blob;
871 sess_data->iov[1].iov_len = blob_length;
873 rc = SMB2_sess_sendreceive(sess_data);
874 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
876 /* If true, rc here is expected and not an error */
877 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
878 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
884 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
885 le16_to_cpu(rsp->SecurityBufferOffset)) {
886 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
887 le16_to_cpu(rsp->SecurityBufferOffset));
891 rc = decode_ntlmssp_challenge(rsp->Buffer,
892 le16_to_cpu(rsp->SecurityBufferLength), ses);
896 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
899 ses->Suid = rsp->hdr.sync_hdr.SessionId;
900 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
901 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
902 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
906 SMB2_sess_free_buffer(sess_data);
908 sess_data->result = 0;
909 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
915 sess_data->result = rc;
916 sess_data->func = NULL;
920 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
923 struct cifs_ses *ses = sess_data->ses;
924 struct smb2_sess_setup_req *req;
925 struct smb2_sess_setup_rsp *rsp = NULL;
926 unsigned char *ntlmssp_blob = NULL;
927 bool use_spnego = false; /* else use raw ntlmssp */
930 rc = SMB2_sess_alloc_buffer(sess_data);
934 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
935 req->hdr.sync_hdr.SessionId = ses->Suid;
937 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
940 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
945 /* BB eventually need to add this */
946 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
950 sess_data->iov[1].iov_base = ntlmssp_blob;
951 sess_data->iov[1].iov_len = blob_length;
953 rc = SMB2_sess_sendreceive(sess_data);
957 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
959 ses->Suid = rsp->hdr.sync_hdr.SessionId;
960 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
961 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
962 cifs_dbg(VFS, "SMB3 encryption not supported yet\n");
964 rc = SMB2_sess_establish_session(sess_data);
967 SMB2_sess_free_buffer(sess_data);
970 sess_data->result = rc;
971 sess_data->func = NULL;
975 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
977 if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP)
978 ses->sectype = RawNTLMSSP;
980 switch (ses->sectype) {
982 sess_data->func = SMB2_auth_kerberos;
985 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
988 cifs_dbg(VFS, "secType %d not supported!\n", ses->sectype);
996 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
997 const struct nls_table *nls_cp)
1000 struct TCP_Server_Info *server = ses->server;
1001 struct SMB2_sess_data *sess_data;
1003 cifs_dbg(FYI, "Session Setup\n");
1006 WARN(1, "%s: server is NULL!\n", __func__);
1010 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1014 rc = SMB2_select_sec(ses, sess_data);
1017 sess_data->xid = xid;
1018 sess_data->ses = ses;
1019 sess_data->buf0_type = CIFS_NO_BUFFER;
1020 sess_data->nls_cp = (struct nls_table *) nls_cp;
1022 while (sess_data->func)
1023 sess_data->func(sess_data);
1025 rc = sess_data->result;
1032 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1034 struct smb2_logoff_req *req; /* response is also trivial struct */
1036 struct TCP_Server_Info *server;
1038 cifs_dbg(FYI, "disconnect session %p\n", ses);
1040 if (ses && (ses->server))
1041 server = ses->server;
1045 /* no need to send SMB logoff if uid already closed due to reconnect */
1046 if (ses->need_reconnect)
1047 goto smb2_session_already_dead;
1049 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1053 /* since no tcon, smb2_init can not do this, so do here */
1054 req->hdr.sync_hdr.SessionId = ses->Suid;
1056 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1058 rc = SendReceiveNoRsp(xid, ses, (char *) req, 0);
1059 cifs_small_buf_release(req);
1061 * No tcon so can't do
1062 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1065 smb2_session_already_dead:
1069 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1071 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1074 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1076 /* These are similar values to what Windows uses */
1077 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1079 tcon->max_chunks = 256;
1080 tcon->max_bytes_chunk = 1048576;
1081 tcon->max_bytes_copy = 16777216;
1085 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1086 struct cifs_tcon *tcon, const struct nls_table *cp)
1088 struct smb2_tree_connect_req *req;
1089 struct smb2_tree_connect_rsp *rsp = NULL;
1091 struct kvec rsp_iov;
1095 struct TCP_Server_Info *server;
1096 __le16 *unc_path = NULL;
1098 cifs_dbg(FYI, "TCON\n");
1100 if ((ses->server) && tree)
1101 server = ses->server;
1105 if (tcon && tcon->bad_network_name)
1108 if ((tcon && tcon->seal) &&
1109 ((ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) == 0)) {
1110 cifs_dbg(VFS, "encryption requested but no server support");
1114 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1115 if (unc_path == NULL)
1118 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1120 if (unc_path_len < 2) {
1125 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1132 /* since no tcon, smb2_init can not do this, so do here */
1133 req->hdr.sync_hdr.SessionId = ses->Suid;
1134 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
1135 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
1138 iov[0].iov_base = (char *)req;
1139 /* 4 for rfc1002 length field and 1 for pad */
1140 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1142 /* Testing shows that buffer offset must be at location of Buffer[0] */
1143 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1144 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1145 req->PathLength = cpu_to_le16(unc_path_len - 2);
1146 iov[1].iov_base = unc_path;
1147 iov[1].iov_len = unc_path_len;
1149 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1151 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0, &rsp_iov);
1152 cifs_small_buf_release(req);
1153 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1157 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1158 tcon->need_reconnect = true;
1160 goto tcon_error_exit;
1164 ses->ipc_tid = rsp->hdr.sync_hdr.TreeId;
1168 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
1169 cifs_dbg(FYI, "connection to disk share\n");
1170 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
1172 cifs_dbg(FYI, "connection to pipe share\n");
1173 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
1175 cifs_dbg(FYI, "connection to printer\n");
1177 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1179 goto tcon_error_exit;
1182 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1183 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1184 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1185 tcon->tidStatus = CifsGood;
1186 tcon->need_reconnect = false;
1187 tcon->tid = rsp->hdr.sync_hdr.TreeId;
1188 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1190 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1191 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1192 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1193 init_copy_chunk_defaults(tcon);
1194 if (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)
1195 cifs_dbg(VFS, "Encrypted shares not supported");
1196 if (tcon->ses->server->ops->validate_negotiate)
1197 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1199 free_rsp_buf(resp_buftype, rsp);
1204 if (rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1205 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1207 tcon->bad_network_name = true;
1213 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1215 struct smb2_tree_disconnect_req *req; /* response is trivial */
1217 struct TCP_Server_Info *server;
1218 struct cifs_ses *ses = tcon->ses;
1220 cifs_dbg(FYI, "Tree Disconnect\n");
1222 if (ses && (ses->server))
1223 server = ses->server;
1227 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1230 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1234 rc = SendReceiveNoRsp(xid, ses, (char *)req, 0);
1235 cifs_small_buf_release(req);
1237 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1243 static struct create_durable *
1244 create_durable_buf(void)
1246 struct create_durable *buf;
1248 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1252 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1253 (struct create_durable, Data));
1254 buf->ccontext.DataLength = cpu_to_le32(16);
1255 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1256 (struct create_durable, Name));
1257 buf->ccontext.NameLength = cpu_to_le16(4);
1258 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1266 static struct create_durable *
1267 create_reconnect_durable_buf(struct cifs_fid *fid)
1269 struct create_durable *buf;
1271 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1275 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1276 (struct create_durable, Data));
1277 buf->ccontext.DataLength = cpu_to_le32(16);
1278 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1279 (struct create_durable, Name));
1280 buf->ccontext.NameLength = cpu_to_le16(4);
1281 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1282 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1283 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1292 parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1293 unsigned int *epoch)
1296 struct create_context *cc;
1298 unsigned int remaining;
1301 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
1302 remaining = le32_to_cpu(rsp->CreateContextsLength);
1303 cc = (struct create_context *)data_offset;
1304 while (remaining >= sizeof(struct create_context)) {
1305 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1306 if (le16_to_cpu(cc->NameLength) == 4 &&
1307 strncmp(name, "RqLs", 4) == 0)
1308 return server->ops->parse_lease_buf(cc, epoch);
1310 next = le32_to_cpu(cc->Next);
1314 cc = (struct create_context *)((char *)cc + next);
1321 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1322 unsigned int *num_iovec, __u8 *oplock)
1324 struct smb2_create_req *req = iov[0].iov_base;
1325 unsigned int num = *num_iovec;
1327 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
1328 if (iov[num].iov_base == NULL)
1330 iov[num].iov_len = server->vals->create_lease_size;
1331 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1332 if (!req->CreateContextsOffset)
1333 req->CreateContextsOffset = cpu_to_le32(
1334 sizeof(struct smb2_create_req) - 4 +
1335 iov[num - 1].iov_len);
1336 le32_add_cpu(&req->CreateContextsLength,
1337 server->vals->create_lease_size);
1338 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
1339 *num_iovec = num + 1;
1343 static struct create_durable_v2 *
1344 create_durable_v2_buf(struct cifs_fid *pfid)
1346 struct create_durable_v2 *buf;
1348 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1352 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1353 (struct create_durable_v2, dcontext));
1354 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1355 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1356 (struct create_durable_v2, Name));
1357 buf->ccontext.NameLength = cpu_to_le16(4);
1359 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1360 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1361 generate_random_uuid(buf->dcontext.CreateGuid);
1362 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1364 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1372 static struct create_durable_handle_reconnect_v2 *
1373 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1375 struct create_durable_handle_reconnect_v2 *buf;
1377 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1382 buf->ccontext.DataOffset =
1383 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1385 buf->ccontext.DataLength =
1386 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1387 buf->ccontext.NameOffset =
1388 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1390 buf->ccontext.NameLength = cpu_to_le16(4);
1392 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1393 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1394 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1395 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1397 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1406 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1407 struct cifs_open_parms *oparms)
1409 struct smb2_create_req *req = iov[0].iov_base;
1410 unsigned int num = *num_iovec;
1412 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1413 if (iov[num].iov_base == NULL)
1415 iov[num].iov_len = sizeof(struct create_durable_v2);
1416 if (!req->CreateContextsOffset)
1417 req->CreateContextsOffset =
1418 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1420 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1421 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1422 *num_iovec = num + 1;
1427 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1428 struct cifs_open_parms *oparms)
1430 struct smb2_create_req *req = iov[0].iov_base;
1431 unsigned int num = *num_iovec;
1433 /* indicate that we don't need to relock the file */
1434 oparms->reconnect = false;
1436 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1437 if (iov[num].iov_base == NULL)
1439 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1440 if (!req->CreateContextsOffset)
1441 req->CreateContextsOffset =
1442 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1444 le32_add_cpu(&req->CreateContextsLength,
1445 sizeof(struct create_durable_handle_reconnect_v2));
1446 inc_rfc1001_len(&req->hdr,
1447 sizeof(struct create_durable_handle_reconnect_v2));
1448 *num_iovec = num + 1;
1453 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1454 struct cifs_open_parms *oparms, bool use_persistent)
1456 struct smb2_create_req *req = iov[0].iov_base;
1457 unsigned int num = *num_iovec;
1459 if (use_persistent) {
1460 if (oparms->reconnect)
1461 return add_durable_reconnect_v2_context(iov, num_iovec,
1464 return add_durable_v2_context(iov, num_iovec, oparms);
1467 if (oparms->reconnect) {
1468 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1469 /* indicate that we don't need to relock the file */
1470 oparms->reconnect = false;
1472 iov[num].iov_base = create_durable_buf();
1473 if (iov[num].iov_base == NULL)
1475 iov[num].iov_len = sizeof(struct create_durable);
1476 if (!req->CreateContextsOffset)
1477 req->CreateContextsOffset =
1478 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1480 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1481 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1482 *num_iovec = num + 1;
1487 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
1488 __u8 *oplock, struct smb2_file_all_info *buf,
1489 struct smb2_err_rsp **err_buf)
1491 struct smb2_create_req *req;
1492 struct smb2_create_rsp *rsp;
1493 struct TCP_Server_Info *server;
1494 struct cifs_tcon *tcon = oparms->tcon;
1495 struct cifs_ses *ses = tcon->ses;
1497 struct kvec rsp_iov;
1500 __le16 *copy_path = NULL;
1503 unsigned int n_iov = 2;
1504 __u32 file_attributes = 0;
1505 char *dhc_buf = NULL, *lc_buf = NULL;
1507 cifs_dbg(FYI, "create/open\n");
1509 if (ses && (ses->server))
1510 server = ses->server;
1514 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1518 if (oparms->create_options & CREATE_OPTION_READONLY)
1519 file_attributes |= ATTR_READONLY;
1520 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1521 file_attributes |= ATTR_SYSTEM;
1523 req->ImpersonationLevel = IL_IMPERSONATION;
1524 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
1525 /* File attributes ignored on open (used in create though) */
1526 req->FileAttributes = cpu_to_le32(file_attributes);
1527 req->ShareAccess = FILE_SHARE_ALL_LE;
1528 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1529 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
1530 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1531 /* do not count rfc1001 len field */
1532 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1534 iov[0].iov_base = (char *)req;
1535 /* 4 for rfc1002 length field */
1536 iov[0].iov_len = get_rfc1002_length(req) + 4;
1538 /* MUST set path len (NameLength) to 0 opening root of share */
1539 req->NameLength = cpu_to_le16(uni_path_len - 2);
1540 /* -1 since last byte is buf[0] which is sent below (path) */
1542 if (uni_path_len % 8 != 0) {
1543 copy_size = uni_path_len / 8 * 8;
1544 if (copy_size < uni_path_len)
1547 copy_path = kzalloc(copy_size, GFP_KERNEL);
1550 memcpy((char *)copy_path, (const char *)path,
1552 uni_path_len = copy_size;
1556 iov[1].iov_len = uni_path_len;
1557 iov[1].iov_base = path;
1558 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1559 inc_rfc1001_len(req, uni_path_len - 1);
1561 if (!server->oplocks)
1562 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1564 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1565 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1566 req->RequestedOplockLevel = *oplock;
1568 rc = add_lease_context(server, iov, &n_iov, oplock);
1570 cifs_small_buf_release(req);
1574 lc_buf = iov[n_iov-1].iov_base;
1577 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1578 /* need to set Next field of lease context if we request it */
1579 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
1580 struct create_context *ccontext =
1581 (struct create_context *)iov[n_iov-1].iov_base;
1583 cpu_to_le32(server->vals->create_lease_size);
1586 rc = add_durable_context(iov, &n_iov, oparms,
1587 tcon->use_persistent);
1589 cifs_small_buf_release(req);
1594 dhc_buf = iov[n_iov-1].iov_base;
1597 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, 0, &rsp_iov);
1598 cifs_small_buf_release(req);
1599 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
1602 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
1604 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1609 oparms->fid->persistent_fid = rsp->PersistentFileId;
1610 oparms->fid->volatile_fid = rsp->VolatileFileId;
1613 memcpy(buf, &rsp->CreationTime, 32);
1614 buf->AllocationSize = rsp->AllocationSize;
1615 buf->EndOfFile = rsp->EndofFile;
1616 buf->Attributes = rsp->FileAttributes;
1617 buf->NumberOfLinks = cpu_to_le32(1);
1618 buf->DeletePending = 0;
1621 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
1622 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
1624 *oplock = rsp->OplockLevel;
1629 free_rsp_buf(resp_buftype, rsp);
1634 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1637 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1638 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1639 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1641 struct smb2_ioctl_req *req;
1642 struct smb2_ioctl_rsp *rsp;
1643 struct smb2_sync_hdr *shdr;
1644 struct TCP_Server_Info *server;
1645 struct cifs_ses *ses;
1647 struct kvec rsp_iov;
1652 cifs_dbg(FYI, "SMB2 IOCTL\n");
1654 if (out_data != NULL)
1657 /* zero out returned data len, in case of error */
1666 if (ses && (ses->server))
1667 server = ses->server;
1671 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1675 req->CtlCode = cpu_to_le32(opcode);
1676 req->PersistentFileId = persistent_fid;
1677 req->VolatileFileId = volatile_fid;
1680 req->InputCount = cpu_to_le32(indatalen);
1681 /* do not set InputOffset if no input data */
1683 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1684 iov[1].iov_base = in_data;
1685 iov[1].iov_len = indatalen;
1690 req->OutputOffset = 0;
1691 req->OutputCount = 0; /* MBZ */
1694 * Could increase MaxOutputResponse, but that would require more
1695 * than one credit. Windows typically sets this smaller, but for some
1696 * ioctls it may be useful to allow server to send more. No point
1697 * limiting what the server can send as long as fits in one credit
1699 req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1702 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1706 iov[0].iov_base = (char *)req;
1709 * If no input data, the size of ioctl struct in
1710 * protocol spec still includes a 1 byte data buffer,
1711 * but if input data passed to ioctl, we do not
1712 * want to double count this, so we do not send
1713 * the dummy one byte of data in iovec[0] if sending
1714 * input data (in iovec[1]). We also must add 4 bytes
1715 * in first iovec to allow for rfc1002 length field.
1719 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1720 inc_rfc1001_len(req, indatalen - 1);
1722 iov[0].iov_len = get_rfc1002_length(req) + 4;
1725 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, 0, &rsp_iov);
1726 cifs_small_buf_release(req);
1727 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
1729 if ((rc != 0) && (rc != -EINVAL)) {
1730 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1732 } else if (rc == -EINVAL) {
1733 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1734 (opcode != FSCTL_SRV_COPYCHUNK)) {
1735 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1740 /* check if caller wants to look at return data or just return rc */
1741 if ((plen == NULL) || (out_data == NULL))
1744 *plen = le32_to_cpu(rsp->OutputCount);
1746 /* We check for obvious errors in the output buffer length and offset */
1748 goto ioctl_exit; /* server returned no data */
1749 else if (*plen > 0xFF00) {
1750 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1756 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1757 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1758 le32_to_cpu(rsp->OutputOffset));
1764 *out_data = kmalloc(*plen, GFP_KERNEL);
1765 if (*out_data == NULL) {
1770 shdr = get_sync_hdr(rsp);
1771 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
1773 free_rsp_buf(resp_buftype, rsp);
1778 * Individual callers to ioctl worker function follow
1782 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1783 u64 persistent_fid, u64 volatile_fid)
1786 struct compress_ioctl fsctl_input;
1787 char *ret_data = NULL;
1789 fsctl_input.CompressionState =
1790 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
1792 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1793 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
1794 (char *)&fsctl_input /* data input */,
1795 2 /* in data len */, &ret_data /* out data */, NULL);
1797 cifs_dbg(FYI, "set compression rc %d\n", rc);
1803 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1804 u64 persistent_fid, u64 volatile_fid)
1806 struct smb2_close_req *req;
1807 struct smb2_close_rsp *rsp;
1808 struct TCP_Server_Info *server;
1809 struct cifs_ses *ses = tcon->ses;
1811 struct kvec rsp_iov;
1815 cifs_dbg(FYI, "Close\n");
1817 if (ses && (ses->server))
1818 server = ses->server;
1822 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1826 req->PersistentFileId = persistent_fid;
1827 req->VolatileFileId = volatile_fid;
1829 iov[0].iov_base = (char *)req;
1830 /* 4 for rfc1002 length field */
1831 iov[0].iov_len = get_rfc1002_length(req) + 4;
1833 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0, &rsp_iov);
1834 cifs_small_buf_release(req);
1835 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
1838 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
1842 /* BB FIXME - decode close response, update inode for caching */
1845 free_rsp_buf(resp_buftype, rsp);
1850 validate_buf(unsigned int offset, unsigned int buffer_length,
1851 struct smb2_hdr *hdr, unsigned int min_buf_size)
1854 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1855 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1856 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1857 char *end_of_buf = begin_of_buf + buffer_length;
1860 if (buffer_length < min_buf_size) {
1861 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1862 buffer_length, min_buf_size);
1866 /* check if beyond RFC1001 maximum length */
1867 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
1868 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1869 buffer_length, smb_len);
1873 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
1874 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
1882 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1883 * Caller must free buffer.
1886 validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1887 struct smb2_hdr *hdr, unsigned int minbufsize,
1891 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1897 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1901 memcpy(data, begin_of_buf, buffer_length);
1907 query_info(const unsigned int xid, struct cifs_tcon *tcon,
1908 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1909 size_t output_len, size_t min_len, void *data)
1911 struct smb2_query_info_req *req;
1912 struct smb2_query_info_rsp *rsp = NULL;
1914 struct kvec rsp_iov;
1917 struct TCP_Server_Info *server;
1918 struct cifs_ses *ses = tcon->ses;
1920 cifs_dbg(FYI, "Query Info\n");
1922 if (ses && (ses->server))
1923 server = ses->server;
1927 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1931 req->InfoType = SMB2_O_INFO_FILE;
1932 req->FileInfoClass = info_class;
1933 req->PersistentFileId = persistent_fid;
1934 req->VolatileFileId = volatile_fid;
1935 /* 4 for rfc1002 length field and 1 for Buffer */
1936 req->InputBufferOffset =
1937 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
1938 req->OutputBufferLength = cpu_to_le32(output_len);
1940 iov[0].iov_base = (char *)req;
1941 /* 4 for rfc1002 length field */
1942 iov[0].iov_len = get_rfc1002_length(req) + 4;
1944 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0, &rsp_iov);
1945 cifs_small_buf_release(req);
1946 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1949 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1953 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1954 le32_to_cpu(rsp->OutputBufferLength),
1955 &rsp->hdr, min_len, data);
1958 free_rsp_buf(resp_buftype, rsp);
1963 SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1964 u64 persistent_fid, u64 volatile_fid,
1965 struct smb2_file_all_info *data)
1967 return query_info(xid, tcon, persistent_fid, volatile_fid,
1968 FILE_ALL_INFORMATION,
1969 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
1970 sizeof(struct smb2_file_all_info), data);
1974 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1975 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1977 return query_info(xid, tcon, persistent_fid, volatile_fid,
1978 FILE_INTERNAL_INFORMATION,
1979 sizeof(struct smb2_file_internal_info),
1980 sizeof(struct smb2_file_internal_info), uniqueid);
1984 * This is a no-op for now. We're not really interested in the reply, but
1985 * rather in the fact that the server sent one and that server->lstrp
1988 * FIXME: maybe we should consider checking that the reply matches request?
1991 smb2_echo_callback(struct mid_q_entry *mid)
1993 struct TCP_Server_Info *server = mid->callback_data;
1994 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
1995 unsigned int credits_received = 1;
1997 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1998 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
2000 mutex_lock(&server->srv_mutex);
2001 DeleteMidQEntry(mid);
2002 mutex_unlock(&server->srv_mutex);
2003 add_credits(server, credits_received, CIFS_ECHO_OP);
2006 void smb2_reconnect_server(struct work_struct *work)
2008 struct TCP_Server_Info *server = container_of(work,
2009 struct TCP_Server_Info, reconnect.work);
2010 struct cifs_ses *ses;
2011 struct cifs_tcon *tcon, *tcon2;
2012 struct list_head tmp_list;
2013 int tcon_exist = false;
2015 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2016 mutex_lock(&server->reconnect_mutex);
2018 INIT_LIST_HEAD(&tmp_list);
2019 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2021 spin_lock(&cifs_tcp_ses_lock);
2022 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2023 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2024 if (tcon->need_reconnect || tcon->need_reopen_files) {
2026 list_add_tail(&tcon->rlist, &tmp_list);
2032 * Get the reference to server struct to be sure that the last call of
2033 * cifs_put_tcon() in the loop below won't release the server pointer.
2036 server->srv_count++;
2038 spin_unlock(&cifs_tcp_ses_lock);
2040 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
2041 if (!smb2_reconnect(SMB2_INTERNAL_CMD, tcon))
2042 cifs_reopen_persistent_handles(tcon);
2043 list_del_init(&tcon->rlist);
2044 cifs_put_tcon(tcon);
2047 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2048 mutex_unlock(&server->reconnect_mutex);
2050 /* now we can safely release srv struct */
2052 cifs_put_tcp_session(server, 1);
2056 SMB2_echo(struct TCP_Server_Info *server)
2058 struct smb2_echo_req *req;
2061 struct smb_rqst rqst = { .rq_iov = iov,
2064 cifs_dbg(FYI, "In echo request\n");
2066 if (server->tcpStatus == CifsNeedNegotiate) {
2067 /* No need to send echo on newly established connections */
2068 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2072 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2076 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
2078 /* 4 for rfc1002 length field */
2080 iov[0].iov_base = (char *)req;
2081 iov[1].iov_len = get_rfc1002_length(req);
2082 iov[1].iov_base = (char *)req + 4;
2084 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
2087 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
2089 cifs_small_buf_release(req);
2094 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2097 struct smb2_flush_req *req;
2098 struct TCP_Server_Info *server;
2099 struct cifs_ses *ses = tcon->ses;
2101 struct kvec rsp_iov;
2105 cifs_dbg(FYI, "Flush\n");
2107 if (ses && (ses->server))
2108 server = ses->server;
2112 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2116 req->PersistentFileId = persistent_fid;
2117 req->VolatileFileId = volatile_fid;
2119 iov[0].iov_base = (char *)req;
2120 /* 4 for rfc1002 length field */
2121 iov[0].iov_len = get_rfc1002_length(req) + 4;
2123 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0, &rsp_iov);
2124 cifs_small_buf_release(req);
2127 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2129 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2134 * To form a chain of read requests, any read requests after the first should
2135 * have the end_of_chain boolean set to true.
2138 smb2_new_read_req(void **buf, unsigned int *total_len,
2139 struct cifs_io_parms *io_parms, unsigned int remaining_bytes,
2143 struct smb2_read_req *req = NULL;
2144 struct smb2_sync_hdr *shdr;
2146 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
2149 if (io_parms->tcon->ses->server == NULL)
2150 return -ECONNABORTED;
2152 shdr = get_sync_hdr(req);
2153 shdr->ProcessId = cpu_to_le32(io_parms->pid);
2155 req->PersistentFileId = io_parms->persistent_fid;
2156 req->VolatileFileId = io_parms->volatile_fid;
2157 req->ReadChannelInfoOffset = 0; /* reserved */
2158 req->ReadChannelInfoLength = 0; /* reserved */
2159 req->Channel = 0; /* reserved */
2160 req->MinimumCount = 0;
2161 req->Length = cpu_to_le32(io_parms->length);
2162 req->Offset = cpu_to_le64(io_parms->offset);
2164 if (request_type & CHAINED_REQUEST) {
2165 if (!(request_type & END_OF_CHAIN)) {
2166 /* 4 for rfc1002 length field */
2168 cpu_to_le32(get_rfc1002_length(req) + 4);
2169 } else /* END_OF_CHAIN */
2170 shdr->NextCommand = 0;
2171 if (request_type & RELATED_REQUEST) {
2172 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2174 * Related requests use info from previous read request
2177 shdr->SessionId = 0xFFFFFFFF;
2178 shdr->TreeId = 0xFFFFFFFF;
2179 req->PersistentFileId = 0xFFFFFFFF;
2180 req->VolatileFileId = 0xFFFFFFFF;
2183 if (remaining_bytes > io_parms->length)
2184 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2186 req->RemainingBytes = 0;
2189 /* 4 for rfc1002 length field */
2190 *total_len = get_rfc1002_length(req) + 4;
2195 smb2_readv_callback(struct mid_q_entry *mid)
2197 struct cifs_readdata *rdata = mid->callback_data;
2198 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2199 struct TCP_Server_Info *server = tcon->ses->server;
2200 struct smb2_sync_hdr *shdr =
2201 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
2202 unsigned int credits_received = 1;
2203 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2205 .rq_pages = rdata->pages,
2206 .rq_npages = rdata->nr_pages,
2207 .rq_pagesz = rdata->pagesz,
2208 .rq_tailsz = rdata->tailsz };
2210 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2211 __func__, mid->mid, mid->mid_state, rdata->result,
2214 switch (mid->mid_state) {
2215 case MID_RESPONSE_RECEIVED:
2216 credits_received = le16_to_cpu(shdr->CreditRequest);
2217 /* result already set, check signature */
2221 rc = smb2_verify_signature(&rqst, server);
2223 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2226 /* FIXME: should this be counted toward the initiating task? */
2227 task_io_account_read(rdata->got_bytes);
2228 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2230 case MID_REQUEST_SUBMITTED:
2231 case MID_RETRY_NEEDED:
2232 rdata->result = -EAGAIN;
2233 if (server->sign && rdata->got_bytes)
2234 /* reset bytes number since we can not check a sign */
2235 rdata->got_bytes = 0;
2236 /* FIXME: should this be counted toward the initiating task? */
2237 task_io_account_read(rdata->got_bytes);
2238 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2241 if (rdata->result != -ENODATA)
2242 rdata->result = -EIO;
2246 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2248 queue_work(cifsiod_wq, &rdata->work);
2249 mutex_lock(&server->srv_mutex);
2250 DeleteMidQEntry(mid);
2251 mutex_unlock(&server->srv_mutex);
2252 add_credits(server, credits_received, 0);
2255 /* smb2_async_readv - send an async read, and set up mid to handle result */
2257 smb2_async_readv(struct cifs_readdata *rdata)
2261 struct smb2_sync_hdr *shdr;
2262 struct cifs_io_parms io_parms;
2263 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2265 struct TCP_Server_Info *server;
2266 unsigned int total_len;
2268 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2269 __func__, rdata->offset, rdata->bytes);
2271 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2272 io_parms.offset = rdata->offset;
2273 io_parms.length = rdata->bytes;
2274 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2275 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2276 io_parms.pid = rdata->pid;
2278 server = io_parms.tcon->ses->server;
2280 rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0);
2282 if (rc == -EAGAIN && rdata->credits) {
2283 /* credits was reset by reconnect */
2285 /* reduce in_flight value since we won't send the req */
2286 spin_lock(&server->req_lock);
2287 server->in_flight--;
2288 spin_unlock(&server->req_lock);
2293 shdr = get_sync_hdr(buf);
2294 /* 4 for rfc1002 length field */
2295 rdata->iov[0].iov_len = 4;
2296 rdata->iov[0].iov_base = buf;
2297 rdata->iov[1].iov_len = total_len - 4;
2298 rdata->iov[1].iov_base = buf + 4;
2300 if (rdata->credits) {
2301 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2302 SMB2_MAX_BUFFER_SIZE));
2303 shdr->CreditRequest = shdr->CreditCharge;
2304 spin_lock(&server->req_lock);
2305 server->credits += rdata->credits -
2306 le16_to_cpu(shdr->CreditCharge);
2307 spin_unlock(&server->req_lock);
2308 wake_up(&server->request_q);
2309 flags = CIFS_HAS_CREDITS;
2312 kref_get(&rdata->refcount);
2313 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
2314 cifs_readv_receive, smb2_readv_callback,
2317 kref_put(&rdata->refcount, cifs_readdata_release);
2318 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2321 cifs_small_buf_release(buf);
2326 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2327 unsigned int *nbytes, char **buf, int *buf_type)
2329 int resp_buftype, rc = -EACCES;
2330 struct smb2_read_rsp *rsp = NULL;
2331 struct smb2_sync_hdr *shdr;
2333 struct kvec rsp_iov;
2334 unsigned int total_len;
2338 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0);
2342 iov[0].iov_base = buf;
2343 iov[0].iov_len = total_len;
2345 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
2346 &resp_buftype, CIFS_LOG_ERROR, &rsp_iov);
2347 cifs_small_buf_release(iov[0].iov_base);
2349 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
2350 shdr = get_sync_hdr(rsp);
2352 if (shdr->Status == STATUS_END_OF_FILE) {
2353 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2358 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2359 cifs_dbg(VFS, "Send error in read = %d\n", rc);
2361 *nbytes = le32_to_cpu(rsp->DataLength);
2362 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2363 (*nbytes > io_parms->length)) {
2364 cifs_dbg(FYI, "bad length %d for count %d\n",
2365 *nbytes, io_parms->length);
2372 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
2373 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2374 } else if (resp_buftype != CIFS_NO_BUFFER) {
2375 *buf = rsp_iov.iov_base;
2376 if (resp_buftype == CIFS_SMALL_BUFFER)
2377 *buf_type = CIFS_SMALL_BUFFER;
2378 else if (resp_buftype == CIFS_LARGE_BUFFER)
2379 *buf_type = CIFS_LARGE_BUFFER;
2385 * Check the mid_state and signature on received buffer (if any), and queue the
2386 * workqueue completion task.
2389 smb2_writev_callback(struct mid_q_entry *mid)
2391 struct cifs_writedata *wdata = mid->callback_data;
2392 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2393 struct TCP_Server_Info *server = tcon->ses->server;
2394 unsigned int written;
2395 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2396 unsigned int credits_received = 1;
2398 switch (mid->mid_state) {
2399 case MID_RESPONSE_RECEIVED:
2400 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
2401 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2402 if (wdata->result != 0)
2405 written = le32_to_cpu(rsp->DataLength);
2407 * Mask off high 16 bits when bytes written as returned
2408 * by the server is greater than bytes requested by the
2409 * client. OS/2 servers are known to set incorrect
2412 if (written > wdata->bytes)
2415 if (written < wdata->bytes)
2416 wdata->result = -ENOSPC;
2418 wdata->bytes = written;
2420 case MID_REQUEST_SUBMITTED:
2421 case MID_RETRY_NEEDED:
2422 wdata->result = -EAGAIN;
2425 wdata->result = -EIO;
2430 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2432 queue_work(cifsiod_wq, &wdata->work);
2433 mutex_lock(&server->srv_mutex);
2434 DeleteMidQEntry(mid);
2435 mutex_unlock(&server->srv_mutex);
2436 add_credits(tcon->ses->server, credits_received, 0);
2439 /* smb2_async_writev - send an async write, and set up mid to handle result */
2441 smb2_async_writev(struct cifs_writedata *wdata,
2442 void (*release)(struct kref *kref))
2444 int rc = -EACCES, flags = 0;
2445 struct smb2_write_req *req = NULL;
2446 struct smb2_sync_hdr *shdr;
2447 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2448 struct TCP_Server_Info *server = tcon->ses->server;
2450 struct smb_rqst rqst = { };
2452 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
2454 if (rc == -EAGAIN && wdata->credits) {
2455 /* credits was reset by reconnect */
2457 /* reduce in_flight value since we won't send the req */
2458 spin_lock(&server->req_lock);
2459 server->in_flight--;
2460 spin_unlock(&server->req_lock);
2462 goto async_writev_out;
2465 shdr = get_sync_hdr(req);
2466 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
2468 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2469 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2470 req->WriteChannelInfoOffset = 0;
2471 req->WriteChannelInfoLength = 0;
2473 req->Offset = cpu_to_le64(wdata->offset);
2474 /* 4 for rfc1002 length field */
2475 req->DataOffset = cpu_to_le16(
2476 offsetof(struct smb2_write_req, Buffer) - 4);
2477 req->RemainingBytes = 0;
2479 /* 4 for rfc1002 length field and 1 for Buffer */
2481 iov[0].iov_base = req;
2482 iov[1].iov_len = get_rfc1002_length(req) - 1;
2483 iov[1].iov_base = (char *)req + 4;
2487 rqst.rq_pages = wdata->pages;
2488 rqst.rq_npages = wdata->nr_pages;
2489 rqst.rq_pagesz = wdata->pagesz;
2490 rqst.rq_tailsz = wdata->tailsz;
2492 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2493 wdata->offset, wdata->bytes);
2495 req->Length = cpu_to_le32(wdata->bytes);
2497 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2499 if (wdata->credits) {
2500 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2501 SMB2_MAX_BUFFER_SIZE));
2502 shdr->CreditRequest = shdr->CreditCharge;
2503 spin_lock(&server->req_lock);
2504 server->credits += wdata->credits -
2505 le16_to_cpu(shdr->CreditCharge);
2506 spin_unlock(&server->req_lock);
2507 wake_up(&server->request_q);
2508 flags = CIFS_HAS_CREDITS;
2511 kref_get(&wdata->refcount);
2512 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, wdata,
2516 kref_put(&wdata->refcount, release);
2517 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2521 cifs_small_buf_release(req);
2526 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2527 * The length field from io_parms must be at least 1 and indicates a number of
2528 * elements with data to write that begins with position 1 in iov array. All
2529 * data length is specified by count.
2532 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2533 unsigned int *nbytes, struct kvec *iov, int n_vec)
2536 struct smb2_write_req *req = NULL;
2537 struct smb2_write_rsp *rsp = NULL;
2539 struct kvec rsp_iov;
2546 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2550 if (io_parms->tcon->ses->server == NULL)
2551 return -ECONNABORTED;
2553 req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
2555 req->PersistentFileId = io_parms->persistent_fid;
2556 req->VolatileFileId = io_parms->volatile_fid;
2557 req->WriteChannelInfoOffset = 0;
2558 req->WriteChannelInfoLength = 0;
2560 req->Length = cpu_to_le32(io_parms->length);
2561 req->Offset = cpu_to_le64(io_parms->offset);
2562 /* 4 for rfc1002 length field */
2563 req->DataOffset = cpu_to_le16(
2564 offsetof(struct smb2_write_req, Buffer) - 4);
2565 req->RemainingBytes = 0;
2567 iov[0].iov_base = (char *)req;
2568 /* 4 for rfc1002 length field and 1 for Buffer */
2569 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2571 /* length of entire message including data to be written */
2572 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2574 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2575 &resp_buftype, 0, &rsp_iov);
2576 cifs_small_buf_release(req);
2577 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
2580 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
2581 cifs_dbg(VFS, "Send error in write = %d\n", rc);
2583 *nbytes = le32_to_cpu(rsp->DataLength);
2585 free_rsp_buf(resp_buftype, rsp);
2590 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2593 unsigned int entrycount = 0;
2594 unsigned int next_offset = 0;
2595 FILE_DIRECTORY_INFO *entryptr;
2597 if (bufstart == NULL)
2600 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2603 entryptr = (FILE_DIRECTORY_INFO *)
2604 ((char *)entryptr + next_offset);
2606 if ((char *)entryptr + size > end_of_buf) {
2607 cifs_dbg(VFS, "malformed search entry would overflow\n");
2611 len = le32_to_cpu(entryptr->FileNameLength);
2612 if ((char *)entryptr + len + size > end_of_buf) {
2613 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2618 *lastentry = (char *)entryptr;
2621 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2633 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2634 u64 persistent_fid, u64 volatile_fid, int index,
2635 struct cifs_search_info *srch_inf)
2637 struct smb2_query_directory_req *req;
2638 struct smb2_query_directory_rsp *rsp = NULL;
2640 struct kvec rsp_iov;
2643 int resp_buftype = CIFS_NO_BUFFER;
2644 unsigned char *bufptr;
2645 struct TCP_Server_Info *server;
2646 struct cifs_ses *ses = tcon->ses;
2647 __le16 asteriks = cpu_to_le16('*');
2649 unsigned int output_size = CIFSMaxBufSize;
2650 size_t info_buf_size;
2652 if (ses && (ses->server))
2653 server = ses->server;
2657 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2661 switch (srch_inf->info_level) {
2662 case SMB_FIND_FILE_DIRECTORY_INFO:
2663 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2664 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2666 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2667 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2668 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2671 cifs_dbg(VFS, "info level %u isn't supported\n",
2672 srch_inf->info_level);
2677 req->FileIndex = cpu_to_le32(index);
2678 req->PersistentFileId = persistent_fid;
2679 req->VolatileFileId = volatile_fid;
2682 bufptr = req->Buffer;
2683 memcpy(bufptr, &asteriks, len);
2685 req->FileNameOffset =
2686 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2687 req->FileNameLength = cpu_to_le16(len);
2689 * BB could be 30 bytes or so longer if we used SMB2 specific
2690 * buffer lengths, but this is safe and close enough.
2692 output_size = min_t(unsigned int, output_size, server->maxBuf);
2693 output_size = min_t(unsigned int, output_size, 2 << 15);
2694 req->OutputBufferLength = cpu_to_le32(output_size);
2696 iov[0].iov_base = (char *)req;
2697 /* 4 for RFC1001 length and 1 for Buffer */
2698 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2700 iov[1].iov_base = (char *)(req->Buffer);
2701 iov[1].iov_len = len;
2703 inc_rfc1001_len(req, len - 1 /* Buffer */);
2705 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0, &rsp_iov);
2706 cifs_small_buf_release(req);
2707 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
2710 if (rc == -ENODATA &&
2711 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
2712 srch_inf->endOfSearch = true;
2715 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2719 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2720 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2725 srch_inf->unicode = true;
2727 if (srch_inf->ntwrk_buf_start) {
2728 if (srch_inf->smallBuf)
2729 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2731 cifs_buf_release(srch_inf->ntwrk_buf_start);
2733 srch_inf->ntwrk_buf_start = (char *)rsp;
2734 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2735 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2736 /* 4 for rfc1002 length field */
2737 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2738 srch_inf->entries_in_buffer =
2739 num_entries(srch_inf->srch_entries_start, end_of_smb,
2740 &srch_inf->last_entry, info_buf_size);
2741 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
2742 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2743 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2744 srch_inf->srch_entries_start, srch_inf->last_entry);
2745 if (resp_buftype == CIFS_LARGE_BUFFER)
2746 srch_inf->smallBuf = false;
2747 else if (resp_buftype == CIFS_SMALL_BUFFER)
2748 srch_inf->smallBuf = true;
2750 cifs_dbg(VFS, "illegal search buffer type\n");
2755 free_rsp_buf(resp_buftype, rsp);
2760 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2761 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
2762 unsigned int num, void **data, unsigned int *size)
2764 struct smb2_set_info_req *req;
2765 struct smb2_set_info_rsp *rsp = NULL;
2767 struct kvec rsp_iov;
2771 struct TCP_Server_Info *server;
2772 struct cifs_ses *ses = tcon->ses;
2774 if (ses && (ses->server))
2775 server = ses->server;
2782 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2786 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2792 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
2794 req->InfoType = SMB2_O_INFO_FILE;
2795 req->FileInfoClass = info_class;
2796 req->PersistentFileId = persistent_fid;
2797 req->VolatileFileId = volatile_fid;
2799 /* 4 for RFC1001 length and 1 for Buffer */
2801 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2802 req->BufferLength = cpu_to_le32(*size);
2804 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2806 memcpy(req->Buffer, *data, *size);
2808 iov[0].iov_base = (char *)req;
2809 /* 4 for RFC1001 length */
2810 iov[0].iov_len = get_rfc1002_length(req) + 4;
2812 for (i = 1; i < num; i++) {
2813 inc_rfc1001_len(req, size[i]);
2814 le32_add_cpu(&req->BufferLength, size[i]);
2815 iov[i].iov_base = (char *)data[i];
2816 iov[i].iov_len = size[i];
2819 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0, &rsp_iov);
2820 cifs_small_buf_release(req);
2821 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
2824 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
2826 free_rsp_buf(resp_buftype, rsp);
2832 SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2833 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2835 struct smb2_file_rename_info info;
2837 unsigned int size[2];
2839 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2841 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2845 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2846 /* 0 = fail if target already exists */
2847 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2848 info.FileNameLength = cpu_to_le32(len);
2851 size[0] = sizeof(struct smb2_file_rename_info);
2853 data[1] = target_file;
2854 size[1] = len + 2 /* null */;
2856 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2857 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2864 SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
2865 u64 persistent_fid, u64 volatile_fid)
2867 __u8 delete_pending = 1;
2871 data = &delete_pending;
2872 size = 1; /* sizeof __u8 */
2874 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2875 current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data,
2880 SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2881 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2883 struct smb2_file_link_info info;
2885 unsigned int size[2];
2887 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2889 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2893 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2894 /* 0 = fail if link already exists */
2895 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2896 info.FileNameLength = cpu_to_le32(len);
2899 size[0] = sizeof(struct smb2_file_link_info);
2901 data[1] = target_file;
2902 size[1] = len + 2 /* null */;
2904 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2905 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
2911 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2912 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
2914 struct smb2_file_eof_info info;
2918 info.EndOfFile = *eof;
2921 size = sizeof(struct smb2_file_eof_info);
2924 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2925 pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size);
2927 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2928 pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
2932 SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2933 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2936 size = sizeof(FILE_BASIC_INFO);
2937 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2938 current->tgid, FILE_BASIC_INFORMATION, 1,
2939 (void **)&buf, &size);
2943 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2944 const u64 persistent_fid, const u64 volatile_fid,
2948 struct smb2_oplock_break *req = NULL;
2950 cifs_dbg(FYI, "SMB2_oplock_break\n");
2951 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2956 req->VolatileFid = volatile_fid;
2957 req->PersistentFid = persistent_fid;
2958 req->OplockLevel = oplock_level;
2959 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
2961 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2962 cifs_small_buf_release(req);
2965 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
2966 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
2973 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2974 struct kstatfs *kst)
2976 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2977 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2978 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2979 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
2980 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2985 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2986 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2989 struct smb2_query_info_req *req;
2991 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
2993 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2996 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
3000 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3001 req->FileInfoClass = level;
3002 req->PersistentFileId = persistent_fid;
3003 req->VolatileFileId = volatile_fid;
3004 /* 4 for rfc1002 length field and 1 for pad */
3005 req->InputBufferOffset =
3006 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
3007 req->OutputBufferLength = cpu_to_le32(
3008 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3010 iov->iov_base = (char *)req;
3011 /* 4 for rfc1002 length field */
3012 iov->iov_len = get_rfc1002_length(req) + 4;
3017 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3018 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3020 struct smb2_query_info_rsp *rsp = NULL;
3022 struct kvec rsp_iov;
3025 struct cifs_ses *ses = tcon->ses;
3026 struct smb2_fs_full_size_info *info = NULL;
3028 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3029 sizeof(struct smb2_fs_full_size_info),
3030 persistent_fid, volatile_fid);
3034 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0, &rsp_iov);
3035 cifs_small_buf_release(iov.iov_base);
3037 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3040 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3042 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3043 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3044 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3045 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3046 sizeof(struct smb2_fs_full_size_info));
3048 copy_fs_info_to_kstatfs(info, fsdata);
3051 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3056 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
3057 u64 persistent_fid, u64 volatile_fid, int level)
3059 struct smb2_query_info_rsp *rsp = NULL;
3061 struct kvec rsp_iov;
3063 int resp_buftype, max_len, min_len;
3064 struct cifs_ses *ses = tcon->ses;
3065 unsigned int rsp_len, offset;
3067 if (level == FS_DEVICE_INFORMATION) {
3068 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3069 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3070 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3071 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3072 min_len = MIN_FS_ATTR_INFO_SIZE;
3073 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3074 max_len = sizeof(struct smb3_fs_ss_info);
3075 min_len = sizeof(struct smb3_fs_ss_info);
3077 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
3081 rc = build_qfs_info_req(&iov, tcon, level, max_len,
3082 persistent_fid, volatile_fid);
3086 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0, &rsp_iov);
3087 cifs_small_buf_release(iov.iov_base);
3089 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3092 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3094 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3095 offset = le16_to_cpu(rsp->OutputBufferOffset);
3096 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3100 if (level == FS_ATTRIBUTE_INFORMATION)
3101 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3102 + (char *)&rsp->hdr, min_t(unsigned int,
3104 else if (level == FS_DEVICE_INFORMATION)
3105 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3106 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
3107 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3108 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3109 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3110 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3111 tcon->perf_sector_size =
3112 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3116 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3121 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3122 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3123 const __u32 num_lock, struct smb2_lock_element *buf)
3126 struct smb2_lock_req *req = NULL;
3128 struct kvec rsp_iov;
3132 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
3134 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3138 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
3139 req->LockCount = cpu_to_le16(num_lock);
3141 req->PersistentFileId = persist_fid;
3142 req->VolatileFileId = volatile_fid;
3144 count = num_lock * sizeof(struct smb2_lock_element);
3145 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3147 iov[0].iov_base = (char *)req;
3148 /* 4 for rfc1002 length field and count for all locks */
3149 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3150 iov[1].iov_base = (char *)buf;
3151 iov[1].iov_len = count;
3153 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3154 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP,
3156 cifs_small_buf_release(req);
3158 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
3159 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3166 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3167 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3168 const __u64 length, const __u64 offset, const __u32 lock_flags,
3171 struct smb2_lock_element lock;
3173 lock.Offset = cpu_to_le64(offset);
3174 lock.Length = cpu_to_le64(length);
3175 lock.Flags = cpu_to_le32(lock_flags);
3176 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3177 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3179 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3183 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3184 __u8 *lease_key, const __le32 lease_state)
3187 struct smb2_lease_ack *req = NULL;
3189 cifs_dbg(FYI, "SMB2_lease_break\n");
3190 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3195 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
3196 req->StructureSize = cpu_to_le16(36);
3197 inc_rfc1001_len(req, 12);
3199 memcpy(req->LeaseKey, lease_key, 16);
3200 req->LeaseState = lease_state;
3202 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
3203 cifs_small_buf_release(req);
3206 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3207 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);