Commit 59bc0552 authored by Jan Kara's avatar Jan Kara

zisofs: Implement reading of compressed files when PAGE_CACHE_SIZE > compress block size

Also split and cleanup zisofs_readpage() when we are changing it anyway.
Signed-off-by: default avatarJan Kara <jack@suse.cz>
parent 3067e02f
...@@ -36,286 +36,323 @@ static void *zisofs_zlib_workspace; ...@@ -36,286 +36,323 @@ static void *zisofs_zlib_workspace;
static DEFINE_MUTEX(zisofs_zlib_lock); static DEFINE_MUTEX(zisofs_zlib_lock);
/* /*
* When decompressing, we typically obtain more than one page * Read data of @inode from @block_start to @block_end and uncompress
* per reference. We inject the additional pages into the page * to one zisofs block. Store the data in the @pages array with @pcount
* cache as a form of readahead. * entries. Start storing at offset @poffset of the first page.
*/ */
static int zisofs_readpage(struct file *file, struct page *page) 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)
{ {
struct inode *inode = file->f_path.dentry->d_inode;
struct address_space *mapping = inode->i_mapping;
unsigned int maxpage, xpage, fpage, blockindex;
unsigned long offset;
unsigned long blockptr, blockendptr, cstart, cend, csize;
struct buffer_head *bh, *ptrbh[2];
unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
unsigned int bufshift = ISOFS_BUFFER_BITS(inode);
unsigned long bufmask = bufsize - 1;
int err = -EIO;
int i;
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 zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
/* unsigned long zisofs_block_size = 1UL << zisofs_block_shift; */ unsigned int bufsize = ISOFS_BUFFER_SIZE(inode);
unsigned int zisofs_block_page_shift = zisofs_block_shift-PAGE_CACHE_SHIFT; unsigned int bufshift = ISOFS_BUFFER_BITS(inode);
unsigned long zisofs_block_pages = 1UL << zisofs_block_page_shift; unsigned int bufmask = bufsize - 1;
unsigned long zisofs_block_page_mask = zisofs_block_pages-1; int i, block_size = block_end - block_start;
struct page *pages[zisofs_block_pages]; z_stream stream = { .total_out = 0,
unsigned long index = page->index; .avail_in = 0,
int indexblocks; .avail_out = 0, };
int zerr;
/* We have already been given one page, this is the one int needblocks = (block_size + (block_start & bufmask) + bufmask)
we must do. */ >> bufshift;
xpage = index & zisofs_block_page_mask; int haveblocks;
pages[xpage] = page; blkcnt_t blocknum;
struct buffer_head *bhs[needblocks + 1];
/* The remaining pages need to be allocated and inserted */ int curbh, curpage;
offset = index & ~zisofs_block_page_mask;
blockindex = offset >> zisofs_block_page_shift; if (block_size > deflateBound(1UL << zisofs_block_shift)) {
maxpage = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; *errp = -EIO;
/*
* If this page is wholly outside i_size we just return zero;
* do_generic_file_read() will handle this for us
*/
if (page->index >= maxpage) {
SetPageUptodate(page);
unlock_page(page);
return 0; return 0;
} }
/* Empty block? */
maxpage = min(zisofs_block_pages, maxpage-offset); if (block_size == 0) {
for ( i = 0 ; i < pcount ; i++ ) {
for ( i = 0 ; i < maxpage ; i++, offset++ ) { if (!pages[i])
if ( i != xpage ) { continue;
pages[i] = grab_cache_page_nowait(mapping, offset); memset(page_address(pages[i]), 0, PAGE_CACHE_SIZE);
} flush_dcache_page(pages[i]);
page = pages[i]; SetPageUptodate(pages[i]);
if ( page ) {
ClearPageError(page);
kmap(page);
} }
return ((loff_t)pcount) << PAGE_CACHE_SHIFT;
} }
/* This is the last page filled, plus one; used in case of abort. */ /* Because zlib is not thread-safe, do all the I/O at the top. */
fpage = 0; 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);
/* Find the pointer to this specific chunk */ curbh = 0;
/* Note: we're not using isonum_731() here because the data is known aligned */ curpage = 0;
/* Note: header_size is in 32-bit words (4 bytes) */ /*
blockptr = (header_size + blockindex) << 2; * First block is special since it may be fractional. We also wait for
blockendptr = blockptr + 4; * 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.
*/
indexblocks = ((blockptr^blockendptr) >> bufshift) ? 2 : 1; if (!bhs[0])
ptrbh[0] = ptrbh[1] = NULL; goto b_eio;
if ( isofs_get_blocks(inode, blockptr >> bufshift, ptrbh, indexblocks) != indexblocks ) { wait_on_buffer(bhs[0]);
if ( ptrbh[0] ) brelse(ptrbh[0]); if (!buffer_uptodate(bhs[0])) {
printk(KERN_DEBUG "zisofs: Null buffer on reading block table, inode = %lu, block = %lu\n", *errp = -EIO;
inode->i_ino, blockptr >> bufshift); goto b_eio;
goto eio;
}
ll_rw_block(READ, indexblocks, ptrbh);
bh = ptrbh[0];
if ( !bh || (wait_on_buffer(bh), !buffer_uptodate(bh)) ) {
printk(KERN_DEBUG "zisofs: Failed to read block table, inode = %lu, block = %lu\n",
inode->i_ino, blockptr >> bufshift);
if ( ptrbh[1] )
brelse(ptrbh[1]);
goto eio;
}
cstart = le32_to_cpu(*(__le32 *)(bh->b_data + (blockptr & bufmask)));
if ( indexblocks == 2 ) {
/* We just crossed a block boundary. Switch to the next block */
brelse(bh);
bh = ptrbh[1];
if ( !bh || (wait_on_buffer(bh), !buffer_uptodate(bh)) ) {
printk(KERN_DEBUG "zisofs: Failed to read block table, inode = %lu, block = %lu\n",
inode->i_ino, blockendptr >> bufshift);
goto eio;
}
} }
cend = le32_to_cpu(*(__le32 *)(bh->b_data + (blockendptr & bufmask)));
brelse(bh);
if (cstart > cend) stream.workspace = zisofs_zlib_workspace;
goto eio; mutex_lock(&zisofs_zlib_lock);
csize = cend-cstart; zerr = zlib_inflateInit(&stream);
if (zerr != Z_OK) {
if (csize > deflateBound(1UL << zisofs_block_shift)) if (zerr == Z_MEM_ERROR)
goto eio; *errp = -ENOMEM;
else
/* Now page[] contains an array of pages, any of which can be NULL, *errp = -EIO;
and the locks on which we hold. We should now read the data and printk(KERN_DEBUG "zisofs: zisofs_inflateInit returned %d\n",
release the pages. If the pages are NULL the decompressed data zerr);
for that particular page should be discarded. */ goto z_eio;
}
if ( csize == 0 ) {
/* This data block is empty. */ while (curpage < pcount && curbh < haveblocks &&
zerr != Z_STREAM_END) {
for ( fpage = 0 ; fpage < maxpage ; fpage++ ) { if (!stream.avail_out) {
if ( (page = pages[fpage]) != NULL ) { if (pages[curpage]) {
memset(page_address(page), 0, PAGE_CACHE_SIZE); stream.next_out = page_address(pages[curpage])
+ poffset;
flush_dcache_page(page); stream.avail_out = PAGE_CACHE_SIZE - poffset;
SetPageUptodate(page); poffset = 0;
kunmap(page); } else {
unlock_page(page); stream.next_out = (void *)&zisofs_sink_page;
if ( fpage == xpage ) stream.avail_out = PAGE_CACHE_SIZE;
err = 0; /* The critical page */
else
page_cache_release(page);
} }
} }
} else { if (!stream.avail_in) {
/* This data block is compressed. */ wait_on_buffer(bhs[curbh]);
z_stream stream; if (!buffer_uptodate(bhs[curbh])) {
int bail = 0, left_out = -1; *errp = -EIO;
int zerr; break;
int needblocks = (csize + (cstart & bufmask) + bufmask) >> bufshift; }
int haveblocks; stream.next_in = bhs[curbh]->b_data +
struct buffer_head *bhs[needblocks+1]; (block_start & bufmask);
struct buffer_head **bhptr; stream.avail_in = min_t(unsigned, bufsize -
(block_start & bufmask),
/* Because zlib is not thread-safe, do all the I/O at the top. */ block_size);
block_size -= stream.avail_in;
blockptr = cstart >> bufshift; block_start = 0;
memset(bhs, 0, (needblocks+1)*sizeof(struct buffer_head *));
haveblocks = isofs_get_blocks(inode, blockptr, bhs, needblocks);
ll_rw_block(READ, haveblocks, bhs);
bhptr = &bhs[0];
bh = *bhptr++;
/* 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. */
if ( !bh || (wait_on_buffer(bh), !buffer_uptodate(bh)) ) {
printk(KERN_DEBUG "zisofs: Hit null buffer, fpage = %d, xpage = %d, csize = %ld\n",
fpage, xpage, csize);
goto b_eio;
}
stream.next_in = bh->b_data + (cstart & bufmask);
stream.avail_in = min(bufsize-(cstart & bufmask), csize);
csize -= stream.avail_in;
stream.workspace = zisofs_zlib_workspace;
mutex_lock(&zisofs_zlib_lock);
zerr = zlib_inflateInit(&stream);
if ( zerr != Z_OK ) {
if ( err && zerr == Z_MEM_ERROR )
err = -ENOMEM;
printk(KERN_DEBUG "zisofs: zisofs_inflateInit returned %d\n",
zerr);
goto z_eio;
} }
while ( !bail && fpage < maxpage ) { while (stream.avail_out && stream.avail_in) {
page = pages[fpage]; zerr = zlib_inflate(&stream, Z_SYNC_FLUSH);
if ( page ) if (zerr == Z_BUF_ERROR && stream.avail_in == 0)
stream.next_out = page_address(page); break;
else if (zerr == Z_STREAM_END)
stream.next_out = (void *)&zisofs_sink_page; break;
stream.avail_out = PAGE_CACHE_SIZE; if (zerr != Z_OK) {
/* EOF, error, or trying to read beyond end of input */
while ( stream.avail_out ) { if (zerr == Z_MEM_ERROR)
int ao, ai; *errp = -ENOMEM;
if ( stream.avail_in == 0 && left_out ) { else {
if ( !csize ) { printk(KERN_DEBUG
printk(KERN_WARNING "zisofs: ZF read beyond end of input\n"); "zisofs: zisofs_inflate returned"
bail = 1; " %d, inode = %lu,"
break; " page idx = %d, bh idx = %d,"
} else { " avail_in = %d,"
bh = *bhptr++; " avail_out = %d\n",
if ( !bh || zerr, inode->i_ino, curpage,
(wait_on_buffer(bh), !buffer_uptodate(bh)) ) { curbh, stream.avail_in,
/* Reached an EIO */ stream.avail_out);
printk(KERN_DEBUG "zisofs: Hit null buffer, fpage = %d, xpage = %d, csize = %ld\n", *errp = -EIO;
fpage, xpage, csize);
bail = 1;
break;
}
stream.next_in = bh->b_data;
stream.avail_in = min(csize,bufsize);
csize -= stream.avail_in;
}
}
ao = stream.avail_out; ai = stream.avail_in;
zerr = zlib_inflate(&stream, Z_SYNC_FLUSH);
left_out = stream.avail_out;
if ( zerr == Z_BUF_ERROR && stream.avail_in == 0 )
continue;
if ( zerr != Z_OK ) {
/* EOF, error, or trying to read beyond end of input */
if ( err && zerr == Z_MEM_ERROR )
err = -ENOMEM;
if ( zerr != Z_STREAM_END )
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",
zerr, inode->i_ino, index,
fpage, xpage,
stream.avail_in, stream.avail_out,
ai, ao);
bail = 1;
break;
} }
goto inflate_out;
} }
}
if ( stream.avail_out && zerr == Z_STREAM_END ) { if (!stream.avail_out) {
/* Fractional page written before EOF. This may /* This page completed */
be the last page in the file. */ if (pages[curpage]) {
memset(stream.next_out, 0, stream.avail_out); flush_dcache_page(pages[curpage]);
stream.avail_out = 0; SetPageUptodate(pages[curpage]);
} }
curpage++;
}
if (!stream.avail_in)
curbh++;
}
inflate_out:
zlib_inflateEnd(&stream);
if ( !stream.avail_out ) { z_eio:
/* This page completed */ mutex_unlock(&zisofs_zlib_lock);
if ( page ) {
flush_dcache_page(page); b_eio:
SetPageUptodate(page); for (i = 0; i < haveblocks; i++)
kunmap(page); brelse(bhs[i]);
unlock_page(page); return stream.total_out;
if ( fpage == xpage ) }
err = 0; /* The critical page */
else /*
page_cache_release(page); * Uncompress data so that pages[full_page] is fully uptodate and possibly
} * fills in other pages if we have data for them.
fpage++; */
} 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;
} }
zlib_inflateEnd(&stream);
z_eio: block_start = block_end;
mutex_unlock(&zisofs_zlib_lock); cstart_block++;
}
if (poffset && *pages) {
memset(page_address(*pages) + poffset, 0,
PAGE_CACHE_SIZE - poffset);
flush_dcache_page(*pages);
SetPageUptodate(*pages);
}
return 0;
}
b_eio: /*
for ( i = 0 ; i < haveblocks ; i++ ) { * When decompressing, we typically obtain more than one page
if ( bhs[i] ) * per reference. We inject the additional pages into the page
brelse(bhs[i]); * 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]);
} }
} }
eio: err = zisofs_fill_pages(inode, full_page, pcount, pages);
/* Release any residual pages, do not SetPageUptodate */ /* Release any residual pages, do not SetPageUptodate */
while ( fpage < maxpage ) { for (i = 0; i < pcount; i++) {
page = pages[fpage]; if (pages[i]) {
if ( page ) { flush_dcache_page(pages[i]);
flush_dcache_page(page); if (i == full_page && err)
if ( fpage == xpage ) SetPageError(pages[i]);
SetPageError(page); kunmap(pages[i]);
kunmap(page); unlock_page(pages[i]);
unlock_page(page); if (i != full_page)
if ( fpage != xpage ) page_cache_release(pages[i]);
page_cache_release(page);
} }
fpage++;
} }
/* At this point, err contains 0 or -EIO depending on the "critical" page */ /* At this point, err contains 0 or -EIO depending on the "critical" page */
......
...@@ -518,8 +518,7 @@ parse_rock_ridge_inode_internal(struct iso_directory_record *de, ...@@ -518,8 +518,7 @@ parse_rock_ridge_inode_internal(struct iso_directory_record *de,
if (algo == SIG('p', 'z')) { if (algo == SIG('p', 'z')) {
int block_shift = int block_shift =
isonum_711(&rr->u.ZF.parms[1]); isonum_711(&rr->u.ZF.parms[1]);
if (block_shift < PAGE_CACHE_SHIFT if (block_shift > 17) {
|| block_shift > 17) {
printk(KERN_WARNING "isofs: " printk(KERN_WARNING "isofs: "
"Can't handle ZF block " "Can't handle ZF block "
"size of 2^%d\n", "size of 2^%d\n",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment