blob: 1cb0d81b164b5cf02ff67ceedc8275838b6db868 [file] [log] [blame]
Phillip Loughere2780ab2009-01-05 08:46:27 +00001/*
2 * Squashfs - a compressed read only filesystem for Linux
3 *
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
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 * block.c
22 */
23
24/*
25 * This file implements the low-level routines to read and decompress
26 * datablocks and metadata blocks.
27 */
28
29#include <linux/fs.h>
30#include <linux/vfs.h>
31#include <linux/slab.h>
Phillip Loughere2780ab2009-01-05 08:46:27 +000032#include <linux/string.h>
33#include <linux/buffer_head.h>
Phillip Loughere2780ab2009-01-05 08:46:27 +000034
35#include "squashfs_fs.h"
36#include "squashfs_fs_sb.h"
37#include "squashfs_fs_i.h"
38#include "squashfs.h"
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010039#include "decompressor.h"
Phillip Loughere2780ab2009-01-05 08:46:27 +000040
41/*
42 * Read the metadata block length, this is stored in the first two
43 * bytes of the metadata block.
44 */
45static struct buffer_head *get_block_length(struct super_block *sb,
46 u64 *cur_index, int *offset, int *length)
47{
48 struct squashfs_sb_info *msblk = sb->s_fs_info;
49 struct buffer_head *bh;
50
51 bh = sb_bread(sb, *cur_index);
52 if (bh == NULL)
53 return NULL;
54
55 if (msblk->devblksize - *offset == 1) {
56 *length = (unsigned char) bh->b_data[*offset];
57 put_bh(bh);
58 bh = sb_bread(sb, ++(*cur_index));
59 if (bh == NULL)
60 return NULL;
61 *length |= (unsigned char) bh->b_data[0] << 8;
62 *offset = 1;
63 } else {
64 *length = (unsigned char) bh->b_data[*offset] |
65 (unsigned char) bh->b_data[*offset + 1] << 8;
66 *offset += 2;
67 }
68
69 return bh;
70}
71
72
73/*
74 * Read and decompress a metadata block or datablock. Length is non-zero
75 * if a datablock is being read (the size is stored elsewhere in the
76 * filesystem), otherwise the length is obtained from the first two bytes of
77 * the metadata block. A bit in the length field indicates if the block
78 * is stored uncompressed in the filesystem (usually because compression
79 * generated a larger block - this does occasionally happen with zlib).
80 */
81int squashfs_read_data(struct super_block *sb, void **buffer, u64 index,
Phillip Lougher118e1ef2009-03-05 00:31:12 +000082 int length, u64 *next_index, int srclength, int pages)
Phillip Loughere2780ab2009-01-05 08:46:27 +000083{
84 struct squashfs_sb_info *msblk = sb->s_fs_info;
85 struct buffer_head **bh;
86 int offset = index & ((1 << msblk->devblksize_log2) - 1);
87 u64 cur_index = index >> msblk->devblksize_log2;
88 int bytes, compressed, b = 0, k = 0, page = 0, avail;
89
90
91 bh = kcalloc((msblk->block_size >> msblk->devblksize_log2) + 1,
92 sizeof(*bh), GFP_KERNEL);
93 if (bh == NULL)
94 return -ENOMEM;
95
96 if (length) {
97 /*
98 * Datablock.
99 */
100 bytes = -offset;
101 compressed = SQUASHFS_COMPRESSED_BLOCK(length);
102 length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length);
103 if (next_index)
104 *next_index = index + length;
105
106 TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n",
107 index, compressed ? "" : "un", length, srclength);
108
109 if (length < 0 || length > srclength ||
110 (index + length) > msblk->bytes_used)
111 goto read_failure;
112
113 for (b = 0; bytes < length; b++, cur_index++) {
114 bh[b] = sb_getblk(sb, cur_index);
115 if (bh[b] == NULL)
116 goto block_release;
117 bytes += msblk->devblksize;
118 }
119 ll_rw_block(READ, b, bh);
120 } else {
121 /*
122 * Metadata block.
123 */
124 if ((index + 2) > msblk->bytes_used)
125 goto read_failure;
126
127 bh[0] = get_block_length(sb, &cur_index, &offset, &length);
128 if (bh[0] == NULL)
129 goto read_failure;
130 b = 1;
131
132 bytes = msblk->devblksize - offset;
133 compressed = SQUASHFS_COMPRESSED(length);
134 length = SQUASHFS_COMPRESSED_SIZE(length);
135 if (next_index)
136 *next_index = index + length + 2;
137
138 TRACE("Block @ 0x%llx, %scompressed size %d\n", index,
139 compressed ? "" : "un", length);
140
141 if (length < 0 || length > srclength ||
142 (index + length) > msblk->bytes_used)
143 goto block_release;
144
145 for (; bytes < length; b++) {
146 bh[b] = sb_getblk(sb, ++cur_index);
147 if (bh[b] == NULL)
148 goto block_release;
149 bytes += msblk->devblksize;
150 }
151 ll_rw_block(READ, b - 1, bh + 1);
152 }
153
154 if (compressed) {
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +0100155 length = squashfs_decompress(msblk, buffer, bh, b, offset,
Phillip Loughere6a6d372009-09-22 19:25:24 +0100156 length, srclength, pages);
157 if (length < 0)
158 goto read_failure;
Phillip Loughere2780ab2009-01-05 08:46:27 +0000159 } else {
160 /*
161 * Block is uncompressed.
162 */
163 int i, in, pg_offset = 0;
164
165 for (i = 0; i < b; i++) {
166 wait_on_buffer(bh[i]);
167 if (!buffer_uptodate(bh[i]))
168 goto block_release;
169 }
170
171 for (bytes = length; k < b; k++) {
172 in = min(bytes, msblk->devblksize - offset);
173 bytes -= in;
174 while (in) {
175 if (pg_offset == PAGE_CACHE_SIZE) {
176 page++;
177 pg_offset = 0;
178 }
179 avail = min_t(int, in, PAGE_CACHE_SIZE -
180 pg_offset);
181 memcpy(buffer[page] + pg_offset,
182 bh[k]->b_data + offset, avail);
183 in -= avail;
184 pg_offset += avail;
185 offset += avail;
186 }
187 offset = 0;
188 put_bh(bh[k]);
189 }
190 }
191
192 kfree(bh);
193 return length;
194
Phillip Loughere2780ab2009-01-05 08:46:27 +0000195block_release:
196 for (; k < b; k++)
197 put_bh(bh[k]);
198
199read_failure:
Phillip Lougher118e1ef2009-03-05 00:31:12 +0000200 ERROR("squashfs_read_data failed to read block 0x%llx\n",
201 (unsigned long long) index);
Phillip Loughere2780ab2009-01-05 08:46:27 +0000202 kfree(bh);
203 return -EIO;
204}