1 // SPDX-License-Identifier: GPL-2.0-or-later
2 #include <linux/module.h>
3 #include <linux/zlib.h>
4 #include "compress.h"
5 
6 struct z_erofs_deflate {
7 	struct z_erofs_deflate *next;
8 	struct z_stream_s z;
9 	u8 bounce[PAGE_SIZE];
10 };
11 
12 static DEFINE_SPINLOCK(z_erofs_deflate_lock);
13 static unsigned int z_erofs_deflate_nstrms, z_erofs_deflate_avail_strms;
14 static struct z_erofs_deflate *z_erofs_deflate_head;
15 static DECLARE_WAIT_QUEUE_HEAD(z_erofs_deflate_wq);
16 
17 module_param_named(deflate_streams, z_erofs_deflate_nstrms, uint, 0444);
18 
19 void z_erofs_deflate_exit(void)
20 {
21 	/* there should be no running fs instance */
22 	while (z_erofs_deflate_avail_strms) {
23 		struct z_erofs_deflate *strm;
24 
25 		spin_lock(&z_erofs_deflate_lock);
26 		strm = z_erofs_deflate_head;
27 		if (!strm) {
28 			spin_unlock(&z_erofs_deflate_lock);
29 			continue;
30 		}
31 		z_erofs_deflate_head = NULL;
32 		spin_unlock(&z_erofs_deflate_lock);
33 
34 		while (strm) {
35 			struct z_erofs_deflate *n = strm->next;
36 
37 			vfree(strm->z.workspace);
38 			kfree(strm);
39 			--z_erofs_deflate_avail_strms;
40 			strm = n;
41 		}
42 	}
43 }
44 
45 int __init z_erofs_deflate_init(void)
46 {
47 	/* by default, use # of possible CPUs instead */
48 	if (!z_erofs_deflate_nstrms)
49 		z_erofs_deflate_nstrms = num_possible_cpus();
50 	return 0;
51 }
52 
53 int z_erofs_load_deflate_config(struct super_block *sb,
54 			struct erofs_super_block *dsb, void *data, int size)
55 {
56 	struct z_erofs_deflate_cfgs *dfl = data;
57 	static DEFINE_MUTEX(deflate_resize_mutex);
58 	static bool inited;
59 
60 	if (!dfl || size < sizeof(struct z_erofs_deflate_cfgs)) {
61 		erofs_err(sb, "invalid deflate cfgs, size=%u", size);
62 		return -EINVAL;
63 	}
64 
65 	if (dfl->windowbits > MAX_WBITS) {
66 		erofs_err(sb, "unsupported windowbits %u", dfl->windowbits);
67 		return -EOPNOTSUPP;
68 	}
69 	mutex_lock(&deflate_resize_mutex);
70 	if (!inited) {
71 		for (; z_erofs_deflate_avail_strms < z_erofs_deflate_nstrms;
72 		     ++z_erofs_deflate_avail_strms) {
73 			struct z_erofs_deflate *strm;
74 
75 			strm = kzalloc(sizeof(*strm), GFP_KERNEL);
76 			if (!strm)
77 				goto failed;
78 			/* XXX: in-kernel zlib cannot customize windowbits */
79 			strm->z.workspace = vmalloc(zlib_inflate_workspacesize());
80 			if (!strm->z.workspace) {
81 				kfree(strm);
82 				goto failed;
83 			}
84 
85 			spin_lock(&z_erofs_deflate_lock);
86 			strm->next = z_erofs_deflate_head;
87 			z_erofs_deflate_head = strm;
88 			spin_unlock(&z_erofs_deflate_lock);
89 		}
90 		inited = true;
91 	}
92 	mutex_unlock(&deflate_resize_mutex);
93 	erofs_info(sb, "EXPERIMENTAL DEFLATE feature in use. Use at your own risk!");
94 	return 0;
95 failed:
96 	mutex_unlock(&deflate_resize_mutex);
97 	z_erofs_deflate_exit();
98 	return -ENOMEM;
99 }
100 
101 int z_erofs_deflate_decompress(struct z_erofs_decompress_req *rq,
102 			       struct page **pagepool)
103 {
104 	const unsigned int nrpages_out =
105 		PAGE_ALIGN(rq->pageofs_out + rq->outputsize) >> PAGE_SHIFT;
106 	const unsigned int nrpages_in =
107 		PAGE_ALIGN(rq->inputsize) >> PAGE_SHIFT;
108 	struct super_block *sb = rq->sb;
109 	unsigned int insz, outsz, pofs;
110 	struct z_erofs_deflate *strm;
111 	u8 *kin, *kout = NULL;
112 	bool bounced = false;
113 	int no = -1, ni = 0, j = 0, zerr, err;
114 
115 	/* 1. get the exact DEFLATE compressed size */
116 	kin = kmap_local_page(*rq->in);
117 	err = z_erofs_fixup_insize(rq, kin + rq->pageofs_in,
118 			min_t(unsigned int, rq->inputsize,
119 			      sb->s_blocksize - rq->pageofs_in));
120 	if (err) {
121 		kunmap_local(kin);
122 		return err;
123 	}
124 
125 	/* 2. get an available DEFLATE context */
126 again:
127 	spin_lock(&z_erofs_deflate_lock);
128 	strm = z_erofs_deflate_head;
129 	if (!strm) {
130 		spin_unlock(&z_erofs_deflate_lock);
131 		wait_event(z_erofs_deflate_wq, READ_ONCE(z_erofs_deflate_head));
132 		goto again;
133 	}
134 	z_erofs_deflate_head = strm->next;
135 	spin_unlock(&z_erofs_deflate_lock);
136 
137 	/* 3. multi-call decompress */
138 	insz = rq->inputsize;
139 	outsz = rq->outputsize;
140 	zerr = zlib_inflateInit2(&strm->z, -MAX_WBITS);
141 	if (zerr != Z_OK) {
142 		err = -EIO;
143 		goto failed_zinit;
144 	}
145 
146 	pofs = rq->pageofs_out;
147 	strm->z.avail_in = min_t(u32, insz, PAGE_SIZE - rq->pageofs_in);
148 	insz -= strm->z.avail_in;
149 	strm->z.next_in = kin + rq->pageofs_in;
150 	strm->z.avail_out = 0;
151 
152 	while (1) {
153 		if (!strm->z.avail_out) {
154 			if (++no >= nrpages_out || !outsz) {
155 				erofs_err(sb, "insufficient space for decompressed data");
156 				err = -EFSCORRUPTED;
157 				break;
158 			}
159 
160 			if (kout)
161 				kunmap_local(kout);
162 			strm->z.avail_out = min_t(u32, outsz, PAGE_SIZE - pofs);
163 			outsz -= strm->z.avail_out;
164 			if (!rq->out[no]) {
165 				rq->out[no] = erofs_allocpage(pagepool,
166 						GFP_KERNEL | __GFP_NOFAIL);
167 				set_page_private(rq->out[no],
168 						 Z_EROFS_SHORTLIVED_PAGE);
169 			}
170 			kout = kmap_local_page(rq->out[no]);
171 			strm->z.next_out = kout + pofs;
172 			pofs = 0;
173 		}
174 
175 		if (!strm->z.avail_in && insz) {
176 			if (++ni >= nrpages_in) {
177 				erofs_err(sb, "invalid compressed data");
178 				err = -EFSCORRUPTED;
179 				break;
180 			}
181 
182 			if (kout) { /* unlike kmap(), take care of the orders */
183 				j = strm->z.next_out - kout;
184 				kunmap_local(kout);
185 			}
186 			kunmap_local(kin);
187 			strm->z.avail_in = min_t(u32, insz, PAGE_SIZE);
188 			insz -= strm->z.avail_in;
189 			kin = kmap_local_page(rq->in[ni]);
190 			strm->z.next_in = kin;
191 			bounced = false;
192 			if (kout) {
193 				kout = kmap_local_page(rq->out[no]);
194 				strm->z.next_out = kout + j;
195 			}
196 		}
197 
198 		/*
199 		 * Handle overlapping: Use bounced buffer if the compressed
200 		 * data is under processing; Or use short-lived pages from the
201 		 * on-stack pagepool where pages share among the same request
202 		 * and not _all_ inplace I/O pages are needed to be doubled.
203 		 */
204 		if (!bounced && rq->out[no] == rq->in[ni]) {
205 			memcpy(strm->bounce, strm->z.next_in, strm->z.avail_in);
206 			strm->z.next_in = strm->bounce;
207 			bounced = true;
208 		}
209 
210 		for (j = ni + 1; j < nrpages_in; ++j) {
211 			struct page *tmppage;
212 
213 			if (rq->out[no] != rq->in[j])
214 				continue;
215 
216 			DBG_BUGON(erofs_page_is_managed(EROFS_SB(sb),
217 							rq->in[j]));
218 			tmppage = erofs_allocpage(pagepool,
219 						  GFP_KERNEL | __GFP_NOFAIL);
220 			set_page_private(tmppage, Z_EROFS_SHORTLIVED_PAGE);
221 			copy_highpage(tmppage, rq->in[j]);
222 			rq->in[j] = tmppage;
223 		}
224 
225 		zerr = zlib_inflate(&strm->z, Z_SYNC_FLUSH);
226 		if (zerr != Z_OK || !(outsz + strm->z.avail_out)) {
227 			if (zerr == Z_OK && rq->partial_decoding)
228 				break;
229 			if (zerr == Z_STREAM_END && !outsz)
230 				break;
231 			erofs_err(sb, "failed to decompress %d in[%u] out[%u]",
232 				  zerr, rq->inputsize, rq->outputsize);
233 			err = -EFSCORRUPTED;
234 			break;
235 		}
236 	}
237 
238 	if (zlib_inflateEnd(&strm->z) != Z_OK && !err)
239 		err = -EIO;
240 	if (kout)
241 		kunmap_local(kout);
242 failed_zinit:
243 	kunmap_local(kin);
244 	/* 4. push back DEFLATE stream context to the global list */
245 	spin_lock(&z_erofs_deflate_lock);
246 	strm->next = z_erofs_deflate_head;
247 	z_erofs_deflate_head = strm;
248 	spin_unlock(&z_erofs_deflate_lock);
249 	wake_up(&z_erofs_deflate_wq);
250 	return err;
251 }
252