blob: c343b14ba2d3eea99e7de45c7eed87311668e873 [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
Jeff Layton05507fa2010-02-05 13:30:36 -050080 if (parent->d_op && parent->d_op->d_hash)
81 parent->d_op->d_hash(parent, name);
82 else
83 name->hash = full_name_hash(name->name, name->len);
84
Jeff Laytoncc0bad72009-06-25 00:56:52 -040085 dentry = d_lookup(parent, name);
86 if (dentry) {
87 /* FIXME: check for inode number changes? */
88 if (dentry->d_inode != NULL)
89 return dentry;
90 d_drop(dentry);
91 dput(dentry);
92 }
93
94 dentry = d_alloc(parent, name);
95 if (dentry == NULL)
96 return NULL;
97
98 inode = cifs_iget(sb, fattr);
99 if (!inode) {
100 dput(dentry);
101 return NULL;
102 }
103
104 if (CIFS_SB(sb)->tcon->nocase)
105 dentry->d_op = &cifs_ci_dentry_ops;
106 else
107 dentry->d_op = &cifs_dentry_ops;
108
109 alias = d_materialise_unique(dentry, inode);
110 if (alias != NULL) {
111 dput(dentry);
112 if (IS_ERR(alias))
113 return NULL;
114 dentry = alias;
115 }
116
117 return dentry;
118}
119
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400120static void
121cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400123 fattr->cf_uid = cifs_sb->mnt_uid;
124 fattr->cf_gid = cifs_sb->mnt_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400126 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
127 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
128 fattr->cf_dtype = DT_DIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 } else {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400130 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
131 fattr->cf_dtype = DT_REG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400134 if (fattr->cf_cifsattrs & ATTR_READONLY)
135 fattr->cf_mode &= ~S_IWUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400137 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
138 fattr->cf_cifsattrs & ATTR_SYSTEM) {
139 if (fattr->cf_eof == 0) {
140 fattr->cf_mode &= ~S_IFMT;
141 fattr->cf_mode |= S_IFIFO;
142 fattr->cf_dtype = DT_FIFO;
Steve French3020a1f2005-11-18 11:31:10 -0800143 } else {
Jeff Layton4468eb32008-05-22 09:31:40 -0400144 /*
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400145 * trying to get the type and mode via SFU can be slow,
146 * so just call those regular files for now, and mark
147 * for reval
Jeff Layton4468eb32008-05-22 09:31:40 -0400148 */
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400149 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
Jeff Layton4468eb32008-05-22 09:31:40 -0400150 }
151 }
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400152}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Steve French15dd4782009-09-25 02:24:45 +0000154static void
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400155cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info,
156 struct cifs_sb_info *cifs_sb)
157{
158 memset(fattr, 0, sizeof(*fattr));
159 fattr->cf_cifsattrs = le32_to_cpu(info->ExtFileAttributes);
160 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
161 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
162 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
163 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
164 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400166 cifs_fill_common_info(fattr, cifs_sb);
167}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Steve French15dd4782009-09-25 02:24:45 +0000169static void
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400170cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info,
171 struct cifs_sb_info *cifs_sb)
172{
173 int offset = cifs_sb->tcon->ses->server->timeAdj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400175 memset(fattr, 0, sizeof(*fattr));
176 fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate,
177 info->LastAccessTime, offset);
178 fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate,
179 info->LastWriteTime, offset);
180 fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate,
181 info->LastWriteTime, offset);
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800182
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400183 fattr->cf_cifsattrs = le16_to_cpu(info->Attributes);
184 fattr->cf_bytes = le32_to_cpu(info->AllocationSize);
185 fattr->cf_eof = le32_to_cpu(info->DataSize);
Dave Kleikamp273d81d2006-06-01 19:41:23 +0000186
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400187 cifs_fill_common_info(fattr, cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Steve French0e0d2cf2009-05-01 05:27:32 +0000190/* BB eventually need to add the following helper function to
191 resolve NT_STATUS_STOPPED_ON_SYMLINK return code when
192 we try to do FindFirst on (NTFS) directory symlinks */
193/*
194int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
195 int xid)
196{
197 __u16 fid;
198 int len;
199 int oplock = 0;
200 int rc;
201 struct cifsTconInfo *ptcon = cifs_sb->tcon;
202 char *tmpbuffer;
203
204 rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ,
205 OPEN_REPARSE_POINT, &fid, &oplock, NULL,
206 cifs_sb->local_nls,
207 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
208 if (!rc) {
209 tmpbuffer = kmalloc(maxpath);
210 rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path,
211 tmpbuffer,
212 maxpath -1,
213 fid,
214 cifs_sb->local_nls);
215 if (CIFSSMBClose(xid, ptcon, fid)) {
216 cFYI(1, ("Error closing temporary reparsepoint open)"));
217 }
218 }
219}
220 */
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static int initiate_cifs_search(const int xid, struct file *file)
223{
224 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000225 char *full_path;
226 struct cifsFileInfo *cifsFile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 struct cifs_sb_info *cifs_sb;
228 struct cifsTconInfo *pTcon;
229
Steve French4523cc32007-04-30 20:13:06 +0000230 if (file->private_data == NULL) {
Steve French38702532007-07-08 15:40:40 +0000231 file->private_data =
232 kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234
Steve French4523cc32007-04-30 20:13:06 +0000235 if (file->private_data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 cifsFile = file->private_data;
Steve French4b18f2a2008-04-29 00:06:05 +0000238 cifsFile->invalidHandle = true;
239 cifsFile->srch_inf.endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800241 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
Steve French4523cc32007-04-30 20:13:06 +0000242 if (cifs_sb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return -EINVAL;
244
245 pTcon = cifs_sb->tcon;
Steve French4523cc32007-04-30 20:13:06 +0000246 if (pTcon == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return -EINVAL;
248
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800249 full_path = build_path_from_dentry(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Steve Frenchad7a2922008-02-07 23:25:02 +0000251 if (full_path == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Steve French966ca922005-04-28 22:41:08 -0700254 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Steve French75cf6bd2005-04-28 22:41:04 -0700256ffirst_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 /* test for Unix extensions */
Steve Frenchc18c8422007-07-18 23:21:09 +0000258 /* but now check for them on the share/mount not on the SMB session */
259/* if (pTcon->ses->capabilities & CAP_UNIX) { */
Steve Frenchad7a2922008-02-07 23:25:02 +0000260 if (pTcon->unix_ext)
Steve French5bafd762006-06-07 00:18:43 +0000261 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
Steve Frenchad7a2922008-02-07 23:25:02 +0000262 else if ((pTcon->ses->capabilities &
Steve French5bafd762006-06-07 00:18:43 +0000263 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
264 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
266 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
267 } else /* not srvinos - BB fixme add check for backlevel? */ {
268 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
269 }
270
Steve French38702532007-07-08 15:40:40 +0000271 rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
Steve French737b7582005-04-28 22:41:06 -0700272 &cifsFile->netfid, &cifsFile->srch_inf,
Steve French38702532007-07-08 15:40:40 +0000273 cifs_sb->mnt_cifs_flags &
Steve Frencheafe8702005-09-15 21:47:30 -0700274 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
Steve French4523cc32007-04-30 20:13:06 +0000275 if (rc == 0)
Steve French4b18f2a2008-04-29 00:06:05 +0000276 cifsFile->invalidHandle = false;
Steve Frenche836f012009-05-01 16:20:35 +0000277 /* BB add following call to handle readdir on new NTFS symlink errors
Steve French0e0d2cf2009-05-01 05:27:32 +0000278 else if STATUS_STOPPED_ON_SYMLINK
279 call get_symlink_reparse_path and retry with new path */
280 else if ((rc == -EOPNOTSUPP) &&
Steve French75cf6bd2005-04-28 22:41:04 -0700281 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
282 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
283 goto ffirst_retry;
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 kfree(full_path);
286 return rc;
287}
288
289/* return length of unicode string in bytes */
290static int cifs_unicode_bytelen(char *str)
291{
292 int len;
Steve French63d25832007-11-05 21:46:10 +0000293 __le16 *ustr = (__le16 *)str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Steve French38702532007-07-08 15:40:40 +0000295 for (len = 0; len <= PATH_MAX; len++) {
Steve French4523cc32007-04-30 20:13:06 +0000296 if (ustr[len] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return len << 1;
298 }
Steve French790fe572007-07-07 19:25:05 +0000299 cFYI(1, ("Unicode string longer than PATH_MAX found"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return len << 1;
301}
302
Steve French5bafd762006-06-07 00:18:43 +0000303static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Steve French38702532007-07-08 15:40:40 +0000305 char *new_entry;
Steve Frenchad7a2922008-02-07 23:25:02 +0000306 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Steve French4523cc32007-04-30 20:13:06 +0000308 if (level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000309 FIND_FILE_STANDARD_INFO *pfData;
Steve French5bafd762006-06-07 00:18:43 +0000310 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
311
312 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
313 pfData->FileNameLength;
314 } else
315 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
Steve French790fe572007-07-07 19:25:05 +0000316 cFYI(1, ("new entry %p old entry %p", new_entry, old_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /* validate that new_entry is not past end of SMB */
Steve French4523cc32007-04-30 20:13:06 +0000318 if (new_entry >= end_of_smb) {
Steve French09d1db52005-04-28 22:41:08 -0700319 cERROR(1,
320 ("search entry %p began after end of SMB %p old entry %p",
Steve French38702532007-07-08 15:40:40 +0000321 new_entry, end_of_smb, old_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return NULL;
Steve French4523cc32007-04-30 20:13:06 +0000323 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
Steve French38702532007-07-08 15:40:40 +0000324 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
325 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
Steve French5bafd762006-06-07 00:18:43 +0000326 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
Steve French38702532007-07-08 15:40:40 +0000327 cERROR(1, ("search entry %p extends after end of SMB %p",
Steve French09d1db52005-04-28 22:41:08 -0700328 new_entry, end_of_smb));
329 return NULL;
Steve French38702532007-07-08 15:40:40 +0000330 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return new_entry;
332
333}
334
335#define UNICODE_DOT cpu_to_le16(0x2e)
336
337/* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
338static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
339{
340 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000341 char *filename = NULL;
342 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Steve French4523cc32007-04-30 20:13:06 +0000344 if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000345 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 filename = &pFindData->FileName[0];
Steve French4523cc32007-04-30 20:13:06 +0000347 if (cfile->srch_inf.unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 len = cifs_unicode_bytelen(filename);
349 } else {
350 /* BB should we make this strnlen of PATH_MAX? */
351 len = strnlen(filename, 5);
352 }
Steve French4523cc32007-04-30 20:13:06 +0000353 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000354 FILE_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 (FILE_DIRECTORY_INFO *)current_entry;
356 filename = &pFindData->FileName[0];
357 len = le32_to_cpu(pFindData->FileNameLength);
Steve French38702532007-07-08 15:40:40 +0000358 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000359 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000360 FILE_FULL_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 (FILE_FULL_DIRECTORY_INFO *)current_entry;
362 filename = &pFindData->FileName[0];
363 len = le32_to_cpu(pFindData->FileNameLength);
Steve French4523cc32007-04-30 20:13:06 +0000364 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000365 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000366 SEARCH_ID_FULL_DIR_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
368 filename = &pFindData->FileName[0];
369 len = le32_to_cpu(pFindData->FileNameLength);
Steve French38702532007-07-08 15:40:40 +0000370 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000371 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000372 FILE_BOTH_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
374 filename = &pFindData->FileName[0];
375 len = le32_to_cpu(pFindData->FileNameLength);
Steve French4523cc32007-04-30 20:13:06 +0000376 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000377 FIND_FILE_STANDARD_INFO *pFindData =
Steve French5bafd762006-06-07 00:18:43 +0000378 (FIND_FILE_STANDARD_INFO *)current_entry;
379 filename = &pFindData->FileName[0];
Steve French5ddaa682006-08-15 13:35:48 +0000380 len = pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 } else {
Steve French790fe572007-07-07 19:25:05 +0000382 cFYI(1, ("Unknown findfirst level %d",
383 cfile->srch_inf.info_level));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385
Steve French4523cc32007-04-30 20:13:06 +0000386 if (filename) {
387 if (cfile->srch_inf.unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 __le16 *ufilename = (__le16 *)filename;
Steve French4523cc32007-04-30 20:13:06 +0000389 if (len == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* check for . */
Steve French4523cc32007-04-30 20:13:06 +0000391 if (ufilename[0] == UNICODE_DOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 rc = 1;
Steve French4523cc32007-04-30 20:13:06 +0000393 } else if (len == 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /* check for .. */
Steve French4523cc32007-04-30 20:13:06 +0000395 if ((ufilename[0] == UNICODE_DOT)
Steve French38702532007-07-08 15:40:40 +0000396 && (ufilename[1] == UNICODE_DOT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 rc = 2;
398 }
399 } else /* ASCII */ {
Steve French4523cc32007-04-30 20:13:06 +0000400 if (len == 1) {
Steve French38702532007-07-08 15:40:40 +0000401 if (filename[0] == '.')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 rc = 1;
Steve French4523cc32007-04-30 20:13:06 +0000403 } else if (len == 2) {
Steve French790fe572007-07-07 19:25:05 +0000404 if ((filename[0] == '.') && (filename[1] == '.'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 rc = 2;
406 }
407 }
408 }
409
410 return rc;
411}
412
Steve Frencheafe8702005-09-15 21:47:30 -0700413/* Check if directory that we are searching has changed so we can decide
414 whether we can use the cached search results from the previous search */
Steve French38702532007-07-08 15:40:40 +0000415static int is_dir_changed(struct file *file)
Steve Frencheafe8702005-09-15 21:47:30 -0700416{
Christoph Hellwigc33f8d32007-04-02 18:47:20 +0000417 struct inode *inode = file->f_path.dentry->d_inode;
418 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
Steve Frencheafe8702005-09-15 21:47:30 -0700419
Christoph Hellwigc33f8d32007-04-02 18:47:20 +0000420 if (cifsInfo->time == 0)
Steve Frencheafe8702005-09-15 21:47:30 -0700421 return 1; /* directory was changed, perhaps due to unlink */
422 else
423 return 0;
424
425}
426
Steve French0752f152008-10-07 20:03:33 +0000427static int cifs_save_resume_key(const char *current_entry,
428 struct cifsFileInfo *cifsFile)
429{
430 int rc = 0;
431 unsigned int len = 0;
432 __u16 level;
433 char *filename;
434
435 if ((cifsFile == NULL) || (current_entry == NULL))
436 return -EINVAL;
437
438 level = cifsFile->srch_inf.info_level;
439
440 if (level == SMB_FIND_FILE_UNIX) {
441 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
442
443 filename = &pFindData->FileName[0];
444 if (cifsFile->srch_inf.unicode) {
445 len = cifs_unicode_bytelen(filename);
446 } else {
447 /* BB should we make this strnlen of PATH_MAX? */
448 len = strnlen(filename, PATH_MAX);
449 }
450 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
451 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
452 FILE_DIRECTORY_INFO *pFindData =
453 (FILE_DIRECTORY_INFO *)current_entry;
454 filename = &pFindData->FileName[0];
455 len = le32_to_cpu(pFindData->FileNameLength);
456 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
457 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
458 FILE_FULL_DIRECTORY_INFO *pFindData =
459 (FILE_FULL_DIRECTORY_INFO *)current_entry;
460 filename = &pFindData->FileName[0];
461 len = le32_to_cpu(pFindData->FileNameLength);
462 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
463 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
464 SEARCH_ID_FULL_DIR_INFO *pFindData =
465 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
466 filename = &pFindData->FileName[0];
467 len = le32_to_cpu(pFindData->FileNameLength);
468 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
469 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
470 FILE_BOTH_DIRECTORY_INFO *pFindData =
471 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
472 filename = &pFindData->FileName[0];
473 len = le32_to_cpu(pFindData->FileNameLength);
474 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
475 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
476 FIND_FILE_STANDARD_INFO *pFindData =
477 (FIND_FILE_STANDARD_INFO *)current_entry;
478 filename = &pFindData->FileName[0];
479 /* one byte length, no name conversion */
480 len = (unsigned int)pFindData->FileNameLength;
481 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
482 } else {
483 cFYI(1, ("Unknown findfirst level %d", level));
484 return -EINVAL;
485 }
486 cifsFile->srch_inf.resume_name_len = len;
487 cifsFile->srch_inf.presume_name = filename;
488 return rc;
489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/* find the corresponding entry in the search */
492/* Note that the SMB server returns search entries for . and .. which
493 complicates logic here if we choose to parse for them and we do not
494 assume that they are located in the findfirst return buffer.*/
495/* We start counting in the buffer with entry 2 and increment for every
496 entry (do not increment for . or .. entry) */
497static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
Steve French38702532007-07-08 15:40:40 +0000498 struct file *file, char **ppCurrentEntry, int *num_to_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 int rc = 0;
501 int pos_in_buf = 0;
502 loff_t first_entry_in_buffer;
503 loff_t index_to_find = file->f_pos;
Steve French38702532007-07-08 15:40:40 +0000504 struct cifsFileInfo *cifsFile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /* check if index in the buffer */
Steve French50c2f752007-07-13 00:33:32 +0000506
Steve French38702532007-07-08 15:40:40 +0000507 if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
Steve Frencheafe8702005-09-15 21:47:30 -0700508 (num_to_ret == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return -ENOENT;
Steve French50c2f752007-07-13 00:33:32 +0000510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 *ppCurrentEntry = NULL;
Steve French38702532007-07-08 15:40:40 +0000512 first_entry_in_buffer =
513 cifsFile->srch_inf.index_of_last_entry -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 cifsFile->srch_inf.entries_in_buffer;
Steve French60808232006-04-22 15:53:05 +0000515
516 /* if first entry in buf is zero then is first buffer
517 in search response data which means it is likely . and ..
518 will be in this buffer, although some servers do not return
519 . and .. for the root of a drive and for those we need
520 to start two entries earlier */
521
Steve French39798772006-05-31 22:40:51 +0000522 dump_cifs_file_struct(file, "In fce ");
Steve French38702532007-07-08 15:40:40 +0000523 if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
524 is_dir_changed(file)) ||
Steve Frencheafe8702005-09-15 21:47:30 -0700525 (index_to_find < first_entry_in_buffer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* close and restart search */
Steve French790fe572007-07-07 19:25:05 +0000527 cFYI(1, ("search backing up - close and restart search"));
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000528 write_lock(&GlobalSMBSeslock);
Steve French77c57ec2008-05-13 21:39:32 +0000529 if (!cifsFile->srch_inf.endOfSearch &&
530 !cifsFile->invalidHandle) {
531 cifsFile->invalidHandle = true;
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000532 write_unlock(&GlobalSMBSeslock);
Steve French77c57ec2008-05-13 21:39:32 +0000533 CIFSFindClose(xid, pTcon, cifsFile->netfid);
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000534 } else
535 write_unlock(&GlobalSMBSeslock);
Steve French4523cc32007-04-30 20:13:06 +0000536 if (cifsFile->srch_inf.ntwrk_buf_start) {
Steve French790fe572007-07-07 19:25:05 +0000537 cFYI(1, ("freeing SMB ff cache buf on search rewind"));
Steve French4523cc32007-04-30 20:13:06 +0000538 if (cifsFile->srch_inf.smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +0000539 cifs_small_buf_release(cifsFile->srch_inf.
540 ntwrk_buf_start);
541 else
542 cifs_buf_release(cifsFile->srch_inf.
543 ntwrk_buf_start);
Shirish Pargaonkar76c510a2008-07-24 14:48:33 +0000544 cifsFile->srch_inf.ntwrk_buf_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
Steve French38702532007-07-08 15:40:40 +0000546 rc = initiate_cifs_search(xid, file);
Steve French4523cc32007-04-30 20:13:06 +0000547 if (rc) {
Steve French790fe572007-07-07 19:25:05 +0000548 cFYI(1, ("error %d reinitiating a search on rewind",
549 rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return rc;
551 }
Jeff Laytona364bc02008-10-21 14:42:13 +0000552 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
Steve French38702532007-07-08 15:40:40 +0000555 while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
Steve French4b18f2a2008-04-29 00:06:05 +0000556 (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
Steve French790fe572007-07-07 19:25:05 +0000557 cFYI(1, ("calling findnext2"));
Steve French38702532007-07-08 15:40:40 +0000558 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
Steve Frenchdfb75332005-06-22 17:13:47 -0700559 &cifsFile->srch_inf);
Jeff Laytona364bc02008-10-21 14:42:13 +0000560 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
Steve French4523cc32007-04-30 20:13:06 +0000561 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return -ENOENT;
563 }
Steve French4523cc32007-04-30 20:13:06 +0000564 if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /* we found the buffer that contains the entry */
566 /* scan and find it */
567 int i;
Steve French38702532007-07-08 15:40:40 +0000568 char *current_entry;
569 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 smbCalcSize((struct smb_hdr *)
571 cifsFile->srch_inf.ntwrk_buf_start);
Steve French60808232006-04-22 15:53:05 +0000572
573 current_entry = cifsFile->srch_inf.srch_entries_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
575 - cifsFile->srch_inf.entries_in_buffer;
576 pos_in_buf = index_to_find - first_entry_in_buffer;
Steve French790fe572007-07-07 19:25:05 +0000577 cFYI(1, ("found entry - pos_in_buf %d", pos_in_buf));
Steve French5bafd762006-06-07 00:18:43 +0000578
Steve Frenchad7a2922008-02-07 23:25:02 +0000579 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
Steve Frenchdfb75332005-06-22 17:13:47 -0700580 /* go entry by entry figuring out which is first */
Steve French38702532007-07-08 15:40:40 +0000581 current_entry = nxt_dir_entry(current_entry, end_of_smb,
Steve French5bafd762006-06-07 00:18:43 +0000582 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
Steve French790fe572007-07-07 19:25:05 +0000584 if ((current_entry == NULL) && (i < pos_in_buf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* BB fixme - check if we should flag this error */
Steve French38702532007-07-08 15:40:40 +0000586 cERROR(1, ("reached end of buf searching for pos in buf"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 " %d index to find %lld rc %d",
Steve French38702532007-07-08 15:40:40 +0000588 pos_in_buf, index_to_find, rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590 rc = 0;
591 *ppCurrentEntry = current_entry;
592 } else {
Steve French790fe572007-07-07 19:25:05 +0000593 cFYI(1, ("index not in buffer - could not findnext into it"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return 0;
595 }
596
Steve French790fe572007-07-07 19:25:05 +0000597 if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
598 cFYI(1, ("can not return entries pos_in_buf beyond last"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 *num_to_ret = 0;
600 } else
601 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 return rc;
604}
605
606/* inode num, inode type and filename returned */
607static int cifs_get_name_from_search_buf(struct qstr *pqst,
608 char *current_entry, __u16 level, unsigned int unicode,
Jeff Layton18295792009-04-30 20:45:45 -0400609 struct cifs_sb_info *cifs_sb, unsigned int max_len, __u64 *pinum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 int rc = 0;
612 unsigned int len = 0;
Steve French38702532007-07-08 15:40:40 +0000613 char *filename;
614 struct nls_table *nlt = cifs_sb->local_nls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 *pinum = 0;
617
Steve French790fe572007-07-07 19:25:05 +0000618 if (level == SMB_FIND_FILE_UNIX) {
Steve French38702532007-07-08 15:40:40 +0000619 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 filename = &pFindData->FileName[0];
Steve French790fe572007-07-07 19:25:05 +0000622 if (unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 len = cifs_unicode_bytelen(filename);
624 } else {
625 /* BB should we make this strnlen of PATH_MAX? */
626 len = strnlen(filename, PATH_MAX);
627 }
628
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400629 *pinum = le64_to_cpu(pFindData->basic.UniqueId);
Steve French790fe572007-07-07 19:25:05 +0000630 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000631 FILE_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 (FILE_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_FULL_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000636 FILE_FULL_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 (FILE_FULL_DIRECTORY_INFO *)current_entry;
638 filename = &pFindData->FileName[0];
639 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000640 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
Steve French38702532007-07-08 15:40:40 +0000641 SEARCH_ID_FULL_DIR_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
643 filename = &pFindData->FileName[0];
644 len = le32_to_cpu(pFindData->FileNameLength);
Steve French85a6dac2009-04-01 05:22:00 +0000645 *pinum = le64_to_cpu(pFindData->UniqueId);
Steve French790fe572007-07-07 19:25:05 +0000646 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000647 FILE_BOTH_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
649 filename = &pFindData->FileName[0];
650 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000651 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000652 FIND_FILE_STANDARD_INFO *pFindData =
Steve French5bafd762006-06-07 00:18:43 +0000653 (FIND_FILE_STANDARD_INFO *)current_entry;
654 filename = &pFindData->FileName[0];
655 /* one byte length, no name conversion */
656 len = (unsigned int)pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 } else {
Steve French790fe572007-07-07 19:25:05 +0000658 cFYI(1, ("Unknown findfirst level %d", level));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return -EINVAL;
660 }
Steve French5bafd762006-06-07 00:18:43 +0000661
Steve French790fe572007-07-07 19:25:05 +0000662 if (len > max_len) {
Steve French38702532007-07-08 15:40:40 +0000663 cERROR(1, ("bad search response length %d past smb end", len));
Steve French5bafd762006-06-07 00:18:43 +0000664 return -EINVAL;
665 }
666
Steve French790fe572007-07-07 19:25:05 +0000667 if (unicode) {
Jeff Laytonf5884162009-04-30 07:18:00 -0400668 pqst->len = cifs_from_ucs2((char *) pqst->name,
669 (__le16 *) filename,
Jeff Layton18295792009-04-30 20:45:45 -0400670 UNICODE_NAME_MAX,
671 min(len, max_len), nlt,
Jeff Laytonf5884162009-04-30 07:18:00 -0400672 cifs_sb->mnt_cifs_flags &
673 CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Laytonf12f98d2010-02-05 13:14:00 -0500674 pqst->len -= nls_nullsize(nlt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 } else {
676 pqst->name = filename;
677 pqst->len = len;
678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return rc;
680}
681
Jeff Layton18295792009-04-30 20:45:45 -0400682static int cifs_filldir(char *pfindEntry, struct file *file, filldir_t filldir,
683 void *direntry, char *scratch_buf, unsigned int max_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 int rc = 0;
686 struct qstr qstring;
Steve French38702532007-07-08 15:40:40 +0000687 struct cifsFileInfo *pCifsF;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400688 u64 inum;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400689 ino_t ino;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400690 struct super_block *sb;
Steve French38702532007-07-08 15:40:40 +0000691 struct cifs_sb_info *cifs_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct dentry *tmp_dentry;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400693 struct cifs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 /* get filename and len into qstring */
696 /* get dentry */
697 /* decide whether to create and populate ionde */
Steve French790fe572007-07-07 19:25:05 +0000698 if ((direntry == NULL) || (file == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return -EINVAL;
700
701 pCifsF = file->private_data;
Steve French50c2f752007-07-13 00:33:32 +0000702
Steve French790fe572007-07-07 19:25:05 +0000703 if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return -ENOENT;
705
Steve French38702532007-07-08 15:40:40 +0000706 rc = cifs_entry_is_dot(pfindEntry, pCifsF);
Steve French60808232006-04-22 15:53:05 +0000707 /* skip . and .. since we added them first */
Steve French790fe572007-07-07 19:25:05 +0000708 if (rc != 0)
Steve French60808232006-04-22 15:53:05 +0000709 return 0;
710
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400711 sb = file->f_path.dentry->d_sb;
712 cifs_sb = CIFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 qstring.name = scratch_buf;
Steve French38702532007-07-08 15:40:40 +0000715 rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 pCifsF->srch_inf.info_level,
Steve French38702532007-07-08 15:40:40 +0000717 pCifsF->srch_inf.unicode, cifs_sb,
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400718 max_len, &inum /* returned */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Steve French790fe572007-07-07 19:25:05 +0000720 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return rc;
722
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400723 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400724 cifs_unix_basic_to_fattr(&fattr,
725 &((FILE_UNIX_INFO *) pfindEntry)->basic,
726 cifs_sb);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400727 else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
728 cifs_std_info_to_fattr(&fattr, (FIND_FILE_STANDARD_INFO *)
729 pfindEntry, cifs_sb);
730 else
731 cifs_dir_info_to_fattr(&fattr, (FILE_DIRECTORY_INFO *)
732 pfindEntry, cifs_sb);
Jeff Layton132ac7b2009-02-10 07:33:57 -0500733
Jeff Laytonec06aed2009-11-06 14:18:29 -0500734 if (inum && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400735 fattr.cf_uniqueid = inum;
Jeff Laytonec06aed2009-11-06 14:18:29 -0500736 } else {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400737 fattr.cf_uniqueid = iunique(sb, ROOT_I);
Jeff Laytonec06aed2009-11-06 14:18:29 -0500738 cifs_autodisable_serverino(cifs_sb);
739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400741 ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
742 tmp_dentry = cifs_readdir_lookup(file->f_dentry, &qstring, &fattr);
Steve French50c2f752007-07-13 00:33:32 +0000743
Steve French38702532007-07-08 15:40:40 +0000744 rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400745 ino, fattr.cf_dtype);
746
747 /*
748 * we can not return filldir errors to the caller since they are
749 * "normal" when the stat blocksize is too small - we return remapped
750 * error instead
751 *
752 * FIXME: This looks bogus. filldir returns -EOVERFLOW in the above
753 * case already. Why should we be clobbering other errors from it?
754 */
Steve French790fe572007-07-07 19:25:05 +0000755 if (rc) {
756 cFYI(1, ("filldir rc = %d", rc));
Steve French7ca85ba2006-10-30 21:42:57 +0000757 rc = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 dput(tmp_dentry);
760 return rc;
761}
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
765{
766 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000767 int xid, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 struct cifs_sb_info *cifs_sb;
769 struct cifsTconInfo *pTcon;
770 struct cifsFileInfo *cifsFile = NULL;
Steve French38702532007-07-08 15:40:40 +0000771 char *current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 int num_to_fill = 0;
Steve French38702532007-07-08 15:40:40 +0000773 char *tmp_buf = NULL;
Steve French50c2f752007-07-13 00:33:32 +0000774 char *end_of_smb;
Jeff Layton18295792009-04-30 20:45:45 -0400775 unsigned int max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 xid = GetXid();
778
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800779 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 pTcon = cifs_sb->tcon;
Steve French790fe572007-07-07 19:25:05 +0000781 if (pTcon == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return -EINVAL;
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 switch ((int) file->f_pos) {
785 case 0:
Steve French60808232006-04-22 15:53:05 +0000786 if (filldir(direntry, ".", 1, file->f_pos,
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800787 file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
Steve French60808232006-04-22 15:53:05 +0000788 cERROR(1, ("Filldir for current dir failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 rc = -ENOMEM;
790 break;
791 }
Steve French60808232006-04-22 15:53:05 +0000792 file->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 case 1:
Steve French60808232006-04-22 15:53:05 +0000794 if (filldir(direntry, "..", 2, file->f_pos,
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800795 file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
Steve French26a21b92006-05-31 18:05:34 +0000796 cERROR(1, ("Filldir for parent dir failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 rc = -ENOMEM;
798 break;
799 }
Steve French60808232006-04-22 15:53:05 +0000800 file->f_pos++;
801 default:
Steve French38702532007-07-08 15:40:40 +0000802 /* 1) If search is active,
803 is in current search buffer?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if it before then restart search
805 if after then keep searching till find it */
806
Steve French790fe572007-07-07 19:25:05 +0000807 if (file->private_data == NULL) {
Steve French38702532007-07-08 15:40:40 +0000808 rc = initiate_cifs_search(xid, file);
Steve French790fe572007-07-07 19:25:05 +0000809 cFYI(1, ("initiate cifs search rc %d", rc));
810 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 FreeXid(xid);
812 return rc;
813 }
814 }
Steve French790fe572007-07-07 19:25:05 +0000815 if (file->private_data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 rc = -EINVAL;
817 FreeXid(xid);
818 return rc;
819 }
820 cifsFile = file->private_data;
821 if (cifsFile->srch_inf.endOfSearch) {
Steve French790fe572007-07-07 19:25:05 +0000822 if (cifsFile->srch_inf.emptyDir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 cFYI(1, ("End of search, empty dir"));
824 rc = 0;
825 break;
826 }
827 } /* else {
Steve French4b18f2a2008-04-29 00:06:05 +0000828 cifsFile->invalidHandle = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 CIFSFindClose(xid, pTcon, cifsFile->netfid);
Steve Frenchaaa9bbe2008-05-23 17:38:32 +0000830 } */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Steve French38702532007-07-08 15:40:40 +0000832 rc = find_cifs_entry(xid, pTcon, file,
833 &current_entry, &num_to_fill);
Steve French790fe572007-07-07 19:25:05 +0000834 if (rc) {
835 cFYI(1, ("fce error %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto rddir2_exit;
837 } else if (current_entry != NULL) {
Steve French790fe572007-07-07 19:25:05 +0000838 cFYI(1, ("entry %lld found", file->f_pos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 } else {
Steve French790fe572007-07-07 19:25:05 +0000840 cFYI(1, ("could not find entry"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 goto rddir2_exit;
842 }
Steve French790fe572007-07-07 19:25:05 +0000843 cFYI(1, ("loop through %d times filling dir for net buf %p",
844 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start));
Steve French5bafd762006-06-07 00:18:43 +0000845 max_len = smbCalcSize((struct smb_hdr *)
846 cifsFile->srch_inf.ntwrk_buf_start);
847 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
848
Jeff Laytonf5884162009-04-30 07:18:00 -0400849 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
Steve French790fe572007-07-07 19:25:05 +0000850 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
851 if (current_entry == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /* evaluate whether this case is an error */
Steve French26f57362007-08-30 22:09:15 +0000853 cERROR(1, ("past SMB end, num to fill %d i %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 num_to_fill, i));
855 break;
856 }
Steve French60808232006-04-22 15:53:05 +0000857 /* if buggy server returns . and .. late do
858 we want to check for that here? */
Steve French5bafd762006-06-07 00:18:43 +0000859 rc = cifs_filldir(current_entry, file,
860 filldir, direntry, tmp_buf, max_len);
Steve French790fe572007-07-07 19:25:05 +0000861 if (rc == -EOVERFLOW) {
Steve French7ca85ba2006-10-30 21:42:57 +0000862 rc = 0;
863 break;
864 }
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 file->f_pos++;
Steve French790fe572007-07-07 19:25:05 +0000867 if (file->f_pos ==
Steve French39798772006-05-31 22:40:51 +0000868 cifsFile->srch_inf.index_of_last_entry) {
Steve French790fe572007-07-07 19:25:05 +0000869 cFYI(1, ("last entry in buf at pos %lld %s",
870 file->f_pos, tmp_buf));
871 cifs_save_resume_key(current_entry, cifsFile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 break;
Steve French790fe572007-07-07 19:25:05 +0000873 } else
874 current_entry =
Steve French5bafd762006-06-07 00:18:43 +0000875 nxt_dir_entry(current_entry, end_of_smb,
876 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878 kfree(tmp_buf);
879 break;
880 } /* end switch */
881
882rddir2_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 FreeXid(xid);
884 return rc;
885}