4 * Copyright (C) International Business Machines Corp., 2002,2010
5 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Contains the routines for constructing the SMB PDUs themselves
9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /* SMB/CIFS PDU handling routines here - except for leftovers in connect.c */
25 /* These are mostly routines that operate on a pathname, or on a tree id */
26 /* (mounted volume), but there are eight handle based routines which must be */
27 /* treated slightly differently for reconnection purposes since we never */
28 /* want to reuse a stale file handle and only the caller knows the file info */
31 #include <linux/kernel.h>
32 #include <linux/vfs.h>
33 #include <linux/slab.h>
34 #include <linux/posix_acl_xattr.h>
35 #include <linux/pagemap.h>
36 #include <linux/swap.h>
37 #include <linux/task_io_accounting_ops.h>
38 #include <asm/uaccess.h>
42 #include "cifsproto.h"
43 #include "cifs_unicode.h"
44 #include "cifs_debug.h"
47 #ifdef CONFIG_CIFS_POSIX
52 #ifdef CONFIG_CIFS_WEAK_PW_HASH
53 {LANMAN_PROT
, "\2LM1.2X002"},
54 {LANMAN2_PROT
, "\2LANMAN2.1"},
55 #endif /* weak password hashing for legacy clients */
56 {CIFS_PROT
, "\2NT LM 0.12"},
57 {POSIX_PROT
, "\2POSIX 2"},
65 #ifdef CONFIG_CIFS_WEAK_PW_HASH
66 {LANMAN_PROT
, "\2LM1.2X002"},
67 {LANMAN2_PROT
, "\2LANMAN2.1"},
68 #endif /* weak password hashing for legacy clients */
69 {CIFS_PROT
, "\2NT LM 0.12"},
74 /* define the number of elements in the cifs dialect array */
75 #ifdef CONFIG_CIFS_POSIX
76 #ifdef CONFIG_CIFS_WEAK_PW_HASH
77 #define CIFS_NUM_PROT 4
79 #define CIFS_NUM_PROT 2
80 #endif /* CIFS_WEAK_PW_HASH */
82 #ifdef CONFIG_CIFS_WEAK_PW_HASH
83 #define CIFS_NUM_PROT 3
85 #define CIFS_NUM_PROT 1
86 #endif /* CONFIG_CIFS_WEAK_PW_HASH */
87 #endif /* CIFS_POSIX */
90 * Mark as invalid, all open files on tree connections since they
91 * were closed when session to server was lost.
94 cifs_mark_open_files_invalid(struct cifs_tcon
*tcon
)
96 struct cifsFileInfo
*open_file
= NULL
;
97 struct list_head
*tmp
;
98 struct list_head
*tmp1
;
100 /* list all files open on tree connection and mark them invalid */
101 spin_lock(&tcon
->open_file_lock
);
102 list_for_each_safe(tmp
, tmp1
, &tcon
->openFileList
) {
103 open_file
= list_entry(tmp
, struct cifsFileInfo
, tlist
);
104 open_file
->invalidHandle
= true;
105 open_file
->oplock_break_cancelled
= true;
107 spin_unlock(&tcon
->open_file_lock
);
109 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
114 /* reconnect the socket, tcon, and smb session if needed */
116 cifs_reconnect_tcon(struct cifs_tcon
*tcon
, int smb_command
)
119 struct cifs_ses
*ses
;
120 struct TCP_Server_Info
*server
;
121 struct nls_table
*nls_codepage
;
124 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
125 * tcp and smb session status done differently for those three - in the
132 server
= ses
->server
;
135 * only tree disconnect, open, and write, (and ulogoff which does not
136 * have tcon) are allowed as we start force umount
138 if (tcon
->tidStatus
== CifsExiting
) {
139 if (smb_command
!= SMB_COM_WRITE_ANDX
&&
140 smb_command
!= SMB_COM_OPEN_ANDX
&&
141 smb_command
!= SMB_COM_TREE_DISCONNECT
) {
142 cifs_dbg(FYI
, "can not send cmd %d while umounting\n",
149 * Give demultiplex thread up to 10 seconds to reconnect, should be
150 * greater than cifs socket timeout which is 7 seconds
152 while (server
->tcpStatus
== CifsNeedReconnect
) {
153 rc
= wait_event_interruptible_timeout(server
->response_q
,
154 (server
->tcpStatus
!= CifsNeedReconnect
),
157 cifs_dbg(FYI
, "%s: aborting reconnect due to a received"
158 " signal by the process\n", __func__
);
162 /* are we still trying to reconnect? */
163 if (server
->tcpStatus
!= CifsNeedReconnect
)
167 * on "soft" mounts we wait once. Hard mounts keep
168 * retrying until process is killed or server comes
172 cifs_dbg(FYI
, "gave up waiting on reconnect in smb_init\n");
177 if (!ses
->need_reconnect
&& !tcon
->need_reconnect
)
180 nls_codepage
= load_nls_default();
183 * need to prevent multiple threads trying to simultaneously
184 * reconnect the same SMB session
186 mutex_lock(&ses
->session_mutex
);
187 rc
= cifs_negotiate_protocol(0, ses
);
188 if (rc
== 0 && ses
->need_reconnect
)
189 rc
= cifs_setup_session(0, ses
, nls_codepage
);
191 /* do we need to reconnect tcon? */
192 if (rc
|| !tcon
->need_reconnect
) {
193 mutex_unlock(&ses
->session_mutex
);
197 cifs_mark_open_files_invalid(tcon
);
198 rc
= CIFSTCon(0, ses
, tcon
->treeName
, tcon
, nls_codepage
);
199 mutex_unlock(&ses
->session_mutex
);
200 cifs_dbg(FYI
, "reconnect tcon rc = %d\n", rc
);
205 atomic_inc(&tconInfoReconnectCount
);
207 /* tell server Unix caps we support */
208 if (ses
->capabilities
& CAP_UNIX
)
209 reset_cifs_unix_caps(0, tcon
, NULL
, NULL
);
212 * Removed call to reopen open files here. It is safer (and faster) to
213 * reopen files one at a time as needed in read and write.
215 * FIXME: what about file locks? don't we need to reclaim them ASAP?
220 * Check if handle based operation so we know whether we can continue
221 * or not without returning to caller to reset file handle
223 switch (smb_command
) {
224 case SMB_COM_READ_ANDX
:
225 case SMB_COM_WRITE_ANDX
:
227 case SMB_COM_FIND_CLOSE2
:
228 case SMB_COM_LOCKING_ANDX
:
232 unload_nls(nls_codepage
);
236 /* Allocate and return pointer to an SMB request buffer, and set basic
237 SMB information in the SMB header. If the return code is zero, this
238 function must have filled in request_buf pointer */
240 small_smb_init(int smb_command
, int wct
, struct cifs_tcon
*tcon
,
245 rc
= cifs_reconnect_tcon(tcon
, smb_command
);
249 *request_buf
= cifs_small_buf_get();
250 if (*request_buf
== NULL
) {
251 /* BB should we add a retry in here if not a writepage? */
255 header_assemble((struct smb_hdr
*) *request_buf
, smb_command
,
259 cifs_stats_inc(&tcon
->num_smbs_sent
);
265 small_smb_init_no_tc(const int smb_command
, const int wct
,
266 struct cifs_ses
*ses
, void **request_buf
)
269 struct smb_hdr
*buffer
;
271 rc
= small_smb_init(smb_command
, wct
, NULL
, request_buf
);
275 buffer
= (struct smb_hdr
*)*request_buf
;
276 buffer
->Mid
= get_next_mid(ses
->server
);
277 if (ses
->capabilities
& CAP_UNICODE
)
278 buffer
->Flags2
|= SMBFLG2_UNICODE
;
279 if (ses
->capabilities
& CAP_STATUS32
)
280 buffer
->Flags2
|= SMBFLG2_ERR_STATUS
;
282 /* uid, tid can stay at zero as set in header assemble */
284 /* BB add support for turning on the signing when
285 this function is used after 1st of session setup requests */
290 /* If the return code is zero, this function must fill in request_buf pointer */
292 __smb_init(int smb_command
, int wct
, struct cifs_tcon
*tcon
,
293 void **request_buf
, void **response_buf
)
295 *request_buf
= cifs_buf_get();
296 if (*request_buf
== NULL
) {
297 /* BB should we add a retry in here if not a writepage? */
300 /* Although the original thought was we needed the response buf for */
301 /* potential retries of smb operations it turns out we can determine */
302 /* from the mid flags when the request buffer can be resent without */
303 /* having to use a second distinct buffer for the response */
305 *response_buf
= *request_buf
;
307 header_assemble((struct smb_hdr
*) *request_buf
, smb_command
, tcon
,
311 cifs_stats_inc(&tcon
->num_smbs_sent
);
316 /* If the return code is zero, this function must fill in request_buf pointer */
318 smb_init(int smb_command
, int wct
, struct cifs_tcon
*tcon
,
319 void **request_buf
, void **response_buf
)
323 rc
= cifs_reconnect_tcon(tcon
, smb_command
);
327 return __smb_init(smb_command
, wct
, tcon
, request_buf
, response_buf
);
331 smb_init_no_reconnect(int smb_command
, int wct
, struct cifs_tcon
*tcon
,
332 void **request_buf
, void **response_buf
)
334 if (tcon
->ses
->need_reconnect
|| tcon
->need_reconnect
)
337 return __smb_init(smb_command
, wct
, tcon
, request_buf
, response_buf
);
340 static int validate_t2(struct smb_t2_rsp
*pSMB
)
342 unsigned int total_size
;
344 /* check for plausible wct */
345 if (pSMB
->hdr
.WordCount
< 10)
348 /* check for parm and data offset going beyond end of smb */
349 if (get_unaligned_le16(&pSMB
->t2_rsp
.ParameterOffset
) > 1024 ||
350 get_unaligned_le16(&pSMB
->t2_rsp
.DataOffset
) > 1024)
353 total_size
= get_unaligned_le16(&pSMB
->t2_rsp
.ParameterCount
);
354 if (total_size
>= 512)
357 /* check that bcc is at least as big as parms + data, and that it is
358 * less than negotiated smb buffer
360 total_size
+= get_unaligned_le16(&pSMB
->t2_rsp
.DataCount
);
361 if (total_size
> get_bcc(&pSMB
->hdr
) ||
362 total_size
>= CIFSMaxBufSize
+ MAX_CIFS_HDR_SIZE
)
367 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB
,
368 sizeof(struct smb_t2_rsp
) + 16);
373 decode_ext_sec_blob(struct cifs_ses
*ses
, NEGOTIATE_RSP
*pSMBr
)
377 char *guid
= pSMBr
->u
.extended_response
.GUID
;
378 struct TCP_Server_Info
*server
= ses
->server
;
380 count
= get_bcc(&pSMBr
->hdr
);
381 if (count
< SMB1_CLIENT_GUID_SIZE
)
384 spin_lock(&cifs_tcp_ses_lock
);
385 if (server
->srv_count
> 1) {
386 spin_unlock(&cifs_tcp_ses_lock
);
387 if (memcmp(server
->server_GUID
, guid
, SMB1_CLIENT_GUID_SIZE
) != 0) {
388 cifs_dbg(FYI
, "server UID changed\n");
389 memcpy(server
->server_GUID
, guid
, SMB1_CLIENT_GUID_SIZE
);
392 spin_unlock(&cifs_tcp_ses_lock
);
393 memcpy(server
->server_GUID
, guid
, SMB1_CLIENT_GUID_SIZE
);
396 if (count
== SMB1_CLIENT_GUID_SIZE
) {
397 server
->sec_ntlmssp
= true;
399 count
-= SMB1_CLIENT_GUID_SIZE
;
400 rc
= decode_negTokenInit(
401 pSMBr
->u
.extended_response
.SecurityBlob
, count
, server
);
410 cifs_enable_signing(struct TCP_Server_Info
*server
, bool mnt_sign_required
)
412 bool srv_sign_required
= server
->sec_mode
& server
->vals
->signing_required
;
413 bool srv_sign_enabled
= server
->sec_mode
& server
->vals
->signing_enabled
;
414 bool mnt_sign_enabled
= global_secflags
& CIFSSEC_MAY_SIGN
;
417 * Is signing required by mnt options? If not then check
418 * global_secflags to see if it is there.
420 if (!mnt_sign_required
)
421 mnt_sign_required
= ((global_secflags
& CIFSSEC_MUST_SIGN
) ==
425 * If signing is required then it's automatically enabled too,
426 * otherwise, check to see if the secflags allow it.
428 mnt_sign_enabled
= mnt_sign_required
? mnt_sign_required
:
429 (global_secflags
& CIFSSEC_MAY_SIGN
);
431 /* If server requires signing, does client allow it? */
432 if (srv_sign_required
) {
433 if (!mnt_sign_enabled
) {
434 cifs_dbg(VFS
, "Server requires signing, but it's disabled in SecurityFlags!");
440 /* If client requires signing, does server allow it? */
441 if (mnt_sign_required
) {
442 if (!srv_sign_enabled
) {
443 cifs_dbg(VFS
, "Server does not support signing!");
452 #ifdef CONFIG_CIFS_WEAK_PW_HASH
454 decode_lanman_negprot_rsp(struct TCP_Server_Info
*server
, NEGOTIATE_RSP
*pSMBr
)
457 struct lanman_neg_rsp
*rsp
= (struct lanman_neg_rsp
*)pSMBr
;
459 if (server
->dialect
!= LANMAN_PROT
&& server
->dialect
!= LANMAN2_PROT
)
462 server
->sec_mode
= le16_to_cpu(rsp
->SecurityMode
);
463 server
->maxReq
= min_t(unsigned int,
464 le16_to_cpu(rsp
->MaxMpxCount
),
466 set_credits(server
, server
->maxReq
);
467 server
->maxBuf
= le16_to_cpu(rsp
->MaxBufSize
);
468 /* even though we do not use raw we might as well set this
469 accurately, in case we ever find a need for it */
470 if ((le16_to_cpu(rsp
->RawMode
) & RAW_ENABLE
) == RAW_ENABLE
) {
471 server
->max_rw
= 0xFF00;
472 server
->capabilities
= CAP_MPX_MODE
| CAP_RAW_MODE
;
474 server
->max_rw
= 0;/* do not need to use raw anyway */
475 server
->capabilities
= CAP_MPX_MODE
;
477 tmp
= (__s16
)le16_to_cpu(rsp
->ServerTimeZone
);
479 /* OS/2 often does not set timezone therefore
480 * we must use server time to calc time zone.
481 * Could deviate slightly from the right zone.
482 * Smallest defined timezone difference is 15 minutes
483 * (i.e. Nepal). Rounding up/down is done to match
486 int val
, seconds
, remain
, result
;
487 struct timespec ts
, utc
;
489 ts
= cnvrtDosUnixTm(rsp
->SrvTime
.Date
,
490 rsp
->SrvTime
.Time
, 0);
491 cifs_dbg(FYI
, "SrvTime %d sec since 1970 (utc: %d) diff: %d\n",
492 (int)ts
.tv_sec
, (int)utc
.tv_sec
,
493 (int)(utc
.tv_sec
- ts
.tv_sec
));
494 val
= (int)(utc
.tv_sec
- ts
.tv_sec
);
496 result
= (seconds
/ MIN_TZ_ADJ
) * MIN_TZ_ADJ
;
497 remain
= seconds
% MIN_TZ_ADJ
;
498 if (remain
>= (MIN_TZ_ADJ
/ 2))
499 result
+= MIN_TZ_ADJ
;
502 server
->timeAdj
= result
;
504 server
->timeAdj
= (int)tmp
;
505 server
->timeAdj
*= 60; /* also in seconds */
507 cifs_dbg(FYI
, "server->timeAdj: %d seconds\n", server
->timeAdj
);
510 /* BB get server time for time conversions and add
511 code to use it and timezone since this is not UTC */
513 if (rsp
->EncryptionKeyLength
==
514 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE
)) {
515 memcpy(server
->cryptkey
, rsp
->EncryptionKey
,
516 CIFS_CRYPTO_KEY_SIZE
);
517 } else if (server
->sec_mode
& SECMODE_PW_ENCRYPT
) {
518 return -EIO
; /* need cryptkey unless plain text */
521 cifs_dbg(FYI
, "LANMAN negotiated\n");
526 decode_lanman_negprot_rsp(struct TCP_Server_Info
*server
, NEGOTIATE_RSP
*pSMBr
)
528 cifs_dbg(VFS
, "mount failed, cifs module not built with CIFS_WEAK_PW_HASH support\n");
534 should_set_ext_sec_flag(enum securityEnum sectype
)
541 if (global_secflags
&
542 (CIFSSEC_MAY_KRB5
| CIFSSEC_MAY_NTLMSSP
))
551 CIFSSMBNegotiate(const unsigned int xid
, struct cifs_ses
*ses
)
554 NEGOTIATE_RSP
*pSMBr
;
558 struct TCP_Server_Info
*server
= ses
->server
;
562 WARN(1, "%s: server is NULL!\n", __func__
);
566 rc
= smb_init(SMB_COM_NEGOTIATE
, 0, NULL
/* no tcon yet */ ,
567 (void **) &pSMB
, (void **) &pSMBr
);
571 pSMB
->hdr
.Mid
= get_next_mid(server
);
572 pSMB
->hdr
.Flags2
|= (SMBFLG2_UNICODE
| SMBFLG2_ERR_STATUS
);
574 if (should_set_ext_sec_flag(ses
->sectype
)) {
575 cifs_dbg(FYI
, "Requesting extended security.");
576 pSMB
->hdr
.Flags2
|= SMBFLG2_EXT_SEC
;
581 * We know that all the name entries in the protocols array
582 * are short (< 16 bytes anyway) and are NUL terminated.
584 for (i
= 0; i
< CIFS_NUM_PROT
; i
++) {
585 size_t len
= strlen(protocols
[i
].name
) + 1;
587 memcpy(pSMB
->DialectsArray
+count
, protocols
[i
].name
, len
);
590 inc_rfc1001_len(pSMB
, count
);
591 pSMB
->ByteCount
= cpu_to_le16(count
);
593 rc
= SendReceive(xid
, ses
, (struct smb_hdr
*) pSMB
,
594 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
598 server
->dialect
= le16_to_cpu(pSMBr
->DialectIndex
);
599 cifs_dbg(FYI
, "Dialect: %d\n", server
->dialect
);
600 /* Check wct = 1 error case */
601 if ((pSMBr
->hdr
.WordCount
< 13) || (server
->dialect
== BAD_PROT
)) {
602 /* core returns wct = 1, but we do not ask for core - otherwise
603 small wct just comes when dialect index is -1 indicating we
604 could not negotiate a common dialect */
607 } else if (pSMBr
->hdr
.WordCount
== 13) {
608 server
->negflavor
= CIFS_NEGFLAVOR_LANMAN
;
609 rc
= decode_lanman_negprot_rsp(server
, pSMBr
);
611 } else if (pSMBr
->hdr
.WordCount
!= 17) {
616 /* else wct == 17, NTLM or better */
618 server
->sec_mode
= pSMBr
->SecurityMode
;
619 if ((server
->sec_mode
& SECMODE_USER
) == 0)
620 cifs_dbg(FYI
, "share mode security\n");
622 /* one byte, so no need to convert this or EncryptionKeyLen from
624 server
->maxReq
= min_t(unsigned int, le16_to_cpu(pSMBr
->MaxMpxCount
),
626 set_credits(server
, server
->maxReq
);
627 /* probably no need to store and check maxvcs */
628 server
->maxBuf
= le32_to_cpu(pSMBr
->MaxBufferSize
);
629 server
->max_rw
= le32_to_cpu(pSMBr
->MaxRawSize
);
630 cifs_dbg(NOISY
, "Max buf = %d\n", ses
->server
->maxBuf
);
631 server
->capabilities
= le32_to_cpu(pSMBr
->Capabilities
);
632 server
->timeAdj
= (int)(__s16
)le16_to_cpu(pSMBr
->ServerTimeZone
);
633 server
->timeAdj
*= 60;
635 if (pSMBr
->EncryptionKeyLength
== CIFS_CRYPTO_KEY_SIZE
) {
636 server
->negflavor
= CIFS_NEGFLAVOR_UNENCAP
;
637 memcpy(ses
->server
->cryptkey
, pSMBr
->u
.EncryptionKey
,
638 CIFS_CRYPTO_KEY_SIZE
);
639 } else if (pSMBr
->hdr
.Flags2
& SMBFLG2_EXT_SEC
||
640 server
->capabilities
& CAP_EXTENDED_SECURITY
) {
641 server
->negflavor
= CIFS_NEGFLAVOR_EXTENDED
;
642 rc
= decode_ext_sec_blob(ses
, pSMBr
);
643 } else if (server
->sec_mode
& SECMODE_PW_ENCRYPT
) {
644 rc
= -EIO
; /* no crypt key only if plain text pwd */
646 server
->negflavor
= CIFS_NEGFLAVOR_UNENCAP
;
647 server
->capabilities
&= ~CAP_EXTENDED_SECURITY
;
652 rc
= cifs_enable_signing(server
, ses
->sign
);
654 cifs_buf_release(pSMB
);
656 cifs_dbg(FYI
, "negprot rc %d\n", rc
);
661 CIFSSMBTDis(const unsigned int xid
, struct cifs_tcon
*tcon
)
663 struct smb_hdr
*smb_buffer
;
666 cifs_dbg(FYI
, "In tree disconnect\n");
668 /* BB: do we need to check this? These should never be NULL. */
669 if ((tcon
->ses
== NULL
) || (tcon
->ses
->server
== NULL
))
673 * No need to return error on this operation if tid invalidated and
674 * closed on server already e.g. due to tcp session crashing. Also,
675 * the tcon is no longer on the list, so no need to take lock before
678 if ((tcon
->need_reconnect
) || (tcon
->ses
->need_reconnect
))
681 rc
= small_smb_init(SMB_COM_TREE_DISCONNECT
, 0, tcon
,
682 (void **)&smb_buffer
);
686 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *)smb_buffer
, 0);
688 cifs_dbg(FYI
, "Tree disconnect failed %d\n", rc
);
690 /* No need to return error on this operation if tid invalidated and
691 closed on server already e.g. due to tcp session crashing */
699 * This is a no-op for now. We're not really interested in the reply, but
700 * rather in the fact that the server sent one and that server->lstrp
703 * FIXME: maybe we should consider checking that the reply matches request?
706 cifs_echo_callback(struct mid_q_entry
*mid
)
708 struct TCP_Server_Info
*server
= mid
->callback_data
;
710 mutex_lock(&server
->srv_mutex
);
711 DeleteMidQEntry(mid
);
712 mutex_unlock(&server
->srv_mutex
);
713 add_credits(server
, 1, CIFS_ECHO_OP
);
717 CIFSSMBEcho(struct TCP_Server_Info
*server
)
722 struct smb_rqst rqst
= { .rq_iov
= &iov
,
725 cifs_dbg(FYI
, "In echo request\n");
727 rc
= small_smb_init(SMB_COM_ECHO
, 0, NULL
, (void **)&smb
);
731 if (server
->capabilities
& CAP_UNICODE
)
732 smb
->hdr
.Flags2
|= SMBFLG2_UNICODE
;
734 /* set up echo request */
735 smb
->hdr
.Tid
= 0xffff;
736 smb
->hdr
.WordCount
= 1;
737 put_unaligned_le16(1, &smb
->EchoCount
);
738 put_bcc(1, &smb
->hdr
);
740 inc_rfc1001_len(smb
, 3);
742 iov
.iov_len
= be32_to_cpu(smb
->hdr
.smb_buf_length
) + 4;
744 rc
= cifs_call_async(server
, &rqst
, NULL
, cifs_echo_callback
,
745 server
, CIFS_ASYNC_OP
| CIFS_ECHO_OP
);
747 cifs_dbg(FYI
, "Echo request failed: %d\n", rc
);
749 cifs_small_buf_release(smb
);
755 CIFSSMBLogoff(const unsigned int xid
, struct cifs_ses
*ses
)
757 LOGOFF_ANDX_REQ
*pSMB
;
760 cifs_dbg(FYI
, "In SMBLogoff for session disconnect\n");
763 * BB: do we need to check validity of ses and server? They should
764 * always be valid since we have an active reference. If not, that
765 * should probably be a BUG()
767 if (!ses
|| !ses
->server
)
770 mutex_lock(&ses
->session_mutex
);
771 if (ses
->need_reconnect
)
772 goto session_already_dead
; /* no need to send SMBlogoff if uid
773 already closed due to reconnect */
774 rc
= small_smb_init(SMB_COM_LOGOFF_ANDX
, 2, NULL
, (void **)&pSMB
);
776 mutex_unlock(&ses
->session_mutex
);
780 pSMB
->hdr
.Mid
= get_next_mid(ses
->server
);
782 if (ses
->server
->sign
)
783 pSMB
->hdr
.Flags2
|= SMBFLG2_SECURITY_SIGNATURE
;
785 pSMB
->hdr
.Uid
= ses
->Suid
;
787 pSMB
->AndXCommand
= 0xFF;
788 rc
= SendReceiveNoRsp(xid
, ses
, (char *) pSMB
, 0);
789 session_already_dead
:
790 mutex_unlock(&ses
->session_mutex
);
792 /* if session dead then we do not need to do ulogoff,
793 since server closed smb session, no sense reporting
801 CIFSPOSIXDelFile(const unsigned int xid
, struct cifs_tcon
*tcon
,
802 const char *fileName
, __u16 type
,
803 const struct nls_table
*nls_codepage
, int remap
)
805 TRANSACTION2_SPI_REQ
*pSMB
= NULL
;
806 TRANSACTION2_SPI_RSP
*pSMBr
= NULL
;
807 struct unlink_psx_rq
*pRqD
;
810 int bytes_returned
= 0;
811 __u16 params
, param_offset
, offset
, byte_count
;
813 cifs_dbg(FYI
, "In POSIX delete\n");
815 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
820 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
822 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, fileName
,
823 PATH_MAX
, nls_codepage
, remap
);
824 name_len
++; /* trailing null */
826 } else { /* BB add path length overrun check */
827 name_len
= strnlen(fileName
, PATH_MAX
);
828 name_len
++; /* trailing null */
829 strncpy(pSMB
->FileName
, fileName
, name_len
);
832 params
= 6 + name_len
;
833 pSMB
->MaxParameterCount
= cpu_to_le16(2);
834 pSMB
->MaxDataCount
= 0; /* BB double check this with jra */
835 pSMB
->MaxSetupCount
= 0;
840 param_offset
= offsetof(struct smb_com_transaction2_spi_req
,
841 InformationLevel
) - 4;
842 offset
= param_offset
+ params
;
844 /* Setup pointer to Request Data (inode type) */
845 pRqD
= (struct unlink_psx_rq
*)(((char *)&pSMB
->hdr
.Protocol
) + offset
);
846 pRqD
->type
= cpu_to_le16(type
);
847 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
848 pSMB
->DataOffset
= cpu_to_le16(offset
);
849 pSMB
->SetupCount
= 1;
851 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_PATH_INFORMATION
);
852 byte_count
= 3 /* pad */ + params
+ sizeof(struct unlink_psx_rq
);
854 pSMB
->DataCount
= cpu_to_le16(sizeof(struct unlink_psx_rq
));
855 pSMB
->TotalDataCount
= cpu_to_le16(sizeof(struct unlink_psx_rq
));
856 pSMB
->ParameterCount
= cpu_to_le16(params
);
857 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
858 pSMB
->InformationLevel
= cpu_to_le16(SMB_POSIX_UNLINK
);
860 inc_rfc1001_len(pSMB
, byte_count
);
861 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
862 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
863 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
865 cifs_dbg(FYI
, "Posix delete returned %d\n", rc
);
866 cifs_buf_release(pSMB
);
868 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_deletes
);
877 CIFSSMBDelFile(const unsigned int xid
, struct cifs_tcon
*tcon
, const char *name
,
878 struct cifs_sb_info
*cifs_sb
)
880 DELETE_FILE_REQ
*pSMB
= NULL
;
881 DELETE_FILE_RSP
*pSMBr
= NULL
;
885 int remap
= cifs_remap(cifs_sb
);
888 rc
= smb_init(SMB_COM_DELETE
, 1, tcon
, (void **) &pSMB
,
893 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
894 name_len
= cifsConvertToUTF16((__le16
*) pSMB
->fileName
, name
,
895 PATH_MAX
, cifs_sb
->local_nls
,
897 name_len
++; /* trailing null */
899 } else { /* BB improve check for buffer overruns BB */
900 name_len
= strnlen(name
, PATH_MAX
);
901 name_len
++; /* trailing null */
902 strncpy(pSMB
->fileName
, name
, name_len
);
904 pSMB
->SearchAttributes
=
905 cpu_to_le16(ATTR_READONLY
| ATTR_HIDDEN
| ATTR_SYSTEM
);
906 pSMB
->BufferFormat
= 0x04;
907 inc_rfc1001_len(pSMB
, name_len
+ 1);
908 pSMB
->ByteCount
= cpu_to_le16(name_len
+ 1);
909 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
910 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
911 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_deletes
);
913 cifs_dbg(FYI
, "Error in RMFile = %d\n", rc
);
915 cifs_buf_release(pSMB
);
923 CIFSSMBRmDir(const unsigned int xid
, struct cifs_tcon
*tcon
, const char *name
,
924 struct cifs_sb_info
*cifs_sb
)
926 DELETE_DIRECTORY_REQ
*pSMB
= NULL
;
927 DELETE_DIRECTORY_RSP
*pSMBr
= NULL
;
931 int remap
= cifs_remap(cifs_sb
);
933 cifs_dbg(FYI
, "In CIFSSMBRmDir\n");
935 rc
= smb_init(SMB_COM_DELETE_DIRECTORY
, 0, tcon
, (void **) &pSMB
,
940 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
941 name_len
= cifsConvertToUTF16((__le16
*) pSMB
->DirName
, name
,
942 PATH_MAX
, cifs_sb
->local_nls
,
944 name_len
++; /* trailing null */
946 } else { /* BB improve check for buffer overruns BB */
947 name_len
= strnlen(name
, PATH_MAX
);
948 name_len
++; /* trailing null */
949 strncpy(pSMB
->DirName
, name
, name_len
);
952 pSMB
->BufferFormat
= 0x04;
953 inc_rfc1001_len(pSMB
, name_len
+ 1);
954 pSMB
->ByteCount
= cpu_to_le16(name_len
+ 1);
955 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
956 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
957 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_rmdirs
);
959 cifs_dbg(FYI
, "Error in RMDir = %d\n", rc
);
961 cifs_buf_release(pSMB
);
968 CIFSSMBMkDir(const unsigned int xid
, struct cifs_tcon
*tcon
, const char *name
,
969 struct cifs_sb_info
*cifs_sb
)
972 CREATE_DIRECTORY_REQ
*pSMB
= NULL
;
973 CREATE_DIRECTORY_RSP
*pSMBr
= NULL
;
976 int remap
= cifs_remap(cifs_sb
);
978 cifs_dbg(FYI
, "In CIFSSMBMkDir\n");
980 rc
= smb_init(SMB_COM_CREATE_DIRECTORY
, 0, tcon
, (void **) &pSMB
,
985 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
986 name_len
= cifsConvertToUTF16((__le16
*) pSMB
->DirName
, name
,
987 PATH_MAX
, cifs_sb
->local_nls
,
989 name_len
++; /* trailing null */
991 } else { /* BB improve check for buffer overruns BB */
992 name_len
= strnlen(name
, PATH_MAX
);
993 name_len
++; /* trailing null */
994 strncpy(pSMB
->DirName
, name
, name_len
);
997 pSMB
->BufferFormat
= 0x04;
998 inc_rfc1001_len(pSMB
, name_len
+ 1);
999 pSMB
->ByteCount
= cpu_to_le16(name_len
+ 1);
1000 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
1001 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
1002 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_mkdirs
);
1004 cifs_dbg(FYI
, "Error in Mkdir = %d\n", rc
);
1006 cifs_buf_release(pSMB
);
1013 CIFSPOSIXCreate(const unsigned int xid
, struct cifs_tcon
*tcon
,
1014 __u32 posix_flags
, __u64 mode
, __u16
*netfid
,
1015 FILE_UNIX_BASIC_INFO
*pRetData
, __u32
*pOplock
,
1016 const char *name
, const struct nls_table
*nls_codepage
,
1019 TRANSACTION2_SPI_REQ
*pSMB
= NULL
;
1020 TRANSACTION2_SPI_RSP
*pSMBr
= NULL
;
1023 int bytes_returned
= 0;
1024 __u16 params
, param_offset
, offset
, byte_count
, count
;
1025 OPEN_PSX_REQ
*pdata
;
1026 OPEN_PSX_RSP
*psx_rsp
;
1028 cifs_dbg(FYI
, "In POSIX Create\n");
1030 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
1035 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
1037 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, name
,
1038 PATH_MAX
, nls_codepage
, remap
);
1039 name_len
++; /* trailing null */
1041 } else { /* BB improve the check for buffer overruns BB */
1042 name_len
= strnlen(name
, PATH_MAX
);
1043 name_len
++; /* trailing null */
1044 strncpy(pSMB
->FileName
, name
, name_len
);
1047 params
= 6 + name_len
;
1048 count
= sizeof(OPEN_PSX_REQ
);
1049 pSMB
->MaxParameterCount
= cpu_to_le16(2);
1050 pSMB
->MaxDataCount
= cpu_to_le16(1000); /* large enough */
1051 pSMB
->MaxSetupCount
= 0;
1055 pSMB
->Reserved2
= 0;
1056 param_offset
= offsetof(struct smb_com_transaction2_spi_req
,
1057 InformationLevel
) - 4;
1058 offset
= param_offset
+ params
;
1059 pdata
= (OPEN_PSX_REQ
*)(((char *)&pSMB
->hdr
.Protocol
) + offset
);
1060 pdata
->Level
= cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC
);
1061 pdata
->Permissions
= cpu_to_le64(mode
);
1062 pdata
->PosixOpenFlags
= cpu_to_le32(posix_flags
);
1063 pdata
->OpenFlags
= cpu_to_le32(*pOplock
);
1064 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
1065 pSMB
->DataOffset
= cpu_to_le16(offset
);
1066 pSMB
->SetupCount
= 1;
1067 pSMB
->Reserved3
= 0;
1068 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_PATH_INFORMATION
);
1069 byte_count
= 3 /* pad */ + params
+ count
;
1071 pSMB
->DataCount
= cpu_to_le16(count
);
1072 pSMB
->ParameterCount
= cpu_to_le16(params
);
1073 pSMB
->TotalDataCount
= pSMB
->DataCount
;
1074 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
1075 pSMB
->InformationLevel
= cpu_to_le16(SMB_POSIX_OPEN
);
1076 pSMB
->Reserved4
= 0;
1077 inc_rfc1001_len(pSMB
, byte_count
);
1078 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
1079 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
1080 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
1082 cifs_dbg(FYI
, "Posix create returned %d\n", rc
);
1083 goto psx_create_err
;
1086 cifs_dbg(FYI
, "copying inode info\n");
1087 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
1089 if (rc
|| get_bcc(&pSMBr
->hdr
) < sizeof(OPEN_PSX_RSP
)) {
1090 rc
= -EIO
; /* bad smb */
1091 goto psx_create_err
;
1094 /* copy return information to pRetData */
1095 psx_rsp
= (OPEN_PSX_RSP
*)((char *) &pSMBr
->hdr
.Protocol
1096 + le16_to_cpu(pSMBr
->t2
.DataOffset
));
1098 *pOplock
= le16_to_cpu(psx_rsp
->OplockFlags
);
1100 *netfid
= psx_rsp
->Fid
; /* cifs fid stays in le */
1101 /* Let caller know file was created so we can set the mode. */
1102 /* Do we care about the CreateAction in any other cases? */
1103 if (cpu_to_le32(FILE_CREATE
) == psx_rsp
->CreateAction
)
1104 *pOplock
|= CIFS_CREATE_ACTION
;
1105 /* check to make sure response data is there */
1106 if (psx_rsp
->ReturnedLevel
!= cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC
)) {
1107 pRetData
->Type
= cpu_to_le32(-1); /* unknown */
1108 cifs_dbg(NOISY
, "unknown type\n");
1110 if (get_bcc(&pSMBr
->hdr
) < sizeof(OPEN_PSX_RSP
)
1111 + sizeof(FILE_UNIX_BASIC_INFO
)) {
1112 cifs_dbg(VFS
, "Open response data too small\n");
1113 pRetData
->Type
= cpu_to_le32(-1);
1114 goto psx_create_err
;
1116 memcpy((char *) pRetData
,
1117 (char *)psx_rsp
+ sizeof(OPEN_PSX_RSP
),
1118 sizeof(FILE_UNIX_BASIC_INFO
));
1122 cifs_buf_release(pSMB
);
1124 if (posix_flags
& SMB_O_DIRECTORY
)
1125 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_posixmkdirs
);
1127 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_posixopens
);
1135 static __u16
convert_disposition(int disposition
)
1139 switch (disposition
) {
1140 case FILE_SUPERSEDE
:
1141 ofun
= SMBOPEN_OCREATE
| SMBOPEN_OTRUNC
;
1144 ofun
= SMBOPEN_OAPPEND
;
1147 ofun
= SMBOPEN_OCREATE
;
1150 ofun
= SMBOPEN_OCREATE
| SMBOPEN_OAPPEND
;
1152 case FILE_OVERWRITE
:
1153 ofun
= SMBOPEN_OTRUNC
;
1155 case FILE_OVERWRITE_IF
:
1156 ofun
= SMBOPEN_OCREATE
| SMBOPEN_OTRUNC
;
1159 cifs_dbg(FYI
, "unknown disposition %d\n", disposition
);
1160 ofun
= SMBOPEN_OAPPEND
; /* regular open */
1166 access_flags_to_smbopen_mode(const int access_flags
)
1168 int masked_flags
= access_flags
& (GENERIC_READ
| GENERIC_WRITE
);
1170 if (masked_flags
== GENERIC_READ
)
1171 return SMBOPEN_READ
;
1172 else if (masked_flags
== GENERIC_WRITE
)
1173 return SMBOPEN_WRITE
;
1175 /* just go for read/write */
1176 return SMBOPEN_READWRITE
;
1180 SMBLegacyOpen(const unsigned int xid
, struct cifs_tcon
*tcon
,
1181 const char *fileName
, const int openDisposition
,
1182 const int access_flags
, const int create_options
, __u16
*netfid
,
1183 int *pOplock
, FILE_ALL_INFO
*pfile_info
,
1184 const struct nls_table
*nls_codepage
, int remap
)
1187 OPENX_REQ
*pSMB
= NULL
;
1188 OPENX_RSP
*pSMBr
= NULL
;
1194 rc
= smb_init(SMB_COM_OPEN_ANDX
, 15, tcon
, (void **) &pSMB
,
1199 pSMB
->AndXCommand
= 0xFF; /* none */
1201 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
1202 count
= 1; /* account for one byte pad to word boundary */
1204 cifsConvertToUTF16((__le16
*) (pSMB
->fileName
+ 1),
1205 fileName
, PATH_MAX
, nls_codepage
, remap
);
1206 name_len
++; /* trailing null */
1208 } else { /* BB improve check for buffer overruns BB */
1209 count
= 0; /* no pad */
1210 name_len
= strnlen(fileName
, PATH_MAX
);
1211 name_len
++; /* trailing null */
1212 strncpy(pSMB
->fileName
, fileName
, name_len
);
1214 if (*pOplock
& REQ_OPLOCK
)
1215 pSMB
->OpenFlags
= cpu_to_le16(REQ_OPLOCK
);
1216 else if (*pOplock
& REQ_BATCHOPLOCK
)
1217 pSMB
->OpenFlags
= cpu_to_le16(REQ_BATCHOPLOCK
);
1219 pSMB
->OpenFlags
|= cpu_to_le16(REQ_MORE_INFO
);
1220 pSMB
->Mode
= cpu_to_le16(access_flags_to_smbopen_mode(access_flags
));
1221 pSMB
->Mode
|= cpu_to_le16(0x40); /* deny none */
1222 /* set file as system file if special file such
1223 as fifo and server expecting SFU style and
1224 no Unix extensions */
1226 if (create_options
& CREATE_OPTION_SPECIAL
)
1227 pSMB
->FileAttributes
= cpu_to_le16(ATTR_SYSTEM
);
1228 else /* BB FIXME BB */
1229 pSMB
->FileAttributes
= cpu_to_le16(0/*ATTR_NORMAL*/);
1231 if (create_options
& CREATE_OPTION_READONLY
)
1232 pSMB
->FileAttributes
|= cpu_to_le16(ATTR_READONLY
);
1235 /* pSMB->CreateOptions = cpu_to_le32(create_options &
1236 CREATE_OPTIONS_MASK); */
1237 /* BB FIXME END BB */
1239 pSMB
->Sattr
= cpu_to_le16(ATTR_HIDDEN
| ATTR_SYSTEM
| ATTR_DIRECTORY
);
1240 pSMB
->OpenFunction
= cpu_to_le16(convert_disposition(openDisposition
));
1242 inc_rfc1001_len(pSMB
, count
);
1244 pSMB
->ByteCount
= cpu_to_le16(count
);
1245 /* long_op set to 1 to allow for oplock break timeouts */
1246 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
1247 (struct smb_hdr
*)pSMBr
, &bytes_returned
, 0);
1248 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_opens
);
1250 cifs_dbg(FYI
, "Error in Open = %d\n", rc
);
1252 /* BB verify if wct == 15 */
1254 /* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
1256 *netfid
= pSMBr
->Fid
; /* cifs fid stays in le */
1257 /* Let caller know file was created so we can set the mode. */
1258 /* Do we care about the CreateAction in any other cases? */
1260 /* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
1261 *pOplock |= CIFS_CREATE_ACTION; */
1265 pfile_info
->CreationTime
= 0; /* BB convert CreateTime*/
1266 pfile_info
->LastAccessTime
= 0; /* BB fixme */
1267 pfile_info
->LastWriteTime
= 0; /* BB fixme */
1268 pfile_info
->ChangeTime
= 0; /* BB fixme */
1269 pfile_info
->Attributes
=
1270 cpu_to_le32(le16_to_cpu(pSMBr
->FileAttributes
));
1271 /* the file_info buf is endian converted by caller */
1272 pfile_info
->AllocationSize
=
1273 cpu_to_le64(le32_to_cpu(pSMBr
->EndOfFile
));
1274 pfile_info
->EndOfFile
= pfile_info
->AllocationSize
;
1275 pfile_info
->NumberOfLinks
= cpu_to_le32(1);
1276 pfile_info
->DeletePending
= 0;
1280 cifs_buf_release(pSMB
);
1287 CIFS_open(const unsigned int xid
, struct cifs_open_parms
*oparms
, int *oplock
,
1291 OPEN_REQ
*req
= NULL
;
1292 OPEN_RSP
*rsp
= NULL
;
1296 struct cifs_sb_info
*cifs_sb
= oparms
->cifs_sb
;
1297 struct cifs_tcon
*tcon
= oparms
->tcon
;
1298 int remap
= cifs_remap(cifs_sb
);
1299 const struct nls_table
*nls
= cifs_sb
->local_nls
;
1300 int create_options
= oparms
->create_options
;
1301 int desired_access
= oparms
->desired_access
;
1302 int disposition
= oparms
->disposition
;
1303 const char *path
= oparms
->path
;
1306 rc
= smb_init(SMB_COM_NT_CREATE_ANDX
, 24, tcon
, (void **)&req
,
1311 /* no commands go after this */
1312 req
->AndXCommand
= 0xFF;
1314 if (req
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
1315 /* account for one byte pad to word boundary */
1317 name_len
= cifsConvertToUTF16((__le16
*)(req
->fileName
+ 1),
1318 path
, PATH_MAX
, nls
, remap
);
1322 req
->NameLength
= cpu_to_le16(name_len
);
1324 /* BB improve check for buffer overruns BB */
1327 name_len
= strnlen(path
, PATH_MAX
);
1330 req
->NameLength
= cpu_to_le16(name_len
);
1331 strncpy(req
->fileName
, path
, name_len
);
1334 if (*oplock
& REQ_OPLOCK
)
1335 req
->OpenFlags
= cpu_to_le32(REQ_OPLOCK
);
1336 else if (*oplock
& REQ_BATCHOPLOCK
)
1337 req
->OpenFlags
= cpu_to_le32(REQ_BATCHOPLOCK
);
1339 req
->DesiredAccess
= cpu_to_le32(desired_access
);
1340 req
->AllocationSize
= 0;
1343 * Set file as system file if special file such as fifo and server
1344 * expecting SFU style and no Unix extensions.
1346 if (create_options
& CREATE_OPTION_SPECIAL
)
1347 req
->FileAttributes
= cpu_to_le32(ATTR_SYSTEM
);
1349 req
->FileAttributes
= cpu_to_le32(ATTR_NORMAL
);
1352 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1353 * sensitive checks for other servers such as Samba.
1355 if (tcon
->ses
->capabilities
& CAP_UNIX
)
1356 req
->FileAttributes
|= cpu_to_le32(ATTR_POSIX_SEMANTICS
);
1358 if (create_options
& CREATE_OPTION_READONLY
)
1359 req
->FileAttributes
|= cpu_to_le32(ATTR_READONLY
);
1361 req
->ShareAccess
= cpu_to_le32(FILE_SHARE_ALL
);
1362 req
->CreateDisposition
= cpu_to_le32(disposition
);
1363 req
->CreateOptions
= cpu_to_le32(create_options
& CREATE_OPTIONS_MASK
);
1365 /* BB Expirement with various impersonation levels and verify */
1366 req
->ImpersonationLevel
= cpu_to_le32(SECURITY_IMPERSONATION
);
1367 req
->SecurityFlags
= SECURITY_CONTEXT_TRACKING
|SECURITY_EFFECTIVE_ONLY
;
1370 inc_rfc1001_len(req
, count
);
1372 req
->ByteCount
= cpu_to_le16(count
);
1373 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*)req
,
1374 (struct smb_hdr
*)rsp
, &bytes_returned
, 0);
1375 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_opens
);
1377 cifs_dbg(FYI
, "Error in Open = %d\n", rc
);
1378 cifs_buf_release(req
);
1384 /* 1 byte no need to le_to_cpu */
1385 *oplock
= rsp
->OplockLevel
;
1386 /* cifs fid stays in le */
1387 oparms
->fid
->netfid
= rsp
->Fid
;
1389 /* Let caller know file was created so we can set the mode. */
1390 /* Do we care about the CreateAction in any other cases? */
1391 if (cpu_to_le32(FILE_CREATE
) == rsp
->CreateAction
)
1392 *oplock
|= CIFS_CREATE_ACTION
;
1395 /* copy from CreationTime to Attributes */
1396 memcpy((char *)buf
, (char *)&rsp
->CreationTime
, 36);
1397 /* the file_info buf is endian converted by caller */
1398 buf
->AllocationSize
= rsp
->AllocationSize
;
1399 buf
->EndOfFile
= rsp
->EndOfFile
;
1400 buf
->NumberOfLinks
= cpu_to_le32(1);
1401 buf
->DeletePending
= 0;
1404 cifs_buf_release(req
);
1409 * Discard any remaining data in the current SMB. To do this, we borrow the
1413 discard_remaining_data(struct TCP_Server_Info
*server
)
1415 unsigned int rfclen
= get_rfc1002_length(server
->smallbuf
);
1416 int remaining
= rfclen
+ 4 - server
->total_read
;
1418 while (remaining
> 0) {
1421 length
= cifs_read_from_socket(server
, server
->bigbuf
,
1422 min_t(unsigned int, remaining
,
1423 CIFSMaxBufSize
+ MAX_HEADER_SIZE(server
)));
1426 server
->total_read
+= length
;
1427 remaining
-= length
;
1434 cifs_readv_discard(struct TCP_Server_Info
*server
, struct mid_q_entry
*mid
)
1437 struct cifs_readdata
*rdata
= mid
->callback_data
;
1439 length
= discard_remaining_data(server
);
1440 dequeue_mid(mid
, rdata
->result
);
1441 mid
->resp_buf
= server
->smallbuf
;
1442 server
->smallbuf
= NULL
;
1447 cifs_readv_receive(struct TCP_Server_Info
*server
, struct mid_q_entry
*mid
)
1450 unsigned int data_offset
, data_len
;
1451 struct cifs_readdata
*rdata
= mid
->callback_data
;
1452 char *buf
= server
->smallbuf
;
1453 unsigned int buflen
= get_rfc1002_length(buf
) + 4;
1455 cifs_dbg(FYI
, "%s: mid=%llu offset=%llu bytes=%u\n",
1456 __func__
, mid
->mid
, rdata
->offset
, rdata
->bytes
);
1459 * read the rest of READ_RSP header (sans Data array), or whatever we
1460 * can if there's not enough data. At this point, we've read down to
1463 len
= min_t(unsigned int, buflen
, server
->vals
->read_rsp_size
) -
1464 HEADER_SIZE(server
) + 1;
1466 rdata
->iov
.iov_base
= buf
+ HEADER_SIZE(server
) - 1;
1467 rdata
->iov
.iov_len
= len
;
1469 length
= cifs_readv_from_socket(server
, &rdata
->iov
, 1, len
);
1472 server
->total_read
+= length
;
1474 if (server
->ops
->is_session_expired
&&
1475 server
->ops
->is_session_expired(buf
)) {
1476 cifs_reconnect(server
);
1477 wake_up(&server
->response_q
);
1481 if (server
->ops
->is_status_pending
&&
1482 server
->ops
->is_status_pending(buf
, server
, 0)) {
1483 discard_remaining_data(server
);
1487 /* Was the SMB read successful? */
1488 rdata
->result
= server
->ops
->map_error(buf
, false);
1489 if (rdata
->result
!= 0) {
1490 cifs_dbg(FYI
, "%s: server returned error %d\n",
1491 __func__
, rdata
->result
);
1492 return cifs_readv_discard(server
, mid
);
1495 /* Is there enough to get to the rest of the READ_RSP header? */
1496 if (server
->total_read
< server
->vals
->read_rsp_size
) {
1497 cifs_dbg(FYI
, "%s: server returned short header. got=%u expected=%zu\n",
1498 __func__
, server
->total_read
,
1499 server
->vals
->read_rsp_size
);
1500 rdata
->result
= -EIO
;
1501 return cifs_readv_discard(server
, mid
);
1504 data_offset
= server
->ops
->read_data_offset(buf
) + 4;
1505 if (data_offset
< server
->total_read
) {
1507 * win2k8 sometimes sends an offset of 0 when the read
1508 * is beyond the EOF. Treat it as if the data starts just after
1511 cifs_dbg(FYI
, "%s: data offset (%u) inside read response header\n",
1512 __func__
, data_offset
);
1513 data_offset
= server
->total_read
;
1514 } else if (data_offset
> MAX_CIFS_SMALL_BUFFER_SIZE
) {
1515 /* data_offset is beyond the end of smallbuf */
1516 cifs_dbg(FYI
, "%s: data offset (%u) beyond end of smallbuf\n",
1517 __func__
, data_offset
);
1518 rdata
->result
= -EIO
;
1519 return cifs_readv_discard(server
, mid
);
1522 cifs_dbg(FYI
, "%s: total_read=%u data_offset=%u\n",
1523 __func__
, server
->total_read
, data_offset
);
1525 len
= data_offset
- server
->total_read
;
1527 /* read any junk before data into the rest of smallbuf */
1528 rdata
->iov
.iov_base
= buf
+ server
->total_read
;
1529 rdata
->iov
.iov_len
= len
;
1530 length
= cifs_readv_from_socket(server
, &rdata
->iov
, 1, len
);
1533 server
->total_read
+= length
;
1536 /* set up first iov for signature check */
1537 rdata
->iov
.iov_base
= buf
;
1538 rdata
->iov
.iov_len
= server
->total_read
;
1539 cifs_dbg(FYI
, "0: iov_base=%p iov_len=%zu\n",
1540 rdata
->iov
.iov_base
, rdata
->iov
.iov_len
);
1542 /* how much data is in the response? */
1543 data_len
= server
->ops
->read_data_length(buf
);
1544 if (data_offset
+ data_len
> buflen
) {
1545 /* data_len is corrupt -- discard frame */
1546 rdata
->result
= -EIO
;
1547 return cifs_readv_discard(server
, mid
);
1550 length
= rdata
->read_into_pages(server
, rdata
, data_len
);
1554 server
->total_read
+= length
;
1556 cifs_dbg(FYI
, "total_read=%u buflen=%u remaining=%u\n",
1557 server
->total_read
, buflen
, data_len
);
1559 /* discard anything left over */
1560 if (server
->total_read
< buflen
)
1561 return cifs_readv_discard(server
, mid
);
1563 dequeue_mid(mid
, false);
1564 mid
->resp_buf
= server
->smallbuf
;
1565 server
->smallbuf
= NULL
;
1570 cifs_readv_callback(struct mid_q_entry
*mid
)
1572 struct cifs_readdata
*rdata
= mid
->callback_data
;
1573 struct cifs_tcon
*tcon
= tlink_tcon(rdata
->cfile
->tlink
);
1574 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
1575 struct smb_rqst rqst
= { .rq_iov
= &rdata
->iov
,
1577 .rq_pages
= rdata
->pages
,
1578 .rq_npages
= rdata
->nr_pages
,
1579 .rq_pagesz
= rdata
->pagesz
,
1580 .rq_tailsz
= rdata
->tailsz
};
1582 cifs_dbg(FYI
, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1583 __func__
, mid
->mid
, mid
->mid_state
, rdata
->result
,
1586 switch (mid
->mid_state
) {
1587 case MID_RESPONSE_RECEIVED
:
1588 /* result already set, check signature */
1592 rc
= cifs_verify_signature(&rqst
, server
,
1593 mid
->sequence_number
);
1595 cifs_dbg(VFS
, "SMB signature verification returned error = %d\n",
1598 /* FIXME: should this be counted toward the initiating task? */
1599 task_io_account_read(rdata
->got_bytes
);
1600 cifs_stats_bytes_read(tcon
, rdata
->got_bytes
);
1602 case MID_REQUEST_SUBMITTED
:
1603 case MID_RETRY_NEEDED
:
1604 rdata
->result
= -EAGAIN
;
1605 if (server
->sign
&& rdata
->got_bytes
)
1606 /* reset bytes number since we can not check a sign */
1607 rdata
->got_bytes
= 0;
1608 /* FIXME: should this be counted toward the initiating task? */
1609 task_io_account_read(rdata
->got_bytes
);
1610 cifs_stats_bytes_read(tcon
, rdata
->got_bytes
);
1613 rdata
->result
= -EIO
;
1616 queue_work(cifsiod_wq
, &rdata
->work
);
1617 mutex_lock(&server
->srv_mutex
);
1618 DeleteMidQEntry(mid
);
1619 mutex_unlock(&server
->srv_mutex
);
1620 add_credits(server
, 1, 0);
1623 /* cifs_async_readv - send an async write, and set up mid to handle result */
1625 cifs_async_readv(struct cifs_readdata
*rdata
)
1628 READ_REQ
*smb
= NULL
;
1630 struct cifs_tcon
*tcon
= tlink_tcon(rdata
->cfile
->tlink
);
1631 struct smb_rqst rqst
= { .rq_iov
= &rdata
->iov
,
1634 cifs_dbg(FYI
, "%s: offset=%llu bytes=%u\n",
1635 __func__
, rdata
->offset
, rdata
->bytes
);
1637 if (tcon
->ses
->capabilities
& CAP_LARGE_FILES
)
1640 wct
= 10; /* old style read */
1641 if ((rdata
->offset
>> 32) > 0) {
1642 /* can not handle this big offset for old */
1647 rc
= small_smb_init(SMB_COM_READ_ANDX
, wct
, tcon
, (void **)&smb
);
1651 smb
->hdr
.Pid
= cpu_to_le16((__u16
)rdata
->pid
);
1652 smb
->hdr
.PidHigh
= cpu_to_le16((__u16
)(rdata
->pid
>> 16));
1654 smb
->AndXCommand
= 0xFF; /* none */
1655 smb
->Fid
= rdata
->cfile
->fid
.netfid
;
1656 smb
->OffsetLow
= cpu_to_le32(rdata
->offset
& 0xFFFFFFFF);
1658 smb
->OffsetHigh
= cpu_to_le32(rdata
->offset
>> 32);
1660 smb
->MaxCount
= cpu_to_le16(rdata
->bytes
& 0xFFFF);
1661 smb
->MaxCountHigh
= cpu_to_le32(rdata
->bytes
>> 16);
1665 /* old style read */
1666 struct smb_com_readx_req
*smbr
=
1667 (struct smb_com_readx_req
*)smb
;
1668 smbr
->ByteCount
= 0;
1671 /* 4 for RFC1001 length + 1 for BCC */
1672 rdata
->iov
.iov_base
= smb
;
1673 rdata
->iov
.iov_len
= be32_to_cpu(smb
->hdr
.smb_buf_length
) + 4;
1675 kref_get(&rdata
->refcount
);
1676 rc
= cifs_call_async(tcon
->ses
->server
, &rqst
, cifs_readv_receive
,
1677 cifs_readv_callback
, rdata
, 0);
1680 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_reads
);
1682 kref_put(&rdata
->refcount
, cifs_readdata_release
);
1684 cifs_small_buf_release(smb
);
1689 CIFSSMBRead(const unsigned int xid
, struct cifs_io_parms
*io_parms
,
1690 unsigned int *nbytes
, char **buf
, int *pbuf_type
)
1693 READ_REQ
*pSMB
= NULL
;
1694 READ_RSP
*pSMBr
= NULL
;
1695 char *pReadData
= NULL
;
1697 int resp_buf_type
= 0;
1699 __u32 pid
= io_parms
->pid
;
1700 __u16 netfid
= io_parms
->netfid
;
1701 __u64 offset
= io_parms
->offset
;
1702 struct cifs_tcon
*tcon
= io_parms
->tcon
;
1703 unsigned int count
= io_parms
->length
;
1705 cifs_dbg(FYI
, "Reading %d bytes on fid %d\n", count
, netfid
);
1706 if (tcon
->ses
->capabilities
& CAP_LARGE_FILES
)
1709 wct
= 10; /* old style read */
1710 if ((offset
>> 32) > 0) {
1711 /* can not handle this big offset for old */
1717 rc
= small_smb_init(SMB_COM_READ_ANDX
, wct
, tcon
, (void **) &pSMB
);
1721 pSMB
->hdr
.Pid
= cpu_to_le16((__u16
)pid
);
1722 pSMB
->hdr
.PidHigh
= cpu_to_le16((__u16
)(pid
>> 16));
1724 /* tcon and ses pointer are checked in smb_init */
1725 if (tcon
->ses
->server
== NULL
)
1726 return -ECONNABORTED
;
1728 pSMB
->AndXCommand
= 0xFF; /* none */
1730 pSMB
->OffsetLow
= cpu_to_le32(offset
& 0xFFFFFFFF);
1732 pSMB
->OffsetHigh
= cpu_to_le32(offset
>> 32);
1734 pSMB
->Remaining
= 0;
1735 pSMB
->MaxCount
= cpu_to_le16(count
& 0xFFFF);
1736 pSMB
->MaxCountHigh
= cpu_to_le32(count
>> 16);
1738 pSMB
->ByteCount
= 0; /* no need to do le conversion since 0 */
1740 /* old style read */
1741 struct smb_com_readx_req
*pSMBW
=
1742 (struct smb_com_readx_req
*)pSMB
;
1743 pSMBW
->ByteCount
= 0;
1746 iov
[0].iov_base
= (char *)pSMB
;
1747 iov
[0].iov_len
= be32_to_cpu(pSMB
->hdr
.smb_buf_length
) + 4;
1748 rc
= SendReceive2(xid
, tcon
->ses
, iov
, 1 /* num iovecs */,
1749 &resp_buf_type
, CIFS_LOG_ERROR
);
1750 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_reads
);
1751 pSMBr
= (READ_RSP
*)iov
[0].iov_base
;
1753 cifs_dbg(VFS
, "Send error in read = %d\n", rc
);
1755 int data_length
= le16_to_cpu(pSMBr
->DataLengthHigh
);
1756 data_length
= data_length
<< 16;
1757 data_length
+= le16_to_cpu(pSMBr
->DataLength
);
1758 *nbytes
= data_length
;
1760 /*check that DataLength would not go beyond end of SMB */
1761 if ((data_length
> CIFSMaxBufSize
)
1762 || (data_length
> count
)) {
1763 cifs_dbg(FYI
, "bad length %d for count %d\n",
1764 data_length
, count
);
1768 pReadData
= (char *) (&pSMBr
->hdr
.Protocol
) +
1769 le16_to_cpu(pSMBr
->DataOffset
);
1770 /* if (rc = copy_to_user(buf, pReadData, data_length)) {
1771 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
1773 }*/ /* can not use copy_to_user when using page cache*/
1775 memcpy(*buf
, pReadData
, data_length
);
1779 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
1781 free_rsp_buf(resp_buf_type
, iov
[0].iov_base
);
1782 } else if (resp_buf_type
!= CIFS_NO_BUFFER
) {
1783 /* return buffer to caller to free */
1784 *buf
= iov
[0].iov_base
;
1785 if (resp_buf_type
== CIFS_SMALL_BUFFER
)
1786 *pbuf_type
= CIFS_SMALL_BUFFER
;
1787 else if (resp_buf_type
== CIFS_LARGE_BUFFER
)
1788 *pbuf_type
= CIFS_LARGE_BUFFER
;
1789 } /* else no valid buffer on return - leave as null */
1791 /* Note: On -EAGAIN error only caller can retry on handle based calls
1792 since file handle passed in no longer valid */
1798 CIFSSMBWrite(const unsigned int xid
, struct cifs_io_parms
*io_parms
,
1799 unsigned int *nbytes
, const char *buf
,
1800 const char __user
*ubuf
, const int long_op
)
1803 WRITE_REQ
*pSMB
= NULL
;
1804 WRITE_RSP
*pSMBr
= NULL
;
1805 int bytes_returned
, wct
;
1808 __u32 pid
= io_parms
->pid
;
1809 __u16 netfid
= io_parms
->netfid
;
1810 __u64 offset
= io_parms
->offset
;
1811 struct cifs_tcon
*tcon
= io_parms
->tcon
;
1812 unsigned int count
= io_parms
->length
;
1816 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
1817 if (tcon
->ses
== NULL
)
1818 return -ECONNABORTED
;
1820 if (tcon
->ses
->capabilities
& CAP_LARGE_FILES
)
1824 if ((offset
>> 32) > 0) {
1825 /* can not handle big offset for old srv */
1830 rc
= smb_init(SMB_COM_WRITE_ANDX
, wct
, tcon
, (void **) &pSMB
,
1835 pSMB
->hdr
.Pid
= cpu_to_le16((__u16
)pid
);
1836 pSMB
->hdr
.PidHigh
= cpu_to_le16((__u16
)(pid
>> 16));
1838 /* tcon and ses pointer are checked in smb_init */
1839 if (tcon
->ses
->server
== NULL
)
1840 return -ECONNABORTED
;
1842 pSMB
->AndXCommand
= 0xFF; /* none */
1844 pSMB
->OffsetLow
= cpu_to_le32(offset
& 0xFFFFFFFF);
1846 pSMB
->OffsetHigh
= cpu_to_le32(offset
>> 32);
1848 pSMB
->Reserved
= 0xFFFFFFFF;
1849 pSMB
->WriteMode
= 0;
1850 pSMB
->Remaining
= 0;
1852 /* Can increase buffer size if buffer is big enough in some cases ie we
1853 can send more if LARGE_WRITE_X capability returned by the server and if
1854 our buffer is big enough or if we convert to iovecs on socket writes
1855 and eliminate the copy to the CIFS buffer */
1856 if (tcon
->ses
->capabilities
& CAP_LARGE_WRITE_X
) {
1857 bytes_sent
= min_t(const unsigned int, CIFSMaxBufSize
, count
);
1859 bytes_sent
= (tcon
->ses
->server
->maxBuf
- MAX_CIFS_HDR_SIZE
)
1863 if (bytes_sent
> count
)
1866 cpu_to_le16(offsetof(struct smb_com_write_req
, Data
) - 4);
1868 memcpy(pSMB
->Data
, buf
, bytes_sent
);
1870 if (copy_from_user(pSMB
->Data
, ubuf
, bytes_sent
)) {
1871 cifs_buf_release(pSMB
);
1874 } else if (count
!= 0) {
1876 cifs_buf_release(pSMB
);
1878 } /* else setting file size with write of zero bytes */
1880 byte_count
= bytes_sent
+ 1; /* pad */
1881 else /* wct == 12 */
1882 byte_count
= bytes_sent
+ 5; /* bigger pad, smaller smb hdr */
1884 pSMB
->DataLengthLow
= cpu_to_le16(bytes_sent
& 0xFFFF);
1885 pSMB
->DataLengthHigh
= cpu_to_le16(bytes_sent
>> 16);
1886 inc_rfc1001_len(pSMB
, byte_count
);
1889 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
1890 else { /* old style write has byte count 4 bytes earlier
1892 struct smb_com_writex_req
*pSMBW
=
1893 (struct smb_com_writex_req
*)pSMB
;
1894 pSMBW
->ByteCount
= cpu_to_le16(byte_count
);
1897 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
1898 (struct smb_hdr
*) pSMBr
, &bytes_returned
, long_op
);
1899 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_writes
);
1901 cifs_dbg(FYI
, "Send error in write = %d\n", rc
);
1903 *nbytes
= le16_to_cpu(pSMBr
->CountHigh
);
1904 *nbytes
= (*nbytes
) << 16;
1905 *nbytes
+= le16_to_cpu(pSMBr
->Count
);
1908 * Mask off high 16 bits when bytes written as returned by the
1909 * server is greater than bytes requested by the client. Some
1910 * OS/2 servers are known to set incorrect CountHigh values.
1912 if (*nbytes
> count
)
1916 cifs_buf_release(pSMB
);
1918 /* Note: On -EAGAIN error only caller can retry on handle based calls
1919 since file handle passed in no longer valid */
1925 cifs_writedata_release(struct kref
*refcount
)
1927 struct cifs_writedata
*wdata
= container_of(refcount
,
1928 struct cifs_writedata
, refcount
);
1931 cifsFileInfo_put(wdata
->cfile
);
1937 * Write failed with a retryable error. Resend the write request. It's also
1938 * possible that the page was redirtied so re-clean the page.
1941 cifs_writev_requeue(struct cifs_writedata
*wdata
)
1944 struct inode
*inode
= d_inode(wdata
->cfile
->dentry
);
1945 struct TCP_Server_Info
*server
;
1946 unsigned int rest_len
;
1948 server
= tlink_tcon(wdata
->cfile
->tlink
)->ses
->server
;
1950 rest_len
= wdata
->bytes
;
1952 struct cifs_writedata
*wdata2
;
1953 unsigned int j
, nr_pages
, wsize
, tailsz
, cur_len
;
1955 wsize
= server
->ops
->wp_retry_size(inode
);
1956 if (wsize
< rest_len
) {
1957 nr_pages
= wsize
/ PAGE_CACHE_SIZE
;
1962 cur_len
= nr_pages
* PAGE_CACHE_SIZE
;
1963 tailsz
= PAGE_CACHE_SIZE
;
1965 nr_pages
= DIV_ROUND_UP(rest_len
, PAGE_CACHE_SIZE
);
1967 tailsz
= rest_len
- (nr_pages
- 1) * PAGE_CACHE_SIZE
;
1970 wdata2
= cifs_writedata_alloc(nr_pages
, cifs_writev_complete
);
1976 for (j
= 0; j
< nr_pages
; j
++) {
1977 wdata2
->pages
[j
] = wdata
->pages
[i
+ j
];
1978 lock_page(wdata2
->pages
[j
]);
1979 clear_page_dirty_for_io(wdata2
->pages
[j
]);
1982 wdata2
->sync_mode
= wdata
->sync_mode
;
1983 wdata2
->nr_pages
= nr_pages
;
1984 wdata2
->offset
= page_offset(wdata2
->pages
[0]);
1985 wdata2
->pagesz
= PAGE_CACHE_SIZE
;
1986 wdata2
->tailsz
= tailsz
;
1987 wdata2
->bytes
= cur_len
;
1989 wdata2
->cfile
= find_writable_file(CIFS_I(inode
), false);
1990 if (!wdata2
->cfile
) {
1991 cifs_dbg(VFS
, "No writable handles for inode\n");
1995 wdata2
->pid
= wdata2
->cfile
->pid
;
1996 rc
= server
->ops
->async_writev(wdata2
, cifs_writedata_release
);
1998 for (j
= 0; j
< nr_pages
; j
++) {
1999 unlock_page(wdata2
->pages
[j
]);
2000 if (rc
!= 0 && rc
!= -EAGAIN
) {
2001 SetPageError(wdata2
->pages
[j
]);
2002 end_page_writeback(wdata2
->pages
[j
]);
2003 page_cache_release(wdata2
->pages
[j
]);
2008 kref_put(&wdata2
->refcount
, cifs_writedata_release
);
2014 rest_len
-= cur_len
;
2016 } while (i
< wdata
->nr_pages
);
2018 mapping_set_error(inode
->i_mapping
, rc
);
2019 kref_put(&wdata
->refcount
, cifs_writedata_release
);
2023 cifs_writev_complete(struct work_struct
*work
)
2025 struct cifs_writedata
*wdata
= container_of(work
,
2026 struct cifs_writedata
, work
);
2027 struct inode
*inode
= d_inode(wdata
->cfile
->dentry
);
2030 if (wdata
->result
== 0) {
2031 spin_lock(&inode
->i_lock
);
2032 cifs_update_eof(CIFS_I(inode
), wdata
->offset
, wdata
->bytes
);
2033 spin_unlock(&inode
->i_lock
);
2034 cifs_stats_bytes_written(tlink_tcon(wdata
->cfile
->tlink
),
2036 } else if (wdata
->sync_mode
== WB_SYNC_ALL
&& wdata
->result
== -EAGAIN
)
2037 return cifs_writev_requeue(wdata
);
2039 for (i
= 0; i
< wdata
->nr_pages
; i
++) {
2040 struct page
*page
= wdata
->pages
[i
];
2041 if (wdata
->result
== -EAGAIN
)
2042 __set_page_dirty_nobuffers(page
);
2043 else if (wdata
->result
< 0)
2045 end_page_writeback(page
);
2046 page_cache_release(page
);
2048 if (wdata
->result
!= -EAGAIN
)
2049 mapping_set_error(inode
->i_mapping
, wdata
->result
);
2050 kref_put(&wdata
->refcount
, cifs_writedata_release
);
2053 struct cifs_writedata
*
2054 cifs_writedata_alloc(unsigned int nr_pages
, work_func_t complete
)
2056 struct cifs_writedata
*wdata
;
2058 /* writedata + number of page pointers */
2059 wdata
= kzalloc(sizeof(*wdata
) +
2060 sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
2061 if (wdata
!= NULL
) {
2062 kref_init(&wdata
->refcount
);
2063 INIT_LIST_HEAD(&wdata
->list
);
2064 init_completion(&wdata
->done
);
2065 INIT_WORK(&wdata
->work
, complete
);
2071 * Check the mid_state and signature on received buffer (if any), and queue the
2072 * workqueue completion task.
2075 cifs_writev_callback(struct mid_q_entry
*mid
)
2077 struct cifs_writedata
*wdata
= mid
->callback_data
;
2078 struct cifs_tcon
*tcon
= tlink_tcon(wdata
->cfile
->tlink
);
2079 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
2080 unsigned int written
;
2081 WRITE_RSP
*smb
= (WRITE_RSP
*)mid
->resp_buf
;
2083 switch (mid
->mid_state
) {
2084 case MID_RESPONSE_RECEIVED
:
2085 wdata
->result
= cifs_check_receive(mid
, tcon
->ses
->server
, 0);
2086 if (wdata
->result
!= 0)
2089 written
= le16_to_cpu(smb
->CountHigh
);
2091 written
+= le16_to_cpu(smb
->Count
);
2093 * Mask off high 16 bits when bytes written as returned
2094 * by the server is greater than bytes requested by the
2095 * client. OS/2 servers are known to set incorrect
2098 if (written
> wdata
->bytes
)
2101 if (written
< wdata
->bytes
)
2102 wdata
->result
= -ENOSPC
;
2104 wdata
->bytes
= written
;
2106 case MID_REQUEST_SUBMITTED
:
2107 case MID_RETRY_NEEDED
:
2108 wdata
->result
= -EAGAIN
;
2111 wdata
->result
= -EIO
;
2115 queue_work(cifsiod_wq
, &wdata
->work
);
2116 mutex_lock(&server
->srv_mutex
);
2117 DeleteMidQEntry(mid
);
2118 mutex_unlock(&server
->srv_mutex
);
2119 add_credits(tcon
->ses
->server
, 1, 0);
2122 /* cifs_async_writev - send an async write, and set up mid to handle result */
2124 cifs_async_writev(struct cifs_writedata
*wdata
,
2125 void (*release
)(struct kref
*kref
))
2128 WRITE_REQ
*smb
= NULL
;
2130 struct cifs_tcon
*tcon
= tlink_tcon(wdata
->cfile
->tlink
);
2132 struct smb_rqst rqst
= { };
2134 if (tcon
->ses
->capabilities
& CAP_LARGE_FILES
) {
2138 if (wdata
->offset
>> 32 > 0) {
2139 /* can not handle big offset for old srv */
2144 rc
= small_smb_init(SMB_COM_WRITE_ANDX
, wct
, tcon
, (void **)&smb
);
2146 goto async_writev_out
;
2148 smb
->hdr
.Pid
= cpu_to_le16((__u16
)wdata
->pid
);
2149 smb
->hdr
.PidHigh
= cpu_to_le16((__u16
)(wdata
->pid
>> 16));
2151 smb
->AndXCommand
= 0xFF; /* none */
2152 smb
->Fid
= wdata
->cfile
->fid
.netfid
;
2153 smb
->OffsetLow
= cpu_to_le32(wdata
->offset
& 0xFFFFFFFF);
2155 smb
->OffsetHigh
= cpu_to_le32(wdata
->offset
>> 32);
2156 smb
->Reserved
= 0xFFFFFFFF;
2161 cpu_to_le16(offsetof(struct smb_com_write_req
, Data
) - 4);
2163 /* 4 for RFC1001 length + 1 for BCC */
2164 iov
.iov_len
= be32_to_cpu(smb
->hdr
.smb_buf_length
) + 4 + 1;
2169 rqst
.rq_pages
= wdata
->pages
;
2170 rqst
.rq_npages
= wdata
->nr_pages
;
2171 rqst
.rq_pagesz
= wdata
->pagesz
;
2172 rqst
.rq_tailsz
= wdata
->tailsz
;
2174 cifs_dbg(FYI
, "async write at %llu %u bytes\n",
2175 wdata
->offset
, wdata
->bytes
);
2177 smb
->DataLengthLow
= cpu_to_le16(wdata
->bytes
& 0xFFFF);
2178 smb
->DataLengthHigh
= cpu_to_le16(wdata
->bytes
>> 16);
2181 inc_rfc1001_len(&smb
->hdr
, wdata
->bytes
+ 1);
2182 put_bcc(wdata
->bytes
+ 1, &smb
->hdr
);
2185 struct smb_com_writex_req
*smbw
=
2186 (struct smb_com_writex_req
*)smb
;
2187 inc_rfc1001_len(&smbw
->hdr
, wdata
->bytes
+ 5);
2188 put_bcc(wdata
->bytes
+ 5, &smbw
->hdr
);
2189 iov
.iov_len
+= 4; /* pad bigger by four bytes */
2192 kref_get(&wdata
->refcount
);
2193 rc
= cifs_call_async(tcon
->ses
->server
, &rqst
, NULL
,
2194 cifs_writev_callback
, wdata
, 0);
2197 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_writes
);
2199 kref_put(&wdata
->refcount
, release
);
2202 cifs_small_buf_release(smb
);
2207 CIFSSMBWrite2(const unsigned int xid
, struct cifs_io_parms
*io_parms
,
2208 unsigned int *nbytes
, struct kvec
*iov
, int n_vec
)
2211 WRITE_REQ
*pSMB
= NULL
;
2214 int resp_buf_type
= 0;
2215 __u32 pid
= io_parms
->pid
;
2216 __u16 netfid
= io_parms
->netfid
;
2217 __u64 offset
= io_parms
->offset
;
2218 struct cifs_tcon
*tcon
= io_parms
->tcon
;
2219 unsigned int count
= io_parms
->length
;
2223 cifs_dbg(FYI
, "write2 at %lld %d bytes\n", (long long)offset
, count
);
2225 if (tcon
->ses
->capabilities
& CAP_LARGE_FILES
) {
2229 if ((offset
>> 32) > 0) {
2230 /* can not handle big offset for old srv */
2234 rc
= small_smb_init(SMB_COM_WRITE_ANDX
, wct
, tcon
, (void **) &pSMB
);
2238 pSMB
->hdr
.Pid
= cpu_to_le16((__u16
)pid
);
2239 pSMB
->hdr
.PidHigh
= cpu_to_le16((__u16
)(pid
>> 16));
2241 /* tcon and ses pointer are checked in smb_init */
2242 if (tcon
->ses
->server
== NULL
)
2243 return -ECONNABORTED
;
2245 pSMB
->AndXCommand
= 0xFF; /* none */
2247 pSMB
->OffsetLow
= cpu_to_le32(offset
& 0xFFFFFFFF);
2249 pSMB
->OffsetHigh
= cpu_to_le32(offset
>> 32);
2250 pSMB
->Reserved
= 0xFFFFFFFF;
2251 pSMB
->WriteMode
= 0;
2252 pSMB
->Remaining
= 0;
2255 cpu_to_le16(offsetof(struct smb_com_write_req
, Data
) - 4);
2257 pSMB
->DataLengthLow
= cpu_to_le16(count
& 0xFFFF);
2258 pSMB
->DataLengthHigh
= cpu_to_le16(count
>> 16);
2259 /* header + 1 byte pad */
2260 smb_hdr_len
= be32_to_cpu(pSMB
->hdr
.smb_buf_length
) + 1;
2262 inc_rfc1001_len(pSMB
, count
+ 1);
2263 else /* wct == 12 */
2264 inc_rfc1001_len(pSMB
, count
+ 5); /* smb data starts later */
2266 pSMB
->ByteCount
= cpu_to_le16(count
+ 1);
2267 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
2268 struct smb_com_writex_req
*pSMBW
=
2269 (struct smb_com_writex_req
*)pSMB
;
2270 pSMBW
->ByteCount
= cpu_to_le16(count
+ 5);
2272 iov
[0].iov_base
= pSMB
;
2274 iov
[0].iov_len
= smb_hdr_len
+ 4;
2275 else /* wct == 12 pad bigger by four bytes */
2276 iov
[0].iov_len
= smb_hdr_len
+ 8;
2279 rc
= SendReceive2(xid
, tcon
->ses
, iov
, n_vec
+ 1, &resp_buf_type
, 0);
2280 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_writes
);
2282 cifs_dbg(FYI
, "Send error Write2 = %d\n", rc
);
2283 } else if (resp_buf_type
== 0) {
2284 /* presumably this can not happen, but best to be safe */
2287 WRITE_RSP
*pSMBr
= (WRITE_RSP
*)iov
[0].iov_base
;
2288 *nbytes
= le16_to_cpu(pSMBr
->CountHigh
);
2289 *nbytes
= (*nbytes
) << 16;
2290 *nbytes
+= le16_to_cpu(pSMBr
->Count
);
2293 * Mask off high 16 bits when bytes written as returned by the
2294 * server is greater than bytes requested by the client. OS/2
2295 * servers are known to set incorrect CountHigh values.
2297 if (*nbytes
> count
)
2301 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
2302 free_rsp_buf(resp_buf_type
, iov
[0].iov_base
);
2304 /* Note: On -EAGAIN error only caller can retry on handle based calls
2305 since file handle passed in no longer valid */
2310 int cifs_lockv(const unsigned int xid
, struct cifs_tcon
*tcon
,
2311 const __u16 netfid
, const __u8 lock_type
, const __u32 num_unlock
,
2312 const __u32 num_lock
, LOCKING_ANDX_RANGE
*buf
)
2315 LOCK_REQ
*pSMB
= NULL
;
2320 cifs_dbg(FYI
, "cifs_lockv num lock %d num unlock %d\n",
2321 num_lock
, num_unlock
);
2323 rc
= small_smb_init(SMB_COM_LOCKING_ANDX
, 8, tcon
, (void **) &pSMB
);
2328 pSMB
->NumberOfLocks
= cpu_to_le16(num_lock
);
2329 pSMB
->NumberOfUnlocks
= cpu_to_le16(num_unlock
);
2330 pSMB
->LockType
= lock_type
;
2331 pSMB
->AndXCommand
= 0xFF; /* none */
2332 pSMB
->Fid
= netfid
; /* netfid stays le */
2334 count
= (num_unlock
+ num_lock
) * sizeof(LOCKING_ANDX_RANGE
);
2335 inc_rfc1001_len(pSMB
, count
);
2336 pSMB
->ByteCount
= cpu_to_le16(count
);
2338 iov
[0].iov_base
= (char *)pSMB
;
2339 iov
[0].iov_len
= be32_to_cpu(pSMB
->hdr
.smb_buf_length
) + 4 -
2340 (num_unlock
+ num_lock
) * sizeof(LOCKING_ANDX_RANGE
);
2341 iov
[1].iov_base
= (char *)buf
;
2342 iov
[1].iov_len
= (num_unlock
+ num_lock
) * sizeof(LOCKING_ANDX_RANGE
);
2344 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_locks
);
2345 rc
= SendReceive2(xid
, tcon
->ses
, iov
, 2, &resp_buf_type
, CIFS_NO_RESP
);
2347 cifs_dbg(FYI
, "Send error in cifs_lockv = %d\n", rc
);
2353 CIFSSMBLock(const unsigned int xid
, struct cifs_tcon
*tcon
,
2354 const __u16 smb_file_id
, const __u32 netpid
, const __u64 len
,
2355 const __u64 offset
, const __u32 numUnlock
,
2356 const __u32 numLock
, const __u8 lockType
,
2357 const bool waitFlag
, const __u8 oplock_level
)
2360 LOCK_REQ
*pSMB
= NULL
;
2361 /* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
2366 cifs_dbg(FYI
, "CIFSSMBLock timeout %d numLock %d\n",
2367 (int)waitFlag
, numLock
);
2368 rc
= small_smb_init(SMB_COM_LOCKING_ANDX
, 8, tcon
, (void **) &pSMB
);
2373 if (lockType
== LOCKING_ANDX_OPLOCK_RELEASE
) {
2374 /* no response expected */
2375 flags
= CIFS_ASYNC_OP
| CIFS_OBREAK_OP
;
2377 } else if (waitFlag
) {
2378 flags
= CIFS_BLOCKING_OP
; /* blocking operation, no timeout */
2379 pSMB
->Timeout
= cpu_to_le32(-1);/* blocking - do not time out */
2384 pSMB
->NumberOfLocks
= cpu_to_le16(numLock
);
2385 pSMB
->NumberOfUnlocks
= cpu_to_le16(numUnlock
);
2386 pSMB
->LockType
= lockType
;
2387 pSMB
->OplockLevel
= oplock_level
;
2388 pSMB
->AndXCommand
= 0xFF; /* none */
2389 pSMB
->Fid
= smb_file_id
; /* netfid stays le */
2391 if ((numLock
!= 0) || (numUnlock
!= 0)) {
2392 pSMB
->Locks
[0].Pid
= cpu_to_le16(netpid
);
2393 /* BB where to store pid high? */
2394 pSMB
->Locks
[0].LengthLow
= cpu_to_le32((u32
)len
);
2395 pSMB
->Locks
[0].LengthHigh
= cpu_to_le32((u32
)(len
>>32));
2396 pSMB
->Locks
[0].OffsetLow
= cpu_to_le32((u32
)offset
);
2397 pSMB
->Locks
[0].OffsetHigh
= cpu_to_le32((u32
)(offset
>>32));
2398 count
= sizeof(LOCKING_ANDX_RANGE
);
2403 inc_rfc1001_len(pSMB
, count
);
2404 pSMB
->ByteCount
= cpu_to_le16(count
);
2407 rc
= SendReceiveBlockingLock(xid
, tcon
, (struct smb_hdr
*) pSMB
,
2408 (struct smb_hdr
*) pSMB
, &bytes_returned
);
2409 cifs_small_buf_release(pSMB
);
2411 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *)pSMB
, flags
);
2412 /* SMB buffer freed by function above */
2414 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_locks
);
2416 cifs_dbg(FYI
, "Send error in Lock = %d\n", rc
);
2418 /* Note: On -EAGAIN error only caller can retry on handle based calls
2419 since file handle passed in no longer valid */
2424 CIFSSMBPosixLock(const unsigned int xid
, struct cifs_tcon
*tcon
,
2425 const __u16 smb_file_id
, const __u32 netpid
,
2426 const loff_t start_offset
, const __u64 len
,
2427 struct file_lock
*pLockData
, const __u16 lock_type
,
2428 const bool waitFlag
)
2430 struct smb_com_transaction2_sfi_req
*pSMB
= NULL
;
2431 struct smb_com_transaction2_sfi_rsp
*pSMBr
= NULL
;
2432 struct cifs_posix_lock
*parm_data
;
2435 int bytes_returned
= 0;
2436 int resp_buf_type
= 0;
2437 __u16 params
, param_offset
, offset
, byte_count
, count
;
2440 cifs_dbg(FYI
, "Posix Lock\n");
2442 rc
= small_smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
);
2447 pSMBr
= (struct smb_com_transaction2_sfi_rsp
*)pSMB
;
2450 pSMB
->MaxSetupCount
= 0;
2453 pSMB
->Reserved2
= 0;
2454 param_offset
= offsetof(struct smb_com_transaction2_sfi_req
, Fid
) - 4;
2455 offset
= param_offset
+ params
;
2457 count
= sizeof(struct cifs_posix_lock
);
2458 pSMB
->MaxParameterCount
= cpu_to_le16(2);
2459 pSMB
->MaxDataCount
= cpu_to_le16(1000); /* BB find max SMB from sess */
2460 pSMB
->SetupCount
= 1;
2461 pSMB
->Reserved3
= 0;
2463 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION
);
2465 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_FILE_INFORMATION
);
2466 byte_count
= 3 /* pad */ + params
+ count
;
2467 pSMB
->DataCount
= cpu_to_le16(count
);
2468 pSMB
->ParameterCount
= cpu_to_le16(params
);
2469 pSMB
->TotalDataCount
= pSMB
->DataCount
;
2470 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
2471 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
2472 parm_data
= (struct cifs_posix_lock
*)
2473 (((char *) &pSMB
->hdr
.Protocol
) + offset
);
2475 parm_data
->lock_type
= cpu_to_le16(lock_type
);
2477 timeout
= CIFS_BLOCKING_OP
; /* blocking operation, no timeout */
2478 parm_data
->lock_flags
= cpu_to_le16(1);
2479 pSMB
->Timeout
= cpu_to_le32(-1);
2483 parm_data
->pid
= cpu_to_le32(netpid
);
2484 parm_data
->start
= cpu_to_le64(start_offset
);
2485 parm_data
->length
= cpu_to_le64(len
); /* normalize negative numbers */
2487 pSMB
->DataOffset
= cpu_to_le16(offset
);
2488 pSMB
->Fid
= smb_file_id
;
2489 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_POSIX_LOCK
);
2490 pSMB
->Reserved4
= 0;
2491 inc_rfc1001_len(pSMB
, byte_count
);
2492 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
2494 rc
= SendReceiveBlockingLock(xid
, tcon
, (struct smb_hdr
*) pSMB
,
2495 (struct smb_hdr
*) pSMBr
, &bytes_returned
);
2497 iov
[0].iov_base
= (char *)pSMB
;
2498 iov
[0].iov_len
= be32_to_cpu(pSMB
->hdr
.smb_buf_length
) + 4;
2499 rc
= SendReceive2(xid
, tcon
->ses
, iov
, 1 /* num iovecs */,
2500 &resp_buf_type
, timeout
);
2501 pSMB
= NULL
; /* request buf already freed by SendReceive2. Do
2502 not try to free it twice below on exit */
2503 pSMBr
= (struct smb_com_transaction2_sfi_rsp
*)iov
[0].iov_base
;
2507 cifs_dbg(FYI
, "Send error in Posix Lock = %d\n", rc
);
2508 } else if (pLockData
) {
2509 /* lock structure can be returned on get */
2512 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
2514 if (rc
|| get_bcc(&pSMBr
->hdr
) < sizeof(*parm_data
)) {
2515 rc
= -EIO
; /* bad smb */
2518 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
2519 data_count
= le16_to_cpu(pSMBr
->t2
.DataCount
);
2520 if (data_count
< sizeof(struct cifs_posix_lock
)) {
2524 parm_data
= (struct cifs_posix_lock
*)
2525 ((char *)&pSMBr
->hdr
.Protocol
+ data_offset
);
2526 if (parm_data
->lock_type
== cpu_to_le16(CIFS_UNLCK
))
2527 pLockData
->fl_type
= F_UNLCK
;
2529 if (parm_data
->lock_type
==
2530 cpu_to_le16(CIFS_RDLCK
))
2531 pLockData
->fl_type
= F_RDLCK
;
2532 else if (parm_data
->lock_type
==
2533 cpu_to_le16(CIFS_WRLCK
))
2534 pLockData
->fl_type
= F_WRLCK
;
2536 pLockData
->fl_start
= le64_to_cpu(parm_data
->start
);
2537 pLockData
->fl_end
= pLockData
->fl_start
+
2538 le64_to_cpu(parm_data
->length
) - 1;
2539 pLockData
->fl_pid
= le32_to_cpu(parm_data
->pid
);
2545 cifs_small_buf_release(pSMB
);
2547 free_rsp_buf(resp_buf_type
, iov
[0].iov_base
);
2549 /* Note: On -EAGAIN error only caller can retry on handle based calls
2550 since file handle passed in no longer valid */
2557 CIFSSMBClose(const unsigned int xid
, struct cifs_tcon
*tcon
, int smb_file_id
)
2560 CLOSE_REQ
*pSMB
= NULL
;
2561 cifs_dbg(FYI
, "In CIFSSMBClose\n");
2563 /* do not retry on dead session on close */
2564 rc
= small_smb_init(SMB_COM_CLOSE
, 3, tcon
, (void **) &pSMB
);
2570 pSMB
->FileID
= (__u16
) smb_file_id
;
2571 pSMB
->LastWriteTime
= 0xFFFFFFFF;
2572 pSMB
->ByteCount
= 0;
2573 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) pSMB
, 0);
2574 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_closes
);
2577 /* EINTR is expected when user ctl-c to kill app */
2578 cifs_dbg(VFS
, "Send error in Close = %d\n", rc
);
2582 /* Since session is dead, file will be closed on server already */
2590 CIFSSMBFlush(const unsigned int xid
, struct cifs_tcon
*tcon
, int smb_file_id
)
2593 FLUSH_REQ
*pSMB
= NULL
;
2594 cifs_dbg(FYI
, "In CIFSSMBFlush\n");
2596 rc
= small_smb_init(SMB_COM_FLUSH
, 1, tcon
, (void **) &pSMB
);
2600 pSMB
->FileID
= (__u16
) smb_file_id
;
2601 pSMB
->ByteCount
= 0;
2602 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) pSMB
, 0);
2603 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_flushes
);
2605 cifs_dbg(VFS
, "Send error in Flush = %d\n", rc
);
2611 CIFSSMBRename(const unsigned int xid
, struct cifs_tcon
*tcon
,
2612 const char *from_name
, const char *to_name
,
2613 struct cifs_sb_info
*cifs_sb
)
2616 RENAME_REQ
*pSMB
= NULL
;
2617 RENAME_RSP
*pSMBr
= NULL
;
2619 int name_len
, name_len2
;
2621 int remap
= cifs_remap(cifs_sb
);
2623 cifs_dbg(FYI
, "In CIFSSMBRename\n");
2625 rc
= smb_init(SMB_COM_RENAME
, 1, tcon
, (void **) &pSMB
,
2630 pSMB
->BufferFormat
= 0x04;
2631 pSMB
->SearchAttributes
=
2632 cpu_to_le16(ATTR_READONLY
| ATTR_HIDDEN
| ATTR_SYSTEM
|
2635 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
2636 name_len
= cifsConvertToUTF16((__le16
*) pSMB
->OldFileName
,
2637 from_name
, PATH_MAX
,
2638 cifs_sb
->local_nls
, remap
);
2639 name_len
++; /* trailing null */
2641 pSMB
->OldFileName
[name_len
] = 0x04; /* pad */
2642 /* protocol requires ASCII signature byte on Unicode string */
2643 pSMB
->OldFileName
[name_len
+ 1] = 0x00;
2645 cifsConvertToUTF16((__le16
*)&pSMB
->OldFileName
[name_len
+2],
2646 to_name
, PATH_MAX
, cifs_sb
->local_nls
,
2648 name_len2
+= 1 /* trailing null */ + 1 /* Signature word */ ;
2649 name_len2
*= 2; /* convert to bytes */
2650 } else { /* BB improve the check for buffer overruns BB */
2651 name_len
= strnlen(from_name
, PATH_MAX
);
2652 name_len
++; /* trailing null */
2653 strncpy(pSMB
->OldFileName
, from_name
, name_len
);
2654 name_len2
= strnlen(to_name
, PATH_MAX
);
2655 name_len2
++; /* trailing null */
2656 pSMB
->OldFileName
[name_len
] = 0x04; /* 2nd buffer format */
2657 strncpy(&pSMB
->OldFileName
[name_len
+ 1], to_name
, name_len2
);
2658 name_len2
++; /* trailing null */
2659 name_len2
++; /* signature byte */
2662 count
= 1 /* 1st signature byte */ + name_len
+ name_len2
;
2663 inc_rfc1001_len(pSMB
, count
);
2664 pSMB
->ByteCount
= cpu_to_le16(count
);
2666 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
2667 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
2668 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_renames
);
2670 cifs_dbg(FYI
, "Send error in rename = %d\n", rc
);
2672 cifs_buf_release(pSMB
);
2680 int CIFSSMBRenameOpenFile(const unsigned int xid
, struct cifs_tcon
*pTcon
,
2681 int netfid
, const char *target_name
,
2682 const struct nls_table
*nls_codepage
, int remap
)
2684 struct smb_com_transaction2_sfi_req
*pSMB
= NULL
;
2685 struct smb_com_transaction2_sfi_rsp
*pSMBr
= NULL
;
2686 struct set_file_rename
*rename_info
;
2688 char dummy_string
[30];
2690 int bytes_returned
= 0;
2692 __u16 params
, param_offset
, offset
, count
, byte_count
;
2694 cifs_dbg(FYI
, "Rename to File by handle\n");
2695 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, pTcon
, (void **) &pSMB
,
2701 pSMB
->MaxSetupCount
= 0;
2705 pSMB
->Reserved2
= 0;
2706 param_offset
= offsetof(struct smb_com_transaction2_sfi_req
, Fid
) - 4;
2707 offset
= param_offset
+ params
;
2709 data_offset
= (char *) (&pSMB
->hdr
.Protocol
) + offset
;
2710 rename_info
= (struct set_file_rename
*) data_offset
;
2711 pSMB
->MaxParameterCount
= cpu_to_le16(2);
2712 pSMB
->MaxDataCount
= cpu_to_le16(1000); /* BB find max SMB from sess */
2713 pSMB
->SetupCount
= 1;
2714 pSMB
->Reserved3
= 0;
2715 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_FILE_INFORMATION
);
2716 byte_count
= 3 /* pad */ + params
;
2717 pSMB
->ParameterCount
= cpu_to_le16(params
);
2718 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
2719 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
2720 pSMB
->DataOffset
= cpu_to_le16(offset
);
2721 /* construct random name ".cifs_tmp<inodenum><mid>" */
2722 rename_info
->overwrite
= cpu_to_le32(1);
2723 rename_info
->root_fid
= 0;
2724 /* unicode only call */
2725 if (target_name
== NULL
) {
2726 sprintf(dummy_string
, "cifs%x", pSMB
->hdr
.Mid
);
2728 cifsConvertToUTF16((__le16
*)rename_info
->target_name
,
2729 dummy_string
, 24, nls_codepage
, remap
);
2732 cifsConvertToUTF16((__le16
*)rename_info
->target_name
,
2733 target_name
, PATH_MAX
, nls_codepage
,
2736 rename_info
->target_name_len
= cpu_to_le32(2 * len_of_str
);
2737 count
= 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str
);
2738 byte_count
+= count
;
2739 pSMB
->DataCount
= cpu_to_le16(count
);
2740 pSMB
->TotalDataCount
= pSMB
->DataCount
;
2742 pSMB
->InformationLevel
=
2743 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION
);
2744 pSMB
->Reserved4
= 0;
2745 inc_rfc1001_len(pSMB
, byte_count
);
2746 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
2747 rc
= SendReceive(xid
, pTcon
->ses
, (struct smb_hdr
*) pSMB
,
2748 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
2749 cifs_stats_inc(&pTcon
->stats
.cifs_stats
.num_t2renames
);
2751 cifs_dbg(FYI
, "Send error in Rename (by file handle) = %d\n",
2754 cifs_buf_release(pSMB
);
2756 /* Note: On -EAGAIN error only caller can retry on handle based calls
2757 since file handle passed in no longer valid */
2763 CIFSSMBCopy(const unsigned int xid
, struct cifs_tcon
*tcon
,
2764 const char *fromName
, const __u16 target_tid
, const char *toName
,
2765 const int flags
, const struct nls_table
*nls_codepage
, int remap
)
2768 COPY_REQ
*pSMB
= NULL
;
2769 COPY_RSP
*pSMBr
= NULL
;
2771 int name_len
, name_len2
;
2774 cifs_dbg(FYI
, "In CIFSSMBCopy\n");
2776 rc
= smb_init(SMB_COM_COPY
, 1, tcon
, (void **) &pSMB
,
2781 pSMB
->BufferFormat
= 0x04;
2782 pSMB
->Tid2
= target_tid
;
2784 pSMB
->Flags
= cpu_to_le16(flags
& COPY_TREE
);
2786 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
2787 name_len
= cifsConvertToUTF16((__le16
*) pSMB
->OldFileName
,
2788 fromName
, PATH_MAX
, nls_codepage
,
2790 name_len
++; /* trailing null */
2792 pSMB
->OldFileName
[name_len
] = 0x04; /* pad */
2793 /* protocol requires ASCII signature byte on Unicode string */
2794 pSMB
->OldFileName
[name_len
+ 1] = 0x00;
2796 cifsConvertToUTF16((__le16
*)&pSMB
->OldFileName
[name_len
+2],
2797 toName
, PATH_MAX
, nls_codepage
, remap
);
2798 name_len2
+= 1 /* trailing null */ + 1 /* Signature word */ ;
2799 name_len2
*= 2; /* convert to bytes */
2800 } else { /* BB improve the check for buffer overruns BB */
2801 name_len
= strnlen(fromName
, PATH_MAX
);
2802 name_len
++; /* trailing null */
2803 strncpy(pSMB
->OldFileName
, fromName
, name_len
);
2804 name_len2
= strnlen(toName
, PATH_MAX
);
2805 name_len2
++; /* trailing null */
2806 pSMB
->OldFileName
[name_len
] = 0x04; /* 2nd buffer format */
2807 strncpy(&pSMB
->OldFileName
[name_len
+ 1], toName
, name_len2
);
2808 name_len2
++; /* trailing null */
2809 name_len2
++; /* signature byte */
2812 count
= 1 /* 1st signature byte */ + name_len
+ name_len2
;
2813 inc_rfc1001_len(pSMB
, count
);
2814 pSMB
->ByteCount
= cpu_to_le16(count
);
2816 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
2817 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
2819 cifs_dbg(FYI
, "Send error in copy = %d with %d files copied\n",
2820 rc
, le16_to_cpu(pSMBr
->CopyCount
));
2822 cifs_buf_release(pSMB
);
2831 CIFSUnixCreateSymLink(const unsigned int xid
, struct cifs_tcon
*tcon
,
2832 const char *fromName
, const char *toName
,
2833 const struct nls_table
*nls_codepage
, int remap
)
2835 TRANSACTION2_SPI_REQ
*pSMB
= NULL
;
2836 TRANSACTION2_SPI_RSP
*pSMBr
= NULL
;
2839 int name_len_target
;
2841 int bytes_returned
= 0;
2842 __u16 params
, param_offset
, offset
, byte_count
;
2844 cifs_dbg(FYI
, "In Symlink Unix style\n");
2846 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
2851 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
2853 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, fromName
,
2854 /* find define for this maxpathcomponent */
2855 PATH_MAX
, nls_codepage
, remap
);
2856 name_len
++; /* trailing null */
2859 } else { /* BB improve the check for buffer overruns BB */
2860 name_len
= strnlen(fromName
, PATH_MAX
);
2861 name_len
++; /* trailing null */
2862 strncpy(pSMB
->FileName
, fromName
, name_len
);
2864 params
= 6 + name_len
;
2865 pSMB
->MaxSetupCount
= 0;
2869 pSMB
->Reserved2
= 0;
2870 param_offset
= offsetof(struct smb_com_transaction2_spi_req
,
2871 InformationLevel
) - 4;
2872 offset
= param_offset
+ params
;
2874 data_offset
= (char *) (&pSMB
->hdr
.Protocol
) + offset
;
2875 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
2877 cifsConvertToUTF16((__le16
*) data_offset
, toName
,
2878 /* find define for this maxpathcomponent */
2879 PATH_MAX
, nls_codepage
, remap
);
2880 name_len_target
++; /* trailing null */
2881 name_len_target
*= 2;
2882 } else { /* BB improve the check for buffer overruns BB */
2883 name_len_target
= strnlen(toName
, PATH_MAX
);
2884 name_len_target
++; /* trailing null */
2885 strncpy(data_offset
, toName
, name_len_target
);
2888 pSMB
->MaxParameterCount
= cpu_to_le16(2);
2889 /* BB find exact max on data count below from sess */
2890 pSMB
->MaxDataCount
= cpu_to_le16(1000);
2891 pSMB
->SetupCount
= 1;
2892 pSMB
->Reserved3
= 0;
2893 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_PATH_INFORMATION
);
2894 byte_count
= 3 /* pad */ + params
+ name_len_target
;
2895 pSMB
->DataCount
= cpu_to_le16(name_len_target
);
2896 pSMB
->ParameterCount
= cpu_to_le16(params
);
2897 pSMB
->TotalDataCount
= pSMB
->DataCount
;
2898 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
2899 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
2900 pSMB
->DataOffset
= cpu_to_le16(offset
);
2901 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_FILE_UNIX_LINK
);
2902 pSMB
->Reserved4
= 0;
2903 inc_rfc1001_len(pSMB
, byte_count
);
2904 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
2905 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
2906 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
2907 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_symlinks
);
2909 cifs_dbg(FYI
, "Send error in SetPathInfo create symlink = %d\n",
2912 cifs_buf_release(pSMB
);
2915 goto createSymLinkRetry
;
2921 CIFSUnixCreateHardLink(const unsigned int xid
, struct cifs_tcon
*tcon
,
2922 const char *fromName
, const char *toName
,
2923 const struct nls_table
*nls_codepage
, int remap
)
2925 TRANSACTION2_SPI_REQ
*pSMB
= NULL
;
2926 TRANSACTION2_SPI_RSP
*pSMBr
= NULL
;
2929 int name_len_target
;
2931 int bytes_returned
= 0;
2932 __u16 params
, param_offset
, offset
, byte_count
;
2934 cifs_dbg(FYI
, "In Create Hard link Unix style\n");
2935 createHardLinkRetry
:
2936 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
2941 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
2942 name_len
= cifsConvertToUTF16((__le16
*) pSMB
->FileName
, toName
,
2943 PATH_MAX
, nls_codepage
, remap
);
2944 name_len
++; /* trailing null */
2947 } else { /* BB improve the check for buffer overruns BB */
2948 name_len
= strnlen(toName
, PATH_MAX
);
2949 name_len
++; /* trailing null */
2950 strncpy(pSMB
->FileName
, toName
, name_len
);
2952 params
= 6 + name_len
;
2953 pSMB
->MaxSetupCount
= 0;
2957 pSMB
->Reserved2
= 0;
2958 param_offset
= offsetof(struct smb_com_transaction2_spi_req
,
2959 InformationLevel
) - 4;
2960 offset
= param_offset
+ params
;
2962 data_offset
= (char *) (&pSMB
->hdr
.Protocol
) + offset
;
2963 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
2965 cifsConvertToUTF16((__le16
*) data_offset
, fromName
,
2966 PATH_MAX
, nls_codepage
, remap
);
2967 name_len_target
++; /* trailing null */
2968 name_len_target
*= 2;
2969 } else { /* BB improve the check for buffer overruns BB */
2970 name_len_target
= strnlen(fromName
, PATH_MAX
);
2971 name_len_target
++; /* trailing null */
2972 strncpy(data_offset
, fromName
, name_len_target
);
2975 pSMB
->MaxParameterCount
= cpu_to_le16(2);
2976 /* BB find exact max on data count below from sess*/
2977 pSMB
->MaxDataCount
= cpu_to_le16(1000);
2978 pSMB
->SetupCount
= 1;
2979 pSMB
->Reserved3
= 0;
2980 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_PATH_INFORMATION
);
2981 byte_count
= 3 /* pad */ + params
+ name_len_target
;
2982 pSMB
->ParameterCount
= cpu_to_le16(params
);
2983 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
2984 pSMB
->DataCount
= cpu_to_le16(name_len_target
);
2985 pSMB
->TotalDataCount
= pSMB
->DataCount
;
2986 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
2987 pSMB
->DataOffset
= cpu_to_le16(offset
);
2988 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_FILE_UNIX_HLINK
);
2989 pSMB
->Reserved4
= 0;
2990 inc_rfc1001_len(pSMB
, byte_count
);
2991 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
2992 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
2993 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
2994 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_hardlinks
);
2996 cifs_dbg(FYI
, "Send error in SetPathInfo (hard link) = %d\n",
2999 cifs_buf_release(pSMB
);
3001 goto createHardLinkRetry
;
3007 CIFSCreateHardLink(const unsigned int xid
, struct cifs_tcon
*tcon
,
3008 const char *from_name
, const char *to_name
,
3009 struct cifs_sb_info
*cifs_sb
)
3012 NT_RENAME_REQ
*pSMB
= NULL
;
3013 RENAME_RSP
*pSMBr
= NULL
;
3015 int name_len
, name_len2
;
3017 int remap
= cifs_remap(cifs_sb
);
3019 cifs_dbg(FYI
, "In CIFSCreateHardLink\n");
3020 winCreateHardLinkRetry
:
3022 rc
= smb_init(SMB_COM_NT_RENAME
, 4, tcon
, (void **) &pSMB
,
3027 pSMB
->SearchAttributes
=
3028 cpu_to_le16(ATTR_READONLY
| ATTR_HIDDEN
| ATTR_SYSTEM
|
3030 pSMB
->Flags
= cpu_to_le16(CREATE_HARD_LINK
);
3031 pSMB
->ClusterCount
= 0;
3033 pSMB
->BufferFormat
= 0x04;
3035 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
3037 cifsConvertToUTF16((__le16
*) pSMB
->OldFileName
, from_name
,
3038 PATH_MAX
, cifs_sb
->local_nls
, remap
);
3039 name_len
++; /* trailing null */
3042 /* protocol specifies ASCII buffer format (0x04) for unicode */
3043 pSMB
->OldFileName
[name_len
] = 0x04;
3044 pSMB
->OldFileName
[name_len
+ 1] = 0x00; /* pad */
3046 cifsConvertToUTF16((__le16
*)&pSMB
->OldFileName
[name_len
+2],
3047 to_name
, PATH_MAX
, cifs_sb
->local_nls
,
3049 name_len2
+= 1 /* trailing null */ + 1 /* Signature word */ ;
3050 name_len2
*= 2; /* convert to bytes */
3051 } else { /* BB improve the check for buffer overruns BB */
3052 name_len
= strnlen(from_name
, PATH_MAX
);
3053 name_len
++; /* trailing null */
3054 strncpy(pSMB
->OldFileName
, from_name
, name_len
);
3055 name_len2
= strnlen(to_name
, PATH_MAX
);
3056 name_len2
++; /* trailing null */
3057 pSMB
->OldFileName
[name_len
] = 0x04; /* 2nd buffer format */
3058 strncpy(&pSMB
->OldFileName
[name_len
+ 1], to_name
, name_len2
);
3059 name_len2
++; /* trailing null */
3060 name_len2
++; /* signature byte */
3063 count
= 1 /* string type byte */ + name_len
+ name_len2
;
3064 inc_rfc1001_len(pSMB
, count
);
3065 pSMB
->ByteCount
= cpu_to_le16(count
);
3067 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
3068 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
3069 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_hardlinks
);
3071 cifs_dbg(FYI
, "Send error in hard link (NT rename) = %d\n", rc
);
3073 cifs_buf_release(pSMB
);
3075 goto winCreateHardLinkRetry
;
3081 CIFSSMBUnixQuerySymLink(const unsigned int xid
, struct cifs_tcon
*tcon
,
3082 const unsigned char *searchName
, char **symlinkinfo
,
3083 const struct nls_table
*nls_codepage
, int remap
)
3085 /* SMB_QUERY_FILE_UNIX_LINK */
3086 TRANSACTION2_QPI_REQ
*pSMB
= NULL
;
3087 TRANSACTION2_QPI_RSP
*pSMBr
= NULL
;
3091 __u16 params
, byte_count
;
3094 cifs_dbg(FYI
, "In QPathSymLinkInfo (Unix) for path %s\n", searchName
);
3097 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
3102 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
3104 cifsConvertToUTF16((__le16
*) pSMB
->FileName
,
3105 searchName
, PATH_MAX
, nls_codepage
,
3107 name_len
++; /* trailing null */
3109 } else { /* BB improve the check for buffer overruns BB */
3110 name_len
= strnlen(searchName
, PATH_MAX
);
3111 name_len
++; /* trailing null */
3112 strncpy(pSMB
->FileName
, searchName
, name_len
);
3115 params
= 2 /* level */ + 4 /* rsrvd */ + name_len
/* incl null */ ;
3116 pSMB
->TotalDataCount
= 0;
3117 pSMB
->MaxParameterCount
= cpu_to_le16(2);
3118 pSMB
->MaxDataCount
= cpu_to_le16(CIFSMaxBufSize
);
3119 pSMB
->MaxSetupCount
= 0;
3123 pSMB
->Reserved2
= 0;
3124 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
3125 struct smb_com_transaction2_qpi_req
, InformationLevel
) - 4);
3126 pSMB
->DataCount
= 0;
3127 pSMB
->DataOffset
= 0;
3128 pSMB
->SetupCount
= 1;
3129 pSMB
->Reserved3
= 0;
3130 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION
);
3131 byte_count
= params
+ 1 /* pad */ ;
3132 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
3133 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
3134 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK
);
3135 pSMB
->Reserved4
= 0;
3136 inc_rfc1001_len(pSMB
, byte_count
);
3137 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
3139 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
3140 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
3142 cifs_dbg(FYI
, "Send error in QuerySymLinkInfo = %d\n", rc
);
3144 /* decode response */
3146 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
3147 /* BB also check enough total bytes returned */
3148 if (rc
|| get_bcc(&pSMBr
->hdr
) < 2)
3152 u16 count
= le16_to_cpu(pSMBr
->t2
.DataCount
);
3154 data_start
= ((char *) &pSMBr
->hdr
.Protocol
) +
3155 le16_to_cpu(pSMBr
->t2
.DataOffset
);
3157 if (pSMBr
->hdr
.Flags2
& SMBFLG2_UNICODE
)
3162 /* BB FIXME investigate remapping reserved chars here */
3163 *symlinkinfo
= cifs_strndup_from_utf16(data_start
,
3164 count
, is_unicode
, nls_codepage
);
3169 cifs_buf_release(pSMB
);
3171 goto querySymLinkRetry
;
3176 * Recent Windows versions now create symlinks more frequently
3177 * and they use the "reparse point" mechanism below. We can of course
3178 * do symlinks nicely to Samba and other servers which support the
3179 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3180 * "MF" symlinks optionally, but for recent Windows we really need to
3181 * reenable the code below and fix the cifs_symlink callers to handle this.
3182 * In the interim this code has been moved to its own config option so
3183 * it is not compiled in by default until callers fixed up and more tested.
3186 CIFSSMBQuerySymLink(const unsigned int xid
, struct cifs_tcon
*tcon
,
3187 __u16 fid
, char **symlinkinfo
,
3188 const struct nls_table
*nls_codepage
)
3192 struct smb_com_transaction_ioctl_req
*pSMB
;
3193 struct smb_com_transaction_ioctl_rsp
*pSMBr
;
3195 unsigned int sub_len
;
3197 struct reparse_symlink_data
*reparse_buf
;
3198 struct reparse_posix_data
*posix_buf
;
3199 __u32 data_offset
, data_count
;
3202 cifs_dbg(FYI
, "In Windows reparse style QueryLink for fid %u\n", fid
);
3203 rc
= smb_init(SMB_COM_NT_TRANSACT
, 23, tcon
, (void **) &pSMB
,
3208 pSMB
->TotalParameterCount
= 0 ;
3209 pSMB
->TotalDataCount
= 0;
3210 pSMB
->MaxParameterCount
= cpu_to_le32(2);
3211 /* BB find exact data count max from sess structure BB */
3212 pSMB
->MaxDataCount
= cpu_to_le32(CIFSMaxBufSize
& 0xFFFFFF00);
3213 pSMB
->MaxSetupCount
= 4;
3215 pSMB
->ParameterOffset
= 0;
3216 pSMB
->DataCount
= 0;
3217 pSMB
->DataOffset
= 0;
3218 pSMB
->SetupCount
= 4;
3219 pSMB
->SubCommand
= cpu_to_le16(NT_TRANSACT_IOCTL
);
3220 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
3221 pSMB
->FunctionCode
= cpu_to_le32(FSCTL_GET_REPARSE_POINT
);
3222 pSMB
->IsFsctl
= 1; /* FSCTL */
3223 pSMB
->IsRootFlag
= 0;
3224 pSMB
->Fid
= fid
; /* file handle always le */
3225 pSMB
->ByteCount
= 0;
3227 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
3228 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
3230 cifs_dbg(FYI
, "Send error in QueryReparseLinkInfo = %d\n", rc
);
3234 data_offset
= le32_to_cpu(pSMBr
->DataOffset
);
3235 data_count
= le32_to_cpu(pSMBr
->DataCount
);
3236 if (get_bcc(&pSMBr
->hdr
) < 2 || data_offset
> 512) {
3237 /* BB also check enough total bytes returned */
3238 rc
= -EIO
; /* bad smb */
3241 if (!data_count
|| (data_count
> 2048)) {
3243 cifs_dbg(FYI
, "Invalid return data count on get reparse info ioctl\n");
3246 end_of_smb
= 2 + get_bcc(&pSMBr
->hdr
) + (char *)&pSMBr
->ByteCount
;
3247 reparse_buf
= (struct reparse_symlink_data
*)
3248 ((char *)&pSMBr
->hdr
.Protocol
+ data_offset
);
3249 if ((char *)reparse_buf
>= end_of_smb
) {
3253 if (reparse_buf
->ReparseTag
== cpu_to_le32(IO_REPARSE_TAG_NFS
)) {
3254 cifs_dbg(FYI
, "NFS style reparse tag\n");
3255 posix_buf
= (struct reparse_posix_data
*)reparse_buf
;
3257 if (posix_buf
->InodeType
!= cpu_to_le64(NFS_SPECFILE_LNK
)) {
3258 cifs_dbg(FYI
, "unsupported file type 0x%llx\n",
3259 le64_to_cpu(posix_buf
->InodeType
));
3264 sub_len
= le16_to_cpu(reparse_buf
->ReparseDataLength
);
3265 if (posix_buf
->PathBuffer
+ sub_len
> end_of_smb
) {
3266 cifs_dbg(FYI
, "reparse buf beyond SMB\n");
3270 *symlinkinfo
= cifs_strndup_from_utf16(posix_buf
->PathBuffer
,
3271 sub_len
, is_unicode
, nls_codepage
);
3273 } else if (reparse_buf
->ReparseTag
!=
3274 cpu_to_le32(IO_REPARSE_TAG_SYMLINK
)) {
3279 /* Reparse tag is NTFS symlink */
3280 sub_start
= le16_to_cpu(reparse_buf
->SubstituteNameOffset
) +
3281 reparse_buf
->PathBuffer
;
3282 sub_len
= le16_to_cpu(reparse_buf
->SubstituteNameLength
);
3283 if (sub_start
+ sub_len
> end_of_smb
) {
3284 cifs_dbg(FYI
, "reparse buf beyond SMB\n");
3288 if (pSMBr
->hdr
.Flags2
& SMBFLG2_UNICODE
)
3293 /* BB FIXME investigate remapping reserved chars here */
3294 *symlinkinfo
= cifs_strndup_from_utf16(sub_start
, sub_len
, is_unicode
,
3299 cifs_buf_release(pSMB
);
3302 * Note: On -EAGAIN error only caller can retry on handle based calls
3303 * since file handle passed in no longer valid.
3309 CIFSSMB_set_compression(const unsigned int xid
, struct cifs_tcon
*tcon
,
3314 struct smb_com_transaction_compr_ioctl_req
*pSMB
;
3315 struct smb_com_transaction_ioctl_rsp
*pSMBr
;
3317 cifs_dbg(FYI
, "Set compression for %u\n", fid
);
3318 rc
= smb_init(SMB_COM_NT_TRANSACT
, 23, tcon
, (void **) &pSMB
,
3323 pSMB
->compression_state
= cpu_to_le16(COMPRESSION_FORMAT_DEFAULT
);
3325 pSMB
->TotalParameterCount
= 0;
3326 pSMB
->TotalDataCount
= cpu_to_le32(2);
3327 pSMB
->MaxParameterCount
= 0;
3328 pSMB
->MaxDataCount
= 0;
3329 pSMB
->MaxSetupCount
= 4;
3331 pSMB
->ParameterOffset
= 0;
3332 pSMB
->DataCount
= cpu_to_le32(2);
3334 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req
,
3335 compression_state
) - 4); /* 84 */
3336 pSMB
->SetupCount
= 4;
3337 pSMB
->SubCommand
= cpu_to_le16(NT_TRANSACT_IOCTL
);
3338 pSMB
->ParameterCount
= 0;
3339 pSMB
->FunctionCode
= cpu_to_le32(FSCTL_SET_COMPRESSION
);
3340 pSMB
->IsFsctl
= 1; /* FSCTL */
3341 pSMB
->IsRootFlag
= 0;
3342 pSMB
->Fid
= fid
; /* file handle always le */
3343 /* 3 byte pad, followed by 2 byte compress state */
3344 pSMB
->ByteCount
= cpu_to_le16(5);
3345 inc_rfc1001_len(pSMB
, 5);
3347 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
3348 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
3350 cifs_dbg(FYI
, "Send error in SetCompression = %d\n", rc
);
3352 cifs_buf_release(pSMB
);
3355 * Note: On -EAGAIN error only caller can retry on handle based calls
3356 * since file handle passed in no longer valid.
3362 #ifdef CONFIG_CIFS_POSIX
3364 /*Convert an Access Control Entry from wire format to local POSIX xattr format*/
3365 static void cifs_convert_ace(posix_acl_xattr_entry
*ace
,
3366 struct cifs_posix_ace
*cifs_ace
)
3368 /* u8 cifs fields do not need le conversion */
3369 ace
->e_perm
= cpu_to_le16(cifs_ace
->cifs_e_perm
);
3370 ace
->e_tag
= cpu_to_le16(cifs_ace
->cifs_e_tag
);
3371 ace
->e_id
= cpu_to_le32(le64_to_cpu(cifs_ace
->cifs_uid
));
3373 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3374 ace->e_perm, ace->e_tag, ace->e_id);
3380 /* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
3381 static int cifs_copy_posix_acl(char *trgt
, char *src
, const int buflen
,
3382 const int acl_type
, const int size_of_data_area
)
3387 struct cifs_posix_ace
*pACE
;
3388 struct cifs_posix_acl
*cifs_acl
= (struct cifs_posix_acl
*)src
;
3389 posix_acl_xattr_header
*local_acl
= (posix_acl_xattr_header
*)trgt
;
3391 if (le16_to_cpu(cifs_acl
->version
) != CIFS_ACL_VERSION
)
3394 if (acl_type
& ACL_TYPE_ACCESS
) {
3395 count
= le16_to_cpu(cifs_acl
->access_entry_count
);
3396 pACE
= &cifs_acl
->ace_array
[0];
3397 size
= sizeof(struct cifs_posix_acl
);
3398 size
+= sizeof(struct cifs_posix_ace
) * count
;
3399 /* check if we would go beyond end of SMB */
3400 if (size_of_data_area
< size
) {
3401 cifs_dbg(FYI
, "bad CIFS POSIX ACL size %d vs. %d\n",
3402 size_of_data_area
, size
);
3405 } else if (acl_type
& ACL_TYPE_DEFAULT
) {
3406 count
= le16_to_cpu(cifs_acl
->access_entry_count
);
3407 size
= sizeof(struct cifs_posix_acl
);
3408 size
+= sizeof(struct cifs_posix_ace
) * count
;
3409 /* skip past access ACEs to get to default ACEs */
3410 pACE
= &cifs_acl
->ace_array
[count
];
3411 count
= le16_to_cpu(cifs_acl
->default_entry_count
);
3412 size
+= sizeof(struct cifs_posix_ace
) * count
;
3413 /* check if we would go beyond end of SMB */
3414 if (size_of_data_area
< size
)
3421 size
= posix_acl_xattr_size(count
);
3422 if ((buflen
== 0) || (local_acl
== NULL
)) {
3423 /* used to query ACL EA size */
3424 } else if (size
> buflen
) {
3426 } else /* buffer big enough */ {
3427 local_acl
->a_version
= cpu_to_le32(POSIX_ACL_XATTR_VERSION
);
3428 for (i
= 0; i
< count
; i
++) {
3429 cifs_convert_ace(&local_acl
->a_entries
[i
], pACE
);
3436 static __u16
convert_ace_to_cifs_ace(struct cifs_posix_ace
*cifs_ace
,
3437 const posix_acl_xattr_entry
*local_ace
)
3439 __u16 rc
= 0; /* 0 = ACL converted ok */
3441 cifs_ace
->cifs_e_perm
= le16_to_cpu(local_ace
->e_perm
);
3442 cifs_ace
->cifs_e_tag
= le16_to_cpu(local_ace
->e_tag
);
3443 /* BB is there a better way to handle the large uid? */
3444 if (local_ace
->e_id
== cpu_to_le32(-1)) {
3445 /* Probably no need to le convert -1 on any arch but can not hurt */
3446 cifs_ace
->cifs_uid
= cpu_to_le64(-1);
3448 cifs_ace
->cifs_uid
= cpu_to_le64(le32_to_cpu(local_ace
->e_id
));
3450 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3451 ace->e_perm, ace->e_tag, ace->e_id);
3456 /* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
3457 static __u16
ACL_to_cifs_posix(char *parm_data
, const char *pACL
,
3458 const int buflen
, const int acl_type
)
3461 struct cifs_posix_acl
*cifs_acl
= (struct cifs_posix_acl
*)parm_data
;
3462 posix_acl_xattr_header
*local_acl
= (posix_acl_xattr_header
*)pACL
;
3466 if ((buflen
== 0) || (pACL
== NULL
) || (cifs_acl
== NULL
))
3469 count
= posix_acl_xattr_count((size_t)buflen
);
3470 cifs_dbg(FYI
, "setting acl with %d entries from buf of length %d and version of %d\n",
3471 count
, buflen
, le32_to_cpu(local_acl
->a_version
));
3472 if (le32_to_cpu(local_acl
->a_version
) != 2) {
3473 cifs_dbg(FYI
, "unknown POSIX ACL version %d\n",
3474 le32_to_cpu(local_acl
->a_version
));
3477 cifs_acl
->version
= cpu_to_le16(1);
3478 if (acl_type
== ACL_TYPE_ACCESS
) {
3479 cifs_acl
->access_entry_count
= cpu_to_le16(count
);
3480 cifs_acl
->default_entry_count
= cpu_to_le16(0xFFFF);
3481 } else if (acl_type
== ACL_TYPE_DEFAULT
) {
3482 cifs_acl
->default_entry_count
= cpu_to_le16(count
);
3483 cifs_acl
->access_entry_count
= cpu_to_le16(0xFFFF);
3485 cifs_dbg(FYI
, "unknown ACL type %d\n", acl_type
);
3488 for (i
= 0; i
< count
; i
++) {
3489 rc
= convert_ace_to_cifs_ace(&cifs_acl
->ace_array
[i
],
3490 &local_acl
->a_entries
[i
]);
3492 /* ACE not converted */
3497 rc
= (__u16
)(count
* sizeof(struct cifs_posix_ace
));
3498 rc
+= sizeof(struct cifs_posix_acl
);
3499 /* BB add check to make sure ACL does not overflow SMB */
3505 CIFSSMBGetPosixACL(const unsigned int xid
, struct cifs_tcon
*tcon
,
3506 const unsigned char *searchName
,
3507 char *acl_inf
, const int buflen
, const int acl_type
,
3508 const struct nls_table
*nls_codepage
, int remap
)
3510 /* SMB_QUERY_POSIX_ACL */
3511 TRANSACTION2_QPI_REQ
*pSMB
= NULL
;
3512 TRANSACTION2_QPI_RSP
*pSMBr
= NULL
;
3516 __u16 params
, byte_count
;
3518 cifs_dbg(FYI
, "In GetPosixACL (Unix) for path %s\n", searchName
);
3521 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
3526 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
3528 cifsConvertToUTF16((__le16
*) pSMB
->FileName
,
3529 searchName
, PATH_MAX
, nls_codepage
,
3531 name_len
++; /* trailing null */
3533 pSMB
->FileName
[name_len
] = 0;
3534 pSMB
->FileName
[name_len
+1] = 0;
3535 } else { /* BB improve the check for buffer overruns BB */
3536 name_len
= strnlen(searchName
, PATH_MAX
);
3537 name_len
++; /* trailing null */
3538 strncpy(pSMB
->FileName
, searchName
, name_len
);
3541 params
= 2 /* level */ + 4 /* rsrvd */ + name_len
/* incl null */ ;
3542 pSMB
->TotalDataCount
= 0;
3543 pSMB
->MaxParameterCount
= cpu_to_le16(2);
3544 /* BB find exact max data count below from sess structure BB */
3545 pSMB
->MaxDataCount
= cpu_to_le16(4000);
3546 pSMB
->MaxSetupCount
= 0;
3550 pSMB
->Reserved2
= 0;
3551 pSMB
->ParameterOffset
= cpu_to_le16(
3552 offsetof(struct smb_com_transaction2_qpi_req
,
3553 InformationLevel
) - 4);
3554 pSMB
->DataCount
= 0;
3555 pSMB
->DataOffset
= 0;
3556 pSMB
->SetupCount
= 1;
3557 pSMB
->Reserved3
= 0;
3558 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION
);
3559 byte_count
= params
+ 1 /* pad */ ;
3560 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
3561 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
3562 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_POSIX_ACL
);
3563 pSMB
->Reserved4
= 0;
3564 inc_rfc1001_len(pSMB
, byte_count
);
3565 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
3567 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
3568 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
3569 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_acl_get
);
3571 cifs_dbg(FYI
, "Send error in Query POSIX ACL = %d\n", rc
);
3573 /* decode response */
3575 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
3576 /* BB also check enough total bytes returned */
3577 if (rc
|| get_bcc(&pSMBr
->hdr
) < 2)
3578 rc
= -EIO
; /* bad smb */
3580 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
3581 __u16 count
= le16_to_cpu(pSMBr
->t2
.DataCount
);
3582 rc
= cifs_copy_posix_acl(acl_inf
,
3583 (char *)&pSMBr
->hdr
.Protocol
+data_offset
,
3584 buflen
, acl_type
, count
);
3587 cifs_buf_release(pSMB
);
3594 CIFSSMBSetPosixACL(const unsigned int xid
, struct cifs_tcon
*tcon
,
3595 const unsigned char *fileName
,
3596 const char *local_acl
, const int buflen
,
3598 const struct nls_table
*nls_codepage
, int remap
)
3600 struct smb_com_transaction2_spi_req
*pSMB
= NULL
;
3601 struct smb_com_transaction2_spi_rsp
*pSMBr
= NULL
;
3605 int bytes_returned
= 0;
3606 __u16 params
, byte_count
, data_count
, param_offset
, offset
;
3608 cifs_dbg(FYI
, "In SetPosixACL (Unix) for path %s\n", fileName
);
3610 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
3614 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
3616 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, fileName
,
3617 PATH_MAX
, nls_codepage
, remap
);
3618 name_len
++; /* trailing null */
3620 } else { /* BB improve the check for buffer overruns BB */
3621 name_len
= strnlen(fileName
, PATH_MAX
);
3622 name_len
++; /* trailing null */
3623 strncpy(pSMB
->FileName
, fileName
, name_len
);
3625 params
= 6 + name_len
;
3626 pSMB
->MaxParameterCount
= cpu_to_le16(2);
3627 /* BB find max SMB size from sess */
3628 pSMB
->MaxDataCount
= cpu_to_le16(1000);
3629 pSMB
->MaxSetupCount
= 0;
3633 pSMB
->Reserved2
= 0;
3634 param_offset
= offsetof(struct smb_com_transaction2_spi_req
,
3635 InformationLevel
) - 4;
3636 offset
= param_offset
+ params
;
3637 parm_data
= ((char *) &pSMB
->hdr
.Protocol
) + offset
;
3638 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
3640 /* convert to on the wire format for POSIX ACL */
3641 data_count
= ACL_to_cifs_posix(parm_data
, local_acl
, buflen
, acl_type
);
3643 if (data_count
== 0) {
3645 goto setACLerrorExit
;
3647 pSMB
->DataOffset
= cpu_to_le16(offset
);
3648 pSMB
->SetupCount
= 1;
3649 pSMB
->Reserved3
= 0;
3650 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_PATH_INFORMATION
);
3651 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_POSIX_ACL
);
3652 byte_count
= 3 /* pad */ + params
+ data_count
;
3653 pSMB
->DataCount
= cpu_to_le16(data_count
);
3654 pSMB
->TotalDataCount
= pSMB
->DataCount
;
3655 pSMB
->ParameterCount
= cpu_to_le16(params
);
3656 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
3657 pSMB
->Reserved4
= 0;
3658 inc_rfc1001_len(pSMB
, byte_count
);
3659 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
3660 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
3661 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
3663 cifs_dbg(FYI
, "Set POSIX ACL returned %d\n", rc
);
3666 cifs_buf_release(pSMB
);
3672 /* BB fix tabs in this function FIXME BB */
3674 CIFSGetExtAttr(const unsigned int xid
, struct cifs_tcon
*tcon
,
3675 const int netfid
, __u64
*pExtAttrBits
, __u64
*pMask
)
3678 struct smb_t2_qfi_req
*pSMB
= NULL
;
3679 struct smb_t2_qfi_rsp
*pSMBr
= NULL
;
3681 __u16 params
, byte_count
;
3683 cifs_dbg(FYI
, "In GetExtAttr\n");
3688 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
3693 params
= 2 /* level */ + 2 /* fid */;
3694 pSMB
->t2
.TotalDataCount
= 0;
3695 pSMB
->t2
.MaxParameterCount
= cpu_to_le16(4);
3696 /* BB find exact max data count below from sess structure BB */
3697 pSMB
->t2
.MaxDataCount
= cpu_to_le16(4000);
3698 pSMB
->t2
.MaxSetupCount
= 0;
3699 pSMB
->t2
.Reserved
= 0;
3701 pSMB
->t2
.Timeout
= 0;
3702 pSMB
->t2
.Reserved2
= 0;
3703 pSMB
->t2
.ParameterOffset
= cpu_to_le16(offsetof(struct smb_t2_qfi_req
,
3705 pSMB
->t2
.DataCount
= 0;
3706 pSMB
->t2
.DataOffset
= 0;
3707 pSMB
->t2
.SetupCount
= 1;
3708 pSMB
->t2
.Reserved3
= 0;
3709 pSMB
->t2
.SubCommand
= cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION
);
3710 byte_count
= params
+ 1 /* pad */ ;
3711 pSMB
->t2
.TotalParameterCount
= cpu_to_le16(params
);
3712 pSMB
->t2
.ParameterCount
= pSMB
->t2
.TotalParameterCount
;
3713 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_ATTR_FLAGS
);
3716 inc_rfc1001_len(pSMB
, byte_count
);
3717 pSMB
->t2
.ByteCount
= cpu_to_le16(byte_count
);
3719 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
3720 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
3722 cifs_dbg(FYI
, "error %d in GetExtAttr\n", rc
);
3724 /* decode response */
3725 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
3726 /* BB also check enough total bytes returned */
3727 if (rc
|| get_bcc(&pSMBr
->hdr
) < 2)
3728 /* If rc should we check for EOPNOSUPP and
3729 disable the srvino flag? or in caller? */
3730 rc
= -EIO
; /* bad smb */
3732 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
3733 __u16 count
= le16_to_cpu(pSMBr
->t2
.DataCount
);
3734 struct file_chattr_info
*pfinfo
;
3735 /* BB Do we need a cast or hash here ? */
3737 cifs_dbg(FYI
, "Illegal size ret in GetExtAttr\n");
3741 pfinfo
= (struct file_chattr_info
*)
3742 (data_offset
+ (char *) &pSMBr
->hdr
.Protocol
);
3743 *pExtAttrBits
= le64_to_cpu(pfinfo
->mode
);
3744 *pMask
= le64_to_cpu(pfinfo
->mask
);
3748 cifs_buf_release(pSMB
);
3750 goto GetExtAttrRetry
;
3754 #endif /* CONFIG_POSIX */
3756 #ifdef CONFIG_CIFS_ACL
3758 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3759 * all NT TRANSACTS that we init here have total parm and data under about 400
3760 * bytes (to fit in small cifs buffer size), which is the case so far, it
3761 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3762 * returned setup area) and MaxParameterCount (returned parms size) must be set
3766 smb_init_nttransact(const __u16 sub_command
, const int setup_count
,
3767 const int parm_len
, struct cifs_tcon
*tcon
,
3772 struct smb_com_ntransact_req
*pSMB
;
3774 rc
= small_smb_init(SMB_COM_NT_TRANSACT
, 19 + setup_count
, tcon
,
3778 *ret_buf
= (void *)pSMB
;
3780 pSMB
->TotalParameterCount
= cpu_to_le32(parm_len
);
3781 pSMB
->TotalDataCount
= 0;
3782 pSMB
->MaxDataCount
= cpu_to_le32(CIFSMaxBufSize
& 0xFFFFFF00);
3783 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
3784 pSMB
->DataCount
= pSMB
->TotalDataCount
;
3785 temp_offset
= offsetof(struct smb_com_ntransact_req
, Parms
) +
3786 (setup_count
* 2) - 4 /* for rfc1001 length itself */;
3787 pSMB
->ParameterOffset
= cpu_to_le32(temp_offset
);
3788 pSMB
->DataOffset
= cpu_to_le32(temp_offset
+ parm_len
);
3789 pSMB
->SetupCount
= setup_count
; /* no need to le convert byte fields */
3790 pSMB
->SubCommand
= cpu_to_le16(sub_command
);
3795 validate_ntransact(char *buf
, char **ppparm
, char **ppdata
,
3796 __u32
*pparmlen
, __u32
*pdatalen
)
3799 __u32 data_count
, data_offset
, parm_count
, parm_offset
;
3800 struct smb_com_ntransact_rsp
*pSMBr
;
3809 pSMBr
= (struct smb_com_ntransact_rsp
*)buf
;
3811 bcc
= get_bcc(&pSMBr
->hdr
);
3812 end_of_smb
= 2 /* sizeof byte count */ + bcc
+
3813 (char *)&pSMBr
->ByteCount
;
3815 data_offset
= le32_to_cpu(pSMBr
->DataOffset
);
3816 data_count
= le32_to_cpu(pSMBr
->DataCount
);
3817 parm_offset
= le32_to_cpu(pSMBr
->ParameterOffset
);
3818 parm_count
= le32_to_cpu(pSMBr
->ParameterCount
);
3820 *ppparm
= (char *)&pSMBr
->hdr
.Protocol
+ parm_offset
;
3821 *ppdata
= (char *)&pSMBr
->hdr
.Protocol
+ data_offset
;
3823 /* should we also check that parm and data areas do not overlap? */
3824 if (*ppparm
> end_of_smb
) {
3825 cifs_dbg(FYI
, "parms start after end of smb\n");
3827 } else if (parm_count
+ *ppparm
> end_of_smb
) {
3828 cifs_dbg(FYI
, "parm end after end of smb\n");
3830 } else if (*ppdata
> end_of_smb
) {
3831 cifs_dbg(FYI
, "data starts after end of smb\n");
3833 } else if (data_count
+ *ppdata
> end_of_smb
) {
3834 cifs_dbg(FYI
, "data %p + count %d (%p) past smb end %p start %p\n",
3835 *ppdata
, data_count
, (data_count
+ *ppdata
),
3838 } else if (parm_count
+ data_count
> bcc
) {
3839 cifs_dbg(FYI
, "parm count and data count larger than SMB\n");
3842 *pdatalen
= data_count
;
3843 *pparmlen
= parm_count
;
3847 /* Get Security Descriptor (by handle) from remote server for a file or dir */
3849 CIFSSMBGetCIFSACL(const unsigned int xid
, struct cifs_tcon
*tcon
, __u16 fid
,
3850 struct cifs_ntsd
**acl_inf
, __u32
*pbuflen
)
3854 QUERY_SEC_DESC_REQ
*pSMB
;
3857 cifs_dbg(FYI
, "GetCifsACL\n");
3862 rc
= smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC
, 0,
3863 8 /* parm len */, tcon
, (void **) &pSMB
);
3867 pSMB
->MaxParameterCount
= cpu_to_le32(4);
3868 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3869 pSMB
->MaxSetupCount
= 0;
3870 pSMB
->Fid
= fid
; /* file handle always le */
3871 pSMB
->AclFlags
= cpu_to_le32(CIFS_ACL_OWNER
| CIFS_ACL_GROUP
|
3873 pSMB
->ByteCount
= cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
3874 inc_rfc1001_len(pSMB
, 11);
3875 iov
[0].iov_base
= (char *)pSMB
;
3876 iov
[0].iov_len
= be32_to_cpu(pSMB
->hdr
.smb_buf_length
) + 4;
3878 rc
= SendReceive2(xid
, tcon
->ses
, iov
, 1 /* num iovec */, &buf_type
,
3880 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_acl_get
);
3882 cifs_dbg(FYI
, "Send error in QuerySecDesc = %d\n", rc
);
3883 } else { /* decode response */
3887 struct smb_com_ntransact_rsp
*pSMBr
;
3890 /* validate_nttransact */
3891 rc
= validate_ntransact(iov
[0].iov_base
, (char **)&parm
,
3892 &pdata
, &parm_len
, pbuflen
);
3895 pSMBr
= (struct smb_com_ntransact_rsp
*)iov
[0].iov_base
;
3897 cifs_dbg(FYI
, "smb %p parm %p data %p\n",
3898 pSMBr
, parm
, *acl_inf
);
3900 if (le32_to_cpu(pSMBr
->ParameterCount
) != 4) {
3901 rc
= -EIO
; /* bad smb */
3906 /* BB check that data area is minimum length and as big as acl_len */
3908 acl_len
= le32_to_cpu(*parm
);
3909 if (acl_len
!= *pbuflen
) {
3910 cifs_dbg(VFS
, "acl length %d does not match %d\n",
3912 if (*pbuflen
> acl_len
)
3916 /* check if buffer is big enough for the acl
3917 header followed by the smallest SID */
3918 if ((*pbuflen
< sizeof(struct cifs_ntsd
) + 8) ||
3919 (*pbuflen
>= 64 * 1024)) {
3920 cifs_dbg(VFS
, "bad acl length %d\n", *pbuflen
);
3924 *acl_inf
= kmemdup(pdata
, *pbuflen
, GFP_KERNEL
);
3925 if (*acl_inf
== NULL
) {
3932 free_rsp_buf(buf_type
, iov
[0].iov_base
);
3933 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
3938 CIFSSMBSetCIFSACL(const unsigned int xid
, struct cifs_tcon
*tcon
, __u16 fid
,
3939 struct cifs_ntsd
*pntsd
, __u32 acllen
, int aclflag
)
3941 __u16 byte_count
, param_count
, data_count
, param_offset
, data_offset
;
3943 int bytes_returned
= 0;
3944 SET_SEC_DESC_REQ
*pSMB
= NULL
;
3948 rc
= smb_init(SMB_COM_NT_TRANSACT
, 19, tcon
, (void **) &pSMB
, &pSMBr
);
3952 pSMB
->MaxSetupCount
= 0;
3956 param_offset
= offsetof(struct smb_com_transaction_ssec_req
, Fid
) - 4;
3957 data_count
= acllen
;
3958 data_offset
= param_offset
+ param_count
;
3959 byte_count
= 3 /* pad */ + param_count
;
3961 pSMB
->DataCount
= cpu_to_le32(data_count
);
3962 pSMB
->TotalDataCount
= pSMB
->DataCount
;
3963 pSMB
->MaxParameterCount
= cpu_to_le32(4);
3964 pSMB
->MaxDataCount
= cpu_to_le32(16384);
3965 pSMB
->ParameterCount
= cpu_to_le32(param_count
);
3966 pSMB
->ParameterOffset
= cpu_to_le32(param_offset
);
3967 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
3968 pSMB
->DataOffset
= cpu_to_le32(data_offset
);
3969 pSMB
->SetupCount
= 0;
3970 pSMB
->SubCommand
= cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC
);
3971 pSMB
->ByteCount
= cpu_to_le16(byte_count
+data_count
);
3973 pSMB
->Fid
= fid
; /* file handle always le */
3974 pSMB
->Reserved2
= 0;
3975 pSMB
->AclFlags
= cpu_to_le32(aclflag
);
3977 if (pntsd
&& acllen
) {
3978 memcpy((char *)pSMBr
+ offsetof(struct smb_hdr
, Protocol
) +
3979 data_offset
, pntsd
, acllen
);
3980 inc_rfc1001_len(pSMB
, byte_count
+ data_count
);
3982 inc_rfc1001_len(pSMB
, byte_count
);
3984 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
3985 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
3987 cifs_dbg(FYI
, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3988 bytes_returned
, rc
);
3990 cifs_dbg(FYI
, "Set CIFS ACL returned %d\n", rc
);
3991 cifs_buf_release(pSMB
);
3994 goto setCifsAclRetry
;
3999 #endif /* CONFIG_CIFS_ACL */
4001 /* Legacy Query Path Information call for lookup to old servers such
4004 SMBQueryInformation(const unsigned int xid
, struct cifs_tcon
*tcon
,
4005 const char *search_name
, FILE_ALL_INFO
*data
,
4006 const struct nls_table
*nls_codepage
, int remap
)
4008 QUERY_INFORMATION_REQ
*pSMB
;
4009 QUERY_INFORMATION_RSP
*pSMBr
;
4014 cifs_dbg(FYI
, "In SMBQPath path %s\n", search_name
);
4016 rc
= smb_init(SMB_COM_QUERY_INFORMATION
, 0, tcon
, (void **) &pSMB
,
4021 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
4023 cifsConvertToUTF16((__le16
*) pSMB
->FileName
,
4024 search_name
, PATH_MAX
, nls_codepage
,
4026 name_len
++; /* trailing null */
4029 name_len
= strnlen(search_name
, PATH_MAX
);
4030 name_len
++; /* trailing null */
4031 strncpy(pSMB
->FileName
, search_name
, name_len
);
4033 pSMB
->BufferFormat
= 0x04;
4034 name_len
++; /* account for buffer type byte */
4035 inc_rfc1001_len(pSMB
, (__u16
)name_len
);
4036 pSMB
->ByteCount
= cpu_to_le16(name_len
);
4038 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
4039 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
4041 cifs_dbg(FYI
, "Send error in QueryInfo = %d\n", rc
);
4044 __u32 time
= le32_to_cpu(pSMBr
->last_write_time
);
4046 /* decode response */
4047 /* BB FIXME - add time zone adjustment BB */
4048 memset(data
, 0, sizeof(FILE_ALL_INFO
));
4051 /* decode time fields */
4052 data
->ChangeTime
= cpu_to_le64(cifs_UnixTimeToNT(ts
));
4053 data
->LastWriteTime
= data
->ChangeTime
;
4054 data
->LastAccessTime
= 0;
4055 data
->AllocationSize
=
4056 cpu_to_le64(le32_to_cpu(pSMBr
->size
));
4057 data
->EndOfFile
= data
->AllocationSize
;
4059 cpu_to_le32(le16_to_cpu(pSMBr
->attr
));
4061 rc
= -EIO
; /* bad buffer passed in */
4063 cifs_buf_release(pSMB
);
4072 CIFSSMBQFileInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
4073 u16 netfid
, FILE_ALL_INFO
*pFindData
)
4075 struct smb_t2_qfi_req
*pSMB
= NULL
;
4076 struct smb_t2_qfi_rsp
*pSMBr
= NULL
;
4079 __u16 params
, byte_count
;
4082 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
4087 params
= 2 /* level */ + 2 /* fid */;
4088 pSMB
->t2
.TotalDataCount
= 0;
4089 pSMB
->t2
.MaxParameterCount
= cpu_to_le16(4);
4090 /* BB find exact max data count below from sess structure BB */
4091 pSMB
->t2
.MaxDataCount
= cpu_to_le16(CIFSMaxBufSize
);
4092 pSMB
->t2
.MaxSetupCount
= 0;
4093 pSMB
->t2
.Reserved
= 0;
4095 pSMB
->t2
.Timeout
= 0;
4096 pSMB
->t2
.Reserved2
= 0;
4097 pSMB
->t2
.ParameterOffset
= cpu_to_le16(offsetof(struct smb_t2_qfi_req
,
4099 pSMB
->t2
.DataCount
= 0;
4100 pSMB
->t2
.DataOffset
= 0;
4101 pSMB
->t2
.SetupCount
= 1;
4102 pSMB
->t2
.Reserved3
= 0;
4103 pSMB
->t2
.SubCommand
= cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION
);
4104 byte_count
= params
+ 1 /* pad */ ;
4105 pSMB
->t2
.TotalParameterCount
= cpu_to_le16(params
);
4106 pSMB
->t2
.ParameterCount
= pSMB
->t2
.TotalParameterCount
;
4107 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_FILE_ALL_INFO
);
4110 inc_rfc1001_len(pSMB
, byte_count
);
4111 pSMB
->t2
.ByteCount
= cpu_to_le16(byte_count
);
4113 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
4114 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
4116 cifs_dbg(FYI
, "Send error in QFileInfo = %d", rc
);
4117 } else { /* decode response */
4118 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
4120 if (rc
) /* BB add auto retry on EOPNOTSUPP? */
4122 else if (get_bcc(&pSMBr
->hdr
) < 40)
4123 rc
= -EIO
; /* bad smb */
4124 else if (pFindData
) {
4125 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
4126 memcpy((char *) pFindData
,
4127 (char *) &pSMBr
->hdr
.Protocol
+
4128 data_offset
, sizeof(FILE_ALL_INFO
));
4132 cifs_buf_release(pSMB
);
4134 goto QFileInfoRetry
;
4140 CIFSSMBQPathInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
4141 const char *search_name
, FILE_ALL_INFO
*data
,
4142 int legacy
/* old style infolevel */,
4143 const struct nls_table
*nls_codepage
, int remap
)
4145 /* level 263 SMB_QUERY_FILE_ALL_INFO */
4146 TRANSACTION2_QPI_REQ
*pSMB
= NULL
;
4147 TRANSACTION2_QPI_RSP
*pSMBr
= NULL
;
4151 __u16 params
, byte_count
;
4153 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
4155 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
4160 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
4162 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, search_name
,
4163 PATH_MAX
, nls_codepage
, remap
);
4164 name_len
++; /* trailing null */
4166 } else { /* BB improve the check for buffer overruns BB */
4167 name_len
= strnlen(search_name
, PATH_MAX
);
4168 name_len
++; /* trailing null */
4169 strncpy(pSMB
->FileName
, search_name
, name_len
);
4172 params
= 2 /* level */ + 4 /* reserved */ + name_len
/* includes NUL */;
4173 pSMB
->TotalDataCount
= 0;
4174 pSMB
->MaxParameterCount
= cpu_to_le16(2);
4175 /* BB find exact max SMB PDU from sess structure BB */
4176 pSMB
->MaxDataCount
= cpu_to_le16(4000);
4177 pSMB
->MaxSetupCount
= 0;
4181 pSMB
->Reserved2
= 0;
4182 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
4183 struct smb_com_transaction2_qpi_req
, InformationLevel
) - 4);
4184 pSMB
->DataCount
= 0;
4185 pSMB
->DataOffset
= 0;
4186 pSMB
->SetupCount
= 1;
4187 pSMB
->Reserved3
= 0;
4188 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION
);
4189 byte_count
= params
+ 1 /* pad */ ;
4190 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
4191 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
4193 pSMB
->InformationLevel
= cpu_to_le16(SMB_INFO_STANDARD
);
4195 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_FILE_ALL_INFO
);
4196 pSMB
->Reserved4
= 0;
4197 inc_rfc1001_len(pSMB
, byte_count
);
4198 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
4200 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
4201 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
4203 cifs_dbg(FYI
, "Send error in QPathInfo = %d\n", rc
);
4204 } else { /* decode response */
4205 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
4207 if (rc
) /* BB add auto retry on EOPNOTSUPP? */
4209 else if (!legacy
&& get_bcc(&pSMBr
->hdr
) < 40)
4210 rc
= -EIO
; /* bad smb */
4211 else if (legacy
&& get_bcc(&pSMBr
->hdr
) < 24)
4212 rc
= -EIO
; /* 24 or 26 expected but we do not read
4216 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
4219 * On legacy responses we do not read the last field,
4220 * EAsize, fortunately since it varies by subdialect and
4221 * also note it differs on Set vs Get, ie two bytes or 4
4222 * bytes depending but we don't care here.
4225 size
= sizeof(FILE_INFO_STANDARD
);
4227 size
= sizeof(FILE_ALL_INFO
);
4228 memcpy((char *) data
, (char *) &pSMBr
->hdr
.Protocol
+
4233 cifs_buf_release(pSMB
);
4235 goto QPathInfoRetry
;
4241 CIFSSMBUnixQFileInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
4242 u16 netfid
, FILE_UNIX_BASIC_INFO
*pFindData
)
4244 struct smb_t2_qfi_req
*pSMB
= NULL
;
4245 struct smb_t2_qfi_rsp
*pSMBr
= NULL
;
4248 __u16 params
, byte_count
;
4251 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
4256 params
= 2 /* level */ + 2 /* fid */;
4257 pSMB
->t2
.TotalDataCount
= 0;
4258 pSMB
->t2
.MaxParameterCount
= cpu_to_le16(4);
4259 /* BB find exact max data count below from sess structure BB */
4260 pSMB
->t2
.MaxDataCount
= cpu_to_le16(CIFSMaxBufSize
);
4261 pSMB
->t2
.MaxSetupCount
= 0;
4262 pSMB
->t2
.Reserved
= 0;
4264 pSMB
->t2
.Timeout
= 0;
4265 pSMB
->t2
.Reserved2
= 0;
4266 pSMB
->t2
.ParameterOffset
= cpu_to_le16(offsetof(struct smb_t2_qfi_req
,
4268 pSMB
->t2
.DataCount
= 0;
4269 pSMB
->t2
.DataOffset
= 0;
4270 pSMB
->t2
.SetupCount
= 1;
4271 pSMB
->t2
.Reserved3
= 0;
4272 pSMB
->t2
.SubCommand
= cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION
);
4273 byte_count
= params
+ 1 /* pad */ ;
4274 pSMB
->t2
.TotalParameterCount
= cpu_to_le16(params
);
4275 pSMB
->t2
.ParameterCount
= pSMB
->t2
.TotalParameterCount
;
4276 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC
);
4279 inc_rfc1001_len(pSMB
, byte_count
);
4280 pSMB
->t2
.ByteCount
= cpu_to_le16(byte_count
);
4282 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
4283 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
4285 cifs_dbg(FYI
, "Send error in UnixQFileInfo = %d", rc
);
4286 } else { /* decode response */
4287 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
4289 if (rc
|| get_bcc(&pSMBr
->hdr
) < sizeof(FILE_UNIX_BASIC_INFO
)) {
4290 cifs_dbg(VFS
, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
4291 rc
= -EIO
; /* bad smb */
4293 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
4294 memcpy((char *) pFindData
,
4295 (char *) &pSMBr
->hdr
.Protocol
+
4297 sizeof(FILE_UNIX_BASIC_INFO
));
4301 cifs_buf_release(pSMB
);
4303 goto UnixQFileInfoRetry
;
4309 CIFSSMBUnixQPathInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
4310 const unsigned char *searchName
,
4311 FILE_UNIX_BASIC_INFO
*pFindData
,
4312 const struct nls_table
*nls_codepage
, int remap
)
4314 /* SMB_QUERY_FILE_UNIX_BASIC */
4315 TRANSACTION2_QPI_REQ
*pSMB
= NULL
;
4316 TRANSACTION2_QPI_RSP
*pSMBr
= NULL
;
4318 int bytes_returned
= 0;
4320 __u16 params
, byte_count
;
4322 cifs_dbg(FYI
, "In QPathInfo (Unix) the path %s\n", searchName
);
4324 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
4329 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
4331 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, searchName
,
4332 PATH_MAX
, nls_codepage
, remap
);
4333 name_len
++; /* trailing null */
4335 } else { /* BB improve the check for buffer overruns BB */
4336 name_len
= strnlen(searchName
, PATH_MAX
);
4337 name_len
++; /* trailing null */
4338 strncpy(pSMB
->FileName
, searchName
, name_len
);
4341 params
= 2 /* level */ + 4 /* reserved */ + name_len
/* includes NUL */;
4342 pSMB
->TotalDataCount
= 0;
4343 pSMB
->MaxParameterCount
= cpu_to_le16(2);
4344 /* BB find exact max SMB PDU from sess structure BB */
4345 pSMB
->MaxDataCount
= cpu_to_le16(4000);
4346 pSMB
->MaxSetupCount
= 0;
4350 pSMB
->Reserved2
= 0;
4351 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
4352 struct smb_com_transaction2_qpi_req
, InformationLevel
) - 4);
4353 pSMB
->DataCount
= 0;
4354 pSMB
->DataOffset
= 0;
4355 pSMB
->SetupCount
= 1;
4356 pSMB
->Reserved3
= 0;
4357 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION
);
4358 byte_count
= params
+ 1 /* pad */ ;
4359 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
4360 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
4361 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC
);
4362 pSMB
->Reserved4
= 0;
4363 inc_rfc1001_len(pSMB
, byte_count
);
4364 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
4366 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
4367 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
4369 cifs_dbg(FYI
, "Send error in UnixQPathInfo = %d", rc
);
4370 } else { /* decode response */
4371 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
4373 if (rc
|| get_bcc(&pSMBr
->hdr
) < sizeof(FILE_UNIX_BASIC_INFO
)) {
4374 cifs_dbg(VFS
, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
4375 rc
= -EIO
; /* bad smb */
4377 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
4378 memcpy((char *) pFindData
,
4379 (char *) &pSMBr
->hdr
.Protocol
+
4381 sizeof(FILE_UNIX_BASIC_INFO
));
4384 cifs_buf_release(pSMB
);
4386 goto UnixQPathInfoRetry
;
4391 /* xid, tcon, searchName and codepage are input parms, rest are returned */
4393 CIFSFindFirst(const unsigned int xid
, struct cifs_tcon
*tcon
,
4394 const char *searchName
, struct cifs_sb_info
*cifs_sb
,
4395 __u16
*pnetfid
, __u16 search_flags
,
4396 struct cifs_search_info
*psrch_inf
, bool msearch
)
4398 /* level 257 SMB_ */
4399 TRANSACTION2_FFIRST_REQ
*pSMB
= NULL
;
4400 TRANSACTION2_FFIRST_RSP
*pSMBr
= NULL
;
4401 T2_FFIRST_RSP_PARMS
*parms
;
4403 int bytes_returned
= 0;
4404 int name_len
, remap
;
4405 __u16 params
, byte_count
;
4406 struct nls_table
*nls_codepage
;
4408 cifs_dbg(FYI
, "In FindFirst for %s\n", searchName
);
4411 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
4416 nls_codepage
= cifs_sb
->local_nls
;
4417 remap
= cifs_remap(cifs_sb
);
4419 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
4421 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, searchName
,
4422 PATH_MAX
, nls_codepage
, remap
);
4423 /* We can not add the asterik earlier in case
4424 it got remapped to 0xF03A as if it were part of the
4425 directory name instead of a wildcard */
4428 pSMB
->FileName
[name_len
] = CIFS_DIR_SEP(cifs_sb
);
4429 pSMB
->FileName
[name_len
+1] = 0;
4430 pSMB
->FileName
[name_len
+2] = '*';
4431 pSMB
->FileName
[name_len
+3] = 0;
4432 name_len
+= 4; /* now the trailing null */
4433 /* null terminate just in case */
4434 pSMB
->FileName
[name_len
] = 0;
4435 pSMB
->FileName
[name_len
+1] = 0;
4438 } else { /* BB add check for overrun of SMB buf BB */
4439 name_len
= strnlen(searchName
, PATH_MAX
);
4440 /* BB fix here and in unicode clause above ie
4441 if (name_len > buffersize-header)
4442 free buffer exit; BB */
4443 strncpy(pSMB
->FileName
, searchName
, name_len
);
4445 pSMB
->FileName
[name_len
] = CIFS_DIR_SEP(cifs_sb
);
4446 pSMB
->FileName
[name_len
+1] = '*';
4447 pSMB
->FileName
[name_len
+2] = 0;
4452 params
= 12 + name_len
/* includes null */ ;
4453 pSMB
->TotalDataCount
= 0; /* no EAs */
4454 pSMB
->MaxParameterCount
= cpu_to_le16(10);
4455 pSMB
->MaxDataCount
= cpu_to_le16(CIFSMaxBufSize
& 0xFFFFFF00);
4456 pSMB
->MaxSetupCount
= 0;
4460 pSMB
->Reserved2
= 0;
4461 byte_count
= params
+ 1 /* pad */ ;
4462 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
4463 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
4464 pSMB
->ParameterOffset
= cpu_to_le16(
4465 offsetof(struct smb_com_transaction2_ffirst_req
, SearchAttributes
)
4467 pSMB
->DataCount
= 0;
4468 pSMB
->DataOffset
= 0;
4469 pSMB
->SetupCount
= 1; /* one byte, no need to make endian neutral */
4470 pSMB
->Reserved3
= 0;
4471 pSMB
->SubCommand
= cpu_to_le16(TRANS2_FIND_FIRST
);
4472 pSMB
->SearchAttributes
=
4473 cpu_to_le16(ATTR_READONLY
| ATTR_HIDDEN
| ATTR_SYSTEM
|
4475 pSMB
->SearchCount
= cpu_to_le16(CIFSMaxBufSize
/sizeof(FILE_UNIX_INFO
));
4476 pSMB
->SearchFlags
= cpu_to_le16(search_flags
);
4477 pSMB
->InformationLevel
= cpu_to_le16(psrch_inf
->info_level
);
4479 /* BB what should we set StorageType to? Does it matter? BB */
4480 pSMB
->SearchStorageType
= 0;
4481 inc_rfc1001_len(pSMB
, byte_count
);
4482 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
4484 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
4485 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
4486 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_ffirst
);
4488 if (rc
) {/* BB add logic to retry regular search if Unix search
4489 rejected unexpectedly by server */
4490 /* BB Add code to handle unsupported level rc */
4491 cifs_dbg(FYI
, "Error in FindFirst = %d\n", rc
);
4493 cifs_buf_release(pSMB
);
4495 /* BB eventually could optimize out free and realloc of buf */
4498 goto findFirstRetry
;
4499 } else { /* decode response */
4500 /* BB remember to free buffer if error BB */
4501 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
4505 if (pSMBr
->hdr
.Flags2
& SMBFLG2_UNICODE
)
4506 psrch_inf
->unicode
= true;
4508 psrch_inf
->unicode
= false;
4510 psrch_inf
->ntwrk_buf_start
= (char *)pSMBr
;
4511 psrch_inf
->smallBuf
= 0;
4512 psrch_inf
->srch_entries_start
=
4513 (char *) &pSMBr
->hdr
.Protocol
+
4514 le16_to_cpu(pSMBr
->t2
.DataOffset
);
4515 parms
= (T2_FFIRST_RSP_PARMS
*)((char *) &pSMBr
->hdr
.Protocol
+
4516 le16_to_cpu(pSMBr
->t2
.ParameterOffset
));
4518 if (parms
->EndofSearch
)
4519 psrch_inf
->endOfSearch
= true;
4521 psrch_inf
->endOfSearch
= false;
4523 psrch_inf
->entries_in_buffer
=
4524 le16_to_cpu(parms
->SearchCount
);
4525 psrch_inf
->index_of_last_entry
= 2 /* skip . and .. */ +
4526 psrch_inf
->entries_in_buffer
;
4527 lnoff
= le16_to_cpu(parms
->LastNameOffset
);
4528 if (CIFSMaxBufSize
< lnoff
) {
4529 cifs_dbg(VFS
, "ignoring corrupt resume name\n");
4530 psrch_inf
->last_entry
= NULL
;
4534 psrch_inf
->last_entry
= psrch_inf
->srch_entries_start
+
4538 *pnetfid
= parms
->SearchHandle
;
4540 cifs_buf_release(pSMB
);
4547 int CIFSFindNext(const unsigned int xid
, struct cifs_tcon
*tcon
,
4548 __u16 searchHandle
, __u16 search_flags
,
4549 struct cifs_search_info
*psrch_inf
)
4551 TRANSACTION2_FNEXT_REQ
*pSMB
= NULL
;
4552 TRANSACTION2_FNEXT_RSP
*pSMBr
= NULL
;
4553 T2_FNEXT_RSP_PARMS
*parms
;
4554 char *response_data
;
4557 unsigned int name_len
;
4558 __u16 params
, byte_count
;
4560 cifs_dbg(FYI
, "In FindNext\n");
4562 if (psrch_inf
->endOfSearch
)
4565 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
4570 params
= 14; /* includes 2 bytes of null string, converted to LE below*/
4572 pSMB
->TotalDataCount
= 0; /* no EAs */
4573 pSMB
->MaxParameterCount
= cpu_to_le16(8);
4574 pSMB
->MaxDataCount
= cpu_to_le16(CIFSMaxBufSize
& 0xFFFFFF00);
4575 pSMB
->MaxSetupCount
= 0;
4579 pSMB
->Reserved2
= 0;
4580 pSMB
->ParameterOffset
= cpu_to_le16(
4581 offsetof(struct smb_com_transaction2_fnext_req
,SearchHandle
) - 4);
4582 pSMB
->DataCount
= 0;
4583 pSMB
->DataOffset
= 0;
4584 pSMB
->SetupCount
= 1;
4585 pSMB
->Reserved3
= 0;
4586 pSMB
->SubCommand
= cpu_to_le16(TRANS2_FIND_NEXT
);
4587 pSMB
->SearchHandle
= searchHandle
; /* always kept as le */
4589 cpu_to_le16(CIFSMaxBufSize
/ sizeof(FILE_UNIX_INFO
));
4590 pSMB
->InformationLevel
= cpu_to_le16(psrch_inf
->info_level
);
4591 pSMB
->ResumeKey
= psrch_inf
->resume_key
;
4592 pSMB
->SearchFlags
= cpu_to_le16(search_flags
);
4594 name_len
= psrch_inf
->resume_name_len
;
4596 if (name_len
< PATH_MAX
) {
4597 memcpy(pSMB
->ResumeFileName
, psrch_inf
->presume_name
, name_len
);
4598 byte_count
+= name_len
;
4599 /* 14 byte parm len above enough for 2 byte null terminator */
4600 pSMB
->ResumeFileName
[name_len
] = 0;
4601 pSMB
->ResumeFileName
[name_len
+1] = 0;
4604 goto FNext2_err_exit
;
4606 byte_count
= params
+ 1 /* pad */ ;
4607 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
4608 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
4609 inc_rfc1001_len(pSMB
, byte_count
);
4610 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
4612 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
4613 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
4614 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_fnext
);
4617 psrch_inf
->endOfSearch
= true;
4618 cifs_buf_release(pSMB
);
4619 rc
= 0; /* search probably was closed at end of search*/
4621 cifs_dbg(FYI
, "FindNext returned = %d\n", rc
);
4622 } else { /* decode response */
4623 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
4628 /* BB fixme add lock for file (srch_info) struct here */
4629 if (pSMBr
->hdr
.Flags2
& SMBFLG2_UNICODE
)
4630 psrch_inf
->unicode
= true;
4632 psrch_inf
->unicode
= false;
4633 response_data
= (char *) &pSMBr
->hdr
.Protocol
+
4634 le16_to_cpu(pSMBr
->t2
.ParameterOffset
);
4635 parms
= (T2_FNEXT_RSP_PARMS
*)response_data
;
4636 response_data
= (char *)&pSMBr
->hdr
.Protocol
+
4637 le16_to_cpu(pSMBr
->t2
.DataOffset
);
4638 if (psrch_inf
->smallBuf
)
4639 cifs_small_buf_release(
4640 psrch_inf
->ntwrk_buf_start
);
4642 cifs_buf_release(psrch_inf
->ntwrk_buf_start
);
4643 psrch_inf
->srch_entries_start
= response_data
;
4644 psrch_inf
->ntwrk_buf_start
= (char *)pSMB
;
4645 psrch_inf
->smallBuf
= 0;
4646 if (parms
->EndofSearch
)
4647 psrch_inf
->endOfSearch
= true;
4649 psrch_inf
->endOfSearch
= false;
4650 psrch_inf
->entries_in_buffer
=
4651 le16_to_cpu(parms
->SearchCount
);
4652 psrch_inf
->index_of_last_entry
+=
4653 psrch_inf
->entries_in_buffer
;
4654 lnoff
= le16_to_cpu(parms
->LastNameOffset
);
4655 if (CIFSMaxBufSize
< lnoff
) {
4656 cifs_dbg(VFS
, "ignoring corrupt resume name\n");
4657 psrch_inf
->last_entry
= NULL
;
4660 psrch_inf
->last_entry
=
4661 psrch_inf
->srch_entries_start
+ lnoff
;
4663 /* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4664 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
4666 /* BB fixme add unlock here */
4671 /* BB On error, should we leave previous search buf (and count and
4672 last entry fields) intact or free the previous one? */
4674 /* Note: On -EAGAIN error only caller can retry on handle based calls
4675 since file handle passed in no longer valid */
4678 cifs_buf_release(pSMB
);
4683 CIFSFindClose(const unsigned int xid
, struct cifs_tcon
*tcon
,
4684 const __u16 searchHandle
)
4687 FINDCLOSE_REQ
*pSMB
= NULL
;
4689 cifs_dbg(FYI
, "In CIFSSMBFindClose\n");
4690 rc
= small_smb_init(SMB_COM_FIND_CLOSE2
, 1, tcon
, (void **)&pSMB
);
4692 /* no sense returning error if session restarted
4693 as file handle has been closed */
4699 pSMB
->FileID
= searchHandle
;
4700 pSMB
->ByteCount
= 0;
4701 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) pSMB
, 0);
4703 cifs_dbg(VFS
, "Send error in FindClose = %d\n", rc
);
4705 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_fclose
);
4707 /* Since session is dead, search handle closed on server already */
4715 CIFSGetSrvInodeNumber(const unsigned int xid
, struct cifs_tcon
*tcon
,
4716 const char *search_name
, __u64
*inode_number
,
4717 const struct nls_table
*nls_codepage
, int remap
)
4720 TRANSACTION2_QPI_REQ
*pSMB
= NULL
;
4721 TRANSACTION2_QPI_RSP
*pSMBr
= NULL
;
4722 int name_len
, bytes_returned
;
4723 __u16 params
, byte_count
;
4725 cifs_dbg(FYI
, "In GetSrvInodeNum for %s\n", search_name
);
4729 GetInodeNumberRetry
:
4730 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
4735 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
4737 cifsConvertToUTF16((__le16
*) pSMB
->FileName
,
4738 search_name
, PATH_MAX
, nls_codepage
,
4740 name_len
++; /* trailing null */
4742 } else { /* BB improve the check for buffer overruns BB */
4743 name_len
= strnlen(search_name
, PATH_MAX
);
4744 name_len
++; /* trailing null */
4745 strncpy(pSMB
->FileName
, search_name
, name_len
);
4748 params
= 2 /* level */ + 4 /* rsrvd */ + name_len
/* incl null */ ;
4749 pSMB
->TotalDataCount
= 0;
4750 pSMB
->MaxParameterCount
= cpu_to_le16(2);
4751 /* BB find exact max data count below from sess structure BB */
4752 pSMB
->MaxDataCount
= cpu_to_le16(4000);
4753 pSMB
->MaxSetupCount
= 0;
4757 pSMB
->Reserved2
= 0;
4758 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
4759 struct smb_com_transaction2_qpi_req
, InformationLevel
) - 4);
4760 pSMB
->DataCount
= 0;
4761 pSMB
->DataOffset
= 0;
4762 pSMB
->SetupCount
= 1;
4763 pSMB
->Reserved3
= 0;
4764 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION
);
4765 byte_count
= params
+ 1 /* pad */ ;
4766 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
4767 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
4768 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO
);
4769 pSMB
->Reserved4
= 0;
4770 inc_rfc1001_len(pSMB
, byte_count
);
4771 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
4773 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
4774 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
4776 cifs_dbg(FYI
, "error %d in QueryInternalInfo\n", rc
);
4778 /* decode response */
4779 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
4780 /* BB also check enough total bytes returned */
4781 if (rc
|| get_bcc(&pSMBr
->hdr
) < 2)
4782 /* If rc should we check for EOPNOSUPP and
4783 disable the srvino flag? or in caller? */
4784 rc
= -EIO
; /* bad smb */
4786 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
4787 __u16 count
= le16_to_cpu(pSMBr
->t2
.DataCount
);
4788 struct file_internal_info
*pfinfo
;
4789 /* BB Do we need a cast or hash here ? */
4791 cifs_dbg(FYI
, "Illegal size ret in QryIntrnlInf\n");
4793 goto GetInodeNumOut
;
4795 pfinfo
= (struct file_internal_info
*)
4796 (data_offset
+ (char *) &pSMBr
->hdr
.Protocol
);
4797 *inode_number
= le64_to_cpu(pfinfo
->UniqueId
);
4801 cifs_buf_release(pSMB
);
4803 goto GetInodeNumberRetry
;
4807 /* parses DFS refferal V3 structure
4808 * caller is responsible for freeing target_nodes
4811 * on failure - errno
4814 parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP
*pSMBr
,
4815 unsigned int *num_of_nodes
,
4816 struct dfs_info3_param
**target_nodes
,
4817 const struct nls_table
*nls_codepage
, int remap
,
4818 const char *searchName
)
4823 struct dfs_referral_level_3
*ref
;
4825 if (pSMBr
->hdr
.Flags2
& SMBFLG2_UNICODE
)
4829 *num_of_nodes
= le16_to_cpu(pSMBr
->NumberOfReferrals
);
4831 if (*num_of_nodes
< 1) {
4832 cifs_dbg(VFS
, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
4835 goto parse_DFS_referrals_exit
;
4838 ref
= (struct dfs_referral_level_3
*) &(pSMBr
->referrals
);
4839 if (ref
->VersionNumber
!= cpu_to_le16(3)) {
4840 cifs_dbg(VFS
, "Referrals of V%d version are not supported, should be V3\n",
4841 le16_to_cpu(ref
->VersionNumber
));
4843 goto parse_DFS_referrals_exit
;
4846 /* get the upper boundary of the resp buffer */
4847 data_end
= (char *)(&(pSMBr
->PathConsumed
)) +
4848 le16_to_cpu(pSMBr
->t2
.DataCount
);
4850 cifs_dbg(FYI
, "num_referrals: %d dfs flags: 0x%x ...\n",
4851 *num_of_nodes
, le32_to_cpu(pSMBr
->DFSFlags
));
4853 *target_nodes
= kcalloc(*num_of_nodes
, sizeof(struct dfs_info3_param
),
4855 if (*target_nodes
== NULL
) {
4857 goto parse_DFS_referrals_exit
;
4860 /* collect necessary data from referrals */
4861 for (i
= 0; i
< *num_of_nodes
; i
++) {
4864 struct dfs_info3_param
*node
= (*target_nodes
)+i
;
4866 node
->flags
= le32_to_cpu(pSMBr
->DFSFlags
);
4868 __le16
*tmp
= kmalloc(strlen(searchName
)*2 + 2,
4872 goto parse_DFS_referrals_exit
;
4874 cifsConvertToUTF16((__le16
*) tmp
, searchName
,
4875 PATH_MAX
, nls_codepage
, remap
);
4876 node
->path_consumed
= cifs_utf16_bytes(tmp
,
4877 le16_to_cpu(pSMBr
->PathConsumed
),
4881 node
->path_consumed
= le16_to_cpu(pSMBr
->PathConsumed
);
4883 node
->server_type
= le16_to_cpu(ref
->ServerType
);
4884 node
->ref_flag
= le16_to_cpu(ref
->ReferralEntryFlags
);
4887 temp
= (char *)ref
+ le16_to_cpu(ref
->DfsPathOffset
);
4888 max_len
= data_end
- temp
;
4889 node
->path_name
= cifs_strndup_from_utf16(temp
, max_len
,
4890 is_unicode
, nls_codepage
);
4891 if (!node
->path_name
) {
4893 goto parse_DFS_referrals_exit
;
4896 /* copy link target UNC */
4897 temp
= (char *)ref
+ le16_to_cpu(ref
->NetworkAddressOffset
);
4898 max_len
= data_end
- temp
;
4899 node
->node_name
= cifs_strndup_from_utf16(temp
, max_len
,
4900 is_unicode
, nls_codepage
);
4901 if (!node
->node_name
) {
4903 goto parse_DFS_referrals_exit
;
4909 parse_DFS_referrals_exit
:
4911 free_dfs_info_array(*target_nodes
, *num_of_nodes
);
4912 *target_nodes
= NULL
;
4919 CIFSGetDFSRefer(const unsigned int xid
, struct cifs_ses
*ses
,
4920 const char *search_name
, struct dfs_info3_param
**target_nodes
,
4921 unsigned int *num_of_nodes
,
4922 const struct nls_table
*nls_codepage
, int remap
)
4924 /* TRANS2_GET_DFS_REFERRAL */
4925 TRANSACTION2_GET_DFS_REFER_REQ
*pSMB
= NULL
;
4926 TRANSACTION2_GET_DFS_REFER_RSP
*pSMBr
= NULL
;
4930 __u16 params
, byte_count
;
4932 *target_nodes
= NULL
;
4934 cifs_dbg(FYI
, "In GetDFSRefer the path %s\n", search_name
);
4938 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, NULL
, (void **) &pSMB
,
4943 /* server pointer checked in called function,
4944 but should never be null here anyway */
4945 pSMB
->hdr
.Mid
= get_next_mid(ses
->server
);
4946 pSMB
->hdr
.Tid
= ses
->ipc_tid
;
4947 pSMB
->hdr
.Uid
= ses
->Suid
;
4948 if (ses
->capabilities
& CAP_STATUS32
)
4949 pSMB
->hdr
.Flags2
|= SMBFLG2_ERR_STATUS
;
4950 if (ses
->capabilities
& CAP_DFS
)
4951 pSMB
->hdr
.Flags2
|= SMBFLG2_DFS
;
4953 if (ses
->capabilities
& CAP_UNICODE
) {
4954 pSMB
->hdr
.Flags2
|= SMBFLG2_UNICODE
;
4956 cifsConvertToUTF16((__le16
*) pSMB
->RequestFileName
,
4957 search_name
, PATH_MAX
, nls_codepage
,
4959 name_len
++; /* trailing null */
4961 } else { /* BB improve the check for buffer overruns BB */
4962 name_len
= strnlen(search_name
, PATH_MAX
);
4963 name_len
++; /* trailing null */
4964 strncpy(pSMB
->RequestFileName
, search_name
, name_len
);
4967 if (ses
->server
->sign
)
4968 pSMB
->hdr
.Flags2
|= SMBFLG2_SECURITY_SIGNATURE
;
4970 pSMB
->hdr
.Uid
= ses
->Suid
;
4972 params
= 2 /* level */ + name_len
/*includes null */ ;
4973 pSMB
->TotalDataCount
= 0;
4974 pSMB
->DataCount
= 0;
4975 pSMB
->DataOffset
= 0;
4976 pSMB
->MaxParameterCount
= 0;
4977 /* BB find exact max SMB PDU from sess structure BB */
4978 pSMB
->MaxDataCount
= cpu_to_le16(4000);
4979 pSMB
->MaxSetupCount
= 0;
4983 pSMB
->Reserved2
= 0;
4984 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
4985 struct smb_com_transaction2_get_dfs_refer_req
, MaxReferralLevel
) - 4);
4986 pSMB
->SetupCount
= 1;
4987 pSMB
->Reserved3
= 0;
4988 pSMB
->SubCommand
= cpu_to_le16(TRANS2_GET_DFS_REFERRAL
);
4989 byte_count
= params
+ 3 /* pad */ ;
4990 pSMB
->ParameterCount
= cpu_to_le16(params
);
4991 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
4992 pSMB
->MaxReferralLevel
= cpu_to_le16(3);
4993 inc_rfc1001_len(pSMB
, byte_count
);
4994 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
4996 rc
= SendReceive(xid
, ses
, (struct smb_hdr
*) pSMB
,
4997 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
4999 cifs_dbg(FYI
, "Send error in GetDFSRefer = %d\n", rc
);
5002 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
5004 /* BB Also check if enough total bytes returned? */
5005 if (rc
|| get_bcc(&pSMBr
->hdr
) < 17) {
5006 rc
= -EIO
; /* bad smb */
5010 cifs_dbg(FYI
, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
5011 get_bcc(&pSMBr
->hdr
), le16_to_cpu(pSMBr
->t2
.DataOffset
));
5013 /* parse returned result into more usable form */
5014 rc
= parse_DFS_referrals(pSMBr
, num_of_nodes
,
5015 target_nodes
, nls_codepage
, remap
,
5019 cifs_buf_release(pSMB
);
5027 /* Query File System Info such as free space to old servers such as Win 9x */
5029 SMBOldQFSInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
5030 struct kstatfs
*FSData
)
5032 /* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
5033 TRANSACTION2_QFSI_REQ
*pSMB
= NULL
;
5034 TRANSACTION2_QFSI_RSP
*pSMBr
= NULL
;
5035 FILE_SYSTEM_ALLOC_INFO
*response_data
;
5037 int bytes_returned
= 0;
5038 __u16 params
, byte_count
;
5040 cifs_dbg(FYI
, "OldQFSInfo\n");
5042 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
5047 params
= 2; /* level */
5048 pSMB
->TotalDataCount
= 0;
5049 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5050 pSMB
->MaxDataCount
= cpu_to_le16(1000);
5051 pSMB
->MaxSetupCount
= 0;
5055 pSMB
->Reserved2
= 0;
5056 byte_count
= params
+ 1 /* pad */ ;
5057 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
5058 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
5059 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
5060 struct smb_com_transaction2_qfsi_req
, InformationLevel
) - 4);
5061 pSMB
->DataCount
= 0;
5062 pSMB
->DataOffset
= 0;
5063 pSMB
->SetupCount
= 1;
5064 pSMB
->Reserved3
= 0;
5065 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_FS_INFORMATION
);
5066 pSMB
->InformationLevel
= cpu_to_le16(SMB_INFO_ALLOCATION
);
5067 inc_rfc1001_len(pSMB
, byte_count
);
5068 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5070 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5071 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5073 cifs_dbg(FYI
, "Send error in QFSInfo = %d\n", rc
);
5074 } else { /* decode response */
5075 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
5077 if (rc
|| get_bcc(&pSMBr
->hdr
) < 18)
5078 rc
= -EIO
; /* bad smb */
5080 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
5081 cifs_dbg(FYI
, "qfsinf resp BCC: %d Offset %d\n",
5082 get_bcc(&pSMBr
->hdr
), data_offset
);
5084 response_data
= (FILE_SYSTEM_ALLOC_INFO
*)
5085 (((char *) &pSMBr
->hdr
.Protocol
) + data_offset
);
5087 le16_to_cpu(response_data
->BytesPerSector
) *
5088 le32_to_cpu(response_data
->
5089 SectorsPerAllocationUnit
);
5091 le32_to_cpu(response_data
->TotalAllocationUnits
);
5092 FSData
->f_bfree
= FSData
->f_bavail
=
5093 le32_to_cpu(response_data
->FreeAllocationUnits
);
5094 cifs_dbg(FYI
, "Blocks: %lld Free: %lld Block size %ld\n",
5095 (unsigned long long)FSData
->f_blocks
,
5096 (unsigned long long)FSData
->f_bfree
,
5100 cifs_buf_release(pSMB
);
5103 goto oldQFSInfoRetry
;
5109 CIFSSMBQFSInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
5110 struct kstatfs
*FSData
)
5112 /* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5113 TRANSACTION2_QFSI_REQ
*pSMB
= NULL
;
5114 TRANSACTION2_QFSI_RSP
*pSMBr
= NULL
;
5115 FILE_SYSTEM_INFO
*response_data
;
5117 int bytes_returned
= 0;
5118 __u16 params
, byte_count
;
5120 cifs_dbg(FYI
, "In QFSInfo\n");
5122 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
5127 params
= 2; /* level */
5128 pSMB
->TotalDataCount
= 0;
5129 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5130 pSMB
->MaxDataCount
= cpu_to_le16(1000);
5131 pSMB
->MaxSetupCount
= 0;
5135 pSMB
->Reserved2
= 0;
5136 byte_count
= params
+ 1 /* pad */ ;
5137 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
5138 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
5139 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
5140 struct smb_com_transaction2_qfsi_req
, InformationLevel
) - 4);
5141 pSMB
->DataCount
= 0;
5142 pSMB
->DataOffset
= 0;
5143 pSMB
->SetupCount
= 1;
5144 pSMB
->Reserved3
= 0;
5145 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_FS_INFORMATION
);
5146 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_FS_SIZE_INFO
);
5147 inc_rfc1001_len(pSMB
, byte_count
);
5148 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5150 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5151 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5153 cifs_dbg(FYI
, "Send error in QFSInfo = %d\n", rc
);
5154 } else { /* decode response */
5155 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
5157 if (rc
|| get_bcc(&pSMBr
->hdr
) < 24)
5158 rc
= -EIO
; /* bad smb */
5160 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
5164 *) (((char *) &pSMBr
->hdr
.Protocol
) +
5167 le32_to_cpu(response_data
->BytesPerSector
) *
5168 le32_to_cpu(response_data
->
5169 SectorsPerAllocationUnit
);
5171 le64_to_cpu(response_data
->TotalAllocationUnits
);
5172 FSData
->f_bfree
= FSData
->f_bavail
=
5173 le64_to_cpu(response_data
->FreeAllocationUnits
);
5174 cifs_dbg(FYI
, "Blocks: %lld Free: %lld Block size %ld\n",
5175 (unsigned long long)FSData
->f_blocks
,
5176 (unsigned long long)FSData
->f_bfree
,
5180 cifs_buf_release(pSMB
);
5189 CIFSSMBQFSAttributeInfo(const unsigned int xid
, struct cifs_tcon
*tcon
)
5191 /* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5192 TRANSACTION2_QFSI_REQ
*pSMB
= NULL
;
5193 TRANSACTION2_QFSI_RSP
*pSMBr
= NULL
;
5194 FILE_SYSTEM_ATTRIBUTE_INFO
*response_data
;
5196 int bytes_returned
= 0;
5197 __u16 params
, byte_count
;
5199 cifs_dbg(FYI
, "In QFSAttributeInfo\n");
5201 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
5206 params
= 2; /* level */
5207 pSMB
->TotalDataCount
= 0;
5208 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5209 /* BB find exact max SMB PDU from sess structure BB */
5210 pSMB
->MaxDataCount
= cpu_to_le16(1000);
5211 pSMB
->MaxSetupCount
= 0;
5215 pSMB
->Reserved2
= 0;
5216 byte_count
= params
+ 1 /* pad */ ;
5217 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
5218 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
5219 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
5220 struct smb_com_transaction2_qfsi_req
, InformationLevel
) - 4);
5221 pSMB
->DataCount
= 0;
5222 pSMB
->DataOffset
= 0;
5223 pSMB
->SetupCount
= 1;
5224 pSMB
->Reserved3
= 0;
5225 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_FS_INFORMATION
);
5226 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO
);
5227 inc_rfc1001_len(pSMB
, byte_count
);
5228 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5230 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5231 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5233 cifs_dbg(VFS
, "Send error in QFSAttributeInfo = %d\n", rc
);
5234 } else { /* decode response */
5235 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
5237 if (rc
|| get_bcc(&pSMBr
->hdr
) < 13) {
5238 /* BB also check if enough bytes returned */
5239 rc
= -EIO
; /* bad smb */
5241 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
5243 (FILE_SYSTEM_ATTRIBUTE_INFO
5244 *) (((char *) &pSMBr
->hdr
.Protocol
) +
5246 memcpy(&tcon
->fsAttrInfo
, response_data
,
5247 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO
));
5250 cifs_buf_release(pSMB
);
5253 goto QFSAttributeRetry
;
5259 CIFSSMBQFSDeviceInfo(const unsigned int xid
, struct cifs_tcon
*tcon
)
5261 /* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5262 TRANSACTION2_QFSI_REQ
*pSMB
= NULL
;
5263 TRANSACTION2_QFSI_RSP
*pSMBr
= NULL
;
5264 FILE_SYSTEM_DEVICE_INFO
*response_data
;
5266 int bytes_returned
= 0;
5267 __u16 params
, byte_count
;
5269 cifs_dbg(FYI
, "In QFSDeviceInfo\n");
5271 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
5276 params
= 2; /* level */
5277 pSMB
->TotalDataCount
= 0;
5278 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5279 /* BB find exact max SMB PDU from sess structure BB */
5280 pSMB
->MaxDataCount
= cpu_to_le16(1000);
5281 pSMB
->MaxSetupCount
= 0;
5285 pSMB
->Reserved2
= 0;
5286 byte_count
= params
+ 1 /* pad */ ;
5287 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
5288 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
5289 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
5290 struct smb_com_transaction2_qfsi_req
, InformationLevel
) - 4);
5292 pSMB
->DataCount
= 0;
5293 pSMB
->DataOffset
= 0;
5294 pSMB
->SetupCount
= 1;
5295 pSMB
->Reserved3
= 0;
5296 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_FS_INFORMATION
);
5297 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO
);
5298 inc_rfc1001_len(pSMB
, byte_count
);
5299 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5301 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5302 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5304 cifs_dbg(FYI
, "Send error in QFSDeviceInfo = %d\n", rc
);
5305 } else { /* decode response */
5306 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
5308 if (rc
|| get_bcc(&pSMBr
->hdr
) <
5309 sizeof(FILE_SYSTEM_DEVICE_INFO
))
5310 rc
= -EIO
; /* bad smb */
5312 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
5314 (FILE_SYSTEM_DEVICE_INFO
*)
5315 (((char *) &pSMBr
->hdr
.Protocol
) +
5317 memcpy(&tcon
->fsDevInfo
, response_data
,
5318 sizeof(FILE_SYSTEM_DEVICE_INFO
));
5321 cifs_buf_release(pSMB
);
5324 goto QFSDeviceRetry
;
5330 CIFSSMBQFSUnixInfo(const unsigned int xid
, struct cifs_tcon
*tcon
)
5332 /* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5333 TRANSACTION2_QFSI_REQ
*pSMB
= NULL
;
5334 TRANSACTION2_QFSI_RSP
*pSMBr
= NULL
;
5335 FILE_SYSTEM_UNIX_INFO
*response_data
;
5337 int bytes_returned
= 0;
5338 __u16 params
, byte_count
;
5340 cifs_dbg(FYI
, "In QFSUnixInfo\n");
5342 rc
= smb_init_no_reconnect(SMB_COM_TRANSACTION2
, 15, tcon
,
5343 (void **) &pSMB
, (void **) &pSMBr
);
5347 params
= 2; /* level */
5348 pSMB
->TotalDataCount
= 0;
5349 pSMB
->DataCount
= 0;
5350 pSMB
->DataOffset
= 0;
5351 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5352 /* BB find exact max SMB PDU from sess structure BB */
5353 pSMB
->MaxDataCount
= cpu_to_le16(100);
5354 pSMB
->MaxSetupCount
= 0;
5358 pSMB
->Reserved2
= 0;
5359 byte_count
= params
+ 1 /* pad */ ;
5360 pSMB
->ParameterCount
= cpu_to_le16(params
);
5361 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
5362 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(struct
5363 smb_com_transaction2_qfsi_req
, InformationLevel
) - 4);
5364 pSMB
->SetupCount
= 1;
5365 pSMB
->Reserved3
= 0;
5366 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_FS_INFORMATION
);
5367 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO
);
5368 inc_rfc1001_len(pSMB
, byte_count
);
5369 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5371 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5372 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5374 cifs_dbg(VFS
, "Send error in QFSUnixInfo = %d\n", rc
);
5375 } else { /* decode response */
5376 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
5378 if (rc
|| get_bcc(&pSMBr
->hdr
) < 13) {
5379 rc
= -EIO
; /* bad smb */
5381 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
5383 (FILE_SYSTEM_UNIX_INFO
5384 *) (((char *) &pSMBr
->hdr
.Protocol
) +
5386 memcpy(&tcon
->fsUnixInfo
, response_data
,
5387 sizeof(FILE_SYSTEM_UNIX_INFO
));
5390 cifs_buf_release(pSMB
);
5400 CIFSSMBSetFSUnixInfo(const unsigned int xid
, struct cifs_tcon
*tcon
, __u64 cap
)
5402 /* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5403 TRANSACTION2_SETFSI_REQ
*pSMB
= NULL
;
5404 TRANSACTION2_SETFSI_RSP
*pSMBr
= NULL
;
5406 int bytes_returned
= 0;
5407 __u16 params
, param_offset
, offset
, byte_count
;
5409 cifs_dbg(FYI
, "In SETFSUnixInfo\n");
5411 /* BB switch to small buf init to save memory */
5412 rc
= smb_init_no_reconnect(SMB_COM_TRANSACTION2
, 15, tcon
,
5413 (void **) &pSMB
, (void **) &pSMBr
);
5417 params
= 4; /* 2 bytes zero followed by info level. */
5418 pSMB
->MaxSetupCount
= 0;
5422 pSMB
->Reserved2
= 0;
5423 param_offset
= offsetof(struct smb_com_transaction2_setfsi_req
, FileNum
)
5425 offset
= param_offset
+ params
;
5427 pSMB
->MaxParameterCount
= cpu_to_le16(4);
5428 /* BB find exact max SMB PDU from sess structure BB */
5429 pSMB
->MaxDataCount
= cpu_to_le16(100);
5430 pSMB
->SetupCount
= 1;
5431 pSMB
->Reserved3
= 0;
5432 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_FS_INFORMATION
);
5433 byte_count
= 1 /* pad */ + params
+ 12;
5435 pSMB
->DataCount
= cpu_to_le16(12);
5436 pSMB
->ParameterCount
= cpu_to_le16(params
);
5437 pSMB
->TotalDataCount
= pSMB
->DataCount
;
5438 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
5439 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
5440 pSMB
->DataOffset
= cpu_to_le16(offset
);
5444 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_CIFS_UNIX_INFO
);
5447 pSMB
->ClientUnixMajor
= cpu_to_le16(CIFS_UNIX_MAJOR_VERSION
);
5448 pSMB
->ClientUnixMinor
= cpu_to_le16(CIFS_UNIX_MINOR_VERSION
);
5449 pSMB
->ClientUnixCap
= cpu_to_le64(cap
);
5451 inc_rfc1001_len(pSMB
, byte_count
);
5452 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5454 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5455 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5457 cifs_dbg(VFS
, "Send error in SETFSUnixInfo = %d\n", rc
);
5458 } else { /* decode response */
5459 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
5461 rc
= -EIO
; /* bad smb */
5463 cifs_buf_release(pSMB
);
5466 goto SETFSUnixRetry
;
5474 CIFSSMBQFSPosixInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
5475 struct kstatfs
*FSData
)
5477 /* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5478 TRANSACTION2_QFSI_REQ
*pSMB
= NULL
;
5479 TRANSACTION2_QFSI_RSP
*pSMBr
= NULL
;
5480 FILE_SYSTEM_POSIX_INFO
*response_data
;
5482 int bytes_returned
= 0;
5483 __u16 params
, byte_count
;
5485 cifs_dbg(FYI
, "In QFSPosixInfo\n");
5487 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
5492 params
= 2; /* level */
5493 pSMB
->TotalDataCount
= 0;
5494 pSMB
->DataCount
= 0;
5495 pSMB
->DataOffset
= 0;
5496 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5497 /* BB find exact max SMB PDU from sess structure BB */
5498 pSMB
->MaxDataCount
= cpu_to_le16(100);
5499 pSMB
->MaxSetupCount
= 0;
5503 pSMB
->Reserved2
= 0;
5504 byte_count
= params
+ 1 /* pad */ ;
5505 pSMB
->ParameterCount
= cpu_to_le16(params
);
5506 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
5507 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(struct
5508 smb_com_transaction2_qfsi_req
, InformationLevel
) - 4);
5509 pSMB
->SetupCount
= 1;
5510 pSMB
->Reserved3
= 0;
5511 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_FS_INFORMATION
);
5512 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_POSIX_FS_INFO
);
5513 inc_rfc1001_len(pSMB
, byte_count
);
5514 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5516 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5517 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5519 cifs_dbg(FYI
, "Send error in QFSUnixInfo = %d\n", rc
);
5520 } else { /* decode response */
5521 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
5523 if (rc
|| get_bcc(&pSMBr
->hdr
) < 13) {
5524 rc
= -EIO
; /* bad smb */
5526 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
5528 (FILE_SYSTEM_POSIX_INFO
5529 *) (((char *) &pSMBr
->hdr
.Protocol
) +
5532 le32_to_cpu(response_data
->BlockSize
);
5534 le64_to_cpu(response_data
->TotalBlocks
);
5536 le64_to_cpu(response_data
->BlocksAvail
);
5537 if (response_data
->UserBlocksAvail
== cpu_to_le64(-1)) {
5538 FSData
->f_bavail
= FSData
->f_bfree
;
5541 le64_to_cpu(response_data
->UserBlocksAvail
);
5543 if (response_data
->TotalFileNodes
!= cpu_to_le64(-1))
5545 le64_to_cpu(response_data
->TotalFileNodes
);
5546 if (response_data
->FreeFileNodes
!= cpu_to_le64(-1))
5548 le64_to_cpu(response_data
->FreeFileNodes
);
5551 cifs_buf_release(pSMB
);
5561 * We can not use write of zero bytes trick to set file size due to need for
5562 * large file support. Also note that this SetPathInfo is preferred to
5563 * SetFileInfo based method in next routine which is only needed to work around
5564 * a sharing violation bugin Samba which this routine can run into.
5567 CIFSSMBSetEOF(const unsigned int xid
, struct cifs_tcon
*tcon
,
5568 const char *file_name
, __u64 size
, struct cifs_sb_info
*cifs_sb
,
5569 bool set_allocation
)
5571 struct smb_com_transaction2_spi_req
*pSMB
= NULL
;
5572 struct smb_com_transaction2_spi_rsp
*pSMBr
= NULL
;
5573 struct file_end_of_file_info
*parm_data
;
5576 int bytes_returned
= 0;
5577 int remap
= cifs_remap(cifs_sb
);
5579 __u16 params
, byte_count
, data_count
, param_offset
, offset
;
5581 cifs_dbg(FYI
, "In SetEOF\n");
5583 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
5588 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
5590 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, file_name
,
5591 PATH_MAX
, cifs_sb
->local_nls
, remap
);
5592 name_len
++; /* trailing null */
5594 } else { /* BB improve the check for buffer overruns BB */
5595 name_len
= strnlen(file_name
, PATH_MAX
);
5596 name_len
++; /* trailing null */
5597 strncpy(pSMB
->FileName
, file_name
, name_len
);
5599 params
= 6 + name_len
;
5600 data_count
= sizeof(struct file_end_of_file_info
);
5601 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5602 pSMB
->MaxDataCount
= cpu_to_le16(4100);
5603 pSMB
->MaxSetupCount
= 0;
5607 pSMB
->Reserved2
= 0;
5608 param_offset
= offsetof(struct smb_com_transaction2_spi_req
,
5609 InformationLevel
) - 4;
5610 offset
= param_offset
+ params
;
5611 if (set_allocation
) {
5612 if (tcon
->ses
->capabilities
& CAP_INFOLEVEL_PASSTHRU
)
5613 pSMB
->InformationLevel
=
5614 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2
);
5616 pSMB
->InformationLevel
=
5617 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO
);
5618 } else /* Set File Size */ {
5619 if (tcon
->ses
->capabilities
& CAP_INFOLEVEL_PASSTHRU
)
5620 pSMB
->InformationLevel
=
5621 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2
);
5623 pSMB
->InformationLevel
=
5624 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO
);
5628 (struct file_end_of_file_info
*) (((char *) &pSMB
->hdr
.Protocol
) +
5630 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
5631 pSMB
->DataOffset
= cpu_to_le16(offset
);
5632 pSMB
->SetupCount
= 1;
5633 pSMB
->Reserved3
= 0;
5634 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_PATH_INFORMATION
);
5635 byte_count
= 3 /* pad */ + params
+ data_count
;
5636 pSMB
->DataCount
= cpu_to_le16(data_count
);
5637 pSMB
->TotalDataCount
= pSMB
->DataCount
;
5638 pSMB
->ParameterCount
= cpu_to_le16(params
);
5639 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
5640 pSMB
->Reserved4
= 0;
5641 inc_rfc1001_len(pSMB
, byte_count
);
5642 parm_data
->FileSize
= cpu_to_le64(size
);
5643 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5644 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5645 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5647 cifs_dbg(FYI
, "SetPathInfo (file size) returned %d\n", rc
);
5649 cifs_buf_release(pSMB
);
5658 CIFSSMBSetFileSize(const unsigned int xid
, struct cifs_tcon
*tcon
,
5659 struct cifsFileInfo
*cfile
, __u64 size
, bool set_allocation
)
5661 struct smb_com_transaction2_sfi_req
*pSMB
= NULL
;
5662 struct file_end_of_file_info
*parm_data
;
5664 __u16 params
, param_offset
, offset
, byte_count
, count
;
5666 cifs_dbg(FYI
, "SetFileSize (via SetFileInfo) %lld\n",
5668 rc
= small_smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
);
5673 pSMB
->hdr
.Pid
= cpu_to_le16((__u16
)cfile
->pid
);
5674 pSMB
->hdr
.PidHigh
= cpu_to_le16((__u16
)(cfile
->pid
>> 16));
5677 pSMB
->MaxSetupCount
= 0;
5681 pSMB
->Reserved2
= 0;
5682 param_offset
= offsetof(struct smb_com_transaction2_sfi_req
, Fid
) - 4;
5683 offset
= param_offset
+ params
;
5685 count
= sizeof(struct file_end_of_file_info
);
5686 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5687 /* BB find exact max SMB PDU from sess structure BB */
5688 pSMB
->MaxDataCount
= cpu_to_le16(1000);
5689 pSMB
->SetupCount
= 1;
5690 pSMB
->Reserved3
= 0;
5691 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_FILE_INFORMATION
);
5692 byte_count
= 3 /* pad */ + params
+ count
;
5693 pSMB
->DataCount
= cpu_to_le16(count
);
5694 pSMB
->ParameterCount
= cpu_to_le16(params
);
5695 pSMB
->TotalDataCount
= pSMB
->DataCount
;
5696 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
5697 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
5699 (struct file_end_of_file_info
*) (((char *) &pSMB
->hdr
.Protocol
)
5701 pSMB
->DataOffset
= cpu_to_le16(offset
);
5702 parm_data
->FileSize
= cpu_to_le64(size
);
5703 pSMB
->Fid
= cfile
->fid
.netfid
;
5704 if (set_allocation
) {
5705 if (tcon
->ses
->capabilities
& CAP_INFOLEVEL_PASSTHRU
)
5706 pSMB
->InformationLevel
=
5707 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2
);
5709 pSMB
->InformationLevel
=
5710 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO
);
5711 } else /* Set File Size */ {
5712 if (tcon
->ses
->capabilities
& CAP_INFOLEVEL_PASSTHRU
)
5713 pSMB
->InformationLevel
=
5714 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2
);
5716 pSMB
->InformationLevel
=
5717 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO
);
5719 pSMB
->Reserved4
= 0;
5720 inc_rfc1001_len(pSMB
, byte_count
);
5721 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5722 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) pSMB
, 0);
5724 cifs_dbg(FYI
, "Send error in SetFileInfo (SetFileSize) = %d\n",
5728 /* Note: On -EAGAIN error only caller can retry on handle based calls
5729 since file handle passed in no longer valid */
5734 /* Some legacy servers such as NT4 require that the file times be set on
5735 an open handle, rather than by pathname - this is awkward due to
5736 potential access conflicts on the open, but it is unavoidable for these
5737 old servers since the only other choice is to go from 100 nanosecond DCE
5738 time and resort to the original setpathinfo level which takes the ancient
5739 DOS time format with 2 second granularity */
5741 CIFSSMBSetFileInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
5742 const FILE_BASIC_INFO
*data
, __u16 fid
, __u32 pid_of_opener
)
5744 struct smb_com_transaction2_sfi_req
*pSMB
= NULL
;
5747 __u16 params
, param_offset
, offset
, byte_count
, count
;
5749 cifs_dbg(FYI
, "Set Times (via SetFileInfo)\n");
5750 rc
= small_smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
);
5755 pSMB
->hdr
.Pid
= cpu_to_le16((__u16
)pid_of_opener
);
5756 pSMB
->hdr
.PidHigh
= cpu_to_le16((__u16
)(pid_of_opener
>> 16));
5759 pSMB
->MaxSetupCount
= 0;
5763 pSMB
->Reserved2
= 0;
5764 param_offset
= offsetof(struct smb_com_transaction2_sfi_req
, Fid
) - 4;
5765 offset
= param_offset
+ params
;
5767 data_offset
= (char *)pSMB
+
5768 offsetof(struct smb_hdr
, Protocol
) + offset
;
5770 count
= sizeof(FILE_BASIC_INFO
);
5771 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5772 /* BB find max SMB PDU from sess */
5773 pSMB
->MaxDataCount
= cpu_to_le16(1000);
5774 pSMB
->SetupCount
= 1;
5775 pSMB
->Reserved3
= 0;
5776 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_FILE_INFORMATION
);
5777 byte_count
= 3 /* pad */ + params
+ count
;
5778 pSMB
->DataCount
= cpu_to_le16(count
);
5779 pSMB
->ParameterCount
= cpu_to_le16(params
);
5780 pSMB
->TotalDataCount
= pSMB
->DataCount
;
5781 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
5782 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
5783 pSMB
->DataOffset
= cpu_to_le16(offset
);
5785 if (tcon
->ses
->capabilities
& CAP_INFOLEVEL_PASSTHRU
)
5786 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_FILE_BASIC_INFO2
);
5788 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_FILE_BASIC_INFO
);
5789 pSMB
->Reserved4
= 0;
5790 inc_rfc1001_len(pSMB
, byte_count
);
5791 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5792 memcpy(data_offset
, data
, sizeof(FILE_BASIC_INFO
));
5793 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) pSMB
, 0);
5795 cifs_dbg(FYI
, "Send error in Set Time (SetFileInfo) = %d\n",
5798 /* Note: On -EAGAIN error only caller can retry on handle based calls
5799 since file handle passed in no longer valid */
5805 CIFSSMBSetFileDisposition(const unsigned int xid
, struct cifs_tcon
*tcon
,
5806 bool delete_file
, __u16 fid
, __u32 pid_of_opener
)
5808 struct smb_com_transaction2_sfi_req
*pSMB
= NULL
;
5811 __u16 params
, param_offset
, offset
, byte_count
, count
;
5813 cifs_dbg(FYI
, "Set File Disposition (via SetFileInfo)\n");
5814 rc
= small_smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
);
5819 pSMB
->hdr
.Pid
= cpu_to_le16((__u16
)pid_of_opener
);
5820 pSMB
->hdr
.PidHigh
= cpu_to_le16((__u16
)(pid_of_opener
>> 16));
5823 pSMB
->MaxSetupCount
= 0;
5827 pSMB
->Reserved2
= 0;
5828 param_offset
= offsetof(struct smb_com_transaction2_sfi_req
, Fid
) - 4;
5829 offset
= param_offset
+ params
;
5831 data_offset
= (char *) (&pSMB
->hdr
.Protocol
) + offset
;
5834 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5835 /* BB find max SMB PDU from sess */
5836 pSMB
->MaxDataCount
= cpu_to_le16(1000);
5837 pSMB
->SetupCount
= 1;
5838 pSMB
->Reserved3
= 0;
5839 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_FILE_INFORMATION
);
5840 byte_count
= 3 /* pad */ + params
+ count
;
5841 pSMB
->DataCount
= cpu_to_le16(count
);
5842 pSMB
->ParameterCount
= cpu_to_le16(params
);
5843 pSMB
->TotalDataCount
= pSMB
->DataCount
;
5844 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
5845 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
5846 pSMB
->DataOffset
= cpu_to_le16(offset
);
5848 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO
);
5849 pSMB
->Reserved4
= 0;
5850 inc_rfc1001_len(pSMB
, byte_count
);
5851 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5852 *data_offset
= delete_file
? 1 : 0;
5853 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) pSMB
, 0);
5855 cifs_dbg(FYI
, "Send error in SetFileDisposition = %d\n", rc
);
5861 CIFSSMBSetPathInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
5862 const char *fileName
, const FILE_BASIC_INFO
*data
,
5863 const struct nls_table
*nls_codepage
, int remap
)
5865 TRANSACTION2_SPI_REQ
*pSMB
= NULL
;
5866 TRANSACTION2_SPI_RSP
*pSMBr
= NULL
;
5869 int bytes_returned
= 0;
5871 __u16 params
, param_offset
, offset
, byte_count
, count
;
5873 cifs_dbg(FYI
, "In SetTimes\n");
5876 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
5881 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
5883 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, fileName
,
5884 PATH_MAX
, nls_codepage
, remap
);
5885 name_len
++; /* trailing null */
5887 } else { /* BB improve the check for buffer overruns BB */
5888 name_len
= strnlen(fileName
, PATH_MAX
);
5889 name_len
++; /* trailing null */
5890 strncpy(pSMB
->FileName
, fileName
, name_len
);
5893 params
= 6 + name_len
;
5894 count
= sizeof(FILE_BASIC_INFO
);
5895 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5896 /* BB find max SMB PDU from sess structure BB */
5897 pSMB
->MaxDataCount
= cpu_to_le16(1000);
5898 pSMB
->MaxSetupCount
= 0;
5902 pSMB
->Reserved2
= 0;
5903 param_offset
= offsetof(struct smb_com_transaction2_spi_req
,
5904 InformationLevel
) - 4;
5905 offset
= param_offset
+ params
;
5906 data_offset
= (char *) (&pSMB
->hdr
.Protocol
) + offset
;
5907 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
5908 pSMB
->DataOffset
= cpu_to_le16(offset
);
5909 pSMB
->SetupCount
= 1;
5910 pSMB
->Reserved3
= 0;
5911 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_PATH_INFORMATION
);
5912 byte_count
= 3 /* pad */ + params
+ count
;
5914 pSMB
->DataCount
= cpu_to_le16(count
);
5915 pSMB
->ParameterCount
= cpu_to_le16(params
);
5916 pSMB
->TotalDataCount
= pSMB
->DataCount
;
5917 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
5918 if (tcon
->ses
->capabilities
& CAP_INFOLEVEL_PASSTHRU
)
5919 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_FILE_BASIC_INFO2
);
5921 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_FILE_BASIC_INFO
);
5922 pSMB
->Reserved4
= 0;
5923 inc_rfc1001_len(pSMB
, byte_count
);
5924 memcpy(data_offset
, data
, sizeof(FILE_BASIC_INFO
));
5925 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5926 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5927 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5929 cifs_dbg(FYI
, "SetPathInfo (times) returned %d\n", rc
);
5931 cifs_buf_release(pSMB
);
5939 /* Can not be used to set time stamps yet (due to old DOS time format) */
5940 /* Can be used to set attributes */
5941 #if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5942 handling it anyway and NT4 was what we thought it would be needed for
5943 Do not delete it until we prove whether needed for Win9x though */
5945 CIFSSMBSetAttrLegacy(unsigned int xid
, struct cifs_tcon
*tcon
, char *fileName
,
5946 __u16 dos_attrs
, const struct nls_table
*nls_codepage
)
5948 SETATTR_REQ
*pSMB
= NULL
;
5949 SETATTR_RSP
*pSMBr
= NULL
;
5954 cifs_dbg(FYI
, "In SetAttrLegacy\n");
5957 rc
= smb_init(SMB_COM_SETATTR
, 8, tcon
, (void **) &pSMB
,
5962 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
5964 ConvertToUTF16((__le16
*) pSMB
->fileName
, fileName
,
5965 PATH_MAX
, nls_codepage
);
5966 name_len
++; /* trailing null */
5968 } else { /* BB improve the check for buffer overruns BB */
5969 name_len
= strnlen(fileName
, PATH_MAX
);
5970 name_len
++; /* trailing null */
5971 strncpy(pSMB
->fileName
, fileName
, name_len
);
5973 pSMB
->attr
= cpu_to_le16(dos_attrs
);
5974 pSMB
->BufferFormat
= 0x04;
5975 inc_rfc1001_len(pSMB
, name_len
+ 1);
5976 pSMB
->ByteCount
= cpu_to_le16(name_len
+ 1);
5977 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5978 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5980 cifs_dbg(FYI
, "Error in LegacySetAttr = %d\n", rc
);
5982 cifs_buf_release(pSMB
);
5985 goto SetAttrLgcyRetry
;
5989 #endif /* temporarily unneeded SetAttr legacy function */
5992 cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO
*data_offset
,
5993 const struct cifs_unix_set_info_args
*args
)
5995 u64 uid
= NO_CHANGE_64
, gid
= NO_CHANGE_64
;
5996 u64 mode
= args
->mode
;
5998 if (uid_valid(args
->uid
))
5999 uid
= from_kuid(&init_user_ns
, args
->uid
);
6000 if (gid_valid(args
->gid
))
6001 gid
= from_kgid(&init_user_ns
, args
->gid
);
6004 * Samba server ignores set of file size to zero due to bugs in some
6005 * older clients, but we should be precise - we use SetFileSize to
6006 * set file size and do not want to truncate file size to zero
6007 * accidentally as happened on one Samba server beta by putting
6008 * zero instead of -1 here
6010 data_offset
->EndOfFile
= cpu_to_le64(NO_CHANGE_64
);
6011 data_offset
->NumOfBytes
= cpu_to_le64(NO_CHANGE_64
);
6012 data_offset
->LastStatusChange
= cpu_to_le64(args
->ctime
);
6013 data_offset
->LastAccessTime
= cpu_to_le64(args
->atime
);
6014 data_offset
->LastModificationTime
= cpu_to_le64(args
->mtime
);
6015 data_offset
->Uid
= cpu_to_le64(uid
);
6016 data_offset
->Gid
= cpu_to_le64(gid
);
6017 /* better to leave device as zero when it is */
6018 data_offset
->DevMajor
= cpu_to_le64(MAJOR(args
->device
));
6019 data_offset
->DevMinor
= cpu_to_le64(MINOR(args
->device
));
6020 data_offset
->Permissions
= cpu_to_le64(mode
);
6023 data_offset
->Type
= cpu_to_le32(UNIX_FILE
);
6024 else if (S_ISDIR(mode
))
6025 data_offset
->Type
= cpu_to_le32(UNIX_DIR
);
6026 else if (S_ISLNK(mode
))
6027 data_offset
->Type
= cpu_to_le32(UNIX_SYMLINK
);
6028 else if (S_ISCHR(mode
))
6029 data_offset
->Type
= cpu_to_le32(UNIX_CHARDEV
);
6030 else if (S_ISBLK(mode
))
6031 data_offset
->Type
= cpu_to_le32(UNIX_BLOCKDEV
);
6032 else if (S_ISFIFO(mode
))
6033 data_offset
->Type
= cpu_to_le32(UNIX_FIFO
);
6034 else if (S_ISSOCK(mode
))
6035 data_offset
->Type
= cpu_to_le32(UNIX_SOCKET
);
6039 CIFSSMBUnixSetFileInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
6040 const struct cifs_unix_set_info_args
*args
,
6041 u16 fid
, u32 pid_of_opener
)
6043 struct smb_com_transaction2_sfi_req
*pSMB
= NULL
;
6046 u16 params
, param_offset
, offset
, byte_count
, count
;
6048 cifs_dbg(FYI
, "Set Unix Info (via SetFileInfo)\n");
6049 rc
= small_smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
);
6054 pSMB
->hdr
.Pid
= cpu_to_le16((__u16
)pid_of_opener
);
6055 pSMB
->hdr
.PidHigh
= cpu_to_le16((__u16
)(pid_of_opener
>> 16));
6058 pSMB
->MaxSetupCount
= 0;
6062 pSMB
->Reserved2
= 0;
6063 param_offset
= offsetof(struct smb_com_transaction2_sfi_req
, Fid
) - 4;
6064 offset
= param_offset
+ params
;
6066 data_offset
= (char *)pSMB
+
6067 offsetof(struct smb_hdr
, Protocol
) + offset
;
6069 count
= sizeof(FILE_UNIX_BASIC_INFO
);
6071 pSMB
->MaxParameterCount
= cpu_to_le16(2);
6072 /* BB find max SMB PDU from sess */
6073 pSMB
->MaxDataCount
= cpu_to_le16(1000);
6074 pSMB
->SetupCount
= 1;
6075 pSMB
->Reserved3
= 0;
6076 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_FILE_INFORMATION
);
6077 byte_count
= 3 /* pad */ + params
+ count
;
6078 pSMB
->DataCount
= cpu_to_le16(count
);
6079 pSMB
->ParameterCount
= cpu_to_le16(params
);
6080 pSMB
->TotalDataCount
= pSMB
->DataCount
;
6081 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
6082 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
6083 pSMB
->DataOffset
= cpu_to_le16(offset
);
6085 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_FILE_UNIX_BASIC
);
6086 pSMB
->Reserved4
= 0;
6087 inc_rfc1001_len(pSMB
, byte_count
);
6088 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
6090 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO
*)data_offset
, args
);
6092 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) pSMB
, 0);
6094 cifs_dbg(FYI
, "Send error in Set Time (SetFileInfo) = %d\n",
6097 /* Note: On -EAGAIN error only caller can retry on handle based calls
6098 since file handle passed in no longer valid */
6104 CIFSSMBUnixSetPathInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
6105 const char *file_name
,
6106 const struct cifs_unix_set_info_args
*args
,
6107 const struct nls_table
*nls_codepage
, int remap
)
6109 TRANSACTION2_SPI_REQ
*pSMB
= NULL
;
6110 TRANSACTION2_SPI_RSP
*pSMBr
= NULL
;
6113 int bytes_returned
= 0;
6114 FILE_UNIX_BASIC_INFO
*data_offset
;
6115 __u16 params
, param_offset
, offset
, count
, byte_count
;
6117 cifs_dbg(FYI
, "In SetUID/GID/Mode\n");
6119 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
6124 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
6126 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, file_name
,
6127 PATH_MAX
, nls_codepage
, remap
);
6128 name_len
++; /* trailing null */
6130 } else { /* BB improve the check for buffer overruns BB */
6131 name_len
= strnlen(file_name
, PATH_MAX
);
6132 name_len
++; /* trailing null */
6133 strncpy(pSMB
->FileName
, file_name
, name_len
);
6136 params
= 6 + name_len
;
6137 count
= sizeof(FILE_UNIX_BASIC_INFO
);
6138 pSMB
->MaxParameterCount
= cpu_to_le16(2);
6139 /* BB find max SMB PDU from sess structure BB */
6140 pSMB
->MaxDataCount
= cpu_to_le16(1000);
6141 pSMB
->MaxSetupCount
= 0;
6145 pSMB
->Reserved2
= 0;
6146 param_offset
= offsetof(struct smb_com_transaction2_spi_req
,
6147 InformationLevel
) - 4;
6148 offset
= param_offset
+ params
;
6150 (FILE_UNIX_BASIC_INFO
*) ((char *) &pSMB
->hdr
.Protocol
+
6152 memset(data_offset
, 0, count
);
6153 pSMB
->DataOffset
= cpu_to_le16(offset
);
6154 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
6155 pSMB
->SetupCount
= 1;
6156 pSMB
->Reserved3
= 0;
6157 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_PATH_INFORMATION
);
6158 byte_count
= 3 /* pad */ + params
+ count
;
6159 pSMB
->ParameterCount
= cpu_to_le16(params
);
6160 pSMB
->DataCount
= cpu_to_le16(count
);
6161 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
6162 pSMB
->TotalDataCount
= pSMB
->DataCount
;
6163 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_FILE_UNIX_BASIC
);
6164 pSMB
->Reserved4
= 0;
6165 inc_rfc1001_len(pSMB
, byte_count
);
6167 cifs_fill_unix_set_info(data_offset
, args
);
6169 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
6170 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
6171 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
6173 cifs_dbg(FYI
, "SetPathInfo (perms) returned %d\n", rc
);
6175 cifs_buf_release(pSMB
);
6181 #ifdef CONFIG_CIFS_XATTR
6183 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6184 * function used by listxattr and getxattr type calls. When ea_name is set,
6185 * it looks for that attribute name and stuffs that value into the EAData
6186 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6187 * buffer. In both cases, the return value is either the length of the
6188 * resulting data or a negative error code. If EAData is a NULL pointer then
6189 * the data isn't copied to it, but the length is returned.
6192 CIFSSMBQAllEAs(const unsigned int xid
, struct cifs_tcon
*tcon
,
6193 const unsigned char *searchName
, const unsigned char *ea_name
,
6194 char *EAData
, size_t buf_size
,
6195 const struct nls_table
*nls_codepage
, int remap
)
6197 /* BB assumes one setup word */
6198 TRANSACTION2_QPI_REQ
*pSMB
= NULL
;
6199 TRANSACTION2_QPI_RSP
*pSMBr
= NULL
;
6203 struct fealist
*ea_response_data
;
6204 struct fea
*temp_fea
;
6207 __u16 params
, byte_count
, data_offset
;
6208 unsigned int ea_name_len
= ea_name
? strlen(ea_name
) : 0;
6210 cifs_dbg(FYI
, "In Query All EAs path %s\n", searchName
);
6212 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
6217 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
6219 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, searchName
,
6220 PATH_MAX
, nls_codepage
, remap
);
6221 list_len
++; /* trailing null */
6223 } else { /* BB improve the check for buffer overruns BB */
6224 list_len
= strnlen(searchName
, PATH_MAX
);
6225 list_len
++; /* trailing null */
6226 strncpy(pSMB
->FileName
, searchName
, list_len
);
6229 params
= 2 /* level */ + 4 /* reserved */ + list_len
/* includes NUL */;
6230 pSMB
->TotalDataCount
= 0;
6231 pSMB
->MaxParameterCount
= cpu_to_le16(2);
6232 /* BB find exact max SMB PDU from sess structure BB */
6233 pSMB
->MaxDataCount
= cpu_to_le16(CIFSMaxBufSize
);
6234 pSMB
->MaxSetupCount
= 0;
6238 pSMB
->Reserved2
= 0;
6239 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
6240 struct smb_com_transaction2_qpi_req
, InformationLevel
) - 4);
6241 pSMB
->DataCount
= 0;
6242 pSMB
->DataOffset
= 0;
6243 pSMB
->SetupCount
= 1;
6244 pSMB
->Reserved3
= 0;
6245 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION
);
6246 byte_count
= params
+ 1 /* pad */ ;
6247 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
6248 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
6249 pSMB
->InformationLevel
= cpu_to_le16(SMB_INFO_QUERY_ALL_EAS
);
6250 pSMB
->Reserved4
= 0;
6251 inc_rfc1001_len(pSMB
, byte_count
);
6252 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
6254 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
6255 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
6257 cifs_dbg(FYI
, "Send error in QueryAllEAs = %d\n", rc
);
6262 /* BB also check enough total bytes returned */
6263 /* BB we need to improve the validity checking
6264 of these trans2 responses */
6266 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
6267 if (rc
|| get_bcc(&pSMBr
->hdr
) < 4) {
6268 rc
= -EIO
; /* bad smb */
6272 /* check that length of list is not more than bcc */
6273 /* check that each entry does not go beyond length
6275 /* check that each element of each entry does not
6276 go beyond end of list */
6277 /* validate_trans2_offsets() */
6278 /* BB check if start of smb + data_offset > &bcc+ bcc */
6280 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
6281 ea_response_data
= (struct fealist
*)
6282 (((char *) &pSMBr
->hdr
.Protocol
) + data_offset
);
6284 list_len
= le32_to_cpu(ea_response_data
->list_len
);
6285 cifs_dbg(FYI
, "ea length %d\n", list_len
);
6286 if (list_len
<= 8) {
6287 cifs_dbg(FYI
, "empty EA list returned from server\n");
6288 /* didn't find the named attribute */
6294 /* make sure list_len doesn't go past end of SMB */
6295 end_of_smb
= (char *)pByteArea(&pSMBr
->hdr
) + get_bcc(&pSMBr
->hdr
);
6296 if ((char *)ea_response_data
+ list_len
> end_of_smb
) {
6297 cifs_dbg(FYI
, "EA list appears to go beyond SMB\n");
6302 /* account for ea list len */
6304 temp_fea
= ea_response_data
->list
;
6305 temp_ptr
= (char *)temp_fea
;
6306 while (list_len
> 0) {
6307 unsigned int name_len
;
6312 /* make sure we can read name_len and value_len */
6314 cifs_dbg(FYI
, "EA entry goes beyond length of list\n");
6319 name_len
= temp_fea
->name_len
;
6320 value_len
= le16_to_cpu(temp_fea
->value_len
);
6321 list_len
-= name_len
+ 1 + value_len
;
6323 cifs_dbg(FYI
, "EA entry goes beyond length of list\n");
6329 if (ea_name_len
== name_len
&&
6330 memcmp(ea_name
, temp_ptr
, name_len
) == 0) {
6331 temp_ptr
+= name_len
+ 1;
6335 if ((size_t)value_len
> buf_size
) {
6339 memcpy(EAData
, temp_ptr
, value_len
);
6343 /* account for prefix user. and trailing null */
6344 rc
+= (5 + 1 + name_len
);
6345 if (rc
< (int) buf_size
) {
6346 memcpy(EAData
, "user.", 5);
6348 memcpy(EAData
, temp_ptr
, name_len
);
6350 /* null terminate name */
6353 } else if (buf_size
== 0) {
6354 /* skip copy - calc size only */
6356 /* stop before overrun buffer */
6361 temp_ptr
+= name_len
+ 1 + value_len
;
6362 temp_fea
= (struct fea
*)temp_ptr
;
6365 /* didn't find the named attribute */
6370 cifs_buf_release(pSMB
);
6378 CIFSSMBSetEA(const unsigned int xid
, struct cifs_tcon
*tcon
,
6379 const char *fileName
, const char *ea_name
, const void *ea_value
,
6380 const __u16 ea_value_len
, const struct nls_table
*nls_codepage
,
6383 struct smb_com_transaction2_spi_req
*pSMB
= NULL
;
6384 struct smb_com_transaction2_spi_rsp
*pSMBr
= NULL
;
6385 struct fealist
*parm_data
;
6388 int bytes_returned
= 0;
6389 __u16 params
, param_offset
, byte_count
, offset
, count
;
6391 cifs_dbg(FYI
, "In SetEA\n");
6393 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
6398 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
6400 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, fileName
,
6401 PATH_MAX
, nls_codepage
, remap
);
6402 name_len
++; /* trailing null */
6404 } else { /* BB improve the check for buffer overruns BB */
6405 name_len
= strnlen(fileName
, PATH_MAX
);
6406 name_len
++; /* trailing null */
6407 strncpy(pSMB
->FileName
, fileName
, name_len
);
6410 params
= 6 + name_len
;
6412 /* done calculating parms using name_len of file name,
6413 now use name_len to calculate length of ea name
6414 we are going to create in the inode xattrs */
6415 if (ea_name
== NULL
)
6418 name_len
= strnlen(ea_name
, 255);
6420 count
= sizeof(*parm_data
) + ea_value_len
+ name_len
;
6421 pSMB
->MaxParameterCount
= cpu_to_le16(2);
6422 /* BB find max SMB PDU from sess */
6423 pSMB
->MaxDataCount
= cpu_to_le16(1000);
6424 pSMB
->MaxSetupCount
= 0;
6428 pSMB
->Reserved2
= 0;
6429 param_offset
= offsetof(struct smb_com_transaction2_spi_req
,
6430 InformationLevel
) - 4;
6431 offset
= param_offset
+ params
;
6432 pSMB
->InformationLevel
=
6433 cpu_to_le16(SMB_SET_FILE_EA
);
6435 parm_data
= (void *)pSMB
+ offsetof(struct smb_hdr
, Protocol
) + offset
;
6436 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
6437 pSMB
->DataOffset
= cpu_to_le16(offset
);
6438 pSMB
->SetupCount
= 1;
6439 pSMB
->Reserved3
= 0;
6440 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_PATH_INFORMATION
);
6441 byte_count
= 3 /* pad */ + params
+ count
;
6442 pSMB
->DataCount
= cpu_to_le16(count
);
6443 parm_data
->list_len
= cpu_to_le32(count
);
6444 parm_data
->list
[0].EA_flags
= 0;
6445 /* we checked above that name len is less than 255 */
6446 parm_data
->list
[0].name_len
= (__u8
)name_len
;
6447 /* EA names are always ASCII */
6449 strncpy(parm_data
->list
[0].name
, ea_name
, name_len
);
6450 parm_data
->list
[0].name
[name_len
] = 0;
6451 parm_data
->list
[0].value_len
= cpu_to_le16(ea_value_len
);
6452 /* caller ensures that ea_value_len is less than 64K but
6453 we need to ensure that it fits within the smb */
6455 /*BB add length check to see if it would fit in
6456 negotiated SMB buffer size BB */
6457 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6459 memcpy(parm_data
->list
[0].name
+name_len
+1,
6460 ea_value
, ea_value_len
);
6462 pSMB
->TotalDataCount
= pSMB
->DataCount
;
6463 pSMB
->ParameterCount
= cpu_to_le16(params
);
6464 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
6465 pSMB
->Reserved4
= 0;
6466 inc_rfc1001_len(pSMB
, byte_count
);
6467 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
6468 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
6469 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
6471 cifs_dbg(FYI
, "SetPathInfo (EA) returned %d\n", rc
);
6473 cifs_buf_release(pSMB
);
6482 #ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6484 * Years ago the kernel added a "dnotify" function for Samba server,
6485 * to allow network clients (such as Windows) to display updated
6486 * lists of files in directory listings automatically when
6487 * files are added by one user when another user has the
6488 * same directory open on their desktop. The Linux cifs kernel
6489 * client hooked into the kernel side of this interface for
6490 * the same reason, but ironically when the VFS moved from
6491 * "dnotify" to "inotify" it became harder to plug in Linux
6492 * network file system clients (the most obvious use case
6493 * for notify interfaces is when multiple users can update
6494 * the contents of the same directory - exactly what network
6495 * file systems can do) although the server (Samba) could
6496 * still use it. For the short term we leave the worker
6497 * function ifdeffed out (below) until inotify is fixed
6498 * in the VFS to make it easier to plug in network file
6499 * system clients. If inotify turns out to be permanently
6500 * incompatible for network fs clients, we could instead simply
6501 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6503 int CIFSSMBNotify(const unsigned int xid
, struct cifs_tcon
*tcon
,
6504 const int notify_subdirs
, const __u16 netfid
,
6505 __u32 filter
, struct file
*pfile
, int multishot
,
6506 const struct nls_table
*nls_codepage
)
6509 struct smb_com_transaction_change_notify_req
*pSMB
= NULL
;
6510 struct smb_com_ntransaction_change_notify_rsp
*pSMBr
= NULL
;
6511 struct dir_notify_req
*dnotify_req
;
6514 cifs_dbg(FYI
, "In CIFSSMBNotify for file handle %d\n", (int)netfid
);
6515 rc
= smb_init(SMB_COM_NT_TRANSACT
, 23, tcon
, (void **) &pSMB
,
6520 pSMB
->TotalParameterCount
= 0 ;
6521 pSMB
->TotalDataCount
= 0;
6522 pSMB
->MaxParameterCount
= cpu_to_le32(2);
6523 pSMB
->MaxDataCount
= cpu_to_le32(CIFSMaxBufSize
& 0xFFFFFF00);
6524 pSMB
->MaxSetupCount
= 4;
6526 pSMB
->ParameterOffset
= 0;
6527 pSMB
->DataCount
= 0;
6528 pSMB
->DataOffset
= 0;
6529 pSMB
->SetupCount
= 4; /* single byte does not need le conversion */
6530 pSMB
->SubCommand
= cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE
);
6531 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
6533 pSMB
->WatchTree
= 1; /* one byte - no le conversion needed */
6534 pSMB
->Reserved2
= 0;
6535 pSMB
->CompletionFilter
= cpu_to_le32(filter
);
6536 pSMB
->Fid
= netfid
; /* file handle always le */
6537 pSMB
->ByteCount
= 0;
6539 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
6540 (struct smb_hdr
*)pSMBr
, &bytes_returned
,
6543 cifs_dbg(FYI
, "Error in Notify = %d\n", rc
);
6545 /* Add file to outstanding requests */
6546 /* BB change to kmem cache alloc */
6547 dnotify_req
= kmalloc(
6548 sizeof(struct dir_notify_req
),
6551 dnotify_req
->Pid
= pSMB
->hdr
.Pid
;
6552 dnotify_req
->PidHigh
= pSMB
->hdr
.PidHigh
;
6553 dnotify_req
->Mid
= pSMB
->hdr
.Mid
;
6554 dnotify_req
->Tid
= pSMB
->hdr
.Tid
;
6555 dnotify_req
->Uid
= pSMB
->hdr
.Uid
;
6556 dnotify_req
->netfid
= netfid
;
6557 dnotify_req
->pfile
= pfile
;
6558 dnotify_req
->filter
= filter
;
6559 dnotify_req
->multishot
= multishot
;
6560 spin_lock(&GlobalMid_Lock
);
6561 list_add_tail(&dnotify_req
->lhead
,
6562 &GlobalDnotifyReqList
);
6563 spin_unlock(&GlobalMid_Lock
);
6567 cifs_buf_release(pSMB
);
6570 #endif /* was needed for dnotify, and will be needed for inotify when VFS fix */