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