mpage.c 20 KB

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