1 /*
2 * fs/cifs/readdir.c
3 *
4 * Directory search handling
5 *
6 * Copyright (C) International Business Machines Corp., 2004, 2008
7 * Copyright (C) Red Hat, Inc., 2011
8 * Author(s): Steve French (sfrench@us.ibm.com)
9 *
10 * This library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published
12 * by the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
18 * the GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 #include <linux/fs.h>
25 #include <linux/pagemap.h>
26 #include <linux/slab.h>
27 #include <linux/stat.h>
28 #include "cifspdu.h"
29 #include "cifsglob.h"
30 #include "cifsproto.h"
31 #include "cifs_unicode.h"
32 #include "cifs_debug.h"
33 #include "cifs_fs_sb.h"
34 #include "cifsfs.h"
35 #include "smb2proto.h"
36 #include "fs_context.h"
37
38 /*
39 * To be safe - for UCS to UTF-8 with strings loaded with the rare long
40 * characters alloc more to account for such multibyte target UTF-8
41 * characters.
42 */
43 #define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
44
45 #ifdef CONFIG_CIFS_DEBUG2
dump_cifs_file_struct(struct file * file,char * label)46 static void dump_cifs_file_struct(struct file *file, char *label)
47 {
48 struct cifsFileInfo *cf;
49
50 if (file) {
51 cf = file->private_data;
52 if (cf == NULL) {
53 cifs_dbg(FYI, "empty cifs private file data\n");
54 return;
55 }
56 if (cf->invalidHandle)
57 cifs_dbg(FYI, "Invalid handle\n");
58 if (cf->srch_inf.endOfSearch)
59 cifs_dbg(FYI, "end of search\n");
60 if (cf->srch_inf.emptyDir)
61 cifs_dbg(FYI, "empty dir\n");
62 }
63 }
64 #else
dump_cifs_file_struct(struct file * file,char * label)65 static inline void dump_cifs_file_struct(struct file *file, char *label)
66 {
67 }
68 #endif /* DEBUG2 */
69
70 /*
71 * Attempt to preload the dcache with the results from the FIND_FIRST/NEXT
72 *
73 * Find the dentry that matches "name". If there isn't one, create one. If it's
74 * a negative dentry or the uniqueid or filetype(mode) changed,
75 * then drop it and recreate it.
76 */
77 static void
cifs_prime_dcache(struct dentry * parent,struct qstr * name,struct cifs_fattr * fattr)78 cifs_prime_dcache(struct dentry *parent, struct qstr *name,
79 struct cifs_fattr *fattr)
80 {
81 struct dentry *dentry, *alias;
82 struct inode *inode;
83 struct super_block *sb = parent->d_sb;
84 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
85 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
86
87 cifs_dbg(FYI, "%s: for %s\n", __func__, name->name);
88
89 dentry = d_hash_and_lookup(parent, name);
90 if (!dentry) {
91 /*
92 * If we know that the inode will need to be revalidated
93 * immediately, then don't create a new dentry for it.
94 * We'll end up doing an on the wire call either way and
95 * this spares us an invalidation.
96 */
97 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
98 return;
99 retry:
100 dentry = d_alloc_parallel(parent, name, &wq);
101 }
102 if (IS_ERR(dentry))
103 return;
104 if (!d_in_lookup(dentry)) {
105 inode = d_inode(dentry);
106 if (inode) {
107 if (d_mountpoint(dentry)) {
108 dput(dentry);
109 return;
110 }
111 /*
112 * If we're generating inode numbers, then we don't
113 * want to clobber the existing one with the one that
114 * the readdir code created.
115 */
116 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM))
117 fattr->cf_uniqueid = CIFS_I(inode)->uniqueid;
118
119 /* update inode in place
120 * if both i_ino and i_mode didn't change */
121 if (CIFS_I(inode)->uniqueid == fattr->cf_uniqueid &&
122 cifs_fattr_to_inode(inode, fattr) == 0) {
123 dput(dentry);
124 return;
125 }
126 }
127 d_invalidate(dentry);
128 dput(dentry);
129 goto retry;
130 } else {
131 inode = cifs_iget(sb, fattr);
132 if (!inode)
133 inode = ERR_PTR(-ENOMEM);
134 alias = d_splice_alias(inode, dentry);
135 d_lookup_done(dentry);
136 if (alias && !IS_ERR(alias))
137 dput(alias);
138 }
139 dput(dentry);
140 }
141
reparse_file_needs_reval(const struct cifs_fattr * fattr)142 static bool reparse_file_needs_reval(const struct cifs_fattr *fattr)
143 {
144 if (!(fattr->cf_cifsattrs & ATTR_REPARSE))
145 return false;
146 /*
147 * The DFS tags should be only intepreted by server side as per
148 * MS-FSCC 2.1.2.1, but let's include them anyway.
149 *
150 * Besides, if cf_cifstag is unset (0), then we still need it to be
151 * revalidated to know exactly what reparse point it is.
152 */
153 switch (fattr->cf_cifstag) {
154 case IO_REPARSE_TAG_DFS:
155 case IO_REPARSE_TAG_DFSR:
156 case IO_REPARSE_TAG_SYMLINK:
157 case IO_REPARSE_TAG_NFS:
158 case 0:
159 return true;
160 }
161 return false;
162 }
163
164 static void
cifs_fill_common_info(struct cifs_fattr * fattr,struct cifs_sb_info * cifs_sb)165 cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
166 {
167 fattr->cf_uid = cifs_sb->ctx->linux_uid;
168 fattr->cf_gid = cifs_sb->ctx->linux_gid;
169
170 /*
171 * The IO_REPARSE_TAG_LX_ tags originally were used by WSL but they
172 * are preferred by the Linux client in some cases since, unlike
173 * the NFS reparse tag (or EAs), they don't require an extra query
174 * to determine which type of special file they represent.
175 * TODO: go through all documented reparse tags to see if we can
176 * reasonably map some of them to directories vs. files vs. symlinks
177 */
178 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
179 fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
180 fattr->cf_dtype = DT_DIR;
181 } else if (fattr->cf_cifstag == IO_REPARSE_TAG_LX_SYMLINK) {
182 fattr->cf_mode |= S_IFLNK | cifs_sb->ctx->file_mode;
183 fattr->cf_dtype = DT_LNK;
184 } else if (fattr->cf_cifstag == IO_REPARSE_TAG_LX_FIFO) {
185 fattr->cf_mode |= S_IFIFO | cifs_sb->ctx->file_mode;
186 fattr->cf_dtype = DT_FIFO;
187 } else if (fattr->cf_cifstag == IO_REPARSE_TAG_AF_UNIX) {
188 fattr->cf_mode |= S_IFSOCK | cifs_sb->ctx->file_mode;
189 fattr->cf_dtype = DT_SOCK;
190 } else if (fattr->cf_cifstag == IO_REPARSE_TAG_LX_CHR) {
191 fattr->cf_mode |= S_IFCHR | cifs_sb->ctx->file_mode;
192 fattr->cf_dtype = DT_CHR;
193 } else if (fattr->cf_cifstag == IO_REPARSE_TAG_LX_BLK) {
194 fattr->cf_mode |= S_IFBLK | cifs_sb->ctx->file_mode;
195 fattr->cf_dtype = DT_BLK;
196 } else { /* TODO: should we mark some other reparse points (like DFSR) as directories? */
197 fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
198 fattr->cf_dtype = DT_REG;
199 }
200
201 /*
202 * We need to revalidate it further to make a decision about whether it
203 * is a symbolic link, DFS referral or a reparse point with a direct
204 * access like junctions, deduplicated files, NFS symlinks.
205 */
206 if (reparse_file_needs_reval(fattr))
207 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
208
209 /* non-unix readdir doesn't provide nlink */
210 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
211
212 if (fattr->cf_cifsattrs & ATTR_READONLY)
213 fattr->cf_mode &= ~S_IWUGO;
214
215 /*
216 * We of course don't get ACL info in FIND_FIRST/NEXT results, so
217 * mark it for revalidation so that "ls -l" will look right. It might
218 * be super-slow, but if we don't do this then the ownership of files
219 * may look wrong since the inodes may not have timed out by the time
220 * "ls" does a stat() call on them.
221 */
222 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
223 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID))
224 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
225
226 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
227 fattr->cf_cifsattrs & ATTR_SYSTEM) {
228 if (fattr->cf_eof == 0) {
229 fattr->cf_mode &= ~S_IFMT;
230 fattr->cf_mode |= S_IFIFO;
231 fattr->cf_dtype = DT_FIFO;
232 } else {
233 /*
234 * trying to get the type and mode via SFU can be slow,
235 * so just call those regular files for now, and mark
236 * for reval
237 */
238 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
239 }
240 }
241 }
242
243 /* Fill a cifs_fattr struct with info from SMB_FIND_FILE_POSIX_INFO. */
244 static void
cifs_posix_to_fattr(struct cifs_fattr * fattr,struct smb2_posix_info * info,struct cifs_sb_info * cifs_sb)245 cifs_posix_to_fattr(struct cifs_fattr *fattr, struct smb2_posix_info *info,
246 struct cifs_sb_info *cifs_sb)
247 {
248 struct smb2_posix_info_parsed parsed;
249
250 posix_info_parse(info, NULL, &parsed);
251
252 memset(fattr, 0, sizeof(*fattr));
253 fattr->cf_uniqueid = le64_to_cpu(info->Inode);
254 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
255 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
256
257 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
258 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
259 fattr->cf_ctime = cifs_NTtimeToUnix(info->CreationTime);
260
261 fattr->cf_nlink = le32_to_cpu(info->HardLinks);
262 fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
263
264 /*
265 * Since we set the inode type below we need to mask off
266 * to avoid strange results if bits set above.
267 * XXX: why not make server&client use the type bits?
268 */
269 fattr->cf_mode = le32_to_cpu(info->Mode) & ~S_IFMT;
270
271 cifs_dbg(FYI, "posix fattr: dev %d, reparse %d, mode %o\n",
272 le32_to_cpu(info->DeviceId),
273 le32_to_cpu(info->ReparseTag),
274 le32_to_cpu(info->Mode));
275
276 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
277 fattr->cf_mode |= S_IFDIR;
278 fattr->cf_dtype = DT_DIR;
279 } else {
280 /*
281 * mark anything that is not a dir as regular
282 * file. special files should have the REPARSE
283 * attribute and will be marked as needing revaluation
284 */
285 fattr->cf_mode |= S_IFREG;
286 fattr->cf_dtype = DT_REG;
287 }
288
289 if (reparse_file_needs_reval(fattr))
290 fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
291
292 sid_to_id(cifs_sb, &parsed.owner, fattr, SIDOWNER);
293 sid_to_id(cifs_sb, &parsed.group, fattr, SIDGROUP);
294 }
295
__dir_info_to_fattr(struct cifs_fattr * fattr,const void * info)296 static void __dir_info_to_fattr(struct cifs_fattr *fattr, const void *info)
297 {
298 const FILE_DIRECTORY_INFO *fi = info;
299
300 memset(fattr, 0, sizeof(*fattr));
301 fattr->cf_cifsattrs = le32_to_cpu(fi->ExtFileAttributes);
302 fattr->cf_eof = le64_to_cpu(fi->EndOfFile);
303 fattr->cf_bytes = le64_to_cpu(fi->AllocationSize);
304 fattr->cf_createtime = le64_to_cpu(fi->CreationTime);
305 fattr->cf_atime = cifs_NTtimeToUnix(fi->LastAccessTime);
306 fattr->cf_ctime = cifs_NTtimeToUnix(fi->ChangeTime);
307 fattr->cf_mtime = cifs_NTtimeToUnix(fi->LastWriteTime);
308 }
309
310 void
cifs_dir_info_to_fattr(struct cifs_fattr * fattr,FILE_DIRECTORY_INFO * info,struct cifs_sb_info * cifs_sb)311 cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info,
312 struct cifs_sb_info *cifs_sb)
313 {
314 __dir_info_to_fattr(fattr, info);
315 cifs_fill_common_info(fattr, cifs_sb);
316 }
317
cifs_fulldir_info_to_fattr(struct cifs_fattr * fattr,SEARCH_ID_FULL_DIR_INFO * info,struct cifs_sb_info * cifs_sb)318 static void cifs_fulldir_info_to_fattr(struct cifs_fattr *fattr,
319 SEARCH_ID_FULL_DIR_INFO *info,
320 struct cifs_sb_info *cifs_sb)
321 {
322 __dir_info_to_fattr(fattr, info);
323
324 /* See MS-FSCC 2.4.18 FileIdFullDirectoryInformation */
325 if (fattr->cf_cifsattrs & ATTR_REPARSE)
326 fattr->cf_cifstag = le32_to_cpu(info->EaSize);
327 cifs_fill_common_info(fattr, cifs_sb);
328 }
329
330 static void
cifs_std_info_to_fattr(struct cifs_fattr * fattr,FIND_FILE_STANDARD_INFO * info,struct cifs_sb_info * cifs_sb)331 cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info,
332 struct cifs_sb_info *cifs_sb)
333 {
334 int offset = cifs_sb_master_tcon(cifs_sb)->ses->server->timeAdj;
335
336 memset(fattr, 0, sizeof(*fattr));
337 fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate,
338 info->LastAccessTime, offset);
339 fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate,
340 info->LastWriteTime, offset);
341 fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate,
342 info->LastWriteTime, offset);
343
344 fattr->cf_cifsattrs = le16_to_cpu(info->Attributes);
345 fattr->cf_bytes = le32_to_cpu(info->AllocationSize);
346 fattr->cf_eof = le32_to_cpu(info->DataSize);
347
348 cifs_fill_common_info(fattr, cifs_sb);
349 }
350
351 /* BB eventually need to add the following helper function to
352 resolve NT_STATUS_STOPPED_ON_SYMLINK return code when
353 we try to do FindFirst on (NTFS) directory symlinks */
354 /*
355 int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
356 unsigned int xid)
357 {
358 __u16 fid;
359 int len;
360 int oplock = 0;
361 int rc;
362 struct cifs_tcon *ptcon = cifs_sb_tcon(cifs_sb);
363 char *tmpbuffer;
364
365 rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ,
366 OPEN_REPARSE_POINT, &fid, &oplock, NULL,
367 cifs_sb->local_nls,
368 cifs_remap(cifs_sb);
369 if (!rc) {
370 tmpbuffer = kmalloc(maxpath);
371 rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path,
372 tmpbuffer,
373 maxpath -1,
374 fid,
375 cifs_sb->local_nls);
376 if (CIFSSMBClose(xid, ptcon, fid)) {
377 cifs_dbg(FYI, "Error closing temporary reparsepoint open\n");
378 }
379 }
380 }
381 */
382
383 static int
initiate_cifs_search(const unsigned int xid,struct file * file,const char * full_path)384 initiate_cifs_search(const unsigned int xid, struct file *file,
385 const char *full_path)
386 {
387 __u16 search_flags;
388 int rc = 0;
389 struct cifsFileInfo *cifsFile;
390 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
391 struct tcon_link *tlink = NULL;
392 struct cifs_tcon *tcon;
393 struct TCP_Server_Info *server;
394
395 if (file->private_data == NULL) {
396 tlink = cifs_sb_tlink(cifs_sb);
397 if (IS_ERR(tlink))
398 return PTR_ERR(tlink);
399
400 cifsFile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
401 if (cifsFile == NULL) {
402 rc = -ENOMEM;
403 goto error_exit;
404 }
405 spin_lock_init(&cifsFile->file_info_lock);
406 file->private_data = cifsFile;
407 cifsFile->tlink = cifs_get_tlink(tlink);
408 tcon = tlink_tcon(tlink);
409 } else {
410 cifsFile = file->private_data;
411 tcon = tlink_tcon(cifsFile->tlink);
412 }
413
414 server = tcon->ses->server;
415
416 if (!server->ops->query_dir_first) {
417 rc = -ENOSYS;
418 goto error_exit;
419 }
420
421 cifsFile->invalidHandle = true;
422 cifsFile->srch_inf.endOfSearch = false;
423
424 cifs_dbg(FYI, "Full path: %s start at: %lld\n", full_path, file->f_pos);
425
426 ffirst_retry:
427 /* test for Unix extensions */
428 /* but now check for them on the share/mount not on the SMB session */
429 /* if (cap_unix(tcon->ses) { */
430 if (tcon->unix_ext)
431 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
432 else if (tcon->posix_extensions)
433 cifsFile->srch_inf.info_level = SMB_FIND_FILE_POSIX_INFO;
434 else if ((tcon->ses->capabilities &
435 tcon->ses->server->vals->cap_nt_find) == 0) {
436 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
437 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
438 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
439 } else /* not srvinos - BB fixme add check for backlevel? */ {
440 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
441 }
442
443 search_flags = CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME;
444 if (backup_cred(cifs_sb))
445 search_flags |= CIFS_SEARCH_BACKUP_SEARCH;
446
447 rc = server->ops->query_dir_first(xid, tcon, full_path, cifs_sb,
448 &cifsFile->fid, search_flags,
449 &cifsFile->srch_inf);
450
451 if (rc == 0)
452 cifsFile->invalidHandle = false;
453 /* BB add following call to handle readdir on new NTFS symlink errors
454 else if STATUS_STOPPED_ON_SYMLINK
455 call get_symlink_reparse_path and retry with new path */
456 else if ((rc == -EOPNOTSUPP) &&
457 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
458 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
459 goto ffirst_retry;
460 }
461 error_exit:
462 cifs_put_tlink(tlink);
463 return rc;
464 }
465
466 /* return length of unicode string in bytes */
cifs_unicode_bytelen(const char * str)467 static int cifs_unicode_bytelen(const char *str)
468 {
469 int len;
470 const __le16 *ustr = (const __le16 *)str;
471
472 for (len = 0; len <= PATH_MAX; len++) {
473 if (ustr[len] == 0)
474 return len << 1;
475 }
476 cifs_dbg(FYI, "Unicode string longer than PATH_MAX found\n");
477 return len << 1;
478 }
479
nxt_dir_entry(char * old_entry,char * end_of_smb,int level)480 static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
481 {
482 char *new_entry;
483 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
484
485 if (level == SMB_FIND_FILE_INFO_STANDARD) {
486 FIND_FILE_STANDARD_INFO *pfData;
487 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
488
489 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
490 pfData->FileNameLength;
491 } else {
492 u32 next_offset = le32_to_cpu(pDirInfo->NextEntryOffset);
493
494 if (old_entry + next_offset < old_entry) {
495 cifs_dbg(VFS, "Invalid offset %u\n", next_offset);
496 return NULL;
497 }
498 new_entry = old_entry + next_offset;
499 }
500 cifs_dbg(FYI, "new entry %p old entry %p\n", new_entry, old_entry);
501 /* validate that new_entry is not past end of SMB */
502 if (new_entry >= end_of_smb) {
503 cifs_dbg(VFS, "search entry %p began after end of SMB %p old entry %p\n",
504 new_entry, end_of_smb, old_entry);
505 return NULL;
506 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
507 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
508 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
509 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
510 cifs_dbg(VFS, "search entry %p extends after end of SMB %p\n",
511 new_entry, end_of_smb);
512 return NULL;
513 } else
514 return new_entry;
515
516 }
517
518 struct cifs_dirent {
519 const char *name;
520 size_t namelen;
521 u32 resume_key;
522 u64 ino;
523 };
524
cifs_fill_dirent_posix(struct cifs_dirent * de,const struct smb2_posix_info * info)525 static void cifs_fill_dirent_posix(struct cifs_dirent *de,
526 const struct smb2_posix_info *info)
527 {
528 struct smb2_posix_info_parsed parsed;
529
530 /* payload should have already been checked at this point */
531 if (posix_info_parse(info, NULL, &parsed) < 0) {
532 cifs_dbg(VFS, "Invalid POSIX info payload\n");
533 return;
534 }
535
536 de->name = parsed.name;
537 de->namelen = parsed.name_len;
538 de->resume_key = info->Ignored;
539 de->ino = le64_to_cpu(info->Inode);
540 }
541
cifs_fill_dirent_unix(struct cifs_dirent * de,const FILE_UNIX_INFO * info,bool is_unicode)542 static void cifs_fill_dirent_unix(struct cifs_dirent *de,
543 const FILE_UNIX_INFO *info, bool is_unicode)
544 {
545 de->name = &info->FileName[0];
546 if (is_unicode)
547 de->namelen = cifs_unicode_bytelen(de->name);
548 else
549 de->namelen = strnlen(de->name, PATH_MAX);
550 de->resume_key = info->ResumeKey;
551 de->ino = le64_to_cpu(info->basic.UniqueId);
552 }
553
cifs_fill_dirent_dir(struct cifs_dirent * de,const FILE_DIRECTORY_INFO * info)554 static void cifs_fill_dirent_dir(struct cifs_dirent *de,
555 const FILE_DIRECTORY_INFO *info)
556 {
557 de->name = &info->FileName[0];
558 de->namelen = le32_to_cpu(info->FileNameLength);
559 de->resume_key = info->FileIndex;
560 }
561
cifs_fill_dirent_full(struct cifs_dirent * de,const FILE_FULL_DIRECTORY_INFO * info)562 static void cifs_fill_dirent_full(struct cifs_dirent *de,
563 const FILE_FULL_DIRECTORY_INFO *info)
564 {
565 de->name = &info->FileName[0];
566 de->namelen = le32_to_cpu(info->FileNameLength);
567 de->resume_key = info->FileIndex;
568 }
569
cifs_fill_dirent_search(struct cifs_dirent * de,const SEARCH_ID_FULL_DIR_INFO * info)570 static void cifs_fill_dirent_search(struct cifs_dirent *de,
571 const SEARCH_ID_FULL_DIR_INFO *info)
572 {
573 de->name = &info->FileName[0];
574 de->namelen = le32_to_cpu(info->FileNameLength);
575 de->resume_key = info->FileIndex;
576 de->ino = le64_to_cpu(info->UniqueId);
577 }
578
cifs_fill_dirent_both(struct cifs_dirent * de,const FILE_BOTH_DIRECTORY_INFO * info)579 static void cifs_fill_dirent_both(struct cifs_dirent *de,
580 const FILE_BOTH_DIRECTORY_INFO *info)
581 {
582 de->name = &info->FileName[0];
583 de->namelen = le32_to_cpu(info->FileNameLength);
584 de->resume_key = info->FileIndex;
585 }
586
cifs_fill_dirent_std(struct cifs_dirent * de,const FIND_FILE_STANDARD_INFO * info)587 static void cifs_fill_dirent_std(struct cifs_dirent *de,
588 const FIND_FILE_STANDARD_INFO *info)
589 {
590 de->name = &info->FileName[0];
591 /* one byte length, no endianess conversion */
592 de->namelen = info->FileNameLength;
593 de->resume_key = info->ResumeKey;
594 }
595
cifs_fill_dirent(struct cifs_dirent * de,const void * info,u16 level,bool is_unicode)596 static int cifs_fill_dirent(struct cifs_dirent *de, const void *info,
597 u16 level, bool is_unicode)
598 {
599 memset(de, 0, sizeof(*de));
600
601 switch (level) {
602 case SMB_FIND_FILE_POSIX_INFO:
603 cifs_fill_dirent_posix(de, info);
604 break;
605 case SMB_FIND_FILE_UNIX:
606 cifs_fill_dirent_unix(de, info, is_unicode);
607 break;
608 case SMB_FIND_FILE_DIRECTORY_INFO:
609 cifs_fill_dirent_dir(de, info);
610 break;
611 case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
612 cifs_fill_dirent_full(de, info);
613 break;
614 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
615 cifs_fill_dirent_search(de, info);
616 break;
617 case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
618 cifs_fill_dirent_both(de, info);
619 break;
620 case SMB_FIND_FILE_INFO_STANDARD:
621 cifs_fill_dirent_std(de, info);
622 break;
623 default:
624 cifs_dbg(FYI, "Unknown findfirst level %d\n", level);
625 return -EINVAL;
626 }
627
628 return 0;
629 }
630
631 #define UNICODE_DOT cpu_to_le16(0x2e)
632
633 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
cifs_entry_is_dot(struct cifs_dirent * de,bool is_unicode)634 static int cifs_entry_is_dot(struct cifs_dirent *de, bool is_unicode)
635 {
636 int rc = 0;
637
638 if (!de->name)
639 return 0;
640
641 if (is_unicode) {
642 __le16 *ufilename = (__le16 *)de->name;
643 if (de->namelen == 2) {
644 /* check for . */
645 if (ufilename[0] == UNICODE_DOT)
646 rc = 1;
647 } else if (de->namelen == 4) {
648 /* check for .. */
649 if (ufilename[0] == UNICODE_DOT &&
650 ufilename[1] == UNICODE_DOT)
651 rc = 2;
652 }
653 } else /* ASCII */ {
654 if (de->namelen == 1) {
655 if (de->name[0] == '.')
656 rc = 1;
657 } else if (de->namelen == 2) {
658 if (de->name[0] == '.' && de->name[1] == '.')
659 rc = 2;
660 }
661 }
662
663 return rc;
664 }
665
666 /* Check if directory that we are searching has changed so we can decide
667 whether we can use the cached search results from the previous search */
is_dir_changed(struct file * file)668 static int is_dir_changed(struct file *file)
669 {
670 struct inode *inode = file_inode(file);
671 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
672
673 if (cifsInfo->time == 0)
674 return 1; /* directory was changed, perhaps due to unlink */
675 else
676 return 0;
677
678 }
679
cifs_save_resume_key(const char * current_entry,struct cifsFileInfo * file_info)680 static int cifs_save_resume_key(const char *current_entry,
681 struct cifsFileInfo *file_info)
682 {
683 struct cifs_dirent de;
684 int rc;
685
686 rc = cifs_fill_dirent(&de, current_entry, file_info->srch_inf.info_level,
687 file_info->srch_inf.unicode);
688 if (!rc) {
689 file_info->srch_inf.presume_name = de.name;
690 file_info->srch_inf.resume_name_len = de.namelen;
691 file_info->srch_inf.resume_key = de.resume_key;
692 }
693 return rc;
694 }
695
696 /*
697 * Find the corresponding entry in the search. Note that the SMB server returns
698 * search entries for . and .. which complicates logic here if we choose to
699 * parse for them and we do not assume that they are located in the findfirst
700 * return buffer. We start counting in the buffer with entry 2 and increment for
701 * every entry (do not increment for . or .. entry).
702 */
703 static int
find_cifs_entry(const unsigned int xid,struct cifs_tcon * tcon,loff_t pos,struct file * file,const char * full_path,char ** current_entry,int * num_to_ret)704 find_cifs_entry(const unsigned int xid, struct cifs_tcon *tcon, loff_t pos,
705 struct file *file, const char *full_path,
706 char **current_entry, int *num_to_ret)
707 {
708 __u16 search_flags;
709 int rc = 0;
710 int pos_in_buf = 0;
711 loff_t first_entry_in_buffer;
712 loff_t index_to_find = pos;
713 struct cifsFileInfo *cfile = file->private_data;
714 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
715 struct TCP_Server_Info *server = tcon->ses->server;
716 /* check if index in the buffer */
717
718 if (!server->ops->query_dir_first || !server->ops->query_dir_next)
719 return -ENOSYS;
720
721 if ((cfile == NULL) || (current_entry == NULL) || (num_to_ret == NULL))
722 return -ENOENT;
723
724 *current_entry = NULL;
725 first_entry_in_buffer = cfile->srch_inf.index_of_last_entry -
726 cfile->srch_inf.entries_in_buffer;
727
728 /*
729 * If first entry in buf is zero then is first buffer
730 * in search response data which means it is likely . and ..
731 * will be in this buffer, although some servers do not return
732 * . and .. for the root of a drive and for those we need
733 * to start two entries earlier.
734 */
735
736 dump_cifs_file_struct(file, "In fce ");
737 if (((index_to_find < cfile->srch_inf.index_of_last_entry) &&
738 is_dir_changed(file)) || (index_to_find < first_entry_in_buffer)) {
739 /* close and restart search */
740 cifs_dbg(FYI, "search backing up - close and restart search\n");
741 spin_lock(&cfile->file_info_lock);
742 if (server->ops->dir_needs_close(cfile)) {
743 cfile->invalidHandle = true;
744 spin_unlock(&cfile->file_info_lock);
745 if (server->ops->close_dir)
746 server->ops->close_dir(xid, tcon, &cfile->fid);
747 } else
748 spin_unlock(&cfile->file_info_lock);
749 if (cfile->srch_inf.ntwrk_buf_start) {
750 cifs_dbg(FYI, "freeing SMB ff cache buf on search rewind\n");
751 if (cfile->srch_inf.smallBuf)
752 cifs_small_buf_release(cfile->srch_inf.
753 ntwrk_buf_start);
754 else
755 cifs_buf_release(cfile->srch_inf.
756 ntwrk_buf_start);
757 cfile->srch_inf.ntwrk_buf_start = NULL;
758 }
759 rc = initiate_cifs_search(xid, file, full_path);
760 if (rc) {
761 cifs_dbg(FYI, "error %d reinitiating a search on rewind\n",
762 rc);
763 return rc;
764 }
765 /* FindFirst/Next set last_entry to NULL on malformed reply */
766 if (cfile->srch_inf.last_entry)
767 cifs_save_resume_key(cfile->srch_inf.last_entry, cfile);
768 }
769
770 search_flags = CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME;
771 if (backup_cred(cifs_sb))
772 search_flags |= CIFS_SEARCH_BACKUP_SEARCH;
773
774 while ((index_to_find >= cfile->srch_inf.index_of_last_entry) &&
775 (rc == 0) && !cfile->srch_inf.endOfSearch) {
776 cifs_dbg(FYI, "calling findnext2\n");
777 rc = server->ops->query_dir_next(xid, tcon, &cfile->fid,
778 search_flags,
779 &cfile->srch_inf);
780 /* FindFirst/Next set last_entry to NULL on malformed reply */
781 if (cfile->srch_inf.last_entry)
782 cifs_save_resume_key(cfile->srch_inf.last_entry, cfile);
783 if (rc)
784 return -ENOENT;
785 }
786 if (index_to_find < cfile->srch_inf.index_of_last_entry) {
787 /* we found the buffer that contains the entry */
788 /* scan and find it */
789 int i;
790 char *cur_ent;
791 char *end_of_smb;
792
793 if (cfile->srch_inf.ntwrk_buf_start == NULL) {
794 cifs_dbg(VFS, "ntwrk_buf_start is NULL during readdir\n");
795 return -EIO;
796 }
797
798 end_of_smb = cfile->srch_inf.ntwrk_buf_start +
799 server->ops->calc_smb_size(
800 cfile->srch_inf.ntwrk_buf_start,
801 server);
802
803 cur_ent = cfile->srch_inf.srch_entries_start;
804 first_entry_in_buffer = cfile->srch_inf.index_of_last_entry
805 - cfile->srch_inf.entries_in_buffer;
806 pos_in_buf = index_to_find - first_entry_in_buffer;
807 cifs_dbg(FYI, "found entry - pos_in_buf %d\n", pos_in_buf);
808
809 for (i = 0; (i < (pos_in_buf)) && (cur_ent != NULL); i++) {
810 /* go entry by entry figuring out which is first */
811 cur_ent = nxt_dir_entry(cur_ent, end_of_smb,
812 cfile->srch_inf.info_level);
813 }
814 if ((cur_ent == NULL) && (i < pos_in_buf)) {
815 /* BB fixme - check if we should flag this error */
816 cifs_dbg(VFS, "reached end of buf searching for pos in buf %d index to find %lld rc %d\n",
817 pos_in_buf, index_to_find, rc);
818 }
819 rc = 0;
820 *current_entry = cur_ent;
821 } else {
822 cifs_dbg(FYI, "index not in buffer - could not findnext into it\n");
823 return 0;
824 }
825
826 if (pos_in_buf >= cfile->srch_inf.entries_in_buffer) {
827 cifs_dbg(FYI, "can not return entries pos_in_buf beyond last\n");
828 *num_to_ret = 0;
829 } else
830 *num_to_ret = cfile->srch_inf.entries_in_buffer - pos_in_buf;
831
832 return rc;
833 }
834
cifs_filldir(char * find_entry,struct file * file,struct dir_context * ctx,char * scratch_buf,unsigned int max_len)835 static int cifs_filldir(char *find_entry, struct file *file,
836 struct dir_context *ctx,
837 char *scratch_buf, unsigned int max_len)
838 {
839 struct cifsFileInfo *file_info = file->private_data;
840 struct super_block *sb = file_inode(file)->i_sb;
841 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
842 struct cifs_dirent de = { NULL, };
843 struct cifs_fattr fattr;
844 struct qstr name;
845 int rc = 0;
846 ino_t ino;
847
848 rc = cifs_fill_dirent(&de, find_entry, file_info->srch_inf.info_level,
849 file_info->srch_inf.unicode);
850 if (rc)
851 return rc;
852
853 if (de.namelen > max_len) {
854 cifs_dbg(VFS, "bad search response length %zd past smb end\n",
855 de.namelen);
856 return -EINVAL;
857 }
858
859 /* skip . and .. since we added them first */
860 if (cifs_entry_is_dot(&de, file_info->srch_inf.unicode))
861 return 0;
862
863 if (file_info->srch_inf.unicode) {
864 struct nls_table *nlt = cifs_sb->local_nls;
865 int map_type;
866
867 map_type = cifs_remap(cifs_sb);
868 name.name = scratch_buf;
869 name.len =
870 cifs_from_utf16((char *)name.name, (__le16 *)de.name,
871 UNICODE_NAME_MAX,
872 min_t(size_t, de.namelen,
873 (size_t)max_len), nlt, map_type);
874 name.len -= nls_nullsize(nlt);
875 } else {
876 name.name = de.name;
877 name.len = de.namelen;
878 }
879
880 switch (file_info->srch_inf.info_level) {
881 case SMB_FIND_FILE_POSIX_INFO:
882 cifs_posix_to_fattr(&fattr,
883 (struct smb2_posix_info *)find_entry,
884 cifs_sb);
885 break;
886 case SMB_FIND_FILE_UNIX:
887 cifs_unix_basic_to_fattr(&fattr,
888 &((FILE_UNIX_INFO *)find_entry)->basic,
889 cifs_sb);
890 break;
891 case SMB_FIND_FILE_INFO_STANDARD:
892 cifs_std_info_to_fattr(&fattr,
893 (FIND_FILE_STANDARD_INFO *)find_entry,
894 cifs_sb);
895 break;
896 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
897 cifs_fulldir_info_to_fattr(&fattr,
898 (SEARCH_ID_FULL_DIR_INFO *)find_entry,
899 cifs_sb);
900 break;
901 default:
902 cifs_dir_info_to_fattr(&fattr,
903 (FILE_DIRECTORY_INFO *)find_entry,
904 cifs_sb);
905 break;
906 }
907
908 if (de.ino && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
909 fattr.cf_uniqueid = de.ino;
910 } else {
911 fattr.cf_uniqueid = iunique(sb, ROOT_I);
912 cifs_autodisable_serverino(cifs_sb);
913 }
914
915 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) &&
916 couldbe_mf_symlink(&fattr))
917 /*
918 * trying to get the type and mode can be slow,
919 * so just call those regular files for now, and mark
920 * for reval
921 */
922 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
923
924 cifs_prime_dcache(file_dentry(file), &name, &fattr);
925
926 ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
927 return !dir_emit(ctx, name.name, name.len, ino, fattr.cf_dtype);
928 }
929
930
cifs_readdir(struct file * file,struct dir_context * ctx)931 int cifs_readdir(struct file *file, struct dir_context *ctx)
932 {
933 int rc = 0;
934 unsigned int xid;
935 int i;
936 struct cifs_tcon *tcon;
937 struct cifsFileInfo *cifsFile = NULL;
938 char *current_entry;
939 int num_to_fill = 0;
940 char *tmp_buf = NULL;
941 char *end_of_smb;
942 unsigned int max_len;
943 const char *full_path;
944 void *page = alloc_dentry_path();
945
946 xid = get_xid();
947
948 full_path = build_path_from_dentry(file_dentry(file), page);
949 if (IS_ERR(full_path)) {
950 rc = PTR_ERR(full_path);
951 goto rddir2_exit;
952 }
953
954 /*
955 * Ensure FindFirst doesn't fail before doing filldir() for '.' and
956 * '..'. Otherwise we won't be able to notify VFS in case of failure.
957 */
958 if (file->private_data == NULL) {
959 rc = initiate_cifs_search(xid, file, full_path);
960 cifs_dbg(FYI, "initiate cifs search rc %d\n", rc);
961 if (rc)
962 goto rddir2_exit;
963 }
964
965 if (!dir_emit_dots(file, ctx))
966 goto rddir2_exit;
967
968 /* 1) If search is active,
969 is in current search buffer?
970 if it before then restart search
971 if after then keep searching till find it */
972
973 cifsFile = file->private_data;
974 if (cifsFile->srch_inf.endOfSearch) {
975 if (cifsFile->srch_inf.emptyDir) {
976 cifs_dbg(FYI, "End of search, empty dir\n");
977 rc = 0;
978 goto rddir2_exit;
979 }
980 } /* else {
981 cifsFile->invalidHandle = true;
982 tcon->ses->server->close(xid, tcon, &cifsFile->fid);
983 } */
984
985 tcon = tlink_tcon(cifsFile->tlink);
986 rc = find_cifs_entry(xid, tcon, ctx->pos, file, full_path,
987 ¤t_entry, &num_to_fill);
988 if (rc) {
989 cifs_dbg(FYI, "fce error %d\n", rc);
990 goto rddir2_exit;
991 } else if (current_entry != NULL) {
992 cifs_dbg(FYI, "entry %lld found\n", ctx->pos);
993 } else {
994 cifs_dbg(FYI, "Could not find entry\n");
995 goto rddir2_exit;
996 }
997 cifs_dbg(FYI, "loop through %d times filling dir for net buf %p\n",
998 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start);
999 max_len = tcon->ses->server->ops->calc_smb_size(
1000 cifsFile->srch_inf.ntwrk_buf_start,
1001 tcon->ses->server);
1002 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1003
1004 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
1005 if (tmp_buf == NULL) {
1006 rc = -ENOMEM;
1007 goto rddir2_exit;
1008 }
1009
1010 for (i = 0; i < num_to_fill; i++) {
1011 if (current_entry == NULL) {
1012 /* evaluate whether this case is an error */
1013 cifs_dbg(VFS, "past SMB end, num to fill %d i %d\n",
1014 num_to_fill, i);
1015 break;
1016 }
1017 /*
1018 * if buggy server returns . and .. late do we want to
1019 * check for that here?
1020 */
1021 *tmp_buf = 0;
1022 rc = cifs_filldir(current_entry, file, ctx,
1023 tmp_buf, max_len);
1024 if (rc) {
1025 if (rc > 0)
1026 rc = 0;
1027 break;
1028 }
1029
1030 ctx->pos++;
1031 if (ctx->pos ==
1032 cifsFile->srch_inf.index_of_last_entry) {
1033 cifs_dbg(FYI, "last entry in buf at pos %lld %s\n",
1034 ctx->pos, tmp_buf);
1035 cifs_save_resume_key(current_entry, cifsFile);
1036 break;
1037 } else
1038 current_entry =
1039 nxt_dir_entry(current_entry, end_of_smb,
1040 cifsFile->srch_inf.info_level);
1041 }
1042 kfree(tmp_buf);
1043
1044 rddir2_exit:
1045 free_dentry_path(page);
1046 free_xid(xid);
1047 return rc;
1048 }
1049