readpage.c 11 KB

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