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