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