/* This should probably be global. */ staticchar zisofs_sink_page[PAGE_SIZE];
/* * This contains the zlib memory allocation and the mutex for the * allocation; this avoids failures at block-decompression time.
*/ staticvoid *zisofs_zlib_workspace; static DEFINE_MUTEX(zisofs_zlib_lock);
/* * Read data of @inode from @block_start to @block_end and uncompress * to one zisofs block. Store the data in the @pages array with @pcount * entries. Start storing at offset @poffset of the first 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)
{ unsignedint zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1]; unsignedint bufsize = ISOFS_BUFFER_SIZE(inode); unsignedint bufshift = ISOFS_BUFFER_BITS(inode); unsignedint bufmask = bufsize - 1; int i, block_size = block_end - block_start;
z_stream stream = { .total_out = 0,
.avail_in = 0,
.avail_out = 0, }; int zerr; int needblocks = (block_size + (block_start & bufmask) + bufmask)
>> bufshift; int haveblocks;
blkcnt_t blocknum; struct buffer_head **bhs; int curbh, curpage;
if (block_size > deflateBound(1UL << zisofs_block_shift)) {
*errp = -EIO; return 0;
} /* Empty block? */ if (block_size == 0) { for ( i = 0 ; i < pcount ; i++ ) { if (!pages[i]) continue;
memzero_page(pages[i], 0, PAGE_SIZE);
SetPageUptodate(pages[i]);
} return ((loff_t)pcount) << PAGE_SHIFT;
}
/* Because zlib is not thread-safe, do all the I/O at the top. */
blocknum = block_start >> bufshift;
bhs = kcalloc(needblocks + 1, sizeof(*bhs), GFP_KERNEL); if (!bhs) {
*errp = -ENOMEM; return 0;
}
haveblocks = isofs_get_blocks(inode, blocknum, bhs, needblocks);
bh_read_batch(haveblocks, bhs);
curbh = 0;
curpage = 0; /* * 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 (!bhs[0]) goto b_eio;
wait_on_buffer(bhs[0]); if (!buffer_uptodate(bhs[0])) {
*errp = -EIO; goto b_eio;
}
while (stream.avail_out && stream.avail_in) {
zerr = zlib_inflate(&stream, Z_SYNC_FLUSH); if (zerr == Z_BUF_ERROR && stream.avail_in == 0) break; if (zerr == Z_STREAM_END) break; if (zerr != Z_OK) { /* EOF, error, or trying to read beyond end of input */ if (zerr == Z_MEM_ERROR)
*errp = -ENOMEM; else {
printk(KERN_DEBUG "zisofs: zisofs_inflate returned" " %d, inode = %lu," " page idx = %d, bh idx = %d," " avail_in = %ld," " avail_out = %ld\n",
zerr, inode->i_ino, curpage,
curbh, stream.avail_in,
stream.avail_out);
*errp = -EIO;
} goto inflate_out;
}
}
if (!stream.avail_out) { /* This page completed */ if (pages[curpage]) {
flush_dcache_page(pages[curpage]);
SetPageUptodate(pages[curpage]);
} if (stream.next_out != (unsignedchar *)zisofs_sink_page) {
kunmap_local(stream.next_out);
stream.next_out = NULL;
}
curpage++;
} if (!stream.avail_in)
curbh++;
}
inflate_out:
zlib_inflateEnd(&stream); if (stream.next_out && stream.next_out != (unsignedchar *)zisofs_sink_page)
kunmap_local(stream.next_out);
z_eio:
mutex_unlock(&zisofs_zlib_lock);
b_eio: for (i = 0; i < haveblocks; i++)
brelse(bhs[i]);
kfree(bhs); return stream.total_out;
}
/* * Uncompress data so that pages[full_page] is fully uptodate and possibly * fills in other pages if we have data for them.
*/ staticint 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; unsignedint header_size = ISOFS_I(inode)->i_format_parm[0]; unsignedint zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1]; unsignedint blockptr;
loff_t poffset = 0;
blkcnt_t cstart_block, cend_block; struct buffer_head *bh; unsignedint blkbits = ISOFS_BUFFER_BITS(inode); unsignedint 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_SIZE, inode->i_size);
/* 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);
/* * When decompressing, we typically obtain more than one page * per reference. We inject the additional pages into the page * cache as a form of readahead.
*/ staticint zisofs_read_folio(struct file *file, struct folio *folio)
{ struct inode *inode = file_inode(file); struct address_space *mapping = inode->i_mapping; int err; int i, pcount, full_page; unsignedint zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1]; unsignedint zisofs_pages_per_cblock =
PAGE_SHIFT <= zisofs_block_shift ?
(1 << (zisofs_block_shift - PAGE_SHIFT)) : 0; struct page **pages;
pgoff_t index = folio->index, end_index;
end_index = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT; /* * If this folio is wholly outside i_size we just return zero; * do_generic_file_read() will handle this for us
*/ if (index >= end_index) {
folio_end_read(folio, true); return 0;
}
if (PAGE_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 = kcalloc(max_t(unsignedint, zisofs_pages_per_cblock, 1), sizeof(*pages), GFP_KERNEL); if (!pages) {
folio_unlock(folio); return -ENOMEM;
}
pages[full_page] = &folio->page;
for (i = 0; i < pcount; i++, index++) { if (i != full_page)
pages[i] = grab_cache_page_nowait(mapping, index);
}
/* Release any residual pages, do not SetPageUptodate */ for (i = 0; i < pcount; i++) { if (pages[i]) {
flush_dcache_page(pages[i]);
unlock_page(pages[i]); if (i != full_page)
put_page(pages[i]);
}
}
/* At this point, err contains 0 or -EIO depending on the "critical" page */
kfree(pages); return err;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.