xz_wrapper.c 3.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5
/*
 * Squashfs - a compressed read only filesystem for Linux
 *
 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
P
Phillip Lougher 已提交
6
 * Phillip Lougher <phillip@squashfs.org.uk>
7 8 9 10 11 12
 *
 * xz_wrapper.c
 */


#include <linux/mutex.h>
13
#include <linux/bio.h>
14 15
#include <linux/slab.h>
#include <linux/xz.h>
16
#include <linux/bitops.h>
17 18 19 20 21

#include "squashfs_fs.h"
#include "squashfs_fs_sb.h"
#include "squashfs.h"
#include "decompressor.h"
22
#include "page_actor.h"
23 24 25 26 27 28

struct squashfs_xz {
	struct xz_dec *state;
	struct xz_buf buf;
};

29
struct disk_comp_opts {
30 31 32 33
	__le32 dictionary_size;
	__le32 flags;
};

34 35 36 37 38 39
struct comp_opts {
	int dict_size;
};

static void *squashfs_xz_comp_opts(struct squashfs_sb_info *msblk,
	void *buff, int len)
40
{
41 42 43 44 45 46 47 48 49
	struct disk_comp_opts *comp_opts = buff;
	struct comp_opts *opts;
	int err = 0, n;

	opts = kmalloc(sizeof(*opts), GFP_KERNEL);
	if (opts == NULL) {
		err = -ENOMEM;
		goto out2;
	}
50 51 52 53 54

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

58
		opts->dict_size = le32_to_cpu(comp_opts->dictionary_size);
59 60

		/* the dictionary size should be 2^n or 2^n+2^(n+1) */
61 62
		n = ffs(opts->dict_size) - 1;
		if (opts->dict_size != (1 << n) && opts->dict_size != (1 << n) +
63 64
						(1 << (n + 1))) {
			err = -EIO;
65
			goto out;
66
		}
67 68 69 70
	} else
		/* use defaults */
		opts->dict_size = max_t(int, msblk->block_size,
							SQUASHFS_METADATA_SIZE);
71

72 73 74 75 76 77 78 79 80 81 82 83 84 85
	return opts;

out:
	kfree(opts);
out2:
	return ERR_PTR(err);
}


static void *squashfs_xz_init(struct squashfs_sb_info *msblk, void *buff)
{
	struct comp_opts *comp_opts = buff;
	struct squashfs_xz *stream;
	int err;
86

87 88 89
	stream = kmalloc(sizeof(*stream), GFP_KERNEL);
	if (stream == NULL) {
		err = -ENOMEM;
90
		goto failed;
91
	}
92

93
	stream->state = xz_dec_init(XZ_PREALLOC, comp_opts->dict_size);
94 95 96
	if (stream->state == NULL) {
		kfree(stream);
		err = -ENOMEM;
97
		goto failed;
98
	}
99 100 101 102

	return stream;

failed:
103 104
	ERROR("Failed to initialise xz decompressor\n");
	return ERR_PTR(err);
105 106 107 108 109 110 111 112 113 114 115 116 117 118
}


static void squashfs_xz_free(void *strm)
{
	struct squashfs_xz *stream = strm;

	if (stream) {
		xz_dec_end(stream->state);
		kfree(stream);
	}
}


119
static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void *strm,
120
	struct bio *bio, int offset, int length,
121
	struct squashfs_page_actor *output)
122
{
123 124 125
	struct bvec_iter_all iter_all = {};
	struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
	int total = 0, error = 0;
126
	struct squashfs_xz *stream = strm;
127 128 129 130 131

	xz_dec_reset(stream->state);
	stream->buf.in_pos = 0;
	stream->buf.in_size = 0;
	stream->buf.out_pos = 0;
132
	stream->buf.out_size = PAGE_SIZE;
133
	stream->buf.out = squashfs_first_page(output);
134

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	for (;;) {
		enum xz_ret xz_err;

		if (stream->buf.in_pos == stream->buf.in_size) {
			const void *data;
			int avail;

			if (!bio_next_segment(bio, &iter_all)) {
				/* XZ_STREAM_END must be reached. */
				error = -EIO;
				break;
			}

			avail = min(length, ((int)bvec->bv_len) - offset);
			data = page_address(bvec->bv_page) + bvec->bv_offset;
150
			length -= avail;
151
			stream->buf.in = data + offset;
152 153 154 155 156
			stream->buf.in_size = avail;
			stream->buf.in_pos = 0;
			offset = 0;
		}

157 158 159 160
		if (stream->buf.out_pos == stream->buf.out_size) {
			stream->buf.out = squashfs_next_page(output);
			if (stream->buf.out != NULL) {
				stream->buf.out_pos = 0;
161
				total += PAGE_SIZE;
162
			}
163 164 165
		}

		xz_err = xz_dec_run(stream->state, &stream->buf);
166 167 168 169 170 171 172
		if (xz_err == XZ_STREAM_END)
			break;
		if (xz_err != XZ_OK) {
			error = -EIO;
			break;
		}
	}
173

174 175
	squashfs_finish_page(output);

176
	return error ? error : total + stream->buf.out_pos;
177 178 179 180
}

const struct squashfs_decompressor squashfs_xz_comp_ops = {
	.init = squashfs_xz_init,
181
	.comp_opts = squashfs_xz_comp_opts,
182 183 184 185 186 187
	.free = squashfs_xz_free,
	.decompress = squashfs_xz_uncompress,
	.id = XZ_COMPRESSION,
	.name = "xz",
	.supported = 1
};