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