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
8 * Author(s): Steve French (sfrench@us.ibm.com)
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"
27 * To be safe - for UCS to UTF-8 with strings loaded with the rare long
28 * characters alloc more to account for such multibyte target UTF-8
31 #define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
33 #ifdef CONFIG_CIFS_DEBUG2
34 static void dump_cifs_file_struct(struct file *file, char *label)
36 struct cifsFileInfo *cf;
39 cf = file->private_data;
41 cifs_dbg(FYI, "empty cifs private file data\n");
44 if (cf->invalidHandle)
45 cifs_dbg(FYI, "Invalid handle\n");
46 if (cf->srch_inf.endOfSearch)
47 cifs_dbg(FYI, "end of search\n");
48 if (cf->srch_inf.emptyDir)
49 cifs_dbg(FYI, "empty dir\n");
53 static inline void dump_cifs_file_struct(struct file *file, char *label)
59 * Match a reparse point inode if reparse tag and ctime haven't changed.
61 * Windows Server updates ctime of reparse points when their data have changed.
62 * The server doesn't allow changing reparse tags from existing reparse points,
63 * though it's worth checking.
65 static inline bool reparse_inode_match(struct inode *inode,
66 struct cifs_fattr *fattr)
68 struct timespec64 ctime = inode_get_ctime(inode);
70 return (CIFS_I(inode)->cifsAttrs & ATTR_REPARSE) &&
71 CIFS_I(inode)->reparse_tag == fattr->cf_cifstag &&
72 timespec64_equal(&ctime, &fattr->cf_ctime);
76 * Attempt to preload the dcache with the results from the FIND_FIRST/NEXT
78 * Find the dentry that matches "name". If there isn't one, create one. If it's
79 * a negative dentry or the uniqueid or filetype(mode) changed,
80 * then drop it and recreate it.
83 cifs_prime_dcache(struct dentry *parent, struct qstr *name,
84 struct cifs_fattr *fattr)
86 struct dentry *dentry, *alias;
88 struct super_block *sb = parent->d_sb;
89 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
90 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
93 cifs_dbg(FYI, "%s: for %s\n", __func__, name->name);
95 dentry = d_hash_and_lookup(parent, name);
98 * If we know that the inode will need to be revalidated
99 * immediately, then don't create a new dentry for it.
100 * We'll end up doing an on the wire call either way and
101 * this spares us an invalidation.
104 if ((fattr->cf_cifsattrs & ATTR_REPARSE) ||
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 the existing mode, size and
137 * symlink target (if any) when reparse tag and
138 * ctime haven't changed.
141 if (fattr->cf_cifsattrs & ATTR_REPARSE) {
142 if (likely(reparse_inode_match(inode, fattr))) {
143 fattr->cf_mode = inode->i_mode;
144 fattr->cf_eof = CIFS_I(inode)->server_eof;
145 fattr->cf_symlink_target = NULL;
147 CIFS_I(inode)->time = 0;
151 if (!rc && !cifs_fattr_to_inode(inode, fattr)) {
157 d_invalidate(dentry);
161 inode = cifs_iget(sb, fattr);
163 inode = ERR_PTR(-ENOMEM);
164 alias = d_splice_alias(inode, dentry);
165 d_lookup_done(dentry);
166 if (alias && !IS_ERR(alias))
173 cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
175 struct cifs_open_info_data data = {
176 .reparse = { .tag = fattr->cf_cifstag, },
179 fattr->cf_uid = cifs_sb->ctx->linux_uid;
180 fattr->cf_gid = cifs_sb->ctx->linux_gid;
183 * The IO_REPARSE_TAG_LX_ tags originally were used by WSL but they
184 * are preferred by the Linux client in some cases since, unlike
185 * the NFS reparse tag (or EAs), they don't require an extra query
186 * to determine which type of special file they represent.
187 * TODO: go through all documented reparse tags to see if we can
188 * reasonably map some of them to directories vs. files vs. symlinks
190 if ((fattr->cf_cifsattrs & ATTR_REPARSE) &&
191 cifs_reparse_point_to_fattr(cifs_sb, fattr, &data))
194 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
195 fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
196 fattr->cf_dtype = DT_DIR;
198 fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
199 fattr->cf_dtype = DT_REG;
203 /* non-unix readdir doesn't provide nlink */
204 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
206 if (fattr->cf_cifsattrs & ATTR_READONLY)
207 fattr->cf_mode &= ~S_IWUGO;
210 * We of course don't get ACL info in FIND_FIRST/NEXT results, so
211 * mark it for revalidation so that "ls -l" will look right. It might
212 * be super-slow, but if we don't do this then the ownership of files
213 * may look wrong since the inodes may not have timed out by the time
214 * "ls" does a stat() call on them.
216 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
217 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID))
218 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
220 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
221 fattr->cf_cifsattrs & ATTR_SYSTEM) {
222 if (fattr->cf_eof == 0) {
223 fattr->cf_mode &= ~S_IFMT;
224 fattr->cf_mode |= S_IFIFO;
225 fattr->cf_dtype = DT_FIFO;
228 * trying to get the type and mode via SFU can be slow,
229 * so just call those regular files for now, and mark
232 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
237 /* Fill a cifs_fattr struct with info from SMB_FIND_FILE_POSIX_INFO. */
239 cifs_posix_to_fattr(struct cifs_fattr *fattr, struct smb2_posix_info *info,
240 struct cifs_sb_info *cifs_sb)
242 struct smb2_posix_info_parsed parsed;
244 posix_info_parse(info, NULL, &parsed);
246 memset(fattr, 0, sizeof(*fattr));
247 fattr->cf_uniqueid = le64_to_cpu(info->Inode);
248 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
249 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
251 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
252 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
253 fattr->cf_ctime = cifs_NTtimeToUnix(info->CreationTime);
255 fattr->cf_nlink = le32_to_cpu(info->HardLinks);
256 fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
259 * Since we set the inode type below we need to mask off
260 * to avoid strange results if bits set above.
261 * XXX: why not make server&client use the type bits?
263 fattr->cf_mode = le32_to_cpu(info->Mode) & ~S_IFMT;
265 cifs_dbg(FYI, "posix fattr: dev %d, reparse %d, mode %o\n",
266 le32_to_cpu(info->DeviceId),
267 le32_to_cpu(info->ReparseTag),
268 le32_to_cpu(info->Mode));
270 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
271 fattr->cf_mode |= S_IFDIR;
272 fattr->cf_dtype = DT_DIR;
275 * mark anything that is not a dir as regular
276 * file. special files should have the REPARSE
277 * attribute and will be marked as needing revaluation
279 fattr->cf_mode |= S_IFREG;
280 fattr->cf_dtype = DT_REG;
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,
310 SEARCH_ID_FULL_DIR_INFO *info,
311 struct cifs_sb_info *cifs_sb)
313 __dir_info_to_fattr(fattr, info);
315 /* See MS-FSCC 2.4.19 FileIdFullDirectoryInformation */
316 if (fattr->cf_cifsattrs & ATTR_REPARSE)
317 fattr->cf_cifstag = le32_to_cpu(info->EaSize);
318 cifs_fill_common_info(fattr, cifs_sb);
322 cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info,
323 struct cifs_sb_info *cifs_sb)
325 int offset = cifs_sb_master_tcon(cifs_sb)->ses->server->timeAdj;
327 memset(fattr, 0, sizeof(*fattr));
328 fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate,
329 info->LastAccessTime, offset);
330 fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate,
331 info->LastWriteTime, offset);
332 fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate,
333 info->LastWriteTime, offset);
335 fattr->cf_cifsattrs = le16_to_cpu(info->Attributes);
336 fattr->cf_bytes = le32_to_cpu(info->AllocationSize);
337 fattr->cf_eof = le32_to_cpu(info->DataSize);
339 cifs_fill_common_info(fattr, cifs_sb);
343 _initiate_cifs_search(const unsigned int xid, struct file *file,
344 const char *full_path)
348 struct cifsFileInfo *cifsFile;
349 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
350 struct tcon_link *tlink = NULL;
351 struct cifs_tcon *tcon;
352 struct TCP_Server_Info *server;
354 if (file->private_data == NULL) {
355 tlink = cifs_sb_tlink(cifs_sb);
357 return PTR_ERR(tlink);
359 cifsFile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
360 if (cifsFile == NULL) {
364 spin_lock_init(&cifsFile->file_info_lock);
365 file->private_data = cifsFile;
366 cifsFile->tlink = cifs_get_tlink(tlink);
367 tcon = tlink_tcon(tlink);
369 cifsFile = file->private_data;
370 tcon = tlink_tcon(cifsFile->tlink);
373 server = tcon->ses->server;
375 if (!server->ops->query_dir_first) {
380 cifsFile->invalidHandle = true;
381 cifsFile->srch_inf.endOfSearch = false;
383 cifs_dbg(FYI, "Full path: %s start at: %lld\n", full_path, file->f_pos);
386 /* test for Unix extensions */
387 /* but now check for them on the share/mount not on the SMB session */
388 /* if (cap_unix(tcon->ses) { */
390 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
391 else if (tcon->posix_extensions)
392 cifsFile->srch_inf.info_level = SMB_FIND_FILE_POSIX_INFO;
393 else if ((tcon->ses->capabilities &
394 tcon->ses->server->vals->cap_nt_find) == 0) {
395 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
396 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
397 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
398 } else /* not srvinos - BB fixme add check for backlevel? */ {
399 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
402 search_flags = CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME;
403 if (backup_cred(cifs_sb))
404 search_flags |= CIFS_SEARCH_BACKUP_SEARCH;
406 rc = server->ops->query_dir_first(xid, tcon, full_path, cifs_sb,
407 &cifsFile->fid, search_flags,
408 &cifsFile->srch_inf);
411 cifsFile->invalidHandle = false;
412 } else if ((rc == -EOPNOTSUPP) &&
413 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
414 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
418 cifs_put_tlink(tlink);
423 initiate_cifs_search(const unsigned int xid, struct file *file,
424 const char *full_path)
426 int rc, retry_count = 0;
429 rc = _initiate_cifs_search(xid, file, full_path);
431 * If we don't have enough credits to start reading the
432 * directory just try again after short wait.
437 usleep_range(512, 2048);
438 } while (retry_count++ < 5);
443 /* return length of unicode string in bytes */
444 static int cifs_unicode_bytelen(const char *str)
447 const __le16 *ustr = (const __le16 *)str;
449 for (len = 0; len <= PATH_MAX; len++) {
453 cifs_dbg(FYI, "Unicode string longer than PATH_MAX found\n");
457 static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
460 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
462 if (level == SMB_FIND_FILE_INFO_STANDARD) {
463 FIND_FILE_STANDARD_INFO *pfData;
464 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
466 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) + 1 +
467 pfData->FileNameLength;
469 u32 next_offset = le32_to_cpu(pDirInfo->NextEntryOffset);
471 if (old_entry + next_offset < old_entry) {
472 cifs_dbg(VFS, "Invalid offset %u\n", next_offset);
475 new_entry = old_entry + next_offset;
477 cifs_dbg(FYI, "new entry %p old entry %p\n", new_entry, old_entry);
478 /* validate that new_entry is not past end of SMB */
479 if (new_entry >= end_of_smb) {
480 cifs_dbg(VFS, "search entry %p began after end of SMB %p old entry %p\n",
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) + 1 > end_of_smb))
485 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
486 (new_entry + sizeof(FILE_DIRECTORY_INFO) + 1 > end_of_smb))) {
487 cifs_dbg(VFS, "search entry %p extends after end of SMB %p\n",
488 new_entry, end_of_smb);
502 static void cifs_fill_dirent_posix(struct cifs_dirent *de,
503 const struct smb2_posix_info *info)
505 struct smb2_posix_info_parsed parsed;
507 /* payload should have already been checked at this point */
508 if (posix_info_parse(info, NULL, &parsed) < 0) {
509 cifs_dbg(VFS, "Invalid POSIX info payload\n");
513 de->name = parsed.name;
514 de->namelen = parsed.name_len;
515 de->resume_key = info->Ignored;
516 de->ino = le64_to_cpu(info->Inode);
519 static void cifs_fill_dirent_unix(struct cifs_dirent *de,
520 const FILE_UNIX_INFO *info, bool is_unicode)
522 de->name = &info->FileName[0];
524 de->namelen = cifs_unicode_bytelen(de->name);
526 de->namelen = strnlen(de->name, PATH_MAX);
527 de->resume_key = info->ResumeKey;
528 de->ino = le64_to_cpu(info->basic.UniqueId);
531 static void cifs_fill_dirent_dir(struct cifs_dirent *de,
532 const FILE_DIRECTORY_INFO *info)
534 de->name = &info->FileName[0];
535 de->namelen = le32_to_cpu(info->FileNameLength);
536 de->resume_key = info->FileIndex;
539 static void cifs_fill_dirent_full(struct cifs_dirent *de,
540 const FILE_FULL_DIRECTORY_INFO *info)
542 de->name = &info->FileName[0];
543 de->namelen = le32_to_cpu(info->FileNameLength);
544 de->resume_key = info->FileIndex;
547 static void cifs_fill_dirent_search(struct cifs_dirent *de,
548 const SEARCH_ID_FULL_DIR_INFO *info)
550 de->name = &info->FileName[0];
551 de->namelen = le32_to_cpu(info->FileNameLength);
552 de->resume_key = info->FileIndex;
553 de->ino = le64_to_cpu(info->UniqueId);
556 static void cifs_fill_dirent_both(struct cifs_dirent *de,
557 const FILE_BOTH_DIRECTORY_INFO *info)
559 de->name = &info->FileName[0];
560 de->namelen = le32_to_cpu(info->FileNameLength);
561 de->resume_key = info->FileIndex;
564 static void cifs_fill_dirent_std(struct cifs_dirent *de,
565 const FIND_FILE_STANDARD_INFO *info)
567 de->name = &info->FileName[0];
568 /* one byte length, no endianess conversion */
569 de->namelen = info->FileNameLength;
570 de->resume_key = info->ResumeKey;
573 static int cifs_fill_dirent(struct cifs_dirent *de, const void *info,
574 u16 level, bool is_unicode)
576 memset(de, 0, sizeof(*de));
579 case SMB_FIND_FILE_POSIX_INFO:
580 cifs_fill_dirent_posix(de, info);
582 case SMB_FIND_FILE_UNIX:
583 cifs_fill_dirent_unix(de, info, is_unicode);
585 case SMB_FIND_FILE_DIRECTORY_INFO:
586 cifs_fill_dirent_dir(de, info);
588 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
589 cifs_fill_dirent_full(de, info);
591 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
592 cifs_fill_dirent_search(de, info);
594 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
595 cifs_fill_dirent_both(de, info);
597 case SMB_FIND_FILE_INFO_STANDARD:
598 cifs_fill_dirent_std(de, info);
601 cifs_dbg(FYI, "Unknown findfirst level %d\n", level);
608 #define UNICODE_DOT cpu_to_le16(0x2e)
610 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
611 static int cifs_entry_is_dot(struct cifs_dirent *de, bool is_unicode)
619 __le16 *ufilename = (__le16 *)de->name;
620 if (de->namelen == 2) {
622 if (ufilename[0] == UNICODE_DOT)
624 } else if (de->namelen == 4) {
626 if (ufilename[0] == UNICODE_DOT &&
627 ufilename[1] == UNICODE_DOT)
631 if (de->namelen == 1) {
632 if (de->name[0] == '.')
634 } else if (de->namelen == 2) {
635 if (de->name[0] == '.' && de->name[1] == '.')
643 /* Check if directory that we are searching has changed so we can decide
644 whether we can use the cached search results from the previous search */
645 static int is_dir_changed(struct file *file)
647 struct inode *inode = file_inode(file);
648 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
650 if (cifsInfo->time == 0)
651 return 1; /* directory was changed, perhaps due to unlink */
657 static int cifs_save_resume_key(const char *current_entry,
658 struct cifsFileInfo *file_info)
660 struct cifs_dirent de;
663 rc = cifs_fill_dirent(&de, current_entry, file_info->srch_inf.info_level,
664 file_info->srch_inf.unicode);
666 file_info->srch_inf.presume_name = de.name;
667 file_info->srch_inf.resume_name_len = de.namelen;
668 file_info->srch_inf.resume_key = de.resume_key;
674 * Find the corresponding entry in the search. Note that the SMB server returns
675 * search entries for . and .. which complicates logic here if we choose to
676 * parse for them and we do not assume that they are located in the findfirst
677 * return buffer. We start counting in the buffer with entry 2 and increment for
678 * every entry (do not increment for . or .. entry).
681 find_cifs_entry(const unsigned int xid, struct cifs_tcon *tcon, loff_t pos,
682 struct file *file, const char *full_path,
683 char **current_entry, int *num_to_ret)
688 loff_t first_entry_in_buffer;
689 loff_t index_to_find = pos;
690 struct cifsFileInfo *cfile = file->private_data;
691 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
692 struct TCP_Server_Info *server = tcon->ses->server;
693 /* check if index in the buffer */
695 if (!server->ops->query_dir_first || !server->ops->query_dir_next)
698 if ((cfile == NULL) || (current_entry == NULL) || (num_to_ret == NULL))
701 *current_entry = NULL;
702 first_entry_in_buffer = cfile->srch_inf.index_of_last_entry -
703 cfile->srch_inf.entries_in_buffer;
706 * If first entry in buf is zero then is first buffer
707 * in search response data which means it is likely . and ..
708 * will be in this buffer, although some servers do not return
709 * . and .. for the root of a drive and for those we need
710 * to start two entries earlier.
713 dump_cifs_file_struct(file, "In fce ");
714 if (((index_to_find < cfile->srch_inf.index_of_last_entry) &&
715 is_dir_changed(file)) || (index_to_find < first_entry_in_buffer)) {
716 /* close and restart search */
717 cifs_dbg(FYI, "search backing up - close and restart search\n");
718 spin_lock(&cfile->file_info_lock);
719 if (server->ops->dir_needs_close(cfile)) {
720 cfile->invalidHandle = true;
721 spin_unlock(&cfile->file_info_lock);
722 if (server->ops->close_dir)
723 server->ops->close_dir(xid, tcon, &cfile->fid);
725 spin_unlock(&cfile->file_info_lock);
726 if (cfile->srch_inf.ntwrk_buf_start) {
727 cifs_dbg(FYI, "freeing SMB ff cache buf on search rewind\n");
728 if (cfile->srch_inf.smallBuf)
729 cifs_small_buf_release(cfile->srch_inf.
732 cifs_buf_release(cfile->srch_inf.
734 cfile->srch_inf.ntwrk_buf_start = NULL;
736 rc = initiate_cifs_search(xid, file, full_path);
738 cifs_dbg(FYI, "error %d reinitiating a search on rewind\n",
742 /* FindFirst/Next set last_entry to NULL on malformed reply */
743 if (cfile->srch_inf.last_entry)
744 cifs_save_resume_key(cfile->srch_inf.last_entry, cfile);
747 search_flags = CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME;
748 if (backup_cred(cifs_sb))
749 search_flags |= CIFS_SEARCH_BACKUP_SEARCH;
751 while ((index_to_find >= cfile->srch_inf.index_of_last_entry) &&
752 (rc == 0) && !cfile->srch_inf.endOfSearch) {
753 cifs_dbg(FYI, "calling findnext2\n");
754 rc = server->ops->query_dir_next(xid, tcon, &cfile->fid,
757 /* FindFirst/Next set last_entry to NULL on malformed reply */
758 if (cfile->srch_inf.last_entry)
759 cifs_save_resume_key(cfile->srch_inf.last_entry, cfile);
763 if (index_to_find < cfile->srch_inf.index_of_last_entry) {
764 /* we found the buffer that contains the entry */
765 /* scan and find it */
770 if (cfile->srch_inf.ntwrk_buf_start == NULL) {
771 cifs_dbg(VFS, "ntwrk_buf_start is NULL during readdir\n");
775 end_of_smb = cfile->srch_inf.ntwrk_buf_start +
776 server->ops->calc_smb_size(
777 cfile->srch_inf.ntwrk_buf_start);
779 cur_ent = cfile->srch_inf.srch_entries_start;
780 first_entry_in_buffer = cfile->srch_inf.index_of_last_entry
781 - cfile->srch_inf.entries_in_buffer;
782 pos_in_buf = index_to_find - first_entry_in_buffer;
783 cifs_dbg(FYI, "found entry - pos_in_buf %d\n", pos_in_buf);
785 for (i = 0; (i < (pos_in_buf)) && (cur_ent != NULL); i++) {
786 /* go entry by entry figuring out which is first */
787 cur_ent = nxt_dir_entry(cur_ent, end_of_smb,
788 cfile->srch_inf.info_level);
790 if ((cur_ent == NULL) && (i < pos_in_buf)) {
791 /* BB fixme - check if we should flag this error */
792 cifs_dbg(VFS, "reached end of buf searching for pos in buf %d index to find %lld rc %d\n",
793 pos_in_buf, index_to_find, rc);
796 *current_entry = cur_ent;
798 cifs_dbg(FYI, "index not in buffer - could not findnext into it\n");
802 if (pos_in_buf >= cfile->srch_inf.entries_in_buffer) {
803 cifs_dbg(FYI, "can not return entries pos_in_buf beyond last\n");
806 *num_to_ret = cfile->srch_inf.entries_in_buffer - pos_in_buf;
811 static bool emit_cached_dirents(struct cached_dirents *cde,
812 struct dir_context *ctx)
814 struct cached_dirent *dirent;
817 list_for_each_entry(dirent, &cde->entries, entry) {
819 * Skip all early entries prior to the current lseek()
822 if (ctx->pos > dirent->pos)
825 * We recorded the current ->pos value for the dirent
826 * when we stored it in the cache.
827 * However, this sequence of ->pos values may have holes
828 * in it, for example dot-dirs returned from the server
830 * Handle this bu forcing ctx->pos to be the same as the
831 * ->pos of the current dirent we emit from the cache.
832 * This means that when we emit these entries from the cache
833 * we now emit them with the same ->pos value as in the
836 ctx->pos = dirent->pos;
837 rc = dir_emit(ctx, dirent->name, dirent->namelen,
838 dirent->fattr.cf_uniqueid,
839 dirent->fattr.cf_dtype);
847 static void update_cached_dirents_count(struct cached_dirents *cde,
848 struct dir_context *ctx)
852 if (cde->is_valid || cde->is_failed)
858 static void finished_cached_dirents_count(struct cached_dirents *cde,
859 struct dir_context *ctx)
863 if (cde->is_valid || cde->is_failed)
865 if (ctx->pos != cde->pos)
871 static void add_cached_dirent(struct cached_dirents *cde,
872 struct dir_context *ctx,
873 const char *name, int namelen,
874 struct cifs_fattr *fattr)
876 struct cached_dirent *de;
880 if (cde->is_valid || cde->is_failed)
882 if (ctx->pos != cde->pos) {
886 de = kzalloc(sizeof(*de), GFP_ATOMIC);
891 de->namelen = namelen;
892 de->name = kstrndup(name, namelen, GFP_ATOMIC);
893 if (de->name == NULL) {
900 memcpy(&de->fattr, fattr, sizeof(struct cifs_fattr));
902 list_add_tail(&de->entry, &cde->entries);
905 static bool cifs_dir_emit(struct dir_context *ctx,
906 const char *name, int namelen,
907 struct cifs_fattr *fattr,
908 struct cached_fid *cfid)
911 ino_t ino = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
913 rc = dir_emit(ctx, name, namelen, ino, fattr->cf_dtype);
918 mutex_lock(&cfid->dirents.de_mutex);
919 add_cached_dirent(&cfid->dirents, ctx, name, namelen,
921 mutex_unlock(&cfid->dirents.de_mutex);
927 static int cifs_filldir(char *find_entry, struct file *file,
928 struct dir_context *ctx,
929 char *scratch_buf, unsigned int max_len,
930 struct cached_fid *cfid)
932 struct cifsFileInfo *file_info = file->private_data;
933 struct super_block *sb = file_inode(file)->i_sb;
934 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
935 struct cifs_dirent de = { NULL, };
936 struct cifs_fattr fattr;
940 rc = cifs_fill_dirent(&de, find_entry, file_info->srch_inf.info_level,
941 file_info->srch_inf.unicode);
945 if (de.namelen > max_len) {
946 cifs_dbg(VFS, "bad search response length %zd past smb end\n",
951 /* skip . and .. since we added them first */
952 if (cifs_entry_is_dot(&de, file_info->srch_inf.unicode))
955 if (file_info->srch_inf.unicode) {
956 struct nls_table *nlt = cifs_sb->local_nls;
959 map_type = cifs_remap(cifs_sb);
960 name.name = scratch_buf;
962 cifs_from_utf16((char *)name.name, (__le16 *)de.name,
964 min_t(size_t, de.namelen,
965 (size_t)max_len), nlt, map_type);
966 name.len -= nls_nullsize(nlt);
969 name.len = de.namelen;
972 switch (file_info->srch_inf.info_level) {
973 case SMB_FIND_FILE_POSIX_INFO:
974 cifs_posix_to_fattr(&fattr,
975 (struct smb2_posix_info *)find_entry,
978 case SMB_FIND_FILE_UNIX:
979 cifs_unix_basic_to_fattr(&fattr,
980 &((FILE_UNIX_INFO *)find_entry)->basic,
982 if (S_ISLNK(fattr.cf_mode))
983 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
985 case SMB_FIND_FILE_INFO_STANDARD:
986 cifs_std_info_to_fattr(&fattr,
987 (FIND_FILE_STANDARD_INFO *)find_entry,
990 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
991 cifs_fulldir_info_to_fattr(&fattr,
992 (SEARCH_ID_FULL_DIR_INFO *)find_entry,
996 cifs_dir_info_to_fattr(&fattr,
997 (FILE_DIRECTORY_INFO *)find_entry,
1002 if (de.ino && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
1003 fattr.cf_uniqueid = de.ino;
1005 fattr.cf_uniqueid = iunique(sb, ROOT_I);
1006 cifs_autodisable_serverino(cifs_sb);
1009 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) &&
1010 couldbe_mf_symlink(&fattr))
1012 * trying to get the type and mode can be slow,
1013 * so just call those regular files for now, and mark
1016 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
1018 cifs_prime_dcache(file_dentry(file), &name, &fattr);
1020 return !cifs_dir_emit(ctx, name.name, name.len,
1025 int cifs_readdir(struct file *file, struct dir_context *ctx)
1030 struct tcon_link *tlink = NULL;
1031 struct cifs_tcon *tcon;
1032 struct cifsFileInfo *cifsFile;
1033 char *current_entry;
1034 int num_to_fill = 0;
1035 char *tmp_buf = NULL;
1037 unsigned int max_len;
1038 const char *full_path;
1039 void *page = alloc_dentry_path();
1040 struct cached_fid *cfid = NULL;
1041 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
1045 full_path = build_path_from_dentry(file_dentry(file), page);
1046 if (IS_ERR(full_path)) {
1047 rc = PTR_ERR(full_path);
1051 if (file->private_data == NULL) {
1052 tlink = cifs_sb_tlink(cifs_sb);
1054 goto cache_not_found;
1055 tcon = tlink_tcon(tlink);
1057 cifsFile = file->private_data;
1058 tcon = tlink_tcon(cifsFile->tlink);
1061 rc = open_cached_dir(xid, tcon, full_path, cifs_sb, false, &cfid);
1062 cifs_put_tlink(tlink);
1064 goto cache_not_found;
1066 mutex_lock(&cfid->dirents.de_mutex);
1068 * If this was reading from the start of the directory
1069 * we need to initialize scanning and storing the
1070 * directory content.
1072 if (ctx->pos == 0 && cfid->dirents.ctx == NULL) {
1073 cfid->dirents.ctx = ctx;
1074 cfid->dirents.pos = 2;
1077 * If we already have the entire directory cached then
1078 * we can just serve the cache.
1080 if (cfid->dirents.is_valid) {
1081 if (!dir_emit_dots(file, ctx)) {
1082 mutex_unlock(&cfid->dirents.de_mutex);
1085 emit_cached_dirents(&cfid->dirents, ctx);
1086 mutex_unlock(&cfid->dirents.de_mutex);
1089 mutex_unlock(&cfid->dirents.de_mutex);
1091 /* Drop the cache while calling initiate_cifs_search and
1092 * find_cifs_entry in case there will be reconnects during
1095 close_cached_dir(cfid);
1100 * Ensure FindFirst doesn't fail before doing filldir() for '.' and
1101 * '..'. Otherwise we won't be able to notify VFS in case of failure.
1103 if (file->private_data == NULL) {
1104 rc = initiate_cifs_search(xid, file, full_path);
1105 cifs_dbg(FYI, "initiate cifs search rc %d\n", rc);
1110 if (!dir_emit_dots(file, ctx))
1113 /* 1) If search is active,
1114 is in current search buffer?
1115 if it before then restart search
1116 if after then keep searching till find it */
1117 cifsFile = file->private_data;
1118 if (cifsFile->srch_inf.endOfSearch) {
1119 if (cifsFile->srch_inf.emptyDir) {
1120 cifs_dbg(FYI, "End of search, empty dir\n");
1125 cifsFile->invalidHandle = true;
1126 tcon->ses->server->close(xid, tcon, &cifsFile->fid);
1129 tcon = tlink_tcon(cifsFile->tlink);
1130 rc = find_cifs_entry(xid, tcon, ctx->pos, file, full_path,
1131 ¤t_entry, &num_to_fill);
1132 open_cached_dir(xid, tcon, full_path, cifs_sb, false, &cfid);
1134 cifs_dbg(FYI, "fce error %d\n", rc);
1136 } else if (current_entry != NULL) {
1137 cifs_dbg(FYI, "entry %lld found\n", ctx->pos);
1140 mutex_lock(&cfid->dirents.de_mutex);
1141 finished_cached_dirents_count(&cfid->dirents, ctx);
1142 mutex_unlock(&cfid->dirents.de_mutex);
1144 cifs_dbg(FYI, "Could not find entry\n");
1147 cifs_dbg(FYI, "loop through %d times filling dir for net buf %p\n",
1148 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start);
1149 max_len = tcon->ses->server->ops->calc_smb_size(
1150 cifsFile->srch_inf.ntwrk_buf_start);
1151 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1153 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
1154 if (tmp_buf == NULL) {
1159 for (i = 0; i < num_to_fill; i++) {
1160 if (current_entry == NULL) {
1161 /* evaluate whether this case is an error */
1162 cifs_dbg(VFS, "past SMB end, num to fill %d i %d\n",
1167 * if buggy server returns . and .. late do we want to
1168 * check for that here?
1171 rc = cifs_filldir(current_entry, file, ctx,
1172 tmp_buf, max_len, cfid);
1181 mutex_lock(&cfid->dirents.de_mutex);
1182 update_cached_dirents_count(&cfid->dirents, ctx);
1183 mutex_unlock(&cfid->dirents.de_mutex);
1187 cifsFile->srch_inf.index_of_last_entry) {
1188 cifs_dbg(FYI, "last entry in buf at pos %lld %s\n",
1190 cifs_save_resume_key(current_entry, cifsFile);
1194 nxt_dir_entry(current_entry, end_of_smb,
1195 cifsFile->srch_inf.info_level);
1201 close_cached_dir(cfid);
1202 free_dentry_path(page);