blob: b7cb1faa652d54ef51a93d661cd7744ea44b4ed0 [file] [log] [blame]
Thomas Gleixner3e0a4e82019-05-23 11:14:55 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Sean Purcell87bf54b2017-08-09 19:42:36 -07002/*
3 * Squashfs - a compressed read only filesystem for Linux
4 *
5 * Copyright (c) 2016-present, Facebook, Inc.
6 * All rights reserved.
7 *
Sean Purcell87bf54b2017-08-09 19:42:36 -07008 * zstd_wrapper.c
9 */
10
11#include <linux/mutex.h>
Philippe Liard93e72b32020-06-01 21:45:23 -070012#include <linux/bio.h>
Sean Purcell87bf54b2017-08-09 19:42:36 -070013#include <linux/slab.h>
14#include <linux/zstd.h>
15#include <linux/vmalloc.h>
16
17#include "squashfs_fs.h"
18#include "squashfs_fs_sb.h"
19#include "squashfs.h"
20#include "decompressor.h"
21#include "page_actor.h"
22
23struct workspace {
24 void *mem;
25 size_t mem_size;
26 size_t window_size;
27};
28
29static void *zstd_init(struct squashfs_sb_info *msblk, void *buff)
30{
31 struct workspace *wksp = kmalloc(sizeof(*wksp), GFP_KERNEL);
32
33 if (wksp == NULL)
34 goto failed;
35 wksp->window_size = max_t(size_t,
36 msblk->block_size, SQUASHFS_METADATA_SIZE);
37 wksp->mem_size = ZSTD_DStreamWorkspaceBound(wksp->window_size);
38 wksp->mem = vmalloc(wksp->mem_size);
39 if (wksp->mem == NULL)
40 goto failed;
41
42 return wksp;
43
44failed:
45 ERROR("Failed to allocate zstd workspace\n");
46 kfree(wksp);
47 return ERR_PTR(-ENOMEM);
48}
49
50
51static void zstd_free(void *strm)
52{
53 struct workspace *wksp = strm;
54
55 if (wksp)
56 vfree(wksp->mem);
57 kfree(wksp);
58}
59
60
61static int zstd_uncompress(struct squashfs_sb_info *msblk, void *strm,
Philippe Liard93e72b32020-06-01 21:45:23 -070062 struct bio *bio, int offset, int length,
Sean Purcell87bf54b2017-08-09 19:42:36 -070063 struct squashfs_page_actor *output)
64{
65 struct workspace *wksp = strm;
66 ZSTD_DStream *stream;
67 size_t total_out = 0;
Philippe Liard93e72b32020-06-01 21:45:23 -070068 int error = 0;
Sean Purcell87bf54b2017-08-09 19:42:36 -070069 ZSTD_inBuffer in_buf = { NULL, 0, 0 };
70 ZSTD_outBuffer out_buf = { NULL, 0, 0 };
Philippe Liard93e72b32020-06-01 21:45:23 -070071 struct bvec_iter_all iter_all = {};
72 struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
Sean Purcell87bf54b2017-08-09 19:42:36 -070073
74 stream = ZSTD_initDStream(wksp->window_size, wksp->mem, wksp->mem_size);
75
76 if (!stream) {
77 ERROR("Failed to initialize zstd decompressor\n");
Philippe Liard93e72b32020-06-01 21:45:23 -070078 return -EIO;
Sean Purcell87bf54b2017-08-09 19:42:36 -070079 }
80
81 out_buf.size = PAGE_SIZE;
82 out_buf.dst = squashfs_first_page(output);
83
Philippe Liard93e72b32020-06-01 21:45:23 -070084 for (;;) {
85 size_t zstd_err;
Sean Purcell87bf54b2017-08-09 19:42:36 -070086
Philippe Liard93e72b32020-06-01 21:45:23 -070087 if (in_buf.pos == in_buf.size) {
88 const void *data;
89 int avail;
90
91 if (!bio_next_segment(bio, &iter_all)) {
92 error = -EIO;
93 break;
94 }
95
96 avail = min(length, ((int)bvec->bv_len) - offset);
97 data = page_address(bvec->bv_page) + bvec->bv_offset;
Sean Purcell87bf54b2017-08-09 19:42:36 -070098 length -= avail;
Philippe Liard93e72b32020-06-01 21:45:23 -070099 in_buf.src = data + offset;
Sean Purcell87bf54b2017-08-09 19:42:36 -0700100 in_buf.size = avail;
101 in_buf.pos = 0;
102 offset = 0;
103 }
104
105 if (out_buf.pos == out_buf.size) {
106 out_buf.dst = squashfs_next_page(output);
107 if (out_buf.dst == NULL) {
108 /* Shouldn't run out of pages
109 * before stream is done.
110 */
Philippe Liard93e72b32020-06-01 21:45:23 -0700111 error = -EIO;
112 break;
Sean Purcell87bf54b2017-08-09 19:42:36 -0700113 }
114 out_buf.pos = 0;
115 out_buf.size = PAGE_SIZE;
116 }
117
118 total_out -= out_buf.pos;
119 zstd_err = ZSTD_decompressStream(stream, &out_buf, &in_buf);
120 total_out += out_buf.pos; /* add the additional data produced */
Philippe Liard93e72b32020-06-01 21:45:23 -0700121 if (zstd_err == 0)
122 break;
Sean Purcell87bf54b2017-08-09 19:42:36 -0700123
Philippe Liard93e72b32020-06-01 21:45:23 -0700124 if (ZSTD_isError(zstd_err)) {
125 ERROR("zstd decompression error: %d\n",
126 (int)ZSTD_getErrorCode(zstd_err));
127 error = -EIO;
128 break;
129 }
130 }
Sean Purcell87bf54b2017-08-09 19:42:36 -0700131
132 squashfs_finish_page(output);
133
Philippe Liard93e72b32020-06-01 21:45:23 -0700134 return error ? error : total_out;
Sean Purcell87bf54b2017-08-09 19:42:36 -0700135}
136
137const struct squashfs_decompressor squashfs_zstd_comp_ops = {
138 .init = zstd_init,
139 .free = zstd_free,
140 .decompress = zstd_uncompress,
141 .id = ZSTD_COMPRESSION,
142 .name = "zstd",
143 .supported = 1
144};