readpage.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * linux/fs/ext4/readpage.c
  3. *
  4. * Copyright (C) 2002, Linus Torvalds.
  5. * Copyright (C) 2015, Google, Inc.
  6. *
  7. * This was originally taken from fs/mpage.c
  8. *
  9. * The intent is the ext4_mpage_readpages() function here is intended
  10. * to replace mpage_readpages() in the general case, not just for
  11. * encrypted files. It has some limitations (see below), where it
  12. * will fall back to read_block_full_page(), but these limitations
  13. * should only be hit when page_size != block_size.
  14. *
  15. * This will allow us to attach a callback function to support ext4
  16. * encryption.
  17. *
  18. * If anything unusual happens, such as:
  19. *
  20. * - encountering a page which has buffers
  21. * - encountering a page which has a non-hole after a hole
  22. * - encountering a page with non-contiguous blocks
  23. *
  24. * then this code just gives up and calls the buffer_head-based read function.
  25. * It does handle a page which has holes at the end - that is a common case:
  26. * the end-of-file on blocksize < PAGE_SIZE setups.
  27. *
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/export.h>
  31. #include <linux/mm.h>
  32. #include <linux/kdev_t.h>
  33. #include <linux/gfp.h>
  34. #include <linux/bio.h>
  35. #include <linux/fs.h>
  36. #include <linux/buffer_head.h>
  37. #include <linux/blkdev.h>
  38. #include <linux/highmem.h>
  39. #include <linux/prefetch.h>
  40. #include <linux/mpage.h>
  41. #include <linux/writeback.h>
  42. #include <linux/backing-dev.h>
  43. #include <linux/pagevec.h>
  44. #include <linux/cleancache.h>
  45. #include "ext4.h"
  46. static inline bool ext4_bio_encrypted(struct bio *bio)
  47. {
  48. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  49. return unlikely(bio->bi_private != NULL);
  50. #else
  51. return false;
  52. #endif
  53. }
  54. /*
  55. * I/O completion handler for multipage BIOs.
  56. *
  57. * The mpage code never puts partial pages into a BIO (except for end-of-file).
  58. * If a page does not map to a contiguous run of blocks then it simply falls
  59. * back to block_read_full_page().
  60. *
  61. * Why is this? If a page's completion depends on a number of different BIOs
  62. * which can complete in any order (or at the same time) then determining the
  63. * status of that page is hard. See end_buffer_async_read() for the details.
  64. * There is no point in duplicating all that complexity.
  65. */
  66. static void mpage_end_io(struct bio *bio)
  67. {
  68. struct bio_vec *bv;
  69. int i;
  70. if (ext4_bio_encrypted(bio)) {
  71. if (bio->bi_error) {
  72. fscrypt_release_ctx(bio->bi_private);
  73. } else {
  74. fscrypt_decrypt_bio_pages(bio->bi_private, bio);
  75. return;
  76. }
  77. }
  78. bio_for_each_segment_all(bv, bio, i) {
  79. struct page *page = bv->bv_page;
  80. if (!bio->bi_error) {
  81. SetPageUptodate(page);
  82. } else {
  83. ClearPageUptodate(page);
  84. SetPageError(page);
  85. }
  86. unlock_page(page);
  87. }
  88. bio_put(bio);
  89. }
  90. int ext4_mpage_readpages(struct address_space *mapping,
  91. struct list_head *pages, struct page *page,
  92. unsigned nr_pages)
  93. {
  94. struct bio *bio = NULL;
  95. sector_t last_block_in_bio = 0;
  96. struct inode *inode = mapping->host;
  97. const unsigned blkbits = inode->i_blkbits;
  98. const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
  99. const unsigned blocksize = 1 << blkbits;
  100. sector_t block_in_file;
  101. sector_t last_block;
  102. sector_t last_block_in_file;
  103. sector_t blocks[MAX_BUF_PER_PAGE];
  104. unsigned page_block;
  105. struct block_device *bdev = inode->i_sb->s_bdev;
  106. int length;
  107. unsigned relative_block = 0;
  108. struct ext4_map_blocks map;
  109. map.m_pblk = 0;
  110. map.m_lblk = 0;
  111. map.m_len = 0;
  112. map.m_flags = 0;
  113. for (; nr_pages; nr_pages--) {
  114. int fully_mapped = 1;
  115. unsigned first_hole = blocks_per_page;
  116. prefetchw(&page->flags);
  117. if (pages) {
  118. page = list_entry(pages->prev, struct page, lru);
  119. list_del(&page->lru);
  120. if (add_to_page_cache_lru(page, mapping, page->index,
  121. readahead_gfp_mask(mapping)))
  122. goto next_page;
  123. }
  124. if (page_has_buffers(page))
  125. goto confused;
  126. block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
  127. last_block = block_in_file + nr_pages * blocks_per_page;
  128. last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
  129. if (last_block > last_block_in_file)
  130. last_block = last_block_in_file;
  131. page_block = 0;
  132. /*
  133. * Map blocks using the previous result first.
  134. */
  135. if ((map.m_flags & EXT4_MAP_MAPPED) &&
  136. block_in_file > map.m_lblk &&
  137. block_in_file < (map.m_lblk + map.m_len)) {
  138. unsigned map_offset = block_in_file - map.m_lblk;
  139. unsigned last = map.m_len - map_offset;
  140. for (relative_block = 0; ; relative_block++) {
  141. if (relative_block == last) {
  142. /* needed? */
  143. map.m_flags &= ~EXT4_MAP_MAPPED;
  144. break;
  145. }
  146. if (page_block == blocks_per_page)
  147. break;
  148. blocks[page_block] = map.m_pblk + map_offset +
  149. relative_block;
  150. page_block++;
  151. block_in_file++;
  152. }
  153. }
  154. /*
  155. * Then do more ext4_map_blocks() calls until we are
  156. * done with this page.
  157. */
  158. while (page_block < blocks_per_page) {
  159. if (block_in_file < last_block) {
  160. map.m_lblk = block_in_file;
  161. map.m_len = last_block - block_in_file;
  162. if (ext4_map_blocks(NULL, inode, &map, 0) < 0) {
  163. set_error_page:
  164. SetPageError(page);
  165. zero_user_segment(page, 0,
  166. PAGE_SIZE);
  167. unlock_page(page);
  168. goto next_page;
  169. }
  170. }
  171. if ((map.m_flags & EXT4_MAP_MAPPED) == 0) {
  172. fully_mapped = 0;
  173. if (first_hole == blocks_per_page)
  174. first_hole = page_block;
  175. page_block++;
  176. block_in_file++;
  177. continue;
  178. }
  179. if (first_hole != blocks_per_page)
  180. goto confused; /* hole -> non-hole */
  181. /* Contiguous blocks? */
  182. if (page_block && blocks[page_block-1] != map.m_pblk-1)
  183. goto confused;
  184. for (relative_block = 0; ; relative_block++) {
  185. if (relative_block == map.m_len) {
  186. /* needed? */
  187. map.m_flags &= ~EXT4_MAP_MAPPED;
  188. break;
  189. } else if (page_block == blocks_per_page)
  190. break;
  191. blocks[page_block] = map.m_pblk+relative_block;
  192. page_block++;
  193. block_in_file++;
  194. }
  195. }
  196. if (first_hole != blocks_per_page) {
  197. zero_user_segment(page, first_hole << blkbits,
  198. PAGE_SIZE);
  199. if (first_hole == 0) {
  200. SetPageUptodate(page);
  201. unlock_page(page);
  202. goto next_page;
  203. }
  204. } else if (fully_mapped) {
  205. SetPageMappedToDisk(page);
  206. }
  207. if (fully_mapped && blocks_per_page == 1 &&
  208. !PageUptodate(page) && cleancache_get_page(page) == 0) {
  209. SetPageUptodate(page);
  210. goto confused;
  211. }
  212. /*
  213. * This page will go to BIO. Do we need to send this
  214. * BIO off first?
  215. */
  216. if (bio && (last_block_in_bio != blocks[0] - 1)) {
  217. submit_and_realloc:
  218. submit_bio(bio);
  219. bio = NULL;
  220. }
  221. if (bio == NULL) {
  222. struct fscrypt_ctx *ctx = NULL;
  223. if (ext4_encrypted_inode(inode) &&
  224. S_ISREG(inode->i_mode)) {
  225. ctx = fscrypt_get_ctx(inode, GFP_NOFS);
  226. if (IS_ERR(ctx))
  227. goto set_error_page;
  228. }
  229. bio = bio_alloc(GFP_KERNEL,
  230. min_t(int, nr_pages, BIO_MAX_PAGES));
  231. if (!bio) {
  232. if (ctx)
  233. fscrypt_release_ctx(ctx);
  234. goto set_error_page;
  235. }
  236. bio->bi_bdev = bdev;
  237. bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);
  238. bio->bi_end_io = mpage_end_io;
  239. bio->bi_private = ctx;
  240. bio_set_op_attrs(bio, REQ_OP_READ, 0);
  241. }
  242. length = first_hole << blkbits;
  243. if (bio_add_page(bio, page, length, 0) < length)
  244. goto submit_and_realloc;
  245. if (((map.m_flags & EXT4_MAP_BOUNDARY) &&
  246. (relative_block == map.m_len)) ||
  247. (first_hole != blocks_per_page)) {
  248. submit_bio(bio);
  249. bio = NULL;
  250. } else
  251. last_block_in_bio = blocks[blocks_per_page - 1];
  252. goto next_page;
  253. confused:
  254. if (bio) {
  255. submit_bio(bio);
  256. bio = NULL;
  257. }
  258. if (!PageUptodate(page))
  259. block_read_full_page(page, ext4_get_block);
  260. else
  261. unlock_page(page);
  262. next_page:
  263. if (pages)
  264. put_page(page);
  265. }
  266. BUG_ON(pages && !list_empty(pages));
  267. if (bio)
  268. submit_bio(bio);
  269. return 0;
  270. }