xref: /openbmc/linux/fs/squashfs/block.c (revision 846b730e)
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
5d7f2ff67SPhillip Lougher  * Phillip Lougher <phillip@squashfs.org.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.h"
384c0f0bb2SPhillip Lougher #include "decompressor.h"
39846b730eSPhillip Lougher #include "page_actor.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;
673689456bSPhillip Lougher 
683689456bSPhillip Lougher 		if (*offset == msblk->devblksize) {
693689456bSPhillip Lougher 			put_bh(bh);
703689456bSPhillip Lougher 			bh = sb_bread(sb, ++(*cur_index));
713689456bSPhillip Lougher 			if (bh == NULL)
723689456bSPhillip Lougher 				return NULL;
733689456bSPhillip Lougher 			*offset = 0;
743689456bSPhillip Lougher 		}
75e2780ab1SPhillip Lougher 	}
76e2780ab1SPhillip Lougher 
77e2780ab1SPhillip Lougher 	return bh;
78e2780ab1SPhillip Lougher }
79e2780ab1SPhillip Lougher 
80e2780ab1SPhillip Lougher 
81e2780ab1SPhillip Lougher /*
82e2780ab1SPhillip Lougher  * Read and decompress a metadata block or datablock.  Length is non-zero
83e2780ab1SPhillip Lougher  * if a datablock is being read (the size is stored elsewhere in the
84e2780ab1SPhillip Lougher  * filesystem), otherwise the length is obtained from the first two bytes of
85e2780ab1SPhillip Lougher  * the metadata block.  A bit in the length field indicates if the block
86e2780ab1SPhillip Lougher  * is stored uncompressed in the filesystem (usually because compression
87ec9267b6SPhillip Lougher  * generated a larger block - this does occasionally happen with compression
88ec9267b6SPhillip Lougher  * algorithms).
89e2780ab1SPhillip Lougher  */
90846b730eSPhillip Lougher int squashfs_read_data(struct super_block *sb, u64 index, int length,
91846b730eSPhillip Lougher 		u64 *next_index, struct squashfs_page_actor *output)
92e2780ab1SPhillip Lougher {
93e2780ab1SPhillip Lougher 	struct squashfs_sb_info *msblk = sb->s_fs_info;
94e2780ab1SPhillip Lougher 	struct buffer_head **bh;
95e2780ab1SPhillip Lougher 	int offset = index & ((1 << msblk->devblksize_log2) - 1);
96e2780ab1SPhillip Lougher 	u64 cur_index = index >> msblk->devblksize_log2;
97846b730eSPhillip Lougher 	int bytes, compressed, b = 0, k = 0, avail, i;
98e2780ab1SPhillip Lougher 
99846b730eSPhillip Lougher 	bh = kcalloc(((output->length + msblk->devblksize - 1)
100e0d1f700SPhillip Lougher 		>> msblk->devblksize_log2) + 1, sizeof(*bh), GFP_KERNEL);
101e2780ab1SPhillip Lougher 	if (bh == NULL)
102e2780ab1SPhillip Lougher 		return -ENOMEM;
103e2780ab1SPhillip Lougher 
104e2780ab1SPhillip Lougher 	if (length) {
105e2780ab1SPhillip Lougher 		/*
106e2780ab1SPhillip Lougher 		 * Datablock.
107e2780ab1SPhillip Lougher 		 */
108e2780ab1SPhillip Lougher 		bytes = -offset;
109e2780ab1SPhillip Lougher 		compressed = SQUASHFS_COMPRESSED_BLOCK(length);
110e2780ab1SPhillip Lougher 		length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length);
111e2780ab1SPhillip Lougher 		if (next_index)
112e2780ab1SPhillip Lougher 			*next_index = index + length;
113e2780ab1SPhillip Lougher 
114e2780ab1SPhillip Lougher 		TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n",
115846b730eSPhillip Lougher 			index, compressed ? "" : "un", length, output->length);
116e2780ab1SPhillip Lougher 
117846b730eSPhillip Lougher 		if (length < 0 || length > output->length ||
118e2780ab1SPhillip Lougher 				(index + length) > msblk->bytes_used)
119e2780ab1SPhillip Lougher 			goto read_failure;
120e2780ab1SPhillip Lougher 
121e2780ab1SPhillip Lougher 		for (b = 0; bytes < length; b++, cur_index++) {
122e2780ab1SPhillip Lougher 			bh[b] = sb_getblk(sb, cur_index);
123e2780ab1SPhillip Lougher 			if (bh[b] == NULL)
124e2780ab1SPhillip Lougher 				goto block_release;
125e2780ab1SPhillip Lougher 			bytes += msblk->devblksize;
126e2780ab1SPhillip Lougher 		}
127e2780ab1SPhillip Lougher 		ll_rw_block(READ, b, bh);
128e2780ab1SPhillip Lougher 	} else {
129e2780ab1SPhillip Lougher 		/*
130e2780ab1SPhillip Lougher 		 * Metadata block.
131e2780ab1SPhillip Lougher 		 */
132e2780ab1SPhillip Lougher 		if ((index + 2) > msblk->bytes_used)
133e2780ab1SPhillip Lougher 			goto read_failure;
134e2780ab1SPhillip Lougher 
135e2780ab1SPhillip Lougher 		bh[0] = get_block_length(sb, &cur_index, &offset, &length);
136e2780ab1SPhillip Lougher 		if (bh[0] == NULL)
137e2780ab1SPhillip Lougher 			goto read_failure;
138e2780ab1SPhillip Lougher 		b = 1;
139e2780ab1SPhillip Lougher 
140e2780ab1SPhillip Lougher 		bytes = msblk->devblksize - offset;
141e2780ab1SPhillip Lougher 		compressed = SQUASHFS_COMPRESSED(length);
142e2780ab1SPhillip Lougher 		length = SQUASHFS_COMPRESSED_SIZE(length);
143e2780ab1SPhillip Lougher 		if (next_index)
144e2780ab1SPhillip Lougher 			*next_index = index + length + 2;
145e2780ab1SPhillip Lougher 
146e2780ab1SPhillip Lougher 		TRACE("Block @ 0x%llx, %scompressed size %d\n", index,
147e2780ab1SPhillip Lougher 				compressed ? "" : "un", length);
148e2780ab1SPhillip Lougher 
149846b730eSPhillip Lougher 		if (length < 0 || length > output->length ||
150e2780ab1SPhillip Lougher 					(index + length) > msblk->bytes_used)
151e2780ab1SPhillip Lougher 			goto block_release;
152e2780ab1SPhillip Lougher 
153e2780ab1SPhillip Lougher 		for (; bytes < length; b++) {
154e2780ab1SPhillip Lougher 			bh[b] = sb_getblk(sb, ++cur_index);
155e2780ab1SPhillip Lougher 			if (bh[b] == NULL)
156e2780ab1SPhillip Lougher 				goto block_release;
157e2780ab1SPhillip Lougher 			bytes += msblk->devblksize;
158e2780ab1SPhillip Lougher 		}
159e2780ab1SPhillip Lougher 		ll_rw_block(READ, b - 1, bh + 1);
160e2780ab1SPhillip Lougher 	}
161e2780ab1SPhillip Lougher 
1629508c6b9SPhillip Lougher 	for (i = 0; i < b; i++) {
1639508c6b9SPhillip Lougher 		wait_on_buffer(bh[i]);
1649508c6b9SPhillip Lougher 		if (!buffer_uptodate(bh[i]))
1659508c6b9SPhillip Lougher 			goto block_release;
1669508c6b9SPhillip Lougher 	}
1679508c6b9SPhillip Lougher 
168e2780ab1SPhillip Lougher 	if (compressed) {
169846b730eSPhillip Lougher 		length = squashfs_decompress(msblk, bh, b, offset, length,
170846b730eSPhillip Lougher 			output);
171e6a6d379SPhillip Lougher 		if (length < 0)
172e6a6d379SPhillip Lougher 			goto read_failure;
173e2780ab1SPhillip Lougher 	} else {
174e2780ab1SPhillip Lougher 		/*
175e2780ab1SPhillip Lougher 		 * Block is uncompressed.
176e2780ab1SPhillip Lougher 		 */
177e0125262SManish Sharma 		int in, pg_offset = 0;
178846b730eSPhillip Lougher 		void *data = squashfs_first_page(output);
179e2780ab1SPhillip Lougher 
180e2780ab1SPhillip Lougher 		for (bytes = length; k < b; k++) {
181e2780ab1SPhillip Lougher 			in = min(bytes, msblk->devblksize - offset);
182e2780ab1SPhillip Lougher 			bytes -= in;
183e2780ab1SPhillip Lougher 			while (in) {
184e2780ab1SPhillip Lougher 				if (pg_offset == PAGE_CACHE_SIZE) {
185846b730eSPhillip Lougher 					data = squashfs_next_page(output);
186e2780ab1SPhillip Lougher 					pg_offset = 0;
187e2780ab1SPhillip Lougher 				}
188e2780ab1SPhillip Lougher 				avail = min_t(int, in, PAGE_CACHE_SIZE -
189e2780ab1SPhillip Lougher 						pg_offset);
190846b730eSPhillip Lougher 				memcpy(data + pg_offset, bh[k]->b_data + offset,
191846b730eSPhillip Lougher 						avail);
192e2780ab1SPhillip Lougher 				in -= avail;
193e2780ab1SPhillip Lougher 				pg_offset += avail;
194e2780ab1SPhillip Lougher 				offset += avail;
195e2780ab1SPhillip Lougher 			}
196e2780ab1SPhillip Lougher 			offset = 0;
197e2780ab1SPhillip Lougher 			put_bh(bh[k]);
198e2780ab1SPhillip Lougher 		}
199846b730eSPhillip Lougher 		squashfs_finish_page(output);
200e2780ab1SPhillip Lougher 	}
201e2780ab1SPhillip Lougher 
202e2780ab1SPhillip Lougher 	kfree(bh);
203e2780ab1SPhillip Lougher 	return length;
204e2780ab1SPhillip Lougher 
205e2780ab1SPhillip Lougher block_release:
206e2780ab1SPhillip Lougher 	for (; k < b; k++)
207e2780ab1SPhillip Lougher 		put_bh(bh[k]);
208e2780ab1SPhillip Lougher 
209e2780ab1SPhillip Lougher read_failure:
210118e1ef6SPhillip Lougher 	ERROR("squashfs_read_data failed to read block 0x%llx\n",
211118e1ef6SPhillip Lougher 					(unsigned long long) index);
212e2780ab1SPhillip Lougher 	kfree(bh);
213e2780ab1SPhillip Lougher 	return -EIO;
214e2780ab1SPhillip Lougher }
215