blob: b0e5db10664ce1299560290ee903ba21429742da [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 */
Steve French1bd5bbc2006-09-28 03:35:57 +0000138/* struct timespec ts;*/
Steve French5bafd762006-06-07 00:18:43 +0000139 FIND_FILE_STANDARD_INFO * pfindData =
140 (FIND_FILE_STANDARD_INFO *)buf;
141
Steve French1bd5bbc2006-09-28 03:35:57 +0000142/* ts = cnvrtDosUnixTm(
143 le16_to_cpu(pfindData->LastWriteDate),
144 le16_to_cpu(pfindData->LastWriteTime));*/
Steve French5bafd762006-06-07 00:18:43 +0000145 attr = le16_to_cpu(pfindData->Attributes);
146 allocation_size = le32_to_cpu(pfindData->AllocationSize);
147 end_of_file = le32_to_cpu(pfindData->DataSize);
Steve French1bd5bbc2006-09-28 03:35:57 +0000148 /* do not need to use current_fs_time helper function since
149 time not stored for this case so atime can not "go backwards"
150 by pulling newer older from disk when inode refrenshed */
Steve French5bafd762006-06-07 00:18:43 +0000151 tmp_inode->i_atime = CURRENT_TIME;
152 /* tmp_inode->i_mtime = BB FIXME - add dos time handling
153 tmp_inode->i_ctime = 0; BB FIXME */
154
155 }
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /* Linux can not store file creation time unfortunately so ignore it */
Steve French5bafd762006-06-07 00:18:43 +0000158
159 cifsInfo->cifsAttrs = attr;
160 cifsInfo->time = jiffies;
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
163 /* 2767 perms - indicate mandatory locking */
164 /* BB fill in uid and gid here? with help from winbind?
165 or retrieve from NTFS stream extended attribute */
166 if (atomic_read(&cifsInfo->inUse) == 0) {
167 tmp_inode->i_uid = cifs_sb->mnt_uid;
168 tmp_inode->i_gid = cifs_sb->mnt_gid;
169 /* set default mode. will override for dirs below */
170 tmp_inode->i_mode = cifs_sb->mnt_file_mode;
Steve French3020a1f2005-11-18 11:31:10 -0800171 } else {
172 /* mask off the type bits since it gets set
173 below and we do not want to get two type
174 bits set */
175 tmp_inode->i_mode &= ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (attr & ATTR_DIRECTORY) {
179 *pobject_type = DT_DIR;
180 /* override default perms since we do not lock dirs */
181 if(atomic_read(&cifsInfo->inUse) == 0) {
182 tmp_inode->i_mode = cifs_sb->mnt_dir_mode;
183 }
184 tmp_inode->i_mode |= S_IFDIR;
Steve Frencheda3c0292005-07-21 15:20:28 -0700185 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
Steve French3020a1f2005-11-18 11:31:10 -0800186 (attr & ATTR_SYSTEM)) {
187 if (end_of_file == 0) {
188 *pobject_type = DT_FIFO;
189 tmp_inode->i_mode |= S_IFIFO;
190 } else {
191 /* rather than get the type here, we mark the
192 inode as needing revalidate and get the real type
193 (blk vs chr vs. symlink) later ie in lookup */
194 *pobject_type = DT_REG;
195 tmp_inode->i_mode |= S_IFREG;
196 cifsInfo->time = 0;
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/* we no longer mark these because we could not follow them */
199/* } else if (attr & ATTR_REPARSE) {
200 *pobject_type = DT_LNK;
201 tmp_inode->i_mode |= S_IFLNK; */
202 } else {
203 *pobject_type = DT_REG;
204 tmp_inode->i_mode |= S_IFREG;
205 if (attr & ATTR_READONLY)
206 tmp_inode->i_mode &= ~(S_IWUGO);
207 } /* could add code here - to validate if device or weird share type? */
208
209 /* can not fill in nlink here as in qpathinfo version and Unx search */
210 if (atomic_read(&cifsInfo->inUse) == 0) {
211 atomic_set(&cifsInfo->inUse, 1);
212 }
213
214 if (is_size_safe_to_change(cifsInfo)) {
215 /* can not safely change the file size here if the
216 client is writing to it due to potential races */
217 i_size_write(tmp_inode, end_of_file);
218
219 /* 512 bytes (2**9) is the fake blocksize that must be used */
220 /* for this calculation, even though the reported blocksize is larger */
221 tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
222 }
223
224 if (allocation_size < end_of_file)
225 cFYI(1, ("May be sparse file, allocation less than file size"));
Andrew Morton5515eff2006-03-26 01:37:53 -0800226 cFYI(1, ("File Size %ld and blocks %llu and blocksize %ld",
227 (unsigned long)tmp_inode->i_size,
228 (unsigned long long)tmp_inode->i_blocks,
229 tmp_inode->i_blksize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (S_ISREG(tmp_inode->i_mode)) {
Steve French966ca922005-04-28 22:41:08 -0700231 cFYI(1, ("File inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 tmp_inode->i_op = &cifs_file_inode_ops;
Steve French8b94bcb2005-11-11 11:41:00 -0800233 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
234 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
235 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
236 else
237 tmp_inode->i_fop = &cifs_file_direct_ops;
238
239 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
240 tmp_inode->i_fop = &cifs_file_nobrl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 else
242 tmp_inode->i_fop = &cifs_file_ops;
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800243
Steve Frenchbfa0d752005-08-31 21:50:37 -0700244 if((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
245 (cifs_sb->tcon->ses->server->maxBuf <
Dave Kleikamp273d81d2006-06-01 19:41:23 +0000246 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
247 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
248 else
249 tmp_inode->i_data.a_ops = &cifs_addr_ops;
250
Steve French966ca922005-04-28 22:41:08 -0700251 if(isNewInode)
Steve Frenchdfb75332005-06-22 17:13:47 -0700252 return; /* No sense invalidating pages for new inode
253 since have not started caching readahead file
254 data yet */
Steve French966ca922005-04-28 22:41:08 -0700255
256 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
257 (local_size == tmp_inode->i_size)) {
258 cFYI(1, ("inode exists but unchanged"));
259 } else {
260 /* file may have changed on server */
261 cFYI(1, ("invalidate inode, readdir detected change"));
262 invalidate_remote_inode(tmp_inode);
263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 } else if (S_ISDIR(tmp_inode->i_mode)) {
Steve French966ca922005-04-28 22:41:08 -0700265 cFYI(1, ("Directory inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 tmp_inode->i_op = &cifs_dir_inode_ops;
267 tmp_inode->i_fop = &cifs_dir_ops;
268 } else if (S_ISLNK(tmp_inode->i_mode)) {
Steve French966ca922005-04-28 22:41:08 -0700269 cFYI(1, ("Symbolic Link inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 tmp_inode->i_op = &cifs_symlink_inode_ops;
271 } else {
Steve French966ca922005-04-28 22:41:08 -0700272 cFYI(1, ("Init special inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 init_special_inode(tmp_inode, tmp_inode->i_mode,
274 tmp_inode->i_rdev);
275 }
276}
277
278static void unix_fill_in_inode(struct inode *tmp_inode,
Steve French966ca922005-04-28 22:41:08 -0700279 FILE_UNIX_INFO *pfindData, int *pobject_type, int isNewInode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Steve French966ca922005-04-28 22:41:08 -0700281 loff_t local_size;
282 struct timespec local_mtime;
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
285 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
286
287 __u32 type = le32_to_cpu(pfindData->Type);
288 __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes);
289 __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
290 cifsInfo->time = jiffies;
291 atomic_inc(&cifsInfo->inUse);
292
Steve French966ca922005-04-28 22:41:08 -0700293 /* save mtime and size */
294 local_mtime = tmp_inode->i_mtime;
295 local_size = tmp_inode->i_size;
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 tmp_inode->i_atime =
298 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
299 tmp_inode->i_mtime =
300 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime));
301 tmp_inode->i_ctime =
302 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange));
303
304 tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions);
Steve French3020a1f2005-11-18 11:31:10 -0800305 /* since we set the inode type below we need to mask off type
306 to avoid strange results if bits above were corrupt */
307 tmp_inode->i_mode &= ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (type == UNIX_FILE) {
309 *pobject_type = DT_REG;
310 tmp_inode->i_mode |= S_IFREG;
311 } else if (type == UNIX_SYMLINK) {
312 *pobject_type = DT_LNK;
313 tmp_inode->i_mode |= S_IFLNK;
314 } else if (type == UNIX_DIR) {
315 *pobject_type = DT_DIR;
316 tmp_inode->i_mode |= S_IFDIR;
317 } else if (type == UNIX_CHARDEV) {
318 *pobject_type = DT_CHR;
319 tmp_inode->i_mode |= S_IFCHR;
320 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
321 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
322 } else if (type == UNIX_BLOCKDEV) {
323 *pobject_type = DT_BLK;
324 tmp_inode->i_mode |= S_IFBLK;
325 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
326 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
327 } else if (type == UNIX_FIFO) {
328 *pobject_type = DT_FIFO;
329 tmp_inode->i_mode |= S_IFIFO;
330 } else if (type == UNIX_SOCKET) {
331 *pobject_type = DT_SOCK;
332 tmp_inode->i_mode |= S_IFSOCK;
Steve French3020a1f2005-11-18 11:31:10 -0800333 } else {
334 /* safest to just call it a file */
335 *pobject_type = DT_REG;
336 tmp_inode->i_mode |= S_IFREG;
337 cFYI(1,("unknown inode type %d",type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
340 tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
341 tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
342 tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
343
344 if (is_size_safe_to_change(cifsInfo)) {
345 /* can not safely change the file size here if the
346 client is writing to it due to potential races */
347 i_size_write(tmp_inode,end_of_file);
348
349 /* 512 bytes (2**9) is the fake blocksize that must be used */
350 /* for this calculation, not the real blocksize */
351 tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
352 }
353
354 if (S_ISREG(tmp_inode->i_mode)) {
355 cFYI(1, ("File inode"));
356 tmp_inode->i_op = &cifs_file_inode_ops;
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800357
358 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
359 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
360 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
361 else
362 tmp_inode->i_fop = &cifs_file_direct_ops;
363
364 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
365 tmp_inode->i_fop = &cifs_file_nobrl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 else
367 tmp_inode->i_fop = &cifs_file_ops;
Steve Frenchf3f6ec42006-01-08 20:12:58 -0800368
Steve Frenchbfa0d752005-08-31 21:50:37 -0700369 if((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
370 (cifs_sb->tcon->ses->server->maxBuf <
Dave Kleikamp273d81d2006-06-01 19:41:23 +0000371 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
372 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
373 else
374 tmp_inode->i_data.a_ops = &cifs_addr_ops;
Steve French966ca922005-04-28 22:41:08 -0700375
376 if(isNewInode)
377 return; /* No sense invalidating pages for new inode since we
378 have not started caching readahead file data yet */
379
380 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
381 (local_size == tmp_inode->i_size)) {
382 cFYI(1, ("inode exists but unchanged"));
383 } else {
384 /* file may have changed on server */
385 cFYI(1, ("invalidate inode, readdir detected change"));
386 invalidate_remote_inode(tmp_inode);
387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 } else if (S_ISDIR(tmp_inode->i_mode)) {
389 cFYI(1, ("Directory inode"));
390 tmp_inode->i_op = &cifs_dir_inode_ops;
391 tmp_inode->i_fop = &cifs_dir_ops;
392 } else if (S_ISLNK(tmp_inode->i_mode)) {
393 cFYI(1, ("Symbolic Link inode"));
394 tmp_inode->i_op = &cifs_symlink_inode_ops;
395/* tmp_inode->i_fop = *//* do not need to set to anything */
396 } else {
397 cFYI(1, ("Special inode"));
398 init_special_inode(tmp_inode, tmp_inode->i_mode,
399 tmp_inode->i_rdev);
400 }
401}
402
403static int initiate_cifs_search(const int xid, struct file *file)
404{
405 int rc = 0;
406 char * full_path;
407 struct cifsFileInfo * cifsFile;
408 struct cifs_sb_info *cifs_sb;
409 struct cifsTconInfo *pTcon;
410
411 if(file->private_data == NULL) {
412 file->private_data =
413 kmalloc(sizeof(struct cifsFileInfo),GFP_KERNEL);
414 }
415
416 if(file->private_data == NULL) {
417 return -ENOMEM;
418 } else {
419 memset(file->private_data,0,sizeof(struct cifsFileInfo));
420 }
421 cifsFile = file->private_data;
422 cifsFile->invalidHandle = TRUE;
423 cifsFile->srch_inf.endOfSearch = FALSE;
424
425 if(file->f_dentry == NULL)
426 return -ENOENT;
427
428 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
429 if(cifs_sb == NULL)
430 return -EINVAL;
431
432 pTcon = cifs_sb->tcon;
433 if(pTcon == NULL)
434 return -EINVAL;
435
Steve French7f573562005-08-30 11:32:14 -0700436 full_path = build_path_from_dentry(file->f_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 if(full_path == NULL) {
439 return -ENOMEM;
440 }
441
Steve French966ca922005-04-28 22:41:08 -0700442 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Steve French75cf6bd2005-04-28 22:41:04 -0700444ffirst_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /* test for Unix extensions */
446 if (pTcon->ses->capabilities & CAP_UNIX) {
Steve French5bafd762006-06-07 00:18:43 +0000447 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
448 } else if ((pTcon->ses->capabilities &
449 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
450 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
452 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
453 } else /* not srvinos - BB fixme add check for backlevel? */ {
454 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
455 }
456
Steve French737b7582005-04-28 22:41:06 -0700457 rc = CIFSFindFirst(xid, pTcon,full_path,cifs_sb->local_nls,
458 &cifsFile->netfid, &cifsFile->srch_inf,
Steve Frencheafe8702005-09-15 21:47:30 -0700459 cifs_sb->mnt_cifs_flags &
460 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if(rc == 0)
462 cifsFile->invalidHandle = FALSE;
Steve French75cf6bd2005-04-28 22:41:04 -0700463 if((rc == -EOPNOTSUPP) &&
464 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
465 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
466 goto ffirst_retry;
467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 kfree(full_path);
469 return rc;
470}
471
472/* return length of unicode string in bytes */
473static int cifs_unicode_bytelen(char *str)
474{
475 int len;
476 __le16 * ustr = (__le16 *)str;
477
478 for(len=0;len <= PATH_MAX;len++) {
479 if(ustr[len] == 0)
480 return len << 1;
481 }
482 cFYI(1,("Unicode string longer than PATH_MAX found"));
483 return len << 1;
484}
485
Steve French5bafd762006-06-07 00:18:43 +0000486static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 char * new_entry;
489 FILE_DIRECTORY_INFO * pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
490
Steve French5bafd762006-06-07 00:18:43 +0000491 if(level == SMB_FIND_FILE_INFO_STANDARD) {
492 FIND_FILE_STANDARD_INFO * pfData;
493 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
494
495 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
496 pfData->FileNameLength;
497 } else
498 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 cFYI(1,("new entry %p old entry %p",new_entry,old_entry));
500 /* validate that new_entry is not past end of SMB */
501 if(new_entry >= end_of_smb) {
Steve French09d1db52005-04-28 22:41:08 -0700502 cERROR(1,
503 ("search entry %p began after end of SMB %p old entry %p",
504 new_entry, end_of_smb, old_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return NULL;
Steve French5bafd762006-06-07 00:18:43 +0000506 } else if(((level == SMB_FIND_FILE_INFO_STANDARD) &&
507 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb)) ||
508 ((level != SMB_FIND_FILE_INFO_STANDARD) &&
509 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
Steve French09d1db52005-04-28 22:41:08 -0700510 cERROR(1,("search entry %p extends after end of SMB %p",
511 new_entry, end_of_smb));
512 return NULL;
513 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return new_entry;
515
516}
517
518#define UNICODE_DOT cpu_to_le16(0x2e)
519
520/* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
521static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
522{
523 int rc = 0;
524 char * filename = NULL;
525 int len = 0;
526
Steve French5bafd762006-06-07 00:18:43 +0000527 if(cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
529 filename = &pFindData->FileName[0];
530 if(cfile->srch_inf.unicode) {
531 len = cifs_unicode_bytelen(filename);
532 } else {
533 /* BB should we make this strnlen of PATH_MAX? */
534 len = strnlen(filename, 5);
535 }
Steve French5bafd762006-06-07 00:18:43 +0000536 } else if(cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 FILE_DIRECTORY_INFO * pFindData =
538 (FILE_DIRECTORY_INFO *)current_entry;
539 filename = &pFindData->FileName[0];
540 len = le32_to_cpu(pFindData->FileNameLength);
Steve French5bafd762006-06-07 00:18:43 +0000541 } else if(cfile->srch_inf.info_level ==
542 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 FILE_FULL_DIRECTORY_INFO * pFindData =
544 (FILE_FULL_DIRECTORY_INFO *)current_entry;
545 filename = &pFindData->FileName[0];
546 len = le32_to_cpu(pFindData->FileNameLength);
Steve French5bafd762006-06-07 00:18:43 +0000547 } else if(cfile->srch_inf.info_level ==
548 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 SEARCH_ID_FULL_DIR_INFO * pFindData =
550 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
551 filename = &pFindData->FileName[0];
552 len = le32_to_cpu(pFindData->FileNameLength);
Steve French5bafd762006-06-07 00:18:43 +0000553 } else if(cfile->srch_inf.info_level ==
554 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 FILE_BOTH_DIRECTORY_INFO * pFindData =
556 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
557 filename = &pFindData->FileName[0];
558 len = le32_to_cpu(pFindData->FileNameLength);
Steve French5bafd762006-06-07 00:18:43 +0000559 } else if(cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
560 FIND_FILE_STANDARD_INFO * pFindData =
561 (FIND_FILE_STANDARD_INFO *)current_entry;
562 filename = &pFindData->FileName[0];
Steve French5ddaa682006-08-15 13:35:48 +0000563 len = pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 } else {
565 cFYI(1,("Unknown findfirst level %d",cfile->srch_inf.info_level));
566 }
567
568 if(filename) {
569 if(cfile->srch_inf.unicode) {
570 __le16 *ufilename = (__le16 *)filename;
571 if(len == 2) {
572 /* check for . */
573 if(ufilename[0] == UNICODE_DOT)
574 rc = 1;
575 } else if(len == 4) {
576 /* check for .. */
577 if((ufilename[0] == UNICODE_DOT)
578 &&(ufilename[1] == UNICODE_DOT))
579 rc = 2;
580 }
581 } else /* ASCII */ {
582 if(len == 1) {
583 if(filename[0] == '.')
584 rc = 1;
585 } else if(len == 2) {
586 if((filename[0] == '.') && (filename[1] == '.'))
587 rc = 2;
588 }
589 }
590 }
591
592 return rc;
593}
594
Steve Frencheafe8702005-09-15 21:47:30 -0700595/* Check if directory that we are searching has changed so we can decide
596 whether we can use the cached search results from the previous search */
597static int is_dir_changed(struct file * file)
598{
599 struct inode * inode;
600 struct cifsInodeInfo *cifsInfo;
601
602 if(file->f_dentry == NULL)
603 return 0;
604
605 inode = file->f_dentry->d_inode;
606
607 if(inode == NULL)
608 return 0;
609
610 cifsInfo = CIFS_I(inode);
611
612 if(cifsInfo->time == 0)
613 return 1; /* directory was changed, perhaps due to unlink */
614 else
615 return 0;
616
617}
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/* find the corresponding entry in the search */
620/* Note that the SMB server returns search entries for . and .. which
621 complicates logic here if we choose to parse for them and we do not
622 assume that they are located in the findfirst return buffer.*/
623/* We start counting in the buffer with entry 2 and increment for every
624 entry (do not increment for . or .. entry) */
625static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
626 struct file *file, char **ppCurrentEntry, int *num_to_ret)
627{
628 int rc = 0;
629 int pos_in_buf = 0;
630 loff_t first_entry_in_buffer;
631 loff_t index_to_find = file->f_pos;
632 struct cifsFileInfo * cifsFile = file->private_data;
633 /* check if index in the buffer */
634
Steve Frencheafe8702005-09-15 21:47:30 -0700635 if((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
636 (num_to_ret == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return -ENOENT;
638
639 *ppCurrentEntry = NULL;
640 first_entry_in_buffer =
641 cifsFile->srch_inf.index_of_last_entry -
642 cifsFile->srch_inf.entries_in_buffer;
Steve French60808232006-04-22 15:53:05 +0000643
644 /* if first entry in buf is zero then is first buffer
645 in search response data which means it is likely . and ..
646 will be in this buffer, although some servers do not return
647 . and .. for the root of a drive and for those we need
648 to start two entries earlier */
649
Steve French39798772006-05-31 22:40:51 +0000650#ifdef CONFIG_CIFS_DEBUG2
651 dump_cifs_file_struct(file, "In fce ");
652#endif
Steve Frencheafe8702005-09-15 21:47:30 -0700653 if(((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
654 is_dir_changed(file)) ||
655 (index_to_find < first_entry_in_buffer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* close and restart search */
657 cFYI(1,("search backing up - close and restart search"));
658 cifsFile->invalidHandle = TRUE;
659 CIFSFindClose(xid, pTcon, cifsFile->netfid);
660 kfree(cifsFile->search_resume_name);
661 cifsFile->search_resume_name = NULL;
662 if(cifsFile->srch_inf.ntwrk_buf_start) {
663 cFYI(1,("freeing SMB ff cache buf on search rewind"));
Steve Frenchd47d7c12006-02-28 03:45:48 +0000664 if(cifsFile->srch_inf.smallBuf)
665 cifs_small_buf_release(cifsFile->srch_inf.
666 ntwrk_buf_start);
667 else
668 cifs_buf_release(cifsFile->srch_inf.
669 ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671 rc = initiate_cifs_search(xid,file);
672 if(rc) {
673 cFYI(1,("error %d reinitiating a search on rewind",rc));
674 return rc;
675 }
676 }
677
678 while((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
679 (rc == 0) && (cifsFile->srch_inf.endOfSearch == FALSE)){
680 cFYI(1,("calling findnext2"));
Steve Frenchdfb75332005-06-22 17:13:47 -0700681 rc = CIFSFindNext(xid,pTcon,cifsFile->netfid,
682 &cifsFile->srch_inf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if(rc)
684 return -ENOENT;
685 }
686 if(index_to_find < cifsFile->srch_inf.index_of_last_entry) {
687 /* we found the buffer that contains the entry */
688 /* scan and find it */
689 int i;
690 char * current_entry;
691 char * end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
692 smbCalcSize((struct smb_hdr *)
693 cifsFile->srch_inf.ntwrk_buf_start);
Steve French60808232006-04-22 15:53:05 +0000694
695 current_entry = cifsFile->srch_inf.srch_entries_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
697 - cifsFile->srch_inf.entries_in_buffer;
698 pos_in_buf = index_to_find - first_entry_in_buffer;
Steve French5bafd762006-06-07 00:18:43 +0000699 cFYI(1,("found entry - pos_in_buf %d",pos_in_buf));
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 for(i=0;(i<(pos_in_buf)) && (current_entry != NULL);i++) {
Steve Frenchdfb75332005-06-22 17:13:47 -0700702 /* go entry by entry figuring out which is first */
Steve French5bafd762006-06-07 00:18:43 +0000703 current_entry = nxt_dir_entry(current_entry,end_of_smb,
704 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706 if((current_entry == NULL) && (i < pos_in_buf)) {
707 /* BB fixme - check if we should flag this error */
708 cERROR(1,("reached end of buf searching for pos in buf"
709 " %d index to find %lld rc %d",
710 pos_in_buf,index_to_find,rc));
711 }
712 rc = 0;
713 *ppCurrentEntry = current_entry;
714 } else {
715 cFYI(1,("index not in buffer - could not findnext into it"));
716 return 0;
717 }
718
719 if(pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
Steve Frencheafe8702005-09-15 21:47:30 -0700720 cFYI(1,("can not return entries pos_in_buf beyond last entry"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 *num_to_ret = 0;
722 } else
723 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 return rc;
726}
727
728/* inode num, inode type and filename returned */
729static int cifs_get_name_from_search_buf(struct qstr *pqst,
730 char *current_entry, __u16 level, unsigned int unicode,
Steve French5bafd762006-06-07 00:18:43 +0000731 struct cifs_sb_info * cifs_sb, int max_len, ino_t *pinum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 int rc = 0;
734 unsigned int len = 0;
735 char * filename;
736 struct nls_table * nlt = cifs_sb->local_nls;
737
738 *pinum = 0;
739
740 if(level == SMB_FIND_FILE_UNIX) {
741 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
742
743 filename = &pFindData->FileName[0];
744 if(unicode) {
745 len = cifs_unicode_bytelen(filename);
746 } else {
747 /* BB should we make this strnlen of PATH_MAX? */
748 len = strnlen(filename, PATH_MAX);
749 }
750
751 /* BB fixme - hash low and high 32 bits if not 64 bit arch BB fixme */
752 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
753 *pinum = pFindData->UniqueId;
754 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
755 FILE_DIRECTORY_INFO * pFindData =
756 (FILE_DIRECTORY_INFO *)current_entry;
757 filename = &pFindData->FileName[0];
758 len = le32_to_cpu(pFindData->FileNameLength);
759 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
760 FILE_FULL_DIRECTORY_INFO * pFindData =
761 (FILE_FULL_DIRECTORY_INFO *)current_entry;
762 filename = &pFindData->FileName[0];
763 len = le32_to_cpu(pFindData->FileNameLength);
764 } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
765 SEARCH_ID_FULL_DIR_INFO * pFindData =
766 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
767 filename = &pFindData->FileName[0];
768 len = le32_to_cpu(pFindData->FileNameLength);
769 *pinum = pFindData->UniqueId;
770 } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
771 FILE_BOTH_DIRECTORY_INFO * pFindData =
772 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
773 filename = &pFindData->FileName[0];
774 len = le32_to_cpu(pFindData->FileNameLength);
Steve French5bafd762006-06-07 00:18:43 +0000775 } else if(level == SMB_FIND_FILE_INFO_STANDARD) {
776 FIND_FILE_STANDARD_INFO * pFindData =
777 (FIND_FILE_STANDARD_INFO *)current_entry;
778 filename = &pFindData->FileName[0];
779 /* one byte length, no name conversion */
780 len = (unsigned int)pFindData->FileNameLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 } else {
782 cFYI(1,("Unknown findfirst level %d",level));
783 return -EINVAL;
784 }
Steve French5bafd762006-06-07 00:18:43 +0000785
786 if(len > max_len) {
787 cERROR(1,("bad search response length %d past smb end", len));
788 return -EINVAL;
789 }
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if(unicode) {
792 /* BB fixme - test with long names */
793 /* Note converted filename can be longer than in unicode */
Steve French6a0b4822005-04-28 22:41:05 -0700794 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
795 pqst->len = cifs_convertUCSpath((char *)pqst->name,
796 (__le16 *)filename, len/2, nlt);
797 else
Steve French6a0b4822005-04-28 22:41:05 -0700798 pqst->len = cifs_strfromUCS_le((char *)pqst->name,
Steve Frenche89dc922005-11-11 15:18:19 -0800799 (__le16 *)filename,len/2,nlt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 } else {
801 pqst->name = filename;
802 pqst->len = len;
803 }
804 pqst->hash = full_name_hash(pqst->name,pqst->len);
805/* cFYI(1,("filldir on %s",pqst->name)); */
806 return rc;
807}
808
809static int cifs_filldir(char *pfindEntry, struct file *file,
Steve French5bafd762006-06-07 00:18:43 +0000810 filldir_t filldir, void *direntry, char *scratch_buf, int max_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 int rc = 0;
813 struct qstr qstring;
814 struct cifsFileInfo * pCifsF;
815 unsigned obj_type;
816 ino_t inum;
817 struct cifs_sb_info * cifs_sb;
818 struct inode *tmp_inode;
819 struct dentry *tmp_dentry;
820
821 /* get filename and len into qstring */
822 /* get dentry */
823 /* decide whether to create and populate ionde */
824 if((direntry == NULL) || (file == NULL))
825 return -EINVAL;
826
827 pCifsF = file->private_data;
828
829 if((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
830 return -ENOENT;
831
832 if(file->f_dentry == NULL)
833 return -ENOENT;
834
Steve Frenchb66ac3e2006-04-23 01:54:50 +0000835 rc = cifs_entry_is_dot(pfindEntry,pCifsF);
Steve French60808232006-04-22 15:53:05 +0000836 /* skip . and .. since we added them first */
837 if(rc != 0)
838 return 0;
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
841
842 qstring.name = scratch_buf;
843 rc = cifs_get_name_from_search_buf(&qstring,pfindEntry,
844 pCifsF->srch_inf.info_level,
845 pCifsF->srch_inf.unicode,cifs_sb,
Steve French5bafd762006-06-07 00:18:43 +0000846 max_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 &inum /* returned */);
848
849 if(rc)
850 return rc;
851
852 rc = construct_dentry(&qstring,file,&tmp_inode, &tmp_dentry);
853 if((tmp_inode == NULL) || (tmp_dentry == NULL))
854 return -ENOMEM;
855
856 if(rc) {
857 /* inode created, we need to hash it with right inode number */
858 if(inum != 0) {
859 /* BB fixme - hash the 2 32 quantities bits together if necessary BB */
860 tmp_inode->i_ino = inum;
861 }
862 insert_inode_hash(tmp_inode);
863 }
864
Steve French966ca922005-04-28 22:41:08 -0700865 /* we pass in rc below, indicating whether it is a new inode,
866 so we can figure out whether to invalidate the inode cached
867 data if the file has changed */
Steve French5bafd762006-06-07 00:18:43 +0000868 if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
Steve French966ca922005-04-28 22:41:08 -0700869 unix_fill_in_inode(tmp_inode,
Steve French5bafd762006-06-07 00:18:43 +0000870 (FILE_UNIX_INFO *)pfindEntry,
871 &obj_type, rc);
872 else if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
873 fill_in_inode(tmp_inode, 0 /* old level 1 buffer type */,
874 pfindEntry, &obj_type, rc);
875 else
876 fill_in_inode(tmp_inode, 1 /* NT */, pfindEntry, &obj_type, rc);
Steve Frenchb835beb2006-09-06 22:02:22 +0000877
878 if(rc) /* new inode - needs to be tied to dentry */ {
879 d_instantiate(tmp_dentry, tmp_inode);
880 if(rc == 2)
881 d_rehash(tmp_dentry);
882 }
Steve French5bafd762006-06-07 00:18:43 +0000883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Steve Frenchdfb75332005-06-22 17:13:47 -0700885 rc = filldir(direntry,qstring.name,qstring.len,file->f_pos,
886 tmp_inode->i_ino,obj_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if(rc) {
888 cFYI(1,("filldir rc = %d",rc));
889 }
890
891 dput(tmp_dentry);
892 return rc;
893}
894
895static int cifs_save_resume_key(const char *current_entry,
896 struct cifsFileInfo *cifsFile)
897{
898 int rc = 0;
899 unsigned int len = 0;
900 __u16 level;
901 char * filename;
902
903 if((cifsFile == NULL) || (current_entry == NULL))
904 return -EINVAL;
905
906 level = cifsFile->srch_inf.info_level;
907
908 if(level == SMB_FIND_FILE_UNIX) {
909 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
910
911 filename = &pFindData->FileName[0];
912 if(cifsFile->srch_inf.unicode) {
913 len = cifs_unicode_bytelen(filename);
914 } else {
915 /* BB should we make this strnlen of PATH_MAX? */
916 len = strnlen(filename, PATH_MAX);
917 }
918 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
919 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
920 FILE_DIRECTORY_INFO * pFindData =
921 (FILE_DIRECTORY_INFO *)current_entry;
922 filename = &pFindData->FileName[0];
923 len = le32_to_cpu(pFindData->FileNameLength);
924 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
925 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
926 FILE_FULL_DIRECTORY_INFO * pFindData =
927 (FILE_FULL_DIRECTORY_INFO *)current_entry;
928 filename = &pFindData->FileName[0];
929 len = le32_to_cpu(pFindData->FileNameLength);
930 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
931 } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
932 SEARCH_ID_FULL_DIR_INFO * pFindData =
933 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
934 filename = &pFindData->FileName[0];
935 len = le32_to_cpu(pFindData->FileNameLength);
936 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
937 } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
938 FILE_BOTH_DIRECTORY_INFO * pFindData =
939 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
940 filename = &pFindData->FileName[0];
941 len = le32_to_cpu(pFindData->FileNameLength);
942 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
Steve French5bafd762006-06-07 00:18:43 +0000943 } else if(level == SMB_FIND_FILE_INFO_STANDARD) {
944 FIND_FILE_STANDARD_INFO * pFindData =
945 (FIND_FILE_STANDARD_INFO *)current_entry;
946 filename = &pFindData->FileName[0];
947 /* one byte length, no name conversion */
948 len = (unsigned int)pFindData->FileNameLength;
Steve French203cf2f2006-10-01 19:59:41 +0000949 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 } else {
951 cFYI(1,("Unknown findfirst level %d",level));
952 return -EINVAL;
953 }
954 cifsFile->srch_inf.resume_name_len = len;
955 cifsFile->srch_inf.presume_name = filename;
956 return rc;
957}
958
959int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
960{
961 int rc = 0;
962 int xid,i;
963 struct cifs_sb_info *cifs_sb;
964 struct cifsTconInfo *pTcon;
965 struct cifsFileInfo *cifsFile = NULL;
966 char * current_entry;
967 int num_to_fill = 0;
968 char * tmp_buf = NULL;
969 char * end_of_smb;
Steve French5bafd762006-06-07 00:18:43 +0000970 int max_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 xid = GetXid();
973
974 if(file->f_dentry == NULL) {
975 FreeXid(xid);
976 return -EIO;
977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
980 pTcon = cifs_sb->tcon;
981 if(pTcon == NULL)
982 return -EINVAL;
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 switch ((int) file->f_pos) {
985 case 0:
Steve French60808232006-04-22 15:53:05 +0000986 if (filldir(direntry, ".", 1, file->f_pos,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 file->f_dentry->d_inode->i_ino, DT_DIR) < 0) {
Steve French60808232006-04-22 15:53:05 +0000988 cERROR(1, ("Filldir for current 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++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 case 1:
Steve French60808232006-04-22 15:53:05 +0000994 if (filldir(direntry, "..", 2, file->f_pos,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 file->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
Steve French26a21b92006-05-31 18:05:34 +0000996 cERROR(1, ("Filldir for parent dir failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 rc = -ENOMEM;
998 break;
999 }
Steve French60808232006-04-22 15:53:05 +00001000 file->f_pos++;
1001 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 /* 1) If search is active,
1003 is in current search buffer?
1004 if it before then restart search
1005 if after then keep searching till find it */
1006
1007 if(file->private_data == NULL) {
1008 rc = initiate_cifs_search(xid,file);
1009 cFYI(1,("initiate cifs search rc %d",rc));
1010 if(rc) {
1011 FreeXid(xid);
1012 return rc;
1013 }
1014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if(file->private_data == NULL) {
1016 rc = -EINVAL;
1017 FreeXid(xid);
1018 return rc;
1019 }
1020 cifsFile = file->private_data;
1021 if (cifsFile->srch_inf.endOfSearch) {
1022 if(cifsFile->srch_inf.emptyDir) {
1023 cFYI(1, ("End of search, empty dir"));
1024 rc = 0;
1025 break;
1026 }
1027 } /* else {
1028 cifsFile->invalidHandle = TRUE;
1029 CIFSFindClose(xid, pTcon, cifsFile->netfid);
1030 }
1031 kfree(cifsFile->search_resume_name);
1032 cifsFile->search_resume_name = NULL; */
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 rc = find_cifs_entry(xid,pTcon, file,
1035 &current_entry,&num_to_fill);
1036 if(rc) {
1037 cFYI(1,("fce error %d",rc));
1038 goto rddir2_exit;
1039 } else if (current_entry != NULL) {
1040 cFYI(1,("entry %lld found",file->f_pos));
1041 } else {
1042 cFYI(1,("could not find entry"));
1043 goto rddir2_exit;
1044 }
1045 cFYI(1,("loop through %d times filling dir for net buf %p",
Steve French5bafd762006-06-07 00:18:43 +00001046 num_to_fill,cifsFile->srch_inf.ntwrk_buf_start));
1047 max_len = smbCalcSize((struct smb_hdr *)
1048 cifsFile->srch_inf.ntwrk_buf_start);
1049 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1050
Steve French6a0b4822005-04-28 22:41:05 -07001051 /* To be safe - for UCS to UTF-8 with strings loaded
1052 with the rare long characters alloc more to account for
1053 such multibyte target UTF-8 characters. cifs_unicode.c,
1054 which actually does the conversion, has the same limit */
1055 tmp_buf = kmalloc((2 * NAME_MAX) + 4, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 for(i=0;(i<num_to_fill) && (rc == 0);i++) {
1057 if(current_entry == NULL) {
1058 /* evaluate whether this case is an error */
1059 cERROR(1,("past end of SMB num to fill %d i %d",
1060 num_to_fill, i));
1061 break;
1062 }
Steve French60808232006-04-22 15:53:05 +00001063 /* if buggy server returns . and .. late do
1064 we want to check for that here? */
Steve French5bafd762006-06-07 00:18:43 +00001065 rc = cifs_filldir(current_entry, file,
1066 filldir, direntry, tmp_buf, max_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 file->f_pos++;
Steve French39798772006-05-31 22:40:51 +00001068 if(file->f_pos ==
1069 cifsFile->srch_inf.index_of_last_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 cFYI(1,("last entry in buf at pos %lld %s",
Steve French39798772006-05-31 22:40:51 +00001071 file->f_pos,tmp_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 cifs_save_resume_key(current_entry,cifsFile);
1073 break;
1074 } else
Steve French5bafd762006-06-07 00:18:43 +00001075 current_entry =
1076 nxt_dir_entry(current_entry, end_of_smb,
1077 cifsFile->srch_inf.info_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079 kfree(tmp_buf);
1080 break;
1081 } /* end switch */
1082
1083rddir2_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 FreeXid(xid);
1085 return rc;
1086}