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