block.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Squashfs - a compressed read only filesystem for Linux
  4. *
  5. * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
  6. * Phillip Lougher <[email protected]>
  7. *
  8. * block.c
  9. */
  10. /*
  11. * This file implements the low-level routines to read and decompress
  12. * datablocks and metadata blocks.
  13. */
  14. #include <linux/blkdev.h>
  15. #include <linux/fs.h>
  16. #include <linux/vfs.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include <linux/buffer_head.h>
  20. #include <linux/bio.h>
  21. #include "squashfs_fs.h"
  22. #include "squashfs_fs_sb.h"
  23. #include "squashfs.h"
  24. #include "decompressor.h"
  25. #include "page_actor.h"
  26. /*
  27. * Returns the amount of bytes copied to the page actor.
  28. */
  29. static int copy_bio_to_actor(struct bio *bio,
  30. struct squashfs_page_actor *actor,
  31. int offset, int req_length)
  32. {
  33. void *actor_addr;
  34. struct bvec_iter_all iter_all = {};
  35. struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
  36. int copied_bytes = 0;
  37. int actor_offset = 0;
  38. squashfs_actor_nobuff(actor);
  39. actor_addr = squashfs_first_page(actor);
  40. if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all)))
  41. return 0;
  42. while (copied_bytes < req_length) {
  43. int bytes_to_copy = min_t(int, bvec->bv_len - offset,
  44. PAGE_SIZE - actor_offset);
  45. bytes_to_copy = min_t(int, bytes_to_copy,
  46. req_length - copied_bytes);
  47. if (!IS_ERR(actor_addr))
  48. memcpy(actor_addr + actor_offset, bvec_virt(bvec) +
  49. offset, bytes_to_copy);
  50. actor_offset += bytes_to_copy;
  51. copied_bytes += bytes_to_copy;
  52. offset += bytes_to_copy;
  53. if (actor_offset >= PAGE_SIZE) {
  54. actor_addr = squashfs_next_page(actor);
  55. if (!actor_addr)
  56. break;
  57. actor_offset = 0;
  58. }
  59. if (offset >= bvec->bv_len) {
  60. if (!bio_next_segment(bio, &iter_all))
  61. break;
  62. offset = 0;
  63. }
  64. }
  65. squashfs_finish_page(actor);
  66. return copied_bytes;
  67. }
  68. static int squashfs_bio_read(struct super_block *sb, u64 index, int length,
  69. struct bio **biop, int *block_offset)
  70. {
  71. struct squashfs_sb_info *msblk = sb->s_fs_info;
  72. const u64 read_start = round_down(index, msblk->devblksize);
  73. const sector_t block = read_start >> msblk->devblksize_log2;
  74. const u64 read_end = round_up(index + length, msblk->devblksize);
  75. const sector_t block_end = read_end >> msblk->devblksize_log2;
  76. int offset = read_start - round_down(index, PAGE_SIZE);
  77. int total_len = (block_end - block) << msblk->devblksize_log2;
  78. const int page_count = DIV_ROUND_UP(total_len + offset, PAGE_SIZE);
  79. int error, i;
  80. struct bio *bio;
  81. bio = bio_kmalloc(page_count, GFP_NOIO);
  82. if (!bio)
  83. return -ENOMEM;
  84. bio_init(bio, sb->s_bdev, bio->bi_inline_vecs, page_count, REQ_OP_READ);
  85. bio->bi_iter.bi_sector = block * (msblk->devblksize >> SECTOR_SHIFT);
  86. for (i = 0; i < page_count; ++i) {
  87. unsigned int len =
  88. min_t(unsigned int, PAGE_SIZE - offset, total_len);
  89. struct page *page = alloc_page(GFP_NOIO);
  90. if (!page) {
  91. error = -ENOMEM;
  92. goto out_free_bio;
  93. }
  94. if (!bio_add_page(bio, page, len, offset)) {
  95. error = -EIO;
  96. goto out_free_bio;
  97. }
  98. offset = 0;
  99. total_len -= len;
  100. }
  101. error = submit_bio_wait(bio);
  102. if (error)
  103. goto out_free_bio;
  104. *biop = bio;
  105. *block_offset = index & ((1 << msblk->devblksize_log2) - 1);
  106. return 0;
  107. out_free_bio:
  108. bio_free_pages(bio);
  109. bio_uninit(bio);
  110. kfree(bio);
  111. return error;
  112. }
  113. /*
  114. * Read and decompress a metadata block or datablock. Length is non-zero
  115. * if a datablock is being read (the size is stored elsewhere in the
  116. * filesystem), otherwise the length is obtained from the first two bytes of
  117. * the metadata block. A bit in the length field indicates if the block
  118. * is stored uncompressed in the filesystem (usually because compression
  119. * generated a larger block - this does occasionally happen with compression
  120. * algorithms).
  121. */
  122. int squashfs_read_data(struct super_block *sb, u64 index, int length,
  123. u64 *next_index, struct squashfs_page_actor *output)
  124. {
  125. struct squashfs_sb_info *msblk = sb->s_fs_info;
  126. struct bio *bio = NULL;
  127. int compressed;
  128. int res;
  129. int offset;
  130. if (length) {
  131. /*
  132. * Datablock.
  133. */
  134. compressed = SQUASHFS_COMPRESSED_BLOCK(length);
  135. length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length);
  136. TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n",
  137. index, compressed ? "" : "un", length, output->length);
  138. } else {
  139. /*
  140. * Metadata block.
  141. */
  142. const u8 *data;
  143. struct bvec_iter_all iter_all = {};
  144. struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
  145. if (index + 2 > msblk->bytes_used) {
  146. res = -EIO;
  147. goto out;
  148. }
  149. res = squashfs_bio_read(sb, index, 2, &bio, &offset);
  150. if (res)
  151. goto out;
  152. if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all))) {
  153. res = -EIO;
  154. goto out_free_bio;
  155. }
  156. /* Extract the length of the metadata block */
  157. data = bvec_virt(bvec);
  158. length = data[offset];
  159. if (offset < bvec->bv_len - 1) {
  160. length |= data[offset + 1] << 8;
  161. } else {
  162. if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all))) {
  163. res = -EIO;
  164. goto out_free_bio;
  165. }
  166. data = bvec_virt(bvec);
  167. length |= data[0] << 8;
  168. }
  169. bio_free_pages(bio);
  170. bio_uninit(bio);
  171. kfree(bio);
  172. compressed = SQUASHFS_COMPRESSED(length);
  173. length = SQUASHFS_COMPRESSED_SIZE(length);
  174. index += 2;
  175. TRACE("Block @ 0x%llx, %scompressed size %d\n", index - 2,
  176. compressed ? "" : "un", length);
  177. }
  178. if (length < 0 || length > output->length ||
  179. (index + length) > msblk->bytes_used) {
  180. res = -EIO;
  181. goto out;
  182. }
  183. if (next_index)
  184. *next_index = index + length;
  185. res = squashfs_bio_read(sb, index, length, &bio, &offset);
  186. if (res)
  187. goto out;
  188. if (compressed) {
  189. if (!msblk->stream) {
  190. res = -EIO;
  191. goto out_free_bio;
  192. }
  193. res = squashfs_decompress(msblk, bio, offset, length, output);
  194. } else {
  195. res = copy_bio_to_actor(bio, output, offset, length);
  196. }
  197. out_free_bio:
  198. bio_free_pages(bio);
  199. bio_uninit(bio);
  200. kfree(bio);
  201. out:
  202. if (res < 0) {
  203. ERROR("Failed to read block 0x%llx: %d\n", index, res);
  204. if (msblk->panic_on_errors)
  205. panic("squashfs read failed");
  206. }
  207. return res;
  208. }