1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Copyright (C) 2021, Alibaba Cloud
8 #include <trace/events/erofs.h>
10 static int erofs_fill_symlink(struct inode
*inode
, void *kaddr
,
13 struct erofs_inode
*vi
= EROFS_I(inode
);
16 m_pofs
+= vi
->xattr_isize
;
17 /* check if it cannot be handled with fast symlink scheme */
18 if (vi
->datalayout
!= EROFS_INODE_FLAT_INLINE
||
19 check_add_overflow(m_pofs
, inode
->i_size
, &off
) ||
20 off
> i_blocksize(inode
))
23 inode
->i_link
= kmemdup_nul(kaddr
+ m_pofs
, inode
->i_size
, GFP_KERNEL
);
24 return inode
->i_link
? 0 : -ENOMEM
;
27 static int erofs_read_inode(struct inode
*inode
)
29 struct super_block
*sb
= inode
->i_sb
;
30 struct erofs_sb_info
*sbi
= EROFS_SB(sb
);
31 struct erofs_inode
*vi
= EROFS_I(inode
);
32 const erofs_off_t inode_loc
= erofs_iloc(inode
);
33 erofs_blk_t blkaddr
, nblks
= 0;
35 struct erofs_inode_compact
*dic
;
36 struct erofs_inode_extended
*die
, *copied
= NULL
;
37 union erofs_inode_i_u iu
;
38 struct erofs_buf buf
= __EROFS_BUF_INITIALIZER
;
39 unsigned int ifmt
, ofs
;
42 blkaddr
= erofs_blknr(sb
, inode_loc
);
43 ofs
= erofs_blkoff(sb
, inode_loc
);
45 kaddr
= erofs_read_metabuf(&buf
, sb
, erofs_pos(sb
, blkaddr
), EROFS_KMAP
);
47 erofs_err(sb
, "failed to get inode (nid: %llu) page, err %ld",
48 vi
->nid
, PTR_ERR(kaddr
));
49 return PTR_ERR(kaddr
);
53 ifmt
= le16_to_cpu(dic
->i_format
);
54 if (ifmt
& ~EROFS_I_ALL
) {
55 erofs_err(sb
, "unsupported i_format %u of nid %llu",
61 vi
->datalayout
= erofs_inode_datalayout(ifmt
);
62 if (vi
->datalayout
>= EROFS_INODE_DATALAYOUT_MAX
) {
63 erofs_err(sb
, "unsupported datalayout %u of nid %llu",
64 vi
->datalayout
, vi
->nid
);
69 switch (erofs_inode_version(ifmt
)) {
70 case EROFS_INODE_LAYOUT_EXTENDED
:
71 vi
->inode_isize
= sizeof(struct erofs_inode_extended
);
72 /* check if the extended inode acrosses block boundary */
73 if (ofs
+ vi
->inode_isize
<= sb
->s_blocksize
) {
74 ofs
+= vi
->inode_isize
;
75 die
= (struct erofs_inode_extended
*)dic
;
77 const unsigned int gotten
= sb
->s_blocksize
- ofs
;
79 copied
= kmalloc(vi
->inode_isize
, GFP_KERNEL
);
84 memcpy(copied
, dic
, gotten
);
85 kaddr
= erofs_read_metabuf(&buf
, sb
, erofs_pos(sb
, blkaddr
+ 1),
88 erofs_err(sb
, "failed to get inode payload block (nid: %llu), err %ld",
89 vi
->nid
, PTR_ERR(kaddr
));
91 return PTR_ERR(kaddr
);
93 ofs
= vi
->inode_isize
- gotten
;
94 memcpy((u8
*)copied
+ gotten
, kaddr
, ofs
);
97 vi
->xattr_isize
= erofs_xattr_ibody_size(die
->i_xattr_icount
);
99 inode
->i_mode
= le16_to_cpu(die
->i_mode
);
101 i_uid_write(inode
, le32_to_cpu(die
->i_uid
));
102 i_gid_write(inode
, le32_to_cpu(die
->i_gid
));
103 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
),
106 le32_to_cpu(die
->i_mtime_nsec
));
108 inode
->i_size
= le64_to_cpu(die
->i_size
);
111 case EROFS_INODE_LAYOUT_COMPACT
:
112 vi
->inode_isize
= sizeof(struct erofs_inode_compact
);
113 ofs
+= vi
->inode_isize
;
114 vi
->xattr_isize
= erofs_xattr_ibody_size(dic
->i_xattr_icount
);
116 inode
->i_mode
= le16_to_cpu(dic
->i_mode
);
118 i_uid_write(inode
, le16_to_cpu(dic
->i_uid
));
119 i_gid_write(inode
, le16_to_cpu(dic
->i_gid
));
120 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
);
124 inode
->i_size
= le32_to_cpu(dic
->i_size
);
127 erofs_err(sb
, "unsupported on-disk inode version %u of nid %llu",
128 erofs_inode_version(ifmt
), vi
->nid
);
133 if (unlikely(inode
->i_size
< 0)) {
134 erofs_err(sb
, "negative i_size @ nid %llu", vi
->nid
);
138 switch (inode
->i_mode
& S_IFMT
) {
142 vi
->raw_blkaddr
= le32_to_cpu(iu
.raw_blkaddr
);
143 if(S_ISLNK(inode
->i_mode
)) {
144 err
= erofs_fill_symlink(inode
, kaddr
, ofs
);
151 inode
->i_rdev
= new_decode_dev(le32_to_cpu(iu
.rdev
));
158 erofs_err(sb
, "bogus i_mode (%o) @ nid %llu", inode
->i_mode
,
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
) {
168 /* fill chunked inode summary info */
169 vi
->chunkformat
= le16_to_cpu(iu
.c
.format
);
170 if (vi
->chunkformat
& ~EROFS_CHUNK_FORMAT_ALL
) {
171 erofs_err(sb
, "unsupported chunk format %x of nid %llu",
172 vi
->chunkformat
, vi
->nid
);
176 vi
->chunkbits
= sb
->s_blocksize_bits
+
177 (vi
->chunkformat
& EROFS_CHUNK_FORMAT_BLKBITS_MASK
);
179 inode_set_mtime_to_ts(inode
,
180 inode_set_atime_to_ts(inode
, inode_get_ctime(inode
)));
182 inode
->i_flags
&= ~S_DAX
;
183 if (test_opt(&sbi
->opt
, DAX_ALWAYS
) && S_ISREG(inode
->i_mode
) &&
184 (vi
->datalayout
== EROFS_INODE_FLAT_PLAIN
||
185 vi
->datalayout
== EROFS_INODE_CHUNK_BASED
))
186 inode
->i_flags
|= S_DAX
;
189 /* measure inode.i_blocks as generic filesystems */
190 inode
->i_blocks
= round_up(inode
->i_size
, sb
->s_blocksize
) >> 9;
192 inode
->i_blocks
= nblks
<< (sb
->s_blocksize_bits
- 9);
195 erofs_put_metabuf(&buf
);
199 static int erofs_fill_inode(struct inode
*inode
)
201 struct erofs_inode
*vi
= EROFS_I(inode
);
204 trace_erofs_fill_inode(inode
);
206 /* read inode base data from disk */
207 err
= erofs_read_inode(inode
);
211 /* setup the new inode */
212 switch (inode
->i_mode
& S_IFMT
) {
214 inode
->i_op
= &erofs_generic_iops
;
215 if (erofs_inode_is_data_compressed(vi
->datalayout
))
216 inode
->i_fop
= &generic_ro_fops
;
218 inode
->i_fop
= &erofs_file_fops
;
221 inode
->i_op
= &erofs_dir_iops
;
222 inode
->i_fop
= &erofs_dir_fops
;
223 inode_nohighmem(inode
);
227 inode
->i_op
= &erofs_fast_symlink_iops
;
229 inode
->i_op
= &erofs_symlink_iops
;
230 inode_nohighmem(inode
);
236 inode
->i_op
= &erofs_generic_iops
;
237 init_special_inode(inode
, inode
->i_mode
, inode
->i_rdev
);
240 return -EFSCORRUPTED
;
243 mapping_set_large_folios(inode
->i_mapping
);
244 if (erofs_inode_is_data_compressed(vi
->datalayout
)) {
245 #ifdef CONFIG_EROFS_FS_ZIP
246 DO_ONCE_LITE_IF(inode
->i_blkbits
!= PAGE_SHIFT
,
247 erofs_info
, inode
->i_sb
,
248 "EXPERIMENTAL EROFS subpage compressed block support in use. Use at your own risk!");
249 inode
->i_mapping
->a_ops
= &z_erofs_aops
;
254 inode
->i_mapping
->a_ops
= &erofs_aops
;
255 #ifdef CONFIG_EROFS_FS_ONDEMAND
256 if (erofs_is_fscache_mode(inode
->i_sb
))
257 inode
->i_mapping
->a_ops
= &erofs_fscache_access_aops
;
259 #ifdef CONFIG_EROFS_FS_BACKED_BY_FILE
260 if (erofs_is_fileio_mode(EROFS_SB(inode
->i_sb
)))
261 inode
->i_mapping
->a_ops
= &erofs_fileio_aops
;
269 * ino_t is 32-bits on 32-bit arch. We have to squash the 64-bit value down
270 * so that it will fit.
272 static ino_t
erofs_squash_ino(erofs_nid_t nid
)
274 ino_t ino
= (ino_t
)nid
;
276 if (sizeof(ino_t
) < sizeof(erofs_nid_t
))
277 ino
^= nid
>> (sizeof(erofs_nid_t
) - sizeof(ino_t
)) * 8;
281 static int erofs_iget5_eq(struct inode
*inode
, void *opaque
)
283 return EROFS_I(inode
)->nid
== *(erofs_nid_t
*)opaque
;
286 static int erofs_iget5_set(struct inode
*inode
, void *opaque
)
288 const erofs_nid_t nid
= *(erofs_nid_t
*)opaque
;
290 inode
->i_ino
= erofs_squash_ino(nid
);
291 EROFS_I(inode
)->nid
= nid
;
295 struct inode
*erofs_iget(struct super_block
*sb
, erofs_nid_t nid
)
299 inode
= iget5_locked(sb
, erofs_squash_ino(nid
), erofs_iget5_eq
,
300 erofs_iget5_set
, &nid
);
302 return ERR_PTR(-ENOMEM
);
304 if (inode
->i_state
& I_NEW
) {
305 int err
= erofs_fill_inode(inode
);
311 unlock_new_inode(inode
);
316 int erofs_getattr(struct mnt_idmap
*idmap
, const struct path
*path
,
317 struct kstat
*stat
, u32 request_mask
,
318 unsigned int query_flags
)
320 struct inode
*const inode
= d_inode(path
->dentry
);
321 struct block_device
*bdev
= inode
->i_sb
->s_bdev
;
323 erofs_inode_is_data_compressed(EROFS_I(inode
)->datalayout
);
326 stat
->attributes
|= STATX_ATTR_COMPRESSED
;
327 stat
->attributes
|= STATX_ATTR_IMMUTABLE
;
328 stat
->attributes_mask
|= (STATX_ATTR_COMPRESSED
|
329 STATX_ATTR_IMMUTABLE
);
332 * Return the DIO alignment restrictions if requested.
334 * In EROFS, STATX_DIOALIGN is only supported in bdev-based mode
335 * and uncompressed inodes, otherwise we report no DIO support.
337 if ((request_mask
& STATX_DIOALIGN
) && S_ISREG(inode
->i_mode
)) {
338 stat
->result_mask
|= STATX_DIOALIGN
;
339 if (bdev
&& !compressed
) {
340 stat
->dio_mem_align
= bdev_dma_alignment(bdev
) + 1;
341 stat
->dio_offset_align
= bdev_logical_block_size(bdev
);
344 generic_fillattr(idmap
, request_mask
, inode
, stat
);
348 const struct inode_operations erofs_generic_iops
= {
349 .getattr
= erofs_getattr
,
350 .listxattr
= erofs_listxattr
,
351 .get_inode_acl
= erofs_get_acl
,
352 .fiemap
= erofs_fiemap
,
355 const struct inode_operations erofs_symlink_iops
= {
356 .get_link
= page_get_link
,
357 .getattr
= erofs_getattr
,
358 .listxattr
= erofs_listxattr
,
359 .get_inode_acl
= erofs_get_acl
,
362 const struct inode_operations erofs_fast_symlink_iops
= {
363 .get_link
= simple_get_link
,
364 .getattr
= erofs_getattr
,
365 .listxattr
= erofs_listxattr
,
366 .get_inode_acl
= erofs_get_acl
,