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",
220 (unsigned long)tmp_inode
->i_size
,
221 (unsigned long long)tmp_inode
->i_blocks
));
222 if (S_ISREG(tmp_inode
->i_mode
)) {
223 cFYI(1, ("File inode"));
224 tmp_inode
->i_op
= &cifs_file_inode_ops
;
225 if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_DIRECT_IO
) {
226 if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_BRL
)
227 tmp_inode
->i_fop
= &cifs_file_direct_nobrl_ops
;
229 tmp_inode
->i_fop
= &cifs_file_direct_ops
;
231 } else if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_BRL
)
232 tmp_inode
->i_fop
= &cifs_file_nobrl_ops
;
234 tmp_inode
->i_fop
= &cifs_file_ops
;
236 if((cifs_sb
->tcon
) && (cifs_sb
->tcon
->ses
) &&
237 (cifs_sb
->tcon
->ses
->server
->maxBuf
<
238 PAGE_CACHE_SIZE
+ MAX_CIFS_HDR_SIZE
))
239 tmp_inode
->i_data
.a_ops
= &cifs_addr_ops_smallbuf
;
241 tmp_inode
->i_data
.a_ops
= &cifs_addr_ops
;
244 return; /* No sense invalidating pages for new inode
245 since have not started caching readahead file
248 if (timespec_equal(&tmp_inode
->i_mtime
, &local_mtime
) &&
249 (local_size
== tmp_inode
->i_size
)) {
250 cFYI(1, ("inode exists but unchanged"));
252 /* file may have changed on server */
253 cFYI(1, ("invalidate inode, readdir detected change"));
254 invalidate_remote_inode(tmp_inode
);
256 } else if (S_ISDIR(tmp_inode
->i_mode
)) {
257 cFYI(1, ("Directory inode"));
258 tmp_inode
->i_op
= &cifs_dir_inode_ops
;
259 tmp_inode
->i_fop
= &cifs_dir_ops
;
260 } else if (S_ISLNK(tmp_inode
->i_mode
)) {
261 cFYI(1, ("Symbolic Link inode"));
262 tmp_inode
->i_op
= &cifs_symlink_inode_ops
;
264 cFYI(1, ("Init special inode"));
265 init_special_inode(tmp_inode
, tmp_inode
->i_mode
,
270 static void unix_fill_in_inode(struct inode
*tmp_inode
,
271 FILE_UNIX_INFO
*pfindData
, int *pobject_type
, int isNewInode
)
274 struct timespec local_mtime
;
276 struct cifsInodeInfo
*cifsInfo
= CIFS_I(tmp_inode
);
277 struct cifs_sb_info
*cifs_sb
= CIFS_SB(tmp_inode
->i_sb
);
279 __u32 type
= le32_to_cpu(pfindData
->Type
);
280 __u64 num_of_bytes
= le64_to_cpu(pfindData
->NumOfBytes
);
281 __u64 end_of_file
= le64_to_cpu(pfindData
->EndOfFile
);
282 cifsInfo
->time
= jiffies
;
283 atomic_inc(&cifsInfo
->inUse
);
285 /* save mtime and size */
286 local_mtime
= tmp_inode
->i_mtime
;
287 local_size
= tmp_inode
->i_size
;
290 cifs_NTtimeToUnix(le64_to_cpu(pfindData
->LastAccessTime
));
292 cifs_NTtimeToUnix(le64_to_cpu(pfindData
->LastModificationTime
));
294 cifs_NTtimeToUnix(le64_to_cpu(pfindData
->LastStatusChange
));
296 tmp_inode
->i_mode
= le64_to_cpu(pfindData
->Permissions
);
297 /* since we set the inode type below we need to mask off type
298 to avoid strange results if bits above were corrupt */
299 tmp_inode
->i_mode
&= ~S_IFMT
;
300 if (type
== UNIX_FILE
) {
301 *pobject_type
= DT_REG
;
302 tmp_inode
->i_mode
|= S_IFREG
;
303 } else if (type
== UNIX_SYMLINK
) {
304 *pobject_type
= DT_LNK
;
305 tmp_inode
->i_mode
|= S_IFLNK
;
306 } else if (type
== UNIX_DIR
) {
307 *pobject_type
= DT_DIR
;
308 tmp_inode
->i_mode
|= S_IFDIR
;
309 } else if (type
== UNIX_CHARDEV
) {
310 *pobject_type
= DT_CHR
;
311 tmp_inode
->i_mode
|= S_IFCHR
;
312 tmp_inode
->i_rdev
= MKDEV(le64_to_cpu(pfindData
->DevMajor
),
313 le64_to_cpu(pfindData
->DevMinor
) & MINORMASK
);
314 } else if (type
== UNIX_BLOCKDEV
) {
315 *pobject_type
= DT_BLK
;
316 tmp_inode
->i_mode
|= S_IFBLK
;
317 tmp_inode
->i_rdev
= MKDEV(le64_to_cpu(pfindData
->DevMajor
),
318 le64_to_cpu(pfindData
->DevMinor
) & MINORMASK
);
319 } else if (type
== UNIX_FIFO
) {
320 *pobject_type
= DT_FIFO
;
321 tmp_inode
->i_mode
|= S_IFIFO
;
322 } else if (type
== UNIX_SOCKET
) {
323 *pobject_type
= DT_SOCK
;
324 tmp_inode
->i_mode
|= S_IFSOCK
;
326 /* safest to just call it a file */
327 *pobject_type
= DT_REG
;
328 tmp_inode
->i_mode
|= S_IFREG
;
329 cFYI(1,("unknown inode type %d",type
));
332 tmp_inode
->i_uid
= le64_to_cpu(pfindData
->Uid
);
333 tmp_inode
->i_gid
= le64_to_cpu(pfindData
->Gid
);
334 tmp_inode
->i_nlink
= le64_to_cpu(pfindData
->Nlinks
);
336 if (is_size_safe_to_change(cifsInfo
)) {
337 /* can not safely change the file size here if the
338 client is writing to it due to potential races */
339 i_size_write(tmp_inode
,end_of_file
);
341 /* 512 bytes (2**9) is the fake blocksize that must be used */
342 /* for this calculation, not the real blocksize */
343 tmp_inode
->i_blocks
= (512 - 1 + num_of_bytes
) >> 9;
346 if (S_ISREG(tmp_inode
->i_mode
)) {
347 cFYI(1, ("File inode"));
348 tmp_inode
->i_op
= &cifs_file_inode_ops
;
350 if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_DIRECT_IO
) {
351 if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_BRL
)
352 tmp_inode
->i_fop
= &cifs_file_direct_nobrl_ops
;
354 tmp_inode
->i_fop
= &cifs_file_direct_ops
;
356 } else if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_BRL
)
357 tmp_inode
->i_fop
= &cifs_file_nobrl_ops
;
359 tmp_inode
->i_fop
= &cifs_file_ops
;
361 if((cifs_sb
->tcon
) && (cifs_sb
->tcon
->ses
) &&
362 (cifs_sb
->tcon
->ses
->server
->maxBuf
<
363 PAGE_CACHE_SIZE
+ MAX_CIFS_HDR_SIZE
))
364 tmp_inode
->i_data
.a_ops
= &cifs_addr_ops_smallbuf
;
366 tmp_inode
->i_data
.a_ops
= &cifs_addr_ops
;
369 return; /* No sense invalidating pages for new inode since we
370 have not started caching readahead file data yet */
372 if (timespec_equal(&tmp_inode
->i_mtime
, &local_mtime
) &&
373 (local_size
== tmp_inode
->i_size
)) {
374 cFYI(1, ("inode exists but unchanged"));
376 /* file may have changed on server */
377 cFYI(1, ("invalidate inode, readdir detected change"));
378 invalidate_remote_inode(tmp_inode
);
380 } else if (S_ISDIR(tmp_inode
->i_mode
)) {
381 cFYI(1, ("Directory inode"));
382 tmp_inode
->i_op
= &cifs_dir_inode_ops
;
383 tmp_inode
->i_fop
= &cifs_dir_ops
;
384 } else if (S_ISLNK(tmp_inode
->i_mode
)) {
385 cFYI(1, ("Symbolic Link inode"));
386 tmp_inode
->i_op
= &cifs_symlink_inode_ops
;
387 /* tmp_inode->i_fop = *//* do not need to set to anything */
389 cFYI(1, ("Special inode"));
390 init_special_inode(tmp_inode
, tmp_inode
->i_mode
,
395 static int initiate_cifs_search(const int xid
, struct file
*file
)
399 struct cifsFileInfo
* cifsFile
;
400 struct cifs_sb_info
*cifs_sb
;
401 struct cifsTconInfo
*pTcon
;
403 if(file
->private_data
== NULL
) {
405 kmalloc(sizeof(struct cifsFileInfo
),GFP_KERNEL
);
408 if(file
->private_data
== NULL
) {
411 memset(file
->private_data
,0,sizeof(struct cifsFileInfo
));
413 cifsFile
= file
->private_data
;
414 cifsFile
->invalidHandle
= TRUE
;
415 cifsFile
->srch_inf
.endOfSearch
= FALSE
;
417 if(file
->f_dentry
== NULL
)
420 cifs_sb
= CIFS_SB(file
->f_dentry
->d_sb
);
424 pTcon
= cifs_sb
->tcon
;
428 full_path
= build_path_from_dentry(file
->f_dentry
);
430 if(full_path
== NULL
) {
434 cFYI(1, ("Full path: %s start at: %lld", full_path
, file
->f_pos
));
437 /* test for Unix extensions */
438 if (pTcon
->ses
->capabilities
& CAP_UNIX
) {
439 cifsFile
->srch_inf
.info_level
= SMB_FIND_FILE_UNIX
;
440 } else if ((pTcon
->ses
->capabilities
&
441 (CAP_NT_SMBS
| CAP_NT_FIND
)) == 0) {
442 cifsFile
->srch_inf
.info_level
= SMB_FIND_FILE_INFO_STANDARD
;
443 } else if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SERVER_INUM
) {
444 cifsFile
->srch_inf
.info_level
= SMB_FIND_FILE_ID_FULL_DIR_INFO
;
445 } else /* not srvinos - BB fixme add check for backlevel? */ {
446 cifsFile
->srch_inf
.info_level
= SMB_FIND_FILE_DIRECTORY_INFO
;
449 rc
= CIFSFindFirst(xid
, pTcon
,full_path
,cifs_sb
->local_nls
,
450 &cifsFile
->netfid
, &cifsFile
->srch_inf
,
451 cifs_sb
->mnt_cifs_flags
&
452 CIFS_MOUNT_MAP_SPECIAL_CHR
, CIFS_DIR_SEP(cifs_sb
));
454 cifsFile
->invalidHandle
= FALSE
;
455 if((rc
== -EOPNOTSUPP
) &&
456 (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SERVER_INUM
)) {
457 cifs_sb
->mnt_cifs_flags
&= ~CIFS_MOUNT_SERVER_INUM
;
464 /* return length of unicode string in bytes */
465 static int cifs_unicode_bytelen(char *str
)
468 __le16
* ustr
= (__le16
*)str
;
470 for(len
=0;len
<= PATH_MAX
;len
++) {
474 cFYI(1,("Unicode string longer than PATH_MAX found"));
478 static char *nxt_dir_entry(char *old_entry
, char *end_of_smb
, int level
)
481 FILE_DIRECTORY_INFO
* pDirInfo
= (FILE_DIRECTORY_INFO
*)old_entry
;
483 if(level
== SMB_FIND_FILE_INFO_STANDARD
) {
484 FIND_FILE_STANDARD_INFO
* pfData
;
485 pfData
= (FIND_FILE_STANDARD_INFO
*)pDirInfo
;
487 new_entry
= old_entry
+ sizeof(FIND_FILE_STANDARD_INFO
) +
488 pfData
->FileNameLength
;
490 new_entry
= old_entry
+ le32_to_cpu(pDirInfo
->NextEntryOffset
);
491 cFYI(1,("new entry %p old entry %p",new_entry
,old_entry
));
492 /* validate that new_entry is not past end of SMB */
493 if(new_entry
>= end_of_smb
) {
495 ("search entry %p began after end of SMB %p old entry %p",
496 new_entry
, end_of_smb
, old_entry
));
498 } else if(((level
== SMB_FIND_FILE_INFO_STANDARD
) &&
499 (new_entry
+ sizeof(FIND_FILE_STANDARD_INFO
) > end_of_smb
)) ||
500 ((level
!= SMB_FIND_FILE_INFO_STANDARD
) &&
501 (new_entry
+ sizeof(FILE_DIRECTORY_INFO
) > end_of_smb
))) {
502 cERROR(1,("search entry %p extends after end of SMB %p",
503 new_entry
, end_of_smb
));
510 #define UNICODE_DOT cpu_to_le16(0x2e)
512 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
513 static int cifs_entry_is_dot(char *current_entry
, struct cifsFileInfo
*cfile
)
516 char * filename
= NULL
;
519 if(cfile
->srch_inf
.info_level
== SMB_FIND_FILE_UNIX
) {
520 FILE_UNIX_INFO
* pFindData
= (FILE_UNIX_INFO
*)current_entry
;
521 filename
= &pFindData
->FileName
[0];
522 if(cfile
->srch_inf
.unicode
) {
523 len
= cifs_unicode_bytelen(filename
);
525 /* BB should we make this strnlen of PATH_MAX? */
526 len
= strnlen(filename
, 5);
528 } else if(cfile
->srch_inf
.info_level
== SMB_FIND_FILE_DIRECTORY_INFO
) {
529 FILE_DIRECTORY_INFO
* pFindData
=
530 (FILE_DIRECTORY_INFO
*)current_entry
;
531 filename
= &pFindData
->FileName
[0];
532 len
= le32_to_cpu(pFindData
->FileNameLength
);
533 } else if(cfile
->srch_inf
.info_level
==
534 SMB_FIND_FILE_FULL_DIRECTORY_INFO
) {
535 FILE_FULL_DIRECTORY_INFO
* pFindData
=
536 (FILE_FULL_DIRECTORY_INFO
*)current_entry
;
537 filename
= &pFindData
->FileName
[0];
538 len
= le32_to_cpu(pFindData
->FileNameLength
);
539 } else if(cfile
->srch_inf
.info_level
==
540 SMB_FIND_FILE_ID_FULL_DIR_INFO
) {
541 SEARCH_ID_FULL_DIR_INFO
* pFindData
=
542 (SEARCH_ID_FULL_DIR_INFO
*)current_entry
;
543 filename
= &pFindData
->FileName
[0];
544 len
= le32_to_cpu(pFindData
->FileNameLength
);
545 } else if(cfile
->srch_inf
.info_level
==
546 SMB_FIND_FILE_BOTH_DIRECTORY_INFO
) {
547 FILE_BOTH_DIRECTORY_INFO
* pFindData
=
548 (FILE_BOTH_DIRECTORY_INFO
*)current_entry
;
549 filename
= &pFindData
->FileName
[0];
550 len
= le32_to_cpu(pFindData
->FileNameLength
);
551 } else if(cfile
->srch_inf
.info_level
== SMB_FIND_FILE_INFO_STANDARD
) {
552 FIND_FILE_STANDARD_INFO
* pFindData
=
553 (FIND_FILE_STANDARD_INFO
*)current_entry
;
554 filename
= &pFindData
->FileName
[0];
555 len
= pFindData
->FileNameLength
;
557 cFYI(1,("Unknown findfirst level %d",cfile
->srch_inf
.info_level
));
561 if(cfile
->srch_inf
.unicode
) {
562 __le16
*ufilename
= (__le16
*)filename
;
565 if(ufilename
[0] == UNICODE_DOT
)
567 } else if(len
== 4) {
569 if((ufilename
[0] == UNICODE_DOT
)
570 &&(ufilename
[1] == UNICODE_DOT
))
575 if(filename
[0] == '.')
577 } else if(len
== 2) {
578 if((filename
[0] == '.') && (filename
[1] == '.'))
587 /* Check if directory that we are searching has changed so we can decide
588 whether we can use the cached search results from the previous search */
589 static int is_dir_changed(struct file
* file
)
591 struct inode
* inode
;
592 struct cifsInodeInfo
*cifsInfo
;
594 if(file
->f_dentry
== NULL
)
597 inode
= file
->f_dentry
->d_inode
;
602 cifsInfo
= CIFS_I(inode
);
604 if(cifsInfo
->time
== 0)
605 return 1; /* directory was changed, perhaps due to unlink */
611 /* find the corresponding entry in the search */
612 /* Note that the SMB server returns search entries for . and .. which
613 complicates logic here if we choose to parse for them and we do not
614 assume that they are located in the findfirst return buffer.*/
615 /* We start counting in the buffer with entry 2 and increment for every
616 entry (do not increment for . or .. entry) */
617 static int find_cifs_entry(const int xid
, struct cifsTconInfo
*pTcon
,
618 struct file
*file
, char **ppCurrentEntry
, int *num_to_ret
)
622 loff_t first_entry_in_buffer
;
623 loff_t index_to_find
= file
->f_pos
;
624 struct cifsFileInfo
* cifsFile
= file
->private_data
;
625 /* check if index in the buffer */
627 if((cifsFile
== NULL
) || (ppCurrentEntry
== NULL
) ||
628 (num_to_ret
== NULL
))
631 *ppCurrentEntry
= NULL
;
632 first_entry_in_buffer
=
633 cifsFile
->srch_inf
.index_of_last_entry
-
634 cifsFile
->srch_inf
.entries_in_buffer
;
636 /* if first entry in buf is zero then is first buffer
637 in search response data which means it is likely . and ..
638 will be in this buffer, although some servers do not return
639 . and .. for the root of a drive and for those we need
640 to start two entries earlier */
642 #ifdef CONFIG_CIFS_DEBUG2
643 dump_cifs_file_struct(file
, "In fce ");
645 if(((index_to_find
< cifsFile
->srch_inf
.index_of_last_entry
) &&
646 is_dir_changed(file
)) ||
647 (index_to_find
< first_entry_in_buffer
)) {
648 /* close and restart search */
649 cFYI(1,("search backing up - close and restart search"));
650 cifsFile
->invalidHandle
= TRUE
;
651 CIFSFindClose(xid
, pTcon
, cifsFile
->netfid
);
652 kfree(cifsFile
->search_resume_name
);
653 cifsFile
->search_resume_name
= NULL
;
654 if(cifsFile
->srch_inf
.ntwrk_buf_start
) {
655 cFYI(1,("freeing SMB ff cache buf on search rewind"));
656 if(cifsFile
->srch_inf
.smallBuf
)
657 cifs_small_buf_release(cifsFile
->srch_inf
.
660 cifs_buf_release(cifsFile
->srch_inf
.
663 rc
= initiate_cifs_search(xid
,file
);
665 cFYI(1,("error %d reinitiating a search on rewind",rc
));
670 while((index_to_find
>= cifsFile
->srch_inf
.index_of_last_entry
) &&
671 (rc
== 0) && (cifsFile
->srch_inf
.endOfSearch
== FALSE
)){
672 cFYI(1,("calling findnext2"));
673 rc
= CIFSFindNext(xid
,pTcon
,cifsFile
->netfid
,
674 &cifsFile
->srch_inf
);
678 if(index_to_find
< cifsFile
->srch_inf
.index_of_last_entry
) {
679 /* we found the buffer that contains the entry */
680 /* scan and find it */
682 char * current_entry
;
683 char * end_of_smb
= cifsFile
->srch_inf
.ntwrk_buf_start
+
684 smbCalcSize((struct smb_hdr
*)
685 cifsFile
->srch_inf
.ntwrk_buf_start
);
687 current_entry
= cifsFile
->srch_inf
.srch_entries_start
;
688 first_entry_in_buffer
= cifsFile
->srch_inf
.index_of_last_entry
689 - cifsFile
->srch_inf
.entries_in_buffer
;
690 pos_in_buf
= index_to_find
- first_entry_in_buffer
;
691 cFYI(1,("found entry - pos_in_buf %d",pos_in_buf
));
693 for(i
=0;(i
<(pos_in_buf
)) && (current_entry
!= NULL
);i
++) {
694 /* go entry by entry figuring out which is first */
695 current_entry
= nxt_dir_entry(current_entry
,end_of_smb
,
696 cifsFile
->srch_inf
.info_level
);
698 if((current_entry
== NULL
) && (i
< pos_in_buf
)) {
699 /* BB fixme - check if we should flag this error */
700 cERROR(1,("reached end of buf searching for pos in buf"
701 " %d index to find %lld rc %d",
702 pos_in_buf
,index_to_find
,rc
));
705 *ppCurrentEntry
= current_entry
;
707 cFYI(1,("index not in buffer - could not findnext into it"));
711 if(pos_in_buf
>= cifsFile
->srch_inf
.entries_in_buffer
) {
712 cFYI(1,("can not return entries pos_in_buf beyond last entry"));
715 *num_to_ret
= cifsFile
->srch_inf
.entries_in_buffer
- pos_in_buf
;
720 /* inode num, inode type and filename returned */
721 static int cifs_get_name_from_search_buf(struct qstr
*pqst
,
722 char *current_entry
, __u16 level
, unsigned int unicode
,
723 struct cifs_sb_info
* cifs_sb
, int max_len
, ino_t
*pinum
)
726 unsigned int len
= 0;
728 struct nls_table
* nlt
= cifs_sb
->local_nls
;
732 if(level
== SMB_FIND_FILE_UNIX
) {
733 FILE_UNIX_INFO
* pFindData
= (FILE_UNIX_INFO
*)current_entry
;
735 filename
= &pFindData
->FileName
[0];
737 len
= cifs_unicode_bytelen(filename
);
739 /* BB should we make this strnlen of PATH_MAX? */
740 len
= strnlen(filename
, PATH_MAX
);
743 /* BB fixme - hash low and high 32 bits if not 64 bit arch BB fixme */
744 if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SERVER_INUM
)
745 *pinum
= pFindData
->UniqueId
;
746 } else if(level
== SMB_FIND_FILE_DIRECTORY_INFO
) {
747 FILE_DIRECTORY_INFO
* pFindData
=
748 (FILE_DIRECTORY_INFO
*)current_entry
;
749 filename
= &pFindData
->FileName
[0];
750 len
= le32_to_cpu(pFindData
->FileNameLength
);
751 } else if(level
== SMB_FIND_FILE_FULL_DIRECTORY_INFO
) {
752 FILE_FULL_DIRECTORY_INFO
* pFindData
=
753 (FILE_FULL_DIRECTORY_INFO
*)current_entry
;
754 filename
= &pFindData
->FileName
[0];
755 len
= le32_to_cpu(pFindData
->FileNameLength
);
756 } else if(level
== SMB_FIND_FILE_ID_FULL_DIR_INFO
) {
757 SEARCH_ID_FULL_DIR_INFO
* pFindData
=
758 (SEARCH_ID_FULL_DIR_INFO
*)current_entry
;
759 filename
= &pFindData
->FileName
[0];
760 len
= le32_to_cpu(pFindData
->FileNameLength
);
761 *pinum
= pFindData
->UniqueId
;
762 } else if(level
== SMB_FIND_FILE_BOTH_DIRECTORY_INFO
) {
763 FILE_BOTH_DIRECTORY_INFO
* pFindData
=
764 (FILE_BOTH_DIRECTORY_INFO
*)current_entry
;
765 filename
= &pFindData
->FileName
[0];
766 len
= le32_to_cpu(pFindData
->FileNameLength
);
767 } else if(level
== SMB_FIND_FILE_INFO_STANDARD
) {
768 FIND_FILE_STANDARD_INFO
* pFindData
=
769 (FIND_FILE_STANDARD_INFO
*)current_entry
;
770 filename
= &pFindData
->FileName
[0];
771 /* one byte length, no name conversion */
772 len
= (unsigned int)pFindData
->FileNameLength
;
774 cFYI(1,("Unknown findfirst level %d",level
));
779 cERROR(1,("bad search response length %d past smb end", len
));
784 /* BB fixme - test with long names */
785 /* Note converted filename can be longer than in unicode */
786 if(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MAP_SPECIAL_CHR
)
787 pqst
->len
= cifs_convertUCSpath((char *)pqst
->name
,
788 (__le16
*)filename
, len
/2, nlt
);
790 pqst
->len
= cifs_strfromUCS_le((char *)pqst
->name
,
791 (__le16
*)filename
,len
/2,nlt
);
793 pqst
->name
= filename
;
796 pqst
->hash
= full_name_hash(pqst
->name
,pqst
->len
);
797 /* cFYI(1,("filldir on %s",pqst->name)); */
801 static int cifs_filldir(char *pfindEntry
, struct file
*file
,
802 filldir_t filldir
, void *direntry
, char *scratch_buf
, int max_len
)
806 struct cifsFileInfo
* pCifsF
;
809 struct cifs_sb_info
* cifs_sb
;
810 struct inode
*tmp_inode
;
811 struct dentry
*tmp_dentry
;
813 /* get filename and len into qstring */
815 /* decide whether to create and populate ionde */
816 if((direntry
== NULL
) || (file
== NULL
))
819 pCifsF
= file
->private_data
;
821 if((scratch_buf
== NULL
) || (pfindEntry
== NULL
) || (pCifsF
== NULL
))
824 if(file
->f_dentry
== NULL
)
827 rc
= cifs_entry_is_dot(pfindEntry
,pCifsF
);
828 /* skip . and .. since we added them first */
832 cifs_sb
= CIFS_SB(file
->f_dentry
->d_sb
);
834 qstring
.name
= scratch_buf
;
835 rc
= cifs_get_name_from_search_buf(&qstring
,pfindEntry
,
836 pCifsF
->srch_inf
.info_level
,
837 pCifsF
->srch_inf
.unicode
,cifs_sb
,
839 &inum
/* returned */);
844 rc
= construct_dentry(&qstring
,file
,&tmp_inode
, &tmp_dentry
);
845 if((tmp_inode
== NULL
) || (tmp_dentry
== NULL
))
849 /* inode created, we need to hash it with right inode number */
851 /* BB fixme - hash the 2 32 quantities bits together if necessary BB */
852 tmp_inode
->i_ino
= inum
;
854 insert_inode_hash(tmp_inode
);
857 /* we pass in rc below, indicating whether it is a new inode,
858 so we can figure out whether to invalidate the inode cached
859 data if the file has changed */
860 if(pCifsF
->srch_inf
.info_level
== SMB_FIND_FILE_UNIX
)
861 unix_fill_in_inode(tmp_inode
,
862 (FILE_UNIX_INFO
*)pfindEntry
,
864 else if(pCifsF
->srch_inf
.info_level
== SMB_FIND_FILE_INFO_STANDARD
)
865 fill_in_inode(tmp_inode
, 0 /* old level 1 buffer type */,
866 pfindEntry
, &obj_type
, rc
);
868 fill_in_inode(tmp_inode
, 1 /* NT */, pfindEntry
, &obj_type
, rc
);
870 if(rc
) /* new inode - needs to be tied to dentry */ {
871 d_instantiate(tmp_dentry
, tmp_inode
);
873 d_rehash(tmp_dentry
);
877 rc
= filldir(direntry
,qstring
.name
,qstring
.len
,file
->f_pos
,
878 tmp_inode
->i_ino
,obj_type
);
880 cFYI(1,("filldir rc = %d",rc
));
887 static int cifs_save_resume_key(const char *current_entry
,
888 struct cifsFileInfo
*cifsFile
)
891 unsigned int len
= 0;
895 if((cifsFile
== NULL
) || (current_entry
== NULL
))
898 level
= cifsFile
->srch_inf
.info_level
;
900 if(level
== SMB_FIND_FILE_UNIX
) {
901 FILE_UNIX_INFO
* pFindData
= (FILE_UNIX_INFO
*)current_entry
;
903 filename
= &pFindData
->FileName
[0];
904 if(cifsFile
->srch_inf
.unicode
) {
905 len
= cifs_unicode_bytelen(filename
);
907 /* BB should we make this strnlen of PATH_MAX? */
908 len
= strnlen(filename
, PATH_MAX
);
910 cifsFile
->srch_inf
.resume_key
= pFindData
->ResumeKey
;
911 } else if(level
== SMB_FIND_FILE_DIRECTORY_INFO
) {
912 FILE_DIRECTORY_INFO
* pFindData
=
913 (FILE_DIRECTORY_INFO
*)current_entry
;
914 filename
= &pFindData
->FileName
[0];
915 len
= le32_to_cpu(pFindData
->FileNameLength
);
916 cifsFile
->srch_inf
.resume_key
= pFindData
->FileIndex
;
917 } else if(level
== SMB_FIND_FILE_FULL_DIRECTORY_INFO
) {
918 FILE_FULL_DIRECTORY_INFO
* pFindData
=
919 (FILE_FULL_DIRECTORY_INFO
*)current_entry
;
920 filename
= &pFindData
->FileName
[0];
921 len
= le32_to_cpu(pFindData
->FileNameLength
);
922 cifsFile
->srch_inf
.resume_key
= pFindData
->FileIndex
;
923 } else if(level
== SMB_FIND_FILE_ID_FULL_DIR_INFO
) {
924 SEARCH_ID_FULL_DIR_INFO
* pFindData
=
925 (SEARCH_ID_FULL_DIR_INFO
*)current_entry
;
926 filename
= &pFindData
->FileName
[0];
927 len
= le32_to_cpu(pFindData
->FileNameLength
);
928 cifsFile
->srch_inf
.resume_key
= pFindData
->FileIndex
;
929 } else if(level
== SMB_FIND_FILE_BOTH_DIRECTORY_INFO
) {
930 FILE_BOTH_DIRECTORY_INFO
* pFindData
=
931 (FILE_BOTH_DIRECTORY_INFO
*)current_entry
;
932 filename
= &pFindData
->FileName
[0];
933 len
= le32_to_cpu(pFindData
->FileNameLength
);
934 cifsFile
->srch_inf
.resume_key
= pFindData
->FileIndex
;
935 } else if(level
== SMB_FIND_FILE_INFO_STANDARD
) {
936 FIND_FILE_STANDARD_INFO
* pFindData
=
937 (FIND_FILE_STANDARD_INFO
*)current_entry
;
938 filename
= &pFindData
->FileName
[0];
939 /* one byte length, no name conversion */
940 len
= (unsigned int)pFindData
->FileNameLength
;
942 cFYI(1,("Unknown findfirst level %d",level
));
945 cifsFile
->srch_inf
.resume_name_len
= len
;
946 cifsFile
->srch_inf
.presume_name
= filename
;
950 int cifs_readdir(struct file
*file
, void *direntry
, filldir_t filldir
)
954 struct cifs_sb_info
*cifs_sb
;
955 struct cifsTconInfo
*pTcon
;
956 struct cifsFileInfo
*cifsFile
= NULL
;
957 char * current_entry
;
959 char * tmp_buf
= NULL
;
965 if(file
->f_dentry
== NULL
) {
970 cifs_sb
= CIFS_SB(file
->f_dentry
->d_sb
);
971 pTcon
= cifs_sb
->tcon
;
975 switch ((int) file
->f_pos
) {
977 if (filldir(direntry
, ".", 1, file
->f_pos
,
978 file
->f_dentry
->d_inode
->i_ino
, DT_DIR
) < 0) {
979 cERROR(1, ("Filldir for current dir failed"));
985 if (filldir(direntry
, "..", 2, file
->f_pos
,
986 file
->f_dentry
->d_parent
->d_inode
->i_ino
, DT_DIR
) < 0) {
987 cERROR(1, ("Filldir for parent dir failed"));
993 /* 1) If search is active,
994 is in current search buffer?
995 if it before then restart search
996 if after then keep searching till find it */
998 if(file
->private_data
== NULL
) {
999 rc
= initiate_cifs_search(xid
,file
);
1000 cFYI(1,("initiate cifs search rc %d",rc
));
1006 if(file
->private_data
== NULL
) {
1011 cifsFile
= file
->private_data
;
1012 if (cifsFile
->srch_inf
.endOfSearch
) {
1013 if(cifsFile
->srch_inf
.emptyDir
) {
1014 cFYI(1, ("End of search, empty dir"));
1019 cifsFile->invalidHandle = TRUE;
1020 CIFSFindClose(xid, pTcon, cifsFile->netfid);
1022 kfree(cifsFile->search_resume_name);
1023 cifsFile->search_resume_name = NULL; */
1025 rc
= find_cifs_entry(xid
,pTcon
, file
,
1026 ¤t_entry
,&num_to_fill
);
1028 cFYI(1,("fce error %d",rc
));
1030 } else if (current_entry
!= NULL
) {
1031 cFYI(1,("entry %lld found",file
->f_pos
));
1033 cFYI(1,("could not find entry"));
1036 cFYI(1,("loop through %d times filling dir for net buf %p",
1037 num_to_fill
,cifsFile
->srch_inf
.ntwrk_buf_start
));
1038 max_len
= smbCalcSize((struct smb_hdr
*)
1039 cifsFile
->srch_inf
.ntwrk_buf_start
);
1040 end_of_smb
= cifsFile
->srch_inf
.ntwrk_buf_start
+ max_len
;
1042 /* To be safe - for UCS to UTF-8 with strings loaded
1043 with the rare long characters alloc more to account for
1044 such multibyte target UTF-8 characters. cifs_unicode.c,
1045 which actually does the conversion, has the same limit */
1046 tmp_buf
= kmalloc((2 * NAME_MAX
) + 4, GFP_KERNEL
);
1047 for(i
=0;(i
<num_to_fill
) && (rc
== 0);i
++) {
1048 if(current_entry
== NULL
) {
1049 /* evaluate whether this case is an error */
1050 cERROR(1,("past end of SMB num to fill %d i %d",
1054 /* if buggy server returns . and .. late do
1055 we want to check for that here? */
1056 rc
= cifs_filldir(current_entry
, file
,
1057 filldir
, direntry
, tmp_buf
, max_len
);
1060 cifsFile
->srch_inf
.index_of_last_entry
) {
1061 cFYI(1,("last entry in buf at pos %lld %s",
1062 file
->f_pos
,tmp_buf
));
1063 cifs_save_resume_key(current_entry
,cifsFile
);
1067 nxt_dir_entry(current_entry
, end_of_smb
,
1068 cifsFile
->srch_inf
.info_level
);