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