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