block.c 5.6 KB

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