mpage.c 20 KB

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