blob: 105761e3ba0ea8277da8c95c5ccb28ca06446389 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/readdir.c
3 *
4 * Directory search handling
5 *
Steve French966ca922005-04-28 22:41:08 -07006 * Copyright (C) International Business Machines Corp., 2004, 2005
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>
26#include <linux/smp_lock.h>
27#include "cifspdu.h"
28#include "cifsglob.h"
29#include "cifsproto.h"
30#include "cifs_unicode.h"
31#include "cifs_debug.h"
32#include "cifs_fs_sb.h"
33#include "cifsfs.h"
34
Steve French39798772006-05-31 22:40:51 +000035#ifdef CONFIG_CIFS_DEBUG2
36static void dump_cifs_file_struct(struct file *file, char *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
38 struct cifsFileInfo * cf;
39
40 if(file) {
41 cf = file->private_data;
42 if(cf == NULL) {
43 cFYI(1,("empty cifs private file data"));
44 return;
45 }
46 if(cf->invalidHandle) {
47 cFYI(1,("invalid handle"));
48 }
49 if(cf->srch_inf.endOfSearch) {
50 cFYI(1,("end of search"));
51 }
52 if(cf->srch_inf.emptyDir) {
53 cFYI(1,("empty dir"));
54 }
55
56 }
Steve French39798772006-05-31 22:40:51 +000057}
58#endif /* DEBUG2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/* Returns one if new inode created (which therefore needs to be hashed) */
61/* Might check in the future if inode number changed so we can rehash inode */
62static int construct_dentry(struct qstr *qstring, struct file *file,
63 struct inode **ptmp_inode, struct dentry **pnew_dentry)
64{
65 struct dentry *tmp_dentry;
66 struct cifs_sb_info *cifs_sb;
67 struct cifsTconInfo *pTcon;
68 int rc = 0;
69
Steve French966ca922005-04-28 22:41:08 -070070 cFYI(1, ("For %s", qstring->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
72 pTcon = cifs_sb->tcon;
73
74 qstring->hash = full_name_hash(qstring->name, qstring->len);
75 tmp_dentry = d_lookup(file->f_dentry, qstring);
76 if (tmp_dentry) {
Steve French966ca922005-04-28 22:41:08 -070077 cFYI(0, ("existing dentry with inode 0x%p", tmp_dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *ptmp_inode = tmp_dentry->d_inode;
79/* BB overwrite old name? i.e. tmp_dentry->d_name and tmp_dentry->d_name.len??*/
80 if(*ptmp_inode == NULL) {
81 *ptmp_inode = new_inode(file->f_dentry->d_sb);
82 if(*ptmp_inode == NULL)
83 return rc;
84 rc = 1;
85 d_instantiate(tmp_dentry, *ptmp_inode);
86 }
87 } else {
88 tmp_dentry = d_alloc(file->f_dentry, qstring);
89 if(tmp_dentry == NULL) {
90 cERROR(1,("Failed allocating dentry"));
91 *ptmp_inode = NULL;
92 return rc;
93 }
94
95 *ptmp_inode = new_inode(file->f_dentry->d_sb);
Steve Frenchb92327f2005-08-22 20:09:43 -070096 if (pTcon->nocase)
97 tmp_dentry->d_op = &cifs_ci_dentry_ops;
98 else
99 tmp_dentry->d_op = &cifs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if(*ptmp_inode == NULL)
101 return rc;
102 rc = 1;
103 d_instantiate(tmp_dentry, *ptmp_inode);
104 d_rehash(tmp_dentry);
105 }
106
107 tmp_dentry->d_time = jiffies;
108 *pnew_dentry = tmp_dentry;
109 return rc;
110}
111
Steve French5bafd762006-06-07 00:18:43 +0000112static void fill_in_inode(struct inode *tmp_inode, int new_buf_type,
113 char * buf, int *pobject_type, int isNewInode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Steve French966ca922005-04-28 22:41:08 -0700115 loff_t local_size;
116 struct timespec local_mtime;
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
119 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
Steve French5bafd762006-06-07 00:18:43 +0000120 __u32 attr;
121 __u64 allocation_size;
122 __u64 end_of_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Steve French966ca922005-04-28 22:41:08 -0700124 /* save mtime and size */
125 local_mtime = tmp_inode->i_mtime;
126 local_size = tmp_inode->i_size;
127
Steve French5bafd762006-06-07 00:18:43 +0000128 if(new_buf_type) {
129 FILE_DIRECTORY_INFO *pfindData = (FILE_DIRECTORY_INFO *)buf;
130
131 attr = le32_to_cpu(pfindData->ExtFileAttributes);
132 allocation_size = le64_to_cpu(pfindData->AllocationSize);
133 end_of_file = le64_to_cpu(pfindData->EndOfFile);
134 tmp_inode->i_atime =
135 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
136 tmp_inode->i_mtime =
137 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
138 tmp_inode->i_ctime =
139 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
140 } else { /* legacy, OS2 and DOS style */
141 FIND_FILE_STANDARD_INFO * pfindData =
142 (FIND_FILE_STANDARD_INFO *)buf;
143
144 attr = le16_to_cpu(pfindData->Attributes);
145 allocation_size = le32_to_cpu(pfindData->AllocationSize);
146 end_of_file = le32_to_cpu(pfindData->DataSize);
147 tmp_inode->i_atime = CURRENT_TIME;
148 /* tmp_inode->i_mtime = BB FIXME - add dos time handling
149 tmp_inode->i_ctime = 0; BB FIXME */
150
151 }
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* Linux can not store file creation time unfortunately so ignore it */
Steve French5bafd762006-06-07 00:18:43 +0000154
155 cifsInfo->cifsAttrs = attr;
156 cifsInfo->time = jiffies;
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
159 /* 2767 perms - indicate mandatory locking */
160 /* BB fill in uid and gid here? with help from winbind?
161 or retrieve from NTFS stream extended attribute */
162 if (atomic_read(&cifsInfo->inUse) == 0) {
163 tmp_inode->i_uid = cifs_sb->mnt_uid;
164 tmp_inode->i_gid = cifs_sb->mnt_gid;
165 /* set default mode. will override for dirs below */
166 tmp_inode->i_mode = cifs_sb->mnt_file_mode;
Steve French3020a1f2005-11-18 11:31:10 -0800167 } else {
168 /* mask off the type bits since it gets set
169 below and we do not want to get two type
170 bits set */
171 tmp_inode->i_mode &= ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (attr & ATTR_DIRECTORY) {
175 *pobject_type = DT_DIR;
176 /* override default perms since we do not lock dirs */
177 if(atomic_read(&cifsInfo->inUse) == 0) {
178 tmp_inode->i_mode = cifs_sb->mnt_dir_mode;
179 }
180 tmp_inode->i_mode |= S_IFDIR;
Steve Frencheda3c022005-07-21 15:20:28 -0700181 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
Steve French3020a1f2005-11-18 11:31:10 -0800182 (attr & ATTR_SYSTEM)) {
183 if (end_of_file == 0) {
184 *pobject_type = DT_FIFO;
185 tmp_inode->i_mode |= S_IFIFO;
186 } else {
187 /* rather than get the type here, we mark the
188 inode as needing revalidate and get the real type
189 (blk vs chr vs. symlink) later ie in lookup */
190 *pobject_type = DT_REG;
191 tmp_inode->i_mode |= S_IFREG;
192 cifsInfo->time = 0;
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/* we no longer mark these because we could not follow them */
195/* } else if (attr & ATTR_REPARSE) {
196 *pobject_type = DT_LNK;
197 tmp_inode->i_mode |= S_IFLNK; */
198 } else {
199 *pobject_type = DT_REG;
200 tmp_inode->i_mode |= S_IFREG;
201 if (attr & ATTR_READONLY)
202 tmp_inode->i_mode &= ~(S_IWUGO);
203 } /* could add code here - to validate if device or weird share type? */
204
205 /* can not fill in nlink here as in qpathinfo version and Unx search */
206 if (atomic_read(&cifsInfo->inUse) == 0) {
207 atomic_set(&cifsInfo->inUse, 1);
208 }
209
210 if (is_size_safe_to_change(cifsInfo)) {
211 /* can not safely change the file size here if the
212 client is writing to it due to potential races */
213 i_size_write(tmp_inode, end_of_file);
214
215 /* 512 bytes (2**9) is the fake blocksize that must be used */
216 /* for this calculation, even though the reported blocksize is larger */
217 tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
218 }
219
220 if (allocation_size < end_of_file)
221 cFYI(1, ("May be sparse file, allocation less than file size"));
Andrew Morton5515eff2006-03-26 01:37:53 -0800222 cFYI(1, ("File Size %ld and blocks %llu and blocksize %ld",
223 (unsigned long)tmp_inode->i_size,
224 (unsigned long long)tmp_inode->i_blocks,
225 tmp_inode->i_blksize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (S_ISREG(tmp_inode->i_mode)) {
Steve French966ca922005-04-28 22:41:08 -0700227 cFYI(1, ("File inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 tmp_inode->i_op = &cifs_file_inode_ops;
Steve French8b94bcb2005-11-11 11:41:00 -0800229 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
230 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
231 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
232 else
233 tmp_inode->i_fop = &cifs_file_direct_ops;
234
235 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
236 tmp_inode->i_fop = &cifs_file_nobrl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 else
238 tmp_inode->i_fop = &cifs_file_ops;
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800239
Steve Frenchbfa0d752005-08-31 21:50:37 -0700240 if((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
241 (cifs_sb->tcon->ses->server->maxBuf <
Dave Kleikamp273d81d2006-06-01 19:41:23 +0000242 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
243 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
244 else
245 tmp_inode->i_data.a_ops = &cifs_addr_ops;
246
Steve French966ca922005-04-28 22:41:08 -0700247 if(isNewInode)
Steve Frenchdfb75332005-06-22 17:13:47 -0700248 return; /* No sense invalidating pages for new inode
249 since have not started caching readahead file
250 data yet */
Steve French966ca922005-04-28 22:41:08 -0700251
252 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
253 (local_size == tmp_inode->i_size)) {
254 cFYI(1, ("inode exists but unchanged"));
255 } else {
256 /* file may have changed on server */
257 cFYI(1, ("invalidate inode, readdir detected change"));
258 invalidate_remote_inode(tmp_inode);
259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 } else if (S_ISDIR(tmp_inode->i_mode)) {
Steve French966ca922005-04-28 22:41:08 -0700261 cFYI(1, ("Directory inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 tmp_inode->i_op = &cifs_dir_inode_ops;
263 tmp_inode->i_fop = &cifs_dir_ops;
264 } else if (S_ISLNK(tmp_inode->i_mode)) {
Steve French966ca922005-04-28 22:41:08 -0700265 cFYI(1, ("Symbolic Link inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 tmp_inode->i_op = &cifs_symlink_inode_ops;
267 } else {
Steve French966ca922005-04-28 22:41:08 -0700268 cFYI(1, ("Init special inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 init_special_inode(tmp_inode, tmp_inode->i_mode,
270 tmp_inode->i_rdev);
271 }
272}
273
274static void unix_fill_in_inode(struct inode *tmp_inode,
Steve French966ca922005-04-28 22:41:08 -0700275 FILE_UNIX_INFO *pfindData, int *pobject_type, int isNewInode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Steve French966ca922005-04-28 22:41:08 -0700277 loff_t local_size;
278 struct timespec local_mtime;
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
281 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
282
283 __u32 type = le32_to_cpu(pfindData->Type);
284 __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes);
285 __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
286 cifsInfo->time = jiffies;
287 atomic_inc(&cifsInfo->inUse);
288
Steve French966ca922005-04-28 22:41:08 -0700289 /* save mtime and size */
290 local_mtime = tmp_inode->i_mtime;
291 local_size = tmp_inode->i_size;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 tmp_inode->i_atime =
294 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
295 tmp_inode->i_mtime =
296 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime));
297 tmp_inode->i_ctime =
298 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange));
299
300 tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions);
Steve French3020a1f2005-11-18 11:31:10 -0800301 /* since we set the inode type below we need to mask off type
302 to avoid strange results if bits above were corrupt */
303 tmp_inode->i_mode &= ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (type == UNIX_FILE) {
305 *pobject_type = DT_REG;
306 tmp_inode->i_mode |= S_IFREG;
307 } else if (type == UNIX_SYMLINK) {
308 *pobject_type = DT_LNK;
309 tmp_inode->i_mode |= S_IFLNK;
310 } else if (type == UNIX_DIR) {
311 *pobject_type = DT_DIR;
312 tmp_inode->i_mode |= S_IFDIR;
313 } else if (type == UNIX_CHARDEV) {
314 *pobject_type = DT_CHR;
315 tmp_inode->i_mode |= S_IFCHR;
316 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
317 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
318 } else if (type == UNIX_BLOCKDEV) {
319 *pobject_type = DT_BLK;
320 tmp_inode->i_mode |= S_IFBLK;
321 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
322 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
323 } else if (type == UNIX_FIFO) {
324 *pobject_type = DT_FIFO;
325 tmp_inode->i_mode |= S_IFIFO;
326 } else if (type == UNIX_SOCKET) {
327 *pobject_type = DT_SOCK;
328 tmp_inode->i_mode |= S_IFSOCK;
Steve French3020a1f2005-11-18 11:31:10 -0800329 } else {
330 /* safest to just call it a file */
331 *pobject_type = DT_REG;
332 tmp_inode->i_mode |= S_IFREG;
333 cFYI(1,("unknown inode type %d",type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
336 tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
337 tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
338 tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
339
340 if (is_size_safe_to_change(cifsInfo)) {
341 /* can not safely change the file size here if the
342 client is writing to it due to potential races */
343 i_size_write(tmp_inode,end_of_file);
344
345 /* 512 bytes (2**9) is the fake blocksize that must be used */
346 /* for this calculation, not the real blocksize */
347 tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
348 }
349
350 if (S_ISREG(tmp_inode->i_mode)) {
351 cFYI(1, ("File inode"));
352 tmp_inode->i_op = &cifs_file_inode_ops;
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800353
354 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
355 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
356 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
357 else
358 tmp_inode->i_fop = &cifs_file_direct_ops;
359
360 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
361 tmp_inode->i_fop = &cifs_file_nobrl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 else
363 tmp_inode->i_fop = &cifs_file_ops;
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800364
Steve Frenchbfa0d752005-08-31 21:50:37 -0700365 if((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
366 (cifs_sb->tcon->ses->server->maxBuf <
Dave Kleikamp273d81d2006-06-01 19:41:23 +0000367 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
368 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
369 else
370 tmp_inode->i_data.a_ops = &cifs_addr_ops;
Steve French966ca922005-04-28 22:41:08 -0700371
372 if(isNewInode)
373 return; /* No sense invalidating pages for new inode since we
374 have not started caching readahead file data yet */
375
376 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
377 (local_size == tmp_inode->i_size)) {
378 cFYI(1, ("inode exists but unchanged"));
379 } else {
380 /* file may have changed on server */
381 cFYI(1, ("invalidate inode, readdir detected change"));
382 invalidate_remote_inode(tmp_inode);
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 } else if (S_ISDIR(tmp_inode->i_mode)) {
385 cFYI(1, ("Directory inode"));
386 tmp_inode->i_op = &cifs_dir_inode_ops;
387 tmp_inode->i_fop = &cifs_dir_ops;
388 } else if (S_ISLNK(tmp_inode->i_mode)) {
389 cFYI(1, ("Symbolic Link inode"));
390 tmp_inode->i_op = &cifs_symlink_inode_ops;
391/* tmp_inode->i_fop = *//* do not need to set to anything */
392 } else {
393 cFYI(1, ("Special inode"));
394 init_special_inode(tmp_inode, tmp_inode->i_mode,
395 tmp_inode->i_rdev);
396 }
397}
398
399static int initiate_cifs_search(const int xid, struct file *file)
400{
401 int rc = 0;
402 char * full_path;
403 struct cifsFileInfo * cifsFile;
404 struct cifs_sb_info *cifs_sb;
405 struct cifsTconInfo *pTcon;
406
407 if(file->private_data == NULL) {
408 file->private_data =
409 kmalloc(sizeof(struct cifsFileInfo),GFP_KERNEL);
410 }
411
412 if(file->private_data == NULL) {
413 return -ENOMEM;
414 } else {
415 memset(file->private_data,0,sizeof(struct cifsFileInfo));
416 }
417 cifsFile = file->private_data;
418 cifsFile->invalidHandle = TRUE;
419 cifsFile->srch_inf.endOfSearch = FALSE;
420
421 if(file->f_dentry == NULL)
422 return -ENOENT;
423
424 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
425 if(cifs_sb == NULL)
426 return -EINVAL;
427
428 pTcon = cifs_sb->tcon;
429 if(pTcon == NULL)
430 return -EINVAL;
431
Steve French7f573562005-08-30 11:32:14 -0700432 full_path = build_path_from_dentry(file->f_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 if(full_path == NULL) {
435 return -ENOMEM;
436 }
437
Steve French966ca922005-04-28 22:41:08 -0700438 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Steve French75cf6bd2005-04-28 22:41:04 -0700440ffirst_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /* test for Unix extensions */
442 if (pTcon->ses->capabilities & CAP_UNIX) {
Steve French5bafd762006-06-07 00:18:43 +0000443 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
444 } else if ((pTcon->ses->capabilities &
445 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
446 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
448 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
449 } else /* not srvinos - BB fixme add check for backlevel? */ {
450 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
451 }
452
Steve French737b7582005-04-28 22:41:06 -0700453 rc = CIFSFindFirst(xid, pTcon,full_path,cifs_sb->local_nls,
454 &cifsFile->netfid, &cifsFile->srch_inf,
Steve Frencheafe8702005-09-15 21:47:30 -0700455 cifs_sb->mnt_cifs_flags &
456 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if(rc == 0)
458 cifsFile->invalidHandle = FALSE;
Steve French75cf6bd2005-04-28 22:41:04 -0700459 if((rc == -EOPNOTSUPP) &&
460 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
461 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
462 goto ffirst_retry;
463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 kfree(full_path);
465 return rc;
466}
467
468/* return length of unicode string in bytes */
469static int cifs_unicode_bytelen(char *str)
470{
471 int len;
472 __le16 * ustr = (__le16 *)str;
473
474 for(len=0;len <= PATH_MAX;len++) {
475 if(ustr[len] == 0)
476 return len << 1;
477 }
478 cFYI(1,("Unicode string longer than PATH_MAX found"));
479 return len << 1;
480}
481
Steve French5bafd762006-06-07 00:18:43 +0000482static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 char * new_entry;
485 FILE_DIRECTORY_INFO * pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
486
Steve French5bafd762006-06-07 00:18:43 +0000487 if(level == SMB_FIND_FILE_INFO_STANDARD) {
488 FIND_FILE_STANDARD_INFO * pfData;
489 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
490
491 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
492 pfData->FileNameLength;
493 } else
494 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 cFYI(1,("new entry %p old entry %p",new_entry,old_entry));
496 /* validate that new_entry is not past end of SMB */
497 if(new_entry >= end_of_smb) {
Steve French09d1db52005-04-28 22:41:08 -0700498 cERROR(1,
499 ("search entry %p began after end of SMB %p old entry %p",
500 new_entry, end_of_smb, old_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return NULL;
Steve French5bafd762006-06-07 00:18:43 +0000502 } else if(((level == SMB_FIND_FILE_INFO_STANDARD) &&
503 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb)) ||
504 ((level != SMB_FIND_FILE_INFO_STANDARD) &&
505 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
Steve French09d1db52005-04-28 22:41:08 -0700506 cERROR(1,("search entry %p extends after end of SMB %p",
507 new_entry, end_of_smb));
508 return NULL;
509 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return new_entry;
511
512}
513
514#define UNICODE_DOT cpu_to_le16(0x2e)
515
516/* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
517static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
518{
519 int rc = 0;
520 char * filename = NULL;
521 int len = 0;
522
Steve French5bafd762006-06-07 00:18:43 +0000523 if(cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
525 filename = &pFindData->FileName[0];
526 if(cfile->srch_inf.unicode) {
527 len = cifs_unicode_bytelen(filename);
528 } else {
529 /* BB should we make this strnlen of PATH_MAX? */
530 len = strnlen(filename, 5);
531 }
Steve French5bafd762006-06-07 00:18:43 +0000532 } else if(cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 FILE_DIRECTORY_INFO * pFindData =
534 (FILE_DIRECTORY_INFO *)current_entry;
535 filename = &pFindData->FileName[0];
536 len = le32_to_cpu(pFindData->FileNameLength);
Steve French5bafd762006-06-07 00:18:43 +0000537 } else if(cfile->srch_inf.info_level ==
538 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 FILE_FULL_DIRECTORY_INFO * pFindData =
540 (FILE_FULL_DIRECTORY_INFO *)current_entry;
541 filename = &pFindData->FileName[0];
542 len = le32_to_cpu(pFindData->FileNameLength);
Steve French5bafd762006-06-07 00:18:43 +0000543 } else if(cfile->srch_inf.info_level ==
544 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 SEARCH_ID_FULL_DIR_INFO * pFindData =
546 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
547 filename = &pFindData->FileName[0];
548 len = le32_to_cpu(pFindData->FileNameLength);
Steve French5bafd762006-06-07 00:18:43 +0000549 } else if(cfile->srch_inf.info_level ==
550 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 FILE_BOTH_DIRECTORY_INFO * pFindData =
552 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
553 filename = &pFindData->FileName[0];
554 len = le32_to_cpu(pFindData->FileNameLength);
Steve French5bafd762006-06-07 00:18:43 +0000555 } else if(cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
556 FIND_FILE_STANDARD_INFO * pFindData =
557 (FIND_FILE_STANDARD_INFO *)current_entry;
558 filename = &pFindData->FileName[0];
Steve French5ddaa682006-08-15 13:35:48 +0000559 len = pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 } else {
561 cFYI(1,("Unknown findfirst level %d",cfile->srch_inf.info_level));
562 }
563
564 if(filename) {
565 if(cfile->srch_inf.unicode) {
566 __le16 *ufilename = (__le16 *)filename;
567 if(len == 2) {
568 /* check for . */
569 if(ufilename[0] == UNICODE_DOT)
570 rc = 1;
571 } else if(len == 4) {
572 /* check for .. */
573 if((ufilename[0] == UNICODE_DOT)
574 &&(ufilename[1] == UNICODE_DOT))
575 rc = 2;
576 }
577 } else /* ASCII */ {
578 if(len == 1) {
579 if(filename[0] == '.')
580 rc = 1;
581 } else if(len == 2) {
582 if((filename[0] == '.') && (filename[1] == '.'))
583 rc = 2;
584 }
585 }
586 }
587
588 return rc;
589}
590
Steve Frencheafe8702005-09-15 21:47:30 -0700591/* Check if directory that we are searching has changed so we can decide
592 whether we can use the cached search results from the previous search */
593static int is_dir_changed(struct file * file)
594{
595 struct inode * inode;
596 struct cifsInodeInfo *cifsInfo;
597
598 if(file->f_dentry == NULL)
599 return 0;
600
601 inode = file->f_dentry->d_inode;
602
603 if(inode == NULL)
604 return 0;
605
606 cifsInfo = CIFS_I(inode);
607
608 if(cifsInfo->time == 0)
609 return 1; /* directory was changed, perhaps due to unlink */
610 else
611 return 0;
612
613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615/* find the corresponding entry in the search */
616/* Note that the SMB server returns search entries for . and .. which
617 complicates logic here if we choose to parse for them and we do not
618 assume that they are located in the findfirst return buffer.*/
619/* We start counting in the buffer with entry 2 and increment for every
620 entry (do not increment for . or .. entry) */
621static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
622 struct file *file, char **ppCurrentEntry, int *num_to_ret)
623{
624 int rc = 0;
625 int pos_in_buf = 0;
626 loff_t first_entry_in_buffer;
627 loff_t index_to_find = file->f_pos;
628 struct cifsFileInfo * cifsFile = file->private_data;
629 /* check if index in the buffer */
630
Steve Frencheafe8702005-09-15 21:47:30 -0700631 if((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
632 (num_to_ret == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return -ENOENT;
634
635 *ppCurrentEntry = NULL;
636 first_entry_in_buffer =
637 cifsFile->srch_inf.index_of_last_entry -
638 cifsFile->srch_inf.entries_in_buffer;
Steve French60808232006-04-22 15:53:05 +0000639
640 /* if first entry in buf is zero then is first buffer
641 in search response data which means it is likely . and ..
642 will be in this buffer, although some servers do not return
643 . and .. for the root of a drive and for those we need
644 to start two entries earlier */
645
Steve French39798772006-05-31 22:40:51 +0000646#ifdef CONFIG_CIFS_DEBUG2
647 dump_cifs_file_struct(file, "In fce ");
648#endif
Steve Frencheafe8702005-09-15 21:47:30 -0700649 if(((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
650 is_dir_changed(file)) ||
651 (index_to_find < first_entry_in_buffer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /* close and restart search */
653 cFYI(1,("search backing up - close and restart search"));
654 cifsFile->invalidHandle = TRUE;
655 CIFSFindClose(xid, pTcon, cifsFile->netfid);
656 kfree(cifsFile->search_resume_name);
657 cifsFile->search_resume_name = NULL;
658 if(cifsFile->srch_inf.ntwrk_buf_start) {
659 cFYI(1,("freeing SMB ff cache buf on search rewind"));
Steve Frenchd47d7c12006-02-28 03:45:48 +0000660 if(cifsFile->srch_inf.smallBuf)
661 cifs_small_buf_release(cifsFile->srch_inf.
662 ntwrk_buf_start);
663 else
664 cifs_buf_release(cifsFile->srch_inf.
665 ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667 rc = initiate_cifs_search(xid,file);
668 if(rc) {
669 cFYI(1,("error %d reinitiating a search on rewind",rc));
670 return rc;
671 }
672 }
673
674 while((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
675 (rc == 0) && (cifsFile->srch_inf.endOfSearch == FALSE)){
676 cFYI(1,("calling findnext2"));
Steve Frenchdfb75332005-06-22 17:13:47 -0700677 rc = CIFSFindNext(xid,pTcon,cifsFile->netfid,
678 &cifsFile->srch_inf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if(rc)
680 return -ENOENT;
681 }
682 if(index_to_find < cifsFile->srch_inf.index_of_last_entry) {
683 /* we found the buffer that contains the entry */
684 /* scan and find it */
685 int i;
686 char * current_entry;
687 char * end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
688 smbCalcSize((struct smb_hdr *)
689 cifsFile->srch_inf.ntwrk_buf_start);
Steve French60808232006-04-22 15:53:05 +0000690
691 current_entry = cifsFile->srch_inf.srch_entries_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
693 - cifsFile->srch_inf.entries_in_buffer;
694 pos_in_buf = index_to_find - first_entry_in_buffer;
Steve French5bafd762006-06-07 00:18:43 +0000695 cFYI(1,("found entry - pos_in_buf %d",pos_in_buf));
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 for(i=0;(i<(pos_in_buf)) && (current_entry != NULL);i++) {
Steve Frenchdfb75332005-06-22 17:13:47 -0700698 /* go entry by entry figuring out which is first */
Steve French5bafd762006-06-07 00:18:43 +0000699 current_entry = nxt_dir_entry(current_entry,end_of_smb,
700 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702 if((current_entry == NULL) && (i < pos_in_buf)) {
703 /* BB fixme - check if we should flag this error */
704 cERROR(1,("reached end of buf searching for pos in buf"
705 " %d index to find %lld rc %d",
706 pos_in_buf,index_to_find,rc));
707 }
708 rc = 0;
709 *ppCurrentEntry = current_entry;
710 } else {
711 cFYI(1,("index not in buffer - could not findnext into it"));
712 return 0;
713 }
714
715 if(pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
Steve Frencheafe8702005-09-15 21:47:30 -0700716 cFYI(1,("can not return entries pos_in_buf beyond last entry"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 *num_to_ret = 0;
718 } else
719 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 return rc;
722}
723
724/* inode num, inode type and filename returned */
725static int cifs_get_name_from_search_buf(struct qstr *pqst,
726 char *current_entry, __u16 level, unsigned int unicode,
Steve French5bafd762006-06-07 00:18:43 +0000727 struct cifs_sb_info * cifs_sb, int max_len, ino_t *pinum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
729 int rc = 0;
730 unsigned int len = 0;
731 char * filename;
732 struct nls_table * nlt = cifs_sb->local_nls;
733
734 *pinum = 0;
735
736 if(level == SMB_FIND_FILE_UNIX) {
737 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
738
739 filename = &pFindData->FileName[0];
740 if(unicode) {
741 len = cifs_unicode_bytelen(filename);
742 } else {
743 /* BB should we make this strnlen of PATH_MAX? */
744 len = strnlen(filename, PATH_MAX);
745 }
746
747 /* BB fixme - hash low and high 32 bits if not 64 bit arch BB fixme */
748 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
749 *pinum = pFindData->UniqueId;
750 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
751 FILE_DIRECTORY_INFO * pFindData =
752 (FILE_DIRECTORY_INFO *)current_entry;
753 filename = &pFindData->FileName[0];
754 len = le32_to_cpu(pFindData->FileNameLength);
755 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
756 FILE_FULL_DIRECTORY_INFO * pFindData =
757 (FILE_FULL_DIRECTORY_INFO *)current_entry;
758 filename = &pFindData->FileName[0];
759 len = le32_to_cpu(pFindData->FileNameLength);
760 } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
761 SEARCH_ID_FULL_DIR_INFO * pFindData =
762 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
763 filename = &pFindData->FileName[0];
764 len = le32_to_cpu(pFindData->FileNameLength);
765 *pinum = pFindData->UniqueId;
766 } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
767 FILE_BOTH_DIRECTORY_INFO * pFindData =
768 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
769 filename = &pFindData->FileName[0];
770 len = le32_to_cpu(pFindData->FileNameLength);
Steve French5bafd762006-06-07 00:18:43 +0000771 } else if(level == SMB_FIND_FILE_INFO_STANDARD) {
772 FIND_FILE_STANDARD_INFO * pFindData =
773 (FIND_FILE_STANDARD_INFO *)current_entry;
774 filename = &pFindData->FileName[0];
775 /* one byte length, no name conversion */
776 len = (unsigned int)pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 } else {
778 cFYI(1,("Unknown findfirst level %d",level));
779 return -EINVAL;
780 }
Steve French5bafd762006-06-07 00:18:43 +0000781
782 if(len > max_len) {
783 cERROR(1,("bad search response length %d past smb end", len));
784 return -EINVAL;
785 }
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if(unicode) {
788 /* BB fixme - test with long names */
789 /* Note converted filename can be longer than in unicode */
Steve French6a0b4822005-04-28 22:41:05 -0700790 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
791 pqst->len = cifs_convertUCSpath((char *)pqst->name,
792 (__le16 *)filename, len/2, nlt);
793 else
Steve French6a0b4822005-04-28 22:41:05 -0700794 pqst->len = cifs_strfromUCS_le((char *)pqst->name,
Steve Frenche89dc922005-11-11 15:18:19 -0800795 (__le16 *)filename,len/2,nlt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 } else {
797 pqst->name = filename;
798 pqst->len = len;
799 }
800 pqst->hash = full_name_hash(pqst->name,pqst->len);
801/* cFYI(1,("filldir on %s",pqst->name)); */
802 return rc;
803}
804
805static int cifs_filldir(char *pfindEntry, struct file *file,
Steve French5bafd762006-06-07 00:18:43 +0000806 filldir_t filldir, void *direntry, char *scratch_buf, int max_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 int rc = 0;
809 struct qstr qstring;
810 struct cifsFileInfo * pCifsF;
811 unsigned obj_type;
812 ino_t inum;
813 struct cifs_sb_info * cifs_sb;
814 struct inode *tmp_inode;
815 struct dentry *tmp_dentry;
816
817 /* get filename and len into qstring */
818 /* get dentry */
819 /* decide whether to create and populate ionde */
820 if((direntry == NULL) || (file == NULL))
821 return -EINVAL;
822
823 pCifsF = file->private_data;
824
825 if((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
826 return -ENOENT;
827
828 if(file->f_dentry == NULL)
829 return -ENOENT;
830
Steve Frenchb66ac3e2006-04-23 01:54:50 +0000831 rc = cifs_entry_is_dot(pfindEntry,pCifsF);
Steve French60808232006-04-22 15:53:05 +0000832 /* skip . and .. since we added them first */
833 if(rc != 0)
834 return 0;
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
837
838 qstring.name = scratch_buf;
839 rc = cifs_get_name_from_search_buf(&qstring,pfindEntry,
840 pCifsF->srch_inf.info_level,
841 pCifsF->srch_inf.unicode,cifs_sb,
Steve French5bafd762006-06-07 00:18:43 +0000842 max_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 &inum /* returned */);
844
845 if(rc)
846 return rc;
847
848 rc = construct_dentry(&qstring,file,&tmp_inode, &tmp_dentry);
849 if((tmp_inode == NULL) || (tmp_dentry == NULL))
850 return -ENOMEM;
851
852 if(rc) {
853 /* inode created, we need to hash it with right inode number */
854 if(inum != 0) {
855 /* BB fixme - hash the 2 32 quantities bits together if necessary BB */
856 tmp_inode->i_ino = inum;
857 }
858 insert_inode_hash(tmp_inode);
859 }
860
Steve French966ca922005-04-28 22:41:08 -0700861 /* we pass in rc below, indicating whether it is a new inode,
862 so we can figure out whether to invalidate the inode cached
863 data if the file has changed */
Steve French5bafd762006-06-07 00:18:43 +0000864 if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
Steve French966ca922005-04-28 22:41:08 -0700865 unix_fill_in_inode(tmp_inode,
Steve French5bafd762006-06-07 00:18:43 +0000866 (FILE_UNIX_INFO *)pfindEntry,
867 &obj_type, rc);
868 else if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
869 fill_in_inode(tmp_inode, 0 /* old level 1 buffer type */,
870 pfindEntry, &obj_type, rc);
871 else
872 fill_in_inode(tmp_inode, 1 /* NT */, pfindEntry, &obj_type, rc);
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Steve Frenchdfb75332005-06-22 17:13:47 -0700875 rc = filldir(direntry,qstring.name,qstring.len,file->f_pos,
876 tmp_inode->i_ino,obj_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if(rc) {
878 cFYI(1,("filldir rc = %d",rc));
879 }
880
881 dput(tmp_dentry);
882 return rc;
883}
884
885static int cifs_save_resume_key(const char *current_entry,
886 struct cifsFileInfo *cifsFile)
887{
888 int rc = 0;
889 unsigned int len = 0;
890 __u16 level;
891 char * filename;
892
893 if((cifsFile == NULL) || (current_entry == NULL))
894 return -EINVAL;
895
896 level = cifsFile->srch_inf.info_level;
897
898 if(level == SMB_FIND_FILE_UNIX) {
899 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
900
901 filename = &pFindData->FileName[0];
902 if(cifsFile->srch_inf.unicode) {
903 len = cifs_unicode_bytelen(filename);
904 } else {
905 /* BB should we make this strnlen of PATH_MAX? */
906 len = strnlen(filename, PATH_MAX);
907 }
908 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
909 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
910 FILE_DIRECTORY_INFO * pFindData =
911 (FILE_DIRECTORY_INFO *)current_entry;
912 filename = &pFindData->FileName[0];
913 len = le32_to_cpu(pFindData->FileNameLength);
914 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
915 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
916 FILE_FULL_DIRECTORY_INFO * pFindData =
917 (FILE_FULL_DIRECTORY_INFO *)current_entry;
918 filename = &pFindData->FileName[0];
919 len = le32_to_cpu(pFindData->FileNameLength);
920 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
921 } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
922 SEARCH_ID_FULL_DIR_INFO * pFindData =
923 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
924 filename = &pFindData->FileName[0];
925 len = le32_to_cpu(pFindData->FileNameLength);
926 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
927 } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
928 FILE_BOTH_DIRECTORY_INFO * pFindData =
929 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
930 filename = &pFindData->FileName[0];
931 len = le32_to_cpu(pFindData->FileNameLength);
932 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
Steve French5bafd762006-06-07 00:18:43 +0000933 } else if(level == SMB_FIND_FILE_INFO_STANDARD) {
934 FIND_FILE_STANDARD_INFO * pFindData =
935 (FIND_FILE_STANDARD_INFO *)current_entry;
936 filename = &pFindData->FileName[0];
937 /* one byte length, no name conversion */
938 len = (unsigned int)pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 } else {
940 cFYI(1,("Unknown findfirst level %d",level));
941 return -EINVAL;
942 }
943 cifsFile->srch_inf.resume_name_len = len;
944 cifsFile->srch_inf.presume_name = filename;
945 return rc;
946}
947
948int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
949{
950 int rc = 0;
951 int xid,i;
952 struct cifs_sb_info *cifs_sb;
953 struct cifsTconInfo *pTcon;
954 struct cifsFileInfo *cifsFile = NULL;
955 char * current_entry;
956 int num_to_fill = 0;
957 char * tmp_buf = NULL;
958 char * end_of_smb;
Steve French5bafd762006-06-07 00:18:43 +0000959 int max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 xid = GetXid();
962
963 if(file->f_dentry == NULL) {
964 FreeXid(xid);
965 return -EIO;
966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
969 pTcon = cifs_sb->tcon;
970 if(pTcon == NULL)
971 return -EINVAL;
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 switch ((int) file->f_pos) {
974 case 0:
Steve French60808232006-04-22 15:53:05 +0000975 if (filldir(direntry, ".", 1, file->f_pos,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 file->f_dentry->d_inode->i_ino, DT_DIR) < 0) {
Steve French60808232006-04-22 15:53:05 +0000977 cERROR(1, ("Filldir for current dir failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 rc = -ENOMEM;
979 break;
980 }
Steve French60808232006-04-22 15:53:05 +0000981 file->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 case 1:
Steve French60808232006-04-22 15:53:05 +0000983 if (filldir(direntry, "..", 2, file->f_pos,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 file->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
Steve French26a21b92006-05-31 18:05:34 +0000985 cERROR(1, ("Filldir for parent dir failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 rc = -ENOMEM;
987 break;
988 }
Steve French60808232006-04-22 15:53:05 +0000989 file->f_pos++;
990 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 /* 1) If search is active,
992 is in current search buffer?
993 if it before then restart search
994 if after then keep searching till find it */
995
996 if(file->private_data == NULL) {
997 rc = initiate_cifs_search(xid,file);
998 cFYI(1,("initiate cifs search rc %d",rc));
999 if(rc) {
1000 FreeXid(xid);
1001 return rc;
1002 }
1003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if(file->private_data == NULL) {
1005 rc = -EINVAL;
1006 FreeXid(xid);
1007 return rc;
1008 }
1009 cifsFile = file->private_data;
1010 if (cifsFile->srch_inf.endOfSearch) {
1011 if(cifsFile->srch_inf.emptyDir) {
1012 cFYI(1, ("End of search, empty dir"));
1013 rc = 0;
1014 break;
1015 }
1016 } /* else {
1017 cifsFile->invalidHandle = TRUE;
1018 CIFSFindClose(xid, pTcon, cifsFile->netfid);
1019 }
1020 kfree(cifsFile->search_resume_name);
1021 cifsFile->search_resume_name = NULL; */
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 rc = find_cifs_entry(xid,pTcon, file,
1024 &current_entry,&num_to_fill);
1025 if(rc) {
1026 cFYI(1,("fce error %d",rc));
1027 goto rddir2_exit;
1028 } else if (current_entry != NULL) {
1029 cFYI(1,("entry %lld found",file->f_pos));
1030 } else {
1031 cFYI(1,("could not find entry"));
1032 goto rddir2_exit;
1033 }
1034 cFYI(1,("loop through %d times filling dir for net buf %p",
Steve French5bafd762006-06-07 00:18:43 +00001035 num_to_fill,cifsFile->srch_inf.ntwrk_buf_start));
1036 max_len = smbCalcSize((struct smb_hdr *)
1037 cifsFile->srch_inf.ntwrk_buf_start);
1038 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1039
Steve French6a0b4822005-04-28 22:41:05 -07001040 /* To be safe - for UCS to UTF-8 with strings loaded
1041 with the rare long characters alloc more to account for
1042 such multibyte target UTF-8 characters. cifs_unicode.c,
1043 which actually does the conversion, has the same limit */
1044 tmp_buf = kmalloc((2 * NAME_MAX) + 4, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 for(i=0;(i<num_to_fill) && (rc == 0);i++) {
1046 if(current_entry == NULL) {
1047 /* evaluate whether this case is an error */
1048 cERROR(1,("past end of SMB num to fill %d i %d",
1049 num_to_fill, i));
1050 break;
1051 }
Steve French60808232006-04-22 15:53:05 +00001052 /* if buggy server returns . and .. late do
1053 we want to check for that here? */
Steve French5bafd762006-06-07 00:18:43 +00001054 rc = cifs_filldir(current_entry, file,
1055 filldir, direntry, tmp_buf, max_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 file->f_pos++;
Steve French39798772006-05-31 22:40:51 +00001057 if(file->f_pos ==
1058 cifsFile->srch_inf.index_of_last_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 cFYI(1,("last entry in buf at pos %lld %s",
Steve French39798772006-05-31 22:40:51 +00001060 file->f_pos,tmp_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 cifs_save_resume_key(current_entry,cifsFile);
1062 break;
1063 } else
Steve French5bafd762006-06-07 00:18:43 +00001064 current_entry =
1065 nxt_dir_entry(current_entry, end_of_smb,
1066 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068 kfree(tmp_buf);
1069 break;
1070 } /* end switch */
1071
1072rddir2_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 FreeXid(xid);
1074 return rc;
1075}