Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ff750311 authored by Phillip Lougher's avatar Phillip Lougher
Browse files

Squashfs: add compression options support to xz decompressor



Pass the dictionary size used to compress datablocks.  Using a
dictionary size less than the block size saves memory overhead, in many
cases without adversely affecting compression ratio.

Signed-off-by: default avatarPhillip Lougher <phillip@lougher.demon.co.uk>
parent b7fc0ff0
Loading
Loading
Loading
Loading
+41 −8
Original line number Original line Diff line number Diff line
@@ -26,6 +26,7 @@
#include <linux/buffer_head.h>
#include <linux/buffer_head.h>
#include <linux/slab.h>
#include <linux/slab.h>
#include <linux/xz.h>
#include <linux/xz.h>
#include <linux/bitops.h>


#include "squashfs_fs.h"
#include "squashfs_fs.h"
#include "squashfs_fs_sb.h"
#include "squashfs_fs_sb.h"
@@ -38,25 +39,57 @@ struct squashfs_xz {
	struct xz_buf buf;
	struct xz_buf buf;
};
};


struct comp_opts {
	__le32 dictionary_size;
	__le32 flags;
};

static void *squashfs_xz_init(struct squashfs_sb_info *msblk, void *buff,
static void *squashfs_xz_init(struct squashfs_sb_info *msblk, void *buff,
	int len)
	int len)
{
{
	int block_size = max_t(int, msblk->block_size, SQUASHFS_METADATA_SIZE);
	struct comp_opts *comp_opts = buff;
	struct squashfs_xz *stream;
	int dict_size = msblk->block_size;
	int err, n;

	if (comp_opts) {
		/* check compressor options are the expected length */
		if (len < sizeof(*comp_opts)) {
			err = -EIO;
			goto failed;
		}

		dict_size = le32_to_cpu(comp_opts->dictionary_size);

		/* the dictionary size should be 2^n or 2^n+2^(n+1) */
		n = ffs(dict_size) - 1;
		if (dict_size != (1 << n) && dict_size != (1 << n) +
						(1 << (n + 1))) {
			err = -EIO;
			goto failed;
		}
	}


	struct squashfs_xz *stream = kmalloc(sizeof(*stream), GFP_KERNEL);
	dict_size = max_t(int, dict_size, SQUASHFS_METADATA_SIZE);
	if (stream == NULL)

	stream = kmalloc(sizeof(*stream), GFP_KERNEL);
	if (stream == NULL) {
		err = -ENOMEM;
		goto failed;
		goto failed;
	}


	stream->state = xz_dec_init(XZ_PREALLOC, block_size);
	stream->state = xz_dec_init(XZ_PREALLOC, dict_size);
	if (stream->state == NULL)
	if (stream->state == NULL) {
		kfree(stream);
		err = -ENOMEM;
		goto failed;
		goto failed;
	}


	return stream;
	return stream;


failed:
failed:
	ERROR("Failed to allocate xz workspace\n");
	ERROR("Failed to initialise xz decompressor\n");
	kfree(stream);
	return ERR_PTR(err);
	return ERR_PTR(-ENOMEM);
}
}