mpage.c 21 KB

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