readpage.c 8.2 KB

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