| Start/ | End/ | |||
| True | False | - | Line | Source |
| 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 | ||||
| 19 | #include <linux/config.h> | |||
| 20 | #include <linux/module.h> | |||
| 21 | #include <linux/init.h> | |||
| 22 | ||||
| 23 | #include <linux/vmalloc.h> | |||
| 24 | #include <linux/zlib.h> | |||
| 25 | ||||
| 26 | #include "isofs.h" | |||
| 27 | #include "zisofs.h" | |||
| 28 | ||||
| 29 | /* This should probably be global. */ | |||
| 30 | static char zisofs_sink_page[PAGE_CACHE_SIZE]; | |||
| 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; | |||
| 37 | static struct semaphore zisofs_zlib_semaphore; | |||
| 38 | ||||
| 39 | /* | |||
| 40 | * When decompressing, we typically obtain more than one page | |||
| 41 | * per reference. We inject the additional pages into the page | |||
| 42 | * cache as a form of readahead. | |||
| 43 | */ | |||
| 0 | 0 | - | 44 | static int zisofs_readpage(struct file *file, struct page *page) |
| 45 | { | |||
| 46 | struct inode *inode = file->f_dentry->d_inode; | |||
| 47 | struct address_space *mapping = inode->i_mapping; | |||
| 48 | unsigned int maxpage, xpage, fpage, blockindex; | |||
| 49 | unsigned long offset; | |||
| 50 | unsigned long blockptr, blockendptr, cstart, cend, csize; | |||
| 51 | struct buffer_head *bh, *ptrbh[2]; | |||
| 52 | unsigned long bufsize = ISOFS_BUFFER_SIZE(inode); | |||
| 53 | unsigned int bufshift = ISOFS_BUFFER_BITS(inode); | |||
| 54 | unsigned long bufmask = bufsize - 1; | |||
| 55 | int err = -EIO; | |||
| 56 | int i; | |||
| 57 | unsigned int header_size = ISOFS_I(inode)->i_format_parm[0]; | |||
| 58 | unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1]; | |||
| 59 | /* unsigned long zisofs_block_size = 1UL << zisofs_block_shift; */ | |||
| 60 | unsigned int zisofs_block_page_shift = zisofs_block_shift-PAGE_CACHE_SHIFT; | |||
| 61 | unsigned long zisofs_block_pages = 1UL << zisofs_block_page_shift; | |||
| 62 | unsigned long zisofs_block_page_mask = zisofs_block_pages-1; | |||
| 63 | struct page *pages[zisofs_block_pages]; | |||
| 64 | unsigned long index = page->index; | |||
| 65 | int indexblocks; | |||
| 66 | ||||
| 67 | /* We have already been given one page, this is the one | |||
| 68 | we must do. */ | |||
| 69 | xpage = index & zisofs_block_page_mask; | |||
| 70 | pages[xpage] = page; | |||
| 71 | ||||
| 72 | /* The remaining pages need to be allocated and inserted */ | |||
| 73 | offset = index & ~zisofs_block_page_mask; | |||
| 74 | blockindex = offset >> zisofs_block_page_shift; | |||
| 75 | maxpage = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | |||
| 76 | maxpage = min(zisofs_block_pages, maxpage-offset); | |||
| 77 | ||||
| 0 | 0 | - | 78 | for ( i = 0 ; i < maxpage ; i++, offset++ ) { |
| 0 | 0 | - | 79 | if ( i != xpage ) { |
| 80 | pages[i] = grab_cache_page_nowait(mapping, offset); | |||
| 81 | } | |||
| 82 | page = pages[i]; | |||
| 0 | 0 | - | 83 | if ( page ) { |
| 84 | ClearPageError(page); | |||
| 85 | kmap(page); | |||
| 86 | } | |||
| 87 | } | |||
| 88 | ||||
| 89 | /* This is the last page filled, plus one; used in case of abort. */ | |||
| 90 | fpage = 0; | |||
| 91 | ||||
| 92 | /* Find the pointer to this specific chunk */ | |||
| 93 | /* Note: we're not using isonum_731() here because the data is known aligned */ | |||
| 94 | /* Note: header_size is in 32-bit words (4 bytes) */ | |||
| 95 | blockptr = (header_size + blockindex) << 2; | |||
| 96 | blockendptr = blockptr + 4; | |||
| 97 | ||||
| 98 | indexblocks = ((blockptr^blockendptr) >> bufshift) ? 2 : 1; | |||
| 0 | 0 | - | 98 | ternary-?: ( ( blockptr ^ blockendptr ) >> buf.. |
| 99 | ptrbh[0] = ptrbh[1] = NULL; | |||
| 100 | ||||
| 0 | 0 | - | 101 | if ( isofs_get_blocks(inode, blockptr >> bufshift, ptrbh, indexblocks) != indexblocks ) { |
| 0 | 0 | - | 102 | if ( ptrbh[0] ) brelse(ptrbh[0]); |
| 103 | printk(KERN_DEBUG "zisofs: Null buffer on reading block table, inode = %lu, block = %lu\n", | |||
| 104 | inode->i_ino, blockptr >> bufshift); | |||
| 0 | - | 105 | goto eio; | |
| 106 | } | |||
| 107 | ll_rw_block(READ, indexblocks, ptrbh); | |||
| 108 | ||||
| 109 | bh = ptrbh[0]; | |||
| 0 | 0 | - | 110 | if ( !bh || (wait_on_buffer(bh), !buffer_uptodate(bh)) ) { |
| 0 | - | 110 | T || (_) | |
| 0 | - | 110 | F || (T) | |
| 0 | - | 110 | F || (F) | |
| 111 | printk(KERN_DEBUG "zisofs: Failed to read block table, inode = %lu, block = %lu\n", | |||
| 112 | inode->i_ino, blockptr >> bufshift); | |||
| 0 | 0 | - | 113 | if ( ptrbh[1] ) |
| 114 | brelse(ptrbh[1]); | |||
| 0 | - | 115 | goto eio; | |
| 116 | } | |||
| 117 | cstart = le32_to_cpu(*(__le32 *)(bh->b_data + (blockptr & bufmask))); | |||
| 118 | ||||
| 0 | 0 | - | 119 | if ( indexblocks == 2 ) { |
| 120 | /* We just crossed a block boundary. Switch to the next block */ | |||
| 121 | brelse(bh); | |||
| 122 | bh = ptrbh[1]; | |||
| 0 | 0 | - | 123 | if ( !bh || (wait_on_buffer(bh), !buffer_uptodate(bh)) ) { |
| 0 | - | 123 | T || (_) | |
| 0 | - | 123 | F || (T) | |
| 0 | - | 123 | F || (F) | |
| 124 | printk(KERN_DEBUG "zisofs: Failed to read block table, inode = %lu, block = %lu\n", | |||
| 125 | inode->i_ino, blockendptr >> bufshift); | |||
| 0 | - | 126 | goto eio; | |
| 127 | } | |||
| 128 | } | |||
| 129 | cend = le32_to_cpu(*(__le32 *)(bh->b_data + (blockendptr & bufmask))); | |||
| 130 | brelse(bh); | |||
| 131 | ||||
| 0 | 0 | - | 132 | if (cstart > cend) |
| 0 | - | 133 | goto eio; | |
| 134 | ||||
| 135 | csize = cend-cstart; | |||
| 136 | ||||
| 0 | 0 | - | 137 | if (csize > deflateBound(1UL << zisofs_block_shift)) |
| 0 | - | 138 | goto eio; | |
| 139 | ||||
| 140 | /* Now page[] contains an array of pages, any of which can be NULL, | |||
| 141 | and the locks on which we hold. We should now read the data and | |||
| 142 | release the pages. If the pages are NULL the decompressed data | |||
| 143 | for that particular page should be discarded. */ | |||
| 144 | ||||
| 0 | 0 | - | 145 | if ( csize == 0 ) { |
| 146 | /* This data block is empty. */ | |||
| 147 | ||||
| 0 | 0 | - | 148 | for ( fpage = 0 ; fpage < maxpage ; fpage++ ) { |
| 0 | 0 | - | 149 | if ( (page = pages[fpage]) != NULL ) { |
| 150 | memset(page_address(page), 0, PAGE_CACHE_SIZE); | |||
| 151 | ||||
| 152 | flush_dcache_page(page); | |||
| 0 | 0 | - | 152 | do-while (0) |
| 153 | SetPageUptodate(page); | |||
| 154 | kunmap(page); | |||
| 0 | 0 | - | 154 | do-while (0) |
| 155 | unlock_page(page); | |||
| 0 | 0 | - | 156 | if ( fpage == xpage ) |
| 157 | err = 0; /* The critical page */ | |||
| 158 | else | |||
| 159 | page_cache_release(page); | |||
| 160 | } | |||
| 161 | } | |||
| 162 | } else { | |||
| 163 | /* This data block is compressed. */ | |||
| 164 | z_stream stream; | |||
| 165 | int bail = 0, left_out = -1; | |||
| 166 | int zerr; | |||
| 167 | int needblocks = (csize + (cstart & bufmask) + bufmask) >> bufshift; | |||
| 168 | int haveblocks; | |||
| 169 | struct buffer_head *bhs[needblocks+1]; | |||
| 170 | struct buffer_head **bhptr; | |||
| 171 | ||||
| 172 | /* Because zlib is not thread-safe, do all the I/O at the top. */ | |||
| 173 | ||||
| 174 | blockptr = cstart >> bufshift; | |||
| 175 | memset(bhs, 0, (needblocks+1)*sizeof(struct buffer_head *)); | |||
| 176 | haveblocks = isofs_get_blocks(inode, blockptr, bhs, needblocks); | |||
| 177 | ll_rw_block(READ, haveblocks, bhs); | |||
| 178 | ||||
| 179 | bhptr = &bhs[0]; | |||
| 180 | bh = *bhptr++; | |||
| 181 | ||||
| 182 | /* First block is special since it may be fractional. | |||
| 183 | We also wait for it before grabbing the zlib | |||
| 184 | semaphore; odds are that the subsequent blocks are | |||
| 185 | going to come in in short order so we don't hold | |||
| 186 | the zlib semaphore longer than necessary. */ | |||
| 187 | ||||
| 0 | 0 | - | 188 | if ( !bh || (wait_on_buffer(bh), !buffer_uptodate(bh)) ) { |
| 0 | - | 188 | T || (_) | |
| 0 | - | 188 | F || (T) | |
| 0 | - | 188 | F || (F) | |
| 189 | printk(KERN_DEBUG "zisofs: Hit null buffer, fpage = %d, xpage = %d, csize = %ld\n", | |||
| 190 | fpage, xpage, csize); | |||
| 0 | - | 191 | goto b_eio; | |
| 192 | } | |||
| 193 | stream.next_in = bh->b_data + (cstart & bufmask); | |||
| 194 | stream.avail_in = min(bufsize-(cstart & bufmask), csize); | |||
| 195 | csize -= stream.avail_in; | |||
| 196 | ||||
| 197 | stream.workspace = zisofs_zlib_workspace; | |||
| 198 | down(&zisofs_zlib_semaphore); | |||
| 199 | ||||
| 200 | zerr = zlib_inflateInit(&stream); | |||
| 0 | 0 | - | 201 | if ( zerr != Z_OK ) { |
| 0 | 0 | - | 202 | if ( err && zerr == Z_MEM_ERROR ) |
| 0 | - | 202 | T && T | |
| 0 | - | 202 | T && F | |
| 0 | - | 202 | F && _ | |
| 203 | err = -ENOMEM; | |||
| 204 | printk(KERN_DEBUG "zisofs: zisofs_inflateInit returned %d\n", | |||
| 205 | zerr); | |||
| 0 | - | 206 | goto z_eio; | |
| 207 | } | |||
| 208 | ||||
| 0 | 0 | - | 209 | while ( !bail && fpage < maxpage ) { |
| 0 | - | 209 | T && T | |
| 0 | - | 209 | T && F | |
| 0 | - | 209 | F && _ | |
| 210 | page = pages[fpage]; | |||
| 0 | 0 | - | 211 | if ( page ) |
| 212 | stream.next_out = page_address(page); | |||
| 213 | else | |||
| 214 | stream.next_out = (void *)&zisofs_sink_page; | |||
| 215 | stream.avail_out = PAGE_CACHE_SIZE; | |||
| 216 | ||||
| 0 | 0 | - | 217 | while ( stream.avail_out ) { |
| 218 | int ao, ai; | |||
| 0 | 0 | - | 219 | if ( stream.avail_in == 0 && left_out ) { |
| 0 | - | 219 | T && T | |
| 0 | - | 219 | T && F | |
| 0 | - | 219 | F && _ | |
| 0 | 0 | - | 220 | if ( !csize ) { |
| 221 | printk(KERN_WARNING "zisofs: ZF read beyond end of input\n"); | |||
| 222 | bail = 1; | |||
| 0 | - | 223 | break; | |
| 224 | } else { | |||
| 225 | bh = *bhptr++; | |||
| 226 | if ( !bh || | |||
| 0 | 0 | - | 227 | (wait_on_buffer(bh), !buffer_uptodate(bh)) ) { |
| 0 | - | 227 | T || (_) | |
| 0 | - | 227 | F || (T) | |
| 0 | - | 227 | F || (F) | |
| 228 | /* Reached an EIO */ | |||
| 229 | printk(KERN_DEBUG "zisofs: Hit null buffer, fpage = %d, xpage = %d, csize = %ld\n", | |||
| 230 | fpage, xpage, csize); | |||
| 231 | ||||
| 232 | bail = 1; | |||
| 0 | - | 233 | break; | |
| 234 | } | |||
| 235 | stream.next_in = bh->b_data; | |||
| 236 | stream.avail_in = min(csize,bufsize); | |||
| 237 | csize -= stream.avail_in; | |||
| 238 | } | |||
| 239 | } | |||
| 240 | ao = stream.avail_out; ai = stream.avail_in; | |||
| 241 | zerr = zlib_inflate(&stream, Z_SYNC_FLUSH); | |||
| 242 | left_out = stream.avail_out; | |||
| 0 | 0 | - | 243 | if ( zerr == Z_BUF_ERROR && stream.avail_in == 0 ) |
| 0 | - | 243 | T && T | |
| 0 | - | 243 | T && F | |
| 0 | - | 243 | F && _ | |
| 0 | - | 244 | continue; | |
| 0 | 0 | - | 245 | if ( zerr != Z_OK ) { |
| 246 | /* EOF, error, or trying to read beyond end of input */ | |||
| 0 | 0 | - | 247 | if ( err && zerr == Z_MEM_ERROR ) |
| 0 | - | 247 | T && T | |
| 0 | - | 247 | T && F | |
| 0 | - | 247 | F && _ | |
| 248 | err = -ENOMEM; | |||
| 0 | 0 | - | 249 | if ( zerr != Z_STREAM_END ) |
| 250 | printk(KERN_DEBUG "zisofs: zisofs_inflate returned %d, inode = %lu, index = %lu, fpage = %d, xpage = %d, avail_in = %d, avail_out = %d, ai = %d, ao = %d\n", | |||
| 251 | zerr, inode->i_ino, index, | |||
| 252 | fpage, xpage, | |||
| 253 | stream.avail_in, stream.avail_out, | |||
| 254 | ai, ao); | |||
| 255 | bail = 1; | |||
| 0 | - | 256 | break; | |
| 257 | } | |||
| 258 | } | |||
| 259 | ||||
| 0 | 0 | - | 260 | if ( stream.avail_out && zerr == Z_STREAM_END ) { |
| 0 | - | 260 | T && T | |
| 0 | - | 260 | T && F | |
| 0 | - | 260 | F && _ | |
| 261 | /* Fractional page written before EOF. This may | |||
| 262 | be the last page in the file. */ | |||
| 263 | memset(stream.next_out, 0, stream.avail_out); | |||
| 264 | stream.avail_out = 0; | |||
| 265 | } | |||
| 266 | ||||
| 0 | 0 | - | 267 | if ( !stream.avail_out ) { |
| 268 | /* This page completed */ | |||
| 0 | 0 | - | 269 | if ( page ) { |
| 270 | flush_dcache_page(page); | |||
| 0 | 0 | - | 270 | do-while (0) |
| 271 | SetPageUptodate(page); | |||
| 272 | kunmap(page); | |||
| 0 | 0 | - | 272 | do-while (0) |
| 273 | unlock_page(page); | |||
| 0 | 0 | - | 274 | if ( fpage == xpage ) |
| 275 | err = 0; /* The critical page */ | |||
| 276 | else | |||
| 277 | page_cache_release(page); | |||
| 278 | } | |||
| 279 | fpage++; | |||
| 280 | } | |||
| 281 | } | |||
| 282 | zlib_inflateEnd(&stream); | |||
| 283 | ||||
| 284 | z_eio: | |||
| 285 | up(&zisofs_zlib_semaphore); | |||
| 286 | ||||
| 287 | b_eio: | |||
| 0 | 0 | - | 288 | for ( i = 0 ; i < haveblocks ; i++ ) { |
| 0 | 0 | - | 289 | if ( bhs[i] ) |
| 290 | brelse(bhs[i]); | |||
| 291 | } | |||
| 292 | } | |||
| 293 | ||||
| 294 | eio: | |||
| 295 | ||||
| 296 | /* Release any residual pages, do not SetPageUptodate */ | |||
| 0 | 0 | - | 297 | while ( fpage < maxpage ) { |
| 298 | page = pages[fpage]; | |||
| 0 | 0 | - | 299 | if ( page ) { |
| 300 | flush_dcache_page(page); | |||
| 0 | 0 | - | 300 | do-while (0) |
| 0 | 0 | - | 301 | if ( fpage == xpage ) |
| 302 | SetPageError(page); | |||
| 303 | kunmap(page); | |||
| 0 | 0 | - | 303 | do-while (0) |
| 304 | unlock_page(page); | |||
| 0 | 0 | - | 305 | if ( fpage != xpage ) |
| 306 | page_cache_release(page); | |||
| 307 | } | |||
| 308 | fpage++; | |||
| 309 | } | |||
| 310 | ||||
| 311 | /* At this point, err contains 0 or -EIO depending on the "critical" page */ | |||
| 0 | - | 312 | return err; | |
| 313 | } | |||
| 314 | ||||
| 315 | struct address_space_operations zisofs_aops = { | |||
| 316 | .readpage = zisofs_readpage, | |||
| 317 | /* No sync_page operation supported? */ | |||
| 318 | /* No bmap operation supported */ | |||
| 319 | }; | |||
| 320 | ||||
| 321 | static int initialized; | |||
| 322 | ||||
| 6 | 0 | 323 | int __init zisofs_init(void) | |
| 324 | { | |||
| 0 | 6 | - | 325 | if ( initialized ) { |
| 326 | printk("zisofs_init: called more than once\n"); | |||
| 0 | - | 327 | return 0; | |
| 328 | } | |||
| 329 | ||||
| 330 | zisofs_zlib_workspace = vmalloc(zlib_inflate_workspacesize()); | |||
| 0 | 6 | - | 331 | if ( !zisofs_zlib_workspace ) |
| 0 | - | 332 | return -ENOMEM; | |
| 333 | init_MUTEX(&zisofs_zlib_semaphore); | |||
| 334 | ||||
| 335 | initialized = 1; | |||
| 6 | 336 | return 0; | ||
| 337 | } | |||
| 338 | ||||
| 0 | 0 | - | 339 | void zisofs_cleanup(void) |
| 340 | { | |||
| 0 | 0 | - | 341 | if ( !initialized ) { |
| 342 | printk("zisofs_cleanup: called without initialization\n"); | |||
| 0 | - | 343 | return; | |
| 344 | } | |||
| 345 | ||||
| 346 | vfree(zisofs_zlib_workspace); | |||
| 347 | initialized = 0; | |||
| 348 | } | |||
| ***TER 3% (4/145) of SOURCE FILE compress.c | ||||