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
9 #include <trace/events/erofs.h>
11 static void *erofs_read_inode(struct erofs_buf *buf,
12 struct inode *inode, unsigned int *ofs)
14 struct super_block *sb = inode->i_sb;
15 struct erofs_sb_info *sbi = EROFS_SB(sb);
16 struct erofs_inode *vi = EROFS_I(inode);
17 const erofs_off_t inode_loc = erofs_iloc(inode);
19 erofs_blk_t blkaddr, nblks = 0;
21 struct erofs_inode_compact *dic;
22 struct erofs_inode_extended *die, *copied = NULL;
26 blkaddr = erofs_blknr(sb, inode_loc);
27 *ofs = erofs_blkoff(sb, inode_loc);
29 kaddr = erofs_read_metabuf(buf, sb, blkaddr, EROFS_KMAP);
31 erofs_err(sb, "failed to get inode (nid: %llu) page, err %ld",
32 vi->nid, PTR_ERR(kaddr));
37 ifmt = le16_to_cpu(dic->i_format);
39 if (ifmt & ~EROFS_I_ALL) {
40 erofs_err(inode->i_sb, "unsupported i_format %u of nid %llu",
46 vi->datalayout = erofs_inode_datalayout(ifmt);
47 if (vi->datalayout >= EROFS_INODE_DATALAYOUT_MAX) {
48 erofs_err(inode->i_sb, "unsupported datalayout %u of nid %llu",
49 vi->datalayout, vi->nid);
54 switch (erofs_inode_version(ifmt)) {
55 case EROFS_INODE_LAYOUT_EXTENDED:
56 vi->inode_isize = sizeof(struct erofs_inode_extended);
57 /* check if the extended inode acrosses block boundary */
58 if (*ofs + vi->inode_isize <= sb->s_blocksize) {
59 *ofs += vi->inode_isize;
60 die = (struct erofs_inode_extended *)dic;
62 const unsigned int gotten = sb->s_blocksize - *ofs;
64 copied = kmalloc(vi->inode_isize, GFP_NOFS);
69 memcpy(copied, dic, gotten);
70 kaddr = erofs_read_metabuf(buf, sb, blkaddr + 1,
73 erofs_err(sb, "failed to get inode payload block (nid: %llu), err %ld",
74 vi->nid, PTR_ERR(kaddr));
78 *ofs = vi->inode_isize - gotten;
79 memcpy((u8 *)copied + gotten, kaddr, *ofs);
82 vi->xattr_isize = erofs_xattr_ibody_size(die->i_xattr_icount);
84 inode->i_mode = le16_to_cpu(die->i_mode);
85 switch (inode->i_mode & S_IFMT) {
89 vi->raw_blkaddr = le32_to_cpu(die->i_u.raw_blkaddr);
94 new_decode_dev(le32_to_cpu(die->i_u.rdev));
103 i_uid_write(inode, le32_to_cpu(die->i_uid));
104 i_gid_write(inode, le32_to_cpu(die->i_gid));
105 set_nlink(inode, le32_to_cpu(die->i_nlink));
107 /* extended inode has its own timestamp */
108 inode_set_ctime(inode, le64_to_cpu(die->i_mtime),
109 le32_to_cpu(die->i_mtime_nsec));
111 inode->i_size = le64_to_cpu(die->i_size);
113 /* total blocks for compressed files */
114 if (erofs_inode_is_data_compressed(vi->datalayout))
115 nblks = le32_to_cpu(die->i_u.compressed_blocks);
116 else if (vi->datalayout == EROFS_INODE_CHUNK_BASED)
117 /* fill chunked inode summary info */
118 vi->chunkformat = le16_to_cpu(die->i_u.c.format);
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_set_ctime(inode, sbi->build_time, sbi->build_time_nsec);
153 inode->i_size = le32_to_cpu(dic->i_size);
154 if (erofs_inode_is_data_compressed(vi->datalayout))
155 nblks = le32_to_cpu(dic->i_u.compressed_blocks);
156 else if (vi->datalayout == EROFS_INODE_CHUNK_BASED)
157 vi->chunkformat = le16_to_cpu(dic->i_u.c.format);
160 erofs_err(inode->i_sb,
161 "unsupported on-disk inode version %u of nid %llu",
162 erofs_inode_version(ifmt), vi->nid);
167 if (vi->datalayout == EROFS_INODE_CHUNK_BASED) {
168 if (vi->chunkformat & ~EROFS_CHUNK_FORMAT_ALL) {
169 erofs_err(inode->i_sb,
170 "unsupported chunk format %x of nid %llu",
171 vi->chunkformat, vi->nid);
175 vi->chunkbits = sb->s_blocksize_bits +
176 (vi->chunkformat & EROFS_CHUNK_FORMAT_BLKBITS_MASK);
178 inode_set_mtime_to_ts(inode,
179 inode_set_atime_to_ts(inode, inode_get_ctime(inode)));
181 inode->i_flags &= ~S_DAX;
182 if (test_opt(&sbi->opt, DAX_ALWAYS) && S_ISREG(inode->i_mode) &&
183 (vi->datalayout == EROFS_INODE_FLAT_PLAIN ||
184 vi->datalayout == EROFS_INODE_CHUNK_BASED))
185 inode->i_flags |= S_DAX;
188 /* measure inode.i_blocks as generic filesystems */
189 inode->i_blocks = round_up(inode->i_size, sb->s_blocksize) >> 9;
191 inode->i_blocks = nblks << (sb->s_blocksize_bits - 9);
195 erofs_err(inode->i_sb, "bogus i_mode (%o) @ nid %llu",
196 inode->i_mode, vi->nid);
201 erofs_put_metabuf(buf);
205 static int erofs_fill_symlink(struct inode *inode, void *kaddr,
208 struct erofs_inode *vi = EROFS_I(inode);
209 unsigned int bsz = i_blocksize(inode);
212 /* if it cannot be handled with fast symlink scheme */
213 if (vi->datalayout != EROFS_INODE_FLAT_INLINE ||
214 inode->i_size >= bsz || inode->i_size < 0) {
215 inode->i_op = &erofs_symlink_iops;
219 lnk = kmalloc(inode->i_size + 1, GFP_KERNEL);
223 m_pofs += vi->xattr_isize;
224 /* inline symlink data shouldn't cross block boundary */
225 if (m_pofs + inode->i_size > bsz) {
227 erofs_err(inode->i_sb,
228 "inline data cross block boundary @ nid %llu",
231 return -EFSCORRUPTED;
233 memcpy(lnk, kaddr + m_pofs, inode->i_size);
234 lnk[inode->i_size] = '\0';
237 inode->i_op = &erofs_fast_symlink_iops;
241 static int erofs_fill_inode(struct inode *inode)
243 struct erofs_inode *vi = EROFS_I(inode);
244 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
249 trace_erofs_fill_inode(inode);
251 /* read inode base data from disk */
252 kaddr = erofs_read_inode(&buf, inode, &ofs);
254 return PTR_ERR(kaddr);
256 /* setup the new inode */
257 switch (inode->i_mode & S_IFMT) {
259 inode->i_op = &erofs_generic_iops;
260 if (erofs_inode_is_data_compressed(vi->datalayout))
261 inode->i_fop = &generic_ro_fops;
263 inode->i_fop = &erofs_file_fops;
266 inode->i_op = &erofs_dir_iops;
267 inode->i_fop = &erofs_dir_fops;
268 inode_nohighmem(inode);
271 err = erofs_fill_symlink(inode, kaddr, ofs);
274 inode_nohighmem(inode);
280 inode->i_op = &erofs_generic_iops;
281 init_special_inode(inode, inode->i_mode, inode->i_rdev);
288 if (erofs_inode_is_data_compressed(vi->datalayout)) {
289 #ifdef CONFIG_EROFS_FS_ZIP
290 if (!erofs_is_fscache_mode(inode->i_sb) &&
291 inode->i_sb->s_blocksize_bits == PAGE_SHIFT) {
292 inode->i_mapping->a_ops = &z_erofs_aops;
300 inode->i_mapping->a_ops = &erofs_raw_access_aops;
301 mapping_set_large_folios(inode->i_mapping);
302 #ifdef CONFIG_EROFS_FS_ONDEMAND
303 if (erofs_is_fscache_mode(inode->i_sb))
304 inode->i_mapping->a_ops = &erofs_fscache_access_aops;
308 erofs_put_metabuf(&buf);
313 * ino_t is 32-bits on 32-bit arch. We have to squash the 64-bit value down
314 * so that it will fit.
316 static ino_t erofs_squash_ino(erofs_nid_t nid)
318 ino_t ino = (ino_t)nid;
320 if (sizeof(ino_t) < sizeof(erofs_nid_t))
321 ino ^= nid >> (sizeof(erofs_nid_t) - sizeof(ino_t)) * 8;
325 static int erofs_iget5_eq(struct inode *inode, void *opaque)
327 return EROFS_I(inode)->nid == *(erofs_nid_t *)opaque;
330 static int erofs_iget5_set(struct inode *inode, void *opaque)
332 const erofs_nid_t nid = *(erofs_nid_t *)opaque;
334 inode->i_ino = erofs_squash_ino(nid);
335 EROFS_I(inode)->nid = nid;
339 struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid)
343 inode = iget5_locked(sb, erofs_squash_ino(nid), erofs_iget5_eq,
344 erofs_iget5_set, &nid);
346 return ERR_PTR(-ENOMEM);
348 if (inode->i_state & I_NEW) {
349 int err = erofs_fill_inode(inode);
355 unlock_new_inode(inode);
360 int erofs_getattr(struct mnt_idmap *idmap, const struct path *path,
361 struct kstat *stat, u32 request_mask,
362 unsigned int query_flags)
364 struct inode *const inode = d_inode(path->dentry);
366 if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout))
367 stat->attributes |= STATX_ATTR_COMPRESSED;
369 stat->attributes |= STATX_ATTR_IMMUTABLE;
370 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
371 STATX_ATTR_IMMUTABLE);
373 generic_fillattr(idmap, request_mask, inode, stat);
377 const struct inode_operations erofs_generic_iops = {
378 .getattr = erofs_getattr,
379 .listxattr = erofs_listxattr,
380 .get_inode_acl = erofs_get_acl,
381 .fiemap = erofs_fiemap,
384 const struct inode_operations erofs_symlink_iops = {
385 .get_link = page_get_link,
386 .getattr = erofs_getattr,
387 .listxattr = erofs_listxattr,
388 .get_inode_acl = erofs_get_acl,
391 const struct inode_operations erofs_fast_symlink_iops = {
392 .get_link = simple_get_link,
393 .getattr = erofs_getattr,
394 .listxattr = erofs_listxattr,
395 .get_inode_acl = erofs_get_acl,