mpage.c 21 KB

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