mpage.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/mpage.c
  4. *
  5. * Copyright (C) 2002, Linus Torvalds.
  6. *
  7. * Contains functions related to preparing and submitting BIOs which contain
  8. * multiple pagecache pages.
  9. *
  10. * 15May2002 Andrew Morton
  11. * Initial version
  12. * 27Jun2002 axboe@suse.de
  13. * use bio_add_page() to build bio's just the right size
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/export.h>
  17. #include <linux/mm.h>
  18. #include <linux/kdev_t.h>
  19. #include <linux/gfp.h>
  20. #include <linux/bio.h>
  21. #include <linux/fs.h>
  22. #include <linux/buffer_head.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/highmem.h>
  25. #include <linux/prefetch.h>
  26. #include <linux/mpage.h>
  27. #include <linux/mm_inline.h>
  28. #include <linux/writeback.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/pagevec.h>
  31. #include <linux/cleancache.h>
  32. #include "internal.h"
  33. #define CREATE_TRACE_POINTS
  34. #include <trace/events/android_fs.h>
  35. EXPORT_TRACEPOINT_SYMBOL(android_fs_datawrite_start);
  36. EXPORT_TRACEPOINT_SYMBOL(android_fs_datawrite_end);
  37. EXPORT_TRACEPOINT_SYMBOL(android_fs_dataread_start);
  38. EXPORT_TRACEPOINT_SYMBOL(android_fs_dataread_end);
  39. /*
  40. * I/O completion handler for multipage BIOs.
  41. *
  42. * The mpage code never puts partial pages into a BIO (except for end-of-file).
  43. * If a page does not map to a contiguous run of blocks then it simply falls
  44. * back to block_read_full_page().
  45. *
  46. * Why is this? If a page's completion depends on a number of different BIOs
  47. * which can complete in any order (or at the same time) then determining the
  48. * status of that page is hard. See end_buffer_async_read() for the details.
  49. * There is no point in duplicating all that complexity.
  50. */
  51. static void mpage_end_io(struct bio *bio)
  52. {
  53. struct bio_vec *bv;
  54. struct bvec_iter_all iter_all;
  55. if (trace_android_fs_dataread_end_enabled() &&
  56. (bio_data_dir(bio) == READ)) {
  57. struct page *first_page = bio->bi_io_vec[0].bv_page;
  58. if (first_page != NULL)
  59. trace_android_fs_dataread_end(first_page->mapping->host,
  60. page_offset(first_page),
  61. bio->bi_iter.bi_size);
  62. }
  63. bio_for_each_segment_all(bv, bio, iter_all) {
  64. struct page *page = bv->bv_page;
  65. page_endio(page, bio_op(bio),
  66. blk_status_to_errno(bio->bi_status));
  67. }
  68. bio_put(bio);
  69. }
  70. static struct bio *mpage_bio_submit(int op, int op_flags, struct bio *bio)
  71. {
  72. if (trace_android_fs_dataread_start_enabled() && (op == REQ_OP_READ)) {
  73. struct page *first_page = bio->bi_io_vec[0].bv_page;
  74. if (first_page != NULL) {
  75. char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
  76. path = android_fstrace_get_pathname(pathbuf,
  77. MAX_TRACE_PATHBUF_LEN,
  78. first_page->mapping->host);
  79. trace_android_fs_dataread_start(
  80. first_page->mapping->host,
  81. page_offset(first_page),
  82. bio->bi_iter.bi_size,
  83. current->pid,
  84. path,
  85. current->comm);
  86. }
  87. }
  88. bio->bi_end_io = mpage_end_io;
  89. bio_set_op_attrs(bio, op, op_flags);
  90. guard_bio_eod(bio);
  91. submit_bio(bio);
  92. return NULL;
  93. }
  94. static struct bio *
  95. mpage_alloc(struct block_device *bdev,
  96. sector_t first_sector, int nr_vecs,
  97. gfp_t gfp_flags)
  98. {
  99. struct bio *bio;
  100. /* Restrict the given (page cache) mask for slab allocations */
  101. gfp_flags &= GFP_KERNEL;
  102. bio = bio_alloc(gfp_flags, nr_vecs);
  103. if (bio == NULL && (current->flags & PF_MEMALLOC)) {
  104. while (!bio && (nr_vecs /= 2))
  105. bio = bio_alloc(gfp_flags, nr_vecs);
  106. }
  107. if (bio) {
  108. bio_set_dev(bio, bdev);
  109. bio->bi_iter.bi_sector = first_sector;
  110. }
  111. return bio;
  112. }
  113. /*
  114. * support function for mpage_readahead. The fs supplied get_block might
  115. * return an up to date buffer. This is used to map that buffer into
  116. * the page, which allows readpage to avoid triggering a duplicate call
  117. * to get_block.
  118. *
  119. * The idea is to avoid adding buffers to pages that don't already have
  120. * them. So when the buffer is up to date and the page size == block size,
  121. * this marks the page up to date instead of adding new buffers.
  122. */
  123. static void
  124. map_buffer_to_page(struct page *page, struct buffer_head *bh, int page_block)
  125. {
  126. struct inode *inode = page->mapping->host;
  127. struct buffer_head *page_bh, *head;
  128. int block = 0;
  129. if (!page_has_buffers(page)) {
  130. /*
  131. * don't make any buffers if there is only one buffer on
  132. * the page and the page just needs to be set up to date
  133. */
  134. if (inode->i_blkbits == PAGE_SHIFT &&
  135. buffer_uptodate(bh)) {
  136. SetPageUptodate(page);
  137. return;
  138. }
  139. create_empty_buffers(page, i_blocksize(inode), 0);
  140. }
  141. head = page_buffers(page);
  142. page_bh = head;
  143. do {
  144. if (block == page_block) {
  145. page_bh->b_state = bh->b_state;
  146. page_bh->b_bdev = bh->b_bdev;
  147. page_bh->b_blocknr = bh->b_blocknr;
  148. break;
  149. }
  150. page_bh = page_bh->b_this_page;
  151. block++;
  152. } while (page_bh != head);
  153. }
  154. struct mpage_readpage_args {
  155. struct bio *bio;
  156. struct page *page;
  157. unsigned int nr_pages;
  158. bool is_readahead;
  159. sector_t last_block_in_bio;
  160. struct buffer_head map_bh;
  161. unsigned long first_logical_block;
  162. get_block_t *get_block;
  163. };
  164. /*
  165. * This is the worker routine which does all the work of mapping the disk
  166. * blocks and constructs largest possible bios, submits them for IO if the
  167. * blocks are not contiguous on the disk.
  168. *
  169. * We pass a buffer_head back and forth and use its buffer_mapped() flag to
  170. * represent the validity of its disk mapping and to decide when to do the next
  171. * get_block() call.
  172. */
  173. static struct bio *do_mpage_readpage(struct mpage_readpage_args *args)
  174. {
  175. struct page *page = args->page;
  176. struct inode *inode = page->mapping->host;
  177. const unsigned blkbits = inode->i_blkbits;
  178. const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
  179. const unsigned blocksize = 1 << blkbits;
  180. struct buffer_head *map_bh = &args->map_bh;
  181. sector_t block_in_file;
  182. sector_t last_block;
  183. sector_t last_block_in_file;
  184. sector_t blocks[MAX_BUF_PER_PAGE];
  185. unsigned page_block;
  186. unsigned first_hole = blocks_per_page;
  187. struct block_device *bdev = NULL;
  188. int length;
  189. int fully_mapped = 1;
  190. int op_flags;
  191. unsigned nblocks;
  192. unsigned relative_block;
  193. gfp_t gfp;
  194. if (args->is_readahead) {
  195. op_flags = REQ_RAHEAD;
  196. gfp = readahead_gfp_mask(page->mapping);
  197. } else {
  198. op_flags = 0;
  199. gfp = mapping_gfp_constraint(page->mapping, GFP_KERNEL);
  200. }
  201. if (page_has_buffers(page))
  202. goto confused;
  203. block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
  204. last_block = block_in_file + args->nr_pages * blocks_per_page;
  205. last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
  206. if (last_block > last_block_in_file)
  207. last_block = last_block_in_file;
  208. page_block = 0;
  209. /*
  210. * Map blocks using the result from the previous get_blocks call first.
  211. */
  212. nblocks = map_bh->b_size >> blkbits;
  213. if (buffer_mapped(map_bh) &&
  214. block_in_file > args->first_logical_block &&
  215. block_in_file < (args->first_logical_block + nblocks)) {
  216. unsigned map_offset = block_in_file - args->first_logical_block;
  217. unsigned last = nblocks - map_offset;
  218. for (relative_block = 0; ; relative_block++) {
  219. if (relative_block == last) {
  220. clear_buffer_mapped(map_bh);
  221. break;
  222. }
  223. if (page_block == blocks_per_page)
  224. break;
  225. blocks[page_block] = map_bh->b_blocknr + map_offset +
  226. relative_block;
  227. page_block++;
  228. block_in_file++;
  229. }
  230. bdev = map_bh->b_bdev;
  231. }
  232. /*
  233. * Then do more get_blocks calls until we are done with this page.
  234. */
  235. map_bh->b_page = page;
  236. while (page_block < blocks_per_page) {
  237. map_bh->b_state = 0;
  238. map_bh->b_size = 0;
  239. if (block_in_file < last_block) {
  240. map_bh->b_size = (last_block-block_in_file) << blkbits;
  241. if (args->get_block(inode, block_in_file, map_bh, 0))
  242. goto confused;
  243. args->first_logical_block = block_in_file;
  244. }
  245. if (!buffer_mapped(map_bh)) {
  246. fully_mapped = 0;
  247. if (first_hole == blocks_per_page)
  248. first_hole = page_block;
  249. page_block++;
  250. block_in_file++;
  251. continue;
  252. }
  253. /* some filesystems will copy data into the page during
  254. * the get_block call, in which case we don't want to
  255. * read it again. map_buffer_to_page copies the data
  256. * we just collected from get_block into the page's buffers
  257. * so readpage doesn't have to repeat the get_block call
  258. */
  259. if (buffer_uptodate(map_bh)) {
  260. map_buffer_to_page(page, map_bh, page_block);
  261. goto confused;
  262. }
  263. if (first_hole != blocks_per_page)
  264. goto confused; /* hole -> non-hole */
  265. /* Contiguous blocks? */
  266. if (page_block && blocks[page_block-1] != map_bh->b_blocknr-1)
  267. goto confused;
  268. nblocks = map_bh->b_size >> blkbits;
  269. for (relative_block = 0; ; relative_block++) {
  270. if (relative_block == nblocks) {
  271. clear_buffer_mapped(map_bh);
  272. break;
  273. } else if (page_block == blocks_per_page)
  274. break;
  275. blocks[page_block] = map_bh->b_blocknr+relative_block;
  276. page_block++;
  277. block_in_file++;
  278. }
  279. bdev = map_bh->b_bdev;
  280. }
  281. if (first_hole != blocks_per_page) {
  282. zero_user_segment(page, first_hole << blkbits, PAGE_SIZE);
  283. if (first_hole == 0) {
  284. SetPageUptodate(page);
  285. unlock_page(page);
  286. goto out;
  287. }
  288. } else if (fully_mapped) {
  289. SetPageMappedToDisk(page);
  290. }
  291. if (fully_mapped && blocks_per_page == 1 && !PageUptodate(page) &&
  292. cleancache_get_page(page) == 0) {
  293. SetPageUptodate(page);
  294. goto confused;
  295. }
  296. /*
  297. * This page will go to BIO. Do we need to send this BIO off first?
  298. */
  299. if (args->bio && (args->last_block_in_bio != blocks[0] - 1))
  300. args->bio = mpage_bio_submit(REQ_OP_READ, op_flags, args->bio);
  301. alloc_new:
  302. if (args->bio == NULL) {
  303. if (first_hole == blocks_per_page) {
  304. if (!bdev_read_page(bdev, blocks[0] << (blkbits - 9),
  305. page))
  306. goto out;
  307. }
  308. args->bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
  309. min_t(int, args->nr_pages,
  310. BIO_MAX_PAGES),
  311. gfp);
  312. if (args->bio == NULL)
  313. goto confused;
  314. }
  315. length = first_hole << blkbits;
  316. if (bio_add_page(args->bio, page, length, 0) < length) {
  317. args->bio = mpage_bio_submit(REQ_OP_READ, op_flags, args->bio);
  318. goto alloc_new;
  319. }
  320. relative_block = block_in_file - args->first_logical_block;
  321. nblocks = map_bh->b_size >> blkbits;
  322. if ((buffer_boundary(map_bh) && relative_block == nblocks) ||
  323. (first_hole != blocks_per_page))
  324. args->bio = mpage_bio_submit(REQ_OP_READ, op_flags, args->bio);
  325. else
  326. args->last_block_in_bio = blocks[blocks_per_page - 1];
  327. out:
  328. return args->bio;
  329. confused:
  330. if (args->bio)
  331. args->bio = mpage_bio_submit(REQ_OP_READ, op_flags, args->bio);
  332. if (!PageUptodate(page))
  333. block_read_full_page(page, args->get_block);
  334. else
  335. unlock_page(page);
  336. goto out;
  337. }
  338. /**
  339. * mpage_readahead - start reads against pages
  340. * @rac: Describes which pages to read.
  341. * @get_block: The filesystem's block mapper function.
  342. *
  343. * This function walks the pages and the blocks within each page, building and
  344. * emitting large BIOs.
  345. *
  346. * If anything unusual happens, such as:
  347. *
  348. * - encountering a page which has buffers
  349. * - encountering a page which has a non-hole after a hole
  350. * - encountering a page with non-contiguous blocks
  351. *
  352. * then this code just gives up and calls the buffer_head-based read function.
  353. * It does handle a page which has holes at the end - that is a common case:
  354. * the end-of-file on blocksize < PAGE_SIZE setups.
  355. *
  356. * BH_Boundary explanation:
  357. *
  358. * There is a problem. The mpage read code assembles several pages, gets all
  359. * their disk mappings, and then submits them all. That's fine, but obtaining
  360. * the disk mappings may require I/O. Reads of indirect blocks, for example.
  361. *
  362. * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
  363. * submitted in the following order:
  364. *
  365. * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
  366. *
  367. * because the indirect block has to be read to get the mappings of blocks
  368. * 13,14,15,16. Obviously, this impacts performance.
  369. *
  370. * So what we do it to allow the filesystem's get_block() function to set
  371. * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block
  372. * after this one will require I/O against a block which is probably close to
  373. * this one. So you should push what I/O you have currently accumulated.
  374. *
  375. * This all causes the disk requests to be issued in the correct order.
  376. */
  377. void mpage_readahead(struct readahead_control *rac, get_block_t get_block)
  378. {
  379. struct page *page;
  380. struct mpage_readpage_args args = {
  381. .get_block = get_block,
  382. .is_readahead = true,
  383. };
  384. while ((page = readahead_page(rac))) {
  385. prefetchw(&page->flags);
  386. args.page = page;
  387. args.nr_pages = readahead_count(rac);
  388. args.bio = do_mpage_readpage(&args);
  389. put_page(page);
  390. }
  391. if (args.bio)
  392. mpage_bio_submit(REQ_OP_READ, REQ_RAHEAD, args.bio);
  393. }
  394. EXPORT_SYMBOL_NS(mpage_readahead, ANDROID_GKI_VFS_EXPORT_ONLY);
  395. /*
  396. * This isn't called much at all
  397. */
  398. int mpage_readpage(struct page *page, get_block_t get_block)
  399. {
  400. struct mpage_readpage_args args = {
  401. .page = page,
  402. .nr_pages = 1,
  403. .get_block = get_block,
  404. };
  405. args.bio = do_mpage_readpage(&args);
  406. if (args.bio)
  407. mpage_bio_submit(REQ_OP_READ, 0, args.bio);
  408. return 0;
  409. }
  410. EXPORT_SYMBOL_NS(mpage_readpage, ANDROID_GKI_VFS_EXPORT_ONLY);
  411. /*
  412. * Writing is not so simple.
  413. *
  414. * If the page has buffers then they will be used for obtaining the disk
  415. * mapping. We only support pages which are fully mapped-and-dirty, with a
  416. * special case for pages which are unmapped at the end: end-of-file.
  417. *
  418. * If the page has no buffers (preferred) then the page is mapped here.
  419. *
  420. * If all blocks are found to be contiguous then the page can go into the
  421. * BIO. Otherwise fall back to the mapping's writepage().
  422. *
  423. * FIXME: This code wants an estimate of how many pages are still to be
  424. * written, so it can intelligently allocate a suitably-sized BIO. For now,
  425. * just allocate full-size (16-page) BIOs.
  426. */
  427. struct mpage_data {
  428. struct bio *bio;
  429. sector_t last_block_in_bio;
  430. get_block_t *get_block;
  431. unsigned use_writepage;
  432. };
  433. /*
  434. * We have our BIO, so we can now mark the buffers clean. Make
  435. * sure to only clean buffers which we know we'll be writing.
  436. */
  437. static void clean_buffers(struct page *page, unsigned first_unmapped)
  438. {
  439. unsigned buffer_counter = 0;
  440. struct buffer_head *bh, *head;
  441. if (!page_has_buffers(page))
  442. return;
  443. head = page_buffers(page);
  444. bh = head;
  445. do {
  446. if (buffer_counter++ == first_unmapped)
  447. break;
  448. clear_buffer_dirty(bh);
  449. bh = bh->b_this_page;
  450. } while (bh != head);
  451. /*
  452. * we cannot drop the bh if the page is not uptodate or a concurrent
  453. * readpage would fail to serialize with the bh and it would read from
  454. * disk before we reach the platter.
  455. */
  456. if (buffer_heads_over_limit && PageUptodate(page))
  457. try_to_free_buffers(page);
  458. }
  459. /*
  460. * For situations where we want to clean all buffers attached to a page.
  461. * We don't need to calculate how many buffers are attached to the page,
  462. * we just need to specify a number larger than the maximum number of buffers.
  463. */
  464. void clean_page_buffers(struct page *page)
  465. {
  466. clean_buffers(page, ~0U);
  467. }
  468. static int __mpage_writepage(struct page *page, struct writeback_control *wbc,
  469. void *data)
  470. {
  471. struct mpage_data *mpd = data;
  472. struct bio *bio = mpd->bio;
  473. struct address_space *mapping = page->mapping;
  474. struct inode *inode = page->mapping->host;
  475. const unsigned blkbits = inode->i_blkbits;
  476. unsigned long end_index;
  477. const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
  478. sector_t last_block;
  479. sector_t block_in_file;
  480. sector_t blocks[MAX_BUF_PER_PAGE];
  481. unsigned page_block;
  482. unsigned first_unmapped = blocks_per_page;
  483. struct block_device *bdev = NULL;
  484. int boundary = 0;
  485. sector_t boundary_block = 0;
  486. struct block_device *boundary_bdev = NULL;
  487. int length;
  488. struct buffer_head map_bh;
  489. loff_t i_size = i_size_read(inode);
  490. int ret = 0;
  491. int op_flags = wbc_to_write_flags(wbc);
  492. if (page_has_buffers(page)) {
  493. struct buffer_head *head = page_buffers(page);
  494. struct buffer_head *bh = head;
  495. /* If they're all mapped and dirty, do it */
  496. page_block = 0;
  497. do {
  498. BUG_ON(buffer_locked(bh));
  499. if (!buffer_mapped(bh)) {
  500. /*
  501. * unmapped dirty buffers are created by
  502. * __set_page_dirty_buffers -> mmapped data
  503. */
  504. if (buffer_dirty(bh))
  505. goto confused;
  506. if (first_unmapped == blocks_per_page)
  507. first_unmapped = page_block;
  508. continue;
  509. }
  510. if (first_unmapped != blocks_per_page)
  511. goto confused; /* hole -> non-hole */
  512. if (!buffer_dirty(bh) || !buffer_uptodate(bh))
  513. goto confused;
  514. if (page_block) {
  515. if (bh->b_blocknr != blocks[page_block-1] + 1)
  516. goto confused;
  517. }
  518. blocks[page_block++] = bh->b_blocknr;
  519. boundary = buffer_boundary(bh);
  520. if (boundary) {
  521. boundary_block = bh->b_blocknr;
  522. boundary_bdev = bh->b_bdev;
  523. }
  524. bdev = bh->b_bdev;
  525. } while ((bh = bh->b_this_page) != head);
  526. if (first_unmapped)
  527. goto page_is_mapped;
  528. /*
  529. * Page has buffers, but they are all unmapped. The page was
  530. * created by pagein or read over a hole which was handled by
  531. * block_read_full_page(). If this address_space is also
  532. * using mpage_readahead then this can rarely happen.
  533. */
  534. goto confused;
  535. }
  536. /*
  537. * The page has no buffers: map it to disk
  538. */
  539. BUG_ON(!PageUptodate(page));
  540. block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
  541. last_block = (i_size - 1) >> blkbits;
  542. map_bh.b_page = page;
  543. for (page_block = 0; page_block < blocks_per_page; ) {
  544. map_bh.b_state = 0;
  545. map_bh.b_size = 1 << blkbits;
  546. if (mpd->get_block(inode, block_in_file, &map_bh, 1))
  547. goto confused;
  548. if (buffer_new(&map_bh))
  549. clean_bdev_bh_alias(&map_bh);
  550. if (buffer_boundary(&map_bh)) {
  551. boundary_block = map_bh.b_blocknr;
  552. boundary_bdev = map_bh.b_bdev;
  553. }
  554. if (page_block) {
  555. if (map_bh.b_blocknr != blocks[page_block-1] + 1)
  556. goto confused;
  557. }
  558. blocks[page_block++] = map_bh.b_blocknr;
  559. boundary = buffer_boundary(&map_bh);
  560. bdev = map_bh.b_bdev;
  561. if (block_in_file == last_block)
  562. break;
  563. block_in_file++;
  564. }
  565. BUG_ON(page_block == 0);
  566. first_unmapped = page_block;
  567. page_is_mapped:
  568. end_index = i_size >> PAGE_SHIFT;
  569. if (page->index >= end_index) {
  570. /*
  571. * The page straddles i_size. It must be zeroed out on each
  572. * and every writepage invocation because it may be mmapped.
  573. * "A file is mapped in multiples of the page size. For a file
  574. * that is not a multiple of the page size, the remaining memory
  575. * is zeroed when mapped, and writes to that region are not
  576. * written out to the file."
  577. */
  578. unsigned offset = i_size & (PAGE_SIZE - 1);
  579. if (page->index > end_index || !offset)
  580. goto confused;
  581. zero_user_segment(page, offset, PAGE_SIZE);
  582. }
  583. /*
  584. * This page will go to BIO. Do we need to send this BIO off first?
  585. */
  586. if (bio && mpd->last_block_in_bio != blocks[0] - 1)
  587. bio = mpage_bio_submit(REQ_OP_WRITE, op_flags, bio);
  588. alloc_new:
  589. if (bio == NULL) {
  590. if (first_unmapped == blocks_per_page) {
  591. if (!bdev_write_page(bdev, blocks[0] << (blkbits - 9),
  592. page, wbc))
  593. goto out;
  594. }
  595. bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
  596. BIO_MAX_PAGES, GFP_NOFS|__GFP_HIGH);
  597. if (bio == NULL)
  598. goto confused;
  599. wbc_init_bio(wbc, bio);
  600. bio->bi_write_hint = inode->i_write_hint;
  601. }
  602. /*
  603. * Must try to add the page before marking the buffer clean or
  604. * the confused fail path above (OOM) will be very confused when
  605. * it finds all bh marked clean (i.e. it will not write anything)
  606. */
  607. wbc_account_cgroup_owner(wbc, page, PAGE_SIZE);
  608. length = first_unmapped << blkbits;
  609. if (bio_add_page(bio, page, length, 0) < length) {
  610. bio = mpage_bio_submit(REQ_OP_WRITE, op_flags, bio);
  611. goto alloc_new;
  612. }
  613. clean_buffers(page, first_unmapped);
  614. BUG_ON(PageWriteback(page));
  615. set_page_writeback(page);
  616. unlock_page(page);
  617. if (boundary || (first_unmapped != blocks_per_page)) {
  618. bio = mpage_bio_submit(REQ_OP_WRITE, op_flags, bio);
  619. if (boundary_block) {
  620. write_boundary_block(boundary_bdev,
  621. boundary_block, 1 << blkbits);
  622. }
  623. } else {
  624. mpd->last_block_in_bio = blocks[blocks_per_page - 1];
  625. }
  626. goto out;
  627. confused:
  628. if (bio)
  629. bio = mpage_bio_submit(REQ_OP_WRITE, op_flags, bio);
  630. if (mpd->use_writepage) {
  631. ret = mapping->a_ops->writepage(page, wbc);
  632. } else {
  633. ret = -EAGAIN;
  634. goto out;
  635. }
  636. /*
  637. * The caller has a ref on the inode, so *mapping is stable
  638. */
  639. mapping_set_error(mapping, ret);
  640. out:
  641. mpd->bio = bio;
  642. return ret;
  643. }
  644. /**
  645. * mpage_writepages - walk the list of dirty pages of the given address space & writepage() all of them
  646. * @mapping: address space structure to write
  647. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  648. * @get_block: the filesystem's block mapper function.
  649. * If this is NULL then use a_ops->writepage. Otherwise, go
  650. * direct-to-BIO.
  651. *
  652. * This is a library function, which implements the writepages()
  653. * address_space_operation.
  654. *
  655. * If a page is already under I/O, generic_writepages() skips it, even
  656. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  657. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  658. * and msync() need to guarantee that all the data which was dirty at the time
  659. * the call was made get new I/O started against them. If wbc->sync_mode is
  660. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  661. * existing IO to complete.
  662. */
  663. int
  664. mpage_writepages(struct address_space *mapping,
  665. struct writeback_control *wbc, get_block_t get_block)
  666. {
  667. struct blk_plug plug;
  668. int ret;
  669. blk_start_plug(&plug);
  670. if (!get_block)
  671. ret = generic_writepages(mapping, wbc);
  672. else {
  673. struct mpage_data mpd = {
  674. .bio = NULL,
  675. .last_block_in_bio = 0,
  676. .get_block = get_block,
  677. .use_writepage = 1,
  678. };
  679. ret = write_cache_pages(mapping, wbc, __mpage_writepage, &mpd);
  680. if (mpd.bio) {
  681. int op_flags = (wbc->sync_mode == WB_SYNC_ALL ?
  682. REQ_SYNC : 0);
  683. mpage_bio_submit(REQ_OP_WRITE, op_flags, mpd.bio);
  684. }
  685. }
  686. blk_finish_plug(&plug);
  687. return ret;
  688. }
  689. EXPORT_SYMBOL(mpage_writepages);
  690. int mpage_writepage(struct page *page, get_block_t get_block,
  691. struct writeback_control *wbc)
  692. {
  693. struct mpage_data mpd = {
  694. .bio = NULL,
  695. .last_block_in_bio = 0,
  696. .get_block = get_block,
  697. .use_writepage = 0,
  698. };
  699. int ret = __mpage_writepage(page, wbc, &mpd);
  700. if (mpd.bio) {
  701. int op_flags = (wbc->sync_mode == WB_SYNC_ALL ?
  702. REQ_SYNC : 0);
  703. mpage_bio_submit(REQ_OP_WRITE, op_flags, mpd.bio);
  704. }
  705. return ret;
  706. }
  707. EXPORT_SYMBOL(mpage_writepage);