compress.c 10.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* -*- linux-c -*- ------------------------------------------------------- *
 *   
 *   Copyright 2001 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
 *   USA; either version 2 of the License, or (at your option) any later
 *   version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * linux/fs/isofs/compress.c
 *
 * Transparent decompression of files on an iso9660 filesystem
 */

#include <linux/module.h>
#include <linux/init.h>
21
#include <linux/bio.h>
A
Al Viro 已提交
22

K
Kyle Spiers 已提交
23
#include <linux/slab.h>
L
Linus Torvalds 已提交
24 25 26
#include <linux/vmalloc.h>
#include <linux/zlib.h>

A
Al Viro 已提交
27
#include "isofs.h"
L
Linus Torvalds 已提交
28 29 30
#include "zisofs.h"

/* This should probably be global. */
31
static char zisofs_sink_page[PAGE_SIZE];
L
Linus Torvalds 已提交
32 33 34 35 36 37

/*
 * This contains the zlib memory allocation and the mutex for the
 * allocation; this avoids failures at block-decompression time.
 */
static void *zisofs_zlib_workspace;
D
Dave Young 已提交
38
static DEFINE_MUTEX(zisofs_zlib_lock);
L
Linus Torvalds 已提交
39 40

/*
41 42 43
 * Read data of @inode from @block_start to @block_end and uncompress
 * to one zisofs block. Store the data in the @pages array with @pcount
 * entries. Start storing at offset @poffset of the first page.
L
Linus Torvalds 已提交
44
 */
45 46 47 48
static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start,
				      loff_t block_end, int pcount,
				      struct page **pages, unsigned poffset,
				      int *errp)
L
Linus Torvalds 已提交
49 50
{
	unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
51 52 53 54 55 56 57 58 59 60 61 62
	unsigned int bufsize = ISOFS_BUFFER_SIZE(inode);
	unsigned int bufshift = ISOFS_BUFFER_BITS(inode);
	unsigned int bufmask = bufsize - 1;
	int i, block_size = block_end - block_start;
	z_stream stream = { .total_out = 0,
			    .avail_in = 0,
			    .avail_out = 0, };
	int zerr;
	int needblocks = (block_size + (block_start & bufmask) + bufmask)
				>> bufshift;
	int haveblocks;
	blkcnt_t blocknum;
K
Kyle Spiers 已提交
63
	struct buffer_head **bhs;
64 65 66 67
	int curbh, curpage;

	if (block_size > deflateBound(1UL << zisofs_block_shift)) {
		*errp = -EIO;
68 69
		return 0;
	}
70 71 72 73 74
	/* Empty block? */
	if (block_size == 0) {
		for ( i = 0 ; i < pcount ; i++ ) {
			if (!pages[i])
				continue;
75
			memset(page_address(pages[i]), 0, PAGE_SIZE);
76 77
			flush_dcache_page(pages[i]);
			SetPageUptodate(pages[i]);
L
Linus Torvalds 已提交
78
		}
79
		return ((loff_t)pcount) << PAGE_SHIFT;
L
Linus Torvalds 已提交
80 81
	}

82 83
	/* Because zlib is not thread-safe, do all the I/O at the top. */
	blocknum = block_start >> bufshift;
K
Kyle Spiers 已提交
84 85 86 87 88
	bhs = kcalloc(needblocks + 1, sizeof(*bhs), GFP_KERNEL);
	if (!bhs) {
		*errp = -ENOMEM;
		return 0;
	}
89
	haveblocks = isofs_get_blocks(inode, blocknum, bhs, needblocks);
90
	ll_rw_block(REQ_OP_READ, 0, haveblocks, bhs);
L
Linus Torvalds 已提交
91

92 93 94 95 96 97 98 99
	curbh = 0;
	curpage = 0;
	/*
	 * First block is special since it may be fractional.  We also wait for
	 * it before grabbing the zlib mutex; odds are that the subsequent
	 * blocks are going to come in in short order so we don't hold the zlib
	 * mutex longer than necessary.
	 */
L
Linus Torvalds 已提交
100

101 102
	if (!bhs[0])
		goto b_eio;
L
Linus Torvalds 已提交
103

104 105 106 107
	wait_on_buffer(bhs[0]);
	if (!buffer_uptodate(bhs[0])) {
		*errp = -EIO;
		goto b_eio;
L
Linus Torvalds 已提交
108 109
	}

110 111
	stream.workspace = zisofs_zlib_workspace;
	mutex_lock(&zisofs_zlib_lock);
112
		
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	zerr = zlib_inflateInit(&stream);
	if (zerr != Z_OK) {
		if (zerr == Z_MEM_ERROR)
			*errp = -ENOMEM;
		else
			*errp = -EIO;
		printk(KERN_DEBUG "zisofs: zisofs_inflateInit returned %d\n",
			       zerr);
		goto z_eio;
	}

	while (curpage < pcount && curbh < haveblocks &&
	       zerr != Z_STREAM_END) {
		if (!stream.avail_out) {
			if (pages[curpage]) {
				stream.next_out = page_address(pages[curpage])
						+ poffset;
130
				stream.avail_out = PAGE_SIZE - poffset;
131 132 133
				poffset = 0;
			} else {
				stream.next_out = (void *)&zisofs_sink_page;
134
				stream.avail_out = PAGE_SIZE;
L
Linus Torvalds 已提交
135 136
			}
		}
137 138 139 140 141 142 143 144 145 146 147 148 149
		if (!stream.avail_in) {
			wait_on_buffer(bhs[curbh]);
			if (!buffer_uptodate(bhs[curbh])) {
				*errp = -EIO;
				break;
			}
			stream.next_in  = bhs[curbh]->b_data +
						(block_start & bufmask);
			stream.avail_in = min_t(unsigned, bufsize -
						(block_start & bufmask),
						block_size);
			block_size -= stream.avail_in;
			block_start = 0;
L
Linus Torvalds 已提交
150 151
		}

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
		while (stream.avail_out && stream.avail_in) {
			zerr = zlib_inflate(&stream, Z_SYNC_FLUSH);
			if (zerr == Z_BUF_ERROR && stream.avail_in == 0)
				break;
			if (zerr == Z_STREAM_END)
				break;
			if (zerr != Z_OK) {
				/* EOF, error, or trying to read beyond end of input */
				if (zerr == Z_MEM_ERROR)
					*errp = -ENOMEM;
				else {
					printk(KERN_DEBUG
					       "zisofs: zisofs_inflate returned"
					       " %d, inode = %lu,"
					       " page idx = %d, bh idx = %d,"
167 168
					       " avail_in = %ld,"
					       " avail_out = %ld\n",
169 170 171 172
					       zerr, inode->i_ino, curpage,
					       curbh, stream.avail_in,
					       stream.avail_out);
					*errp = -EIO;
L
Linus Torvalds 已提交
173
				}
174
				goto inflate_out;
L
Linus Torvalds 已提交
175
			}
176
		}
L
Linus Torvalds 已提交
177

178 179 180 181 182
		if (!stream.avail_out) {
			/* This page completed */
			if (pages[curpage]) {
				flush_dcache_page(pages[curpage]);
				SetPageUptodate(pages[curpage]);
L
Linus Torvalds 已提交
183
			}
184 185 186 187 188 189 190
			curpage++;
		}
		if (!stream.avail_in)
			curbh++;
	}
inflate_out:
	zlib_inflateEnd(&stream);
L
Linus Torvalds 已提交
191

192 193 194 195 196 197
z_eio:
	mutex_unlock(&zisofs_zlib_lock);

b_eio:
	for (i = 0; i < haveblocks; i++)
		brelse(bhs[i]);
K
Kyle Spiers 已提交
198
	kfree(bhs);
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
	return stream.total_out;
}

/*
 * Uncompress data so that pages[full_page] is fully uptodate and possibly
 * fills in other pages if we have data for them.
 */
static int zisofs_fill_pages(struct inode *inode, int full_page, int pcount,
			     struct page **pages)
{
	loff_t start_off, end_off;
	loff_t block_start, block_end;
	unsigned int header_size = ISOFS_I(inode)->i_format_parm[0];
	unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
	unsigned int blockptr;
	loff_t poffset = 0;
	blkcnt_t cstart_block, cend_block;
	struct buffer_head *bh;
	unsigned int blkbits = ISOFS_BUFFER_BITS(inode);
	unsigned int blksize = 1 << blkbits;
	int err;
	loff_t ret;

	BUG_ON(!pages[full_page]);

	/*
	 * We want to read at least 'full_page' page. Because we have to
	 * uncompress the whole compression block anyway, fill the surrounding
	 * pages with the data we have anyway...
	 */
	start_off = page_offset(pages[full_page]);
230
	end_off = min_t(loff_t, start_off + PAGE_SIZE, inode->i_size);
231 232 233 234 235

	cstart_block = start_off >> zisofs_block_shift;
	cend_block = (end_off + (1 << zisofs_block_shift) - 1)
			>> zisofs_block_shift;

236 237
	WARN_ON(start_off - (full_page << PAGE_SHIFT) !=
		((cstart_block << zisofs_block_shift) & PAGE_MASK));
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

	/* Find the pointer to this specific chunk */
	/* Note: we're not using isonum_731() here because the data is known aligned */
	/* Note: header_size is in 32-bit words (4 bytes) */
	blockptr = (header_size + cstart_block) << 2;
	bh = isofs_bread(inode, blockptr >> blkbits);
	if (!bh)
		return -EIO;
	block_start = le32_to_cpu(*(__le32 *)
				(bh->b_data + (blockptr & (blksize - 1))));

	while (cstart_block < cend_block && pcount > 0) {
		/* Load end of the compressed block in the file */
		blockptr += 4;
		/* Traversed to next block? */
		if (!(blockptr & (blksize - 1))) {
			brelse(bh);

			bh = isofs_bread(inode, blockptr >> blkbits);
			if (!bh)
				return -EIO;
		}
		block_end = le32_to_cpu(*(__le32 *)
				(bh->b_data + (blockptr & (blksize - 1))));
		if (block_start > block_end) {
			brelse(bh);
			return -EIO;
		}
		err = 0;
		ret = zisofs_uncompress_block(inode, block_start, block_end,
					      pcount, pages, poffset, &err);
		poffset += ret;
270 271 272 273
		pages += poffset >> PAGE_SHIFT;
		pcount -= poffset >> PAGE_SHIFT;
		full_page -= poffset >> PAGE_SHIFT;
		poffset &= ~PAGE_MASK;
274 275 276 277 278 279 280 281 282 283

		if (err) {
			brelse(bh);
			/*
			 * Did we finish reading the page we really wanted
			 * to read?
			 */
			if (full_page < 0)
				return 0;
			return err;
L
Linus Torvalds 已提交
284 285
		}

286 287 288 289 290 291
		block_start = block_end;
		cstart_block++;
	}

	if (poffset && *pages) {
		memset(page_address(*pages) + poffset, 0,
292
		       PAGE_SIZE - poffset);
293 294 295 296 297
		flush_dcache_page(*pages);
		SetPageUptodate(*pages);
	}
	return 0;
}
L
Linus Torvalds 已提交
298

299 300 301 302 303 304 305
/*
 * When decompressing, we typically obtain more than one page
 * per reference.  We inject the additional pages into the page
 * cache as a form of readahead.
 */
static int zisofs_readpage(struct file *file, struct page *page)
{
A
Al Viro 已提交
306
	struct inode *inode = file_inode(file);
307 308 309 310 311
	struct address_space *mapping = inode->i_mapping;
	int err;
	int i, pcount, full_page;
	unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
	unsigned int zisofs_pages_per_cblock =
312 313
		PAGE_SHIFT <= zisofs_block_shift ?
		(1 << (zisofs_block_shift - PAGE_SHIFT)) : 0;
K
Kyle Spiers 已提交
314
	struct page **pages;
315 316
	pgoff_t index = page->index, end_index;

317
	end_index = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
318 319 320 321 322 323 324 325 326 327
	/*
	 * If this page is wholly outside i_size we just return zero;
	 * do_generic_file_read() will handle this for us
	 */
	if (index >= end_index) {
		SetPageUptodate(page);
		unlock_page(page);
		return 0;
	}

328
	if (PAGE_SHIFT <= zisofs_block_shift) {
329 330 331 332 333 334 335 336 337 338
		/* We have already been given one page, this is the one
		   we must do. */
		full_page = index & (zisofs_pages_per_cblock - 1);
		pcount = min_t(int, zisofs_pages_per_cblock,
			end_index - (index & ~(zisofs_pages_per_cblock - 1)));
		index -= full_page;
	} else {
		full_page = 0;
		pcount = 1;
	}
K
Kyle Spiers 已提交
339 340 341 342 343 344
	pages = kcalloc(max_t(unsigned int, zisofs_pages_per_cblock, 1),
					sizeof(*pages), GFP_KERNEL);
	if (!pages) {
		unlock_page(page);
		return -ENOMEM;
	}
345 346 347 348 349 350 351 352
	pages[full_page] = page;

	for (i = 0; i < pcount; i++, index++) {
		if (i != full_page)
			pages[i] = grab_cache_page_nowait(mapping, index);
		if (pages[i]) {
			ClearPageError(pages[i]);
			kmap(pages[i]);
L
Linus Torvalds 已提交
353 354 355
		}
	}

356
	err = zisofs_fill_pages(inode, full_page, pcount, pages);
L
Linus Torvalds 已提交
357 358

	/* Release any residual pages, do not SetPageUptodate */
359 360 361 362 363 364 365 366
	for (i = 0; i < pcount; i++) {
		if (pages[i]) {
			flush_dcache_page(pages[i]);
			if (i == full_page && err)
				SetPageError(pages[i]);
			kunmap(pages[i]);
			unlock_page(pages[i]);
			if (i != full_page)
367
				put_page(pages[i]);
L
Linus Torvalds 已提交
368 369 370 371
		}
	}			

	/* At this point, err contains 0 or -EIO depending on the "critical" page */
K
Kyle Spiers 已提交
372
	kfree(pages);
L
Linus Torvalds 已提交
373 374 375
	return err;
}

376
const struct address_space_operations zisofs_aops = {
L
Linus Torvalds 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
	.readpage = zisofs_readpage,
	/* No bmap operation supported */
};

int __init zisofs_init(void)
{
	zisofs_zlib_workspace = vmalloc(zlib_inflate_workspacesize());
	if ( !zisofs_zlib_workspace )
		return -ENOMEM;

	return 0;
}

void zisofs_cleanup(void)
{
	vfree(zisofs_zlib_workspace);
}