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_hdr
*hdr
, __le16 smb2_cmd
/* command */ ,
83 const struct cifs_tcon
*tcon
)
85 struct smb2_pdu
*pdu
= (struct smb2_pdu
*)hdr
;
86 struct smb2_sync_hdr
*shdr
= get_sync_hdr(hdr
);
87 char *temp
= (char *)hdr
;
88 /* lookup word count ie StructureSize from table */
89 __u16 parmsize
= smb2_req_struct_sizes
[le16_to_cpu(smb2_cmd
)];
92 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
93 * largest operations (Create)
97 /* Note this is only network field converted to big endian */
98 hdr
->smb2_buf_length
=
99 cpu_to_be32(parmsize
+ sizeof(struct smb2_sync_hdr
));
101 shdr
->ProtocolId
= SMB2_PROTO_NUMBER
;
102 shdr
->StructureSize
= cpu_to_le16(64);
103 shdr
->Command
= smb2_cmd
;
104 if (tcon
&& tcon
->ses
&& tcon
->ses
->server
) {
105 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
107 spin_lock(&server
->req_lock
);
108 /* Request up to 2 credits but don't go over the limit. */
109 if (server
->credits
>= server
->max_credits
)
110 shdr
->CreditRequest
= cpu_to_le16(0);
112 shdr
->CreditRequest
= cpu_to_le16(
113 min_t(int, server
->max_credits
-
114 server
->credits
, 2));
115 spin_unlock(&server
->req_lock
);
117 shdr
->CreditRequest
= cpu_to_le16(2);
119 shdr
->ProcessId
= cpu_to_le32((__u16
)current
->tgid
);
124 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
125 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
126 if ((tcon
->ses
) && (tcon
->ses
->server
) &&
127 (tcon
->ses
->server
->capabilities
& SMB2_GLOBAL_CAP_LARGE_MTU
))
128 shdr
->CreditCharge
= cpu_to_le16(1);
129 /* else CreditCharge MBZ */
131 shdr
->TreeId
= tcon
->tid
;
132 /* Uid is not converted */
134 shdr
->SessionId
= tcon
->ses
->Suid
;
137 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
138 * to pass the path on the Open SMB prefixed by \\server\share.
139 * Not sure when we would need to do the augmented path (if ever) and
140 * setting this flag breaks the SMB2 open operation since it is
141 * illegal to send an empty path name (without \\server\share prefix)
142 * when the DFS flag is set in the SMB open header. We could
143 * consider setting the flag on all operations other than open
144 * but it is safer to net set it for now.
146 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
147 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
149 if (tcon
->ses
&& tcon
->ses
->server
&& tcon
->ses
->server
->sign
)
150 shdr
->Flags
|= SMB2_FLAGS_SIGNED
;
152 pdu
->StructureSize2
= cpu_to_le16(parmsize
);
157 smb2_reconnect(__le16 smb2_command
, struct cifs_tcon
*tcon
)
160 struct nls_table
*nls_codepage
;
161 struct cifs_ses
*ses
;
162 struct TCP_Server_Info
*server
;
165 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
166 * check for tcp and smb session status done differently
167 * for those three - in the calling routine.
172 if (smb2_command
== SMB2_TREE_CONNECT
)
175 if (tcon
->tidStatus
== CifsExiting
) {
177 * only tree disconnect, open, and write,
178 * (and ulogoff which does not have tcon)
179 * are allowed as we start force umount.
181 if ((smb2_command
!= SMB2_WRITE
) &&
182 (smb2_command
!= SMB2_CREATE
) &&
183 (smb2_command
!= SMB2_TREE_DISCONNECT
)) {
184 cifs_dbg(FYI
, "can not send cmd %d while umounting\n",
189 if ((!tcon
->ses
) || (tcon
->ses
->status
== CifsExiting
) ||
190 (!tcon
->ses
->server
))
194 server
= ses
->server
;
197 * Give demultiplex thread up to 10 seconds to reconnect, should be
198 * greater than cifs socket timeout which is 7 seconds
200 while (server
->tcpStatus
== CifsNeedReconnect
) {
202 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
203 * here since they are implicitly done when session drops.
205 switch (smb2_command
) {
207 * BB Should we keep oplock break and add flush to exceptions?
209 case SMB2_TREE_DISCONNECT
:
212 case SMB2_OPLOCK_BREAK
:
216 wait_event_interruptible_timeout(server
->response_q
,
217 (server
->tcpStatus
!= CifsNeedReconnect
), 10 * HZ
);
219 /* are we still trying to reconnect? */
220 if (server
->tcpStatus
!= CifsNeedReconnect
)
224 * on "soft" mounts we wait once. Hard mounts keep
225 * retrying until process is killed or server comes
229 cifs_dbg(FYI
, "gave up waiting on reconnect in smb_init\n");
234 if (!tcon
->ses
->need_reconnect
&& !tcon
->need_reconnect
)
237 nls_codepage
= load_nls_default();
240 * need to prevent multiple threads trying to simultaneously reconnect
241 * the same SMB session
243 mutex_lock(&tcon
->ses
->session_mutex
);
244 rc
= cifs_negotiate_protocol(0, tcon
->ses
);
245 if (!rc
&& tcon
->ses
->need_reconnect
)
246 rc
= cifs_setup_session(0, tcon
->ses
, nls_codepage
);
248 if (rc
|| !tcon
->need_reconnect
) {
249 mutex_unlock(&tcon
->ses
->session_mutex
);
253 cifs_mark_open_files_invalid(tcon
);
254 if (tcon
->use_persistent
)
255 tcon
->need_reopen_files
= true;
257 rc
= SMB2_tcon(0, tcon
->ses
, tcon
->treeName
, tcon
, nls_codepage
);
258 mutex_unlock(&tcon
->ses
->session_mutex
);
260 cifs_dbg(FYI
, "reconnect tcon rc = %d\n", rc
);
264 if (smb2_command
!= SMB2_INTERNAL_CMD
)
265 queue_delayed_work(cifsiod_wq
, &server
->reconnect
, 0);
267 atomic_inc(&tconInfoReconnectCount
);
270 * Check if handle based operation so we know whether we can continue
271 * or not without returning to caller to reset file handle.
274 * BB Is flush done by server on drop of tcp session? Should we special
275 * case it and skip above?
277 switch (smb2_command
) {
283 case SMB2_QUERY_DIRECTORY
:
284 case SMB2_CHANGE_NOTIFY
:
285 case SMB2_QUERY_INFO
:
289 unload_nls(nls_codepage
);
294 * Allocate and return pointer to an SMB request hdr, and set basic
295 * SMB information in the SMB header. If the return code is zero, this
296 * function must have filled in request_buf pointer.
299 small_smb2_init(__le16 smb2_command
, struct cifs_tcon
*tcon
,
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 smb2_hdr_assemble((struct smb2_hdr
*) *request_buf
, smb2_command
, tcon
);
318 #ifdef CONFIG_CIFS_STATS2
319 uint16_t com_code
= le16_to_cpu(smb2_command
);
320 cifs_stats_inc(&tcon
->stats
.smb2_stats
.smb2_com_sent
[com_code
]);
322 cifs_stats_inc(&tcon
->num_smbs_sent
);
328 #ifdef CONFIG_CIFS_SMB311
329 /* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
330 #define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
333 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
334 #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
337 build_preauth_ctxt(struct smb2_preauth_neg_context
*pneg_ctxt
)
339 pneg_ctxt
->ContextType
= SMB2_PREAUTH_INTEGRITY_CAPABILITIES
;
340 pneg_ctxt
->DataLength
= cpu_to_le16(38);
341 pneg_ctxt
->HashAlgorithmCount
= cpu_to_le16(1);
342 pneg_ctxt
->SaltLength
= cpu_to_le16(SMB311_SALT_SIZE
);
343 get_random_bytes(pneg_ctxt
->Salt
, SMB311_SALT_SIZE
);
344 pneg_ctxt
->HashAlgorithms
= SMB2_PREAUTH_INTEGRITY_SHA512
;
348 build_encrypt_ctxt(struct smb2_encryption_neg_context
*pneg_ctxt
)
350 pneg_ctxt
->ContextType
= SMB2_ENCRYPTION_CAPABILITIES
;
351 pneg_ctxt
->DataLength
= cpu_to_le16(6);
352 pneg_ctxt
->CipherCount
= cpu_to_le16(2);
353 pneg_ctxt
->Ciphers
[0] = SMB2_ENCRYPTION_AES128_GCM
;
354 pneg_ctxt
->Ciphers
[1] = SMB2_ENCRYPTION_AES128_CCM
;
358 assemble_neg_contexts(struct smb2_negotiate_req
*req
)
361 /* +4 is to account for the RFC1001 len field */
362 char *pneg_ctxt
= (char *)req
+ OFFSET_OF_NEG_CONTEXT
+ 4;
364 build_preauth_ctxt((struct smb2_preauth_neg_context
*)pneg_ctxt
);
365 /* Add 2 to size to round to 8 byte boundary */
366 pneg_ctxt
+= 2 + sizeof(struct smb2_preauth_neg_context
);
367 build_encrypt_ctxt((struct smb2_encryption_neg_context
*)pneg_ctxt
);
368 req
->NegotiateContextOffset
= cpu_to_le32(OFFSET_OF_NEG_CONTEXT
);
369 req
->NegotiateContextCount
= cpu_to_le16(2);
370 inc_rfc1001_len(req
, 4 + sizeof(struct smb2_preauth_neg_context
) + 2
371 + sizeof(struct smb2_encryption_neg_context
)); /* calculate hash */
374 static void assemble_neg_contexts(struct smb2_negotiate_req
*req
)
383 * SMB2 Worker functions follow:
385 * The general structure of the worker functions is:
386 * 1) Call smb2_init (assembles SMB2 header)
387 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
388 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
389 * 4) Decode SMB2 command specific fields in the fixed length area
390 * 5) Decode variable length data area (if any for this SMB2 command type)
391 * 6) Call free smb buffer
397 SMB2_negotiate(const unsigned int xid
, struct cifs_ses
*ses
)
399 struct smb2_negotiate_req
*req
;
400 struct smb2_negotiate_rsp
*rsp
;
404 struct TCP_Server_Info
*server
= ses
->server
;
405 int blob_offset
, blob_length
;
407 int flags
= CIFS_NEG_OP
;
409 cifs_dbg(FYI
, "Negotiate protocol\n");
412 WARN(1, "%s: server is NULL!\n", __func__
);
416 rc
= small_smb2_init(SMB2_NEGOTIATE
, NULL
, (void **) &req
);
420 req
->hdr
.sync_hdr
.SessionId
= 0;
422 req
->Dialects
[0] = cpu_to_le16(ses
->server
->vals
->protocol_id
);
424 req
->DialectCount
= cpu_to_le16(1); /* One vers= at a time for now */
425 inc_rfc1001_len(req
, 2);
427 /* only one of SMB2 signing flags may be set in SMB2 request */
429 req
->SecurityMode
= cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED
);
430 else if (global_secflags
& CIFSSEC_MAY_SIGN
)
431 req
->SecurityMode
= cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED
);
433 req
->SecurityMode
= 0;
435 req
->Capabilities
= cpu_to_le32(ses
->server
->vals
->req_capabilities
);
437 /* ClientGUID must be zero for SMB2.02 dialect */
438 if (ses
->server
->vals
->protocol_id
== SMB20_PROT_ID
)
439 memset(req
->ClientGUID
, 0, SMB2_CLIENT_GUID_SIZE
);
441 memcpy(req
->ClientGUID
, server
->client_guid
,
442 SMB2_CLIENT_GUID_SIZE
);
443 if (ses
->server
->vals
->protocol_id
== SMB311_PROT_ID
)
444 assemble_neg_contexts(req
);
446 iov
[0].iov_base
= (char *)req
;
447 /* 4 for rfc1002 length field */
448 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
450 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buftype
, flags
);
452 rsp
= (struct smb2_negotiate_rsp
*)iov
[0].iov_base
;
454 * No tcon so can't do
455 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
460 cifs_dbg(FYI
, "mode 0x%x\n", rsp
->SecurityMode
);
462 /* BB we may eventually want to match the negotiated vs. requested
463 dialect, even though we are only requesting one at a time */
464 if (rsp
->DialectRevision
== cpu_to_le16(SMB20_PROT_ID
))
465 cifs_dbg(FYI
, "negotiated smb2.0 dialect\n");
466 else if (rsp
->DialectRevision
== cpu_to_le16(SMB21_PROT_ID
))
467 cifs_dbg(FYI
, "negotiated smb2.1 dialect\n");
468 else if (rsp
->DialectRevision
== cpu_to_le16(SMB30_PROT_ID
))
469 cifs_dbg(FYI
, "negotiated smb3.0 dialect\n");
470 else if (rsp
->DialectRevision
== cpu_to_le16(SMB302_PROT_ID
))
471 cifs_dbg(FYI
, "negotiated smb3.02 dialect\n");
472 #ifdef CONFIG_CIFS_SMB311
473 else if (rsp
->DialectRevision
== cpu_to_le16(SMB311_PROT_ID
))
474 cifs_dbg(FYI
, "negotiated smb3.1.1 dialect\n");
477 cifs_dbg(VFS
, "Illegal dialect returned by server 0x%x\n",
478 le16_to_cpu(rsp
->DialectRevision
));
482 server
->dialect
= le16_to_cpu(rsp
->DialectRevision
);
484 /* SMB2 only has an extended negflavor */
485 server
->negflavor
= CIFS_NEGFLAVOR_EXTENDED
;
486 /* set it to the maximum buffer size value we can send with 1 credit */
487 server
->maxBuf
= min_t(unsigned int, le32_to_cpu(rsp
->MaxTransactSize
),
488 SMB2_MAX_BUFFER_SIZE
);
489 server
->max_read
= le32_to_cpu(rsp
->MaxReadSize
);
490 server
->max_write
= le32_to_cpu(rsp
->MaxWriteSize
);
491 /* BB Do we need to validate the SecurityMode? */
492 server
->sec_mode
= le16_to_cpu(rsp
->SecurityMode
);
493 server
->capabilities
= le32_to_cpu(rsp
->Capabilities
);
495 server
->capabilities
|= SMB2_NT_FIND
| SMB2_LARGE_FILES
;
497 security_blob
= smb2_get_data_area_len(&blob_offset
, &blob_length
,
500 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
502 * ses->sectype = RawNTLMSSP;
503 * but for time being this is our only auth choice so doesn't matter.
504 * We just found a server which sets blob length to zero expecting raw.
506 if (blob_length
== 0)
507 cifs_dbg(FYI
, "missing security blob on negprot\n");
509 rc
= cifs_enable_signing(server
, ses
->sign
);
513 rc
= decode_negTokenInit(security_blob
, blob_length
, server
);
520 free_rsp_buf(resp_buftype
, rsp
);
524 int smb3_validate_negotiate(const unsigned int xid
, struct cifs_tcon
*tcon
)
527 struct validate_negotiate_info_req vneg_inbuf
;
528 struct validate_negotiate_info_rsp
*pneg_rsp
;
531 cifs_dbg(FYI
, "validate negotiate\n");
534 * validation ioctl must be signed, so no point sending this if we
535 * can not sign it. We could eventually change this to selectively
536 * sign just this, the first and only signed request on a connection.
537 * This is good enough for now since a user who wants better security
538 * would also enable signing on the mount. Having validation of
539 * negotiate info for signed connections helps reduce attack vectors
541 if (tcon
->ses
->server
->sign
== false)
542 return 0; /* validation requires signing */
544 vneg_inbuf
.Capabilities
=
545 cpu_to_le32(tcon
->ses
->server
->vals
->req_capabilities
);
546 memcpy(vneg_inbuf
.Guid
, tcon
->ses
->server
->client_guid
,
547 SMB2_CLIENT_GUID_SIZE
);
550 vneg_inbuf
.SecurityMode
=
551 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED
);
552 else if (global_secflags
& CIFSSEC_MAY_SIGN
)
553 vneg_inbuf
.SecurityMode
=
554 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED
);
556 vneg_inbuf
.SecurityMode
= 0;
558 vneg_inbuf
.DialectCount
= cpu_to_le16(1);
559 vneg_inbuf
.Dialects
[0] =
560 cpu_to_le16(tcon
->ses
->server
->vals
->protocol_id
);
562 rc
= SMB2_ioctl(xid
, tcon
, NO_FILE_ID
, NO_FILE_ID
,
563 FSCTL_VALIDATE_NEGOTIATE_INFO
, true /* is_fsctl */,
564 (char *)&vneg_inbuf
, sizeof(struct validate_negotiate_info_req
),
565 (char **)&pneg_rsp
, &rsplen
);
568 cifs_dbg(VFS
, "validate protocol negotiate failed: %d\n", rc
);
572 if (rsplen
!= sizeof(struct validate_negotiate_info_rsp
)) {
573 cifs_dbg(VFS
, "invalid size of protocol negotiate response\n");
577 /* check validate negotiate info response matches what we got earlier */
578 if (pneg_rsp
->Dialect
!=
579 cpu_to_le16(tcon
->ses
->server
->vals
->protocol_id
))
582 if (pneg_rsp
->SecurityMode
!= cpu_to_le16(tcon
->ses
->server
->sec_mode
))
585 /* do not validate server guid because not saved at negprot time yet */
587 if ((le32_to_cpu(pneg_rsp
->Capabilities
) | SMB2_NT_FIND
|
588 SMB2_LARGE_FILES
) != tcon
->ses
->server
->capabilities
)
591 /* validate negotiate successful */
592 cifs_dbg(FYI
, "validate negotiate info successful\n");
596 cifs_dbg(VFS
, "protocol revalidation - security settings mismatch\n");
600 struct SMB2_sess_data
{
602 struct cifs_ses
*ses
;
603 struct nls_table
*nls_cp
;
604 void (*func
)(struct SMB2_sess_data
*);
606 u64 previous_session
;
608 /* we will send the SMB in three pieces:
609 * a fixed length beginning part, an optional
610 * SPNEGO blob (which can be zero length), and a
611 * last part which will include the strings
612 * and rest of bcc area. This allows us to avoid
613 * a large buffer 17K allocation
620 SMB2_sess_alloc_buffer(struct SMB2_sess_data
*sess_data
)
623 struct cifs_ses
*ses
= sess_data
->ses
;
624 struct smb2_sess_setup_req
*req
;
625 struct TCP_Server_Info
*server
= ses
->server
;
627 rc
= small_smb2_init(SMB2_SESSION_SETUP
, NULL
, (void **) &req
);
631 /* First session, not a reauthenticate */
632 req
->hdr
.sync_hdr
.SessionId
= 0;
634 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
635 req
->PreviousSessionId
= sess_data
->previous_session
;
637 req
->Flags
= 0; /* MBZ */
638 /* to enable echos and oplocks */
639 req
->hdr
.sync_hdr
.CreditRequest
= cpu_to_le16(3);
641 /* only one of SMB2 signing flags may be set in SMB2 request */
643 req
->SecurityMode
= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
644 else if (global_secflags
& CIFSSEC_MAY_SIGN
) /* one flag unlike MUST_ */
645 req
->SecurityMode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
647 req
->SecurityMode
= 0;
649 req
->Capabilities
= 0;
650 req
->Channel
= 0; /* MBZ */
652 sess_data
->iov
[0].iov_base
= (char *)req
;
653 /* 4 for rfc1002 length field and 1 for pad */
654 sess_data
->iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
656 * This variable will be used to clear the buffer
657 * allocated above in case of any error in the calling function.
659 sess_data
->buf0_type
= CIFS_SMALL_BUFFER
;
665 SMB2_sess_free_buffer(struct SMB2_sess_data
*sess_data
)
667 free_rsp_buf(sess_data
->buf0_type
, sess_data
->iov
[0].iov_base
);
668 sess_data
->buf0_type
= CIFS_NO_BUFFER
;
672 SMB2_sess_sendreceive(struct SMB2_sess_data
*sess_data
)
675 struct smb2_sess_setup_req
*req
= sess_data
->iov
[0].iov_base
;
677 /* Testing shows that buffer offset must be at location of Buffer[0] */
678 req
->SecurityBufferOffset
=
679 cpu_to_le16(sizeof(struct smb2_sess_setup_req
) -
680 1 /* pad */ - 4 /* rfc1001 len */);
681 req
->SecurityBufferLength
= cpu_to_le16(sess_data
->iov
[1].iov_len
);
683 inc_rfc1001_len(req
, sess_data
->iov
[1].iov_len
- 1 /* pad */);
685 /* BB add code to build os and lm fields */
687 rc
= SendReceive2(sess_data
->xid
, sess_data
->ses
,
689 &sess_data
->buf0_type
,
690 CIFS_LOG_ERROR
| CIFS_NEG_OP
);
696 SMB2_sess_establish_session(struct SMB2_sess_data
*sess_data
)
699 struct cifs_ses
*ses
= sess_data
->ses
;
701 mutex_lock(&ses
->server
->srv_mutex
);
702 if (ses
->server
->sign
&& ses
->server
->ops
->generate_signingkey
) {
703 rc
= ses
->server
->ops
->generate_signingkey(ses
);
704 kfree(ses
->auth_key
.response
);
705 ses
->auth_key
.response
= NULL
;
708 "SMB3 session key generation failed\n");
709 mutex_unlock(&ses
->server
->srv_mutex
);
713 if (!ses
->server
->session_estab
) {
714 ses
->server
->sequence_number
= 0x2;
715 ses
->server
->session_estab
= true;
717 mutex_unlock(&ses
->server
->srv_mutex
);
719 cifs_dbg(FYI
, "SMB2/3 session established successfully\n");
720 spin_lock(&GlobalMid_Lock
);
721 ses
->status
= CifsGood
;
722 ses
->need_reconnect
= false;
723 spin_unlock(&GlobalMid_Lock
);
726 if (!ses
->server
->sign
) {
727 kfree(ses
->auth_key
.response
);
728 ses
->auth_key
.response
= NULL
;
733 #ifdef CONFIG_CIFS_UPCALL
735 SMB2_auth_kerberos(struct SMB2_sess_data
*sess_data
)
738 struct cifs_ses
*ses
= sess_data
->ses
;
739 struct cifs_spnego_msg
*msg
;
740 struct key
*spnego_key
= NULL
;
741 struct smb2_sess_setup_rsp
*rsp
= NULL
;
743 rc
= SMB2_sess_alloc_buffer(sess_data
);
747 spnego_key
= cifs_get_spnego_key(ses
);
748 if (IS_ERR(spnego_key
)) {
749 rc
= PTR_ERR(spnego_key
);
754 msg
= spnego_key
->payload
.data
[0];
756 * check version field to make sure that cifs.upcall is
757 * sending us a response in an expected form
759 if (msg
->version
!= CIFS_SPNEGO_UPCALL_VERSION
) {
761 "bad cifs.upcall version. Expected %d got %d",
762 CIFS_SPNEGO_UPCALL_VERSION
, msg
->version
);
764 goto out_put_spnego_key
;
767 ses
->auth_key
.response
= kmemdup(msg
->data
, msg
->sesskey_len
,
769 if (!ses
->auth_key
.response
) {
771 "Kerberos can't allocate (%u bytes) memory",
774 goto out_put_spnego_key
;
776 ses
->auth_key
.len
= msg
->sesskey_len
;
778 sess_data
->iov
[1].iov_base
= msg
->data
+ msg
->sesskey_len
;
779 sess_data
->iov
[1].iov_len
= msg
->secblob_len
;
781 rc
= SMB2_sess_sendreceive(sess_data
);
783 goto out_put_spnego_key
;
785 rsp
= (struct smb2_sess_setup_rsp
*)sess_data
->iov
[0].iov_base
;
786 ses
->Suid
= rsp
->hdr
.sync_hdr
.SessionId
;
788 ses
->session_flags
= le16_to_cpu(rsp
->SessionFlags
);
789 if (ses
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
)
790 cifs_dbg(VFS
, "SMB3 encryption not supported yet\n");
792 rc
= SMB2_sess_establish_session(sess_data
);
794 key_invalidate(spnego_key
);
797 sess_data
->result
= rc
;
798 sess_data
->func
= NULL
;
799 SMB2_sess_free_buffer(sess_data
);
803 SMB2_auth_kerberos(struct SMB2_sess_data
*sess_data
)
805 cifs_dbg(VFS
, "Kerberos negotiated but upcall support disabled!\n");
806 sess_data
->result
= -EOPNOTSUPP
;
807 sess_data
->func
= NULL
;
812 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data
*sess_data
);
815 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data
*sess_data
)
818 struct cifs_ses
*ses
= sess_data
->ses
;
819 struct smb2_sess_setup_rsp
*rsp
= NULL
;
820 char *ntlmssp_blob
= NULL
;
821 bool use_spnego
= false; /* else use raw ntlmssp */
825 * If memory allocation is successful, caller of this function
828 ses
->ntlmssp
= kmalloc(sizeof(struct ntlmssp_auth
), GFP_KERNEL
);
833 ses
->ntlmssp
->sesskey_per_smbsess
= true;
835 rc
= SMB2_sess_alloc_buffer(sess_data
);
839 ntlmssp_blob
= kmalloc(sizeof(struct _NEGOTIATE_MESSAGE
),
841 if (ntlmssp_blob
== NULL
) {
846 build_ntlmssp_negotiate_blob(ntlmssp_blob
, ses
);
848 /* BB eventually need to add this */
849 cifs_dbg(VFS
, "spnego not supported for SMB2 yet\n");
853 blob_length
= sizeof(struct _NEGOTIATE_MESSAGE
);
854 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
856 sess_data
->iov
[1].iov_base
= ntlmssp_blob
;
857 sess_data
->iov
[1].iov_len
= blob_length
;
859 rc
= SMB2_sess_sendreceive(sess_data
);
860 rsp
= (struct smb2_sess_setup_rsp
*)sess_data
->iov
[0].iov_base
;
862 /* If true, rc here is expected and not an error */
863 if (sess_data
->buf0_type
!= CIFS_NO_BUFFER
&&
864 rsp
->hdr
.sync_hdr
.Status
== STATUS_MORE_PROCESSING_REQUIRED
)
870 if (offsetof(struct smb2_sess_setup_rsp
, Buffer
) - 4 !=
871 le16_to_cpu(rsp
->SecurityBufferOffset
)) {
872 cifs_dbg(VFS
, "Invalid security buffer offset %d\n",
873 le16_to_cpu(rsp
->SecurityBufferOffset
));
877 rc
= decode_ntlmssp_challenge(rsp
->Buffer
,
878 le16_to_cpu(rsp
->SecurityBufferLength
), ses
);
882 cifs_dbg(FYI
, "rawntlmssp session setup challenge phase\n");
885 ses
->Suid
= rsp
->hdr
.sync_hdr
.SessionId
;
886 ses
->session_flags
= le16_to_cpu(rsp
->SessionFlags
);
887 if (ses
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
)
888 cifs_dbg(VFS
, "SMB3 encryption not supported yet\n");
892 SMB2_sess_free_buffer(sess_data
);
894 sess_data
->result
= 0;
895 sess_data
->func
= SMB2_sess_auth_rawntlmssp_authenticate
;
901 sess_data
->result
= rc
;
902 sess_data
->func
= NULL
;
906 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data
*sess_data
)
909 struct cifs_ses
*ses
= sess_data
->ses
;
910 struct smb2_sess_setup_req
*req
;
911 struct smb2_sess_setup_rsp
*rsp
= NULL
;
912 unsigned char *ntlmssp_blob
= NULL
;
913 bool use_spnego
= false; /* else use raw ntlmssp */
916 rc
= SMB2_sess_alloc_buffer(sess_data
);
920 req
= (struct smb2_sess_setup_req
*) sess_data
->iov
[0].iov_base
;
921 req
->hdr
.sync_hdr
.SessionId
= ses
->Suid
;
923 rc
= build_ntlmssp_auth_blob(&ntlmssp_blob
, &blob_length
, ses
,
926 cifs_dbg(FYI
, "build_ntlmssp_auth_blob failed %d\n", rc
);
931 /* BB eventually need to add this */
932 cifs_dbg(VFS
, "spnego not supported for SMB2 yet\n");
936 sess_data
->iov
[1].iov_base
= ntlmssp_blob
;
937 sess_data
->iov
[1].iov_len
= blob_length
;
939 rc
= SMB2_sess_sendreceive(sess_data
);
943 rsp
= (struct smb2_sess_setup_rsp
*)sess_data
->iov
[0].iov_base
;
945 ses
->Suid
= rsp
->hdr
.sync_hdr
.SessionId
;
946 ses
->session_flags
= le16_to_cpu(rsp
->SessionFlags
);
947 if (ses
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
)
948 cifs_dbg(VFS
, "SMB3 encryption not supported yet\n");
950 rc
= SMB2_sess_establish_session(sess_data
);
953 SMB2_sess_free_buffer(sess_data
);
956 sess_data
->result
= rc
;
957 sess_data
->func
= NULL
;
961 SMB2_select_sec(struct cifs_ses
*ses
, struct SMB2_sess_data
*sess_data
)
963 if (ses
->sectype
!= Kerberos
&& ses
->sectype
!= RawNTLMSSP
)
964 ses
->sectype
= RawNTLMSSP
;
966 switch (ses
->sectype
) {
968 sess_data
->func
= SMB2_auth_kerberos
;
971 sess_data
->func
= SMB2_sess_auth_rawntlmssp_negotiate
;
974 cifs_dbg(VFS
, "secType %d not supported!\n", ses
->sectype
);
982 SMB2_sess_setup(const unsigned int xid
, struct cifs_ses
*ses
,
983 const struct nls_table
*nls_cp
)
986 struct TCP_Server_Info
*server
= ses
->server
;
987 struct SMB2_sess_data
*sess_data
;
989 cifs_dbg(FYI
, "Session Setup\n");
992 WARN(1, "%s: server is NULL!\n", __func__
);
996 sess_data
= kzalloc(sizeof(struct SMB2_sess_data
), GFP_KERNEL
);
1000 rc
= SMB2_select_sec(ses
, sess_data
);
1003 sess_data
->xid
= xid
;
1004 sess_data
->ses
= ses
;
1005 sess_data
->buf0_type
= CIFS_NO_BUFFER
;
1006 sess_data
->nls_cp
= (struct nls_table
*) nls_cp
;
1008 while (sess_data
->func
)
1009 sess_data
->func(sess_data
);
1011 rc
= sess_data
->result
;
1018 SMB2_logoff(const unsigned int xid
, struct cifs_ses
*ses
)
1020 struct smb2_logoff_req
*req
; /* response is also trivial struct */
1022 struct TCP_Server_Info
*server
;
1024 cifs_dbg(FYI
, "disconnect session %p\n", ses
);
1026 if (ses
&& (ses
->server
))
1027 server
= ses
->server
;
1031 /* no need to send SMB logoff if uid already closed due to reconnect */
1032 if (ses
->need_reconnect
)
1033 goto smb2_session_already_dead
;
1035 rc
= small_smb2_init(SMB2_LOGOFF
, NULL
, (void **) &req
);
1039 /* since no tcon, smb2_init can not do this, so do here */
1040 req
->hdr
.sync_hdr
.SessionId
= ses
->Suid
;
1042 req
->hdr
.sync_hdr
.Flags
|= SMB2_FLAGS_SIGNED
;
1044 rc
= SendReceiveNoRsp(xid
, ses
, (char *) &req
->hdr
, 0);
1046 * No tcon so can't do
1047 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1050 smb2_session_already_dead
:
1054 static inline void cifs_stats_fail_inc(struct cifs_tcon
*tcon
, uint16_t code
)
1056 cifs_stats_inc(&tcon
->stats
.smb2_stats
.smb2_com_failed
[code
]);
1059 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1061 /* These are similar values to what Windows uses */
1062 static inline void init_copy_chunk_defaults(struct cifs_tcon
*tcon
)
1064 tcon
->max_chunks
= 256;
1065 tcon
->max_bytes_chunk
= 1048576;
1066 tcon
->max_bytes_copy
= 16777216;
1070 SMB2_tcon(const unsigned int xid
, struct cifs_ses
*ses
, const char *tree
,
1071 struct cifs_tcon
*tcon
, const struct nls_table
*cp
)
1073 struct smb2_tree_connect_req
*req
;
1074 struct smb2_tree_connect_rsp
*rsp
= NULL
;
1079 struct TCP_Server_Info
*server
;
1080 __le16
*unc_path
= NULL
;
1082 cifs_dbg(FYI
, "TCON\n");
1084 if ((ses
->server
) && tree
)
1085 server
= ses
->server
;
1089 if (tcon
&& tcon
->bad_network_name
)
1092 if ((tcon
&& tcon
->seal
) &&
1093 ((ses
->server
->capabilities
& SMB2_GLOBAL_CAP_ENCRYPTION
) == 0)) {
1094 cifs_dbg(VFS
, "encryption requested but no server support");
1098 unc_path
= kmalloc(MAX_SHARENAME_LENGTH
* 2, GFP_KERNEL
);
1099 if (unc_path
== NULL
)
1102 unc_path_len
= cifs_strtoUTF16(unc_path
, tree
, strlen(tree
), cp
) + 1;
1104 if (unc_path_len
< 2) {
1109 rc
= small_smb2_init(SMB2_TREE_CONNECT
, tcon
, (void **) &req
);
1116 /* since no tcon, smb2_init can not do this, so do here */
1117 req
->hdr
.sync_hdr
.SessionId
= ses
->Suid
;
1118 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
1119 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
1122 iov
[0].iov_base
= (char *)req
;
1123 /* 4 for rfc1002 length field and 1 for pad */
1124 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
1126 /* Testing shows that buffer offset must be at location of Buffer[0] */
1127 req
->PathOffset
= cpu_to_le16(sizeof(struct smb2_tree_connect_req
)
1128 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1129 req
->PathLength
= cpu_to_le16(unc_path_len
- 2);
1130 iov
[1].iov_base
= unc_path
;
1131 iov
[1].iov_len
= unc_path_len
;
1133 inc_rfc1001_len(req
, unc_path_len
- 1 /* pad */);
1135 rc
= SendReceive2(xid
, ses
, iov
, 2, &resp_buftype
, 0);
1136 rsp
= (struct smb2_tree_connect_rsp
*)iov
[0].iov_base
;
1140 cifs_stats_fail_inc(tcon
, SMB2_TREE_CONNECT_HE
);
1141 tcon
->need_reconnect
= true;
1143 goto tcon_error_exit
;
1147 ses
->ipc_tid
= rsp
->hdr
.sync_hdr
.TreeId
;
1151 if (rsp
->ShareType
& SMB2_SHARE_TYPE_DISK
)
1152 cifs_dbg(FYI
, "connection to disk share\n");
1153 else if (rsp
->ShareType
& SMB2_SHARE_TYPE_PIPE
) {
1155 cifs_dbg(FYI
, "connection to pipe share\n");
1156 } else if (rsp
->ShareType
& SMB2_SHARE_TYPE_PRINT
) {
1158 cifs_dbg(FYI
, "connection to printer\n");
1160 cifs_dbg(VFS
, "unknown share type %d\n", rsp
->ShareType
);
1162 goto tcon_error_exit
;
1165 tcon
->share_flags
= le32_to_cpu(rsp
->ShareFlags
);
1166 tcon
->capabilities
= rsp
->Capabilities
; /* we keep caps little endian */
1167 tcon
->maximal_access
= le32_to_cpu(rsp
->MaximalAccess
);
1168 tcon
->tidStatus
= CifsGood
;
1169 tcon
->need_reconnect
= false;
1170 tcon
->tid
= rsp
->hdr
.sync_hdr
.TreeId
;
1171 strlcpy(tcon
->treeName
, tree
, sizeof(tcon
->treeName
));
1173 if ((rsp
->Capabilities
& SMB2_SHARE_CAP_DFS
) &&
1174 ((tcon
->share_flags
& SHI1005_FLAGS_DFS
) == 0))
1175 cifs_dbg(VFS
, "DFS capability contradicts DFS flag\n");
1176 init_copy_chunk_defaults(tcon
);
1177 if (tcon
->share_flags
& SHI1005_FLAGS_ENCRYPT_DATA
)
1178 cifs_dbg(VFS
, "Encrypted shares not supported");
1179 if (tcon
->ses
->server
->ops
->validate_negotiate
)
1180 rc
= tcon
->ses
->server
->ops
->validate_negotiate(xid
, tcon
);
1182 free_rsp_buf(resp_buftype
, rsp
);
1187 if (rsp
->hdr
.sync_hdr
.Status
== STATUS_BAD_NETWORK_NAME
) {
1188 cifs_dbg(VFS
, "BAD_NETWORK_NAME: %s\n", tree
);
1190 tcon
->bad_network_name
= true;
1196 SMB2_tdis(const unsigned int xid
, struct cifs_tcon
*tcon
)
1198 struct smb2_tree_disconnect_req
*req
; /* response is trivial */
1200 struct TCP_Server_Info
*server
;
1201 struct cifs_ses
*ses
= tcon
->ses
;
1203 cifs_dbg(FYI
, "Tree Disconnect\n");
1205 if (ses
&& (ses
->server
))
1206 server
= ses
->server
;
1210 if ((tcon
->need_reconnect
) || (tcon
->ses
->need_reconnect
))
1213 rc
= small_smb2_init(SMB2_TREE_DISCONNECT
, tcon
, (void **) &req
);
1217 rc
= SendReceiveNoRsp(xid
, ses
, (char *)&req
->hdr
, 0);
1219 cifs_stats_fail_inc(tcon
, SMB2_TREE_DISCONNECT_HE
);
1225 static struct create_durable
*
1226 create_durable_buf(void)
1228 struct create_durable
*buf
;
1230 buf
= kzalloc(sizeof(struct create_durable
), GFP_KERNEL
);
1234 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
1235 (struct create_durable
, Data
));
1236 buf
->ccontext
.DataLength
= cpu_to_le32(16);
1237 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
1238 (struct create_durable
, Name
));
1239 buf
->ccontext
.NameLength
= cpu_to_le16(4);
1240 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1248 static struct create_durable
*
1249 create_reconnect_durable_buf(struct cifs_fid
*fid
)
1251 struct create_durable
*buf
;
1253 buf
= kzalloc(sizeof(struct create_durable
), GFP_KERNEL
);
1257 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
1258 (struct create_durable
, Data
));
1259 buf
->ccontext
.DataLength
= cpu_to_le32(16);
1260 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
1261 (struct create_durable
, Name
));
1262 buf
->ccontext
.NameLength
= cpu_to_le16(4);
1263 buf
->Data
.Fid
.PersistentFileId
= fid
->persistent_fid
;
1264 buf
->Data
.Fid
.VolatileFileId
= fid
->volatile_fid
;
1265 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1274 parse_lease_state(struct TCP_Server_Info
*server
, struct smb2_create_rsp
*rsp
,
1275 unsigned int *epoch
)
1278 struct create_context
*cc
;
1280 unsigned int remaining
;
1283 data_offset
= (char *)rsp
+ 4 + le32_to_cpu(rsp
->CreateContextsOffset
);
1284 remaining
= le32_to_cpu(rsp
->CreateContextsLength
);
1285 cc
= (struct create_context
*)data_offset
;
1286 while (remaining
>= sizeof(struct create_context
)) {
1287 name
= le16_to_cpu(cc
->NameOffset
) + (char *)cc
;
1288 if (le16_to_cpu(cc
->NameLength
) == 4 &&
1289 strncmp(name
, "RqLs", 4) == 0)
1290 return server
->ops
->parse_lease_buf(cc
, epoch
);
1292 next
= le32_to_cpu(cc
->Next
);
1296 cc
= (struct create_context
*)((char *)cc
+ next
);
1303 add_lease_context(struct TCP_Server_Info
*server
, struct kvec
*iov
,
1304 unsigned int *num_iovec
, __u8
*oplock
)
1306 struct smb2_create_req
*req
= iov
[0].iov_base
;
1307 unsigned int num
= *num_iovec
;
1309 iov
[num
].iov_base
= server
->ops
->create_lease_buf(oplock
+1, *oplock
);
1310 if (iov
[num
].iov_base
== NULL
)
1312 iov
[num
].iov_len
= server
->vals
->create_lease_size
;
1313 req
->RequestedOplockLevel
= SMB2_OPLOCK_LEVEL_LEASE
;
1314 if (!req
->CreateContextsOffset
)
1315 req
->CreateContextsOffset
= cpu_to_le32(
1316 sizeof(struct smb2_create_req
) - 4 +
1317 iov
[num
- 1].iov_len
);
1318 le32_add_cpu(&req
->CreateContextsLength
,
1319 server
->vals
->create_lease_size
);
1320 inc_rfc1001_len(&req
->hdr
, server
->vals
->create_lease_size
);
1321 *num_iovec
= num
+ 1;
1325 static struct create_durable_v2
*
1326 create_durable_v2_buf(struct cifs_fid
*pfid
)
1328 struct create_durable_v2
*buf
;
1330 buf
= kzalloc(sizeof(struct create_durable_v2
), GFP_KERNEL
);
1334 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
1335 (struct create_durable_v2
, dcontext
));
1336 buf
->ccontext
.DataLength
= cpu_to_le32(sizeof(struct durable_context_v2
));
1337 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
1338 (struct create_durable_v2
, Name
));
1339 buf
->ccontext
.NameLength
= cpu_to_le16(4);
1341 buf
->dcontext
.Timeout
= 0; /* Should this be configurable by workload */
1342 buf
->dcontext
.Flags
= cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT
);
1343 generate_random_uuid(buf
->dcontext
.CreateGuid
);
1344 memcpy(pfid
->create_guid
, buf
->dcontext
.CreateGuid
, 16);
1346 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1354 static struct create_durable_handle_reconnect_v2
*
1355 create_reconnect_durable_v2_buf(struct cifs_fid
*fid
)
1357 struct create_durable_handle_reconnect_v2
*buf
;
1359 buf
= kzalloc(sizeof(struct create_durable_handle_reconnect_v2
),
1364 buf
->ccontext
.DataOffset
=
1365 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2
,
1367 buf
->ccontext
.DataLength
=
1368 cpu_to_le32(sizeof(struct durable_reconnect_context_v2
));
1369 buf
->ccontext
.NameOffset
=
1370 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2
,
1372 buf
->ccontext
.NameLength
= cpu_to_le16(4);
1374 buf
->dcontext
.Fid
.PersistentFileId
= fid
->persistent_fid
;
1375 buf
->dcontext
.Fid
.VolatileFileId
= fid
->volatile_fid
;
1376 buf
->dcontext
.Flags
= cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT
);
1377 memcpy(buf
->dcontext
.CreateGuid
, fid
->create_guid
, 16);
1379 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1388 add_durable_v2_context(struct kvec
*iov
, unsigned int *num_iovec
,
1389 struct cifs_open_parms
*oparms
)
1391 struct smb2_create_req
*req
= iov
[0].iov_base
;
1392 unsigned int num
= *num_iovec
;
1394 iov
[num
].iov_base
= create_durable_v2_buf(oparms
->fid
);
1395 if (iov
[num
].iov_base
== NULL
)
1397 iov
[num
].iov_len
= sizeof(struct create_durable_v2
);
1398 if (!req
->CreateContextsOffset
)
1399 req
->CreateContextsOffset
=
1400 cpu_to_le32(sizeof(struct smb2_create_req
) - 4 +
1402 le32_add_cpu(&req
->CreateContextsLength
, sizeof(struct create_durable_v2
));
1403 inc_rfc1001_len(&req
->hdr
, sizeof(struct create_durable_v2
));
1404 *num_iovec
= num
+ 1;
1409 add_durable_reconnect_v2_context(struct kvec
*iov
, unsigned int *num_iovec
,
1410 struct cifs_open_parms
*oparms
)
1412 struct smb2_create_req
*req
= iov
[0].iov_base
;
1413 unsigned int num
= *num_iovec
;
1415 /* indicate that we don't need to relock the file */
1416 oparms
->reconnect
= false;
1418 iov
[num
].iov_base
= create_reconnect_durable_v2_buf(oparms
->fid
);
1419 if (iov
[num
].iov_base
== NULL
)
1421 iov
[num
].iov_len
= sizeof(struct create_durable_handle_reconnect_v2
);
1422 if (!req
->CreateContextsOffset
)
1423 req
->CreateContextsOffset
=
1424 cpu_to_le32(sizeof(struct smb2_create_req
) - 4 +
1426 le32_add_cpu(&req
->CreateContextsLength
,
1427 sizeof(struct create_durable_handle_reconnect_v2
));
1428 inc_rfc1001_len(&req
->hdr
,
1429 sizeof(struct create_durable_handle_reconnect_v2
));
1430 *num_iovec
= num
+ 1;
1435 add_durable_context(struct kvec
*iov
, unsigned int *num_iovec
,
1436 struct cifs_open_parms
*oparms
, bool use_persistent
)
1438 struct smb2_create_req
*req
= iov
[0].iov_base
;
1439 unsigned int num
= *num_iovec
;
1441 if (use_persistent
) {
1442 if (oparms
->reconnect
)
1443 return add_durable_reconnect_v2_context(iov
, num_iovec
,
1446 return add_durable_v2_context(iov
, num_iovec
, oparms
);
1449 if (oparms
->reconnect
) {
1450 iov
[num
].iov_base
= create_reconnect_durable_buf(oparms
->fid
);
1451 /* indicate that we don't need to relock the file */
1452 oparms
->reconnect
= false;
1454 iov
[num
].iov_base
= create_durable_buf();
1455 if (iov
[num
].iov_base
== NULL
)
1457 iov
[num
].iov_len
= sizeof(struct create_durable
);
1458 if (!req
->CreateContextsOffset
)
1459 req
->CreateContextsOffset
=
1460 cpu_to_le32(sizeof(struct smb2_create_req
) - 4 +
1462 le32_add_cpu(&req
->CreateContextsLength
, sizeof(struct create_durable
));
1463 inc_rfc1001_len(&req
->hdr
, sizeof(struct create_durable
));
1464 *num_iovec
= num
+ 1;
1469 SMB2_open(const unsigned int xid
, struct cifs_open_parms
*oparms
, __le16
*path
,
1470 __u8
*oplock
, struct smb2_file_all_info
*buf
,
1471 struct smb2_err_rsp
**err_buf
)
1473 struct smb2_create_req
*req
;
1474 struct smb2_create_rsp
*rsp
;
1475 struct TCP_Server_Info
*server
;
1476 struct cifs_tcon
*tcon
= oparms
->tcon
;
1477 struct cifs_ses
*ses
= tcon
->ses
;
1481 __le16
*copy_path
= NULL
;
1484 unsigned int num_iovecs
= 2;
1485 __u32 file_attributes
= 0;
1486 char *dhc_buf
= NULL
, *lc_buf
= NULL
;
1488 cifs_dbg(FYI
, "create/open\n");
1490 if (ses
&& (ses
->server
))
1491 server
= ses
->server
;
1495 rc
= small_smb2_init(SMB2_CREATE
, tcon
, (void **) &req
);
1499 if (oparms
->create_options
& CREATE_OPTION_READONLY
)
1500 file_attributes
|= ATTR_READONLY
;
1501 if (oparms
->create_options
& CREATE_OPTION_SPECIAL
)
1502 file_attributes
|= ATTR_SYSTEM
;
1504 req
->ImpersonationLevel
= IL_IMPERSONATION
;
1505 req
->DesiredAccess
= cpu_to_le32(oparms
->desired_access
);
1506 /* File attributes ignored on open (used in create though) */
1507 req
->FileAttributes
= cpu_to_le32(file_attributes
);
1508 req
->ShareAccess
= FILE_SHARE_ALL_LE
;
1509 req
->CreateDisposition
= cpu_to_le32(oparms
->disposition
);
1510 req
->CreateOptions
= cpu_to_le32(oparms
->create_options
& CREATE_OPTIONS_MASK
);
1511 uni_path_len
= (2 * UniStrnlen((wchar_t *)path
, PATH_MAX
)) + 2;
1512 /* do not count rfc1001 len field */
1513 req
->NameOffset
= cpu_to_le16(sizeof(struct smb2_create_req
) - 4);
1515 iov
[0].iov_base
= (char *)req
;
1516 /* 4 for rfc1002 length field */
1517 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
1519 /* MUST set path len (NameLength) to 0 opening root of share */
1520 req
->NameLength
= cpu_to_le16(uni_path_len
- 2);
1521 /* -1 since last byte is buf[0] which is sent below (path) */
1523 if (uni_path_len
% 8 != 0) {
1524 copy_size
= uni_path_len
/ 8 * 8;
1525 if (copy_size
< uni_path_len
)
1528 copy_path
= kzalloc(copy_size
, GFP_KERNEL
);
1531 memcpy((char *)copy_path
, (const char *)path
,
1533 uni_path_len
= copy_size
;
1537 iov
[1].iov_len
= uni_path_len
;
1538 iov
[1].iov_base
= path
;
1539 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1540 inc_rfc1001_len(req
, uni_path_len
- 1);
1542 if (!server
->oplocks
)
1543 *oplock
= SMB2_OPLOCK_LEVEL_NONE
;
1545 if (!(server
->capabilities
& SMB2_GLOBAL_CAP_LEASING
) ||
1546 *oplock
== SMB2_OPLOCK_LEVEL_NONE
)
1547 req
->RequestedOplockLevel
= *oplock
;
1549 rc
= add_lease_context(server
, iov
, &num_iovecs
, oplock
);
1551 cifs_small_buf_release(req
);
1555 lc_buf
= iov
[num_iovecs
-1].iov_base
;
1558 if (*oplock
== SMB2_OPLOCK_LEVEL_BATCH
) {
1559 /* need to set Next field of lease context if we request it */
1560 if (server
->capabilities
& SMB2_GLOBAL_CAP_LEASING
) {
1561 struct create_context
*ccontext
=
1562 (struct create_context
*)iov
[num_iovecs
-1].iov_base
;
1564 cpu_to_le32(server
->vals
->create_lease_size
);
1567 rc
= add_durable_context(iov
, &num_iovecs
, oparms
,
1568 tcon
->use_persistent
);
1570 cifs_small_buf_release(req
);
1575 dhc_buf
= iov
[num_iovecs
-1].iov_base
;
1578 rc
= SendReceive2(xid
, ses
, iov
, num_iovecs
, &resp_buftype
, 0);
1579 rsp
= (struct smb2_create_rsp
*)iov
[0].iov_base
;
1582 cifs_stats_fail_inc(tcon
, SMB2_CREATE_HE
);
1584 *err_buf
= kmemdup(rsp
, get_rfc1002_length(rsp
) + 4,
1589 oparms
->fid
->persistent_fid
= rsp
->PersistentFileId
;
1590 oparms
->fid
->volatile_fid
= rsp
->VolatileFileId
;
1593 memcpy(buf
, &rsp
->CreationTime
, 32);
1594 buf
->AllocationSize
= rsp
->AllocationSize
;
1595 buf
->EndOfFile
= rsp
->EndofFile
;
1596 buf
->Attributes
= rsp
->FileAttributes
;
1597 buf
->NumberOfLinks
= cpu_to_le32(1);
1598 buf
->DeletePending
= 0;
1601 if (rsp
->OplockLevel
== SMB2_OPLOCK_LEVEL_LEASE
)
1602 *oplock
= parse_lease_state(server
, rsp
, &oparms
->fid
->epoch
);
1604 *oplock
= rsp
->OplockLevel
;
1609 free_rsp_buf(resp_buftype
, rsp
);
1614 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1617 SMB2_ioctl(const unsigned int xid
, struct cifs_tcon
*tcon
, u64 persistent_fid
,
1618 u64 volatile_fid
, u32 opcode
, bool is_fsctl
, char *in_data
,
1619 u32 indatalen
, char **out_data
, u32
*plen
/* returned data len */)
1621 struct smb2_ioctl_req
*req
;
1622 struct smb2_ioctl_rsp
*rsp
;
1623 struct smb2_sync_hdr
*shdr
;
1624 struct TCP_Server_Info
*server
;
1625 struct cifs_ses
*ses
;
1631 cifs_dbg(FYI
, "SMB2 IOCTL\n");
1633 if (out_data
!= NULL
)
1636 /* zero out returned data len, in case of error */
1645 if (ses
&& (ses
->server
))
1646 server
= ses
->server
;
1650 rc
= small_smb2_init(SMB2_IOCTL
, tcon
, (void **) &req
);
1654 req
->CtlCode
= cpu_to_le32(opcode
);
1655 req
->PersistentFileId
= persistent_fid
;
1656 req
->VolatileFileId
= volatile_fid
;
1659 req
->InputCount
= cpu_to_le32(indatalen
);
1660 /* do not set InputOffset if no input data */
1662 cpu_to_le32(offsetof(struct smb2_ioctl_req
, Buffer
) - 4);
1663 iov
[1].iov_base
= in_data
;
1664 iov
[1].iov_len
= indatalen
;
1669 req
->OutputOffset
= 0;
1670 req
->OutputCount
= 0; /* MBZ */
1673 * Could increase MaxOutputResponse, but that would require more
1674 * than one credit. Windows typically sets this smaller, but for some
1675 * ioctls it may be useful to allow server to send more. No point
1676 * limiting what the server can send as long as fits in one credit
1678 req
->MaxOutputResponse
= cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1681 req
->Flags
= cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL
);
1685 iov
[0].iov_base
= (char *)req
;
1688 * If no input data, the size of ioctl struct in
1689 * protocol spec still includes a 1 byte data buffer,
1690 * but if input data passed to ioctl, we do not
1691 * want to double count this, so we do not send
1692 * the dummy one byte of data in iovec[0] if sending
1693 * input data (in iovec[1]). We also must add 4 bytes
1694 * in first iovec to allow for rfc1002 length field.
1698 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
1699 inc_rfc1001_len(req
, indatalen
- 1);
1701 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
1704 rc
= SendReceive2(xid
, ses
, iov
, num_iovecs
, &resp_buftype
, 0);
1705 rsp
= (struct smb2_ioctl_rsp
*)iov
[0].iov_base
;
1707 if ((rc
!= 0) && (rc
!= -EINVAL
)) {
1708 cifs_stats_fail_inc(tcon
, SMB2_IOCTL_HE
);
1710 } else if (rc
== -EINVAL
) {
1711 if ((opcode
!= FSCTL_SRV_COPYCHUNK_WRITE
) &&
1712 (opcode
!= FSCTL_SRV_COPYCHUNK
)) {
1713 cifs_stats_fail_inc(tcon
, SMB2_IOCTL_HE
);
1718 /* check if caller wants to look at return data or just return rc */
1719 if ((plen
== NULL
) || (out_data
== NULL
))
1722 *plen
= le32_to_cpu(rsp
->OutputCount
);
1724 /* We check for obvious errors in the output buffer length and offset */
1726 goto ioctl_exit
; /* server returned no data */
1727 else if (*plen
> 0xFF00) {
1728 cifs_dbg(VFS
, "srv returned invalid ioctl length: %d\n", *plen
);
1734 if (get_rfc1002_length(rsp
) < le32_to_cpu(rsp
->OutputOffset
) + *plen
) {
1735 cifs_dbg(VFS
, "Malformed ioctl resp: len %d offset %d\n", *plen
,
1736 le32_to_cpu(rsp
->OutputOffset
));
1742 *out_data
= kmalloc(*plen
, GFP_KERNEL
);
1743 if (*out_data
== NULL
) {
1748 shdr
= get_sync_hdr(rsp
);
1749 memcpy(*out_data
, (char *)shdr
+ le32_to_cpu(rsp
->OutputOffset
), *plen
);
1751 free_rsp_buf(resp_buftype
, rsp
);
1756 * Individual callers to ioctl worker function follow
1760 SMB2_set_compression(const unsigned int xid
, struct cifs_tcon
*tcon
,
1761 u64 persistent_fid
, u64 volatile_fid
)
1764 struct compress_ioctl fsctl_input
;
1765 char *ret_data
= NULL
;
1767 fsctl_input
.CompressionState
=
1768 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT
);
1770 rc
= SMB2_ioctl(xid
, tcon
, persistent_fid
, volatile_fid
,
1771 FSCTL_SET_COMPRESSION
, true /* is_fsctl */,
1772 (char *)&fsctl_input
/* data input */,
1773 2 /* in data len */, &ret_data
/* out data */, NULL
);
1775 cifs_dbg(FYI
, "set compression rc %d\n", rc
);
1781 SMB2_close(const unsigned int xid
, struct cifs_tcon
*tcon
,
1782 u64 persistent_fid
, u64 volatile_fid
)
1784 struct smb2_close_req
*req
;
1785 struct smb2_close_rsp
*rsp
;
1786 struct TCP_Server_Info
*server
;
1787 struct cifs_ses
*ses
= tcon
->ses
;
1792 cifs_dbg(FYI
, "Close\n");
1794 if (ses
&& (ses
->server
))
1795 server
= ses
->server
;
1799 rc
= small_smb2_init(SMB2_CLOSE
, tcon
, (void **) &req
);
1803 req
->PersistentFileId
= persistent_fid
;
1804 req
->VolatileFileId
= volatile_fid
;
1806 iov
[0].iov_base
= (char *)req
;
1807 /* 4 for rfc1002 length field */
1808 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
1810 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buftype
, 0);
1811 rsp
= (struct smb2_close_rsp
*)iov
[0].iov_base
;
1814 cifs_stats_fail_inc(tcon
, SMB2_CLOSE_HE
);
1818 /* BB FIXME - decode close response, update inode for caching */
1821 free_rsp_buf(resp_buftype
, rsp
);
1826 validate_buf(unsigned int offset
, unsigned int buffer_length
,
1827 struct smb2_hdr
*hdr
, unsigned int min_buf_size
)
1830 unsigned int smb_len
= be32_to_cpu(hdr
->smb2_buf_length
);
1831 char *end_of_smb
= smb_len
+ 4 /* RFC1001 length field */ + (char *)hdr
;
1832 char *begin_of_buf
= 4 /* RFC1001 len field */ + offset
+ (char *)hdr
;
1833 char *end_of_buf
= begin_of_buf
+ buffer_length
;
1836 if (buffer_length
< min_buf_size
) {
1837 cifs_dbg(VFS
, "buffer length %d smaller than minimum size %d\n",
1838 buffer_length
, min_buf_size
);
1842 /* check if beyond RFC1001 maximum length */
1843 if ((smb_len
> 0x7FFFFF) || (buffer_length
> 0x7FFFFF)) {
1844 cifs_dbg(VFS
, "buffer length %d or smb length %d too large\n",
1845 buffer_length
, smb_len
);
1849 if ((begin_of_buf
> end_of_smb
) || (end_of_buf
> end_of_smb
)) {
1850 cifs_dbg(VFS
, "illegal server response, bad offset to data\n");
1858 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1859 * Caller must free buffer.
1862 validate_and_copy_buf(unsigned int offset
, unsigned int buffer_length
,
1863 struct smb2_hdr
*hdr
, unsigned int minbufsize
,
1867 char *begin_of_buf
= 4 /* RFC1001 len field */ + offset
+ (char *)hdr
;
1873 rc
= validate_buf(offset
, buffer_length
, hdr
, minbufsize
);
1877 memcpy(data
, begin_of_buf
, buffer_length
);
1883 query_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
1884 u64 persistent_fid
, u64 volatile_fid
, u8 info_class
,
1885 size_t output_len
, size_t min_len
, void *data
)
1887 struct smb2_query_info_req
*req
;
1888 struct smb2_query_info_rsp
*rsp
= NULL
;
1892 struct TCP_Server_Info
*server
;
1893 struct cifs_ses
*ses
= tcon
->ses
;
1895 cifs_dbg(FYI
, "Query Info\n");
1897 if (ses
&& (ses
->server
))
1898 server
= ses
->server
;
1902 rc
= small_smb2_init(SMB2_QUERY_INFO
, tcon
, (void **) &req
);
1906 req
->InfoType
= SMB2_O_INFO_FILE
;
1907 req
->FileInfoClass
= info_class
;
1908 req
->PersistentFileId
= persistent_fid
;
1909 req
->VolatileFileId
= volatile_fid
;
1910 /* 4 for rfc1002 length field and 1 for Buffer */
1911 req
->InputBufferOffset
=
1912 cpu_to_le16(sizeof(struct smb2_query_info_req
) - 1 - 4);
1913 req
->OutputBufferLength
= cpu_to_le32(output_len
);
1915 iov
[0].iov_base
= (char *)req
;
1916 /* 4 for rfc1002 length field */
1917 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
1919 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buftype
, 0);
1920 rsp
= (struct smb2_query_info_rsp
*)iov
[0].iov_base
;
1923 cifs_stats_fail_inc(tcon
, SMB2_QUERY_INFO_HE
);
1927 rc
= validate_and_copy_buf(le16_to_cpu(rsp
->OutputBufferOffset
),
1928 le32_to_cpu(rsp
->OutputBufferLength
),
1929 &rsp
->hdr
, min_len
, data
);
1932 free_rsp_buf(resp_buftype
, rsp
);
1937 SMB2_query_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
1938 u64 persistent_fid
, u64 volatile_fid
,
1939 struct smb2_file_all_info
*data
)
1941 return query_info(xid
, tcon
, persistent_fid
, volatile_fid
,
1942 FILE_ALL_INFORMATION
,
1943 sizeof(struct smb2_file_all_info
) + PATH_MAX
* 2,
1944 sizeof(struct smb2_file_all_info
), data
);
1948 SMB2_get_srv_num(const unsigned int xid
, struct cifs_tcon
*tcon
,
1949 u64 persistent_fid
, u64 volatile_fid
, __le64
*uniqueid
)
1951 return query_info(xid
, tcon
, persistent_fid
, volatile_fid
,
1952 FILE_INTERNAL_INFORMATION
,
1953 sizeof(struct smb2_file_internal_info
),
1954 sizeof(struct smb2_file_internal_info
), uniqueid
);
1958 * This is a no-op for now. We're not really interested in the reply, but
1959 * rather in the fact that the server sent one and that server->lstrp
1962 * FIXME: maybe we should consider checking that the reply matches request?
1965 smb2_echo_callback(struct mid_q_entry
*mid
)
1967 struct TCP_Server_Info
*server
= mid
->callback_data
;
1968 struct smb2_echo_rsp
*rsp
= (struct smb2_echo_rsp
*)mid
->resp_buf
;
1969 unsigned int credits_received
= 1;
1971 if (mid
->mid_state
== MID_RESPONSE_RECEIVED
)
1972 credits_received
= le16_to_cpu(rsp
->hdr
.sync_hdr
.CreditRequest
);
1974 mutex_lock(&server
->srv_mutex
);
1975 DeleteMidQEntry(mid
);
1976 mutex_unlock(&server
->srv_mutex
);
1977 add_credits(server
, credits_received
, CIFS_ECHO_OP
);
1980 void smb2_reconnect_server(struct work_struct
*work
)
1982 struct TCP_Server_Info
*server
= container_of(work
,
1983 struct TCP_Server_Info
, reconnect
.work
);
1984 struct cifs_ses
*ses
;
1985 struct cifs_tcon
*tcon
, *tcon2
;
1986 struct list_head tmp_list
;
1987 int tcon_exist
= false;
1989 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
1990 mutex_lock(&server
->reconnect_mutex
);
1992 INIT_LIST_HEAD(&tmp_list
);
1993 cifs_dbg(FYI
, "Need negotiate, reconnecting tcons\n");
1995 spin_lock(&cifs_tcp_ses_lock
);
1996 list_for_each_entry(ses
, &server
->smb_ses_list
, smb_ses_list
) {
1997 list_for_each_entry(tcon
, &ses
->tcon_list
, tcon_list
) {
1998 if (tcon
->need_reconnect
|| tcon
->need_reopen_files
) {
2000 list_add_tail(&tcon
->rlist
, &tmp_list
);
2006 * Get the reference to server struct to be sure that the last call of
2007 * cifs_put_tcon() in the loop below won't release the server pointer.
2010 server
->srv_count
++;
2012 spin_unlock(&cifs_tcp_ses_lock
);
2014 list_for_each_entry_safe(tcon
, tcon2
, &tmp_list
, rlist
) {
2015 if (!smb2_reconnect(SMB2_INTERNAL_CMD
, tcon
))
2016 cifs_reopen_persistent_handles(tcon
);
2017 list_del_init(&tcon
->rlist
);
2018 cifs_put_tcon(tcon
);
2021 cifs_dbg(FYI
, "Reconnecting tcons finished\n");
2022 mutex_unlock(&server
->reconnect_mutex
);
2024 /* now we can safely release srv struct */
2026 cifs_put_tcp_session(server
, 1);
2030 SMB2_echo(struct TCP_Server_Info
*server
)
2032 struct smb2_echo_req
*req
;
2035 struct smb_rqst rqst
= { .rq_iov
= &iov
,
2038 cifs_dbg(FYI
, "In echo request\n");
2040 if (server
->tcpStatus
== CifsNeedNegotiate
) {
2041 /* No need to send echo on newly established connections */
2042 queue_delayed_work(cifsiod_wq
, &server
->reconnect
, 0);
2046 rc
= small_smb2_init(SMB2_ECHO
, NULL
, (void **)&req
);
2050 req
->hdr
.sync_hdr
.CreditRequest
= cpu_to_le16(1);
2052 iov
.iov_base
= (char *)req
;
2053 /* 4 for rfc1002 length field */
2054 iov
.iov_len
= get_rfc1002_length(req
) + 4;
2056 rc
= cifs_call_async(server
, &rqst
, NULL
, smb2_echo_callback
, server
,
2059 cifs_dbg(FYI
, "Echo request failed: %d\n", rc
);
2061 cifs_small_buf_release(req
);
2066 SMB2_flush(const unsigned int xid
, struct cifs_tcon
*tcon
, u64 persistent_fid
,
2069 struct smb2_flush_req
*req
;
2070 struct TCP_Server_Info
*server
;
2071 struct cifs_ses
*ses
= tcon
->ses
;
2076 cifs_dbg(FYI
, "Flush\n");
2078 if (ses
&& (ses
->server
))
2079 server
= ses
->server
;
2083 rc
= small_smb2_init(SMB2_FLUSH
, tcon
, (void **) &req
);
2087 req
->PersistentFileId
= persistent_fid
;
2088 req
->VolatileFileId
= volatile_fid
;
2090 iov
[0].iov_base
= (char *)req
;
2091 /* 4 for rfc1002 length field */
2092 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
2094 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buftype
, 0);
2097 cifs_stats_fail_inc(tcon
, SMB2_FLUSH_HE
);
2099 free_rsp_buf(resp_buftype
, iov
[0].iov_base
);
2104 * To form a chain of read requests, any read requests after the first should
2105 * have the end_of_chain boolean set to true.
2108 smb2_new_read_req(struct kvec
*iov
, struct cifs_io_parms
*io_parms
,
2109 unsigned int remaining_bytes
, int request_type
)
2112 struct smb2_read_req
*req
= NULL
;
2113 struct smb2_sync_hdr
*shdr
;
2115 rc
= small_smb2_init(SMB2_READ
, io_parms
->tcon
, (void **) &req
);
2118 if (io_parms
->tcon
->ses
->server
== NULL
)
2119 return -ECONNABORTED
;
2121 shdr
= get_sync_hdr(req
);
2122 shdr
->ProcessId
= cpu_to_le32(io_parms
->pid
);
2124 req
->PersistentFileId
= io_parms
->persistent_fid
;
2125 req
->VolatileFileId
= io_parms
->volatile_fid
;
2126 req
->ReadChannelInfoOffset
= 0; /* reserved */
2127 req
->ReadChannelInfoLength
= 0; /* reserved */
2128 req
->Channel
= 0; /* reserved */
2129 req
->MinimumCount
= 0;
2130 req
->Length
= cpu_to_le32(io_parms
->length
);
2131 req
->Offset
= cpu_to_le64(io_parms
->offset
);
2133 if (request_type
& CHAINED_REQUEST
) {
2134 if (!(request_type
& END_OF_CHAIN
)) {
2135 /* 4 for rfc1002 length field */
2137 cpu_to_le32(get_rfc1002_length(req
) + 4);
2138 } else /* END_OF_CHAIN */
2139 shdr
->NextCommand
= 0;
2140 if (request_type
& RELATED_REQUEST
) {
2141 shdr
->Flags
|= SMB2_FLAGS_RELATED_OPERATIONS
;
2143 * Related requests use info from previous read request
2146 shdr
->SessionId
= 0xFFFFFFFF;
2147 shdr
->TreeId
= 0xFFFFFFFF;
2148 req
->PersistentFileId
= 0xFFFFFFFF;
2149 req
->VolatileFileId
= 0xFFFFFFFF;
2152 if (remaining_bytes
> io_parms
->length
)
2153 req
->RemainingBytes
= cpu_to_le32(remaining_bytes
);
2155 req
->RemainingBytes
= 0;
2157 iov
[0].iov_base
= (char *)req
;
2158 /* 4 for rfc1002 length field */
2159 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
2164 smb2_readv_callback(struct mid_q_entry
*mid
)
2166 struct cifs_readdata
*rdata
= mid
->callback_data
;
2167 struct cifs_tcon
*tcon
= tlink_tcon(rdata
->cfile
->tlink
);
2168 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
2169 struct smb2_sync_hdr
*shdr
= get_sync_hdr(rdata
->iov
.iov_base
);
2170 unsigned int credits_received
= 1;
2171 struct smb_rqst rqst
= { .rq_iov
= &rdata
->iov
,
2173 .rq_pages
= rdata
->pages
,
2174 .rq_npages
= rdata
->nr_pages
,
2175 .rq_pagesz
= rdata
->pagesz
,
2176 .rq_tailsz
= rdata
->tailsz
};
2178 cifs_dbg(FYI
, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2179 __func__
, mid
->mid
, mid
->mid_state
, rdata
->result
,
2182 switch (mid
->mid_state
) {
2183 case MID_RESPONSE_RECEIVED
:
2184 credits_received
= le16_to_cpu(shdr
->CreditRequest
);
2185 /* result already set, check signature */
2189 rc
= smb2_verify_signature(&rqst
, server
);
2191 cifs_dbg(VFS
, "SMB signature verification returned error = %d\n",
2194 /* FIXME: should this be counted toward the initiating task? */
2195 task_io_account_read(rdata
->got_bytes
);
2196 cifs_stats_bytes_read(tcon
, rdata
->got_bytes
);
2198 case MID_REQUEST_SUBMITTED
:
2199 case MID_RETRY_NEEDED
:
2200 rdata
->result
= -EAGAIN
;
2201 if (server
->sign
&& rdata
->got_bytes
)
2202 /* reset bytes number since we can not check a sign */
2203 rdata
->got_bytes
= 0;
2204 /* FIXME: should this be counted toward the initiating task? */
2205 task_io_account_read(rdata
->got_bytes
);
2206 cifs_stats_bytes_read(tcon
, rdata
->got_bytes
);
2209 if (rdata
->result
!= -ENODATA
)
2210 rdata
->result
= -EIO
;
2214 cifs_stats_fail_inc(tcon
, SMB2_READ_HE
);
2216 queue_work(cifsiod_wq
, &rdata
->work
);
2217 mutex_lock(&server
->srv_mutex
);
2218 DeleteMidQEntry(mid
);
2219 mutex_unlock(&server
->srv_mutex
);
2220 add_credits(server
, credits_received
, 0);
2223 /* smb2_async_readv - send an async write, and set up mid to handle result */
2225 smb2_async_readv(struct cifs_readdata
*rdata
)
2229 struct smb2_sync_hdr
*shdr
;
2230 struct cifs_io_parms io_parms
;
2231 struct smb_rqst rqst
= { .rq_iov
= &rdata
->iov
,
2233 struct TCP_Server_Info
*server
;
2235 cifs_dbg(FYI
, "%s: offset=%llu bytes=%u\n",
2236 __func__
, rdata
->offset
, rdata
->bytes
);
2238 io_parms
.tcon
= tlink_tcon(rdata
->cfile
->tlink
);
2239 io_parms
.offset
= rdata
->offset
;
2240 io_parms
.length
= rdata
->bytes
;
2241 io_parms
.persistent_fid
= rdata
->cfile
->fid
.persistent_fid
;
2242 io_parms
.volatile_fid
= rdata
->cfile
->fid
.volatile_fid
;
2243 io_parms
.pid
= rdata
->pid
;
2245 server
= io_parms
.tcon
->ses
->server
;
2247 rc
= smb2_new_read_req(&rdata
->iov
, &io_parms
, 0, 0);
2249 if (rc
== -EAGAIN
&& rdata
->credits
) {
2250 /* credits was reset by reconnect */
2252 /* reduce in_flight value since we won't send the req */
2253 spin_lock(&server
->req_lock
);
2254 server
->in_flight
--;
2255 spin_unlock(&server
->req_lock
);
2260 buf
= rdata
->iov
.iov_base
;
2261 shdr
= get_sync_hdr(buf
);
2262 /* 4 for rfc1002 length field */
2263 rdata
->iov
.iov_len
= get_rfc1002_length(rdata
->iov
.iov_base
) + 4;
2265 if (rdata
->credits
) {
2266 shdr
->CreditCharge
= cpu_to_le16(DIV_ROUND_UP(rdata
->bytes
,
2267 SMB2_MAX_BUFFER_SIZE
));
2268 shdr
->CreditRequest
= shdr
->CreditCharge
;
2269 spin_lock(&server
->req_lock
);
2270 server
->credits
+= rdata
->credits
-
2271 le16_to_cpu(shdr
->CreditCharge
);
2272 spin_unlock(&server
->req_lock
);
2273 wake_up(&server
->request_q
);
2274 flags
= CIFS_HAS_CREDITS
;
2277 kref_get(&rdata
->refcount
);
2278 rc
= cifs_call_async(io_parms
.tcon
->ses
->server
, &rqst
,
2279 cifs_readv_receive
, smb2_readv_callback
,
2282 kref_put(&rdata
->refcount
, cifs_readdata_release
);
2283 cifs_stats_fail_inc(io_parms
.tcon
, SMB2_READ_HE
);
2286 cifs_small_buf_release(buf
);
2291 SMB2_read(const unsigned int xid
, struct cifs_io_parms
*io_parms
,
2292 unsigned int *nbytes
, char **buf
, int *buf_type
)
2294 int resp_buftype
, rc
= -EACCES
;
2295 struct smb2_read_rsp
*rsp
= NULL
;
2296 struct smb2_sync_hdr
*shdr
;
2300 rc
= smb2_new_read_req(iov
, io_parms
, 0, 0);
2304 rc
= SendReceive2(xid
, io_parms
->tcon
->ses
, iov
, 1,
2305 &resp_buftype
, CIFS_LOG_ERROR
);
2307 rsp
= (struct smb2_read_rsp
*)iov
[0].iov_base
;
2308 shdr
= get_sync_hdr(rsp
);
2310 if (shdr
->Status
== STATUS_END_OF_FILE
) {
2311 free_rsp_buf(resp_buftype
, iov
[0].iov_base
);
2316 cifs_stats_fail_inc(io_parms
->tcon
, SMB2_READ_HE
);
2317 cifs_dbg(VFS
, "Send error in read = %d\n", rc
);
2319 *nbytes
= le32_to_cpu(rsp
->DataLength
);
2320 if ((*nbytes
> CIFS_MAX_MSGSIZE
) ||
2321 (*nbytes
> io_parms
->length
)) {
2322 cifs_dbg(FYI
, "bad length %d for count %d\n",
2323 *nbytes
, io_parms
->length
);
2330 memcpy(*buf
, (char *)shdr
+ rsp
->DataOffset
, *nbytes
);
2331 free_rsp_buf(resp_buftype
, iov
[0].iov_base
);
2332 } else if (resp_buftype
!= CIFS_NO_BUFFER
) {
2333 *buf
= iov
[0].iov_base
;
2334 if (resp_buftype
== CIFS_SMALL_BUFFER
)
2335 *buf_type
= CIFS_SMALL_BUFFER
;
2336 else if (resp_buftype
== CIFS_LARGE_BUFFER
)
2337 *buf_type
= CIFS_LARGE_BUFFER
;
2343 * Check the mid_state and signature on received buffer (if any), and queue the
2344 * workqueue completion task.
2347 smb2_writev_callback(struct mid_q_entry
*mid
)
2349 struct cifs_writedata
*wdata
= mid
->callback_data
;
2350 struct cifs_tcon
*tcon
= tlink_tcon(wdata
->cfile
->tlink
);
2351 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
2352 unsigned int written
;
2353 struct smb2_write_rsp
*rsp
= (struct smb2_write_rsp
*)mid
->resp_buf
;
2354 unsigned int credits_received
= 1;
2356 switch (mid
->mid_state
) {
2357 case MID_RESPONSE_RECEIVED
:
2358 credits_received
= le16_to_cpu(rsp
->hdr
.sync_hdr
.CreditRequest
);
2359 wdata
->result
= smb2_check_receive(mid
, tcon
->ses
->server
, 0);
2360 if (wdata
->result
!= 0)
2363 written
= le32_to_cpu(rsp
->DataLength
);
2365 * Mask off high 16 bits when bytes written as returned
2366 * by the server is greater than bytes requested by the
2367 * client. OS/2 servers are known to set incorrect
2370 if (written
> wdata
->bytes
)
2373 if (written
< wdata
->bytes
)
2374 wdata
->result
= -ENOSPC
;
2376 wdata
->bytes
= written
;
2378 case MID_REQUEST_SUBMITTED
:
2379 case MID_RETRY_NEEDED
:
2380 wdata
->result
= -EAGAIN
;
2383 wdata
->result
= -EIO
;
2388 cifs_stats_fail_inc(tcon
, SMB2_WRITE_HE
);
2390 queue_work(cifsiod_wq
, &wdata
->work
);
2391 mutex_lock(&server
->srv_mutex
);
2392 DeleteMidQEntry(mid
);
2393 mutex_unlock(&server
->srv_mutex
);
2394 add_credits(tcon
->ses
->server
, credits_received
, 0);
2397 /* smb2_async_writev - send an async write, and set up mid to handle result */
2399 smb2_async_writev(struct cifs_writedata
*wdata
,
2400 void (*release
)(struct kref
*kref
))
2402 int rc
= -EACCES
, flags
= 0;
2403 struct smb2_write_req
*req
= NULL
;
2404 struct smb2_sync_hdr
*shdr
;
2405 struct cifs_tcon
*tcon
= tlink_tcon(wdata
->cfile
->tlink
);
2406 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
2408 struct smb_rqst rqst
;
2410 rc
= small_smb2_init(SMB2_WRITE
, tcon
, (void **) &req
);
2412 if (rc
== -EAGAIN
&& wdata
->credits
) {
2413 /* credits was reset by reconnect */
2415 /* reduce in_flight value since we won't send the req */
2416 spin_lock(&server
->req_lock
);
2417 server
->in_flight
--;
2418 spin_unlock(&server
->req_lock
);
2420 goto async_writev_out
;
2423 shdr
= get_sync_hdr(req
);
2424 shdr
->ProcessId
= cpu_to_le32(wdata
->cfile
->pid
);
2426 req
->PersistentFileId
= wdata
->cfile
->fid
.persistent_fid
;
2427 req
->VolatileFileId
= wdata
->cfile
->fid
.volatile_fid
;
2428 req
->WriteChannelInfoOffset
= 0;
2429 req
->WriteChannelInfoLength
= 0;
2431 req
->Offset
= cpu_to_le64(wdata
->offset
);
2432 /* 4 for rfc1002 length field */
2433 req
->DataOffset
= cpu_to_le16(
2434 offsetof(struct smb2_write_req
, Buffer
) - 4);
2435 req
->RemainingBytes
= 0;
2437 /* 4 for rfc1002 length field and 1 for Buffer */
2438 iov
.iov_len
= get_rfc1002_length(req
) + 4 - 1;
2443 rqst
.rq_pages
= wdata
->pages
;
2444 rqst
.rq_npages
= wdata
->nr_pages
;
2445 rqst
.rq_pagesz
= wdata
->pagesz
;
2446 rqst
.rq_tailsz
= wdata
->tailsz
;
2448 cifs_dbg(FYI
, "async write at %llu %u bytes\n",
2449 wdata
->offset
, wdata
->bytes
);
2451 req
->Length
= cpu_to_le32(wdata
->bytes
);
2453 inc_rfc1001_len(&req
->hdr
, wdata
->bytes
- 1 /* Buffer */);
2455 if (wdata
->credits
) {
2456 shdr
->CreditCharge
= cpu_to_le16(DIV_ROUND_UP(wdata
->bytes
,
2457 SMB2_MAX_BUFFER_SIZE
));
2458 shdr
->CreditRequest
= shdr
->CreditCharge
;
2459 spin_lock(&server
->req_lock
);
2460 server
->credits
+= wdata
->credits
-
2461 le16_to_cpu(shdr
->CreditCharge
);
2462 spin_unlock(&server
->req_lock
);
2463 wake_up(&server
->request_q
);
2464 flags
= CIFS_HAS_CREDITS
;
2467 kref_get(&wdata
->refcount
);
2468 rc
= cifs_call_async(server
, &rqst
, NULL
, smb2_writev_callback
, wdata
,
2472 kref_put(&wdata
->refcount
, release
);
2473 cifs_stats_fail_inc(tcon
, SMB2_WRITE_HE
);
2477 cifs_small_buf_release(req
);
2482 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2483 * The length field from io_parms must be at least 1 and indicates a number of
2484 * elements with data to write that begins with position 1 in iov array. All
2485 * data length is specified by count.
2488 SMB2_write(const unsigned int xid
, struct cifs_io_parms
*io_parms
,
2489 unsigned int *nbytes
, struct kvec
*iov
, int n_vec
)
2492 struct smb2_write_req
*req
= NULL
;
2493 struct smb2_write_rsp
*rsp
= NULL
;
2500 rc
= small_smb2_init(SMB2_WRITE
, io_parms
->tcon
, (void **) &req
);
2504 if (io_parms
->tcon
->ses
->server
== NULL
)
2505 return -ECONNABORTED
;
2507 req
->hdr
.sync_hdr
.ProcessId
= cpu_to_le32(io_parms
->pid
);
2509 req
->PersistentFileId
= io_parms
->persistent_fid
;
2510 req
->VolatileFileId
= io_parms
->volatile_fid
;
2511 req
->WriteChannelInfoOffset
= 0;
2512 req
->WriteChannelInfoLength
= 0;
2514 req
->Length
= cpu_to_le32(io_parms
->length
);
2515 req
->Offset
= cpu_to_le64(io_parms
->offset
);
2516 /* 4 for rfc1002 length field */
2517 req
->DataOffset
= cpu_to_le16(
2518 offsetof(struct smb2_write_req
, Buffer
) - 4);
2519 req
->RemainingBytes
= 0;
2521 iov
[0].iov_base
= (char *)req
;
2522 /* 4 for rfc1002 length field and 1 for Buffer */
2523 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
2525 /* length of entire message including data to be written */
2526 inc_rfc1001_len(req
, io_parms
->length
- 1 /* Buffer */);
2528 rc
= SendReceive2(xid
, io_parms
->tcon
->ses
, iov
, n_vec
+ 1,
2530 rsp
= (struct smb2_write_rsp
*)iov
[0].iov_base
;
2533 cifs_stats_fail_inc(io_parms
->tcon
, SMB2_WRITE_HE
);
2534 cifs_dbg(VFS
, "Send error in write = %d\n", rc
);
2536 *nbytes
= le32_to_cpu(rsp
->DataLength
);
2538 free_rsp_buf(resp_buftype
, rsp
);
2543 num_entries(char *bufstart
, char *end_of_buf
, char **lastentry
, size_t size
)
2546 unsigned int entrycount
= 0;
2547 unsigned int next_offset
= 0;
2548 FILE_DIRECTORY_INFO
*entryptr
;
2550 if (bufstart
== NULL
)
2553 entryptr
= (FILE_DIRECTORY_INFO
*)bufstart
;
2556 entryptr
= (FILE_DIRECTORY_INFO
*)
2557 ((char *)entryptr
+ next_offset
);
2559 if ((char *)entryptr
+ size
> end_of_buf
) {
2560 cifs_dbg(VFS
, "malformed search entry would overflow\n");
2564 len
= le32_to_cpu(entryptr
->FileNameLength
);
2565 if ((char *)entryptr
+ len
+ size
> end_of_buf
) {
2566 cifs_dbg(VFS
, "directory entry name would overflow frame end of buf %p\n",
2571 *lastentry
= (char *)entryptr
;
2574 next_offset
= le32_to_cpu(entryptr
->NextEntryOffset
);
2586 SMB2_query_directory(const unsigned int xid
, struct cifs_tcon
*tcon
,
2587 u64 persistent_fid
, u64 volatile_fid
, int index
,
2588 struct cifs_search_info
*srch_inf
)
2590 struct smb2_query_directory_req
*req
;
2591 struct smb2_query_directory_rsp
*rsp
= NULL
;
2595 int resp_buftype
= CIFS_NO_BUFFER
;
2596 unsigned char *bufptr
;
2597 struct TCP_Server_Info
*server
;
2598 struct cifs_ses
*ses
= tcon
->ses
;
2599 __le16 asteriks
= cpu_to_le16('*');
2601 unsigned int output_size
= CIFSMaxBufSize
;
2602 size_t info_buf_size
;
2604 if (ses
&& (ses
->server
))
2605 server
= ses
->server
;
2609 rc
= small_smb2_init(SMB2_QUERY_DIRECTORY
, tcon
, (void **) &req
);
2613 switch (srch_inf
->info_level
) {
2614 case SMB_FIND_FILE_DIRECTORY_INFO
:
2615 req
->FileInformationClass
= FILE_DIRECTORY_INFORMATION
;
2616 info_buf_size
= sizeof(FILE_DIRECTORY_INFO
) - 1;
2618 case SMB_FIND_FILE_ID_FULL_DIR_INFO
:
2619 req
->FileInformationClass
= FILEID_FULL_DIRECTORY_INFORMATION
;
2620 info_buf_size
= sizeof(SEARCH_ID_FULL_DIR_INFO
) - 1;
2623 cifs_dbg(VFS
, "info level %u isn't supported\n",
2624 srch_inf
->info_level
);
2629 req
->FileIndex
= cpu_to_le32(index
);
2630 req
->PersistentFileId
= persistent_fid
;
2631 req
->VolatileFileId
= volatile_fid
;
2634 bufptr
= req
->Buffer
;
2635 memcpy(bufptr
, &asteriks
, len
);
2637 req
->FileNameOffset
=
2638 cpu_to_le16(sizeof(struct smb2_query_directory_req
) - 1 - 4);
2639 req
->FileNameLength
= cpu_to_le16(len
);
2641 * BB could be 30 bytes or so longer if we used SMB2 specific
2642 * buffer lengths, but this is safe and close enough.
2644 output_size
= min_t(unsigned int, output_size
, server
->maxBuf
);
2645 output_size
= min_t(unsigned int, output_size
, 2 << 15);
2646 req
->OutputBufferLength
= cpu_to_le32(output_size
);
2648 iov
[0].iov_base
= (char *)req
;
2649 /* 4 for RFC1001 length and 1 for Buffer */
2650 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
2652 iov
[1].iov_base
= (char *)(req
->Buffer
);
2653 iov
[1].iov_len
= len
;
2655 inc_rfc1001_len(req
, len
- 1 /* Buffer */);
2657 rc
= SendReceive2(xid
, ses
, iov
, 2, &resp_buftype
, 0);
2658 rsp
= (struct smb2_query_directory_rsp
*)iov
[0].iov_base
;
2661 if (rc
== -ENODATA
&&
2662 rsp
->hdr
.sync_hdr
.Status
== STATUS_NO_MORE_FILES
) {
2663 srch_inf
->endOfSearch
= true;
2666 cifs_stats_fail_inc(tcon
, SMB2_QUERY_DIRECTORY_HE
);
2670 rc
= validate_buf(le16_to_cpu(rsp
->OutputBufferOffset
),
2671 le32_to_cpu(rsp
->OutputBufferLength
), &rsp
->hdr
,
2676 srch_inf
->unicode
= true;
2678 if (srch_inf
->ntwrk_buf_start
) {
2679 if (srch_inf
->smallBuf
)
2680 cifs_small_buf_release(srch_inf
->ntwrk_buf_start
);
2682 cifs_buf_release(srch_inf
->ntwrk_buf_start
);
2684 srch_inf
->ntwrk_buf_start
= (char *)rsp
;
2685 srch_inf
->srch_entries_start
= srch_inf
->last_entry
= 4 /* rfclen */ +
2686 (char *)&rsp
->hdr
+ le16_to_cpu(rsp
->OutputBufferOffset
);
2687 /* 4 for rfc1002 length field */
2688 end_of_smb
= get_rfc1002_length(rsp
) + 4 + (char *)&rsp
->hdr
;
2689 srch_inf
->entries_in_buffer
=
2690 num_entries(srch_inf
->srch_entries_start
, end_of_smb
,
2691 &srch_inf
->last_entry
, info_buf_size
);
2692 srch_inf
->index_of_last_entry
+= srch_inf
->entries_in_buffer
;
2693 cifs_dbg(FYI
, "num entries %d last_index %lld srch start %p srch end %p\n",
2694 srch_inf
->entries_in_buffer
, srch_inf
->index_of_last_entry
,
2695 srch_inf
->srch_entries_start
, srch_inf
->last_entry
);
2696 if (resp_buftype
== CIFS_LARGE_BUFFER
)
2697 srch_inf
->smallBuf
= false;
2698 else if (resp_buftype
== CIFS_SMALL_BUFFER
)
2699 srch_inf
->smallBuf
= true;
2701 cifs_dbg(VFS
, "illegal search buffer type\n");
2706 free_rsp_buf(resp_buftype
, rsp
);
2711 send_set_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
2712 u64 persistent_fid
, u64 volatile_fid
, u32 pid
, int info_class
,
2713 unsigned int num
, void **data
, unsigned int *size
)
2715 struct smb2_set_info_req
*req
;
2716 struct smb2_set_info_rsp
*rsp
= NULL
;
2721 struct TCP_Server_Info
*server
;
2722 struct cifs_ses
*ses
= tcon
->ses
;
2724 if (ses
&& (ses
->server
))
2725 server
= ses
->server
;
2732 iov
= kmalloc(sizeof(struct kvec
) * num
, GFP_KERNEL
);
2736 rc
= small_smb2_init(SMB2_SET_INFO
, tcon
, (void **) &req
);
2742 req
->hdr
.sync_hdr
.ProcessId
= cpu_to_le32(pid
);
2744 req
->InfoType
= SMB2_O_INFO_FILE
;
2745 req
->FileInfoClass
= info_class
;
2746 req
->PersistentFileId
= persistent_fid
;
2747 req
->VolatileFileId
= volatile_fid
;
2749 /* 4 for RFC1001 length and 1 for Buffer */
2751 cpu_to_le16(sizeof(struct smb2_set_info_req
) - 1 - 4);
2752 req
->BufferLength
= cpu_to_le32(*size
);
2754 inc_rfc1001_len(req
, *size
- 1 /* Buffer */);
2756 memcpy(req
->Buffer
, *data
, *size
);
2758 iov
[0].iov_base
= (char *)req
;
2759 /* 4 for RFC1001 length */
2760 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
2762 for (i
= 1; i
< num
; i
++) {
2763 inc_rfc1001_len(req
, size
[i
]);
2764 le32_add_cpu(&req
->BufferLength
, size
[i
]);
2765 iov
[i
].iov_base
= (char *)data
[i
];
2766 iov
[i
].iov_len
= size
[i
];
2769 rc
= SendReceive2(xid
, ses
, iov
, num
, &resp_buftype
, 0);
2770 rsp
= (struct smb2_set_info_rsp
*)iov
[0].iov_base
;
2773 cifs_stats_fail_inc(tcon
, SMB2_SET_INFO_HE
);
2775 free_rsp_buf(resp_buftype
, rsp
);
2781 SMB2_rename(const unsigned int xid
, struct cifs_tcon
*tcon
,
2782 u64 persistent_fid
, u64 volatile_fid
, __le16
*target_file
)
2784 struct smb2_file_rename_info info
;
2786 unsigned int size
[2];
2788 int len
= (2 * UniStrnlen((wchar_t *)target_file
, PATH_MAX
));
2790 data
= kmalloc(sizeof(void *) * 2, GFP_KERNEL
);
2794 info
.ReplaceIfExists
= 1; /* 1 = replace existing target with new */
2795 /* 0 = fail if target already exists */
2796 info
.RootDirectory
= 0; /* MBZ for network ops (why does spec say?) */
2797 info
.FileNameLength
= cpu_to_le32(len
);
2800 size
[0] = sizeof(struct smb2_file_rename_info
);
2802 data
[1] = target_file
;
2803 size
[1] = len
+ 2 /* null */;
2805 rc
= send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2806 current
->tgid
, FILE_RENAME_INFORMATION
, 2, data
,
2813 SMB2_rmdir(const unsigned int xid
, struct cifs_tcon
*tcon
,
2814 u64 persistent_fid
, u64 volatile_fid
)
2816 __u8 delete_pending
= 1;
2820 data
= &delete_pending
;
2821 size
= 1; /* sizeof __u8 */
2823 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2824 current
->tgid
, FILE_DISPOSITION_INFORMATION
, 1, &data
,
2829 SMB2_set_hardlink(const unsigned int xid
, struct cifs_tcon
*tcon
,
2830 u64 persistent_fid
, u64 volatile_fid
, __le16
*target_file
)
2832 struct smb2_file_link_info info
;
2834 unsigned int size
[2];
2836 int len
= (2 * UniStrnlen((wchar_t *)target_file
, PATH_MAX
));
2838 data
= kmalloc(sizeof(void *) * 2, GFP_KERNEL
);
2842 info
.ReplaceIfExists
= 0; /* 1 = replace existing link with new */
2843 /* 0 = fail if link already exists */
2844 info
.RootDirectory
= 0; /* MBZ for network ops (why does spec say?) */
2845 info
.FileNameLength
= cpu_to_le32(len
);
2848 size
[0] = sizeof(struct smb2_file_link_info
);
2850 data
[1] = target_file
;
2851 size
[1] = len
+ 2 /* null */;
2853 rc
= send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2854 current
->tgid
, FILE_LINK_INFORMATION
, 2, data
, size
);
2860 SMB2_set_eof(const unsigned int xid
, struct cifs_tcon
*tcon
, u64 persistent_fid
,
2861 u64 volatile_fid
, u32 pid
, __le64
*eof
, bool is_falloc
)
2863 struct smb2_file_eof_info info
;
2867 info
.EndOfFile
= *eof
;
2870 size
= sizeof(struct smb2_file_eof_info
);
2873 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2874 pid
, FILE_ALLOCATION_INFORMATION
, 1, &data
, &size
);
2876 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2877 pid
, FILE_END_OF_FILE_INFORMATION
, 1, &data
, &size
);
2881 SMB2_set_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
2882 u64 persistent_fid
, u64 volatile_fid
, FILE_BASIC_INFO
*buf
)
2885 size
= sizeof(FILE_BASIC_INFO
);
2886 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
2887 current
->tgid
, FILE_BASIC_INFORMATION
, 1,
2888 (void **)&buf
, &size
);
2892 SMB2_oplock_break(const unsigned int xid
, struct cifs_tcon
*tcon
,
2893 const u64 persistent_fid
, const u64 volatile_fid
,
2897 struct smb2_oplock_break
*req
= NULL
;
2899 cifs_dbg(FYI
, "SMB2_oplock_break\n");
2900 rc
= small_smb2_init(SMB2_OPLOCK_BREAK
, tcon
, (void **) &req
);
2905 req
->VolatileFid
= volatile_fid
;
2906 req
->PersistentFid
= persistent_fid
;
2907 req
->OplockLevel
= oplock_level
;
2908 req
->hdr
.sync_hdr
.CreditRequest
= cpu_to_le16(1);
2910 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) req
, CIFS_OBREAK_OP
);
2911 /* SMB2 buffer freed by function above */
2914 cifs_stats_fail_inc(tcon
, SMB2_OPLOCK_BREAK_HE
);
2915 cifs_dbg(FYI
, "Send error in Oplock Break = %d\n", rc
);
2922 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info
*pfs_inf
,
2923 struct kstatfs
*kst
)
2925 kst
->f_bsize
= le32_to_cpu(pfs_inf
->BytesPerSector
) *
2926 le32_to_cpu(pfs_inf
->SectorsPerAllocationUnit
);
2927 kst
->f_blocks
= le64_to_cpu(pfs_inf
->TotalAllocationUnits
);
2928 kst
->f_bfree
= le64_to_cpu(pfs_inf
->ActualAvailableAllocationUnits
);
2929 kst
->f_bavail
= le64_to_cpu(pfs_inf
->CallerAvailableAllocationUnits
);
2934 build_qfs_info_req(struct kvec
*iov
, struct cifs_tcon
*tcon
, int level
,
2935 int outbuf_len
, u64 persistent_fid
, u64 volatile_fid
)
2938 struct smb2_query_info_req
*req
;
2940 cifs_dbg(FYI
, "Query FSInfo level %d\n", level
);
2942 if ((tcon
->ses
== NULL
) || (tcon
->ses
->server
== NULL
))
2945 rc
= small_smb2_init(SMB2_QUERY_INFO
, tcon
, (void **) &req
);
2949 req
->InfoType
= SMB2_O_INFO_FILESYSTEM
;
2950 req
->FileInfoClass
= level
;
2951 req
->PersistentFileId
= persistent_fid
;
2952 req
->VolatileFileId
= volatile_fid
;
2953 /* 4 for rfc1002 length field and 1 for pad */
2954 req
->InputBufferOffset
=
2955 cpu_to_le16(sizeof(struct smb2_query_info_req
) - 1 - 4);
2956 req
->OutputBufferLength
= cpu_to_le32(
2957 outbuf_len
+ sizeof(struct smb2_query_info_rsp
) - 1 - 4);
2959 iov
->iov_base
= (char *)req
;
2960 /* 4 for rfc1002 length field */
2961 iov
->iov_len
= get_rfc1002_length(req
) + 4;
2966 SMB2_QFS_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
2967 u64 persistent_fid
, u64 volatile_fid
, struct kstatfs
*fsdata
)
2969 struct smb2_query_info_rsp
*rsp
= NULL
;
2973 struct cifs_ses
*ses
= tcon
->ses
;
2974 struct smb2_fs_full_size_info
*info
= NULL
;
2976 rc
= build_qfs_info_req(&iov
, tcon
, FS_FULL_SIZE_INFORMATION
,
2977 sizeof(struct smb2_fs_full_size_info
),
2978 persistent_fid
, volatile_fid
);
2982 rc
= SendReceive2(xid
, ses
, &iov
, 1, &resp_buftype
, 0);
2984 cifs_stats_fail_inc(tcon
, SMB2_QUERY_INFO_HE
);
2987 rsp
= (struct smb2_query_info_rsp
*)iov
.iov_base
;
2989 info
= (struct smb2_fs_full_size_info
*)(4 /* RFC1001 len */ +
2990 le16_to_cpu(rsp
->OutputBufferOffset
) + (char *)&rsp
->hdr
);
2991 rc
= validate_buf(le16_to_cpu(rsp
->OutputBufferOffset
),
2992 le32_to_cpu(rsp
->OutputBufferLength
), &rsp
->hdr
,
2993 sizeof(struct smb2_fs_full_size_info
));
2995 copy_fs_info_to_kstatfs(info
, fsdata
);
2998 free_rsp_buf(resp_buftype
, iov
.iov_base
);
3003 SMB2_QFS_attr(const unsigned int xid
, struct cifs_tcon
*tcon
,
3004 u64 persistent_fid
, u64 volatile_fid
, int level
)
3006 struct smb2_query_info_rsp
*rsp
= NULL
;
3009 int resp_buftype
, max_len
, min_len
;
3010 struct cifs_ses
*ses
= tcon
->ses
;
3011 unsigned int rsp_len
, offset
;
3013 if (level
== FS_DEVICE_INFORMATION
) {
3014 max_len
= sizeof(FILE_SYSTEM_DEVICE_INFO
);
3015 min_len
= sizeof(FILE_SYSTEM_DEVICE_INFO
);
3016 } else if (level
== FS_ATTRIBUTE_INFORMATION
) {
3017 max_len
= sizeof(FILE_SYSTEM_ATTRIBUTE_INFO
);
3018 min_len
= MIN_FS_ATTR_INFO_SIZE
;
3019 } else if (level
== FS_SECTOR_SIZE_INFORMATION
) {
3020 max_len
= sizeof(struct smb3_fs_ss_info
);
3021 min_len
= sizeof(struct smb3_fs_ss_info
);
3023 cifs_dbg(FYI
, "Invalid qfsinfo level %d\n", level
);
3027 rc
= build_qfs_info_req(&iov
, tcon
, level
, max_len
,
3028 persistent_fid
, volatile_fid
);
3032 rc
= SendReceive2(xid
, ses
, &iov
, 1, &resp_buftype
, 0);
3034 cifs_stats_fail_inc(tcon
, SMB2_QUERY_INFO_HE
);
3037 rsp
= (struct smb2_query_info_rsp
*)iov
.iov_base
;
3039 rsp_len
= le32_to_cpu(rsp
->OutputBufferLength
);
3040 offset
= le16_to_cpu(rsp
->OutputBufferOffset
);
3041 rc
= validate_buf(offset
, rsp_len
, &rsp
->hdr
, min_len
);
3045 if (level
== FS_ATTRIBUTE_INFORMATION
)
3046 memcpy(&tcon
->fsAttrInfo
, 4 /* RFC1001 len */ + offset
3047 + (char *)&rsp
->hdr
, min_t(unsigned int,
3049 else if (level
== FS_DEVICE_INFORMATION
)
3050 memcpy(&tcon
->fsDevInfo
, 4 /* RFC1001 len */ + offset
3051 + (char *)&rsp
->hdr
, sizeof(FILE_SYSTEM_DEVICE_INFO
));
3052 else if (level
== FS_SECTOR_SIZE_INFORMATION
) {
3053 struct smb3_fs_ss_info
*ss_info
= (struct smb3_fs_ss_info
*)
3054 (4 /* RFC1001 len */ + offset
+ (char *)&rsp
->hdr
);
3055 tcon
->ss_flags
= le32_to_cpu(ss_info
->Flags
);
3056 tcon
->perf_sector_size
=
3057 le32_to_cpu(ss_info
->PhysicalBytesPerSectorForPerf
);
3061 free_rsp_buf(resp_buftype
, iov
.iov_base
);
3066 smb2_lockv(const unsigned int xid
, struct cifs_tcon
*tcon
,
3067 const __u64 persist_fid
, const __u64 volatile_fid
, const __u32 pid
,
3068 const __u32 num_lock
, struct smb2_lock_element
*buf
)
3071 struct smb2_lock_req
*req
= NULL
;
3076 cifs_dbg(FYI
, "smb2_lockv num lock %d\n", num_lock
);
3078 rc
= small_smb2_init(SMB2_LOCK
, tcon
, (void **) &req
);
3082 req
->hdr
.sync_hdr
.ProcessId
= cpu_to_le32(pid
);
3083 req
->LockCount
= cpu_to_le16(num_lock
);
3085 req
->PersistentFileId
= persist_fid
;
3086 req
->VolatileFileId
= volatile_fid
;
3088 count
= num_lock
* sizeof(struct smb2_lock_element
);
3089 inc_rfc1001_len(req
, count
- sizeof(struct smb2_lock_element
));
3091 iov
[0].iov_base
= (char *)req
;
3092 /* 4 for rfc1002 length field and count for all locks */
3093 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - count
;
3094 iov
[1].iov_base
= (char *)buf
;
3095 iov
[1].iov_len
= count
;
3097 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_locks
);
3098 rc
= SendReceive2(xid
, tcon
->ses
, iov
, 2, &resp_buf_type
, CIFS_NO_RESP
);
3100 cifs_dbg(FYI
, "Send error in smb2_lockv = %d\n", rc
);
3101 cifs_stats_fail_inc(tcon
, SMB2_LOCK_HE
);
3108 SMB2_lock(const unsigned int xid
, struct cifs_tcon
*tcon
,
3109 const __u64 persist_fid
, const __u64 volatile_fid
, const __u32 pid
,
3110 const __u64 length
, const __u64 offset
, const __u32 lock_flags
,
3113 struct smb2_lock_element lock
;
3115 lock
.Offset
= cpu_to_le64(offset
);
3116 lock
.Length
= cpu_to_le64(length
);
3117 lock
.Flags
= cpu_to_le32(lock_flags
);
3118 if (!wait
&& lock_flags
!= SMB2_LOCKFLAG_UNLOCK
)
3119 lock
.Flags
|= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY
);
3121 return smb2_lockv(xid
, tcon
, persist_fid
, volatile_fid
, pid
, 1, &lock
);
3125 SMB2_lease_break(const unsigned int xid
, struct cifs_tcon
*tcon
,
3126 __u8
*lease_key
, const __le32 lease_state
)
3129 struct smb2_lease_ack
*req
= NULL
;
3131 cifs_dbg(FYI
, "SMB2_lease_break\n");
3132 rc
= small_smb2_init(SMB2_OPLOCK_BREAK
, tcon
, (void **) &req
);
3137 req
->hdr
.sync_hdr
.CreditRequest
= cpu_to_le16(1);
3138 req
->StructureSize
= cpu_to_le16(36);
3139 inc_rfc1001_len(req
, 12);
3141 memcpy(req
->LeaseKey
, lease_key
, 16);
3142 req
->LeaseState
= lease_state
;
3144 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) req
, CIFS_OBREAK_OP
);
3145 /* SMB2 buffer freed by function above */
3148 cifs_stats_fail_inc(tcon
, SMB2_OPLOCK_BREAK_HE
);
3149 cifs_dbg(FYI
, "Send error in Lease Break = %d\n", rc
);