cifs: add new cifs_iget function and convert unix codepath to use it
[pandora-kernel.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/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"
33
34 /*
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.
38  */
39 #define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
40
41 #ifdef CONFIG_CIFS_DEBUG2
42 static void dump_cifs_file_struct(struct file *file, char *label)
43 {
44         struct cifsFileInfo *cf;
45
46         if (file) {
47                 cf = file->private_data;
48                 if (cf == NULL) {
49                         cFYI(1, ("empty cifs private file data"));
50                         return;
51                 }
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"));
58         }
59 }
60 #else
61 static inline void dump_cifs_file_struct(struct file *file, char *label)
62 {
63 }
64 #endif /* DEBUG2 */
65
66 /*
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.
69  */
70 static struct dentry *
71 cifs_readdir_lookup(struct dentry *parent, struct qstr *name,
72                     struct cifs_fattr *fattr)
73 {
74         struct dentry *dentry, *alias;
75         struct inode *inode;
76         struct super_block *sb = parent->d_inode->i_sb;
77
78         cFYI(1, ("For %s", name->name));
79
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);
87         }
88
89         dentry = d_alloc(parent, name);
90         if (dentry == NULL)
91                 return NULL;
92
93         inode = cifs_iget(sb, fattr);
94         if (!inode) {
95                 dput(dentry);
96                 return NULL;
97         }
98
99         if (CIFS_SB(sb)->tcon->nocase)
100                 dentry->d_op = &cifs_ci_dentry_ops;
101         else
102                 dentry->d_op = &cifs_dentry_ops;
103
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;
110         }
111
112         return dentry;
113 }
114
115 /* Returns 1 if new inode created, 2 if both dentry and inode were */
116 /* Might check in the future if inode number changed so we can rehash inode */
117 static int
118 construct_dentry(struct qstr *qstring, struct file *file,
119                  struct inode **ptmp_inode, struct dentry **pnew_dentry,
120                  __u64 *inum)
121 {
122         struct dentry *tmp_dentry = NULL;
123         struct super_block *sb = file->f_path.dentry->d_sb;
124         int rc = 0;
125
126         cFYI(1, ("For %s", qstring->name));
127
128         tmp_dentry = d_lookup(file->f_path.dentry, qstring);
129         if (tmp_dentry) {
130                 /* BB: overwrite old name? i.e. tmp_dentry->d_name and
131                  * tmp_dentry->d_name.len??
132                  */
133                 cFYI(0, ("existing dentry with inode 0x%p",
134                          tmp_dentry->d_inode));
135                 *ptmp_inode = tmp_dentry->d_inode;
136                 if (*ptmp_inode == NULL) {
137                         *ptmp_inode = cifs_new_inode(sb, inum);
138                         if (*ptmp_inode == NULL)
139                                 return rc;
140                         rc = 1;
141                 }
142         } else {
143                 tmp_dentry = d_alloc(file->f_path.dentry, qstring);
144                 if (tmp_dentry == NULL) {
145                         cERROR(1, ("Failed allocating dentry"));
146                         *ptmp_inode = NULL;
147                         return rc;
148                 }
149
150                 if (CIFS_SB(sb)->tcon->nocase)
151                         tmp_dentry->d_op = &cifs_ci_dentry_ops;
152                 else
153                         tmp_dentry->d_op = &cifs_dentry_ops;
154
155                 *ptmp_inode = cifs_new_inode(sb, inum);
156                 if (*ptmp_inode == NULL)
157                         return rc;
158                 rc = 2;
159         }
160
161         tmp_dentry->d_time = jiffies;
162         *pnew_dentry = tmp_dentry;
163         return rc;
164 }
165
166 static void fill_in_inode(struct inode *tmp_inode, int new_buf_type,
167                           char *buf, unsigned int *pobject_type, int isNewInode)
168 {
169         loff_t local_size;
170         struct timespec local_mtime;
171
172         struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
173         struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
174         __u32 attr;
175         __u64 allocation_size;
176         __u64 end_of_file;
177         umode_t default_mode;
178
179         /* save mtime and size */
180         local_mtime = tmp_inode->i_mtime;
181         local_size  = tmp_inode->i_size;
182
183         if (new_buf_type) {
184                 FILE_DIRECTORY_INFO *pfindData = (FILE_DIRECTORY_INFO *)buf;
185
186                 attr = le32_to_cpu(pfindData->ExtFileAttributes);
187                 allocation_size = le64_to_cpu(pfindData->AllocationSize);
188                 end_of_file = le64_to_cpu(pfindData->EndOfFile);
189                 tmp_inode->i_atime =
190                         cifs_NTtimeToUnix(pfindData->LastAccessTime);
191                 tmp_inode->i_mtime =
192                         cifs_NTtimeToUnix(pfindData->LastWriteTime);
193                 tmp_inode->i_ctime =
194                         cifs_NTtimeToUnix(pfindData->ChangeTime);
195         } else { /* legacy, OS2 and DOS style */
196                 int offset = cifs_sb->tcon->ses->server->timeAdj;
197                 FIND_FILE_STANDARD_INFO *pfindData =
198                         (FIND_FILE_STANDARD_INFO *)buf;
199
200                 tmp_inode->i_mtime = cnvrtDosUnixTm(pfindData->LastWriteDate,
201                                                     pfindData->LastWriteTime,
202                                                     offset);
203                 tmp_inode->i_atime = cnvrtDosUnixTm(pfindData->LastAccessDate,
204                                                     pfindData->LastAccessTime,
205                                                     offset);
206                 tmp_inode->i_ctime = cnvrtDosUnixTm(pfindData->LastWriteDate,
207                                                     pfindData->LastWriteTime,
208                                                     offset);
209                 attr = le16_to_cpu(pfindData->Attributes);
210                 allocation_size = le32_to_cpu(pfindData->AllocationSize);
211                 end_of_file = le32_to_cpu(pfindData->DataSize);
212         }
213
214         /* Linux can not store file creation time unfortunately so ignore it */
215
216         cifsInfo->cifsAttrs = attr;
217 #ifdef CONFIG_CIFS_EXPERIMENTAL
218         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
219                 /* get more accurate mode via ACL - so force inode refresh */
220                 cifsInfo->time = 0;
221         } else
222 #endif /* CONFIG_CIFS_EXPERIMENTAL */
223                 cifsInfo->time = jiffies;
224
225         /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
226         /* 2767 perms - indicate mandatory locking */
227                 /* BB fill in uid and gid here? with help from winbind?
228                    or retrieve from NTFS stream extended attribute */
229         if (atomic_read(&cifsInfo->inUse) == 0) {
230                 tmp_inode->i_uid = cifs_sb->mnt_uid;
231                 tmp_inode->i_gid = cifs_sb->mnt_gid;
232         }
233
234         if (attr & ATTR_DIRECTORY)
235                 default_mode = cifs_sb->mnt_dir_mode;
236         else
237                 default_mode = cifs_sb->mnt_file_mode;
238
239         /* set initial permissions */
240         if ((atomic_read(&cifsInfo->inUse) == 0) ||
241             (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
242                 tmp_inode->i_mode = default_mode;
243         else {
244                 /* just reenable write bits if !ATTR_READONLY */
245                 if ((tmp_inode->i_mode & S_IWUGO) == 0 &&
246                     (attr & ATTR_READONLY) == 0)
247                         tmp_inode->i_mode |= (S_IWUGO & default_mode);
248
249                 tmp_inode->i_mode &= ~S_IFMT;
250         }
251
252         /* clear write bits if ATTR_READONLY is set */
253         if (attr & ATTR_READONLY)
254                 tmp_inode->i_mode &= ~S_IWUGO;
255
256         /* set inode type */
257         if ((attr & ATTR_SYSTEM) &&
258             (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
259                 if (end_of_file == 0)  {
260                         tmp_inode->i_mode |= S_IFIFO;
261                         *pobject_type = DT_FIFO;
262                 } else {
263                         /*
264                          * trying to get the type can be slow, so just call
265                          * this a regular file for now, and mark for reval
266                          */
267                         tmp_inode->i_mode |= S_IFREG;
268                         *pobject_type = DT_REG;
269                         cifsInfo->time = 0;
270                 }
271         } else {
272                 if (attr & ATTR_DIRECTORY) {
273                         tmp_inode->i_mode |= S_IFDIR;
274                         *pobject_type = DT_DIR;
275                 } else {
276                         tmp_inode->i_mode |= S_IFREG;
277                         *pobject_type = DT_REG;
278                 }
279         }
280
281         /* can not fill in nlink here as in qpathinfo version and Unx search */
282         if (atomic_read(&cifsInfo->inUse) == 0)
283                 atomic_set(&cifsInfo->inUse, 1);
284
285         cifsInfo->server_eof = end_of_file;
286         spin_lock(&tmp_inode->i_lock);
287         if (is_size_safe_to_change(cifsInfo, end_of_file)) {
288                 /* can not safely change the file size here if the
289                 client is writing to it due to potential races */
290                 i_size_write(tmp_inode, end_of_file);
291
292         /* 512 bytes (2**9) is the fake blocksize that must be used */
293         /* for this calculation, even though the reported blocksize is larger */
294                 tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
295         }
296         spin_unlock(&tmp_inode->i_lock);
297
298         if (allocation_size < end_of_file)
299                 cFYI(1, ("May be sparse file, allocation less than file size"));
300         cFYI(1, ("File Size %ld and blocks %llu",
301                 (unsigned long)tmp_inode->i_size,
302                 (unsigned long long)tmp_inode->i_blocks));
303         if (S_ISREG(tmp_inode->i_mode)) {
304                 cFYI(1, ("File inode"));
305                 tmp_inode->i_op = &cifs_file_inode_ops;
306                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
307                         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
308                                 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
309                         else
310                                 tmp_inode->i_fop = &cifs_file_direct_ops;
311                 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
312                         tmp_inode->i_fop = &cifs_file_nobrl_ops;
313                 else
314                         tmp_inode->i_fop = &cifs_file_ops;
315
316                 if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
317                    (cifs_sb->tcon->ses->server->maxBuf <
318                         PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
319                         tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
320                 else
321                         tmp_inode->i_data.a_ops = &cifs_addr_ops;
322
323                 if (isNewInode)
324                         return; /* No sense invalidating pages for new inode
325                                    since have not started caching readahead file
326                                    data yet */
327
328                 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
329                         (local_size == tmp_inode->i_size)) {
330                         cFYI(1, ("inode exists but unchanged"));
331                 } else {
332                         /* file may have changed on server */
333                         cFYI(1, ("invalidate inode, readdir detected change"));
334                         invalidate_remote_inode(tmp_inode);
335                 }
336         } else if (S_ISDIR(tmp_inode->i_mode)) {
337                 cFYI(1, ("Directory inode"));
338                 tmp_inode->i_op = &cifs_dir_inode_ops;
339                 tmp_inode->i_fop = &cifs_dir_ops;
340         } else if (S_ISLNK(tmp_inode->i_mode)) {
341                 cFYI(1, ("Symbolic Link inode"));
342                 tmp_inode->i_op = &cifs_symlink_inode_ops;
343         } else {
344                 cFYI(1, ("Init special inode"));
345                 init_special_inode(tmp_inode, tmp_inode->i_mode,
346                                    tmp_inode->i_rdev);
347         }
348 }
349
350 /* BB eventually need to add the following helper function to
351       resolve NT_STATUS_STOPPED_ON_SYMLINK return code when
352       we try to do FindFirst on (NTFS) directory symlinks */
353 /*
354 int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
355                              int xid)
356 {
357         __u16 fid;
358         int len;
359         int oplock = 0;
360         int rc;
361         struct cifsTconInfo *ptcon = cifs_sb->tcon;
362         char *tmpbuffer;
363
364         rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ,
365                         OPEN_REPARSE_POINT, &fid, &oplock, NULL,
366                         cifs_sb->local_nls,
367                         cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
368         if (!rc) {
369                 tmpbuffer = kmalloc(maxpath);
370                 rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path,
371                                 tmpbuffer,
372                                 maxpath -1,
373                                 fid,
374                                 cifs_sb->local_nls);
375                 if (CIFSSMBClose(xid, ptcon, fid)) {
376                         cFYI(1, ("Error closing temporary reparsepoint open)"));
377                 }
378         }
379 }
380  */
381
382 static int initiate_cifs_search(const int xid, struct file *file)
383 {
384         int rc = 0;
385         char *full_path;
386         struct cifsFileInfo *cifsFile;
387         struct cifs_sb_info *cifs_sb;
388         struct cifsTconInfo *pTcon;
389
390         if (file->private_data == NULL) {
391                 file->private_data =
392                         kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
393         }
394
395         if (file->private_data == NULL)
396                 return -ENOMEM;
397         cifsFile = file->private_data;
398         cifsFile->invalidHandle = true;
399         cifsFile->srch_inf.endOfSearch = false;
400
401         cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
402         if (cifs_sb == NULL)
403                 return -EINVAL;
404
405         pTcon = cifs_sb->tcon;
406         if (pTcon == NULL)
407                 return -EINVAL;
408
409         full_path = build_path_from_dentry(file->f_path.dentry);
410
411         if (full_path == NULL)
412                 return -ENOMEM;
413
414         cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
415
416 ffirst_retry:
417         /* test for Unix extensions */
418         /* but now check for them on the share/mount not on the SMB session */
419 /*      if (pTcon->ses->capabilities & CAP_UNIX) { */
420         if (pTcon->unix_ext)
421                 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
422         else if ((pTcon->ses->capabilities &
423                         (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
424                 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
425         } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
426                 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
427         } else /* not srvinos - BB fixme add check for backlevel? */ {
428                 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
429         }
430
431         rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
432                 &cifsFile->netfid, &cifsFile->srch_inf,
433                 cifs_sb->mnt_cifs_flags &
434                         CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
435         if (rc == 0)
436                 cifsFile->invalidHandle = false;
437         /* BB add following call to handle readdir on new NTFS symlink errors
438         else if STATUS_STOPPED_ON_SYMLINK
439                 call get_symlink_reparse_path and retry with new path */
440         else if ((rc == -EOPNOTSUPP) &&
441                 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
442                 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
443                 goto ffirst_retry;
444         }
445         kfree(full_path);
446         return rc;
447 }
448
449 /* return length of unicode string in bytes */
450 static int cifs_unicode_bytelen(char *str)
451 {
452         int len;
453         __le16 *ustr = (__le16 *)str;
454
455         for (len = 0; len <= PATH_MAX; len++) {
456                 if (ustr[len] == 0)
457                         return len << 1;
458         }
459         cFYI(1, ("Unicode string longer than PATH_MAX found"));
460         return len << 1;
461 }
462
463 static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
464 {
465         char *new_entry;
466         FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
467
468         if (level == SMB_FIND_FILE_INFO_STANDARD) {
469                 FIND_FILE_STANDARD_INFO *pfData;
470                 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
471
472                 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
473                                 pfData->FileNameLength;
474         } else
475                 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
476         cFYI(1, ("new entry %p old entry %p", new_entry, old_entry));
477         /* validate that new_entry is not past end of SMB */
478         if (new_entry >= end_of_smb) {
479                 cERROR(1,
480                       ("search entry %p began after end of SMB %p old entry %p",
481                         new_entry, end_of_smb, old_entry));
482                 return NULL;
483         } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
484                     (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
485                   || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
486                    (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb)))  {
487                 cERROR(1, ("search entry %p extends after end of SMB %p",
488                         new_entry, end_of_smb));
489                 return NULL;
490         } else
491                 return new_entry;
492
493 }
494
495 #define UNICODE_DOT cpu_to_le16(0x2e)
496
497 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
498 static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
499 {
500         int rc = 0;
501         char *filename = NULL;
502         int len = 0;
503
504         if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
505                 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
506                 filename = &pFindData->FileName[0];
507                 if (cfile->srch_inf.unicode) {
508                         len = cifs_unicode_bytelen(filename);
509                 } else {
510                         /* BB should we make this strnlen of PATH_MAX? */
511                         len = strnlen(filename, 5);
512                 }
513         } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
514                 FILE_DIRECTORY_INFO *pFindData =
515                         (FILE_DIRECTORY_INFO *)current_entry;
516                 filename = &pFindData->FileName[0];
517                 len = le32_to_cpu(pFindData->FileNameLength);
518         } else if (cfile->srch_inf.info_level ==
519                         SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
520                 FILE_FULL_DIRECTORY_INFO *pFindData =
521                         (FILE_FULL_DIRECTORY_INFO *)current_entry;
522                 filename = &pFindData->FileName[0];
523                 len = le32_to_cpu(pFindData->FileNameLength);
524         } else if (cfile->srch_inf.info_level ==
525                         SMB_FIND_FILE_ID_FULL_DIR_INFO) {
526                 SEARCH_ID_FULL_DIR_INFO *pFindData =
527                         (SEARCH_ID_FULL_DIR_INFO *)current_entry;
528                 filename = &pFindData->FileName[0];
529                 len = le32_to_cpu(pFindData->FileNameLength);
530         } else if (cfile->srch_inf.info_level ==
531                         SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
532                 FILE_BOTH_DIRECTORY_INFO *pFindData =
533                         (FILE_BOTH_DIRECTORY_INFO *)current_entry;
534                 filename = &pFindData->FileName[0];
535                 len = le32_to_cpu(pFindData->FileNameLength);
536         } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
537                 FIND_FILE_STANDARD_INFO *pFindData =
538                         (FIND_FILE_STANDARD_INFO *)current_entry;
539                 filename = &pFindData->FileName[0];
540                 len = pFindData->FileNameLength;
541         } else {
542                 cFYI(1, ("Unknown findfirst level %d",
543                          cfile->srch_inf.info_level));
544         }
545
546         if (filename) {
547                 if (cfile->srch_inf.unicode) {
548                         __le16 *ufilename = (__le16 *)filename;
549                         if (len == 2) {
550                                 /* check for . */
551                                 if (ufilename[0] == UNICODE_DOT)
552                                         rc = 1;
553                         } else if (len == 4) {
554                                 /* check for .. */
555                                 if ((ufilename[0] == UNICODE_DOT)
556                                    && (ufilename[1] == UNICODE_DOT))
557                                         rc = 2;
558                         }
559                 } else /* ASCII */ {
560                         if (len == 1) {
561                                 if (filename[0] == '.')
562                                         rc = 1;
563                         } else if (len == 2) {
564                                 if ((filename[0] == '.') && (filename[1] == '.'))
565                                         rc = 2;
566                         }
567                 }
568         }
569
570         return rc;
571 }
572
573 /* Check if directory that we are searching has changed so we can decide
574    whether we can use the cached search results from the previous search */
575 static int is_dir_changed(struct file *file)
576 {
577         struct inode *inode = file->f_path.dentry->d_inode;
578         struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
579
580         if (cifsInfo->time == 0)
581                 return 1; /* directory was changed, perhaps due to unlink */
582         else
583                 return 0;
584
585 }
586
587 static int cifs_save_resume_key(const char *current_entry,
588         struct cifsFileInfo *cifsFile)
589 {
590         int rc = 0;
591         unsigned int len = 0;
592         __u16 level;
593         char *filename;
594
595         if ((cifsFile == NULL) || (current_entry == NULL))
596                 return -EINVAL;
597
598         level = cifsFile->srch_inf.info_level;
599
600         if (level == SMB_FIND_FILE_UNIX) {
601                 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
602
603                 filename = &pFindData->FileName[0];
604                 if (cifsFile->srch_inf.unicode) {
605                         len = cifs_unicode_bytelen(filename);
606                 } else {
607                         /* BB should we make this strnlen of PATH_MAX? */
608                         len = strnlen(filename, PATH_MAX);
609                 }
610                 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
611         } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
612                 FILE_DIRECTORY_INFO *pFindData =
613                         (FILE_DIRECTORY_INFO *)current_entry;
614                 filename = &pFindData->FileName[0];
615                 len = le32_to_cpu(pFindData->FileNameLength);
616                 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
617         } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
618                 FILE_FULL_DIRECTORY_INFO *pFindData =
619                         (FILE_FULL_DIRECTORY_INFO *)current_entry;
620                 filename = &pFindData->FileName[0];
621                 len = le32_to_cpu(pFindData->FileNameLength);
622                 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
623         } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
624                 SEARCH_ID_FULL_DIR_INFO *pFindData =
625                         (SEARCH_ID_FULL_DIR_INFO *)current_entry;
626                 filename = &pFindData->FileName[0];
627                 len = le32_to_cpu(pFindData->FileNameLength);
628                 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
629         } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
630                 FILE_BOTH_DIRECTORY_INFO *pFindData =
631                         (FILE_BOTH_DIRECTORY_INFO *)current_entry;
632                 filename = &pFindData->FileName[0];
633                 len = le32_to_cpu(pFindData->FileNameLength);
634                 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
635         } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
636                 FIND_FILE_STANDARD_INFO *pFindData =
637                         (FIND_FILE_STANDARD_INFO *)current_entry;
638                 filename = &pFindData->FileName[0];
639                 /* one byte length, no name conversion */
640                 len = (unsigned int)pFindData->FileNameLength;
641                 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
642         } else {
643                 cFYI(1, ("Unknown findfirst level %d", level));
644                 return -EINVAL;
645         }
646         cifsFile->srch_inf.resume_name_len = len;
647         cifsFile->srch_inf.presume_name = filename;
648         return rc;
649 }
650
651 /* find the corresponding entry in the search */
652 /* Note that the SMB server returns search entries for . and .. which
653    complicates logic here if we choose to parse for them and we do not
654    assume that they are located in the findfirst return buffer.*/
655 /* We start counting in the buffer with entry 2 and increment for every
656    entry (do not increment for . or .. entry) */
657 static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
658         struct file *file, char **ppCurrentEntry, int *num_to_ret)
659 {
660         int rc = 0;
661         int pos_in_buf = 0;
662         loff_t first_entry_in_buffer;
663         loff_t index_to_find = file->f_pos;
664         struct cifsFileInfo *cifsFile = file->private_data;
665         /* check if index in the buffer */
666
667         if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
668            (num_to_ret == NULL))
669                 return -ENOENT;
670
671         *ppCurrentEntry = NULL;
672         first_entry_in_buffer =
673                 cifsFile->srch_inf.index_of_last_entry -
674                         cifsFile->srch_inf.entries_in_buffer;
675
676         /* if first entry in buf is zero then is first buffer
677         in search response data which means it is likely . and ..
678         will be in this buffer, although some servers do not return
679         . and .. for the root of a drive and for those we need
680         to start two entries earlier */
681
682         dump_cifs_file_struct(file, "In fce ");
683         if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
684              is_dir_changed(file)) ||
685            (index_to_find < first_entry_in_buffer)) {
686                 /* close and restart search */
687                 cFYI(1, ("search backing up - close and restart search"));
688                 write_lock(&GlobalSMBSeslock);
689                 if (!cifsFile->srch_inf.endOfSearch &&
690                     !cifsFile->invalidHandle) {
691                         cifsFile->invalidHandle = true;
692                         write_unlock(&GlobalSMBSeslock);
693                         CIFSFindClose(xid, pTcon, cifsFile->netfid);
694                 } else
695                         write_unlock(&GlobalSMBSeslock);
696                 if (cifsFile->srch_inf.ntwrk_buf_start) {
697                         cFYI(1, ("freeing SMB ff cache buf on search rewind"));
698                         if (cifsFile->srch_inf.smallBuf)
699                                 cifs_small_buf_release(cifsFile->srch_inf.
700                                                 ntwrk_buf_start);
701                         else
702                                 cifs_buf_release(cifsFile->srch_inf.
703                                                 ntwrk_buf_start);
704                         cifsFile->srch_inf.ntwrk_buf_start = NULL;
705                 }
706                 rc = initiate_cifs_search(xid, file);
707                 if (rc) {
708                         cFYI(1, ("error %d reinitiating a search on rewind",
709                                  rc));
710                         return rc;
711                 }
712                 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
713         }
714
715         while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
716               (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
717                 cFYI(1, ("calling findnext2"));
718                 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
719                                   &cifsFile->srch_inf);
720                 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
721                 if (rc)
722                         return -ENOENT;
723         }
724         if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
725                 /* we found the buffer that contains the entry */
726                 /* scan and find it */
727                 int i;
728                 char *current_entry;
729                 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
730                         smbCalcSize((struct smb_hdr *)
731                                 cifsFile->srch_inf.ntwrk_buf_start);
732
733                 current_entry = cifsFile->srch_inf.srch_entries_start;
734                 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
735                                         - cifsFile->srch_inf.entries_in_buffer;
736                 pos_in_buf = index_to_find - first_entry_in_buffer;
737                 cFYI(1, ("found entry - pos_in_buf %d", pos_in_buf));
738
739                 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
740                         /* go entry by entry figuring out which is first */
741                         current_entry = nxt_dir_entry(current_entry, end_of_smb,
742                                                 cifsFile->srch_inf.info_level);
743                 }
744                 if ((current_entry == NULL) && (i < pos_in_buf)) {
745                         /* BB fixme - check if we should flag this error */
746                         cERROR(1, ("reached end of buf searching for pos in buf"
747                           " %d index to find %lld rc %d",
748                           pos_in_buf, index_to_find, rc));
749                 }
750                 rc = 0;
751                 *ppCurrentEntry = current_entry;
752         } else {
753                 cFYI(1, ("index not in buffer - could not findnext into it"));
754                 return 0;
755         }
756
757         if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
758                 cFYI(1, ("can not return entries pos_in_buf beyond last"));
759                 *num_to_ret = 0;
760         } else
761                 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
762
763         return rc;
764 }
765
766 /* inode num, inode type and filename returned */
767 static int cifs_get_name_from_search_buf(struct qstr *pqst,
768         char *current_entry, __u16 level, unsigned int unicode,
769         struct cifs_sb_info *cifs_sb, unsigned int max_len, __u64 *pinum)
770 {
771         int rc = 0;
772         unsigned int len = 0;
773         char *filename;
774         struct nls_table *nlt = cifs_sb->local_nls;
775
776         *pinum = 0;
777
778         if (level == SMB_FIND_FILE_UNIX) {
779                 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
780
781                 filename = &pFindData->FileName[0];
782                 if (unicode) {
783                         len = cifs_unicode_bytelen(filename);
784                 } else {
785                         /* BB should we make this strnlen of PATH_MAX? */
786                         len = strnlen(filename, PATH_MAX);
787                 }
788
789                 *pinum = le64_to_cpu(pFindData->basic.UniqueId);
790         } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
791                 FILE_DIRECTORY_INFO *pFindData =
792                         (FILE_DIRECTORY_INFO *)current_entry;
793                 filename = &pFindData->FileName[0];
794                 len = le32_to_cpu(pFindData->FileNameLength);
795         } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
796                 FILE_FULL_DIRECTORY_INFO *pFindData =
797                         (FILE_FULL_DIRECTORY_INFO *)current_entry;
798                 filename = &pFindData->FileName[0];
799                 len = le32_to_cpu(pFindData->FileNameLength);
800         } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
801                 SEARCH_ID_FULL_DIR_INFO *pFindData =
802                         (SEARCH_ID_FULL_DIR_INFO *)current_entry;
803                 filename = &pFindData->FileName[0];
804                 len = le32_to_cpu(pFindData->FileNameLength);
805                 *pinum = le64_to_cpu(pFindData->UniqueId);
806         } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
807                 FILE_BOTH_DIRECTORY_INFO *pFindData =
808                         (FILE_BOTH_DIRECTORY_INFO *)current_entry;
809                 filename = &pFindData->FileName[0];
810                 len = le32_to_cpu(pFindData->FileNameLength);
811         } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
812                 FIND_FILE_STANDARD_INFO *pFindData =
813                         (FIND_FILE_STANDARD_INFO *)current_entry;
814                 filename = &pFindData->FileName[0];
815                 /* one byte length, no name conversion */
816                 len = (unsigned int)pFindData->FileNameLength;
817         } else {
818                 cFYI(1, ("Unknown findfirst level %d", level));
819                 return -EINVAL;
820         }
821
822         if (len > max_len) {
823                 cERROR(1, ("bad search response length %d past smb end", len));
824                 return -EINVAL;
825         }
826
827         if (unicode) {
828                 pqst->len = cifs_from_ucs2((char *) pqst->name,
829                                            (__le16 *) filename,
830                                            UNICODE_NAME_MAX,
831                                            min(len, max_len), nlt,
832                                            cifs_sb->mnt_cifs_flags &
833                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
834         } else {
835                 pqst->name = filename;
836                 pqst->len = len;
837         }
838         pqst->hash = full_name_hash(pqst->name, pqst->len);
839 /*      cFYI(1, ("filldir on %s",pqst->name));  */
840         return rc;
841 }
842
843 static int cifs_filldir(char *pfindEntry, struct file *file, filldir_t filldir,
844                         void *direntry, char *scratch_buf, unsigned int max_len)
845 {
846         int rc = 0;
847         struct qstr qstring;
848         struct cifsFileInfo *pCifsF;
849         unsigned int obj_type;
850         __u64  inum;
851         ino_t  ino;
852         struct cifs_sb_info *cifs_sb;
853         struct inode *tmp_inode;
854         struct dentry *tmp_dentry;
855         struct cifs_fattr fattr;
856
857         /* get filename and len into qstring */
858         /* get dentry */
859         /* decide whether to create and populate ionde */
860         if ((direntry == NULL) || (file == NULL))
861                 return -EINVAL;
862
863         pCifsF = file->private_data;
864
865         if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
866                 return -ENOENT;
867
868         rc = cifs_entry_is_dot(pfindEntry, pCifsF);
869         /* skip . and .. since we added them first */
870         if (rc != 0)
871                 return 0;
872
873         cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
874
875         qstring.name = scratch_buf;
876         rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
877                         pCifsF->srch_inf.info_level,
878                         pCifsF->srch_inf.unicode, cifs_sb,
879                         max_len,
880                         &inum /* returned */);
881
882         if (rc)
883                 return rc;
884
885         /* only these two infolevels return valid inode numbers */
886         if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
887                 cifs_unix_basic_to_fattr(&fattr,
888                                  &((FILE_UNIX_INFO *) pfindEntry)->basic,
889                                  cifs_sb);
890                 tmp_dentry = cifs_readdir_lookup(file->f_dentry, &qstring,
891                                                  &fattr);
892                 obj_type = fattr.cf_dtype;
893                 ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
894         } else {
895                 if (pCifsF->srch_inf.info_level ==
896                     SMB_FIND_FILE_ID_FULL_DIR_INFO)
897                         rc = construct_dentry(&qstring, file, &tmp_inode,
898                                                 &tmp_dentry, &inum);
899                 else
900                         rc = construct_dentry(&qstring, file, &tmp_inode,
901                                                 &tmp_dentry, NULL);
902
903                 if ((tmp_inode == NULL) || (tmp_dentry == NULL)) {
904                         rc = -ENOMEM;
905                         goto out;
906                 }
907
908                 /* we pass in rc below, indicating whether it is a new inode,
909                  * so we can figure out whether to invalidate the inode cached
910                  * data if the file has changed
911                  */
912                 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
913                         fill_in_inode(tmp_inode, 0, pfindEntry, &obj_type, rc);
914                 else
915                         fill_in_inode(tmp_inode, 1, pfindEntry, &obj_type, rc);
916
917                 /* new inode - needs to be tied to dentry */
918                 if (rc) {
919                         d_instantiate(tmp_dentry, tmp_inode);
920                         if (rc == 2)
921                                 d_rehash(tmp_dentry);
922                 }
923
924                 ino = cifs_uniqueid_to_ino_t(tmp_inode->i_ino);
925         }
926
927         rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
928                      ino, obj_type);
929         if (rc) {
930                 cFYI(1, ("filldir rc = %d", rc));
931                 /* we can not return filldir errors to the caller
932                 since they are "normal" when the stat blocksize
933                 is too small - we return remapped error instead */
934                 rc = -EOVERFLOW;
935         }
936
937 out:
938         dput(tmp_dentry);
939         return rc;
940 }
941
942
943 int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
944 {
945         int rc = 0;
946         int xid, i;
947         struct cifs_sb_info *cifs_sb;
948         struct cifsTconInfo *pTcon;
949         struct cifsFileInfo *cifsFile = NULL;
950         char *current_entry;
951         int num_to_fill = 0;
952         char *tmp_buf = NULL;
953         char *end_of_smb;
954         unsigned int max_len;
955
956         xid = GetXid();
957
958         cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
959         pTcon = cifs_sb->tcon;
960         if (pTcon == NULL)
961                 return -EINVAL;
962
963         switch ((int) file->f_pos) {
964         case 0:
965                 if (filldir(direntry, ".", 1, file->f_pos,
966                      file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
967                         cERROR(1, ("Filldir for current dir failed"));
968                         rc = -ENOMEM;
969                         break;
970                 }
971                 file->f_pos++;
972         case 1:
973                 if (filldir(direntry, "..", 2, file->f_pos,
974                      file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
975                         cERROR(1, ("Filldir for parent dir failed"));
976                         rc = -ENOMEM;
977                         break;
978                 }
979                 file->f_pos++;
980         default:
981                 /* 1) If search is active,
982                         is in current search buffer?
983                         if it before then restart search
984                         if after then keep searching till find it */
985
986                 if (file->private_data == NULL) {
987                         rc = initiate_cifs_search(xid, file);
988                         cFYI(1, ("initiate cifs search rc %d", rc));
989                         if (rc) {
990                                 FreeXid(xid);
991                                 return rc;
992                         }
993                 }
994                 if (file->private_data == NULL) {
995                         rc = -EINVAL;
996                         FreeXid(xid);
997                         return rc;
998                 }
999                 cifsFile = file->private_data;
1000                 if (cifsFile->srch_inf.endOfSearch) {
1001                         if (cifsFile->srch_inf.emptyDir) {
1002                                 cFYI(1, ("End of search, empty dir"));
1003                                 rc = 0;
1004                                 break;
1005                         }
1006                 } /* else {
1007                         cifsFile->invalidHandle = true;
1008                         CIFSFindClose(xid, pTcon, cifsFile->netfid);
1009                 } */
1010
1011                 rc = find_cifs_entry(xid, pTcon, file,
1012                                 &current_entry, &num_to_fill);
1013                 if (rc) {
1014                         cFYI(1, ("fce error %d", rc));
1015                         goto rddir2_exit;
1016                 } else if (current_entry != NULL) {
1017                         cFYI(1, ("entry %lld found", file->f_pos));
1018                 } else {
1019                         cFYI(1, ("could not find entry"));
1020                         goto rddir2_exit;
1021                 }
1022                 cFYI(1, ("loop through %d times filling dir for net buf %p",
1023                         num_to_fill, cifsFile->srch_inf.ntwrk_buf_start));
1024                 max_len = smbCalcSize((struct smb_hdr *)
1025                                 cifsFile->srch_inf.ntwrk_buf_start);
1026                 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1027
1028                 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
1029                 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
1030                         if (current_entry == NULL) {
1031                                 /* evaluate whether this case is an error */
1032                                 cERROR(1, ("past SMB end,  num to fill %d i %d",
1033                                           num_to_fill, i));
1034                                 break;
1035                         }
1036                         /* if buggy server returns . and .. late do
1037                         we want to check for that here? */
1038                         rc = cifs_filldir(current_entry, file,
1039                                         filldir, direntry, tmp_buf, max_len);
1040                         if (rc == -EOVERFLOW) {
1041                                 rc = 0;
1042                                 break;
1043                         }
1044
1045                         file->f_pos++;
1046                         if (file->f_pos ==
1047                                 cifsFile->srch_inf.index_of_last_entry) {
1048                                 cFYI(1, ("last entry in buf at pos %lld %s",
1049                                         file->f_pos, tmp_buf));
1050                                 cifs_save_resume_key(current_entry, cifsFile);
1051                                 break;
1052                         } else
1053                                 current_entry =
1054                                         nxt_dir_entry(current_entry, end_of_smb,
1055                                                 cifsFile->srch_inf.info_level);
1056                 }
1057                 kfree(tmp_buf);
1058                 break;
1059         } /* end switch */
1060
1061 rddir2_exit:
1062         FreeXid(xid);
1063         return rc;
1064 }