blob: 76b1b37c9e6b7e916258cb19a862875843d20e66 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/readdir.c
3 *
4 * Directory search handling
Steve French6dc0f872007-07-06 23:13:06 +00005 *
Steve Frenchad7a2922008-02-07 23:25:02 +00006 * Copyright (C) International Business Machines Corp., 2004, 2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Author(s): Steve French (sfrench@us.ibm.com)
8 *
9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23#include <linux/fs.h>
Dave Kleikamp273d81d2006-06-01 19:41:23 +000024#include <linux/pagemap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "cifspdu.h"
28#include "cifsglob.h"
29#include "cifsproto.h"
30#include "cifs_unicode.h"
31#include "cifs_debug.h"
32#include "cifs_fs_sb.h"
33#include "cifsfs.h"
34
Jeff Laytonf5884162009-04-30 07:18:00 -040035/*
36 * To be safe - for UCS to UTF-8 with strings loaded with the rare long
37 * characters alloc more to account for such multibyte target UTF-8
38 * characters.
39 */
40#define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
41
Steve French39798772006-05-31 22:40:51 +000042#ifdef CONFIG_CIFS_DEBUG2
43static void dump_cifs_file_struct(struct file *file, char *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Steve French6dc0f872007-07-06 23:13:06 +000045 struct cifsFileInfo *cf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Steve French4523cc32007-04-30 20:13:06 +000047 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 cf = file->private_data;
Steve French4523cc32007-04-30 20:13:06 +000049 if (cf == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +000050 cFYI(1, "empty cifs private file data");
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 return;
52 }
Steve Frenchad7a2922008-02-07 23:25:02 +000053 if (cf->invalidHandle)
Joe Perchesb6b38f72010-04-21 03:50:45 +000054 cFYI(1, "invalid handle");
Steve Frenchad7a2922008-02-07 23:25:02 +000055 if (cf->srch_inf.endOfSearch)
Joe Perchesb6b38f72010-04-21 03:50:45 +000056 cFYI(1, "end of search");
Steve Frenchad7a2922008-02-07 23:25:02 +000057 if (cf->srch_inf.emptyDir)
Joe Perchesb6b38f72010-04-21 03:50:45 +000058 cFYI(1, "empty dir");
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
Steve French39798772006-05-31 22:40:51 +000060}
Steve French90c81e02008-02-12 20:32:36 +000061#else
62static inline void dump_cifs_file_struct(struct file *file, char *label)
63{
64}
Steve French39798772006-05-31 22:40:51 +000065#endif /* DEBUG2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Jeff Laytoncc0bad72009-06-25 00:56:52 -040067/*
68 * Find the dentry that matches "name". If there isn't one, create one. If it's
69 * a negative dentry or the uniqueid changed, then drop it and recreate it.
70 */
71static struct dentry *
72cifs_readdir_lookup(struct dentry *parent, struct qstr *name,
73 struct cifs_fattr *fattr)
74{
75 struct dentry *dentry, *alias;
76 struct inode *inode;
77 struct super_block *sb = parent->d_inode->i_sb;
78
Joe Perchesb6b38f72010-04-21 03:50:45 +000079 cFYI(1, "For %s", name->name);
Jeff Laytoncc0bad72009-06-25 00:56:52 -040080
Jeff Layton05507fa2010-02-05 13:30:36 -050081 if (parent->d_op && parent->d_op->d_hash)
Nick Pigginb1e6a012011-01-07 17:49:28 +110082 parent->d_op->d_hash(parent, parent->d_inode, name);
Jeff Layton05507fa2010-02-05 13:30:36 -050083 else
84 name->hash = full_name_hash(name->name, name->len);
85
Jeff Laytoncc0bad72009-06-25 00:56:52 -040086 dentry = d_lookup(parent, name);
87 if (dentry) {
88 /* FIXME: check for inode number changes? */
89 if (dentry->d_inode != NULL)
90 return dentry;
91 d_drop(dentry);
92 dput(dentry);
93 }
94
95 dentry = d_alloc(parent, name);
96 if (dentry == NULL)
97 return NULL;
98
99 inode = cifs_iget(sb, fattr);
100 if (!inode) {
101 dput(dentry);
102 return NULL;
103 }
104
Jeff Layton0d424ad2010-09-20 16:01:35 -0700105 if (cifs_sb_master_tcon(CIFS_SB(sb))->nocase)
Nick Pigginfb045ad2011-01-07 17:49:55 +1100106 d_set_d_op(dentry, &cifs_ci_dentry_ops);
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400107 else
Nick Pigginfb045ad2011-01-07 17:49:55 +1100108 d_set_d_op(dentry, &cifs_dentry_ops);
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400109
110 alias = d_materialise_unique(dentry, inode);
111 if (alias != NULL) {
112 dput(dentry);
113 if (IS_ERR(alias))
114 return NULL;
115 dentry = alias;
116 }
117
118 return dentry;
119}
120
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400121static void
122cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400124 fattr->cf_uid = cifs_sb->mnt_uid;
125 fattr->cf_gid = cifs_sb->mnt_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400127 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
128 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
129 fattr->cf_dtype = DT_DIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 } else {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400131 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
132 fattr->cf_dtype = DT_REG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400135 if (fattr->cf_cifsattrs & ATTR_READONLY)
136 fattr->cf_mode &= ~S_IWUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400138 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
139 fattr->cf_cifsattrs & ATTR_SYSTEM) {
140 if (fattr->cf_eof == 0) {
141 fattr->cf_mode &= ~S_IFMT;
142 fattr->cf_mode |= S_IFIFO;
143 fattr->cf_dtype = DT_FIFO;
Steve French3020a1f2005-11-18 11:31:10 -0800144 } else {
Jeff Layton4468eb32008-05-22 09:31:40 -0400145 /*
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400146 * trying to get the type and mode via SFU can be slow,
147 * so just call those regular files for now, and mark
148 * for reval
Jeff Layton4468eb32008-05-22 09:31:40 -0400149 */
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400150 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
Jeff Layton4468eb32008-05-22 09:31:40 -0400151 }
152 }
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400153}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Steve French15dd4782009-09-25 02:24:45 +0000155static void
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400156cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info,
157 struct cifs_sb_info *cifs_sb)
158{
159 memset(fattr, 0, sizeof(*fattr));
160 fattr->cf_cifsattrs = le32_to_cpu(info->ExtFileAttributes);
161 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
162 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
Jeff Layton20054bd2011-01-07 11:30:27 -0500163 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400164 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
165 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
166 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400168 cifs_fill_common_info(fattr, cifs_sb);
169}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Steve French15dd4782009-09-25 02:24:45 +0000171static void
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400172cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info,
173 struct cifs_sb_info *cifs_sb)
174{
Jeff Layton0d424ad2010-09-20 16:01:35 -0700175 int offset = cifs_sb_master_tcon(cifs_sb)->ses->server->timeAdj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400177 memset(fattr, 0, sizeof(*fattr));
178 fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate,
179 info->LastAccessTime, offset);
180 fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate,
181 info->LastWriteTime, offset);
182 fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate,
183 info->LastWriteTime, offset);
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800184
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400185 fattr->cf_cifsattrs = le16_to_cpu(info->Attributes);
186 fattr->cf_bytes = le32_to_cpu(info->AllocationSize);
187 fattr->cf_eof = le32_to_cpu(info->DataSize);
Dave Kleikamp273d81d2006-06-01 19:41:23 +0000188
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400189 cifs_fill_common_info(fattr, cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Steve French0e0d2cf2009-05-01 05:27:32 +0000192/* BB eventually need to add the following helper function to
193 resolve NT_STATUS_STOPPED_ON_SYMLINK return code when
194 we try to do FindFirst on (NTFS) directory symlinks */
195/*
196int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
197 int xid)
198{
199 __u16 fid;
200 int len;
201 int oplock = 0;
202 int rc;
Jeff Laytona6e8a842010-09-20 16:01:33 -0700203 struct cifsTconInfo *ptcon = cifs_sb_tcon(cifs_sb);
Steve French0e0d2cf2009-05-01 05:27:32 +0000204 char *tmpbuffer;
205
206 rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ,
207 OPEN_REPARSE_POINT, &fid, &oplock, NULL,
208 cifs_sb->local_nls,
209 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
210 if (!rc) {
211 tmpbuffer = kmalloc(maxpath);
212 rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path,
213 tmpbuffer,
214 maxpath -1,
215 fid,
216 cifs_sb->local_nls);
217 if (CIFSSMBClose(xid, ptcon, fid)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000218 cFYI(1, "Error closing temporary reparsepoint open");
Steve French0e0d2cf2009-05-01 05:27:32 +0000219 }
220 }
221}
222 */
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224static int initiate_cifs_search(const int xid, struct file *file)
225{
226 int rc = 0;
Jeff Layton7ffec372010-09-29 19:51:11 -0400227 char *full_path = NULL;
Steve French38702532007-07-08 15:40:40 +0000228 struct cifsFileInfo *cifsFile;
Jeff Layton7ffec372010-09-29 19:51:11 -0400229 struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
Jeff Layton59c55ba2010-11-12 06:30:29 -0500230 struct tcon_link *tlink = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct cifsTconInfo *pTcon;
232
Jeff Layton7ffec372010-09-29 19:51:11 -0400233 if (file->private_data == NULL) {
Jeff Layton59c55ba2010-11-12 06:30:29 -0500234 tlink = cifs_sb_tlink(cifs_sb);
235 if (IS_ERR(tlink))
236 return PTR_ERR(tlink);
237
238 cifsFile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
239 if (cifsFile == NULL) {
240 rc = -ENOMEM;
241 goto error_exit;
242 }
243 file->private_data = cifsFile;
244 cifsFile->tlink = cifs_get_tlink(tlink);
245 pTcon = tlink_tcon(tlink);
246 } else {
247 cifsFile = file->private_data;
248 pTcon = tlink_tcon(cifsFile->tlink);
Jeff Layton7ffec372010-09-29 19:51:11 -0400249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Steve French4b18f2a2008-04-29 00:06:05 +0000251 cifsFile->invalidHandle = true;
252 cifsFile->srch_inf.endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800254 full_path = build_path_from_dentry(file->f_path.dentry);
Jeff Layton7ffec372010-09-29 19:51:11 -0400255 if (full_path == NULL) {
256 rc = -ENOMEM;
257 goto error_exit;
258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Joe Perchesb6b38f72010-04-21 03:50:45 +0000260 cFYI(1, "Full path: %s start at: %lld", full_path, file->f_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Steve French75cf6bd2005-04-28 22:41:04 -0700262ffirst_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 /* test for Unix extensions */
Steve Frenchc18c8422007-07-18 23:21:09 +0000264 /* but now check for them on the share/mount not on the SMB session */
265/* if (pTcon->ses->capabilities & CAP_UNIX) { */
Steve Frenchad7a2922008-02-07 23:25:02 +0000266 if (pTcon->unix_ext)
Steve French5bafd762006-06-07 00:18:43 +0000267 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
Steve Frenchad7a2922008-02-07 23:25:02 +0000268 else if ((pTcon->ses->capabilities &
Steve French5bafd762006-06-07 00:18:43 +0000269 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
270 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
272 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
273 } else /* not srvinos - BB fixme add check for backlevel? */ {
274 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
275 }
276
Steve French38702532007-07-08 15:40:40 +0000277 rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
Steve French737b7582005-04-28 22:41:06 -0700278 &cifsFile->netfid, &cifsFile->srch_inf,
Steve French38702532007-07-08 15:40:40 +0000279 cifs_sb->mnt_cifs_flags &
Steve Frencheafe8702005-09-15 21:47:30 -0700280 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
Steve French4523cc32007-04-30 20:13:06 +0000281 if (rc == 0)
Steve French4b18f2a2008-04-29 00:06:05 +0000282 cifsFile->invalidHandle = false;
Steve Frenche836f012009-05-01 16:20:35 +0000283 /* BB add following call to handle readdir on new NTFS symlink errors
Steve French0e0d2cf2009-05-01 05:27:32 +0000284 else if STATUS_STOPPED_ON_SYMLINK
285 call get_symlink_reparse_path and retry with new path */
286 else if ((rc == -EOPNOTSUPP) &&
Steve French75cf6bd2005-04-28 22:41:04 -0700287 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
288 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
289 goto ffirst_retry;
290 }
Jeff Layton7ffec372010-09-29 19:51:11 -0400291error_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 kfree(full_path);
Jeff Layton7ffec372010-09-29 19:51:11 -0400293 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return rc;
295}
296
297/* return length of unicode string in bytes */
298static int cifs_unicode_bytelen(char *str)
299{
300 int len;
Steve French63d25832007-11-05 21:46:10 +0000301 __le16 *ustr = (__le16 *)str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Steve French38702532007-07-08 15:40:40 +0000303 for (len = 0; len <= PATH_MAX; len++) {
Steve French4523cc32007-04-30 20:13:06 +0000304 if (ustr[len] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return len << 1;
306 }
Joe Perchesb6b38f72010-04-21 03:50:45 +0000307 cFYI(1, "Unicode string longer than PATH_MAX found");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return len << 1;
309}
310
Steve French5bafd762006-06-07 00:18:43 +0000311static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Steve French38702532007-07-08 15:40:40 +0000313 char *new_entry;
Steve Frenchad7a2922008-02-07 23:25:02 +0000314 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Steve French4523cc32007-04-30 20:13:06 +0000316 if (level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000317 FIND_FILE_STANDARD_INFO *pfData;
Steve French5bafd762006-06-07 00:18:43 +0000318 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
319
320 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
321 pfData->FileNameLength;
322 } else
323 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000324 cFYI(1, "new entry %p old entry %p", new_entry, old_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /* validate that new_entry is not past end of SMB */
Steve French4523cc32007-04-30 20:13:06 +0000326 if (new_entry >= end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000327 cERROR(1, "search entry %p began after end of SMB %p old entry %p",
328 new_entry, end_of_smb, old_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return NULL;
Steve French4523cc32007-04-30 20:13:06 +0000330 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
Steve French38702532007-07-08 15:40:40 +0000331 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
332 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
Steve French5bafd762006-06-07 00:18:43 +0000333 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000334 cERROR(1, "search entry %p extends after end of SMB %p",
335 new_entry, end_of_smb);
Steve French09d1db52005-04-28 22:41:08 -0700336 return NULL;
Steve French38702532007-07-08 15:40:40 +0000337 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return new_entry;
339
340}
341
342#define UNICODE_DOT cpu_to_le16(0x2e)
343
344/* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
345static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
346{
347 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000348 char *filename = NULL;
349 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Steve French4523cc32007-04-30 20:13:06 +0000351 if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000352 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 filename = &pFindData->FileName[0];
Steve French4523cc32007-04-30 20:13:06 +0000354 if (cfile->srch_inf.unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 len = cifs_unicode_bytelen(filename);
356 } else {
357 /* BB should we make this strnlen of PATH_MAX? */
358 len = strnlen(filename, 5);
359 }
Steve French4523cc32007-04-30 20:13:06 +0000360 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000361 FILE_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 (FILE_DIRECTORY_INFO *)current_entry;
363 filename = &pFindData->FileName[0];
364 len = le32_to_cpu(pFindData->FileNameLength);
Steve French38702532007-07-08 15:40:40 +0000365 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000366 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000367 FILE_FULL_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 (FILE_FULL_DIRECTORY_INFO *)current_entry;
369 filename = &pFindData->FileName[0];
370 len = le32_to_cpu(pFindData->FileNameLength);
Steve French4523cc32007-04-30 20:13:06 +0000371 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000372 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000373 SEARCH_ID_FULL_DIR_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
375 filename = &pFindData->FileName[0];
376 len = le32_to_cpu(pFindData->FileNameLength);
Steve French38702532007-07-08 15:40:40 +0000377 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000378 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000379 FILE_BOTH_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
381 filename = &pFindData->FileName[0];
382 len = le32_to_cpu(pFindData->FileNameLength);
Steve French4523cc32007-04-30 20:13:06 +0000383 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000384 FIND_FILE_STANDARD_INFO *pFindData =
Steve French5bafd762006-06-07 00:18:43 +0000385 (FIND_FILE_STANDARD_INFO *)current_entry;
386 filename = &pFindData->FileName[0];
Steve French5ddaa682006-08-15 13:35:48 +0000387 len = pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 } else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000389 cFYI(1, "Unknown findfirst level %d",
390 cfile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
Steve French4523cc32007-04-30 20:13:06 +0000393 if (filename) {
394 if (cfile->srch_inf.unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 __le16 *ufilename = (__le16 *)filename;
Steve French4523cc32007-04-30 20:13:06 +0000396 if (len == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* check for . */
Steve French4523cc32007-04-30 20:13:06 +0000398 if (ufilename[0] == UNICODE_DOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 rc = 1;
Steve French4523cc32007-04-30 20:13:06 +0000400 } else if (len == 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /* check for .. */
Steve French4523cc32007-04-30 20:13:06 +0000402 if ((ufilename[0] == UNICODE_DOT)
Steve French38702532007-07-08 15:40:40 +0000403 && (ufilename[1] == UNICODE_DOT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 rc = 2;
405 }
406 } else /* ASCII */ {
Steve French4523cc32007-04-30 20:13:06 +0000407 if (len == 1) {
Steve French38702532007-07-08 15:40:40 +0000408 if (filename[0] == '.')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 rc = 1;
Steve French4523cc32007-04-30 20:13:06 +0000410 } else if (len == 2) {
Steve French790fe572007-07-07 19:25:05 +0000411 if ((filename[0] == '.') && (filename[1] == '.'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 rc = 2;
413 }
414 }
415 }
416
417 return rc;
418}
419
Steve Frencheafe8702005-09-15 21:47:30 -0700420/* Check if directory that we are searching has changed so we can decide
421 whether we can use the cached search results from the previous search */
Steve French38702532007-07-08 15:40:40 +0000422static int is_dir_changed(struct file *file)
Steve Frencheafe8702005-09-15 21:47:30 -0700423{
Christoph Hellwigc33f8d32007-04-02 18:47:20 +0000424 struct inode *inode = file->f_path.dentry->d_inode;
425 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
Steve Frencheafe8702005-09-15 21:47:30 -0700426
Christoph Hellwigc33f8d32007-04-02 18:47:20 +0000427 if (cifsInfo->time == 0)
Steve Frencheafe8702005-09-15 21:47:30 -0700428 return 1; /* directory was changed, perhaps due to unlink */
429 else
430 return 0;
431
432}
433
Steve French0752f152008-10-07 20:03:33 +0000434static int cifs_save_resume_key(const char *current_entry,
435 struct cifsFileInfo *cifsFile)
436{
437 int rc = 0;
438 unsigned int len = 0;
439 __u16 level;
440 char *filename;
441
442 if ((cifsFile == NULL) || (current_entry == NULL))
443 return -EINVAL;
444
445 level = cifsFile->srch_inf.info_level;
446
447 if (level == SMB_FIND_FILE_UNIX) {
448 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
449
450 filename = &pFindData->FileName[0];
451 if (cifsFile->srch_inf.unicode) {
452 len = cifs_unicode_bytelen(filename);
453 } else {
454 /* BB should we make this strnlen of PATH_MAX? */
455 len = strnlen(filename, PATH_MAX);
456 }
457 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
458 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
459 FILE_DIRECTORY_INFO *pFindData =
460 (FILE_DIRECTORY_INFO *)current_entry;
461 filename = &pFindData->FileName[0];
462 len = le32_to_cpu(pFindData->FileNameLength);
463 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
464 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
465 FILE_FULL_DIRECTORY_INFO *pFindData =
466 (FILE_FULL_DIRECTORY_INFO *)current_entry;
467 filename = &pFindData->FileName[0];
468 len = le32_to_cpu(pFindData->FileNameLength);
469 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
470 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
471 SEARCH_ID_FULL_DIR_INFO *pFindData =
472 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
473 filename = &pFindData->FileName[0];
474 len = le32_to_cpu(pFindData->FileNameLength);
475 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
476 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
477 FILE_BOTH_DIRECTORY_INFO *pFindData =
478 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
479 filename = &pFindData->FileName[0];
480 len = le32_to_cpu(pFindData->FileNameLength);
481 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
482 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
483 FIND_FILE_STANDARD_INFO *pFindData =
484 (FIND_FILE_STANDARD_INFO *)current_entry;
485 filename = &pFindData->FileName[0];
486 /* one byte length, no name conversion */
487 len = (unsigned int)pFindData->FileNameLength;
488 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
489 } else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000490 cFYI(1, "Unknown findfirst level %d", level);
Steve French0752f152008-10-07 20:03:33 +0000491 return -EINVAL;
492 }
493 cifsFile->srch_inf.resume_name_len = len;
494 cifsFile->srch_inf.presume_name = filename;
495 return rc;
496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498/* find the corresponding entry in the search */
499/* Note that the SMB server returns search entries for . and .. which
500 complicates logic here if we choose to parse for them and we do not
501 assume that they are located in the findfirst return buffer.*/
502/* We start counting in the buffer with entry 2 and increment for every
503 entry (do not increment for . or .. entry) */
504static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
Steve French38702532007-07-08 15:40:40 +0000505 struct file *file, char **ppCurrentEntry, int *num_to_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 int rc = 0;
508 int pos_in_buf = 0;
509 loff_t first_entry_in_buffer;
510 loff_t index_to_find = file->f_pos;
Steve French38702532007-07-08 15:40:40 +0000511 struct cifsFileInfo *cifsFile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /* check if index in the buffer */
Steve French50c2f752007-07-13 00:33:32 +0000513
Steve French38702532007-07-08 15:40:40 +0000514 if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
Steve Frencheafe8702005-09-15 21:47:30 -0700515 (num_to_ret == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return -ENOENT;
Steve French50c2f752007-07-13 00:33:32 +0000517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 *ppCurrentEntry = NULL;
Steve French38702532007-07-08 15:40:40 +0000519 first_entry_in_buffer =
520 cifsFile->srch_inf.index_of_last_entry -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 cifsFile->srch_inf.entries_in_buffer;
Steve French60808232006-04-22 15:53:05 +0000522
523 /* if first entry in buf is zero then is first buffer
524 in search response data which means it is likely . and ..
525 will be in this buffer, although some servers do not return
526 . and .. for the root of a drive and for those we need
527 to start two entries earlier */
528
Steve French39798772006-05-31 22:40:51 +0000529 dump_cifs_file_struct(file, "In fce ");
Steve French38702532007-07-08 15:40:40 +0000530 if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
531 is_dir_changed(file)) ||
Steve Frencheafe8702005-09-15 21:47:30 -0700532 (index_to_find < first_entry_in_buffer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /* close and restart search */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000534 cFYI(1, "search backing up - close and restart search");
Jeff Layton44772882010-10-15 15:34:03 -0400535 spin_lock(&cifs_file_list_lock);
Steve French77c57ec2008-05-13 21:39:32 +0000536 if (!cifsFile->srch_inf.endOfSearch &&
537 !cifsFile->invalidHandle) {
538 cifsFile->invalidHandle = true;
Jeff Layton44772882010-10-15 15:34:03 -0400539 spin_unlock(&cifs_file_list_lock);
Steve French77c57ec2008-05-13 21:39:32 +0000540 CIFSFindClose(xid, pTcon, cifsFile->netfid);
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000541 } else
Jeff Layton44772882010-10-15 15:34:03 -0400542 spin_unlock(&cifs_file_list_lock);
Steve French4523cc32007-04-30 20:13:06 +0000543 if (cifsFile->srch_inf.ntwrk_buf_start) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000544 cFYI(1, "freeing SMB ff cache buf on search rewind");
Steve French4523cc32007-04-30 20:13:06 +0000545 if (cifsFile->srch_inf.smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +0000546 cifs_small_buf_release(cifsFile->srch_inf.
547 ntwrk_buf_start);
548 else
549 cifs_buf_release(cifsFile->srch_inf.
550 ntwrk_buf_start);
Shirish Pargaonkar76c510a2008-07-24 14:48:33 +0000551 cifsFile->srch_inf.ntwrk_buf_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
Steve French38702532007-07-08 15:40:40 +0000553 rc = initiate_cifs_search(xid, file);
Steve French4523cc32007-04-30 20:13:06 +0000554 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000555 cFYI(1, "error %d reinitiating a search on rewind",
556 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return rc;
558 }
Jeff Laytona364bc02008-10-21 14:42:13 +0000559 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
Steve French38702532007-07-08 15:40:40 +0000562 while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
Steve French4b18f2a2008-04-29 00:06:05 +0000563 (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000564 cFYI(1, "calling findnext2");
Steve French38702532007-07-08 15:40:40 +0000565 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
Steve Frenchdfb75332005-06-22 17:13:47 -0700566 &cifsFile->srch_inf);
Jeff Laytona364bc02008-10-21 14:42:13 +0000567 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
Steve French4523cc32007-04-30 20:13:06 +0000568 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return -ENOENT;
570 }
Steve French4523cc32007-04-30 20:13:06 +0000571 if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* we found the buffer that contains the entry */
573 /* scan and find it */
574 int i;
Steve French38702532007-07-08 15:40:40 +0000575 char *current_entry;
576 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 smbCalcSize((struct smb_hdr *)
578 cifsFile->srch_inf.ntwrk_buf_start);
Steve French60808232006-04-22 15:53:05 +0000579
580 current_entry = cifsFile->srch_inf.srch_entries_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
582 - cifsFile->srch_inf.entries_in_buffer;
583 pos_in_buf = index_to_find - first_entry_in_buffer;
Joe Perchesb6b38f72010-04-21 03:50:45 +0000584 cFYI(1, "found entry - pos_in_buf %d", pos_in_buf);
Steve French5bafd762006-06-07 00:18:43 +0000585
Steve Frenchad7a2922008-02-07 23:25:02 +0000586 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
Steve Frenchdfb75332005-06-22 17:13:47 -0700587 /* go entry by entry figuring out which is first */
Steve French38702532007-07-08 15:40:40 +0000588 current_entry = nxt_dir_entry(current_entry, end_of_smb,
Steve French5bafd762006-06-07 00:18:43 +0000589 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Steve French790fe572007-07-07 19:25:05 +0000591 if ((current_entry == NULL) && (i < pos_in_buf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /* BB fixme - check if we should flag this error */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000593 cERROR(1, "reached end of buf searching for pos in buf"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 " %d index to find %lld rc %d",
Joe Perchesb6b38f72010-04-21 03:50:45 +0000595 pos_in_buf, index_to_find, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597 rc = 0;
598 *ppCurrentEntry = current_entry;
599 } else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000600 cFYI(1, "index not in buffer - could not findnext into it");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 0;
602 }
603
Steve French790fe572007-07-07 19:25:05 +0000604 if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000605 cFYI(1, "can not return entries pos_in_buf beyond last");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 *num_to_ret = 0;
607 } else
608 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 return rc;
611}
612
613/* inode num, inode type and filename returned */
614static int cifs_get_name_from_search_buf(struct qstr *pqst,
615 char *current_entry, __u16 level, unsigned int unicode,
Jeff Layton18295792009-04-30 20:45:45 -0400616 struct cifs_sb_info *cifs_sb, unsigned int max_len, __u64 *pinum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 int rc = 0;
619 unsigned int len = 0;
Steve French38702532007-07-08 15:40:40 +0000620 char *filename;
621 struct nls_table *nlt = cifs_sb->local_nls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 *pinum = 0;
624
Steve French790fe572007-07-07 19:25:05 +0000625 if (level == SMB_FIND_FILE_UNIX) {
Steve French38702532007-07-08 15:40:40 +0000626 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 filename = &pFindData->FileName[0];
Steve French790fe572007-07-07 19:25:05 +0000629 if (unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 len = cifs_unicode_bytelen(filename);
631 } else {
632 /* BB should we make this strnlen of PATH_MAX? */
633 len = strnlen(filename, PATH_MAX);
634 }
635
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400636 *pinum = le64_to_cpu(pFindData->basic.UniqueId);
Steve French790fe572007-07-07 19:25:05 +0000637 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000638 FILE_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 (FILE_DIRECTORY_INFO *)current_entry;
640 filename = &pFindData->FileName[0];
641 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000642 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000643 FILE_FULL_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 (FILE_FULL_DIRECTORY_INFO *)current_entry;
645 filename = &pFindData->FileName[0];
646 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000647 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
Steve French38702532007-07-08 15:40:40 +0000648 SEARCH_ID_FULL_DIR_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
650 filename = &pFindData->FileName[0];
651 len = le32_to_cpu(pFindData->FileNameLength);
Steve French85a6dac2009-04-01 05:22:00 +0000652 *pinum = le64_to_cpu(pFindData->UniqueId);
Steve French790fe572007-07-07 19:25:05 +0000653 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000654 FILE_BOTH_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
656 filename = &pFindData->FileName[0];
657 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000658 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000659 FIND_FILE_STANDARD_INFO *pFindData =
Steve French5bafd762006-06-07 00:18:43 +0000660 (FIND_FILE_STANDARD_INFO *)current_entry;
661 filename = &pFindData->FileName[0];
662 /* one byte length, no name conversion */
663 len = (unsigned int)pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 } else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000665 cFYI(1, "Unknown findfirst level %d", level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return -EINVAL;
667 }
Steve French5bafd762006-06-07 00:18:43 +0000668
Steve French790fe572007-07-07 19:25:05 +0000669 if (len > max_len) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000670 cERROR(1, "bad search response length %d past smb end", len);
Steve French5bafd762006-06-07 00:18:43 +0000671 return -EINVAL;
672 }
673
Steve French790fe572007-07-07 19:25:05 +0000674 if (unicode) {
Jeff Laytonf5884162009-04-30 07:18:00 -0400675 pqst->len = cifs_from_ucs2((char *) pqst->name,
676 (__le16 *) filename,
Jeff Layton18295792009-04-30 20:45:45 -0400677 UNICODE_NAME_MAX,
678 min(len, max_len), nlt,
Jeff Laytonf5884162009-04-30 07:18:00 -0400679 cifs_sb->mnt_cifs_flags &
680 CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Laytonf12f98d2010-02-05 13:14:00 -0500681 pqst->len -= nls_nullsize(nlt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 } else {
683 pqst->name = filename;
684 pqst->len = len;
685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return rc;
687}
688
Jeff Layton18295792009-04-30 20:45:45 -0400689static int cifs_filldir(char *pfindEntry, struct file *file, filldir_t filldir,
690 void *direntry, char *scratch_buf, unsigned int max_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 int rc = 0;
693 struct qstr qstring;
Steve French38702532007-07-08 15:40:40 +0000694 struct cifsFileInfo *pCifsF;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400695 u64 inum;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400696 ino_t ino;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400697 struct super_block *sb;
Steve French38702532007-07-08 15:40:40 +0000698 struct cifs_sb_info *cifs_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 struct dentry *tmp_dentry;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400700 struct cifs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 /* get filename and len into qstring */
703 /* get dentry */
704 /* decide whether to create and populate ionde */
Steve French790fe572007-07-07 19:25:05 +0000705 if ((direntry == NULL) || (file == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return -EINVAL;
707
708 pCifsF = file->private_data;
Steve French50c2f752007-07-13 00:33:32 +0000709
Steve French790fe572007-07-07 19:25:05 +0000710 if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return -ENOENT;
712
Steve French38702532007-07-08 15:40:40 +0000713 rc = cifs_entry_is_dot(pfindEntry, pCifsF);
Steve French60808232006-04-22 15:53:05 +0000714 /* skip . and .. since we added them first */
Steve French790fe572007-07-07 19:25:05 +0000715 if (rc != 0)
Steve French60808232006-04-22 15:53:05 +0000716 return 0;
717
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400718 sb = file->f_path.dentry->d_sb;
719 cifs_sb = CIFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 qstring.name = scratch_buf;
Steve French38702532007-07-08 15:40:40 +0000722 rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 pCifsF->srch_inf.info_level,
Steve French38702532007-07-08 15:40:40 +0000724 pCifsF->srch_inf.unicode, cifs_sb,
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400725 max_len, &inum /* returned */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Steve French790fe572007-07-07 19:25:05 +0000727 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return rc;
729
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400730 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400731 cifs_unix_basic_to_fattr(&fattr,
732 &((FILE_UNIX_INFO *) pfindEntry)->basic,
733 cifs_sb);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400734 else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
735 cifs_std_info_to_fattr(&fattr, (FIND_FILE_STANDARD_INFO *)
736 pfindEntry, cifs_sb);
737 else
738 cifs_dir_info_to_fattr(&fattr, (FILE_DIRECTORY_INFO *)
739 pfindEntry, cifs_sb);
Jeff Layton132ac7b2009-02-10 07:33:57 -0500740
Jeff Laytonec06aed2009-11-06 14:18:29 -0500741 if (inum && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400742 fattr.cf_uniqueid = inum;
Jeff Laytonec06aed2009-11-06 14:18:29 -0500743 } else {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400744 fattr.cf_uniqueid = iunique(sb, ROOT_I);
Jeff Laytonec06aed2009-11-06 14:18:29 -0500745 cifs_autodisable_serverino(cifs_sb);
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200748 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) &&
749 CIFSCouldBeMFSymlink(&fattr))
750 /*
751 * trying to get the type and mode can be slow,
752 * so just call those regular files for now, and mark
753 * for reval
754 */
755 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
756
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400757 ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
758 tmp_dentry = cifs_readdir_lookup(file->f_dentry, &qstring, &fattr);
Steve French50c2f752007-07-13 00:33:32 +0000759
Steve French38702532007-07-08 15:40:40 +0000760 rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400761 ino, fattr.cf_dtype);
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 dput(tmp_dentry);
764 return rc;
765}
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
769{
770 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000771 int xid, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 struct cifs_sb_info *cifs_sb;
773 struct cifsTconInfo *pTcon;
774 struct cifsFileInfo *cifsFile = NULL;
Steve French38702532007-07-08 15:40:40 +0000775 char *current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 int num_to_fill = 0;
Steve French38702532007-07-08 15:40:40 +0000777 char *tmp_buf = NULL;
Steve French50c2f752007-07-13 00:33:32 +0000778 char *end_of_smb;
Jeff Layton18295792009-04-30 20:45:45 -0400779 unsigned int max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 xid = GetXid();
782
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800783 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Suresh Jayaraman6221ddd2010-10-01 21:23:33 +0530785 /*
786 * Ensure FindFirst doesn't fail before doing filldir() for '.' and
787 * '..'. Otherwise we won't be able to notify VFS in case of failure.
788 */
789 if (file->private_data == NULL) {
790 rc = initiate_cifs_search(xid, file);
791 cFYI(1, "initiate cifs search rc %d", rc);
792 if (rc)
793 goto rddir2_exit;
794 }
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 switch ((int) file->f_pos) {
797 case 0:
Steve French60808232006-04-22 15:53:05 +0000798 if (filldir(direntry, ".", 1, file->f_pos,
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800799 file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000800 cERROR(1, "Filldir for current dir failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 rc = -ENOMEM;
802 break;
803 }
Steve French60808232006-04-22 15:53:05 +0000804 file->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 case 1:
Steve French60808232006-04-22 15:53:05 +0000806 if (filldir(direntry, "..", 2, file->f_pos,
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800807 file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000808 cERROR(1, "Filldir for parent dir failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 rc = -ENOMEM;
810 break;
811 }
Steve French60808232006-04-22 15:53:05 +0000812 file->f_pos++;
813 default:
Steve French38702532007-07-08 15:40:40 +0000814 /* 1) If search is active,
815 is in current search buffer?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if it before then restart search
817 if after then keep searching till find it */
818
Steve French790fe572007-07-07 19:25:05 +0000819 if (file->private_data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 rc = -EINVAL;
821 FreeXid(xid);
822 return rc;
823 }
824 cifsFile = file->private_data;
825 if (cifsFile->srch_inf.endOfSearch) {
Steve French790fe572007-07-07 19:25:05 +0000826 if (cifsFile->srch_inf.emptyDir) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000827 cFYI(1, "End of search, empty dir");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 rc = 0;
829 break;
830 }
831 } /* else {
Steve French4b18f2a2008-04-29 00:06:05 +0000832 cifsFile->invalidHandle = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 CIFSFindClose(xid, pTcon, cifsFile->netfid);
Steve Frenchaaa9bbe2008-05-23 17:38:32 +0000834 } */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Jeff Layton13cfb732010-09-29 19:51:11 -0400836 pTcon = tlink_tcon(cifsFile->tlink);
Steve French38702532007-07-08 15:40:40 +0000837 rc = find_cifs_entry(xid, pTcon, file,
838 &current_entry, &num_to_fill);
Steve French790fe572007-07-07 19:25:05 +0000839 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000840 cFYI(1, "fce error %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 goto rddir2_exit;
842 } else if (current_entry != NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000843 cFYI(1, "entry %lld found", file->f_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 } else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000845 cFYI(1, "could not find entry");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 goto rddir2_exit;
847 }
Joe Perchesb6b38f72010-04-21 03:50:45 +0000848 cFYI(1, "loop through %d times filling dir for net buf %p",
849 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start);
Steve French5bafd762006-06-07 00:18:43 +0000850 max_len = smbCalcSize((struct smb_hdr *)
851 cifsFile->srch_inf.ntwrk_buf_start);
852 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
853
Jeff Laytonf5884162009-04-30 07:18:00 -0400854 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
Kulikov Vasiliyf55fdcc2010-07-16 20:15:25 +0400855 if (tmp_buf == NULL) {
856 rc = -ENOMEM;
857 break;
858 }
859
Steve French790fe572007-07-07 19:25:05 +0000860 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
861 if (current_entry == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /* evaluate whether this case is an error */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000863 cERROR(1, "past SMB end, num to fill %d i %d",
864 num_to_fill, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 break;
866 }
Steve French60808232006-04-22 15:53:05 +0000867 /* if buggy server returns . and .. late do
868 we want to check for that here? */
Steve French5bafd762006-06-07 00:18:43 +0000869 rc = cifs_filldir(current_entry, file,
870 filldir, direntry, tmp_buf, max_len);
Steve French790fe572007-07-07 19:25:05 +0000871 if (rc == -EOVERFLOW) {
Steve French7ca85ba2006-10-30 21:42:57 +0000872 rc = 0;
873 break;
874 }
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 file->f_pos++;
Steve French790fe572007-07-07 19:25:05 +0000877 if (file->f_pos ==
Steve French39798772006-05-31 22:40:51 +0000878 cifsFile->srch_inf.index_of_last_entry) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000879 cFYI(1, "last entry in buf at pos %lld %s",
880 file->f_pos, tmp_buf);
Steve French790fe572007-07-07 19:25:05 +0000881 cifs_save_resume_key(current_entry, cifsFile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 break;
Steve French790fe572007-07-07 19:25:05 +0000883 } else
884 current_entry =
Steve French5bafd762006-06-07 00:18:43 +0000885 nxt_dir_entry(current_entry, end_of_smb,
886 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888 kfree(tmp_buf);
889 break;
890 } /* end switch */
891
892rddir2_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 FreeXid(xid);
894 return rc;
895}