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)
10 * This library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published
12 * by the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
18 * the GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/pagemap.h>
26 #include <linux/slab.h>
27 #include <linux/stat.h>
30 #include "cifsproto.h"
31 #include "cifs_unicode.h"
32 #include "cifs_debug.h"
33 #include "cifs_fs_sb.h"
35 #include "smb2proto.h"
36 #include "fs_context.h"
39 * To be safe - for UCS to UTF-8 with strings loaded with the rare long
40 * characters alloc more to account for such multibyte target UTF-8
43 #define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
45 #ifdef CONFIG_CIFS_DEBUG2
46 static void dump_cifs_file_struct(struct file *file, char *label)
48 struct cifsFileInfo *cf;
51 cf = file->private_data;
53 cifs_dbg(FYI, "empty cifs private file data\n");
56 if (cf->invalidHandle)
57 cifs_dbg(FYI, "Invalid handle\n");
58 if (cf->srch_inf.endOfSearch)
59 cifs_dbg(FYI, "end of search\n");
60 if (cf->srch_inf.emptyDir)
61 cifs_dbg(FYI, "empty dir\n");
65 static inline void dump_cifs_file_struct(struct file *file, char *label)
71 * Attempt to preload the dcache with the results from the FIND_FIRST/NEXT
73 * Find the dentry that matches "name". If there isn't one, create one. If it's
74 * a negative dentry or the uniqueid or filetype(mode) changed,
75 * then drop it and recreate it.
78 cifs_prime_dcache(struct dentry *parent, struct qstr *name,
79 struct cifs_fattr *fattr)
81 struct dentry *dentry, *alias;
83 struct super_block *sb = parent->d_sb;
84 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
85 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
87 cifs_dbg(FYI, "%s: for %s\n", __func__, name->name);
89 dentry = d_hash_and_lookup(parent, name);
92 * If we know that the inode will need to be revalidated
93 * immediately, then don't create a new dentry for it.
94 * We'll end up doing an on the wire call either way and
95 * this spares us an invalidation.
97 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
100 dentry = d_alloc_parallel(parent, name, &wq);
104 if (!d_in_lookup(dentry)) {
105 inode = d_inode(dentry);
107 if (d_mountpoint(dentry)) {
112 * If we're generating inode numbers, then we don't
113 * want to clobber the existing one with the one that
114 * the readdir code created.
116 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM))
117 fattr->cf_uniqueid = CIFS_I(inode)->uniqueid;
119 /* update inode in place
120 * if both i_ino and i_mode didn't change */
121 if (CIFS_I(inode)->uniqueid == fattr->cf_uniqueid &&
122 cifs_fattr_to_inode(inode, fattr) == 0) {
127 d_invalidate(dentry);
131 inode = cifs_iget(sb, fattr);
133 inode = ERR_PTR(-ENOMEM);
134 alias = d_splice_alias(inode, dentry);
135 d_lookup_done(dentry);
136 if (alias && !IS_ERR(alias))
142 static bool reparse_file_needs_reval(const struct cifs_fattr *fattr)
144 if (!(fattr->cf_cifsattrs & ATTR_REPARSE))
147 * The DFS tags should be only intepreted by server side as per
148 * MS-FSCC 2.1.2.1, but let's include them anyway.
150 * Besides, if cf_cifstag is unset (0), then we still need it to be
151 * revalidated to know exactly what reparse point it is.
153 switch (fattr->cf_cifstag) {
154 case IO_REPARSE_TAG_DFS:
155 case IO_REPARSE_TAG_DFSR:
156 case IO_REPARSE_TAG_SYMLINK:
157 case IO_REPARSE_TAG_NFS:
165 cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
167 fattr->cf_uid = cifs_sb->ctx->linux_uid;
168 fattr->cf_gid = cifs_sb->ctx->linux_gid;
171 * The IO_REPARSE_TAG_LX_ tags originally were used by WSL but they
172 * are preferred by the Linux client in some cases since, unlike
173 * the NFS reparse tag (or EAs), they don't require an extra query
174 * to determine which type of special file they represent.
175 * TODO: go through all documented reparse tags to see if we can
176 * reasonably map some of them to directories vs. files vs. symlinks
178 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
179 fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
180 fattr->cf_dtype = DT_DIR;
181 } else if (fattr->cf_cifstag == IO_REPARSE_TAG_LX_SYMLINK) {
182 fattr->cf_mode |= S_IFLNK | cifs_sb->ctx->file_mode;
183 fattr->cf_dtype = DT_LNK;
184 } else if (fattr->cf_cifstag == IO_REPARSE_TAG_LX_FIFO) {
185 fattr->cf_mode |= S_IFIFO | cifs_sb->ctx->file_mode;
186 fattr->cf_dtype = DT_FIFO;
187 } else if (fattr->cf_cifstag == IO_REPARSE_TAG_AF_UNIX) {
188 fattr->cf_mode |= S_IFSOCK | cifs_sb->ctx->file_mode;
189 fattr->cf_dtype = DT_SOCK;
190 } else if (fattr->cf_cifstag == IO_REPARSE_TAG_LX_CHR) {
191 fattr->cf_mode |= S_IFCHR | cifs_sb->ctx->file_mode;
192 fattr->cf_dtype = DT_CHR;
193 } else if (fattr->cf_cifstag == IO_REPARSE_TAG_LX_BLK) {
194 fattr->cf_mode |= S_IFBLK | cifs_sb->ctx->file_mode;
195 fattr->cf_dtype = DT_BLK;
196 } else { /* TODO: should we mark some other reparse points (like DFSR) as directories? */
197 fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
198 fattr->cf_dtype = DT_REG;
202 * We need to revalidate it further to make a decision about whether it
203 * is a symbolic link, DFS referral or a reparse point with a direct
204 * access like junctions, deduplicated files, NFS symlinks.
206 if (reparse_file_needs_reval(fattr))
207 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
209 /* non-unix readdir doesn't provide nlink */
210 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
212 if (fattr->cf_cifsattrs & ATTR_READONLY)
213 fattr->cf_mode &= ~S_IWUGO;
216 * We of course don't get ACL info in FIND_FIRST/NEXT results, so
217 * mark it for revalidation so that "ls -l" will look right. It might
218 * be super-slow, but if we don't do this then the ownership of files
219 * may look wrong since the inodes may not have timed out by the time
220 * "ls" does a stat() call on them.
222 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
223 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID))
224 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
226 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
227 fattr->cf_cifsattrs & ATTR_SYSTEM) {
228 if (fattr->cf_eof == 0) {
229 fattr->cf_mode &= ~S_IFMT;
230 fattr->cf_mode |= S_IFIFO;
231 fattr->cf_dtype = DT_FIFO;
234 * trying to get the type and mode via SFU can be slow,
235 * so just call those regular files for now, and mark
238 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
243 /* Fill a cifs_fattr struct with info from SMB_FIND_FILE_POSIX_INFO. */
245 cifs_posix_to_fattr(struct cifs_fattr *fattr, struct smb2_posix_info *info,
246 struct cifs_sb_info *cifs_sb)
248 struct smb2_posix_info_parsed parsed;
250 posix_info_parse(info, NULL, &parsed);
252 memset(fattr, 0, sizeof(*fattr));
253 fattr->cf_uniqueid = le64_to_cpu(info->Inode);
254 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
255 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
257 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
258 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
259 fattr->cf_ctime = cifs_NTtimeToUnix(info->CreationTime);
261 fattr->cf_nlink = le32_to_cpu(info->HardLinks);
262 fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
265 * Since we set the inode type below we need to mask off
266 * to avoid strange results if bits set above.
267 * XXX: why not make server&client use the type bits?
269 fattr->cf_mode = le32_to_cpu(info->Mode) & ~S_IFMT;
271 cifs_dbg(FYI, "posix fattr: dev %d, reparse %d, mode %o\n",
272 le32_to_cpu(info->DeviceId),
273 le32_to_cpu(info->ReparseTag),
274 le32_to_cpu(info->Mode));
276 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
277 fattr->cf_mode |= S_IFDIR;
278 fattr->cf_dtype = DT_DIR;
281 * mark anything that is not a dir as regular
282 * file. special files should have the REPARSE
283 * attribute and will be marked as needing revaluation
285 fattr->cf_mode |= S_IFREG;
286 fattr->cf_dtype = DT_REG;
289 if (reparse_file_needs_reval(fattr))
290 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
292 sid_to_id(cifs_sb, &parsed.owner, fattr, SIDOWNER);
293 sid_to_id(cifs_sb, &parsed.group, fattr, SIDGROUP);
296 static void __dir_info_to_fattr(struct cifs_fattr *fattr, const void *info)
298 const FILE_DIRECTORY_INFO *fi = info;
300 memset(fattr, 0, sizeof(*fattr));
301 fattr->cf_cifsattrs = le32_to_cpu(fi->ExtFileAttributes);
302 fattr->cf_eof = le64_to_cpu(fi->EndOfFile);
303 fattr->cf_bytes = le64_to_cpu(fi->AllocationSize);
304 fattr->cf_createtime = le64_to_cpu(fi->CreationTime);
305 fattr->cf_atime = cifs_NTtimeToUnix(fi->LastAccessTime);
306 fattr->cf_ctime = cifs_NTtimeToUnix(fi->ChangeTime);
307 fattr->cf_mtime = cifs_NTtimeToUnix(fi->LastWriteTime);
311 cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info,
312 struct cifs_sb_info *cifs_sb)
314 __dir_info_to_fattr(fattr, info);
315 cifs_fill_common_info(fattr, cifs_sb);
318 static void cifs_fulldir_info_to_fattr(struct cifs_fattr *fattr,
319 SEARCH_ID_FULL_DIR_INFO *info,
320 struct cifs_sb_info *cifs_sb)
322 __dir_info_to_fattr(fattr, info);
324 /* See MS-FSCC 2.4.18 FileIdFullDirectoryInformation */
325 if (fattr->cf_cifsattrs & ATTR_REPARSE)
326 fattr->cf_cifstag = le32_to_cpu(info->EaSize);
327 cifs_fill_common_info(fattr, cifs_sb);
331 cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info,
332 struct cifs_sb_info *cifs_sb)
334 int offset = cifs_sb_master_tcon(cifs_sb)->ses->server->timeAdj;
336 memset(fattr, 0, sizeof(*fattr));
337 fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate,
338 info->LastAccessTime, offset);
339 fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate,
340 info->LastWriteTime, offset);
341 fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate,
342 info->LastWriteTime, offset);
344 fattr->cf_cifsattrs = le16_to_cpu(info->Attributes);
345 fattr->cf_bytes = le32_to_cpu(info->AllocationSize);
346 fattr->cf_eof = le32_to_cpu(info->DataSize);
348 cifs_fill_common_info(fattr, cifs_sb);
351 /* BB eventually need to add the following helper function to
352 resolve NT_STATUS_STOPPED_ON_SYMLINK return code when
353 we try to do FindFirst on (NTFS) directory symlinks */
355 int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
362 struct cifs_tcon *ptcon = cifs_sb_tcon(cifs_sb);
365 rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ,
366 OPEN_REPARSE_POINT, &fid, &oplock, NULL,
370 tmpbuffer = kmalloc(maxpath);
371 rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path,
376 if (CIFSSMBClose(xid, ptcon, fid)) {
377 cifs_dbg(FYI, "Error closing temporary reparsepoint open\n");
384 initiate_cifs_search(const unsigned int xid, struct file *file,
389 struct cifsFileInfo *cifsFile;
390 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
391 struct tcon_link *tlink = NULL;
392 struct cifs_tcon *tcon;
393 struct TCP_Server_Info *server;
395 if (file->private_data == NULL) {
396 tlink = cifs_sb_tlink(cifs_sb);
398 return PTR_ERR(tlink);
400 cifsFile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
401 if (cifsFile == NULL) {
405 spin_lock_init(&cifsFile->file_info_lock);
406 file->private_data = cifsFile;
407 cifsFile->tlink = cifs_get_tlink(tlink);
408 tcon = tlink_tcon(tlink);
410 cifsFile = file->private_data;
411 tcon = tlink_tcon(cifsFile->tlink);
414 server = tcon->ses->server;
416 if (!server->ops->query_dir_first) {
421 cifsFile->invalidHandle = true;
422 cifsFile->srch_inf.endOfSearch = false;
424 cifs_dbg(FYI, "Full path: %s start at: %lld\n", full_path, file->f_pos);
427 /* test for Unix extensions */
428 /* but now check for them on the share/mount not on the SMB session */
429 /* if (cap_unix(tcon->ses) { */
431 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
432 else if (tcon->posix_extensions)
433 cifsFile->srch_inf.info_level = SMB_FIND_FILE_POSIX_INFO;
434 else if ((tcon->ses->capabilities &
435 tcon->ses->server->vals->cap_nt_find) == 0) {
436 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
437 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
438 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
439 } else /* not srvinos - BB fixme add check for backlevel? */ {
440 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
443 search_flags = CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME;
444 if (backup_cred(cifs_sb))
445 search_flags |= CIFS_SEARCH_BACKUP_SEARCH;
447 rc = server->ops->query_dir_first(xid, tcon, full_path, cifs_sb,
448 &cifsFile->fid, search_flags,
449 &cifsFile->srch_inf);
452 cifsFile->invalidHandle = false;
453 /* BB add following call to handle readdir on new NTFS symlink errors
454 else if STATUS_STOPPED_ON_SYMLINK
455 call get_symlink_reparse_path and retry with new path */
456 else if ((rc == -EOPNOTSUPP) &&
457 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
458 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
462 cifs_put_tlink(tlink);
466 /* return length of unicode string in bytes */
467 static int cifs_unicode_bytelen(const char *str)
470 const __le16 *ustr = (const __le16 *)str;
472 for (len = 0; len <= PATH_MAX; len++) {
476 cifs_dbg(FYI, "Unicode string longer than PATH_MAX found\n");
480 static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
483 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
485 if (level == SMB_FIND_FILE_INFO_STANDARD) {
486 FIND_FILE_STANDARD_INFO *pfData;
487 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
489 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
490 pfData->FileNameLength;
492 u32 next_offset = le32_to_cpu(pDirInfo->NextEntryOffset);
494 if (old_entry + next_offset < old_entry) {
495 cifs_dbg(VFS, "Invalid offset %u\n", next_offset);
498 new_entry = old_entry + next_offset;
500 cifs_dbg(FYI, "new entry %p old entry %p\n", new_entry, old_entry);
501 /* validate that new_entry is not past end of SMB */
502 if (new_entry >= end_of_smb) {
503 cifs_dbg(VFS, "search entry %p began after end of SMB %p old entry %p\n",
504 new_entry, end_of_smb, old_entry);
506 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
507 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
508 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
509 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
510 cifs_dbg(VFS, "search entry %p extends after end of SMB %p\n",
511 new_entry, end_of_smb);
525 static void cifs_fill_dirent_posix(struct cifs_dirent *de,
526 const struct smb2_posix_info *info)
528 struct smb2_posix_info_parsed parsed;
530 /* payload should have already been checked at this point */
531 if (posix_info_parse(info, NULL, &parsed) < 0) {
532 cifs_dbg(VFS, "Invalid POSIX info payload\n");
536 de->name = parsed.name;
537 de->namelen = parsed.name_len;
538 de->resume_key = info->Ignored;
539 de->ino = le64_to_cpu(info->Inode);
542 static void cifs_fill_dirent_unix(struct cifs_dirent *de,
543 const FILE_UNIX_INFO *info, bool is_unicode)
545 de->name = &info->FileName[0];
547 de->namelen = cifs_unicode_bytelen(de->name);
549 de->namelen = strnlen(de->name, PATH_MAX);
550 de->resume_key = info->ResumeKey;
551 de->ino = le64_to_cpu(info->basic.UniqueId);
554 static void cifs_fill_dirent_dir(struct cifs_dirent *de,
555 const FILE_DIRECTORY_INFO *info)
557 de->name = &info->FileName[0];
558 de->namelen = le32_to_cpu(info->FileNameLength);
559 de->resume_key = info->FileIndex;
562 static void cifs_fill_dirent_full(struct cifs_dirent *de,
563 const FILE_FULL_DIRECTORY_INFO *info)
565 de->name = &info->FileName[0];
566 de->namelen = le32_to_cpu(info->FileNameLength);
567 de->resume_key = info->FileIndex;
570 static void cifs_fill_dirent_search(struct cifs_dirent *de,
571 const SEARCH_ID_FULL_DIR_INFO *info)
573 de->name = &info->FileName[0];
574 de->namelen = le32_to_cpu(info->FileNameLength);
575 de->resume_key = info->FileIndex;
576 de->ino = le64_to_cpu(info->UniqueId);
579 static void cifs_fill_dirent_both(struct cifs_dirent *de,
580 const FILE_BOTH_DIRECTORY_INFO *info)
582 de->name = &info->FileName[0];
583 de->namelen = le32_to_cpu(info->FileNameLength);
584 de->resume_key = info->FileIndex;
587 static void cifs_fill_dirent_std(struct cifs_dirent *de,
588 const FIND_FILE_STANDARD_INFO *info)
590 de->name = &info->FileName[0];
591 /* one byte length, no endianess conversion */
592 de->namelen = info->FileNameLength;
593 de->resume_key = info->ResumeKey;
596 static int cifs_fill_dirent(struct cifs_dirent *de, const void *info,
597 u16 level, bool is_unicode)
599 memset(de, 0, sizeof(*de));
602 case SMB_FIND_FILE_POSIX_INFO:
603 cifs_fill_dirent_posix(de, info);
605 case SMB_FIND_FILE_UNIX:
606 cifs_fill_dirent_unix(de, info, is_unicode);
608 case SMB_FIND_FILE_DIRECTORY_INFO:
609 cifs_fill_dirent_dir(de, info);
611 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
612 cifs_fill_dirent_full(de, info);
614 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
615 cifs_fill_dirent_search(de, info);
617 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
618 cifs_fill_dirent_both(de, info);
620 case SMB_FIND_FILE_INFO_STANDARD:
621 cifs_fill_dirent_std(de, info);
624 cifs_dbg(FYI, "Unknown findfirst level %d\n", level);
631 #define UNICODE_DOT cpu_to_le16(0x2e)
633 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
634 static int cifs_entry_is_dot(struct cifs_dirent *de, bool is_unicode)
642 __le16 *ufilename = (__le16 *)de->name;
643 if (de->namelen == 2) {
645 if (ufilename[0] == UNICODE_DOT)
647 } else if (de->namelen == 4) {
649 if (ufilename[0] == UNICODE_DOT &&
650 ufilename[1] == UNICODE_DOT)
654 if (de->namelen == 1) {
655 if (de->name[0] == '.')
657 } else if (de->namelen == 2) {
658 if (de->name[0] == '.' && de->name[1] == '.')
666 /* Check if directory that we are searching has changed so we can decide
667 whether we can use the cached search results from the previous search */
668 static int is_dir_changed(struct file *file)
670 struct inode *inode = file_inode(file);
671 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
673 if (cifsInfo->time == 0)
674 return 1; /* directory was changed, perhaps due to unlink */
680 static int cifs_save_resume_key(const char *current_entry,
681 struct cifsFileInfo *file_info)
683 struct cifs_dirent de;
686 rc = cifs_fill_dirent(&de, current_entry, file_info->srch_inf.info_level,
687 file_info->srch_inf.unicode);
689 file_info->srch_inf.presume_name = de.name;
690 file_info->srch_inf.resume_name_len = de.namelen;
691 file_info->srch_inf.resume_key = de.resume_key;
697 * Find the corresponding entry in the search. Note that the SMB server returns
698 * search entries for . and .. which complicates logic here if we choose to
699 * parse for them and we do not assume that they are located in the findfirst
700 * return buffer. We start counting in the buffer with entry 2 and increment for
701 * every entry (do not increment for . or .. entry).
704 find_cifs_entry(const unsigned int xid, struct cifs_tcon *tcon, loff_t pos,
705 struct file *file, char *full_path,
706 char **current_entry, int *num_to_ret)
711 loff_t first_entry_in_buffer;
712 loff_t index_to_find = pos;
713 struct cifsFileInfo *cfile = file->private_data;
714 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
715 struct TCP_Server_Info *server = tcon->ses->server;
716 /* check if index in the buffer */
718 if (!server->ops->query_dir_first || !server->ops->query_dir_next)
721 if ((cfile == NULL) || (current_entry == NULL) || (num_to_ret == NULL))
724 *current_entry = NULL;
725 first_entry_in_buffer = cfile->srch_inf.index_of_last_entry -
726 cfile->srch_inf.entries_in_buffer;
729 * If first entry in buf is zero then is first buffer
730 * in search response data which means it is likely . and ..
731 * will be in this buffer, although some servers do not return
732 * . and .. for the root of a drive and for those we need
733 * to start two entries earlier.
736 dump_cifs_file_struct(file, "In fce ");
737 if (((index_to_find < cfile->srch_inf.index_of_last_entry) &&
738 is_dir_changed(file)) || (index_to_find < first_entry_in_buffer)) {
739 /* close and restart search */
740 cifs_dbg(FYI, "search backing up - close and restart search\n");
741 spin_lock(&cfile->file_info_lock);
742 if (server->ops->dir_needs_close(cfile)) {
743 cfile->invalidHandle = true;
744 spin_unlock(&cfile->file_info_lock);
745 if (server->ops->close_dir)
746 server->ops->close_dir(xid, tcon, &cfile->fid);
748 spin_unlock(&cfile->file_info_lock);
749 if (cfile->srch_inf.ntwrk_buf_start) {
750 cifs_dbg(FYI, "freeing SMB ff cache buf on search rewind\n");
751 if (cfile->srch_inf.smallBuf)
752 cifs_small_buf_release(cfile->srch_inf.
755 cifs_buf_release(cfile->srch_inf.
757 cfile->srch_inf.ntwrk_buf_start = NULL;
759 rc = initiate_cifs_search(xid, file, full_path);
761 cifs_dbg(FYI, "error %d reinitiating a search on rewind\n",
765 /* FindFirst/Next set last_entry to NULL on malformed reply */
766 if (cfile->srch_inf.last_entry)
767 cifs_save_resume_key(cfile->srch_inf.last_entry, cfile);
770 search_flags = CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME;
771 if (backup_cred(cifs_sb))
772 search_flags |= CIFS_SEARCH_BACKUP_SEARCH;
774 while ((index_to_find >= cfile->srch_inf.index_of_last_entry) &&
775 (rc == 0) && !cfile->srch_inf.endOfSearch) {
776 cifs_dbg(FYI, "calling findnext2\n");
777 rc = server->ops->query_dir_next(xid, tcon, &cfile->fid,
780 /* FindFirst/Next set last_entry to NULL on malformed reply */
781 if (cfile->srch_inf.last_entry)
782 cifs_save_resume_key(cfile->srch_inf.last_entry, cfile);
786 if (index_to_find < cfile->srch_inf.index_of_last_entry) {
787 /* we found the buffer that contains the entry */
788 /* scan and find it */
793 if (cfile->srch_inf.ntwrk_buf_start == NULL) {
794 cifs_dbg(VFS, "ntwrk_buf_start is NULL during readdir\n");
798 end_of_smb = cfile->srch_inf.ntwrk_buf_start +
799 server->ops->calc_smb_size(
800 cfile->srch_inf.ntwrk_buf_start,
803 cur_ent = cfile->srch_inf.srch_entries_start;
804 first_entry_in_buffer = cfile->srch_inf.index_of_last_entry
805 - cfile->srch_inf.entries_in_buffer;
806 pos_in_buf = index_to_find - first_entry_in_buffer;
807 cifs_dbg(FYI, "found entry - pos_in_buf %d\n", pos_in_buf);
809 for (i = 0; (i < (pos_in_buf)) && (cur_ent != NULL); i++) {
810 /* go entry by entry figuring out which is first */
811 cur_ent = nxt_dir_entry(cur_ent, end_of_smb,
812 cfile->srch_inf.info_level);
814 if ((cur_ent == NULL) && (i < pos_in_buf)) {
815 /* BB fixme - check if we should flag this error */
816 cifs_dbg(VFS, "reached end of buf searching for pos in buf %d index to find %lld rc %d\n",
817 pos_in_buf, index_to_find, rc);
820 *current_entry = cur_ent;
822 cifs_dbg(FYI, "index not in buffer - could not findnext into it\n");
826 if (pos_in_buf >= cfile->srch_inf.entries_in_buffer) {
827 cifs_dbg(FYI, "can not return entries pos_in_buf beyond last\n");
830 *num_to_ret = cfile->srch_inf.entries_in_buffer - pos_in_buf;
835 static int cifs_filldir(char *find_entry, struct file *file,
836 struct dir_context *ctx,
837 char *scratch_buf, unsigned int max_len)
839 struct cifsFileInfo *file_info = file->private_data;
840 struct super_block *sb = file_inode(file)->i_sb;
841 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
842 struct cifs_dirent de = { NULL, };
843 struct cifs_fattr fattr;
848 rc = cifs_fill_dirent(&de, find_entry, file_info->srch_inf.info_level,
849 file_info->srch_inf.unicode);
853 if (de.namelen > max_len) {
854 cifs_dbg(VFS, "bad search response length %zd past smb end\n",
859 /* skip . and .. since we added them first */
860 if (cifs_entry_is_dot(&de, file_info->srch_inf.unicode))
863 if (file_info->srch_inf.unicode) {
864 struct nls_table *nlt = cifs_sb->local_nls;
867 map_type = cifs_remap(cifs_sb);
868 name.name = scratch_buf;
870 cifs_from_utf16((char *)name.name, (__le16 *)de.name,
872 min_t(size_t, de.namelen,
873 (size_t)max_len), nlt, map_type);
874 name.len -= nls_nullsize(nlt);
877 name.len = de.namelen;
880 switch (file_info->srch_inf.info_level) {
881 case SMB_FIND_FILE_POSIX_INFO:
882 cifs_posix_to_fattr(&fattr,
883 (struct smb2_posix_info *)find_entry,
886 case SMB_FIND_FILE_UNIX:
887 cifs_unix_basic_to_fattr(&fattr,
888 &((FILE_UNIX_INFO *)find_entry)->basic,
891 case SMB_FIND_FILE_INFO_STANDARD:
892 cifs_std_info_to_fattr(&fattr,
893 (FIND_FILE_STANDARD_INFO *)find_entry,
896 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
897 cifs_fulldir_info_to_fattr(&fattr,
898 (SEARCH_ID_FULL_DIR_INFO *)find_entry,
902 cifs_dir_info_to_fattr(&fattr,
903 (FILE_DIRECTORY_INFO *)find_entry,
908 if (de.ino && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
909 fattr.cf_uniqueid = de.ino;
911 fattr.cf_uniqueid = iunique(sb, ROOT_I);
912 cifs_autodisable_serverino(cifs_sb);
915 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) &&
916 couldbe_mf_symlink(&fattr))
918 * trying to get the type and mode can be slow,
919 * so just call those regular files for now, and mark
922 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
924 cifs_prime_dcache(file_dentry(file), &name, &fattr);
926 ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
927 return !dir_emit(ctx, name.name, name.len, ino, fattr.cf_dtype);
931 int cifs_readdir(struct file *file, struct dir_context *ctx)
936 struct cifs_tcon *tcon;
937 struct cifsFileInfo *cifsFile = NULL;
940 char *tmp_buf = NULL;
942 unsigned int max_len;
943 char *full_path = NULL;
947 full_path = build_path_from_dentry(file_dentry(file));
948 if (full_path == NULL) {
954 * Ensure FindFirst doesn't fail before doing filldir() for '.' and
955 * '..'. Otherwise we won't be able to notify VFS in case of failure.
957 if (file->private_data == NULL) {
958 rc = initiate_cifs_search(xid, file, full_path);
959 cifs_dbg(FYI, "initiate cifs search rc %d\n", rc);
964 if (!dir_emit_dots(file, ctx))
967 /* 1) If search is active,
968 is in current search buffer?
969 if it before then restart search
970 if after then keep searching till find it */
972 cifsFile = file->private_data;
973 if (cifsFile->srch_inf.endOfSearch) {
974 if (cifsFile->srch_inf.emptyDir) {
975 cifs_dbg(FYI, "End of search, empty dir\n");
980 cifsFile->invalidHandle = true;
981 tcon->ses->server->close(xid, tcon, &cifsFile->fid);
984 tcon = tlink_tcon(cifsFile->tlink);
985 rc = find_cifs_entry(xid, tcon, ctx->pos, file, full_path,
986 ¤t_entry, &num_to_fill);
988 cifs_dbg(FYI, "fce error %d\n", rc);
990 } else if (current_entry != NULL) {
991 cifs_dbg(FYI, "entry %lld found\n", ctx->pos);
993 cifs_dbg(FYI, "Could not find entry\n");
996 cifs_dbg(FYI, "loop through %d times filling dir for net buf %p\n",
997 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start);
998 max_len = tcon->ses->server->ops->calc_smb_size(
999 cifsFile->srch_inf.ntwrk_buf_start,
1001 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1003 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
1004 if (tmp_buf == NULL) {
1009 for (i = 0; i < num_to_fill; i++) {
1010 if (current_entry == NULL) {
1011 /* evaluate whether this case is an error */
1012 cifs_dbg(VFS, "past SMB end, num to fill %d i %d\n",
1017 * if buggy server returns . and .. late do we want to
1018 * check for that here?
1021 rc = cifs_filldir(current_entry, file, ctx,
1031 cifsFile->srch_inf.index_of_last_entry) {
1032 cifs_dbg(FYI, "last entry in buf at pos %lld %s\n",
1034 cifs_save_resume_key(current_entry, cifsFile);
1038 nxt_dir_entry(current_entry, end_of_smb,
1039 cifsFile->srch_inf.info_level);