blob: 170047cf45222cea629c55603524876ec7ad8f8d [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)
82 parent->d_op->d_hash(parent, name);
83 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)
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400106 dentry->d_op = &cifs_ci_dentry_ops;
107 else
108 dentry->d_op = &cifs_dentry_ops;
109
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);
163 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
164 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
165 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400167 cifs_fill_common_info(fattr, cifs_sb);
168}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Steve French15dd4782009-09-25 02:24:45 +0000170static void
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400171cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info,
172 struct cifs_sb_info *cifs_sb)
173{
Jeff Layton0d424ad2010-09-20 16:01:35 -0700174 int offset = cifs_sb_master_tcon(cifs_sb)->ses->server->timeAdj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400176 memset(fattr, 0, sizeof(*fattr));
177 fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate,
178 info->LastAccessTime, offset);
179 fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate,
180 info->LastWriteTime, offset);
181 fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate,
182 info->LastWriteTime, offset);
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800183
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400184 fattr->cf_cifsattrs = le16_to_cpu(info->Attributes);
185 fattr->cf_bytes = le32_to_cpu(info->AllocationSize);
186 fattr->cf_eof = le32_to_cpu(info->DataSize);
Dave Kleikamp273d81d2006-06-01 19:41:23 +0000187
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400188 cifs_fill_common_info(fattr, cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Steve French0e0d2cf2009-05-01 05:27:32 +0000191/* BB eventually need to add the following helper function to
192 resolve NT_STATUS_STOPPED_ON_SYMLINK return code when
193 we try to do FindFirst on (NTFS) directory symlinks */
194/*
195int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
196 int xid)
197{
198 __u16 fid;
199 int len;
200 int oplock = 0;
201 int rc;
Jeff Laytona6e8a842010-09-20 16:01:33 -0700202 struct cifsTconInfo *ptcon = cifs_sb_tcon(cifs_sb);
Steve French0e0d2cf2009-05-01 05:27:32 +0000203 char *tmpbuffer;
204
205 rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ,
206 OPEN_REPARSE_POINT, &fid, &oplock, NULL,
207 cifs_sb->local_nls,
208 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
209 if (!rc) {
210 tmpbuffer = kmalloc(maxpath);
211 rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path,
212 tmpbuffer,
213 maxpath -1,
214 fid,
215 cifs_sb->local_nls);
216 if (CIFSSMBClose(xid, ptcon, fid)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000217 cFYI(1, "Error closing temporary reparsepoint open");
Steve French0e0d2cf2009-05-01 05:27:32 +0000218 }
219 }
220}
221 */
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static int initiate_cifs_search(const int xid, struct file *file)
224{
225 int rc = 0;
Jeff Layton7ffec372010-09-29 19:51:11 -0400226 char *full_path = NULL;
Steve French38702532007-07-08 15:40:40 +0000227 struct cifsFileInfo *cifsFile;
Jeff Layton7ffec372010-09-29 19:51:11 -0400228 struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
229 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 struct cifsTconInfo *pTcon;
231
Jeff Layton7ffec372010-09-29 19:51:11 -0400232 tlink = cifs_sb_tlink(cifs_sb);
233 if (IS_ERR(tlink))
234 return PTR_ERR(tlink);
235 pTcon = tlink_tcon(tlink);
Jeff Laytonba00ba62010-09-20 16:01:31 -0700236
237 if (file->private_data == NULL)
Steve French38702532007-07-08 15:40:40 +0000238 file->private_data =
239 kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
Jeff Layton7ffec372010-09-29 19:51:11 -0400240 if (file->private_data == NULL) {
241 rc = -ENOMEM;
242 goto error_exit;
243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 cifsFile = file->private_data;
Steve French4b18f2a2008-04-29 00:06:05 +0000246 cifsFile->invalidHandle = true;
247 cifsFile->srch_inf.endOfSearch = false;
Jeff Layton7ffec372010-09-29 19:51:11 -0400248 cifsFile->tcon = pTcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800250 full_path = build_path_from_dentry(file->f_path.dentry);
Jeff Layton7ffec372010-09-29 19:51:11 -0400251 if (full_path == NULL) {
252 rc = -ENOMEM;
253 goto error_exit;
254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Joe Perchesb6b38f72010-04-21 03:50:45 +0000256 cFYI(1, "Full path: %s start at: %lld", full_path, file->f_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Steve French75cf6bd2005-04-28 22:41:04 -0700258ffirst_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /* test for Unix extensions */
Steve Frenchc18c8422007-07-18 23:21:09 +0000260 /* but now check for them on the share/mount not on the SMB session */
261/* if (pTcon->ses->capabilities & CAP_UNIX) { */
Steve Frenchad7a2922008-02-07 23:25:02 +0000262 if (pTcon->unix_ext)
Steve French5bafd762006-06-07 00:18:43 +0000263 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
Steve Frenchad7a2922008-02-07 23:25:02 +0000264 else if ((pTcon->ses->capabilities &
Steve French5bafd762006-06-07 00:18:43 +0000265 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
266 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
268 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
269 } else /* not srvinos - BB fixme add check for backlevel? */ {
270 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
271 }
272
Steve French38702532007-07-08 15:40:40 +0000273 rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
Steve French737b7582005-04-28 22:41:06 -0700274 &cifsFile->netfid, &cifsFile->srch_inf,
Steve French38702532007-07-08 15:40:40 +0000275 cifs_sb->mnt_cifs_flags &
Steve Frencheafe8702005-09-15 21:47:30 -0700276 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
Steve French4523cc32007-04-30 20:13:06 +0000277 if (rc == 0)
Steve French4b18f2a2008-04-29 00:06:05 +0000278 cifsFile->invalidHandle = false;
Steve Frenche836f012009-05-01 16:20:35 +0000279 /* BB add following call to handle readdir on new NTFS symlink errors
Steve French0e0d2cf2009-05-01 05:27:32 +0000280 else if STATUS_STOPPED_ON_SYMLINK
281 call get_symlink_reparse_path and retry with new path */
282 else if ((rc == -EOPNOTSUPP) &&
Steve French75cf6bd2005-04-28 22:41:04 -0700283 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
284 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
285 goto ffirst_retry;
286 }
Jeff Layton7ffec372010-09-29 19:51:11 -0400287error_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 kfree(full_path);
Jeff Layton7ffec372010-09-29 19:51:11 -0400289 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return rc;
291}
292
293/* return length of unicode string in bytes */
294static int cifs_unicode_bytelen(char *str)
295{
296 int len;
Steve French63d25832007-11-05 21:46:10 +0000297 __le16 *ustr = (__le16 *)str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Steve French38702532007-07-08 15:40:40 +0000299 for (len = 0; len <= PATH_MAX; len++) {
Steve French4523cc32007-04-30 20:13:06 +0000300 if (ustr[len] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return len << 1;
302 }
Joe Perchesb6b38f72010-04-21 03:50:45 +0000303 cFYI(1, "Unicode string longer than PATH_MAX found");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return len << 1;
305}
306
Steve French5bafd762006-06-07 00:18:43 +0000307static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Steve French38702532007-07-08 15:40:40 +0000309 char *new_entry;
Steve Frenchad7a2922008-02-07 23:25:02 +0000310 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Steve French4523cc32007-04-30 20:13:06 +0000312 if (level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000313 FIND_FILE_STANDARD_INFO *pfData;
Steve French5bafd762006-06-07 00:18:43 +0000314 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
315
316 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
317 pfData->FileNameLength;
318 } else
319 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000320 cFYI(1, "new entry %p old entry %p", new_entry, old_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /* validate that new_entry is not past end of SMB */
Steve French4523cc32007-04-30 20:13:06 +0000322 if (new_entry >= end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000323 cERROR(1, "search entry %p began after end of SMB %p old entry %p",
324 new_entry, end_of_smb, old_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return NULL;
Steve French4523cc32007-04-30 20:13:06 +0000326 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
Steve French38702532007-07-08 15:40:40 +0000327 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
328 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
Steve French5bafd762006-06-07 00:18:43 +0000329 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000330 cERROR(1, "search entry %p extends after end of SMB %p",
331 new_entry, end_of_smb);
Steve French09d1db52005-04-28 22:41:08 -0700332 return NULL;
Steve French38702532007-07-08 15:40:40 +0000333 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return new_entry;
335
336}
337
338#define UNICODE_DOT cpu_to_le16(0x2e)
339
340/* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
341static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
342{
343 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000344 char *filename = NULL;
345 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Steve French4523cc32007-04-30 20:13:06 +0000347 if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000348 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 filename = &pFindData->FileName[0];
Steve French4523cc32007-04-30 20:13:06 +0000350 if (cfile->srch_inf.unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 len = cifs_unicode_bytelen(filename);
352 } else {
353 /* BB should we make this strnlen of PATH_MAX? */
354 len = strnlen(filename, 5);
355 }
Steve French4523cc32007-04-30 20:13:06 +0000356 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000357 FILE_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 (FILE_DIRECTORY_INFO *)current_entry;
359 filename = &pFindData->FileName[0];
360 len = le32_to_cpu(pFindData->FileNameLength);
Steve French38702532007-07-08 15:40:40 +0000361 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000362 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000363 FILE_FULL_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 (FILE_FULL_DIRECTORY_INFO *)current_entry;
365 filename = &pFindData->FileName[0];
366 len = le32_to_cpu(pFindData->FileNameLength);
Steve French4523cc32007-04-30 20:13:06 +0000367 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000368 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000369 SEARCH_ID_FULL_DIR_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
371 filename = &pFindData->FileName[0];
372 len = le32_to_cpu(pFindData->FileNameLength);
Steve French38702532007-07-08 15:40:40 +0000373 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000374 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000375 FILE_BOTH_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
377 filename = &pFindData->FileName[0];
378 len = le32_to_cpu(pFindData->FileNameLength);
Steve French4523cc32007-04-30 20:13:06 +0000379 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000380 FIND_FILE_STANDARD_INFO *pFindData =
Steve French5bafd762006-06-07 00:18:43 +0000381 (FIND_FILE_STANDARD_INFO *)current_entry;
382 filename = &pFindData->FileName[0];
Steve French5ddaa682006-08-15 13:35:48 +0000383 len = pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 } else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000385 cFYI(1, "Unknown findfirst level %d",
386 cfile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
Steve French4523cc32007-04-30 20:13:06 +0000389 if (filename) {
390 if (cfile->srch_inf.unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 __le16 *ufilename = (__le16 *)filename;
Steve French4523cc32007-04-30 20:13:06 +0000392 if (len == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /* check for . */
Steve French4523cc32007-04-30 20:13:06 +0000394 if (ufilename[0] == UNICODE_DOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 rc = 1;
Steve French4523cc32007-04-30 20:13:06 +0000396 } else if (len == 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* check for .. */
Steve French4523cc32007-04-30 20:13:06 +0000398 if ((ufilename[0] == UNICODE_DOT)
Steve French38702532007-07-08 15:40:40 +0000399 && (ufilename[1] == UNICODE_DOT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 rc = 2;
401 }
402 } else /* ASCII */ {
Steve French4523cc32007-04-30 20:13:06 +0000403 if (len == 1) {
Steve French38702532007-07-08 15:40:40 +0000404 if (filename[0] == '.')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 rc = 1;
Steve French4523cc32007-04-30 20:13:06 +0000406 } else if (len == 2) {
Steve French790fe572007-07-07 19:25:05 +0000407 if ((filename[0] == '.') && (filename[1] == '.'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 rc = 2;
409 }
410 }
411 }
412
413 return rc;
414}
415
Steve Frencheafe8702005-09-15 21:47:30 -0700416/* Check if directory that we are searching has changed so we can decide
417 whether we can use the cached search results from the previous search */
Steve French38702532007-07-08 15:40:40 +0000418static int is_dir_changed(struct file *file)
Steve Frencheafe8702005-09-15 21:47:30 -0700419{
Christoph Hellwigc33f8d32007-04-02 18:47:20 +0000420 struct inode *inode = file->f_path.dentry->d_inode;
421 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
Steve Frencheafe8702005-09-15 21:47:30 -0700422
Christoph Hellwigc33f8d32007-04-02 18:47:20 +0000423 if (cifsInfo->time == 0)
Steve Frencheafe8702005-09-15 21:47:30 -0700424 return 1; /* directory was changed, perhaps due to unlink */
425 else
426 return 0;
427
428}
429
Steve French0752f152008-10-07 20:03:33 +0000430static int cifs_save_resume_key(const char *current_entry,
431 struct cifsFileInfo *cifsFile)
432{
433 int rc = 0;
434 unsigned int len = 0;
435 __u16 level;
436 char *filename;
437
438 if ((cifsFile == NULL) || (current_entry == NULL))
439 return -EINVAL;
440
441 level = cifsFile->srch_inf.info_level;
442
443 if (level == SMB_FIND_FILE_UNIX) {
444 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
445
446 filename = &pFindData->FileName[0];
447 if (cifsFile->srch_inf.unicode) {
448 len = cifs_unicode_bytelen(filename);
449 } else {
450 /* BB should we make this strnlen of PATH_MAX? */
451 len = strnlen(filename, PATH_MAX);
452 }
453 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
454 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
455 FILE_DIRECTORY_INFO *pFindData =
456 (FILE_DIRECTORY_INFO *)current_entry;
457 filename = &pFindData->FileName[0];
458 len = le32_to_cpu(pFindData->FileNameLength);
459 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
460 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
461 FILE_FULL_DIRECTORY_INFO *pFindData =
462 (FILE_FULL_DIRECTORY_INFO *)current_entry;
463 filename = &pFindData->FileName[0];
464 len = le32_to_cpu(pFindData->FileNameLength);
465 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
466 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
467 SEARCH_ID_FULL_DIR_INFO *pFindData =
468 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
469 filename = &pFindData->FileName[0];
470 len = le32_to_cpu(pFindData->FileNameLength);
471 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
472 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
473 FILE_BOTH_DIRECTORY_INFO *pFindData =
474 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
475 filename = &pFindData->FileName[0];
476 len = le32_to_cpu(pFindData->FileNameLength);
477 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
478 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
479 FIND_FILE_STANDARD_INFO *pFindData =
480 (FIND_FILE_STANDARD_INFO *)current_entry;
481 filename = &pFindData->FileName[0];
482 /* one byte length, no name conversion */
483 len = (unsigned int)pFindData->FileNameLength;
484 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
485 } else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000486 cFYI(1, "Unknown findfirst level %d", level);
Steve French0752f152008-10-07 20:03:33 +0000487 return -EINVAL;
488 }
489 cifsFile->srch_inf.resume_name_len = len;
490 cifsFile->srch_inf.presume_name = filename;
491 return rc;
492}
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/* find the corresponding entry in the search */
495/* Note that the SMB server returns search entries for . and .. which
496 complicates logic here if we choose to parse for them and we do not
497 assume that they are located in the findfirst return buffer.*/
498/* We start counting in the buffer with entry 2 and increment for every
499 entry (do not increment for . or .. entry) */
500static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
Steve French38702532007-07-08 15:40:40 +0000501 struct file *file, char **ppCurrentEntry, int *num_to_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 int rc = 0;
504 int pos_in_buf = 0;
505 loff_t first_entry_in_buffer;
506 loff_t index_to_find = file->f_pos;
Steve French38702532007-07-08 15:40:40 +0000507 struct cifsFileInfo *cifsFile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /* check if index in the buffer */
Steve French50c2f752007-07-13 00:33:32 +0000509
Steve French38702532007-07-08 15:40:40 +0000510 if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
Steve Frencheafe8702005-09-15 21:47:30 -0700511 (num_to_ret == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return -ENOENT;
Steve French50c2f752007-07-13 00:33:32 +0000513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 *ppCurrentEntry = NULL;
Steve French38702532007-07-08 15:40:40 +0000515 first_entry_in_buffer =
516 cifsFile->srch_inf.index_of_last_entry -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 cifsFile->srch_inf.entries_in_buffer;
Steve French60808232006-04-22 15:53:05 +0000518
519 /* if first entry in buf is zero then is first buffer
520 in search response data which means it is likely . and ..
521 will be in this buffer, although some servers do not return
522 . and .. for the root of a drive and for those we need
523 to start two entries earlier */
524
Steve French39798772006-05-31 22:40:51 +0000525 dump_cifs_file_struct(file, "In fce ");
Steve French38702532007-07-08 15:40:40 +0000526 if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
527 is_dir_changed(file)) ||
Steve Frencheafe8702005-09-15 21:47:30 -0700528 (index_to_find < first_entry_in_buffer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* close and restart search */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000530 cFYI(1, "search backing up - close and restart search");
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000531 write_lock(&GlobalSMBSeslock);
Steve French77c57ec2008-05-13 21:39:32 +0000532 if (!cifsFile->srch_inf.endOfSearch &&
533 !cifsFile->invalidHandle) {
534 cifsFile->invalidHandle = true;
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000535 write_unlock(&GlobalSMBSeslock);
Steve French77c57ec2008-05-13 21:39:32 +0000536 CIFSFindClose(xid, pTcon, cifsFile->netfid);
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000537 } else
538 write_unlock(&GlobalSMBSeslock);
Steve French4523cc32007-04-30 20:13:06 +0000539 if (cifsFile->srch_inf.ntwrk_buf_start) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000540 cFYI(1, "freeing SMB ff cache buf on search rewind");
Steve French4523cc32007-04-30 20:13:06 +0000541 if (cifsFile->srch_inf.smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +0000542 cifs_small_buf_release(cifsFile->srch_inf.
543 ntwrk_buf_start);
544 else
545 cifs_buf_release(cifsFile->srch_inf.
546 ntwrk_buf_start);
Shirish Pargaonkar76c510a2008-07-24 14:48:33 +0000547 cifsFile->srch_inf.ntwrk_buf_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Steve French38702532007-07-08 15:40:40 +0000549 rc = initiate_cifs_search(xid, file);
Steve French4523cc32007-04-30 20:13:06 +0000550 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000551 cFYI(1, "error %d reinitiating a search on rewind",
552 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return rc;
554 }
Jeff Laytona364bc02008-10-21 14:42:13 +0000555 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557
Steve French38702532007-07-08 15:40:40 +0000558 while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
Steve French4b18f2a2008-04-29 00:06:05 +0000559 (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000560 cFYI(1, "calling findnext2");
Steve French38702532007-07-08 15:40:40 +0000561 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
Steve Frenchdfb75332005-06-22 17:13:47 -0700562 &cifsFile->srch_inf);
Jeff Laytona364bc02008-10-21 14:42:13 +0000563 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
Steve French4523cc32007-04-30 20:13:06 +0000564 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return -ENOENT;
566 }
Steve French4523cc32007-04-30 20:13:06 +0000567 if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* we found the buffer that contains the entry */
569 /* scan and find it */
570 int i;
Steve French38702532007-07-08 15:40:40 +0000571 char *current_entry;
572 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 smbCalcSize((struct smb_hdr *)
574 cifsFile->srch_inf.ntwrk_buf_start);
Steve French60808232006-04-22 15:53:05 +0000575
576 current_entry = cifsFile->srch_inf.srch_entries_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
578 - cifsFile->srch_inf.entries_in_buffer;
579 pos_in_buf = index_to_find - first_entry_in_buffer;
Joe Perchesb6b38f72010-04-21 03:50:45 +0000580 cFYI(1, "found entry - pos_in_buf %d", pos_in_buf);
Steve French5bafd762006-06-07 00:18:43 +0000581
Steve Frenchad7a2922008-02-07 23:25:02 +0000582 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
Steve Frenchdfb75332005-06-22 17:13:47 -0700583 /* go entry by entry figuring out which is first */
Steve French38702532007-07-08 15:40:40 +0000584 current_entry = nxt_dir_entry(current_entry, end_of_smb,
Steve French5bafd762006-06-07 00:18:43 +0000585 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Steve French790fe572007-07-07 19:25:05 +0000587 if ((current_entry == NULL) && (i < pos_in_buf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /* BB fixme - check if we should flag this error */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000589 cERROR(1, "reached end of buf searching for pos in buf"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 " %d index to find %lld rc %d",
Joe Perchesb6b38f72010-04-21 03:50:45 +0000591 pos_in_buf, index_to_find, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593 rc = 0;
594 *ppCurrentEntry = current_entry;
595 } else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000596 cFYI(1, "index not in buffer - could not findnext into it");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return 0;
598 }
599
Steve French790fe572007-07-07 19:25:05 +0000600 if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000601 cFYI(1, "can not return entries pos_in_buf beyond last");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 *num_to_ret = 0;
603 } else
604 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 return rc;
607}
608
609/* inode num, inode type and filename returned */
610static int cifs_get_name_from_search_buf(struct qstr *pqst,
611 char *current_entry, __u16 level, unsigned int unicode,
Jeff Layton18295792009-04-30 20:45:45 -0400612 struct cifs_sb_info *cifs_sb, unsigned int max_len, __u64 *pinum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 int rc = 0;
615 unsigned int len = 0;
Steve French38702532007-07-08 15:40:40 +0000616 char *filename;
617 struct nls_table *nlt = cifs_sb->local_nls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 *pinum = 0;
620
Steve French790fe572007-07-07 19:25:05 +0000621 if (level == SMB_FIND_FILE_UNIX) {
Steve French38702532007-07-08 15:40:40 +0000622 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 filename = &pFindData->FileName[0];
Steve French790fe572007-07-07 19:25:05 +0000625 if (unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 len = cifs_unicode_bytelen(filename);
627 } else {
628 /* BB should we make this strnlen of PATH_MAX? */
629 len = strnlen(filename, PATH_MAX);
630 }
631
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400632 *pinum = le64_to_cpu(pFindData->basic.UniqueId);
Steve French790fe572007-07-07 19:25:05 +0000633 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000634 FILE_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 (FILE_DIRECTORY_INFO *)current_entry;
636 filename = &pFindData->FileName[0];
637 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000638 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000639 FILE_FULL_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 (FILE_FULL_DIRECTORY_INFO *)current_entry;
641 filename = &pFindData->FileName[0];
642 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000643 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
Steve French38702532007-07-08 15:40:40 +0000644 SEARCH_ID_FULL_DIR_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
646 filename = &pFindData->FileName[0];
647 len = le32_to_cpu(pFindData->FileNameLength);
Steve French85a6dac2009-04-01 05:22:00 +0000648 *pinum = le64_to_cpu(pFindData->UniqueId);
Steve French790fe572007-07-07 19:25:05 +0000649 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000650 FILE_BOTH_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
652 filename = &pFindData->FileName[0];
653 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000654 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000655 FIND_FILE_STANDARD_INFO *pFindData =
Steve French5bafd762006-06-07 00:18:43 +0000656 (FIND_FILE_STANDARD_INFO *)current_entry;
657 filename = &pFindData->FileName[0];
658 /* one byte length, no name conversion */
659 len = (unsigned int)pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 } else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000661 cFYI(1, "Unknown findfirst level %d", level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return -EINVAL;
663 }
Steve French5bafd762006-06-07 00:18:43 +0000664
Steve French790fe572007-07-07 19:25:05 +0000665 if (len > max_len) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000666 cERROR(1, "bad search response length %d past smb end", len);
Steve French5bafd762006-06-07 00:18:43 +0000667 return -EINVAL;
668 }
669
Steve French790fe572007-07-07 19:25:05 +0000670 if (unicode) {
Jeff Laytonf5884162009-04-30 07:18:00 -0400671 pqst->len = cifs_from_ucs2((char *) pqst->name,
672 (__le16 *) filename,
Jeff Layton18295792009-04-30 20:45:45 -0400673 UNICODE_NAME_MAX,
674 min(len, max_len), nlt,
Jeff Laytonf5884162009-04-30 07:18:00 -0400675 cifs_sb->mnt_cifs_flags &
676 CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Laytonf12f98d2010-02-05 13:14:00 -0500677 pqst->len -= nls_nullsize(nlt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 } else {
679 pqst->name = filename;
680 pqst->len = len;
681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return rc;
683}
684
Jeff Layton18295792009-04-30 20:45:45 -0400685static int cifs_filldir(char *pfindEntry, struct file *file, filldir_t filldir,
686 void *direntry, char *scratch_buf, unsigned int max_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 int rc = 0;
689 struct qstr qstring;
Steve French38702532007-07-08 15:40:40 +0000690 struct cifsFileInfo *pCifsF;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400691 u64 inum;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400692 ino_t ino;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400693 struct super_block *sb;
Steve French38702532007-07-08 15:40:40 +0000694 struct cifs_sb_info *cifs_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct dentry *tmp_dentry;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400696 struct cifs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 /* get filename and len into qstring */
699 /* get dentry */
700 /* decide whether to create and populate ionde */
Steve French790fe572007-07-07 19:25:05 +0000701 if ((direntry == NULL) || (file == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return -EINVAL;
703
704 pCifsF = file->private_data;
Steve French50c2f752007-07-13 00:33:32 +0000705
Steve French790fe572007-07-07 19:25:05 +0000706 if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return -ENOENT;
708
Steve French38702532007-07-08 15:40:40 +0000709 rc = cifs_entry_is_dot(pfindEntry, pCifsF);
Steve French60808232006-04-22 15:53:05 +0000710 /* skip . and .. since we added them first */
Steve French790fe572007-07-07 19:25:05 +0000711 if (rc != 0)
Steve French60808232006-04-22 15:53:05 +0000712 return 0;
713
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400714 sb = file->f_path.dentry->d_sb;
715 cifs_sb = CIFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 qstring.name = scratch_buf;
Steve French38702532007-07-08 15:40:40 +0000718 rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 pCifsF->srch_inf.info_level,
Steve French38702532007-07-08 15:40:40 +0000720 pCifsF->srch_inf.unicode, cifs_sb,
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400721 max_len, &inum /* returned */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Steve French790fe572007-07-07 19:25:05 +0000723 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return rc;
725
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400726 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400727 cifs_unix_basic_to_fattr(&fattr,
728 &((FILE_UNIX_INFO *) pfindEntry)->basic,
729 cifs_sb);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400730 else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
731 cifs_std_info_to_fattr(&fattr, (FIND_FILE_STANDARD_INFO *)
732 pfindEntry, cifs_sb);
733 else
734 cifs_dir_info_to_fattr(&fattr, (FILE_DIRECTORY_INFO *)
735 pfindEntry, cifs_sb);
Jeff Layton132ac7b2009-02-10 07:33:57 -0500736
Jeff Laytonec06aed2009-11-06 14:18:29 -0500737 if (inum && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400738 fattr.cf_uniqueid = inum;
Jeff Laytonec06aed2009-11-06 14:18:29 -0500739 } else {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400740 fattr.cf_uniqueid = iunique(sb, ROOT_I);
Jeff Laytonec06aed2009-11-06 14:18:29 -0500741 cifs_autodisable_serverino(cifs_sb);
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200744 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) &&
745 CIFSCouldBeMFSymlink(&fattr))
746 /*
747 * trying to get the type and mode can be slow,
748 * so just call those regular files for now, and mark
749 * for reval
750 */
751 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
752
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400753 ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
754 tmp_dentry = cifs_readdir_lookup(file->f_dentry, &qstring, &fattr);
Steve French50c2f752007-07-13 00:33:32 +0000755
Steve French38702532007-07-08 15:40:40 +0000756 rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400757 ino, fattr.cf_dtype);
758
759 /*
760 * we can not return filldir errors to the caller since they are
761 * "normal" when the stat blocksize is too small - we return remapped
762 * error instead
763 *
764 * FIXME: This looks bogus. filldir returns -EOVERFLOW in the above
765 * case already. Why should we be clobbering other errors from it?
766 */
Steve French790fe572007-07-07 19:25:05 +0000767 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000768 cFYI(1, "filldir rc = %d", rc);
Steve French7ca85ba2006-10-30 21:42:57 +0000769 rc = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 dput(tmp_dentry);
772 return rc;
773}
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
777{
778 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000779 int xid, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 struct cifs_sb_info *cifs_sb;
781 struct cifsTconInfo *pTcon;
782 struct cifsFileInfo *cifsFile = NULL;
Steve French38702532007-07-08 15:40:40 +0000783 char *current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 int num_to_fill = 0;
Steve French38702532007-07-08 15:40:40 +0000785 char *tmp_buf = NULL;
Steve French50c2f752007-07-13 00:33:32 +0000786 char *end_of_smb;
Jeff Layton18295792009-04-30 20:45:45 -0400787 unsigned int max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 xid = GetXid();
790
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800791 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 switch ((int) file->f_pos) {
794 case 0:
Steve French60808232006-04-22 15:53:05 +0000795 if (filldir(direntry, ".", 1, file->f_pos,
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800796 file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000797 cERROR(1, "Filldir for current dir failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 rc = -ENOMEM;
799 break;
800 }
Steve French60808232006-04-22 15:53:05 +0000801 file->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 case 1:
Steve French60808232006-04-22 15:53:05 +0000803 if (filldir(direntry, "..", 2, file->f_pos,
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800804 file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000805 cERROR(1, "Filldir for parent dir failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 rc = -ENOMEM;
807 break;
808 }
Steve French60808232006-04-22 15:53:05 +0000809 file->f_pos++;
810 default:
Steve French38702532007-07-08 15:40:40 +0000811 /* 1) If search is active,
812 is in current search buffer?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if it before then restart search
814 if after then keep searching till find it */
815
Steve French790fe572007-07-07 19:25:05 +0000816 if (file->private_data == NULL) {
Steve French38702532007-07-08 15:40:40 +0000817 rc = initiate_cifs_search(xid, file);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000818 cFYI(1, "initiate cifs search rc %d", rc);
Steve French790fe572007-07-07 19:25:05 +0000819 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 FreeXid(xid);
821 return rc;
822 }
823 }
Steve French790fe572007-07-07 19:25:05 +0000824 if (file->private_data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 rc = -EINVAL;
826 FreeXid(xid);
827 return rc;
828 }
829 cifsFile = file->private_data;
830 if (cifsFile->srch_inf.endOfSearch) {
Steve French790fe572007-07-07 19:25:05 +0000831 if (cifsFile->srch_inf.emptyDir) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000832 cFYI(1, "End of search, empty dir");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 rc = 0;
834 break;
835 }
836 } /* else {
Steve French4b18f2a2008-04-29 00:06:05 +0000837 cifsFile->invalidHandle = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 CIFSFindClose(xid, pTcon, cifsFile->netfid);
Steve Frenchaaa9bbe2008-05-23 17:38:32 +0000839 } */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Jeff Laytonba00ba62010-09-20 16:01:31 -0700841 pTcon = cifsFile->tcon;
Steve French38702532007-07-08 15:40:40 +0000842 rc = find_cifs_entry(xid, pTcon, file,
843 &current_entry, &num_to_fill);
Steve French790fe572007-07-07 19:25:05 +0000844 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000845 cFYI(1, "fce error %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 goto rddir2_exit;
847 } else if (current_entry != NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000848 cFYI(1, "entry %lld found", file->f_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 } else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000850 cFYI(1, "could not find entry");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 goto rddir2_exit;
852 }
Joe Perchesb6b38f72010-04-21 03:50:45 +0000853 cFYI(1, "loop through %d times filling dir for net buf %p",
854 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start);
Steve French5bafd762006-06-07 00:18:43 +0000855 max_len = smbCalcSize((struct smb_hdr *)
856 cifsFile->srch_inf.ntwrk_buf_start);
857 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
858
Jeff Laytonf5884162009-04-30 07:18:00 -0400859 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
Kulikov Vasiliyf55fdcc2010-07-16 20:15:25 +0400860 if (tmp_buf == NULL) {
861 rc = -ENOMEM;
862 break;
863 }
864
Steve French790fe572007-07-07 19:25:05 +0000865 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
866 if (current_entry == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 /* evaluate whether this case is an error */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000868 cERROR(1, "past SMB end, num to fill %d i %d",
869 num_to_fill, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 break;
871 }
Steve French60808232006-04-22 15:53:05 +0000872 /* if buggy server returns . and .. late do
873 we want to check for that here? */
Steve French5bafd762006-06-07 00:18:43 +0000874 rc = cifs_filldir(current_entry, file,
875 filldir, direntry, tmp_buf, max_len);
Steve French790fe572007-07-07 19:25:05 +0000876 if (rc == -EOVERFLOW) {
Steve French7ca85ba2006-10-30 21:42:57 +0000877 rc = 0;
878 break;
879 }
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 file->f_pos++;
Steve French790fe572007-07-07 19:25:05 +0000882 if (file->f_pos ==
Steve French39798772006-05-31 22:40:51 +0000883 cifsFile->srch_inf.index_of_last_entry) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000884 cFYI(1, "last entry in buf at pos %lld %s",
885 file->f_pos, tmp_buf);
Steve French790fe572007-07-07 19:25:05 +0000886 cifs_save_resume_key(current_entry, cifsFile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 break;
Steve French790fe572007-07-07 19:25:05 +0000888 } else
889 current_entry =
Steve French5bafd762006-06-07 00:18:43 +0000890 nxt_dir_entry(current_entry, end_of_smb,
891 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893 kfree(tmp_buf);
894 break;
895 } /* end switch */
896
897rddir2_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 FreeXid(xid);
899 return rc;
900}