1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Created by Gao Xiang <gaoxiang25@huawei.com>
9 #include <trace/events/erofs.h>
12 * if inode is successfully read, return its inode page (or sometimes
13 * the inode payload page if it's an extended inode) in order to fill
14 * inline data if possible.
16 static struct page
*erofs_read_inode(struct inode
*inode
,
19 struct super_block
*sb
= inode
->i_sb
;
20 struct erofs_sb_info
*sbi
= EROFS_SB(sb
);
21 struct erofs_inode
*vi
= EROFS_I(inode
);
22 const erofs_off_t inode_loc
= iloc(sbi
, vi
->nid
);
24 erofs_blk_t blkaddr
, nblks
= 0;
26 struct erofs_inode_compact
*dic
;
27 struct erofs_inode_extended
*die
, *copied
= NULL
;
31 blkaddr
= erofs_blknr(inode_loc
);
32 *ofs
= erofs_blkoff(inode_loc
);
34 erofs_dbg("%s, reading inode nid %llu at %u of blkaddr %u",
35 __func__
, vi
->nid
, *ofs
, blkaddr
);
37 page
= erofs_get_meta_page(sb
, blkaddr
);
39 erofs_err(sb
, "failed to get inode (nid: %llu) page, err %ld",
40 vi
->nid
, PTR_ERR(page
));
44 dic
= page_address(page
) + *ofs
;
45 ifmt
= le16_to_cpu(dic
->i_format
);
47 vi
->datalayout
= erofs_inode_datalayout(ifmt
);
48 if (vi
->datalayout
>= EROFS_INODE_DATALAYOUT_MAX
) {
49 erofs_err(inode
->i_sb
, "unsupported datalayout %u of nid %llu",
50 vi
->datalayout
, vi
->nid
);
55 switch (erofs_inode_version(ifmt
)) {
56 case EROFS_INODE_LAYOUT_EXTENDED
:
57 vi
->inode_isize
= sizeof(struct erofs_inode_extended
);
58 /* check if the inode acrosses page boundary */
59 if (*ofs
+ vi
->inode_isize
<= PAGE_SIZE
) {
60 *ofs
+= vi
->inode_isize
;
61 die
= (struct erofs_inode_extended
*)dic
;
63 const unsigned int gotten
= PAGE_SIZE
- *ofs
;
65 copied
= kmalloc(vi
->inode_isize
, GFP_NOFS
);
70 memcpy(copied
, dic
, gotten
);
74 page
= erofs_get_meta_page(sb
, blkaddr
+ 1);
76 erofs_err(sb
, "failed to get inode payload page (nid: %llu), err %ld",
77 vi
->nid
, PTR_ERR(page
));
81 *ofs
= vi
->inode_isize
- gotten
;
82 memcpy((u8
*)copied
+ gotten
, page_address(page
), *ofs
);
85 vi
->xattr_isize
= erofs_xattr_ibody_size(die
->i_xattr_icount
);
87 inode
->i_mode
= le16_to_cpu(die
->i_mode
);
88 switch (inode
->i_mode
& S_IFMT
) {
92 vi
->raw_blkaddr
= le32_to_cpu(die
->i_u
.raw_blkaddr
);
97 new_decode_dev(le32_to_cpu(die
->i_u
.rdev
));
106 i_uid_write(inode
, le32_to_cpu(die
->i_uid
));
107 i_gid_write(inode
, le32_to_cpu(die
->i_gid
));
108 set_nlink(inode
, le32_to_cpu(die
->i_nlink
));
110 /* extended inode has its own timestamp */
111 inode
->i_ctime
.tv_sec
= le64_to_cpu(die
->i_ctime
);
112 inode
->i_ctime
.tv_nsec
= le32_to_cpu(die
->i_ctime_nsec
);
114 inode
->i_size
= le64_to_cpu(die
->i_size
);
116 /* total blocks for compressed files */
117 if (erofs_inode_is_data_compressed(vi
->datalayout
))
118 nblks
= le32_to_cpu(die
->i_u
.compressed_blocks
);
122 case EROFS_INODE_LAYOUT_COMPACT
:
123 vi
->inode_isize
= sizeof(struct erofs_inode_compact
);
124 *ofs
+= vi
->inode_isize
;
125 vi
->xattr_isize
= erofs_xattr_ibody_size(dic
->i_xattr_icount
);
127 inode
->i_mode
= le16_to_cpu(dic
->i_mode
);
128 switch (inode
->i_mode
& S_IFMT
) {
132 vi
->raw_blkaddr
= le32_to_cpu(dic
->i_u
.raw_blkaddr
);
137 new_decode_dev(le32_to_cpu(dic
->i_u
.rdev
));
146 i_uid_write(inode
, le16_to_cpu(dic
->i_uid
));
147 i_gid_write(inode
, le16_to_cpu(dic
->i_gid
));
148 set_nlink(inode
, le16_to_cpu(dic
->i_nlink
));
150 /* use build time for compact inodes */
151 inode
->i_ctime
.tv_sec
= sbi
->build_time
;
152 inode
->i_ctime
.tv_nsec
= sbi
->build_time_nsec
;
154 inode
->i_size
= le32_to_cpu(dic
->i_size
);
155 if (erofs_inode_is_data_compressed(vi
->datalayout
))
156 nblks
= le32_to_cpu(dic
->i_u
.compressed_blocks
);
159 erofs_err(inode
->i_sb
,
160 "unsupported on-disk inode version %u of nid %llu",
161 erofs_inode_version(ifmt
), vi
->nid
);
166 inode
->i_mtime
.tv_sec
= inode
->i_ctime
.tv_sec
;
167 inode
->i_atime
.tv_sec
= inode
->i_ctime
.tv_sec
;
168 inode
->i_mtime
.tv_nsec
= inode
->i_ctime
.tv_nsec
;
169 inode
->i_atime
.tv_nsec
= inode
->i_ctime
.tv_nsec
;
172 /* measure inode.i_blocks as generic filesystems */
173 inode
->i_blocks
= roundup(inode
->i_size
, EROFS_BLKSIZ
) >> 9;
175 inode
->i_blocks
= nblks
<< LOG_SECTORS_PER_BLOCK
;
179 erofs_err(inode
->i_sb
, "bogus i_mode (%o) @ nid %llu",
180 inode
->i_mode
, vi
->nid
);
190 static int erofs_fill_symlink(struct inode
*inode
, void *data
,
193 struct erofs_inode
*vi
= EROFS_I(inode
);
196 /* if it cannot be handled with fast symlink scheme */
197 if (vi
->datalayout
!= EROFS_INODE_FLAT_INLINE
||
198 inode
->i_size
>= PAGE_SIZE
) {
199 inode
->i_op
= &erofs_symlink_iops
;
203 lnk
= kmalloc(inode
->i_size
+ 1, GFP_KERNEL
);
207 m_pofs
+= vi
->xattr_isize
;
208 /* inline symlink data shouldn't cross page boundary as well */
209 if (m_pofs
+ inode
->i_size
> PAGE_SIZE
) {
211 erofs_err(inode
->i_sb
,
212 "inline data cross block boundary @ nid %llu",
215 return -EFSCORRUPTED
;
218 memcpy(lnk
, data
+ m_pofs
, inode
->i_size
);
219 lnk
[inode
->i_size
] = '\0';
222 inode
->i_op
= &erofs_fast_symlink_iops
;
226 static int erofs_fill_inode(struct inode
*inode
, int isdir
)
228 struct erofs_inode
*vi
= EROFS_I(inode
);
233 trace_erofs_fill_inode(inode
, isdir
);
235 /* read inode base data from disk */
236 page
= erofs_read_inode(inode
, &ofs
);
238 return PTR_ERR(page
);
240 /* setup the new inode */
241 switch (inode
->i_mode
& S_IFMT
) {
243 inode
->i_op
= &erofs_generic_iops
;
244 inode
->i_fop
= &generic_ro_fops
;
247 inode
->i_op
= &erofs_dir_iops
;
248 inode
->i_fop
= &erofs_dir_fops
;
251 err
= erofs_fill_symlink(inode
, page_address(page
), ofs
);
254 inode_nohighmem(inode
);
260 inode
->i_op
= &erofs_generic_iops
;
261 init_special_inode(inode
, inode
->i_mode
, inode
->i_rdev
);
268 if (erofs_inode_is_data_compressed(vi
->datalayout
)) {
269 err
= z_erofs_fill_inode(inode
);
272 inode
->i_mapping
->a_ops
= &erofs_raw_access_aops
;
281 * erofs nid is 64bits, but i_ino is 'unsigned long', therefore
282 * we should do more for 32-bit platform to find the right inode.
284 static int erofs_ilookup_test_actor(struct inode
*inode
, void *opaque
)
286 const erofs_nid_t nid
= *(erofs_nid_t
*)opaque
;
288 return EROFS_I(inode
)->nid
== nid
;
291 static int erofs_iget_set_actor(struct inode
*inode
, void *opaque
)
293 const erofs_nid_t nid
= *(erofs_nid_t
*)opaque
;
295 inode
->i_ino
= erofs_inode_hash(nid
);
299 static inline struct inode
*erofs_iget_locked(struct super_block
*sb
,
302 const unsigned long hashval
= erofs_inode_hash(nid
);
304 return iget5_locked(sb
, hashval
, erofs_ilookup_test_actor
,
305 erofs_iget_set_actor
, &nid
);
308 struct inode
*erofs_iget(struct super_block
*sb
,
312 struct inode
*inode
= erofs_iget_locked(sb
, nid
);
315 return ERR_PTR(-ENOMEM
);
317 if (inode
->i_state
& I_NEW
) {
319 struct erofs_inode
*vi
= EROFS_I(inode
);
323 err
= erofs_fill_inode(inode
, isdir
);
325 unlock_new_inode(inode
);
328 inode
= ERR_PTR(err
);
334 int erofs_getattr(const struct path
*path
, struct kstat
*stat
,
335 u32 request_mask
, unsigned int query_flags
)
337 struct inode
*const inode
= d_inode(path
->dentry
);
339 if (erofs_inode_is_data_compressed(EROFS_I(inode
)->datalayout
))
340 stat
->attributes
|= STATX_ATTR_COMPRESSED
;
342 stat
->attributes
|= STATX_ATTR_IMMUTABLE
;
343 stat
->attributes_mask
|= (STATX_ATTR_COMPRESSED
|
344 STATX_ATTR_IMMUTABLE
);
346 generic_fillattr(inode
, stat
);
350 const struct inode_operations erofs_generic_iops
= {
351 .getattr
= erofs_getattr
,
352 .listxattr
= erofs_listxattr
,
353 .get_acl
= erofs_get_acl
,
356 const struct inode_operations erofs_symlink_iops
= {
357 .get_link
= page_get_link
,
358 .getattr
= erofs_getattr
,
359 .listxattr
= erofs_listxattr
,
360 .get_acl
= erofs_get_acl
,
363 const struct inode_operations erofs_fast_symlink_iops
= {
364 .get_link
= simple_get_link
,
365 .getattr
= erofs_getattr
,
366 .listxattr
= erofs_listxattr
,
367 .get_acl
= erofs_get_acl
,