mpage.c 20 KB

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