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