1 // SPDX-License-Identifier: LGPL-2.1
4 * Directory search handling
6 * Copyright (C) International Business Machines Corp., 2004, 2008
7 * Copyright (C) Red Hat, Inc., 2011
12 #include <linux/pagemap.h>
13 #include <linux/slab.h>
14 #include <linux/stat.h>
17 #include "cifsproto.h"
18 #include "cifs_unicode.h"
19 #include "cifs_debug.h"
20 #include "cifs_fs_sb.h"
22 #include "smb2proto.h"
23 #include "fs_context.h"
24 #include "cached_dir.h"
28 * To be safe - for UCS to UTF-8 with strings loaded with the rare long
29 * characters alloc more to account for such multibyte target UTF-8
32 #define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
34 #ifdef CONFIG_CIFS_DEBUG2
35 static void dump_cifs_file_struct(struct file *file, char *label)
37 struct cifsFileInfo *cf;
40 cf = file->private_data;
42 cifs_dbg(FYI, "empty cifs private file data\n");
45 if (cf->invalidHandle)
46 cifs_dbg(FYI, "Invalid handle\n");
47 if (cf->srch_inf.endOfSearch)
48 cifs_dbg(FYI, "end of search\n");
49 if (cf->srch_inf.emptyDir)
50 cifs_dbg(FYI, "empty dir\n");
54 static inline void dump_cifs_file_struct(struct file *file, char *label)
60 * Attempt to preload the dcache with the results from the FIND_FIRST/NEXT
62 * Find the dentry that matches "name". If there isn't one, create one. If it's
63 * a negative dentry or the uniqueid or filetype(mode) changed,
64 * then drop it and recreate it.
67 cifs_prime_dcache(struct dentry *parent, struct qstr *name,
68 struct cifs_fattr *fattr)
70 struct dentry *dentry, *alias;
72 struct super_block *sb = parent->d_sb;
73 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
74 bool posix = cifs_sb_master_tcon(cifs_sb)->posix_extensions;
75 bool reparse_need_reval = false;
76 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
79 cifs_dbg(FYI, "%s: for %s\n", __func__, name->name);
81 dentry = d_hash_and_lookup(parent, name);
84 * If we know that the inode will need to be revalidated
85 * immediately, then don't create a new dentry for it.
86 * We'll end up doing an on the wire call either way and
87 * this spares us an invalidation.
91 switch (fattr->cf_mode & S_IFMT) {
95 reparse_need_reval = true;
100 } else if (fattr->cf_cifsattrs & ATTR_REPARSE) {
101 reparse_need_reval = true;
104 if (reparse_need_reval ||
105 (fattr->cf_flags & CIFS_FATTR_NEED_REVAL))
108 dentry = d_alloc_parallel(parent, name, &wq);
112 if (!d_in_lookup(dentry)) {
113 inode = d_inode(dentry);
115 if (d_mountpoint(dentry)) {
120 * If we're generating inode numbers, then we don't
121 * want to clobber the existing one with the one that
122 * the readdir code created.
124 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM))
125 fattr->cf_uniqueid = CIFS_I(inode)->uniqueid;
128 * Update inode in place if both i_ino and i_mode didn't
131 if (CIFS_I(inode)->uniqueid == fattr->cf_uniqueid) {
133 * Query dir responses don't provide enough
134 * information about reparse points other than
135 * their reparse tags. Save an invalidation by
136 * not clobbering some existing attributes when
137 * reparse tag and ctime haven't changed.
140 if (fattr->cf_cifsattrs & ATTR_REPARSE) {
141 if (likely(reparse_inode_match(inode, fattr))) {
142 fattr->cf_mode = inode->i_mode;
143 fattr->cf_rdev = inode->i_rdev;
144 fattr->cf_uid = inode->i_uid;
145 fattr->cf_gid = inode->i_gid;
146 fattr->cf_eof = CIFS_I(inode)->netfs.remote_i_size;
147 fattr->cf_symlink_target = NULL;
149 CIFS_I(inode)->time = 0;
153 if (!rc && !cifs_fattr_to_inode(inode, fattr, true)) {
159 d_invalidate(dentry);
163 inode = cifs_iget(sb, fattr);
165 inode = ERR_PTR(-ENOMEM);
166 alias = d_splice_alias(inode, dentry);
167 d_lookup_done(dentry);
168 if (alias && !IS_ERR(alias))
175 cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
177 struct cifs_open_info_data data = {
178 .reparse = { .tag = fattr->cf_cifstag, },
181 fattr->cf_uid = cifs_sb->ctx->linux_uid;
182 fattr->cf_gid = cifs_sb->ctx->linux_gid;
185 * The IO_REPARSE_TAG_LX_ tags originally were used by WSL but they
186 * are preferred by the Linux client in some cases since, unlike
187 * the NFS reparse tag (or EAs), they don't require an extra query
188 * to determine which type of special file they represent.
189 * TODO: go through all documented reparse tags to see if we can
190 * reasonably map some of them to directories vs. files vs. symlinks
192 if ((fattr->cf_cifsattrs & ATTR_REPARSE) &&
193 cifs_reparse_point_to_fattr(cifs_sb, fattr, &data))
196 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
197 fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
198 fattr->cf_dtype = DT_DIR;
200 fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
201 fattr->cf_dtype = DT_REG;
205 /* non-unix readdir doesn't provide nlink */
206 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
208 if (fattr->cf_cifsattrs & ATTR_READONLY)
209 fattr->cf_mode &= ~S_IWUGO;
212 * We of course don't get ACL info in FIND_FIRST/NEXT results, so
213 * mark it for revalidation so that "ls -l" will look right. It might
214 * be super-slow, but if we don't do this then the ownership of files
215 * may look wrong since the inodes may not have timed out by the time
216 * "ls" does a stat() call on them.
218 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
219 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID))
220 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
222 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
223 fattr->cf_cifsattrs & ATTR_SYSTEM) {
224 if (fattr->cf_eof == 0) {
225 fattr->cf_mode &= ~S_IFMT;
226 fattr->cf_mode |= S_IFIFO;
227 fattr->cf_dtype = DT_FIFO;
230 * trying to get the type and mode via SFU can be slow,
231 * so just call those regular files for now, and mark
234 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
239 /* Fill a cifs_fattr struct with info from SMB_FIND_FILE_POSIX_INFO. */
241 cifs_posix_to_fattr(struct cifs_fattr *fattr, struct smb2_posix_info *info,
242 struct cifs_sb_info *cifs_sb)
244 struct smb2_posix_info_parsed parsed;
246 posix_info_parse(info, NULL, &parsed);
248 memset(fattr, 0, sizeof(*fattr));
249 fattr->cf_uniqueid = le64_to_cpu(info->Inode);
250 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
251 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
253 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
254 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
255 fattr->cf_ctime = cifs_NTtimeToUnix(info->CreationTime);
257 fattr->cf_nlink = le32_to_cpu(info->HardLinks);
258 fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
260 if (fattr->cf_cifsattrs & ATTR_REPARSE)
261 fattr->cf_cifstag = le32_to_cpu(info->ReparseTag);
263 /* The Mode field in the response can now include the file type as well */
264 fattr->cf_mode = wire_mode_to_posix(le32_to_cpu(info->Mode),
265 fattr->cf_cifsattrs & ATTR_DIRECTORY);
266 fattr->cf_dtype = S_DT(le32_to_cpu(info->Mode));
268 switch (fattr->cf_mode & S_IFMT) {
272 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
278 cifs_dbg(FYI, "posix fattr: dev %d, reparse %d, mode %o\n",
279 le32_to_cpu(info->DeviceId),
280 le32_to_cpu(info->ReparseTag),
281 le32_to_cpu(info->Mode));
283 sid_to_id(cifs_sb, &parsed.owner, fattr, SIDOWNER);
284 sid_to_id(cifs_sb, &parsed.group, fattr, SIDGROUP);
287 static void __dir_info_to_fattr(struct cifs_fattr *fattr, const void *info)
289 const FILE_DIRECTORY_INFO *fi = info;
291 memset(fattr, 0, sizeof(*fattr));
292 fattr->cf_cifsattrs = le32_to_cpu(fi->ExtFileAttributes);
293 fattr->cf_eof = le64_to_cpu(fi->EndOfFile);
294 fattr->cf_bytes = le64_to_cpu(fi->AllocationSize);
295 fattr->cf_createtime = le64_to_cpu(fi->CreationTime);
296 fattr->cf_atime = cifs_NTtimeToUnix(fi->LastAccessTime);
297 fattr->cf_ctime = cifs_NTtimeToUnix(fi->ChangeTime);
298 fattr->cf_mtime = cifs_NTtimeToUnix(fi->LastWriteTime);
302 cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info,
303 struct cifs_sb_info *cifs_sb)
305 __dir_info_to_fattr(fattr, info);
306 cifs_fill_common_info(fattr, cifs_sb);
309 static void cifs_fulldir_info_to_fattr(struct cifs_fattr *fattr,
311 struct cifs_sb_info *cifs_sb)
313 const FILE_FULL_DIRECTORY_INFO *di = info;
315 __dir_info_to_fattr(fattr, info);
317 /* See MS-FSCC 2.4.14, 2.4.19 */
318 if (fattr->cf_cifsattrs & ATTR_REPARSE)
319 fattr->cf_cifstag = le32_to_cpu(di->EaSize);
320 cifs_fill_common_info(fattr, cifs_sb);
324 cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info,
325 struct cifs_sb_info *cifs_sb)
327 int offset = cifs_sb_master_tcon(cifs_sb)->ses->server->timeAdj;
329 memset(fattr, 0, sizeof(*fattr));
330 fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate,
331 info->LastAccessTime, offset);
332 fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate,
333 info->LastWriteTime, offset);
334 fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate,
335 info->LastWriteTime, offset);
337 fattr->cf_cifsattrs = le16_to_cpu(info->Attributes);
338 fattr->cf_bytes = le32_to_cpu(info->AllocationSize);
339 fattr->cf_eof = le32_to_cpu(info->DataSize);
341 cifs_fill_common_info(fattr, cifs_sb);
345 _initiate_cifs_search(const unsigned int xid, struct file *file,
346 const char *full_path)
350 struct cifsFileInfo *cifsFile;
351 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
352 struct tcon_link *tlink = NULL;
353 struct cifs_tcon *tcon;
354 struct TCP_Server_Info *server;
356 if (file->private_data == NULL) {
357 tlink = cifs_sb_tlink(cifs_sb);
359 return PTR_ERR(tlink);
361 cifsFile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
362 if (cifsFile == NULL) {
366 spin_lock_init(&cifsFile->file_info_lock);
367 file->private_data = cifsFile;
368 cifsFile->tlink = cifs_get_tlink(tlink);
369 tcon = tlink_tcon(tlink);
371 cifsFile = file->private_data;
372 tcon = tlink_tcon(cifsFile->tlink);
375 server = tcon->ses->server;
377 if (!server->ops->query_dir_first) {
382 cifsFile->invalidHandle = true;
383 cifsFile->srch_inf.endOfSearch = false;
385 cifs_dbg(FYI, "Full path: %s start at: %lld\n", full_path, file->f_pos);
388 /* test for Unix extensions */
389 /* but now check for them on the share/mount not on the SMB session */
390 /* if (cap_unix(tcon->ses) { */
392 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
393 else if (tcon->posix_extensions)
394 cifsFile->srch_inf.info_level = SMB_FIND_FILE_POSIX_INFO;
395 else if ((tcon->ses->capabilities &
396 tcon->ses->server->vals->cap_nt_find) == 0) {
397 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
398 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
399 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
400 } else /* not srvinos - BB fixme add check for backlevel? */ {
401 cifsFile->srch_inf.info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
404 search_flags = CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME;
405 if (backup_cred(cifs_sb))
406 search_flags |= CIFS_SEARCH_BACKUP_SEARCH;
408 rc = server->ops->query_dir_first(xid, tcon, full_path, cifs_sb,
409 &cifsFile->fid, search_flags,
410 &cifsFile->srch_inf);
413 cifsFile->invalidHandle = false;
414 } else if ((rc == -EOPNOTSUPP) &&
415 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
416 cifs_autodisable_serverino(cifs_sb);
420 cifs_put_tlink(tlink);
425 initiate_cifs_search(const unsigned int xid, struct file *file,
426 const char *full_path)
428 int rc, retry_count = 0;
431 rc = _initiate_cifs_search(xid, file, full_path);
433 * If we don't have enough credits to start reading the
434 * directory just try again after short wait.
439 usleep_range(512, 2048);
440 } while (retry_count++ < 5);
445 /* return length of unicode string in bytes */
446 static int cifs_unicode_bytelen(const char *str)
449 const __le16 *ustr = (const __le16 *)str;
451 for (len = 0; len <= PATH_MAX; len++) {
455 cifs_dbg(FYI, "Unicode string longer than PATH_MAX found\n");
459 static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
462 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
464 if (level == SMB_FIND_FILE_INFO_STANDARD) {
465 FIND_FILE_STANDARD_INFO *pfData;
466 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
468 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) + 1 +
469 pfData->FileNameLength;
471 u32 next_offset = le32_to_cpu(pDirInfo->NextEntryOffset);
473 if (old_entry + next_offset < old_entry) {
474 cifs_dbg(VFS, "Invalid offset %u\n", next_offset);
477 new_entry = old_entry + next_offset;
479 cifs_dbg(FYI, "new entry %p old entry %p\n", new_entry, old_entry);
480 /* validate that new_entry is not past end of SMB */
481 if (new_entry >= end_of_smb) {
482 cifs_dbg(VFS, "search entry %p began after end of SMB %p old entry %p\n",
483 new_entry, end_of_smb, old_entry);
485 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
486 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) + 1 > end_of_smb))
487 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
488 (new_entry + sizeof(FILE_DIRECTORY_INFO) + 1 > end_of_smb))) {
489 cifs_dbg(VFS, "search entry %p extends after end of SMB %p\n",
490 new_entry, end_of_smb);
504 static void cifs_fill_dirent_posix(struct cifs_dirent *de,
505 const struct smb2_posix_info *info)
507 struct smb2_posix_info_parsed parsed;
509 /* payload should have already been checked at this point */
510 if (posix_info_parse(info, NULL, &parsed) < 0) {
511 cifs_dbg(VFS, "Invalid POSIX info payload\n");
515 de->name = parsed.name;
516 de->namelen = parsed.name_len;
517 de->resume_key = info->Ignored;
518 de->ino = le64_to_cpu(info->Inode);
521 static void cifs_fill_dirent_unix(struct cifs_dirent *de,
522 const FILE_UNIX_INFO *info, bool is_unicode)
524 de->name = &info->FileName[0];
526 de->namelen = cifs_unicode_bytelen(de->name);
528 de->namelen = strnlen(de->name, PATH_MAX);
529 de->resume_key = info->ResumeKey;
530 de->ino = le64_to_cpu(info->basic.UniqueId);
533 static void cifs_fill_dirent_dir(struct cifs_dirent *de,
534 const FILE_DIRECTORY_INFO *info)
536 de->name = &info->FileName[0];
537 de->namelen = le32_to_cpu(info->FileNameLength);
538 de->resume_key = info->FileIndex;
541 static void cifs_fill_dirent_full(struct cifs_dirent *de,
542 const FILE_FULL_DIRECTORY_INFO *info)
544 de->name = &info->FileName[0];
545 de->namelen = le32_to_cpu(info->FileNameLength);
546 de->resume_key = info->FileIndex;
549 static void cifs_fill_dirent_search(struct cifs_dirent *de,
550 const SEARCH_ID_FULL_DIR_INFO *info)
552 de->name = &info->FileName[0];
553 de->namelen = le32_to_cpu(info->FileNameLength);
554 de->resume_key = info->FileIndex;
555 de->ino = le64_to_cpu(info->UniqueId);
558 static void cifs_fill_dirent_both(struct cifs_dirent *de,
559 const FILE_BOTH_DIRECTORY_INFO *info)
561 de->name = &info->FileName[0];
562 de->namelen = le32_to_cpu(info->FileNameLength);
563 de->resume_key = info->FileIndex;
566 static void cifs_fill_dirent_std(struct cifs_dirent *de,
567 const FIND_FILE_STANDARD_INFO *info)
569 de->name = &info->FileName[0];
570 /* one byte length, no endianness conversion */
571 de->namelen = info->FileNameLength;
572 de->resume_key = info->ResumeKey;
575 static int cifs_fill_dirent(struct cifs_dirent *de, const void *info,
576 u16 level, bool is_unicode)
578 memset(de, 0, sizeof(*de));
581 case SMB_FIND_FILE_POSIX_INFO:
582 cifs_fill_dirent_posix(de, info);
584 case SMB_FIND_FILE_UNIX:
585 cifs_fill_dirent_unix(de, info, is_unicode);
587 case SMB_FIND_FILE_DIRECTORY_INFO:
588 cifs_fill_dirent_dir(de, info);
590 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
591 cifs_fill_dirent_full(de, info);
593 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
594 cifs_fill_dirent_search(de, info);
596 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
597 cifs_fill_dirent_both(de, info);
599 case SMB_FIND_FILE_INFO_STANDARD:
600 cifs_fill_dirent_std(de, info);
603 cifs_dbg(FYI, "Unknown findfirst level %d\n", level);
610 #define UNICODE_DOT cpu_to_le16(0x2e)
612 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
613 static int cifs_entry_is_dot(struct cifs_dirent *de, bool is_unicode)
621 __le16 *ufilename = (__le16 *)de->name;
622 if (de->namelen == 2) {
624 if (ufilename[0] == UNICODE_DOT)
626 } else if (de->namelen == 4) {
628 if (ufilename[0] == UNICODE_DOT &&
629 ufilename[1] == UNICODE_DOT)
633 if (de->namelen == 1) {
634 if (de->name[0] == '.')
636 } else if (de->namelen == 2) {
637 if (de->name[0] == '.' && de->name[1] == '.')
645 /* Check if directory that we are searching has changed so we can decide
646 whether we can use the cached search results from the previous search */
647 static int is_dir_changed(struct file *file)
649 struct inode *inode = file_inode(file);
650 struct cifsInodeInfo *cifs_inode_info = CIFS_I(inode);
652 if (cifs_inode_info->time == 0)
653 return 1; /* directory was changed, e.g. unlink or new file */
659 static int cifs_save_resume_key(const char *current_entry,
660 struct cifsFileInfo *file_info)
662 struct cifs_dirent de;
665 rc = cifs_fill_dirent(&de, current_entry, file_info->srch_inf.info_level,
666 file_info->srch_inf.unicode);
668 file_info->srch_inf.presume_name = de.name;
669 file_info->srch_inf.resume_name_len = de.namelen;
670 file_info->srch_inf.resume_key = de.resume_key;
676 * Find the corresponding entry in the search. Note that the SMB server returns
677 * search entries for . and .. which complicates logic here if we choose to
678 * parse for them and we do not assume that they are located in the findfirst
679 * return buffer. We start counting in the buffer with entry 2 and increment for
680 * every entry (do not increment for . or .. entry).
683 find_cifs_entry(const unsigned int xid, struct cifs_tcon *tcon, loff_t pos,
684 struct file *file, const char *full_path,
685 char **current_entry, int *num_to_ret)
690 loff_t first_entry_in_buffer;
691 loff_t index_to_find = pos;
692 struct cifsFileInfo *cfile = file->private_data;
693 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
694 struct TCP_Server_Info *server = tcon->ses->server;
695 /* check if index in the buffer */
697 if (!server->ops->query_dir_first || !server->ops->query_dir_next)
700 if ((cfile == NULL) || (current_entry == NULL) || (num_to_ret == NULL))
703 *current_entry = NULL;
704 first_entry_in_buffer = cfile->srch_inf.index_of_last_entry -
705 cfile->srch_inf.entries_in_buffer;
708 * If first entry in buf is zero then is first buffer
709 * in search response data which means it is likely . and ..
710 * will be in this buffer, although some servers do not return
711 * . and .. for the root of a drive and for those we need
712 * to start two entries earlier.
715 dump_cifs_file_struct(file, "In fce ");
716 if (((index_to_find < cfile->srch_inf.index_of_last_entry) &&
717 is_dir_changed(file)) || (index_to_find < first_entry_in_buffer)) {
718 /* close and restart search */
719 cifs_dbg(FYI, "search backing up - close and restart search\n");
720 spin_lock(&cfile->file_info_lock);
721 if (server->ops->dir_needs_close(cfile)) {
722 cfile->invalidHandle = true;
723 spin_unlock(&cfile->file_info_lock);
724 if (server->ops->close_dir)
725 server->ops->close_dir(xid, tcon, &cfile->fid);
727 spin_unlock(&cfile->file_info_lock);
728 if (cfile->srch_inf.ntwrk_buf_start) {
729 cifs_dbg(FYI, "freeing SMB ff cache buf on search rewind\n");
730 if (cfile->srch_inf.smallBuf)
731 cifs_small_buf_release(cfile->srch_inf.
734 cifs_buf_release(cfile->srch_inf.
736 cfile->srch_inf.ntwrk_buf_start = NULL;
738 rc = initiate_cifs_search(xid, file, full_path);
740 cifs_dbg(FYI, "error %d reinitiating a search on rewind\n",
744 /* FindFirst/Next set last_entry to NULL on malformed reply */
745 if (cfile->srch_inf.last_entry)
746 cifs_save_resume_key(cfile->srch_inf.last_entry, cfile);
749 search_flags = CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME;
750 if (backup_cred(cifs_sb))
751 search_flags |= CIFS_SEARCH_BACKUP_SEARCH;
753 while ((index_to_find >= cfile->srch_inf.index_of_last_entry) &&
754 (rc == 0) && !cfile->srch_inf.endOfSearch) {
755 cifs_dbg(FYI, "calling findnext2\n");
756 rc = server->ops->query_dir_next(xid, tcon, &cfile->fid,
759 /* FindFirst/Next set last_entry to NULL on malformed reply */
760 if (cfile->srch_inf.last_entry)
761 cifs_save_resume_key(cfile->srch_inf.last_entry, cfile);
765 if (index_to_find < cfile->srch_inf.index_of_last_entry) {
766 /* we found the buffer that contains the entry */
767 /* scan and find it */
772 if (cfile->srch_inf.ntwrk_buf_start == NULL) {
773 cifs_dbg(VFS, "ntwrk_buf_start is NULL during readdir\n");
777 end_of_smb = cfile->srch_inf.ntwrk_buf_start +
778 server->ops->calc_smb_size(
779 cfile->srch_inf.ntwrk_buf_start);
781 cur_ent = cfile->srch_inf.srch_entries_start;
782 first_entry_in_buffer = cfile->srch_inf.index_of_last_entry
783 - cfile->srch_inf.entries_in_buffer;
784 pos_in_buf = index_to_find - first_entry_in_buffer;
785 cifs_dbg(FYI, "found entry - pos_in_buf %d\n", pos_in_buf);
787 for (i = 0; (i < (pos_in_buf)) && (cur_ent != NULL); i++) {
788 /* go entry by entry figuring out which is first */
789 cur_ent = nxt_dir_entry(cur_ent, end_of_smb,
790 cfile->srch_inf.info_level);
792 if ((cur_ent == NULL) && (i < pos_in_buf)) {
793 /* BB fixme - check if we should flag this error */
794 cifs_dbg(VFS, "reached end of buf searching for pos in buf %d index to find %lld rc %d\n",
795 pos_in_buf, index_to_find, rc);
798 *current_entry = cur_ent;
800 cifs_dbg(FYI, "index not in buffer - could not findnext into it\n");
804 if (pos_in_buf >= cfile->srch_inf.entries_in_buffer) {
805 cifs_dbg(FYI, "can not return entries pos_in_buf beyond last\n");
808 *num_to_ret = cfile->srch_inf.entries_in_buffer - pos_in_buf;
813 static bool emit_cached_dirents(struct cached_dirents *cde,
814 struct dir_context *ctx)
816 struct cached_dirent *dirent;
819 list_for_each_entry(dirent, &cde->entries, entry) {
821 * Skip all early entries prior to the current lseek()
824 if (ctx->pos > dirent->pos)
827 * We recorded the current ->pos value for the dirent
828 * when we stored it in the cache.
829 * However, this sequence of ->pos values may have holes
830 * in it, for example dot-dirs returned from the server
832 * Handle this by forcing ctx->pos to be the same as the
833 * ->pos of the current dirent we emit from the cache.
834 * This means that when we emit these entries from the cache
835 * we now emit them with the same ->pos value as in the
838 ctx->pos = dirent->pos;
839 rc = dir_emit(ctx, dirent->name, dirent->namelen,
840 dirent->fattr.cf_uniqueid,
841 dirent->fattr.cf_dtype);
849 static void update_cached_dirents_count(struct cached_dirents *cde,
850 struct dir_context *ctx)
854 if (cde->is_valid || cde->is_failed)
860 static void finished_cached_dirents_count(struct cached_dirents *cde,
861 struct dir_context *ctx)
865 if (cde->is_valid || cde->is_failed)
867 if (ctx->pos != cde->pos)
873 static void add_cached_dirent(struct cached_dirents *cde,
874 struct dir_context *ctx,
875 const char *name, int namelen,
876 struct cifs_fattr *fattr)
878 struct cached_dirent *de;
882 if (cde->is_valid || cde->is_failed)
884 if (ctx->pos != cde->pos) {
888 de = kzalloc(sizeof(*de), GFP_ATOMIC);
893 de->namelen = namelen;
894 de->name = kstrndup(name, namelen, GFP_ATOMIC);
895 if (de->name == NULL) {
902 memcpy(&de->fattr, fattr, sizeof(struct cifs_fattr));
904 list_add_tail(&de->entry, &cde->entries);
907 static bool cifs_dir_emit(struct dir_context *ctx,
908 const char *name, int namelen,
909 struct cifs_fattr *fattr,
910 struct cached_fid *cfid)
913 ino_t ino = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
915 rc = dir_emit(ctx, name, namelen, ino, fattr->cf_dtype);
920 mutex_lock(&cfid->dirents.de_mutex);
921 add_cached_dirent(&cfid->dirents, ctx, name, namelen,
923 mutex_unlock(&cfid->dirents.de_mutex);
929 static int cifs_filldir(char *find_entry, struct file *file,
930 struct dir_context *ctx,
931 char *scratch_buf, unsigned int max_len,
932 struct cached_fid *cfid)
934 struct cifsFileInfo *file_info = file->private_data;
935 struct super_block *sb = file_inode(file)->i_sb;
936 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
937 struct cifs_dirent de = { NULL, };
938 struct cifs_fattr fattr;
942 rc = cifs_fill_dirent(&de, find_entry, file_info->srch_inf.info_level,
943 file_info->srch_inf.unicode);
947 if (de.namelen > max_len) {
948 cifs_dbg(VFS, "bad search response length %zd past smb end\n",
953 /* skip . and .. since we added them first */
954 if (cifs_entry_is_dot(&de, file_info->srch_inf.unicode))
957 if (file_info->srch_inf.unicode) {
958 struct nls_table *nlt = cifs_sb->local_nls;
961 map_type = cifs_remap(cifs_sb);
962 name.name = scratch_buf;
964 cifs_from_utf16((char *)name.name, (__le16 *)de.name,
966 min_t(size_t, de.namelen,
967 (size_t)max_len), nlt, map_type);
968 name.len -= nls_nullsize(nlt);
971 name.len = de.namelen;
974 switch (file_info->srch_inf.info_level) {
975 case SMB_FIND_FILE_POSIX_INFO:
976 cifs_posix_to_fattr(&fattr,
977 (struct smb2_posix_info *)find_entry,
980 case SMB_FIND_FILE_UNIX:
981 cifs_unix_basic_to_fattr(&fattr,
982 &((FILE_UNIX_INFO *)find_entry)->basic,
984 if (S_ISLNK(fattr.cf_mode))
985 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
987 case SMB_FIND_FILE_INFO_STANDARD:
988 cifs_std_info_to_fattr(&fattr,
989 (FIND_FILE_STANDARD_INFO *)find_entry,
992 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
993 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
994 cifs_fulldir_info_to_fattr(&fattr, find_entry, cifs_sb);
997 cifs_dir_info_to_fattr(&fattr,
998 (FILE_DIRECTORY_INFO *)find_entry,
1003 if (de.ino && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
1004 fattr.cf_uniqueid = de.ino;
1006 fattr.cf_uniqueid = iunique(sb, ROOT_I);
1007 cifs_autodisable_serverino(cifs_sb);
1010 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) &&
1011 couldbe_mf_symlink(&fattr))
1013 * trying to get the type and mode can be slow,
1014 * so just call those regular files for now, and mark
1017 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
1019 cifs_prime_dcache(file_dentry(file), &name, &fattr);
1021 return !cifs_dir_emit(ctx, name.name, name.len,
1026 int cifs_readdir(struct file *file, struct dir_context *ctx)
1031 struct tcon_link *tlink = NULL;
1032 struct cifs_tcon *tcon;
1033 struct cifsFileInfo *cifsFile;
1034 char *current_entry;
1035 int num_to_fill = 0;
1036 char *tmp_buf = NULL;
1038 unsigned int max_len;
1039 const char *full_path;
1040 void *page = alloc_dentry_path();
1041 struct cached_fid *cfid = NULL;
1042 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
1046 full_path = build_path_from_dentry(file_dentry(file), page);
1047 if (IS_ERR(full_path)) {
1048 rc = PTR_ERR(full_path);
1052 if (file->private_data == NULL) {
1053 tlink = cifs_sb_tlink(cifs_sb);
1055 goto cache_not_found;
1056 tcon = tlink_tcon(tlink);
1058 cifsFile = file->private_data;
1059 tcon = tlink_tcon(cifsFile->tlink);
1062 rc = open_cached_dir(xid, tcon, full_path, cifs_sb, false, &cfid);
1063 cifs_put_tlink(tlink);
1065 goto cache_not_found;
1067 mutex_lock(&cfid->dirents.de_mutex);
1069 * If this was reading from the start of the directory
1070 * we need to initialize scanning and storing the
1071 * directory content.
1073 if (ctx->pos == 0 && cfid->dirents.ctx == NULL) {
1074 cfid->dirents.ctx = ctx;
1075 cfid->dirents.pos = 2;
1078 * If we already have the entire directory cached then
1079 * we can just serve the cache.
1081 if (cfid->dirents.is_valid) {
1082 if (!dir_emit_dots(file, ctx)) {
1083 mutex_unlock(&cfid->dirents.de_mutex);
1086 emit_cached_dirents(&cfid->dirents, ctx);
1087 mutex_unlock(&cfid->dirents.de_mutex);
1090 mutex_unlock(&cfid->dirents.de_mutex);
1092 /* Drop the cache while calling initiate_cifs_search and
1093 * find_cifs_entry in case there will be reconnects during
1096 close_cached_dir(cfid);
1101 * Ensure FindFirst doesn't fail before doing filldir() for '.' and
1102 * '..'. Otherwise we won't be able to notify VFS in case of failure.
1104 if (file->private_data == NULL) {
1105 rc = initiate_cifs_search(xid, file, full_path);
1106 cifs_dbg(FYI, "initiate cifs search rc %d\n", rc);
1111 if (!dir_emit_dots(file, ctx))
1114 /* 1) If search is active,
1115 is in current search buffer?
1116 if it before then restart search
1117 if after then keep searching till find it */
1118 cifsFile = file->private_data;
1119 if (cifsFile->srch_inf.endOfSearch) {
1120 if (cifsFile->srch_inf.emptyDir) {
1121 cifs_dbg(FYI, "End of search, empty dir\n");
1126 cifsFile->invalidHandle = true;
1127 tcon->ses->server->close(xid, tcon, &cifsFile->fid);
1130 tcon = tlink_tcon(cifsFile->tlink);
1131 rc = find_cifs_entry(xid, tcon, ctx->pos, file, full_path,
1132 ¤t_entry, &num_to_fill);
1133 open_cached_dir(xid, tcon, full_path, cifs_sb, false, &cfid);
1135 cifs_dbg(FYI, "fce error %d\n", rc);
1137 } else if (current_entry != NULL) {
1138 cifs_dbg(FYI, "entry %lld found\n", ctx->pos);
1141 mutex_lock(&cfid->dirents.de_mutex);
1142 finished_cached_dirents_count(&cfid->dirents, ctx);
1143 mutex_unlock(&cfid->dirents.de_mutex);
1145 cifs_dbg(FYI, "Could not find entry\n");
1148 cifs_dbg(FYI, "loop through %d times filling dir for net buf %p\n",
1149 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start);
1150 max_len = tcon->ses->server->ops->calc_smb_size(
1151 cifsFile->srch_inf.ntwrk_buf_start);
1152 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1154 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
1155 if (tmp_buf == NULL) {
1160 for (i = 0; i < num_to_fill; i++) {
1161 if (current_entry == NULL) {
1162 /* evaluate whether this case is an error */
1163 cifs_dbg(VFS, "past SMB end, num to fill %d i %d\n",
1168 * if buggy server returns . and .. late do we want to
1169 * check for that here?
1172 rc = cifs_filldir(current_entry, file, ctx,
1173 tmp_buf, max_len, cfid);
1182 mutex_lock(&cfid->dirents.de_mutex);
1183 update_cached_dirents_count(&cfid->dirents, ctx);
1184 mutex_unlock(&cfid->dirents.de_mutex);
1188 cifsFile->srch_inf.index_of_last_entry) {
1189 cifs_dbg(FYI, "last entry in buf at pos %lld %s\n",
1191 cifs_save_resume_key(current_entry, cifsFile);
1195 nxt_dir_entry(current_entry, end_of_smb,
1196 cifsFile->srch_inf.info_level);
1202 close_cached_dir(cfid);
1203 free_dentry_path(page);