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