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/stat.h>
25 #include <linux/smp_lock.h>
28 #include "cifsproto.h"
29 #include "cifs_unicode.h"
30 #include "cifs_debug.h"
31 #include "cifs_fs_sb.h"
34 /* BB fixme - add debug wrappers around this function to disable it fixme BB */
35 /* static void dump_cifs_file_struct(struct file *file, char *label)
37 struct cifsFileInfo * cf;
40 cf = file->private_data;
42 cFYI(1,("empty cifs private file data"));
45 if(cf->invalidHandle) {
46 cFYI(1,("invalid handle"));
48 if(cf->srch_inf.endOfSearch) {
49 cFYI(1,("end of search"));
51 if(cf->srch_inf.emptyDir) {
52 cFYI(1,("empty dir"));
58 /* Returns one if new inode created (which therefore needs to be hashed) */
59 /* Might check in the future if inode number changed so we can rehash inode */
60 static int construct_dentry(struct qstr *qstring, struct file *file,
61 struct inode **ptmp_inode, struct dentry **pnew_dentry)
63 struct dentry *tmp_dentry;
64 struct cifs_sb_info *cifs_sb;
65 struct cifsTconInfo *pTcon;
68 cFYI(1, ("For %s", qstring->name));
69 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
70 pTcon = cifs_sb->tcon;
72 qstring->hash = full_name_hash(qstring->name, qstring->len);
73 tmp_dentry = d_lookup(file->f_dentry, qstring);
75 cFYI(0, ("existing dentry with inode 0x%p", tmp_dentry->d_inode));
76 *ptmp_inode = tmp_dentry->d_inode;
77 /* BB overwrite old name? i.e. tmp_dentry->d_name and tmp_dentry->d_name.len??*/
78 if(*ptmp_inode == NULL) {
79 *ptmp_inode = new_inode(file->f_dentry->d_sb);
80 if(*ptmp_inode == NULL)
83 d_instantiate(tmp_dentry, *ptmp_inode);
86 tmp_dentry = d_alloc(file->f_dentry, qstring);
87 if(tmp_dentry == NULL) {
88 cERROR(1,("Failed allocating dentry"));
93 *ptmp_inode = new_inode(file->f_dentry->d_sb);
94 tmp_dentry->d_op = &cifs_dentry_ops;
95 if(*ptmp_inode == NULL)
98 d_instantiate(tmp_dentry, *ptmp_inode);
102 tmp_dentry->d_time = jiffies;
103 *pnew_dentry = tmp_dentry;
107 static void fill_in_inode(struct inode *tmp_inode,
108 FILE_DIRECTORY_INFO *pfindData, int *pobject_type, int isNewInode)
111 struct timespec local_mtime;
113 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
114 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
115 __u32 attr = le32_to_cpu(pfindData->ExtFileAttributes);
116 __u64 allocation_size = le64_to_cpu(pfindData->AllocationSize);
117 __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
119 cifsInfo->cifsAttrs = attr;
120 cifsInfo->time = jiffies;
122 /* save mtime and size */
123 local_mtime = tmp_inode->i_mtime;
124 local_size = tmp_inode->i_size;
126 /* Linux can not store file creation time unfortunately so ignore it */
128 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
130 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
132 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
133 /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
134 /* 2767 perms - indicate mandatory locking */
135 /* BB fill in uid and gid here? with help from winbind?
136 or retrieve from NTFS stream extended attribute */
137 if (atomic_read(&cifsInfo->inUse) == 0) {
138 tmp_inode->i_uid = cifs_sb->mnt_uid;
139 tmp_inode->i_gid = cifs_sb->mnt_gid;
140 /* set default mode. will override for dirs below */
141 tmp_inode->i_mode = cifs_sb->mnt_file_mode;
144 if (attr & ATTR_DIRECTORY) {
145 *pobject_type = DT_DIR;
146 /* override default perms since we do not lock dirs */
147 if(atomic_read(&cifsInfo->inUse) == 0) {
148 tmp_inode->i_mode = cifs_sb->mnt_dir_mode;
150 tmp_inode->i_mode |= S_IFDIR;
151 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
152 (attr & ATTR_SYSTEM) && (end_of_file == 0)) {
153 *pobject_type = DT_FIFO;
154 tmp_inode->i_mode |= S_IFIFO;
155 /* BB Finish for SFU style symlinks and devies */
156 /* } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
157 (attr & ATTR_SYSTEM) && ) { */
158 /* we no longer mark these because we could not follow them */
159 /* } else if (attr & ATTR_REPARSE) {
160 *pobject_type = DT_LNK;
161 tmp_inode->i_mode |= S_IFLNK; */
163 *pobject_type = DT_REG;
164 tmp_inode->i_mode |= S_IFREG;
165 if (attr & ATTR_READONLY)
166 tmp_inode->i_mode &= ~(S_IWUGO);
167 } /* could add code here - to validate if device or weird share type? */
169 /* can not fill in nlink here as in qpathinfo version and Unx search */
170 if (atomic_read(&cifsInfo->inUse) == 0) {
171 atomic_set(&cifsInfo->inUse, 1);
174 if (is_size_safe_to_change(cifsInfo)) {
175 /* can not safely change the file size here if the
176 client is writing to it due to potential races */
177 i_size_write(tmp_inode, end_of_file);
179 /* 512 bytes (2**9) is the fake blocksize that must be used */
180 /* for this calculation, even though the reported blocksize is larger */
181 tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
184 if (allocation_size < end_of_file)
185 cFYI(1, ("May be sparse file, allocation less than file size"));
187 ("File Size %ld and blocks %ld and blocksize %ld",
188 (unsigned long)tmp_inode->i_size, tmp_inode->i_blocks,
189 tmp_inode->i_blksize));
190 if (S_ISREG(tmp_inode->i_mode)) {
191 cFYI(1, ("File inode"));
192 tmp_inode->i_op = &cifs_file_inode_ops;
193 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
194 tmp_inode->i_fop = &cifs_file_direct_ops;
196 tmp_inode->i_fop = &cifs_file_ops;
197 tmp_inode->i_data.a_ops = &cifs_addr_ops;
200 return; /* No sense invalidating pages for new inode
201 since have not started caching readahead file
204 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
205 (local_size == tmp_inode->i_size)) {
206 cFYI(1, ("inode exists but unchanged"));
208 /* file may have changed on server */
209 cFYI(1, ("invalidate inode, readdir detected change"));
210 invalidate_remote_inode(tmp_inode);
212 } else if (S_ISDIR(tmp_inode->i_mode)) {
213 cFYI(1, ("Directory inode"));
214 tmp_inode->i_op = &cifs_dir_inode_ops;
215 tmp_inode->i_fop = &cifs_dir_ops;
216 } else if (S_ISLNK(tmp_inode->i_mode)) {
217 cFYI(1, ("Symbolic Link inode"));
218 tmp_inode->i_op = &cifs_symlink_inode_ops;
220 cFYI(1, ("Init special inode"));
221 init_special_inode(tmp_inode, tmp_inode->i_mode,
226 static void unix_fill_in_inode(struct inode *tmp_inode,
227 FILE_UNIX_INFO *pfindData, int *pobject_type, int isNewInode)
230 struct timespec local_mtime;
232 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
233 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
235 __u32 type = le32_to_cpu(pfindData->Type);
236 __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes);
237 __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
238 cifsInfo->time = jiffies;
239 atomic_inc(&cifsInfo->inUse);
241 /* save mtime and size */
242 local_mtime = tmp_inode->i_mtime;
243 local_size = tmp_inode->i_size;
246 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
248 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime));
250 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange));
252 tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions);
253 if (type == UNIX_FILE) {
254 *pobject_type = DT_REG;
255 tmp_inode->i_mode |= S_IFREG;
256 } else if (type == UNIX_SYMLINK) {
257 *pobject_type = DT_LNK;
258 tmp_inode->i_mode |= S_IFLNK;
259 } else if (type == UNIX_DIR) {
260 *pobject_type = DT_DIR;
261 tmp_inode->i_mode |= S_IFDIR;
262 } else if (type == UNIX_CHARDEV) {
263 *pobject_type = DT_CHR;
264 tmp_inode->i_mode |= S_IFCHR;
265 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
266 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
267 } else if (type == UNIX_BLOCKDEV) {
268 *pobject_type = DT_BLK;
269 tmp_inode->i_mode |= S_IFBLK;
270 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
271 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
272 } else if (type == UNIX_FIFO) {
273 *pobject_type = DT_FIFO;
274 tmp_inode->i_mode |= S_IFIFO;
275 } else if (type == UNIX_SOCKET) {
276 *pobject_type = DT_SOCK;
277 tmp_inode->i_mode |= S_IFSOCK;
280 tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
281 tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
282 tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
284 if (is_size_safe_to_change(cifsInfo)) {
285 /* can not safely change the file size here if the
286 client is writing to it due to potential races */
287 i_size_write(tmp_inode,end_of_file);
289 /* 512 bytes (2**9) is the fake blocksize that must be used */
290 /* for this calculation, not the real blocksize */
291 tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
294 if (S_ISREG(tmp_inode->i_mode)) {
295 cFYI(1, ("File inode"));
296 tmp_inode->i_op = &cifs_file_inode_ops;
297 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
298 tmp_inode->i_fop = &cifs_file_direct_ops;
300 tmp_inode->i_fop = &cifs_file_ops;
301 tmp_inode->i_data.a_ops = &cifs_addr_ops;
304 return; /* No sense invalidating pages for new inode since we
305 have not started caching readahead file data yet */
307 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
308 (local_size == tmp_inode->i_size)) {
309 cFYI(1, ("inode exists but unchanged"));
311 /* file may have changed on server */
312 cFYI(1, ("invalidate inode, readdir detected change"));
313 invalidate_remote_inode(tmp_inode);
315 } else if (S_ISDIR(tmp_inode->i_mode)) {
316 cFYI(1, ("Directory inode"));
317 tmp_inode->i_op = &cifs_dir_inode_ops;
318 tmp_inode->i_fop = &cifs_dir_ops;
319 } else if (S_ISLNK(tmp_inode->i_mode)) {
320 cFYI(1, ("Symbolic Link inode"));
321 tmp_inode->i_op = &cifs_symlink_inode_ops;
322 /* tmp_inode->i_fop = *//* do not need to set to anything */
324 cFYI(1, ("Special inode"));
325 init_special_inode(tmp_inode, tmp_inode->i_mode,
330 static int initiate_cifs_search(const int xid, struct file *file)
334 struct cifsFileInfo * cifsFile;
335 struct cifs_sb_info *cifs_sb;
336 struct cifsTconInfo *pTcon;
338 if(file->private_data == NULL) {
340 kmalloc(sizeof(struct cifsFileInfo),GFP_KERNEL);
343 if(file->private_data == NULL) {
346 memset(file->private_data,0,sizeof(struct cifsFileInfo));
348 cifsFile = file->private_data;
349 cifsFile->invalidHandle = TRUE;
350 cifsFile->srch_inf.endOfSearch = FALSE;
352 if(file->f_dentry == NULL)
355 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
359 pTcon = cifs_sb->tcon;
363 down(&file->f_dentry->d_sb->s_vfs_rename_sem);
364 full_path = build_path_from_dentry(file->f_dentry, cifs_sb);
365 up(&file->f_dentry->d_sb->s_vfs_rename_sem);
367 if(full_path == NULL) {
371 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
374 /* test for Unix extensions */
375 if (pTcon->ses->capabilities & CAP_UNIX) {
376 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
377 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
378 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
379 } else /* not srvinos - BB fixme add check for backlevel? */ {
380 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
383 rc = CIFSFindFirst(xid, pTcon,full_path,cifs_sb->local_nls,
384 &cifsFile->netfid, &cifsFile->srch_inf,
385 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
387 cifsFile->invalidHandle = FALSE;
388 if((rc == -EOPNOTSUPP) &&
389 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
390 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
397 /* return length of unicode string in bytes */
398 static int cifs_unicode_bytelen(char *str)
401 __le16 * ustr = (__le16 *)str;
403 for(len=0;len <= PATH_MAX;len++) {
407 cFYI(1,("Unicode string longer than PATH_MAX found"));
411 static char *nxt_dir_entry(char *old_entry, char *end_of_smb)
414 FILE_DIRECTORY_INFO * pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
416 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
417 cFYI(1,("new entry %p old entry %p",new_entry,old_entry));
418 /* validate that new_entry is not past end of SMB */
419 if(new_entry >= end_of_smb) {
421 ("search entry %p began after end of SMB %p old entry %p",
422 new_entry, end_of_smb, old_entry));
424 } else if (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb) {
425 cERROR(1,("search entry %p extends after end of SMB %p",
426 new_entry, end_of_smb));
433 #define UNICODE_DOT cpu_to_le16(0x2e)
435 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
436 static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
439 char * filename = NULL;
442 if(cfile->srch_inf.info_level == 0x202) {
443 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
444 filename = &pFindData->FileName[0];
445 if(cfile->srch_inf.unicode) {
446 len = cifs_unicode_bytelen(filename);
448 /* BB should we make this strnlen of PATH_MAX? */
449 len = strnlen(filename, 5);
451 } else if(cfile->srch_inf.info_level == 0x101) {
452 FILE_DIRECTORY_INFO * pFindData =
453 (FILE_DIRECTORY_INFO *)current_entry;
454 filename = &pFindData->FileName[0];
455 len = le32_to_cpu(pFindData->FileNameLength);
456 } else if(cfile->srch_inf.info_level == 0x102) {
457 FILE_FULL_DIRECTORY_INFO * pFindData =
458 (FILE_FULL_DIRECTORY_INFO *)current_entry;
459 filename = &pFindData->FileName[0];
460 len = le32_to_cpu(pFindData->FileNameLength);
461 } else if(cfile->srch_inf.info_level == 0x105) {
462 SEARCH_ID_FULL_DIR_INFO * pFindData =
463 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
464 filename = &pFindData->FileName[0];
465 len = le32_to_cpu(pFindData->FileNameLength);
466 } else if(cfile->srch_inf.info_level == 0x104) {
467 FILE_BOTH_DIRECTORY_INFO * pFindData =
468 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
469 filename = &pFindData->FileName[0];
470 len = le32_to_cpu(pFindData->FileNameLength);
472 cFYI(1,("Unknown findfirst level %d",cfile->srch_inf.info_level));
476 if(cfile->srch_inf.unicode) {
477 __le16 *ufilename = (__le16 *)filename;
480 if(ufilename[0] == UNICODE_DOT)
482 } else if(len == 4) {
484 if((ufilename[0] == UNICODE_DOT)
485 &&(ufilename[1] == UNICODE_DOT))
490 if(filename[0] == '.')
492 } else if(len == 2) {
493 if((filename[0] == '.') && (filename[1] == '.'))
502 /* find the corresponding entry in the search */
503 /* Note that the SMB server returns search entries for . and .. which
504 complicates logic here if we choose to parse for them and we do not
505 assume that they are located in the findfirst return buffer.*/
506 /* We start counting in the buffer with entry 2 and increment for every
507 entry (do not increment for . or .. entry) */
508 static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
509 struct file *file, char **ppCurrentEntry, int *num_to_ret)
513 loff_t first_entry_in_buffer;
514 loff_t index_to_find = file->f_pos;
515 struct cifsFileInfo * cifsFile = file->private_data;
516 /* check if index in the buffer */
518 if((cifsFile == NULL) || (ppCurrentEntry == NULL) || (num_to_ret == NULL))
521 *ppCurrentEntry = NULL;
522 first_entry_in_buffer =
523 cifsFile->srch_inf.index_of_last_entry -
524 cifsFile->srch_inf.entries_in_buffer;
525 /* dump_cifs_file_struct(file, "In fce ");*/
526 if(index_to_find < first_entry_in_buffer) {
527 /* close and restart search */
528 cFYI(1,("search backing up - close and restart search"));
529 cifsFile->invalidHandle = TRUE;
530 CIFSFindClose(xid, pTcon, cifsFile->netfid);
531 kfree(cifsFile->search_resume_name);
532 cifsFile->search_resume_name = NULL;
533 if(cifsFile->srch_inf.ntwrk_buf_start) {
534 cFYI(1,("freeing SMB ff cache buf on search rewind"));
535 cifs_buf_release(cifsFile->srch_inf.ntwrk_buf_start);
537 rc = initiate_cifs_search(xid,file);
539 cFYI(1,("error %d reinitiating a search on rewind",rc));
544 while((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
545 (rc == 0) && (cifsFile->srch_inf.endOfSearch == FALSE)){
546 cFYI(1,("calling findnext2"));
547 rc = CIFSFindNext(xid,pTcon,cifsFile->netfid,
548 &cifsFile->srch_inf);
552 if(index_to_find < cifsFile->srch_inf.index_of_last_entry) {
553 /* we found the buffer that contains the entry */
554 /* scan and find it */
556 char * current_entry;
557 char * end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
558 smbCalcSize((struct smb_hdr *)
559 cifsFile->srch_inf.ntwrk_buf_start);
560 /* dump_cifs_file_struct(file,"found entry in fce "); */
561 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
562 - cifsFile->srch_inf.entries_in_buffer;
563 pos_in_buf = index_to_find - first_entry_in_buffer;
564 cFYI(1,("found entry - pos_in_buf %d",pos_in_buf));
565 current_entry = cifsFile->srch_inf.srch_entries_start;
566 for(i=0;(i<(pos_in_buf)) && (current_entry != NULL);i++) {
567 /* go entry by entry figuring out which is first */
570 rc = cifs_entry_is_dot(current_entry,cifsFile);
571 if(rc == 1) /* is . or .. so skip */ {
572 cFYI(1,("Entry is .")); /* BB removeme BB */
574 } else if (rc == 2 ) {
575 cFYI(1,("Entry is ..")); /* BB removeme BB */
578 current_entry = nxt_dir_entry(current_entry,end_of_smb);
580 if((current_entry == NULL) && (i < pos_in_buf)) {
581 /* BB fixme - check if we should flag this error */
582 cERROR(1,("reached end of buf searching for pos in buf"
583 " %d index to find %lld rc %d",
584 pos_in_buf,index_to_find,rc));
587 *ppCurrentEntry = current_entry;
589 cFYI(1,("index not in buffer - could not findnext into it"));
593 if(pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
594 cFYI(1,("can not return entries when pos_in_buf beyond last entry"));
597 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
598 /* dump_cifs_file_struct(file, "end fce ");*/
603 /* inode num, inode type and filename returned */
604 static int cifs_get_name_from_search_buf(struct qstr *pqst,
605 char *current_entry, __u16 level, unsigned int unicode,
606 struct cifs_sb_info * cifs_sb, ino_t *pinum)
609 unsigned int len = 0;
611 struct nls_table * nlt = cifs_sb->local_nls;
615 if(level == SMB_FIND_FILE_UNIX) {
616 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
618 filename = &pFindData->FileName[0];
620 len = cifs_unicode_bytelen(filename);
622 /* BB should we make this strnlen of PATH_MAX? */
623 len = strnlen(filename, PATH_MAX);
626 /* BB fixme - hash low and high 32 bits if not 64 bit arch BB fixme */
627 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
628 *pinum = pFindData->UniqueId;
629 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
630 FILE_DIRECTORY_INFO * pFindData =
631 (FILE_DIRECTORY_INFO *)current_entry;
632 filename = &pFindData->FileName[0];
633 len = le32_to_cpu(pFindData->FileNameLength);
634 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
635 FILE_FULL_DIRECTORY_INFO * pFindData =
636 (FILE_FULL_DIRECTORY_INFO *)current_entry;
637 filename = &pFindData->FileName[0];
638 len = le32_to_cpu(pFindData->FileNameLength);
639 } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
640 SEARCH_ID_FULL_DIR_INFO * pFindData =
641 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
642 filename = &pFindData->FileName[0];
643 len = le32_to_cpu(pFindData->FileNameLength);
644 *pinum = pFindData->UniqueId;
645 } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
646 FILE_BOTH_DIRECTORY_INFO * pFindData =
647 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
648 filename = &pFindData->FileName[0];
649 len = le32_to_cpu(pFindData->FileNameLength);
651 cFYI(1,("Unknown findfirst level %d",level));
655 /* BB fixme - test with long names */
656 /* Note converted filename can be longer than in unicode */
657 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
658 pqst->len = cifs_convertUCSpath((char *)pqst->name,
659 (__le16 *)filename, len/2, nlt);
661 pqst->len = cifs_strfromUCS_le((char *)pqst->name,
662 (wchar_t *)filename,len/2,nlt);
664 pqst->name = filename;
667 pqst->hash = full_name_hash(pqst->name,pqst->len);
668 /* cFYI(1,("filldir on %s",pqst->name)); */
672 static int cifs_filldir(char *pfindEntry, struct file *file,
673 filldir_t filldir, void *direntry, char *scratch_buf)
677 struct cifsFileInfo * pCifsF;
680 struct cifs_sb_info * cifs_sb;
681 struct inode *tmp_inode;
682 struct dentry *tmp_dentry;
684 /* get filename and len into qstring */
686 /* decide whether to create and populate ionde */
687 if((direntry == NULL) || (file == NULL))
690 pCifsF = file->private_data;
692 if((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
695 if(file->f_dentry == NULL)
698 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
700 qstring.name = scratch_buf;
701 rc = cifs_get_name_from_search_buf(&qstring,pfindEntry,
702 pCifsF->srch_inf.info_level,
703 pCifsF->srch_inf.unicode,cifs_sb,
704 &inum /* returned */);
709 rc = construct_dentry(&qstring,file,&tmp_inode, &tmp_dentry);
710 if((tmp_inode == NULL) || (tmp_dentry == NULL))
714 /* inode created, we need to hash it with right inode number */
716 /* BB fixme - hash the 2 32 quantities bits together if necessary BB */
717 tmp_inode->i_ino = inum;
719 insert_inode_hash(tmp_inode);
722 /* we pass in rc below, indicating whether it is a new inode,
723 so we can figure out whether to invalidate the inode cached
724 data if the file has changed */
725 if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
726 unix_fill_in_inode(tmp_inode,
727 (FILE_UNIX_INFO *)pfindEntry,&obj_type, rc);
729 fill_in_inode(tmp_inode,
730 (FILE_DIRECTORY_INFO *)pfindEntry,&obj_type, rc);
733 rc = filldir(direntry,qstring.name,qstring.len,file->f_pos,
734 tmp_inode->i_ino,obj_type);
736 cFYI(1,("filldir rc = %d",rc));
743 static int cifs_save_resume_key(const char *current_entry,
744 struct cifsFileInfo *cifsFile)
747 unsigned int len = 0;
751 if((cifsFile == NULL) || (current_entry == NULL))
754 level = cifsFile->srch_inf.info_level;
756 if(level == SMB_FIND_FILE_UNIX) {
757 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
759 filename = &pFindData->FileName[0];
760 if(cifsFile->srch_inf.unicode) {
761 len = cifs_unicode_bytelen(filename);
763 /* BB should we make this strnlen of PATH_MAX? */
764 len = strnlen(filename, PATH_MAX);
766 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
767 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
768 FILE_DIRECTORY_INFO * pFindData =
769 (FILE_DIRECTORY_INFO *)current_entry;
770 filename = &pFindData->FileName[0];
771 len = le32_to_cpu(pFindData->FileNameLength);
772 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
773 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
774 FILE_FULL_DIRECTORY_INFO * pFindData =
775 (FILE_FULL_DIRECTORY_INFO *)current_entry;
776 filename = &pFindData->FileName[0];
777 len = le32_to_cpu(pFindData->FileNameLength);
778 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
779 } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
780 SEARCH_ID_FULL_DIR_INFO * pFindData =
781 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
782 filename = &pFindData->FileName[0];
783 len = le32_to_cpu(pFindData->FileNameLength);
784 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
785 } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
786 FILE_BOTH_DIRECTORY_INFO * pFindData =
787 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
788 filename = &pFindData->FileName[0];
789 len = le32_to_cpu(pFindData->FileNameLength);
790 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
792 cFYI(1,("Unknown findfirst level %d",level));
795 cifsFile->srch_inf.resume_name_len = len;
796 cifsFile->srch_inf.presume_name = filename;
800 int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
804 struct cifs_sb_info *cifs_sb;
805 struct cifsTconInfo *pTcon;
806 struct cifsFileInfo *cifsFile = NULL;
807 char * current_entry;
809 char * tmp_buf = NULL;
814 if(file->f_dentry == NULL) {
818 /* dump_cifs_file_struct(file, "Begin rdir "); */
820 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
821 pTcon = cifs_sb->tcon;
825 /* cFYI(1,("readdir2 pos: %lld",file->f_pos)); */
827 switch ((int) file->f_pos) {
829 /*if (filldir(direntry, ".", 1, file->f_pos,
830 file->f_dentry->d_inode->i_ino, DT_DIR) < 0) {
831 cERROR(1, ("Filldir for current dir failed "));
837 /* if (filldir(direntry, "..", 2, file->f_pos,
838 file->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
839 cERROR(1, ("Filldir for parent dir failed "));
845 /* 1) If search is active,
846 is in current search buffer?
847 if it before then restart search
848 if after then keep searching till find it */
850 if(file->private_data == NULL) {
851 rc = initiate_cifs_search(xid,file);
852 cFYI(1,("initiate cifs search rc %d",rc));
859 if(file->private_data == NULL) {
864 cifsFile = file->private_data;
865 if (cifsFile->srch_inf.endOfSearch) {
866 if(cifsFile->srch_inf.emptyDir) {
867 cFYI(1, ("End of search, empty dir"));
872 cifsFile->invalidHandle = TRUE;
873 CIFSFindClose(xid, pTcon, cifsFile->netfid);
875 kfree(cifsFile->search_resume_name);
876 cifsFile->search_resume_name = NULL; */
878 /* BB account for . and .. in f_pos as special case */
879 /* dump_cifs_file_struct(file, "rdir after default ");*/
881 rc = find_cifs_entry(xid,pTcon, file,
882 ¤t_entry,&num_to_fill);
884 cFYI(1,("fce error %d",rc));
886 } else if (current_entry != NULL) {
887 cFYI(1,("entry %lld found",file->f_pos));
889 cFYI(1,("could not find entry"));
892 cFYI(1,("loop through %d times filling dir for net buf %p",
893 num_to_fill,cifsFile->srch_inf.ntwrk_buf_start));
894 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
895 smbCalcSize((struct smb_hdr *)
896 cifsFile->srch_inf.ntwrk_buf_start);
897 /* To be safe - for UCS to UTF-8 with strings loaded
898 with the rare long characters alloc more to account for
899 such multibyte target UTF-8 characters. cifs_unicode.c,
900 which actually does the conversion, has the same limit */
901 tmp_buf = kmalloc((2 * NAME_MAX) + 4, GFP_KERNEL);
902 for(i=0;(i<num_to_fill) && (rc == 0);i++) {
903 if(current_entry == NULL) {
904 /* evaluate whether this case is an error */
905 cERROR(1,("past end of SMB num to fill %d i %d",
910 rc = cifs_filldir(current_entry, file,
911 filldir, direntry,tmp_buf);
913 if(file->f_pos == cifsFile->srch_inf.index_of_last_entry) {
914 cFYI(1,("last entry in buf at pos %lld %s",
915 file->f_pos,tmp_buf)); /* BB removeme BB */
916 cifs_save_resume_key(current_entry,cifsFile);
919 current_entry = nxt_dir_entry(current_entry,
927 /* dump_cifs_file_struct(file, "end rdir "); */