cifs: eliminate pfile pointer from cifsFileInfo
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / cifs / readdir.c
blob6f3d13ff947086fb0ad35114c1c601cc1b0d8c8c
1 /*
2 * fs/cifs/readdir.c
4 * Directory search handling
6 * Copyright (C) International Business Machines Corp., 2004, 2008
7 * Author(s): Steve French (sfrench@us.ibm.com)
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.
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.
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
23 #include <linux/fs.h>
24 #include <linux/pagemap.h>
25 #include <linux/slab.h>
26 #include <linux/stat.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"
36 * To be safe - for UCS to UTF-8 with strings loaded with the rare long
37 * characters alloc more to account for such multibyte target UTF-8
38 * characters.
40 #define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
42 #ifdef CONFIG_CIFS_DEBUG2
43 static void dump_cifs_file_struct(struct file *file, char *label)
45 struct cifsFileInfo *cf;
47 if (file) {
48 cf = file->private_data;
49 if (cf == NULL) {
50 cFYI(1, "empty cifs private file data");
51 return;
53 if (cf->invalidHandle)
54 cFYI(1, "invalid handle");
55 if (cf->srch_inf.endOfSearch)
56 cFYI(1, "end of search");
57 if (cf->srch_inf.emptyDir)
58 cFYI(1, "empty dir");
61 #else
62 static inline void dump_cifs_file_struct(struct file *file, char *label)
65 #endif /* DEBUG2 */
68 * Find the dentry that matches "name". If there isn't one, create one. If it's
69 * a negative dentry or the uniqueid changed, then drop it and recreate it.
71 static struct dentry *
72 cifs_readdir_lookup(struct dentry *parent, struct qstr *name,
73 struct cifs_fattr *fattr)
75 struct dentry *dentry, *alias;
76 struct inode *inode;
77 struct super_block *sb = parent->d_inode->i_sb;
79 cFYI(1, "For %s", name->name);
81 if (parent->d_op && parent->d_op->d_hash)
82 parent->d_op->d_hash(parent, name);
83 else
84 name->hash = full_name_hash(name->name, name->len);
86 dentry = d_lookup(parent, name);
87 if (dentry) {
88 /* FIXME: check for inode number changes? */
89 if (dentry->d_inode != NULL)
90 return dentry;
91 d_drop(dentry);
92 dput(dentry);
95 dentry = d_alloc(parent, name);
96 if (dentry == NULL)
97 return NULL;
99 inode = cifs_iget(sb, fattr);
100 if (!inode) {
101 dput(dentry);
102 return NULL;
105 if (cifs_sb_master_tcon(CIFS_SB(sb))->nocase)
106 dentry->d_op = &cifs_ci_dentry_ops;
107 else
108 dentry->d_op = &cifs_dentry_ops;
110 alias = d_materialise_unique(dentry, inode);
111 if (alias != NULL) {
112 dput(dentry);
113 if (IS_ERR(alias))
114 return NULL;
115 dentry = alias;
118 return dentry;
121 static void
122 cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
124 fattr->cf_uid = cifs_sb->mnt_uid;
125 fattr->cf_gid = cifs_sb->mnt_gid;
127 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
128 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
129 fattr->cf_dtype = DT_DIR;
130 } else {
131 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
132 fattr->cf_dtype = DT_REG;
135 if (fattr->cf_cifsattrs & ATTR_READONLY)
136 fattr->cf_mode &= ~S_IWUGO;
138 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
139 fattr->cf_cifsattrs & ATTR_SYSTEM) {
140 if (fattr->cf_eof == 0) {
141 fattr->cf_mode &= ~S_IFMT;
142 fattr->cf_mode |= S_IFIFO;
143 fattr->cf_dtype = DT_FIFO;
144 } else {
146 * trying to get the type and mode via SFU can be slow,
147 * so just call those regular files for now, and mark
148 * for reval
150 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
155 static void
156 cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info,
157 struct cifs_sb_info *cifs_sb)
159 memset(fattr, 0, sizeof(*fattr));
160 fattr->cf_cifsattrs = le32_to_cpu(info->ExtFileAttributes);
161 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
162 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
163 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
164 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
165 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
167 cifs_fill_common_info(fattr, cifs_sb);
170 static void
171 cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info,
172 struct cifs_sb_info *cifs_sb)
174 int offset = cifs_sb_master_tcon(cifs_sb)->ses->server->timeAdj;
176 memset(fattr, 0, sizeof(*fattr));
177 fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate,
178 info->LastAccessTime, offset);
179 fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate,
180 info->LastWriteTime, offset);
181 fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate,
182 info->LastWriteTime, offset);
184 fattr->cf_cifsattrs = le16_to_cpu(info->Attributes);
185 fattr->cf_bytes = le32_to_cpu(info->AllocationSize);
186 fattr->cf_eof = le32_to_cpu(info->DataSize);
188 cifs_fill_common_info(fattr, cifs_sb);
191 /* BB eventually need to add the following helper function to
192 resolve NT_STATUS_STOPPED_ON_SYMLINK return code when
193 we try to do FindFirst on (NTFS) directory symlinks */
195 int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
196 int xid)
198 __u16 fid;
199 int len;
200 int oplock = 0;
201 int rc;
202 struct cifsTconInfo *ptcon = cifs_sb_tcon(cifs_sb);
203 char *tmpbuffer;
205 rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ,
206 OPEN_REPARSE_POINT, &fid, &oplock, NULL,
207 cifs_sb->local_nls,
208 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
209 if (!rc) {
210 tmpbuffer = kmalloc(maxpath);
211 rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path,
212 tmpbuffer,
213 maxpath -1,
214 fid,
215 cifs_sb->local_nls);
216 if (CIFSSMBClose(xid, ptcon, fid)) {
217 cFYI(1, "Error closing temporary reparsepoint open");
223 static int initiate_cifs_search(const int xid, struct file *file)
225 int rc = 0;
226 char *full_path = NULL;
227 struct cifsFileInfo *cifsFile;
228 struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
229 struct tcon_link *tlink;
230 struct cifsTconInfo *pTcon;
232 tlink = cifs_sb_tlink(cifs_sb);
233 if (IS_ERR(tlink))
234 return PTR_ERR(tlink);
235 pTcon = tlink_tcon(tlink);
237 if (file->private_data == NULL)
238 file->private_data =
239 kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
240 if (file->private_data == NULL) {
241 rc = -ENOMEM;
242 goto error_exit;
245 cifsFile = file->private_data;
246 cifsFile->invalidHandle = true;
247 cifsFile->srch_inf.endOfSearch = false;
248 cifsFile->tlink = cifs_get_tlink(tlink);
250 full_path = build_path_from_dentry(file->f_path.dentry);
251 if (full_path == NULL) {
252 rc = -ENOMEM;
253 goto error_exit;
256 cFYI(1, "Full path: %s start at: %lld", full_path, file->f_pos);
258 ffirst_retry:
259 /* test for Unix extensions */
260 /* but now check for them on the share/mount not on the SMB session */
261 /* if (pTcon->ses->capabilities & CAP_UNIX) { */
262 if (pTcon->unix_ext)
263 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
264 else if ((pTcon->ses->capabilities &
265 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
266 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
267 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
268 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
269 } else /* not srvinos - BB fixme add check for backlevel? */ {
270 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
273 rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
274 &cifsFile->netfid, &cifsFile->srch_inf,
275 cifs_sb->mnt_cifs_flags &
276 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
277 if (rc == 0)
278 cifsFile->invalidHandle = false;
279 /* BB add following call to handle readdir on new NTFS symlink errors
280 else if STATUS_STOPPED_ON_SYMLINK
281 call get_symlink_reparse_path and retry with new path */
282 else if ((rc == -EOPNOTSUPP) &&
283 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
284 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
285 goto ffirst_retry;
287 error_exit:
288 kfree(full_path);
289 cifs_put_tlink(tlink);
290 return rc;
293 /* return length of unicode string in bytes */
294 static int cifs_unicode_bytelen(char *str)
296 int len;
297 __le16 *ustr = (__le16 *)str;
299 for (len = 0; len <= PATH_MAX; len++) {
300 if (ustr[len] == 0)
301 return len << 1;
303 cFYI(1, "Unicode string longer than PATH_MAX found");
304 return len << 1;
307 static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
309 char *new_entry;
310 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
312 if (level == SMB_FIND_FILE_INFO_STANDARD) {
313 FIND_FILE_STANDARD_INFO *pfData;
314 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
316 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
317 pfData->FileNameLength;
318 } else
319 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
320 cFYI(1, "new entry %p old entry %p", new_entry, old_entry);
321 /* validate that new_entry is not past end of SMB */
322 if (new_entry >= end_of_smb) {
323 cERROR(1, "search entry %p began after end of SMB %p old entry %p",
324 new_entry, end_of_smb, old_entry);
325 return NULL;
326 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
327 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
328 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
329 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
330 cERROR(1, "search entry %p extends after end of SMB %p",
331 new_entry, end_of_smb);
332 return NULL;
333 } else
334 return new_entry;
338 #define UNICODE_DOT cpu_to_le16(0x2e)
340 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
341 static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
343 int rc = 0;
344 char *filename = NULL;
345 int len = 0;
347 if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
348 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
349 filename = &pFindData->FileName[0];
350 if (cfile->srch_inf.unicode) {
351 len = cifs_unicode_bytelen(filename);
352 } else {
353 /* BB should we make this strnlen of PATH_MAX? */
354 len = strnlen(filename, 5);
356 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
357 FILE_DIRECTORY_INFO *pFindData =
358 (FILE_DIRECTORY_INFO *)current_entry;
359 filename = &pFindData->FileName[0];
360 len = le32_to_cpu(pFindData->FileNameLength);
361 } else if (cfile->srch_inf.info_level ==
362 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
363 FILE_FULL_DIRECTORY_INFO *pFindData =
364 (FILE_FULL_DIRECTORY_INFO *)current_entry;
365 filename = &pFindData->FileName[0];
366 len = le32_to_cpu(pFindData->FileNameLength);
367 } else if (cfile->srch_inf.info_level ==
368 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
369 SEARCH_ID_FULL_DIR_INFO *pFindData =
370 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
371 filename = &pFindData->FileName[0];
372 len = le32_to_cpu(pFindData->FileNameLength);
373 } else if (cfile->srch_inf.info_level ==
374 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
375 FILE_BOTH_DIRECTORY_INFO *pFindData =
376 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
377 filename = &pFindData->FileName[0];
378 len = le32_to_cpu(pFindData->FileNameLength);
379 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
380 FIND_FILE_STANDARD_INFO *pFindData =
381 (FIND_FILE_STANDARD_INFO *)current_entry;
382 filename = &pFindData->FileName[0];
383 len = pFindData->FileNameLength;
384 } else {
385 cFYI(1, "Unknown findfirst level %d",
386 cfile->srch_inf.info_level);
389 if (filename) {
390 if (cfile->srch_inf.unicode) {
391 __le16 *ufilename = (__le16 *)filename;
392 if (len == 2) {
393 /* check for . */
394 if (ufilename[0] == UNICODE_DOT)
395 rc = 1;
396 } else if (len == 4) {
397 /* check for .. */
398 if ((ufilename[0] == UNICODE_DOT)
399 && (ufilename[1] == UNICODE_DOT))
400 rc = 2;
402 } else /* ASCII */ {
403 if (len == 1) {
404 if (filename[0] == '.')
405 rc = 1;
406 } else if (len == 2) {
407 if ((filename[0] == '.') && (filename[1] == '.'))
408 rc = 2;
413 return rc;
416 /* Check if directory that we are searching has changed so we can decide
417 whether we can use the cached search results from the previous search */
418 static int is_dir_changed(struct file *file)
420 struct inode *inode = file->f_path.dentry->d_inode;
421 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
423 if (cifsInfo->time == 0)
424 return 1; /* directory was changed, perhaps due to unlink */
425 else
426 return 0;
430 static int cifs_save_resume_key(const char *current_entry,
431 struct cifsFileInfo *cifsFile)
433 int rc = 0;
434 unsigned int len = 0;
435 __u16 level;
436 char *filename;
438 if ((cifsFile == NULL) || (current_entry == NULL))
439 return -EINVAL;
441 level = cifsFile->srch_inf.info_level;
443 if (level == SMB_FIND_FILE_UNIX) {
444 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
446 filename = &pFindData->FileName[0];
447 if (cifsFile->srch_inf.unicode) {
448 len = cifs_unicode_bytelen(filename);
449 } else {
450 /* BB should we make this strnlen of PATH_MAX? */
451 len = strnlen(filename, PATH_MAX);
453 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
454 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
455 FILE_DIRECTORY_INFO *pFindData =
456 (FILE_DIRECTORY_INFO *)current_entry;
457 filename = &pFindData->FileName[0];
458 len = le32_to_cpu(pFindData->FileNameLength);
459 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
460 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
461 FILE_FULL_DIRECTORY_INFO *pFindData =
462 (FILE_FULL_DIRECTORY_INFO *)current_entry;
463 filename = &pFindData->FileName[0];
464 len = le32_to_cpu(pFindData->FileNameLength);
465 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
466 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
467 SEARCH_ID_FULL_DIR_INFO *pFindData =
468 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
469 filename = &pFindData->FileName[0];
470 len = le32_to_cpu(pFindData->FileNameLength);
471 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
472 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
473 FILE_BOTH_DIRECTORY_INFO *pFindData =
474 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
475 filename = &pFindData->FileName[0];
476 len = le32_to_cpu(pFindData->FileNameLength);
477 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
478 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
479 FIND_FILE_STANDARD_INFO *pFindData =
480 (FIND_FILE_STANDARD_INFO *)current_entry;
481 filename = &pFindData->FileName[0];
482 /* one byte length, no name conversion */
483 len = (unsigned int)pFindData->FileNameLength;
484 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
485 } else {
486 cFYI(1, "Unknown findfirst level %d", level);
487 return -EINVAL;
489 cifsFile->srch_inf.resume_name_len = len;
490 cifsFile->srch_inf.presume_name = filename;
491 return rc;
494 /* find the corresponding entry in the search */
495 /* Note that the SMB server returns search entries for . and .. which
496 complicates logic here if we choose to parse for them and we do not
497 assume that they are located in the findfirst return buffer.*/
498 /* We start counting in the buffer with entry 2 and increment for every
499 entry (do not increment for . or .. entry) */
500 static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
501 struct file *file, char **ppCurrentEntry, int *num_to_ret)
503 int rc = 0;
504 int pos_in_buf = 0;
505 loff_t first_entry_in_buffer;
506 loff_t index_to_find = file->f_pos;
507 struct cifsFileInfo *cifsFile = file->private_data;
508 /* check if index in the buffer */
510 if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
511 (num_to_ret == NULL))
512 return -ENOENT;
514 *ppCurrentEntry = NULL;
515 first_entry_in_buffer =
516 cifsFile->srch_inf.index_of_last_entry -
517 cifsFile->srch_inf.entries_in_buffer;
519 /* if first entry in buf is zero then is first buffer
520 in search response data which means it is likely . and ..
521 will be in this buffer, although some servers do not return
522 . and .. for the root of a drive and for those we need
523 to start two entries earlier */
525 dump_cifs_file_struct(file, "In fce ");
526 if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
527 is_dir_changed(file)) ||
528 (index_to_find < first_entry_in_buffer)) {
529 /* close and restart search */
530 cFYI(1, "search backing up - close and restart search");
531 write_lock(&GlobalSMBSeslock);
532 if (!cifsFile->srch_inf.endOfSearch &&
533 !cifsFile->invalidHandle) {
534 cifsFile->invalidHandle = true;
535 write_unlock(&GlobalSMBSeslock);
536 CIFSFindClose(xid, pTcon, cifsFile->netfid);
537 } else
538 write_unlock(&GlobalSMBSeslock);
539 if (cifsFile->srch_inf.ntwrk_buf_start) {
540 cFYI(1, "freeing SMB ff cache buf on search rewind");
541 if (cifsFile->srch_inf.smallBuf)
542 cifs_small_buf_release(cifsFile->srch_inf.
543 ntwrk_buf_start);
544 else
545 cifs_buf_release(cifsFile->srch_inf.
546 ntwrk_buf_start);
547 cifsFile->srch_inf.ntwrk_buf_start = NULL;
549 rc = initiate_cifs_search(xid, file);
550 if (rc) {
551 cFYI(1, "error %d reinitiating a search on rewind",
552 rc);
553 return rc;
555 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
558 while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
559 (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
560 cFYI(1, "calling findnext2");
561 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
562 &cifsFile->srch_inf);
563 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
564 if (rc)
565 return -ENOENT;
567 if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
568 /* we found the buffer that contains the entry */
569 /* scan and find it */
570 int i;
571 char *current_entry;
572 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
573 smbCalcSize((struct smb_hdr *)
574 cifsFile->srch_inf.ntwrk_buf_start);
576 current_entry = cifsFile->srch_inf.srch_entries_start;
577 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
578 - cifsFile->srch_inf.entries_in_buffer;
579 pos_in_buf = index_to_find - first_entry_in_buffer;
580 cFYI(1, "found entry - pos_in_buf %d", pos_in_buf);
582 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
583 /* go entry by entry figuring out which is first */
584 current_entry = nxt_dir_entry(current_entry, end_of_smb,
585 cifsFile->srch_inf.info_level);
587 if ((current_entry == NULL) && (i < pos_in_buf)) {
588 /* BB fixme - check if we should flag this error */
589 cERROR(1, "reached end of buf searching for pos in buf"
590 " %d index to find %lld rc %d",
591 pos_in_buf, index_to_find, rc);
593 rc = 0;
594 *ppCurrentEntry = current_entry;
595 } else {
596 cFYI(1, "index not in buffer - could not findnext into it");
597 return 0;
600 if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
601 cFYI(1, "can not return entries pos_in_buf beyond last");
602 *num_to_ret = 0;
603 } else
604 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
606 return rc;
609 /* inode num, inode type and filename returned */
610 static int cifs_get_name_from_search_buf(struct qstr *pqst,
611 char *current_entry, __u16 level, unsigned int unicode,
612 struct cifs_sb_info *cifs_sb, unsigned int max_len, __u64 *pinum)
614 int rc = 0;
615 unsigned int len = 0;
616 char *filename;
617 struct nls_table *nlt = cifs_sb->local_nls;
619 *pinum = 0;
621 if (level == SMB_FIND_FILE_UNIX) {
622 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
624 filename = &pFindData->FileName[0];
625 if (unicode) {
626 len = cifs_unicode_bytelen(filename);
627 } else {
628 /* BB should we make this strnlen of PATH_MAX? */
629 len = strnlen(filename, PATH_MAX);
632 *pinum = le64_to_cpu(pFindData->basic.UniqueId);
633 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
634 FILE_DIRECTORY_INFO *pFindData =
635 (FILE_DIRECTORY_INFO *)current_entry;
636 filename = &pFindData->FileName[0];
637 len = le32_to_cpu(pFindData->FileNameLength);
638 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
639 FILE_FULL_DIRECTORY_INFO *pFindData =
640 (FILE_FULL_DIRECTORY_INFO *)current_entry;
641 filename = &pFindData->FileName[0];
642 len = le32_to_cpu(pFindData->FileNameLength);
643 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
644 SEARCH_ID_FULL_DIR_INFO *pFindData =
645 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
646 filename = &pFindData->FileName[0];
647 len = le32_to_cpu(pFindData->FileNameLength);
648 *pinum = le64_to_cpu(pFindData->UniqueId);
649 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
650 FILE_BOTH_DIRECTORY_INFO *pFindData =
651 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
652 filename = &pFindData->FileName[0];
653 len = le32_to_cpu(pFindData->FileNameLength);
654 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
655 FIND_FILE_STANDARD_INFO *pFindData =
656 (FIND_FILE_STANDARD_INFO *)current_entry;
657 filename = &pFindData->FileName[0];
658 /* one byte length, no name conversion */
659 len = (unsigned int)pFindData->FileNameLength;
660 } else {
661 cFYI(1, "Unknown findfirst level %d", level);
662 return -EINVAL;
665 if (len > max_len) {
666 cERROR(1, "bad search response length %d past smb end", len);
667 return -EINVAL;
670 if (unicode) {
671 pqst->len = cifs_from_ucs2((char *) pqst->name,
672 (__le16 *) filename,
673 UNICODE_NAME_MAX,
674 min(len, max_len), nlt,
675 cifs_sb->mnt_cifs_flags &
676 CIFS_MOUNT_MAP_SPECIAL_CHR);
677 pqst->len -= nls_nullsize(nlt);
678 } else {
679 pqst->name = filename;
680 pqst->len = len;
682 return rc;
685 static int cifs_filldir(char *pfindEntry, struct file *file, filldir_t filldir,
686 void *direntry, char *scratch_buf, unsigned int max_len)
688 int rc = 0;
689 struct qstr qstring;
690 struct cifsFileInfo *pCifsF;
691 u64 inum;
692 ino_t ino;
693 struct super_block *sb;
694 struct cifs_sb_info *cifs_sb;
695 struct dentry *tmp_dentry;
696 struct cifs_fattr fattr;
698 /* get filename and len into qstring */
699 /* get dentry */
700 /* decide whether to create and populate ionde */
701 if ((direntry == NULL) || (file == NULL))
702 return -EINVAL;
704 pCifsF = file->private_data;
706 if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
707 return -ENOENT;
709 rc = cifs_entry_is_dot(pfindEntry, pCifsF);
710 /* skip . and .. since we added them first */
711 if (rc != 0)
712 return 0;
714 sb = file->f_path.dentry->d_sb;
715 cifs_sb = CIFS_SB(sb);
717 qstring.name = scratch_buf;
718 rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
719 pCifsF->srch_inf.info_level,
720 pCifsF->srch_inf.unicode, cifs_sb,
721 max_len, &inum /* returned */);
723 if (rc)
724 return rc;
726 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
727 cifs_unix_basic_to_fattr(&fattr,
728 &((FILE_UNIX_INFO *) pfindEntry)->basic,
729 cifs_sb);
730 else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
731 cifs_std_info_to_fattr(&fattr, (FIND_FILE_STANDARD_INFO *)
732 pfindEntry, cifs_sb);
733 else
734 cifs_dir_info_to_fattr(&fattr, (FILE_DIRECTORY_INFO *)
735 pfindEntry, cifs_sb);
737 if (inum && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
738 fattr.cf_uniqueid = inum;
739 } else {
740 fattr.cf_uniqueid = iunique(sb, ROOT_I);
741 cifs_autodisable_serverino(cifs_sb);
744 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) &&
745 CIFSCouldBeMFSymlink(&fattr))
747 * trying to get the type and mode can be slow,
748 * so just call those regular files for now, and mark
749 * for reval
751 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
753 ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
754 tmp_dentry = cifs_readdir_lookup(file->f_dentry, &qstring, &fattr);
756 rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
757 ino, fattr.cf_dtype);
760 * we can not return filldir errors to the caller since they are
761 * "normal" when the stat blocksize is too small - we return remapped
762 * error instead
764 * FIXME: This looks bogus. filldir returns -EOVERFLOW in the above
765 * case already. Why should we be clobbering other errors from it?
767 if (rc) {
768 cFYI(1, "filldir rc = %d", rc);
769 rc = -EOVERFLOW;
771 dput(tmp_dentry);
772 return rc;
776 int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
778 int rc = 0;
779 int xid, i;
780 struct cifs_sb_info *cifs_sb;
781 struct cifsTconInfo *pTcon;
782 struct cifsFileInfo *cifsFile = NULL;
783 char *current_entry;
784 int num_to_fill = 0;
785 char *tmp_buf = NULL;
786 char *end_of_smb;
787 unsigned int max_len;
789 xid = GetXid();
791 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
794 * Ensure FindFirst doesn't fail before doing filldir() for '.' and
795 * '..'. Otherwise we won't be able to notify VFS in case of failure.
797 if (file->private_data == NULL) {
798 rc = initiate_cifs_search(xid, file);
799 cFYI(1, "initiate cifs search rc %d", rc);
800 if (rc)
801 goto rddir2_exit;
804 switch ((int) file->f_pos) {
805 case 0:
806 if (filldir(direntry, ".", 1, file->f_pos,
807 file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
808 cERROR(1, "Filldir for current dir failed");
809 rc = -ENOMEM;
810 break;
812 file->f_pos++;
813 case 1:
814 if (filldir(direntry, "..", 2, file->f_pos,
815 file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
816 cERROR(1, "Filldir for parent dir failed");
817 rc = -ENOMEM;
818 break;
820 file->f_pos++;
821 default:
822 /* 1) If search is active,
823 is in current search buffer?
824 if it before then restart search
825 if after then keep searching till find it */
827 if (file->private_data == NULL) {
828 rc = -EINVAL;
829 FreeXid(xid);
830 return rc;
832 cifsFile = file->private_data;
833 if (cifsFile->srch_inf.endOfSearch) {
834 if (cifsFile->srch_inf.emptyDir) {
835 cFYI(1, "End of search, empty dir");
836 rc = 0;
837 break;
839 } /* else {
840 cifsFile->invalidHandle = true;
841 CIFSFindClose(xid, pTcon, cifsFile->netfid);
842 } */
844 pTcon = tlink_tcon(cifsFile->tlink);
845 rc = find_cifs_entry(xid, pTcon, file,
846 &current_entry, &num_to_fill);
847 if (rc) {
848 cFYI(1, "fce error %d", rc);
849 goto rddir2_exit;
850 } else if (current_entry != NULL) {
851 cFYI(1, "entry %lld found", file->f_pos);
852 } else {
853 cFYI(1, "could not find entry");
854 goto rddir2_exit;
856 cFYI(1, "loop through %d times filling dir for net buf %p",
857 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start);
858 max_len = smbCalcSize((struct smb_hdr *)
859 cifsFile->srch_inf.ntwrk_buf_start);
860 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
862 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
863 if (tmp_buf == NULL) {
864 rc = -ENOMEM;
865 break;
868 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
869 if (current_entry == NULL) {
870 /* evaluate whether this case is an error */
871 cERROR(1, "past SMB end, num to fill %d i %d",
872 num_to_fill, i);
873 break;
875 /* if buggy server returns . and .. late do
876 we want to check for that here? */
877 rc = cifs_filldir(current_entry, file,
878 filldir, direntry, tmp_buf, max_len);
879 if (rc == -EOVERFLOW) {
880 rc = 0;
881 break;
884 file->f_pos++;
885 if (file->f_pos ==
886 cifsFile->srch_inf.index_of_last_entry) {
887 cFYI(1, "last entry in buf at pos %lld %s",
888 file->f_pos, tmp_buf);
889 cifs_save_resume_key(current_entry, cifsFile);
890 break;
891 } else
892 current_entry =
893 nxt_dir_entry(current_entry, end_of_smb,
894 cifsFile->srch_inf.info_level);
896 kfree(tmp_buf);
897 break;
898 } /* end switch */
900 rddir2_exit:
901 FreeXid(xid);
902 return rc;