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/slab.h>
26 #include <linux/stat.h>
29 #include "cifsproto.h"
30 #include "cifs_unicode.h"
31 #include "cifs_debug.h"
32 #include "cifs_fs_sb.h"
36 * To be safe - for UCS to UTF-8 with strings loaded with the rare long
37 * characters alloc more to account for such multibyte target UTF-8
40 #define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
42 #ifdef CONFIG_CIFS_DEBUG2
43 static void dump_cifs_file_struct(struct file *file, char *label)
45 struct cifsFileInfo *cf;
48 cf = file->private_data;
50 cFYI(1, "empty cifs private file data");
53 if (cf->invalidHandle)
54 cFYI(1, "invalid handle");
55 if (cf->srch_inf.endOfSearch)
56 cFYI(1, "end of search");
57 if (cf->srch_inf.emptyDir)
62 static inline void dump_cifs_file_struct(struct file *file, char *label)
68 * Find the dentry that matches "name". If there isn't one, create one. If it's
69 * a negative dentry or the uniqueid changed, then drop it and recreate it.
71 static struct dentry *
72 cifs_readdir_lookup(struct dentry *parent, struct qstr *name,
73 struct cifs_fattr *fattr)
75 struct dentry *dentry, *alias;
77 struct super_block *sb = parent->d_inode->i_sb;
79 cFYI(1, "For %s", name->name);
81 if (parent->d_op && parent->d_op->d_hash)
82 parent->d_op->d_hash(parent, parent->d_inode, name);
84 name->hash = full_name_hash(name->name, name->len);
86 dentry = d_lookup(parent, name);
88 /* FIXME: check for inode number changes? */
89 if (dentry->d_inode != NULL)
95 dentry = d_alloc(parent, name);
99 inode = cifs_iget(sb, fattr);
105 if (cifs_sb_master_tcon(CIFS_SB(sb))->nocase)
106 d_set_d_op(dentry, &cifs_ci_dentry_ops);
108 d_set_d_op(dentry, &cifs_dentry_ops);
110 alias = d_materialise_unique(dentry, inode);
122 cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
124 fattr->cf_uid = cifs_sb->mnt_uid;
125 fattr->cf_gid = cifs_sb->mnt_gid;
127 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
128 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
129 fattr->cf_dtype = DT_DIR;
131 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
132 fattr->cf_dtype = DT_REG;
135 if (fattr->cf_cifsattrs & ATTR_READONLY)
136 fattr->cf_mode &= ~S_IWUGO;
138 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
139 fattr->cf_cifsattrs & ATTR_SYSTEM) {
140 if (fattr->cf_eof == 0) {
141 fattr->cf_mode &= ~S_IFMT;
142 fattr->cf_mode |= S_IFIFO;
143 fattr->cf_dtype = DT_FIFO;
146 * trying to get the type and mode via SFU can be slow,
147 * so just call those regular files for now, and mark
150 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
156 cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info,
157 struct cifs_sb_info *cifs_sb)
159 memset(fattr, 0, sizeof(*fattr));
160 fattr->cf_cifsattrs = le32_to_cpu(info->ExtFileAttributes);
161 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
162 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
163 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
164 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
165 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
166 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
168 cifs_fill_common_info(fattr, cifs_sb);
172 cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info,
173 struct cifs_sb_info *cifs_sb)
175 int offset = cifs_sb_master_tcon(cifs_sb)->ses->server->timeAdj;
177 memset(fattr, 0, sizeof(*fattr));
178 fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate,
179 info->LastAccessTime, offset);
180 fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate,
181 info->LastWriteTime, offset);
182 fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate,
183 info->LastWriteTime, offset);
185 fattr->cf_cifsattrs = le16_to_cpu(info->Attributes);
186 fattr->cf_bytes = le32_to_cpu(info->AllocationSize);
187 fattr->cf_eof = le32_to_cpu(info->DataSize);
189 cifs_fill_common_info(fattr, cifs_sb);
192 /* BB eventually need to add the following helper function to
193 resolve NT_STATUS_STOPPED_ON_SYMLINK return code when
194 we try to do FindFirst on (NTFS) directory symlinks */
196 int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
203 struct cifsTconInfo *ptcon = cifs_sb_tcon(cifs_sb);
206 rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ,
207 OPEN_REPARSE_POINT, &fid, &oplock, NULL,
209 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
211 tmpbuffer = kmalloc(maxpath);
212 rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path,
217 if (CIFSSMBClose(xid, ptcon, fid)) {
218 cFYI(1, "Error closing temporary reparsepoint open");
224 static int initiate_cifs_search(const int xid, struct file *file)
227 char *full_path = NULL;
228 struct cifsFileInfo *cifsFile;
229 struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
230 struct tcon_link *tlink = NULL;
231 struct cifsTconInfo *pTcon;
233 if (file->private_data == NULL) {
234 tlink = cifs_sb_tlink(cifs_sb);
236 return PTR_ERR(tlink);
238 cifsFile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
239 if (cifsFile == NULL) {
243 file->private_data = cifsFile;
244 cifsFile->tlink = cifs_get_tlink(tlink);
245 pTcon = tlink_tcon(tlink);
247 cifsFile = file->private_data;
248 pTcon = tlink_tcon(cifsFile->tlink);
251 cifsFile->invalidHandle = true;
252 cifsFile->srch_inf.endOfSearch = false;
254 full_path = build_path_from_dentry(file->f_path.dentry);
255 if (full_path == NULL) {
260 cFYI(1, "Full path: %s start at: %lld", full_path, file->f_pos);
263 /* test for Unix extensions */
264 /* but now check for them on the share/mount not on the SMB session */
265 /* if (pTcon->ses->capabilities & CAP_UNIX) { */
267 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
268 else if ((pTcon->ses->capabilities &
269 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
270 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
271 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
272 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
273 } else /* not srvinos - BB fixme add check for backlevel? */ {
274 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
277 rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
278 &cifsFile->netfid, &cifsFile->srch_inf,
279 cifs_sb->mnt_cifs_flags &
280 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
282 cifsFile->invalidHandle = false;
283 /* BB add following call to handle readdir on new NTFS symlink errors
284 else if STATUS_STOPPED_ON_SYMLINK
285 call get_symlink_reparse_path and retry with new path */
286 else if ((rc == -EOPNOTSUPP) &&
287 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
288 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
293 cifs_put_tlink(tlink);
297 /* return length of unicode string in bytes */
298 static int cifs_unicode_bytelen(char *str)
301 __le16 *ustr = (__le16 *)str;
303 for (len = 0; len <= PATH_MAX; len++) {
307 cFYI(1, "Unicode string longer than PATH_MAX found");
311 static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
314 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
316 if (level == SMB_FIND_FILE_INFO_STANDARD) {
317 FIND_FILE_STANDARD_INFO *pfData;
318 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
320 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
321 pfData->FileNameLength;
323 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
324 cFYI(1, "new entry %p old entry %p", new_entry, old_entry);
325 /* validate that new_entry is not past end of SMB */
326 if (new_entry >= end_of_smb) {
327 cERROR(1, "search entry %p began after end of SMB %p old entry %p",
328 new_entry, end_of_smb, old_entry);
330 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
331 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
332 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
333 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
334 cERROR(1, "search entry %p extends after end of SMB %p",
335 new_entry, end_of_smb);
342 #define UNICODE_DOT cpu_to_le16(0x2e)
344 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
345 static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
348 char *filename = NULL;
351 if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
352 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
353 filename = &pFindData->FileName[0];
354 if (cfile->srch_inf.unicode) {
355 len = cifs_unicode_bytelen(filename);
357 /* BB should we make this strnlen of PATH_MAX? */
358 len = strnlen(filename, 5);
360 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
361 FILE_DIRECTORY_INFO *pFindData =
362 (FILE_DIRECTORY_INFO *)current_entry;
363 filename = &pFindData->FileName[0];
364 len = le32_to_cpu(pFindData->FileNameLength);
365 } else if (cfile->srch_inf.info_level ==
366 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
367 FILE_FULL_DIRECTORY_INFO *pFindData =
368 (FILE_FULL_DIRECTORY_INFO *)current_entry;
369 filename = &pFindData->FileName[0];
370 len = le32_to_cpu(pFindData->FileNameLength);
371 } else if (cfile->srch_inf.info_level ==
372 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
373 SEARCH_ID_FULL_DIR_INFO *pFindData =
374 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
375 filename = &pFindData->FileName[0];
376 len = le32_to_cpu(pFindData->FileNameLength);
377 } else if (cfile->srch_inf.info_level ==
378 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
379 FILE_BOTH_DIRECTORY_INFO *pFindData =
380 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
381 filename = &pFindData->FileName[0];
382 len = le32_to_cpu(pFindData->FileNameLength);
383 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
384 FIND_FILE_STANDARD_INFO *pFindData =
385 (FIND_FILE_STANDARD_INFO *)current_entry;
386 filename = &pFindData->FileName[0];
387 len = pFindData->FileNameLength;
389 cFYI(1, "Unknown findfirst level %d",
390 cfile->srch_inf.info_level);
394 if (cfile->srch_inf.unicode) {
395 __le16 *ufilename = (__le16 *)filename;
398 if (ufilename[0] == UNICODE_DOT)
400 } else if (len == 4) {
402 if ((ufilename[0] == UNICODE_DOT)
403 && (ufilename[1] == UNICODE_DOT))
408 if (filename[0] == '.')
410 } else if (len == 2) {
411 if ((filename[0] == '.') && (filename[1] == '.'))
420 /* Check if directory that we are searching has changed so we can decide
421 whether we can use the cached search results from the previous search */
422 static int is_dir_changed(struct file *file)
424 struct inode *inode = file->f_path.dentry->d_inode;
425 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
427 if (cifsInfo->time == 0)
428 return 1; /* directory was changed, perhaps due to unlink */
434 static int cifs_save_resume_key(const char *current_entry,
435 struct cifsFileInfo *cifsFile)
438 unsigned int len = 0;
442 if ((cifsFile == NULL) || (current_entry == NULL))
445 level = cifsFile->srch_inf.info_level;
447 if (level == SMB_FIND_FILE_UNIX) {
448 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
450 filename = &pFindData->FileName[0];
451 if (cifsFile->srch_inf.unicode) {
452 len = cifs_unicode_bytelen(filename);
454 /* BB should we make this strnlen of PATH_MAX? */
455 len = strnlen(filename, PATH_MAX);
457 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
458 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
459 FILE_DIRECTORY_INFO *pFindData =
460 (FILE_DIRECTORY_INFO *)current_entry;
461 filename = &pFindData->FileName[0];
462 len = le32_to_cpu(pFindData->FileNameLength);
463 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
464 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
465 FILE_FULL_DIRECTORY_INFO *pFindData =
466 (FILE_FULL_DIRECTORY_INFO *)current_entry;
467 filename = &pFindData->FileName[0];
468 len = le32_to_cpu(pFindData->FileNameLength);
469 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
470 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
471 SEARCH_ID_FULL_DIR_INFO *pFindData =
472 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
473 filename = &pFindData->FileName[0];
474 len = le32_to_cpu(pFindData->FileNameLength);
475 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
476 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
477 FILE_BOTH_DIRECTORY_INFO *pFindData =
478 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
479 filename = &pFindData->FileName[0];
480 len = le32_to_cpu(pFindData->FileNameLength);
481 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
482 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
483 FIND_FILE_STANDARD_INFO *pFindData =
484 (FIND_FILE_STANDARD_INFO *)current_entry;
485 filename = &pFindData->FileName[0];
486 /* one byte length, no name conversion */
487 len = (unsigned int)pFindData->FileNameLength;
488 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
490 cFYI(1, "Unknown findfirst level %d", level);
493 cifsFile->srch_inf.resume_name_len = len;
494 cifsFile->srch_inf.presume_name = filename;
498 /* find the corresponding entry in the search */
499 /* Note that the SMB server returns search entries for . and .. which
500 complicates logic here if we choose to parse for them and we do not
501 assume that they are located in the findfirst return buffer.*/
502 /* We start counting in the buffer with entry 2 and increment for every
503 entry (do not increment for . or .. entry) */
504 static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
505 struct file *file, char **ppCurrentEntry, int *num_to_ret)
509 loff_t first_entry_in_buffer;
510 loff_t index_to_find = file->f_pos;
511 struct cifsFileInfo *cifsFile = file->private_data;
512 /* check if index in the buffer */
514 if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
515 (num_to_ret == NULL))
518 *ppCurrentEntry = NULL;
519 first_entry_in_buffer =
520 cifsFile->srch_inf.index_of_last_entry -
521 cifsFile->srch_inf.entries_in_buffer;
523 /* if first entry in buf is zero then is first buffer
524 in search response data which means it is likely . and ..
525 will be in this buffer, although some servers do not return
526 . and .. for the root of a drive and for those we need
527 to start two entries earlier */
529 dump_cifs_file_struct(file, "In fce ");
530 if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
531 is_dir_changed(file)) ||
532 (index_to_find < first_entry_in_buffer)) {
533 /* close and restart search */
534 cFYI(1, "search backing up - close and restart search");
535 spin_lock(&cifs_file_list_lock);
536 if (!cifsFile->srch_inf.endOfSearch &&
537 !cifsFile->invalidHandle) {
538 cifsFile->invalidHandle = true;
539 spin_unlock(&cifs_file_list_lock);
540 CIFSFindClose(xid, pTcon, cifsFile->netfid);
542 spin_unlock(&cifs_file_list_lock);
543 if (cifsFile->srch_inf.ntwrk_buf_start) {
544 cFYI(1, "freeing SMB ff cache buf on search rewind");
545 if (cifsFile->srch_inf.smallBuf)
546 cifs_small_buf_release(cifsFile->srch_inf.
549 cifs_buf_release(cifsFile->srch_inf.
551 cifsFile->srch_inf.ntwrk_buf_start = NULL;
553 rc = initiate_cifs_search(xid, file);
555 cFYI(1, "error %d reinitiating a search on rewind",
559 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
562 while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
563 (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
564 cFYI(1, "calling findnext2");
565 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
566 &cifsFile->srch_inf);
567 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
571 if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
572 /* we found the buffer that contains the entry */
573 /* scan and find it */
576 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
577 smbCalcSize((struct smb_hdr *)
578 cifsFile->srch_inf.ntwrk_buf_start);
580 current_entry = cifsFile->srch_inf.srch_entries_start;
581 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
582 - cifsFile->srch_inf.entries_in_buffer;
583 pos_in_buf = index_to_find - first_entry_in_buffer;
584 cFYI(1, "found entry - pos_in_buf %d", pos_in_buf);
586 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
587 /* go entry by entry figuring out which is first */
588 current_entry = nxt_dir_entry(current_entry, end_of_smb,
589 cifsFile->srch_inf.info_level);
591 if ((current_entry == NULL) && (i < pos_in_buf)) {
592 /* BB fixme - check if we should flag this error */
593 cERROR(1, "reached end of buf searching for pos in buf"
594 " %d index to find %lld rc %d",
595 pos_in_buf, index_to_find, rc);
598 *ppCurrentEntry = current_entry;
600 cFYI(1, "index not in buffer - could not findnext into it");
604 if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
605 cFYI(1, "can not return entries pos_in_buf beyond last");
608 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
613 /* inode num, inode type and filename returned */
614 static int cifs_get_name_from_search_buf(struct qstr *pqst,
615 char *current_entry, __u16 level, unsigned int unicode,
616 struct cifs_sb_info *cifs_sb, unsigned int max_len, __u64 *pinum)
619 unsigned int len = 0;
621 struct nls_table *nlt = cifs_sb->local_nls;
625 if (level == SMB_FIND_FILE_UNIX) {
626 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
628 filename = &pFindData->FileName[0];
630 len = cifs_unicode_bytelen(filename);
632 /* BB should we make this strnlen of PATH_MAX? */
633 len = strnlen(filename, PATH_MAX);
636 *pinum = le64_to_cpu(pFindData->basic.UniqueId);
637 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
638 FILE_DIRECTORY_INFO *pFindData =
639 (FILE_DIRECTORY_INFO *)current_entry;
640 filename = &pFindData->FileName[0];
641 len = le32_to_cpu(pFindData->FileNameLength);
642 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
643 FILE_FULL_DIRECTORY_INFO *pFindData =
644 (FILE_FULL_DIRECTORY_INFO *)current_entry;
645 filename = &pFindData->FileName[0];
646 len = le32_to_cpu(pFindData->FileNameLength);
647 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
648 SEARCH_ID_FULL_DIR_INFO *pFindData =
649 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
650 filename = &pFindData->FileName[0];
651 len = le32_to_cpu(pFindData->FileNameLength);
652 *pinum = le64_to_cpu(pFindData->UniqueId);
653 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
654 FILE_BOTH_DIRECTORY_INFO *pFindData =
655 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
656 filename = &pFindData->FileName[0];
657 len = le32_to_cpu(pFindData->FileNameLength);
658 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
659 FIND_FILE_STANDARD_INFO *pFindData =
660 (FIND_FILE_STANDARD_INFO *)current_entry;
661 filename = &pFindData->FileName[0];
662 /* one byte length, no name conversion */
663 len = (unsigned int)pFindData->FileNameLength;
665 cFYI(1, "Unknown findfirst level %d", level);
670 cERROR(1, "bad search response length %d past smb end", len);
675 pqst->len = cifs_from_ucs2((char *) pqst->name,
678 min(len, max_len), nlt,
679 cifs_sb->mnt_cifs_flags &
680 CIFS_MOUNT_MAP_SPECIAL_CHR);
681 pqst->len -= nls_nullsize(nlt);
683 pqst->name = filename;
689 static int cifs_filldir(char *pfindEntry, struct file *file, filldir_t filldir,
690 void *direntry, char *scratch_buf, unsigned int max_len)
694 struct cifsFileInfo *pCifsF;
697 struct super_block *sb;
698 struct cifs_sb_info *cifs_sb;
699 struct dentry *tmp_dentry;
700 struct cifs_fattr fattr;
702 /* get filename and len into qstring */
704 /* decide whether to create and populate ionde */
705 if ((direntry == NULL) || (file == NULL))
708 pCifsF = file->private_data;
710 if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
713 rc = cifs_entry_is_dot(pfindEntry, pCifsF);
714 /* skip . and .. since we added them first */
718 sb = file->f_path.dentry->d_sb;
719 cifs_sb = CIFS_SB(sb);
721 qstring.name = scratch_buf;
722 rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
723 pCifsF->srch_inf.info_level,
724 pCifsF->srch_inf.unicode, cifs_sb,
725 max_len, &inum /* returned */);
730 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
731 cifs_unix_basic_to_fattr(&fattr,
732 &((FILE_UNIX_INFO *) pfindEntry)->basic,
734 else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
735 cifs_std_info_to_fattr(&fattr, (FIND_FILE_STANDARD_INFO *)
736 pfindEntry, cifs_sb);
738 cifs_dir_info_to_fattr(&fattr, (FILE_DIRECTORY_INFO *)
739 pfindEntry, cifs_sb);
741 if (inum && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
742 fattr.cf_uniqueid = inum;
744 fattr.cf_uniqueid = iunique(sb, ROOT_I);
745 cifs_autodisable_serverino(cifs_sb);
748 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) &&
749 CIFSCouldBeMFSymlink(&fattr))
751 * trying to get the type and mode can be slow,
752 * so just call those regular files for now, and mark
755 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
757 ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
758 tmp_dentry = cifs_readdir_lookup(file->f_dentry, &qstring, &fattr);
760 rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
761 ino, fattr.cf_dtype);
768 int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
772 struct cifs_sb_info *cifs_sb;
773 struct cifsTconInfo *pTcon;
774 struct cifsFileInfo *cifsFile = NULL;
777 char *tmp_buf = NULL;
779 unsigned int max_len;
783 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
786 * Ensure FindFirst doesn't fail before doing filldir() for '.' and
787 * '..'. Otherwise we won't be able to notify VFS in case of failure.
789 if (file->private_data == NULL) {
790 rc = initiate_cifs_search(xid, file);
791 cFYI(1, "initiate cifs search rc %d", rc);
796 switch ((int) file->f_pos) {
798 if (filldir(direntry, ".", 1, file->f_pos,
799 file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
800 cERROR(1, "Filldir for current dir failed");
806 if (filldir(direntry, "..", 2, file->f_pos,
807 file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
808 cERROR(1, "Filldir for parent dir failed");
814 /* 1) If search is active,
815 is in current search buffer?
816 if it before then restart search
817 if after then keep searching till find it */
819 if (file->private_data == NULL) {
824 cifsFile = file->private_data;
825 if (cifsFile->srch_inf.endOfSearch) {
826 if (cifsFile->srch_inf.emptyDir) {
827 cFYI(1, "End of search, empty dir");
832 cifsFile->invalidHandle = true;
833 CIFSFindClose(xid, pTcon, cifsFile->netfid);
836 pTcon = tlink_tcon(cifsFile->tlink);
837 rc = find_cifs_entry(xid, pTcon, file,
838 ¤t_entry, &num_to_fill);
840 cFYI(1, "fce error %d", rc);
842 } else if (current_entry != NULL) {
843 cFYI(1, "entry %lld found", file->f_pos);
845 cFYI(1, "could not find entry");
848 cFYI(1, "loop through %d times filling dir for net buf %p",
849 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start);
850 max_len = smbCalcSize((struct smb_hdr *)
851 cifsFile->srch_inf.ntwrk_buf_start);
852 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
854 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
855 if (tmp_buf == NULL) {
860 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
861 if (current_entry == NULL) {
862 /* evaluate whether this case is an error */
863 cERROR(1, "past SMB end, num to fill %d i %d",
867 /* if buggy server returns . and .. late do
868 we want to check for that here? */
869 rc = cifs_filldir(current_entry, file,
870 filldir, direntry, tmp_buf, max_len);
871 if (rc == -EOVERFLOW) {
878 cifsFile->srch_inf.index_of_last_entry) {
879 cFYI(1, "last entry in buf at pos %lld %s",
880 file->f_pos, tmp_buf);
881 cifs_save_resume_key(current_entry, cifsFile);
885 nxt_dir_entry(current_entry, end_of_smb,
886 cifsFile->srch_inf.info_level);