blob: 660489a7fb64d5aa032a991b898a0005ba0327b7 [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
10int z_erofs_fill_inode(struct inode *inode)
11{
Gao Xianga5876e22019-09-04 10:08:56 +080012 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiangcec6e932021-04-07 12:39:24 +080013 struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
Gao Xiang152a3332019-06-24 15:22:52 +080014
Gao Xiangcec6e932021-04-07 12:39:24 +080015 if (!erofs_sb_has_big_pcluster(sbi) &&
16 vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY) {
Gao Xiang152a3332019-06-24 15:22:52 +080017 vi->z_advise = 0;
18 vi->z_algorithmtype[0] = 0;
19 vi->z_algorithmtype[1] = 0;
Gao Xiangeace9942019-07-31 23:57:48 +080020 vi->z_logical_clusterbits = LOG_BLOCK_SIZE;
Gao Xianga5876e22019-09-04 10:08:56 +080021 set_bit(EROFS_I_Z_INITED_BIT, &vi->flags);
Gao Xiang152a3332019-06-24 15:22:52 +080022 }
Gao Xiang0c638f72019-11-08 11:37:33 +080023 inode->i_mapping->a_ops = &z_erofs_aops;
Gao Xiang152a3332019-06-24 15:22:52 +080024 return 0;
25}
26
Gao Xiang0c638f72019-11-08 11:37:33 +080027static int z_erofs_fill_inode_lazy(struct inode *inode)
Gao Xiang152a3332019-06-24 15:22:52 +080028{
Gao Xianga5876e22019-09-04 10:08:56 +080029 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiang152a3332019-06-24 15:22:52 +080030 struct super_block *const sb = inode->i_sb;
Gao Xiang72bb5262021-10-18 00:57:21 +080031 int err, headnr;
Gao Xiang152a3332019-06-24 15:22:52 +080032 erofs_off_t pos;
33 struct page *page;
34 void *kaddr;
35 struct z_erofs_map_header *h;
36
Gao Xiangce0631292021-02-09 21:06:18 +080037 if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags)) {
38 /*
39 * paired with smp_mb() at the end of the function to ensure
40 * fields will only be observed after the bit is set.
41 */
42 smp_mb();
Gao Xiang152a3332019-06-24 15:22:52 +080043 return 0;
Gao Xiangce0631292021-02-09 21:06:18 +080044 }
Gao Xiang152a3332019-06-24 15:22:52 +080045
Gao Xianga5876e22019-09-04 10:08:56 +080046 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_Z_BIT, TASK_KILLABLE))
Gao Xiang152a3332019-06-24 15:22:52 +080047 return -ERESTARTSYS;
48
49 err = 0;
Gao Xianga5876e22019-09-04 10:08:56 +080050 if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags))
Gao Xiang152a3332019-06-24 15:22:52 +080051 goto out_unlock;
52
Gao Xiangcec6e932021-04-07 12:39:24 +080053 DBG_BUGON(!erofs_sb_has_big_pcluster(EROFS_SB(sb)) &&
54 vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY);
Gao Xiang152a3332019-06-24 15:22:52 +080055
56 pos = ALIGN(iloc(EROFS_SB(sb), vi->nid) + vi->inode_isize +
57 vi->xattr_isize, 8);
Gao Xiange655b5b2019-09-04 10:09:03 +080058 page = erofs_get_meta_page(sb, erofs_blknr(pos));
Gao Xiang152a3332019-06-24 15:22:52 +080059 if (IS_ERR(page)) {
60 err = PTR_ERR(page);
61 goto out_unlock;
62 }
63
64 kaddr = kmap_atomic(page);
65
66 h = kaddr + erofs_blkoff(pos);
67 vi->z_advise = le16_to_cpu(h->h_advise);
68 vi->z_algorithmtype[0] = h->h_algorithmtype & 15;
69 vi->z_algorithmtype[1] = h->h_algorithmtype >> 4;
70
Gao Xiang72bb5262021-10-18 00:57:21 +080071 headnr = 0;
72 if (vi->z_algorithmtype[0] >= Z_EROFS_COMPRESSION_MAX ||
73 vi->z_algorithmtype[++headnr] >= Z_EROFS_COMPRESSION_MAX) {
74 erofs_err(sb, "unknown HEAD%u format %u for nid %llu, please upgrade kernel",
75 headnr + 1, vi->z_algorithmtype[headnr], vi->nid);
Gao Xiangff784a72019-08-14 18:37:05 +080076 err = -EOPNOTSUPP;
Gao Xiang152a3332019-06-24 15:22:52 +080077 goto unmap_done;
78 }
79
80 vi->z_logical_clusterbits = LOG_BLOCK_SIZE + (h->h_clusterbits & 7);
Gao Xiangb86269f2021-04-07 12:39:25 +080081 if (!erofs_sb_has_big_pcluster(EROFS_SB(sb)) &&
82 vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 |
83 Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
84 erofs_err(sb, "per-inode big pcluster without sb feature for nid %llu",
85 vi->nid);
86 err = -EFSCORRUPTED;
87 goto unmap_done;
88 }
89 if (vi->datalayout == EROFS_INODE_FLAT_COMPRESSION &&
90 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1) ^
91 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
92 erofs_err(sb, "big pcluster head1/2 of compact indexes should be consistent for nid %llu",
93 vi->nid);
94 err = -EFSCORRUPTED;
95 goto unmap_done;
96 }
Gao Xiangce0631292021-02-09 21:06:18 +080097 /* paired with smp_mb() at the beginning of the function */
98 smp_mb();
Gao Xianga5876e22019-09-04 10:08:56 +080099 set_bit(EROFS_I_Z_INITED_BIT, &vi->flags);
Gao Xiang152a3332019-06-24 15:22:52 +0800100unmap_done:
101 kunmap_atomic(kaddr);
102 unlock_page(page);
103 put_page(page);
Gao Xiang152a3332019-06-24 15:22:52 +0800104out_unlock:
Gao Xianga5876e22019-09-04 10:08:56 +0800105 clear_and_wake_up_bit(EROFS_I_BL_Z_BIT, &vi->flags);
Gao Xiang152a3332019-06-24 15:22:52 +0800106 return err;
107}
108
109struct z_erofs_maprecorder {
110 struct inode *inode;
111 struct erofs_map_blocks *map;
112 void *kaddr;
113
114 unsigned long lcn;
115 /* compression extent information gathered */
Gao Xiang8f899262021-10-09 04:08:37 +0800116 u8 type, headtype;
Gao Xiang152a3332019-06-24 15:22:52 +0800117 u16 clusterofs;
118 u16 delta[2];
Gao Xiangcec6e932021-04-07 12:39:24 +0800119 erofs_blk_t pblk, compressedlcs;
Gao Xiang152a3332019-06-24 15:22:52 +0800120};
121
122static int z_erofs_reload_indexes(struct z_erofs_maprecorder *m,
123 erofs_blk_t eblk)
124{
125 struct super_block *const sb = m->inode->i_sb;
126 struct erofs_map_blocks *const map = m->map;
127 struct page *mpage = map->mpage;
128
129 if (mpage) {
130 if (mpage->index == eblk) {
131 if (!m->kaddr)
132 m->kaddr = kmap_atomic(mpage);
133 return 0;
134 }
135
136 if (m->kaddr) {
137 kunmap_atomic(m->kaddr);
138 m->kaddr = NULL;
139 }
140 put_page(mpage);
141 }
142
Gao Xiange655b5b2019-09-04 10:09:03 +0800143 mpage = erofs_get_meta_page(sb, eblk);
Gao Xiang152a3332019-06-24 15:22:52 +0800144 if (IS_ERR(mpage)) {
145 map->mpage = NULL;
146 return PTR_ERR(mpage);
147 }
148 m->kaddr = kmap_atomic(mpage);
149 unlock_page(mpage);
150 map->mpage = mpage;
151 return 0;
152}
153
Gao Xiang0c638f72019-11-08 11:37:33 +0800154static int legacy_load_cluster_from_disk(struct z_erofs_maprecorder *m,
155 unsigned long lcn)
Gao Xiang152a3332019-06-24 15:22:52 +0800156{
157 struct inode *const inode = m->inode;
Gao Xianga5876e22019-09-04 10:08:56 +0800158 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiang152a3332019-06-24 15:22:52 +0800159 const erofs_off_t ibase = iloc(EROFS_I_SB(inode), vi->nid);
160 const erofs_off_t pos =
161 Z_EROFS_VLE_LEGACY_INDEX_ALIGN(ibase + vi->inode_isize +
162 vi->xattr_isize) +
163 lcn * sizeof(struct z_erofs_vle_decompressed_index);
164 struct z_erofs_vle_decompressed_index *di;
165 unsigned int advise, type;
166 int err;
167
168 err = z_erofs_reload_indexes(m, erofs_blknr(pos));
169 if (err)
170 return err;
171
172 m->lcn = lcn;
173 di = m->kaddr + erofs_blkoff(pos);
174
175 advise = le16_to_cpu(di->di_advise);
176 type = (advise >> Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT) &
177 ((1 << Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) - 1);
178 switch (type) {
179 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
180 m->clusterofs = 1 << vi->z_logical_clusterbits;
181 m->delta[0] = le16_to_cpu(di->di_u.delta[0]);
Gao Xiangcec6e932021-04-07 12:39:24 +0800182 if (m->delta[0] & Z_EROFS_VLE_DI_D0_CBLKCNT) {
Gao Xiang72bb5262021-10-18 00:57:21 +0800183 if (!(vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 |
184 Z_EROFS_ADVISE_BIG_PCLUSTER_2))) {
Gao Xiangcec6e932021-04-07 12:39:24 +0800185 DBG_BUGON(1);
186 return -EFSCORRUPTED;
187 }
188 m->compressedlcs = m->delta[0] &
189 ~Z_EROFS_VLE_DI_D0_CBLKCNT;
190 m->delta[0] = 1;
191 }
Gao Xiang152a3332019-06-24 15:22:52 +0800192 m->delta[1] = le16_to_cpu(di->di_u.delta[1]);
193 break;
194 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
Gao Xiang72bb5262021-10-18 00:57:21 +0800195 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD1:
196 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD2:
Gao Xiang152a3332019-06-24 15:22:52 +0800197 m->clusterofs = le16_to_cpu(di->di_clusterofs);
198 m->pblk = le32_to_cpu(di->di_u.blkaddr);
199 break;
200 default:
201 DBG_BUGON(1);
Gao Xiang382329a2019-08-14 18:37:04 +0800202 return -EOPNOTSUPP;
Gao Xiang152a3332019-06-24 15:22:52 +0800203 }
204 m->type = type;
205 return 0;
206}
207
208static unsigned int decode_compactedbits(unsigned int lobits,
209 unsigned int lomask,
210 u8 *in, unsigned int pos, u8 *type)
211{
212 const unsigned int v = get_unaligned_le32(in + pos / 8) >> (pos & 7);
213 const unsigned int lo = v & lomask;
214
215 *type = (v >> lobits) & 3;
216 return lo;
217}
218
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800219static int get_compacted_la_distance(unsigned int lclusterbits,
220 unsigned int encodebits,
221 unsigned int vcnt, u8 *in, int i)
222{
223 const unsigned int lomask = (1 << lclusterbits) - 1;
224 unsigned int lo, d1 = 0;
225 u8 type;
226
227 DBG_BUGON(i >= vcnt);
228
229 do {
230 lo = decode_compactedbits(lclusterbits, lomask,
231 in, encodebits * i, &type);
232
233 if (type != Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
234 return d1;
235 ++d1;
236 } while (++i < vcnt);
237
238 /* vcnt - 1 (Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) item */
239 if (!(lo & Z_EROFS_VLE_DI_D0_CBLKCNT))
240 d1 += lo - 1;
241 return d1;
242}
243
Gao Xiang152a3332019-06-24 15:22:52 +0800244static int unpack_compacted_index(struct z_erofs_maprecorder *m,
245 unsigned int amortizedshift,
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800246 unsigned int eofs, bool lookahead)
Gao Xiang152a3332019-06-24 15:22:52 +0800247{
Gao Xianga5876e22019-09-04 10:08:56 +0800248 struct erofs_inode *const vi = EROFS_I(m->inode);
Gao Xiang152a3332019-06-24 15:22:52 +0800249 const unsigned int lclusterbits = vi->z_logical_clusterbits;
250 const unsigned int lomask = (1 << lclusterbits) - 1;
251 unsigned int vcnt, base, lo, encodebits, nblk;
252 int i;
253 u8 *in, type;
Gao Xiangb86269f2021-04-07 12:39:25 +0800254 bool big_pcluster;
Gao Xiang152a3332019-06-24 15:22:52 +0800255
256 if (1 << amortizedshift == 4)
257 vcnt = 2;
258 else if (1 << amortizedshift == 2 && lclusterbits == 12)
259 vcnt = 16;
260 else
Gao Xiangff784a72019-08-14 18:37:05 +0800261 return -EOPNOTSUPP;
Gao Xiang152a3332019-06-24 15:22:52 +0800262
Gao Xiangb86269f2021-04-07 12:39:25 +0800263 big_pcluster = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1;
Gao Xiang152a3332019-06-24 15:22:52 +0800264 encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt;
265 base = round_down(eofs, vcnt << amortizedshift);
266 in = m->kaddr + base;
267
268 i = (eofs - base) >> amortizedshift;
269
270 lo = decode_compactedbits(lclusterbits, lomask,
271 in, encodebits * i, &type);
272 m->type = type;
273 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) {
274 m->clusterofs = 1 << lclusterbits;
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800275
276 /* figure out lookahead_distance: delta[1] if needed */
277 if (lookahead)
278 m->delta[1] = get_compacted_la_distance(lclusterbits,
279 encodebits, vcnt, in, i);
Gao Xiangb86269f2021-04-07 12:39:25 +0800280 if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT) {
281 if (!big_pcluster) {
282 DBG_BUGON(1);
283 return -EFSCORRUPTED;
284 }
285 m->compressedlcs = lo & ~Z_EROFS_VLE_DI_D0_CBLKCNT;
286 m->delta[0] = 1;
287 return 0;
288 } else if (i + 1 != (int)vcnt) {
Gao Xiang152a3332019-06-24 15:22:52 +0800289 m->delta[0] = lo;
290 return 0;
291 }
292 /*
293 * since the last lcluster in the pack is special,
294 * of which lo saves delta[1] rather than delta[0].
295 * Hence, get delta[0] by the previous lcluster indirectly.
296 */
297 lo = decode_compactedbits(lclusterbits, lomask,
298 in, encodebits * (i - 1), &type);
299 if (type != Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
300 lo = 0;
Gao Xiangb86269f2021-04-07 12:39:25 +0800301 else if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT)
302 lo = 1;
Gao Xiang152a3332019-06-24 15:22:52 +0800303 m->delta[0] = lo + 1;
304 return 0;
305 }
306 m->clusterofs = lo;
307 m->delta[0] = 0;
308 /* figout out blkaddr (pblk) for HEAD lclusters */
Gao Xiangb86269f2021-04-07 12:39:25 +0800309 if (!big_pcluster) {
310 nblk = 1;
311 while (i > 0) {
312 --i;
313 lo = decode_compactedbits(lclusterbits, lomask,
314 in, encodebits * i, &type);
315 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
316 i -= lo;
Gao Xiang152a3332019-06-24 15:22:52 +0800317
Gao Xiangb86269f2021-04-07 12:39:25 +0800318 if (i >= 0)
319 ++nblk;
320 }
321 } else {
322 nblk = 0;
323 while (i > 0) {
324 --i;
325 lo = decode_compactedbits(lclusterbits, lomask,
326 in, encodebits * i, &type);
327 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) {
328 if (lo & Z_EROFS_VLE_DI_D0_CBLKCNT) {
329 --i;
330 nblk += lo & ~Z_EROFS_VLE_DI_D0_CBLKCNT;
331 continue;
332 }
333 /* bigpcluster shouldn't have plain d0 == 1 */
334 if (lo <= 1) {
335 DBG_BUGON(1);
336 return -EFSCORRUPTED;
337 }
338 i -= lo - 2;
339 continue;
340 }
Gao Xiang152a3332019-06-24 15:22:52 +0800341 ++nblk;
Gao Xiangb86269f2021-04-07 12:39:25 +0800342 }
Gao Xiang152a3332019-06-24 15:22:52 +0800343 }
344 in += (vcnt << amortizedshift) - sizeof(__le32);
345 m->pblk = le32_to_cpu(*(__le32 *)in) + nblk;
346 return 0;
347}
348
349static int compacted_load_cluster_from_disk(struct z_erofs_maprecorder *m,
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800350 unsigned long lcn, bool lookahead)
Gao Xiang152a3332019-06-24 15:22:52 +0800351{
352 struct inode *const inode = m->inode;
Gao Xianga5876e22019-09-04 10:08:56 +0800353 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiang152a3332019-06-24 15:22:52 +0800354 const unsigned int lclusterbits = vi->z_logical_clusterbits;
355 const erofs_off_t ebase = ALIGN(iloc(EROFS_I_SB(inode), vi->nid) +
356 vi->inode_isize + vi->xattr_isize, 8) +
357 sizeof(struct z_erofs_map_header);
358 const unsigned int totalidx = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
359 unsigned int compacted_4b_initial, compacted_2b;
360 unsigned int amortizedshift;
361 erofs_off_t pos;
362 int err;
363
364 if (lclusterbits != 12)
Gao Xiangff784a72019-08-14 18:37:05 +0800365 return -EOPNOTSUPP;
Gao Xiang152a3332019-06-24 15:22:52 +0800366
367 if (lcn >= totalidx)
368 return -EINVAL;
369
370 m->lcn = lcn;
371 /* used to align to 32-byte (compacted_2b) alignment */
372 compacted_4b_initial = (32 - ebase % 32) / 4;
373 if (compacted_4b_initial == 32 / 4)
374 compacted_4b_initial = 0;
375
Yue Huc40dd3c2021-09-14 11:59:15 +0800376 if ((vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B) &&
377 compacted_4b_initial < totalidx)
Gao Xiang152a3332019-06-24 15:22:52 +0800378 compacted_2b = rounddown(totalidx - compacted_4b_initial, 16);
379 else
380 compacted_2b = 0;
381
382 pos = ebase;
383 if (lcn < compacted_4b_initial) {
384 amortizedshift = 2;
385 goto out;
386 }
387 pos += compacted_4b_initial * 4;
388 lcn -= compacted_4b_initial;
389
390 if (lcn < compacted_2b) {
391 amortizedshift = 1;
392 goto out;
393 }
394 pos += compacted_2b * 2;
395 lcn -= compacted_2b;
396 amortizedshift = 2;
397out:
398 pos += lcn * (1 << amortizedshift);
399 err = z_erofs_reload_indexes(m, erofs_blknr(pos));
400 if (err)
401 return err;
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800402 return unpack_compacted_index(m, amortizedshift, erofs_blkoff(pos),
403 lookahead);
Gao Xiang152a3332019-06-24 15:22:52 +0800404}
405
Gao Xiang0c638f72019-11-08 11:37:33 +0800406static int z_erofs_load_cluster_from_disk(struct z_erofs_maprecorder *m,
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800407 unsigned int lcn, bool lookahead)
Gao Xiang152a3332019-06-24 15:22:52 +0800408{
Gao Xianga5876e22019-09-04 10:08:56 +0800409 const unsigned int datamode = EROFS_I(m->inode)->datalayout;
Gao Xiang152a3332019-06-24 15:22:52 +0800410
411 if (datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY)
Gao Xiang0c638f72019-11-08 11:37:33 +0800412 return legacy_load_cluster_from_disk(m, lcn);
Gao Xiang152a3332019-06-24 15:22:52 +0800413
414 if (datamode == EROFS_INODE_FLAT_COMPRESSION)
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800415 return compacted_load_cluster_from_disk(m, lcn, lookahead);
Gao Xiang152a3332019-06-24 15:22:52 +0800416
417 return -EINVAL;
418}
419
Gao Xiang0c638f72019-11-08 11:37:33 +0800420static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m,
421 unsigned int lookback_distance)
Gao Xiang152a3332019-06-24 15:22:52 +0800422{
Gao Xianga5876e22019-09-04 10:08:56 +0800423 struct erofs_inode *const vi = EROFS_I(m->inode);
Gao Xiang152a3332019-06-24 15:22:52 +0800424 struct erofs_map_blocks *const map = m->map;
425 const unsigned int lclusterbits = vi->z_logical_clusterbits;
426 unsigned long lcn = m->lcn;
427 int err;
428
429 if (lcn < lookback_distance) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800430 erofs_err(m->inode->i_sb,
431 "bogus lookback distance @ nid %llu", vi->nid);
Gao Xiang152a3332019-06-24 15:22:52 +0800432 DBG_BUGON(1);
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800433 return -EFSCORRUPTED;
Gao Xiang152a3332019-06-24 15:22:52 +0800434 }
435
436 /* load extent head logical cluster if needed */
437 lcn -= lookback_distance;
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800438 err = z_erofs_load_cluster_from_disk(m, lcn, false);
Gao Xiang152a3332019-06-24 15:22:52 +0800439 if (err)
440 return err;
441
442 switch (m->type) {
443 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800444 if (!m->delta[0]) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800445 erofs_err(m->inode->i_sb,
446 "invalid lookback distance 0 @ nid %llu",
447 vi->nid);
Gao Xiang598bb892019-08-19 18:34:26 +0800448 DBG_BUGON(1);
449 return -EFSCORRUPTED;
450 }
Gao Xiang0c638f72019-11-08 11:37:33 +0800451 return z_erofs_extent_lookback(m, m->delta[0]);
Gao Xiang152a3332019-06-24 15:22:52 +0800452 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
Gao Xiang72bb5262021-10-18 00:57:21 +0800453 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD1:
454 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD2:
Gao Xiang8f899262021-10-09 04:08:37 +0800455 m->headtype = m->type;
Gao Xiang152a3332019-06-24 15:22:52 +0800456 map->m_la = (lcn << lclusterbits) | m->clusterofs;
457 break;
458 default:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800459 erofs_err(m->inode->i_sb,
460 "unknown type %u @ lcn %lu of nid %llu",
461 m->type, lcn, vi->nid);
Gao Xiang152a3332019-06-24 15:22:52 +0800462 DBG_BUGON(1);
Gao Xiang382329a2019-08-14 18:37:04 +0800463 return -EOPNOTSUPP;
Gao Xiang152a3332019-06-24 15:22:52 +0800464 }
465 return 0;
466}
467
Gao Xiangcec6e932021-04-07 12:39:24 +0800468static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m,
469 unsigned int initial_lcn)
470{
471 struct erofs_inode *const vi = EROFS_I(m->inode);
472 struct erofs_map_blocks *const map = m->map;
473 const unsigned int lclusterbits = vi->z_logical_clusterbits;
474 unsigned long lcn;
475 int err;
476
477 DBG_BUGON(m->type != Z_EROFS_VLE_CLUSTER_TYPE_PLAIN &&
Gao Xiang72bb5262021-10-18 00:57:21 +0800478 m->type != Z_EROFS_VLE_CLUSTER_TYPE_HEAD1 &&
479 m->type != Z_EROFS_VLE_CLUSTER_TYPE_HEAD2);
480 DBG_BUGON(m->type != m->headtype);
481
Gao Xiang8f899262021-10-09 04:08:37 +0800482 if (m->headtype == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN ||
Gao Xiang72bb5262021-10-18 00:57:21 +0800483 ((m->headtype == Z_EROFS_VLE_CLUSTER_TYPE_HEAD1) &&
484 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1)) ||
485 ((m->headtype == Z_EROFS_VLE_CLUSTER_TYPE_HEAD2) &&
486 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2))) {
Gao Xiangcec6e932021-04-07 12:39:24 +0800487 map->m_plen = 1 << lclusterbits;
488 return 0;
489 }
Gao Xiangcec6e932021-04-07 12:39:24 +0800490 lcn = m->lcn + 1;
491 if (m->compressedlcs)
492 goto out;
Gao Xiangcec6e932021-04-07 12:39:24 +0800493
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800494 err = z_erofs_load_cluster_from_disk(m, lcn, false);
Gao Xiangcec6e932021-04-07 12:39:24 +0800495 if (err)
496 return err;
497
Gao Xiang0852b6c2021-05-10 14:47:15 +0800498 /*
499 * If the 1st NONHEAD lcluster has already been handled initially w/o
500 * valid compressedlcs, which means at least it mustn't be CBLKCNT, or
501 * an internal implemenatation error is detected.
502 *
503 * The following code can also handle it properly anyway, but let's
504 * BUG_ON in the debugging mode only for developers to notice that.
505 */
506 DBG_BUGON(lcn == initial_lcn &&
507 m->type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD);
508
Gao Xiangcec6e932021-04-07 12:39:24 +0800509 switch (m->type) {
Gao Xiang0852b6c2021-05-10 14:47:15 +0800510 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
Gao Xiang72bb5262021-10-18 00:57:21 +0800511 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD1:
512 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD2:
Gao Xiang0852b6c2021-05-10 14:47:15 +0800513 /*
514 * if the 1st NONHEAD lcluster is actually PLAIN or HEAD type
515 * rather than CBLKCNT, it's a 1 lcluster-sized pcluster.
516 */
517 m->compressedlcs = 1;
518 break;
Gao Xiangcec6e932021-04-07 12:39:24 +0800519 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
520 if (m->delta[0] != 1)
521 goto err_bonus_cblkcnt;
522 if (m->compressedlcs)
523 break;
524 fallthrough;
525 default:
526 erofs_err(m->inode->i_sb,
527 "cannot found CBLKCNT @ lcn %lu of nid %llu",
528 lcn, vi->nid);
529 DBG_BUGON(1);
530 return -EFSCORRUPTED;
531 }
532out:
533 map->m_plen = m->compressedlcs << lclusterbits;
534 return 0;
535err_bonus_cblkcnt:
536 erofs_err(m->inode->i_sb,
537 "bogus CBLKCNT @ lcn %lu of nid %llu",
538 lcn, vi->nid);
539 DBG_BUGON(1);
540 return -EFSCORRUPTED;
541}
542
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800543static int z_erofs_get_extent_decompressedlen(struct z_erofs_maprecorder *m)
544{
545 struct inode *inode = m->inode;
546 struct erofs_inode *vi = EROFS_I(inode);
547 struct erofs_map_blocks *map = m->map;
548 unsigned int lclusterbits = vi->z_logical_clusterbits;
549 u64 lcn = m->lcn, headlcn = map->m_la >> lclusterbits;
550 int err;
551
552 do {
553 /* handle the last EOF pcluster (no next HEAD lcluster) */
554 if ((lcn << lclusterbits) >= inode->i_size) {
555 map->m_llen = inode->i_size - map->m_la;
556 return 0;
557 }
558
559 err = z_erofs_load_cluster_from_disk(m, lcn, true);
560 if (err)
561 return err;
562
563 if (m->type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) {
564 DBG_BUGON(!m->delta[1] &&
565 m->clusterofs != 1 << lclusterbits);
566 } else if (m->type == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN ||
Gao Xiang72bb5262021-10-18 00:57:21 +0800567 m->type == Z_EROFS_VLE_CLUSTER_TYPE_HEAD1 ||
568 m->type == Z_EROFS_VLE_CLUSTER_TYPE_HEAD2) {
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800569 /* go on until the next HEAD lcluster */
570 if (lcn != headlcn)
571 break;
572 m->delta[1] = 1;
573 } else {
574 erofs_err(inode->i_sb, "unknown type %u @ lcn %llu of nid %llu",
575 m->type, lcn, vi->nid);
576 DBG_BUGON(1);
577 return -EOPNOTSUPP;
578 }
579 lcn += m->delta[1];
580 } while (m->delta[1]);
581
582 map->m_llen = (lcn << lclusterbits) + m->clusterofs - map->m_la;
583 return 0;
584}
585
Gao Xiang152a3332019-06-24 15:22:52 +0800586int z_erofs_map_blocks_iter(struct inode *inode,
587 struct erofs_map_blocks *map,
588 int flags)
589{
Gao Xianga5876e22019-09-04 10:08:56 +0800590 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiang152a3332019-06-24 15:22:52 +0800591 struct z_erofs_maprecorder m = {
592 .inode = inode,
593 .map = map,
594 };
595 int err = 0;
596 unsigned int lclusterbits, endoff;
Gao Xiangcec6e932021-04-07 12:39:24 +0800597 unsigned long initial_lcn;
Gao Xiang152a3332019-06-24 15:22:52 +0800598 unsigned long long ofs, end;
599
600 trace_z_erofs_map_blocks_iter_enter(inode, map, flags);
601
602 /* when trying to read beyond EOF, leave it unmapped */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800603 if (map->m_la >= inode->i_size) {
Gao Xiang152a3332019-06-24 15:22:52 +0800604 map->m_llen = map->m_la + 1 - inode->i_size;
605 map->m_la = inode->i_size;
606 map->m_flags = 0;
607 goto out;
608 }
609
Gao Xiang0c638f72019-11-08 11:37:33 +0800610 err = z_erofs_fill_inode_lazy(inode);
Gao Xiang152a3332019-06-24 15:22:52 +0800611 if (err)
612 goto out;
613
614 lclusterbits = vi->z_logical_clusterbits;
615 ofs = map->m_la;
Gao Xiangcec6e932021-04-07 12:39:24 +0800616 initial_lcn = ofs >> lclusterbits;
Gao Xiang152a3332019-06-24 15:22:52 +0800617 endoff = ofs & ((1 << lclusterbits) - 1);
618
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800619 err = z_erofs_load_cluster_from_disk(&m, initial_lcn, false);
Gao Xiang152a3332019-06-24 15:22:52 +0800620 if (err)
621 goto unmap_out;
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 map->m_pa = blknr_to_addr(m.pblk);
Gao Xiang152a3332019-06-24 15:22:52 +0800663
Gao Xiangcec6e932021-04-07 12:39:24 +0800664 err = z_erofs_get_extent_compressedlen(&m, initial_lcn);
665 if (err)
666 goto out;
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800667
Gao Xiang8f899262021-10-09 04:08:37 +0800668 if (m.headtype == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN)
669 map->m_algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
Gao Xiang72bb5262021-10-18 00:57:21 +0800670 else if (m.headtype == Z_EROFS_VLE_CLUSTER_TYPE_HEAD2)
671 map->m_algorithmformat = vi->z_algorithmtype[1];
Gao Xiang8f899262021-10-09 04:08:37 +0800672 else
673 map->m_algorithmformat = vi->z_algorithmtype[0];
674
Gao Xiang622cead2021-10-11 05:31:45 +0800675 if ((flags & EROFS_GET_BLOCKS_FIEMAP) ||
676 ((flags & EROFS_GET_BLOCKS_READMORE) &&
677 map->m_algorithmformat == Z_EROFS_COMPRESSION_LZMA &&
678 map->m_llen >= EROFS_BLKSIZ)) {
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800679 err = z_erofs_get_extent_decompressedlen(&m);
680 if (!err)
681 map->m_flags |= EROFS_MAP_FULL_MAPPED;
682 }
Gao Xiang152a3332019-06-24 15:22:52 +0800683unmap_out:
684 if (m.kaddr)
685 kunmap_atomic(m.kaddr);
686
687out:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800688 erofs_dbg("%s, m_la %llu m_pa %llu m_llen %llu m_plen %llu m_flags 0%o",
689 __func__, map->m_la, map->m_pa,
690 map->m_llen, map->m_plen, map->m_flags);
Gao Xiang152a3332019-06-24 15:22:52 +0800691
692 trace_z_erofs_map_blocks_iter_exit(inode, map, flags, err);
693
694 /* aggressively BUG_ON iff CONFIG_EROFS_FS_DEBUG is on */
695 DBG_BUGON(err < 0 && err != -ENOMEM);
696 return err;
697}
Gao Xiangeadcd6b2021-08-13 13:29:31 +0800698
699static int z_erofs_iomap_begin_report(struct inode *inode, loff_t offset,
700 loff_t length, unsigned int flags,
701 struct iomap *iomap, struct iomap *srcmap)
702{
703 int ret;
704 struct erofs_map_blocks map = { .m_la = offset };
705
706 ret = z_erofs_map_blocks_iter(inode, &map, EROFS_GET_BLOCKS_FIEMAP);
707 if (map.mpage)
708 put_page(map.mpage);
709 if (ret < 0)
710 return ret;
711
712 iomap->bdev = inode->i_sb->s_bdev;
713 iomap->offset = map.m_la;
714 iomap->length = map.m_llen;
715 if (map.m_flags & EROFS_MAP_MAPPED) {
716 iomap->type = IOMAP_MAPPED;
717 iomap->addr = map.m_pa;
718 } else {
719 iomap->type = IOMAP_HOLE;
720 iomap->addr = IOMAP_NULL_ADDR;
721 /*
722 * No strict rule how to describe extents for post EOF, yet
723 * we need do like below. Otherwise, iomap itself will get
724 * into an endless loop on post EOF.
725 */
726 if (iomap->offset >= inode->i_size)
727 iomap->length = length + map.m_la - offset;
728 }
729 iomap->flags = 0;
730 return 0;
731}
732
733const struct iomap_ops z_erofs_iomap_report_ops = {
734 .iomap_begin = z_erofs_iomap_begin_report,
735};