mpage.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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. {
  132. struct inode *inode = page->mapping->host;
  133. const unsigned blkbits = inode->i_blkbits;
  134. const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
  135. const unsigned blocksize = 1 << blkbits;
  136. sector_t block_in_file;
  137. sector_t last_block;
  138. sector_t last_block_in_file;
  139. sector_t blocks[MAX_BUF_PER_PAGE];
  140. unsigned page_block;
  141. unsigned first_hole = blocks_per_page;
  142. struct block_device *bdev = NULL;
  143. int length;
  144. int fully_mapped = 1;
  145. unsigned nblocks;
  146. unsigned relative_block;
  147. if (page_has_buffers(page))
  148. goto confused;
  149. block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
  150. last_block = block_in_file + nr_pages * blocks_per_page;
  151. last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
  152. if (last_block > last_block_in_file)
  153. last_block = last_block_in_file;
  154. page_block = 0;
  155. /*
  156. * Map blocks using the result from the previous get_blocks call first.
  157. */
  158. nblocks = map_bh->b_size >> blkbits;
  159. if (buffer_mapped(map_bh) && block_in_file > *first_logical_block &&
  160. block_in_file < (*first_logical_block + nblocks)) {
  161. unsigned map_offset = block_in_file - *first_logical_block;
  162. unsigned last = nblocks - map_offset;
  163. for (relative_block = 0; ; relative_block++) {
  164. if (relative_block == last) {
  165. clear_buffer_mapped(map_bh);
  166. break;
  167. }
  168. if (page_block == blocks_per_page)
  169. break;
  170. blocks[page_block] = map_bh->b_blocknr + map_offset +
  171. relative_block;
  172. page_block++;
  173. block_in_file++;
  174. }
  175. bdev = map_bh->b_bdev;
  176. }
  177. /*
  178. * Then do more get_blocks calls until we are done with this page.
  179. */
  180. map_bh->b_page = page;
  181. while (page_block < blocks_per_page) {
  182. map_bh->b_state = 0;
  183. map_bh->b_size = 0;
  184. if (block_in_file < last_block) {
  185. map_bh->b_size = (last_block-block_in_file) << blkbits;
  186. if (get_block(inode, block_in_file, map_bh, 0))
  187. goto confused;
  188. *first_logical_block = block_in_file;
  189. }
  190. if (!buffer_mapped(map_bh)) {
  191. fully_mapped = 0;
  192. if (first_hole == blocks_per_page)
  193. first_hole = page_block;
  194. page_block++;
  195. block_in_file++;
  196. continue;
  197. }
  198. /* some filesystems will copy data into the page during
  199. * the get_block call, in which case we don't want to
  200. * read it again. map_buffer_to_page copies the data
  201. * we just collected from get_block into the page's buffers
  202. * so readpage doesn't have to repeat the get_block call
  203. */
  204. if (buffer_uptodate(map_bh)) {
  205. map_buffer_to_page(page, map_bh, page_block);
  206. goto confused;
  207. }
  208. if (first_hole != blocks_per_page)
  209. goto confused; /* hole -> non-hole */
  210. /* Contiguous blocks? */
  211. if (page_block && blocks[page_block-1] != map_bh->b_blocknr-1)
  212. goto confused;
  213. nblocks = map_bh->b_size >> blkbits;
  214. for (relative_block = 0; ; relative_block++) {
  215. if (relative_block == nblocks) {
  216. clear_buffer_mapped(map_bh);
  217. break;
  218. } else if (page_block == blocks_per_page)
  219. break;
  220. blocks[page_block] = map_bh->b_blocknr+relative_block;
  221. page_block++;
  222. block_in_file++;
  223. }
  224. bdev = map_bh->b_bdev;
  225. }
  226. if (first_hole != blocks_per_page) {
  227. zero_user_segment(page, first_hole << blkbits, PAGE_CACHE_SIZE);
  228. if (first_hole == 0) {
  229. SetPageUptodate(page);
  230. unlock_page(page);
  231. goto out;
  232. }
  233. } else if (fully_mapped) {
  234. SetPageMappedToDisk(page);
  235. }
  236. if (fully_mapped && blocks_per_page == 1 && !PageUptodate(page) &&
  237. cleancache_get_page(page) == 0) {
  238. SetPageUptodate(page);
  239. goto confused;
  240. }
  241. /*
  242. * This page will go to BIO. Do we need to send this BIO off first?
  243. */
  244. if (bio && (*last_block_in_bio != blocks[0] - 1))
  245. bio = mpage_bio_submit(READ, bio);
  246. alloc_new:
  247. if (bio == NULL) {
  248. if (first_hole == blocks_per_page) {
  249. if (!bdev_read_page(bdev, blocks[0] << (blkbits - 9),
  250. page))
  251. goto out;
  252. }
  253. bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
  254. min_t(int, nr_pages, BIO_MAX_PAGES),
  255. GFP_KERNEL);
  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. map_bh.b_state = 0;
  335. map_bh.b_size = 0;
  336. for (page_idx = 0; page_idx < nr_pages; page_idx++) {
  337. struct page *page = list_entry(pages->prev, struct page, lru);
  338. prefetchw(&page->flags);
  339. list_del(&page->lru);
  340. if (!add_to_page_cache_lru(page, mapping,
  341. page->index, GFP_KERNEL)) {
  342. bio = do_mpage_readpage(bio, page,
  343. nr_pages - page_idx,
  344. &last_block_in_bio, &map_bh,
  345. &first_logical_block,
  346. get_block);
  347. }
  348. page_cache_release(page);
  349. }
  350. BUG_ON(!list_empty(pages));
  351. if (bio)
  352. mpage_bio_submit(READ, bio);
  353. return 0;
  354. }
  355. EXPORT_SYMBOL(mpage_readpages);
  356. /*
  357. * This isn't called much at all
  358. */
  359. int mpage_readpage(struct page *page, get_block_t get_block)
  360. {
  361. struct bio *bio = NULL;
  362. sector_t last_block_in_bio = 0;
  363. struct buffer_head map_bh;
  364. unsigned long first_logical_block = 0;
  365. map_bh.b_state = 0;
  366. map_bh.b_size = 0;
  367. bio = do_mpage_readpage(bio, page, 1, &last_block_in_bio,
  368. &map_bh, &first_logical_block, get_block);
  369. if (bio)
  370. mpage_bio_submit(READ, bio);
  371. return 0;
  372. }
  373. EXPORT_SYMBOL(mpage_readpage);
  374. /*
  375. * Writing is not so simple.
  376. *
  377. * If the page has buffers then they will be used for obtaining the disk
  378. * mapping. We only support pages which are fully mapped-and-dirty, with a
  379. * special case for pages which are unmapped at the end: end-of-file.
  380. *
  381. * If the page has no buffers (preferred) then the page is mapped here.
  382. *
  383. * If all blocks are found to be contiguous then the page can go into the
  384. * BIO. Otherwise fall back to the mapping's writepage().
  385. *
  386. * FIXME: This code wants an estimate of how many pages are still to be
  387. * written, so it can intelligently allocate a suitably-sized BIO. For now,
  388. * just allocate full-size (16-page) BIOs.
  389. */
  390. struct mpage_data {
  391. struct bio *bio;
  392. sector_t last_block_in_bio;
  393. get_block_t *get_block;
  394. unsigned use_writepage;
  395. };
  396. /*
  397. * We have our BIO, so we can now mark the buffers clean. Make
  398. * sure to only clean buffers which we know we'll be writing.
  399. */
  400. static void clean_buffers(struct page *page, unsigned first_unmapped)
  401. {
  402. unsigned buffer_counter = 0;
  403. struct buffer_head *bh, *head;
  404. if (!page_has_buffers(page))
  405. return;
  406. head = page_buffers(page);
  407. bh = head;
  408. do {
  409. if (buffer_counter++ == first_unmapped)
  410. break;
  411. clear_buffer_dirty(bh);
  412. bh = bh->b_this_page;
  413. } while (bh != head);
  414. /*
  415. * we cannot drop the bh if the page is not uptodate or a concurrent
  416. * readpage would fail to serialize with the bh and it would read from
  417. * disk before we reach the platter.
  418. */
  419. if (buffer_heads_over_limit && PageUptodate(page))
  420. try_to_free_buffers(page);
  421. }
  422. static int __mpage_writepage(struct page *page, struct writeback_control *wbc,
  423. void *data)
  424. {
  425. struct mpage_data *mpd = data;
  426. struct bio *bio = mpd->bio;
  427. struct address_space *mapping = page->mapping;
  428. struct inode *inode = page->mapping->host;
  429. const unsigned blkbits = inode->i_blkbits;
  430. unsigned long end_index;
  431. const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
  432. sector_t last_block;
  433. sector_t block_in_file;
  434. sector_t blocks[MAX_BUF_PER_PAGE];
  435. unsigned page_block;
  436. unsigned first_unmapped = blocks_per_page;
  437. struct block_device *bdev = NULL;
  438. int boundary = 0;
  439. sector_t boundary_block = 0;
  440. struct block_device *boundary_bdev = NULL;
  441. int length;
  442. struct buffer_head map_bh;
  443. loff_t i_size = i_size_read(inode);
  444. int ret = 0;
  445. if (page_has_buffers(page)) {
  446. struct buffer_head *head = page_buffers(page);
  447. struct buffer_head *bh = head;
  448. /* If they're all mapped and dirty, do it */
  449. page_block = 0;
  450. do {
  451. BUG_ON(buffer_locked(bh));
  452. if (!buffer_mapped(bh)) {
  453. /*
  454. * unmapped dirty buffers are created by
  455. * __set_page_dirty_buffers -> mmapped data
  456. */
  457. if (buffer_dirty(bh))
  458. goto confused;
  459. if (first_unmapped == blocks_per_page)
  460. first_unmapped = page_block;
  461. continue;
  462. }
  463. if (first_unmapped != blocks_per_page)
  464. goto confused; /* hole -> non-hole */
  465. if (!buffer_dirty(bh) || !buffer_uptodate(bh))
  466. goto confused;
  467. if (page_block) {
  468. if (bh->b_blocknr != blocks[page_block-1] + 1)
  469. goto confused;
  470. }
  471. blocks[page_block++] = bh->b_blocknr;
  472. boundary = buffer_boundary(bh);
  473. if (boundary) {
  474. boundary_block = bh->b_blocknr;
  475. boundary_bdev = bh->b_bdev;
  476. }
  477. bdev = bh->b_bdev;
  478. } while ((bh = bh->b_this_page) != head);
  479. if (first_unmapped)
  480. goto page_is_mapped;
  481. /*
  482. * Page has buffers, but they are all unmapped. The page was
  483. * created by pagein or read over a hole which was handled by
  484. * block_read_full_page(). If this address_space is also
  485. * using mpage_readpages then this can rarely happen.
  486. */
  487. goto confused;
  488. }
  489. /*
  490. * The page has no buffers: map it to disk
  491. */
  492. BUG_ON(!PageUptodate(page));
  493. block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
  494. last_block = (i_size - 1) >> blkbits;
  495. map_bh.b_page = page;
  496. for (page_block = 0; page_block < blocks_per_page; ) {
  497. map_bh.b_state = 0;
  498. map_bh.b_size = 1 << blkbits;
  499. if (mpd->get_block(inode, block_in_file, &map_bh, 1))
  500. goto confused;
  501. if (buffer_new(&map_bh))
  502. unmap_underlying_metadata(map_bh.b_bdev,
  503. map_bh.b_blocknr);
  504. if (buffer_boundary(&map_bh)) {
  505. boundary_block = map_bh.b_blocknr;
  506. boundary_bdev = map_bh.b_bdev;
  507. }
  508. if (page_block) {
  509. if (map_bh.b_blocknr != blocks[page_block-1] + 1)
  510. goto confused;
  511. }
  512. blocks[page_block++] = map_bh.b_blocknr;
  513. boundary = buffer_boundary(&map_bh);
  514. bdev = map_bh.b_bdev;
  515. if (block_in_file == last_block)
  516. break;
  517. block_in_file++;
  518. }
  519. BUG_ON(page_block == 0);
  520. first_unmapped = page_block;
  521. page_is_mapped:
  522. end_index = i_size >> PAGE_CACHE_SHIFT;
  523. if (page->index >= end_index) {
  524. /*
  525. * The page straddles i_size. It must be zeroed out on each
  526. * and every writepage invocation because it may be mmapped.
  527. * "A file is mapped in multiples of the page size. For a file
  528. * that is not a multiple of the page size, the remaining memory
  529. * is zeroed when mapped, and writes to that region are not
  530. * written out to the file."
  531. */
  532. unsigned offset = i_size & (PAGE_CACHE_SIZE - 1);
  533. if (page->index > end_index || !offset)
  534. goto confused;
  535. zero_user_segment(page, offset, PAGE_CACHE_SIZE);
  536. }
  537. /*
  538. * This page will go to BIO. Do we need to send this BIO off first?
  539. */
  540. if (bio && mpd->last_block_in_bio != blocks[0] - 1)
  541. bio = mpage_bio_submit(WRITE, bio);
  542. alloc_new:
  543. if (bio == NULL) {
  544. if (first_unmapped == blocks_per_page) {
  545. if (!bdev_write_page(bdev, blocks[0] << (blkbits - 9),
  546. page, wbc)) {
  547. clean_buffers(page, first_unmapped);
  548. goto out;
  549. }
  550. }
  551. bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
  552. BIO_MAX_PAGES, GFP_NOFS|__GFP_HIGH);
  553. if (bio == NULL)
  554. goto confused;
  555. wbc_init_bio(wbc, bio);
  556. }
  557. /*
  558. * Must try to add the page before marking the buffer clean or
  559. * the confused fail path above (OOM) will be very confused when
  560. * it finds all bh marked clean (i.e. it will not write anything)
  561. */
  562. wbc_account_io(wbc, page, PAGE_SIZE);
  563. length = first_unmapped << blkbits;
  564. if (bio_add_page(bio, page, length, 0) < length) {
  565. bio = mpage_bio_submit(WRITE, bio);
  566. goto alloc_new;
  567. }
  568. clean_buffers(page, first_unmapped);
  569. BUG_ON(PageWriteback(page));
  570. set_page_writeback(page);
  571. unlock_page(page);
  572. if (boundary || (first_unmapped != blocks_per_page)) {
  573. bio = mpage_bio_submit(WRITE, bio);
  574. if (boundary_block) {
  575. write_boundary_block(boundary_bdev,
  576. boundary_block, 1 << blkbits);
  577. }
  578. } else {
  579. mpd->last_block_in_bio = blocks[blocks_per_page - 1];
  580. }
  581. goto out;
  582. confused:
  583. if (bio)
  584. bio = mpage_bio_submit(WRITE, bio);
  585. if (mpd->use_writepage) {
  586. ret = mapping->a_ops->writepage(page, wbc);
  587. } else {
  588. ret = -EAGAIN;
  589. goto out;
  590. }
  591. /*
  592. * The caller has a ref on the inode, so *mapping is stable
  593. */
  594. mapping_set_error(mapping, ret);
  595. out:
  596. mpd->bio = bio;
  597. return ret;
  598. }
  599. /**
  600. * mpage_writepages - walk the list of dirty pages of the given address space & writepage() all of them
  601. * @mapping: address space structure to write
  602. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  603. * @get_block: the filesystem's block mapper function.
  604. * If this is NULL then use a_ops->writepage. Otherwise, go
  605. * direct-to-BIO.
  606. *
  607. * This is a library function, which implements the writepages()
  608. * address_space_operation.
  609. *
  610. * If a page is already under I/O, generic_writepages() skips it, even
  611. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  612. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  613. * and msync() need to guarantee that all the data which was dirty at the time
  614. * the call was made get new I/O started against them. If wbc->sync_mode is
  615. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  616. * existing IO to complete.
  617. */
  618. int
  619. mpage_writepages(struct address_space *mapping,
  620. struct writeback_control *wbc, get_block_t get_block)
  621. {
  622. struct blk_plug plug;
  623. int ret;
  624. blk_start_plug(&plug);
  625. if (!get_block)
  626. ret = generic_writepages(mapping, wbc);
  627. else {
  628. struct mpage_data mpd = {
  629. .bio = NULL,
  630. .last_block_in_bio = 0,
  631. .get_block = get_block,
  632. .use_writepage = 1,
  633. };
  634. ret = write_cache_pages(mapping, wbc, __mpage_writepage, &mpd);
  635. if (mpd.bio)
  636. mpage_bio_submit(WRITE, mpd.bio);
  637. }
  638. blk_finish_plug(&plug);
  639. return ret;
  640. }
  641. EXPORT_SYMBOL(mpage_writepages);
  642. int mpage_writepage(struct page *page, get_block_t get_block,
  643. struct writeback_control *wbc)
  644. {
  645. struct mpage_data mpd = {
  646. .bio = NULL,
  647. .last_block_in_bio = 0,
  648. .get_block = get_block,
  649. .use_writepage = 0,
  650. };
  651. int ret = __mpage_writepage(page, wbc, &mpd);
  652. if (mpd.bio)
  653. mpage_bio_submit(WRITE, mpd.bio);
  654. return ret;
  655. }
  656. EXPORT_SYMBOL(mpage_writepage);