1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2018-2019 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Created by Gao Xiang <gaoxiang25@huawei.com>
6 */
7 #include "internal.h"
8 #include <asm/unaligned.h>
9 #include <trace/events/erofs.h>
10
z_erofs_fill_inode(struct inode * inode)11 int z_erofs_fill_inode(struct inode *inode)
12 {
13 struct erofs_inode *const vi = EROFS_I(inode);
14
15 if (vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY) {
16 vi->z_advise = 0;
17 vi->z_algorithmtype[0] = 0;
18 vi->z_algorithmtype[1] = 0;
19 vi->z_logical_clusterbits = LOG_BLOCK_SIZE;
20 vi->z_physical_clusterbits[0] = vi->z_logical_clusterbits;
21 vi->z_physical_clusterbits[1] = vi->z_logical_clusterbits;
22 set_bit(EROFS_I_Z_INITED_BIT, &vi->flags);
23 }
24
25 inode->i_mapping->a_ops = &z_erofs_aops;
26 return 0;
27 }
28
z_erofs_fill_inode_lazy(struct inode * inode)29 static int z_erofs_fill_inode_lazy(struct inode *inode)
30 {
31 struct erofs_inode *const vi = EROFS_I(inode);
32 struct super_block *const sb = inode->i_sb;
33 int err;
34 erofs_off_t pos;
35 struct page *page;
36 void *kaddr;
37 struct z_erofs_map_header *h;
38
39 if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags)) {
40 /*
41 * paired with smp_mb() at the end of the function to ensure
42 * fields will only be observed after the bit is set.
43 */
44 smp_mb();
45 return 0;
46 }
47
48 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_Z_BIT, TASK_KILLABLE))
49 return -ERESTARTSYS;
50
51 err = 0;
52 if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags))
53 goto out_unlock;
54
55 DBG_BUGON(vi->datalayout == EROFS_INODE_FLAT_COMPRESSION_LEGACY);
56
57 pos = ALIGN(iloc(EROFS_SB(sb), vi->nid) + vi->inode_isize +
58 vi->xattr_isize, 8);
59 page = erofs_get_meta_page(sb, erofs_blknr(pos));
60 if (IS_ERR(page)) {
61 err = PTR_ERR(page);
62 goto out_unlock;
63 }
64
65 kaddr = kmap_atomic(page);
66
67 h = kaddr + erofs_blkoff(pos);
68 vi->z_advise = le16_to_cpu(h->h_advise);
69 vi->z_algorithmtype[0] = h->h_algorithmtype & 15;
70 vi->z_algorithmtype[1] = h->h_algorithmtype >> 4;
71
72 if (vi->z_algorithmtype[0] >= Z_EROFS_COMPRESSION_MAX) {
73 erofs_err(sb, "unknown compression format %u for nid %llu, please upgrade kernel",
74 vi->z_algorithmtype[0], vi->nid);
75 err = -EOPNOTSUPP;
76 goto unmap_done;
77 }
78
79 vi->z_logical_clusterbits = LOG_BLOCK_SIZE + (h->h_clusterbits & 7);
80 vi->z_physical_clusterbits[0] = vi->z_logical_clusterbits +
81 ((h->h_clusterbits >> 3) & 3);
82
83 if (vi->z_physical_clusterbits[0] != LOG_BLOCK_SIZE) {
84 erofs_err(sb, "unsupported physical clusterbits %u for nid %llu, please upgrade kernel",
85 vi->z_physical_clusterbits[0], vi->nid);
86 err = -EOPNOTSUPP;
87 goto unmap_done;
88 }
89
90 vi->z_physical_clusterbits[1] = vi->z_logical_clusterbits +
91 ((h->h_clusterbits >> 5) & 7);
92 /* paired with smp_mb() at the beginning of the function */
93 smp_mb();
94 set_bit(EROFS_I_Z_INITED_BIT, &vi->flags);
95 unmap_done:
96 kunmap_atomic(kaddr);
97 unlock_page(page);
98 put_page(page);
99 out_unlock:
100 clear_and_wake_up_bit(EROFS_I_BL_Z_BIT, &vi->flags);
101 return err;
102 }
103
104 struct z_erofs_maprecorder {
105 struct inode *inode;
106 struct erofs_map_blocks *map;
107 void *kaddr;
108
109 unsigned long lcn;
110 /* compression extent information gathered */
111 u8 type;
112 u16 clusterofs;
113 u16 delta[2];
114 erofs_blk_t pblk;
115 };
116
z_erofs_reload_indexes(struct z_erofs_maprecorder * m,erofs_blk_t eblk)117 static int z_erofs_reload_indexes(struct z_erofs_maprecorder *m,
118 erofs_blk_t eblk)
119 {
120 struct super_block *const sb = m->inode->i_sb;
121 struct erofs_map_blocks *const map = m->map;
122 struct page *mpage = map->mpage;
123
124 if (mpage) {
125 if (mpage->index == eblk) {
126 if (!m->kaddr)
127 m->kaddr = kmap_atomic(mpage);
128 return 0;
129 }
130
131 if (m->kaddr) {
132 kunmap_atomic(m->kaddr);
133 m->kaddr = NULL;
134 }
135 put_page(mpage);
136 }
137
138 mpage = erofs_get_meta_page(sb, eblk);
139 if (IS_ERR(mpage)) {
140 map->mpage = NULL;
141 return PTR_ERR(mpage);
142 }
143 m->kaddr = kmap_atomic(mpage);
144 unlock_page(mpage);
145 map->mpage = mpage;
146 return 0;
147 }
148
legacy_load_cluster_from_disk(struct z_erofs_maprecorder * m,unsigned long lcn)149 static int legacy_load_cluster_from_disk(struct z_erofs_maprecorder *m,
150 unsigned long lcn)
151 {
152 struct inode *const inode = m->inode;
153 struct erofs_inode *const vi = EROFS_I(inode);
154 const erofs_off_t ibase = iloc(EROFS_I_SB(inode), vi->nid);
155 const erofs_off_t pos =
156 Z_EROFS_VLE_LEGACY_INDEX_ALIGN(ibase + vi->inode_isize +
157 vi->xattr_isize) +
158 lcn * sizeof(struct z_erofs_vle_decompressed_index);
159 struct z_erofs_vle_decompressed_index *di;
160 unsigned int advise, type;
161 int err;
162
163 err = z_erofs_reload_indexes(m, erofs_blknr(pos));
164 if (err)
165 return err;
166
167 m->lcn = lcn;
168 di = m->kaddr + erofs_blkoff(pos);
169
170 advise = le16_to_cpu(di->di_advise);
171 type = (advise >> Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT) &
172 ((1 << Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) - 1);
173 switch (type) {
174 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
175 m->clusterofs = 1 << vi->z_logical_clusterbits;
176 m->delta[0] = le16_to_cpu(di->di_u.delta[0]);
177 m->delta[1] = le16_to_cpu(di->di_u.delta[1]);
178 break;
179 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
180 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
181 m->clusterofs = le16_to_cpu(di->di_clusterofs);
182 if (m->clusterofs >= 1 << vi->z_logical_clusterbits) {
183 DBG_BUGON(1);
184 return -EFSCORRUPTED;
185 }
186 m->pblk = le32_to_cpu(di->di_u.blkaddr);
187 break;
188 default:
189 DBG_BUGON(1);
190 return -EOPNOTSUPP;
191 }
192 m->type = type;
193 return 0;
194 }
195
decode_compactedbits(unsigned int lobits,unsigned int lomask,u8 * in,unsigned int pos,u8 * type)196 static unsigned int decode_compactedbits(unsigned int lobits,
197 unsigned int lomask,
198 u8 *in, unsigned int pos, u8 *type)
199 {
200 const unsigned int v = get_unaligned_le32(in + pos / 8) >> (pos & 7);
201 const unsigned int lo = v & lomask;
202
203 *type = (v >> lobits) & 3;
204 return lo;
205 }
206
unpack_compacted_index(struct z_erofs_maprecorder * m,unsigned int amortizedshift,unsigned int eofs)207 static int unpack_compacted_index(struct z_erofs_maprecorder *m,
208 unsigned int amortizedshift,
209 unsigned int eofs)
210 {
211 struct erofs_inode *const vi = EROFS_I(m->inode);
212 const unsigned int lclusterbits = vi->z_logical_clusterbits;
213 const unsigned int lomask = (1 << lclusterbits) - 1;
214 unsigned int vcnt, base, lo, encodebits, nblk;
215 int i;
216 u8 *in, type;
217
218 if (1 << amortizedshift == 4)
219 vcnt = 2;
220 else if (1 << amortizedshift == 2 && lclusterbits == 12)
221 vcnt = 16;
222 else
223 return -EOPNOTSUPP;
224
225 encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt;
226 base = round_down(eofs, vcnt << amortizedshift);
227 in = m->kaddr + base;
228
229 i = (eofs - base) >> amortizedshift;
230
231 lo = decode_compactedbits(lclusterbits, lomask,
232 in, encodebits * i, &type);
233 m->type = type;
234 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) {
235 m->clusterofs = 1 << lclusterbits;
236 if (i + 1 != vcnt) {
237 m->delta[0] = lo;
238 return 0;
239 }
240 /*
241 * since the last lcluster in the pack is special,
242 * of which lo saves delta[1] rather than delta[0].
243 * Hence, get delta[0] by the previous lcluster indirectly.
244 */
245 lo = decode_compactedbits(lclusterbits, lomask,
246 in, encodebits * (i - 1), &type);
247 if (type != Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
248 lo = 0;
249 m->delta[0] = lo + 1;
250 return 0;
251 }
252 m->clusterofs = lo;
253 m->delta[0] = 0;
254 /* figout out blkaddr (pblk) for HEAD lclusters */
255 nblk = 1;
256 while (i > 0) {
257 --i;
258 lo = decode_compactedbits(lclusterbits, lomask,
259 in, encodebits * i, &type);
260 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
261 i -= lo;
262
263 if (i >= 0)
264 ++nblk;
265 }
266 in += (vcnt << amortizedshift) - sizeof(__le32);
267 m->pblk = le32_to_cpu(*(__le32 *)in) + nblk;
268 return 0;
269 }
270
compacted_load_cluster_from_disk(struct z_erofs_maprecorder * m,unsigned long lcn)271 static int compacted_load_cluster_from_disk(struct z_erofs_maprecorder *m,
272 unsigned long lcn)
273 {
274 struct inode *const inode = m->inode;
275 struct erofs_inode *const vi = EROFS_I(inode);
276 const unsigned int lclusterbits = vi->z_logical_clusterbits;
277 const erofs_off_t ebase = ALIGN(iloc(EROFS_I_SB(inode), vi->nid) +
278 vi->inode_isize + vi->xattr_isize, 8) +
279 sizeof(struct z_erofs_map_header);
280 const unsigned int totalidx = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
281 unsigned int compacted_4b_initial, compacted_2b;
282 unsigned int amortizedshift;
283 erofs_off_t pos;
284 int err;
285
286 if (lclusterbits != 12)
287 return -EOPNOTSUPP;
288
289 if (lcn >= totalidx)
290 return -EINVAL;
291
292 m->lcn = lcn;
293 /* used to align to 32-byte (compacted_2b) alignment */
294 compacted_4b_initial = (32 - ebase % 32) / 4;
295 if (compacted_4b_initial == 32 / 4)
296 compacted_4b_initial = 0;
297
298 if (vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B)
299 compacted_2b = rounddown(totalidx - compacted_4b_initial, 16);
300 else
301 compacted_2b = 0;
302
303 pos = ebase;
304 if (lcn < compacted_4b_initial) {
305 amortizedshift = 2;
306 goto out;
307 }
308 pos += compacted_4b_initial * 4;
309 lcn -= compacted_4b_initial;
310
311 if (lcn < compacted_2b) {
312 amortizedshift = 1;
313 goto out;
314 }
315 pos += compacted_2b * 2;
316 lcn -= compacted_2b;
317 amortizedshift = 2;
318 out:
319 pos += lcn * (1 << amortizedshift);
320 err = z_erofs_reload_indexes(m, erofs_blknr(pos));
321 if (err)
322 return err;
323 return unpack_compacted_index(m, amortizedshift, erofs_blkoff(pos));
324 }
325
z_erofs_load_cluster_from_disk(struct z_erofs_maprecorder * m,unsigned int lcn)326 static int z_erofs_load_cluster_from_disk(struct z_erofs_maprecorder *m,
327 unsigned int lcn)
328 {
329 const unsigned int datamode = EROFS_I(m->inode)->datalayout;
330
331 if (datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY)
332 return legacy_load_cluster_from_disk(m, lcn);
333
334 if (datamode == EROFS_INODE_FLAT_COMPRESSION)
335 return compacted_load_cluster_from_disk(m, lcn);
336
337 return -EINVAL;
338 }
339
z_erofs_extent_lookback(struct z_erofs_maprecorder * m,unsigned int lookback_distance)340 static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m,
341 unsigned int lookback_distance)
342 {
343 struct erofs_inode *const vi = EROFS_I(m->inode);
344 struct erofs_map_blocks *const map = m->map;
345 const unsigned int lclusterbits = vi->z_logical_clusterbits;
346 unsigned long lcn = m->lcn;
347 int err;
348
349 if (lcn < lookback_distance) {
350 erofs_err(m->inode->i_sb,
351 "bogus lookback distance @ nid %llu", vi->nid);
352 DBG_BUGON(1);
353 return -EFSCORRUPTED;
354 }
355
356 /* load extent head logical cluster if needed */
357 lcn -= lookback_distance;
358 err = z_erofs_load_cluster_from_disk(m, lcn);
359 if (err)
360 return err;
361
362 switch (m->type) {
363 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
364 if (!m->delta[0]) {
365 erofs_err(m->inode->i_sb,
366 "invalid lookback distance 0 @ nid %llu",
367 vi->nid);
368 DBG_BUGON(1);
369 return -EFSCORRUPTED;
370 }
371 return z_erofs_extent_lookback(m, m->delta[0]);
372 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
373 map->m_flags &= ~EROFS_MAP_ZIPPED;
374 fallthrough;
375 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
376 map->m_la = (lcn << lclusterbits) | m->clusterofs;
377 break;
378 default:
379 erofs_err(m->inode->i_sb,
380 "unknown type %u @ lcn %lu of nid %llu",
381 m->type, lcn, vi->nid);
382 DBG_BUGON(1);
383 return -EOPNOTSUPP;
384 }
385 return 0;
386 }
387
z_erofs_map_blocks_iter(struct inode * inode,struct erofs_map_blocks * map,int flags)388 int z_erofs_map_blocks_iter(struct inode *inode,
389 struct erofs_map_blocks *map,
390 int flags)
391 {
392 struct erofs_inode *const vi = EROFS_I(inode);
393 struct z_erofs_maprecorder m = {
394 .inode = inode,
395 .map = map,
396 };
397 int err = 0;
398 unsigned int lclusterbits, endoff;
399 unsigned long long ofs, end;
400
401 trace_z_erofs_map_blocks_iter_enter(inode, map, flags);
402
403 /* when trying to read beyond EOF, leave it unmapped */
404 if (map->m_la >= inode->i_size) {
405 map->m_llen = map->m_la + 1 - inode->i_size;
406 map->m_la = inode->i_size;
407 map->m_flags = 0;
408 goto out;
409 }
410
411 err = z_erofs_fill_inode_lazy(inode);
412 if (err)
413 goto out;
414
415 lclusterbits = vi->z_logical_clusterbits;
416 ofs = map->m_la;
417 m.lcn = ofs >> lclusterbits;
418 endoff = ofs & ((1 << lclusterbits) - 1);
419
420 err = z_erofs_load_cluster_from_disk(&m, m.lcn);
421 if (err)
422 goto unmap_out;
423
424 map->m_flags = EROFS_MAP_ZIPPED; /* by default, compressed */
425 end = (m.lcn + 1ULL) << lclusterbits;
426
427 switch (m.type) {
428 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
429 if (endoff >= m.clusterofs)
430 map->m_flags &= ~EROFS_MAP_ZIPPED;
431 fallthrough;
432 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
433 if (endoff >= m.clusterofs) {
434 map->m_la = (m.lcn << lclusterbits) | m.clusterofs;
435 break;
436 }
437 /* m.lcn should be >= 1 if endoff < m.clusterofs */
438 if (!m.lcn) {
439 erofs_err(inode->i_sb,
440 "invalid logical cluster 0 at nid %llu",
441 vi->nid);
442 err = -EFSCORRUPTED;
443 goto unmap_out;
444 }
445 end = (m.lcn << lclusterbits) | m.clusterofs;
446 map->m_flags |= EROFS_MAP_FULL_MAPPED;
447 m.delta[0] = 1;
448 fallthrough;
449 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
450 /* get the correspoinding first chunk */
451 err = z_erofs_extent_lookback(&m, m.delta[0]);
452 if (err)
453 goto unmap_out;
454 break;
455 default:
456 erofs_err(inode->i_sb,
457 "unknown type %u @ offset %llu of nid %llu",
458 m.type, ofs, vi->nid);
459 err = -EOPNOTSUPP;
460 goto unmap_out;
461 }
462
463 map->m_llen = end - map->m_la;
464 map->m_plen = 1 << lclusterbits;
465 map->m_pa = blknr_to_addr(m.pblk);
466 map->m_flags |= EROFS_MAP_MAPPED;
467
468 unmap_out:
469 if (m.kaddr)
470 kunmap_atomic(m.kaddr);
471
472 out:
473 erofs_dbg("%s, m_la %llu m_pa %llu m_llen %llu m_plen %llu m_flags 0%o",
474 __func__, map->m_la, map->m_pa,
475 map->m_llen, map->m_plen, map->m_flags);
476
477 trace_z_erofs_map_blocks_iter_exit(inode, map, flags, err);
478
479 /* aggressively BUG_ON iff CONFIG_EROFS_FS_DEBUG is on */
480 DBG_BUGON(err < 0 && err != -ENOMEM);
481 return err;
482 }
483
484