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"
53 * The following table defines the expected "StructureSize" of SMB2 requests
54 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
56 * Note that commands are defined in smb2pdu.h in le16 but the array below is
57 * indexed by command in host byte order.
59 static const int smb2_req_struct_sizes
[NUMBER_OF_SMB2_COMMANDS
] = {
60 /* SMB2_NEGOTIATE */ 36,
61 /* SMB2_SESSION_SETUP */ 25,
63 /* SMB2_TREE_CONNECT */ 9,
64 /* SMB2_TREE_DISCONNECT */ 4,
74 /* SMB2_QUERY_DIRECTORY */ 33,
75 /* SMB2_CHANGE_NOTIFY */ 32,
76 /* SMB2_QUERY_INFO */ 41,
77 /* SMB2_SET_INFO */ 33,
78 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
81 static int encryption_required(const struct cifs_tcon
*tcon
)
85 if ((tcon
->ses
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
) ||
86 (tcon
->share_flags
& SHI1005_FLAGS_ENCRYPT_DATA
))
89 (tcon
->ses
->server
->capabilities
& SMB2_GLOBAL_CAP_ENCRYPTION
))
95 smb2_hdr_assemble(struct smb2_sync_hdr
*shdr
, __le16 smb2_cmd
,
96 const struct cifs_tcon
*tcon
)
98 shdr
->ProtocolId
= SMB2_PROTO_NUMBER
;
99 shdr
->StructureSize
= cpu_to_le16(64);
100 shdr
->Command
= smb2_cmd
;
101 if (tcon
&& tcon
->ses
&& tcon
->ses
->server
) {
102 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
104 spin_lock(&server
->req_lock
);
105 /* Request up to 2 credits but don't go over the limit. */
106 if (server
->credits
>= server
->max_credits
)
107 shdr
->CreditRequest
= cpu_to_le16(0);
109 shdr
->CreditRequest
= cpu_to_le16(
110 min_t(int, server
->max_credits
-
111 server
->credits
, 2));
112 spin_unlock(&server
->req_lock
);
114 shdr
->CreditRequest
= cpu_to_le16(2);
116 shdr
->ProcessId
= cpu_to_le32((__u16
)current
->tgid
);
121 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
122 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
123 if ((tcon
->ses
) && (tcon
->ses
->server
) &&
124 (tcon
->ses
->server
->capabilities
& SMB2_GLOBAL_CAP_LARGE_MTU
))
125 shdr
->CreditCharge
= cpu_to_le16(1);
126 /* else CreditCharge MBZ */
128 shdr
->TreeId
= tcon
->tid
;
129 /* Uid is not converted */
131 shdr
->SessionId
= tcon
->ses
->Suid
;
134 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
135 * to pass the path on the Open SMB prefixed by \\server\share.
136 * Not sure when we would need to do the augmented path (if ever) and
137 * setting this flag breaks the SMB2 open operation since it is
138 * illegal to send an empty path name (without \\server\share prefix)
139 * when the DFS flag is set in the SMB open header. We could
140 * consider setting the flag on all operations other than open
141 * but it is safer to net set it for now.
143 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
144 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
146 if (tcon
->ses
&& tcon
->ses
->server
&& tcon
->ses
->server
->sign
&&
147 !encryption_required(tcon
))
148 shdr
->Flags
|= SMB2_FLAGS_SIGNED
;
154 smb2_reconnect(__le16 smb2_command
, struct cifs_tcon
*tcon
)
157 struct nls_table
*nls_codepage
;
158 struct cifs_ses
*ses
;
159 struct TCP_Server_Info
*server
;
162 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
163 * check for tcp and smb session status done differently
164 * for those three - in the calling routine.
169 if (smb2_command
== SMB2_TREE_CONNECT
)
172 if (tcon
->tidStatus
== CifsExiting
) {
174 * only tree disconnect, open, and write,
175 * (and ulogoff which does not have tcon)
176 * are allowed as we start force umount.
178 if ((smb2_command
!= SMB2_WRITE
) &&
179 (smb2_command
!= SMB2_CREATE
) &&
180 (smb2_command
!= SMB2_TREE_DISCONNECT
)) {
181 cifs_dbg(FYI
, "can not send cmd %d while umounting\n",
186 if ((!tcon
->ses
) || (tcon
->ses
->status
== CifsExiting
) ||
187 (!tcon
->ses
->server
))
191 server
= ses
->server
;
194 * Give demultiplex thread up to 10 seconds to reconnect, should be
195 * greater than cifs socket timeout which is 7 seconds
197 while (server
->tcpStatus
== CifsNeedReconnect
) {
199 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
200 * here since they are implicitly done when session drops.
202 switch (smb2_command
) {
204 * BB Should we keep oplock break and add flush to exceptions?
206 case SMB2_TREE_DISCONNECT
:
209 case SMB2_OPLOCK_BREAK
:
213 wait_event_interruptible_timeout(server
->response_q
,
214 (server
->tcpStatus
!= CifsNeedReconnect
), 10 * HZ
);
216 /* are we still trying to reconnect? */
217 if (server
->tcpStatus
!= CifsNeedReconnect
)
221 * on "soft" mounts we wait once. Hard mounts keep
222 * retrying until process is killed or server comes
226 cifs_dbg(FYI
, "gave up waiting on reconnect in smb_init\n");
231 if (!tcon
->ses
->need_reconnect
&& !tcon
->need_reconnect
)
234 nls_codepage
= load_nls_default();
237 * need to prevent multiple threads trying to simultaneously reconnect
238 * the same SMB session
240 mutex_lock(&tcon
->ses
->session_mutex
);
243 * Recheck after acquire mutex. If another thread is negotiating
244 * and the server never sends an answer the socket will be closed
245 * and tcpStatus set to reconnect.
247 if (server
->tcpStatus
== CifsNeedReconnect
) {
249 mutex_unlock(&tcon
->ses
->session_mutex
);
253 rc
= cifs_negotiate_protocol(0, tcon
->ses
);
254 if (!rc
&& tcon
->ses
->need_reconnect
)
255 rc
= cifs_setup_session(0, tcon
->ses
, nls_codepage
);
257 if (rc
|| !tcon
->need_reconnect
) {
258 mutex_unlock(&tcon
->ses
->session_mutex
);
262 cifs_mark_open_files_invalid(tcon
);
263 if (tcon
->use_persistent
)
264 tcon
->need_reopen_files
= true;
266 rc
= SMB2_tcon(0, tcon
->ses
, tcon
->treeName
, tcon
, nls_codepage
);
267 mutex_unlock(&tcon
->ses
->session_mutex
);
269 cifs_dbg(FYI
, "reconnect tcon rc = %d\n", rc
);
273 if (smb2_command
!= SMB2_INTERNAL_CMD
)
274 queue_delayed_work(cifsiod_wq
, &server
->reconnect
, 0);
276 atomic_inc(&tconInfoReconnectCount
);
279 * Check if handle based operation so we know whether we can continue
280 * or not without returning to caller to reset file handle.
283 * BB Is flush done by server on drop of tcp session? Should we special
284 * case it and skip above?
286 switch (smb2_command
) {
292 case SMB2_QUERY_DIRECTORY
:
293 case SMB2_CHANGE_NOTIFY
:
294 case SMB2_QUERY_INFO
:
298 unload_nls(nls_codepage
);
303 fill_small_buf(__le16 smb2_command
, struct cifs_tcon
*tcon
, void *buf
,
304 unsigned int *total_len
)
306 struct smb2_sync_pdu
*spdu
= (struct smb2_sync_pdu
*)buf
;
307 /* lookup word count ie StructureSize from table */
308 __u16 parmsize
= smb2_req_struct_sizes
[le16_to_cpu(smb2_command
)];
311 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
312 * largest operations (Create)
316 smb2_hdr_assemble(&spdu
->sync_hdr
, smb2_command
, tcon
);
317 spdu
->StructureSize2
= cpu_to_le16(parmsize
);
319 *total_len
= parmsize
+ sizeof(struct smb2_sync_hdr
);
322 /* init request without RFC1001 length at the beginning */
324 smb2_plain_req_init(__le16 smb2_command
, struct cifs_tcon
*tcon
,
325 void **request_buf
, unsigned int *total_len
)
328 struct smb2_sync_hdr
*shdr
;
330 rc
= smb2_reconnect(smb2_command
, tcon
);
334 /* BB eventually switch this to SMB2 specific small buf size */
335 *request_buf
= cifs_small_buf_get();
336 if (*request_buf
== NULL
) {
337 /* BB should we add a retry in here if not a writepage? */
341 shdr
= (struct smb2_sync_hdr
*)(*request_buf
);
343 fill_small_buf(smb2_command
, tcon
, shdr
, total_len
);
346 #ifdef CONFIG_CIFS_STATS2
347 uint16_t com_code
= le16_to_cpu(smb2_command
);
349 cifs_stats_inc(&tcon
->stats
.smb2_stats
.smb2_com_sent
[com_code
]);
351 cifs_stats_inc(&tcon
->num_smbs_sent
);
358 * Allocate and return pointer to an SMB request hdr, and set basic
359 * SMB information in the SMB header. If the return code is zero, this
360 * function must have filled in request_buf pointer. The returned buffer
361 * has RFC1001 length at the beginning.
364 small_smb2_init(__le16 smb2_command
, struct cifs_tcon
*tcon
,
368 unsigned int total_len
;
369 struct smb2_pdu
*pdu
;
371 rc
= smb2_reconnect(smb2_command
, tcon
);
375 /* BB eventually switch this to SMB2 specific small buf size */
376 *request_buf
= cifs_small_buf_get();
377 if (*request_buf
== NULL
) {
378 /* BB should we add a retry in here if not a writepage? */
382 pdu
= (struct smb2_pdu
*)(*request_buf
);
384 fill_small_buf(smb2_command
, tcon
, get_sync_hdr(pdu
), &total_len
);
386 /* Note this is only network field converted to big endian */
387 pdu
->hdr
.smb2_buf_length
= cpu_to_be32(total_len
);
390 #ifdef CONFIG_CIFS_STATS2
391 uint16_t com_code
= le16_to_cpu(smb2_command
);
392 cifs_stats_inc(&tcon
->stats
.smb2_stats
.smb2_com_sent
[com_code
]);
394 cifs_stats_inc(&tcon
->num_smbs_sent
);
400 #ifdef CONFIG_CIFS_SMB311
401 /* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
402 #define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
405 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
406 #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
409 build_preauth_ctxt(struct smb2_preauth_neg_context
*pneg_ctxt
)
411 pneg_ctxt
->ContextType
= SMB2_PREAUTH_INTEGRITY_CAPABILITIES
;
412 pneg_ctxt
->DataLength
= cpu_to_le16(38);
413 pneg_ctxt
->HashAlgorithmCount
= cpu_to_le16(1);
414 pneg_ctxt
->SaltLength
= cpu_to_le16(SMB311_SALT_SIZE
);
415 get_random_bytes(pneg_ctxt
->Salt
, SMB311_SALT_SIZE
);
416 pneg_ctxt
->HashAlgorithms
= SMB2_PREAUTH_INTEGRITY_SHA512
;
420 build_encrypt_ctxt(struct smb2_encryption_neg_context
*pneg_ctxt
)
422 pneg_ctxt
->ContextType
= SMB2_ENCRYPTION_CAPABILITIES
;
423 pneg_ctxt
->DataLength
= cpu_to_le16(6);
424 pneg_ctxt
->CipherCount
= cpu_to_le16(2);
425 pneg_ctxt
->Ciphers
[0] = SMB2_ENCRYPTION_AES128_GCM
;
426 pneg_ctxt
->Ciphers
[1] = SMB2_ENCRYPTION_AES128_CCM
;
430 assemble_neg_contexts(struct smb2_negotiate_req
*req
)
433 /* +4 is to account for the RFC1001 len field */
434 char *pneg_ctxt
= (char *)req
+ OFFSET_OF_NEG_CONTEXT
+ 4;
436 build_preauth_ctxt((struct smb2_preauth_neg_context
*)pneg_ctxt
);
437 /* Add 2 to size to round to 8 byte boundary */
438 pneg_ctxt
+= 2 + sizeof(struct smb2_preauth_neg_context
);
439 build_encrypt_ctxt((struct smb2_encryption_neg_context
*)pneg_ctxt
);
440 req
->NegotiateContextOffset
= cpu_to_le32(OFFSET_OF_NEG_CONTEXT
);
441 req
->NegotiateContextCount
= cpu_to_le16(2);
442 inc_rfc1001_len(req
, 4 + sizeof(struct smb2_preauth_neg_context
) + 2
443 + sizeof(struct smb2_encryption_neg_context
)); /* calculate hash */
446 static void assemble_neg_contexts(struct smb2_negotiate_req
*req
)
454 * SMB2 Worker functions follow:
456 * The general structure of the worker functions is:
457 * 1) Call smb2_init (assembles SMB2 header)
458 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
459 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
460 * 4) Decode SMB2 command specific fields in the fixed length area
461 * 5) Decode variable length data area (if any for this SMB2 command type)
462 * 6) Call free smb buffer
468 SMB2_negotiate(const unsigned int xid
, struct cifs_ses
*ses
)
470 struct smb2_negotiate_req
*req
;
471 struct smb2_negotiate_rsp
*rsp
;
476 struct TCP_Server_Info
*server
= ses
->server
;
477 int blob_offset
, blob_length
;
479 int flags
= CIFS_NEG_OP
;
481 cifs_dbg(FYI
, "Negotiate protocol\n");
484 WARN(1, "%s: server is NULL!\n", __func__
);
488 rc
= small_smb2_init(SMB2_NEGOTIATE
, NULL
, (void **) &req
);
492 req
->hdr
.sync_hdr
.SessionId
= 0;
494 if (strcmp(ses
->server
->vals
->version_string
,
495 SMB3ANY_VERSION_STRING
) == 0) {
496 req
->Dialects
[0] = cpu_to_le16(SMB30_PROT_ID
);
497 req
->Dialects
[1] = cpu_to_le16(SMB302_PROT_ID
);
498 req
->DialectCount
= cpu_to_le16(2);
499 inc_rfc1001_len(req
, 4);
500 } else if (strcmp(ses
->server
->vals
->version_string
,
501 SMBDEFAULT_VERSION_STRING
) == 0) {
502 req
->Dialects
[0] = cpu_to_le16(SMB21_PROT_ID
);
503 req
->Dialects
[1] = cpu_to_le16(SMB30_PROT_ID
);
504 req
->Dialects
[2] = cpu_to_le16(SMB302_PROT_ID
);
505 req
->DialectCount
= cpu_to_le16(3);
506 inc_rfc1001_len(req
, 6);
508 /* otherwise send specific dialect */
509 req
->Dialects
[0] = cpu_to_le16(ses
->server
->vals
->protocol_id
);
510 req
->DialectCount
= cpu_to_le16(1);
511 inc_rfc1001_len(req
, 2);
514 /* only one of SMB2 signing flags may be set in SMB2 request */
516 req
->SecurityMode
= cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED
);
517 else if (global_secflags
& CIFSSEC_MAY_SIGN
)
518 req
->SecurityMode
= cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED
);
520 req
->SecurityMode
= 0;
522 req
->Capabilities
= cpu_to_le32(ses
->server
->vals
->req_capabilities
);
524 /* ClientGUID must be zero for SMB2.02 dialect */
525 if (ses
->server
->vals
->protocol_id
== SMB20_PROT_ID
)
526 memset(req
->ClientGUID
, 0, SMB2_CLIENT_GUID_SIZE
);
528 memcpy(req
->ClientGUID
, server
->client_guid
,
529 SMB2_CLIENT_GUID_SIZE
);
530 if (ses
->server
->vals
->protocol_id
== SMB311_PROT_ID
)
531 assemble_neg_contexts(req
);
533 iov
[0].iov_base
= (char *)req
;
534 /* 4 for rfc1002 length field */
535 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
537 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buftype
, flags
, &rsp_iov
);
538 cifs_small_buf_release(req
);
539 rsp
= (struct smb2_negotiate_rsp
*)rsp_iov
.iov_base
;
541 * No tcon so can't do
542 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
544 if (rc
== -EOPNOTSUPP
) {
545 cifs_dbg(VFS
, "Dialect not supported by server. Consider "
546 "specifying vers=1.0 or vers=2.0 on mount for accessing"
552 if (strcmp(ses
->server
->vals
->version_string
,
553 SMB3ANY_VERSION_STRING
) == 0) {
554 if (rsp
->DialectRevision
== cpu_to_le16(SMB20_PROT_ID
)) {
556 "SMB2 dialect returned but not requested\n");
558 } else if (rsp
->DialectRevision
== cpu_to_le16(SMB21_PROT_ID
)) {
560 "SMB2.1 dialect returned but not requested\n");
563 } else if (strcmp(ses
->server
->vals
->version_string
,
564 SMBDEFAULT_VERSION_STRING
) == 0) {
565 if (rsp
->DialectRevision
== cpu_to_le16(SMB20_PROT_ID
)) {
567 "SMB2 dialect returned but not requested\n");
569 } else if (rsp
->DialectRevision
== cpu_to_le16(SMB21_PROT_ID
)) {
570 /* ops set to 3.0 by default for default so update */
571 ses
->server
->ops
= &smb21_operations
;
573 } else if (rsp
->DialectRevision
!= ses
->server
->vals
->protocol_id
) {
574 /* if requested single dialect ensure returned dialect matched */
575 cifs_dbg(VFS
, "Illegal 0x%x dialect returned: not requested\n",
576 cpu_to_le16(rsp
->DialectRevision
));
580 cifs_dbg(FYI
, "mode 0x%x\n", rsp
->SecurityMode
);
582 if (rsp
->DialectRevision
== cpu_to_le16(SMB20_PROT_ID
))
583 cifs_dbg(FYI
, "negotiated smb2.0 dialect\n");
584 else if (rsp
->DialectRevision
== cpu_to_le16(SMB21_PROT_ID
))
585 cifs_dbg(FYI
, "negotiated smb2.1 dialect\n");
586 else if (rsp
->DialectRevision
== cpu_to_le16(SMB30_PROT_ID
))
587 cifs_dbg(FYI
, "negotiated smb3.0 dialect\n");
588 else if (rsp
->DialectRevision
== cpu_to_le16(SMB302_PROT_ID
))
589 cifs_dbg(FYI
, "negotiated smb3.02 dialect\n");
590 #ifdef CONFIG_CIFS_SMB311
591 else if (rsp
->DialectRevision
== cpu_to_le16(SMB311_PROT_ID
))
592 cifs_dbg(FYI
, "negotiated smb3.1.1 dialect\n");
595 cifs_dbg(VFS
, "Illegal dialect returned by server 0x%x\n",
596 le16_to_cpu(rsp
->DialectRevision
));
600 server
->dialect
= le16_to_cpu(rsp
->DialectRevision
);
602 /* BB: add check that dialect was valid given dialect(s) we asked for */
604 /* SMB2 only has an extended negflavor */
605 server
->negflavor
= CIFS_NEGFLAVOR_EXTENDED
;
606 /* set it to the maximum buffer size value we can send with 1 credit */
607 server
->maxBuf
= min_t(unsigned int, le32_to_cpu(rsp
->MaxTransactSize
),
608 SMB2_MAX_BUFFER_SIZE
);
609 server
->max_read
= le32_to_cpu(rsp
->MaxReadSize
);
610 server
->max_write
= le32_to_cpu(rsp
->MaxWriteSize
);
611 /* BB Do we need to validate the SecurityMode? */
612 server
->sec_mode
= le16_to_cpu(rsp
->SecurityMode
);
613 server
->capabilities
= le32_to_cpu(rsp
->Capabilities
);
615 server
->capabilities
|= SMB2_NT_FIND
| SMB2_LARGE_FILES
;
617 security_blob
= smb2_get_data_area_len(&blob_offset
, &blob_length
,
620 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
622 * ses->sectype = RawNTLMSSP;
623 * but for time being this is our only auth choice so doesn't matter.
624 * We just found a server which sets blob length to zero expecting raw.
626 if (blob_length
== 0) {
627 cifs_dbg(FYI
, "missing security blob on negprot\n");
628 server
->sec_ntlmssp
= true;
631 rc
= cifs_enable_signing(server
, ses
->sign
);
635 rc
= decode_negTokenInit(security_blob
, blob_length
, server
);
642 free_rsp_buf(resp_buftype
, rsp
);
646 int smb3_validate_negotiate(const unsigned int xid
, struct cifs_tcon
*tcon
)
649 struct validate_negotiate_info_req vneg_inbuf
;
650 struct validate_negotiate_info_rsp
*pneg_rsp
;
652 u32 inbuflen
; /* max of 4 dialects */
654 cifs_dbg(FYI
, "validate negotiate\n");
657 * validation ioctl must be signed, so no point sending this if we
658 * can not sign it. We could eventually change this to selectively
659 * sign just this, the first and only signed request on a connection.
660 * This is good enough for now since a user who wants better security
661 * would also enable signing on the mount. Having validation of
662 * negotiate info for signed connections helps reduce attack vectors
664 if (tcon
->ses
->server
->sign
== false)
665 return 0; /* validation requires signing */
667 vneg_inbuf
.Capabilities
=
668 cpu_to_le32(tcon
->ses
->server
->vals
->req_capabilities
);
669 memcpy(vneg_inbuf
.Guid
, tcon
->ses
->server
->client_guid
,
670 SMB2_CLIENT_GUID_SIZE
);
673 vneg_inbuf
.SecurityMode
=
674 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED
);
675 else if (global_secflags
& CIFSSEC_MAY_SIGN
)
676 vneg_inbuf
.SecurityMode
=
677 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED
);
679 vneg_inbuf
.SecurityMode
= 0;
682 if (strcmp(tcon
->ses
->server
->vals
->version_string
,
683 SMB3ANY_VERSION_STRING
) == 0) {
684 vneg_inbuf
.Dialects
[0] = cpu_to_le16(SMB30_PROT_ID
);
685 vneg_inbuf
.Dialects
[1] = cpu_to_le16(SMB302_PROT_ID
);
686 vneg_inbuf
.DialectCount
= cpu_to_le16(2);
687 /* structure is big enough for 3 dialects, sending only 2 */
688 inbuflen
= sizeof(struct validate_negotiate_info_req
) - 2;
689 } else if (strcmp(tcon
->ses
->server
->vals
->version_string
,
690 SMBDEFAULT_VERSION_STRING
) == 0) {
691 vneg_inbuf
.Dialects
[0] = cpu_to_le16(SMB21_PROT_ID
);
692 vneg_inbuf
.Dialects
[1] = cpu_to_le16(SMB30_PROT_ID
);
693 vneg_inbuf
.Dialects
[2] = cpu_to_le16(SMB302_PROT_ID
);
694 vneg_inbuf
.DialectCount
= cpu_to_le16(3);
695 /* structure is big enough for 3 dialects */
696 inbuflen
= sizeof(struct validate_negotiate_info_req
);
698 /* otherwise specific dialect was requested */
699 vneg_inbuf
.Dialects
[0] =
700 cpu_to_le16(tcon
->ses
->server
->vals
->protocol_id
);
701 vneg_inbuf
.DialectCount
= cpu_to_le16(1);
702 /* structure is big enough for 3 dialects, sending only 1 */
703 inbuflen
= sizeof(struct validate_negotiate_info_req
) - 4;
706 rc
= SMB2_ioctl(xid
, tcon
, NO_FILE_ID
, NO_FILE_ID
,
707 FSCTL_VALIDATE_NEGOTIATE_INFO
, true /* is_fsctl */,
709 (char *)&vneg_inbuf
, sizeof(struct validate_negotiate_info_req
),
710 (char **)&pneg_rsp
, &rsplen
);
713 cifs_dbg(VFS
, "validate protocol negotiate failed: %d\n", rc
);
717 if (rsplen
!= sizeof(struct validate_negotiate_info_rsp
)) {
718 cifs_dbg(VFS
, "invalid protocol negotiate response size: %d\n",
721 /* relax check since Mac returns max bufsize allowed on ioctl */
722 if (rsplen
> CIFSMaxBufSize
)
726 /* check validate negotiate info response matches what we got earlier */
727 if (pneg_rsp
->Dialect
!=
728 cpu_to_le16(tcon
->ses
->server
->vals
->protocol_id
))
731 if (pneg_rsp
->SecurityMode
!= cpu_to_le16(tcon
->ses
->server
->sec_mode
))
734 /* do not validate server guid because not saved at negprot time yet */
736 if ((le32_to_cpu(pneg_rsp
->Capabilities
) | SMB2_NT_FIND
|
737 SMB2_LARGE_FILES
) != tcon
->ses
->server
->capabilities
)
740 /* validate negotiate successful */
741 cifs_dbg(FYI
, "validate negotiate info successful\n");
745 cifs_dbg(VFS
, "protocol revalidation - security settings mismatch\n");
750 smb2_select_sectype(struct TCP_Server_Info
*server
, enum securityEnum requested
)
759 if (server
->sec_ntlmssp
&&
760 (global_secflags
& CIFSSEC_MAY_NTLMSSP
))
762 if ((server
->sec_kerberos
|| server
->sec_mskerberos
) &&
763 (global_secflags
& CIFSSEC_MAY_KRB5
))
771 struct SMB2_sess_data
{
773 struct cifs_ses
*ses
;
774 struct nls_table
*nls_cp
;
775 void (*func
)(struct SMB2_sess_data
*);
777 u64 previous_session
;
779 /* we will send the SMB in three pieces:
780 * a fixed length beginning part, an optional
781 * SPNEGO blob (which can be zero length), and a
782 * last part which will include the strings
783 * and rest of bcc area. This allows us to avoid
784 * a large buffer 17K allocation
791 SMB2_sess_alloc_buffer(struct SMB2_sess_data
*sess_data
)
794 struct cifs_ses
*ses
= sess_data
->ses
;
795 struct smb2_sess_setup_req
*req
;
796 struct TCP_Server_Info
*server
= ses
->server
;
798 rc
= small_smb2_init(SMB2_SESSION_SETUP
, NULL
, (void **) &req
);
802 /* First session, not a reauthenticate */
803 req
->hdr
.sync_hdr
.SessionId
= 0;
805 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
806 req
->PreviousSessionId
= sess_data
->previous_session
;
808 req
->Flags
= 0; /* MBZ */
809 /* to enable echos and oplocks */
810 req
->hdr
.sync_hdr
.CreditRequest
= cpu_to_le16(3);
812 /* only one of SMB2 signing flags may be set in SMB2 request */
814 req
->SecurityMode
= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
815 else if (global_secflags
& CIFSSEC_MAY_SIGN
) /* one flag unlike MUST_ */
816 req
->SecurityMode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
818 req
->SecurityMode
= 0;
820 req
->Capabilities
= 0;
821 req
->Channel
= 0; /* MBZ */
823 sess_data
->iov
[0].iov_base
= (char *)req
;
824 /* 4 for rfc1002 length field and 1 for pad */
825 sess_data
->iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
827 * This variable will be used to clear the buffer
828 * allocated above in case of any error in the calling function.
830 sess_data
->buf0_type
= CIFS_SMALL_BUFFER
;
836 SMB2_sess_free_buffer(struct SMB2_sess_data
*sess_data
)
838 free_rsp_buf(sess_data
->buf0_type
, sess_data
->iov
[0].iov_base
);
839 sess_data
->buf0_type
= CIFS_NO_BUFFER
;
843 SMB2_sess_sendreceive(struct SMB2_sess_data
*sess_data
)
846 struct smb2_sess_setup_req
*req
= sess_data
->iov
[0].iov_base
;
847 struct kvec rsp_iov
= { NULL
, 0 };
849 /* Testing shows that buffer offset must be at location of Buffer[0] */
850 req
->SecurityBufferOffset
=
851 cpu_to_le16(sizeof(struct smb2_sess_setup_req
) -
852 1 /* pad */ - 4 /* rfc1001 len */);
853 req
->SecurityBufferLength
= cpu_to_le16(sess_data
->iov
[1].iov_len
);
855 inc_rfc1001_len(req
, sess_data
->iov
[1].iov_len
- 1 /* pad */);
857 /* BB add code to build os and lm fields */
859 rc
= SendReceive2(sess_data
->xid
, sess_data
->ses
,
861 &sess_data
->buf0_type
,
862 CIFS_LOG_ERROR
| CIFS_NEG_OP
, &rsp_iov
);
863 cifs_small_buf_release(sess_data
->iov
[0].iov_base
);
864 memcpy(&sess_data
->iov
[0], &rsp_iov
, sizeof(struct kvec
));
870 SMB2_sess_establish_session(struct SMB2_sess_data
*sess_data
)
873 struct cifs_ses
*ses
= sess_data
->ses
;
875 mutex_lock(&ses
->server
->srv_mutex
);
876 if (ses
->server
->ops
->generate_signingkey
) {
877 rc
= ses
->server
->ops
->generate_signingkey(ses
);
880 "SMB3 session key generation failed\n");
881 mutex_unlock(&ses
->server
->srv_mutex
);
885 if (!ses
->server
->session_estab
) {
886 ses
->server
->sequence_number
= 0x2;
887 ses
->server
->session_estab
= true;
889 mutex_unlock(&ses
->server
->srv_mutex
);
891 cifs_dbg(FYI
, "SMB2/3 session established successfully\n");
892 spin_lock(&GlobalMid_Lock
);
893 ses
->status
= CifsGood
;
894 ses
->need_reconnect
= false;
895 spin_unlock(&GlobalMid_Lock
);
899 #ifdef CONFIG_CIFS_UPCALL
901 SMB2_auth_kerberos(struct SMB2_sess_data
*sess_data
)
904 struct cifs_ses
*ses
= sess_data
->ses
;
905 struct cifs_spnego_msg
*msg
;
906 struct key
*spnego_key
= NULL
;
907 struct smb2_sess_setup_rsp
*rsp
= NULL
;
909 rc
= SMB2_sess_alloc_buffer(sess_data
);
913 spnego_key
= cifs_get_spnego_key(ses
);
914 if (IS_ERR(spnego_key
)) {
915 rc
= PTR_ERR(spnego_key
);
920 msg
= spnego_key
->payload
.data
[0];
922 * check version field to make sure that cifs.upcall is
923 * sending us a response in an expected form
925 if (msg
->version
!= CIFS_SPNEGO_UPCALL_VERSION
) {
927 "bad cifs.upcall version. Expected %d got %d",
928 CIFS_SPNEGO_UPCALL_VERSION
, msg
->version
);
930 goto out_put_spnego_key
;
933 ses
->auth_key
.response
= kmemdup(msg
->data
, msg
->sesskey_len
,
935 if (!ses
->auth_key
.response
) {
937 "Kerberos can't allocate (%u bytes) memory",
940 goto out_put_spnego_key
;
942 ses
->auth_key
.len
= msg
->sesskey_len
;
944 sess_data
->iov
[1].iov_base
= msg
->data
+ msg
->sesskey_len
;
945 sess_data
->iov
[1].iov_len
= msg
->secblob_len
;
947 rc
= SMB2_sess_sendreceive(sess_data
);
949 goto out_put_spnego_key
;
951 rsp
= (struct smb2_sess_setup_rsp
*)sess_data
->iov
[0].iov_base
;
952 ses
->Suid
= rsp
->hdr
.sync_hdr
.SessionId
;
954 ses
->session_flags
= le16_to_cpu(rsp
->SessionFlags
);
956 rc
= SMB2_sess_establish_session(sess_data
);
958 key_invalidate(spnego_key
);
961 sess_data
->result
= rc
;
962 sess_data
->func
= NULL
;
963 SMB2_sess_free_buffer(sess_data
);
967 SMB2_auth_kerberos(struct SMB2_sess_data
*sess_data
)
969 cifs_dbg(VFS
, "Kerberos negotiated but upcall support disabled!\n");
970 sess_data
->result
= -EOPNOTSUPP
;
971 sess_data
->func
= NULL
;
976 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data
*sess_data
);
979 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data
*sess_data
)
982 struct cifs_ses
*ses
= sess_data
->ses
;
983 struct smb2_sess_setup_rsp
*rsp
= NULL
;
984 char *ntlmssp_blob
= NULL
;
985 bool use_spnego
= false; /* else use raw ntlmssp */
989 * If memory allocation is successful, caller of this function
992 ses
->ntlmssp
= kmalloc(sizeof(struct ntlmssp_auth
), GFP_KERNEL
);
997 ses
->ntlmssp
->sesskey_per_smbsess
= true;
999 rc
= SMB2_sess_alloc_buffer(sess_data
);
1003 ntlmssp_blob
= kmalloc(sizeof(struct _NEGOTIATE_MESSAGE
),
1005 if (ntlmssp_blob
== NULL
) {
1010 build_ntlmssp_negotiate_blob(ntlmssp_blob
, ses
);
1012 /* BB eventually need to add this */
1013 cifs_dbg(VFS
, "spnego not supported for SMB2 yet\n");
1017 blob_length
= sizeof(struct _NEGOTIATE_MESSAGE
);
1018 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1020 sess_data
->iov
[1].iov_base
= ntlmssp_blob
;
1021 sess_data
->iov
[1].iov_len
= blob_length
;
1023 rc
= SMB2_sess_sendreceive(sess_data
);
1024 rsp
= (struct smb2_sess_setup_rsp
*)sess_data
->iov
[0].iov_base
;
1026 /* If true, rc here is expected and not an error */
1027 if (sess_data
->buf0_type
!= CIFS_NO_BUFFER
&&
1028 rsp
->hdr
.sync_hdr
.Status
== STATUS_MORE_PROCESSING_REQUIRED
)
1034 if (offsetof(struct smb2_sess_setup_rsp
, Buffer
) - 4 !=
1035 le16_to_cpu(rsp
->SecurityBufferOffset
)) {
1036 cifs_dbg(VFS
, "Invalid security buffer offset %d\n",
1037 le16_to_cpu(rsp
->SecurityBufferOffset
));
1041 rc
= decode_ntlmssp_challenge(rsp
->Buffer
,
1042 le16_to_cpu(rsp
->SecurityBufferLength
), ses
);
1046 cifs_dbg(FYI
, "rawntlmssp session setup challenge phase\n");
1049 ses
->Suid
= rsp
->hdr
.sync_hdr
.SessionId
;
1050 ses
->session_flags
= le16_to_cpu(rsp
->SessionFlags
);
1053 kfree(ntlmssp_blob
);
1054 SMB2_sess_free_buffer(sess_data
);
1056 sess_data
->result
= 0;
1057 sess_data
->func
= SMB2_sess_auth_rawntlmssp_authenticate
;
1061 kfree(ses
->ntlmssp
);
1062 ses
->ntlmssp
= NULL
;
1063 sess_data
->result
= rc
;
1064 sess_data
->func
= NULL
;
1068 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data
*sess_data
)
1071 struct cifs_ses
*ses
= sess_data
->ses
;
1072 struct smb2_sess_setup_req
*req
;
1073 struct smb2_sess_setup_rsp
*rsp
= NULL
;
1074 unsigned char *ntlmssp_blob
= NULL
;
1075 bool use_spnego
= false; /* else use raw ntlmssp */
1076 u16 blob_length
= 0;
1078 rc
= SMB2_sess_alloc_buffer(sess_data
);
1082 req
= (struct smb2_sess_setup_req
*) sess_data
->iov
[0].iov_base
;
1083 req
->hdr
.sync_hdr
.SessionId
= ses
->Suid
;
1085 rc
= build_ntlmssp_auth_blob(&ntlmssp_blob
, &blob_length
, ses
,
1088 cifs_dbg(FYI
, "build_ntlmssp_auth_blob failed %d\n", rc
);
1093 /* BB eventually need to add this */
1094 cifs_dbg(VFS
, "spnego not supported for SMB2 yet\n");
1098 sess_data
->iov
[1].iov_base
= ntlmssp_blob
;
1099 sess_data
->iov
[1].iov_len
= blob_length
;
1101 rc
= SMB2_sess_sendreceive(sess_data
);
1105 rsp
= (struct smb2_sess_setup_rsp
*)sess_data
->iov
[0].iov_base
;
1107 ses
->Suid
= rsp
->hdr
.sync_hdr
.SessionId
;
1108 ses
->session_flags
= le16_to_cpu(rsp
->SessionFlags
);
1110 rc
= SMB2_sess_establish_session(sess_data
);
1112 kfree(ntlmssp_blob
);
1113 SMB2_sess_free_buffer(sess_data
);
1114 kfree(ses
->ntlmssp
);
1115 ses
->ntlmssp
= NULL
;
1116 sess_data
->result
= rc
;
1117 sess_data
->func
= NULL
;
1121 SMB2_select_sec(struct cifs_ses
*ses
, struct SMB2_sess_data
*sess_data
)
1125 type
= smb2_select_sectype(ses
->server
, ses
->sectype
);
1126 cifs_dbg(FYI
, "sess setup type %d\n", type
);
1127 if (type
== Unspecified
) {
1129 "Unable to select appropriate authentication method!");
1135 sess_data
->func
= SMB2_auth_kerberos
;
1138 sess_data
->func
= SMB2_sess_auth_rawntlmssp_negotiate
;
1141 cifs_dbg(VFS
, "secType %d not supported!\n", type
);
1149 SMB2_sess_setup(const unsigned int xid
, struct cifs_ses
*ses
,
1150 const struct nls_table
*nls_cp
)
1153 struct TCP_Server_Info
*server
= ses
->server
;
1154 struct SMB2_sess_data
*sess_data
;
1156 cifs_dbg(FYI
, "Session Setup\n");
1159 WARN(1, "%s: server is NULL!\n", __func__
);
1163 sess_data
= kzalloc(sizeof(struct SMB2_sess_data
), GFP_KERNEL
);
1167 rc
= SMB2_select_sec(ses
, sess_data
);
1170 sess_data
->xid
= xid
;
1171 sess_data
->ses
= ses
;
1172 sess_data
->buf0_type
= CIFS_NO_BUFFER
;
1173 sess_data
->nls_cp
= (struct nls_table
*) nls_cp
;
1175 while (sess_data
->func
)
1176 sess_data
->func(sess_data
);
1178 rc
= sess_data
->result
;
1185 SMB2_logoff(const unsigned int xid
, struct cifs_ses
*ses
)
1187 struct smb2_logoff_req
*req
; /* response is also trivial struct */
1189 struct TCP_Server_Info
*server
;
1192 cifs_dbg(FYI
, "disconnect session %p\n", ses
);
1194 if (ses
&& (ses
->server
))
1195 server
= ses
->server
;
1199 /* no need to send SMB logoff if uid already closed due to reconnect */
1200 if (ses
->need_reconnect
)
1201 goto smb2_session_already_dead
;
1203 rc
= small_smb2_init(SMB2_LOGOFF
, NULL
, (void **) &req
);
1207 /* since no tcon, smb2_init can not do this, so do here */
1208 req
->hdr
.sync_hdr
.SessionId
= ses
->Suid
;
1210 if (ses
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
)
1211 flags
|= CIFS_TRANSFORM_REQ
;
1212 else if (server
->sign
)
1213 req
->hdr
.sync_hdr
.Flags
|= SMB2_FLAGS_SIGNED
;
1215 rc
= SendReceiveNoRsp(xid
, ses
, (char *) req
, flags
);
1216 cifs_small_buf_release(req
);
1218 * No tcon so can't do
1219 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1222 smb2_session_already_dead
:
1226 static inline void cifs_stats_fail_inc(struct cifs_tcon
*tcon
, uint16_t code
)
1228 cifs_stats_inc(&tcon
->stats
.smb2_stats
.smb2_com_failed
[code
]);
1231 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1233 /* These are similar values to what Windows uses */
1234 static inline void init_copy_chunk_defaults(struct cifs_tcon
*tcon
)
1236 tcon
->max_chunks
= 256;
1237 tcon
->max_bytes_chunk
= 1048576;
1238 tcon
->max_bytes_copy
= 16777216;
1242 SMB2_tcon(const unsigned int xid
, struct cifs_ses
*ses
, const char *tree
,
1243 struct cifs_tcon
*tcon
, const struct nls_table
*cp
)
1245 struct smb2_tree_connect_req
*req
;
1246 struct smb2_tree_connect_rsp
*rsp
= NULL
;
1248 struct kvec rsp_iov
;
1252 __le16
*unc_path
= NULL
;
1255 cifs_dbg(FYI
, "TCON\n");
1257 if (!(ses
->server
) || !tree
)
1260 unc_path
= kmalloc(MAX_SHARENAME_LENGTH
* 2, GFP_KERNEL
);
1261 if (unc_path
== NULL
)
1264 unc_path_len
= cifs_strtoUTF16(unc_path
, tree
, strlen(tree
), cp
) + 1;
1266 if (unc_path_len
< 2) {
1271 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1275 rc
= small_smb2_init(SMB2_TREE_CONNECT
, tcon
, (void **) &req
);
1282 if ((ses
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
))
1283 flags
|= CIFS_TRANSFORM_REQ
;
1285 /* since no tcon, smb2_init can not do this, so do here */
1286 req
->hdr
.sync_hdr
.SessionId
= ses
->Suid
;
1287 if (ses
->server
->sign
)
1288 req
->hdr
.sync_hdr
.Flags
|= SMB2_FLAGS_SIGNED
;
1289 } else if (encryption_required(tcon
))
1290 flags
|= CIFS_TRANSFORM_REQ
;
1292 iov
[0].iov_base
= (char *)req
;
1293 /* 4 for rfc1002 length field and 1 for pad */
1294 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
1296 /* Testing shows that buffer offset must be at location of Buffer[0] */
1297 req
->PathOffset
= cpu_to_le16(sizeof(struct smb2_tree_connect_req
)
1298 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1299 req
->PathLength
= cpu_to_le16(unc_path_len
- 2);
1300 iov
[1].iov_base
= unc_path
;
1301 iov
[1].iov_len
= unc_path_len
;
1303 inc_rfc1001_len(req
, unc_path_len
- 1 /* pad */);
1305 rc
= SendReceive2(xid
, ses
, iov
, 2, &resp_buftype
, flags
, &rsp_iov
);
1306 cifs_small_buf_release(req
);
1307 rsp
= (struct smb2_tree_connect_rsp
*)rsp_iov
.iov_base
;
1311 cifs_stats_fail_inc(tcon
, SMB2_TREE_CONNECT_HE
);
1312 tcon
->need_reconnect
= true;
1314 goto tcon_error_exit
;
1318 ses
->ipc_tid
= rsp
->hdr
.sync_hdr
.TreeId
;
1322 switch (rsp
->ShareType
) {
1323 case SMB2_SHARE_TYPE_DISK
:
1324 cifs_dbg(FYI
, "connection to disk share\n");
1326 case SMB2_SHARE_TYPE_PIPE
:
1328 cifs_dbg(FYI
, "connection to pipe share\n");
1330 case SMB2_SHARE_TYPE_PRINT
:
1332 cifs_dbg(FYI
, "connection to printer\n");
1335 cifs_dbg(VFS
, "unknown share type %d\n", rsp
->ShareType
);
1337 goto tcon_error_exit
;
1340 tcon
->share_flags
= le32_to_cpu(rsp
->ShareFlags
);
1341 tcon
->capabilities
= rsp
->Capabilities
; /* we keep caps little endian */
1342 tcon
->maximal_access
= le32_to_cpu(rsp
->MaximalAccess
);
1343 tcon
->tidStatus
= CifsGood
;
1344 tcon
->need_reconnect
= false;
1345 tcon
->tid
= rsp
->hdr
.sync_hdr
.TreeId
;
1346 strlcpy(tcon
->treeName
, tree
, sizeof(tcon
->treeName
));
1348 if ((rsp
->Capabilities
& SMB2_SHARE_CAP_DFS
) &&
1349 ((tcon
->share_flags
& SHI1005_FLAGS_DFS
) == 0))
1350 cifs_dbg(VFS
, "DFS capability contradicts DFS flag\n");
1353 !(tcon
->ses
->server
->capabilities
& SMB2_GLOBAL_CAP_ENCRYPTION
))
1354 cifs_dbg(VFS
, "Encryption is requested but not supported\n");
1356 init_copy_chunk_defaults(tcon
);
1357 if (tcon
->ses
->server
->ops
->validate_negotiate
)
1358 rc
= tcon
->ses
->server
->ops
->validate_negotiate(xid
, tcon
);
1360 free_rsp_buf(resp_buftype
, rsp
);
1365 if (rsp
->hdr
.sync_hdr
.Status
== STATUS_BAD_NETWORK_NAME
) {
1366 cifs_dbg(VFS
, "BAD_NETWORK_NAME: %s\n", tree
);
1372 SMB2_tdis(const unsigned int xid
, struct cifs_tcon
*tcon
)
1374 struct smb2_tree_disconnect_req
*req
; /* response is trivial */
1376 struct cifs_ses
*ses
= tcon
->ses
;
1379 cifs_dbg(FYI
, "Tree Disconnect\n");
1381 if (!ses
|| !(ses
->server
))
1384 if ((tcon
->need_reconnect
) || (tcon
->ses
->need_reconnect
))
1387 rc
= small_smb2_init(SMB2_TREE_DISCONNECT
, tcon
, (void **) &req
);
1391 if (encryption_required(tcon
))
1392 flags
|= CIFS_TRANSFORM_REQ
;
1394 rc
= SendReceiveNoRsp(xid
, ses
, (char *)req
, flags
);
1395 cifs_small_buf_release(req
);
1397 cifs_stats_fail_inc(tcon
, SMB2_TREE_DISCONNECT_HE
);
1403 static struct create_durable
*
1404 create_durable_buf(void)
1406 struct create_durable
*buf
;
1408 buf
= kzalloc(sizeof(struct create_durable
), GFP_KERNEL
);
1412 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
1413 (struct create_durable
, Data
));
1414 buf
->ccontext
.DataLength
= cpu_to_le32(16);
1415 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
1416 (struct create_durable
, Name
));
1417 buf
->ccontext
.NameLength
= cpu_to_le16(4);
1418 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1426 static struct create_durable
*
1427 create_reconnect_durable_buf(struct cifs_fid
*fid
)
1429 struct create_durable
*buf
;
1431 buf
= kzalloc(sizeof(struct create_durable
), GFP_KERNEL
);
1435 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
1436 (struct create_durable
, Data
));
1437 buf
->ccontext
.DataLength
= cpu_to_le32(16);
1438 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
1439 (struct create_durable
, Name
));
1440 buf
->ccontext
.NameLength
= cpu_to_le16(4);
1441 buf
->Data
.Fid
.PersistentFileId
= fid
->persistent_fid
;
1442 buf
->Data
.Fid
.VolatileFileId
= fid
->volatile_fid
;
1443 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1452 parse_lease_state(struct TCP_Server_Info
*server
, struct smb2_create_rsp
*rsp
,
1453 unsigned int *epoch
)
1456 struct create_context
*cc
;
1458 unsigned int remaining
;
1461 data_offset
= (char *)rsp
+ 4 + le32_to_cpu(rsp
->CreateContextsOffset
);
1462 remaining
= le32_to_cpu(rsp
->CreateContextsLength
);
1463 cc
= (struct create_context
*)data_offset
;
1464 while (remaining
>= sizeof(struct create_context
)) {
1465 name
= le16_to_cpu(cc
->NameOffset
) + (char *)cc
;
1466 if (le16_to_cpu(cc
->NameLength
) == 4 &&
1467 strncmp(name
, "RqLs", 4) == 0)
1468 return server
->ops
->parse_lease_buf(cc
, epoch
);
1470 next
= le32_to_cpu(cc
->Next
);
1474 cc
= (struct create_context
*)((char *)cc
+ next
);
1481 add_lease_context(struct TCP_Server_Info
*server
, struct kvec
*iov
,
1482 unsigned int *num_iovec
, __u8
*oplock
)
1484 struct smb2_create_req
*req
= iov
[0].iov_base
;
1485 unsigned int num
= *num_iovec
;
1487 iov
[num
].iov_base
= server
->ops
->create_lease_buf(oplock
+1, *oplock
);
1488 if (iov
[num
].iov_base
== NULL
)
1490 iov
[num
].iov_len
= server
->vals
->create_lease_size
;
1491 req
->RequestedOplockLevel
= SMB2_OPLOCK_LEVEL_LEASE
;
1492 if (!req
->CreateContextsOffset
)
1493 req
->CreateContextsOffset
= cpu_to_le32(
1494 sizeof(struct smb2_create_req
) - 4 +
1495 iov
[num
- 1].iov_len
);
1496 le32_add_cpu(&req
->CreateContextsLength
,
1497 server
->vals
->create_lease_size
);
1498 inc_rfc1001_len(&req
->hdr
, server
->vals
->create_lease_size
);
1499 *num_iovec
= num
+ 1;
1503 static struct create_durable_v2
*
1504 create_durable_v2_buf(struct cifs_fid
*pfid
)
1506 struct create_durable_v2
*buf
;
1508 buf
= kzalloc(sizeof(struct create_durable_v2
), GFP_KERNEL
);
1512 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
1513 (struct create_durable_v2
, dcontext
));
1514 buf
->ccontext
.DataLength
= cpu_to_le32(sizeof(struct durable_context_v2
));
1515 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
1516 (struct create_durable_v2
, Name
));
1517 buf
->ccontext
.NameLength
= cpu_to_le16(4);
1519 buf
->dcontext
.Timeout
= 0; /* Should this be configurable by workload */
1520 buf
->dcontext
.Flags
= cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT
);
1521 generate_random_uuid(buf
->dcontext
.CreateGuid
);
1522 memcpy(pfid
->create_guid
, buf
->dcontext
.CreateGuid
, 16);
1524 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1532 static struct create_durable_handle_reconnect_v2
*
1533 create_reconnect_durable_v2_buf(struct cifs_fid
*fid
)
1535 struct create_durable_handle_reconnect_v2
*buf
;
1537 buf
= kzalloc(sizeof(struct create_durable_handle_reconnect_v2
),
1542 buf
->ccontext
.DataOffset
=
1543 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2
,
1545 buf
->ccontext
.DataLength
=
1546 cpu_to_le32(sizeof(struct durable_reconnect_context_v2
));
1547 buf
->ccontext
.NameOffset
=
1548 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2
,
1550 buf
->ccontext
.NameLength
= cpu_to_le16(4);
1552 buf
->dcontext
.Fid
.PersistentFileId
= fid
->persistent_fid
;
1553 buf
->dcontext
.Fid
.VolatileFileId
= fid
->volatile_fid
;
1554 buf
->dcontext
.Flags
= cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT
);
1555 memcpy(buf
->dcontext
.CreateGuid
, fid
->create_guid
, 16);
1557 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1566 add_durable_v2_context(struct kvec
*iov
, unsigned int *num_iovec
,
1567 struct cifs_open_parms
*oparms
)
1569 struct smb2_create_req
*req
= iov
[0].iov_base
;
1570 unsigned int num
= *num_iovec
;
1572 iov
[num
].iov_base
= create_durable_v2_buf(oparms
->fid
);
1573 if (iov
[num
].iov_base
== NULL
)
1575 iov
[num
].iov_len
= sizeof(struct create_durable_v2
);
1576 if (!req
->CreateContextsOffset
)
1577 req
->CreateContextsOffset
=
1578 cpu_to_le32(sizeof(struct smb2_create_req
) - 4 +
1580 le32_add_cpu(&req
->CreateContextsLength
, sizeof(struct create_durable_v2
));
1581 inc_rfc1001_len(&req
->hdr
, sizeof(struct create_durable_v2
));
1582 *num_iovec
= num
+ 1;
1587 add_durable_reconnect_v2_context(struct kvec
*iov
, unsigned int *num_iovec
,
1588 struct cifs_open_parms
*oparms
)
1590 struct smb2_create_req
*req
= iov
[0].iov_base
;
1591 unsigned int num
= *num_iovec
;
1593 /* indicate that we don't need to relock the file */
1594 oparms
->reconnect
= false;
1596 iov
[num
].iov_base
= create_reconnect_durable_v2_buf(oparms
->fid
);
1597 if (iov
[num
].iov_base
== NULL
)
1599 iov
[num
].iov_len
= sizeof(struct create_durable_handle_reconnect_v2
);
1600 if (!req
->CreateContextsOffset
)
1601 req
->CreateContextsOffset
=
1602 cpu_to_le32(sizeof(struct smb2_create_req
) - 4 +
1604 le32_add_cpu(&req
->CreateContextsLength
,
1605 sizeof(struct create_durable_handle_reconnect_v2
));
1606 inc_rfc1001_len(&req
->hdr
,
1607 sizeof(struct create_durable_handle_reconnect_v2
));
1608 *num_iovec
= num
+ 1;
1613 add_durable_context(struct kvec
*iov
, unsigned int *num_iovec
,
1614 struct cifs_open_parms
*oparms
, bool use_persistent
)
1616 struct smb2_create_req
*req
= iov
[0].iov_base
;
1617 unsigned int num
= *num_iovec
;
1619 if (use_persistent
) {
1620 if (oparms
->reconnect
)
1621 return add_durable_reconnect_v2_context(iov
, num_iovec
,
1624 return add_durable_v2_context(iov
, num_iovec
, oparms
);
1627 if (oparms
->reconnect
) {
1628 iov
[num
].iov_base
= create_reconnect_durable_buf(oparms
->fid
);
1629 /* indicate that we don't need to relock the file */
1630 oparms
->reconnect
= false;
1632 iov
[num
].iov_base
= create_durable_buf();
1633 if (iov
[num
].iov_base
== NULL
)
1635 iov
[num
].iov_len
= sizeof(struct create_durable
);
1636 if (!req
->CreateContextsOffset
)
1637 req
->CreateContextsOffset
=
1638 cpu_to_le32(sizeof(struct smb2_create_req
) - 4 +
1640 le32_add_cpu(&req
->CreateContextsLength
, sizeof(struct create_durable
));
1641 inc_rfc1001_len(&req
->hdr
, sizeof(struct create_durable
));
1642 *num_iovec
= num
+ 1;
1647 alloc_path_with_tree_prefix(__le16
**out_path
, int *out_size
, int *out_len
,
1648 const char *treename
, const __le16
*path
)
1650 int treename_len
, path_len
;
1651 struct nls_table
*cp
;
1652 const __le16 sep
[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1657 treename_len
= strlen(treename
);
1658 if (treename_len
< 2 || !(treename
[0] == '\\' && treename
[1] == '\\'))
1664 path_len
= UniStrnlen((wchar_t *)path
, PATH_MAX
);
1667 * make room for one path separator between the treename and
1670 *out_len
= treename_len
+ 1 + path_len
;
1673 * final path needs to be null-terminated UTF16 with a
1677 *out_size
= roundup((*out_len
+1)*2, 8);
1678 *out_path
= kzalloc(*out_size
, GFP_KERNEL
);
1682 cp
= load_nls_default();
1683 cifs_strtoUTF16(*out_path
, treename
, treename_len
, cp
);
1684 UniStrcat(*out_path
, sep
);
1685 UniStrcat(*out_path
, path
);
1692 SMB2_open(const unsigned int xid
, struct cifs_open_parms
*oparms
, __le16
*path
,
1693 __u8
*oplock
, struct smb2_file_all_info
*buf
,
1694 struct smb2_err_rsp
**err_buf
)
1696 struct smb2_create_req
*req
;
1697 struct smb2_create_rsp
*rsp
;
1698 struct TCP_Server_Info
*server
;
1699 struct cifs_tcon
*tcon
= oparms
->tcon
;
1700 struct cifs_ses
*ses
= tcon
->ses
;
1702 struct kvec rsp_iov
= {NULL
, 0};
1705 __le16
*copy_path
= NULL
;
1708 unsigned int n_iov
= 2;
1709 __u32 file_attributes
= 0;
1710 char *dhc_buf
= NULL
, *lc_buf
= NULL
;
1713 cifs_dbg(FYI
, "create/open\n");
1715 if (ses
&& (ses
->server
))
1716 server
= ses
->server
;
1720 rc
= small_smb2_init(SMB2_CREATE
, tcon
, (void **) &req
);
1724 if (encryption_required(tcon
))
1725 flags
|= CIFS_TRANSFORM_REQ
;
1727 if (oparms
->create_options
& CREATE_OPTION_READONLY
)
1728 file_attributes
|= ATTR_READONLY
;
1729 if (oparms
->create_options
& CREATE_OPTION_SPECIAL
)
1730 file_attributes
|= ATTR_SYSTEM
;
1732 req
->ImpersonationLevel
= IL_IMPERSONATION
;
1733 req
->DesiredAccess
= cpu_to_le32(oparms
->desired_access
);
1734 /* File attributes ignored on open (used in create though) */
1735 req
->FileAttributes
= cpu_to_le32(file_attributes
);
1736 req
->ShareAccess
= FILE_SHARE_ALL_LE
;
1737 req
->CreateDisposition
= cpu_to_le32(oparms
->disposition
);
1738 req
->CreateOptions
= cpu_to_le32(oparms
->create_options
& CREATE_OPTIONS_MASK
);
1740 iov
[0].iov_base
= (char *)req
;
1741 /* 4 for rfc1002 length field */
1742 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
1743 /* -1 since last byte is buf[0] which is sent below (path) */
1746 req
->NameOffset
= cpu_to_le16(sizeof(struct smb2_create_req
) - 4);
1748 /* [MS-SMB2] 2.2.13 NameOffset:
1749 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1750 * the SMB2 header, the file name includes a prefix that will
1751 * be processed during DFS name normalization as specified in
1752 * section 3.3.5.9. Otherwise, the file name is relative to
1753 * the share that is identified by the TreeId in the SMB2
1756 if (tcon
->share_flags
& SHI1005_FLAGS_DFS
) {
1759 req
->hdr
.sync_hdr
.Flags
|= SMB2_FLAGS_DFS_OPERATIONS
;
1760 rc
= alloc_path_with_tree_prefix(©_path
, ©_size
,
1762 tcon
->treeName
, path
);
1765 req
->NameLength
= cpu_to_le16(name_len
* 2);
1766 uni_path_len
= copy_size
;
1769 uni_path_len
= (2 * UniStrnlen((wchar_t *)path
, PATH_MAX
)) + 2;
1770 /* MUST set path len (NameLength) to 0 opening root of share */
1771 req
->NameLength
= cpu_to_le16(uni_path_len
- 2);
1772 if (uni_path_len
% 8 != 0) {
1773 copy_size
= roundup(uni_path_len
, 8);
1774 copy_path
= kzalloc(copy_size
, GFP_KERNEL
);
1777 memcpy((char *)copy_path
, (const char *)path
,
1779 uni_path_len
= copy_size
;
1784 iov
[1].iov_len
= uni_path_len
;
1785 iov
[1].iov_base
= path
;
1786 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1787 inc_rfc1001_len(req
, uni_path_len
- 1);
1789 if (!server
->oplocks
)
1790 *oplock
= SMB2_OPLOCK_LEVEL_NONE
;
1792 if (!(server
->capabilities
& SMB2_GLOBAL_CAP_LEASING
) ||
1793 *oplock
== SMB2_OPLOCK_LEVEL_NONE
)
1794 req
->RequestedOplockLevel
= *oplock
;
1796 rc
= add_lease_context(server
, iov
, &n_iov
, oplock
);
1798 cifs_small_buf_release(req
);
1802 lc_buf
= iov
[n_iov
-1].iov_base
;
1805 if (*oplock
== SMB2_OPLOCK_LEVEL_BATCH
) {
1806 /* need to set Next field of lease context if we request it */
1807 if (server
->capabilities
& SMB2_GLOBAL_CAP_LEASING
) {
1808 struct create_context
*ccontext
=
1809 (struct create_context
*)iov
[n_iov
-1].iov_base
;
1811 cpu_to_le32(server
->vals
->create_lease_size
);
1814 rc
= add_durable_context(iov
, &n_iov
, oparms
,
1815 tcon
->use_persistent
);
1817 cifs_small_buf_release(req
);
1822 dhc_buf
= iov
[n_iov
-1].iov_base
;
1825 rc
= SendReceive2(xid
, ses
, iov
, n_iov
, &resp_buftype
, flags
, &rsp_iov
);
1826 cifs_small_buf_release(req
);
1827 rsp
= (struct smb2_create_rsp
*)rsp_iov
.iov_base
;
1830 cifs_stats_fail_inc(tcon
, SMB2_CREATE_HE
);
1832 *err_buf
= kmemdup(rsp
, get_rfc1002_length(rsp
) + 4,
1837 oparms
->fid
->persistent_fid
= rsp
->PersistentFileId
;
1838 oparms
->fid
->volatile_fid
= rsp
->VolatileFileId
;
1841 memcpy(buf
, &rsp
->CreationTime
, 32);
1842 buf
->AllocationSize
= rsp
->AllocationSize
;
1843 buf
->EndOfFile
= rsp
->EndofFile
;
1844 buf
->Attributes
= rsp
->FileAttributes
;
1845 buf
->NumberOfLinks
= cpu_to_le32(1);
1846 buf
->DeletePending
= 0;
1849 if (rsp
->OplockLevel
== SMB2_OPLOCK_LEVEL_LEASE
)
1850 *oplock
= parse_lease_state(server
, rsp
, &oparms
->fid
->epoch
);
1852 *oplock
= rsp
->OplockLevel
;
1857 free_rsp_buf(resp_buftype
, rsp
);
1862 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1865 SMB2_ioctl(const unsigned int xid
, struct cifs_tcon
*tcon
, u64 persistent_fid
,
1866 u64 volatile_fid
, u32 opcode
, bool is_fsctl
, bool use_ipc
,
1867 char *in_data
, u32 indatalen
,
1868 char **out_data
, u32
*plen
/* returned data len */)
1870 struct smb2_ioctl_req
*req
;
1871 struct smb2_ioctl_rsp
*rsp
;
1872 struct smb2_sync_hdr
*shdr
;
1873 struct cifs_ses
*ses
;
1875 struct kvec rsp_iov
;
1881 cifs_dbg(FYI
, "SMB2 IOCTL\n");
1883 if (out_data
!= NULL
)
1886 /* zero out returned data len, in case of error */
1895 if (!ses
|| !(ses
->server
))
1898 rc
= small_smb2_init(SMB2_IOCTL
, tcon
, (void **) &req
);
1903 if (ses
->ipc_tid
== 0) {
1904 cifs_small_buf_release(req
);
1908 cifs_dbg(FYI
, "replacing tid 0x%x with IPC tid 0x%x\n",
1909 req
->hdr
.sync_hdr
.TreeId
, ses
->ipc_tid
);
1910 req
->hdr
.sync_hdr
.TreeId
= ses
->ipc_tid
;
1912 if (encryption_required(tcon
))
1913 flags
|= CIFS_TRANSFORM_REQ
;
1915 req
->CtlCode
= cpu_to_le32(opcode
);
1916 req
->PersistentFileId
= persistent_fid
;
1917 req
->VolatileFileId
= volatile_fid
;
1920 req
->InputCount
= cpu_to_le32(indatalen
);
1921 /* do not set InputOffset if no input data */
1923 cpu_to_le32(offsetof(struct smb2_ioctl_req
, Buffer
) - 4);
1924 iov
[1].iov_base
= in_data
;
1925 iov
[1].iov_len
= indatalen
;
1930 req
->OutputOffset
= 0;
1931 req
->OutputCount
= 0; /* MBZ */
1934 * Could increase MaxOutputResponse, but that would require more
1935 * than one credit. Windows typically sets this smaller, but for some
1936 * ioctls it may be useful to allow server to send more. No point
1937 * limiting what the server can send as long as fits in one credit
1938 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1939 * (by default, note that it can be overridden to make max larger)
1940 * in responses (except for read responses which can be bigger.
1941 * We may want to bump this limit up
1943 req
->MaxOutputResponse
= cpu_to_le32(CIFSMaxBufSize
);
1946 req
->Flags
= cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL
);
1950 iov
[0].iov_base
= (char *)req
;
1953 * If no input data, the size of ioctl struct in
1954 * protocol spec still includes a 1 byte data buffer,
1955 * but if input data passed to ioctl, we do not
1956 * want to double count this, so we do not send
1957 * the dummy one byte of data in iovec[0] if sending
1958 * input data (in iovec[1]). We also must add 4 bytes
1959 * in first iovec to allow for rfc1002 length field.
1963 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
1964 inc_rfc1001_len(req
, indatalen
- 1);
1966 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
1969 rc
= SendReceive2(xid
, ses
, iov
, n_iov
, &resp_buftype
, flags
, &rsp_iov
);
1970 cifs_small_buf_release(req
);
1971 rsp
= (struct smb2_ioctl_rsp
*)rsp_iov
.iov_base
;
1973 if ((rc
!= 0) && (rc
!= -EINVAL
)) {
1974 cifs_stats_fail_inc(tcon
, SMB2_IOCTL_HE
);
1976 } else if (rc
== -EINVAL
) {
1977 if ((opcode
!= FSCTL_SRV_COPYCHUNK_WRITE
) &&
1978 (opcode
!= FSCTL_SRV_COPYCHUNK
)) {
1979 cifs_stats_fail_inc(tcon
, SMB2_IOCTL_HE
);
1984 /* check if caller wants to look at return data or just return rc */
1985 if ((plen
== NULL
) || (out_data
== NULL
))
1988 *plen
= le32_to_cpu(rsp
->OutputCount
);
1990 /* We check for obvious errors in the output buffer length and offset */
1992 goto ioctl_exit
; /* server returned no data */
1993 else if (*plen
> 0xFF00) {
1994 cifs_dbg(VFS
, "srv returned invalid ioctl length: %d\n", *plen
);
2000 if (get_rfc1002_length(rsp
) < le32_to_cpu(rsp
->OutputOffset
) + *plen
) {
2001 cifs_dbg(VFS
, "Malformed ioctl resp: len %d offset %d\n", *plen
,
2002 le32_to_cpu(rsp
->OutputOffset
));
2008 *out_data
= kmalloc(*plen
, GFP_KERNEL
);
2009 if (*out_data
== NULL
) {
2014 shdr
= get_sync_hdr(rsp
);
2015 memcpy(*out_data
, (char *)shdr
+ le32_to_cpu(rsp
->OutputOffset
), *plen
);
2017 free_rsp_buf(resp_buftype
, rsp
);
2022 * Individual callers to ioctl worker function follow
2026 SMB2_set_compression(const unsigned int xid
, struct cifs_tcon
*tcon
,
2027 u64 persistent_fid
, u64 volatile_fid
)
2030 struct compress_ioctl fsctl_input
;
2031 char *ret_data
= NULL
;
2033 fsctl_input
.CompressionState
=
2034 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT
);
2036 rc
= SMB2_ioctl(xid
, tcon
, persistent_fid
, volatile_fid
,
2037 FSCTL_SET_COMPRESSION
, true /* is_fsctl */,
2038 false /* use_ipc */,
2039 (char *)&fsctl_input
/* data input */,
2040 2 /* in data len */, &ret_data
/* out data */, NULL
);
2042 cifs_dbg(FYI
, "set compression rc %d\n", rc
);
2048 SMB2_close(const unsigned int xid
, struct cifs_tcon
*tcon
,
2049 u64 persistent_fid
, u64 volatile_fid
)
2051 struct smb2_close_req
*req
;
2052 struct smb2_close_rsp
*rsp
;
2053 struct cifs_ses
*ses
= tcon
->ses
;
2055 struct kvec rsp_iov
;
2060 cifs_dbg(FYI
, "Close\n");
2062 if (!ses
|| !(ses
->server
))
2065 rc
= small_smb2_init(SMB2_CLOSE
, tcon
, (void **) &req
);
2069 if (encryption_required(tcon
))
2070 flags
|= CIFS_TRANSFORM_REQ
;
2072 req
->PersistentFileId
= persistent_fid
;
2073 req
->VolatileFileId
= volatile_fid
;
2075 iov
[0].iov_base
= (char *)req
;
2076 /* 4 for rfc1002 length field */
2077 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
2079 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buftype
, flags
, &rsp_iov
);
2080 cifs_small_buf_release(req
);
2081 rsp
= (struct smb2_close_rsp
*)rsp_iov
.iov_base
;
2084 cifs_stats_fail_inc(tcon
, SMB2_CLOSE_HE
);
2088 /* BB FIXME - decode close response, update inode for caching */
2091 free_rsp_buf(resp_buftype
, rsp
);
2096 validate_buf(unsigned int offset
, unsigned int buffer_length
,
2097 struct smb2_hdr
*hdr
, unsigned int min_buf_size
)
2100 unsigned int smb_len
= be32_to_cpu(hdr
->smb2_buf_length
);
2101 char *end_of_smb
= smb_len
+ 4 /* RFC1001 length field */ + (char *)hdr
;
2102 char *begin_of_buf
= 4 /* RFC1001 len field */ + offset
+ (char *)hdr
;
2103 char *end_of_buf
= begin_of_buf
+ buffer_length
;
2106 if (buffer_length
< min_buf_size
) {
2107 cifs_dbg(VFS
, "buffer length %d smaller than minimum size %d\n",
2108 buffer_length
, min_buf_size
);
2112 /* check if beyond RFC1001 maximum length */
2113 if ((smb_len
> 0x7FFFFF) || (buffer_length
> 0x7FFFFF)) {
2114 cifs_dbg(VFS
, "buffer length %d or smb length %d too large\n",
2115 buffer_length
, smb_len
);
2119 if ((begin_of_buf
> end_of_smb
) || (end_of_buf
> end_of_smb
)) {
2120 cifs_dbg(VFS
, "illegal server response, bad offset to data\n");
2128 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2129 * Caller must free buffer.
2132 validate_and_copy_buf(unsigned int offset
, unsigned int buffer_length
,
2133 struct smb2_hdr
*hdr
, unsigned int minbufsize
,
2137 char *begin_of_buf
= 4 /* RFC1001 len field */ + offset
+ (char *)hdr
;
2143 rc
= validate_buf(offset
, buffer_length
, hdr
, minbufsize
);
2147 memcpy(data
, begin_of_buf
, buffer_length
);
2153 query_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
2154 u64 persistent_fid
, u64 volatile_fid
, u8 info_class
, u8 info_type
,
2155 u32 additional_info
, size_t output_len
, size_t min_len
, void **data
,
2158 struct smb2_query_info_req
*req
;
2159 struct smb2_query_info_rsp
*rsp
= NULL
;
2161 struct kvec rsp_iov
;
2164 struct cifs_ses
*ses
= tcon
->ses
;
2167 cifs_dbg(FYI
, "Query Info\n");
2169 if (!ses
|| !(ses
->server
))
2172 rc
= small_smb2_init(SMB2_QUERY_INFO
, tcon
, (void **) &req
);
2176 if (encryption_required(tcon
))
2177 flags
|= CIFS_TRANSFORM_REQ
;
2179 req
->InfoType
= info_type
;
2180 req
->FileInfoClass
= info_class
;
2181 req
->PersistentFileId
= persistent_fid
;
2182 req
->VolatileFileId
= volatile_fid
;
2183 req
->AdditionalInformation
= cpu_to_le32(additional_info
);
2184 /* 4 for rfc1002 length field and 1 for Buffer */
2185 req
->InputBufferOffset
=
2186 cpu_to_le16(sizeof(struct smb2_query_info_req
) - 1 - 4);
2187 req
->OutputBufferLength
= cpu_to_le32(output_len
);
2189 iov
[0].iov_base
= (char *)req
;
2190 /* 4 for rfc1002 length field */
2191 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
2193 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buftype
, flags
, &rsp_iov
);
2194 cifs_small_buf_release(req
);
2195 rsp
= (struct smb2_query_info_rsp
*)rsp_iov
.iov_base
;
2198 cifs_stats_fail_inc(tcon
, SMB2_QUERY_INFO_HE
);
2203 *dlen
= le32_to_cpu(rsp
->OutputBufferLength
);
2205 *data
= kmalloc(*dlen
, GFP_KERNEL
);
2208 "Error %d allocating memory for acl\n",
2216 rc
= validate_and_copy_buf(le16_to_cpu(rsp
->OutputBufferOffset
),
2217 le32_to_cpu(rsp
->OutputBufferLength
),
2218 &rsp
->hdr
, min_len
, *data
);
2221 free_rsp_buf(resp_buftype
, rsp
);
2225 int SMB2_query_eas(const unsigned int xid
, struct cifs_tcon
*tcon
,
2226 u64 persistent_fid
, u64 volatile_fid
,
2227 struct smb2_file_full_ea_info
*data
)
2229 return query_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2230 FILE_FULL_EA_INFORMATION
, SMB2_O_INFO_FILE
, 0,
2232 sizeof(struct smb2_file_full_ea_info
),
2237 int SMB2_query_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
2238 u64 persistent_fid
, u64 volatile_fid
, struct smb2_file_all_info
*data
)
2240 return query_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2241 FILE_ALL_INFORMATION
, SMB2_O_INFO_FILE
, 0,
2242 sizeof(struct smb2_file_all_info
) + PATH_MAX
* 2,
2243 sizeof(struct smb2_file_all_info
), (void **)&data
,
2248 SMB2_query_acl(const unsigned int xid
, struct cifs_tcon
*tcon
,
2249 u64 persistent_fid
, u64 volatile_fid
,
2250 void **data
, u32
*plen
)
2252 __u32 additional_info
= OWNER_SECINFO
| GROUP_SECINFO
| DACL_SECINFO
;
2255 return query_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2256 0, SMB2_O_INFO_SECURITY
, additional_info
,
2257 SMB2_MAX_BUFFER_SIZE
,
2258 sizeof(struct smb2_file_all_info
), data
, plen
);
2262 SMB2_get_srv_num(const unsigned int xid
, struct cifs_tcon
*tcon
,
2263 u64 persistent_fid
, u64 volatile_fid
, __le64
*uniqueid
)
2265 return query_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2266 FILE_INTERNAL_INFORMATION
, SMB2_O_INFO_FILE
, 0,
2267 sizeof(struct smb2_file_internal_info
),
2268 sizeof(struct smb2_file_internal_info
),
2269 (void **)&uniqueid
, NULL
);
2273 * This is a no-op for now. We're not really interested in the reply, but
2274 * rather in the fact that the server sent one and that server->lstrp
2277 * FIXME: maybe we should consider checking that the reply matches request?
2280 smb2_echo_callback(struct mid_q_entry
*mid
)
2282 struct TCP_Server_Info
*server
= mid
->callback_data
;
2283 struct smb2_echo_rsp
*rsp
= (struct smb2_echo_rsp
*)mid
->resp_buf
;
2284 unsigned int credits_received
= 1;
2286 if (mid
->mid_state
== MID_RESPONSE_RECEIVED
)
2287 credits_received
= le16_to_cpu(rsp
->hdr
.sync_hdr
.CreditRequest
);
2289 DeleteMidQEntry(mid
);
2290 add_credits(server
, credits_received
, CIFS_ECHO_OP
);
2293 void smb2_reconnect_server(struct work_struct
*work
)
2295 struct TCP_Server_Info
*server
= container_of(work
,
2296 struct TCP_Server_Info
, reconnect
.work
);
2297 struct cifs_ses
*ses
;
2298 struct cifs_tcon
*tcon
, *tcon2
;
2299 struct list_head tmp_list
;
2300 int tcon_exist
= false;
2302 int resched
= false;
2305 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2306 mutex_lock(&server
->reconnect_mutex
);
2308 INIT_LIST_HEAD(&tmp_list
);
2309 cifs_dbg(FYI
, "Need negotiate, reconnecting tcons\n");
2311 spin_lock(&cifs_tcp_ses_lock
);
2312 list_for_each_entry(ses
, &server
->smb_ses_list
, smb_ses_list
) {
2313 list_for_each_entry(tcon
, &ses
->tcon_list
, tcon_list
) {
2314 if (tcon
->need_reconnect
|| tcon
->need_reopen_files
) {
2316 list_add_tail(&tcon
->rlist
, &tmp_list
);
2322 * Get the reference to server struct to be sure that the last call of
2323 * cifs_put_tcon() in the loop below won't release the server pointer.
2326 server
->srv_count
++;
2328 spin_unlock(&cifs_tcp_ses_lock
);
2330 list_for_each_entry_safe(tcon
, tcon2
, &tmp_list
, rlist
) {
2331 rc
= smb2_reconnect(SMB2_INTERNAL_CMD
, tcon
);
2333 cifs_reopen_persistent_handles(tcon
);
2336 list_del_init(&tcon
->rlist
);
2337 cifs_put_tcon(tcon
);
2340 cifs_dbg(FYI
, "Reconnecting tcons finished\n");
2342 queue_delayed_work(cifsiod_wq
, &server
->reconnect
, 2 * HZ
);
2343 mutex_unlock(&server
->reconnect_mutex
);
2345 /* now we can safely release srv struct */
2347 cifs_put_tcp_session(server
, 1);
2351 SMB2_echo(struct TCP_Server_Info
*server
)
2353 struct smb2_echo_req
*req
;
2356 struct smb_rqst rqst
= { .rq_iov
= iov
,
2359 cifs_dbg(FYI
, "In echo request\n");
2361 if (server
->tcpStatus
== CifsNeedNegotiate
) {
2362 /* No need to send echo on newly established connections */
2363 queue_delayed_work(cifsiod_wq
, &server
->reconnect
, 0);
2367 rc
= small_smb2_init(SMB2_ECHO
, NULL
, (void **)&req
);
2371 req
->hdr
.sync_hdr
.CreditRequest
= cpu_to_le16(1);
2373 /* 4 for rfc1002 length field */
2375 iov
[0].iov_base
= (char *)req
;
2376 iov
[1].iov_len
= get_rfc1002_length(req
);
2377 iov
[1].iov_base
= (char *)req
+ 4;
2379 rc
= cifs_call_async(server
, &rqst
, NULL
, smb2_echo_callback
, NULL
,
2380 server
, CIFS_ECHO_OP
);
2382 cifs_dbg(FYI
, "Echo request failed: %d\n", rc
);
2384 cifs_small_buf_release(req
);
2389 SMB2_flush(const unsigned int xid
, struct cifs_tcon
*tcon
, u64 persistent_fid
,
2392 struct smb2_flush_req
*req
;
2393 struct cifs_ses
*ses
= tcon
->ses
;
2395 struct kvec rsp_iov
;
2400 cifs_dbg(FYI
, "Flush\n");
2402 if (!ses
|| !(ses
->server
))
2405 rc
= small_smb2_init(SMB2_FLUSH
, tcon
, (void **) &req
);
2409 if (encryption_required(tcon
))
2410 flags
|= CIFS_TRANSFORM_REQ
;
2412 req
->PersistentFileId
= persistent_fid
;
2413 req
->VolatileFileId
= volatile_fid
;
2415 iov
[0].iov_base
= (char *)req
;
2416 /* 4 for rfc1002 length field */
2417 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
2419 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buftype
, flags
, &rsp_iov
);
2420 cifs_small_buf_release(req
);
2423 cifs_stats_fail_inc(tcon
, SMB2_FLUSH_HE
);
2425 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
2430 * To form a chain of read requests, any read requests after the first should
2431 * have the end_of_chain boolean set to true.
2434 smb2_new_read_req(void **buf
, unsigned int *total_len
,
2435 struct cifs_io_parms
*io_parms
, unsigned int remaining_bytes
,
2439 struct smb2_read_plain_req
*req
= NULL
;
2440 struct smb2_sync_hdr
*shdr
;
2442 rc
= smb2_plain_req_init(SMB2_READ
, io_parms
->tcon
, (void **) &req
,
2446 if (io_parms
->tcon
->ses
->server
== NULL
)
2447 return -ECONNABORTED
;
2449 shdr
= &req
->sync_hdr
;
2450 shdr
->ProcessId
= cpu_to_le32(io_parms
->pid
);
2452 req
->PersistentFileId
= io_parms
->persistent_fid
;
2453 req
->VolatileFileId
= io_parms
->volatile_fid
;
2454 req
->ReadChannelInfoOffset
= 0; /* reserved */
2455 req
->ReadChannelInfoLength
= 0; /* reserved */
2456 req
->Channel
= 0; /* reserved */
2457 req
->MinimumCount
= 0;
2458 req
->Length
= cpu_to_le32(io_parms
->length
);
2459 req
->Offset
= cpu_to_le64(io_parms
->offset
);
2461 if (request_type
& CHAINED_REQUEST
) {
2462 if (!(request_type
& END_OF_CHAIN
)) {
2463 /* next 8-byte aligned request */
2464 *total_len
= DIV_ROUND_UP(*total_len
, 8) * 8;
2465 shdr
->NextCommand
= cpu_to_le32(*total_len
);
2466 } else /* END_OF_CHAIN */
2467 shdr
->NextCommand
= 0;
2468 if (request_type
& RELATED_REQUEST
) {
2469 shdr
->Flags
|= SMB2_FLAGS_RELATED_OPERATIONS
;
2471 * Related requests use info from previous read request
2474 shdr
->SessionId
= 0xFFFFFFFF;
2475 shdr
->TreeId
= 0xFFFFFFFF;
2476 req
->PersistentFileId
= 0xFFFFFFFF;
2477 req
->VolatileFileId
= 0xFFFFFFFF;
2480 if (remaining_bytes
> io_parms
->length
)
2481 req
->RemainingBytes
= cpu_to_le32(remaining_bytes
);
2483 req
->RemainingBytes
= 0;
2490 smb2_readv_callback(struct mid_q_entry
*mid
)
2492 struct cifs_readdata
*rdata
= mid
->callback_data
;
2493 struct cifs_tcon
*tcon
= tlink_tcon(rdata
->cfile
->tlink
);
2494 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
2495 struct smb2_sync_hdr
*shdr
=
2496 (struct smb2_sync_hdr
*)rdata
->iov
[1].iov_base
;
2497 unsigned int credits_received
= 1;
2498 struct smb_rqst rqst
= { .rq_iov
= rdata
->iov
,
2500 .rq_pages
= rdata
->pages
,
2501 .rq_npages
= rdata
->nr_pages
,
2502 .rq_pagesz
= rdata
->pagesz
,
2503 .rq_tailsz
= rdata
->tailsz
};
2505 cifs_dbg(FYI
, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2506 __func__
, mid
->mid
, mid
->mid_state
, rdata
->result
,
2509 switch (mid
->mid_state
) {
2510 case MID_RESPONSE_RECEIVED
:
2511 credits_received
= le16_to_cpu(shdr
->CreditRequest
);
2512 /* result already set, check signature */
2513 if (server
->sign
&& !mid
->decrypted
) {
2516 rc
= smb2_verify_signature(&rqst
, server
);
2518 cifs_dbg(VFS
, "SMB signature verification returned error = %d\n",
2521 /* FIXME: should this be counted toward the initiating task? */
2522 task_io_account_read(rdata
->got_bytes
);
2523 cifs_stats_bytes_read(tcon
, rdata
->got_bytes
);
2525 case MID_REQUEST_SUBMITTED
:
2526 case MID_RETRY_NEEDED
:
2527 rdata
->result
= -EAGAIN
;
2528 if (server
->sign
&& rdata
->got_bytes
)
2529 /* reset bytes number since we can not check a sign */
2530 rdata
->got_bytes
= 0;
2531 /* FIXME: should this be counted toward the initiating task? */
2532 task_io_account_read(rdata
->got_bytes
);
2533 cifs_stats_bytes_read(tcon
, rdata
->got_bytes
);
2536 if (rdata
->result
!= -ENODATA
)
2537 rdata
->result
= -EIO
;
2541 cifs_stats_fail_inc(tcon
, SMB2_READ_HE
);
2543 queue_work(cifsiod_wq
, &rdata
->work
);
2544 DeleteMidQEntry(mid
);
2545 add_credits(server
, credits_received
, 0);
2548 /* smb2_async_readv - send an async read, and set up mid to handle result */
2550 smb2_async_readv(struct cifs_readdata
*rdata
)
2554 struct smb2_sync_hdr
*shdr
;
2555 struct cifs_io_parms io_parms
;
2556 struct smb_rqst rqst
= { .rq_iov
= rdata
->iov
,
2558 struct TCP_Server_Info
*server
;
2559 unsigned int total_len
;
2562 cifs_dbg(FYI
, "%s: offset=%llu bytes=%u\n",
2563 __func__
, rdata
->offset
, rdata
->bytes
);
2565 io_parms
.tcon
= tlink_tcon(rdata
->cfile
->tlink
);
2566 io_parms
.offset
= rdata
->offset
;
2567 io_parms
.length
= rdata
->bytes
;
2568 io_parms
.persistent_fid
= rdata
->cfile
->fid
.persistent_fid
;
2569 io_parms
.volatile_fid
= rdata
->cfile
->fid
.volatile_fid
;
2570 io_parms
.pid
= rdata
->pid
;
2572 server
= io_parms
.tcon
->ses
->server
;
2574 rc
= smb2_new_read_req((void **) &buf
, &total_len
, &io_parms
, 0, 0);
2576 if (rc
== -EAGAIN
&& rdata
->credits
) {
2577 /* credits was reset by reconnect */
2579 /* reduce in_flight value since we won't send the req */
2580 spin_lock(&server
->req_lock
);
2581 server
->in_flight
--;
2582 spin_unlock(&server
->req_lock
);
2587 if (encryption_required(io_parms
.tcon
))
2588 flags
|= CIFS_TRANSFORM_REQ
;
2590 req_len
= cpu_to_be32(total_len
);
2592 rdata
->iov
[0].iov_base
= &req_len
;
2593 rdata
->iov
[0].iov_len
= sizeof(__be32
);
2594 rdata
->iov
[1].iov_base
= buf
;
2595 rdata
->iov
[1].iov_len
= total_len
;
2597 shdr
= (struct smb2_sync_hdr
*)buf
;
2599 if (rdata
->credits
) {
2600 shdr
->CreditCharge
= cpu_to_le16(DIV_ROUND_UP(rdata
->bytes
,
2601 SMB2_MAX_BUFFER_SIZE
));
2602 shdr
->CreditRequest
= shdr
->CreditCharge
;
2603 spin_lock(&server
->req_lock
);
2604 server
->credits
+= rdata
->credits
-
2605 le16_to_cpu(shdr
->CreditCharge
);
2606 spin_unlock(&server
->req_lock
);
2607 wake_up(&server
->request_q
);
2608 flags
|= CIFS_HAS_CREDITS
;
2611 kref_get(&rdata
->refcount
);
2612 rc
= cifs_call_async(io_parms
.tcon
->ses
->server
, &rqst
,
2613 cifs_readv_receive
, smb2_readv_callback
,
2614 smb3_handle_read_data
, rdata
, flags
);
2616 kref_put(&rdata
->refcount
, cifs_readdata_release
);
2617 cifs_stats_fail_inc(io_parms
.tcon
, SMB2_READ_HE
);
2620 cifs_small_buf_release(buf
);
2625 SMB2_read(const unsigned int xid
, struct cifs_io_parms
*io_parms
,
2626 unsigned int *nbytes
, char **buf
, int *buf_type
)
2628 int resp_buftype
, rc
= -EACCES
;
2629 struct smb2_read_plain_req
*req
= NULL
;
2630 struct smb2_read_rsp
*rsp
= NULL
;
2631 struct smb2_sync_hdr
*shdr
;
2633 struct kvec rsp_iov
;
2634 unsigned int total_len
;
2636 struct smb_rqst rqst
= { .rq_iov
= iov
,
2638 int flags
= CIFS_LOG_ERROR
;
2639 struct cifs_ses
*ses
= io_parms
->tcon
->ses
;
2642 rc
= smb2_new_read_req((void **)&req
, &total_len
, io_parms
, 0, 0);
2646 if (encryption_required(io_parms
->tcon
))
2647 flags
|= CIFS_TRANSFORM_REQ
;
2649 req_len
= cpu_to_be32(total_len
);
2651 iov
[0].iov_base
= &req_len
;
2652 iov
[0].iov_len
= sizeof(__be32
);
2653 iov
[1].iov_base
= req
;
2654 iov
[1].iov_len
= total_len
;
2656 rc
= cifs_send_recv(xid
, ses
, &rqst
, &resp_buftype
, flags
, &rsp_iov
);
2657 cifs_small_buf_release(req
);
2659 rsp
= (struct smb2_read_rsp
*)rsp_iov
.iov_base
;
2660 shdr
= get_sync_hdr(rsp
);
2662 if (shdr
->Status
== STATUS_END_OF_FILE
) {
2663 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
2668 cifs_stats_fail_inc(io_parms
->tcon
, SMB2_READ_HE
);
2669 cifs_dbg(VFS
, "Send error in read = %d\n", rc
);
2671 *nbytes
= le32_to_cpu(rsp
->DataLength
);
2672 if ((*nbytes
> CIFS_MAX_MSGSIZE
) ||
2673 (*nbytes
> io_parms
->length
)) {
2674 cifs_dbg(FYI
, "bad length %d for count %d\n",
2675 *nbytes
, io_parms
->length
);
2682 memcpy(*buf
, (char *)shdr
+ rsp
->DataOffset
, *nbytes
);
2683 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
2684 } else if (resp_buftype
!= CIFS_NO_BUFFER
) {
2685 *buf
= rsp_iov
.iov_base
;
2686 if (resp_buftype
== CIFS_SMALL_BUFFER
)
2687 *buf_type
= CIFS_SMALL_BUFFER
;
2688 else if (resp_buftype
== CIFS_LARGE_BUFFER
)
2689 *buf_type
= CIFS_LARGE_BUFFER
;
2695 * Check the mid_state and signature on received buffer (if any), and queue the
2696 * workqueue completion task.
2699 smb2_writev_callback(struct mid_q_entry
*mid
)
2701 struct cifs_writedata
*wdata
= mid
->callback_data
;
2702 struct cifs_tcon
*tcon
= tlink_tcon(wdata
->cfile
->tlink
);
2703 unsigned int written
;
2704 struct smb2_write_rsp
*rsp
= (struct smb2_write_rsp
*)mid
->resp_buf
;
2705 unsigned int credits_received
= 1;
2707 switch (mid
->mid_state
) {
2708 case MID_RESPONSE_RECEIVED
:
2709 credits_received
= le16_to_cpu(rsp
->hdr
.sync_hdr
.CreditRequest
);
2710 wdata
->result
= smb2_check_receive(mid
, tcon
->ses
->server
, 0);
2711 if (wdata
->result
!= 0)
2714 written
= le32_to_cpu(rsp
->DataLength
);
2716 * Mask off high 16 bits when bytes written as returned
2717 * by the server is greater than bytes requested by the
2718 * client. OS/2 servers are known to set incorrect
2721 if (written
> wdata
->bytes
)
2724 if (written
< wdata
->bytes
)
2725 wdata
->result
= -ENOSPC
;
2727 wdata
->bytes
= written
;
2729 case MID_REQUEST_SUBMITTED
:
2730 case MID_RETRY_NEEDED
:
2731 wdata
->result
= -EAGAIN
;
2734 wdata
->result
= -EIO
;
2739 cifs_stats_fail_inc(tcon
, SMB2_WRITE_HE
);
2741 queue_work(cifsiod_wq
, &wdata
->work
);
2742 DeleteMidQEntry(mid
);
2743 add_credits(tcon
->ses
->server
, credits_received
, 0);
2746 /* smb2_async_writev - send an async write, and set up mid to handle result */
2748 smb2_async_writev(struct cifs_writedata
*wdata
,
2749 void (*release
)(struct kref
*kref
))
2751 int rc
= -EACCES
, flags
= 0;
2752 struct smb2_write_req
*req
= NULL
;
2753 struct smb2_sync_hdr
*shdr
;
2754 struct cifs_tcon
*tcon
= tlink_tcon(wdata
->cfile
->tlink
);
2755 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
2757 struct smb_rqst rqst
= { };
2759 rc
= small_smb2_init(SMB2_WRITE
, tcon
, (void **) &req
);
2761 if (rc
== -EAGAIN
&& wdata
->credits
) {
2762 /* credits was reset by reconnect */
2764 /* reduce in_flight value since we won't send the req */
2765 spin_lock(&server
->req_lock
);
2766 server
->in_flight
--;
2767 spin_unlock(&server
->req_lock
);
2769 goto async_writev_out
;
2772 if (encryption_required(tcon
))
2773 flags
|= CIFS_TRANSFORM_REQ
;
2775 shdr
= get_sync_hdr(req
);
2776 shdr
->ProcessId
= cpu_to_le32(wdata
->cfile
->pid
);
2778 req
->PersistentFileId
= wdata
->cfile
->fid
.persistent_fid
;
2779 req
->VolatileFileId
= wdata
->cfile
->fid
.volatile_fid
;
2780 req
->WriteChannelInfoOffset
= 0;
2781 req
->WriteChannelInfoLength
= 0;
2783 req
->Offset
= cpu_to_le64(wdata
->offset
);
2784 /* 4 for rfc1002 length field */
2785 req
->DataOffset
= cpu_to_le16(
2786 offsetof(struct smb2_write_req
, Buffer
) - 4);
2787 req
->RemainingBytes
= 0;
2789 /* 4 for rfc1002 length field and 1 for Buffer */
2791 iov
[0].iov_base
= req
;
2792 iov
[1].iov_len
= get_rfc1002_length(req
) - 1;
2793 iov
[1].iov_base
= (char *)req
+ 4;
2797 rqst
.rq_pages
= wdata
->pages
;
2798 rqst
.rq_npages
= wdata
->nr_pages
;
2799 rqst
.rq_pagesz
= wdata
->pagesz
;
2800 rqst
.rq_tailsz
= wdata
->tailsz
;
2802 cifs_dbg(FYI
, "async write at %llu %u bytes\n",
2803 wdata
->offset
, wdata
->bytes
);
2805 req
->Length
= cpu_to_le32(wdata
->bytes
);
2807 inc_rfc1001_len(&req
->hdr
, wdata
->bytes
- 1 /* Buffer */);
2809 if (wdata
->credits
) {
2810 shdr
->CreditCharge
= cpu_to_le16(DIV_ROUND_UP(wdata
->bytes
,
2811 SMB2_MAX_BUFFER_SIZE
));
2812 shdr
->CreditRequest
= shdr
->CreditCharge
;
2813 spin_lock(&server
->req_lock
);
2814 server
->credits
+= wdata
->credits
-
2815 le16_to_cpu(shdr
->CreditCharge
);
2816 spin_unlock(&server
->req_lock
);
2817 wake_up(&server
->request_q
);
2818 flags
|= CIFS_HAS_CREDITS
;
2821 kref_get(&wdata
->refcount
);
2822 rc
= cifs_call_async(server
, &rqst
, NULL
, smb2_writev_callback
, NULL
,
2826 kref_put(&wdata
->refcount
, release
);
2827 cifs_stats_fail_inc(tcon
, SMB2_WRITE_HE
);
2831 cifs_small_buf_release(req
);
2836 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2837 * The length field from io_parms must be at least 1 and indicates a number of
2838 * elements with data to write that begins with position 1 in iov array. All
2839 * data length is specified by count.
2842 SMB2_write(const unsigned int xid
, struct cifs_io_parms
*io_parms
,
2843 unsigned int *nbytes
, struct kvec
*iov
, int n_vec
)
2846 struct smb2_write_req
*req
= NULL
;
2847 struct smb2_write_rsp
*rsp
= NULL
;
2849 struct kvec rsp_iov
;
2857 rc
= small_smb2_init(SMB2_WRITE
, io_parms
->tcon
, (void **) &req
);
2861 if (io_parms
->tcon
->ses
->server
== NULL
)
2862 return -ECONNABORTED
;
2864 if (encryption_required(io_parms
->tcon
))
2865 flags
|= CIFS_TRANSFORM_REQ
;
2867 req
->hdr
.sync_hdr
.ProcessId
= cpu_to_le32(io_parms
->pid
);
2869 req
->PersistentFileId
= io_parms
->persistent_fid
;
2870 req
->VolatileFileId
= io_parms
->volatile_fid
;
2871 req
->WriteChannelInfoOffset
= 0;
2872 req
->WriteChannelInfoLength
= 0;
2874 req
->Length
= cpu_to_le32(io_parms
->length
);
2875 req
->Offset
= cpu_to_le64(io_parms
->offset
);
2876 /* 4 for rfc1002 length field */
2877 req
->DataOffset
= cpu_to_le16(
2878 offsetof(struct smb2_write_req
, Buffer
) - 4);
2879 req
->RemainingBytes
= 0;
2881 iov
[0].iov_base
= (char *)req
;
2882 /* 4 for rfc1002 length field and 1 for Buffer */
2883 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
2885 /* length of entire message including data to be written */
2886 inc_rfc1001_len(req
, io_parms
->length
- 1 /* Buffer */);
2888 rc
= SendReceive2(xid
, io_parms
->tcon
->ses
, iov
, n_vec
+ 1,
2889 &resp_buftype
, flags
, &rsp_iov
);
2890 cifs_small_buf_release(req
);
2891 rsp
= (struct smb2_write_rsp
*)rsp_iov
.iov_base
;
2894 cifs_stats_fail_inc(io_parms
->tcon
, SMB2_WRITE_HE
);
2895 cifs_dbg(VFS
, "Send error in write = %d\n", rc
);
2897 *nbytes
= le32_to_cpu(rsp
->DataLength
);
2899 free_rsp_buf(resp_buftype
, rsp
);
2904 num_entries(char *bufstart
, char *end_of_buf
, char **lastentry
, size_t size
)
2907 unsigned int entrycount
= 0;
2908 unsigned int next_offset
= 0;
2909 FILE_DIRECTORY_INFO
*entryptr
;
2911 if (bufstart
== NULL
)
2914 entryptr
= (FILE_DIRECTORY_INFO
*)bufstart
;
2917 entryptr
= (FILE_DIRECTORY_INFO
*)
2918 ((char *)entryptr
+ next_offset
);
2920 if ((char *)entryptr
+ size
> end_of_buf
) {
2921 cifs_dbg(VFS
, "malformed search entry would overflow\n");
2925 len
= le32_to_cpu(entryptr
->FileNameLength
);
2926 if ((char *)entryptr
+ len
+ size
> end_of_buf
) {
2927 cifs_dbg(VFS
, "directory entry name would overflow frame end of buf %p\n",
2932 *lastentry
= (char *)entryptr
;
2935 next_offset
= le32_to_cpu(entryptr
->NextEntryOffset
);
2947 SMB2_query_directory(const unsigned int xid
, struct cifs_tcon
*tcon
,
2948 u64 persistent_fid
, u64 volatile_fid
, int index
,
2949 struct cifs_search_info
*srch_inf
)
2951 struct smb2_query_directory_req
*req
;
2952 struct smb2_query_directory_rsp
*rsp
= NULL
;
2954 struct kvec rsp_iov
;
2957 int resp_buftype
= CIFS_NO_BUFFER
;
2958 unsigned char *bufptr
;
2959 struct TCP_Server_Info
*server
;
2960 struct cifs_ses
*ses
= tcon
->ses
;
2961 __le16 asteriks
= cpu_to_le16('*');
2963 unsigned int output_size
= CIFSMaxBufSize
;
2964 size_t info_buf_size
;
2967 if (ses
&& (ses
->server
))
2968 server
= ses
->server
;
2972 rc
= small_smb2_init(SMB2_QUERY_DIRECTORY
, tcon
, (void **) &req
);
2976 if (encryption_required(tcon
))
2977 flags
|= CIFS_TRANSFORM_REQ
;
2979 switch (srch_inf
->info_level
) {
2980 case SMB_FIND_FILE_DIRECTORY_INFO
:
2981 req
->FileInformationClass
= FILE_DIRECTORY_INFORMATION
;
2982 info_buf_size
= sizeof(FILE_DIRECTORY_INFO
) - 1;
2984 case SMB_FIND_FILE_ID_FULL_DIR_INFO
:
2985 req
->FileInformationClass
= FILEID_FULL_DIRECTORY_INFORMATION
;
2986 info_buf_size
= sizeof(SEARCH_ID_FULL_DIR_INFO
) - 1;
2989 cifs_dbg(VFS
, "info level %u isn't supported\n",
2990 srch_inf
->info_level
);
2995 req
->FileIndex
= cpu_to_le32(index
);
2996 req
->PersistentFileId
= persistent_fid
;
2997 req
->VolatileFileId
= volatile_fid
;
3000 bufptr
= req
->Buffer
;
3001 memcpy(bufptr
, &asteriks
, len
);
3003 req
->FileNameOffset
=
3004 cpu_to_le16(sizeof(struct smb2_query_directory_req
) - 1 - 4);
3005 req
->FileNameLength
= cpu_to_le16(len
);
3007 * BB could be 30 bytes or so longer if we used SMB2 specific
3008 * buffer lengths, but this is safe and close enough.
3010 output_size
= min_t(unsigned int, output_size
, server
->maxBuf
);
3011 output_size
= min_t(unsigned int, output_size
, 2 << 15);
3012 req
->OutputBufferLength
= cpu_to_le32(output_size
);
3014 iov
[0].iov_base
= (char *)req
;
3015 /* 4 for RFC1001 length and 1 for Buffer */
3016 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
3018 iov
[1].iov_base
= (char *)(req
->Buffer
);
3019 iov
[1].iov_len
= len
;
3021 inc_rfc1001_len(req
, len
- 1 /* Buffer */);
3023 rc
= SendReceive2(xid
, ses
, iov
, 2, &resp_buftype
, flags
, &rsp_iov
);
3024 cifs_small_buf_release(req
);
3025 rsp
= (struct smb2_query_directory_rsp
*)rsp_iov
.iov_base
;
3028 if (rc
== -ENODATA
&&
3029 rsp
->hdr
.sync_hdr
.Status
== STATUS_NO_MORE_FILES
) {
3030 srch_inf
->endOfSearch
= true;
3033 cifs_stats_fail_inc(tcon
, SMB2_QUERY_DIRECTORY_HE
);
3037 rc
= validate_buf(le16_to_cpu(rsp
->OutputBufferOffset
),
3038 le32_to_cpu(rsp
->OutputBufferLength
), &rsp
->hdr
,
3043 srch_inf
->unicode
= true;
3045 if (srch_inf
->ntwrk_buf_start
) {
3046 if (srch_inf
->smallBuf
)
3047 cifs_small_buf_release(srch_inf
->ntwrk_buf_start
);
3049 cifs_buf_release(srch_inf
->ntwrk_buf_start
);
3051 srch_inf
->ntwrk_buf_start
= (char *)rsp
;
3052 srch_inf
->srch_entries_start
= srch_inf
->last_entry
= 4 /* rfclen */ +
3053 (char *)&rsp
->hdr
+ le16_to_cpu(rsp
->OutputBufferOffset
);
3054 /* 4 for rfc1002 length field */
3055 end_of_smb
= get_rfc1002_length(rsp
) + 4 + (char *)&rsp
->hdr
;
3056 srch_inf
->entries_in_buffer
=
3057 num_entries(srch_inf
->srch_entries_start
, end_of_smb
,
3058 &srch_inf
->last_entry
, info_buf_size
);
3059 srch_inf
->index_of_last_entry
+= srch_inf
->entries_in_buffer
;
3060 cifs_dbg(FYI
, "num entries %d last_index %lld srch start %p srch end %p\n",
3061 srch_inf
->entries_in_buffer
, srch_inf
->index_of_last_entry
,
3062 srch_inf
->srch_entries_start
, srch_inf
->last_entry
);
3063 if (resp_buftype
== CIFS_LARGE_BUFFER
)
3064 srch_inf
->smallBuf
= false;
3065 else if (resp_buftype
== CIFS_SMALL_BUFFER
)
3066 srch_inf
->smallBuf
= true;
3068 cifs_dbg(VFS
, "illegal search buffer type\n");
3073 free_rsp_buf(resp_buftype
, rsp
);
3078 send_set_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
3079 u64 persistent_fid
, u64 volatile_fid
, u32 pid
, u8 info_class
,
3080 u8 info_type
, u32 additional_info
, unsigned int num
,
3081 void **data
, unsigned int *size
)
3083 struct smb2_set_info_req
*req
;
3084 struct smb2_set_info_rsp
*rsp
= NULL
;
3086 struct kvec rsp_iov
;
3090 struct cifs_ses
*ses
= tcon
->ses
;
3093 if (!ses
|| !(ses
->server
))
3099 iov
= kmalloc(sizeof(struct kvec
) * num
, GFP_KERNEL
);
3103 rc
= small_smb2_init(SMB2_SET_INFO
, tcon
, (void **) &req
);
3109 if (encryption_required(tcon
))
3110 flags
|= CIFS_TRANSFORM_REQ
;
3112 req
->hdr
.sync_hdr
.ProcessId
= cpu_to_le32(pid
);
3114 req
->InfoType
= info_type
;
3115 req
->FileInfoClass
= info_class
;
3116 req
->PersistentFileId
= persistent_fid
;
3117 req
->VolatileFileId
= volatile_fid
;
3118 req
->AdditionalInformation
= cpu_to_le32(additional_info
);
3120 /* 4 for RFC1001 length and 1 for Buffer */
3122 cpu_to_le16(sizeof(struct smb2_set_info_req
) - 1 - 4);
3123 req
->BufferLength
= cpu_to_le32(*size
);
3125 inc_rfc1001_len(req
, *size
- 1 /* Buffer */);
3127 memcpy(req
->Buffer
, *data
, *size
);
3129 iov
[0].iov_base
= (char *)req
;
3130 /* 4 for RFC1001 length */
3131 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
3133 for (i
= 1; i
< num
; i
++) {
3134 inc_rfc1001_len(req
, size
[i
]);
3135 le32_add_cpu(&req
->BufferLength
, size
[i
]);
3136 iov
[i
].iov_base
= (char *)data
[i
];
3137 iov
[i
].iov_len
= size
[i
];
3140 rc
= SendReceive2(xid
, ses
, iov
, num
, &resp_buftype
, flags
, &rsp_iov
);
3141 cifs_small_buf_release(req
);
3142 rsp
= (struct smb2_set_info_rsp
*)rsp_iov
.iov_base
;
3145 cifs_stats_fail_inc(tcon
, SMB2_SET_INFO_HE
);
3147 free_rsp_buf(resp_buftype
, rsp
);
3153 SMB2_rename(const unsigned int xid
, struct cifs_tcon
*tcon
,
3154 u64 persistent_fid
, u64 volatile_fid
, __le16
*target_file
)
3156 struct smb2_file_rename_info info
;
3158 unsigned int size
[2];
3160 int len
= (2 * UniStrnlen((wchar_t *)target_file
, PATH_MAX
));
3162 data
= kmalloc(sizeof(void *) * 2, GFP_KERNEL
);
3166 info
.ReplaceIfExists
= 1; /* 1 = replace existing target with new */
3167 /* 0 = fail if target already exists */
3168 info
.RootDirectory
= 0; /* MBZ for network ops (why does spec say?) */
3169 info
.FileNameLength
= cpu_to_le32(len
);
3172 size
[0] = sizeof(struct smb2_file_rename_info
);
3174 data
[1] = target_file
;
3175 size
[1] = len
+ 2 /* null */;
3177 rc
= send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3178 current
->tgid
, FILE_RENAME_INFORMATION
, SMB2_O_INFO_FILE
,
3185 SMB2_rmdir(const unsigned int xid
, struct cifs_tcon
*tcon
,
3186 u64 persistent_fid
, u64 volatile_fid
)
3188 __u8 delete_pending
= 1;
3192 data
= &delete_pending
;
3193 size
= 1; /* sizeof __u8 */
3195 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3196 current
->tgid
, FILE_DISPOSITION_INFORMATION
, SMB2_O_INFO_FILE
,
3197 0, 1, &data
, &size
);
3201 SMB2_set_hardlink(const unsigned int xid
, struct cifs_tcon
*tcon
,
3202 u64 persistent_fid
, u64 volatile_fid
, __le16
*target_file
)
3204 struct smb2_file_link_info info
;
3206 unsigned int size
[2];
3208 int len
= (2 * UniStrnlen((wchar_t *)target_file
, PATH_MAX
));
3210 data
= kmalloc(sizeof(void *) * 2, GFP_KERNEL
);
3214 info
.ReplaceIfExists
= 0; /* 1 = replace existing link with new */
3215 /* 0 = fail if link already exists */
3216 info
.RootDirectory
= 0; /* MBZ for network ops (why does spec say?) */
3217 info
.FileNameLength
= cpu_to_le32(len
);
3220 size
[0] = sizeof(struct smb2_file_link_info
);
3222 data
[1] = target_file
;
3223 size
[1] = len
+ 2 /* null */;
3225 rc
= send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3226 current
->tgid
, FILE_LINK_INFORMATION
, SMB2_O_INFO_FILE
,
3233 SMB2_set_eof(const unsigned int xid
, struct cifs_tcon
*tcon
, u64 persistent_fid
,
3234 u64 volatile_fid
, u32 pid
, __le64
*eof
, bool is_falloc
)
3236 struct smb2_file_eof_info info
;
3240 info
.EndOfFile
= *eof
;
3243 size
= sizeof(struct smb2_file_eof_info
);
3246 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3247 pid
, FILE_ALLOCATION_INFORMATION
, SMB2_O_INFO_FILE
,
3248 0, 1, &data
, &size
);
3250 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3251 pid
, FILE_END_OF_FILE_INFORMATION
, SMB2_O_INFO_FILE
,
3252 0, 1, &data
, &size
);
3256 SMB2_set_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
3257 u64 persistent_fid
, u64 volatile_fid
, FILE_BASIC_INFO
*buf
)
3260 size
= sizeof(FILE_BASIC_INFO
);
3261 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3262 current
->tgid
, FILE_BASIC_INFORMATION
, SMB2_O_INFO_FILE
,
3263 0, 1, (void **)&buf
, &size
);
3267 SMB2_set_acl(const unsigned int xid
, struct cifs_tcon
*tcon
,
3268 u64 persistent_fid
, u64 volatile_fid
,
3269 struct cifs_ntsd
*pnntsd
, int pacllen
, int aclflag
)
3271 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3272 current
->tgid
, 0, SMB2_O_INFO_SECURITY
, aclflag
,
3273 1, (void **)&pnntsd
, &pacllen
);
3277 SMB2_set_ea(const unsigned int xid
, struct cifs_tcon
*tcon
,
3278 u64 persistent_fid
, u64 volatile_fid
,
3279 struct smb2_file_full_ea_info
*buf
, int len
)
3281 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3282 current
->tgid
, FILE_FULL_EA_INFORMATION
, SMB2_O_INFO_FILE
,
3283 0, 1, (void **)&buf
, &len
);
3287 SMB2_oplock_break(const unsigned int xid
, struct cifs_tcon
*tcon
,
3288 const u64 persistent_fid
, const u64 volatile_fid
,
3292 struct smb2_oplock_break
*req
= NULL
;
3293 int flags
= CIFS_OBREAK_OP
;
3295 cifs_dbg(FYI
, "SMB2_oplock_break\n");
3296 rc
= small_smb2_init(SMB2_OPLOCK_BREAK
, tcon
, (void **) &req
);
3300 if (encryption_required(tcon
))
3301 flags
|= CIFS_TRANSFORM_REQ
;
3303 req
->VolatileFid
= volatile_fid
;
3304 req
->PersistentFid
= persistent_fid
;
3305 req
->OplockLevel
= oplock_level
;
3306 req
->hdr
.sync_hdr
.CreditRequest
= cpu_to_le16(1);
3308 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) req
, flags
);
3309 cifs_small_buf_release(req
);
3312 cifs_stats_fail_inc(tcon
, SMB2_OPLOCK_BREAK_HE
);
3313 cifs_dbg(FYI
, "Send error in Oplock Break = %d\n", rc
);
3320 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info
*pfs_inf
,
3321 struct kstatfs
*kst
)
3323 kst
->f_bsize
= le32_to_cpu(pfs_inf
->BytesPerSector
) *
3324 le32_to_cpu(pfs_inf
->SectorsPerAllocationUnit
);
3325 kst
->f_blocks
= le64_to_cpu(pfs_inf
->TotalAllocationUnits
);
3326 kst
->f_bfree
= kst
->f_bavail
=
3327 le64_to_cpu(pfs_inf
->CallerAvailableAllocationUnits
);
3332 build_qfs_info_req(struct kvec
*iov
, struct cifs_tcon
*tcon
, int level
,
3333 int outbuf_len
, u64 persistent_fid
, u64 volatile_fid
)
3336 struct smb2_query_info_req
*req
;
3338 cifs_dbg(FYI
, "Query FSInfo level %d\n", level
);
3340 if ((tcon
->ses
== NULL
) || (tcon
->ses
->server
== NULL
))
3343 rc
= small_smb2_init(SMB2_QUERY_INFO
, tcon
, (void **) &req
);
3347 req
->InfoType
= SMB2_O_INFO_FILESYSTEM
;
3348 req
->FileInfoClass
= level
;
3349 req
->PersistentFileId
= persistent_fid
;
3350 req
->VolatileFileId
= volatile_fid
;
3351 /* 4 for rfc1002 length field and 1 for pad */
3352 req
->InputBufferOffset
=
3353 cpu_to_le16(sizeof(struct smb2_query_info_req
) - 1 - 4);
3354 req
->OutputBufferLength
= cpu_to_le32(
3355 outbuf_len
+ sizeof(struct smb2_query_info_rsp
) - 1 - 4);
3357 iov
->iov_base
= (char *)req
;
3358 /* 4 for rfc1002 length field */
3359 iov
->iov_len
= get_rfc1002_length(req
) + 4;
3364 SMB2_QFS_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
3365 u64 persistent_fid
, u64 volatile_fid
, struct kstatfs
*fsdata
)
3367 struct smb2_query_info_rsp
*rsp
= NULL
;
3369 struct kvec rsp_iov
;
3372 struct cifs_ses
*ses
= tcon
->ses
;
3373 struct smb2_fs_full_size_info
*info
= NULL
;
3376 rc
= build_qfs_info_req(&iov
, tcon
, FS_FULL_SIZE_INFORMATION
,
3377 sizeof(struct smb2_fs_full_size_info
),
3378 persistent_fid
, volatile_fid
);
3382 if (encryption_required(tcon
))
3383 flags
|= CIFS_TRANSFORM_REQ
;
3385 rc
= SendReceive2(xid
, ses
, &iov
, 1, &resp_buftype
, flags
, &rsp_iov
);
3386 cifs_small_buf_release(iov
.iov_base
);
3388 cifs_stats_fail_inc(tcon
, SMB2_QUERY_INFO_HE
);
3391 rsp
= (struct smb2_query_info_rsp
*)rsp_iov
.iov_base
;
3393 info
= (struct smb2_fs_full_size_info
*)(4 /* RFC1001 len */ +
3394 le16_to_cpu(rsp
->OutputBufferOffset
) + (char *)&rsp
->hdr
);
3395 rc
= validate_buf(le16_to_cpu(rsp
->OutputBufferOffset
),
3396 le32_to_cpu(rsp
->OutputBufferLength
), &rsp
->hdr
,
3397 sizeof(struct smb2_fs_full_size_info
));
3399 copy_fs_info_to_kstatfs(info
, fsdata
);
3402 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
3407 SMB2_QFS_attr(const unsigned int xid
, struct cifs_tcon
*tcon
,
3408 u64 persistent_fid
, u64 volatile_fid
, int level
)
3410 struct smb2_query_info_rsp
*rsp
= NULL
;
3412 struct kvec rsp_iov
;
3414 int resp_buftype
, max_len
, min_len
;
3415 struct cifs_ses
*ses
= tcon
->ses
;
3416 unsigned int rsp_len
, offset
;
3419 if (level
== FS_DEVICE_INFORMATION
) {
3420 max_len
= sizeof(FILE_SYSTEM_DEVICE_INFO
);
3421 min_len
= sizeof(FILE_SYSTEM_DEVICE_INFO
);
3422 } else if (level
== FS_ATTRIBUTE_INFORMATION
) {
3423 max_len
= sizeof(FILE_SYSTEM_ATTRIBUTE_INFO
);
3424 min_len
= MIN_FS_ATTR_INFO_SIZE
;
3425 } else if (level
== FS_SECTOR_SIZE_INFORMATION
) {
3426 max_len
= sizeof(struct smb3_fs_ss_info
);
3427 min_len
= sizeof(struct smb3_fs_ss_info
);
3429 cifs_dbg(FYI
, "Invalid qfsinfo level %d\n", level
);
3433 rc
= build_qfs_info_req(&iov
, tcon
, level
, max_len
,
3434 persistent_fid
, volatile_fid
);
3438 if (encryption_required(tcon
))
3439 flags
|= CIFS_TRANSFORM_REQ
;
3441 rc
= SendReceive2(xid
, ses
, &iov
, 1, &resp_buftype
, flags
, &rsp_iov
);
3442 cifs_small_buf_release(iov
.iov_base
);
3444 cifs_stats_fail_inc(tcon
, SMB2_QUERY_INFO_HE
);
3447 rsp
= (struct smb2_query_info_rsp
*)rsp_iov
.iov_base
;
3449 rsp_len
= le32_to_cpu(rsp
->OutputBufferLength
);
3450 offset
= le16_to_cpu(rsp
->OutputBufferOffset
);
3451 rc
= validate_buf(offset
, rsp_len
, &rsp
->hdr
, min_len
);
3455 if (level
== FS_ATTRIBUTE_INFORMATION
)
3456 memcpy(&tcon
->fsAttrInfo
, 4 /* RFC1001 len */ + offset
3457 + (char *)&rsp
->hdr
, min_t(unsigned int,
3459 else if (level
== FS_DEVICE_INFORMATION
)
3460 memcpy(&tcon
->fsDevInfo
, 4 /* RFC1001 len */ + offset
3461 + (char *)&rsp
->hdr
, sizeof(FILE_SYSTEM_DEVICE_INFO
));
3462 else if (level
== FS_SECTOR_SIZE_INFORMATION
) {
3463 struct smb3_fs_ss_info
*ss_info
= (struct smb3_fs_ss_info
*)
3464 (4 /* RFC1001 len */ + offset
+ (char *)&rsp
->hdr
);
3465 tcon
->ss_flags
= le32_to_cpu(ss_info
->Flags
);
3466 tcon
->perf_sector_size
=
3467 le32_to_cpu(ss_info
->PhysicalBytesPerSectorForPerf
);
3471 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
3476 smb2_lockv(const unsigned int xid
, struct cifs_tcon
*tcon
,
3477 const __u64 persist_fid
, const __u64 volatile_fid
, const __u32 pid
,
3478 const __u32 num_lock
, struct smb2_lock_element
*buf
)
3481 struct smb2_lock_req
*req
= NULL
;
3483 struct kvec rsp_iov
;
3486 int flags
= CIFS_NO_RESP
;
3488 cifs_dbg(FYI
, "smb2_lockv num lock %d\n", num_lock
);
3490 rc
= small_smb2_init(SMB2_LOCK
, tcon
, (void **) &req
);
3494 if (encryption_required(tcon
))
3495 flags
|= CIFS_TRANSFORM_REQ
;
3497 req
->hdr
.sync_hdr
.ProcessId
= cpu_to_le32(pid
);
3498 req
->LockCount
= cpu_to_le16(num_lock
);
3500 req
->PersistentFileId
= persist_fid
;
3501 req
->VolatileFileId
= volatile_fid
;
3503 count
= num_lock
* sizeof(struct smb2_lock_element
);
3504 inc_rfc1001_len(req
, count
- sizeof(struct smb2_lock_element
));
3506 iov
[0].iov_base
= (char *)req
;
3507 /* 4 for rfc1002 length field and count for all locks */
3508 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - count
;
3509 iov
[1].iov_base
= (char *)buf
;
3510 iov
[1].iov_len
= count
;
3512 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_locks
);
3513 rc
= SendReceive2(xid
, tcon
->ses
, iov
, 2, &resp_buf_type
, flags
,
3515 cifs_small_buf_release(req
);
3517 cifs_dbg(FYI
, "Send error in smb2_lockv = %d\n", rc
);
3518 cifs_stats_fail_inc(tcon
, SMB2_LOCK_HE
);
3525 SMB2_lock(const unsigned int xid
, struct cifs_tcon
*tcon
,
3526 const __u64 persist_fid
, const __u64 volatile_fid
, const __u32 pid
,
3527 const __u64 length
, const __u64 offset
, const __u32 lock_flags
,
3530 struct smb2_lock_element lock
;
3532 lock
.Offset
= cpu_to_le64(offset
);
3533 lock
.Length
= cpu_to_le64(length
);
3534 lock
.Flags
= cpu_to_le32(lock_flags
);
3535 if (!wait
&& lock_flags
!= SMB2_LOCKFLAG_UNLOCK
)
3536 lock
.Flags
|= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY
);
3538 return smb2_lockv(xid
, tcon
, persist_fid
, volatile_fid
, pid
, 1, &lock
);
3542 SMB2_lease_break(const unsigned int xid
, struct cifs_tcon
*tcon
,
3543 __u8
*lease_key
, const __le32 lease_state
)
3546 struct smb2_lease_ack
*req
= NULL
;
3547 int flags
= CIFS_OBREAK_OP
;
3549 cifs_dbg(FYI
, "SMB2_lease_break\n");
3550 rc
= small_smb2_init(SMB2_OPLOCK_BREAK
, tcon
, (void **) &req
);
3554 if (encryption_required(tcon
))
3555 flags
|= CIFS_TRANSFORM_REQ
;
3557 req
->hdr
.sync_hdr
.CreditRequest
= cpu_to_le16(1);
3558 req
->StructureSize
= cpu_to_le16(36);
3559 inc_rfc1001_len(req
, 12);
3561 memcpy(req
->LeaseKey
, lease_key
, 16);
3562 req
->LeaseState
= lease_state
;
3564 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) req
, flags
);
3565 cifs_small_buf_release(req
);
3568 cifs_stats_fail_inc(tcon
, SMB2_OPLOCK_BREAK_HE
);
3569 cifs_dbg(FYI
, "Send error in Lease Break = %d\n", rc
);