Skip to content

Commit 3422dfa

Browse files
committed
erofs: simplify erofs_{read,fill}_inode()
- Switch to on-stack `copied` since it's just 64 bytes; - Get rid of `nblks` and derive `i_blocks` directly; - Use `inode_set_mtime()` instead of `inode_set_ctime()` to follow the ondisk naming; - Rearrange the code. Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com> Acked-by: Chao Yu <chao@kernel.org> Link: https://lore.kernel.org/r/20250310095459.2620647-3-hsiangkao@linux.alibaba.com
1 parent 8e49c33 commit 3422dfa

1 file changed

Lines changed: 35 additions & 57 deletions

File tree

fs/erofs/inode.c

Lines changed: 35 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,27 @@ static int erofs_fill_symlink(struct inode *inode, void *kaddr,
2727
static int erofs_read_inode(struct inode *inode)
2828
{
2929
struct super_block *sb = inode->i_sb;
30+
erofs_blk_t blkaddr = erofs_blknr(sb, erofs_iloc(inode));
31+
unsigned int ofs = erofs_blkoff(sb, erofs_iloc(inode));
32+
struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
3033
struct erofs_sb_info *sbi = EROFS_SB(sb);
3134
struct erofs_inode *vi = EROFS_I(inode);
32-
const erofs_off_t inode_loc = erofs_iloc(inode);
33-
erofs_blk_t blkaddr, nblks = 0;
34-
void *kaddr;
35+
struct erofs_inode_extended *die, copied;
3536
struct erofs_inode_compact *dic;
36-
struct erofs_inode_extended *die, *copied = NULL;
3737
union erofs_inode_i_u iu;
38-
struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
39-
unsigned int ifmt, ofs;
38+
unsigned int ifmt;
39+
void *ptr;
4040
int err = 0;
4141

42-
blkaddr = erofs_blknr(sb, inode_loc);
43-
ofs = erofs_blkoff(sb, inode_loc);
44-
45-
kaddr = erofs_read_metabuf(&buf, sb, erofs_pos(sb, blkaddr), true);
46-
if (IS_ERR(kaddr)) {
47-
erofs_err(sb, "failed to get inode (nid: %llu) page, err %ld",
48-
vi->nid, PTR_ERR(kaddr));
49-
return PTR_ERR(kaddr);
42+
ptr = erofs_read_metabuf(&buf, sb, erofs_pos(sb, blkaddr), true);
43+
if (IS_ERR(ptr)) {
44+
err = PTR_ERR(ptr);
45+
erofs_err(sb, "failed to get inode (nid: %llu) page, err %d",
46+
vi->nid, err);
47+
goto err_out;
5048
}
5149

52-
dic = kaddr + ofs;
50+
dic = ptr + ofs;
5351
ifmt = le16_to_cpu(dic->i_format);
5452
if (ifmt & ~EROFS_I_ALL) {
5553
erofs_err(sb, "unsupported i_format %u of nid %llu",
@@ -76,23 +74,18 @@ static int erofs_read_inode(struct inode *inode)
7674
} else {
7775
const unsigned int gotten = sb->s_blocksize - ofs;
7876

79-
copied = kmalloc(vi->inode_isize, GFP_KERNEL);
80-
if (!copied) {
81-
err = -ENOMEM;
82-
goto err_out;
83-
}
84-
memcpy(copied, dic, gotten);
85-
kaddr = erofs_read_metabuf(&buf, sb,
77+
memcpy(&copied, dic, gotten);
78+
ptr = erofs_read_metabuf(&buf, sb,
8679
erofs_pos(sb, blkaddr + 1), true);
87-
if (IS_ERR(kaddr)) {
88-
erofs_err(sb, "failed to get inode payload block (nid: %llu), err %ld",
89-
vi->nid, PTR_ERR(kaddr));
90-
kfree(copied);
91-
return PTR_ERR(kaddr);
80+
if (IS_ERR(ptr)) {
81+
err = PTR_ERR(ptr);
82+
erofs_err(sb, "failed to get inode payload block (nid: %llu), err %d",
83+
vi->nid, err);
84+
goto err_out;
9285
}
9386
ofs = vi->inode_isize - gotten;
94-
memcpy((u8 *)copied + gotten, kaddr, ofs);
95-
die = copied;
87+
memcpy((u8 *)&copied + gotten, ptr, ofs);
88+
die = &copied;
9689
}
9790
vi->xattr_isize = erofs_xattr_ibody_size(die->i_xattr_icount);
9891

@@ -101,12 +94,10 @@ static int erofs_read_inode(struct inode *inode)
10194
i_uid_write(inode, le32_to_cpu(die->i_uid));
10295
i_gid_write(inode, le32_to_cpu(die->i_gid));
10396
set_nlink(inode, le32_to_cpu(die->i_nlink));
104-
/* each extended inode has its own timestamp */
105-
inode_set_ctime(inode, le64_to_cpu(die->i_mtime),
97+
inode_set_mtime(inode, le64_to_cpu(die->i_mtime),
10698
le32_to_cpu(die->i_mtime_nsec));
10799

108100
inode->i_size = le64_to_cpu(die->i_size);
109-
kfree(copied);
110101
break;
111102
case EROFS_INODE_LAYOUT_COMPACT:
112103
vi->inode_isize = sizeof(struct erofs_inode_compact);
@@ -118,8 +109,7 @@ static int erofs_read_inode(struct inode *inode)
118109
i_uid_write(inode, le16_to_cpu(dic->i_uid));
119110
i_gid_write(inode, le16_to_cpu(dic->i_gid));
120111
set_nlink(inode, le16_to_cpu(dic->i_nlink));
121-
/* use build time for compact inodes */
122-
inode_set_ctime(inode, sbi->build_time, sbi->build_time_nsec);
112+
inode_set_mtime(inode, sbi->build_time, sbi->build_time_nsec);
123113

124114
inode->i_size = le32_to_cpu(dic->i_size);
125115
break;
@@ -141,7 +131,7 @@ static int erofs_read_inode(struct inode *inode)
141131
case S_IFLNK:
142132
vi->raw_blkaddr = le32_to_cpu(iu.raw_blkaddr);
143133
if(S_ISLNK(inode->i_mode)) {
144-
err = erofs_fill_symlink(inode, kaddr, ofs);
134+
err = erofs_fill_symlink(inode, ptr, ofs);
145135
if (err)
146136
goto err_out;
147137
}
@@ -161,10 +151,13 @@ static int erofs_read_inode(struct inode *inode)
161151
goto err_out;
162152
}
163153

164-
/* total blocks for compressed files */
165-
if (erofs_inode_is_data_compressed(vi->datalayout)) {
166-
nblks = le32_to_cpu(iu.compressed_blocks);
167-
} else if (vi->datalayout == EROFS_INODE_CHUNK_BASED) {
154+
if (erofs_inode_is_data_compressed(vi->datalayout))
155+
inode->i_blocks = le32_to_cpu(iu.compressed_blocks) <<
156+
(sb->s_blocksize_bits - 9);
157+
else
158+
inode->i_blocks = round_up(inode->i_size, sb->s_blocksize) >> 9;
159+
160+
if (vi->datalayout == EROFS_INODE_CHUNK_BASED) {
168161
/* fill chunked inode summary info */
169162
vi->chunkformat = le16_to_cpu(iu.c.format);
170163
if (vi->chunkformat & ~EROFS_CHUNK_FORMAT_ALL) {
@@ -176,22 +169,15 @@ static int erofs_read_inode(struct inode *inode)
176169
vi->chunkbits = sb->s_blocksize_bits +
177170
(vi->chunkformat & EROFS_CHUNK_FORMAT_BLKBITS_MASK);
178171
}
179-
inode_set_mtime_to_ts(inode,
180-
inode_set_atime_to_ts(inode, inode_get_ctime(inode)));
172+
inode_set_atime_to_ts(inode,
173+
inode_set_ctime_to_ts(inode, inode_get_mtime(inode)));
181174

182175
inode->i_flags &= ~S_DAX;
183176
if (test_opt(&sbi->opt, DAX_ALWAYS) && S_ISREG(inode->i_mode) &&
184177
(vi->datalayout == EROFS_INODE_FLAT_PLAIN ||
185178
vi->datalayout == EROFS_INODE_CHUNK_BASED))
186179
inode->i_flags |= S_DAX;
187-
188-
if (!nblks)
189-
/* measure inode.i_blocks as generic filesystems */
190-
inode->i_blocks = round_up(inode->i_size, sb->s_blocksize) >> 9;
191-
else
192-
inode->i_blocks = nblks << (sb->s_blocksize_bits - 9);
193180
err_out:
194-
DBG_BUGON(err);
195181
erofs_put_metabuf(&buf);
196182
return err;
197183
}
@@ -202,13 +188,10 @@ static int erofs_fill_inode(struct inode *inode)
202188
int err;
203189

204190
trace_erofs_fill_inode(inode);
205-
206-
/* read inode base data from disk */
207191
err = erofs_read_inode(inode);
208192
if (err)
209193
return err;
210194

211-
/* setup the new inode */
212195
switch (inode->i_mode & S_IFMT) {
213196
case S_IFREG:
214197
inode->i_op = &erofs_generic_iops;
@@ -229,15 +212,10 @@ static int erofs_fill_inode(struct inode *inode)
229212
inode->i_op = &erofs_symlink_iops;
230213
inode_nohighmem(inode);
231214
break;
232-
case S_IFCHR:
233-
case S_IFBLK:
234-
case S_IFIFO:
235-
case S_IFSOCK:
215+
default:
236216
inode->i_op = &erofs_generic_iops;
237217
init_special_inode(inode, inode->i_mode, inode->i_rdev);
238218
return 0;
239-
default:
240-
return -EFSCORRUPTED;
241219
}
242220

243221
mapping_set_large_folios(inode->i_mapping);

0 commit comments

Comments
 (0)