Merge tag 'io_uring-6.16-20250630' of git://git.kernel.dk/linux
[linux-block.git] / fs / smb / client / readdir.c
CommitLineData
929be906 1// SPDX-License-Identifier: LGPL-2.1
1da177e4 2/*
1da177e4
LT
3 *
4 * Directory search handling
6dc0f87e 5 *
ad7a2926 6 * Copyright (C) International Business Machines Corp., 2004, 2008
cda0ec6a 7 * Copyright (C) Red Hat, Inc., 2011
1da177e4
LT
8 * Author(s): Steve French (sfrench@us.ibm.com)
9 *
1da177e4
LT
10 */
11#include <linux/fs.h>
06c56740 12#include <linux/namei.h>
273d81d6 13#include <linux/pagemap.h>
5a0e3ad6 14#include <linux/slab.h>
1da177e4 15#include <linux/stat.h>
1da177e4
LT
16#include "cifspdu.h"
17#include "cifsglob.h"
18#include "cifsproto.h"
19#include "cifs_unicode.h"
20#include "cifs_debug.h"
21#include "cifs_fs_sb.h"
22#include "cifsfs.h"
3d519bd1 23#include "smb2proto.h"
8401e936 24#include "fs_context.h"
05b98fd2 25#include "cached_dir.h"
c520ba75 26#include "reparse.h"
1da177e4 27
f5884166
JL
28/*
29 * To be safe - for UCS to UTF-8 with strings loaded with the rare long
30 * characters alloc more to account for such multibyte target UTF-8
31 * characters.
32 */
33#define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
34
3979877e
SF
35#ifdef CONFIG_CIFS_DEBUG2
36static void dump_cifs_file_struct(struct file *file, char *label)
1da177e4 37{
6dc0f87e 38 struct cifsFileInfo *cf;
1da177e4 39
4523cc30 40 if (file) {
1da177e4 41 cf = file->private_data;
4523cc30 42 if (cf == NULL) {
f96637be 43 cifs_dbg(FYI, "empty cifs private file data\n");
1da177e4
LT
44 return;
45 }
ad7a2926 46 if (cf->invalidHandle)
a0a3036b 47 cifs_dbg(FYI, "Invalid handle\n");
ad7a2926 48 if (cf->srch_inf.endOfSearch)
f96637be 49 cifs_dbg(FYI, "end of search\n");
ad7a2926 50 if (cf->srch_inf.emptyDir)
f96637be 51 cifs_dbg(FYI, "empty dir\n");
1da177e4 52 }
3979877e 53}
90c81e0b
SF
54#else
55static inline void dump_cifs_file_struct(struct file *file, char *label)
56{
57}
3979877e 58#endif /* DEBUG2 */
1da177e4 59
cc0bad75 60/*
eb1b3fa5
JL
61 * Attempt to preload the dcache with the results from the FIND_FIRST/NEXT
62 *
cc0bad75 63 * Find the dentry that matches "name". If there isn't one, create one. If it's
9e6d722f
NA
64 * a negative dentry or the uniqueid or filetype(mode) changed,
65 * then drop it and recreate it.
cc0bad75 66 */
eb1b3fa5
JL
67static void
68cifs_prime_dcache(struct dentry *parent, struct qstr *name,
cc0bad75
JL
69 struct cifs_fattr *fattr)
70{
71 struct dentry *dentry, *alias;
72 struct inode *inode;
fc64005c 73 struct super_block *sb = parent->d_sb;
2f2591a3 74 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
8cb0bc54
RB
75 bool posix = cifs_sb_master_tcon(cifs_sb)->posix_extensions;
76 bool reparse_need_reval = false;
3125d265 77 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
6d039984 78 int rc;
cc0bad75 79
f96637be 80 cifs_dbg(FYI, "%s: for %s\n", __func__, name->name);
cc0bad75 81
06c56740 82 dentry = try_lookup_noperm(name, parent);
3125d265
AV
83 if (!dentry) {
84 /*
85 * If we know that the inode will need to be revalidated
86 * immediately, then don't create a new dentry for it.
87 * We'll end up doing an on the wire call either way and
88 * this spares us an invalidation.
89 */
3125d265 90retry:
8cb0bc54
RB
91 if (posix) {
92 switch (fattr->cf_mode & S_IFMT) {
93 case S_IFLNK:
94 case S_IFBLK:
95 case S_IFCHR:
96 reparse_need_reval = true;
97 break;
98 default:
99 break;
100 }
101 } else if (fattr->cf_cifsattrs & ATTR_REPARSE) {
102 reparse_need_reval = true;
103 }
104
105 if (reparse_need_reval ||
6d039984
PA
106 (fattr->cf_flags & CIFS_FATTR_NEED_REVAL))
107 return;
108
3125d265
AV
109 dentry = d_alloc_parallel(parent, name, &wq);
110 }
a1c83681 111 if (IS_ERR(dentry))
4f522a24 112 return;
3125d265 113 if (!d_in_lookup(dentry)) {
2b0143b5 114 inode = d_inode(dentry);
2f2591a3 115 if (inode) {
3125d265
AV
116 if (d_mountpoint(dentry)) {
117 dput(dentry);
118 return;
119 }
2f2591a3
JL
120 /*
121 * If we're generating inode numbers, then we don't
122 * want to clobber the existing one with the one that
123 * the readdir code created.
124 */
125 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM))
126 fattr->cf_uniqueid = CIFS_I(inode)->uniqueid;
127
6d039984
PA
128 /*
129 * Update inode in place if both i_ino and i_mode didn't
130 * change.
131 */
132 if (CIFS_I(inode)->uniqueid == fattr->cf_uniqueid) {
133 /*
134 * Query dir responses don't provide enough
135 * information about reparse points other than
136 * their reparse tags. Save an invalidation by
66c9314b
PA
137 * not clobbering some existing attributes when
138 * reparse tag and ctime haven't changed.
6d039984
PA
139 */
140 rc = 0;
141 if (fattr->cf_cifsattrs & ATTR_REPARSE) {
142 if (likely(reparse_inode_match(inode, fattr))) {
143 fattr->cf_mode = inode->i_mode;
66c9314b 144 fattr->cf_rdev = inode->i_rdev;
78e26bec
PA
145 fattr->cf_uid = inode->i_uid;
146 fattr->cf_gid = inode->i_gid;
966cc171 147 fattr->cf_eof = CIFS_I(inode)->netfs.remote_i_size;
6d039984
PA
148 fattr->cf_symlink_target = NULL;
149 } else {
150 CIFS_I(inode)->time = 0;
151 rc = -ESTALE;
152 }
153 }
e4b61f3b 154 if (!rc && !cifs_fattr_to_inode(inode, fattr, true)) {
6d039984
PA
155 dput(dentry);
156 return;
157 }
2f2591a3 158 }
cd60042c 159 }
5542aa2f 160 d_invalidate(dentry);
cc0bad75 161 dput(dentry);
3125d265
AV
162 goto retry;
163 } else {
164 inode = cifs_iget(sb, fattr);
165 if (!inode)
166 inode = ERR_PTR(-ENOMEM);
167 alias = d_splice_alias(inode, dentry);
168 d_lookup_done(dentry);
169 if (alias && !IS_ERR(alias))
170 dput(alias);
cc0bad75 171 }
eb1b3fa5 172 dput(dentry);
cc0bad75
JL
173}
174
0b8f18e3
JL
175static void
176cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
1da177e4 177{
45e72402
PA
178 struct cifs_open_info_data data = {
179 .reparse = { .tag = fattr->cf_cifstag, },
180 };
181
8401e936
RS
182 fattr->cf_uid = cifs_sb->ctx->linux_uid;
183 fattr->cf_gid = cifs_sb->ctx->linux_gid;
1da177e4 184
13909d96
SF
185 /*
186 * The IO_REPARSE_TAG_LX_ tags originally were used by WSL but they
187 * are preferred by the Linux client in some cases since, unlike
188 * the NFS reparse tag (or EAs), they don't require an extra query
189 * to determine which type of special file they represent.
190 * TODO: go through all documented reparse tags to see if we can
191 * reasonably map some of them to directories vs. files vs. symlinks
192 */
5f71ebc4 193 if ((fattr->cf_cifsattrs & ATTR_REPARSE) &&
45e72402 194 cifs_reparse_point_to_fattr(cifs_sb, fattr, &data))
5f71ebc4
PA
195 goto out_reparse;
196
0b8f18e3 197 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
8401e936 198 fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
0b8f18e3 199 fattr->cf_dtype = DT_DIR;
5f71ebc4 200 } else {
8401e936 201 fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
0b8f18e3 202 fattr->cf_dtype = DT_REG;
1da177e4
LT
203 }
204
5f71ebc4 205out_reparse:
74d290da
JM
206 /* non-unix readdir doesn't provide nlink */
207 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
208
0b8f18e3
JL
209 if (fattr->cf_cifsattrs & ATTR_READONLY)
210 fattr->cf_mode &= ~S_IWUGO;
5bafd765 211
ccb5c001
JL
212 /*
213 * We of course don't get ACL info in FIND_FIRST/NEXT results, so
214 * mark it for revalidation so that "ls -l" will look right. It might
215 * be super-slow, but if we don't do this then the ownership of files
216 * may look wrong since the inodes may not have timed out by the time
217 * "ls" does a stat() call on them.
218 */
e3e056c3
AA
219 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
220 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID))
ccb5c001
JL
221 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
222
0b8f18e3
JL
223 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
224 fattr->cf_cifsattrs & ATTR_SYSTEM) {
225 if (fattr->cf_eof == 0) {
226 fattr->cf_mode &= ~S_IFMT;
227 fattr->cf_mode |= S_IFIFO;
228 fattr->cf_dtype = DT_FIFO;
3020a1f5 229 } else {
4468eb3f 230 /*
0b8f18e3
JL
231 * trying to get the type and mode via SFU can be slow,
232 * so just call those regular files for now, and mark
233 * for reval
4468eb3f 234 */
0b8f18e3 235 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
4468eb3f
JL
236 }
237 }
0b8f18e3 238}
1da177e4 239
3d519bd1
AA
240/* Fill a cifs_fattr struct with info from SMB_FIND_FILE_POSIX_INFO. */
241static void
242cifs_posix_to_fattr(struct cifs_fattr *fattr, struct smb2_posix_info *info,
243 struct cifs_sb_info *cifs_sb)
244{
245 struct smb2_posix_info_parsed parsed;
246
247 posix_info_parse(info, NULL, &parsed);
248
249 memset(fattr, 0, sizeof(*fattr));
250 fattr->cf_uniqueid = le64_to_cpu(info->Inode);
251 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
252 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
253
254 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
255 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
256 fattr->cf_ctime = cifs_NTtimeToUnix(info->CreationTime);
257
258 fattr->cf_nlink = le32_to_cpu(info->HardLinks);
259 fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
260
6a832bc8
RB
261 if (fattr->cf_cifsattrs & ATTR_REPARSE)
262 fattr->cf_cifstag = le32_to_cpu(info->ReparseTag);
263
264 /* The Mode field in the response can now include the file type as well */
ddca5023
SF
265 fattr->cf_mode = wire_mode_to_posix(le32_to_cpu(info->Mode),
266 fattr->cf_cifsattrs & ATTR_DIRECTORY);
6a832bc8
RB
267 fattr->cf_dtype = S_DT(le32_to_cpu(info->Mode));
268
269 switch (fattr->cf_mode & S_IFMT) {
270 case S_IFLNK:
271 case S_IFBLK:
272 case S_IFCHR:
273 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
274 break;
275 default:
276 break;
277 }
3d519bd1 278
a0a3036b 279 cifs_dbg(FYI, "posix fattr: dev %d, reparse %d, mode %o\n",
3d519bd1
AA
280 le32_to_cpu(info->DeviceId),
281 le32_to_cpu(info->ReparseTag),
282 le32_to_cpu(info->Mode));
283
9934430e
SF
284 sid_to_id(cifs_sb, &parsed.owner, fattr, SIDOWNER);
285 sid_to_id(cifs_sb, &parsed.group, fattr, SIDGROUP);
3d519bd1
AA
286}
287
046aca3c
PAS
288static void __dir_info_to_fattr(struct cifs_fattr *fattr, const void *info)
289{
290 const FILE_DIRECTORY_INFO *fi = info;
291
292 memset(fattr, 0, sizeof(*fattr));
293 fattr->cf_cifsattrs = le32_to_cpu(fi->ExtFileAttributes);
294 fattr->cf_eof = le64_to_cpu(fi->EndOfFile);
295 fattr->cf_bytes = le64_to_cpu(fi->AllocationSize);
296 fattr->cf_createtime = le64_to_cpu(fi->CreationTime);
297 fattr->cf_atime = cifs_NTtimeToUnix(fi->LastAccessTime);
298 fattr->cf_ctime = cifs_NTtimeToUnix(fi->ChangeTime);
299 fattr->cf_mtime = cifs_NTtimeToUnix(fi->LastWriteTime);
300}
301
c052e2b4 302void
0b8f18e3
JL
303cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info,
304 struct cifs_sb_info *cifs_sb)
305{
046aca3c
PAS
306 __dir_info_to_fattr(fattr, info);
307 cifs_fill_common_info(fattr, cifs_sb);
308}
0b8f18e3 309
046aca3c 310static void cifs_fulldir_info_to_fattr(struct cifs_fattr *fattr,
55c7788c 311 const void *info,
046aca3c
PAS
312 struct cifs_sb_info *cifs_sb)
313{
55c7788c
PA
314 const FILE_FULL_DIRECTORY_INFO *di = info;
315
046aca3c
PAS
316 __dir_info_to_fattr(fattr, info);
317
55c7788c 318 /* See MS-FSCC 2.4.14, 2.4.19 */
046aca3c 319 if (fattr->cf_cifsattrs & ATTR_REPARSE)
55c7788c 320 fattr->cf_cifstag = le32_to_cpu(di->EaSize);
0b8f18e3
JL
321 cifs_fill_common_info(fattr, cifs_sb);
322}
1da177e4 323
15dd4781 324static void
0b8f18e3
JL
325cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info,
326 struct cifs_sb_info *cifs_sb)
327{
0d424ad0 328 int offset = cifs_sb_master_tcon(cifs_sb)->ses->server->timeAdj;
1da177e4 329
0b8f18e3
JL
330 memset(fattr, 0, sizeof(*fattr));
331 fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate,
332 info->LastAccessTime, offset);
333 fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate,
334 info->LastWriteTime, offset);
335 fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate,
336 info->LastWriteTime, offset);
337
338 fattr->cf_cifsattrs = le16_to_cpu(info->Attributes);
339 fattr->cf_bytes = le32_to_cpu(info->AllocationSize);
340 fattr->cf_eof = le32_to_cpu(info->DataSize);
341
342 cifs_fill_common_info(fattr, cifs_sb);
1da177e4
LT
343}
344
92fc65a7 345static int
3998f0b8 346_initiate_cifs_search(const unsigned int xid, struct file *file,
f6f1f179 347 const char *full_path)
1da177e4 348{
2608bee7 349 __u16 search_flags;
1da177e4 350 int rc = 0;
3870253e 351 struct cifsFileInfo *cifsFile;
7119e220 352 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
59c55ba1 353 struct tcon_link *tlink = NULL;
29e20f9c 354 struct cifs_tcon *tcon;
92fc65a7 355 struct TCP_Server_Info *server;
1da177e4 356
7ffec372 357 if (file->private_data == NULL) {
59c55ba1
JL
358 tlink = cifs_sb_tlink(cifs_sb);
359 if (IS_ERR(tlink))
360 return PTR_ERR(tlink);
361
362 cifsFile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
363 if (cifsFile == NULL) {
364 rc = -ENOMEM;
365 goto error_exit;
366 }
81ddd8c0 367 spin_lock_init(&cifsFile->file_info_lock);
59c55ba1
JL
368 file->private_data = cifsFile;
369 cifsFile->tlink = cifs_get_tlink(tlink);
29e20f9c 370 tcon = tlink_tcon(tlink);
59c55ba1
JL
371 } else {
372 cifsFile = file->private_data;
29e20f9c 373 tcon = tlink_tcon(cifsFile->tlink);
7ffec372 374 }
1da177e4 375
92fc65a7
PS
376 server = tcon->ses->server;
377
378 if (!server->ops->query_dir_first) {
379 rc = -ENOSYS;
380 goto error_exit;
381 }
382
4b18f2a9
SF
383 cifsFile->invalidHandle = true;
384 cifsFile->srch_inf.endOfSearch = false;
1da177e4 385
f96637be 386 cifs_dbg(FYI, "Full path: %s start at: %lld\n", full_path, file->f_pos);
1da177e4 387
75cf6bdc 388ffirst_retry:
1da177e4 389 /* test for Unix extensions */
c18c842b 390 /* but now check for them on the share/mount not on the SMB session */
29e20f9c
PS
391 /* if (cap_unix(tcon->ses) { */
392 if (tcon->unix_ext)
5bafd765 393 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
3d519bd1
AA
394 else if (tcon->posix_extensions)
395 cifsFile->srch_inf.info_level = SMB_FIND_FILE_POSIX_INFO;
29e20f9c
PS
396 else if ((tcon->ses->capabilities &
397 tcon->ses->server->vals->cap_nt_find) == 0) {
5bafd765 398 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
1da177e4
LT
399 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
400 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
401 } else /* not srvinos - BB fixme add check for backlevel? */ {
55c7788c 402 cifsFile->srch_inf.info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
1da177e4
LT
403 }
404
2608bee7
SP
405 search_flags = CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME;
406 if (backup_cred(cifs_sb))
407 search_flags |= CIFS_SEARCH_BACKUP_SEARCH;
408
92fc65a7
PS
409 rc = server->ops->query_dir_first(xid, tcon, full_path, cifs_sb,
410 &cifsFile->fid, search_flags,
411 &cifsFile->srch_inf);
412
6d039984 413 if (rc == 0) {
4b18f2a9 414 cifsFile->invalidHandle = false;
6d039984
PA
415 } else if ((rc == -EOPNOTSUPP) &&
416 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
015683d4 417 cifs_autodisable_serverino(cifs_sb);
75cf6bdc
SF
418 goto ffirst_retry;
419 }
7ffec372 420error_exit:
7ffec372 421 cifs_put_tlink(tlink);
1da177e4
LT
422 return rc;
423}
424
3998f0b8
RS
425static int
426initiate_cifs_search(const unsigned int xid, struct file *file,
427 const char *full_path)
428{
429 int rc, retry_count = 0;
430
431 do {
432 rc = _initiate_cifs_search(xid, file, full_path);
433 /*
434 * If we don't have enough credits to start reading the
435 * directory just try again after short wait.
436 */
437 if (rc != -EDEADLK)
438 break;
439
440 usleep_range(512, 2048);
441 } while (retry_count++ < 5);
442
443 return rc;
444}
445
1da177e4 446/* return length of unicode string in bytes */
cda0ec6a 447static int cifs_unicode_bytelen(const char *str)
1da177e4
LT
448{
449 int len;
cda0ec6a 450 const __le16 *ustr = (const __le16 *)str;
1da177e4 451
3870253e 452 for (len = 0; len <= PATH_MAX; len++) {
4523cc30 453 if (ustr[len] == 0)
1da177e4
LT
454 return len << 1;
455 }
f96637be 456 cifs_dbg(FYI, "Unicode string longer than PATH_MAX found\n");
1da177e4
LT
457 return len << 1;
458}
459
5bafd765 460static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
1da177e4 461{
3870253e 462 char *new_entry;
ad7a2926 463 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
1da177e4 464
4523cc30 465 if (level == SMB_FIND_FILE_INFO_STANDARD) {
ad7a2926 466 FIND_FILE_STANDARD_INFO *pfData;
5bafd765
SF
467 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
468
35235e19 469 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) + 1 +
5bafd765 470 pfData->FileNameLength;
8ad8aa35
DC
471 } else {
472 u32 next_offset = le32_to_cpu(pDirInfo->NextEntryOffset);
473
474 if (old_entry + next_offset < old_entry) {
a0a3036b 475 cifs_dbg(VFS, "Invalid offset %u\n", next_offset);
8ad8aa35
DC
476 return NULL;
477 }
478 new_entry = old_entry + next_offset;
479 }
f96637be 480 cifs_dbg(FYI, "new entry %p old entry %p\n", new_entry, old_entry);
1da177e4 481 /* validate that new_entry is not past end of SMB */
4523cc30 482 if (new_entry >= end_of_smb) {
f96637be
JP
483 cifs_dbg(VFS, "search entry %p began after end of SMB %p old entry %p\n",
484 new_entry, end_of_smb, old_entry);
1da177e4 485 return NULL;
4523cc30 486 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
35235e19 487 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) + 1 > end_of_smb))
3870253e 488 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
35235e19 489 (new_entry + sizeof(FILE_DIRECTORY_INFO) + 1 > end_of_smb))) {
f96637be
JP
490 cifs_dbg(VFS, "search entry %p extends after end of SMB %p\n",
491 new_entry, end_of_smb);
09d1db5c 492 return NULL;
3870253e 493 } else
1da177e4
LT
494 return new_entry;
495
496}
497
cda0ec6a
CH
498struct cifs_dirent {
499 const char *name;
500 size_t namelen;
501 u32 resume_key;
502 u64 ino;
503};
504
3d519bd1
AA
505static void cifs_fill_dirent_posix(struct cifs_dirent *de,
506 const struct smb2_posix_info *info)
507{
508 struct smb2_posix_info_parsed parsed;
509
510 /* payload should have already been checked at this point */
511 if (posix_info_parse(info, NULL, &parsed) < 0) {
a0a3036b 512 cifs_dbg(VFS, "Invalid POSIX info payload\n");
3d519bd1
AA
513 return;
514 }
515
516 de->name = parsed.name;
517 de->namelen = parsed.name_len;
518 de->resume_key = info->Ignored;
519 de->ino = le64_to_cpu(info->Inode);
520}
521
cda0ec6a
CH
522static void cifs_fill_dirent_unix(struct cifs_dirent *de,
523 const FILE_UNIX_INFO *info, bool is_unicode)
524{
525 de->name = &info->FileName[0];
526 if (is_unicode)
527 de->namelen = cifs_unicode_bytelen(de->name);
528 else
529 de->namelen = strnlen(de->name, PATH_MAX);
530 de->resume_key = info->ResumeKey;
531 de->ino = le64_to_cpu(info->basic.UniqueId);
532}
533
534static void cifs_fill_dirent_dir(struct cifs_dirent *de,
535 const FILE_DIRECTORY_INFO *info)
536{
537 de->name = &info->FileName[0];
538 de->namelen = le32_to_cpu(info->FileNameLength);
539 de->resume_key = info->FileIndex;
540}
541
542static void cifs_fill_dirent_full(struct cifs_dirent *de,
543 const FILE_FULL_DIRECTORY_INFO *info)
544{
545 de->name = &info->FileName[0];
546 de->namelen = le32_to_cpu(info->FileNameLength);
547 de->resume_key = info->FileIndex;
548}
549
550static void cifs_fill_dirent_search(struct cifs_dirent *de,
551 const SEARCH_ID_FULL_DIR_INFO *info)
552{
553 de->name = &info->FileName[0];
554 de->namelen = le32_to_cpu(info->FileNameLength);
555 de->resume_key = info->FileIndex;
556 de->ino = le64_to_cpu(info->UniqueId);
557}
558
559static void cifs_fill_dirent_both(struct cifs_dirent *de,
560 const FILE_BOTH_DIRECTORY_INFO *info)
561{
562 de->name = &info->FileName[0];
563 de->namelen = le32_to_cpu(info->FileNameLength);
564 de->resume_key = info->FileIndex;
565}
566
567static void cifs_fill_dirent_std(struct cifs_dirent *de,
568 const FIND_FILE_STANDARD_INFO *info)
569{
570 de->name = &info->FileName[0];
e9f49fee 571 /* one byte length, no endianness conversion */
cda0ec6a
CH
572 de->namelen = info->FileNameLength;
573 de->resume_key = info->ResumeKey;
574}
575
576static int cifs_fill_dirent(struct cifs_dirent *de, const void *info,
577 u16 level, bool is_unicode)
578{
579 memset(de, 0, sizeof(*de));
580
581 switch (level) {
3d519bd1
AA
582 case SMB_FIND_FILE_POSIX_INFO:
583 cifs_fill_dirent_posix(de, info);
584 break;
cda0ec6a
CH
585 case SMB_FIND_FILE_UNIX:
586 cifs_fill_dirent_unix(de, info, is_unicode);
587 break;
588 case SMB_FIND_FILE_DIRECTORY_INFO:
589 cifs_fill_dirent_dir(de, info);
590 break;
591 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
592 cifs_fill_dirent_full(de, info);
593 break;
594 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
595 cifs_fill_dirent_search(de, info);
596 break;
597 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
598 cifs_fill_dirent_both(de, info);
599 break;
600 case SMB_FIND_FILE_INFO_STANDARD:
601 cifs_fill_dirent_std(de, info);
602 break;
603 default:
f96637be 604 cifs_dbg(FYI, "Unknown findfirst level %d\n", level);
cda0ec6a
CH
605 return -EINVAL;
606 }
607
608 return 0;
609}
610
1da177e4
LT
611#define UNICODE_DOT cpu_to_le16(0x2e)
612
613/* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
cda0ec6a 614static int cifs_entry_is_dot(struct cifs_dirent *de, bool is_unicode)
1da177e4
LT
615{
616 int rc = 0;
1da177e4 617
cda0ec6a
CH
618 if (!de->name)
619 return 0;
1da177e4 620
cda0ec6a
CH
621 if (is_unicode) {
622 __le16 *ufilename = (__le16 *)de->name;
623 if (de->namelen == 2) {
624 /* check for . */
625 if (ufilename[0] == UNICODE_DOT)
626 rc = 1;
627 } else if (de->namelen == 4) {
628 /* check for .. */
629 if (ufilename[0] == UNICODE_DOT &&
630 ufilename[1] == UNICODE_DOT)
631 rc = 2;
632 }
633 } else /* ASCII */ {
634 if (de->namelen == 1) {
635 if (de->name[0] == '.')
636 rc = 1;
637 } else if (de->namelen == 2) {
638 if (de->name[0] == '.' && de->name[1] == '.')
639 rc = 2;
1da177e4
LT
640 }
641 }
642
643 return rc;
644}
645
eafe8701
SF
646/* Check if directory that we are searching has changed so we can decide
647 whether we can use the cached search results from the previous search */
3870253e 648static int is_dir_changed(struct file *file)
eafe8701 649{
496ad9aa 650 struct inode *inode = file_inode(file);
0b549c4f 651 struct cifsInodeInfo *cifs_inode_info = CIFS_I(inode);
eafe8701 652
0b549c4f
SF
653 if (cifs_inode_info->time == 0)
654 return 1; /* directory was changed, e.g. unlink or new file */
eafe8701
SF
655 else
656 return 0;
657
658}
659
0752f152 660static int cifs_save_resume_key(const char *current_entry,
eaf35b1e 661 struct cifsFileInfo *file_info)
0752f152 662{
eaf35b1e
CH
663 struct cifs_dirent de;
664 int rc;
0752f152 665
eaf35b1e
CH
666 rc = cifs_fill_dirent(&de, current_entry, file_info->srch_inf.info_level,
667 file_info->srch_inf.unicode);
668 if (!rc) {
669 file_info->srch_inf.presume_name = de.name;
670 file_info->srch_inf.resume_name_len = de.namelen;
671 file_info->srch_inf.resume_key = de.resume_key;
0752f152 672 }
0752f152
SF
673 return rc;
674}
675
92fc65a7
PS
676/*
677 * Find the corresponding entry in the search. Note that the SMB server returns
678 * search entries for . and .. which complicates logic here if we choose to
679 * parse for them and we do not assume that they are located in the findfirst
680 * return buffer. We start counting in the buffer with entry 2 and increment for
681 * every entry (do not increment for . or .. entry).
682 */
683static int
be4ccdcc 684find_cifs_entry(const unsigned int xid, struct cifs_tcon *tcon, loff_t pos,
f6f1f179 685 struct file *file, const char *full_path,
d1542cf6 686 char **current_entry, int *num_to_ret)
1da177e4 687{
2608bee7 688 __u16 search_flags;
1da177e4
LT
689 int rc = 0;
690 int pos_in_buf = 0;
691 loff_t first_entry_in_buffer;
be4ccdcc 692 loff_t index_to_find = pos;
92fc65a7 693 struct cifsFileInfo *cfile = file->private_data;
7119e220 694 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
92fc65a7 695 struct TCP_Server_Info *server = tcon->ses->server;
1da177e4 696 /* check if index in the buffer */
50c2f753 697
92fc65a7
PS
698 if (!server->ops->query_dir_first || !server->ops->query_dir_next)
699 return -ENOSYS;
700
701 if ((cfile == NULL) || (current_entry == NULL) || (num_to_ret == NULL))
1da177e4 702 return -ENOENT;
50c2f753 703
92fc65a7
PS
704 *current_entry = NULL;
705 first_entry_in_buffer = cfile->srch_inf.index_of_last_entry -
706 cfile->srch_inf.entries_in_buffer;
60808233 707
92fc65a7
PS
708 /*
709 * If first entry in buf is zero then is first buffer
710 * in search response data which means it is likely . and ..
711 * will be in this buffer, although some servers do not return
712 * . and .. for the root of a drive and for those we need
713 * to start two entries earlier.
714 */
60808233 715
3979877e 716 dump_cifs_file_struct(file, "In fce ");
92fc65a7
PS
717 if (((index_to_find < cfile->srch_inf.index_of_last_entry) &&
718 is_dir_changed(file)) || (index_to_find < first_entry_in_buffer)) {
1da177e4 719 /* close and restart search */
f96637be 720 cifs_dbg(FYI, "search backing up - close and restart search\n");
3afca265 721 spin_lock(&cfile->file_info_lock);
52755808 722 if (server->ops->dir_needs_close(cfile)) {
92fc65a7 723 cfile->invalidHandle = true;
3afca265 724 spin_unlock(&cfile->file_info_lock);
f736906a
PS
725 if (server->ops->close_dir)
726 server->ops->close_dir(xid, tcon, &cfile->fid);
ddb4cbfc 727 } else
3afca265 728 spin_unlock(&cfile->file_info_lock);
92fc65a7 729 if (cfile->srch_inf.ntwrk_buf_start) {
f96637be 730 cifs_dbg(FYI, "freeing SMB ff cache buf on search rewind\n");
92fc65a7
PS
731 if (cfile->srch_inf.smallBuf)
732 cifs_small_buf_release(cfile->srch_inf.
d47d7c1a
SF
733 ntwrk_buf_start);
734 else
92fc65a7 735 cifs_buf_release(cfile->srch_inf.
d47d7c1a 736 ntwrk_buf_start);
e48f9d84 737 /* Reset all pointers to the network buffer to prevent stale references */
92fc65a7 738 cfile->srch_inf.ntwrk_buf_start = NULL;
e48f9d84
WZ
739 cfile->srch_inf.srch_entries_start = NULL;
740 cfile->srch_inf.last_entry = NULL;
1da177e4 741 }
d1542cf6 742 rc = initiate_cifs_search(xid, file, full_path);
4523cc30 743 if (rc) {
f96637be 744 cifs_dbg(FYI, "error %d reinitiating a search on rewind\n",
b6b38f70 745 rc);
1da177e4
LT
746 return rc;
747 }
7023676f 748 /* FindFirst/Next set last_entry to NULL on malformed reply */
92fc65a7
PS
749 if (cfile->srch_inf.last_entry)
750 cifs_save_resume_key(cfile->srch_inf.last_entry, cfile);
1da177e4
LT
751 }
752
2608bee7
SP
753 search_flags = CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME;
754 if (backup_cred(cifs_sb))
755 search_flags |= CIFS_SEARCH_BACKUP_SEARCH;
756
92fc65a7
PS
757 while ((index_to_find >= cfile->srch_inf.index_of_last_entry) &&
758 (rc == 0) && !cfile->srch_inf.endOfSearch) {
f96637be 759 cifs_dbg(FYI, "calling findnext2\n");
92fc65a7
PS
760 rc = server->ops->query_dir_next(xid, tcon, &cfile->fid,
761 search_flags,
762 &cfile->srch_inf);
a7a8fe56
WZ
763 if (rc)
764 return -ENOENT;
7023676f 765 /* FindFirst/Next set last_entry to NULL on malformed reply */
92fc65a7
PS
766 if (cfile->srch_inf.last_entry)
767 cifs_save_resume_key(cfile->srch_inf.last_entry, cfile);
1da177e4 768 }
92fc65a7 769 if (index_to_find < cfile->srch_inf.index_of_last_entry) {
1da177e4
LT
770 /* we found the buffer that contains the entry */
771 /* scan and find it */
772 int i;
92fc65a7 773 char *cur_ent;
59a63e47
RS
774 char *end_of_smb;
775
776 if (cfile->srch_inf.ntwrk_buf_start == NULL) {
777 cifs_dbg(VFS, "ntwrk_buf_start is NULL during readdir\n");
778 return -EIO;
779 }
780
781 end_of_smb = cfile->srch_inf.ntwrk_buf_start +
92fc65a7 782 server->ops->calc_smb_size(
68ed1449 783 cfile->srch_inf.ntwrk_buf_start);
92fc65a7
PS
784
785 cur_ent = cfile->srch_inf.srch_entries_start;
786 first_entry_in_buffer = cfile->srch_inf.index_of_last_entry
787 - cfile->srch_inf.entries_in_buffer;
1da177e4 788 pos_in_buf = index_to_find - first_entry_in_buffer;
f96637be 789 cifs_dbg(FYI, "found entry - pos_in_buf %d\n", pos_in_buf);
5bafd765 790
92fc65a7 791 for (i = 0; (i < (pos_in_buf)) && (cur_ent != NULL); i++) {
dfb7533b 792 /* go entry by entry figuring out which is first */
92fc65a7
PS
793 cur_ent = nxt_dir_entry(cur_ent, end_of_smb,
794 cfile->srch_inf.info_level);
1da177e4 795 }
92fc65a7 796 if ((cur_ent == NULL) && (i < pos_in_buf)) {
1da177e4 797 /* BB fixme - check if we should flag this error */
f96637be
JP
798 cifs_dbg(VFS, "reached end of buf searching for pos in buf %d index to find %lld rc %d\n",
799 pos_in_buf, index_to_find, rc);
1da177e4
LT
800 }
801 rc = 0;
92fc65a7 802 *current_entry = cur_ent;
1da177e4 803 } else {
f96637be 804 cifs_dbg(FYI, "index not in buffer - could not findnext into it\n");
1da177e4
LT
805 return 0;
806 }
807
92fc65a7 808 if (pos_in_buf >= cfile->srch_inf.entries_in_buffer) {
f96637be 809 cifs_dbg(FYI, "can not return entries pos_in_buf beyond last\n");
1da177e4
LT
810 *num_to_ret = 0;
811 } else
92fc65a7 812 *num_to_ret = cfile->srch_inf.entries_in_buffer - pos_in_buf;
1da177e4
LT
813
814 return rc;
815}
816
d87c48ce
RS
817static bool emit_cached_dirents(struct cached_dirents *cde,
818 struct dir_context *ctx)
819{
820 struct cached_dirent *dirent;
780614ce 821 bool rc;
d87c48ce
RS
822
823 list_for_each_entry(dirent, &cde->entries, entry) {
780614ce
RS
824 /*
825 * Skip all early entries prior to the current lseek()
826 * position.
827 */
828 if (ctx->pos > dirent->pos)
d87c48ce 829 continue;
780614ce
RS
830 /*
831 * We recorded the current ->pos value for the dirent
832 * when we stored it in the cache.
833 * However, this sequence of ->pos values may have holes
834 * in it, for example dot-dirs returned from the server
835 * are suppressed.
e9f49fee 836 * Handle this by forcing ctx->pos to be the same as the
780614ce
RS
837 * ->pos of the current dirent we emit from the cache.
838 * This means that when we emit these entries from the cache
839 * we now emit them with the same ->pos value as in the
840 * initial scan.
841 */
d87c48ce
RS
842 ctx->pos = dirent->pos;
843 rc = dir_emit(ctx, dirent->name, dirent->namelen,
844 dirent->fattr.cf_uniqueid,
845 dirent->fattr.cf_dtype);
846 if (!rc)
847 return rc;
780614ce 848 ctx->pos++;
d87c48ce
RS
849 }
850 return true;
851}
852
853static void update_cached_dirents_count(struct cached_dirents *cde,
72dd7961 854 struct file *file)
d87c48ce 855{
72dd7961 856 if (cde->file != file)
d87c48ce
RS
857 return;
858 if (cde->is_valid || cde->is_failed)
859 return;
860
861 cde->pos++;
862}
863
864static void finished_cached_dirents_count(struct cached_dirents *cde,
72dd7961 865 struct dir_context *ctx, struct file *file)
d87c48ce 866{
72dd7961 867 if (cde->file != file)
d87c48ce
RS
868 return;
869 if (cde->is_valid || cde->is_failed)
870 return;
871 if (ctx->pos != cde->pos)
872 return;
873
874 cde->is_valid = 1;
875}
876
877static void add_cached_dirent(struct cached_dirents *cde,
878 struct dir_context *ctx,
879 const char *name, int namelen,
72dd7961
BS
880 struct cifs_fattr *fattr,
881 struct file *file)
d87c48ce
RS
882{
883 struct cached_dirent *de;
884
72dd7961 885 if (cde->file != file)
d87c48ce
RS
886 return;
887 if (cde->is_valid || cde->is_failed)
888 return;
889 if (ctx->pos != cde->pos) {
890 cde->is_failed = 1;
891 return;
892 }
893 de = kzalloc(sizeof(*de), GFP_ATOMIC);
894 if (de == NULL) {
895 cde->is_failed = 1;
896 return;
897 }
898 de->namelen = namelen;
899 de->name = kstrndup(name, namelen, GFP_ATOMIC);
900 if (de->name == NULL) {
901 kfree(de);
902 cde->is_failed = 1;
903 return;
904 }
905 de->pos = ctx->pos;
906
907 memcpy(&de->fattr, fattr, sizeof(struct cifs_fattr));
908
909 list_add_tail(&de->entry, &cde->entries);
910}
911
912static bool cifs_dir_emit(struct dir_context *ctx,
913 const char *name, int namelen,
914 struct cifs_fattr *fattr,
72dd7961
BS
915 struct cached_fid *cfid,
916 struct file *file)
d87c48ce
RS
917{
918 bool rc;
919 ino_t ino = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
920
921 rc = dir_emit(ctx, name, namelen, ino, fattr->cf_dtype);
922 if (!rc)
923 return rc;
924
925 if (cfid) {
926 mutex_lock(&cfid->dirents.de_mutex);
927 add_cached_dirent(&cfid->dirents, ctx, name, namelen,
72dd7961 928 fattr, file);
d87c48ce
RS
929 mutex_unlock(&cfid->dirents.de_mutex);
930 }
931
932 return rc;
933}
934
be4ccdcc 935static int cifs_filldir(char *find_entry, struct file *file,
d87c48ce
RS
936 struct dir_context *ctx,
937 char *scratch_buf, unsigned int max_len,
938 struct cached_fid *cfid)
1da177e4 939{
9feed6f8 940 struct cifsFileInfo *file_info = file->private_data;
7119e220 941 struct super_block *sb = file_inode(file)->i_sb;
9feed6f8 942 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
cda0ec6a 943 struct cifs_dirent de = { NULL, };
cc0bad75 944 struct cifs_fattr fattr;
9feed6f8 945 struct qstr name;
1da177e4 946 int rc = 0;
1da177e4 947
cda0ec6a
CH
948 rc = cifs_fill_dirent(&de, find_entry, file_info->srch_inf.info_level,
949 file_info->srch_inf.unicode);
950 if (rc)
951 return rc;
5bafd765 952
f16d59b4 953 if (de.namelen > max_len) {
f96637be
JP
954 cifs_dbg(VFS, "bad search response length %zd past smb end\n",
955 de.namelen);
5bafd765
SF
956 return -EINVAL;
957 }
958
60808233 959 /* skip . and .. since we added them first */
cda0ec6a 960 if (cifs_entry_is_dot(&de, file_info->srch_inf.unicode))
60808233
SF
961 return 0;
962
f16d59b4
CH
963 if (file_info->srch_inf.unicode) {
964 struct nls_table *nlt = cifs_sb->local_nls;
b693855f
SF
965 int map_type;
966
2baa2682 967 map_type = cifs_remap(cifs_sb);
f16d59b4
CH
968 name.name = scratch_buf;
969 name.len =
acbbb76a
SF
970 cifs_from_utf16((char *)name.name, (__le16 *)de.name,
971 UNICODE_NAME_MAX,
972 min_t(size_t, de.namelen,
b693855f 973 (size_t)max_len), nlt, map_type);
f16d59b4
CH
974 name.len -= nls_nullsize(nlt);
975 } else {
976 name.name = de.name;
977 name.len = de.namelen;
978 }
1da177e4 979
9feed6f8 980 switch (file_info->srch_inf.info_level) {
3d519bd1
AA
981 case SMB_FIND_FILE_POSIX_INFO:
982 cifs_posix_to_fattr(&fattr,
983 (struct smb2_posix_info *)find_entry,
984 cifs_sb);
985 break;
9feed6f8 986 case SMB_FIND_FILE_UNIX:
cc0bad75 987 cifs_unix_basic_to_fattr(&fattr,
9feed6f8
CH
988 &((FILE_UNIX_INFO *)find_entry)->basic,
989 cifs_sb);
76894f3e
PA
990 if (S_ISLNK(fattr.cf_mode))
991 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
9feed6f8
CH
992 break;
993 case SMB_FIND_FILE_INFO_STANDARD:
994 cifs_std_info_to_fattr(&fattr,
995 (FIND_FILE_STANDARD_INFO *)find_entry,
996 cifs_sb);
997 break;
55c7788c 998 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
046aca3c 999 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
55c7788c 1000 cifs_fulldir_info_to_fattr(&fattr, find_entry, cifs_sb);
046aca3c 1001 break;
9feed6f8
CH
1002 default:
1003 cifs_dir_info_to_fattr(&fattr,
1004 (FILE_DIRECTORY_INFO *)find_entry,
1005 cifs_sb);
1006 break;
1007 }
b835bebe 1008
f16d59b4
CH
1009 if (de.ino && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
1010 fattr.cf_uniqueid = de.ino;
ec06aedd 1011 } else {
0b8f18e3 1012 fattr.cf_uniqueid = iunique(sb, ROOT_I);
ec06aedd
JL
1013 cifs_autodisable_serverino(cifs_sb);
1014 }
50c2f753 1015
1b12b9c1 1016 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) &&
cb084b1a 1017 couldbe_mf_symlink(&fattr))
1b12b9c1
SM
1018 /*
1019 * trying to get the type and mode can be slow,
1020 * so just call those regular files for now, and mark
1021 * for reval
1022 */
1023 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
1024
1f1735cb 1025 cifs_prime_dcache(file_dentry(file), &name, &fattr);
3870253e 1026
d87c48ce 1027 return !cifs_dir_emit(ctx, name.name, name.len,
72dd7961 1028 &fattr, cfid, file);
1da177e4
LT
1029}
1030
1da177e4 1031
be4ccdcc 1032int cifs_readdir(struct file *file, struct dir_context *ctx)
1da177e4
LT
1033{
1034 int rc = 0;
6d5786a3
PS
1035 unsigned int xid;
1036 int i;
d87c48ce 1037 struct tcon_link *tlink = NULL;
92fc65a7 1038 struct cifs_tcon *tcon;
d87c48ce 1039 struct cifsFileInfo *cifsFile;
3870253e 1040 char *current_entry;
1da177e4 1041 int num_to_fill = 0;
3870253e 1042 char *tmp_buf = NULL;
50c2f753 1043 char *end_of_smb;
18295796 1044 unsigned int max_len;
f6a9bc33
AV
1045 const char *full_path;
1046 void *page = alloc_dentry_path();
d87c48ce
RS
1047 struct cached_fid *cfid = NULL;
1048 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
1da177e4 1049
6d5786a3 1050 xid = get_xid();
1da177e4 1051
f6a9bc33
AV
1052 full_path = build_path_from_dentry(file_dentry(file), page);
1053 if (IS_ERR(full_path)) {
1054 rc = PTR_ERR(full_path);
d1542cf6
RS
1055 goto rddir2_exit;
1056 }
1057
d87c48ce
RS
1058 if (file->private_data == NULL) {
1059 tlink = cifs_sb_tlink(cifs_sb);
1060 if (IS_ERR(tlink))
1061 goto cache_not_found;
1062 tcon = tlink_tcon(tlink);
1063 } else {
1064 cifsFile = file->private_data;
1065 tcon = tlink_tcon(cifsFile->tlink);
1066 }
1067
7eb59a98 1068 rc = open_cached_dir(xid, tcon, full_path, cifs_sb, false, &cfid);
d87c48ce
RS
1069 cifs_put_tlink(tlink);
1070 if (rc)
1071 goto cache_not_found;
1072
1073 mutex_lock(&cfid->dirents.de_mutex);
1074 /*
1075 * If this was reading from the start of the directory
1076 * we need to initialize scanning and storing the
1077 * directory content.
1078 */
72dd7961
BS
1079 if (ctx->pos == 0 && cfid->dirents.file == NULL) {
1080 cfid->dirents.file = file;
d87c48ce
RS
1081 cfid->dirents.pos = 2;
1082 }
1083 /*
1084 * If we already have the entire directory cached then
1085 * we can just serve the cache.
1086 */
1087 if (cfid->dirents.is_valid) {
1088 if (!dir_emit_dots(file, ctx)) {
1089 mutex_unlock(&cfid->dirents.de_mutex);
1090 goto rddir2_exit;
1091 }
1092 emit_cached_dirents(&cfid->dirents, ctx);
1093 mutex_unlock(&cfid->dirents.de_mutex);
1094 goto rddir2_exit;
1095 }
1096 mutex_unlock(&cfid->dirents.de_mutex);
1097
1098 /* Drop the cache while calling initiate_cifs_search and
1099 * find_cifs_entry in case there will be reconnects during
1100 * query_directory.
1101 */
44a48081
SF
1102 close_cached_dir(cfid);
1103 cfid = NULL;
d87c48ce
RS
1104
1105 cache_not_found:
6221ddd0
SJ
1106 /*
1107 * Ensure FindFirst doesn't fail before doing filldir() for '.' and
1108 * '..'. Otherwise we won't be able to notify VFS in case of failure.
1109 */
1110 if (file->private_data == NULL) {
d1542cf6 1111 rc = initiate_cifs_search(xid, file, full_path);
f96637be 1112 cifs_dbg(FYI, "initiate cifs search rc %d\n", rc);
6221ddd0
SJ
1113 if (rc)
1114 goto rddir2_exit;
1115 }
1116
be4ccdcc
AV
1117 if (!dir_emit_dots(file, ctx))
1118 goto rddir2_exit;
1da177e4 1119
be4ccdcc
AV
1120 /* 1) If search is active,
1121 is in current search buffer?
1122 if it before then restart search
1123 if after then keep searching till find it */
be4ccdcc
AV
1124 cifsFile = file->private_data;
1125 if (cifsFile->srch_inf.endOfSearch) {
1126 if (cifsFile->srch_inf.emptyDir) {
1127 cifs_dbg(FYI, "End of search, empty dir\n");
1128 rc = 0;
1da177e4
LT
1129 goto rddir2_exit;
1130 }
be4ccdcc
AV
1131 } /* else {
1132 cifsFile->invalidHandle = true;
1133 tcon->ses->server->close(xid, tcon, &cifsFile->fid);
1134 } */
1135
1136 tcon = tlink_tcon(cifsFile->tlink);
d1542cf6
RS
1137 rc = find_cifs_entry(xid, tcon, ctx->pos, file, full_path,
1138 &current_entry, &num_to_fill);
7eb59a98 1139 open_cached_dir(xid, tcon, full_path, cifs_sb, false, &cfid);
be4ccdcc
AV
1140 if (rc) {
1141 cifs_dbg(FYI, "fce error %d\n", rc);
1142 goto rddir2_exit;
1143 } else if (current_entry != NULL) {
1144 cifs_dbg(FYI, "entry %lld found\n", ctx->pos);
1145 } else {
d87c48ce
RS
1146 if (cfid) {
1147 mutex_lock(&cfid->dirents.de_mutex);
72dd7961 1148 finished_cached_dirents_count(&cfid->dirents, ctx, file);
d87c48ce
RS
1149 mutex_unlock(&cfid->dirents.de_mutex);
1150 }
a0a3036b 1151 cifs_dbg(FYI, "Could not find entry\n");
be4ccdcc
AV
1152 goto rddir2_exit;
1153 }
1154 cifs_dbg(FYI, "loop through %d times filling dir for net buf %p\n",
1155 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start);
1156 max_len = tcon->ses->server->ops->calc_smb_size(
68ed1449 1157 cifsFile->srch_inf.ntwrk_buf_start);
be4ccdcc
AV
1158 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1159
1160 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
1161 if (tmp_buf == NULL) {
1162 rc = -ENOMEM;
1163 goto rddir2_exit;
1164 }
1165
1166 for (i = 0; i < num_to_fill; i++) {
1167 if (current_entry == NULL) {
1168 /* evaluate whether this case is an error */
1169 cifs_dbg(VFS, "past SMB end, num to fill %d i %d\n",
1170 num_to_fill, i);
f55fdcca
KV
1171 break;
1172 }
be4ccdcc
AV
1173 /*
1174 * if buggy server returns . and .. late do we want to
1175 * check for that here?
1176 */
01b9b0b2 1177 *tmp_buf = 0;
be4ccdcc 1178 rc = cifs_filldir(current_entry, file, ctx,
d87c48ce 1179 tmp_buf, max_len, cfid);
be4ccdcc
AV
1180 if (rc) {
1181 if (rc > 0)
7ca85ba7 1182 rc = 0;
be4ccdcc 1183 break;
1da177e4 1184 }
be4ccdcc
AV
1185
1186 ctx->pos++;
d87c48ce
RS
1187 if (cfid) {
1188 mutex_lock(&cfid->dirents.de_mutex);
72dd7961 1189 update_cached_dirents_count(&cfid->dirents, file);
d87c48ce
RS
1190 mutex_unlock(&cfid->dirents.de_mutex);
1191 }
1192
be4ccdcc
AV
1193 if (ctx->pos ==
1194 cifsFile->srch_inf.index_of_last_entry) {
1195 cifs_dbg(FYI, "last entry in buf at pos %lld %s\n",
1196 ctx->pos, tmp_buf);
1197 cifs_save_resume_key(current_entry, cifsFile);
1198 break;
780614ce
RS
1199 }
1200 current_entry =
1201 nxt_dir_entry(current_entry, end_of_smb,
1202 cifsFile->srch_inf.info_level);
be4ccdcc
AV
1203 }
1204 kfree(tmp_buf);
1da177e4
LT
1205
1206rddir2_exit:
d87c48ce
RS
1207 if (cfid)
1208 close_cached_dir(cfid);
f6a9bc33 1209 free_dentry_path(page);
6d5786a3 1210 free_xid(xid);
1da177e4
LT
1211 return rc;
1212}