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"
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 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq
);
77 cifs_dbg(FYI
, "%s: for %s\n", __func__
, name
->name
);
79 dentry
= d_hash_and_lookup(parent
, name
);
82 * If we know that the inode will need to be revalidated
83 * immediately, then don't create a new dentry for it.
84 * We'll end up doing an on the wire call either way and
85 * this spares us an invalidation.
88 if ((fattr
->cf_cifsattrs
& ATTR_REPARSE
) ||
89 (fattr
->cf_flags
& CIFS_FATTR_NEED_REVAL
))
92 dentry
= d_alloc_parallel(parent
, name
, &wq
);
96 if (!d_in_lookup(dentry
)) {
97 inode
= d_inode(dentry
);
99 if (d_mountpoint(dentry
)) {
104 * If we're generating inode numbers, then we don't
105 * want to clobber the existing one with the one that
106 * the readdir code created.
108 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SERVER_INUM
))
109 fattr
->cf_uniqueid
= CIFS_I(inode
)->uniqueid
;
112 * Update inode in place if both i_ino and i_mode didn't
115 if (CIFS_I(inode
)->uniqueid
== fattr
->cf_uniqueid
) {
117 * Query dir responses don't provide enough
118 * information about reparse points other than
119 * their reparse tags. Save an invalidation by
120 * not clobbering some existing attributes when
121 * reparse tag and ctime haven't changed.
124 if (fattr
->cf_cifsattrs
& ATTR_REPARSE
) {
125 if (likely(reparse_inode_match(inode
, fattr
))) {
126 fattr
->cf_mode
= inode
->i_mode
;
127 fattr
->cf_rdev
= inode
->i_rdev
;
128 fattr
->cf_uid
= inode
->i_uid
;
129 fattr
->cf_gid
= inode
->i_gid
;
130 fattr
->cf_eof
= CIFS_I(inode
)->netfs
.remote_i_size
;
131 fattr
->cf_symlink_target
= NULL
;
133 CIFS_I(inode
)->time
= 0;
137 if (!rc
&& !cifs_fattr_to_inode(inode
, fattr
, true)) {
143 d_invalidate(dentry
);
147 inode
= cifs_iget(sb
, fattr
);
149 inode
= ERR_PTR(-ENOMEM
);
150 alias
= d_splice_alias(inode
, dentry
);
151 d_lookup_done(dentry
);
152 if (alias
&& !IS_ERR(alias
))
159 cifs_fill_common_info(struct cifs_fattr
*fattr
, struct cifs_sb_info
*cifs_sb
)
161 struct cifs_open_info_data data
= {
162 .reparse
= { .tag
= fattr
->cf_cifstag
, },
165 fattr
->cf_uid
= cifs_sb
->ctx
->linux_uid
;
166 fattr
->cf_gid
= cifs_sb
->ctx
->linux_gid
;
169 * The IO_REPARSE_TAG_LX_ tags originally were used by WSL but they
170 * are preferred by the Linux client in some cases since, unlike
171 * the NFS reparse tag (or EAs), they don't require an extra query
172 * to determine which type of special file they represent.
173 * TODO: go through all documented reparse tags to see if we can
174 * reasonably map some of them to directories vs. files vs. symlinks
176 if ((fattr
->cf_cifsattrs
& ATTR_REPARSE
) &&
177 cifs_reparse_point_to_fattr(cifs_sb
, fattr
, &data
))
180 if (fattr
->cf_cifsattrs
& ATTR_DIRECTORY
) {
181 fattr
->cf_mode
= S_IFDIR
| cifs_sb
->ctx
->dir_mode
;
182 fattr
->cf_dtype
= DT_DIR
;
184 fattr
->cf_mode
= S_IFREG
| cifs_sb
->ctx
->file_mode
;
185 fattr
->cf_dtype
= DT_REG
;
189 /* non-unix readdir doesn't provide nlink */
190 fattr
->cf_flags
|= CIFS_FATTR_UNKNOWN_NLINK
;
192 if (fattr
->cf_cifsattrs
& ATTR_READONLY
)
193 fattr
->cf_mode
&= ~S_IWUGO
;
196 * We of course don't get ACL info in FIND_FIRST/NEXT results, so
197 * mark it for revalidation so that "ls -l" will look right. It might
198 * be super-slow, but if we don't do this then the ownership of files
199 * may look wrong since the inodes may not have timed out by the time
200 * "ls" does a stat() call on them.
202 if ((cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_CIFS_ACL
) ||
203 (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MODE_FROM_SID
))
204 fattr
->cf_flags
|= CIFS_FATTR_NEED_REVAL
;
206 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_UNX_EMUL
&&
207 fattr
->cf_cifsattrs
& ATTR_SYSTEM
) {
208 if (fattr
->cf_eof
== 0) {
209 fattr
->cf_mode
&= ~S_IFMT
;
210 fattr
->cf_mode
|= S_IFIFO
;
211 fattr
->cf_dtype
= DT_FIFO
;
214 * trying to get the type and mode via SFU can be slow,
215 * so just call those regular files for now, and mark
218 fattr
->cf_flags
|= CIFS_FATTR_NEED_REVAL
;
223 /* Fill a cifs_fattr struct with info from SMB_FIND_FILE_POSIX_INFO. */
225 cifs_posix_to_fattr(struct cifs_fattr
*fattr
, struct smb2_posix_info
*info
,
226 struct cifs_sb_info
*cifs_sb
)
228 struct smb2_posix_info_parsed parsed
;
230 posix_info_parse(info
, NULL
, &parsed
);
232 memset(fattr
, 0, sizeof(*fattr
));
233 fattr
->cf_uniqueid
= le64_to_cpu(info
->Inode
);
234 fattr
->cf_bytes
= le64_to_cpu(info
->AllocationSize
);
235 fattr
->cf_eof
= le64_to_cpu(info
->EndOfFile
);
237 fattr
->cf_atime
= cifs_NTtimeToUnix(info
->LastAccessTime
);
238 fattr
->cf_mtime
= cifs_NTtimeToUnix(info
->LastWriteTime
);
239 fattr
->cf_ctime
= cifs_NTtimeToUnix(info
->CreationTime
);
241 fattr
->cf_nlink
= le32_to_cpu(info
->HardLinks
);
242 fattr
->cf_cifsattrs
= le32_to_cpu(info
->DosAttributes
);
245 * Since we set the inode type below we need to mask off
246 * to avoid strange results if bits set above.
247 * XXX: why not make server&client use the type bits?
249 fattr
->cf_mode
= le32_to_cpu(info
->Mode
) & ~S_IFMT
;
251 cifs_dbg(FYI
, "posix fattr: dev %d, reparse %d, mode %o\n",
252 le32_to_cpu(info
->DeviceId
),
253 le32_to_cpu(info
->ReparseTag
),
254 le32_to_cpu(info
->Mode
));
256 if (fattr
->cf_cifsattrs
& ATTR_DIRECTORY
) {
257 fattr
->cf_mode
|= S_IFDIR
;
258 fattr
->cf_dtype
= DT_DIR
;
261 * mark anything that is not a dir as regular
262 * file. special files should have the REPARSE
263 * attribute and will be marked as needing revaluation
265 fattr
->cf_mode
|= S_IFREG
;
266 fattr
->cf_dtype
= DT_REG
;
269 sid_to_id(cifs_sb
, &parsed
.owner
, fattr
, SIDOWNER
);
270 sid_to_id(cifs_sb
, &parsed
.group
, fattr
, SIDGROUP
);
273 static void __dir_info_to_fattr(struct cifs_fattr
*fattr
, const void *info
)
275 const FILE_DIRECTORY_INFO
*fi
= info
;
277 memset(fattr
, 0, sizeof(*fattr
));
278 fattr
->cf_cifsattrs
= le32_to_cpu(fi
->ExtFileAttributes
);
279 fattr
->cf_eof
= le64_to_cpu(fi
->EndOfFile
);
280 fattr
->cf_bytes
= le64_to_cpu(fi
->AllocationSize
);
281 fattr
->cf_createtime
= le64_to_cpu(fi
->CreationTime
);
282 fattr
->cf_atime
= cifs_NTtimeToUnix(fi
->LastAccessTime
);
283 fattr
->cf_ctime
= cifs_NTtimeToUnix(fi
->ChangeTime
);
284 fattr
->cf_mtime
= cifs_NTtimeToUnix(fi
->LastWriteTime
);
288 cifs_dir_info_to_fattr(struct cifs_fattr
*fattr
, FILE_DIRECTORY_INFO
*info
,
289 struct cifs_sb_info
*cifs_sb
)
291 __dir_info_to_fattr(fattr
, info
);
292 cifs_fill_common_info(fattr
, cifs_sb
);
295 static void cifs_fulldir_info_to_fattr(struct cifs_fattr
*fattr
,
297 struct cifs_sb_info
*cifs_sb
)
299 const FILE_FULL_DIRECTORY_INFO
*di
= info
;
301 __dir_info_to_fattr(fattr
, info
);
303 /* See MS-FSCC 2.4.14, 2.4.19 */
304 if (fattr
->cf_cifsattrs
& ATTR_REPARSE
)
305 fattr
->cf_cifstag
= le32_to_cpu(di
->EaSize
);
306 cifs_fill_common_info(fattr
, cifs_sb
);
310 cifs_std_info_to_fattr(struct cifs_fattr
*fattr
, FIND_FILE_STANDARD_INFO
*info
,
311 struct cifs_sb_info
*cifs_sb
)
313 int offset
= cifs_sb_master_tcon(cifs_sb
)->ses
->server
->timeAdj
;
315 memset(fattr
, 0, sizeof(*fattr
));
316 fattr
->cf_atime
= cnvrtDosUnixTm(info
->LastAccessDate
,
317 info
->LastAccessTime
, offset
);
318 fattr
->cf_ctime
= cnvrtDosUnixTm(info
->LastWriteDate
,
319 info
->LastWriteTime
, offset
);
320 fattr
->cf_mtime
= cnvrtDosUnixTm(info
->LastWriteDate
,
321 info
->LastWriteTime
, offset
);
323 fattr
->cf_cifsattrs
= le16_to_cpu(info
->Attributes
);
324 fattr
->cf_bytes
= le32_to_cpu(info
->AllocationSize
);
325 fattr
->cf_eof
= le32_to_cpu(info
->DataSize
);
327 cifs_fill_common_info(fattr
, cifs_sb
);
331 _initiate_cifs_search(const unsigned int xid
, struct file
*file
,
332 const char *full_path
)
336 struct cifsFileInfo
*cifsFile
;
337 struct cifs_sb_info
*cifs_sb
= CIFS_FILE_SB(file
);
338 struct tcon_link
*tlink
= NULL
;
339 struct cifs_tcon
*tcon
;
340 struct TCP_Server_Info
*server
;
342 if (file
->private_data
== NULL
) {
343 tlink
= cifs_sb_tlink(cifs_sb
);
345 return PTR_ERR(tlink
);
347 cifsFile
= kzalloc(sizeof(struct cifsFileInfo
), GFP_KERNEL
);
348 if (cifsFile
== NULL
) {
352 spin_lock_init(&cifsFile
->file_info_lock
);
353 file
->private_data
= cifsFile
;
354 cifsFile
->tlink
= cifs_get_tlink(tlink
);
355 tcon
= tlink_tcon(tlink
);
357 cifsFile
= file
->private_data
;
358 tcon
= tlink_tcon(cifsFile
->tlink
);
361 server
= tcon
->ses
->server
;
363 if (!server
->ops
->query_dir_first
) {
368 cifsFile
->invalidHandle
= true;
369 cifsFile
->srch_inf
.endOfSearch
= false;
371 cifs_dbg(FYI
, "Full path: %s start at: %lld\n", full_path
, file
->f_pos
);
374 /* test for Unix extensions */
375 /* but now check for them on the share/mount not on the SMB session */
376 /* if (cap_unix(tcon->ses) { */
378 cifsFile
->srch_inf
.info_level
= SMB_FIND_FILE_UNIX
;
379 else if (tcon
->posix_extensions
)
380 cifsFile
->srch_inf
.info_level
= SMB_FIND_FILE_POSIX_INFO
;
381 else if ((tcon
->ses
->capabilities
&
382 tcon
->ses
->server
->vals
->cap_nt_find
) == 0) {
383 cifsFile
->srch_inf
.info_level
= SMB_FIND_FILE_INFO_STANDARD
;
384 } else if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SERVER_INUM
) {
385 cifsFile
->srch_inf
.info_level
= SMB_FIND_FILE_ID_FULL_DIR_INFO
;
386 } else /* not srvinos - BB fixme add check for backlevel? */ {
387 cifsFile
->srch_inf
.info_level
= SMB_FIND_FILE_FULL_DIRECTORY_INFO
;
390 search_flags
= CIFS_SEARCH_CLOSE_AT_END
| CIFS_SEARCH_RETURN_RESUME
;
391 if (backup_cred(cifs_sb
))
392 search_flags
|= CIFS_SEARCH_BACKUP_SEARCH
;
394 rc
= server
->ops
->query_dir_first(xid
, tcon
, full_path
, cifs_sb
,
395 &cifsFile
->fid
, search_flags
,
396 &cifsFile
->srch_inf
);
399 cifsFile
->invalidHandle
= false;
400 } else if ((rc
== -EOPNOTSUPP
) &&
401 (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SERVER_INUM
)) {
402 cifs_sb
->mnt_cifs_flags
&= ~CIFS_MOUNT_SERVER_INUM
;
406 cifs_put_tlink(tlink
);
411 initiate_cifs_search(const unsigned int xid
, struct file
*file
,
412 const char *full_path
)
414 int rc
, retry_count
= 0;
417 rc
= _initiate_cifs_search(xid
, file
, full_path
);
419 * If we don't have enough credits to start reading the
420 * directory just try again after short wait.
425 usleep_range(512, 2048);
426 } while (retry_count
++ < 5);
431 /* return length of unicode string in bytes */
432 static int cifs_unicode_bytelen(const char *str
)
435 const __le16
*ustr
= (const __le16
*)str
;
437 for (len
= 0; len
<= PATH_MAX
; len
++) {
441 cifs_dbg(FYI
, "Unicode string longer than PATH_MAX found\n");
445 static char *nxt_dir_entry(char *old_entry
, char *end_of_smb
, int level
)
448 FILE_DIRECTORY_INFO
*pDirInfo
= (FILE_DIRECTORY_INFO
*)old_entry
;
450 if (level
== SMB_FIND_FILE_INFO_STANDARD
) {
451 FIND_FILE_STANDARD_INFO
*pfData
;
452 pfData
= (FIND_FILE_STANDARD_INFO
*)pDirInfo
;
454 new_entry
= old_entry
+ sizeof(FIND_FILE_STANDARD_INFO
) + 1 +
455 pfData
->FileNameLength
;
457 u32 next_offset
= le32_to_cpu(pDirInfo
->NextEntryOffset
);
459 if (old_entry
+ next_offset
< old_entry
) {
460 cifs_dbg(VFS
, "Invalid offset %u\n", next_offset
);
463 new_entry
= old_entry
+ next_offset
;
465 cifs_dbg(FYI
, "new entry %p old entry %p\n", new_entry
, old_entry
);
466 /* validate that new_entry is not past end of SMB */
467 if (new_entry
>= end_of_smb
) {
468 cifs_dbg(VFS
, "search entry %p began after end of SMB %p old entry %p\n",
469 new_entry
, end_of_smb
, old_entry
);
471 } else if (((level
== SMB_FIND_FILE_INFO_STANDARD
) &&
472 (new_entry
+ sizeof(FIND_FILE_STANDARD_INFO
) + 1 > end_of_smb
))
473 || ((level
!= SMB_FIND_FILE_INFO_STANDARD
) &&
474 (new_entry
+ sizeof(FILE_DIRECTORY_INFO
) + 1 > end_of_smb
))) {
475 cifs_dbg(VFS
, "search entry %p extends after end of SMB %p\n",
476 new_entry
, end_of_smb
);
490 static void cifs_fill_dirent_posix(struct cifs_dirent
*de
,
491 const struct smb2_posix_info
*info
)
493 struct smb2_posix_info_parsed parsed
;
495 /* payload should have already been checked at this point */
496 if (posix_info_parse(info
, NULL
, &parsed
) < 0) {
497 cifs_dbg(VFS
, "Invalid POSIX info payload\n");
501 de
->name
= parsed
.name
;
502 de
->namelen
= parsed
.name_len
;
503 de
->resume_key
= info
->Ignored
;
504 de
->ino
= le64_to_cpu(info
->Inode
);
507 static void cifs_fill_dirent_unix(struct cifs_dirent
*de
,
508 const FILE_UNIX_INFO
*info
, bool is_unicode
)
510 de
->name
= &info
->FileName
[0];
512 de
->namelen
= cifs_unicode_bytelen(de
->name
);
514 de
->namelen
= strnlen(de
->name
, PATH_MAX
);
515 de
->resume_key
= info
->ResumeKey
;
516 de
->ino
= le64_to_cpu(info
->basic
.UniqueId
);
519 static void cifs_fill_dirent_dir(struct cifs_dirent
*de
,
520 const FILE_DIRECTORY_INFO
*info
)
522 de
->name
= &info
->FileName
[0];
523 de
->namelen
= le32_to_cpu(info
->FileNameLength
);
524 de
->resume_key
= info
->FileIndex
;
527 static void cifs_fill_dirent_full(struct cifs_dirent
*de
,
528 const FILE_FULL_DIRECTORY_INFO
*info
)
530 de
->name
= &info
->FileName
[0];
531 de
->namelen
= le32_to_cpu(info
->FileNameLength
);
532 de
->resume_key
= info
->FileIndex
;
535 static void cifs_fill_dirent_search(struct cifs_dirent
*de
,
536 const SEARCH_ID_FULL_DIR_INFO
*info
)
538 de
->name
= &info
->FileName
[0];
539 de
->namelen
= le32_to_cpu(info
->FileNameLength
);
540 de
->resume_key
= info
->FileIndex
;
541 de
->ino
= le64_to_cpu(info
->UniqueId
);
544 static void cifs_fill_dirent_both(struct cifs_dirent
*de
,
545 const FILE_BOTH_DIRECTORY_INFO
*info
)
547 de
->name
= &info
->FileName
[0];
548 de
->namelen
= le32_to_cpu(info
->FileNameLength
);
549 de
->resume_key
= info
->FileIndex
;
552 static void cifs_fill_dirent_std(struct cifs_dirent
*de
,
553 const FIND_FILE_STANDARD_INFO
*info
)
555 de
->name
= &info
->FileName
[0];
556 /* one byte length, no endianness conversion */
557 de
->namelen
= info
->FileNameLength
;
558 de
->resume_key
= info
->ResumeKey
;
561 static int cifs_fill_dirent(struct cifs_dirent
*de
, const void *info
,
562 u16 level
, bool is_unicode
)
564 memset(de
, 0, sizeof(*de
));
567 case SMB_FIND_FILE_POSIX_INFO
:
568 cifs_fill_dirent_posix(de
, info
);
570 case SMB_FIND_FILE_UNIX
:
571 cifs_fill_dirent_unix(de
, info
, is_unicode
);
573 case SMB_FIND_FILE_DIRECTORY_INFO
:
574 cifs_fill_dirent_dir(de
, info
);
576 case SMB_FIND_FILE_FULL_DIRECTORY_INFO
:
577 cifs_fill_dirent_full(de
, info
);
579 case SMB_FIND_FILE_ID_FULL_DIR_INFO
:
580 cifs_fill_dirent_search(de
, info
);
582 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO
:
583 cifs_fill_dirent_both(de
, info
);
585 case SMB_FIND_FILE_INFO_STANDARD
:
586 cifs_fill_dirent_std(de
, info
);
589 cifs_dbg(FYI
, "Unknown findfirst level %d\n", level
);
596 #define UNICODE_DOT cpu_to_le16(0x2e)
598 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
599 static int cifs_entry_is_dot(struct cifs_dirent
*de
, bool is_unicode
)
607 __le16
*ufilename
= (__le16
*)de
->name
;
608 if (de
->namelen
== 2) {
610 if (ufilename
[0] == UNICODE_DOT
)
612 } else if (de
->namelen
== 4) {
614 if (ufilename
[0] == UNICODE_DOT
&&
615 ufilename
[1] == UNICODE_DOT
)
619 if (de
->namelen
== 1) {
620 if (de
->name
[0] == '.')
622 } else if (de
->namelen
== 2) {
623 if (de
->name
[0] == '.' && de
->name
[1] == '.')
631 /* Check if directory that we are searching has changed so we can decide
632 whether we can use the cached search results from the previous search */
633 static int is_dir_changed(struct file
*file
)
635 struct inode
*inode
= file_inode(file
);
636 struct cifsInodeInfo
*cifs_inode_info
= CIFS_I(inode
);
638 if (cifs_inode_info
->time
== 0)
639 return 1; /* directory was changed, e.g. unlink or new file */
645 static int cifs_save_resume_key(const char *current_entry
,
646 struct cifsFileInfo
*file_info
)
648 struct cifs_dirent de
;
651 rc
= cifs_fill_dirent(&de
, current_entry
, file_info
->srch_inf
.info_level
,
652 file_info
->srch_inf
.unicode
);
654 file_info
->srch_inf
.presume_name
= de
.name
;
655 file_info
->srch_inf
.resume_name_len
= de
.namelen
;
656 file_info
->srch_inf
.resume_key
= de
.resume_key
;
662 * Find the corresponding entry in the search. Note that the SMB server returns
663 * search entries for . and .. which complicates logic here if we choose to
664 * parse for them and we do not assume that they are located in the findfirst
665 * return buffer. We start counting in the buffer with entry 2 and increment for
666 * every entry (do not increment for . or .. entry).
669 find_cifs_entry(const unsigned int xid
, struct cifs_tcon
*tcon
, loff_t pos
,
670 struct file
*file
, const char *full_path
,
671 char **current_entry
, int *num_to_ret
)
676 loff_t first_entry_in_buffer
;
677 loff_t index_to_find
= pos
;
678 struct cifsFileInfo
*cfile
= file
->private_data
;
679 struct cifs_sb_info
*cifs_sb
= CIFS_FILE_SB(file
);
680 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
681 /* check if index in the buffer */
683 if (!server
->ops
->query_dir_first
|| !server
->ops
->query_dir_next
)
686 if ((cfile
== NULL
) || (current_entry
== NULL
) || (num_to_ret
== NULL
))
689 *current_entry
= NULL
;
690 first_entry_in_buffer
= cfile
->srch_inf
.index_of_last_entry
-
691 cfile
->srch_inf
.entries_in_buffer
;
694 * If first entry in buf is zero then is first buffer
695 * in search response data which means it is likely . and ..
696 * will be in this buffer, although some servers do not return
697 * . and .. for the root of a drive and for those we need
698 * to start two entries earlier.
701 dump_cifs_file_struct(file
, "In fce ");
702 if (((index_to_find
< cfile
->srch_inf
.index_of_last_entry
) &&
703 is_dir_changed(file
)) || (index_to_find
< first_entry_in_buffer
)) {
704 /* close and restart search */
705 cifs_dbg(FYI
, "search backing up - close and restart search\n");
706 spin_lock(&cfile
->file_info_lock
);
707 if (server
->ops
->dir_needs_close(cfile
)) {
708 cfile
->invalidHandle
= true;
709 spin_unlock(&cfile
->file_info_lock
);
710 if (server
->ops
->close_dir
)
711 server
->ops
->close_dir(xid
, tcon
, &cfile
->fid
);
713 spin_unlock(&cfile
->file_info_lock
);
714 if (cfile
->srch_inf
.ntwrk_buf_start
) {
715 cifs_dbg(FYI
, "freeing SMB ff cache buf on search rewind\n");
716 if (cfile
->srch_inf
.smallBuf
)
717 cifs_small_buf_release(cfile
->srch_inf
.
720 cifs_buf_release(cfile
->srch_inf
.
722 cfile
->srch_inf
.ntwrk_buf_start
= NULL
;
724 rc
= initiate_cifs_search(xid
, file
, full_path
);
726 cifs_dbg(FYI
, "error %d reinitiating a search on rewind\n",
730 /* FindFirst/Next set last_entry to NULL on malformed reply */
731 if (cfile
->srch_inf
.last_entry
)
732 cifs_save_resume_key(cfile
->srch_inf
.last_entry
, cfile
);
735 search_flags
= CIFS_SEARCH_CLOSE_AT_END
| CIFS_SEARCH_RETURN_RESUME
;
736 if (backup_cred(cifs_sb
))
737 search_flags
|= CIFS_SEARCH_BACKUP_SEARCH
;
739 while ((index_to_find
>= cfile
->srch_inf
.index_of_last_entry
) &&
740 (rc
== 0) && !cfile
->srch_inf
.endOfSearch
) {
741 cifs_dbg(FYI
, "calling findnext2\n");
742 rc
= server
->ops
->query_dir_next(xid
, tcon
, &cfile
->fid
,
745 /* FindFirst/Next set last_entry to NULL on malformed reply */
746 if (cfile
->srch_inf
.last_entry
)
747 cifs_save_resume_key(cfile
->srch_inf
.last_entry
, cfile
);
751 if (index_to_find
< cfile
->srch_inf
.index_of_last_entry
) {
752 /* we found the buffer that contains the entry */
753 /* scan and find it */
758 if (cfile
->srch_inf
.ntwrk_buf_start
== NULL
) {
759 cifs_dbg(VFS
, "ntwrk_buf_start is NULL during readdir\n");
763 end_of_smb
= cfile
->srch_inf
.ntwrk_buf_start
+
764 server
->ops
->calc_smb_size(
765 cfile
->srch_inf
.ntwrk_buf_start
);
767 cur_ent
= cfile
->srch_inf
.srch_entries_start
;
768 first_entry_in_buffer
= cfile
->srch_inf
.index_of_last_entry
769 - cfile
->srch_inf
.entries_in_buffer
;
770 pos_in_buf
= index_to_find
- first_entry_in_buffer
;
771 cifs_dbg(FYI
, "found entry - pos_in_buf %d\n", pos_in_buf
);
773 for (i
= 0; (i
< (pos_in_buf
)) && (cur_ent
!= NULL
); i
++) {
774 /* go entry by entry figuring out which is first */
775 cur_ent
= nxt_dir_entry(cur_ent
, end_of_smb
,
776 cfile
->srch_inf
.info_level
);
778 if ((cur_ent
== NULL
) && (i
< pos_in_buf
)) {
779 /* BB fixme - check if we should flag this error */
780 cifs_dbg(VFS
, "reached end of buf searching for pos in buf %d index to find %lld rc %d\n",
781 pos_in_buf
, index_to_find
, rc
);
784 *current_entry
= cur_ent
;
786 cifs_dbg(FYI
, "index not in buffer - could not findnext into it\n");
790 if (pos_in_buf
>= cfile
->srch_inf
.entries_in_buffer
) {
791 cifs_dbg(FYI
, "can not return entries pos_in_buf beyond last\n");
794 *num_to_ret
= cfile
->srch_inf
.entries_in_buffer
- pos_in_buf
;
799 static bool emit_cached_dirents(struct cached_dirents
*cde
,
800 struct dir_context
*ctx
)
802 struct cached_dirent
*dirent
;
805 list_for_each_entry(dirent
, &cde
->entries
, entry
) {
807 * Skip all early entries prior to the current lseek()
810 if (ctx
->pos
> dirent
->pos
)
813 * We recorded the current ->pos value for the dirent
814 * when we stored it in the cache.
815 * However, this sequence of ->pos values may have holes
816 * in it, for example dot-dirs returned from the server
818 * Handle this by forcing ctx->pos to be the same as the
819 * ->pos of the current dirent we emit from the cache.
820 * This means that when we emit these entries from the cache
821 * we now emit them with the same ->pos value as in the
824 ctx
->pos
= dirent
->pos
;
825 rc
= dir_emit(ctx
, dirent
->name
, dirent
->namelen
,
826 dirent
->fattr
.cf_uniqueid
,
827 dirent
->fattr
.cf_dtype
);
835 static void update_cached_dirents_count(struct cached_dirents
*cde
,
836 struct dir_context
*ctx
)
840 if (cde
->is_valid
|| cde
->is_failed
)
846 static void finished_cached_dirents_count(struct cached_dirents
*cde
,
847 struct dir_context
*ctx
)
851 if (cde
->is_valid
|| cde
->is_failed
)
853 if (ctx
->pos
!= cde
->pos
)
859 static void add_cached_dirent(struct cached_dirents
*cde
,
860 struct dir_context
*ctx
,
861 const char *name
, int namelen
,
862 struct cifs_fattr
*fattr
)
864 struct cached_dirent
*de
;
868 if (cde
->is_valid
|| cde
->is_failed
)
870 if (ctx
->pos
!= cde
->pos
) {
874 de
= kzalloc(sizeof(*de
), GFP_ATOMIC
);
879 de
->namelen
= namelen
;
880 de
->name
= kstrndup(name
, namelen
, GFP_ATOMIC
);
881 if (de
->name
== NULL
) {
888 memcpy(&de
->fattr
, fattr
, sizeof(struct cifs_fattr
));
890 list_add_tail(&de
->entry
, &cde
->entries
);
893 static bool cifs_dir_emit(struct dir_context
*ctx
,
894 const char *name
, int namelen
,
895 struct cifs_fattr
*fattr
,
896 struct cached_fid
*cfid
)
899 ino_t ino
= cifs_uniqueid_to_ino_t(fattr
->cf_uniqueid
);
901 rc
= dir_emit(ctx
, name
, namelen
, ino
, fattr
->cf_dtype
);
906 mutex_lock(&cfid
->dirents
.de_mutex
);
907 add_cached_dirent(&cfid
->dirents
, ctx
, name
, namelen
,
909 mutex_unlock(&cfid
->dirents
.de_mutex
);
915 static int cifs_filldir(char *find_entry
, struct file
*file
,
916 struct dir_context
*ctx
,
917 char *scratch_buf
, unsigned int max_len
,
918 struct cached_fid
*cfid
)
920 struct cifsFileInfo
*file_info
= file
->private_data
;
921 struct super_block
*sb
= file_inode(file
)->i_sb
;
922 struct cifs_sb_info
*cifs_sb
= CIFS_SB(sb
);
923 struct cifs_dirent de
= { NULL
, };
924 struct cifs_fattr fattr
;
928 rc
= cifs_fill_dirent(&de
, find_entry
, file_info
->srch_inf
.info_level
,
929 file_info
->srch_inf
.unicode
);
933 if (de
.namelen
> max_len
) {
934 cifs_dbg(VFS
, "bad search response length %zd past smb end\n",
939 /* skip . and .. since we added them first */
940 if (cifs_entry_is_dot(&de
, file_info
->srch_inf
.unicode
))
943 if (file_info
->srch_inf
.unicode
) {
944 struct nls_table
*nlt
= cifs_sb
->local_nls
;
947 map_type
= cifs_remap(cifs_sb
);
948 name
.name
= scratch_buf
;
950 cifs_from_utf16((char *)name
.name
, (__le16
*)de
.name
,
952 min_t(size_t, de
.namelen
,
953 (size_t)max_len
), nlt
, map_type
);
954 name
.len
-= nls_nullsize(nlt
);
957 name
.len
= de
.namelen
;
960 switch (file_info
->srch_inf
.info_level
) {
961 case SMB_FIND_FILE_POSIX_INFO
:
962 cifs_posix_to_fattr(&fattr
,
963 (struct smb2_posix_info
*)find_entry
,
966 case SMB_FIND_FILE_UNIX
:
967 cifs_unix_basic_to_fattr(&fattr
,
968 &((FILE_UNIX_INFO
*)find_entry
)->basic
,
970 if (S_ISLNK(fattr
.cf_mode
))
971 fattr
.cf_flags
|= CIFS_FATTR_NEED_REVAL
;
973 case SMB_FIND_FILE_INFO_STANDARD
:
974 cifs_std_info_to_fattr(&fattr
,
975 (FIND_FILE_STANDARD_INFO
*)find_entry
,
978 case SMB_FIND_FILE_FULL_DIRECTORY_INFO
:
979 case SMB_FIND_FILE_ID_FULL_DIR_INFO
:
980 cifs_fulldir_info_to_fattr(&fattr
, find_entry
, cifs_sb
);
983 cifs_dir_info_to_fattr(&fattr
,
984 (FILE_DIRECTORY_INFO
*)find_entry
,
989 if (de
.ino
&& (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_SERVER_INUM
)) {
990 fattr
.cf_uniqueid
= de
.ino
;
992 fattr
.cf_uniqueid
= iunique(sb
, ROOT_I
);
993 cifs_autodisable_serverino(cifs_sb
);
996 if ((cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MF_SYMLINKS
) &&
997 couldbe_mf_symlink(&fattr
))
999 * trying to get the type and mode can be slow,
1000 * so just call those regular files for now, and mark
1003 fattr
.cf_flags
|= CIFS_FATTR_NEED_REVAL
;
1005 cifs_prime_dcache(file_dentry(file
), &name
, &fattr
);
1007 return !cifs_dir_emit(ctx
, name
.name
, name
.len
,
1012 int cifs_readdir(struct file
*file
, struct dir_context
*ctx
)
1017 struct tcon_link
*tlink
= NULL
;
1018 struct cifs_tcon
*tcon
;
1019 struct cifsFileInfo
*cifsFile
;
1020 char *current_entry
;
1021 int num_to_fill
= 0;
1022 char *tmp_buf
= NULL
;
1024 unsigned int max_len
;
1025 const char *full_path
;
1026 void *page
= alloc_dentry_path();
1027 struct cached_fid
*cfid
= NULL
;
1028 struct cifs_sb_info
*cifs_sb
= CIFS_FILE_SB(file
);
1032 full_path
= build_path_from_dentry(file_dentry(file
), page
);
1033 if (IS_ERR(full_path
)) {
1034 rc
= PTR_ERR(full_path
);
1038 if (file
->private_data
== NULL
) {
1039 tlink
= cifs_sb_tlink(cifs_sb
);
1041 goto cache_not_found
;
1042 tcon
= tlink_tcon(tlink
);
1044 cifsFile
= file
->private_data
;
1045 tcon
= tlink_tcon(cifsFile
->tlink
);
1048 rc
= open_cached_dir(xid
, tcon
, full_path
, cifs_sb
, false, &cfid
);
1049 cifs_put_tlink(tlink
);
1051 goto cache_not_found
;
1053 mutex_lock(&cfid
->dirents
.de_mutex
);
1055 * If this was reading from the start of the directory
1056 * we need to initialize scanning and storing the
1057 * directory content.
1059 if (ctx
->pos
== 0 && cfid
->dirents
.ctx
== NULL
) {
1060 cfid
->dirents
.ctx
= ctx
;
1061 cfid
->dirents
.pos
= 2;
1064 * If we already have the entire directory cached then
1065 * we can just serve the cache.
1067 if (cfid
->dirents
.is_valid
) {
1068 if (!dir_emit_dots(file
, ctx
)) {
1069 mutex_unlock(&cfid
->dirents
.de_mutex
);
1072 emit_cached_dirents(&cfid
->dirents
, ctx
);
1073 mutex_unlock(&cfid
->dirents
.de_mutex
);
1076 mutex_unlock(&cfid
->dirents
.de_mutex
);
1078 /* Drop the cache while calling initiate_cifs_search and
1079 * find_cifs_entry in case there will be reconnects during
1082 close_cached_dir(cfid
);
1087 * Ensure FindFirst doesn't fail before doing filldir() for '.' and
1088 * '..'. Otherwise we won't be able to notify VFS in case of failure.
1090 if (file
->private_data
== NULL
) {
1091 rc
= initiate_cifs_search(xid
, file
, full_path
);
1092 cifs_dbg(FYI
, "initiate cifs search rc %d\n", rc
);
1097 if (!dir_emit_dots(file
, ctx
))
1100 /* 1) If search is active,
1101 is in current search buffer?
1102 if it before then restart search
1103 if after then keep searching till find it */
1104 cifsFile
= file
->private_data
;
1105 if (cifsFile
->srch_inf
.endOfSearch
) {
1106 if (cifsFile
->srch_inf
.emptyDir
) {
1107 cifs_dbg(FYI
, "End of search, empty dir\n");
1112 cifsFile->invalidHandle = true;
1113 tcon->ses->server->close(xid, tcon, &cifsFile->fid);
1116 tcon
= tlink_tcon(cifsFile
->tlink
);
1117 rc
= find_cifs_entry(xid
, tcon
, ctx
->pos
, file
, full_path
,
1118 ¤t_entry
, &num_to_fill
);
1119 open_cached_dir(xid
, tcon
, full_path
, cifs_sb
, false, &cfid
);
1121 cifs_dbg(FYI
, "fce error %d\n", rc
);
1123 } else if (current_entry
!= NULL
) {
1124 cifs_dbg(FYI
, "entry %lld found\n", ctx
->pos
);
1127 mutex_lock(&cfid
->dirents
.de_mutex
);
1128 finished_cached_dirents_count(&cfid
->dirents
, ctx
);
1129 mutex_unlock(&cfid
->dirents
.de_mutex
);
1131 cifs_dbg(FYI
, "Could not find entry\n");
1134 cifs_dbg(FYI
, "loop through %d times filling dir for net buf %p\n",
1135 num_to_fill
, cifsFile
->srch_inf
.ntwrk_buf_start
);
1136 max_len
= tcon
->ses
->server
->ops
->calc_smb_size(
1137 cifsFile
->srch_inf
.ntwrk_buf_start
);
1138 end_of_smb
= cifsFile
->srch_inf
.ntwrk_buf_start
+ max_len
;
1140 tmp_buf
= kmalloc(UNICODE_NAME_MAX
, GFP_KERNEL
);
1141 if (tmp_buf
== NULL
) {
1146 for (i
= 0; i
< num_to_fill
; i
++) {
1147 if (current_entry
== NULL
) {
1148 /* evaluate whether this case is an error */
1149 cifs_dbg(VFS
, "past SMB end, num to fill %d i %d\n",
1154 * if buggy server returns . and .. late do we want to
1155 * check for that here?
1158 rc
= cifs_filldir(current_entry
, file
, ctx
,
1159 tmp_buf
, max_len
, cfid
);
1168 mutex_lock(&cfid
->dirents
.de_mutex
);
1169 update_cached_dirents_count(&cfid
->dirents
, ctx
);
1170 mutex_unlock(&cfid
->dirents
.de_mutex
);
1174 cifsFile
->srch_inf
.index_of_last_entry
) {
1175 cifs_dbg(FYI
, "last entry in buf at pos %lld %s\n",
1177 cifs_save_resume_key(current_entry
, cifsFile
);
1181 nxt_dir_entry(current_entry
, end_of_smb
,
1182 cifsFile
->srch_inf
.info_level
);
1188 close_cached_dir(cfid
);
1189 free_dentry_path(page
);