Commit 78809913 authored by Sergey Senozhatsky's avatar Sergey Senozhatsky Committed by Chris Mason

btrfs compression: merge inflate and deflate z_streams

`struct workspace' used for zlib compression contains two zlib
z_stream-s: `def_strm' used in zlib_compress_pages(), and `inf_strm'
used in zlib_decompress/zlib_decompress_biovec(). None of these
functions use `inf_strm' and `def_strm' simultaniously, meaning that
for every compress/decompress operation we need only one z_stream
(out of two available).

`inf_strm' and `def_strm' are different in size of ->workspace. For
inflate stream we vmalloc() zlib_inflate_workspacesize() bytes, for
deflate stream - zlib_deflate_workspacesize() bytes. On my system zlib
returns the following workspace sizes, correspondingly: 42312 and 268104
(+ guard pages).

Keep only one `z_stream' in `struct workspace' and use it for both
compression and decompression. Hence, instead of vmalloc() of two
z_stream->worskpace-s, allocate only one of size:
	max(zlib_deflate_workspacesize(), zlib_inflate_workspacesize())
Reviewed-by: default avatarDavid Sterba <dsterba@suse.cz>
Signed-off-by: default avatarSergey Senozhatsky <sergey.senozhatsky@gmail.com>
Signed-off-by: default avatarChris Mason <clm@fb.com>
parent 555e1286
...@@ -33,8 +33,7 @@ ...@@ -33,8 +33,7 @@
#include "compression.h" #include "compression.h"
struct workspace { struct workspace {
z_stream inf_strm; z_stream strm;
z_stream def_strm;
char *buf; char *buf;
struct list_head list; struct list_head list;
}; };
...@@ -43,8 +42,7 @@ static void zlib_free_workspace(struct list_head *ws) ...@@ -43,8 +42,7 @@ static void zlib_free_workspace(struct list_head *ws)
{ {
struct workspace *workspace = list_entry(ws, struct workspace, list); struct workspace *workspace = list_entry(ws, struct workspace, list);
vfree(workspace->def_strm.workspace); vfree(workspace->strm.workspace);
vfree(workspace->inf_strm.workspace);
kfree(workspace->buf); kfree(workspace->buf);
kfree(workspace); kfree(workspace);
} }
...@@ -52,17 +50,17 @@ static void zlib_free_workspace(struct list_head *ws) ...@@ -52,17 +50,17 @@ static void zlib_free_workspace(struct list_head *ws)
static struct list_head *zlib_alloc_workspace(void) static struct list_head *zlib_alloc_workspace(void)
{ {
struct workspace *workspace; struct workspace *workspace;
int workspacesize;
workspace = kzalloc(sizeof(*workspace), GFP_NOFS); workspace = kzalloc(sizeof(*workspace), GFP_NOFS);
if (!workspace) if (!workspace)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
workspace->def_strm.workspace = vmalloc(zlib_deflate_workspacesize( workspacesize = max(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
MAX_WBITS, MAX_MEM_LEVEL)); zlib_inflate_workspacesize());
workspace->inf_strm.workspace = vmalloc(zlib_inflate_workspacesize()); workspace->strm.workspace = vmalloc(workspacesize);
workspace->buf = kmalloc(PAGE_CACHE_SIZE, GFP_NOFS); workspace->buf = kmalloc(PAGE_CACHE_SIZE, GFP_NOFS);
if (!workspace->def_strm.workspace || if (!workspace->strm.workspace || !workspace->buf)
!workspace->inf_strm.workspace || !workspace->buf)
goto fail; goto fail;
INIT_LIST_HEAD(&workspace->list); INIT_LIST_HEAD(&workspace->list);
...@@ -96,14 +94,14 @@ static int zlib_compress_pages(struct list_head *ws, ...@@ -96,14 +94,14 @@ static int zlib_compress_pages(struct list_head *ws,
*total_out = 0; *total_out = 0;
*total_in = 0; *total_in = 0;
if (Z_OK != zlib_deflateInit(&workspace->def_strm, 3)) { if (Z_OK != zlib_deflateInit(&workspace->strm, 3)) {
printk(KERN_WARNING "BTRFS: deflateInit failed\n"); printk(KERN_WARNING "BTRFS: deflateInit failed\n");
ret = -EIO; ret = -EIO;
goto out; goto out;
} }
workspace->def_strm.total_in = 0; workspace->strm.total_in = 0;
workspace->def_strm.total_out = 0; workspace->strm.total_out = 0;
in_page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT); in_page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
data_in = kmap(in_page); data_in = kmap(in_page);
...@@ -117,25 +115,25 @@ static int zlib_compress_pages(struct list_head *ws, ...@@ -117,25 +115,25 @@ static int zlib_compress_pages(struct list_head *ws,
pages[0] = out_page; pages[0] = out_page;
nr_pages = 1; nr_pages = 1;
workspace->def_strm.next_in = data_in; workspace->strm.next_in = data_in;
workspace->def_strm.next_out = cpage_out; workspace->strm.next_out = cpage_out;
workspace->def_strm.avail_out = PAGE_CACHE_SIZE; workspace->strm.avail_out = PAGE_CACHE_SIZE;
workspace->def_strm.avail_in = min(len, PAGE_CACHE_SIZE); workspace->strm.avail_in = min(len, PAGE_CACHE_SIZE);
while (workspace->def_strm.total_in < len) { while (workspace->strm.total_in < len) {
ret = zlib_deflate(&workspace->def_strm, Z_SYNC_FLUSH); ret = zlib_deflate(&workspace->strm, Z_SYNC_FLUSH);
if (ret != Z_OK) { if (ret != Z_OK) {
printk(KERN_DEBUG "BTRFS: deflate in loop returned %d\n", printk(KERN_DEBUG "BTRFS: deflate in loop returned %d\n",
ret); ret);
zlib_deflateEnd(&workspace->def_strm); zlib_deflateEnd(&workspace->strm);
ret = -EIO; ret = -EIO;
goto out; goto out;
} }
/* we're making it bigger, give up */ /* we're making it bigger, give up */
if (workspace->def_strm.total_in > 8192 && if (workspace->strm.total_in > 8192 &&
workspace->def_strm.total_in < workspace->strm.total_in <
workspace->def_strm.total_out) { workspace->strm.total_out) {
ret = -E2BIG; ret = -E2BIG;
goto out; goto out;
} }
...@@ -143,7 +141,7 @@ static int zlib_compress_pages(struct list_head *ws, ...@@ -143,7 +141,7 @@ static int zlib_compress_pages(struct list_head *ws,
* before the total_in so we will pull in a new page for * before the total_in so we will pull in a new page for
* the stream end if required * the stream end if required
*/ */
if (workspace->def_strm.avail_out == 0) { if (workspace->strm.avail_out == 0) {
kunmap(out_page); kunmap(out_page);
if (nr_pages == nr_dest_pages) { if (nr_pages == nr_dest_pages) {
out_page = NULL; out_page = NULL;
...@@ -158,19 +156,19 @@ static int zlib_compress_pages(struct list_head *ws, ...@@ -158,19 +156,19 @@ static int zlib_compress_pages(struct list_head *ws,
cpage_out = kmap(out_page); cpage_out = kmap(out_page);
pages[nr_pages] = out_page; pages[nr_pages] = out_page;
nr_pages++; nr_pages++;
workspace->def_strm.avail_out = PAGE_CACHE_SIZE; workspace->strm.avail_out = PAGE_CACHE_SIZE;
workspace->def_strm.next_out = cpage_out; workspace->strm.next_out = cpage_out;
} }
/* we're all done */ /* we're all done */
if (workspace->def_strm.total_in >= len) if (workspace->strm.total_in >= len)
break; break;
/* we've read in a full page, get a new one */ /* we've read in a full page, get a new one */
if (workspace->def_strm.avail_in == 0) { if (workspace->strm.avail_in == 0) {
if (workspace->def_strm.total_out > max_out) if (workspace->strm.total_out > max_out)
break; break;
bytes_left = len - workspace->def_strm.total_in; bytes_left = len - workspace->strm.total_in;
kunmap(in_page); kunmap(in_page);
page_cache_release(in_page); page_cache_release(in_page);
...@@ -178,28 +176,28 @@ static int zlib_compress_pages(struct list_head *ws, ...@@ -178,28 +176,28 @@ static int zlib_compress_pages(struct list_head *ws,
in_page = find_get_page(mapping, in_page = find_get_page(mapping,
start >> PAGE_CACHE_SHIFT); start >> PAGE_CACHE_SHIFT);
data_in = kmap(in_page); data_in = kmap(in_page);
workspace->def_strm.avail_in = min(bytes_left, workspace->strm.avail_in = min(bytes_left,
PAGE_CACHE_SIZE); PAGE_CACHE_SIZE);
workspace->def_strm.next_in = data_in; workspace->strm.next_in = data_in;
} }
} }
workspace->def_strm.avail_in = 0; workspace->strm.avail_in = 0;
ret = zlib_deflate(&workspace->def_strm, Z_FINISH); ret = zlib_deflate(&workspace->strm, Z_FINISH);
zlib_deflateEnd(&workspace->def_strm); zlib_deflateEnd(&workspace->strm);
if (ret != Z_STREAM_END) { if (ret != Z_STREAM_END) {
ret = -EIO; ret = -EIO;
goto out; goto out;
} }
if (workspace->def_strm.total_out >= workspace->def_strm.total_in) { if (workspace->strm.total_out >= workspace->strm.total_in) {
ret = -E2BIG; ret = -E2BIG;
goto out; goto out;
} }
ret = 0; ret = 0;
*total_out = workspace->def_strm.total_out; *total_out = workspace->strm.total_out;
*total_in = workspace->def_strm.total_in; *total_in = workspace->strm.total_in;
out: out:
*out_pages = nr_pages; *out_pages = nr_pages;
if (out_page) if (out_page)
...@@ -230,13 +228,13 @@ static int zlib_decompress_biovec(struct list_head *ws, struct page **pages_in, ...@@ -230,13 +228,13 @@ static int zlib_decompress_biovec(struct list_head *ws, struct page **pages_in,
unsigned long pg_offset; unsigned long pg_offset;
data_in = kmap(pages_in[page_in_index]); data_in = kmap(pages_in[page_in_index]);
workspace->inf_strm.next_in = data_in; workspace->strm.next_in = data_in;
workspace->inf_strm.avail_in = min_t(size_t, srclen, PAGE_CACHE_SIZE); workspace->strm.avail_in = min_t(size_t, srclen, PAGE_CACHE_SIZE);
workspace->inf_strm.total_in = 0; workspace->strm.total_in = 0;
workspace->inf_strm.total_out = 0; workspace->strm.total_out = 0;
workspace->inf_strm.next_out = workspace->buf; workspace->strm.next_out = workspace->buf;
workspace->inf_strm.avail_out = PAGE_CACHE_SIZE; workspace->strm.avail_out = PAGE_CACHE_SIZE;
pg_offset = 0; pg_offset = 0;
/* If it's deflate, and it's got no preset dictionary, then /* If it's deflate, and it's got no preset dictionary, then
...@@ -246,21 +244,21 @@ static int zlib_decompress_biovec(struct list_head *ws, struct page **pages_in, ...@@ -246,21 +244,21 @@ static int zlib_decompress_biovec(struct list_head *ws, struct page **pages_in,
!(((data_in[0]<<8) + data_in[1]) % 31)) { !(((data_in[0]<<8) + data_in[1]) % 31)) {
wbits = -((data_in[0] >> 4) + 8); wbits = -((data_in[0] >> 4) + 8);
workspace->inf_strm.next_in += 2; workspace->strm.next_in += 2;
workspace->inf_strm.avail_in -= 2; workspace->strm.avail_in -= 2;
} }
if (Z_OK != zlib_inflateInit2(&workspace->inf_strm, wbits)) { if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) {
printk(KERN_WARNING "BTRFS: inflateInit failed\n"); printk(KERN_WARNING "BTRFS: inflateInit failed\n");
return -EIO; return -EIO;
} }
while (workspace->inf_strm.total_in < srclen) { while (workspace->strm.total_in < srclen) {
ret = zlib_inflate(&workspace->inf_strm, Z_NO_FLUSH); ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH);
if (ret != Z_OK && ret != Z_STREAM_END) if (ret != Z_OK && ret != Z_STREAM_END)
break; break;
buf_start = total_out; buf_start = total_out;
total_out = workspace->inf_strm.total_out; total_out = workspace->strm.total_out;
/* we didn't make progress in this inflate call, we're done */ /* we didn't make progress in this inflate call, we're done */
if (buf_start == total_out) if (buf_start == total_out)
...@@ -275,10 +273,10 @@ static int zlib_decompress_biovec(struct list_head *ws, struct page **pages_in, ...@@ -275,10 +273,10 @@ static int zlib_decompress_biovec(struct list_head *ws, struct page **pages_in,
goto done; goto done;
} }
workspace->inf_strm.next_out = workspace->buf; workspace->strm.next_out = workspace->buf;
workspace->inf_strm.avail_out = PAGE_CACHE_SIZE; workspace->strm.avail_out = PAGE_CACHE_SIZE;
if (workspace->inf_strm.avail_in == 0) { if (workspace->strm.avail_in == 0) {
unsigned long tmp; unsigned long tmp;
kunmap(pages_in[page_in_index]); kunmap(pages_in[page_in_index]);
page_in_index++; page_in_index++;
...@@ -287,9 +285,9 @@ static int zlib_decompress_biovec(struct list_head *ws, struct page **pages_in, ...@@ -287,9 +285,9 @@ static int zlib_decompress_biovec(struct list_head *ws, struct page **pages_in,
break; break;
} }
data_in = kmap(pages_in[page_in_index]); data_in = kmap(pages_in[page_in_index]);
workspace->inf_strm.next_in = data_in; workspace->strm.next_in = data_in;
tmp = srclen - workspace->inf_strm.total_in; tmp = srclen - workspace->strm.total_in;
workspace->inf_strm.avail_in = min(tmp, workspace->strm.avail_in = min(tmp,
PAGE_CACHE_SIZE); PAGE_CACHE_SIZE);
} }
} }
...@@ -298,7 +296,7 @@ static int zlib_decompress_biovec(struct list_head *ws, struct page **pages_in, ...@@ -298,7 +296,7 @@ static int zlib_decompress_biovec(struct list_head *ws, struct page **pages_in,
else else
ret = 0; ret = 0;
done: done:
zlib_inflateEnd(&workspace->inf_strm); zlib_inflateEnd(&workspace->strm);
if (data_in) if (data_in)
kunmap(pages_in[page_in_index]); kunmap(pages_in[page_in_index]);
return ret; return ret;
...@@ -316,13 +314,13 @@ static int zlib_decompress(struct list_head *ws, unsigned char *data_in, ...@@ -316,13 +314,13 @@ static int zlib_decompress(struct list_head *ws, unsigned char *data_in,
unsigned long total_out = 0; unsigned long total_out = 0;
char *kaddr; char *kaddr;
workspace->inf_strm.next_in = data_in; workspace->strm.next_in = data_in;
workspace->inf_strm.avail_in = srclen; workspace->strm.avail_in = srclen;
workspace->inf_strm.total_in = 0; workspace->strm.total_in = 0;
workspace->inf_strm.next_out = workspace->buf; workspace->strm.next_out = workspace->buf;
workspace->inf_strm.avail_out = PAGE_CACHE_SIZE; workspace->strm.avail_out = PAGE_CACHE_SIZE;
workspace->inf_strm.total_out = 0; workspace->strm.total_out = 0;
/* If it's deflate, and it's got no preset dictionary, then /* If it's deflate, and it's got no preset dictionary, then
we can tell zlib to skip the adler32 check. */ we can tell zlib to skip the adler32 check. */
if (srclen > 2 && !(data_in[1] & PRESET_DICT) && if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
...@@ -330,11 +328,11 @@ static int zlib_decompress(struct list_head *ws, unsigned char *data_in, ...@@ -330,11 +328,11 @@ static int zlib_decompress(struct list_head *ws, unsigned char *data_in,
!(((data_in[0]<<8) + data_in[1]) % 31)) { !(((data_in[0]<<8) + data_in[1]) % 31)) {
wbits = -((data_in[0] >> 4) + 8); wbits = -((data_in[0] >> 4) + 8);
workspace->inf_strm.next_in += 2; workspace->strm.next_in += 2;
workspace->inf_strm.avail_in -= 2; workspace->strm.avail_in -= 2;
} }
if (Z_OK != zlib_inflateInit2(&workspace->inf_strm, wbits)) { if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) {
printk(KERN_WARNING "BTRFS: inflateInit failed\n"); printk(KERN_WARNING "BTRFS: inflateInit failed\n");
return -EIO; return -EIO;
} }
...@@ -345,12 +343,12 @@ static int zlib_decompress(struct list_head *ws, unsigned char *data_in, ...@@ -345,12 +343,12 @@ static int zlib_decompress(struct list_head *ws, unsigned char *data_in,
unsigned long bytes; unsigned long bytes;
unsigned long pg_offset = 0; unsigned long pg_offset = 0;
ret = zlib_inflate(&workspace->inf_strm, Z_NO_FLUSH); ret = zlib_inflate(&workspace->strm, Z_NO_FLUSH);
if (ret != Z_OK && ret != Z_STREAM_END) if (ret != Z_OK && ret != Z_STREAM_END)
break; break;
buf_start = total_out; buf_start = total_out;
total_out = workspace->inf_strm.total_out; total_out = workspace->strm.total_out;
if (total_out == buf_start) { if (total_out == buf_start) {
ret = -EIO; ret = -EIO;
...@@ -376,8 +374,8 @@ static int zlib_decompress(struct list_head *ws, unsigned char *data_in, ...@@ -376,8 +374,8 @@ static int zlib_decompress(struct list_head *ws, unsigned char *data_in,
pg_offset += bytes; pg_offset += bytes;
bytes_left -= bytes; bytes_left -= bytes;
next: next:
workspace->inf_strm.next_out = workspace->buf; workspace->strm.next_out = workspace->buf;
workspace->inf_strm.avail_out = PAGE_CACHE_SIZE; workspace->strm.avail_out = PAGE_CACHE_SIZE;
} }
if (ret != Z_STREAM_END && bytes_left != 0) if (ret != Z_STREAM_END && bytes_left != 0)
...@@ -385,7 +383,7 @@ static int zlib_decompress(struct list_head *ws, unsigned char *data_in, ...@@ -385,7 +383,7 @@ static int zlib_decompress(struct list_head *ws, unsigned char *data_in,
else else
ret = 0; ret = 0;
zlib_inflateEnd(&workspace->inf_strm); zlib_inflateEnd(&workspace->strm);
return ret; return ret;
} }
......
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