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 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
198 tmp_inode->i_fop->lock = NULL;
199 tmp_inode->i_data.a_ops = &cifs_addr_ops;
202 return; /* No sense invalidating pages for new inode
203 since have not started caching readahead file
206 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
207 (local_size == tmp_inode->i_size)) {
208 cFYI(1, ("inode exists but unchanged"));
210 /* file may have changed on server */
211 cFYI(1, ("invalidate inode, readdir detected change"));
212 invalidate_remote_inode(tmp_inode);
214 } else if (S_ISDIR(tmp_inode->i_mode)) {
215 cFYI(1, ("Directory inode"));
216 tmp_inode->i_op = &cifs_dir_inode_ops;
217 tmp_inode->i_fop = &cifs_dir_ops;
218 } else if (S_ISLNK(tmp_inode->i_mode)) {
219 cFYI(1, ("Symbolic Link inode"));
220 tmp_inode->i_op = &cifs_symlink_inode_ops;
222 cFYI(1, ("Init special inode"));
223 init_special_inode(tmp_inode, tmp_inode->i_mode,
228 static void unix_fill_in_inode(struct inode *tmp_inode,
229 FILE_UNIX_INFO *pfindData, int *pobject_type, int isNewInode)
232 struct timespec local_mtime;
234 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
235 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
237 __u32 type = le32_to_cpu(pfindData->Type);
238 __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes);
239 __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
240 cifsInfo->time = jiffies;
241 atomic_inc(&cifsInfo->inUse);
243 /* save mtime and size */
244 local_mtime = tmp_inode->i_mtime;
245 local_size = tmp_inode->i_size;
248 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
250 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime));
252 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange));
254 tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions);
255 if (type == UNIX_FILE) {
256 *pobject_type = DT_REG;
257 tmp_inode->i_mode |= S_IFREG;
258 } else if (type == UNIX_SYMLINK) {
259 *pobject_type = DT_LNK;
260 tmp_inode->i_mode |= S_IFLNK;
261 } else if (type == UNIX_DIR) {
262 *pobject_type = DT_DIR;
263 tmp_inode->i_mode |= S_IFDIR;
264 } else if (type == UNIX_CHARDEV) {
265 *pobject_type = DT_CHR;
266 tmp_inode->i_mode |= S_IFCHR;
267 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
268 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
269 } else if (type == UNIX_BLOCKDEV) {
270 *pobject_type = DT_BLK;
271 tmp_inode->i_mode |= S_IFBLK;
272 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
273 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
274 } else if (type == UNIX_FIFO) {
275 *pobject_type = DT_FIFO;
276 tmp_inode->i_mode |= S_IFIFO;
277 } else if (type == UNIX_SOCKET) {
278 *pobject_type = DT_SOCK;
279 tmp_inode->i_mode |= S_IFSOCK;
282 tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
283 tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
284 tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
286 if (is_size_safe_to_change(cifsInfo)) {
287 /* can not safely change the file size here if the
288 client is writing to it due to potential races */
289 i_size_write(tmp_inode,end_of_file);
291 /* 512 bytes (2**9) is the fake blocksize that must be used */
292 /* for this calculation, not the real blocksize */
293 tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
296 if (S_ISREG(tmp_inode->i_mode)) {
297 cFYI(1, ("File inode"));
298 tmp_inode->i_op = &cifs_file_inode_ops;
299 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
300 tmp_inode->i_fop = &cifs_file_direct_ops;
302 tmp_inode->i_fop = &cifs_file_ops;
303 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
304 tmp_inode->i_fop->lock = NULL;
305 tmp_inode->i_data.a_ops = &cifs_addr_ops;
308 return; /* No sense invalidating pages for new inode since we
309 have not started caching readahead file data yet */
311 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
312 (local_size == tmp_inode->i_size)) {
313 cFYI(1, ("inode exists but unchanged"));
315 /* file may have changed on server */
316 cFYI(1, ("invalidate inode, readdir detected change"));
317 invalidate_remote_inode(tmp_inode);
319 } else if (S_ISDIR(tmp_inode->i_mode)) {
320 cFYI(1, ("Directory inode"));
321 tmp_inode->i_op = &cifs_dir_inode_ops;
322 tmp_inode->i_fop = &cifs_dir_ops;
323 } else if (S_ISLNK(tmp_inode->i_mode)) {
324 cFYI(1, ("Symbolic Link inode"));
325 tmp_inode->i_op = &cifs_symlink_inode_ops;
326 /* tmp_inode->i_fop = *//* do not need to set to anything */
328 cFYI(1, ("Special inode"));
329 init_special_inode(tmp_inode, tmp_inode->i_mode,
334 static int initiate_cifs_search(const int xid, struct file *file)
338 struct cifsFileInfo * cifsFile;
339 struct cifs_sb_info *cifs_sb;
340 struct cifsTconInfo *pTcon;
342 if(file->private_data == NULL) {
344 kmalloc(sizeof(struct cifsFileInfo),GFP_KERNEL);
347 if(file->private_data == NULL) {
350 memset(file->private_data,0,sizeof(struct cifsFileInfo));
352 cifsFile = file->private_data;
353 cifsFile->invalidHandle = TRUE;
354 cifsFile->srch_inf.endOfSearch = FALSE;
356 if(file->f_dentry == NULL)
359 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
363 pTcon = cifs_sb->tcon;
367 down(&file->f_dentry->d_sb->s_vfs_rename_sem);
368 full_path = build_path_from_dentry(file->f_dentry, cifs_sb);
369 up(&file->f_dentry->d_sb->s_vfs_rename_sem);
371 if(full_path == NULL) {
375 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
378 /* test for Unix extensions */
379 if (pTcon->ses->capabilities & CAP_UNIX) {
380 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
381 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
382 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
383 } else /* not srvinos - BB fixme add check for backlevel? */ {
384 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
387 rc = CIFSFindFirst(xid, pTcon,full_path,cifs_sb->local_nls,
388 &cifsFile->netfid, &cifsFile->srch_inf,
389 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
391 cifsFile->invalidHandle = FALSE;
392 if((rc == -EOPNOTSUPP) &&
393 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
394 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
401 /* return length of unicode string in bytes */
402 static int cifs_unicode_bytelen(char *str)
405 __le16 * ustr = (__le16 *)str;
407 for(len=0;len <= PATH_MAX;len++) {
411 cFYI(1,("Unicode string longer than PATH_MAX found"));
415 static char *nxt_dir_entry(char *old_entry, char *end_of_smb)
418 FILE_DIRECTORY_INFO * pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
420 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
421 cFYI(1,("new entry %p old entry %p",new_entry,old_entry));
422 /* validate that new_entry is not past end of SMB */
423 if(new_entry >= end_of_smb) {
425 ("search entry %p began after end of SMB %p old entry %p",
426 new_entry, end_of_smb, old_entry));
428 } else if (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb) {
429 cERROR(1,("search entry %p extends after end of SMB %p",
430 new_entry, end_of_smb));
437 #define UNICODE_DOT cpu_to_le16(0x2e)
439 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
440 static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
443 char * filename = NULL;
446 if(cfile->srch_inf.info_level == 0x202) {
447 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
448 filename = &pFindData->FileName[0];
449 if(cfile->srch_inf.unicode) {
450 len = cifs_unicode_bytelen(filename);
452 /* BB should we make this strnlen of PATH_MAX? */
453 len = strnlen(filename, 5);
455 } else if(cfile->srch_inf.info_level == 0x101) {
456 FILE_DIRECTORY_INFO * pFindData =
457 (FILE_DIRECTORY_INFO *)current_entry;
458 filename = &pFindData->FileName[0];
459 len = le32_to_cpu(pFindData->FileNameLength);
460 } else if(cfile->srch_inf.info_level == 0x102) {
461 FILE_FULL_DIRECTORY_INFO * pFindData =
462 (FILE_FULL_DIRECTORY_INFO *)current_entry;
463 filename = &pFindData->FileName[0];
464 len = le32_to_cpu(pFindData->FileNameLength);
465 } else if(cfile->srch_inf.info_level == 0x105) {
466 SEARCH_ID_FULL_DIR_INFO * pFindData =
467 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
468 filename = &pFindData->FileName[0];
469 len = le32_to_cpu(pFindData->FileNameLength);
470 } else if(cfile->srch_inf.info_level == 0x104) {
471 FILE_BOTH_DIRECTORY_INFO * pFindData =
472 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
473 filename = &pFindData->FileName[0];
474 len = le32_to_cpu(pFindData->FileNameLength);
476 cFYI(1,("Unknown findfirst level %d",cfile->srch_inf.info_level));
480 if(cfile->srch_inf.unicode) {
481 __le16 *ufilename = (__le16 *)filename;
484 if(ufilename[0] == UNICODE_DOT)
486 } else if(len == 4) {
488 if((ufilename[0] == UNICODE_DOT)
489 &&(ufilename[1] == UNICODE_DOT))
494 if(filename[0] == '.')
496 } else if(len == 2) {
497 if((filename[0] == '.') && (filename[1] == '.'))
506 /* find the corresponding entry in the search */
507 /* Note that the SMB server returns search entries for . and .. which
508 complicates logic here if we choose to parse for them and we do not
509 assume that they are located in the findfirst return buffer.*/
510 /* We start counting in the buffer with entry 2 and increment for every
511 entry (do not increment for . or .. entry) */
512 static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
513 struct file *file, char **ppCurrentEntry, int *num_to_ret)
517 loff_t first_entry_in_buffer;
518 loff_t index_to_find = file->f_pos;
519 struct cifsFileInfo * cifsFile = file->private_data;
520 /* check if index in the buffer */
522 if((cifsFile == NULL) || (ppCurrentEntry == NULL) || (num_to_ret == NULL))
525 *ppCurrentEntry = NULL;
526 first_entry_in_buffer =
527 cifsFile->srch_inf.index_of_last_entry -
528 cifsFile->srch_inf.entries_in_buffer;
529 /* dump_cifs_file_struct(file, "In fce ");*/
530 if(index_to_find < first_entry_in_buffer) {
531 /* close and restart search */
532 cFYI(1,("search backing up - close and restart search"));
533 cifsFile->invalidHandle = TRUE;
534 CIFSFindClose(xid, pTcon, cifsFile->netfid);
535 kfree(cifsFile->search_resume_name);
536 cifsFile->search_resume_name = NULL;
537 if(cifsFile->srch_inf.ntwrk_buf_start) {
538 cFYI(1,("freeing SMB ff cache buf on search rewind"));
539 cifs_buf_release(cifsFile->srch_inf.ntwrk_buf_start);
541 rc = initiate_cifs_search(xid,file);
543 cFYI(1,("error %d reinitiating a search on rewind",rc));
548 while((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
549 (rc == 0) && (cifsFile->srch_inf.endOfSearch == FALSE)){
550 cFYI(1,("calling findnext2"));
551 rc = CIFSFindNext(xid,pTcon,cifsFile->netfid,
552 &cifsFile->srch_inf);
556 if(index_to_find < cifsFile->srch_inf.index_of_last_entry) {
557 /* we found the buffer that contains the entry */
558 /* scan and find it */
560 char * current_entry;
561 char * end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
562 smbCalcSize((struct smb_hdr *)
563 cifsFile->srch_inf.ntwrk_buf_start);
564 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
565 - cifsFile->srch_inf.entries_in_buffer;
566 pos_in_buf = index_to_find - first_entry_in_buffer;
567 cFYI(1,("found entry - pos_in_buf %d",pos_in_buf));
568 current_entry = cifsFile->srch_inf.srch_entries_start;
569 for(i=0;(i<(pos_in_buf)) && (current_entry != NULL);i++) {
570 /* go entry by entry figuring out which is first */
573 rc = cifs_entry_is_dot(current_entry,cifsFile);
574 if(rc == 1) /* is . or .. so skip */ {
575 cFYI(1,("Entry is .")); /* BB removeme BB */
577 } else if (rc == 2 ) {
578 cFYI(1,("Entry is ..")); /* BB removeme BB */
581 current_entry = nxt_dir_entry(current_entry,end_of_smb);
583 if((current_entry == NULL) && (i < pos_in_buf)) {
584 /* BB fixme - check if we should flag this error */
585 cERROR(1,("reached end of buf searching for pos in buf"
586 " %d index to find %lld rc %d",
587 pos_in_buf,index_to_find,rc));
590 *ppCurrentEntry = current_entry;
592 cFYI(1,("index not in buffer - could not findnext into it"));
596 if(pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
597 cFYI(1,("can not return entries when pos_in_buf beyond last entry"));
600 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
605 /* inode num, inode type and filename returned */
606 static int cifs_get_name_from_search_buf(struct qstr *pqst,
607 char *current_entry, __u16 level, unsigned int unicode,
608 struct cifs_sb_info * cifs_sb, ino_t *pinum)
611 unsigned int len = 0;
613 struct nls_table * nlt = cifs_sb->local_nls;
617 if(level == SMB_FIND_FILE_UNIX) {
618 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
620 filename = &pFindData->FileName[0];
622 len = cifs_unicode_bytelen(filename);
624 /* BB should we make this strnlen of PATH_MAX? */
625 len = strnlen(filename, PATH_MAX);
628 /* BB fixme - hash low and high 32 bits if not 64 bit arch BB fixme */
629 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
630 *pinum = pFindData->UniqueId;
631 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
632 FILE_DIRECTORY_INFO * pFindData =
633 (FILE_DIRECTORY_INFO *)current_entry;
634 filename = &pFindData->FileName[0];
635 len = le32_to_cpu(pFindData->FileNameLength);
636 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
637 FILE_FULL_DIRECTORY_INFO * pFindData =
638 (FILE_FULL_DIRECTORY_INFO *)current_entry;
639 filename = &pFindData->FileName[0];
640 len = le32_to_cpu(pFindData->FileNameLength);
641 } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
642 SEARCH_ID_FULL_DIR_INFO * pFindData =
643 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
644 filename = &pFindData->FileName[0];
645 len = le32_to_cpu(pFindData->FileNameLength);
646 *pinum = pFindData->UniqueId;
647 } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
648 FILE_BOTH_DIRECTORY_INFO * pFindData =
649 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
650 filename = &pFindData->FileName[0];
651 len = le32_to_cpu(pFindData->FileNameLength);
653 cFYI(1,("Unknown findfirst level %d",level));
657 /* BB fixme - test with long names */
658 /* Note converted filename can be longer than in unicode */
659 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
660 pqst->len = cifs_convertUCSpath((char *)pqst->name,
661 (__le16 *)filename, len/2, nlt);
663 pqst->len = cifs_strfromUCS_le((char *)pqst->name,
664 (wchar_t *)filename,len/2,nlt);
666 pqst->name = filename;
669 pqst->hash = full_name_hash(pqst->name,pqst->len);
670 /* cFYI(1,("filldir on %s",pqst->name)); */
674 static int cifs_filldir(char *pfindEntry, struct file *file,
675 filldir_t filldir, void *direntry, char *scratch_buf)
679 struct cifsFileInfo * pCifsF;
682 struct cifs_sb_info * cifs_sb;
683 struct inode *tmp_inode;
684 struct dentry *tmp_dentry;
686 /* get filename and len into qstring */
688 /* decide whether to create and populate ionde */
689 if((direntry == NULL) || (file == NULL))
692 pCifsF = file->private_data;
694 if((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
697 if(file->f_dentry == NULL)
700 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
702 qstring.name = scratch_buf;
703 rc = cifs_get_name_from_search_buf(&qstring,pfindEntry,
704 pCifsF->srch_inf.info_level,
705 pCifsF->srch_inf.unicode,cifs_sb,
706 &inum /* returned */);
711 rc = construct_dentry(&qstring,file,&tmp_inode, &tmp_dentry);
712 if((tmp_inode == NULL) || (tmp_dentry == NULL))
716 /* inode created, we need to hash it with right inode number */
718 /* BB fixme - hash the 2 32 quantities bits together if necessary BB */
719 tmp_inode->i_ino = inum;
721 insert_inode_hash(tmp_inode);
724 /* we pass in rc below, indicating whether it is a new inode,
725 so we can figure out whether to invalidate the inode cached
726 data if the file has changed */
727 if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
728 unix_fill_in_inode(tmp_inode,
729 (FILE_UNIX_INFO *)pfindEntry,&obj_type, rc);
731 fill_in_inode(tmp_inode,
732 (FILE_DIRECTORY_INFO *)pfindEntry,&obj_type, rc);
735 rc = filldir(direntry,qstring.name,qstring.len,file->f_pos,
736 tmp_inode->i_ino,obj_type);
738 cFYI(1,("filldir rc = %d",rc));
745 static int cifs_save_resume_key(const char *current_entry,
746 struct cifsFileInfo *cifsFile)
749 unsigned int len = 0;
753 if((cifsFile == NULL) || (current_entry == NULL))
756 level = cifsFile->srch_inf.info_level;
758 if(level == SMB_FIND_FILE_UNIX) {
759 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
761 filename = &pFindData->FileName[0];
762 if(cifsFile->srch_inf.unicode) {
763 len = cifs_unicode_bytelen(filename);
765 /* BB should we make this strnlen of PATH_MAX? */
766 len = strnlen(filename, PATH_MAX);
768 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
769 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
770 FILE_DIRECTORY_INFO * pFindData =
771 (FILE_DIRECTORY_INFO *)current_entry;
772 filename = &pFindData->FileName[0];
773 len = le32_to_cpu(pFindData->FileNameLength);
774 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
775 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
776 FILE_FULL_DIRECTORY_INFO * pFindData =
777 (FILE_FULL_DIRECTORY_INFO *)current_entry;
778 filename = &pFindData->FileName[0];
779 len = le32_to_cpu(pFindData->FileNameLength);
780 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
781 } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
782 SEARCH_ID_FULL_DIR_INFO * pFindData =
783 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
784 filename = &pFindData->FileName[0];
785 len = le32_to_cpu(pFindData->FileNameLength);
786 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
787 } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
788 FILE_BOTH_DIRECTORY_INFO * pFindData =
789 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
790 filename = &pFindData->FileName[0];
791 len = le32_to_cpu(pFindData->FileNameLength);
792 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
794 cFYI(1,("Unknown findfirst level %d",level));
797 cifsFile->srch_inf.resume_name_len = len;
798 cifsFile->srch_inf.presume_name = filename;
802 int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
806 struct cifs_sb_info *cifs_sb;
807 struct cifsTconInfo *pTcon;
808 struct cifsFileInfo *cifsFile = NULL;
809 char * current_entry;
811 char * tmp_buf = NULL;
816 if(file->f_dentry == NULL) {
821 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
822 pTcon = cifs_sb->tcon;
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 */
880 rc = find_cifs_entry(xid,pTcon, file,
881 ¤t_entry,&num_to_fill);
883 cFYI(1,("fce error %d",rc));
885 } else if (current_entry != NULL) {
886 cFYI(1,("entry %lld found",file->f_pos));
888 cFYI(1,("could not find entry"));
891 cFYI(1,("loop through %d times filling dir for net buf %p",
892 num_to_fill,cifsFile->srch_inf.ntwrk_buf_start));
893 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
894 smbCalcSize((struct smb_hdr *)
895 cifsFile->srch_inf.ntwrk_buf_start);
896 /* To be safe - for UCS to UTF-8 with strings loaded
897 with the rare long characters alloc more to account for
898 such multibyte target UTF-8 characters. cifs_unicode.c,
899 which actually does the conversion, has the same limit */
900 tmp_buf = kmalloc((2 * NAME_MAX) + 4, GFP_KERNEL);
901 for(i=0;(i<num_to_fill) && (rc == 0);i++) {
902 if(current_entry == NULL) {
903 /* evaluate whether this case is an error */
904 cERROR(1,("past end of SMB num to fill %d i %d",
909 rc = cifs_filldir(current_entry, file,
910 filldir, direntry,tmp_buf);
912 if(file->f_pos == cifsFile->srch_inf.index_of_last_entry) {
913 cFYI(1,("last entry in buf at pos %lld %s",
914 file->f_pos,tmp_buf)); /* BB removeme BB */
915 cifs_save_resume_key(current_entry,cifsFile);
918 current_entry = nxt_dir_entry(current_entry,