thinkpad-acpi: deprecate hotkey_bios_mask
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / cifs / readdir.c
blobf55d42bbf7e86f1f1424856b67e1df62cf277967
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/stat.h>
26 #include "cifspdu.h"
27 #include "cifsglob.h"
28 #include "cifsproto.h"
29 #include "cifs_unicode.h"
30 #include "cifs_debug.h"
31 #include "cifs_fs_sb.h"
32 #include "cifsfs.h"
35 * To be safe - for UCS to UTF-8 with strings loaded with the rare long
36 * characters alloc more to account for such multibyte target UTF-8
37 * characters.
39 #define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
41 #ifdef CONFIG_CIFS_DEBUG2
42 static void dump_cifs_file_struct(struct file *file, char *label)
44 struct cifsFileInfo *cf;
46 if (file) {
47 cf = file->private_data;
48 if (cf == NULL) {
49 cFYI(1, ("empty cifs private file data"));
50 return;
52 if (cf->invalidHandle)
53 cFYI(1, ("invalid handle"));
54 if (cf->srch_inf.endOfSearch)
55 cFYI(1, ("end of search"));
56 if (cf->srch_inf.emptyDir)
57 cFYI(1, ("empty dir"));
60 #else
61 static inline void dump_cifs_file_struct(struct file *file, char *label)
64 #endif /* DEBUG2 */
67 * Find the dentry that matches "name". If there isn't one, create one. If it's
68 * a negative dentry or the uniqueid changed, then drop it and recreate it.
70 static struct dentry *
71 cifs_readdir_lookup(struct dentry *parent, struct qstr *name,
72 struct cifs_fattr *fattr)
74 struct dentry *dentry, *alias;
75 struct inode *inode;
76 struct super_block *sb = parent->d_inode->i_sb;
78 cFYI(1, ("For %s", name->name));
80 dentry = d_lookup(parent, name);
81 if (dentry) {
82 /* FIXME: check for inode number changes? */
83 if (dentry->d_inode != NULL)
84 return dentry;
85 d_drop(dentry);
86 dput(dentry);
89 dentry = d_alloc(parent, name);
90 if (dentry == NULL)
91 return NULL;
93 inode = cifs_iget(sb, fattr);
94 if (!inode) {
95 dput(dentry);
96 return NULL;
99 if (CIFS_SB(sb)->tcon->nocase)
100 dentry->d_op = &cifs_ci_dentry_ops;
101 else
102 dentry->d_op = &cifs_dentry_ops;
104 alias = d_materialise_unique(dentry, inode);
105 if (alias != NULL) {
106 dput(dentry);
107 if (IS_ERR(alias))
108 return NULL;
109 dentry = alias;
112 return dentry;
115 static void
116 cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
118 fattr->cf_uid = cifs_sb->mnt_uid;
119 fattr->cf_gid = cifs_sb->mnt_gid;
121 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
122 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
123 fattr->cf_dtype = DT_DIR;
124 } else {
125 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
126 fattr->cf_dtype = DT_REG;
129 if (fattr->cf_cifsattrs & ATTR_READONLY)
130 fattr->cf_mode &= ~S_IWUGO;
132 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
133 fattr->cf_cifsattrs & ATTR_SYSTEM) {
134 if (fattr->cf_eof == 0) {
135 fattr->cf_mode &= ~S_IFMT;
136 fattr->cf_mode |= S_IFIFO;
137 fattr->cf_dtype = DT_FIFO;
138 } else {
140 * trying to get the type and mode via SFU can be slow,
141 * so just call those regular files for now, and mark
142 * for reval
144 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
149 void
150 cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info,
151 struct cifs_sb_info *cifs_sb)
153 memset(fattr, 0, sizeof(*fattr));
154 fattr->cf_cifsattrs = le32_to_cpu(info->ExtFileAttributes);
155 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
156 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
157 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
158 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
159 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
161 cifs_fill_common_info(fattr, cifs_sb);
164 void
165 cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info,
166 struct cifs_sb_info *cifs_sb)
168 int offset = cifs_sb->tcon->ses->server->timeAdj;
170 memset(fattr, 0, sizeof(*fattr));
171 fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate,
172 info->LastAccessTime, offset);
173 fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate,
174 info->LastWriteTime, offset);
175 fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate,
176 info->LastWriteTime, offset);
178 fattr->cf_cifsattrs = le16_to_cpu(info->Attributes);
179 fattr->cf_bytes = le32_to_cpu(info->AllocationSize);
180 fattr->cf_eof = le32_to_cpu(info->DataSize);
182 cifs_fill_common_info(fattr, cifs_sb);
185 /* BB eventually need to add the following helper function to
186 resolve NT_STATUS_STOPPED_ON_SYMLINK return code when
187 we try to do FindFirst on (NTFS) directory symlinks */
189 int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
190 int xid)
192 __u16 fid;
193 int len;
194 int oplock = 0;
195 int rc;
196 struct cifsTconInfo *ptcon = cifs_sb->tcon;
197 char *tmpbuffer;
199 rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ,
200 OPEN_REPARSE_POINT, &fid, &oplock, NULL,
201 cifs_sb->local_nls,
202 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
203 if (!rc) {
204 tmpbuffer = kmalloc(maxpath);
205 rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path,
206 tmpbuffer,
207 maxpath -1,
208 fid,
209 cifs_sb->local_nls);
210 if (CIFSSMBClose(xid, ptcon, fid)) {
211 cFYI(1, ("Error closing temporary reparsepoint open)"));
217 static int initiate_cifs_search(const int xid, struct file *file)
219 int rc = 0;
220 char *full_path;
221 struct cifsFileInfo *cifsFile;
222 struct cifs_sb_info *cifs_sb;
223 struct cifsTconInfo *pTcon;
225 if (file->private_data == NULL) {
226 file->private_data =
227 kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
230 if (file->private_data == NULL)
231 return -ENOMEM;
232 cifsFile = file->private_data;
233 cifsFile->invalidHandle = true;
234 cifsFile->srch_inf.endOfSearch = false;
236 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
237 if (cifs_sb == NULL)
238 return -EINVAL;
240 pTcon = cifs_sb->tcon;
241 if (pTcon == NULL)
242 return -EINVAL;
244 full_path = build_path_from_dentry(file->f_path.dentry);
246 if (full_path == NULL)
247 return -ENOMEM;
249 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
251 ffirst_retry:
252 /* test for Unix extensions */
253 /* but now check for them on the share/mount not on the SMB session */
254 /* if (pTcon->ses->capabilities & CAP_UNIX) { */
255 if (pTcon->unix_ext)
256 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
257 else if ((pTcon->ses->capabilities &
258 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
259 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
260 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
261 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
262 } else /* not srvinos - BB fixme add check for backlevel? */ {
263 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
266 rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
267 &cifsFile->netfid, &cifsFile->srch_inf,
268 cifs_sb->mnt_cifs_flags &
269 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
270 if (rc == 0)
271 cifsFile->invalidHandle = false;
272 /* BB add following call to handle readdir on new NTFS symlink errors
273 else if STATUS_STOPPED_ON_SYMLINK
274 call get_symlink_reparse_path and retry with new path */
275 else if ((rc == -EOPNOTSUPP) &&
276 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
277 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
278 goto ffirst_retry;
280 kfree(full_path);
281 return rc;
284 /* return length of unicode string in bytes */
285 static int cifs_unicode_bytelen(char *str)
287 int len;
288 __le16 *ustr = (__le16 *)str;
290 for (len = 0; len <= PATH_MAX; len++) {
291 if (ustr[len] == 0)
292 return len << 1;
294 cFYI(1, ("Unicode string longer than PATH_MAX found"));
295 return len << 1;
298 static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
300 char *new_entry;
301 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
303 if (level == SMB_FIND_FILE_INFO_STANDARD) {
304 FIND_FILE_STANDARD_INFO *pfData;
305 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
307 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
308 pfData->FileNameLength;
309 } else
310 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
311 cFYI(1, ("new entry %p old entry %p", new_entry, old_entry));
312 /* validate that new_entry is not past end of SMB */
313 if (new_entry >= end_of_smb) {
314 cERROR(1,
315 ("search entry %p began after end of SMB %p old entry %p",
316 new_entry, end_of_smb, old_entry));
317 return NULL;
318 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
319 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
320 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
321 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
322 cERROR(1, ("search entry %p extends after end of SMB %p",
323 new_entry, end_of_smb));
324 return NULL;
325 } else
326 return new_entry;
330 #define UNICODE_DOT cpu_to_le16(0x2e)
332 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
333 static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
335 int rc = 0;
336 char *filename = NULL;
337 int len = 0;
339 if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
340 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
341 filename = &pFindData->FileName[0];
342 if (cfile->srch_inf.unicode) {
343 len = cifs_unicode_bytelen(filename);
344 } else {
345 /* BB should we make this strnlen of PATH_MAX? */
346 len = strnlen(filename, 5);
348 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
349 FILE_DIRECTORY_INFO *pFindData =
350 (FILE_DIRECTORY_INFO *)current_entry;
351 filename = &pFindData->FileName[0];
352 len = le32_to_cpu(pFindData->FileNameLength);
353 } else if (cfile->srch_inf.info_level ==
354 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
355 FILE_FULL_DIRECTORY_INFO *pFindData =
356 (FILE_FULL_DIRECTORY_INFO *)current_entry;
357 filename = &pFindData->FileName[0];
358 len = le32_to_cpu(pFindData->FileNameLength);
359 } else if (cfile->srch_inf.info_level ==
360 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
361 SEARCH_ID_FULL_DIR_INFO *pFindData =
362 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
363 filename = &pFindData->FileName[0];
364 len = le32_to_cpu(pFindData->FileNameLength);
365 } else if (cfile->srch_inf.info_level ==
366 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
367 FILE_BOTH_DIRECTORY_INFO *pFindData =
368 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
369 filename = &pFindData->FileName[0];
370 len = le32_to_cpu(pFindData->FileNameLength);
371 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
372 FIND_FILE_STANDARD_INFO *pFindData =
373 (FIND_FILE_STANDARD_INFO *)current_entry;
374 filename = &pFindData->FileName[0];
375 len = pFindData->FileNameLength;
376 } else {
377 cFYI(1, ("Unknown findfirst level %d",
378 cfile->srch_inf.info_level));
381 if (filename) {
382 if (cfile->srch_inf.unicode) {
383 __le16 *ufilename = (__le16 *)filename;
384 if (len == 2) {
385 /* check for . */
386 if (ufilename[0] == UNICODE_DOT)
387 rc = 1;
388 } else if (len == 4) {
389 /* check for .. */
390 if ((ufilename[0] == UNICODE_DOT)
391 && (ufilename[1] == UNICODE_DOT))
392 rc = 2;
394 } else /* ASCII */ {
395 if (len == 1) {
396 if (filename[0] == '.')
397 rc = 1;
398 } else if (len == 2) {
399 if ((filename[0] == '.') && (filename[1] == '.'))
400 rc = 2;
405 return rc;
408 /* Check if directory that we are searching has changed so we can decide
409 whether we can use the cached search results from the previous search */
410 static int is_dir_changed(struct file *file)
412 struct inode *inode = file->f_path.dentry->d_inode;
413 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
415 if (cifsInfo->time == 0)
416 return 1; /* directory was changed, perhaps due to unlink */
417 else
418 return 0;
422 static int cifs_save_resume_key(const char *current_entry,
423 struct cifsFileInfo *cifsFile)
425 int rc = 0;
426 unsigned int len = 0;
427 __u16 level;
428 char *filename;
430 if ((cifsFile == NULL) || (current_entry == NULL))
431 return -EINVAL;
433 level = cifsFile->srch_inf.info_level;
435 if (level == SMB_FIND_FILE_UNIX) {
436 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
438 filename = &pFindData->FileName[0];
439 if (cifsFile->srch_inf.unicode) {
440 len = cifs_unicode_bytelen(filename);
441 } else {
442 /* BB should we make this strnlen of PATH_MAX? */
443 len = strnlen(filename, PATH_MAX);
445 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
446 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
447 FILE_DIRECTORY_INFO *pFindData =
448 (FILE_DIRECTORY_INFO *)current_entry;
449 filename = &pFindData->FileName[0];
450 len = le32_to_cpu(pFindData->FileNameLength);
451 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
452 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
453 FILE_FULL_DIRECTORY_INFO *pFindData =
454 (FILE_FULL_DIRECTORY_INFO *)current_entry;
455 filename = &pFindData->FileName[0];
456 len = le32_to_cpu(pFindData->FileNameLength);
457 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
458 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
459 SEARCH_ID_FULL_DIR_INFO *pFindData =
460 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
461 filename = &pFindData->FileName[0];
462 len = le32_to_cpu(pFindData->FileNameLength);
463 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
464 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
465 FILE_BOTH_DIRECTORY_INFO *pFindData =
466 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
467 filename = &pFindData->FileName[0];
468 len = le32_to_cpu(pFindData->FileNameLength);
469 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
470 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
471 FIND_FILE_STANDARD_INFO *pFindData =
472 (FIND_FILE_STANDARD_INFO *)current_entry;
473 filename = &pFindData->FileName[0];
474 /* one byte length, no name conversion */
475 len = (unsigned int)pFindData->FileNameLength;
476 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
477 } else {
478 cFYI(1, ("Unknown findfirst level %d", level));
479 return -EINVAL;
481 cifsFile->srch_inf.resume_name_len = len;
482 cifsFile->srch_inf.presume_name = filename;
483 return rc;
486 /* find the corresponding entry in the search */
487 /* Note that the SMB server returns search entries for . and .. which
488 complicates logic here if we choose to parse for them and we do not
489 assume that they are located in the findfirst return buffer.*/
490 /* We start counting in the buffer with entry 2 and increment for every
491 entry (do not increment for . or .. entry) */
492 static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
493 struct file *file, char **ppCurrentEntry, int *num_to_ret)
495 int rc = 0;
496 int pos_in_buf = 0;
497 loff_t first_entry_in_buffer;
498 loff_t index_to_find = file->f_pos;
499 struct cifsFileInfo *cifsFile = file->private_data;
500 /* check if index in the buffer */
502 if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
503 (num_to_ret == NULL))
504 return -ENOENT;
506 *ppCurrentEntry = NULL;
507 first_entry_in_buffer =
508 cifsFile->srch_inf.index_of_last_entry -
509 cifsFile->srch_inf.entries_in_buffer;
511 /* if first entry in buf is zero then is first buffer
512 in search response data which means it is likely . and ..
513 will be in this buffer, although some servers do not return
514 . and .. for the root of a drive and for those we need
515 to start two entries earlier */
517 dump_cifs_file_struct(file, "In fce ");
518 if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
519 is_dir_changed(file)) ||
520 (index_to_find < first_entry_in_buffer)) {
521 /* close and restart search */
522 cFYI(1, ("search backing up - close and restart search"));
523 write_lock(&GlobalSMBSeslock);
524 if (!cifsFile->srch_inf.endOfSearch &&
525 !cifsFile->invalidHandle) {
526 cifsFile->invalidHandle = true;
527 write_unlock(&GlobalSMBSeslock);
528 CIFSFindClose(xid, pTcon, cifsFile->netfid);
529 } else
530 write_unlock(&GlobalSMBSeslock);
531 if (cifsFile->srch_inf.ntwrk_buf_start) {
532 cFYI(1, ("freeing SMB ff cache buf on search rewind"));
533 if (cifsFile->srch_inf.smallBuf)
534 cifs_small_buf_release(cifsFile->srch_inf.
535 ntwrk_buf_start);
536 else
537 cifs_buf_release(cifsFile->srch_inf.
538 ntwrk_buf_start);
539 cifsFile->srch_inf.ntwrk_buf_start = NULL;
541 rc = initiate_cifs_search(xid, file);
542 if (rc) {
543 cFYI(1, ("error %d reinitiating a search on rewind",
544 rc));
545 return rc;
547 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
550 while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
551 (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
552 cFYI(1, ("calling findnext2"));
553 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
554 &cifsFile->srch_inf);
555 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
556 if (rc)
557 return -ENOENT;
559 if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
560 /* we found the buffer that contains the entry */
561 /* scan and find it */
562 int i;
563 char *current_entry;
564 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
565 smbCalcSize((struct smb_hdr *)
566 cifsFile->srch_inf.ntwrk_buf_start);
568 current_entry = cifsFile->srch_inf.srch_entries_start;
569 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
570 - cifsFile->srch_inf.entries_in_buffer;
571 pos_in_buf = index_to_find - first_entry_in_buffer;
572 cFYI(1, ("found entry - pos_in_buf %d", pos_in_buf));
574 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
575 /* go entry by entry figuring out which is first */
576 current_entry = nxt_dir_entry(current_entry, end_of_smb,
577 cifsFile->srch_inf.info_level);
579 if ((current_entry == NULL) && (i < pos_in_buf)) {
580 /* BB fixme - check if we should flag this error */
581 cERROR(1, ("reached end of buf searching for pos in buf"
582 " %d index to find %lld rc %d",
583 pos_in_buf, index_to_find, rc));
585 rc = 0;
586 *ppCurrentEntry = current_entry;
587 } else {
588 cFYI(1, ("index not in buffer - could not findnext into it"));
589 return 0;
592 if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
593 cFYI(1, ("can not return entries pos_in_buf beyond last"));
594 *num_to_ret = 0;
595 } else
596 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
598 return rc;
601 /* inode num, inode type and filename returned */
602 static int cifs_get_name_from_search_buf(struct qstr *pqst,
603 char *current_entry, __u16 level, unsigned int unicode,
604 struct cifs_sb_info *cifs_sb, unsigned int max_len, __u64 *pinum)
606 int rc = 0;
607 unsigned int len = 0;
608 char *filename;
609 struct nls_table *nlt = cifs_sb->local_nls;
611 *pinum = 0;
613 if (level == SMB_FIND_FILE_UNIX) {
614 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
616 filename = &pFindData->FileName[0];
617 if (unicode) {
618 len = cifs_unicode_bytelen(filename);
619 } else {
620 /* BB should we make this strnlen of PATH_MAX? */
621 len = strnlen(filename, PATH_MAX);
624 *pinum = le64_to_cpu(pFindData->basic.UniqueId);
625 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
626 FILE_DIRECTORY_INFO *pFindData =
627 (FILE_DIRECTORY_INFO *)current_entry;
628 filename = &pFindData->FileName[0];
629 len = le32_to_cpu(pFindData->FileNameLength);
630 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
631 FILE_FULL_DIRECTORY_INFO *pFindData =
632 (FILE_FULL_DIRECTORY_INFO *)current_entry;
633 filename = &pFindData->FileName[0];
634 len = le32_to_cpu(pFindData->FileNameLength);
635 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
636 SEARCH_ID_FULL_DIR_INFO *pFindData =
637 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
638 filename = &pFindData->FileName[0];
639 len = le32_to_cpu(pFindData->FileNameLength);
640 *pinum = le64_to_cpu(pFindData->UniqueId);
641 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
642 FILE_BOTH_DIRECTORY_INFO *pFindData =
643 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
644 filename = &pFindData->FileName[0];
645 len = le32_to_cpu(pFindData->FileNameLength);
646 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
647 FIND_FILE_STANDARD_INFO *pFindData =
648 (FIND_FILE_STANDARD_INFO *)current_entry;
649 filename = &pFindData->FileName[0];
650 /* one byte length, no name conversion */
651 len = (unsigned int)pFindData->FileNameLength;
652 } else {
653 cFYI(1, ("Unknown findfirst level %d", level));
654 return -EINVAL;
657 if (len > max_len) {
658 cERROR(1, ("bad search response length %d past smb end", len));
659 return -EINVAL;
662 if (unicode) {
663 pqst->len = cifs_from_ucs2((char *) pqst->name,
664 (__le16 *) filename,
665 UNICODE_NAME_MAX,
666 min(len, max_len), nlt,
667 cifs_sb->mnt_cifs_flags &
668 CIFS_MOUNT_MAP_SPECIAL_CHR);
669 pqst->len -= nls_nullsize(nlt);
670 } else {
671 pqst->name = filename;
672 pqst->len = len;
674 pqst->hash = full_name_hash(pqst->name, pqst->len);
675 /* cFYI(1, ("filldir on %s",pqst->name)); */
676 return rc;
679 static int cifs_filldir(char *pfindEntry, struct file *file, filldir_t filldir,
680 void *direntry, char *scratch_buf, unsigned int max_len)
682 int rc = 0;
683 struct qstr qstring;
684 struct cifsFileInfo *pCifsF;
685 u64 inum;
686 ino_t ino;
687 struct super_block *sb;
688 struct cifs_sb_info *cifs_sb;
689 struct dentry *tmp_dentry;
690 struct cifs_fattr fattr;
692 /* get filename and len into qstring */
693 /* get dentry */
694 /* decide whether to create and populate ionde */
695 if ((direntry == NULL) || (file == NULL))
696 return -EINVAL;
698 pCifsF = file->private_data;
700 if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
701 return -ENOENT;
703 rc = cifs_entry_is_dot(pfindEntry, pCifsF);
704 /* skip . and .. since we added them first */
705 if (rc != 0)
706 return 0;
708 sb = file->f_path.dentry->d_sb;
709 cifs_sb = CIFS_SB(sb);
711 qstring.name = scratch_buf;
712 rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
713 pCifsF->srch_inf.info_level,
714 pCifsF->srch_inf.unicode, cifs_sb,
715 max_len, &inum /* returned */);
717 if (rc)
718 return rc;
720 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
721 cifs_unix_basic_to_fattr(&fattr,
722 &((FILE_UNIX_INFO *) pfindEntry)->basic,
723 cifs_sb);
724 else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
725 cifs_std_info_to_fattr(&fattr, (FIND_FILE_STANDARD_INFO *)
726 pfindEntry, cifs_sb);
727 else
728 cifs_dir_info_to_fattr(&fattr, (FILE_DIRECTORY_INFO *)
729 pfindEntry, cifs_sb);
731 if (inum && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
732 fattr.cf_uniqueid = inum;
733 } else {
734 fattr.cf_uniqueid = iunique(sb, ROOT_I);
735 cifs_autodisable_serverino(cifs_sb);
738 ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
739 tmp_dentry = cifs_readdir_lookup(file->f_dentry, &qstring, &fattr);
741 rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
742 ino, fattr.cf_dtype);
745 * we can not return filldir errors to the caller since they are
746 * "normal" when the stat blocksize is too small - we return remapped
747 * error instead
749 * FIXME: This looks bogus. filldir returns -EOVERFLOW in the above
750 * case already. Why should we be clobbering other errors from it?
752 if (rc) {
753 cFYI(1, ("filldir rc = %d", rc));
754 rc = -EOVERFLOW;
756 dput(tmp_dentry);
757 return rc;
761 int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
763 int rc = 0;
764 int xid, i;
765 struct cifs_sb_info *cifs_sb;
766 struct cifsTconInfo *pTcon;
767 struct cifsFileInfo *cifsFile = NULL;
768 char *current_entry;
769 int num_to_fill = 0;
770 char *tmp_buf = NULL;
771 char *end_of_smb;
772 unsigned int max_len;
774 xid = GetXid();
776 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
777 pTcon = cifs_sb->tcon;
778 if (pTcon == NULL)
779 return -EINVAL;
781 switch ((int) file->f_pos) {
782 case 0:
783 if (filldir(direntry, ".", 1, file->f_pos,
784 file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
785 cERROR(1, ("Filldir for current dir failed"));
786 rc = -ENOMEM;
787 break;
789 file->f_pos++;
790 case 1:
791 if (filldir(direntry, "..", 2, file->f_pos,
792 file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
793 cERROR(1, ("Filldir for parent dir failed"));
794 rc = -ENOMEM;
795 break;
797 file->f_pos++;
798 default:
799 /* 1) If search is active,
800 is in current search buffer?
801 if it before then restart search
802 if after then keep searching till find it */
804 if (file->private_data == NULL) {
805 rc = initiate_cifs_search(xid, file);
806 cFYI(1, ("initiate cifs search rc %d", rc));
807 if (rc) {
808 FreeXid(xid);
809 return rc;
812 if (file->private_data == NULL) {
813 rc = -EINVAL;
814 FreeXid(xid);
815 return rc;
817 cifsFile = file->private_data;
818 if (cifsFile->srch_inf.endOfSearch) {
819 if (cifsFile->srch_inf.emptyDir) {
820 cFYI(1, ("End of search, empty dir"));
821 rc = 0;
822 break;
824 } /* else {
825 cifsFile->invalidHandle = true;
826 CIFSFindClose(xid, pTcon, cifsFile->netfid);
827 } */
829 rc = find_cifs_entry(xid, pTcon, file,
830 &current_entry, &num_to_fill);
831 if (rc) {
832 cFYI(1, ("fce error %d", rc));
833 goto rddir2_exit;
834 } else if (current_entry != NULL) {
835 cFYI(1, ("entry %lld found", file->f_pos));
836 } else {
837 cFYI(1, ("could not find entry"));
838 goto rddir2_exit;
840 cFYI(1, ("loop through %d times filling dir for net buf %p",
841 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start));
842 max_len = smbCalcSize((struct smb_hdr *)
843 cifsFile->srch_inf.ntwrk_buf_start);
844 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
846 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
847 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
848 if (current_entry == NULL) {
849 /* evaluate whether this case is an error */
850 cERROR(1, ("past SMB end, num to fill %d i %d",
851 num_to_fill, i));
852 break;
854 /* if buggy server returns . and .. late do
855 we want to check for that here? */
856 rc = cifs_filldir(current_entry, file,
857 filldir, direntry, tmp_buf, max_len);
858 if (rc == -EOVERFLOW) {
859 rc = 0;
860 break;
863 file->f_pos++;
864 if (file->f_pos ==
865 cifsFile->srch_inf.index_of_last_entry) {
866 cFYI(1, ("last entry in buf at pos %lld %s",
867 file->f_pos, tmp_buf));
868 cifs_save_resume_key(current_entry, cifsFile);
869 break;
870 } else
871 current_entry =
872 nxt_dir_entry(current_entry, end_of_smb,
873 cifsFile->srch_inf.info_level);
875 kfree(tmp_buf);
876 break;
877 } /* end switch */
879 rddir2_exit:
880 FreeXid(xid);
881 return rc;