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