1 // SPDX-License-Identifier: GPL-2.0
3 * linux/drivers/staging/erofs/zmap.c
5 * Copyright (C) 2018-2019 HUAWEI, Inc.
6 * http://www.huawei.com/
7 * Created by Gao Xiang <gaoxiang25@huawei.com>
10 #include <asm/unaligned.h>
11 #include <trace/events/erofs.h>
13 int z_erofs_fill_inode(struct inode *inode)
15 struct erofs_vnode *const vi = EROFS_V(inode);
16 struct super_block *const sb = inode->i_sb;
18 if (vi->datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY) {
20 vi->z_algorithmtype[0] = 0;
21 vi->z_algorithmtype[1] = 0;
22 vi->z_logical_clusterbits = EROFS_SB(sb)->clusterbits;
23 vi->z_physical_clusterbits[0] = vi->z_logical_clusterbits;
24 vi->z_physical_clusterbits[1] = vi->z_logical_clusterbits;
25 set_bit(EROFS_V_Z_INITED_BIT, &vi->flags);
28 inode->i_mapping->a_ops = &z_erofs_vle_normalaccess_aops;
32 static int fill_inode_lazy(struct inode *inode)
34 struct erofs_vnode *const vi = EROFS_V(inode);
35 struct super_block *const sb = inode->i_sb;
40 struct z_erofs_map_header *h;
42 if (test_bit(EROFS_V_Z_INITED_BIT, &vi->flags))
45 if (wait_on_bit_lock(&vi->flags, EROFS_V_BL_Z_BIT, TASK_KILLABLE))
49 if (test_bit(EROFS_V_Z_INITED_BIT, &vi->flags))
52 DBG_BUGON(vi->datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY);
54 pos = ALIGN(iloc(EROFS_SB(sb), vi->nid) + vi->inode_isize +
56 page = erofs_get_meta_page(sb, erofs_blknr(pos), false);
62 kaddr = kmap_atomic(page);
64 h = kaddr + erofs_blkoff(pos);
65 vi->z_advise = le16_to_cpu(h->h_advise);
66 vi->z_algorithmtype[0] = h->h_algorithmtype & 15;
67 vi->z_algorithmtype[1] = h->h_algorithmtype >> 4;
69 if (vi->z_algorithmtype[0] >= Z_EROFS_COMPRESSION_MAX) {
70 errln("unknown compression format %u for nid %llu, please upgrade kernel",
71 vi->z_algorithmtype[0], vi->nid);
76 vi->z_logical_clusterbits = LOG_BLOCK_SIZE + (h->h_clusterbits & 7);
77 vi->z_physical_clusterbits[0] = vi->z_logical_clusterbits +
78 ((h->h_clusterbits >> 3) & 3);
80 if (vi->z_physical_clusterbits[0] != LOG_BLOCK_SIZE) {
81 errln("unsupported physical clusterbits %u for nid %llu, please upgrade kernel",
82 vi->z_physical_clusterbits[0], vi->nid);
87 vi->z_physical_clusterbits[1] = vi->z_logical_clusterbits +
88 ((h->h_clusterbits >> 5) & 7);
89 set_bit(EROFS_V_Z_INITED_BIT, &vi->flags);
95 clear_and_wake_up_bit(EROFS_V_BL_Z_BIT, &vi->flags);
99 struct z_erofs_maprecorder {
101 struct erofs_map_blocks *map;
105 /* compression extent information gathered */
112 static int z_erofs_reload_indexes(struct z_erofs_maprecorder *m,
115 struct super_block *const sb = m->inode->i_sb;
116 struct erofs_map_blocks *const map = m->map;
117 struct page *mpage = map->mpage;
120 if (mpage->index == eblk) {
122 m->kaddr = kmap_atomic(mpage);
127 kunmap_atomic(m->kaddr);
133 mpage = erofs_get_meta_page(sb, eblk, false);
136 return PTR_ERR(mpage);
138 m->kaddr = kmap_atomic(mpage);
144 static int vle_legacy_load_cluster_from_disk(struct z_erofs_maprecorder *m,
147 struct inode *const inode = m->inode;
148 struct erofs_vnode *const vi = EROFS_V(inode);
149 const erofs_off_t ibase = iloc(EROFS_I_SB(inode), vi->nid);
150 const erofs_off_t pos =
151 Z_EROFS_VLE_LEGACY_INDEX_ALIGN(ibase + vi->inode_isize +
153 lcn * sizeof(struct z_erofs_vle_decompressed_index);
154 struct z_erofs_vle_decompressed_index *di;
155 unsigned int advise, type;
158 err = z_erofs_reload_indexes(m, erofs_blknr(pos));
163 di = m->kaddr + erofs_blkoff(pos);
165 advise = le16_to_cpu(di->di_advise);
166 type = (advise >> Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT) &
167 ((1 << Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) - 1);
169 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
170 m->clusterofs = 1 << vi->z_logical_clusterbits;
171 m->delta[0] = le16_to_cpu(di->di_u.delta[0]);
172 m->delta[1] = le16_to_cpu(di->di_u.delta[1]);
174 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
175 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
176 m->clusterofs = le16_to_cpu(di->di_clusterofs);
177 m->pblk = le32_to_cpu(di->di_u.blkaddr);
187 static unsigned int decode_compactedbits(unsigned int lobits,
189 u8 *in, unsigned int pos, u8 *type)
191 const unsigned int v = get_unaligned_le32(in + pos / 8) >> (pos & 7);
192 const unsigned int lo = v & lomask;
194 *type = (v >> lobits) & 3;
198 static int unpack_compacted_index(struct z_erofs_maprecorder *m,
199 unsigned int amortizedshift,
202 struct erofs_vnode *const vi = EROFS_V(m->inode);
203 const unsigned int lclusterbits = vi->z_logical_clusterbits;
204 const unsigned int lomask = (1 << lclusterbits) - 1;
205 unsigned int vcnt, base, lo, encodebits, nblk;
209 if (1 << amortizedshift == 4)
211 else if (1 << amortizedshift == 2 && lclusterbits == 12)
216 encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt;
217 base = round_down(eofs, vcnt << amortizedshift);
218 in = m->kaddr + base;
220 i = (eofs - base) >> amortizedshift;
222 lo = decode_compactedbits(lclusterbits, lomask,
223 in, encodebits * i, &type);
225 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) {
226 m->clusterofs = 1 << lclusterbits;
232 * since the last lcluster in the pack is special,
233 * of which lo saves delta[1] rather than delta[0].
234 * Hence, get delta[0] by the previous lcluster indirectly.
236 lo = decode_compactedbits(lclusterbits, lomask,
237 in, encodebits * (i - 1), &type);
238 if (type != Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
240 m->delta[0] = lo + 1;
245 /* figout out blkaddr (pblk) for HEAD lclusters */
249 lo = decode_compactedbits(lclusterbits, lomask,
250 in, encodebits * i, &type);
251 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
257 in += (vcnt << amortizedshift) - sizeof(__le32);
258 m->pblk = le32_to_cpu(*(__le32 *)in) + nblk;
262 static int compacted_load_cluster_from_disk(struct z_erofs_maprecorder *m,
265 struct inode *const inode = m->inode;
266 struct erofs_vnode *const vi = EROFS_V(inode);
267 const unsigned int lclusterbits = vi->z_logical_clusterbits;
268 const erofs_off_t ebase = ALIGN(iloc(EROFS_I_SB(inode), vi->nid) +
269 vi->inode_isize + vi->xattr_isize, 8) +
270 sizeof(struct z_erofs_map_header);
271 const unsigned int totalidx = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
272 unsigned int compacted_4b_initial, compacted_2b;
273 unsigned int amortizedshift;
277 if (lclusterbits != 12)
284 /* used to align to 32-byte (compacted_2b) alignment */
285 compacted_4b_initial = (32 - ebase % 32) / 4;
286 if (compacted_4b_initial == 32 / 4)
287 compacted_4b_initial = 0;
289 if (vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B)
290 compacted_2b = rounddown(totalidx - compacted_4b_initial, 16);
295 if (lcn < compacted_4b_initial) {
299 pos += compacted_4b_initial * 4;
300 lcn -= compacted_4b_initial;
302 if (lcn < compacted_2b) {
306 pos += compacted_2b * 2;
310 pos += lcn * (1 << amortizedshift);
311 err = z_erofs_reload_indexes(m, erofs_blknr(pos));
314 return unpack_compacted_index(m, amortizedshift, erofs_blkoff(pos));
317 static int vle_load_cluster_from_disk(struct z_erofs_maprecorder *m,
320 const unsigned int datamode = EROFS_V(m->inode)->datamode;
322 if (datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY)
323 return vle_legacy_load_cluster_from_disk(m, lcn);
325 if (datamode == EROFS_INODE_FLAT_COMPRESSION)
326 return compacted_load_cluster_from_disk(m, lcn);
331 static int vle_extent_lookback(struct z_erofs_maprecorder *m,
332 unsigned int lookback_distance)
334 struct erofs_vnode *const vi = EROFS_V(m->inode);
335 struct erofs_map_blocks *const map = m->map;
336 const unsigned int lclusterbits = vi->z_logical_clusterbits;
337 unsigned long lcn = m->lcn;
340 if (lcn < lookback_distance) {
345 /* load extent head logical cluster if needed */
346 lcn -= lookback_distance;
347 err = vle_load_cluster_from_disk(m, lcn);
352 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
354 errln("invalid lookback distance 0 at nid %llu",
359 return vle_extent_lookback(m, m->delta[0]);
360 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
361 map->m_flags &= ~EROFS_MAP_ZIPPED;
363 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
364 map->m_la = (lcn << lclusterbits) | m->clusterofs;
367 errln("unknown type %u at lcn %lu of nid %llu",
368 m->type, lcn, vi->nid);
375 int z_erofs_map_blocks_iter(struct inode *inode,
376 struct erofs_map_blocks *map,
379 struct erofs_vnode *const vi = EROFS_V(inode);
380 struct z_erofs_maprecorder m = {
385 unsigned int lclusterbits, endoff;
386 unsigned long long ofs, end;
388 trace_z_erofs_map_blocks_iter_enter(inode, map, flags);
390 /* when trying to read beyond EOF, leave it unmapped */
391 if (unlikely(map->m_la >= inode->i_size)) {
392 map->m_llen = map->m_la + 1 - inode->i_size;
393 map->m_la = inode->i_size;
398 err = fill_inode_lazy(inode);
402 lclusterbits = vi->z_logical_clusterbits;
404 m.lcn = ofs >> lclusterbits;
405 endoff = ofs & ((1 << lclusterbits) - 1);
407 err = vle_load_cluster_from_disk(&m, m.lcn);
411 map->m_flags = EROFS_MAP_ZIPPED; /* by default, compressed */
412 end = (m.lcn + 1ULL) << lclusterbits;
415 case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
416 if (endoff >= m.clusterofs)
417 map->m_flags &= ~EROFS_MAP_ZIPPED;
419 case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
420 if (endoff >= m.clusterofs) {
421 map->m_la = (m.lcn << lclusterbits) | m.clusterofs;
424 /* m.lcn should be >= 1 if endoff < m.clusterofs */
425 if (unlikely(!m.lcn)) {
426 errln("invalid logical cluster 0 at nid %llu",
431 end = (m.lcn << lclusterbits) | m.clusterofs;
432 map->m_flags |= EROFS_MAP_FULL_MAPPED;
435 case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
436 /* get the correspoinding first chunk */
437 err = vle_extent_lookback(&m, m.delta[0]);
442 errln("unknown type %u at offset %llu of nid %llu",
443 m.type, ofs, vi->nid);
448 map->m_llen = end - map->m_la;
449 map->m_plen = 1 << lclusterbits;
450 map->m_pa = blknr_to_addr(m.pblk);
451 map->m_flags |= EROFS_MAP_MAPPED;
455 kunmap_atomic(m.kaddr);
458 debugln("%s, m_la %llu m_pa %llu m_llen %llu m_plen %llu m_flags 0%o",
459 __func__, map->m_la, map->m_pa,
460 map->m_llen, map->m_plen, map->m_flags);
462 trace_z_erofs_map_blocks_iter_exit(inode, map, flags, err);
464 /* aggressively BUG_ON iff CONFIG_EROFS_FS_DEBUG is on */
465 DBG_BUGON(err < 0 && err != -ENOMEM);