xref: /linux/fs/smb/client/readdir.c (revision 63e62baaa72e1aceb422f64a50408bc9b02a6022)
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 "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"
23 #include "smb2proto.h"
24 #include "fs_context.h"
25 #include "cached_dir.h"
26 #include "reparse.h"
27 
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 
35 #ifdef CONFIG_CIFS_DEBUG2
dump_cifs_file_struct(struct file * file,char * label)36 static void dump_cifs_file_struct(struct file *file, char *label)
37 {
38 	struct cifsFileInfo *cf;
39 
40 	if (file) {
41 		cf = file->private_data;
42 		if (cf == NULL) {
43 			cifs_dbg(FYI, "empty cifs private file data\n");
44 			return;
45 		}
46 		if (cf->invalidHandle)
47 			cifs_dbg(FYI, "Invalid handle\n");
48 		if (cf->srch_inf.endOfSearch)
49 			cifs_dbg(FYI, "end of search\n");
50 		if (cf->srch_inf.emptyDir)
51 			cifs_dbg(FYI, "empty dir\n");
52 	}
53 }
54 #else
dump_cifs_file_struct(struct file * file,char * label)55 static inline void dump_cifs_file_struct(struct file *file, char *label)
56 {
57 }
58 #endif /* DEBUG2 */
59 
60 /*
61  * Attempt to preload the dcache with the results from the FIND_FIRST/NEXT
62  *
63  * Find the dentry that matches "name". If there isn't one, create one. If it's
64  * a negative dentry or the uniqueid or filetype(mode) changed,
65  * then drop it and recreate it.
66  */
67 static void
cifs_prime_dcache(struct dentry * parent,struct qstr * name,struct cifs_fattr * fattr)68 cifs_prime_dcache(struct dentry *parent, struct qstr *name,
69 		    struct cifs_fattr *fattr)
70 {
71 	struct dentry *dentry, *alias;
72 	struct inode *inode;
73 	struct super_block *sb = parent->d_sb;
74 	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
75 	bool posix = cifs_sb_master_tcon(cifs_sb)->posix_extensions;
76 	bool reparse_need_reval = false;
77 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
78 	int rc;
79 
80 	cifs_dbg(FYI, "%s: for %s\n", __func__, name->name);
81 
82 	dentry = try_lookup_noperm(name, parent);
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 		 */
90 retry:
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 ||
106 		    (fattr->cf_flags & CIFS_FATTR_NEED_REVAL))
107 			return;
108 
109 		dentry = d_alloc_parallel(parent, name, &wq);
110 	}
111 	if (IS_ERR(dentry))
112 		return;
113 	if (!d_in_lookup(dentry)) {
114 		inode = d_inode(dentry);
115 		if (inode) {
116 			if (d_mountpoint(dentry)) {
117 				dput(dentry);
118 				return;
119 			}
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 
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
137 				 * not clobbering some existing attributes when
138 				 * reparse tag and ctime haven't changed.
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;
144 						fattr->cf_rdev = inode->i_rdev;
145 						fattr->cf_uid = inode->i_uid;
146 						fattr->cf_gid = inode->i_gid;
147 						fattr->cf_eof = CIFS_I(inode)->netfs.remote_i_size;
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
cifs_fill_common_info(struct cifs_fattr * fattr,struct cifs_sb_info * cifs_sb)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 
182 	fattr->cf_uid = cifs_sb->ctx->linux_uid;
183 	fattr->cf_gid = cifs_sb->ctx->linux_gid;
184 
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 	 */
193 	if ((fattr->cf_cifsattrs & ATTR_REPARSE) &&
194 	    cifs_reparse_point_to_fattr(cifs_sb, fattr, &data))
195 		goto out_reparse;
196 
197 	if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
198 		fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
199 		fattr->cf_dtype = DT_DIR;
200 	} else {
201 		fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
202 		fattr->cf_dtype = DT_REG;
203 	}
204 
205 out_reparse:
206 	/* non-unix readdir doesn't provide nlink */
207 	fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
208 
209 	if (fattr->cf_cifsattrs & ATTR_READONLY)
210 		fattr->cf_mode &= ~S_IWUGO;
211 
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 	 */
219 	if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
220 	    (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID))
221 		fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
222 
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;
229 		} else {
230 			/*
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
234 			 */
235 			fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
236 		}
237 	}
238 }
239 
240 /* Fill a cifs_fattr struct with info from SMB_FIND_FILE_POSIX_INFO. */
241 static void
cifs_posix_to_fattr(struct cifs_fattr * fattr,struct smb2_posix_info * info,struct cifs_sb_info * cifs_sb)242 cifs_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 
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 */
265 	fattr->cf_mode = wire_mode_to_posix(le32_to_cpu(info->Mode),
266 					    fattr->cf_cifsattrs & ATTR_DIRECTORY);
267 	fattr->cf_dtype = S_DT(fattr->cf_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 	}
278 
279 	cifs_dbg(FYI, "posix fattr: dev %d, reparse %d, mode %o\n",
280 		 le32_to_cpu(info->DeviceId),
281 		 le32_to_cpu(info->ReparseTag),
282 		 le32_to_cpu(info->Mode));
283 
284 	sid_to_id(cifs_sb, &parsed.owner, fattr, SIDOWNER);
285 	sid_to_id(cifs_sb, &parsed.group, fattr, SIDGROUP);
286 }
287 
__dir_info_to_fattr(struct cifs_fattr * fattr,const void * info)288 static 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 
302 void
cifs_dir_info_to_fattr(struct cifs_fattr * fattr,FILE_DIRECTORY_INFO * info,struct cifs_sb_info * cifs_sb)303 cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info,
304 		       struct cifs_sb_info *cifs_sb)
305 {
306 	__dir_info_to_fattr(fattr, info);
307 	cifs_fill_common_info(fattr, cifs_sb);
308 }
309 
cifs_fulldir_info_to_fattr(struct cifs_fattr * fattr,const void * info,struct cifs_sb_info * cifs_sb)310 static void cifs_fulldir_info_to_fattr(struct cifs_fattr *fattr,
311 				       const void *info,
312 				       struct cifs_sb_info *cifs_sb)
313 {
314 	const FILE_FULL_DIRECTORY_INFO *di = info;
315 
316 	__dir_info_to_fattr(fattr, info);
317 
318 	/* See MS-FSCC 2.4.14, 2.4.19 */
319 	if (fattr->cf_cifsattrs & ATTR_REPARSE)
320 		fattr->cf_cifstag = le32_to_cpu(di->EaSize);
321 	cifs_fill_common_info(fattr, cifs_sb);
322 }
323 
324 static void
cifs_std_info_to_fattr(struct cifs_fattr * fattr,FIND_FILE_STANDARD_INFO * info,struct cifs_sb_info * cifs_sb)325 cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info,
326 		       struct cifs_sb_info *cifs_sb)
327 {
328 	int offset = cifs_sb_master_tcon(cifs_sb)->ses->server->timeAdj;
329 
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);
343 }
344 
345 static int
_initiate_cifs_search(const unsigned int xid,struct file * file,const char * full_path)346 _initiate_cifs_search(const unsigned int xid, struct file *file,
347 		     const char *full_path)
348 {
349 	__u16 search_flags;
350 	int rc = 0;
351 	struct cifsFileInfo *cifsFile;
352 	struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
353 	struct tcon_link *tlink = NULL;
354 	struct cifs_tcon *tcon;
355 	struct TCP_Server_Info *server;
356 
357 	if (file->private_data == NULL) {
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 		}
367 		spin_lock_init(&cifsFile->file_info_lock);
368 		file->private_data = cifsFile;
369 		cifsFile->tlink = cifs_get_tlink(tlink);
370 		tcon = tlink_tcon(tlink);
371 	} else {
372 		cifsFile = file->private_data;
373 		tcon = tlink_tcon(cifsFile->tlink);
374 	}
375 
376 	server = tcon->ses->server;
377 
378 	if (!server->ops->query_dir_first) {
379 		rc = -ENOSYS;
380 		goto error_exit;
381 	}
382 
383 	cifsFile->invalidHandle = true;
384 	cifsFile->srch_inf.endOfSearch = false;
385 
386 	cifs_dbg(FYI, "Full path: %s start at: %lld\n", full_path, file->f_pos);
387 
388 ffirst_retry:
389 	/* test for Unix extensions */
390 	/* but now check for them on the share/mount not on the SMB session */
391 	/* if (cap_unix(tcon->ses) { */
392 	if (tcon->unix_ext)
393 		cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
394 	else if (tcon->posix_extensions)
395 		cifsFile->srch_inf.info_level = SMB_FIND_FILE_POSIX_INFO;
396 	else if ((tcon->ses->capabilities &
397 		  tcon->ses->server->vals->cap_nt_find) == 0) {
398 		cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
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? */ {
402 		cifsFile->srch_inf.info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
403 	}
404 
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 
409 	rc = server->ops->query_dir_first(xid, tcon, full_path, cifs_sb,
410 					  &cifsFile->fid, search_flags,
411 					  &cifsFile->srch_inf);
412 
413 	if (rc == 0) {
414 		cifsFile->invalidHandle = false;
415 	} else if ((rc == -EOPNOTSUPP) &&
416 		   (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
417 		cifs_autodisable_serverino(cifs_sb);
418 		goto ffirst_retry;
419 	}
420 error_exit:
421 	cifs_put_tlink(tlink);
422 	return rc;
423 }
424 
425 static int
initiate_cifs_search(const unsigned int xid,struct file * file,const char * full_path)426 initiate_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 
446 /* return length of unicode string in bytes */
cifs_unicode_bytelen(const char * str)447 static int cifs_unicode_bytelen(const char *str)
448 {
449 	int len;
450 	const __le16 *ustr = (const __le16 *)str;
451 
452 	for (len = 0; len <= PATH_MAX; len++) {
453 		if (ustr[len] == 0)
454 			return len << 1;
455 	}
456 	cifs_dbg(FYI, "Unicode string longer than PATH_MAX found\n");
457 	return len << 1;
458 }
459 
nxt_dir_entry(char * old_entry,char * end_of_smb,int level)460 static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
461 {
462 	char *new_entry;
463 	FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
464 
465 	if (level == SMB_FIND_FILE_INFO_STANDARD) {
466 		FIND_FILE_STANDARD_INFO *pfData;
467 		pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
468 
469 		new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) + 1 +
470 				pfData->FileNameLength;
471 	} else {
472 		u32 next_offset = le32_to_cpu(pDirInfo->NextEntryOffset);
473 
474 		if (old_entry + next_offset < old_entry) {
475 			cifs_dbg(VFS, "Invalid offset %u\n", next_offset);
476 			return NULL;
477 		}
478 		new_entry = old_entry + next_offset;
479 	}
480 	cifs_dbg(FYI, "new entry %p old entry %p\n", new_entry, old_entry);
481 	/* validate that new_entry is not past end of SMB */
482 	if (new_entry >= end_of_smb) {
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);
485 		return NULL;
486 	} else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
487 		    (new_entry + sizeof(FIND_FILE_STANDARD_INFO) + 1 > end_of_smb))
488 		  || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
489 		   (new_entry + sizeof(FILE_DIRECTORY_INFO) + 1 > end_of_smb)))  {
490 		cifs_dbg(VFS, "search entry %p extends after end of SMB %p\n",
491 			 new_entry, end_of_smb);
492 		return NULL;
493 	} else
494 		return new_entry;
495 
496 }
497 
498 struct cifs_dirent {
499 	const char	*name;
500 	size_t		namelen;
501 	u32		resume_key;
502 	u64		ino;
503 };
504 
cifs_fill_dirent_posix(struct cifs_dirent * de,const struct smb2_posix_info * info)505 static 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) {
512 		cifs_dbg(VFS, "Invalid POSIX info payload\n");
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 
cifs_fill_dirent_unix(struct cifs_dirent * de,const FILE_UNIX_INFO * info,bool is_unicode)522 static 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 
cifs_fill_dirent_dir(struct cifs_dirent * de,const FILE_DIRECTORY_INFO * info)534 static 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 
cifs_fill_dirent_full(struct cifs_dirent * de,const FILE_FULL_DIRECTORY_INFO * info)542 static 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 
cifs_fill_dirent_search(struct cifs_dirent * de,const SEARCH_ID_FULL_DIR_INFO * info)550 static 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 
cifs_fill_dirent_both(struct cifs_dirent * de,const FILE_BOTH_DIRECTORY_INFO * info)559 static 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 
cifs_fill_dirent_std(struct cifs_dirent * de,const FIND_FILE_STANDARD_INFO * info)567 static void cifs_fill_dirent_std(struct cifs_dirent *de,
568 		const FIND_FILE_STANDARD_INFO *info)
569 {
570 	de->name = &info->FileName[0];
571 	/* one byte length, no endianness conversion */
572 	de->namelen = info->FileNameLength;
573 	de->resume_key = info->ResumeKey;
574 }
575 
cifs_fill_dirent(struct cifs_dirent * de,const void * info,u16 level,bool is_unicode)576 static 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) {
582 	case SMB_FIND_FILE_POSIX_INFO:
583 		cifs_fill_dirent_posix(de, info);
584 		break;
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:
604 		cifs_dbg(FYI, "Unknown findfirst level %d\n", level);
605 		return -EINVAL;
606 	}
607 
608 	return 0;
609 }
610 
611 #define UNICODE_DOT cpu_to_le16(0x2e)
612 
613 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
cifs_entry_is_dot(struct cifs_dirent * de,bool is_unicode)614 static int cifs_entry_is_dot(struct cifs_dirent *de, bool is_unicode)
615 {
616 	int rc = 0;
617 
618 	if (!de->name)
619 		return 0;
620 
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;
640 		}
641 	}
642 
643 	return rc;
644 }
645 
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 */
is_dir_changed(struct file * file)648 static int is_dir_changed(struct file *file)
649 {
650 	struct inode *inode = file_inode(file);
651 	struct cifsInodeInfo *cifs_inode_info = CIFS_I(inode);
652 
653 	if (cifs_inode_info->time == 0)
654 		return 1; /* directory was changed, e.g. unlink or new file */
655 	else
656 		return 0;
657 
658 }
659 
cifs_save_resume_key(const char * current_entry,struct cifsFileInfo * file_info)660 static int cifs_save_resume_key(const char *current_entry,
661 	struct cifsFileInfo *file_info)
662 {
663 	struct cifs_dirent de;
664 	int rc;
665 
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;
672 	}
673 	return rc;
674 }
675 
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  */
683 static int
find_cifs_entry(const unsigned int xid,struct cifs_tcon * tcon,loff_t pos,struct file * file,const char * full_path,char ** current_entry,int * num_to_ret)684 find_cifs_entry(const unsigned int xid, struct cifs_tcon *tcon, loff_t pos,
685 		struct file *file, const char *full_path,
686 		char **current_entry, int *num_to_ret)
687 {
688 	__u16 search_flags;
689 	int rc = 0;
690 	int pos_in_buf = 0;
691 	loff_t first_entry_in_buffer;
692 	loff_t index_to_find = pos;
693 	struct cifsFileInfo *cfile = file->private_data;
694 	struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
695 	struct TCP_Server_Info *server = tcon->ses->server;
696 	/* check if index in the buffer */
697 
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))
702 		return -ENOENT;
703 
704 	*current_entry = NULL;
705 	first_entry_in_buffer = cfile->srch_inf.index_of_last_entry -
706 					cfile->srch_inf.entries_in_buffer;
707 
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 	 */
715 
716 	dump_cifs_file_struct(file, "In fce ");
717 	if (((index_to_find < cfile->srch_inf.index_of_last_entry) &&
718 	     is_dir_changed(file)) || (index_to_find < first_entry_in_buffer)) {
719 		/* close and restart search */
720 		cifs_dbg(FYI, "search backing up - close and restart search\n");
721 		spin_lock(&cfile->file_info_lock);
722 		if (server->ops->dir_needs_close(cfile)) {
723 			cfile->invalidHandle = true;
724 			spin_unlock(&cfile->file_info_lock);
725 			if (server->ops->close_dir)
726 				server->ops->close_dir(xid, tcon, &cfile->fid);
727 		} else
728 			spin_unlock(&cfile->file_info_lock);
729 		if (cfile->srch_inf.ntwrk_buf_start) {
730 			cifs_dbg(FYI, "freeing SMB ff cache buf on search rewind\n");
731 			if (cfile->srch_inf.smallBuf)
732 				cifs_small_buf_release(cfile->srch_inf.
733 						ntwrk_buf_start);
734 			else
735 				cifs_buf_release(cfile->srch_inf.
736 						ntwrk_buf_start);
737 			/* Reset all pointers to the network buffer to prevent stale references */
738 			cfile->srch_inf.ntwrk_buf_start = NULL;
739 			cfile->srch_inf.srch_entries_start = NULL;
740 			cfile->srch_inf.last_entry = NULL;
741 		}
742 		rc = initiate_cifs_search(xid, file, full_path);
743 		if (rc) {
744 			cifs_dbg(FYI, "error %d reinitiating a search on rewind\n",
745 				 rc);
746 			return rc;
747 		}
748 		/* FindFirst/Next set last_entry to NULL on malformed reply */
749 		if (cfile->srch_inf.last_entry)
750 			cifs_save_resume_key(cfile->srch_inf.last_entry, cfile);
751 	}
752 
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 
757 	while ((index_to_find >= cfile->srch_inf.index_of_last_entry) &&
758 	       (rc == 0) && !cfile->srch_inf.endOfSearch) {
759 		cifs_dbg(FYI, "calling findnext2\n");
760 		rc = server->ops->query_dir_next(xid, tcon, &cfile->fid,
761 						 search_flags,
762 						 &cfile->srch_inf);
763 		if (rc)
764 			return -ENOENT;
765 		/* FindFirst/Next set last_entry to NULL on malformed reply */
766 		if (cfile->srch_inf.last_entry)
767 			cifs_save_resume_key(cfile->srch_inf.last_entry, cfile);
768 	}
769 	if (index_to_find < cfile->srch_inf.index_of_last_entry) {
770 		/* we found the buffer that contains the entry */
771 		/* scan and find it */
772 		int i;
773 		char *cur_ent;
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 +
782 			server->ops->calc_smb_size(
783 					cfile->srch_inf.ntwrk_buf_start);
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;
788 		pos_in_buf = index_to_find - first_entry_in_buffer;
789 		cifs_dbg(FYI, "found entry - pos_in_buf %d\n", pos_in_buf);
790 
791 		for (i = 0; (i < (pos_in_buf)) && (cur_ent != NULL); i++) {
792 			/* go entry by entry figuring out which is first */
793 			cur_ent = nxt_dir_entry(cur_ent, end_of_smb,
794 						cfile->srch_inf.info_level);
795 		}
796 		if ((cur_ent == NULL) && (i < pos_in_buf)) {
797 			/* BB fixme - check if we should flag this error */
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);
800 		}
801 		rc = 0;
802 		*current_entry = cur_ent;
803 	} else {
804 		cifs_dbg(FYI, "index not in buffer - could not findnext into it\n");
805 		return 0;
806 	}
807 
808 	if (pos_in_buf >= cfile->srch_inf.entries_in_buffer) {
809 		cifs_dbg(FYI, "can not return entries pos_in_buf beyond last\n");
810 		*num_to_ret = 0;
811 	} else
812 		*num_to_ret = cfile->srch_inf.entries_in_buffer - pos_in_buf;
813 
814 	return rc;
815 }
816 
emit_cached_dirents(struct cached_dirents * cde,struct dir_context * ctx)817 static bool emit_cached_dirents(struct cached_dirents *cde,
818 				struct dir_context *ctx)
819 {
820 	struct cached_dirent *dirent;
821 	bool rc;
822 
823 	list_for_each_entry(dirent, &cde->entries, entry) {
824 		/*
825 		 * Skip all early entries prior to the current lseek()
826 		 * position.
827 		 */
828 		if (ctx->pos > dirent->pos)
829 			continue;
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.
836 		 * Handle this by forcing ctx->pos to be the same as the
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 		 */
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;
848 		ctx->pos++;
849 	}
850 	return true;
851 }
852 
update_cached_dirents_count(struct cached_dirents * cde,struct file * file)853 static void update_cached_dirents_count(struct cached_dirents *cde,
854 					struct file *file)
855 {
856 	if (cde->file != file)
857 		return;
858 	if (cde->is_valid || cde->is_failed)
859 		return;
860 
861 	cde->pos++;
862 }
863 
finished_cached_dirents_count(struct cached_dirents * cde,struct dir_context * ctx,struct file * file)864 static void finished_cached_dirents_count(struct cached_dirents *cde,
865 					struct dir_context *ctx, struct file *file)
866 {
867 	if (cde->file != file)
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 
add_cached_dirent(struct cached_dirents * cde,struct dir_context * ctx,const char * name,int namelen,struct cifs_fattr * fattr,struct file * file)877 static bool add_cached_dirent(struct cached_dirents *cde,
878 			      struct dir_context *ctx, const char *name,
879 			      int namelen, struct cifs_fattr *fattr,
880 			      struct file *file)
881 {
882 	struct cached_dirent *de;
883 
884 	if (cde->file != file)
885 		return false;
886 	if (cde->is_valid || cde->is_failed)
887 		return false;
888 	if (ctx->pos != cde->pos) {
889 		cde->is_failed = 1;
890 		return false;
891 	}
892 	de = kzalloc(sizeof(*de), GFP_ATOMIC);
893 	if (de == NULL) {
894 		cde->is_failed = 1;
895 		return false;
896 	}
897 	de->namelen = namelen;
898 	de->name = kstrndup(name, namelen, GFP_ATOMIC);
899 	if (de->name == NULL) {
900 		kfree(de);
901 		cde->is_failed = 1;
902 		return false;
903 	}
904 	de->pos = ctx->pos;
905 
906 	memcpy(&de->fattr, fattr, sizeof(struct cifs_fattr));
907 
908 	list_add_tail(&de->entry, &cde->entries);
909 	/* update accounting */
910 	cde->entries_count++;
911 	cde->bytes_used += sizeof(*de) + (size_t)namelen + 1;
912 	return true;
913 }
914 
cifs_dir_emit(struct dir_context * ctx,const char * name,int namelen,struct cifs_fattr * fattr,struct cached_fid * cfid,struct file * file)915 static bool cifs_dir_emit(struct dir_context *ctx,
916 			  const char *name, int namelen,
917 			  struct cifs_fattr *fattr,
918 			  struct cached_fid *cfid,
919 			  struct file *file)
920 {
921 	size_t delta_bytes = 0;
922 	bool rc, added = false;
923 	ino_t ino = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
924 
925 	rc = dir_emit(ctx, name, namelen, ino, fattr->cf_dtype);
926 	if (!rc)
927 		return rc;
928 
929 	if (cfid) {
930 		/* Cost of this entry */
931 		delta_bytes = sizeof(struct cached_dirent) + (size_t)namelen + 1;
932 
933 		mutex_lock(&cfid->dirents.de_mutex);
934 		added = add_cached_dirent(&cfid->dirents, ctx, name, namelen,
935 					  fattr, file);
936 		mutex_unlock(&cfid->dirents.de_mutex);
937 
938 		if (added) {
939 			/* per-tcon then global for consistency with free path */
940 			atomic64_add((long long)delta_bytes, &cfid->cfids->total_dirents_bytes);
941 			atomic_long_inc(&cfid->cfids->total_dirents_entries);
942 			atomic64_add((long long)delta_bytes, &cifs_dircache_bytes_used);
943 		}
944 	}
945 
946 	return rc;
947 }
948 
cifs_filldir(char * find_entry,struct file * file,struct dir_context * ctx,char * scratch_buf,unsigned int max_len,struct cached_fid * cfid)949 static int cifs_filldir(char *find_entry, struct file *file,
950 			struct dir_context *ctx,
951 			char *scratch_buf, unsigned int max_len,
952 			struct cached_fid *cfid)
953 {
954 	struct cifsFileInfo *file_info = file->private_data;
955 	struct super_block *sb = file_inode(file)->i_sb;
956 	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
957 	struct cifs_dirent de = { NULL, };
958 	struct cifs_fattr fattr;
959 	struct qstr name;
960 	int rc = 0;
961 
962 	rc = cifs_fill_dirent(&de, find_entry, file_info->srch_inf.info_level,
963 			      file_info->srch_inf.unicode);
964 	if (rc)
965 		return rc;
966 
967 	if (de.namelen > max_len) {
968 		cifs_dbg(VFS, "bad search response length %zd past smb end\n",
969 			 de.namelen);
970 		return -EINVAL;
971 	}
972 
973 	/* skip . and .. since we added them first */
974 	if (cifs_entry_is_dot(&de, file_info->srch_inf.unicode))
975 		return 0;
976 
977 	if (file_info->srch_inf.unicode) {
978 		struct nls_table *nlt = cifs_sb->local_nls;
979 		int map_type;
980 
981 		map_type = cifs_remap(cifs_sb);
982 		name.name = scratch_buf;
983 		name.len =
984 			cifs_from_utf16((char *)name.name, (__le16 *)de.name,
985 					UNICODE_NAME_MAX,
986 					min_t(size_t, de.namelen,
987 					      (size_t)max_len), nlt, map_type);
988 		name.len -= nls_nullsize(nlt);
989 	} else {
990 		name.name = de.name;
991 		name.len = de.namelen;
992 	}
993 
994 	switch (file_info->srch_inf.info_level) {
995 	case SMB_FIND_FILE_POSIX_INFO:
996 		cifs_posix_to_fattr(&fattr,
997 				    (struct smb2_posix_info *)find_entry,
998 				    cifs_sb);
999 		break;
1000 	case SMB_FIND_FILE_UNIX:
1001 		cifs_unix_basic_to_fattr(&fattr,
1002 					 &((FILE_UNIX_INFO *)find_entry)->basic,
1003 					 cifs_sb);
1004 		if (S_ISLNK(fattr.cf_mode))
1005 			fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
1006 		break;
1007 	case SMB_FIND_FILE_INFO_STANDARD:
1008 		cifs_std_info_to_fattr(&fattr,
1009 				       (FIND_FILE_STANDARD_INFO *)find_entry,
1010 				       cifs_sb);
1011 		break;
1012 	case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
1013 	case SMB_FIND_FILE_ID_FULL_DIR_INFO:
1014 		cifs_fulldir_info_to_fattr(&fattr, find_entry, cifs_sb);
1015 		break;
1016 	default:
1017 		cifs_dir_info_to_fattr(&fattr,
1018 				       (FILE_DIRECTORY_INFO *)find_entry,
1019 				       cifs_sb);
1020 		break;
1021 	}
1022 
1023 	if (de.ino && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
1024 		fattr.cf_uniqueid = de.ino;
1025 	} else {
1026 		fattr.cf_uniqueid = iunique(sb, ROOT_I);
1027 		cifs_autodisable_serverino(cifs_sb);
1028 	}
1029 
1030 	if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) &&
1031 	    couldbe_mf_symlink(&fattr))
1032 		/*
1033 		 * trying to get the type and mode can be slow,
1034 		 * so just call those regular files for now, and mark
1035 		 * for reval
1036 		 */
1037 		fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
1038 
1039 	cifs_prime_dcache(file_dentry(file), &name, &fattr);
1040 
1041 	return !cifs_dir_emit(ctx, name.name, name.len,
1042 			      &fattr, cfid, file);
1043 }
1044 
1045 
cifs_readdir(struct file * file,struct dir_context * ctx)1046 int cifs_readdir(struct file *file, struct dir_context *ctx)
1047 {
1048 	int rc = 0;
1049 	unsigned int xid;
1050 	int i;
1051 	struct tcon_link *tlink = NULL;
1052 	struct cifs_tcon *tcon;
1053 	struct cifsFileInfo *cifsFile;
1054 	char *current_entry;
1055 	int num_to_fill = 0;
1056 	char *tmp_buf = NULL;
1057 	char *end_of_smb;
1058 	unsigned int max_len;
1059 	const char *full_path;
1060 	void *page = alloc_dentry_path();
1061 	struct cached_fid *cfid = NULL;
1062 	struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
1063 
1064 	xid = get_xid();
1065 
1066 	full_path = build_path_from_dentry(file_dentry(file), page);
1067 	if (IS_ERR(full_path)) {
1068 		rc = PTR_ERR(full_path);
1069 		goto rddir2_exit;
1070 	}
1071 
1072 	if (file->private_data == NULL) {
1073 		tlink = cifs_sb_tlink(cifs_sb);
1074 		if (IS_ERR(tlink))
1075 			goto cache_not_found;
1076 		tcon = tlink_tcon(tlink);
1077 	} else {
1078 		cifsFile = file->private_data;
1079 		tcon = tlink_tcon(cifsFile->tlink);
1080 	}
1081 
1082 	rc = open_cached_dir(xid, tcon, full_path, cifs_sb, false, &cfid);
1083 	cifs_put_tlink(tlink);
1084 	if (rc)
1085 		goto cache_not_found;
1086 
1087 	mutex_lock(&cfid->dirents.de_mutex);
1088 	/*
1089 	 * If this was reading from the start of the directory
1090 	 * we need to initialize scanning and storing the
1091 	 * directory content.
1092 	 */
1093 	if (ctx->pos == 0 && cfid->dirents.file == NULL) {
1094 		cfid->dirents.file = file;
1095 		cfid->dirents.pos = 2;
1096 	}
1097 	/*
1098 	 * If we already have the entire directory cached then
1099 	 * we can just serve the cache.
1100 	 */
1101 	if (cfid->dirents.is_valid) {
1102 		if (!dir_emit_dots(file, ctx)) {
1103 			mutex_unlock(&cfid->dirents.de_mutex);
1104 			goto rddir2_exit;
1105 		}
1106 		emit_cached_dirents(&cfid->dirents, ctx);
1107 		mutex_unlock(&cfid->dirents.de_mutex);
1108 		goto rddir2_exit;
1109 	}
1110 	mutex_unlock(&cfid->dirents.de_mutex);
1111 
1112 	/* Drop the cache while calling initiate_cifs_search and
1113 	 * find_cifs_entry in case there will be reconnects during
1114 	 * query_directory.
1115 	 */
1116 	close_cached_dir(cfid);
1117 	cfid = NULL;
1118 
1119  cache_not_found:
1120 	/*
1121 	 * Ensure FindFirst doesn't fail before doing filldir() for '.' and
1122 	 * '..'. Otherwise we won't be able to notify VFS in case of failure.
1123 	 */
1124 	if (file->private_data == NULL) {
1125 		rc = initiate_cifs_search(xid, file, full_path);
1126 		cifs_dbg(FYI, "initiate cifs search rc %d\n", rc);
1127 		if (rc)
1128 			goto rddir2_exit;
1129 	}
1130 
1131 	if (!dir_emit_dots(file, ctx))
1132 		goto rddir2_exit;
1133 
1134 	/* 1) If search is active,
1135 		is in current search buffer?
1136 		if it before then restart search
1137 		if after then keep searching till find it */
1138 	cifsFile = file->private_data;
1139 	if (cifsFile->srch_inf.endOfSearch) {
1140 		if (cifsFile->srch_inf.emptyDir) {
1141 			cifs_dbg(FYI, "End of search, empty dir\n");
1142 			rc = 0;
1143 			goto rddir2_exit;
1144 		}
1145 	} /* else {
1146 		cifsFile->invalidHandle = true;
1147 		tcon->ses->server->close(xid, tcon, &cifsFile->fid);
1148 	} */
1149 
1150 	tcon = tlink_tcon(cifsFile->tlink);
1151 	rc = find_cifs_entry(xid, tcon, ctx->pos, file, full_path,
1152 			     &current_entry, &num_to_fill);
1153 	open_cached_dir(xid, tcon, full_path, cifs_sb, false, &cfid);
1154 	if (rc) {
1155 		cifs_dbg(FYI, "fce error %d\n", rc);
1156 		goto rddir2_exit;
1157 	} else if (current_entry != NULL) {
1158 		cifs_dbg(FYI, "entry %lld found\n", ctx->pos);
1159 	} else {
1160 		if (cfid) {
1161 			mutex_lock(&cfid->dirents.de_mutex);
1162 			finished_cached_dirents_count(&cfid->dirents, ctx, file);
1163 			mutex_unlock(&cfid->dirents.de_mutex);
1164 		}
1165 		cifs_dbg(FYI, "Could not find entry\n");
1166 		goto rddir2_exit;
1167 	}
1168 	cifs_dbg(FYI, "loop through %d times filling dir for net buf %p\n",
1169 		 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start);
1170 	max_len = tcon->ses->server->ops->calc_smb_size(
1171 			cifsFile->srch_inf.ntwrk_buf_start);
1172 	end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1173 
1174 	tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
1175 	if (tmp_buf == NULL) {
1176 		rc = -ENOMEM;
1177 		goto rddir2_exit;
1178 	}
1179 
1180 	for (i = 0; i < num_to_fill; i++) {
1181 		if (current_entry == NULL) {
1182 			/* evaluate whether this case is an error */
1183 			cifs_dbg(VFS, "past SMB end,  num to fill %d i %d\n",
1184 				 num_to_fill, i);
1185 			break;
1186 		}
1187 		/*
1188 		 * if buggy server returns . and .. late do we want to
1189 		 * check for that here?
1190 		 */
1191 		*tmp_buf = 0;
1192 		rc = cifs_filldir(current_entry, file, ctx,
1193 				  tmp_buf, max_len, cfid);
1194 		if (rc) {
1195 			if (rc > 0)
1196 				rc = 0;
1197 			break;
1198 		}
1199 
1200 		ctx->pos++;
1201 		if (cfid) {
1202 			mutex_lock(&cfid->dirents.de_mutex);
1203 			update_cached_dirents_count(&cfid->dirents, file);
1204 			mutex_unlock(&cfid->dirents.de_mutex);
1205 		}
1206 
1207 		if (ctx->pos ==
1208 			cifsFile->srch_inf.index_of_last_entry) {
1209 			cifs_dbg(FYI, "last entry in buf at pos %lld %s\n",
1210 				 ctx->pos, tmp_buf);
1211 			cifs_save_resume_key(current_entry, cifsFile);
1212 			break;
1213 		}
1214 		current_entry =
1215 			nxt_dir_entry(current_entry, end_of_smb,
1216 				      cifsFile->srch_inf.info_level);
1217 	}
1218 	kfree(tmp_buf);
1219 
1220 rddir2_exit:
1221 	if (cfid)
1222 		close_cached_dir(cfid);
1223 	free_dentry_path(page);
1224 	free_xid(xid);
1225 	return rc;
1226 }
1227