blob: 9aeb58a7d369380d3f81a5f63569c66405af2463 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86 } else {
87 tmp_dentry = d_alloc(file->f_dentry, qstring);
88 if(tmp_dentry == NULL) {
89 cERROR(1,("Failed allocating dentry"));
90 *ptmp_inode = NULL;
91 return rc;
92 }
93
94 *ptmp_inode = new_inode(file->f_dentry->d_sb);
Steve Frenchb92327f2005-08-22 20:09:43 -070095 if (pTcon->nocase)
96 tmp_dentry->d_op = &cifs_ci_dentry_ops;
97 else
98 tmp_dentry->d_op = &cifs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if(*ptmp_inode == NULL)
100 return rc;
Steve Frenchb835beb2006-09-06 22:02:22 +0000101 rc = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103
104 tmp_dentry->d_time = jiffies;
105 *pnew_dentry = tmp_dentry;
106 return rc;
107}
108
Steve French5bafd762006-06-07 00:18:43 +0000109static void fill_in_inode(struct inode *tmp_inode, int new_buf_type,
110 char * buf, int *pobject_type, int isNewInode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Steve French966ca922005-04-28 22:41:08 -0700112 loff_t local_size;
113 struct timespec local_mtime;
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
116 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
Steve French5bafd762006-06-07 00:18:43 +0000117 __u32 attr;
118 __u64 allocation_size;
119 __u64 end_of_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Steve French966ca922005-04-28 22:41:08 -0700121 /* save mtime and size */
122 local_mtime = tmp_inode->i_mtime;
123 local_size = tmp_inode->i_size;
124
Steve French5bafd762006-06-07 00:18:43 +0000125 if(new_buf_type) {
126 FILE_DIRECTORY_INFO *pfindData = (FILE_DIRECTORY_INFO *)buf;
127
128 attr = le32_to_cpu(pfindData->ExtFileAttributes);
129 allocation_size = le64_to_cpu(pfindData->AllocationSize);
130 end_of_file = le64_to_cpu(pfindData->EndOfFile);
131 tmp_inode->i_atime =
132 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
133 tmp_inode->i_mtime =
134 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
135 tmp_inode->i_ctime =
136 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
137 } else { /* legacy, OS2 and DOS style */
138 FIND_FILE_STANDARD_INFO * pfindData =
139 (FIND_FILE_STANDARD_INFO *)buf;
140
141 attr = le16_to_cpu(pfindData->Attributes);
142 allocation_size = le32_to_cpu(pfindData->AllocationSize);
143 end_of_file = le32_to_cpu(pfindData->DataSize);
144 tmp_inode->i_atime = CURRENT_TIME;
145 /* tmp_inode->i_mtime = BB FIXME - add dos time handling
146 tmp_inode->i_ctime = 0; BB FIXME */
147
148 }
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 /* Linux can not store file creation time unfortunately so ignore it */
Steve French5bafd762006-06-07 00:18:43 +0000151
152 cifsInfo->cifsAttrs = attr;
153 cifsInfo->time = jiffies;
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
156 /* 2767 perms - indicate mandatory locking */
157 /* BB fill in uid and gid here? with help from winbind?
158 or retrieve from NTFS stream extended attribute */
159 if (atomic_read(&cifsInfo->inUse) == 0) {
160 tmp_inode->i_uid = cifs_sb->mnt_uid;
161 tmp_inode->i_gid = cifs_sb->mnt_gid;
162 /* set default mode. will override for dirs below */
163 tmp_inode->i_mode = cifs_sb->mnt_file_mode;
Steve French3020a1f2005-11-18 11:31:10 -0800164 } else {
165 /* mask off the type bits since it gets set
166 below and we do not want to get two type
167 bits set */
168 tmp_inode->i_mode &= ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (attr & ATTR_DIRECTORY) {
172 *pobject_type = DT_DIR;
173 /* override default perms since we do not lock dirs */
174 if(atomic_read(&cifsInfo->inUse) == 0) {
175 tmp_inode->i_mode = cifs_sb->mnt_dir_mode;
176 }
177 tmp_inode->i_mode |= S_IFDIR;
Steve Frencheda3c0292005-07-21 15:20:28 -0700178 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
Steve French3020a1f2005-11-18 11:31:10 -0800179 (attr & ATTR_SYSTEM)) {
180 if (end_of_file == 0) {
181 *pobject_type = DT_FIFO;
182 tmp_inode->i_mode |= S_IFIFO;
183 } else {
184 /* rather than get the type here, we mark the
185 inode as needing revalidate and get the real type
186 (blk vs chr vs. symlink) later ie in lookup */
187 *pobject_type = DT_REG;
188 tmp_inode->i_mode |= S_IFREG;
189 cifsInfo->time = 0;
190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/* we no longer mark these because we could not follow them */
192/* } else if (attr & ATTR_REPARSE) {
193 *pobject_type = DT_LNK;
194 tmp_inode->i_mode |= S_IFLNK; */
195 } else {
196 *pobject_type = DT_REG;
197 tmp_inode->i_mode |= S_IFREG;
198 if (attr & ATTR_READONLY)
199 tmp_inode->i_mode &= ~(S_IWUGO);
200 } /* could add code here - to validate if device or weird share type? */
201
202 /* can not fill in nlink here as in qpathinfo version and Unx search */
203 if (atomic_read(&cifsInfo->inUse) == 0) {
204 atomic_set(&cifsInfo->inUse, 1);
205 }
206
207 if (is_size_safe_to_change(cifsInfo)) {
208 /* can not safely change the file size here if the
209 client is writing to it due to potential races */
210 i_size_write(tmp_inode, end_of_file);
211
212 /* 512 bytes (2**9) is the fake blocksize that must be used */
213 /* for this calculation, even though the reported blocksize is larger */
214 tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
215 }
216
217 if (allocation_size < end_of_file)
218 cFYI(1, ("May be sparse file, allocation less than file size"));
Andrew Morton5515eff2006-03-26 01:37:53 -0800219 cFYI(1, ("File Size %ld and blocks %llu and blocksize %ld",
220 (unsigned long)tmp_inode->i_size,
221 (unsigned long long)tmp_inode->i_blocks,
222 tmp_inode->i_blksize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (S_ISREG(tmp_inode->i_mode)) {
Steve French966ca922005-04-28 22:41:08 -0700224 cFYI(1, ("File inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 tmp_inode->i_op = &cifs_file_inode_ops;
Steve French8b94bcb2005-11-11 11:41:00 -0800226 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
227 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
228 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
229 else
230 tmp_inode->i_fop = &cifs_file_direct_ops;
231
232 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
233 tmp_inode->i_fop = &cifs_file_nobrl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 else
235 tmp_inode->i_fop = &cifs_file_ops;
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800236
Steve Frenchbfa0d752005-08-31 21:50:37 -0700237 if((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
238 (cifs_sb->tcon->ses->server->maxBuf <
Dave Kleikamp273d81d2006-06-01 19:41:23 +0000239 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
240 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
241 else
242 tmp_inode->i_data.a_ops = &cifs_addr_ops;
243
Steve French966ca922005-04-28 22:41:08 -0700244 if(isNewInode)
Steve Frenchdfb75332005-06-22 17:13:47 -0700245 return; /* No sense invalidating pages for new inode
246 since have not started caching readahead file
247 data yet */
Steve French966ca922005-04-28 22:41:08 -0700248
249 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
250 (local_size == tmp_inode->i_size)) {
251 cFYI(1, ("inode exists but unchanged"));
252 } else {
253 /* file may have changed on server */
254 cFYI(1, ("invalidate inode, readdir detected change"));
255 invalidate_remote_inode(tmp_inode);
256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 } else if (S_ISDIR(tmp_inode->i_mode)) {
Steve French966ca922005-04-28 22:41:08 -0700258 cFYI(1, ("Directory inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 tmp_inode->i_op = &cifs_dir_inode_ops;
260 tmp_inode->i_fop = &cifs_dir_ops;
261 } else if (S_ISLNK(tmp_inode->i_mode)) {
Steve French966ca922005-04-28 22:41:08 -0700262 cFYI(1, ("Symbolic Link inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 tmp_inode->i_op = &cifs_symlink_inode_ops;
264 } else {
Steve French966ca922005-04-28 22:41:08 -0700265 cFYI(1, ("Init special inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 init_special_inode(tmp_inode, tmp_inode->i_mode,
267 tmp_inode->i_rdev);
268 }
269}
270
271static void unix_fill_in_inode(struct inode *tmp_inode,
Steve French966ca922005-04-28 22:41:08 -0700272 FILE_UNIX_INFO *pfindData, int *pobject_type, int isNewInode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Steve French966ca922005-04-28 22:41:08 -0700274 loff_t local_size;
275 struct timespec local_mtime;
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
278 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
279
280 __u32 type = le32_to_cpu(pfindData->Type);
281 __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes);
282 __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
283 cifsInfo->time = jiffies;
284 atomic_inc(&cifsInfo->inUse);
285
Steve French966ca922005-04-28 22:41:08 -0700286 /* save mtime and size */
287 local_mtime = tmp_inode->i_mtime;
288 local_size = tmp_inode->i_size;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 tmp_inode->i_atime =
291 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
292 tmp_inode->i_mtime =
293 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime));
294 tmp_inode->i_ctime =
295 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange));
296
297 tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions);
Steve French3020a1f2005-11-18 11:31:10 -0800298 /* since we set the inode type below we need to mask off type
299 to avoid strange results if bits above were corrupt */
300 tmp_inode->i_mode &= ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (type == UNIX_FILE) {
302 *pobject_type = DT_REG;
303 tmp_inode->i_mode |= S_IFREG;
304 } else if (type == UNIX_SYMLINK) {
305 *pobject_type = DT_LNK;
306 tmp_inode->i_mode |= S_IFLNK;
307 } else if (type == UNIX_DIR) {
308 *pobject_type = DT_DIR;
309 tmp_inode->i_mode |= S_IFDIR;
310 } else if (type == UNIX_CHARDEV) {
311 *pobject_type = DT_CHR;
312 tmp_inode->i_mode |= S_IFCHR;
313 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
314 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
315 } else if (type == UNIX_BLOCKDEV) {
316 *pobject_type = DT_BLK;
317 tmp_inode->i_mode |= S_IFBLK;
318 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
319 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
320 } else if (type == UNIX_FIFO) {
321 *pobject_type = DT_FIFO;
322 tmp_inode->i_mode |= S_IFIFO;
323 } else if (type == UNIX_SOCKET) {
324 *pobject_type = DT_SOCK;
325 tmp_inode->i_mode |= S_IFSOCK;
Steve French3020a1f2005-11-18 11:31:10 -0800326 } else {
327 /* safest to just call it a file */
328 *pobject_type = DT_REG;
329 tmp_inode->i_mode |= S_IFREG;
330 cFYI(1,("unknown inode type %d",type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332
333 tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
334 tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
335 tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
336
337 if (is_size_safe_to_change(cifsInfo)) {
338 /* can not safely change the file size here if the
339 client is writing to it due to potential races */
340 i_size_write(tmp_inode,end_of_file);
341
342 /* 512 bytes (2**9) is the fake blocksize that must be used */
343 /* for this calculation, not the real blocksize */
344 tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
345 }
346
347 if (S_ISREG(tmp_inode->i_mode)) {
348 cFYI(1, ("File inode"));
349 tmp_inode->i_op = &cifs_file_inode_ops;
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800350
351 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
352 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
353 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
354 else
355 tmp_inode->i_fop = &cifs_file_direct_ops;
356
357 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
358 tmp_inode->i_fop = &cifs_file_nobrl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 else
360 tmp_inode->i_fop = &cifs_file_ops;
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800361
Steve Frenchbfa0d752005-08-31 21:50:37 -0700362 if((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
363 (cifs_sb->tcon->ses->server->maxBuf <
Dave Kleikamp273d81d2006-06-01 19:41:23 +0000364 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
365 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
366 else
367 tmp_inode->i_data.a_ops = &cifs_addr_ops;
Steve French966ca922005-04-28 22:41:08 -0700368
369 if(isNewInode)
370 return; /* No sense invalidating pages for new inode since we
371 have not started caching readahead file data yet */
372
373 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
374 (local_size == tmp_inode->i_size)) {
375 cFYI(1, ("inode exists but unchanged"));
376 } else {
377 /* file may have changed on server */
378 cFYI(1, ("invalidate inode, readdir detected change"));
379 invalidate_remote_inode(tmp_inode);
380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 } else if (S_ISDIR(tmp_inode->i_mode)) {
382 cFYI(1, ("Directory inode"));
383 tmp_inode->i_op = &cifs_dir_inode_ops;
384 tmp_inode->i_fop = &cifs_dir_ops;
385 } else if (S_ISLNK(tmp_inode->i_mode)) {
386 cFYI(1, ("Symbolic Link inode"));
387 tmp_inode->i_op = &cifs_symlink_inode_ops;
388/* tmp_inode->i_fop = *//* do not need to set to anything */
389 } else {
390 cFYI(1, ("Special inode"));
391 init_special_inode(tmp_inode, tmp_inode->i_mode,
392 tmp_inode->i_rdev);
393 }
394}
395
396static int initiate_cifs_search(const int xid, struct file *file)
397{
398 int rc = 0;
399 char * full_path;
400 struct cifsFileInfo * cifsFile;
401 struct cifs_sb_info *cifs_sb;
402 struct cifsTconInfo *pTcon;
403
404 if(file->private_data == NULL) {
405 file->private_data =
406 kmalloc(sizeof(struct cifsFileInfo),GFP_KERNEL);
407 }
408
409 if(file->private_data == NULL) {
410 return -ENOMEM;
411 } else {
412 memset(file->private_data,0,sizeof(struct cifsFileInfo));
413 }
414 cifsFile = file->private_data;
415 cifsFile->invalidHandle = TRUE;
416 cifsFile->srch_inf.endOfSearch = FALSE;
417
418 if(file->f_dentry == NULL)
419 return -ENOENT;
420
421 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
422 if(cifs_sb == NULL)
423 return -EINVAL;
424
425 pTcon = cifs_sb->tcon;
426 if(pTcon == NULL)
427 return -EINVAL;
428
Steve French7f573562005-08-30 11:32:14 -0700429 full_path = build_path_from_dentry(file->f_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 if(full_path == NULL) {
432 return -ENOMEM;
433 }
434
Steve French966ca922005-04-28 22:41:08 -0700435 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Steve French75cf6bd2005-04-28 22:41:04 -0700437ffirst_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 /* test for Unix extensions */
439 if (pTcon->ses->capabilities & CAP_UNIX) {
Steve French5bafd762006-06-07 00:18:43 +0000440 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
441 } else if ((pTcon->ses->capabilities &
442 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
443 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
445 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
446 } else /* not srvinos - BB fixme add check for backlevel? */ {
447 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
448 }
449
Steve French737b7582005-04-28 22:41:06 -0700450 rc = CIFSFindFirst(xid, pTcon,full_path,cifs_sb->local_nls,
451 &cifsFile->netfid, &cifsFile->srch_inf,
Steve Frencheafe8702005-09-15 21:47:30 -0700452 cifs_sb->mnt_cifs_flags &
453 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if(rc == 0)
455 cifsFile->invalidHandle = FALSE;
Steve French75cf6bd2005-04-28 22:41:04 -0700456 if((rc == -EOPNOTSUPP) &&
457 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
458 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
459 goto ffirst_retry;
460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 kfree(full_path);
462 return rc;
463}
464
465/* return length of unicode string in bytes */
466static int cifs_unicode_bytelen(char *str)
467{
468 int len;
469 __le16 * ustr = (__le16 *)str;
470
471 for(len=0;len <= PATH_MAX;len++) {
472 if(ustr[len] == 0)
473 return len << 1;
474 }
475 cFYI(1,("Unicode string longer than PATH_MAX found"));
476 return len << 1;
477}
478
Steve French5bafd762006-06-07 00:18:43 +0000479static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 char * new_entry;
482 FILE_DIRECTORY_INFO * pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
483
Steve French5bafd762006-06-07 00:18:43 +0000484 if(level == SMB_FIND_FILE_INFO_STANDARD) {
485 FIND_FILE_STANDARD_INFO * pfData;
486 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
487
488 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
489 pfData->FileNameLength;
490 } else
491 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 cFYI(1,("new entry %p old entry %p",new_entry,old_entry));
493 /* validate that new_entry is not past end of SMB */
494 if(new_entry >= end_of_smb) {
Steve French09d1db52005-04-28 22:41:08 -0700495 cERROR(1,
496 ("search entry %p began after end of SMB %p old entry %p",
497 new_entry, end_of_smb, old_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return NULL;
Steve French5bafd762006-06-07 00:18:43 +0000499 } else if(((level == SMB_FIND_FILE_INFO_STANDARD) &&
500 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb)) ||
501 ((level != SMB_FIND_FILE_INFO_STANDARD) &&
502 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
Steve French09d1db52005-04-28 22:41:08 -0700503 cERROR(1,("search entry %p extends after end of SMB %p",
504 new_entry, end_of_smb));
505 return NULL;
506 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return new_entry;
508
509}
510
511#define UNICODE_DOT cpu_to_le16(0x2e)
512
513/* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
514static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
515{
516 int rc = 0;
517 char * filename = NULL;
518 int len = 0;
519
Steve French5bafd762006-06-07 00:18:43 +0000520 if(cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
522 filename = &pFindData->FileName[0];
523 if(cfile->srch_inf.unicode) {
524 len = cifs_unicode_bytelen(filename);
525 } else {
526 /* BB should we make this strnlen of PATH_MAX? */
527 len = strnlen(filename, 5);
528 }
Steve French5bafd762006-06-07 00:18:43 +0000529 } else if(cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 FILE_DIRECTORY_INFO * pFindData =
531 (FILE_DIRECTORY_INFO *)current_entry;
532 filename = &pFindData->FileName[0];
533 len = le32_to_cpu(pFindData->FileNameLength);
Steve French5bafd762006-06-07 00:18:43 +0000534 } else if(cfile->srch_inf.info_level ==
535 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 FILE_FULL_DIRECTORY_INFO * pFindData =
537 (FILE_FULL_DIRECTORY_INFO *)current_entry;
538 filename = &pFindData->FileName[0];
539 len = le32_to_cpu(pFindData->FileNameLength);
Steve French5bafd762006-06-07 00:18:43 +0000540 } else if(cfile->srch_inf.info_level ==
541 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 SEARCH_ID_FULL_DIR_INFO * pFindData =
543 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
544 filename = &pFindData->FileName[0];
545 len = le32_to_cpu(pFindData->FileNameLength);
Steve French5bafd762006-06-07 00:18:43 +0000546 } else if(cfile->srch_inf.info_level ==
547 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 FILE_BOTH_DIRECTORY_INFO * pFindData =
549 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
550 filename = &pFindData->FileName[0];
551 len = le32_to_cpu(pFindData->FileNameLength);
Steve French5bafd762006-06-07 00:18:43 +0000552 } else if(cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
553 FIND_FILE_STANDARD_INFO * pFindData =
554 (FIND_FILE_STANDARD_INFO *)current_entry;
555 filename = &pFindData->FileName[0];
Steve French5ddaa682006-08-15 13:35:48 +0000556 len = pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 } else {
558 cFYI(1,("Unknown findfirst level %d",cfile->srch_inf.info_level));
559 }
560
561 if(filename) {
562 if(cfile->srch_inf.unicode) {
563 __le16 *ufilename = (__le16 *)filename;
564 if(len == 2) {
565 /* check for . */
566 if(ufilename[0] == UNICODE_DOT)
567 rc = 1;
568 } else if(len == 4) {
569 /* check for .. */
570 if((ufilename[0] == UNICODE_DOT)
571 &&(ufilename[1] == UNICODE_DOT))
572 rc = 2;
573 }
574 } else /* ASCII */ {
575 if(len == 1) {
576 if(filename[0] == '.')
577 rc = 1;
578 } else if(len == 2) {
579 if((filename[0] == '.') && (filename[1] == '.'))
580 rc = 2;
581 }
582 }
583 }
584
585 return rc;
586}
587
Steve Frencheafe8702005-09-15 21:47:30 -0700588/* Check if directory that we are searching has changed so we can decide
589 whether we can use the cached search results from the previous search */
590static int is_dir_changed(struct file * file)
591{
592 struct inode * inode;
593 struct cifsInodeInfo *cifsInfo;
594
595 if(file->f_dentry == NULL)
596 return 0;
597
598 inode = file->f_dentry->d_inode;
599
600 if(inode == NULL)
601 return 0;
602
603 cifsInfo = CIFS_I(inode);
604
605 if(cifsInfo->time == 0)
606 return 1; /* directory was changed, perhaps due to unlink */
607 else
608 return 0;
609
610}
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/* find the corresponding entry in the search */
613/* Note that the SMB server returns search entries for . and .. which
614 complicates logic here if we choose to parse for them and we do not
615 assume that they are located in the findfirst return buffer.*/
616/* We start counting in the buffer with entry 2 and increment for every
617 entry (do not increment for . or .. entry) */
618static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
619 struct file *file, char **ppCurrentEntry, int *num_to_ret)
620{
621 int rc = 0;
622 int pos_in_buf = 0;
623 loff_t first_entry_in_buffer;
624 loff_t index_to_find = file->f_pos;
625 struct cifsFileInfo * cifsFile = file->private_data;
626 /* check if index in the buffer */
627
Steve Frencheafe8702005-09-15 21:47:30 -0700628 if((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
629 (num_to_ret == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return -ENOENT;
631
632 *ppCurrentEntry = NULL;
633 first_entry_in_buffer =
634 cifsFile->srch_inf.index_of_last_entry -
635 cifsFile->srch_inf.entries_in_buffer;
Steve French60808232006-04-22 15:53:05 +0000636
637 /* if first entry in buf is zero then is first buffer
638 in search response data which means it is likely . and ..
639 will be in this buffer, although some servers do not return
640 . and .. for the root of a drive and for those we need
641 to start two entries earlier */
642
Steve French39798772006-05-31 22:40:51 +0000643#ifdef CONFIG_CIFS_DEBUG2
644 dump_cifs_file_struct(file, "In fce ");
645#endif
Steve Frencheafe8702005-09-15 21:47:30 -0700646 if(((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
647 is_dir_changed(file)) ||
648 (index_to_find < first_entry_in_buffer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /* close and restart search */
650 cFYI(1,("search backing up - close and restart search"));
651 cifsFile->invalidHandle = TRUE;
652 CIFSFindClose(xid, pTcon, cifsFile->netfid);
653 kfree(cifsFile->search_resume_name);
654 cifsFile->search_resume_name = NULL;
655 if(cifsFile->srch_inf.ntwrk_buf_start) {
656 cFYI(1,("freeing SMB ff cache buf on search rewind"));
Steve Frenchd47d7c12006-02-28 03:45:48 +0000657 if(cifsFile->srch_inf.smallBuf)
658 cifs_small_buf_release(cifsFile->srch_inf.
659 ntwrk_buf_start);
660 else
661 cifs_buf_release(cifsFile->srch_inf.
662 ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664 rc = initiate_cifs_search(xid,file);
665 if(rc) {
666 cFYI(1,("error %d reinitiating a search on rewind",rc));
667 return rc;
668 }
669 }
670
671 while((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
672 (rc == 0) && (cifsFile->srch_inf.endOfSearch == FALSE)){
673 cFYI(1,("calling findnext2"));
Steve Frenchdfb75332005-06-22 17:13:47 -0700674 rc = CIFSFindNext(xid,pTcon,cifsFile->netfid,
675 &cifsFile->srch_inf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if(rc)
677 return -ENOENT;
678 }
679 if(index_to_find < cifsFile->srch_inf.index_of_last_entry) {
680 /* we found the buffer that contains the entry */
681 /* scan and find it */
682 int i;
683 char * current_entry;
684 char * end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
685 smbCalcSize((struct smb_hdr *)
686 cifsFile->srch_inf.ntwrk_buf_start);
Steve French60808232006-04-22 15:53:05 +0000687
688 current_entry = cifsFile->srch_inf.srch_entries_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
690 - cifsFile->srch_inf.entries_in_buffer;
691 pos_in_buf = index_to_find - first_entry_in_buffer;
Steve French5bafd762006-06-07 00:18:43 +0000692 cFYI(1,("found entry - pos_in_buf %d",pos_in_buf));
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 for(i=0;(i<(pos_in_buf)) && (current_entry != NULL);i++) {
Steve Frenchdfb75332005-06-22 17:13:47 -0700695 /* go entry by entry figuring out which is first */
Steve French5bafd762006-06-07 00:18:43 +0000696 current_entry = nxt_dir_entry(current_entry,end_of_smb,
697 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699 if((current_entry == NULL) && (i < pos_in_buf)) {
700 /* BB fixme - check if we should flag this error */
701 cERROR(1,("reached end of buf searching for pos in buf"
702 " %d index to find %lld rc %d",
703 pos_in_buf,index_to_find,rc));
704 }
705 rc = 0;
706 *ppCurrentEntry = current_entry;
707 } else {
708 cFYI(1,("index not in buffer - could not findnext into it"));
709 return 0;
710 }
711
712 if(pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
Steve Frencheafe8702005-09-15 21:47:30 -0700713 cFYI(1,("can not return entries pos_in_buf beyond last entry"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 *num_to_ret = 0;
715 } else
716 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 return rc;
719}
720
721/* inode num, inode type and filename returned */
722static int cifs_get_name_from_search_buf(struct qstr *pqst,
723 char *current_entry, __u16 level, unsigned int unicode,
Steve French5bafd762006-06-07 00:18:43 +0000724 struct cifs_sb_info * cifs_sb, int max_len, ino_t *pinum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 int rc = 0;
727 unsigned int len = 0;
728 char * filename;
729 struct nls_table * nlt = cifs_sb->local_nls;
730
731 *pinum = 0;
732
733 if(level == SMB_FIND_FILE_UNIX) {
734 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
735
736 filename = &pFindData->FileName[0];
737 if(unicode) {
738 len = cifs_unicode_bytelen(filename);
739 } else {
740 /* BB should we make this strnlen of PATH_MAX? */
741 len = strnlen(filename, PATH_MAX);
742 }
743
744 /* BB fixme - hash low and high 32 bits if not 64 bit arch BB fixme */
745 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
746 *pinum = pFindData->UniqueId;
747 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
748 FILE_DIRECTORY_INFO * pFindData =
749 (FILE_DIRECTORY_INFO *)current_entry;
750 filename = &pFindData->FileName[0];
751 len = le32_to_cpu(pFindData->FileNameLength);
752 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
753 FILE_FULL_DIRECTORY_INFO * pFindData =
754 (FILE_FULL_DIRECTORY_INFO *)current_entry;
755 filename = &pFindData->FileName[0];
756 len = le32_to_cpu(pFindData->FileNameLength);
757 } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
758 SEARCH_ID_FULL_DIR_INFO * pFindData =
759 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
760 filename = &pFindData->FileName[0];
761 len = le32_to_cpu(pFindData->FileNameLength);
762 *pinum = pFindData->UniqueId;
763 } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
764 FILE_BOTH_DIRECTORY_INFO * pFindData =
765 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
766 filename = &pFindData->FileName[0];
767 len = le32_to_cpu(pFindData->FileNameLength);
Steve French5bafd762006-06-07 00:18:43 +0000768 } else if(level == SMB_FIND_FILE_INFO_STANDARD) {
769 FIND_FILE_STANDARD_INFO * pFindData =
770 (FIND_FILE_STANDARD_INFO *)current_entry;
771 filename = &pFindData->FileName[0];
772 /* one byte length, no name conversion */
773 len = (unsigned int)pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 } else {
775 cFYI(1,("Unknown findfirst level %d",level));
776 return -EINVAL;
777 }
Steve French5bafd762006-06-07 00:18:43 +0000778
779 if(len > max_len) {
780 cERROR(1,("bad search response length %d past smb end", len));
781 return -EINVAL;
782 }
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if(unicode) {
785 /* BB fixme - test with long names */
786 /* Note converted filename can be longer than in unicode */
Steve French6a0b4822005-04-28 22:41:05 -0700787 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
788 pqst->len = cifs_convertUCSpath((char *)pqst->name,
789 (__le16 *)filename, len/2, nlt);
790 else
Steve French6a0b4822005-04-28 22:41:05 -0700791 pqst->len = cifs_strfromUCS_le((char *)pqst->name,
Steve Frenche89dc922005-11-11 15:18:19 -0800792 (__le16 *)filename,len/2,nlt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 } else {
794 pqst->name = filename;
795 pqst->len = len;
796 }
797 pqst->hash = full_name_hash(pqst->name,pqst->len);
798/* cFYI(1,("filldir on %s",pqst->name)); */
799 return rc;
800}
801
802static int cifs_filldir(char *pfindEntry, struct file *file,
Steve French5bafd762006-06-07 00:18:43 +0000803 filldir_t filldir, void *direntry, char *scratch_buf, int max_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 int rc = 0;
806 struct qstr qstring;
807 struct cifsFileInfo * pCifsF;
808 unsigned obj_type;
809 ino_t inum;
810 struct cifs_sb_info * cifs_sb;
811 struct inode *tmp_inode;
812 struct dentry *tmp_dentry;
813
814 /* get filename and len into qstring */
815 /* get dentry */
816 /* decide whether to create and populate ionde */
817 if((direntry == NULL) || (file == NULL))
818 return -EINVAL;
819
820 pCifsF = file->private_data;
821
822 if((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
823 return -ENOENT;
824
825 if(file->f_dentry == NULL)
826 return -ENOENT;
827
Steve Frenchb66ac3e2006-04-23 01:54:50 +0000828 rc = cifs_entry_is_dot(pfindEntry,pCifsF);
Steve French60808232006-04-22 15:53:05 +0000829 /* skip . and .. since we added them first */
830 if(rc != 0)
831 return 0;
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
834
835 qstring.name = scratch_buf;
836 rc = cifs_get_name_from_search_buf(&qstring,pfindEntry,
837 pCifsF->srch_inf.info_level,
838 pCifsF->srch_inf.unicode,cifs_sb,
Steve French5bafd762006-06-07 00:18:43 +0000839 max_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 &inum /* returned */);
841
842 if(rc)
843 return rc;
844
845 rc = construct_dentry(&qstring,file,&tmp_inode, &tmp_dentry);
846 if((tmp_inode == NULL) || (tmp_dentry == NULL))
847 return -ENOMEM;
848
849 if(rc) {
850 /* inode created, we need to hash it with right inode number */
851 if(inum != 0) {
852 /* BB fixme - hash the 2 32 quantities bits together if necessary BB */
853 tmp_inode->i_ino = inum;
854 }
855 insert_inode_hash(tmp_inode);
856 }
857
Steve French966ca922005-04-28 22:41:08 -0700858 /* we pass in rc below, indicating whether it is a new inode,
859 so we can figure out whether to invalidate the inode cached
860 data if the file has changed */
Steve French5bafd762006-06-07 00:18:43 +0000861 if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
Steve French966ca922005-04-28 22:41:08 -0700862 unix_fill_in_inode(tmp_inode,
Steve French5bafd762006-06-07 00:18:43 +0000863 (FILE_UNIX_INFO *)pfindEntry,
864 &obj_type, rc);
865 else if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
866 fill_in_inode(tmp_inode, 0 /* old level 1 buffer type */,
867 pfindEntry, &obj_type, rc);
868 else
869 fill_in_inode(tmp_inode, 1 /* NT */, pfindEntry, &obj_type, rc);
Steve Frenchb835beb2006-09-06 22:02:22 +0000870
871 if(rc) /* new inode - needs to be tied to dentry */ {
872 d_instantiate(tmp_dentry, tmp_inode);
873 if(rc == 2)
874 d_rehash(tmp_dentry);
875 }
Steve French5bafd762006-06-07 00:18:43 +0000876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Steve Frenchdfb75332005-06-22 17:13:47 -0700878 rc = filldir(direntry,qstring.name,qstring.len,file->f_pos,
879 tmp_inode->i_ino,obj_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if(rc) {
881 cFYI(1,("filldir rc = %d",rc));
882 }
883
884 dput(tmp_dentry);
885 return rc;
886}
887
888static int cifs_save_resume_key(const char *current_entry,
889 struct cifsFileInfo *cifsFile)
890{
891 int rc = 0;
892 unsigned int len = 0;
893 __u16 level;
894 char * filename;
895
896 if((cifsFile == NULL) || (current_entry == NULL))
897 return -EINVAL;
898
899 level = cifsFile->srch_inf.info_level;
900
901 if(level == SMB_FIND_FILE_UNIX) {
902 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
903
904 filename = &pFindData->FileName[0];
905 if(cifsFile->srch_inf.unicode) {
906 len = cifs_unicode_bytelen(filename);
907 } else {
908 /* BB should we make this strnlen of PATH_MAX? */
909 len = strnlen(filename, PATH_MAX);
910 }
911 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
912 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
913 FILE_DIRECTORY_INFO * pFindData =
914 (FILE_DIRECTORY_INFO *)current_entry;
915 filename = &pFindData->FileName[0];
916 len = le32_to_cpu(pFindData->FileNameLength);
917 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
918 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
919 FILE_FULL_DIRECTORY_INFO * pFindData =
920 (FILE_FULL_DIRECTORY_INFO *)current_entry;
921 filename = &pFindData->FileName[0];
922 len = le32_to_cpu(pFindData->FileNameLength);
923 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
924 } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
925 SEARCH_ID_FULL_DIR_INFO * pFindData =
926 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
927 filename = &pFindData->FileName[0];
928 len = le32_to_cpu(pFindData->FileNameLength);
929 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
930 } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
931 FILE_BOTH_DIRECTORY_INFO * pFindData =
932 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
933 filename = &pFindData->FileName[0];
934 len = le32_to_cpu(pFindData->FileNameLength);
935 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
Steve French5bafd762006-06-07 00:18:43 +0000936 } else if(level == SMB_FIND_FILE_INFO_STANDARD) {
937 FIND_FILE_STANDARD_INFO * pFindData =
938 (FIND_FILE_STANDARD_INFO *)current_entry;
939 filename = &pFindData->FileName[0];
940 /* one byte length, no name conversion */
941 len = (unsigned int)pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 } else {
943 cFYI(1,("Unknown findfirst level %d",level));
944 return -EINVAL;
945 }
946 cifsFile->srch_inf.resume_name_len = len;
947 cifsFile->srch_inf.presume_name = filename;
948 return rc;
949}
950
951int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
952{
953 int rc = 0;
954 int xid,i;
955 struct cifs_sb_info *cifs_sb;
956 struct cifsTconInfo *pTcon;
957 struct cifsFileInfo *cifsFile = NULL;
958 char * current_entry;
959 int num_to_fill = 0;
960 char * tmp_buf = NULL;
961 char * end_of_smb;
Steve French5bafd762006-06-07 00:18:43 +0000962 int max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 xid = GetXid();
965
966 if(file->f_dentry == NULL) {
967 FreeXid(xid);
968 return -EIO;
969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
972 pTcon = cifs_sb->tcon;
973 if(pTcon == NULL)
974 return -EINVAL;
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 switch ((int) file->f_pos) {
977 case 0:
Steve French60808232006-04-22 15:53:05 +0000978 if (filldir(direntry, ".", 1, file->f_pos,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 file->f_dentry->d_inode->i_ino, DT_DIR) < 0) {
Steve French60808232006-04-22 15:53:05 +0000980 cERROR(1, ("Filldir for current dir failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 rc = -ENOMEM;
982 break;
983 }
Steve French60808232006-04-22 15:53:05 +0000984 file->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 case 1:
Steve French60808232006-04-22 15:53:05 +0000986 if (filldir(direntry, "..", 2, file->f_pos,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 file->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
Steve French26a21b92006-05-31 18:05:34 +0000988 cERROR(1, ("Filldir for parent dir failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 rc = -ENOMEM;
990 break;
991 }
Steve French60808232006-04-22 15:53:05 +0000992 file->f_pos++;
993 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /* 1) If search is active,
995 is in current search buffer?
996 if it before then restart search
997 if after then keep searching till find it */
998
999 if(file->private_data == NULL) {
1000 rc = initiate_cifs_search(xid,file);
1001 cFYI(1,("initiate cifs search rc %d",rc));
1002 if(rc) {
1003 FreeXid(xid);
1004 return rc;
1005 }
1006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if(file->private_data == NULL) {
1008 rc = -EINVAL;
1009 FreeXid(xid);
1010 return rc;
1011 }
1012 cifsFile = file->private_data;
1013 if (cifsFile->srch_inf.endOfSearch) {
1014 if(cifsFile->srch_inf.emptyDir) {
1015 cFYI(1, ("End of search, empty dir"));
1016 rc = 0;
1017 break;
1018 }
1019 } /* else {
1020 cifsFile->invalidHandle = TRUE;
1021 CIFSFindClose(xid, pTcon, cifsFile->netfid);
1022 }
1023 kfree(cifsFile->search_resume_name);
1024 cifsFile->search_resume_name = NULL; */
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 rc = find_cifs_entry(xid,pTcon, file,
1027 &current_entry,&num_to_fill);
1028 if(rc) {
1029 cFYI(1,("fce error %d",rc));
1030 goto rddir2_exit;
1031 } else if (current_entry != NULL) {
1032 cFYI(1,("entry %lld found",file->f_pos));
1033 } else {
1034 cFYI(1,("could not find entry"));
1035 goto rddir2_exit;
1036 }
1037 cFYI(1,("loop through %d times filling dir for net buf %p",
Steve French5bafd762006-06-07 00:18:43 +00001038 num_to_fill,cifsFile->srch_inf.ntwrk_buf_start));
1039 max_len = smbCalcSize((struct smb_hdr *)
1040 cifsFile->srch_inf.ntwrk_buf_start);
1041 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1042
Steve French6a0b4822005-04-28 22:41:05 -07001043 /* To be safe - for UCS to UTF-8 with strings loaded
1044 with the rare long characters alloc more to account for
1045 such multibyte target UTF-8 characters. cifs_unicode.c,
1046 which actually does the conversion, has the same limit */
1047 tmp_buf = kmalloc((2 * NAME_MAX) + 4, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 for(i=0;(i<num_to_fill) && (rc == 0);i++) {
1049 if(current_entry == NULL) {
1050 /* evaluate whether this case is an error */
1051 cERROR(1,("past end of SMB num to fill %d i %d",
1052 num_to_fill, i));
1053 break;
1054 }
Steve French60808232006-04-22 15:53:05 +00001055 /* if buggy server returns . and .. late do
1056 we want to check for that here? */
Steve French5bafd762006-06-07 00:18:43 +00001057 rc = cifs_filldir(current_entry, file,
1058 filldir, direntry, tmp_buf, max_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 file->f_pos++;
Steve French39798772006-05-31 22:40:51 +00001060 if(file->f_pos ==
1061 cifsFile->srch_inf.index_of_last_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 cFYI(1,("last entry in buf at pos %lld %s",
Steve French39798772006-05-31 22:40:51 +00001063 file->f_pos,tmp_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 cifs_save_resume_key(current_entry,cifsFile);
1065 break;
1066 } else
Steve French5bafd762006-06-07 00:18:43 +00001067 current_entry =
1068 nxt_dir_entry(current_entry, end_of_smb,
1069 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071 kfree(tmp_buf);
1072 break;
1073 } /* end switch */
1074
1075rddir2_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 FreeXid(xid);
1077 return rc;
1078}