include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / cifs / readdir.c
CommitLineData
1da177e4
LT
1/*
2 * fs/cifs/readdir.c
3 *
4 * Directory search handling
6dc0f87e 5 *
ad7a2926 6 * Copyright (C) International Business Machines Corp., 2004, 2008
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>
5a0e3ad6 25#include <linux/slab.h>
1da177e4 26#include <linux/stat.h>
1da177e4
LT
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
f5884166
JL
35/*
36 * To be safe - for UCS to UTF-8 with strings loaded with the rare long
37 * characters alloc more to account for such multibyte target UTF-8
38 * characters.
39 */
40#define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
41
3979877e
SF
42#ifdef CONFIG_CIFS_DEBUG2
43static void dump_cifs_file_struct(struct file *file, char *label)
1da177e4 44{
6dc0f87e 45 struct cifsFileInfo *cf;
1da177e4 46
4523cc30 47 if (file) {
1da177e4 48 cf = file->private_data;
4523cc30 49 if (cf == NULL) {
6dc0f87e 50 cFYI(1, ("empty cifs private file data"));
1da177e4
LT
51 return;
52 }
ad7a2926 53 if (cf->invalidHandle)
6dc0f87e 54 cFYI(1, ("invalid handle"));
ad7a2926 55 if (cf->srch_inf.endOfSearch)
6dc0f87e 56 cFYI(1, ("end of search"));
ad7a2926 57 if (cf->srch_inf.emptyDir)
6dc0f87e 58 cFYI(1, ("empty dir"));
1da177e4 59 }
3979877e 60}
90c81e0b
SF
61#else
62static inline void dump_cifs_file_struct(struct file *file, char *label)
63{
64}
3979877e 65#endif /* DEBUG2 */
1da177e4 66
cc0bad75
JL
67/*
68 * Find the dentry that matches "name". If there isn't one, create one. If it's
69 * a negative dentry or the uniqueid changed, then drop it and recreate it.
70 */
71static struct dentry *
72cifs_readdir_lookup(struct dentry *parent, struct qstr *name,
73 struct cifs_fattr *fattr)
74{
75 struct dentry *dentry, *alias;
76 struct inode *inode;
77 struct super_block *sb = parent->d_inode->i_sb;
78
79 cFYI(1, ("For %s", name->name));
80
05507fa2
JL
81 if (parent->d_op && parent->d_op->d_hash)
82 parent->d_op->d_hash(parent, name);
83 else
84 name->hash = full_name_hash(name->name, name->len);
85
cc0bad75
JL
86 dentry = d_lookup(parent, name);
87 if (dentry) {
88 /* FIXME: check for inode number changes? */
89 if (dentry->d_inode != NULL)
90 return dentry;
91 d_drop(dentry);
92 dput(dentry);
93 }
94
95 dentry = d_alloc(parent, name);
96 if (dentry == NULL)
97 return NULL;
98
99 inode = cifs_iget(sb, fattr);
100 if (!inode) {
101 dput(dentry);
102 return NULL;
103 }
104
105 if (CIFS_SB(sb)->tcon->nocase)
106 dentry->d_op = &cifs_ci_dentry_ops;
107 else
108 dentry->d_op = &cifs_dentry_ops;
109
110 alias = d_materialise_unique(dentry, inode);
111 if (alias != NULL) {
112 dput(dentry);
113 if (IS_ERR(alias))
114 return NULL;
115 dentry = alias;
116 }
117
118 return dentry;
119}
120
0b8f18e3
JL
121static void
122cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
1da177e4 123{
0b8f18e3
JL
124 fattr->cf_uid = cifs_sb->mnt_uid;
125 fattr->cf_gid = cifs_sb->mnt_gid;
1da177e4 126
0b8f18e3
JL
127 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
128 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
129 fattr->cf_dtype = DT_DIR;
1da177e4 130 } else {
0b8f18e3
JL
131 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
132 fattr->cf_dtype = DT_REG;
1da177e4
LT
133 }
134
0b8f18e3
JL
135 if (fattr->cf_cifsattrs & ATTR_READONLY)
136 fattr->cf_mode &= ~S_IWUGO;
5bafd765 137
0b8f18e3
JL
138 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
139 fattr->cf_cifsattrs & ATTR_SYSTEM) {
140 if (fattr->cf_eof == 0) {
141 fattr->cf_mode &= ~S_IFMT;
142 fattr->cf_mode |= S_IFIFO;
143 fattr->cf_dtype = DT_FIFO;
3020a1f5 144 } else {
4468eb3f 145 /*
0b8f18e3
JL
146 * trying to get the type and mode via SFU can be slow,
147 * so just call those regular files for now, and mark
148 * for reval
4468eb3f 149 */
0b8f18e3 150 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
4468eb3f
JL
151 }
152 }
0b8f18e3 153}
1da177e4 154
15dd4781 155static void
0b8f18e3
JL
156cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info,
157 struct cifs_sb_info *cifs_sb)
158{
159 memset(fattr, 0, sizeof(*fattr));
160 fattr->cf_cifsattrs = le32_to_cpu(info->ExtFileAttributes);
161 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
162 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
163 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
164 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
165 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
166
167 cifs_fill_common_info(fattr, cifs_sb);
168}
1da177e4 169
15dd4781 170static void
0b8f18e3
JL
171cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info,
172 struct cifs_sb_info *cifs_sb)
173{
174 int offset = cifs_sb->tcon->ses->server->timeAdj;
1da177e4 175
0b8f18e3
JL
176 memset(fattr, 0, sizeof(*fattr));
177 fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate,
178 info->LastAccessTime, offset);
179 fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate,
180 info->LastWriteTime, offset);
181 fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate,
182 info->LastWriteTime, offset);
183
184 fattr->cf_cifsattrs = le16_to_cpu(info->Attributes);
185 fattr->cf_bytes = le32_to_cpu(info->AllocationSize);
186 fattr->cf_eof = le32_to_cpu(info->DataSize);
187
188 cifs_fill_common_info(fattr, cifs_sb);
1da177e4
LT
189}
190
0e0d2cf3
SF
191/* BB eventually need to add the following helper function to
192 resolve NT_STATUS_STOPPED_ON_SYMLINK return code when
193 we try to do FindFirst on (NTFS) directory symlinks */
194/*
195int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
196 int xid)
197{
198 __u16 fid;
199 int len;
200 int oplock = 0;
201 int rc;
202 struct cifsTconInfo *ptcon = cifs_sb->tcon;
203 char *tmpbuffer;
204
205 rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ,
206 OPEN_REPARSE_POINT, &fid, &oplock, NULL,
207 cifs_sb->local_nls,
208 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
209 if (!rc) {
210 tmpbuffer = kmalloc(maxpath);
211 rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path,
212 tmpbuffer,
213 maxpath -1,
214 fid,
215 cifs_sb->local_nls);
216 if (CIFSSMBClose(xid, ptcon, fid)) {
217 cFYI(1, ("Error closing temporary reparsepoint open)"));
218 }
219 }
220}
221 */
222
1da177e4
LT
223static int initiate_cifs_search(const int xid, struct file *file)
224{
225 int rc = 0;
3870253e
SF
226 char *full_path;
227 struct cifsFileInfo *cifsFile;
1da177e4
LT
228 struct cifs_sb_info *cifs_sb;
229 struct cifsTconInfo *pTcon;
230
4523cc30 231 if (file->private_data == NULL) {
3870253e
SF
232 file->private_data =
233 kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
1da177e4
LT
234 }
235
4523cc30 236 if (file->private_data == NULL)
1da177e4 237 return -ENOMEM;
1da177e4 238 cifsFile = file->private_data;
4b18f2a9
SF
239 cifsFile->invalidHandle = true;
240 cifsFile->srch_inf.endOfSearch = false;
1da177e4 241
e6a00296 242 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
4523cc30 243 if (cifs_sb == NULL)
1da177e4
LT
244 return -EINVAL;
245
246 pTcon = cifs_sb->tcon;
4523cc30 247 if (pTcon == NULL)
1da177e4
LT
248 return -EINVAL;
249
e6a00296 250 full_path = build_path_from_dentry(file->f_path.dentry);
1da177e4 251
ad7a2926 252 if (full_path == NULL)
1da177e4 253 return -ENOMEM;
1da177e4 254
966ca923 255 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
1da177e4 256
75cf6bdc 257ffirst_retry:
1da177e4 258 /* test for Unix extensions */
c18c842b
SF
259 /* but now check for them on the share/mount not on the SMB session */
260/* if (pTcon->ses->capabilities & CAP_UNIX) { */
ad7a2926 261 if (pTcon->unix_ext)
5bafd765 262 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
ad7a2926 263 else if ((pTcon->ses->capabilities &
5bafd765
SF
264 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
265 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
1da177e4
LT
266 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
267 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
268 } else /* not srvinos - BB fixme add check for backlevel? */ {
269 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
270 }
271
3870253e 272 rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
737b758c 273 &cifsFile->netfid, &cifsFile->srch_inf,
3870253e 274 cifs_sb->mnt_cifs_flags &
eafe8701 275 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
4523cc30 276 if (rc == 0)
4b18f2a9 277 cifsFile->invalidHandle = false;
e836f015 278 /* BB add following call to handle readdir on new NTFS symlink errors
0e0d2cf3
SF
279 else if STATUS_STOPPED_ON_SYMLINK
280 call get_symlink_reparse_path and retry with new path */
281 else if ((rc == -EOPNOTSUPP) &&
75cf6bdc
SF
282 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
283 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
284 goto ffirst_retry;
285 }
1da177e4
LT
286 kfree(full_path);
287 return rc;
288}
289
290/* return length of unicode string in bytes */
291static int cifs_unicode_bytelen(char *str)
292{
293 int len;
63d2583f 294 __le16 *ustr = (__le16 *)str;
1da177e4 295
3870253e 296 for (len = 0; len <= PATH_MAX; len++) {
4523cc30 297 if (ustr[len] == 0)
1da177e4
LT
298 return len << 1;
299 }
790fe579 300 cFYI(1, ("Unicode string longer than PATH_MAX found"));
1da177e4
LT
301 return len << 1;
302}
303
5bafd765 304static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
1da177e4 305{
3870253e 306 char *new_entry;
ad7a2926 307 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
1da177e4 308
4523cc30 309 if (level == SMB_FIND_FILE_INFO_STANDARD) {
ad7a2926 310 FIND_FILE_STANDARD_INFO *pfData;
5bafd765
SF
311 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
312
313 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
314 pfData->FileNameLength;
315 } else
316 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
790fe579 317 cFYI(1, ("new entry %p old entry %p", new_entry, old_entry));
1da177e4 318 /* validate that new_entry is not past end of SMB */
4523cc30 319 if (new_entry >= end_of_smb) {
09d1db5c
SF
320 cERROR(1,
321 ("search entry %p began after end of SMB %p old entry %p",
3870253e 322 new_entry, end_of_smb, old_entry));
1da177e4 323 return NULL;
4523cc30 324 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
3870253e
SF
325 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
326 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
5bafd765 327 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
3870253e 328 cERROR(1, ("search entry %p extends after end of SMB %p",
09d1db5c
SF
329 new_entry, end_of_smb));
330 return NULL;
3870253e 331 } else
1da177e4
LT
332 return new_entry;
333
334}
335
336#define UNICODE_DOT cpu_to_le16(0x2e)
337
338/* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
339static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
340{
341 int rc = 0;
3870253e
SF
342 char *filename = NULL;
343 int len = 0;
1da177e4 344
4523cc30 345 if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
ad7a2926 346 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
1da177e4 347 filename = &pFindData->FileName[0];
4523cc30 348 if (cfile->srch_inf.unicode) {
1da177e4
LT
349 len = cifs_unicode_bytelen(filename);
350 } else {
351 /* BB should we make this strnlen of PATH_MAX? */
352 len = strnlen(filename, 5);
353 }
4523cc30 354 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
ad7a2926 355 FILE_DIRECTORY_INFO *pFindData =
1da177e4
LT
356 (FILE_DIRECTORY_INFO *)current_entry;
357 filename = &pFindData->FileName[0];
358 len = le32_to_cpu(pFindData->FileNameLength);
3870253e 359 } else if (cfile->srch_inf.info_level ==
5bafd765 360 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
ad7a2926 361 FILE_FULL_DIRECTORY_INFO *pFindData =
1da177e4
LT
362 (FILE_FULL_DIRECTORY_INFO *)current_entry;
363 filename = &pFindData->FileName[0];
364 len = le32_to_cpu(pFindData->FileNameLength);
4523cc30 365 } else if (cfile->srch_inf.info_level ==
5bafd765 366 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
ad7a2926 367 SEARCH_ID_FULL_DIR_INFO *pFindData =
1da177e4
LT
368 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
369 filename = &pFindData->FileName[0];
370 len = le32_to_cpu(pFindData->FileNameLength);
3870253e 371 } else if (cfile->srch_inf.info_level ==
5bafd765 372 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
ad7a2926 373 FILE_BOTH_DIRECTORY_INFO *pFindData =
1da177e4
LT
374 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
375 filename = &pFindData->FileName[0];
376 len = le32_to_cpu(pFindData->FileNameLength);
4523cc30 377 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
ad7a2926 378 FIND_FILE_STANDARD_INFO *pFindData =
5bafd765
SF
379 (FIND_FILE_STANDARD_INFO *)current_entry;
380 filename = &pFindData->FileName[0];
5ddaa683 381 len = pFindData->FileNameLength;
1da177e4 382 } else {
790fe579
SF
383 cFYI(1, ("Unknown findfirst level %d",
384 cfile->srch_inf.info_level));
1da177e4
LT
385 }
386
4523cc30
SF
387 if (filename) {
388 if (cfile->srch_inf.unicode) {
1da177e4 389 __le16 *ufilename = (__le16 *)filename;
4523cc30 390 if (len == 2) {
1da177e4 391 /* check for . */
4523cc30 392 if (ufilename[0] == UNICODE_DOT)
1da177e4 393 rc = 1;
4523cc30 394 } else if (len == 4) {
1da177e4 395 /* check for .. */
4523cc30 396 if ((ufilename[0] == UNICODE_DOT)
3870253e 397 && (ufilename[1] == UNICODE_DOT))
1da177e4
LT
398 rc = 2;
399 }
400 } else /* ASCII */ {
4523cc30 401 if (len == 1) {
3870253e 402 if (filename[0] == '.')
1da177e4 403 rc = 1;
4523cc30 404 } else if (len == 2) {
790fe579 405 if ((filename[0] == '.') && (filename[1] == '.'))
1da177e4
LT
406 rc = 2;
407 }
408 }
409 }
410
411 return rc;
412}
413
eafe8701
SF
414/* Check if directory that we are searching has changed so we can decide
415 whether we can use the cached search results from the previous search */
3870253e 416static int is_dir_changed(struct file *file)
eafe8701 417{
c33f8d32
CH
418 struct inode *inode = file->f_path.dentry->d_inode;
419 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
eafe8701 420
c33f8d32 421 if (cifsInfo->time == 0)
eafe8701
SF
422 return 1; /* directory was changed, perhaps due to unlink */
423 else
424 return 0;
425
426}
427
0752f152
SF
428static int cifs_save_resume_key(const char *current_entry,
429 struct cifsFileInfo *cifsFile)
430{
431 int rc = 0;
432 unsigned int len = 0;
433 __u16 level;
434 char *filename;
435
436 if ((cifsFile == NULL) || (current_entry == NULL))
437 return -EINVAL;
438
439 level = cifsFile->srch_inf.info_level;
440
441 if (level == SMB_FIND_FILE_UNIX) {
442 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
443
444 filename = &pFindData->FileName[0];
445 if (cifsFile->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, PATH_MAX);
450 }
451 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
452 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
453 FILE_DIRECTORY_INFO *pFindData =
454 (FILE_DIRECTORY_INFO *)current_entry;
455 filename = &pFindData->FileName[0];
456 len = le32_to_cpu(pFindData->FileNameLength);
457 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
458 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
459 FILE_FULL_DIRECTORY_INFO *pFindData =
460 (FILE_FULL_DIRECTORY_INFO *)current_entry;
461 filename = &pFindData->FileName[0];
462 len = le32_to_cpu(pFindData->FileNameLength);
463 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
464 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
465 SEARCH_ID_FULL_DIR_INFO *pFindData =
466 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
467 filename = &pFindData->FileName[0];
468 len = le32_to_cpu(pFindData->FileNameLength);
469 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
470 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
471 FILE_BOTH_DIRECTORY_INFO *pFindData =
472 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
473 filename = &pFindData->FileName[0];
474 len = le32_to_cpu(pFindData->FileNameLength);
475 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
476 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
477 FIND_FILE_STANDARD_INFO *pFindData =
478 (FIND_FILE_STANDARD_INFO *)current_entry;
479 filename = &pFindData->FileName[0];
480 /* one byte length, no name conversion */
481 len = (unsigned int)pFindData->FileNameLength;
482 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
483 } else {
484 cFYI(1, ("Unknown findfirst level %d", level));
485 return -EINVAL;
486 }
487 cifsFile->srch_inf.resume_name_len = len;
488 cifsFile->srch_inf.presume_name = filename;
489 return rc;
490}
491
1da177e4
LT
492/* find the corresponding entry in the search */
493/* Note that the SMB server returns search entries for . and .. which
494 complicates logic here if we choose to parse for them and we do not
495 assume that they are located in the findfirst return buffer.*/
496/* We start counting in the buffer with entry 2 and increment for every
497 entry (do not increment for . or .. entry) */
498static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
3870253e 499 struct file *file, char **ppCurrentEntry, int *num_to_ret)
1da177e4
LT
500{
501 int rc = 0;
502 int pos_in_buf = 0;
503 loff_t first_entry_in_buffer;
504 loff_t index_to_find = file->f_pos;
3870253e 505 struct cifsFileInfo *cifsFile = file->private_data;
1da177e4 506 /* check if index in the buffer */
50c2f753 507
3870253e 508 if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
eafe8701 509 (num_to_ret == NULL))
1da177e4 510 return -ENOENT;
50c2f753 511
1da177e4 512 *ppCurrentEntry = NULL;
3870253e
SF
513 first_entry_in_buffer =
514 cifsFile->srch_inf.index_of_last_entry -
1da177e4 515 cifsFile->srch_inf.entries_in_buffer;
60808233
SF
516
517 /* if first entry in buf is zero then is first buffer
518 in search response data which means it is likely . and ..
519 will be in this buffer, although some servers do not return
520 . and .. for the root of a drive and for those we need
521 to start two entries earlier */
522
3979877e 523 dump_cifs_file_struct(file, "In fce ");
3870253e
SF
524 if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
525 is_dir_changed(file)) ||
eafe8701 526 (index_to_find < first_entry_in_buffer)) {
1da177e4 527 /* close and restart search */
790fe579 528 cFYI(1, ("search backing up - close and restart search"));
ddb4cbfc 529 write_lock(&GlobalSMBSeslock);
77c57ec8
SF
530 if (!cifsFile->srch_inf.endOfSearch &&
531 !cifsFile->invalidHandle) {
532 cifsFile->invalidHandle = true;
ddb4cbfc 533 write_unlock(&GlobalSMBSeslock);
77c57ec8 534 CIFSFindClose(xid, pTcon, cifsFile->netfid);
ddb4cbfc
SF
535 } else
536 write_unlock(&GlobalSMBSeslock);
4523cc30 537 if (cifsFile->srch_inf.ntwrk_buf_start) {
790fe579 538 cFYI(1, ("freeing SMB ff cache buf on search rewind"));
4523cc30 539 if (cifsFile->srch_inf.smallBuf)
d47d7c1a
SF
540 cifs_small_buf_release(cifsFile->srch_inf.
541 ntwrk_buf_start);
542 else
543 cifs_buf_release(cifsFile->srch_inf.
544 ntwrk_buf_start);
76c510ad 545 cifsFile->srch_inf.ntwrk_buf_start = NULL;
1da177e4 546 }
3870253e 547 rc = initiate_cifs_search(xid, file);
4523cc30 548 if (rc) {
790fe579
SF
549 cFYI(1, ("error %d reinitiating a search on rewind",
550 rc));
1da177e4
LT
551 return rc;
552 }
a364bc0b 553 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
1da177e4
LT
554 }
555
3870253e 556 while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
4b18f2a9 557 (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
790fe579 558 cFYI(1, ("calling findnext2"));
3870253e 559 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
dfb7533b 560 &cifsFile->srch_inf);
a364bc0b 561 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
4523cc30 562 if (rc)
1da177e4
LT
563 return -ENOENT;
564 }
4523cc30 565 if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
1da177e4
LT
566 /* we found the buffer that contains the entry */
567 /* scan and find it */
568 int i;
3870253e
SF
569 char *current_entry;
570 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
1da177e4
LT
571 smbCalcSize((struct smb_hdr *)
572 cifsFile->srch_inf.ntwrk_buf_start);
60808233
SF
573
574 current_entry = cifsFile->srch_inf.srch_entries_start;
1da177e4
LT
575 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
576 - cifsFile->srch_inf.entries_in_buffer;
577 pos_in_buf = index_to_find - first_entry_in_buffer;
790fe579 578 cFYI(1, ("found entry - pos_in_buf %d", pos_in_buf));
5bafd765 579
ad7a2926 580 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
dfb7533b 581 /* go entry by entry figuring out which is first */
3870253e 582 current_entry = nxt_dir_entry(current_entry, end_of_smb,
5bafd765 583 cifsFile->srch_inf.info_level);
1da177e4 584 }
790fe579 585 if ((current_entry == NULL) && (i < pos_in_buf)) {
1da177e4 586 /* BB fixme - check if we should flag this error */
3870253e 587 cERROR(1, ("reached end of buf searching for pos in buf"
1da177e4 588 " %d index to find %lld rc %d",
3870253e 589 pos_in_buf, index_to_find, rc));
1da177e4
LT
590 }
591 rc = 0;
592 *ppCurrentEntry = current_entry;
593 } else {
790fe579 594 cFYI(1, ("index not in buffer - could not findnext into it"));
1da177e4
LT
595 return 0;
596 }
597
790fe579
SF
598 if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
599 cFYI(1, ("can not return entries pos_in_buf beyond last"));
1da177e4
LT
600 *num_to_ret = 0;
601 } else
602 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
1da177e4
LT
603
604 return rc;
605}
606
607/* inode num, inode type and filename returned */
608static int cifs_get_name_from_search_buf(struct qstr *pqst,
609 char *current_entry, __u16 level, unsigned int unicode,
18295796 610 struct cifs_sb_info *cifs_sb, unsigned int max_len, __u64 *pinum)
1da177e4
LT
611{
612 int rc = 0;
613 unsigned int len = 0;
3870253e
SF
614 char *filename;
615 struct nls_table *nlt = cifs_sb->local_nls;
1da177e4
LT
616
617 *pinum = 0;
618
790fe579 619 if (level == SMB_FIND_FILE_UNIX) {
3870253e 620 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
1da177e4
LT
621
622 filename = &pFindData->FileName[0];
790fe579 623 if (unicode) {
1da177e4
LT
624 len = cifs_unicode_bytelen(filename);
625 } else {
626 /* BB should we make this strnlen of PATH_MAX? */
627 len = strnlen(filename, PATH_MAX);
628 }
629
cc0bad75 630 *pinum = le64_to_cpu(pFindData->basic.UniqueId);
790fe579 631 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
3870253e 632 FILE_DIRECTORY_INFO *pFindData =
1da177e4
LT
633 (FILE_DIRECTORY_INFO *)current_entry;
634 filename = &pFindData->FileName[0];
635 len = le32_to_cpu(pFindData->FileNameLength);
790fe579 636 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
3870253e 637 FILE_FULL_DIRECTORY_INFO *pFindData =
1da177e4
LT
638 (FILE_FULL_DIRECTORY_INFO *)current_entry;
639 filename = &pFindData->FileName[0];
640 len = le32_to_cpu(pFindData->FileNameLength);
790fe579 641 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
3870253e 642 SEARCH_ID_FULL_DIR_INFO *pFindData =
1da177e4
LT
643 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
644 filename = &pFindData->FileName[0];
645 len = le32_to_cpu(pFindData->FileNameLength);
85a6dac5 646 *pinum = le64_to_cpu(pFindData->UniqueId);
790fe579 647 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
3870253e 648 FILE_BOTH_DIRECTORY_INFO *pFindData =
1da177e4
LT
649 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
650 filename = &pFindData->FileName[0];
651 len = le32_to_cpu(pFindData->FileNameLength);
790fe579 652 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
ad7a2926 653 FIND_FILE_STANDARD_INFO *pFindData =
5bafd765
SF
654 (FIND_FILE_STANDARD_INFO *)current_entry;
655 filename = &pFindData->FileName[0];
656 /* one byte length, no name conversion */
657 len = (unsigned int)pFindData->FileNameLength;
1da177e4 658 } else {
790fe579 659 cFYI(1, ("Unknown findfirst level %d", level));
1da177e4
LT
660 return -EINVAL;
661 }
5bafd765 662
790fe579 663 if (len > max_len) {
3870253e 664 cERROR(1, ("bad search response length %d past smb end", len));
5bafd765
SF
665 return -EINVAL;
666 }
667
790fe579 668 if (unicode) {
f5884166
JL
669 pqst->len = cifs_from_ucs2((char *) pqst->name,
670 (__le16 *) filename,
18295796
JL
671 UNICODE_NAME_MAX,
672 min(len, max_len), nlt,
f5884166
JL
673 cifs_sb->mnt_cifs_flags &
674 CIFS_MOUNT_MAP_SPECIAL_CHR);
f12f98db 675 pqst->len -= nls_nullsize(nlt);
1da177e4
LT
676 } else {
677 pqst->name = filename;
678 pqst->len = len;
679 }
1da177e4
LT
680 return rc;
681}
682
18295796
JL
683static int cifs_filldir(char *pfindEntry, struct file *file, filldir_t filldir,
684 void *direntry, char *scratch_buf, unsigned int max_len)
1da177e4
LT
685{
686 int rc = 0;
687 struct qstr qstring;
3870253e 688 struct cifsFileInfo *pCifsF;
0b8f18e3 689 u64 inum;
cc0bad75 690 ino_t ino;
0b8f18e3 691 struct super_block *sb;
3870253e 692 struct cifs_sb_info *cifs_sb;
1da177e4 693 struct dentry *tmp_dentry;
cc0bad75 694 struct cifs_fattr fattr;
1da177e4
LT
695
696 /* get filename and len into qstring */
697 /* get dentry */
698 /* decide whether to create and populate ionde */
790fe579 699 if ((direntry == NULL) || (file == NULL))
1da177e4
LT
700 return -EINVAL;
701
702 pCifsF = file->private_data;
50c2f753 703
790fe579 704 if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
1da177e4
LT
705 return -ENOENT;
706
3870253e 707 rc = cifs_entry_is_dot(pfindEntry, pCifsF);
60808233 708 /* skip . and .. since we added them first */
790fe579 709 if (rc != 0)
60808233
SF
710 return 0;
711
0b8f18e3
JL
712 sb = file->f_path.dentry->d_sb;
713 cifs_sb = CIFS_SB(sb);
1da177e4
LT
714
715 qstring.name = scratch_buf;
3870253e 716 rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
1da177e4 717 pCifsF->srch_inf.info_level,
3870253e 718 pCifsF->srch_inf.unicode, cifs_sb,
0b8f18e3 719 max_len, &inum /* returned */);
1da177e4 720
790fe579 721 if (rc)
1da177e4
LT
722 return rc;
723
0b8f18e3 724 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
cc0bad75
JL
725 cifs_unix_basic_to_fattr(&fattr,
726 &((FILE_UNIX_INFO *) pfindEntry)->basic,
727 cifs_sb);
0b8f18e3
JL
728 else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
729 cifs_std_info_to_fattr(&fattr, (FIND_FILE_STANDARD_INFO *)
730 pfindEntry, cifs_sb);
731 else
732 cifs_dir_info_to_fattr(&fattr, (FILE_DIRECTORY_INFO *)
733 pfindEntry, cifs_sb);
b835bebe 734
ec06aedd 735 if (inum && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
0b8f18e3 736 fattr.cf_uniqueid = inum;
ec06aedd 737 } else {
0b8f18e3 738 fattr.cf_uniqueid = iunique(sb, ROOT_I);
ec06aedd
JL
739 cifs_autodisable_serverino(cifs_sb);
740 }
50c2f753 741
0b8f18e3
JL
742 ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
743 tmp_dentry = cifs_readdir_lookup(file->f_dentry, &qstring, &fattr);
3870253e
SF
744
745 rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
0b8f18e3
JL
746 ino, fattr.cf_dtype);
747
748 /*
749 * we can not return filldir errors to the caller since they are
750 * "normal" when the stat blocksize is too small - we return remapped
751 * error instead
752 *
753 * FIXME: This looks bogus. filldir returns -EOVERFLOW in the above
754 * case already. Why should we be clobbering other errors from it?
755 */
790fe579
SF
756 if (rc) {
757 cFYI(1, ("filldir rc = %d", rc));
7ca85ba7 758 rc = -EOVERFLOW;
1da177e4 759 }
1da177e4
LT
760 dput(tmp_dentry);
761 return rc;
762}
763
1da177e4
LT
764
765int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
766{
767 int rc = 0;
3870253e 768 int xid, i;
1da177e4
LT
769 struct cifs_sb_info *cifs_sb;
770 struct cifsTconInfo *pTcon;
771 struct cifsFileInfo *cifsFile = NULL;
3870253e 772 char *current_entry;
1da177e4 773 int num_to_fill = 0;
3870253e 774 char *tmp_buf = NULL;
50c2f753 775 char *end_of_smb;
18295796 776 unsigned int max_len;
1da177e4
LT
777
778 xid = GetXid();
779
e6a00296 780 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
1da177e4 781 pTcon = cifs_sb->tcon;
790fe579 782 if (pTcon == NULL)
1da177e4
LT
783 return -EINVAL;
784
1da177e4
LT
785 switch ((int) file->f_pos) {
786 case 0:
60808233 787 if (filldir(direntry, ".", 1, file->f_pos,
e6a00296 788 file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
60808233 789 cERROR(1, ("Filldir for current dir failed"));
1da177e4
LT
790 rc = -ENOMEM;
791 break;
792 }
60808233 793 file->f_pos++;
1da177e4 794 case 1:
60808233 795 if (filldir(direntry, "..", 2, file->f_pos,
e6a00296 796 file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
26a21b98 797 cERROR(1, ("Filldir for parent dir failed"));
1da177e4
LT
798 rc = -ENOMEM;
799 break;
800 }
60808233
SF
801 file->f_pos++;
802 default:
3870253e
SF
803 /* 1) If search is active,
804 is in current search buffer?
1da177e4
LT
805 if it before then restart search
806 if after then keep searching till find it */
807
790fe579 808 if (file->private_data == NULL) {
3870253e 809 rc = initiate_cifs_search(xid, file);
790fe579
SF
810 cFYI(1, ("initiate cifs search rc %d", rc));
811 if (rc) {
1da177e4
LT
812 FreeXid(xid);
813 return rc;
814 }
815 }
790fe579 816 if (file->private_data == NULL) {
1da177e4
LT
817 rc = -EINVAL;
818 FreeXid(xid);
819 return rc;
820 }
821 cifsFile = file->private_data;
822 if (cifsFile->srch_inf.endOfSearch) {
790fe579 823 if (cifsFile->srch_inf.emptyDir) {
1da177e4
LT
824 cFYI(1, ("End of search, empty dir"));
825 rc = 0;
826 break;
827 }
828 } /* else {
4b18f2a9 829 cifsFile->invalidHandle = true;
1da177e4 830 CIFSFindClose(xid, pTcon, cifsFile->netfid);
aaa9bbe0 831 } */
1da177e4 832
3870253e
SF
833 rc = find_cifs_entry(xid, pTcon, file,
834 &current_entry, &num_to_fill);
790fe579
SF
835 if (rc) {
836 cFYI(1, ("fce error %d", rc));
1da177e4
LT
837 goto rddir2_exit;
838 } else if (current_entry != NULL) {
790fe579 839 cFYI(1, ("entry %lld found", file->f_pos));
1da177e4 840 } else {
790fe579 841 cFYI(1, ("could not find entry"));
1da177e4
LT
842 goto rddir2_exit;
843 }
790fe579
SF
844 cFYI(1, ("loop through %d times filling dir for net buf %p",
845 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start));
5bafd765
SF
846 max_len = smbCalcSize((struct smb_hdr *)
847 cifsFile->srch_inf.ntwrk_buf_start);
848 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
849
f5884166 850 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
790fe579
SF
851 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
852 if (current_entry == NULL) {
1da177e4 853 /* evaluate whether this case is an error */
26f57364 854 cERROR(1, ("past SMB end, num to fill %d i %d",
1da177e4
LT
855 num_to_fill, i));
856 break;
857 }
60808233
SF
858 /* if buggy server returns . and .. late do
859 we want to check for that here? */
5bafd765
SF
860 rc = cifs_filldir(current_entry, file,
861 filldir, direntry, tmp_buf, max_len);
790fe579 862 if (rc == -EOVERFLOW) {
7ca85ba7
SF
863 rc = 0;
864 break;
865 }
866
1da177e4 867 file->f_pos++;
790fe579 868 if (file->f_pos ==
3979877e 869 cifsFile->srch_inf.index_of_last_entry) {
790fe579
SF
870 cFYI(1, ("last entry in buf at pos %lld %s",
871 file->f_pos, tmp_buf));
872 cifs_save_resume_key(current_entry, cifsFile);
1da177e4 873 break;
790fe579
SF
874 } else
875 current_entry =
5bafd765
SF
876 nxt_dir_entry(current_entry, end_of_smb,
877 cifsFile->srch_inf.info_level);
1da177e4
LT
878 }
879 kfree(tmp_buf);
880 break;
881 } /* end switch */
882
883rddir2_exit:
1da177e4
LT
884 FreeXid(xid);
885 return rc;
886}