mpage.c 20 KB

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