block.c 5.6 KB

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