2 * Squashfs - a compressed read only filesystem for Linux
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 * Phillip Lougher <phillip@squashfs.org.uk>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * This file implements the low-level routines to read and decompress
26 * datablocks and metadata blocks.
30 #include <linux/vfs.h>
31 #include <linux/bio.h>
32 #include <linux/slab.h>
33 #include <linux/string.h>
34 #include <linux/pagemap.h>
35 #include <linux/buffer_head.h>
36 #include <linux/workqueue.h>
38 #include "squashfs_fs.h"
39 #include "squashfs_fs_sb.h"
41 #include "decompressor.h"
42 #include "page_actor.h"
44 static struct workqueue_struct *squashfs_read_wq;
46 struct squashfs_read_request {
47 struct super_block *sb;
53 struct squashfs_page_actor *output;
62 * If the read is synchronous, it is possible to retrieve information
63 * about the request by setting these pointers.
67 int *bytes_uncompressed;
70 struct buffer_head **bh;
71 struct work_struct offload;
74 struct squashfs_bio_request {
75 struct buffer_head **bh;
79 static int squashfs_bio_submit(struct squashfs_read_request *req);
81 int squashfs_init_read_wq(void)
83 squashfs_read_wq = create_workqueue("SquashFS read wq");
84 return !!squashfs_read_wq;
87 void squashfs_destroy_read_wq(void)
89 flush_workqueue(squashfs_read_wq);
90 destroy_workqueue(squashfs_read_wq);
93 static void free_read_request(struct squashfs_read_request *req, int error)
95 if (!req->synchronous)
96 squashfs_page_actor_free(req->output, error);
103 static void squashfs_process_blocks(struct squashfs_read_request *req)
106 int bytes, i, length;
107 struct squashfs_sb_info *msblk = req->sb->s_fs_info;
108 struct squashfs_page_actor *actor = req->output;
109 struct buffer_head **bh = req->bh;
110 int nr_buffers = req->nr_buffers;
112 for (i = 0; i < nr_buffers; ++i) {
115 wait_on_buffer(bh[i]);
116 if (!buffer_uptodate(bh[i]))
122 if (req->data_processing == SQUASHFS_METADATA) {
123 /* Extract the length of the metadata block */
124 if (req->offset != msblk->devblksize - 1) {
125 length = le16_to_cpup((__le16 *)
126 (bh[0]->b_data + req->offset));
128 length = (unsigned char)bh[0]->b_data[req->offset];
129 length |= (unsigned char)bh[1]->b_data[0] << 8;
131 req->compressed = SQUASHFS_COMPRESSED(length);
132 req->data_processing = req->compressed ? SQUASHFS_DECOMPRESS
134 length = SQUASHFS_COMPRESSED_SIZE(length);
135 if (req->index + length + 2 > req->read_end) {
136 for (i = 0; i < nr_buffers; ++i)
139 req->length = length;
141 squashfs_bio_submit(req);
144 req->length = length;
145 req->offset = (req->offset + 2) % PAGE_SIZE;
146 if (req->offset < 2) {
153 *(req->bytes_read) = req->length;
155 if (req->data_processing == SQUASHFS_COPY) {
156 squashfs_bh_to_actor(bh, nr_buffers, req->output, req->offset,
157 req->length, msblk->devblksize);
158 } else if (req->data_processing == SQUASHFS_DECOMPRESS) {
159 req->length = squashfs_decompress(msblk, bh, nr_buffers,
160 req->offset, req->length, actor);
161 if (req->length < 0) {
167 /* Last page may have trailing bytes not filled */
168 bytes = req->length % PAGE_SIZE;
169 if (bytes && actor->page[actor->pages - 1])
170 zero_user_segment(actor->page[actor->pages - 1], bytes,
174 if (req->bytes_uncompressed)
175 *(req->bytes_uncompressed) = req->length;
177 for (i = 0; i < nr_buffers; ++i)
181 free_read_request(req, error);
184 static void read_wq_handler(struct work_struct *work)
186 squashfs_process_blocks(container_of(work,
187 struct squashfs_read_request, offload));
190 static void squashfs_bio_end_io(struct bio *bio)
193 int error = bio->bi_error;
194 struct squashfs_bio_request *bio_req = bio->bi_private;
198 for (i = 0; i < bio_req->nr_buffers; ++i) {
202 set_buffer_uptodate(bio_req->bh[i]);
204 clear_buffer_uptodate(bio_req->bh[i]);
205 unlock_buffer(bio_req->bh[i]);
210 static int bh_is_optional(struct squashfs_read_request *req, int idx)
212 int start_idx, end_idx;
213 struct squashfs_sb_info *msblk = req->sb->s_fs_info;
215 start_idx = (idx * msblk->devblksize - req->offset) >> PAGE_SHIFT;
216 end_idx = ((idx + 1) * msblk->devblksize - req->offset + 1) >> PAGE_SHIFT;
217 if (start_idx >= req->output->pages)
221 if (end_idx >= req->output->pages)
223 return !req->output->page[start_idx] && !req->output->page[end_idx];
226 static int actor_getblks(struct squashfs_read_request *req, u64 block)
230 req->bh = kmalloc_array(req->nr_buffers, sizeof(*(req->bh)), GFP_NOIO);
234 for (i = 0; i < req->nr_buffers; ++i) {
236 * When dealing with an uncompressed block, the actor may
237 * contains NULL pages. There's no need to read the buffers
238 * associated with these pages.
240 if (!req->compressed && bh_is_optional(req, i)) {
244 req->bh[i] = sb_getblk(req->sb, block + i);
256 static int squashfs_bio_submit(struct squashfs_read_request *req)
258 struct bio *bio = NULL;
259 struct buffer_head *bh;
260 struct squashfs_bio_request *bio_req = NULL;
261 int b = 0, prev_block = 0;
262 struct squashfs_sb_info *msblk = req->sb->s_fs_info;
264 u64 read_start = round_down(req->index, msblk->devblksize);
265 u64 read_end = round_up(req->index + req->length, msblk->devblksize);
266 sector_t block = read_start >> msblk->devblksize_log2;
267 sector_t block_end = read_end >> msblk->devblksize_log2;
268 int offset = read_start - round_down(req->index, PAGE_SIZE);
269 int nr_buffers = block_end - block;
270 int blksz = msblk->devblksize;
271 int bio_max_pages = nr_buffers > BIO_MAX_PAGES ? BIO_MAX_PAGES
274 /* Setup the request */
275 req->read_end = read_end;
276 req->offset = req->index - read_start;
277 req->nr_buffers = nr_buffers;
278 if (actor_getblks(req, block) < 0)
281 /* Create and submit the BIOs */
282 for (b = 0; b < nr_buffers; ++b, offset += blksz) {
284 if (!bh || !trylock_buffer(bh))
286 if (buffer_uptodate(bh)) {
292 /* Append the buffer to the current BIO if it is contiguous */
293 if (bio && bio_req && prev_block + 1 == b) {
294 if (bio_add_page(bio, bh->b_page, blksz, offset)) {
295 bio_req->nr_buffers += 1;
301 /* Otherwise, submit the current BIO and create a new one */
303 submit_bio(READ, bio);
304 bio_req = kcalloc(1, sizeof(struct squashfs_bio_request),
307 goto req_alloc_failed;
308 bio_req->bh = &req->bh[b];
309 bio = bio_alloc(GFP_NOIO, bio_max_pages);
311 goto bio_alloc_failed;
312 bio->bi_bdev = req->sb->s_bdev;
313 bio->bi_iter.bi_sector = (block + b)
314 << (msblk->devblksize_log2 - 9);
315 bio->bi_private = bio_req;
316 bio->bi_end_io = squashfs_bio_end_io;
318 bio_add_page(bio, bh->b_page, blksz, offset);
319 bio_req->nr_buffers += 1;
323 submit_bio(READ, bio);
325 if (req->synchronous)
326 squashfs_process_blocks(req);
328 INIT_WORK(&req->offload, read_wq_handler);
329 schedule_work(&req->offload);
337 while (--nr_buffers >= b)
338 if (req->bh[nr_buffers])
339 put_bh(req->bh[nr_buffers]);
342 wait_on_buffer(req->bh[b]);
344 free_read_request(req, -ENOMEM);
348 static int read_metadata_block(struct squashfs_read_request *req,
351 int ret, error, bytes_read = 0, bytes_uncompressed = 0;
352 struct squashfs_sb_info *msblk = req->sb->s_fs_info;
354 if (req->index + 2 > msblk->bytes_used) {
355 free_read_request(req, -EINVAL);
360 /* Do not read beyond the end of the device */
361 if (req->index + req->length > msblk->bytes_used)
362 req->length = msblk->bytes_used - req->index;
363 req->data_processing = SQUASHFS_METADATA;
366 * Reading metadata is always synchronous because we don't know the
367 * length in advance and the function is expected to update
368 * 'next_index' and return the length.
370 req->synchronous = true;
372 req->bytes_read = &bytes_read;
373 req->bytes_uncompressed = &bytes_uncompressed;
375 TRACE("Metadata block @ 0x%llx, %scompressed size %d, src size %d\n",
376 req->index, req->compressed ? "" : "un", bytes_read,
377 req->output->length);
379 ret = squashfs_bio_submit(req);
385 *next_index += 2 + bytes_read;
386 return bytes_uncompressed;
389 static int read_data_block(struct squashfs_read_request *req, int length,
390 u64 *next_index, bool synchronous)
392 int ret, error = 0, bytes_uncompressed = 0, bytes_read = 0;
394 req->compressed = SQUASHFS_COMPRESSED_BLOCK(length);
395 req->length = length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length);
396 req->data_processing = req->compressed ? SQUASHFS_DECOMPRESS
399 req->synchronous = synchronous;
402 req->bytes_read = &bytes_read;
403 req->bytes_uncompressed = &bytes_uncompressed;
406 TRACE("Data block @ 0x%llx, %scompressed size %d, src size %d\n",
407 req->index, req->compressed ? "" : "un", req->length,
408 req->output->length);
410 ret = squashfs_bio_submit(req);
414 ret = error ? error : bytes_uncompressed;
416 *next_index += length;
421 * Read and decompress a metadata block or datablock. Length is non-zero
422 * if a datablock is being read (the size is stored elsewhere in the
423 * filesystem), otherwise the length is obtained from the first two bytes of
424 * the metadata block. A bit in the length field indicates if the block
425 * is stored uncompressed in the filesystem (usually because compression
426 * generated a larger block - this does occasionally happen with compression
429 static int __squashfs_read_data(struct super_block *sb, u64 index, int length,
430 u64 *next_index, struct squashfs_page_actor *output, bool sync)
432 struct squashfs_read_request *req;
434 req = kcalloc(1, sizeof(struct squashfs_read_request), GFP_KERNEL);
437 squashfs_page_actor_free(output, -ENOMEM);
443 req->output = output;
449 length = read_data_block(req, length, next_index, sync);
451 length = read_metadata_block(req, next_index);
454 ERROR("squashfs_read_data failed to read block 0x%llx\n",
455 (unsigned long long)index);
462 int squashfs_read_data(struct super_block *sb, u64 index, int length,
463 u64 *next_index, struct squashfs_page_actor *output)
465 return __squashfs_read_data(sb, index, length, next_index, output,
469 int squashfs_read_data_async(struct super_block *sb, u64 index, int length,
470 u64 *next_index, struct squashfs_page_actor *output)
473 return __squashfs_read_data(sb, index, length, next_index, output,