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