blob: f5618f8cc462912c0e4cd9ed91c9e480ff3fd398 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "cifspdu.h"
27#include "cifsglob.h"
28#include "cifsproto.h"
29#include "cifs_unicode.h"
30#include "cifs_debug.h"
31#include "cifs_fs_sb.h"
32#include "cifsfs.h"
33
Jeff Laytonf5884162009-04-30 07:18:00 -040034/*
35 * To be safe - for UCS to UTF-8 with strings loaded with the rare long
36 * characters alloc more to account for such multibyte target UTF-8
37 * characters.
38 */
39#define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
40
Steve French39798772006-05-31 22:40:51 +000041#ifdef CONFIG_CIFS_DEBUG2
42static void dump_cifs_file_struct(struct file *file, char *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Steve French6dc0f872007-07-06 23:13:06 +000044 struct cifsFileInfo *cf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Steve French4523cc32007-04-30 20:13:06 +000046 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 cf = file->private_data;
Steve French4523cc32007-04-30 20:13:06 +000048 if (cf == NULL) {
Steve French6dc0f872007-07-06 23:13:06 +000049 cFYI(1, ("empty cifs private file data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 return;
51 }
Steve Frenchad7a2922008-02-07 23:25:02 +000052 if (cf->invalidHandle)
Steve French6dc0f872007-07-06 23:13:06 +000053 cFYI(1, ("invalid handle"));
Steve Frenchad7a2922008-02-07 23:25:02 +000054 if (cf->srch_inf.endOfSearch)
Steve French6dc0f872007-07-06 23:13:06 +000055 cFYI(1, ("end of search"));
Steve Frenchad7a2922008-02-07 23:25:02 +000056 if (cf->srch_inf.emptyDir)
Steve French6dc0f872007-07-06 23:13:06 +000057 cFYI(1, ("empty dir"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
Steve French39798772006-05-31 22:40:51 +000059}
Steve French90c81e02008-02-12 20:32:36 +000060#else
61static inline void dump_cifs_file_struct(struct file *file, char *label)
62{
63}
Steve French39798772006-05-31 22:40:51 +000064#endif /* DEBUG2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Jeff Laytoncc0bad72009-06-25 00:56:52 -040066/*
67 * Find the dentry that matches "name". If there isn't one, create one. If it's
68 * a negative dentry or the uniqueid changed, then drop it and recreate it.
69 */
70static struct dentry *
71cifs_readdir_lookup(struct dentry *parent, struct qstr *name,
72 struct cifs_fattr *fattr)
73{
74 struct dentry *dentry, *alias;
75 struct inode *inode;
76 struct super_block *sb = parent->d_inode->i_sb;
77
78 cFYI(1, ("For %s", name->name));
79
80 dentry = d_lookup(parent, name);
81 if (dentry) {
82 /* FIXME: check for inode number changes? */
83 if (dentry->d_inode != NULL)
84 return dentry;
85 d_drop(dentry);
86 dput(dentry);
87 }
88
89 dentry = d_alloc(parent, name);
90 if (dentry == NULL)
91 return NULL;
92
93 inode = cifs_iget(sb, fattr);
94 if (!inode) {
95 dput(dentry);
96 return NULL;
97 }
98
99 if (CIFS_SB(sb)->tcon->nocase)
100 dentry->d_op = &cifs_ci_dentry_ops;
101 else
102 dentry->d_op = &cifs_dentry_ops;
103
104 alias = d_materialise_unique(dentry, inode);
105 if (alias != NULL) {
106 dput(dentry);
107 if (IS_ERR(alias))
108 return NULL;
109 dentry = alias;
110 }
111
112 return dentry;
113}
114
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400115static void
116cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400118 fattr->cf_uid = cifs_sb->mnt_uid;
119 fattr->cf_gid = cifs_sb->mnt_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400121 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
122 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
123 fattr->cf_dtype = DT_DIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 } else {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400125 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
126 fattr->cf_dtype = DT_REG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
128
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400129 if (fattr->cf_cifsattrs & ATTR_READONLY)
130 fattr->cf_mode &= ~S_IWUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400132 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
133 fattr->cf_cifsattrs & ATTR_SYSTEM) {
134 if (fattr->cf_eof == 0) {
135 fattr->cf_mode &= ~S_IFMT;
136 fattr->cf_mode |= S_IFIFO;
137 fattr->cf_dtype = DT_FIFO;
Steve French3020a1f2005-11-18 11:31:10 -0800138 } else {
Jeff Layton4468eb32008-05-22 09:31:40 -0400139 /*
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400140 * trying to get the type and mode via SFU can be slow,
141 * so just call those regular files for now, and mark
142 * for reval
Jeff Layton4468eb32008-05-22 09:31:40 -0400143 */
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400144 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
Jeff Layton4468eb32008-05-22 09:31:40 -0400145 }
146 }
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400147}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Steve French15dd4782009-09-25 02:24:45 +0000149static void
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400150cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info,
151 struct cifs_sb_info *cifs_sb)
152{
153 memset(fattr, 0, sizeof(*fattr));
154 fattr->cf_cifsattrs = le32_to_cpu(info->ExtFileAttributes);
155 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
156 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
157 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
158 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
159 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400161 cifs_fill_common_info(fattr, cifs_sb);
162}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Steve French15dd4782009-09-25 02:24:45 +0000164static void
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400165cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info,
166 struct cifs_sb_info *cifs_sb)
167{
168 int offset = cifs_sb->tcon->ses->server->timeAdj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400170 memset(fattr, 0, sizeof(*fattr));
171 fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate,
172 info->LastAccessTime, offset);
173 fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate,
174 info->LastWriteTime, offset);
175 fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate,
176 info->LastWriteTime, offset);
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800177
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400178 fattr->cf_cifsattrs = le16_to_cpu(info->Attributes);
179 fattr->cf_bytes = le32_to_cpu(info->AllocationSize);
180 fattr->cf_eof = le32_to_cpu(info->DataSize);
Dave Kleikamp273d81d2006-06-01 19:41:23 +0000181
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400182 cifs_fill_common_info(fattr, cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Steve French0e0d2cf2009-05-01 05:27:32 +0000185/* BB eventually need to add the following helper function to
186 resolve NT_STATUS_STOPPED_ON_SYMLINK return code when
187 we try to do FindFirst on (NTFS) directory symlinks */
188/*
189int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
190 int xid)
191{
192 __u16 fid;
193 int len;
194 int oplock = 0;
195 int rc;
196 struct cifsTconInfo *ptcon = cifs_sb->tcon;
197 char *tmpbuffer;
198
199 rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ,
200 OPEN_REPARSE_POINT, &fid, &oplock, NULL,
201 cifs_sb->local_nls,
202 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
203 if (!rc) {
204 tmpbuffer = kmalloc(maxpath);
205 rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path,
206 tmpbuffer,
207 maxpath -1,
208 fid,
209 cifs_sb->local_nls);
210 if (CIFSSMBClose(xid, ptcon, fid)) {
211 cFYI(1, ("Error closing temporary reparsepoint open)"));
212 }
213 }
214}
215 */
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static int initiate_cifs_search(const int xid, struct file *file)
218{
219 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000220 char *full_path;
221 struct cifsFileInfo *cifsFile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 struct cifs_sb_info *cifs_sb;
223 struct cifsTconInfo *pTcon;
224
Steve French4523cc32007-04-30 20:13:06 +0000225 if (file->private_data == NULL) {
Steve French38702532007-07-08 15:40:40 +0000226 file->private_data =
227 kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229
Steve French4523cc32007-04-30 20:13:06 +0000230 if (file->private_data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 cifsFile = file->private_data;
Steve French4b18f2a2008-04-29 00:06:05 +0000233 cifsFile->invalidHandle = true;
234 cifsFile->srch_inf.endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800236 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
Steve French4523cc32007-04-30 20:13:06 +0000237 if (cifs_sb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return -EINVAL;
239
240 pTcon = cifs_sb->tcon;
Steve French4523cc32007-04-30 20:13:06 +0000241 if (pTcon == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return -EINVAL;
243
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800244 full_path = build_path_from_dentry(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Steve Frenchad7a2922008-02-07 23:25:02 +0000246 if (full_path == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Steve French966ca922005-04-28 22:41:08 -0700249 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Steve French75cf6bd2005-04-28 22:41:04 -0700251ffirst_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /* test for Unix extensions */
Steve Frenchc18c8422007-07-18 23:21:09 +0000253 /* but now check for them on the share/mount not on the SMB session */
254/* if (pTcon->ses->capabilities & CAP_UNIX) { */
Steve Frenchad7a2922008-02-07 23:25:02 +0000255 if (pTcon->unix_ext)
Steve French5bafd762006-06-07 00:18:43 +0000256 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
Steve Frenchad7a2922008-02-07 23:25:02 +0000257 else if ((pTcon->ses->capabilities &
Steve French5bafd762006-06-07 00:18:43 +0000258 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
259 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
261 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
262 } else /* not srvinos - BB fixme add check for backlevel? */ {
263 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
264 }
265
Steve French38702532007-07-08 15:40:40 +0000266 rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
Steve French737b7582005-04-28 22:41:06 -0700267 &cifsFile->netfid, &cifsFile->srch_inf,
Steve French38702532007-07-08 15:40:40 +0000268 cifs_sb->mnt_cifs_flags &
Steve Frencheafe8702005-09-15 21:47:30 -0700269 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
Steve French4523cc32007-04-30 20:13:06 +0000270 if (rc == 0)
Steve French4b18f2a2008-04-29 00:06:05 +0000271 cifsFile->invalidHandle = false;
Steve Frenche836f012009-05-01 16:20:35 +0000272 /* BB add following call to handle readdir on new NTFS symlink errors
Steve French0e0d2cf2009-05-01 05:27:32 +0000273 else if STATUS_STOPPED_ON_SYMLINK
274 call get_symlink_reparse_path and retry with new path */
275 else if ((rc == -EOPNOTSUPP) &&
Steve French75cf6bd2005-04-28 22:41:04 -0700276 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
277 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
278 goto ffirst_retry;
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 kfree(full_path);
281 return rc;
282}
283
284/* return length of unicode string in bytes */
285static int cifs_unicode_bytelen(char *str)
286{
287 int len;
Steve French63d25832007-11-05 21:46:10 +0000288 __le16 *ustr = (__le16 *)str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Steve French38702532007-07-08 15:40:40 +0000290 for (len = 0; len <= PATH_MAX; len++) {
Steve French4523cc32007-04-30 20:13:06 +0000291 if (ustr[len] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return len << 1;
293 }
Steve French790fe572007-07-07 19:25:05 +0000294 cFYI(1, ("Unicode string longer than PATH_MAX found"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return len << 1;
296}
297
Steve French5bafd762006-06-07 00:18:43 +0000298static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Steve French38702532007-07-08 15:40:40 +0000300 char *new_entry;
Steve Frenchad7a2922008-02-07 23:25:02 +0000301 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Steve French4523cc32007-04-30 20:13:06 +0000303 if (level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000304 FIND_FILE_STANDARD_INFO *pfData;
Steve French5bafd762006-06-07 00:18:43 +0000305 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
306
307 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
308 pfData->FileNameLength;
309 } else
310 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
Steve French790fe572007-07-07 19:25:05 +0000311 cFYI(1, ("new entry %p old entry %p", new_entry, old_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 /* validate that new_entry is not past end of SMB */
Steve French4523cc32007-04-30 20:13:06 +0000313 if (new_entry >= end_of_smb) {
Steve French09d1db52005-04-28 22:41:08 -0700314 cERROR(1,
315 ("search entry %p began after end of SMB %p old entry %p",
Steve French38702532007-07-08 15:40:40 +0000316 new_entry, end_of_smb, old_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return NULL;
Steve French4523cc32007-04-30 20:13:06 +0000318 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
Steve French38702532007-07-08 15:40:40 +0000319 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
320 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
Steve French5bafd762006-06-07 00:18:43 +0000321 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
Steve French38702532007-07-08 15:40:40 +0000322 cERROR(1, ("search entry %p extends after end of SMB %p",
Steve French09d1db52005-04-28 22:41:08 -0700323 new_entry, end_of_smb));
324 return NULL;
Steve French38702532007-07-08 15:40:40 +0000325 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return new_entry;
327
328}
329
330#define UNICODE_DOT cpu_to_le16(0x2e)
331
332/* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
333static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
334{
335 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000336 char *filename = NULL;
337 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Steve French4523cc32007-04-30 20:13:06 +0000339 if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000340 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 filename = &pFindData->FileName[0];
Steve French4523cc32007-04-30 20:13:06 +0000342 if (cfile->srch_inf.unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 len = cifs_unicode_bytelen(filename);
344 } else {
345 /* BB should we make this strnlen of PATH_MAX? */
346 len = strnlen(filename, 5);
347 }
Steve French4523cc32007-04-30 20:13:06 +0000348 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000349 FILE_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 (FILE_DIRECTORY_INFO *)current_entry;
351 filename = &pFindData->FileName[0];
352 len = le32_to_cpu(pFindData->FileNameLength);
Steve French38702532007-07-08 15:40:40 +0000353 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000354 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000355 FILE_FULL_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 (FILE_FULL_DIRECTORY_INFO *)current_entry;
357 filename = &pFindData->FileName[0];
358 len = le32_to_cpu(pFindData->FileNameLength);
Steve French4523cc32007-04-30 20:13:06 +0000359 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000360 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000361 SEARCH_ID_FULL_DIR_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 (SEARCH_ID_FULL_DIR_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_BOTH_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000367 FILE_BOTH_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 (FILE_BOTH_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 == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000372 FIND_FILE_STANDARD_INFO *pFindData =
Steve French5bafd762006-06-07 00:18:43 +0000373 (FIND_FILE_STANDARD_INFO *)current_entry;
374 filename = &pFindData->FileName[0];
Steve French5ddaa682006-08-15 13:35:48 +0000375 len = pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 } else {
Steve French790fe572007-07-07 19:25:05 +0000377 cFYI(1, ("Unknown findfirst level %d",
378 cfile->srch_inf.info_level));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
Steve French4523cc32007-04-30 20:13:06 +0000381 if (filename) {
382 if (cfile->srch_inf.unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 __le16 *ufilename = (__le16 *)filename;
Steve French4523cc32007-04-30 20:13:06 +0000384 if (len == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 /* check for . */
Steve French4523cc32007-04-30 20:13:06 +0000386 if (ufilename[0] == UNICODE_DOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 rc = 1;
Steve French4523cc32007-04-30 20:13:06 +0000388 } else if (len == 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /* check for .. */
Steve French4523cc32007-04-30 20:13:06 +0000390 if ((ufilename[0] == UNICODE_DOT)
Steve French38702532007-07-08 15:40:40 +0000391 && (ufilename[1] == UNICODE_DOT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 rc = 2;
393 }
394 } else /* ASCII */ {
Steve French4523cc32007-04-30 20:13:06 +0000395 if (len == 1) {
Steve French38702532007-07-08 15:40:40 +0000396 if (filename[0] == '.')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 rc = 1;
Steve French4523cc32007-04-30 20:13:06 +0000398 } else if (len == 2) {
Steve French790fe572007-07-07 19:25:05 +0000399 if ((filename[0] == '.') && (filename[1] == '.'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 rc = 2;
401 }
402 }
403 }
404
405 return rc;
406}
407
Steve Frencheafe8702005-09-15 21:47:30 -0700408/* Check if directory that we are searching has changed so we can decide
409 whether we can use the cached search results from the previous search */
Steve French38702532007-07-08 15:40:40 +0000410static int is_dir_changed(struct file *file)
Steve Frencheafe8702005-09-15 21:47:30 -0700411{
Christoph Hellwigc33f8d32007-04-02 18:47:20 +0000412 struct inode *inode = file->f_path.dentry->d_inode;
413 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
Steve Frencheafe8702005-09-15 21:47:30 -0700414
Christoph Hellwigc33f8d32007-04-02 18:47:20 +0000415 if (cifsInfo->time == 0)
Steve Frencheafe8702005-09-15 21:47:30 -0700416 return 1; /* directory was changed, perhaps due to unlink */
417 else
418 return 0;
419
420}
421
Steve French0752f152008-10-07 20:03:33 +0000422static int cifs_save_resume_key(const char *current_entry,
423 struct cifsFileInfo *cifsFile)
424{
425 int rc = 0;
426 unsigned int len = 0;
427 __u16 level;
428 char *filename;
429
430 if ((cifsFile == NULL) || (current_entry == NULL))
431 return -EINVAL;
432
433 level = cifsFile->srch_inf.info_level;
434
435 if (level == SMB_FIND_FILE_UNIX) {
436 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
437
438 filename = &pFindData->FileName[0];
439 if (cifsFile->srch_inf.unicode) {
440 len = cifs_unicode_bytelen(filename);
441 } else {
442 /* BB should we make this strnlen of PATH_MAX? */
443 len = strnlen(filename, PATH_MAX);
444 }
445 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
446 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
447 FILE_DIRECTORY_INFO *pFindData =
448 (FILE_DIRECTORY_INFO *)current_entry;
449 filename = &pFindData->FileName[0];
450 len = le32_to_cpu(pFindData->FileNameLength);
451 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
452 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
453 FILE_FULL_DIRECTORY_INFO *pFindData =
454 (FILE_FULL_DIRECTORY_INFO *)current_entry;
455 filename = &pFindData->FileName[0];
456 len = le32_to_cpu(pFindData->FileNameLength);
457 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
458 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
459 SEARCH_ID_FULL_DIR_INFO *pFindData =
460 (SEARCH_ID_FULL_DIR_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_BOTH_DIRECTORY_INFO) {
465 FILE_BOTH_DIRECTORY_INFO *pFindData =
466 (FILE_BOTH_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_INFO_STANDARD) {
471 FIND_FILE_STANDARD_INFO *pFindData =
472 (FIND_FILE_STANDARD_INFO *)current_entry;
473 filename = &pFindData->FileName[0];
474 /* one byte length, no name conversion */
475 len = (unsigned int)pFindData->FileNameLength;
476 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
477 } else {
478 cFYI(1, ("Unknown findfirst level %d", level));
479 return -EINVAL;
480 }
481 cifsFile->srch_inf.resume_name_len = len;
482 cifsFile->srch_inf.presume_name = filename;
483 return rc;
484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/* find the corresponding entry in the search */
487/* Note that the SMB server returns search entries for . and .. which
488 complicates logic here if we choose to parse for them and we do not
489 assume that they are located in the findfirst return buffer.*/
490/* We start counting in the buffer with entry 2 and increment for every
491 entry (do not increment for . or .. entry) */
492static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
Steve French38702532007-07-08 15:40:40 +0000493 struct file *file, char **ppCurrentEntry, int *num_to_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 int rc = 0;
496 int pos_in_buf = 0;
497 loff_t first_entry_in_buffer;
498 loff_t index_to_find = file->f_pos;
Steve French38702532007-07-08 15:40:40 +0000499 struct cifsFileInfo *cifsFile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* check if index in the buffer */
Steve French50c2f752007-07-13 00:33:32 +0000501
Steve French38702532007-07-08 15:40:40 +0000502 if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
Steve Frencheafe8702005-09-15 21:47:30 -0700503 (num_to_ret == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return -ENOENT;
Steve French50c2f752007-07-13 00:33:32 +0000505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 *ppCurrentEntry = NULL;
Steve French38702532007-07-08 15:40:40 +0000507 first_entry_in_buffer =
508 cifsFile->srch_inf.index_of_last_entry -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 cifsFile->srch_inf.entries_in_buffer;
Steve French60808232006-04-22 15:53:05 +0000510
511 /* if first entry in buf is zero then is first buffer
512 in search response data which means it is likely . and ..
513 will be in this buffer, although some servers do not return
514 . and .. for the root of a drive and for those we need
515 to start two entries earlier */
516
Steve French39798772006-05-31 22:40:51 +0000517 dump_cifs_file_struct(file, "In fce ");
Steve French38702532007-07-08 15:40:40 +0000518 if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
519 is_dir_changed(file)) ||
Steve Frencheafe8702005-09-15 21:47:30 -0700520 (index_to_find < first_entry_in_buffer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* close and restart search */
Steve French790fe572007-07-07 19:25:05 +0000522 cFYI(1, ("search backing up - close and restart search"));
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000523 write_lock(&GlobalSMBSeslock);
Steve French77c57ec2008-05-13 21:39:32 +0000524 if (!cifsFile->srch_inf.endOfSearch &&
525 !cifsFile->invalidHandle) {
526 cifsFile->invalidHandle = true;
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000527 write_unlock(&GlobalSMBSeslock);
Steve French77c57ec2008-05-13 21:39:32 +0000528 CIFSFindClose(xid, pTcon, cifsFile->netfid);
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000529 } else
530 write_unlock(&GlobalSMBSeslock);
Steve French4523cc32007-04-30 20:13:06 +0000531 if (cifsFile->srch_inf.ntwrk_buf_start) {
Steve French790fe572007-07-07 19:25:05 +0000532 cFYI(1, ("freeing SMB ff cache buf on search rewind"));
Steve French4523cc32007-04-30 20:13:06 +0000533 if (cifsFile->srch_inf.smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +0000534 cifs_small_buf_release(cifsFile->srch_inf.
535 ntwrk_buf_start);
536 else
537 cifs_buf_release(cifsFile->srch_inf.
538 ntwrk_buf_start);
Shirish Pargaonkar76c510a2008-07-24 14:48:33 +0000539 cifsFile->srch_inf.ntwrk_buf_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Steve French38702532007-07-08 15:40:40 +0000541 rc = initiate_cifs_search(xid, file);
Steve French4523cc32007-04-30 20:13:06 +0000542 if (rc) {
Steve French790fe572007-07-07 19:25:05 +0000543 cFYI(1, ("error %d reinitiating a search on rewind",
544 rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return rc;
546 }
Jeff Laytona364bc02008-10-21 14:42:13 +0000547 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549
Steve French38702532007-07-08 15:40:40 +0000550 while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
Steve French4b18f2a2008-04-29 00:06:05 +0000551 (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
Steve French790fe572007-07-07 19:25:05 +0000552 cFYI(1, ("calling findnext2"));
Steve French38702532007-07-08 15:40:40 +0000553 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
Steve Frenchdfb75332005-06-22 17:13:47 -0700554 &cifsFile->srch_inf);
Jeff Laytona364bc02008-10-21 14:42:13 +0000555 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
Steve French4523cc32007-04-30 20:13:06 +0000556 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return -ENOENT;
558 }
Steve French4523cc32007-04-30 20:13:06 +0000559 if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /* we found the buffer that contains the entry */
561 /* scan and find it */
562 int i;
Steve French38702532007-07-08 15:40:40 +0000563 char *current_entry;
564 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 smbCalcSize((struct smb_hdr *)
566 cifsFile->srch_inf.ntwrk_buf_start);
Steve French60808232006-04-22 15:53:05 +0000567
568 current_entry = cifsFile->srch_inf.srch_entries_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
570 - cifsFile->srch_inf.entries_in_buffer;
571 pos_in_buf = index_to_find - first_entry_in_buffer;
Steve French790fe572007-07-07 19:25:05 +0000572 cFYI(1, ("found entry - pos_in_buf %d", pos_in_buf));
Steve French5bafd762006-06-07 00:18:43 +0000573
Steve Frenchad7a2922008-02-07 23:25:02 +0000574 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
Steve Frenchdfb75332005-06-22 17:13:47 -0700575 /* go entry by entry figuring out which is first */
Steve French38702532007-07-08 15:40:40 +0000576 current_entry = nxt_dir_entry(current_entry, end_of_smb,
Steve French5bafd762006-06-07 00:18:43 +0000577 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Steve French790fe572007-07-07 19:25:05 +0000579 if ((current_entry == NULL) && (i < pos_in_buf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /* BB fixme - check if we should flag this error */
Steve French38702532007-07-08 15:40:40 +0000581 cERROR(1, ("reached end of buf searching for pos in buf"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 " %d index to find %lld rc %d",
Steve French38702532007-07-08 15:40:40 +0000583 pos_in_buf, index_to_find, rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585 rc = 0;
586 *ppCurrentEntry = current_entry;
587 } else {
Steve French790fe572007-07-07 19:25:05 +0000588 cFYI(1, ("index not in buffer - could not findnext into it"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return 0;
590 }
591
Steve French790fe572007-07-07 19:25:05 +0000592 if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
593 cFYI(1, ("can not return entries pos_in_buf beyond last"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 *num_to_ret = 0;
595 } else
596 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 return rc;
599}
600
601/* inode num, inode type and filename returned */
602static int cifs_get_name_from_search_buf(struct qstr *pqst,
603 char *current_entry, __u16 level, unsigned int unicode,
Jeff Layton18295792009-04-30 20:45:45 -0400604 struct cifs_sb_info *cifs_sb, unsigned int max_len, __u64 *pinum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 int rc = 0;
607 unsigned int len = 0;
Steve French38702532007-07-08 15:40:40 +0000608 char *filename;
609 struct nls_table *nlt = cifs_sb->local_nls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 *pinum = 0;
612
Steve French790fe572007-07-07 19:25:05 +0000613 if (level == SMB_FIND_FILE_UNIX) {
Steve French38702532007-07-08 15:40:40 +0000614 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 filename = &pFindData->FileName[0];
Steve French790fe572007-07-07 19:25:05 +0000617 if (unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 len = cifs_unicode_bytelen(filename);
619 } else {
620 /* BB should we make this strnlen of PATH_MAX? */
621 len = strnlen(filename, PATH_MAX);
622 }
623
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400624 *pinum = le64_to_cpu(pFindData->basic.UniqueId);
Steve French790fe572007-07-07 19:25:05 +0000625 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000626 FILE_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 (FILE_DIRECTORY_INFO *)current_entry;
628 filename = &pFindData->FileName[0];
629 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000630 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000631 FILE_FULL_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 (FILE_FULL_DIRECTORY_INFO *)current_entry;
633 filename = &pFindData->FileName[0];
634 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000635 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
Steve French38702532007-07-08 15:40:40 +0000636 SEARCH_ID_FULL_DIR_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
638 filename = &pFindData->FileName[0];
639 len = le32_to_cpu(pFindData->FileNameLength);
Steve French85a6dac2009-04-01 05:22:00 +0000640 *pinum = le64_to_cpu(pFindData->UniqueId);
Steve French790fe572007-07-07 19:25:05 +0000641 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000642 FILE_BOTH_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
644 filename = &pFindData->FileName[0];
645 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000646 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000647 FIND_FILE_STANDARD_INFO *pFindData =
Steve French5bafd762006-06-07 00:18:43 +0000648 (FIND_FILE_STANDARD_INFO *)current_entry;
649 filename = &pFindData->FileName[0];
650 /* one byte length, no name conversion */
651 len = (unsigned int)pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 } else {
Steve French790fe572007-07-07 19:25:05 +0000653 cFYI(1, ("Unknown findfirst level %d", level));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return -EINVAL;
655 }
Steve French5bafd762006-06-07 00:18:43 +0000656
Steve French790fe572007-07-07 19:25:05 +0000657 if (len > max_len) {
Steve French38702532007-07-08 15:40:40 +0000658 cERROR(1, ("bad search response length %d past smb end", len));
Steve French5bafd762006-06-07 00:18:43 +0000659 return -EINVAL;
660 }
661
Steve French790fe572007-07-07 19:25:05 +0000662 if (unicode) {
Jeff Laytonf5884162009-04-30 07:18:00 -0400663 pqst->len = cifs_from_ucs2((char *) pqst->name,
664 (__le16 *) filename,
Jeff Layton18295792009-04-30 20:45:45 -0400665 UNICODE_NAME_MAX,
666 min(len, max_len), nlt,
Jeff Laytonf5884162009-04-30 07:18:00 -0400667 cifs_sb->mnt_cifs_flags &
668 CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Laytonf12f98d2010-02-05 13:14:00 -0500669 pqst->len -= nls_nullsize(nlt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 } else {
671 pqst->name = filename;
672 pqst->len = len;
673 }
Steve French38702532007-07-08 15:40:40 +0000674 pqst->hash = full_name_hash(pqst->name, pqst->len);
Steve French790fe572007-07-07 19:25:05 +0000675/* cFYI(1, ("filldir on %s",pqst->name)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return rc;
677}
678
Jeff Layton18295792009-04-30 20:45:45 -0400679static int cifs_filldir(char *pfindEntry, struct file *file, filldir_t filldir,
680 void *direntry, char *scratch_buf, unsigned int max_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 int rc = 0;
683 struct qstr qstring;
Steve French38702532007-07-08 15:40:40 +0000684 struct cifsFileInfo *pCifsF;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400685 u64 inum;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400686 ino_t ino;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400687 struct super_block *sb;
Steve French38702532007-07-08 15:40:40 +0000688 struct cifs_sb_info *cifs_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct dentry *tmp_dentry;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400690 struct cifs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 /* get filename and len into qstring */
693 /* get dentry */
694 /* decide whether to create and populate ionde */
Steve French790fe572007-07-07 19:25:05 +0000695 if ((direntry == NULL) || (file == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return -EINVAL;
697
698 pCifsF = file->private_data;
Steve French50c2f752007-07-13 00:33:32 +0000699
Steve French790fe572007-07-07 19:25:05 +0000700 if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return -ENOENT;
702
Steve French38702532007-07-08 15:40:40 +0000703 rc = cifs_entry_is_dot(pfindEntry, pCifsF);
Steve French60808232006-04-22 15:53:05 +0000704 /* skip . and .. since we added them first */
Steve French790fe572007-07-07 19:25:05 +0000705 if (rc != 0)
Steve French60808232006-04-22 15:53:05 +0000706 return 0;
707
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400708 sb = file->f_path.dentry->d_sb;
709 cifs_sb = CIFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 qstring.name = scratch_buf;
Steve French38702532007-07-08 15:40:40 +0000712 rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 pCifsF->srch_inf.info_level,
Steve French38702532007-07-08 15:40:40 +0000714 pCifsF->srch_inf.unicode, cifs_sb,
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400715 max_len, &inum /* returned */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Steve French790fe572007-07-07 19:25:05 +0000717 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return rc;
719
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400720 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400721 cifs_unix_basic_to_fattr(&fattr,
722 &((FILE_UNIX_INFO *) pfindEntry)->basic,
723 cifs_sb);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400724 else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
725 cifs_std_info_to_fattr(&fattr, (FIND_FILE_STANDARD_INFO *)
726 pfindEntry, cifs_sb);
727 else
728 cifs_dir_info_to_fattr(&fattr, (FILE_DIRECTORY_INFO *)
729 pfindEntry, cifs_sb);
Jeff Layton132ac7b2009-02-10 07:33:57 -0500730
Jeff Laytonec06aed2009-11-06 14:18:29 -0500731 if (inum && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400732 fattr.cf_uniqueid = inum;
Jeff Laytonec06aed2009-11-06 14:18:29 -0500733 } else {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400734 fattr.cf_uniqueid = iunique(sb, ROOT_I);
Jeff Laytonec06aed2009-11-06 14:18:29 -0500735 cifs_autodisable_serverino(cifs_sb);
736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400738 ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
739 tmp_dentry = cifs_readdir_lookup(file->f_dentry, &qstring, &fattr);
Steve French50c2f752007-07-13 00:33:32 +0000740
Steve French38702532007-07-08 15:40:40 +0000741 rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400742 ino, fattr.cf_dtype);
743
744 /*
745 * we can not return filldir errors to the caller since they are
746 * "normal" when the stat blocksize is too small - we return remapped
747 * error instead
748 *
749 * FIXME: This looks bogus. filldir returns -EOVERFLOW in the above
750 * case already. Why should we be clobbering other errors from it?
751 */
Steve French790fe572007-07-07 19:25:05 +0000752 if (rc) {
753 cFYI(1, ("filldir rc = %d", rc));
Steve French7ca85ba2006-10-30 21:42:57 +0000754 rc = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 dput(tmp_dentry);
757 return rc;
758}
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
762{
763 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000764 int xid, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 struct cifs_sb_info *cifs_sb;
766 struct cifsTconInfo *pTcon;
767 struct cifsFileInfo *cifsFile = NULL;
Steve French38702532007-07-08 15:40:40 +0000768 char *current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int num_to_fill = 0;
Steve French38702532007-07-08 15:40:40 +0000770 char *tmp_buf = NULL;
Steve French50c2f752007-07-13 00:33:32 +0000771 char *end_of_smb;
Jeff Layton18295792009-04-30 20:45:45 -0400772 unsigned int max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 xid = GetXid();
775
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800776 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 pTcon = cifs_sb->tcon;
Steve French790fe572007-07-07 19:25:05 +0000778 if (pTcon == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return -EINVAL;
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 switch ((int) file->f_pos) {
782 case 0:
Steve French60808232006-04-22 15:53:05 +0000783 if (filldir(direntry, ".", 1, file->f_pos,
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800784 file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
Steve French60808232006-04-22 15:53:05 +0000785 cERROR(1, ("Filldir for current dir failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 rc = -ENOMEM;
787 break;
788 }
Steve French60808232006-04-22 15:53:05 +0000789 file->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 case 1:
Steve French60808232006-04-22 15:53:05 +0000791 if (filldir(direntry, "..", 2, file->f_pos,
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800792 file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
Steve French26a21b92006-05-31 18:05:34 +0000793 cERROR(1, ("Filldir for parent dir failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 rc = -ENOMEM;
795 break;
796 }
Steve French60808232006-04-22 15:53:05 +0000797 file->f_pos++;
798 default:
Steve French38702532007-07-08 15:40:40 +0000799 /* 1) If search is active,
800 is in current search buffer?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if it before then restart search
802 if after then keep searching till find it */
803
Steve French790fe572007-07-07 19:25:05 +0000804 if (file->private_data == NULL) {
Steve French38702532007-07-08 15:40:40 +0000805 rc = initiate_cifs_search(xid, file);
Steve French790fe572007-07-07 19:25:05 +0000806 cFYI(1, ("initiate cifs search rc %d", rc));
807 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 FreeXid(xid);
809 return rc;
810 }
811 }
Steve French790fe572007-07-07 19:25:05 +0000812 if (file->private_data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 rc = -EINVAL;
814 FreeXid(xid);
815 return rc;
816 }
817 cifsFile = file->private_data;
818 if (cifsFile->srch_inf.endOfSearch) {
Steve French790fe572007-07-07 19:25:05 +0000819 if (cifsFile->srch_inf.emptyDir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 cFYI(1, ("End of search, empty dir"));
821 rc = 0;
822 break;
823 }
824 } /* else {
Steve French4b18f2a2008-04-29 00:06:05 +0000825 cifsFile->invalidHandle = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 CIFSFindClose(xid, pTcon, cifsFile->netfid);
Steve Frenchaaa9bbe2008-05-23 17:38:32 +0000827 } */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Steve French38702532007-07-08 15:40:40 +0000829 rc = find_cifs_entry(xid, pTcon, file,
830 &current_entry, &num_to_fill);
Steve French790fe572007-07-07 19:25:05 +0000831 if (rc) {
832 cFYI(1, ("fce error %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 goto rddir2_exit;
834 } else if (current_entry != NULL) {
Steve French790fe572007-07-07 19:25:05 +0000835 cFYI(1, ("entry %lld found", file->f_pos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 } else {
Steve French790fe572007-07-07 19:25:05 +0000837 cFYI(1, ("could not find entry"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 goto rddir2_exit;
839 }
Steve French790fe572007-07-07 19:25:05 +0000840 cFYI(1, ("loop through %d times filling dir for net buf %p",
841 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start));
Steve French5bafd762006-06-07 00:18:43 +0000842 max_len = smbCalcSize((struct smb_hdr *)
843 cifsFile->srch_inf.ntwrk_buf_start);
844 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
845
Jeff Laytonf5884162009-04-30 07:18:00 -0400846 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
Steve French790fe572007-07-07 19:25:05 +0000847 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
848 if (current_entry == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /* evaluate whether this case is an error */
Steve French26f57362007-08-30 22:09:15 +0000850 cERROR(1, ("past SMB end, num to fill %d i %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 num_to_fill, i));
852 break;
853 }
Steve French60808232006-04-22 15:53:05 +0000854 /* if buggy server returns . and .. late do
855 we want to check for that here? */
Steve French5bafd762006-06-07 00:18:43 +0000856 rc = cifs_filldir(current_entry, file,
857 filldir, direntry, tmp_buf, max_len);
Steve French790fe572007-07-07 19:25:05 +0000858 if (rc == -EOVERFLOW) {
Steve French7ca85ba2006-10-30 21:42:57 +0000859 rc = 0;
860 break;
861 }
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 file->f_pos++;
Steve French790fe572007-07-07 19:25:05 +0000864 if (file->f_pos ==
Steve French39798772006-05-31 22:40:51 +0000865 cifsFile->srch_inf.index_of_last_entry) {
Steve French790fe572007-07-07 19:25:05 +0000866 cFYI(1, ("last entry in buf at pos %lld %s",
867 file->f_pos, tmp_buf));
868 cifs_save_resume_key(current_entry, cifsFile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 break;
Steve French790fe572007-07-07 19:25:05 +0000870 } else
871 current_entry =
Steve French5bafd762006-06-07 00:18:43 +0000872 nxt_dir_entry(current_entry, end_of_smb,
873 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875 kfree(tmp_buf);
876 break;
877 } /* end switch */
878
879rddir2_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 FreeXid(xid);
881 return rc;
882}