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