blob: 7f25cc3d22569509cd59ac27f6b9e3acc2ddaf5f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/readdir.c
3 *
4 * Directory search handling
Steve French6dc0f872007-07-06 23:13:06 +00005 *
Steve Frenchad7a2922008-02-07 23:25:02 +00006 * Copyright (C) International Business Machines Corp., 2004, 2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Author(s): Steve French (sfrench@us.ibm.com)
8 *
9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23#include <linux/fs.h>
Dave Kleikamp273d81d2006-06-01 19:41:23 +000024#include <linux/pagemap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "cifspdu.h"
28#include "cifsglob.h"
29#include "cifsproto.h"
30#include "cifs_unicode.h"
31#include "cifs_debug.h"
32#include "cifs_fs_sb.h"
33#include "cifsfs.h"
34
Jeff Laytonf5884162009-04-30 07:18:00 -040035/*
36 * To be safe - for UCS to UTF-8 with strings loaded with the rare long
37 * characters alloc more to account for such multibyte target UTF-8
38 * characters.
39 */
40#define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
41
Steve French39798772006-05-31 22:40:51 +000042#ifdef CONFIG_CIFS_DEBUG2
43static void dump_cifs_file_struct(struct file *file, char *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Steve French6dc0f872007-07-06 23:13:06 +000045 struct cifsFileInfo *cf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Steve French4523cc32007-04-30 20:13:06 +000047 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 cf = file->private_data;
Steve French4523cc32007-04-30 20:13:06 +000049 if (cf == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +000050 cFYI(1, "empty cifs private file data");
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 return;
52 }
Steve Frenchad7a2922008-02-07 23:25:02 +000053 if (cf->invalidHandle)
Joe Perchesb6b38f72010-04-21 03:50:45 +000054 cFYI(1, "invalid handle");
Steve Frenchad7a2922008-02-07 23:25:02 +000055 if (cf->srch_inf.endOfSearch)
Joe Perchesb6b38f72010-04-21 03:50:45 +000056 cFYI(1, "end of search");
Steve Frenchad7a2922008-02-07 23:25:02 +000057 if (cf->srch_inf.emptyDir)
Joe Perchesb6b38f72010-04-21 03:50:45 +000058 cFYI(1, "empty dir");
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
Steve French39798772006-05-31 22:40:51 +000060}
Steve French90c81e02008-02-12 20:32:36 +000061#else
62static inline void dump_cifs_file_struct(struct file *file, char *label)
63{
64}
Steve French39798772006-05-31 22:40:51 +000065#endif /* DEBUG2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Jeff Laytoncc0bad72009-06-25 00:56:52 -040067/*
68 * Find the dentry that matches "name". If there isn't one, create one. If it's
69 * a negative dentry or the uniqueid changed, then drop it and recreate it.
70 */
71static struct dentry *
72cifs_readdir_lookup(struct dentry *parent, struct qstr *name,
73 struct cifs_fattr *fattr)
74{
75 struct dentry *dentry, *alias;
76 struct inode *inode;
77 struct super_block *sb = parent->d_inode->i_sb;
78
Joe Perchesb6b38f72010-04-21 03:50:45 +000079 cFYI(1, "For %s", name->name);
Jeff Laytoncc0bad72009-06-25 00:56:52 -040080
Jeff Layton05507fa2010-02-05 13:30:36 -050081 if (parent->d_op && parent->d_op->d_hash)
Nick Pigginb1e6a012011-01-07 17:49:28 +110082 parent->d_op->d_hash(parent, parent->d_inode, name);
Jeff Layton05507fa2010-02-05 13:30:36 -050083 else
84 name->hash = full_name_hash(name->name, name->len);
85
Jeff Laytoncc0bad72009-06-25 00:56:52 -040086 dentry = d_lookup(parent, name);
87 if (dentry) {
88 /* FIXME: check for inode number changes? */
89 if (dentry->d_inode != NULL)
90 return dentry;
91 d_drop(dentry);
92 dput(dentry);
93 }
94
95 dentry = d_alloc(parent, name);
96 if (dentry == NULL)
97 return NULL;
98
99 inode = cifs_iget(sb, fattr);
100 if (!inode) {
101 dput(dentry);
102 return NULL;
103 }
104
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400105 alias = d_materialise_unique(dentry, inode);
106 if (alias != NULL) {
107 dput(dentry);
108 if (IS_ERR(alias))
109 return NULL;
110 dentry = alias;
111 }
112
113 return dentry;
114}
115
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400116static void
117cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400119 fattr->cf_uid = cifs_sb->mnt_uid;
120 fattr->cf_gid = cifs_sb->mnt_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400122 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
123 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
124 fattr->cf_dtype = DT_DIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 } else {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400126 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
127 fattr->cf_dtype = DT_REG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400130 if (fattr->cf_cifsattrs & ATTR_READONLY)
131 fattr->cf_mode &= ~S_IWUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400133 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
134 fattr->cf_cifsattrs & ATTR_SYSTEM) {
135 if (fattr->cf_eof == 0) {
136 fattr->cf_mode &= ~S_IFMT;
137 fattr->cf_mode |= S_IFIFO;
138 fattr->cf_dtype = DT_FIFO;
Steve French3020a1f2005-11-18 11:31:10 -0800139 } else {
Jeff Layton4468eb32008-05-22 09:31:40 -0400140 /*
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400141 * trying to get the type and mode via SFU can be slow,
142 * so just call those regular files for now, and mark
143 * for reval
Jeff Layton4468eb32008-05-22 09:31:40 -0400144 */
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400145 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
Jeff Layton4468eb32008-05-22 09:31:40 -0400146 }
147 }
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400148}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Steve French15dd4782009-09-25 02:24:45 +0000150static void
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400151cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info,
152 struct cifs_sb_info *cifs_sb)
153{
154 memset(fattr, 0, sizeof(*fattr));
155 fattr->cf_cifsattrs = le32_to_cpu(info->ExtFileAttributes);
156 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
157 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
Jeff Layton20054bd2011-01-07 11:30:27 -0500158 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400159 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
160 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
161 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400163 cifs_fill_common_info(fattr, cifs_sb);
164}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Steve French15dd4782009-09-25 02:24:45 +0000166static void
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400167cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info,
168 struct cifs_sb_info *cifs_sb)
169{
Jeff Layton0d424ad2010-09-20 16:01:35 -0700170 int offset = cifs_sb_master_tcon(cifs_sb)->ses->server->timeAdj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400172 memset(fattr, 0, sizeof(*fattr));
173 fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate,
174 info->LastAccessTime, offset);
175 fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate,
176 info->LastWriteTime, offset);
177 fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate,
178 info->LastWriteTime, offset);
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800179
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400180 fattr->cf_cifsattrs = le16_to_cpu(info->Attributes);
181 fattr->cf_bytes = le32_to_cpu(info->AllocationSize);
182 fattr->cf_eof = le32_to_cpu(info->DataSize);
Dave Kleikamp273d81d2006-06-01 19:41:23 +0000183
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400184 cifs_fill_common_info(fattr, cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Steve French0e0d2cf2009-05-01 05:27:32 +0000187/* BB eventually need to add the following helper function to
188 resolve NT_STATUS_STOPPED_ON_SYMLINK return code when
189 we try to do FindFirst on (NTFS) directory symlinks */
190/*
191int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
192 int xid)
193{
194 __u16 fid;
195 int len;
196 int oplock = 0;
197 int rc;
Jeff Laytona6e8a842010-09-20 16:01:33 -0700198 struct cifsTconInfo *ptcon = cifs_sb_tcon(cifs_sb);
Steve French0e0d2cf2009-05-01 05:27:32 +0000199 char *tmpbuffer;
200
201 rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ,
202 OPEN_REPARSE_POINT, &fid, &oplock, NULL,
203 cifs_sb->local_nls,
204 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
205 if (!rc) {
206 tmpbuffer = kmalloc(maxpath);
207 rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path,
208 tmpbuffer,
209 maxpath -1,
210 fid,
211 cifs_sb->local_nls);
212 if (CIFSSMBClose(xid, ptcon, fid)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000213 cFYI(1, "Error closing temporary reparsepoint open");
Steve French0e0d2cf2009-05-01 05:27:32 +0000214 }
215 }
216}
217 */
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219static int initiate_cifs_search(const int xid, struct file *file)
220{
221 int rc = 0;
Jeff Layton7ffec372010-09-29 19:51:11 -0400222 char *full_path = NULL;
Steve French38702532007-07-08 15:40:40 +0000223 struct cifsFileInfo *cifsFile;
Jeff Layton7ffec372010-09-29 19:51:11 -0400224 struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
Jeff Layton59c55ba2010-11-12 06:30:29 -0500225 struct tcon_link *tlink = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 struct cifsTconInfo *pTcon;
227
Jeff Layton7ffec372010-09-29 19:51:11 -0400228 if (file->private_data == NULL) {
Jeff Layton59c55ba2010-11-12 06:30:29 -0500229 tlink = cifs_sb_tlink(cifs_sb);
230 if (IS_ERR(tlink))
231 return PTR_ERR(tlink);
232
233 cifsFile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
234 if (cifsFile == NULL) {
235 rc = -ENOMEM;
236 goto error_exit;
237 }
238 file->private_data = cifsFile;
239 cifsFile->tlink = cifs_get_tlink(tlink);
240 pTcon = tlink_tcon(tlink);
241 } else {
242 cifsFile = file->private_data;
243 pTcon = tlink_tcon(cifsFile->tlink);
Jeff Layton7ffec372010-09-29 19:51:11 -0400244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Steve French4b18f2a2008-04-29 00:06:05 +0000246 cifsFile->invalidHandle = true;
247 cifsFile->srch_inf.endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800249 full_path = build_path_from_dentry(file->f_path.dentry);
Jeff Layton7ffec372010-09-29 19:51:11 -0400250 if (full_path == NULL) {
251 rc = -ENOMEM;
252 goto error_exit;
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Joe Perchesb6b38f72010-04-21 03:50:45 +0000255 cFYI(1, "Full path: %s start at: %lld", full_path, file->f_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Steve French75cf6bd2005-04-28 22:41:04 -0700257ffirst_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /* test for Unix extensions */
Steve Frenchc18c8422007-07-18 23:21:09 +0000259 /* but now check for them on the share/mount not on the SMB session */
260/* if (pTcon->ses->capabilities & CAP_UNIX) { */
Steve Frenchad7a2922008-02-07 23:25:02 +0000261 if (pTcon->unix_ext)
Steve French5bafd762006-06-07 00:18:43 +0000262 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
Steve Frenchad7a2922008-02-07 23:25:02 +0000263 else if ((pTcon->ses->capabilities &
Steve French5bafd762006-06-07 00:18:43 +0000264 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
265 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
267 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
268 } else /* not srvinos - BB fixme add check for backlevel? */ {
269 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
270 }
271
Steve French38702532007-07-08 15:40:40 +0000272 rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
Steve French737b7582005-04-28 22:41:06 -0700273 &cifsFile->netfid, &cifsFile->srch_inf,
Steve French38702532007-07-08 15:40:40 +0000274 cifs_sb->mnt_cifs_flags &
Steve Frencheafe8702005-09-15 21:47:30 -0700275 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
Steve French4523cc32007-04-30 20:13:06 +0000276 if (rc == 0)
Steve French4b18f2a2008-04-29 00:06:05 +0000277 cifsFile->invalidHandle = false;
Steve Frenche836f012009-05-01 16:20:35 +0000278 /* BB add following call to handle readdir on new NTFS symlink errors
Steve French0e0d2cf2009-05-01 05:27:32 +0000279 else if STATUS_STOPPED_ON_SYMLINK
280 call get_symlink_reparse_path and retry with new path */
281 else if ((rc == -EOPNOTSUPP) &&
Steve French75cf6bd2005-04-28 22:41:04 -0700282 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
283 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
284 goto ffirst_retry;
285 }
Jeff Layton7ffec372010-09-29 19:51:11 -0400286error_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 kfree(full_path);
Jeff Layton7ffec372010-09-29 19:51:11 -0400288 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return rc;
290}
291
292/* return length of unicode string in bytes */
293static int cifs_unicode_bytelen(char *str)
294{
295 int len;
Steve French63d25832007-11-05 21:46:10 +0000296 __le16 *ustr = (__le16 *)str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Steve French38702532007-07-08 15:40:40 +0000298 for (len = 0; len <= PATH_MAX; len++) {
Steve French4523cc32007-04-30 20:13:06 +0000299 if (ustr[len] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return len << 1;
301 }
Joe Perchesb6b38f72010-04-21 03:50:45 +0000302 cFYI(1, "Unicode string longer than PATH_MAX found");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return len << 1;
304}
305
Steve French5bafd762006-06-07 00:18:43 +0000306static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Steve French38702532007-07-08 15:40:40 +0000308 char *new_entry;
Steve Frenchad7a2922008-02-07 23:25:02 +0000309 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Steve French4523cc32007-04-30 20:13:06 +0000311 if (level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000312 FIND_FILE_STANDARD_INFO *pfData;
Steve French5bafd762006-06-07 00:18:43 +0000313 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
314
315 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
316 pfData->FileNameLength;
317 } else
318 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000319 cFYI(1, "new entry %p old entry %p", new_entry, old_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 /* validate that new_entry is not past end of SMB */
Steve French4523cc32007-04-30 20:13:06 +0000321 if (new_entry >= end_of_smb) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000322 cERROR(1, "search entry %p began after end of SMB %p old entry %p",
323 new_entry, end_of_smb, old_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return NULL;
Steve French4523cc32007-04-30 20:13:06 +0000325 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
Steve French38702532007-07-08 15:40:40 +0000326 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
327 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
Steve French5bafd762006-06-07 00:18:43 +0000328 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000329 cERROR(1, "search entry %p extends after end of SMB %p",
330 new_entry, end_of_smb);
Steve French09d1db52005-04-28 22:41:08 -0700331 return NULL;
Steve French38702532007-07-08 15:40:40 +0000332 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return new_entry;
334
335}
336
337#define UNICODE_DOT cpu_to_le16(0x2e)
338
339/* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
340static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
341{
342 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000343 char *filename = NULL;
344 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Steve French4523cc32007-04-30 20:13:06 +0000346 if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000347 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 filename = &pFindData->FileName[0];
Steve French4523cc32007-04-30 20:13:06 +0000349 if (cfile->srch_inf.unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 len = cifs_unicode_bytelen(filename);
351 } else {
352 /* BB should we make this strnlen of PATH_MAX? */
353 len = strnlen(filename, 5);
354 }
Steve French4523cc32007-04-30 20:13:06 +0000355 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000356 FILE_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 (FILE_DIRECTORY_INFO *)current_entry;
358 filename = &pFindData->FileName[0];
359 len = le32_to_cpu(pFindData->FileNameLength);
Steve French38702532007-07-08 15:40:40 +0000360 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000361 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000362 FILE_FULL_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 (FILE_FULL_DIRECTORY_INFO *)current_entry;
364 filename = &pFindData->FileName[0];
365 len = le32_to_cpu(pFindData->FileNameLength);
Steve French4523cc32007-04-30 20:13:06 +0000366 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000367 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000368 SEARCH_ID_FULL_DIR_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
370 filename = &pFindData->FileName[0];
371 len = le32_to_cpu(pFindData->FileNameLength);
Steve French38702532007-07-08 15:40:40 +0000372 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000373 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000374 FILE_BOTH_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
376 filename = &pFindData->FileName[0];
377 len = le32_to_cpu(pFindData->FileNameLength);
Steve French4523cc32007-04-30 20:13:06 +0000378 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000379 FIND_FILE_STANDARD_INFO *pFindData =
Steve French5bafd762006-06-07 00:18:43 +0000380 (FIND_FILE_STANDARD_INFO *)current_entry;
381 filename = &pFindData->FileName[0];
Steve French5ddaa682006-08-15 13:35:48 +0000382 len = pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 } else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000384 cFYI(1, "Unknown findfirst level %d",
385 cfile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387
Steve French4523cc32007-04-30 20:13:06 +0000388 if (filename) {
389 if (cfile->srch_inf.unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 __le16 *ufilename = (__le16 *)filename;
Steve French4523cc32007-04-30 20:13:06 +0000391 if (len == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /* check for . */
Steve French4523cc32007-04-30 20:13:06 +0000393 if (ufilename[0] == UNICODE_DOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 rc = 1;
Steve French4523cc32007-04-30 20:13:06 +0000395 } else if (len == 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /* check for .. */
Steve French4523cc32007-04-30 20:13:06 +0000397 if ((ufilename[0] == UNICODE_DOT)
Steve French38702532007-07-08 15:40:40 +0000398 && (ufilename[1] == UNICODE_DOT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 rc = 2;
400 }
401 } else /* ASCII */ {
Steve French4523cc32007-04-30 20:13:06 +0000402 if (len == 1) {
Steve French38702532007-07-08 15:40:40 +0000403 if (filename[0] == '.')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 rc = 1;
Steve French4523cc32007-04-30 20:13:06 +0000405 } else if (len == 2) {
Steve French790fe572007-07-07 19:25:05 +0000406 if ((filename[0] == '.') && (filename[1] == '.'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 rc = 2;
408 }
409 }
410 }
411
412 return rc;
413}
414
Steve Frencheafe8702005-09-15 21:47:30 -0700415/* Check if directory that we are searching has changed so we can decide
416 whether we can use the cached search results from the previous search */
Steve French38702532007-07-08 15:40:40 +0000417static int is_dir_changed(struct file *file)
Steve Frencheafe8702005-09-15 21:47:30 -0700418{
Christoph Hellwigc33f8d32007-04-02 18:47:20 +0000419 struct inode *inode = file->f_path.dentry->d_inode;
420 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
Steve Frencheafe8702005-09-15 21:47:30 -0700421
Christoph Hellwigc33f8d32007-04-02 18:47:20 +0000422 if (cifsInfo->time == 0)
Steve Frencheafe8702005-09-15 21:47:30 -0700423 return 1; /* directory was changed, perhaps due to unlink */
424 else
425 return 0;
426
427}
428
Steve French0752f152008-10-07 20:03:33 +0000429static int cifs_save_resume_key(const char *current_entry,
430 struct cifsFileInfo *cifsFile)
431{
432 int rc = 0;
433 unsigned int len = 0;
434 __u16 level;
435 char *filename;
436
437 if ((cifsFile == NULL) || (current_entry == NULL))
438 return -EINVAL;
439
440 level = cifsFile->srch_inf.info_level;
441
442 if (level == SMB_FIND_FILE_UNIX) {
443 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
444
445 filename = &pFindData->FileName[0];
446 if (cifsFile->srch_inf.unicode) {
447 len = cifs_unicode_bytelen(filename);
448 } else {
449 /* BB should we make this strnlen of PATH_MAX? */
450 len = strnlen(filename, PATH_MAX);
451 }
452 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
453 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
454 FILE_DIRECTORY_INFO *pFindData =
455 (FILE_DIRECTORY_INFO *)current_entry;
456 filename = &pFindData->FileName[0];
457 len = le32_to_cpu(pFindData->FileNameLength);
458 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
459 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
460 FILE_FULL_DIRECTORY_INFO *pFindData =
461 (FILE_FULL_DIRECTORY_INFO *)current_entry;
462 filename = &pFindData->FileName[0];
463 len = le32_to_cpu(pFindData->FileNameLength);
464 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
465 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
466 SEARCH_ID_FULL_DIR_INFO *pFindData =
467 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
468 filename = &pFindData->FileName[0];
469 len = le32_to_cpu(pFindData->FileNameLength);
470 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
471 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
472 FILE_BOTH_DIRECTORY_INFO *pFindData =
473 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
474 filename = &pFindData->FileName[0];
475 len = le32_to_cpu(pFindData->FileNameLength);
476 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
477 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
478 FIND_FILE_STANDARD_INFO *pFindData =
479 (FIND_FILE_STANDARD_INFO *)current_entry;
480 filename = &pFindData->FileName[0];
481 /* one byte length, no name conversion */
482 len = (unsigned int)pFindData->FileNameLength;
483 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
484 } else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000485 cFYI(1, "Unknown findfirst level %d", level);
Steve French0752f152008-10-07 20:03:33 +0000486 return -EINVAL;
487 }
488 cifsFile->srch_inf.resume_name_len = len;
489 cifsFile->srch_inf.presume_name = filename;
490 return rc;
491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493/* find the corresponding entry in the search */
494/* Note that the SMB server returns search entries for . and .. which
495 complicates logic here if we choose to parse for them and we do not
496 assume that they are located in the findfirst return buffer.*/
497/* We start counting in the buffer with entry 2 and increment for every
498 entry (do not increment for . or .. entry) */
499static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
Steve French38702532007-07-08 15:40:40 +0000500 struct file *file, char **ppCurrentEntry, int *num_to_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 int rc = 0;
503 int pos_in_buf = 0;
504 loff_t first_entry_in_buffer;
505 loff_t index_to_find = file->f_pos;
Steve French38702532007-07-08 15:40:40 +0000506 struct cifsFileInfo *cifsFile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /* check if index in the buffer */
Steve French50c2f752007-07-13 00:33:32 +0000508
Steve French38702532007-07-08 15:40:40 +0000509 if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
Steve Frencheafe8702005-09-15 21:47:30 -0700510 (num_to_ret == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return -ENOENT;
Steve French50c2f752007-07-13 00:33:32 +0000512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 *ppCurrentEntry = NULL;
Steve French38702532007-07-08 15:40:40 +0000514 first_entry_in_buffer =
515 cifsFile->srch_inf.index_of_last_entry -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 cifsFile->srch_inf.entries_in_buffer;
Steve French60808232006-04-22 15:53:05 +0000517
518 /* if first entry in buf is zero then is first buffer
519 in search response data which means it is likely . and ..
520 will be in this buffer, although some servers do not return
521 . and .. for the root of a drive and for those we need
522 to start two entries earlier */
523
Steve French39798772006-05-31 22:40:51 +0000524 dump_cifs_file_struct(file, "In fce ");
Steve French38702532007-07-08 15:40:40 +0000525 if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
526 is_dir_changed(file)) ||
Steve Frencheafe8702005-09-15 21:47:30 -0700527 (index_to_find < first_entry_in_buffer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /* close and restart search */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000529 cFYI(1, "search backing up - close and restart search");
Jeff Layton44772882010-10-15 15:34:03 -0400530 spin_lock(&cifs_file_list_lock);
Steve French77c57ec2008-05-13 21:39:32 +0000531 if (!cifsFile->srch_inf.endOfSearch &&
532 !cifsFile->invalidHandle) {
533 cifsFile->invalidHandle = true;
Jeff Layton44772882010-10-15 15:34:03 -0400534 spin_unlock(&cifs_file_list_lock);
Steve French77c57ec2008-05-13 21:39:32 +0000535 CIFSFindClose(xid, pTcon, cifsFile->netfid);
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000536 } else
Jeff Layton44772882010-10-15 15:34:03 -0400537 spin_unlock(&cifs_file_list_lock);
Steve French4523cc32007-04-30 20:13:06 +0000538 if (cifsFile->srch_inf.ntwrk_buf_start) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000539 cFYI(1, "freeing SMB ff cache buf on search rewind");
Steve French4523cc32007-04-30 20:13:06 +0000540 if (cifsFile->srch_inf.smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +0000541 cifs_small_buf_release(cifsFile->srch_inf.
542 ntwrk_buf_start);
543 else
544 cifs_buf_release(cifsFile->srch_inf.
545 ntwrk_buf_start);
Shirish Pargaonkar76c510a2008-07-24 14:48:33 +0000546 cifsFile->srch_inf.ntwrk_buf_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
Steve French38702532007-07-08 15:40:40 +0000548 rc = initiate_cifs_search(xid, file);
Steve French4523cc32007-04-30 20:13:06 +0000549 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000550 cFYI(1, "error %d reinitiating a search on rewind",
551 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return rc;
553 }
Jeff Laytona364bc02008-10-21 14:42:13 +0000554 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556
Steve French38702532007-07-08 15:40:40 +0000557 while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
Steve French4b18f2a2008-04-29 00:06:05 +0000558 (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000559 cFYI(1, "calling findnext2");
Steve French38702532007-07-08 15:40:40 +0000560 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
Steve Frenchdfb75332005-06-22 17:13:47 -0700561 &cifsFile->srch_inf);
Jeff Laytona364bc02008-10-21 14:42:13 +0000562 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
Steve French4523cc32007-04-30 20:13:06 +0000563 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return -ENOENT;
565 }
Steve French4523cc32007-04-30 20:13:06 +0000566 if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* we found the buffer that contains the entry */
568 /* scan and find it */
569 int i;
Steve French38702532007-07-08 15:40:40 +0000570 char *current_entry;
571 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 smbCalcSize((struct smb_hdr *)
573 cifsFile->srch_inf.ntwrk_buf_start);
Steve French60808232006-04-22 15:53:05 +0000574
575 current_entry = cifsFile->srch_inf.srch_entries_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
577 - cifsFile->srch_inf.entries_in_buffer;
578 pos_in_buf = index_to_find - first_entry_in_buffer;
Joe Perchesb6b38f72010-04-21 03:50:45 +0000579 cFYI(1, "found entry - pos_in_buf %d", pos_in_buf);
Steve French5bafd762006-06-07 00:18:43 +0000580
Steve Frenchad7a2922008-02-07 23:25:02 +0000581 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
Steve Frenchdfb75332005-06-22 17:13:47 -0700582 /* go entry by entry figuring out which is first */
Steve French38702532007-07-08 15:40:40 +0000583 current_entry = nxt_dir_entry(current_entry, end_of_smb,
Steve French5bafd762006-06-07 00:18:43 +0000584 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
Steve French790fe572007-07-07 19:25:05 +0000586 if ((current_entry == NULL) && (i < pos_in_buf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* BB fixme - check if we should flag this error */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000588 cERROR(1, "reached end of buf searching for pos in buf"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 " %d index to find %lld rc %d",
Joe Perchesb6b38f72010-04-21 03:50:45 +0000590 pos_in_buf, index_to_find, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592 rc = 0;
593 *ppCurrentEntry = current_entry;
594 } else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000595 cFYI(1, "index not in buffer - could not findnext into it");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return 0;
597 }
598
Steve French790fe572007-07-07 19:25:05 +0000599 if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000600 cFYI(1, "can not return entries pos_in_buf beyond last");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 *num_to_ret = 0;
602 } else
603 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 return rc;
606}
607
608/* inode num, inode type and filename returned */
609static int cifs_get_name_from_search_buf(struct qstr *pqst,
610 char *current_entry, __u16 level, unsigned int unicode,
Jeff Layton18295792009-04-30 20:45:45 -0400611 struct cifs_sb_info *cifs_sb, unsigned int max_len, __u64 *pinum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 int rc = 0;
614 unsigned int len = 0;
Steve French38702532007-07-08 15:40:40 +0000615 char *filename;
616 struct nls_table *nlt = cifs_sb->local_nls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 *pinum = 0;
619
Steve French790fe572007-07-07 19:25:05 +0000620 if (level == SMB_FIND_FILE_UNIX) {
Steve French38702532007-07-08 15:40:40 +0000621 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 filename = &pFindData->FileName[0];
Steve French790fe572007-07-07 19:25:05 +0000624 if (unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 len = cifs_unicode_bytelen(filename);
626 } else {
627 /* BB should we make this strnlen of PATH_MAX? */
628 len = strnlen(filename, PATH_MAX);
629 }
630
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400631 *pinum = le64_to_cpu(pFindData->basic.UniqueId);
Steve French790fe572007-07-07 19:25:05 +0000632 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000633 FILE_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 (FILE_DIRECTORY_INFO *)current_entry;
635 filename = &pFindData->FileName[0];
636 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000637 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000638 FILE_FULL_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 (FILE_FULL_DIRECTORY_INFO *)current_entry;
640 filename = &pFindData->FileName[0];
641 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000642 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
Steve French38702532007-07-08 15:40:40 +0000643 SEARCH_ID_FULL_DIR_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
645 filename = &pFindData->FileName[0];
646 len = le32_to_cpu(pFindData->FileNameLength);
Steve French85a6dac2009-04-01 05:22:00 +0000647 *pinum = le64_to_cpu(pFindData->UniqueId);
Steve French790fe572007-07-07 19:25:05 +0000648 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000649 FILE_BOTH_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
651 filename = &pFindData->FileName[0];
652 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000653 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000654 FIND_FILE_STANDARD_INFO *pFindData =
Steve French5bafd762006-06-07 00:18:43 +0000655 (FIND_FILE_STANDARD_INFO *)current_entry;
656 filename = &pFindData->FileName[0];
657 /* one byte length, no name conversion */
658 len = (unsigned int)pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 } else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000660 cFYI(1, "Unknown findfirst level %d", level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return -EINVAL;
662 }
Steve French5bafd762006-06-07 00:18:43 +0000663
Steve French790fe572007-07-07 19:25:05 +0000664 if (len > max_len) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000665 cERROR(1, "bad search response length %d past smb end", len);
Steve French5bafd762006-06-07 00:18:43 +0000666 return -EINVAL;
667 }
668
Steve French790fe572007-07-07 19:25:05 +0000669 if (unicode) {
Jeff Laytonf5884162009-04-30 07:18:00 -0400670 pqst->len = cifs_from_ucs2((char *) pqst->name,
671 (__le16 *) filename,
Jeff Layton18295792009-04-30 20:45:45 -0400672 UNICODE_NAME_MAX,
673 min(len, max_len), nlt,
Jeff Laytonf5884162009-04-30 07:18:00 -0400674 cifs_sb->mnt_cifs_flags &
675 CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Laytonf12f98d2010-02-05 13:14:00 -0500676 pqst->len -= nls_nullsize(nlt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 } else {
678 pqst->name = filename;
679 pqst->len = len;
680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return rc;
682}
683
Jeff Layton18295792009-04-30 20:45:45 -0400684static int cifs_filldir(char *pfindEntry, struct file *file, filldir_t filldir,
685 void *direntry, char *scratch_buf, unsigned int max_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 int rc = 0;
688 struct qstr qstring;
Steve French38702532007-07-08 15:40:40 +0000689 struct cifsFileInfo *pCifsF;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400690 u64 inum;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400691 ino_t ino;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400692 struct super_block *sb;
Steve French38702532007-07-08 15:40:40 +0000693 struct cifs_sb_info *cifs_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 struct dentry *tmp_dentry;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400695 struct cifs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 /* get filename and len into qstring */
698 /* get dentry */
699 /* decide whether to create and populate ionde */
Steve French790fe572007-07-07 19:25:05 +0000700 if ((direntry == NULL) || (file == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return -EINVAL;
702
703 pCifsF = file->private_data;
Steve French50c2f752007-07-13 00:33:32 +0000704
Steve French790fe572007-07-07 19:25:05 +0000705 if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return -ENOENT;
707
Steve French38702532007-07-08 15:40:40 +0000708 rc = cifs_entry_is_dot(pfindEntry, pCifsF);
Steve French60808232006-04-22 15:53:05 +0000709 /* skip . and .. since we added them first */
Steve French790fe572007-07-07 19:25:05 +0000710 if (rc != 0)
Steve French60808232006-04-22 15:53:05 +0000711 return 0;
712
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400713 sb = file->f_path.dentry->d_sb;
714 cifs_sb = CIFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 qstring.name = scratch_buf;
Steve French38702532007-07-08 15:40:40 +0000717 rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 pCifsF->srch_inf.info_level,
Steve French38702532007-07-08 15:40:40 +0000719 pCifsF->srch_inf.unicode, cifs_sb,
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400720 max_len, &inum /* returned */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Steve French790fe572007-07-07 19:25:05 +0000722 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return rc;
724
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400725 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400726 cifs_unix_basic_to_fattr(&fattr,
727 &((FILE_UNIX_INFO *) pfindEntry)->basic,
728 cifs_sb);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400729 else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
730 cifs_std_info_to_fattr(&fattr, (FIND_FILE_STANDARD_INFO *)
731 pfindEntry, cifs_sb);
732 else
733 cifs_dir_info_to_fattr(&fattr, (FILE_DIRECTORY_INFO *)
734 pfindEntry, cifs_sb);
Jeff Layton132ac7b2009-02-10 07:33:57 -0500735
Jeff Laytonec06aed2009-11-06 14:18:29 -0500736 if (inum && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400737 fattr.cf_uniqueid = inum;
Jeff Laytonec06aed2009-11-06 14:18:29 -0500738 } else {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400739 fattr.cf_uniqueid = iunique(sb, ROOT_I);
Jeff Laytonec06aed2009-11-06 14:18:29 -0500740 cifs_autodisable_serverino(cifs_sb);
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200743 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) &&
744 CIFSCouldBeMFSymlink(&fattr))
745 /*
746 * trying to get the type and mode can be slow,
747 * so just call those regular files for now, and mark
748 * for reval
749 */
750 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
751
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400752 ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
753 tmp_dentry = cifs_readdir_lookup(file->f_dentry, &qstring, &fattr);
Steve French50c2f752007-07-13 00:33:32 +0000754
Steve French38702532007-07-08 15:40:40 +0000755 rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400756 ino, fattr.cf_dtype);
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 dput(tmp_dentry);
759 return rc;
760}
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
764{
765 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000766 int xid, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 struct cifs_sb_info *cifs_sb;
768 struct cifsTconInfo *pTcon;
769 struct cifsFileInfo *cifsFile = NULL;
Steve French38702532007-07-08 15:40:40 +0000770 char *current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 int num_to_fill = 0;
Steve French38702532007-07-08 15:40:40 +0000772 char *tmp_buf = NULL;
Steve French50c2f752007-07-13 00:33:32 +0000773 char *end_of_smb;
Jeff Layton18295792009-04-30 20:45:45 -0400774 unsigned int max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 xid = GetXid();
777
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800778 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Suresh Jayaraman6221ddd2010-10-01 21:23:33 +0530780 /*
781 * Ensure FindFirst doesn't fail before doing filldir() for '.' and
782 * '..'. Otherwise we won't be able to notify VFS in case of failure.
783 */
784 if (file->private_data == NULL) {
785 rc = initiate_cifs_search(xid, file);
786 cFYI(1, "initiate cifs search rc %d", rc);
787 if (rc)
788 goto rddir2_exit;
789 }
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 switch ((int) file->f_pos) {
792 case 0:
Steve French60808232006-04-22 15:53:05 +0000793 if (filldir(direntry, ".", 1, file->f_pos,
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800794 file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000795 cERROR(1, "Filldir for current dir failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 rc = -ENOMEM;
797 break;
798 }
Steve French60808232006-04-22 15:53:05 +0000799 file->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 case 1:
Steve French60808232006-04-22 15:53:05 +0000801 if (filldir(direntry, "..", 2, file->f_pos,
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800802 file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000803 cERROR(1, "Filldir for parent dir failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 rc = -ENOMEM;
805 break;
806 }
Steve French60808232006-04-22 15:53:05 +0000807 file->f_pos++;
808 default:
Steve French38702532007-07-08 15:40:40 +0000809 /* 1) If search is active,
810 is in current search buffer?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if it before then restart search
812 if after then keep searching till find it */
813
Steve French790fe572007-07-07 19:25:05 +0000814 if (file->private_data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 rc = -EINVAL;
816 FreeXid(xid);
817 return rc;
818 }
819 cifsFile = file->private_data;
820 if (cifsFile->srch_inf.endOfSearch) {
Steve French790fe572007-07-07 19:25:05 +0000821 if (cifsFile->srch_inf.emptyDir) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000822 cFYI(1, "End of search, empty dir");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 rc = 0;
824 break;
825 }
826 } /* else {
Steve French4b18f2a2008-04-29 00:06:05 +0000827 cifsFile->invalidHandle = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 CIFSFindClose(xid, pTcon, cifsFile->netfid);
Steve Frenchaaa9bbe2008-05-23 17:38:32 +0000829 } */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Jeff Layton13cfb732010-09-29 19:51:11 -0400831 pTcon = tlink_tcon(cifsFile->tlink);
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) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000835 cFYI(1, "fce error %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto rddir2_exit;
837 } else if (current_entry != NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000838 cFYI(1, "entry %lld found", file->f_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 } else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000840 cFYI(1, "could not find entry");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 goto rddir2_exit;
842 }
Joe Perchesb6b38f72010-04-21 03:50:45 +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);
Kulikov Vasiliyf55fdcc2010-07-16 20:15:25 +0400850 if (tmp_buf == NULL) {
851 rc = -ENOMEM;
852 break;
853 }
854
Steve French790fe572007-07-07 19:25:05 +0000855 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
856 if (current_entry == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 /* evaluate whether this case is an error */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000858 cERROR(1, "past SMB end, num to fill %d i %d",
859 num_to_fill, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 break;
861 }
Steve French60808232006-04-22 15:53:05 +0000862 /* if buggy server returns . and .. late do
863 we want to check for that here? */
Steve French5bafd762006-06-07 00:18:43 +0000864 rc = cifs_filldir(current_entry, file,
865 filldir, direntry, tmp_buf, max_len);
Steve French790fe572007-07-07 19:25:05 +0000866 if (rc == -EOVERFLOW) {
Steve French7ca85ba2006-10-30 21:42:57 +0000867 rc = 0;
868 break;
869 }
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 file->f_pos++;
Steve French790fe572007-07-07 19:25:05 +0000872 if (file->f_pos ==
Steve French39798772006-05-31 22:40:51 +0000873 cifsFile->srch_inf.index_of_last_entry) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000874 cFYI(1, "last entry in buf at pos %lld %s",
875 file->f_pos, tmp_buf);
Steve French790fe572007-07-07 19:25:05 +0000876 cifs_save_resume_key(current_entry, cifsFile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 break;
Steve French790fe572007-07-07 19:25:05 +0000878 } else
879 current_entry =
Steve French5bafd762006-06-07 00:18:43 +0000880 nxt_dir_entry(current_entry, end_of_smb,
881 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883 kfree(tmp_buf);
884 break;
885 } /* end switch */
886
887rddir2_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 FreeXid(xid);
889 return rc;
890}