Skip to content

Commit

Permalink
Squashfs: Generalise paging handling in the decompressors
Browse files Browse the repository at this point in the history
Further generalise the decompressors by adding a page handler
abstraction.  This adds helpers to allow the decompressors
to access and process the output buffers in an implementation
independant manner.

This allows different types of output buffer to be passed
to the decompressors, with the implementation specific
aspects handled at decompression time, but without the
knowledge being held in the decompressor wrapper code.

This will allow the decompressors to handle Squashfs
cache buffers, and page cache pages.

This patch adds the abstraction and an implementation for
the caches.

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
Reviewed-by: Minchan Kim <minchan@kernel.org>
  • Loading branch information
Phillip Lougher committed Nov 20, 2013
1 parent d208383 commit 846b730
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 67 deletions.
27 changes: 15 additions & 12 deletions fs/squashfs/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "squashfs_fs_sb.h"
#include "squashfs.h"
#include "decompressor.h"
#include "page_actor.h"

/*
* Read the metadata block length, this is stored in the first two
Expand Down Expand Up @@ -86,16 +87,16 @@ static struct buffer_head *get_block_length(struct super_block *sb,
* generated a larger block - this does occasionally happen with compression
* algorithms).
*/
int squashfs_read_data(struct super_block *sb, void **buffer, u64 index,
int length, u64 *next_index, int srclength, int pages)
int squashfs_read_data(struct super_block *sb, u64 index, int length,
u64 *next_index, struct squashfs_page_actor *output)
{
struct squashfs_sb_info *msblk = sb->s_fs_info;
struct buffer_head **bh;
int offset = index & ((1 << msblk->devblksize_log2) - 1);
u64 cur_index = index >> msblk->devblksize_log2;
int bytes, compressed, b = 0, k = 0, page = 0, avail, i;
int bytes, compressed, b = 0, k = 0, avail, i;

bh = kcalloc(((srclength + msblk->devblksize - 1)
bh = kcalloc(((output->length + msblk->devblksize - 1)
>> msblk->devblksize_log2) + 1, sizeof(*bh), GFP_KERNEL);
if (bh == NULL)
return -ENOMEM;
Expand All @@ -111,9 +112,9 @@ int squashfs_read_data(struct super_block *sb, void **buffer, u64 index,
*next_index = index + length;

TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n",
index, compressed ? "" : "un", length, srclength);
index, compressed ? "" : "un", length, output->length);

if (length < 0 || length > srclength ||
if (length < 0 || length > output->length ||
(index + length) > msblk->bytes_used)
goto read_failure;

Expand Down Expand Up @@ -145,7 +146,7 @@ int squashfs_read_data(struct super_block *sb, void **buffer, u64 index,
TRACE("Block @ 0x%llx, %scompressed size %d\n", index,
compressed ? "" : "un", length);

if (length < 0 || length > srclength ||
if (length < 0 || length > output->length ||
(index + length) > msblk->bytes_used)
goto block_release;

Expand All @@ -165,35 +166,37 @@ int squashfs_read_data(struct super_block *sb, void **buffer, u64 index,
}

if (compressed) {
length = squashfs_decompress(msblk, buffer, bh, b, offset,
length, srclength, pages);
length = squashfs_decompress(msblk, bh, b, offset, length,
output);
if (length < 0)
goto read_failure;
} else {
/*
* Block is uncompressed.
*/
int in, pg_offset = 0;
void *data = squashfs_first_page(output);

for (bytes = length; k < b; k++) {
in = min(bytes, msblk->devblksize - offset);
bytes -= in;
while (in) {
if (pg_offset == PAGE_CACHE_SIZE) {
page++;
data = squashfs_next_page(output);
pg_offset = 0;
}
avail = min_t(int, in, PAGE_CACHE_SIZE -
pg_offset);
memcpy(buffer[page] + pg_offset,
bh[k]->b_data + offset, avail);
memcpy(data + pg_offset, bh[k]->b_data + offset,
avail);
in -= avail;
pg_offset += avail;
offset += avail;
}
offset = 0;
put_bh(bh[k]);
}
squashfs_finish_page(output);
}

kfree(bh);
Expand Down
28 changes: 23 additions & 5 deletions fs/squashfs/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include "squashfs_fs.h"
#include "squashfs_fs_sb.h"
#include "squashfs.h"
#include "page_actor.h"

/*
* Look-up block in cache, and increment usage count. If not in cache, read
Expand Down Expand Up @@ -119,9 +120,8 @@ struct squashfs_cache_entry *squashfs_cache_get(struct super_block *sb,
entry->error = 0;
spin_unlock(&cache->lock);

entry->length = squashfs_read_data(sb, entry->data,
block, length, &entry->next_index,
cache->block_size, cache->pages);
entry->length = squashfs_read_data(sb, block, length,
&entry->next_index, entry->actor);

spin_lock(&cache->lock);

Expand Down Expand Up @@ -220,6 +220,7 @@ void squashfs_cache_delete(struct squashfs_cache *cache)
kfree(cache->entry[i].data[j]);
kfree(cache->entry[i].data);
}
kfree(cache->entry[i].actor);
}

kfree(cache->entry);
Expand Down Expand Up @@ -280,6 +281,13 @@ struct squashfs_cache *squashfs_cache_init(char *name, int entries,
goto cleanup;
}
}

entry->actor = squashfs_page_actor_init(entry->data,
cache->pages, 0);
if (entry->actor == NULL) {
ERROR("Failed to allocate %s cache entry\n", name);
goto cleanup;
}
}

return cache;
Expand Down Expand Up @@ -410,6 +418,7 @@ void *squashfs_read_table(struct super_block *sb, u64 block, int length)
int pages = (length + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
int i, res;
void *table, *buffer, **data;
struct squashfs_page_actor *actor;

table = buffer = kmalloc(length, GFP_KERNEL);
if (table == NULL)
Expand All @@ -421,19 +430,28 @@ void *squashfs_read_table(struct super_block *sb, u64 block, int length)
goto failed;
}

actor = squashfs_page_actor_init(data, pages, length);
if (actor == NULL) {
res = -ENOMEM;
goto failed2;
}

for (i = 0; i < pages; i++, buffer += PAGE_CACHE_SIZE)
data[i] = buffer;

res = squashfs_read_data(sb, data, block, length |
SQUASHFS_COMPRESSED_BIT_BLOCK, NULL, length, pages);
res = squashfs_read_data(sb, block, length |
SQUASHFS_COMPRESSED_BIT_BLOCK, NULL, actor);

kfree(data);
kfree(actor);

if (res < 0)
goto failed;

return table;

failed2:
kfree(data);
failed:
kfree(table);
return ERR_PTR(res);
Expand Down
14 changes: 11 additions & 3 deletions fs/squashfs/decompressor.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "squashfs_fs_sb.h"
#include "decompressor.h"
#include "squashfs.h"
#include "page_actor.h"

/*
* This file (and decompressor.h) implements a decompressor framework for
Expand Down Expand Up @@ -87,6 +88,7 @@ static void *get_comp_opts(struct super_block *sb, unsigned short flags)
{
struct squashfs_sb_info *msblk = sb->s_fs_info;
void *buffer = NULL, *comp_opts;
struct squashfs_page_actor *actor = NULL;
int length = 0;

/*
Expand All @@ -99,9 +101,14 @@ static void *get_comp_opts(struct super_block *sb, unsigned short flags)
goto out;
}

length = squashfs_read_data(sb, &buffer,
sizeof(struct squashfs_super_block), 0, NULL,
PAGE_CACHE_SIZE, 1);
actor = squashfs_page_actor_init(&buffer, 1, 0);
if (actor == NULL) {
comp_opts = ERR_PTR(-ENOMEM);
goto out;
}

length = squashfs_read_data(sb,
sizeof(struct squashfs_super_block), 0, NULL, actor);

if (length < 0) {
comp_opts = ERR_PTR(length);
Expand All @@ -112,6 +119,7 @@ static void *get_comp_opts(struct super_block *sb, unsigned short flags)
comp_opts = squashfs_comp_opts(msblk, buffer, length);

out:
kfree(actor);
kfree(buffer);
return comp_opts;
}
Expand Down
5 changes: 3 additions & 2 deletions fs/squashfs/decompressor.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ struct squashfs_decompressor {
void *(*init)(struct squashfs_sb_info *, void *);
void *(*comp_opts)(struct squashfs_sb_info *, void *, int);
void (*free)(void *);
int (*decompress)(struct squashfs_sb_info *, void *, void **,
struct buffer_head **, int, int, int, int, int);
int (*decompress)(struct squashfs_sb_info *, void *,
struct buffer_head **, int, int, int,
struct squashfs_page_actor *);
int id;
char *name;
int supported;
Expand Down
7 changes: 3 additions & 4 deletions fs/squashfs/decompressor_multi.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,14 @@ static struct decomp_stream *get_decomp_stream(struct squashfs_sb_info *msblk,
}


int squashfs_decompress(struct squashfs_sb_info *msblk,
void **buffer, struct buffer_head **bh, int b, int offset, int length,
int srclength, int pages)
int squashfs_decompress(struct squashfs_sb_info *msblk, struct buffer_head **bh,
int b, int offset, int length, struct squashfs_page_actor *output)
{
int res;
struct squashfs_stream *stream = msblk->stream;
struct decomp_stream *decomp_stream = get_decomp_stream(msblk, stream);
res = msblk->decompressor->decompress(msblk, decomp_stream->stream,
buffer, bh, b, offset, length, srclength, pages);
bh, b, offset, length, output);
put_decomp_stream(decomp_stream, stream);
if (res < 0)
ERROR("%s decompression failed, data probably corrupt\n",
Expand Down
9 changes: 4 additions & 5 deletions fs/squashfs/decompressor_multi_percpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,14 @@ void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk)
}
}

int squashfs_decompress(struct squashfs_sb_info *msblk,
void **buffer, struct buffer_head **bh, int b, int offset, int length,
int srclength, int pages)
int squashfs_decompress(struct squashfs_sb_info *msblk, struct buffer_head **bh,
int b, int offset, int length, struct squashfs_page_actor *output)
{
struct squashfs_stream __percpu *percpu =
(struct squashfs_stream __percpu *) msblk->stream;
struct squashfs_stream *stream = get_cpu_ptr(percpu);
int res = msblk->decompressor->decompress(msblk, stream->stream, buffer,
bh, b, offset, length, srclength, pages);
int res = msblk->decompressor->decompress(msblk, stream->stream, bh, b,
offset, length, output);
put_cpu_ptr(stream);

if (res < 0)
Expand Down
9 changes: 4 additions & 5 deletions fs/squashfs/decompressor_single.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,15 @@ void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk)
}
}

int squashfs_decompress(struct squashfs_sb_info *msblk,
void **buffer, struct buffer_head **bh, int b, int offset, int length,
int srclength, int pages)
int squashfs_decompress(struct squashfs_sb_info *msblk, struct buffer_head **bh,
int b, int offset, int length, struct squashfs_page_actor *output)
{
int res;
struct squashfs_stream *stream = msblk->stream;

mutex_lock(&stream->mutex);
res = msblk->decompressor->decompress(msblk, stream->stream, buffer,
bh, b, offset, length, srclength, pages);
res = msblk->decompressor->decompress(msblk, stream->stream, bh, b,
offset, length, output);
mutex_unlock(&stream->mutex);

if (res < 0)
Expand Down
27 changes: 18 additions & 9 deletions fs/squashfs/lzo_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "squashfs_fs_sb.h"
#include "squashfs.h"
#include "decompressor.h"
#include "page_actor.h"

struct squashfs_lzo {
void *input;
Expand Down Expand Up @@ -75,13 +76,13 @@ static void lzo_free(void *strm)


static int lzo_uncompress(struct squashfs_sb_info *msblk, void *strm,
void **buffer, struct buffer_head **bh, int b, int offset, int length,
int srclength, int pages)
struct buffer_head **bh, int b, int offset, int length,
struct squashfs_page_actor *output)
{
struct squashfs_lzo *stream = strm;
void *buff = stream->input;
void *buff = stream->input, *data;
int avail, i, bytes = length, res;
size_t out_len = srclength;
size_t out_len = output->length;

for (i = 0; i < b; i++) {
avail = min(bytes, msblk->devblksize - offset);
Expand All @@ -98,12 +99,20 @@ static int lzo_uncompress(struct squashfs_sb_info *msblk, void *strm,
goto failed;

res = bytes = (int)out_len;
for (i = 0, buff = stream->output; bytes && i < pages; i++) {
avail = min_t(int, bytes, PAGE_CACHE_SIZE);
memcpy(buffer[i], buff, avail);
buff += avail;
bytes -= avail;
data = squashfs_first_page(output);
buff = stream->output;
while (data) {
if (bytes <= PAGE_CACHE_SIZE) {
memcpy(data, buff, bytes);
break;
} else {
memcpy(data, buff, PAGE_CACHE_SIZE);
buff += PAGE_CACHE_SIZE;
bytes -= PAGE_CACHE_SIZE;
data = squashfs_next_page(output);
}
}
squashfs_finish_page(output);

return res;

Expand Down
49 changes: 49 additions & 0 deletions fs/squashfs/page_actor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef PAGE_ACTOR_H
#define PAGE_ACTOR_H
/*
* Copyright (c) 2013
* Phillip Lougher <phillip@squashfs.org.uk>
*
* This work is licensed under the terms of the GNU GPL, version 2. See
* the COPYING file in the top-level directory.
*/

struct squashfs_page_actor {
void **page;
int pages;
int length;
int next_page;
};

static inline struct squashfs_page_actor *squashfs_page_actor_init(void **page,
int pages, int length)
{
struct squashfs_page_actor *actor = kmalloc(sizeof(*actor), GFP_KERNEL);

if (actor == NULL)
return NULL;

actor->length = length ? : pages * PAGE_CACHE_SIZE;
actor->page = page;
actor->pages = pages;
actor->next_page = 0;
return actor;
}

static inline void *squashfs_first_page(struct squashfs_page_actor *actor)
{
actor->next_page = 1;
return actor->page[0];
}

static inline void *squashfs_next_page(struct squashfs_page_actor *actor)
{
return actor->next_page == actor->pages ? NULL :
actor->page[actor->next_page++];
}

static inline void squashfs_finish_page(struct squashfs_page_actor *actor)
{
/* empty */
}
#endif
Loading

0 comments on commit 846b730

Please sign in to comment.