xref: /openbmc/linux/fs/squashfs/block.c (revision 2f8b5444)
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>
342f8b5444SChristoph Hellwig #include <linux/bio.h>
35e2780ab1SPhillip Lougher 
36e2780ab1SPhillip Lougher #include "squashfs_fs.h"
37e2780ab1SPhillip Lougher #include "squashfs_fs_sb.h"
38e2780ab1SPhillip Lougher #include "squashfs.h"
394c0f0bb2SPhillip Lougher #include "decompressor.h"
40846b730eSPhillip Lougher #include "page_actor.h"
41e2780ab1SPhillip Lougher 
42e2780ab1SPhillip Lougher /*
43e2780ab1SPhillip Lougher  * Read the metadata block length, this is stored in the first two
44e2780ab1SPhillip Lougher  * bytes of the metadata block.
45e2780ab1SPhillip Lougher  */
46e2780ab1SPhillip Lougher static struct buffer_head *get_block_length(struct super_block *sb,
47e2780ab1SPhillip Lougher 			u64 *cur_index, int *offset, int *length)
48e2780ab1SPhillip Lougher {
49e2780ab1SPhillip Lougher 	struct squashfs_sb_info *msblk = sb->s_fs_info;
50e2780ab1SPhillip Lougher 	struct buffer_head *bh;
51e2780ab1SPhillip Lougher 
52e2780ab1SPhillip Lougher 	bh = sb_bread(sb, *cur_index);
53e2780ab1SPhillip Lougher 	if (bh == NULL)
54e2780ab1SPhillip Lougher 		return NULL;
55e2780ab1SPhillip Lougher 
56e2780ab1SPhillip Lougher 	if (msblk->devblksize - *offset == 1) {
57e2780ab1SPhillip Lougher 		*length = (unsigned char) bh->b_data[*offset];
58e2780ab1SPhillip Lougher 		put_bh(bh);
59e2780ab1SPhillip Lougher 		bh = sb_bread(sb, ++(*cur_index));
60e2780ab1SPhillip Lougher 		if (bh == NULL)
61e2780ab1SPhillip Lougher 			return NULL;
62e2780ab1SPhillip Lougher 		*length |= (unsigned char) bh->b_data[0] << 8;
63e2780ab1SPhillip Lougher 		*offset = 1;
64e2780ab1SPhillip Lougher 	} else {
65e2780ab1SPhillip Lougher 		*length = (unsigned char) bh->b_data[*offset] |
66e2780ab1SPhillip Lougher 			(unsigned char) bh->b_data[*offset + 1] << 8;
67e2780ab1SPhillip Lougher 		*offset += 2;
683689456bSPhillip Lougher 
693689456bSPhillip Lougher 		if (*offset == msblk->devblksize) {
703689456bSPhillip Lougher 			put_bh(bh);
713689456bSPhillip Lougher 			bh = sb_bread(sb, ++(*cur_index));
723689456bSPhillip Lougher 			if (bh == NULL)
733689456bSPhillip Lougher 				return NULL;
743689456bSPhillip Lougher 			*offset = 0;
753689456bSPhillip Lougher 		}
76e2780ab1SPhillip Lougher 	}
77e2780ab1SPhillip Lougher 
78e2780ab1SPhillip Lougher 	return bh;
79e2780ab1SPhillip Lougher }
80e2780ab1SPhillip Lougher 
81e2780ab1SPhillip Lougher 
82e2780ab1SPhillip Lougher /*
83e2780ab1SPhillip Lougher  * Read and decompress a metadata block or datablock.  Length is non-zero
84e2780ab1SPhillip Lougher  * if a datablock is being read (the size is stored elsewhere in the
85e2780ab1SPhillip Lougher  * filesystem), otherwise the length is obtained from the first two bytes of
86e2780ab1SPhillip Lougher  * the metadata block.  A bit in the length field indicates if the block
87e2780ab1SPhillip Lougher  * is stored uncompressed in the filesystem (usually because compression
88ec9267b6SPhillip Lougher  * generated a larger block - this does occasionally happen with compression
89ec9267b6SPhillip Lougher  * algorithms).
90e2780ab1SPhillip Lougher  */
91846b730eSPhillip Lougher int squashfs_read_data(struct super_block *sb, u64 index, int length,
92846b730eSPhillip Lougher 		u64 *next_index, struct squashfs_page_actor *output)
93e2780ab1SPhillip Lougher {
94e2780ab1SPhillip Lougher 	struct squashfs_sb_info *msblk = sb->s_fs_info;
95e2780ab1SPhillip Lougher 	struct buffer_head **bh;
96e2780ab1SPhillip Lougher 	int offset = index & ((1 << msblk->devblksize_log2) - 1);
97e2780ab1SPhillip Lougher 	u64 cur_index = index >> msblk->devblksize_log2;
98846b730eSPhillip Lougher 	int bytes, compressed, b = 0, k = 0, avail, i;
99e2780ab1SPhillip Lougher 
100846b730eSPhillip Lougher 	bh = kcalloc(((output->length + msblk->devblksize - 1)
101e0d1f700SPhillip Lougher 		>> msblk->devblksize_log2) + 1, sizeof(*bh), GFP_KERNEL);
102e2780ab1SPhillip Lougher 	if (bh == NULL)
103e2780ab1SPhillip Lougher 		return -ENOMEM;
104e2780ab1SPhillip Lougher 
105e2780ab1SPhillip Lougher 	if (length) {
106e2780ab1SPhillip Lougher 		/*
107e2780ab1SPhillip Lougher 		 * Datablock.
108e2780ab1SPhillip Lougher 		 */
109e2780ab1SPhillip Lougher 		bytes = -offset;
110e2780ab1SPhillip Lougher 		compressed = SQUASHFS_COMPRESSED_BLOCK(length);
111e2780ab1SPhillip Lougher 		length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length);
112e2780ab1SPhillip Lougher 		if (next_index)
113e2780ab1SPhillip Lougher 			*next_index = index + length;
114e2780ab1SPhillip Lougher 
115e2780ab1SPhillip Lougher 		TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n",
116846b730eSPhillip Lougher 			index, compressed ? "" : "un", length, output->length);
117e2780ab1SPhillip Lougher 
118846b730eSPhillip Lougher 		if (length < 0 || length > output->length ||
119e2780ab1SPhillip Lougher 				(index + length) > msblk->bytes_used)
120e2780ab1SPhillip Lougher 			goto read_failure;
121e2780ab1SPhillip Lougher 
122e2780ab1SPhillip Lougher 		for (b = 0; bytes < length; b++, cur_index++) {
123e2780ab1SPhillip Lougher 			bh[b] = sb_getblk(sb, cur_index);
124e2780ab1SPhillip Lougher 			if (bh[b] == NULL)
125e2780ab1SPhillip Lougher 				goto block_release;
126e2780ab1SPhillip Lougher 			bytes += msblk->devblksize;
127e2780ab1SPhillip Lougher 		}
128dfec8a14SMike Christie 		ll_rw_block(REQ_OP_READ, 0, b, bh);
129e2780ab1SPhillip Lougher 	} else {
130e2780ab1SPhillip Lougher 		/*
131e2780ab1SPhillip Lougher 		 * Metadata block.
132e2780ab1SPhillip Lougher 		 */
133e2780ab1SPhillip Lougher 		if ((index + 2) > msblk->bytes_used)
134e2780ab1SPhillip Lougher 			goto read_failure;
135e2780ab1SPhillip Lougher 
136e2780ab1SPhillip Lougher 		bh[0] = get_block_length(sb, &cur_index, &offset, &length);
137e2780ab1SPhillip Lougher 		if (bh[0] == NULL)
138e2780ab1SPhillip Lougher 			goto read_failure;
139e2780ab1SPhillip Lougher 		b = 1;
140e2780ab1SPhillip Lougher 
141e2780ab1SPhillip Lougher 		bytes = msblk->devblksize - offset;
142e2780ab1SPhillip Lougher 		compressed = SQUASHFS_COMPRESSED(length);
143e2780ab1SPhillip Lougher 		length = SQUASHFS_COMPRESSED_SIZE(length);
144e2780ab1SPhillip Lougher 		if (next_index)
145e2780ab1SPhillip Lougher 			*next_index = index + length + 2;
146e2780ab1SPhillip Lougher 
147e2780ab1SPhillip Lougher 		TRACE("Block @ 0x%llx, %scompressed size %d\n", index,
148e2780ab1SPhillip Lougher 				compressed ? "" : "un", length);
149e2780ab1SPhillip Lougher 
150846b730eSPhillip Lougher 		if (length < 0 || length > output->length ||
151e2780ab1SPhillip Lougher 					(index + length) > msblk->bytes_used)
152e2780ab1SPhillip Lougher 			goto block_release;
153e2780ab1SPhillip Lougher 
154e2780ab1SPhillip Lougher 		for (; bytes < length; b++) {
155e2780ab1SPhillip Lougher 			bh[b] = sb_getblk(sb, ++cur_index);
156e2780ab1SPhillip Lougher 			if (bh[b] == NULL)
157e2780ab1SPhillip Lougher 				goto block_release;
158e2780ab1SPhillip Lougher 			bytes += msblk->devblksize;
159e2780ab1SPhillip Lougher 		}
160dfec8a14SMike Christie 		ll_rw_block(REQ_OP_READ, 0, b - 1, bh + 1);
161e2780ab1SPhillip Lougher 	}
162e2780ab1SPhillip Lougher 
1639508c6b9SPhillip Lougher 	for (i = 0; i < b; i++) {
1649508c6b9SPhillip Lougher 		wait_on_buffer(bh[i]);
1659508c6b9SPhillip Lougher 		if (!buffer_uptodate(bh[i]))
1669508c6b9SPhillip Lougher 			goto block_release;
1679508c6b9SPhillip Lougher 	}
1689508c6b9SPhillip Lougher 
169e2780ab1SPhillip Lougher 	if (compressed) {
170846b730eSPhillip Lougher 		length = squashfs_decompress(msblk, bh, b, offset, length,
171846b730eSPhillip Lougher 			output);
172e6a6d379SPhillip Lougher 		if (length < 0)
173e6a6d379SPhillip Lougher 			goto read_failure;
174e2780ab1SPhillip Lougher 	} else {
175e2780ab1SPhillip Lougher 		/*
176e2780ab1SPhillip Lougher 		 * Block is uncompressed.
177e2780ab1SPhillip Lougher 		 */
178e0125262SManish Sharma 		int in, pg_offset = 0;
179846b730eSPhillip Lougher 		void *data = squashfs_first_page(output);
180e2780ab1SPhillip Lougher 
181e2780ab1SPhillip Lougher 		for (bytes = length; k < b; k++) {
182e2780ab1SPhillip Lougher 			in = min(bytes, msblk->devblksize - offset);
183e2780ab1SPhillip Lougher 			bytes -= in;
184e2780ab1SPhillip Lougher 			while (in) {
18509cbfeafSKirill A. Shutemov 				if (pg_offset == PAGE_SIZE) {
186846b730eSPhillip Lougher 					data = squashfs_next_page(output);
187e2780ab1SPhillip Lougher 					pg_offset = 0;
188e2780ab1SPhillip Lougher 				}
18909cbfeafSKirill A. Shutemov 				avail = min_t(int, in, PAGE_SIZE -
190e2780ab1SPhillip Lougher 						pg_offset);
191846b730eSPhillip Lougher 				memcpy(data + pg_offset, bh[k]->b_data + offset,
192846b730eSPhillip Lougher 						avail);
193e2780ab1SPhillip Lougher 				in -= avail;
194e2780ab1SPhillip Lougher 				pg_offset += avail;
195e2780ab1SPhillip Lougher 				offset += avail;
196e2780ab1SPhillip Lougher 			}
197e2780ab1SPhillip Lougher 			offset = 0;
198e2780ab1SPhillip Lougher 			put_bh(bh[k]);
199e2780ab1SPhillip Lougher 		}
200846b730eSPhillip Lougher 		squashfs_finish_page(output);
201e2780ab1SPhillip Lougher 	}
202e2780ab1SPhillip Lougher 
203e2780ab1SPhillip Lougher 	kfree(bh);
204e2780ab1SPhillip Lougher 	return length;
205e2780ab1SPhillip Lougher 
206e2780ab1SPhillip Lougher block_release:
207e2780ab1SPhillip Lougher 	for (; k < b; k++)
208e2780ab1SPhillip Lougher 		put_bh(bh[k]);
209e2780ab1SPhillip Lougher 
210e2780ab1SPhillip Lougher read_failure:
211118e1ef6SPhillip Lougher 	ERROR("squashfs_read_data failed to read block 0x%llx\n",
212118e1ef6SPhillip Lougher 					(unsigned long long) index);
213e2780ab1SPhillip Lougher 	kfree(bh);
214e2780ab1SPhillip Lougher 	return -EIO;
215e2780ab1SPhillip Lougher }
216