4 * Copyright (C) International Business Machines Corp., 2009, 2013
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
9 * Contains the routines for constructing the SMB2 PDUs themselves
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
32 #include <linux/kernel.h>
33 #include <linux/vfs.h>
34 #include <linux/task_io_accounting_ops.h>
35 #include <linux/uaccess.h>
36 #include <linux/pagemap.h>
37 #include <linux/xattr.h>
41 #include "cifsproto.h"
42 #include "smb2proto.h"
43 #include "cifs_unicode.h"
44 #include "cifs_debug.h"
46 #include "smb2status.h"
49 #include "cifs_spnego.h"
52 * The following table defines the expected "StructureSize" of SMB2 requests
53 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
55 * Note that commands are defined in smb2pdu.h in le16 but the array below is
56 * indexed by command in host byte order.
58 static const int smb2_req_struct_sizes
[NUMBER_OF_SMB2_COMMANDS
] = {
59 /* SMB2_NEGOTIATE */ 36,
60 /* SMB2_SESSION_SETUP */ 25,
62 /* SMB2_TREE_CONNECT */ 9,
63 /* SMB2_TREE_DISCONNECT */ 4,
73 /* SMB2_QUERY_DIRECTORY */ 33,
74 /* SMB2_CHANGE_NOTIFY */ 32,
75 /* SMB2_QUERY_INFO */ 41,
76 /* SMB2_SET_INFO */ 33,
77 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
82 smb2_hdr_assemble(struct smb2_sync_hdr
*shdr
, __le16 smb2_cmd
,
83 const struct cifs_tcon
*tcon
)
85 shdr
->ProtocolId
= SMB2_PROTO_NUMBER
;
86 shdr
->StructureSize
= cpu_to_le16(64);
87 shdr
->Command
= smb2_cmd
;
88 if (tcon
&& tcon
->ses
&& tcon
->ses
->server
) {
89 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
91 spin_lock(&server
->req_lock
);
92 /* Request up to 2 credits but don't go over the limit. */
93 if (server
->credits
>= server
->max_credits
)
94 shdr
->CreditRequest
= cpu_to_le16(0);
96 shdr
->CreditRequest
= cpu_to_le16(
97 min_t(int, server
->max_credits
-
99 spin_unlock(&server
->req_lock
);
101 shdr
->CreditRequest
= cpu_to_le16(2);
103 shdr
->ProcessId
= cpu_to_le32((__u16
)current
->tgid
);
108 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
109 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
110 if ((tcon
->ses
) && (tcon
->ses
->server
) &&
111 (tcon
->ses
->server
->capabilities
& SMB2_GLOBAL_CAP_LARGE_MTU
))
112 shdr
->CreditCharge
= cpu_to_le16(1);
113 /* else CreditCharge MBZ */
115 shdr
->TreeId
= tcon
->tid
;
116 /* Uid is not converted */
118 shdr
->SessionId
= tcon
->ses
->Suid
;
121 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
122 * to pass the path on the Open SMB prefixed by \\server\share.
123 * Not sure when we would need to do the augmented path (if ever) and
124 * setting this flag breaks the SMB2 open operation since it is
125 * illegal to send an empty path name (without \\server\share prefix)
126 * when the DFS flag is set in the SMB open header. We could
127 * consider setting the flag on all operations other than open
128 * but it is safer to net set it for now.
130 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
131 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
133 if (tcon
->ses
&& tcon
->ses
->server
&& tcon
->ses
->server
->sign
)
134 shdr
->Flags
|= SMB2_FLAGS_SIGNED
;
140 smb2_reconnect(__le16 smb2_command
, struct cifs_tcon
*tcon
)
143 struct nls_table
*nls_codepage
;
144 struct cifs_ses
*ses
;
145 struct TCP_Server_Info
*server
;
148 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
149 * check for tcp and smb session status done differently
150 * for those three - in the calling routine.
155 if (smb2_command
== SMB2_TREE_CONNECT
)
158 if (tcon
->tidStatus
== CifsExiting
) {
160 * only tree disconnect, open, and write,
161 * (and ulogoff which does not have tcon)
162 * are allowed as we start force umount.
164 if ((smb2_command
!= SMB2_WRITE
) &&
165 (smb2_command
!= SMB2_CREATE
) &&
166 (smb2_command
!= SMB2_TREE_DISCONNECT
)) {
167 cifs_dbg(FYI
, "can not send cmd %d while umounting\n",
172 if ((!tcon
->ses
) || (tcon
->ses
->status
== CifsExiting
) ||
173 (!tcon
->ses
->server
))
177 server
= ses
->server
;
180 * Give demultiplex thread up to 10 seconds to reconnect, should be
181 * greater than cifs socket timeout which is 7 seconds
183 while (server
->tcpStatus
== CifsNeedReconnect
) {
185 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
186 * here since they are implicitly done when session drops.
188 switch (smb2_command
) {
190 * BB Should we keep oplock break and add flush to exceptions?
192 case SMB2_TREE_DISCONNECT
:
195 case SMB2_OPLOCK_BREAK
:
199 wait_event_interruptible_timeout(server
->response_q
,
200 (server
->tcpStatus
!= CifsNeedReconnect
), 10 * HZ
);
202 /* are we still trying to reconnect? */
203 if (server
->tcpStatus
!= CifsNeedReconnect
)
207 * on "soft" mounts we wait once. Hard mounts keep
208 * retrying until process is killed or server comes
212 cifs_dbg(FYI
, "gave up waiting on reconnect in smb_init\n");
217 if (!tcon
->ses
->need_reconnect
&& !tcon
->need_reconnect
)
220 nls_codepage
= load_nls_default();
223 * need to prevent multiple threads trying to simultaneously reconnect
224 * the same SMB session
226 mutex_lock(&tcon
->ses
->session_mutex
);
227 rc
= cifs_negotiate_protocol(0, tcon
->ses
);
228 if (!rc
&& tcon
->ses
->need_reconnect
)
229 rc
= cifs_setup_session(0, tcon
->ses
, nls_codepage
);
231 if (rc
|| !tcon
->need_reconnect
) {
232 mutex_unlock(&tcon
->ses
->session_mutex
);
236 cifs_mark_open_files_invalid(tcon
);
237 if (tcon
->use_persistent
)
238 tcon
->need_reopen_files
= true;
240 rc
= SMB2_tcon(0, tcon
->ses
, tcon
->treeName
, tcon
, nls_codepage
);
241 mutex_unlock(&tcon
->ses
->session_mutex
);
243 cifs_dbg(FYI
, "reconnect tcon rc = %d\n", rc
);
247 if (smb2_command
!= SMB2_INTERNAL_CMD
)
248 queue_delayed_work(cifsiod_wq
, &server
->reconnect
, 0);
250 atomic_inc(&tconInfoReconnectCount
);
253 * Check if handle based operation so we know whether we can continue
254 * or not without returning to caller to reset file handle.
257 * BB Is flush done by server on drop of tcp session? Should we special
258 * case it and skip above?
260 switch (smb2_command
) {
266 case SMB2_QUERY_DIRECTORY
:
267 case SMB2_CHANGE_NOTIFY
:
268 case SMB2_QUERY_INFO
:
272 unload_nls(nls_codepage
);
277 fill_small_buf(__le16 smb2_command
, struct cifs_tcon
*tcon
, void *buf
,
278 unsigned int *total_len
)
280 struct smb2_sync_pdu
*spdu
= (struct smb2_sync_pdu
*)buf
;
281 /* lookup word count ie StructureSize from table */
282 __u16 parmsize
= smb2_req_struct_sizes
[le16_to_cpu(smb2_command
)];
285 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
286 * largest operations (Create)
290 smb2_hdr_assemble(&spdu
->sync_hdr
, smb2_command
, tcon
);
291 spdu
->StructureSize2
= cpu_to_le16(parmsize
);
293 *total_len
= parmsize
+ sizeof(struct smb2_sync_hdr
);
296 /* init request without RFC1001 length at the beginning */
298 smb2_plain_req_init(__le16 smb2_command
, struct cifs_tcon
*tcon
,
299 void **request_buf
, unsigned int *total_len
)
302 struct smb2_sync_hdr
*shdr
;
304 rc
= smb2_reconnect(smb2_command
, tcon
);
308 /* BB eventually switch this to SMB2 specific small buf size */
309 *request_buf
= cifs_small_buf_get();
310 if (*request_buf
== NULL
) {
311 /* BB should we add a retry in here if not a writepage? */
315 shdr
= (struct smb2_sync_hdr
*)(*request_buf
);
317 fill_small_buf(smb2_command
, tcon
, shdr
, total_len
);
320 #ifdef CONFIG_CIFS_STATS2
321 uint16_t com_code
= le16_to_cpu(smb2_command
);
323 cifs_stats_inc(&tcon
->stats
.smb2_stats
.smb2_com_sent
[com_code
]);
325 cifs_stats_inc(&tcon
->num_smbs_sent
);
332 * Allocate and return pointer to an SMB request hdr, and set basic
333 * SMB information in the SMB header. If the return code is zero, this
334 * function must have filled in request_buf pointer. The returned buffer
335 * has RFC1001 length at the beginning.
338 small_smb2_init(__le16 smb2_command
, struct cifs_tcon
*tcon
,
342 unsigned int total_len
;
343 struct smb2_pdu
*pdu
;
345 rc
= smb2_reconnect(smb2_command
, tcon
);
349 /* BB eventually switch this to SMB2 specific small buf size */
350 *request_buf
= cifs_small_buf_get();
351 if (*request_buf
== NULL
) {
352 /* BB should we add a retry in here if not a writepage? */
356 pdu
= (struct smb2_pdu
*)(*request_buf
);
358 fill_small_buf(smb2_command
, tcon
, get_sync_hdr(pdu
), &total_len
);
360 /* Note this is only network field converted to big endian */
361 pdu
->hdr
.smb2_buf_length
= cpu_to_be32(total_len
);
364 #ifdef CONFIG_CIFS_STATS2
365 uint16_t com_code
= le16_to_cpu(smb2_command
);
366 cifs_stats_inc(&tcon
->stats
.smb2_stats
.smb2_com_sent
[com_code
]);
368 cifs_stats_inc(&tcon
->num_smbs_sent
);
374 #ifdef CONFIG_CIFS_SMB311
375 /* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
376 #define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
379 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
380 #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
383 build_preauth_ctxt(struct smb2_preauth_neg_context
*pneg_ctxt
)
385 pneg_ctxt
->ContextType
= SMB2_PREAUTH_INTEGRITY_CAPABILITIES
;
386 pneg_ctxt
->DataLength
= cpu_to_le16(38);
387 pneg_ctxt
->HashAlgorithmCount
= cpu_to_le16(1);
388 pneg_ctxt
->SaltLength
= cpu_to_le16(SMB311_SALT_SIZE
);
389 get_random_bytes(pneg_ctxt
->Salt
, SMB311_SALT_SIZE
);
390 pneg_ctxt
->HashAlgorithms
= SMB2_PREAUTH_INTEGRITY_SHA512
;
394 build_encrypt_ctxt(struct smb2_encryption_neg_context
*pneg_ctxt
)
396 pneg_ctxt
->ContextType
= SMB2_ENCRYPTION_CAPABILITIES
;
397 pneg_ctxt
->DataLength
= cpu_to_le16(6);
398 pneg_ctxt
->CipherCount
= cpu_to_le16(2);
399 pneg_ctxt
->Ciphers
[0] = SMB2_ENCRYPTION_AES128_GCM
;
400 pneg_ctxt
->Ciphers
[1] = SMB2_ENCRYPTION_AES128_CCM
;
404 assemble_neg_contexts(struct smb2_negotiate_req
*req
)
407 /* +4 is to account for the RFC1001 len field */
408 char *pneg_ctxt
= (char *)req
+ OFFSET_OF_NEG_CONTEXT
+ 4;
410 build_preauth_ctxt((struct smb2_preauth_neg_context
*)pneg_ctxt
);
411 /* Add 2 to size to round to 8 byte boundary */
412 pneg_ctxt
+= 2 + sizeof(struct smb2_preauth_neg_context
);
413 build_encrypt_ctxt((struct smb2_encryption_neg_context
*)pneg_ctxt
);
414 req
->NegotiateContextOffset
= cpu_to_le32(OFFSET_OF_NEG_CONTEXT
);
415 req
->NegotiateContextCount
= cpu_to_le16(2);
416 inc_rfc1001_len(req
, 4 + sizeof(struct smb2_preauth_neg_context
) + 2
417 + sizeof(struct smb2_encryption_neg_context
)); /* calculate hash */
420 static void assemble_neg_contexts(struct smb2_negotiate_req
*req
)
429 * SMB2 Worker functions follow:
431 * The general structure of the worker functions is:
432 * 1) Call smb2_init (assembles SMB2 header)
433 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
434 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
435 * 4) Decode SMB2 command specific fields in the fixed length area
436 * 5) Decode variable length data area (if any for this SMB2 command type)
437 * 6) Call free smb buffer
443 SMB2_negotiate(const unsigned int xid
, struct cifs_ses
*ses
)
445 struct smb2_negotiate_req
*req
;
446 struct smb2_negotiate_rsp
*rsp
;
451 struct TCP_Server_Info
*server
= ses
->server
;
452 int blob_offset
, blob_length
;
454 int flags
= CIFS_NEG_OP
;
456 cifs_dbg(FYI
, "Negotiate protocol\n");
459 WARN(1, "%s: server is NULL!\n", __func__
);
463 rc
= small_smb2_init(SMB2_NEGOTIATE
, NULL
, (void **) &req
);
467 req
->hdr
.sync_hdr
.SessionId
= 0;
469 req
->Dialects
[0] = cpu_to_le16(ses
->server
->vals
->protocol_id
);
471 req
->DialectCount
= cpu_to_le16(1); /* One vers= at a time for now */
472 inc_rfc1001_len(req
, 2);
474 /* only one of SMB2 signing flags may be set in SMB2 request */
476 req
->SecurityMode
= cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED
);
477 else if (global_secflags
& CIFSSEC_MAY_SIGN
)
478 req
->SecurityMode
= cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED
);
480 req
->SecurityMode
= 0;
482 req
->Capabilities
= cpu_to_le32(ses
->server
->vals
->req_capabilities
);
484 /* ClientGUID must be zero for SMB2.02 dialect */
485 if (ses
->server
->vals
->protocol_id
== SMB20_PROT_ID
)
486 memset(req
->ClientGUID
, 0, SMB2_CLIENT_GUID_SIZE
);
488 memcpy(req
->ClientGUID
, server
->client_guid
,
489 SMB2_CLIENT_GUID_SIZE
);
490 if (ses
->server
->vals
->protocol_id
== SMB311_PROT_ID
)
491 assemble_neg_contexts(req
);
493 iov
[0].iov_base
= (char *)req
;
494 /* 4 for rfc1002 length field */
495 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
497 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buftype
, flags
, &rsp_iov
);
498 cifs_small_buf_release(req
);
499 rsp
= (struct smb2_negotiate_rsp
*)rsp_iov
.iov_base
;
501 * No tcon so can't do
502 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
507 cifs_dbg(FYI
, "mode 0x%x\n", rsp
->SecurityMode
);
509 /* BB we may eventually want to match the negotiated vs. requested
510 dialect, even though we are only requesting one at a time */
511 if (rsp
->DialectRevision
== cpu_to_le16(SMB20_PROT_ID
))
512 cifs_dbg(FYI
, "negotiated smb2.0 dialect\n");
513 else if (rsp
->DialectRevision
== cpu_to_le16(SMB21_PROT_ID
))
514 cifs_dbg(FYI
, "negotiated smb2.1 dialect\n");
515 else if (rsp
->DialectRevision
== cpu_to_le16(SMB30_PROT_ID
))
516 cifs_dbg(FYI
, "negotiated smb3.0 dialect\n");
517 else if (rsp
->DialectRevision
== cpu_to_le16(SMB302_PROT_ID
))
518 cifs_dbg(FYI
, "negotiated smb3.02 dialect\n");
519 #ifdef CONFIG_CIFS_SMB311
520 else if (rsp
->DialectRevision
== cpu_to_le16(SMB311_PROT_ID
))
521 cifs_dbg(FYI
, "negotiated smb3.1.1 dialect\n");
524 cifs_dbg(VFS
, "Illegal dialect returned by server 0x%x\n",
525 le16_to_cpu(rsp
->DialectRevision
));
529 server
->dialect
= le16_to_cpu(rsp
->DialectRevision
);
531 /* SMB2 only has an extended negflavor */
532 server
->negflavor
= CIFS_NEGFLAVOR_EXTENDED
;
533 /* set it to the maximum buffer size value we can send with 1 credit */
534 server
->maxBuf
= min_t(unsigned int, le32_to_cpu(rsp
->MaxTransactSize
),
535 SMB2_MAX_BUFFER_SIZE
);
536 server
->max_read
= le32_to_cpu(rsp
->MaxReadSize
);
537 server
->max_write
= le32_to_cpu(rsp
->MaxWriteSize
);
538 /* BB Do we need to validate the SecurityMode? */
539 server
->sec_mode
= le16_to_cpu(rsp
->SecurityMode
);
540 server
->capabilities
= le32_to_cpu(rsp
->Capabilities
);
542 server
->capabilities
|= SMB2_NT_FIND
| SMB2_LARGE_FILES
;
544 security_blob
= smb2_get_data_area_len(&blob_offset
, &blob_length
,
547 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
549 * ses->sectype = RawNTLMSSP;
550 * but for time being this is our only auth choice so doesn't matter.
551 * We just found a server which sets blob length to zero expecting raw.
553 if (blob_length
== 0)
554 cifs_dbg(FYI
, "missing security blob on negprot\n");
556 rc
= cifs_enable_signing(server
, ses
->sign
);
560 rc
= decode_negTokenInit(security_blob
, blob_length
, server
);
567 free_rsp_buf(resp_buftype
, rsp
);
571 int smb3_validate_negotiate(const unsigned int xid
, struct cifs_tcon
*tcon
)
574 struct validate_negotiate_info_req vneg_inbuf
;
575 struct validate_negotiate_info_rsp
*pneg_rsp
;
578 cifs_dbg(FYI
, "validate negotiate\n");
581 * validation ioctl must be signed, so no point sending this if we
582 * can not sign it. We could eventually change this to selectively
583 * sign just this, the first and only signed request on a connection.
584 * This is good enough for now since a user who wants better security
585 * would also enable signing on the mount. Having validation of
586 * negotiate info for signed connections helps reduce attack vectors
588 if (tcon
->ses
->server
->sign
== false)
589 return 0; /* validation requires signing */
591 vneg_inbuf
.Capabilities
=
592 cpu_to_le32(tcon
->ses
->server
->vals
->req_capabilities
);
593 memcpy(vneg_inbuf
.Guid
, tcon
->ses
->server
->client_guid
,
594 SMB2_CLIENT_GUID_SIZE
);
597 vneg_inbuf
.SecurityMode
=
598 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED
);
599 else if (global_secflags
& CIFSSEC_MAY_SIGN
)
600 vneg_inbuf
.SecurityMode
=
601 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED
);
603 vneg_inbuf
.SecurityMode
= 0;
605 vneg_inbuf
.DialectCount
= cpu_to_le16(1);
606 vneg_inbuf
.Dialects
[0] =
607 cpu_to_le16(tcon
->ses
->server
->vals
->protocol_id
);
609 rc
= SMB2_ioctl(xid
, tcon
, NO_FILE_ID
, NO_FILE_ID
,
610 FSCTL_VALIDATE_NEGOTIATE_INFO
, true /* is_fsctl */,
611 (char *)&vneg_inbuf
, sizeof(struct validate_negotiate_info_req
),
612 (char **)&pneg_rsp
, &rsplen
);
615 cifs_dbg(VFS
, "validate protocol negotiate failed: %d\n", rc
);
619 if (rsplen
!= sizeof(struct validate_negotiate_info_rsp
)) {
620 cifs_dbg(VFS
, "invalid size of protocol negotiate response\n");
624 /* check validate negotiate info response matches what we got earlier */
625 if (pneg_rsp
->Dialect
!=
626 cpu_to_le16(tcon
->ses
->server
->vals
->protocol_id
))
629 if (pneg_rsp
->SecurityMode
!= cpu_to_le16(tcon
->ses
->server
->sec_mode
))
632 /* do not validate server guid because not saved at negprot time yet */
634 if ((le32_to_cpu(pneg_rsp
->Capabilities
) | SMB2_NT_FIND
|
635 SMB2_LARGE_FILES
) != tcon
->ses
->server
->capabilities
)
638 /* validate negotiate successful */
639 cifs_dbg(FYI
, "validate negotiate info successful\n");
643 cifs_dbg(VFS
, "protocol revalidation - security settings mismatch\n");
647 struct SMB2_sess_data
{
649 struct cifs_ses
*ses
;
650 struct nls_table
*nls_cp
;
651 void (*func
)(struct SMB2_sess_data
*);
653 u64 previous_session
;
655 /* we will send the SMB in three pieces:
656 * a fixed length beginning part, an optional
657 * SPNEGO blob (which can be zero length), and a
658 * last part which will include the strings
659 * and rest of bcc area. This allows us to avoid
660 * a large buffer 17K allocation
667 SMB2_sess_alloc_buffer(struct SMB2_sess_data
*sess_data
)
670 struct cifs_ses
*ses
= sess_data
->ses
;
671 struct smb2_sess_setup_req
*req
;
672 struct TCP_Server_Info
*server
= ses
->server
;
674 rc
= small_smb2_init(SMB2_SESSION_SETUP
, NULL
, (void **) &req
);
678 /* First session, not a reauthenticate */
679 req
->hdr
.sync_hdr
.SessionId
= 0;
681 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
682 req
->PreviousSessionId
= sess_data
->previous_session
;
684 req
->Flags
= 0; /* MBZ */
685 /* to enable echos and oplocks */
686 req
->hdr
.sync_hdr
.CreditRequest
= cpu_to_le16(3);
688 /* only one of SMB2 signing flags may be set in SMB2 request */
690 req
->SecurityMode
= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
691 else if (global_secflags
& CIFSSEC_MAY_SIGN
) /* one flag unlike MUST_ */
692 req
->SecurityMode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
694 req
->SecurityMode
= 0;
696 req
->Capabilities
= 0;
697 req
->Channel
= 0; /* MBZ */
699 sess_data
->iov
[0].iov_base
= (char *)req
;
700 /* 4 for rfc1002 length field and 1 for pad */
701 sess_data
->iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
703 * This variable will be used to clear the buffer
704 * allocated above in case of any error in the calling function.
706 sess_data
->buf0_type
= CIFS_SMALL_BUFFER
;
712 SMB2_sess_free_buffer(struct SMB2_sess_data
*sess_data
)
714 free_rsp_buf(sess_data
->buf0_type
, sess_data
->iov
[0].iov_base
);
715 sess_data
->buf0_type
= CIFS_NO_BUFFER
;
719 SMB2_sess_sendreceive(struct SMB2_sess_data
*sess_data
)
722 struct smb2_sess_setup_req
*req
= sess_data
->iov
[0].iov_base
;
723 struct kvec rsp_iov
= { NULL
, 0 };
725 /* Testing shows that buffer offset must be at location of Buffer[0] */
726 req
->SecurityBufferOffset
=
727 cpu_to_le16(sizeof(struct smb2_sess_setup_req
) -
728 1 /* pad */ - 4 /* rfc1001 len */);
729 req
->SecurityBufferLength
= cpu_to_le16(sess_data
->iov
[1].iov_len
);
731 inc_rfc1001_len(req
, sess_data
->iov
[1].iov_len
- 1 /* pad */);
733 /* BB add code to build os and lm fields */
735 rc
= SendReceive2(sess_data
->xid
, sess_data
->ses
,
737 &sess_data
->buf0_type
,
738 CIFS_LOG_ERROR
| CIFS_NEG_OP
, &rsp_iov
);
739 cifs_small_buf_release(sess_data
->iov
[0].iov_base
);
740 memcpy(&sess_data
->iov
[0], &rsp_iov
, sizeof(struct kvec
));
746 SMB2_sess_establish_session(struct SMB2_sess_data
*sess_data
)
749 struct cifs_ses
*ses
= sess_data
->ses
;
751 mutex_lock(&ses
->server
->srv_mutex
);
752 if (ses
->server
->sign
&& ses
->server
->ops
->generate_signingkey
) {
753 rc
= ses
->server
->ops
->generate_signingkey(ses
);
754 kfree(ses
->auth_key
.response
);
755 ses
->auth_key
.response
= NULL
;
758 "SMB3 session key generation failed\n");
759 mutex_unlock(&ses
->server
->srv_mutex
);
763 if (!ses
->server
->session_estab
) {
764 ses
->server
->sequence_number
= 0x2;
765 ses
->server
->session_estab
= true;
767 mutex_unlock(&ses
->server
->srv_mutex
);
769 cifs_dbg(FYI
, "SMB2/3 session established successfully\n");
770 spin_lock(&GlobalMid_Lock
);
771 ses
->status
= CifsGood
;
772 ses
->need_reconnect
= false;
773 spin_unlock(&GlobalMid_Lock
);
776 if (!ses
->server
->sign
) {
777 kfree(ses
->auth_key
.response
);
778 ses
->auth_key
.response
= NULL
;
783 #ifdef CONFIG_CIFS_UPCALL
785 SMB2_auth_kerberos(struct SMB2_sess_data
*sess_data
)
788 struct cifs_ses
*ses
= sess_data
->ses
;
789 struct cifs_spnego_msg
*msg
;
790 struct key
*spnego_key
= NULL
;
791 struct smb2_sess_setup_rsp
*rsp
= NULL
;
793 rc
= SMB2_sess_alloc_buffer(sess_data
);
797 spnego_key
= cifs_get_spnego_key(ses
);
798 if (IS_ERR(spnego_key
)) {
799 rc
= PTR_ERR(spnego_key
);
804 msg
= spnego_key
->payload
.data
[0];
806 * check version field to make sure that cifs.upcall is
807 * sending us a response in an expected form
809 if (msg
->version
!= CIFS_SPNEGO_UPCALL_VERSION
) {
811 "bad cifs.upcall version. Expected %d got %d",
812 CIFS_SPNEGO_UPCALL_VERSION
, msg
->version
);
814 goto out_put_spnego_key
;
817 ses
->auth_key
.response
= kmemdup(msg
->data
, msg
->sesskey_len
,
819 if (!ses
->auth_key
.response
) {
821 "Kerberos can't allocate (%u bytes) memory",
824 goto out_put_spnego_key
;
826 ses
->auth_key
.len
= msg
->sesskey_len
;
828 sess_data
->iov
[1].iov_base
= msg
->data
+ msg
->sesskey_len
;
829 sess_data
->iov
[1].iov_len
= msg
->secblob_len
;
831 rc
= SMB2_sess_sendreceive(sess_data
);
833 goto out_put_spnego_key
;
835 rsp
= (struct smb2_sess_setup_rsp
*)sess_data
->iov
[0].iov_base
;
836 ses
->Suid
= rsp
->hdr
.sync_hdr
.SessionId
;
838 ses
->session_flags
= le16_to_cpu(rsp
->SessionFlags
);
839 if (ses
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
)
840 cifs_dbg(VFS
, "SMB3 encryption not supported yet\n");
842 rc
= SMB2_sess_establish_session(sess_data
);
844 key_invalidate(spnego_key
);
847 sess_data
->result
= rc
;
848 sess_data
->func
= NULL
;
849 SMB2_sess_free_buffer(sess_data
);
853 SMB2_auth_kerberos(struct SMB2_sess_data
*sess_data
)
855 cifs_dbg(VFS
, "Kerberos negotiated but upcall support disabled!\n");
856 sess_data
->result
= -EOPNOTSUPP
;
857 sess_data
->func
= NULL
;
862 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data
*sess_data
);
865 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data
*sess_data
)
868 struct cifs_ses
*ses
= sess_data
->ses
;
869 struct smb2_sess_setup_rsp
*rsp
= NULL
;
870 char *ntlmssp_blob
= NULL
;
871 bool use_spnego
= false; /* else use raw ntlmssp */
875 * If memory allocation is successful, caller of this function
878 ses
->ntlmssp
= kmalloc(sizeof(struct ntlmssp_auth
), GFP_KERNEL
);
883 ses
->ntlmssp
->sesskey_per_smbsess
= true;
885 rc
= SMB2_sess_alloc_buffer(sess_data
);
889 ntlmssp_blob
= kmalloc(sizeof(struct _NEGOTIATE_MESSAGE
),
891 if (ntlmssp_blob
== NULL
) {
896 build_ntlmssp_negotiate_blob(ntlmssp_blob
, ses
);
898 /* BB eventually need to add this */
899 cifs_dbg(VFS
, "spnego not supported for SMB2 yet\n");
903 blob_length
= sizeof(struct _NEGOTIATE_MESSAGE
);
904 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
906 sess_data
->iov
[1].iov_base
= ntlmssp_blob
;
907 sess_data
->iov
[1].iov_len
= blob_length
;
909 rc
= SMB2_sess_sendreceive(sess_data
);
910 rsp
= (struct smb2_sess_setup_rsp
*)sess_data
->iov
[0].iov_base
;
912 /* If true, rc here is expected and not an error */
913 if (sess_data
->buf0_type
!= CIFS_NO_BUFFER
&&
914 rsp
->hdr
.sync_hdr
.Status
== STATUS_MORE_PROCESSING_REQUIRED
)
920 if (offsetof(struct smb2_sess_setup_rsp
, Buffer
) - 4 !=
921 le16_to_cpu(rsp
->SecurityBufferOffset
)) {
922 cifs_dbg(VFS
, "Invalid security buffer offset %d\n",
923 le16_to_cpu(rsp
->SecurityBufferOffset
));
927 rc
= decode_ntlmssp_challenge(rsp
->Buffer
,
928 le16_to_cpu(rsp
->SecurityBufferLength
), ses
);
932 cifs_dbg(FYI
, "rawntlmssp session setup challenge phase\n");
935 ses
->Suid
= rsp
->hdr
.sync_hdr
.SessionId
;
936 ses
->session_flags
= le16_to_cpu(rsp
->SessionFlags
);
937 if (ses
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
)
938 cifs_dbg(VFS
, "SMB3 encryption not supported yet\n");
942 SMB2_sess_free_buffer(sess_data
);
944 sess_data
->result
= 0;
945 sess_data
->func
= SMB2_sess_auth_rawntlmssp_authenticate
;
951 sess_data
->result
= rc
;
952 sess_data
->func
= NULL
;
956 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data
*sess_data
)
959 struct cifs_ses
*ses
= sess_data
->ses
;
960 struct smb2_sess_setup_req
*req
;
961 struct smb2_sess_setup_rsp
*rsp
= NULL
;
962 unsigned char *ntlmssp_blob
= NULL
;
963 bool use_spnego
= false; /* else use raw ntlmssp */
966 rc
= SMB2_sess_alloc_buffer(sess_data
);
970 req
= (struct smb2_sess_setup_req
*) sess_data
->iov
[0].iov_base
;
971 req
->hdr
.sync_hdr
.SessionId
= ses
->Suid
;
973 rc
= build_ntlmssp_auth_blob(&ntlmssp_blob
, &blob_length
, ses
,
976 cifs_dbg(FYI
, "build_ntlmssp_auth_blob failed %d\n", rc
);
981 /* BB eventually need to add this */
982 cifs_dbg(VFS
, "spnego not supported for SMB2 yet\n");
986 sess_data
->iov
[1].iov_base
= ntlmssp_blob
;
987 sess_data
->iov
[1].iov_len
= blob_length
;
989 rc
= SMB2_sess_sendreceive(sess_data
);
993 rsp
= (struct smb2_sess_setup_rsp
*)sess_data
->iov
[0].iov_base
;
995 ses
->Suid
= rsp
->hdr
.sync_hdr
.SessionId
;
996 ses
->session_flags
= le16_to_cpu(rsp
->SessionFlags
);
997 if (ses
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
)
998 cifs_dbg(VFS
, "SMB3 encryption not supported yet\n");
1000 rc
= SMB2_sess_establish_session(sess_data
);
1002 kfree(ntlmssp_blob
);
1003 SMB2_sess_free_buffer(sess_data
);
1004 kfree(ses
->ntlmssp
);
1005 ses
->ntlmssp
= NULL
;
1006 sess_data
->result
= rc
;
1007 sess_data
->func
= NULL
;
1011 SMB2_select_sec(struct cifs_ses
*ses
, struct SMB2_sess_data
*sess_data
)
1013 if (ses
->sectype
!= Kerberos
&& ses
->sectype
!= RawNTLMSSP
)
1014 ses
->sectype
= RawNTLMSSP
;
1016 switch (ses
->sectype
) {
1018 sess_data
->func
= SMB2_auth_kerberos
;
1021 sess_data
->func
= SMB2_sess_auth_rawntlmssp_negotiate
;
1024 cifs_dbg(VFS
, "secType %d not supported!\n", ses
->sectype
);
1032 SMB2_sess_setup(const unsigned int xid
, struct cifs_ses
*ses
,
1033 const struct nls_table
*nls_cp
)
1036 struct TCP_Server_Info
*server
= ses
->server
;
1037 struct SMB2_sess_data
*sess_data
;
1039 cifs_dbg(FYI
, "Session Setup\n");
1042 WARN(1, "%s: server is NULL!\n", __func__
);
1046 sess_data
= kzalloc(sizeof(struct SMB2_sess_data
), GFP_KERNEL
);
1050 rc
= SMB2_select_sec(ses
, sess_data
);
1053 sess_data
->xid
= xid
;
1054 sess_data
->ses
= ses
;
1055 sess_data
->buf0_type
= CIFS_NO_BUFFER
;
1056 sess_data
->nls_cp
= (struct nls_table
*) nls_cp
;
1058 while (sess_data
->func
)
1059 sess_data
->func(sess_data
);
1061 rc
= sess_data
->result
;
1068 SMB2_logoff(const unsigned int xid
, struct cifs_ses
*ses
)
1070 struct smb2_logoff_req
*req
; /* response is also trivial struct */
1072 struct TCP_Server_Info
*server
;
1074 cifs_dbg(FYI
, "disconnect session %p\n", ses
);
1076 if (ses
&& (ses
->server
))
1077 server
= ses
->server
;
1081 /* no need to send SMB logoff if uid already closed due to reconnect */
1082 if (ses
->need_reconnect
)
1083 goto smb2_session_already_dead
;
1085 rc
= small_smb2_init(SMB2_LOGOFF
, NULL
, (void **) &req
);
1089 /* since no tcon, smb2_init can not do this, so do here */
1090 req
->hdr
.sync_hdr
.SessionId
= ses
->Suid
;
1092 req
->hdr
.sync_hdr
.Flags
|= SMB2_FLAGS_SIGNED
;
1094 rc
= SendReceiveNoRsp(xid
, ses
, (char *) req
, 0);
1095 cifs_small_buf_release(req
);
1097 * No tcon so can't do
1098 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1101 smb2_session_already_dead
:
1105 static inline void cifs_stats_fail_inc(struct cifs_tcon
*tcon
, uint16_t code
)
1107 cifs_stats_inc(&tcon
->stats
.smb2_stats
.smb2_com_failed
[code
]);
1110 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1112 /* These are similar values to what Windows uses */
1113 static inline void init_copy_chunk_defaults(struct cifs_tcon
*tcon
)
1115 tcon
->max_chunks
= 256;
1116 tcon
->max_bytes_chunk
= 1048576;
1117 tcon
->max_bytes_copy
= 16777216;
1121 SMB2_tcon(const unsigned int xid
, struct cifs_ses
*ses
, const char *tree
,
1122 struct cifs_tcon
*tcon
, const struct nls_table
*cp
)
1124 struct smb2_tree_connect_req
*req
;
1125 struct smb2_tree_connect_rsp
*rsp
= NULL
;
1127 struct kvec rsp_iov
;
1131 struct TCP_Server_Info
*server
;
1132 __le16
*unc_path
= NULL
;
1134 cifs_dbg(FYI
, "TCON\n");
1136 if ((ses
->server
) && tree
)
1137 server
= ses
->server
;
1141 if (tcon
&& tcon
->bad_network_name
)
1144 if ((tcon
&& tcon
->seal
) &&
1145 ((ses
->server
->capabilities
& SMB2_GLOBAL_CAP_ENCRYPTION
) == 0)) {
1146 cifs_dbg(VFS
, "encryption requested but no server support");
1150 unc_path
= kmalloc(MAX_SHARENAME_LENGTH
* 2, GFP_KERNEL
);
1151 if (unc_path
== NULL
)
1154 unc_path_len
= cifs_strtoUTF16(unc_path
, tree
, strlen(tree
), cp
) + 1;
1156 if (unc_path_len
< 2) {
1161 rc
= small_smb2_init(SMB2_TREE_CONNECT
, tcon
, (void **) &req
);
1168 /* since no tcon, smb2_init can not do this, so do here */
1169 req
->hdr
.sync_hdr
.SessionId
= ses
->Suid
;
1170 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
1171 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
1174 iov
[0].iov_base
= (char *)req
;
1175 /* 4 for rfc1002 length field and 1 for pad */
1176 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
1178 /* Testing shows that buffer offset must be at location of Buffer[0] */
1179 req
->PathOffset
= cpu_to_le16(sizeof(struct smb2_tree_connect_req
)
1180 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1181 req
->PathLength
= cpu_to_le16(unc_path_len
- 2);
1182 iov
[1].iov_base
= unc_path
;
1183 iov
[1].iov_len
= unc_path_len
;
1185 inc_rfc1001_len(req
, unc_path_len
- 1 /* pad */);
1187 rc
= SendReceive2(xid
, ses
, iov
, 2, &resp_buftype
, 0, &rsp_iov
);
1188 cifs_small_buf_release(req
);
1189 rsp
= (struct smb2_tree_connect_rsp
*)rsp_iov
.iov_base
;
1193 cifs_stats_fail_inc(tcon
, SMB2_TREE_CONNECT_HE
);
1194 tcon
->need_reconnect
= true;
1196 goto tcon_error_exit
;
1200 ses
->ipc_tid
= rsp
->hdr
.sync_hdr
.TreeId
;
1204 if (rsp
->ShareType
& SMB2_SHARE_TYPE_DISK
)
1205 cifs_dbg(FYI
, "connection to disk share\n");
1206 else if (rsp
->ShareType
& SMB2_SHARE_TYPE_PIPE
) {
1208 cifs_dbg(FYI
, "connection to pipe share\n");
1209 } else if (rsp
->ShareType
& SMB2_SHARE_TYPE_PRINT
) {
1211 cifs_dbg(FYI
, "connection to printer\n");
1213 cifs_dbg(VFS
, "unknown share type %d\n", rsp
->ShareType
);
1215 goto tcon_error_exit
;
1218 tcon
->share_flags
= le32_to_cpu(rsp
->ShareFlags
);
1219 tcon
->capabilities
= rsp
->Capabilities
; /* we keep caps little endian */
1220 tcon
->maximal_access
= le32_to_cpu(rsp
->MaximalAccess
);
1221 tcon
->tidStatus
= CifsGood
;
1222 tcon
->need_reconnect
= false;
1223 tcon
->tid
= rsp
->hdr
.sync_hdr
.TreeId
;
1224 strlcpy(tcon
->treeName
, tree
, sizeof(tcon
->treeName
));
1226 if ((rsp
->Capabilities
& SMB2_SHARE_CAP_DFS
) &&
1227 ((tcon
->share_flags
& SHI1005_FLAGS_DFS
) == 0))
1228 cifs_dbg(VFS
, "DFS capability contradicts DFS flag\n");
1229 init_copy_chunk_defaults(tcon
);
1230 if (tcon
->share_flags
& SHI1005_FLAGS_ENCRYPT_DATA
)
1231 cifs_dbg(VFS
, "Encrypted shares not supported");
1232 if (tcon
->ses
->server
->ops
->validate_negotiate
)
1233 rc
= tcon
->ses
->server
->ops
->validate_negotiate(xid
, tcon
);
1235 free_rsp_buf(resp_buftype
, rsp
);
1240 if (rsp
->hdr
.sync_hdr
.Status
== STATUS_BAD_NETWORK_NAME
) {
1241 cifs_dbg(VFS
, "BAD_NETWORK_NAME: %s\n", tree
);
1243 tcon
->bad_network_name
= true;
1249 SMB2_tdis(const unsigned int xid
, struct cifs_tcon
*tcon
)
1251 struct smb2_tree_disconnect_req
*req
; /* response is trivial */
1253 struct TCP_Server_Info
*server
;
1254 struct cifs_ses
*ses
= tcon
->ses
;
1256 cifs_dbg(FYI
, "Tree Disconnect\n");
1258 if (ses
&& (ses
->server
))
1259 server
= ses
->server
;
1263 if ((tcon
->need_reconnect
) || (tcon
->ses
->need_reconnect
))
1266 rc
= small_smb2_init(SMB2_TREE_DISCONNECT
, tcon
, (void **) &req
);
1270 rc
= SendReceiveNoRsp(xid
, ses
, (char *)req
, 0);
1271 cifs_small_buf_release(req
);
1273 cifs_stats_fail_inc(tcon
, SMB2_TREE_DISCONNECT_HE
);
1279 static struct create_durable
*
1280 create_durable_buf(void)
1282 struct create_durable
*buf
;
1284 buf
= kzalloc(sizeof(struct create_durable
), GFP_KERNEL
);
1288 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
1289 (struct create_durable
, Data
));
1290 buf
->ccontext
.DataLength
= cpu_to_le32(16);
1291 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
1292 (struct create_durable
, Name
));
1293 buf
->ccontext
.NameLength
= cpu_to_le16(4);
1294 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1302 static struct create_durable
*
1303 create_reconnect_durable_buf(struct cifs_fid
*fid
)
1305 struct create_durable
*buf
;
1307 buf
= kzalloc(sizeof(struct create_durable
), GFP_KERNEL
);
1311 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
1312 (struct create_durable
, Data
));
1313 buf
->ccontext
.DataLength
= cpu_to_le32(16);
1314 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
1315 (struct create_durable
, Name
));
1316 buf
->ccontext
.NameLength
= cpu_to_le16(4);
1317 buf
->Data
.Fid
.PersistentFileId
= fid
->persistent_fid
;
1318 buf
->Data
.Fid
.VolatileFileId
= fid
->volatile_fid
;
1319 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1328 parse_lease_state(struct TCP_Server_Info
*server
, struct smb2_create_rsp
*rsp
,
1329 unsigned int *epoch
)
1332 struct create_context
*cc
;
1334 unsigned int remaining
;
1337 data_offset
= (char *)rsp
+ 4 + le32_to_cpu(rsp
->CreateContextsOffset
);
1338 remaining
= le32_to_cpu(rsp
->CreateContextsLength
);
1339 cc
= (struct create_context
*)data_offset
;
1340 while (remaining
>= sizeof(struct create_context
)) {
1341 name
= le16_to_cpu(cc
->NameOffset
) + (char *)cc
;
1342 if (le16_to_cpu(cc
->NameLength
) == 4 &&
1343 strncmp(name
, "RqLs", 4) == 0)
1344 return server
->ops
->parse_lease_buf(cc
, epoch
);
1346 next
= le32_to_cpu(cc
->Next
);
1350 cc
= (struct create_context
*)((char *)cc
+ next
);
1357 add_lease_context(struct TCP_Server_Info
*server
, struct kvec
*iov
,
1358 unsigned int *num_iovec
, __u8
*oplock
)
1360 struct smb2_create_req
*req
= iov
[0].iov_base
;
1361 unsigned int num
= *num_iovec
;
1363 iov
[num
].iov_base
= server
->ops
->create_lease_buf(oplock
+1, *oplock
);
1364 if (iov
[num
].iov_base
== NULL
)
1366 iov
[num
].iov_len
= server
->vals
->create_lease_size
;
1367 req
->RequestedOplockLevel
= SMB2_OPLOCK_LEVEL_LEASE
;
1368 if (!req
->CreateContextsOffset
)
1369 req
->CreateContextsOffset
= cpu_to_le32(
1370 sizeof(struct smb2_create_req
) - 4 +
1371 iov
[num
- 1].iov_len
);
1372 le32_add_cpu(&req
->CreateContextsLength
,
1373 server
->vals
->create_lease_size
);
1374 inc_rfc1001_len(&req
->hdr
, server
->vals
->create_lease_size
);
1375 *num_iovec
= num
+ 1;
1379 static struct create_durable_v2
*
1380 create_durable_v2_buf(struct cifs_fid
*pfid
)
1382 struct create_durable_v2
*buf
;
1384 buf
= kzalloc(sizeof(struct create_durable_v2
), GFP_KERNEL
);
1388 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
1389 (struct create_durable_v2
, dcontext
));
1390 buf
->ccontext
.DataLength
= cpu_to_le32(sizeof(struct durable_context_v2
));
1391 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
1392 (struct create_durable_v2
, Name
));
1393 buf
->ccontext
.NameLength
= cpu_to_le16(4);
1395 buf
->dcontext
.Timeout
= 0; /* Should this be configurable by workload */
1396 buf
->dcontext
.Flags
= cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT
);
1397 generate_random_uuid(buf
->dcontext
.CreateGuid
);
1398 memcpy(pfid
->create_guid
, buf
->dcontext
.CreateGuid
, 16);
1400 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1408 static struct create_durable_handle_reconnect_v2
*
1409 create_reconnect_durable_v2_buf(struct cifs_fid
*fid
)
1411 struct create_durable_handle_reconnect_v2
*buf
;
1413 buf
= kzalloc(sizeof(struct create_durable_handle_reconnect_v2
),
1418 buf
->ccontext
.DataOffset
=
1419 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2
,
1421 buf
->ccontext
.DataLength
=
1422 cpu_to_le32(sizeof(struct durable_reconnect_context_v2
));
1423 buf
->ccontext
.NameOffset
=
1424 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2
,
1426 buf
->ccontext
.NameLength
= cpu_to_le16(4);
1428 buf
->dcontext
.Fid
.PersistentFileId
= fid
->persistent_fid
;
1429 buf
->dcontext
.Fid
.VolatileFileId
= fid
->volatile_fid
;
1430 buf
->dcontext
.Flags
= cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT
);
1431 memcpy(buf
->dcontext
.CreateGuid
, fid
->create_guid
, 16);
1433 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1442 add_durable_v2_context(struct kvec
*iov
, unsigned int *num_iovec
,
1443 struct cifs_open_parms
*oparms
)
1445 struct smb2_create_req
*req
= iov
[0].iov_base
;
1446 unsigned int num
= *num_iovec
;
1448 iov
[num
].iov_base
= create_durable_v2_buf(oparms
->fid
);
1449 if (iov
[num
].iov_base
== NULL
)
1451 iov
[num
].iov_len
= sizeof(struct create_durable_v2
);
1452 if (!req
->CreateContextsOffset
)
1453 req
->CreateContextsOffset
=
1454 cpu_to_le32(sizeof(struct smb2_create_req
) - 4 +
1456 le32_add_cpu(&req
->CreateContextsLength
, sizeof(struct create_durable_v2
));
1457 inc_rfc1001_len(&req
->hdr
, sizeof(struct create_durable_v2
));
1458 *num_iovec
= num
+ 1;
1463 add_durable_reconnect_v2_context(struct kvec
*iov
, unsigned int *num_iovec
,
1464 struct cifs_open_parms
*oparms
)
1466 struct smb2_create_req
*req
= iov
[0].iov_base
;
1467 unsigned int num
= *num_iovec
;
1469 /* indicate that we don't need to relock the file */
1470 oparms
->reconnect
= false;
1472 iov
[num
].iov_base
= create_reconnect_durable_v2_buf(oparms
->fid
);
1473 if (iov
[num
].iov_base
== NULL
)
1475 iov
[num
].iov_len
= sizeof(struct create_durable_handle_reconnect_v2
);
1476 if (!req
->CreateContextsOffset
)
1477 req
->CreateContextsOffset
=
1478 cpu_to_le32(sizeof(struct smb2_create_req
) - 4 +
1480 le32_add_cpu(&req
->CreateContextsLength
,
1481 sizeof(struct create_durable_handle_reconnect_v2
));
1482 inc_rfc1001_len(&req
->hdr
,
1483 sizeof(struct create_durable_handle_reconnect_v2
));
1484 *num_iovec
= num
+ 1;
1489 add_durable_context(struct kvec
*iov
, unsigned int *num_iovec
,
1490 struct cifs_open_parms
*oparms
, bool use_persistent
)
1492 struct smb2_create_req
*req
= iov
[0].iov_base
;
1493 unsigned int num
= *num_iovec
;
1495 if (use_persistent
) {
1496 if (oparms
->reconnect
)
1497 return add_durable_reconnect_v2_context(iov
, num_iovec
,
1500 return add_durable_v2_context(iov
, num_iovec
, oparms
);
1503 if (oparms
->reconnect
) {
1504 iov
[num
].iov_base
= create_reconnect_durable_buf(oparms
->fid
);
1505 /* indicate that we don't need to relock the file */
1506 oparms
->reconnect
= false;
1508 iov
[num
].iov_base
= create_durable_buf();
1509 if (iov
[num
].iov_base
== NULL
)
1511 iov
[num
].iov_len
= sizeof(struct create_durable
);
1512 if (!req
->CreateContextsOffset
)
1513 req
->CreateContextsOffset
=
1514 cpu_to_le32(sizeof(struct smb2_create_req
) - 4 +
1516 le32_add_cpu(&req
->CreateContextsLength
, sizeof(struct create_durable
));
1517 inc_rfc1001_len(&req
->hdr
, sizeof(struct create_durable
));
1518 *num_iovec
= num
+ 1;
1523 SMB2_open(const unsigned int xid
, struct cifs_open_parms
*oparms
, __le16
*path
,
1524 __u8
*oplock
, struct smb2_file_all_info
*buf
,
1525 struct smb2_err_rsp
**err_buf
)
1527 struct smb2_create_req
*req
;
1528 struct smb2_create_rsp
*rsp
;
1529 struct TCP_Server_Info
*server
;
1530 struct cifs_tcon
*tcon
= oparms
->tcon
;
1531 struct cifs_ses
*ses
= tcon
->ses
;
1533 struct kvec rsp_iov
;
1536 __le16
*copy_path
= NULL
;
1539 unsigned int n_iov
= 2;
1540 __u32 file_attributes
= 0;
1541 char *dhc_buf
= NULL
, *lc_buf
= NULL
;
1543 cifs_dbg(FYI
, "create/open\n");
1545 if (ses
&& (ses
->server
))
1546 server
= ses
->server
;
1550 rc
= small_smb2_init(SMB2_CREATE
, tcon
, (void **) &req
);
1554 if (oparms
->create_options
& CREATE_OPTION_READONLY
)
1555 file_attributes
|= ATTR_READONLY
;
1556 if (oparms
->create_options
& CREATE_OPTION_SPECIAL
)
1557 file_attributes
|= ATTR_SYSTEM
;
1559 req
->ImpersonationLevel
= IL_IMPERSONATION
;
1560 req
->DesiredAccess
= cpu_to_le32(oparms
->desired_access
);
1561 /* File attributes ignored on open (used in create though) */
1562 req
->FileAttributes
= cpu_to_le32(file_attributes
);
1563 req
->ShareAccess
= FILE_SHARE_ALL_LE
;
1564 req
->CreateDisposition
= cpu_to_le32(oparms
->disposition
);
1565 req
->CreateOptions
= cpu_to_le32(oparms
->create_options
& CREATE_OPTIONS_MASK
);
1566 uni_path_len
= (2 * UniStrnlen((wchar_t *)path
, PATH_MAX
)) + 2;
1567 /* do not count rfc1001 len field */
1568 req
->NameOffset
= cpu_to_le16(sizeof(struct smb2_create_req
) - 4);
1570 iov
[0].iov_base
= (char *)req
;
1571 /* 4 for rfc1002 length field */
1572 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
1574 /* MUST set path len (NameLength) to 0 opening root of share */
1575 req
->NameLength
= cpu_to_le16(uni_path_len
- 2);
1576 /* -1 since last byte is buf[0] which is sent below (path) */
1578 if (uni_path_len
% 8 != 0) {
1579 copy_size
= uni_path_len
/ 8 * 8;
1580 if (copy_size
< uni_path_len
)
1583 copy_path
= kzalloc(copy_size
, GFP_KERNEL
);
1586 memcpy((char *)copy_path
, (const char *)path
,
1588 uni_path_len
= copy_size
;
1592 iov
[1].iov_len
= uni_path_len
;
1593 iov
[1].iov_base
= path
;
1594 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1595 inc_rfc1001_len(req
, uni_path_len
- 1);
1597 if (!server
->oplocks
)
1598 *oplock
= SMB2_OPLOCK_LEVEL_NONE
;
1600 if (!(server
->capabilities
& SMB2_GLOBAL_CAP_LEASING
) ||
1601 *oplock
== SMB2_OPLOCK_LEVEL_NONE
)
1602 req
->RequestedOplockLevel
= *oplock
;
1604 rc
= add_lease_context(server
, iov
, &n_iov
, oplock
);
1606 cifs_small_buf_release(req
);
1610 lc_buf
= iov
[n_iov
-1].iov_base
;
1613 if (*oplock
== SMB2_OPLOCK_LEVEL_BATCH
) {
1614 /* need to set Next field of lease context if we request it */
1615 if (server
->capabilities
& SMB2_GLOBAL_CAP_LEASING
) {
1616 struct create_context
*ccontext
=
1617 (struct create_context
*)iov
[n_iov
-1].iov_base
;
1619 cpu_to_le32(server
->vals
->create_lease_size
);
1622 rc
= add_durable_context(iov
, &n_iov
, oparms
,
1623 tcon
->use_persistent
);
1625 cifs_small_buf_release(req
);
1630 dhc_buf
= iov
[n_iov
-1].iov_base
;
1633 rc
= SendReceive2(xid
, ses
, iov
, n_iov
, &resp_buftype
, 0, &rsp_iov
);
1634 cifs_small_buf_release(req
);
1635 rsp
= (struct smb2_create_rsp
*)rsp_iov
.iov_base
;
1638 cifs_stats_fail_inc(tcon
, SMB2_CREATE_HE
);
1640 *err_buf
= kmemdup(rsp
, get_rfc1002_length(rsp
) + 4,
1645 oparms
->fid
->persistent_fid
= rsp
->PersistentFileId
;
1646 oparms
->fid
->volatile_fid
= rsp
->VolatileFileId
;
1649 memcpy(buf
, &rsp
->CreationTime
, 32);
1650 buf
->AllocationSize
= rsp
->AllocationSize
;
1651 buf
->EndOfFile
= rsp
->EndofFile
;
1652 buf
->Attributes
= rsp
->FileAttributes
;
1653 buf
->NumberOfLinks
= cpu_to_le32(1);
1654 buf
->DeletePending
= 0;
1657 if (rsp
->OplockLevel
== SMB2_OPLOCK_LEVEL_LEASE
)
1658 *oplock
= parse_lease_state(server
, rsp
, &oparms
->fid
->epoch
);
1660 *oplock
= rsp
->OplockLevel
;
1665 free_rsp_buf(resp_buftype
, rsp
);
1670 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1673 SMB2_ioctl(const unsigned int xid
, struct cifs_tcon
*tcon
, u64 persistent_fid
,
1674 u64 volatile_fid
, u32 opcode
, bool is_fsctl
, char *in_data
,
1675 u32 indatalen
, char **out_data
, u32
*plen
/* returned data len */)
1677 struct smb2_ioctl_req
*req
;
1678 struct smb2_ioctl_rsp
*rsp
;
1679 struct smb2_sync_hdr
*shdr
;
1680 struct TCP_Server_Info
*server
;
1681 struct cifs_ses
*ses
;
1683 struct kvec rsp_iov
;
1688 cifs_dbg(FYI
, "SMB2 IOCTL\n");
1690 if (out_data
!= NULL
)
1693 /* zero out returned data len, in case of error */
1702 if (ses
&& (ses
->server
))
1703 server
= ses
->server
;
1707 rc
= small_smb2_init(SMB2_IOCTL
, tcon
, (void **) &req
);
1711 req
->CtlCode
= cpu_to_le32(opcode
);
1712 req
->PersistentFileId
= persistent_fid
;
1713 req
->VolatileFileId
= volatile_fid
;
1716 req
->InputCount
= cpu_to_le32(indatalen
);
1717 /* do not set InputOffset if no input data */
1719 cpu_to_le32(offsetof(struct smb2_ioctl_req
, Buffer
) - 4);
1720 iov
[1].iov_base
= in_data
;
1721 iov
[1].iov_len
= indatalen
;
1726 req
->OutputOffset
= 0;
1727 req
->OutputCount
= 0; /* MBZ */
1730 * Could increase MaxOutputResponse, but that would require more
1731 * than one credit. Windows typically sets this smaller, but for some
1732 * ioctls it may be useful to allow server to send more. No point
1733 * limiting what the server can send as long as fits in one credit
1735 req
->MaxOutputResponse
= cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1738 req
->Flags
= cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL
);
1742 iov
[0].iov_base
= (char *)req
;
1745 * If no input data, the size of ioctl struct in
1746 * protocol spec still includes a 1 byte data buffer,
1747 * but if input data passed to ioctl, we do not
1748 * want to double count this, so we do not send
1749 * the dummy one byte of data in iovec[0] if sending
1750 * input data (in iovec[1]). We also must add 4 bytes
1751 * in first iovec to allow for rfc1002 length field.
1755 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
1756 inc_rfc1001_len(req
, indatalen
- 1);
1758 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
1761 rc
= SendReceive2(xid
, ses
, iov
, n_iov
, &resp_buftype
, 0, &rsp_iov
);
1762 cifs_small_buf_release(req
);
1763 rsp
= (struct smb2_ioctl_rsp
*)rsp_iov
.iov_base
;
1765 if ((rc
!= 0) && (rc
!= -EINVAL
)) {
1766 cifs_stats_fail_inc(tcon
, SMB2_IOCTL_HE
);
1768 } else if (rc
== -EINVAL
) {
1769 if ((opcode
!= FSCTL_SRV_COPYCHUNK_WRITE
) &&
1770 (opcode
!= FSCTL_SRV_COPYCHUNK
)) {
1771 cifs_stats_fail_inc(tcon
, SMB2_IOCTL_HE
);
1776 /* check if caller wants to look at return data or just return rc */
1777 if ((plen
== NULL
) || (out_data
== NULL
))
1780 *plen
= le32_to_cpu(rsp
->OutputCount
);
1782 /* We check for obvious errors in the output buffer length and offset */
1784 goto ioctl_exit
; /* server returned no data */
1785 else if (*plen
> 0xFF00) {
1786 cifs_dbg(VFS
, "srv returned invalid ioctl length: %d\n", *plen
);
1792 if (get_rfc1002_length(rsp
) < le32_to_cpu(rsp
->OutputOffset
) + *plen
) {
1793 cifs_dbg(VFS
, "Malformed ioctl resp: len %d offset %d\n", *plen
,
1794 le32_to_cpu(rsp
->OutputOffset
));
1800 *out_data
= kmalloc(*plen
, GFP_KERNEL
);
1801 if (*out_data
== NULL
) {
1806 shdr
= get_sync_hdr(rsp
);
1807 memcpy(*out_data
, (char *)shdr
+ le32_to_cpu(rsp
->OutputOffset
), *plen
);
1809 free_rsp_buf(resp_buftype
, rsp
);
1814 * Individual callers to ioctl worker function follow
1818 SMB2_set_compression(const unsigned int xid
, struct cifs_tcon
*tcon
,
1819 u64 persistent_fid
, u64 volatile_fid
)
1822 struct compress_ioctl fsctl_input
;
1823 char *ret_data
= NULL
;
1825 fsctl_input
.CompressionState
=
1826 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT
);
1828 rc
= SMB2_ioctl(xid
, tcon
, persistent_fid
, volatile_fid
,
1829 FSCTL_SET_COMPRESSION
, true /* is_fsctl */,
1830 (char *)&fsctl_input
/* data input */,
1831 2 /* in data len */, &ret_data
/* out data */, NULL
);
1833 cifs_dbg(FYI
, "set compression rc %d\n", rc
);
1839 SMB2_close(const unsigned int xid
, struct cifs_tcon
*tcon
,
1840 u64 persistent_fid
, u64 volatile_fid
)
1842 struct smb2_close_req
*req
;
1843 struct smb2_close_rsp
*rsp
;
1844 struct TCP_Server_Info
*server
;
1845 struct cifs_ses
*ses
= tcon
->ses
;
1847 struct kvec rsp_iov
;
1851 cifs_dbg(FYI
, "Close\n");
1853 if (ses
&& (ses
->server
))
1854 server
= ses
->server
;
1858 rc
= small_smb2_init(SMB2_CLOSE
, tcon
, (void **) &req
);
1862 req
->PersistentFileId
= persistent_fid
;
1863 req
->VolatileFileId
= volatile_fid
;
1865 iov
[0].iov_base
= (char *)req
;
1866 /* 4 for rfc1002 length field */
1867 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
1869 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buftype
, 0, &rsp_iov
);
1870 cifs_small_buf_release(req
);
1871 rsp
= (struct smb2_close_rsp
*)rsp_iov
.iov_base
;
1874 cifs_stats_fail_inc(tcon
, SMB2_CLOSE_HE
);
1878 /* BB FIXME - decode close response, update inode for caching */
1881 free_rsp_buf(resp_buftype
, rsp
);
1886 validate_buf(unsigned int offset
, unsigned int buffer_length
,
1887 struct smb2_hdr
*hdr
, unsigned int min_buf_size
)
1890 unsigned int smb_len
= be32_to_cpu(hdr
->smb2_buf_length
);
1891 char *end_of_smb
= smb_len
+ 4 /* RFC1001 length field */ + (char *)hdr
;
1892 char *begin_of_buf
= 4 /* RFC1001 len field */ + offset
+ (char *)hdr
;
1893 char *end_of_buf
= begin_of_buf
+ buffer_length
;
1896 if (buffer_length
< min_buf_size
) {
1897 cifs_dbg(VFS
, "buffer length %d smaller than minimum size %d\n",
1898 buffer_length
, min_buf_size
);
1902 /* check if beyond RFC1001 maximum length */
1903 if ((smb_len
> 0x7FFFFF) || (buffer_length
> 0x7FFFFF)) {
1904 cifs_dbg(VFS
, "buffer length %d or smb length %d too large\n",
1905 buffer_length
, smb_len
);
1909 if ((begin_of_buf
> end_of_smb
) || (end_of_buf
> end_of_smb
)) {
1910 cifs_dbg(VFS
, "illegal server response, bad offset to data\n");
1918 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1919 * Caller must free buffer.
1922 validate_and_copy_buf(unsigned int offset
, unsigned int buffer_length
,
1923 struct smb2_hdr
*hdr
, unsigned int minbufsize
,
1927 char *begin_of_buf
= 4 /* RFC1001 len field */ + offset
+ (char *)hdr
;
1933 rc
= validate_buf(offset
, buffer_length
, hdr
, minbufsize
);
1937 memcpy(data
, begin_of_buf
, buffer_length
);
1943 query_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
1944 u64 persistent_fid
, u64 volatile_fid
, u8 info_class
,
1945 size_t output_len
, size_t min_len
, void *data
)
1947 struct smb2_query_info_req
*req
;
1948 struct smb2_query_info_rsp
*rsp
= NULL
;
1950 struct kvec rsp_iov
;
1953 struct TCP_Server_Info
*server
;
1954 struct cifs_ses
*ses
= tcon
->ses
;
1956 cifs_dbg(FYI
, "Query Info\n");
1958 if (ses
&& (ses
->server
))
1959 server
= ses
->server
;
1963 rc
= small_smb2_init(SMB2_QUERY_INFO
, tcon
, (void **) &req
);
1967 req
->InfoType
= SMB2_O_INFO_FILE
;
1968 req
->FileInfoClass
= info_class
;
1969 req
->PersistentFileId
= persistent_fid
;
1970 req
->VolatileFileId
= volatile_fid
;
1971 /* 4 for rfc1002 length field and 1 for Buffer */
1972 req
->InputBufferOffset
=
1973 cpu_to_le16(sizeof(struct smb2_query_info_req
) - 1 - 4);
1974 req
->OutputBufferLength
= cpu_to_le32(output_len
);
1976 iov
[0].iov_base
= (char *)req
;
1977 /* 4 for rfc1002 length field */
1978 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
1980 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buftype
, 0, &rsp_iov
);
1981 cifs_small_buf_release(req
);
1982 rsp
= (struct smb2_query_info_rsp
*)rsp_iov
.iov_base
;
1985 cifs_stats_fail_inc(tcon
, SMB2_QUERY_INFO_HE
);
1989 rc
= validate_and_copy_buf(le16_to_cpu(rsp
->OutputBufferOffset
),
1990 le32_to_cpu(rsp
->OutputBufferLength
),
1991 &rsp
->hdr
, min_len
, data
);
1994 free_rsp_buf(resp_buftype
, rsp
);
1999 SMB2_query_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
2000 u64 persistent_fid
, u64 volatile_fid
,
2001 struct smb2_file_all_info
*data
)
2003 return query_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2004 FILE_ALL_INFORMATION
,
2005 sizeof(struct smb2_file_all_info
) + PATH_MAX
* 2,
2006 sizeof(struct smb2_file_all_info
), data
);
2010 SMB2_get_srv_num(const unsigned int xid
, struct cifs_tcon
*tcon
,
2011 u64 persistent_fid
, u64 volatile_fid
, __le64
*uniqueid
)
2013 return query_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2014 FILE_INTERNAL_INFORMATION
,
2015 sizeof(struct smb2_file_internal_info
),
2016 sizeof(struct smb2_file_internal_info
), uniqueid
);
2020 * This is a no-op for now. We're not really interested in the reply, but
2021 * rather in the fact that the server sent one and that server->lstrp
2024 * FIXME: maybe we should consider checking that the reply matches request?
2027 smb2_echo_callback(struct mid_q_entry
*mid
)
2029 struct TCP_Server_Info
*server
= mid
->callback_data
;
2030 struct smb2_echo_rsp
*rsp
= (struct smb2_echo_rsp
*)mid
->resp_buf
;
2031 unsigned int credits_received
= 1;
2033 if (mid
->mid_state
== MID_RESPONSE_RECEIVED
)
2034 credits_received
= le16_to_cpu(rsp
->hdr
.sync_hdr
.CreditRequest
);
2036 mutex_lock(&server
->srv_mutex
);
2037 DeleteMidQEntry(mid
);
2038 mutex_unlock(&server
->srv_mutex
);
2039 add_credits(server
, credits_received
, CIFS_ECHO_OP
);
2042 void smb2_reconnect_server(struct work_struct
*work
)
2044 struct TCP_Server_Info
*server
= container_of(work
,
2045 struct TCP_Server_Info
, reconnect
.work
);
2046 struct cifs_ses
*ses
;
2047 struct cifs_tcon
*tcon
, *tcon2
;
2048 struct list_head tmp_list
;
2049 int tcon_exist
= false;
2051 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2052 mutex_lock(&server
->reconnect_mutex
);
2054 INIT_LIST_HEAD(&tmp_list
);
2055 cifs_dbg(FYI
, "Need negotiate, reconnecting tcons\n");
2057 spin_lock(&cifs_tcp_ses_lock
);
2058 list_for_each_entry(ses
, &server
->smb_ses_list
, smb_ses_list
) {
2059 list_for_each_entry(tcon
, &ses
->tcon_list
, tcon_list
) {
2060 if (tcon
->need_reconnect
|| tcon
->need_reopen_files
) {
2062 list_add_tail(&tcon
->rlist
, &tmp_list
);
2068 * Get the reference to server struct to be sure that the last call of
2069 * cifs_put_tcon() in the loop below won't release the server pointer.
2072 server
->srv_count
++;
2074 spin_unlock(&cifs_tcp_ses_lock
);
2076 list_for_each_entry_safe(tcon
, tcon2
, &tmp_list
, rlist
) {
2077 if (!smb2_reconnect(SMB2_INTERNAL_CMD
, tcon
))
2078 cifs_reopen_persistent_handles(tcon
);
2079 list_del_init(&tcon
->rlist
);
2080 cifs_put_tcon(tcon
);
2083 cifs_dbg(FYI
, "Reconnecting tcons finished\n");
2084 mutex_unlock(&server
->reconnect_mutex
);
2086 /* now we can safely release srv struct */
2088 cifs_put_tcp_session(server
, 1);
2092 SMB2_echo(struct TCP_Server_Info
*server
)
2094 struct smb2_echo_req
*req
;
2097 struct smb_rqst rqst
= { .rq_iov
= iov
,
2100 cifs_dbg(FYI
, "In echo request\n");
2102 if (server
->tcpStatus
== CifsNeedNegotiate
) {
2103 /* No need to send echo on newly established connections */
2104 queue_delayed_work(cifsiod_wq
, &server
->reconnect
, 0);
2108 rc
= small_smb2_init(SMB2_ECHO
, NULL
, (void **)&req
);
2112 req
->hdr
.sync_hdr
.CreditRequest
= cpu_to_le16(1);
2114 /* 4 for rfc1002 length field */
2116 iov
[0].iov_base
= (char *)req
;
2117 iov
[1].iov_len
= get_rfc1002_length(req
);
2118 iov
[1].iov_base
= (char *)req
+ 4;
2120 rc
= cifs_call_async(server
, &rqst
, NULL
, smb2_echo_callback
, server
,
2123 cifs_dbg(FYI
, "Echo request failed: %d\n", rc
);
2125 cifs_small_buf_release(req
);
2130 SMB2_flush(const unsigned int xid
, struct cifs_tcon
*tcon
, u64 persistent_fid
,
2133 struct smb2_flush_req
*req
;
2134 struct TCP_Server_Info
*server
;
2135 struct cifs_ses
*ses
= tcon
->ses
;
2137 struct kvec rsp_iov
;
2141 cifs_dbg(FYI
, "Flush\n");
2143 if (ses
&& (ses
->server
))
2144 server
= ses
->server
;
2148 rc
= small_smb2_init(SMB2_FLUSH
, tcon
, (void **) &req
);
2152 req
->PersistentFileId
= persistent_fid
;
2153 req
->VolatileFileId
= volatile_fid
;
2155 iov
[0].iov_base
= (char *)req
;
2156 /* 4 for rfc1002 length field */
2157 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
2159 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buftype
, 0, &rsp_iov
);
2160 cifs_small_buf_release(req
);
2163 cifs_stats_fail_inc(tcon
, SMB2_FLUSH_HE
);
2165 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
2170 * To form a chain of read requests, any read requests after the first should
2171 * have the end_of_chain boolean set to true.
2174 smb2_new_read_req(void **buf
, unsigned int *total_len
,
2175 struct cifs_io_parms
*io_parms
, unsigned int remaining_bytes
,
2179 struct smb2_read_plain_req
*req
= NULL
;
2180 struct smb2_sync_hdr
*shdr
;
2182 rc
= smb2_plain_req_init(SMB2_READ
, io_parms
->tcon
, (void **) &req
,
2186 if (io_parms
->tcon
->ses
->server
== NULL
)
2187 return -ECONNABORTED
;
2189 shdr
= &req
->sync_hdr
;
2190 shdr
->ProcessId
= cpu_to_le32(io_parms
->pid
);
2192 req
->PersistentFileId
= io_parms
->persistent_fid
;
2193 req
->VolatileFileId
= io_parms
->volatile_fid
;
2194 req
->ReadChannelInfoOffset
= 0; /* reserved */
2195 req
->ReadChannelInfoLength
= 0; /* reserved */
2196 req
->Channel
= 0; /* reserved */
2197 req
->MinimumCount
= 0;
2198 req
->Length
= cpu_to_le32(io_parms
->length
);
2199 req
->Offset
= cpu_to_le64(io_parms
->offset
);
2201 if (request_type
& CHAINED_REQUEST
) {
2202 if (!(request_type
& END_OF_CHAIN
)) {
2203 /* next 8-byte aligned request */
2204 *total_len
= DIV_ROUND_UP(*total_len
, 8) * 8;
2205 shdr
->NextCommand
= cpu_to_le32(*total_len
);
2206 } else /* END_OF_CHAIN */
2207 shdr
->NextCommand
= 0;
2208 if (request_type
& RELATED_REQUEST
) {
2209 shdr
->Flags
|= SMB2_FLAGS_RELATED_OPERATIONS
;
2211 * Related requests use info from previous read request
2214 shdr
->SessionId
= 0xFFFFFFFF;
2215 shdr
->TreeId
= 0xFFFFFFFF;
2216 req
->PersistentFileId
= 0xFFFFFFFF;
2217 req
->VolatileFileId
= 0xFFFFFFFF;
2220 if (remaining_bytes
> io_parms
->length
)
2221 req
->RemainingBytes
= cpu_to_le32(remaining_bytes
);
2223 req
->RemainingBytes
= 0;
2230 smb2_readv_callback(struct mid_q_entry
*mid
)
2232 struct cifs_readdata
*rdata
= mid
->callback_data
;
2233 struct cifs_tcon
*tcon
= tlink_tcon(rdata
->cfile
->tlink
);
2234 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
2235 struct smb2_sync_hdr
*shdr
=
2236 (struct smb2_sync_hdr
*)rdata
->iov
[1].iov_base
;
2237 unsigned int credits_received
= 1;
2238 struct smb_rqst rqst
= { .rq_iov
= rdata
->iov
,
2240 .rq_pages
= rdata
->pages
,
2241 .rq_npages
= rdata
->nr_pages
,
2242 .rq_pagesz
= rdata
->pagesz
,
2243 .rq_tailsz
= rdata
->tailsz
};
2245 cifs_dbg(FYI
, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2246 __func__
, mid
->mid
, mid
->mid_state
, rdata
->result
,
2249 switch (mid
->mid_state
) {
2250 case MID_RESPONSE_RECEIVED
:
2251 credits_received
= le16_to_cpu(shdr
->CreditRequest
);
2252 /* result already set, check signature */
2256 rc
= smb2_verify_signature(&rqst
, server
);
2258 cifs_dbg(VFS
, "SMB signature verification returned error = %d\n",
2261 /* FIXME: should this be counted toward the initiating task? */
2262 task_io_account_read(rdata
->got_bytes
);
2263 cifs_stats_bytes_read(tcon
, rdata
->got_bytes
);
2265 case MID_REQUEST_SUBMITTED
:
2266 case MID_RETRY_NEEDED
:
2267 rdata
->result
= -EAGAIN
;
2268 if (server
->sign
&& rdata
->got_bytes
)
2269 /* reset bytes number since we can not check a sign */
2270 rdata
->got_bytes
= 0;
2271 /* FIXME: should this be counted toward the initiating task? */
2272 task_io_account_read(rdata
->got_bytes
);
2273 cifs_stats_bytes_read(tcon
, rdata
->got_bytes
);
2276 if (rdata
->result
!= -ENODATA
)
2277 rdata
->result
= -EIO
;
2281 cifs_stats_fail_inc(tcon
, SMB2_READ_HE
);
2283 queue_work(cifsiod_wq
, &rdata
->work
);
2284 mutex_lock(&server
->srv_mutex
);
2285 DeleteMidQEntry(mid
);
2286 mutex_unlock(&server
->srv_mutex
);
2287 add_credits(server
, credits_received
, 0);
2290 /* smb2_async_readv - send an async read, and set up mid to handle result */
2292 smb2_async_readv(struct cifs_readdata
*rdata
)
2296 struct smb2_sync_hdr
*shdr
;
2297 struct cifs_io_parms io_parms
;
2298 struct smb_rqst rqst
= { .rq_iov
= rdata
->iov
,
2300 struct TCP_Server_Info
*server
;
2301 unsigned int total_len
;
2304 cifs_dbg(FYI
, "%s: offset=%llu bytes=%u\n",
2305 __func__
, rdata
->offset
, rdata
->bytes
);
2307 io_parms
.tcon
= tlink_tcon(rdata
->cfile
->tlink
);
2308 io_parms
.offset
= rdata
->offset
;
2309 io_parms
.length
= rdata
->bytes
;
2310 io_parms
.persistent_fid
= rdata
->cfile
->fid
.persistent_fid
;
2311 io_parms
.volatile_fid
= rdata
->cfile
->fid
.volatile_fid
;
2312 io_parms
.pid
= rdata
->pid
;
2314 server
= io_parms
.tcon
->ses
->server
;
2316 rc
= smb2_new_read_req((void **) &buf
, &total_len
, &io_parms
, 0, 0);
2318 if (rc
== -EAGAIN
&& rdata
->credits
) {
2319 /* credits was reset by reconnect */
2321 /* reduce in_flight value since we won't send the req */
2322 spin_lock(&server
->req_lock
);
2323 server
->in_flight
--;
2324 spin_unlock(&server
->req_lock
);
2329 req_len
= cpu_to_be32(total_len
);
2331 rdata
->iov
[0].iov_base
= &req_len
;
2332 rdata
->iov
[0].iov_len
= sizeof(__be32
);
2333 rdata
->iov
[1].iov_base
= buf
;
2334 rdata
->iov
[1].iov_len
= total_len
;
2336 shdr
= (struct smb2_sync_hdr
*)buf
;
2338 if (rdata
->credits
) {
2339 shdr
->CreditCharge
= cpu_to_le16(DIV_ROUND_UP(rdata
->bytes
,
2340 SMB2_MAX_BUFFER_SIZE
));
2341 shdr
->CreditRequest
= shdr
->CreditCharge
;
2342 spin_lock(&server
->req_lock
);
2343 server
->credits
+= rdata
->credits
-
2344 le16_to_cpu(shdr
->CreditCharge
);
2345 spin_unlock(&server
->req_lock
);
2346 wake_up(&server
->request_q
);
2347 flags
= CIFS_HAS_CREDITS
;
2350 kref_get(&rdata
->refcount
);
2351 rc
= cifs_call_async(io_parms
.tcon
->ses
->server
, &rqst
,
2352 cifs_readv_receive
, smb2_readv_callback
,
2355 kref_put(&rdata
->refcount
, cifs_readdata_release
);
2356 cifs_stats_fail_inc(io_parms
.tcon
, SMB2_READ_HE
);
2359 cifs_small_buf_release(buf
);
2364 SMB2_read(const unsigned int xid
, struct cifs_io_parms
*io_parms
,
2365 unsigned int *nbytes
, char **buf
, int *buf_type
)
2367 int resp_buftype
, rc
= -EACCES
;
2368 struct smb2_read_plain_req
*req
= NULL
;
2369 struct smb2_read_rsp
*rsp
= NULL
;
2370 struct smb2_sync_hdr
*shdr
;
2372 struct kvec rsp_iov
;
2373 unsigned int total_len
;
2375 struct smb_rqst rqst
= { .rq_iov
= iov
,
2379 rc
= smb2_new_read_req((void **)&req
, &total_len
, io_parms
, 0, 0);
2383 req_len
= cpu_to_be32(total_len
);
2385 iov
[0].iov_base
= &req_len
;
2386 iov
[0].iov_len
= sizeof(__be32
);
2387 iov
[1].iov_base
= req
;
2388 iov
[1].iov_len
= total_len
;
2390 rc
= cifs_send_recv(xid
, io_parms
->tcon
->ses
, &rqst
, &resp_buftype
,
2391 CIFS_LOG_ERROR
, &rsp_iov
);
2392 cifs_small_buf_release(req
);
2394 rsp
= (struct smb2_read_rsp
*)rsp_iov
.iov_base
;
2395 shdr
= get_sync_hdr(rsp
);
2397 if (shdr
->Status
== STATUS_END_OF_FILE
) {
2398 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
2403 cifs_stats_fail_inc(io_parms
->tcon
, SMB2_READ_HE
);
2404 cifs_dbg(VFS
, "Send error in read = %d\n", rc
);
2406 *nbytes
= le32_to_cpu(rsp
->DataLength
);
2407 if ((*nbytes
> CIFS_MAX_MSGSIZE
) ||
2408 (*nbytes
> io_parms
->length
)) {
2409 cifs_dbg(FYI
, "bad length %d for count %d\n",
2410 *nbytes
, io_parms
->length
);
2417 memcpy(*buf
, (char *)shdr
+ rsp
->DataOffset
, *nbytes
);
2418 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
2419 } else if (resp_buftype
!= CIFS_NO_BUFFER
) {
2420 *buf
= rsp_iov
.iov_base
;
2421 if (resp_buftype
== CIFS_SMALL_BUFFER
)
2422 *buf_type
= CIFS_SMALL_BUFFER
;
2423 else if (resp_buftype
== CIFS_LARGE_BUFFER
)
2424 *buf_type
= CIFS_LARGE_BUFFER
;
2430 * Check the mid_state and signature on received buffer (if any), and queue the
2431 * workqueue completion task.
2434 smb2_writev_callback(struct mid_q_entry
*mid
)
2436 struct cifs_writedata
*wdata
= mid
->callback_data
;
2437 struct cifs_tcon
*tcon
= tlink_tcon(wdata
->cfile
->tlink
);
2438 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
2439 unsigned int written
;
2440 struct smb2_write_rsp
*rsp
= (struct smb2_write_rsp
*)mid
->resp_buf
;
2441 unsigned int credits_received
= 1;
2443 switch (mid
->mid_state
) {
2444 case MID_RESPONSE_RECEIVED
:
2445 credits_received
= le16_to_cpu(rsp
->hdr
.sync_hdr
.CreditRequest
);
2446 wdata
->result
= smb2_check_receive(mid
, tcon
->ses
->server
, 0);
2447 if (wdata
->result
!= 0)
2450 written
= le32_to_cpu(rsp
->DataLength
);
2452 * Mask off high 16 bits when bytes written as returned
2453 * by the server is greater than bytes requested by the
2454 * client. OS/2 servers are known to set incorrect
2457 if (written
> wdata
->bytes
)
2460 if (written
< wdata
->bytes
)
2461 wdata
->result
= -ENOSPC
;
2463 wdata
->bytes
= written
;
2465 case MID_REQUEST_SUBMITTED
:
2466 case MID_RETRY_NEEDED
:
2467 wdata
->result
= -EAGAIN
;
2470 wdata
->result
= -EIO
;
2475 cifs_stats_fail_inc(tcon
, SMB2_WRITE_HE
);
2477 queue_work(cifsiod_wq
, &wdata
->work
);
2478 mutex_lock(&server
->srv_mutex
);
2479 DeleteMidQEntry(mid
);
2480 mutex_unlock(&server
->srv_mutex
);
2481 add_credits(tcon
->ses
->server
, credits_received
, 0);
2484 /* smb2_async_writev - send an async write, and set up mid to handle result */
2486 smb2_async_writev(struct cifs_writedata
*wdata
,
2487 void (*release
)(struct kref
*kref
))
2489 int rc
= -EACCES
, flags
= 0;
2490 struct smb2_write_req
*req
= NULL
;
2491 struct smb2_sync_hdr
*shdr
;
2492 struct cifs_tcon
*tcon
= tlink_tcon(wdata
->cfile
->tlink
);
2493 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
2495 struct smb_rqst rqst
= { };
2497 rc
= small_smb2_init(SMB2_WRITE
, tcon
, (void **) &req
);
2499 if (rc
== -EAGAIN
&& wdata
->credits
) {
2500 /* credits was reset by reconnect */
2502 /* reduce in_flight value since we won't send the req */
2503 spin_lock(&server
->req_lock
);
2504 server
->in_flight
--;
2505 spin_unlock(&server
->req_lock
);
2507 goto async_writev_out
;
2510 shdr
= get_sync_hdr(req
);
2511 shdr
->ProcessId
= cpu_to_le32(wdata
->cfile
->pid
);
2513 req
->PersistentFileId
= wdata
->cfile
->fid
.persistent_fid
;
2514 req
->VolatileFileId
= wdata
->cfile
->fid
.volatile_fid
;
2515 req
->WriteChannelInfoOffset
= 0;
2516 req
->WriteChannelInfoLength
= 0;
2518 req
->Offset
= cpu_to_le64(wdata
->offset
);
2519 /* 4 for rfc1002 length field */
2520 req
->DataOffset
= cpu_to_le16(
2521 offsetof(struct smb2_write_req
, Buffer
) - 4);
2522 req
->RemainingBytes
= 0;
2524 /* 4 for rfc1002 length field and 1 for Buffer */
2526 iov
[0].iov_base
= req
;
2527 iov
[1].iov_len
= get_rfc1002_length(req
) - 1;
2528 iov
[1].iov_base
= (char *)req
+ 4;
2532 rqst
.rq_pages
= wdata
->pages
;
2533 rqst
.rq_npages
= wdata
->nr_pages
;
2534 rqst
.rq_pagesz
= wdata
->pagesz
;
2535 rqst
.rq_tailsz
= wdata
->tailsz
;
2537 cifs_dbg(FYI
, "async write at %llu %u bytes\n",
2538 wdata
->offset
, wdata
->bytes
);
2540 req
->Length
= cpu_to_le32(wdata
->bytes
);
2542 inc_rfc1001_len(&req
->hdr
, wdata
->bytes
- 1 /* Buffer */);
2544 if (wdata
->credits
) {
2545 shdr
->CreditCharge
= cpu_to_le16(DIV_ROUND_UP(wdata
->bytes
,
2546 SMB2_MAX_BUFFER_SIZE
));
2547 shdr
->CreditRequest
= shdr
->CreditCharge
;
2548 spin_lock(&server
->req_lock
);
2549 server
->credits
+= wdata
->credits
-
2550 le16_to_cpu(shdr
->CreditCharge
);
2551 spin_unlock(&server
->req_lock
);
2552 wake_up(&server
->request_q
);
2553 flags
= CIFS_HAS_CREDITS
;
2556 kref_get(&wdata
->refcount
);
2557 rc
= cifs_call_async(server
, &rqst
, NULL
, smb2_writev_callback
, wdata
,
2561 kref_put(&wdata
->refcount
, release
);
2562 cifs_stats_fail_inc(tcon
, SMB2_WRITE_HE
);
2566 cifs_small_buf_release(req
);
2571 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2572 * The length field from io_parms must be at least 1 and indicates a number of
2573 * elements with data to write that begins with position 1 in iov array. All
2574 * data length is specified by count.
2577 SMB2_write(const unsigned int xid
, struct cifs_io_parms
*io_parms
,
2578 unsigned int *nbytes
, struct kvec
*iov
, int n_vec
)
2581 struct smb2_write_req
*req
= NULL
;
2582 struct smb2_write_rsp
*rsp
= NULL
;
2584 struct kvec rsp_iov
;
2591 rc
= small_smb2_init(SMB2_WRITE
, io_parms
->tcon
, (void **) &req
);
2595 if (io_parms
->tcon
->ses
->server
== NULL
)
2596 return -ECONNABORTED
;
2598 req
->hdr
.sync_hdr
.ProcessId
= cpu_to_le32(io_parms
->pid
);
2600 req
->PersistentFileId
= io_parms
->persistent_fid
;
2601 req
->VolatileFileId
= io_parms
->volatile_fid
;
2602 req
->WriteChannelInfoOffset
= 0;
2603 req
->WriteChannelInfoLength
= 0;
2605 req
->Length
= cpu_to_le32(io_parms
->length
);
2606 req
->Offset
= cpu_to_le64(io_parms
->offset
);
2607 /* 4 for rfc1002 length field */
2608 req
->DataOffset
= cpu_to_le16(
2609 offsetof(struct smb2_write_req
, Buffer
) - 4);
2610 req
->RemainingBytes
= 0;
2612 iov
[0].iov_base
= (char *)req
;
2613 /* 4 for rfc1002 length field and 1 for Buffer */
2614 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
2616 /* length of entire message including data to be written */
2617 inc_rfc1001_len(req
, io_parms
->length
- 1 /* Buffer */);
2619 rc
= SendReceive2(xid
, io_parms
->tcon
->ses
, iov
, n_vec
+ 1,
2620 &resp_buftype
, 0, &rsp_iov
);
2621 cifs_small_buf_release(req
);
2622 rsp
= (struct smb2_write_rsp
*)rsp_iov
.iov_base
;
2625 cifs_stats_fail_inc(io_parms
->tcon
, SMB2_WRITE_HE
);
2626 cifs_dbg(VFS
, "Send error in write = %d\n", rc
);
2628 *nbytes
= le32_to_cpu(rsp
->DataLength
);
2630 free_rsp_buf(resp_buftype
, rsp
);
2635 num_entries(char *bufstart
, char *end_of_buf
, char **lastentry
, size_t size
)
2638 unsigned int entrycount
= 0;
2639 unsigned int next_offset
= 0;
2640 FILE_DIRECTORY_INFO
*entryptr
;
2642 if (bufstart
== NULL
)
2645 entryptr
= (FILE_DIRECTORY_INFO
*)bufstart
;
2648 entryptr
= (FILE_DIRECTORY_INFO
*)
2649 ((char *)entryptr
+ next_offset
);
2651 if ((char *)entryptr
+ size
> end_of_buf
) {
2652 cifs_dbg(VFS
, "malformed search entry would overflow\n");
2656 len
= le32_to_cpu(entryptr
->FileNameLength
);
2657 if ((char *)entryptr
+ len
+ size
> end_of_buf
) {
2658 cifs_dbg(VFS
, "directory entry name would overflow frame end of buf %p\n",
2663 *lastentry
= (char *)entryptr
;
2666 next_offset
= le32_to_cpu(entryptr
->NextEntryOffset
);
2678 SMB2_query_directory(const unsigned int xid
, struct cifs_tcon
*tcon
,
2679 u64 persistent_fid
, u64 volatile_fid
, int index
,
2680 struct cifs_search_info
*srch_inf
)
2682 struct smb2_query_directory_req
*req
;
2683 struct smb2_query_directory_rsp
*rsp
= NULL
;
2685 struct kvec rsp_iov
;
2688 int resp_buftype
= CIFS_NO_BUFFER
;
2689 unsigned char *bufptr
;
2690 struct TCP_Server_Info
*server
;
2691 struct cifs_ses
*ses
= tcon
->ses
;
2692 __le16 asteriks
= cpu_to_le16('*');
2694 unsigned int output_size
= CIFSMaxBufSize
;
2695 size_t info_buf_size
;
2697 if (ses
&& (ses
->server
))
2698 server
= ses
->server
;
2702 rc
= small_smb2_init(SMB2_QUERY_DIRECTORY
, tcon
, (void **) &req
);
2706 switch (srch_inf
->info_level
) {
2707 case SMB_FIND_FILE_DIRECTORY_INFO
:
2708 req
->FileInformationClass
= FILE_DIRECTORY_INFORMATION
;
2709 info_buf_size
= sizeof(FILE_DIRECTORY_INFO
) - 1;
2711 case SMB_FIND_FILE_ID_FULL_DIR_INFO
:
2712 req
->FileInformationClass
= FILEID_FULL_DIRECTORY_INFORMATION
;
2713 info_buf_size
= sizeof(SEARCH_ID_FULL_DIR_INFO
) - 1;
2716 cifs_dbg(VFS
, "info level %u isn't supported\n",
2717 srch_inf
->info_level
);
2722 req
->FileIndex
= cpu_to_le32(index
);
2723 req
->PersistentFileId
= persistent_fid
;
2724 req
->VolatileFileId
= volatile_fid
;
2727 bufptr
= req
->Buffer
;
2728 memcpy(bufptr
, &asteriks
, len
);
2730 req
->FileNameOffset
=
2731 cpu_to_le16(sizeof(struct smb2_query_directory_req
) - 1 - 4);
2732 req
->FileNameLength
= cpu_to_le16(len
);
2734 * BB could be 30 bytes or so longer if we used SMB2 specific
2735 * buffer lengths, but this is safe and close enough.
2737 output_size
= min_t(unsigned int, output_size
, server
->maxBuf
);
2738 output_size
= min_t(unsigned int, output_size
, 2 << 15);
2739 req
->OutputBufferLength
= cpu_to_le32(output_size
);
2741 iov
[0].iov_base
= (char *)req
;
2742 /* 4 for RFC1001 length and 1 for Buffer */
2743 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
2745 iov
[1].iov_base
= (char *)(req
->Buffer
);
2746 iov
[1].iov_len
= len
;
2748 inc_rfc1001_len(req
, len
- 1 /* Buffer */);
2750 rc
= SendReceive2(xid
, ses
, iov
, 2, &resp_buftype
, 0, &rsp_iov
);
2751 cifs_small_buf_release(req
);
2752 rsp
= (struct smb2_query_directory_rsp
*)rsp_iov
.iov_base
;
2755 if (rc
== -ENODATA
&&
2756 rsp
->hdr
.sync_hdr
.Status
== STATUS_NO_MORE_FILES
) {
2757 srch_inf
->endOfSearch
= true;
2760 cifs_stats_fail_inc(tcon
, SMB2_QUERY_DIRECTORY_HE
);
2764 rc
= validate_buf(le16_to_cpu(rsp
->OutputBufferOffset
),
2765 le32_to_cpu(rsp
->OutputBufferLength
), &rsp
->hdr
,
2770 srch_inf
->unicode
= true;
2772 if (srch_inf
->ntwrk_buf_start
) {
2773 if (srch_inf
->smallBuf
)
2774 cifs_small_buf_release(srch_inf
->ntwrk_buf_start
);
2776 cifs_buf_release(srch_inf
->ntwrk_buf_start
);
2778 srch_inf
->ntwrk_buf_start
= (char *)rsp
;
2779 srch_inf
->srch_entries_start
= srch_inf
->last_entry
= 4 /* rfclen */ +
2780 (char *)&rsp
->hdr
+ le16_to_cpu(rsp
->OutputBufferOffset
);
2781 /* 4 for rfc1002 length field */
2782 end_of_smb
= get_rfc1002_length(rsp
) + 4 + (char *)&rsp
->hdr
;
2783 srch_inf
->entries_in_buffer
=
2784 num_entries(srch_inf
->srch_entries_start
, end_of_smb
,
2785 &srch_inf
->last_entry
, info_buf_size
);
2786 srch_inf
->index_of_last_entry
+= srch_inf
->entries_in_buffer
;
2787 cifs_dbg(FYI
, "num entries %d last_index %lld srch start %p srch end %p\n",
2788 srch_inf
->entries_in_buffer
, srch_inf
->index_of_last_entry
,
2789 srch_inf
->srch_entries_start
, srch_inf
->last_entry
);
2790 if (resp_buftype
== CIFS_LARGE_BUFFER
)
2791 srch_inf
->smallBuf
= false;
2792 else if (resp_buftype
== CIFS_SMALL_BUFFER
)
2793 srch_inf
->smallBuf
= true;
2795 cifs_dbg(VFS
, "illegal search buffer type\n");
2800 free_rsp_buf(resp_buftype
, rsp
);
2805 send_set_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
2806 u64 persistent_fid
, u64 volatile_fid
, u32 pid
, int info_class
,
2807 unsigned int num
, void **data
, unsigned int *size
)
2809 struct smb2_set_info_req
*req
;
2810 struct smb2_set_info_rsp
*rsp
= NULL
;
2812 struct kvec rsp_iov
;
2816 struct TCP_Server_Info
*server
;
2817 struct cifs_ses
*ses
= tcon
->ses
;
2819 if (ses
&& (ses
->server
))
2820 server
= ses
->server
;
2827 iov
= kmalloc(sizeof(struct kvec
) * num
, GFP_KERNEL
);
2831 rc
= small_smb2_init(SMB2_SET_INFO
, tcon
, (void **) &req
);
2837 req
->hdr
.sync_hdr
.ProcessId
= cpu_to_le32(pid
);
2839 req
->InfoType
= SMB2_O_INFO_FILE
;
2840 req
->FileInfoClass
= info_class
;
2841 req
->PersistentFileId
= persistent_fid
;
2842 req
->VolatileFileId
= volatile_fid
;
2844 /* 4 for RFC1001 length and 1 for Buffer */
2846 cpu_to_le16(sizeof(struct smb2_set_info_req
) - 1 - 4);
2847 req
->BufferLength
= cpu_to_le32(*size
);
2849 inc_rfc1001_len(req
, *size
- 1 /* Buffer */);
2851 memcpy(req
->Buffer
, *data
, *size
);
2853 iov
[0].iov_base
= (char *)req
;
2854 /* 4 for RFC1001 length */
2855 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
2857 for (i
= 1; i
< num
; i
++) {
2858 inc_rfc1001_len(req
, size
[i
]);
2859 le32_add_cpu(&req
->BufferLength
, size
[i
]);
2860 iov
[i
].iov_base
= (char *)data
[i
];
2861 iov
[i
].iov_len
= size
[i
];
2864 rc
= SendReceive2(xid
, ses
, iov
, num
, &resp_buftype
, 0, &rsp_iov
);
2865 cifs_small_buf_release(req
);
2866 rsp
= (struct smb2_set_info_rsp
*)rsp_iov
.iov_base
;
2869 cifs_stats_fail_inc(tcon
, SMB2_SET_INFO_HE
);
2871 free_rsp_buf(resp_buftype
, rsp
);
2877 SMB2_rename(const unsigned int xid
, struct cifs_tcon
*tcon
,
2878 u64 persistent_fid
, u64 volatile_fid
, __le16
*target_file
)
2880 struct smb2_file_rename_info info
;
2882 unsigned int size
[2];
2884 int len
= (2 * UniStrnlen((wchar_t *)target_file
, PATH_MAX
));
2886 data
= kmalloc(sizeof(void *) * 2, GFP_KERNEL
);
2890 info
.ReplaceIfExists
= 1; /* 1 = replace existing target with new */
2891 /* 0 = fail if target already exists */
2892 info
.RootDirectory
= 0; /* MBZ for network ops (why does spec say?) */
2893 info
.FileNameLength
= cpu_to_le32(len
);
2896 size
[0] = sizeof(struct smb2_file_rename_info
);
2898 data
[1] = target_file
;
2899 size
[1] = len
+ 2 /* null */;
2901 rc
= send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2902 current
->tgid
, FILE_RENAME_INFORMATION
, 2, data
,
2909 SMB2_rmdir(const unsigned int xid
, struct cifs_tcon
*tcon
,
2910 u64 persistent_fid
, u64 volatile_fid
)
2912 __u8 delete_pending
= 1;
2916 data
= &delete_pending
;
2917 size
= 1; /* sizeof __u8 */
2919 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2920 current
->tgid
, FILE_DISPOSITION_INFORMATION
, 1, &data
,
2925 SMB2_set_hardlink(const unsigned int xid
, struct cifs_tcon
*tcon
,
2926 u64 persistent_fid
, u64 volatile_fid
, __le16
*target_file
)
2928 struct smb2_file_link_info info
;
2930 unsigned int size
[2];
2932 int len
= (2 * UniStrnlen((wchar_t *)target_file
, PATH_MAX
));
2934 data
= kmalloc(sizeof(void *) * 2, GFP_KERNEL
);
2938 info
.ReplaceIfExists
= 0; /* 1 = replace existing link with new */
2939 /* 0 = fail if link already exists */
2940 info
.RootDirectory
= 0; /* MBZ for network ops (why does spec say?) */
2941 info
.FileNameLength
= cpu_to_le32(len
);
2944 size
[0] = sizeof(struct smb2_file_link_info
);
2946 data
[1] = target_file
;
2947 size
[1] = len
+ 2 /* null */;
2949 rc
= send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2950 current
->tgid
, FILE_LINK_INFORMATION
, 2, data
, size
);
2956 SMB2_set_eof(const unsigned int xid
, struct cifs_tcon
*tcon
, u64 persistent_fid
,
2957 u64 volatile_fid
, u32 pid
, __le64
*eof
, bool is_falloc
)
2959 struct smb2_file_eof_info info
;
2963 info
.EndOfFile
= *eof
;
2966 size
= sizeof(struct smb2_file_eof_info
);
2969 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2970 pid
, FILE_ALLOCATION_INFORMATION
, 1, &data
, &size
);
2972 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2973 pid
, FILE_END_OF_FILE_INFORMATION
, 1, &data
, &size
);
2977 SMB2_set_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
2978 u64 persistent_fid
, u64 volatile_fid
, FILE_BASIC_INFO
*buf
)
2981 size
= sizeof(FILE_BASIC_INFO
);
2982 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2983 current
->tgid
, FILE_BASIC_INFORMATION
, 1,
2984 (void **)&buf
, &size
);
2988 SMB2_oplock_break(const unsigned int xid
, struct cifs_tcon
*tcon
,
2989 const u64 persistent_fid
, const u64 volatile_fid
,
2993 struct smb2_oplock_break
*req
= NULL
;
2995 cifs_dbg(FYI
, "SMB2_oplock_break\n");
2996 rc
= small_smb2_init(SMB2_OPLOCK_BREAK
, tcon
, (void **) &req
);
3001 req
->VolatileFid
= volatile_fid
;
3002 req
->PersistentFid
= persistent_fid
;
3003 req
->OplockLevel
= oplock_level
;
3004 req
->hdr
.sync_hdr
.CreditRequest
= cpu_to_le16(1);
3006 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) req
, CIFS_OBREAK_OP
);
3007 cifs_small_buf_release(req
);
3010 cifs_stats_fail_inc(tcon
, SMB2_OPLOCK_BREAK_HE
);
3011 cifs_dbg(FYI
, "Send error in Oplock Break = %d\n", rc
);
3018 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info
*pfs_inf
,
3019 struct kstatfs
*kst
)
3021 kst
->f_bsize
= le32_to_cpu(pfs_inf
->BytesPerSector
) *
3022 le32_to_cpu(pfs_inf
->SectorsPerAllocationUnit
);
3023 kst
->f_blocks
= le64_to_cpu(pfs_inf
->TotalAllocationUnits
);
3024 kst
->f_bfree
= le64_to_cpu(pfs_inf
->ActualAvailableAllocationUnits
);
3025 kst
->f_bavail
= le64_to_cpu(pfs_inf
->CallerAvailableAllocationUnits
);
3030 build_qfs_info_req(struct kvec
*iov
, struct cifs_tcon
*tcon
, int level
,
3031 int outbuf_len
, u64 persistent_fid
, u64 volatile_fid
)
3034 struct smb2_query_info_req
*req
;
3036 cifs_dbg(FYI
, "Query FSInfo level %d\n", level
);
3038 if ((tcon
->ses
== NULL
) || (tcon
->ses
->server
== NULL
))
3041 rc
= small_smb2_init(SMB2_QUERY_INFO
, tcon
, (void **) &req
);
3045 req
->InfoType
= SMB2_O_INFO_FILESYSTEM
;
3046 req
->FileInfoClass
= level
;
3047 req
->PersistentFileId
= persistent_fid
;
3048 req
->VolatileFileId
= volatile_fid
;
3049 /* 4 for rfc1002 length field and 1 for pad */
3050 req
->InputBufferOffset
=
3051 cpu_to_le16(sizeof(struct smb2_query_info_req
) - 1 - 4);
3052 req
->OutputBufferLength
= cpu_to_le32(
3053 outbuf_len
+ sizeof(struct smb2_query_info_rsp
) - 1 - 4);
3055 iov
->iov_base
= (char *)req
;
3056 /* 4 for rfc1002 length field */
3057 iov
->iov_len
= get_rfc1002_length(req
) + 4;
3062 SMB2_QFS_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
3063 u64 persistent_fid
, u64 volatile_fid
, struct kstatfs
*fsdata
)
3065 struct smb2_query_info_rsp
*rsp
= NULL
;
3067 struct kvec rsp_iov
;
3070 struct cifs_ses
*ses
= tcon
->ses
;
3071 struct smb2_fs_full_size_info
*info
= NULL
;
3073 rc
= build_qfs_info_req(&iov
, tcon
, FS_FULL_SIZE_INFORMATION
,
3074 sizeof(struct smb2_fs_full_size_info
),
3075 persistent_fid
, volatile_fid
);
3079 rc
= SendReceive2(xid
, ses
, &iov
, 1, &resp_buftype
, 0, &rsp_iov
);
3080 cifs_small_buf_release(iov
.iov_base
);
3082 cifs_stats_fail_inc(tcon
, SMB2_QUERY_INFO_HE
);
3085 rsp
= (struct smb2_query_info_rsp
*)rsp_iov
.iov_base
;
3087 info
= (struct smb2_fs_full_size_info
*)(4 /* RFC1001 len */ +
3088 le16_to_cpu(rsp
->OutputBufferOffset
) + (char *)&rsp
->hdr
);
3089 rc
= validate_buf(le16_to_cpu(rsp
->OutputBufferOffset
),
3090 le32_to_cpu(rsp
->OutputBufferLength
), &rsp
->hdr
,
3091 sizeof(struct smb2_fs_full_size_info
));
3093 copy_fs_info_to_kstatfs(info
, fsdata
);
3096 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
3101 SMB2_QFS_attr(const unsigned int xid
, struct cifs_tcon
*tcon
,
3102 u64 persistent_fid
, u64 volatile_fid
, int level
)
3104 struct smb2_query_info_rsp
*rsp
= NULL
;
3106 struct kvec rsp_iov
;
3108 int resp_buftype
, max_len
, min_len
;
3109 struct cifs_ses
*ses
= tcon
->ses
;
3110 unsigned int rsp_len
, offset
;
3112 if (level
== FS_DEVICE_INFORMATION
) {
3113 max_len
= sizeof(FILE_SYSTEM_DEVICE_INFO
);
3114 min_len
= sizeof(FILE_SYSTEM_DEVICE_INFO
);
3115 } else if (level
== FS_ATTRIBUTE_INFORMATION
) {
3116 max_len
= sizeof(FILE_SYSTEM_ATTRIBUTE_INFO
);
3117 min_len
= MIN_FS_ATTR_INFO_SIZE
;
3118 } else if (level
== FS_SECTOR_SIZE_INFORMATION
) {
3119 max_len
= sizeof(struct smb3_fs_ss_info
);
3120 min_len
= sizeof(struct smb3_fs_ss_info
);
3122 cifs_dbg(FYI
, "Invalid qfsinfo level %d\n", level
);
3126 rc
= build_qfs_info_req(&iov
, tcon
, level
, max_len
,
3127 persistent_fid
, volatile_fid
);
3131 rc
= SendReceive2(xid
, ses
, &iov
, 1, &resp_buftype
, 0, &rsp_iov
);
3132 cifs_small_buf_release(iov
.iov_base
);
3134 cifs_stats_fail_inc(tcon
, SMB2_QUERY_INFO_HE
);
3137 rsp
= (struct smb2_query_info_rsp
*)rsp_iov
.iov_base
;
3139 rsp_len
= le32_to_cpu(rsp
->OutputBufferLength
);
3140 offset
= le16_to_cpu(rsp
->OutputBufferOffset
);
3141 rc
= validate_buf(offset
, rsp_len
, &rsp
->hdr
, min_len
);
3145 if (level
== FS_ATTRIBUTE_INFORMATION
)
3146 memcpy(&tcon
->fsAttrInfo
, 4 /* RFC1001 len */ + offset
3147 + (char *)&rsp
->hdr
, min_t(unsigned int,
3149 else if (level
== FS_DEVICE_INFORMATION
)
3150 memcpy(&tcon
->fsDevInfo
, 4 /* RFC1001 len */ + offset
3151 + (char *)&rsp
->hdr
, sizeof(FILE_SYSTEM_DEVICE_INFO
));
3152 else if (level
== FS_SECTOR_SIZE_INFORMATION
) {
3153 struct smb3_fs_ss_info
*ss_info
= (struct smb3_fs_ss_info
*)
3154 (4 /* RFC1001 len */ + offset
+ (char *)&rsp
->hdr
);
3155 tcon
->ss_flags
= le32_to_cpu(ss_info
->Flags
);
3156 tcon
->perf_sector_size
=
3157 le32_to_cpu(ss_info
->PhysicalBytesPerSectorForPerf
);
3161 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
3166 smb2_lockv(const unsigned int xid
, struct cifs_tcon
*tcon
,
3167 const __u64 persist_fid
, const __u64 volatile_fid
, const __u32 pid
,
3168 const __u32 num_lock
, struct smb2_lock_element
*buf
)
3171 struct smb2_lock_req
*req
= NULL
;
3173 struct kvec rsp_iov
;
3177 cifs_dbg(FYI
, "smb2_lockv num lock %d\n", num_lock
);
3179 rc
= small_smb2_init(SMB2_LOCK
, tcon
, (void **) &req
);
3183 req
->hdr
.sync_hdr
.ProcessId
= cpu_to_le32(pid
);
3184 req
->LockCount
= cpu_to_le16(num_lock
);
3186 req
->PersistentFileId
= persist_fid
;
3187 req
->VolatileFileId
= volatile_fid
;
3189 count
= num_lock
* sizeof(struct smb2_lock_element
);
3190 inc_rfc1001_len(req
, count
- sizeof(struct smb2_lock_element
));
3192 iov
[0].iov_base
= (char *)req
;
3193 /* 4 for rfc1002 length field and count for all locks */
3194 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - count
;
3195 iov
[1].iov_base
= (char *)buf
;
3196 iov
[1].iov_len
= count
;
3198 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_locks
);
3199 rc
= SendReceive2(xid
, tcon
->ses
, iov
, 2, &resp_buf_type
, CIFS_NO_RESP
,
3201 cifs_small_buf_release(req
);
3203 cifs_dbg(FYI
, "Send error in smb2_lockv = %d\n", rc
);
3204 cifs_stats_fail_inc(tcon
, SMB2_LOCK_HE
);
3211 SMB2_lock(const unsigned int xid
, struct cifs_tcon
*tcon
,
3212 const __u64 persist_fid
, const __u64 volatile_fid
, const __u32 pid
,
3213 const __u64 length
, const __u64 offset
, const __u32 lock_flags
,
3216 struct smb2_lock_element lock
;
3218 lock
.Offset
= cpu_to_le64(offset
);
3219 lock
.Length
= cpu_to_le64(length
);
3220 lock
.Flags
= cpu_to_le32(lock_flags
);
3221 if (!wait
&& lock_flags
!= SMB2_LOCKFLAG_UNLOCK
)
3222 lock
.Flags
|= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY
);
3224 return smb2_lockv(xid
, tcon
, persist_fid
, volatile_fid
, pid
, 1, &lock
);
3228 SMB2_lease_break(const unsigned int xid
, struct cifs_tcon
*tcon
,
3229 __u8
*lease_key
, const __le32 lease_state
)
3232 struct smb2_lease_ack
*req
= NULL
;
3234 cifs_dbg(FYI
, "SMB2_lease_break\n");
3235 rc
= small_smb2_init(SMB2_OPLOCK_BREAK
, tcon
, (void **) &req
);
3240 req
->hdr
.sync_hdr
.CreditRequest
= cpu_to_le16(1);
3241 req
->StructureSize
= cpu_to_le16(36);
3242 inc_rfc1001_len(req
, 12);
3244 memcpy(req
->LeaseKey
, lease_key
, 16);
3245 req
->LeaseState
= lease_state
;
3247 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) req
, CIFS_OBREAK_OP
);
3248 cifs_small_buf_release(req
);
3251 cifs_stats_fail_inc(tcon
, SMB2_OPLOCK_BREAK_HE
);
3252 cifs_dbg(FYI
, "Send error in Lease Break = %d\n", rc
);