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