blob: 18d7fd1a50646039c29b6769cea3bbfd4a9d3766 [file] [log] [blame]
Gao Xiang29b24f62019-07-31 23:57:31 +08001// SPDX-License-Identifier: GPL-2.0-only
Gao Xiang152a3332019-06-24 15:22:52 +08002/*
Gao Xiang152a3332019-06-24 15:22:52 +08003 * Copyright (C) 2018-2019 HUAWEI, Inc.
Alexander A. Klimov592e7cd2020-07-13 15:09:44 +02004 * https://www.huawei.com/
Gao Xiang152a3332019-06-24 15:22:52 +08005 */
6#include "internal.h"
7#include <asm/unaligned.h>
8#include <trace/events/erofs.h>
9
Yue Huab921842021-12-28 13:46:04 +080010static int z_erofs_do_map_blocks(struct inode *inode,
11 struct erofs_map_blocks *map,
12 int flags);
13
Gao Xiang152a3332019-06-24 15:22:52 +080014int z_erofs_fill_inode(struct inode *inode)
15{
Gao Xianga5876e22019-09-04 10:08:56 +080016 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiangcec6e932021-04-07 12:39:24 +080017 struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
Gao Xiang152a3332019-06-24 15:22:52 +080018
Gao Xiangcec6e932021-04-07 12:39:24 +080019 if (!erofs_sb_has_big_pcluster(sbi) &&
Yue Huab921842021-12-28 13:46:04 +080020 !erofs_sb_has_ztailpacking(sbi) &&
Gao Xiangcec6e932021-04-07 12:39:24 +080021 vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY) {
Gao Xiang152a3332019-06-24 15:22:52 +080022 vi->z_advise = 0;
23 vi->z_algorithmtype[0] = 0;
24 vi->z_algorithmtype[1] = 0;
Gao Xiangeace9942019-07-31 23:57:48 +080025 vi->z_logical_clusterbits = LOG_BLOCK_SIZE;
Gao Xianga5876e22019-09-04 10:08:56 +080026 set_bit(EROFS_I_Z_INITED_BIT, &vi->flags);
Gao Xiang152a3332019-06-24 15:22:52 +080027 }
Gao Xiang0c638f72019-11-08 11:37:33 +080028 inode->i_mapping->a_ops = &z_erofs_aops;
Gao Xiang152a3332019-06-24 15:22:52 +080029 return 0;
30}
31
Gao Xiang0c638f72019-11-08 11:37:33 +080032static int z_erofs_fill_inode_lazy(struct inode *inode)
Gao Xiang152a3332019-06-24 15:22:52 +080033{
Gao Xianga5876e22019-09-04 10:08:56 +080034 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiang152a3332019-06-24 15:22:52 +080035 struct super_block *const sb = inode->i_sb;
Gao Xiang72bb5262021-10-18 00:57:21 +080036 int err, headnr;
Gao Xiang152a3332019-06-24 15:22:52 +080037 erofs_off_t pos;
Gao Xiang09c54372022-01-02 12:00:17 +080038 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
Gao Xiang152a3332019-06-24 15:22:52 +080039 void *kaddr;
40 struct z_erofs_map_header *h;
41
Gao Xiangce0631292021-02-09 21:06:18 +080042 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 Xiang152a3332019-06-24 15:22:52 +080048 return 0;
Gao Xiangce0631292021-02-09 21:06:18 +080049 }
Gao Xiang152a3332019-06-24 15:22:52 +080050
Gao Xianga5876e22019-09-04 10:08:56 +080051 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_Z_BIT, TASK_KILLABLE))
Gao Xiang152a3332019-06-24 15:22:52 +080052 return -ERESTARTSYS;
53
54 err = 0;
Gao Xianga5876e22019-09-04 10:08:56 +080055 if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags))
Gao Xiang152a3332019-06-24 15:22:52 +080056 goto out_unlock;
57
Gao Xiangcec6e932021-04-07 12:39:24 +080058 DBG_BUGON(!erofs_sb_has_big_pcluster(EROFS_SB(sb)) &&
Yue Huab921842021-12-28 13:46:04 +080059 !erofs_sb_has_ztailpacking(EROFS_SB(sb)) &&
Gao Xiangcec6e932021-04-07 12:39:24 +080060 vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY);
Gao Xiang152a3332019-06-24 15:22:52 +080061
62 pos = ALIGN(iloc(EROFS_SB(sb), vi->nid) + vi->inode_isize +
63 vi->xattr_isize, 8);
Gao Xiang09c54372022-01-02 12:00:17 +080064 kaddr = erofs_read_metabuf(&buf, sb, erofs_blknr(pos),
65 EROFS_KMAP_ATOMIC);
66 if (IS_ERR(kaddr)) {
67 err = PTR_ERR(kaddr);
Gao Xiang152a3332019-06-24 15:22:52 +080068 goto out_unlock;
69 }
70
Gao Xiang152a3332019-06-24 15:22:52 +080071 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 Xiang72bb5262021-10-18 00:57:21 +080076 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 Xiangff784a72019-08-14 18:37:05 +080081 err = -EOPNOTSUPP;
Gao Xiang152a3332019-06-24 15:22:52 +080082 goto unmap_done;
83 }
84
85 vi->z_logical_clusterbits = LOG_BLOCK_SIZE + (h->h_clusterbits & 7);
Gao Xiangb86269f2021-04-07 12:39:25 +080086 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 Xiang152a3332019-06-24 15:22:52 +0800102unmap_done:
Gao Xiang09c54372022-01-02 12:00:17 +0800103 erofs_put_metabuf(&buf);
Yue Huab921842021-12-28 13:46:04 +0800104 if (err)
105 goto out_unlock;
106
107 if (vi->z_advise & Z_EROFS_ADVISE_INLINE_PCLUSTER) {
Gao Xiang09c54372022-01-02 12:00:17 +0800108 struct erofs_map_blocks map = {
109 .buf = __EROFS_BUF_INITIALIZER
110 };
Yue Huab921842021-12-28 13:46:04 +0800111
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 Xiang09c54372022-01-02 12:00:17 +0800115 erofs_put_metabuf(&map.buf);
Yue Huab921842021-12-28 13:46:04 +0800116
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 Xiang152a3332019-06-24 15:22:52 +0800129out_unlock:
Gao Xianga5876e22019-09-04 10:08:56 +0800130 clear_and_wake_up_bit(EROFS_I_BL_Z_BIT, &vi->flags);
Gao Xiang152a3332019-06-24 15:22:52 +0800131 return err;
132}
133
134struct 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 Xiang8f899262021-10-09 04:08:37 +0800141 u8 type, headtype;
Gao Xiang152a3332019-06-24 15:22:52 +0800142 u16 clusterofs;
143 u16 delta[2];
Gao Xiangcec6e932021-04-07 12:39:24 +0800144 erofs_blk_t pblk, compressedlcs;
Yue Huab921842021-12-28 13:46:04 +0800145 erofs_off_t nextpackoff;
Gao Xiang152a3332019-06-24 15:22:52 +0800146};
147
148static 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 Xiang152a3332019-06-24 15:22:52 +0800152
Gao Xiang09c54372022-01-02 12:00:17 +0800153 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 Xiang152a3332019-06-24 15:22:52 +0800157 return 0;
158}
159
Gao Xiang0c638f72019-11-08 11:37:33 +0800160static int legacy_load_cluster_from_disk(struct z_erofs_maprecorder *m,
161 unsigned long lcn)
Gao Xiang152a3332019-06-24 15:22:52 +0800162{
163 struct inode *const inode = m->inode;
Gao Xianga5876e22019-09-04 10:08:56 +0800164 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiang152a3332019-06-24 15:22:52 +0800165 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 Huab921842021-12-28 13:46:04 +0800178 m->nextpackoff = pos + sizeof(struct z_erofs_vle_decompressed_index);
Gao Xiang152a3332019-06-24 15:22:52 +0800179 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 Xiangcec6e932021-04-07 12:39:24 +0800189 if (m->delta[0] & Z_EROFS_VLE_DI_D0_CBLKCNT) {
Gao Xiang72bb5262021-10-18 00:57:21 +0800190 if (!(vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 |
191 Z_EROFS_ADVISE_BIG_PCLUSTER_2))) {
Gao Xiangcec6e932021-04-07 12:39:24 +0800192 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 Xiang152a3332019-06-24 15:22:52 +0800199 m->delta[1] = le16_to_cpu(di->di_u.delta[1]);
200 break;
201 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
Gao Xiang72bb5262021-10-18 00:57:21 +0800202 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD1:
203 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD2:
Gao Xiang152a3332019-06-24 15:22:52 +0800204 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 Xiang382329a2019-08-14 18:37:04 +0800209 return -EOPNOTSUPP;
Gao Xiang152a3332019-06-24 15:22:52 +0800210 }
211 m->type = type;
212 return 0;
213}
214
215static 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 Xiangd95ae5e2021-08-18 23:22:31 +0800226static 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 Xiang152a3332019-06-24 15:22:52 +0800251static int unpack_compacted_index(struct z_erofs_maprecorder *m,
252 unsigned int amortizedshift,
Yue Huab921842021-12-28 13:46:04 +0800253 erofs_off_t pos, bool lookahead)
Gao Xiang152a3332019-06-24 15:22:52 +0800254{
Gao Xianga5876e22019-09-04 10:08:56 +0800255 struct erofs_inode *const vi = EROFS_I(m->inode);
Gao Xiang152a3332019-06-24 15:22:52 +0800256 const unsigned int lclusterbits = vi->z_logical_clusterbits;
257 const unsigned int lomask = (1 << lclusterbits) - 1;
Yue Huab921842021-12-28 13:46:04 +0800258 unsigned int vcnt, base, lo, encodebits, nblk, eofs;
Gao Xiang152a3332019-06-24 15:22:52 +0800259 int i;
260 u8 *in, type;
Gao Xiangb86269f2021-04-07 12:39:25 +0800261 bool big_pcluster;
Gao Xiang152a3332019-06-24 15:22:52 +0800262
263 if (1 << amortizedshift == 4)
264 vcnt = 2;
265 else if (1 << amortizedshift == 2 && lclusterbits == 12)
266 vcnt = 16;
267 else
Gao Xiangff784a72019-08-14 18:37:05 +0800268 return -EOPNOTSUPP;
Gao Xiang152a3332019-06-24 15:22:52 +0800269
Yue Huab921842021-12-28 13:46:04 +0800270 /* it doesn't equal to round_up(..) */
271 m->nextpackoff = round_down(pos, vcnt << amortizedshift) +
272 (vcnt << amortizedshift);
Gao Xiangb86269f2021-04-07 12:39:25 +0800273 big_pcluster = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1;
Gao Xiang152a3332019-06-24 15:22:52 +0800274 encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt;
Yue Huab921842021-12-28 13:46:04 +0800275 eofs = erofs_blkoff(pos);
Gao Xiang152a3332019-06-24 15:22:52 +0800276 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 Xiangd95ae5e2021-08-18 23:22:31 +0800286
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 Xiangb86269f2021-04-07 12:39:25 +0800291 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 Xiang152a3332019-06-24 15:22:52 +0800300 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 Xiangb86269f2021-04-07 12:39:25 +0800312 else if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT)
313 lo = 1;
Gao Xiang152a3332019-06-24 15:22:52 +0800314 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 Xiangb86269f2021-04-07 12:39:25 +0800320 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 Xiang152a3332019-06-24 15:22:52 +0800328
Gao Xiangb86269f2021-04-07 12:39:25 +0800329 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 Xiang152a3332019-06-24 15:22:52 +0800352 ++nblk;
Gao Xiangb86269f2021-04-07 12:39:25 +0800353 }
Gao Xiang152a3332019-06-24 15:22:52 +0800354 }
355 in += (vcnt << amortizedshift) - sizeof(__le32);
356 m->pblk = le32_to_cpu(*(__le32 *)in) + nblk;
357 return 0;
358}
359
360static int compacted_load_cluster_from_disk(struct z_erofs_maprecorder *m,
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800361 unsigned long lcn, bool lookahead)
Gao Xiang152a3332019-06-24 15:22:52 +0800362{
363 struct inode *const inode = m->inode;
Gao Xianga5876e22019-09-04 10:08:56 +0800364 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiang152a3332019-06-24 15:22:52 +0800365 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 Xiangff784a72019-08-14 18:37:05 +0800376 return -EOPNOTSUPP;
Gao Xiang152a3332019-06-24 15:22:52 +0800377
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 Huc40dd3c2021-09-14 11:59:15 +0800387 if ((vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B) &&
388 compacted_4b_initial < totalidx)
Gao Xiang152a3332019-06-24 15:22:52 +0800389 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;
408out:
409 pos += lcn * (1 << amortizedshift);
410 err = z_erofs_reload_indexes(m, erofs_blknr(pos));
411 if (err)
412 return err;
Yue Huab921842021-12-28 13:46:04 +0800413 return unpack_compacted_index(m, amortizedshift, pos, lookahead);
Gao Xiang152a3332019-06-24 15:22:52 +0800414}
415
Gao Xiang0c638f72019-11-08 11:37:33 +0800416static int z_erofs_load_cluster_from_disk(struct z_erofs_maprecorder *m,
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800417 unsigned int lcn, bool lookahead)
Gao Xiang152a3332019-06-24 15:22:52 +0800418{
Gao Xianga5876e22019-09-04 10:08:56 +0800419 const unsigned int datamode = EROFS_I(m->inode)->datalayout;
Gao Xiang152a3332019-06-24 15:22:52 +0800420
421 if (datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY)
Gao Xiang0c638f72019-11-08 11:37:33 +0800422 return legacy_load_cluster_from_disk(m, lcn);
Gao Xiang152a3332019-06-24 15:22:52 +0800423
424 if (datamode == EROFS_INODE_FLAT_COMPRESSION)
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800425 return compacted_load_cluster_from_disk(m, lcn, lookahead);
Gao Xiang152a3332019-06-24 15:22:52 +0800426
427 return -EINVAL;
428}
429
Gao Xiang0c638f72019-11-08 11:37:33 +0800430static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m,
431 unsigned int lookback_distance)
Gao Xiang152a3332019-06-24 15:22:52 +0800432{
Gao Xianga5876e22019-09-04 10:08:56 +0800433 struct erofs_inode *const vi = EROFS_I(m->inode);
Gao Xiang152a3332019-06-24 15:22:52 +0800434 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 Xiang4f761fa2019-09-04 10:09:09 +0800440 erofs_err(m->inode->i_sb,
441 "bogus lookback distance @ nid %llu", vi->nid);
Gao Xiang152a3332019-06-24 15:22:52 +0800442 DBG_BUGON(1);
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800443 return -EFSCORRUPTED;
Gao Xiang152a3332019-06-24 15:22:52 +0800444 }
445
446 /* load extent head logical cluster if needed */
447 lcn -= lookback_distance;
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800448 err = z_erofs_load_cluster_from_disk(m, lcn, false);
Gao Xiang152a3332019-06-24 15:22:52 +0800449 if (err)
450 return err;
451
452 switch (m->type) {
453 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800454 if (!m->delta[0]) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800455 erofs_err(m->inode->i_sb,
456 "invalid lookback distance 0 @ nid %llu",
457 vi->nid);
Gao Xiang598bb892019-08-19 18:34:26 +0800458 DBG_BUGON(1);
459 return -EFSCORRUPTED;
460 }
Gao Xiang0c638f72019-11-08 11:37:33 +0800461 return z_erofs_extent_lookback(m, m->delta[0]);
Gao Xiang152a3332019-06-24 15:22:52 +0800462 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
Gao Xiang72bb5262021-10-18 00:57:21 +0800463 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD1:
464 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD2:
Gao Xiang8f899262021-10-09 04:08:37 +0800465 m->headtype = m->type;
Gao Xiang152a3332019-06-24 15:22:52 +0800466 map->m_la = (lcn << lclusterbits) | m->clusterofs;
467 break;
468 default:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800469 erofs_err(m->inode->i_sb,
470 "unknown type %u @ lcn %lu of nid %llu",
471 m->type, lcn, vi->nid);
Gao Xiang152a3332019-06-24 15:22:52 +0800472 DBG_BUGON(1);
Gao Xiang382329a2019-08-14 18:37:04 +0800473 return -EOPNOTSUPP;
Gao Xiang152a3332019-06-24 15:22:52 +0800474 }
475 return 0;
476}
477
Gao Xiangcec6e932021-04-07 12:39:24 +0800478static 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 Xiang72bb5262021-10-18 00:57:21 +0800488 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 Xiang8f899262021-10-09 04:08:37 +0800492 if (m->headtype == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN ||
Gao Xiang72bb5262021-10-18 00:57:21 +0800493 ((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 Xiangcec6e932021-04-07 12:39:24 +0800497 map->m_plen = 1 << lclusterbits;
498 return 0;
499 }
Gao Xiangcec6e932021-04-07 12:39:24 +0800500 lcn = m->lcn + 1;
501 if (m->compressedlcs)
502 goto out;
Gao Xiangcec6e932021-04-07 12:39:24 +0800503
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800504 err = z_erofs_load_cluster_from_disk(m, lcn, false);
Gao Xiangcec6e932021-04-07 12:39:24 +0800505 if (err)
506 return err;
507
Gao Xiang0852b6c2021-05-10 14:47:15 +0800508 /*
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 Xiangcec6e932021-04-07 12:39:24 +0800519 switch (m->type) {
Gao Xiang0852b6c2021-05-10 14:47:15 +0800520 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
Gao Xiang72bb5262021-10-18 00:57:21 +0800521 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD1:
522 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD2:
Gao Xiang0852b6c2021-05-10 14:47:15 +0800523 /*
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 Xiangcec6e932021-04-07 12:39:24 +0800529 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 }
542out:
543 map->m_plen = m->compressedlcs << lclusterbits;
544 return 0;
545err_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 Xiangd95ae5e2021-08-18 23:22:31 +0800553static 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 Xiang72bb5262021-10-18 00:57:21 +0800577 m->type == Z_EROFS_VLE_CLUSTER_TYPE_HEAD1 ||
578 m->type == Z_EROFS_VLE_CLUSTER_TYPE_HEAD2) {
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800579 /* 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 Huab921842021-12-28 13:46:04 +0800596static int z_erofs_do_map_blocks(struct inode *inode,
597 struct erofs_map_blocks *map,
598 int flags)
Gao Xiang152a3332019-06-24 15:22:52 +0800599{
Gao Xianga5876e22019-09-04 10:08:56 +0800600 struct erofs_inode *const vi = EROFS_I(inode);
Yue Huab921842021-12-28 13:46:04 +0800601 bool ztailpacking = vi->z_advise & Z_EROFS_ADVISE_INLINE_PCLUSTER;
Gao Xiang152a3332019-06-24 15:22:52 +0800602 struct z_erofs_maprecorder m = {
603 .inode = inode,
604 .map = map,
605 };
606 int err = 0;
607 unsigned int lclusterbits, endoff;
Gao Xiangcec6e932021-04-07 12:39:24 +0800608 unsigned long initial_lcn;
Gao Xiang152a3332019-06-24 15:22:52 +0800609 unsigned long long ofs, end;
610
Gao Xiang152a3332019-06-24 15:22:52 +0800611 lclusterbits = vi->z_logical_clusterbits;
Yue Huab921842021-12-28 13:46:04 +0800612 ofs = flags & EROFS_GET_BLOCKS_FINDTAIL ? inode->i_size - 1 : map->m_la;
Gao Xiangcec6e932021-04-07 12:39:24 +0800613 initial_lcn = ofs >> lclusterbits;
Gao Xiang152a3332019-06-24 15:22:52 +0800614 endoff = ofs & ((1 << lclusterbits) - 1);
615
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800616 err = z_erofs_load_cluster_from_disk(&m, initial_lcn, false);
Gao Xiang152a3332019-06-24 15:22:52 +0800617 if (err)
618 goto unmap_out;
619
Yue Huab921842021-12-28 13:46:04 +0800620 if (ztailpacking && (flags & EROFS_GET_BLOCKS_FINDTAIL))
621 vi->z_idataoff = m.nextpackoff;
622
Gao Xiang8f899262021-10-09 04:08:37 +0800623 map->m_flags = EROFS_MAP_MAPPED | EROFS_MAP_ENCODED;
Gao Xiang152a3332019-06-24 15:22:52 +0800624 end = (m.lcn + 1ULL) << lclusterbits;
625
626 switch (m.type) {
627 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
Gao Xiang72bb5262021-10-18 00:57:21 +0800628 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD1:
629 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD2:
Gao Xiang152a3332019-06-24 15:22:52 +0800630 if (endoff >= m.clusterofs) {
Gao Xiang8f899262021-10-09 04:08:37 +0800631 m.headtype = m.type;
Gao Xiang152a3332019-06-24 15:22:52 +0800632 map->m_la = (m.lcn << lclusterbits) | m.clusterofs;
633 break;
634 }
635 /* m.lcn should be >= 1 if endoff < m.clusterofs */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800636 if (!m.lcn) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800637 erofs_err(inode->i_sb,
638 "invalid logical cluster 0 at nid %llu",
639 vi->nid);
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800640 err = -EFSCORRUPTED;
Gao Xiang152a3332019-06-24 15:22:52 +0800641 goto unmap_out;
642 }
643 end = (m.lcn << lclusterbits) | m.clusterofs;
Gao Xiangb6a76182019-06-24 15:22:58 +0800644 map->m_flags |= EROFS_MAP_FULL_MAPPED;
Gao Xiang152a3332019-06-24 15:22:52 +0800645 m.delta[0] = 1;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500646 fallthrough;
Gao Xiang152a3332019-06-24 15:22:52 +0800647 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
Ruiqi Gongfe6adcc2021-03-31 05:39:20 -0400648 /* get the corresponding first chunk */
Gao Xiang0c638f72019-11-08 11:37:33 +0800649 err = z_erofs_extent_lookback(&m, m.delta[0]);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800650 if (err)
Gao Xiang152a3332019-06-24 15:22:52 +0800651 goto unmap_out;
652 break;
653 default:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800654 erofs_err(inode->i_sb,
655 "unknown type %u @ offset %llu of nid %llu",
656 m.type, ofs, vi->nid);
Gao Xiang382329a2019-08-14 18:37:04 +0800657 err = -EOPNOTSUPP;
Gao Xiang152a3332019-06-24 15:22:52 +0800658 goto unmap_out;
659 }
660
661 map->m_llen = end - map->m_la;
Gao Xiang152a3332019-06-24 15:22:52 +0800662
Yue Huab921842021-12-28 13:46:04 +0800663 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 Xiangd95ae5e2021-08-18 23:22:31 +0800675
Gao Xiang8f899262021-10-09 04:08:37 +0800676 if (m.headtype == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN)
677 map->m_algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
Gao Xiang72bb5262021-10-18 00:57:21 +0800678 else if (m.headtype == Z_EROFS_VLE_CLUSTER_TYPE_HEAD2)
679 map->m_algorithmformat = vi->z_algorithmtype[1];
Gao Xiang8f899262021-10-09 04:08:37 +0800680 else
681 map->m_algorithmformat = vi->z_algorithmtype[0];
682
Gao Xiang622cead2021-10-11 05:31:45 +0800683 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 Xiangd95ae5e2021-08-18 23:22:31 +0800687 err = z_erofs_get_extent_decompressedlen(&m);
688 if (!err)
689 map->m_flags |= EROFS_MAP_FULL_MAPPED;
690 }
Gao Xiang152a3332019-06-24 15:22:52 +0800691unmap_out:
Gao Xiang09c54372022-01-02 12:00:17 +0800692 erofs_unmap_metabuf(&m.map->buf);
Gao Xiang152a3332019-06-24 15:22:52 +0800693
694out:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800695 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 Xiang152a3332019-06-24 15:22:52 +0800698
Yue Huab921842021-12-28 13:46:04 +0800699 return err;
700}
701
702int 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);
723out:
Gao Xiang152a3332019-06-24 15:22:52 +0800724 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 Xiangeadcd6b2021-08-13 13:29:31 +0800730
731static 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 Xiang09c54372022-01-02 12:00:17 +0800739 erofs_put_metabuf(&map.buf);
Gao Xiangeadcd6b2021-08-13 13:29:31 +0800740 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
764const struct iomap_ops z_erofs_iomap_report_ops = {
765 .iomap_begin = z_erofs_iomap_begin_report,
766};