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, in_mbox;
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_START(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, m->in_mbox);
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_lclusterbits;
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 m->pblk = le32_to_cpu(di->di_u.blkaddr);
59 }
60 return 0;
61 }
62
decode_compactedbits(unsigned int lobits,u8 * in,unsigned int pos,u8 * type)63 static unsigned int decode_compactedbits(unsigned int lobits,
64 u8 *in, unsigned int pos, u8 *type)
65 {
66 const unsigned int v = get_unaligned_le32(in + pos / 8) >> (pos & 7);
67 const unsigned int lo = v & ((1 << lobits) - 1);
68
69 *type = (v >> lobits) & 3;
70 return lo;
71 }
72
get_compacted_la_distance(unsigned int lobits,unsigned int encodebits,unsigned int vcnt,u8 * in,int i)73 static int get_compacted_la_distance(unsigned int lobits,
74 unsigned int encodebits,
75 unsigned int vcnt, u8 *in, int i)
76 {
77 unsigned int lo, d1 = 0;
78 u8 type;
79
80 DBG_BUGON(i >= vcnt);
81
82 do {
83 lo = decode_compactedbits(lobits, in, encodebits * i, &type);
84
85 if (type != Z_EROFS_LCLUSTER_TYPE_NONHEAD)
86 return d1;
87 ++d1;
88 } while (++i < vcnt);
89
90 /* vcnt - 1 (Z_EROFS_LCLUSTER_TYPE_NONHEAD) item */
91 if (!(lo & Z_EROFS_LI_D0_CBLKCNT))
92 d1 += lo - 1;
93 return d1;
94 }
95
z_erofs_load_compact_lcluster(struct z_erofs_maprecorder * m,unsigned long lcn,bool lookahead)96 static int z_erofs_load_compact_lcluster(struct z_erofs_maprecorder *m,
97 unsigned long lcn, bool lookahead)
98 {
99 struct inode *const inode = m->inode;
100 struct erofs_inode *const vi = EROFS_I(inode);
101 const erofs_off_t ebase = Z_EROFS_MAP_HEADER_END(erofs_iloc(inode) +
102 vi->inode_isize + vi->xattr_isize);
103 const unsigned int lclusterbits = vi->z_lclusterbits;
104 const unsigned int totalidx = erofs_iblks(inode);
105 unsigned int compacted_4b_initial, compacted_2b, amortizedshift;
106 unsigned int vcnt, lo, lobits, encodebits, nblk, bytes;
107 bool big_pcluster = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1;
108 erofs_off_t pos;
109 u8 *in, type;
110 int i;
111
112 if (lcn >= totalidx || lclusterbits > 14)
113 return -EINVAL;
114
115 m->lcn = lcn;
116 /* used to align to 32-byte (compacted_2b) alignment */
117 compacted_4b_initial = ((32 - ebase % 32) / 4) & 7;
118 compacted_2b = 0;
119 if ((vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B) &&
120 compacted_4b_initial < totalidx)
121 compacted_2b = rounddown(totalidx - compacted_4b_initial, 16);
122
123 pos = ebase;
124 amortizedshift = 2; /* compact_4b */
125 if (lcn >= compacted_4b_initial) {
126 pos += compacted_4b_initial * 4;
127 lcn -= compacted_4b_initial;
128 if (lcn < compacted_2b) {
129 amortizedshift = 1;
130 } else {
131 pos += compacted_2b * 2;
132 lcn -= compacted_2b;
133 }
134 }
135 pos += lcn * (1 << amortizedshift);
136
137 /* figure out the lcluster count in this pack */
138 if (1 << amortizedshift == 4 && lclusterbits <= 14)
139 vcnt = 2;
140 else if (1 << amortizedshift == 2 && lclusterbits <= 12)
141 vcnt = 16;
142 else
143 return -EOPNOTSUPP;
144
145 in = erofs_read_metabuf(&m->map->buf, inode->i_sb, pos, m->in_mbox);
146 if (IS_ERR(in))
147 return PTR_ERR(in);
148
149 /* it doesn't equal to round_up(..) */
150 m->nextpackoff = round_down(pos, vcnt << amortizedshift) +
151 (vcnt << amortizedshift);
152 lobits = max(lclusterbits, ilog2(Z_EROFS_LI_D0_CBLKCNT) + 1U);
153 encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt;
154 bytes = pos & ((vcnt << amortizedshift) - 1);
155 in -= bytes;
156 i = bytes >> amortizedshift;
157
158 lo = decode_compactedbits(lobits, in, encodebits * i, &type);
159 m->type = type;
160 if (type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
161 m->clusterofs = 1 << lclusterbits;
162
163 /* figure out lookahead_distance: delta[1] if needed */
164 if (lookahead)
165 m->delta[1] = get_compacted_la_distance(lobits,
166 encodebits, vcnt, in, i);
167 if (lo & Z_EROFS_LI_D0_CBLKCNT) {
168 if (!big_pcluster) {
169 DBG_BUGON(1);
170 return -EFSCORRUPTED;
171 }
172 m->compressedblks = lo & ~Z_EROFS_LI_D0_CBLKCNT;
173 m->delta[0] = 1;
174 return 0;
175 } else if (i + 1 != (int)vcnt) {
176 m->delta[0] = lo;
177 return 0;
178 }
179 /*
180 * since the last lcluster in the pack is special,
181 * of which lo saves delta[1] rather than delta[0].
182 * Hence, get delta[0] by the previous lcluster indirectly.
183 */
184 lo = decode_compactedbits(lobits, in,
185 encodebits * (i - 1), &type);
186 if (type != Z_EROFS_LCLUSTER_TYPE_NONHEAD)
187 lo = 0;
188 else if (lo & Z_EROFS_LI_D0_CBLKCNT)
189 lo = 1;
190 m->delta[0] = lo + 1;
191 return 0;
192 }
193 m->clusterofs = lo;
194 m->delta[0] = 0;
195 /* figout out blkaddr (pblk) for HEAD lclusters */
196 if (!big_pcluster) {
197 nblk = 1;
198 while (i > 0) {
199 --i;
200 lo = decode_compactedbits(lobits, in,
201 encodebits * i, &type);
202 if (type == Z_EROFS_LCLUSTER_TYPE_NONHEAD)
203 i -= lo;
204
205 if (i >= 0)
206 ++nblk;
207 }
208 } else {
209 nblk = 0;
210 while (i > 0) {
211 --i;
212 lo = decode_compactedbits(lobits, in,
213 encodebits * i, &type);
214 if (type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
215 if (lo & Z_EROFS_LI_D0_CBLKCNT) {
216 --i;
217 nblk += lo & ~Z_EROFS_LI_D0_CBLKCNT;
218 continue;
219 }
220 /* bigpcluster shouldn't have plain d0 == 1 */
221 if (lo <= 1) {
222 DBG_BUGON(1);
223 return -EFSCORRUPTED;
224 }
225 i -= lo - 2;
226 continue;
227 }
228 ++nblk;
229 }
230 }
231 in += (vcnt << amortizedshift) - sizeof(__le32);
232 m->pblk = le32_to_cpu(*(__le32 *)in) + nblk;
233 return 0;
234 }
235
z_erofs_load_lcluster_from_disk(struct z_erofs_maprecorder * m,unsigned int lcn,bool lookahead)236 static int z_erofs_load_lcluster_from_disk(struct z_erofs_maprecorder *m,
237 unsigned int lcn, bool lookahead)
238 {
239 struct erofs_inode *vi = EROFS_I(m->inode);
240 int err;
241
242 if (vi->datalayout == EROFS_INODE_COMPRESSED_COMPACT) {
243 err = z_erofs_load_compact_lcluster(m, lcn, lookahead);
244 } else {
245 DBG_BUGON(vi->datalayout != EROFS_INODE_COMPRESSED_FULL);
246 err = z_erofs_load_full_lcluster(m, lcn);
247 }
248 if (err)
249 return err;
250
251 if (m->type >= Z_EROFS_LCLUSTER_TYPE_MAX) {
252 erofs_err(m->inode->i_sb, "unknown type %u @ lcn %u of nid %llu",
253 m->type, lcn, EROFS_I(m->inode)->nid);
254 DBG_BUGON(1);
255 return -EOPNOTSUPP;
256 } else if (m->type != Z_EROFS_LCLUSTER_TYPE_NONHEAD &&
257 m->clusterofs >= (1 << vi->z_lclusterbits)) {
258 DBG_BUGON(1);
259 return -EFSCORRUPTED;
260 }
261 return 0;
262 }
263
z_erofs_extent_lookback(struct z_erofs_maprecorder * m,unsigned int lookback_distance)264 static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m,
265 unsigned int lookback_distance)
266 {
267 struct super_block *sb = m->inode->i_sb;
268 struct erofs_inode *const vi = EROFS_I(m->inode);
269 const unsigned int lclusterbits = vi->z_lclusterbits;
270
271 while (m->lcn >= lookback_distance) {
272 unsigned long lcn = m->lcn - lookback_distance;
273 int err;
274
275 if (!lookback_distance)
276 break;
277
278 err = z_erofs_load_lcluster_from_disk(m, lcn, false);
279 if (err)
280 return err;
281 if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
282 lookback_distance = m->delta[0];
283 continue;
284 }
285 m->headtype = m->type;
286 m->map->m_la = (lcn << lclusterbits) | m->clusterofs;
287 return 0;
288 }
289 erofs_err(sb, "bogus lookback distance %u @ lcn %lu of nid %llu",
290 lookback_distance, m->lcn, vi->nid);
291 DBG_BUGON(1);
292 return -EFSCORRUPTED;
293 }
294
z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder * m,unsigned int initial_lcn)295 static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m,
296 unsigned int initial_lcn)
297 {
298 struct inode *inode = m->inode;
299 struct super_block *sb = inode->i_sb;
300 struct erofs_inode *vi = EROFS_I(inode);
301 bool bigpcl1 = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1;
302 bool bigpcl2 = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2;
303 unsigned long lcn = m->lcn + 1;
304 int err;
305
306 DBG_BUGON(m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD);
307 DBG_BUGON(m->type != m->headtype);
308
309 if ((m->headtype == Z_EROFS_LCLUSTER_TYPE_HEAD1 && !bigpcl1) ||
310 ((m->headtype == Z_EROFS_LCLUSTER_TYPE_PLAIN ||
311 m->headtype == Z_EROFS_LCLUSTER_TYPE_HEAD2) && !bigpcl2) ||
312 (lcn << vi->z_lclusterbits) >= inode->i_size)
313 m->compressedblks = 1;
314
315 if (m->compressedblks)
316 goto out;
317
318 err = z_erofs_load_lcluster_from_disk(m, lcn, false);
319 if (err)
320 return err;
321
322 /*
323 * If the 1st NONHEAD lcluster has already been handled initially w/o
324 * valid compressedblks, which means at least it mustn't be CBLKCNT, or
325 * an internal implemenatation error is detected.
326 *
327 * The following code can also handle it properly anyway, but let's
328 * BUG_ON in the debugging mode only for developers to notice that.
329 */
330 DBG_BUGON(lcn == initial_lcn &&
331 m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD);
332
333 if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD && m->delta[0] != 1) {
334 erofs_err(sb, "bogus CBLKCNT @ lcn %lu of nid %llu", lcn, vi->nid);
335 DBG_BUGON(1);
336 return -EFSCORRUPTED;
337 }
338
339 /*
340 * if the 1st NONHEAD lcluster is actually PLAIN or HEAD type rather
341 * than CBLKCNT, it's a 1 block-sized pcluster.
342 */
343 if (m->type != Z_EROFS_LCLUSTER_TYPE_NONHEAD || !m->compressedblks)
344 m->compressedblks = 1;
345 out:
346 m->map->m_plen = erofs_pos(sb, m->compressedblks);
347 return 0;
348 }
349
z_erofs_get_extent_decompressedlen(struct z_erofs_maprecorder * m)350 static int z_erofs_get_extent_decompressedlen(struct z_erofs_maprecorder *m)
351 {
352 struct inode *inode = m->inode;
353 struct erofs_inode *vi = EROFS_I(inode);
354 struct erofs_map_blocks *map = m->map;
355 unsigned int lclusterbits = vi->z_lclusterbits;
356 u64 lcn = m->lcn, headlcn = map->m_la >> lclusterbits;
357 int err;
358
359 while (1) {
360 /* handle the last EOF pcluster (no next HEAD lcluster) */
361 if ((lcn << lclusterbits) >= inode->i_size) {
362 map->m_llen = inode->i_size - map->m_la;
363 return 0;
364 }
365
366 err = z_erofs_load_lcluster_from_disk(m, lcn, true);
367 if (err)
368 return err;
369
370 if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
371 /* work around invalid d1 generated by pre-1.0 mkfs */
372 if (unlikely(!m->delta[1])) {
373 m->delta[1] = 1;
374 DBG_BUGON(1);
375 }
376 } else if (m->type < Z_EROFS_LCLUSTER_TYPE_MAX) {
377 if (lcn != headlcn)
378 break; /* ends at the next HEAD lcluster */
379 m->delta[1] = 1;
380 }
381 lcn += m->delta[1];
382 }
383 map->m_llen = (lcn << lclusterbits) + m->clusterofs - map->m_la;
384 return 0;
385 }
386
z_erofs_map_blocks_fo(struct inode * inode,struct erofs_map_blocks * map,int flags)387 static int z_erofs_map_blocks_fo(struct inode *inode,
388 struct erofs_map_blocks *map, int flags)
389 {
390 struct erofs_inode *vi = EROFS_I(inode);
391 struct super_block *sb = inode->i_sb;
392 bool fragment = vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER;
393 bool ztailpacking = vi->z_idata_size;
394 unsigned int lclusterbits = vi->z_lclusterbits;
395 struct z_erofs_maprecorder m = {
396 .inode = inode,
397 .map = map,
398 .in_mbox = erofs_inode_in_metabox(inode),
399 };
400 unsigned int endoff;
401 unsigned long initial_lcn;
402 unsigned long long ofs, end;
403 int err;
404
405 ofs = flags & EROFS_GET_BLOCKS_FINDTAIL ? inode->i_size - 1 : map->m_la;
406 if (fragment && !(flags & EROFS_GET_BLOCKS_FINDTAIL) &&
407 !vi->z_tailextent_headlcn) {
408 map->m_la = 0;
409 map->m_llen = inode->i_size;
410 map->m_flags = EROFS_MAP_FRAGMENT;
411 return 0;
412 }
413 initial_lcn = ofs >> lclusterbits;
414 endoff = ofs & ((1 << lclusterbits) - 1);
415
416 err = z_erofs_load_lcluster_from_disk(&m, initial_lcn, false);
417 if (err)
418 goto unmap_out;
419
420 if ((flags & EROFS_GET_BLOCKS_FINDTAIL) && ztailpacking)
421 vi->z_fragmentoff = m.nextpackoff;
422 map->m_flags = EROFS_MAP_MAPPED | EROFS_MAP_PARTIAL_MAPPED;
423 end = (m.lcn + 1ULL) << lclusterbits;
424
425 if (m.type != Z_EROFS_LCLUSTER_TYPE_NONHEAD && endoff >= m.clusterofs) {
426 m.headtype = m.type;
427 map->m_la = (m.lcn << lclusterbits) | m.clusterofs;
428 /*
429 * For ztailpacking files, in order to inline data more
430 * effectively, special EOF lclusters are now supported
431 * which can have three parts at most.
432 */
433 if (ztailpacking && end > inode->i_size)
434 end = inode->i_size;
435 } else {
436 if (m.type != Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
437 end = (m.lcn << lclusterbits) | m.clusterofs;
438 map->m_flags &= ~EROFS_MAP_PARTIAL_MAPPED;
439 m.delta[0] = 1;
440 }
441 /* get the corresponding first chunk */
442 err = z_erofs_extent_lookback(&m, m.delta[0]);
443 if (err)
444 goto unmap_out;
445 }
446 if (m.partialref)
447 map->m_flags |= EROFS_MAP_PARTIAL_REF;
448 map->m_llen = end - map->m_la;
449
450 if (flags & EROFS_GET_BLOCKS_FINDTAIL) {
451 vi->z_tailextent_headlcn = m.lcn;
452 /* for non-compact indexes, fragmentoff is 64 bits */
453 if (fragment && vi->datalayout == EROFS_INODE_COMPRESSED_FULL)
454 vi->z_fragmentoff |= (u64)m.pblk << 32;
455 }
456 if (ztailpacking && m.lcn == vi->z_tailextent_headlcn) {
457 map->m_flags |= EROFS_MAP_META;
458 map->m_pa = vi->z_fragmentoff;
459 map->m_plen = vi->z_idata_size;
460 if (erofs_blkoff(sb, map->m_pa) + map->m_plen > sb->s_blocksize) {
461 erofs_err(sb, "ztailpacking inline data across blocks @ nid %llu",
462 vi->nid);
463 err = -EFSCORRUPTED;
464 goto unmap_out;
465 }
466 } else if (fragment && m.lcn == vi->z_tailextent_headlcn) {
467 map->m_flags = EROFS_MAP_FRAGMENT;
468 } else {
469 map->m_pa = erofs_pos(sb, m.pblk);
470 err = z_erofs_get_extent_compressedlen(&m, initial_lcn);
471 if (err)
472 goto unmap_out;
473 }
474
475 if (m.headtype == Z_EROFS_LCLUSTER_TYPE_PLAIN) {
476 if (vi->z_advise & Z_EROFS_ADVISE_INTERLACED_PCLUSTER)
477 map->m_algorithmformat = Z_EROFS_COMPRESSION_INTERLACED;
478 else
479 map->m_algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
480 } else if (m.headtype == Z_EROFS_LCLUSTER_TYPE_HEAD2) {
481 map->m_algorithmformat = vi->z_algorithmtype[1];
482 } else {
483 map->m_algorithmformat = vi->z_algorithmtype[0];
484 }
485
486 if ((flags & EROFS_GET_BLOCKS_FIEMAP) ||
487 ((flags & EROFS_GET_BLOCKS_READMORE) &&
488 (map->m_algorithmformat == Z_EROFS_COMPRESSION_LZMA ||
489 map->m_algorithmformat == Z_EROFS_COMPRESSION_DEFLATE ||
490 map->m_algorithmformat == Z_EROFS_COMPRESSION_ZSTD) &&
491 map->m_llen >= i_blocksize(inode))) {
492 err = z_erofs_get_extent_decompressedlen(&m);
493 if (!err)
494 map->m_flags &= ~EROFS_MAP_PARTIAL_MAPPED;
495 }
496
497 unmap_out:
498 erofs_unmap_metabuf(&m.map->buf);
499 return err;
500 }
501
z_erofs_map_blocks_ext(struct inode * inode,struct erofs_map_blocks * map,int flags)502 static int z_erofs_map_blocks_ext(struct inode *inode,
503 struct erofs_map_blocks *map, int flags)
504 {
505 struct erofs_inode *vi = EROFS_I(inode);
506 struct super_block *sb = inode->i_sb;
507 bool interlaced = vi->z_advise & Z_EROFS_ADVISE_INTERLACED_PCLUSTER;
508 unsigned int recsz = z_erofs_extent_recsize(vi->z_advise);
509 erofs_off_t pos = round_up(Z_EROFS_MAP_HEADER_END(erofs_iloc(inode) +
510 vi->inode_isize + vi->xattr_isize), recsz);
511 unsigned int bmask = sb->s_blocksize - 1;
512 bool in_mbox = erofs_inode_in_metabox(inode);
513 erofs_off_t lend = inode->i_size;
514 erofs_off_t l, r, mid, pa, la, lstart;
515 struct z_erofs_extent *ext;
516 unsigned int fmt;
517 bool last;
518
519 map->m_flags = 0;
520 if (recsz <= offsetof(struct z_erofs_extent, pstart_hi)) {
521 if (recsz <= offsetof(struct z_erofs_extent, pstart_lo)) {
522 ext = erofs_read_metabuf(&map->buf, sb, pos, in_mbox);
523 if (IS_ERR(ext))
524 return PTR_ERR(ext);
525 pa = le64_to_cpu(*(__le64 *)ext);
526 pos += sizeof(__le64);
527 lstart = 0;
528 } else {
529 lstart = round_down(map->m_la, 1 << vi->z_lclusterbits);
530 pos += (lstart >> vi->z_lclusterbits) * recsz;
531 pa = EROFS_NULL_ADDR;
532 }
533
534 for (; lstart <= map->m_la; lstart += 1 << vi->z_lclusterbits) {
535 ext = erofs_read_metabuf(&map->buf, sb, pos, in_mbox);
536 if (IS_ERR(ext))
537 return PTR_ERR(ext);
538 map->m_plen = le32_to_cpu(ext->plen);
539 if (pa != EROFS_NULL_ADDR) {
540 map->m_pa = pa;
541 pa += map->m_plen & Z_EROFS_EXTENT_PLEN_MASK;
542 } else {
543 map->m_pa = le32_to_cpu(ext->pstart_lo);
544 }
545 pos += recsz;
546 }
547 last = (lstart >= round_up(lend, 1 << vi->z_lclusterbits));
548 lend = min(lstart, lend);
549 lstart -= 1 << vi->z_lclusterbits;
550 } else {
551 lstart = lend;
552 for (l = 0, r = vi->z_extents; l < r; ) {
553 mid = l + (r - l) / 2;
554 ext = erofs_read_metabuf(&map->buf, sb,
555 pos + mid * recsz, in_mbox);
556 if (IS_ERR(ext))
557 return PTR_ERR(ext);
558
559 la = le32_to_cpu(ext->lstart_lo);
560 pa = le32_to_cpu(ext->pstart_lo) |
561 (u64)le32_to_cpu(ext->pstart_hi) << 32;
562 if (recsz > offsetof(struct z_erofs_extent, lstart_hi))
563 la |= (u64)le32_to_cpu(ext->lstart_hi) << 32;
564
565 if (la > map->m_la) {
566 r = mid;
567 if (la > lend) {
568 DBG_BUGON(1);
569 return -EFSCORRUPTED;
570 }
571 lend = la;
572 } else {
573 l = mid + 1;
574 if (map->m_la == la)
575 r = min(l + 1, r);
576 lstart = la;
577 map->m_plen = le32_to_cpu(ext->plen);
578 map->m_pa = pa;
579 }
580 }
581 last = (l >= vi->z_extents);
582 }
583
584 if (lstart < lend) {
585 map->m_la = lstart;
586 if (last && (vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER)) {
587 map->m_flags = EROFS_MAP_FRAGMENT;
588 vi->z_fragmentoff = map->m_plen;
589 if (recsz > offsetof(struct z_erofs_extent, pstart_lo))
590 vi->z_fragmentoff |= map->m_pa << 32;
591 } else if (map->m_plen & Z_EROFS_EXTENT_PLEN_MASK) {
592 map->m_flags |= EROFS_MAP_MAPPED;
593 fmt = map->m_plen >> Z_EROFS_EXTENT_PLEN_FMT_BIT;
594 if (map->m_plen & Z_EROFS_EXTENT_PLEN_PARTIAL)
595 map->m_flags |= EROFS_MAP_PARTIAL_REF;
596 map->m_plen &= Z_EROFS_EXTENT_PLEN_MASK;
597 if (fmt)
598 map->m_algorithmformat = fmt - 1;
599 else if (interlaced && !((map->m_pa | map->m_plen) & bmask))
600 map->m_algorithmformat =
601 Z_EROFS_COMPRESSION_INTERLACED;
602 else
603 map->m_algorithmformat =
604 Z_EROFS_COMPRESSION_SHIFTED;
605 }
606 }
607 map->m_llen = lend - map->m_la;
608 return 0;
609 }
610
z_erofs_fill_inode(struct inode * inode,struct erofs_map_blocks * map)611 static int z_erofs_fill_inode(struct inode *inode, struct erofs_map_blocks *map)
612 {
613 struct erofs_inode *const vi = EROFS_I(inode);
614 struct super_block *const sb = inode->i_sb;
615 struct z_erofs_map_header *h;
616 erofs_off_t pos;
617 int err = 0;
618
619 if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags)) {
620 /*
621 * paired with smp_mb() at the end of the function to ensure
622 * fields will only be observed after the bit is set.
623 */
624 smp_mb();
625 return 0;
626 }
627
628 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_Z_BIT, TASK_KILLABLE))
629 return -ERESTARTSYS;
630
631 if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags))
632 goto out_unlock;
633
634 pos = ALIGN(erofs_iloc(inode) + vi->inode_isize + vi->xattr_isize, 8);
635 h = erofs_read_metabuf(&map->buf, sb, pos, erofs_inode_in_metabox(inode));
636 if (IS_ERR(h)) {
637 err = PTR_ERR(h);
638 goto out_unlock;
639 }
640
641 /*
642 * if the highest bit of the 8-byte map header is set, the whole file
643 * is stored in the packed inode. The rest bits keeps z_fragmentoff.
644 */
645 if (h->h_clusterbits >> Z_EROFS_FRAGMENT_INODE_BIT) {
646 vi->z_advise = Z_EROFS_ADVISE_FRAGMENT_PCLUSTER;
647 vi->z_fragmentoff = le64_to_cpu(*(__le64 *)h) ^ (1ULL << 63);
648 vi->z_tailextent_headlcn = 0;
649 goto done;
650 }
651 vi->z_advise = le16_to_cpu(h->h_advise);
652 vi->z_lclusterbits = sb->s_blocksize_bits + (h->h_clusterbits & 15);
653 if (vi->datalayout == EROFS_INODE_COMPRESSED_FULL &&
654 (vi->z_advise & Z_EROFS_ADVISE_EXTENTS)) {
655 vi->z_extents = le32_to_cpu(h->h_extents_lo) |
656 ((u64)le16_to_cpu(h->h_extents_hi) << 32);
657 goto done;
658 }
659
660 vi->z_algorithmtype[0] = h->h_algorithmtype & 15;
661 vi->z_algorithmtype[1] = h->h_algorithmtype >> 4;
662 if (vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER)
663 vi->z_fragmentoff = le32_to_cpu(h->h_fragmentoff);
664 else if (vi->z_advise & Z_EROFS_ADVISE_INLINE_PCLUSTER)
665 vi->z_idata_size = le16_to_cpu(h->h_idata_size);
666
667 if (!erofs_sb_has_big_pcluster(EROFS_SB(sb)) &&
668 vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 |
669 Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
670 erofs_err(sb, "per-inode big pcluster without sb feature for nid %llu",
671 vi->nid);
672 err = -EFSCORRUPTED;
673 goto out_unlock;
674 }
675 if (vi->datalayout == EROFS_INODE_COMPRESSED_COMPACT &&
676 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1) ^
677 !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
678 erofs_err(sb, "big pcluster head1/2 of compact indexes should be consistent for nid %llu",
679 vi->nid);
680 err = -EFSCORRUPTED;
681 goto out_unlock;
682 }
683
684 if (vi->z_idata_size ||
685 (vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER)) {
686 struct erofs_map_blocks tm = {
687 .buf = __EROFS_BUF_INITIALIZER
688 };
689
690 err = z_erofs_map_blocks_fo(inode, &tm,
691 EROFS_GET_BLOCKS_FINDTAIL);
692 erofs_put_metabuf(&tm.buf);
693 if (err < 0)
694 goto out_unlock;
695 }
696 done:
697 /* paired with smp_mb() at the beginning of the function */
698 smp_mb();
699 set_bit(EROFS_I_Z_INITED_BIT, &vi->flags);
700 out_unlock:
701 clear_and_wake_up_bit(EROFS_I_BL_Z_BIT, &vi->flags);
702 return err;
703 }
704
z_erofs_map_sanity_check(struct inode * inode,struct erofs_map_blocks * map)705 static int z_erofs_map_sanity_check(struct inode *inode,
706 struct erofs_map_blocks *map)
707 {
708 struct erofs_sb_info *sbi = EROFS_I_SB(inode);
709 u64 pend;
710
711 if (!(map->m_flags & EROFS_MAP_MAPPED))
712 return 0;
713 if (unlikely(map->m_algorithmformat >= Z_EROFS_COMPRESSION_RUNTIME_MAX)) {
714 erofs_err(inode->i_sb, "unknown algorithm %d @ pos %llu for nid %llu, please upgrade kernel",
715 map->m_algorithmformat, map->m_la, EROFS_I(inode)->nid);
716 return -EOPNOTSUPP;
717 }
718
719 if (map->m_algorithmformat < Z_EROFS_COMPRESSION_MAX) {
720 if (sbi->available_compr_algs ^ BIT(map->m_algorithmformat)) {
721 erofs_err(inode->i_sb, "inconsistent algorithmtype %u for nid %llu",
722 map->m_algorithmformat, EROFS_I(inode)->nid);
723 return -EFSCORRUPTED;
724 }
725 if (EROFS_MAP_FULL(map->m_flags) && map->m_llen < map->m_plen) {
726 erofs_err(inode->i_sb, "too much compressed data @ la %llu of nid %llu",
727 map->m_la, EROFS_I(inode)->nid);
728 return -EFSCORRUPTED;
729 }
730 } else if (map->m_llen > map->m_plen) {
731 erofs_err(inode->i_sb, "not enough plain data on disk @ la %llu of nid %llu",
732 map->m_la, EROFS_I(inode)->nid);
733 return -EFSCORRUPTED;
734 }
735 if (unlikely(map->m_plen > Z_EROFS_PCLUSTER_MAX_SIZE ||
736 map->m_llen > Z_EROFS_PCLUSTER_MAX_DSIZE))
737 return -EOPNOTSUPP;
738 /* Filesystems beyond 48-bit physical block addresses are invalid */
739 if (unlikely(check_add_overflow(map->m_pa, map->m_plen, &pend) ||
740 (pend >> sbi->blkszbits) >= BIT_ULL(48)))
741 return -EFSCORRUPTED;
742 return 0;
743 }
744
z_erofs_map_blocks_iter(struct inode * inode,struct erofs_map_blocks * map,int flags)745 int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
746 int flags)
747 {
748 struct erofs_inode *const vi = EROFS_I(inode);
749 int err = 0;
750
751 trace_erofs_map_blocks_enter(inode, map, flags);
752 if (map->m_la >= inode->i_size) { /* post-EOF unmapped extent */
753 map->m_llen = map->m_la + 1 - inode->i_size;
754 map->m_la = inode->i_size;
755 map->m_flags = 0;
756 } else {
757 err = z_erofs_fill_inode(inode, map);
758 if (!err) {
759 if (vi->datalayout == EROFS_INODE_COMPRESSED_FULL &&
760 (vi->z_advise & Z_EROFS_ADVISE_EXTENTS))
761 err = z_erofs_map_blocks_ext(inode, map, flags);
762 else
763 err = z_erofs_map_blocks_fo(inode, map, flags);
764 }
765 if (!err)
766 err = z_erofs_map_sanity_check(inode, map);
767 if (err)
768 map->m_llen = 0;
769 }
770 trace_erofs_map_blocks_exit(inode, map, flags, err);
771 return err;
772 }
773
z_erofs_iomap_begin_report(struct inode * inode,loff_t offset,loff_t length,unsigned int flags,struct iomap * iomap,struct iomap * srcmap)774 static int z_erofs_iomap_begin_report(struct inode *inode, loff_t offset,
775 loff_t length, unsigned int flags,
776 struct iomap *iomap, struct iomap *srcmap)
777 {
778 int ret;
779 struct erofs_map_blocks map = { .m_la = offset };
780
781 ret = z_erofs_map_blocks_iter(inode, &map, EROFS_GET_BLOCKS_FIEMAP);
782 erofs_put_metabuf(&map.buf);
783 if (ret < 0)
784 return ret;
785
786 iomap->bdev = inode->i_sb->s_bdev;
787 iomap->offset = map.m_la;
788 iomap->length = map.m_llen;
789 if (map.m_flags & EROFS_MAP_FRAGMENT) {
790 iomap->type = IOMAP_MAPPED;
791 iomap->addr = IOMAP_NULL_ADDR;
792 } else if (map.m_flags & EROFS_MAP_MAPPED) {
793 iomap->type = IOMAP_MAPPED;
794 iomap->addr = map.m_pa;
795 } else {
796 iomap->type = IOMAP_HOLE;
797 iomap->addr = IOMAP_NULL_ADDR;
798 /*
799 * No strict rule on how to describe extents for post EOF, yet
800 * we need to do like below. Otherwise, iomap itself will get
801 * into an endless loop on post EOF.
802 *
803 * Calculate the effective offset by subtracting extent start
804 * (map.m_la) from the requested offset, and add it to length.
805 * (NB: offset >= map.m_la always)
806 */
807 if (iomap->offset >= inode->i_size)
808 iomap->length = length + offset - map.m_la;
809 }
810 iomap->flags = 0;
811 return 0;
812 }
813
814 const struct iomap_ops z_erofs_iomap_report_ops = {
815 .iomap_begin = z_erofs_iomap_begin_report,
816 };
817