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