linux/fs/isofs/compress.c
<<
>>
Prefs
   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/module.h>
  20#include <linux/init.h>
  21
  22#include <linux/vmalloc.h>
  23#include <linux/zlib.h>
  24
  25#include "isofs.h"
  26#include "zisofs.h"
  27
  28/* This should probably be global. */
  29static char zisofs_sink_page[PAGE_CACHE_SIZE];
  30
  31/*
  32 * This contains the zlib memory allocation and the mutex for the
  33 * allocation; this avoids failures at block-decompression time.
  34 */
  35static void *zisofs_zlib_workspace;
  36static DEFINE_MUTEX(zisofs_zlib_lock);
  37
  38/*
  39 * Read data of @inode from @block_start to @block_end and uncompress
  40 * to one zisofs block. Store the data in the @pages array with @pcount
  41 * entries. Start storing at offset @poffset of the first page.
  42 */
  43static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start,
  44                                      loff_t block_end, int pcount,
  45                                      struct page **pages, unsigned poffset,
  46                                      int *errp)
  47{
  48        unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
  49        unsigned int bufsize = ISOFS_BUFFER_SIZE(inode);
  50        unsigned int bufshift = ISOFS_BUFFER_BITS(inode);
  51        unsigned int bufmask = bufsize - 1;
  52        int i, block_size = block_end - block_start;
  53        z_stream stream = { .total_out = 0,
  54                            .avail_in = 0,
  55                            .avail_out = 0, };
  56        int zerr;
  57        int needblocks = (block_size + (block_start & bufmask) + bufmask)
  58                                >> bufshift;
  59        int haveblocks;
  60        blkcnt_t blocknum;
  61        struct buffer_head *bhs[needblocks + 1];
  62        int curbh, curpage;
  63
  64        if (block_size > deflateBound(1UL << zisofs_block_shift)) {
  65                *errp = -EIO;
  66                return 0;
  67        }
  68        /* Empty block? */
  69        if (block_size == 0) {
  70                for ( i = 0 ; i < pcount ; i++ ) {
  71                        if (!pages[i])
  72                                continue;
  73                        memset(page_address(pages[i]), 0, PAGE_CACHE_SIZE);
  74                        flush_dcache_page(pages[i]);
  75                        SetPageUptodate(pages[i]);
  76                }
  77                return ((loff_t)pcount) << PAGE_CACHE_SHIFT;
  78        }
  79
  80        /* Because zlib is not thread-safe, do all the I/O at the top. */
  81        blocknum = block_start >> bufshift;
  82        memset(bhs, 0, (needblocks + 1) * sizeof(struct buffer_head *));
  83        haveblocks = isofs_get_blocks(inode, blocknum, bhs, needblocks);
  84        ll_rw_block(READ, haveblocks, bhs);
  85
  86        curbh = 0;
  87        curpage = 0;
  88        /*
  89         * First block is special since it may be fractional.  We also wait for
  90         * it before grabbing the zlib mutex; odds are that the subsequent
  91         * blocks are going to come in in short order so we don't hold the zlib
  92         * mutex longer than necessary.
  93         */
  94
  95        if (!bhs[0])
  96                goto b_eio;
  97
  98        wait_on_buffer(bhs[0]);
  99        if (!buffer_uptodate(bhs[0])) {
 100                *errp = -EIO;
 101                goto b_eio;
 102        }
 103
 104        stream.workspace = zisofs_zlib_workspace;
 105        mutex_lock(&zisofs_zlib_lock);
 106                
 107        zerr = zlib_inflateInit(&stream);
 108        if (zerr != Z_OK) {
 109                if (zerr == Z_MEM_ERROR)
 110                        *errp = -ENOMEM;
 111                else
 112                        *errp = -EIO;
 113                printk(KERN_DEBUG "zisofs: zisofs_inflateInit returned %d\n",
 114                               zerr);
 115                goto z_eio;
 116        }
 117
 118        while (curpage < pcount && curbh < haveblocks &&
 119               zerr != Z_STREAM_END) {
 120                if (!stream.avail_out) {
 121                        if (pages[curpage]) {
 122                                stream.next_out = page_address(pages[curpage])
 123                                                + poffset;
 124                                stream.avail_out = PAGE_CACHE_SIZE - poffset;
 125                                poffset = 0;
 126                        } else {
 127                                stream.next_out = (void *)&zisofs_sink_page;
 128                                stream.avail_out = PAGE_CACHE_SIZE;
 129                        }
 130                }
 131                if (!stream.avail_in) {
 132                        wait_on_buffer(bhs[curbh]);
 133                        if (!buffer_uptodate(bhs[curbh])) {
 134                                *errp = -EIO;
 135                                break;
 136                        }
 137                        stream.next_in  = bhs[curbh]->b_data +
 138                                                (block_start & bufmask);
 139                        stream.avail_in = min_t(unsigned, bufsize -
 140                                                (block_start & bufmask),
 141                                                block_size);
 142                        block_size -= stream.avail_in;
 143                        block_start = 0;
 144                }
 145
 146                while (stream.avail_out && stream.avail_in) {
 147                        zerr = zlib_inflate(&stream, Z_SYNC_FLUSH);
 148                        if (zerr == Z_BUF_ERROR && stream.avail_in == 0)
 149                                break;
 150                        if (zerr == Z_STREAM_END)
 151                                break;
 152                        if (zerr != Z_OK) {
 153                                /* EOF, error, or trying to read beyond end of input */
 154                                if (zerr == Z_MEM_ERROR)
 155                                        *errp = -ENOMEM;
 156                                else {
 157                                        printk(KERN_DEBUG
 158                                               "zisofs: zisofs_inflate returned"
 159                                               " %d, inode = %lu,"
 160                                               " page idx = %d, bh idx = %d,"
 161                                               " avail_in = %d,"
 162                                               " avail_out = %d\n",
 163                                               zerr, inode->i_ino, curpage,
 164                                               curbh, stream.avail_in,
 165                                               stream.avail_out);
 166                                        *errp = -EIO;
 167                                }
 168                                goto inflate_out;
 169                        }
 170                }
 171
 172                if (!stream.avail_out) {
 173                        /* This page completed */
 174                        if (pages[curpage]) {
 175                                flush_dcache_page(pages[curpage]);
 176                                SetPageUptodate(pages[curpage]);
 177                        }
 178                        curpage++;
 179                }
 180                if (!stream.avail_in)
 181                        curbh++;
 182        }
 183inflate_out:
 184        zlib_inflateEnd(&stream);
 185
 186z_eio:
 187        mutex_unlock(&zisofs_zlib_lock);
 188
 189b_eio:
 190        for (i = 0; i < haveblocks; i++)
 191                brelse(bhs[i]);
 192        return stream.total_out;
 193}
 194
 195/*
 196 * Uncompress data so that pages[full_page] is fully uptodate and possibly
 197 * fills in other pages if we have data for them.
 198 */
 199static int zisofs_fill_pages(struct inode *inode, int full_page, int pcount,
 200                             struct page **pages)
 201{
 202        loff_t start_off, end_off;
 203        loff_t block_start, block_end;
 204        unsigned int header_size = ISOFS_I(inode)->i_format_parm[0];
 205        unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
 206        unsigned int blockptr;
 207        loff_t poffset = 0;
 208        blkcnt_t cstart_block, cend_block;
 209        struct buffer_head *bh;
 210        unsigned int blkbits = ISOFS_BUFFER_BITS(inode);
 211        unsigned int blksize = 1 << blkbits;
 212        int err;
 213        loff_t ret;
 214
 215        BUG_ON(!pages[full_page]);
 216
 217        /*
 218         * We want to read at least 'full_page' page. Because we have to
 219         * uncompress the whole compression block anyway, fill the surrounding
 220         * pages with the data we have anyway...
 221         */
 222        start_off = page_offset(pages[full_page]);
 223        end_off = min_t(loff_t, start_off + PAGE_CACHE_SIZE, inode->i_size);
 224
 225        cstart_block = start_off >> zisofs_block_shift;
 226        cend_block = (end_off + (1 << zisofs_block_shift) - 1)
 227                        >> zisofs_block_shift;
 228
 229        WARN_ON(start_off - (full_page << PAGE_CACHE_SHIFT) !=
 230                ((cstart_block << zisofs_block_shift) & PAGE_CACHE_MASK));
 231
 232        /* Find the pointer to this specific chunk */
 233        /* Note: we're not using isonum_731() here because the data is known aligned */
 234        /* Note: header_size is in 32-bit words (4 bytes) */
 235        blockptr = (header_size + cstart_block) << 2;
 236        bh = isofs_bread(inode, blockptr >> blkbits);
 237        if (!bh)
 238                return -EIO;
 239        block_start = le32_to_cpu(*(__le32 *)
 240                                (bh->b_data + (blockptr & (blksize - 1))));
 241
 242        while (cstart_block < cend_block && pcount > 0) {
 243                /* Load end of the compressed block in the file */
 244                blockptr += 4;
 245                /* Traversed to next block? */
 246                if (!(blockptr & (blksize - 1))) {
 247                        brelse(bh);
 248
 249                        bh = isofs_bread(inode, blockptr >> blkbits);
 250                        if (!bh)
 251                                return -EIO;
 252                }
 253                block_end = le32_to_cpu(*(__le32 *)
 254                                (bh->b_data + (blockptr & (blksize - 1))));
 255                if (block_start > block_end) {
 256                        brelse(bh);
 257                        return -EIO;
 258                }
 259                err = 0;
 260                ret = zisofs_uncompress_block(inode, block_start, block_end,
 261                                              pcount, pages, poffset, &err);
 262                poffset += ret;
 263                pages += poffset >> PAGE_CACHE_SHIFT;
 264                pcount -= poffset >> PAGE_CACHE_SHIFT;
 265                full_page -= poffset >> PAGE_CACHE_SHIFT;
 266                poffset &= ~PAGE_CACHE_MASK;
 267
 268                if (err) {
 269                        brelse(bh);
 270                        /*
 271                         * Did we finish reading the page we really wanted
 272                         * to read?
 273                         */
 274                        if (full_page < 0)
 275                                return 0;
 276                        return err;
 277                }
 278
 279                block_start = block_end;
 280                cstart_block++;
 281        }
 282
 283        if (poffset && *pages) {
 284                memset(page_address(*pages) + poffset, 0,
 285                       poffset;
 1828/a>                flush_dcache_page(pages) + 1828/a>                }
 href="+code=patPageUptodate" class="sref">SetPageUptodate(pages) + 18288a>        }
 1828/a>          turn 0;
 192   1929/a>
 1929/a>  
 1929/a>   * When dempressionng,e retypicly waobtain morthe  clonpage wspan>
 192
   * per tuference. e wainjecthe padreitionalages if n thispage wspan>
 192/* * che_p as for tmf thad?
 192 * U/span>
 192zisofs_siad?(flue *a> *)
 href="+code=bule *class="sref">flue *a> *,truct page **< href="+code=page" class="sref">page * 192 1929/a>        struct inode *inode,  flue *a> *gt;bhfaget a> *a href="+code=todt">ryclass="sref">bhdt">rya> *gt;ind_ode" a>;
 203          ruct indressc a> *&lass="sref">memappg">a> = inode->&lass="sref">mesimappg">a>;
 2030/a>        }
t err;
 2030/a>        int i])pcount, full_page & 203        zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
 203        unsigned int zisofs_unges i_per_cock" a> =  2030/a>                PAGE_CACHE_SHIFT;
lt; 0zisofs_block_shift = ?a href="fs/isofs/compress.c#L213"6id="L203" class="line" name="L193"> 2030/a>                zisofs_block_shift = ofs/compress.c#L1GE_CACHE_SHIFT" class="sref">PAGE_CACHE_SHIFT) !) :;
 203        page **< href="+code=page"  class="sref">pages[minax" a>(zisofs_unges i_per_cock" a> =, 1)
 203        lopgf_t" a>    href="+code=inoddexclass="sref">inoddexa> = page *gt;inoddexa> =ind_offddexa> & 203  a href="fs/isofs/compress.c#L213" id="L213" class="line" name="L213"> 213        un href="+code=end_offddexclass="sref">ind_offddexa> & (inode->i_size); PAGE_CACHE_SIZE,  1)))gt;> PAGE_CACHE_SHIFT;
 213        unpan class="comment">  
 2131/a>           * Ifhis spge idxshole  waoutsididxize iswe just turn 0;zero/span>
 2131/a>           * do_genericill_eiad?<() wl thhandlehis spr thusspan>
 2131/a>
           */span>
 213        inoddexa> = t; <ind_offddexa> &{
 2131/a>                SetPageUptodate(page *< 2131/a>                }
 href="+code=palockfulock(page *< 2131/a>                return -<
 2131/a>        st 223 22321a>        PAGE_CACHE_SHIFT;
lt; 0zisofs_block_shift ={
 2232/a>                /* TrWhave anylad?
 2232/a>                     we must do. */span>
 2232/a>                full_page &linoddexa> = tp; (zisofs_unges i_per_cock" a> = 1))) 2232/a>                pcount -=min_t(zisofs_unges i_per_cock" a> =,a href="fs/isofs/compress.c#L273" id="L213" class="line" name="L223"> 2232/a>                        ind_offddexa> & (inoddexa> = tp; (<~a href="+code=blsofs_blges i_per_cock" class="sref">zisofs_unges i_per_cock" a> = 1)));
 223                   href="+code=fuoddexclass="sref">inoddexa> =  full_page & 22328a>        }
 2232/a>                full_page &l 233                (( href="+code=fuount" class="sref">pcount -= 2333/a>        }
 2333/a>        pages[full_page]); page * 233  a href="fs/isofs/compress.c#L273" id="L223" class="line" name="L233"> 233        i = 0; i < pcount -i++)
inoddexa> =++{
 2333/a>                i &l!full_page & 2333/a>                        pages[i]); pcgrab_che_page(&lass="sref">memappg">a> =inoddexa> =
 2333/a>                   (pages[i]);

 233                re      erCleargeUpErro a>(pages[i]);
 2333/a>                        i(pages[i]);
 243                   2434/a>        }
 2434/a>
 2434/a>        err = 0; href="+code=blsofs_blll_pages" class="sref">zisofs_fill_pages(st href="+code=heode" class="sref">inode-&full_page &pcount, pages) + 2434/a>
 243           an class="comment">/* TrReast anywa ss.idualages i, doot ustPageUptodate
 243          r (i = 0; i < pcount -i++)


 243                   (pages[i]);

 2434/a>                re      flush_dcache_page(pages[i]);
 243                        i &l=full_page && err) {a href="fs/isofs/compress.c#L253" id="L253" class="line" name="L253"> 253                        if      ertPageUpErro a>(pages[i]);
 253                           href="+code=brkunmapclass="sref">i(pages[i]);
 253                }
         href="+code=brlockfulock(pages[i]);
 253                i &l!full_page & 253                                (< href="+code=pages"_che_pareast aclass="sref">pages<_che_pareast aa>(pages[i]);
 253                if 253          }                     ( 2535/a>
 253           an class="comment">/* TrAthis spgoint
 2535/a>          turn 0; href="+code=pcr" class="sref">err)  2636/a>   2636/a>
 263  const ruct padressc _operation a>    href="+code=blsofs_blaop class="sref">zisofs_fiaop a> = 0; 263          a href="+code=toad?ziad?(< 0; href="+code=blsofs_blad?zisofs_siad?(<,a href="fs/isofs/compress.c#L273" id="L263" class="line" name="L263"> 263          pan class="comment">/* Note syncage
 2636/a>           an class="comment">/* Trte bmap operation supported/
 263  } 263
 263  t pc__ini a>    href="+code=blsofs_blini class="sref">pcsofs_blini a>( 263   2737/a>        un href="+code=ensofs_blzlib_workan>c class="sref">insofs_blzlib_workan>c a>(< 0; href="+code=blvmalckinvmalck(i_zlib_inflate_workan>c ze" a>( 27371a>        insofs_blzlib_workan>c a>(<  2737/a>                }
turn -inONOMEMa>)  2737/a>  a href="fs/isofs/compress.c#L273" id="L273" class="line" name="L273"> 273          turn -<
 273   2737/a>
 273  voidn href="+code=ensofs_blcastnupclass="sref">i( 2737/a> 273           href="+code=blvfre class="sref">invfre a>(insofs_blzlib_workan>c a>(<
 2838/a>   283  


T whoriginalaLXR fs/twa becyhe fi href="fshttp://sourceforge.net/esojects/lxr">LXR mmenuni ya>, alarsedn bloyi href="fsmailto:lxr@ne"ux.no">lxr@ne"ux.noa>, .
lxr.ne"ux.no kindly hosted/oyi href="fshttp://www.redpill-ne"eso.no">Redpill Le"eso ASa>,