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
)
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 (le16_to_cpu(rsp
->DialectRevision
) !=
574 ses
->server
->vals
->protocol_id
) {
575 /* if requested single dialect ensure returned dialect matched */
576 cifs_dbg(VFS
, "Illegal 0x%x dialect returned: not requested\n",
577 le16_to_cpu(rsp
->DialectRevision
));
581 cifs_dbg(FYI
, "mode 0x%x\n", rsp
->SecurityMode
);
583 if (rsp
->DialectRevision
== cpu_to_le16(SMB20_PROT_ID
))
584 cifs_dbg(FYI
, "negotiated smb2.0 dialect\n");
585 else if (rsp
->DialectRevision
== cpu_to_le16(SMB21_PROT_ID
))
586 cifs_dbg(FYI
, "negotiated smb2.1 dialect\n");
587 else if (rsp
->DialectRevision
== cpu_to_le16(SMB30_PROT_ID
))
588 cifs_dbg(FYI
, "negotiated smb3.0 dialect\n");
589 else if (rsp
->DialectRevision
== cpu_to_le16(SMB302_PROT_ID
))
590 cifs_dbg(FYI
, "negotiated smb3.02 dialect\n");
591 #ifdef CONFIG_CIFS_SMB311
592 else if (rsp
->DialectRevision
== cpu_to_le16(SMB311_PROT_ID
))
593 cifs_dbg(FYI
, "negotiated smb3.1.1 dialect\n");
596 cifs_dbg(VFS
, "Illegal dialect returned by server 0x%x\n",
597 le16_to_cpu(rsp
->DialectRevision
));
601 server
->dialect
= le16_to_cpu(rsp
->DialectRevision
);
603 /* BB: add check that dialect was valid given dialect(s) we asked for */
605 /* SMB2 only has an extended negflavor */
606 server
->negflavor
= CIFS_NEGFLAVOR_EXTENDED
;
607 /* set it to the maximum buffer size value we can send with 1 credit */
608 server
->maxBuf
= min_t(unsigned int, le32_to_cpu(rsp
->MaxTransactSize
),
609 SMB2_MAX_BUFFER_SIZE
);
610 server
->max_read
= le32_to_cpu(rsp
->MaxReadSize
);
611 server
->max_write
= le32_to_cpu(rsp
->MaxWriteSize
);
612 /* BB Do we need to validate the SecurityMode? */
613 server
->sec_mode
= le16_to_cpu(rsp
->SecurityMode
);
614 server
->capabilities
= le32_to_cpu(rsp
->Capabilities
);
616 server
->capabilities
|= SMB2_NT_FIND
| SMB2_LARGE_FILES
;
618 security_blob
= smb2_get_data_area_len(&blob_offset
, &blob_length
,
621 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
623 * ses->sectype = RawNTLMSSP;
624 * but for time being this is our only auth choice so doesn't matter.
625 * We just found a server which sets blob length to zero expecting raw.
627 if (blob_length
== 0) {
628 cifs_dbg(FYI
, "missing security blob on negprot\n");
629 server
->sec_ntlmssp
= true;
632 rc
= cifs_enable_signing(server
, ses
->sign
);
636 rc
= decode_negTokenInit(security_blob
, blob_length
, server
);
643 free_rsp_buf(resp_buftype
, rsp
);
647 int smb3_validate_negotiate(const unsigned int xid
, struct cifs_tcon
*tcon
)
650 struct validate_negotiate_info_req vneg_inbuf
;
651 struct validate_negotiate_info_rsp
*pneg_rsp
;
653 u32 inbuflen
; /* max of 4 dialects */
655 cifs_dbg(FYI
, "validate negotiate\n");
658 * validation ioctl must be signed, so no point sending this if we
659 * can not sign it (ie are not known user). Even if signing is not
660 * required (enabled but not negotiated), in those cases we selectively
661 * sign just this, the first and only signed request on a connection.
662 * Having validation of negotiate info helps reduce attack vectors.
664 if (tcon
->ses
->session_flags
& SMB2_SESSION_FLAG_IS_GUEST
)
665 return 0; /* validation requires signing */
667 if (tcon
->ses
->user_name
== NULL
) {
668 cifs_dbg(FYI
, "Can't validate negotiate: null user mount\n");
669 return 0; /* validation requires signing */
672 if (tcon
->ses
->session_flags
& SMB2_SESSION_FLAG_IS_NULL
)
673 cifs_dbg(VFS
, "Unexpected null user (anonymous) auth flag sent by server\n");
675 vneg_inbuf
.Capabilities
=
676 cpu_to_le32(tcon
->ses
->server
->vals
->req_capabilities
);
677 memcpy(vneg_inbuf
.Guid
, tcon
->ses
->server
->client_guid
,
678 SMB2_CLIENT_GUID_SIZE
);
681 vneg_inbuf
.SecurityMode
=
682 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED
);
683 else if (global_secflags
& CIFSSEC_MAY_SIGN
)
684 vneg_inbuf
.SecurityMode
=
685 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED
);
687 vneg_inbuf
.SecurityMode
= 0;
690 if (strcmp(tcon
->ses
->server
->vals
->version_string
,
691 SMB3ANY_VERSION_STRING
) == 0) {
692 vneg_inbuf
.Dialects
[0] = cpu_to_le16(SMB30_PROT_ID
);
693 vneg_inbuf
.Dialects
[1] = cpu_to_le16(SMB302_PROT_ID
);
694 vneg_inbuf
.DialectCount
= cpu_to_le16(2);
695 /* structure is big enough for 3 dialects, sending only 2 */
696 inbuflen
= sizeof(struct validate_negotiate_info_req
) - 2;
697 } else if (strcmp(tcon
->ses
->server
->vals
->version_string
,
698 SMBDEFAULT_VERSION_STRING
) == 0) {
699 vneg_inbuf
.Dialects
[0] = cpu_to_le16(SMB21_PROT_ID
);
700 vneg_inbuf
.Dialects
[1] = cpu_to_le16(SMB30_PROT_ID
);
701 vneg_inbuf
.Dialects
[2] = cpu_to_le16(SMB302_PROT_ID
);
702 vneg_inbuf
.DialectCount
= cpu_to_le16(3);
703 /* structure is big enough for 3 dialects */
704 inbuflen
= sizeof(struct validate_negotiate_info_req
);
706 /* otherwise specific dialect was requested */
707 vneg_inbuf
.Dialects
[0] =
708 cpu_to_le16(tcon
->ses
->server
->vals
->protocol_id
);
709 vneg_inbuf
.DialectCount
= cpu_to_le16(1);
710 /* structure is big enough for 3 dialects, sending only 1 */
711 inbuflen
= sizeof(struct validate_negotiate_info_req
) - 4;
714 rc
= SMB2_ioctl(xid
, tcon
, NO_FILE_ID
, NO_FILE_ID
,
715 FSCTL_VALIDATE_NEGOTIATE_INFO
, true /* is_fsctl */,
717 (char *)&vneg_inbuf
, sizeof(struct validate_negotiate_info_req
),
718 (char **)&pneg_rsp
, &rsplen
);
721 cifs_dbg(VFS
, "validate protocol negotiate failed: %d\n", rc
);
725 if (rsplen
!= sizeof(struct validate_negotiate_info_rsp
)) {
726 cifs_dbg(VFS
, "invalid protocol negotiate response size: %d\n",
729 /* relax check since Mac returns max bufsize allowed on ioctl */
730 if (rsplen
> CIFSMaxBufSize
)
734 /* check validate negotiate info response matches what we got earlier */
735 if (pneg_rsp
->Dialect
!=
736 cpu_to_le16(tcon
->ses
->server
->vals
->protocol_id
))
739 if (pneg_rsp
->SecurityMode
!= cpu_to_le16(tcon
->ses
->server
->sec_mode
))
742 /* do not validate server guid because not saved at negprot time yet */
744 if ((le32_to_cpu(pneg_rsp
->Capabilities
) | SMB2_NT_FIND
|
745 SMB2_LARGE_FILES
) != tcon
->ses
->server
->capabilities
)
748 /* validate negotiate successful */
749 cifs_dbg(FYI
, "validate negotiate info successful\n");
753 cifs_dbg(VFS
, "protocol revalidation - security settings mismatch\n");
758 smb2_select_sectype(struct TCP_Server_Info
*server
, enum securityEnum requested
)
767 if (server
->sec_ntlmssp
&&
768 (global_secflags
& CIFSSEC_MAY_NTLMSSP
))
770 if ((server
->sec_kerberos
|| server
->sec_mskerberos
) &&
771 (global_secflags
& CIFSSEC_MAY_KRB5
))
779 struct SMB2_sess_data
{
781 struct cifs_ses
*ses
;
782 struct nls_table
*nls_cp
;
783 void (*func
)(struct SMB2_sess_data
*);
785 u64 previous_session
;
787 /* we will send the SMB in three pieces:
788 * a fixed length beginning part, an optional
789 * SPNEGO blob (which can be zero length), and a
790 * last part which will include the strings
791 * and rest of bcc area. This allows us to avoid
792 * a large buffer 17K allocation
799 SMB2_sess_alloc_buffer(struct SMB2_sess_data
*sess_data
)
802 struct cifs_ses
*ses
= sess_data
->ses
;
803 struct smb2_sess_setup_req
*req
;
804 struct TCP_Server_Info
*server
= ses
->server
;
806 rc
= small_smb2_init(SMB2_SESSION_SETUP
, NULL
, (void **) &req
);
810 /* First session, not a reauthenticate */
811 req
->hdr
.sync_hdr
.SessionId
= 0;
813 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
814 req
->PreviousSessionId
= sess_data
->previous_session
;
816 req
->Flags
= 0; /* MBZ */
817 /* to enable echos and oplocks */
818 req
->hdr
.sync_hdr
.CreditRequest
= cpu_to_le16(3);
820 /* only one of SMB2 signing flags may be set in SMB2 request */
822 req
->SecurityMode
= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
823 else if (global_secflags
& CIFSSEC_MAY_SIGN
) /* one flag unlike MUST_ */
824 req
->SecurityMode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
826 req
->SecurityMode
= 0;
828 req
->Capabilities
= 0;
829 req
->Channel
= 0; /* MBZ */
831 sess_data
->iov
[0].iov_base
= (char *)req
;
832 /* 4 for rfc1002 length field and 1 for pad */
833 sess_data
->iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
835 * This variable will be used to clear the buffer
836 * allocated above in case of any error in the calling function.
838 sess_data
->buf0_type
= CIFS_SMALL_BUFFER
;
844 SMB2_sess_free_buffer(struct SMB2_sess_data
*sess_data
)
846 free_rsp_buf(sess_data
->buf0_type
, sess_data
->iov
[0].iov_base
);
847 sess_data
->buf0_type
= CIFS_NO_BUFFER
;
851 SMB2_sess_sendreceive(struct SMB2_sess_data
*sess_data
)
854 struct smb2_sess_setup_req
*req
= sess_data
->iov
[0].iov_base
;
855 struct kvec rsp_iov
= { NULL
, 0 };
857 /* Testing shows that buffer offset must be at location of Buffer[0] */
858 req
->SecurityBufferOffset
=
859 cpu_to_le16(sizeof(struct smb2_sess_setup_req
) -
860 1 /* pad */ - 4 /* rfc1001 len */);
861 req
->SecurityBufferLength
= cpu_to_le16(sess_data
->iov
[1].iov_len
);
863 inc_rfc1001_len(req
, sess_data
->iov
[1].iov_len
- 1 /* pad */);
865 /* BB add code to build os and lm fields */
867 rc
= SendReceive2(sess_data
->xid
, sess_data
->ses
,
869 &sess_data
->buf0_type
,
870 CIFS_LOG_ERROR
| CIFS_NEG_OP
, &rsp_iov
);
871 cifs_small_buf_release(sess_data
->iov
[0].iov_base
);
872 memcpy(&sess_data
->iov
[0], &rsp_iov
, sizeof(struct kvec
));
878 SMB2_sess_establish_session(struct SMB2_sess_data
*sess_data
)
881 struct cifs_ses
*ses
= sess_data
->ses
;
883 mutex_lock(&ses
->server
->srv_mutex
);
884 if (ses
->server
->ops
->generate_signingkey
) {
885 rc
= ses
->server
->ops
->generate_signingkey(ses
);
888 "SMB3 session key generation failed\n");
889 mutex_unlock(&ses
->server
->srv_mutex
);
893 if (!ses
->server
->session_estab
) {
894 ses
->server
->sequence_number
= 0x2;
895 ses
->server
->session_estab
= true;
897 mutex_unlock(&ses
->server
->srv_mutex
);
899 cifs_dbg(FYI
, "SMB2/3 session established successfully\n");
900 spin_lock(&GlobalMid_Lock
);
901 ses
->status
= CifsGood
;
902 ses
->need_reconnect
= false;
903 spin_unlock(&GlobalMid_Lock
);
907 #ifdef CONFIG_CIFS_UPCALL
909 SMB2_auth_kerberos(struct SMB2_sess_data
*sess_data
)
912 struct cifs_ses
*ses
= sess_data
->ses
;
913 struct cifs_spnego_msg
*msg
;
914 struct key
*spnego_key
= NULL
;
915 struct smb2_sess_setup_rsp
*rsp
= NULL
;
917 rc
= SMB2_sess_alloc_buffer(sess_data
);
921 spnego_key
= cifs_get_spnego_key(ses
);
922 if (IS_ERR(spnego_key
)) {
923 rc
= PTR_ERR(spnego_key
);
928 msg
= spnego_key
->payload
.data
[0];
930 * check version field to make sure that cifs.upcall is
931 * sending us a response in an expected form
933 if (msg
->version
!= CIFS_SPNEGO_UPCALL_VERSION
) {
935 "bad cifs.upcall version. Expected %d got %d",
936 CIFS_SPNEGO_UPCALL_VERSION
, msg
->version
);
938 goto out_put_spnego_key
;
941 ses
->auth_key
.response
= kmemdup(msg
->data
, msg
->sesskey_len
,
943 if (!ses
->auth_key
.response
) {
945 "Kerberos can't allocate (%u bytes) memory",
948 goto out_put_spnego_key
;
950 ses
->auth_key
.len
= msg
->sesskey_len
;
952 sess_data
->iov
[1].iov_base
= msg
->data
+ msg
->sesskey_len
;
953 sess_data
->iov
[1].iov_len
= msg
->secblob_len
;
955 rc
= SMB2_sess_sendreceive(sess_data
);
957 goto out_put_spnego_key
;
959 rsp
= (struct smb2_sess_setup_rsp
*)sess_data
->iov
[0].iov_base
;
960 ses
->Suid
= rsp
->hdr
.sync_hdr
.SessionId
;
962 ses
->session_flags
= le16_to_cpu(rsp
->SessionFlags
);
964 rc
= SMB2_sess_establish_session(sess_data
);
966 key_invalidate(spnego_key
);
969 sess_data
->result
= rc
;
970 sess_data
->func
= NULL
;
971 SMB2_sess_free_buffer(sess_data
);
975 SMB2_auth_kerberos(struct SMB2_sess_data
*sess_data
)
977 cifs_dbg(VFS
, "Kerberos negotiated but upcall support disabled!\n");
978 sess_data
->result
= -EOPNOTSUPP
;
979 sess_data
->func
= NULL
;
984 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data
*sess_data
);
987 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data
*sess_data
)
990 struct cifs_ses
*ses
= sess_data
->ses
;
991 struct smb2_sess_setup_rsp
*rsp
= NULL
;
992 char *ntlmssp_blob
= NULL
;
993 bool use_spnego
= false; /* else use raw ntlmssp */
997 * If memory allocation is successful, caller of this function
1000 ses
->ntlmssp
= kmalloc(sizeof(struct ntlmssp_auth
), GFP_KERNEL
);
1001 if (!ses
->ntlmssp
) {
1005 ses
->ntlmssp
->sesskey_per_smbsess
= true;
1007 rc
= SMB2_sess_alloc_buffer(sess_data
);
1011 ntlmssp_blob
= kmalloc(sizeof(struct _NEGOTIATE_MESSAGE
),
1013 if (ntlmssp_blob
== NULL
) {
1018 build_ntlmssp_negotiate_blob(ntlmssp_blob
, ses
);
1020 /* BB eventually need to add this */
1021 cifs_dbg(VFS
, "spnego not supported for SMB2 yet\n");
1025 blob_length
= sizeof(struct _NEGOTIATE_MESSAGE
);
1026 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1028 sess_data
->iov
[1].iov_base
= ntlmssp_blob
;
1029 sess_data
->iov
[1].iov_len
= blob_length
;
1031 rc
= SMB2_sess_sendreceive(sess_data
);
1032 rsp
= (struct smb2_sess_setup_rsp
*)sess_data
->iov
[0].iov_base
;
1034 /* If true, rc here is expected and not an error */
1035 if (sess_data
->buf0_type
!= CIFS_NO_BUFFER
&&
1036 rsp
->hdr
.sync_hdr
.Status
== STATUS_MORE_PROCESSING_REQUIRED
)
1042 if (offsetof(struct smb2_sess_setup_rsp
, Buffer
) - 4 !=
1043 le16_to_cpu(rsp
->SecurityBufferOffset
)) {
1044 cifs_dbg(VFS
, "Invalid security buffer offset %d\n",
1045 le16_to_cpu(rsp
->SecurityBufferOffset
));
1049 rc
= decode_ntlmssp_challenge(rsp
->Buffer
,
1050 le16_to_cpu(rsp
->SecurityBufferLength
), ses
);
1054 cifs_dbg(FYI
, "rawntlmssp session setup challenge phase\n");
1057 ses
->Suid
= rsp
->hdr
.sync_hdr
.SessionId
;
1058 ses
->session_flags
= le16_to_cpu(rsp
->SessionFlags
);
1061 kfree(ntlmssp_blob
);
1062 SMB2_sess_free_buffer(sess_data
);
1064 sess_data
->result
= 0;
1065 sess_data
->func
= SMB2_sess_auth_rawntlmssp_authenticate
;
1069 kfree(ses
->ntlmssp
);
1070 ses
->ntlmssp
= NULL
;
1071 sess_data
->result
= rc
;
1072 sess_data
->func
= NULL
;
1076 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data
*sess_data
)
1079 struct cifs_ses
*ses
= sess_data
->ses
;
1080 struct smb2_sess_setup_req
*req
;
1081 struct smb2_sess_setup_rsp
*rsp
= NULL
;
1082 unsigned char *ntlmssp_blob
= NULL
;
1083 bool use_spnego
= false; /* else use raw ntlmssp */
1084 u16 blob_length
= 0;
1086 rc
= SMB2_sess_alloc_buffer(sess_data
);
1090 req
= (struct smb2_sess_setup_req
*) sess_data
->iov
[0].iov_base
;
1091 req
->hdr
.sync_hdr
.SessionId
= ses
->Suid
;
1093 rc
= build_ntlmssp_auth_blob(&ntlmssp_blob
, &blob_length
, ses
,
1096 cifs_dbg(FYI
, "build_ntlmssp_auth_blob failed %d\n", rc
);
1101 /* BB eventually need to add this */
1102 cifs_dbg(VFS
, "spnego not supported for SMB2 yet\n");
1106 sess_data
->iov
[1].iov_base
= ntlmssp_blob
;
1107 sess_data
->iov
[1].iov_len
= blob_length
;
1109 rc
= SMB2_sess_sendreceive(sess_data
);
1113 rsp
= (struct smb2_sess_setup_rsp
*)sess_data
->iov
[0].iov_base
;
1115 ses
->Suid
= rsp
->hdr
.sync_hdr
.SessionId
;
1116 ses
->session_flags
= le16_to_cpu(rsp
->SessionFlags
);
1118 rc
= SMB2_sess_establish_session(sess_data
);
1120 kfree(ntlmssp_blob
);
1121 SMB2_sess_free_buffer(sess_data
);
1122 kfree(ses
->ntlmssp
);
1123 ses
->ntlmssp
= NULL
;
1124 sess_data
->result
= rc
;
1125 sess_data
->func
= NULL
;
1129 SMB2_select_sec(struct cifs_ses
*ses
, struct SMB2_sess_data
*sess_data
)
1133 type
= smb2_select_sectype(ses
->server
, ses
->sectype
);
1134 cifs_dbg(FYI
, "sess setup type %d\n", type
);
1135 if (type
== Unspecified
) {
1137 "Unable to select appropriate authentication method!");
1143 sess_data
->func
= SMB2_auth_kerberos
;
1146 sess_data
->func
= SMB2_sess_auth_rawntlmssp_negotiate
;
1149 cifs_dbg(VFS
, "secType %d not supported!\n", type
);
1157 SMB2_sess_setup(const unsigned int xid
, struct cifs_ses
*ses
,
1158 const struct nls_table
*nls_cp
)
1161 struct TCP_Server_Info
*server
= ses
->server
;
1162 struct SMB2_sess_data
*sess_data
;
1164 cifs_dbg(FYI
, "Session Setup\n");
1167 WARN(1, "%s: server is NULL!\n", __func__
);
1171 sess_data
= kzalloc(sizeof(struct SMB2_sess_data
), GFP_KERNEL
);
1175 rc
= SMB2_select_sec(ses
, sess_data
);
1178 sess_data
->xid
= xid
;
1179 sess_data
->ses
= ses
;
1180 sess_data
->buf0_type
= CIFS_NO_BUFFER
;
1181 sess_data
->nls_cp
= (struct nls_table
*) nls_cp
;
1183 while (sess_data
->func
)
1184 sess_data
->func(sess_data
);
1186 if ((ses
->session_flags
& SMB2_SESSION_FLAG_IS_GUEST
) && (ses
->sign
))
1187 cifs_dbg(VFS
, "signing requested but authenticated as guest\n");
1188 rc
= sess_data
->result
;
1195 SMB2_logoff(const unsigned int xid
, struct cifs_ses
*ses
)
1197 struct smb2_logoff_req
*req
; /* response is also trivial struct */
1199 struct TCP_Server_Info
*server
;
1202 cifs_dbg(FYI
, "disconnect session %p\n", ses
);
1204 if (ses
&& (ses
->server
))
1205 server
= ses
->server
;
1209 /* no need to send SMB logoff if uid already closed due to reconnect */
1210 if (ses
->need_reconnect
)
1211 goto smb2_session_already_dead
;
1213 rc
= small_smb2_init(SMB2_LOGOFF
, NULL
, (void **) &req
);
1217 /* since no tcon, smb2_init can not do this, so do here */
1218 req
->hdr
.sync_hdr
.SessionId
= ses
->Suid
;
1220 if (ses
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
)
1221 flags
|= CIFS_TRANSFORM_REQ
;
1222 else if (server
->sign
)
1223 req
->hdr
.sync_hdr
.Flags
|= SMB2_FLAGS_SIGNED
;
1225 rc
= SendReceiveNoRsp(xid
, ses
, (char *) req
, flags
);
1226 cifs_small_buf_release(req
);
1228 * No tcon so can't do
1229 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1232 smb2_session_already_dead
:
1236 static inline void cifs_stats_fail_inc(struct cifs_tcon
*tcon
, uint16_t code
)
1238 cifs_stats_inc(&tcon
->stats
.smb2_stats
.smb2_com_failed
[code
]);
1241 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1243 /* These are similar values to what Windows uses */
1244 static inline void init_copy_chunk_defaults(struct cifs_tcon
*tcon
)
1246 tcon
->max_chunks
= 256;
1247 tcon
->max_bytes_chunk
= 1048576;
1248 tcon
->max_bytes_copy
= 16777216;
1252 SMB2_tcon(const unsigned int xid
, struct cifs_ses
*ses
, const char *tree
,
1253 struct cifs_tcon
*tcon
, const struct nls_table
*cp
)
1255 struct smb2_tree_connect_req
*req
;
1256 struct smb2_tree_connect_rsp
*rsp
= NULL
;
1258 struct kvec rsp_iov
;
1262 __le16
*unc_path
= NULL
;
1265 cifs_dbg(FYI
, "TCON\n");
1267 if (!(ses
->server
) || !tree
)
1270 unc_path
= kmalloc(MAX_SHARENAME_LENGTH
* 2, GFP_KERNEL
);
1271 if (unc_path
== NULL
)
1274 unc_path_len
= cifs_strtoUTF16(unc_path
, tree
, strlen(tree
), cp
) + 1;
1276 if (unc_path_len
< 2) {
1281 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1285 rc
= small_smb2_init(SMB2_TREE_CONNECT
, tcon
, (void **) &req
);
1292 if ((ses
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
))
1293 flags
|= CIFS_TRANSFORM_REQ
;
1295 /* since no tcon, smb2_init can not do this, so do here */
1296 req
->hdr
.sync_hdr
.SessionId
= ses
->Suid
;
1297 if (ses
->server
->sign
)
1298 req
->hdr
.sync_hdr
.Flags
|= SMB2_FLAGS_SIGNED
;
1299 } else if (encryption_required(tcon
))
1300 flags
|= CIFS_TRANSFORM_REQ
;
1302 iov
[0].iov_base
= (char *)req
;
1303 /* 4 for rfc1002 length field and 1 for pad */
1304 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
1306 /* Testing shows that buffer offset must be at location of Buffer[0] */
1307 req
->PathOffset
= cpu_to_le16(sizeof(struct smb2_tree_connect_req
)
1308 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1309 req
->PathLength
= cpu_to_le16(unc_path_len
- 2);
1310 iov
[1].iov_base
= unc_path
;
1311 iov
[1].iov_len
= unc_path_len
;
1313 inc_rfc1001_len(req
, unc_path_len
- 1 /* pad */);
1315 rc
= SendReceive2(xid
, ses
, iov
, 2, &resp_buftype
, flags
, &rsp_iov
);
1316 cifs_small_buf_release(req
);
1317 rsp
= (struct smb2_tree_connect_rsp
*)rsp_iov
.iov_base
;
1321 cifs_stats_fail_inc(tcon
, SMB2_TREE_CONNECT_HE
);
1322 tcon
->need_reconnect
= true;
1324 goto tcon_error_exit
;
1328 ses
->ipc_tid
= rsp
->hdr
.sync_hdr
.TreeId
;
1332 switch (rsp
->ShareType
) {
1333 case SMB2_SHARE_TYPE_DISK
:
1334 cifs_dbg(FYI
, "connection to disk share\n");
1336 case SMB2_SHARE_TYPE_PIPE
:
1338 cifs_dbg(FYI
, "connection to pipe share\n");
1340 case SMB2_SHARE_TYPE_PRINT
:
1342 cifs_dbg(FYI
, "connection to printer\n");
1345 cifs_dbg(VFS
, "unknown share type %d\n", rsp
->ShareType
);
1347 goto tcon_error_exit
;
1350 tcon
->share_flags
= le32_to_cpu(rsp
->ShareFlags
);
1351 tcon
->capabilities
= rsp
->Capabilities
; /* we keep caps little endian */
1352 tcon
->maximal_access
= le32_to_cpu(rsp
->MaximalAccess
);
1353 tcon
->tidStatus
= CifsGood
;
1354 tcon
->need_reconnect
= false;
1355 tcon
->tid
= rsp
->hdr
.sync_hdr
.TreeId
;
1356 strlcpy(tcon
->treeName
, tree
, sizeof(tcon
->treeName
));
1358 if ((rsp
->Capabilities
& SMB2_SHARE_CAP_DFS
) &&
1359 ((tcon
->share_flags
& SHI1005_FLAGS_DFS
) == 0))
1360 cifs_dbg(VFS
, "DFS capability contradicts DFS flag\n");
1363 !(tcon
->ses
->server
->capabilities
& SMB2_GLOBAL_CAP_ENCRYPTION
))
1364 cifs_dbg(VFS
, "Encryption is requested but not supported\n");
1366 init_copy_chunk_defaults(tcon
);
1367 if (tcon
->ses
->server
->ops
->validate_negotiate
)
1368 rc
= tcon
->ses
->server
->ops
->validate_negotiate(xid
, tcon
);
1370 free_rsp_buf(resp_buftype
, rsp
);
1375 if (rsp
->hdr
.sync_hdr
.Status
== STATUS_BAD_NETWORK_NAME
) {
1376 cifs_dbg(VFS
, "BAD_NETWORK_NAME: %s\n", tree
);
1382 SMB2_tdis(const unsigned int xid
, struct cifs_tcon
*tcon
)
1384 struct smb2_tree_disconnect_req
*req
; /* response is trivial */
1386 struct cifs_ses
*ses
= tcon
->ses
;
1389 cifs_dbg(FYI
, "Tree Disconnect\n");
1391 if (!ses
|| !(ses
->server
))
1394 if ((tcon
->need_reconnect
) || (tcon
->ses
->need_reconnect
))
1397 rc
= small_smb2_init(SMB2_TREE_DISCONNECT
, tcon
, (void **) &req
);
1401 if (encryption_required(tcon
))
1402 flags
|= CIFS_TRANSFORM_REQ
;
1404 rc
= SendReceiveNoRsp(xid
, ses
, (char *)req
, flags
);
1405 cifs_small_buf_release(req
);
1407 cifs_stats_fail_inc(tcon
, SMB2_TREE_DISCONNECT_HE
);
1413 static struct create_durable
*
1414 create_durable_buf(void)
1416 struct create_durable
*buf
;
1418 buf
= kzalloc(sizeof(struct create_durable
), GFP_KERNEL
);
1422 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
1423 (struct create_durable
, Data
));
1424 buf
->ccontext
.DataLength
= cpu_to_le32(16);
1425 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
1426 (struct create_durable
, Name
));
1427 buf
->ccontext
.NameLength
= cpu_to_le16(4);
1428 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1436 static struct create_durable
*
1437 create_reconnect_durable_buf(struct cifs_fid
*fid
)
1439 struct create_durable
*buf
;
1441 buf
= kzalloc(sizeof(struct create_durable
), GFP_KERNEL
);
1445 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
1446 (struct create_durable
, Data
));
1447 buf
->ccontext
.DataLength
= cpu_to_le32(16);
1448 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
1449 (struct create_durable
, Name
));
1450 buf
->ccontext
.NameLength
= cpu_to_le16(4);
1451 buf
->Data
.Fid
.PersistentFileId
= fid
->persistent_fid
;
1452 buf
->Data
.Fid
.VolatileFileId
= fid
->volatile_fid
;
1453 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1462 parse_lease_state(struct TCP_Server_Info
*server
, struct smb2_create_rsp
*rsp
,
1463 unsigned int *epoch
)
1466 struct create_context
*cc
;
1468 unsigned int remaining
;
1471 data_offset
= (char *)rsp
+ 4 + le32_to_cpu(rsp
->CreateContextsOffset
);
1472 remaining
= le32_to_cpu(rsp
->CreateContextsLength
);
1473 cc
= (struct create_context
*)data_offset
;
1474 while (remaining
>= sizeof(struct create_context
)) {
1475 name
= le16_to_cpu(cc
->NameOffset
) + (char *)cc
;
1476 if (le16_to_cpu(cc
->NameLength
) == 4 &&
1477 strncmp(name
, "RqLs", 4) == 0)
1478 return server
->ops
->parse_lease_buf(cc
, epoch
);
1480 next
= le32_to_cpu(cc
->Next
);
1484 cc
= (struct create_context
*)((char *)cc
+ next
);
1491 add_lease_context(struct TCP_Server_Info
*server
, struct kvec
*iov
,
1492 unsigned int *num_iovec
, __u8
*oplock
)
1494 struct smb2_create_req
*req
= iov
[0].iov_base
;
1495 unsigned int num
= *num_iovec
;
1497 iov
[num
].iov_base
= server
->ops
->create_lease_buf(oplock
+1, *oplock
);
1498 if (iov
[num
].iov_base
== NULL
)
1500 iov
[num
].iov_len
= server
->vals
->create_lease_size
;
1501 req
->RequestedOplockLevel
= SMB2_OPLOCK_LEVEL_LEASE
;
1502 if (!req
->CreateContextsOffset
)
1503 req
->CreateContextsOffset
= cpu_to_le32(
1504 sizeof(struct smb2_create_req
) - 4 +
1505 iov
[num
- 1].iov_len
);
1506 le32_add_cpu(&req
->CreateContextsLength
,
1507 server
->vals
->create_lease_size
);
1508 inc_rfc1001_len(&req
->hdr
, server
->vals
->create_lease_size
);
1509 *num_iovec
= num
+ 1;
1513 static struct create_durable_v2
*
1514 create_durable_v2_buf(struct cifs_fid
*pfid
)
1516 struct create_durable_v2
*buf
;
1518 buf
= kzalloc(sizeof(struct create_durable_v2
), GFP_KERNEL
);
1522 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
1523 (struct create_durable_v2
, dcontext
));
1524 buf
->ccontext
.DataLength
= cpu_to_le32(sizeof(struct durable_context_v2
));
1525 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
1526 (struct create_durable_v2
, Name
));
1527 buf
->ccontext
.NameLength
= cpu_to_le16(4);
1529 buf
->dcontext
.Timeout
= 0; /* Should this be configurable by workload */
1530 buf
->dcontext
.Flags
= cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT
);
1531 generate_random_uuid(buf
->dcontext
.CreateGuid
);
1532 memcpy(pfid
->create_guid
, buf
->dcontext
.CreateGuid
, 16);
1534 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1542 static struct create_durable_handle_reconnect_v2
*
1543 create_reconnect_durable_v2_buf(struct cifs_fid
*fid
)
1545 struct create_durable_handle_reconnect_v2
*buf
;
1547 buf
= kzalloc(sizeof(struct create_durable_handle_reconnect_v2
),
1552 buf
->ccontext
.DataOffset
=
1553 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2
,
1555 buf
->ccontext
.DataLength
=
1556 cpu_to_le32(sizeof(struct durable_reconnect_context_v2
));
1557 buf
->ccontext
.NameOffset
=
1558 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2
,
1560 buf
->ccontext
.NameLength
= cpu_to_le16(4);
1562 buf
->dcontext
.Fid
.PersistentFileId
= fid
->persistent_fid
;
1563 buf
->dcontext
.Fid
.VolatileFileId
= fid
->volatile_fid
;
1564 buf
->dcontext
.Flags
= cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT
);
1565 memcpy(buf
->dcontext
.CreateGuid
, fid
->create_guid
, 16);
1567 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1576 add_durable_v2_context(struct kvec
*iov
, unsigned int *num_iovec
,
1577 struct cifs_open_parms
*oparms
)
1579 struct smb2_create_req
*req
= iov
[0].iov_base
;
1580 unsigned int num
= *num_iovec
;
1582 iov
[num
].iov_base
= create_durable_v2_buf(oparms
->fid
);
1583 if (iov
[num
].iov_base
== NULL
)
1585 iov
[num
].iov_len
= sizeof(struct create_durable_v2
);
1586 if (!req
->CreateContextsOffset
)
1587 req
->CreateContextsOffset
=
1588 cpu_to_le32(sizeof(struct smb2_create_req
) - 4 +
1590 le32_add_cpu(&req
->CreateContextsLength
, sizeof(struct create_durable_v2
));
1591 inc_rfc1001_len(&req
->hdr
, sizeof(struct create_durable_v2
));
1592 *num_iovec
= num
+ 1;
1597 add_durable_reconnect_v2_context(struct kvec
*iov
, unsigned int *num_iovec
,
1598 struct cifs_open_parms
*oparms
)
1600 struct smb2_create_req
*req
= iov
[0].iov_base
;
1601 unsigned int num
= *num_iovec
;
1603 /* indicate that we don't need to relock the file */
1604 oparms
->reconnect
= false;
1606 iov
[num
].iov_base
= create_reconnect_durable_v2_buf(oparms
->fid
);
1607 if (iov
[num
].iov_base
== NULL
)
1609 iov
[num
].iov_len
= sizeof(struct create_durable_handle_reconnect_v2
);
1610 if (!req
->CreateContextsOffset
)
1611 req
->CreateContextsOffset
=
1612 cpu_to_le32(sizeof(struct smb2_create_req
) - 4 +
1614 le32_add_cpu(&req
->CreateContextsLength
,
1615 sizeof(struct create_durable_handle_reconnect_v2
));
1616 inc_rfc1001_len(&req
->hdr
,
1617 sizeof(struct create_durable_handle_reconnect_v2
));
1618 *num_iovec
= num
+ 1;
1623 add_durable_context(struct kvec
*iov
, unsigned int *num_iovec
,
1624 struct cifs_open_parms
*oparms
, bool use_persistent
)
1626 struct smb2_create_req
*req
= iov
[0].iov_base
;
1627 unsigned int num
= *num_iovec
;
1629 if (use_persistent
) {
1630 if (oparms
->reconnect
)
1631 return add_durable_reconnect_v2_context(iov
, num_iovec
,
1634 return add_durable_v2_context(iov
, num_iovec
, oparms
);
1637 if (oparms
->reconnect
) {
1638 iov
[num
].iov_base
= create_reconnect_durable_buf(oparms
->fid
);
1639 /* indicate that we don't need to relock the file */
1640 oparms
->reconnect
= false;
1642 iov
[num
].iov_base
= create_durable_buf();
1643 if (iov
[num
].iov_base
== NULL
)
1645 iov
[num
].iov_len
= sizeof(struct create_durable
);
1646 if (!req
->CreateContextsOffset
)
1647 req
->CreateContextsOffset
=
1648 cpu_to_le32(sizeof(struct smb2_create_req
) - 4 +
1650 le32_add_cpu(&req
->CreateContextsLength
, sizeof(struct create_durable
));
1651 inc_rfc1001_len(&req
->hdr
, sizeof(struct create_durable
));
1652 *num_iovec
= num
+ 1;
1657 alloc_path_with_tree_prefix(__le16
**out_path
, int *out_size
, int *out_len
,
1658 const char *treename
, const __le16
*path
)
1660 int treename_len
, path_len
;
1661 struct nls_table
*cp
;
1662 const __le16 sep
[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1667 treename_len
= strlen(treename
);
1668 if (treename_len
< 2 || !(treename
[0] == '\\' && treename
[1] == '\\'))
1674 path_len
= UniStrnlen((wchar_t *)path
, PATH_MAX
);
1677 * make room for one path separator between the treename and
1680 *out_len
= treename_len
+ 1 + path_len
;
1683 * final path needs to be null-terminated UTF16 with a
1687 *out_size
= roundup((*out_len
+1)*2, 8);
1688 *out_path
= kzalloc(*out_size
, GFP_KERNEL
);
1692 cp
= load_nls_default();
1693 cifs_strtoUTF16(*out_path
, treename
, treename_len
, cp
);
1694 UniStrcat(*out_path
, sep
);
1695 UniStrcat(*out_path
, path
);
1702 SMB2_open(const unsigned int xid
, struct cifs_open_parms
*oparms
, __le16
*path
,
1703 __u8
*oplock
, struct smb2_file_all_info
*buf
,
1704 struct smb2_err_rsp
**err_buf
)
1706 struct smb2_create_req
*req
;
1707 struct smb2_create_rsp
*rsp
;
1708 struct TCP_Server_Info
*server
;
1709 struct cifs_tcon
*tcon
= oparms
->tcon
;
1710 struct cifs_ses
*ses
= tcon
->ses
;
1712 struct kvec rsp_iov
= {NULL
, 0};
1715 __le16
*copy_path
= NULL
;
1718 unsigned int n_iov
= 2;
1719 __u32 file_attributes
= 0;
1720 char *dhc_buf
= NULL
, *lc_buf
= NULL
;
1723 cifs_dbg(FYI
, "create/open\n");
1725 if (ses
&& (ses
->server
))
1726 server
= ses
->server
;
1730 rc
= small_smb2_init(SMB2_CREATE
, tcon
, (void **) &req
);
1734 if (encryption_required(tcon
))
1735 flags
|= CIFS_TRANSFORM_REQ
;
1737 if (oparms
->create_options
& CREATE_OPTION_READONLY
)
1738 file_attributes
|= ATTR_READONLY
;
1739 if (oparms
->create_options
& CREATE_OPTION_SPECIAL
)
1740 file_attributes
|= ATTR_SYSTEM
;
1742 req
->ImpersonationLevel
= IL_IMPERSONATION
;
1743 req
->DesiredAccess
= cpu_to_le32(oparms
->desired_access
);
1744 /* File attributes ignored on open (used in create though) */
1745 req
->FileAttributes
= cpu_to_le32(file_attributes
);
1746 req
->ShareAccess
= FILE_SHARE_ALL_LE
;
1747 req
->CreateDisposition
= cpu_to_le32(oparms
->disposition
);
1748 req
->CreateOptions
= cpu_to_le32(oparms
->create_options
& CREATE_OPTIONS_MASK
);
1750 iov
[0].iov_base
= (char *)req
;
1751 /* 4 for rfc1002 length field */
1752 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
1753 /* -1 since last byte is buf[0] which is sent below (path) */
1756 req
->NameOffset
= cpu_to_le16(sizeof(struct smb2_create_req
) - 4);
1758 /* [MS-SMB2] 2.2.13 NameOffset:
1759 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1760 * the SMB2 header, the file name includes a prefix that will
1761 * be processed during DFS name normalization as specified in
1762 * section 3.3.5.9. Otherwise, the file name is relative to
1763 * the share that is identified by the TreeId in the SMB2
1766 if (tcon
->share_flags
& SHI1005_FLAGS_DFS
) {
1769 req
->hdr
.sync_hdr
.Flags
|= SMB2_FLAGS_DFS_OPERATIONS
;
1770 rc
= alloc_path_with_tree_prefix(©_path
, ©_size
,
1772 tcon
->treeName
, path
);
1775 req
->NameLength
= cpu_to_le16(name_len
* 2);
1776 uni_path_len
= copy_size
;
1779 uni_path_len
= (2 * UniStrnlen((wchar_t *)path
, PATH_MAX
)) + 2;
1780 /* MUST set path len (NameLength) to 0 opening root of share */
1781 req
->NameLength
= cpu_to_le16(uni_path_len
- 2);
1782 if (uni_path_len
% 8 != 0) {
1783 copy_size
= roundup(uni_path_len
, 8);
1784 copy_path
= kzalloc(copy_size
, GFP_KERNEL
);
1787 memcpy((char *)copy_path
, (const char *)path
,
1789 uni_path_len
= copy_size
;
1794 iov
[1].iov_len
= uni_path_len
;
1795 iov
[1].iov_base
= path
;
1796 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1797 inc_rfc1001_len(req
, uni_path_len
- 1);
1799 if (!server
->oplocks
)
1800 *oplock
= SMB2_OPLOCK_LEVEL_NONE
;
1802 if (!(server
->capabilities
& SMB2_GLOBAL_CAP_LEASING
) ||
1803 *oplock
== SMB2_OPLOCK_LEVEL_NONE
)
1804 req
->RequestedOplockLevel
= *oplock
;
1806 rc
= add_lease_context(server
, iov
, &n_iov
, oplock
);
1808 cifs_small_buf_release(req
);
1812 lc_buf
= iov
[n_iov
-1].iov_base
;
1815 if (*oplock
== SMB2_OPLOCK_LEVEL_BATCH
) {
1816 /* need to set Next field of lease context if we request it */
1817 if (server
->capabilities
& SMB2_GLOBAL_CAP_LEASING
) {
1818 struct create_context
*ccontext
=
1819 (struct create_context
*)iov
[n_iov
-1].iov_base
;
1821 cpu_to_le32(server
->vals
->create_lease_size
);
1824 rc
= add_durable_context(iov
, &n_iov
, oparms
,
1825 tcon
->use_persistent
);
1827 cifs_small_buf_release(req
);
1832 dhc_buf
= iov
[n_iov
-1].iov_base
;
1835 rc
= SendReceive2(xid
, ses
, iov
, n_iov
, &resp_buftype
, flags
, &rsp_iov
);
1836 cifs_small_buf_release(req
);
1837 rsp
= (struct smb2_create_rsp
*)rsp_iov
.iov_base
;
1840 cifs_stats_fail_inc(tcon
, SMB2_CREATE_HE
);
1842 *err_buf
= kmemdup(rsp
, get_rfc1002_length(rsp
) + 4,
1847 oparms
->fid
->persistent_fid
= rsp
->PersistentFileId
;
1848 oparms
->fid
->volatile_fid
= rsp
->VolatileFileId
;
1851 memcpy(buf
, &rsp
->CreationTime
, 32);
1852 buf
->AllocationSize
= rsp
->AllocationSize
;
1853 buf
->EndOfFile
= rsp
->EndofFile
;
1854 buf
->Attributes
= rsp
->FileAttributes
;
1855 buf
->NumberOfLinks
= cpu_to_le32(1);
1856 buf
->DeletePending
= 0;
1859 if (rsp
->OplockLevel
== SMB2_OPLOCK_LEVEL_LEASE
)
1860 *oplock
= parse_lease_state(server
, rsp
, &oparms
->fid
->epoch
);
1862 *oplock
= rsp
->OplockLevel
;
1867 free_rsp_buf(resp_buftype
, rsp
);
1872 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1875 SMB2_ioctl(const unsigned int xid
, struct cifs_tcon
*tcon
, u64 persistent_fid
,
1876 u64 volatile_fid
, u32 opcode
, bool is_fsctl
, bool use_ipc
,
1877 char *in_data
, u32 indatalen
,
1878 char **out_data
, u32
*plen
/* returned data len */)
1880 struct smb2_ioctl_req
*req
;
1881 struct smb2_ioctl_rsp
*rsp
;
1882 struct smb2_sync_hdr
*shdr
;
1883 struct cifs_ses
*ses
;
1885 struct kvec rsp_iov
;
1891 cifs_dbg(FYI
, "SMB2 IOCTL\n");
1893 if (out_data
!= NULL
)
1896 /* zero out returned data len, in case of error */
1905 if (!ses
|| !(ses
->server
))
1908 rc
= small_smb2_init(SMB2_IOCTL
, tcon
, (void **) &req
);
1913 if (ses
->ipc_tid
== 0) {
1914 cifs_small_buf_release(req
);
1918 cifs_dbg(FYI
, "replacing tid 0x%x with IPC tid 0x%x\n",
1919 req
->hdr
.sync_hdr
.TreeId
, ses
->ipc_tid
);
1920 req
->hdr
.sync_hdr
.TreeId
= ses
->ipc_tid
;
1922 if (encryption_required(tcon
))
1923 flags
|= CIFS_TRANSFORM_REQ
;
1925 req
->CtlCode
= cpu_to_le32(opcode
);
1926 req
->PersistentFileId
= persistent_fid
;
1927 req
->VolatileFileId
= volatile_fid
;
1930 req
->InputCount
= cpu_to_le32(indatalen
);
1931 /* do not set InputOffset if no input data */
1933 cpu_to_le32(offsetof(struct smb2_ioctl_req
, Buffer
) - 4);
1934 iov
[1].iov_base
= in_data
;
1935 iov
[1].iov_len
= indatalen
;
1940 req
->OutputOffset
= 0;
1941 req
->OutputCount
= 0; /* MBZ */
1944 * Could increase MaxOutputResponse, but that would require more
1945 * than one credit. Windows typically sets this smaller, but for some
1946 * ioctls it may be useful to allow server to send more. No point
1947 * limiting what the server can send as long as fits in one credit
1948 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1949 * (by default, note that it can be overridden to make max larger)
1950 * in responses (except for read responses which can be bigger.
1951 * We may want to bump this limit up
1953 req
->MaxOutputResponse
= cpu_to_le32(CIFSMaxBufSize
);
1956 req
->Flags
= cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL
);
1960 iov
[0].iov_base
= (char *)req
;
1963 * If no input data, the size of ioctl struct in
1964 * protocol spec still includes a 1 byte data buffer,
1965 * but if input data passed to ioctl, we do not
1966 * want to double count this, so we do not send
1967 * the dummy one byte of data in iovec[0] if sending
1968 * input data (in iovec[1]). We also must add 4 bytes
1969 * in first iovec to allow for rfc1002 length field.
1973 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
1974 inc_rfc1001_len(req
, indatalen
- 1);
1976 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
1979 rc
= SendReceive2(xid
, ses
, iov
, n_iov
, &resp_buftype
, flags
, &rsp_iov
);
1980 cifs_small_buf_release(req
);
1981 rsp
= (struct smb2_ioctl_rsp
*)rsp_iov
.iov_base
;
1983 if ((rc
!= 0) && (rc
!= -EINVAL
)) {
1984 cifs_stats_fail_inc(tcon
, SMB2_IOCTL_HE
);
1986 } else if (rc
== -EINVAL
) {
1987 if ((opcode
!= FSCTL_SRV_COPYCHUNK_WRITE
) &&
1988 (opcode
!= FSCTL_SRV_COPYCHUNK
)) {
1989 cifs_stats_fail_inc(tcon
, SMB2_IOCTL_HE
);
1994 /* check if caller wants to look at return data or just return rc */
1995 if ((plen
== NULL
) || (out_data
== NULL
))
1998 *plen
= le32_to_cpu(rsp
->OutputCount
);
2000 /* We check for obvious errors in the output buffer length and offset */
2002 goto ioctl_exit
; /* server returned no data */
2003 else if (*plen
> 0xFF00) {
2004 cifs_dbg(VFS
, "srv returned invalid ioctl length: %d\n", *plen
);
2010 if (get_rfc1002_length(rsp
) < le32_to_cpu(rsp
->OutputOffset
) + *plen
) {
2011 cifs_dbg(VFS
, "Malformed ioctl resp: len %d offset %d\n", *plen
,
2012 le32_to_cpu(rsp
->OutputOffset
));
2018 *out_data
= kmalloc(*plen
, GFP_KERNEL
);
2019 if (*out_data
== NULL
) {
2024 shdr
= get_sync_hdr(rsp
);
2025 memcpy(*out_data
, (char *)shdr
+ le32_to_cpu(rsp
->OutputOffset
), *plen
);
2027 free_rsp_buf(resp_buftype
, rsp
);
2032 * Individual callers to ioctl worker function follow
2036 SMB2_set_compression(const unsigned int xid
, struct cifs_tcon
*tcon
,
2037 u64 persistent_fid
, u64 volatile_fid
)
2040 struct compress_ioctl fsctl_input
;
2041 char *ret_data
= NULL
;
2043 fsctl_input
.CompressionState
=
2044 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT
);
2046 rc
= SMB2_ioctl(xid
, tcon
, persistent_fid
, volatile_fid
,
2047 FSCTL_SET_COMPRESSION
, true /* is_fsctl */,
2048 false /* use_ipc */,
2049 (char *)&fsctl_input
/* data input */,
2050 2 /* in data len */, &ret_data
/* out data */, NULL
);
2052 cifs_dbg(FYI
, "set compression rc %d\n", rc
);
2058 SMB2_close(const unsigned int xid
, struct cifs_tcon
*tcon
,
2059 u64 persistent_fid
, u64 volatile_fid
)
2061 struct smb2_close_req
*req
;
2062 struct smb2_close_rsp
*rsp
;
2063 struct cifs_ses
*ses
= tcon
->ses
;
2065 struct kvec rsp_iov
;
2070 cifs_dbg(FYI
, "Close\n");
2072 if (!ses
|| !(ses
->server
))
2075 rc
= small_smb2_init(SMB2_CLOSE
, tcon
, (void **) &req
);
2079 if (encryption_required(tcon
))
2080 flags
|= CIFS_TRANSFORM_REQ
;
2082 req
->PersistentFileId
= persistent_fid
;
2083 req
->VolatileFileId
= volatile_fid
;
2085 iov
[0].iov_base
= (char *)req
;
2086 /* 4 for rfc1002 length field */
2087 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
2089 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buftype
, flags
, &rsp_iov
);
2090 cifs_small_buf_release(req
);
2091 rsp
= (struct smb2_close_rsp
*)rsp_iov
.iov_base
;
2094 cifs_stats_fail_inc(tcon
, SMB2_CLOSE_HE
);
2098 /* BB FIXME - decode close response, update inode for caching */
2101 free_rsp_buf(resp_buftype
, rsp
);
2106 validate_buf(unsigned int offset
, unsigned int buffer_length
,
2107 struct smb2_hdr
*hdr
, unsigned int min_buf_size
)
2110 unsigned int smb_len
= be32_to_cpu(hdr
->smb2_buf_length
);
2111 char *end_of_smb
= smb_len
+ 4 /* RFC1001 length field */ + (char *)hdr
;
2112 char *begin_of_buf
= 4 /* RFC1001 len field */ + offset
+ (char *)hdr
;
2113 char *end_of_buf
= begin_of_buf
+ buffer_length
;
2116 if (buffer_length
< min_buf_size
) {
2117 cifs_dbg(VFS
, "buffer length %d smaller than minimum size %d\n",
2118 buffer_length
, min_buf_size
);
2122 /* check if beyond RFC1001 maximum length */
2123 if ((smb_len
> 0x7FFFFF) || (buffer_length
> 0x7FFFFF)) {
2124 cifs_dbg(VFS
, "buffer length %d or smb length %d too large\n",
2125 buffer_length
, smb_len
);
2129 if ((begin_of_buf
> end_of_smb
) || (end_of_buf
> end_of_smb
)) {
2130 cifs_dbg(VFS
, "illegal server response, bad offset to data\n");
2138 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2139 * Caller must free buffer.
2142 validate_and_copy_buf(unsigned int offset
, unsigned int buffer_length
,
2143 struct smb2_hdr
*hdr
, unsigned int minbufsize
,
2147 char *begin_of_buf
= 4 /* RFC1001 len field */ + offset
+ (char *)hdr
;
2153 rc
= validate_buf(offset
, buffer_length
, hdr
, minbufsize
);
2157 memcpy(data
, begin_of_buf
, buffer_length
);
2163 query_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
2164 u64 persistent_fid
, u64 volatile_fid
, u8 info_class
, u8 info_type
,
2165 u32 additional_info
, size_t output_len
, size_t min_len
, void **data
,
2168 struct smb2_query_info_req
*req
;
2169 struct smb2_query_info_rsp
*rsp
= NULL
;
2171 struct kvec rsp_iov
;
2174 struct cifs_ses
*ses
= tcon
->ses
;
2177 cifs_dbg(FYI
, "Query Info\n");
2179 if (!ses
|| !(ses
->server
))
2182 rc
= small_smb2_init(SMB2_QUERY_INFO
, tcon
, (void **) &req
);
2186 if (encryption_required(tcon
))
2187 flags
|= CIFS_TRANSFORM_REQ
;
2189 req
->InfoType
= info_type
;
2190 req
->FileInfoClass
= info_class
;
2191 req
->PersistentFileId
= persistent_fid
;
2192 req
->VolatileFileId
= volatile_fid
;
2193 req
->AdditionalInformation
= cpu_to_le32(additional_info
);
2194 /* 4 for rfc1002 length field and 1 for Buffer */
2195 req
->InputBufferOffset
=
2196 cpu_to_le16(sizeof(struct smb2_query_info_req
) - 1 - 4);
2197 req
->OutputBufferLength
= cpu_to_le32(output_len
);
2199 iov
[0].iov_base
= (char *)req
;
2200 /* 4 for rfc1002 length field */
2201 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
2203 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buftype
, flags
, &rsp_iov
);
2204 cifs_small_buf_release(req
);
2205 rsp
= (struct smb2_query_info_rsp
*)rsp_iov
.iov_base
;
2208 cifs_stats_fail_inc(tcon
, SMB2_QUERY_INFO_HE
);
2213 *dlen
= le32_to_cpu(rsp
->OutputBufferLength
);
2215 *data
= kmalloc(*dlen
, GFP_KERNEL
);
2218 "Error %d allocating memory for acl\n",
2226 rc
= validate_and_copy_buf(le16_to_cpu(rsp
->OutputBufferOffset
),
2227 le32_to_cpu(rsp
->OutputBufferLength
),
2228 &rsp
->hdr
, min_len
, *data
);
2231 free_rsp_buf(resp_buftype
, rsp
);
2235 int SMB2_query_eas(const unsigned int xid
, struct cifs_tcon
*tcon
,
2236 u64 persistent_fid
, u64 volatile_fid
,
2237 struct smb2_file_full_ea_info
*data
)
2239 return query_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2240 FILE_FULL_EA_INFORMATION
, SMB2_O_INFO_FILE
, 0,
2242 sizeof(struct smb2_file_full_ea_info
),
2247 int SMB2_query_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
2248 u64 persistent_fid
, u64 volatile_fid
, struct smb2_file_all_info
*data
)
2250 return query_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2251 FILE_ALL_INFORMATION
, SMB2_O_INFO_FILE
, 0,
2252 sizeof(struct smb2_file_all_info
) + PATH_MAX
* 2,
2253 sizeof(struct smb2_file_all_info
), (void **)&data
,
2258 SMB2_query_acl(const unsigned int xid
, struct cifs_tcon
*tcon
,
2259 u64 persistent_fid
, u64 volatile_fid
,
2260 void **data
, u32
*plen
)
2262 __u32 additional_info
= OWNER_SECINFO
| GROUP_SECINFO
| DACL_SECINFO
;
2265 return query_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2266 0, SMB2_O_INFO_SECURITY
, additional_info
,
2267 SMB2_MAX_BUFFER_SIZE
,
2268 sizeof(struct smb2_file_all_info
), data
, plen
);
2272 SMB2_get_srv_num(const unsigned int xid
, struct cifs_tcon
*tcon
,
2273 u64 persistent_fid
, u64 volatile_fid
, __le64
*uniqueid
)
2275 return query_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2276 FILE_INTERNAL_INFORMATION
, SMB2_O_INFO_FILE
, 0,
2277 sizeof(struct smb2_file_internal_info
),
2278 sizeof(struct smb2_file_internal_info
),
2279 (void **)&uniqueid
, NULL
);
2283 * This is a no-op for now. We're not really interested in the reply, but
2284 * rather in the fact that the server sent one and that server->lstrp
2287 * FIXME: maybe we should consider checking that the reply matches request?
2290 smb2_echo_callback(struct mid_q_entry
*mid
)
2292 struct TCP_Server_Info
*server
= mid
->callback_data
;
2293 struct smb2_echo_rsp
*rsp
= (struct smb2_echo_rsp
*)mid
->resp_buf
;
2294 unsigned int credits_received
= 1;
2296 if (mid
->mid_state
== MID_RESPONSE_RECEIVED
)
2297 credits_received
= le16_to_cpu(rsp
->hdr
.sync_hdr
.CreditRequest
);
2299 DeleteMidQEntry(mid
);
2300 add_credits(server
, credits_received
, CIFS_ECHO_OP
);
2303 void smb2_reconnect_server(struct work_struct
*work
)
2305 struct TCP_Server_Info
*server
= container_of(work
,
2306 struct TCP_Server_Info
, reconnect
.work
);
2307 struct cifs_ses
*ses
;
2308 struct cifs_tcon
*tcon
, *tcon2
;
2309 struct list_head tmp_list
;
2310 int tcon_exist
= false;
2312 int resched
= false;
2315 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2316 mutex_lock(&server
->reconnect_mutex
);
2318 INIT_LIST_HEAD(&tmp_list
);
2319 cifs_dbg(FYI
, "Need negotiate, reconnecting tcons\n");
2321 spin_lock(&cifs_tcp_ses_lock
);
2322 list_for_each_entry(ses
, &server
->smb_ses_list
, smb_ses_list
) {
2323 list_for_each_entry(tcon
, &ses
->tcon_list
, tcon_list
) {
2324 if (tcon
->need_reconnect
|| tcon
->need_reopen_files
) {
2326 list_add_tail(&tcon
->rlist
, &tmp_list
);
2332 * Get the reference to server struct to be sure that the last call of
2333 * cifs_put_tcon() in the loop below won't release the server pointer.
2336 server
->srv_count
++;
2338 spin_unlock(&cifs_tcp_ses_lock
);
2340 list_for_each_entry_safe(tcon
, tcon2
, &tmp_list
, rlist
) {
2341 rc
= smb2_reconnect(SMB2_INTERNAL_CMD
, tcon
);
2343 cifs_reopen_persistent_handles(tcon
);
2346 list_del_init(&tcon
->rlist
);
2347 cifs_put_tcon(tcon
);
2350 cifs_dbg(FYI
, "Reconnecting tcons finished\n");
2352 queue_delayed_work(cifsiod_wq
, &server
->reconnect
, 2 * HZ
);
2353 mutex_unlock(&server
->reconnect_mutex
);
2355 /* now we can safely release srv struct */
2357 cifs_put_tcp_session(server
, 1);
2361 SMB2_echo(struct TCP_Server_Info
*server
)
2363 struct smb2_echo_req
*req
;
2366 struct smb_rqst rqst
= { .rq_iov
= iov
,
2369 cifs_dbg(FYI
, "In echo request\n");
2371 if (server
->tcpStatus
== CifsNeedNegotiate
) {
2372 /* No need to send echo on newly established connections */
2373 queue_delayed_work(cifsiod_wq
, &server
->reconnect
, 0);
2377 rc
= small_smb2_init(SMB2_ECHO
, NULL
, (void **)&req
);
2381 req
->hdr
.sync_hdr
.CreditRequest
= cpu_to_le16(1);
2383 /* 4 for rfc1002 length field */
2385 iov
[0].iov_base
= (char *)req
;
2386 iov
[1].iov_len
= get_rfc1002_length(req
);
2387 iov
[1].iov_base
= (char *)req
+ 4;
2389 rc
= cifs_call_async(server
, &rqst
, NULL
, smb2_echo_callback
, NULL
,
2390 server
, CIFS_ECHO_OP
);
2392 cifs_dbg(FYI
, "Echo request failed: %d\n", rc
);
2394 cifs_small_buf_release(req
);
2399 SMB2_flush(const unsigned int xid
, struct cifs_tcon
*tcon
, u64 persistent_fid
,
2402 struct smb2_flush_req
*req
;
2403 struct cifs_ses
*ses
= tcon
->ses
;
2405 struct kvec rsp_iov
;
2410 cifs_dbg(FYI
, "Flush\n");
2412 if (!ses
|| !(ses
->server
))
2415 rc
= small_smb2_init(SMB2_FLUSH
, tcon
, (void **) &req
);
2419 if (encryption_required(tcon
))
2420 flags
|= CIFS_TRANSFORM_REQ
;
2422 req
->PersistentFileId
= persistent_fid
;
2423 req
->VolatileFileId
= volatile_fid
;
2425 iov
[0].iov_base
= (char *)req
;
2426 /* 4 for rfc1002 length field */
2427 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
2429 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buftype
, flags
, &rsp_iov
);
2430 cifs_small_buf_release(req
);
2433 cifs_stats_fail_inc(tcon
, SMB2_FLUSH_HE
);
2435 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
2440 * To form a chain of read requests, any read requests after the first should
2441 * have the end_of_chain boolean set to true.
2444 smb2_new_read_req(void **buf
, unsigned int *total_len
,
2445 struct cifs_io_parms
*io_parms
, unsigned int remaining_bytes
,
2449 struct smb2_read_plain_req
*req
= NULL
;
2450 struct smb2_sync_hdr
*shdr
;
2452 rc
= smb2_plain_req_init(SMB2_READ
, io_parms
->tcon
, (void **) &req
,
2456 if (io_parms
->tcon
->ses
->server
== NULL
)
2457 return -ECONNABORTED
;
2459 shdr
= &req
->sync_hdr
;
2460 shdr
->ProcessId
= cpu_to_le32(io_parms
->pid
);
2462 req
->PersistentFileId
= io_parms
->persistent_fid
;
2463 req
->VolatileFileId
= io_parms
->volatile_fid
;
2464 req
->ReadChannelInfoOffset
= 0; /* reserved */
2465 req
->ReadChannelInfoLength
= 0; /* reserved */
2466 req
->Channel
= 0; /* reserved */
2467 req
->MinimumCount
= 0;
2468 req
->Length
= cpu_to_le32(io_parms
->length
);
2469 req
->Offset
= cpu_to_le64(io_parms
->offset
);
2471 if (request_type
& CHAINED_REQUEST
) {
2472 if (!(request_type
& END_OF_CHAIN
)) {
2473 /* next 8-byte aligned request */
2474 *total_len
= DIV_ROUND_UP(*total_len
, 8) * 8;
2475 shdr
->NextCommand
= cpu_to_le32(*total_len
);
2476 } else /* END_OF_CHAIN */
2477 shdr
->NextCommand
= 0;
2478 if (request_type
& RELATED_REQUEST
) {
2479 shdr
->Flags
|= SMB2_FLAGS_RELATED_OPERATIONS
;
2481 * Related requests use info from previous read request
2484 shdr
->SessionId
= 0xFFFFFFFF;
2485 shdr
->TreeId
= 0xFFFFFFFF;
2486 req
->PersistentFileId
= 0xFFFFFFFF;
2487 req
->VolatileFileId
= 0xFFFFFFFF;
2490 if (remaining_bytes
> io_parms
->length
)
2491 req
->RemainingBytes
= cpu_to_le32(remaining_bytes
);
2493 req
->RemainingBytes
= 0;
2500 smb2_readv_callback(struct mid_q_entry
*mid
)
2502 struct cifs_readdata
*rdata
= mid
->callback_data
;
2503 struct cifs_tcon
*tcon
= tlink_tcon(rdata
->cfile
->tlink
);
2504 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
2505 struct smb2_sync_hdr
*shdr
=
2506 (struct smb2_sync_hdr
*)rdata
->iov
[1].iov_base
;
2507 unsigned int credits_received
= 1;
2508 struct smb_rqst rqst
= { .rq_iov
= rdata
->iov
,
2510 .rq_pages
= rdata
->pages
,
2511 .rq_npages
= rdata
->nr_pages
,
2512 .rq_pagesz
= rdata
->pagesz
,
2513 .rq_tailsz
= rdata
->tailsz
};
2515 cifs_dbg(FYI
, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2516 __func__
, mid
->mid
, mid
->mid_state
, rdata
->result
,
2519 switch (mid
->mid_state
) {
2520 case MID_RESPONSE_RECEIVED
:
2521 credits_received
= le16_to_cpu(shdr
->CreditRequest
);
2522 /* result already set, check signature */
2523 if (server
->sign
&& !mid
->decrypted
) {
2526 rc
= smb2_verify_signature(&rqst
, server
);
2528 cifs_dbg(VFS
, "SMB signature verification returned error = %d\n",
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
);
2535 case MID_REQUEST_SUBMITTED
:
2536 case MID_RETRY_NEEDED
:
2537 rdata
->result
= -EAGAIN
;
2538 if (server
->sign
&& rdata
->got_bytes
)
2539 /* reset bytes number since we can not check a sign */
2540 rdata
->got_bytes
= 0;
2541 /* FIXME: should this be counted toward the initiating task? */
2542 task_io_account_read(rdata
->got_bytes
);
2543 cifs_stats_bytes_read(tcon
, rdata
->got_bytes
);
2546 if (rdata
->result
!= -ENODATA
)
2547 rdata
->result
= -EIO
;
2551 cifs_stats_fail_inc(tcon
, SMB2_READ_HE
);
2553 queue_work(cifsiod_wq
, &rdata
->work
);
2554 DeleteMidQEntry(mid
);
2555 add_credits(server
, credits_received
, 0);
2558 /* smb2_async_readv - send an async read, and set up mid to handle result */
2560 smb2_async_readv(struct cifs_readdata
*rdata
)
2564 struct smb2_sync_hdr
*shdr
;
2565 struct cifs_io_parms io_parms
;
2566 struct smb_rqst rqst
= { .rq_iov
= rdata
->iov
,
2568 struct TCP_Server_Info
*server
;
2569 unsigned int total_len
;
2572 cifs_dbg(FYI
, "%s: offset=%llu bytes=%u\n",
2573 __func__
, rdata
->offset
, rdata
->bytes
);
2575 io_parms
.tcon
= tlink_tcon(rdata
->cfile
->tlink
);
2576 io_parms
.offset
= rdata
->offset
;
2577 io_parms
.length
= rdata
->bytes
;
2578 io_parms
.persistent_fid
= rdata
->cfile
->fid
.persistent_fid
;
2579 io_parms
.volatile_fid
= rdata
->cfile
->fid
.volatile_fid
;
2580 io_parms
.pid
= rdata
->pid
;
2582 server
= io_parms
.tcon
->ses
->server
;
2584 rc
= smb2_new_read_req((void **) &buf
, &total_len
, &io_parms
, 0, 0);
2586 if (rc
== -EAGAIN
&& rdata
->credits
) {
2587 /* credits was reset by reconnect */
2589 /* reduce in_flight value since we won't send the req */
2590 spin_lock(&server
->req_lock
);
2591 server
->in_flight
--;
2592 spin_unlock(&server
->req_lock
);
2597 if (encryption_required(io_parms
.tcon
))
2598 flags
|= CIFS_TRANSFORM_REQ
;
2600 req_len
= cpu_to_be32(total_len
);
2602 rdata
->iov
[0].iov_base
= &req_len
;
2603 rdata
->iov
[0].iov_len
= sizeof(__be32
);
2604 rdata
->iov
[1].iov_base
= buf
;
2605 rdata
->iov
[1].iov_len
= total_len
;
2607 shdr
= (struct smb2_sync_hdr
*)buf
;
2609 if (rdata
->credits
) {
2610 shdr
->CreditCharge
= cpu_to_le16(DIV_ROUND_UP(rdata
->bytes
,
2611 SMB2_MAX_BUFFER_SIZE
));
2612 shdr
->CreditRequest
= shdr
->CreditCharge
;
2613 spin_lock(&server
->req_lock
);
2614 server
->credits
+= rdata
->credits
-
2615 le16_to_cpu(shdr
->CreditCharge
);
2616 spin_unlock(&server
->req_lock
);
2617 wake_up(&server
->request_q
);
2618 flags
|= CIFS_HAS_CREDITS
;
2621 kref_get(&rdata
->refcount
);
2622 rc
= cifs_call_async(io_parms
.tcon
->ses
->server
, &rqst
,
2623 cifs_readv_receive
, smb2_readv_callback
,
2624 smb3_handle_read_data
, rdata
, flags
);
2626 kref_put(&rdata
->refcount
, cifs_readdata_release
);
2627 cifs_stats_fail_inc(io_parms
.tcon
, SMB2_READ_HE
);
2630 cifs_small_buf_release(buf
);
2635 SMB2_read(const unsigned int xid
, struct cifs_io_parms
*io_parms
,
2636 unsigned int *nbytes
, char **buf
, int *buf_type
)
2638 int resp_buftype
, rc
= -EACCES
;
2639 struct smb2_read_plain_req
*req
= NULL
;
2640 struct smb2_read_rsp
*rsp
= NULL
;
2641 struct smb2_sync_hdr
*shdr
;
2643 struct kvec rsp_iov
;
2644 unsigned int total_len
;
2646 struct smb_rqst rqst
= { .rq_iov
= iov
,
2648 int flags
= CIFS_LOG_ERROR
;
2649 struct cifs_ses
*ses
= io_parms
->tcon
->ses
;
2652 rc
= smb2_new_read_req((void **)&req
, &total_len
, io_parms
, 0, 0);
2656 if (encryption_required(io_parms
->tcon
))
2657 flags
|= CIFS_TRANSFORM_REQ
;
2659 req_len
= cpu_to_be32(total_len
);
2661 iov
[0].iov_base
= &req_len
;
2662 iov
[0].iov_len
= sizeof(__be32
);
2663 iov
[1].iov_base
= req
;
2664 iov
[1].iov_len
= total_len
;
2666 rc
= cifs_send_recv(xid
, ses
, &rqst
, &resp_buftype
, flags
, &rsp_iov
);
2667 cifs_small_buf_release(req
);
2669 rsp
= (struct smb2_read_rsp
*)rsp_iov
.iov_base
;
2670 shdr
= get_sync_hdr(rsp
);
2672 if (shdr
->Status
== STATUS_END_OF_FILE
) {
2673 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
2678 cifs_stats_fail_inc(io_parms
->tcon
, SMB2_READ_HE
);
2679 cifs_dbg(VFS
, "Send error in read = %d\n", rc
);
2681 *nbytes
= le32_to_cpu(rsp
->DataLength
);
2682 if ((*nbytes
> CIFS_MAX_MSGSIZE
) ||
2683 (*nbytes
> io_parms
->length
)) {
2684 cifs_dbg(FYI
, "bad length %d for count %d\n",
2685 *nbytes
, io_parms
->length
);
2692 memcpy(*buf
, (char *)shdr
+ rsp
->DataOffset
, *nbytes
);
2693 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
2694 } else if (resp_buftype
!= CIFS_NO_BUFFER
) {
2695 *buf
= rsp_iov
.iov_base
;
2696 if (resp_buftype
== CIFS_SMALL_BUFFER
)
2697 *buf_type
= CIFS_SMALL_BUFFER
;
2698 else if (resp_buftype
== CIFS_LARGE_BUFFER
)
2699 *buf_type
= CIFS_LARGE_BUFFER
;
2705 * Check the mid_state and signature on received buffer (if any), and queue the
2706 * workqueue completion task.
2709 smb2_writev_callback(struct mid_q_entry
*mid
)
2711 struct cifs_writedata
*wdata
= mid
->callback_data
;
2712 struct cifs_tcon
*tcon
= tlink_tcon(wdata
->cfile
->tlink
);
2713 unsigned int written
;
2714 struct smb2_write_rsp
*rsp
= (struct smb2_write_rsp
*)mid
->resp_buf
;
2715 unsigned int credits_received
= 1;
2717 switch (mid
->mid_state
) {
2718 case MID_RESPONSE_RECEIVED
:
2719 credits_received
= le16_to_cpu(rsp
->hdr
.sync_hdr
.CreditRequest
);
2720 wdata
->result
= smb2_check_receive(mid
, tcon
->ses
->server
, 0);
2721 if (wdata
->result
!= 0)
2724 written
= le32_to_cpu(rsp
->DataLength
);
2726 * Mask off high 16 bits when bytes written as returned
2727 * by the server is greater than bytes requested by the
2728 * client. OS/2 servers are known to set incorrect
2731 if (written
> wdata
->bytes
)
2734 if (written
< wdata
->bytes
)
2735 wdata
->result
= -ENOSPC
;
2737 wdata
->bytes
= written
;
2739 case MID_REQUEST_SUBMITTED
:
2740 case MID_RETRY_NEEDED
:
2741 wdata
->result
= -EAGAIN
;
2744 wdata
->result
= -EIO
;
2749 cifs_stats_fail_inc(tcon
, SMB2_WRITE_HE
);
2751 queue_work(cifsiod_wq
, &wdata
->work
);
2752 DeleteMidQEntry(mid
);
2753 add_credits(tcon
->ses
->server
, credits_received
, 0);
2756 /* smb2_async_writev - send an async write, and set up mid to handle result */
2758 smb2_async_writev(struct cifs_writedata
*wdata
,
2759 void (*release
)(struct kref
*kref
))
2761 int rc
= -EACCES
, flags
= 0;
2762 struct smb2_write_req
*req
= NULL
;
2763 struct smb2_sync_hdr
*shdr
;
2764 struct cifs_tcon
*tcon
= tlink_tcon(wdata
->cfile
->tlink
);
2765 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
2767 struct smb_rqst rqst
= { };
2769 rc
= small_smb2_init(SMB2_WRITE
, tcon
, (void **) &req
);
2771 if (rc
== -EAGAIN
&& wdata
->credits
) {
2772 /* credits was reset by reconnect */
2774 /* reduce in_flight value since we won't send the req */
2775 spin_lock(&server
->req_lock
);
2776 server
->in_flight
--;
2777 spin_unlock(&server
->req_lock
);
2779 goto async_writev_out
;
2782 if (encryption_required(tcon
))
2783 flags
|= CIFS_TRANSFORM_REQ
;
2785 shdr
= get_sync_hdr(req
);
2786 shdr
->ProcessId
= cpu_to_le32(wdata
->cfile
->pid
);
2788 req
->PersistentFileId
= wdata
->cfile
->fid
.persistent_fid
;
2789 req
->VolatileFileId
= wdata
->cfile
->fid
.volatile_fid
;
2790 req
->WriteChannelInfoOffset
= 0;
2791 req
->WriteChannelInfoLength
= 0;
2793 req
->Offset
= cpu_to_le64(wdata
->offset
);
2794 /* 4 for rfc1002 length field */
2795 req
->DataOffset
= cpu_to_le16(
2796 offsetof(struct smb2_write_req
, Buffer
) - 4);
2797 req
->RemainingBytes
= 0;
2799 /* 4 for rfc1002 length field and 1 for Buffer */
2801 iov
[0].iov_base
= req
;
2802 iov
[1].iov_len
= get_rfc1002_length(req
) - 1;
2803 iov
[1].iov_base
= (char *)req
+ 4;
2807 rqst
.rq_pages
= wdata
->pages
;
2808 rqst
.rq_npages
= wdata
->nr_pages
;
2809 rqst
.rq_pagesz
= wdata
->pagesz
;
2810 rqst
.rq_tailsz
= wdata
->tailsz
;
2812 cifs_dbg(FYI
, "async write at %llu %u bytes\n",
2813 wdata
->offset
, wdata
->bytes
);
2815 req
->Length
= cpu_to_le32(wdata
->bytes
);
2817 inc_rfc1001_len(&req
->hdr
, wdata
->bytes
- 1 /* Buffer */);
2819 if (wdata
->credits
) {
2820 shdr
->CreditCharge
= cpu_to_le16(DIV_ROUND_UP(wdata
->bytes
,
2821 SMB2_MAX_BUFFER_SIZE
));
2822 shdr
->CreditRequest
= shdr
->CreditCharge
;
2823 spin_lock(&server
->req_lock
);
2824 server
->credits
+= wdata
->credits
-
2825 le16_to_cpu(shdr
->CreditCharge
);
2826 spin_unlock(&server
->req_lock
);
2827 wake_up(&server
->request_q
);
2828 flags
|= CIFS_HAS_CREDITS
;
2831 kref_get(&wdata
->refcount
);
2832 rc
= cifs_call_async(server
, &rqst
, NULL
, smb2_writev_callback
, NULL
,
2836 kref_put(&wdata
->refcount
, release
);
2837 cifs_stats_fail_inc(tcon
, SMB2_WRITE_HE
);
2841 cifs_small_buf_release(req
);
2846 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2847 * The length field from io_parms must be at least 1 and indicates a number of
2848 * elements with data to write that begins with position 1 in iov array. All
2849 * data length is specified by count.
2852 SMB2_write(const unsigned int xid
, struct cifs_io_parms
*io_parms
,
2853 unsigned int *nbytes
, struct kvec
*iov
, int n_vec
)
2856 struct smb2_write_req
*req
= NULL
;
2857 struct smb2_write_rsp
*rsp
= NULL
;
2859 struct kvec rsp_iov
;
2867 rc
= small_smb2_init(SMB2_WRITE
, io_parms
->tcon
, (void **) &req
);
2871 if (io_parms
->tcon
->ses
->server
== NULL
)
2872 return -ECONNABORTED
;
2874 if (encryption_required(io_parms
->tcon
))
2875 flags
|= CIFS_TRANSFORM_REQ
;
2877 req
->hdr
.sync_hdr
.ProcessId
= cpu_to_le32(io_parms
->pid
);
2879 req
->PersistentFileId
= io_parms
->persistent_fid
;
2880 req
->VolatileFileId
= io_parms
->volatile_fid
;
2881 req
->WriteChannelInfoOffset
= 0;
2882 req
->WriteChannelInfoLength
= 0;
2884 req
->Length
= cpu_to_le32(io_parms
->length
);
2885 req
->Offset
= cpu_to_le64(io_parms
->offset
);
2886 /* 4 for rfc1002 length field */
2887 req
->DataOffset
= cpu_to_le16(
2888 offsetof(struct smb2_write_req
, Buffer
) - 4);
2889 req
->RemainingBytes
= 0;
2891 iov
[0].iov_base
= (char *)req
;
2892 /* 4 for rfc1002 length field and 1 for Buffer */
2893 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
2895 /* length of entire message including data to be written */
2896 inc_rfc1001_len(req
, io_parms
->length
- 1 /* Buffer */);
2898 rc
= SendReceive2(xid
, io_parms
->tcon
->ses
, iov
, n_vec
+ 1,
2899 &resp_buftype
, flags
, &rsp_iov
);
2900 cifs_small_buf_release(req
);
2901 rsp
= (struct smb2_write_rsp
*)rsp_iov
.iov_base
;
2904 cifs_stats_fail_inc(io_parms
->tcon
, SMB2_WRITE_HE
);
2905 cifs_dbg(VFS
, "Send error in write = %d\n", rc
);
2907 *nbytes
= le32_to_cpu(rsp
->DataLength
);
2909 free_rsp_buf(resp_buftype
, rsp
);
2914 num_entries(char *bufstart
, char *end_of_buf
, char **lastentry
, size_t size
)
2917 unsigned int entrycount
= 0;
2918 unsigned int next_offset
= 0;
2919 FILE_DIRECTORY_INFO
*entryptr
;
2921 if (bufstart
== NULL
)
2924 entryptr
= (FILE_DIRECTORY_INFO
*)bufstart
;
2927 entryptr
= (FILE_DIRECTORY_INFO
*)
2928 ((char *)entryptr
+ next_offset
);
2930 if ((char *)entryptr
+ size
> end_of_buf
) {
2931 cifs_dbg(VFS
, "malformed search entry would overflow\n");
2935 len
= le32_to_cpu(entryptr
->FileNameLength
);
2936 if ((char *)entryptr
+ len
+ size
> end_of_buf
) {
2937 cifs_dbg(VFS
, "directory entry name would overflow frame end of buf %p\n",
2942 *lastentry
= (char *)entryptr
;
2945 next_offset
= le32_to_cpu(entryptr
->NextEntryOffset
);
2957 SMB2_query_directory(const unsigned int xid
, struct cifs_tcon
*tcon
,
2958 u64 persistent_fid
, u64 volatile_fid
, int index
,
2959 struct cifs_search_info
*srch_inf
)
2961 struct smb2_query_directory_req
*req
;
2962 struct smb2_query_directory_rsp
*rsp
= NULL
;
2964 struct kvec rsp_iov
;
2967 int resp_buftype
= CIFS_NO_BUFFER
;
2968 unsigned char *bufptr
;
2969 struct TCP_Server_Info
*server
;
2970 struct cifs_ses
*ses
= tcon
->ses
;
2971 __le16 asteriks
= cpu_to_le16('*');
2973 unsigned int output_size
= CIFSMaxBufSize
;
2974 size_t info_buf_size
;
2977 if (ses
&& (ses
->server
))
2978 server
= ses
->server
;
2982 rc
= small_smb2_init(SMB2_QUERY_DIRECTORY
, tcon
, (void **) &req
);
2986 if (encryption_required(tcon
))
2987 flags
|= CIFS_TRANSFORM_REQ
;
2989 switch (srch_inf
->info_level
) {
2990 case SMB_FIND_FILE_DIRECTORY_INFO
:
2991 req
->FileInformationClass
= FILE_DIRECTORY_INFORMATION
;
2992 info_buf_size
= sizeof(FILE_DIRECTORY_INFO
) - 1;
2994 case SMB_FIND_FILE_ID_FULL_DIR_INFO
:
2995 req
->FileInformationClass
= FILEID_FULL_DIRECTORY_INFORMATION
;
2996 info_buf_size
= sizeof(SEARCH_ID_FULL_DIR_INFO
) - 1;
2999 cifs_dbg(VFS
, "info level %u isn't supported\n",
3000 srch_inf
->info_level
);
3005 req
->FileIndex
= cpu_to_le32(index
);
3006 req
->PersistentFileId
= persistent_fid
;
3007 req
->VolatileFileId
= volatile_fid
;
3010 bufptr
= req
->Buffer
;
3011 memcpy(bufptr
, &asteriks
, len
);
3013 req
->FileNameOffset
=
3014 cpu_to_le16(sizeof(struct smb2_query_directory_req
) - 1 - 4);
3015 req
->FileNameLength
= cpu_to_le16(len
);
3017 * BB could be 30 bytes or so longer if we used SMB2 specific
3018 * buffer lengths, but this is safe and close enough.
3020 output_size
= min_t(unsigned int, output_size
, server
->maxBuf
);
3021 output_size
= min_t(unsigned int, output_size
, 2 << 15);
3022 req
->OutputBufferLength
= cpu_to_le32(output_size
);
3024 iov
[0].iov_base
= (char *)req
;
3025 /* 4 for RFC1001 length and 1 for Buffer */
3026 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
3028 iov
[1].iov_base
= (char *)(req
->Buffer
);
3029 iov
[1].iov_len
= len
;
3031 inc_rfc1001_len(req
, len
- 1 /* Buffer */);
3033 rc
= SendReceive2(xid
, ses
, iov
, 2, &resp_buftype
, flags
, &rsp_iov
);
3034 cifs_small_buf_release(req
);
3035 rsp
= (struct smb2_query_directory_rsp
*)rsp_iov
.iov_base
;
3038 if (rc
== -ENODATA
&&
3039 rsp
->hdr
.sync_hdr
.Status
== STATUS_NO_MORE_FILES
) {
3040 srch_inf
->endOfSearch
= true;
3043 cifs_stats_fail_inc(tcon
, SMB2_QUERY_DIRECTORY_HE
);
3047 rc
= validate_buf(le16_to_cpu(rsp
->OutputBufferOffset
),
3048 le32_to_cpu(rsp
->OutputBufferLength
), &rsp
->hdr
,
3053 srch_inf
->unicode
= true;
3055 if (srch_inf
->ntwrk_buf_start
) {
3056 if (srch_inf
->smallBuf
)
3057 cifs_small_buf_release(srch_inf
->ntwrk_buf_start
);
3059 cifs_buf_release(srch_inf
->ntwrk_buf_start
);
3061 srch_inf
->ntwrk_buf_start
= (char *)rsp
;
3062 srch_inf
->srch_entries_start
= srch_inf
->last_entry
= 4 /* rfclen */ +
3063 (char *)&rsp
->hdr
+ le16_to_cpu(rsp
->OutputBufferOffset
);
3064 /* 4 for rfc1002 length field */
3065 end_of_smb
= get_rfc1002_length(rsp
) + 4 + (char *)&rsp
->hdr
;
3066 srch_inf
->entries_in_buffer
=
3067 num_entries(srch_inf
->srch_entries_start
, end_of_smb
,
3068 &srch_inf
->last_entry
, info_buf_size
);
3069 srch_inf
->index_of_last_entry
+= srch_inf
->entries_in_buffer
;
3070 cifs_dbg(FYI
, "num entries %d last_index %lld srch start %p srch end %p\n",
3071 srch_inf
->entries_in_buffer
, srch_inf
->index_of_last_entry
,
3072 srch_inf
->srch_entries_start
, srch_inf
->last_entry
);
3073 if (resp_buftype
== CIFS_LARGE_BUFFER
)
3074 srch_inf
->smallBuf
= false;
3075 else if (resp_buftype
== CIFS_SMALL_BUFFER
)
3076 srch_inf
->smallBuf
= true;
3078 cifs_dbg(VFS
, "illegal search buffer type\n");
3083 free_rsp_buf(resp_buftype
, rsp
);
3088 send_set_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
3089 u64 persistent_fid
, u64 volatile_fid
, u32 pid
, u8 info_class
,
3090 u8 info_type
, u32 additional_info
, unsigned int num
,
3091 void **data
, unsigned int *size
)
3093 struct smb2_set_info_req
*req
;
3094 struct smb2_set_info_rsp
*rsp
= NULL
;
3096 struct kvec rsp_iov
;
3100 struct cifs_ses
*ses
= tcon
->ses
;
3103 if (!ses
|| !(ses
->server
))
3109 iov
= kmalloc(sizeof(struct kvec
) * num
, GFP_KERNEL
);
3113 rc
= small_smb2_init(SMB2_SET_INFO
, tcon
, (void **) &req
);
3119 if (encryption_required(tcon
))
3120 flags
|= CIFS_TRANSFORM_REQ
;
3122 req
->hdr
.sync_hdr
.ProcessId
= cpu_to_le32(pid
);
3124 req
->InfoType
= info_type
;
3125 req
->FileInfoClass
= info_class
;
3126 req
->PersistentFileId
= persistent_fid
;
3127 req
->VolatileFileId
= volatile_fid
;
3128 req
->AdditionalInformation
= cpu_to_le32(additional_info
);
3130 /* 4 for RFC1001 length and 1 for Buffer */
3132 cpu_to_le16(sizeof(struct smb2_set_info_req
) - 1 - 4);
3133 req
->BufferLength
= cpu_to_le32(*size
);
3135 inc_rfc1001_len(req
, *size
- 1 /* Buffer */);
3137 memcpy(req
->Buffer
, *data
, *size
);
3139 iov
[0].iov_base
= (char *)req
;
3140 /* 4 for RFC1001 length */
3141 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
3143 for (i
= 1; i
< num
; i
++) {
3144 inc_rfc1001_len(req
, size
[i
]);
3145 le32_add_cpu(&req
->BufferLength
, size
[i
]);
3146 iov
[i
].iov_base
= (char *)data
[i
];
3147 iov
[i
].iov_len
= size
[i
];
3150 rc
= SendReceive2(xid
, ses
, iov
, num
, &resp_buftype
, flags
, &rsp_iov
);
3151 cifs_small_buf_release(req
);
3152 rsp
= (struct smb2_set_info_rsp
*)rsp_iov
.iov_base
;
3155 cifs_stats_fail_inc(tcon
, SMB2_SET_INFO_HE
);
3157 free_rsp_buf(resp_buftype
, rsp
);
3163 SMB2_rename(const unsigned int xid
, struct cifs_tcon
*tcon
,
3164 u64 persistent_fid
, u64 volatile_fid
, __le16
*target_file
)
3166 struct smb2_file_rename_info info
;
3168 unsigned int size
[2];
3170 int len
= (2 * UniStrnlen((wchar_t *)target_file
, PATH_MAX
));
3172 data
= kmalloc(sizeof(void *) * 2, GFP_KERNEL
);
3176 info
.ReplaceIfExists
= 1; /* 1 = replace existing target with new */
3177 /* 0 = fail if target already exists */
3178 info
.RootDirectory
= 0; /* MBZ for network ops (why does spec say?) */
3179 info
.FileNameLength
= cpu_to_le32(len
);
3182 size
[0] = sizeof(struct smb2_file_rename_info
);
3184 data
[1] = target_file
;
3185 size
[1] = len
+ 2 /* null */;
3187 rc
= send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3188 current
->tgid
, FILE_RENAME_INFORMATION
, SMB2_O_INFO_FILE
,
3195 SMB2_rmdir(const unsigned int xid
, struct cifs_tcon
*tcon
,
3196 u64 persistent_fid
, u64 volatile_fid
)
3198 __u8 delete_pending
= 1;
3202 data
= &delete_pending
;
3203 size
= 1; /* sizeof __u8 */
3205 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3206 current
->tgid
, FILE_DISPOSITION_INFORMATION
, SMB2_O_INFO_FILE
,
3207 0, 1, &data
, &size
);
3211 SMB2_set_hardlink(const unsigned int xid
, struct cifs_tcon
*tcon
,
3212 u64 persistent_fid
, u64 volatile_fid
, __le16
*target_file
)
3214 struct smb2_file_link_info info
;
3216 unsigned int size
[2];
3218 int len
= (2 * UniStrnlen((wchar_t *)target_file
, PATH_MAX
));
3220 data
= kmalloc(sizeof(void *) * 2, GFP_KERNEL
);
3224 info
.ReplaceIfExists
= 0; /* 1 = replace existing link with new */
3225 /* 0 = fail if link already exists */
3226 info
.RootDirectory
= 0; /* MBZ for network ops (why does spec say?) */
3227 info
.FileNameLength
= cpu_to_le32(len
);
3230 size
[0] = sizeof(struct smb2_file_link_info
);
3232 data
[1] = target_file
;
3233 size
[1] = len
+ 2 /* null */;
3235 rc
= send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3236 current
->tgid
, FILE_LINK_INFORMATION
, SMB2_O_INFO_FILE
,
3243 SMB2_set_eof(const unsigned int xid
, struct cifs_tcon
*tcon
, u64 persistent_fid
,
3244 u64 volatile_fid
, u32 pid
, __le64
*eof
, bool is_falloc
)
3246 struct smb2_file_eof_info info
;
3250 info
.EndOfFile
= *eof
;
3253 size
= sizeof(struct smb2_file_eof_info
);
3256 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3257 pid
, FILE_ALLOCATION_INFORMATION
, SMB2_O_INFO_FILE
,
3258 0, 1, &data
, &size
);
3260 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3261 pid
, FILE_END_OF_FILE_INFORMATION
, SMB2_O_INFO_FILE
,
3262 0, 1, &data
, &size
);
3266 SMB2_set_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
3267 u64 persistent_fid
, u64 volatile_fid
, FILE_BASIC_INFO
*buf
)
3270 size
= sizeof(FILE_BASIC_INFO
);
3271 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3272 current
->tgid
, FILE_BASIC_INFORMATION
, SMB2_O_INFO_FILE
,
3273 0, 1, (void **)&buf
, &size
);
3277 SMB2_set_acl(const unsigned int xid
, struct cifs_tcon
*tcon
,
3278 u64 persistent_fid
, u64 volatile_fid
,
3279 struct cifs_ntsd
*pnntsd
, int pacllen
, int aclflag
)
3281 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3282 current
->tgid
, 0, SMB2_O_INFO_SECURITY
, aclflag
,
3283 1, (void **)&pnntsd
, &pacllen
);
3287 SMB2_set_ea(const unsigned int xid
, struct cifs_tcon
*tcon
,
3288 u64 persistent_fid
, u64 volatile_fid
,
3289 struct smb2_file_full_ea_info
*buf
, int len
)
3291 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3292 current
->tgid
, FILE_FULL_EA_INFORMATION
, SMB2_O_INFO_FILE
,
3293 0, 1, (void **)&buf
, &len
);
3297 SMB2_oplock_break(const unsigned int xid
, struct cifs_tcon
*tcon
,
3298 const u64 persistent_fid
, const u64 volatile_fid
,
3302 struct smb2_oplock_break
*req
= NULL
;
3303 int flags
= CIFS_OBREAK_OP
;
3305 cifs_dbg(FYI
, "SMB2_oplock_break\n");
3306 rc
= small_smb2_init(SMB2_OPLOCK_BREAK
, tcon
, (void **) &req
);
3310 if (encryption_required(tcon
))
3311 flags
|= CIFS_TRANSFORM_REQ
;
3313 req
->VolatileFid
= volatile_fid
;
3314 req
->PersistentFid
= persistent_fid
;
3315 req
->OplockLevel
= oplock_level
;
3316 req
->hdr
.sync_hdr
.CreditRequest
= cpu_to_le16(1);
3318 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) req
, flags
);
3319 cifs_small_buf_release(req
);
3322 cifs_stats_fail_inc(tcon
, SMB2_OPLOCK_BREAK_HE
);
3323 cifs_dbg(FYI
, "Send error in Oplock Break = %d\n", rc
);
3330 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info
*pfs_inf
,
3331 struct kstatfs
*kst
)
3333 kst
->f_bsize
= le32_to_cpu(pfs_inf
->BytesPerSector
) *
3334 le32_to_cpu(pfs_inf
->SectorsPerAllocationUnit
);
3335 kst
->f_blocks
= le64_to_cpu(pfs_inf
->TotalAllocationUnits
);
3336 kst
->f_bfree
= kst
->f_bavail
=
3337 le64_to_cpu(pfs_inf
->CallerAvailableAllocationUnits
);
3342 build_qfs_info_req(struct kvec
*iov
, struct cifs_tcon
*tcon
, int level
,
3343 int outbuf_len
, u64 persistent_fid
, u64 volatile_fid
)
3346 struct smb2_query_info_req
*req
;
3348 cifs_dbg(FYI
, "Query FSInfo level %d\n", level
);
3350 if ((tcon
->ses
== NULL
) || (tcon
->ses
->server
== NULL
))
3353 rc
= small_smb2_init(SMB2_QUERY_INFO
, tcon
, (void **) &req
);
3357 req
->InfoType
= SMB2_O_INFO_FILESYSTEM
;
3358 req
->FileInfoClass
= level
;
3359 req
->PersistentFileId
= persistent_fid
;
3360 req
->VolatileFileId
= volatile_fid
;
3361 /* 4 for rfc1002 length field and 1 for pad */
3362 req
->InputBufferOffset
=
3363 cpu_to_le16(sizeof(struct smb2_query_info_req
) - 1 - 4);
3364 req
->OutputBufferLength
= cpu_to_le32(
3365 outbuf_len
+ sizeof(struct smb2_query_info_rsp
) - 1 - 4);
3367 iov
->iov_base
= (char *)req
;
3368 /* 4 for rfc1002 length field */
3369 iov
->iov_len
= get_rfc1002_length(req
) + 4;
3374 SMB2_QFS_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
3375 u64 persistent_fid
, u64 volatile_fid
, struct kstatfs
*fsdata
)
3377 struct smb2_query_info_rsp
*rsp
= NULL
;
3379 struct kvec rsp_iov
;
3382 struct cifs_ses
*ses
= tcon
->ses
;
3383 struct smb2_fs_full_size_info
*info
= NULL
;
3386 rc
= build_qfs_info_req(&iov
, tcon
, FS_FULL_SIZE_INFORMATION
,
3387 sizeof(struct smb2_fs_full_size_info
),
3388 persistent_fid
, volatile_fid
);
3392 if (encryption_required(tcon
))
3393 flags
|= CIFS_TRANSFORM_REQ
;
3395 rc
= SendReceive2(xid
, ses
, &iov
, 1, &resp_buftype
, flags
, &rsp_iov
);
3396 cifs_small_buf_release(iov
.iov_base
);
3398 cifs_stats_fail_inc(tcon
, SMB2_QUERY_INFO_HE
);
3401 rsp
= (struct smb2_query_info_rsp
*)rsp_iov
.iov_base
;
3403 info
= (struct smb2_fs_full_size_info
*)(4 /* RFC1001 len */ +
3404 le16_to_cpu(rsp
->OutputBufferOffset
) + (char *)&rsp
->hdr
);
3405 rc
= validate_buf(le16_to_cpu(rsp
->OutputBufferOffset
),
3406 le32_to_cpu(rsp
->OutputBufferLength
), &rsp
->hdr
,
3407 sizeof(struct smb2_fs_full_size_info
));
3409 copy_fs_info_to_kstatfs(info
, fsdata
);
3412 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
3417 SMB2_QFS_attr(const unsigned int xid
, struct cifs_tcon
*tcon
,
3418 u64 persistent_fid
, u64 volatile_fid
, int level
)
3420 struct smb2_query_info_rsp
*rsp
= NULL
;
3422 struct kvec rsp_iov
;
3424 int resp_buftype
, max_len
, min_len
;
3425 struct cifs_ses
*ses
= tcon
->ses
;
3426 unsigned int rsp_len
, offset
;
3429 if (level
== FS_DEVICE_INFORMATION
) {
3430 max_len
= sizeof(FILE_SYSTEM_DEVICE_INFO
);
3431 min_len
= sizeof(FILE_SYSTEM_DEVICE_INFO
);
3432 } else if (level
== FS_ATTRIBUTE_INFORMATION
) {
3433 max_len
= sizeof(FILE_SYSTEM_ATTRIBUTE_INFO
);
3434 min_len
= MIN_FS_ATTR_INFO_SIZE
;
3435 } else if (level
== FS_SECTOR_SIZE_INFORMATION
) {
3436 max_len
= sizeof(struct smb3_fs_ss_info
);
3437 min_len
= sizeof(struct smb3_fs_ss_info
);
3439 cifs_dbg(FYI
, "Invalid qfsinfo level %d\n", level
);
3443 rc
= build_qfs_info_req(&iov
, tcon
, level
, max_len
,
3444 persistent_fid
, volatile_fid
);
3448 if (encryption_required(tcon
))
3449 flags
|= CIFS_TRANSFORM_REQ
;
3451 rc
= SendReceive2(xid
, ses
, &iov
, 1, &resp_buftype
, flags
, &rsp_iov
);
3452 cifs_small_buf_release(iov
.iov_base
);
3454 cifs_stats_fail_inc(tcon
, SMB2_QUERY_INFO_HE
);
3457 rsp
= (struct smb2_query_info_rsp
*)rsp_iov
.iov_base
;
3459 rsp_len
= le32_to_cpu(rsp
->OutputBufferLength
);
3460 offset
= le16_to_cpu(rsp
->OutputBufferOffset
);
3461 rc
= validate_buf(offset
, rsp_len
, &rsp
->hdr
, min_len
);
3465 if (level
== FS_ATTRIBUTE_INFORMATION
)
3466 memcpy(&tcon
->fsAttrInfo
, 4 /* RFC1001 len */ + offset
3467 + (char *)&rsp
->hdr
, min_t(unsigned int,
3469 else if (level
== FS_DEVICE_INFORMATION
)
3470 memcpy(&tcon
->fsDevInfo
, 4 /* RFC1001 len */ + offset
3471 + (char *)&rsp
->hdr
, sizeof(FILE_SYSTEM_DEVICE_INFO
));
3472 else if (level
== FS_SECTOR_SIZE_INFORMATION
) {
3473 struct smb3_fs_ss_info
*ss_info
= (struct smb3_fs_ss_info
*)
3474 (4 /* RFC1001 len */ + offset
+ (char *)&rsp
->hdr
);
3475 tcon
->ss_flags
= le32_to_cpu(ss_info
->Flags
);
3476 tcon
->perf_sector_size
=
3477 le32_to_cpu(ss_info
->PhysicalBytesPerSectorForPerf
);
3481 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
3486 smb2_lockv(const unsigned int xid
, struct cifs_tcon
*tcon
,
3487 const __u64 persist_fid
, const __u64 volatile_fid
, const __u32 pid
,
3488 const __u32 num_lock
, struct smb2_lock_element
*buf
)
3491 struct smb2_lock_req
*req
= NULL
;
3493 struct kvec rsp_iov
;
3496 int flags
= CIFS_NO_RESP
;
3498 cifs_dbg(FYI
, "smb2_lockv num lock %d\n", num_lock
);
3500 rc
= small_smb2_init(SMB2_LOCK
, tcon
, (void **) &req
);
3504 if (encryption_required(tcon
))
3505 flags
|= CIFS_TRANSFORM_REQ
;
3507 req
->hdr
.sync_hdr
.ProcessId
= cpu_to_le32(pid
);
3508 req
->LockCount
= cpu_to_le16(num_lock
);
3510 req
->PersistentFileId
= persist_fid
;
3511 req
->VolatileFileId
= volatile_fid
;
3513 count
= num_lock
* sizeof(struct smb2_lock_element
);
3514 inc_rfc1001_len(req
, count
- sizeof(struct smb2_lock_element
));
3516 iov
[0].iov_base
= (char *)req
;
3517 /* 4 for rfc1002 length field and count for all locks */
3518 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - count
;
3519 iov
[1].iov_base
= (char *)buf
;
3520 iov
[1].iov_len
= count
;
3522 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_locks
);
3523 rc
= SendReceive2(xid
, tcon
->ses
, iov
, 2, &resp_buf_type
, flags
,
3525 cifs_small_buf_release(req
);
3527 cifs_dbg(FYI
, "Send error in smb2_lockv = %d\n", rc
);
3528 cifs_stats_fail_inc(tcon
, SMB2_LOCK_HE
);
3535 SMB2_lock(const unsigned int xid
, struct cifs_tcon
*tcon
,
3536 const __u64 persist_fid
, const __u64 volatile_fid
, const __u32 pid
,
3537 const __u64 length
, const __u64 offset
, const __u32 lock_flags
,
3540 struct smb2_lock_element lock
;
3542 lock
.Offset
= cpu_to_le64(offset
);
3543 lock
.Length
= cpu_to_le64(length
);
3544 lock
.Flags
= cpu_to_le32(lock_flags
);
3545 if (!wait
&& lock_flags
!= SMB2_LOCKFLAG_UNLOCK
)
3546 lock
.Flags
|= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY
);
3548 return smb2_lockv(xid
, tcon
, persist_fid
, volatile_fid
, pid
, 1, &lock
);
3552 SMB2_lease_break(const unsigned int xid
, struct cifs_tcon
*tcon
,
3553 __u8
*lease_key
, const __le32 lease_state
)
3556 struct smb2_lease_ack
*req
= NULL
;
3557 int flags
= CIFS_OBREAK_OP
;
3559 cifs_dbg(FYI
, "SMB2_lease_break\n");
3560 rc
= small_smb2_init(SMB2_OPLOCK_BREAK
, tcon
, (void **) &req
);
3564 if (encryption_required(tcon
))
3565 flags
|= CIFS_TRANSFORM_REQ
;
3567 req
->hdr
.sync_hdr
.CreditRequest
= cpu_to_le16(1);
3568 req
->StructureSize
= cpu_to_le16(36);
3569 inc_rfc1001_len(req
, 12);
3571 memcpy(req
->LeaseKey
, lease_key
, 16);
3572 req
->LeaseState
= lease_state
;
3574 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) req
, flags
);
3575 cifs_small_buf_release(req
);
3578 cifs_stats_fail_inc(tcon
, SMB2_OPLOCK_BREAK_HE
);
3579 cifs_dbg(FYI
, "Send error in Lease Break = %d\n", rc
);