1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2018-2019 HUAWEI, Inc.
4 * https://www.huawei.com/
5 */
6 #include "internal.h"
7 #include <linux/unaligned.h>
8 #include <trace/events/erofs.h>
9
10 struct z_erofs_maprecorder {
11 struct inode *inode;
12 struct erofs_map_blocks *map;
13 unsigned long lcn;
14 /* compression extent information gathered */
15 u8 type, headtype;
16 u16 clusterofs;
17 u16 delta[2];
18 erofs_blk_t pblk, compressedblks;
19 erofs_off_t nextpackoff;
20 bool partialref;
21 };
22
z_erofs_load_full_lcluster(struct z_erofs_maprecorder * m,unsigned long lcn)23 static int z_erofs_load_full_lcluster(struct z_erofs_maprecorder *m,
24 unsigned long lcn)
25 {
26 struct inode *const inode = m->inode;
27 struct erofs_inode *const vi = EROFS_I(inode);
28 const erofs_off_t pos = Z_EROFS_FULL_INDEX_ALIGN(erofs_iloc(inode) +
29 vi->inode_isize + vi->xattr_isize) +
30 lcn * sizeof(struct z_erofs_lcluster_index);
31 struct z_erofs_lcluster_index *di;
32 unsigned int advise;
33
34 di = erofs_read_metabuf(&m->map->buf, inode->i_sb, pos, EROFS_KMAP);
35 if (IS_ERR(di))
36 return PTR_ERR(di);
37 m->lcn = lcn;
38 m->nextpackoff = pos + sizeof(struct z_erofs_lcluster_index);
39
40 advise = le16_to_cpu(di->di_advise);
41 m->type = advise & Z_EROFS_LI_LCLUSTER_TYPE_MASK;
42 if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
43 m->clusterofs = 1 << vi->z_logical_clusterbits;
44 m->delta[0] = le16_to_cpu(di->di_u.delta[0]);
45 if (m->delta[0] & Z_EROFS_LI_D0_CBLKCNT) {
46 if (!(vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 |
47 Z_EROFS_ADVISE_BIG_PCLUSTER_2))) {
48 DBG_BUGON(1);
49 return -EFSCORRUPTED;
50 }
51 m->compressedblks = m->delta[0] & ~Z_EROFS_LI_D0_CBLKCNT;
52 m->delta[0] = 1;
53 }
54 m->delta[1] = le16_to_cpu(di->di_u.delta[1]);
55 } else {
56 m->partialref = !!(advise & Z_EROFS_LI_PARTIAL_REF);
57 m->clusterofs = le16_to_cpu(di->di_clusterofs);
58 if (m->clusterofs >= 1 << vi->z_logical_clusterbits) {
59 DBG_BUGON(1);
60 return -EFSCORRUPTED;
61 }
62 m->pblk = le32_to_cpu(di->di_u.blkaddr);
63 }
64 return 0;
65 }
66
decode_compactedbits(unsigned int lobits,u8 * in,unsigned int pos,u8 * type)67 static unsigned int decode_compactedbits(unsigned int lobits,
68 u8 *in, unsigned int pos, u8 *type)
69 {
70 const unsigned int v = get_unaligned_le32(in + pos / 8) >> (pos & 7);
71 const unsigned int lo = v & ((1 << lobits) - 1);
72
73 *type = (v >> lobits) & 3;
74 return lo;
75 }
76
get_compacted_la_distance(unsigned int lobits,unsigned int encodebits,unsigned int vcnt,u8 * in,int i)77 static int get_compacted_la_distance(unsigned int lobits,
78 unsigned int encodebits,
79 unsigned int vcnt, u8 *in, int i)
80 {
81 unsigned int lo, d1 = 0;
82 u8 type;
83
84 DBG_BUGON(i >= vcnt);
85
86 do {
87 lo = decode_compactedbits(lobits, in, encodebits * i, &type);
88
89 if (type != Z_EROFS_LCLUSTER_TYPE_NONHEAD)
90 return d1;
91 ++d1;
92 } while (++i < vcnt);
93
94 /* vcnt - 1 (Z_EROFS_LCLUSTER_TYPE_NONHEAD) item */
95 if (!(lo & Z_EROFS_LI_D0_CBLKCNT))
96 d1 += lo - 1;
97 return d1;
98 }
99
z_erofs_load_compact_lcluster(struct z_erofs_maprecorder * m,unsigned long lcn,bool lookahead)100 static int z_erofs_load_compact_lcluster(struct z_erofs_maprecorder *m,
101 unsigned long lcn, bool lookahead)
102 {
103 struct inode *const inode = m->inode;
104 struct erofs_inode *const vi = EROFS_I(inode);
105 const erofs_off_t ebase = sizeof(struct z_erofs_map_header) +
106 ALIGN(erofs_iloc(inode) + vi->inode_isize + vi->xattr_isize, 8);
107 const unsigned int lclusterbits = vi->z_logical_clusterbits;
108 const unsigned int totalidx = erofs_iblks(inode);
109 unsigned int compacted_4b_initial, compacted_2b, amortizedshift;
110 unsigned int vcnt, lo, lobits, encodebits, nblk, bytes;
111 bool big_pcluster = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1;
112 erofs_off_t pos;
113 u8 *in, type;
114 int i;
115
116 if (lcn >= totalidx || lclusterbits > 14)
117 return -EINVAL;
118
119 m->lcn = lcn;
120 /* used to align to 32-byte (compacted_2b) alignment */
121 compacted_4b_initial = ((32 - ebase % 32) / 4) & 7;
122 compacted_2b = 0;
123 if ((vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B) &&
124 compacted_4b_initial < totalidx)
125 compacted_2b = rounddown(totalidx - compacted_4b_initial, 16);
126
127 pos = ebase;
128 amortizedshift = 2; /* compact_4b */
129 if (lcn >= compacted_4b_initial) {
130 pos += compacted_4b_initial * 4;
131 lcn -= compacted_4b_initial;
132 if (lcn < compacted_2b) {
133 amortizedshift = 1;
134 } else {
135 pos += compacted_2b * 2;
136 lcn -= compacted_2b;
137 }
138 }
139 pos += lcn * (1 << amortizedshift);
140
141 /* figure out the lcluster count in this pack */
142 if (1 << amortizedshift == 4 && lclusterbits <= 14)
143 vcnt = 2;
144 else if (1 << amortizedshift == 2 && lclusterbits <= 12)
145 vcnt = 16;
146 else
147 return -EOPNOTSUPP;
148
149 in = erofs_read_metabuf(&m->map->buf, m->inode->i_sb, pos, EROFS_KMAP);
150 if (IS_ERR(in))
151 return PTR_ERR(in);
152
153 /* it doesn't equal to round_up(..) */
154 m->nextpackoff = round_down(pos, vcnt << amortizedshift) +
155 (vcnt << amortizedshift);
156 lobits = max(lclusterbits, ilog2(Z_EROFS_LI_D0_CBLKCNT) + 1U);
157 encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt;
158 bytes = pos & ((vcnt << amortizedshift) - 1);
159 in -= bytes;
160 i = bytes >> amortizedshift;
161
162 lo = decode_compactedbits(lobits, in, encodebits * i, &type);
163 m->type = type;
164 if (type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
165 m->clusterofs = 1 << lclusterbits;
166
167 /* figure out lookahead_distance: delta[1] if needed */
168 if (lookahead)
169 m->delta[1] = get_compacted_la_distance(lobits,
170 encodebits, vcnt, in, i);
171 if (lo & Z_EROFS_LI_D0_CBLKCNT) {
172 if (!big_pcluster) {
173 DBG_BUGON(1);
174 return -EFSCORRUPTED;
175 }
176 m->compressedblks = lo & ~Z_EROFS_LI_D0_CBLKCNT;
177 m->delta[0] = 1;
178 return 0;
179 } else if (i + 1 != (int)vcnt) {
180 m->delta[0] = lo;
181 return 0;
182 }
183 /*
184 * since the last lcluster in the pack is special,
185 * of which lo saves delta[1] rather than delta[0].
186 * Hence, get delta[0] by the previous lcluster indirectly.
187 */
188 lo = decode_compactedbits(lobits, in,
189 encodebits * (i - 1), &type);
190 if (type != Z_EROFS_LCLUSTER_TYPE_NONHEAD)
191 lo = 0;
192 else if (lo & Z_EROFS_LI_D0_CBLKCNT)
193 lo = 1;
194 m->delta[0] = lo + 1;
195 return 0;
196 }
197 m->clusterofs = lo;
198 m->delta[0] = 0;
199 /* figout out blkaddr (pblk) for HEAD lclusters */
200 if (!big_pcluster) {
201 nblk = 1;
202 while (i > 0) {
203 --i;
204 lo = decode_compactedbits(lobits, in,
205 encodebits * i, &type);
206 if (type == Z_EROFS_LCLUSTER_TYPE_NONHEAD)
207 i -= lo;
208
209 if (i >= 0)
210 ++nblk;
211 }
212 } else {
213 nblk = 0;
214 while (i > 0) {
215 --i;
216 lo = decode_compactedbits(lobits, in,
217 encodebits * i, &type);
218 if (type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
219 if (lo & Z_EROFS_LI_D0_CBLKCNT) {
220 --i;
221 nblk += lo & ~Z_EROFS_LI_D0_CBLKCNT;
222 continue;
223 }
224 /* bigpcluster shouldn't have plain d0 == 1 */
225 if (lo <= 1) {
226 DBG_BUGON(1);
227 return -EFSCORRUPTED;
228 }
229 i -= lo - 2;
230 continue;
231 }
232 ++nblk;
233 }
234 }
235 in += (vcnt << amortizedshift) - sizeof(__le32);
236 m->pblk = le32_to_cpu(*(__le32 *)in) + nblk;
237 return 0;
238 }
239
z_erofs_load_lcluster_from_disk(struct z_erofs_maprecorder * m,unsigned int lcn,bool lookahead)240 static int z_erofs_load_lcluster_from_disk(struct z_erofs_maprecorder *m,
241 unsigned int lcn, bool lookahead)
242 {
243 switch (EROFS_I(m->inode)->datalayout) {
244 case EROFS_INODE_COMPRESSED_FULL:
245 return z_erofs_load_full_lcluster(m, lcn);
246 case EROFS_INODE_COMPRESSED_COMPACT:
247 return z_erofs_load_compact_lcluster(m, lcn, lookahead);
248 default:
249 return -EINVAL;
250 }
251 }
252
z_erofs_extent_lookback(struct z_erofs_maprecorder * m,unsigned int lookback_distance)253 static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m,
254 unsigned int lookback_distance)
255 {
256 struct super_block *sb = m->inode->i_sb;
257 struct erofs_inode *const vi = EROFS_I(m->inode);
258 const unsigned int lclusterbits = vi->z_logical_clusterbits;
259
260 while (m->lcn >= lookback_distance) {
261 unsigned long lcn = m->lcn - lookback_distance;
262 int err;
263
264 err = z_erofs_load_lcluster_from_disk(m, lcn, false);
265 if (err)
266 return err;
267
268 if (m->type >= Z_EROFS_LCLUSTER_TYPE_MAX) {
269 erofs_err(sb, "unknown type %u @ lcn %lu of nid %llu",
270 m->type, lcn, vi->nid);
271 DBG_BUGON(1);
272 return -EOPNOTSUPP;
273 } else if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
274 lookback_distance = m->delta[0];
275 if (!lookback_distance)
276 break;
277 continue;
278 } else {
279 m->headtype = m->type;
280 m->map->m_la = (lcn << lclusterbits) | m->clusterofs;
281 return 0;
282 }
283 }
284 erofs_err(sb, "bogus lookback distance %u @ lcn %lu of nid %llu",
285 lookback_distance, m->lcn, vi->nid);
286 DBG_BUGON(1);
287 return -EFSCORRUPTED;
288 }
289
z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder * m,unsigned int initial_lcn)290 static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m,
291 unsigned int initial_lcn)
292 {
293 struct inode *inode = m->inode;
294 struct super_block *sb = inode->i_sb;
295 struct erofs_inode *vi = EROFS_I(inode);
296 bool bigpcl1 = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1;
297 bool bigpcl2 = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2;
298 unsigned long lcn = m->lcn + 1;
299 int err;
300
301 DBG_BUGON(m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD);
302 DBG_BUGON(m->type != m->headtype);
303
304 if ((m->headtype == Z_EROFS_LCLUSTER_TYPE_HEAD1 && !bigpcl1) ||
305 ((m->headtype == Z_EROFS_LCLUSTER_TYPE_PLAIN ||
306 m->headtype == Z_EROFS_LCLUSTER_TYPE_HEAD2) && !bigpcl2) ||
307 (lcn << vi->z_logical_clusterbits) >= inode->i_size)
308 m->compressedblks = 1;
309
310 if (m->compressedblks)
311 goto out;
312
313 err = z_erofs_load_lcluster_from_disk(m, lcn, false);
314 if (err)
315 return err;
316
317 /*
318 * If the 1st NONHEAD lcluster has already been handled initially w/o
319 * valid compressedblks, which means at least it mustn't be CBLKCNT, or
320 * an internal implemenatation error is detected.
321 *
322 * The following code can also handle it properly anyway, but let's
323 * BUG_ON in the debugging mode only for developers to notice that.
324 */
325 DBG_BUGON(lcn == initial_lcn &&
326 m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD);
327
328 if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
329 if (m->delta[0] != 1) {
330 erofs_err(sb, "bogus CBLKCNT @ lcn %lu of nid %llu", lcn, vi->nid);
331 DBG_BUGON(1);
332 return -EFSCORRUPTED;
333 }
334 if (m->compressedblks)
335 goto out;
336 } else if (m->type < Z_EROFS_LCLUSTER_TYPE_MAX) {
337 /*
338 * if the 1st NONHEAD lcluster is actually PLAIN or HEAD type
339 * rather than CBLKCNT, it's a 1 block-sized pcluster.
340 */
341 m->compressedblks = 1;
342 goto out;
343 }
344 erofs_err(sb, "cannot found CBLKCNT @ lcn %lu of nid %llu", lcn, vi->nid);
345 DBG_BUGON(1);
346 return -EFSCORRUPTED;
347 out:
348 m->map->m_plen = erofs_pos(sb, m->compressedblks);
349 return 0;
350 }
351
z_erofs_get_extent_decompressedlen(struct z_erofs_maprecorder * m)352 static int z_erofs_get_extent_decompressedlen(struct z_erofs_maprecorder *m)
353 {
354 struct inode *inode = m->inode;
355 struct erofs_inode *vi = EROFS_I(inode);
356 struct erofs_map_blocks *map = m->map;
357 unsigned int lclusterbits = vi->z_logical_clusterbits;
358 u64 lcn = m->lcn, headlcn = map->m_la >> lclusterbits;
359 int err;
360
361 while (1) {
362 /* handle the last EOF pcluster (no next HEAD lcluster) */
363 if ((lcn << lclusterbits) >= inode->i_size) {
364 map->m_llen = inode->i_size - map->m_la;
365 return 0;
366 }
367
368 err = z_erofs_load_lcluster_from_disk(m, lcn, true);
369 if (err)
370 return err;
371
372 if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
373 /* work around invalid d1 generated by pre-1.0 mkfs */
374 if (unlikely(!m->delta[1])) {
375 m->delta[1] = 1;
376 DBG_BUGON(1);
377 }
378 } else if (m->type < Z_EROFS_LCLUSTER_TYPE_MAX) {
379 if (lcn != headlcn)
380 break; /* ends at the next HEAD lcluster */
381 m->delta[1] = 1;
382 } else {
383 erofs_err(inode->i_sb, "unknown type %u @ lcn %llu of nid %llu",
384 m->type, lcn, vi->nid);
385 DBG_BUGON(1);
386 return -EOPNOTSUPP;
387 }
388 lcn += m->delta[1];
389 }
390 map->m_llen = (lcn << lclusterbits) + m->clusterofs - map->m_la;
391 return 0;
392 }
393
z_erofs_do_map_blocks(struct inode * inode,struct erofs_map_blocks * map,int flags)394 static int z_erofs_do_map_blocks(struct inode *inode,
395 struct erofs_map_blocks *map, int flags)
396 {
397 struct erofs_inode *vi = EROFS_I(inode);
398 struct super_block *sb = inode->i_sb;
399 bool fragment = vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER;
400 bool ztailpacking = vi->z_idata_size;
401 struct z_erofs_maprecorder m = {
402 .inode = inode,
403 .map = map,
404 };
405 int err = 0;
406 unsigned int lclusterbits, endoff, afmt;
407 unsigned long initial_lcn;
408 unsigned long long ofs, end;
409
410 lclusterbits = vi->z_logical_clusterbits;
411 ofs = flags & EROFS_GET_BLOCKS_FINDTAIL ? inode->i_size - 1 : map->m_la;
412 initial_lcn = ofs >> lclusterbits;
413 endoff = ofs & ((1 << lclusterbits) - 1);
414
415 err = z_erofs_load_lcluster_from_disk(&m, initial_lcn, false);
416 if (err)
417 goto unmap_out;
418
419 if ((flags & EROFS_GET_BLOCKS_FINDTAIL) && ztailpacking)
420 vi->z_fragmentoff = m.nextpackoff;
421 map->m_flags = EROFS_MAP_MAPPED | EROFS_MAP_ENCODED;
422 end = (m.lcn + 1ULL) << lclusterbits;
423
424 switch (m.type) {
425 case Z_EROFS_LCLUSTER_TYPE_PLAIN:
426 case Z_EROFS_LCLUSTER_TYPE_HEAD1:
427 case Z_EROFS_LCLUSTER_TYPE_HEAD2:
428 if (endoff >= m.clusterofs) {
429 m.headtype = m.type;
430 map->m_la = (m.lcn << lclusterbits) | m.clusterofs;
431 /*
432 * For ztailpacking files, in order to inline data more
433 * effectively, special EOF lclusters are now supported
434 * which can have three parts at most.
435 */
436 if (ztailpacking && end > inode->i_size)
437 end = inode->i_size;
438 break;
439 }
440 /* m.lcn should be >= 1 if endoff < m.clusterofs */
441 if (!m.lcn) {
442 erofs_err(sb, "invalid logical cluster 0 at nid %llu",
443 vi->nid);
444 err = -EFSCORRUPTED;
445 goto unmap_out;
446 }
447 end = (m.lcn << lclusterbits) | m.clusterofs;
448 map->m_flags |= EROFS_MAP_FULL_MAPPED;
449 m.delta[0] = 1;
450 fallthrough;
451 case Z_EROFS_LCLUSTER_TYPE_NONHEAD:
452 /* get the corresponding first chunk */
453 err = z_erofs_extent_lookback(&m, m.delta[0]);
454 if (err)
455 goto unmap_out;
456 break;
457 default:
458 erofs_err(sb, "unknown type %u @ offset %llu of nid %llu",
459 m.type, ofs, vi->nid);
460 err = -EOPNOTSUPP;
461 goto unmap_out;
462 }
463 if (m.partialref)
464 map->m_flags |= EROFS_MAP_PARTIAL_REF;
465 map->m_llen = end - map->m_la;
466
467 if (flags & EROFS_GET_BLOCKS_FINDTAIL) {
468 vi->z_tailextent_headlcn = m.lcn;
469 /* for non-compact indexes, fragmentoff is 64 bits */
470 if (fragment && vi->datalayout == EROFS_INODE_COMPRESSED_FULL)
471 vi->z_fragmentoff |= (u64)m.pblk << 32;
472 }
473 if (ztailpacking && m.lcn == vi->z_tailextent_headlcn) {
474 map->m_flags |= EROFS_MAP_META;
475 map->m_pa = vi->z_fragmentoff;
476 map->m_plen = vi->z_idata_size;
477 if (erofs_blkoff(sb, map->m_pa) + map->m_plen > sb->s_blocksize) {
478 erofs_err(sb, "invalid tail-packing pclustersize %llu",
479 map->m_plen);
480 err = -EFSCORRUPTED;
481 goto unmap_out;
482 }
483 } else if (fragment && m.lcn == vi->z_tailextent_headlcn) {
484 map->m_flags = EROFS_MAP_FRAGMENT;
485 } else {
486 map->m_pa = erofs_pos(sb, m.pblk);
487 err = z_erofs_get_extent_compressedlen(&m, initial_lcn);
488 if (err)
489 goto unmap_out;
490 }
491
492 if (m.headtype == Z_EROFS_LCLUSTER_TYPE_PLAIN) {
493 if (map->m_llen > map->m_plen) {
494 DBG_BUGON(1);
495 err = -EFSCORRUPTED;
496 goto unmap_out;
497 }
498 afmt = vi->z_advise & Z_EROFS_ADVISE_INTERLACED_PCLUSTER ?
499 Z_EROFS_COMPRESSION_INTERLACED :
500 Z_EROFS_COMPRESSION_SHIFTED;
501 } else {
502 afmt = m.headtype == Z_EROFS_LCLUSTER_TYPE_HEAD2 ?
503 vi->z_algorithmtype[1] : vi->z_algorithmtype[0];
504 if (!(EROFS_I_SB(inode)->available_compr_algs & (1 << afmt))) {
505 erofs_err(sb, "inconsistent algorithmtype %u for nid %llu",
506 afmt, vi->nid);
507 err = -EFSCORRUPTED;
508 goto unmap_out;
509 }
510 }
511 map->m_algorithmformat = afmt;
512
513 if ((flags & EROFS_GET_BLOCKS_FIEMAP) ||
514 ((flags & EROFS_GET_BLOCKS_READMORE) &&
515 (map->m_algorithmformat == Z_EROFS_COMPRESSION_LZMA ||
516 map->m_algorithmformat == Z_EROFS_COMPRESSION_DEFLATE ||
517 map->m_algorithmformat == Z_EROFS_COMPRESSION_ZSTD) &&
518 map->m_llen >= i_blocksize(inode))) {
519 err = z_erofs_get_extent_decompressedlen(&m);
520 if (!err)
521 map->m_flags |= EROFS_MAP_FULL_MAPPED;
522 }
523
524 unmap_out:
525 erofs_unmap_metabuf(&m.map->buf);
526 return err;
527 }
528
z_erofs_fill_inode_lazy(struct inode * inode)529 static int z_erofs_fill_inode_lazy(struct inode *inode)
530 {
531 struct erofs_inode *const vi = EROFS_I(inode);
532 struct super_block *const sb = inode->i_sb;
533 int err, headnr;
534 erofs_off_t pos;
535 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
536 struct z_erofs_map_header *h;
537
538 if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags)) {
539 /*
540 * paired with smp_mb() at the end of the function to ensure
541 * fields will only be observed after the bit is set.
542 */
543 smp_mb();
544 return 0;
545 }
546
547 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_Z_BIT, TASK_KILLABLE))
548 return -ERESTARTSYS;
549
550 err = 0;
551 if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags))
552 goto out_unlock;
553
554 pos = ALIGN(erofs_iloc(inode) + vi->inode_isize + vi->xattr_isize, 8);
555 h = erofs_read_metabuf(&buf, sb, pos, EROFS_KMAP);
556 if (IS_ERR(h)) {
557 err = PTR_ERR(h);
558 goto out_unlock;
559 }
560
561 /*
562 * if the highest bit of the 8-byte map header is set, the whole file
563 * is stored in the packed inode. The rest bits keeps z_fragmentoff.
564 */
565 if (h->h_clusterbits >> Z_EROFS_FRAGMENT_INODE_BIT) {
566 vi->z_advise = Z_EROFS_ADVISE_FRAGMENT_PCLUSTER;
567 vi->z_fragmentoff = le64_to_cpu(*(__le64 *)h) ^ (1ULL << 63);
568 vi->z_tailextent_headlcn = 0;
569 goto done;
570 }
571 vi->z_advise = le16_to_cpu(h->h_advise);
572 vi->z_algorithmtype[0] = h->h_algorithmtype & 15;
573 vi->z_algorithmtype[1] = h->h_algorithmtype >> 4;
574 if (vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER)
575 vi->z_fragmentoff = le32_to_cpu(h->h_fragmentoff);
576 else if (vi->z_advise & Z_EROFS_ADVISE_INLINE_PCLUSTER)
577 vi->z_idata_size = le16_to_cpu(h->h_idata_size);
578
579 headnr = 0;
580 if (vi->z_algorithmtype[0] >= Z_EROFS_COMPRESSION_MAX ||
581 vi->z_algorithmtype[++headnr] >= Z_EROFS_COMPRESSION_MAX) {
582 erofs_err(sb, "unknown HEAD%u format %u for nid %llu, please upgrade kernel",
583 headnr + 1, vi->z_algorithmtype[headnr], vi->nid);
584 err = -EOPNOTSUPP;
585 goto out_put_metabuf;
586 }
587
588 vi->z_logical_clusterbits = sb->s_blocksize_bits + (h->h_clusterbits & 7);
589 if (!erofs_sb_has_big_pcluster(EROFS_SB(sb)) &&
590 vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 |
591 Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
592 erofs_err(sb, "per-inode big pcluster without sb feature for nid %llu",
593 vi->nid);
594 err = -EFSCORRUPTED;
595 goto out_put_metabuf;
596 }
597 if (vi->datalayout == EROFS_INODE_COMPRESSED_COMPACT &&
598 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1) ^
599 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
600 erofs_err(sb, "big pcluster head1/2 of compact indexes should be consistent for nid %llu",
601 vi->nid);
602 err = -EFSCORRUPTED;
603 goto out_put_metabuf;
604 }
605
606 if (vi->z_idata_size ||
607 (vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER)) {
608 struct erofs_map_blocks map = {
609 .buf = __EROFS_BUF_INITIALIZER
610 };
611
612 err = z_erofs_do_map_blocks(inode, &map,
613 EROFS_GET_BLOCKS_FINDTAIL);
614 erofs_put_metabuf(&map.buf);
615 if (err < 0)
616 goto out_put_metabuf;
617 }
618 done:
619 /* paired with smp_mb() at the beginning of the function */
620 smp_mb();
621 set_bit(EROFS_I_Z_INITED_BIT, &vi->flags);
622 out_put_metabuf:
623 erofs_put_metabuf(&buf);
624 out_unlock:
625 clear_and_wake_up_bit(EROFS_I_BL_Z_BIT, &vi->flags);
626 return err;
627 }
628
z_erofs_map_blocks_iter(struct inode * inode,struct erofs_map_blocks * map,int flags)629 int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
630 int flags)
631 {
632 struct erofs_inode *const vi = EROFS_I(inode);
633 int err = 0;
634
635 trace_erofs_map_blocks_enter(inode, map, flags);
636 if (map->m_la >= inode->i_size) { /* post-EOF unmapped extent */
637 map->m_llen = map->m_la + 1 - inode->i_size;
638 map->m_la = inode->i_size;
639 map->m_flags = 0;
640 } else {
641 err = z_erofs_fill_inode_lazy(inode);
642 if (!err) {
643 if ((vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER) &&
644 !vi->z_tailextent_headlcn) {
645 map->m_la = 0;
646 map->m_llen = inode->i_size;
647 map->m_flags = EROFS_MAP_FRAGMENT;
648 } else {
649 err = z_erofs_do_map_blocks(inode, map, flags);
650 }
651 }
652 if (!err && (map->m_flags & EROFS_MAP_ENCODED) &&
653 unlikely(map->m_plen > Z_EROFS_PCLUSTER_MAX_SIZE ||
654 map->m_llen > Z_EROFS_PCLUSTER_MAX_DSIZE))
655 err = -EOPNOTSUPP;
656 if (err)
657 map->m_llen = 0;
658 }
659 trace_erofs_map_blocks_exit(inode, map, flags, err);
660 return err;
661 }
662
z_erofs_iomap_begin_report(struct inode * inode,loff_t offset,loff_t length,unsigned int flags,struct iomap * iomap,struct iomap * srcmap)663 static int z_erofs_iomap_begin_report(struct inode *inode, loff_t offset,
664 loff_t length, unsigned int flags,
665 struct iomap *iomap, struct iomap *srcmap)
666 {
667 int ret;
668 struct erofs_map_blocks map = { .m_la = offset };
669
670 ret = z_erofs_map_blocks_iter(inode, &map, EROFS_GET_BLOCKS_FIEMAP);
671 erofs_put_metabuf(&map.buf);
672 if (ret < 0)
673 return ret;
674
675 iomap->bdev = inode->i_sb->s_bdev;
676 iomap->offset = map.m_la;
677 iomap->length = map.m_llen;
678 if (map.m_flags & EROFS_MAP_MAPPED) {
679 iomap->type = IOMAP_MAPPED;
680 iomap->addr = map.m_flags & __EROFS_MAP_FRAGMENT ?
681 IOMAP_NULL_ADDR : map.m_pa;
682 } else {
683 iomap->type = IOMAP_HOLE;
684 iomap->addr = IOMAP_NULL_ADDR;
685 /*
686 * No strict rule on how to describe extents for post EOF, yet
687 * we need to do like below. Otherwise, iomap itself will get
688 * into an endless loop on post EOF.
689 *
690 * Calculate the effective offset by subtracting extent start
691 * (map.m_la) from the requested offset, and add it to length.
692 * (NB: offset >= map.m_la always)
693 */
694 if (iomap->offset >= inode->i_size)
695 iomap->length = length + offset - map.m_la;
696 }
697 iomap->flags = 0;
698 return 0;
699 }
700
701 const struct iomap_ops z_erofs_iomap_report_ops = {
702 .iomap_begin = z_erofs_iomap_begin_report,
703 };
704