xref: /openbmc/linux/fs/squashfs/xz_wrapper.c (revision 9508c6b9)
1 /*
2  * Squashfs - a compressed read only filesystem for Linux
3  *
4  * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5  * Phillip Lougher <phillip@squashfs.org.uk>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2,
10  * or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * xz_wrapper.c
22  */
23 
24 
25 #include <linux/mutex.h>
26 #include <linux/buffer_head.h>
27 #include <linux/slab.h>
28 #include <linux/xz.h>
29 #include <linux/bitops.h>
30 
31 #include "squashfs_fs.h"
32 #include "squashfs_fs_sb.h"
33 #include "squashfs.h"
34 #include "decompressor.h"
35 
36 struct squashfs_xz {
37 	struct xz_dec *state;
38 	struct xz_buf buf;
39 };
40 
41 struct disk_comp_opts {
42 	__le32 dictionary_size;
43 	__le32 flags;
44 };
45 
46 struct comp_opts {
47 	int dict_size;
48 };
49 
50 static void *squashfs_xz_comp_opts(struct squashfs_sb_info *msblk,
51 	void *buff, int len)
52 {
53 	struct disk_comp_opts *comp_opts = buff;
54 	struct comp_opts *opts;
55 	int err = 0, n;
56 
57 	opts = kmalloc(sizeof(*opts), GFP_KERNEL);
58 	if (opts == NULL) {
59 		err = -ENOMEM;
60 		goto out2;
61 	}
62 
63 	if (comp_opts) {
64 		/* check compressor options are the expected length */
65 		if (len < sizeof(*comp_opts)) {
66 			err = -EIO;
67 			goto out;
68 		}
69 
70 		opts->dict_size = le32_to_cpu(comp_opts->dictionary_size);
71 
72 		/* the dictionary size should be 2^n or 2^n+2^(n+1) */
73 		n = ffs(opts->dict_size) - 1;
74 		if (opts->dict_size != (1 << n) && opts->dict_size != (1 << n) +
75 						(1 << (n + 1))) {
76 			err = -EIO;
77 			goto out;
78 		}
79 	} else
80 		/* use defaults */
81 		opts->dict_size = max_t(int, msblk->block_size,
82 							SQUASHFS_METADATA_SIZE);
83 
84 	return opts;
85 
86 out:
87 	kfree(opts);
88 out2:
89 	return ERR_PTR(err);
90 }
91 
92 
93 static void *squashfs_xz_init(struct squashfs_sb_info *msblk, void *buff)
94 {
95 	struct comp_opts *comp_opts = buff;
96 	struct squashfs_xz *stream;
97 	int err;
98 
99 	stream = kmalloc(sizeof(*stream), GFP_KERNEL);
100 	if (stream == NULL) {
101 		err = -ENOMEM;
102 		goto failed;
103 	}
104 
105 	stream->state = xz_dec_init(XZ_PREALLOC, comp_opts->dict_size);
106 	if (stream->state == NULL) {
107 		kfree(stream);
108 		err = -ENOMEM;
109 		goto failed;
110 	}
111 
112 	return stream;
113 
114 failed:
115 	ERROR("Failed to initialise xz decompressor\n");
116 	return ERR_PTR(err);
117 }
118 
119 
120 static void squashfs_xz_free(void *strm)
121 {
122 	struct squashfs_xz *stream = strm;
123 
124 	if (stream) {
125 		xz_dec_end(stream->state);
126 		kfree(stream);
127 	}
128 }
129 
130 
131 static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void *strm,
132 	void **buffer, struct buffer_head **bh, int b, int offset, int length,
133 	int srclength, int pages)
134 {
135 	enum xz_ret xz_err;
136 	int avail, total = 0, k = 0, page = 0;
137 	struct squashfs_xz *stream = strm;
138 
139 	xz_dec_reset(stream->state);
140 	stream->buf.in_pos = 0;
141 	stream->buf.in_size = 0;
142 	stream->buf.out_pos = 0;
143 	stream->buf.out_size = PAGE_CACHE_SIZE;
144 	stream->buf.out = buffer[page++];
145 
146 	do {
147 		if (stream->buf.in_pos == stream->buf.in_size && k < b) {
148 			avail = min(length, msblk->devblksize - offset);
149 			length -= avail;
150 			stream->buf.in = bh[k]->b_data + offset;
151 			stream->buf.in_size = avail;
152 			stream->buf.in_pos = 0;
153 			offset = 0;
154 		}
155 
156 		if (stream->buf.out_pos == stream->buf.out_size
157 							&& page < pages) {
158 			stream->buf.out = buffer[page++];
159 			stream->buf.out_pos = 0;
160 			total += PAGE_CACHE_SIZE;
161 		}
162 
163 		xz_err = xz_dec_run(stream->state, &stream->buf);
164 
165 		if (stream->buf.in_pos == stream->buf.in_size && k < b)
166 			put_bh(bh[k++]);
167 	} while (xz_err == XZ_OK);
168 
169 	if (xz_err != XZ_STREAM_END || k < b)
170 		goto out;
171 
172 	return total + stream->buf.out_pos;
173 
174 out:
175 	for (; k < b; k++)
176 		put_bh(bh[k]);
177 
178 	return -EIO;
179 }
180 
181 const struct squashfs_decompressor squashfs_xz_comp_ops = {
182 	.init = squashfs_xz_init,
183 	.comp_opts = squashfs_xz_comp_opts,
184 	.free = squashfs_xz_free,
185 	.decompress = squashfs_xz_uncompress,
186 	.id = XZ_COMPRESSION,
187 	.name = "xz",
188 	.supported = 1
189 };
190