4 * Directory search handling
6 * Copyright (C) International Business Machines Corp., 2004, 2008
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>
28 #include "cifsproto.h"
29 #include "cifs_unicode.h"
30 #include "cifs_debug.h"
31 #include "cifs_fs_sb.h"
35 * To be safe - for UCS to UTF-8 with strings loaded with the rare long
36 * characters alloc more to account for such multibyte target UTF-8
39 #define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
41 #ifdef CONFIG_CIFS_DEBUG2
42 static void dump_cifs_file_struct(struct file *file, char *label)
44 struct cifsFileInfo *cf;
47 cf = file->private_data;
49 cFYI(1, ("empty cifs private file data"));
52 if (cf->invalidHandle)
53 cFYI(1, ("invalid handle"));
54 if (cf->srch_inf.endOfSearch)
55 cFYI(1, ("end of search"));
56 if (cf->srch_inf.emptyDir)
57 cFYI(1, ("empty dir"));
61 static inline void dump_cifs_file_struct(struct file *file, char *label)
67 * Find the dentry that matches "name". If there isn't one, create one. If it's
68 * a negative dentry or the uniqueid changed, then drop it and recreate it.
70 static struct dentry *
71 cifs_readdir_lookup(struct dentry *parent, struct qstr *name,
72 struct cifs_fattr *fattr)
74 struct dentry *dentry, *alias;
76 struct super_block *sb = parent->d_inode->i_sb;
78 cFYI(1, ("For %s", name->name));
80 dentry = d_lookup(parent, name);
82 /* FIXME: check for inode number changes? */
83 if (dentry->d_inode != NULL)
89 dentry = d_alloc(parent, name);
93 inode = cifs_iget(sb, fattr);
99 if (CIFS_SB(sb)->tcon->nocase)
100 dentry->d_op = &cifs_ci_dentry_ops;
102 dentry->d_op = &cifs_dentry_ops;
104 alias = d_materialise_unique(dentry, inode);
115 /* Returns 1 if new inode created, 2 if both dentry and inode were */
116 /* Might check in the future if inode number changed so we can rehash inode */
118 construct_dentry(struct qstr *qstring, struct file *file,
119 struct inode **ptmp_inode, struct dentry **pnew_dentry,
122 struct dentry *tmp_dentry = NULL;
123 struct super_block *sb = file->f_path.dentry->d_sb;
126 cFYI(1, ("For %s", qstring->name));
128 tmp_dentry = d_lookup(file->f_path.dentry, qstring);
130 /* BB: overwrite old name? i.e. tmp_dentry->d_name and
131 * tmp_dentry->d_name.len??
133 cFYI(0, ("existing dentry with inode 0x%p",
134 tmp_dentry->d_inode));
135 *ptmp_inode = tmp_dentry->d_inode;
136 if (*ptmp_inode == NULL) {
137 *ptmp_inode = cifs_new_inode(sb, inum);
138 if (*ptmp_inode == NULL)
143 tmp_dentry = d_alloc(file->f_path.dentry, qstring);
144 if (tmp_dentry == NULL) {
145 cERROR(1, ("Failed allocating dentry"));
150 if (CIFS_SB(sb)->tcon->nocase)
151 tmp_dentry->d_op = &cifs_ci_dentry_ops;
153 tmp_dentry->d_op = &cifs_dentry_ops;
155 *ptmp_inode = cifs_new_inode(sb, inum);
156 if (*ptmp_inode == NULL)
161 tmp_dentry->d_time = jiffies;
162 *pnew_dentry = tmp_dentry;
166 static void fill_in_inode(struct inode *tmp_inode, int new_buf_type,
167 char *buf, unsigned int *pobject_type, int isNewInode)
170 struct timespec local_mtime;
172 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
173 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
175 __u64 allocation_size;
177 umode_t default_mode;
179 /* save mtime and size */
180 local_mtime = tmp_inode->i_mtime;
181 local_size = tmp_inode->i_size;
184 FILE_DIRECTORY_INFO *pfindData = (FILE_DIRECTORY_INFO *)buf;
186 attr = le32_to_cpu(pfindData->ExtFileAttributes);
187 allocation_size = le64_to_cpu(pfindData->AllocationSize);
188 end_of_file = le64_to_cpu(pfindData->EndOfFile);
190 cifs_NTtimeToUnix(pfindData->LastAccessTime);
192 cifs_NTtimeToUnix(pfindData->LastWriteTime);
194 cifs_NTtimeToUnix(pfindData->ChangeTime);
195 } else { /* legacy, OS2 and DOS style */
196 int offset = cifs_sb->tcon->ses->server->timeAdj;
197 FIND_FILE_STANDARD_INFO *pfindData =
198 (FIND_FILE_STANDARD_INFO *)buf;
200 tmp_inode->i_mtime = cnvrtDosUnixTm(pfindData->LastWriteDate,
201 pfindData->LastWriteTime,
203 tmp_inode->i_atime = cnvrtDosUnixTm(pfindData->LastAccessDate,
204 pfindData->LastAccessTime,
206 tmp_inode->i_ctime = cnvrtDosUnixTm(pfindData->LastWriteDate,
207 pfindData->LastWriteTime,
209 attr = le16_to_cpu(pfindData->Attributes);
210 allocation_size = le32_to_cpu(pfindData->AllocationSize);
211 end_of_file = le32_to_cpu(pfindData->DataSize);
214 /* Linux can not store file creation time unfortunately so ignore it */
216 cifsInfo->cifsAttrs = attr;
217 #ifdef CONFIG_CIFS_EXPERIMENTAL
218 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
219 /* get more accurate mode via ACL - so force inode refresh */
222 #endif /* CONFIG_CIFS_EXPERIMENTAL */
223 cifsInfo->time = jiffies;
225 /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
226 /* 2767 perms - indicate mandatory locking */
227 /* BB fill in uid and gid here? with help from winbind?
228 or retrieve from NTFS stream extended attribute */
229 if (atomic_read(&cifsInfo->inUse) == 0) {
230 tmp_inode->i_uid = cifs_sb->mnt_uid;
231 tmp_inode->i_gid = cifs_sb->mnt_gid;
234 if (attr & ATTR_DIRECTORY)
235 default_mode = cifs_sb->mnt_dir_mode;
237 default_mode = cifs_sb->mnt_file_mode;
239 /* set initial permissions */
240 if ((atomic_read(&cifsInfo->inUse) == 0) ||
241 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
242 tmp_inode->i_mode = default_mode;
244 /* just reenable write bits if !ATTR_READONLY */
245 if ((tmp_inode->i_mode & S_IWUGO) == 0 &&
246 (attr & ATTR_READONLY) == 0)
247 tmp_inode->i_mode |= (S_IWUGO & default_mode);
249 tmp_inode->i_mode &= ~S_IFMT;
252 /* clear write bits if ATTR_READONLY is set */
253 if (attr & ATTR_READONLY)
254 tmp_inode->i_mode &= ~S_IWUGO;
257 if ((attr & ATTR_SYSTEM) &&
258 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
259 if (end_of_file == 0) {
260 tmp_inode->i_mode |= S_IFIFO;
261 *pobject_type = DT_FIFO;
264 * trying to get the type can be slow, so just call
265 * this a regular file for now, and mark for reval
267 tmp_inode->i_mode |= S_IFREG;
268 *pobject_type = DT_REG;
272 if (attr & ATTR_DIRECTORY) {
273 tmp_inode->i_mode |= S_IFDIR;
274 *pobject_type = DT_DIR;
276 tmp_inode->i_mode |= S_IFREG;
277 *pobject_type = DT_REG;
281 /* can not fill in nlink here as in qpathinfo version and Unx search */
282 if (atomic_read(&cifsInfo->inUse) == 0)
283 atomic_set(&cifsInfo->inUse, 1);
285 cifsInfo->server_eof = end_of_file;
286 spin_lock(&tmp_inode->i_lock);
287 if (is_size_safe_to_change(cifsInfo, end_of_file)) {
288 /* can not safely change the file size here if the
289 client is writing to it due to potential races */
290 i_size_write(tmp_inode, end_of_file);
292 /* 512 bytes (2**9) is the fake blocksize that must be used */
293 /* for this calculation, even though the reported blocksize is larger */
294 tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
296 spin_unlock(&tmp_inode->i_lock);
298 if (allocation_size < end_of_file)
299 cFYI(1, ("May be sparse file, allocation less than file size"));
300 cFYI(1, ("File Size %ld and blocks %llu",
301 (unsigned long)tmp_inode->i_size,
302 (unsigned long long)tmp_inode->i_blocks));
303 if (S_ISREG(tmp_inode->i_mode)) {
304 cFYI(1, ("File inode"));
305 tmp_inode->i_op = &cifs_file_inode_ops;
306 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
307 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
308 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
310 tmp_inode->i_fop = &cifs_file_direct_ops;
311 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
312 tmp_inode->i_fop = &cifs_file_nobrl_ops;
314 tmp_inode->i_fop = &cifs_file_ops;
316 if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
317 (cifs_sb->tcon->ses->server->maxBuf <
318 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
319 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
321 tmp_inode->i_data.a_ops = &cifs_addr_ops;
324 return; /* No sense invalidating pages for new inode
325 since have not started caching readahead file
328 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
329 (local_size == tmp_inode->i_size)) {
330 cFYI(1, ("inode exists but unchanged"));
332 /* file may have changed on server */
333 cFYI(1, ("invalidate inode, readdir detected change"));
334 invalidate_remote_inode(tmp_inode);
336 } else if (S_ISDIR(tmp_inode->i_mode)) {
337 cFYI(1, ("Directory inode"));
338 tmp_inode->i_op = &cifs_dir_inode_ops;
339 tmp_inode->i_fop = &cifs_dir_ops;
340 } else if (S_ISLNK(tmp_inode->i_mode)) {
341 cFYI(1, ("Symbolic Link inode"));
342 tmp_inode->i_op = &cifs_symlink_inode_ops;
344 cFYI(1, ("Init special inode"));
345 init_special_inode(tmp_inode, tmp_inode->i_mode,
350 /* BB eventually need to add the following helper function to
351 resolve NT_STATUS_STOPPED_ON_SYMLINK return code when
352 we try to do FindFirst on (NTFS) directory symlinks */
354 int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
361 struct cifsTconInfo *ptcon = cifs_sb->tcon;
364 rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ,
365 OPEN_REPARSE_POINT, &fid, &oplock, NULL,
367 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
369 tmpbuffer = kmalloc(maxpath);
370 rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path,
375 if (CIFSSMBClose(xid, ptcon, fid)) {
376 cFYI(1, ("Error closing temporary reparsepoint open)"));
382 static int initiate_cifs_search(const int xid, struct file *file)
386 struct cifsFileInfo *cifsFile;
387 struct cifs_sb_info *cifs_sb;
388 struct cifsTconInfo *pTcon;
390 if (file->private_data == NULL) {
392 kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
395 if (file->private_data == NULL)
397 cifsFile = file->private_data;
398 cifsFile->invalidHandle = true;
399 cifsFile->srch_inf.endOfSearch = false;
401 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
405 pTcon = cifs_sb->tcon;
409 full_path = build_path_from_dentry(file->f_path.dentry);
411 if (full_path == NULL)
414 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
417 /* test for Unix extensions */
418 /* but now check for them on the share/mount not on the SMB session */
419 /* if (pTcon->ses->capabilities & CAP_UNIX) { */
421 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
422 else if ((pTcon->ses->capabilities &
423 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
424 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
425 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
426 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
427 } else /* not srvinos - BB fixme add check for backlevel? */ {
428 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
431 rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
432 &cifsFile->netfid, &cifsFile->srch_inf,
433 cifs_sb->mnt_cifs_flags &
434 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
436 cifsFile->invalidHandle = false;
437 /* BB add following call to handle readdir on new NTFS symlink errors
438 else if STATUS_STOPPED_ON_SYMLINK
439 call get_symlink_reparse_path and retry with new path */
440 else if ((rc == -EOPNOTSUPP) &&
441 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
442 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
449 /* return length of unicode string in bytes */
450 static int cifs_unicode_bytelen(char *str)
453 __le16 *ustr = (__le16 *)str;
455 for (len = 0; len <= PATH_MAX; len++) {
459 cFYI(1, ("Unicode string longer than PATH_MAX found"));
463 static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
466 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
468 if (level == SMB_FIND_FILE_INFO_STANDARD) {
469 FIND_FILE_STANDARD_INFO *pfData;
470 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
472 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
473 pfData->FileNameLength;
475 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
476 cFYI(1, ("new entry %p old entry %p", new_entry, old_entry));
477 /* validate that new_entry is not past end of SMB */
478 if (new_entry >= end_of_smb) {
480 ("search entry %p began after end of SMB %p old entry %p",
481 new_entry, end_of_smb, old_entry));
483 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
484 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
485 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
486 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
487 cERROR(1, ("search entry %p extends after end of SMB %p",
488 new_entry, end_of_smb));
495 #define UNICODE_DOT cpu_to_le16(0x2e)
497 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
498 static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
501 char *filename = NULL;
504 if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
505 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
506 filename = &pFindData->FileName[0];
507 if (cfile->srch_inf.unicode) {
508 len = cifs_unicode_bytelen(filename);
510 /* BB should we make this strnlen of PATH_MAX? */
511 len = strnlen(filename, 5);
513 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
514 FILE_DIRECTORY_INFO *pFindData =
515 (FILE_DIRECTORY_INFO *)current_entry;
516 filename = &pFindData->FileName[0];
517 len = le32_to_cpu(pFindData->FileNameLength);
518 } else if (cfile->srch_inf.info_level ==
519 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
520 FILE_FULL_DIRECTORY_INFO *pFindData =
521 (FILE_FULL_DIRECTORY_INFO *)current_entry;
522 filename = &pFindData->FileName[0];
523 len = le32_to_cpu(pFindData->FileNameLength);
524 } else if (cfile->srch_inf.info_level ==
525 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
526 SEARCH_ID_FULL_DIR_INFO *pFindData =
527 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
528 filename = &pFindData->FileName[0];
529 len = le32_to_cpu(pFindData->FileNameLength);
530 } else if (cfile->srch_inf.info_level ==
531 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
532 FILE_BOTH_DIRECTORY_INFO *pFindData =
533 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
534 filename = &pFindData->FileName[0];
535 len = le32_to_cpu(pFindData->FileNameLength);
536 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
537 FIND_FILE_STANDARD_INFO *pFindData =
538 (FIND_FILE_STANDARD_INFO *)current_entry;
539 filename = &pFindData->FileName[0];
540 len = pFindData->FileNameLength;
542 cFYI(1, ("Unknown findfirst level %d",
543 cfile->srch_inf.info_level));
547 if (cfile->srch_inf.unicode) {
548 __le16 *ufilename = (__le16 *)filename;
551 if (ufilename[0] == UNICODE_DOT)
553 } else if (len == 4) {
555 if ((ufilename[0] == UNICODE_DOT)
556 && (ufilename[1] == UNICODE_DOT))
561 if (filename[0] == '.')
563 } else if (len == 2) {
564 if ((filename[0] == '.') && (filename[1] == '.'))
573 /* Check if directory that we are searching has changed so we can decide
574 whether we can use the cached search results from the previous search */
575 static int is_dir_changed(struct file *file)
577 struct inode *inode = file->f_path.dentry->d_inode;
578 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
580 if (cifsInfo->time == 0)
581 return 1; /* directory was changed, perhaps due to unlink */
587 static int cifs_save_resume_key(const char *current_entry,
588 struct cifsFileInfo *cifsFile)
591 unsigned int len = 0;
595 if ((cifsFile == NULL) || (current_entry == NULL))
598 level = cifsFile->srch_inf.info_level;
600 if (level == SMB_FIND_FILE_UNIX) {
601 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
603 filename = &pFindData->FileName[0];
604 if (cifsFile->srch_inf.unicode) {
605 len = cifs_unicode_bytelen(filename);
607 /* BB should we make this strnlen of PATH_MAX? */
608 len = strnlen(filename, PATH_MAX);
610 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
611 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
612 FILE_DIRECTORY_INFO *pFindData =
613 (FILE_DIRECTORY_INFO *)current_entry;
614 filename = &pFindData->FileName[0];
615 len = le32_to_cpu(pFindData->FileNameLength);
616 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
617 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
618 FILE_FULL_DIRECTORY_INFO *pFindData =
619 (FILE_FULL_DIRECTORY_INFO *)current_entry;
620 filename = &pFindData->FileName[0];
621 len = le32_to_cpu(pFindData->FileNameLength);
622 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
623 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
624 SEARCH_ID_FULL_DIR_INFO *pFindData =
625 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
626 filename = &pFindData->FileName[0];
627 len = le32_to_cpu(pFindData->FileNameLength);
628 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
629 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
630 FILE_BOTH_DIRECTORY_INFO *pFindData =
631 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
632 filename = &pFindData->FileName[0];
633 len = le32_to_cpu(pFindData->FileNameLength);
634 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
635 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
636 FIND_FILE_STANDARD_INFO *pFindData =
637 (FIND_FILE_STANDARD_INFO *)current_entry;
638 filename = &pFindData->FileName[0];
639 /* one byte length, no name conversion */
640 len = (unsigned int)pFindData->FileNameLength;
641 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
643 cFYI(1, ("Unknown findfirst level %d", level));
646 cifsFile->srch_inf.resume_name_len = len;
647 cifsFile->srch_inf.presume_name = filename;
651 /* find the corresponding entry in the search */
652 /* Note that the SMB server returns search entries for . and .. which
653 complicates logic here if we choose to parse for them and we do not
654 assume that they are located in the findfirst return buffer.*/
655 /* We start counting in the buffer with entry 2 and increment for every
656 entry (do not increment for . or .. entry) */
657 static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
658 struct file *file, char **ppCurrentEntry, int *num_to_ret)
662 loff_t first_entry_in_buffer;
663 loff_t index_to_find = file->f_pos;
664 struct cifsFileInfo *cifsFile = file->private_data;
665 /* check if index in the buffer */
667 if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
668 (num_to_ret == NULL))
671 *ppCurrentEntry = NULL;
672 first_entry_in_buffer =
673 cifsFile->srch_inf.index_of_last_entry -
674 cifsFile->srch_inf.entries_in_buffer;
676 /* if first entry in buf is zero then is first buffer
677 in search response data which means it is likely . and ..
678 will be in this buffer, although some servers do not return
679 . and .. for the root of a drive and for those we need
680 to start two entries earlier */
682 dump_cifs_file_struct(file, "In fce ");
683 if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
684 is_dir_changed(file)) ||
685 (index_to_find < first_entry_in_buffer)) {
686 /* close and restart search */
687 cFYI(1, ("search backing up - close and restart search"));
688 write_lock(&GlobalSMBSeslock);
689 if (!cifsFile->srch_inf.endOfSearch &&
690 !cifsFile->invalidHandle) {
691 cifsFile->invalidHandle = true;
692 write_unlock(&GlobalSMBSeslock);
693 CIFSFindClose(xid, pTcon, cifsFile->netfid);
695 write_unlock(&GlobalSMBSeslock);
696 if (cifsFile->srch_inf.ntwrk_buf_start) {
697 cFYI(1, ("freeing SMB ff cache buf on search rewind"));
698 if (cifsFile->srch_inf.smallBuf)
699 cifs_small_buf_release(cifsFile->srch_inf.
702 cifs_buf_release(cifsFile->srch_inf.
704 cifsFile->srch_inf.ntwrk_buf_start = NULL;
706 rc = initiate_cifs_search(xid, file);
708 cFYI(1, ("error %d reinitiating a search on rewind",
712 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
715 while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
716 (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
717 cFYI(1, ("calling findnext2"));
718 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
719 &cifsFile->srch_inf);
720 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
724 if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
725 /* we found the buffer that contains the entry */
726 /* scan and find it */
729 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
730 smbCalcSize((struct smb_hdr *)
731 cifsFile->srch_inf.ntwrk_buf_start);
733 current_entry = cifsFile->srch_inf.srch_entries_start;
734 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
735 - cifsFile->srch_inf.entries_in_buffer;
736 pos_in_buf = index_to_find - first_entry_in_buffer;
737 cFYI(1, ("found entry - pos_in_buf %d", pos_in_buf));
739 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
740 /* go entry by entry figuring out which is first */
741 current_entry = nxt_dir_entry(current_entry, end_of_smb,
742 cifsFile->srch_inf.info_level);
744 if ((current_entry == NULL) && (i < pos_in_buf)) {
745 /* BB fixme - check if we should flag this error */
746 cERROR(1, ("reached end of buf searching for pos in buf"
747 " %d index to find %lld rc %d",
748 pos_in_buf, index_to_find, rc));
751 *ppCurrentEntry = current_entry;
753 cFYI(1, ("index not in buffer - could not findnext into it"));
757 if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
758 cFYI(1, ("can not return entries pos_in_buf beyond last"));
761 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
766 /* inode num, inode type and filename returned */
767 static int cifs_get_name_from_search_buf(struct qstr *pqst,
768 char *current_entry, __u16 level, unsigned int unicode,
769 struct cifs_sb_info *cifs_sb, unsigned int max_len, __u64 *pinum)
772 unsigned int len = 0;
774 struct nls_table *nlt = cifs_sb->local_nls;
778 if (level == SMB_FIND_FILE_UNIX) {
779 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
781 filename = &pFindData->FileName[0];
783 len = cifs_unicode_bytelen(filename);
785 /* BB should we make this strnlen of PATH_MAX? */
786 len = strnlen(filename, PATH_MAX);
789 *pinum = le64_to_cpu(pFindData->basic.UniqueId);
790 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
791 FILE_DIRECTORY_INFO *pFindData =
792 (FILE_DIRECTORY_INFO *)current_entry;
793 filename = &pFindData->FileName[0];
794 len = le32_to_cpu(pFindData->FileNameLength);
795 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
796 FILE_FULL_DIRECTORY_INFO *pFindData =
797 (FILE_FULL_DIRECTORY_INFO *)current_entry;
798 filename = &pFindData->FileName[0];
799 len = le32_to_cpu(pFindData->FileNameLength);
800 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
801 SEARCH_ID_FULL_DIR_INFO *pFindData =
802 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
803 filename = &pFindData->FileName[0];
804 len = le32_to_cpu(pFindData->FileNameLength);
805 *pinum = le64_to_cpu(pFindData->UniqueId);
806 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
807 FILE_BOTH_DIRECTORY_INFO *pFindData =
808 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
809 filename = &pFindData->FileName[0];
810 len = le32_to_cpu(pFindData->FileNameLength);
811 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
812 FIND_FILE_STANDARD_INFO *pFindData =
813 (FIND_FILE_STANDARD_INFO *)current_entry;
814 filename = &pFindData->FileName[0];
815 /* one byte length, no name conversion */
816 len = (unsigned int)pFindData->FileNameLength;
818 cFYI(1, ("Unknown findfirst level %d", level));
823 cERROR(1, ("bad search response length %d past smb end", len));
828 pqst->len = cifs_from_ucs2((char *) pqst->name,
831 min(len, max_len), nlt,
832 cifs_sb->mnt_cifs_flags &
833 CIFS_MOUNT_MAP_SPECIAL_CHR);
835 pqst->name = filename;
838 pqst->hash = full_name_hash(pqst->name, pqst->len);
839 /* cFYI(1, ("filldir on %s",pqst->name)); */
843 static int cifs_filldir(char *pfindEntry, struct file *file, filldir_t filldir,
844 void *direntry, char *scratch_buf, unsigned int max_len)
848 struct cifsFileInfo *pCifsF;
849 unsigned int obj_type;
852 struct cifs_sb_info *cifs_sb;
853 struct inode *tmp_inode;
854 struct dentry *tmp_dentry;
855 struct cifs_fattr fattr;
857 /* get filename and len into qstring */
859 /* decide whether to create and populate ionde */
860 if ((direntry == NULL) || (file == NULL))
863 pCifsF = file->private_data;
865 if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
868 rc = cifs_entry_is_dot(pfindEntry, pCifsF);
869 /* skip . and .. since we added them first */
873 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
875 qstring.name = scratch_buf;
876 rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
877 pCifsF->srch_inf.info_level,
878 pCifsF->srch_inf.unicode, cifs_sb,
880 &inum /* returned */);
885 /* only these two infolevels return valid inode numbers */
886 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
887 cifs_unix_basic_to_fattr(&fattr,
888 &((FILE_UNIX_INFO *) pfindEntry)->basic,
890 tmp_dentry = cifs_readdir_lookup(file->f_dentry, &qstring,
892 obj_type = fattr.cf_dtype;
893 ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
895 if (pCifsF->srch_inf.info_level ==
896 SMB_FIND_FILE_ID_FULL_DIR_INFO)
897 rc = construct_dentry(&qstring, file, &tmp_inode,
900 rc = construct_dentry(&qstring, file, &tmp_inode,
903 if ((tmp_inode == NULL) || (tmp_dentry == NULL)) {
908 /* we pass in rc below, indicating whether it is a new inode,
909 * so we can figure out whether to invalidate the inode cached
910 * data if the file has changed
912 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
913 fill_in_inode(tmp_inode, 0, pfindEntry, &obj_type, rc);
915 fill_in_inode(tmp_inode, 1, pfindEntry, &obj_type, rc);
917 /* new inode - needs to be tied to dentry */
919 d_instantiate(tmp_dentry, tmp_inode);
921 d_rehash(tmp_dentry);
924 ino = cifs_uniqueid_to_ino_t(tmp_inode->i_ino);
927 rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
930 cFYI(1, ("filldir rc = %d", rc));
931 /* we can not return filldir errors to the caller
932 since they are "normal" when the stat blocksize
933 is too small - we return remapped error instead */
943 int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
947 struct cifs_sb_info *cifs_sb;
948 struct cifsTconInfo *pTcon;
949 struct cifsFileInfo *cifsFile = NULL;
952 char *tmp_buf = NULL;
954 unsigned int max_len;
958 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
959 pTcon = cifs_sb->tcon;
963 switch ((int) file->f_pos) {
965 if (filldir(direntry, ".", 1, file->f_pos,
966 file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
967 cERROR(1, ("Filldir for current dir failed"));
973 if (filldir(direntry, "..", 2, file->f_pos,
974 file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
975 cERROR(1, ("Filldir for parent dir failed"));
981 /* 1) If search is active,
982 is in current search buffer?
983 if it before then restart search
984 if after then keep searching till find it */
986 if (file->private_data == NULL) {
987 rc = initiate_cifs_search(xid, file);
988 cFYI(1, ("initiate cifs search rc %d", rc));
994 if (file->private_data == NULL) {
999 cifsFile = file->private_data;
1000 if (cifsFile->srch_inf.endOfSearch) {
1001 if (cifsFile->srch_inf.emptyDir) {
1002 cFYI(1, ("End of search, empty dir"));
1007 cifsFile->invalidHandle = true;
1008 CIFSFindClose(xid, pTcon, cifsFile->netfid);
1011 rc = find_cifs_entry(xid, pTcon, file,
1012 ¤t_entry, &num_to_fill);
1014 cFYI(1, ("fce error %d", rc));
1016 } else if (current_entry != NULL) {
1017 cFYI(1, ("entry %lld found", file->f_pos));
1019 cFYI(1, ("could not find entry"));
1022 cFYI(1, ("loop through %d times filling dir for net buf %p",
1023 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start));
1024 max_len = smbCalcSize((struct smb_hdr *)
1025 cifsFile->srch_inf.ntwrk_buf_start);
1026 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1028 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
1029 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
1030 if (current_entry == NULL) {
1031 /* evaluate whether this case is an error */
1032 cERROR(1, ("past SMB end, num to fill %d i %d",
1036 /* if buggy server returns . and .. late do
1037 we want to check for that here? */
1038 rc = cifs_filldir(current_entry, file,
1039 filldir, direntry, tmp_buf, max_len);
1040 if (rc == -EOVERFLOW) {
1047 cifsFile->srch_inf.index_of_last_entry) {
1048 cFYI(1, ("last entry in buf at pos %lld %s",
1049 file->f_pos, tmp_buf));
1050 cifs_save_resume_key(current_entry, cifsFile);
1054 nxt_dir_entry(current_entry, end_of_smb,
1055 cifsFile->srch_inf.info_level);