mpage.c 20 KB

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