Merge branch 'bugzilla-21212' into release
[firefly-linux-kernel-4.4.55.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, name);
83         else
84                 name->hash = full_name_hash(name->name, name->len);
85
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);
93         }
94
95         dentry = d_alloc(parent, name);
96         if (dentry == NULL)
97                 return NULL;
98
99         inode = cifs_iget(sb, fattr);
100         if (!inode) {
101                 dput(dentry);
102                 return NULL;
103         }
104
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;
109
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;
116         }
117
118         return dentry;
119 }
120
121 static void
122 cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
123 {
124         fattr->cf_uid = cifs_sb->mnt_uid;
125         fattr->cf_gid = cifs_sb->mnt_gid;
126
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;
133         }
134
135         if (fattr->cf_cifsattrs & ATTR_READONLY)
136                 fattr->cf_mode &= ~S_IWUGO;
137
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 {
145                         /*
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
149                          */
150                         fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
151                 }
152         }
153 }
154
155 static void
156 cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info,
157                        struct cifs_sb_info *cifs_sb)
158 {
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);
166
167         cifs_fill_common_info(fattr, cifs_sb);
168 }
169
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)
173 {
174         int offset = cifs_sb_master_tcon(cifs_sb)->ses->server->timeAdj;
175
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);
183
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);
187
188         cifs_fill_common_info(fattr, cifs_sb);
189 }
190
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 */
194 /*
195 int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
196                              int xid)
197 {
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;
204
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");
218                 }
219         }
220 }
221  */
222
223 static int initiate_cifs_search(const int xid, struct file *file)
224 {
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 = NULL;
230         struct cifsTconInfo *pTcon;
231
232         if (file->private_data == NULL) {
233                 tlink = cifs_sb_tlink(cifs_sb);
234                 if (IS_ERR(tlink))
235                         return PTR_ERR(tlink);
236
237                 cifsFile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
238                 if (cifsFile == NULL) {
239                         rc = -ENOMEM;
240                         goto error_exit;
241                 }
242                 file->private_data = cifsFile;
243                 cifsFile->tlink = cifs_get_tlink(tlink);
244                 pTcon = tlink_tcon(tlink);
245         } else {
246                 cifsFile = file->private_data;
247                 pTcon = tlink_tcon(cifsFile->tlink);
248         }
249
250         cifsFile->invalidHandle = true;
251         cifsFile->srch_inf.endOfSearch = false;
252
253         full_path = build_path_from_dentry(file->f_path.dentry);
254         if (full_path == NULL) {
255                 rc = -ENOMEM;
256                 goto error_exit;
257         }
258
259         cFYI(1, "Full path: %s start at: %lld", full_path, file->f_pos);
260
261 ffirst_retry:
262         /* test for Unix extensions */
263         /* but now check for them on the share/mount not on the SMB session */
264 /*      if (pTcon->ses->capabilities & CAP_UNIX) { */
265         if (pTcon->unix_ext)
266                 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
267         else if ((pTcon->ses->capabilities &
268                         (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
269                 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
270         } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
271                 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
272         } else /* not srvinos - BB fixme add check for backlevel? */ {
273                 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
274         }
275
276         rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
277                 &cifsFile->netfid, &cifsFile->srch_inf,
278                 cifs_sb->mnt_cifs_flags &
279                         CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
280         if (rc == 0)
281                 cifsFile->invalidHandle = false;
282         /* BB add following call to handle readdir on new NTFS symlink errors
283         else if STATUS_STOPPED_ON_SYMLINK
284                 call get_symlink_reparse_path and retry with new path */
285         else if ((rc == -EOPNOTSUPP) &&
286                 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
287                 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
288                 goto ffirst_retry;
289         }
290 error_exit:
291         kfree(full_path);
292         cifs_put_tlink(tlink);
293         return rc;
294 }
295
296 /* return length of unicode string in bytes */
297 static int cifs_unicode_bytelen(char *str)
298 {
299         int len;
300         __le16 *ustr = (__le16 *)str;
301
302         for (len = 0; len <= PATH_MAX; len++) {
303                 if (ustr[len] == 0)
304                         return len << 1;
305         }
306         cFYI(1, "Unicode string longer than PATH_MAX found");
307         return len << 1;
308 }
309
310 static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
311 {
312         char *new_entry;
313         FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
314
315         if (level == SMB_FIND_FILE_INFO_STANDARD) {
316                 FIND_FILE_STANDARD_INFO *pfData;
317                 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
318
319                 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
320                                 pfData->FileNameLength;
321         } else
322                 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
323         cFYI(1, "new entry %p old entry %p", new_entry, old_entry);
324         /* validate that new_entry is not past end of SMB */
325         if (new_entry >= end_of_smb) {
326                 cERROR(1, "search entry %p began after end of SMB %p old entry %p",
327                         new_entry, end_of_smb, old_entry);
328                 return NULL;
329         } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
330                     (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
331                   || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
332                    (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb)))  {
333                 cERROR(1, "search entry %p extends after end of SMB %p",
334                         new_entry, end_of_smb);
335                 return NULL;
336         } else
337                 return new_entry;
338
339 }
340
341 #define UNICODE_DOT cpu_to_le16(0x2e)
342
343 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
344 static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
345 {
346         int rc = 0;
347         char *filename = NULL;
348         int len = 0;
349
350         if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
351                 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
352                 filename = &pFindData->FileName[0];
353                 if (cfile->srch_inf.unicode) {
354                         len = cifs_unicode_bytelen(filename);
355                 } else {
356                         /* BB should we make this strnlen of PATH_MAX? */
357                         len = strnlen(filename, 5);
358                 }
359         } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
360                 FILE_DIRECTORY_INFO *pFindData =
361                         (FILE_DIRECTORY_INFO *)current_entry;
362                 filename = &pFindData->FileName[0];
363                 len = le32_to_cpu(pFindData->FileNameLength);
364         } else if (cfile->srch_inf.info_level ==
365                         SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
366                 FILE_FULL_DIRECTORY_INFO *pFindData =
367                         (FILE_FULL_DIRECTORY_INFO *)current_entry;
368                 filename = &pFindData->FileName[0];
369                 len = le32_to_cpu(pFindData->FileNameLength);
370         } else if (cfile->srch_inf.info_level ==
371                         SMB_FIND_FILE_ID_FULL_DIR_INFO) {
372                 SEARCH_ID_FULL_DIR_INFO *pFindData =
373                         (SEARCH_ID_FULL_DIR_INFO *)current_entry;
374                 filename = &pFindData->FileName[0];
375                 len = le32_to_cpu(pFindData->FileNameLength);
376         } else if (cfile->srch_inf.info_level ==
377                         SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
378                 FILE_BOTH_DIRECTORY_INFO *pFindData =
379                         (FILE_BOTH_DIRECTORY_INFO *)current_entry;
380                 filename = &pFindData->FileName[0];
381                 len = le32_to_cpu(pFindData->FileNameLength);
382         } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
383                 FIND_FILE_STANDARD_INFO *pFindData =
384                         (FIND_FILE_STANDARD_INFO *)current_entry;
385                 filename = &pFindData->FileName[0];
386                 len = pFindData->FileNameLength;
387         } else {
388                 cFYI(1, "Unknown findfirst level %d",
389                          cfile->srch_inf.info_level);
390         }
391
392         if (filename) {
393                 if (cfile->srch_inf.unicode) {
394                         __le16 *ufilename = (__le16 *)filename;
395                         if (len == 2) {
396                                 /* check for . */
397                                 if (ufilename[0] == UNICODE_DOT)
398                                         rc = 1;
399                         } else if (len == 4) {
400                                 /* check for .. */
401                                 if ((ufilename[0] == UNICODE_DOT)
402                                    && (ufilename[1] == UNICODE_DOT))
403                                         rc = 2;
404                         }
405                 } else /* ASCII */ {
406                         if (len == 1) {
407                                 if (filename[0] == '.')
408                                         rc = 1;
409                         } else if (len == 2) {
410                                 if ((filename[0] == '.') && (filename[1] == '.'))
411                                         rc = 2;
412                         }
413                 }
414         }
415
416         return rc;
417 }
418
419 /* Check if directory that we are searching has changed so we can decide
420    whether we can use the cached search results from the previous search */
421 static int is_dir_changed(struct file *file)
422 {
423         struct inode *inode = file->f_path.dentry->d_inode;
424         struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
425
426         if (cifsInfo->time == 0)
427                 return 1; /* directory was changed, perhaps due to unlink */
428         else
429                 return 0;
430
431 }
432
433 static int cifs_save_resume_key(const char *current_entry,
434         struct cifsFileInfo *cifsFile)
435 {
436         int rc = 0;
437         unsigned int len = 0;
438         __u16 level;
439         char *filename;
440
441         if ((cifsFile == NULL) || (current_entry == NULL))
442                 return -EINVAL;
443
444         level = cifsFile->srch_inf.info_level;
445
446         if (level == SMB_FIND_FILE_UNIX) {
447                 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
448
449                 filename = &pFindData->FileName[0];
450                 if (cifsFile->srch_inf.unicode) {
451                         len = cifs_unicode_bytelen(filename);
452                 } else {
453                         /* BB should we make this strnlen of PATH_MAX? */
454                         len = strnlen(filename, PATH_MAX);
455                 }
456                 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
457         } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
458                 FILE_DIRECTORY_INFO *pFindData =
459                         (FILE_DIRECTORY_INFO *)current_entry;
460                 filename = &pFindData->FileName[0];
461                 len = le32_to_cpu(pFindData->FileNameLength);
462                 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
463         } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
464                 FILE_FULL_DIRECTORY_INFO *pFindData =
465                         (FILE_FULL_DIRECTORY_INFO *)current_entry;
466                 filename = &pFindData->FileName[0];
467                 len = le32_to_cpu(pFindData->FileNameLength);
468                 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
469         } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
470                 SEARCH_ID_FULL_DIR_INFO *pFindData =
471                         (SEARCH_ID_FULL_DIR_INFO *)current_entry;
472                 filename = &pFindData->FileName[0];
473                 len = le32_to_cpu(pFindData->FileNameLength);
474                 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
475         } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
476                 FILE_BOTH_DIRECTORY_INFO *pFindData =
477                         (FILE_BOTH_DIRECTORY_INFO *)current_entry;
478                 filename = &pFindData->FileName[0];
479                 len = le32_to_cpu(pFindData->FileNameLength);
480                 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
481         } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
482                 FIND_FILE_STANDARD_INFO *pFindData =
483                         (FIND_FILE_STANDARD_INFO *)current_entry;
484                 filename = &pFindData->FileName[0];
485                 /* one byte length, no name conversion */
486                 len = (unsigned int)pFindData->FileNameLength;
487                 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
488         } else {
489                 cFYI(1, "Unknown findfirst level %d", level);
490                 return -EINVAL;
491         }
492         cifsFile->srch_inf.resume_name_len = len;
493         cifsFile->srch_inf.presume_name = filename;
494         return rc;
495 }
496
497 /* find the corresponding entry in the search */
498 /* Note that the SMB server returns search entries for . and .. which
499    complicates logic here if we choose to parse for them and we do not
500    assume that they are located in the findfirst return buffer.*/
501 /* We start counting in the buffer with entry 2 and increment for every
502    entry (do not increment for . or .. entry) */
503 static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
504         struct file *file, char **ppCurrentEntry, int *num_to_ret)
505 {
506         int rc = 0;
507         int pos_in_buf = 0;
508         loff_t first_entry_in_buffer;
509         loff_t index_to_find = file->f_pos;
510         struct cifsFileInfo *cifsFile = file->private_data;
511         /* check if index in the buffer */
512
513         if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
514            (num_to_ret == NULL))
515                 return -ENOENT;
516
517         *ppCurrentEntry = NULL;
518         first_entry_in_buffer =
519                 cifsFile->srch_inf.index_of_last_entry -
520                         cifsFile->srch_inf.entries_in_buffer;
521
522         /* if first entry in buf is zero then is first buffer
523         in search response data which means it is likely . and ..
524         will be in this buffer, although some servers do not return
525         . and .. for the root of a drive and for those we need
526         to start two entries earlier */
527
528         dump_cifs_file_struct(file, "In fce ");
529         if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
530              is_dir_changed(file)) ||
531            (index_to_find < first_entry_in_buffer)) {
532                 /* close and restart search */
533                 cFYI(1, "search backing up - close and restart search");
534                 spin_lock(&cifs_file_list_lock);
535                 if (!cifsFile->srch_inf.endOfSearch &&
536                     !cifsFile->invalidHandle) {
537                         cifsFile->invalidHandle = true;
538                         spin_unlock(&cifs_file_list_lock);
539                         CIFSFindClose(xid, pTcon, cifsFile->netfid);
540                 } else
541                         spin_unlock(&cifs_file_list_lock);
542                 if (cifsFile->srch_inf.ntwrk_buf_start) {
543                         cFYI(1, "freeing SMB ff cache buf on search rewind");
544                         if (cifsFile->srch_inf.smallBuf)
545                                 cifs_small_buf_release(cifsFile->srch_inf.
546                                                 ntwrk_buf_start);
547                         else
548                                 cifs_buf_release(cifsFile->srch_inf.
549                                                 ntwrk_buf_start);
550                         cifsFile->srch_inf.ntwrk_buf_start = NULL;
551                 }
552                 rc = initiate_cifs_search(xid, file);
553                 if (rc) {
554                         cFYI(1, "error %d reinitiating a search on rewind",
555                                  rc);
556                         return rc;
557                 }
558                 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
559         }
560
561         while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
562               (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
563                 cFYI(1, "calling findnext2");
564                 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
565                                   &cifsFile->srch_inf);
566                 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
567                 if (rc)
568                         return -ENOENT;
569         }
570         if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
571                 /* we found the buffer that contains the entry */
572                 /* scan and find it */
573                 int i;
574                 char *current_entry;
575                 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
576                         smbCalcSize((struct smb_hdr *)
577                                 cifsFile->srch_inf.ntwrk_buf_start);
578
579                 current_entry = cifsFile->srch_inf.srch_entries_start;
580                 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
581                                         - cifsFile->srch_inf.entries_in_buffer;
582                 pos_in_buf = index_to_find - first_entry_in_buffer;
583                 cFYI(1, "found entry - pos_in_buf %d", pos_in_buf);
584
585                 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
586                         /* go entry by entry figuring out which is first */
587                         current_entry = nxt_dir_entry(current_entry, end_of_smb,
588                                                 cifsFile->srch_inf.info_level);
589                 }
590                 if ((current_entry == NULL) && (i < pos_in_buf)) {
591                         /* BB fixme - check if we should flag this error */
592                         cERROR(1, "reached end of buf searching for pos in buf"
593                           " %d index to find %lld rc %d",
594                           pos_in_buf, index_to_find, rc);
595                 }
596                 rc = 0;
597                 *ppCurrentEntry = current_entry;
598         } else {
599                 cFYI(1, "index not in buffer - could not findnext into it");
600                 return 0;
601         }
602
603         if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
604                 cFYI(1, "can not return entries pos_in_buf beyond last");
605                 *num_to_ret = 0;
606         } else
607                 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
608
609         return rc;
610 }
611
612 /* inode num, inode type and filename returned */
613 static int cifs_get_name_from_search_buf(struct qstr *pqst,
614         char *current_entry, __u16 level, unsigned int unicode,
615         struct cifs_sb_info *cifs_sb, unsigned int max_len, __u64 *pinum)
616 {
617         int rc = 0;
618         unsigned int len = 0;
619         char *filename;
620         struct nls_table *nlt = cifs_sb->local_nls;
621
622         *pinum = 0;
623
624         if (level == SMB_FIND_FILE_UNIX) {
625                 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
626
627                 filename = &pFindData->FileName[0];
628                 if (unicode) {
629                         len = cifs_unicode_bytelen(filename);
630                 } else {
631                         /* BB should we make this strnlen of PATH_MAX? */
632                         len = strnlen(filename, PATH_MAX);
633                 }
634
635                 *pinum = le64_to_cpu(pFindData->basic.UniqueId);
636         } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
637                 FILE_DIRECTORY_INFO *pFindData =
638                         (FILE_DIRECTORY_INFO *)current_entry;
639                 filename = &pFindData->FileName[0];
640                 len = le32_to_cpu(pFindData->FileNameLength);
641         } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
642                 FILE_FULL_DIRECTORY_INFO *pFindData =
643                         (FILE_FULL_DIRECTORY_INFO *)current_entry;
644                 filename = &pFindData->FileName[0];
645                 len = le32_to_cpu(pFindData->FileNameLength);
646         } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
647                 SEARCH_ID_FULL_DIR_INFO *pFindData =
648                         (SEARCH_ID_FULL_DIR_INFO *)current_entry;
649                 filename = &pFindData->FileName[0];
650                 len = le32_to_cpu(pFindData->FileNameLength);
651                 *pinum = le64_to_cpu(pFindData->UniqueId);
652         } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
653                 FILE_BOTH_DIRECTORY_INFO *pFindData =
654                         (FILE_BOTH_DIRECTORY_INFO *)current_entry;
655                 filename = &pFindData->FileName[0];
656                 len = le32_to_cpu(pFindData->FileNameLength);
657         } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
658                 FIND_FILE_STANDARD_INFO *pFindData =
659                         (FIND_FILE_STANDARD_INFO *)current_entry;
660                 filename = &pFindData->FileName[0];
661                 /* one byte length, no name conversion */
662                 len = (unsigned int)pFindData->FileNameLength;
663         } else {
664                 cFYI(1, "Unknown findfirst level %d", level);
665                 return -EINVAL;
666         }
667
668         if (len > max_len) {
669                 cERROR(1, "bad search response length %d past smb end", len);
670                 return -EINVAL;
671         }
672
673         if (unicode) {
674                 pqst->len = cifs_from_ucs2((char *) pqst->name,
675                                            (__le16 *) filename,
676                                            UNICODE_NAME_MAX,
677                                            min(len, max_len), nlt,
678                                            cifs_sb->mnt_cifs_flags &
679                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
680                 pqst->len -= nls_nullsize(nlt);
681         } else {
682                 pqst->name = filename;
683                 pqst->len = len;
684         }
685         return rc;
686 }
687
688 static int cifs_filldir(char *pfindEntry, struct file *file, filldir_t filldir,
689                         void *direntry, char *scratch_buf, unsigned int max_len)
690 {
691         int rc = 0;
692         struct qstr qstring;
693         struct cifsFileInfo *pCifsF;
694         u64    inum;
695         ino_t  ino;
696         struct super_block *sb;
697         struct cifs_sb_info *cifs_sb;
698         struct dentry *tmp_dentry;
699         struct cifs_fattr fattr;
700
701         /* get filename and len into qstring */
702         /* get dentry */
703         /* decide whether to create and populate ionde */
704         if ((direntry == NULL) || (file == NULL))
705                 return -EINVAL;
706
707         pCifsF = file->private_data;
708
709         if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
710                 return -ENOENT;
711
712         rc = cifs_entry_is_dot(pfindEntry, pCifsF);
713         /* skip . and .. since we added them first */
714         if (rc != 0)
715                 return 0;
716
717         sb = file->f_path.dentry->d_sb;
718         cifs_sb = CIFS_SB(sb);
719
720         qstring.name = scratch_buf;
721         rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
722                         pCifsF->srch_inf.info_level,
723                         pCifsF->srch_inf.unicode, cifs_sb,
724                         max_len, &inum /* returned */);
725
726         if (rc)
727                 return rc;
728
729         if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
730                 cifs_unix_basic_to_fattr(&fattr,
731                                  &((FILE_UNIX_INFO *) pfindEntry)->basic,
732                                  cifs_sb);
733         else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
734                 cifs_std_info_to_fattr(&fattr, (FIND_FILE_STANDARD_INFO *)
735                                         pfindEntry, cifs_sb);
736         else
737                 cifs_dir_info_to_fattr(&fattr, (FILE_DIRECTORY_INFO *)
738                                         pfindEntry, cifs_sb);
739
740         if (inum && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
741                 fattr.cf_uniqueid = inum;
742         } else {
743                 fattr.cf_uniqueid = iunique(sb, ROOT_I);
744                 cifs_autodisable_serverino(cifs_sb);
745         }
746
747         if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) &&
748             CIFSCouldBeMFSymlink(&fattr))
749                 /*
750                  * trying to get the type and mode can be slow,
751                  * so just call those regular files for now, and mark
752                  * for reval
753                  */
754                 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
755
756         ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
757         tmp_dentry = cifs_readdir_lookup(file->f_dentry, &qstring, &fattr);
758
759         rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
760                      ino, fattr.cf_dtype);
761
762         dput(tmp_dentry);
763         return rc;
764 }
765
766
767 int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
768 {
769         int rc = 0;
770         int xid, i;
771         struct cifs_sb_info *cifs_sb;
772         struct cifsTconInfo *pTcon;
773         struct cifsFileInfo *cifsFile = NULL;
774         char *current_entry;
775         int num_to_fill = 0;
776         char *tmp_buf = NULL;
777         char *end_of_smb;
778         unsigned int max_len;
779
780         xid = GetXid();
781
782         cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
783
784         /*
785          * Ensure FindFirst doesn't fail before doing filldir() for '.' and
786          * '..'. Otherwise we won't be able to notify VFS in case of failure.
787          */
788         if (file->private_data == NULL) {
789                 rc = initiate_cifs_search(xid, file);
790                 cFYI(1, "initiate cifs search rc %d", rc);
791                 if (rc)
792                         goto rddir2_exit;
793         }
794
795         switch ((int) file->f_pos) {
796         case 0:
797                 if (filldir(direntry, ".", 1, file->f_pos,
798                      file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
799                         cERROR(1, "Filldir for current dir failed");
800                         rc = -ENOMEM;
801                         break;
802                 }
803                 file->f_pos++;
804         case 1:
805                 if (filldir(direntry, "..", 2, file->f_pos,
806                      file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
807                         cERROR(1, "Filldir for parent dir failed");
808                         rc = -ENOMEM;
809                         break;
810                 }
811                 file->f_pos++;
812         default:
813                 /* 1) If search is active,
814                         is in current search buffer?
815                         if it before then restart search
816                         if after then keep searching till find it */
817
818                 if (file->private_data == NULL) {
819                         rc = -EINVAL;
820                         FreeXid(xid);
821                         return rc;
822                 }
823                 cifsFile = file->private_data;
824                 if (cifsFile->srch_inf.endOfSearch) {
825                         if (cifsFile->srch_inf.emptyDir) {
826                                 cFYI(1, "End of search, empty dir");
827                                 rc = 0;
828                                 break;
829                         }
830                 } /* else {
831                         cifsFile->invalidHandle = true;
832                         CIFSFindClose(xid, pTcon, cifsFile->netfid);
833                 } */
834
835                 pTcon = tlink_tcon(cifsFile->tlink);
836                 rc = find_cifs_entry(xid, pTcon, file,
837                                 &current_entry, &num_to_fill);
838                 if (rc) {
839                         cFYI(1, "fce error %d", rc);
840                         goto rddir2_exit;
841                 } else if (current_entry != NULL) {
842                         cFYI(1, "entry %lld found", file->f_pos);
843                 } else {
844                         cFYI(1, "could not find entry");
845                         goto rddir2_exit;
846                 }
847                 cFYI(1, "loop through %d times filling dir for net buf %p",
848                         num_to_fill, cifsFile->srch_inf.ntwrk_buf_start);
849                 max_len = smbCalcSize((struct smb_hdr *)
850                                 cifsFile->srch_inf.ntwrk_buf_start);
851                 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
852
853                 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
854                 if (tmp_buf == NULL) {
855                         rc = -ENOMEM;
856                         break;
857                 }
858
859                 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
860                         if (current_entry == NULL) {
861                                 /* evaluate whether this case is an error */
862                                 cERROR(1, "past SMB end,  num to fill %d i %d",
863                                           num_to_fill, i);
864                                 break;
865                         }
866                         /* if buggy server returns . and .. late do
867                         we want to check for that here? */
868                         rc = cifs_filldir(current_entry, file,
869                                         filldir, direntry, tmp_buf, max_len);
870                         if (rc == -EOVERFLOW) {
871                                 rc = 0;
872                                 break;
873                         }
874
875                         file->f_pos++;
876                         if (file->f_pos ==
877                                 cifsFile->srch_inf.index_of_last_entry) {
878                                 cFYI(1, "last entry in buf at pos %lld %s",
879                                         file->f_pos, tmp_buf);
880                                 cifs_save_resume_key(current_entry, cifsFile);
881                                 break;
882                         } else
883                                 current_entry =
884                                         nxt_dir_entry(current_entry, end_of_smb,
885                                                 cifsFile->srch_inf.info_level);
886                 }
887                 kfree(tmp_buf);
888                 break;
889         } /* end switch */
890
891 rddir2_exit:
892         FreeXid(xid);
893         return rc;
894 }