blob: 86d0055dc529906df58a3dfddbc4b5601af08d0f [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 Layton132ac7b2009-02-10 07:33:57 -050066/* Returns 1 if new inode created, 2 if both dentry and inode were */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/* Might check in the future if inode number changed so we can rehash inode */
Jeff Layton132ac7b2009-02-10 07:33:57 -050068static int
69construct_dentry(struct qstr *qstring, struct file *file,
70 struct inode **ptmp_inode, struct dentry **pnew_dentry,
Jeff Layton950ec522009-02-11 08:08:26 -050071 __u64 *inum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Jeff Layton132ac7b2009-02-10 07:33:57 -050073 struct dentry *tmp_dentry = NULL;
74 struct super_block *sb = file->f_path.dentry->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 int rc = 0;
76
Steve French966ca922005-04-28 22:41:08 -070077 cFYI(1, ("For %s", qstring->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 qstring->hash = full_name_hash(qstring->name, qstring->len);
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -080080 tmp_dentry = d_lookup(file->f_path.dentry, qstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (tmp_dentry) {
Jeff Layton132ac7b2009-02-10 07:33:57 -050082 /* BB: overwrite old name? i.e. tmp_dentry->d_name and
83 * tmp_dentry->d_name.len??
84 */
Steve French6dc0f872007-07-06 23:13:06 +000085 cFYI(0, ("existing dentry with inode 0x%p",
86 tmp_dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 *ptmp_inode = tmp_dentry->d_inode;
Steve French4523cc32007-04-30 20:13:06 +000088 if (*ptmp_inode == NULL) {
Jeff Layton132ac7b2009-02-10 07:33:57 -050089 *ptmp_inode = cifs_new_inode(sb, inum);
Steve French4523cc32007-04-30 20:13:06 +000090 if (*ptmp_inode == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return rc;
92 rc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94 } else {
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -080095 tmp_dentry = d_alloc(file->f_path.dentry, qstring);
Steve French4523cc32007-04-30 20:13:06 +000096 if (tmp_dentry == NULL) {
Steve French6dc0f872007-07-06 23:13:06 +000097 cERROR(1, ("Failed allocating dentry"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *ptmp_inode = NULL;
99 return rc;
100 }
101
Jeff Layton132ac7b2009-02-10 07:33:57 -0500102 if (CIFS_SB(sb)->tcon->nocase)
Steve Frenchb92327f2005-08-22 20:09:43 -0700103 tmp_dentry->d_op = &cifs_ci_dentry_ops;
104 else
105 tmp_dentry->d_op = &cifs_dentry_ops;
Jeff Layton132ac7b2009-02-10 07:33:57 -0500106
107 *ptmp_inode = cifs_new_inode(sb, inum);
Steve French4523cc32007-04-30 20:13:06 +0000108 if (*ptmp_inode == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return rc;
Steve Frenchb835beb2006-09-06 22:02:22 +0000110 rc = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112
113 tmp_dentry->d_time = jiffies;
114 *pnew_dentry = tmp_dentry;
115 return rc;
116}
117
Steve French5bafd762006-06-07 00:18:43 +0000118static void fill_in_inode(struct inode *tmp_inode, int new_buf_type,
Steve French2c2130e2007-10-12 19:10:28 +0000119 char *buf, unsigned int *pobject_type, int isNewInode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Steve French966ca922005-04-28 22:41:08 -0700121 loff_t local_size;
122 struct timespec local_mtime;
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
125 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
Steve French5bafd762006-06-07 00:18:43 +0000126 __u32 attr;
127 __u64 allocation_size;
128 __u64 end_of_file;
Jeff Layton4468eb32008-05-22 09:31:40 -0400129 umode_t default_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Steve French966ca922005-04-28 22:41:08 -0700131 /* save mtime and size */
132 local_mtime = tmp_inode->i_mtime;
133 local_size = tmp_inode->i_size;
134
Steve French4523cc32007-04-30 20:13:06 +0000135 if (new_buf_type) {
Steve French5bafd762006-06-07 00:18:43 +0000136 FILE_DIRECTORY_INFO *pfindData = (FILE_DIRECTORY_INFO *)buf;
137
138 attr = le32_to_cpu(pfindData->ExtFileAttributes);
139 allocation_size = le64_to_cpu(pfindData->AllocationSize);
140 end_of_file = le64_to_cpu(pfindData->EndOfFile);
141 tmp_inode->i_atime =
Jeff Layton07119a42009-05-27 09:37:33 -0400142 cifs_NTtimeToUnix(pfindData->LastAccessTime);
Steve French5bafd762006-06-07 00:18:43 +0000143 tmp_inode->i_mtime =
Jeff Layton07119a42009-05-27 09:37:33 -0400144 cifs_NTtimeToUnix(pfindData->LastWriteTime);
Steve French5bafd762006-06-07 00:18:43 +0000145 tmp_inode->i_ctime =
Jeff Layton07119a42009-05-27 09:37:33 -0400146 cifs_NTtimeToUnix(pfindData->ChangeTime);
Steve French5bafd762006-06-07 00:18:43 +0000147 } else { /* legacy, OS2 and DOS style */
Jeff Laytonc4a2c082009-05-27 09:37:33 -0400148 int offset = cifs_sb->tcon->ses->server->timeAdj;
Steve Frenchad7a2922008-02-07 23:25:02 +0000149 FIND_FILE_STANDARD_INFO *pfindData =
Steve French5bafd762006-06-07 00:18:43 +0000150 (FIND_FILE_STANDARD_INFO *)buf;
151
Jeff Laytonc4a2c082009-05-27 09:37:33 -0400152 tmp_inode->i_mtime = cnvrtDosUnixTm(pfindData->LastWriteDate,
153 pfindData->LastWriteTime,
154 offset);
155 tmp_inode->i_atime = cnvrtDosUnixTm(pfindData->LastAccessDate,
156 pfindData->LastAccessTime,
157 offset);
158 tmp_inode->i_ctime = cnvrtDosUnixTm(pfindData->LastWriteDate,
159 pfindData->LastWriteTime,
160 offset);
Steve French5bafd762006-06-07 00:18:43 +0000161 attr = le16_to_cpu(pfindData->Attributes);
162 allocation_size = le32_to_cpu(pfindData->AllocationSize);
163 end_of_file = le32_to_cpu(pfindData->DataSize);
Steve French5bafd762006-06-07 00:18:43 +0000164 }
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /* Linux can not store file creation time unfortunately so ignore it */
Steve French5bafd762006-06-07 00:18:43 +0000167
168 cifsInfo->cifsAttrs = attr;
Steve Frencha6f8de32007-11-08 23:10:32 +0000169#ifdef CONFIG_CIFS_EXPERIMENTAL
170 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
171 /* get more accurate mode via ACL - so force inode refresh */
172 cifsInfo->time = 0;
173 } else
174#endif /* CONFIG_CIFS_EXPERIMENTAL */
175 cifsInfo->time = jiffies;
Steve French5bafd762006-06-07 00:18:43 +0000176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
178 /* 2767 perms - indicate mandatory locking */
Steve French790fe572007-07-07 19:25:05 +0000179 /* BB fill in uid and gid here? with help from winbind?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 or retrieve from NTFS stream extended attribute */
181 if (atomic_read(&cifsInfo->inUse) == 0) {
182 tmp_inode->i_uid = cifs_sb->mnt_uid;
183 tmp_inode->i_gid = cifs_sb->mnt_gid;
Jeff Layton4468eb32008-05-22 09:31:40 -0400184 }
185
186 if (attr & ATTR_DIRECTORY)
187 default_mode = cifs_sb->mnt_dir_mode;
188 else
189 default_mode = cifs_sb->mnt_file_mode;
190
191 /* set initial permissions */
192 if ((atomic_read(&cifsInfo->inUse) == 0) ||
193 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
194 tmp_inode->i_mode = default_mode;
195 else {
196 /* just reenable write bits if !ATTR_READONLY */
197 if ((tmp_inode->i_mode & S_IWUGO) == 0 &&
198 (attr & ATTR_READONLY) == 0)
199 tmp_inode->i_mode |= (S_IWUGO & default_mode);
200
Steve French3020a1f2005-11-18 11:31:10 -0800201 tmp_inode->i_mode &= ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203
Jeff Layton4468eb32008-05-22 09:31:40 -0400204 /* clear write bits if ATTR_READONLY is set */
205 if (attr & ATTR_READONLY)
206 tmp_inode->i_mode &= ~S_IWUGO;
207
208 /* set inode type */
209 if ((attr & ATTR_SYSTEM) &&
210 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
Steve French3020a1f2005-11-18 11:31:10 -0800211 if (end_of_file == 0) {
Steve French3020a1f2005-11-18 11:31:10 -0800212 tmp_inode->i_mode |= S_IFIFO;
Jeff Layton4468eb32008-05-22 09:31:40 -0400213 *pobject_type = DT_FIFO;
Steve French3020a1f2005-11-18 11:31:10 -0800214 } else {
Jeff Layton4468eb32008-05-22 09:31:40 -0400215 /*
216 * trying to get the type can be slow, so just call
217 * this a regular file for now, and mark for reval
218 */
Steve French790fe572007-07-07 19:25:05 +0000219 tmp_inode->i_mode |= S_IFREG;
Jeff Layton4468eb32008-05-22 09:31:40 -0400220 *pobject_type = DT_REG;
Steve French790fe572007-07-07 19:25:05 +0000221 cifsInfo->time = 0;
Steve French3020a1f2005-11-18 11:31:10 -0800222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 } else {
Jeff Layton4468eb32008-05-22 09:31:40 -0400224 if (attr & ATTR_DIRECTORY) {
225 tmp_inode->i_mode |= S_IFDIR;
226 *pobject_type = DT_DIR;
227 } else {
228 tmp_inode->i_mode |= S_IFREG;
229 *pobject_type = DT_REG;
230 }
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 /* can not fill in nlink here as in qpathinfo version and Unx search */
Steve Frenchad7a2922008-02-07 23:25:02 +0000234 if (atomic_read(&cifsInfo->inUse) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 atomic_set(&cifsInfo->inUse, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Jeff Laytonfbec9ab2009-04-03 13:44:00 -0400237 cifsInfo->server_eof = end_of_file;
Steve French3677db12007-02-26 16:46:11 +0000238 spin_lock(&tmp_inode->i_lock);
Steve French7ba52632007-02-08 18:14:13 +0000239 if (is_size_safe_to_change(cifsInfo, end_of_file)) {
Steve French790fe572007-07-07 19:25:05 +0000240 /* can not safely change the file size here if the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 client is writing to it due to potential races */
242 i_size_write(tmp_inode, end_of_file);
243
244 /* 512 bytes (2**9) is the fake blocksize that must be used */
245 /* for this calculation, even though the reported blocksize is larger */
246 tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
247 }
Steve French3677db12007-02-26 16:46:11 +0000248 spin_unlock(&tmp_inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 if (allocation_size < end_of_file)
251 cFYI(1, ("May be sparse file, allocation less than file size"));
Theodore Ts'oba52de12006-09-27 01:50:49 -0700252 cFYI(1, ("File Size %ld and blocks %llu",
Andrew Morton5515eff2006-03-26 01:37:53 -0800253 (unsigned long)tmp_inode->i_size,
Theodore Ts'oba52de12006-09-27 01:50:49 -0700254 (unsigned long long)tmp_inode->i_blocks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (S_ISREG(tmp_inode->i_mode)) {
Steve French966ca922005-04-28 22:41:08 -0700256 cFYI(1, ("File inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 tmp_inode->i_op = &cifs_file_inode_ops;
Steve French4523cc32007-04-30 20:13:06 +0000258 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
259 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
Steve French8b94bcb2005-11-11 11:41:00 -0800260 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
261 else
262 tmp_inode->i_fop = &cifs_file_direct_ops;
Steve French4523cc32007-04-30 20:13:06 +0000263 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
Steve French8b94bcb2005-11-11 11:41:00 -0800264 tmp_inode->i_fop = &cifs_file_nobrl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 else
266 tmp_inode->i_fop = &cifs_file_ops;
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800267
Steve French4523cc32007-04-30 20:13:06 +0000268 if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
Steve Frenchbfa0d752005-08-31 21:50:37 -0700269 (cifs_sb->tcon->ses->server->maxBuf <
Dave Kleikamp273d81d2006-06-01 19:41:23 +0000270 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
271 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
272 else
273 tmp_inode->i_data.a_ops = &cifs_addr_ops;
274
Steve French4523cc32007-04-30 20:13:06 +0000275 if (isNewInode)
Steve Frenchdfb75332005-06-22 17:13:47 -0700276 return; /* No sense invalidating pages for new inode
277 since have not started caching readahead file
278 data yet */
Steve French966ca922005-04-28 22:41:08 -0700279
280 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
281 (local_size == tmp_inode->i_size)) {
282 cFYI(1, ("inode exists but unchanged"));
283 } else {
284 /* file may have changed on server */
285 cFYI(1, ("invalidate inode, readdir detected change"));
286 invalidate_remote_inode(tmp_inode);
287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 } else if (S_ISDIR(tmp_inode->i_mode)) {
Steve French966ca922005-04-28 22:41:08 -0700289 cFYI(1, ("Directory inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 tmp_inode->i_op = &cifs_dir_inode_ops;
291 tmp_inode->i_fop = &cifs_dir_ops;
292 } else if (S_ISLNK(tmp_inode->i_mode)) {
Steve French966ca922005-04-28 22:41:08 -0700293 cFYI(1, ("Symbolic Link inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 tmp_inode->i_op = &cifs_symlink_inode_ops;
295 } else {
Steve French966ca922005-04-28 22:41:08 -0700296 cFYI(1, ("Init special inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 init_special_inode(tmp_inode, tmp_inode->i_mode,
298 tmp_inode->i_rdev);
299 }
300}
301
302static void unix_fill_in_inode(struct inode *tmp_inode,
Steve French2c2130e2007-10-12 19:10:28 +0000303 FILE_UNIX_INFO *pfindData, unsigned int *pobject_type, int isNewInode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Steve French966ca922005-04-28 22:41:08 -0700305 loff_t local_size;
306 struct timespec local_mtime;
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
309 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
310
311 __u32 type = le32_to_cpu(pfindData->Type);
312 __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes);
313 __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
314 cifsInfo->time = jiffies;
315 atomic_inc(&cifsInfo->inUse);
316
Steve French966ca922005-04-28 22:41:08 -0700317 /* save mtime and size */
318 local_mtime = tmp_inode->i_mtime;
319 local_size = tmp_inode->i_size;
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 tmp_inode->i_atime =
Jeff Layton07119a42009-05-27 09:37:33 -0400322 cifs_NTtimeToUnix(pfindData->LastAccessTime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 tmp_inode->i_mtime =
Jeff Layton07119a42009-05-27 09:37:33 -0400324 cifs_NTtimeToUnix(pfindData->LastModificationTime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 tmp_inode->i_ctime =
Jeff Layton07119a42009-05-27 09:37:33 -0400326 cifs_NTtimeToUnix(pfindData->LastStatusChange);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions);
Steve French3020a1f2005-11-18 11:31:10 -0800329 /* since we set the inode type below we need to mask off type
Steve French790fe572007-07-07 19:25:05 +0000330 to avoid strange results if bits above were corrupt */
331 tmp_inode->i_mode &= ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (type == UNIX_FILE) {
333 *pobject_type = DT_REG;
334 tmp_inode->i_mode |= S_IFREG;
335 } else if (type == UNIX_SYMLINK) {
336 *pobject_type = DT_LNK;
337 tmp_inode->i_mode |= S_IFLNK;
338 } else if (type == UNIX_DIR) {
339 *pobject_type = DT_DIR;
340 tmp_inode->i_mode |= S_IFDIR;
341 } else if (type == UNIX_CHARDEV) {
342 *pobject_type = DT_CHR;
343 tmp_inode->i_mode |= S_IFCHR;
344 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
345 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
346 } else if (type == UNIX_BLOCKDEV) {
347 *pobject_type = DT_BLK;
348 tmp_inode->i_mode |= S_IFBLK;
349 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
350 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
351 } else if (type == UNIX_FIFO) {
352 *pobject_type = DT_FIFO;
353 tmp_inode->i_mode |= S_IFIFO;
354 } else if (type == UNIX_SOCKET) {
355 *pobject_type = DT_SOCK;
356 tmp_inode->i_mode |= S_IFSOCK;
Steve French3020a1f2005-11-18 11:31:10 -0800357 } else {
358 /* safest to just call it a file */
359 *pobject_type = DT_REG;
360 tmp_inode->i_mode |= S_IFREG;
Steve French790fe572007-07-07 19:25:05 +0000361 cFYI(1, ("unknown inode type %d", type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
Steve French4523cc32007-04-30 20:13:06 +0000364 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
365 tmp_inode->i_uid = cifs_sb->mnt_uid;
366 else
367 tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
368 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
369 tmp_inode->i_gid = cifs_sb->mnt_gid;
370 else
371 tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
373
Jeff Laytonfbec9ab2009-04-03 13:44:00 -0400374 cifsInfo->server_eof = end_of_file;
Steve French3677db12007-02-26 16:46:11 +0000375 spin_lock(&tmp_inode->i_lock);
Steve French7ba52632007-02-08 18:14:13 +0000376 if (is_size_safe_to_change(cifsInfo, end_of_file)) {
Steve French790fe572007-07-07 19:25:05 +0000377 /* can not safely change the file size here if the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 client is writing to it due to potential races */
Steve French7ba52632007-02-08 18:14:13 +0000379 i_size_write(tmp_inode, end_of_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 /* 512 bytes (2**9) is the fake blocksize that must be used */
382 /* for this calculation, not the real blocksize */
383 tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
384 }
Steve French3677db12007-02-26 16:46:11 +0000385 spin_unlock(&tmp_inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 if (S_ISREG(tmp_inode->i_mode)) {
388 cFYI(1, ("File inode"));
389 tmp_inode->i_op = &cifs_file_inode_ops;
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800390
Steve French4523cc32007-04-30 20:13:06 +0000391 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
392 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800393 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
394 else
395 tmp_inode->i_fop = &cifs_file_direct_ops;
Steve French4523cc32007-04-30 20:13:06 +0000396 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800397 tmp_inode->i_fop = &cifs_file_nobrl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 else
399 tmp_inode->i_fop = &cifs_file_ops;
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800400
Steve French4523cc32007-04-30 20:13:06 +0000401 if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
Steve French790fe572007-07-07 19:25:05 +0000402 (cifs_sb->tcon->ses->server->maxBuf <
Dave Kleikamp273d81d2006-06-01 19:41:23 +0000403 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
404 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
405 else
406 tmp_inode->i_data.a_ops = &cifs_addr_ops;
Steve French966ca922005-04-28 22:41:08 -0700407
Steve French4523cc32007-04-30 20:13:06 +0000408 if (isNewInode)
Steve French790fe572007-07-07 19:25:05 +0000409 return; /* No sense invalidating pages for new inode
410 since we have not started caching readahead
411 file data for it yet */
Steve French966ca922005-04-28 22:41:08 -0700412
413 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
414 (local_size == tmp_inode->i_size)) {
415 cFYI(1, ("inode exists but unchanged"));
416 } else {
417 /* file may have changed on server */
418 cFYI(1, ("invalidate inode, readdir detected change"));
419 invalidate_remote_inode(tmp_inode);
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 } else if (S_ISDIR(tmp_inode->i_mode)) {
422 cFYI(1, ("Directory inode"));
423 tmp_inode->i_op = &cifs_dir_inode_ops;
424 tmp_inode->i_fop = &cifs_dir_ops;
425 } else if (S_ISLNK(tmp_inode->i_mode)) {
426 cFYI(1, ("Symbolic Link inode"));
427 tmp_inode->i_op = &cifs_symlink_inode_ops;
428/* tmp_inode->i_fop = *//* do not need to set to anything */
429 } else {
Steve French790fe572007-07-07 19:25:05 +0000430 cFYI(1, ("Special inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 init_special_inode(tmp_inode, tmp_inode->i_mode,
432 tmp_inode->i_rdev);
433 }
434}
435
Steve French0e0d2cf2009-05-01 05:27:32 +0000436/* BB eventually need to add the following helper function to
437 resolve NT_STATUS_STOPPED_ON_SYMLINK return code when
438 we try to do FindFirst on (NTFS) directory symlinks */
439/*
440int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
441 int xid)
442{
443 __u16 fid;
444 int len;
445 int oplock = 0;
446 int rc;
447 struct cifsTconInfo *ptcon = cifs_sb->tcon;
448 char *tmpbuffer;
449
450 rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ,
451 OPEN_REPARSE_POINT, &fid, &oplock, NULL,
452 cifs_sb->local_nls,
453 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
454 if (!rc) {
455 tmpbuffer = kmalloc(maxpath);
456 rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path,
457 tmpbuffer,
458 maxpath -1,
459 fid,
460 cifs_sb->local_nls);
461 if (CIFSSMBClose(xid, ptcon, fid)) {
462 cFYI(1, ("Error closing temporary reparsepoint open)"));
463 }
464 }
465}
466 */
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468static int initiate_cifs_search(const int xid, struct file *file)
469{
470 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000471 char *full_path;
472 struct cifsFileInfo *cifsFile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 struct cifs_sb_info *cifs_sb;
474 struct cifsTconInfo *pTcon;
475
Steve French4523cc32007-04-30 20:13:06 +0000476 if (file->private_data == NULL) {
Steve French38702532007-07-08 15:40:40 +0000477 file->private_data =
478 kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480
Steve French4523cc32007-04-30 20:13:06 +0000481 if (file->private_data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 cifsFile = file->private_data;
Steve French4b18f2a2008-04-29 00:06:05 +0000484 cifsFile->invalidHandle = true;
485 cifsFile->srch_inf.endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800487 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
Steve French4523cc32007-04-30 20:13:06 +0000488 if (cifs_sb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return -EINVAL;
490
491 pTcon = cifs_sb->tcon;
Steve French4523cc32007-04-30 20:13:06 +0000492 if (pTcon == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return -EINVAL;
494
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800495 full_path = build_path_from_dentry(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Steve Frenchad7a2922008-02-07 23:25:02 +0000497 if (full_path == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Steve French966ca922005-04-28 22:41:08 -0700500 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Steve French75cf6bd2005-04-28 22:41:04 -0700502ffirst_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /* test for Unix extensions */
Steve Frenchc18c8422007-07-18 23:21:09 +0000504 /* but now check for them on the share/mount not on the SMB session */
505/* if (pTcon->ses->capabilities & CAP_UNIX) { */
Steve Frenchad7a2922008-02-07 23:25:02 +0000506 if (pTcon->unix_ext)
Steve French5bafd762006-06-07 00:18:43 +0000507 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
Steve Frenchad7a2922008-02-07 23:25:02 +0000508 else if ((pTcon->ses->capabilities &
Steve French5bafd762006-06-07 00:18:43 +0000509 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
510 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
512 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
513 } else /* not srvinos - BB fixme add check for backlevel? */ {
514 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
515 }
516
Steve French38702532007-07-08 15:40:40 +0000517 rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
Steve French737b7582005-04-28 22:41:06 -0700518 &cifsFile->netfid, &cifsFile->srch_inf,
Steve French38702532007-07-08 15:40:40 +0000519 cifs_sb->mnt_cifs_flags &
Steve Frencheafe8702005-09-15 21:47:30 -0700520 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
Steve French4523cc32007-04-30 20:13:06 +0000521 if (rc == 0)
Steve French4b18f2a2008-04-29 00:06:05 +0000522 cifsFile->invalidHandle = false;
Steve Frenche836f012009-05-01 16:20:35 +0000523 /* BB add following call to handle readdir on new NTFS symlink errors
Steve French0e0d2cf2009-05-01 05:27:32 +0000524 else if STATUS_STOPPED_ON_SYMLINK
525 call get_symlink_reparse_path and retry with new path */
526 else if ((rc == -EOPNOTSUPP) &&
Steve French75cf6bd2005-04-28 22:41:04 -0700527 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
528 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
529 goto ffirst_retry;
530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 kfree(full_path);
532 return rc;
533}
534
535/* return length of unicode string in bytes */
536static int cifs_unicode_bytelen(char *str)
537{
538 int len;
Steve French63d25832007-11-05 21:46:10 +0000539 __le16 *ustr = (__le16 *)str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Steve French38702532007-07-08 15:40:40 +0000541 for (len = 0; len <= PATH_MAX; len++) {
Steve French4523cc32007-04-30 20:13:06 +0000542 if (ustr[len] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return len << 1;
544 }
Steve French790fe572007-07-07 19:25:05 +0000545 cFYI(1, ("Unicode string longer than PATH_MAX found"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return len << 1;
547}
548
Steve French5bafd762006-06-07 00:18:43 +0000549static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Steve French38702532007-07-08 15:40:40 +0000551 char *new_entry;
Steve Frenchad7a2922008-02-07 23:25:02 +0000552 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Steve French4523cc32007-04-30 20:13:06 +0000554 if (level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000555 FIND_FILE_STANDARD_INFO *pfData;
Steve French5bafd762006-06-07 00:18:43 +0000556 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
557
558 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
559 pfData->FileNameLength;
560 } else
561 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
Steve French790fe572007-07-07 19:25:05 +0000562 cFYI(1, ("new entry %p old entry %p", new_entry, old_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* validate that new_entry is not past end of SMB */
Steve French4523cc32007-04-30 20:13:06 +0000564 if (new_entry >= end_of_smb) {
Steve French09d1db52005-04-28 22:41:08 -0700565 cERROR(1,
566 ("search entry %p began after end of SMB %p old entry %p",
Steve French38702532007-07-08 15:40:40 +0000567 new_entry, end_of_smb, old_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return NULL;
Steve French4523cc32007-04-30 20:13:06 +0000569 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
Steve French38702532007-07-08 15:40:40 +0000570 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
571 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
Steve French5bafd762006-06-07 00:18:43 +0000572 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
Steve French38702532007-07-08 15:40:40 +0000573 cERROR(1, ("search entry %p extends after end of SMB %p",
Steve French09d1db52005-04-28 22:41:08 -0700574 new_entry, end_of_smb));
575 return NULL;
Steve French38702532007-07-08 15:40:40 +0000576 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return new_entry;
578
579}
580
581#define UNICODE_DOT cpu_to_le16(0x2e)
582
583/* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
584static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
585{
586 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000587 char *filename = NULL;
588 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Steve French4523cc32007-04-30 20:13:06 +0000590 if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000591 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 filename = &pFindData->FileName[0];
Steve French4523cc32007-04-30 20:13:06 +0000593 if (cfile->srch_inf.unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 len = cifs_unicode_bytelen(filename);
595 } else {
596 /* BB should we make this strnlen of PATH_MAX? */
597 len = strnlen(filename, 5);
598 }
Steve French4523cc32007-04-30 20:13:06 +0000599 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000600 FILE_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 (FILE_DIRECTORY_INFO *)current_entry;
602 filename = &pFindData->FileName[0];
603 len = le32_to_cpu(pFindData->FileNameLength);
Steve French38702532007-07-08 15:40:40 +0000604 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000605 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000606 FILE_FULL_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 (FILE_FULL_DIRECTORY_INFO *)current_entry;
608 filename = &pFindData->FileName[0];
609 len = le32_to_cpu(pFindData->FileNameLength);
Steve French4523cc32007-04-30 20:13:06 +0000610 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000611 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000612 SEARCH_ID_FULL_DIR_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
614 filename = &pFindData->FileName[0];
615 len = le32_to_cpu(pFindData->FileNameLength);
Steve French38702532007-07-08 15:40:40 +0000616 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000617 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000618 FILE_BOTH_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
620 filename = &pFindData->FileName[0];
621 len = le32_to_cpu(pFindData->FileNameLength);
Steve French4523cc32007-04-30 20:13:06 +0000622 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000623 FIND_FILE_STANDARD_INFO *pFindData =
Steve French5bafd762006-06-07 00:18:43 +0000624 (FIND_FILE_STANDARD_INFO *)current_entry;
625 filename = &pFindData->FileName[0];
Steve French5ddaa682006-08-15 13:35:48 +0000626 len = pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 } else {
Steve French790fe572007-07-07 19:25:05 +0000628 cFYI(1, ("Unknown findfirst level %d",
629 cfile->srch_inf.info_level));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631
Steve French4523cc32007-04-30 20:13:06 +0000632 if (filename) {
633 if (cfile->srch_inf.unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 __le16 *ufilename = (__le16 *)filename;
Steve French4523cc32007-04-30 20:13:06 +0000635 if (len == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /* check for . */
Steve French4523cc32007-04-30 20:13:06 +0000637 if (ufilename[0] == UNICODE_DOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 rc = 1;
Steve French4523cc32007-04-30 20:13:06 +0000639 } else if (len == 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /* check for .. */
Steve French4523cc32007-04-30 20:13:06 +0000641 if ((ufilename[0] == UNICODE_DOT)
Steve French38702532007-07-08 15:40:40 +0000642 && (ufilename[1] == UNICODE_DOT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 rc = 2;
644 }
645 } else /* ASCII */ {
Steve French4523cc32007-04-30 20:13:06 +0000646 if (len == 1) {
Steve French38702532007-07-08 15:40:40 +0000647 if (filename[0] == '.')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 rc = 1;
Steve French4523cc32007-04-30 20:13:06 +0000649 } else if (len == 2) {
Steve French790fe572007-07-07 19:25:05 +0000650 if ((filename[0] == '.') && (filename[1] == '.'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 rc = 2;
652 }
653 }
654 }
655
656 return rc;
657}
658
Steve Frencheafe8702005-09-15 21:47:30 -0700659/* Check if directory that we are searching has changed so we can decide
660 whether we can use the cached search results from the previous search */
Steve French38702532007-07-08 15:40:40 +0000661static int is_dir_changed(struct file *file)
Steve Frencheafe8702005-09-15 21:47:30 -0700662{
Christoph Hellwigc33f8d32007-04-02 18:47:20 +0000663 struct inode *inode = file->f_path.dentry->d_inode;
664 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
Steve Frencheafe8702005-09-15 21:47:30 -0700665
Christoph Hellwigc33f8d32007-04-02 18:47:20 +0000666 if (cifsInfo->time == 0)
Steve Frencheafe8702005-09-15 21:47:30 -0700667 return 1; /* directory was changed, perhaps due to unlink */
668 else
669 return 0;
670
671}
672
Steve French0752f152008-10-07 20:03:33 +0000673static int cifs_save_resume_key(const char *current_entry,
674 struct cifsFileInfo *cifsFile)
675{
676 int rc = 0;
677 unsigned int len = 0;
678 __u16 level;
679 char *filename;
680
681 if ((cifsFile == NULL) || (current_entry == NULL))
682 return -EINVAL;
683
684 level = cifsFile->srch_inf.info_level;
685
686 if (level == SMB_FIND_FILE_UNIX) {
687 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
688
689 filename = &pFindData->FileName[0];
690 if (cifsFile->srch_inf.unicode) {
691 len = cifs_unicode_bytelen(filename);
692 } else {
693 /* BB should we make this strnlen of PATH_MAX? */
694 len = strnlen(filename, PATH_MAX);
695 }
696 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
697 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
698 FILE_DIRECTORY_INFO *pFindData =
699 (FILE_DIRECTORY_INFO *)current_entry;
700 filename = &pFindData->FileName[0];
701 len = le32_to_cpu(pFindData->FileNameLength);
702 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
703 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
704 FILE_FULL_DIRECTORY_INFO *pFindData =
705 (FILE_FULL_DIRECTORY_INFO *)current_entry;
706 filename = &pFindData->FileName[0];
707 len = le32_to_cpu(pFindData->FileNameLength);
708 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
709 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
710 SEARCH_ID_FULL_DIR_INFO *pFindData =
711 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
712 filename = &pFindData->FileName[0];
713 len = le32_to_cpu(pFindData->FileNameLength);
714 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
715 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
716 FILE_BOTH_DIRECTORY_INFO *pFindData =
717 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
718 filename = &pFindData->FileName[0];
719 len = le32_to_cpu(pFindData->FileNameLength);
720 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
721 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
722 FIND_FILE_STANDARD_INFO *pFindData =
723 (FIND_FILE_STANDARD_INFO *)current_entry;
724 filename = &pFindData->FileName[0];
725 /* one byte length, no name conversion */
726 len = (unsigned int)pFindData->FileNameLength;
727 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
728 } else {
729 cFYI(1, ("Unknown findfirst level %d", level));
730 return -EINVAL;
731 }
732 cifsFile->srch_inf.resume_name_len = len;
733 cifsFile->srch_inf.presume_name = filename;
734 return rc;
735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737/* find the corresponding entry in the search */
738/* Note that the SMB server returns search entries for . and .. which
739 complicates logic here if we choose to parse for them and we do not
740 assume that they are located in the findfirst return buffer.*/
741/* We start counting in the buffer with entry 2 and increment for every
742 entry (do not increment for . or .. entry) */
743static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
Steve French38702532007-07-08 15:40:40 +0000744 struct file *file, char **ppCurrentEntry, int *num_to_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 int rc = 0;
747 int pos_in_buf = 0;
748 loff_t first_entry_in_buffer;
749 loff_t index_to_find = file->f_pos;
Steve French38702532007-07-08 15:40:40 +0000750 struct cifsFileInfo *cifsFile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 /* check if index in the buffer */
Steve French50c2f752007-07-13 00:33:32 +0000752
Steve French38702532007-07-08 15:40:40 +0000753 if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
Steve Frencheafe8702005-09-15 21:47:30 -0700754 (num_to_ret == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return -ENOENT;
Steve French50c2f752007-07-13 00:33:32 +0000756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 *ppCurrentEntry = NULL;
Steve French38702532007-07-08 15:40:40 +0000758 first_entry_in_buffer =
759 cifsFile->srch_inf.index_of_last_entry -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 cifsFile->srch_inf.entries_in_buffer;
Steve French60808232006-04-22 15:53:05 +0000761
762 /* if first entry in buf is zero then is first buffer
763 in search response data which means it is likely . and ..
764 will be in this buffer, although some servers do not return
765 . and .. for the root of a drive and for those we need
766 to start two entries earlier */
767
Steve French39798772006-05-31 22:40:51 +0000768 dump_cifs_file_struct(file, "In fce ");
Steve French38702532007-07-08 15:40:40 +0000769 if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
770 is_dir_changed(file)) ||
Steve Frencheafe8702005-09-15 21:47:30 -0700771 (index_to_find < first_entry_in_buffer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /* close and restart search */
Steve French790fe572007-07-07 19:25:05 +0000773 cFYI(1, ("search backing up - close and restart search"));
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000774 write_lock(&GlobalSMBSeslock);
Steve French77c57ec2008-05-13 21:39:32 +0000775 if (!cifsFile->srch_inf.endOfSearch &&
776 !cifsFile->invalidHandle) {
777 cifsFile->invalidHandle = true;
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000778 write_unlock(&GlobalSMBSeslock);
Steve French77c57ec2008-05-13 21:39:32 +0000779 CIFSFindClose(xid, pTcon, cifsFile->netfid);
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000780 } else
781 write_unlock(&GlobalSMBSeslock);
Steve French4523cc32007-04-30 20:13:06 +0000782 if (cifsFile->srch_inf.ntwrk_buf_start) {
Steve French790fe572007-07-07 19:25:05 +0000783 cFYI(1, ("freeing SMB ff cache buf on search rewind"));
Steve French4523cc32007-04-30 20:13:06 +0000784 if (cifsFile->srch_inf.smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +0000785 cifs_small_buf_release(cifsFile->srch_inf.
786 ntwrk_buf_start);
787 else
788 cifs_buf_release(cifsFile->srch_inf.
789 ntwrk_buf_start);
Shirish Pargaonkar76c510a2008-07-24 14:48:33 +0000790 cifsFile->srch_inf.ntwrk_buf_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
Steve French38702532007-07-08 15:40:40 +0000792 rc = initiate_cifs_search(xid, file);
Steve French4523cc32007-04-30 20:13:06 +0000793 if (rc) {
Steve French790fe572007-07-07 19:25:05 +0000794 cFYI(1, ("error %d reinitiating a search on rewind",
795 rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return rc;
797 }
Jeff Laytona364bc02008-10-21 14:42:13 +0000798 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800
Steve French38702532007-07-08 15:40:40 +0000801 while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
Steve French4b18f2a2008-04-29 00:06:05 +0000802 (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
Steve French790fe572007-07-07 19:25:05 +0000803 cFYI(1, ("calling findnext2"));
Steve French38702532007-07-08 15:40:40 +0000804 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
Steve Frenchdfb75332005-06-22 17:13:47 -0700805 &cifsFile->srch_inf);
Jeff Laytona364bc02008-10-21 14:42:13 +0000806 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
Steve French4523cc32007-04-30 20:13:06 +0000807 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return -ENOENT;
809 }
Steve French4523cc32007-04-30 20:13:06 +0000810 if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /* we found the buffer that contains the entry */
812 /* scan and find it */
813 int i;
Steve French38702532007-07-08 15:40:40 +0000814 char *current_entry;
815 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 smbCalcSize((struct smb_hdr *)
817 cifsFile->srch_inf.ntwrk_buf_start);
Steve French60808232006-04-22 15:53:05 +0000818
819 current_entry = cifsFile->srch_inf.srch_entries_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
821 - cifsFile->srch_inf.entries_in_buffer;
822 pos_in_buf = index_to_find - first_entry_in_buffer;
Steve French790fe572007-07-07 19:25:05 +0000823 cFYI(1, ("found entry - pos_in_buf %d", pos_in_buf));
Steve French5bafd762006-06-07 00:18:43 +0000824
Steve Frenchad7a2922008-02-07 23:25:02 +0000825 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
Steve Frenchdfb75332005-06-22 17:13:47 -0700826 /* go entry by entry figuring out which is first */
Steve French38702532007-07-08 15:40:40 +0000827 current_entry = nxt_dir_entry(current_entry, end_of_smb,
Steve French5bafd762006-06-07 00:18:43 +0000828 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
Steve French790fe572007-07-07 19:25:05 +0000830 if ((current_entry == NULL) && (i < pos_in_buf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /* BB fixme - check if we should flag this error */
Steve French38702532007-07-08 15:40:40 +0000832 cERROR(1, ("reached end of buf searching for pos in buf"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 " %d index to find %lld rc %d",
Steve French38702532007-07-08 15:40:40 +0000834 pos_in_buf, index_to_find, rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836 rc = 0;
837 *ppCurrentEntry = current_entry;
838 } else {
Steve French790fe572007-07-07 19:25:05 +0000839 cFYI(1, ("index not in buffer - could not findnext into it"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return 0;
841 }
842
Steve French790fe572007-07-07 19:25:05 +0000843 if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
844 cFYI(1, ("can not return entries pos_in_buf beyond last"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 *num_to_ret = 0;
846 } else
847 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 return rc;
850}
851
852/* inode num, inode type and filename returned */
853static int cifs_get_name_from_search_buf(struct qstr *pqst,
854 char *current_entry, __u16 level, unsigned int unicode,
Jeff Layton18295792009-04-30 20:45:45 -0400855 struct cifs_sb_info *cifs_sb, unsigned int max_len, __u64 *pinum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857 int rc = 0;
858 unsigned int len = 0;
Steve French38702532007-07-08 15:40:40 +0000859 char *filename;
860 struct nls_table *nlt = cifs_sb->local_nls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 *pinum = 0;
863
Steve French790fe572007-07-07 19:25:05 +0000864 if (level == SMB_FIND_FILE_UNIX) {
Steve French38702532007-07-08 15:40:40 +0000865 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 filename = &pFindData->FileName[0];
Steve French790fe572007-07-07 19:25:05 +0000868 if (unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 len = cifs_unicode_bytelen(filename);
870 } else {
871 /* BB should we make this strnlen of PATH_MAX? */
872 len = strnlen(filename, PATH_MAX);
873 }
874
Steve French85a6dac2009-04-01 05:22:00 +0000875 *pinum = le64_to_cpu(pFindData->UniqueId);
Steve French790fe572007-07-07 19:25:05 +0000876 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000877 FILE_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 (FILE_DIRECTORY_INFO *)current_entry;
879 filename = &pFindData->FileName[0];
880 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000881 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000882 FILE_FULL_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 (FILE_FULL_DIRECTORY_INFO *)current_entry;
884 filename = &pFindData->FileName[0];
885 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000886 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
Steve French38702532007-07-08 15:40:40 +0000887 SEARCH_ID_FULL_DIR_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
889 filename = &pFindData->FileName[0];
890 len = le32_to_cpu(pFindData->FileNameLength);
Steve French85a6dac2009-04-01 05:22:00 +0000891 *pinum = le64_to_cpu(pFindData->UniqueId);
Steve French790fe572007-07-07 19:25:05 +0000892 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000893 FILE_BOTH_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
895 filename = &pFindData->FileName[0];
896 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000897 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000898 FIND_FILE_STANDARD_INFO *pFindData =
Steve French5bafd762006-06-07 00:18:43 +0000899 (FIND_FILE_STANDARD_INFO *)current_entry;
900 filename = &pFindData->FileName[0];
901 /* one byte length, no name conversion */
902 len = (unsigned int)pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 } else {
Steve French790fe572007-07-07 19:25:05 +0000904 cFYI(1, ("Unknown findfirst level %d", level));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return -EINVAL;
906 }
Steve French5bafd762006-06-07 00:18:43 +0000907
Steve French790fe572007-07-07 19:25:05 +0000908 if (len > max_len) {
Steve French38702532007-07-08 15:40:40 +0000909 cERROR(1, ("bad search response length %d past smb end", len));
Steve French5bafd762006-06-07 00:18:43 +0000910 return -EINVAL;
911 }
912
Steve French790fe572007-07-07 19:25:05 +0000913 if (unicode) {
Jeff Laytonf5884162009-04-30 07:18:00 -0400914 pqst->len = cifs_from_ucs2((char *) pqst->name,
915 (__le16 *) filename,
Jeff Layton18295792009-04-30 20:45:45 -0400916 UNICODE_NAME_MAX,
917 min(len, max_len), nlt,
Jeff Laytonf5884162009-04-30 07:18:00 -0400918 cifs_sb->mnt_cifs_flags &
919 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 } else {
921 pqst->name = filename;
922 pqst->len = len;
923 }
Steve French38702532007-07-08 15:40:40 +0000924 pqst->hash = full_name_hash(pqst->name, pqst->len);
Steve French790fe572007-07-07 19:25:05 +0000925/* cFYI(1, ("filldir on %s",pqst->name)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return rc;
927}
928
Jeff Layton18295792009-04-30 20:45:45 -0400929static int cifs_filldir(char *pfindEntry, struct file *file, filldir_t filldir,
930 void *direntry, char *scratch_buf, unsigned int max_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
932 int rc = 0;
933 struct qstr qstring;
Steve French38702532007-07-08 15:40:40 +0000934 struct cifsFileInfo *pCifsF;
Steve French2c2130e2007-10-12 19:10:28 +0000935 unsigned int obj_type;
Jeff Layton950ec522009-02-11 08:08:26 -0500936 __u64 inum;
Steve French38702532007-07-08 15:40:40 +0000937 struct cifs_sb_info *cifs_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 struct inode *tmp_inode;
939 struct dentry *tmp_dentry;
940
941 /* get filename and len into qstring */
942 /* get dentry */
943 /* decide whether to create and populate ionde */
Steve French790fe572007-07-07 19:25:05 +0000944 if ((direntry == NULL) || (file == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return -EINVAL;
946
947 pCifsF = file->private_data;
Steve French50c2f752007-07-13 00:33:32 +0000948
Steve French790fe572007-07-07 19:25:05 +0000949 if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return -ENOENT;
951
Steve French38702532007-07-08 15:40:40 +0000952 rc = cifs_entry_is_dot(pfindEntry, pCifsF);
Steve French60808232006-04-22 15:53:05 +0000953 /* skip . and .. since we added them first */
Steve French790fe572007-07-07 19:25:05 +0000954 if (rc != 0)
Steve French60808232006-04-22 15:53:05 +0000955 return 0;
956
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800957 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 qstring.name = scratch_buf;
Steve French38702532007-07-08 15:40:40 +0000960 rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 pCifsF->srch_inf.info_level,
Steve French38702532007-07-08 15:40:40 +0000962 pCifsF->srch_inf.unicode, cifs_sb,
Steve French5bafd762006-06-07 00:18:43 +0000963 max_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 &inum /* returned */);
965
Steve French790fe572007-07-07 19:25:05 +0000966 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return rc;
968
Jeff Layton132ac7b2009-02-10 07:33:57 -0500969 /* only these two infolevels return valid inode numbers */
970 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX ||
971 pCifsF->srch_inf.info_level == SMB_FIND_FILE_ID_FULL_DIR_INFO)
972 rc = construct_dentry(&qstring, file, &tmp_inode, &tmp_dentry,
973 &inum);
974 else
975 rc = construct_dentry(&qstring, file, &tmp_inode, &tmp_dentry,
976 NULL);
977
Steve French790fe572007-07-07 19:25:05 +0000978 if ((tmp_inode == NULL) || (tmp_dentry == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return -ENOMEM;
980
Steve French966ca922005-04-28 22:41:08 -0700981 /* we pass in rc below, indicating whether it is a new inode,
982 so we can figure out whether to invalidate the inode cached
983 data if the file has changed */
Steve French790fe572007-07-07 19:25:05 +0000984 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
Steve French966ca922005-04-28 22:41:08 -0700985 unix_fill_in_inode(tmp_inode,
Steve French5bafd762006-06-07 00:18:43 +0000986 (FILE_UNIX_INFO *)pfindEntry,
987 &obj_type, rc);
Steve French790fe572007-07-07 19:25:05 +0000988 else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
Steve French5bafd762006-06-07 00:18:43 +0000989 fill_in_inode(tmp_inode, 0 /* old level 1 buffer type */,
990 pfindEntry, &obj_type, rc);
991 else
992 fill_in_inode(tmp_inode, 1 /* NT */, pfindEntry, &obj_type, rc);
Steve Frenchb835beb2006-09-06 22:02:22 +0000993
Steve French790fe572007-07-07 19:25:05 +0000994 if (rc) /* new inode - needs to be tied to dentry */ {
Steve Frenchb835beb2006-09-06 22:02:22 +0000995 d_instantiate(tmp_dentry, tmp_inode);
Steve French790fe572007-07-07 19:25:05 +0000996 if (rc == 2)
Steve Frenchb835beb2006-09-06 22:02:22 +0000997 d_rehash(tmp_dentry);
998 }
Steve French50c2f752007-07-13 00:33:32 +0000999
Steve French38702532007-07-08 15:40:40 +00001000
1001 rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
1002 tmp_inode->i_ino, obj_type);
Steve French790fe572007-07-07 19:25:05 +00001003 if (rc) {
1004 cFYI(1, ("filldir rc = %d", rc));
Steve French7ca85ba2006-10-30 21:42:57 +00001005 /* we can not return filldir errors to the caller
1006 since they are "normal" when the stat blocksize
1007 is too small - we return remapped error instead */
1008 rc = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010
1011 dput(tmp_dentry);
1012 return rc;
1013}
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
1017{
1018 int rc = 0;
Steve French38702532007-07-08 15:40:40 +00001019 int xid, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 struct cifs_sb_info *cifs_sb;
1021 struct cifsTconInfo *pTcon;
1022 struct cifsFileInfo *cifsFile = NULL;
Steve French38702532007-07-08 15:40:40 +00001023 char *current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 int num_to_fill = 0;
Steve French38702532007-07-08 15:40:40 +00001025 char *tmp_buf = NULL;
Steve French50c2f752007-07-13 00:33:32 +00001026 char *end_of_smb;
Jeff Layton18295792009-04-30 20:45:45 -04001027 unsigned int max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 xid = GetXid();
1030
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -08001031 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 pTcon = cifs_sb->tcon;
Steve French790fe572007-07-07 19:25:05 +00001033 if (pTcon == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return -EINVAL;
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 switch ((int) file->f_pos) {
1037 case 0:
Steve French60808232006-04-22 15:53:05 +00001038 if (filldir(direntry, ".", 1, file->f_pos,
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -08001039 file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
Steve French60808232006-04-22 15:53:05 +00001040 cERROR(1, ("Filldir for current dir failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 rc = -ENOMEM;
1042 break;
1043 }
Steve French60808232006-04-22 15:53:05 +00001044 file->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 case 1:
Steve French60808232006-04-22 15:53:05 +00001046 if (filldir(direntry, "..", 2, file->f_pos,
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -08001047 file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
Steve French26a21b92006-05-31 18:05:34 +00001048 cERROR(1, ("Filldir for parent dir failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 rc = -ENOMEM;
1050 break;
1051 }
Steve French60808232006-04-22 15:53:05 +00001052 file->f_pos++;
1053 default:
Steve French38702532007-07-08 15:40:40 +00001054 /* 1) If search is active,
1055 is in current search buffer?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if it before then restart search
1057 if after then keep searching till find it */
1058
Steve French790fe572007-07-07 19:25:05 +00001059 if (file->private_data == NULL) {
Steve French38702532007-07-08 15:40:40 +00001060 rc = initiate_cifs_search(xid, file);
Steve French790fe572007-07-07 19:25:05 +00001061 cFYI(1, ("initiate cifs search rc %d", rc));
1062 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 FreeXid(xid);
1064 return rc;
1065 }
1066 }
Steve French790fe572007-07-07 19:25:05 +00001067 if (file->private_data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 rc = -EINVAL;
1069 FreeXid(xid);
1070 return rc;
1071 }
1072 cifsFile = file->private_data;
1073 if (cifsFile->srch_inf.endOfSearch) {
Steve French790fe572007-07-07 19:25:05 +00001074 if (cifsFile->srch_inf.emptyDir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 cFYI(1, ("End of search, empty dir"));
1076 rc = 0;
1077 break;
1078 }
1079 } /* else {
Steve French4b18f2a2008-04-29 00:06:05 +00001080 cifsFile->invalidHandle = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 CIFSFindClose(xid, pTcon, cifsFile->netfid);
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00001082 } */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Steve French38702532007-07-08 15:40:40 +00001084 rc = find_cifs_entry(xid, pTcon, file,
1085 &current_entry, &num_to_fill);
Steve French790fe572007-07-07 19:25:05 +00001086 if (rc) {
1087 cFYI(1, ("fce error %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 goto rddir2_exit;
1089 } else if (current_entry != NULL) {
Steve French790fe572007-07-07 19:25:05 +00001090 cFYI(1, ("entry %lld found", file->f_pos));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 } else {
Steve French790fe572007-07-07 19:25:05 +00001092 cFYI(1, ("could not find entry"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 goto rddir2_exit;
1094 }
Steve French790fe572007-07-07 19:25:05 +00001095 cFYI(1, ("loop through %d times filling dir for net buf %p",
1096 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start));
Steve French5bafd762006-06-07 00:18:43 +00001097 max_len = smbCalcSize((struct smb_hdr *)
1098 cifsFile->srch_inf.ntwrk_buf_start);
1099 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1100
Jeff Laytonf5884162009-04-30 07:18:00 -04001101 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
Steve French790fe572007-07-07 19:25:05 +00001102 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
1103 if (current_entry == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 /* evaluate whether this case is an error */
Steve French26f57362007-08-30 22:09:15 +00001105 cERROR(1, ("past SMB end, num to fill %d i %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 num_to_fill, i));
1107 break;
1108 }
Steve French60808232006-04-22 15:53:05 +00001109 /* if buggy server returns . and .. late do
1110 we want to check for that here? */
Steve French5bafd762006-06-07 00:18:43 +00001111 rc = cifs_filldir(current_entry, file,
1112 filldir, direntry, tmp_buf, max_len);
Steve French790fe572007-07-07 19:25:05 +00001113 if (rc == -EOVERFLOW) {
Steve French7ca85ba2006-10-30 21:42:57 +00001114 rc = 0;
1115 break;
1116 }
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 file->f_pos++;
Steve French790fe572007-07-07 19:25:05 +00001119 if (file->f_pos ==
Steve French39798772006-05-31 22:40:51 +00001120 cifsFile->srch_inf.index_of_last_entry) {
Steve French790fe572007-07-07 19:25:05 +00001121 cFYI(1, ("last entry in buf at pos %lld %s",
1122 file->f_pos, tmp_buf));
1123 cifs_save_resume_key(current_entry, cifsFile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 break;
Steve French790fe572007-07-07 19:25:05 +00001125 } else
1126 current_entry =
Steve French5bafd762006-06-07 00:18:43 +00001127 nxt_dir_entry(current_entry, end_of_smb,
1128 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130 kfree(tmp_buf);
1131 break;
1132 } /* end switch */
1133
1134rddir2_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 FreeXid(xid);
1136 return rc;
1137}