1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Copyright (C) 2022, Alibaba Cloud
6 */
7 #include "internal.h"
8
erofs_fill_dentries(struct inode * dir,struct dir_context * ctx,void * dentry_blk,struct erofs_dirent * de,unsigned int nameoff0,unsigned int maxsize)9 static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
10 void *dentry_blk, struct erofs_dirent *de,
11 unsigned int nameoff0, unsigned int maxsize)
12 {
13 const struct erofs_dirent *end = dentry_blk + nameoff0;
14
15 while (de < end) {
16 unsigned char d_type = fs_ftype_to_dtype(de->file_type);
17 unsigned int nameoff = le16_to_cpu(de->nameoff);
18 const char *de_name = (char *)dentry_blk + nameoff;
19 unsigned int de_namelen;
20
21 /* the last dirent in the block? */
22 if (de + 1 >= end)
23 de_namelen = strnlen(de_name, maxsize - nameoff);
24 else
25 de_namelen = le16_to_cpu(de[1].nameoff) - nameoff;
26
27 /* a corrupted entry is found */
28 if (nameoff + de_namelen > maxsize ||
29 de_namelen > EROFS_NAME_LEN) {
30 erofs_err(dir->i_sb, "bogus dirent @ nid %llu",
31 EROFS_I(dir)->nid);
32 DBG_BUGON(1);
33 return -EFSCORRUPTED;
34 }
35
36 if (!dir_emit(ctx, de_name, de_namelen,
37 erofs_nid_to_ino64(EROFS_SB(dir->i_sb),
38 le64_to_cpu(de->nid)), d_type))
39 return 1;
40 ++de;
41 ctx->pos += sizeof(struct erofs_dirent);
42 }
43 return 0;
44 }
45
erofs_readdir(struct file * f,struct dir_context * ctx)46 static int erofs_readdir(struct file *f, struct dir_context *ctx)
47 {
48 struct inode *dir = file_inode(f);
49 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
50 struct super_block *sb = dir->i_sb;
51 struct file_ra_state *ra = &f->f_ra;
52 unsigned long bsz = sb->s_blocksize;
53 unsigned int ofs = erofs_blkoff(sb, ctx->pos);
54 pgoff_t ra_pages = DIV_ROUND_UP_POW2(
55 EROFS_I_SB(dir)->dir_ra_bytes, PAGE_SIZE);
56 pgoff_t nr_pages = DIV_ROUND_UP_POW2(dir->i_size, PAGE_SIZE);
57 int err = 0;
58 bool initial = true;
59
60 buf.mapping = dir->i_mapping;
61 while (ctx->pos < dir->i_size) {
62 erofs_off_t dbstart = ctx->pos - ofs;
63 struct erofs_dirent *de;
64 unsigned int nameoff, maxsize;
65
66 if (fatal_signal_pending(current)) {
67 err = -ERESTARTSYS;
68 break;
69 }
70
71 /* readahead blocks to enhance performance for large directories */
72 if (ra_pages) {
73 pgoff_t idx = DIV_ROUND_UP_POW2(ctx->pos, PAGE_SIZE);
74 pgoff_t pages = min(nr_pages - idx, ra_pages);
75
76 if (pages > 1 && !ra_has_index(ra, idx))
77 page_cache_sync_readahead(dir->i_mapping, ra,
78 f, idx, pages);
79 }
80
81 de = erofs_bread(&buf, dbstart, true);
82 if (IS_ERR(de)) {
83 erofs_err(sb, "failed to readdir of logical block %llu of nid %llu",
84 erofs_blknr(sb, dbstart), EROFS_I(dir)->nid);
85 err = PTR_ERR(de);
86 break;
87 }
88
89 nameoff = le16_to_cpu(de->nameoff);
90 if (nameoff < sizeof(struct erofs_dirent) || nameoff >= bsz) {
91 erofs_err(sb, "invalid de[0].nameoff %u @ nid %llu",
92 nameoff, EROFS_I(dir)->nid);
93 err = -EFSCORRUPTED;
94 break;
95 }
96
97 maxsize = min_t(unsigned int, dir->i_size - dbstart, bsz);
98 /* search dirents at the arbitrary position */
99 if (initial) {
100 initial = false;
101 ofs = roundup(ofs, sizeof(struct erofs_dirent));
102 ctx->pos = dbstart + ofs;
103 }
104
105 err = erofs_fill_dentries(dir, ctx, de, (void *)de + ofs,
106 nameoff, maxsize);
107 if (err)
108 break;
109 ctx->pos = dbstart + maxsize;
110 ofs = 0;
111 cond_resched();
112 }
113 erofs_put_metabuf(&buf);
114 if (EROFS_I(dir)->dot_omitted && ctx->pos == dir->i_size) {
115 if (!dir_emit_dot(f, ctx))
116 return 0;
117 ++ctx->pos;
118 }
119 return err < 0 ? err : 0;
120 }
121
122 const struct file_operations erofs_dir_fops = {
123 .llseek = generic_file_llseek,
124 .read = generic_read_dir,
125 .iterate_shared = erofs_readdir,
126 };
127