block.c 5.3 KB

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