1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -*- linux-c -*- ------------------------------------------------------- *
3 *
4 * Copyright 2001 H. Peter Anvin - All Rights Reserved
5 *
6 * ----------------------------------------------------------------------- */
7
8 /*
9 * linux/fs/isofs/compress.c
10 *
11 * Transparent decompression of files on an iso9660 filesystem
12 */
13
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/bio.h>
17
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/zlib.h>
21
22 #include "isofs.h"
23 #include "zisofs.h"
24
25 /* This should probably be global. */
26 static char zisofs_sink_page[PAGE_SIZE];
27
28 /*
29 * This contains the zlib memory allocation and the mutex for the
30 * allocation; this avoids failures at block-decompression time.
31 */
32 static void *zisofs_zlib_workspace;
33 static DEFINE_MUTEX(zisofs_zlib_lock);
34
35 /*
36 * Read data of @inode from @block_start to @block_end and uncompress
37 * to one zisofs block. Store the data in the @pages array with @pcount
38 * entries. Start storing at offset @poffset of the first page.
39 */
zisofs_uncompress_block(struct inode * inode,loff_t block_start,loff_t block_end,int pcount,struct page ** pages,unsigned poffset,int * errp)40 static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start,
41 loff_t block_end, int pcount,
42 struct page **pages, unsigned poffset,
43 int *errp)
44 {
45 unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
46 unsigned int bufsize = ISOFS_BUFFER_SIZE(inode);
47 unsigned int bufshift = ISOFS_BUFFER_BITS(inode);
48 unsigned int bufmask = bufsize - 1;
49 int i, block_size = block_end - block_start;
50 z_stream stream = { .total_out = 0,
51 .avail_in = 0,
52 .avail_out = 0, };
53 int zerr;
54 int needblocks = (block_size + (block_start & bufmask) + bufmask)
55 >> bufshift;
56 int haveblocks;
57 blkcnt_t blocknum;
58 struct buffer_head **bhs;
59 int curbh, curpage;
60
61 if (block_size > deflateBound(1UL << zisofs_block_shift)) {
62 *errp = -EIO;
63 return 0;
64 }
65 /* Empty block? */
66 if (block_size == 0) {
67 for ( i = 0 ; i < pcount ; i++ ) {
68 unsigned int off = i ? 0 : poffset;
69
70 if (!pages[i])
71 continue;
72 memzero_page(pages[i], off, PAGE_SIZE - off);
73 SetPageUptodate(pages[i]);
74 }
75 return (((loff_t)pcount) << PAGE_SHIFT) - poffset;
76 }
77
78 /* Because zlib is not thread-safe, do all the I/O at the top. */
79 blocknum = block_start >> bufshift;
80 bhs = kzalloc_objs(*bhs, needblocks + 1);
81 if (!bhs) {
82 *errp = -ENOMEM;
83 return 0;
84 }
85 haveblocks = isofs_get_blocks(inode, blocknum, bhs, needblocks);
86 bh_read_batch(haveblocks, bhs);
87
88 curbh = 0;
89 curpage = 0;
90 /*
91 * First block is special since it may be fractional. We also wait for
92 * it before grabbing the zlib mutex; odds are that the subsequent
93 * blocks are going to come in in short order so we don't hold the zlib
94 * mutex longer than necessary.
95 */
96
97 if (!bhs[0])
98 goto b_eio;
99
100 wait_on_buffer(bhs[0]);
101 if (!buffer_uptodate(bhs[0])) {
102 *errp = -EIO;
103 goto b_eio;
104 }
105
106 stream.workspace = zisofs_zlib_workspace;
107 mutex_lock(&zisofs_zlib_lock);
108
109 zerr = zlib_inflateInit(&stream);
110 if (zerr != Z_OK) {
111 if (zerr == Z_MEM_ERROR)
112 *errp = -ENOMEM;
113 else
114 *errp = -EIO;
115 printk(KERN_DEBUG "zisofs: zisofs_inflateInit returned %d\n",
116 zerr);
117 goto z_eio;
118 }
119
120 while (curpage < pcount && curbh < haveblocks &&
121 zerr != Z_STREAM_END) {
122 if (!stream.avail_out) {
123 if (pages[curpage]) {
124 stream.next_out = kmap_local_page(pages[curpage])
125 + poffset;
126 stream.avail_out = PAGE_SIZE - poffset;
127 poffset = 0;
128 } else {
129 stream.next_out = (void *)&zisofs_sink_page;
130 stream.avail_out = PAGE_SIZE;
131 }
132 }
133 if (!stream.avail_in) {
134 wait_on_buffer(bhs[curbh]);
135 if (!buffer_uptodate(bhs[curbh])) {
136 *errp = -EIO;
137 break;
138 }
139 stream.next_in = bhs[curbh]->b_data +
140 (block_start & bufmask);
141 stream.avail_in = min_t(unsigned, bufsize -
142 (block_start & bufmask),
143 block_size);
144 block_size -= stream.avail_in;
145 block_start = 0;
146 }
147
148 while (stream.avail_out && stream.avail_in) {
149 zerr = zlib_inflate(&stream, Z_SYNC_FLUSH);
150 if (zerr == Z_BUF_ERROR && stream.avail_in == 0)
151 break;
152 if (zerr == Z_STREAM_END)
153 break;
154 if (zerr != Z_OK) {
155 /* EOF, error, or trying to read beyond end of input */
156 if (zerr == Z_MEM_ERROR)
157 *errp = -ENOMEM;
158 else {
159 printk(KERN_DEBUG
160 "zisofs: zisofs_inflate returned"
161 " %d, inode = %llu,"
162 " page idx = %d, bh idx = %d,"
163 " avail_in = %ld,"
164 " avail_out = %ld\n",
165 zerr, inode->i_ino, curpage,
166 curbh, stream.avail_in,
167 stream.avail_out);
168 *errp = -EIO;
169 }
170 goto inflate_out;
171 }
172 }
173
174 if (!stream.avail_out) {
175 /* This page completed */
176 if (pages[curpage]) {
177 flush_dcache_page(pages[curpage]);
178 SetPageUptodate(pages[curpage]);
179 }
180 if (stream.next_out != (unsigned char *)zisofs_sink_page) {
181 kunmap_local(stream.next_out);
182 stream.next_out = NULL;
183 }
184 curpage++;
185 }
186 if (!stream.avail_in)
187 curbh++;
188 }
189 inflate_out:
190 zlib_inflateEnd(&stream);
191 if (stream.next_out && stream.next_out != (unsigned char *)zisofs_sink_page)
192 kunmap_local(stream.next_out);
193
194 z_eio:
195 mutex_unlock(&zisofs_zlib_lock);
196
197 b_eio:
198 for (i = 0; i < haveblocks; i++)
199 brelse(bhs[i]);
200 kfree(bhs);
201 return stream.total_out;
202 }
203
204 /*
205 * Uncompress data so that pages[full_page] is fully uptodate and possibly
206 * fills in other pages if we have data for them.
207 */
zisofs_fill_pages(struct inode * inode,int full_page,int pcount,struct page ** pages)208 static int zisofs_fill_pages(struct inode *inode, int full_page, int pcount,
209 struct page **pages)
210 {
211 loff_t start_off, end_off;
212 loff_t block_start, block_end;
213 unsigned int header_size = ISOFS_I(inode)->i_format_parm[0];
214 unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
215 unsigned int blockptr;
216 loff_t poffset = 0;
217 blkcnt_t cstart_block, cend_block;
218 struct buffer_head *bh;
219 unsigned int blkbits = ISOFS_BUFFER_BITS(inode);
220 unsigned int blksize = 1 << blkbits;
221 int err;
222 loff_t ret;
223
224 BUG_ON(!pages[full_page]);
225
226 /*
227 * We want to read at least 'full_page' page. Because we have to
228 * uncompress the whole compression block anyway, fill the surrounding
229 * pages with the data we have anyway...
230 */
231 start_off = page_offset(pages[full_page]);
232 end_off = min_t(loff_t, start_off + PAGE_SIZE, inode->i_size);
233
234 cstart_block = start_off >> zisofs_block_shift;
235 cend_block = (end_off + (1 << zisofs_block_shift) - 1)
236 >> zisofs_block_shift;
237
238 WARN_ON(start_off - (full_page << PAGE_SHIFT) !=
239 ((cstart_block << zisofs_block_shift) & PAGE_MASK));
240
241 /* Find the pointer to this specific chunk */
242 /* Note: we're not using isonum_731() here because the data is known aligned */
243 /* Note: header_size is in 32-bit words (4 bytes) */
244 blockptr = (header_size + cstart_block) << 2;
245 bh = isofs_bread(inode, blockptr >> blkbits);
246 if (!bh)
247 return -EIO;
248 block_start = le32_to_cpu(*(__le32 *)
249 (bh->b_data + (blockptr & (blksize - 1))));
250
251 while (cstart_block < cend_block && pcount > 0) {
252 /* Load end of the compressed block in the file */
253 blockptr += 4;
254 /* Traversed to next block? */
255 if (!(blockptr & (blksize - 1))) {
256 brelse(bh);
257
258 bh = isofs_bread(inode, blockptr >> blkbits);
259 if (!bh)
260 return -EIO;
261 }
262 block_end = le32_to_cpu(*(__le32 *)
263 (bh->b_data + (blockptr & (blksize - 1))));
264 if (block_start > block_end) {
265 brelse(bh);
266 return -EIO;
267 }
268 err = 0;
269 ret = zisofs_uncompress_block(inode, block_start, block_end,
270 pcount, pages, poffset, &err);
271 poffset += ret;
272 pages += poffset >> PAGE_SHIFT;
273 pcount -= poffset >> PAGE_SHIFT;
274 full_page -= poffset >> PAGE_SHIFT;
275 poffset &= ~PAGE_MASK;
276
277 if (err) {
278 brelse(bh);
279 /*
280 * Did we finish reading the page we really wanted
281 * to read?
282 */
283 if (full_page < 0)
284 return 0;
285 return err;
286 }
287
288 block_start = block_end;
289 cstart_block++;
290 }
291
292 if (poffset && *pages) {
293 memzero_page(*pages, poffset, PAGE_SIZE - poffset);
294 SetPageUptodate(*pages);
295 }
296 brelse(bh);
297 return 0;
298 }
299
300 /*
301 * When decompressing, we typically obtain more than one page
302 * per reference. We inject the additional pages into the page
303 * cache as a form of readahead.
304 */
zisofs_read_folio(struct file * file,struct folio * folio)305 static int zisofs_read_folio(struct file *file, struct folio *folio)
306 {
307 struct inode *inode = file_inode(file);
308 struct address_space *mapping = inode->i_mapping;
309 int err;
310 int i, pcount, full_page;
311 unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
312 unsigned int zisofs_pages_per_cblock =
313 PAGE_SHIFT <= zisofs_block_shift ?
314 (1 << (zisofs_block_shift - PAGE_SHIFT)) : 0;
315 struct page **pages;
316 pgoff_t index = folio->index, end_index;
317
318 end_index = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
319 /*
320 * If this folio is wholly outside i_size we just return zero;
321 * do_generic_file_read() will handle this for us
322 */
323 if (index >= end_index) {
324 folio_end_read(folio, true);
325 return 0;
326 }
327
328 if (PAGE_SHIFT <= zisofs_block_shift) {
329 /* We have already been given one page, this is the one
330 we must do. */
331 full_page = index & (zisofs_pages_per_cblock - 1);
332 pcount = min_t(int, zisofs_pages_per_cblock,
333 end_index - (index & ~(zisofs_pages_per_cblock - 1)));
334 index -= full_page;
335 } else {
336 full_page = 0;
337 pcount = 1;
338 }
339 pages = kzalloc_objs(*pages,
340 max_t(unsigned int, zisofs_pages_per_cblock, 1));
341 if (!pages) {
342 folio_unlock(folio);
343 return -ENOMEM;
344 }
345 pages[full_page] = &folio->page;
346
347 for (i = 0; i < pcount; i++, index++) {
348 if (i != full_page)
349 pages[i] = grab_cache_page_nowait(mapping, index);
350 }
351
352 err = zisofs_fill_pages(inode, full_page, pcount, pages);
353
354 /* Release any residual pages, do not SetPageUptodate */
355 for (i = 0; i < pcount; i++) {
356 if (pages[i]) {
357 flush_dcache_page(pages[i]);
358 unlock_page(pages[i]);
359 if (i != full_page)
360 put_page(pages[i]);
361 }
362 }
363
364 /* At this point, err contains 0 or -EIO depending on the "critical" page */
365 kfree(pages);
366 return err;
367 }
368
369 const struct address_space_operations zisofs_aops = {
370 .read_folio = zisofs_read_folio,
371 /* No bmap operation supported */
372 };
373
zisofs_init(void)374 int __init zisofs_init(void)
375 {
376 zisofs_zlib_workspace = vmalloc(zlib_inflate_workspacesize());
377 if ( !zisofs_zlib_workspace )
378 return -ENOMEM;
379
380 return 0;
381 }
382
zisofs_cleanup(void)383 void zisofs_cleanup(void)
384 {
385 vfree(zisofs_zlib_workspace);
386 }
387