block.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Squashfs - a compressed read only filesystem for Linux
  3. *
  4. * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
  5. * Phillip Lougher <phillip@lougher.demon.co.uk>
  6. *
  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.
  11. *
  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.
  16. *
  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.
  20. *
  21. * block.c
  22. */
  23. /*
  24. * This file implements the low-level routines to read and decompress
  25. * datablocks and metadata blocks.
  26. */
  27. #include <linux/fs.h>
  28. #include <linux/vfs.h>
  29. #include <linux/slab.h>
  30. #include <linux/string.h>
  31. #include <linux/buffer_head.h>
  32. #include "squashfs_fs.h"
  33. #include "squashfs_fs_sb.h"
  34. #include "squashfs_fs_i.h"
  35. #include "squashfs.h"
  36. #include "decompressor.h"
  37. /*
  38. * Read the metadata block length, this is stored in the first two
  39. * bytes of the metadata block.
  40. */
  41. static struct buffer_head *get_block_length(struct super_block *sb,
  42. u64 *cur_index, int *offset, int *length)
  43. {
  44. struct squashfs_sb_info *msblk = sb->s_fs_info;
  45. struct buffer_head *bh;
  46. bh = sb_bread(sb, *cur_index);
  47. if (bh == NULL)
  48. return NULL;
  49. if (msblk->devblksize - *offset == 1) {
  50. *length = (unsigned char) bh->b_data[*offset];
  51. put_bh(bh);
  52. bh = sb_bread(sb, ++(*cur_index));
  53. if (bh == NULL)
  54. return NULL;
  55. *length |= (unsigned char) bh->b_data[0] << 8;
  56. *offset = 1;
  57. } else {
  58. *length = (unsigned char) bh->b_data[*offset] |
  59. (unsigned char) bh->b_data[*offset + 1] << 8;
  60. *offset += 2;
  61. }
  62. return bh;
  63. }
  64. /*
  65. * Read and decompress a metadata block or datablock. Length is non-zero
  66. * if a datablock is being read (the size is stored elsewhere in the
  67. * filesystem), otherwise the length is obtained from the first two bytes of
  68. * the metadata block. A bit in the length field indicates if the block
  69. * is stored uncompressed in the filesystem (usually because compression
  70. * generated a larger block - this does occasionally happen with zlib).
  71. */
  72. int squashfs_read_data(struct super_block *sb, void **buffer, u64 index,
  73. int length, u64 *next_index, int srclength, int pages)
  74. {
  75. struct squashfs_sb_info *msblk = sb->s_fs_info;
  76. struct buffer_head **bh;
  77. int offset = index & ((1 << msblk->devblksize_log2) - 1);
  78. u64 cur_index = index >> msblk->devblksize_log2;
  79. int bytes, compressed, b = 0, k = 0, page = 0, avail;
  80. bh = kcalloc(((srclength + msblk->devblksize - 1)
  81. >> msblk->devblksize_log2) + 1, sizeof(*bh), GFP_KERNEL);
  82. if (bh == NULL)
  83. return -ENOMEM;
  84. if (length) {
  85. /*
  86. * Datablock.
  87. */
  88. bytes = -offset;
  89. compressed = SQUASHFS_COMPRESSED_BLOCK(length);
  90. length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length);
  91. if (next_index)
  92. *next_index = index + length;
  93. TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n",
  94. index, compressed ? "" : "un", length, srclength);
  95. if (length < 0 || length > srclength ||
  96. (index + length) > msblk->bytes_used)
  97. goto read_failure;
  98. for (b = 0; bytes < length; b++, cur_index++) {
  99. bh[b] = sb_getblk(sb, cur_index);
  100. if (bh[b] == NULL)
  101. goto block_release;
  102. bytes += msblk->devblksize;
  103. }
  104. ll_rw_block(READ, b, bh);
  105. } else {
  106. /*
  107. * Metadata block.
  108. */
  109. if ((index + 2) > msblk->bytes_used)
  110. goto read_failure;
  111. bh[0] = get_block_length(sb, &cur_index, &offset, &length);
  112. if (bh[0] == NULL)
  113. goto read_failure;
  114. b = 1;
  115. bytes = msblk->devblksize - offset;
  116. compressed = SQUASHFS_COMPRESSED(length);
  117. length = SQUASHFS_COMPRESSED_SIZE(length);
  118. if (next_index)
  119. *next_index = index + length + 2;
  120. TRACE("Block @ 0x%llx, %scompressed size %d\n", index,
  121. compressed ? "" : "un", length);
  122. if (length < 0 || length > srclength ||
  123. (index + length) > msblk->bytes_used)
  124. goto block_release;
  125. for (; bytes < length; b++) {
  126. bh[b] = sb_getblk(sb, ++cur_index);
  127. if (bh[b] == NULL)
  128. goto block_release;
  129. bytes += msblk->devblksize;
  130. }
  131. ll_rw_block(READ, b - 1, bh + 1);
  132. }
  133. if (compressed) {
  134. length = squashfs_decompress(msblk, buffer, bh, b, offset,
  135. length, srclength, pages);
  136. if (length < 0)
  137. goto read_failure;
  138. } else {
  139. /*
  140. * Block is uncompressed.
  141. */
  142. int i, in, pg_offset = 0;
  143. for (i = 0; i < b; i++) {
  144. wait_on_buffer(bh[i]);
  145. if (!buffer_uptodate(bh[i]))
  146. goto block_release;
  147. }
  148. for (bytes = length; k < b; k++) {
  149. in = min(bytes, msblk->devblksize - offset);
  150. bytes -= in;
  151. while (in) {
  152. if (pg_offset == PAGE_CACHE_SIZE) {
  153. page++;
  154. pg_offset = 0;
  155. }
  156. avail = min_t(int, in, PAGE_CACHE_SIZE -
  157. pg_offset);
  158. memcpy(buffer[page] + pg_offset,
  159. bh[k]->b_data + offset, avail);
  160. in -= avail;
  161. pg_offset += avail;
  162. offset += avail;
  163. }
  164. offset = 0;
  165. put_bh(bh[k]);
  166. }
  167. }
  168. kfree(bh);
  169. return length;
  170. block_release:
  171. for (; k < b; k++)
  172. put_bh(bh[k]);
  173. read_failure:
  174. ERROR("squashfs_read_data failed to read block 0x%llx\n",
  175. (unsigned long long) index);
  176. kfree(bh);
  177. return -EIO;
  178. }