tizen 2.3.1 release
[kernel/linux-3.0.git] / fs / cifs / readdir.c
1 /*
2  *   fs/cifs/readdir.c
3  *
4  *   Directory search handling
5  *
6  *   Copyright (C) International Business Machines  Corp., 2004, 2008
7  *   Author(s): Steve French (sfrench@us.ibm.com)
8  *
9  *   This library is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU Lesser General Public License as published
11  *   by the Free Software Foundation; either version 2.1 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This library is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
17  *   the GNU Lesser General Public License for more details.
18  *
19  *   You should have received a copy of the GNU Lesser General Public License
20  *   along with this library; if not, write to the Free Software
21  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 #include <linux/fs.h>
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"
34
35 /*
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.
39  */
40 #define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
41
42 #ifdef CONFIG_CIFS_DEBUG2
43 static void dump_cifs_file_struct(struct file *file, char *label)
44 {
45         struct cifsFileInfo *cf;
46
47         if (file) {
48                 cf = file->private_data;
49                 if (cf == NULL) {
50                         cFYI(1, "empty cifs private file data");
51                         return;
52                 }
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");
59         }
60 }
61 #else
62 static inline void dump_cifs_file_struct(struct file *file, char *label)
63 {
64 }
65 #endif /* DEBUG2 */
66
67 /*
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.
70  */
71 static struct dentry *
72 cifs_readdir_lookup(struct dentry *parent, struct qstr *name,
73                     struct cifs_fattr *fattr)
74 {
75         struct dentry *dentry, *alias;
76         struct inode *inode;
77         struct super_block *sb = parent->d_inode->i_sb;
78
79         cFYI(1, "For %s", name->name);
80
81         if (parent->d_op && parent->d_op->d_hash)
82                 parent->d_op->d_hash(parent, parent->d_inode, name);
83         else
84                 name->hash = full_name_hash(name->name, name->len);
85
86         dentry = d_lookup(parent, name);
87         if (dentry) {
88                 inode = dentry->d_inode;
89                 /* update inode in place if i_ino didn't change */
90                 if (inode && CIFS_I(inode)->uniqueid == fattr->cf_uniqueid) {
91                         cifs_fattr_to_inode(inode, fattr);
92                         return dentry;
93                 }
94                 d_drop(dentry);
95                 dput(dentry);
96         }
97
98         dentry = d_alloc(parent, name);
99         if (dentry == NULL)
100                 return NULL;
101
102         inode = cifs_iget(sb, fattr);
103         if (!inode) {
104                 dput(dentry);
105                 return NULL;
106         }
107
108         alias = d_materialise_unique(dentry, inode);
109         if (alias != NULL) {
110                 dput(dentry);
111                 if (IS_ERR(alias))
112                         return NULL;
113                 dentry = alias;
114         }
115
116         return dentry;
117 }
118
119 static void
120 cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
121 {
122         fattr->cf_uid = cifs_sb->mnt_uid;
123         fattr->cf_gid = cifs_sb->mnt_gid;
124
125         if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
126                 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
127                 fattr->cf_dtype = DT_DIR;
128         } else {
129                 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
130                 fattr->cf_dtype = DT_REG;
131         }
132
133         if (fattr->cf_cifsattrs & ATTR_READONLY)
134                 fattr->cf_mode &= ~S_IWUGO;
135
136         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
137             fattr->cf_cifsattrs & ATTR_SYSTEM) {
138                 if (fattr->cf_eof == 0)  {
139                         fattr->cf_mode &= ~S_IFMT;
140                         fattr->cf_mode |= S_IFIFO;
141                         fattr->cf_dtype = DT_FIFO;
142                 } else {
143                         /*
144                          * trying to get the type and mode via SFU can be slow,
145                          * so just call those regular files for now, and mark
146                          * for reval
147                          */
148                         fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
149                 }
150         }
151 }
152
153 static void
154 cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info,
155                        struct cifs_sb_info *cifs_sb)
156 {
157         memset(fattr, 0, sizeof(*fattr));
158         fattr->cf_cifsattrs = le32_to_cpu(info->ExtFileAttributes);
159         fattr->cf_eof = le64_to_cpu(info->EndOfFile);
160         fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
161         fattr->cf_createtime = le64_to_cpu(info->CreationTime);
162         fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
163         fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
164         fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
165
166         cifs_fill_common_info(fattr, cifs_sb);
167 }
168
169 static void
170 cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info,
171                        struct cifs_sb_info *cifs_sb)
172 {
173         int offset = cifs_sb_master_tcon(cifs_sb)->ses->server->timeAdj;
174
175         memset(fattr, 0, sizeof(*fattr));
176         fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate,
177                                             info->LastAccessTime, offset);
178         fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate,
179                                             info->LastWriteTime, offset);
180         fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate,
181                                             info->LastWriteTime, offset);
182
183         fattr->cf_cifsattrs = le16_to_cpu(info->Attributes);
184         fattr->cf_bytes = le32_to_cpu(info->AllocationSize);
185         fattr->cf_eof = le32_to_cpu(info->DataSize);
186
187         cifs_fill_common_info(fattr, cifs_sb);
188 }
189
190 /* BB eventually need to add the following helper function to
191       resolve NT_STATUS_STOPPED_ON_SYMLINK return code when
192       we try to do FindFirst on (NTFS) directory symlinks */
193 /*
194 int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
195                              int xid)
196 {
197         __u16 fid;
198         int len;
199         int oplock = 0;
200         int rc;
201         struct cifs_tcon *ptcon = cifs_sb_tcon(cifs_sb);
202         char *tmpbuffer;
203
204         rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ,
205                         OPEN_REPARSE_POINT, &fid, &oplock, NULL,
206                         cifs_sb->local_nls,
207                         cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
208         if (!rc) {
209                 tmpbuffer = kmalloc(maxpath);
210                 rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path,
211                                 tmpbuffer,
212                                 maxpath -1,
213                                 fid,
214                                 cifs_sb->local_nls);
215                 if (CIFSSMBClose(xid, ptcon, fid)) {
216                         cFYI(1, "Error closing temporary reparsepoint open");
217                 }
218         }
219 }
220  */
221
222 static int initiate_cifs_search(const int xid, struct file *file)
223 {
224         int rc = 0;
225         char *full_path = NULL;
226         struct cifsFileInfo *cifsFile;
227         struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
228         struct tcon_link *tlink = NULL;
229         struct cifs_tcon *pTcon;
230
231         if (file->private_data == NULL) {
232                 tlink = cifs_sb_tlink(cifs_sb);
233                 if (IS_ERR(tlink))
234                         return PTR_ERR(tlink);
235
236                 cifsFile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
237                 if (cifsFile == NULL) {
238                         rc = -ENOMEM;
239                         goto error_exit;
240                 }
241                 file->private_data = cifsFile;
242                 cifsFile->tlink = cifs_get_tlink(tlink);
243                 pTcon = tlink_tcon(tlink);
244         } else {
245                 cifsFile = file->private_data;
246                 pTcon = tlink_tcon(cifsFile->tlink);
247         }
248
249         cifsFile->invalidHandle = true;
250         cifsFile->srch_inf.endOfSearch = false;
251
252         full_path = build_path_from_dentry(file->f_path.dentry);
253         if (full_path == NULL) {
254                 rc = -ENOMEM;
255                 goto error_exit;
256         }
257
258         cFYI(1, "Full path: %s start at: %lld", full_path, file->f_pos);
259
260 ffirst_retry:
261         /* test for Unix extensions */
262         /* but now check for them on the share/mount not on the SMB session */
263 /*      if (pTcon->ses->capabilities & CAP_UNIX) { */
264         if (pTcon->unix_ext)
265                 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
266         else if ((pTcon->ses->capabilities &
267                         (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
268                 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
269         } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
270                 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
271         } else /* not srvinos - BB fixme add check for backlevel? */ {
272                 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
273         }
274
275         rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
276                 &cifsFile->netfid, &cifsFile->srch_inf,
277                 cifs_sb->mnt_cifs_flags &
278                         CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
279         if (rc == 0)
280                 cifsFile->invalidHandle = false;
281         /* BB add following call to handle readdir on new NTFS symlink errors
282         else if STATUS_STOPPED_ON_SYMLINK
283                 call get_symlink_reparse_path and retry with new path */
284         else if ((rc == -EOPNOTSUPP) &&
285                 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
286                 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
287                 goto ffirst_retry;
288         }
289 error_exit:
290         kfree(full_path);
291         cifs_put_tlink(tlink);
292         return rc;
293 }
294
295 /* return length of unicode string in bytes */
296 static int cifs_unicode_bytelen(char *str)
297 {
298         int len;
299         __le16 *ustr = (__le16 *)str;
300
301         for (len = 0; len <= PATH_MAX; len++) {
302                 if (ustr[len] == 0)
303                         return len << 1;
304         }
305         cFYI(1, "Unicode string longer than PATH_MAX found");
306         return len << 1;
307 }
308
309 static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
310 {
311         char *new_entry;
312         FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
313
314         if (level == SMB_FIND_FILE_INFO_STANDARD) {
315                 FIND_FILE_STANDARD_INFO *pfData;
316                 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
317
318                 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
319                                 pfData->FileNameLength;
320         } else
321                 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
322         cFYI(1, "new entry %p old entry %p", new_entry, old_entry);
323         /* validate that new_entry is not past end of SMB */
324         if (new_entry >= end_of_smb) {
325                 cERROR(1, "search entry %p began after end of SMB %p old entry %p",
326                         new_entry, end_of_smb, old_entry);
327                 return NULL;
328         } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
329                     (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
330                   || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
331                    (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb)))  {
332                 cERROR(1, "search entry %p extends after end of SMB %p",
333                         new_entry, end_of_smb);
334                 return NULL;
335         } else
336                 return new_entry;
337
338 }
339
340 #define UNICODE_DOT cpu_to_le16(0x2e)
341
342 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
343 static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
344 {
345         int rc = 0;
346         char *filename = NULL;
347         int len = 0;
348
349         if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
350                 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
351                 filename = &pFindData->FileName[0];
352                 if (cfile->srch_inf.unicode) {
353                         len = cifs_unicode_bytelen(filename);
354                 } else {
355                         /* BB should we make this strnlen of PATH_MAX? */
356                         len = strnlen(filename, 5);
357                 }
358         } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
359                 FILE_DIRECTORY_INFO *pFindData =
360                         (FILE_DIRECTORY_INFO *)current_entry;
361                 filename = &pFindData->FileName[0];
362                 len = le32_to_cpu(pFindData->FileNameLength);
363         } else if (cfile->srch_inf.info_level ==
364                         SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
365                 FILE_FULL_DIRECTORY_INFO *pFindData =
366                         (FILE_FULL_DIRECTORY_INFO *)current_entry;
367                 filename = &pFindData->FileName[0];
368                 len = le32_to_cpu(pFindData->FileNameLength);
369         } else if (cfile->srch_inf.info_level ==
370                         SMB_FIND_FILE_ID_FULL_DIR_INFO) {
371                 SEARCH_ID_FULL_DIR_INFO *pFindData =
372                         (SEARCH_ID_FULL_DIR_INFO *)current_entry;
373                 filename = &pFindData->FileName[0];
374                 len = le32_to_cpu(pFindData->FileNameLength);
375         } else if (cfile->srch_inf.info_level ==
376                         SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
377                 FILE_BOTH_DIRECTORY_INFO *pFindData =
378                         (FILE_BOTH_DIRECTORY_INFO *)current_entry;
379                 filename = &pFindData->FileName[0];
380                 len = le32_to_cpu(pFindData->FileNameLength);
381         } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
382                 FIND_FILE_STANDARD_INFO *pFindData =
383                         (FIND_FILE_STANDARD_INFO *)current_entry;
384                 filename = &pFindData->FileName[0];
385                 len = pFindData->FileNameLength;
386         } else {
387                 cFYI(1, "Unknown findfirst level %d",
388                          cfile->srch_inf.info_level);
389         }
390
391         if (filename) {
392                 if (cfile->srch_inf.unicode) {
393                         __le16 *ufilename = (__le16 *)filename;
394                         if (len == 2) {
395                                 /* check for . */
396                                 if (ufilename[0] == UNICODE_DOT)
397                                         rc = 1;
398                         } else if (len == 4) {
399                                 /* check for .. */
400                                 if ((ufilename[0] == UNICODE_DOT)
401                                    && (ufilename[1] == UNICODE_DOT))
402                                         rc = 2;
403                         }
404                 } else /* ASCII */ {
405                         if (len == 1) {
406                                 if (filename[0] == '.')
407                                         rc = 1;
408                         } else if (len == 2) {
409                                 if ((filename[0] == '.') && (filename[1] == '.'))
410                                         rc = 2;
411                         }
412                 }
413         }
414
415         return rc;
416 }
417
418 /* Check if directory that we are searching has changed so we can decide
419    whether we can use the cached search results from the previous search */
420 static int is_dir_changed(struct file *file)
421 {
422         struct inode *inode = file->f_path.dentry->d_inode;
423         struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
424
425         if (cifsInfo->time == 0)
426                 return 1; /* directory was changed, perhaps due to unlink */
427         else
428                 return 0;
429
430 }
431
432 static int cifs_save_resume_key(const char *current_entry,
433         struct cifsFileInfo *cifsFile)
434 {
435         int rc = 0;
436         unsigned int len = 0;
437         __u16 level;
438         char *filename;
439
440         if ((cifsFile == NULL) || (current_entry == NULL))
441                 return -EINVAL;
442
443         level = cifsFile->srch_inf.info_level;
444
445         if (level == SMB_FIND_FILE_UNIX) {
446                 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
447
448                 filename = &pFindData->FileName[0];
449                 if (cifsFile->srch_inf.unicode) {
450                         len = cifs_unicode_bytelen(filename);
451                 } else {
452                         /* BB should we make this strnlen of PATH_MAX? */
453                         len = strnlen(filename, PATH_MAX);
454                 }
455                 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
456         } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
457                 FILE_DIRECTORY_INFO *pFindData =
458                         (FILE_DIRECTORY_INFO *)current_entry;
459                 filename = &pFindData->FileName[0];
460                 len = le32_to_cpu(pFindData->FileNameLength);
461                 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
462         } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
463                 FILE_FULL_DIRECTORY_INFO *pFindData =
464                         (FILE_FULL_DIRECTORY_INFO *)current_entry;
465                 filename = &pFindData->FileName[0];
466                 len = le32_to_cpu(pFindData->FileNameLength);
467                 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
468         } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
469                 SEARCH_ID_FULL_DIR_INFO *pFindData =
470                         (SEARCH_ID_FULL_DIR_INFO *)current_entry;
471                 filename = &pFindData->FileName[0];
472                 len = le32_to_cpu(pFindData->FileNameLength);
473                 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
474         } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
475                 FILE_BOTH_DIRECTORY_INFO *pFindData =
476                         (FILE_BOTH_DIRECTORY_INFO *)current_entry;
477                 filename = &pFindData->FileName[0];
478                 len = le32_to_cpu(pFindData->FileNameLength);
479                 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
480         } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
481                 FIND_FILE_STANDARD_INFO *pFindData =
482                         (FIND_FILE_STANDARD_INFO *)current_entry;
483                 filename = &pFindData->FileName[0];
484                 /* one byte length, no name conversion */
485                 len = (unsigned int)pFindData->FileNameLength;
486                 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
487         } else {
488                 cFYI(1, "Unknown findfirst level %d", level);
489                 return -EINVAL;
490         }
491         cifsFile->srch_inf.resume_name_len = len;
492         cifsFile->srch_inf.presume_name = filename;
493         return rc;
494 }
495
496 /* find the corresponding entry in the search */
497 /* Note that the SMB server returns search entries for . and .. which
498    complicates logic here if we choose to parse for them and we do not
499    assume that they are located in the findfirst return buffer.*/
500 /* We start counting in the buffer with entry 2 and increment for every
501    entry (do not increment for . or .. entry) */
502 static int find_cifs_entry(const int xid, struct cifs_tcon *pTcon,
503         struct file *file, char **ppCurrentEntry, int *num_to_ret)
504 {
505         int rc = 0;
506         int pos_in_buf = 0;
507         loff_t first_entry_in_buffer;
508         loff_t index_to_find = file->f_pos;
509         struct cifsFileInfo *cifsFile = file->private_data;
510         /* check if index in the buffer */
511
512         if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
513            (num_to_ret == NULL))
514                 return -ENOENT;
515
516         *ppCurrentEntry = NULL;
517         first_entry_in_buffer =
518                 cifsFile->srch_inf.index_of_last_entry -
519                         cifsFile->srch_inf.entries_in_buffer;
520
521         /* if first entry in buf is zero then is first buffer
522         in search response data which means it is likely . and ..
523         will be in this buffer, although some servers do not return
524         . and .. for the root of a drive and for those we need
525         to start two entries earlier */
526
527         dump_cifs_file_struct(file, "In fce ");
528         if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
529              is_dir_changed(file)) ||
530            (index_to_find < first_entry_in_buffer)) {
531                 /* close and restart search */
532                 cFYI(1, "search backing up - close and restart search");
533                 spin_lock(&cifs_file_list_lock);
534                 if (!cifsFile->srch_inf.endOfSearch &&
535                     !cifsFile->invalidHandle) {
536                         cifsFile->invalidHandle = true;
537                         spin_unlock(&cifs_file_list_lock);
538                         CIFSFindClose(xid, pTcon, cifsFile->netfid);
539                 } else
540                         spin_unlock(&cifs_file_list_lock);
541                 if (cifsFile->srch_inf.ntwrk_buf_start) {
542                         cFYI(1, "freeing SMB ff cache buf on search rewind");
543                         if (cifsFile->srch_inf.smallBuf)
544                                 cifs_small_buf_release(cifsFile->srch_inf.
545                                                 ntwrk_buf_start);
546                         else
547                                 cifs_buf_release(cifsFile->srch_inf.
548                                                 ntwrk_buf_start);
549                         cifsFile->srch_inf.ntwrk_buf_start = NULL;
550                 }
551                 rc = initiate_cifs_search(xid, file);
552                 if (rc) {
553                         cFYI(1, "error %d reinitiating a search on rewind",
554                                  rc);
555                         return rc;
556                 }
557                 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
558         }
559
560         while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
561               (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
562                 cFYI(1, "calling findnext2");
563                 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
564                                   &cifsFile->srch_inf);
565                 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
566                 if (rc)
567                         return -ENOENT;
568         }
569         if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
570                 /* we found the buffer that contains the entry */
571                 /* scan and find it */
572                 int i;
573                 char *current_entry;
574                 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
575                         smbCalcSize((struct smb_hdr *)
576                                 cifsFile->srch_inf.ntwrk_buf_start);
577
578                 current_entry = cifsFile->srch_inf.srch_entries_start;
579                 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
580                                         - cifsFile->srch_inf.entries_in_buffer;
581                 pos_in_buf = index_to_find - first_entry_in_buffer;
582                 cFYI(1, "found entry - pos_in_buf %d", pos_in_buf);
583
584                 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
585                         /* go entry by entry figuring out which is first */
586                         current_entry = nxt_dir_entry(current_entry, end_of_smb,
587                                                 cifsFile->srch_inf.info_level);
588                 }
589                 if ((current_entry == NULL) && (i < pos_in_buf)) {
590                         /* BB fixme - check if we should flag this error */
591                         cERROR(1, "reached end of buf searching for pos in buf"
592                           " %d index to find %lld rc %d",
593                           pos_in_buf, index_to_find, rc);
594                 }
595                 rc = 0;
596                 *ppCurrentEntry = current_entry;
597         } else {
598                 cFYI(1, "index not in buffer - could not findnext into it");
599                 return 0;
600         }
601
602         if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
603                 cFYI(1, "can not return entries pos_in_buf beyond last");
604                 *num_to_ret = 0;
605         } else
606                 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
607
608         return rc;
609 }
610
611 /* inode num, inode type and filename returned */
612 static int cifs_get_name_from_search_buf(struct qstr *pqst,
613         char *current_entry, __u16 level, unsigned int unicode,
614         struct cifs_sb_info *cifs_sb, unsigned int max_len, __u64 *pinum)
615 {
616         int rc = 0;
617         unsigned int len = 0;
618         char *filename;
619         struct nls_table *nlt = cifs_sb->local_nls;
620
621         *pinum = 0;
622
623         if (level == SMB_FIND_FILE_UNIX) {
624                 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
625
626                 filename = &pFindData->FileName[0];
627                 if (unicode) {
628                         len = cifs_unicode_bytelen(filename);
629                 } else {
630                         /* BB should we make this strnlen of PATH_MAX? */
631                         len = strnlen(filename, PATH_MAX);
632                 }
633
634                 *pinum = le64_to_cpu(pFindData->basic.UniqueId);
635         } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
636                 FILE_DIRECTORY_INFO *pFindData =
637                         (FILE_DIRECTORY_INFO *)current_entry;
638                 filename = &pFindData->FileName[0];
639                 len = le32_to_cpu(pFindData->FileNameLength);
640         } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
641                 FILE_FULL_DIRECTORY_INFO *pFindData =
642                         (FILE_FULL_DIRECTORY_INFO *)current_entry;
643                 filename = &pFindData->FileName[0];
644                 len = le32_to_cpu(pFindData->FileNameLength);
645         } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
646                 SEARCH_ID_FULL_DIR_INFO *pFindData =
647                         (SEARCH_ID_FULL_DIR_INFO *)current_entry;
648                 filename = &pFindData->FileName[0];
649                 len = le32_to_cpu(pFindData->FileNameLength);
650                 *pinum = le64_to_cpu(pFindData->UniqueId);
651         } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
652                 FILE_BOTH_DIRECTORY_INFO *pFindData =
653                         (FILE_BOTH_DIRECTORY_INFO *)current_entry;
654                 filename = &pFindData->FileName[0];
655                 len = le32_to_cpu(pFindData->FileNameLength);
656         } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
657                 FIND_FILE_STANDARD_INFO *pFindData =
658                         (FIND_FILE_STANDARD_INFO *)current_entry;
659                 filename = &pFindData->FileName[0];
660                 /* one byte length, no name conversion */
661                 len = (unsigned int)pFindData->FileNameLength;
662         } else {
663                 cFYI(1, "Unknown findfirst level %d", level);
664                 return -EINVAL;
665         }
666
667         if (len > max_len) {
668                 cERROR(1, "bad search response length %d past smb end", len);
669                 return -EINVAL;
670         }
671
672         if (unicode) {
673                 pqst->len = cifs_from_ucs2((char *) pqst->name,
674                                            (__le16 *) filename,
675                                            UNICODE_NAME_MAX,
676                                            min(len, max_len), nlt,
677                                            cifs_sb->mnt_cifs_flags &
678                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
679                 pqst->len -= nls_nullsize(nlt);
680         } else {
681                 pqst->name = filename;
682                 pqst->len = len;
683         }
684         return rc;
685 }
686
687 static int cifs_filldir(char *pfindEntry, struct file *file, filldir_t filldir,
688                         void *direntry, char *scratch_buf, unsigned int max_len)
689 {
690         int rc = 0;
691         struct qstr qstring;
692         struct cifsFileInfo *pCifsF;
693         u64    inum;
694         ino_t  ino;
695         struct super_block *sb;
696         struct cifs_sb_info *cifs_sb;
697         struct dentry *tmp_dentry;
698         struct cifs_fattr fattr;
699
700         /* get filename and len into qstring */
701         /* get dentry */
702         /* decide whether to create and populate ionde */
703         if ((direntry == NULL) || (file == NULL))
704                 return -EINVAL;
705
706         pCifsF = file->private_data;
707
708         if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
709                 return -ENOENT;
710
711         rc = cifs_entry_is_dot(pfindEntry, pCifsF);
712         /* skip . and .. since we added them first */
713         if (rc != 0)
714                 return 0;
715
716         sb = file->f_path.dentry->d_sb;
717         cifs_sb = CIFS_SB(sb);
718
719         qstring.name = scratch_buf;
720         rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
721                         pCifsF->srch_inf.info_level,
722                         pCifsF->srch_inf.unicode, cifs_sb,
723                         max_len, &inum /* returned */);
724
725         if (rc)
726                 return rc;
727
728         if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
729                 cifs_unix_basic_to_fattr(&fattr,
730                                  &((FILE_UNIX_INFO *) pfindEntry)->basic,
731                                  cifs_sb);
732         else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
733                 cifs_std_info_to_fattr(&fattr, (FIND_FILE_STANDARD_INFO *)
734                                         pfindEntry, cifs_sb);
735         else
736                 cifs_dir_info_to_fattr(&fattr, (FILE_DIRECTORY_INFO *)
737                                         pfindEntry, cifs_sb);
738
739         if (inum && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
740                 fattr.cf_uniqueid = inum;
741         } else {
742                 fattr.cf_uniqueid = iunique(sb, ROOT_I);
743                 cifs_autodisable_serverino(cifs_sb);
744         }
745
746         if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) &&
747             CIFSCouldBeMFSymlink(&fattr))
748                 /*
749                  * trying to get the type and mode can be slow,
750                  * so just call those regular files for now, and mark
751                  * for reval
752                  */
753                 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
754
755         ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
756         tmp_dentry = cifs_readdir_lookup(file->f_dentry, &qstring, &fattr);
757
758         rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
759                      ino, fattr.cf_dtype);
760
761         dput(tmp_dentry);
762         return rc;
763 }
764
765
766 int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
767 {
768         int rc = 0;
769         int xid, i;
770         struct cifs_tcon *pTcon;
771         struct cifsFileInfo *cifsFile = NULL;
772         char *current_entry;
773         int num_to_fill = 0;
774         char *tmp_buf = NULL;
775         char *end_of_smb;
776         unsigned int max_len;
777
778         xid = GetXid();
779
780         /*
781          * Ensure FindFirst doesn't fail before doing filldir() for '.' and
782          * '..'. Otherwise we won't be able to notify VFS in case of failure.
783          */
784         if (file->private_data == NULL) {
785                 rc = initiate_cifs_search(xid, file);
786                 cFYI(1, "initiate cifs search rc %d", rc);
787                 if (rc)
788                         goto rddir2_exit;
789         }
790
791         switch ((int) file->f_pos) {
792         case 0:
793                 if (filldir(direntry, ".", 1, file->f_pos,
794                      file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
795                         cERROR(1, "Filldir for current dir failed");
796                         rc = -ENOMEM;
797                         break;
798                 }
799                 file->f_pos++;
800         case 1:
801                 if (filldir(direntry, "..", 2, file->f_pos,
802                      file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
803                         cERROR(1, "Filldir for parent dir failed");
804                         rc = -ENOMEM;
805                         break;
806                 }
807                 file->f_pos++;
808         default:
809                 /* 1) If search is active,
810                         is in current search buffer?
811                         if it before then restart search
812                         if after then keep searching till find it */
813
814                 if (file->private_data == NULL) {
815                         rc = -EINVAL;
816                         FreeXid(xid);
817                         return rc;
818                 }
819                 cifsFile = file->private_data;
820                 if (cifsFile->srch_inf.endOfSearch) {
821                         if (cifsFile->srch_inf.emptyDir) {
822                                 cFYI(1, "End of search, empty dir");
823                                 rc = 0;
824                                 break;
825                         }
826                 } /* else {
827                         cifsFile->invalidHandle = true;
828                         CIFSFindClose(xid, pTcon, cifsFile->netfid);
829                 } */
830
831                 pTcon = tlink_tcon(cifsFile->tlink);
832                 rc = find_cifs_entry(xid, pTcon, file,
833                                 &current_entry, &num_to_fill);
834                 if (rc) {
835                         cFYI(1, "fce error %d", rc);
836                         goto rddir2_exit;
837                 } else if (current_entry != NULL) {
838                         cFYI(1, "entry %lld found", file->f_pos);
839                 } else {
840                         cFYI(1, "could not find entry");
841                         goto rddir2_exit;
842                 }
843                 cFYI(1, "loop through %d times filling dir for net buf %p",
844                         num_to_fill, cifsFile->srch_inf.ntwrk_buf_start);
845                 max_len = smbCalcSize((struct smb_hdr *)
846                                 cifsFile->srch_inf.ntwrk_buf_start);
847                 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
848
849                 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
850                 if (tmp_buf == NULL) {
851                         rc = -ENOMEM;
852                         break;
853                 }
854
855                 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
856                         if (current_entry == NULL) {
857                                 /* evaluate whether this case is an error */
858                                 cERROR(1, "past SMB end,  num to fill %d i %d",
859                                           num_to_fill, i);
860                                 break;
861                         }
862                         /* if buggy server returns . and .. late do
863                         we want to check for that here? */
864                         rc = cifs_filldir(current_entry, file,
865                                         filldir, direntry, tmp_buf, max_len);
866                         if (rc == -EOVERFLOW) {
867                                 rc = 0;
868                                 break;
869                         }
870
871                         file->f_pos++;
872                         if (file->f_pos ==
873                                 cifsFile->srch_inf.index_of_last_entry) {
874                                 cFYI(1, "last entry in buf at pos %lld %s",
875                                         file->f_pos, tmp_buf);
876                                 cifs_save_resume_key(current_entry, cifsFile);
877                                 break;
878                         } else
879                                 current_entry =
880                                         nxt_dir_entry(current_entry, end_of_smb,
881                                                 cifsFile->srch_inf.info_level);
882                 }
883                 kfree(tmp_buf);
884                 break;
885         } /* end switch */
886
887 rddir2_exit:
888         FreeXid(xid);
889         return rc;
890 }