mpage.c 20 KB

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