GNU Linux-libre 5.10.215-gnu1
[releases.git] / fs / erofs / decompressor.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2019 HUAWEI, Inc.
4  *             https://www.huawei.com/
5  * Created by Gao Xiang <gaoxiang25@huawei.com>
6  */
7 #include "compress.h"
8 #include <linux/module.h>
9 #include <linux/lz4.h>
10
11 #ifndef LZ4_DISTANCE_MAX        /* history window size */
12 #define LZ4_DISTANCE_MAX 65535  /* set to maximum value by default */
13 #endif
14
15 #define LZ4_MAX_DISTANCE_PAGES  (DIV_ROUND_UP(LZ4_DISTANCE_MAX, PAGE_SIZE) + 1)
16 #ifndef LZ4_DECOMPRESS_INPLACE_MARGIN
17 #define LZ4_DECOMPRESS_INPLACE_MARGIN(srcsize)  (((srcsize) >> 8) + 32)
18 #endif
19
20 struct z_erofs_decompressor {
21         /*
22          * if destpages have sparsed pages, fill them with bounce pages.
23          * it also check whether destpages indicate continuous physical memory.
24          */
25         int (*prepare_destpages)(struct z_erofs_decompress_req *rq,
26                                  struct list_head *pagepool);
27         int (*decompress)(struct z_erofs_decompress_req *rq, u8 *out,
28                           u8 *obase);
29         char *name;
30 };
31
32 static int z_erofs_lz4_prepare_destpages(struct z_erofs_decompress_req *rq,
33                                          struct list_head *pagepool)
34 {
35         const unsigned int nr =
36                 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
37         struct page *availables[LZ4_MAX_DISTANCE_PAGES] = { NULL };
38         unsigned long bounced[DIV_ROUND_UP(LZ4_MAX_DISTANCE_PAGES,
39                                            BITS_PER_LONG)] = { 0 };
40         void *kaddr = NULL;
41         unsigned int i, j, top;
42
43         top = 0;
44         for (i = j = 0; i < nr; ++i, ++j) {
45                 struct page *const page = rq->out[i];
46                 struct page *victim;
47
48                 if (j >= LZ4_MAX_DISTANCE_PAGES)
49                         j = 0;
50
51                 /* 'valid' bounced can only be tested after a complete round */
52                 if (test_bit(j, bounced)) {
53                         DBG_BUGON(i < LZ4_MAX_DISTANCE_PAGES);
54                         DBG_BUGON(top >= LZ4_MAX_DISTANCE_PAGES);
55                         availables[top++] = rq->out[i - LZ4_MAX_DISTANCE_PAGES];
56                 }
57
58                 if (page) {
59                         __clear_bit(j, bounced);
60                         if (!PageHighMem(page)) {
61                                 if (!i) {
62                                         kaddr = page_address(page);
63                                         continue;
64                                 }
65                                 if (kaddr &&
66                                     kaddr + PAGE_SIZE == page_address(page)) {
67                                         kaddr += PAGE_SIZE;
68                                         continue;
69                                 }
70                         }
71                         kaddr = NULL;
72                         continue;
73                 }
74                 kaddr = NULL;
75                 __set_bit(j, bounced);
76
77                 if (top) {
78                         victim = availables[--top];
79                         get_page(victim);
80                 } else {
81                         victim = erofs_allocpage(pagepool, GFP_KERNEL);
82                         if (!victim)
83                                 return -ENOMEM;
84                         victim->mapping = Z_EROFS_MAPPING_STAGING;
85                 }
86                 rq->out[i] = victim;
87         }
88         return kaddr ? 1 : 0;
89 }
90
91 static void *generic_copy_inplace_data(struct z_erofs_decompress_req *rq,
92                                        u8 *src, unsigned int pageofs_in)
93 {
94         /*
95          * if in-place decompression is ongoing, those decompressed
96          * pages should be copied in order to avoid being overlapped.
97          */
98         struct page **in = rq->in;
99         u8 *const tmp = erofs_get_pcpubuf(0);
100         u8 *tmpp = tmp;
101         unsigned int inlen = rq->inputsize - pageofs_in;
102         unsigned int count = min_t(uint, inlen, PAGE_SIZE - pageofs_in);
103
104         while (tmpp < tmp + inlen) {
105                 if (!src)
106                         src = kmap_atomic(*in);
107                 memcpy(tmpp, src + pageofs_in, count);
108                 kunmap_atomic(src);
109                 src = NULL;
110                 tmpp += count;
111                 pageofs_in = 0;
112                 count = PAGE_SIZE;
113                 ++in;
114         }
115         return tmp;
116 }
117
118 static int z_erofs_lz4_decompress(struct z_erofs_decompress_req *rq, u8 *out,
119                                   u8 *obase)
120 {
121         const uint nrpages_out = PAGE_ALIGN(rq->pageofs_out +
122                                             rq->outputsize) >> PAGE_SHIFT;
123         unsigned int inputmargin, inlen;
124         u8 *src, *src2;
125         bool copied, support_0padding;
126         int ret;
127
128         if (rq->inputsize > PAGE_SIZE)
129                 return -EOPNOTSUPP;
130
131         src = kmap_atomic(*rq->in);
132         src2 = src;
133         inputmargin = 0;
134         support_0padding = false;
135
136         /* decompression inplace is only safe when 0padding is enabled */
137         if (EROFS_SB(rq->sb)->feature_incompat &
138             EROFS_FEATURE_INCOMPAT_LZ4_0PADDING) {
139                 support_0padding = true;
140
141                 while (!src[inputmargin & ~PAGE_MASK])
142                         if (!(++inputmargin & ~PAGE_MASK))
143                                 break;
144
145                 if (inputmargin >= rq->inputsize) {
146                         kunmap_atomic(src);
147                         return -EIO;
148                 }
149         }
150
151         copied = false;
152         inlen = rq->inputsize - inputmargin;
153         if (rq->inplace_io) {
154                 const uint oend = (rq->pageofs_out +
155                                    rq->outputsize) & ~PAGE_MASK;
156                 if (rq->partial_decoding || !support_0padding ||
157                     rq->out[nrpages_out - 1] != rq->in[0] ||
158                     rq->inputsize - oend <
159                       LZ4_DECOMPRESS_INPLACE_MARGIN(inlen)) {
160                         src = generic_copy_inplace_data(rq, src, inputmargin);
161                         inputmargin = 0;
162                         copied = true;
163                 } else {
164                         src = obase + ((nrpages_out - 1) << PAGE_SHIFT);
165                 }
166         }
167
168         /* legacy format could compress extra data in a pcluster. */
169         if (rq->partial_decoding || !support_0padding)
170                 ret = LZ4_decompress_safe_partial(src + inputmargin, out,
171                                                   inlen, rq->outputsize,
172                                                   rq->outputsize);
173         else
174                 ret = LZ4_decompress_safe(src + inputmargin, out,
175                                           inlen, rq->outputsize);
176
177         if (ret != rq->outputsize) {
178                 erofs_err(rq->sb, "failed to decompress %d in[%u, %u] out[%u]",
179                           ret, inlen, inputmargin, rq->outputsize);
180
181                 print_hex_dump(KERN_DEBUG, "[ in]: ", DUMP_PREFIX_OFFSET,
182                                16, 1, src + inputmargin, inlen, true);
183                 print_hex_dump(KERN_DEBUG, "[out]: ", DUMP_PREFIX_OFFSET,
184                                16, 1, out, rq->outputsize, true);
185
186                 if (ret >= 0)
187                         memset(out + ret, 0, rq->outputsize - ret);
188                 ret = -EIO;
189         }
190
191         if (copied)
192                 erofs_put_pcpubuf(src);
193         else
194                 kunmap_atomic(src2);
195         return ret;
196 }
197
198 static struct z_erofs_decompressor decompressors[] = {
199         [Z_EROFS_COMPRESSION_SHIFTED] = {
200                 .name = "shifted"
201         },
202         [Z_EROFS_COMPRESSION_LZ4] = {
203                 .prepare_destpages = z_erofs_lz4_prepare_destpages,
204                 .decompress = z_erofs_lz4_decompress,
205                 .name = "lz4"
206         },
207 };
208
209 static void copy_from_pcpubuf(struct page **out, const char *dst,
210                               unsigned short pageofs_out,
211                               unsigned int outputsize)
212 {
213         const char *end = dst + outputsize;
214         const unsigned int righthalf = PAGE_SIZE - pageofs_out;
215         const char *cur = dst - pageofs_out;
216
217         while (cur < end) {
218                 struct page *const page = *out++;
219
220                 if (page) {
221                         char *buf = kmap_atomic(page);
222
223                         if (cur >= dst) {
224                                 memcpy(buf, cur, min_t(uint, PAGE_SIZE,
225                                                        end - cur));
226                         } else {
227                                 memcpy(buf + pageofs_out, cur + pageofs_out,
228                                        min_t(uint, righthalf, end - cur));
229                         }
230                         kunmap_atomic(buf);
231                 }
232                 cur += PAGE_SIZE;
233         }
234 }
235
236 static int z_erofs_decompress_generic(struct z_erofs_decompress_req *rq,
237                                       struct list_head *pagepool)
238 {
239         const unsigned int nrpages_out =
240                 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
241         const struct z_erofs_decompressor *alg = decompressors + rq->alg;
242         unsigned int dst_maptype;
243         void *dst;
244         int ret, i;
245
246         if (nrpages_out == 1 && !rq->inplace_io) {
247                 DBG_BUGON(!*rq->out);
248                 dst = kmap_atomic(*rq->out);
249                 dst_maptype = 0;
250                 goto dstmap_out;
251         }
252
253         /*
254          * For the case of small output size (especially much less
255          * than PAGE_SIZE), memcpy the decompressed data rather than
256          * compressed data is preferred.
257          */
258         if (rq->outputsize <= PAGE_SIZE * 7 / 8) {
259                 dst = erofs_get_pcpubuf(0);
260                 if (IS_ERR(dst))
261                         return PTR_ERR(dst);
262
263                 rq->inplace_io = false;
264                 ret = alg->decompress(rq, dst, NULL);
265                 if (!ret)
266                         copy_from_pcpubuf(rq->out, dst, rq->pageofs_out,
267                                           rq->outputsize);
268
269                 erofs_put_pcpubuf(dst);
270                 return ret;
271         }
272
273         ret = alg->prepare_destpages(rq, pagepool);
274         if (ret < 0) {
275                 return ret;
276         } else if (ret) {
277                 dst = page_address(*rq->out);
278                 dst_maptype = 1;
279                 goto dstmap_out;
280         }
281
282         i = 0;
283         while (1) {
284                 dst = vm_map_ram(rq->out, nrpages_out, -1);
285
286                 /* retry two more times (totally 3 times) */
287                 if (dst || ++i >= 3)
288                         break;
289                 vm_unmap_aliases();
290         }
291
292         if (!dst)
293                 return -ENOMEM;
294
295         dst_maptype = 2;
296
297 dstmap_out:
298         ret = alg->decompress(rq, dst + rq->pageofs_out, dst);
299
300         if (!dst_maptype)
301                 kunmap_atomic(dst);
302         else if (dst_maptype == 2)
303                 vm_unmap_ram(dst, nrpages_out);
304         return ret;
305 }
306
307 static int z_erofs_shifted_transform(const struct z_erofs_decompress_req *rq,
308                                      struct list_head *pagepool)
309 {
310         const unsigned int nrpages_out =
311                 PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
312         const unsigned int righthalf = PAGE_SIZE - rq->pageofs_out;
313         unsigned char *src, *dst;
314
315         if (nrpages_out > 2) {
316                 DBG_BUGON(1);
317                 return -EIO;
318         }
319
320         if (rq->out[0] == *rq->in) {
321                 DBG_BUGON(nrpages_out != 1);
322                 return 0;
323         }
324
325         src = kmap_atomic(*rq->in);
326         if (rq->out[0]) {
327                 dst = kmap_atomic(rq->out[0]);
328                 memcpy(dst + rq->pageofs_out, src, righthalf);
329                 kunmap_atomic(dst);
330         }
331
332         if (nrpages_out == 2) {
333                 DBG_BUGON(!rq->out[1]);
334                 if (rq->out[1] == *rq->in) {
335                         memmove(src, src + righthalf, rq->pageofs_out);
336                 } else {
337                         dst = kmap_atomic(rq->out[1]);
338                         memcpy(dst, src + righthalf, rq->pageofs_out);
339                         kunmap_atomic(dst);
340                 }
341         }
342         kunmap_atomic(src);
343         return 0;
344 }
345
346 int z_erofs_decompress(struct z_erofs_decompress_req *rq,
347                        struct list_head *pagepool)
348 {
349         if (rq->alg == Z_EROFS_COMPRESSION_SHIFTED)
350                 return z_erofs_shifted_transform(rq, pagepool);
351         return z_erofs_decompress_generic(rq, pagepool);
352 }
353