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 86ec9267b6SPhillip Lougher * generated a larger block - this does occasionally happen with compression 87ec9267b6SPhillip Lougher * algorithms). 88e2780ab1SPhillip Lougher */ 89e2780ab1SPhillip Lougher int squashfs_read_data(struct super_block *sb, void **buffer, u64 index, 90118e1ef6SPhillip Lougher int length, u64 *next_index, int srclength, int pages) 91e2780ab1SPhillip Lougher { 92e2780ab1SPhillip Lougher struct squashfs_sb_info *msblk = sb->s_fs_info; 93e2780ab1SPhillip Lougher struct buffer_head **bh; 94e2780ab1SPhillip Lougher int offset = index & ((1 << msblk->devblksize_log2) - 1); 95e2780ab1SPhillip Lougher u64 cur_index = index >> msblk->devblksize_log2; 96e2780ab1SPhillip Lougher int bytes, compressed, b = 0, k = 0, page = 0, avail; 97e2780ab1SPhillip Lougher 98e0d1f700SPhillip Lougher bh = kcalloc(((srclength + msblk->devblksize - 1) 99e0d1f700SPhillip Lougher >> msblk->devblksize_log2) + 1, sizeof(*bh), GFP_KERNEL); 100e2780ab1SPhillip Lougher if (bh == NULL) 101e2780ab1SPhillip Lougher return -ENOMEM; 102e2780ab1SPhillip Lougher 103e2780ab1SPhillip Lougher if (length) { 104e2780ab1SPhillip Lougher /* 105e2780ab1SPhillip Lougher * Datablock. 106e2780ab1SPhillip Lougher */ 107e2780ab1SPhillip Lougher bytes = -offset; 108e2780ab1SPhillip Lougher compressed = SQUASHFS_COMPRESSED_BLOCK(length); 109e2780ab1SPhillip Lougher length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length); 110e2780ab1SPhillip Lougher if (next_index) 111e2780ab1SPhillip Lougher *next_index = index + length; 112e2780ab1SPhillip Lougher 113e2780ab1SPhillip Lougher TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n", 114e2780ab1SPhillip Lougher index, compressed ? "" : "un", length, srclength); 115e2780ab1SPhillip Lougher 116e2780ab1SPhillip Lougher if (length < 0 || length > srclength || 117e2780ab1SPhillip Lougher (index + length) > msblk->bytes_used) 118e2780ab1SPhillip Lougher goto read_failure; 119e2780ab1SPhillip Lougher 120e2780ab1SPhillip Lougher for (b = 0; bytes < length; b++, cur_index++) { 121e2780ab1SPhillip Lougher bh[b] = sb_getblk(sb, cur_index); 122e2780ab1SPhillip Lougher if (bh[b] == NULL) 123e2780ab1SPhillip Lougher goto block_release; 124e2780ab1SPhillip Lougher bytes += msblk->devblksize; 125e2780ab1SPhillip Lougher } 126e2780ab1SPhillip Lougher ll_rw_block(READ, b, bh); 127e2780ab1SPhillip Lougher } else { 128e2780ab1SPhillip Lougher /* 129e2780ab1SPhillip Lougher * Metadata block. 130e2780ab1SPhillip Lougher */ 131e2780ab1SPhillip Lougher if ((index + 2) > msblk->bytes_used) 132e2780ab1SPhillip Lougher goto read_failure; 133e2780ab1SPhillip Lougher 134e2780ab1SPhillip Lougher bh[0] = get_block_length(sb, &cur_index, &offset, &length); 135e2780ab1SPhillip Lougher if (bh[0] == NULL) 136e2780ab1SPhillip Lougher goto read_failure; 137e2780ab1SPhillip Lougher b = 1; 138e2780ab1SPhillip Lougher 139e2780ab1SPhillip Lougher bytes = msblk->devblksize - offset; 140e2780ab1SPhillip Lougher compressed = SQUASHFS_COMPRESSED(length); 141e2780ab1SPhillip Lougher length = SQUASHFS_COMPRESSED_SIZE(length); 142e2780ab1SPhillip Lougher if (next_index) 143e2780ab1SPhillip Lougher *next_index = index + length + 2; 144e2780ab1SPhillip Lougher 145e2780ab1SPhillip Lougher TRACE("Block @ 0x%llx, %scompressed size %d\n", index, 146e2780ab1SPhillip Lougher compressed ? "" : "un", length); 147e2780ab1SPhillip Lougher 148e2780ab1SPhillip Lougher if (length < 0 || length > srclength || 149e2780ab1SPhillip Lougher (index + length) > msblk->bytes_used) 150e2780ab1SPhillip Lougher goto block_release; 151e2780ab1SPhillip Lougher 152e2780ab1SPhillip Lougher for (; bytes < length; b++) { 153e2780ab1SPhillip Lougher bh[b] = sb_getblk(sb, ++cur_index); 154e2780ab1SPhillip Lougher if (bh[b] == NULL) 155e2780ab1SPhillip Lougher goto block_release; 156e2780ab1SPhillip Lougher bytes += msblk->devblksize; 157e2780ab1SPhillip Lougher } 158e2780ab1SPhillip Lougher ll_rw_block(READ, b - 1, bh + 1); 159e2780ab1SPhillip Lougher } 160e2780ab1SPhillip Lougher 161e2780ab1SPhillip Lougher if (compressed) { 1624c0f0bb2SPhillip Lougher length = squashfs_decompress(msblk, buffer, bh, b, offset, 163e6a6d379SPhillip Lougher length, srclength, pages); 164e6a6d379SPhillip Lougher if (length < 0) 165e6a6d379SPhillip Lougher goto read_failure; 166e2780ab1SPhillip Lougher } else { 167e2780ab1SPhillip Lougher /* 168e2780ab1SPhillip Lougher * Block is uncompressed. 169e2780ab1SPhillip Lougher */ 170*e0125262SManish Sharma int in, pg_offset = 0; 171e2780ab1SPhillip Lougher 172e2780ab1SPhillip Lougher for (bytes = length; k < b; k++) { 173e2780ab1SPhillip Lougher in = min(bytes, msblk->devblksize - offset); 174e2780ab1SPhillip Lougher bytes -= in; 175*e0125262SManish Sharma wait_on_buffer(bh[k]); 176*e0125262SManish Sharma if (!buffer_uptodate(bh[k])) 177*e0125262SManish Sharma goto block_release; 178e2780ab1SPhillip Lougher while (in) { 179e2780ab1SPhillip Lougher if (pg_offset == PAGE_CACHE_SIZE) { 180e2780ab1SPhillip Lougher page++; 181e2780ab1SPhillip Lougher pg_offset = 0; 182e2780ab1SPhillip Lougher } 183e2780ab1SPhillip Lougher avail = min_t(int, in, PAGE_CACHE_SIZE - 184e2780ab1SPhillip Lougher pg_offset); 185e2780ab1SPhillip Lougher memcpy(buffer[page] + pg_offset, 186e2780ab1SPhillip Lougher bh[k]->b_data + offset, avail); 187e2780ab1SPhillip Lougher in -= avail; 188e2780ab1SPhillip Lougher pg_offset += avail; 189e2780ab1SPhillip Lougher offset += avail; 190e2780ab1SPhillip Lougher } 191e2780ab1SPhillip Lougher offset = 0; 192e2780ab1SPhillip Lougher put_bh(bh[k]); 193e2780ab1SPhillip Lougher } 194e2780ab1SPhillip Lougher } 195e2780ab1SPhillip Lougher 196e2780ab1SPhillip Lougher kfree(bh); 197e2780ab1SPhillip Lougher return length; 198e2780ab1SPhillip Lougher 199e2780ab1SPhillip Lougher block_release: 200e2780ab1SPhillip Lougher for (; k < b; k++) 201e2780ab1SPhillip Lougher put_bh(bh[k]); 202e2780ab1SPhillip Lougher 203e2780ab1SPhillip Lougher read_failure: 204118e1ef6SPhillip Lougher ERROR("squashfs_read_data failed to read block 0x%llx\n", 205118e1ef6SPhillip Lougher (unsigned long long) index); 206e2780ab1SPhillip Lougher kfree(bh); 207e2780ab1SPhillip Lougher return -EIO; 208e2780ab1SPhillip Lougher } 209