mpage.c 21 KB

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