mpage.c 19 KB

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