Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / fs / cifs / readdir.c
blob34354e4bea99c32c7e286406d80359c1f98d5d5e
1 /*
2 * fs/cifs/readdir.c
4 * Directory search handling
6 <<<<<<< HEAD:fs/cifs/readdir.c
7 * Copyright (C) International Business Machines Corp., 2004, 2007
8 =======
9 * Copyright (C) International Business Machines Corp., 2004, 2008
10 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
11 * Author(s): Steve French (sfrench@us.ibm.com)
13 * This library is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License as published
15 * by the Free Software Foundation; either version 2.1 of the License, or
16 * (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
21 * the GNU Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this library; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <linux/fs.h>
28 #include <linux/pagemap.h>
29 #include <linux/stat.h>
30 #include "cifspdu.h"
31 #include "cifsglob.h"
32 #include "cifsproto.h"
33 #include "cifs_unicode.h"
34 #include "cifs_debug.h"
35 #include "cifs_fs_sb.h"
36 #include "cifsfs.h"
38 #ifdef CONFIG_CIFS_DEBUG2
39 static void dump_cifs_file_struct(struct file *file, char *label)
41 struct cifsFileInfo *cf;
43 if (file) {
44 cf = file->private_data;
45 if (cf == NULL) {
46 cFYI(1, ("empty cifs private file data"));
47 return;
49 <<<<<<< HEAD:fs/cifs/readdir.c
50 if (cf->invalidHandle) {
51 =======
52 if (cf->invalidHandle)
53 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
54 cFYI(1, ("invalid handle"));
55 <<<<<<< HEAD:fs/cifs/readdir.c
57 if (cf->srch_inf.endOfSearch) {
58 =======
59 if (cf->srch_inf.endOfSearch)
60 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
61 cFYI(1, ("end of search"));
62 <<<<<<< HEAD:fs/cifs/readdir.c
64 if (cf->srch_inf.emptyDir) {
65 =======
66 if (cf->srch_inf.emptyDir)
67 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
68 cFYI(1, ("empty dir"));
69 <<<<<<< HEAD:fs/cifs/readdir.c
71 =======
72 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
75 <<<<<<< HEAD:fs/cifs/readdir.c
76 =======
77 #else
78 static inline void dump_cifs_file_struct(struct file *file, char *label)
81 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
82 #endif /* DEBUG2 */
84 /* Returns one if new inode created (which therefore needs to be hashed) */
85 /* Might check in the future if inode number changed so we can rehash inode */
86 static int construct_dentry(struct qstr *qstring, struct file *file,
87 struct inode **ptmp_inode, struct dentry **pnew_dentry)
89 struct dentry *tmp_dentry;
90 struct cifs_sb_info *cifs_sb;
91 struct cifsTconInfo *pTcon;
92 int rc = 0;
94 cFYI(1, ("For %s", qstring->name));
95 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
96 pTcon = cifs_sb->tcon;
98 qstring->hash = full_name_hash(qstring->name, qstring->len);
99 tmp_dentry = d_lookup(file->f_path.dentry, qstring);
100 if (tmp_dentry) {
101 cFYI(0, ("existing dentry with inode 0x%p",
102 tmp_dentry->d_inode));
103 *ptmp_inode = tmp_dentry->d_inode;
104 /* BB overwrite old name? i.e. tmp_dentry->d_name and tmp_dentry->d_name.len??*/
105 if (*ptmp_inode == NULL) {
106 *ptmp_inode = new_inode(file->f_path.dentry->d_sb);
107 if (*ptmp_inode == NULL)
108 return rc;
109 rc = 1;
111 if (file->f_path.dentry->d_sb->s_flags & MS_NOATIME)
112 (*ptmp_inode)->i_flags |= S_NOATIME | S_NOCMTIME;
113 } else {
114 tmp_dentry = d_alloc(file->f_path.dentry, qstring);
115 if (tmp_dentry == NULL) {
116 cERROR(1, ("Failed allocating dentry"));
117 *ptmp_inode = NULL;
118 return rc;
121 *ptmp_inode = new_inode(file->f_path.dentry->d_sb);
122 if (pTcon->nocase)
123 tmp_dentry->d_op = &cifs_ci_dentry_ops;
124 else
125 tmp_dentry->d_op = &cifs_dentry_ops;
126 if (*ptmp_inode == NULL)
127 return rc;
128 if (file->f_path.dentry->d_sb->s_flags & MS_NOATIME)
129 (*ptmp_inode)->i_flags |= S_NOATIME | S_NOCMTIME;
130 rc = 2;
133 tmp_dentry->d_time = jiffies;
134 *pnew_dentry = tmp_dentry;
135 return rc;
138 static void AdjustForTZ(struct cifsTconInfo *tcon, struct inode *inode)
140 if ((tcon) && (tcon->ses) && (tcon->ses->server)) {
141 inode->i_ctime.tv_sec += tcon->ses->server->timeAdj;
142 inode->i_mtime.tv_sec += tcon->ses->server->timeAdj;
143 inode->i_atime.tv_sec += tcon->ses->server->timeAdj;
145 return;
149 static void fill_in_inode(struct inode *tmp_inode, int new_buf_type,
150 char *buf, unsigned int *pobject_type, int isNewInode)
152 loff_t local_size;
153 struct timespec local_mtime;
155 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
156 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
157 __u32 attr;
158 __u64 allocation_size;
159 __u64 end_of_file;
161 /* save mtime and size */
162 local_mtime = tmp_inode->i_mtime;
163 local_size = tmp_inode->i_size;
165 if (new_buf_type) {
166 FILE_DIRECTORY_INFO *pfindData = (FILE_DIRECTORY_INFO *)buf;
168 attr = le32_to_cpu(pfindData->ExtFileAttributes);
169 allocation_size = le64_to_cpu(pfindData->AllocationSize);
170 end_of_file = le64_to_cpu(pfindData->EndOfFile);
171 tmp_inode->i_atime =
172 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
173 tmp_inode->i_mtime =
174 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
175 tmp_inode->i_ctime =
176 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
177 } else { /* legacy, OS2 and DOS style */
178 /* struct timespec ts;*/
179 <<<<<<< HEAD:fs/cifs/readdir.c
180 FIND_FILE_STANDARD_INFO * pfindData =
181 =======
182 FIND_FILE_STANDARD_INFO *pfindData =
183 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
184 (FIND_FILE_STANDARD_INFO *)buf;
186 tmp_inode->i_mtime = cnvrtDosUnixTm(
187 le16_to_cpu(pfindData->LastWriteDate),
188 le16_to_cpu(pfindData->LastWriteTime));
189 tmp_inode->i_atime = cnvrtDosUnixTm(
190 le16_to_cpu(pfindData->LastAccessDate),
191 le16_to_cpu(pfindData->LastAccessTime));
192 tmp_inode->i_ctime = cnvrtDosUnixTm(
193 le16_to_cpu(pfindData->LastWriteDate),
194 le16_to_cpu(pfindData->LastWriteTime));
195 AdjustForTZ(cifs_sb->tcon, tmp_inode);
196 attr = le16_to_cpu(pfindData->Attributes);
197 allocation_size = le32_to_cpu(pfindData->AllocationSize);
198 end_of_file = le32_to_cpu(pfindData->DataSize);
201 /* Linux can not store file creation time unfortunately so ignore it */
203 cifsInfo->cifsAttrs = attr;
204 #ifdef CONFIG_CIFS_EXPERIMENTAL
205 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
206 /* get more accurate mode via ACL - so force inode refresh */
207 cifsInfo->time = 0;
208 } else
209 #endif /* CONFIG_CIFS_EXPERIMENTAL */
210 cifsInfo->time = jiffies;
212 /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
213 /* 2767 perms - indicate mandatory locking */
214 /* BB fill in uid and gid here? with help from winbind?
215 or retrieve from NTFS stream extended attribute */
216 if (atomic_read(&cifsInfo->inUse) == 0) {
217 tmp_inode->i_uid = cifs_sb->mnt_uid;
218 tmp_inode->i_gid = cifs_sb->mnt_gid;
219 /* set default mode. will override for dirs below */
220 tmp_inode->i_mode = cifs_sb->mnt_file_mode;
221 } else {
222 /* mask off the type bits since it gets set
223 below and we do not want to get two type
224 bits set */
225 tmp_inode->i_mode &= ~S_IFMT;
228 if (attr & ATTR_DIRECTORY) {
229 *pobject_type = DT_DIR;
230 /* override default perms since we do not lock dirs */
231 <<<<<<< HEAD:fs/cifs/readdir.c
232 if (atomic_read(&cifsInfo->inUse) == 0) {
233 =======
234 if (atomic_read(&cifsInfo->inUse) == 0)
235 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
236 tmp_inode->i_mode = cifs_sb->mnt_dir_mode;
237 <<<<<<< HEAD:fs/cifs/readdir.c
239 =======
240 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
241 tmp_inode->i_mode |= S_IFDIR;
242 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
243 (attr & ATTR_SYSTEM)) {
244 if (end_of_file == 0) {
245 *pobject_type = DT_FIFO;
246 tmp_inode->i_mode |= S_IFIFO;
247 } else {
248 /* rather than get the type here, we mark the
249 inode as needing revalidate and get the real type
250 (blk vs chr vs. symlink) later ie in lookup */
251 *pobject_type = DT_REG;
252 tmp_inode->i_mode |= S_IFREG;
253 cifsInfo->time = 0;
255 /* we no longer mark these because we could not follow them */
256 /* } else if (attr & ATTR_REPARSE) {
257 *pobject_type = DT_LNK;
258 tmp_inode->i_mode |= S_IFLNK; */
259 } else {
260 *pobject_type = DT_REG;
261 tmp_inode->i_mode |= S_IFREG;
262 if (attr & ATTR_READONLY)
263 tmp_inode->i_mode &= ~(S_IWUGO);
264 else if ((tmp_inode->i_mode & S_IWUGO) == 0)
265 /* the ATTR_READONLY flag may have been changed on */
266 /* server -- set any w bits allowed by mnt_file_mode */
267 tmp_inode->i_mode |= (S_IWUGO & cifs_sb->mnt_file_mode);
268 } /* could add code here - to validate if device or weird share type? */
270 /* can not fill in nlink here as in qpathinfo version and Unx search */
271 <<<<<<< HEAD:fs/cifs/readdir.c
272 if (atomic_read(&cifsInfo->inUse) == 0) {
273 =======
274 if (atomic_read(&cifsInfo->inUse) == 0)
275 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
276 atomic_set(&cifsInfo->inUse, 1);
277 <<<<<<< HEAD:fs/cifs/readdir.c
279 =======
280 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
282 spin_lock(&tmp_inode->i_lock);
283 if (is_size_safe_to_change(cifsInfo, end_of_file)) {
284 /* can not safely change the file size here if the
285 client is writing to it due to potential races */
286 i_size_write(tmp_inode, end_of_file);
288 /* 512 bytes (2**9) is the fake blocksize that must be used */
289 /* for this calculation, even though the reported blocksize is larger */
290 tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
292 spin_unlock(&tmp_inode->i_lock);
294 if (allocation_size < end_of_file)
295 cFYI(1, ("May be sparse file, allocation less than file size"));
296 cFYI(1, ("File Size %ld and blocks %llu",
297 (unsigned long)tmp_inode->i_size,
298 (unsigned long long)tmp_inode->i_blocks));
299 if (S_ISREG(tmp_inode->i_mode)) {
300 cFYI(1, ("File inode"));
301 tmp_inode->i_op = &cifs_file_inode_ops;
302 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
303 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
304 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
305 else
306 tmp_inode->i_fop = &cifs_file_direct_ops;
307 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
308 tmp_inode->i_fop = &cifs_file_nobrl_ops;
309 else
310 tmp_inode->i_fop = &cifs_file_ops;
312 if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
313 (cifs_sb->tcon->ses->server->maxBuf <
314 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
315 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
316 else
317 tmp_inode->i_data.a_ops = &cifs_addr_ops;
319 if (isNewInode)
320 return; /* No sense invalidating pages for new inode
321 since have not started caching readahead file
322 data yet */
324 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
325 (local_size == tmp_inode->i_size)) {
326 cFYI(1, ("inode exists but unchanged"));
327 } else {
328 /* file may have changed on server */
329 cFYI(1, ("invalidate inode, readdir detected change"));
330 invalidate_remote_inode(tmp_inode);
332 } else if (S_ISDIR(tmp_inode->i_mode)) {
333 cFYI(1, ("Directory inode"));
334 tmp_inode->i_op = &cifs_dir_inode_ops;
335 tmp_inode->i_fop = &cifs_dir_ops;
336 } else if (S_ISLNK(tmp_inode->i_mode)) {
337 cFYI(1, ("Symbolic Link inode"));
338 tmp_inode->i_op = &cifs_symlink_inode_ops;
339 } else {
340 cFYI(1, ("Init special inode"));
341 init_special_inode(tmp_inode, tmp_inode->i_mode,
342 tmp_inode->i_rdev);
346 static void unix_fill_in_inode(struct inode *tmp_inode,
347 FILE_UNIX_INFO *pfindData, unsigned int *pobject_type, int isNewInode)
349 loff_t local_size;
350 struct timespec local_mtime;
352 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
353 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
355 __u32 type = le32_to_cpu(pfindData->Type);
356 __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes);
357 __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
358 cifsInfo->time = jiffies;
359 atomic_inc(&cifsInfo->inUse);
361 /* save mtime and size */
362 local_mtime = tmp_inode->i_mtime;
363 local_size = tmp_inode->i_size;
365 tmp_inode->i_atime =
366 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
367 tmp_inode->i_mtime =
368 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime));
369 tmp_inode->i_ctime =
370 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange));
372 tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions);
373 /* since we set the inode type below we need to mask off type
374 to avoid strange results if bits above were corrupt */
375 tmp_inode->i_mode &= ~S_IFMT;
376 if (type == UNIX_FILE) {
377 *pobject_type = DT_REG;
378 tmp_inode->i_mode |= S_IFREG;
379 } else if (type == UNIX_SYMLINK) {
380 *pobject_type = DT_LNK;
381 tmp_inode->i_mode |= S_IFLNK;
382 } else if (type == UNIX_DIR) {
383 *pobject_type = DT_DIR;
384 tmp_inode->i_mode |= S_IFDIR;
385 } else if (type == UNIX_CHARDEV) {
386 *pobject_type = DT_CHR;
387 tmp_inode->i_mode |= S_IFCHR;
388 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
389 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
390 } else if (type == UNIX_BLOCKDEV) {
391 *pobject_type = DT_BLK;
392 tmp_inode->i_mode |= S_IFBLK;
393 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
394 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
395 } else if (type == UNIX_FIFO) {
396 *pobject_type = DT_FIFO;
397 tmp_inode->i_mode |= S_IFIFO;
398 } else if (type == UNIX_SOCKET) {
399 *pobject_type = DT_SOCK;
400 tmp_inode->i_mode |= S_IFSOCK;
401 } else {
402 /* safest to just call it a file */
403 *pobject_type = DT_REG;
404 tmp_inode->i_mode |= S_IFREG;
405 cFYI(1, ("unknown inode type %d", type));
408 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
409 tmp_inode->i_uid = cifs_sb->mnt_uid;
410 else
411 tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
412 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
413 tmp_inode->i_gid = cifs_sb->mnt_gid;
414 else
415 tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
416 tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
418 spin_lock(&tmp_inode->i_lock);
419 if (is_size_safe_to_change(cifsInfo, end_of_file)) {
420 /* can not safely change the file size here if the
421 client is writing to it due to potential races */
422 i_size_write(tmp_inode, end_of_file);
424 /* 512 bytes (2**9) is the fake blocksize that must be used */
425 /* for this calculation, not the real blocksize */
426 tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
428 spin_unlock(&tmp_inode->i_lock);
430 if (S_ISREG(tmp_inode->i_mode)) {
431 cFYI(1, ("File inode"));
432 tmp_inode->i_op = &cifs_file_inode_ops;
434 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
435 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
436 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
437 else
438 tmp_inode->i_fop = &cifs_file_direct_ops;
439 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
440 tmp_inode->i_fop = &cifs_file_nobrl_ops;
441 else
442 tmp_inode->i_fop = &cifs_file_ops;
444 if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
445 (cifs_sb->tcon->ses->server->maxBuf <
446 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
447 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
448 else
449 tmp_inode->i_data.a_ops = &cifs_addr_ops;
451 if (isNewInode)
452 return; /* No sense invalidating pages for new inode
453 since we have not started caching readahead
454 file data for it yet */
456 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
457 (local_size == tmp_inode->i_size)) {
458 cFYI(1, ("inode exists but unchanged"));
459 } else {
460 /* file may have changed on server */
461 cFYI(1, ("invalidate inode, readdir detected change"));
462 invalidate_remote_inode(tmp_inode);
464 } else if (S_ISDIR(tmp_inode->i_mode)) {
465 cFYI(1, ("Directory inode"));
466 tmp_inode->i_op = &cifs_dir_inode_ops;
467 tmp_inode->i_fop = &cifs_dir_ops;
468 } else if (S_ISLNK(tmp_inode->i_mode)) {
469 cFYI(1, ("Symbolic Link inode"));
470 tmp_inode->i_op = &cifs_symlink_inode_ops;
471 /* tmp_inode->i_fop = *//* do not need to set to anything */
472 } else {
473 cFYI(1, ("Special inode"));
474 init_special_inode(tmp_inode, tmp_inode->i_mode,
475 tmp_inode->i_rdev);
479 static int initiate_cifs_search(const int xid, struct file *file)
481 int rc = 0;
482 char *full_path;
483 struct cifsFileInfo *cifsFile;
484 struct cifs_sb_info *cifs_sb;
485 struct cifsTconInfo *pTcon;
487 if (file->private_data == NULL) {
488 file->private_data =
489 kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
492 if (file->private_data == NULL)
493 return -ENOMEM;
494 cifsFile = file->private_data;
495 cifsFile->invalidHandle = TRUE;
496 cifsFile->srch_inf.endOfSearch = FALSE;
498 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
499 if (cifs_sb == NULL)
500 return -EINVAL;
502 pTcon = cifs_sb->tcon;
503 if (pTcon == NULL)
504 return -EINVAL;
506 full_path = build_path_from_dentry(file->f_path.dentry);
508 <<<<<<< HEAD:fs/cifs/readdir.c
509 if (full_path == NULL) {
510 =======
511 if (full_path == NULL)
512 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
513 return -ENOMEM;
514 <<<<<<< HEAD:fs/cifs/readdir.c
516 =======
517 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
519 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
521 ffirst_retry:
522 /* test for Unix extensions */
523 /* but now check for them on the share/mount not on the SMB session */
524 /* if (pTcon->ses->capabilities & CAP_UNIX) { */
525 <<<<<<< HEAD:fs/cifs/readdir.c
526 if (pTcon->unix_ext) {
527 =======
528 if (pTcon->unix_ext)
529 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
530 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
531 <<<<<<< HEAD:fs/cifs/readdir.c
532 } else if ((pTcon->ses->capabilities &
533 =======
534 else if ((pTcon->ses->capabilities &
535 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
536 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
537 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
538 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
539 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
540 } else /* not srvinos - BB fixme add check for backlevel? */ {
541 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
544 rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
545 &cifsFile->netfid, &cifsFile->srch_inf,
546 cifs_sb->mnt_cifs_flags &
547 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
548 if (rc == 0)
549 cifsFile->invalidHandle = FALSE;
550 if ((rc == -EOPNOTSUPP) &&
551 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
552 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
553 goto ffirst_retry;
555 kfree(full_path);
556 return rc;
559 /* return length of unicode string in bytes */
560 static int cifs_unicode_bytelen(char *str)
562 int len;
563 __le16 *ustr = (__le16 *)str;
565 for (len = 0; len <= PATH_MAX; len++) {
566 if (ustr[len] == 0)
567 return len << 1;
569 cFYI(1, ("Unicode string longer than PATH_MAX found"));
570 return len << 1;
573 static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
575 char *new_entry;
576 <<<<<<< HEAD:fs/cifs/readdir.c
577 FILE_DIRECTORY_INFO * pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
578 =======
579 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
580 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
582 if (level == SMB_FIND_FILE_INFO_STANDARD) {
583 <<<<<<< HEAD:fs/cifs/readdir.c
584 FIND_FILE_STANDARD_INFO * pfData;
585 =======
586 FIND_FILE_STANDARD_INFO *pfData;
587 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
588 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
590 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
591 pfData->FileNameLength;
592 } else
593 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
594 cFYI(1, ("new entry %p old entry %p", new_entry, old_entry));
595 /* validate that new_entry is not past end of SMB */
596 if (new_entry >= end_of_smb) {
597 cERROR(1,
598 ("search entry %p began after end of SMB %p old entry %p",
599 new_entry, end_of_smb, old_entry));
600 return NULL;
601 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
602 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
603 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
604 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
605 cERROR(1, ("search entry %p extends after end of SMB %p",
606 new_entry, end_of_smb));
607 return NULL;
608 } else
609 return new_entry;
613 #define UNICODE_DOT cpu_to_le16(0x2e)
615 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
616 static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
618 int rc = 0;
619 char *filename = NULL;
620 int len = 0;
622 if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
623 <<<<<<< HEAD:fs/cifs/readdir.c
624 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
625 =======
626 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
627 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
628 filename = &pFindData->FileName[0];
629 if (cfile->srch_inf.unicode) {
630 len = cifs_unicode_bytelen(filename);
631 } else {
632 /* BB should we make this strnlen of PATH_MAX? */
633 len = strnlen(filename, 5);
635 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
636 <<<<<<< HEAD:fs/cifs/readdir.c
637 FILE_DIRECTORY_INFO * pFindData =
638 =======
639 FILE_DIRECTORY_INFO *pFindData =
640 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
641 (FILE_DIRECTORY_INFO *)current_entry;
642 filename = &pFindData->FileName[0];
643 len = le32_to_cpu(pFindData->FileNameLength);
644 } else if (cfile->srch_inf.info_level ==
645 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
646 <<<<<<< HEAD:fs/cifs/readdir.c
647 FILE_FULL_DIRECTORY_INFO * pFindData =
648 =======
649 FILE_FULL_DIRECTORY_INFO *pFindData =
650 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
651 (FILE_FULL_DIRECTORY_INFO *)current_entry;
652 filename = &pFindData->FileName[0];
653 len = le32_to_cpu(pFindData->FileNameLength);
654 } else if (cfile->srch_inf.info_level ==
655 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
656 <<<<<<< HEAD:fs/cifs/readdir.c
657 SEARCH_ID_FULL_DIR_INFO * pFindData =
658 =======
659 SEARCH_ID_FULL_DIR_INFO *pFindData =
660 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
661 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
662 filename = &pFindData->FileName[0];
663 len = le32_to_cpu(pFindData->FileNameLength);
664 } else if (cfile->srch_inf.info_level ==
665 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
666 <<<<<<< HEAD:fs/cifs/readdir.c
667 FILE_BOTH_DIRECTORY_INFO * pFindData =
668 =======
669 FILE_BOTH_DIRECTORY_INFO *pFindData =
670 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
671 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
672 filename = &pFindData->FileName[0];
673 len = le32_to_cpu(pFindData->FileNameLength);
674 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
675 <<<<<<< HEAD:fs/cifs/readdir.c
676 FIND_FILE_STANDARD_INFO * pFindData =
677 =======
678 FIND_FILE_STANDARD_INFO *pFindData =
679 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
680 (FIND_FILE_STANDARD_INFO *)current_entry;
681 filename = &pFindData->FileName[0];
682 len = pFindData->FileNameLength;
683 } else {
684 cFYI(1, ("Unknown findfirst level %d",
685 cfile->srch_inf.info_level));
688 if (filename) {
689 if (cfile->srch_inf.unicode) {
690 __le16 *ufilename = (__le16 *)filename;
691 if (len == 2) {
692 /* check for . */
693 if (ufilename[0] == UNICODE_DOT)
694 rc = 1;
695 } else if (len == 4) {
696 /* check for .. */
697 if ((ufilename[0] == UNICODE_DOT)
698 && (ufilename[1] == UNICODE_DOT))
699 rc = 2;
701 } else /* ASCII */ {
702 if (len == 1) {
703 if (filename[0] == '.')
704 rc = 1;
705 } else if (len == 2) {
706 if ((filename[0] == '.') && (filename[1] == '.'))
707 rc = 2;
712 return rc;
715 /* Check if directory that we are searching has changed so we can decide
716 whether we can use the cached search results from the previous search */
717 static int is_dir_changed(struct file *file)
719 struct inode *inode = file->f_path.dentry->d_inode;
720 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
722 if (cifsInfo->time == 0)
723 return 1; /* directory was changed, perhaps due to unlink */
724 else
725 return 0;
729 /* find the corresponding entry in the search */
730 /* Note that the SMB server returns search entries for . and .. which
731 complicates logic here if we choose to parse for them and we do not
732 assume that they are located in the findfirst return buffer.*/
733 /* We start counting in the buffer with entry 2 and increment for every
734 entry (do not increment for . or .. entry) */
735 static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
736 struct file *file, char **ppCurrentEntry, int *num_to_ret)
738 int rc = 0;
739 int pos_in_buf = 0;
740 loff_t first_entry_in_buffer;
741 loff_t index_to_find = file->f_pos;
742 struct cifsFileInfo *cifsFile = file->private_data;
743 /* check if index in the buffer */
745 if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
746 (num_to_ret == NULL))
747 return -ENOENT;
749 *ppCurrentEntry = NULL;
750 first_entry_in_buffer =
751 cifsFile->srch_inf.index_of_last_entry -
752 cifsFile->srch_inf.entries_in_buffer;
754 /* if first entry in buf is zero then is first buffer
755 in search response data which means it is likely . and ..
756 will be in this buffer, although some servers do not return
757 . and .. for the root of a drive and for those we need
758 to start two entries earlier */
760 <<<<<<< HEAD:fs/cifs/readdir.c
761 #ifdef CONFIG_CIFS_DEBUG2
762 =======
763 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
764 dump_cifs_file_struct(file, "In fce ");
765 <<<<<<< HEAD:fs/cifs/readdir.c
766 #endif
767 =======
768 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
769 if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
770 is_dir_changed(file)) ||
771 (index_to_find < first_entry_in_buffer)) {
772 /* close and restart search */
773 cFYI(1, ("search backing up - close and restart search"));
774 cifsFile->invalidHandle = TRUE;
775 CIFSFindClose(xid, pTcon, cifsFile->netfid);
776 kfree(cifsFile->search_resume_name);
777 cifsFile->search_resume_name = NULL;
778 if (cifsFile->srch_inf.ntwrk_buf_start) {
779 cFYI(1, ("freeing SMB ff cache buf on search rewind"));
780 if (cifsFile->srch_inf.smallBuf)
781 cifs_small_buf_release(cifsFile->srch_inf.
782 ntwrk_buf_start);
783 else
784 cifs_buf_release(cifsFile->srch_inf.
785 ntwrk_buf_start);
787 rc = initiate_cifs_search(xid, file);
788 if (rc) {
789 cFYI(1, ("error %d reinitiating a search on rewind",
790 rc));
791 return rc;
795 while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
796 (rc == 0) && (cifsFile->srch_inf.endOfSearch == FALSE)) {
797 cFYI(1, ("calling findnext2"));
798 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
799 &cifsFile->srch_inf);
800 if (rc)
801 return -ENOENT;
803 if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
804 /* we found the buffer that contains the entry */
805 /* scan and find it */
806 int i;
807 char *current_entry;
808 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
809 smbCalcSize((struct smb_hdr *)
810 cifsFile->srch_inf.ntwrk_buf_start);
812 current_entry = cifsFile->srch_inf.srch_entries_start;
813 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
814 - cifsFile->srch_inf.entries_in_buffer;
815 pos_in_buf = index_to_find - first_entry_in_buffer;
816 cFYI(1, ("found entry - pos_in_buf %d", pos_in_buf));
818 <<<<<<< HEAD:fs/cifs/readdir.c
819 for (i=0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
820 =======
821 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
822 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
823 /* go entry by entry figuring out which is first */
824 current_entry = nxt_dir_entry(current_entry, end_of_smb,
825 cifsFile->srch_inf.info_level);
827 if ((current_entry == NULL) && (i < pos_in_buf)) {
828 /* BB fixme - check if we should flag this error */
829 cERROR(1, ("reached end of buf searching for pos in buf"
830 " %d index to find %lld rc %d",
831 pos_in_buf, index_to_find, rc));
833 rc = 0;
834 *ppCurrentEntry = current_entry;
835 } else {
836 cFYI(1, ("index not in buffer - could not findnext into it"));
837 return 0;
840 if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
841 cFYI(1, ("can not return entries pos_in_buf beyond last"));
842 *num_to_ret = 0;
843 } else
844 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
846 return rc;
849 /* inode num, inode type and filename returned */
850 static int cifs_get_name_from_search_buf(struct qstr *pqst,
851 char *current_entry, __u16 level, unsigned int unicode,
852 struct cifs_sb_info *cifs_sb, int max_len, ino_t *pinum)
854 int rc = 0;
855 unsigned int len = 0;
856 char *filename;
857 struct nls_table *nlt = cifs_sb->local_nls;
859 *pinum = 0;
861 if (level == SMB_FIND_FILE_UNIX) {
862 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
864 filename = &pFindData->FileName[0];
865 if (unicode) {
866 len = cifs_unicode_bytelen(filename);
867 } else {
868 /* BB should we make this strnlen of PATH_MAX? */
869 len = strnlen(filename, PATH_MAX);
872 /* BB fixme - hash low and high 32 bits if not 64 bit arch BB */
873 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
874 *pinum = pFindData->UniqueId;
875 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
876 FILE_DIRECTORY_INFO *pFindData =
877 (FILE_DIRECTORY_INFO *)current_entry;
878 filename = &pFindData->FileName[0];
879 len = le32_to_cpu(pFindData->FileNameLength);
880 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
881 FILE_FULL_DIRECTORY_INFO *pFindData =
882 (FILE_FULL_DIRECTORY_INFO *)current_entry;
883 filename = &pFindData->FileName[0];
884 len = le32_to_cpu(pFindData->FileNameLength);
885 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
886 SEARCH_ID_FULL_DIR_INFO *pFindData =
887 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
888 filename = &pFindData->FileName[0];
889 len = le32_to_cpu(pFindData->FileNameLength);
890 *pinum = pFindData->UniqueId;
891 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
892 FILE_BOTH_DIRECTORY_INFO *pFindData =
893 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
894 filename = &pFindData->FileName[0];
895 len = le32_to_cpu(pFindData->FileNameLength);
896 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
897 <<<<<<< HEAD:fs/cifs/readdir.c
898 FIND_FILE_STANDARD_INFO * pFindData =
899 =======
900 FIND_FILE_STANDARD_INFO *pFindData =
901 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
902 (FIND_FILE_STANDARD_INFO *)current_entry;
903 filename = &pFindData->FileName[0];
904 /* one byte length, no name conversion */
905 len = (unsigned int)pFindData->FileNameLength;
906 } else {
907 cFYI(1, ("Unknown findfirst level %d", level));
908 return -EINVAL;
911 if (len > max_len) {
912 cERROR(1, ("bad search response length %d past smb end", len));
913 return -EINVAL;
916 if (unicode) {
917 /* BB fixme - test with long names */
918 /* Note converted filename can be longer than in unicode */
919 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
920 pqst->len = cifs_convertUCSpath((char *)pqst->name,
921 (__le16 *)filename, len/2, nlt);
922 else
923 pqst->len = cifs_strfromUCS_le((char *)pqst->name,
924 (__le16 *)filename, len/2, nlt);
925 } else {
926 pqst->name = filename;
927 pqst->len = len;
929 pqst->hash = full_name_hash(pqst->name, pqst->len);
930 /* cFYI(1, ("filldir on %s",pqst->name)); */
931 return rc;
934 static int cifs_filldir(char *pfindEntry, struct file *file,
935 filldir_t filldir, void *direntry, char *scratch_buf, int max_len)
937 int rc = 0;
938 struct qstr qstring;
939 struct cifsFileInfo *pCifsF;
940 unsigned int obj_type;
941 ino_t inum;
942 struct cifs_sb_info *cifs_sb;
943 struct inode *tmp_inode;
944 struct dentry *tmp_dentry;
946 /* get filename and len into qstring */
947 /* get dentry */
948 /* decide whether to create and populate ionde */
949 if ((direntry == NULL) || (file == NULL))
950 return -EINVAL;
952 pCifsF = file->private_data;
954 if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
955 return -ENOENT;
957 rc = cifs_entry_is_dot(pfindEntry, pCifsF);
958 /* skip . and .. since we added them first */
959 if (rc != 0)
960 return 0;
962 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
964 qstring.name = scratch_buf;
965 rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
966 pCifsF->srch_inf.info_level,
967 pCifsF->srch_inf.unicode, cifs_sb,
968 max_len,
969 &inum /* returned */);
971 if (rc)
972 return rc;
974 rc = construct_dentry(&qstring, file, &tmp_inode, &tmp_dentry);
975 if ((tmp_inode == NULL) || (tmp_dentry == NULL))
976 return -ENOMEM;
978 if (rc) {
979 /* inode created, we need to hash it with right inode number */
980 if (inum != 0) {
981 /* BB fixme - hash the 2 32 quantities bits together if
982 * necessary BB */
983 tmp_inode->i_ino = inum;
985 insert_inode_hash(tmp_inode);
988 /* we pass in rc below, indicating whether it is a new inode,
989 so we can figure out whether to invalidate the inode cached
990 data if the file has changed */
991 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
992 unix_fill_in_inode(tmp_inode,
993 (FILE_UNIX_INFO *)pfindEntry,
994 &obj_type, rc);
995 else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
996 fill_in_inode(tmp_inode, 0 /* old level 1 buffer type */,
997 pfindEntry, &obj_type, rc);
998 else
999 fill_in_inode(tmp_inode, 1 /* NT */, pfindEntry, &obj_type, rc);
1001 if (rc) /* new inode - needs to be tied to dentry */ {
1002 d_instantiate(tmp_dentry, tmp_inode);
1003 if (rc == 2)
1004 d_rehash(tmp_dentry);
1008 rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
1009 tmp_inode->i_ino, obj_type);
1010 if (rc) {
1011 cFYI(1, ("filldir rc = %d", rc));
1012 /* we can not return filldir errors to the caller
1013 since they are "normal" when the stat blocksize
1014 is too small - we return remapped error instead */
1015 rc = -EOVERFLOW;
1018 dput(tmp_dentry);
1019 return rc;
1022 static int cifs_save_resume_key(const char *current_entry,
1023 struct cifsFileInfo *cifsFile)
1025 int rc = 0;
1026 unsigned int len = 0;
1027 __u16 level;
1028 char *filename;
1030 if ((cifsFile == NULL) || (current_entry == NULL))
1031 return -EINVAL;
1033 level = cifsFile->srch_inf.info_level;
1035 if (level == SMB_FIND_FILE_UNIX) {
1036 <<<<<<< HEAD:fs/cifs/readdir.c
1037 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
1038 =======
1039 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
1040 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/cifs/readdir.c
1042 filename = &pFindData->FileName[0];
1043 if (cifsFile->srch_inf.unicode) {
1044 len = cifs_unicode_bytelen(filename);
1045 } else {
1046 /* BB should we make this strnlen of PATH_MAX? */
1047 len = strnlen(filename, PATH_MAX);
1049 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
1050 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
1051 FILE_DIRECTORY_INFO *pFindData =
1052 (FILE_DIRECTORY_INFO *)current_entry;
1053 filename = &pFindData->FileName[0];
1054 len = le32_to_cpu(pFindData->FileNameLength);
1055 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
1056 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
1057 FILE_FULL_DIRECTORY_INFO *pFindData =
1058 (FILE_FULL_DIRECTORY_INFO *)current_entry;
1059 filename = &pFindData->FileName[0];
1060 len = le32_to_cpu(pFindData->FileNameLength);
1061 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
1062 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
1063 SEARCH_ID_FULL_DIR_INFO *pFindData =
1064 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
1065 filename = &pFindData->FileName[0];
1066 len = le32_to_cpu(pFindData->FileNameLength);
1067 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
1068 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
1069 FILE_BOTH_DIRECTORY_INFO *pFindData =
1070 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
1071 filename = &pFindData->FileName[0];
1072 len = le32_to_cpu(pFindData->FileNameLength);
1073 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
1074 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
1075 FIND_FILE_STANDARD_INFO *pFindData =
1076 (FIND_FILE_STANDARD_INFO *)current_entry;
1077 filename = &pFindData->FileName[0];
1078 /* one byte length, no name conversion */
1079 len = (unsigned int)pFindData->FileNameLength;
1080 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
1081 } else {
1082 cFYI(1, ("Unknown findfirst level %d", level));
1083 return -EINVAL;
1085 cifsFile->srch_inf.resume_name_len = len;
1086 cifsFile->srch_inf.presume_name = filename;
1087 return rc;
1090 int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
1092 int rc = 0;
1093 int xid, i;
1094 struct cifs_sb_info *cifs_sb;
1095 struct cifsTconInfo *pTcon;
1096 struct cifsFileInfo *cifsFile = NULL;
1097 char *current_entry;
1098 int num_to_fill = 0;
1099 char *tmp_buf = NULL;
1100 char *end_of_smb;
1101 int max_len;
1103 xid = GetXid();
1105 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
1106 pTcon = cifs_sb->tcon;
1107 if (pTcon == NULL)
1108 return -EINVAL;
1110 switch ((int) file->f_pos) {
1111 case 0:
1112 if (filldir(direntry, ".", 1, file->f_pos,
1113 file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
1114 cERROR(1, ("Filldir for current dir failed"));
1115 rc = -ENOMEM;
1116 break;
1118 file->f_pos++;
1119 case 1:
1120 if (filldir(direntry, "..", 2, file->f_pos,
1121 file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
1122 cERROR(1, ("Filldir for parent dir failed"));
1123 rc = -ENOMEM;
1124 break;
1126 file->f_pos++;
1127 default:
1128 /* 1) If search is active,
1129 is in current search buffer?
1130 if it before then restart search
1131 if after then keep searching till find it */
1133 if (file->private_data == NULL) {
1134 rc = initiate_cifs_search(xid, file);
1135 cFYI(1, ("initiate cifs search rc %d", rc));
1136 if (rc) {
1137 FreeXid(xid);
1138 return rc;
1141 if (file->private_data == NULL) {
1142 rc = -EINVAL;
1143 FreeXid(xid);
1144 return rc;
1146 cifsFile = file->private_data;
1147 if (cifsFile->srch_inf.endOfSearch) {
1148 if (cifsFile->srch_inf.emptyDir) {
1149 cFYI(1, ("End of search, empty dir"));
1150 rc = 0;
1151 break;
1153 } /* else {
1154 cifsFile->invalidHandle = TRUE;
1155 CIFSFindClose(xid, pTcon, cifsFile->netfid);
1157 kfree(cifsFile->search_resume_name);
1158 cifsFile->search_resume_name = NULL; */
1160 rc = find_cifs_entry(xid, pTcon, file,
1161 &current_entry, &num_to_fill);
1162 if (rc) {
1163 cFYI(1, ("fce error %d", rc));
1164 goto rddir2_exit;
1165 } else if (current_entry != NULL) {
1166 cFYI(1, ("entry %lld found", file->f_pos));
1167 } else {
1168 cFYI(1, ("could not find entry"));
1169 goto rddir2_exit;
1171 cFYI(1, ("loop through %d times filling dir for net buf %p",
1172 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start));
1173 max_len = smbCalcSize((struct smb_hdr *)
1174 cifsFile->srch_inf.ntwrk_buf_start);
1175 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1177 /* To be safe - for UCS to UTF-8 with strings loaded
1178 with the rare long characters alloc more to account for
1179 such multibyte target UTF-8 characters. cifs_unicode.c,
1180 which actually does the conversion, has the same limit */
1181 tmp_buf = kmalloc((2 * NAME_MAX) + 4, GFP_KERNEL);
1182 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
1183 if (current_entry == NULL) {
1184 /* evaluate whether this case is an error */
1185 cERROR(1, ("past SMB end, num to fill %d i %d",
1186 num_to_fill, i));
1187 break;
1189 /* if buggy server returns . and .. late do
1190 we want to check for that here? */
1191 rc = cifs_filldir(current_entry, file,
1192 filldir, direntry, tmp_buf, max_len);
1193 if (rc == -EOVERFLOW) {
1194 rc = 0;
1195 break;
1198 file->f_pos++;
1199 if (file->f_pos ==
1200 cifsFile->srch_inf.index_of_last_entry) {
1201 cFYI(1, ("last entry in buf at pos %lld %s",
1202 file->f_pos, tmp_buf));
1203 cifs_save_resume_key(current_entry, cifsFile);
1204 break;
1205 } else
1206 current_entry =
1207 nxt_dir_entry(current_entry, end_of_smb,
1208 cifsFile->srch_inf.info_level);
1210 kfree(tmp_buf);
1211 break;
1212 } /* end switch */
1214 rddir2_exit:
1215 FreeXid(xid);
1216 return rc;