4 * Directory search handling
6 * Copyright (C) International Business Machines Corp., 2004, 2005
7 * Author(s): Steve French (sfrench@us.ibm.com)
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 #include <linux/pagemap.h>
25 #include <linux/stat.h>
26 #include <linux/smp_lock.h>
29 #include "cifsproto.h"
30 #include "cifs_unicode.h"
31 #include "cifs_debug.h"
32 #include "cifs_fs_sb.h"
35 #ifdef CONFIG_CIFS_DEBUG2
36 static void dump_cifs_file_struct(struct file
*file
, char *label
)
38 struct cifsFileInfo
* cf
;
41 cf
= file
->private_data
;
43 cFYI(1,("empty cifs private file data"));
46 if(cf
->invalidHandle
) {
47 cFYI(1,("invalid handle"));
49 if(cf
->srch_inf
.endOfSearch
) {
50 cFYI(1,("end of search"));
52 if(cf
->srch_inf
.emptyDir
) {
53 cFYI(1,("empty dir"));
60 /* Returns one if new inode created (which therefore needs to be hashed) */
61 /* Might check in the future if inode number changed so we can rehash inode */
62 static int construct_dentry(struct qstr
*qstring
, struct file
*file
,
63 struct inode
**ptmp_inode
, struct dentry
**pnew_dentry
)
65 struct dentry
*tmp_dentry
;
66 struct cifs_sb_info
*cifs_sb
;
67 struct cifsTconInfo
*pTcon
;
70 cFYI(1, ("For %s", qstring
->name
));
71 cifs_sb
= CIFS_SB(file
->f_dentry
->d_sb
);
72 pTcon
= cifs_sb
->tcon
;
74 qstring
->hash
= full_name_hash(qstring
->name
, qstring
->len
);
75 tmp_dentry
= d_lookup(file
->f_dentry
, qstring
);
77 cFYI(0, ("existing dentry with inode 0x%p", tmp_dentry
->d_inode
));
78 *ptmp_inode
= tmp_dentry
->d_inode
;
79 /* BB overwrite old name? i.e. tmp_dentry->d_name and tmp_dentry->d_name.len??*/
80 if(*ptmp_inode
== NULL
) {
81 *ptmp_inode
= new_inode(file
->f_dentry
->d_sb
);
82 if(*ptmp_inode
== NULL
)
87 tmp_dentry
= d_alloc(file
->f_dentry
, qstring
);
88 if(tmp_dentry
== NULL
) {
89 cERROR(1,("Failed allocating dentry"));
94 *ptmp_inode
= new_inode(file
->f_dentry
->d_sb
);
96 tmp_dentry
->d_op
= &cifs_ci_dentry_ops
;
98 tmp_dentry
->d_op
= &cifs_dentry_ops
;
99 if(*ptmp_inode
== NULL
)
104 tmp_dentry
->d_time
= jiffies
;
105 *pnew_dentry
= tmp_dentry
;
109 static void fill_in_inode(struct inode
*tmp_inode
, int new_buf_type
,
110 char * buf
, int *pobject_type
, int isNewInode
)
113 struct timespec local_mtime
;
115 struct cifsInodeInfo
*cifsInfo
= CIFS_I(tmp_inode
);
116 struct cifs_sb_info
*cifs_sb
= CIFS_SB(tmp_inode
->i_sb
);
118 __u64 allocation_size
;
121 /* save mtime and size */
122 local_mtime
= tmp_inode
->i_mtime
;
123 local_size
= tmp_inode
->i_size
;
126 FILE_DIRECTORY_INFO
*pfindData
= (FILE_DIRECTORY_INFO
*)buf
;
128 attr
= le32_to_cpu(pfindData
->ExtFileAttributes
);
129 allocation_size
= le64_to_cpu(pfindData
->AllocationSize
);
130 end_of_file
= le64_to_cpu(pfindData
->EndOfFile
);
132 cifs_NTtimeToUnix(le64_to_cpu(pfindData
->LastAccessTime
));
134 cifs_NTtimeToUnix(le64_to_cpu(pfindData
->LastWriteTime
));
136 cifs_NTtimeToUnix(le64_to_cpu(pfindData
->ChangeTime
));
137 } else { /* legacy, OS2 and DOS style */
138 FIND_FILE_STANDARD_INFO
* pfindData
=
139 (FIND_FILE_STANDARD_INFO
*)buf
;
141 attr
= le16_to_cpu(pfindData
->Attributes
);
142 allocation_size
= le32_to_cpu(pfindData
->AllocationSize
);
143 end_of_file
= le32_to_cpu(pfindData
->DataSize
);
144 tmp_inode
->i_atime
= CURRENT_TIME
;
145 /* tmp_inode->i_mtime = BB FIXME - add dos time handling
146 tmp_inode->i_ctime = 0; BB FIXME */
150 /* Linux can not store file creation time unfortunately so ignore it */
152 cifsInfo
->cifsAttrs
= attr
;
153 cifsInfo
->time
= jiffies
;
155 /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
156 /* 2767 perms - indicate mandatory locking */
157 /* BB fill in uid and gid here? with help from winbind?
158 or retrieve from NTFS stream extended attribute */
159 if (atomic_read(&cifsInfo
->inUse
) == 0) {
160 tmp_inode
->i_uid
= cifs_sb
->mnt_uid
;
161 tmp_inode
->i_gid
= cifs_sb
->mnt_gid
;
162 /* set default mode. will override for dirs below */
163 tmp_inode
->i_mode
= cifs_sb
->mnt_file_mode
;
165 /* mask off the type bits since it gets set
166 below and we do not want to get two type
168 tmp_inode
->i_mode
&= ~S_IFMT
;
171 if (attr
& ATTR_DIRECTORY
) {
172 *pobject_type
= DT_DIR
;
173 /* override default perms since we do not lock dirs */
174 if(atomic_read(&cifsInfo
->inUse
) == 0) {
175 tmp_inode
->i_mode
= cifs_sb
->mnt_dir_mode
;
177 tmp_inode
->i_mode
|= S_IFDIR
;
178 } else if ((cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_UNX_EMUL
) &&
179 (attr
& ATTR_SYSTEM
)) {
180 if (end_of_file
== 0) {
181 *pobject_type
= DT_FIFO
;
182 tmp_inode
->i_mode
|= S_IFIFO
;
184 /* rather than get the type here, we mark the
185 inode as needing revalidate and get the real type
186 (blk vs chr vs. symlink) later ie in lookup */
187 *pobject_type
= DT_REG
;
188 tmp_inode
->i_mode
|= S_IFREG
;
191 /* we no longer mark these because we could not follow them */
192 /* } else if (attr & ATTR_REPARSE) {
193 *pobject_type = DT_LNK;
194 tmp_inode->i_mode |= S_IFLNK; */
196 *pobject_type
= DT_REG
;
197 tmp_inode
->i_mode
|= S_IFREG
;
198 if (attr
& ATTR_READONLY
)
199 tmp_inode
->i_mode
&= ~(S_IWUGO
);
200 } /* could add code here - to validate if device or weird share type? */
202 /* can not fill in nlink here as in qpathinfo version and Unx search */
203 if (atomic_read(&cifsInfo
->inUse
) == 0) {
204 atomic_set(&cifsInfo
->inUse
, 1);
207 if (is_size_safe_to_change(cifsInfo
)) {
208 /* can not safely change the file size here if the
209 client is writing to it due to potential races */
210 i_size_write(tmp_inode
, end_of_file
);
212 /* 512 bytes (2**9) is the fake blocksize that must be used */
213 /* for this calculation, even though the reported blocksize is larger */
214 tmp_inode
->i_blocks
= (512 - 1 + allocation_size
) >> 9;
217 if (allocation_size
< end_of_file
)
218 cFYI(1, ("May be sparse file, allocation less than file size"));
219 cFYI(1, ("File Size %ld and blocks %llu and blocksize %ld",
220 (unsigned long)tmp_inode
->i_size
,
221 (unsigned long long)tmp_inode
->i_blocks
,
222 tmp_inode
->i_blksize
));
223 if (S_ISREG(tmp_inode
->i_mode
)) {
224 cFYI(1, ("File inode"));
225 tmp_inode
->i_op
= &cifs_file_inode_ops
;
226 if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_DIRECT_IO
) {
227 if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_BRL
)
228 tmp_inode
->i_fop
= &cifs_file_direct_nobrl_ops
;
230 tmp_inode
->i_fop
= &cifs_file_direct_ops
;
232 } else if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_BRL
)
233 tmp_inode
->i_fop
= &cifs_file_nobrl_ops
;
235 tmp_inode
->i_fop
= &cifs_file_ops
;
237 if((cifs_sb
->tcon
) && (cifs_sb
->tcon
->ses
) &&
238 (cifs_sb
->tcon
->ses
->server
->maxBuf
<
239 PAGE_CACHE_SIZE
+ MAX_CIFS_HDR_SIZE
))
240 tmp_inode
->i_data
.a_ops
= &cifs_addr_ops_smallbuf
;
242 tmp_inode
->i_data
.a_ops
= &cifs_addr_ops
;
245 return; /* No sense invalidating pages for new inode
246 since have not started caching readahead file
249 if (timespec_equal(&tmp_inode
->i_mtime
, &local_mtime
) &&
250 (local_size
== tmp_inode
->i_size
)) {
251 cFYI(1, ("inode exists but unchanged"));
253 /* file may have changed on server */
254 cFYI(1, ("invalidate inode, readdir detected change"));
255 invalidate_remote_inode(tmp_inode
);
257 } else if (S_ISDIR(tmp_inode
->i_mode
)) {
258 cFYI(1, ("Directory inode"));
259 tmp_inode
->i_op
= &cifs_dir_inode_ops
;
260 tmp_inode
->i_fop
= &cifs_dir_ops
;
261 } else if (S_ISLNK(tmp_inode
->i_mode
)) {
262 cFYI(1, ("Symbolic Link inode"));
263 tmp_inode
->i_op
= &cifs_symlink_inode_ops
;
265 cFYI(1, ("Init special inode"));
266 init_special_inode(tmp_inode
, tmp_inode
->i_mode
,
271 static void unix_fill_in_inode(struct inode
*tmp_inode
,
272 FILE_UNIX_INFO
*pfindData
, int *pobject_type
, int isNewInode
)
275 struct timespec local_mtime
;
277 struct cifsInodeInfo
*cifsInfo
= CIFS_I(tmp_inode
);
278 struct cifs_sb_info
*cifs_sb
= CIFS_SB(tmp_inode
->i_sb
);
280 __u32 type
= le32_to_cpu(pfindData
->Type
);
281 __u64 num_of_bytes
= le64_to_cpu(pfindData
->NumOfBytes
);
282 __u64 end_of_file
= le64_to_cpu(pfindData
->EndOfFile
);
283 cifsInfo
->time
= jiffies
;
284 atomic_inc(&cifsInfo
->inUse
);
286 /* save mtime and size */
287 local_mtime
= tmp_inode
->i_mtime
;
288 local_size
= tmp_inode
->i_size
;
291 cifs_NTtimeToUnix(le64_to_cpu(pfindData
->LastAccessTime
));
293 cifs_NTtimeToUnix(le64_to_cpu(pfindData
->LastModificationTime
));
295 cifs_NTtimeToUnix(le64_to_cpu(pfindData
->LastStatusChange
));
297 tmp_inode
->i_mode
= le64_to_cpu(pfindData
->Permissions
);
298 /* since we set the inode type below we need to mask off type
299 to avoid strange results if bits above were corrupt */
300 tmp_inode
->i_mode
&= ~S_IFMT
;
301 if (type
== UNIX_FILE
) {
302 *pobject_type
= DT_REG
;
303 tmp_inode
->i_mode
|= S_IFREG
;
304 } else if (type
== UNIX_SYMLINK
) {
305 *pobject_type
= DT_LNK
;
306 tmp_inode
->i_mode
|= S_IFLNK
;
307 } else if (type
== UNIX_DIR
) {
308 *pobject_type
= DT_DIR
;
309 tmp_inode
->i_mode
|= S_IFDIR
;
310 } else if (type
== UNIX_CHARDEV
) {
311 *pobject_type
= DT_CHR
;
312 tmp_inode
->i_mode
|= S_IFCHR
;
313 tmp_inode
->i_rdev
= MKDEV(le64_to_cpu(pfindData
->DevMajor
),
314 le64_to_cpu(pfindData
->DevMinor
) & MINORMASK
);
315 } else if (type
== UNIX_BLOCKDEV
) {
316 *pobject_type
= DT_BLK
;
317 tmp_inode
->i_mode
|= S_IFBLK
;
318 tmp_inode
->i_rdev
= MKDEV(le64_to_cpu(pfindData
->DevMajor
),
319 le64_to_cpu(pfindData
->DevMinor
) & MINORMASK
);
320 } else if (type
== UNIX_FIFO
) {
321 *pobject_type
= DT_FIFO
;
322 tmp_inode
->i_mode
|= S_IFIFO
;
323 } else if (type
== UNIX_SOCKET
) {
324 *pobject_type
= DT_SOCK
;
325 tmp_inode
->i_mode
|= S_IFSOCK
;
327 /* safest to just call it a file */
328 *pobject_type
= DT_REG
;
329 tmp_inode
->i_mode
|= S_IFREG
;
330 cFYI(1,("unknown inode type %d",type
));
333 tmp_inode
->i_uid
= le64_to_cpu(pfindData
->Uid
);
334 tmp_inode
->i_gid
= le64_to_cpu(pfindData
->Gid
);
335 tmp_inode
->i_nlink
= le64_to_cpu(pfindData
->Nlinks
);
337 if (is_size_safe_to_change(cifsInfo
)) {
338 /* can not safely change the file size here if the
339 client is writing to it due to potential races */
340 i_size_write(tmp_inode
,end_of_file
);
342 /* 512 bytes (2**9) is the fake blocksize that must be used */
343 /* for this calculation, not the real blocksize */
344 tmp_inode
->i_blocks
= (512 - 1 + num_of_bytes
) >> 9;
347 if (S_ISREG(tmp_inode
->i_mode
)) {
348 cFYI(1, ("File inode"));
349 tmp_inode
->i_op
= &cifs_file_inode_ops
;
351 if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_DIRECT_IO
) {
352 if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_BRL
)
353 tmp_inode
->i_fop
= &cifs_file_direct_nobrl_ops
;
355 tmp_inode
->i_fop
= &cifs_file_direct_ops
;
357 } else if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_BRL
)
358 tmp_inode
->i_fop
= &cifs_file_nobrl_ops
;
360 tmp_inode
->i_fop
= &cifs_file_ops
;
362 if((cifs_sb
->tcon
) && (cifs_sb
->tcon
->ses
) &&
363 (cifs_sb
->tcon
->ses
->server
->maxBuf
<
364 PAGE_CACHE_SIZE
+ MAX_CIFS_HDR_SIZE
))
365 tmp_inode
->i_data
.a_ops
= &cifs_addr_ops_smallbuf
;
367 tmp_inode
->i_data
.a_ops
= &cifs_addr_ops
;
370 return; /* No sense invalidating pages for new inode since we
371 have not started caching readahead file data yet */
373 if (timespec_equal(&tmp_inode
->i_mtime
, &local_mtime
) &&
374 (local_size
== tmp_inode
->i_size
)) {
375 cFYI(1, ("inode exists but unchanged"));
377 /* file may have changed on server */
378 cFYI(1, ("invalidate inode, readdir detected change"));
379 invalidate_remote_inode(tmp_inode
);
381 } else if (S_ISDIR(tmp_inode
->i_mode
)) {
382 cFYI(1, ("Directory inode"));
383 tmp_inode
->i_op
= &cifs_dir_inode_ops
;
384 tmp_inode
->i_fop
= &cifs_dir_ops
;
385 } else if (S_ISLNK(tmp_inode
->i_mode
)) {
386 cFYI(1, ("Symbolic Link inode"));
387 tmp_inode
->i_op
= &cifs_symlink_inode_ops
;
388 /* tmp_inode->i_fop = *//* do not need to set to anything */
390 cFYI(1, ("Special inode"));
391 init_special_inode(tmp_inode
, tmp_inode
->i_mode
,
396 static int initiate_cifs_search(const int xid
, struct file
*file
)
400 struct cifsFileInfo
* cifsFile
;
401 struct cifs_sb_info
*cifs_sb
;
402 struct cifsTconInfo
*pTcon
;
404 if(file
->private_data
== NULL
) {
406 kmalloc(sizeof(struct cifsFileInfo
),GFP_KERNEL
);
409 if(file
->private_data
== NULL
) {
412 memset(file
->private_data
,0,sizeof(struct cifsFileInfo
));
414 cifsFile
= file
->private_data
;
415 cifsFile
->invalidHandle
= TRUE
;
416 cifsFile
->srch_inf
.endOfSearch
= FALSE
;
418 if(file
->f_dentry
== NULL
)
421 cifs_sb
= CIFS_SB(file
->f_dentry
->d_sb
);
425 pTcon
= cifs_sb
->tcon
;
429 full_path
= build_path_from_dentry(file
->f_dentry
);
431 if(full_path
== NULL
) {
435 cFYI(1, ("Full path: %s start at: %lld", full_path
, file
->f_pos
));
438 /* test for Unix extensions */
439 if (pTcon
->ses
->capabilities
& CAP_UNIX
) {
440 cifsFile
->srch_inf
.info_level
= SMB_FIND_FILE_UNIX
;
441 } else if ((pTcon
->ses
->capabilities
&
442 (CAP_NT_SMBS
| CAP_NT_FIND
)) == 0) {
443 cifsFile
->srch_inf
.info_level
= SMB_FIND_FILE_INFO_STANDARD
;
444 } else if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SERVER_INUM
) {
445 cifsFile
->srch_inf
.info_level
= SMB_FIND_FILE_ID_FULL_DIR_INFO
;
446 } else /* not srvinos - BB fixme add check for backlevel? */ {
447 cifsFile
->srch_inf
.info_level
= SMB_FIND_FILE_DIRECTORY_INFO
;
450 rc
= CIFSFindFirst(xid
, pTcon
,full_path
,cifs_sb
->local_nls
,
451 &cifsFile
->netfid
, &cifsFile
->srch_inf
,
452 cifs_sb
->mnt_cifs_flags
&
453 CIFS_MOUNT_MAP_SPECIAL_CHR
, CIFS_DIR_SEP(cifs_sb
));
455 cifsFile
->invalidHandle
= FALSE
;
456 if((rc
== -EOPNOTSUPP
) &&
457 (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SERVER_INUM
)) {
458 cifs_sb
->mnt_cifs_flags
&= ~CIFS_MOUNT_SERVER_INUM
;
465 /* return length of unicode string in bytes */
466 static int cifs_unicode_bytelen(char *str
)
469 __le16
* ustr
= (__le16
*)str
;
471 for(len
=0;len
<= PATH_MAX
;len
++) {
475 cFYI(1,("Unicode string longer than PATH_MAX found"));
479 static char *nxt_dir_entry(char *old_entry
, char *end_of_smb
, int level
)
482 FILE_DIRECTORY_INFO
* pDirInfo
= (FILE_DIRECTORY_INFO
*)old_entry
;
484 if(level
== SMB_FIND_FILE_INFO_STANDARD
) {
485 FIND_FILE_STANDARD_INFO
* pfData
;
486 pfData
= (FIND_FILE_STANDARD_INFO
*)pDirInfo
;
488 new_entry
= old_entry
+ sizeof(FIND_FILE_STANDARD_INFO
) +
489 pfData
->FileNameLength
;
491 new_entry
= old_entry
+ le32_to_cpu(pDirInfo
->NextEntryOffset
);
492 cFYI(1,("new entry %p old entry %p",new_entry
,old_entry
));
493 /* validate that new_entry is not past end of SMB */
494 if(new_entry
>= end_of_smb
) {
496 ("search entry %p began after end of SMB %p old entry %p",
497 new_entry
, end_of_smb
, old_entry
));
499 } else if(((level
== SMB_FIND_FILE_INFO_STANDARD
) &&
500 (new_entry
+ sizeof(FIND_FILE_STANDARD_INFO
) > end_of_smb
)) ||
501 ((level
!= SMB_FIND_FILE_INFO_STANDARD
) &&
502 (new_entry
+ sizeof(FILE_DIRECTORY_INFO
) > end_of_smb
))) {
503 cERROR(1,("search entry %p extends after end of SMB %p",
504 new_entry
, end_of_smb
));
511 #define UNICODE_DOT cpu_to_le16(0x2e)
513 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
514 static int cifs_entry_is_dot(char *current_entry
, struct cifsFileInfo
*cfile
)
517 char * filename
= NULL
;
520 if(cfile
->srch_inf
.info_level
== SMB_FIND_FILE_UNIX
) {
521 FILE_UNIX_INFO
* pFindData
= (FILE_UNIX_INFO
*)current_entry
;
522 filename
= &pFindData
->FileName
[0];
523 if(cfile
->srch_inf
.unicode
) {
524 len
= cifs_unicode_bytelen(filename
);
526 /* BB should we make this strnlen of PATH_MAX? */
527 len
= strnlen(filename
, 5);
529 } else if(cfile
->srch_inf
.info_level
== SMB_FIND_FILE_DIRECTORY_INFO
) {
530 FILE_DIRECTORY_INFO
* pFindData
=
531 (FILE_DIRECTORY_INFO
*)current_entry
;
532 filename
= &pFindData
->FileName
[0];
533 len
= le32_to_cpu(pFindData
->FileNameLength
);
534 } else if(cfile
->srch_inf
.info_level
==
535 SMB_FIND_FILE_FULL_DIRECTORY_INFO
) {
536 FILE_FULL_DIRECTORY_INFO
* pFindData
=
537 (FILE_FULL_DIRECTORY_INFO
*)current_entry
;
538 filename
= &pFindData
->FileName
[0];
539 len
= le32_to_cpu(pFindData
->FileNameLength
);
540 } else if(cfile
->srch_inf
.info_level
==
541 SMB_FIND_FILE_ID_FULL_DIR_INFO
) {
542 SEARCH_ID_FULL_DIR_INFO
* pFindData
=
543 (SEARCH_ID_FULL_DIR_INFO
*)current_entry
;
544 filename
= &pFindData
->FileName
[0];
545 len
= le32_to_cpu(pFindData
->FileNameLength
);
546 } else if(cfile
->srch_inf
.info_level
==
547 SMB_FIND_FILE_BOTH_DIRECTORY_INFO
) {
548 FILE_BOTH_DIRECTORY_INFO
* pFindData
=
549 (FILE_BOTH_DIRECTORY_INFO
*)current_entry
;
550 filename
= &pFindData
->FileName
[0];
551 len
= le32_to_cpu(pFindData
->FileNameLength
);
552 } else if(cfile
->srch_inf
.info_level
== SMB_FIND_FILE_INFO_STANDARD
) {
553 FIND_FILE_STANDARD_INFO
* pFindData
=
554 (FIND_FILE_STANDARD_INFO
*)current_entry
;
555 filename
= &pFindData
->FileName
[0];
556 len
= pFindData
->FileNameLength
;
558 cFYI(1,("Unknown findfirst level %d",cfile
->srch_inf
.info_level
));
562 if(cfile
->srch_inf
.unicode
) {
563 __le16
*ufilename
= (__le16
*)filename
;
566 if(ufilename
[0] == UNICODE_DOT
)
568 } else if(len
== 4) {
570 if((ufilename
[0] == UNICODE_DOT
)
571 &&(ufilename
[1] == UNICODE_DOT
))
576 if(filename
[0] == '.')
578 } else if(len
== 2) {
579 if((filename
[0] == '.') && (filename
[1] == '.'))
588 /* Check if directory that we are searching has changed so we can decide
589 whether we can use the cached search results from the previous search */
590 static int is_dir_changed(struct file
* file
)
592 struct inode
* inode
;
593 struct cifsInodeInfo
*cifsInfo
;
595 if(file
->f_dentry
== NULL
)
598 inode
= file
->f_dentry
->d_inode
;
603 cifsInfo
= CIFS_I(inode
);
605 if(cifsInfo
->time
== 0)
606 return 1; /* directory was changed, perhaps due to unlink */
612 /* find the corresponding entry in the search */
613 /* Note that the SMB server returns search entries for . and .. which
614 complicates logic here if we choose to parse for them and we do not
615 assume that they are located in the findfirst return buffer.*/
616 /* We start counting in the buffer with entry 2 and increment for every
617 entry (do not increment for . or .. entry) */
618 static int find_cifs_entry(const int xid
, struct cifsTconInfo
*pTcon
,
619 struct file
*file
, char **ppCurrentEntry
, int *num_to_ret
)
623 loff_t first_entry_in_buffer
;
624 loff_t index_to_find
= file
->f_pos
;
625 struct cifsFileInfo
* cifsFile
= file
->private_data
;
626 /* check if index in the buffer */
628 if((cifsFile
== NULL
) || (ppCurrentEntry
== NULL
) ||
629 (num_to_ret
== NULL
))
632 *ppCurrentEntry
= NULL
;
633 first_entry_in_buffer
=
634 cifsFile
->srch_inf
.index_of_last_entry
-
635 cifsFile
->srch_inf
.entries_in_buffer
;
637 /* if first entry in buf is zero then is first buffer
638 in search response data which means it is likely . and ..
639 will be in this buffer, although some servers do not return
640 . and .. for the root of a drive and for those we need
641 to start two entries earlier */
643 #ifdef CONFIG_CIFS_DEBUG2
644 dump_cifs_file_struct(file
, "In fce ");
646 if(((index_to_find
< cifsFile
->srch_inf
.index_of_last_entry
) &&
647 is_dir_changed(file
)) ||
648 (index_to_find
< first_entry_in_buffer
)) {
649 /* close and restart search */
650 cFYI(1,("search backing up - close and restart search"));
651 cifsFile
->invalidHandle
= TRUE
;
652 CIFSFindClose(xid
, pTcon
, cifsFile
->netfid
);
653 kfree(cifsFile
->search_resume_name
);
654 cifsFile
->search_resume_name
= NULL
;
655 if(cifsFile
->srch_inf
.ntwrk_buf_start
) {
656 cFYI(1,("freeing SMB ff cache buf on search rewind"));
657 if(cifsFile
->srch_inf
.smallBuf
)
658 cifs_small_buf_release(cifsFile
->srch_inf
.
661 cifs_buf_release(cifsFile
->srch_inf
.
664 rc
= initiate_cifs_search(xid
,file
);
666 cFYI(1,("error %d reinitiating a search on rewind",rc
));
671 while((index_to_find
>= cifsFile
->srch_inf
.index_of_last_entry
) &&
672 (rc
== 0) && (cifsFile
->srch_inf
.endOfSearch
== FALSE
)){
673 cFYI(1,("calling findnext2"));
674 rc
= CIFSFindNext(xid
,pTcon
,cifsFile
->netfid
,
675 &cifsFile
->srch_inf
);
679 if(index_to_find
< cifsFile
->srch_inf
.index_of_last_entry
) {
680 /* we found the buffer that contains the entry */
681 /* scan and find it */
683 char * current_entry
;
684 char * end_of_smb
= cifsFile
->srch_inf
.ntwrk_buf_start
+
685 smbCalcSize((struct smb_hdr
*)
686 cifsFile
->srch_inf
.ntwrk_buf_start
);
688 current_entry
= cifsFile
->srch_inf
.srch_entries_start
;
689 first_entry_in_buffer
= cifsFile
->srch_inf
.index_of_last_entry
690 - cifsFile
->srch_inf
.entries_in_buffer
;
691 pos_in_buf
= index_to_find
- first_entry_in_buffer
;
692 cFYI(1,("found entry - pos_in_buf %d",pos_in_buf
));
694 for(i
=0;(i
<(pos_in_buf
)) && (current_entry
!= NULL
);i
++) {
695 /* go entry by entry figuring out which is first */
696 current_entry
= nxt_dir_entry(current_entry
,end_of_smb
,
697 cifsFile
->srch_inf
.info_level
);
699 if((current_entry
== NULL
) && (i
< pos_in_buf
)) {
700 /* BB fixme - check if we should flag this error */
701 cERROR(1,("reached end of buf searching for pos in buf"
702 " %d index to find %lld rc %d",
703 pos_in_buf
,index_to_find
,rc
));
706 *ppCurrentEntry
= current_entry
;
708 cFYI(1,("index not in buffer - could not findnext into it"));
712 if(pos_in_buf
>= cifsFile
->srch_inf
.entries_in_buffer
) {
713 cFYI(1,("can not return entries pos_in_buf beyond last entry"));
716 *num_to_ret
= cifsFile
->srch_inf
.entries_in_buffer
- pos_in_buf
;
721 /* inode num, inode type and filename returned */
722 static int cifs_get_name_from_search_buf(struct qstr
*pqst
,
723 char *current_entry
, __u16 level
, unsigned int unicode
,
724 struct cifs_sb_info
* cifs_sb
, int max_len
, ino_t
*pinum
)
727 unsigned int len
= 0;
729 struct nls_table
* nlt
= cifs_sb
->local_nls
;
733 if(level
== SMB_FIND_FILE_UNIX
) {
734 FILE_UNIX_INFO
* pFindData
= (FILE_UNIX_INFO
*)current_entry
;
736 filename
= &pFindData
->FileName
[0];
738 len
= cifs_unicode_bytelen(filename
);
740 /* BB should we make this strnlen of PATH_MAX? */
741 len
= strnlen(filename
, PATH_MAX
);
744 /* BB fixme - hash low and high 32 bits if not 64 bit arch BB fixme */
745 if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SERVER_INUM
)
746 *pinum
= pFindData
->UniqueId
;
747 } else if(level
== SMB_FIND_FILE_DIRECTORY_INFO
) {
748 FILE_DIRECTORY_INFO
* pFindData
=
749 (FILE_DIRECTORY_INFO
*)current_entry
;
750 filename
= &pFindData
->FileName
[0];
751 len
= le32_to_cpu(pFindData
->FileNameLength
);
752 } else if(level
== SMB_FIND_FILE_FULL_DIRECTORY_INFO
) {
753 FILE_FULL_DIRECTORY_INFO
* pFindData
=
754 (FILE_FULL_DIRECTORY_INFO
*)current_entry
;
755 filename
= &pFindData
->FileName
[0];
756 len
= le32_to_cpu(pFindData
->FileNameLength
);
757 } else if(level
== SMB_FIND_FILE_ID_FULL_DIR_INFO
) {
758 SEARCH_ID_FULL_DIR_INFO
* pFindData
=
759 (SEARCH_ID_FULL_DIR_INFO
*)current_entry
;
760 filename
= &pFindData
->FileName
[0];
761 len
= le32_to_cpu(pFindData
->FileNameLength
);
762 *pinum
= pFindData
->UniqueId
;
763 } else if(level
== SMB_FIND_FILE_BOTH_DIRECTORY_INFO
) {
764 FILE_BOTH_DIRECTORY_INFO
* pFindData
=
765 (FILE_BOTH_DIRECTORY_INFO
*)current_entry
;
766 filename
= &pFindData
->FileName
[0];
767 len
= le32_to_cpu(pFindData
->FileNameLength
);
768 } else if(level
== SMB_FIND_FILE_INFO_STANDARD
) {
769 FIND_FILE_STANDARD_INFO
* pFindData
=
770 (FIND_FILE_STANDARD_INFO
*)current_entry
;
771 filename
= &pFindData
->FileName
[0];
772 /* one byte length, no name conversion */
773 len
= (unsigned int)pFindData
->FileNameLength
;
775 cFYI(1,("Unknown findfirst level %d",level
));
780 cERROR(1,("bad search response length %d past smb end", len
));
785 /* BB fixme - test with long names */
786 /* Note converted filename can be longer than in unicode */
787 if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MAP_SPECIAL_CHR
)
788 pqst
->len
= cifs_convertUCSpath((char *)pqst
->name
,
789 (__le16
*)filename
, len
/2, nlt
);
791 pqst
->len
= cifs_strfromUCS_le((char *)pqst
->name
,
792 (__le16
*)filename
,len
/2,nlt
);
794 pqst
->name
= filename
;
797 pqst
->hash
= full_name_hash(pqst
->name
,pqst
->len
);
798 /* cFYI(1,("filldir on %s",pqst->name)); */
802 static int cifs_filldir(char *pfindEntry
, struct file
*file
,
803 filldir_t filldir
, void *direntry
, char *scratch_buf
, int max_len
)
807 struct cifsFileInfo
* pCifsF
;
810 struct cifs_sb_info
* cifs_sb
;
811 struct inode
*tmp_inode
;
812 struct dentry
*tmp_dentry
;
814 /* get filename and len into qstring */
816 /* decide whether to create and populate ionde */
817 if((direntry
== NULL
) || (file
== NULL
))
820 pCifsF
= file
->private_data
;
822 if((scratch_buf
== NULL
) || (pfindEntry
== NULL
) || (pCifsF
== NULL
))
825 if(file
->f_dentry
== NULL
)
828 rc
= cifs_entry_is_dot(pfindEntry
,pCifsF
);
829 /* skip . and .. since we added them first */
833 cifs_sb
= CIFS_SB(file
->f_dentry
->d_sb
);
835 qstring
.name
= scratch_buf
;
836 rc
= cifs_get_name_from_search_buf(&qstring
,pfindEntry
,
837 pCifsF
->srch_inf
.info_level
,
838 pCifsF
->srch_inf
.unicode
,cifs_sb
,
840 &inum
/* returned */);
845 rc
= construct_dentry(&qstring
,file
,&tmp_inode
, &tmp_dentry
);
846 if((tmp_inode
== NULL
) || (tmp_dentry
== NULL
))
850 /* inode created, we need to hash it with right inode number */
852 /* BB fixme - hash the 2 32 quantities bits together if necessary BB */
853 tmp_inode
->i_ino
= inum
;
855 insert_inode_hash(tmp_inode
);
858 /* we pass in rc below, indicating whether it is a new inode,
859 so we can figure out whether to invalidate the inode cached
860 data if the file has changed */
861 if(pCifsF
->srch_inf
.info_level
== SMB_FIND_FILE_UNIX
)
862 unix_fill_in_inode(tmp_inode
,
863 (FILE_UNIX_INFO
*)pfindEntry
,
865 else if(pCifsF
->srch_inf
.info_level
== SMB_FIND_FILE_INFO_STANDARD
)
866 fill_in_inode(tmp_inode
, 0 /* old level 1 buffer type */,
867 pfindEntry
, &obj_type
, rc
);
869 fill_in_inode(tmp_inode
, 1 /* NT */, pfindEntry
, &obj_type
, rc
);
871 if(rc
) /* new inode - needs to be tied to dentry */ {
872 d_instantiate(tmp_dentry
, tmp_inode
);
874 d_rehash(tmp_dentry
);
878 rc
= filldir(direntry
,qstring
.name
,qstring
.len
,file
->f_pos
,
879 tmp_inode
->i_ino
,obj_type
);
881 cFYI(1,("filldir rc = %d",rc
));
888 static int cifs_save_resume_key(const char *current_entry
,
889 struct cifsFileInfo
*cifsFile
)
892 unsigned int len
= 0;
896 if((cifsFile
== NULL
) || (current_entry
== NULL
))
899 level
= cifsFile
->srch_inf
.info_level
;
901 if(level
== SMB_FIND_FILE_UNIX
) {
902 FILE_UNIX_INFO
* pFindData
= (FILE_UNIX_INFO
*)current_entry
;
904 filename
= &pFindData
->FileName
[0];
905 if(cifsFile
->srch_inf
.unicode
) {
906 len
= cifs_unicode_bytelen(filename
);
908 /* BB should we make this strnlen of PATH_MAX? */
909 len
= strnlen(filename
, PATH_MAX
);
911 cifsFile
->srch_inf
.resume_key
= pFindData
->ResumeKey
;
912 } else if(level
== SMB_FIND_FILE_DIRECTORY_INFO
) {
913 FILE_DIRECTORY_INFO
* pFindData
=
914 (FILE_DIRECTORY_INFO
*)current_entry
;
915 filename
= &pFindData
->FileName
[0];
916 len
= le32_to_cpu(pFindData
->FileNameLength
);
917 cifsFile
->srch_inf
.resume_key
= pFindData
->FileIndex
;
918 } else if(level
== SMB_FIND_FILE_FULL_DIRECTORY_INFO
) {
919 FILE_FULL_DIRECTORY_INFO
* pFindData
=
920 (FILE_FULL_DIRECTORY_INFO
*)current_entry
;
921 filename
= &pFindData
->FileName
[0];
922 len
= le32_to_cpu(pFindData
->FileNameLength
);
923 cifsFile
->srch_inf
.resume_key
= pFindData
->FileIndex
;
924 } else if(level
== SMB_FIND_FILE_ID_FULL_DIR_INFO
) {
925 SEARCH_ID_FULL_DIR_INFO
* pFindData
=
926 (SEARCH_ID_FULL_DIR_INFO
*)current_entry
;
927 filename
= &pFindData
->FileName
[0];
928 len
= le32_to_cpu(pFindData
->FileNameLength
);
929 cifsFile
->srch_inf
.resume_key
= pFindData
->FileIndex
;
930 } else if(level
== SMB_FIND_FILE_BOTH_DIRECTORY_INFO
) {
931 FILE_BOTH_DIRECTORY_INFO
* pFindData
=
932 (FILE_BOTH_DIRECTORY_INFO
*)current_entry
;
933 filename
= &pFindData
->FileName
[0];
934 len
= le32_to_cpu(pFindData
->FileNameLength
);
935 cifsFile
->srch_inf
.resume_key
= pFindData
->FileIndex
;
936 } else if(level
== SMB_FIND_FILE_INFO_STANDARD
) {
937 FIND_FILE_STANDARD_INFO
* pFindData
=
938 (FIND_FILE_STANDARD_INFO
*)current_entry
;
939 filename
= &pFindData
->FileName
[0];
940 /* one byte length, no name conversion */
941 len
= (unsigned int)pFindData
->FileNameLength
;
943 cFYI(1,("Unknown findfirst level %d",level
));
946 cifsFile
->srch_inf
.resume_name_len
= len
;
947 cifsFile
->srch_inf
.presume_name
= filename
;
951 int cifs_readdir(struct file
*file
, void *direntry
, filldir_t filldir
)
955 struct cifs_sb_info
*cifs_sb
;
956 struct cifsTconInfo
*pTcon
;
957 struct cifsFileInfo
*cifsFile
= NULL
;
958 char * current_entry
;
960 char * tmp_buf
= NULL
;
966 if(file
->f_dentry
== NULL
) {
971 cifs_sb
= CIFS_SB(file
->f_dentry
->d_sb
);
972 pTcon
= cifs_sb
->tcon
;
976 switch ((int) file
->f_pos
) {
978 if (filldir(direntry
, ".", 1, file
->f_pos
,
979 file
->f_dentry
->d_inode
->i_ino
, DT_DIR
) < 0) {
980 cERROR(1, ("Filldir for current dir failed"));
986 if (filldir(direntry
, "..", 2, file
->f_pos
,
987 file
->f_dentry
->d_parent
->d_inode
->i_ino
, DT_DIR
) < 0) {
988 cERROR(1, ("Filldir for parent dir failed"));
994 /* 1) If search is active,
995 is in current search buffer?
996 if it before then restart search
997 if after then keep searching till find it */
999 if(file
->private_data
== NULL
) {
1000 rc
= initiate_cifs_search(xid
,file
);
1001 cFYI(1,("initiate cifs search rc %d",rc
));
1007 if(file
->private_data
== NULL
) {
1012 cifsFile
= file
->private_data
;
1013 if (cifsFile
->srch_inf
.endOfSearch
) {
1014 if(cifsFile
->srch_inf
.emptyDir
) {
1015 cFYI(1, ("End of search, empty dir"));
1020 cifsFile->invalidHandle = TRUE;
1021 CIFSFindClose(xid, pTcon, cifsFile->netfid);
1023 kfree(cifsFile->search_resume_name);
1024 cifsFile->search_resume_name = NULL; */
1026 rc
= find_cifs_entry(xid
,pTcon
, file
,
1027 ¤t_entry
,&num_to_fill
);
1029 cFYI(1,("fce error %d",rc
));
1031 } else if (current_entry
!= NULL
) {
1032 cFYI(1,("entry %lld found",file
->f_pos
));
1034 cFYI(1,("could not find entry"));
1037 cFYI(1,("loop through %d times filling dir for net buf %p",
1038 num_to_fill
,cifsFile
->srch_inf
.ntwrk_buf_start
));
1039 max_len
= smbCalcSize((struct smb_hdr
*)
1040 cifsFile
->srch_inf
.ntwrk_buf_start
);
1041 end_of_smb
= cifsFile
->srch_inf
.ntwrk_buf_start
+ max_len
;
1043 /* To be safe - for UCS to UTF-8 with strings loaded
1044 with the rare long characters alloc more to account for
1045 such multibyte target UTF-8 characters. cifs_unicode.c,
1046 which actually does the conversion, has the same limit */
1047 tmp_buf
= kmalloc((2 * NAME_MAX
) + 4, GFP_KERNEL
);
1048 for(i
=0;(i
<num_to_fill
) && (rc
== 0);i
++) {
1049 if(current_entry
== NULL
) {
1050 /* evaluate whether this case is an error */
1051 cERROR(1,("past end of SMB num to fill %d i %d",
1055 /* if buggy server returns . and .. late do
1056 we want to check for that here? */
1057 rc
= cifs_filldir(current_entry
, file
,
1058 filldir
, direntry
, tmp_buf
, max_len
);
1061 cifsFile
->srch_inf
.index_of_last_entry
) {
1062 cFYI(1,("last entry in buf at pos %lld %s",
1063 file
->f_pos
,tmp_buf
));
1064 cifs_save_resume_key(current_entry
,cifsFile
);
1068 nxt_dir_entry(current_entry
, end_of_smb
,
1069 cifsFile
->srch_inf
.info_level
);