readpage.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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/blkdev.h>
  39. #include <linux/highmem.h>
  40. #include <linux/prefetch.h>
  41. #include <linux/mpage.h>
  42. #include <linux/writeback.h>
  43. #include <linux/backing-dev.h>
  44. #include <linux/pagevec.h>
  45. #include <linux/cleancache.h>
  46. #include "ext4.h"
  47. #define NUM_PREALLOC_POST_READ_CTXS 128
  48. static struct kmem_cache *bio_post_read_ctx_cache;
  49. static mempool_t *bio_post_read_ctx_pool;
  50. /* postprocessing steps for read bios */
  51. enum bio_post_read_step {
  52. STEP_INITIAL = 0,
  53. STEP_DECRYPT,
  54. STEP_VERITY,
  55. STEP_MAX,
  56. };
  57. struct bio_post_read_ctx {
  58. struct bio *bio;
  59. struct work_struct work;
  60. unsigned int cur_step;
  61. unsigned int enabled_steps;
  62. };
  63. static void __read_end_io(struct bio *bio)
  64. {
  65. struct page *page;
  66. struct bio_vec *bv;
  67. struct bvec_iter_all iter_all;
  68. bio_for_each_segment_all(bv, bio, iter_all) {
  69. page = bv->bv_page;
  70. if (bio->bi_status)
  71. ClearPageUptodate(page);
  72. else
  73. SetPageUptodate(page);
  74. unlock_page(page);
  75. }
  76. if (bio->bi_private)
  77. mempool_free(bio->bi_private, bio_post_read_ctx_pool);
  78. bio_put(bio);
  79. }
  80. static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
  81. static void decrypt_work(struct work_struct *work)
  82. {
  83. struct bio_post_read_ctx *ctx =
  84. container_of(work, struct bio_post_read_ctx, work);
  85. struct bio *bio = ctx->bio;
  86. if (fscrypt_decrypt_bio(bio))
  87. bio_post_read_processing(ctx);
  88. else
  89. __read_end_io(bio);
  90. }
  91. static void verity_work(struct work_struct *work)
  92. {
  93. struct bio_post_read_ctx *ctx =
  94. container_of(work, struct bio_post_read_ctx, work);
  95. struct bio *bio = ctx->bio;
  96. /*
  97. * fsverity_verify_bio() may call readahead() again, and although verity
  98. * will be disabled for that, decryption may still be needed, causing
  99. * another bio_post_read_ctx to be allocated. So to guarantee that
  100. * mempool_alloc() never deadlocks we must free the current ctx first.
  101. * This is safe because verity is the last post-read step.
  102. */
  103. BUILD_BUG_ON(STEP_VERITY + 1 != STEP_MAX);
  104. mempool_free(ctx, bio_post_read_ctx_pool);
  105. bio->bi_private = NULL;
  106. fsverity_verify_bio(bio);
  107. __read_end_io(bio);
  108. }
  109. static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
  110. {
  111. /*
  112. * We use different work queues for decryption and for verity because
  113. * verity may require reading metadata pages that need decryption, and
  114. * we shouldn't recurse to the same workqueue.
  115. */
  116. switch (++ctx->cur_step) {
  117. case STEP_DECRYPT:
  118. if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
  119. INIT_WORK(&ctx->work, decrypt_work);
  120. fscrypt_enqueue_decrypt_work(&ctx->work);
  121. return;
  122. }
  123. ctx->cur_step++;
  124. fallthrough;
  125. case STEP_VERITY:
  126. if (ctx->enabled_steps & (1 << STEP_VERITY)) {
  127. INIT_WORK(&ctx->work, verity_work);
  128. fsverity_enqueue_verify_work(&ctx->work);
  129. return;
  130. }
  131. ctx->cur_step++;
  132. fallthrough;
  133. default:
  134. __read_end_io(ctx->bio);
  135. }
  136. }
  137. static bool bio_post_read_required(struct bio *bio)
  138. {
  139. return bio->bi_private && !bio->bi_status;
  140. }
  141. /*
  142. * I/O completion handler for multipage BIOs.
  143. *
  144. * The mpage code never puts partial pages into a BIO (except for end-of-file).
  145. * If a page does not map to a contiguous run of blocks then it simply falls
  146. * back to block_read_full_folio().
  147. *
  148. * Why is this? If a page's completion depends on a number of different BIOs
  149. * which can complete in any order (or at the same time) then determining the
  150. * status of that page is hard. See end_buffer_async_read() for the details.
  151. * There is no point in duplicating all that complexity.
  152. */
  153. static void mpage_end_io(struct bio *bio)
  154. {
  155. if (bio_post_read_required(bio)) {
  156. struct bio_post_read_ctx *ctx = bio->bi_private;
  157. ctx->cur_step = STEP_INITIAL;
  158. bio_post_read_processing(ctx);
  159. return;
  160. }
  161. __read_end_io(bio);
  162. }
  163. static inline bool ext4_need_verity(const struct inode *inode, pgoff_t idx)
  164. {
  165. return fsverity_active(inode) &&
  166. idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
  167. }
  168. static void ext4_set_bio_post_read_ctx(struct bio *bio,
  169. const struct inode *inode,
  170. pgoff_t first_idx)
  171. {
  172. unsigned int post_read_steps = 0;
  173. if (fscrypt_inode_uses_fs_layer_crypto(inode))
  174. post_read_steps |= 1 << STEP_DECRYPT;
  175. if (ext4_need_verity(inode, first_idx))
  176. post_read_steps |= 1 << STEP_VERITY;
  177. if (post_read_steps) {
  178. /* Due to the mempool, this never fails. */
  179. struct bio_post_read_ctx *ctx =
  180. mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
  181. ctx->bio = bio;
  182. ctx->enabled_steps = post_read_steps;
  183. bio->bi_private = ctx;
  184. }
  185. }
  186. static inline loff_t ext4_readpage_limit(struct inode *inode)
  187. {
  188. if (IS_ENABLED(CONFIG_FS_VERITY) && IS_VERITY(inode))
  189. return inode->i_sb->s_maxbytes;
  190. return i_size_read(inode);
  191. }
  192. int ext4_mpage_readpages(struct inode *inode,
  193. struct readahead_control *rac, struct page *page)
  194. {
  195. struct bio *bio = NULL;
  196. sector_t last_block_in_bio = 0;
  197. const unsigned blkbits = inode->i_blkbits;
  198. const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
  199. const unsigned blocksize = 1 << blkbits;
  200. sector_t next_block;
  201. sector_t block_in_file;
  202. sector_t last_block;
  203. sector_t last_block_in_file;
  204. sector_t blocks[MAX_BUF_PER_PAGE];
  205. unsigned page_block;
  206. struct block_device *bdev = inode->i_sb->s_bdev;
  207. int length;
  208. unsigned relative_block = 0;
  209. struct ext4_map_blocks map;
  210. unsigned int nr_pages = rac ? readahead_count(rac) : 1;
  211. map.m_pblk = 0;
  212. map.m_lblk = 0;
  213. map.m_len = 0;
  214. map.m_flags = 0;
  215. for (; nr_pages; nr_pages--) {
  216. int fully_mapped = 1;
  217. unsigned first_hole = blocks_per_page;
  218. if (rac) {
  219. page = readahead_page(rac);
  220. prefetchw(&page->flags);
  221. }
  222. if (page_has_buffers(page))
  223. goto confused;
  224. block_in_file = next_block =
  225. (sector_t)page->index << (PAGE_SHIFT - blkbits);
  226. last_block = block_in_file + nr_pages * blocks_per_page;
  227. last_block_in_file = (ext4_readpage_limit(inode) +
  228. blocksize - 1) >> blkbits;
  229. if (last_block > last_block_in_file)
  230. last_block = last_block_in_file;
  231. page_block = 0;
  232. /*
  233. * Map blocks using the previous result first.
  234. */
  235. if ((map.m_flags & EXT4_MAP_MAPPED) &&
  236. block_in_file > map.m_lblk &&
  237. block_in_file < (map.m_lblk + map.m_len)) {
  238. unsigned map_offset = block_in_file - map.m_lblk;
  239. unsigned last = map.m_len - map_offset;
  240. for (relative_block = 0; ; relative_block++) {
  241. if (relative_block == last) {
  242. /* needed? */
  243. map.m_flags &= ~EXT4_MAP_MAPPED;
  244. break;
  245. }
  246. if (page_block == blocks_per_page)
  247. break;
  248. blocks[page_block] = map.m_pblk + map_offset +
  249. relative_block;
  250. page_block++;
  251. block_in_file++;
  252. }
  253. }
  254. /*
  255. * Then do more ext4_map_blocks() calls until we are
  256. * done with this page.
  257. */
  258. while (page_block < blocks_per_page) {
  259. if (block_in_file < last_block) {
  260. map.m_lblk = block_in_file;
  261. map.m_len = last_block - block_in_file;
  262. if (ext4_map_blocks(NULL, inode, &map, 0) < 0) {
  263. set_error_page:
  264. SetPageError(page);
  265. zero_user_segment(page, 0,
  266. PAGE_SIZE);
  267. unlock_page(page);
  268. goto next_page;
  269. }
  270. }
  271. if ((map.m_flags & EXT4_MAP_MAPPED) == 0) {
  272. fully_mapped = 0;
  273. if (first_hole == blocks_per_page)
  274. first_hole = page_block;
  275. page_block++;
  276. block_in_file++;
  277. continue;
  278. }
  279. if (first_hole != blocks_per_page)
  280. goto confused; /* hole -> non-hole */
  281. /* Contiguous blocks? */
  282. if (page_block && blocks[page_block-1] != map.m_pblk-1)
  283. goto confused;
  284. for (relative_block = 0; ; relative_block++) {
  285. if (relative_block == map.m_len) {
  286. /* needed? */
  287. map.m_flags &= ~EXT4_MAP_MAPPED;
  288. break;
  289. } else if (page_block == blocks_per_page)
  290. break;
  291. blocks[page_block] = map.m_pblk+relative_block;
  292. page_block++;
  293. block_in_file++;
  294. }
  295. }
  296. if (first_hole != blocks_per_page) {
  297. zero_user_segment(page, first_hole << blkbits,
  298. PAGE_SIZE);
  299. if (first_hole == 0) {
  300. if (ext4_need_verity(inode, page->index) &&
  301. !fsverity_verify_page(page))
  302. goto set_error_page;
  303. SetPageUptodate(page);
  304. unlock_page(page);
  305. goto next_page;
  306. }
  307. } else if (fully_mapped) {
  308. SetPageMappedToDisk(page);
  309. }
  310. if (fully_mapped && blocks_per_page == 1 &&
  311. !PageUptodate(page) && cleancache_get_page(page) == 0) {
  312. SetPageUptodate(page);
  313. goto confused;
  314. }
  315. /*
  316. * This page will go to BIO. Do we need to send this
  317. * BIO off first?
  318. */
  319. if (bio && (last_block_in_bio != blocks[0] - 1 ||
  320. !fscrypt_mergeable_bio(bio, inode, next_block))) {
  321. submit_and_realloc:
  322. submit_bio(bio);
  323. bio = NULL;
  324. }
  325. if (bio == NULL) {
  326. /*
  327. * bio_alloc will _always_ be able to allocate a bio if
  328. * __GFP_DIRECT_RECLAIM is set, see bio_alloc_bioset().
  329. */
  330. bio = bio_alloc(bdev, bio_max_segs(nr_pages),
  331. REQ_OP_READ, GFP_KERNEL);
  332. fscrypt_set_bio_crypt_ctx(bio, inode, next_block,
  333. GFP_KERNEL);
  334. ext4_set_bio_post_read_ctx(bio, inode, page->index);
  335. bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);
  336. bio->bi_end_io = mpage_end_io;
  337. if (rac)
  338. bio->bi_opf |= REQ_RAHEAD;
  339. }
  340. length = first_hole << blkbits;
  341. if (bio_add_page(bio, page, length, 0) < length)
  342. goto submit_and_realloc;
  343. if (((map.m_flags & EXT4_MAP_BOUNDARY) &&
  344. (relative_block == map.m_len)) ||
  345. (first_hole != blocks_per_page)) {
  346. submit_bio(bio);
  347. bio = NULL;
  348. } else
  349. last_block_in_bio = blocks[blocks_per_page - 1];
  350. goto next_page;
  351. confused:
  352. if (bio) {
  353. submit_bio(bio);
  354. bio = NULL;
  355. }
  356. if (!PageUptodate(page))
  357. block_read_full_folio(page_folio(page), ext4_get_block);
  358. else
  359. unlock_page(page);
  360. next_page:
  361. if (rac)
  362. put_page(page);
  363. }
  364. if (bio)
  365. submit_bio(bio);
  366. return 0;
  367. }
  368. int __init ext4_init_post_read_processing(void)
  369. {
  370. bio_post_read_ctx_cache =
  371. kmem_cache_create("ext4_bio_post_read_ctx",
  372. sizeof(struct bio_post_read_ctx), 0, 0, NULL);
  373. if (!bio_post_read_ctx_cache)
  374. goto fail;
  375. bio_post_read_ctx_pool =
  376. mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
  377. bio_post_read_ctx_cache);
  378. if (!bio_post_read_ctx_pool)
  379. goto fail_free_cache;
  380. return 0;
  381. fail_free_cache:
  382. kmem_cache_destroy(bio_post_read_ctx_cache);
  383. fail:
  384. return -ENOMEM;
  385. }
  386. void ext4_exit_post_read_processing(void)
  387. {
  388. mempool_destroy(bio_post_read_ctx_pool);
  389. kmem_cache_destroy(bio_post_read_ctx_cache);
  390. }