block.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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/mutex.h>
  31. #include <linux/string.h>
  32. #include <linux/buffer_head.h>
  33. #include <linux/zlib.h>
  34. #include "squashfs_fs.h"
  35. #include "squashfs_fs_sb.h"
  36. #include "squashfs_fs_i.h"
  37. #include "squashfs.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. }
  63. return bh;
  64. }
  65. /*
  66. * Read and decompress a metadata block or datablock. Length is non-zero
  67. * if a datablock is being read (the size is stored elsewhere in the
  68. * filesystem), otherwise the length is obtained from the first two bytes of
  69. * the metadata block. A bit in the length field indicates if the block
  70. * is stored uncompressed in the filesystem (usually because compression
  71. * generated a larger block - this does occasionally happen with zlib).
  72. */
  73. int squashfs_read_data(struct super_block *sb, void **buffer, u64 index,
  74. int length, u64 *next_index, int srclength, int pages)
  75. {
  76. struct squashfs_sb_info *msblk = sb->s_fs_info;
  77. struct buffer_head **bh;
  78. int offset = index & ((1 << msblk->devblksize_log2) - 1);
  79. u64 cur_index = index >> msblk->devblksize_log2;
  80. int bytes, compressed, b = 0, k = 0, page = 0, avail;
  81. bh = kcalloc((msblk->block_size >> msblk->devblksize_log2) + 1,
  82. sizeof(*bh), GFP_KERNEL);
  83. if (bh == NULL)
  84. return -ENOMEM;
  85. if (length) {
  86. /*
  87. * Datablock.
  88. */
  89. bytes = -offset;
  90. compressed = SQUASHFS_COMPRESSED_BLOCK(length);
  91. length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length);
  92. if (next_index)
  93. *next_index = index + length;
  94. TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n",
  95. index, compressed ? "" : "un", length, srclength);
  96. if (length < 0 || length > srclength ||
  97. (index + length) > msblk->bytes_used)
  98. goto read_failure;
  99. for (b = 0; bytes < length; b++, cur_index++) {
  100. bh[b] = sb_getblk(sb, cur_index);
  101. if (bh[b] == NULL)
  102. goto block_release;
  103. bytes += msblk->devblksize;
  104. }
  105. ll_rw_block(READ, b, bh);
  106. } else {
  107. /*
  108. * Metadata block.
  109. */
  110. if ((index + 2) > msblk->bytes_used)
  111. goto read_failure;
  112. bh[0] = get_block_length(sb, &cur_index, &offset, &length);
  113. if (bh[0] == NULL)
  114. goto read_failure;
  115. b = 1;
  116. bytes = msblk->devblksize - offset;
  117. compressed = SQUASHFS_COMPRESSED(length);
  118. length = SQUASHFS_COMPRESSED_SIZE(length);
  119. if (next_index)
  120. *next_index = index + length + 2;
  121. TRACE("Block @ 0x%llx, %scompressed size %d\n", index,
  122. compressed ? "" : "un", length);
  123. if (length < 0 || length > srclength ||
  124. (index + length) > msblk->bytes_used)
  125. goto block_release;
  126. for (; bytes < length; b++) {
  127. bh[b] = sb_getblk(sb, ++cur_index);
  128. if (bh[b] == NULL)
  129. goto block_release;
  130. bytes += msblk->devblksize;
  131. }
  132. ll_rw_block(READ, b - 1, bh + 1);
  133. }
  134. if (compressed) {
  135. int zlib_err = 0, zlib_init = 0;
  136. /*
  137. * Uncompress block.
  138. */
  139. mutex_lock(&msblk->read_data_mutex);
  140. msblk->stream.avail_out = 0;
  141. msblk->stream.avail_in = 0;
  142. bytes = length;
  143. do {
  144. if (msblk->stream.avail_in == 0 && k < b) {
  145. avail = min(bytes, msblk->devblksize - offset);
  146. bytes -= avail;
  147. wait_on_buffer(bh[k]);
  148. if (!buffer_uptodate(bh[k]))
  149. goto release_mutex;
  150. if (avail == 0) {
  151. offset = 0;
  152. put_bh(bh[k++]);
  153. continue;
  154. }
  155. msblk->stream.next_in = bh[k]->b_data + offset;
  156. msblk->stream.avail_in = avail;
  157. offset = 0;
  158. }
  159. if (msblk->stream.avail_out == 0 && page < pages) {
  160. msblk->stream.next_out = buffer[page++];
  161. msblk->stream.avail_out = PAGE_CACHE_SIZE;
  162. }
  163. if (!zlib_init) {
  164. zlib_err = zlib_inflateInit(&msblk->stream);
  165. if (zlib_err != Z_OK) {
  166. ERROR("zlib_inflateInit returned"
  167. " unexpected result 0x%x,"
  168. " srclength %d\n", zlib_err,
  169. srclength);
  170. goto release_mutex;
  171. }
  172. zlib_init = 1;
  173. }
  174. zlib_err = zlib_inflate(&msblk->stream, Z_SYNC_FLUSH);
  175. if (msblk->stream.avail_in == 0 && k < b)
  176. put_bh(bh[k++]);
  177. } while (zlib_err == Z_OK);
  178. if (zlib_err != Z_STREAM_END) {
  179. ERROR("zlib_inflate error, data probably corrupt\n");
  180. goto release_mutex;
  181. }
  182. zlib_err = zlib_inflateEnd(&msblk->stream);
  183. if (zlib_err != Z_OK) {
  184. ERROR("zlib_inflate error, data probably corrupt\n");
  185. goto release_mutex;
  186. }
  187. length = msblk->stream.total_out;
  188. mutex_unlock(&msblk->read_data_mutex);
  189. } else {
  190. /*
  191. * Block is uncompressed.
  192. */
  193. int i, in, pg_offset = 0;
  194. for (i = 0; i < b; i++) {
  195. wait_on_buffer(bh[i]);
  196. if (!buffer_uptodate(bh[i]))
  197. goto block_release;
  198. }
  199. for (bytes = length; k < b; k++) {
  200. in = min(bytes, msblk->devblksize - offset);
  201. bytes -= in;
  202. while (in) {
  203. if (pg_offset == PAGE_CACHE_SIZE) {
  204. page++;
  205. pg_offset = 0;
  206. }
  207. avail = min_t(int, in, PAGE_CACHE_SIZE -
  208. pg_offset);
  209. memcpy(buffer[page] + pg_offset,
  210. bh[k]->b_data + offset, avail);
  211. in -= avail;
  212. pg_offset += avail;
  213. offset += avail;
  214. }
  215. offset = 0;
  216. put_bh(bh[k]);
  217. }
  218. }
  219. kfree(bh);
  220. return length;
  221. release_mutex:
  222. mutex_unlock(&msblk->read_data_mutex);
  223. block_release:
  224. for (; k < b; k++)
  225. put_bh(bh[k]);
  226. read_failure:
  227. ERROR("squashfs_read_data failed to read block 0x%llx\n",
  228. (unsigned long long) index);
  229. kfree(bh);
  230. return -EIO;
  231. }