mpage.c 20 KB

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