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 = iloc(sbi, vi->nid);
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(inode_loc);
27 *ofs = erofs_blkoff(inode_loc);
29 erofs_dbg("%s, reading inode nid %llu at %u of blkaddr %u",
30 __func__, vi->nid, *ofs, blkaddr);
32 kaddr = erofs_read_metabuf(buf, sb, blkaddr, EROFS_KMAP);
34 erofs_err(sb, "failed to get inode (nid: %llu) page, err %ld",
35 vi->nid, PTR_ERR(kaddr));
40 ifmt = le16_to_cpu(dic->i_format);
42 if (ifmt & ~EROFS_I_ALL) {
43 erofs_err(inode->i_sb, "unsupported i_format %u of nid %llu",
49 vi->datalayout = erofs_inode_datalayout(ifmt);
50 if (vi->datalayout >= EROFS_INODE_DATALAYOUT_MAX) {
51 erofs_err(inode->i_sb, "unsupported datalayout %u of nid %llu",
52 vi->datalayout, vi->nid);
57 switch (erofs_inode_version(ifmt)) {
58 case EROFS_INODE_LAYOUT_EXTENDED:
59 vi->inode_isize = sizeof(struct erofs_inode_extended);
60 /* check if the extended inode acrosses block boundary */
61 if (*ofs + vi->inode_isize <= EROFS_BLKSIZ) {
62 *ofs += vi->inode_isize;
63 die = (struct erofs_inode_extended *)dic;
65 const unsigned int gotten = EROFS_BLKSIZ - *ofs;
67 copied = kmalloc(vi->inode_isize, GFP_NOFS);
72 memcpy(copied, dic, gotten);
73 kaddr = erofs_read_metabuf(buf, sb, blkaddr + 1,
76 erofs_err(sb, "failed to get inode payload block (nid: %llu), err %ld",
77 vi->nid, PTR_ERR(kaddr));
81 *ofs = vi->inode_isize - gotten;
82 memcpy((u8 *)copied + gotten, kaddr, *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_mtime);
112 inode->i_ctime.tv_nsec = le32_to_cpu(die->i_mtime_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);
119 else if (vi->datalayout == EROFS_INODE_CHUNK_BASED)
120 /* fill chunked inode summary info */
121 vi->chunkformat = le16_to_cpu(die->i_u.c.format);
125 case EROFS_INODE_LAYOUT_COMPACT:
126 vi->inode_isize = sizeof(struct erofs_inode_compact);
127 *ofs += vi->inode_isize;
128 vi->xattr_isize = erofs_xattr_ibody_size(dic->i_xattr_icount);
130 inode->i_mode = le16_to_cpu(dic->i_mode);
131 switch (inode->i_mode & S_IFMT) {
135 vi->raw_blkaddr = le32_to_cpu(dic->i_u.raw_blkaddr);
140 new_decode_dev(le32_to_cpu(dic->i_u.rdev));
149 i_uid_write(inode, le16_to_cpu(dic->i_uid));
150 i_gid_write(inode, le16_to_cpu(dic->i_gid));
151 set_nlink(inode, le16_to_cpu(dic->i_nlink));
153 /* use build time for compact inodes */
154 inode->i_ctime.tv_sec = sbi->build_time;
155 inode->i_ctime.tv_nsec = sbi->build_time_nsec;
157 inode->i_size = le32_to_cpu(dic->i_size);
158 if (erofs_inode_is_data_compressed(vi->datalayout))
159 nblks = le32_to_cpu(dic->i_u.compressed_blocks);
160 else if (vi->datalayout == EROFS_INODE_CHUNK_BASED)
161 vi->chunkformat = le16_to_cpu(dic->i_u.c.format);
164 erofs_err(inode->i_sb,
165 "unsupported on-disk inode version %u of nid %llu",
166 erofs_inode_version(ifmt), vi->nid);
171 if (vi->datalayout == EROFS_INODE_CHUNK_BASED) {
172 if (vi->chunkformat & ~EROFS_CHUNK_FORMAT_ALL) {
173 erofs_err(inode->i_sb,
174 "unsupported chunk format %x of nid %llu",
175 vi->chunkformat, vi->nid);
179 vi->chunkbits = LOG_BLOCK_SIZE +
180 (vi->chunkformat & EROFS_CHUNK_FORMAT_BLKBITS_MASK);
182 inode->i_mtime.tv_sec = inode->i_ctime.tv_sec;
183 inode->i_atime.tv_sec = inode->i_ctime.tv_sec;
184 inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec;
185 inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec;
187 inode->i_flags &= ~S_DAX;
188 if (test_opt(&sbi->opt, DAX_ALWAYS) && S_ISREG(inode->i_mode) &&
189 vi->datalayout == EROFS_INODE_FLAT_PLAIN)
190 inode->i_flags |= S_DAX;
192 /* measure inode.i_blocks as generic filesystems */
193 inode->i_blocks = roundup(inode->i_size, EROFS_BLKSIZ) >> 9;
195 inode->i_blocks = nblks << LOG_SECTORS_PER_BLOCK;
199 erofs_err(inode->i_sb, "bogus i_mode (%o) @ nid %llu",
200 inode->i_mode, vi->nid);
205 erofs_put_metabuf(buf);
209 static int erofs_fill_symlink(struct inode *inode, void *kaddr,
212 struct erofs_inode *vi = EROFS_I(inode);
215 /* if it cannot be handled with fast symlink scheme */
216 if (vi->datalayout != EROFS_INODE_FLAT_INLINE ||
217 inode->i_size >= EROFS_BLKSIZ) {
218 inode->i_op = &erofs_symlink_iops;
222 lnk = kmalloc(inode->i_size + 1, GFP_KERNEL);
226 m_pofs += vi->xattr_isize;
227 /* inline symlink data shouldn't cross block boundary */
228 if (m_pofs + inode->i_size > EROFS_BLKSIZ) {
230 erofs_err(inode->i_sb,
231 "inline data cross block boundary @ nid %llu",
234 return -EFSCORRUPTED;
236 memcpy(lnk, kaddr + m_pofs, inode->i_size);
237 lnk[inode->i_size] = '\0';
240 inode->i_op = &erofs_fast_symlink_iops;
244 static int erofs_fill_inode(struct inode *inode, int isdir)
246 struct erofs_inode *vi = EROFS_I(inode);
247 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
252 trace_erofs_fill_inode(inode, isdir);
254 /* read inode base data from disk */
255 kaddr = erofs_read_inode(&buf, inode, &ofs);
257 return PTR_ERR(kaddr);
259 /* setup the new inode */
260 switch (inode->i_mode & S_IFMT) {
262 inode->i_op = &erofs_generic_iops;
263 if (erofs_inode_is_data_compressed(vi->datalayout))
264 inode->i_fop = &generic_ro_fops;
266 inode->i_fop = &erofs_file_fops;
269 inode->i_op = &erofs_dir_iops;
270 inode->i_fop = &erofs_dir_fops;
273 err = erofs_fill_symlink(inode, kaddr, ofs);
276 inode_nohighmem(inode);
282 inode->i_op = &erofs_generic_iops;
283 init_special_inode(inode, inode->i_mode, inode->i_rdev);
290 if (erofs_inode_is_data_compressed(vi->datalayout)) {
291 if (!erofs_is_fscache_mode(inode->i_sb))
292 err = z_erofs_fill_inode(inode);
297 inode->i_mapping->a_ops = &erofs_raw_access_aops;
298 #ifdef CONFIG_EROFS_FS_ONDEMAND
299 if (erofs_is_fscache_mode(inode->i_sb))
300 inode->i_mapping->a_ops = &erofs_fscache_access_aops;
304 erofs_put_metabuf(&buf);
309 * erofs nid is 64bits, but i_ino is 'unsigned long', therefore
310 * we should do more for 32-bit platform to find the right inode.
312 static int erofs_ilookup_test_actor(struct inode *inode, void *opaque)
314 const erofs_nid_t nid = *(erofs_nid_t *)opaque;
316 return EROFS_I(inode)->nid == nid;
319 static int erofs_iget_set_actor(struct inode *inode, void *opaque)
321 const erofs_nid_t nid = *(erofs_nid_t *)opaque;
323 inode->i_ino = erofs_inode_hash(nid);
327 static inline struct inode *erofs_iget_locked(struct super_block *sb,
330 const unsigned long hashval = erofs_inode_hash(nid);
332 return iget5_locked(sb, hashval, erofs_ilookup_test_actor,
333 erofs_iget_set_actor, &nid);
336 struct inode *erofs_iget(struct super_block *sb,
340 struct inode *inode = erofs_iget_locked(sb, nid);
343 return ERR_PTR(-ENOMEM);
345 if (inode->i_state & I_NEW) {
347 struct erofs_inode *vi = EROFS_I(inode);
351 err = erofs_fill_inode(inode, isdir);
353 unlock_new_inode(inode);
356 inode = ERR_PTR(err);
362 int erofs_getattr(struct user_namespace *mnt_userns, const struct path *path,
363 struct kstat *stat, u32 request_mask,
364 unsigned int query_flags)
366 struct inode *const inode = d_inode(path->dentry);
368 if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout))
369 stat->attributes |= STATX_ATTR_COMPRESSED;
371 stat->attributes |= STATX_ATTR_IMMUTABLE;
372 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
373 STATX_ATTR_IMMUTABLE);
375 generic_fillattr(mnt_userns, inode, stat);
379 const struct inode_operations erofs_generic_iops = {
380 .getattr = erofs_getattr,
381 .listxattr = erofs_listxattr,
382 .get_acl = erofs_get_acl,
383 .fiemap = erofs_fiemap,
386 const struct inode_operations erofs_symlink_iops = {
387 .get_link = page_get_link,
388 .getattr = erofs_getattr,
389 .listxattr = erofs_listxattr,
390 .get_acl = erofs_get_acl,
393 const struct inode_operations erofs_fast_symlink_iops = {
394 .get_link = simple_get_link,
395 .getattr = erofs_getattr,
396 .listxattr = erofs_listxattr,
397 .get_acl = erofs_get_acl,