blob: 1f098ca7163602bcdbb75aa4a69ed1f2bde4b8a2 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 } else {
670 pqst->name = filename;
671 pqst->len = len;
672 }
Steve French38702532007-07-08 15:40:40 +0000673 pqst->hash = full_name_hash(pqst->name, pqst->len);
Steve French790fe572007-07-07 19:25:05 +0000674/* cFYI(1, ("filldir on %s",pqst->name)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return rc;
676}
677
Jeff Layton18295792009-04-30 20:45:45 -0400678static int cifs_filldir(char *pfindEntry, struct file *file, filldir_t filldir,
679 void *direntry, char *scratch_buf, unsigned int max_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 int rc = 0;
682 struct qstr qstring;
Steve French38702532007-07-08 15:40:40 +0000683 struct cifsFileInfo *pCifsF;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400684 u64 inum;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400685 ino_t ino;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400686 struct super_block *sb;
Steve French38702532007-07-08 15:40:40 +0000687 struct cifs_sb_info *cifs_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct dentry *tmp_dentry;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400689 struct cifs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 /* get filename and len into qstring */
692 /* get dentry */
693 /* decide whether to create and populate ionde */
Steve French790fe572007-07-07 19:25:05 +0000694 if ((direntry == NULL) || (file == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return -EINVAL;
696
697 pCifsF = file->private_data;
Steve French50c2f752007-07-13 00:33:32 +0000698
Steve French790fe572007-07-07 19:25:05 +0000699 if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return -ENOENT;
701
Steve French38702532007-07-08 15:40:40 +0000702 rc = cifs_entry_is_dot(pfindEntry, pCifsF);
Steve French60808232006-04-22 15:53:05 +0000703 /* skip . and .. since we added them first */
Steve French790fe572007-07-07 19:25:05 +0000704 if (rc != 0)
Steve French60808232006-04-22 15:53:05 +0000705 return 0;
706
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400707 sb = file->f_path.dentry->d_sb;
708 cifs_sb = CIFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 qstring.name = scratch_buf;
Steve French38702532007-07-08 15:40:40 +0000711 rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 pCifsF->srch_inf.info_level,
Steve French38702532007-07-08 15:40:40 +0000713 pCifsF->srch_inf.unicode, cifs_sb,
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400714 max_len, &inum /* returned */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Steve French790fe572007-07-07 19:25:05 +0000716 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return rc;
718
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400719 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400720 cifs_unix_basic_to_fattr(&fattr,
721 &((FILE_UNIX_INFO *) pfindEntry)->basic,
722 cifs_sb);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400723 else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
724 cifs_std_info_to_fattr(&fattr, (FIND_FILE_STANDARD_INFO *)
725 pfindEntry, cifs_sb);
726 else
727 cifs_dir_info_to_fattr(&fattr, (FILE_DIRECTORY_INFO *)
728 pfindEntry, cifs_sb);
Jeff Layton132ac7b2009-02-10 07:33:57 -0500729
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400730 /* FIXME: make _to_fattr functions fill this out */
731 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_ID_FULL_DIR_INFO)
732 fattr.cf_uniqueid = inum;
733 else
734 fattr.cf_uniqueid = iunique(sb, ROOT_I);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400736 ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
737 tmp_dentry = cifs_readdir_lookup(file->f_dentry, &qstring, &fattr);
Steve French50c2f752007-07-13 00:33:32 +0000738
Steve French38702532007-07-08 15:40:40 +0000739 rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400740 ino, fattr.cf_dtype);
741
742 /*
743 * we can not return filldir errors to the caller since they are
744 * "normal" when the stat blocksize is too small - we return remapped
745 * error instead
746 *
747 * FIXME: This looks bogus. filldir returns -EOVERFLOW in the above
748 * case already. Why should we be clobbering other errors from it?
749 */
Steve French790fe572007-07-07 19:25:05 +0000750 if (rc) {
751 cFYI(1, ("filldir rc = %d", rc));
Steve French7ca85ba2006-10-30 21:42:57 +0000752 rc = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 dput(tmp_dentry);
755 return rc;
756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
760{
761 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000762 int xid, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 struct cifs_sb_info *cifs_sb;
764 struct cifsTconInfo *pTcon;
765 struct cifsFileInfo *cifsFile = NULL;
Steve French38702532007-07-08 15:40:40 +0000766 char *current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 int num_to_fill = 0;
Steve French38702532007-07-08 15:40:40 +0000768 char *tmp_buf = NULL;
Steve French50c2f752007-07-13 00:33:32 +0000769 char *end_of_smb;
Jeff Layton18295792009-04-30 20:45:45 -0400770 unsigned int max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 xid = GetXid();
773
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800774 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 pTcon = cifs_sb->tcon;
Steve French790fe572007-07-07 19:25:05 +0000776 if (pTcon == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return -EINVAL;
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 switch ((int) file->f_pos) {
780 case 0:
Steve French60808232006-04-22 15:53:05 +0000781 if (filldir(direntry, ".", 1, file->f_pos,
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800782 file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
Steve French60808232006-04-22 15:53:05 +0000783 cERROR(1, ("Filldir for current dir failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 rc = -ENOMEM;
785 break;
786 }
Steve French60808232006-04-22 15:53:05 +0000787 file->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 case 1:
Steve French60808232006-04-22 15:53:05 +0000789 if (filldir(direntry, "..", 2, file->f_pos,
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800790 file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
Steve French26a21b92006-05-31 18:05:34 +0000791 cERROR(1, ("Filldir for parent dir failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 rc = -ENOMEM;
793 break;
794 }
Steve French60808232006-04-22 15:53:05 +0000795 file->f_pos++;
796 default:
Steve French38702532007-07-08 15:40:40 +0000797 /* 1) If search is active,
798 is in current search buffer?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if it before then restart search
800 if after then keep searching till find it */
801
Steve French790fe572007-07-07 19:25:05 +0000802 if (file->private_data == NULL) {
Steve French38702532007-07-08 15:40:40 +0000803 rc = initiate_cifs_search(xid, file);
Steve French790fe572007-07-07 19:25:05 +0000804 cFYI(1, ("initiate cifs search rc %d", rc));
805 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 FreeXid(xid);
807 return rc;
808 }
809 }
Steve French790fe572007-07-07 19:25:05 +0000810 if (file->private_data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 rc = -EINVAL;
812 FreeXid(xid);
813 return rc;
814 }
815 cifsFile = file->private_data;
816 if (cifsFile->srch_inf.endOfSearch) {
Steve French790fe572007-07-07 19:25:05 +0000817 if (cifsFile->srch_inf.emptyDir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 cFYI(1, ("End of search, empty dir"));
819 rc = 0;
820 break;
821 }
822 } /* else {
Steve French4b18f2a2008-04-29 00:06:05 +0000823 cifsFile->invalidHandle = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 CIFSFindClose(xid, pTcon, cifsFile->netfid);
Steve Frenchaaa9bbe2008-05-23 17:38:32 +0000825 } */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Steve French38702532007-07-08 15:40:40 +0000827 rc = find_cifs_entry(xid, pTcon, file,
828 &current_entry, &num_to_fill);
Steve French790fe572007-07-07 19:25:05 +0000829 if (rc) {
830 cFYI(1, ("fce error %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 goto rddir2_exit;
832 } else if (current_entry != NULL) {
Steve French790fe572007-07-07 19:25:05 +0000833 cFYI(1, ("entry %lld found", file->f_pos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 } else {
Steve French790fe572007-07-07 19:25:05 +0000835 cFYI(1, ("could not find entry"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto rddir2_exit;
837 }
Steve French790fe572007-07-07 19:25:05 +0000838 cFYI(1, ("loop through %d times filling dir for net buf %p",
839 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start));
Steve French5bafd762006-06-07 00:18:43 +0000840 max_len = smbCalcSize((struct smb_hdr *)
841 cifsFile->srch_inf.ntwrk_buf_start);
842 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
843
Jeff Laytonf5884162009-04-30 07:18:00 -0400844 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
Steve French790fe572007-07-07 19:25:05 +0000845 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
846 if (current_entry == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 /* evaluate whether this case is an error */
Steve French26f57362007-08-30 22:09:15 +0000848 cERROR(1, ("past SMB end, num to fill %d i %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 num_to_fill, i));
850 break;
851 }
Steve French60808232006-04-22 15:53:05 +0000852 /* if buggy server returns . and .. late do
853 we want to check for that here? */
Steve French5bafd762006-06-07 00:18:43 +0000854 rc = cifs_filldir(current_entry, file,
855 filldir, direntry, tmp_buf, max_len);
Steve French790fe572007-07-07 19:25:05 +0000856 if (rc == -EOVERFLOW) {
Steve French7ca85ba2006-10-30 21:42:57 +0000857 rc = 0;
858 break;
859 }
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 file->f_pos++;
Steve French790fe572007-07-07 19:25:05 +0000862 if (file->f_pos ==
Steve French39798772006-05-31 22:40:51 +0000863 cifsFile->srch_inf.index_of_last_entry) {
Steve French790fe572007-07-07 19:25:05 +0000864 cFYI(1, ("last entry in buf at pos %lld %s",
865 file->f_pos, tmp_buf));
866 cifs_save_resume_key(current_entry, cifsFile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 break;
Steve French790fe572007-07-07 19:25:05 +0000868 } else
869 current_entry =
Steve French5bafd762006-06-07 00:18:43 +0000870 nxt_dir_entry(current_entry, end_of_smb,
871 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873 kfree(tmp_buf);
874 break;
875 } /* end switch */
876
877rddir2_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 FreeXid(xid);
879 return rc;
880}