4 * Copyright (C) International Business Machines Corp., 2009, 2011
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"
51 * The following table defines the expected "StructureSize" of SMB2 requests
52 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
54 * Note that commands are defined in smb2pdu.h in le16 but the array below is
55 * indexed by command in host byte order.
57 static const int smb2_req_struct_sizes
[NUMBER_OF_SMB2_COMMANDS
] = {
58 /* SMB2_NEGOTIATE */ 36,
59 /* SMB2_SESSION_SETUP */ 25,
61 /* SMB2_TREE_CONNECT */ 9,
62 /* SMB2_TREE_DISCONNECT */ 4,
72 /* SMB2_QUERY_DIRECTORY */ 33,
73 /* SMB2_CHANGE_NOTIFY */ 32,
74 /* SMB2_QUERY_INFO */ 41,
75 /* SMB2_SET_INFO */ 33,
76 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
81 smb2_hdr_assemble(struct smb2_hdr
*hdr
, __le16 smb2_cmd
/* command */ ,
82 const struct cifs_tcon
*tcon
)
84 struct smb2_pdu
*pdu
= (struct smb2_pdu
*)hdr
;
85 char *temp
= (char *)hdr
;
86 /* lookup word count ie StructureSize from table */
87 __u16 parmsize
= smb2_req_struct_sizes
[le16_to_cpu(smb2_cmd
)];
90 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
91 * largest operations (Create)
95 /* Note this is only network field converted to big endian */
96 hdr
->smb2_buf_length
= cpu_to_be32(parmsize
+ sizeof(struct smb2_hdr
)
97 - 4 /* RFC 1001 length field itself not counted */);
99 hdr
->ProtocolId
[0] = 0xFE;
100 hdr
->ProtocolId
[1] = 'S';
101 hdr
->ProtocolId
[2] = 'M';
102 hdr
->ProtocolId
[3] = 'B';
103 hdr
->StructureSize
= cpu_to_le16(64);
104 hdr
->Command
= smb2_cmd
;
105 hdr
->CreditRequest
= cpu_to_le16(2); /* BB make this dynamic */
106 hdr
->ProcessId
= cpu_to_le32((__u16
)current
->tgid
);
111 hdr
->TreeId
= tcon
->tid
;
112 /* Uid is not converted */
114 hdr
->SessionId
= tcon
->ses
->Suid
;
115 /* BB check following DFS flags BB */
116 /* BB do we have to add check for SHI1005_FLAGS_DFS_ROOT too? */
117 if (tcon
->share_flags
& SHI1005_FLAGS_DFS
)
118 hdr
->Flags
|= SMB2_FLAGS_DFS_OPERATIONS
;
119 /* BB how does SMB2 do case sensitive? */
121 hdr->Flags |= SMBFLG_CASELESS; */
122 if (tcon
->ses
&& tcon
->ses
->server
&&
123 (tcon
->ses
->server
->sec_mode
& SECMODE_SIGN_REQUIRED
))
124 hdr
->Flags
|= SMB2_FLAGS_SIGNED
;
126 pdu
->StructureSize2
= cpu_to_le16(parmsize
);
131 smb2_reconnect(__le16 smb2_command
, struct cifs_tcon
*tcon
)
134 struct nls_table
*nls_codepage
;
135 struct cifs_ses
*ses
;
136 struct TCP_Server_Info
*server
;
139 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
140 * check for tcp and smb session status done differently
141 * for those three - in the calling routine.
146 if (smb2_command
== SMB2_TREE_CONNECT
)
149 if (tcon
->tidStatus
== CifsExiting
) {
151 * only tree disconnect, open, and write,
152 * (and ulogoff which does not have tcon)
153 * are allowed as we start force umount.
155 if ((smb2_command
!= SMB2_WRITE
) &&
156 (smb2_command
!= SMB2_CREATE
) &&
157 (smb2_command
!= SMB2_TREE_DISCONNECT
)) {
158 cFYI(1, "can not send cmd %d while umounting",
163 if ((!tcon
->ses
) || (tcon
->ses
->status
== CifsExiting
) ||
164 (!tcon
->ses
->server
))
168 server
= ses
->server
;
171 * Give demultiplex thread up to 10 seconds to reconnect, should be
172 * greater than cifs socket timeout which is 7 seconds
174 while (server
->tcpStatus
== CifsNeedReconnect
) {
176 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
177 * here since they are implicitly done when session drops.
179 switch (smb2_command
) {
181 * BB Should we keep oplock break and add flush to exceptions?
183 case SMB2_TREE_DISCONNECT
:
186 case SMB2_OPLOCK_BREAK
:
190 wait_event_interruptible_timeout(server
->response_q
,
191 (server
->tcpStatus
!= CifsNeedReconnect
), 10 * HZ
);
193 /* are we still trying to reconnect? */
194 if (server
->tcpStatus
!= CifsNeedReconnect
)
198 * on "soft" mounts we wait once. Hard mounts keep
199 * retrying until process is killed or server comes
203 cFYI(1, "gave up waiting on reconnect in smb_init");
208 if (!tcon
->ses
->need_reconnect
&& !tcon
->need_reconnect
)
211 nls_codepage
= load_nls_default();
214 * need to prevent multiple threads trying to simultaneously reconnect
215 * the same SMB session
217 mutex_lock(&tcon
->ses
->session_mutex
);
218 rc
= cifs_negotiate_protocol(0, tcon
->ses
);
219 if (!rc
&& tcon
->ses
->need_reconnect
)
220 rc
= cifs_setup_session(0, tcon
->ses
, nls_codepage
);
222 if (rc
|| !tcon
->need_reconnect
) {
223 mutex_unlock(&tcon
->ses
->session_mutex
);
227 cifs_mark_open_files_invalid(tcon
);
228 rc
= SMB2_tcon(0, tcon
->ses
, tcon
->treeName
, tcon
, nls_codepage
);
229 mutex_unlock(&tcon
->ses
->session_mutex
);
230 cFYI(1, "reconnect tcon rc = %d", rc
);
233 atomic_inc(&tconInfoReconnectCount
);
235 * BB FIXME add code to check if wsize needs update due to negotiated
236 * smb buffer size shrinking.
240 * Check if handle based operation so we know whether we can continue
241 * or not without returning to caller to reset file handle.
244 * BB Is flush done by server on drop of tcp session? Should we special
245 * case it and skip above?
247 switch (smb2_command
) {
253 case SMB2_QUERY_DIRECTORY
:
254 case SMB2_CHANGE_NOTIFY
:
255 case SMB2_QUERY_INFO
:
259 unload_nls(nls_codepage
);
264 * Allocate and return pointer to an SMB request hdr, and set basic
265 * SMB information in the SMB header. If the return code is zero, this
266 * function must have filled in request_buf pointer.
269 small_smb2_init(__le16 smb2_command
, struct cifs_tcon
*tcon
,
274 rc
= smb2_reconnect(smb2_command
, tcon
);
278 /* BB eventually switch this to SMB2 specific small buf size */
279 *request_buf
= cifs_small_buf_get();
280 if (*request_buf
== NULL
) {
281 /* BB should we add a retry in here if not a writepage? */
285 smb2_hdr_assemble((struct smb2_hdr
*) *request_buf
, smb2_command
, tcon
);
288 #ifdef CONFIG_CIFS_STATS2
289 uint16_t com_code
= le16_to_cpu(smb2_command
);
290 cifs_stats_inc(&tcon
->stats
.smb2_stats
.smb2_com_sent
[com_code
]);
292 cifs_stats_inc(&tcon
->num_smbs_sent
);
299 free_rsp_buf(int resp_buftype
, void *rsp
)
301 if (resp_buftype
== CIFS_SMALL_BUFFER
)
302 cifs_small_buf_release(rsp
);
303 else if (resp_buftype
== CIFS_LARGE_BUFFER
)
304 cifs_buf_release(rsp
);
307 #define SMB2_NUM_PROT 1
311 #define BAD_PROT 0xFFFF
313 #define SMB2_PROT_ID 0x0202
314 #define SMB21_PROT_ID 0x0210
315 #define BAD_PROT_ID 0xFFFF
320 } smb2protocols
[] = {
321 {SMB2_PROT
, cpu_to_le16(SMB2_PROT_ID
)},
322 {SMB21_PROT
, cpu_to_le16(SMB21_PROT_ID
)},
323 {BAD_PROT
, cpu_to_le16(BAD_PROT_ID
)}
328 * SMB2 Worker functions follow:
330 * The general structure of the worker functions is:
331 * 1) Call smb2_init (assembles SMB2 header)
332 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
333 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
334 * 4) Decode SMB2 command specific fields in the fixed length area
335 * 5) Decode variable length data area (if any for this SMB2 command type)
336 * 6) Call free smb buffer
342 SMB2_negotiate(const unsigned int xid
, struct cifs_ses
*ses
)
344 struct smb2_negotiate_req
*req
;
345 struct smb2_negotiate_rsp
*rsp
;
349 struct TCP_Server_Info
*server
;
350 unsigned int sec_flags
;
353 int blob_offset
, blob_length
;
355 int flags
= CIFS_NEG_OP
;
357 cFYI(1, "Negotiate protocol");
360 server
= ses
->server
;
366 rc
= small_smb2_init(SMB2_NEGOTIATE
, NULL
, (void **) &req
);
370 /* if any of auth flags (ie not sign or seal) are overriden use them */
371 if (ses
->overrideSecFlg
& (~(CIFSSEC_MUST_SIGN
| CIFSSEC_MUST_SEAL
)))
372 sec_flags
= ses
->overrideSecFlg
; /* BB FIXME fix sign flags?*/
373 else /* if override flags set only sign/seal OR them with global auth */
374 sec_flags
= global_secflags
| ses
->overrideSecFlg
;
376 cFYI(1, "sec_flags 0x%x", sec_flags
);
378 req
->hdr
.SessionId
= 0;
380 for (i
= 0; i
< SMB2_NUM_PROT
; i
++)
381 req
->Dialects
[i
] = smb2protocols
[i
].name
;
383 req
->DialectCount
= cpu_to_le16(i
);
384 inc_rfc1001_len(req
, i
* 2);
386 /* only one of SMB2 signing flags may be set in SMB2 request */
387 if ((sec_flags
& CIFSSEC_MUST_SIGN
) == CIFSSEC_MUST_SIGN
)
388 temp
= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
389 else if (sec_flags
& CIFSSEC_MAY_SIGN
) /* MAY_SIGN is a single flag */
390 temp
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
392 req
->SecurityMode
= cpu_to_le16(temp
);
394 req
->Capabilities
= cpu_to_le32(SMB2_GLOBAL_CAP_DFS
);
396 iov
[0].iov_base
= (char *)req
;
397 /* 4 for rfc1002 length field */
398 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
400 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buftype
, flags
);
402 rsp
= (struct smb2_negotiate_rsp
*)iov
[0].iov_base
;
404 * No tcon so can't do
405 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
415 cFYI(1, "mode 0x%x", rsp
->SecurityMode
);
417 if (rsp
->DialectRevision
== smb2protocols
[SMB21_PROT
].name
)
418 cFYI(1, "negotiated smb2.1 dialect");
419 else if (rsp
->DialectRevision
== smb2protocols
[SMB2_PROT
].name
)
420 cFYI(1, "negotiated smb2 dialect");
422 cERROR(1, "Illegal dialect returned by server %d",
423 le16_to_cpu(rsp
->DialectRevision
));
427 server
->dialect
= le16_to_cpu(rsp
->DialectRevision
);
429 server
->maxBuf
= le32_to_cpu(rsp
->MaxTransactSize
);
430 server
->max_read
= le32_to_cpu(rsp
->MaxReadSize
);
431 server
->max_write
= le32_to_cpu(rsp
->MaxWriteSize
);
432 /* BB Do we need to validate the SecurityMode? */
433 server
->sec_mode
= le16_to_cpu(rsp
->SecurityMode
);
434 server
->capabilities
= le32_to_cpu(rsp
->Capabilities
);
436 server
->capabilities
|= SMB2_NT_FIND
| SMB2_LARGE_FILES
;
438 security_blob
= smb2_get_data_area_len(&blob_offset
, &blob_length
,
440 if (blob_length
== 0) {
441 cERROR(1, "missing security blob on negprot");
446 cFYI(1, "sec_flags 0x%x", sec_flags
);
447 if (sec_flags
& CIFSSEC_MUST_SIGN
) {
448 cFYI(1, "Signing required");
449 if (!(server
->sec_mode
& (SMB2_NEGOTIATE_SIGNING_REQUIRED
|
450 SMB2_NEGOTIATE_SIGNING_ENABLED
))) {
451 cERROR(1, "signing required but server lacks support");
455 server
->sec_mode
|= SECMODE_SIGN_REQUIRED
;
456 } else if (sec_flags
& CIFSSEC_MAY_SIGN
) {
457 cFYI(1, "Signing optional");
458 if (server
->sec_mode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
) {
459 cFYI(1, "Server requires signing");
460 server
->sec_mode
|= SECMODE_SIGN_REQUIRED
;
463 ~(SECMODE_SIGN_ENABLED
| SECMODE_SIGN_REQUIRED
);
466 cFYI(1, "Signing disabled");
467 if (server
->sec_mode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
) {
468 cERROR(1, "Server requires packet signing to be enabled"
469 " in /proc/fs/cifs/SecurityFlags.");
474 ~(SECMODE_SIGN_ENABLED
| SECMODE_SIGN_REQUIRED
);
477 #ifdef CONFIG_SMB2_ASN1 /* BB REMOVEME when updated asn1.c ready */
478 rc
= decode_neg_token_init(security_blob
, blob_length
,
489 free_rsp_buf(resp_buftype
, rsp
);
494 SMB2_sess_setup(const unsigned int xid
, struct cifs_ses
*ses
,
495 const struct nls_table
*nls_cp
)
497 struct smb2_sess_setup_req
*req
;
498 struct smb2_sess_setup_rsp
*rsp
= NULL
;
502 __le32 phase
= NtLmNegotiate
; /* NTLMSSP, if needed, is multistage */
503 struct TCP_Server_Info
*server
;
504 unsigned int sec_flags
;
508 char *ntlmssp_blob
= NULL
;
509 bool use_spnego
= false; /* else use raw ntlmssp */
511 cFYI(1, "Session Setup");
514 server
= ses
->server
;
521 * If memory allocation is successful, caller of this function
524 ses
->ntlmssp
= kmalloc(sizeof(struct ntlmssp_auth
), GFP_KERNEL
);
528 ses
->server
->secType
= RawNTLMSSP
;
530 ssetup_ntlmssp_authenticate
:
531 if (phase
== NtLmChallenge
)
532 phase
= NtLmAuthenticate
; /* if ntlmssp, now final phase */
534 rc
= small_smb2_init(SMB2_SESSION_SETUP
, NULL
, (void **) &req
);
538 /* if any of auth flags (ie not sign or seal) are overriden use them */
539 if (ses
->overrideSecFlg
& (~(CIFSSEC_MUST_SIGN
| CIFSSEC_MUST_SEAL
)))
540 sec_flags
= ses
->overrideSecFlg
; /* BB FIXME fix sign flags?*/
541 else /* if override flags set only sign/seal OR them with global auth */
542 sec_flags
= global_secflags
| ses
->overrideSecFlg
;
544 cFYI(1, "sec_flags 0x%x", sec_flags
);
546 req
->hdr
.SessionId
= 0; /* First session, not a reauthenticate */
547 req
->VcNumber
= 0; /* MBZ */
548 /* to enable echos and oplocks */
549 req
->hdr
.CreditRequest
= cpu_to_le16(3);
551 /* only one of SMB2 signing flags may be set in SMB2 request */
552 if ((sec_flags
& CIFSSEC_MUST_SIGN
) == CIFSSEC_MUST_SIGN
)
553 temp
= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
554 else if (ses
->server
->sec_mode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
)
555 temp
= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
556 else if (sec_flags
& CIFSSEC_MAY_SIGN
) /* MAY_SIGN is a single flag */
557 temp
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
559 req
->SecurityMode
= temp
;
560 req
->Capabilities
= 0;
561 req
->Channel
= 0; /* MBZ */
563 iov
[0].iov_base
= (char *)req
;
564 /* 4 for rfc1002 length field and 1 for pad */
565 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
566 if (phase
== NtLmNegotiate
) {
567 ntlmssp_blob
= kmalloc(sizeof(struct _NEGOTIATE_MESSAGE
),
569 if (ntlmssp_blob
== NULL
) {
573 build_ntlmssp_negotiate_blob(ntlmssp_blob
, ses
);
575 /* blob_length = build_spnego_ntlmssp_blob(
577 sizeof(struct _NEGOTIATE_MESSAGE),
579 /* BB eventually need to add this */
580 cERROR(1, "spnego not supported for SMB2 yet");
585 blob_length
= sizeof(struct _NEGOTIATE_MESSAGE
);
586 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
587 security_blob
= ntlmssp_blob
;
589 } else if (phase
== NtLmAuthenticate
) {
590 req
->hdr
.SessionId
= ses
->Suid
;
591 ntlmssp_blob
= kzalloc(sizeof(struct _NEGOTIATE_MESSAGE
) + 500,
593 if (ntlmssp_blob
== NULL
) {
594 cERROR(1, "failed to malloc ntlmssp blob");
598 rc
= build_ntlmssp_auth_blob(ntlmssp_blob
, &blob_length
, ses
,
601 cFYI(1, "build_ntlmssp_auth_blob failed %d", rc
);
602 goto ssetup_exit
; /* BB double check error handling */
605 /* blob_length = build_spnego_ntlmssp_blob(
609 cERROR(1, "spnego not supported for SMB2 yet");
614 security_blob
= ntlmssp_blob
;
617 cERROR(1, "illegal ntlmssp phase");
622 /* Testing shows that buffer offset must be at location of Buffer[0] */
623 req
->SecurityBufferOffset
=
624 cpu_to_le16(sizeof(struct smb2_sess_setup_req
) -
625 1 /* pad */ - 4 /* rfc1001 len */);
626 req
->SecurityBufferLength
= cpu_to_le16(blob_length
);
627 iov
[1].iov_base
= security_blob
;
628 iov
[1].iov_len
= blob_length
;
630 inc_rfc1001_len(req
, blob_length
- 1 /* pad */);
632 /* BB add code to build os and lm fields */
634 rc
= SendReceive2(xid
, ses
, iov
, 2, &resp_buftype
, CIFS_LOG_ERROR
);
636 kfree(security_blob
);
637 rsp
= (struct smb2_sess_setup_rsp
*)iov
[0].iov_base
;
638 if (rsp
->hdr
.Status
== STATUS_MORE_PROCESSING_REQUIRED
) {
639 if (phase
!= NtLmNegotiate
) {
640 cERROR(1, "Unexpected more processing error");
643 if (offsetof(struct smb2_sess_setup_rsp
, Buffer
) - 4 !=
644 le16_to_cpu(rsp
->SecurityBufferOffset
)) {
645 cERROR(1, "Invalid security buffer offset %d",
646 le16_to_cpu(rsp
->SecurityBufferOffset
));
651 /* NTLMSSP Negotiate sent now processing challenge (response) */
652 phase
= NtLmChallenge
; /* process ntlmssp challenge */
653 rc
= 0; /* MORE_PROCESSING is not an error here but expected */
654 ses
->Suid
= rsp
->hdr
.SessionId
;
655 rc
= decode_ntlmssp_challenge(rsp
->Buffer
,
656 le16_to_cpu(rsp
->SecurityBufferLength
), ses
);
660 * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
661 * but at least the raw NTLMSSP case works.
664 * No tcon so can't do
665 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
675 ses
->session_flags
= le16_to_cpu(rsp
->SessionFlags
);
677 free_rsp_buf(resp_buftype
, rsp
);
679 /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
680 if ((phase
== NtLmChallenge
) && (rc
== 0))
681 goto ssetup_ntlmssp_authenticate
;
686 SMB2_logoff(const unsigned int xid
, struct cifs_ses
*ses
)
688 struct smb2_logoff_req
*req
; /* response is also trivial struct */
690 struct TCP_Server_Info
*server
;
692 cFYI(1, "disconnect session %p", ses
);
694 if (ses
&& (ses
->server
))
695 server
= ses
->server
;
699 rc
= small_smb2_init(SMB2_LOGOFF
, NULL
, (void **) &req
);
703 /* since no tcon, smb2_init can not do this, so do here */
704 req
->hdr
.SessionId
= ses
->Suid
;
705 if (server
->sec_mode
& SECMODE_SIGN_REQUIRED
)
706 req
->hdr
.Flags
|= SMB2_FLAGS_SIGNED
;
708 rc
= SendReceiveNoRsp(xid
, ses
, (char *) &req
->hdr
, 0);
710 * No tcon so can't do
711 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
716 static inline void cifs_stats_fail_inc(struct cifs_tcon
*tcon
, uint16_t code
)
718 cifs_stats_inc(&tcon
->stats
.smb2_stats
.smb2_com_failed
[code
]);
721 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
724 SMB2_tcon(const unsigned int xid
, struct cifs_ses
*ses
, const char *tree
,
725 struct cifs_tcon
*tcon
, const struct nls_table
*cp
)
727 struct smb2_tree_connect_req
*req
;
728 struct smb2_tree_connect_rsp
*rsp
= NULL
;
733 struct TCP_Server_Info
*server
;
734 __le16
*unc_path
= NULL
;
738 if ((ses
->server
) && tree
)
739 server
= ses
->server
;
743 if (tcon
&& tcon
->bad_network_name
)
746 unc_path
= kmalloc(MAX_SHARENAME_LENGTH
* 2, GFP_KERNEL
);
747 if (unc_path
== NULL
)
750 unc_path_len
= cifs_strtoUTF16(unc_path
, tree
, strlen(tree
), cp
) + 1;
752 if (unc_path_len
< 2) {
757 rc
= small_smb2_init(SMB2_TREE_CONNECT
, tcon
, (void **) &req
);
764 /* since no tcon, smb2_init can not do this, so do here */
765 req
->hdr
.SessionId
= ses
->Suid
;
766 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
767 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
770 iov
[0].iov_base
= (char *)req
;
771 /* 4 for rfc1002 length field and 1 for pad */
772 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
774 /* Testing shows that buffer offset must be at location of Buffer[0] */
775 req
->PathOffset
= cpu_to_le16(sizeof(struct smb2_tree_connect_req
)
776 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
777 req
->PathLength
= cpu_to_le16(unc_path_len
- 2);
778 iov
[1].iov_base
= unc_path
;
779 iov
[1].iov_len
= unc_path_len
;
781 inc_rfc1001_len(req
, unc_path_len
- 1 /* pad */);
783 rc
= SendReceive2(xid
, ses
, iov
, 2, &resp_buftype
, 0);
784 rsp
= (struct smb2_tree_connect_rsp
*)iov
[0].iov_base
;
788 cifs_stats_fail_inc(tcon
, SMB2_TREE_CONNECT_HE
);
789 tcon
->need_reconnect
= true;
791 goto tcon_error_exit
;
800 ses
->ipc_tid
= rsp
->hdr
.TreeId
;
804 if (rsp
->ShareType
& SMB2_SHARE_TYPE_DISK
)
805 cFYI(1, "connection to disk share");
806 else if (rsp
->ShareType
& SMB2_SHARE_TYPE_PIPE
) {
808 cFYI(1, "connection to pipe share");
809 } else if (rsp
->ShareType
& SMB2_SHARE_TYPE_PRINT
) {
811 cFYI(1, "connection to printer");
813 cERROR(1, "unknown share type %d", rsp
->ShareType
);
815 goto tcon_error_exit
;
818 tcon
->share_flags
= le32_to_cpu(rsp
->ShareFlags
);
819 tcon
->maximal_access
= le32_to_cpu(rsp
->MaximalAccess
);
820 tcon
->tidStatus
= CifsGood
;
821 tcon
->need_reconnect
= false;
822 tcon
->tid
= rsp
->hdr
.TreeId
;
823 strncpy(tcon
->treeName
, tree
, MAX_TREE_SIZE
);
825 if ((rsp
->Capabilities
& SMB2_SHARE_CAP_DFS
) &&
826 ((tcon
->share_flags
& SHI1005_FLAGS_DFS
) == 0))
827 cERROR(1, "DFS capability contradicts DFS flag");
830 free_rsp_buf(resp_buftype
, rsp
);
835 if (rsp
->hdr
.Status
== STATUS_BAD_NETWORK_NAME
) {
836 cERROR(1, "BAD_NETWORK_NAME: %s", tree
);
837 tcon
->bad_network_name
= true;
843 SMB2_tdis(const unsigned int xid
, struct cifs_tcon
*tcon
)
845 struct smb2_tree_disconnect_req
*req
; /* response is trivial */
847 struct TCP_Server_Info
*server
;
848 struct cifs_ses
*ses
= tcon
->ses
;
850 cFYI(1, "Tree Disconnect");
852 if (ses
&& (ses
->server
))
853 server
= ses
->server
;
857 if ((tcon
->need_reconnect
) || (tcon
->ses
->need_reconnect
))
860 rc
= small_smb2_init(SMB2_TREE_DISCONNECT
, tcon
, (void **) &req
);
864 rc
= SendReceiveNoRsp(xid
, ses
, (char *)&req
->hdr
, 0);
866 cifs_stats_fail_inc(tcon
, SMB2_TREE_DISCONNECT_HE
);
872 SMB2_open(const unsigned int xid
, struct cifs_tcon
*tcon
, __le16
*path
,
873 u64
*persistent_fid
, u64
*volatile_fid
, __u32 desired_access
,
874 __u32 create_disposition
, __u32 file_attributes
, __u32 create_options
,
875 struct smb2_file_all_info
*buf
)
877 struct smb2_create_req
*req
;
878 struct smb2_create_rsp
*rsp
;
879 struct TCP_Server_Info
*server
;
880 struct cifs_ses
*ses
= tcon
->ses
;
887 cFYI(1, "create/open");
889 if (ses
&& (ses
->server
))
890 server
= ses
->server
;
894 rc
= small_smb2_init(SMB2_CREATE
, tcon
, (void **) &req
);
898 /* if (server->oplocks)
899 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_BATCH;
901 req
->RequestedOplockLevel
= SMB2_OPLOCK_LEVEL_NONE
;
902 req
->ImpersonationLevel
= IL_IMPERSONATION
;
903 req
->DesiredAccess
= cpu_to_le32(desired_access
);
904 /* File attributes ignored on open (used in create though) */
905 req
->FileAttributes
= cpu_to_le32(file_attributes
);
906 req
->ShareAccess
= FILE_SHARE_ALL_LE
;
907 req
->CreateDisposition
= cpu_to_le32(create_disposition
);
908 req
->CreateOptions
= cpu_to_le32(create_options
);
909 uni_path_len
= (2 * UniStrnlen((wchar_t *)path
, PATH_MAX
)) + 2;
910 req
->NameOffset
= cpu_to_le16(sizeof(struct smb2_create_req
)
911 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
913 iov
[0].iov_base
= (char *)req
;
914 /* 4 for rfc1002 length field */
915 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
917 /* MUST set path len (NameLength) to 0 opening root of share */
918 if (uni_path_len
>= 4) {
919 req
->NameLength
= cpu_to_le16(uni_path_len
- 2);
920 /* -1 since last byte is buf[0] which is sent below (path) */
922 iov
[1].iov_len
= uni_path_len
;
923 iov
[1].iov_base
= path
;
925 * -1 since last byte is buf[0] which was counted in
928 inc_rfc1001_len(req
, uni_path_len
- 1);
934 rc
= SendReceive2(xid
, ses
, iov
, num_iovecs
, &resp_buftype
, 0);
935 rsp
= (struct smb2_create_rsp
*)iov
[0].iov_base
;
938 cifs_stats_fail_inc(tcon
, SMB2_CREATE_HE
);
946 *persistent_fid
= rsp
->PersistentFileId
;
947 *volatile_fid
= rsp
->VolatileFileId
;
950 memcpy(buf
, &rsp
->CreationTime
, 32);
951 buf
->AllocationSize
= rsp
->AllocationSize
;
952 buf
->EndOfFile
= rsp
->EndofFile
;
953 buf
->Attributes
= rsp
->FileAttributes
;
954 buf
->NumberOfLinks
= cpu_to_le32(1);
955 buf
->DeletePending
= 0;
958 free_rsp_buf(resp_buftype
, rsp
);
963 SMB2_close(const unsigned int xid
, struct cifs_tcon
*tcon
,
964 u64 persistent_fid
, u64 volatile_fid
)
966 struct smb2_close_req
*req
;
967 struct smb2_close_rsp
*rsp
;
968 struct TCP_Server_Info
*server
;
969 struct cifs_ses
*ses
= tcon
->ses
;
976 if (ses
&& (ses
->server
))
977 server
= ses
->server
;
981 rc
= small_smb2_init(SMB2_CLOSE
, tcon
, (void **) &req
);
985 req
->PersistentFileId
= persistent_fid
;
986 req
->VolatileFileId
= volatile_fid
;
988 iov
[0].iov_base
= (char *)req
;
989 /* 4 for rfc1002 length field */
990 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
992 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buftype
, 0);
993 rsp
= (struct smb2_close_rsp
*)iov
[0].iov_base
;
997 cifs_stats_fail_inc(tcon
, SMB2_CLOSE_HE
);
1006 /* BB FIXME - decode close response, update inode for caching */
1009 free_rsp_buf(resp_buftype
, rsp
);
1014 validate_buf(unsigned int offset
, unsigned int buffer_length
,
1015 struct smb2_hdr
*hdr
, unsigned int min_buf_size
)
1018 unsigned int smb_len
= be32_to_cpu(hdr
->smb2_buf_length
);
1019 char *end_of_smb
= smb_len
+ 4 /* RFC1001 length field */ + (char *)hdr
;
1020 char *begin_of_buf
= 4 /* RFC1001 len field */ + offset
+ (char *)hdr
;
1021 char *end_of_buf
= begin_of_buf
+ buffer_length
;
1024 if (buffer_length
< min_buf_size
) {
1025 cERROR(1, "buffer length %d smaller than minimum size %d",
1026 buffer_length
, min_buf_size
);
1030 /* check if beyond RFC1001 maximum length */
1031 if ((smb_len
> 0x7FFFFF) || (buffer_length
> 0x7FFFFF)) {
1032 cERROR(1, "buffer length %d or smb length %d too large",
1033 buffer_length
, smb_len
);
1037 if ((begin_of_buf
> end_of_smb
) || (end_of_buf
> end_of_smb
)) {
1038 cERROR(1, "illegal server response, bad offset to data");
1046 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1047 * Caller must free buffer.
1050 validate_and_copy_buf(unsigned int offset
, unsigned int buffer_length
,
1051 struct smb2_hdr
*hdr
, unsigned int minbufsize
,
1055 char *begin_of_buf
= 4 /* RFC1001 len field */ + offset
+ (char *)hdr
;
1061 rc
= validate_buf(offset
, buffer_length
, hdr
, minbufsize
);
1065 memcpy(data
, begin_of_buf
, buffer_length
);
1071 query_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
1072 u64 persistent_fid
, u64 volatile_fid
, u8 info_class
,
1073 size_t output_len
, size_t min_len
, void *data
)
1075 struct smb2_query_info_req
*req
;
1076 struct smb2_query_info_rsp
*rsp
= NULL
;
1080 struct TCP_Server_Info
*server
;
1081 struct cifs_ses
*ses
= tcon
->ses
;
1083 cFYI(1, "Query Info");
1085 if (ses
&& (ses
->server
))
1086 server
= ses
->server
;
1090 rc
= small_smb2_init(SMB2_QUERY_INFO
, tcon
, (void **) &req
);
1094 req
->InfoType
= SMB2_O_INFO_FILE
;
1095 req
->FileInfoClass
= info_class
;
1096 req
->PersistentFileId
= persistent_fid
;
1097 req
->VolatileFileId
= volatile_fid
;
1098 /* 4 for rfc1002 length field and 1 for Buffer */
1099 req
->InputBufferOffset
=
1100 cpu_to_le16(sizeof(struct smb2_query_info_req
) - 1 - 4);
1101 req
->OutputBufferLength
= cpu_to_le32(output_len
);
1103 iov
[0].iov_base
= (char *)req
;
1104 /* 4 for rfc1002 length field */
1105 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
1107 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buftype
, 0);
1109 cifs_stats_fail_inc(tcon
, SMB2_QUERY_INFO_HE
);
1113 rsp
= (struct smb2_query_info_rsp
*)iov
[0].iov_base
;
1115 rc
= validate_and_copy_buf(le16_to_cpu(rsp
->OutputBufferOffset
),
1116 le32_to_cpu(rsp
->OutputBufferLength
),
1117 &rsp
->hdr
, min_len
, data
);
1120 free_rsp_buf(resp_buftype
, rsp
);
1125 SMB2_query_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
1126 u64 persistent_fid
, u64 volatile_fid
,
1127 struct smb2_file_all_info
*data
)
1129 return query_info(xid
, tcon
, persistent_fid
, volatile_fid
,
1130 FILE_ALL_INFORMATION
,
1131 sizeof(struct smb2_file_all_info
) + MAX_NAME
* 2,
1132 sizeof(struct smb2_file_all_info
), data
);
1136 SMB2_get_srv_num(const unsigned int xid
, struct cifs_tcon
*tcon
,
1137 u64 persistent_fid
, u64 volatile_fid
, __le64
*uniqueid
)
1139 return query_info(xid
, tcon
, persistent_fid
, volatile_fid
,
1140 FILE_INTERNAL_INFORMATION
,
1141 sizeof(struct smb2_file_internal_info
),
1142 sizeof(struct smb2_file_internal_info
), uniqueid
);
1146 * This is a no-op for now. We're not really interested in the reply, but
1147 * rather in the fact that the server sent one and that server->lstrp
1150 * FIXME: maybe we should consider checking that the reply matches request?
1153 smb2_echo_callback(struct mid_q_entry
*mid
)
1155 struct TCP_Server_Info
*server
= mid
->callback_data
;
1156 struct smb2_echo_rsp
*smb2
= (struct smb2_echo_rsp
*)mid
->resp_buf
;
1157 unsigned int credits_received
= 1;
1159 if (mid
->mid_state
== MID_RESPONSE_RECEIVED
)
1160 credits_received
= le16_to_cpu(smb2
->hdr
.CreditRequest
);
1162 DeleteMidQEntry(mid
);
1163 add_credits(server
, credits_received
, CIFS_ECHO_OP
);
1167 SMB2_echo(struct TCP_Server_Info
*server
)
1169 struct smb2_echo_req
*req
;
1173 cFYI(1, "In echo request");
1175 rc
= small_smb2_init(SMB2_ECHO
, NULL
, (void **)&req
);
1179 req
->hdr
.CreditRequest
= cpu_to_le16(1);
1181 iov
.iov_base
= (char *)req
;
1182 /* 4 for rfc1002 length field */
1183 iov
.iov_len
= get_rfc1002_length(req
) + 4;
1185 rc
= cifs_call_async(server
, &iov
, 1, NULL
, smb2_echo_callback
, server
,
1188 cFYI(1, "Echo request failed: %d", rc
);
1190 cifs_small_buf_release(req
);
1195 SMB2_flush(const unsigned int xid
, struct cifs_tcon
*tcon
, u64 persistent_fid
,
1198 struct smb2_flush_req
*req
;
1199 struct TCP_Server_Info
*server
;
1200 struct cifs_ses
*ses
= tcon
->ses
;
1207 if (ses
&& (ses
->server
))
1208 server
= ses
->server
;
1212 rc
= small_smb2_init(SMB2_FLUSH
, tcon
, (void **) &req
);
1216 req
->PersistentFileId
= persistent_fid
;
1217 req
->VolatileFileId
= volatile_fid
;
1219 iov
[0].iov_base
= (char *)req
;
1220 /* 4 for rfc1002 length field */
1221 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
1223 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buftype
, 0);
1225 if ((rc
!= 0) && tcon
)
1226 cifs_stats_fail_inc(tcon
, SMB2_FLUSH_HE
);
1228 free_rsp_buf(resp_buftype
, iov
[0].iov_base
);
1233 * To form a chain of read requests, any read requests after the first should
1234 * have the end_of_chain boolean set to true.
1237 smb2_new_read_req(struct kvec
*iov
, struct cifs_io_parms
*io_parms
,
1238 unsigned int remaining_bytes
, int request_type
)
1241 struct smb2_read_req
*req
= NULL
;
1243 rc
= small_smb2_init(SMB2_READ
, io_parms
->tcon
, (void **) &req
);
1246 if (io_parms
->tcon
->ses
->server
== NULL
)
1247 return -ECONNABORTED
;
1249 req
->hdr
.ProcessId
= cpu_to_le32(io_parms
->pid
);
1251 req
->PersistentFileId
= io_parms
->persistent_fid
;
1252 req
->VolatileFileId
= io_parms
->volatile_fid
;
1253 req
->ReadChannelInfoOffset
= 0; /* reserved */
1254 req
->ReadChannelInfoLength
= 0; /* reserved */
1255 req
->Channel
= 0; /* reserved */
1256 req
->MinimumCount
= 0;
1257 req
->Length
= cpu_to_le32(io_parms
->length
);
1258 req
->Offset
= cpu_to_le64(io_parms
->offset
);
1260 if (request_type
& CHAINED_REQUEST
) {
1261 if (!(request_type
& END_OF_CHAIN
)) {
1262 /* 4 for rfc1002 length field */
1263 req
->hdr
.NextCommand
=
1264 cpu_to_le32(get_rfc1002_length(req
) + 4);
1265 } else /* END_OF_CHAIN */
1266 req
->hdr
.NextCommand
= 0;
1267 if (request_type
& RELATED_REQUEST
) {
1268 req
->hdr
.Flags
|= SMB2_FLAGS_RELATED_OPERATIONS
;
1270 * Related requests use info from previous read request
1273 req
->hdr
.SessionId
= 0xFFFFFFFF;
1274 req
->hdr
.TreeId
= 0xFFFFFFFF;
1275 req
->PersistentFileId
= 0xFFFFFFFF;
1276 req
->VolatileFileId
= 0xFFFFFFFF;
1279 if (remaining_bytes
> io_parms
->length
)
1280 req
->RemainingBytes
= cpu_to_le32(remaining_bytes
);
1282 req
->RemainingBytes
= 0;
1284 iov
[0].iov_base
= (char *)req
;
1285 /* 4 for rfc1002 length field */
1286 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
1291 smb2_readv_callback(struct mid_q_entry
*mid
)
1293 struct cifs_readdata
*rdata
= mid
->callback_data
;
1294 struct cifs_tcon
*tcon
= tlink_tcon(rdata
->cfile
->tlink
);
1295 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
1296 struct smb2_hdr
*buf
= (struct smb2_hdr
*)rdata
->iov
[0].iov_base
;
1297 unsigned int credits_received
= 1;
1299 cFYI(1, "%s: mid=%llu state=%d result=%d bytes=%u", __func__
,
1300 mid
->mid
, mid
->mid_state
, rdata
->result
, rdata
->bytes
);
1302 switch (mid
->mid_state
) {
1303 case MID_RESPONSE_RECEIVED
:
1304 credits_received
= le16_to_cpu(buf
->CreditRequest
);
1305 /* result already set, check signature */
1306 if (server
->sec_mode
&
1307 (SECMODE_SIGN_REQUIRED
| SECMODE_SIGN_ENABLED
)) {
1310 rc
= smb2_verify_signature2(rdata
->iov
, rdata
->nr_iov
,
1313 cERROR(1, "SMB signature verification returned "
1316 /* FIXME: should this be counted toward the initiating task? */
1317 task_io_account_read(rdata
->bytes
);
1318 cifs_stats_bytes_read(tcon
, rdata
->bytes
);
1320 case MID_REQUEST_SUBMITTED
:
1321 case MID_RETRY_NEEDED
:
1322 rdata
->result
= -EAGAIN
;
1325 if (rdata
->result
!= -ENODATA
)
1326 rdata
->result
= -EIO
;
1330 cifs_stats_fail_inc(tcon
, SMB2_READ_HE
);
1332 queue_work(cifsiod_wq
, &rdata
->work
);
1333 DeleteMidQEntry(mid
);
1334 add_credits(server
, credits_received
, 0);
1337 /* smb2_async_readv - send an async write, and set up mid to handle result */
1339 smb2_async_readv(struct cifs_readdata
*rdata
)
1342 struct smb2_hdr
*buf
;
1343 struct cifs_io_parms io_parms
;
1345 cFYI(1, "%s: offset=%llu bytes=%u", __func__
,
1346 rdata
->offset
, rdata
->bytes
);
1348 io_parms
.tcon
= tlink_tcon(rdata
->cfile
->tlink
);
1349 io_parms
.offset
= rdata
->offset
;
1350 io_parms
.length
= rdata
->bytes
;
1351 io_parms
.persistent_fid
= rdata
->cfile
->fid
.persistent_fid
;
1352 io_parms
.volatile_fid
= rdata
->cfile
->fid
.volatile_fid
;
1353 io_parms
.pid
= rdata
->pid
;
1354 rc
= smb2_new_read_req(&rdata
->iov
[0], &io_parms
, 0, 0);
1358 buf
= (struct smb2_hdr
*)rdata
->iov
[0].iov_base
;
1359 /* 4 for rfc1002 length field */
1360 rdata
->iov
[0].iov_len
= get_rfc1002_length(rdata
->iov
[0].iov_base
) + 4;
1362 kref_get(&rdata
->refcount
);
1363 rc
= cifs_call_async(io_parms
.tcon
->ses
->server
, rdata
->iov
, 1,
1364 cifs_readv_receive
, smb2_readv_callback
,
1367 kref_put(&rdata
->refcount
, cifs_readdata_release
);
1369 cifs_small_buf_release(buf
);
1374 SMB2_read(const unsigned int xid
, struct cifs_io_parms
*io_parms
,
1375 unsigned int *nbytes
, char **buf
, int *buf_type
)
1377 int resp_buftype
, rc
= -EACCES
;
1378 struct smb2_read_rsp
*rsp
= NULL
;
1382 rc
= smb2_new_read_req(iov
, io_parms
, 0, 0);
1386 rc
= SendReceive2(xid
, io_parms
->tcon
->ses
, iov
, 1,
1387 &resp_buftype
, CIFS_LOG_ERROR
);
1389 rsp
= (struct smb2_read_rsp
*)iov
[0].iov_base
;
1391 if (rsp
->hdr
.Status
== STATUS_END_OF_FILE
) {
1392 free_rsp_buf(resp_buftype
, iov
[0].iov_base
);
1397 cifs_stats_fail_inc(io_parms
->tcon
, SMB2_READ_HE
);
1398 cERROR(1, "Send error in read = %d", rc
);
1400 *nbytes
= le32_to_cpu(rsp
->DataLength
);
1401 if ((*nbytes
> CIFS_MAX_MSGSIZE
) ||
1402 (*nbytes
> io_parms
->length
)) {
1403 cFYI(1, "bad length %d for count %d", *nbytes
,
1411 memcpy(*buf
, (char *)rsp
->hdr
.ProtocolId
+ rsp
->DataOffset
,
1413 free_rsp_buf(resp_buftype
, iov
[0].iov_base
);
1414 } else if (resp_buftype
!= CIFS_NO_BUFFER
) {
1415 *buf
= iov
[0].iov_base
;
1416 if (resp_buftype
== CIFS_SMALL_BUFFER
)
1417 *buf_type
= CIFS_SMALL_BUFFER
;
1418 else if (resp_buftype
== CIFS_LARGE_BUFFER
)
1419 *buf_type
= CIFS_LARGE_BUFFER
;
1425 * Check the mid_state and signature on received buffer (if any), and queue the
1426 * workqueue completion task.
1429 smb2_writev_callback(struct mid_q_entry
*mid
)
1431 struct cifs_writedata
*wdata
= mid
->callback_data
;
1432 struct cifs_tcon
*tcon
= tlink_tcon(wdata
->cfile
->tlink
);
1433 unsigned int written
;
1434 struct smb2_write_rsp
*rsp
= (struct smb2_write_rsp
*)mid
->resp_buf
;
1435 unsigned int credits_received
= 1;
1437 switch (mid
->mid_state
) {
1438 case MID_RESPONSE_RECEIVED
:
1439 credits_received
= le16_to_cpu(rsp
->hdr
.CreditRequest
);
1440 wdata
->result
= smb2_check_receive(mid
, tcon
->ses
->server
, 0);
1441 if (wdata
->result
!= 0)
1444 written
= le32_to_cpu(rsp
->DataLength
);
1446 * Mask off high 16 bits when bytes written as returned
1447 * by the server is greater than bytes requested by the
1448 * client. OS/2 servers are known to set incorrect
1451 if (written
> wdata
->bytes
)
1454 if (written
< wdata
->bytes
)
1455 wdata
->result
= -ENOSPC
;
1457 wdata
->bytes
= written
;
1459 case MID_REQUEST_SUBMITTED
:
1460 case MID_RETRY_NEEDED
:
1461 wdata
->result
= -EAGAIN
;
1464 wdata
->result
= -EIO
;
1469 cifs_stats_fail_inc(tcon
, SMB2_WRITE_HE
);
1471 queue_work(cifsiod_wq
, &wdata
->work
);
1472 DeleteMidQEntry(mid
);
1473 add_credits(tcon
->ses
->server
, credits_received
, 0);
1476 /* smb2_async_writev - send an async write, and set up mid to handle result */
1478 smb2_async_writev(struct cifs_writedata
*wdata
)
1480 int i
, rc
= -EACCES
;
1481 struct smb2_write_req
*req
= NULL
;
1482 struct cifs_tcon
*tcon
= tlink_tcon(wdata
->cfile
->tlink
);
1483 struct kvec
*iov
= NULL
;
1485 rc
= small_smb2_init(SMB2_WRITE
, tcon
, (void **) &req
);
1487 goto async_writev_out
;
1489 /* 1 iov per page + 1 for header */
1490 iov
= kzalloc((wdata
->nr_pages
+ 1) * sizeof(*iov
), GFP_NOFS
);
1493 goto async_writev_out
;
1496 req
->hdr
.ProcessId
= cpu_to_le32(wdata
->cfile
->pid
);
1498 req
->PersistentFileId
= wdata
->cfile
->fid
.persistent_fid
;
1499 req
->VolatileFileId
= wdata
->cfile
->fid
.volatile_fid
;
1500 req
->WriteChannelInfoOffset
= 0;
1501 req
->WriteChannelInfoLength
= 0;
1503 req
->Offset
= cpu_to_le64(wdata
->offset
);
1504 /* 4 for rfc1002 length field */
1505 req
->DataOffset
= cpu_to_le16(
1506 offsetof(struct smb2_write_req
, Buffer
) - 4);
1507 req
->RemainingBytes
= 0;
1509 /* 4 for rfc1002 length field and 1 for Buffer */
1510 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
1511 iov
[0].iov_base
= (char *)req
;
1514 * This function should marshal up the page array into the kvec
1515 * array, reserving [0] for the header. It should kmap the pages
1516 * and set the iov_len properly for each one. It may also set
1520 wdata
->marshal_iov(iov
, wdata
);
1523 cFYI(1, "async write at %llu %u bytes", wdata
->offset
, wdata
->bytes
);
1525 req
->Length
= cpu_to_le32(wdata
->bytes
);
1527 inc_rfc1001_len(&req
->hdr
, wdata
->bytes
- 1 /* Buffer */);
1529 kref_get(&wdata
->refcount
);
1530 rc
= cifs_call_async(tcon
->ses
->server
, iov
, wdata
->nr_pages
+ 1,
1531 NULL
, smb2_writev_callback
, wdata
, 0);
1534 kref_put(&wdata
->refcount
, cifs_writedata_release
);
1536 /* send is done, unmap pages */
1537 for (i
= 0; i
< wdata
->nr_pages
; i
++)
1538 kunmap(wdata
->pages
[i
]);
1541 cifs_small_buf_release(req
);
1547 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
1548 * The length field from io_parms must be at least 1 and indicates a number of
1549 * elements with data to write that begins with position 1 in iov array. All
1550 * data length is specified by count.
1553 SMB2_write(const unsigned int xid
, struct cifs_io_parms
*io_parms
,
1554 unsigned int *nbytes
, struct kvec
*iov
, int n_vec
)
1557 struct smb2_write_req
*req
= NULL
;
1558 struct smb2_write_rsp
*rsp
= NULL
;
1565 rc
= small_smb2_init(SMB2_WRITE
, io_parms
->tcon
, (void **) &req
);
1569 if (io_parms
->tcon
->ses
->server
== NULL
)
1570 return -ECONNABORTED
;
1572 req
->hdr
.ProcessId
= cpu_to_le32(io_parms
->pid
);
1574 req
->PersistentFileId
= io_parms
->persistent_fid
;
1575 req
->VolatileFileId
= io_parms
->volatile_fid
;
1576 req
->WriteChannelInfoOffset
= 0;
1577 req
->WriteChannelInfoLength
= 0;
1579 req
->Length
= cpu_to_le32(io_parms
->length
);
1580 req
->Offset
= cpu_to_le64(io_parms
->offset
);
1581 /* 4 for rfc1002 length field */
1582 req
->DataOffset
= cpu_to_le16(
1583 offsetof(struct smb2_write_req
, Buffer
) - 4);
1584 req
->RemainingBytes
= 0;
1586 iov
[0].iov_base
= (char *)req
;
1587 /* 4 for rfc1002 length field and 1 for Buffer */
1588 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
1590 /* length of entire message including data to be written */
1591 inc_rfc1001_len(req
, io_parms
->length
- 1 /* Buffer */);
1593 rc
= SendReceive2(xid
, io_parms
->tcon
->ses
, iov
, n_vec
+ 1,
1597 cifs_stats_fail_inc(io_parms
->tcon
, SMB2_WRITE_HE
);
1598 cERROR(1, "Send error in write = %d", rc
);
1600 rsp
= (struct smb2_write_rsp
*)iov
[0].iov_base
;
1601 *nbytes
= le32_to_cpu(rsp
->DataLength
);
1602 free_rsp_buf(resp_buftype
, rsp
);
1608 num_entries(char *bufstart
, char *end_of_buf
, char **lastentry
, size_t size
)
1611 unsigned int entrycount
= 0;
1612 unsigned int next_offset
= 0;
1613 FILE_DIRECTORY_INFO
*entryptr
;
1615 if (bufstart
== NULL
)
1618 entryptr
= (FILE_DIRECTORY_INFO
*)bufstart
;
1621 entryptr
= (FILE_DIRECTORY_INFO
*)
1622 ((char *)entryptr
+ next_offset
);
1624 if ((char *)entryptr
+ size
> end_of_buf
) {
1625 cERROR(1, "malformed search entry would overflow");
1629 len
= le32_to_cpu(entryptr
->FileNameLength
);
1630 if ((char *)entryptr
+ len
+ size
> end_of_buf
) {
1631 cERROR(1, "directory entry name would overflow frame "
1632 "end of buf %p", end_of_buf
);
1636 *lastentry
= (char *)entryptr
;
1639 next_offset
= le32_to_cpu(entryptr
->NextEntryOffset
);
1651 SMB2_query_directory(const unsigned int xid
, struct cifs_tcon
*tcon
,
1652 u64 persistent_fid
, u64 volatile_fid
, int index
,
1653 struct cifs_search_info
*srch_inf
)
1655 struct smb2_query_directory_req
*req
;
1656 struct smb2_query_directory_rsp
*rsp
= NULL
;
1661 unsigned char *bufptr
;
1662 struct TCP_Server_Info
*server
;
1663 struct cifs_ses
*ses
= tcon
->ses
;
1664 __le16 asteriks
= cpu_to_le16('*');
1666 unsigned int output_size
= CIFSMaxBufSize
;
1667 size_t info_buf_size
;
1669 if (ses
&& (ses
->server
))
1670 server
= ses
->server
;
1674 rc
= small_smb2_init(SMB2_QUERY_DIRECTORY
, tcon
, (void **) &req
);
1678 switch (srch_inf
->info_level
) {
1679 case SMB_FIND_FILE_DIRECTORY_INFO
:
1680 req
->FileInformationClass
= FILE_DIRECTORY_INFORMATION
;
1681 info_buf_size
= sizeof(FILE_DIRECTORY_INFO
) - 1;
1683 case SMB_FIND_FILE_ID_FULL_DIR_INFO
:
1684 req
->FileInformationClass
= FILEID_FULL_DIRECTORY_INFORMATION
;
1685 info_buf_size
= sizeof(SEARCH_ID_FULL_DIR_INFO
) - 1;
1688 cERROR(1, "info level %u isn't supported",
1689 srch_inf
->info_level
);
1694 req
->FileIndex
= cpu_to_le32(index
);
1695 req
->PersistentFileId
= persistent_fid
;
1696 req
->VolatileFileId
= volatile_fid
;
1699 bufptr
= req
->Buffer
;
1700 memcpy(bufptr
, &asteriks
, len
);
1702 req
->FileNameOffset
=
1703 cpu_to_le16(sizeof(struct smb2_query_directory_req
) - 1 - 4);
1704 req
->FileNameLength
= cpu_to_le16(len
);
1706 * BB could be 30 bytes or so longer if we used SMB2 specific
1707 * buffer lengths, but this is safe and close enough.
1709 output_size
= min_t(unsigned int, output_size
, server
->maxBuf
);
1710 output_size
= min_t(unsigned int, output_size
, 2 << 15);
1711 req
->OutputBufferLength
= cpu_to_le32(output_size
);
1713 iov
[0].iov_base
= (char *)req
;
1714 /* 4 for RFC1001 length and 1 for Buffer */
1715 iov
[0].iov_len
= get_rfc1002_length(req
) + 4 - 1;
1717 iov
[1].iov_base
= (char *)(req
->Buffer
);
1718 iov
[1].iov_len
= len
;
1720 inc_rfc1001_len(req
, len
- 1 /* Buffer */);
1722 rc
= SendReceive2(xid
, ses
, iov
, 2, &resp_buftype
, 0);
1724 cifs_stats_fail_inc(tcon
, SMB2_QUERY_DIRECTORY_HE
);
1727 rsp
= (struct smb2_query_directory_rsp
*)iov
[0].iov_base
;
1729 rc
= validate_buf(le16_to_cpu(rsp
->OutputBufferOffset
),
1730 le32_to_cpu(rsp
->OutputBufferLength
), &rsp
->hdr
,
1735 srch_inf
->unicode
= true;
1737 if (srch_inf
->ntwrk_buf_start
) {
1738 if (srch_inf
->smallBuf
)
1739 cifs_small_buf_release(srch_inf
->ntwrk_buf_start
);
1741 cifs_buf_release(srch_inf
->ntwrk_buf_start
);
1743 srch_inf
->ntwrk_buf_start
= (char *)rsp
;
1744 srch_inf
->srch_entries_start
= srch_inf
->last_entry
= 4 /* rfclen */ +
1745 (char *)&rsp
->hdr
+ le16_to_cpu(rsp
->OutputBufferOffset
);
1746 /* 4 for rfc1002 length field */
1747 end_of_smb
= get_rfc1002_length(rsp
) + 4 + (char *)&rsp
->hdr
;
1748 srch_inf
->entries_in_buffer
=
1749 num_entries(srch_inf
->srch_entries_start
, end_of_smb
,
1750 &srch_inf
->last_entry
, info_buf_size
);
1751 srch_inf
->index_of_last_entry
+= srch_inf
->entries_in_buffer
;
1752 cFYI(1, "num entries %d last_index %lld srch start %p srch end %p",
1753 srch_inf
->entries_in_buffer
, srch_inf
->index_of_last_entry
,
1754 srch_inf
->srch_entries_start
, srch_inf
->last_entry
);
1755 if (resp_buftype
== CIFS_LARGE_BUFFER
)
1756 srch_inf
->smallBuf
= false;
1757 else if (resp_buftype
== CIFS_SMALL_BUFFER
)
1758 srch_inf
->smallBuf
= true;
1760 cERROR(1, "illegal search buffer type");
1762 if (rsp
->hdr
.Status
== STATUS_NO_MORE_FILES
)
1763 srch_inf
->endOfSearch
= 1;
1765 srch_inf
->endOfSearch
= 0;
1770 free_rsp_buf(resp_buftype
, rsp
);
1775 send_set_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
1776 u64 persistent_fid
, u64 volatile_fid
, u32 pid
, int info_class
,
1777 unsigned int num
, void **data
, unsigned int *size
)
1779 struct smb2_set_info_req
*req
;
1780 struct smb2_set_info_rsp
*rsp
= NULL
;
1785 struct TCP_Server_Info
*server
;
1786 struct cifs_ses
*ses
= tcon
->ses
;
1788 if (ses
&& (ses
->server
))
1789 server
= ses
->server
;
1796 iov
= kmalloc(sizeof(struct kvec
) * num
, GFP_KERNEL
);
1800 rc
= small_smb2_init(SMB2_SET_INFO
, tcon
, (void **) &req
);
1806 req
->hdr
.ProcessId
= cpu_to_le32(pid
);
1808 req
->InfoType
= SMB2_O_INFO_FILE
;
1809 req
->FileInfoClass
= info_class
;
1810 req
->PersistentFileId
= persistent_fid
;
1811 req
->VolatileFileId
= volatile_fid
;
1813 /* 4 for RFC1001 length and 1 for Buffer */
1815 cpu_to_le16(sizeof(struct smb2_set_info_req
) - 1 - 4);
1816 req
->BufferLength
= cpu_to_le32(*size
);
1818 inc_rfc1001_len(req
, *size
- 1 /* Buffer */);
1820 memcpy(req
->Buffer
, *data
, *size
);
1822 iov
[0].iov_base
= (char *)req
;
1823 /* 4 for RFC1001 length */
1824 iov
[0].iov_len
= get_rfc1002_length(req
) + 4;
1826 for (i
= 1; i
< num
; i
++) {
1827 inc_rfc1001_len(req
, size
[i
]);
1828 le32_add_cpu(&req
->BufferLength
, size
[i
]);
1829 iov
[i
].iov_base
= (char *)data
[i
];
1830 iov
[i
].iov_len
= size
[i
];
1833 rc
= SendReceive2(xid
, ses
, iov
, num
, &resp_buftype
, 0);
1834 rsp
= (struct smb2_set_info_rsp
*)iov
[0].iov_base
;
1837 cifs_stats_fail_inc(tcon
, SMB2_SET_INFO_HE
);
1847 free_rsp_buf(resp_buftype
, rsp
);
1853 SMB2_rename(const unsigned int xid
, struct cifs_tcon
*tcon
,
1854 u64 persistent_fid
, u64 volatile_fid
, __le16
*target_file
)
1856 struct smb2_file_rename_info info
;
1858 unsigned int size
[2];
1860 int len
= (2 * UniStrnlen((wchar_t *)target_file
, PATH_MAX
));
1862 data
= kmalloc(sizeof(void *) * 2, GFP_KERNEL
);
1866 info
.ReplaceIfExists
= 1; /* 1 = replace existing target with new */
1867 /* 0 = fail if target already exists */
1868 info
.RootDirectory
= 0; /* MBZ for network ops (why does spec say?) */
1869 info
.FileNameLength
= cpu_to_le32(len
);
1872 size
[0] = sizeof(struct smb2_file_rename_info
);
1874 data
[1] = target_file
;
1875 size
[1] = len
+ 2 /* null */;
1877 rc
= send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
1878 current
->tgid
, FILE_RENAME_INFORMATION
, 2, data
,
1885 SMB2_set_hardlink(const unsigned int xid
, struct cifs_tcon
*tcon
,
1886 u64 persistent_fid
, u64 volatile_fid
, __le16
*target_file
)
1888 struct smb2_file_link_info info
;
1890 unsigned int size
[2];
1892 int len
= (2 * UniStrnlen((wchar_t *)target_file
, PATH_MAX
));
1894 data
= kmalloc(sizeof(void *) * 2, GFP_KERNEL
);
1898 info
.ReplaceIfExists
= 0; /* 1 = replace existing link with new */
1899 /* 0 = fail if link already exists */
1900 info
.RootDirectory
= 0; /* MBZ for network ops (why does spec say?) */
1901 info
.FileNameLength
= cpu_to_le32(len
);
1904 size
[0] = sizeof(struct smb2_file_link_info
);
1906 data
[1] = target_file
;
1907 size
[1] = len
+ 2 /* null */;
1909 rc
= send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
1910 current
->tgid
, FILE_LINK_INFORMATION
, 2, data
, size
);
1916 SMB2_set_eof(const unsigned int xid
, struct cifs_tcon
*tcon
, u64 persistent_fid
,
1917 u64 volatile_fid
, u32 pid
, __le64
*eof
)
1919 struct smb2_file_eof_info info
;
1923 info
.EndOfFile
= *eof
;
1926 size
= sizeof(struct smb2_file_eof_info
);
1928 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
, pid
,
1929 FILE_END_OF_FILE_INFORMATION
, 1, &data
, &size
);
1933 SMB2_set_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
1934 u64 persistent_fid
, u64 volatile_fid
, FILE_BASIC_INFO
*buf
)
1937 size
= sizeof(FILE_BASIC_INFO
);
1938 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
1939 current
->tgid
, FILE_BASIC_INFORMATION
, 1,
1940 (void **)&buf
, &size
);