1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * https://www.huawei.com/
8 #include <trace/events/erofs.h>
11 * if inode is successfully read, return its inode page (or sometimes
12 * the inode payload page if it's an extended inode) in order to fill
13 * inline data if possible.
15 static struct page *erofs_read_inode(struct inode *inode,
18 struct super_block *sb = inode->i_sb;
19 struct erofs_sb_info *sbi = EROFS_SB(sb);
20 struct erofs_inode *vi = EROFS_I(inode);
21 const erofs_off_t inode_loc = iloc(sbi, vi->nid);
23 erofs_blk_t blkaddr, nblks = 0;
25 struct erofs_inode_compact *dic;
26 struct erofs_inode_extended *die, *copied = NULL;
30 blkaddr = erofs_blknr(inode_loc);
31 *ofs = erofs_blkoff(inode_loc);
33 erofs_dbg("%s, reading inode nid %llu at %u of blkaddr %u",
34 __func__, vi->nid, *ofs, blkaddr);
36 page = erofs_get_meta_page(sb, blkaddr);
38 erofs_err(sb, "failed to get inode (nid: %llu) page, err %ld",
39 vi->nid, PTR_ERR(page));
43 dic = page_address(page) + *ofs;
44 ifmt = le16_to_cpu(dic->i_format);
46 if (ifmt & ~EROFS_I_ALL) {
47 erofs_err(inode->i_sb, "unsupported i_format %u of nid %llu",
53 vi->datalayout = erofs_inode_datalayout(ifmt);
54 if (vi->datalayout >= EROFS_INODE_DATALAYOUT_MAX) {
55 erofs_err(inode->i_sb, "unsupported datalayout %u of nid %llu",
56 vi->datalayout, vi->nid);
61 switch (erofs_inode_version(ifmt)) {
62 case EROFS_INODE_LAYOUT_EXTENDED:
63 vi->inode_isize = sizeof(struct erofs_inode_extended);
64 /* check if the inode acrosses page boundary */
65 if (*ofs + vi->inode_isize <= PAGE_SIZE) {
66 *ofs += vi->inode_isize;
67 die = (struct erofs_inode_extended *)dic;
69 const unsigned int gotten = PAGE_SIZE - *ofs;
71 copied = kmalloc(vi->inode_isize, GFP_NOFS);
76 memcpy(copied, dic, gotten);
80 page = erofs_get_meta_page(sb, blkaddr + 1);
82 erofs_err(sb, "failed to get inode payload page (nid: %llu), err %ld",
83 vi->nid, PTR_ERR(page));
87 *ofs = vi->inode_isize - gotten;
88 memcpy((u8 *)copied + gotten, page_address(page), *ofs);
91 vi->xattr_isize = erofs_xattr_ibody_size(die->i_xattr_icount);
93 inode->i_mode = le16_to_cpu(die->i_mode);
94 switch (inode->i_mode & S_IFMT) {
98 vi->raw_blkaddr = le32_to_cpu(die->i_u.raw_blkaddr);
103 new_decode_dev(le32_to_cpu(die->i_u.rdev));
112 i_uid_write(inode, le32_to_cpu(die->i_uid));
113 i_gid_write(inode, le32_to_cpu(die->i_gid));
114 set_nlink(inode, le32_to_cpu(die->i_nlink));
116 /* extended inode has its own timestamp */
117 inode->i_ctime.tv_sec = le64_to_cpu(die->i_ctime);
118 inode->i_ctime.tv_nsec = le32_to_cpu(die->i_ctime_nsec);
120 inode->i_size = le64_to_cpu(die->i_size);
122 /* total blocks for compressed files */
123 if (erofs_inode_is_data_compressed(vi->datalayout))
124 nblks = le32_to_cpu(die->i_u.compressed_blocks);
128 case EROFS_INODE_LAYOUT_COMPACT:
129 vi->inode_isize = sizeof(struct erofs_inode_compact);
130 *ofs += vi->inode_isize;
131 vi->xattr_isize = erofs_xattr_ibody_size(dic->i_xattr_icount);
133 inode->i_mode = le16_to_cpu(dic->i_mode);
134 switch (inode->i_mode & S_IFMT) {
138 vi->raw_blkaddr = le32_to_cpu(dic->i_u.raw_blkaddr);
143 new_decode_dev(le32_to_cpu(dic->i_u.rdev));
152 i_uid_write(inode, le16_to_cpu(dic->i_uid));
153 i_gid_write(inode, le16_to_cpu(dic->i_gid));
154 set_nlink(inode, le16_to_cpu(dic->i_nlink));
156 /* use build time for compact inodes */
157 inode->i_ctime.tv_sec = sbi->build_time;
158 inode->i_ctime.tv_nsec = sbi->build_time_nsec;
160 inode->i_size = le32_to_cpu(dic->i_size);
161 if (erofs_inode_is_data_compressed(vi->datalayout))
162 nblks = le32_to_cpu(dic->i_u.compressed_blocks);
165 erofs_err(inode->i_sb,
166 "unsupported on-disk inode version %u of nid %llu",
167 erofs_inode_version(ifmt), vi->nid);
172 inode->i_mtime.tv_sec = inode->i_ctime.tv_sec;
173 inode->i_atime.tv_sec = inode->i_ctime.tv_sec;
174 inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec;
175 inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec;
178 /* measure inode.i_blocks as generic filesystems */
179 inode->i_blocks = roundup(inode->i_size, EROFS_BLKSIZ) >> 9;
181 inode->i_blocks = nblks << LOG_SECTORS_PER_BLOCK;
185 erofs_err(inode->i_sb, "bogus i_mode (%o) @ nid %llu",
186 inode->i_mode, vi->nid);
196 static int erofs_fill_symlink(struct inode *inode, void *data,
199 struct erofs_inode *vi = EROFS_I(inode);
202 /* if it cannot be handled with fast symlink scheme */
203 if (vi->datalayout != EROFS_INODE_FLAT_INLINE ||
204 inode->i_size >= PAGE_SIZE) {
205 inode->i_op = &erofs_symlink_iops;
209 lnk = kmalloc(inode->i_size + 1, GFP_KERNEL);
213 m_pofs += vi->xattr_isize;
214 /* inline symlink data shouldn't cross page boundary as well */
215 if (m_pofs + inode->i_size > PAGE_SIZE) {
217 erofs_err(inode->i_sb,
218 "inline data cross block boundary @ nid %llu",
221 return -EFSCORRUPTED;
224 memcpy(lnk, data + m_pofs, inode->i_size);
225 lnk[inode->i_size] = '\0';
228 inode->i_op = &erofs_fast_symlink_iops;
232 static int erofs_fill_inode(struct inode *inode, int isdir)
234 struct erofs_inode *vi = EROFS_I(inode);
239 trace_erofs_fill_inode(inode, isdir);
241 /* read inode base data from disk */
242 page = erofs_read_inode(inode, &ofs);
244 return PTR_ERR(page);
246 /* setup the new inode */
247 switch (inode->i_mode & S_IFMT) {
249 inode->i_op = &erofs_generic_iops;
250 inode->i_fop = &generic_ro_fops;
253 inode->i_op = &erofs_dir_iops;
254 inode->i_fop = &erofs_dir_fops;
257 err = erofs_fill_symlink(inode, page_address(page), ofs);
260 inode_nohighmem(inode);
266 inode->i_op = &erofs_generic_iops;
267 init_special_inode(inode, inode->i_mode, inode->i_rdev);
274 if (erofs_inode_is_data_compressed(vi->datalayout)) {
275 err = z_erofs_fill_inode(inode);
278 inode->i_mapping->a_ops = &erofs_raw_access_aops;
287 * erofs nid is 64bits, but i_ino is 'unsigned long', therefore
288 * we should do more for 32-bit platform to find the right inode.
290 static int erofs_ilookup_test_actor(struct inode *inode, void *opaque)
292 const erofs_nid_t nid = *(erofs_nid_t *)opaque;
294 return EROFS_I(inode)->nid == nid;
297 static int erofs_iget_set_actor(struct inode *inode, void *opaque)
299 const erofs_nid_t nid = *(erofs_nid_t *)opaque;
301 inode->i_ino = erofs_inode_hash(nid);
305 static inline struct inode *erofs_iget_locked(struct super_block *sb,
308 const unsigned long hashval = erofs_inode_hash(nid);
310 return iget5_locked(sb, hashval, erofs_ilookup_test_actor,
311 erofs_iget_set_actor, &nid);
314 struct inode *erofs_iget(struct super_block *sb,
318 struct inode *inode = erofs_iget_locked(sb, nid);
321 return ERR_PTR(-ENOMEM);
323 if (inode->i_state & I_NEW) {
325 struct erofs_inode *vi = EROFS_I(inode);
329 err = erofs_fill_inode(inode, isdir);
331 unlock_new_inode(inode);
334 inode = ERR_PTR(err);
340 int erofs_getattr(struct user_namespace *mnt_userns, const struct path *path,
341 struct kstat *stat, u32 request_mask,
342 unsigned int query_flags)
344 struct inode *const inode = d_inode(path->dentry);
346 if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout))
347 stat->attributes |= STATX_ATTR_COMPRESSED;
349 stat->attributes |= STATX_ATTR_IMMUTABLE;
350 stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
351 STATX_ATTR_IMMUTABLE);
353 generic_fillattr(&init_user_ns, inode, stat);
357 const struct inode_operations erofs_generic_iops = {
358 .getattr = erofs_getattr,
359 .listxattr = erofs_listxattr,
360 .get_acl = erofs_get_acl,
363 const struct inode_operations erofs_symlink_iops = {
364 .get_link = page_get_link,
365 .getattr = erofs_getattr,
366 .listxattr = erofs_listxattr,
367 .get_acl = erofs_get_acl,
370 const struct inode_operations erofs_fast_symlink_iops = {
371 .get_link = simple_get_link,
372 .getattr = erofs_getattr,
373 .listxattr = erofs_listxattr,
374 .get_acl = erofs_get_acl,