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)
85 d_instantiate(tmp_dentry, *ptmp_inode);
88 tmp_dentry = d_alloc(file->f_dentry, qstring);
89 if(tmp_dentry == NULL) {
90 cERROR(1,("Failed allocating dentry"));
95 *ptmp_inode = new_inode(file->f_dentry->d_sb);
97 tmp_dentry->d_op = &cifs_ci_dentry_ops;
99 tmp_dentry->d_op = &cifs_dentry_ops;
100 if(*ptmp_inode == NULL)
103 d_instantiate(tmp_dentry, *ptmp_inode);
104 d_rehash(tmp_dentry);
107 tmp_dentry->d_time = jiffies;
108 *pnew_dentry = tmp_dentry;
112 static void fill_in_inode(struct inode *tmp_inode,
113 FILE_DIRECTORY_INFO *pfindData, int *pobject_type, int isNewInode)
116 struct timespec local_mtime;
118 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
119 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
120 __u32 attr = le32_to_cpu(pfindData->ExtFileAttributes);
121 __u64 allocation_size = le64_to_cpu(pfindData->AllocationSize);
122 __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
124 cifsInfo->cifsAttrs = attr;
125 cifsInfo->time = jiffies;
127 /* save mtime and size */
128 local_mtime = tmp_inode->i_mtime;
129 local_size = tmp_inode->i_size;
131 /* Linux can not store file creation time unfortunately so ignore it */
133 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
135 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
137 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
138 /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
139 /* 2767 perms - indicate mandatory locking */
140 /* BB fill in uid and gid here? with help from winbind?
141 or retrieve from NTFS stream extended attribute */
142 if (atomic_read(&cifsInfo->inUse) == 0) {
143 tmp_inode->i_uid = cifs_sb->mnt_uid;
144 tmp_inode->i_gid = cifs_sb->mnt_gid;
145 /* set default mode. will override for dirs below */
146 tmp_inode->i_mode = cifs_sb->mnt_file_mode;
148 /* mask off the type bits since it gets set
149 below and we do not want to get two type
151 tmp_inode->i_mode &= ~S_IFMT;
154 if (attr & ATTR_DIRECTORY) {
155 *pobject_type = DT_DIR;
156 /* override default perms since we do not lock dirs */
157 if(atomic_read(&cifsInfo->inUse) == 0) {
158 tmp_inode->i_mode = cifs_sb->mnt_dir_mode;
160 tmp_inode->i_mode |= S_IFDIR;
161 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
162 (attr & ATTR_SYSTEM)) {
163 if (end_of_file == 0) {
164 *pobject_type = DT_FIFO;
165 tmp_inode->i_mode |= S_IFIFO;
167 /* rather than get the type here, we mark the
168 inode as needing revalidate and get the real type
169 (blk vs chr vs. symlink) later ie in lookup */
170 *pobject_type = DT_REG;
171 tmp_inode->i_mode |= S_IFREG;
174 /* we no longer mark these because we could not follow them */
175 /* } else if (attr & ATTR_REPARSE) {
176 *pobject_type = DT_LNK;
177 tmp_inode->i_mode |= S_IFLNK; */
179 *pobject_type = DT_REG;
180 tmp_inode->i_mode |= S_IFREG;
181 if (attr & ATTR_READONLY)
182 tmp_inode->i_mode &= ~(S_IWUGO);
183 } /* could add code here - to validate if device or weird share type? */
185 /* can not fill in nlink here as in qpathinfo version and Unx search */
186 if (atomic_read(&cifsInfo->inUse) == 0) {
187 atomic_set(&cifsInfo->inUse, 1);
190 if (is_size_safe_to_change(cifsInfo)) {
191 /* can not safely change the file size here if the
192 client is writing to it due to potential races */
193 i_size_write(tmp_inode, end_of_file);
195 /* 512 bytes (2**9) is the fake blocksize that must be used */
196 /* for this calculation, even though the reported blocksize is larger */
197 tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
200 if (allocation_size < end_of_file)
201 cFYI(1, ("May be sparse file, allocation less than file size"));
202 cFYI(1, ("File Size %ld and blocks %llu and blocksize %ld",
203 (unsigned long)tmp_inode->i_size,
204 (unsigned long long)tmp_inode->i_blocks,
205 tmp_inode->i_blksize));
206 if (S_ISREG(tmp_inode->i_mode)) {
207 cFYI(1, ("File inode"));
208 tmp_inode->i_op = &cifs_file_inode_ops;
209 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
210 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
211 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
213 tmp_inode->i_fop = &cifs_file_direct_ops;
215 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
216 tmp_inode->i_fop = &cifs_file_nobrl_ops;
218 tmp_inode->i_fop = &cifs_file_ops;
220 if((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
221 (cifs_sb->tcon->ses->server->maxBuf <
222 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
223 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
225 tmp_inode->i_data.a_ops = &cifs_addr_ops;
228 return; /* No sense invalidating pages for new inode
229 since have not started caching readahead file
232 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
233 (local_size == tmp_inode->i_size)) {
234 cFYI(1, ("inode exists but unchanged"));
236 /* file may have changed on server */
237 cFYI(1, ("invalidate inode, readdir detected change"));
238 invalidate_remote_inode(tmp_inode);
240 } else if (S_ISDIR(tmp_inode->i_mode)) {
241 cFYI(1, ("Directory inode"));
242 tmp_inode->i_op = &cifs_dir_inode_ops;
243 tmp_inode->i_fop = &cifs_dir_ops;
244 } else if (S_ISLNK(tmp_inode->i_mode)) {
245 cFYI(1, ("Symbolic Link inode"));
246 tmp_inode->i_op = &cifs_symlink_inode_ops;
248 cFYI(1, ("Init special inode"));
249 init_special_inode(tmp_inode, tmp_inode->i_mode,
254 static void unix_fill_in_inode(struct inode *tmp_inode,
255 FILE_UNIX_INFO *pfindData, int *pobject_type, int isNewInode)
258 struct timespec local_mtime;
260 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
261 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
263 __u32 type = le32_to_cpu(pfindData->Type);
264 __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes);
265 __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
266 cifsInfo->time = jiffies;
267 atomic_inc(&cifsInfo->inUse);
269 /* save mtime and size */
270 local_mtime = tmp_inode->i_mtime;
271 local_size = tmp_inode->i_size;
274 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
276 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime));
278 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange));
280 tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions);
281 /* since we set the inode type below we need to mask off type
282 to avoid strange results if bits above were corrupt */
283 tmp_inode->i_mode &= ~S_IFMT;
284 if (type == UNIX_FILE) {
285 *pobject_type = DT_REG;
286 tmp_inode->i_mode |= S_IFREG;
287 } else if (type == UNIX_SYMLINK) {
288 *pobject_type = DT_LNK;
289 tmp_inode->i_mode |= S_IFLNK;
290 } else if (type == UNIX_DIR) {
291 *pobject_type = DT_DIR;
292 tmp_inode->i_mode |= S_IFDIR;
293 } else if (type == UNIX_CHARDEV) {
294 *pobject_type = DT_CHR;
295 tmp_inode->i_mode |= S_IFCHR;
296 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
297 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
298 } else if (type == UNIX_BLOCKDEV) {
299 *pobject_type = DT_BLK;
300 tmp_inode->i_mode |= S_IFBLK;
301 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
302 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
303 } else if (type == UNIX_FIFO) {
304 *pobject_type = DT_FIFO;
305 tmp_inode->i_mode |= S_IFIFO;
306 } else if (type == UNIX_SOCKET) {
307 *pobject_type = DT_SOCK;
308 tmp_inode->i_mode |= S_IFSOCK;
310 /* safest to just call it a file */
311 *pobject_type = DT_REG;
312 tmp_inode->i_mode |= S_IFREG;
313 cFYI(1,("unknown inode type %d",type));
316 tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
317 tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
318 tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
320 if (is_size_safe_to_change(cifsInfo)) {
321 /* can not safely change the file size here if the
322 client is writing to it due to potential races */
323 i_size_write(tmp_inode,end_of_file);
325 /* 512 bytes (2**9) is the fake blocksize that must be used */
326 /* for this calculation, not the real blocksize */
327 tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
330 if (S_ISREG(tmp_inode->i_mode)) {
331 cFYI(1, ("File inode"));
332 tmp_inode->i_op = &cifs_file_inode_ops;
334 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
335 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
336 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
338 tmp_inode->i_fop = &cifs_file_direct_ops;
340 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
341 tmp_inode->i_fop = &cifs_file_nobrl_ops;
343 tmp_inode->i_fop = &cifs_file_ops;
345 if((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
346 (cifs_sb->tcon->ses->server->maxBuf <
347 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
348 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
350 tmp_inode->i_data.a_ops = &cifs_addr_ops;
353 return; /* No sense invalidating pages for new inode since we
354 have not started caching readahead file data yet */
356 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
357 (local_size == tmp_inode->i_size)) {
358 cFYI(1, ("inode exists but unchanged"));
360 /* file may have changed on server */
361 cFYI(1, ("invalidate inode, readdir detected change"));
362 invalidate_remote_inode(tmp_inode);
364 } else if (S_ISDIR(tmp_inode->i_mode)) {
365 cFYI(1, ("Directory inode"));
366 tmp_inode->i_op = &cifs_dir_inode_ops;
367 tmp_inode->i_fop = &cifs_dir_ops;
368 } else if (S_ISLNK(tmp_inode->i_mode)) {
369 cFYI(1, ("Symbolic Link inode"));
370 tmp_inode->i_op = &cifs_symlink_inode_ops;
371 /* tmp_inode->i_fop = *//* do not need to set to anything */
373 cFYI(1, ("Special inode"));
374 init_special_inode(tmp_inode, tmp_inode->i_mode,
379 static int initiate_cifs_search(const int xid, struct file *file)
383 struct cifsFileInfo * cifsFile;
384 struct cifs_sb_info *cifs_sb;
385 struct cifsTconInfo *pTcon;
387 if(file->private_data == NULL) {
389 kmalloc(sizeof(struct cifsFileInfo),GFP_KERNEL);
392 if(file->private_data == NULL) {
395 memset(file->private_data,0,sizeof(struct cifsFileInfo));
397 cifsFile = file->private_data;
398 cifsFile->invalidHandle = TRUE;
399 cifsFile->srch_inf.endOfSearch = FALSE;
401 if(file->f_dentry == NULL)
404 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
408 pTcon = cifs_sb->tcon;
412 full_path = build_path_from_dentry(file->f_dentry);
414 if(full_path == NULL) {
418 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
421 /* test for Unix extensions */
422 if (pTcon->ses->capabilities & CAP_UNIX) {
423 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
424 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
425 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
426 } else /* not srvinos - BB fixme add check for backlevel? */ {
427 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
430 rc = CIFSFindFirst(xid, pTcon,full_path,cifs_sb->local_nls,
431 &cifsFile->netfid, &cifsFile->srch_inf,
432 cifs_sb->mnt_cifs_flags &
433 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
435 cifsFile->invalidHandle = FALSE;
436 if((rc == -EOPNOTSUPP) &&
437 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
438 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
445 /* return length of unicode string in bytes */
446 static int cifs_unicode_bytelen(char *str)
449 __le16 * ustr = (__le16 *)str;
451 for(len=0;len <= PATH_MAX;len++) {
455 cFYI(1,("Unicode string longer than PATH_MAX found"));
459 static char *nxt_dir_entry(char *old_entry, char *end_of_smb)
462 FILE_DIRECTORY_INFO * pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
464 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
465 cFYI(1,("new entry %p old entry %p",new_entry,old_entry));
466 /* validate that new_entry is not past end of SMB */
467 if(new_entry >= end_of_smb) {
469 ("search entry %p began after end of SMB %p old entry %p",
470 new_entry, end_of_smb, old_entry));
472 } else if (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb) {
473 cERROR(1,("search entry %p extends after end of SMB %p",
474 new_entry, end_of_smb));
481 #define UNICODE_DOT cpu_to_le16(0x2e)
483 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
484 static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
487 char * filename = NULL;
490 if(cfile->srch_inf.info_level == 0x202) {
491 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
492 filename = &pFindData->FileName[0];
493 if(cfile->srch_inf.unicode) {
494 len = cifs_unicode_bytelen(filename);
496 /* BB should we make this strnlen of PATH_MAX? */
497 len = strnlen(filename, 5);
499 } else if(cfile->srch_inf.info_level == 0x101) {
500 FILE_DIRECTORY_INFO * pFindData =
501 (FILE_DIRECTORY_INFO *)current_entry;
502 filename = &pFindData->FileName[0];
503 len = le32_to_cpu(pFindData->FileNameLength);
504 } else if(cfile->srch_inf.info_level == 0x102) {
505 FILE_FULL_DIRECTORY_INFO * pFindData =
506 (FILE_FULL_DIRECTORY_INFO *)current_entry;
507 filename = &pFindData->FileName[0];
508 len = le32_to_cpu(pFindData->FileNameLength);
509 } else if(cfile->srch_inf.info_level == 0x105) {
510 SEARCH_ID_FULL_DIR_INFO * pFindData =
511 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
512 filename = &pFindData->FileName[0];
513 len = le32_to_cpu(pFindData->FileNameLength);
514 } else if(cfile->srch_inf.info_level == 0x104) {
515 FILE_BOTH_DIRECTORY_INFO * pFindData =
516 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
517 filename = &pFindData->FileName[0];
518 len = le32_to_cpu(pFindData->FileNameLength);
520 cFYI(1,("Unknown findfirst level %d",cfile->srch_inf.info_level));
524 if(cfile->srch_inf.unicode) {
525 __le16 *ufilename = (__le16 *)filename;
528 if(ufilename[0] == UNICODE_DOT)
530 } else if(len == 4) {
532 if((ufilename[0] == UNICODE_DOT)
533 &&(ufilename[1] == UNICODE_DOT))
538 if(filename[0] == '.')
540 } else if(len == 2) {
541 if((filename[0] == '.') && (filename[1] == '.'))
550 /* Check if directory that we are searching has changed so we can decide
551 whether we can use the cached search results from the previous search */
552 static int is_dir_changed(struct file * file)
554 struct inode * inode;
555 struct cifsInodeInfo *cifsInfo;
557 if(file->f_dentry == NULL)
560 inode = file->f_dentry->d_inode;
565 cifsInfo = CIFS_I(inode);
567 if(cifsInfo->time == 0)
568 return 1; /* directory was changed, perhaps due to unlink */
574 /* find the corresponding entry in the search */
575 /* Note that the SMB server returns search entries for . and .. which
576 complicates logic here if we choose to parse for them and we do not
577 assume that they are located in the findfirst return buffer.*/
578 /* We start counting in the buffer with entry 2 and increment for every
579 entry (do not increment for . or .. entry) */
580 static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
581 struct file *file, char **ppCurrentEntry, int *num_to_ret)
585 loff_t first_entry_in_buffer;
586 loff_t index_to_find = file->f_pos;
587 struct cifsFileInfo * cifsFile = file->private_data;
588 /* check if index in the buffer */
590 if((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
591 (num_to_ret == NULL))
594 *ppCurrentEntry = NULL;
595 first_entry_in_buffer =
596 cifsFile->srch_inf.index_of_last_entry -
597 cifsFile->srch_inf.entries_in_buffer;
599 /* if first entry in buf is zero then is first buffer
600 in search response data which means it is likely . and ..
601 will be in this buffer, although some servers do not return
602 . and .. for the root of a drive and for those we need
603 to start two entries earlier */
605 #ifdef CONFIG_CIFS_DEBUG2
606 dump_cifs_file_struct(file, "In fce ");
608 if(((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
609 is_dir_changed(file)) ||
610 (index_to_find < first_entry_in_buffer)) {
611 /* close and restart search */
612 cFYI(1,("search backing up - close and restart search"));
613 cifsFile->invalidHandle = TRUE;
614 CIFSFindClose(xid, pTcon, cifsFile->netfid);
615 kfree(cifsFile->search_resume_name);
616 cifsFile->search_resume_name = NULL;
617 if(cifsFile->srch_inf.ntwrk_buf_start) {
618 cFYI(1,("freeing SMB ff cache buf on search rewind"));
619 if(cifsFile->srch_inf.smallBuf)
620 cifs_small_buf_release(cifsFile->srch_inf.
623 cifs_buf_release(cifsFile->srch_inf.
626 rc = initiate_cifs_search(xid,file);
628 cFYI(1,("error %d reinitiating a search on rewind",rc));
633 while((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
634 (rc == 0) && (cifsFile->srch_inf.endOfSearch == FALSE)){
635 cFYI(1,("calling findnext2"));
636 rc = CIFSFindNext(xid,pTcon,cifsFile->netfid,
637 &cifsFile->srch_inf);
641 if(index_to_find < cifsFile->srch_inf.index_of_last_entry) {
642 /* we found the buffer that contains the entry */
643 /* scan and find it */
645 char * current_entry;
646 char * end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
647 smbCalcSize((struct smb_hdr *)
648 cifsFile->srch_inf.ntwrk_buf_start);
650 current_entry = cifsFile->srch_inf.srch_entries_start;
651 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
652 - cifsFile->srch_inf.entries_in_buffer;
653 pos_in_buf = index_to_find - first_entry_in_buffer;
654 cFYI(1,("found entry - pos_in_buf %d",pos_in_buf));
655 for(i=0;(i<(pos_in_buf)) && (current_entry != NULL);i++) {
656 /* go entry by entry figuring out which is first */
657 current_entry = nxt_dir_entry(current_entry,end_of_smb);
659 if((current_entry == NULL) && (i < pos_in_buf)) {
660 /* BB fixme - check if we should flag this error */
661 cERROR(1,("reached end of buf searching for pos in buf"
662 " %d index to find %lld rc %d",
663 pos_in_buf,index_to_find,rc));
666 *ppCurrentEntry = current_entry;
668 cFYI(1,("index not in buffer - could not findnext into it"));
672 if(pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
673 cFYI(1,("can not return entries pos_in_buf beyond last entry"));
676 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
681 /* inode num, inode type and filename returned */
682 static int cifs_get_name_from_search_buf(struct qstr *pqst,
683 char *current_entry, __u16 level, unsigned int unicode,
684 struct cifs_sb_info * cifs_sb, ino_t *pinum)
687 unsigned int len = 0;
689 struct nls_table * nlt = cifs_sb->local_nls;
693 if(level == SMB_FIND_FILE_UNIX) {
694 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
696 filename = &pFindData->FileName[0];
698 len = cifs_unicode_bytelen(filename);
700 /* BB should we make this strnlen of PATH_MAX? */
701 len = strnlen(filename, PATH_MAX);
704 /* BB fixme - hash low and high 32 bits if not 64 bit arch BB fixme */
705 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
706 *pinum = pFindData->UniqueId;
707 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
708 FILE_DIRECTORY_INFO * pFindData =
709 (FILE_DIRECTORY_INFO *)current_entry;
710 filename = &pFindData->FileName[0];
711 len = le32_to_cpu(pFindData->FileNameLength);
712 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
713 FILE_FULL_DIRECTORY_INFO * pFindData =
714 (FILE_FULL_DIRECTORY_INFO *)current_entry;
715 filename = &pFindData->FileName[0];
716 len = le32_to_cpu(pFindData->FileNameLength);
717 } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
718 SEARCH_ID_FULL_DIR_INFO * pFindData =
719 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
720 filename = &pFindData->FileName[0];
721 len = le32_to_cpu(pFindData->FileNameLength);
722 *pinum = pFindData->UniqueId;
723 } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
724 FILE_BOTH_DIRECTORY_INFO * pFindData =
725 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
726 filename = &pFindData->FileName[0];
727 len = le32_to_cpu(pFindData->FileNameLength);
729 cFYI(1,("Unknown findfirst level %d",level));
733 /* BB fixme - test with long names */
734 /* Note converted filename can be longer than in unicode */
735 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
736 pqst->len = cifs_convertUCSpath((char *)pqst->name,
737 (__le16 *)filename, len/2, nlt);
739 pqst->len = cifs_strfromUCS_le((char *)pqst->name,
740 (__le16 *)filename,len/2,nlt);
742 pqst->name = filename;
745 pqst->hash = full_name_hash(pqst->name,pqst->len);
746 /* cFYI(1,("filldir on %s",pqst->name)); */
750 static int cifs_filldir(char *pfindEntry, struct file *file,
751 filldir_t filldir, void *direntry, char *scratch_buf)
755 struct cifsFileInfo * pCifsF;
758 struct cifs_sb_info * cifs_sb;
759 struct inode *tmp_inode;
760 struct dentry *tmp_dentry;
762 /* get filename and len into qstring */
764 /* decide whether to create and populate ionde */
765 if((direntry == NULL) || (file == NULL))
768 pCifsF = file->private_data;
770 if((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
773 if(file->f_dentry == NULL)
776 rc = cifs_entry_is_dot(pfindEntry,pCifsF);
777 /* skip . and .. since we added them first */
781 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
783 qstring.name = scratch_buf;
784 rc = cifs_get_name_from_search_buf(&qstring,pfindEntry,
785 pCifsF->srch_inf.info_level,
786 pCifsF->srch_inf.unicode,cifs_sb,
787 &inum /* returned */);
792 rc = construct_dentry(&qstring,file,&tmp_inode, &tmp_dentry);
793 if((tmp_inode == NULL) || (tmp_dentry == NULL))
797 /* inode created, we need to hash it with right inode number */
799 /* BB fixme - hash the 2 32 quantities bits together if necessary BB */
800 tmp_inode->i_ino = inum;
802 insert_inode_hash(tmp_inode);
805 /* we pass in rc below, indicating whether it is a new inode,
806 so we can figure out whether to invalidate the inode cached
807 data if the file has changed */
808 if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
809 unix_fill_in_inode(tmp_inode,
810 (FILE_UNIX_INFO *)pfindEntry,&obj_type, rc);
812 fill_in_inode(tmp_inode,
813 (FILE_DIRECTORY_INFO *)pfindEntry,&obj_type, rc);
816 rc = filldir(direntry,qstring.name,qstring.len,file->f_pos,
817 tmp_inode->i_ino,obj_type);
819 cFYI(1,("filldir rc = %d",rc));
826 static int cifs_save_resume_key(const char *current_entry,
827 struct cifsFileInfo *cifsFile)
830 unsigned int len = 0;
834 if((cifsFile == NULL) || (current_entry == NULL))
837 level = cifsFile->srch_inf.info_level;
839 if(level == SMB_FIND_FILE_UNIX) {
840 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
842 filename = &pFindData->FileName[0];
843 if(cifsFile->srch_inf.unicode) {
844 len = cifs_unicode_bytelen(filename);
846 /* BB should we make this strnlen of PATH_MAX? */
847 len = strnlen(filename, PATH_MAX);
849 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
850 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
851 FILE_DIRECTORY_INFO * pFindData =
852 (FILE_DIRECTORY_INFO *)current_entry;
853 filename = &pFindData->FileName[0];
854 len = le32_to_cpu(pFindData->FileNameLength);
855 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
856 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
857 FILE_FULL_DIRECTORY_INFO * pFindData =
858 (FILE_FULL_DIRECTORY_INFO *)current_entry;
859 filename = &pFindData->FileName[0];
860 len = le32_to_cpu(pFindData->FileNameLength);
861 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
862 } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
863 SEARCH_ID_FULL_DIR_INFO * pFindData =
864 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
865 filename = &pFindData->FileName[0];
866 len = le32_to_cpu(pFindData->FileNameLength);
867 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
868 } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
869 FILE_BOTH_DIRECTORY_INFO * pFindData =
870 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
871 filename = &pFindData->FileName[0];
872 len = le32_to_cpu(pFindData->FileNameLength);
873 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
875 cFYI(1,("Unknown findfirst level %d",level));
878 cifsFile->srch_inf.resume_name_len = len;
879 cifsFile->srch_inf.presume_name = filename;
883 int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
887 struct cifs_sb_info *cifs_sb;
888 struct cifsTconInfo *pTcon;
889 struct cifsFileInfo *cifsFile = NULL;
890 char * current_entry;
892 char * tmp_buf = NULL;
897 if(file->f_dentry == NULL) {
902 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
903 pTcon = cifs_sb->tcon;
907 switch ((int) file->f_pos) {
909 if (filldir(direntry, ".", 1, file->f_pos,
910 file->f_dentry->d_inode->i_ino, DT_DIR) < 0) {
911 cERROR(1, ("Filldir for current dir failed"));
917 if (filldir(direntry, "..", 2, file->f_pos,
918 file->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
919 cERROR(1, ("Filldir for parent dir failed"));
925 /* 1) If search is active,
926 is in current search buffer?
927 if it before then restart search
928 if after then keep searching till find it */
930 if(file->private_data == NULL) {
931 rc = initiate_cifs_search(xid,file);
932 cFYI(1,("initiate cifs search rc %d",rc));
938 if(file->private_data == NULL) {
943 cifsFile = file->private_data;
944 if (cifsFile->srch_inf.endOfSearch) {
945 if(cifsFile->srch_inf.emptyDir) {
946 cFYI(1, ("End of search, empty dir"));
951 cifsFile->invalidHandle = TRUE;
952 CIFSFindClose(xid, pTcon, cifsFile->netfid);
954 kfree(cifsFile->search_resume_name);
955 cifsFile->search_resume_name = NULL; */
957 rc = find_cifs_entry(xid,pTcon, file,
958 ¤t_entry,&num_to_fill);
960 cFYI(1,("fce error %d",rc));
962 } else if (current_entry != NULL) {
963 cFYI(1,("entry %lld found",file->f_pos));
965 cFYI(1,("could not find entry"));
968 cFYI(1,("loop through %d times filling dir for net buf %p",
969 num_to_fill,cifsFile->srch_inf.ntwrk_buf_start));
970 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
971 smbCalcSize((struct smb_hdr *)
972 cifsFile->srch_inf.ntwrk_buf_start);
973 /* To be safe - for UCS to UTF-8 with strings loaded
974 with the rare long characters alloc more to account for
975 such multibyte target UTF-8 characters. cifs_unicode.c,
976 which actually does the conversion, has the same limit */
977 tmp_buf = kmalloc((2 * NAME_MAX) + 4, GFP_KERNEL);
978 for(i=0;(i<num_to_fill) && (rc == 0);i++) {
979 if(current_entry == NULL) {
980 /* evaluate whether this case is an error */
981 cERROR(1,("past end of SMB num to fill %d i %d",
985 /* if buggy server returns . and .. late do
986 we want to check for that here? */
987 rc = cifs_filldir(current_entry, file,
988 filldir, direntry,tmp_buf);
991 cifsFile->srch_inf.index_of_last_entry) {
992 cFYI(1,("last entry in buf at pos %lld %s",
993 file->f_pos,tmp_buf));
994 cifs_save_resume_key(current_entry,cifsFile);
997 current_entry = nxt_dir_entry(current_entry,