1 /* -*- linux-c -*- ------------------------------------------------------- *
3 * Copyright 2001 H. Peter Anvin - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
8 * USA; either version 2 of the License, or (at your option) any later
9 * version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
14 * linux/fs/isofs/compress.c
16 * Transparent decompression of files on an iso9660 filesystem
19 #include <linux/module.h>
20 #include <linux/init.h>
22 #include <linux/vmalloc.h>
23 #include <linux/zlib.h>
28 /* This should probably be global. */
29 static char zisofs_sink_page[PAGE_CACHE_SIZE];
32 * This contains the zlib memory allocation and the mutex for the
33 * allocation; this avoids failures at block-decompression time.
35 static void *zisofs_zlib_workspace;
36 static DEFINE_MUTEX(zisofs_zlib_lock);
39 * Read data of @inode from @block_start to @block_end and uncompress
40 * to one zisofs block. Store the data in the @pages array with @pcount
41 * entries. Start storing at offset @poffset of the first page.
43 static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start,
44 loff_t block_end, int pcount,
45 struct page **pages, unsigned poffset,
48 unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
49 unsigned int bufsize = ISOFS_BUFFER_SIZE(inode);
50 unsigned int bufshift = ISOFS_BUFFER_BITS(inode);
51 unsigned int bufmask = bufsize - 1;
52 int i, block_size = block_end - block_start;
53 z_stream stream = { .total_out = 0,
57 int needblocks = (block_size + (block_start & bufmask) + bufmask)
61 struct buffer_head *bhs[needblocks + 1];
64 if (block_size > deflateBound(1UL << zisofs_block_shift)) {
69 if (block_size == 0) {
70 for ( i = 0 ; i < pcount ; i++ ) {
73 memset(page_address(pages[i]), 0, PAGE_CACHE_SIZE);
74 flush_dcache_page(pages[i]);
75 SetPageUptodate(pages[i]);
77 return ((loff_t)pcount) << PAGE_CACHE_SHIFT;
80 /* Because zlib is not thread-safe, do all the I/O at the top. */
81 blocknum = block_start >> bufshift;
82 memset(bhs, 0, (needblocks + 1) * sizeof(struct buffer_head *));
83 haveblocks = isofs_get_blocks(inode, blocknum, bhs, needblocks);
84 ll_rw_block(READ, haveblocks, bhs);
89 * First block is special since it may be fractional. We also wait for
90 * it before grabbing the zlib mutex; odds are that the subsequent
91 * blocks are going to come in in short order so we don't hold the zlib
92 * mutex longer than necessary.
98 wait_on_buffer(bhs[0]);
99 if (!buffer_uptodate(bhs[0])) {
104 stream.workspace = zisofs_zlib_workspace;
105 mutex_lock(&zisofs_zlib_lock);
107 zerr = zlib_inflateInit(&stream);
109 if (zerr == Z_MEM_ERROR)
113 printk(KERN_DEBUG "zisofs: zisofs_inflateInit returned %d\n",
118 while (curpage < pcount && curbh < haveblocks &&
119 zerr != Z_STREAM_END) {
120 if (!stream.avail_out) {
121 if (pages[curpage]) {
122 stream.next_out = page_address(pages[curpage])
124 stream.avail_out = PAGE_CACHE_SIZE - poffset;
127 stream.next_out = (void *)&zisofs_sink_page;
128 stream.avail_out = PAGE_CACHE_SIZE;
131 if (!stream.avail_in) {
132 wait_on_buffer(bhs[curbh]);
133 if (!buffer_uptodate(bhs[curbh])) {
137 stream.next_in = bhs[curbh]->b_data +
138 (block_start & bufmask);
139 stream.avail_in = min_t(unsigned, bufsize -
140 (block_start & bufmask),
142 block_size -= stream.avail_in;
146 while (stream.avail_out && stream.avail_in) {
147 zerr = zlib_inflate(&stream, Z_SYNC_FLUSH);
148 if (zerr == Z_BUF_ERROR && stream.avail_in == 0)
150 if (zerr == Z_STREAM_END)
153 /* EOF, error, or trying to read beyond end of input */
154 if (zerr == Z_MEM_ERROR)
158 "zisofs: zisofs_inflate returned"
160 " page idx = %d, bh idx = %d,"
163 zerr, inode->i_ino, curpage,
164 curbh, stream.avail_in,
172 if (!stream.avail_out) {
173 /* This page completed */
174 if (pages[curpage]) {
175 flush_dcache_page(pages[curpage]);
176 SetPageUptodate(pages[curpage]);
180 if (!stream.avail_in)
184 zlib_inflateEnd(&stream);
187 mutex_unlock(&zisofs_zlib_lock);
190 for (i = 0; i < haveblocks; i++)
192 return stream.total_out;
196 * Uncompress data so that pages[full_page] is fully uptodate and possibly
197 * fills in other pages if we have data for them.
199 static int zisofs_fill_pages(struct inode *inode, int full_page, int pcount,
202 loff_t start_off, end_off;
203 loff_t block_start, block_end;
204 unsigned int header_size = ISOFS_I(inode)->i_format_parm[0];
205 unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
206 unsigned int blockptr;
208 blkcnt_t cstart_block, cend_block;
209 struct buffer_head *bh;
210 unsigned int blkbits = ISOFS_BUFFER_BITS(inode);
211 unsigned int blksize = 1 << blkbits;
215 BUG_ON(!pages[full_page]);
218 * We want to read at least 'full_page' page. Because we have to
219 * uncompress the whole compression block anyway, fill the surrounding
220 * pages with the data we have anyway...
222 start_off = page_offset(pages[full_page]);
223 end_off = min_t(loff_t, start_off + PAGE_CACHE_SIZE, inode->i_size);
225 cstart_block = start_off >> zisofs_block_shift;
226 cend_block = (end_off + (1 << zisofs_block_shift) - 1)
227 >> zisofs_block_shift;
229 WARN_ON(start_off - (full_page << PAGE_CACHE_SHIFT) !=
230 ((cstart_block << zisofs_block_shift) & PAGE_CACHE_MASK));
232 /* Find the pointer to this specific chunk */
233 /* Note: we're not using isonum_731() here because the data is known aligned */
234 /* Note: header_size is in 32-bit words (4 bytes) */
235 blockptr = (header_size + cstart_block) << 2;
236 bh = isofs_bread(inode, blockptr >> blkbits);
239 block_start = le32_to_cpu(*(__le32 *)
240 (bh->b_data + (blockptr & (blksize - 1))));
242 while (cstart_block < cend_block && pcount > 0) {
243 /* Load end of the compressed block in the file */
245 /* Traversed to next block? */
246 if (!(blockptr & (blksize - 1))) {
249 bh = isofs_bread(inode, blockptr >> blkbits);
253 block_end = le32_to_cpu(*(__le32 *)
254 (bh->b_data + (blockptr & (blksize - 1))));
255 if (block_start > block_end) {
260 ret = zisofs_uncompress_block(inode, block_start, block_end,
261 pcount, pages, poffset, &err);
263 pages += poffset >> PAGE_CACHE_SHIFT;
264 pcount -= poffset >> PAGE_CACHE_SHIFT;
265 full_page -= poffset >> PAGE_CACHE_SHIFT;
266 poffset &= ~PAGE_CACHE_MASK;
271 * Did we finish reading the page we really wanted
279 block_start = block_end;
283 if (poffset && *pages) {
284 memset(page_address(*pages) + poffset, 0,
285 PAGE_CACHE_SIZE - poffset);
286 flush_dcache_page(*pages);
287 SetPageUptodate(*pages);
293 * When decompressing, we typically obtain more than one page
294 * per reference. We inject the additional pages into the page
295 * cache as a form of readahead.
297 static int zisofs_readpage(struct file *file, struct page *page)
299 struct inode *inode = file_inode(file);
300 struct address_space *mapping = inode->i_mapping;
302 int i, pcount, full_page;
303 unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
304 unsigned int zisofs_pages_per_cblock =
305 PAGE_CACHE_SHIFT <= zisofs_block_shift ?
306 (1 << (zisofs_block_shift - PAGE_CACHE_SHIFT)) : 0;
307 struct page *pages[max_t(unsigned, zisofs_pages_per_cblock, 1)];
308 pgoff_t index = page->index, end_index;
310 end_index = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
312 * If this page is wholly outside i_size we just return zero;
313 * do_generic_file_read() will handle this for us
315 if (index >= end_index) {
316 SetPageUptodate(page);
321 if (PAGE_CACHE_SHIFT <= zisofs_block_shift) {
322 /* We have already been given one page, this is the one
324 full_page = index & (zisofs_pages_per_cblock - 1);
325 pcount = min_t(int, zisofs_pages_per_cblock,
326 end_index - (index & ~(zisofs_pages_per_cblock - 1)));
332 pages[full_page] = page;
334 for (i = 0; i < pcount; i++, index++) {
336 pages[i] = grab_cache_page_nowait(mapping, index);
338 ClearPageError(pages[i]);
343 err = zisofs_fill_pages(inode, full_page, pcount, pages);
345 /* Release any residual pages, do not SetPageUptodate */
346 for (i = 0; i < pcount; i++) {
348 flush_dcache_page(pages[i]);
349 if (i == full_page && err)
350 SetPageError(pages[i]);
352 unlock_page(pages[i]);
354 page_cache_release(pages[i]);
358 /* At this point, err contains 0 or -EIO depending on the "critical" page */
362 const struct address_space_operations zisofs_aops = {
363 .readpage = zisofs_readpage,
364 /* No sync_page operation supported? */
365 /* No bmap operation supported */
368 int __init zisofs_init(void)
370 zisofs_zlib_workspace = vmalloc(zlib_inflate_workspacesize());
371 if ( !zisofs_zlib_workspace )
377 void zisofs_cleanup(void)
379 vfree(zisofs_zlib_workspace);