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