Gao Xiang | 29b24f6 | 2019-07-31 23:57:31 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 2 | /* |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 3 | * Copyright (C) 2017-2018 HUAWEI, Inc. |
Alexander A. Klimov | 592e7cd | 2020-07-13 15:09:44 +0200 | [diff] [blame] | 4 | * https://www.huawei.com/ |
Gao Xiang | c5aa903 | 2021-08-20 18:00:19 +0800 | [diff] [blame] | 5 | * Copyright (C) 2021, Alibaba Cloud |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 6 | */ |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 7 | #include "xattr.h" |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 8 | |
Chao Yu | 13f06f4 | 2018-07-26 20:21:55 +0800 | [diff] [blame] | 9 | #include <trace/events/erofs.h> |
| 10 | |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 11 | /* |
| 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. |
| 15 | */ |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 16 | static void *erofs_read_inode(struct erofs_buf *buf, |
| 17 | struct inode *inode, unsigned int *ofs) |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 18 | { |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 19 | struct super_block *sb = inode->i_sb; |
| 20 | struct erofs_sb_info *sbi = EROFS_SB(sb); |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 21 | struct erofs_inode *vi = EROFS_I(inode); |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 22 | const erofs_off_t inode_loc = iloc(sbi, vi->nid); |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 23 | |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 24 | erofs_blk_t blkaddr, nblks = 0; |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 25 | void *kaddr; |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 26 | struct erofs_inode_compact *dic; |
| 27 | struct erofs_inode_extended *die, *copied = NULL; |
| 28 | unsigned int ifmt; |
| 29 | int err; |
| 30 | |
| 31 | blkaddr = erofs_blknr(inode_loc); |
| 32 | *ofs = erofs_blkoff(inode_loc); |
| 33 | |
| 34 | erofs_dbg("%s, reading inode nid %llu at %u of blkaddr %u", |
| 35 | __func__, vi->nid, *ofs, blkaddr); |
| 36 | |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 37 | kaddr = erofs_read_metabuf(buf, sb, blkaddr, EROFS_KMAP); |
| 38 | if (IS_ERR(kaddr)) { |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 39 | erofs_err(sb, "failed to get inode (nid: %llu) page, err %ld", |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 40 | vi->nid, PTR_ERR(kaddr)); |
| 41 | return kaddr; |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 42 | } |
| 43 | |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 44 | dic = kaddr + *ofs; |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 45 | ifmt = le16_to_cpu(dic->i_format); |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 46 | |
Gao Xiang | 24a806d | 2021-03-29 08:36:14 +0800 | [diff] [blame] | 47 | if (ifmt & ~EROFS_I_ALL) { |
| 48 | erofs_err(inode->i_sb, "unsupported i_format %u of nid %llu", |
| 49 | ifmt, vi->nid); |
| 50 | err = -EOPNOTSUPP; |
| 51 | goto err_out; |
| 52 | } |
| 53 | |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 54 | vi->datalayout = erofs_inode_datalayout(ifmt); |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 55 | if (vi->datalayout >= EROFS_INODE_DATALAYOUT_MAX) { |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 56 | erofs_err(inode->i_sb, "unsupported datalayout %u of nid %llu", |
| 57 | vi->datalayout, vi->nid); |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 58 | err = -EOPNOTSUPP; |
| 59 | goto err_out; |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 60 | } |
| 61 | |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 62 | switch (erofs_inode_version(ifmt)) { |
| 63 | case EROFS_INODE_LAYOUT_EXTENDED: |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 64 | vi->inode_isize = sizeof(struct erofs_inode_extended); |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 65 | /* check if the extended inode acrosses block boundary */ |
| 66 | if (*ofs + vi->inode_isize <= EROFS_BLKSIZ) { |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 67 | *ofs += vi->inode_isize; |
| 68 | die = (struct erofs_inode_extended *)dic; |
| 69 | } else { |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 70 | const unsigned int gotten = EROFS_BLKSIZ - *ofs; |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 71 | |
| 72 | copied = kmalloc(vi->inode_isize, GFP_NOFS); |
| 73 | if (!copied) { |
| 74 | err = -ENOMEM; |
| 75 | goto err_out; |
| 76 | } |
| 77 | memcpy(copied, dic, gotten); |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 78 | kaddr = erofs_read_metabuf(buf, sb, blkaddr + 1, |
| 79 | EROFS_KMAP); |
| 80 | if (IS_ERR(kaddr)) { |
| 81 | erofs_err(sb, "failed to get inode payload block (nid: %llu), err %ld", |
| 82 | vi->nid, PTR_ERR(kaddr)); |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 83 | kfree(copied); |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 84 | return kaddr; |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 85 | } |
| 86 | *ofs = vi->inode_isize - gotten; |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 87 | memcpy((u8 *)copied + gotten, kaddr, *ofs); |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 88 | die = copied; |
| 89 | } |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 90 | vi->xattr_isize = erofs_xattr_ibody_size(die->i_xattr_icount); |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 91 | |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 92 | inode->i_mode = le16_to_cpu(die->i_mode); |
| 93 | switch (inode->i_mode & S_IFMT) { |
| 94 | case S_IFREG: |
| 95 | case S_IFDIR: |
| 96 | case S_IFLNK: |
| 97 | vi->raw_blkaddr = le32_to_cpu(die->i_u.raw_blkaddr); |
| 98 | break; |
| 99 | case S_IFCHR: |
| 100 | case S_IFBLK: |
Chao Yu | d5beb31b | 2018-07-26 20:21:53 +0800 | [diff] [blame] | 101 | inode->i_rdev = |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 102 | new_decode_dev(le32_to_cpu(die->i_u.rdev)); |
| 103 | break; |
| 104 | case S_IFIFO: |
| 105 | case S_IFSOCK: |
Chao Yu | d5beb31b | 2018-07-26 20:21:53 +0800 | [diff] [blame] | 106 | inode->i_rdev = 0; |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 107 | break; |
| 108 | default: |
Gao Xiang | a6b9b1d | 2019-08-14 18:37:03 +0800 | [diff] [blame] | 109 | goto bogusimode; |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 110 | } |
| 111 | i_uid_write(inode, le32_to_cpu(die->i_uid)); |
| 112 | i_gid_write(inode, le32_to_cpu(die->i_gid)); |
| 113 | set_nlink(inode, le32_to_cpu(die->i_nlink)); |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 114 | |
Gao Xiang | d3938ee | 2020-11-01 03:51:02 +0800 | [diff] [blame] | 115 | /* extended inode has its own timestamp */ |
| 116 | inode->i_ctime.tv_sec = le64_to_cpu(die->i_ctime); |
| 117 | inode->i_ctime.tv_nsec = le32_to_cpu(die->i_ctime_nsec); |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 118 | |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 119 | inode->i_size = le64_to_cpu(die->i_size); |
Gao Xiang | fe6d987 | 2019-05-28 11:19:43 +0800 | [diff] [blame] | 120 | |
| 121 | /* total blocks for compressed files */ |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 122 | if (erofs_inode_is_data_compressed(vi->datalayout)) |
| 123 | nblks = le32_to_cpu(die->i_u.compressed_blocks); |
Gao Xiang | c5aa903 | 2021-08-20 18:00:19 +0800 | [diff] [blame] | 124 | else if (vi->datalayout == EROFS_INODE_CHUNK_BASED) |
| 125 | /* fill chunked inode summary info */ |
| 126 | vi->chunkformat = le16_to_cpu(die->i_u.c.format); |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 127 | kfree(copied); |
Gao Xiang | 1266b4a | 2021-08-25 20:07:57 +0800 | [diff] [blame] | 128 | copied = NULL; |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 129 | break; |
| 130 | case EROFS_INODE_LAYOUT_COMPACT: |
| 131 | vi->inode_isize = sizeof(struct erofs_inode_compact); |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 132 | *ofs += vi->inode_isize; |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 133 | vi->xattr_isize = erofs_xattr_ibody_size(dic->i_xattr_icount); |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 134 | |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 135 | inode->i_mode = le16_to_cpu(dic->i_mode); |
| 136 | switch (inode->i_mode & S_IFMT) { |
| 137 | case S_IFREG: |
| 138 | case S_IFDIR: |
| 139 | case S_IFLNK: |
| 140 | vi->raw_blkaddr = le32_to_cpu(dic->i_u.raw_blkaddr); |
| 141 | break; |
| 142 | case S_IFCHR: |
| 143 | case S_IFBLK: |
Chao Yu | d5beb31b | 2018-07-26 20:21:53 +0800 | [diff] [blame] | 144 | inode->i_rdev = |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 145 | new_decode_dev(le32_to_cpu(dic->i_u.rdev)); |
| 146 | break; |
| 147 | case S_IFIFO: |
| 148 | case S_IFSOCK: |
Chao Yu | d5beb31b | 2018-07-26 20:21:53 +0800 | [diff] [blame] | 149 | inode->i_rdev = 0; |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 150 | break; |
| 151 | default: |
Gao Xiang | a6b9b1d | 2019-08-14 18:37:03 +0800 | [diff] [blame] | 152 | goto bogusimode; |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 153 | } |
| 154 | i_uid_write(inode, le16_to_cpu(dic->i_uid)); |
| 155 | i_gid_write(inode, le16_to_cpu(dic->i_gid)); |
| 156 | set_nlink(inode, le16_to_cpu(dic->i_nlink)); |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 157 | |
Gao Xiang | d3938ee | 2020-11-01 03:51:02 +0800 | [diff] [blame] | 158 | /* use build time for compact inodes */ |
| 159 | inode->i_ctime.tv_sec = sbi->build_time; |
| 160 | inode->i_ctime.tv_nsec = sbi->build_time_nsec; |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 161 | |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 162 | inode->i_size = le32_to_cpu(dic->i_size); |
| 163 | if (erofs_inode_is_data_compressed(vi->datalayout)) |
| 164 | nblks = le32_to_cpu(dic->i_u.compressed_blocks); |
Gao Xiang | c5aa903 | 2021-08-20 18:00:19 +0800 | [diff] [blame] | 165 | else if (vi->datalayout == EROFS_INODE_CHUNK_BASED) |
| 166 | vi->chunkformat = le16_to_cpu(dic->i_u.c.format); |
Gao Xiang | 8a76568 | 2019-09-04 10:08:54 +0800 | [diff] [blame] | 167 | break; |
| 168 | default: |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 169 | erofs_err(inode->i_sb, |
| 170 | "unsupported on-disk inode version %u of nid %llu", |
| 171 | erofs_inode_version(ifmt), vi->nid); |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 172 | err = -EOPNOTSUPP; |
| 173 | goto err_out; |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 174 | } |
| 175 | |
Gao Xiang | c5aa903 | 2021-08-20 18:00:19 +0800 | [diff] [blame] | 176 | if (vi->datalayout == EROFS_INODE_CHUNK_BASED) { |
Gao Xiang | d705117 | 2021-09-22 17:51:41 +0800 | [diff] [blame] | 177 | if (vi->chunkformat & ~EROFS_CHUNK_FORMAT_ALL) { |
Gao Xiang | c5aa903 | 2021-08-20 18:00:19 +0800 | [diff] [blame] | 178 | erofs_err(inode->i_sb, |
| 179 | "unsupported chunk format %x of nid %llu", |
| 180 | vi->chunkformat, vi->nid); |
| 181 | err = -EOPNOTSUPP; |
| 182 | goto err_out; |
| 183 | } |
| 184 | vi->chunkbits = LOG_BLOCK_SIZE + |
| 185 | (vi->chunkformat & EROFS_CHUNK_FORMAT_BLKBITS_MASK); |
| 186 | } |
Gao Xiang | d3938ee | 2020-11-01 03:51:02 +0800 | [diff] [blame] | 187 | inode->i_mtime.tv_sec = inode->i_ctime.tv_sec; |
| 188 | inode->i_atime.tv_sec = inode->i_ctime.tv_sec; |
| 189 | inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec; |
| 190 | inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec; |
| 191 | |
Gao Xiang | 06252e9 | 2021-08-05 08:36:00 +0800 | [diff] [blame] | 192 | inode->i_flags &= ~S_DAX; |
Gao Xiang | e624246 | 2021-10-07 15:02:23 +0800 | [diff] [blame] | 193 | if (test_opt(&sbi->opt, DAX_ALWAYS) && S_ISREG(inode->i_mode) && |
Gao Xiang | 06252e9 | 2021-08-05 08:36:00 +0800 | [diff] [blame] | 194 | vi->datalayout == EROFS_INODE_FLAT_PLAIN) |
| 195 | inode->i_flags |= S_DAX; |
Gao Xiang | fe6d987 | 2019-05-28 11:19:43 +0800 | [diff] [blame] | 196 | if (!nblks) |
| 197 | /* measure inode.i_blocks as generic filesystems */ |
| 198 | inode->i_blocks = roundup(inode->i_size, EROFS_BLKSIZ) >> 9; |
| 199 | else |
| 200 | inode->i_blocks = nblks << LOG_SECTORS_PER_BLOCK; |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 201 | return kaddr; |
Gao Xiang | a6b9b1d | 2019-08-14 18:37:03 +0800 | [diff] [blame] | 202 | |
| 203 | bogusimode: |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 204 | erofs_err(inode->i_sb, "bogus i_mode (%o) @ nid %llu", |
| 205 | inode->i_mode, vi->nid); |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 206 | err = -EFSCORRUPTED; |
| 207 | err_out: |
Gao Xiang | a6b9b1d | 2019-08-14 18:37:03 +0800 | [diff] [blame] | 208 | DBG_BUGON(1); |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 209 | kfree(copied); |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 210 | erofs_put_metabuf(buf); |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 211 | return ERR_PTR(err); |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 212 | } |
| 213 | |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 214 | static int erofs_fill_symlink(struct inode *inode, void *kaddr, |
Gao Xiang | a2c75c8 | 2019-09-04 10:08:59 +0800 | [diff] [blame] | 215 | unsigned int m_pofs) |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 216 | { |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 217 | struct erofs_inode *vi = EROFS_I(inode); |
Gao Xiang | a2c75c8 | 2019-09-04 10:08:59 +0800 | [diff] [blame] | 218 | char *lnk; |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 219 | |
Gao Xiang | a2c75c8 | 2019-09-04 10:08:59 +0800 | [diff] [blame] | 220 | /* if it cannot be handled with fast symlink scheme */ |
| 221 | if (vi->datalayout != EROFS_INODE_FLAT_INLINE || |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 222 | inode->i_size >= EROFS_BLKSIZ) { |
Gao Xiang | a2c75c8 | 2019-09-04 10:08:59 +0800 | [diff] [blame] | 223 | inode->i_op = &erofs_symlink_iops; |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 224 | return 0; |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 225 | } |
Gao Xiang | a2c75c8 | 2019-09-04 10:08:59 +0800 | [diff] [blame] | 226 | |
Gao Xiang | e2c71e7 | 2019-09-04 10:09:06 +0800 | [diff] [blame] | 227 | lnk = kmalloc(inode->i_size + 1, GFP_KERNEL); |
Gao Xiang | a2c75c8 | 2019-09-04 10:08:59 +0800 | [diff] [blame] | 228 | if (!lnk) |
| 229 | return -ENOMEM; |
| 230 | |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 231 | m_pofs += vi->xattr_isize; |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 232 | /* inline symlink data shouldn't cross block boundary */ |
| 233 | if (m_pofs + inode->i_size > EROFS_BLKSIZ) { |
Gao Xiang | a2c75c8 | 2019-09-04 10:08:59 +0800 | [diff] [blame] | 234 | kfree(lnk); |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 235 | erofs_err(inode->i_sb, |
| 236 | "inline data cross block boundary @ nid %llu", |
| 237 | vi->nid); |
Gao Xiang | a2c75c8 | 2019-09-04 10:08:59 +0800 | [diff] [blame] | 238 | DBG_BUGON(1); |
| 239 | return -EFSCORRUPTED; |
| 240 | } |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 241 | memcpy(lnk, kaddr + m_pofs, inode->i_size); |
Gao Xiang | a2c75c8 | 2019-09-04 10:08:59 +0800 | [diff] [blame] | 242 | lnk[inode->i_size] = '\0'; |
| 243 | |
| 244 | inode->i_link = lnk; |
| 245 | inode->i_op = &erofs_fast_symlink_iops; |
Yue Hu | 5545745 | 2019-06-27 17:46:15 +0800 | [diff] [blame] | 246 | return 0; |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 247 | } |
| 248 | |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 249 | static int erofs_fill_inode(struct inode *inode, int isdir) |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 250 | { |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 251 | struct erofs_inode *vi = EROFS_I(inode); |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 252 | struct erofs_buf buf = __EROFS_BUF_INITIALIZER; |
| 253 | void *kaddr; |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 254 | unsigned int ofs; |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 255 | int err = 0; |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 256 | |
Chao Yu | 13f06f4 | 2018-07-26 20:21:55 +0800 | [diff] [blame] | 257 | trace_erofs_fill_inode(inode, isdir); |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 258 | |
Gao Xiang | 0dcd3c9 | 2020-07-30 01:58:01 +0800 | [diff] [blame] | 259 | /* read inode base data from disk */ |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 260 | kaddr = erofs_read_inode(&buf, inode, &ofs); |
| 261 | if (IS_ERR(kaddr)) |
| 262 | return PTR_ERR(kaddr); |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 263 | |
Gao Xiang | 84947eb | 2019-09-04 10:09:08 +0800 | [diff] [blame] | 264 | /* setup the new inode */ |
| 265 | switch (inode->i_mode & S_IFMT) { |
| 266 | case S_IFREG: |
| 267 | inode->i_op = &erofs_generic_iops; |
Huang Jianan | a08e67a | 2021-08-05 08:35:59 +0800 | [diff] [blame] | 268 | if (erofs_inode_is_data_compressed(vi->datalayout)) |
| 269 | inode->i_fop = &generic_ro_fops; |
| 270 | else |
| 271 | inode->i_fop = &erofs_file_fops; |
Gao Xiang | 84947eb | 2019-09-04 10:09:08 +0800 | [diff] [blame] | 272 | break; |
| 273 | case S_IFDIR: |
| 274 | inode->i_op = &erofs_dir_iops; |
| 275 | inode->i_fop = &erofs_dir_fops; |
| 276 | break; |
| 277 | case S_IFLNK: |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 278 | err = erofs_fill_symlink(inode, kaddr, ofs); |
Gao Xiang | 84947eb | 2019-09-04 10:09:08 +0800 | [diff] [blame] | 279 | if (err) |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 280 | goto out_unlock; |
Gao Xiang | 84947eb | 2019-09-04 10:09:08 +0800 | [diff] [blame] | 281 | inode_nohighmem(inode); |
| 282 | break; |
| 283 | case S_IFCHR: |
| 284 | case S_IFBLK: |
| 285 | case S_IFIFO: |
| 286 | case S_IFSOCK: |
| 287 | inode->i_op = &erofs_generic_iops; |
| 288 | init_special_inode(inode, inode->i_mode, inode->i_rdev); |
| 289 | goto out_unlock; |
| 290 | default: |
| 291 | err = -EFSCORRUPTED; |
| 292 | goto out_unlock; |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 293 | } |
| 294 | |
Gao Xiang | 84947eb | 2019-09-04 10:09:08 +0800 | [diff] [blame] | 295 | if (erofs_inode_is_data_compressed(vi->datalayout)) { |
| 296 | err = z_erofs_fill_inode(inode); |
| 297 | goto out_unlock; |
| 298 | } |
| 299 | inode->i_mapping->a_ops = &erofs_raw_access_aops; |
| 300 | |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 301 | out_unlock: |
Gao Xiang | c521e3a | 2022-01-02 12:00:14 +0800 | [diff] [blame] | 302 | erofs_put_metabuf(&buf); |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 303 | return err; |
| 304 | } |
| 305 | |
Gao Xiang | 2abd781 | 2018-10-09 22:07:13 +0800 | [diff] [blame] | 306 | /* |
| 307 | * erofs nid is 64bits, but i_ino is 'unsigned long', therefore |
| 308 | * we should do more for 32-bit platform to find the right inode. |
| 309 | */ |
Gao Xiang | 2abd781 | 2018-10-09 22:07:13 +0800 | [diff] [blame] | 310 | static int erofs_ilookup_test_actor(struct inode *inode, void *opaque) |
| 311 | { |
| 312 | const erofs_nid_t nid = *(erofs_nid_t *)opaque; |
| 313 | |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 314 | return EROFS_I(inode)->nid == nid; |
Gao Xiang | 2abd781 | 2018-10-09 22:07:13 +0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static int erofs_iget_set_actor(struct inode *inode, void *opaque) |
| 318 | { |
| 319 | const erofs_nid_t nid = *(erofs_nid_t *)opaque; |
| 320 | |
| 321 | inode->i_ino = erofs_inode_hash(nid); |
| 322 | return 0; |
| 323 | } |
Gao Xiang | 2abd781 | 2018-10-09 22:07:13 +0800 | [diff] [blame] | 324 | |
| 325 | static inline struct inode *erofs_iget_locked(struct super_block *sb, |
| 326 | erofs_nid_t nid) |
| 327 | { |
| 328 | const unsigned long hashval = erofs_inode_hash(nid); |
| 329 | |
Gao Xiang | 2abd781 | 2018-10-09 22:07:13 +0800 | [diff] [blame] | 330 | return iget5_locked(sb, hashval, erofs_ilookup_test_actor, |
| 331 | erofs_iget_set_actor, &nid); |
Gao Xiang | 2abd781 | 2018-10-09 22:07:13 +0800 | [diff] [blame] | 332 | } |
| 333 | |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 334 | struct inode *erofs_iget(struct super_block *sb, |
Julian Merida | 447a362 | 2019-03-18 20:58:41 -0300 | [diff] [blame] | 335 | erofs_nid_t nid, |
| 336 | bool isdir) |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 337 | { |
Gao Xiang | 2abd781 | 2018-10-09 22:07:13 +0800 | [diff] [blame] | 338 | struct inode *inode = erofs_iget_locked(sb, nid); |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 339 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 340 | if (!inode) |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 341 | return ERR_PTR(-ENOMEM); |
| 342 | |
| 343 | if (inode->i_state & I_NEW) { |
| 344 | int err; |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 345 | struct erofs_inode *vi = EROFS_I(inode); |
Julio Bianco | 8af3647 | 2019-03-09 14:08:53 -0300 | [diff] [blame] | 346 | |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 347 | vi->nid = nid; |
| 348 | |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 349 | err = erofs_fill_inode(inode, isdir); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 350 | if (!err) |
Gao Xiang | 431339b | 2018-07-26 20:21:48 +0800 | [diff] [blame] | 351 | unlock_new_inode(inode); |
| 352 | else { |
| 353 | iget_failed(inode); |
| 354 | inode = ERR_PTR(err); |
| 355 | } |
| 356 | } |
| 357 | return inode; |
| 358 | } |
| 359 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 360 | int erofs_getattr(struct user_namespace *mnt_userns, const struct path *path, |
| 361 | struct kstat *stat, u32 request_mask, |
| 362 | unsigned int query_flags) |
Gao Xiang | 89f27ed | 2019-05-28 11:19:42 +0800 | [diff] [blame] | 363 | { |
| 364 | struct inode *const inode = d_inode(path->dentry); |
Gao Xiang | 89f27ed | 2019-05-28 11:19:42 +0800 | [diff] [blame] | 365 | |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 366 | if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) |
Gao Xiang | 89f27ed | 2019-05-28 11:19:42 +0800 | [diff] [blame] | 367 | stat->attributes |= STATX_ATTR_COMPRESSED; |
| 368 | |
| 369 | stat->attributes |= STATX_ATTR_IMMUTABLE; |
| 370 | stat->attributes_mask |= (STATX_ATTR_COMPRESSED | |
| 371 | STATX_ATTR_IMMUTABLE); |
| 372 | |
Christian Brauner | 0d56a45 | 2021-01-21 14:19:30 +0100 | [diff] [blame] | 373 | generic_fillattr(&init_user_ns, inode, stat); |
Gao Xiang | 89f27ed | 2019-05-28 11:19:42 +0800 | [diff] [blame] | 374 | return 0; |
| 375 | } |
| 376 | |
Gao Xiang | 6093982 | 2019-01-14 19:40:24 +0800 | [diff] [blame] | 377 | const struct inode_operations erofs_generic_iops = { |
Gao Xiang | 89f27ed | 2019-05-28 11:19:42 +0800 | [diff] [blame] | 378 | .getattr = erofs_getattr, |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 379 | .listxattr = erofs_listxattr, |
Gao Xiang | 516c115c | 2019-01-29 16:35:20 +0800 | [diff] [blame] | 380 | .get_acl = erofs_get_acl, |
Gao Xiang | eadcd6b | 2021-08-13 13:29:31 +0800 | [diff] [blame] | 381 | .fiemap = erofs_fiemap, |
Gao Xiang | 6093982 | 2019-01-14 19:40:24 +0800 | [diff] [blame] | 382 | }; |
| 383 | |
| 384 | const struct inode_operations erofs_symlink_iops = { |
| 385 | .get_link = page_get_link, |
Gao Xiang | 89f27ed | 2019-05-28 11:19:42 +0800 | [diff] [blame] | 386 | .getattr = erofs_getattr, |
Gao Xiang | 6093982 | 2019-01-14 19:40:24 +0800 | [diff] [blame] | 387 | .listxattr = erofs_listxattr, |
Gao Xiang | 516c115c | 2019-01-29 16:35:20 +0800 | [diff] [blame] | 388 | .get_acl = erofs_get_acl, |
Gao Xiang | 6093982 | 2019-01-14 19:40:24 +0800 | [diff] [blame] | 389 | }; |
| 390 | |
| 391 | const struct inode_operations erofs_fast_symlink_iops = { |
| 392 | .get_link = simple_get_link, |
Gao Xiang | 89f27ed | 2019-05-28 11:19:42 +0800 | [diff] [blame] | 393 | .getattr = erofs_getattr, |
Gao Xiang | 6093982 | 2019-01-14 19:40:24 +0800 | [diff] [blame] | 394 | .listxattr = erofs_listxattr, |
Gao Xiang | 516c115c | 2019-01-29 16:35:20 +0800 | [diff] [blame] | 395 | .get_acl = erofs_get_acl, |
Gao Xiang | 6093982 | 2019-01-14 19:40:24 +0800 | [diff] [blame] | 396 | }; |