]>
Commit | Line | Data |
---|---|---|
81bb8deb PL |
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@lougher.demon.co.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 | ||
30 | #include "squashfs_fs.h" | |
31 | #include "squashfs_fs_sb.h" | |
32 | #include "squashfs_fs_i.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 | static void *squashfs_xz_init(struct squashfs_sb_info *msblk) | |
42 | { | |
43 | int block_size = max_t(int, msblk->block_size, SQUASHFS_METADATA_SIZE); | |
44 | ||
45 | struct squashfs_xz *stream = kmalloc(sizeof(*stream), GFP_KERNEL); | |
46 | if (stream == NULL) | |
47 | goto failed; | |
48 | ||
49 | stream->state = xz_dec_init(XZ_PREALLOC, block_size); | |
50 | if (stream->state == NULL) | |
51 | goto failed; | |
52 | ||
53 | return stream; | |
54 | ||
55 | failed: | |
56 | ERROR("Failed to allocate xz workspace\n"); | |
57 | kfree(stream); | |
58 | return NULL; | |
59 | } | |
60 | ||
61 | ||
62 | static void squashfs_xz_free(void *strm) | |
63 | { | |
64 | struct squashfs_xz *stream = strm; | |
65 | ||
66 | if (stream) { | |
67 | xz_dec_end(stream->state); | |
68 | kfree(stream); | |
69 | } | |
70 | } | |
71 | ||
72 | ||
73 | static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void **buffer, | |
74 | struct buffer_head **bh, int b, int offset, int length, int srclength, | |
75 | int pages) | |
76 | { | |
77 | enum xz_ret xz_err; | |
78 | int avail, total = 0, k = 0, page = 0; | |
79 | struct squashfs_xz *stream = msblk->stream; | |
80 | ||
81 | mutex_lock(&msblk->read_data_mutex); | |
82 | ||
83 | xz_dec_reset(stream->state); | |
84 | stream->buf.in_pos = 0; | |
85 | stream->buf.in_size = 0; | |
86 | stream->buf.out_pos = 0; | |
87 | stream->buf.out_size = PAGE_CACHE_SIZE; | |
88 | stream->buf.out = buffer[page++]; | |
89 | ||
90 | do { | |
91 | if (stream->buf.in_pos == stream->buf.in_size && k < b) { | |
92 | avail = min(length, msblk->devblksize - offset); | |
93 | length -= avail; | |
94 | wait_on_buffer(bh[k]); | |
95 | if (!buffer_uptodate(bh[k])) | |
96 | goto release_mutex; | |
97 | ||
81bb8deb PL |
98 | stream->buf.in = bh[k]->b_data + offset; |
99 | stream->buf.in_size = avail; | |
100 | stream->buf.in_pos = 0; | |
101 | offset = 0; | |
102 | } | |
103 | ||
104 | if (stream->buf.out_pos == stream->buf.out_size | |
105 | && page < pages) { | |
106 | stream->buf.out = buffer[page++]; | |
107 | stream->buf.out_pos = 0; | |
108 | total += PAGE_CACHE_SIZE; | |
109 | } | |
110 | ||
111 | xz_err = xz_dec_run(stream->state, &stream->buf); | |
112 | ||
113 | if (stream->buf.in_pos == stream->buf.in_size && k < b) | |
114 | put_bh(bh[k++]); | |
115 | } while (xz_err == XZ_OK); | |
116 | ||
117 | if (xz_err != XZ_STREAM_END) { | |
118 | ERROR("xz_dec_run error, data probably corrupt\n"); | |
119 | goto release_mutex; | |
120 | } | |
121 | ||
122 | if (k < b) { | |
123 | ERROR("xz_uncompress error, input remaining\n"); | |
124 | goto release_mutex; | |
125 | } | |
126 | ||
127 | total += stream->buf.out_pos; | |
128 | mutex_unlock(&msblk->read_data_mutex); | |
129 | return total; | |
130 | ||
131 | release_mutex: | |
132 | mutex_unlock(&msblk->read_data_mutex); | |
133 | ||
134 | for (; k < b; k++) | |
135 | put_bh(bh[k]); | |
136 | ||
137 | return -EIO; | |
138 | } | |
139 | ||
140 | const struct squashfs_decompressor squashfs_xz_comp_ops = { | |
141 | .init = squashfs_xz_init, | |
142 | .free = squashfs_xz_free, | |
143 | .decompress = squashfs_xz_uncompress, | |
144 | .id = XZ_COMPRESSION, | |
145 | .name = "xz", | |
146 | .supported = 1 | |
147 | }; |