mpage.c 21 KB

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