a951d4fe77f7de51607c42c91d2dd5820bb26932
[sfrench/cifs-2.6.git] / fs / btrfs / zstd.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016-present, Facebook, Inc.
4  * All rights reserved.
5  *
6  */
7
8 #include <linux/bio.h>
9 #include <linux/err.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/mm.h>
13 #include <linux/pagemap.h>
14 #include <linux/refcount.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/zstd.h>
18 #include "compression.h"
19
20 #define ZSTD_BTRFS_MAX_WINDOWLOG 17
21 #define ZSTD_BTRFS_MAX_INPUT (1 << ZSTD_BTRFS_MAX_WINDOWLOG)
22 #define ZSTD_BTRFS_DEFAULT_LEVEL 3
23
24 static ZSTD_parameters zstd_get_btrfs_parameters(unsigned int level,
25                                                  size_t src_len)
26 {
27         ZSTD_parameters params = ZSTD_getParams(level, src_len, 0);
28
29         if (params.cParams.windowLog > ZSTD_BTRFS_MAX_WINDOWLOG)
30                 params.cParams.windowLog = ZSTD_BTRFS_MAX_WINDOWLOG;
31         WARN_ON(src_len > ZSTD_BTRFS_MAX_INPUT);
32         return params;
33 }
34
35 struct workspace {
36         void *mem;
37         size_t size;
38         char *buf;
39         unsigned int req_level;
40         struct list_head list;
41         ZSTD_inBuffer in_buf;
42         ZSTD_outBuffer out_buf;
43 };
44
45 static struct workspace_manager wsm;
46
47 static void zstd_init_workspace_manager(void)
48 {
49         btrfs_init_workspace_manager(&wsm, &btrfs_zstd_compress);
50 }
51
52 static void zstd_cleanup_workspace_manager(void)
53 {
54         btrfs_cleanup_workspace_manager(&wsm);
55 }
56
57 static struct list_head *zstd_get_workspace(unsigned int level)
58 {
59         struct list_head *ws = btrfs_get_workspace(&wsm, level);
60         struct workspace *workspace = list_entry(ws, struct workspace, list);
61
62         workspace->req_level = level;
63
64         return ws;
65 }
66
67 static void zstd_put_workspace(struct list_head *ws)
68 {
69         btrfs_put_workspace(&wsm, ws);
70 }
71
72 static void zstd_free_workspace(struct list_head *ws)
73 {
74         struct workspace *workspace = list_entry(ws, struct workspace, list);
75
76         kvfree(workspace->mem);
77         kfree(workspace->buf);
78         kfree(workspace);
79 }
80
81 static struct list_head *zstd_alloc_workspace(unsigned int level)
82 {
83         ZSTD_parameters params =
84                         zstd_get_btrfs_parameters(level, ZSTD_BTRFS_MAX_INPUT);
85         struct workspace *workspace;
86
87         workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
88         if (!workspace)
89                 return ERR_PTR(-ENOMEM);
90
91         workspace->size = max_t(size_t,
92                         ZSTD_CStreamWorkspaceBound(params.cParams),
93                         ZSTD_DStreamWorkspaceBound(ZSTD_BTRFS_MAX_INPUT));
94         workspace->mem = kvmalloc(workspace->size, GFP_KERNEL);
95         workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
96         if (!workspace->mem || !workspace->buf)
97                 goto fail;
98
99         INIT_LIST_HEAD(&workspace->list);
100
101         return &workspace->list;
102 fail:
103         zstd_free_workspace(&workspace->list);
104         return ERR_PTR(-ENOMEM);
105 }
106
107 static int zstd_compress_pages(struct list_head *ws,
108                 struct address_space *mapping,
109                 u64 start,
110                 struct page **pages,
111                 unsigned long *out_pages,
112                 unsigned long *total_in,
113                 unsigned long *total_out)
114 {
115         struct workspace *workspace = list_entry(ws, struct workspace, list);
116         ZSTD_CStream *stream;
117         int ret = 0;
118         int nr_pages = 0;
119         struct page *in_page = NULL;  /* The current page to read */
120         struct page *out_page = NULL; /* The current page to write to */
121         unsigned long tot_in = 0;
122         unsigned long tot_out = 0;
123         unsigned long len = *total_out;
124         const unsigned long nr_dest_pages = *out_pages;
125         unsigned long max_out = nr_dest_pages * PAGE_SIZE;
126         ZSTD_parameters params = zstd_get_btrfs_parameters(workspace->req_level,
127                                                            len);
128
129         *out_pages = 0;
130         *total_out = 0;
131         *total_in = 0;
132
133         /* Initialize the stream */
134         stream = ZSTD_initCStream(params, len, workspace->mem,
135                         workspace->size);
136         if (!stream) {
137                 pr_warn("BTRFS: ZSTD_initCStream failed\n");
138                 ret = -EIO;
139                 goto out;
140         }
141
142         /* map in the first page of input data */
143         in_page = find_get_page(mapping, start >> PAGE_SHIFT);
144         workspace->in_buf.src = kmap(in_page);
145         workspace->in_buf.pos = 0;
146         workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
147
148
149         /* Allocate and map in the output buffer */
150         out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
151         if (out_page == NULL) {
152                 ret = -ENOMEM;
153                 goto out;
154         }
155         pages[nr_pages++] = out_page;
156         workspace->out_buf.dst = kmap(out_page);
157         workspace->out_buf.pos = 0;
158         workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
159
160         while (1) {
161                 size_t ret2;
162
163                 ret2 = ZSTD_compressStream(stream, &workspace->out_buf,
164                                 &workspace->in_buf);
165                 if (ZSTD_isError(ret2)) {
166                         pr_debug("BTRFS: ZSTD_compressStream returned %d\n",
167                                         ZSTD_getErrorCode(ret2));
168                         ret = -EIO;
169                         goto out;
170                 }
171
172                 /* Check to see if we are making it bigger */
173                 if (tot_in + workspace->in_buf.pos > 8192 &&
174                                 tot_in + workspace->in_buf.pos <
175                                 tot_out + workspace->out_buf.pos) {
176                         ret = -E2BIG;
177                         goto out;
178                 }
179
180                 /* We've reached the end of our output range */
181                 if (workspace->out_buf.pos >= max_out) {
182                         tot_out += workspace->out_buf.pos;
183                         ret = -E2BIG;
184                         goto out;
185                 }
186
187                 /* Check if we need more output space */
188                 if (workspace->out_buf.pos == workspace->out_buf.size) {
189                         tot_out += PAGE_SIZE;
190                         max_out -= PAGE_SIZE;
191                         kunmap(out_page);
192                         if (nr_pages == nr_dest_pages) {
193                                 out_page = NULL;
194                                 ret = -E2BIG;
195                                 goto out;
196                         }
197                         out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
198                         if (out_page == NULL) {
199                                 ret = -ENOMEM;
200                                 goto out;
201                         }
202                         pages[nr_pages++] = out_page;
203                         workspace->out_buf.dst = kmap(out_page);
204                         workspace->out_buf.pos = 0;
205                         workspace->out_buf.size = min_t(size_t, max_out,
206                                                         PAGE_SIZE);
207                 }
208
209                 /* We've reached the end of the input */
210                 if (workspace->in_buf.pos >= len) {
211                         tot_in += workspace->in_buf.pos;
212                         break;
213                 }
214
215                 /* Check if we need more input */
216                 if (workspace->in_buf.pos == workspace->in_buf.size) {
217                         tot_in += PAGE_SIZE;
218                         kunmap(in_page);
219                         put_page(in_page);
220
221                         start += PAGE_SIZE;
222                         len -= PAGE_SIZE;
223                         in_page = find_get_page(mapping, start >> PAGE_SHIFT);
224                         workspace->in_buf.src = kmap(in_page);
225                         workspace->in_buf.pos = 0;
226                         workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
227                 }
228         }
229         while (1) {
230                 size_t ret2;
231
232                 ret2 = ZSTD_endStream(stream, &workspace->out_buf);
233                 if (ZSTD_isError(ret2)) {
234                         pr_debug("BTRFS: ZSTD_endStream returned %d\n",
235                                         ZSTD_getErrorCode(ret2));
236                         ret = -EIO;
237                         goto out;
238                 }
239                 if (ret2 == 0) {
240                         tot_out += workspace->out_buf.pos;
241                         break;
242                 }
243                 if (workspace->out_buf.pos >= max_out) {
244                         tot_out += workspace->out_buf.pos;
245                         ret = -E2BIG;
246                         goto out;
247                 }
248
249                 tot_out += PAGE_SIZE;
250                 max_out -= PAGE_SIZE;
251                 kunmap(out_page);
252                 if (nr_pages == nr_dest_pages) {
253                         out_page = NULL;
254                         ret = -E2BIG;
255                         goto out;
256                 }
257                 out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
258                 if (out_page == NULL) {
259                         ret = -ENOMEM;
260                         goto out;
261                 }
262                 pages[nr_pages++] = out_page;
263                 workspace->out_buf.dst = kmap(out_page);
264                 workspace->out_buf.pos = 0;
265                 workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
266         }
267
268         if (tot_out >= tot_in) {
269                 ret = -E2BIG;
270                 goto out;
271         }
272
273         ret = 0;
274         *total_in = tot_in;
275         *total_out = tot_out;
276 out:
277         *out_pages = nr_pages;
278         /* Cleanup */
279         if (in_page) {
280                 kunmap(in_page);
281                 put_page(in_page);
282         }
283         if (out_page)
284                 kunmap(out_page);
285         return ret;
286 }
287
288 static int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
289 {
290         struct workspace *workspace = list_entry(ws, struct workspace, list);
291         struct page **pages_in = cb->compressed_pages;
292         u64 disk_start = cb->start;
293         struct bio *orig_bio = cb->orig_bio;
294         size_t srclen = cb->compressed_len;
295         ZSTD_DStream *stream;
296         int ret = 0;
297         unsigned long page_in_index = 0;
298         unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
299         unsigned long buf_start;
300         unsigned long total_out = 0;
301
302         stream = ZSTD_initDStream(
303                         ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
304         if (!stream) {
305                 pr_debug("BTRFS: ZSTD_initDStream failed\n");
306                 ret = -EIO;
307                 goto done;
308         }
309
310         workspace->in_buf.src = kmap(pages_in[page_in_index]);
311         workspace->in_buf.pos = 0;
312         workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
313
314         workspace->out_buf.dst = workspace->buf;
315         workspace->out_buf.pos = 0;
316         workspace->out_buf.size = PAGE_SIZE;
317
318         while (1) {
319                 size_t ret2;
320
321                 ret2 = ZSTD_decompressStream(stream, &workspace->out_buf,
322                                 &workspace->in_buf);
323                 if (ZSTD_isError(ret2)) {
324                         pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
325                                         ZSTD_getErrorCode(ret2));
326                         ret = -EIO;
327                         goto done;
328                 }
329                 buf_start = total_out;
330                 total_out += workspace->out_buf.pos;
331                 workspace->out_buf.pos = 0;
332
333                 ret = btrfs_decompress_buf2page(workspace->out_buf.dst,
334                                 buf_start, total_out, disk_start, orig_bio);
335                 if (ret == 0)
336                         break;
337
338                 if (workspace->in_buf.pos >= srclen)
339                         break;
340
341                 /* Check if we've hit the end of a frame */
342                 if (ret2 == 0)
343                         break;
344
345                 if (workspace->in_buf.pos == workspace->in_buf.size) {
346                         kunmap(pages_in[page_in_index++]);
347                         if (page_in_index >= total_pages_in) {
348                                 workspace->in_buf.src = NULL;
349                                 ret = -EIO;
350                                 goto done;
351                         }
352                         srclen -= PAGE_SIZE;
353                         workspace->in_buf.src = kmap(pages_in[page_in_index]);
354                         workspace->in_buf.pos = 0;
355                         workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
356                 }
357         }
358         ret = 0;
359         zero_fill_bio(orig_bio);
360 done:
361         if (workspace->in_buf.src)
362                 kunmap(pages_in[page_in_index]);
363         return ret;
364 }
365
366 static int zstd_decompress(struct list_head *ws, unsigned char *data_in,
367                 struct page *dest_page,
368                 unsigned long start_byte,
369                 size_t srclen, size_t destlen)
370 {
371         struct workspace *workspace = list_entry(ws, struct workspace, list);
372         ZSTD_DStream *stream;
373         int ret = 0;
374         size_t ret2;
375         unsigned long total_out = 0;
376         unsigned long pg_offset = 0;
377         char *kaddr;
378
379         stream = ZSTD_initDStream(
380                         ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
381         if (!stream) {
382                 pr_warn("BTRFS: ZSTD_initDStream failed\n");
383                 ret = -EIO;
384                 goto finish;
385         }
386
387         destlen = min_t(size_t, destlen, PAGE_SIZE);
388
389         workspace->in_buf.src = data_in;
390         workspace->in_buf.pos = 0;
391         workspace->in_buf.size = srclen;
392
393         workspace->out_buf.dst = workspace->buf;
394         workspace->out_buf.pos = 0;
395         workspace->out_buf.size = PAGE_SIZE;
396
397         ret2 = 1;
398         while (pg_offset < destlen
399                && workspace->in_buf.pos < workspace->in_buf.size) {
400                 unsigned long buf_start;
401                 unsigned long buf_offset;
402                 unsigned long bytes;
403
404                 /* Check if the frame is over and we still need more input */
405                 if (ret2 == 0) {
406                         pr_debug("BTRFS: ZSTD_decompressStream ended early\n");
407                         ret = -EIO;
408                         goto finish;
409                 }
410                 ret2 = ZSTD_decompressStream(stream, &workspace->out_buf,
411                                 &workspace->in_buf);
412                 if (ZSTD_isError(ret2)) {
413                         pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
414                                         ZSTD_getErrorCode(ret2));
415                         ret = -EIO;
416                         goto finish;
417                 }
418
419                 buf_start = total_out;
420                 total_out += workspace->out_buf.pos;
421                 workspace->out_buf.pos = 0;
422
423                 if (total_out <= start_byte)
424                         continue;
425
426                 if (total_out > start_byte && buf_start < start_byte)
427                         buf_offset = start_byte - buf_start;
428                 else
429                         buf_offset = 0;
430
431                 bytes = min_t(unsigned long, destlen - pg_offset,
432                                 workspace->out_buf.size - buf_offset);
433
434                 kaddr = kmap_atomic(dest_page);
435                 memcpy(kaddr + pg_offset, workspace->out_buf.dst + buf_offset,
436                                 bytes);
437                 kunmap_atomic(kaddr);
438
439                 pg_offset += bytes;
440         }
441         ret = 0;
442 finish:
443         if (pg_offset < destlen) {
444                 kaddr = kmap_atomic(dest_page);
445                 memset(kaddr + pg_offset, 0, destlen - pg_offset);
446                 kunmap_atomic(kaddr);
447         }
448         return ret;
449 }
450
451 static unsigned int zstd_set_level(unsigned int level)
452 {
453         return ZSTD_BTRFS_DEFAULT_LEVEL;
454 }
455
456 const struct btrfs_compress_op btrfs_zstd_compress = {
457         .init_workspace_manager = zstd_init_workspace_manager,
458         .cleanup_workspace_manager = zstd_cleanup_workspace_manager,
459         .get_workspace = zstd_get_workspace,
460         .put_workspace = zstd_put_workspace,
461         .alloc_workspace = zstd_alloc_workspace,
462         .free_workspace = zstd_free_workspace,
463         .compress_pages = zstd_compress_pages,
464         .decompress_bio = zstd_decompress_bio,
465         .decompress = zstd_decompress,
466         .set_level = zstd_set_level,
467 };