mpage.c 21 KB

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