readpage.c 8.4 KB

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