xref: /openbmc/linux/fs/squashfs/block.c (revision 4c0f0bb2)
1e2780ab1SPhillip Lougher /*
2e2780ab1SPhillip Lougher  * Squashfs - a compressed read only filesystem for Linux
3e2780ab1SPhillip Lougher  *
4e2780ab1SPhillip Lougher  * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
5e2780ab1SPhillip Lougher  * Phillip Lougher <phillip@lougher.demon.co.uk>
6e2780ab1SPhillip Lougher  *
7e2780ab1SPhillip Lougher  * This program is free software; you can redistribute it and/or
8e2780ab1SPhillip Lougher  * modify it under the terms of the GNU General Public License
9e2780ab1SPhillip Lougher  * as published by the Free Software Foundation; either version 2,
10e2780ab1SPhillip Lougher  * or (at your option) any later version.
11e2780ab1SPhillip Lougher  *
12e2780ab1SPhillip Lougher  * This program is distributed in the hope that it will be useful,
13e2780ab1SPhillip Lougher  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14e2780ab1SPhillip Lougher  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15e2780ab1SPhillip Lougher  * GNU General Public License for more details.
16e2780ab1SPhillip Lougher  *
17e2780ab1SPhillip Lougher  * You should have received a copy of the GNU General Public License
18e2780ab1SPhillip Lougher  * along with this program; if not, write to the Free Software
19e2780ab1SPhillip Lougher  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20e2780ab1SPhillip Lougher  *
21e2780ab1SPhillip Lougher  * block.c
22e2780ab1SPhillip Lougher  */
23e2780ab1SPhillip Lougher 
24e2780ab1SPhillip Lougher /*
25e2780ab1SPhillip Lougher  * This file implements the low-level routines to read and decompress
26e2780ab1SPhillip Lougher  * datablocks and metadata blocks.
27e2780ab1SPhillip Lougher  */
28e2780ab1SPhillip Lougher 
29e2780ab1SPhillip Lougher #include <linux/fs.h>
30e2780ab1SPhillip Lougher #include <linux/vfs.h>
31e2780ab1SPhillip Lougher #include <linux/slab.h>
32e2780ab1SPhillip Lougher #include <linux/string.h>
33e2780ab1SPhillip Lougher #include <linux/buffer_head.h>
34e2780ab1SPhillip Lougher 
35e2780ab1SPhillip Lougher #include "squashfs_fs.h"
36e2780ab1SPhillip Lougher #include "squashfs_fs_sb.h"
37e2780ab1SPhillip Lougher #include "squashfs_fs_i.h"
38e2780ab1SPhillip Lougher #include "squashfs.h"
394c0f0bb2SPhillip Lougher #include "decompressor.h"
40e2780ab1SPhillip Lougher 
41e2780ab1SPhillip Lougher /*
42e2780ab1SPhillip Lougher  * Read the metadata block length, this is stored in the first two
43e2780ab1SPhillip Lougher  * bytes of the metadata block.
44e2780ab1SPhillip Lougher  */
45e2780ab1SPhillip Lougher static struct buffer_head *get_block_length(struct super_block *sb,
46e2780ab1SPhillip Lougher 			u64 *cur_index, int *offset, int *length)
47e2780ab1SPhillip Lougher {
48e2780ab1SPhillip Lougher 	struct squashfs_sb_info *msblk = sb->s_fs_info;
49e2780ab1SPhillip Lougher 	struct buffer_head *bh;
50e2780ab1SPhillip Lougher 
51e2780ab1SPhillip Lougher 	bh = sb_bread(sb, *cur_index);
52e2780ab1SPhillip Lougher 	if (bh == NULL)
53e2780ab1SPhillip Lougher 		return NULL;
54e2780ab1SPhillip Lougher 
55e2780ab1SPhillip Lougher 	if (msblk->devblksize - *offset == 1) {
56e2780ab1SPhillip Lougher 		*length = (unsigned char) bh->b_data[*offset];
57e2780ab1SPhillip Lougher 		put_bh(bh);
58e2780ab1SPhillip Lougher 		bh = sb_bread(sb, ++(*cur_index));
59e2780ab1SPhillip Lougher 		if (bh == NULL)
60e2780ab1SPhillip Lougher 			return NULL;
61e2780ab1SPhillip Lougher 		*length |= (unsigned char) bh->b_data[0] << 8;
62e2780ab1SPhillip Lougher 		*offset = 1;
63e2780ab1SPhillip Lougher 	} else {
64e2780ab1SPhillip Lougher 		*length = (unsigned char) bh->b_data[*offset] |
65e2780ab1SPhillip Lougher 			(unsigned char) bh->b_data[*offset + 1] << 8;
66e2780ab1SPhillip Lougher 		*offset += 2;
67e2780ab1SPhillip Lougher 	}
68e2780ab1SPhillip Lougher 
69e2780ab1SPhillip Lougher 	return bh;
70e2780ab1SPhillip Lougher }
71e2780ab1SPhillip Lougher 
72e2780ab1SPhillip Lougher 
73e2780ab1SPhillip Lougher /*
74e2780ab1SPhillip Lougher  * Read and decompress a metadata block or datablock.  Length is non-zero
75e2780ab1SPhillip Lougher  * if a datablock is being read (the size is stored elsewhere in the
76e2780ab1SPhillip Lougher  * filesystem), otherwise the length is obtained from the first two bytes of
77e2780ab1SPhillip Lougher  * the metadata block.  A bit in the length field indicates if the block
78e2780ab1SPhillip Lougher  * is stored uncompressed in the filesystem (usually because compression
79e2780ab1SPhillip Lougher  * generated a larger block - this does occasionally happen with zlib).
80e2780ab1SPhillip Lougher  */
81e2780ab1SPhillip Lougher int squashfs_read_data(struct super_block *sb, void **buffer, u64 index,
82118e1ef6SPhillip Lougher 			int length, u64 *next_index, int srclength, int pages)
83e2780ab1SPhillip Lougher {
84e2780ab1SPhillip Lougher 	struct squashfs_sb_info *msblk = sb->s_fs_info;
85e2780ab1SPhillip Lougher 	struct buffer_head **bh;
86e2780ab1SPhillip Lougher 	int offset = index & ((1 << msblk->devblksize_log2) - 1);
87e2780ab1SPhillip Lougher 	u64 cur_index = index >> msblk->devblksize_log2;
88e2780ab1SPhillip Lougher 	int bytes, compressed, b = 0, k = 0, page = 0, avail;
89e2780ab1SPhillip Lougher 
90e2780ab1SPhillip Lougher 
91e2780ab1SPhillip Lougher 	bh = kcalloc((msblk->block_size >> msblk->devblksize_log2) + 1,
92e2780ab1SPhillip Lougher 				sizeof(*bh), GFP_KERNEL);
93e2780ab1SPhillip Lougher 	if (bh == NULL)
94e2780ab1SPhillip Lougher 		return -ENOMEM;
95e2780ab1SPhillip Lougher 
96e2780ab1SPhillip Lougher 	if (length) {
97e2780ab1SPhillip Lougher 		/*
98e2780ab1SPhillip Lougher 		 * Datablock.
99e2780ab1SPhillip Lougher 		 */
100e2780ab1SPhillip Lougher 		bytes = -offset;
101e2780ab1SPhillip Lougher 		compressed = SQUASHFS_COMPRESSED_BLOCK(length);
102e2780ab1SPhillip Lougher 		length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length);
103e2780ab1SPhillip Lougher 		if (next_index)
104e2780ab1SPhillip Lougher 			*next_index = index + length;
105e2780ab1SPhillip Lougher 
106e2780ab1SPhillip Lougher 		TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n",
107e2780ab1SPhillip Lougher 			index, compressed ? "" : "un", length, srclength);
108e2780ab1SPhillip Lougher 
109e2780ab1SPhillip Lougher 		if (length < 0 || length > srclength ||
110e2780ab1SPhillip Lougher 				(index + length) > msblk->bytes_used)
111e2780ab1SPhillip Lougher 			goto read_failure;
112e2780ab1SPhillip Lougher 
113e2780ab1SPhillip Lougher 		for (b = 0; bytes < length; b++, cur_index++) {
114e2780ab1SPhillip Lougher 			bh[b] = sb_getblk(sb, cur_index);
115e2780ab1SPhillip Lougher 			if (bh[b] == NULL)
116e2780ab1SPhillip Lougher 				goto block_release;
117e2780ab1SPhillip Lougher 			bytes += msblk->devblksize;
118e2780ab1SPhillip Lougher 		}
119e2780ab1SPhillip Lougher 		ll_rw_block(READ, b, bh);
120e2780ab1SPhillip Lougher 	} else {
121e2780ab1SPhillip Lougher 		/*
122e2780ab1SPhillip Lougher 		 * Metadata block.
123e2780ab1SPhillip Lougher 		 */
124e2780ab1SPhillip Lougher 		if ((index + 2) > msblk->bytes_used)
125e2780ab1SPhillip Lougher 			goto read_failure;
126e2780ab1SPhillip Lougher 
127e2780ab1SPhillip Lougher 		bh[0] = get_block_length(sb, &cur_index, &offset, &length);
128e2780ab1SPhillip Lougher 		if (bh[0] == NULL)
129e2780ab1SPhillip Lougher 			goto read_failure;
130e2780ab1SPhillip Lougher 		b = 1;
131e2780ab1SPhillip Lougher 
132e2780ab1SPhillip Lougher 		bytes = msblk->devblksize - offset;
133e2780ab1SPhillip Lougher 		compressed = SQUASHFS_COMPRESSED(length);
134e2780ab1SPhillip Lougher 		length = SQUASHFS_COMPRESSED_SIZE(length);
135e2780ab1SPhillip Lougher 		if (next_index)
136e2780ab1SPhillip Lougher 			*next_index = index + length + 2;
137e2780ab1SPhillip Lougher 
138e2780ab1SPhillip Lougher 		TRACE("Block @ 0x%llx, %scompressed size %d\n", index,
139e2780ab1SPhillip Lougher 				compressed ? "" : "un", length);
140e2780ab1SPhillip Lougher 
141e2780ab1SPhillip Lougher 		if (length < 0 || length > srclength ||
142e2780ab1SPhillip Lougher 					(index + length) > msblk->bytes_used)
143e2780ab1SPhillip Lougher 			goto block_release;
144e2780ab1SPhillip Lougher 
145e2780ab1SPhillip Lougher 		for (; bytes < length; b++) {
146e2780ab1SPhillip Lougher 			bh[b] = sb_getblk(sb, ++cur_index);
147e2780ab1SPhillip Lougher 			if (bh[b] == NULL)
148e2780ab1SPhillip Lougher 				goto block_release;
149e2780ab1SPhillip Lougher 			bytes += msblk->devblksize;
150e2780ab1SPhillip Lougher 		}
151e2780ab1SPhillip Lougher 		ll_rw_block(READ, b - 1, bh + 1);
152e2780ab1SPhillip Lougher 	}
153e2780ab1SPhillip Lougher 
154e2780ab1SPhillip Lougher 	if (compressed) {
1554c0f0bb2SPhillip Lougher 		length = squashfs_decompress(msblk, buffer, bh, b, offset,
156e6a6d379SPhillip Lougher 			 length, srclength, pages);
157e6a6d379SPhillip Lougher 		if (length < 0)
158e6a6d379SPhillip Lougher 			goto read_failure;
159e2780ab1SPhillip Lougher 	} else {
160e2780ab1SPhillip Lougher 		/*
161e2780ab1SPhillip Lougher 		 * Block is uncompressed.
162e2780ab1SPhillip Lougher 		 */
163e2780ab1SPhillip Lougher 		int i, in, pg_offset = 0;
164e2780ab1SPhillip Lougher 
165e2780ab1SPhillip Lougher 		for (i = 0; i < b; i++) {
166e2780ab1SPhillip Lougher 			wait_on_buffer(bh[i]);
167e2780ab1SPhillip Lougher 			if (!buffer_uptodate(bh[i]))
168e2780ab1SPhillip Lougher 				goto block_release;
169e2780ab1SPhillip Lougher 		}
170e2780ab1SPhillip Lougher 
171e2780ab1SPhillip Lougher 		for (bytes = length; k < b; k++) {
172e2780ab1SPhillip Lougher 			in = min(bytes, msblk->devblksize - offset);
173e2780ab1SPhillip Lougher 			bytes -= in;
174e2780ab1SPhillip Lougher 			while (in) {
175e2780ab1SPhillip Lougher 				if (pg_offset == PAGE_CACHE_SIZE) {
176e2780ab1SPhillip Lougher 					page++;
177e2780ab1SPhillip Lougher 					pg_offset = 0;
178e2780ab1SPhillip Lougher 				}
179e2780ab1SPhillip Lougher 				avail = min_t(int, in, PAGE_CACHE_SIZE -
180e2780ab1SPhillip Lougher 						pg_offset);
181e2780ab1SPhillip Lougher 				memcpy(buffer[page] + pg_offset,
182e2780ab1SPhillip Lougher 						bh[k]->b_data + offset, avail);
183e2780ab1SPhillip Lougher 				in -= avail;
184e2780ab1SPhillip Lougher 				pg_offset += avail;
185e2780ab1SPhillip Lougher 				offset += avail;
186e2780ab1SPhillip Lougher 			}
187e2780ab1SPhillip Lougher 			offset = 0;
188e2780ab1SPhillip Lougher 			put_bh(bh[k]);
189e2780ab1SPhillip Lougher 		}
190e2780ab1SPhillip Lougher 	}
191e2780ab1SPhillip Lougher 
192e2780ab1SPhillip Lougher 	kfree(bh);
193e2780ab1SPhillip Lougher 	return length;
194e2780ab1SPhillip Lougher 
195e2780ab1SPhillip Lougher block_release:
196e2780ab1SPhillip Lougher 	for (; k < b; k++)
197e2780ab1SPhillip Lougher 		put_bh(bh[k]);
198e2780ab1SPhillip Lougher 
199e2780ab1SPhillip Lougher read_failure:
200118e1ef6SPhillip Lougher 	ERROR("squashfs_read_data failed to read block 0x%llx\n",
201118e1ef6SPhillip Lougher 					(unsigned long long) index);
202e2780ab1SPhillip Lougher 	kfree(bh);
203e2780ab1SPhillip Lougher 	return -EIO;
204e2780ab1SPhillip Lougher }
205