mpage.c 20 KB

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