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