xref: /openbmc/linux/fs/squashfs/block.c (revision 066ff571)
168252eb5SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e2780ab1SPhillip Lougher /*
3e2780ab1SPhillip Lougher  * Squashfs - a compressed read only filesystem for Linux
4e2780ab1SPhillip Lougher  *
5e2780ab1SPhillip Lougher  * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
6d7f2ff67SPhillip Lougher  * Phillip Lougher <phillip@squashfs.org.uk>
7e2780ab1SPhillip Lougher  *
8e2780ab1SPhillip Lougher  * block.c
9e2780ab1SPhillip Lougher  */
10e2780ab1SPhillip Lougher 
11e2780ab1SPhillip Lougher /*
12e2780ab1SPhillip Lougher  * This file implements the low-level routines to read and decompress
13e2780ab1SPhillip Lougher  * datablocks and metadata blocks.
14e2780ab1SPhillip Lougher  */
15e2780ab1SPhillip Lougher 
1693e72b3cSPhilippe Liard #include <linux/blkdev.h>
17e2780ab1SPhillip Lougher #include <linux/fs.h>
18e2780ab1SPhillip Lougher #include <linux/vfs.h>
19e2780ab1SPhillip Lougher #include <linux/slab.h>
20e2780ab1SPhillip Lougher #include <linux/string.h>
21e2780ab1SPhillip Lougher #include <linux/buffer_head.h>
222f8b5444SChristoph Hellwig #include <linux/bio.h>
23e2780ab1SPhillip Lougher 
24e2780ab1SPhillip Lougher #include "squashfs_fs.h"
25e2780ab1SPhillip Lougher #include "squashfs_fs_sb.h"
26e2780ab1SPhillip Lougher #include "squashfs.h"
274c0f0bb2SPhillip Lougher #include "decompressor.h"
28846b730eSPhillip Lougher #include "page_actor.h"
29e2780ab1SPhillip Lougher 
30e2780ab1SPhillip Lougher /*
3193e72b3cSPhilippe Liard  * Returns the amount of bytes copied to the page actor.
32e2780ab1SPhillip Lougher  */
3393e72b3cSPhilippe Liard static int copy_bio_to_actor(struct bio *bio,
3493e72b3cSPhilippe Liard 			     struct squashfs_page_actor *actor,
3593e72b3cSPhilippe Liard 			     int offset, int req_length)
3693e72b3cSPhilippe Liard {
3793e72b3cSPhilippe Liard 	void *actor_addr = squashfs_first_page(actor);
3893e72b3cSPhilippe Liard 	struct bvec_iter_all iter_all = {};
3993e72b3cSPhilippe Liard 	struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
4093e72b3cSPhilippe Liard 	int copied_bytes = 0;
4193e72b3cSPhilippe Liard 	int actor_offset = 0;
4293e72b3cSPhilippe Liard 
4393e72b3cSPhilippe Liard 	if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all)))
4493e72b3cSPhilippe Liard 		return 0;
4593e72b3cSPhilippe Liard 
4693e72b3cSPhilippe Liard 	while (copied_bytes < req_length) {
4793e72b3cSPhilippe Liard 		int bytes_to_copy = min_t(int, bvec->bv_len - offset,
4893e72b3cSPhilippe Liard 					  PAGE_SIZE - actor_offset);
4993e72b3cSPhilippe Liard 
5093e72b3cSPhilippe Liard 		bytes_to_copy = min_t(int, bytes_to_copy,
5193e72b3cSPhilippe Liard 				      req_length - copied_bytes);
52fbc27241SChristoph Hellwig 		memcpy(actor_addr + actor_offset, bvec_virt(bvec) + offset,
5393e72b3cSPhilippe Liard 		       bytes_to_copy);
5493e72b3cSPhilippe Liard 
5593e72b3cSPhilippe Liard 		actor_offset += bytes_to_copy;
5693e72b3cSPhilippe Liard 		copied_bytes += bytes_to_copy;
5793e72b3cSPhilippe Liard 		offset += bytes_to_copy;
5893e72b3cSPhilippe Liard 
5993e72b3cSPhilippe Liard 		if (actor_offset >= PAGE_SIZE) {
6093e72b3cSPhilippe Liard 			actor_addr = squashfs_next_page(actor);
6193e72b3cSPhilippe Liard 			if (!actor_addr)
6293e72b3cSPhilippe Liard 				break;
6393e72b3cSPhilippe Liard 			actor_offset = 0;
6493e72b3cSPhilippe Liard 		}
6593e72b3cSPhilippe Liard 		if (offset >= bvec->bv_len) {
6693e72b3cSPhilippe Liard 			if (!bio_next_segment(bio, &iter_all))
6793e72b3cSPhilippe Liard 				break;
6893e72b3cSPhilippe Liard 			offset = 0;
6993e72b3cSPhilippe Liard 		}
7093e72b3cSPhilippe Liard 	}
7193e72b3cSPhilippe Liard 	squashfs_finish_page(actor);
7293e72b3cSPhilippe Liard 	return copied_bytes;
7393e72b3cSPhilippe Liard }
7493e72b3cSPhilippe Liard 
7593e72b3cSPhilippe Liard static int squashfs_bio_read(struct super_block *sb, u64 index, int length,
7693e72b3cSPhilippe Liard 			     struct bio **biop, int *block_offset)
77e2780ab1SPhillip Lougher {
78e2780ab1SPhillip Lougher 	struct squashfs_sb_info *msblk = sb->s_fs_info;
7993e72b3cSPhilippe Liard 	const u64 read_start = round_down(index, msblk->devblksize);
8093e72b3cSPhilippe Liard 	const sector_t block = read_start >> msblk->devblksize_log2;
8193e72b3cSPhilippe Liard 	const u64 read_end = round_up(index + length, msblk->devblksize);
8293e72b3cSPhilippe Liard 	const sector_t block_end = read_end >> msblk->devblksize_log2;
8393e72b3cSPhilippe Liard 	int offset = read_start - round_down(index, PAGE_SIZE);
8493e72b3cSPhilippe Liard 	int total_len = (block_end - block) << msblk->devblksize_log2;
8593e72b3cSPhilippe Liard 	const int page_count = DIV_ROUND_UP(total_len + offset, PAGE_SIZE);
8693e72b3cSPhilippe Liard 	int error, i;
8793e72b3cSPhilippe Liard 	struct bio *bio;
88e2780ab1SPhillip Lougher 
89*066ff571SChristoph Hellwig 	bio = bio_kmalloc(page_count, GFP_NOIO);
9093e72b3cSPhilippe Liard 	if (!bio)
9193e72b3cSPhilippe Liard 		return -ENOMEM;
92*066ff571SChristoph Hellwig 	bio_init(bio, sb->s_bdev, bio->bi_inline_vecs, page_count, REQ_OP_READ);
9393e72b3cSPhilippe Liard 	bio->bi_iter.bi_sector = block * (msblk->devblksize >> SECTOR_SHIFT);
943689456bSPhillip Lougher 
9593e72b3cSPhilippe Liard 	for (i = 0; i < page_count; ++i) {
9693e72b3cSPhilippe Liard 		unsigned int len =
9793e72b3cSPhilippe Liard 			min_t(unsigned int, PAGE_SIZE - offset, total_len);
9893e72b3cSPhilippe Liard 		struct page *page = alloc_page(GFP_NOIO);
9993e72b3cSPhilippe Liard 
10093e72b3cSPhilippe Liard 		if (!page) {
10193e72b3cSPhilippe Liard 			error = -ENOMEM;
10293e72b3cSPhilippe Liard 			goto out_free_bio;
1033689456bSPhillip Lougher 		}
10493e72b3cSPhilippe Liard 		if (!bio_add_page(bio, page, len, offset)) {
10593e72b3cSPhilippe Liard 			error = -EIO;
10693e72b3cSPhilippe Liard 			goto out_free_bio;
10793e72b3cSPhilippe Liard 		}
10893e72b3cSPhilippe Liard 		offset = 0;
10993e72b3cSPhilippe Liard 		total_len -= len;
110e2780ab1SPhillip Lougher 	}
111e2780ab1SPhillip Lougher 
11293e72b3cSPhilippe Liard 	error = submit_bio_wait(bio);
11393e72b3cSPhilippe Liard 	if (error)
11493e72b3cSPhilippe Liard 		goto out_free_bio;
115e2780ab1SPhillip Lougher 
11693e72b3cSPhilippe Liard 	*biop = bio;
11793e72b3cSPhilippe Liard 	*block_offset = index & ((1 << msblk->devblksize_log2) - 1);
11893e72b3cSPhilippe Liard 	return 0;
11993e72b3cSPhilippe Liard 
12093e72b3cSPhilippe Liard out_free_bio:
12193e72b3cSPhilippe Liard 	bio_free_pages(bio);
122*066ff571SChristoph Hellwig 	bio_uninit(bio);
123*066ff571SChristoph Hellwig 	kfree(bio);
12493e72b3cSPhilippe Liard 	return error;
12593e72b3cSPhilippe Liard }
126e2780ab1SPhillip Lougher 
127e2780ab1SPhillip Lougher /*
128e2780ab1SPhillip Lougher  * Read and decompress a metadata block or datablock.  Length is non-zero
129e2780ab1SPhillip Lougher  * if a datablock is being read (the size is stored elsewhere in the
130e2780ab1SPhillip Lougher  * filesystem), otherwise the length is obtained from the first two bytes of
131e2780ab1SPhillip Lougher  * the metadata block.  A bit in the length field indicates if the block
132e2780ab1SPhillip Lougher  * is stored uncompressed in the filesystem (usually because compression
133ec9267b6SPhillip Lougher  * generated a larger block - this does occasionally happen with compression
134ec9267b6SPhillip Lougher  * algorithms).
135e2780ab1SPhillip Lougher  */
136846b730eSPhillip Lougher int squashfs_read_data(struct super_block *sb, u64 index, int length,
137846b730eSPhillip Lougher 		       u64 *next_index, struct squashfs_page_actor *output)
138e2780ab1SPhillip Lougher {
139e2780ab1SPhillip Lougher 	struct squashfs_sb_info *msblk = sb->s_fs_info;
14093e72b3cSPhilippe Liard 	struct bio *bio = NULL;
14193e72b3cSPhilippe Liard 	int compressed;
14293e72b3cSPhilippe Liard 	int res;
14393e72b3cSPhilippe Liard 	int offset;
144e2780ab1SPhillip Lougher 
145e2780ab1SPhillip Lougher 	if (length) {
146e2780ab1SPhillip Lougher 		/*
147e2780ab1SPhillip Lougher 		 * Datablock.
148e2780ab1SPhillip Lougher 		 */
149e2780ab1SPhillip Lougher 		compressed = SQUASHFS_COMPRESSED_BLOCK(length);
150e2780ab1SPhillip Lougher 		length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length);
151e2780ab1SPhillip Lougher 		TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n",
152846b730eSPhillip Lougher 			index, compressed ? "" : "un", length, output->length);
153e2780ab1SPhillip Lougher 	} else {
154e2780ab1SPhillip Lougher 		/*
155e2780ab1SPhillip Lougher 		 * Metadata block.
156e2780ab1SPhillip Lougher 		 */
15793e72b3cSPhilippe Liard 		const u8 *data;
15893e72b3cSPhilippe Liard 		struct bvec_iter_all iter_all = {};
15993e72b3cSPhilippe Liard 		struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
160e2780ab1SPhillip Lougher 
16193e72b3cSPhilippe Liard 		if (index + 2 > msblk->bytes_used) {
16293e72b3cSPhilippe Liard 			res = -EIO;
16393e72b3cSPhilippe Liard 			goto out;
16493e72b3cSPhilippe Liard 		}
16593e72b3cSPhilippe Liard 		res = squashfs_bio_read(sb, index, 2, &bio, &offset);
16693e72b3cSPhilippe Liard 		if (res)
16793e72b3cSPhilippe Liard 			goto out;
168e2780ab1SPhillip Lougher 
16993e72b3cSPhilippe Liard 		if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all))) {
17093e72b3cSPhilippe Liard 			res = -EIO;
17193e72b3cSPhilippe Liard 			goto out_free_bio;
17293e72b3cSPhilippe Liard 		}
17393e72b3cSPhilippe Liard 		/* Extract the length of the metadata block */
174fbc27241SChristoph Hellwig 		data = bvec_virt(bvec);
17593e72b3cSPhilippe Liard 		length = data[offset];
1762910c59fSPhillip Lougher 		if (offset < bvec->bv_len - 1) {
17793e72b3cSPhilippe Liard 			length |= data[offset + 1] << 8;
17893e72b3cSPhilippe Liard 		} else {
17993e72b3cSPhilippe Liard 			if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all))) {
18093e72b3cSPhilippe Liard 				res = -EIO;
18193e72b3cSPhilippe Liard 				goto out_free_bio;
18293e72b3cSPhilippe Liard 			}
183fbc27241SChristoph Hellwig 			data = bvec_virt(bvec);
18493e72b3cSPhilippe Liard 			length |= data[0] << 8;
18593e72b3cSPhilippe Liard 		}
18693e72b3cSPhilippe Liard 		bio_free_pages(bio);
187*066ff571SChristoph Hellwig 		bio_uninit(bio);
188*066ff571SChristoph Hellwig 		kfree(bio);
18993e72b3cSPhilippe Liard 
190e2780ab1SPhillip Lougher 		compressed = SQUASHFS_COMPRESSED(length);
191e2780ab1SPhillip Lougher 		length = SQUASHFS_COMPRESSED_SIZE(length);
19293e72b3cSPhilippe Liard 		index += 2;
193e2780ab1SPhillip Lougher 
194e812cbbbSPhillip Lougher 		TRACE("Block @ 0x%llx, %scompressed size %d\n", index - 2,
195e2780ab1SPhillip Lougher 		      compressed ? "" : "un", length);
196e2780ab1SPhillip Lougher 	}
197e812cbbbSPhillip Lougher 	if (length < 0 || length > output->length ||
198e812cbbbSPhillip Lougher 			(index + length) > msblk->bytes_used) {
199e812cbbbSPhillip Lougher 		res = -EIO;
200e812cbbbSPhillip Lougher 		goto out;
201e812cbbbSPhillip Lougher 	}
202e812cbbbSPhillip Lougher 
20393e72b3cSPhilippe Liard 	if (next_index)
20493e72b3cSPhilippe Liard 		*next_index = index + length;
205e2780ab1SPhillip Lougher 
20693e72b3cSPhilippe Liard 	res = squashfs_bio_read(sb, index, length, &bio, &offset);
20793e72b3cSPhilippe Liard 	if (res)
20893e72b3cSPhilippe Liard 		goto out;
2099508c6b9SPhillip Lougher 
210e2780ab1SPhillip Lougher 	if (compressed) {
21193e72b3cSPhilippe Liard 		if (!msblk->stream) {
21293e72b3cSPhilippe Liard 			res = -EIO;
21393e72b3cSPhilippe Liard 			goto out_free_bio;
21493e72b3cSPhilippe Liard 		}
21593e72b3cSPhilippe Liard 		res = squashfs_decompress(msblk, bio, offset, length, output);
216e2780ab1SPhillip Lougher 	} else {
21793e72b3cSPhilippe Liard 		res = copy_bio_to_actor(bio, output, offset, length);
218e2780ab1SPhillip Lougher 	}
219e2780ab1SPhillip Lougher 
22093e72b3cSPhilippe Liard out_free_bio:
22193e72b3cSPhilippe Liard 	bio_free_pages(bio);
222*066ff571SChristoph Hellwig 	bio_uninit(bio);
223*066ff571SChristoph Hellwig 	kfree(bio);
22493e72b3cSPhilippe Liard out:
22510dde05bSVincent Whitchurch 	if (res < 0) {
22693e72b3cSPhilippe Liard 		ERROR("Failed to read block 0x%llx: %d\n", index, res);
22710dde05bSVincent Whitchurch 		if (msblk->panic_on_errors)
22810dde05bSVincent Whitchurch 			panic("squashfs read failed");
22910dde05bSVincent Whitchurch 	}
230e2780ab1SPhillip Lougher 
23193e72b3cSPhilippe Liard 	return res;
232e2780ab1SPhillip Lougher }
233