mpage.c 20 KB

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