readpage.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext4/readpage.c
  4. *
  5. * Copyright (C) 2002, Linus Torvalds.
  6. * Copyright (C) 2015, Google, Inc.
  7. *
  8. * This was originally taken from fs/mpage.c
  9. *
  10. * The ext4_mpage_readpages() function here is intended to
  11. * replace mpage_readahead() in the general case, not just for
  12. * encrypted files. It has some limitations (see below), where it
  13. * will fall back to read_block_full_page(), but these limitations
  14. * should only be hit when page_size != block_size.
  15. *
  16. * This will allow us to attach a callback function to support ext4
  17. * encryption.
  18. *
  19. * If anything unusual happens, such as:
  20. *
  21. * - encountering a page which has buffers
  22. * - encountering a page which has a non-hole after a hole
  23. * - encountering a page with non-contiguous blocks
  24. *
  25. * then this code just gives up and calls the buffer_head-based read function.
  26. * It does handle a page which has holes at the end - that is a common case:
  27. * the end-of-file on blocksize < PAGE_SIZE setups.
  28. *
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/export.h>
  32. #include <linux/mm.h>
  33. #include <linux/kdev_t.h>
  34. #include <linux/gfp.h>
  35. #include <linux/bio.h>
  36. #include <linux/fs.h>
  37. #include <linux/buffer_head.h>
  38. #include <linux/blk-crypto.h>
  39. #include <linux/blkdev.h>
  40. #include <linux/highmem.h>
  41. #include <linux/prefetch.h>
  42. #include <linux/mpage.h>
  43. #include <linux/writeback.h>
  44. #include <linux/backing-dev.h>
  45. #include <linux/pagevec.h>
  46. #include "ext4.h"
  47. #include <trace/events/ext4.h>
  48. #define NUM_PREALLOC_POST_READ_CTXS 128
  49. static struct kmem_cache *bio_post_read_ctx_cache;
  50. static mempool_t *bio_post_read_ctx_pool;
  51. /* postprocessing steps for read bios */
  52. enum bio_post_read_step {
  53. STEP_INITIAL = 0,
  54. STEP_DECRYPT,
  55. STEP_VERITY,
  56. STEP_MAX,
  57. };
  58. struct bio_post_read_ctx {
  59. struct bio *bio;
  60. struct fsverity_info *vi;
  61. struct work_struct work;
  62. unsigned int cur_step;
  63. unsigned int enabled_steps;
  64. };
  65. static void __read_end_io(struct bio *bio)
  66. {
  67. struct folio_iter fi;
  68. bio_for_each_folio_all(fi, bio)
  69. folio_end_read(fi.folio, bio->bi_status == 0);
  70. if (bio->bi_private)
  71. mempool_free(bio->bi_private, bio_post_read_ctx_pool);
  72. bio_put(bio);
  73. }
  74. static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
  75. static void decrypt_work(struct work_struct *work)
  76. {
  77. struct bio_post_read_ctx *ctx =
  78. container_of(work, struct bio_post_read_ctx, work);
  79. struct bio *bio = ctx->bio;
  80. if (fscrypt_decrypt_bio(bio))
  81. bio_post_read_processing(ctx);
  82. else
  83. __read_end_io(bio);
  84. }
  85. static void verity_work(struct work_struct *work)
  86. {
  87. struct bio_post_read_ctx *ctx =
  88. container_of(work, struct bio_post_read_ctx, work);
  89. struct bio *bio = ctx->bio;
  90. struct fsverity_info *vi = ctx->vi;
  91. /*
  92. * fsverity_verify_bio() may call readahead() again, and although verity
  93. * will be disabled for that, decryption may still be needed, causing
  94. * another bio_post_read_ctx to be allocated. So to guarantee that
  95. * mempool_alloc() never deadlocks we must free the current ctx first.
  96. * This is safe because verity is the last post-read step.
  97. */
  98. BUILD_BUG_ON(STEP_VERITY + 1 != STEP_MAX);
  99. mempool_free(ctx, bio_post_read_ctx_pool);
  100. bio->bi_private = NULL;
  101. fsverity_verify_bio(vi, bio);
  102. __read_end_io(bio);
  103. }
  104. static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
  105. {
  106. /*
  107. * We use different work queues for decryption and for verity because
  108. * verity may require reading metadata pages that need decryption, and
  109. * we shouldn't recurse to the same workqueue.
  110. */
  111. switch (++ctx->cur_step) {
  112. case STEP_DECRYPT:
  113. if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
  114. INIT_WORK(&ctx->work, decrypt_work);
  115. fscrypt_enqueue_decrypt_work(&ctx->work);
  116. return;
  117. }
  118. ctx->cur_step++;
  119. fallthrough;
  120. case STEP_VERITY:
  121. if (IS_ENABLED(CONFIG_FS_VERITY) &&
  122. ctx->enabled_steps & (1 << STEP_VERITY)) {
  123. INIT_WORK(&ctx->work, verity_work);
  124. fsverity_enqueue_verify_work(&ctx->work);
  125. return;
  126. }
  127. ctx->cur_step++;
  128. fallthrough;
  129. default:
  130. __read_end_io(ctx->bio);
  131. }
  132. }
  133. static bool bio_post_read_required(struct bio *bio)
  134. {
  135. return bio->bi_private && !bio->bi_status;
  136. }
  137. /*
  138. * I/O completion handler for multipage BIOs.
  139. *
  140. * The mpage code never puts partial pages into a BIO (except for end-of-file).
  141. * If a page does not map to a contiguous run of blocks then it simply falls
  142. * back to block_read_full_folio().
  143. *
  144. * Why is this? If a page's completion depends on a number of different BIOs
  145. * which can complete in any order (or at the same time) then determining the
  146. * status of that page is hard. See end_buffer_async_read() for the details.
  147. * There is no point in duplicating all that complexity.
  148. */
  149. static void mpage_end_io(struct bio *bio)
  150. {
  151. if (bio_post_read_required(bio)) {
  152. struct bio_post_read_ctx *ctx = bio->bi_private;
  153. ctx->cur_step = STEP_INITIAL;
  154. bio_post_read_processing(ctx);
  155. return;
  156. }
  157. __read_end_io(bio);
  158. }
  159. static void ext4_set_bio_post_read_ctx(struct bio *bio,
  160. const struct inode *inode,
  161. struct fsverity_info *vi)
  162. {
  163. unsigned int post_read_steps = 0;
  164. if (fscrypt_inode_uses_fs_layer_crypto(inode))
  165. post_read_steps |= 1 << STEP_DECRYPT;
  166. if (vi)
  167. post_read_steps |= 1 << STEP_VERITY;
  168. if (post_read_steps) {
  169. /* Due to the mempool, this never fails. */
  170. struct bio_post_read_ctx *ctx =
  171. mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
  172. ctx->bio = bio;
  173. ctx->vi = vi;
  174. ctx->enabled_steps = post_read_steps;
  175. bio->bi_private = ctx;
  176. }
  177. }
  178. static inline loff_t ext4_readpage_limit(struct inode *inode)
  179. {
  180. if (IS_ENABLED(CONFIG_FS_VERITY) && IS_VERITY(inode))
  181. return inode->i_sb->s_maxbytes;
  182. return i_size_read(inode);
  183. }
  184. static int ext4_mpage_readpages(struct inode *inode, struct fsverity_info *vi,
  185. struct readahead_control *rac, struct folio *folio)
  186. {
  187. struct bio *bio = NULL;
  188. sector_t last_block_in_bio = 0;
  189. const unsigned blkbits = inode->i_blkbits;
  190. const unsigned blocksize = 1 << blkbits;
  191. sector_t next_block;
  192. sector_t block_in_file;
  193. sector_t last_block;
  194. sector_t last_block_in_file;
  195. sector_t first_block;
  196. unsigned page_block;
  197. struct block_device *bdev = inode->i_sb->s_bdev;
  198. int length;
  199. unsigned relative_block = 0;
  200. struct ext4_map_blocks map;
  201. unsigned int nr_pages, folio_pages;
  202. map.m_pblk = 0;
  203. map.m_lblk = 0;
  204. map.m_len = 0;
  205. map.m_flags = 0;
  206. nr_pages = rac ? readahead_count(rac) : folio_nr_pages(folio);
  207. for (; nr_pages; nr_pages -= folio_pages) {
  208. int fully_mapped = 1;
  209. unsigned int first_hole;
  210. unsigned int blocks_per_folio;
  211. if (rac)
  212. folio = readahead_folio(rac);
  213. folio_pages = folio_nr_pages(folio);
  214. prefetchw(&folio->flags);
  215. if (folio_buffers(folio))
  216. goto confused;
  217. blocks_per_folio = folio_size(folio) >> blkbits;
  218. first_hole = blocks_per_folio;
  219. block_in_file = next_block = EXT4_PG_TO_LBLK(inode, folio->index);
  220. last_block = EXT4_PG_TO_LBLK(inode, folio->index + nr_pages);
  221. last_block_in_file = (ext4_readpage_limit(inode) +
  222. blocksize - 1) >> blkbits;
  223. if (last_block > last_block_in_file)
  224. last_block = last_block_in_file;
  225. page_block = 0;
  226. /*
  227. * Map blocks using the previous result first.
  228. */
  229. if ((map.m_flags & EXT4_MAP_MAPPED) &&
  230. block_in_file > map.m_lblk &&
  231. block_in_file < (map.m_lblk + map.m_len)) {
  232. unsigned map_offset = block_in_file - map.m_lblk;
  233. unsigned last = map.m_len - map_offset;
  234. first_block = map.m_pblk + map_offset;
  235. for (relative_block = 0; ; relative_block++) {
  236. if (relative_block == last) {
  237. /* needed? */
  238. map.m_flags &= ~EXT4_MAP_MAPPED;
  239. break;
  240. }
  241. if (page_block == blocks_per_folio)
  242. break;
  243. page_block++;
  244. block_in_file++;
  245. }
  246. }
  247. /*
  248. * Then do more ext4_map_blocks() calls until we are
  249. * done with this folio.
  250. */
  251. while (page_block < blocks_per_folio) {
  252. if (block_in_file < last_block) {
  253. map.m_lblk = block_in_file;
  254. map.m_len = last_block - block_in_file;
  255. if (ext4_map_blocks(NULL, inode, &map, 0) < 0) {
  256. set_error_page:
  257. folio_zero_segment(folio, 0,
  258. folio_size(folio));
  259. folio_unlock(folio);
  260. goto next_page;
  261. }
  262. }
  263. if ((map.m_flags & EXT4_MAP_MAPPED) == 0) {
  264. fully_mapped = 0;
  265. if (first_hole == blocks_per_folio)
  266. first_hole = page_block;
  267. page_block++;
  268. block_in_file++;
  269. continue;
  270. }
  271. if (first_hole != blocks_per_folio)
  272. goto confused; /* hole -> non-hole */
  273. /* Contiguous blocks? */
  274. if (!page_block)
  275. first_block = map.m_pblk;
  276. else if (first_block + page_block != map.m_pblk)
  277. goto confused;
  278. for (relative_block = 0; ; relative_block++) {
  279. if (relative_block == map.m_len) {
  280. /* needed? */
  281. map.m_flags &= ~EXT4_MAP_MAPPED;
  282. break;
  283. } else if (page_block == blocks_per_folio)
  284. break;
  285. page_block++;
  286. block_in_file++;
  287. }
  288. }
  289. if (first_hole != blocks_per_folio) {
  290. folio_zero_segment(folio, first_hole << blkbits,
  291. folio_size(folio));
  292. if (first_hole == 0) {
  293. if (vi && !fsverity_verify_folio(vi, folio))
  294. goto set_error_page;
  295. folio_end_read(folio, true);
  296. continue;
  297. }
  298. } else if (fully_mapped) {
  299. folio_set_mappedtodisk(folio);
  300. }
  301. /*
  302. * This folio will go to BIO. Do we need to send this
  303. * BIO off first?
  304. */
  305. if (bio && (last_block_in_bio != first_block - 1 ||
  306. !fscrypt_mergeable_bio(bio, inode, next_block))) {
  307. submit_and_realloc:
  308. blk_crypto_submit_bio(bio);
  309. bio = NULL;
  310. }
  311. if (bio == NULL) {
  312. /*
  313. * bio_alloc will _always_ be able to allocate a bio if
  314. * __GFP_DIRECT_RECLAIM is set, see bio_alloc_bioset().
  315. */
  316. bio = bio_alloc(bdev, bio_max_segs(nr_pages),
  317. REQ_OP_READ, GFP_KERNEL);
  318. fscrypt_set_bio_crypt_ctx(bio, inode, next_block,
  319. GFP_KERNEL);
  320. ext4_set_bio_post_read_ctx(bio, inode, vi);
  321. bio->bi_iter.bi_sector = first_block << (blkbits - 9);
  322. bio->bi_end_io = mpage_end_io;
  323. if (rac)
  324. bio->bi_opf |= REQ_RAHEAD;
  325. }
  326. length = first_hole << blkbits;
  327. if (!bio_add_folio(bio, folio, length, 0))
  328. goto submit_and_realloc;
  329. if (((map.m_flags & EXT4_MAP_BOUNDARY) &&
  330. (relative_block == map.m_len)) ||
  331. (first_hole != blocks_per_folio)) {
  332. blk_crypto_submit_bio(bio);
  333. bio = NULL;
  334. } else
  335. last_block_in_bio = first_block + blocks_per_folio - 1;
  336. continue;
  337. confused:
  338. if (bio) {
  339. blk_crypto_submit_bio(bio);
  340. bio = NULL;
  341. }
  342. if (!folio_test_uptodate(folio))
  343. block_read_full_folio(folio, ext4_get_block);
  344. else
  345. folio_unlock(folio);
  346. next_page:
  347. ; /* A label shall be followed by a statement until C23 */
  348. }
  349. if (bio)
  350. blk_crypto_submit_bio(bio);
  351. return 0;
  352. }
  353. int ext4_read_folio(struct file *file, struct folio *folio)
  354. {
  355. struct inode *inode = folio->mapping->host;
  356. struct fsverity_info *vi = NULL;
  357. int ret;
  358. trace_ext4_read_folio(inode, folio);
  359. if (ext4_has_inline_data(inode)) {
  360. ret = ext4_readpage_inline(inode, folio);
  361. if (ret != -EAGAIN)
  362. return ret;
  363. }
  364. if (folio->index < DIV_ROUND_UP(inode->i_size, PAGE_SIZE))
  365. vi = fsverity_get_info(inode);
  366. if (vi)
  367. fsverity_readahead(vi, folio->index, folio_nr_pages(folio));
  368. return ext4_mpage_readpages(inode, vi, NULL, folio);
  369. }
  370. void ext4_readahead(struct readahead_control *rac)
  371. {
  372. struct inode *inode = rac->mapping->host;
  373. struct fsverity_info *vi = NULL;
  374. /* If the file has inline data, no need to do readahead. */
  375. if (ext4_has_inline_data(inode))
  376. return;
  377. if (readahead_index(rac) < DIV_ROUND_UP(inode->i_size, PAGE_SIZE))
  378. vi = fsverity_get_info(inode);
  379. if (vi)
  380. fsverity_readahead(vi, readahead_index(rac),
  381. readahead_count(rac));
  382. ext4_mpage_readpages(inode, vi, rac, NULL);
  383. }
  384. int __init ext4_init_post_read_processing(void)
  385. {
  386. bio_post_read_ctx_cache = KMEM_CACHE(bio_post_read_ctx, SLAB_RECLAIM_ACCOUNT);
  387. if (!bio_post_read_ctx_cache)
  388. goto fail;
  389. bio_post_read_ctx_pool =
  390. mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
  391. bio_post_read_ctx_cache);
  392. if (!bio_post_read_ctx_pool)
  393. goto fail_free_cache;
  394. return 0;
  395. fail_free_cache:
  396. kmem_cache_destroy(bio_post_read_ctx_cache);
  397. fail:
  398. return -ENOMEM;
  399. }
  400. void ext4_exit_post_read_processing(void)
  401. {
  402. mempool_destroy(bio_post_read_ctx_pool);
  403. kmem_cache_destroy(bio_post_read_ctx_cache);
  404. }