blob: 231aa6953f834e65efb1674b4356d6f3a1dc326f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/readdir.c
3 *
4 * Directory search handling
Steve French6dc0f872007-07-06 23:13:06 +00005 *
Steve Frenchad7a2922008-02-07 23:25:02 +00006 * Copyright (C) International Business Machines Corp., 2004, 2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Author(s): Steve French (sfrench@us.ibm.com)
8 *
9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23#include <linux/fs.h>
Dave Kleikamp273d81d2006-06-01 19:41:23 +000024#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "cifspdu.h"
27#include "cifsglob.h"
28#include "cifsproto.h"
29#include "cifs_unicode.h"
30#include "cifs_debug.h"
31#include "cifs_fs_sb.h"
32#include "cifsfs.h"
33
Jeff Laytonf5884162009-04-30 07:18:00 -040034/*
35 * To be safe - for UCS to UTF-8 with strings loaded with the rare long
36 * characters alloc more to account for such multibyte target UTF-8
37 * characters.
38 */
39#define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
40
Steve French39798772006-05-31 22:40:51 +000041#ifdef CONFIG_CIFS_DEBUG2
42static void dump_cifs_file_struct(struct file *file, char *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Steve French6dc0f872007-07-06 23:13:06 +000044 struct cifsFileInfo *cf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Steve French4523cc32007-04-30 20:13:06 +000046 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 cf = file->private_data;
Steve French4523cc32007-04-30 20:13:06 +000048 if (cf == NULL) {
Steve French6dc0f872007-07-06 23:13:06 +000049 cFYI(1, ("empty cifs private file data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 return;
51 }
Steve Frenchad7a2922008-02-07 23:25:02 +000052 if (cf->invalidHandle)
Steve French6dc0f872007-07-06 23:13:06 +000053 cFYI(1, ("invalid handle"));
Steve Frenchad7a2922008-02-07 23:25:02 +000054 if (cf->srch_inf.endOfSearch)
Steve French6dc0f872007-07-06 23:13:06 +000055 cFYI(1, ("end of search"));
Steve Frenchad7a2922008-02-07 23:25:02 +000056 if (cf->srch_inf.emptyDir)
Steve French6dc0f872007-07-06 23:13:06 +000057 cFYI(1, ("empty dir"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
Steve French39798772006-05-31 22:40:51 +000059}
Steve French90c81e02008-02-12 20:32:36 +000060#else
61static inline void dump_cifs_file_struct(struct file *file, char *label)
62{
63}
Steve French39798772006-05-31 22:40:51 +000064#endif /* DEBUG2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Jeff Laytoncc0bad72009-06-25 00:56:52 -040066/*
67 * Find the dentry that matches "name". If there isn't one, create one. If it's
68 * a negative dentry or the uniqueid changed, then drop it and recreate it.
69 */
70static struct dentry *
71cifs_readdir_lookup(struct dentry *parent, struct qstr *name,
72 struct cifs_fattr *fattr)
73{
74 struct dentry *dentry, *alias;
75 struct inode *inode;
76 struct super_block *sb = parent->d_inode->i_sb;
77
78 cFYI(1, ("For %s", name->name));
79
80 dentry = d_lookup(parent, name);
81 if (dentry) {
82 /* FIXME: check for inode number changes? */
83 if (dentry->d_inode != NULL)
84 return dentry;
85 d_drop(dentry);
86 dput(dentry);
87 }
88
89 dentry = d_alloc(parent, name);
90 if (dentry == NULL)
91 return NULL;
92
93 inode = cifs_iget(sb, fattr);
94 if (!inode) {
95 dput(dentry);
96 return NULL;
97 }
98
99 if (CIFS_SB(sb)->tcon->nocase)
100 dentry->d_op = &cifs_ci_dentry_ops;
101 else
102 dentry->d_op = &cifs_dentry_ops;
103
104 alias = d_materialise_unique(dentry, inode);
105 if (alias != NULL) {
106 dput(dentry);
107 if (IS_ERR(alias))
108 return NULL;
109 dentry = alias;
110 }
111
112 return dentry;
113}
114
Jeff Layton132ac7b2009-02-10 07:33:57 -0500115/* Returns 1 if new inode created, 2 if both dentry and inode were */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/* Might check in the future if inode number changed so we can rehash inode */
Jeff Layton132ac7b2009-02-10 07:33:57 -0500117static int
118construct_dentry(struct qstr *qstring, struct file *file,
119 struct inode **ptmp_inode, struct dentry **pnew_dentry,
Jeff Layton950ec522009-02-11 08:08:26 -0500120 __u64 *inum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Jeff Layton132ac7b2009-02-10 07:33:57 -0500122 struct dentry *tmp_dentry = NULL;
123 struct super_block *sb = file->f_path.dentry->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int rc = 0;
125
Steve French966ca922005-04-28 22:41:08 -0700126 cFYI(1, ("For %s", qstring->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800128 tmp_dentry = d_lookup(file->f_path.dentry, qstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (tmp_dentry) {
Jeff Layton132ac7b2009-02-10 07:33:57 -0500130 /* BB: overwrite old name? i.e. tmp_dentry->d_name and
131 * tmp_dentry->d_name.len??
132 */
Steve French6dc0f872007-07-06 23:13:06 +0000133 cFYI(0, ("existing dentry with inode 0x%p",
134 tmp_dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 *ptmp_inode = tmp_dentry->d_inode;
Steve French4523cc32007-04-30 20:13:06 +0000136 if (*ptmp_inode == NULL) {
Jeff Layton132ac7b2009-02-10 07:33:57 -0500137 *ptmp_inode = cifs_new_inode(sb, inum);
Steve French4523cc32007-04-30 20:13:06 +0000138 if (*ptmp_inode == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return rc;
140 rc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142 } else {
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800143 tmp_dentry = d_alloc(file->f_path.dentry, qstring);
Steve French4523cc32007-04-30 20:13:06 +0000144 if (tmp_dentry == NULL) {
Steve French6dc0f872007-07-06 23:13:06 +0000145 cERROR(1, ("Failed allocating dentry"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 *ptmp_inode = NULL;
147 return rc;
148 }
149
Jeff Layton132ac7b2009-02-10 07:33:57 -0500150 if (CIFS_SB(sb)->tcon->nocase)
Steve Frenchb92327f2005-08-22 20:09:43 -0700151 tmp_dentry->d_op = &cifs_ci_dentry_ops;
152 else
153 tmp_dentry->d_op = &cifs_dentry_ops;
Jeff Layton132ac7b2009-02-10 07:33:57 -0500154
155 *ptmp_inode = cifs_new_inode(sb, inum);
Steve French4523cc32007-04-30 20:13:06 +0000156 if (*ptmp_inode == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return rc;
Steve Frenchb835beb2006-09-06 22:02:22 +0000158 rc = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160
161 tmp_dentry->d_time = jiffies;
162 *pnew_dentry = tmp_dentry;
163 return rc;
164}
165
Steve French5bafd762006-06-07 00:18:43 +0000166static void fill_in_inode(struct inode *tmp_inode, int new_buf_type,
Steve French2c2130e12007-10-12 19:10:28 +0000167 char *buf, unsigned int *pobject_type, int isNewInode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Steve French966ca922005-04-28 22:41:08 -0700169 loff_t local_size;
170 struct timespec local_mtime;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
173 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
Steve French5bafd762006-06-07 00:18:43 +0000174 __u32 attr;
175 __u64 allocation_size;
176 __u64 end_of_file;
Jeff Layton4468eb32008-05-22 09:31:40 -0400177 umode_t default_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Steve French966ca922005-04-28 22:41:08 -0700179 /* save mtime and size */
180 local_mtime = tmp_inode->i_mtime;
181 local_size = tmp_inode->i_size;
182
Steve French4523cc32007-04-30 20:13:06 +0000183 if (new_buf_type) {
Steve French5bafd762006-06-07 00:18:43 +0000184 FILE_DIRECTORY_INFO *pfindData = (FILE_DIRECTORY_INFO *)buf;
185
186 attr = le32_to_cpu(pfindData->ExtFileAttributes);
187 allocation_size = le64_to_cpu(pfindData->AllocationSize);
188 end_of_file = le64_to_cpu(pfindData->EndOfFile);
189 tmp_inode->i_atime =
Jeff Layton07119a42009-05-27 09:37:33 -0400190 cifs_NTtimeToUnix(pfindData->LastAccessTime);
Steve French5bafd762006-06-07 00:18:43 +0000191 tmp_inode->i_mtime =
Jeff Layton07119a42009-05-27 09:37:33 -0400192 cifs_NTtimeToUnix(pfindData->LastWriteTime);
Steve French5bafd762006-06-07 00:18:43 +0000193 tmp_inode->i_ctime =
Jeff Layton07119a42009-05-27 09:37:33 -0400194 cifs_NTtimeToUnix(pfindData->ChangeTime);
Steve French5bafd762006-06-07 00:18:43 +0000195 } else { /* legacy, OS2 and DOS style */
Jeff Laytonc4a2c082009-05-27 09:37:33 -0400196 int offset = cifs_sb->tcon->ses->server->timeAdj;
Steve Frenchad7a2922008-02-07 23:25:02 +0000197 FIND_FILE_STANDARD_INFO *pfindData =
Steve French5bafd762006-06-07 00:18:43 +0000198 (FIND_FILE_STANDARD_INFO *)buf;
199
Jeff Laytonc4a2c082009-05-27 09:37:33 -0400200 tmp_inode->i_mtime = cnvrtDosUnixTm(pfindData->LastWriteDate,
201 pfindData->LastWriteTime,
202 offset);
203 tmp_inode->i_atime = cnvrtDosUnixTm(pfindData->LastAccessDate,
204 pfindData->LastAccessTime,
205 offset);
206 tmp_inode->i_ctime = cnvrtDosUnixTm(pfindData->LastWriteDate,
207 pfindData->LastWriteTime,
208 offset);
Steve French5bafd762006-06-07 00:18:43 +0000209 attr = le16_to_cpu(pfindData->Attributes);
210 allocation_size = le32_to_cpu(pfindData->AllocationSize);
211 end_of_file = le32_to_cpu(pfindData->DataSize);
Steve French5bafd762006-06-07 00:18:43 +0000212 }
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /* Linux can not store file creation time unfortunately so ignore it */
Steve French5bafd762006-06-07 00:18:43 +0000215
216 cifsInfo->cifsAttrs = attr;
Steve Frencha6f8de32007-11-08 23:10:32 +0000217#ifdef CONFIG_CIFS_EXPERIMENTAL
218 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
219 /* get more accurate mode via ACL - so force inode refresh */
220 cifsInfo->time = 0;
221 } else
222#endif /* CONFIG_CIFS_EXPERIMENTAL */
223 cifsInfo->time = jiffies;
Steve French5bafd762006-06-07 00:18:43 +0000224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
226 /* 2767 perms - indicate mandatory locking */
Steve French790fe572007-07-07 19:25:05 +0000227 /* BB fill in uid and gid here? with help from winbind?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 or retrieve from NTFS stream extended attribute */
229 if (atomic_read(&cifsInfo->inUse) == 0) {
230 tmp_inode->i_uid = cifs_sb->mnt_uid;
231 tmp_inode->i_gid = cifs_sb->mnt_gid;
Jeff Layton4468eb32008-05-22 09:31:40 -0400232 }
233
234 if (attr & ATTR_DIRECTORY)
235 default_mode = cifs_sb->mnt_dir_mode;
236 else
237 default_mode = cifs_sb->mnt_file_mode;
238
239 /* set initial permissions */
240 if ((atomic_read(&cifsInfo->inUse) == 0) ||
241 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
242 tmp_inode->i_mode = default_mode;
243 else {
244 /* just reenable write bits if !ATTR_READONLY */
245 if ((tmp_inode->i_mode & S_IWUGO) == 0 &&
246 (attr & ATTR_READONLY) == 0)
247 tmp_inode->i_mode |= (S_IWUGO & default_mode);
248
Steve French3020a1f2005-11-18 11:31:10 -0800249 tmp_inode->i_mode &= ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
Jeff Layton4468eb32008-05-22 09:31:40 -0400252 /* clear write bits if ATTR_READONLY is set */
253 if (attr & ATTR_READONLY)
254 tmp_inode->i_mode &= ~S_IWUGO;
255
256 /* set inode type */
257 if ((attr & ATTR_SYSTEM) &&
258 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
Steve French3020a1f2005-11-18 11:31:10 -0800259 if (end_of_file == 0) {
Steve French3020a1f2005-11-18 11:31:10 -0800260 tmp_inode->i_mode |= S_IFIFO;
Jeff Layton4468eb32008-05-22 09:31:40 -0400261 *pobject_type = DT_FIFO;
Steve French3020a1f2005-11-18 11:31:10 -0800262 } else {
Jeff Layton4468eb32008-05-22 09:31:40 -0400263 /*
264 * trying to get the type can be slow, so just call
265 * this a regular file for now, and mark for reval
266 */
Steve French790fe572007-07-07 19:25:05 +0000267 tmp_inode->i_mode |= S_IFREG;
Jeff Layton4468eb32008-05-22 09:31:40 -0400268 *pobject_type = DT_REG;
Steve French790fe572007-07-07 19:25:05 +0000269 cifsInfo->time = 0;
Steve French3020a1f2005-11-18 11:31:10 -0800270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 } else {
Jeff Layton4468eb32008-05-22 09:31:40 -0400272 if (attr & ATTR_DIRECTORY) {
273 tmp_inode->i_mode |= S_IFDIR;
274 *pobject_type = DT_DIR;
275 } else {
276 tmp_inode->i_mode |= S_IFREG;
277 *pobject_type = DT_REG;
278 }
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 /* can not fill in nlink here as in qpathinfo version and Unx search */
Steve Frenchad7a2922008-02-07 23:25:02 +0000282 if (atomic_read(&cifsInfo->inUse) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 atomic_set(&cifsInfo->inUse, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Jeff Laytonfbec9ab2009-04-03 13:44:00 -0400285 cifsInfo->server_eof = end_of_file;
Steve French3677db12007-02-26 16:46:11 +0000286 spin_lock(&tmp_inode->i_lock);
Steve French7ba52632007-02-08 18:14:13 +0000287 if (is_size_safe_to_change(cifsInfo, end_of_file)) {
Steve French790fe572007-07-07 19:25:05 +0000288 /* can not safely change the file size here if the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 client is writing to it due to potential races */
290 i_size_write(tmp_inode, end_of_file);
291
292 /* 512 bytes (2**9) is the fake blocksize that must be used */
293 /* for this calculation, even though the reported blocksize is larger */
294 tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
295 }
Steve French3677db12007-02-26 16:46:11 +0000296 spin_unlock(&tmp_inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 if (allocation_size < end_of_file)
299 cFYI(1, ("May be sparse file, allocation less than file size"));
Theodore Ts'oba52de12006-09-27 01:50:49 -0700300 cFYI(1, ("File Size %ld and blocks %llu",
Andrew Morton5515eff2006-03-26 01:37:53 -0800301 (unsigned long)tmp_inode->i_size,
Theodore Ts'oba52de12006-09-27 01:50:49 -0700302 (unsigned long long)tmp_inode->i_blocks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (S_ISREG(tmp_inode->i_mode)) {
Steve French966ca922005-04-28 22:41:08 -0700304 cFYI(1, ("File inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 tmp_inode->i_op = &cifs_file_inode_ops;
Steve French4523cc32007-04-30 20:13:06 +0000306 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
307 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
Steve French8b94bcb2005-11-11 11:41:00 -0800308 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
309 else
310 tmp_inode->i_fop = &cifs_file_direct_ops;
Steve French4523cc32007-04-30 20:13:06 +0000311 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
Steve French8b94bcb2005-11-11 11:41:00 -0800312 tmp_inode->i_fop = &cifs_file_nobrl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 else
314 tmp_inode->i_fop = &cifs_file_ops;
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800315
Steve French4523cc32007-04-30 20:13:06 +0000316 if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
Steve Frenchbfa0d752005-08-31 21:50:37 -0700317 (cifs_sb->tcon->ses->server->maxBuf <
Dave Kleikamp273d81d2006-06-01 19:41:23 +0000318 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
319 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
320 else
321 tmp_inode->i_data.a_ops = &cifs_addr_ops;
322
Steve French4523cc32007-04-30 20:13:06 +0000323 if (isNewInode)
Steve Frenchdfb75332005-06-22 17:13:47 -0700324 return; /* No sense invalidating pages for new inode
325 since have not started caching readahead file
326 data yet */
Steve French966ca922005-04-28 22:41:08 -0700327
328 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
329 (local_size == tmp_inode->i_size)) {
330 cFYI(1, ("inode exists but unchanged"));
331 } else {
332 /* file may have changed on server */
333 cFYI(1, ("invalidate inode, readdir detected change"));
334 invalidate_remote_inode(tmp_inode);
335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 } else if (S_ISDIR(tmp_inode->i_mode)) {
Steve French966ca922005-04-28 22:41:08 -0700337 cFYI(1, ("Directory inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 tmp_inode->i_op = &cifs_dir_inode_ops;
339 tmp_inode->i_fop = &cifs_dir_ops;
340 } else if (S_ISLNK(tmp_inode->i_mode)) {
Steve French966ca922005-04-28 22:41:08 -0700341 cFYI(1, ("Symbolic Link inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 tmp_inode->i_op = &cifs_symlink_inode_ops;
343 } else {
Steve French966ca922005-04-28 22:41:08 -0700344 cFYI(1, ("Init special inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 init_special_inode(tmp_inode, tmp_inode->i_mode,
346 tmp_inode->i_rdev);
347 }
348}
349
Steve French0e0d2cf2009-05-01 05:27:32 +0000350/* BB eventually need to add the following helper function to
351 resolve NT_STATUS_STOPPED_ON_SYMLINK return code when
352 we try to do FindFirst on (NTFS) directory symlinks */
353/*
354int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
355 int xid)
356{
357 __u16 fid;
358 int len;
359 int oplock = 0;
360 int rc;
361 struct cifsTconInfo *ptcon = cifs_sb->tcon;
362 char *tmpbuffer;
363
364 rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ,
365 OPEN_REPARSE_POINT, &fid, &oplock, NULL,
366 cifs_sb->local_nls,
367 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
368 if (!rc) {
369 tmpbuffer = kmalloc(maxpath);
370 rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path,
371 tmpbuffer,
372 maxpath -1,
373 fid,
374 cifs_sb->local_nls);
375 if (CIFSSMBClose(xid, ptcon, fid)) {
376 cFYI(1, ("Error closing temporary reparsepoint open)"));
377 }
378 }
379}
380 */
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382static int initiate_cifs_search(const int xid, struct file *file)
383{
384 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000385 char *full_path;
386 struct cifsFileInfo *cifsFile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct cifs_sb_info *cifs_sb;
388 struct cifsTconInfo *pTcon;
389
Steve French4523cc32007-04-30 20:13:06 +0000390 if (file->private_data == NULL) {
Steve French38702532007-07-08 15:40:40 +0000391 file->private_data =
392 kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394
Steve French4523cc32007-04-30 20:13:06 +0000395 if (file->private_data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 cifsFile = file->private_data;
Steve French4b18f2a2008-04-29 00:06:05 +0000398 cifsFile->invalidHandle = true;
399 cifsFile->srch_inf.endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800401 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
Steve French4523cc32007-04-30 20:13:06 +0000402 if (cifs_sb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return -EINVAL;
404
405 pTcon = cifs_sb->tcon;
Steve French4523cc32007-04-30 20:13:06 +0000406 if (pTcon == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return -EINVAL;
408
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800409 full_path = build_path_from_dentry(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Steve Frenchad7a2922008-02-07 23:25:02 +0000411 if (full_path == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Steve French966ca922005-04-28 22:41:08 -0700414 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Steve French75cf6bd2005-04-28 22:41:04 -0700416ffirst_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* test for Unix extensions */
Steve Frenchc18c8422007-07-18 23:21:09 +0000418 /* but now check for them on the share/mount not on the SMB session */
419/* if (pTcon->ses->capabilities & CAP_UNIX) { */
Steve Frenchad7a2922008-02-07 23:25:02 +0000420 if (pTcon->unix_ext)
Steve French5bafd762006-06-07 00:18:43 +0000421 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
Steve Frenchad7a2922008-02-07 23:25:02 +0000422 else if ((pTcon->ses->capabilities &
Steve French5bafd762006-06-07 00:18:43 +0000423 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
424 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
426 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
427 } else /* not srvinos - BB fixme add check for backlevel? */ {
428 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
429 }
430
Steve French38702532007-07-08 15:40:40 +0000431 rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
Steve French737b7582005-04-28 22:41:06 -0700432 &cifsFile->netfid, &cifsFile->srch_inf,
Steve French38702532007-07-08 15:40:40 +0000433 cifs_sb->mnt_cifs_flags &
Steve Frencheafe8702005-09-15 21:47:30 -0700434 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
Steve French4523cc32007-04-30 20:13:06 +0000435 if (rc == 0)
Steve French4b18f2a2008-04-29 00:06:05 +0000436 cifsFile->invalidHandle = false;
Steve Frenche836f012009-05-01 16:20:35 +0000437 /* BB add following call to handle readdir on new NTFS symlink errors
Steve French0e0d2cf2009-05-01 05:27:32 +0000438 else if STATUS_STOPPED_ON_SYMLINK
439 call get_symlink_reparse_path and retry with new path */
440 else if ((rc == -EOPNOTSUPP) &&
Steve French75cf6bd2005-04-28 22:41:04 -0700441 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
442 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
443 goto ffirst_retry;
444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 kfree(full_path);
446 return rc;
447}
448
449/* return length of unicode string in bytes */
450static int cifs_unicode_bytelen(char *str)
451{
452 int len;
Steve French63d25832007-11-05 21:46:10 +0000453 __le16 *ustr = (__le16 *)str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Steve French38702532007-07-08 15:40:40 +0000455 for (len = 0; len <= PATH_MAX; len++) {
Steve French4523cc32007-04-30 20:13:06 +0000456 if (ustr[len] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return len << 1;
458 }
Steve French790fe572007-07-07 19:25:05 +0000459 cFYI(1, ("Unicode string longer than PATH_MAX found"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return len << 1;
461}
462
Steve French5bafd762006-06-07 00:18:43 +0000463static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Steve French38702532007-07-08 15:40:40 +0000465 char *new_entry;
Steve Frenchad7a2922008-02-07 23:25:02 +0000466 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Steve French4523cc32007-04-30 20:13:06 +0000468 if (level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000469 FIND_FILE_STANDARD_INFO *pfData;
Steve French5bafd762006-06-07 00:18:43 +0000470 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
471
472 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
473 pfData->FileNameLength;
474 } else
475 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
Steve French790fe572007-07-07 19:25:05 +0000476 cFYI(1, ("new entry %p old entry %p", new_entry, old_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /* validate that new_entry is not past end of SMB */
Steve French4523cc32007-04-30 20:13:06 +0000478 if (new_entry >= end_of_smb) {
Steve French09d1db52005-04-28 22:41:08 -0700479 cERROR(1,
480 ("search entry %p began after end of SMB %p old entry %p",
Steve French38702532007-07-08 15:40:40 +0000481 new_entry, end_of_smb, old_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return NULL;
Steve French4523cc32007-04-30 20:13:06 +0000483 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
Steve French38702532007-07-08 15:40:40 +0000484 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
485 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
Steve French5bafd762006-06-07 00:18:43 +0000486 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
Steve French38702532007-07-08 15:40:40 +0000487 cERROR(1, ("search entry %p extends after end of SMB %p",
Steve French09d1db52005-04-28 22:41:08 -0700488 new_entry, end_of_smb));
489 return NULL;
Steve French38702532007-07-08 15:40:40 +0000490 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return new_entry;
492
493}
494
495#define UNICODE_DOT cpu_to_le16(0x2e)
496
497/* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
498static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
499{
500 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000501 char *filename = NULL;
502 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Steve French4523cc32007-04-30 20:13:06 +0000504 if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000505 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 filename = &pFindData->FileName[0];
Steve French4523cc32007-04-30 20:13:06 +0000507 if (cfile->srch_inf.unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 len = cifs_unicode_bytelen(filename);
509 } else {
510 /* BB should we make this strnlen of PATH_MAX? */
511 len = strnlen(filename, 5);
512 }
Steve French4523cc32007-04-30 20:13:06 +0000513 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000514 FILE_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 (FILE_DIRECTORY_INFO *)current_entry;
516 filename = &pFindData->FileName[0];
517 len = le32_to_cpu(pFindData->FileNameLength);
Steve French38702532007-07-08 15:40:40 +0000518 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000519 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000520 FILE_FULL_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 (FILE_FULL_DIRECTORY_INFO *)current_entry;
522 filename = &pFindData->FileName[0];
523 len = le32_to_cpu(pFindData->FileNameLength);
Steve French4523cc32007-04-30 20:13:06 +0000524 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000525 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000526 SEARCH_ID_FULL_DIR_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
528 filename = &pFindData->FileName[0];
529 len = le32_to_cpu(pFindData->FileNameLength);
Steve French38702532007-07-08 15:40:40 +0000530 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000531 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000532 FILE_BOTH_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
534 filename = &pFindData->FileName[0];
535 len = le32_to_cpu(pFindData->FileNameLength);
Steve French4523cc32007-04-30 20:13:06 +0000536 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000537 FIND_FILE_STANDARD_INFO *pFindData =
Steve French5bafd762006-06-07 00:18:43 +0000538 (FIND_FILE_STANDARD_INFO *)current_entry;
539 filename = &pFindData->FileName[0];
Steve French5ddaa682006-08-15 13:35:48 +0000540 len = pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 } else {
Steve French790fe572007-07-07 19:25:05 +0000542 cFYI(1, ("Unknown findfirst level %d",
543 cfile->srch_inf.info_level));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545
Steve French4523cc32007-04-30 20:13:06 +0000546 if (filename) {
547 if (cfile->srch_inf.unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 __le16 *ufilename = (__le16 *)filename;
Steve French4523cc32007-04-30 20:13:06 +0000549 if (len == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* check for . */
Steve French4523cc32007-04-30 20:13:06 +0000551 if (ufilename[0] == UNICODE_DOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 rc = 1;
Steve French4523cc32007-04-30 20:13:06 +0000553 } else if (len == 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /* check for .. */
Steve French4523cc32007-04-30 20:13:06 +0000555 if ((ufilename[0] == UNICODE_DOT)
Steve French38702532007-07-08 15:40:40 +0000556 && (ufilename[1] == UNICODE_DOT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 rc = 2;
558 }
559 } else /* ASCII */ {
Steve French4523cc32007-04-30 20:13:06 +0000560 if (len == 1) {
Steve French38702532007-07-08 15:40:40 +0000561 if (filename[0] == '.')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 rc = 1;
Steve French4523cc32007-04-30 20:13:06 +0000563 } else if (len == 2) {
Steve French790fe572007-07-07 19:25:05 +0000564 if ((filename[0] == '.') && (filename[1] == '.'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 rc = 2;
566 }
567 }
568 }
569
570 return rc;
571}
572
Steve Frencheafe8702005-09-15 21:47:30 -0700573/* Check if directory that we are searching has changed so we can decide
574 whether we can use the cached search results from the previous search */
Steve French38702532007-07-08 15:40:40 +0000575static int is_dir_changed(struct file *file)
Steve Frencheafe8702005-09-15 21:47:30 -0700576{
Christoph Hellwigc33f8d32007-04-02 18:47:20 +0000577 struct inode *inode = file->f_path.dentry->d_inode;
578 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
Steve Frencheafe8702005-09-15 21:47:30 -0700579
Christoph Hellwigc33f8d32007-04-02 18:47:20 +0000580 if (cifsInfo->time == 0)
Steve Frencheafe8702005-09-15 21:47:30 -0700581 return 1; /* directory was changed, perhaps due to unlink */
582 else
583 return 0;
584
585}
586
Steve French0752f152008-10-07 20:03:33 +0000587static int cifs_save_resume_key(const char *current_entry,
588 struct cifsFileInfo *cifsFile)
589{
590 int rc = 0;
591 unsigned int len = 0;
592 __u16 level;
593 char *filename;
594
595 if ((cifsFile == NULL) || (current_entry == NULL))
596 return -EINVAL;
597
598 level = cifsFile->srch_inf.info_level;
599
600 if (level == SMB_FIND_FILE_UNIX) {
601 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
602
603 filename = &pFindData->FileName[0];
604 if (cifsFile->srch_inf.unicode) {
605 len = cifs_unicode_bytelen(filename);
606 } else {
607 /* BB should we make this strnlen of PATH_MAX? */
608 len = strnlen(filename, PATH_MAX);
609 }
610 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
611 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
612 FILE_DIRECTORY_INFO *pFindData =
613 (FILE_DIRECTORY_INFO *)current_entry;
614 filename = &pFindData->FileName[0];
615 len = le32_to_cpu(pFindData->FileNameLength);
616 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
617 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
618 FILE_FULL_DIRECTORY_INFO *pFindData =
619 (FILE_FULL_DIRECTORY_INFO *)current_entry;
620 filename = &pFindData->FileName[0];
621 len = le32_to_cpu(pFindData->FileNameLength);
622 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
623 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
624 SEARCH_ID_FULL_DIR_INFO *pFindData =
625 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
626 filename = &pFindData->FileName[0];
627 len = le32_to_cpu(pFindData->FileNameLength);
628 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
629 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
630 FILE_BOTH_DIRECTORY_INFO *pFindData =
631 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
632 filename = &pFindData->FileName[0];
633 len = le32_to_cpu(pFindData->FileNameLength);
634 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
635 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
636 FIND_FILE_STANDARD_INFO *pFindData =
637 (FIND_FILE_STANDARD_INFO *)current_entry;
638 filename = &pFindData->FileName[0];
639 /* one byte length, no name conversion */
640 len = (unsigned int)pFindData->FileNameLength;
641 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
642 } else {
643 cFYI(1, ("Unknown findfirst level %d", level));
644 return -EINVAL;
645 }
646 cifsFile->srch_inf.resume_name_len = len;
647 cifsFile->srch_inf.presume_name = filename;
648 return rc;
649}
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651/* find the corresponding entry in the search */
652/* Note that the SMB server returns search entries for . and .. which
653 complicates logic here if we choose to parse for them and we do not
654 assume that they are located in the findfirst return buffer.*/
655/* We start counting in the buffer with entry 2 and increment for every
656 entry (do not increment for . or .. entry) */
657static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
Steve French38702532007-07-08 15:40:40 +0000658 struct file *file, char **ppCurrentEntry, int *num_to_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 int rc = 0;
661 int pos_in_buf = 0;
662 loff_t first_entry_in_buffer;
663 loff_t index_to_find = file->f_pos;
Steve French38702532007-07-08 15:40:40 +0000664 struct cifsFileInfo *cifsFile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* check if index in the buffer */
Steve French50c2f752007-07-13 00:33:32 +0000666
Steve French38702532007-07-08 15:40:40 +0000667 if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
Steve Frencheafe8702005-09-15 21:47:30 -0700668 (num_to_ret == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return -ENOENT;
Steve French50c2f752007-07-13 00:33:32 +0000670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 *ppCurrentEntry = NULL;
Steve French38702532007-07-08 15:40:40 +0000672 first_entry_in_buffer =
673 cifsFile->srch_inf.index_of_last_entry -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 cifsFile->srch_inf.entries_in_buffer;
Steve French60808232006-04-22 15:53:05 +0000675
676 /* if first entry in buf is zero then is first buffer
677 in search response data which means it is likely . and ..
678 will be in this buffer, although some servers do not return
679 . and .. for the root of a drive and for those we need
680 to start two entries earlier */
681
Steve French39798772006-05-31 22:40:51 +0000682 dump_cifs_file_struct(file, "In fce ");
Steve French38702532007-07-08 15:40:40 +0000683 if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
684 is_dir_changed(file)) ||
Steve Frencheafe8702005-09-15 21:47:30 -0700685 (index_to_find < first_entry_in_buffer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /* close and restart search */
Steve French790fe572007-07-07 19:25:05 +0000687 cFYI(1, ("search backing up - close and restart search"));
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000688 write_lock(&GlobalSMBSeslock);
Steve French77c57ec2008-05-13 21:39:32 +0000689 if (!cifsFile->srch_inf.endOfSearch &&
690 !cifsFile->invalidHandle) {
691 cifsFile->invalidHandle = true;
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000692 write_unlock(&GlobalSMBSeslock);
Steve French77c57ec2008-05-13 21:39:32 +0000693 CIFSFindClose(xid, pTcon, cifsFile->netfid);
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000694 } else
695 write_unlock(&GlobalSMBSeslock);
Steve French4523cc32007-04-30 20:13:06 +0000696 if (cifsFile->srch_inf.ntwrk_buf_start) {
Steve French790fe572007-07-07 19:25:05 +0000697 cFYI(1, ("freeing SMB ff cache buf on search rewind"));
Steve French4523cc32007-04-30 20:13:06 +0000698 if (cifsFile->srch_inf.smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +0000699 cifs_small_buf_release(cifsFile->srch_inf.
700 ntwrk_buf_start);
701 else
702 cifs_buf_release(cifsFile->srch_inf.
703 ntwrk_buf_start);
Shirish Pargaonkar76c510a2008-07-24 14:48:33 +0000704 cifsFile->srch_inf.ntwrk_buf_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
Steve French38702532007-07-08 15:40:40 +0000706 rc = initiate_cifs_search(xid, file);
Steve French4523cc32007-04-30 20:13:06 +0000707 if (rc) {
Steve French790fe572007-07-07 19:25:05 +0000708 cFYI(1, ("error %d reinitiating a search on rewind",
709 rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return rc;
711 }
Jeff Laytona364bc02008-10-21 14:42:13 +0000712 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714
Steve French38702532007-07-08 15:40:40 +0000715 while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
Steve French4b18f2a2008-04-29 00:06:05 +0000716 (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
Steve French790fe572007-07-07 19:25:05 +0000717 cFYI(1, ("calling findnext2"));
Steve French38702532007-07-08 15:40:40 +0000718 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
Steve Frenchdfb75332005-06-22 17:13:47 -0700719 &cifsFile->srch_inf);
Jeff Laytona364bc02008-10-21 14:42:13 +0000720 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
Steve French4523cc32007-04-30 20:13:06 +0000721 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return -ENOENT;
723 }
Steve French4523cc32007-04-30 20:13:06 +0000724 if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /* we found the buffer that contains the entry */
726 /* scan and find it */
727 int i;
Steve French38702532007-07-08 15:40:40 +0000728 char *current_entry;
729 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 smbCalcSize((struct smb_hdr *)
731 cifsFile->srch_inf.ntwrk_buf_start);
Steve French60808232006-04-22 15:53:05 +0000732
733 current_entry = cifsFile->srch_inf.srch_entries_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
735 - cifsFile->srch_inf.entries_in_buffer;
736 pos_in_buf = index_to_find - first_entry_in_buffer;
Steve French790fe572007-07-07 19:25:05 +0000737 cFYI(1, ("found entry - pos_in_buf %d", pos_in_buf));
Steve French5bafd762006-06-07 00:18:43 +0000738
Steve Frenchad7a2922008-02-07 23:25:02 +0000739 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
Steve Frenchdfb75332005-06-22 17:13:47 -0700740 /* go entry by entry figuring out which is first */
Steve French38702532007-07-08 15:40:40 +0000741 current_entry = nxt_dir_entry(current_entry, end_of_smb,
Steve French5bafd762006-06-07 00:18:43 +0000742 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
Steve French790fe572007-07-07 19:25:05 +0000744 if ((current_entry == NULL) && (i < pos_in_buf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /* BB fixme - check if we should flag this error */
Steve French38702532007-07-08 15:40:40 +0000746 cERROR(1, ("reached end of buf searching for pos in buf"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 " %d index to find %lld rc %d",
Steve French38702532007-07-08 15:40:40 +0000748 pos_in_buf, index_to_find, rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750 rc = 0;
751 *ppCurrentEntry = current_entry;
752 } else {
Steve French790fe572007-07-07 19:25:05 +0000753 cFYI(1, ("index not in buffer - could not findnext into it"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return 0;
755 }
756
Steve French790fe572007-07-07 19:25:05 +0000757 if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
758 cFYI(1, ("can not return entries pos_in_buf beyond last"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 *num_to_ret = 0;
760 } else
761 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 return rc;
764}
765
766/* inode num, inode type and filename returned */
767static int cifs_get_name_from_search_buf(struct qstr *pqst,
768 char *current_entry, __u16 level, unsigned int unicode,
Jeff Layton18295792009-04-30 20:45:45 -0400769 struct cifs_sb_info *cifs_sb, unsigned int max_len, __u64 *pinum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 int rc = 0;
772 unsigned int len = 0;
Steve French38702532007-07-08 15:40:40 +0000773 char *filename;
774 struct nls_table *nlt = cifs_sb->local_nls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 *pinum = 0;
777
Steve French790fe572007-07-07 19:25:05 +0000778 if (level == SMB_FIND_FILE_UNIX) {
Steve French38702532007-07-08 15:40:40 +0000779 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 filename = &pFindData->FileName[0];
Steve French790fe572007-07-07 19:25:05 +0000782 if (unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 len = cifs_unicode_bytelen(filename);
784 } else {
785 /* BB should we make this strnlen of PATH_MAX? */
786 len = strnlen(filename, PATH_MAX);
787 }
788
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400789 *pinum = le64_to_cpu(pFindData->basic.UniqueId);
Steve French790fe572007-07-07 19:25:05 +0000790 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000791 FILE_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 (FILE_DIRECTORY_INFO *)current_entry;
793 filename = &pFindData->FileName[0];
794 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000795 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000796 FILE_FULL_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 (FILE_FULL_DIRECTORY_INFO *)current_entry;
798 filename = &pFindData->FileName[0];
799 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000800 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
Steve French38702532007-07-08 15:40:40 +0000801 SEARCH_ID_FULL_DIR_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
803 filename = &pFindData->FileName[0];
804 len = le32_to_cpu(pFindData->FileNameLength);
Steve French85a6dac2009-04-01 05:22:00 +0000805 *pinum = le64_to_cpu(pFindData->UniqueId);
Steve French790fe572007-07-07 19:25:05 +0000806 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000807 FILE_BOTH_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
809 filename = &pFindData->FileName[0];
810 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000811 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000812 FIND_FILE_STANDARD_INFO *pFindData =
Steve French5bafd762006-06-07 00:18:43 +0000813 (FIND_FILE_STANDARD_INFO *)current_entry;
814 filename = &pFindData->FileName[0];
815 /* one byte length, no name conversion */
816 len = (unsigned int)pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 } else {
Steve French790fe572007-07-07 19:25:05 +0000818 cFYI(1, ("Unknown findfirst level %d", level));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return -EINVAL;
820 }
Steve French5bafd762006-06-07 00:18:43 +0000821
Steve French790fe572007-07-07 19:25:05 +0000822 if (len > max_len) {
Steve French38702532007-07-08 15:40:40 +0000823 cERROR(1, ("bad search response length %d past smb end", len));
Steve French5bafd762006-06-07 00:18:43 +0000824 return -EINVAL;
825 }
826
Steve French790fe572007-07-07 19:25:05 +0000827 if (unicode) {
Jeff Laytonf5884162009-04-30 07:18:00 -0400828 pqst->len = cifs_from_ucs2((char *) pqst->name,
829 (__le16 *) filename,
Jeff Layton18295792009-04-30 20:45:45 -0400830 UNICODE_NAME_MAX,
831 min(len, max_len), nlt,
Jeff Laytonf5884162009-04-30 07:18:00 -0400832 cifs_sb->mnt_cifs_flags &
833 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 } else {
835 pqst->name = filename;
836 pqst->len = len;
837 }
Steve French38702532007-07-08 15:40:40 +0000838 pqst->hash = full_name_hash(pqst->name, pqst->len);
Steve French790fe572007-07-07 19:25:05 +0000839/* cFYI(1, ("filldir on %s",pqst->name)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return rc;
841}
842
Jeff Layton18295792009-04-30 20:45:45 -0400843static int cifs_filldir(char *pfindEntry, struct file *file, filldir_t filldir,
844 void *direntry, char *scratch_buf, unsigned int max_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846 int rc = 0;
847 struct qstr qstring;
Steve French38702532007-07-08 15:40:40 +0000848 struct cifsFileInfo *pCifsF;
Steve French2c2130e12007-10-12 19:10:28 +0000849 unsigned int obj_type;
Jeff Layton950ec522009-02-11 08:08:26 -0500850 __u64 inum;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400851 ino_t ino;
Steve French38702532007-07-08 15:40:40 +0000852 struct cifs_sb_info *cifs_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 struct inode *tmp_inode;
854 struct dentry *tmp_dentry;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400855 struct cifs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 /* get filename and len into qstring */
858 /* get dentry */
859 /* decide whether to create and populate ionde */
Steve French790fe572007-07-07 19:25:05 +0000860 if ((direntry == NULL) || (file == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return -EINVAL;
862
863 pCifsF = file->private_data;
Steve French50c2f752007-07-13 00:33:32 +0000864
Steve French790fe572007-07-07 19:25:05 +0000865 if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return -ENOENT;
867
Steve French38702532007-07-08 15:40:40 +0000868 rc = cifs_entry_is_dot(pfindEntry, pCifsF);
Steve French60808232006-04-22 15:53:05 +0000869 /* skip . and .. since we added them first */
Steve French790fe572007-07-07 19:25:05 +0000870 if (rc != 0)
Steve French60808232006-04-22 15:53:05 +0000871 return 0;
872
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800873 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 qstring.name = scratch_buf;
Steve French38702532007-07-08 15:40:40 +0000876 rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 pCifsF->srch_inf.info_level,
Steve French38702532007-07-08 15:40:40 +0000878 pCifsF->srch_inf.unicode, cifs_sb,
Steve French5bafd762006-06-07 00:18:43 +0000879 max_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 &inum /* returned */);
881
Steve French790fe572007-07-07 19:25:05 +0000882 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return rc;
884
Jeff Layton132ac7b2009-02-10 07:33:57 -0500885 /* only these two infolevels return valid inode numbers */
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400886 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
887 cifs_unix_basic_to_fattr(&fattr,
888 &((FILE_UNIX_INFO *) pfindEntry)->basic,
889 cifs_sb);
890 tmp_dentry = cifs_readdir_lookup(file->f_dentry, &qstring,
891 &fattr);
892 obj_type = fattr.cf_dtype;
893 ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
894 } else {
895 if (pCifsF->srch_inf.info_level ==
896 SMB_FIND_FILE_ID_FULL_DIR_INFO)
897 rc = construct_dentry(&qstring, file, &tmp_inode,
898 &tmp_dentry, &inum);
899 else
900 rc = construct_dentry(&qstring, file, &tmp_inode,
901 &tmp_dentry, NULL);
Jeff Layton132ac7b2009-02-10 07:33:57 -0500902
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400903 if ((tmp_inode == NULL) || (tmp_dentry == NULL)) {
904 rc = -ENOMEM;
905 goto out;
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400908 /* we pass in rc below, indicating whether it is a new inode,
909 * so we can figure out whether to invalidate the inode cached
910 * data if the file has changed
911 */
912 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
913 fill_in_inode(tmp_inode, 0, pfindEntry, &obj_type, rc);
914 else
915 fill_in_inode(tmp_inode, 1, pfindEntry, &obj_type, rc);
Steve Frenchb835beb2006-09-06 22:02:22 +0000916
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400917 /* new inode - needs to be tied to dentry */
918 if (rc) {
919 d_instantiate(tmp_dentry, tmp_inode);
920 if (rc == 2)
921 d_rehash(tmp_dentry);
922 }
923
924 ino = cifs_uniqueid_to_ino_t(tmp_inode->i_ino);
Steve Frenchb835beb2006-09-06 22:02:22 +0000925 }
Steve French50c2f752007-07-13 00:33:32 +0000926
Steve French38702532007-07-08 15:40:40 +0000927 rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400928 ino, obj_type);
Steve French790fe572007-07-07 19:25:05 +0000929 if (rc) {
930 cFYI(1, ("filldir rc = %d", rc));
Steve French7ca85ba2006-10-30 21:42:57 +0000931 /* we can not return filldir errors to the caller
932 since they are "normal" when the stat blocksize
933 is too small - we return remapped error instead */
934 rc = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400937out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 dput(tmp_dentry);
939 return rc;
940}
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
944{
945 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000946 int xid, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 struct cifs_sb_info *cifs_sb;
948 struct cifsTconInfo *pTcon;
949 struct cifsFileInfo *cifsFile = NULL;
Steve French38702532007-07-08 15:40:40 +0000950 char *current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 int num_to_fill = 0;
Steve French38702532007-07-08 15:40:40 +0000952 char *tmp_buf = NULL;
Steve French50c2f752007-07-13 00:33:32 +0000953 char *end_of_smb;
Jeff Layton18295792009-04-30 20:45:45 -0400954 unsigned int max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 xid = GetXid();
957
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800958 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 pTcon = cifs_sb->tcon;
Steve French790fe572007-07-07 19:25:05 +0000960 if (pTcon == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return -EINVAL;
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 switch ((int) file->f_pos) {
964 case 0:
Steve French60808232006-04-22 15:53:05 +0000965 if (filldir(direntry, ".", 1, file->f_pos,
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800966 file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
Steve French60808232006-04-22 15:53:05 +0000967 cERROR(1, ("Filldir for current dir failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 rc = -ENOMEM;
969 break;
970 }
Steve French60808232006-04-22 15:53:05 +0000971 file->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 case 1:
Steve French60808232006-04-22 15:53:05 +0000973 if (filldir(direntry, "..", 2, file->f_pos,
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800974 file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
Steve French26a21b92006-05-31 18:05:34 +0000975 cERROR(1, ("Filldir for parent dir failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 rc = -ENOMEM;
977 break;
978 }
Steve French60808232006-04-22 15:53:05 +0000979 file->f_pos++;
980 default:
Steve French38702532007-07-08 15:40:40 +0000981 /* 1) If search is active,
982 is in current search buffer?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if it before then restart search
984 if after then keep searching till find it */
985
Steve French790fe572007-07-07 19:25:05 +0000986 if (file->private_data == NULL) {
Steve French38702532007-07-08 15:40:40 +0000987 rc = initiate_cifs_search(xid, file);
Steve French790fe572007-07-07 19:25:05 +0000988 cFYI(1, ("initiate cifs search rc %d", rc));
989 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 FreeXid(xid);
991 return rc;
992 }
993 }
Steve French790fe572007-07-07 19:25:05 +0000994 if (file->private_data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 rc = -EINVAL;
996 FreeXid(xid);
997 return rc;
998 }
999 cifsFile = file->private_data;
1000 if (cifsFile->srch_inf.endOfSearch) {
Steve French790fe572007-07-07 19:25:05 +00001001 if (cifsFile->srch_inf.emptyDir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 cFYI(1, ("End of search, empty dir"));
1003 rc = 0;
1004 break;
1005 }
1006 } /* else {
Steve French4b18f2a2008-04-29 00:06:05 +00001007 cifsFile->invalidHandle = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 CIFSFindClose(xid, pTcon, cifsFile->netfid);
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00001009 } */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Steve French38702532007-07-08 15:40:40 +00001011 rc = find_cifs_entry(xid, pTcon, file,
1012 &current_entry, &num_to_fill);
Steve French790fe572007-07-07 19:25:05 +00001013 if (rc) {
1014 cFYI(1, ("fce error %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 goto rddir2_exit;
1016 } else if (current_entry != NULL) {
Steve French790fe572007-07-07 19:25:05 +00001017 cFYI(1, ("entry %lld found", file->f_pos));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 } else {
Steve French790fe572007-07-07 19:25:05 +00001019 cFYI(1, ("could not find entry"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 goto rddir2_exit;
1021 }
Steve French790fe572007-07-07 19:25:05 +00001022 cFYI(1, ("loop through %d times filling dir for net buf %p",
1023 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start));
Steve French5bafd762006-06-07 00:18:43 +00001024 max_len = smbCalcSize((struct smb_hdr *)
1025 cifsFile->srch_inf.ntwrk_buf_start);
1026 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1027
Jeff Laytonf5884162009-04-30 07:18:00 -04001028 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
Steve French790fe572007-07-07 19:25:05 +00001029 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
1030 if (current_entry == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /* evaluate whether this case is an error */
Steve French26f57362007-08-30 22:09:15 +00001032 cERROR(1, ("past SMB end, num to fill %d i %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 num_to_fill, i));
1034 break;
1035 }
Steve French60808232006-04-22 15:53:05 +00001036 /* if buggy server returns . and .. late do
1037 we want to check for that here? */
Steve French5bafd762006-06-07 00:18:43 +00001038 rc = cifs_filldir(current_entry, file,
1039 filldir, direntry, tmp_buf, max_len);
Steve French790fe572007-07-07 19:25:05 +00001040 if (rc == -EOVERFLOW) {
Steve French7ca85ba2006-10-30 21:42:57 +00001041 rc = 0;
1042 break;
1043 }
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 file->f_pos++;
Steve French790fe572007-07-07 19:25:05 +00001046 if (file->f_pos ==
Steve French39798772006-05-31 22:40:51 +00001047 cifsFile->srch_inf.index_of_last_entry) {
Steve French790fe572007-07-07 19:25:05 +00001048 cFYI(1, ("last entry in buf at pos %lld %s",
1049 file->f_pos, tmp_buf));
1050 cifs_save_resume_key(current_entry, cifsFile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 break;
Steve French790fe572007-07-07 19:25:05 +00001052 } else
1053 current_entry =
Steve French5bafd762006-06-07 00:18:43 +00001054 nxt_dir_entry(current_entry, end_of_smb,
1055 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057 kfree(tmp_buf);
1058 break;
1059 } /* end switch */
1060
1061rddir2_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 FreeXid(xid);
1063 return rc;
1064}