mpage.c 21 KB

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