Blame view

fs/isofs/compress.c 10.2 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /* -*- 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
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
  #include <linux/module.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
  #include <linux/init.h>
94f2f7157   Al Viro   [PATCH] isofs inc...
20

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
  #include <linux/vmalloc.h>
  #include <linux/zlib.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23

94f2f7157   Al Viro   [PATCH] isofs inc...
24
  #include "isofs.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25
26
27
28
29
30
31
32
33
34
  #include "zisofs.h"
  
  /* This should probably be global. */
  static char zisofs_sink_page[PAGE_CACHE_SIZE];
  
  /*
   * This contains the zlib memory allocation and the mutex for the
   * allocation; this avoids failures at block-decompression time.
   */
  static void *zisofs_zlib_workspace;
a36a151e7   Dave Young   zisofs use mutex ...
35
  static DEFINE_MUTEX(zisofs_zlib_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
37
  
  /*
59bc05521   Jan Kara   zisofs: Implement...
38
39
40
   * 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.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
   */
59bc05521   Jan Kara   zisofs: Implement...
42
43
44
45
  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)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
  	unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
59bc05521   Jan Kara   zisofs: Implement...
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  	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;
  	struct buffer_head *bhs[needblocks + 1];
  	int curbh, curpage;
  
  	if (block_size > deflateBound(1UL << zisofs_block_shift)) {
  		*errp = -EIO;
08ca0db8a   Dave Young   zisofs: fix readp...
65
66
  		return 0;
  	}
59bc05521   Jan Kara   zisofs: Implement...
67
68
69
70
71
72
73
74
  	/* Empty block? */
  	if (block_size == 0) {
  		for ( i = 0 ; i < pcount ; i++ ) {
  			if (!pages[i])
  				continue;
  			memset(page_address(pages[i]), 0, PAGE_CACHE_SIZE);
  			flush_dcache_page(pages[i]);
  			SetPageUptodate(pages[i]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75
  		}
59bc05521   Jan Kara   zisofs: Implement...
76
  		return ((loff_t)pcount) << PAGE_CACHE_SHIFT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
  	}
59bc05521   Jan Kara   zisofs: Implement...
78
79
80
81
82
  	/* Because zlib is not thread-safe, do all the I/O at the top. */
  	blocknum = block_start >> bufshift;
  	memset(bhs, 0, (needblocks + 1) * sizeof(struct buffer_head *));
  	haveblocks = isofs_get_blocks(inode, blocknum, bhs, needblocks);
  	ll_rw_block(READ, haveblocks, bhs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
83

59bc05521   Jan Kara   zisofs: Implement...
84
85
86
87
88
89
90
91
  	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.
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92

59bc05521   Jan Kara   zisofs: Implement...
93
94
  	if (!bhs[0])
  		goto b_eio;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
95

59bc05521   Jan Kara   zisofs: Implement...
96
97
98
99
  	wait_on_buffer(bhs[0]);
  	if (!buffer_uptodate(bhs[0])) {
  		*errp = -EIO;
  		goto b_eio;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101

59bc05521   Jan Kara   zisofs: Implement...
102
103
  	stream.workspace = zisofs_zlib_workspace;
  	mutex_lock(&zisofs_zlib_lock);
fab5a60a2   Linus Torvalds   Check input buffe...
104
  		
59bc05521   Jan Kara   zisofs: Implement...
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  	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
  ",
  			       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;
  				stream.avail_out = PAGE_CACHE_SIZE - poffset;
  				poffset = 0;
  			} else {
  				stream.next_out = (void *)&zisofs_sink_page;
  				stream.avail_out = PAGE_CACHE_SIZE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128
129
  			}
  		}
59bc05521   Jan Kara   zisofs: Implement...
130
131
132
133
134
135
136
137
138
139
140
141
142
  		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;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
143
  		}
59bc05521   Jan Kara   zisofs: Implement...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  		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,"
  					       " avail_in = %d,"
  					       " avail_out = %d
  ",
  					       zerr, inode->i_ino, curpage,
  					       curbh, stream.avail_in,
  					       stream.avail_out);
  					*errp = -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166
  				}
59bc05521   Jan Kara   zisofs: Implement...
167
  				goto inflate_out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168
  			}
59bc05521   Jan Kara   zisofs: Implement...
169
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
170

59bc05521   Jan Kara   zisofs: Implement...
171
172
173
174
175
  		if (!stream.avail_out) {
  			/* This page completed */
  			if (pages[curpage]) {
  				flush_dcache_page(pages[curpage]);
  				SetPageUptodate(pages[curpage]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
176
  			}
59bc05521   Jan Kara   zisofs: Implement...
177
178
179
180
181
182
183
  			curpage++;
  		}
  		if (!stream.avail_in)
  			curbh++;
  	}
  inflate_out:
  	zlib_inflateEnd(&stream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184

59bc05521   Jan Kara   zisofs: Implement...
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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
230
231
232
233
234
235
236
237
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
270
271
272
273
274
275
  z_eio:
  	mutex_unlock(&zisofs_zlib_lock);
  
  b_eio:
  	for (i = 0; i < haveblocks; i++)
  		brelse(bhs[i]);
  	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]);
  	end_off = min_t(loff_t, start_off + PAGE_CACHE_SIZE, inode->i_size);
  
  	cstart_block = start_off >> zisofs_block_shift;
  	cend_block = (end_off + (1 << zisofs_block_shift) - 1)
  			>> zisofs_block_shift;
  
  	WARN_ON(start_off - (full_page << PAGE_CACHE_SHIFT) !=
  		((cstart_block << zisofs_block_shift) & PAGE_CACHE_MASK));
  
  	/* 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;
  		pages += poffset >> PAGE_CACHE_SHIFT;
  		pcount -= poffset >> PAGE_CACHE_SHIFT;
  		full_page -= poffset >> PAGE_CACHE_SHIFT;
  		poffset &= ~PAGE_CACHE_MASK;
  
  		if (err) {
  			brelse(bh);
  			/*
  			 * Did we finish reading the page we really wanted
  			 * to read?
  			 */
  			if (full_page < 0)
  				return 0;
  			return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
276
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
277

59bc05521   Jan Kara   zisofs: Implement...
278
279
280
281
282
283
284
285
286
287
288
289
  		block_start = block_end;
  		cstart_block++;
  	}
  
  	if (poffset && *pages) {
  		memset(page_address(*pages) + poffset, 0,
  		       PAGE_CACHE_SIZE - poffset);
  		flush_dcache_page(*pages);
  		SetPageUptodate(*pages);
  	}
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
290

59bc05521   Jan Kara   zisofs: Implement...
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
  /*
   * 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)
  {
  	struct inode *inode = file->f_path.dentry->d_inode;
  	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 =
  		PAGE_CACHE_SHIFT <= zisofs_block_shift ?
  		(1 << (zisofs_block_shift - PAGE_CACHE_SHIFT)) : 0;
  	struct page *pages[max_t(unsigned, zisofs_pages_per_cblock, 1)];
  	pgoff_t index = page->index, end_index;
  
  	end_index = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  	/*
  	 * 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;
  	}
  
  	if (PAGE_CACHE_SHIFT <= zisofs_block_shift) {
  		/* 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;
  	}
  	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]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
339
340
  		}
  	}
59bc05521   Jan Kara   zisofs: Implement...
341
  	err = zisofs_fill_pages(inode, full_page, pcount, pages);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
342
343
  
  	/* Release any residual pages, do not SetPageUptodate */
59bc05521   Jan Kara   zisofs: Implement...
344
345
346
347
348
349
350
351
352
  	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)
  				page_cache_release(pages[i]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
353
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
354
355
356
357
358
  	}			
  
  	/* At this point, err contains 0 or -EIO depending on the "critical" page */
  	return err;
  }
f5e54d6e5   Christoph Hellwig   [PATCH] mark addr...
359
  const struct address_space_operations zisofs_aops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
360
361
362
363
  	.readpage = zisofs_readpage,
  	/* No sync_page operation supported? */
  	/* No bmap operation supported */
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
365
  int __init zisofs_init(void)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
366
367
368
  	zisofs_zlib_workspace = vmalloc(zlib_inflate_workspacesize());
  	if ( !zisofs_zlib_workspace )
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
369

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
370
371
372
373
374
  	return 0;
  }
  
  void zisofs_cleanup(void)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
375
  	vfree(zisofs_zlib_workspace);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
376
  }