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