block.c 5.4 KB

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