xref: /openbmc/linux/fs/squashfs/block.c (revision d7f2ff67)
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"
39e2780ab1SPhillip Lougher 
40e2780ab1SPhillip Lougher /*
41e2780ab1SPhillip Lougher  * Read the metadata block length, this is stored in the first two
42e2780ab1SPhillip Lougher  * bytes of the metadata block.
43e2780ab1SPhillip Lougher  */
44e2780ab1SPhillip Lougher static struct buffer_head *get_block_length(struct super_block *sb,
45e2780ab1SPhillip Lougher 			u64 *cur_index, int *offset, int *length)
46e2780ab1SPhillip Lougher {
47e2780ab1SPhillip Lougher 	struct squashfs_sb_info *msblk = sb->s_fs_info;
48e2780ab1SPhillip Lougher 	struct buffer_head *bh;
49e2780ab1SPhillip Lougher 
50e2780ab1SPhillip Lougher 	bh = sb_bread(sb, *cur_index);
51e2780ab1SPhillip Lougher 	if (bh == NULL)
52e2780ab1SPhillip Lougher 		return NULL;
53e2780ab1SPhillip Lougher 
54e2780ab1SPhillip Lougher 	if (msblk->devblksize - *offset == 1) {
55e2780ab1SPhillip Lougher 		*length = (unsigned char) bh->b_data[*offset];
56e2780ab1SPhillip Lougher 		put_bh(bh);
57e2780ab1SPhillip Lougher 		bh = sb_bread(sb, ++(*cur_index));
58e2780ab1SPhillip Lougher 		if (bh == NULL)
59e2780ab1SPhillip Lougher 			return NULL;
60e2780ab1SPhillip Lougher 		*length |= (unsigned char) bh->b_data[0] << 8;
61e2780ab1SPhillip Lougher 		*offset = 1;
62e2780ab1SPhillip Lougher 	} else {
63e2780ab1SPhillip Lougher 		*length = (unsigned char) bh->b_data[*offset] |
64e2780ab1SPhillip Lougher 			(unsigned char) bh->b_data[*offset + 1] << 8;
65e2780ab1SPhillip Lougher 		*offset += 2;
663689456bSPhillip Lougher 
673689456bSPhillip Lougher 		if (*offset == msblk->devblksize) {
683689456bSPhillip Lougher 			put_bh(bh);
693689456bSPhillip Lougher 			bh = sb_bread(sb, ++(*cur_index));
703689456bSPhillip Lougher 			if (bh == NULL)
713689456bSPhillip Lougher 				return NULL;
723689456bSPhillip Lougher 			*offset = 0;
733689456bSPhillip Lougher 		}
74e2780ab1SPhillip Lougher 	}
75e2780ab1SPhillip Lougher 
76e2780ab1SPhillip Lougher 	return bh;
77e2780ab1SPhillip Lougher }
78e2780ab1SPhillip Lougher 
79e2780ab1SPhillip Lougher 
80e2780ab1SPhillip Lougher /*
81e2780ab1SPhillip Lougher  * Read and decompress a metadata block or datablock.  Length is non-zero
82e2780ab1SPhillip Lougher  * if a datablock is being read (the size is stored elsewhere in the
83e2780ab1SPhillip Lougher  * filesystem), otherwise the length is obtained from the first two bytes of
84e2780ab1SPhillip Lougher  * the metadata block.  A bit in the length field indicates if the block
85e2780ab1SPhillip Lougher  * is stored uncompressed in the filesystem (usually because compression
86e2780ab1SPhillip Lougher  * generated a larger block - this does occasionally happen with zlib).
87e2780ab1SPhillip Lougher  */
88e2780ab1SPhillip Lougher int squashfs_read_data(struct super_block *sb, void **buffer, u64 index,
89118e1ef6SPhillip Lougher 			int length, u64 *next_index, int srclength, int pages)
90e2780ab1SPhillip Lougher {
91e2780ab1SPhillip Lougher 	struct squashfs_sb_info *msblk = sb->s_fs_info;
92e2780ab1SPhillip Lougher 	struct buffer_head **bh;
93e2780ab1SPhillip Lougher 	int offset = index & ((1 << msblk->devblksize_log2) - 1);
94e2780ab1SPhillip Lougher 	u64 cur_index = index >> msblk->devblksize_log2;
95e2780ab1SPhillip Lougher 	int bytes, compressed, b = 0, k = 0, page = 0, avail;
96e2780ab1SPhillip Lougher 
97e0d1f700SPhillip Lougher 	bh = kcalloc(((srclength + msblk->devblksize - 1)
98e0d1f700SPhillip Lougher 		>> msblk->devblksize_log2) + 1, sizeof(*bh), GFP_KERNEL);
99e2780ab1SPhillip Lougher 	if (bh == NULL)
100e2780ab1SPhillip Lougher 		return -ENOMEM;
101e2780ab1SPhillip Lougher 
102e2780ab1SPhillip Lougher 	if (length) {
103e2780ab1SPhillip Lougher 		/*
104e2780ab1SPhillip Lougher 		 * Datablock.
105e2780ab1SPhillip Lougher 		 */
106e2780ab1SPhillip Lougher 		bytes = -offset;
107e2780ab1SPhillip Lougher 		compressed = SQUASHFS_COMPRESSED_BLOCK(length);
108e2780ab1SPhillip Lougher 		length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length);
109e2780ab1SPhillip Lougher 		if (next_index)
110e2780ab1SPhillip Lougher 			*next_index = index + length;
111e2780ab1SPhillip Lougher 
112e2780ab1SPhillip Lougher 		TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n",
113e2780ab1SPhillip Lougher 			index, compressed ? "" : "un", length, srclength);
114e2780ab1SPhillip Lougher 
115e2780ab1SPhillip Lougher 		if (length < 0 || length > srclength ||
116e2780ab1SPhillip Lougher 				(index + length) > msblk->bytes_used)
117e2780ab1SPhillip Lougher 			goto read_failure;
118e2780ab1SPhillip Lougher 
119e2780ab1SPhillip Lougher 		for (b = 0; bytes < length; b++, cur_index++) {
120e2780ab1SPhillip Lougher 			bh[b] = sb_getblk(sb, cur_index);
121e2780ab1SPhillip Lougher 			if (bh[b] == NULL)
122e2780ab1SPhillip Lougher 				goto block_release;
123e2780ab1SPhillip Lougher 			bytes += msblk->devblksize;
124e2780ab1SPhillip Lougher 		}
125e2780ab1SPhillip Lougher 		ll_rw_block(READ, b, bh);
126e2780ab1SPhillip Lougher 	} else {
127e2780ab1SPhillip Lougher 		/*
128e2780ab1SPhillip Lougher 		 * Metadata block.
129e2780ab1SPhillip Lougher 		 */
130e2780ab1SPhillip Lougher 		if ((index + 2) > msblk->bytes_used)
131e2780ab1SPhillip Lougher 			goto read_failure;
132e2780ab1SPhillip Lougher 
133e2780ab1SPhillip Lougher 		bh[0] = get_block_length(sb, &cur_index, &offset, &length);
134e2780ab1SPhillip Lougher 		if (bh[0] == NULL)
135e2780ab1SPhillip Lougher 			goto read_failure;
136e2780ab1SPhillip Lougher 		b = 1;
137e2780ab1SPhillip Lougher 
138e2780ab1SPhillip Lougher 		bytes = msblk->devblksize - offset;
139e2780ab1SPhillip Lougher 		compressed = SQUASHFS_COMPRESSED(length);
140e2780ab1SPhillip Lougher 		length = SQUASHFS_COMPRESSED_SIZE(length);
141e2780ab1SPhillip Lougher 		if (next_index)
142e2780ab1SPhillip Lougher 			*next_index = index + length + 2;
143e2780ab1SPhillip Lougher 
144e2780ab1SPhillip Lougher 		TRACE("Block @ 0x%llx, %scompressed size %d\n", index,
145e2780ab1SPhillip Lougher 				compressed ? "" : "un", length);
146e2780ab1SPhillip Lougher 
147e2780ab1SPhillip Lougher 		if (length < 0 || length > srclength ||
148e2780ab1SPhillip Lougher 					(index + length) > msblk->bytes_used)
149e2780ab1SPhillip Lougher 			goto block_release;
150e2780ab1SPhillip Lougher 
151e2780ab1SPhillip Lougher 		for (; bytes < length; b++) {
152e2780ab1SPhillip Lougher 			bh[b] = sb_getblk(sb, ++cur_index);
153e2780ab1SPhillip Lougher 			if (bh[b] == NULL)
154e2780ab1SPhillip Lougher 				goto block_release;
155e2780ab1SPhillip Lougher 			bytes += msblk->devblksize;
156e2780ab1SPhillip Lougher 		}
157e2780ab1SPhillip Lougher 		ll_rw_block(READ, b - 1, bh + 1);
158e2780ab1SPhillip Lougher 	}
159e2780ab1SPhillip Lougher 
160e2780ab1SPhillip Lougher 	if (compressed) {
1614c0f0bb2SPhillip Lougher 		length = squashfs_decompress(msblk, buffer, bh, b, offset,
162e6a6d379SPhillip Lougher 			 length, srclength, pages);
163e6a6d379SPhillip Lougher 		if (length < 0)
164e6a6d379SPhillip Lougher 			goto read_failure;
165e2780ab1SPhillip Lougher 	} else {
166e2780ab1SPhillip Lougher 		/*
167e2780ab1SPhillip Lougher 		 * Block is uncompressed.
168e2780ab1SPhillip Lougher 		 */
169e2780ab1SPhillip Lougher 		int i, in, pg_offset = 0;
170e2780ab1SPhillip Lougher 
171e2780ab1SPhillip Lougher 		for (i = 0; i < b; i++) {
172e2780ab1SPhillip Lougher 			wait_on_buffer(bh[i]);
173e2780ab1SPhillip Lougher 			if (!buffer_uptodate(bh[i]))
174e2780ab1SPhillip Lougher 				goto block_release;
175e2780ab1SPhillip Lougher 		}
176e2780ab1SPhillip Lougher 
177e2780ab1SPhillip Lougher 		for (bytes = length; k < b; k++) {
178e2780ab1SPhillip Lougher 			in = min(bytes, msblk->devblksize - offset);
179e2780ab1SPhillip Lougher 			bytes -= in;
180e2780ab1SPhillip Lougher 			while (in) {
181e2780ab1SPhillip Lougher 				if (pg_offset == PAGE_CACHE_SIZE) {
182e2780ab1SPhillip Lougher 					page++;
183e2780ab1SPhillip Lougher 					pg_offset = 0;
184e2780ab1SPhillip Lougher 				}
185e2780ab1SPhillip Lougher 				avail = min_t(int, in, PAGE_CACHE_SIZE -
186e2780ab1SPhillip Lougher 						pg_offset);
187e2780ab1SPhillip Lougher 				memcpy(buffer[page] + pg_offset,
188e2780ab1SPhillip Lougher 						bh[k]->b_data + offset, avail);
189e2780ab1SPhillip Lougher 				in -= avail;
190e2780ab1SPhillip Lougher 				pg_offset += avail;
191e2780ab1SPhillip Lougher 				offset += avail;
192e2780ab1SPhillip Lougher 			}
193e2780ab1SPhillip Lougher 			offset = 0;
194e2780ab1SPhillip Lougher 			put_bh(bh[k]);
195e2780ab1SPhillip Lougher 		}
196e2780ab1SPhillip Lougher 	}
197e2780ab1SPhillip Lougher 
198e2780ab1SPhillip Lougher 	kfree(bh);
199e2780ab1SPhillip Lougher 	return length;
200e2780ab1SPhillip Lougher 
201e2780ab1SPhillip Lougher block_release:
202e2780ab1SPhillip Lougher 	for (; k < b; k++)
203e2780ab1SPhillip Lougher 		put_bh(bh[k]);
204e2780ab1SPhillip Lougher 
205e2780ab1SPhillip Lougher read_failure:
206118e1ef6SPhillip Lougher 	ERROR("squashfs_read_data failed to read block 0x%llx\n",
207118e1ef6SPhillip Lougher 					(unsigned long long) index);
208e2780ab1SPhillip Lougher 	kfree(bh);
209e2780ab1SPhillip Lougher 	return -EIO;
210e2780ab1SPhillip Lougher }
211