xref: /OK3568_Linux_fs/kernel/fs/cifs/readdir.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *   fs/cifs/readdir.c
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  *   Directory search handling
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  *   Copyright (C) International Business Machines  Corp., 2004, 2008
7*4882a593Smuzhiyun  *   Copyright (C) Red Hat, Inc., 2011
8*4882a593Smuzhiyun  *   Author(s): Steve French (sfrench@us.ibm.com)
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  *   This library is free software; you can redistribute it and/or modify
11*4882a593Smuzhiyun  *   it under the terms of the GNU Lesser General Public License as published
12*4882a593Smuzhiyun  *   by the Free Software Foundation; either version 2.1 of the License, or
13*4882a593Smuzhiyun  *   (at your option) any later version.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  *   This library is distributed in the hope that it will be useful,
16*4882a593Smuzhiyun  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17*4882a593Smuzhiyun  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
18*4882a593Smuzhiyun  *   the GNU Lesser General Public License for more details.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  *   You should have received a copy of the GNU Lesser General Public License
21*4882a593Smuzhiyun  *   along with this library; if not, write to the Free Software
22*4882a593Smuzhiyun  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23*4882a593Smuzhiyun  */
24*4882a593Smuzhiyun #include <linux/fs.h>
25*4882a593Smuzhiyun #include <linux/pagemap.h>
26*4882a593Smuzhiyun #include <linux/slab.h>
27*4882a593Smuzhiyun #include <linux/stat.h>
28*4882a593Smuzhiyun #include "cifspdu.h"
29*4882a593Smuzhiyun #include "cifsglob.h"
30*4882a593Smuzhiyun #include "cifsproto.h"
31*4882a593Smuzhiyun #include "cifs_unicode.h"
32*4882a593Smuzhiyun #include "cifs_debug.h"
33*4882a593Smuzhiyun #include "cifs_fs_sb.h"
34*4882a593Smuzhiyun #include "cifsfs.h"
35*4882a593Smuzhiyun #include "smb2proto.h"
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun /*
38*4882a593Smuzhiyun  * To be safe - for UCS to UTF-8 with strings loaded with the rare long
39*4882a593Smuzhiyun  * characters alloc more to account for such multibyte target UTF-8
40*4882a593Smuzhiyun  * characters.
41*4882a593Smuzhiyun  */
42*4882a593Smuzhiyun #define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #ifdef CONFIG_CIFS_DEBUG2
dump_cifs_file_struct(struct file * file,char * label)45*4882a593Smuzhiyun static void dump_cifs_file_struct(struct file *file, char *label)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	struct cifsFileInfo *cf;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	if (file) {
50*4882a593Smuzhiyun 		cf = file->private_data;
51*4882a593Smuzhiyun 		if (cf == NULL) {
52*4882a593Smuzhiyun 			cifs_dbg(FYI, "empty cifs private file data\n");
53*4882a593Smuzhiyun 			return;
54*4882a593Smuzhiyun 		}
55*4882a593Smuzhiyun 		if (cf->invalidHandle)
56*4882a593Smuzhiyun 			cifs_dbg(FYI, "Invalid handle\n");
57*4882a593Smuzhiyun 		if (cf->srch_inf.endOfSearch)
58*4882a593Smuzhiyun 			cifs_dbg(FYI, "end of search\n");
59*4882a593Smuzhiyun 		if (cf->srch_inf.emptyDir)
60*4882a593Smuzhiyun 			cifs_dbg(FYI, "empty dir\n");
61*4882a593Smuzhiyun 	}
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun #else
dump_cifs_file_struct(struct file * file,char * label)64*4882a593Smuzhiyun static inline void dump_cifs_file_struct(struct file *file, char *label)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun #endif /* DEBUG2 */
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun  * Attempt to preload the dcache with the results from the FIND_FIRST/NEXT
71*4882a593Smuzhiyun  *
72*4882a593Smuzhiyun  * Find the dentry that matches "name". If there isn't one, create one. If it's
73*4882a593Smuzhiyun  * a negative dentry or the uniqueid or filetype(mode) changed,
74*4882a593Smuzhiyun  * then drop it and recreate it.
75*4882a593Smuzhiyun  */
76*4882a593Smuzhiyun static void
cifs_prime_dcache(struct dentry * parent,struct qstr * name,struct cifs_fattr * fattr)77*4882a593Smuzhiyun cifs_prime_dcache(struct dentry *parent, struct qstr *name,
78*4882a593Smuzhiyun 		    struct cifs_fattr *fattr)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	struct dentry *dentry, *alias;
81*4882a593Smuzhiyun 	struct inode *inode;
82*4882a593Smuzhiyun 	struct super_block *sb = parent->d_sb;
83*4882a593Smuzhiyun 	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
84*4882a593Smuzhiyun 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	cifs_dbg(FYI, "%s: for %s\n", __func__, name->name);
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	dentry = d_hash_and_lookup(parent, name);
89*4882a593Smuzhiyun 	if (!dentry) {
90*4882a593Smuzhiyun 		/*
91*4882a593Smuzhiyun 		 * If we know that the inode will need to be revalidated
92*4882a593Smuzhiyun 		 * immediately, then don't create a new dentry for it.
93*4882a593Smuzhiyun 		 * We'll end up doing an on the wire call either way and
94*4882a593Smuzhiyun 		 * this spares us an invalidation.
95*4882a593Smuzhiyun 		 */
96*4882a593Smuzhiyun 		if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
97*4882a593Smuzhiyun 			return;
98*4882a593Smuzhiyun retry:
99*4882a593Smuzhiyun 		dentry = d_alloc_parallel(parent, name, &wq);
100*4882a593Smuzhiyun 	}
101*4882a593Smuzhiyun 	if (IS_ERR(dentry))
102*4882a593Smuzhiyun 		return;
103*4882a593Smuzhiyun 	if (!d_in_lookup(dentry)) {
104*4882a593Smuzhiyun 		inode = d_inode(dentry);
105*4882a593Smuzhiyun 		if (inode) {
106*4882a593Smuzhiyun 			if (d_mountpoint(dentry)) {
107*4882a593Smuzhiyun 				dput(dentry);
108*4882a593Smuzhiyun 				return;
109*4882a593Smuzhiyun 			}
110*4882a593Smuzhiyun 			/*
111*4882a593Smuzhiyun 			 * If we're generating inode numbers, then we don't
112*4882a593Smuzhiyun 			 * want to clobber the existing one with the one that
113*4882a593Smuzhiyun 			 * the readdir code created.
114*4882a593Smuzhiyun 			 */
115*4882a593Smuzhiyun 			if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM))
116*4882a593Smuzhiyun 				fattr->cf_uniqueid = CIFS_I(inode)->uniqueid;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 			/* update inode in place
119*4882a593Smuzhiyun 			 * if both i_ino and i_mode didn't change */
120*4882a593Smuzhiyun 			if (CIFS_I(inode)->uniqueid == fattr->cf_uniqueid &&
121*4882a593Smuzhiyun 			    (inode->i_mode & S_IFMT) ==
122*4882a593Smuzhiyun 			    (fattr->cf_mode & S_IFMT)) {
123*4882a593Smuzhiyun 				cifs_fattr_to_inode(inode, fattr);
124*4882a593Smuzhiyun 				dput(dentry);
125*4882a593Smuzhiyun 				return;
126*4882a593Smuzhiyun 			}
127*4882a593Smuzhiyun 		}
128*4882a593Smuzhiyun 		d_invalidate(dentry);
129*4882a593Smuzhiyun 		dput(dentry);
130*4882a593Smuzhiyun 		goto retry;
131*4882a593Smuzhiyun 	} else {
132*4882a593Smuzhiyun 		inode = cifs_iget(sb, fattr);
133*4882a593Smuzhiyun 		if (!inode)
134*4882a593Smuzhiyun 			inode = ERR_PTR(-ENOMEM);
135*4882a593Smuzhiyun 		alias = d_splice_alias(inode, dentry);
136*4882a593Smuzhiyun 		d_lookup_done(dentry);
137*4882a593Smuzhiyun 		if (alias && !IS_ERR(alias))
138*4882a593Smuzhiyun 			dput(alias);
139*4882a593Smuzhiyun 	}
140*4882a593Smuzhiyun 	dput(dentry);
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun 
reparse_file_needs_reval(const struct cifs_fattr * fattr)143*4882a593Smuzhiyun static bool reparse_file_needs_reval(const struct cifs_fattr *fattr)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	if (!(fattr->cf_cifsattrs & ATTR_REPARSE))
146*4882a593Smuzhiyun 		return false;
147*4882a593Smuzhiyun 	/*
148*4882a593Smuzhiyun 	 * The DFS tags should be only intepreted by server side as per
149*4882a593Smuzhiyun 	 * MS-FSCC 2.1.2.1, but let's include them anyway.
150*4882a593Smuzhiyun 	 *
151*4882a593Smuzhiyun 	 * Besides, if cf_cifstag is unset (0), then we still need it to be
152*4882a593Smuzhiyun 	 * revalidated to know exactly what reparse point it is.
153*4882a593Smuzhiyun 	 */
154*4882a593Smuzhiyun 	switch (fattr->cf_cifstag) {
155*4882a593Smuzhiyun 	case IO_REPARSE_TAG_DFS:
156*4882a593Smuzhiyun 	case IO_REPARSE_TAG_DFSR:
157*4882a593Smuzhiyun 	case IO_REPARSE_TAG_SYMLINK:
158*4882a593Smuzhiyun 	case IO_REPARSE_TAG_NFS:
159*4882a593Smuzhiyun 	case 0:
160*4882a593Smuzhiyun 		return true;
161*4882a593Smuzhiyun 	}
162*4882a593Smuzhiyun 	return false;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun static void
cifs_fill_common_info(struct cifs_fattr * fattr,struct cifs_sb_info * cifs_sb)166*4882a593Smuzhiyun cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	fattr->cf_uid = cifs_sb->mnt_uid;
169*4882a593Smuzhiyun 	fattr->cf_gid = cifs_sb->mnt_gid;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	/*
172*4882a593Smuzhiyun 	 * The IO_REPARSE_TAG_LX_ tags originally were used by WSL but they
173*4882a593Smuzhiyun 	 * are preferred by the Linux client in some cases since, unlike
174*4882a593Smuzhiyun 	 * the NFS reparse tag (or EAs), they don't require an extra query
175*4882a593Smuzhiyun 	 * to determine which type of special file they represent.
176*4882a593Smuzhiyun 	 * TODO: go through all documented  reparse tags to see if we can
177*4882a593Smuzhiyun 	 * reasonably map some of them to directories vs. files vs. symlinks
178*4882a593Smuzhiyun 	 */
179*4882a593Smuzhiyun 	if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
180*4882a593Smuzhiyun 		fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
181*4882a593Smuzhiyun 		fattr->cf_dtype = DT_DIR;
182*4882a593Smuzhiyun 	} else if (fattr->cf_cifstag == IO_REPARSE_TAG_LX_SYMLINK) {
183*4882a593Smuzhiyun 		fattr->cf_mode |= S_IFLNK | cifs_sb->mnt_file_mode;
184*4882a593Smuzhiyun 		fattr->cf_dtype = DT_LNK;
185*4882a593Smuzhiyun 	} else if (fattr->cf_cifstag == IO_REPARSE_TAG_LX_FIFO) {
186*4882a593Smuzhiyun 		fattr->cf_mode |= S_IFIFO | cifs_sb->mnt_file_mode;
187*4882a593Smuzhiyun 		fattr->cf_dtype = DT_FIFO;
188*4882a593Smuzhiyun 	} else if (fattr->cf_cifstag == IO_REPARSE_TAG_AF_UNIX) {
189*4882a593Smuzhiyun 		fattr->cf_mode |= S_IFSOCK | cifs_sb->mnt_file_mode;
190*4882a593Smuzhiyun 		fattr->cf_dtype = DT_SOCK;
191*4882a593Smuzhiyun 	} else if (fattr->cf_cifstag == IO_REPARSE_TAG_LX_CHR) {
192*4882a593Smuzhiyun 		fattr->cf_mode |= S_IFCHR | cifs_sb->mnt_file_mode;
193*4882a593Smuzhiyun 		fattr->cf_dtype = DT_CHR;
194*4882a593Smuzhiyun 	} else if (fattr->cf_cifstag == IO_REPARSE_TAG_LX_BLK) {
195*4882a593Smuzhiyun 		fattr->cf_mode |= S_IFBLK | cifs_sb->mnt_file_mode;
196*4882a593Smuzhiyun 		fattr->cf_dtype = DT_BLK;
197*4882a593Smuzhiyun 	} else { /* TODO: should we mark some other reparse points (like DFSR) as directories? */
198*4882a593Smuzhiyun 		fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
199*4882a593Smuzhiyun 		fattr->cf_dtype = DT_REG;
200*4882a593Smuzhiyun 	}
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	/*
203*4882a593Smuzhiyun 	 * We need to revalidate it further to make a decision about whether it
204*4882a593Smuzhiyun 	 * is a symbolic link, DFS referral or a reparse point with a direct
205*4882a593Smuzhiyun 	 * access like junctions, deduplicated files, NFS symlinks.
206*4882a593Smuzhiyun 	 */
207*4882a593Smuzhiyun 	if (reparse_file_needs_reval(fattr))
208*4882a593Smuzhiyun 		fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	/* non-unix readdir doesn't provide nlink */
211*4882a593Smuzhiyun 	fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	if (fattr->cf_cifsattrs & ATTR_READONLY)
214*4882a593Smuzhiyun 		fattr->cf_mode &= ~S_IWUGO;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	/*
217*4882a593Smuzhiyun 	 * We of course don't get ACL info in FIND_FIRST/NEXT results, so
218*4882a593Smuzhiyun 	 * mark it for revalidation so that "ls -l" will look right. It might
219*4882a593Smuzhiyun 	 * be super-slow, but if we don't do this then the ownership of files
220*4882a593Smuzhiyun 	 * may look wrong since the inodes may not have timed out by the time
221*4882a593Smuzhiyun 	 * "ls" does a stat() call on them.
222*4882a593Smuzhiyun 	 */
223*4882a593Smuzhiyun 	if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
224*4882a593Smuzhiyun 	    (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID))
225*4882a593Smuzhiyun 		fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
228*4882a593Smuzhiyun 	    fattr->cf_cifsattrs & ATTR_SYSTEM) {
229*4882a593Smuzhiyun 		if (fattr->cf_eof == 0)  {
230*4882a593Smuzhiyun 			fattr->cf_mode &= ~S_IFMT;
231*4882a593Smuzhiyun 			fattr->cf_mode |= S_IFIFO;
232*4882a593Smuzhiyun 			fattr->cf_dtype = DT_FIFO;
233*4882a593Smuzhiyun 		} else {
234*4882a593Smuzhiyun 			/*
235*4882a593Smuzhiyun 			 * trying to get the type and mode via SFU can be slow,
236*4882a593Smuzhiyun 			 * so just call those regular files for now, and mark
237*4882a593Smuzhiyun 			 * for reval
238*4882a593Smuzhiyun 			 */
239*4882a593Smuzhiyun 			fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
240*4882a593Smuzhiyun 		}
241*4882a593Smuzhiyun 	}
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun /* Fill a cifs_fattr struct with info from SMB_FIND_FILE_POSIX_INFO. */
245*4882a593Smuzhiyun static void
cifs_posix_to_fattr(struct cifs_fattr * fattr,struct smb2_posix_info * info,struct cifs_sb_info * cifs_sb)246*4882a593Smuzhiyun cifs_posix_to_fattr(struct cifs_fattr *fattr, struct smb2_posix_info *info,
247*4882a593Smuzhiyun 		    struct cifs_sb_info *cifs_sb)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun 	struct smb2_posix_info_parsed parsed;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	posix_info_parse(info, NULL, &parsed);
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	memset(fattr, 0, sizeof(*fattr));
254*4882a593Smuzhiyun 	fattr->cf_uniqueid = le64_to_cpu(info->Inode);
255*4882a593Smuzhiyun 	fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
256*4882a593Smuzhiyun 	fattr->cf_eof = le64_to_cpu(info->EndOfFile);
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
259*4882a593Smuzhiyun 	fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
260*4882a593Smuzhiyun 	fattr->cf_ctime = cifs_NTtimeToUnix(info->CreationTime);
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	fattr->cf_nlink = le32_to_cpu(info->HardLinks);
263*4882a593Smuzhiyun 	fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	/*
266*4882a593Smuzhiyun 	 * Since we set the inode type below we need to mask off
267*4882a593Smuzhiyun 	 * to avoid strange results if bits set above.
268*4882a593Smuzhiyun 	 * XXX: why not make server&client use the type bits?
269*4882a593Smuzhiyun 	 */
270*4882a593Smuzhiyun 	fattr->cf_mode = le32_to_cpu(info->Mode) & ~S_IFMT;
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	cifs_dbg(FYI, "posix fattr: dev %d, reparse %d, mode %o\n",
273*4882a593Smuzhiyun 		 le32_to_cpu(info->DeviceId),
274*4882a593Smuzhiyun 		 le32_to_cpu(info->ReparseTag),
275*4882a593Smuzhiyun 		 le32_to_cpu(info->Mode));
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
278*4882a593Smuzhiyun 		fattr->cf_mode |= S_IFDIR;
279*4882a593Smuzhiyun 		fattr->cf_dtype = DT_DIR;
280*4882a593Smuzhiyun 	} else {
281*4882a593Smuzhiyun 		/*
282*4882a593Smuzhiyun 		 * mark anything that is not a dir as regular
283*4882a593Smuzhiyun 		 * file. special files should have the REPARSE
284*4882a593Smuzhiyun 		 * attribute and will be marked as needing revaluation
285*4882a593Smuzhiyun 		 */
286*4882a593Smuzhiyun 		fattr->cf_mode |= S_IFREG;
287*4882a593Smuzhiyun 		fattr->cf_dtype = DT_REG;
288*4882a593Smuzhiyun 	}
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	if (reparse_file_needs_reval(fattr))
291*4882a593Smuzhiyun 		fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	sid_to_id(cifs_sb, &parsed.owner, fattr, SIDOWNER);
294*4882a593Smuzhiyun 	sid_to_id(cifs_sb, &parsed.group, fattr, SIDGROUP);
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun 
__dir_info_to_fattr(struct cifs_fattr * fattr,const void * info)297*4882a593Smuzhiyun static void __dir_info_to_fattr(struct cifs_fattr *fattr, const void *info)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun 	const FILE_DIRECTORY_INFO *fi = info;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	memset(fattr, 0, sizeof(*fattr));
302*4882a593Smuzhiyun 	fattr->cf_cifsattrs = le32_to_cpu(fi->ExtFileAttributes);
303*4882a593Smuzhiyun 	fattr->cf_eof = le64_to_cpu(fi->EndOfFile);
304*4882a593Smuzhiyun 	fattr->cf_bytes = le64_to_cpu(fi->AllocationSize);
305*4882a593Smuzhiyun 	fattr->cf_createtime = le64_to_cpu(fi->CreationTime);
306*4882a593Smuzhiyun 	fattr->cf_atime = cifs_NTtimeToUnix(fi->LastAccessTime);
307*4882a593Smuzhiyun 	fattr->cf_ctime = cifs_NTtimeToUnix(fi->ChangeTime);
308*4882a593Smuzhiyun 	fattr->cf_mtime = cifs_NTtimeToUnix(fi->LastWriteTime);
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun void
cifs_dir_info_to_fattr(struct cifs_fattr * fattr,FILE_DIRECTORY_INFO * info,struct cifs_sb_info * cifs_sb)312*4882a593Smuzhiyun cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info,
313*4882a593Smuzhiyun 		       struct cifs_sb_info *cifs_sb)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun 	__dir_info_to_fattr(fattr, info);
316*4882a593Smuzhiyun 	cifs_fill_common_info(fattr, cifs_sb);
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun 
cifs_fulldir_info_to_fattr(struct cifs_fattr * fattr,SEARCH_ID_FULL_DIR_INFO * info,struct cifs_sb_info * cifs_sb)319*4882a593Smuzhiyun static void cifs_fulldir_info_to_fattr(struct cifs_fattr *fattr,
320*4882a593Smuzhiyun 				       SEARCH_ID_FULL_DIR_INFO *info,
321*4882a593Smuzhiyun 				       struct cifs_sb_info *cifs_sb)
322*4882a593Smuzhiyun {
323*4882a593Smuzhiyun 	__dir_info_to_fattr(fattr, info);
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	/* See MS-FSCC 2.4.18 FileIdFullDirectoryInformation */
326*4882a593Smuzhiyun 	if (fattr->cf_cifsattrs & ATTR_REPARSE)
327*4882a593Smuzhiyun 		fattr->cf_cifstag = le32_to_cpu(info->EaSize);
328*4882a593Smuzhiyun 	cifs_fill_common_info(fattr, cifs_sb);
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun static void
cifs_std_info_to_fattr(struct cifs_fattr * fattr,FIND_FILE_STANDARD_INFO * info,struct cifs_sb_info * cifs_sb)332*4882a593Smuzhiyun cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info,
333*4882a593Smuzhiyun 		       struct cifs_sb_info *cifs_sb)
334*4882a593Smuzhiyun {
335*4882a593Smuzhiyun 	int offset = cifs_sb_master_tcon(cifs_sb)->ses->server->timeAdj;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	memset(fattr, 0, sizeof(*fattr));
338*4882a593Smuzhiyun 	fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate,
339*4882a593Smuzhiyun 					    info->LastAccessTime, offset);
340*4882a593Smuzhiyun 	fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate,
341*4882a593Smuzhiyun 					    info->LastWriteTime, offset);
342*4882a593Smuzhiyun 	fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate,
343*4882a593Smuzhiyun 					    info->LastWriteTime, offset);
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	fattr->cf_cifsattrs = le16_to_cpu(info->Attributes);
346*4882a593Smuzhiyun 	fattr->cf_bytes = le32_to_cpu(info->AllocationSize);
347*4882a593Smuzhiyun 	fattr->cf_eof = le32_to_cpu(info->DataSize);
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	cifs_fill_common_info(fattr, cifs_sb);
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun /* BB eventually need to add the following helper function to
353*4882a593Smuzhiyun       resolve NT_STATUS_STOPPED_ON_SYMLINK return code when
354*4882a593Smuzhiyun       we try to do FindFirst on (NTFS) directory symlinks */
355*4882a593Smuzhiyun /*
356*4882a593Smuzhiyun int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
357*4882a593Smuzhiyun 			     unsigned int xid)
358*4882a593Smuzhiyun {
359*4882a593Smuzhiyun 	__u16 fid;
360*4882a593Smuzhiyun 	int len;
361*4882a593Smuzhiyun 	int oplock = 0;
362*4882a593Smuzhiyun 	int rc;
363*4882a593Smuzhiyun 	struct cifs_tcon *ptcon = cifs_sb_tcon(cifs_sb);
364*4882a593Smuzhiyun 	char *tmpbuffer;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ,
367*4882a593Smuzhiyun 			OPEN_REPARSE_POINT, &fid, &oplock, NULL,
368*4882a593Smuzhiyun 			cifs_sb->local_nls,
369*4882a593Smuzhiyun 			cifs_remap(cifs_sb);
370*4882a593Smuzhiyun 	if (!rc) {
371*4882a593Smuzhiyun 		tmpbuffer = kmalloc(maxpath);
372*4882a593Smuzhiyun 		rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path,
373*4882a593Smuzhiyun 				tmpbuffer,
374*4882a593Smuzhiyun 				maxpath -1,
375*4882a593Smuzhiyun 				fid,
376*4882a593Smuzhiyun 				cifs_sb->local_nls);
377*4882a593Smuzhiyun 		if (CIFSSMBClose(xid, ptcon, fid)) {
378*4882a593Smuzhiyun 			cifs_dbg(FYI, "Error closing temporary reparsepoint open\n");
379*4882a593Smuzhiyun 		}
380*4882a593Smuzhiyun 	}
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun  */
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun static int
initiate_cifs_search(const unsigned int xid,struct file * file,char * full_path)385*4882a593Smuzhiyun initiate_cifs_search(const unsigned int xid, struct file *file,
386*4882a593Smuzhiyun 		     char *full_path)
387*4882a593Smuzhiyun {
388*4882a593Smuzhiyun 	__u16 search_flags;
389*4882a593Smuzhiyun 	int rc = 0;
390*4882a593Smuzhiyun 	struct cifsFileInfo *cifsFile;
391*4882a593Smuzhiyun 	struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
392*4882a593Smuzhiyun 	struct tcon_link *tlink = NULL;
393*4882a593Smuzhiyun 	struct cifs_tcon *tcon;
394*4882a593Smuzhiyun 	struct TCP_Server_Info *server;
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	if (file->private_data == NULL) {
397*4882a593Smuzhiyun 		tlink = cifs_sb_tlink(cifs_sb);
398*4882a593Smuzhiyun 		if (IS_ERR(tlink))
399*4882a593Smuzhiyun 			return PTR_ERR(tlink);
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 		cifsFile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
402*4882a593Smuzhiyun 		if (cifsFile == NULL) {
403*4882a593Smuzhiyun 			rc = -ENOMEM;
404*4882a593Smuzhiyun 			goto error_exit;
405*4882a593Smuzhiyun 		}
406*4882a593Smuzhiyun 		spin_lock_init(&cifsFile->file_info_lock);
407*4882a593Smuzhiyun 		file->private_data = cifsFile;
408*4882a593Smuzhiyun 		cifsFile->tlink = cifs_get_tlink(tlink);
409*4882a593Smuzhiyun 		tcon = tlink_tcon(tlink);
410*4882a593Smuzhiyun 	} else {
411*4882a593Smuzhiyun 		cifsFile = file->private_data;
412*4882a593Smuzhiyun 		tcon = tlink_tcon(cifsFile->tlink);
413*4882a593Smuzhiyun 	}
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	server = tcon->ses->server;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	if (!server->ops->query_dir_first) {
418*4882a593Smuzhiyun 		rc = -ENOSYS;
419*4882a593Smuzhiyun 		goto error_exit;
420*4882a593Smuzhiyun 	}
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	cifsFile->invalidHandle = true;
423*4882a593Smuzhiyun 	cifsFile->srch_inf.endOfSearch = false;
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	cifs_dbg(FYI, "Full path: %s start at: %lld\n", full_path, file->f_pos);
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun ffirst_retry:
428*4882a593Smuzhiyun 	/* test for Unix extensions */
429*4882a593Smuzhiyun 	/* but now check for them on the share/mount not on the SMB session */
430*4882a593Smuzhiyun 	/* if (cap_unix(tcon->ses) { */
431*4882a593Smuzhiyun 	if (tcon->unix_ext)
432*4882a593Smuzhiyun 		cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
433*4882a593Smuzhiyun 	else if (tcon->posix_extensions)
434*4882a593Smuzhiyun 		cifsFile->srch_inf.info_level = SMB_FIND_FILE_POSIX_INFO;
435*4882a593Smuzhiyun 	else if ((tcon->ses->capabilities &
436*4882a593Smuzhiyun 		  tcon->ses->server->vals->cap_nt_find) == 0) {
437*4882a593Smuzhiyun 		cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
438*4882a593Smuzhiyun 	} else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
439*4882a593Smuzhiyun 		cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
440*4882a593Smuzhiyun 	} else /* not srvinos - BB fixme add check for backlevel? */ {
441*4882a593Smuzhiyun 		cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
442*4882a593Smuzhiyun 	}
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	search_flags = CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME;
445*4882a593Smuzhiyun 	if (backup_cred(cifs_sb))
446*4882a593Smuzhiyun 		search_flags |= CIFS_SEARCH_BACKUP_SEARCH;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	rc = server->ops->query_dir_first(xid, tcon, full_path, cifs_sb,
449*4882a593Smuzhiyun 					  &cifsFile->fid, search_flags,
450*4882a593Smuzhiyun 					  &cifsFile->srch_inf);
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	if (rc == 0)
453*4882a593Smuzhiyun 		cifsFile->invalidHandle = false;
454*4882a593Smuzhiyun 	/* BB add following call to handle readdir on new NTFS symlink errors
455*4882a593Smuzhiyun 	else if STATUS_STOPPED_ON_SYMLINK
456*4882a593Smuzhiyun 		call get_symlink_reparse_path and retry with new path */
457*4882a593Smuzhiyun 	else if ((rc == -EOPNOTSUPP) &&
458*4882a593Smuzhiyun 		(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
459*4882a593Smuzhiyun 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
460*4882a593Smuzhiyun 		goto ffirst_retry;
461*4882a593Smuzhiyun 	}
462*4882a593Smuzhiyun error_exit:
463*4882a593Smuzhiyun 	cifs_put_tlink(tlink);
464*4882a593Smuzhiyun 	return rc;
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun /* return length of unicode string in bytes */
cifs_unicode_bytelen(const char * str)468*4882a593Smuzhiyun static int cifs_unicode_bytelen(const char *str)
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun 	int len;
471*4882a593Smuzhiyun 	const __le16 *ustr = (const __le16 *)str;
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	for (len = 0; len <= PATH_MAX; len++) {
474*4882a593Smuzhiyun 		if (ustr[len] == 0)
475*4882a593Smuzhiyun 			return len << 1;
476*4882a593Smuzhiyun 	}
477*4882a593Smuzhiyun 	cifs_dbg(FYI, "Unicode string longer than PATH_MAX found\n");
478*4882a593Smuzhiyun 	return len << 1;
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun 
nxt_dir_entry(char * old_entry,char * end_of_smb,int level)481*4882a593Smuzhiyun static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun 	char *new_entry;
484*4882a593Smuzhiyun 	FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	if (level == SMB_FIND_FILE_INFO_STANDARD) {
487*4882a593Smuzhiyun 		FIND_FILE_STANDARD_INFO *pfData;
488*4882a593Smuzhiyun 		pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 		new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
491*4882a593Smuzhiyun 				pfData->FileNameLength;
492*4882a593Smuzhiyun 	} else {
493*4882a593Smuzhiyun 		u32 next_offset = le32_to_cpu(pDirInfo->NextEntryOffset);
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 		if (old_entry + next_offset < old_entry) {
496*4882a593Smuzhiyun 			cifs_dbg(VFS, "Invalid offset %u\n", next_offset);
497*4882a593Smuzhiyun 			return NULL;
498*4882a593Smuzhiyun 		}
499*4882a593Smuzhiyun 		new_entry = old_entry + next_offset;
500*4882a593Smuzhiyun 	}
501*4882a593Smuzhiyun 	cifs_dbg(FYI, "new entry %p old entry %p\n", new_entry, old_entry);
502*4882a593Smuzhiyun 	/* validate that new_entry is not past end of SMB */
503*4882a593Smuzhiyun 	if (new_entry >= end_of_smb) {
504*4882a593Smuzhiyun 		cifs_dbg(VFS, "search entry %p began after end of SMB %p old entry %p\n",
505*4882a593Smuzhiyun 			 new_entry, end_of_smb, old_entry);
506*4882a593Smuzhiyun 		return NULL;
507*4882a593Smuzhiyun 	} else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
508*4882a593Smuzhiyun 		    (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
509*4882a593Smuzhiyun 		  || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
510*4882a593Smuzhiyun 		   (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb)))  {
511*4882a593Smuzhiyun 		cifs_dbg(VFS, "search entry %p extends after end of SMB %p\n",
512*4882a593Smuzhiyun 			 new_entry, end_of_smb);
513*4882a593Smuzhiyun 		return NULL;
514*4882a593Smuzhiyun 	} else
515*4882a593Smuzhiyun 		return new_entry;
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun struct cifs_dirent {
520*4882a593Smuzhiyun 	const char	*name;
521*4882a593Smuzhiyun 	size_t		namelen;
522*4882a593Smuzhiyun 	u32		resume_key;
523*4882a593Smuzhiyun 	u64		ino;
524*4882a593Smuzhiyun };
525*4882a593Smuzhiyun 
cifs_fill_dirent_posix(struct cifs_dirent * de,const struct smb2_posix_info * info)526*4882a593Smuzhiyun static void cifs_fill_dirent_posix(struct cifs_dirent *de,
527*4882a593Smuzhiyun 				   const struct smb2_posix_info *info)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun 	struct smb2_posix_info_parsed parsed;
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun 	/* payload should have already been checked at this point */
532*4882a593Smuzhiyun 	if (posix_info_parse(info, NULL, &parsed) < 0) {
533*4882a593Smuzhiyun 		cifs_dbg(VFS, "Invalid POSIX info payload\n");
534*4882a593Smuzhiyun 		return;
535*4882a593Smuzhiyun 	}
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	de->name = parsed.name;
538*4882a593Smuzhiyun 	de->namelen = parsed.name_len;
539*4882a593Smuzhiyun 	de->resume_key = info->Ignored;
540*4882a593Smuzhiyun 	de->ino = le64_to_cpu(info->Inode);
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun 
cifs_fill_dirent_unix(struct cifs_dirent * de,const FILE_UNIX_INFO * info,bool is_unicode)543*4882a593Smuzhiyun static void cifs_fill_dirent_unix(struct cifs_dirent *de,
544*4882a593Smuzhiyun 		const FILE_UNIX_INFO *info, bool is_unicode)
545*4882a593Smuzhiyun {
546*4882a593Smuzhiyun 	de->name = &info->FileName[0];
547*4882a593Smuzhiyun 	if (is_unicode)
548*4882a593Smuzhiyun 		de->namelen = cifs_unicode_bytelen(de->name);
549*4882a593Smuzhiyun 	else
550*4882a593Smuzhiyun 		de->namelen = strnlen(de->name, PATH_MAX);
551*4882a593Smuzhiyun 	de->resume_key = info->ResumeKey;
552*4882a593Smuzhiyun 	de->ino = le64_to_cpu(info->basic.UniqueId);
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun 
cifs_fill_dirent_dir(struct cifs_dirent * de,const FILE_DIRECTORY_INFO * info)555*4882a593Smuzhiyun static void cifs_fill_dirent_dir(struct cifs_dirent *de,
556*4882a593Smuzhiyun 		const FILE_DIRECTORY_INFO *info)
557*4882a593Smuzhiyun {
558*4882a593Smuzhiyun 	de->name = &info->FileName[0];
559*4882a593Smuzhiyun 	de->namelen = le32_to_cpu(info->FileNameLength);
560*4882a593Smuzhiyun 	de->resume_key = info->FileIndex;
561*4882a593Smuzhiyun }
562*4882a593Smuzhiyun 
cifs_fill_dirent_full(struct cifs_dirent * de,const FILE_FULL_DIRECTORY_INFO * info)563*4882a593Smuzhiyun static void cifs_fill_dirent_full(struct cifs_dirent *de,
564*4882a593Smuzhiyun 		const FILE_FULL_DIRECTORY_INFO *info)
565*4882a593Smuzhiyun {
566*4882a593Smuzhiyun 	de->name = &info->FileName[0];
567*4882a593Smuzhiyun 	de->namelen = le32_to_cpu(info->FileNameLength);
568*4882a593Smuzhiyun 	de->resume_key = info->FileIndex;
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun 
cifs_fill_dirent_search(struct cifs_dirent * de,const SEARCH_ID_FULL_DIR_INFO * info)571*4882a593Smuzhiyun static void cifs_fill_dirent_search(struct cifs_dirent *de,
572*4882a593Smuzhiyun 		const SEARCH_ID_FULL_DIR_INFO *info)
573*4882a593Smuzhiyun {
574*4882a593Smuzhiyun 	de->name = &info->FileName[0];
575*4882a593Smuzhiyun 	de->namelen = le32_to_cpu(info->FileNameLength);
576*4882a593Smuzhiyun 	de->resume_key = info->FileIndex;
577*4882a593Smuzhiyun 	de->ino = le64_to_cpu(info->UniqueId);
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun 
cifs_fill_dirent_both(struct cifs_dirent * de,const FILE_BOTH_DIRECTORY_INFO * info)580*4882a593Smuzhiyun static void cifs_fill_dirent_both(struct cifs_dirent *de,
581*4882a593Smuzhiyun 		const FILE_BOTH_DIRECTORY_INFO *info)
582*4882a593Smuzhiyun {
583*4882a593Smuzhiyun 	de->name = &info->FileName[0];
584*4882a593Smuzhiyun 	de->namelen = le32_to_cpu(info->FileNameLength);
585*4882a593Smuzhiyun 	de->resume_key = info->FileIndex;
586*4882a593Smuzhiyun }
587*4882a593Smuzhiyun 
cifs_fill_dirent_std(struct cifs_dirent * de,const FIND_FILE_STANDARD_INFO * info)588*4882a593Smuzhiyun static void cifs_fill_dirent_std(struct cifs_dirent *de,
589*4882a593Smuzhiyun 		const FIND_FILE_STANDARD_INFO *info)
590*4882a593Smuzhiyun {
591*4882a593Smuzhiyun 	de->name = &info->FileName[0];
592*4882a593Smuzhiyun 	/* one byte length, no endianess conversion */
593*4882a593Smuzhiyun 	de->namelen = info->FileNameLength;
594*4882a593Smuzhiyun 	de->resume_key = info->ResumeKey;
595*4882a593Smuzhiyun }
596*4882a593Smuzhiyun 
cifs_fill_dirent(struct cifs_dirent * de,const void * info,u16 level,bool is_unicode)597*4882a593Smuzhiyun static int cifs_fill_dirent(struct cifs_dirent *de, const void *info,
598*4882a593Smuzhiyun 		u16 level, bool is_unicode)
599*4882a593Smuzhiyun {
600*4882a593Smuzhiyun 	memset(de, 0, sizeof(*de));
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	switch (level) {
603*4882a593Smuzhiyun 	case SMB_FIND_FILE_POSIX_INFO:
604*4882a593Smuzhiyun 		cifs_fill_dirent_posix(de, info);
605*4882a593Smuzhiyun 		break;
606*4882a593Smuzhiyun 	case SMB_FIND_FILE_UNIX:
607*4882a593Smuzhiyun 		cifs_fill_dirent_unix(de, info, is_unicode);
608*4882a593Smuzhiyun 		break;
609*4882a593Smuzhiyun 	case SMB_FIND_FILE_DIRECTORY_INFO:
610*4882a593Smuzhiyun 		cifs_fill_dirent_dir(de, info);
611*4882a593Smuzhiyun 		break;
612*4882a593Smuzhiyun 	case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
613*4882a593Smuzhiyun 		cifs_fill_dirent_full(de, info);
614*4882a593Smuzhiyun 		break;
615*4882a593Smuzhiyun 	case SMB_FIND_FILE_ID_FULL_DIR_INFO:
616*4882a593Smuzhiyun 		cifs_fill_dirent_search(de, info);
617*4882a593Smuzhiyun 		break;
618*4882a593Smuzhiyun 	case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
619*4882a593Smuzhiyun 		cifs_fill_dirent_both(de, info);
620*4882a593Smuzhiyun 		break;
621*4882a593Smuzhiyun 	case SMB_FIND_FILE_INFO_STANDARD:
622*4882a593Smuzhiyun 		cifs_fill_dirent_std(de, info);
623*4882a593Smuzhiyun 		break;
624*4882a593Smuzhiyun 	default:
625*4882a593Smuzhiyun 		cifs_dbg(FYI, "Unknown findfirst level %d\n", level);
626*4882a593Smuzhiyun 		return -EINVAL;
627*4882a593Smuzhiyun 	}
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	return 0;
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun #define UNICODE_DOT cpu_to_le16(0x2e)
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun /* 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)635*4882a593Smuzhiyun static int cifs_entry_is_dot(struct cifs_dirent *de, bool is_unicode)
636*4882a593Smuzhiyun {
637*4882a593Smuzhiyun 	int rc = 0;
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	if (!de->name)
640*4882a593Smuzhiyun 		return 0;
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	if (is_unicode) {
643*4882a593Smuzhiyun 		__le16 *ufilename = (__le16 *)de->name;
644*4882a593Smuzhiyun 		if (de->namelen == 2) {
645*4882a593Smuzhiyun 			/* check for . */
646*4882a593Smuzhiyun 			if (ufilename[0] == UNICODE_DOT)
647*4882a593Smuzhiyun 				rc = 1;
648*4882a593Smuzhiyun 		} else if (de->namelen == 4) {
649*4882a593Smuzhiyun 			/* check for .. */
650*4882a593Smuzhiyun 			if (ufilename[0] == UNICODE_DOT &&
651*4882a593Smuzhiyun 			    ufilename[1] == UNICODE_DOT)
652*4882a593Smuzhiyun 				rc = 2;
653*4882a593Smuzhiyun 		}
654*4882a593Smuzhiyun 	} else /* ASCII */ {
655*4882a593Smuzhiyun 		if (de->namelen == 1) {
656*4882a593Smuzhiyun 			if (de->name[0] == '.')
657*4882a593Smuzhiyun 				rc = 1;
658*4882a593Smuzhiyun 		} else if (de->namelen == 2) {
659*4882a593Smuzhiyun 			if (de->name[0] == '.' && de->name[1] == '.')
660*4882a593Smuzhiyun 				rc = 2;
661*4882a593Smuzhiyun 		}
662*4882a593Smuzhiyun 	}
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 	return rc;
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun /* Check if directory that we are searching has changed so we can decide
668*4882a593Smuzhiyun    whether we can use the cached search results from the previous search */
is_dir_changed(struct file * file)669*4882a593Smuzhiyun static int is_dir_changed(struct file *file)
670*4882a593Smuzhiyun {
671*4882a593Smuzhiyun 	struct inode *inode = file_inode(file);
672*4882a593Smuzhiyun 	struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	if (cifsInfo->time == 0)
675*4882a593Smuzhiyun 		return 1; /* directory was changed, perhaps due to unlink */
676*4882a593Smuzhiyun 	else
677*4882a593Smuzhiyun 		return 0;
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun 
cifs_save_resume_key(const char * current_entry,struct cifsFileInfo * file_info)681*4882a593Smuzhiyun static int cifs_save_resume_key(const char *current_entry,
682*4882a593Smuzhiyun 	struct cifsFileInfo *file_info)
683*4882a593Smuzhiyun {
684*4882a593Smuzhiyun 	struct cifs_dirent de;
685*4882a593Smuzhiyun 	int rc;
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	rc = cifs_fill_dirent(&de, current_entry, file_info->srch_inf.info_level,
688*4882a593Smuzhiyun 			      file_info->srch_inf.unicode);
689*4882a593Smuzhiyun 	if (!rc) {
690*4882a593Smuzhiyun 		file_info->srch_inf.presume_name = de.name;
691*4882a593Smuzhiyun 		file_info->srch_inf.resume_name_len = de.namelen;
692*4882a593Smuzhiyun 		file_info->srch_inf.resume_key = de.resume_key;
693*4882a593Smuzhiyun 	}
694*4882a593Smuzhiyun 	return rc;
695*4882a593Smuzhiyun }
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun /*
698*4882a593Smuzhiyun  * Find the corresponding entry in the search. Note that the SMB server returns
699*4882a593Smuzhiyun  * search entries for . and .. which complicates logic here if we choose to
700*4882a593Smuzhiyun  * parse for them and we do not assume that they are located in the findfirst
701*4882a593Smuzhiyun  * return buffer. We start counting in the buffer with entry 2 and increment for
702*4882a593Smuzhiyun  * every entry (do not increment for . or .. entry).
703*4882a593Smuzhiyun  */
704*4882a593Smuzhiyun static int
find_cifs_entry(const unsigned int xid,struct cifs_tcon * tcon,loff_t pos,struct file * file,char * full_path,char ** current_entry,int * num_to_ret)705*4882a593Smuzhiyun find_cifs_entry(const unsigned int xid, struct cifs_tcon *tcon, loff_t pos,
706*4882a593Smuzhiyun 		struct file *file, char *full_path,
707*4882a593Smuzhiyun 		char **current_entry, int *num_to_ret)
708*4882a593Smuzhiyun {
709*4882a593Smuzhiyun 	__u16 search_flags;
710*4882a593Smuzhiyun 	int rc = 0;
711*4882a593Smuzhiyun 	int pos_in_buf = 0;
712*4882a593Smuzhiyun 	loff_t first_entry_in_buffer;
713*4882a593Smuzhiyun 	loff_t index_to_find = pos;
714*4882a593Smuzhiyun 	struct cifsFileInfo *cfile = file->private_data;
715*4882a593Smuzhiyun 	struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
716*4882a593Smuzhiyun 	struct TCP_Server_Info *server = tcon->ses->server;
717*4882a593Smuzhiyun 	/* check if index in the buffer */
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun 	if (!server->ops->query_dir_first || !server->ops->query_dir_next)
720*4882a593Smuzhiyun 		return -ENOSYS;
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	if ((cfile == NULL) || (current_entry == NULL) || (num_to_ret == NULL))
723*4882a593Smuzhiyun 		return -ENOENT;
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 	*current_entry = NULL;
726*4882a593Smuzhiyun 	first_entry_in_buffer = cfile->srch_inf.index_of_last_entry -
727*4882a593Smuzhiyun 					cfile->srch_inf.entries_in_buffer;
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 	/*
730*4882a593Smuzhiyun 	 * If first entry in buf is zero then is first buffer
731*4882a593Smuzhiyun 	 * in search response data which means it is likely . and ..
732*4882a593Smuzhiyun 	 * will be in this buffer, although some servers do not return
733*4882a593Smuzhiyun 	 * . and .. for the root of a drive and for those we need
734*4882a593Smuzhiyun 	 * to start two entries earlier.
735*4882a593Smuzhiyun 	 */
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	dump_cifs_file_struct(file, "In fce ");
738*4882a593Smuzhiyun 	if (((index_to_find < cfile->srch_inf.index_of_last_entry) &&
739*4882a593Smuzhiyun 	     is_dir_changed(file)) || (index_to_find < first_entry_in_buffer)) {
740*4882a593Smuzhiyun 		/* close and restart search */
741*4882a593Smuzhiyun 		cifs_dbg(FYI, "search backing up - close and restart search\n");
742*4882a593Smuzhiyun 		spin_lock(&cfile->file_info_lock);
743*4882a593Smuzhiyun 		if (server->ops->dir_needs_close(cfile)) {
744*4882a593Smuzhiyun 			cfile->invalidHandle = true;
745*4882a593Smuzhiyun 			spin_unlock(&cfile->file_info_lock);
746*4882a593Smuzhiyun 			if (server->ops->close_dir)
747*4882a593Smuzhiyun 				server->ops->close_dir(xid, tcon, &cfile->fid);
748*4882a593Smuzhiyun 		} else
749*4882a593Smuzhiyun 			spin_unlock(&cfile->file_info_lock);
750*4882a593Smuzhiyun 		if (cfile->srch_inf.ntwrk_buf_start) {
751*4882a593Smuzhiyun 			cifs_dbg(FYI, "freeing SMB ff cache buf on search rewind\n");
752*4882a593Smuzhiyun 			if (cfile->srch_inf.smallBuf)
753*4882a593Smuzhiyun 				cifs_small_buf_release(cfile->srch_inf.
754*4882a593Smuzhiyun 						ntwrk_buf_start);
755*4882a593Smuzhiyun 			else
756*4882a593Smuzhiyun 				cifs_buf_release(cfile->srch_inf.
757*4882a593Smuzhiyun 						ntwrk_buf_start);
758*4882a593Smuzhiyun 			cfile->srch_inf.ntwrk_buf_start = NULL;
759*4882a593Smuzhiyun 		}
760*4882a593Smuzhiyun 		rc = initiate_cifs_search(xid, file, full_path);
761*4882a593Smuzhiyun 		if (rc) {
762*4882a593Smuzhiyun 			cifs_dbg(FYI, "error %d reinitiating a search on rewind\n",
763*4882a593Smuzhiyun 				 rc);
764*4882a593Smuzhiyun 			return rc;
765*4882a593Smuzhiyun 		}
766*4882a593Smuzhiyun 		/* FindFirst/Next set last_entry to NULL on malformed reply */
767*4882a593Smuzhiyun 		if (cfile->srch_inf.last_entry)
768*4882a593Smuzhiyun 			cifs_save_resume_key(cfile->srch_inf.last_entry, cfile);
769*4882a593Smuzhiyun 	}
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	search_flags = CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME;
772*4882a593Smuzhiyun 	if (backup_cred(cifs_sb))
773*4882a593Smuzhiyun 		search_flags |= CIFS_SEARCH_BACKUP_SEARCH;
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun 	while ((index_to_find >= cfile->srch_inf.index_of_last_entry) &&
776*4882a593Smuzhiyun 	       (rc == 0) && !cfile->srch_inf.endOfSearch) {
777*4882a593Smuzhiyun 		cifs_dbg(FYI, "calling findnext2\n");
778*4882a593Smuzhiyun 		rc = server->ops->query_dir_next(xid, tcon, &cfile->fid,
779*4882a593Smuzhiyun 						 search_flags,
780*4882a593Smuzhiyun 						 &cfile->srch_inf);
781*4882a593Smuzhiyun 		/* FindFirst/Next set last_entry to NULL on malformed reply */
782*4882a593Smuzhiyun 		if (cfile->srch_inf.last_entry)
783*4882a593Smuzhiyun 			cifs_save_resume_key(cfile->srch_inf.last_entry, cfile);
784*4882a593Smuzhiyun 		if (rc)
785*4882a593Smuzhiyun 			return -ENOENT;
786*4882a593Smuzhiyun 	}
787*4882a593Smuzhiyun 	if (index_to_find < cfile->srch_inf.index_of_last_entry) {
788*4882a593Smuzhiyun 		/* we found the buffer that contains the entry */
789*4882a593Smuzhiyun 		/* scan and find it */
790*4882a593Smuzhiyun 		int i;
791*4882a593Smuzhiyun 		char *cur_ent;
792*4882a593Smuzhiyun 		char *end_of_smb;
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 		if (cfile->srch_inf.ntwrk_buf_start == NULL) {
795*4882a593Smuzhiyun 			cifs_dbg(VFS, "ntwrk_buf_start is NULL during readdir\n");
796*4882a593Smuzhiyun 			return -EIO;
797*4882a593Smuzhiyun 		}
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 		end_of_smb = cfile->srch_inf.ntwrk_buf_start +
800*4882a593Smuzhiyun 			server->ops->calc_smb_size(
801*4882a593Smuzhiyun 					cfile->srch_inf.ntwrk_buf_start,
802*4882a593Smuzhiyun 					server);
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun 		cur_ent = cfile->srch_inf.srch_entries_start;
805*4882a593Smuzhiyun 		first_entry_in_buffer = cfile->srch_inf.index_of_last_entry
806*4882a593Smuzhiyun 					- cfile->srch_inf.entries_in_buffer;
807*4882a593Smuzhiyun 		pos_in_buf = index_to_find - first_entry_in_buffer;
808*4882a593Smuzhiyun 		cifs_dbg(FYI, "found entry - pos_in_buf %d\n", pos_in_buf);
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun 		for (i = 0; (i < (pos_in_buf)) && (cur_ent != NULL); i++) {
811*4882a593Smuzhiyun 			/* go entry by entry figuring out which is first */
812*4882a593Smuzhiyun 			cur_ent = nxt_dir_entry(cur_ent, end_of_smb,
813*4882a593Smuzhiyun 						cfile->srch_inf.info_level);
814*4882a593Smuzhiyun 		}
815*4882a593Smuzhiyun 		if ((cur_ent == NULL) && (i < pos_in_buf)) {
816*4882a593Smuzhiyun 			/* BB fixme - check if we should flag this error */
817*4882a593Smuzhiyun 			cifs_dbg(VFS, "reached end of buf searching for pos in buf %d index to find %lld rc %d\n",
818*4882a593Smuzhiyun 				 pos_in_buf, index_to_find, rc);
819*4882a593Smuzhiyun 		}
820*4882a593Smuzhiyun 		rc = 0;
821*4882a593Smuzhiyun 		*current_entry = cur_ent;
822*4882a593Smuzhiyun 	} else {
823*4882a593Smuzhiyun 		cifs_dbg(FYI, "index not in buffer - could not findnext into it\n");
824*4882a593Smuzhiyun 		return 0;
825*4882a593Smuzhiyun 	}
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 	if (pos_in_buf >= cfile->srch_inf.entries_in_buffer) {
828*4882a593Smuzhiyun 		cifs_dbg(FYI, "can not return entries pos_in_buf beyond last\n");
829*4882a593Smuzhiyun 		*num_to_ret = 0;
830*4882a593Smuzhiyun 	} else
831*4882a593Smuzhiyun 		*num_to_ret = cfile->srch_inf.entries_in_buffer - pos_in_buf;
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 	return rc;
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun 
cifs_filldir(char * find_entry,struct file * file,struct dir_context * ctx,char * scratch_buf,unsigned int max_len)836*4882a593Smuzhiyun static int cifs_filldir(char *find_entry, struct file *file,
837*4882a593Smuzhiyun 		struct dir_context *ctx,
838*4882a593Smuzhiyun 		char *scratch_buf, unsigned int max_len)
839*4882a593Smuzhiyun {
840*4882a593Smuzhiyun 	struct cifsFileInfo *file_info = file->private_data;
841*4882a593Smuzhiyun 	struct super_block *sb = file_inode(file)->i_sb;
842*4882a593Smuzhiyun 	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
843*4882a593Smuzhiyun 	struct cifs_dirent de = { NULL, };
844*4882a593Smuzhiyun 	struct cifs_fattr fattr;
845*4882a593Smuzhiyun 	struct qstr name;
846*4882a593Smuzhiyun 	int rc = 0;
847*4882a593Smuzhiyun 	ino_t ino;
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 	rc = cifs_fill_dirent(&de, find_entry, file_info->srch_inf.info_level,
850*4882a593Smuzhiyun 			      file_info->srch_inf.unicode);
851*4882a593Smuzhiyun 	if (rc)
852*4882a593Smuzhiyun 		return rc;
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 	if (de.namelen > max_len) {
855*4882a593Smuzhiyun 		cifs_dbg(VFS, "bad search response length %zd past smb end\n",
856*4882a593Smuzhiyun 			 de.namelen);
857*4882a593Smuzhiyun 		return -EINVAL;
858*4882a593Smuzhiyun 	}
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun 	/* skip . and .. since we added them first */
861*4882a593Smuzhiyun 	if (cifs_entry_is_dot(&de, file_info->srch_inf.unicode))
862*4882a593Smuzhiyun 		return 0;
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 	if (file_info->srch_inf.unicode) {
865*4882a593Smuzhiyun 		struct nls_table *nlt = cifs_sb->local_nls;
866*4882a593Smuzhiyun 		int map_type;
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun 		map_type = cifs_remap(cifs_sb);
869*4882a593Smuzhiyun 		name.name = scratch_buf;
870*4882a593Smuzhiyun 		name.len =
871*4882a593Smuzhiyun 			cifs_from_utf16((char *)name.name, (__le16 *)de.name,
872*4882a593Smuzhiyun 					UNICODE_NAME_MAX,
873*4882a593Smuzhiyun 					min_t(size_t, de.namelen,
874*4882a593Smuzhiyun 					      (size_t)max_len), nlt, map_type);
875*4882a593Smuzhiyun 		name.len -= nls_nullsize(nlt);
876*4882a593Smuzhiyun 	} else {
877*4882a593Smuzhiyun 		name.name = de.name;
878*4882a593Smuzhiyun 		name.len = de.namelen;
879*4882a593Smuzhiyun 	}
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun 	switch (file_info->srch_inf.info_level) {
882*4882a593Smuzhiyun 	case SMB_FIND_FILE_POSIX_INFO:
883*4882a593Smuzhiyun 		cifs_posix_to_fattr(&fattr,
884*4882a593Smuzhiyun 				    (struct smb2_posix_info *)find_entry,
885*4882a593Smuzhiyun 				    cifs_sb);
886*4882a593Smuzhiyun 		break;
887*4882a593Smuzhiyun 	case SMB_FIND_FILE_UNIX:
888*4882a593Smuzhiyun 		cifs_unix_basic_to_fattr(&fattr,
889*4882a593Smuzhiyun 					 &((FILE_UNIX_INFO *)find_entry)->basic,
890*4882a593Smuzhiyun 					 cifs_sb);
891*4882a593Smuzhiyun 		break;
892*4882a593Smuzhiyun 	case SMB_FIND_FILE_INFO_STANDARD:
893*4882a593Smuzhiyun 		cifs_std_info_to_fattr(&fattr,
894*4882a593Smuzhiyun 				       (FIND_FILE_STANDARD_INFO *)find_entry,
895*4882a593Smuzhiyun 				       cifs_sb);
896*4882a593Smuzhiyun 		break;
897*4882a593Smuzhiyun 	case SMB_FIND_FILE_ID_FULL_DIR_INFO:
898*4882a593Smuzhiyun 		cifs_fulldir_info_to_fattr(&fattr,
899*4882a593Smuzhiyun 					   (SEARCH_ID_FULL_DIR_INFO *)find_entry,
900*4882a593Smuzhiyun 					   cifs_sb);
901*4882a593Smuzhiyun 		break;
902*4882a593Smuzhiyun 	default:
903*4882a593Smuzhiyun 		cifs_dir_info_to_fattr(&fattr,
904*4882a593Smuzhiyun 				       (FILE_DIRECTORY_INFO *)find_entry,
905*4882a593Smuzhiyun 				       cifs_sb);
906*4882a593Smuzhiyun 		break;
907*4882a593Smuzhiyun 	}
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 	if (de.ino && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
910*4882a593Smuzhiyun 		fattr.cf_uniqueid = de.ino;
911*4882a593Smuzhiyun 	} else {
912*4882a593Smuzhiyun 		fattr.cf_uniqueid = iunique(sb, ROOT_I);
913*4882a593Smuzhiyun 		cifs_autodisable_serverino(cifs_sb);
914*4882a593Smuzhiyun 	}
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun 	if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) &&
917*4882a593Smuzhiyun 	    couldbe_mf_symlink(&fattr))
918*4882a593Smuzhiyun 		/*
919*4882a593Smuzhiyun 		 * trying to get the type and mode can be slow,
920*4882a593Smuzhiyun 		 * so just call those regular files for now, and mark
921*4882a593Smuzhiyun 		 * for reval
922*4882a593Smuzhiyun 		 */
923*4882a593Smuzhiyun 		fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 	cifs_prime_dcache(file_dentry(file), &name, &fattr);
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun 	ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
928*4882a593Smuzhiyun 	return !dir_emit(ctx, name.name, name.len, ino, fattr.cf_dtype);
929*4882a593Smuzhiyun }
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 
cifs_readdir(struct file * file,struct dir_context * ctx)932*4882a593Smuzhiyun int cifs_readdir(struct file *file, struct dir_context *ctx)
933*4882a593Smuzhiyun {
934*4882a593Smuzhiyun 	int rc = 0;
935*4882a593Smuzhiyun 	unsigned int xid;
936*4882a593Smuzhiyun 	int i;
937*4882a593Smuzhiyun 	struct cifs_tcon *tcon;
938*4882a593Smuzhiyun 	struct cifsFileInfo *cifsFile = NULL;
939*4882a593Smuzhiyun 	char *current_entry;
940*4882a593Smuzhiyun 	int num_to_fill = 0;
941*4882a593Smuzhiyun 	char *tmp_buf = NULL;
942*4882a593Smuzhiyun 	char *end_of_smb;
943*4882a593Smuzhiyun 	unsigned int max_len;
944*4882a593Smuzhiyun 	char *full_path = NULL;
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 	xid = get_xid();
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 	full_path = build_path_from_dentry(file_dentry(file));
949*4882a593Smuzhiyun 	if (full_path == NULL) {
950*4882a593Smuzhiyun 		rc = -ENOMEM;
951*4882a593Smuzhiyun 		goto rddir2_exit;
952*4882a593Smuzhiyun 	}
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun 	/*
955*4882a593Smuzhiyun 	 * Ensure FindFirst doesn't fail before doing filldir() for '.' and
956*4882a593Smuzhiyun 	 * '..'. Otherwise we won't be able to notify VFS in case of failure.
957*4882a593Smuzhiyun 	 */
958*4882a593Smuzhiyun 	if (file->private_data == NULL) {
959*4882a593Smuzhiyun 		rc = initiate_cifs_search(xid, file, full_path);
960*4882a593Smuzhiyun 		cifs_dbg(FYI, "initiate cifs search rc %d\n", rc);
961*4882a593Smuzhiyun 		if (rc)
962*4882a593Smuzhiyun 			goto rddir2_exit;
963*4882a593Smuzhiyun 	}
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun 	if (!dir_emit_dots(file, ctx))
966*4882a593Smuzhiyun 		goto rddir2_exit;
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun 	/* 1) If search is active,
969*4882a593Smuzhiyun 		is in current search buffer?
970*4882a593Smuzhiyun 		if it before then restart search
971*4882a593Smuzhiyun 		if after then keep searching till find it */
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 	cifsFile = file->private_data;
974*4882a593Smuzhiyun 	if (cifsFile->srch_inf.endOfSearch) {
975*4882a593Smuzhiyun 		if (cifsFile->srch_inf.emptyDir) {
976*4882a593Smuzhiyun 			cifs_dbg(FYI, "End of search, empty dir\n");
977*4882a593Smuzhiyun 			rc = 0;
978*4882a593Smuzhiyun 			goto rddir2_exit;
979*4882a593Smuzhiyun 		}
980*4882a593Smuzhiyun 	} /* else {
981*4882a593Smuzhiyun 		cifsFile->invalidHandle = true;
982*4882a593Smuzhiyun 		tcon->ses->server->close(xid, tcon, &cifsFile->fid);
983*4882a593Smuzhiyun 	} */
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun 	tcon = tlink_tcon(cifsFile->tlink);
986*4882a593Smuzhiyun 	rc = find_cifs_entry(xid, tcon, ctx->pos, file, full_path,
987*4882a593Smuzhiyun 			     &current_entry, &num_to_fill);
988*4882a593Smuzhiyun 	if (rc) {
989*4882a593Smuzhiyun 		cifs_dbg(FYI, "fce error %d\n", rc);
990*4882a593Smuzhiyun 		goto rddir2_exit;
991*4882a593Smuzhiyun 	} else if (current_entry != NULL) {
992*4882a593Smuzhiyun 		cifs_dbg(FYI, "entry %lld found\n", ctx->pos);
993*4882a593Smuzhiyun 	} else {
994*4882a593Smuzhiyun 		cifs_dbg(FYI, "Could not find entry\n");
995*4882a593Smuzhiyun 		goto rddir2_exit;
996*4882a593Smuzhiyun 	}
997*4882a593Smuzhiyun 	cifs_dbg(FYI, "loop through %d times filling dir for net buf %p\n",
998*4882a593Smuzhiyun 		 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start);
999*4882a593Smuzhiyun 	max_len = tcon->ses->server->ops->calc_smb_size(
1000*4882a593Smuzhiyun 			cifsFile->srch_inf.ntwrk_buf_start,
1001*4882a593Smuzhiyun 			tcon->ses->server);
1002*4882a593Smuzhiyun 	end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun 	tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
1005*4882a593Smuzhiyun 	if (tmp_buf == NULL) {
1006*4882a593Smuzhiyun 		rc = -ENOMEM;
1007*4882a593Smuzhiyun 		goto rddir2_exit;
1008*4882a593Smuzhiyun 	}
1009*4882a593Smuzhiyun 
1010*4882a593Smuzhiyun 	for (i = 0; i < num_to_fill; i++) {
1011*4882a593Smuzhiyun 		if (current_entry == NULL) {
1012*4882a593Smuzhiyun 			/* evaluate whether this case is an error */
1013*4882a593Smuzhiyun 			cifs_dbg(VFS, "past SMB end,  num to fill %d i %d\n",
1014*4882a593Smuzhiyun 				 num_to_fill, i);
1015*4882a593Smuzhiyun 			break;
1016*4882a593Smuzhiyun 		}
1017*4882a593Smuzhiyun 		/*
1018*4882a593Smuzhiyun 		 * if buggy server returns . and .. late do we want to
1019*4882a593Smuzhiyun 		 * check for that here?
1020*4882a593Smuzhiyun 		 */
1021*4882a593Smuzhiyun 		*tmp_buf = 0;
1022*4882a593Smuzhiyun 		rc = cifs_filldir(current_entry, file, ctx,
1023*4882a593Smuzhiyun 				  tmp_buf, max_len);
1024*4882a593Smuzhiyun 		if (rc) {
1025*4882a593Smuzhiyun 			if (rc > 0)
1026*4882a593Smuzhiyun 				rc = 0;
1027*4882a593Smuzhiyun 			break;
1028*4882a593Smuzhiyun 		}
1029*4882a593Smuzhiyun 
1030*4882a593Smuzhiyun 		ctx->pos++;
1031*4882a593Smuzhiyun 		if (ctx->pos ==
1032*4882a593Smuzhiyun 			cifsFile->srch_inf.index_of_last_entry) {
1033*4882a593Smuzhiyun 			cifs_dbg(FYI, "last entry in buf at pos %lld %s\n",
1034*4882a593Smuzhiyun 				 ctx->pos, tmp_buf);
1035*4882a593Smuzhiyun 			cifs_save_resume_key(current_entry, cifsFile);
1036*4882a593Smuzhiyun 			break;
1037*4882a593Smuzhiyun 		} else
1038*4882a593Smuzhiyun 			current_entry =
1039*4882a593Smuzhiyun 				nxt_dir_entry(current_entry, end_of_smb,
1040*4882a593Smuzhiyun 					cifsFile->srch_inf.info_level);
1041*4882a593Smuzhiyun 	}
1042*4882a593Smuzhiyun 	kfree(tmp_buf);
1043*4882a593Smuzhiyun 
1044*4882a593Smuzhiyun rddir2_exit:
1045*4882a593Smuzhiyun 	kfree(full_path);
1046*4882a593Smuzhiyun 	free_xid(xid);
1047*4882a593Smuzhiyun 	return rc;
1048*4882a593Smuzhiyun }
1049