block.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 <phillip@squashfs.org.uk>
  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/pagemap.h>
  19. #include <linux/string.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_cached(struct bio *fullbio,
  69. struct address_space *cache_mapping, u64 index, int length,
  70. u64 read_start, u64 read_end, int page_count)
  71. {
  72. struct page *head_to_cache = NULL, *tail_to_cache = NULL;
  73. struct block_device *bdev = fullbio->bi_bdev;
  74. int start_idx = 0, end_idx = 0;
  75. struct bvec_iter_all iter_all;
  76. struct bio *bio = NULL;
  77. struct bio_vec *bv;
  78. int idx = 0;
  79. int err = 0;
  80. bio_for_each_segment_all(bv, fullbio, iter_all) {
  81. struct page *page = bv->bv_page;
  82. if (page->mapping == cache_mapping) {
  83. idx++;
  84. continue;
  85. }
  86. /*
  87. * We only use this when the device block size is the same as
  88. * the page size, so read_start and read_end cover full pages.
  89. *
  90. * Compare these to the original required index and length to
  91. * only cache pages which were requested partially, since these
  92. * are the ones which are likely to be needed when reading
  93. * adjacent blocks.
  94. */
  95. if (idx == 0 && index != read_start)
  96. head_to_cache = page;
  97. else if (idx == page_count - 1 && index + length != read_end)
  98. tail_to_cache = page;
  99. if (!bio || idx != end_idx) {
  100. struct bio *new = bio_alloc_clone(bdev, fullbio,
  101. GFP_NOIO, &fs_bio_set);
  102. if (bio) {
  103. bio_trim(bio, start_idx * PAGE_SECTORS,
  104. (end_idx - start_idx) * PAGE_SECTORS);
  105. bio_chain(bio, new);
  106. submit_bio(bio);
  107. }
  108. bio = new;
  109. start_idx = idx;
  110. }
  111. idx++;
  112. end_idx = idx;
  113. }
  114. if (bio) {
  115. bio_trim(bio, start_idx * PAGE_SECTORS,
  116. (end_idx - start_idx) * PAGE_SECTORS);
  117. err = submit_bio_wait(bio);
  118. bio_put(bio);
  119. }
  120. if (err)
  121. return err;
  122. if (head_to_cache) {
  123. int ret = add_to_page_cache_lru(head_to_cache, cache_mapping,
  124. read_start >> PAGE_SHIFT,
  125. GFP_NOIO);
  126. if (!ret) {
  127. SetPageUptodate(head_to_cache);
  128. unlock_page(head_to_cache);
  129. }
  130. }
  131. if (tail_to_cache) {
  132. int ret = add_to_page_cache_lru(tail_to_cache, cache_mapping,
  133. (read_end >> PAGE_SHIFT) - 1,
  134. GFP_NOIO);
  135. if (!ret) {
  136. SetPageUptodate(tail_to_cache);
  137. unlock_page(tail_to_cache);
  138. }
  139. }
  140. return 0;
  141. }
  142. static struct page *squashfs_get_cache_page(struct address_space *mapping,
  143. pgoff_t index)
  144. {
  145. struct page *page;
  146. if (!mapping)
  147. return NULL;
  148. page = find_get_page(mapping, index);
  149. if (!page)
  150. return NULL;
  151. if (!PageUptodate(page)) {
  152. put_page(page);
  153. return NULL;
  154. }
  155. return page;
  156. }
  157. static int squashfs_bio_read(struct super_block *sb, u64 index, int length,
  158. struct bio **biop, int *block_offset)
  159. {
  160. struct squashfs_sb_info *msblk = sb->s_fs_info;
  161. struct address_space *cache_mapping = msblk->cache_mapping;
  162. const u64 read_start = round_down(index, msblk->devblksize);
  163. const sector_t block = read_start >> msblk->devblksize_log2;
  164. const u64 read_end = round_up(index + length, msblk->devblksize);
  165. const sector_t block_end = read_end >> msblk->devblksize_log2;
  166. int offset = read_start - round_down(index, PAGE_SIZE);
  167. int total_len = (block_end - block) << msblk->devblksize_log2;
  168. const int page_count = DIV_ROUND_UP(total_len + offset, PAGE_SIZE);
  169. int error, i;
  170. struct bio *bio;
  171. bio = bio_kmalloc(page_count, GFP_NOIO);
  172. if (!bio)
  173. return -ENOMEM;
  174. bio_init(bio, sb->s_bdev, bio->bi_inline_vecs, page_count, REQ_OP_READ);
  175. bio->bi_iter.bi_sector = block * (msblk->devblksize >> SECTOR_SHIFT);
  176. for (i = 0; i < page_count; ++i) {
  177. unsigned int len =
  178. min_t(unsigned int, PAGE_SIZE - offset, total_len);
  179. pgoff_t index = (read_start >> PAGE_SHIFT) + i;
  180. struct page *page;
  181. page = squashfs_get_cache_page(cache_mapping, index);
  182. if (!page)
  183. page = alloc_page(GFP_NOIO);
  184. if (!page) {
  185. error = -ENOMEM;
  186. goto out_free_bio;
  187. }
  188. /*
  189. * Use the __ version to avoid merging since we need each page
  190. * to be separate when we check for and avoid cached pages.
  191. */
  192. __bio_add_page(bio, page, len, offset);
  193. offset = 0;
  194. total_len -= len;
  195. }
  196. if (cache_mapping)
  197. error = squashfs_bio_read_cached(bio, cache_mapping, index,
  198. length, read_start, read_end,
  199. page_count);
  200. else
  201. error = submit_bio_wait(bio);
  202. if (error)
  203. goto out_free_bio;
  204. *biop = bio;
  205. *block_offset = index & ((1 << msblk->devblksize_log2) - 1);
  206. return 0;
  207. out_free_bio:
  208. bio_free_pages(bio);
  209. bio_uninit(bio);
  210. kfree(bio);
  211. return error;
  212. }
  213. /*
  214. * Read and decompress a metadata block or datablock. Length is non-zero
  215. * if a datablock is being read (the size is stored elsewhere in the
  216. * filesystem), otherwise the length is obtained from the first two bytes of
  217. * the metadata block. A bit in the length field indicates if the block
  218. * is stored uncompressed in the filesystem (usually because compression
  219. * generated a larger block - this does occasionally happen with compression
  220. * algorithms).
  221. */
  222. int squashfs_read_data(struct super_block *sb, u64 index, int length,
  223. u64 *next_index, struct squashfs_page_actor *output)
  224. {
  225. struct squashfs_sb_info *msblk = sb->s_fs_info;
  226. struct bio *bio = NULL;
  227. int compressed;
  228. int res;
  229. int offset;
  230. if (length) {
  231. /*
  232. * Datablock.
  233. */
  234. compressed = SQUASHFS_COMPRESSED_BLOCK(length);
  235. length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length);
  236. TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n",
  237. index, compressed ? "" : "un", length, output->length);
  238. } else {
  239. /*
  240. * Metadata block.
  241. */
  242. const u8 *data;
  243. struct bvec_iter_all iter_all = {};
  244. struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
  245. if (index + 2 > msblk->bytes_used) {
  246. res = -EIO;
  247. goto out;
  248. }
  249. res = squashfs_bio_read(sb, index, 2, &bio, &offset);
  250. if (res)
  251. goto out;
  252. if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all))) {
  253. res = -EIO;
  254. goto out_free_bio;
  255. }
  256. /* Extract the length of the metadata block */
  257. data = bvec_virt(bvec);
  258. length = data[offset];
  259. if (offset < bvec->bv_len - 1) {
  260. length |= data[offset + 1] << 8;
  261. } else {
  262. if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all))) {
  263. res = -EIO;
  264. goto out_free_bio;
  265. }
  266. data = bvec_virt(bvec);
  267. length |= data[0] << 8;
  268. }
  269. bio_free_pages(bio);
  270. bio_uninit(bio);
  271. kfree(bio);
  272. compressed = SQUASHFS_COMPRESSED(length);
  273. length = SQUASHFS_COMPRESSED_SIZE(length);
  274. index += 2;
  275. TRACE("Block @ 0x%llx, %scompressed size %d\n", index - 2,
  276. compressed ? "" : "un", length);
  277. }
  278. if (length <= 0 || length > output->length ||
  279. (index + length) > msblk->bytes_used) {
  280. res = -EIO;
  281. goto out;
  282. }
  283. if (next_index)
  284. *next_index = index + length;
  285. res = squashfs_bio_read(sb, index, length, &bio, &offset);
  286. if (res)
  287. goto out;
  288. if (compressed) {
  289. if (!msblk->stream) {
  290. res = -EIO;
  291. goto out_free_bio;
  292. }
  293. res = msblk->thread_ops->decompress(msblk, bio, offset, length, output);
  294. } else {
  295. res = copy_bio_to_actor(bio, output, offset, length);
  296. }
  297. out_free_bio:
  298. bio_free_pages(bio);
  299. bio_uninit(bio);
  300. kfree(bio);
  301. out:
  302. if (res < 0) {
  303. ERROR("Failed to read block 0x%llx: %d\n", index, res);
  304. if (msblk->panic_on_errors)
  305. panic("squashfs read failed");
  306. }
  307. return res;
  308. }