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