mpage.c 20 KB

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