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