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