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