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 /* we no longer mark these because we could not follow them */
152 /* } else if (attr & ATTR_REPARSE) {
153 *pobject_type = DT_LNK;
154 tmp_inode->i_mode |= S_IFLNK; */
156 *pobject_type = DT_REG;
157 tmp_inode->i_mode |= S_IFREG;
158 if (attr & ATTR_READONLY)
159 tmp_inode->i_mode &= ~(S_IWUGO);
160 } /* could add code here - to validate if device or weird share type? */
162 /* can not fill in nlink here as in qpathinfo version and Unx search */
163 if (atomic_read(&cifsInfo->inUse) == 0) {
164 atomic_set(&cifsInfo->inUse, 1);
167 if (is_size_safe_to_change(cifsInfo)) {
168 /* can not safely change the file size here if the
169 client is writing to it due to potential races */
170 i_size_write(tmp_inode, end_of_file);
172 /* 512 bytes (2**9) is the fake blocksize that must be used */
173 /* for this calculation, even though the reported blocksize is larger */
174 tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
177 if (allocation_size < end_of_file)
178 cFYI(1, ("May be sparse file, allocation less than file size"));
180 ("File Size %ld and blocks %ld and blocksize %ld",
181 (unsigned long)tmp_inode->i_size, tmp_inode->i_blocks,
182 tmp_inode->i_blksize));
183 if (S_ISREG(tmp_inode->i_mode)) {
184 cFYI(1, ("File inode"));
185 tmp_inode->i_op = &cifs_file_inode_ops;
186 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
187 tmp_inode->i_fop = &cifs_file_direct_ops;
189 tmp_inode->i_fop = &cifs_file_ops;
190 tmp_inode->i_data.a_ops = &cifs_addr_ops;
193 return; /* No sense invalidating pages for new inode
194 since have not started caching readahead file
197 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
198 (local_size == tmp_inode->i_size)) {
199 cFYI(1, ("inode exists but unchanged"));
201 /* file may have changed on server */
202 cFYI(1, ("invalidate inode, readdir detected change"));
203 invalidate_remote_inode(tmp_inode);
205 } else if (S_ISDIR(tmp_inode->i_mode)) {
206 cFYI(1, ("Directory inode"));
207 tmp_inode->i_op = &cifs_dir_inode_ops;
208 tmp_inode->i_fop = &cifs_dir_ops;
209 } else if (S_ISLNK(tmp_inode->i_mode)) {
210 cFYI(1, ("Symbolic Link inode"));
211 tmp_inode->i_op = &cifs_symlink_inode_ops;
213 cFYI(1, ("Init special inode"));
214 init_special_inode(tmp_inode, tmp_inode->i_mode,
219 static void unix_fill_in_inode(struct inode *tmp_inode,
220 FILE_UNIX_INFO *pfindData, int *pobject_type, int isNewInode)
223 struct timespec local_mtime;
225 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
226 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
228 __u32 type = le32_to_cpu(pfindData->Type);
229 __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes);
230 __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
231 cifsInfo->time = jiffies;
232 atomic_inc(&cifsInfo->inUse);
234 /* save mtime and size */
235 local_mtime = tmp_inode->i_mtime;
236 local_size = tmp_inode->i_size;
239 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
241 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime));
243 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange));
245 tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions);
246 if (type == UNIX_FILE) {
247 *pobject_type = DT_REG;
248 tmp_inode->i_mode |= S_IFREG;
249 } else if (type == UNIX_SYMLINK) {
250 *pobject_type = DT_LNK;
251 tmp_inode->i_mode |= S_IFLNK;
252 } else if (type == UNIX_DIR) {
253 *pobject_type = DT_DIR;
254 tmp_inode->i_mode |= S_IFDIR;
255 } else if (type == UNIX_CHARDEV) {
256 *pobject_type = DT_CHR;
257 tmp_inode->i_mode |= S_IFCHR;
258 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
259 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
260 } else if (type == UNIX_BLOCKDEV) {
261 *pobject_type = DT_BLK;
262 tmp_inode->i_mode |= S_IFBLK;
263 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
264 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
265 } else if (type == UNIX_FIFO) {
266 *pobject_type = DT_FIFO;
267 tmp_inode->i_mode |= S_IFIFO;
268 } else if (type == UNIX_SOCKET) {
269 *pobject_type = DT_SOCK;
270 tmp_inode->i_mode |= S_IFSOCK;
273 tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
274 tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
275 tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
277 if (is_size_safe_to_change(cifsInfo)) {
278 /* can not safely change the file size here if the
279 client is writing to it due to potential races */
280 i_size_write(tmp_inode,end_of_file);
282 /* 512 bytes (2**9) is the fake blocksize that must be used */
283 /* for this calculation, not the real blocksize */
284 tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
287 if (S_ISREG(tmp_inode->i_mode)) {
288 cFYI(1, ("File inode"));
289 tmp_inode->i_op = &cifs_file_inode_ops;
290 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
291 tmp_inode->i_fop = &cifs_file_direct_ops;
293 tmp_inode->i_fop = &cifs_file_ops;
294 tmp_inode->i_data.a_ops = &cifs_addr_ops;
297 return; /* No sense invalidating pages for new inode since we
298 have not started caching readahead file data yet */
300 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
301 (local_size == tmp_inode->i_size)) {
302 cFYI(1, ("inode exists but unchanged"));
304 /* file may have changed on server */
305 cFYI(1, ("invalidate inode, readdir detected change"));
306 invalidate_remote_inode(tmp_inode);
308 } else if (S_ISDIR(tmp_inode->i_mode)) {
309 cFYI(1, ("Directory inode"));
310 tmp_inode->i_op = &cifs_dir_inode_ops;
311 tmp_inode->i_fop = &cifs_dir_ops;
312 } else if (S_ISLNK(tmp_inode->i_mode)) {
313 cFYI(1, ("Symbolic Link inode"));
314 tmp_inode->i_op = &cifs_symlink_inode_ops;
315 /* tmp_inode->i_fop = *//* do not need to set to anything */
317 cFYI(1, ("Special inode"));
318 init_special_inode(tmp_inode, tmp_inode->i_mode,
323 static int initiate_cifs_search(const int xid, struct file *file)
327 struct cifsFileInfo * cifsFile;
328 struct cifs_sb_info *cifs_sb;
329 struct cifsTconInfo *pTcon;
331 if(file->private_data == NULL) {
333 kmalloc(sizeof(struct cifsFileInfo),GFP_KERNEL);
336 if(file->private_data == NULL) {
339 memset(file->private_data,0,sizeof(struct cifsFileInfo));
341 cifsFile = file->private_data;
342 cifsFile->invalidHandle = TRUE;
343 cifsFile->srch_inf.endOfSearch = FALSE;
345 if(file->f_dentry == NULL)
348 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
352 pTcon = cifs_sb->tcon;
356 down(&file->f_dentry->d_sb->s_vfs_rename_sem);
357 full_path = build_path_from_dentry(file->f_dentry, cifs_sb);
358 up(&file->f_dentry->d_sb->s_vfs_rename_sem);
360 if(full_path == NULL) {
364 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
367 /* test for Unix extensions */
368 if (pTcon->ses->capabilities & CAP_UNIX) {
369 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
370 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
371 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
372 } else /* not srvinos - BB fixme add check for backlevel? */ {
373 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
376 rc = CIFSFindFirst(xid, pTcon,full_path,cifs_sb->local_nls,
377 &cifsFile->netfid, &cifsFile->srch_inf,
378 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
380 cifsFile->invalidHandle = FALSE;
381 if((rc == -EOPNOTSUPP) &&
382 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
383 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
390 /* return length of unicode string in bytes */
391 static int cifs_unicode_bytelen(char *str)
394 __le16 * ustr = (__le16 *)str;
396 for(len=0;len <= PATH_MAX;len++) {
400 cFYI(1,("Unicode string longer than PATH_MAX found"));
404 static char *nxt_dir_entry(char *old_entry, char *end_of_smb)
407 FILE_DIRECTORY_INFO * pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
409 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
410 cFYI(1,("new entry %p old entry %p",new_entry,old_entry));
411 /* validate that new_entry is not past end of SMB */
412 if(new_entry >= end_of_smb) {
414 ("search entry %p began after end of SMB %p old entry %p",
415 new_entry, end_of_smb, old_entry));
417 } else if (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb) {
418 cERROR(1,("search entry %p extends after end of SMB %p",
419 new_entry, end_of_smb));
426 #define UNICODE_DOT cpu_to_le16(0x2e)
428 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
429 static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
432 char * filename = NULL;
435 if(cfile->srch_inf.info_level == 0x202) {
436 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
437 filename = &pFindData->FileName[0];
438 if(cfile->srch_inf.unicode) {
439 len = cifs_unicode_bytelen(filename);
441 /* BB should we make this strnlen of PATH_MAX? */
442 len = strnlen(filename, 5);
444 } else if(cfile->srch_inf.info_level == 0x101) {
445 FILE_DIRECTORY_INFO * pFindData =
446 (FILE_DIRECTORY_INFO *)current_entry;
447 filename = &pFindData->FileName[0];
448 len = le32_to_cpu(pFindData->FileNameLength);
449 } else if(cfile->srch_inf.info_level == 0x102) {
450 FILE_FULL_DIRECTORY_INFO * pFindData =
451 (FILE_FULL_DIRECTORY_INFO *)current_entry;
452 filename = &pFindData->FileName[0];
453 len = le32_to_cpu(pFindData->FileNameLength);
454 } else if(cfile->srch_inf.info_level == 0x105) {
455 SEARCH_ID_FULL_DIR_INFO * pFindData =
456 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
457 filename = &pFindData->FileName[0];
458 len = le32_to_cpu(pFindData->FileNameLength);
459 } else if(cfile->srch_inf.info_level == 0x104) {
460 FILE_BOTH_DIRECTORY_INFO * pFindData =
461 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
462 filename = &pFindData->FileName[0];
463 len = le32_to_cpu(pFindData->FileNameLength);
465 cFYI(1,("Unknown findfirst level %d",cfile->srch_inf.info_level));
469 if(cfile->srch_inf.unicode) {
470 __le16 *ufilename = (__le16 *)filename;
473 if(ufilename[0] == UNICODE_DOT)
475 } else if(len == 4) {
477 if((ufilename[0] == UNICODE_DOT)
478 &&(ufilename[1] == UNICODE_DOT))
483 if(filename[0] == '.')
485 } else if(len == 2) {
486 if((filename[0] == '.') && (filename[1] == '.'))
495 /* find the corresponding entry in the search */
496 /* Note that the SMB server returns search entries for . and .. which
497 complicates logic here if we choose to parse for them and we do not
498 assume that they are located in the findfirst return buffer.*/
499 /* We start counting in the buffer with entry 2 and increment for every
500 entry (do not increment for . or .. entry) */
501 static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
502 struct file *file, char **ppCurrentEntry, int *num_to_ret)
506 loff_t first_entry_in_buffer;
507 loff_t index_to_find = file->f_pos;
508 struct cifsFileInfo * cifsFile = file->private_data;
509 /* check if index in the buffer */
511 if((cifsFile == NULL) || (ppCurrentEntry == NULL) || (num_to_ret == NULL))
514 *ppCurrentEntry = NULL;
515 first_entry_in_buffer =
516 cifsFile->srch_inf.index_of_last_entry -
517 cifsFile->srch_inf.entries_in_buffer;
518 /* dump_cifs_file_struct(file, "In fce ");*/
519 if(index_to_find < first_entry_in_buffer) {
520 /* close and restart search */
521 cFYI(1,("search backing up - close and restart search"));
522 cifsFile->invalidHandle = TRUE;
523 CIFSFindClose(xid, pTcon, cifsFile->netfid);
524 kfree(cifsFile->search_resume_name);
525 cifsFile->search_resume_name = NULL;
526 if(cifsFile->srch_inf.ntwrk_buf_start) {
527 cFYI(1,("freeing SMB ff cache buf on search rewind"));
528 cifs_buf_release(cifsFile->srch_inf.ntwrk_buf_start);
530 rc = initiate_cifs_search(xid,file);
532 cFYI(1,("error %d reinitiating a search on rewind",rc));
537 while((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
538 (rc == 0) && (cifsFile->srch_inf.endOfSearch == FALSE)){
539 cFYI(1,("calling findnext2"));
540 rc = CIFSFindNext(xid,pTcon,cifsFile->netfid,
541 &cifsFile->srch_inf);
545 if(index_to_find < cifsFile->srch_inf.index_of_last_entry) {
546 /* we found the buffer that contains the entry */
547 /* scan and find it */
549 char * current_entry;
550 char * end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
551 smbCalcSize((struct smb_hdr *)
552 cifsFile->srch_inf.ntwrk_buf_start);
553 /* dump_cifs_file_struct(file,"found entry in fce "); */
554 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
555 - cifsFile->srch_inf.entries_in_buffer;
556 pos_in_buf = index_to_find - first_entry_in_buffer;
557 cFYI(1,("found entry - pos_in_buf %d",pos_in_buf));
558 current_entry = cifsFile->srch_inf.srch_entries_start;
559 for(i=0;(i<(pos_in_buf)) && (current_entry != NULL);i++) {
560 /* go entry by entry figuring out which is first */
563 rc = cifs_entry_is_dot(current_entry,cifsFile);
564 if(rc == 1) /* is . or .. so skip */ {
565 cFYI(1,("Entry is .")); /* BB removeme BB */
567 } else if (rc == 2 ) {
568 cFYI(1,("Entry is ..")); /* BB removeme BB */
571 current_entry = nxt_dir_entry(current_entry,end_of_smb);
573 if((current_entry == NULL) && (i < pos_in_buf)) {
574 /* BB fixme - check if we should flag this error */
575 cERROR(1,("reached end of buf searching for pos in buf"
576 " %d index to find %lld rc %d",
577 pos_in_buf,index_to_find,rc));
580 *ppCurrentEntry = current_entry;
582 cFYI(1,("index not in buffer - could not findnext into it"));
586 if(pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
587 cFYI(1,("can not return entries when pos_in_buf beyond last entry"));
590 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
591 /* dump_cifs_file_struct(file, "end fce ");*/
596 /* inode num, inode type and filename returned */
597 static int cifs_get_name_from_search_buf(struct qstr *pqst,
598 char *current_entry, __u16 level, unsigned int unicode,
599 struct cifs_sb_info * cifs_sb, ino_t *pinum)
602 unsigned int len = 0;
604 struct nls_table * nlt = cifs_sb->local_nls;
608 if(level == SMB_FIND_FILE_UNIX) {
609 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
611 filename = &pFindData->FileName[0];
613 len = cifs_unicode_bytelen(filename);
615 /* BB should we make this strnlen of PATH_MAX? */
616 len = strnlen(filename, PATH_MAX);
619 /* BB fixme - hash low and high 32 bits if not 64 bit arch BB fixme */
620 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
621 *pinum = pFindData->UniqueId;
622 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
623 FILE_DIRECTORY_INFO * pFindData =
624 (FILE_DIRECTORY_INFO *)current_entry;
625 filename = &pFindData->FileName[0];
626 len = le32_to_cpu(pFindData->FileNameLength);
627 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
628 FILE_FULL_DIRECTORY_INFO * pFindData =
629 (FILE_FULL_DIRECTORY_INFO *)current_entry;
630 filename = &pFindData->FileName[0];
631 len = le32_to_cpu(pFindData->FileNameLength);
632 } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
633 SEARCH_ID_FULL_DIR_INFO * pFindData =
634 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
635 filename = &pFindData->FileName[0];
636 len = le32_to_cpu(pFindData->FileNameLength);
637 *pinum = pFindData->UniqueId;
638 } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
639 FILE_BOTH_DIRECTORY_INFO * pFindData =
640 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
641 filename = &pFindData->FileName[0];
642 len = le32_to_cpu(pFindData->FileNameLength);
644 cFYI(1,("Unknown findfirst level %d",level));
648 /* BB fixme - test with long names */
649 /* Note converted filename can be longer than in unicode */
650 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
651 pqst->len = cifs_convertUCSpath((char *)pqst->name,
652 (__le16 *)filename, len/2, nlt);
654 pqst->len = cifs_strfromUCS_le((char *)pqst->name,
655 (wchar_t *)filename,len/2,nlt);
657 pqst->name = filename;
660 pqst->hash = full_name_hash(pqst->name,pqst->len);
661 /* cFYI(1,("filldir on %s",pqst->name)); */
665 static int cifs_filldir(char *pfindEntry, struct file *file,
666 filldir_t filldir, void *direntry, char *scratch_buf)
670 struct cifsFileInfo * pCifsF;
673 struct cifs_sb_info * cifs_sb;
674 struct inode *tmp_inode;
675 struct dentry *tmp_dentry;
677 /* get filename and len into qstring */
679 /* decide whether to create and populate ionde */
680 if((direntry == NULL) || (file == NULL))
683 pCifsF = file->private_data;
685 if((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
688 if(file->f_dentry == NULL)
691 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
693 qstring.name = scratch_buf;
694 rc = cifs_get_name_from_search_buf(&qstring,pfindEntry,
695 pCifsF->srch_inf.info_level,
696 pCifsF->srch_inf.unicode,cifs_sb,
697 &inum /* returned */);
702 rc = construct_dentry(&qstring,file,&tmp_inode, &tmp_dentry);
703 if((tmp_inode == NULL) || (tmp_dentry == NULL))
707 /* inode created, we need to hash it with right inode number */
709 /* BB fixme - hash the 2 32 quantities bits together if necessary BB */
710 tmp_inode->i_ino = inum;
712 insert_inode_hash(tmp_inode);
715 /* we pass in rc below, indicating whether it is a new inode,
716 so we can figure out whether to invalidate the inode cached
717 data if the file has changed */
718 if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
719 unix_fill_in_inode(tmp_inode,
720 (FILE_UNIX_INFO *)pfindEntry,&obj_type, rc);
722 fill_in_inode(tmp_inode,
723 (FILE_DIRECTORY_INFO *)pfindEntry,&obj_type, rc);
726 rc = filldir(direntry,qstring.name,qstring.len,file->f_pos,
727 tmp_inode->i_ino,obj_type);
729 cFYI(1,("filldir rc = %d",rc));
736 static int cifs_save_resume_key(const char *current_entry,
737 struct cifsFileInfo *cifsFile)
740 unsigned int len = 0;
744 if((cifsFile == NULL) || (current_entry == NULL))
747 level = cifsFile->srch_inf.info_level;
749 if(level == SMB_FIND_FILE_UNIX) {
750 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
752 filename = &pFindData->FileName[0];
753 if(cifsFile->srch_inf.unicode) {
754 len = cifs_unicode_bytelen(filename);
756 /* BB should we make this strnlen of PATH_MAX? */
757 len = strnlen(filename, PATH_MAX);
759 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
760 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
761 FILE_DIRECTORY_INFO * pFindData =
762 (FILE_DIRECTORY_INFO *)current_entry;
763 filename = &pFindData->FileName[0];
764 len = le32_to_cpu(pFindData->FileNameLength);
765 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
766 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
767 FILE_FULL_DIRECTORY_INFO * pFindData =
768 (FILE_FULL_DIRECTORY_INFO *)current_entry;
769 filename = &pFindData->FileName[0];
770 len = le32_to_cpu(pFindData->FileNameLength);
771 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
772 } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
773 SEARCH_ID_FULL_DIR_INFO * pFindData =
774 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
775 filename = &pFindData->FileName[0];
776 len = le32_to_cpu(pFindData->FileNameLength);
777 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
778 } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
779 FILE_BOTH_DIRECTORY_INFO * pFindData =
780 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
781 filename = &pFindData->FileName[0];
782 len = le32_to_cpu(pFindData->FileNameLength);
783 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
785 cFYI(1,("Unknown findfirst level %d",level));
788 cifsFile->srch_inf.resume_name_len = len;
789 cifsFile->srch_inf.presume_name = filename;
793 int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
797 struct cifs_sb_info *cifs_sb;
798 struct cifsTconInfo *pTcon;
799 struct cifsFileInfo *cifsFile = NULL;
800 char * current_entry;
802 char * tmp_buf = NULL;
807 if(file->f_dentry == NULL) {
811 /* dump_cifs_file_struct(file, "Begin rdir "); */
813 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
814 pTcon = cifs_sb->tcon;
818 /* cFYI(1,("readdir2 pos: %lld",file->f_pos)); */
820 switch ((int) file->f_pos) {
822 /*if (filldir(direntry, ".", 1, file->f_pos,
823 file->f_dentry->d_inode->i_ino, DT_DIR) < 0) {
824 cERROR(1, ("Filldir for current dir failed "));
830 /* if (filldir(direntry, "..", 2, file->f_pos,
831 file->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
832 cERROR(1, ("Filldir for parent dir failed "));
838 /* 1) If search is active,
839 is in current search buffer?
840 if it before then restart search
841 if after then keep searching till find it */
843 if(file->private_data == NULL) {
844 rc = initiate_cifs_search(xid,file);
845 cFYI(1,("initiate cifs search rc %d",rc));
852 if(file->private_data == NULL) {
857 cifsFile = file->private_data;
858 if (cifsFile->srch_inf.endOfSearch) {
859 if(cifsFile->srch_inf.emptyDir) {
860 cFYI(1, ("End of search, empty dir"));
865 cifsFile->invalidHandle = TRUE;
866 CIFSFindClose(xid, pTcon, cifsFile->netfid);
868 kfree(cifsFile->search_resume_name);
869 cifsFile->search_resume_name = NULL; */
871 /* BB account for . and .. in f_pos as special case */
872 /* dump_cifs_file_struct(file, "rdir after default ");*/
874 rc = find_cifs_entry(xid,pTcon, file,
875 ¤t_entry,&num_to_fill);
877 cFYI(1,("fce error %d",rc));
879 } else if (current_entry != NULL) {
880 cFYI(1,("entry %lld found",file->f_pos));
882 cFYI(1,("could not find entry"));
885 cFYI(1,("loop through %d times filling dir for net buf %p",
886 num_to_fill,cifsFile->srch_inf.ntwrk_buf_start));
887 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
888 smbCalcSize((struct smb_hdr *)
889 cifsFile->srch_inf.ntwrk_buf_start);
890 /* To be safe - for UCS to UTF-8 with strings loaded
891 with the rare long characters alloc more to account for
892 such multibyte target UTF-8 characters. cifs_unicode.c,
893 which actually does the conversion, has the same limit */
894 tmp_buf = kmalloc((2 * NAME_MAX) + 4, GFP_KERNEL);
895 for(i=0;(i<num_to_fill) && (rc == 0);i++) {
896 if(current_entry == NULL) {
897 /* evaluate whether this case is an error */
898 cERROR(1,("past end of SMB num to fill %d i %d",
903 rc = cifs_filldir(current_entry, file,
904 filldir, direntry,tmp_buf);
906 if(file->f_pos == cifsFile->srch_inf.index_of_last_entry) {
907 cFYI(1,("last entry in buf at pos %lld %s",
908 file->f_pos,tmp_buf)); /* BB removeme BB */
909 cifs_save_resume_key(current_entry,cifsFile);
912 current_entry = nxt_dir_entry(current_entry,
920 /* dump_cifs_file_struct(file, "end rdir "); */