Skip to content

Commit 5b5c96c

Browse files
Hongzhen Luohsiangkao
authored andcommitted
erofs: simplify readdir operation
- Use i_size instead of i_size_read() due to immutable fses; - Get rid of an unneeded goto since erofs_fill_dentries() also works; - Remove unnecessary lines. Signed-off-by: Hongzhen Luo <[email protected]> Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Gao Xiang <[email protected]> Signed-off-by: Gao Xiang <[email protected]>
1 parent 47ac09b commit 5b5c96c

File tree

2 files changed

+13
-24
lines changed

2 files changed

+13
-24
lines changed

fs/erofs/dir.c

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,15 @@
88

99
static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
1010
void *dentry_blk, struct erofs_dirent *de,
11-
unsigned int nameoff, unsigned int maxsize)
11+
unsigned int nameoff0, unsigned int maxsize)
1212
{
13-
const struct erofs_dirent *end = dentry_blk + nameoff;
13+
const struct erofs_dirent *end = dentry_blk + nameoff0;
1414

1515
while (de < end) {
16-
const char *de_name;
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;
1719
unsigned int de_namelen;
18-
unsigned char d_type;
19-
20-
d_type = fs_ftype_to_dtype(de->file_type);
21-
22-
nameoff = le16_to_cpu(de->nameoff);
23-
de_name = (char *)dentry_blk + nameoff;
2420

2521
/* the last dirent in the block? */
2622
if (de + 1 >= end)
@@ -52,21 +48,20 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx)
5248
struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
5349
struct super_block *sb = dir->i_sb;
5450
unsigned long bsz = sb->s_blocksize;
55-
const size_t dirsize = i_size_read(dir);
56-
unsigned int i = erofs_blknr(sb, ctx->pos);
5751
unsigned int ofs = erofs_blkoff(sb, ctx->pos);
5852
int err = 0;
5953
bool initial = true;
6054

6155
buf.mapping = dir->i_mapping;
62-
while (ctx->pos < dirsize) {
56+
while (ctx->pos < dir->i_size) {
57+
erofs_off_t dbstart = ctx->pos - ofs;
6358
struct erofs_dirent *de;
6459
unsigned int nameoff, maxsize;
6560

66-
de = erofs_bread(&buf, erofs_pos(sb, i), EROFS_KMAP);
61+
de = erofs_bread(&buf, dbstart, EROFS_KMAP);
6762
if (IS_ERR(de)) {
6863
erofs_err(sb, "fail to readdir of logical block %u of nid %llu",
69-
i, EROFS_I(dir)->nid);
64+
erofs_blknr(sb, dbstart), EROFS_I(dir)->nid);
7065
err = PTR_ERR(de);
7166
break;
7267
}
@@ -79,25 +74,19 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx)
7974
break;
8075
}
8176

82-
maxsize = min_t(unsigned int, dirsize - ctx->pos + ofs, bsz);
83-
77+
maxsize = min_t(unsigned int, dir->i_size - dbstart, bsz);
8478
/* search dirents at the arbitrary position */
8579
if (initial) {
8680
initial = false;
87-
8881
ofs = roundup(ofs, sizeof(struct erofs_dirent));
89-
ctx->pos = erofs_pos(sb, i) + ofs;
90-
if (ofs >= nameoff)
91-
goto skip_this;
82+
ctx->pos = dbstart + ofs;
9283
}
9384

9485
err = erofs_fill_dentries(dir, ctx, de, (void *)de + ofs,
9586
nameoff, maxsize);
9687
if (err)
9788
break;
98-
skip_this:
99-
ctx->pos = erofs_pos(sb, i) + maxsize;
100-
++i;
89+
ctx->pos = dbstart + maxsize;
10190
ofs = 0;
10291
}
10392
erofs_put_metabuf(&buf);

fs/erofs/internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ struct erofs_buf {
220220
};
221221
#define __EROFS_BUF_INITIALIZER ((struct erofs_buf){ .page = NULL })
222222

223-
#define erofs_blknr(sb, addr) ((addr) >> (sb)->s_blocksize_bits)
223+
#define erofs_blknr(sb, addr) ((erofs_blk_t)((addr) >> (sb)->s_blocksize_bits))
224224
#define erofs_blkoff(sb, addr) ((addr) & ((sb)->s_blocksize - 1))
225225
#define erofs_pos(sb, blk) ((erofs_off_t)(blk) << (sb)->s_blocksize_bits)
226226
#define erofs_iblks(i) (round_up((i)->i_size, i_blocksize(i)) >> (i)->i_blkbits)

0 commit comments

Comments
 (0)