blob: e1351fe18a154180c6653b315309ebce8a8ee7c3 [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 French790fe572007-07-07 19:25:05 +0000118static void AdjustForTZ(struct cifsTconInfo *tcon, struct inode *inode)
Steve French533f90a2006-10-12 00:02:32 +0000119{
Steve French4523cc32007-04-30 20:13:06 +0000120 if ((tcon) && (tcon->ses) && (tcon->ses->server)) {
Steve Frenchddae9572006-10-12 01:23:29 +0000121 inode->i_ctime.tv_sec += tcon->ses->server->timeAdj;
122 inode->i_mtime.tv_sec += tcon->ses->server->timeAdj;
123 inode->i_atime.tv_sec += tcon->ses->server->timeAdj;
Steve French533f90a2006-10-12 00:02:32 +0000124 }
125 return;
126}
127
128
Steve French5bafd762006-06-07 00:18:43 +0000129static void fill_in_inode(struct inode *tmp_inode, int new_buf_type,
Steve French2c2130e2007-10-12 19:10:28 +0000130 char *buf, unsigned int *pobject_type, int isNewInode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Steve French966ca922005-04-28 22:41:08 -0700132 loff_t local_size;
133 struct timespec local_mtime;
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
136 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
Steve French5bafd762006-06-07 00:18:43 +0000137 __u32 attr;
138 __u64 allocation_size;
139 __u64 end_of_file;
Jeff Layton4468eb32008-05-22 09:31:40 -0400140 umode_t default_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Steve French966ca922005-04-28 22:41:08 -0700142 /* save mtime and size */
143 local_mtime = tmp_inode->i_mtime;
144 local_size = tmp_inode->i_size;
145
Steve French4523cc32007-04-30 20:13:06 +0000146 if (new_buf_type) {
Steve French5bafd762006-06-07 00:18:43 +0000147 FILE_DIRECTORY_INFO *pfindData = (FILE_DIRECTORY_INFO *)buf;
148
149 attr = le32_to_cpu(pfindData->ExtFileAttributes);
150 allocation_size = le64_to_cpu(pfindData->AllocationSize);
151 end_of_file = le64_to_cpu(pfindData->EndOfFile);
152 tmp_inode->i_atime =
153 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
154 tmp_inode->i_mtime =
155 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
156 tmp_inode->i_ctime =
157 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
158 } else { /* legacy, OS2 and DOS style */
Steve French1bd5bbc2006-09-28 03:35:57 +0000159/* struct timespec ts;*/
Steve Frenchad7a2922008-02-07 23:25:02 +0000160 FIND_FILE_STANDARD_INFO *pfindData =
Steve French5bafd762006-06-07 00:18:43 +0000161 (FIND_FILE_STANDARD_INFO *)buf;
162
Steve French268f3be2006-10-06 21:47:09 +0000163 tmp_inode->i_mtime = cnvrtDosUnixTm(
Steve French1bd5bbc2006-09-28 03:35:57 +0000164 le16_to_cpu(pfindData->LastWriteDate),
Steve French268f3be2006-10-06 21:47:09 +0000165 le16_to_cpu(pfindData->LastWriteTime));
166 tmp_inode->i_atime = cnvrtDosUnixTm(
167 le16_to_cpu(pfindData->LastAccessDate),
168 le16_to_cpu(pfindData->LastAccessTime));
Steve Frencha8507902007-02-06 20:43:30 +0000169 tmp_inode->i_ctime = cnvrtDosUnixTm(
170 le16_to_cpu(pfindData->LastWriteDate),
171 le16_to_cpu(pfindData->LastWriteTime));
Steve French533f90a2006-10-12 00:02:32 +0000172 AdjustForTZ(cifs_sb->tcon, tmp_inode);
Steve French5bafd762006-06-07 00:18:43 +0000173 attr = le16_to_cpu(pfindData->Attributes);
174 allocation_size = le32_to_cpu(pfindData->AllocationSize);
175 end_of_file = le32_to_cpu(pfindData->DataSize);
Steve French5bafd762006-06-07 00:18:43 +0000176 }
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /* Linux can not store file creation time unfortunately so ignore it */
Steve French5bafd762006-06-07 00:18:43 +0000179
180 cifsInfo->cifsAttrs = attr;
Steve Frencha6f8de32007-11-08 23:10:32 +0000181#ifdef CONFIG_CIFS_EXPERIMENTAL
182 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
183 /* get more accurate mode via ACL - so force inode refresh */
184 cifsInfo->time = 0;
185 } else
186#endif /* CONFIG_CIFS_EXPERIMENTAL */
187 cifsInfo->time = jiffies;
Steve French5bafd762006-06-07 00:18:43 +0000188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
190 /* 2767 perms - indicate mandatory locking */
Steve French790fe572007-07-07 19:25:05 +0000191 /* BB fill in uid and gid here? with help from winbind?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 or retrieve from NTFS stream extended attribute */
193 if (atomic_read(&cifsInfo->inUse) == 0) {
194 tmp_inode->i_uid = cifs_sb->mnt_uid;
195 tmp_inode->i_gid = cifs_sb->mnt_gid;
Jeff Layton4468eb32008-05-22 09:31:40 -0400196 }
197
198 if (attr & ATTR_DIRECTORY)
199 default_mode = cifs_sb->mnt_dir_mode;
200 else
201 default_mode = cifs_sb->mnt_file_mode;
202
203 /* set initial permissions */
204 if ((atomic_read(&cifsInfo->inUse) == 0) ||
205 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
206 tmp_inode->i_mode = default_mode;
207 else {
208 /* just reenable write bits if !ATTR_READONLY */
209 if ((tmp_inode->i_mode & S_IWUGO) == 0 &&
210 (attr & ATTR_READONLY) == 0)
211 tmp_inode->i_mode |= (S_IWUGO & default_mode);
212
Steve French3020a1f2005-11-18 11:31:10 -0800213 tmp_inode->i_mode &= ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
Jeff Layton4468eb32008-05-22 09:31:40 -0400216 /* clear write bits if ATTR_READONLY is set */
217 if (attr & ATTR_READONLY)
218 tmp_inode->i_mode &= ~S_IWUGO;
219
220 /* set inode type */
221 if ((attr & ATTR_SYSTEM) &&
222 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
Steve French3020a1f2005-11-18 11:31:10 -0800223 if (end_of_file == 0) {
Steve French3020a1f2005-11-18 11:31:10 -0800224 tmp_inode->i_mode |= S_IFIFO;
Jeff Layton4468eb32008-05-22 09:31:40 -0400225 *pobject_type = DT_FIFO;
Steve French3020a1f2005-11-18 11:31:10 -0800226 } else {
Jeff Layton4468eb32008-05-22 09:31:40 -0400227 /*
228 * trying to get the type can be slow, so just call
229 * this a regular file for now, and mark for reval
230 */
Steve French790fe572007-07-07 19:25:05 +0000231 tmp_inode->i_mode |= S_IFREG;
Jeff Layton4468eb32008-05-22 09:31:40 -0400232 *pobject_type = DT_REG;
Steve French790fe572007-07-07 19:25:05 +0000233 cifsInfo->time = 0;
Steve French3020a1f2005-11-18 11:31:10 -0800234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 } else {
Jeff Layton4468eb32008-05-22 09:31:40 -0400236 if (attr & ATTR_DIRECTORY) {
237 tmp_inode->i_mode |= S_IFDIR;
238 *pobject_type = DT_DIR;
239 } else {
240 tmp_inode->i_mode |= S_IFREG;
241 *pobject_type = DT_REG;
242 }
243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 /* can not fill in nlink here as in qpathinfo version and Unx search */
Steve Frenchad7a2922008-02-07 23:25:02 +0000246 if (atomic_read(&cifsInfo->inUse) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 atomic_set(&cifsInfo->inUse, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Jeff Laytonfbec9ab2009-04-03 13:44:00 -0400249 cifsInfo->server_eof = end_of_file;
Steve French3677db12007-02-26 16:46:11 +0000250 spin_lock(&tmp_inode->i_lock);
Steve French7ba526312007-02-08 18:14:13 +0000251 if (is_size_safe_to_change(cifsInfo, end_of_file)) {
Steve French790fe572007-07-07 19:25:05 +0000252 /* can not safely change the file size here if the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 client is writing to it due to potential races */
254 i_size_write(tmp_inode, end_of_file);
255
256 /* 512 bytes (2**9) is the fake blocksize that must be used */
257 /* for this calculation, even though the reported blocksize is larger */
258 tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
259 }
Steve French3677db12007-02-26 16:46:11 +0000260 spin_unlock(&tmp_inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if (allocation_size < end_of_file)
263 cFYI(1, ("May be sparse file, allocation less than file size"));
Theodore Ts'oba52de12006-09-27 01:50:49 -0700264 cFYI(1, ("File Size %ld and blocks %llu",
Andrew Morton5515eff2006-03-26 01:37:53 -0800265 (unsigned long)tmp_inode->i_size,
Theodore Ts'oba52de12006-09-27 01:50:49 -0700266 (unsigned long long)tmp_inode->i_blocks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (S_ISREG(tmp_inode->i_mode)) {
Steve French966ca922005-04-28 22:41:08 -0700268 cFYI(1, ("File inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 tmp_inode->i_op = &cifs_file_inode_ops;
Steve French4523cc32007-04-30 20:13:06 +0000270 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
271 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
Steve French8b94bcb2005-11-11 11:41:00 -0800272 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
273 else
274 tmp_inode->i_fop = &cifs_file_direct_ops;
Steve French4523cc32007-04-30 20:13:06 +0000275 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
Steve French8b94bcb2005-11-11 11:41:00 -0800276 tmp_inode->i_fop = &cifs_file_nobrl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 else
278 tmp_inode->i_fop = &cifs_file_ops;
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800279
Steve French4523cc32007-04-30 20:13:06 +0000280 if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
Steve Frenchbfa0d752005-08-31 21:50:37 -0700281 (cifs_sb->tcon->ses->server->maxBuf <
Dave Kleikamp273d81d2006-06-01 19:41:23 +0000282 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
283 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
284 else
285 tmp_inode->i_data.a_ops = &cifs_addr_ops;
286
Steve French4523cc32007-04-30 20:13:06 +0000287 if (isNewInode)
Steve Frenchdfb75332005-06-22 17:13:47 -0700288 return; /* No sense invalidating pages for new inode
289 since have not started caching readahead file
290 data yet */
Steve French966ca922005-04-28 22:41:08 -0700291
292 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
293 (local_size == tmp_inode->i_size)) {
294 cFYI(1, ("inode exists but unchanged"));
295 } else {
296 /* file may have changed on server */
297 cFYI(1, ("invalidate inode, readdir detected change"));
298 invalidate_remote_inode(tmp_inode);
299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 } else if (S_ISDIR(tmp_inode->i_mode)) {
Steve French966ca922005-04-28 22:41:08 -0700301 cFYI(1, ("Directory inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 tmp_inode->i_op = &cifs_dir_inode_ops;
303 tmp_inode->i_fop = &cifs_dir_ops;
304 } else if (S_ISLNK(tmp_inode->i_mode)) {
Steve French966ca922005-04-28 22:41:08 -0700305 cFYI(1, ("Symbolic Link inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 tmp_inode->i_op = &cifs_symlink_inode_ops;
307 } else {
Steve French966ca922005-04-28 22:41:08 -0700308 cFYI(1, ("Init special inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 init_special_inode(tmp_inode, tmp_inode->i_mode,
310 tmp_inode->i_rdev);
311 }
312}
313
314static void unix_fill_in_inode(struct inode *tmp_inode,
Steve French2c2130e2007-10-12 19:10:28 +0000315 FILE_UNIX_INFO *pfindData, unsigned int *pobject_type, int isNewInode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Steve French966ca922005-04-28 22:41:08 -0700317 loff_t local_size;
318 struct timespec local_mtime;
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
321 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
322
323 __u32 type = le32_to_cpu(pfindData->Type);
324 __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes);
325 __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
326 cifsInfo->time = jiffies;
327 atomic_inc(&cifsInfo->inUse);
328
Steve French966ca922005-04-28 22:41:08 -0700329 /* save mtime and size */
330 local_mtime = tmp_inode->i_mtime;
331 local_size = tmp_inode->i_size;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 tmp_inode->i_atime =
334 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
335 tmp_inode->i_mtime =
336 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime));
337 tmp_inode->i_ctime =
338 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange));
339
340 tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions);
Steve French3020a1f2005-11-18 11:31:10 -0800341 /* since we set the inode type below we need to mask off type
Steve French790fe572007-07-07 19:25:05 +0000342 to avoid strange results if bits above were corrupt */
343 tmp_inode->i_mode &= ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (type == UNIX_FILE) {
345 *pobject_type = DT_REG;
346 tmp_inode->i_mode |= S_IFREG;
347 } else if (type == UNIX_SYMLINK) {
348 *pobject_type = DT_LNK;
349 tmp_inode->i_mode |= S_IFLNK;
350 } else if (type == UNIX_DIR) {
351 *pobject_type = DT_DIR;
352 tmp_inode->i_mode |= S_IFDIR;
353 } else if (type == UNIX_CHARDEV) {
354 *pobject_type = DT_CHR;
355 tmp_inode->i_mode |= S_IFCHR;
356 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
357 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
358 } else if (type == UNIX_BLOCKDEV) {
359 *pobject_type = DT_BLK;
360 tmp_inode->i_mode |= S_IFBLK;
361 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
362 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
363 } else if (type == UNIX_FIFO) {
364 *pobject_type = DT_FIFO;
365 tmp_inode->i_mode |= S_IFIFO;
366 } else if (type == UNIX_SOCKET) {
367 *pobject_type = DT_SOCK;
368 tmp_inode->i_mode |= S_IFSOCK;
Steve French3020a1f2005-11-18 11:31:10 -0800369 } else {
370 /* safest to just call it a file */
371 *pobject_type = DT_REG;
372 tmp_inode->i_mode |= S_IFREG;
Steve French790fe572007-07-07 19:25:05 +0000373 cFYI(1, ("unknown inode type %d", type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
Steve French4523cc32007-04-30 20:13:06 +0000376 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
377 tmp_inode->i_uid = cifs_sb->mnt_uid;
378 else
379 tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
380 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
381 tmp_inode->i_gid = cifs_sb->mnt_gid;
382 else
383 tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
385
Jeff Laytonfbec9ab2009-04-03 13:44:00 -0400386 cifsInfo->server_eof = end_of_file;
Steve French3677db12007-02-26 16:46:11 +0000387 spin_lock(&tmp_inode->i_lock);
Steve French7ba526312007-02-08 18:14:13 +0000388 if (is_size_safe_to_change(cifsInfo, end_of_file)) {
Steve French790fe572007-07-07 19:25:05 +0000389 /* can not safely change the file size here if the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 client is writing to it due to potential races */
Steve French7ba526312007-02-08 18:14:13 +0000391 i_size_write(tmp_inode, end_of_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 /* 512 bytes (2**9) is the fake blocksize that must be used */
394 /* for this calculation, not the real blocksize */
395 tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
396 }
Steve French3677db12007-02-26 16:46:11 +0000397 spin_unlock(&tmp_inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 if (S_ISREG(tmp_inode->i_mode)) {
400 cFYI(1, ("File inode"));
401 tmp_inode->i_op = &cifs_file_inode_ops;
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800402
Steve French4523cc32007-04-30 20:13:06 +0000403 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
404 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800405 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
406 else
407 tmp_inode->i_fop = &cifs_file_direct_ops;
Steve French4523cc32007-04-30 20:13:06 +0000408 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800409 tmp_inode->i_fop = &cifs_file_nobrl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 else
411 tmp_inode->i_fop = &cifs_file_ops;
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800412
Steve French4523cc32007-04-30 20:13:06 +0000413 if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
Steve French790fe572007-07-07 19:25:05 +0000414 (cifs_sb->tcon->ses->server->maxBuf <
Dave Kleikamp273d81d2006-06-01 19:41:23 +0000415 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
416 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
417 else
418 tmp_inode->i_data.a_ops = &cifs_addr_ops;
Steve French966ca922005-04-28 22:41:08 -0700419
Steve French4523cc32007-04-30 20:13:06 +0000420 if (isNewInode)
Steve French790fe572007-07-07 19:25:05 +0000421 return; /* No sense invalidating pages for new inode
422 since we have not started caching readahead
423 file data for it yet */
Steve French966ca922005-04-28 22:41:08 -0700424
425 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
426 (local_size == tmp_inode->i_size)) {
427 cFYI(1, ("inode exists but unchanged"));
428 } else {
429 /* file may have changed on server */
430 cFYI(1, ("invalidate inode, readdir detected change"));
431 invalidate_remote_inode(tmp_inode);
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 } else if (S_ISDIR(tmp_inode->i_mode)) {
434 cFYI(1, ("Directory inode"));
435 tmp_inode->i_op = &cifs_dir_inode_ops;
436 tmp_inode->i_fop = &cifs_dir_ops;
437 } else if (S_ISLNK(tmp_inode->i_mode)) {
438 cFYI(1, ("Symbolic Link inode"));
439 tmp_inode->i_op = &cifs_symlink_inode_ops;
440/* tmp_inode->i_fop = *//* do not need to set to anything */
441 } else {
Steve French790fe572007-07-07 19:25:05 +0000442 cFYI(1, ("Special inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 init_special_inode(tmp_inode, tmp_inode->i_mode,
444 tmp_inode->i_rdev);
445 }
446}
447
448static int initiate_cifs_search(const int xid, struct file *file)
449{
450 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000451 char *full_path;
452 struct cifsFileInfo *cifsFile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct cifs_sb_info *cifs_sb;
454 struct cifsTconInfo *pTcon;
455
Steve French4523cc32007-04-30 20:13:06 +0000456 if (file->private_data == NULL) {
Steve French38702532007-07-08 15:40:40 +0000457 file->private_data =
458 kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
Steve French4523cc32007-04-30 20:13:06 +0000461 if (file->private_data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 cifsFile = file->private_data;
Steve French4b18f2a2008-04-29 00:06:05 +0000464 cifsFile->invalidHandle = true;
465 cifsFile->srch_inf.endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800467 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
Steve French4523cc32007-04-30 20:13:06 +0000468 if (cifs_sb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return -EINVAL;
470
471 pTcon = cifs_sb->tcon;
Steve French4523cc32007-04-30 20:13:06 +0000472 if (pTcon == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return -EINVAL;
474
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800475 full_path = build_path_from_dentry(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Steve Frenchad7a2922008-02-07 23:25:02 +0000477 if (full_path == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Steve French966ca922005-04-28 22:41:08 -0700480 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Steve French75cf6bd2005-04-28 22:41:04 -0700482ffirst_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /* test for Unix extensions */
Steve Frenchc18c8422007-07-18 23:21:09 +0000484 /* but now check for them on the share/mount not on the SMB session */
485/* if (pTcon->ses->capabilities & CAP_UNIX) { */
Steve Frenchad7a2922008-02-07 23:25:02 +0000486 if (pTcon->unix_ext)
Steve French5bafd762006-06-07 00:18:43 +0000487 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
Steve Frenchad7a2922008-02-07 23:25:02 +0000488 else if ((pTcon->ses->capabilities &
Steve French5bafd762006-06-07 00:18:43 +0000489 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
490 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
492 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
493 } else /* not srvinos - BB fixme add check for backlevel? */ {
494 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
495 }
496
Steve French38702532007-07-08 15:40:40 +0000497 rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
Steve French737b7582005-04-28 22:41:06 -0700498 &cifsFile->netfid, &cifsFile->srch_inf,
Steve French38702532007-07-08 15:40:40 +0000499 cifs_sb->mnt_cifs_flags &
Steve Frencheafe8702005-09-15 21:47:30 -0700500 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
Steve French4523cc32007-04-30 20:13:06 +0000501 if (rc == 0)
Steve French4b18f2a2008-04-29 00:06:05 +0000502 cifsFile->invalidHandle = false;
Steve French38702532007-07-08 15:40:40 +0000503 if ((rc == -EOPNOTSUPP) &&
Steve French75cf6bd2005-04-28 22:41:04 -0700504 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
505 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
506 goto ffirst_retry;
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 kfree(full_path);
509 return rc;
510}
511
512/* return length of unicode string in bytes */
513static int cifs_unicode_bytelen(char *str)
514{
515 int len;
Steve French63d25832007-11-05 21:46:10 +0000516 __le16 *ustr = (__le16 *)str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Steve French38702532007-07-08 15:40:40 +0000518 for (len = 0; len <= PATH_MAX; len++) {
Steve French4523cc32007-04-30 20:13:06 +0000519 if (ustr[len] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return len << 1;
521 }
Steve French790fe572007-07-07 19:25:05 +0000522 cFYI(1, ("Unicode string longer than PATH_MAX found"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return len << 1;
524}
525
Steve French5bafd762006-06-07 00:18:43 +0000526static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Steve French38702532007-07-08 15:40:40 +0000528 char *new_entry;
Steve Frenchad7a2922008-02-07 23:25:02 +0000529 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Steve French4523cc32007-04-30 20:13:06 +0000531 if (level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000532 FIND_FILE_STANDARD_INFO *pfData;
Steve French5bafd762006-06-07 00:18:43 +0000533 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
534
535 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
536 pfData->FileNameLength;
537 } else
538 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
Steve French790fe572007-07-07 19:25:05 +0000539 cFYI(1, ("new entry %p old entry %p", new_entry, old_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /* validate that new_entry is not past end of SMB */
Steve French4523cc32007-04-30 20:13:06 +0000541 if (new_entry >= end_of_smb) {
Steve French09d1db52005-04-28 22:41:08 -0700542 cERROR(1,
543 ("search entry %p began after end of SMB %p old entry %p",
Steve French38702532007-07-08 15:40:40 +0000544 new_entry, end_of_smb, old_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return NULL;
Steve French4523cc32007-04-30 20:13:06 +0000546 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
Steve French38702532007-07-08 15:40:40 +0000547 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
548 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
Steve French5bafd762006-06-07 00:18:43 +0000549 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
Steve French38702532007-07-08 15:40:40 +0000550 cERROR(1, ("search entry %p extends after end of SMB %p",
Steve French09d1db52005-04-28 22:41:08 -0700551 new_entry, end_of_smb));
552 return NULL;
Steve French38702532007-07-08 15:40:40 +0000553 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return new_entry;
555
556}
557
558#define UNICODE_DOT cpu_to_le16(0x2e)
559
560/* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
561static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
562{
563 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000564 char *filename = NULL;
565 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Steve French4523cc32007-04-30 20:13:06 +0000567 if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000568 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 filename = &pFindData->FileName[0];
Steve French4523cc32007-04-30 20:13:06 +0000570 if (cfile->srch_inf.unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 len = cifs_unicode_bytelen(filename);
572 } else {
573 /* BB should we make this strnlen of PATH_MAX? */
574 len = strnlen(filename, 5);
575 }
Steve French4523cc32007-04-30 20:13:06 +0000576 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000577 FILE_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 (FILE_DIRECTORY_INFO *)current_entry;
579 filename = &pFindData->FileName[0];
580 len = le32_to_cpu(pFindData->FileNameLength);
Steve French38702532007-07-08 15:40:40 +0000581 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000582 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000583 FILE_FULL_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 (FILE_FULL_DIRECTORY_INFO *)current_entry;
585 filename = &pFindData->FileName[0];
586 len = le32_to_cpu(pFindData->FileNameLength);
Steve French4523cc32007-04-30 20:13:06 +0000587 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000588 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000589 SEARCH_ID_FULL_DIR_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
591 filename = &pFindData->FileName[0];
592 len = le32_to_cpu(pFindData->FileNameLength);
Steve French38702532007-07-08 15:40:40 +0000593 } else if (cfile->srch_inf.info_level ==
Steve French5bafd762006-06-07 00:18:43 +0000594 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000595 FILE_BOTH_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
597 filename = &pFindData->FileName[0];
598 len = le32_to_cpu(pFindData->FileNameLength);
Steve French4523cc32007-04-30 20:13:06 +0000599 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000600 FIND_FILE_STANDARD_INFO *pFindData =
Steve French5bafd762006-06-07 00:18:43 +0000601 (FIND_FILE_STANDARD_INFO *)current_entry;
602 filename = &pFindData->FileName[0];
Steve French5ddaa682006-08-15 13:35:48 +0000603 len = pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 } else {
Steve French790fe572007-07-07 19:25:05 +0000605 cFYI(1, ("Unknown findfirst level %d",
606 cfile->srch_inf.info_level));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
Steve French4523cc32007-04-30 20:13:06 +0000609 if (filename) {
610 if (cfile->srch_inf.unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 __le16 *ufilename = (__le16 *)filename;
Steve French4523cc32007-04-30 20:13:06 +0000612 if (len == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* check for . */
Steve French4523cc32007-04-30 20:13:06 +0000614 if (ufilename[0] == UNICODE_DOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 rc = 1;
Steve French4523cc32007-04-30 20:13:06 +0000616 } else if (len == 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* check for .. */
Steve French4523cc32007-04-30 20:13:06 +0000618 if ((ufilename[0] == UNICODE_DOT)
Steve French38702532007-07-08 15:40:40 +0000619 && (ufilename[1] == UNICODE_DOT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 rc = 2;
621 }
622 } else /* ASCII */ {
Steve French4523cc32007-04-30 20:13:06 +0000623 if (len == 1) {
Steve French38702532007-07-08 15:40:40 +0000624 if (filename[0] == '.')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 rc = 1;
Steve French4523cc32007-04-30 20:13:06 +0000626 } else if (len == 2) {
Steve French790fe572007-07-07 19:25:05 +0000627 if ((filename[0] == '.') && (filename[1] == '.'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 rc = 2;
629 }
630 }
631 }
632
633 return rc;
634}
635
Steve Frencheafe8702005-09-15 21:47:30 -0700636/* Check if directory that we are searching has changed so we can decide
637 whether we can use the cached search results from the previous search */
Steve French38702532007-07-08 15:40:40 +0000638static int is_dir_changed(struct file *file)
Steve Frencheafe8702005-09-15 21:47:30 -0700639{
Christoph Hellwigc33f8d32007-04-02 18:47:20 +0000640 struct inode *inode = file->f_path.dentry->d_inode;
641 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
Steve Frencheafe8702005-09-15 21:47:30 -0700642
Christoph Hellwigc33f8d32007-04-02 18:47:20 +0000643 if (cifsInfo->time == 0)
Steve Frencheafe8702005-09-15 21:47:30 -0700644 return 1; /* directory was changed, perhaps due to unlink */
645 else
646 return 0;
647
648}
649
Steve French0752f152008-10-07 20:03:33 +0000650static int cifs_save_resume_key(const char *current_entry,
651 struct cifsFileInfo *cifsFile)
652{
653 int rc = 0;
654 unsigned int len = 0;
655 __u16 level;
656 char *filename;
657
658 if ((cifsFile == NULL) || (current_entry == NULL))
659 return -EINVAL;
660
661 level = cifsFile->srch_inf.info_level;
662
663 if (level == SMB_FIND_FILE_UNIX) {
664 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
665
666 filename = &pFindData->FileName[0];
667 if (cifsFile->srch_inf.unicode) {
668 len = cifs_unicode_bytelen(filename);
669 } else {
670 /* BB should we make this strnlen of PATH_MAX? */
671 len = strnlen(filename, PATH_MAX);
672 }
673 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
674 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
675 FILE_DIRECTORY_INFO *pFindData =
676 (FILE_DIRECTORY_INFO *)current_entry;
677 filename = &pFindData->FileName[0];
678 len = le32_to_cpu(pFindData->FileNameLength);
679 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
680 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
681 FILE_FULL_DIRECTORY_INFO *pFindData =
682 (FILE_FULL_DIRECTORY_INFO *)current_entry;
683 filename = &pFindData->FileName[0];
684 len = le32_to_cpu(pFindData->FileNameLength);
685 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
686 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
687 SEARCH_ID_FULL_DIR_INFO *pFindData =
688 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
689 filename = &pFindData->FileName[0];
690 len = le32_to_cpu(pFindData->FileNameLength);
691 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
692 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
693 FILE_BOTH_DIRECTORY_INFO *pFindData =
694 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
695 filename = &pFindData->FileName[0];
696 len = le32_to_cpu(pFindData->FileNameLength);
697 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
698 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
699 FIND_FILE_STANDARD_INFO *pFindData =
700 (FIND_FILE_STANDARD_INFO *)current_entry;
701 filename = &pFindData->FileName[0];
702 /* one byte length, no name conversion */
703 len = (unsigned int)pFindData->FileNameLength;
704 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
705 } else {
706 cFYI(1, ("Unknown findfirst level %d", level));
707 return -EINVAL;
708 }
709 cifsFile->srch_inf.resume_name_len = len;
710 cifsFile->srch_inf.presume_name = filename;
711 return rc;
712}
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714/* find the corresponding entry in the search */
715/* Note that the SMB server returns search entries for . and .. which
716 complicates logic here if we choose to parse for them and we do not
717 assume that they are located in the findfirst return buffer.*/
718/* We start counting in the buffer with entry 2 and increment for every
719 entry (do not increment for . or .. entry) */
720static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
Steve French38702532007-07-08 15:40:40 +0000721 struct file *file, char **ppCurrentEntry, int *num_to_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 int rc = 0;
724 int pos_in_buf = 0;
725 loff_t first_entry_in_buffer;
726 loff_t index_to_find = file->f_pos;
Steve French38702532007-07-08 15:40:40 +0000727 struct cifsFileInfo *cifsFile = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /* check if index in the buffer */
Steve French50c2f752007-07-13 00:33:32 +0000729
Steve French38702532007-07-08 15:40:40 +0000730 if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
Steve Frencheafe8702005-09-15 21:47:30 -0700731 (num_to_ret == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return -ENOENT;
Steve French50c2f752007-07-13 00:33:32 +0000733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 *ppCurrentEntry = NULL;
Steve French38702532007-07-08 15:40:40 +0000735 first_entry_in_buffer =
736 cifsFile->srch_inf.index_of_last_entry -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 cifsFile->srch_inf.entries_in_buffer;
Steve French60808232006-04-22 15:53:05 +0000738
739 /* if first entry in buf is zero then is first buffer
740 in search response data which means it is likely . and ..
741 will be in this buffer, although some servers do not return
742 . and .. for the root of a drive and for those we need
743 to start two entries earlier */
744
Steve French39798772006-05-31 22:40:51 +0000745 dump_cifs_file_struct(file, "In fce ");
Steve French38702532007-07-08 15:40:40 +0000746 if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
747 is_dir_changed(file)) ||
Steve Frencheafe8702005-09-15 21:47:30 -0700748 (index_to_find < first_entry_in_buffer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /* close and restart search */
Steve French790fe572007-07-07 19:25:05 +0000750 cFYI(1, ("search backing up - close and restart search"));
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000751 write_lock(&GlobalSMBSeslock);
Steve French77c57ec2008-05-13 21:39:32 +0000752 if (!cifsFile->srch_inf.endOfSearch &&
753 !cifsFile->invalidHandle) {
754 cifsFile->invalidHandle = true;
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000755 write_unlock(&GlobalSMBSeslock);
Steve French77c57ec2008-05-13 21:39:32 +0000756 CIFSFindClose(xid, pTcon, cifsFile->netfid);
Steve Frenchddb4cbf2008-11-20 20:00:44 +0000757 } else
758 write_unlock(&GlobalSMBSeslock);
Steve French4523cc32007-04-30 20:13:06 +0000759 if (cifsFile->srch_inf.ntwrk_buf_start) {
Steve French790fe572007-07-07 19:25:05 +0000760 cFYI(1, ("freeing SMB ff cache buf on search rewind"));
Steve French4523cc32007-04-30 20:13:06 +0000761 if (cifsFile->srch_inf.smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +0000762 cifs_small_buf_release(cifsFile->srch_inf.
763 ntwrk_buf_start);
764 else
765 cifs_buf_release(cifsFile->srch_inf.
766 ntwrk_buf_start);
Shirish Pargaonkar76c510a2008-07-24 14:48:33 +0000767 cifsFile->srch_inf.ntwrk_buf_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
Steve French38702532007-07-08 15:40:40 +0000769 rc = initiate_cifs_search(xid, file);
Steve French4523cc32007-04-30 20:13:06 +0000770 if (rc) {
Steve French790fe572007-07-07 19:25:05 +0000771 cFYI(1, ("error %d reinitiating a search on rewind",
772 rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return rc;
774 }
Jeff Laytona364bc02008-10-21 14:42:13 +0000775 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777
Steve French38702532007-07-08 15:40:40 +0000778 while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
Steve French4b18f2a2008-04-29 00:06:05 +0000779 (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
Steve French790fe572007-07-07 19:25:05 +0000780 cFYI(1, ("calling findnext2"));
Steve French38702532007-07-08 15:40:40 +0000781 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
Steve Frenchdfb75332005-06-22 17:13:47 -0700782 &cifsFile->srch_inf);
Jeff Laytona364bc02008-10-21 14:42:13 +0000783 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
Steve French4523cc32007-04-30 20:13:06 +0000784 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return -ENOENT;
786 }
Steve French4523cc32007-04-30 20:13:06 +0000787 if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 /* we found the buffer that contains the entry */
789 /* scan and find it */
790 int i;
Steve French38702532007-07-08 15:40:40 +0000791 char *current_entry;
792 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 smbCalcSize((struct smb_hdr *)
794 cifsFile->srch_inf.ntwrk_buf_start);
Steve French60808232006-04-22 15:53:05 +0000795
796 current_entry = cifsFile->srch_inf.srch_entries_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
798 - cifsFile->srch_inf.entries_in_buffer;
799 pos_in_buf = index_to_find - first_entry_in_buffer;
Steve French790fe572007-07-07 19:25:05 +0000800 cFYI(1, ("found entry - pos_in_buf %d", pos_in_buf));
Steve French5bafd762006-06-07 00:18:43 +0000801
Steve Frenchad7a2922008-02-07 23:25:02 +0000802 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
Steve Frenchdfb75332005-06-22 17:13:47 -0700803 /* go entry by entry figuring out which is first */
Steve French38702532007-07-08 15:40:40 +0000804 current_entry = nxt_dir_entry(current_entry, end_of_smb,
Steve French5bafd762006-06-07 00:18:43 +0000805 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
Steve French790fe572007-07-07 19:25:05 +0000807 if ((current_entry == NULL) && (i < pos_in_buf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /* BB fixme - check if we should flag this error */
Steve French38702532007-07-08 15:40:40 +0000809 cERROR(1, ("reached end of buf searching for pos in buf"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 " %d index to find %lld rc %d",
Steve French38702532007-07-08 15:40:40 +0000811 pos_in_buf, index_to_find, rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813 rc = 0;
814 *ppCurrentEntry = current_entry;
815 } else {
Steve French790fe572007-07-07 19:25:05 +0000816 cFYI(1, ("index not in buffer - could not findnext into it"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return 0;
818 }
819
Steve French790fe572007-07-07 19:25:05 +0000820 if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
821 cFYI(1, ("can not return entries pos_in_buf beyond last"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 *num_to_ret = 0;
823 } else
824 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 return rc;
827}
828
829/* inode num, inode type and filename returned */
830static int cifs_get_name_from_search_buf(struct qstr *pqst,
831 char *current_entry, __u16 level, unsigned int unicode,
Jeff Layton950ec522009-02-11 08:08:26 -0500832 struct cifs_sb_info *cifs_sb, int max_len, __u64 *pinum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 int rc = 0;
835 unsigned int len = 0;
Steve French38702532007-07-08 15:40:40 +0000836 char *filename;
837 struct nls_table *nlt = cifs_sb->local_nls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 *pinum = 0;
840
Steve French790fe572007-07-07 19:25:05 +0000841 if (level == SMB_FIND_FILE_UNIX) {
Steve French38702532007-07-08 15:40:40 +0000842 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 filename = &pFindData->FileName[0];
Steve French790fe572007-07-07 19:25:05 +0000845 if (unicode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 len = cifs_unicode_bytelen(filename);
847 } else {
848 /* BB should we make this strnlen of PATH_MAX? */
849 len = strnlen(filename, PATH_MAX);
850 }
851
Steve French85a6dac2009-04-01 05:22:00 +0000852 *pinum = le64_to_cpu(pFindData->UniqueId);
Steve French790fe572007-07-07 19:25:05 +0000853 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000854 FILE_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 (FILE_DIRECTORY_INFO *)current_entry;
856 filename = &pFindData->FileName[0];
857 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000858 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000859 FILE_FULL_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 (FILE_FULL_DIRECTORY_INFO *)current_entry;
861 filename = &pFindData->FileName[0];
862 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000863 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
Steve French38702532007-07-08 15:40:40 +0000864 SEARCH_ID_FULL_DIR_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
866 filename = &pFindData->FileName[0];
867 len = le32_to_cpu(pFindData->FileNameLength);
Steve French85a6dac2009-04-01 05:22:00 +0000868 *pinum = le64_to_cpu(pFindData->UniqueId);
Steve French790fe572007-07-07 19:25:05 +0000869 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
Steve French38702532007-07-08 15:40:40 +0000870 FILE_BOTH_DIRECTORY_INFO *pFindData =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
872 filename = &pFindData->FileName[0];
873 len = le32_to_cpu(pFindData->FileNameLength);
Steve French790fe572007-07-07 19:25:05 +0000874 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000875 FIND_FILE_STANDARD_INFO *pFindData =
Steve French5bafd762006-06-07 00:18:43 +0000876 (FIND_FILE_STANDARD_INFO *)current_entry;
877 filename = &pFindData->FileName[0];
878 /* one byte length, no name conversion */
879 len = (unsigned int)pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 } else {
Steve French790fe572007-07-07 19:25:05 +0000881 cFYI(1, ("Unknown findfirst level %d", level));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return -EINVAL;
883 }
Steve French5bafd762006-06-07 00:18:43 +0000884
Steve French790fe572007-07-07 19:25:05 +0000885 if (len > max_len) {
Steve French38702532007-07-08 15:40:40 +0000886 cERROR(1, ("bad search response length %d past smb end", len));
Steve French5bafd762006-06-07 00:18:43 +0000887 return -EINVAL;
888 }
889
Steve French790fe572007-07-07 19:25:05 +0000890 if (unicode) {
Jeff Laytonf5884162009-04-30 07:18:00 -0400891 pqst->len = cifs_from_ucs2((char *) pqst->name,
892 (__le16 *) filename,
893 UNICODE_NAME_MAX, max_len, nlt,
894 cifs_sb->mnt_cifs_flags &
895 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 } else {
897 pqst->name = filename;
898 pqst->len = len;
899 }
Steve French38702532007-07-08 15:40:40 +0000900 pqst->hash = full_name_hash(pqst->name, pqst->len);
Steve French790fe572007-07-07 19:25:05 +0000901/* cFYI(1, ("filldir on %s",pqst->name)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return rc;
903}
904
905static int cifs_filldir(char *pfindEntry, struct file *file,
Steve French5bafd762006-06-07 00:18:43 +0000906 filldir_t filldir, void *direntry, char *scratch_buf, int max_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 int rc = 0;
909 struct qstr qstring;
Steve French38702532007-07-08 15:40:40 +0000910 struct cifsFileInfo *pCifsF;
Steve French2c2130e2007-10-12 19:10:28 +0000911 unsigned int obj_type;
Jeff Layton950ec522009-02-11 08:08:26 -0500912 __u64 inum;
Steve French38702532007-07-08 15:40:40 +0000913 struct cifs_sb_info *cifs_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 struct inode *tmp_inode;
915 struct dentry *tmp_dentry;
916
917 /* get filename and len into qstring */
918 /* get dentry */
919 /* decide whether to create and populate ionde */
Steve French790fe572007-07-07 19:25:05 +0000920 if ((direntry == NULL) || (file == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return -EINVAL;
922
923 pCifsF = file->private_data;
Steve French50c2f752007-07-13 00:33:32 +0000924
Steve French790fe572007-07-07 19:25:05 +0000925 if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return -ENOENT;
927
Steve French38702532007-07-08 15:40:40 +0000928 rc = cifs_entry_is_dot(pfindEntry, pCifsF);
Steve French60808232006-04-22 15:53:05 +0000929 /* skip . and .. since we added them first */
Steve French790fe572007-07-07 19:25:05 +0000930 if (rc != 0)
Steve French60808232006-04-22 15:53:05 +0000931 return 0;
932
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800933 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 qstring.name = scratch_buf;
Steve French38702532007-07-08 15:40:40 +0000936 rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 pCifsF->srch_inf.info_level,
Steve French38702532007-07-08 15:40:40 +0000938 pCifsF->srch_inf.unicode, cifs_sb,
Steve French5bafd762006-06-07 00:18:43 +0000939 max_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 &inum /* returned */);
941
Steve French790fe572007-07-07 19:25:05 +0000942 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return rc;
944
Jeff Layton132ac7b2009-02-10 07:33:57 -0500945 /* only these two infolevels return valid inode numbers */
946 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX ||
947 pCifsF->srch_inf.info_level == SMB_FIND_FILE_ID_FULL_DIR_INFO)
948 rc = construct_dentry(&qstring, file, &tmp_inode, &tmp_dentry,
949 &inum);
950 else
951 rc = construct_dentry(&qstring, file, &tmp_inode, &tmp_dentry,
952 NULL);
953
Steve French790fe572007-07-07 19:25:05 +0000954 if ((tmp_inode == NULL) || (tmp_dentry == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return -ENOMEM;
956
Steve French966ca922005-04-28 22:41:08 -0700957 /* we pass in rc below, indicating whether it is a new inode,
958 so we can figure out whether to invalidate the inode cached
959 data if the file has changed */
Steve French790fe572007-07-07 19:25:05 +0000960 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
Steve French966ca922005-04-28 22:41:08 -0700961 unix_fill_in_inode(tmp_inode,
Steve French5bafd762006-06-07 00:18:43 +0000962 (FILE_UNIX_INFO *)pfindEntry,
963 &obj_type, rc);
Steve French790fe572007-07-07 19:25:05 +0000964 else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
Steve French5bafd762006-06-07 00:18:43 +0000965 fill_in_inode(tmp_inode, 0 /* old level 1 buffer type */,
966 pfindEntry, &obj_type, rc);
967 else
968 fill_in_inode(tmp_inode, 1 /* NT */, pfindEntry, &obj_type, rc);
Steve Frenchb835beb2006-09-06 22:02:22 +0000969
Steve French790fe572007-07-07 19:25:05 +0000970 if (rc) /* new inode - needs to be tied to dentry */ {
Steve Frenchb835beb2006-09-06 22:02:22 +0000971 d_instantiate(tmp_dentry, tmp_inode);
Steve French790fe572007-07-07 19:25:05 +0000972 if (rc == 2)
Steve Frenchb835beb2006-09-06 22:02:22 +0000973 d_rehash(tmp_dentry);
974 }
Steve French50c2f752007-07-13 00:33:32 +0000975
Steve French38702532007-07-08 15:40:40 +0000976
977 rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
978 tmp_inode->i_ino, obj_type);
Steve French790fe572007-07-07 19:25:05 +0000979 if (rc) {
980 cFYI(1, ("filldir rc = %d", rc));
Steve French7ca85ba2006-10-30 21:42:57 +0000981 /* we can not return filldir errors to the caller
982 since they are "normal" when the stat blocksize
983 is too small - we return remapped error instead */
984 rc = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986
987 dput(tmp_dentry);
988 return rc;
989}
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
993{
994 int rc = 0;
Steve French38702532007-07-08 15:40:40 +0000995 int xid, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 struct cifs_sb_info *cifs_sb;
997 struct cifsTconInfo *pTcon;
998 struct cifsFileInfo *cifsFile = NULL;
Steve French38702532007-07-08 15:40:40 +0000999 char *current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 int num_to_fill = 0;
Steve French38702532007-07-08 15:40:40 +00001001 char *tmp_buf = NULL;
Steve French50c2f752007-07-13 00:33:32 +00001002 char *end_of_smb;
Steve French5bafd762006-06-07 00:18:43 +00001003 int max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 xid = GetXid();
1006
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -08001007 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 pTcon = cifs_sb->tcon;
Steve French790fe572007-07-07 19:25:05 +00001009 if (pTcon == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return -EINVAL;
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 switch ((int) file->f_pos) {
1013 case 0:
Steve French60808232006-04-22 15:53:05 +00001014 if (filldir(direntry, ".", 1, file->f_pos,
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -08001015 file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
Steve French60808232006-04-22 15:53:05 +00001016 cERROR(1, ("Filldir for current dir failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 rc = -ENOMEM;
1018 break;
1019 }
Steve French60808232006-04-22 15:53:05 +00001020 file->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 case 1:
Steve French60808232006-04-22 15:53:05 +00001022 if (filldir(direntry, "..", 2, file->f_pos,
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -08001023 file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
Steve French26a21b92006-05-31 18:05:34 +00001024 cERROR(1, ("Filldir for parent dir failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 rc = -ENOMEM;
1026 break;
1027 }
Steve French60808232006-04-22 15:53:05 +00001028 file->f_pos++;
1029 default:
Steve French38702532007-07-08 15:40:40 +00001030 /* 1) If search is active,
1031 is in current search buffer?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if it before then restart search
1033 if after then keep searching till find it */
1034
Steve French790fe572007-07-07 19:25:05 +00001035 if (file->private_data == NULL) {
Steve French38702532007-07-08 15:40:40 +00001036 rc = initiate_cifs_search(xid, file);
Steve French790fe572007-07-07 19:25:05 +00001037 cFYI(1, ("initiate cifs search rc %d", rc));
1038 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 FreeXid(xid);
1040 return rc;
1041 }
1042 }
Steve French790fe572007-07-07 19:25:05 +00001043 if (file->private_data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 rc = -EINVAL;
1045 FreeXid(xid);
1046 return rc;
1047 }
1048 cifsFile = file->private_data;
1049 if (cifsFile->srch_inf.endOfSearch) {
Steve French790fe572007-07-07 19:25:05 +00001050 if (cifsFile->srch_inf.emptyDir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 cFYI(1, ("End of search, empty dir"));
1052 rc = 0;
1053 break;
1054 }
1055 } /* else {
Steve French4b18f2a2008-04-29 00:06:05 +00001056 cifsFile->invalidHandle = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 CIFSFindClose(xid, pTcon, cifsFile->netfid);
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00001058 } */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Steve French38702532007-07-08 15:40:40 +00001060 rc = find_cifs_entry(xid, pTcon, file,
1061 &current_entry, &num_to_fill);
Steve French790fe572007-07-07 19:25:05 +00001062 if (rc) {
1063 cFYI(1, ("fce error %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 goto rddir2_exit;
1065 } else if (current_entry != NULL) {
Steve French790fe572007-07-07 19:25:05 +00001066 cFYI(1, ("entry %lld found", file->f_pos));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 } else {
Steve French790fe572007-07-07 19:25:05 +00001068 cFYI(1, ("could not find entry"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 goto rddir2_exit;
1070 }
Steve French790fe572007-07-07 19:25:05 +00001071 cFYI(1, ("loop through %d times filling dir for net buf %p",
1072 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start));
Steve French5bafd762006-06-07 00:18:43 +00001073 max_len = smbCalcSize((struct smb_hdr *)
1074 cifsFile->srch_inf.ntwrk_buf_start);
1075 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1076
Jeff Laytonf5884162009-04-30 07:18:00 -04001077 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
Steve French790fe572007-07-07 19:25:05 +00001078 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
1079 if (current_entry == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 /* evaluate whether this case is an error */
Steve French26f57362007-08-30 22:09:15 +00001081 cERROR(1, ("past SMB end, num to fill %d i %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 num_to_fill, i));
1083 break;
1084 }
Steve French60808232006-04-22 15:53:05 +00001085 /* if buggy server returns . and .. late do
1086 we want to check for that here? */
Steve French5bafd762006-06-07 00:18:43 +00001087 rc = cifs_filldir(current_entry, file,
1088 filldir, direntry, tmp_buf, max_len);
Steve French790fe572007-07-07 19:25:05 +00001089 if (rc == -EOVERFLOW) {
Steve French7ca85ba2006-10-30 21:42:57 +00001090 rc = 0;
1091 break;
1092 }
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 file->f_pos++;
Steve French790fe572007-07-07 19:25:05 +00001095 if (file->f_pos ==
Steve French39798772006-05-31 22:40:51 +00001096 cifsFile->srch_inf.index_of_last_entry) {
Steve French790fe572007-07-07 19:25:05 +00001097 cFYI(1, ("last entry in buf at pos %lld %s",
1098 file->f_pos, tmp_buf));
1099 cifs_save_resume_key(current_entry, cifsFile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 break;
Steve French790fe572007-07-07 19:25:05 +00001101 } else
1102 current_entry =
Steve French5bafd762006-06-07 00:18:43 +00001103 nxt_dir_entry(current_entry, end_of_smb,
1104 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106 kfree(tmp_buf);
1107 break;
1108 } /* end switch */
1109
1110rddir2_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 FreeXid(xid);
1112 return rc;
1113}