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(&cifs_file_list_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(&cifs_file_list_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 wait_event_interruptible_timeout(server
->response_q
,
154 (server
->tcpStatus
!= CifsNeedReconnect
), 10 * HZ
);
156 /* are we still trying to reconnect? */
157 if (server
->tcpStatus
!= CifsNeedReconnect
)
161 * on "soft" mounts we wait once. Hard mounts keep
162 * retrying until process is killed or server comes
166 cifs_dbg(FYI
, "gave up waiting on reconnect in smb_init\n");
171 if (!ses
->need_reconnect
&& !tcon
->need_reconnect
)
174 nls_codepage
= load_nls_default();
177 * need to prevent multiple threads trying to simultaneously
178 * reconnect the same SMB session
180 mutex_lock(&ses
->session_mutex
);
181 rc
= cifs_negotiate_protocol(0, ses
);
182 if (rc
== 0 && ses
->need_reconnect
)
183 rc
= cifs_setup_session(0, ses
, nls_codepage
);
185 /* do we need to reconnect tcon? */
186 if (rc
|| !tcon
->need_reconnect
) {
187 mutex_unlock(&ses
->session_mutex
);
191 cifs_mark_open_files_invalid(tcon
);
192 rc
= CIFSTCon(0, ses
, tcon
->treeName
, tcon
, nls_codepage
);
193 mutex_unlock(&ses
->session_mutex
);
194 cifs_dbg(FYI
, "reconnect tcon rc = %d\n", rc
);
199 atomic_inc(&tconInfoReconnectCount
);
201 /* tell server Unix caps we support */
202 if (ses
->capabilities
& CAP_UNIX
)
203 reset_cifs_unix_caps(0, tcon
, NULL
, NULL
);
206 * Removed call to reopen open files here. It is safer (and faster) to
207 * reopen files one at a time as needed in read and write.
209 * FIXME: what about file locks? don't we need to reclaim them ASAP?
214 * Check if handle based operation so we know whether we can continue
215 * or not without returning to caller to reset file handle
217 switch (smb_command
) {
218 case SMB_COM_READ_ANDX
:
219 case SMB_COM_WRITE_ANDX
:
221 case SMB_COM_FIND_CLOSE2
:
222 case SMB_COM_LOCKING_ANDX
:
226 unload_nls(nls_codepage
);
230 /* Allocate and return pointer to an SMB request buffer, and set basic
231 SMB information in the SMB header. If the return code is zero, this
232 function must have filled in request_buf pointer */
234 small_smb_init(int smb_command
, int wct
, struct cifs_tcon
*tcon
,
239 rc
= cifs_reconnect_tcon(tcon
, smb_command
);
243 *request_buf
= cifs_small_buf_get();
244 if (*request_buf
== NULL
) {
245 /* BB should we add a retry in here if not a writepage? */
249 header_assemble((struct smb_hdr
*) *request_buf
, smb_command
,
253 cifs_stats_inc(&tcon
->num_smbs_sent
);
259 small_smb_init_no_tc(const int smb_command
, const int wct
,
260 struct cifs_ses
*ses
, void **request_buf
)
263 struct smb_hdr
*buffer
;
265 rc
= small_smb_init(smb_command
, wct
, NULL
, request_buf
);
269 buffer
= (struct smb_hdr
*)*request_buf
;
270 buffer
->Mid
= get_next_mid(ses
->server
);
271 if (ses
->capabilities
& CAP_UNICODE
)
272 buffer
->Flags2
|= SMBFLG2_UNICODE
;
273 if (ses
->capabilities
& CAP_STATUS32
)
274 buffer
->Flags2
|= SMBFLG2_ERR_STATUS
;
276 /* uid, tid can stay at zero as set in header assemble */
278 /* BB add support for turning on the signing when
279 this function is used after 1st of session setup requests */
284 /* If the return code is zero, this function must fill in request_buf pointer */
286 __smb_init(int smb_command
, int wct
, struct cifs_tcon
*tcon
,
287 void **request_buf
, void **response_buf
)
289 *request_buf
= cifs_buf_get();
290 if (*request_buf
== NULL
) {
291 /* BB should we add a retry in here if not a writepage? */
294 /* Although the original thought was we needed the response buf for */
295 /* potential retries of smb operations it turns out we can determine */
296 /* from the mid flags when the request buffer can be resent without */
297 /* having to use a second distinct buffer for the response */
299 *response_buf
= *request_buf
;
301 header_assemble((struct smb_hdr
*) *request_buf
, smb_command
, tcon
,
305 cifs_stats_inc(&tcon
->num_smbs_sent
);
310 /* If the return code is zero, this function must fill in request_buf pointer */
312 smb_init(int smb_command
, int wct
, struct cifs_tcon
*tcon
,
313 void **request_buf
, void **response_buf
)
317 rc
= cifs_reconnect_tcon(tcon
, smb_command
);
321 return __smb_init(smb_command
, wct
, tcon
, request_buf
, response_buf
);
325 smb_init_no_reconnect(int smb_command
, int wct
, struct cifs_tcon
*tcon
,
326 void **request_buf
, void **response_buf
)
328 if (tcon
->ses
->need_reconnect
|| tcon
->need_reconnect
)
331 return __smb_init(smb_command
, wct
, tcon
, request_buf
, response_buf
);
334 static int validate_t2(struct smb_t2_rsp
*pSMB
)
336 unsigned int total_size
;
338 /* check for plausible wct */
339 if (pSMB
->hdr
.WordCount
< 10)
342 /* check for parm and data offset going beyond end of smb */
343 if (get_unaligned_le16(&pSMB
->t2_rsp
.ParameterOffset
) > 1024 ||
344 get_unaligned_le16(&pSMB
->t2_rsp
.DataOffset
) > 1024)
347 total_size
= get_unaligned_le16(&pSMB
->t2_rsp
.ParameterCount
);
348 if (total_size
>= 512)
351 /* check that bcc is at least as big as parms + data, and that it is
352 * less than negotiated smb buffer
354 total_size
+= get_unaligned_le16(&pSMB
->t2_rsp
.DataCount
);
355 if (total_size
> get_bcc(&pSMB
->hdr
) ||
356 total_size
>= CIFSMaxBufSize
+ MAX_CIFS_HDR_SIZE
)
361 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB
,
362 sizeof(struct smb_t2_rsp
) + 16);
367 decode_ext_sec_blob(struct cifs_ses
*ses
, NEGOTIATE_RSP
*pSMBr
)
371 char *guid
= pSMBr
->u
.extended_response
.GUID
;
372 struct TCP_Server_Info
*server
= ses
->server
;
374 count
= get_bcc(&pSMBr
->hdr
);
375 if (count
< SMB1_CLIENT_GUID_SIZE
)
378 spin_lock(&cifs_tcp_ses_lock
);
379 if (server
->srv_count
> 1) {
380 spin_unlock(&cifs_tcp_ses_lock
);
381 if (memcmp(server
->server_GUID
, guid
, SMB1_CLIENT_GUID_SIZE
) != 0) {
382 cifs_dbg(FYI
, "server UID changed\n");
383 memcpy(server
->server_GUID
, guid
, SMB1_CLIENT_GUID_SIZE
);
386 spin_unlock(&cifs_tcp_ses_lock
);
387 memcpy(server
->server_GUID
, guid
, SMB1_CLIENT_GUID_SIZE
);
390 if (count
== SMB1_CLIENT_GUID_SIZE
) {
391 server
->sec_ntlmssp
= true;
393 count
-= SMB1_CLIENT_GUID_SIZE
;
394 rc
= decode_negTokenInit(
395 pSMBr
->u
.extended_response
.SecurityBlob
, count
, server
);
404 cifs_enable_signing(struct TCP_Server_Info
*server
, bool mnt_sign_required
)
406 bool srv_sign_required
= server
->sec_mode
& server
->vals
->signing_required
;
407 bool srv_sign_enabled
= server
->sec_mode
& server
->vals
->signing_enabled
;
408 bool mnt_sign_enabled
= global_secflags
& CIFSSEC_MAY_SIGN
;
411 * Is signing required by mnt options? If not then check
412 * global_secflags to see if it is there.
414 if (!mnt_sign_required
)
415 mnt_sign_required
= ((global_secflags
& CIFSSEC_MUST_SIGN
) ==
419 * If signing is required then it's automatically enabled too,
420 * otherwise, check to see if the secflags allow it.
422 mnt_sign_enabled
= mnt_sign_required
? mnt_sign_required
:
423 (global_secflags
& CIFSSEC_MAY_SIGN
);
425 /* If server requires signing, does client allow it? */
426 if (srv_sign_required
) {
427 if (!mnt_sign_enabled
) {
428 cifs_dbg(VFS
, "Server requires signing, but it's disabled in SecurityFlags!");
434 /* If client requires signing, does server allow it? */
435 if (mnt_sign_required
) {
436 if (!srv_sign_enabled
) {
437 cifs_dbg(VFS
, "Server does not support signing!");
446 #ifdef CONFIG_CIFS_WEAK_PW_HASH
448 decode_lanman_negprot_rsp(struct TCP_Server_Info
*server
, NEGOTIATE_RSP
*pSMBr
)
451 struct lanman_neg_rsp
*rsp
= (struct lanman_neg_rsp
*)pSMBr
;
453 if (server
->dialect
!= LANMAN_PROT
&& server
->dialect
!= LANMAN2_PROT
)
456 server
->sec_mode
= le16_to_cpu(rsp
->SecurityMode
);
457 server
->maxReq
= min_t(unsigned int,
458 le16_to_cpu(rsp
->MaxMpxCount
),
460 set_credits(server
, server
->maxReq
);
461 server
->maxBuf
= le16_to_cpu(rsp
->MaxBufSize
);
462 /* even though we do not use raw we might as well set this
463 accurately, in case we ever find a need for it */
464 if ((le16_to_cpu(rsp
->RawMode
) & RAW_ENABLE
) == RAW_ENABLE
) {
465 server
->max_rw
= 0xFF00;
466 server
->capabilities
= CAP_MPX_MODE
| CAP_RAW_MODE
;
468 server
->max_rw
= 0;/* do not need to use raw anyway */
469 server
->capabilities
= CAP_MPX_MODE
;
471 tmp
= (__s16
)le16_to_cpu(rsp
->ServerTimeZone
);
473 /* OS/2 often does not set timezone therefore
474 * we must use server time to calc time zone.
475 * Could deviate slightly from the right zone.
476 * Smallest defined timezone difference is 15 minutes
477 * (i.e. Nepal). Rounding up/down is done to match
480 int val
, seconds
, remain
, result
;
481 struct timespec ts
, utc
;
483 ts
= cnvrtDosUnixTm(rsp
->SrvTime
.Date
,
484 rsp
->SrvTime
.Time
, 0);
485 cifs_dbg(FYI
, "SrvTime %d sec since 1970 (utc: %d) diff: %d\n",
486 (int)ts
.tv_sec
, (int)utc
.tv_sec
,
487 (int)(utc
.tv_sec
- ts
.tv_sec
));
488 val
= (int)(utc
.tv_sec
- ts
.tv_sec
);
490 result
= (seconds
/ MIN_TZ_ADJ
) * MIN_TZ_ADJ
;
491 remain
= seconds
% MIN_TZ_ADJ
;
492 if (remain
>= (MIN_TZ_ADJ
/ 2))
493 result
+= MIN_TZ_ADJ
;
496 server
->timeAdj
= result
;
498 server
->timeAdj
= (int)tmp
;
499 server
->timeAdj
*= 60; /* also in seconds */
501 cifs_dbg(FYI
, "server->timeAdj: %d seconds\n", server
->timeAdj
);
504 /* BB get server time for time conversions and add
505 code to use it and timezone since this is not UTC */
507 if (rsp
->EncryptionKeyLength
==
508 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE
)) {
509 memcpy(server
->cryptkey
, rsp
->EncryptionKey
,
510 CIFS_CRYPTO_KEY_SIZE
);
511 } else if (server
->sec_mode
& SECMODE_PW_ENCRYPT
) {
512 return -EIO
; /* need cryptkey unless plain text */
515 cifs_dbg(FYI
, "LANMAN negotiated\n");
520 decode_lanman_negprot_rsp(struct TCP_Server_Info
*server
, NEGOTIATE_RSP
*pSMBr
)
522 cifs_dbg(VFS
, "mount failed, cifs module not built with CIFS_WEAK_PW_HASH support\n");
528 should_set_ext_sec_flag(enum securityEnum sectype
)
535 if (global_secflags
&
536 (CIFSSEC_MAY_KRB5
| CIFSSEC_MAY_NTLMSSP
))
545 CIFSSMBNegotiate(const unsigned int xid
, struct cifs_ses
*ses
)
548 NEGOTIATE_RSP
*pSMBr
;
552 struct TCP_Server_Info
*server
= ses
->server
;
556 WARN(1, "%s: server is NULL!\n", __func__
);
560 rc
= smb_init(SMB_COM_NEGOTIATE
, 0, NULL
/* no tcon yet */ ,
561 (void **) &pSMB
, (void **) &pSMBr
);
565 pSMB
->hdr
.Mid
= get_next_mid(server
);
566 pSMB
->hdr
.Flags2
|= (SMBFLG2_UNICODE
| SMBFLG2_ERR_STATUS
);
568 if (should_set_ext_sec_flag(ses
->sectype
)) {
569 cifs_dbg(FYI
, "Requesting extended security.");
570 pSMB
->hdr
.Flags2
|= SMBFLG2_EXT_SEC
;
574 for (i
= 0; i
< CIFS_NUM_PROT
; i
++) {
575 strncpy(pSMB
->DialectsArray
+count
, protocols
[i
].name
, 16);
576 count
+= strlen(protocols
[i
].name
) + 1;
577 /* null at end of source and target buffers anyway */
579 inc_rfc1001_len(pSMB
, count
);
580 pSMB
->ByteCount
= cpu_to_le16(count
);
582 rc
= SendReceive(xid
, ses
, (struct smb_hdr
*) pSMB
,
583 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
587 server
->dialect
= le16_to_cpu(pSMBr
->DialectIndex
);
588 cifs_dbg(FYI
, "Dialect: %d\n", server
->dialect
);
589 /* Check wct = 1 error case */
590 if ((pSMBr
->hdr
.WordCount
< 13) || (server
->dialect
== BAD_PROT
)) {
591 /* core returns wct = 1, but we do not ask for core - otherwise
592 small wct just comes when dialect index is -1 indicating we
593 could not negotiate a common dialect */
596 } else if (pSMBr
->hdr
.WordCount
== 13) {
597 server
->negflavor
= CIFS_NEGFLAVOR_LANMAN
;
598 rc
= decode_lanman_negprot_rsp(server
, pSMBr
);
600 } else if (pSMBr
->hdr
.WordCount
!= 17) {
605 /* else wct == 17, NTLM or better */
607 server
->sec_mode
= pSMBr
->SecurityMode
;
608 if ((server
->sec_mode
& SECMODE_USER
) == 0)
609 cifs_dbg(FYI
, "share mode security\n");
611 /* one byte, so no need to convert this or EncryptionKeyLen from
613 server
->maxReq
= min_t(unsigned int, le16_to_cpu(pSMBr
->MaxMpxCount
),
615 set_credits(server
, server
->maxReq
);
616 /* probably no need to store and check maxvcs */
617 server
->maxBuf
= le32_to_cpu(pSMBr
->MaxBufferSize
);
618 server
->max_rw
= le32_to_cpu(pSMBr
->MaxRawSize
);
619 cifs_dbg(NOISY
, "Max buf = %d\n", ses
->server
->maxBuf
);
620 server
->capabilities
= le32_to_cpu(pSMBr
->Capabilities
);
621 server
->timeAdj
= (int)(__s16
)le16_to_cpu(pSMBr
->ServerTimeZone
);
622 server
->timeAdj
*= 60;
624 if (pSMBr
->EncryptionKeyLength
== CIFS_CRYPTO_KEY_SIZE
) {
625 server
->negflavor
= CIFS_NEGFLAVOR_UNENCAP
;
626 memcpy(ses
->server
->cryptkey
, pSMBr
->u
.EncryptionKey
,
627 CIFS_CRYPTO_KEY_SIZE
);
628 } else if (pSMBr
->hdr
.Flags2
& SMBFLG2_EXT_SEC
||
629 server
->capabilities
& CAP_EXTENDED_SECURITY
) {
630 server
->negflavor
= CIFS_NEGFLAVOR_EXTENDED
;
631 rc
= decode_ext_sec_blob(ses
, pSMBr
);
632 } else if (server
->sec_mode
& SECMODE_PW_ENCRYPT
) {
633 rc
= -EIO
; /* no crypt key only if plain text pwd */
635 server
->negflavor
= CIFS_NEGFLAVOR_UNENCAP
;
636 server
->capabilities
&= ~CAP_EXTENDED_SECURITY
;
641 rc
= cifs_enable_signing(server
, ses
->sign
);
643 cifs_buf_release(pSMB
);
645 cifs_dbg(FYI
, "negprot rc %d\n", rc
);
650 CIFSSMBTDis(const unsigned int xid
, struct cifs_tcon
*tcon
)
652 struct smb_hdr
*smb_buffer
;
655 cifs_dbg(FYI
, "In tree disconnect\n");
657 /* BB: do we need to check this? These should never be NULL. */
658 if ((tcon
->ses
== NULL
) || (tcon
->ses
->server
== NULL
))
662 * No need to return error on this operation if tid invalidated and
663 * closed on server already e.g. due to tcp session crashing. Also,
664 * the tcon is no longer on the list, so no need to take lock before
667 if ((tcon
->need_reconnect
) || (tcon
->ses
->need_reconnect
))
670 rc
= small_smb_init(SMB_COM_TREE_DISCONNECT
, 0, tcon
,
671 (void **)&smb_buffer
);
675 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *)smb_buffer
, 0);
677 cifs_dbg(FYI
, "Tree disconnect failed %d\n", rc
);
679 /* No need to return error on this operation if tid invalidated and
680 closed on server already e.g. due to tcp session crashing */
688 * This is a no-op for now. We're not really interested in the reply, but
689 * rather in the fact that the server sent one and that server->lstrp
692 * FIXME: maybe we should consider checking that the reply matches request?
695 cifs_echo_callback(struct mid_q_entry
*mid
)
697 struct TCP_Server_Info
*server
= mid
->callback_data
;
699 mutex_lock(&server
->srv_mutex
);
700 DeleteMidQEntry(mid
);
701 mutex_unlock(&server
->srv_mutex
);
702 add_credits(server
, 1, CIFS_ECHO_OP
);
706 CIFSSMBEcho(struct TCP_Server_Info
*server
)
711 struct smb_rqst rqst
= { .rq_iov
= &iov
,
714 cifs_dbg(FYI
, "In echo request\n");
716 rc
= small_smb_init(SMB_COM_ECHO
, 0, NULL
, (void **)&smb
);
720 /* set up echo request */
721 smb
->hdr
.Tid
= 0xffff;
722 smb
->hdr
.WordCount
= 1;
723 put_unaligned_le16(1, &smb
->EchoCount
);
724 put_bcc(1, &smb
->hdr
);
726 inc_rfc1001_len(smb
, 3);
728 iov
.iov_len
= be32_to_cpu(smb
->hdr
.smb_buf_length
) + 4;
730 rc
= cifs_call_async(server
, &rqst
, NULL
, cifs_echo_callback
,
731 server
, CIFS_ASYNC_OP
| CIFS_ECHO_OP
);
733 cifs_dbg(FYI
, "Echo request failed: %d\n", rc
);
735 cifs_small_buf_release(smb
);
741 CIFSSMBLogoff(const unsigned int xid
, struct cifs_ses
*ses
)
743 LOGOFF_ANDX_REQ
*pSMB
;
746 cifs_dbg(FYI
, "In SMBLogoff for session disconnect\n");
749 * BB: do we need to check validity of ses and server? They should
750 * always be valid since we have an active reference. If not, that
751 * should probably be a BUG()
753 if (!ses
|| !ses
->server
)
756 mutex_lock(&ses
->session_mutex
);
757 if (ses
->need_reconnect
)
758 goto session_already_dead
; /* no need to send SMBlogoff if uid
759 already closed due to reconnect */
760 rc
= small_smb_init(SMB_COM_LOGOFF_ANDX
, 2, NULL
, (void **)&pSMB
);
762 mutex_unlock(&ses
->session_mutex
);
766 pSMB
->hdr
.Mid
= get_next_mid(ses
->server
);
768 if (ses
->server
->sign
)
769 pSMB
->hdr
.Flags2
|= SMBFLG2_SECURITY_SIGNATURE
;
771 pSMB
->hdr
.Uid
= ses
->Suid
;
773 pSMB
->AndXCommand
= 0xFF;
774 rc
= SendReceiveNoRsp(xid
, ses
, (char *) pSMB
, 0);
775 session_already_dead
:
776 mutex_unlock(&ses
->session_mutex
);
778 /* if session dead then we do not need to do ulogoff,
779 since server closed smb session, no sense reporting
787 CIFSPOSIXDelFile(const unsigned int xid
, struct cifs_tcon
*tcon
,
788 const char *fileName
, __u16 type
,
789 const struct nls_table
*nls_codepage
, int remap
)
791 TRANSACTION2_SPI_REQ
*pSMB
= NULL
;
792 TRANSACTION2_SPI_RSP
*pSMBr
= NULL
;
793 struct unlink_psx_rq
*pRqD
;
796 int bytes_returned
= 0;
797 __u16 params
, param_offset
, offset
, byte_count
;
799 cifs_dbg(FYI
, "In POSIX delete\n");
801 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
806 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
808 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, fileName
,
809 PATH_MAX
, nls_codepage
, remap
);
810 name_len
++; /* trailing null */
812 } else { /* BB add path length overrun check */
813 name_len
= strnlen(fileName
, PATH_MAX
);
814 name_len
++; /* trailing null */
815 strncpy(pSMB
->FileName
, fileName
, name_len
);
818 params
= 6 + name_len
;
819 pSMB
->MaxParameterCount
= cpu_to_le16(2);
820 pSMB
->MaxDataCount
= 0; /* BB double check this with jra */
821 pSMB
->MaxSetupCount
= 0;
826 param_offset
= offsetof(struct smb_com_transaction2_spi_req
,
827 InformationLevel
) - 4;
828 offset
= param_offset
+ params
;
830 /* Setup pointer to Request Data (inode type) */
831 pRqD
= (struct unlink_psx_rq
*)(((char *)&pSMB
->hdr
.Protocol
) + offset
);
832 pRqD
->type
= cpu_to_le16(type
);
833 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
834 pSMB
->DataOffset
= cpu_to_le16(offset
);
835 pSMB
->SetupCount
= 1;
837 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_PATH_INFORMATION
);
838 byte_count
= 3 /* pad */ + params
+ sizeof(struct unlink_psx_rq
);
840 pSMB
->DataCount
= cpu_to_le16(sizeof(struct unlink_psx_rq
));
841 pSMB
->TotalDataCount
= cpu_to_le16(sizeof(struct unlink_psx_rq
));
842 pSMB
->ParameterCount
= cpu_to_le16(params
);
843 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
844 pSMB
->InformationLevel
= cpu_to_le16(SMB_POSIX_UNLINK
);
846 inc_rfc1001_len(pSMB
, byte_count
);
847 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
848 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
849 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
851 cifs_dbg(FYI
, "Posix delete returned %d\n", rc
);
852 cifs_buf_release(pSMB
);
854 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_deletes
);
863 CIFSSMBDelFile(const unsigned int xid
, struct cifs_tcon
*tcon
, const char *name
,
864 struct cifs_sb_info
*cifs_sb
)
866 DELETE_FILE_REQ
*pSMB
= NULL
;
867 DELETE_FILE_RSP
*pSMBr
= NULL
;
871 int remap
= cifs_remap(cifs_sb
);
874 rc
= smb_init(SMB_COM_DELETE
, 1, tcon
, (void **) &pSMB
,
879 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
880 name_len
= cifsConvertToUTF16((__le16
*) pSMB
->fileName
, name
,
881 PATH_MAX
, cifs_sb
->local_nls
,
883 name_len
++; /* trailing null */
885 } else { /* BB improve check for buffer overruns BB */
886 name_len
= strnlen(name
, PATH_MAX
);
887 name_len
++; /* trailing null */
888 strncpy(pSMB
->fileName
, name
, name_len
);
890 pSMB
->SearchAttributes
=
891 cpu_to_le16(ATTR_READONLY
| ATTR_HIDDEN
| ATTR_SYSTEM
);
892 pSMB
->BufferFormat
= 0x04;
893 inc_rfc1001_len(pSMB
, name_len
+ 1);
894 pSMB
->ByteCount
= cpu_to_le16(name_len
+ 1);
895 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
896 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
897 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_deletes
);
899 cifs_dbg(FYI
, "Error in RMFile = %d\n", rc
);
901 cifs_buf_release(pSMB
);
909 CIFSSMBRmDir(const unsigned int xid
, struct cifs_tcon
*tcon
, const char *name
,
910 struct cifs_sb_info
*cifs_sb
)
912 DELETE_DIRECTORY_REQ
*pSMB
= NULL
;
913 DELETE_DIRECTORY_RSP
*pSMBr
= NULL
;
917 int remap
= cifs_remap(cifs_sb
);
919 cifs_dbg(FYI
, "In CIFSSMBRmDir\n");
921 rc
= smb_init(SMB_COM_DELETE_DIRECTORY
, 0, tcon
, (void **) &pSMB
,
926 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
927 name_len
= cifsConvertToUTF16((__le16
*) pSMB
->DirName
, name
,
928 PATH_MAX
, cifs_sb
->local_nls
,
930 name_len
++; /* trailing null */
932 } else { /* BB improve check for buffer overruns BB */
933 name_len
= strnlen(name
, PATH_MAX
);
934 name_len
++; /* trailing null */
935 strncpy(pSMB
->DirName
, name
, name_len
);
938 pSMB
->BufferFormat
= 0x04;
939 inc_rfc1001_len(pSMB
, name_len
+ 1);
940 pSMB
->ByteCount
= cpu_to_le16(name_len
+ 1);
941 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
942 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
943 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_rmdirs
);
945 cifs_dbg(FYI
, "Error in RMDir = %d\n", rc
);
947 cifs_buf_release(pSMB
);
954 CIFSSMBMkDir(const unsigned int xid
, struct cifs_tcon
*tcon
, const char *name
,
955 struct cifs_sb_info
*cifs_sb
)
958 CREATE_DIRECTORY_REQ
*pSMB
= NULL
;
959 CREATE_DIRECTORY_RSP
*pSMBr
= NULL
;
962 int remap
= cifs_remap(cifs_sb
);
964 cifs_dbg(FYI
, "In CIFSSMBMkDir\n");
966 rc
= smb_init(SMB_COM_CREATE_DIRECTORY
, 0, tcon
, (void **) &pSMB
,
971 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
972 name_len
= cifsConvertToUTF16((__le16
*) pSMB
->DirName
, name
,
973 PATH_MAX
, cifs_sb
->local_nls
,
975 name_len
++; /* trailing null */
977 } else { /* BB improve check for buffer overruns BB */
978 name_len
= strnlen(name
, PATH_MAX
);
979 name_len
++; /* trailing null */
980 strncpy(pSMB
->DirName
, name
, name_len
);
983 pSMB
->BufferFormat
= 0x04;
984 inc_rfc1001_len(pSMB
, name_len
+ 1);
985 pSMB
->ByteCount
= cpu_to_le16(name_len
+ 1);
986 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
987 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
988 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_mkdirs
);
990 cifs_dbg(FYI
, "Error in Mkdir = %d\n", rc
);
992 cifs_buf_release(pSMB
);
999 CIFSPOSIXCreate(const unsigned int xid
, struct cifs_tcon
*tcon
,
1000 __u32 posix_flags
, __u64 mode
, __u16
*netfid
,
1001 FILE_UNIX_BASIC_INFO
*pRetData
, __u32
*pOplock
,
1002 const char *name
, const struct nls_table
*nls_codepage
,
1005 TRANSACTION2_SPI_REQ
*pSMB
= NULL
;
1006 TRANSACTION2_SPI_RSP
*pSMBr
= NULL
;
1009 int bytes_returned
= 0;
1010 __u16 params
, param_offset
, offset
, byte_count
, count
;
1011 OPEN_PSX_REQ
*pdata
;
1012 OPEN_PSX_RSP
*psx_rsp
;
1014 cifs_dbg(FYI
, "In POSIX Create\n");
1016 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
1021 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
1023 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, name
,
1024 PATH_MAX
, nls_codepage
, remap
);
1025 name_len
++; /* trailing null */
1027 } else { /* BB improve the check for buffer overruns BB */
1028 name_len
= strnlen(name
, PATH_MAX
);
1029 name_len
++; /* trailing null */
1030 strncpy(pSMB
->FileName
, name
, name_len
);
1033 params
= 6 + name_len
;
1034 count
= sizeof(OPEN_PSX_REQ
);
1035 pSMB
->MaxParameterCount
= cpu_to_le16(2);
1036 pSMB
->MaxDataCount
= cpu_to_le16(1000); /* large enough */
1037 pSMB
->MaxSetupCount
= 0;
1041 pSMB
->Reserved2
= 0;
1042 param_offset
= offsetof(struct smb_com_transaction2_spi_req
,
1043 InformationLevel
) - 4;
1044 offset
= param_offset
+ params
;
1045 pdata
= (OPEN_PSX_REQ
*)(((char *)&pSMB
->hdr
.Protocol
) + offset
);
1046 pdata
->Level
= cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC
);
1047 pdata
->Permissions
= cpu_to_le64(mode
);
1048 pdata
->PosixOpenFlags
= cpu_to_le32(posix_flags
);
1049 pdata
->OpenFlags
= cpu_to_le32(*pOplock
);
1050 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
1051 pSMB
->DataOffset
= cpu_to_le16(offset
);
1052 pSMB
->SetupCount
= 1;
1053 pSMB
->Reserved3
= 0;
1054 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_PATH_INFORMATION
);
1055 byte_count
= 3 /* pad */ + params
+ count
;
1057 pSMB
->DataCount
= cpu_to_le16(count
);
1058 pSMB
->ParameterCount
= cpu_to_le16(params
);
1059 pSMB
->TotalDataCount
= pSMB
->DataCount
;
1060 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
1061 pSMB
->InformationLevel
= cpu_to_le16(SMB_POSIX_OPEN
);
1062 pSMB
->Reserved4
= 0;
1063 inc_rfc1001_len(pSMB
, byte_count
);
1064 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
1065 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
1066 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
1068 cifs_dbg(FYI
, "Posix create returned %d\n", rc
);
1069 goto psx_create_err
;
1072 cifs_dbg(FYI
, "copying inode info\n");
1073 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
1075 if (rc
|| get_bcc(&pSMBr
->hdr
) < sizeof(OPEN_PSX_RSP
)) {
1076 rc
= -EIO
; /* bad smb */
1077 goto psx_create_err
;
1080 /* copy return information to pRetData */
1081 psx_rsp
= (OPEN_PSX_RSP
*)((char *) &pSMBr
->hdr
.Protocol
1082 + le16_to_cpu(pSMBr
->t2
.DataOffset
));
1084 *pOplock
= le16_to_cpu(psx_rsp
->OplockFlags
);
1086 *netfid
= psx_rsp
->Fid
; /* cifs fid stays in le */
1087 /* Let caller know file was created so we can set the mode. */
1088 /* Do we care about the CreateAction in any other cases? */
1089 if (cpu_to_le32(FILE_CREATE
) == psx_rsp
->CreateAction
)
1090 *pOplock
|= CIFS_CREATE_ACTION
;
1091 /* check to make sure response data is there */
1092 if (psx_rsp
->ReturnedLevel
!= cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC
)) {
1093 pRetData
->Type
= cpu_to_le32(-1); /* unknown */
1094 cifs_dbg(NOISY
, "unknown type\n");
1096 if (get_bcc(&pSMBr
->hdr
) < sizeof(OPEN_PSX_RSP
)
1097 + sizeof(FILE_UNIX_BASIC_INFO
)) {
1098 cifs_dbg(VFS
, "Open response data too small\n");
1099 pRetData
->Type
= cpu_to_le32(-1);
1100 goto psx_create_err
;
1102 memcpy((char *) pRetData
,
1103 (char *)psx_rsp
+ sizeof(OPEN_PSX_RSP
),
1104 sizeof(FILE_UNIX_BASIC_INFO
));
1108 cifs_buf_release(pSMB
);
1110 if (posix_flags
& SMB_O_DIRECTORY
)
1111 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_posixmkdirs
);
1113 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_posixopens
);
1121 static __u16
convert_disposition(int disposition
)
1125 switch (disposition
) {
1126 case FILE_SUPERSEDE
:
1127 ofun
= SMBOPEN_OCREATE
| SMBOPEN_OTRUNC
;
1130 ofun
= SMBOPEN_OAPPEND
;
1133 ofun
= SMBOPEN_OCREATE
;
1136 ofun
= SMBOPEN_OCREATE
| SMBOPEN_OAPPEND
;
1138 case FILE_OVERWRITE
:
1139 ofun
= SMBOPEN_OTRUNC
;
1141 case FILE_OVERWRITE_IF
:
1142 ofun
= SMBOPEN_OCREATE
| SMBOPEN_OTRUNC
;
1145 cifs_dbg(FYI
, "unknown disposition %d\n", disposition
);
1146 ofun
= SMBOPEN_OAPPEND
; /* regular open */
1152 access_flags_to_smbopen_mode(const int access_flags
)
1154 int masked_flags
= access_flags
& (GENERIC_READ
| GENERIC_WRITE
);
1156 if (masked_flags
== GENERIC_READ
)
1157 return SMBOPEN_READ
;
1158 else if (masked_flags
== GENERIC_WRITE
)
1159 return SMBOPEN_WRITE
;
1161 /* just go for read/write */
1162 return SMBOPEN_READWRITE
;
1166 SMBLegacyOpen(const unsigned int xid
, struct cifs_tcon
*tcon
,
1167 const char *fileName
, const int openDisposition
,
1168 const int access_flags
, const int create_options
, __u16
*netfid
,
1169 int *pOplock
, FILE_ALL_INFO
*pfile_info
,
1170 const struct nls_table
*nls_codepage
, int remap
)
1173 OPENX_REQ
*pSMB
= NULL
;
1174 OPENX_RSP
*pSMBr
= NULL
;
1180 rc
= smb_init(SMB_COM_OPEN_ANDX
, 15, tcon
, (void **) &pSMB
,
1185 pSMB
->AndXCommand
= 0xFF; /* none */
1187 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
1188 count
= 1; /* account for one byte pad to word boundary */
1190 cifsConvertToUTF16((__le16
*) (pSMB
->fileName
+ 1),
1191 fileName
, PATH_MAX
, nls_codepage
, remap
);
1192 name_len
++; /* trailing null */
1194 } else { /* BB improve check for buffer overruns BB */
1195 count
= 0; /* no pad */
1196 name_len
= strnlen(fileName
, PATH_MAX
);
1197 name_len
++; /* trailing null */
1198 strncpy(pSMB
->fileName
, fileName
, name_len
);
1200 if (*pOplock
& REQ_OPLOCK
)
1201 pSMB
->OpenFlags
= cpu_to_le16(REQ_OPLOCK
);
1202 else if (*pOplock
& REQ_BATCHOPLOCK
)
1203 pSMB
->OpenFlags
= cpu_to_le16(REQ_BATCHOPLOCK
);
1205 pSMB
->OpenFlags
|= cpu_to_le16(REQ_MORE_INFO
);
1206 pSMB
->Mode
= cpu_to_le16(access_flags_to_smbopen_mode(access_flags
));
1207 pSMB
->Mode
|= cpu_to_le16(0x40); /* deny none */
1208 /* set file as system file if special file such
1209 as fifo and server expecting SFU style and
1210 no Unix extensions */
1212 if (create_options
& CREATE_OPTION_SPECIAL
)
1213 pSMB
->FileAttributes
= cpu_to_le16(ATTR_SYSTEM
);
1214 else /* BB FIXME BB */
1215 pSMB
->FileAttributes
= cpu_to_le16(0/*ATTR_NORMAL*/);
1217 if (create_options
& CREATE_OPTION_READONLY
)
1218 pSMB
->FileAttributes
|= cpu_to_le16(ATTR_READONLY
);
1221 /* pSMB->CreateOptions = cpu_to_le32(create_options &
1222 CREATE_OPTIONS_MASK); */
1223 /* BB FIXME END BB */
1225 pSMB
->Sattr
= cpu_to_le16(ATTR_HIDDEN
| ATTR_SYSTEM
| ATTR_DIRECTORY
);
1226 pSMB
->OpenFunction
= cpu_to_le16(convert_disposition(openDisposition
));
1228 inc_rfc1001_len(pSMB
, count
);
1230 pSMB
->ByteCount
= cpu_to_le16(count
);
1231 /* long_op set to 1 to allow for oplock break timeouts */
1232 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
1233 (struct smb_hdr
*)pSMBr
, &bytes_returned
, 0);
1234 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_opens
);
1236 cifs_dbg(FYI
, "Error in Open = %d\n", rc
);
1238 /* BB verify if wct == 15 */
1240 /* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
1242 *netfid
= pSMBr
->Fid
; /* cifs fid stays in le */
1243 /* Let caller know file was created so we can set the mode. */
1244 /* Do we care about the CreateAction in any other cases? */
1246 /* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
1247 *pOplock |= CIFS_CREATE_ACTION; */
1251 pfile_info
->CreationTime
= 0; /* BB convert CreateTime*/
1252 pfile_info
->LastAccessTime
= 0; /* BB fixme */
1253 pfile_info
->LastWriteTime
= 0; /* BB fixme */
1254 pfile_info
->ChangeTime
= 0; /* BB fixme */
1255 pfile_info
->Attributes
=
1256 cpu_to_le32(le16_to_cpu(pSMBr
->FileAttributes
));
1257 /* the file_info buf is endian converted by caller */
1258 pfile_info
->AllocationSize
=
1259 cpu_to_le64(le32_to_cpu(pSMBr
->EndOfFile
));
1260 pfile_info
->EndOfFile
= pfile_info
->AllocationSize
;
1261 pfile_info
->NumberOfLinks
= cpu_to_le32(1);
1262 pfile_info
->DeletePending
= 0;
1266 cifs_buf_release(pSMB
);
1273 CIFS_open(const unsigned int xid
, struct cifs_open_parms
*oparms
, int *oplock
,
1277 OPEN_REQ
*req
= NULL
;
1278 OPEN_RSP
*rsp
= NULL
;
1282 struct cifs_sb_info
*cifs_sb
= oparms
->cifs_sb
;
1283 struct cifs_tcon
*tcon
= oparms
->tcon
;
1284 int remap
= cifs_remap(cifs_sb
);
1285 const struct nls_table
*nls
= cifs_sb
->local_nls
;
1286 int create_options
= oparms
->create_options
;
1287 int desired_access
= oparms
->desired_access
;
1288 int disposition
= oparms
->disposition
;
1289 const char *path
= oparms
->path
;
1292 rc
= smb_init(SMB_COM_NT_CREATE_ANDX
, 24, tcon
, (void **)&req
,
1297 /* no commands go after this */
1298 req
->AndXCommand
= 0xFF;
1300 if (req
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
1301 /* account for one byte pad to word boundary */
1303 name_len
= cifsConvertToUTF16((__le16
*)(req
->fileName
+ 1),
1304 path
, PATH_MAX
, nls
, remap
);
1308 req
->NameLength
= cpu_to_le16(name_len
);
1310 /* BB improve check for buffer overruns BB */
1313 name_len
= strnlen(path
, PATH_MAX
);
1316 req
->NameLength
= cpu_to_le16(name_len
);
1317 strncpy(req
->fileName
, path
, name_len
);
1320 if (*oplock
& REQ_OPLOCK
)
1321 req
->OpenFlags
= cpu_to_le32(REQ_OPLOCK
);
1322 else if (*oplock
& REQ_BATCHOPLOCK
)
1323 req
->OpenFlags
= cpu_to_le32(REQ_BATCHOPLOCK
);
1325 req
->DesiredAccess
= cpu_to_le32(desired_access
);
1326 req
->AllocationSize
= 0;
1329 * Set file as system file if special file such as fifo and server
1330 * expecting SFU style and no Unix extensions.
1332 if (create_options
& CREATE_OPTION_SPECIAL
)
1333 req
->FileAttributes
= cpu_to_le32(ATTR_SYSTEM
);
1335 req
->FileAttributes
= cpu_to_le32(ATTR_NORMAL
);
1338 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1339 * sensitive checks for other servers such as Samba.
1341 if (tcon
->ses
->capabilities
& CAP_UNIX
)
1342 req
->FileAttributes
|= cpu_to_le32(ATTR_POSIX_SEMANTICS
);
1344 if (create_options
& CREATE_OPTION_READONLY
)
1345 req
->FileAttributes
|= cpu_to_le32(ATTR_READONLY
);
1347 req
->ShareAccess
= cpu_to_le32(FILE_SHARE_ALL
);
1348 req
->CreateDisposition
= cpu_to_le32(disposition
);
1349 req
->CreateOptions
= cpu_to_le32(create_options
& CREATE_OPTIONS_MASK
);
1351 /* BB Expirement with various impersonation levels and verify */
1352 req
->ImpersonationLevel
= cpu_to_le32(SECURITY_IMPERSONATION
);
1353 req
->SecurityFlags
= SECURITY_CONTEXT_TRACKING
|SECURITY_EFFECTIVE_ONLY
;
1356 inc_rfc1001_len(req
, count
);
1358 req
->ByteCount
= cpu_to_le16(count
);
1359 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*)req
,
1360 (struct smb_hdr
*)rsp
, &bytes_returned
, 0);
1361 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_opens
);
1363 cifs_dbg(FYI
, "Error in Open = %d\n", rc
);
1364 cifs_buf_release(req
);
1370 /* 1 byte no need to le_to_cpu */
1371 *oplock
= rsp
->OplockLevel
;
1372 /* cifs fid stays in le */
1373 oparms
->fid
->netfid
= rsp
->Fid
;
1375 /* Let caller know file was created so we can set the mode. */
1376 /* Do we care about the CreateAction in any other cases? */
1377 if (cpu_to_le32(FILE_CREATE
) == rsp
->CreateAction
)
1378 *oplock
|= CIFS_CREATE_ACTION
;
1381 /* copy from CreationTime to Attributes */
1382 memcpy((char *)buf
, (char *)&rsp
->CreationTime
, 36);
1383 /* the file_info buf is endian converted by caller */
1384 buf
->AllocationSize
= rsp
->AllocationSize
;
1385 buf
->EndOfFile
= rsp
->EndOfFile
;
1386 buf
->NumberOfLinks
= cpu_to_le32(1);
1387 buf
->DeletePending
= 0;
1390 cifs_buf_release(req
);
1395 * Discard any remaining data in the current SMB. To do this, we borrow the
1399 cifs_readv_discard(struct TCP_Server_Info
*server
, struct mid_q_entry
*mid
)
1401 unsigned int rfclen
= get_rfc1002_length(server
->smallbuf
);
1402 int remaining
= rfclen
+ 4 - server
->total_read
;
1403 struct cifs_readdata
*rdata
= mid
->callback_data
;
1405 while (remaining
> 0) {
1408 length
= cifs_read_from_socket(server
, server
->bigbuf
,
1409 min_t(unsigned int, remaining
,
1410 CIFSMaxBufSize
+ MAX_HEADER_SIZE(server
)));
1413 server
->total_read
+= length
;
1414 remaining
-= length
;
1417 dequeue_mid(mid
, rdata
->result
);
1422 cifs_readv_receive(struct TCP_Server_Info
*server
, struct mid_q_entry
*mid
)
1425 unsigned int data_offset
, data_len
;
1426 struct cifs_readdata
*rdata
= mid
->callback_data
;
1427 char *buf
= server
->smallbuf
;
1428 unsigned int buflen
= get_rfc1002_length(buf
) + 4;
1430 cifs_dbg(FYI
, "%s: mid=%llu offset=%llu bytes=%u\n",
1431 __func__
, mid
->mid
, rdata
->offset
, rdata
->bytes
);
1434 * read the rest of READ_RSP header (sans Data array), or whatever we
1435 * can if there's not enough data. At this point, we've read down to
1438 len
= min_t(unsigned int, buflen
, server
->vals
->read_rsp_size
) -
1439 HEADER_SIZE(server
) + 1;
1441 rdata
->iov
.iov_base
= buf
+ HEADER_SIZE(server
) - 1;
1442 rdata
->iov
.iov_len
= len
;
1444 length
= cifs_readv_from_socket(server
, &rdata
->iov
, 1, len
);
1447 server
->total_read
+= length
;
1449 /* Was the SMB read successful? */
1450 rdata
->result
= server
->ops
->map_error(buf
, false);
1451 if (rdata
->result
!= 0) {
1452 cifs_dbg(FYI
, "%s: server returned error %d\n",
1453 __func__
, rdata
->result
);
1454 return cifs_readv_discard(server
, mid
);
1457 /* Is there enough to get to the rest of the READ_RSP header? */
1458 if (server
->total_read
< server
->vals
->read_rsp_size
) {
1459 cifs_dbg(FYI
, "%s: server returned short header. got=%u expected=%zu\n",
1460 __func__
, server
->total_read
,
1461 server
->vals
->read_rsp_size
);
1462 rdata
->result
= -EIO
;
1463 return cifs_readv_discard(server
, mid
);
1466 data_offset
= server
->ops
->read_data_offset(buf
) + 4;
1467 if (data_offset
< server
->total_read
) {
1469 * win2k8 sometimes sends an offset of 0 when the read
1470 * is beyond the EOF. Treat it as if the data starts just after
1473 cifs_dbg(FYI
, "%s: data offset (%u) inside read response header\n",
1474 __func__
, data_offset
);
1475 data_offset
= server
->total_read
;
1476 } else if (data_offset
> MAX_CIFS_SMALL_BUFFER_SIZE
) {
1477 /* data_offset is beyond the end of smallbuf */
1478 cifs_dbg(FYI
, "%s: data offset (%u) beyond end of smallbuf\n",
1479 __func__
, data_offset
);
1480 rdata
->result
= -EIO
;
1481 return cifs_readv_discard(server
, mid
);
1484 cifs_dbg(FYI
, "%s: total_read=%u data_offset=%u\n",
1485 __func__
, server
->total_read
, data_offset
);
1487 len
= data_offset
- server
->total_read
;
1489 /* read any junk before data into the rest of smallbuf */
1490 rdata
->iov
.iov_base
= buf
+ server
->total_read
;
1491 rdata
->iov
.iov_len
= len
;
1492 length
= cifs_readv_from_socket(server
, &rdata
->iov
, 1, len
);
1495 server
->total_read
+= length
;
1498 /* set up first iov for signature check */
1499 rdata
->iov
.iov_base
= buf
;
1500 rdata
->iov
.iov_len
= server
->total_read
;
1501 cifs_dbg(FYI
, "0: iov_base=%p iov_len=%zu\n",
1502 rdata
->iov
.iov_base
, rdata
->iov
.iov_len
);
1504 /* how much data is in the response? */
1505 data_len
= server
->ops
->read_data_length(buf
);
1506 if (data_offset
+ data_len
> buflen
) {
1507 /* data_len is corrupt -- discard frame */
1508 rdata
->result
= -EIO
;
1509 return cifs_readv_discard(server
, mid
);
1512 length
= rdata
->read_into_pages(server
, rdata
, data_len
);
1516 server
->total_read
+= length
;
1518 cifs_dbg(FYI
, "total_read=%u buflen=%u remaining=%u\n",
1519 server
->total_read
, buflen
, data_len
);
1521 /* discard anything left over */
1522 if (server
->total_read
< buflen
)
1523 return cifs_readv_discard(server
, mid
);
1525 dequeue_mid(mid
, false);
1530 cifs_readv_callback(struct mid_q_entry
*mid
)
1532 struct cifs_readdata
*rdata
= mid
->callback_data
;
1533 struct cifs_tcon
*tcon
= tlink_tcon(rdata
->cfile
->tlink
);
1534 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
1535 struct smb_rqst rqst
= { .rq_iov
= &rdata
->iov
,
1537 .rq_pages
= rdata
->pages
,
1538 .rq_npages
= rdata
->nr_pages
,
1539 .rq_pagesz
= rdata
->pagesz
,
1540 .rq_tailsz
= rdata
->tailsz
};
1542 cifs_dbg(FYI
, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1543 __func__
, mid
->mid
, mid
->mid_state
, rdata
->result
,
1546 switch (mid
->mid_state
) {
1547 case MID_RESPONSE_RECEIVED
:
1548 /* result already set, check signature */
1552 rc
= cifs_verify_signature(&rqst
, server
,
1553 mid
->sequence_number
);
1555 cifs_dbg(VFS
, "SMB signature verification returned error = %d\n",
1558 /* FIXME: should this be counted toward the initiating task? */
1559 task_io_account_read(rdata
->got_bytes
);
1560 cifs_stats_bytes_read(tcon
, rdata
->got_bytes
);
1562 case MID_REQUEST_SUBMITTED
:
1563 case MID_RETRY_NEEDED
:
1564 rdata
->result
= -EAGAIN
;
1565 if (server
->sign
&& rdata
->got_bytes
)
1566 /* reset bytes number since we can not check a sign */
1567 rdata
->got_bytes
= 0;
1568 /* FIXME: should this be counted toward the initiating task? */
1569 task_io_account_read(rdata
->got_bytes
);
1570 cifs_stats_bytes_read(tcon
, rdata
->got_bytes
);
1573 rdata
->result
= -EIO
;
1576 queue_work(cifsiod_wq
, &rdata
->work
);
1577 mutex_lock(&server
->srv_mutex
);
1578 DeleteMidQEntry(mid
);
1579 mutex_unlock(&server
->srv_mutex
);
1580 add_credits(server
, 1, 0);
1583 /* cifs_async_readv - send an async write, and set up mid to handle result */
1585 cifs_async_readv(struct cifs_readdata
*rdata
)
1588 READ_REQ
*smb
= NULL
;
1590 struct cifs_tcon
*tcon
= tlink_tcon(rdata
->cfile
->tlink
);
1591 struct smb_rqst rqst
= { .rq_iov
= &rdata
->iov
,
1594 cifs_dbg(FYI
, "%s: offset=%llu bytes=%u\n",
1595 __func__
, rdata
->offset
, rdata
->bytes
);
1597 if (tcon
->ses
->capabilities
& CAP_LARGE_FILES
)
1600 wct
= 10; /* old style read */
1601 if ((rdata
->offset
>> 32) > 0) {
1602 /* can not handle this big offset for old */
1607 rc
= small_smb_init(SMB_COM_READ_ANDX
, wct
, tcon
, (void **)&smb
);
1611 smb
->hdr
.Pid
= cpu_to_le16((__u16
)rdata
->pid
);
1612 smb
->hdr
.PidHigh
= cpu_to_le16((__u16
)(rdata
->pid
>> 16));
1614 smb
->AndXCommand
= 0xFF; /* none */
1615 smb
->Fid
= rdata
->cfile
->fid
.netfid
;
1616 smb
->OffsetLow
= cpu_to_le32(rdata
->offset
& 0xFFFFFFFF);
1618 smb
->OffsetHigh
= cpu_to_le32(rdata
->offset
>> 32);
1620 smb
->MaxCount
= cpu_to_le16(rdata
->bytes
& 0xFFFF);
1621 smb
->MaxCountHigh
= cpu_to_le32(rdata
->bytes
>> 16);
1625 /* old style read */
1626 struct smb_com_readx_req
*smbr
=
1627 (struct smb_com_readx_req
*)smb
;
1628 smbr
->ByteCount
= 0;
1631 /* 4 for RFC1001 length + 1 for BCC */
1632 rdata
->iov
.iov_base
= smb
;
1633 rdata
->iov
.iov_len
= be32_to_cpu(smb
->hdr
.smb_buf_length
) + 4;
1635 kref_get(&rdata
->refcount
);
1636 rc
= cifs_call_async(tcon
->ses
->server
, &rqst
, cifs_readv_receive
,
1637 cifs_readv_callback
, rdata
, 0);
1640 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_reads
);
1642 kref_put(&rdata
->refcount
, cifs_readdata_release
);
1644 cifs_small_buf_release(smb
);
1649 CIFSSMBRead(const unsigned int xid
, struct cifs_io_parms
*io_parms
,
1650 unsigned int *nbytes
, char **buf
, int *pbuf_type
)
1653 READ_REQ
*pSMB
= NULL
;
1654 READ_RSP
*pSMBr
= NULL
;
1655 char *pReadData
= NULL
;
1657 int resp_buf_type
= 0;
1659 __u32 pid
= io_parms
->pid
;
1660 __u16 netfid
= io_parms
->netfid
;
1661 __u64 offset
= io_parms
->offset
;
1662 struct cifs_tcon
*tcon
= io_parms
->tcon
;
1663 unsigned int count
= io_parms
->length
;
1665 cifs_dbg(FYI
, "Reading %d bytes on fid %d\n", count
, netfid
);
1666 if (tcon
->ses
->capabilities
& CAP_LARGE_FILES
)
1669 wct
= 10; /* old style read */
1670 if ((offset
>> 32) > 0) {
1671 /* can not handle this big offset for old */
1677 rc
= small_smb_init(SMB_COM_READ_ANDX
, wct
, tcon
, (void **) &pSMB
);
1681 pSMB
->hdr
.Pid
= cpu_to_le16((__u16
)pid
);
1682 pSMB
->hdr
.PidHigh
= cpu_to_le16((__u16
)(pid
>> 16));
1684 /* tcon and ses pointer are checked in smb_init */
1685 if (tcon
->ses
->server
== NULL
)
1686 return -ECONNABORTED
;
1688 pSMB
->AndXCommand
= 0xFF; /* none */
1690 pSMB
->OffsetLow
= cpu_to_le32(offset
& 0xFFFFFFFF);
1692 pSMB
->OffsetHigh
= cpu_to_le32(offset
>> 32);
1694 pSMB
->Remaining
= 0;
1695 pSMB
->MaxCount
= cpu_to_le16(count
& 0xFFFF);
1696 pSMB
->MaxCountHigh
= cpu_to_le32(count
>> 16);
1698 pSMB
->ByteCount
= 0; /* no need to do le conversion since 0 */
1700 /* old style read */
1701 struct smb_com_readx_req
*pSMBW
=
1702 (struct smb_com_readx_req
*)pSMB
;
1703 pSMBW
->ByteCount
= 0;
1706 iov
[0].iov_base
= (char *)pSMB
;
1707 iov
[0].iov_len
= be32_to_cpu(pSMB
->hdr
.smb_buf_length
) + 4;
1708 rc
= SendReceive2(xid
, tcon
->ses
, iov
, 1 /* num iovecs */,
1709 &resp_buf_type
, CIFS_LOG_ERROR
);
1710 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_reads
);
1711 pSMBr
= (READ_RSP
*)iov
[0].iov_base
;
1713 cifs_dbg(VFS
, "Send error in read = %d\n", rc
);
1715 int data_length
= le16_to_cpu(pSMBr
->DataLengthHigh
);
1716 data_length
= data_length
<< 16;
1717 data_length
+= le16_to_cpu(pSMBr
->DataLength
);
1718 *nbytes
= data_length
;
1720 /*check that DataLength would not go beyond end of SMB */
1721 if ((data_length
> CIFSMaxBufSize
)
1722 || (data_length
> count
)) {
1723 cifs_dbg(FYI
, "bad length %d for count %d\n",
1724 data_length
, count
);
1728 pReadData
= (char *) (&pSMBr
->hdr
.Protocol
) +
1729 le16_to_cpu(pSMBr
->DataOffset
);
1730 /* if (rc = copy_to_user(buf, pReadData, data_length)) {
1731 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
1733 }*/ /* can not use copy_to_user when using page cache*/
1735 memcpy(*buf
, pReadData
, data_length
);
1739 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
1741 free_rsp_buf(resp_buf_type
, iov
[0].iov_base
);
1742 } else if (resp_buf_type
!= CIFS_NO_BUFFER
) {
1743 /* return buffer to caller to free */
1744 *buf
= iov
[0].iov_base
;
1745 if (resp_buf_type
== CIFS_SMALL_BUFFER
)
1746 *pbuf_type
= CIFS_SMALL_BUFFER
;
1747 else if (resp_buf_type
== CIFS_LARGE_BUFFER
)
1748 *pbuf_type
= CIFS_LARGE_BUFFER
;
1749 } /* else no valid buffer on return - leave as null */
1751 /* Note: On -EAGAIN error only caller can retry on handle based calls
1752 since file handle passed in no longer valid */
1758 CIFSSMBWrite(const unsigned int xid
, struct cifs_io_parms
*io_parms
,
1759 unsigned int *nbytes
, const char *buf
,
1760 const char __user
*ubuf
, const int long_op
)
1763 WRITE_REQ
*pSMB
= NULL
;
1764 WRITE_RSP
*pSMBr
= NULL
;
1765 int bytes_returned
, wct
;
1768 __u32 pid
= io_parms
->pid
;
1769 __u16 netfid
= io_parms
->netfid
;
1770 __u64 offset
= io_parms
->offset
;
1771 struct cifs_tcon
*tcon
= io_parms
->tcon
;
1772 unsigned int count
= io_parms
->length
;
1776 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
1777 if (tcon
->ses
== NULL
)
1778 return -ECONNABORTED
;
1780 if (tcon
->ses
->capabilities
& CAP_LARGE_FILES
)
1784 if ((offset
>> 32) > 0) {
1785 /* can not handle big offset for old srv */
1790 rc
= smb_init(SMB_COM_WRITE_ANDX
, wct
, tcon
, (void **) &pSMB
,
1795 pSMB
->hdr
.Pid
= cpu_to_le16((__u16
)pid
);
1796 pSMB
->hdr
.PidHigh
= cpu_to_le16((__u16
)(pid
>> 16));
1798 /* tcon and ses pointer are checked in smb_init */
1799 if (tcon
->ses
->server
== NULL
)
1800 return -ECONNABORTED
;
1802 pSMB
->AndXCommand
= 0xFF; /* none */
1804 pSMB
->OffsetLow
= cpu_to_le32(offset
& 0xFFFFFFFF);
1806 pSMB
->OffsetHigh
= cpu_to_le32(offset
>> 32);
1808 pSMB
->Reserved
= 0xFFFFFFFF;
1809 pSMB
->WriteMode
= 0;
1810 pSMB
->Remaining
= 0;
1812 /* Can increase buffer size if buffer is big enough in some cases ie we
1813 can send more if LARGE_WRITE_X capability returned by the server and if
1814 our buffer is big enough or if we convert to iovecs on socket writes
1815 and eliminate the copy to the CIFS buffer */
1816 if (tcon
->ses
->capabilities
& CAP_LARGE_WRITE_X
) {
1817 bytes_sent
= min_t(const unsigned int, CIFSMaxBufSize
, count
);
1819 bytes_sent
= (tcon
->ses
->server
->maxBuf
- MAX_CIFS_HDR_SIZE
)
1823 if (bytes_sent
> count
)
1826 cpu_to_le16(offsetof(struct smb_com_write_req
, Data
) - 4);
1828 memcpy(pSMB
->Data
, buf
, bytes_sent
);
1830 if (copy_from_user(pSMB
->Data
, ubuf
, bytes_sent
)) {
1831 cifs_buf_release(pSMB
);
1834 } else if (count
!= 0) {
1836 cifs_buf_release(pSMB
);
1838 } /* else setting file size with write of zero bytes */
1840 byte_count
= bytes_sent
+ 1; /* pad */
1841 else /* wct == 12 */
1842 byte_count
= bytes_sent
+ 5; /* bigger pad, smaller smb hdr */
1844 pSMB
->DataLengthLow
= cpu_to_le16(bytes_sent
& 0xFFFF);
1845 pSMB
->DataLengthHigh
= cpu_to_le16(bytes_sent
>> 16);
1846 inc_rfc1001_len(pSMB
, byte_count
);
1849 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
1850 else { /* old style write has byte count 4 bytes earlier
1852 struct smb_com_writex_req
*pSMBW
=
1853 (struct smb_com_writex_req
*)pSMB
;
1854 pSMBW
->ByteCount
= cpu_to_le16(byte_count
);
1857 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
1858 (struct smb_hdr
*) pSMBr
, &bytes_returned
, long_op
);
1859 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_writes
);
1861 cifs_dbg(FYI
, "Send error in write = %d\n", rc
);
1863 *nbytes
= le16_to_cpu(pSMBr
->CountHigh
);
1864 *nbytes
= (*nbytes
) << 16;
1865 *nbytes
+= le16_to_cpu(pSMBr
->Count
);
1868 * Mask off high 16 bits when bytes written as returned by the
1869 * server is greater than bytes requested by the client. Some
1870 * OS/2 servers are known to set incorrect CountHigh values.
1872 if (*nbytes
> count
)
1876 cifs_buf_release(pSMB
);
1878 /* Note: On -EAGAIN error only caller can retry on handle based calls
1879 since file handle passed in no longer valid */
1885 cifs_writedata_release(struct kref
*refcount
)
1887 struct cifs_writedata
*wdata
= container_of(refcount
,
1888 struct cifs_writedata
, refcount
);
1891 cifsFileInfo_put(wdata
->cfile
);
1897 * Write failed with a retryable error. Resend the write request. It's also
1898 * possible that the page was redirtied so re-clean the page.
1901 cifs_writev_requeue(struct cifs_writedata
*wdata
)
1904 struct inode
*inode
= d_inode(wdata
->cfile
->dentry
);
1905 struct TCP_Server_Info
*server
;
1906 unsigned int rest_len
;
1908 server
= tlink_tcon(wdata
->cfile
->tlink
)->ses
->server
;
1910 rest_len
= wdata
->bytes
;
1912 struct cifs_writedata
*wdata2
;
1913 unsigned int j
, nr_pages
, wsize
, tailsz
, cur_len
;
1915 wsize
= server
->ops
->wp_retry_size(inode
);
1916 if (wsize
< rest_len
) {
1917 nr_pages
= wsize
/ PAGE_CACHE_SIZE
;
1922 cur_len
= nr_pages
* PAGE_CACHE_SIZE
;
1923 tailsz
= PAGE_CACHE_SIZE
;
1925 nr_pages
= DIV_ROUND_UP(rest_len
, PAGE_CACHE_SIZE
);
1927 tailsz
= rest_len
- (nr_pages
- 1) * PAGE_CACHE_SIZE
;
1930 wdata2
= cifs_writedata_alloc(nr_pages
, cifs_writev_complete
);
1936 for (j
= 0; j
< nr_pages
; j
++) {
1937 wdata2
->pages
[j
] = wdata
->pages
[i
+ j
];
1938 lock_page(wdata2
->pages
[j
]);
1939 clear_page_dirty_for_io(wdata2
->pages
[j
]);
1942 wdata2
->sync_mode
= wdata
->sync_mode
;
1943 wdata2
->nr_pages
= nr_pages
;
1944 wdata2
->offset
= page_offset(wdata2
->pages
[0]);
1945 wdata2
->pagesz
= PAGE_CACHE_SIZE
;
1946 wdata2
->tailsz
= tailsz
;
1947 wdata2
->bytes
= cur_len
;
1949 wdata2
->cfile
= find_writable_file(CIFS_I(inode
), false);
1950 if (!wdata2
->cfile
) {
1951 cifs_dbg(VFS
, "No writable handles for inode\n");
1955 wdata2
->pid
= wdata2
->cfile
->pid
;
1956 rc
= server
->ops
->async_writev(wdata2
, cifs_writedata_release
);
1958 for (j
= 0; j
< nr_pages
; j
++) {
1959 unlock_page(wdata2
->pages
[j
]);
1960 if (rc
!= 0 && rc
!= -EAGAIN
) {
1961 SetPageError(wdata2
->pages
[j
]);
1962 end_page_writeback(wdata2
->pages
[j
]);
1963 page_cache_release(wdata2
->pages
[j
]);
1968 kref_put(&wdata2
->refcount
, cifs_writedata_release
);
1974 rest_len
-= cur_len
;
1976 } while (i
< wdata
->nr_pages
);
1978 mapping_set_error(inode
->i_mapping
, rc
);
1979 kref_put(&wdata
->refcount
, cifs_writedata_release
);
1983 cifs_writev_complete(struct work_struct
*work
)
1985 struct cifs_writedata
*wdata
= container_of(work
,
1986 struct cifs_writedata
, work
);
1987 struct inode
*inode
= d_inode(wdata
->cfile
->dentry
);
1990 if (wdata
->result
== 0) {
1991 spin_lock(&inode
->i_lock
);
1992 cifs_update_eof(CIFS_I(inode
), wdata
->offset
, wdata
->bytes
);
1993 spin_unlock(&inode
->i_lock
);
1994 cifs_stats_bytes_written(tlink_tcon(wdata
->cfile
->tlink
),
1996 } else if (wdata
->sync_mode
== WB_SYNC_ALL
&& wdata
->result
== -EAGAIN
)
1997 return cifs_writev_requeue(wdata
);
1999 for (i
= 0; i
< wdata
->nr_pages
; i
++) {
2000 struct page
*page
= wdata
->pages
[i
];
2001 if (wdata
->result
== -EAGAIN
)
2002 __set_page_dirty_nobuffers(page
);
2003 else if (wdata
->result
< 0)
2005 end_page_writeback(page
);
2006 page_cache_release(page
);
2008 if (wdata
->result
!= -EAGAIN
)
2009 mapping_set_error(inode
->i_mapping
, wdata
->result
);
2010 kref_put(&wdata
->refcount
, cifs_writedata_release
);
2013 struct cifs_writedata
*
2014 cifs_writedata_alloc(unsigned int nr_pages
, work_func_t complete
)
2016 struct cifs_writedata
*wdata
;
2018 /* writedata + number of page pointers */
2019 wdata
= kzalloc(sizeof(*wdata
) +
2020 sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
2021 if (wdata
!= NULL
) {
2022 kref_init(&wdata
->refcount
);
2023 INIT_LIST_HEAD(&wdata
->list
);
2024 init_completion(&wdata
->done
);
2025 INIT_WORK(&wdata
->work
, complete
);
2031 * Check the mid_state and signature on received buffer (if any), and queue the
2032 * workqueue completion task.
2035 cifs_writev_callback(struct mid_q_entry
*mid
)
2037 struct cifs_writedata
*wdata
= mid
->callback_data
;
2038 struct cifs_tcon
*tcon
= tlink_tcon(wdata
->cfile
->tlink
);
2039 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
2040 unsigned int written
;
2041 WRITE_RSP
*smb
= (WRITE_RSP
*)mid
->resp_buf
;
2043 switch (mid
->mid_state
) {
2044 case MID_RESPONSE_RECEIVED
:
2045 wdata
->result
= cifs_check_receive(mid
, tcon
->ses
->server
, 0);
2046 if (wdata
->result
!= 0)
2049 written
= le16_to_cpu(smb
->CountHigh
);
2051 written
+= le16_to_cpu(smb
->Count
);
2053 * Mask off high 16 bits when bytes written as returned
2054 * by the server is greater than bytes requested by the
2055 * client. OS/2 servers are known to set incorrect
2058 if (written
> wdata
->bytes
)
2061 if (written
< wdata
->bytes
)
2062 wdata
->result
= -ENOSPC
;
2064 wdata
->bytes
= written
;
2066 case MID_REQUEST_SUBMITTED
:
2067 case MID_RETRY_NEEDED
:
2068 wdata
->result
= -EAGAIN
;
2071 wdata
->result
= -EIO
;
2075 queue_work(cifsiod_wq
, &wdata
->work
);
2076 mutex_lock(&server
->srv_mutex
);
2077 DeleteMidQEntry(mid
);
2078 mutex_unlock(&server
->srv_mutex
);
2079 add_credits(tcon
->ses
->server
, 1, 0);
2082 /* cifs_async_writev - send an async write, and set up mid to handle result */
2084 cifs_async_writev(struct cifs_writedata
*wdata
,
2085 void (*release
)(struct kref
*kref
))
2088 WRITE_REQ
*smb
= NULL
;
2090 struct cifs_tcon
*tcon
= tlink_tcon(wdata
->cfile
->tlink
);
2092 struct smb_rqst rqst
= { };
2094 if (tcon
->ses
->capabilities
& CAP_LARGE_FILES
) {
2098 if (wdata
->offset
>> 32 > 0) {
2099 /* can not handle big offset for old srv */
2104 rc
= small_smb_init(SMB_COM_WRITE_ANDX
, wct
, tcon
, (void **)&smb
);
2106 goto async_writev_out
;
2108 smb
->hdr
.Pid
= cpu_to_le16((__u16
)wdata
->pid
);
2109 smb
->hdr
.PidHigh
= cpu_to_le16((__u16
)(wdata
->pid
>> 16));
2111 smb
->AndXCommand
= 0xFF; /* none */
2112 smb
->Fid
= wdata
->cfile
->fid
.netfid
;
2113 smb
->OffsetLow
= cpu_to_le32(wdata
->offset
& 0xFFFFFFFF);
2115 smb
->OffsetHigh
= cpu_to_le32(wdata
->offset
>> 32);
2116 smb
->Reserved
= 0xFFFFFFFF;
2121 cpu_to_le16(offsetof(struct smb_com_write_req
, Data
) - 4);
2123 /* 4 for RFC1001 length + 1 for BCC */
2124 iov
.iov_len
= be32_to_cpu(smb
->hdr
.smb_buf_length
) + 4 + 1;
2129 rqst
.rq_pages
= wdata
->pages
;
2130 rqst
.rq_npages
= wdata
->nr_pages
;
2131 rqst
.rq_pagesz
= wdata
->pagesz
;
2132 rqst
.rq_tailsz
= wdata
->tailsz
;
2134 cifs_dbg(FYI
, "async write at %llu %u bytes\n",
2135 wdata
->offset
, wdata
->bytes
);
2137 smb
->DataLengthLow
= cpu_to_le16(wdata
->bytes
& 0xFFFF);
2138 smb
->DataLengthHigh
= cpu_to_le16(wdata
->bytes
>> 16);
2141 inc_rfc1001_len(&smb
->hdr
, wdata
->bytes
+ 1);
2142 put_bcc(wdata
->bytes
+ 1, &smb
->hdr
);
2145 struct smb_com_writex_req
*smbw
=
2146 (struct smb_com_writex_req
*)smb
;
2147 inc_rfc1001_len(&smbw
->hdr
, wdata
->bytes
+ 5);
2148 put_bcc(wdata
->bytes
+ 5, &smbw
->hdr
);
2149 iov
.iov_len
+= 4; /* pad bigger by four bytes */
2152 kref_get(&wdata
->refcount
);
2153 rc
= cifs_call_async(tcon
->ses
->server
, &rqst
, NULL
,
2154 cifs_writev_callback
, wdata
, 0);
2157 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_writes
);
2159 kref_put(&wdata
->refcount
, release
);
2162 cifs_small_buf_release(smb
);
2167 CIFSSMBWrite2(const unsigned int xid
, struct cifs_io_parms
*io_parms
,
2168 unsigned int *nbytes
, struct kvec
*iov
, int n_vec
)
2171 WRITE_REQ
*pSMB
= NULL
;
2174 int resp_buf_type
= 0;
2175 __u32 pid
= io_parms
->pid
;
2176 __u16 netfid
= io_parms
->netfid
;
2177 __u64 offset
= io_parms
->offset
;
2178 struct cifs_tcon
*tcon
= io_parms
->tcon
;
2179 unsigned int count
= io_parms
->length
;
2183 cifs_dbg(FYI
, "write2 at %lld %d bytes\n", (long long)offset
, count
);
2185 if (tcon
->ses
->capabilities
& CAP_LARGE_FILES
) {
2189 if ((offset
>> 32) > 0) {
2190 /* can not handle big offset for old srv */
2194 rc
= small_smb_init(SMB_COM_WRITE_ANDX
, wct
, tcon
, (void **) &pSMB
);
2198 pSMB
->hdr
.Pid
= cpu_to_le16((__u16
)pid
);
2199 pSMB
->hdr
.PidHigh
= cpu_to_le16((__u16
)(pid
>> 16));
2201 /* tcon and ses pointer are checked in smb_init */
2202 if (tcon
->ses
->server
== NULL
)
2203 return -ECONNABORTED
;
2205 pSMB
->AndXCommand
= 0xFF; /* none */
2207 pSMB
->OffsetLow
= cpu_to_le32(offset
& 0xFFFFFFFF);
2209 pSMB
->OffsetHigh
= cpu_to_le32(offset
>> 32);
2210 pSMB
->Reserved
= 0xFFFFFFFF;
2211 pSMB
->WriteMode
= 0;
2212 pSMB
->Remaining
= 0;
2215 cpu_to_le16(offsetof(struct smb_com_write_req
, Data
) - 4);
2217 pSMB
->DataLengthLow
= cpu_to_le16(count
& 0xFFFF);
2218 pSMB
->DataLengthHigh
= cpu_to_le16(count
>> 16);
2219 /* header + 1 byte pad */
2220 smb_hdr_len
= be32_to_cpu(pSMB
->hdr
.smb_buf_length
) + 1;
2222 inc_rfc1001_len(pSMB
, count
+ 1);
2223 else /* wct == 12 */
2224 inc_rfc1001_len(pSMB
, count
+ 5); /* smb data starts later */
2226 pSMB
->ByteCount
= cpu_to_le16(count
+ 1);
2227 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
2228 struct smb_com_writex_req
*pSMBW
=
2229 (struct smb_com_writex_req
*)pSMB
;
2230 pSMBW
->ByteCount
= cpu_to_le16(count
+ 5);
2232 iov
[0].iov_base
= pSMB
;
2234 iov
[0].iov_len
= smb_hdr_len
+ 4;
2235 else /* wct == 12 pad bigger by four bytes */
2236 iov
[0].iov_len
= smb_hdr_len
+ 8;
2239 rc
= SendReceive2(xid
, tcon
->ses
, iov
, n_vec
+ 1, &resp_buf_type
, 0);
2240 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_writes
);
2242 cifs_dbg(FYI
, "Send error Write2 = %d\n", rc
);
2243 } else if (resp_buf_type
== 0) {
2244 /* presumably this can not happen, but best to be safe */
2247 WRITE_RSP
*pSMBr
= (WRITE_RSP
*)iov
[0].iov_base
;
2248 *nbytes
= le16_to_cpu(pSMBr
->CountHigh
);
2249 *nbytes
= (*nbytes
) << 16;
2250 *nbytes
+= le16_to_cpu(pSMBr
->Count
);
2253 * Mask off high 16 bits when bytes written as returned by the
2254 * server is greater than bytes requested by the client. OS/2
2255 * servers are known to set incorrect CountHigh values.
2257 if (*nbytes
> count
)
2261 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
2262 free_rsp_buf(resp_buf_type
, iov
[0].iov_base
);
2264 /* Note: On -EAGAIN error only caller can retry on handle based calls
2265 since file handle passed in no longer valid */
2270 int cifs_lockv(const unsigned int xid
, struct cifs_tcon
*tcon
,
2271 const __u16 netfid
, const __u8 lock_type
, const __u32 num_unlock
,
2272 const __u32 num_lock
, LOCKING_ANDX_RANGE
*buf
)
2275 LOCK_REQ
*pSMB
= NULL
;
2280 cifs_dbg(FYI
, "cifs_lockv num lock %d num unlock %d\n",
2281 num_lock
, num_unlock
);
2283 rc
= small_smb_init(SMB_COM_LOCKING_ANDX
, 8, tcon
, (void **) &pSMB
);
2288 pSMB
->NumberOfLocks
= cpu_to_le16(num_lock
);
2289 pSMB
->NumberOfUnlocks
= cpu_to_le16(num_unlock
);
2290 pSMB
->LockType
= lock_type
;
2291 pSMB
->AndXCommand
= 0xFF; /* none */
2292 pSMB
->Fid
= netfid
; /* netfid stays le */
2294 count
= (num_unlock
+ num_lock
) * sizeof(LOCKING_ANDX_RANGE
);
2295 inc_rfc1001_len(pSMB
, count
);
2296 pSMB
->ByteCount
= cpu_to_le16(count
);
2298 iov
[0].iov_base
= (char *)pSMB
;
2299 iov
[0].iov_len
= be32_to_cpu(pSMB
->hdr
.smb_buf_length
) + 4 -
2300 (num_unlock
+ num_lock
) * sizeof(LOCKING_ANDX_RANGE
);
2301 iov
[1].iov_base
= (char *)buf
;
2302 iov
[1].iov_len
= (num_unlock
+ num_lock
) * sizeof(LOCKING_ANDX_RANGE
);
2304 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_locks
);
2305 rc
= SendReceive2(xid
, tcon
->ses
, iov
, 2, &resp_buf_type
, CIFS_NO_RESP
);
2307 cifs_dbg(FYI
, "Send error in cifs_lockv = %d\n", rc
);
2313 CIFSSMBLock(const unsigned int xid
, struct cifs_tcon
*tcon
,
2314 const __u16 smb_file_id
, const __u32 netpid
, const __u64 len
,
2315 const __u64 offset
, const __u32 numUnlock
,
2316 const __u32 numLock
, const __u8 lockType
,
2317 const bool waitFlag
, const __u8 oplock_level
)
2320 LOCK_REQ
*pSMB
= NULL
;
2321 /* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
2326 cifs_dbg(FYI
, "CIFSSMBLock timeout %d numLock %d\n",
2327 (int)waitFlag
, numLock
);
2328 rc
= small_smb_init(SMB_COM_LOCKING_ANDX
, 8, tcon
, (void **) &pSMB
);
2333 if (lockType
== LOCKING_ANDX_OPLOCK_RELEASE
) {
2334 /* no response expected */
2335 flags
= CIFS_ASYNC_OP
| CIFS_OBREAK_OP
;
2337 } else if (waitFlag
) {
2338 flags
= CIFS_BLOCKING_OP
; /* blocking operation, no timeout */
2339 pSMB
->Timeout
= cpu_to_le32(-1);/* blocking - do not time out */
2344 pSMB
->NumberOfLocks
= cpu_to_le16(numLock
);
2345 pSMB
->NumberOfUnlocks
= cpu_to_le16(numUnlock
);
2346 pSMB
->LockType
= lockType
;
2347 pSMB
->OplockLevel
= oplock_level
;
2348 pSMB
->AndXCommand
= 0xFF; /* none */
2349 pSMB
->Fid
= smb_file_id
; /* netfid stays le */
2351 if ((numLock
!= 0) || (numUnlock
!= 0)) {
2352 pSMB
->Locks
[0].Pid
= cpu_to_le16(netpid
);
2353 /* BB where to store pid high? */
2354 pSMB
->Locks
[0].LengthLow
= cpu_to_le32((u32
)len
);
2355 pSMB
->Locks
[0].LengthHigh
= cpu_to_le32((u32
)(len
>>32));
2356 pSMB
->Locks
[0].OffsetLow
= cpu_to_le32((u32
)offset
);
2357 pSMB
->Locks
[0].OffsetHigh
= cpu_to_le32((u32
)(offset
>>32));
2358 count
= sizeof(LOCKING_ANDX_RANGE
);
2363 inc_rfc1001_len(pSMB
, count
);
2364 pSMB
->ByteCount
= cpu_to_le16(count
);
2367 rc
= SendReceiveBlockingLock(xid
, tcon
, (struct smb_hdr
*) pSMB
,
2368 (struct smb_hdr
*) pSMB
, &bytes_returned
);
2369 cifs_small_buf_release(pSMB
);
2371 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *)pSMB
, flags
);
2372 /* SMB buffer freed by function above */
2374 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_locks
);
2376 cifs_dbg(FYI
, "Send error in Lock = %d\n", rc
);
2378 /* Note: On -EAGAIN error only caller can retry on handle based calls
2379 since file handle passed in no longer valid */
2384 CIFSSMBPosixLock(const unsigned int xid
, struct cifs_tcon
*tcon
,
2385 const __u16 smb_file_id
, const __u32 netpid
,
2386 const loff_t start_offset
, const __u64 len
,
2387 struct file_lock
*pLockData
, const __u16 lock_type
,
2388 const bool waitFlag
)
2390 struct smb_com_transaction2_sfi_req
*pSMB
= NULL
;
2391 struct smb_com_transaction2_sfi_rsp
*pSMBr
= NULL
;
2392 struct cifs_posix_lock
*parm_data
;
2395 int bytes_returned
= 0;
2396 int resp_buf_type
= 0;
2397 __u16 params
, param_offset
, offset
, byte_count
, count
;
2400 cifs_dbg(FYI
, "Posix Lock\n");
2402 rc
= small_smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
);
2407 pSMBr
= (struct smb_com_transaction2_sfi_rsp
*)pSMB
;
2410 pSMB
->MaxSetupCount
= 0;
2413 pSMB
->Reserved2
= 0;
2414 param_offset
= offsetof(struct smb_com_transaction2_sfi_req
, Fid
) - 4;
2415 offset
= param_offset
+ params
;
2417 count
= sizeof(struct cifs_posix_lock
);
2418 pSMB
->MaxParameterCount
= cpu_to_le16(2);
2419 pSMB
->MaxDataCount
= cpu_to_le16(1000); /* BB find max SMB from sess */
2420 pSMB
->SetupCount
= 1;
2421 pSMB
->Reserved3
= 0;
2423 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION
);
2425 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_FILE_INFORMATION
);
2426 byte_count
= 3 /* pad */ + params
+ count
;
2427 pSMB
->DataCount
= cpu_to_le16(count
);
2428 pSMB
->ParameterCount
= cpu_to_le16(params
);
2429 pSMB
->TotalDataCount
= pSMB
->DataCount
;
2430 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
2431 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
2432 parm_data
= (struct cifs_posix_lock
*)
2433 (((char *) &pSMB
->hdr
.Protocol
) + offset
);
2435 parm_data
->lock_type
= cpu_to_le16(lock_type
);
2437 timeout
= CIFS_BLOCKING_OP
; /* blocking operation, no timeout */
2438 parm_data
->lock_flags
= cpu_to_le16(1);
2439 pSMB
->Timeout
= cpu_to_le32(-1);
2443 parm_data
->pid
= cpu_to_le32(netpid
);
2444 parm_data
->start
= cpu_to_le64(start_offset
);
2445 parm_data
->length
= cpu_to_le64(len
); /* normalize negative numbers */
2447 pSMB
->DataOffset
= cpu_to_le16(offset
);
2448 pSMB
->Fid
= smb_file_id
;
2449 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_POSIX_LOCK
);
2450 pSMB
->Reserved4
= 0;
2451 inc_rfc1001_len(pSMB
, byte_count
);
2452 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
2454 rc
= SendReceiveBlockingLock(xid
, tcon
, (struct smb_hdr
*) pSMB
,
2455 (struct smb_hdr
*) pSMBr
, &bytes_returned
);
2457 iov
[0].iov_base
= (char *)pSMB
;
2458 iov
[0].iov_len
= be32_to_cpu(pSMB
->hdr
.smb_buf_length
) + 4;
2459 rc
= SendReceive2(xid
, tcon
->ses
, iov
, 1 /* num iovecs */,
2460 &resp_buf_type
, timeout
);
2461 pSMB
= NULL
; /* request buf already freed by SendReceive2. Do
2462 not try to free it twice below on exit */
2463 pSMBr
= (struct smb_com_transaction2_sfi_rsp
*)iov
[0].iov_base
;
2467 cifs_dbg(FYI
, "Send error in Posix Lock = %d\n", rc
);
2468 } else if (pLockData
) {
2469 /* lock structure can be returned on get */
2472 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
2474 if (rc
|| get_bcc(&pSMBr
->hdr
) < sizeof(*parm_data
)) {
2475 rc
= -EIO
; /* bad smb */
2478 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
2479 data_count
= le16_to_cpu(pSMBr
->t2
.DataCount
);
2480 if (data_count
< sizeof(struct cifs_posix_lock
)) {
2484 parm_data
= (struct cifs_posix_lock
*)
2485 ((char *)&pSMBr
->hdr
.Protocol
+ data_offset
);
2486 if (parm_data
->lock_type
== cpu_to_le16(CIFS_UNLCK
))
2487 pLockData
->fl_type
= F_UNLCK
;
2489 if (parm_data
->lock_type
==
2490 cpu_to_le16(CIFS_RDLCK
))
2491 pLockData
->fl_type
= F_RDLCK
;
2492 else if (parm_data
->lock_type
==
2493 cpu_to_le16(CIFS_WRLCK
))
2494 pLockData
->fl_type
= F_WRLCK
;
2496 pLockData
->fl_start
= le64_to_cpu(parm_data
->start
);
2497 pLockData
->fl_end
= pLockData
->fl_start
+
2498 le64_to_cpu(parm_data
->length
) - 1;
2499 pLockData
->fl_pid
= le32_to_cpu(parm_data
->pid
);
2505 cifs_small_buf_release(pSMB
);
2507 free_rsp_buf(resp_buf_type
, iov
[0].iov_base
);
2509 /* Note: On -EAGAIN error only caller can retry on handle based calls
2510 since file handle passed in no longer valid */
2517 CIFSSMBClose(const unsigned int xid
, struct cifs_tcon
*tcon
, int smb_file_id
)
2520 CLOSE_REQ
*pSMB
= NULL
;
2521 cifs_dbg(FYI
, "In CIFSSMBClose\n");
2523 /* do not retry on dead session on close */
2524 rc
= small_smb_init(SMB_COM_CLOSE
, 3, tcon
, (void **) &pSMB
);
2530 pSMB
->FileID
= (__u16
) smb_file_id
;
2531 pSMB
->LastWriteTime
= 0xFFFFFFFF;
2532 pSMB
->ByteCount
= 0;
2533 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) pSMB
, 0);
2534 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_closes
);
2537 /* EINTR is expected when user ctl-c to kill app */
2538 cifs_dbg(VFS
, "Send error in Close = %d\n", rc
);
2542 /* Since session is dead, file will be closed on server already */
2550 CIFSSMBFlush(const unsigned int xid
, struct cifs_tcon
*tcon
, int smb_file_id
)
2553 FLUSH_REQ
*pSMB
= NULL
;
2554 cifs_dbg(FYI
, "In CIFSSMBFlush\n");
2556 rc
= small_smb_init(SMB_COM_FLUSH
, 1, tcon
, (void **) &pSMB
);
2560 pSMB
->FileID
= (__u16
) smb_file_id
;
2561 pSMB
->ByteCount
= 0;
2562 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) pSMB
, 0);
2563 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_flushes
);
2565 cifs_dbg(VFS
, "Send error in Flush = %d\n", rc
);
2571 CIFSSMBRename(const unsigned int xid
, struct cifs_tcon
*tcon
,
2572 const char *from_name
, const char *to_name
,
2573 struct cifs_sb_info
*cifs_sb
)
2576 RENAME_REQ
*pSMB
= NULL
;
2577 RENAME_RSP
*pSMBr
= NULL
;
2579 int name_len
, name_len2
;
2581 int remap
= cifs_remap(cifs_sb
);
2583 cifs_dbg(FYI
, "In CIFSSMBRename\n");
2585 rc
= smb_init(SMB_COM_RENAME
, 1, tcon
, (void **) &pSMB
,
2590 pSMB
->BufferFormat
= 0x04;
2591 pSMB
->SearchAttributes
=
2592 cpu_to_le16(ATTR_READONLY
| ATTR_HIDDEN
| ATTR_SYSTEM
|
2595 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
2596 name_len
= cifsConvertToUTF16((__le16
*) pSMB
->OldFileName
,
2597 from_name
, PATH_MAX
,
2598 cifs_sb
->local_nls
, remap
);
2599 name_len
++; /* trailing null */
2601 pSMB
->OldFileName
[name_len
] = 0x04; /* pad */
2602 /* protocol requires ASCII signature byte on Unicode string */
2603 pSMB
->OldFileName
[name_len
+ 1] = 0x00;
2605 cifsConvertToUTF16((__le16
*)&pSMB
->OldFileName
[name_len
+2],
2606 to_name
, PATH_MAX
, cifs_sb
->local_nls
,
2608 name_len2
+= 1 /* trailing null */ + 1 /* Signature word */ ;
2609 name_len2
*= 2; /* convert to bytes */
2610 } else { /* BB improve the check for buffer overruns BB */
2611 name_len
= strnlen(from_name
, PATH_MAX
);
2612 name_len
++; /* trailing null */
2613 strncpy(pSMB
->OldFileName
, from_name
, name_len
);
2614 name_len2
= strnlen(to_name
, PATH_MAX
);
2615 name_len2
++; /* trailing null */
2616 pSMB
->OldFileName
[name_len
] = 0x04; /* 2nd buffer format */
2617 strncpy(&pSMB
->OldFileName
[name_len
+ 1], to_name
, name_len2
);
2618 name_len2
++; /* trailing null */
2619 name_len2
++; /* signature byte */
2622 count
= 1 /* 1st signature byte */ + name_len
+ name_len2
;
2623 inc_rfc1001_len(pSMB
, count
);
2624 pSMB
->ByteCount
= cpu_to_le16(count
);
2626 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
2627 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
2628 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_renames
);
2630 cifs_dbg(FYI
, "Send error in rename = %d\n", rc
);
2632 cifs_buf_release(pSMB
);
2640 int CIFSSMBRenameOpenFile(const unsigned int xid
, struct cifs_tcon
*pTcon
,
2641 int netfid
, const char *target_name
,
2642 const struct nls_table
*nls_codepage
, int remap
)
2644 struct smb_com_transaction2_sfi_req
*pSMB
= NULL
;
2645 struct smb_com_transaction2_sfi_rsp
*pSMBr
= NULL
;
2646 struct set_file_rename
*rename_info
;
2648 char dummy_string
[30];
2650 int bytes_returned
= 0;
2652 __u16 params
, param_offset
, offset
, count
, byte_count
;
2654 cifs_dbg(FYI
, "Rename to File by handle\n");
2655 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, pTcon
, (void **) &pSMB
,
2661 pSMB
->MaxSetupCount
= 0;
2665 pSMB
->Reserved2
= 0;
2666 param_offset
= offsetof(struct smb_com_transaction2_sfi_req
, Fid
) - 4;
2667 offset
= param_offset
+ params
;
2669 data_offset
= (char *) (&pSMB
->hdr
.Protocol
) + offset
;
2670 rename_info
= (struct set_file_rename
*) data_offset
;
2671 pSMB
->MaxParameterCount
= cpu_to_le16(2);
2672 pSMB
->MaxDataCount
= cpu_to_le16(1000); /* BB find max SMB from sess */
2673 pSMB
->SetupCount
= 1;
2674 pSMB
->Reserved3
= 0;
2675 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_FILE_INFORMATION
);
2676 byte_count
= 3 /* pad */ + params
;
2677 pSMB
->ParameterCount
= cpu_to_le16(params
);
2678 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
2679 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
2680 pSMB
->DataOffset
= cpu_to_le16(offset
);
2681 /* construct random name ".cifs_tmp<inodenum><mid>" */
2682 rename_info
->overwrite
= cpu_to_le32(1);
2683 rename_info
->root_fid
= 0;
2684 /* unicode only call */
2685 if (target_name
== NULL
) {
2686 sprintf(dummy_string
, "cifs%x", pSMB
->hdr
.Mid
);
2688 cifsConvertToUTF16((__le16
*)rename_info
->target_name
,
2689 dummy_string
, 24, nls_codepage
, remap
);
2692 cifsConvertToUTF16((__le16
*)rename_info
->target_name
,
2693 target_name
, PATH_MAX
, nls_codepage
,
2696 rename_info
->target_name_len
= cpu_to_le32(2 * len_of_str
);
2697 count
= 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str
);
2698 byte_count
+= count
;
2699 pSMB
->DataCount
= cpu_to_le16(count
);
2700 pSMB
->TotalDataCount
= pSMB
->DataCount
;
2702 pSMB
->InformationLevel
=
2703 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION
);
2704 pSMB
->Reserved4
= 0;
2705 inc_rfc1001_len(pSMB
, byte_count
);
2706 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
2707 rc
= SendReceive(xid
, pTcon
->ses
, (struct smb_hdr
*) pSMB
,
2708 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
2709 cifs_stats_inc(&pTcon
->stats
.cifs_stats
.num_t2renames
);
2711 cifs_dbg(FYI
, "Send error in Rename (by file handle) = %d\n",
2714 cifs_buf_release(pSMB
);
2716 /* Note: On -EAGAIN error only caller can retry on handle based calls
2717 since file handle passed in no longer valid */
2723 CIFSSMBCopy(const unsigned int xid
, struct cifs_tcon
*tcon
,
2724 const char *fromName
, const __u16 target_tid
, const char *toName
,
2725 const int flags
, const struct nls_table
*nls_codepage
, int remap
)
2728 COPY_REQ
*pSMB
= NULL
;
2729 COPY_RSP
*pSMBr
= NULL
;
2731 int name_len
, name_len2
;
2734 cifs_dbg(FYI
, "In CIFSSMBCopy\n");
2736 rc
= smb_init(SMB_COM_COPY
, 1, tcon
, (void **) &pSMB
,
2741 pSMB
->BufferFormat
= 0x04;
2742 pSMB
->Tid2
= target_tid
;
2744 pSMB
->Flags
= cpu_to_le16(flags
& COPY_TREE
);
2746 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
2747 name_len
= cifsConvertToUTF16((__le16
*) pSMB
->OldFileName
,
2748 fromName
, PATH_MAX
, nls_codepage
,
2750 name_len
++; /* trailing null */
2752 pSMB
->OldFileName
[name_len
] = 0x04; /* pad */
2753 /* protocol requires ASCII signature byte on Unicode string */
2754 pSMB
->OldFileName
[name_len
+ 1] = 0x00;
2756 cifsConvertToUTF16((__le16
*)&pSMB
->OldFileName
[name_len
+2],
2757 toName
, PATH_MAX
, nls_codepage
, remap
);
2758 name_len2
+= 1 /* trailing null */ + 1 /* Signature word */ ;
2759 name_len2
*= 2; /* convert to bytes */
2760 } else { /* BB improve the check for buffer overruns BB */
2761 name_len
= strnlen(fromName
, PATH_MAX
);
2762 name_len
++; /* trailing null */
2763 strncpy(pSMB
->OldFileName
, fromName
, name_len
);
2764 name_len2
= strnlen(toName
, PATH_MAX
);
2765 name_len2
++; /* trailing null */
2766 pSMB
->OldFileName
[name_len
] = 0x04; /* 2nd buffer format */
2767 strncpy(&pSMB
->OldFileName
[name_len
+ 1], toName
, name_len2
);
2768 name_len2
++; /* trailing null */
2769 name_len2
++; /* signature byte */
2772 count
= 1 /* 1st signature byte */ + name_len
+ name_len2
;
2773 inc_rfc1001_len(pSMB
, count
);
2774 pSMB
->ByteCount
= cpu_to_le16(count
);
2776 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
2777 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
2779 cifs_dbg(FYI
, "Send error in copy = %d with %d files copied\n",
2780 rc
, le16_to_cpu(pSMBr
->CopyCount
));
2782 cifs_buf_release(pSMB
);
2791 CIFSUnixCreateSymLink(const unsigned int xid
, struct cifs_tcon
*tcon
,
2792 const char *fromName
, const char *toName
,
2793 const struct nls_table
*nls_codepage
, int remap
)
2795 TRANSACTION2_SPI_REQ
*pSMB
= NULL
;
2796 TRANSACTION2_SPI_RSP
*pSMBr
= NULL
;
2799 int name_len_target
;
2801 int bytes_returned
= 0;
2802 __u16 params
, param_offset
, offset
, byte_count
;
2804 cifs_dbg(FYI
, "In Symlink Unix style\n");
2806 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
2811 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
2813 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, fromName
,
2814 /* find define for this maxpathcomponent */
2815 PATH_MAX
, nls_codepage
, remap
);
2816 name_len
++; /* trailing null */
2819 } else { /* BB improve the check for buffer overruns BB */
2820 name_len
= strnlen(fromName
, PATH_MAX
);
2821 name_len
++; /* trailing null */
2822 strncpy(pSMB
->FileName
, fromName
, name_len
);
2824 params
= 6 + name_len
;
2825 pSMB
->MaxSetupCount
= 0;
2829 pSMB
->Reserved2
= 0;
2830 param_offset
= offsetof(struct smb_com_transaction2_spi_req
,
2831 InformationLevel
) - 4;
2832 offset
= param_offset
+ params
;
2834 data_offset
= (char *) (&pSMB
->hdr
.Protocol
) + offset
;
2835 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
2837 cifsConvertToUTF16((__le16
*) data_offset
, toName
,
2838 /* find define for this maxpathcomponent */
2839 PATH_MAX
, nls_codepage
, remap
);
2840 name_len_target
++; /* trailing null */
2841 name_len_target
*= 2;
2842 } else { /* BB improve the check for buffer overruns BB */
2843 name_len_target
= strnlen(toName
, PATH_MAX
);
2844 name_len_target
++; /* trailing null */
2845 strncpy(data_offset
, toName
, name_len_target
);
2848 pSMB
->MaxParameterCount
= cpu_to_le16(2);
2849 /* BB find exact max on data count below from sess */
2850 pSMB
->MaxDataCount
= cpu_to_le16(1000);
2851 pSMB
->SetupCount
= 1;
2852 pSMB
->Reserved3
= 0;
2853 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_PATH_INFORMATION
);
2854 byte_count
= 3 /* pad */ + params
+ name_len_target
;
2855 pSMB
->DataCount
= cpu_to_le16(name_len_target
);
2856 pSMB
->ParameterCount
= cpu_to_le16(params
);
2857 pSMB
->TotalDataCount
= pSMB
->DataCount
;
2858 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
2859 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
2860 pSMB
->DataOffset
= cpu_to_le16(offset
);
2861 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_FILE_UNIX_LINK
);
2862 pSMB
->Reserved4
= 0;
2863 inc_rfc1001_len(pSMB
, byte_count
);
2864 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
2865 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
2866 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
2867 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_symlinks
);
2869 cifs_dbg(FYI
, "Send error in SetPathInfo create symlink = %d\n",
2872 cifs_buf_release(pSMB
);
2875 goto createSymLinkRetry
;
2881 CIFSUnixCreateHardLink(const unsigned int xid
, struct cifs_tcon
*tcon
,
2882 const char *fromName
, const char *toName
,
2883 const struct nls_table
*nls_codepage
, int remap
)
2885 TRANSACTION2_SPI_REQ
*pSMB
= NULL
;
2886 TRANSACTION2_SPI_RSP
*pSMBr
= NULL
;
2889 int name_len_target
;
2891 int bytes_returned
= 0;
2892 __u16 params
, param_offset
, offset
, byte_count
;
2894 cifs_dbg(FYI
, "In Create Hard link Unix style\n");
2895 createHardLinkRetry
:
2896 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
2901 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
2902 name_len
= cifsConvertToUTF16((__le16
*) pSMB
->FileName
, toName
,
2903 PATH_MAX
, nls_codepage
, remap
);
2904 name_len
++; /* trailing null */
2907 } else { /* BB improve the check for buffer overruns BB */
2908 name_len
= strnlen(toName
, PATH_MAX
);
2909 name_len
++; /* trailing null */
2910 strncpy(pSMB
->FileName
, toName
, name_len
);
2912 params
= 6 + name_len
;
2913 pSMB
->MaxSetupCount
= 0;
2917 pSMB
->Reserved2
= 0;
2918 param_offset
= offsetof(struct smb_com_transaction2_spi_req
,
2919 InformationLevel
) - 4;
2920 offset
= param_offset
+ params
;
2922 data_offset
= (char *) (&pSMB
->hdr
.Protocol
) + offset
;
2923 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
2925 cifsConvertToUTF16((__le16
*) data_offset
, fromName
,
2926 PATH_MAX
, nls_codepage
, remap
);
2927 name_len_target
++; /* trailing null */
2928 name_len_target
*= 2;
2929 } else { /* BB improve the check for buffer overruns BB */
2930 name_len_target
= strnlen(fromName
, PATH_MAX
);
2931 name_len_target
++; /* trailing null */
2932 strncpy(data_offset
, fromName
, name_len_target
);
2935 pSMB
->MaxParameterCount
= cpu_to_le16(2);
2936 /* BB find exact max on data count below from sess*/
2937 pSMB
->MaxDataCount
= cpu_to_le16(1000);
2938 pSMB
->SetupCount
= 1;
2939 pSMB
->Reserved3
= 0;
2940 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_PATH_INFORMATION
);
2941 byte_count
= 3 /* pad */ + params
+ name_len_target
;
2942 pSMB
->ParameterCount
= cpu_to_le16(params
);
2943 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
2944 pSMB
->DataCount
= cpu_to_le16(name_len_target
);
2945 pSMB
->TotalDataCount
= pSMB
->DataCount
;
2946 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
2947 pSMB
->DataOffset
= cpu_to_le16(offset
);
2948 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_FILE_UNIX_HLINK
);
2949 pSMB
->Reserved4
= 0;
2950 inc_rfc1001_len(pSMB
, byte_count
);
2951 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
2952 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
2953 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
2954 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_hardlinks
);
2956 cifs_dbg(FYI
, "Send error in SetPathInfo (hard link) = %d\n",
2959 cifs_buf_release(pSMB
);
2961 goto createHardLinkRetry
;
2967 CIFSCreateHardLink(const unsigned int xid
, struct cifs_tcon
*tcon
,
2968 const char *from_name
, const char *to_name
,
2969 struct cifs_sb_info
*cifs_sb
)
2972 NT_RENAME_REQ
*pSMB
= NULL
;
2973 RENAME_RSP
*pSMBr
= NULL
;
2975 int name_len
, name_len2
;
2977 int remap
= cifs_remap(cifs_sb
);
2979 cifs_dbg(FYI
, "In CIFSCreateHardLink\n");
2980 winCreateHardLinkRetry
:
2982 rc
= smb_init(SMB_COM_NT_RENAME
, 4, tcon
, (void **) &pSMB
,
2987 pSMB
->SearchAttributes
=
2988 cpu_to_le16(ATTR_READONLY
| ATTR_HIDDEN
| ATTR_SYSTEM
|
2990 pSMB
->Flags
= cpu_to_le16(CREATE_HARD_LINK
);
2991 pSMB
->ClusterCount
= 0;
2993 pSMB
->BufferFormat
= 0x04;
2995 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
2997 cifsConvertToUTF16((__le16
*) pSMB
->OldFileName
, from_name
,
2998 PATH_MAX
, cifs_sb
->local_nls
, remap
);
2999 name_len
++; /* trailing null */
3002 /* protocol specifies ASCII buffer format (0x04) for unicode */
3003 pSMB
->OldFileName
[name_len
] = 0x04;
3004 pSMB
->OldFileName
[name_len
+ 1] = 0x00; /* pad */
3006 cifsConvertToUTF16((__le16
*)&pSMB
->OldFileName
[name_len
+2],
3007 to_name
, PATH_MAX
, cifs_sb
->local_nls
,
3009 name_len2
+= 1 /* trailing null */ + 1 /* Signature word */ ;
3010 name_len2
*= 2; /* convert to bytes */
3011 } else { /* BB improve the check for buffer overruns BB */
3012 name_len
= strnlen(from_name
, PATH_MAX
);
3013 name_len
++; /* trailing null */
3014 strncpy(pSMB
->OldFileName
, from_name
, name_len
);
3015 name_len2
= strnlen(to_name
, PATH_MAX
);
3016 name_len2
++; /* trailing null */
3017 pSMB
->OldFileName
[name_len
] = 0x04; /* 2nd buffer format */
3018 strncpy(&pSMB
->OldFileName
[name_len
+ 1], to_name
, name_len2
);
3019 name_len2
++; /* trailing null */
3020 name_len2
++; /* signature byte */
3023 count
= 1 /* string type byte */ + name_len
+ name_len2
;
3024 inc_rfc1001_len(pSMB
, count
);
3025 pSMB
->ByteCount
= cpu_to_le16(count
);
3027 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
3028 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
3029 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_hardlinks
);
3031 cifs_dbg(FYI
, "Send error in hard link (NT rename) = %d\n", rc
);
3033 cifs_buf_release(pSMB
);
3035 goto winCreateHardLinkRetry
;
3041 CIFSSMBUnixQuerySymLink(const unsigned int xid
, struct cifs_tcon
*tcon
,
3042 const unsigned char *searchName
, char **symlinkinfo
,
3043 const struct nls_table
*nls_codepage
, int remap
)
3045 /* SMB_QUERY_FILE_UNIX_LINK */
3046 TRANSACTION2_QPI_REQ
*pSMB
= NULL
;
3047 TRANSACTION2_QPI_RSP
*pSMBr
= NULL
;
3051 __u16 params
, byte_count
;
3054 cifs_dbg(FYI
, "In QPathSymLinkInfo (Unix) for path %s\n", searchName
);
3057 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
3062 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
3064 cifsConvertToUTF16((__le16
*) pSMB
->FileName
,
3065 searchName
, PATH_MAX
, nls_codepage
,
3067 name_len
++; /* trailing null */
3069 } else { /* BB improve the check for buffer overruns BB */
3070 name_len
= strnlen(searchName
, PATH_MAX
);
3071 name_len
++; /* trailing null */
3072 strncpy(pSMB
->FileName
, searchName
, name_len
);
3075 params
= 2 /* level */ + 4 /* rsrvd */ + name_len
/* incl null */ ;
3076 pSMB
->TotalDataCount
= 0;
3077 pSMB
->MaxParameterCount
= cpu_to_le16(2);
3078 pSMB
->MaxDataCount
= cpu_to_le16(CIFSMaxBufSize
);
3079 pSMB
->MaxSetupCount
= 0;
3083 pSMB
->Reserved2
= 0;
3084 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
3085 struct smb_com_transaction2_qpi_req
, InformationLevel
) - 4);
3086 pSMB
->DataCount
= 0;
3087 pSMB
->DataOffset
= 0;
3088 pSMB
->SetupCount
= 1;
3089 pSMB
->Reserved3
= 0;
3090 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION
);
3091 byte_count
= params
+ 1 /* pad */ ;
3092 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
3093 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
3094 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK
);
3095 pSMB
->Reserved4
= 0;
3096 inc_rfc1001_len(pSMB
, byte_count
);
3097 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
3099 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
3100 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
3102 cifs_dbg(FYI
, "Send error in QuerySymLinkInfo = %d\n", rc
);
3104 /* decode response */
3106 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
3107 /* BB also check enough total bytes returned */
3108 if (rc
|| get_bcc(&pSMBr
->hdr
) < 2)
3112 u16 count
= le16_to_cpu(pSMBr
->t2
.DataCount
);
3114 data_start
= ((char *) &pSMBr
->hdr
.Protocol
) +
3115 le16_to_cpu(pSMBr
->t2
.DataOffset
);
3117 if (pSMBr
->hdr
.Flags2
& SMBFLG2_UNICODE
)
3122 /* BB FIXME investigate remapping reserved chars here */
3123 *symlinkinfo
= cifs_strndup_from_utf16(data_start
,
3124 count
, is_unicode
, nls_codepage
);
3129 cifs_buf_release(pSMB
);
3131 goto querySymLinkRetry
;
3136 * Recent Windows versions now create symlinks more frequently
3137 * and they use the "reparse point" mechanism below. We can of course
3138 * do symlinks nicely to Samba and other servers which support the
3139 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3140 * "MF" symlinks optionally, but for recent Windows we really need to
3141 * reenable the code below and fix the cifs_symlink callers to handle this.
3142 * In the interim this code has been moved to its own config option so
3143 * it is not compiled in by default until callers fixed up and more tested.
3146 CIFSSMBQuerySymLink(const unsigned int xid
, struct cifs_tcon
*tcon
,
3147 __u16 fid
, char **symlinkinfo
,
3148 const struct nls_table
*nls_codepage
)
3152 struct smb_com_transaction_ioctl_req
*pSMB
;
3153 struct smb_com_transaction_ioctl_rsp
*pSMBr
;
3155 unsigned int sub_len
;
3157 struct reparse_symlink_data
*reparse_buf
;
3158 struct reparse_posix_data
*posix_buf
;
3159 __u32 data_offset
, data_count
;
3162 cifs_dbg(FYI
, "In Windows reparse style QueryLink for fid %u\n", fid
);
3163 rc
= smb_init(SMB_COM_NT_TRANSACT
, 23, tcon
, (void **) &pSMB
,
3168 pSMB
->TotalParameterCount
= 0 ;
3169 pSMB
->TotalDataCount
= 0;
3170 pSMB
->MaxParameterCount
= cpu_to_le32(2);
3171 /* BB find exact data count max from sess structure BB */
3172 pSMB
->MaxDataCount
= cpu_to_le32(CIFSMaxBufSize
& 0xFFFFFF00);
3173 pSMB
->MaxSetupCount
= 4;
3175 pSMB
->ParameterOffset
= 0;
3176 pSMB
->DataCount
= 0;
3177 pSMB
->DataOffset
= 0;
3178 pSMB
->SetupCount
= 4;
3179 pSMB
->SubCommand
= cpu_to_le16(NT_TRANSACT_IOCTL
);
3180 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
3181 pSMB
->FunctionCode
= cpu_to_le32(FSCTL_GET_REPARSE_POINT
);
3182 pSMB
->IsFsctl
= 1; /* FSCTL */
3183 pSMB
->IsRootFlag
= 0;
3184 pSMB
->Fid
= fid
; /* file handle always le */
3185 pSMB
->ByteCount
= 0;
3187 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
3188 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
3190 cifs_dbg(FYI
, "Send error in QueryReparseLinkInfo = %d\n", rc
);
3194 data_offset
= le32_to_cpu(pSMBr
->DataOffset
);
3195 data_count
= le32_to_cpu(pSMBr
->DataCount
);
3196 if (get_bcc(&pSMBr
->hdr
) < 2 || data_offset
> 512) {
3197 /* BB also check enough total bytes returned */
3198 rc
= -EIO
; /* bad smb */
3201 if (!data_count
|| (data_count
> 2048)) {
3203 cifs_dbg(FYI
, "Invalid return data count on get reparse info ioctl\n");
3206 end_of_smb
= 2 + get_bcc(&pSMBr
->hdr
) + (char *)&pSMBr
->ByteCount
;
3207 reparse_buf
= (struct reparse_symlink_data
*)
3208 ((char *)&pSMBr
->hdr
.Protocol
+ data_offset
);
3209 if ((char *)reparse_buf
>= end_of_smb
) {
3213 if (reparse_buf
->ReparseTag
== cpu_to_le32(IO_REPARSE_TAG_NFS
)) {
3214 cifs_dbg(FYI
, "NFS style reparse tag\n");
3215 posix_buf
= (struct reparse_posix_data
*)reparse_buf
;
3217 if (posix_buf
->InodeType
!= cpu_to_le64(NFS_SPECFILE_LNK
)) {
3218 cifs_dbg(FYI
, "unsupported file type 0x%llx\n",
3219 le64_to_cpu(posix_buf
->InodeType
));
3224 sub_len
= le16_to_cpu(reparse_buf
->ReparseDataLength
);
3225 if (posix_buf
->PathBuffer
+ sub_len
> end_of_smb
) {
3226 cifs_dbg(FYI
, "reparse buf beyond SMB\n");
3230 *symlinkinfo
= cifs_strndup_from_utf16(posix_buf
->PathBuffer
,
3231 sub_len
, is_unicode
, nls_codepage
);
3233 } else if (reparse_buf
->ReparseTag
!=
3234 cpu_to_le32(IO_REPARSE_TAG_SYMLINK
)) {
3239 /* Reparse tag is NTFS symlink */
3240 sub_start
= le16_to_cpu(reparse_buf
->SubstituteNameOffset
) +
3241 reparse_buf
->PathBuffer
;
3242 sub_len
= le16_to_cpu(reparse_buf
->SubstituteNameLength
);
3243 if (sub_start
+ sub_len
> end_of_smb
) {
3244 cifs_dbg(FYI
, "reparse buf beyond SMB\n");
3248 if (pSMBr
->hdr
.Flags2
& SMBFLG2_UNICODE
)
3253 /* BB FIXME investigate remapping reserved chars here */
3254 *symlinkinfo
= cifs_strndup_from_utf16(sub_start
, sub_len
, is_unicode
,
3259 cifs_buf_release(pSMB
);
3262 * Note: On -EAGAIN error only caller can retry on handle based calls
3263 * since file handle passed in no longer valid.
3269 CIFSSMB_set_compression(const unsigned int xid
, struct cifs_tcon
*tcon
,
3274 struct smb_com_transaction_compr_ioctl_req
*pSMB
;
3275 struct smb_com_transaction_ioctl_rsp
*pSMBr
;
3277 cifs_dbg(FYI
, "Set compression for %u\n", fid
);
3278 rc
= smb_init(SMB_COM_NT_TRANSACT
, 23, tcon
, (void **) &pSMB
,
3283 pSMB
->compression_state
= cpu_to_le16(COMPRESSION_FORMAT_DEFAULT
);
3285 pSMB
->TotalParameterCount
= 0;
3286 pSMB
->TotalDataCount
= cpu_to_le32(2);
3287 pSMB
->MaxParameterCount
= 0;
3288 pSMB
->MaxDataCount
= 0;
3289 pSMB
->MaxSetupCount
= 4;
3291 pSMB
->ParameterOffset
= 0;
3292 pSMB
->DataCount
= cpu_to_le32(2);
3294 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req
,
3295 compression_state
) - 4); /* 84 */
3296 pSMB
->SetupCount
= 4;
3297 pSMB
->SubCommand
= cpu_to_le16(NT_TRANSACT_IOCTL
);
3298 pSMB
->ParameterCount
= 0;
3299 pSMB
->FunctionCode
= cpu_to_le32(FSCTL_SET_COMPRESSION
);
3300 pSMB
->IsFsctl
= 1; /* FSCTL */
3301 pSMB
->IsRootFlag
= 0;
3302 pSMB
->Fid
= fid
; /* file handle always le */
3303 /* 3 byte pad, followed by 2 byte compress state */
3304 pSMB
->ByteCount
= cpu_to_le16(5);
3305 inc_rfc1001_len(pSMB
, 5);
3307 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
3308 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
3310 cifs_dbg(FYI
, "Send error in SetCompression = %d\n", rc
);
3312 cifs_buf_release(pSMB
);
3315 * Note: On -EAGAIN error only caller can retry on handle based calls
3316 * since file handle passed in no longer valid.
3322 #ifdef CONFIG_CIFS_POSIX
3324 /*Convert an Access Control Entry from wire format to local POSIX xattr format*/
3325 static void cifs_convert_ace(posix_acl_xattr_entry
*ace
,
3326 struct cifs_posix_ace
*cifs_ace
)
3328 /* u8 cifs fields do not need le conversion */
3329 ace
->e_perm
= cpu_to_le16(cifs_ace
->cifs_e_perm
);
3330 ace
->e_tag
= cpu_to_le16(cifs_ace
->cifs_e_tag
);
3331 ace
->e_id
= cpu_to_le32(le64_to_cpu(cifs_ace
->cifs_uid
));
3333 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3334 ace->e_perm, ace->e_tag, ace->e_id);
3340 /* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
3341 static int cifs_copy_posix_acl(char *trgt
, char *src
, const int buflen
,
3342 const int acl_type
, const int size_of_data_area
)
3347 struct cifs_posix_ace
*pACE
;
3348 struct cifs_posix_acl
*cifs_acl
= (struct cifs_posix_acl
*)src
;
3349 posix_acl_xattr_header
*local_acl
= (posix_acl_xattr_header
*)trgt
;
3351 if (le16_to_cpu(cifs_acl
->version
) != CIFS_ACL_VERSION
)
3354 if (acl_type
& ACL_TYPE_ACCESS
) {
3355 count
= le16_to_cpu(cifs_acl
->access_entry_count
);
3356 pACE
= &cifs_acl
->ace_array
[0];
3357 size
= sizeof(struct cifs_posix_acl
);
3358 size
+= sizeof(struct cifs_posix_ace
) * count
;
3359 /* check if we would go beyond end of SMB */
3360 if (size_of_data_area
< size
) {
3361 cifs_dbg(FYI
, "bad CIFS POSIX ACL size %d vs. %d\n",
3362 size_of_data_area
, size
);
3365 } else if (acl_type
& ACL_TYPE_DEFAULT
) {
3366 count
= le16_to_cpu(cifs_acl
->access_entry_count
);
3367 size
= sizeof(struct cifs_posix_acl
);
3368 size
+= sizeof(struct cifs_posix_ace
) * count
;
3369 /* skip past access ACEs to get to default ACEs */
3370 pACE
= &cifs_acl
->ace_array
[count
];
3371 count
= le16_to_cpu(cifs_acl
->default_entry_count
);
3372 size
+= sizeof(struct cifs_posix_ace
) * count
;
3373 /* check if we would go beyond end of SMB */
3374 if (size_of_data_area
< size
)
3381 size
= posix_acl_xattr_size(count
);
3382 if ((buflen
== 0) || (local_acl
== NULL
)) {
3383 /* used to query ACL EA size */
3384 } else if (size
> buflen
) {
3386 } else /* buffer big enough */ {
3387 local_acl
->a_version
= cpu_to_le32(POSIX_ACL_XATTR_VERSION
);
3388 for (i
= 0; i
< count
; i
++) {
3389 cifs_convert_ace(&local_acl
->a_entries
[i
], pACE
);
3396 static __u16
convert_ace_to_cifs_ace(struct cifs_posix_ace
*cifs_ace
,
3397 const posix_acl_xattr_entry
*local_ace
)
3399 __u16 rc
= 0; /* 0 = ACL converted ok */
3401 cifs_ace
->cifs_e_perm
= le16_to_cpu(local_ace
->e_perm
);
3402 cifs_ace
->cifs_e_tag
= le16_to_cpu(local_ace
->e_tag
);
3403 /* BB is there a better way to handle the large uid? */
3404 if (local_ace
->e_id
== cpu_to_le32(-1)) {
3405 /* Probably no need to le convert -1 on any arch but can not hurt */
3406 cifs_ace
->cifs_uid
= cpu_to_le64(-1);
3408 cifs_ace
->cifs_uid
= cpu_to_le64(le32_to_cpu(local_ace
->e_id
));
3410 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3411 ace->e_perm, ace->e_tag, ace->e_id);
3416 /* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
3417 static __u16
ACL_to_cifs_posix(char *parm_data
, const char *pACL
,
3418 const int buflen
, const int acl_type
)
3421 struct cifs_posix_acl
*cifs_acl
= (struct cifs_posix_acl
*)parm_data
;
3422 posix_acl_xattr_header
*local_acl
= (posix_acl_xattr_header
*)pACL
;
3426 if ((buflen
== 0) || (pACL
== NULL
) || (cifs_acl
== NULL
))
3429 count
= posix_acl_xattr_count((size_t)buflen
);
3430 cifs_dbg(FYI
, "setting acl with %d entries from buf of length %d and version of %d\n",
3431 count
, buflen
, le32_to_cpu(local_acl
->a_version
));
3432 if (le32_to_cpu(local_acl
->a_version
) != 2) {
3433 cifs_dbg(FYI
, "unknown POSIX ACL version %d\n",
3434 le32_to_cpu(local_acl
->a_version
));
3437 cifs_acl
->version
= cpu_to_le16(1);
3438 if (acl_type
== ACL_TYPE_ACCESS
) {
3439 cifs_acl
->access_entry_count
= cpu_to_le16(count
);
3440 cifs_acl
->default_entry_count
= cpu_to_le16(0xFFFF);
3441 } else if (acl_type
== ACL_TYPE_DEFAULT
) {
3442 cifs_acl
->default_entry_count
= cpu_to_le16(count
);
3443 cifs_acl
->access_entry_count
= cpu_to_le16(0xFFFF);
3445 cifs_dbg(FYI
, "unknown ACL type %d\n", acl_type
);
3448 for (i
= 0; i
< count
; i
++) {
3449 rc
= convert_ace_to_cifs_ace(&cifs_acl
->ace_array
[i
],
3450 &local_acl
->a_entries
[i
]);
3452 /* ACE not converted */
3457 rc
= (__u16
)(count
* sizeof(struct cifs_posix_ace
));
3458 rc
+= sizeof(struct cifs_posix_acl
);
3459 /* BB add check to make sure ACL does not overflow SMB */
3465 CIFSSMBGetPosixACL(const unsigned int xid
, struct cifs_tcon
*tcon
,
3466 const unsigned char *searchName
,
3467 char *acl_inf
, const int buflen
, const int acl_type
,
3468 const struct nls_table
*nls_codepage
, int remap
)
3470 /* SMB_QUERY_POSIX_ACL */
3471 TRANSACTION2_QPI_REQ
*pSMB
= NULL
;
3472 TRANSACTION2_QPI_RSP
*pSMBr
= NULL
;
3476 __u16 params
, byte_count
;
3478 cifs_dbg(FYI
, "In GetPosixACL (Unix) for path %s\n", searchName
);
3481 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
3486 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
3488 cifsConvertToUTF16((__le16
*) pSMB
->FileName
,
3489 searchName
, PATH_MAX
, nls_codepage
,
3491 name_len
++; /* trailing null */
3493 pSMB
->FileName
[name_len
] = 0;
3494 pSMB
->FileName
[name_len
+1] = 0;
3495 } else { /* BB improve the check for buffer overruns BB */
3496 name_len
= strnlen(searchName
, PATH_MAX
);
3497 name_len
++; /* trailing null */
3498 strncpy(pSMB
->FileName
, searchName
, name_len
);
3501 params
= 2 /* level */ + 4 /* rsrvd */ + name_len
/* incl null */ ;
3502 pSMB
->TotalDataCount
= 0;
3503 pSMB
->MaxParameterCount
= cpu_to_le16(2);
3504 /* BB find exact max data count below from sess structure BB */
3505 pSMB
->MaxDataCount
= cpu_to_le16(4000);
3506 pSMB
->MaxSetupCount
= 0;
3510 pSMB
->Reserved2
= 0;
3511 pSMB
->ParameterOffset
= cpu_to_le16(
3512 offsetof(struct smb_com_transaction2_qpi_req
,
3513 InformationLevel
) - 4);
3514 pSMB
->DataCount
= 0;
3515 pSMB
->DataOffset
= 0;
3516 pSMB
->SetupCount
= 1;
3517 pSMB
->Reserved3
= 0;
3518 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION
);
3519 byte_count
= params
+ 1 /* pad */ ;
3520 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
3521 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
3522 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_POSIX_ACL
);
3523 pSMB
->Reserved4
= 0;
3524 inc_rfc1001_len(pSMB
, byte_count
);
3525 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
3527 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
3528 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
3529 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_acl_get
);
3531 cifs_dbg(FYI
, "Send error in Query POSIX ACL = %d\n", rc
);
3533 /* decode response */
3535 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
3536 /* BB also check enough total bytes returned */
3537 if (rc
|| get_bcc(&pSMBr
->hdr
) < 2)
3538 rc
= -EIO
; /* bad smb */
3540 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
3541 __u16 count
= le16_to_cpu(pSMBr
->t2
.DataCount
);
3542 rc
= cifs_copy_posix_acl(acl_inf
,
3543 (char *)&pSMBr
->hdr
.Protocol
+data_offset
,
3544 buflen
, acl_type
, count
);
3547 cifs_buf_release(pSMB
);
3554 CIFSSMBSetPosixACL(const unsigned int xid
, struct cifs_tcon
*tcon
,
3555 const unsigned char *fileName
,
3556 const char *local_acl
, const int buflen
,
3558 const struct nls_table
*nls_codepage
, int remap
)
3560 struct smb_com_transaction2_spi_req
*pSMB
= NULL
;
3561 struct smb_com_transaction2_spi_rsp
*pSMBr
= NULL
;
3565 int bytes_returned
= 0;
3566 __u16 params
, byte_count
, data_count
, param_offset
, offset
;
3568 cifs_dbg(FYI
, "In SetPosixACL (Unix) for path %s\n", fileName
);
3570 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
3574 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
3576 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, fileName
,
3577 PATH_MAX
, nls_codepage
, remap
);
3578 name_len
++; /* trailing null */
3580 } else { /* BB improve the check for buffer overruns BB */
3581 name_len
= strnlen(fileName
, PATH_MAX
);
3582 name_len
++; /* trailing null */
3583 strncpy(pSMB
->FileName
, fileName
, name_len
);
3585 params
= 6 + name_len
;
3586 pSMB
->MaxParameterCount
= cpu_to_le16(2);
3587 /* BB find max SMB size from sess */
3588 pSMB
->MaxDataCount
= cpu_to_le16(1000);
3589 pSMB
->MaxSetupCount
= 0;
3593 pSMB
->Reserved2
= 0;
3594 param_offset
= offsetof(struct smb_com_transaction2_spi_req
,
3595 InformationLevel
) - 4;
3596 offset
= param_offset
+ params
;
3597 parm_data
= ((char *) &pSMB
->hdr
.Protocol
) + offset
;
3598 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
3600 /* convert to on the wire format for POSIX ACL */
3601 data_count
= ACL_to_cifs_posix(parm_data
, local_acl
, buflen
, acl_type
);
3603 if (data_count
== 0) {
3605 goto setACLerrorExit
;
3607 pSMB
->DataOffset
= cpu_to_le16(offset
);
3608 pSMB
->SetupCount
= 1;
3609 pSMB
->Reserved3
= 0;
3610 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_PATH_INFORMATION
);
3611 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_POSIX_ACL
);
3612 byte_count
= 3 /* pad */ + params
+ data_count
;
3613 pSMB
->DataCount
= cpu_to_le16(data_count
);
3614 pSMB
->TotalDataCount
= pSMB
->DataCount
;
3615 pSMB
->ParameterCount
= cpu_to_le16(params
);
3616 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
3617 pSMB
->Reserved4
= 0;
3618 inc_rfc1001_len(pSMB
, byte_count
);
3619 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
3620 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
3621 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
3623 cifs_dbg(FYI
, "Set POSIX ACL returned %d\n", rc
);
3626 cifs_buf_release(pSMB
);
3632 /* BB fix tabs in this function FIXME BB */
3634 CIFSGetExtAttr(const unsigned int xid
, struct cifs_tcon
*tcon
,
3635 const int netfid
, __u64
*pExtAttrBits
, __u64
*pMask
)
3638 struct smb_t2_qfi_req
*pSMB
= NULL
;
3639 struct smb_t2_qfi_rsp
*pSMBr
= NULL
;
3641 __u16 params
, byte_count
;
3643 cifs_dbg(FYI
, "In GetExtAttr\n");
3648 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
3653 params
= 2 /* level */ + 2 /* fid */;
3654 pSMB
->t2
.TotalDataCount
= 0;
3655 pSMB
->t2
.MaxParameterCount
= cpu_to_le16(4);
3656 /* BB find exact max data count below from sess structure BB */
3657 pSMB
->t2
.MaxDataCount
= cpu_to_le16(4000);
3658 pSMB
->t2
.MaxSetupCount
= 0;
3659 pSMB
->t2
.Reserved
= 0;
3661 pSMB
->t2
.Timeout
= 0;
3662 pSMB
->t2
.Reserved2
= 0;
3663 pSMB
->t2
.ParameterOffset
= cpu_to_le16(offsetof(struct smb_t2_qfi_req
,
3665 pSMB
->t2
.DataCount
= 0;
3666 pSMB
->t2
.DataOffset
= 0;
3667 pSMB
->t2
.SetupCount
= 1;
3668 pSMB
->t2
.Reserved3
= 0;
3669 pSMB
->t2
.SubCommand
= cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION
);
3670 byte_count
= params
+ 1 /* pad */ ;
3671 pSMB
->t2
.TotalParameterCount
= cpu_to_le16(params
);
3672 pSMB
->t2
.ParameterCount
= pSMB
->t2
.TotalParameterCount
;
3673 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_ATTR_FLAGS
);
3676 inc_rfc1001_len(pSMB
, byte_count
);
3677 pSMB
->t2
.ByteCount
= cpu_to_le16(byte_count
);
3679 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
3680 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
3682 cifs_dbg(FYI
, "error %d in GetExtAttr\n", rc
);
3684 /* decode response */
3685 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
3686 /* BB also check enough total bytes returned */
3687 if (rc
|| get_bcc(&pSMBr
->hdr
) < 2)
3688 /* If rc should we check for EOPNOSUPP and
3689 disable the srvino flag? or in caller? */
3690 rc
= -EIO
; /* bad smb */
3692 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
3693 __u16 count
= le16_to_cpu(pSMBr
->t2
.DataCount
);
3694 struct file_chattr_info
*pfinfo
;
3695 /* BB Do we need a cast or hash here ? */
3697 cifs_dbg(FYI
, "Illegal size ret in GetExtAttr\n");
3701 pfinfo
= (struct file_chattr_info
*)
3702 (data_offset
+ (char *) &pSMBr
->hdr
.Protocol
);
3703 *pExtAttrBits
= le64_to_cpu(pfinfo
->mode
);
3704 *pMask
= le64_to_cpu(pfinfo
->mask
);
3708 cifs_buf_release(pSMB
);
3710 goto GetExtAttrRetry
;
3714 #endif /* CONFIG_POSIX */
3716 #ifdef CONFIG_CIFS_ACL
3718 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3719 * all NT TRANSACTS that we init here have total parm and data under about 400
3720 * bytes (to fit in small cifs buffer size), which is the case so far, it
3721 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3722 * returned setup area) and MaxParameterCount (returned parms size) must be set
3726 smb_init_nttransact(const __u16 sub_command
, const int setup_count
,
3727 const int parm_len
, struct cifs_tcon
*tcon
,
3732 struct smb_com_ntransact_req
*pSMB
;
3734 rc
= small_smb_init(SMB_COM_NT_TRANSACT
, 19 + setup_count
, tcon
,
3738 *ret_buf
= (void *)pSMB
;
3740 pSMB
->TotalParameterCount
= cpu_to_le32(parm_len
);
3741 pSMB
->TotalDataCount
= 0;
3742 pSMB
->MaxDataCount
= cpu_to_le32(CIFSMaxBufSize
& 0xFFFFFF00);
3743 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
3744 pSMB
->DataCount
= pSMB
->TotalDataCount
;
3745 temp_offset
= offsetof(struct smb_com_ntransact_req
, Parms
) +
3746 (setup_count
* 2) - 4 /* for rfc1001 length itself */;
3747 pSMB
->ParameterOffset
= cpu_to_le32(temp_offset
);
3748 pSMB
->DataOffset
= cpu_to_le32(temp_offset
+ parm_len
);
3749 pSMB
->SetupCount
= setup_count
; /* no need to le convert byte fields */
3750 pSMB
->SubCommand
= cpu_to_le16(sub_command
);
3755 validate_ntransact(char *buf
, char **ppparm
, char **ppdata
,
3756 __u32
*pparmlen
, __u32
*pdatalen
)
3759 __u32 data_count
, data_offset
, parm_count
, parm_offset
;
3760 struct smb_com_ntransact_rsp
*pSMBr
;
3769 pSMBr
= (struct smb_com_ntransact_rsp
*)buf
;
3771 bcc
= get_bcc(&pSMBr
->hdr
);
3772 end_of_smb
= 2 /* sizeof byte count */ + bcc
+
3773 (char *)&pSMBr
->ByteCount
;
3775 data_offset
= le32_to_cpu(pSMBr
->DataOffset
);
3776 data_count
= le32_to_cpu(pSMBr
->DataCount
);
3777 parm_offset
= le32_to_cpu(pSMBr
->ParameterOffset
);
3778 parm_count
= le32_to_cpu(pSMBr
->ParameterCount
);
3780 *ppparm
= (char *)&pSMBr
->hdr
.Protocol
+ parm_offset
;
3781 *ppdata
= (char *)&pSMBr
->hdr
.Protocol
+ data_offset
;
3783 /* should we also check that parm and data areas do not overlap? */
3784 if (*ppparm
> end_of_smb
) {
3785 cifs_dbg(FYI
, "parms start after end of smb\n");
3787 } else if (parm_count
+ *ppparm
> end_of_smb
) {
3788 cifs_dbg(FYI
, "parm end after end of smb\n");
3790 } else if (*ppdata
> end_of_smb
) {
3791 cifs_dbg(FYI
, "data starts after end of smb\n");
3793 } else if (data_count
+ *ppdata
> end_of_smb
) {
3794 cifs_dbg(FYI
, "data %p + count %d (%p) past smb end %p start %p\n",
3795 *ppdata
, data_count
, (data_count
+ *ppdata
),
3798 } else if (parm_count
+ data_count
> bcc
) {
3799 cifs_dbg(FYI
, "parm count and data count larger than SMB\n");
3802 *pdatalen
= data_count
;
3803 *pparmlen
= parm_count
;
3807 /* Get Security Descriptor (by handle) from remote server for a file or dir */
3809 CIFSSMBGetCIFSACL(const unsigned int xid
, struct cifs_tcon
*tcon
, __u16 fid
,
3810 struct cifs_ntsd
**acl_inf
, __u32
*pbuflen
)
3814 QUERY_SEC_DESC_REQ
*pSMB
;
3817 cifs_dbg(FYI
, "GetCifsACL\n");
3822 rc
= smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC
, 0,
3823 8 /* parm len */, tcon
, (void **) &pSMB
);
3827 pSMB
->MaxParameterCount
= cpu_to_le32(4);
3828 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3829 pSMB
->MaxSetupCount
= 0;
3830 pSMB
->Fid
= fid
; /* file handle always le */
3831 pSMB
->AclFlags
= cpu_to_le32(CIFS_ACL_OWNER
| CIFS_ACL_GROUP
|
3833 pSMB
->ByteCount
= cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
3834 inc_rfc1001_len(pSMB
, 11);
3835 iov
[0].iov_base
= (char *)pSMB
;
3836 iov
[0].iov_len
= be32_to_cpu(pSMB
->hdr
.smb_buf_length
) + 4;
3838 rc
= SendReceive2(xid
, tcon
->ses
, iov
, 1 /* num iovec */, &buf_type
,
3840 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_acl_get
);
3842 cifs_dbg(FYI
, "Send error in QuerySecDesc = %d\n", rc
);
3843 } else { /* decode response */
3847 struct smb_com_ntransact_rsp
*pSMBr
;
3850 /* validate_nttransact */
3851 rc
= validate_ntransact(iov
[0].iov_base
, (char **)&parm
,
3852 &pdata
, &parm_len
, pbuflen
);
3855 pSMBr
= (struct smb_com_ntransact_rsp
*)iov
[0].iov_base
;
3857 cifs_dbg(FYI
, "smb %p parm %p data %p\n",
3858 pSMBr
, parm
, *acl_inf
);
3860 if (le32_to_cpu(pSMBr
->ParameterCount
) != 4) {
3861 rc
= -EIO
; /* bad smb */
3866 /* BB check that data area is minimum length and as big as acl_len */
3868 acl_len
= le32_to_cpu(*parm
);
3869 if (acl_len
!= *pbuflen
) {
3870 cifs_dbg(VFS
, "acl length %d does not match %d\n",
3872 if (*pbuflen
> acl_len
)
3876 /* check if buffer is big enough for the acl
3877 header followed by the smallest SID */
3878 if ((*pbuflen
< sizeof(struct cifs_ntsd
) + 8) ||
3879 (*pbuflen
>= 64 * 1024)) {
3880 cifs_dbg(VFS
, "bad acl length %d\n", *pbuflen
);
3884 *acl_inf
= kmemdup(pdata
, *pbuflen
, GFP_KERNEL
);
3885 if (*acl_inf
== NULL
) {
3892 free_rsp_buf(buf_type
, iov
[0].iov_base
);
3893 /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
3898 CIFSSMBSetCIFSACL(const unsigned int xid
, struct cifs_tcon
*tcon
, __u16 fid
,
3899 struct cifs_ntsd
*pntsd
, __u32 acllen
, int aclflag
)
3901 __u16 byte_count
, param_count
, data_count
, param_offset
, data_offset
;
3903 int bytes_returned
= 0;
3904 SET_SEC_DESC_REQ
*pSMB
= NULL
;
3908 rc
= smb_init(SMB_COM_NT_TRANSACT
, 19, tcon
, (void **) &pSMB
, &pSMBr
);
3912 pSMB
->MaxSetupCount
= 0;
3916 param_offset
= offsetof(struct smb_com_transaction_ssec_req
, Fid
) - 4;
3917 data_count
= acllen
;
3918 data_offset
= param_offset
+ param_count
;
3919 byte_count
= 3 /* pad */ + param_count
;
3921 pSMB
->DataCount
= cpu_to_le32(data_count
);
3922 pSMB
->TotalDataCount
= pSMB
->DataCount
;
3923 pSMB
->MaxParameterCount
= cpu_to_le32(4);
3924 pSMB
->MaxDataCount
= cpu_to_le32(16384);
3925 pSMB
->ParameterCount
= cpu_to_le32(param_count
);
3926 pSMB
->ParameterOffset
= cpu_to_le32(param_offset
);
3927 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
3928 pSMB
->DataOffset
= cpu_to_le32(data_offset
);
3929 pSMB
->SetupCount
= 0;
3930 pSMB
->SubCommand
= cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC
);
3931 pSMB
->ByteCount
= cpu_to_le16(byte_count
+data_count
);
3933 pSMB
->Fid
= fid
; /* file handle always le */
3934 pSMB
->Reserved2
= 0;
3935 pSMB
->AclFlags
= cpu_to_le32(aclflag
);
3937 if (pntsd
&& acllen
) {
3938 memcpy((char *)pSMBr
+ offsetof(struct smb_hdr
, Protocol
) +
3939 data_offset
, pntsd
, acllen
);
3940 inc_rfc1001_len(pSMB
, byte_count
+ data_count
);
3942 inc_rfc1001_len(pSMB
, byte_count
);
3944 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
3945 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
3947 cifs_dbg(FYI
, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3948 bytes_returned
, rc
);
3950 cifs_dbg(FYI
, "Set CIFS ACL returned %d\n", rc
);
3951 cifs_buf_release(pSMB
);
3954 goto setCifsAclRetry
;
3959 #endif /* CONFIG_CIFS_ACL */
3961 /* Legacy Query Path Information call for lookup to old servers such
3964 SMBQueryInformation(const unsigned int xid
, struct cifs_tcon
*tcon
,
3965 const char *search_name
, FILE_ALL_INFO
*data
,
3966 const struct nls_table
*nls_codepage
, int remap
)
3968 QUERY_INFORMATION_REQ
*pSMB
;
3969 QUERY_INFORMATION_RSP
*pSMBr
;
3974 cifs_dbg(FYI
, "In SMBQPath path %s\n", search_name
);
3976 rc
= smb_init(SMB_COM_QUERY_INFORMATION
, 0, tcon
, (void **) &pSMB
,
3981 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
3983 cifsConvertToUTF16((__le16
*) pSMB
->FileName
,
3984 search_name
, PATH_MAX
, nls_codepage
,
3986 name_len
++; /* trailing null */
3989 name_len
= strnlen(search_name
, PATH_MAX
);
3990 name_len
++; /* trailing null */
3991 strncpy(pSMB
->FileName
, search_name
, name_len
);
3993 pSMB
->BufferFormat
= 0x04;
3994 name_len
++; /* account for buffer type byte */
3995 inc_rfc1001_len(pSMB
, (__u16
)name_len
);
3996 pSMB
->ByteCount
= cpu_to_le16(name_len
);
3998 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
3999 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
4001 cifs_dbg(FYI
, "Send error in QueryInfo = %d\n", rc
);
4004 __u32 time
= le32_to_cpu(pSMBr
->last_write_time
);
4006 /* decode response */
4007 /* BB FIXME - add time zone adjustment BB */
4008 memset(data
, 0, sizeof(FILE_ALL_INFO
));
4011 /* decode time fields */
4012 data
->ChangeTime
= cpu_to_le64(cifs_UnixTimeToNT(ts
));
4013 data
->LastWriteTime
= data
->ChangeTime
;
4014 data
->LastAccessTime
= 0;
4015 data
->AllocationSize
=
4016 cpu_to_le64(le32_to_cpu(pSMBr
->size
));
4017 data
->EndOfFile
= data
->AllocationSize
;
4019 cpu_to_le32(le16_to_cpu(pSMBr
->attr
));
4021 rc
= -EIO
; /* bad buffer passed in */
4023 cifs_buf_release(pSMB
);
4032 CIFSSMBQFileInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
4033 u16 netfid
, FILE_ALL_INFO
*pFindData
)
4035 struct smb_t2_qfi_req
*pSMB
= NULL
;
4036 struct smb_t2_qfi_rsp
*pSMBr
= NULL
;
4039 __u16 params
, byte_count
;
4042 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
4047 params
= 2 /* level */ + 2 /* fid */;
4048 pSMB
->t2
.TotalDataCount
= 0;
4049 pSMB
->t2
.MaxParameterCount
= cpu_to_le16(4);
4050 /* BB find exact max data count below from sess structure BB */
4051 pSMB
->t2
.MaxDataCount
= cpu_to_le16(CIFSMaxBufSize
);
4052 pSMB
->t2
.MaxSetupCount
= 0;
4053 pSMB
->t2
.Reserved
= 0;
4055 pSMB
->t2
.Timeout
= 0;
4056 pSMB
->t2
.Reserved2
= 0;
4057 pSMB
->t2
.ParameterOffset
= cpu_to_le16(offsetof(struct smb_t2_qfi_req
,
4059 pSMB
->t2
.DataCount
= 0;
4060 pSMB
->t2
.DataOffset
= 0;
4061 pSMB
->t2
.SetupCount
= 1;
4062 pSMB
->t2
.Reserved3
= 0;
4063 pSMB
->t2
.SubCommand
= cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION
);
4064 byte_count
= params
+ 1 /* pad */ ;
4065 pSMB
->t2
.TotalParameterCount
= cpu_to_le16(params
);
4066 pSMB
->t2
.ParameterCount
= pSMB
->t2
.TotalParameterCount
;
4067 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_FILE_ALL_INFO
);
4070 inc_rfc1001_len(pSMB
, byte_count
);
4071 pSMB
->t2
.ByteCount
= cpu_to_le16(byte_count
);
4073 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
4074 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
4076 cifs_dbg(FYI
, "Send error in QFileInfo = %d", rc
);
4077 } else { /* decode response */
4078 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
4080 if (rc
) /* BB add auto retry on EOPNOTSUPP? */
4082 else if (get_bcc(&pSMBr
->hdr
) < 40)
4083 rc
= -EIO
; /* bad smb */
4084 else if (pFindData
) {
4085 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
4086 memcpy((char *) pFindData
,
4087 (char *) &pSMBr
->hdr
.Protocol
+
4088 data_offset
, sizeof(FILE_ALL_INFO
));
4092 cifs_buf_release(pSMB
);
4094 goto QFileInfoRetry
;
4100 CIFSSMBQPathInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
4101 const char *search_name
, FILE_ALL_INFO
*data
,
4102 int legacy
/* old style infolevel */,
4103 const struct nls_table
*nls_codepage
, int remap
)
4105 /* level 263 SMB_QUERY_FILE_ALL_INFO */
4106 TRANSACTION2_QPI_REQ
*pSMB
= NULL
;
4107 TRANSACTION2_QPI_RSP
*pSMBr
= NULL
;
4111 __u16 params
, byte_count
;
4113 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
4115 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
4120 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
4122 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, search_name
,
4123 PATH_MAX
, nls_codepage
, remap
);
4124 name_len
++; /* trailing null */
4126 } else { /* BB improve the check for buffer overruns BB */
4127 name_len
= strnlen(search_name
, PATH_MAX
);
4128 name_len
++; /* trailing null */
4129 strncpy(pSMB
->FileName
, search_name
, name_len
);
4132 params
= 2 /* level */ + 4 /* reserved */ + name_len
/* includes NUL */;
4133 pSMB
->TotalDataCount
= 0;
4134 pSMB
->MaxParameterCount
= cpu_to_le16(2);
4135 /* BB find exact max SMB PDU from sess structure BB */
4136 pSMB
->MaxDataCount
= cpu_to_le16(4000);
4137 pSMB
->MaxSetupCount
= 0;
4141 pSMB
->Reserved2
= 0;
4142 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
4143 struct smb_com_transaction2_qpi_req
, InformationLevel
) - 4);
4144 pSMB
->DataCount
= 0;
4145 pSMB
->DataOffset
= 0;
4146 pSMB
->SetupCount
= 1;
4147 pSMB
->Reserved3
= 0;
4148 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION
);
4149 byte_count
= params
+ 1 /* pad */ ;
4150 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
4151 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
4153 pSMB
->InformationLevel
= cpu_to_le16(SMB_INFO_STANDARD
);
4155 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_FILE_ALL_INFO
);
4156 pSMB
->Reserved4
= 0;
4157 inc_rfc1001_len(pSMB
, byte_count
);
4158 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
4160 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
4161 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
4163 cifs_dbg(FYI
, "Send error in QPathInfo = %d\n", rc
);
4164 } else { /* decode response */
4165 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
4167 if (rc
) /* BB add auto retry on EOPNOTSUPP? */
4169 else if (!legacy
&& get_bcc(&pSMBr
->hdr
) < 40)
4170 rc
= -EIO
; /* bad smb */
4171 else if (legacy
&& get_bcc(&pSMBr
->hdr
) < 24)
4172 rc
= -EIO
; /* 24 or 26 expected but we do not read
4176 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
4179 * On legacy responses we do not read the last field,
4180 * EAsize, fortunately since it varies by subdialect and
4181 * also note it differs on Set vs Get, ie two bytes or 4
4182 * bytes depending but we don't care here.
4185 size
= sizeof(FILE_INFO_STANDARD
);
4187 size
= sizeof(FILE_ALL_INFO
);
4188 memcpy((char *) data
, (char *) &pSMBr
->hdr
.Protocol
+
4193 cifs_buf_release(pSMB
);
4195 goto QPathInfoRetry
;
4201 CIFSSMBUnixQFileInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
4202 u16 netfid
, FILE_UNIX_BASIC_INFO
*pFindData
)
4204 struct smb_t2_qfi_req
*pSMB
= NULL
;
4205 struct smb_t2_qfi_rsp
*pSMBr
= NULL
;
4208 __u16 params
, byte_count
;
4211 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
4216 params
= 2 /* level */ + 2 /* fid */;
4217 pSMB
->t2
.TotalDataCount
= 0;
4218 pSMB
->t2
.MaxParameterCount
= cpu_to_le16(4);
4219 /* BB find exact max data count below from sess structure BB */
4220 pSMB
->t2
.MaxDataCount
= cpu_to_le16(CIFSMaxBufSize
);
4221 pSMB
->t2
.MaxSetupCount
= 0;
4222 pSMB
->t2
.Reserved
= 0;
4224 pSMB
->t2
.Timeout
= 0;
4225 pSMB
->t2
.Reserved2
= 0;
4226 pSMB
->t2
.ParameterOffset
= cpu_to_le16(offsetof(struct smb_t2_qfi_req
,
4228 pSMB
->t2
.DataCount
= 0;
4229 pSMB
->t2
.DataOffset
= 0;
4230 pSMB
->t2
.SetupCount
= 1;
4231 pSMB
->t2
.Reserved3
= 0;
4232 pSMB
->t2
.SubCommand
= cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION
);
4233 byte_count
= params
+ 1 /* pad */ ;
4234 pSMB
->t2
.TotalParameterCount
= cpu_to_le16(params
);
4235 pSMB
->t2
.ParameterCount
= pSMB
->t2
.TotalParameterCount
;
4236 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC
);
4239 inc_rfc1001_len(pSMB
, byte_count
);
4240 pSMB
->t2
.ByteCount
= cpu_to_le16(byte_count
);
4242 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
4243 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
4245 cifs_dbg(FYI
, "Send error in UnixQFileInfo = %d", rc
);
4246 } else { /* decode response */
4247 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
4249 if (rc
|| get_bcc(&pSMBr
->hdr
) < sizeof(FILE_UNIX_BASIC_INFO
)) {
4250 cifs_dbg(VFS
, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
4251 rc
= -EIO
; /* bad smb */
4253 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
4254 memcpy((char *) pFindData
,
4255 (char *) &pSMBr
->hdr
.Protocol
+
4257 sizeof(FILE_UNIX_BASIC_INFO
));
4261 cifs_buf_release(pSMB
);
4263 goto UnixQFileInfoRetry
;
4269 CIFSSMBUnixQPathInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
4270 const unsigned char *searchName
,
4271 FILE_UNIX_BASIC_INFO
*pFindData
,
4272 const struct nls_table
*nls_codepage
, int remap
)
4274 /* SMB_QUERY_FILE_UNIX_BASIC */
4275 TRANSACTION2_QPI_REQ
*pSMB
= NULL
;
4276 TRANSACTION2_QPI_RSP
*pSMBr
= NULL
;
4278 int bytes_returned
= 0;
4280 __u16 params
, byte_count
;
4282 cifs_dbg(FYI
, "In QPathInfo (Unix) the path %s\n", searchName
);
4284 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
4289 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
4291 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, searchName
,
4292 PATH_MAX
, nls_codepage
, remap
);
4293 name_len
++; /* trailing null */
4295 } else { /* BB improve the check for buffer overruns BB */
4296 name_len
= strnlen(searchName
, PATH_MAX
);
4297 name_len
++; /* trailing null */
4298 strncpy(pSMB
->FileName
, searchName
, name_len
);
4301 params
= 2 /* level */ + 4 /* reserved */ + name_len
/* includes NUL */;
4302 pSMB
->TotalDataCount
= 0;
4303 pSMB
->MaxParameterCount
= cpu_to_le16(2);
4304 /* BB find exact max SMB PDU from sess structure BB */
4305 pSMB
->MaxDataCount
= cpu_to_le16(4000);
4306 pSMB
->MaxSetupCount
= 0;
4310 pSMB
->Reserved2
= 0;
4311 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
4312 struct smb_com_transaction2_qpi_req
, InformationLevel
) - 4);
4313 pSMB
->DataCount
= 0;
4314 pSMB
->DataOffset
= 0;
4315 pSMB
->SetupCount
= 1;
4316 pSMB
->Reserved3
= 0;
4317 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION
);
4318 byte_count
= params
+ 1 /* pad */ ;
4319 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
4320 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
4321 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC
);
4322 pSMB
->Reserved4
= 0;
4323 inc_rfc1001_len(pSMB
, byte_count
);
4324 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
4326 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
4327 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
4329 cifs_dbg(FYI
, "Send error in UnixQPathInfo = %d", rc
);
4330 } else { /* decode response */
4331 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
4333 if (rc
|| get_bcc(&pSMBr
->hdr
) < sizeof(FILE_UNIX_BASIC_INFO
)) {
4334 cifs_dbg(VFS
, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
4335 rc
= -EIO
; /* bad smb */
4337 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
4338 memcpy((char *) pFindData
,
4339 (char *) &pSMBr
->hdr
.Protocol
+
4341 sizeof(FILE_UNIX_BASIC_INFO
));
4344 cifs_buf_release(pSMB
);
4346 goto UnixQPathInfoRetry
;
4351 /* xid, tcon, searchName and codepage are input parms, rest are returned */
4353 CIFSFindFirst(const unsigned int xid
, struct cifs_tcon
*tcon
,
4354 const char *searchName
, struct cifs_sb_info
*cifs_sb
,
4355 __u16
*pnetfid
, __u16 search_flags
,
4356 struct cifs_search_info
*psrch_inf
, bool msearch
)
4358 /* level 257 SMB_ */
4359 TRANSACTION2_FFIRST_REQ
*pSMB
= NULL
;
4360 TRANSACTION2_FFIRST_RSP
*pSMBr
= NULL
;
4361 T2_FFIRST_RSP_PARMS
*parms
;
4363 int bytes_returned
= 0;
4364 int name_len
, remap
;
4365 __u16 params
, byte_count
;
4366 struct nls_table
*nls_codepage
;
4368 cifs_dbg(FYI
, "In FindFirst for %s\n", searchName
);
4371 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
4376 nls_codepage
= cifs_sb
->local_nls
;
4377 remap
= cifs_remap(cifs_sb
);
4379 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
4381 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, searchName
,
4382 PATH_MAX
, nls_codepage
, remap
);
4383 /* We can not add the asterik earlier in case
4384 it got remapped to 0xF03A as if it were part of the
4385 directory name instead of a wildcard */
4388 pSMB
->FileName
[name_len
] = CIFS_DIR_SEP(cifs_sb
);
4389 pSMB
->FileName
[name_len
+1] = 0;
4390 pSMB
->FileName
[name_len
+2] = '*';
4391 pSMB
->FileName
[name_len
+3] = 0;
4392 name_len
+= 4; /* now the trailing null */
4393 /* null terminate just in case */
4394 pSMB
->FileName
[name_len
] = 0;
4395 pSMB
->FileName
[name_len
+1] = 0;
4398 } else { /* BB add check for overrun of SMB buf BB */
4399 name_len
= strnlen(searchName
, PATH_MAX
);
4400 /* BB fix here and in unicode clause above ie
4401 if (name_len > buffersize-header)
4402 free buffer exit; BB */
4403 strncpy(pSMB
->FileName
, searchName
, name_len
);
4405 pSMB
->FileName
[name_len
] = CIFS_DIR_SEP(cifs_sb
);
4406 pSMB
->FileName
[name_len
+1] = '*';
4407 pSMB
->FileName
[name_len
+2] = 0;
4412 params
= 12 + name_len
/* includes null */ ;
4413 pSMB
->TotalDataCount
= 0; /* no EAs */
4414 pSMB
->MaxParameterCount
= cpu_to_le16(10);
4415 pSMB
->MaxDataCount
= cpu_to_le16(CIFSMaxBufSize
& 0xFFFFFF00);
4416 pSMB
->MaxSetupCount
= 0;
4420 pSMB
->Reserved2
= 0;
4421 byte_count
= params
+ 1 /* pad */ ;
4422 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
4423 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
4424 pSMB
->ParameterOffset
= cpu_to_le16(
4425 offsetof(struct smb_com_transaction2_ffirst_req
, SearchAttributes
)
4427 pSMB
->DataCount
= 0;
4428 pSMB
->DataOffset
= 0;
4429 pSMB
->SetupCount
= 1; /* one byte, no need to make endian neutral */
4430 pSMB
->Reserved3
= 0;
4431 pSMB
->SubCommand
= cpu_to_le16(TRANS2_FIND_FIRST
);
4432 pSMB
->SearchAttributes
=
4433 cpu_to_le16(ATTR_READONLY
| ATTR_HIDDEN
| ATTR_SYSTEM
|
4435 pSMB
->SearchCount
= cpu_to_le16(CIFSMaxBufSize
/sizeof(FILE_UNIX_INFO
));
4436 pSMB
->SearchFlags
= cpu_to_le16(search_flags
);
4437 pSMB
->InformationLevel
= cpu_to_le16(psrch_inf
->info_level
);
4439 /* BB what should we set StorageType to? Does it matter? BB */
4440 pSMB
->SearchStorageType
= 0;
4441 inc_rfc1001_len(pSMB
, byte_count
);
4442 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
4444 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
4445 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
4446 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_ffirst
);
4448 if (rc
) {/* BB add logic to retry regular search if Unix search
4449 rejected unexpectedly by server */
4450 /* BB Add code to handle unsupported level rc */
4451 cifs_dbg(FYI
, "Error in FindFirst = %d\n", rc
);
4453 cifs_buf_release(pSMB
);
4455 /* BB eventually could optimize out free and realloc of buf */
4458 goto findFirstRetry
;
4459 } else { /* decode response */
4460 /* BB remember to free buffer if error BB */
4461 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
4465 if (pSMBr
->hdr
.Flags2
& SMBFLG2_UNICODE
)
4466 psrch_inf
->unicode
= true;
4468 psrch_inf
->unicode
= false;
4470 psrch_inf
->ntwrk_buf_start
= (char *)pSMBr
;
4471 psrch_inf
->smallBuf
= 0;
4472 psrch_inf
->srch_entries_start
=
4473 (char *) &pSMBr
->hdr
.Protocol
+
4474 le16_to_cpu(pSMBr
->t2
.DataOffset
);
4475 parms
= (T2_FFIRST_RSP_PARMS
*)((char *) &pSMBr
->hdr
.Protocol
+
4476 le16_to_cpu(pSMBr
->t2
.ParameterOffset
));
4478 if (parms
->EndofSearch
)
4479 psrch_inf
->endOfSearch
= true;
4481 psrch_inf
->endOfSearch
= false;
4483 psrch_inf
->entries_in_buffer
=
4484 le16_to_cpu(parms
->SearchCount
);
4485 psrch_inf
->index_of_last_entry
= 2 /* skip . and .. */ +
4486 psrch_inf
->entries_in_buffer
;
4487 lnoff
= le16_to_cpu(parms
->LastNameOffset
);
4488 if (CIFSMaxBufSize
< lnoff
) {
4489 cifs_dbg(VFS
, "ignoring corrupt resume name\n");
4490 psrch_inf
->last_entry
= NULL
;
4494 psrch_inf
->last_entry
= psrch_inf
->srch_entries_start
+
4498 *pnetfid
= parms
->SearchHandle
;
4500 cifs_buf_release(pSMB
);
4507 int CIFSFindNext(const unsigned int xid
, struct cifs_tcon
*tcon
,
4508 __u16 searchHandle
, __u16 search_flags
,
4509 struct cifs_search_info
*psrch_inf
)
4511 TRANSACTION2_FNEXT_REQ
*pSMB
= NULL
;
4512 TRANSACTION2_FNEXT_RSP
*pSMBr
= NULL
;
4513 T2_FNEXT_RSP_PARMS
*parms
;
4514 char *response_data
;
4517 unsigned int name_len
;
4518 __u16 params
, byte_count
;
4520 cifs_dbg(FYI
, "In FindNext\n");
4522 if (psrch_inf
->endOfSearch
)
4525 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
4530 params
= 14; /* includes 2 bytes of null string, converted to LE below*/
4532 pSMB
->TotalDataCount
= 0; /* no EAs */
4533 pSMB
->MaxParameterCount
= cpu_to_le16(8);
4534 pSMB
->MaxDataCount
= cpu_to_le16(CIFSMaxBufSize
& 0xFFFFFF00);
4535 pSMB
->MaxSetupCount
= 0;
4539 pSMB
->Reserved2
= 0;
4540 pSMB
->ParameterOffset
= cpu_to_le16(
4541 offsetof(struct smb_com_transaction2_fnext_req
,SearchHandle
) - 4);
4542 pSMB
->DataCount
= 0;
4543 pSMB
->DataOffset
= 0;
4544 pSMB
->SetupCount
= 1;
4545 pSMB
->Reserved3
= 0;
4546 pSMB
->SubCommand
= cpu_to_le16(TRANS2_FIND_NEXT
);
4547 pSMB
->SearchHandle
= searchHandle
; /* always kept as le */
4549 cpu_to_le16(CIFSMaxBufSize
/ sizeof(FILE_UNIX_INFO
));
4550 pSMB
->InformationLevel
= cpu_to_le16(psrch_inf
->info_level
);
4551 pSMB
->ResumeKey
= psrch_inf
->resume_key
;
4552 pSMB
->SearchFlags
= cpu_to_le16(search_flags
);
4554 name_len
= psrch_inf
->resume_name_len
;
4556 if (name_len
< PATH_MAX
) {
4557 memcpy(pSMB
->ResumeFileName
, psrch_inf
->presume_name
, name_len
);
4558 byte_count
+= name_len
;
4559 /* 14 byte parm len above enough for 2 byte null terminator */
4560 pSMB
->ResumeFileName
[name_len
] = 0;
4561 pSMB
->ResumeFileName
[name_len
+1] = 0;
4564 goto FNext2_err_exit
;
4566 byte_count
= params
+ 1 /* pad */ ;
4567 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
4568 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
4569 inc_rfc1001_len(pSMB
, byte_count
);
4570 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
4572 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
4573 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
4574 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_fnext
);
4577 psrch_inf
->endOfSearch
= true;
4578 cifs_buf_release(pSMB
);
4579 rc
= 0; /* search probably was closed at end of search*/
4581 cifs_dbg(FYI
, "FindNext returned = %d\n", rc
);
4582 } else { /* decode response */
4583 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
4588 /* BB fixme add lock for file (srch_info) struct here */
4589 if (pSMBr
->hdr
.Flags2
& SMBFLG2_UNICODE
)
4590 psrch_inf
->unicode
= true;
4592 psrch_inf
->unicode
= false;
4593 response_data
= (char *) &pSMBr
->hdr
.Protocol
+
4594 le16_to_cpu(pSMBr
->t2
.ParameterOffset
);
4595 parms
= (T2_FNEXT_RSP_PARMS
*)response_data
;
4596 response_data
= (char *)&pSMBr
->hdr
.Protocol
+
4597 le16_to_cpu(pSMBr
->t2
.DataOffset
);
4598 if (psrch_inf
->smallBuf
)
4599 cifs_small_buf_release(
4600 psrch_inf
->ntwrk_buf_start
);
4602 cifs_buf_release(psrch_inf
->ntwrk_buf_start
);
4603 psrch_inf
->srch_entries_start
= response_data
;
4604 psrch_inf
->ntwrk_buf_start
= (char *)pSMB
;
4605 psrch_inf
->smallBuf
= 0;
4606 if (parms
->EndofSearch
)
4607 psrch_inf
->endOfSearch
= true;
4609 psrch_inf
->endOfSearch
= false;
4610 psrch_inf
->entries_in_buffer
=
4611 le16_to_cpu(parms
->SearchCount
);
4612 psrch_inf
->index_of_last_entry
+=
4613 psrch_inf
->entries_in_buffer
;
4614 lnoff
= le16_to_cpu(parms
->LastNameOffset
);
4615 if (CIFSMaxBufSize
< lnoff
) {
4616 cifs_dbg(VFS
, "ignoring corrupt resume name\n");
4617 psrch_inf
->last_entry
= NULL
;
4620 psrch_inf
->last_entry
=
4621 psrch_inf
->srch_entries_start
+ lnoff
;
4623 /* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4624 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
4626 /* BB fixme add unlock here */
4631 /* BB On error, should we leave previous search buf (and count and
4632 last entry fields) intact or free the previous one? */
4634 /* Note: On -EAGAIN error only caller can retry on handle based calls
4635 since file handle passed in no longer valid */
4638 cifs_buf_release(pSMB
);
4643 CIFSFindClose(const unsigned int xid
, struct cifs_tcon
*tcon
,
4644 const __u16 searchHandle
)
4647 FINDCLOSE_REQ
*pSMB
= NULL
;
4649 cifs_dbg(FYI
, "In CIFSSMBFindClose\n");
4650 rc
= small_smb_init(SMB_COM_FIND_CLOSE2
, 1, tcon
, (void **)&pSMB
);
4652 /* no sense returning error if session restarted
4653 as file handle has been closed */
4659 pSMB
->FileID
= searchHandle
;
4660 pSMB
->ByteCount
= 0;
4661 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) pSMB
, 0);
4663 cifs_dbg(VFS
, "Send error in FindClose = %d\n", rc
);
4665 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_fclose
);
4667 /* Since session is dead, search handle closed on server already */
4675 CIFSGetSrvInodeNumber(const unsigned int xid
, struct cifs_tcon
*tcon
,
4676 const char *search_name
, __u64
*inode_number
,
4677 const struct nls_table
*nls_codepage
, int remap
)
4680 TRANSACTION2_QPI_REQ
*pSMB
= NULL
;
4681 TRANSACTION2_QPI_RSP
*pSMBr
= NULL
;
4682 int name_len
, bytes_returned
;
4683 __u16 params
, byte_count
;
4685 cifs_dbg(FYI
, "In GetSrvInodeNum for %s\n", search_name
);
4689 GetInodeNumberRetry
:
4690 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
4695 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
4697 cifsConvertToUTF16((__le16
*) pSMB
->FileName
,
4698 search_name
, PATH_MAX
, nls_codepage
,
4700 name_len
++; /* trailing null */
4702 } else { /* BB improve the check for buffer overruns BB */
4703 name_len
= strnlen(search_name
, PATH_MAX
);
4704 name_len
++; /* trailing null */
4705 strncpy(pSMB
->FileName
, search_name
, name_len
);
4708 params
= 2 /* level */ + 4 /* rsrvd */ + name_len
/* incl null */ ;
4709 pSMB
->TotalDataCount
= 0;
4710 pSMB
->MaxParameterCount
= cpu_to_le16(2);
4711 /* BB find exact max data count below from sess structure BB */
4712 pSMB
->MaxDataCount
= cpu_to_le16(4000);
4713 pSMB
->MaxSetupCount
= 0;
4717 pSMB
->Reserved2
= 0;
4718 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
4719 struct smb_com_transaction2_qpi_req
, InformationLevel
) - 4);
4720 pSMB
->DataCount
= 0;
4721 pSMB
->DataOffset
= 0;
4722 pSMB
->SetupCount
= 1;
4723 pSMB
->Reserved3
= 0;
4724 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION
);
4725 byte_count
= params
+ 1 /* pad */ ;
4726 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
4727 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
4728 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO
);
4729 pSMB
->Reserved4
= 0;
4730 inc_rfc1001_len(pSMB
, byte_count
);
4731 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
4733 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
4734 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
4736 cifs_dbg(FYI
, "error %d in QueryInternalInfo\n", rc
);
4738 /* decode response */
4739 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
4740 /* BB also check enough total bytes returned */
4741 if (rc
|| get_bcc(&pSMBr
->hdr
) < 2)
4742 /* If rc should we check for EOPNOSUPP and
4743 disable the srvino flag? or in caller? */
4744 rc
= -EIO
; /* bad smb */
4746 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
4747 __u16 count
= le16_to_cpu(pSMBr
->t2
.DataCount
);
4748 struct file_internal_info
*pfinfo
;
4749 /* BB Do we need a cast or hash here ? */
4751 cifs_dbg(FYI
, "Illegal size ret in QryIntrnlInf\n");
4753 goto GetInodeNumOut
;
4755 pfinfo
= (struct file_internal_info
*)
4756 (data_offset
+ (char *) &pSMBr
->hdr
.Protocol
);
4757 *inode_number
= le64_to_cpu(pfinfo
->UniqueId
);
4761 cifs_buf_release(pSMB
);
4763 goto GetInodeNumberRetry
;
4767 /* parses DFS refferal V3 structure
4768 * caller is responsible for freeing target_nodes
4771 * on failure - errno
4774 parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP
*pSMBr
,
4775 unsigned int *num_of_nodes
,
4776 struct dfs_info3_param
**target_nodes
,
4777 const struct nls_table
*nls_codepage
, int remap
,
4778 const char *searchName
)
4783 struct dfs_referral_level_3
*ref
;
4785 if (pSMBr
->hdr
.Flags2
& SMBFLG2_UNICODE
)
4789 *num_of_nodes
= le16_to_cpu(pSMBr
->NumberOfReferrals
);
4791 if (*num_of_nodes
< 1) {
4792 cifs_dbg(VFS
, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
4795 goto parse_DFS_referrals_exit
;
4798 ref
= (struct dfs_referral_level_3
*) &(pSMBr
->referrals
);
4799 if (ref
->VersionNumber
!= cpu_to_le16(3)) {
4800 cifs_dbg(VFS
, "Referrals of V%d version are not supported, should be V3\n",
4801 le16_to_cpu(ref
->VersionNumber
));
4803 goto parse_DFS_referrals_exit
;
4806 /* get the upper boundary of the resp buffer */
4807 data_end
= (char *)(&(pSMBr
->PathConsumed
)) +
4808 le16_to_cpu(pSMBr
->t2
.DataCount
);
4810 cifs_dbg(FYI
, "num_referrals: %d dfs flags: 0x%x ...\n",
4811 *num_of_nodes
, le32_to_cpu(pSMBr
->DFSFlags
));
4813 *target_nodes
= kcalloc(*num_of_nodes
, sizeof(struct dfs_info3_param
),
4815 if (*target_nodes
== NULL
) {
4817 goto parse_DFS_referrals_exit
;
4820 /* collect necessary data from referrals */
4821 for (i
= 0; i
< *num_of_nodes
; i
++) {
4824 struct dfs_info3_param
*node
= (*target_nodes
)+i
;
4826 node
->flags
= le32_to_cpu(pSMBr
->DFSFlags
);
4828 __le16
*tmp
= kmalloc(strlen(searchName
)*2 + 2,
4832 goto parse_DFS_referrals_exit
;
4834 cifsConvertToUTF16((__le16
*) tmp
, searchName
,
4835 PATH_MAX
, nls_codepage
, remap
);
4836 node
->path_consumed
= cifs_utf16_bytes(tmp
,
4837 le16_to_cpu(pSMBr
->PathConsumed
),
4841 node
->path_consumed
= le16_to_cpu(pSMBr
->PathConsumed
);
4843 node
->server_type
= le16_to_cpu(ref
->ServerType
);
4844 node
->ref_flag
= le16_to_cpu(ref
->ReferralEntryFlags
);
4847 temp
= (char *)ref
+ le16_to_cpu(ref
->DfsPathOffset
);
4848 max_len
= data_end
- temp
;
4849 node
->path_name
= cifs_strndup_from_utf16(temp
, max_len
,
4850 is_unicode
, nls_codepage
);
4851 if (!node
->path_name
) {
4853 goto parse_DFS_referrals_exit
;
4856 /* copy link target UNC */
4857 temp
= (char *)ref
+ le16_to_cpu(ref
->NetworkAddressOffset
);
4858 max_len
= data_end
- temp
;
4859 node
->node_name
= cifs_strndup_from_utf16(temp
, max_len
,
4860 is_unicode
, nls_codepage
);
4861 if (!node
->node_name
) {
4863 goto parse_DFS_referrals_exit
;
4869 parse_DFS_referrals_exit
:
4871 free_dfs_info_array(*target_nodes
, *num_of_nodes
);
4872 *target_nodes
= NULL
;
4879 CIFSGetDFSRefer(const unsigned int xid
, struct cifs_ses
*ses
,
4880 const char *search_name
, struct dfs_info3_param
**target_nodes
,
4881 unsigned int *num_of_nodes
,
4882 const struct nls_table
*nls_codepage
, int remap
)
4884 /* TRANS2_GET_DFS_REFERRAL */
4885 TRANSACTION2_GET_DFS_REFER_REQ
*pSMB
= NULL
;
4886 TRANSACTION2_GET_DFS_REFER_RSP
*pSMBr
= NULL
;
4890 __u16 params
, byte_count
;
4892 *target_nodes
= NULL
;
4894 cifs_dbg(FYI
, "In GetDFSRefer the path %s\n", search_name
);
4898 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, NULL
, (void **) &pSMB
,
4903 /* server pointer checked in called function,
4904 but should never be null here anyway */
4905 pSMB
->hdr
.Mid
= get_next_mid(ses
->server
);
4906 pSMB
->hdr
.Tid
= ses
->ipc_tid
;
4907 pSMB
->hdr
.Uid
= ses
->Suid
;
4908 if (ses
->capabilities
& CAP_STATUS32
)
4909 pSMB
->hdr
.Flags2
|= SMBFLG2_ERR_STATUS
;
4910 if (ses
->capabilities
& CAP_DFS
)
4911 pSMB
->hdr
.Flags2
|= SMBFLG2_DFS
;
4913 if (ses
->capabilities
& CAP_UNICODE
) {
4914 pSMB
->hdr
.Flags2
|= SMBFLG2_UNICODE
;
4916 cifsConvertToUTF16((__le16
*) pSMB
->RequestFileName
,
4917 search_name
, PATH_MAX
, nls_codepage
,
4919 name_len
++; /* trailing null */
4921 } else { /* BB improve the check for buffer overruns BB */
4922 name_len
= strnlen(search_name
, PATH_MAX
);
4923 name_len
++; /* trailing null */
4924 strncpy(pSMB
->RequestFileName
, search_name
, name_len
);
4927 if (ses
->server
->sign
)
4928 pSMB
->hdr
.Flags2
|= SMBFLG2_SECURITY_SIGNATURE
;
4930 pSMB
->hdr
.Uid
= ses
->Suid
;
4932 params
= 2 /* level */ + name_len
/*includes null */ ;
4933 pSMB
->TotalDataCount
= 0;
4934 pSMB
->DataCount
= 0;
4935 pSMB
->DataOffset
= 0;
4936 pSMB
->MaxParameterCount
= 0;
4937 /* BB find exact max SMB PDU from sess structure BB */
4938 pSMB
->MaxDataCount
= cpu_to_le16(4000);
4939 pSMB
->MaxSetupCount
= 0;
4943 pSMB
->Reserved2
= 0;
4944 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
4945 struct smb_com_transaction2_get_dfs_refer_req
, MaxReferralLevel
) - 4);
4946 pSMB
->SetupCount
= 1;
4947 pSMB
->Reserved3
= 0;
4948 pSMB
->SubCommand
= cpu_to_le16(TRANS2_GET_DFS_REFERRAL
);
4949 byte_count
= params
+ 3 /* pad */ ;
4950 pSMB
->ParameterCount
= cpu_to_le16(params
);
4951 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
4952 pSMB
->MaxReferralLevel
= cpu_to_le16(3);
4953 inc_rfc1001_len(pSMB
, byte_count
);
4954 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
4956 rc
= SendReceive(xid
, ses
, (struct smb_hdr
*) pSMB
,
4957 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
4959 cifs_dbg(FYI
, "Send error in GetDFSRefer = %d\n", rc
);
4962 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
4964 /* BB Also check if enough total bytes returned? */
4965 if (rc
|| get_bcc(&pSMBr
->hdr
) < 17) {
4966 rc
= -EIO
; /* bad smb */
4970 cifs_dbg(FYI
, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4971 get_bcc(&pSMBr
->hdr
), le16_to_cpu(pSMBr
->t2
.DataOffset
));
4973 /* parse returned result into more usable form */
4974 rc
= parse_DFS_referrals(pSMBr
, num_of_nodes
,
4975 target_nodes
, nls_codepage
, remap
,
4979 cifs_buf_release(pSMB
);
4987 /* Query File System Info such as free space to old servers such as Win 9x */
4989 SMBOldQFSInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
4990 struct kstatfs
*FSData
)
4992 /* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4993 TRANSACTION2_QFSI_REQ
*pSMB
= NULL
;
4994 TRANSACTION2_QFSI_RSP
*pSMBr
= NULL
;
4995 FILE_SYSTEM_ALLOC_INFO
*response_data
;
4997 int bytes_returned
= 0;
4998 __u16 params
, byte_count
;
5000 cifs_dbg(FYI
, "OldQFSInfo\n");
5002 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
5007 params
= 2; /* level */
5008 pSMB
->TotalDataCount
= 0;
5009 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5010 pSMB
->MaxDataCount
= cpu_to_le16(1000);
5011 pSMB
->MaxSetupCount
= 0;
5015 pSMB
->Reserved2
= 0;
5016 byte_count
= params
+ 1 /* pad */ ;
5017 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
5018 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
5019 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
5020 struct smb_com_transaction2_qfsi_req
, InformationLevel
) - 4);
5021 pSMB
->DataCount
= 0;
5022 pSMB
->DataOffset
= 0;
5023 pSMB
->SetupCount
= 1;
5024 pSMB
->Reserved3
= 0;
5025 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_FS_INFORMATION
);
5026 pSMB
->InformationLevel
= cpu_to_le16(SMB_INFO_ALLOCATION
);
5027 inc_rfc1001_len(pSMB
, byte_count
);
5028 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5030 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5031 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5033 cifs_dbg(FYI
, "Send error in QFSInfo = %d\n", rc
);
5034 } else { /* decode response */
5035 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
5037 if (rc
|| get_bcc(&pSMBr
->hdr
) < 18)
5038 rc
= -EIO
; /* bad smb */
5040 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
5041 cifs_dbg(FYI
, "qfsinf resp BCC: %d Offset %d\n",
5042 get_bcc(&pSMBr
->hdr
), data_offset
);
5044 response_data
= (FILE_SYSTEM_ALLOC_INFO
*)
5045 (((char *) &pSMBr
->hdr
.Protocol
) + data_offset
);
5047 le16_to_cpu(response_data
->BytesPerSector
) *
5048 le32_to_cpu(response_data
->
5049 SectorsPerAllocationUnit
);
5051 le32_to_cpu(response_data
->TotalAllocationUnits
);
5052 FSData
->f_bfree
= FSData
->f_bavail
=
5053 le32_to_cpu(response_data
->FreeAllocationUnits
);
5054 cifs_dbg(FYI
, "Blocks: %lld Free: %lld Block size %ld\n",
5055 (unsigned long long)FSData
->f_blocks
,
5056 (unsigned long long)FSData
->f_bfree
,
5060 cifs_buf_release(pSMB
);
5063 goto oldQFSInfoRetry
;
5069 CIFSSMBQFSInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
5070 struct kstatfs
*FSData
)
5072 /* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5073 TRANSACTION2_QFSI_REQ
*pSMB
= NULL
;
5074 TRANSACTION2_QFSI_RSP
*pSMBr
= NULL
;
5075 FILE_SYSTEM_INFO
*response_data
;
5077 int bytes_returned
= 0;
5078 __u16 params
, byte_count
;
5080 cifs_dbg(FYI
, "In QFSInfo\n");
5082 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
5087 params
= 2; /* level */
5088 pSMB
->TotalDataCount
= 0;
5089 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5090 pSMB
->MaxDataCount
= cpu_to_le16(1000);
5091 pSMB
->MaxSetupCount
= 0;
5095 pSMB
->Reserved2
= 0;
5096 byte_count
= params
+ 1 /* pad */ ;
5097 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
5098 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
5099 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
5100 struct smb_com_transaction2_qfsi_req
, InformationLevel
) - 4);
5101 pSMB
->DataCount
= 0;
5102 pSMB
->DataOffset
= 0;
5103 pSMB
->SetupCount
= 1;
5104 pSMB
->Reserved3
= 0;
5105 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_FS_INFORMATION
);
5106 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_FS_SIZE_INFO
);
5107 inc_rfc1001_len(pSMB
, byte_count
);
5108 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5110 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5111 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5113 cifs_dbg(FYI
, "Send error in QFSInfo = %d\n", rc
);
5114 } else { /* decode response */
5115 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
5117 if (rc
|| get_bcc(&pSMBr
->hdr
) < 24)
5118 rc
= -EIO
; /* bad smb */
5120 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
5124 *) (((char *) &pSMBr
->hdr
.Protocol
) +
5127 le32_to_cpu(response_data
->BytesPerSector
) *
5128 le32_to_cpu(response_data
->
5129 SectorsPerAllocationUnit
);
5131 le64_to_cpu(response_data
->TotalAllocationUnits
);
5132 FSData
->f_bfree
= FSData
->f_bavail
=
5133 le64_to_cpu(response_data
->FreeAllocationUnits
);
5134 cifs_dbg(FYI
, "Blocks: %lld Free: %lld Block size %ld\n",
5135 (unsigned long long)FSData
->f_blocks
,
5136 (unsigned long long)FSData
->f_bfree
,
5140 cifs_buf_release(pSMB
);
5149 CIFSSMBQFSAttributeInfo(const unsigned int xid
, struct cifs_tcon
*tcon
)
5151 /* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5152 TRANSACTION2_QFSI_REQ
*pSMB
= NULL
;
5153 TRANSACTION2_QFSI_RSP
*pSMBr
= NULL
;
5154 FILE_SYSTEM_ATTRIBUTE_INFO
*response_data
;
5156 int bytes_returned
= 0;
5157 __u16 params
, byte_count
;
5159 cifs_dbg(FYI
, "In QFSAttributeInfo\n");
5161 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
5166 params
= 2; /* level */
5167 pSMB
->TotalDataCount
= 0;
5168 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5169 /* BB find exact max SMB PDU from sess structure BB */
5170 pSMB
->MaxDataCount
= cpu_to_le16(1000);
5171 pSMB
->MaxSetupCount
= 0;
5175 pSMB
->Reserved2
= 0;
5176 byte_count
= params
+ 1 /* pad */ ;
5177 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
5178 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
5179 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
5180 struct smb_com_transaction2_qfsi_req
, InformationLevel
) - 4);
5181 pSMB
->DataCount
= 0;
5182 pSMB
->DataOffset
= 0;
5183 pSMB
->SetupCount
= 1;
5184 pSMB
->Reserved3
= 0;
5185 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_FS_INFORMATION
);
5186 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO
);
5187 inc_rfc1001_len(pSMB
, byte_count
);
5188 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5190 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5191 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5193 cifs_dbg(VFS
, "Send error in QFSAttributeInfo = %d\n", rc
);
5194 } else { /* decode response */
5195 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
5197 if (rc
|| get_bcc(&pSMBr
->hdr
) < 13) {
5198 /* BB also check if enough bytes returned */
5199 rc
= -EIO
; /* bad smb */
5201 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
5203 (FILE_SYSTEM_ATTRIBUTE_INFO
5204 *) (((char *) &pSMBr
->hdr
.Protocol
) +
5206 memcpy(&tcon
->fsAttrInfo
, response_data
,
5207 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO
));
5210 cifs_buf_release(pSMB
);
5213 goto QFSAttributeRetry
;
5219 CIFSSMBQFSDeviceInfo(const unsigned int xid
, struct cifs_tcon
*tcon
)
5221 /* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5222 TRANSACTION2_QFSI_REQ
*pSMB
= NULL
;
5223 TRANSACTION2_QFSI_RSP
*pSMBr
= NULL
;
5224 FILE_SYSTEM_DEVICE_INFO
*response_data
;
5226 int bytes_returned
= 0;
5227 __u16 params
, byte_count
;
5229 cifs_dbg(FYI
, "In QFSDeviceInfo\n");
5231 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
5236 params
= 2; /* level */
5237 pSMB
->TotalDataCount
= 0;
5238 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5239 /* BB find exact max SMB PDU from sess structure BB */
5240 pSMB
->MaxDataCount
= cpu_to_le16(1000);
5241 pSMB
->MaxSetupCount
= 0;
5245 pSMB
->Reserved2
= 0;
5246 byte_count
= params
+ 1 /* pad */ ;
5247 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
5248 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
5249 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
5250 struct smb_com_transaction2_qfsi_req
, InformationLevel
) - 4);
5252 pSMB
->DataCount
= 0;
5253 pSMB
->DataOffset
= 0;
5254 pSMB
->SetupCount
= 1;
5255 pSMB
->Reserved3
= 0;
5256 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_FS_INFORMATION
);
5257 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO
);
5258 inc_rfc1001_len(pSMB
, byte_count
);
5259 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5261 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5262 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5264 cifs_dbg(FYI
, "Send error in QFSDeviceInfo = %d\n", rc
);
5265 } else { /* decode response */
5266 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
5268 if (rc
|| get_bcc(&pSMBr
->hdr
) <
5269 sizeof(FILE_SYSTEM_DEVICE_INFO
))
5270 rc
= -EIO
; /* bad smb */
5272 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
5274 (FILE_SYSTEM_DEVICE_INFO
*)
5275 (((char *) &pSMBr
->hdr
.Protocol
) +
5277 memcpy(&tcon
->fsDevInfo
, response_data
,
5278 sizeof(FILE_SYSTEM_DEVICE_INFO
));
5281 cifs_buf_release(pSMB
);
5284 goto QFSDeviceRetry
;
5290 CIFSSMBQFSUnixInfo(const unsigned int xid
, struct cifs_tcon
*tcon
)
5292 /* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5293 TRANSACTION2_QFSI_REQ
*pSMB
= NULL
;
5294 TRANSACTION2_QFSI_RSP
*pSMBr
= NULL
;
5295 FILE_SYSTEM_UNIX_INFO
*response_data
;
5297 int bytes_returned
= 0;
5298 __u16 params
, byte_count
;
5300 cifs_dbg(FYI
, "In QFSUnixInfo\n");
5302 rc
= smb_init_no_reconnect(SMB_COM_TRANSACTION2
, 15, tcon
,
5303 (void **) &pSMB
, (void **) &pSMBr
);
5307 params
= 2; /* level */
5308 pSMB
->TotalDataCount
= 0;
5309 pSMB
->DataCount
= 0;
5310 pSMB
->DataOffset
= 0;
5311 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5312 /* BB find exact max SMB PDU from sess structure BB */
5313 pSMB
->MaxDataCount
= cpu_to_le16(100);
5314 pSMB
->MaxSetupCount
= 0;
5318 pSMB
->Reserved2
= 0;
5319 byte_count
= params
+ 1 /* pad */ ;
5320 pSMB
->ParameterCount
= cpu_to_le16(params
);
5321 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
5322 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(struct
5323 smb_com_transaction2_qfsi_req
, InformationLevel
) - 4);
5324 pSMB
->SetupCount
= 1;
5325 pSMB
->Reserved3
= 0;
5326 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_FS_INFORMATION
);
5327 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO
);
5328 inc_rfc1001_len(pSMB
, byte_count
);
5329 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5331 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5332 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5334 cifs_dbg(VFS
, "Send error in QFSUnixInfo = %d\n", rc
);
5335 } else { /* decode response */
5336 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
5338 if (rc
|| get_bcc(&pSMBr
->hdr
) < 13) {
5339 rc
= -EIO
; /* bad smb */
5341 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
5343 (FILE_SYSTEM_UNIX_INFO
5344 *) (((char *) &pSMBr
->hdr
.Protocol
) +
5346 memcpy(&tcon
->fsUnixInfo
, response_data
,
5347 sizeof(FILE_SYSTEM_UNIX_INFO
));
5350 cifs_buf_release(pSMB
);
5360 CIFSSMBSetFSUnixInfo(const unsigned int xid
, struct cifs_tcon
*tcon
, __u64 cap
)
5362 /* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5363 TRANSACTION2_SETFSI_REQ
*pSMB
= NULL
;
5364 TRANSACTION2_SETFSI_RSP
*pSMBr
= NULL
;
5366 int bytes_returned
= 0;
5367 __u16 params
, param_offset
, offset
, byte_count
;
5369 cifs_dbg(FYI
, "In SETFSUnixInfo\n");
5371 /* BB switch to small buf init to save memory */
5372 rc
= smb_init_no_reconnect(SMB_COM_TRANSACTION2
, 15, tcon
,
5373 (void **) &pSMB
, (void **) &pSMBr
);
5377 params
= 4; /* 2 bytes zero followed by info level. */
5378 pSMB
->MaxSetupCount
= 0;
5382 pSMB
->Reserved2
= 0;
5383 param_offset
= offsetof(struct smb_com_transaction2_setfsi_req
, FileNum
)
5385 offset
= param_offset
+ params
;
5387 pSMB
->MaxParameterCount
= cpu_to_le16(4);
5388 /* BB find exact max SMB PDU from sess structure BB */
5389 pSMB
->MaxDataCount
= cpu_to_le16(100);
5390 pSMB
->SetupCount
= 1;
5391 pSMB
->Reserved3
= 0;
5392 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_FS_INFORMATION
);
5393 byte_count
= 1 /* pad */ + params
+ 12;
5395 pSMB
->DataCount
= cpu_to_le16(12);
5396 pSMB
->ParameterCount
= cpu_to_le16(params
);
5397 pSMB
->TotalDataCount
= pSMB
->DataCount
;
5398 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
5399 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
5400 pSMB
->DataOffset
= cpu_to_le16(offset
);
5404 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_CIFS_UNIX_INFO
);
5407 pSMB
->ClientUnixMajor
= cpu_to_le16(CIFS_UNIX_MAJOR_VERSION
);
5408 pSMB
->ClientUnixMinor
= cpu_to_le16(CIFS_UNIX_MINOR_VERSION
);
5409 pSMB
->ClientUnixCap
= cpu_to_le64(cap
);
5411 inc_rfc1001_len(pSMB
, byte_count
);
5412 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5414 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5415 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5417 cifs_dbg(VFS
, "Send error in SETFSUnixInfo = %d\n", rc
);
5418 } else { /* decode response */
5419 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
5421 rc
= -EIO
; /* bad smb */
5423 cifs_buf_release(pSMB
);
5426 goto SETFSUnixRetry
;
5434 CIFSSMBQFSPosixInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
5435 struct kstatfs
*FSData
)
5437 /* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5438 TRANSACTION2_QFSI_REQ
*pSMB
= NULL
;
5439 TRANSACTION2_QFSI_RSP
*pSMBr
= NULL
;
5440 FILE_SYSTEM_POSIX_INFO
*response_data
;
5442 int bytes_returned
= 0;
5443 __u16 params
, byte_count
;
5445 cifs_dbg(FYI
, "In QFSPosixInfo\n");
5447 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
5452 params
= 2; /* level */
5453 pSMB
->TotalDataCount
= 0;
5454 pSMB
->DataCount
= 0;
5455 pSMB
->DataOffset
= 0;
5456 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5457 /* BB find exact max SMB PDU from sess structure BB */
5458 pSMB
->MaxDataCount
= cpu_to_le16(100);
5459 pSMB
->MaxSetupCount
= 0;
5463 pSMB
->Reserved2
= 0;
5464 byte_count
= params
+ 1 /* pad */ ;
5465 pSMB
->ParameterCount
= cpu_to_le16(params
);
5466 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
5467 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(struct
5468 smb_com_transaction2_qfsi_req
, InformationLevel
) - 4);
5469 pSMB
->SetupCount
= 1;
5470 pSMB
->Reserved3
= 0;
5471 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_FS_INFORMATION
);
5472 pSMB
->InformationLevel
= cpu_to_le16(SMB_QUERY_POSIX_FS_INFO
);
5473 inc_rfc1001_len(pSMB
, byte_count
);
5474 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5476 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5477 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5479 cifs_dbg(FYI
, "Send error in QFSUnixInfo = %d\n", rc
);
5480 } else { /* decode response */
5481 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
5483 if (rc
|| get_bcc(&pSMBr
->hdr
) < 13) {
5484 rc
= -EIO
; /* bad smb */
5486 __u16 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
5488 (FILE_SYSTEM_POSIX_INFO
5489 *) (((char *) &pSMBr
->hdr
.Protocol
) +
5492 le32_to_cpu(response_data
->BlockSize
);
5494 le64_to_cpu(response_data
->TotalBlocks
);
5496 le64_to_cpu(response_data
->BlocksAvail
);
5497 if (response_data
->UserBlocksAvail
== cpu_to_le64(-1)) {
5498 FSData
->f_bavail
= FSData
->f_bfree
;
5501 le64_to_cpu(response_data
->UserBlocksAvail
);
5503 if (response_data
->TotalFileNodes
!= cpu_to_le64(-1))
5505 le64_to_cpu(response_data
->TotalFileNodes
);
5506 if (response_data
->FreeFileNodes
!= cpu_to_le64(-1))
5508 le64_to_cpu(response_data
->FreeFileNodes
);
5511 cifs_buf_release(pSMB
);
5521 * We can not use write of zero bytes trick to set file size due to need for
5522 * large file support. Also note that this SetPathInfo is preferred to
5523 * SetFileInfo based method in next routine which is only needed to work around
5524 * a sharing violation bugin Samba which this routine can run into.
5527 CIFSSMBSetEOF(const unsigned int xid
, struct cifs_tcon
*tcon
,
5528 const char *file_name
, __u64 size
, struct cifs_sb_info
*cifs_sb
,
5529 bool set_allocation
)
5531 struct smb_com_transaction2_spi_req
*pSMB
= NULL
;
5532 struct smb_com_transaction2_spi_rsp
*pSMBr
= NULL
;
5533 struct file_end_of_file_info
*parm_data
;
5536 int bytes_returned
= 0;
5537 int remap
= cifs_remap(cifs_sb
);
5539 __u16 params
, byte_count
, data_count
, param_offset
, offset
;
5541 cifs_dbg(FYI
, "In SetEOF\n");
5543 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
5548 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
5550 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, file_name
,
5551 PATH_MAX
, cifs_sb
->local_nls
, remap
);
5552 name_len
++; /* trailing null */
5554 } else { /* BB improve the check for buffer overruns BB */
5555 name_len
= strnlen(file_name
, PATH_MAX
);
5556 name_len
++; /* trailing null */
5557 strncpy(pSMB
->FileName
, file_name
, name_len
);
5559 params
= 6 + name_len
;
5560 data_count
= sizeof(struct file_end_of_file_info
);
5561 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5562 pSMB
->MaxDataCount
= cpu_to_le16(4100);
5563 pSMB
->MaxSetupCount
= 0;
5567 pSMB
->Reserved2
= 0;
5568 param_offset
= offsetof(struct smb_com_transaction2_spi_req
,
5569 InformationLevel
) - 4;
5570 offset
= param_offset
+ params
;
5571 if (set_allocation
) {
5572 if (tcon
->ses
->capabilities
& CAP_INFOLEVEL_PASSTHRU
)
5573 pSMB
->InformationLevel
=
5574 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2
);
5576 pSMB
->InformationLevel
=
5577 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO
);
5578 } else /* Set File Size */ {
5579 if (tcon
->ses
->capabilities
& CAP_INFOLEVEL_PASSTHRU
)
5580 pSMB
->InformationLevel
=
5581 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2
);
5583 pSMB
->InformationLevel
=
5584 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO
);
5588 (struct file_end_of_file_info
*) (((char *) &pSMB
->hdr
.Protocol
) +
5590 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
5591 pSMB
->DataOffset
= cpu_to_le16(offset
);
5592 pSMB
->SetupCount
= 1;
5593 pSMB
->Reserved3
= 0;
5594 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_PATH_INFORMATION
);
5595 byte_count
= 3 /* pad */ + params
+ data_count
;
5596 pSMB
->DataCount
= cpu_to_le16(data_count
);
5597 pSMB
->TotalDataCount
= pSMB
->DataCount
;
5598 pSMB
->ParameterCount
= cpu_to_le16(params
);
5599 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
5600 pSMB
->Reserved4
= 0;
5601 inc_rfc1001_len(pSMB
, byte_count
);
5602 parm_data
->FileSize
= cpu_to_le64(size
);
5603 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5604 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5605 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5607 cifs_dbg(FYI
, "SetPathInfo (file size) returned %d\n", rc
);
5609 cifs_buf_release(pSMB
);
5618 CIFSSMBSetFileSize(const unsigned int xid
, struct cifs_tcon
*tcon
,
5619 struct cifsFileInfo
*cfile
, __u64 size
, bool set_allocation
)
5621 struct smb_com_transaction2_sfi_req
*pSMB
= NULL
;
5622 struct file_end_of_file_info
*parm_data
;
5624 __u16 params
, param_offset
, offset
, byte_count
, count
;
5626 cifs_dbg(FYI
, "SetFileSize (via SetFileInfo) %lld\n",
5628 rc
= small_smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
);
5633 pSMB
->hdr
.Pid
= cpu_to_le16((__u16
)cfile
->pid
);
5634 pSMB
->hdr
.PidHigh
= cpu_to_le16((__u16
)(cfile
->pid
>> 16));
5637 pSMB
->MaxSetupCount
= 0;
5641 pSMB
->Reserved2
= 0;
5642 param_offset
= offsetof(struct smb_com_transaction2_sfi_req
, Fid
) - 4;
5643 offset
= param_offset
+ params
;
5645 count
= sizeof(struct file_end_of_file_info
);
5646 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5647 /* BB find exact max SMB PDU from sess structure BB */
5648 pSMB
->MaxDataCount
= cpu_to_le16(1000);
5649 pSMB
->SetupCount
= 1;
5650 pSMB
->Reserved3
= 0;
5651 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_FILE_INFORMATION
);
5652 byte_count
= 3 /* pad */ + params
+ count
;
5653 pSMB
->DataCount
= cpu_to_le16(count
);
5654 pSMB
->ParameterCount
= cpu_to_le16(params
);
5655 pSMB
->TotalDataCount
= pSMB
->DataCount
;
5656 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
5657 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
5659 (struct file_end_of_file_info
*) (((char *) &pSMB
->hdr
.Protocol
)
5661 pSMB
->DataOffset
= cpu_to_le16(offset
);
5662 parm_data
->FileSize
= cpu_to_le64(size
);
5663 pSMB
->Fid
= cfile
->fid
.netfid
;
5664 if (set_allocation
) {
5665 if (tcon
->ses
->capabilities
& CAP_INFOLEVEL_PASSTHRU
)
5666 pSMB
->InformationLevel
=
5667 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2
);
5669 pSMB
->InformationLevel
=
5670 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO
);
5671 } else /* Set File Size */ {
5672 if (tcon
->ses
->capabilities
& CAP_INFOLEVEL_PASSTHRU
)
5673 pSMB
->InformationLevel
=
5674 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2
);
5676 pSMB
->InformationLevel
=
5677 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO
);
5679 pSMB
->Reserved4
= 0;
5680 inc_rfc1001_len(pSMB
, byte_count
);
5681 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5682 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) pSMB
, 0);
5684 cifs_dbg(FYI
, "Send error in SetFileInfo (SetFileSize) = %d\n",
5688 /* Note: On -EAGAIN error only caller can retry on handle based calls
5689 since file handle passed in no longer valid */
5694 /* Some legacy servers such as NT4 require that the file times be set on
5695 an open handle, rather than by pathname - this is awkward due to
5696 potential access conflicts on the open, but it is unavoidable for these
5697 old servers since the only other choice is to go from 100 nanosecond DCE
5698 time and resort to the original setpathinfo level which takes the ancient
5699 DOS time format with 2 second granularity */
5701 CIFSSMBSetFileInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
5702 const FILE_BASIC_INFO
*data
, __u16 fid
, __u32 pid_of_opener
)
5704 struct smb_com_transaction2_sfi_req
*pSMB
= NULL
;
5707 __u16 params
, param_offset
, offset
, byte_count
, count
;
5709 cifs_dbg(FYI
, "Set Times (via SetFileInfo)\n");
5710 rc
= small_smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
);
5715 pSMB
->hdr
.Pid
= cpu_to_le16((__u16
)pid_of_opener
);
5716 pSMB
->hdr
.PidHigh
= cpu_to_le16((__u16
)(pid_of_opener
>> 16));
5719 pSMB
->MaxSetupCount
= 0;
5723 pSMB
->Reserved2
= 0;
5724 param_offset
= offsetof(struct smb_com_transaction2_sfi_req
, Fid
) - 4;
5725 offset
= param_offset
+ params
;
5727 data_offset
= (char *)pSMB
+
5728 offsetof(struct smb_hdr
, Protocol
) + offset
;
5730 count
= sizeof(FILE_BASIC_INFO
);
5731 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5732 /* BB find max SMB PDU from sess */
5733 pSMB
->MaxDataCount
= cpu_to_le16(1000);
5734 pSMB
->SetupCount
= 1;
5735 pSMB
->Reserved3
= 0;
5736 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_FILE_INFORMATION
);
5737 byte_count
= 3 /* pad */ + params
+ count
;
5738 pSMB
->DataCount
= cpu_to_le16(count
);
5739 pSMB
->ParameterCount
= cpu_to_le16(params
);
5740 pSMB
->TotalDataCount
= pSMB
->DataCount
;
5741 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
5742 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
5743 pSMB
->DataOffset
= cpu_to_le16(offset
);
5745 if (tcon
->ses
->capabilities
& CAP_INFOLEVEL_PASSTHRU
)
5746 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_FILE_BASIC_INFO2
);
5748 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_FILE_BASIC_INFO
);
5749 pSMB
->Reserved4
= 0;
5750 inc_rfc1001_len(pSMB
, byte_count
);
5751 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5752 memcpy(data_offset
, data
, sizeof(FILE_BASIC_INFO
));
5753 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) pSMB
, 0);
5755 cifs_dbg(FYI
, "Send error in Set Time (SetFileInfo) = %d\n",
5758 /* Note: On -EAGAIN error only caller can retry on handle based calls
5759 since file handle passed in no longer valid */
5765 CIFSSMBSetFileDisposition(const unsigned int xid
, struct cifs_tcon
*tcon
,
5766 bool delete_file
, __u16 fid
, __u32 pid_of_opener
)
5768 struct smb_com_transaction2_sfi_req
*pSMB
= NULL
;
5771 __u16 params
, param_offset
, offset
, byte_count
, count
;
5773 cifs_dbg(FYI
, "Set File Disposition (via SetFileInfo)\n");
5774 rc
= small_smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
);
5779 pSMB
->hdr
.Pid
= cpu_to_le16((__u16
)pid_of_opener
);
5780 pSMB
->hdr
.PidHigh
= cpu_to_le16((__u16
)(pid_of_opener
>> 16));
5783 pSMB
->MaxSetupCount
= 0;
5787 pSMB
->Reserved2
= 0;
5788 param_offset
= offsetof(struct smb_com_transaction2_sfi_req
, Fid
) - 4;
5789 offset
= param_offset
+ params
;
5791 data_offset
= (char *) (&pSMB
->hdr
.Protocol
) + offset
;
5794 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5795 /* BB find max SMB PDU from sess */
5796 pSMB
->MaxDataCount
= cpu_to_le16(1000);
5797 pSMB
->SetupCount
= 1;
5798 pSMB
->Reserved3
= 0;
5799 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_FILE_INFORMATION
);
5800 byte_count
= 3 /* pad */ + params
+ count
;
5801 pSMB
->DataCount
= cpu_to_le16(count
);
5802 pSMB
->ParameterCount
= cpu_to_le16(params
);
5803 pSMB
->TotalDataCount
= pSMB
->DataCount
;
5804 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
5805 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
5806 pSMB
->DataOffset
= cpu_to_le16(offset
);
5808 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO
);
5809 pSMB
->Reserved4
= 0;
5810 inc_rfc1001_len(pSMB
, byte_count
);
5811 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5812 *data_offset
= delete_file
? 1 : 0;
5813 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) pSMB
, 0);
5815 cifs_dbg(FYI
, "Send error in SetFileDisposition = %d\n", rc
);
5821 CIFSSMBSetPathInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
5822 const char *fileName
, const FILE_BASIC_INFO
*data
,
5823 const struct nls_table
*nls_codepage
, int remap
)
5825 TRANSACTION2_SPI_REQ
*pSMB
= NULL
;
5826 TRANSACTION2_SPI_RSP
*pSMBr
= NULL
;
5829 int bytes_returned
= 0;
5831 __u16 params
, param_offset
, offset
, byte_count
, count
;
5833 cifs_dbg(FYI
, "In SetTimes\n");
5836 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
5841 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
5843 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, fileName
,
5844 PATH_MAX
, nls_codepage
, remap
);
5845 name_len
++; /* trailing null */
5847 } else { /* BB improve the check for buffer overruns BB */
5848 name_len
= strnlen(fileName
, PATH_MAX
);
5849 name_len
++; /* trailing null */
5850 strncpy(pSMB
->FileName
, fileName
, name_len
);
5853 params
= 6 + name_len
;
5854 count
= sizeof(FILE_BASIC_INFO
);
5855 pSMB
->MaxParameterCount
= cpu_to_le16(2);
5856 /* BB find max SMB PDU from sess structure BB */
5857 pSMB
->MaxDataCount
= cpu_to_le16(1000);
5858 pSMB
->MaxSetupCount
= 0;
5862 pSMB
->Reserved2
= 0;
5863 param_offset
= offsetof(struct smb_com_transaction2_spi_req
,
5864 InformationLevel
) - 4;
5865 offset
= param_offset
+ params
;
5866 data_offset
= (char *) (&pSMB
->hdr
.Protocol
) + offset
;
5867 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
5868 pSMB
->DataOffset
= cpu_to_le16(offset
);
5869 pSMB
->SetupCount
= 1;
5870 pSMB
->Reserved3
= 0;
5871 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_PATH_INFORMATION
);
5872 byte_count
= 3 /* pad */ + params
+ count
;
5874 pSMB
->DataCount
= cpu_to_le16(count
);
5875 pSMB
->ParameterCount
= cpu_to_le16(params
);
5876 pSMB
->TotalDataCount
= pSMB
->DataCount
;
5877 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
5878 if (tcon
->ses
->capabilities
& CAP_INFOLEVEL_PASSTHRU
)
5879 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_FILE_BASIC_INFO2
);
5881 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_FILE_BASIC_INFO
);
5882 pSMB
->Reserved4
= 0;
5883 inc_rfc1001_len(pSMB
, byte_count
);
5884 memcpy(data_offset
, data
, sizeof(FILE_BASIC_INFO
));
5885 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
5886 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5887 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5889 cifs_dbg(FYI
, "SetPathInfo (times) returned %d\n", rc
);
5891 cifs_buf_release(pSMB
);
5899 /* Can not be used to set time stamps yet (due to old DOS time format) */
5900 /* Can be used to set attributes */
5901 #if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5902 handling it anyway and NT4 was what we thought it would be needed for
5903 Do not delete it until we prove whether needed for Win9x though */
5905 CIFSSMBSetAttrLegacy(unsigned int xid
, struct cifs_tcon
*tcon
, char *fileName
,
5906 __u16 dos_attrs
, const struct nls_table
*nls_codepage
)
5908 SETATTR_REQ
*pSMB
= NULL
;
5909 SETATTR_RSP
*pSMBr
= NULL
;
5914 cifs_dbg(FYI
, "In SetAttrLegacy\n");
5917 rc
= smb_init(SMB_COM_SETATTR
, 8, tcon
, (void **) &pSMB
,
5922 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
5924 ConvertToUTF16((__le16
*) pSMB
->fileName
, fileName
,
5925 PATH_MAX
, nls_codepage
);
5926 name_len
++; /* trailing null */
5928 } else { /* BB improve the check for buffer overruns BB */
5929 name_len
= strnlen(fileName
, PATH_MAX
);
5930 name_len
++; /* trailing null */
5931 strncpy(pSMB
->fileName
, fileName
, name_len
);
5933 pSMB
->attr
= cpu_to_le16(dos_attrs
);
5934 pSMB
->BufferFormat
= 0x04;
5935 inc_rfc1001_len(pSMB
, name_len
+ 1);
5936 pSMB
->ByteCount
= cpu_to_le16(name_len
+ 1);
5937 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
5938 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
5940 cifs_dbg(FYI
, "Error in LegacySetAttr = %d\n", rc
);
5942 cifs_buf_release(pSMB
);
5945 goto SetAttrLgcyRetry
;
5949 #endif /* temporarily unneeded SetAttr legacy function */
5952 cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO
*data_offset
,
5953 const struct cifs_unix_set_info_args
*args
)
5955 u64 uid
= NO_CHANGE_64
, gid
= NO_CHANGE_64
;
5956 u64 mode
= args
->mode
;
5958 if (uid_valid(args
->uid
))
5959 uid
= from_kuid(&init_user_ns
, args
->uid
);
5960 if (gid_valid(args
->gid
))
5961 gid
= from_kgid(&init_user_ns
, args
->gid
);
5964 * Samba server ignores set of file size to zero due to bugs in some
5965 * older clients, but we should be precise - we use SetFileSize to
5966 * set file size and do not want to truncate file size to zero
5967 * accidentally as happened on one Samba server beta by putting
5968 * zero instead of -1 here
5970 data_offset
->EndOfFile
= cpu_to_le64(NO_CHANGE_64
);
5971 data_offset
->NumOfBytes
= cpu_to_le64(NO_CHANGE_64
);
5972 data_offset
->LastStatusChange
= cpu_to_le64(args
->ctime
);
5973 data_offset
->LastAccessTime
= cpu_to_le64(args
->atime
);
5974 data_offset
->LastModificationTime
= cpu_to_le64(args
->mtime
);
5975 data_offset
->Uid
= cpu_to_le64(uid
);
5976 data_offset
->Gid
= cpu_to_le64(gid
);
5977 /* better to leave device as zero when it is */
5978 data_offset
->DevMajor
= cpu_to_le64(MAJOR(args
->device
));
5979 data_offset
->DevMinor
= cpu_to_le64(MINOR(args
->device
));
5980 data_offset
->Permissions
= cpu_to_le64(mode
);
5983 data_offset
->Type
= cpu_to_le32(UNIX_FILE
);
5984 else if (S_ISDIR(mode
))
5985 data_offset
->Type
= cpu_to_le32(UNIX_DIR
);
5986 else if (S_ISLNK(mode
))
5987 data_offset
->Type
= cpu_to_le32(UNIX_SYMLINK
);
5988 else if (S_ISCHR(mode
))
5989 data_offset
->Type
= cpu_to_le32(UNIX_CHARDEV
);
5990 else if (S_ISBLK(mode
))
5991 data_offset
->Type
= cpu_to_le32(UNIX_BLOCKDEV
);
5992 else if (S_ISFIFO(mode
))
5993 data_offset
->Type
= cpu_to_le32(UNIX_FIFO
);
5994 else if (S_ISSOCK(mode
))
5995 data_offset
->Type
= cpu_to_le32(UNIX_SOCKET
);
5999 CIFSSMBUnixSetFileInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
6000 const struct cifs_unix_set_info_args
*args
,
6001 u16 fid
, u32 pid_of_opener
)
6003 struct smb_com_transaction2_sfi_req
*pSMB
= NULL
;
6006 u16 params
, param_offset
, offset
, byte_count
, count
;
6008 cifs_dbg(FYI
, "Set Unix Info (via SetFileInfo)\n");
6009 rc
= small_smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
);
6014 pSMB
->hdr
.Pid
= cpu_to_le16((__u16
)pid_of_opener
);
6015 pSMB
->hdr
.PidHigh
= cpu_to_le16((__u16
)(pid_of_opener
>> 16));
6018 pSMB
->MaxSetupCount
= 0;
6022 pSMB
->Reserved2
= 0;
6023 param_offset
= offsetof(struct smb_com_transaction2_sfi_req
, Fid
) - 4;
6024 offset
= param_offset
+ params
;
6026 data_offset
= (char *)pSMB
+
6027 offsetof(struct smb_hdr
, Protocol
) + offset
;
6029 count
= sizeof(FILE_UNIX_BASIC_INFO
);
6031 pSMB
->MaxParameterCount
= cpu_to_le16(2);
6032 /* BB find max SMB PDU from sess */
6033 pSMB
->MaxDataCount
= cpu_to_le16(1000);
6034 pSMB
->SetupCount
= 1;
6035 pSMB
->Reserved3
= 0;
6036 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_FILE_INFORMATION
);
6037 byte_count
= 3 /* pad */ + params
+ count
;
6038 pSMB
->DataCount
= cpu_to_le16(count
);
6039 pSMB
->ParameterCount
= cpu_to_le16(params
);
6040 pSMB
->TotalDataCount
= pSMB
->DataCount
;
6041 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
6042 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
6043 pSMB
->DataOffset
= cpu_to_le16(offset
);
6045 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_FILE_UNIX_BASIC
);
6046 pSMB
->Reserved4
= 0;
6047 inc_rfc1001_len(pSMB
, byte_count
);
6048 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
6050 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO
*)data_offset
, args
);
6052 rc
= SendReceiveNoRsp(xid
, tcon
->ses
, (char *) pSMB
, 0);
6054 cifs_dbg(FYI
, "Send error in Set Time (SetFileInfo) = %d\n",
6057 /* Note: On -EAGAIN error only caller can retry on handle based calls
6058 since file handle passed in no longer valid */
6064 CIFSSMBUnixSetPathInfo(const unsigned int xid
, struct cifs_tcon
*tcon
,
6065 const char *file_name
,
6066 const struct cifs_unix_set_info_args
*args
,
6067 const struct nls_table
*nls_codepage
, int remap
)
6069 TRANSACTION2_SPI_REQ
*pSMB
= NULL
;
6070 TRANSACTION2_SPI_RSP
*pSMBr
= NULL
;
6073 int bytes_returned
= 0;
6074 FILE_UNIX_BASIC_INFO
*data_offset
;
6075 __u16 params
, param_offset
, offset
, count
, byte_count
;
6077 cifs_dbg(FYI
, "In SetUID/GID/Mode\n");
6079 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
6084 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
6086 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, file_name
,
6087 PATH_MAX
, nls_codepage
, remap
);
6088 name_len
++; /* trailing null */
6090 } else { /* BB improve the check for buffer overruns BB */
6091 name_len
= strnlen(file_name
, PATH_MAX
);
6092 name_len
++; /* trailing null */
6093 strncpy(pSMB
->FileName
, file_name
, name_len
);
6096 params
= 6 + name_len
;
6097 count
= sizeof(FILE_UNIX_BASIC_INFO
);
6098 pSMB
->MaxParameterCount
= cpu_to_le16(2);
6099 /* BB find max SMB PDU from sess structure BB */
6100 pSMB
->MaxDataCount
= cpu_to_le16(1000);
6101 pSMB
->MaxSetupCount
= 0;
6105 pSMB
->Reserved2
= 0;
6106 param_offset
= offsetof(struct smb_com_transaction2_spi_req
,
6107 InformationLevel
) - 4;
6108 offset
= param_offset
+ params
;
6110 (FILE_UNIX_BASIC_INFO
*) ((char *) &pSMB
->hdr
.Protocol
+
6112 memset(data_offset
, 0, count
);
6113 pSMB
->DataOffset
= cpu_to_le16(offset
);
6114 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
6115 pSMB
->SetupCount
= 1;
6116 pSMB
->Reserved3
= 0;
6117 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_PATH_INFORMATION
);
6118 byte_count
= 3 /* pad */ + params
+ count
;
6119 pSMB
->ParameterCount
= cpu_to_le16(params
);
6120 pSMB
->DataCount
= cpu_to_le16(count
);
6121 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
6122 pSMB
->TotalDataCount
= pSMB
->DataCount
;
6123 pSMB
->InformationLevel
= cpu_to_le16(SMB_SET_FILE_UNIX_BASIC
);
6124 pSMB
->Reserved4
= 0;
6125 inc_rfc1001_len(pSMB
, byte_count
);
6127 cifs_fill_unix_set_info(data_offset
, args
);
6129 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
6130 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
6131 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
6133 cifs_dbg(FYI
, "SetPathInfo (perms) returned %d\n", rc
);
6135 cifs_buf_release(pSMB
);
6141 #ifdef CONFIG_CIFS_XATTR
6143 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6144 * function used by listxattr and getxattr type calls. When ea_name is set,
6145 * it looks for that attribute name and stuffs that value into the EAData
6146 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6147 * buffer. In both cases, the return value is either the length of the
6148 * resulting data or a negative error code. If EAData is a NULL pointer then
6149 * the data isn't copied to it, but the length is returned.
6152 CIFSSMBQAllEAs(const unsigned int xid
, struct cifs_tcon
*tcon
,
6153 const unsigned char *searchName
, const unsigned char *ea_name
,
6154 char *EAData
, size_t buf_size
,
6155 const struct nls_table
*nls_codepage
, int remap
)
6157 /* BB assumes one setup word */
6158 TRANSACTION2_QPI_REQ
*pSMB
= NULL
;
6159 TRANSACTION2_QPI_RSP
*pSMBr
= NULL
;
6163 struct fealist
*ea_response_data
;
6164 struct fea
*temp_fea
;
6167 __u16 params
, byte_count
, data_offset
;
6168 unsigned int ea_name_len
= ea_name
? strlen(ea_name
) : 0;
6170 cifs_dbg(FYI
, "In Query All EAs path %s\n", searchName
);
6172 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
6177 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
6179 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, searchName
,
6180 PATH_MAX
, nls_codepage
, remap
);
6181 list_len
++; /* trailing null */
6183 } else { /* BB improve the check for buffer overruns BB */
6184 list_len
= strnlen(searchName
, PATH_MAX
);
6185 list_len
++; /* trailing null */
6186 strncpy(pSMB
->FileName
, searchName
, list_len
);
6189 params
= 2 /* level */ + 4 /* reserved */ + list_len
/* includes NUL */;
6190 pSMB
->TotalDataCount
= 0;
6191 pSMB
->MaxParameterCount
= cpu_to_le16(2);
6192 /* BB find exact max SMB PDU from sess structure BB */
6193 pSMB
->MaxDataCount
= cpu_to_le16(CIFSMaxBufSize
);
6194 pSMB
->MaxSetupCount
= 0;
6198 pSMB
->Reserved2
= 0;
6199 pSMB
->ParameterOffset
= cpu_to_le16(offsetof(
6200 struct smb_com_transaction2_qpi_req
, InformationLevel
) - 4);
6201 pSMB
->DataCount
= 0;
6202 pSMB
->DataOffset
= 0;
6203 pSMB
->SetupCount
= 1;
6204 pSMB
->Reserved3
= 0;
6205 pSMB
->SubCommand
= cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION
);
6206 byte_count
= params
+ 1 /* pad */ ;
6207 pSMB
->TotalParameterCount
= cpu_to_le16(params
);
6208 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
6209 pSMB
->InformationLevel
= cpu_to_le16(SMB_INFO_QUERY_ALL_EAS
);
6210 pSMB
->Reserved4
= 0;
6211 inc_rfc1001_len(pSMB
, byte_count
);
6212 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
6214 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
6215 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
6217 cifs_dbg(FYI
, "Send error in QueryAllEAs = %d\n", rc
);
6222 /* BB also check enough total bytes returned */
6223 /* BB we need to improve the validity checking
6224 of these trans2 responses */
6226 rc
= validate_t2((struct smb_t2_rsp
*)pSMBr
);
6227 if (rc
|| get_bcc(&pSMBr
->hdr
) < 4) {
6228 rc
= -EIO
; /* bad smb */
6232 /* check that length of list is not more than bcc */
6233 /* check that each entry does not go beyond length
6235 /* check that each element of each entry does not
6236 go beyond end of list */
6237 /* validate_trans2_offsets() */
6238 /* BB check if start of smb + data_offset > &bcc+ bcc */
6240 data_offset
= le16_to_cpu(pSMBr
->t2
.DataOffset
);
6241 ea_response_data
= (struct fealist
*)
6242 (((char *) &pSMBr
->hdr
.Protocol
) + data_offset
);
6244 list_len
= le32_to_cpu(ea_response_data
->list_len
);
6245 cifs_dbg(FYI
, "ea length %d\n", list_len
);
6246 if (list_len
<= 8) {
6247 cifs_dbg(FYI
, "empty EA list returned from server\n");
6248 /* didn't find the named attribute */
6254 /* make sure list_len doesn't go past end of SMB */
6255 end_of_smb
= (char *)pByteArea(&pSMBr
->hdr
) + get_bcc(&pSMBr
->hdr
);
6256 if ((char *)ea_response_data
+ list_len
> end_of_smb
) {
6257 cifs_dbg(FYI
, "EA list appears to go beyond SMB\n");
6262 /* account for ea list len */
6264 temp_fea
= ea_response_data
->list
;
6265 temp_ptr
= (char *)temp_fea
;
6266 while (list_len
> 0) {
6267 unsigned int name_len
;
6272 /* make sure we can read name_len and value_len */
6274 cifs_dbg(FYI
, "EA entry goes beyond length of list\n");
6279 name_len
= temp_fea
->name_len
;
6280 value_len
= le16_to_cpu(temp_fea
->value_len
);
6281 list_len
-= name_len
+ 1 + value_len
;
6283 cifs_dbg(FYI
, "EA entry goes beyond length of list\n");
6289 if (ea_name_len
== name_len
&&
6290 memcmp(ea_name
, temp_ptr
, name_len
) == 0) {
6291 temp_ptr
+= name_len
+ 1;
6295 if ((size_t)value_len
> buf_size
) {
6299 memcpy(EAData
, temp_ptr
, value_len
);
6303 /* account for prefix user. and trailing null */
6304 rc
+= (5 + 1 + name_len
);
6305 if (rc
< (int) buf_size
) {
6306 memcpy(EAData
, "user.", 5);
6308 memcpy(EAData
, temp_ptr
, name_len
);
6310 /* null terminate name */
6313 } else if (buf_size
== 0) {
6314 /* skip copy - calc size only */
6316 /* stop before overrun buffer */
6321 temp_ptr
+= name_len
+ 1 + value_len
;
6322 temp_fea
= (struct fea
*)temp_ptr
;
6325 /* didn't find the named attribute */
6330 cifs_buf_release(pSMB
);
6338 CIFSSMBSetEA(const unsigned int xid
, struct cifs_tcon
*tcon
,
6339 const char *fileName
, const char *ea_name
, const void *ea_value
,
6340 const __u16 ea_value_len
, const struct nls_table
*nls_codepage
,
6343 struct smb_com_transaction2_spi_req
*pSMB
= NULL
;
6344 struct smb_com_transaction2_spi_rsp
*pSMBr
= NULL
;
6345 struct fealist
*parm_data
;
6348 int bytes_returned
= 0;
6349 __u16 params
, param_offset
, byte_count
, offset
, count
;
6351 cifs_dbg(FYI
, "In SetEA\n");
6353 rc
= smb_init(SMB_COM_TRANSACTION2
, 15, tcon
, (void **) &pSMB
,
6358 if (pSMB
->hdr
.Flags2
& SMBFLG2_UNICODE
) {
6360 cifsConvertToUTF16((__le16
*) pSMB
->FileName
, fileName
,
6361 PATH_MAX
, nls_codepage
, remap
);
6362 name_len
++; /* trailing null */
6364 } else { /* BB improve the check for buffer overruns BB */
6365 name_len
= strnlen(fileName
, PATH_MAX
);
6366 name_len
++; /* trailing null */
6367 strncpy(pSMB
->FileName
, fileName
, name_len
);
6370 params
= 6 + name_len
;
6372 /* done calculating parms using name_len of file name,
6373 now use name_len to calculate length of ea name
6374 we are going to create in the inode xattrs */
6375 if (ea_name
== NULL
)
6378 name_len
= strnlen(ea_name
, 255);
6380 count
= sizeof(*parm_data
) + ea_value_len
+ name_len
;
6381 pSMB
->MaxParameterCount
= cpu_to_le16(2);
6382 /* BB find max SMB PDU from sess */
6383 pSMB
->MaxDataCount
= cpu_to_le16(1000);
6384 pSMB
->MaxSetupCount
= 0;
6388 pSMB
->Reserved2
= 0;
6389 param_offset
= offsetof(struct smb_com_transaction2_spi_req
,
6390 InformationLevel
) - 4;
6391 offset
= param_offset
+ params
;
6392 pSMB
->InformationLevel
=
6393 cpu_to_le16(SMB_SET_FILE_EA
);
6396 (struct fealist
*) (((char *) &pSMB
->hdr
.Protocol
) +
6398 pSMB
->ParameterOffset
= cpu_to_le16(param_offset
);
6399 pSMB
->DataOffset
= cpu_to_le16(offset
);
6400 pSMB
->SetupCount
= 1;
6401 pSMB
->Reserved3
= 0;
6402 pSMB
->SubCommand
= cpu_to_le16(TRANS2_SET_PATH_INFORMATION
);
6403 byte_count
= 3 /* pad */ + params
+ count
;
6404 pSMB
->DataCount
= cpu_to_le16(count
);
6405 parm_data
->list_len
= cpu_to_le32(count
);
6406 parm_data
->list
[0].EA_flags
= 0;
6407 /* we checked above that name len is less than 255 */
6408 parm_data
->list
[0].name_len
= (__u8
)name_len
;
6409 /* EA names are always ASCII */
6411 strncpy(parm_data
->list
[0].name
, ea_name
, name_len
);
6412 parm_data
->list
[0].name
[name_len
] = 0;
6413 parm_data
->list
[0].value_len
= cpu_to_le16(ea_value_len
);
6414 /* caller ensures that ea_value_len is less than 64K but
6415 we need to ensure that it fits within the smb */
6417 /*BB add length check to see if it would fit in
6418 negotiated SMB buffer size BB */
6419 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6421 memcpy(parm_data
->list
[0].name
+name_len
+1,
6422 ea_value
, ea_value_len
);
6424 pSMB
->TotalDataCount
= pSMB
->DataCount
;
6425 pSMB
->ParameterCount
= cpu_to_le16(params
);
6426 pSMB
->TotalParameterCount
= pSMB
->ParameterCount
;
6427 pSMB
->Reserved4
= 0;
6428 inc_rfc1001_len(pSMB
, byte_count
);
6429 pSMB
->ByteCount
= cpu_to_le16(byte_count
);
6430 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
6431 (struct smb_hdr
*) pSMBr
, &bytes_returned
, 0);
6433 cifs_dbg(FYI
, "SetPathInfo (EA) returned %d\n", rc
);
6435 cifs_buf_release(pSMB
);
6444 #ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6446 * Years ago the kernel added a "dnotify" function for Samba server,
6447 * to allow network clients (such as Windows) to display updated
6448 * lists of files in directory listings automatically when
6449 * files are added by one user when another user has the
6450 * same directory open on their desktop. The Linux cifs kernel
6451 * client hooked into the kernel side of this interface for
6452 * the same reason, but ironically when the VFS moved from
6453 * "dnotify" to "inotify" it became harder to plug in Linux
6454 * network file system clients (the most obvious use case
6455 * for notify interfaces is when multiple users can update
6456 * the contents of the same directory - exactly what network
6457 * file systems can do) although the server (Samba) could
6458 * still use it. For the short term we leave the worker
6459 * function ifdeffed out (below) until inotify is fixed
6460 * in the VFS to make it easier to plug in network file
6461 * system clients. If inotify turns out to be permanently
6462 * incompatible for network fs clients, we could instead simply
6463 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6465 int CIFSSMBNotify(const unsigned int xid
, struct cifs_tcon
*tcon
,
6466 const int notify_subdirs
, const __u16 netfid
,
6467 __u32 filter
, struct file
*pfile
, int multishot
,
6468 const struct nls_table
*nls_codepage
)
6471 struct smb_com_transaction_change_notify_req
*pSMB
= NULL
;
6472 struct smb_com_ntransaction_change_notify_rsp
*pSMBr
= NULL
;
6473 struct dir_notify_req
*dnotify_req
;
6476 cifs_dbg(FYI
, "In CIFSSMBNotify for file handle %d\n", (int)netfid
);
6477 rc
= smb_init(SMB_COM_NT_TRANSACT
, 23, tcon
, (void **) &pSMB
,
6482 pSMB
->TotalParameterCount
= 0 ;
6483 pSMB
->TotalDataCount
= 0;
6484 pSMB
->MaxParameterCount
= cpu_to_le32(2);
6485 pSMB
->MaxDataCount
= cpu_to_le32(CIFSMaxBufSize
& 0xFFFFFF00);
6486 pSMB
->MaxSetupCount
= 4;
6488 pSMB
->ParameterOffset
= 0;
6489 pSMB
->DataCount
= 0;
6490 pSMB
->DataOffset
= 0;
6491 pSMB
->SetupCount
= 4; /* single byte does not need le conversion */
6492 pSMB
->SubCommand
= cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE
);
6493 pSMB
->ParameterCount
= pSMB
->TotalParameterCount
;
6495 pSMB
->WatchTree
= 1; /* one byte - no le conversion needed */
6496 pSMB
->Reserved2
= 0;
6497 pSMB
->CompletionFilter
= cpu_to_le32(filter
);
6498 pSMB
->Fid
= netfid
; /* file handle always le */
6499 pSMB
->ByteCount
= 0;
6501 rc
= SendReceive(xid
, tcon
->ses
, (struct smb_hdr
*) pSMB
,
6502 (struct smb_hdr
*)pSMBr
, &bytes_returned
,
6505 cifs_dbg(FYI
, "Error in Notify = %d\n", rc
);
6507 /* Add file to outstanding requests */
6508 /* BB change to kmem cache alloc */
6509 dnotify_req
= kmalloc(
6510 sizeof(struct dir_notify_req
),
6513 dnotify_req
->Pid
= pSMB
->hdr
.Pid
;
6514 dnotify_req
->PidHigh
= pSMB
->hdr
.PidHigh
;
6515 dnotify_req
->Mid
= pSMB
->hdr
.Mid
;
6516 dnotify_req
->Tid
= pSMB
->hdr
.Tid
;
6517 dnotify_req
->Uid
= pSMB
->hdr
.Uid
;
6518 dnotify_req
->netfid
= netfid
;
6519 dnotify_req
->pfile
= pfile
;
6520 dnotify_req
->filter
= filter
;
6521 dnotify_req
->multishot
= multishot
;
6522 spin_lock(&GlobalMid_Lock
);
6523 list_add_tail(&dnotify_req
->lhead
,
6524 &GlobalDnotifyReqList
);
6525 spin_unlock(&GlobalMid_Lock
);
6529 cifs_buf_release(pSMB
);
6532 #endif /* was needed for dnotify, and will be needed for inotify when VFS fix */