mpage.c 20 KB

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