readpage.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 intent is the ext4_mpage_readpages() function here is intended
  11. * to replace mpage_readpages() 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. static inline bool ext4_bio_encrypted(struct bio *bio)
  48. {
  49. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  50. return unlikely(bio->bi_private != NULL);
  51. #else
  52. return false;
  53. #endif
  54. }
  55. /*
  56. * I/O completion handler for multipage BIOs.
  57. *
  58. * The mpage code never puts partial pages into a BIO (except for end-of-file).
  59. * If a page does not map to a contiguous run of blocks then it simply falls
  60. * back to block_read_full_page().
  61. *
  62. * Why is this? If a page's completion depends on a number of different BIOs
  63. * which can complete in any order (or at the same time) then determining the
  64. * status of that page is hard. See end_buffer_async_read() for the details.
  65. * There is no point in duplicating all that complexity.
  66. */
  67. static void mpage_end_io(struct bio *bio)
  68. {
  69. struct bio_vec *bv;
  70. int i;
  71. if (ext4_bio_encrypted(bio)) {
  72. if (bio->bi_status) {
  73. fscrypt_release_ctx(bio->bi_private);
  74. } else {
  75. fscrypt_enqueue_decrypt_bio(bio->bi_private, bio);
  76. return;
  77. }
  78. }
  79. bio_for_each_segment_all(bv, bio, i) {
  80. struct page *page = bv->bv_page;
  81. if (!bio->bi_status) {
  82. SetPageUptodate(page);
  83. } else {
  84. ClearPageUptodate(page);
  85. SetPageError(page);
  86. }
  87. unlock_page(page);
  88. }
  89. bio_put(bio);
  90. }
  91. int ext4_mpage_readpages(struct address_space *mapping,
  92. struct list_head *pages, struct page *page,
  93. unsigned nr_pages, bool is_readahead)
  94. {
  95. struct bio *bio = NULL;
  96. sector_t last_block_in_bio = 0;
  97. struct inode *inode = mapping->host;
  98. const unsigned blkbits = inode->i_blkbits;
  99. const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
  100. const unsigned blocksize = 1 << blkbits;
  101. sector_t block_in_file;
  102. sector_t last_block;
  103. sector_t last_block_in_file;
  104. sector_t blocks[MAX_BUF_PER_PAGE];
  105. unsigned page_block;
  106. struct block_device *bdev = inode->i_sb->s_bdev;
  107. int length;
  108. unsigned relative_block = 0;
  109. struct ext4_map_blocks map;
  110. map.m_pblk = 0;
  111. map.m_lblk = 0;
  112. map.m_len = 0;
  113. map.m_flags = 0;
  114. for (; nr_pages; nr_pages--) {
  115. int fully_mapped = 1;
  116. unsigned first_hole = blocks_per_page;
  117. prefetchw(&page->flags);
  118. if (pages) {
  119. page = list_entry(pages->prev, struct page, lru);
  120. list_del(&page->lru);
  121. if (add_to_page_cache_lru(page, mapping, page->index,
  122. readahead_gfp_mask(mapping)))
  123. goto next_page;
  124. }
  125. if (page_has_buffers(page))
  126. goto confused;
  127. block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
  128. last_block = block_in_file + nr_pages * blocks_per_page;
  129. last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
  130. if (last_block > last_block_in_file)
  131. last_block = last_block_in_file;
  132. page_block = 0;
  133. /*
  134. * Map blocks using the previous result first.
  135. */
  136. if ((map.m_flags & EXT4_MAP_MAPPED) &&
  137. block_in_file > map.m_lblk &&
  138. block_in_file < (map.m_lblk + map.m_len)) {
  139. unsigned map_offset = block_in_file - map.m_lblk;
  140. unsigned last = map.m_len - map_offset;
  141. for (relative_block = 0; ; relative_block++) {
  142. if (relative_block == last) {
  143. /* needed? */
  144. map.m_flags &= ~EXT4_MAP_MAPPED;
  145. break;
  146. }
  147. if (page_block == blocks_per_page)
  148. break;
  149. blocks[page_block] = map.m_pblk + map_offset +
  150. relative_block;
  151. page_block++;
  152. block_in_file++;
  153. }
  154. }
  155. /*
  156. * Then do more ext4_map_blocks() calls until we are
  157. * done with this page.
  158. */
  159. while (page_block < blocks_per_page) {
  160. if (block_in_file < last_block) {
  161. map.m_lblk = block_in_file;
  162. map.m_len = last_block - block_in_file;
  163. if (ext4_map_blocks(NULL, inode, &map, 0) < 0) {
  164. set_error_page:
  165. SetPageError(page);
  166. zero_user_segment(page, 0,
  167. PAGE_SIZE);
  168. unlock_page(page);
  169. goto next_page;
  170. }
  171. }
  172. if ((map.m_flags & EXT4_MAP_MAPPED) == 0) {
  173. fully_mapped = 0;
  174. if (first_hole == blocks_per_page)
  175. first_hole = page_block;
  176. page_block++;
  177. block_in_file++;
  178. continue;
  179. }
  180. if (first_hole != blocks_per_page)
  181. goto confused; /* hole -> non-hole */
  182. /* Contiguous blocks? */
  183. if (page_block && blocks[page_block-1] != map.m_pblk-1)
  184. goto confused;
  185. for (relative_block = 0; ; relative_block++) {
  186. if (relative_block == map.m_len) {
  187. /* needed? */
  188. map.m_flags &= ~EXT4_MAP_MAPPED;
  189. break;
  190. } else if (page_block == blocks_per_page)
  191. break;
  192. blocks[page_block] = map.m_pblk+relative_block;
  193. page_block++;
  194. block_in_file++;
  195. }
  196. }
  197. if (first_hole != blocks_per_page) {
  198. zero_user_segment(page, first_hole << blkbits,
  199. PAGE_SIZE);
  200. if (first_hole == 0) {
  201. SetPageUptodate(page);
  202. unlock_page(page);
  203. goto next_page;
  204. }
  205. } else if (fully_mapped) {
  206. SetPageMappedToDisk(page);
  207. }
  208. if (fully_mapped && blocks_per_page == 1 &&
  209. !PageUptodate(page) && cleancache_get_page(page) == 0) {
  210. SetPageUptodate(page);
  211. goto confused;
  212. }
  213. /*
  214. * This page will go to BIO. Do we need to send this
  215. * BIO off first?
  216. */
  217. if (bio && (last_block_in_bio != blocks[0] - 1)) {
  218. submit_and_realloc:
  219. submit_bio(bio);
  220. bio = NULL;
  221. }
  222. if (bio == NULL) {
  223. struct fscrypt_ctx *ctx = NULL;
  224. if (ext4_encrypted_inode(inode) &&
  225. S_ISREG(inode->i_mode)) {
  226. ctx = fscrypt_get_ctx(inode, GFP_NOFS);
  227. if (IS_ERR(ctx))
  228. goto set_error_page;
  229. }
  230. bio = bio_alloc(GFP_KERNEL,
  231. min_t(int, nr_pages, BIO_MAX_PAGES));
  232. if (!bio) {
  233. if (ctx)
  234. fscrypt_release_ctx(ctx);
  235. goto set_error_page;
  236. }
  237. bio_set_dev(bio, bdev);
  238. bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);
  239. bio->bi_end_io = mpage_end_io;
  240. bio->bi_private = ctx;
  241. bio_set_op_attrs(bio, REQ_OP_READ,
  242. is_readahead ? REQ_RAHEAD : 0);
  243. }
  244. length = first_hole << blkbits;
  245. if (bio_add_page(bio, page, length, 0) < length)
  246. goto submit_and_realloc;
  247. if (((map.m_flags & EXT4_MAP_BOUNDARY) &&
  248. (relative_block == map.m_len)) ||
  249. (first_hole != blocks_per_page)) {
  250. submit_bio(bio);
  251. bio = NULL;
  252. } else
  253. last_block_in_bio = blocks[blocks_per_page - 1];
  254. goto next_page;
  255. confused:
  256. if (bio) {
  257. submit_bio(bio);
  258. bio = NULL;
  259. }
  260. if (!PageUptodate(page))
  261. block_read_full_page(page, ext4_get_block);
  262. else
  263. unlock_page(page);
  264. next_page:
  265. if (pages)
  266. put_page(page);
  267. }
  268. BUG_ON(pages && !list_empty(pages));
  269. if (bio)
  270. submit_bio(bio);
  271. return 0;
  272. }