block.c 9.2 KB

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