Merge branch 'work.misc' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-2.6-block.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 unmap_done:
90         kunmap_atomic(kaddr);
91         unlock_page(page);
92         put_page(page);
93
94         set_bit(EROFS_V_Z_INITED_BIT, &vi->flags);
95 out_unlock:
96         clear_and_wake_up_bit(EROFS_V_BL_Z_BIT, &vi->flags);
97         return err;
98 }
99
100 struct z_erofs_maprecorder {
101         struct inode *inode;
102         struct erofs_map_blocks *map;
103         void *kaddr;
104
105         unsigned long lcn;
106         /* compression extent information gathered */
107         u8  type;
108         u16 clusterofs;
109         u16 delta[2];
110         erofs_blk_t pblk;
111 };
112
113 static int z_erofs_reload_indexes(struct z_erofs_maprecorder *m,
114                                   erofs_blk_t eblk)
115 {
116         struct super_block *const sb = m->inode->i_sb;
117         struct erofs_map_blocks *const map = m->map;
118         struct page *mpage = map->mpage;
119
120         if (mpage) {
121                 if (mpage->index == eblk) {
122                         if (!m->kaddr)
123                                 m->kaddr = kmap_atomic(mpage);
124                         return 0;
125                 }
126
127                 if (m->kaddr) {
128                         kunmap_atomic(m->kaddr);
129                         m->kaddr = NULL;
130                 }
131                 put_page(mpage);
132         }
133
134         mpage = erofs_get_meta_page(sb, eblk, false);
135         if (IS_ERR(mpage)) {
136                 map->mpage = NULL;
137                 return PTR_ERR(mpage);
138         }
139         m->kaddr = kmap_atomic(mpage);
140         unlock_page(mpage);
141         map->mpage = mpage;
142         return 0;
143 }
144
145 static int vle_legacy_load_cluster_from_disk(struct z_erofs_maprecorder *m,
146                                              unsigned long lcn)
147 {
148         struct inode *const inode = m->inode;
149         struct erofs_vnode *const vi = EROFS_V(inode);
150         const erofs_off_t ibase = iloc(EROFS_I_SB(inode), vi->nid);
151         const erofs_off_t pos =
152                 Z_EROFS_VLE_LEGACY_INDEX_ALIGN(ibase + vi->inode_isize +
153                                                vi->xattr_isize) +
154                 lcn * sizeof(struct z_erofs_vle_decompressed_index);
155         struct z_erofs_vle_decompressed_index *di;
156         unsigned int advise, type;
157         int err;
158
159         err = z_erofs_reload_indexes(m, erofs_blknr(pos));
160         if (err)
161                 return err;
162
163         m->lcn = lcn;
164         di = m->kaddr + erofs_blkoff(pos);
165
166         advise = le16_to_cpu(di->di_advise);
167         type = (advise >> Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT) &
168                 ((1 << Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) - 1);
169         switch (type) {
170         case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
171                 m->clusterofs = 1 << vi->z_logical_clusterbits;
172                 m->delta[0] = le16_to_cpu(di->di_u.delta[0]);
173                 m->delta[1] = le16_to_cpu(di->di_u.delta[1]);
174                 break;
175         case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
176         case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
177                 m->clusterofs = le16_to_cpu(di->di_clusterofs);
178                 m->pblk = le32_to_cpu(di->di_u.blkaddr);
179                 break;
180         default:
181                 DBG_BUGON(1);
182                 return -EIO;
183         }
184         m->type = type;
185         return 0;
186 }
187
188 static unsigned int decode_compactedbits(unsigned int lobits,
189                                          unsigned int lomask,
190                                          u8 *in, unsigned int pos, u8 *type)
191 {
192         const unsigned int v = get_unaligned_le32(in + pos / 8) >> (pos & 7);
193         const unsigned int lo = v & lomask;
194
195         *type = (v >> lobits) & 3;
196         return lo;
197 }
198
199 static int unpack_compacted_index(struct z_erofs_maprecorder *m,
200                                   unsigned int amortizedshift,
201                                   unsigned int eofs)
202 {
203         struct erofs_vnode *const vi = EROFS_V(m->inode);
204         const unsigned int lclusterbits = vi->z_logical_clusterbits;
205         const unsigned int lomask = (1 << lclusterbits) - 1;
206         unsigned int vcnt, base, lo, encodebits, nblk;
207         int i;
208         u8 *in, type;
209
210         if (1 << amortizedshift == 4)
211                 vcnt = 2;
212         else if (1 << amortizedshift == 2 && lclusterbits == 12)
213                 vcnt = 16;
214         else
215                 return -ENOTSUPP;
216
217         encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt;
218         base = round_down(eofs, vcnt << amortizedshift);
219         in = m->kaddr + base;
220
221         i = (eofs - base) >> amortizedshift;
222
223         lo = decode_compactedbits(lclusterbits, lomask,
224                                   in, encodebits * i, &type);
225         m->type = type;
226         if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD) {
227                 m->clusterofs = 1 << lclusterbits;
228                 if (i + 1 != vcnt) {
229                         m->delta[0] = lo;
230                         return 0;
231                 }
232                 /*
233                  * since the last lcluster in the pack is special,
234                  * of which lo saves delta[1] rather than delta[0].
235                  * Hence, get delta[0] by the previous lcluster indirectly.
236                  */
237                 lo = decode_compactedbits(lclusterbits, lomask,
238                                           in, encodebits * (i - 1), &type);
239                 if (type != Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
240                         lo = 0;
241                 m->delta[0] = lo + 1;
242                 return 0;
243         }
244         m->clusterofs = lo;
245         m->delta[0] = 0;
246         /* figout out blkaddr (pblk) for HEAD lclusters */
247         nblk = 1;
248         while (i > 0) {
249                 --i;
250                 lo = decode_compactedbits(lclusterbits, lomask,
251                                           in, encodebits * i, &type);
252                 if (type == Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD)
253                         i -= lo;
254
255                 if (i >= 0)
256                         ++nblk;
257         }
258         in += (vcnt << amortizedshift) - sizeof(__le32);
259         m->pblk = le32_to_cpu(*(__le32 *)in) + nblk;
260         return 0;
261 }
262
263 static int compacted_load_cluster_from_disk(struct z_erofs_maprecorder *m,
264                                             unsigned long lcn)
265 {
266         struct inode *const inode = m->inode;
267         struct erofs_vnode *const vi = EROFS_V(inode);
268         const unsigned int lclusterbits = vi->z_logical_clusterbits;
269         const erofs_off_t ebase = ALIGN(iloc(EROFS_I_SB(inode), vi->nid) +
270                                         vi->inode_isize + vi->xattr_isize, 8) +
271                 sizeof(struct z_erofs_map_header);
272         const unsigned int totalidx = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
273         unsigned int compacted_4b_initial, compacted_2b;
274         unsigned int amortizedshift;
275         erofs_off_t pos;
276         int err;
277
278         if (lclusterbits != 12)
279                 return -ENOTSUPP;
280
281         if (lcn >= totalidx)
282                 return -EINVAL;
283
284         m->lcn = lcn;
285         /* used to align to 32-byte (compacted_2b) alignment */
286         compacted_4b_initial = (32 - ebase % 32) / 4;
287         if (compacted_4b_initial == 32 / 4)
288                 compacted_4b_initial = 0;
289
290         if (vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B)
291                 compacted_2b = rounddown(totalidx - compacted_4b_initial, 16);
292         else
293                 compacted_2b = 0;
294
295         pos = ebase;
296         if (lcn < compacted_4b_initial) {
297                 amortizedshift = 2;
298                 goto out;
299         }
300         pos += compacted_4b_initial * 4;
301         lcn -= compacted_4b_initial;
302
303         if (lcn < compacted_2b) {
304                 amortizedshift = 1;
305                 goto out;
306         }
307         pos += compacted_2b * 2;
308         lcn -= compacted_2b;
309         amortizedshift = 2;
310 out:
311         pos += lcn * (1 << amortizedshift);
312         err = z_erofs_reload_indexes(m, erofs_blknr(pos));
313         if (err)
314                 return err;
315         return unpack_compacted_index(m, amortizedshift, erofs_blkoff(pos));
316 }
317
318 static int vle_load_cluster_from_disk(struct z_erofs_maprecorder *m,
319                                       unsigned int lcn)
320 {
321         const unsigned int datamode = EROFS_V(m->inode)->datamode;
322
323         if (datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY)
324                 return vle_legacy_load_cluster_from_disk(m, lcn);
325
326         if (datamode == EROFS_INODE_FLAT_COMPRESSION)
327                 return compacted_load_cluster_from_disk(m, lcn);
328
329         return -EINVAL;
330 }
331
332 static int vle_extent_lookback(struct z_erofs_maprecorder *m,
333                                unsigned int lookback_distance)
334 {
335         struct erofs_vnode *const vi = EROFS_V(m->inode);
336         struct erofs_map_blocks *const map = m->map;
337         const unsigned int lclusterbits = vi->z_logical_clusterbits;
338         unsigned long lcn = m->lcn;
339         int err;
340
341         if (lcn < lookback_distance) {
342                 DBG_BUGON(1);
343                 return -EIO;
344         }
345
346         /* load extent head logical cluster if needed */
347         lcn -= lookback_distance;
348         err = vle_load_cluster_from_disk(m, lcn);
349         if (err)
350                 return err;
351
352         switch (m->type) {
353         case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
354                 return vle_extent_lookback(m, m->delta[0]);
355         case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
356                 map->m_flags &= ~EROFS_MAP_ZIPPED;
357                 /* fallthrough */
358         case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
359                 map->m_la = (lcn << lclusterbits) | m->clusterofs;
360                 break;
361         default:
362                 errln("unknown type %u at lcn %lu of nid %llu",
363                       m->type, lcn, vi->nid);
364                 DBG_BUGON(1);
365                 return -EIO;
366         }
367         return 0;
368 }
369
370 int z_erofs_map_blocks_iter(struct inode *inode,
371                             struct erofs_map_blocks *map,
372                             int flags)
373 {
374         struct erofs_vnode *const vi = EROFS_V(inode);
375         struct z_erofs_maprecorder m = {
376                 .inode = inode,
377                 .map = map,
378         };
379         int err = 0;
380         unsigned int lclusterbits, endoff;
381         unsigned long long ofs, end;
382
383         trace_z_erofs_map_blocks_iter_enter(inode, map, flags);
384
385         /* when trying to read beyond EOF, leave it unmapped */
386         if (unlikely(map->m_la >= inode->i_size)) {
387                 map->m_llen = map->m_la + 1 - inode->i_size;
388                 map->m_la = inode->i_size;
389                 map->m_flags = 0;
390                 goto out;
391         }
392
393         err = fill_inode_lazy(inode);
394         if (err)
395                 goto out;
396
397         lclusterbits = vi->z_logical_clusterbits;
398         ofs = map->m_la;
399         m.lcn = ofs >> lclusterbits;
400         endoff = ofs & ((1 << lclusterbits) - 1);
401
402         err = vle_load_cluster_from_disk(&m, m.lcn);
403         if (err)
404                 goto unmap_out;
405
406         map->m_flags = EROFS_MAP_ZIPPED;        /* by default, compressed */
407         end = (m.lcn + 1ULL) << lclusterbits;
408
409         switch (m.type) {
410         case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN:
411                 if (endoff >= m.clusterofs)
412                         map->m_flags &= ~EROFS_MAP_ZIPPED;
413                 /* fallthrough */
414         case Z_EROFS_VLE_CLUSTER_TYPE_HEAD:
415                 if (endoff >= m.clusterofs) {
416                         map->m_la = (m.lcn << lclusterbits) | m.clusterofs;
417                         break;
418                 }
419                 /* m.lcn should be >= 1 if endoff < m.clusterofs */
420                 if (unlikely(!m.lcn)) {
421                         errln("invalid logical cluster 0 at nid %llu",
422                               vi->nid);
423                         err = -EIO;
424                         goto unmap_out;
425                 }
426                 end = (m.lcn << lclusterbits) | m.clusterofs;
427                 map->m_flags |= EROFS_MAP_FULL_MAPPED;
428                 m.delta[0] = 1;
429                 /* fallthrough */
430         case Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD:
431                 /* get the correspoinding first chunk */
432                 err = vle_extent_lookback(&m, m.delta[0]);
433                 if (unlikely(err))
434                         goto unmap_out;
435                 break;
436         default:
437                 errln("unknown type %u at offset %llu of nid %llu",
438                       m.type, ofs, vi->nid);
439                 err = -EIO;
440                 goto unmap_out;
441         }
442
443         map->m_llen = end - map->m_la;
444         map->m_plen = 1 << lclusterbits;
445         map->m_pa = blknr_to_addr(m.pblk);
446         map->m_flags |= EROFS_MAP_MAPPED;
447
448 unmap_out:
449         if (m.kaddr)
450                 kunmap_atomic(m.kaddr);
451
452 out:
453         debugln("%s, m_la %llu m_pa %llu m_llen %llu m_plen %llu m_flags 0%o",
454                 __func__, map->m_la, map->m_pa,
455                 map->m_llen, map->m_plen, map->m_flags);
456
457         trace_z_erofs_map_blocks_iter_exit(inode, map, flags, err);
458
459         /* aggressively BUG_ON iff CONFIG_EROFS_FS_DEBUG is on */
460         DBG_BUGON(err < 0 && err != -ENOMEM);
461         return err;
462 }
463