Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / drivers / staging / erofs / zmap.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/drivers/staging/erofs/zmap.c
4  *
5  * Copyright (C) 2018-2019 HUAWEI, Inc.
6  *             http://www.huawei.com/
7  * Created by Gao Xiang <gaoxiang25@huawei.com>
8  */
9 #include "internal.h"
10 #include <asm/unaligned.h>
11 #include <trace/events/erofs.h>
12
13 int z_erofs_fill_inode(struct inode *inode)
14 {
15         struct erofs_vnode *const vi = EROFS_V(inode);
16         struct super_block *const sb = inode->i_sb;
17
18         if (vi->datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY) {
19                 vi->z_advise = 0;
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);
26         }
27
28         inode->i_mapping->a_ops = &z_erofs_vle_normalaccess_aops;
29         return 0;
30 }
31
32 static int fill_inode_lazy(struct inode *inode)
33 {
34         struct erofs_vnode *const vi = EROFS_V(inode);
35         struct super_block *const sb = inode->i_sb;
36         int err;
37         erofs_off_t pos;
38         struct page *page;
39         void *kaddr;
40         struct z_erofs_map_header *h;
41
42         if (test_bit(EROFS_V_Z_INITED_BIT, &vi->flags))
43                 return 0;
44
45         if (wait_on_bit_lock(&vi->flags, EROFS_V_BL_Z_BIT, TASK_KILLABLE))
46                 return -ERESTARTSYS;
47
48         err = 0;
49         if (test_bit(EROFS_V_Z_INITED_BIT, &vi->flags))
50                 goto out_unlock;
51
52         DBG_BUGON(vi->datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY);
53
54         pos = ALIGN(iloc(EROFS_SB(sb), vi->nid) + vi->inode_isize +
55                     vi->xattr_isize, 8);
56         page = erofs_get_meta_page(sb, erofs_blknr(pos), false);
57         if (IS_ERR(page)) {
58                 err = PTR_ERR(page);
59                 goto out_unlock;
60         }
61
62         kaddr = kmap_atomic(page);
63
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;
68
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);
72                 err = -ENOTSUPP;
73                 goto unmap_done;
74         }
75
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);
79
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);
83                 err = -ENOTSUPP;
84                 goto unmap_done;
85         }
86
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);
90 unmap_done:
91         kunmap_atomic(kaddr);
92         unlock_page(page);
93         put_page(page);
94 out_unlock:
95         clear_and_wake_up_bit(EROFS_V_BL_Z_BIT, &vi->flags);
96         return err;
97 }
98
99 struct z_erofs_maprecorder {
100         struct inode *inode;
101         struct erofs_map_blocks *map;
102         void *kaddr;
103
104         unsigned long lcn;
105         /* compression extent information gathered */
106         u8  type;
107         u16 clusterofs;
108         u16 delta[2];
109         erofs_blk_t pblk;
110 };
111
112 static int z_erofs_reload_indexes(struct z_erofs_maprecorder *m,
113                                   erofs_blk_t eblk)
114 {
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;
118
119         if (mpage) {
120                 if (mpage->index == eblk) {
121                         if (!m->kaddr)
122                                 m->kaddr = kmap_atomic(mpage);
123                         return 0;
124                 }
125
126                 if (m->kaddr) {
127                         kunmap_atomic(m->kaddr);
128                         m->kaddr = NULL;
129                 }
130                 put_page(mpage);
131         }
132
133         mpage = erofs_get_meta_page(sb, eblk, false);
134         if (IS_ERR(mpage)) {
135                 map->mpage = NULL;
136                 return PTR_ERR(mpage);
137         }
138         m->kaddr = kmap_atomic(mpage);
139         unlock_page(mpage);
140         map->mpage = mpage;
141         return 0;
142 }
143
144 static int vle_legacy_load_cluster_from_disk(struct z_erofs_maprecorder *m,
145                                              unsigned long lcn)
146 {
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 +
152                                                vi->xattr_isize) +
153                 lcn * sizeof(struct z_erofs_vle_decompressed_index);
154         struct z_erofs_vle_decompressed_index *di;
155         unsigned int advise, type;
156         int err;
157
158         err = z_erofs_reload_indexes(m, erofs_blknr(pos));
159         if (err)
160                 return err;
161
162         m->lcn = lcn;
163         di = m->kaddr + erofs_blkoff(pos);
164
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);
168         switch (type) {
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]);
173                 break;
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);
178                 break;
179         default:
180                 DBG_BUGON(1);
181                 return -EIO;
182         }
183         m->type = type;
184         return 0;
185 }
186
187 static unsigned int decode_compactedbits(unsigned int lobits,
188                                          unsigned int lomask,
189                                          u8 *in, unsigned int pos, u8 *type)
190 {
191         const unsigned int v = get_unaligned_le32(in + pos / 8) >> (pos & 7);
192         const unsigned int lo = v & lomask;
193
194         *type = (v >> lobits) & 3;
195         return lo;
196 }
197
198 static int unpack_compacted_index(struct z_erofs_maprecorder *m,
199                                   unsigned int amortizedshift,
200                                   unsigned int eofs)
201 {
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;
206         int i;
207         u8 *in, type;
208
209         if (1 << amortizedshift == 4)
210                 vcnt = 2;
211         else if (1 << amortizedshift == 2 && lclusterbits == 12)
212                 vcnt = 16;
213         else
214                 return -ENOTSUPP;
215
216         encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt;
217         base = round_down(eofs, vcnt << amortizedshift);
218         in = m->kaddr + base;
219
220         i = (eofs - base) >> amortizedshift;
221
222         lo = decode_compactedbits(lclusterbits, lomask,
223                                   in, encodebits * i, &type);
224         m->type = type;
225         if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) {
226                 m->clusterofs = 1 << lclusterbits;
227                 if (i + 1 != vcnt) {
228                         m->delta[0] = lo;
229                         return 0;
230                 }
231                 /*
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.
235                  */
236                 lo = decode_compactedbits(lclusterbits, lomask,
237                                           in, encodebits * (i - 1), &type);
238                 if (type != Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
239                         lo = 0;
240                 m->delta[0] = lo + 1;
241                 return 0;
242         }
243         m->clusterofs = lo;
244         m->delta[0] = 0;
245         /* figout out blkaddr (pblk) for HEAD lclusters */
246         nblk = 1;
247         while (i > 0) {
248                 --i;
249                 lo = decode_compactedbits(lclusterbits, lomask,
250                                           in, encodebits * i, &type);
251                 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
252                         i -= lo;
253
254                 if (i >= 0)
255                         ++nblk;
256         }
257         in += (vcnt << amortizedshift) - sizeof(__le32);
258         m->pblk = le32_to_cpu(*(__le32 *)in) + nblk;
259         return 0;
260 }
261
262 static int compacted_load_cluster_from_disk(struct z_erofs_maprecorder *m,
263                                             unsigned long lcn)
264 {
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;
274         erofs_off_t pos;
275         int err;
276
277         if (lclusterbits != 12)
278                 return -ENOTSUPP;
279
280         if (lcn >= totalidx)
281                 return -EINVAL;
282
283         m->lcn = lcn;
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;
288
289         if (vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B)
290                 compacted_2b = rounddown(totalidx - compacted_4b_initial, 16);
291         else
292                 compacted_2b = 0;
293
294         pos = ebase;
295         if (lcn < compacted_4b_initial) {
296                 amortizedshift = 2;
297                 goto out;
298         }
299         pos += compacted_4b_initial * 4;
300         lcn -= compacted_4b_initial;
301
302         if (lcn < compacted_2b) {
303                 amortizedshift = 1;
304                 goto out;
305         }
306         pos += compacted_2b * 2;
307         lcn -= compacted_2b;
308         amortizedshift = 2;
309 out:
310         pos += lcn * (1 << amortizedshift);
311         err = z_erofs_reload_indexes(m, erofs_blknr(pos));
312         if (err)
313                 return err;
314         return unpack_compacted_index(m, amortizedshift, erofs_blkoff(pos));
315 }
316
317 static int vle_load_cluster_from_disk(struct z_erofs_maprecorder *m,
318                                       unsigned int lcn)
319 {
320         const unsigned int datamode = EROFS_V(m->inode)->datamode;
321
322         if (datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY)
323                 return vle_legacy_load_cluster_from_disk(m, lcn);
324
325         if (datamode == EROFS_INODE_FLAT_COMPRESSION)
326                 return compacted_load_cluster_from_disk(m, lcn);
327
328         return -EINVAL;
329 }
330
331 static int vle_extent_lookback(struct z_erofs_maprecorder *m,
332                                unsigned int lookback_distance)
333 {
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;
338         int err;
339
340         if (lcn < lookback_distance) {
341                 DBG_BUGON(1);
342                 return -EIO;
343         }
344
345         /* load extent head logical cluster if needed */
346         lcn -= lookback_distance;
347         err = vle_load_cluster_from_disk(m, lcn);
348         if (err)
349                 return err;
350
351         switch (m->type) {
352         case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
353                 if (!m->delta[0]) {
354                         errln("invalid lookback distance 0 at nid %llu",
355                               vi->nid);
356                         DBG_BUGON(1);
357                         return -EIO;
358                 }
359                 return vle_extent_lookback(m, m->delta[0]);
360         case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
361                 map->m_flags &= ~EROFS_MAP_ZIPPED;
362                 /* fallthrough */
363         case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
364                 map->m_la = (lcn << lclusterbits) | m->clusterofs;
365                 break;
366         default:
367                 errln("unknown type %u at lcn %lu of nid %llu",
368                       m->type, lcn, vi->nid);
369                 DBG_BUGON(1);
370                 return -EIO;
371         }
372         return 0;
373 }
374
375 int z_erofs_map_blocks_iter(struct inode *inode,
376                             struct erofs_map_blocks *map,
377                             int flags)
378 {
379         struct erofs_vnode *const vi = EROFS_V(inode);
380         struct z_erofs_maprecorder m = {
381                 .inode = inode,
382                 .map = map,
383         };
384         int err = 0;
385         unsigned int lclusterbits, endoff;
386         unsigned long long ofs, end;
387
388         trace_z_erofs_map_blocks_iter_enter(inode, map, flags);
389
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;
394                 map->m_flags = 0;
395                 goto out;
396         }
397
398         err = fill_inode_lazy(inode);
399         if (err)
400                 goto out;
401
402         lclusterbits = vi->z_logical_clusterbits;
403         ofs = map->m_la;
404         m.lcn = ofs >> lclusterbits;
405         endoff = ofs & ((1 << lclusterbits) - 1);
406
407         err = vle_load_cluster_from_disk(&m, m.lcn);
408         if (err)
409                 goto unmap_out;
410
411         map->m_flags = EROFS_MAP_ZIPPED;        /* by default, compressed */
412         end = (m.lcn + 1ULL) << lclusterbits;
413
414         switch (m.type) {
415         case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
416                 if (endoff >= m.clusterofs)
417                         map->m_flags &= ~EROFS_MAP_ZIPPED;
418                 /* fallthrough */
419         case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
420                 if (endoff >= m.clusterofs) {
421                         map->m_la = (m.lcn << lclusterbits) | m.clusterofs;
422                         break;
423                 }
424                 /* m.lcn should be >= 1 if endoff < m.clusterofs */
425                 if (unlikely(!m.lcn)) {
426                         errln("invalid logical cluster 0 at nid %llu",
427                               vi->nid);
428                         err = -EIO;
429                         goto unmap_out;
430                 }
431                 end = (m.lcn << lclusterbits) | m.clusterofs;
432                 map->m_flags |= EROFS_MAP_FULL_MAPPED;
433                 m.delta[0] = 1;
434                 /* fallthrough */
435         case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
436                 /* get the correspoinding first chunk */
437                 err = vle_extent_lookback(&m, m.delta[0]);
438                 if (unlikely(err))
439                         goto unmap_out;
440                 break;
441         default:
442                 errln("unknown type %u at offset %llu of nid %llu",
443                       m.type, ofs, vi->nid);
444                 err = -EIO;
445                 goto unmap_out;
446         }
447
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;
452
453 unmap_out:
454         if (m.kaddr)
455                 kunmap_atomic(m.kaddr);
456
457 out:
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);
461
462         trace_z_erofs_map_blocks_iter_exit(inode, map, flags, err);
463
464         /* aggressively BUG_ON iff CONFIG_EROFS_FS_DEBUG is on */
465         DBG_BUGON(err < 0 && err != -ENOMEM);
466         return err;
467 }
468