Gao Xiang | 29b24f6 | 2019-07-31 23:57:31 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 2 | /* |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 3 | * Copyright (C) 2018-2019 HUAWEI, Inc. |
Alexander A. Klimov | 592e7cd | 2020-07-13 15:09:44 +0200 | [diff] [blame] | 4 | * https://www.huawei.com/ |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 5 | */ |
| 6 | #include "internal.h" |
| 7 | #include <asm/unaligned.h> |
| 8 | #include <trace/events/erofs.h> |
| 9 | |
Yue Hu | ab92184 | 2021-12-28 13:46:04 +0800 | [diff] [blame] | 10 | static int z_erofs_do_map_blocks(struct inode *inode, |
| 11 | struct erofs_map_blocks *map, |
| 12 | int flags); |
| 13 | |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 14 | int z_erofs_fill_inode(struct inode *inode) |
| 15 | { |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 16 | struct erofs_inode *const vi = EROFS_I(inode); |
Gao Xiang | cec6e93 | 2021-04-07 12:39:24 +0800 | [diff] [blame] | 17 | struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 18 | |
Gao Xiang | cec6e93 | 2021-04-07 12:39:24 +0800 | [diff] [blame] | 19 | if (!erofs_sb_has_big_pcluster(sbi) && |
Yue Hu | ab92184 | 2021-12-28 13:46:04 +0800 | [diff] [blame] | 20 | !erofs_sb_has_ztailpacking(sbi) && |
Gao Xiang | cec6e93 | 2021-04-07 12:39:24 +0800 | [diff] [blame] | 21 | vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY) { |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 22 | vi->z_advise = 0; |
| 23 | vi->z_algorithmtype[0] = 0; |
| 24 | vi->z_algorithmtype[1] = 0; |
Gao Xiang | eace994 | 2019-07-31 23:57:48 +0800 | [diff] [blame] | 25 | vi->z_logical_clusterbits = LOG_BLOCK_SIZE; |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 26 | set_bit(EROFS_I_Z_INITED_BIT, &vi->flags); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 27 | } |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 28 | inode->i_mapping->a_ops = &z_erofs_aops; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 29 | return 0; |
| 30 | } |
| 31 | |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 32 | static int z_erofs_fill_inode_lazy(struct inode *inode) |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 33 | { |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 34 | struct erofs_inode *const vi = EROFS_I(inode); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 35 | struct super_block *const sb = inode->i_sb; |
Gao Xiang | 72bb526 | 2021-10-18 00:57:21 +0800 | [diff] [blame] | 36 | int err, headnr; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 37 | erofs_off_t pos; |
Gao Xiang | 09c5437 | 2022-01-02 12:00:17 +0800 | [diff] [blame^] | 38 | struct erofs_buf buf = __EROFS_BUF_INITIALIZER; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 39 | void *kaddr; |
| 40 | struct z_erofs_map_header *h; |
| 41 | |
Gao Xiang | ce063129 | 2021-02-09 21:06:18 +0800 | [diff] [blame] | 42 | if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags)) { |
| 43 | /* |
| 44 | * paired with smp_mb() at the end of the function to ensure |
| 45 | * fields will only be observed after the bit is set. |
| 46 | */ |
| 47 | smp_mb(); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 48 | return 0; |
Gao Xiang | ce063129 | 2021-02-09 21:06:18 +0800 | [diff] [blame] | 49 | } |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 50 | |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 51 | if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_Z_BIT, TASK_KILLABLE)) |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 52 | return -ERESTARTSYS; |
| 53 | |
| 54 | err = 0; |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 55 | if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags)) |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 56 | goto out_unlock; |
| 57 | |
Gao Xiang | cec6e93 | 2021-04-07 12:39:24 +0800 | [diff] [blame] | 58 | DBG_BUGON(!erofs_sb_has_big_pcluster(EROFS_SB(sb)) && |
Yue Hu | ab92184 | 2021-12-28 13:46:04 +0800 | [diff] [blame] | 59 | !erofs_sb_has_ztailpacking(EROFS_SB(sb)) && |
Gao Xiang | cec6e93 | 2021-04-07 12:39:24 +0800 | [diff] [blame] | 60 | vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 61 | |
| 62 | pos = ALIGN(iloc(EROFS_SB(sb), vi->nid) + vi->inode_isize + |
| 63 | vi->xattr_isize, 8); |
Gao Xiang | 09c5437 | 2022-01-02 12:00:17 +0800 | [diff] [blame^] | 64 | kaddr = erofs_read_metabuf(&buf, sb, erofs_blknr(pos), |
| 65 | EROFS_KMAP_ATOMIC); |
| 66 | if (IS_ERR(kaddr)) { |
| 67 | err = PTR_ERR(kaddr); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 68 | goto out_unlock; |
| 69 | } |
| 70 | |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 71 | h = kaddr + erofs_blkoff(pos); |
| 72 | vi->z_advise = le16_to_cpu(h->h_advise); |
| 73 | vi->z_algorithmtype[0] = h->h_algorithmtype & 15; |
| 74 | vi->z_algorithmtype[1] = h->h_algorithmtype >> 4; |
| 75 | |
Gao Xiang | 72bb526 | 2021-10-18 00:57:21 +0800 | [diff] [blame] | 76 | headnr = 0; |
| 77 | if (vi->z_algorithmtype[0] >= Z_EROFS_COMPRESSION_MAX || |
| 78 | vi->z_algorithmtype[++headnr] >= Z_EROFS_COMPRESSION_MAX) { |
| 79 | erofs_err(sb, "unknown HEAD%u format %u for nid %llu, please upgrade kernel", |
| 80 | headnr + 1, vi->z_algorithmtype[headnr], vi->nid); |
Gao Xiang | ff784a7 | 2019-08-14 18:37:05 +0800 | [diff] [blame] | 81 | err = -EOPNOTSUPP; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 82 | goto unmap_done; |
| 83 | } |
| 84 | |
| 85 | vi->z_logical_clusterbits = LOG_BLOCK_SIZE + (h->h_clusterbits & 7); |
Gao Xiang | b86269f | 2021-04-07 12:39:25 +0800 | [diff] [blame] | 86 | if (!erofs_sb_has_big_pcluster(EROFS_SB(sb)) && |
| 87 | vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 | |
| 88 | Z_EROFS_ADVISE_BIG_PCLUSTER_2)) { |
| 89 | erofs_err(sb, "per-inode big pcluster without sb feature for nid %llu", |
| 90 | vi->nid); |
| 91 | err = -EFSCORRUPTED; |
| 92 | goto unmap_done; |
| 93 | } |
| 94 | if (vi->datalayout == EROFS_INODE_FLAT_COMPRESSION && |
| 95 | !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1) ^ |
| 96 | !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2)) { |
| 97 | erofs_err(sb, "big pcluster head1/2 of compact indexes should be consistent for nid %llu", |
| 98 | vi->nid); |
| 99 | err = -EFSCORRUPTED; |
| 100 | goto unmap_done; |
| 101 | } |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 102 | unmap_done: |
Gao Xiang | 09c5437 | 2022-01-02 12:00:17 +0800 | [diff] [blame^] | 103 | erofs_put_metabuf(&buf); |
Yue Hu | ab92184 | 2021-12-28 13:46:04 +0800 | [diff] [blame] | 104 | if (err) |
| 105 | goto out_unlock; |
| 106 | |
| 107 | if (vi->z_advise & Z_EROFS_ADVISE_INLINE_PCLUSTER) { |
Gao Xiang | 09c5437 | 2022-01-02 12:00:17 +0800 | [diff] [blame^] | 108 | struct erofs_map_blocks map = { |
| 109 | .buf = __EROFS_BUF_INITIALIZER |
| 110 | }; |
Yue Hu | ab92184 | 2021-12-28 13:46:04 +0800 | [diff] [blame] | 111 | |
| 112 | vi->z_idata_size = le16_to_cpu(h->h_idata_size); |
| 113 | err = z_erofs_do_map_blocks(inode, &map, |
| 114 | EROFS_GET_BLOCKS_FINDTAIL); |
Gao Xiang | 09c5437 | 2022-01-02 12:00:17 +0800 | [diff] [blame^] | 115 | erofs_put_metabuf(&map.buf); |
Yue Hu | ab92184 | 2021-12-28 13:46:04 +0800 | [diff] [blame] | 116 | |
| 117 | if (!map.m_plen || |
| 118 | erofs_blkoff(map.m_pa) + map.m_plen > EROFS_BLKSIZ) { |
| 119 | erofs_err(sb, "invalid tail-packing pclustersize %llu", |
| 120 | map.m_plen); |
| 121 | err = -EFSCORRUPTED; |
| 122 | } |
| 123 | if (err < 0) |
| 124 | goto out_unlock; |
| 125 | } |
| 126 | /* paired with smp_mb() at the beginning of the function */ |
| 127 | smp_mb(); |
| 128 | set_bit(EROFS_I_Z_INITED_BIT, &vi->flags); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 129 | out_unlock: |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 130 | clear_and_wake_up_bit(EROFS_I_BL_Z_BIT, &vi->flags); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 131 | return err; |
| 132 | } |
| 133 | |
| 134 | struct z_erofs_maprecorder { |
| 135 | struct inode *inode; |
| 136 | struct erofs_map_blocks *map; |
| 137 | void *kaddr; |
| 138 | |
| 139 | unsigned long lcn; |
| 140 | /* compression extent information gathered */ |
Gao Xiang | 8f89926 | 2021-10-09 04:08:37 +0800 | [diff] [blame] | 141 | u8 type, headtype; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 142 | u16 clusterofs; |
| 143 | u16 delta[2]; |
Gao Xiang | cec6e93 | 2021-04-07 12:39:24 +0800 | [diff] [blame] | 144 | erofs_blk_t pblk, compressedlcs; |
Yue Hu | ab92184 | 2021-12-28 13:46:04 +0800 | [diff] [blame] | 145 | erofs_off_t nextpackoff; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | static int z_erofs_reload_indexes(struct z_erofs_maprecorder *m, |
| 149 | erofs_blk_t eblk) |
| 150 | { |
| 151 | struct super_block *const sb = m->inode->i_sb; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 152 | |
Gao Xiang | 09c5437 | 2022-01-02 12:00:17 +0800 | [diff] [blame^] | 153 | m->kaddr = erofs_read_metabuf(&m->map->buf, sb, eblk, |
| 154 | EROFS_KMAP_ATOMIC); |
| 155 | if (IS_ERR(m->kaddr)) |
| 156 | return PTR_ERR(m->kaddr); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 160 | static int legacy_load_cluster_from_disk(struct z_erofs_maprecorder *m, |
| 161 | unsigned long lcn) |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 162 | { |
| 163 | struct inode *const inode = m->inode; |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 164 | struct erofs_inode *const vi = EROFS_I(inode); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 165 | const erofs_off_t ibase = iloc(EROFS_I_SB(inode), vi->nid); |
| 166 | const erofs_off_t pos = |
| 167 | Z_EROFS_VLE_LEGACY_INDEX_ALIGN(ibase + vi->inode_isize + |
| 168 | vi->xattr_isize) + |
| 169 | lcn * sizeof(struct z_erofs_vle_decompressed_index); |
| 170 | struct z_erofs_vle_decompressed_index *di; |
| 171 | unsigned int advise, type; |
| 172 | int err; |
| 173 | |
| 174 | err = z_erofs_reload_indexes(m, erofs_blknr(pos)); |
| 175 | if (err) |
| 176 | return err; |
| 177 | |
Yue Hu | ab92184 | 2021-12-28 13:46:04 +0800 | [diff] [blame] | 178 | m->nextpackoff = pos + sizeof(struct z_erofs_vle_decompressed_index); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 179 | m->lcn = lcn; |
| 180 | di = m->kaddr + erofs_blkoff(pos); |
| 181 | |
| 182 | advise = le16_to_cpu(di->di_advise); |
| 183 | type = (advise >> Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT) & |
| 184 | ((1 << Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) - 1); |
| 185 | switch (type) { |
| 186 | case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD: |
| 187 | m->clusterofs = 1 << vi->z_logical_clusterbits; |
| 188 | m->delta[0] = le16_to_cpu(di->di_u.delta[0]); |
Gao Xiang | cec6e93 | 2021-04-07 12:39:24 +0800 | [diff] [blame] | 189 | if (m->delta[0] & Z_EROFS_VLE_DI_D0_CBLKCNT) { |
Gao Xiang | 72bb526 | 2021-10-18 00:57:21 +0800 | [diff] [blame] | 190 | if (!(vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 | |
| 191 | Z_EROFS_ADVISE_BIG_PCLUSTER_2))) { |
Gao Xiang | cec6e93 | 2021-04-07 12:39:24 +0800 | [diff] [blame] | 192 | DBG_BUGON(1); |
| 193 | return -EFSCORRUPTED; |
| 194 | } |
| 195 | m->compressedlcs = m->delta[0] & |
| 196 | ~Z_EROFS_VLE_DI_D0_CBLKCNT; |
| 197 | m->delta[0] = 1; |
| 198 | } |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 199 | m->delta[1] = le16_to_cpu(di->di_u.delta[1]); |
| 200 | break; |
| 201 | case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN: |
Gao Xiang | 72bb526 | 2021-10-18 00:57:21 +0800 | [diff] [blame] | 202 | case Z_EROFS_VLE_CLUSTER_TYPE_HEAD1: |
| 203 | case Z_EROFS_VLE_CLUSTER_TYPE_HEAD2: |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 204 | m->clusterofs = le16_to_cpu(di->di_clusterofs); |
| 205 | m->pblk = le32_to_cpu(di->di_u.blkaddr); |
| 206 | break; |
| 207 | default: |
| 208 | DBG_BUGON(1); |
Gao Xiang | 382329a | 2019-08-14 18:37:04 +0800 | [diff] [blame] | 209 | return -EOPNOTSUPP; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 210 | } |
| 211 | m->type = type; |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static unsigned int decode_compactedbits(unsigned int lobits, |
| 216 | unsigned int lomask, |
| 217 | u8 *in, unsigned int pos, u8 *type) |
| 218 | { |
| 219 | const unsigned int v = get_unaligned_le32(in + pos / 8) >> (pos & 7); |
| 220 | const unsigned int lo = v & lomask; |
| 221 | |
| 222 | *type = (v >> lobits) & 3; |
| 223 | return lo; |
| 224 | } |
| 225 | |
Gao Xiang | d95ae5e | 2021-08-18 23:22:31 +0800 | [diff] [blame] | 226 | static int get_compacted_la_distance(unsigned int lclusterbits, |
| 227 | unsigned int encodebits, |
| 228 | unsigned int vcnt, u8 *in, int i) |
| 229 | { |
| 230 | const unsigned int lomask = (1 << lclusterbits) - 1; |
| 231 | unsigned int lo, d1 = 0; |
| 232 | u8 type; |
| 233 | |
| 234 | DBG_BUGON(i >= vcnt); |
| 235 | |
| 236 | do { |
| 237 | lo = decode_compactedbits(lclusterbits, lomask, |
| 238 | in, encodebits * i, &type); |
| 239 | |
| 240 | if (type != Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) |
| 241 | return d1; |
| 242 | ++d1; |
| 243 | } while (++i < vcnt); |
| 244 | |
| 245 | /* vcnt - 1 (Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) item */ |
| 246 | if (!(lo & Z_EROFS_VLE_DI_D0_CBLKCNT)) |
| 247 | d1 += lo - 1; |
| 248 | return d1; |
| 249 | } |
| 250 | |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 251 | static int unpack_compacted_index(struct z_erofs_maprecorder *m, |
| 252 | unsigned int amortizedshift, |
Yue Hu | ab92184 | 2021-12-28 13:46:04 +0800 | [diff] [blame] | 253 | erofs_off_t pos, bool lookahead) |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 254 | { |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 255 | struct erofs_inode *const vi = EROFS_I(m->inode); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 256 | const unsigned int lclusterbits = vi->z_logical_clusterbits; |
| 257 | const unsigned int lomask = (1 << lclusterbits) - 1; |
Yue Hu | ab92184 | 2021-12-28 13:46:04 +0800 | [diff] [blame] | 258 | unsigned int vcnt, base, lo, encodebits, nblk, eofs; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 259 | int i; |
| 260 | u8 *in, type; |
Gao Xiang | b86269f | 2021-04-07 12:39:25 +0800 | [diff] [blame] | 261 | bool big_pcluster; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 262 | |
| 263 | if (1 << amortizedshift == 4) |
| 264 | vcnt = 2; |
| 265 | else if (1 << amortizedshift == 2 && lclusterbits == 12) |
| 266 | vcnt = 16; |
| 267 | else |
Gao Xiang | ff784a7 | 2019-08-14 18:37:05 +0800 | [diff] [blame] | 268 | return -EOPNOTSUPP; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 269 | |
Yue Hu | ab92184 | 2021-12-28 13:46:04 +0800 | [diff] [blame] | 270 | /* it doesn't equal to round_up(..) */ |
| 271 | m->nextpackoff = round_down(pos, vcnt << amortizedshift) + |
| 272 | (vcnt << amortizedshift); |
Gao Xiang | b86269f | 2021-04-07 12:39:25 +0800 | [diff] [blame] | 273 | big_pcluster = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 274 | encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt; |
Yue Hu | ab92184 | 2021-12-28 13:46:04 +0800 | [diff] [blame] | 275 | eofs = erofs_blkoff(pos); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 276 | base = round_down(eofs, vcnt << amortizedshift); |
| 277 | in = m->kaddr + base; |
| 278 | |
| 279 | i = (eofs - base) >> amortizedshift; |
| 280 | |
| 281 | lo = decode_compactedbits(lclusterbits, lomask, |
| 282 | in, encodebits * i, &type); |
| 283 | m->type = type; |
| 284 | if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) { |
| 285 | m->clusterofs = 1 << lclusterbits; |
Gao Xiang | d95ae5e | 2021-08-18 23:22:31 +0800 | [diff] [blame] | 286 | |
| 287 | /* figure out lookahead_distance: delta[1] if needed */ |
| 288 | if (lookahead) |
| 289 | m->delta[1] = get_compacted_la_distance(lclusterbits, |
| 290 | encodebits, vcnt, in, i); |
Gao Xiang | b86269f | 2021-04-07 12:39:25 +0800 | [diff] [blame] | 291 | if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT) { |
| 292 | if (!big_pcluster) { |
| 293 | DBG_BUGON(1); |
| 294 | return -EFSCORRUPTED; |
| 295 | } |
| 296 | m->compressedlcs = lo & ~Z_EROFS_VLE_DI_D0_CBLKCNT; |
| 297 | m->delta[0] = 1; |
| 298 | return 0; |
| 299 | } else if (i + 1 != (int)vcnt) { |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 300 | m->delta[0] = lo; |
| 301 | return 0; |
| 302 | } |
| 303 | /* |
| 304 | * since the last lcluster in the pack is special, |
| 305 | * of which lo saves delta[1] rather than delta[0]. |
| 306 | * Hence, get delta[0] by the previous lcluster indirectly. |
| 307 | */ |
| 308 | lo = decode_compactedbits(lclusterbits, lomask, |
| 309 | in, encodebits * (i - 1), &type); |
| 310 | if (type != Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) |
| 311 | lo = 0; |
Gao Xiang | b86269f | 2021-04-07 12:39:25 +0800 | [diff] [blame] | 312 | else if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT) |
| 313 | lo = 1; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 314 | m->delta[0] = lo + 1; |
| 315 | return 0; |
| 316 | } |
| 317 | m->clusterofs = lo; |
| 318 | m->delta[0] = 0; |
| 319 | /* figout out blkaddr (pblk) for HEAD lclusters */ |
Gao Xiang | b86269f | 2021-04-07 12:39:25 +0800 | [diff] [blame] | 320 | if (!big_pcluster) { |
| 321 | nblk = 1; |
| 322 | while (i > 0) { |
| 323 | --i; |
| 324 | lo = decode_compactedbits(lclusterbits, lomask, |
| 325 | in, encodebits * i, &type); |
| 326 | if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) |
| 327 | i -= lo; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 328 | |
Gao Xiang | b86269f | 2021-04-07 12:39:25 +0800 | [diff] [blame] | 329 | if (i >= 0) |
| 330 | ++nblk; |
| 331 | } |
| 332 | } else { |
| 333 | nblk = 0; |
| 334 | while (i > 0) { |
| 335 | --i; |
| 336 | lo = decode_compactedbits(lclusterbits, lomask, |
| 337 | in, encodebits * i, &type); |
| 338 | if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) { |
| 339 | if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT) { |
| 340 | --i; |
| 341 | nblk += lo & ~Z_EROFS_VLE_DI_D0_CBLKCNT; |
| 342 | continue; |
| 343 | } |
| 344 | /* bigpcluster shouldn't have plain d0 == 1 */ |
| 345 | if (lo <= 1) { |
| 346 | DBG_BUGON(1); |
| 347 | return -EFSCORRUPTED; |
| 348 | } |
| 349 | i -= lo - 2; |
| 350 | continue; |
| 351 | } |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 352 | ++nblk; |
Gao Xiang | b86269f | 2021-04-07 12:39:25 +0800 | [diff] [blame] | 353 | } |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 354 | } |
| 355 | in += (vcnt << amortizedshift) - sizeof(__le32); |
| 356 | m->pblk = le32_to_cpu(*(__le32 *)in) + nblk; |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | static int compacted_load_cluster_from_disk(struct z_erofs_maprecorder *m, |
Gao Xiang | d95ae5e | 2021-08-18 23:22:31 +0800 | [diff] [blame] | 361 | unsigned long lcn, bool lookahead) |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 362 | { |
| 363 | struct inode *const inode = m->inode; |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 364 | struct erofs_inode *const vi = EROFS_I(inode); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 365 | const unsigned int lclusterbits = vi->z_logical_clusterbits; |
| 366 | const erofs_off_t ebase = ALIGN(iloc(EROFS_I_SB(inode), vi->nid) + |
| 367 | vi->inode_isize + vi->xattr_isize, 8) + |
| 368 | sizeof(struct z_erofs_map_header); |
| 369 | const unsigned int totalidx = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ); |
| 370 | unsigned int compacted_4b_initial, compacted_2b; |
| 371 | unsigned int amortizedshift; |
| 372 | erofs_off_t pos; |
| 373 | int err; |
| 374 | |
| 375 | if (lclusterbits != 12) |
Gao Xiang | ff784a7 | 2019-08-14 18:37:05 +0800 | [diff] [blame] | 376 | return -EOPNOTSUPP; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 377 | |
| 378 | if (lcn >= totalidx) |
| 379 | return -EINVAL; |
| 380 | |
| 381 | m->lcn = lcn; |
| 382 | /* used to align to 32-byte (compacted_2b) alignment */ |
| 383 | compacted_4b_initial = (32 - ebase % 32) / 4; |
| 384 | if (compacted_4b_initial == 32 / 4) |
| 385 | compacted_4b_initial = 0; |
| 386 | |
Yue Hu | c40dd3c | 2021-09-14 11:59:15 +0800 | [diff] [blame] | 387 | if ((vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B) && |
| 388 | compacted_4b_initial < totalidx) |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 389 | compacted_2b = rounddown(totalidx - compacted_4b_initial, 16); |
| 390 | else |
| 391 | compacted_2b = 0; |
| 392 | |
| 393 | pos = ebase; |
| 394 | if (lcn < compacted_4b_initial) { |
| 395 | amortizedshift = 2; |
| 396 | goto out; |
| 397 | } |
| 398 | pos += compacted_4b_initial * 4; |
| 399 | lcn -= compacted_4b_initial; |
| 400 | |
| 401 | if (lcn < compacted_2b) { |
| 402 | amortizedshift = 1; |
| 403 | goto out; |
| 404 | } |
| 405 | pos += compacted_2b * 2; |
| 406 | lcn -= compacted_2b; |
| 407 | amortizedshift = 2; |
| 408 | out: |
| 409 | pos += lcn * (1 << amortizedshift); |
| 410 | err = z_erofs_reload_indexes(m, erofs_blknr(pos)); |
| 411 | if (err) |
| 412 | return err; |
Yue Hu | ab92184 | 2021-12-28 13:46:04 +0800 | [diff] [blame] | 413 | return unpack_compacted_index(m, amortizedshift, pos, lookahead); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 414 | } |
| 415 | |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 416 | static int z_erofs_load_cluster_from_disk(struct z_erofs_maprecorder *m, |
Gao Xiang | d95ae5e | 2021-08-18 23:22:31 +0800 | [diff] [blame] | 417 | unsigned int lcn, bool lookahead) |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 418 | { |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 419 | const unsigned int datamode = EROFS_I(m->inode)->datalayout; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 420 | |
| 421 | if (datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY) |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 422 | return legacy_load_cluster_from_disk(m, lcn); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 423 | |
| 424 | if (datamode == EROFS_INODE_FLAT_COMPRESSION) |
Gao Xiang | d95ae5e | 2021-08-18 23:22:31 +0800 | [diff] [blame] | 425 | return compacted_load_cluster_from_disk(m, lcn, lookahead); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 426 | |
| 427 | return -EINVAL; |
| 428 | } |
| 429 | |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 430 | static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m, |
| 431 | unsigned int lookback_distance) |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 432 | { |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 433 | struct erofs_inode *const vi = EROFS_I(m->inode); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 434 | struct erofs_map_blocks *const map = m->map; |
| 435 | const unsigned int lclusterbits = vi->z_logical_clusterbits; |
| 436 | unsigned long lcn = m->lcn; |
| 437 | int err; |
| 438 | |
| 439 | if (lcn < lookback_distance) { |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 440 | erofs_err(m->inode->i_sb, |
| 441 | "bogus lookback distance @ nid %llu", vi->nid); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 442 | DBG_BUGON(1); |
Gao Xiang | a6b9b1d | 2019-08-14 18:37:03 +0800 | [diff] [blame] | 443 | return -EFSCORRUPTED; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* load extent head logical cluster if needed */ |
| 447 | lcn -= lookback_distance; |
Gao Xiang | d95ae5e | 2021-08-18 23:22:31 +0800 | [diff] [blame] | 448 | err = z_erofs_load_cluster_from_disk(m, lcn, false); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 449 | if (err) |
| 450 | return err; |
| 451 | |
| 452 | switch (m->type) { |
| 453 | case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD: |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 454 | if (!m->delta[0]) { |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 455 | erofs_err(m->inode->i_sb, |
| 456 | "invalid lookback distance 0 @ nid %llu", |
| 457 | vi->nid); |
Gao Xiang | 598bb89 | 2019-08-19 18:34:26 +0800 | [diff] [blame] | 458 | DBG_BUGON(1); |
| 459 | return -EFSCORRUPTED; |
| 460 | } |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 461 | return z_erofs_extent_lookback(m, m->delta[0]); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 462 | case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN: |
Gao Xiang | 72bb526 | 2021-10-18 00:57:21 +0800 | [diff] [blame] | 463 | case Z_EROFS_VLE_CLUSTER_TYPE_HEAD1: |
| 464 | case Z_EROFS_VLE_CLUSTER_TYPE_HEAD2: |
Gao Xiang | 8f89926 | 2021-10-09 04:08:37 +0800 | [diff] [blame] | 465 | m->headtype = m->type; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 466 | map->m_la = (lcn << lclusterbits) | m->clusterofs; |
| 467 | break; |
| 468 | default: |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 469 | erofs_err(m->inode->i_sb, |
| 470 | "unknown type %u @ lcn %lu of nid %llu", |
| 471 | m->type, lcn, vi->nid); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 472 | DBG_BUGON(1); |
Gao Xiang | 382329a | 2019-08-14 18:37:04 +0800 | [diff] [blame] | 473 | return -EOPNOTSUPP; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 474 | } |
| 475 | return 0; |
| 476 | } |
| 477 | |
Gao Xiang | cec6e93 | 2021-04-07 12:39:24 +0800 | [diff] [blame] | 478 | static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m, |
| 479 | unsigned int initial_lcn) |
| 480 | { |
| 481 | struct erofs_inode *const vi = EROFS_I(m->inode); |
| 482 | struct erofs_map_blocks *const map = m->map; |
| 483 | const unsigned int lclusterbits = vi->z_logical_clusterbits; |
| 484 | unsigned long lcn; |
| 485 | int err; |
| 486 | |
| 487 | DBG_BUGON(m->type != Z_EROFS_VLE_CLUSTER_TYPE_PLAIN && |
Gao Xiang | 72bb526 | 2021-10-18 00:57:21 +0800 | [diff] [blame] | 488 | m->type != Z_EROFS_VLE_CLUSTER_TYPE_HEAD1 && |
| 489 | m->type != Z_EROFS_VLE_CLUSTER_TYPE_HEAD2); |
| 490 | DBG_BUGON(m->type != m->headtype); |
| 491 | |
Gao Xiang | 8f89926 | 2021-10-09 04:08:37 +0800 | [diff] [blame] | 492 | if (m->headtype == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN || |
Gao Xiang | 72bb526 | 2021-10-18 00:57:21 +0800 | [diff] [blame] | 493 | ((m->headtype == Z_EROFS_VLE_CLUSTER_TYPE_HEAD1) && |
| 494 | !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1)) || |
| 495 | ((m->headtype == Z_EROFS_VLE_CLUSTER_TYPE_HEAD2) && |
| 496 | !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2))) { |
Gao Xiang | cec6e93 | 2021-04-07 12:39:24 +0800 | [diff] [blame] | 497 | map->m_plen = 1 << lclusterbits; |
| 498 | return 0; |
| 499 | } |
Gao Xiang | cec6e93 | 2021-04-07 12:39:24 +0800 | [diff] [blame] | 500 | lcn = m->lcn + 1; |
| 501 | if (m->compressedlcs) |
| 502 | goto out; |
Gao Xiang | cec6e93 | 2021-04-07 12:39:24 +0800 | [diff] [blame] | 503 | |
Gao Xiang | d95ae5e | 2021-08-18 23:22:31 +0800 | [diff] [blame] | 504 | err = z_erofs_load_cluster_from_disk(m, lcn, false); |
Gao Xiang | cec6e93 | 2021-04-07 12:39:24 +0800 | [diff] [blame] | 505 | if (err) |
| 506 | return err; |
| 507 | |
Gao Xiang | 0852b6c | 2021-05-10 14:47:15 +0800 | [diff] [blame] | 508 | /* |
| 509 | * If the 1st NONHEAD lcluster has already been handled initially w/o |
| 510 | * valid compressedlcs, which means at least it mustn't be CBLKCNT, or |
| 511 | * an internal implemenatation error is detected. |
| 512 | * |
| 513 | * The following code can also handle it properly anyway, but let's |
| 514 | * BUG_ON in the debugging mode only for developers to notice that. |
| 515 | */ |
| 516 | DBG_BUGON(lcn == initial_lcn && |
| 517 | m->type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD); |
| 518 | |
Gao Xiang | cec6e93 | 2021-04-07 12:39:24 +0800 | [diff] [blame] | 519 | switch (m->type) { |
Gao Xiang | 0852b6c | 2021-05-10 14:47:15 +0800 | [diff] [blame] | 520 | case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN: |
Gao Xiang | 72bb526 | 2021-10-18 00:57:21 +0800 | [diff] [blame] | 521 | case Z_EROFS_VLE_CLUSTER_TYPE_HEAD1: |
| 522 | case Z_EROFS_VLE_CLUSTER_TYPE_HEAD2: |
Gao Xiang | 0852b6c | 2021-05-10 14:47:15 +0800 | [diff] [blame] | 523 | /* |
| 524 | * if the 1st NONHEAD lcluster is actually PLAIN or HEAD type |
| 525 | * rather than CBLKCNT, it's a 1 lcluster-sized pcluster. |
| 526 | */ |
| 527 | m->compressedlcs = 1; |
| 528 | break; |
Gao Xiang | cec6e93 | 2021-04-07 12:39:24 +0800 | [diff] [blame] | 529 | case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD: |
| 530 | if (m->delta[0] != 1) |
| 531 | goto err_bonus_cblkcnt; |
| 532 | if (m->compressedlcs) |
| 533 | break; |
| 534 | fallthrough; |
| 535 | default: |
| 536 | erofs_err(m->inode->i_sb, |
| 537 | "cannot found CBLKCNT @ lcn %lu of nid %llu", |
| 538 | lcn, vi->nid); |
| 539 | DBG_BUGON(1); |
| 540 | return -EFSCORRUPTED; |
| 541 | } |
| 542 | out: |
| 543 | map->m_plen = m->compressedlcs << lclusterbits; |
| 544 | return 0; |
| 545 | err_bonus_cblkcnt: |
| 546 | erofs_err(m->inode->i_sb, |
| 547 | "bogus CBLKCNT @ lcn %lu of nid %llu", |
| 548 | lcn, vi->nid); |
| 549 | DBG_BUGON(1); |
| 550 | return -EFSCORRUPTED; |
| 551 | } |
| 552 | |
Gao Xiang | d95ae5e | 2021-08-18 23:22:31 +0800 | [diff] [blame] | 553 | static int z_erofs_get_extent_decompressedlen(struct z_erofs_maprecorder *m) |
| 554 | { |
| 555 | struct inode *inode = m->inode; |
| 556 | struct erofs_inode *vi = EROFS_I(inode); |
| 557 | struct erofs_map_blocks *map = m->map; |
| 558 | unsigned int lclusterbits = vi->z_logical_clusterbits; |
| 559 | u64 lcn = m->lcn, headlcn = map->m_la >> lclusterbits; |
| 560 | int err; |
| 561 | |
| 562 | do { |
| 563 | /* handle the last EOF pcluster (no next HEAD lcluster) */ |
| 564 | if ((lcn << lclusterbits) >= inode->i_size) { |
| 565 | map->m_llen = inode->i_size - map->m_la; |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | err = z_erofs_load_cluster_from_disk(m, lcn, true); |
| 570 | if (err) |
| 571 | return err; |
| 572 | |
| 573 | if (m->type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) { |
| 574 | DBG_BUGON(!m->delta[1] && |
| 575 | m->clusterofs != 1 << lclusterbits); |
| 576 | } else if (m->type == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN || |
Gao Xiang | 72bb526 | 2021-10-18 00:57:21 +0800 | [diff] [blame] | 577 | m->type == Z_EROFS_VLE_CLUSTER_TYPE_HEAD1 || |
| 578 | m->type == Z_EROFS_VLE_CLUSTER_TYPE_HEAD2) { |
Gao Xiang | d95ae5e | 2021-08-18 23:22:31 +0800 | [diff] [blame] | 579 | /* go on until the next HEAD lcluster */ |
| 580 | if (lcn != headlcn) |
| 581 | break; |
| 582 | m->delta[1] = 1; |
| 583 | } else { |
| 584 | erofs_err(inode->i_sb, "unknown type %u @ lcn %llu of nid %llu", |
| 585 | m->type, lcn, vi->nid); |
| 586 | DBG_BUGON(1); |
| 587 | return -EOPNOTSUPP; |
| 588 | } |
| 589 | lcn += m->delta[1]; |
| 590 | } while (m->delta[1]); |
| 591 | |
| 592 | map->m_llen = (lcn << lclusterbits) + m->clusterofs - map->m_la; |
| 593 | return 0; |
| 594 | } |
| 595 | |
Yue Hu | ab92184 | 2021-12-28 13:46:04 +0800 | [diff] [blame] | 596 | static int z_erofs_do_map_blocks(struct inode *inode, |
| 597 | struct erofs_map_blocks *map, |
| 598 | int flags) |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 599 | { |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 600 | struct erofs_inode *const vi = EROFS_I(inode); |
Yue Hu | ab92184 | 2021-12-28 13:46:04 +0800 | [diff] [blame] | 601 | bool ztailpacking = vi->z_advise & Z_EROFS_ADVISE_INLINE_PCLUSTER; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 602 | struct z_erofs_maprecorder m = { |
| 603 | .inode = inode, |
| 604 | .map = map, |
| 605 | }; |
| 606 | int err = 0; |
| 607 | unsigned int lclusterbits, endoff; |
Gao Xiang | cec6e93 | 2021-04-07 12:39:24 +0800 | [diff] [blame] | 608 | unsigned long initial_lcn; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 609 | unsigned long long ofs, end; |
| 610 | |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 611 | lclusterbits = vi->z_logical_clusterbits; |
Yue Hu | ab92184 | 2021-12-28 13:46:04 +0800 | [diff] [blame] | 612 | ofs = flags & EROFS_GET_BLOCKS_FINDTAIL ? inode->i_size - 1 : map->m_la; |
Gao Xiang | cec6e93 | 2021-04-07 12:39:24 +0800 | [diff] [blame] | 613 | initial_lcn = ofs >> lclusterbits; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 614 | endoff = ofs & ((1 << lclusterbits) - 1); |
| 615 | |
Gao Xiang | d95ae5e | 2021-08-18 23:22:31 +0800 | [diff] [blame] | 616 | err = z_erofs_load_cluster_from_disk(&m, initial_lcn, false); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 617 | if (err) |
| 618 | goto unmap_out; |
| 619 | |
Yue Hu | ab92184 | 2021-12-28 13:46:04 +0800 | [diff] [blame] | 620 | if (ztailpacking && (flags & EROFS_GET_BLOCKS_FINDTAIL)) |
| 621 | vi->z_idataoff = m.nextpackoff; |
| 622 | |
Gao Xiang | 8f89926 | 2021-10-09 04:08:37 +0800 | [diff] [blame] | 623 | map->m_flags = EROFS_MAP_MAPPED | EROFS_MAP_ENCODED; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 624 | end = (m.lcn + 1ULL) << lclusterbits; |
| 625 | |
| 626 | switch (m.type) { |
| 627 | case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN: |
Gao Xiang | 72bb526 | 2021-10-18 00:57:21 +0800 | [diff] [blame] | 628 | case Z_EROFS_VLE_CLUSTER_TYPE_HEAD1: |
| 629 | case Z_EROFS_VLE_CLUSTER_TYPE_HEAD2: |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 630 | if (endoff >= m.clusterofs) { |
Gao Xiang | 8f89926 | 2021-10-09 04:08:37 +0800 | [diff] [blame] | 631 | m.headtype = m.type; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 632 | map->m_la = (m.lcn << lclusterbits) | m.clusterofs; |
| 633 | break; |
| 634 | } |
| 635 | /* m.lcn should be >= 1 if endoff < m.clusterofs */ |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 636 | if (!m.lcn) { |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 637 | erofs_err(inode->i_sb, |
| 638 | "invalid logical cluster 0 at nid %llu", |
| 639 | vi->nid); |
Gao Xiang | a6b9b1d | 2019-08-14 18:37:03 +0800 | [diff] [blame] | 640 | err = -EFSCORRUPTED; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 641 | goto unmap_out; |
| 642 | } |
| 643 | end = (m.lcn << lclusterbits) | m.clusterofs; |
Gao Xiang | b6a7618 | 2019-06-24 15:22:58 +0800 | [diff] [blame] | 644 | map->m_flags |= EROFS_MAP_FULL_MAPPED; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 645 | m.delta[0] = 1; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 646 | fallthrough; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 647 | case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD: |
Ruiqi Gong | fe6adcc | 2021-03-31 05:39:20 -0400 | [diff] [blame] | 648 | /* get the corresponding first chunk */ |
Gao Xiang | 0c638f7 | 2019-11-08 11:37:33 +0800 | [diff] [blame] | 649 | err = z_erofs_extent_lookback(&m, m.delta[0]); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 650 | if (err) |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 651 | goto unmap_out; |
| 652 | break; |
| 653 | default: |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 654 | erofs_err(inode->i_sb, |
| 655 | "unknown type %u @ offset %llu of nid %llu", |
| 656 | m.type, ofs, vi->nid); |
Gao Xiang | 382329a | 2019-08-14 18:37:04 +0800 | [diff] [blame] | 657 | err = -EOPNOTSUPP; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 658 | goto unmap_out; |
| 659 | } |
| 660 | |
| 661 | map->m_llen = end - map->m_la; |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 662 | |
Yue Hu | ab92184 | 2021-12-28 13:46:04 +0800 | [diff] [blame] | 663 | if (flags & EROFS_GET_BLOCKS_FINDTAIL) |
| 664 | vi->z_tailextent_headlcn = m.lcn; |
| 665 | if (ztailpacking && m.lcn == vi->z_tailextent_headlcn) { |
| 666 | map->m_flags |= EROFS_MAP_META; |
| 667 | map->m_pa = vi->z_idataoff; |
| 668 | map->m_plen = vi->z_idata_size; |
| 669 | } else { |
| 670 | map->m_pa = blknr_to_addr(m.pblk); |
| 671 | err = z_erofs_get_extent_compressedlen(&m, initial_lcn); |
| 672 | if (err) |
| 673 | goto out; |
| 674 | } |
Gao Xiang | d95ae5e | 2021-08-18 23:22:31 +0800 | [diff] [blame] | 675 | |
Gao Xiang | 8f89926 | 2021-10-09 04:08:37 +0800 | [diff] [blame] | 676 | if (m.headtype == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN) |
| 677 | map->m_algorithmformat = Z_EROFS_COMPRESSION_SHIFTED; |
Gao Xiang | 72bb526 | 2021-10-18 00:57:21 +0800 | [diff] [blame] | 678 | else if (m.headtype == Z_EROFS_VLE_CLUSTER_TYPE_HEAD2) |
| 679 | map->m_algorithmformat = vi->z_algorithmtype[1]; |
Gao Xiang | 8f89926 | 2021-10-09 04:08:37 +0800 | [diff] [blame] | 680 | else |
| 681 | map->m_algorithmformat = vi->z_algorithmtype[0]; |
| 682 | |
Gao Xiang | 622cead | 2021-10-11 05:31:45 +0800 | [diff] [blame] | 683 | if ((flags & EROFS_GET_BLOCKS_FIEMAP) || |
| 684 | ((flags & EROFS_GET_BLOCKS_READMORE) && |
| 685 | map->m_algorithmformat == Z_EROFS_COMPRESSION_LZMA && |
| 686 | map->m_llen >= EROFS_BLKSIZ)) { |
Gao Xiang | d95ae5e | 2021-08-18 23:22:31 +0800 | [diff] [blame] | 687 | err = z_erofs_get_extent_decompressedlen(&m); |
| 688 | if (!err) |
| 689 | map->m_flags |= EROFS_MAP_FULL_MAPPED; |
| 690 | } |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 691 | unmap_out: |
Gao Xiang | 09c5437 | 2022-01-02 12:00:17 +0800 | [diff] [blame^] | 692 | erofs_unmap_metabuf(&m.map->buf); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 693 | |
| 694 | out: |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 695 | erofs_dbg("%s, m_la %llu m_pa %llu m_llen %llu m_plen %llu m_flags 0%o", |
| 696 | __func__, map->m_la, map->m_pa, |
| 697 | map->m_llen, map->m_plen, map->m_flags); |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 698 | |
Yue Hu | ab92184 | 2021-12-28 13:46:04 +0800 | [diff] [blame] | 699 | return err; |
| 700 | } |
| 701 | |
| 702 | int z_erofs_map_blocks_iter(struct inode *inode, |
| 703 | struct erofs_map_blocks *map, |
| 704 | int flags) |
| 705 | { |
| 706 | int err = 0; |
| 707 | |
| 708 | trace_z_erofs_map_blocks_iter_enter(inode, map, flags); |
| 709 | |
| 710 | /* when trying to read beyond EOF, leave it unmapped */ |
| 711 | if (map->m_la >= inode->i_size) { |
| 712 | map->m_llen = map->m_la + 1 - inode->i_size; |
| 713 | map->m_la = inode->i_size; |
| 714 | map->m_flags = 0; |
| 715 | goto out; |
| 716 | } |
| 717 | |
| 718 | err = z_erofs_fill_inode_lazy(inode); |
| 719 | if (err) |
| 720 | goto out; |
| 721 | |
| 722 | err = z_erofs_do_map_blocks(inode, map, flags); |
| 723 | out: |
Gao Xiang | 152a333 | 2019-06-24 15:22:52 +0800 | [diff] [blame] | 724 | trace_z_erofs_map_blocks_iter_exit(inode, map, flags, err); |
| 725 | |
| 726 | /* aggressively BUG_ON iff CONFIG_EROFS_FS_DEBUG is on */ |
| 727 | DBG_BUGON(err < 0 && err != -ENOMEM); |
| 728 | return err; |
| 729 | } |
Gao Xiang | eadcd6b | 2021-08-13 13:29:31 +0800 | [diff] [blame] | 730 | |
| 731 | static int z_erofs_iomap_begin_report(struct inode *inode, loff_t offset, |
| 732 | loff_t length, unsigned int flags, |
| 733 | struct iomap *iomap, struct iomap *srcmap) |
| 734 | { |
| 735 | int ret; |
| 736 | struct erofs_map_blocks map = { .m_la = offset }; |
| 737 | |
| 738 | ret = z_erofs_map_blocks_iter(inode, &map, EROFS_GET_BLOCKS_FIEMAP); |
Gao Xiang | 09c5437 | 2022-01-02 12:00:17 +0800 | [diff] [blame^] | 739 | erofs_put_metabuf(&map.buf); |
Gao Xiang | eadcd6b | 2021-08-13 13:29:31 +0800 | [diff] [blame] | 740 | if (ret < 0) |
| 741 | return ret; |
| 742 | |
| 743 | iomap->bdev = inode->i_sb->s_bdev; |
| 744 | iomap->offset = map.m_la; |
| 745 | iomap->length = map.m_llen; |
| 746 | if (map.m_flags & EROFS_MAP_MAPPED) { |
| 747 | iomap->type = IOMAP_MAPPED; |
| 748 | iomap->addr = map.m_pa; |
| 749 | } else { |
| 750 | iomap->type = IOMAP_HOLE; |
| 751 | iomap->addr = IOMAP_NULL_ADDR; |
| 752 | /* |
| 753 | * No strict rule how to describe extents for post EOF, yet |
| 754 | * we need do like below. Otherwise, iomap itself will get |
| 755 | * into an endless loop on post EOF. |
| 756 | */ |
| 757 | if (iomap->offset >= inode->i_size) |
| 758 | iomap->length = length + map.m_la - offset; |
| 759 | } |
| 760 | iomap->flags = 0; |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | const struct iomap_ops z_erofs_iomap_report_ops = { |
| 765 | .iomap_begin = z_erofs_iomap_begin_report, |
| 766 | }; |