mpage.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. 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. /**
  145. * mpage_readpages - populate an address space with some pages, and
  146. * start reads against them.
  147. *
  148. * @mapping: the address_space
  149. * @pages: The address of a list_head which contains the target pages. These
  150. * pages have their ->index populated and are otherwise uninitialised.
  151. *
  152. * The page at @pages->prev has the lowest file offset, and reads should be
  153. * issued in @pages->prev to @pages->next order.
  154. *
  155. * @nr_pages: The number of pages at *@pages
  156. * @get_block: The filesystem's block mapper function.
  157. *
  158. * This function walks the pages and the blocks within each page, building and
  159. * emitting large BIOs.
  160. *
  161. * If anything unusual happens, such as:
  162. *
  163. * - encountering a page which has buffers
  164. * - encountering a page which has a non-hole after a hole
  165. * - encountering a page with non-contiguous blocks
  166. *
  167. * then this code just gives up and calls the buffer_head-based read function.
  168. * It does handle a page which has holes at the end - that is a common case:
  169. * the end-of-file on blocksize < PAGE_CACHE_SIZE setups.
  170. *
  171. * BH_Boundary explanation:
  172. *
  173. * There is a problem. The mpage read code assembles several pages, gets all
  174. * their disk mappings, and then submits them all. That's fine, but obtaining
  175. * the disk mappings may require I/O. Reads of indirect blocks, for example.
  176. *
  177. * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
  178. * submitted in the following order:
  179. * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
  180. * because the indirect block has to be read to get the mappings of blocks
  181. * 13,14,15,16. Obviously, this impacts performance.
  182. *
  183. * So what we do it to allow the filesystem's get_block() function to set
  184. * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block
  185. * after this one will require I/O against a block which is probably close to
  186. * this one. So you should push what I/O you have currently accumulated.
  187. *
  188. * This all causes the disk requests to be issued in the correct order.
  189. */
  190. static struct bio *
  191. do_mpage_readpage(struct bio *bio, struct page *page, unsigned nr_pages,
  192. sector_t *last_block_in_bio, get_block_t get_block)
  193. {
  194. struct inode *inode = page->mapping->host;
  195. const unsigned blkbits = inode->i_blkbits;
  196. const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
  197. const unsigned blocksize = 1 << blkbits;
  198. sector_t block_in_file;
  199. sector_t last_block;
  200. sector_t blocks[MAX_BUF_PER_PAGE];
  201. unsigned page_block;
  202. unsigned first_hole = blocks_per_page;
  203. struct block_device *bdev = NULL;
  204. struct buffer_head bh;
  205. int length;
  206. int fully_mapped = 1;
  207. if (page_has_buffers(page))
  208. goto confused;
  209. block_in_file = page->index << (PAGE_CACHE_SHIFT - blkbits);
  210. last_block = (i_size_read(inode) + blocksize - 1) >> blkbits;
  211. bh.b_page = page;
  212. for (page_block = 0; page_block < blocks_per_page;
  213. page_block++, block_in_file++) {
  214. bh.b_state = 0;
  215. if (block_in_file < last_block) {
  216. if (get_block(inode, block_in_file, &bh, 0))
  217. goto confused;
  218. }
  219. if (!buffer_mapped(&bh)) {
  220. fully_mapped = 0;
  221. if (first_hole == blocks_per_page)
  222. first_hole = page_block;
  223. continue;
  224. }
  225. /* some filesystems will copy data into the page during
  226. * the get_block call, in which case we don't want to
  227. * read it again. map_buffer_to_page copies the data
  228. * we just collected from get_block into the page's buffers
  229. * so readpage doesn't have to repeat the get_block call
  230. */
  231. if (buffer_uptodate(&bh)) {
  232. map_buffer_to_page(page, &bh, page_block);
  233. goto confused;
  234. }
  235. if (first_hole != blocks_per_page)
  236. goto confused; /* hole -> non-hole */
  237. /* Contiguous blocks? */
  238. if (page_block && blocks[page_block-1] != bh.b_blocknr-1)
  239. goto confused;
  240. blocks[page_block] = bh.b_blocknr;
  241. bdev = bh.b_bdev;
  242. }
  243. if (first_hole != blocks_per_page) {
  244. char *kaddr = kmap_atomic(page, KM_USER0);
  245. memset(kaddr + (first_hole << blkbits), 0,
  246. PAGE_CACHE_SIZE - (first_hole << blkbits));
  247. flush_dcache_page(page);
  248. kunmap_atomic(kaddr, KM_USER0);
  249. if (first_hole == 0) {
  250. SetPageUptodate(page);
  251. unlock_page(page);
  252. goto out;
  253. }
  254. } else if (fully_mapped) {
  255. SetPageMappedToDisk(page);
  256. }
  257. /*
  258. * This page will go to BIO. Do we need to send this BIO off first?
  259. */
  260. if (bio && (*last_block_in_bio != blocks[0] - 1))
  261. bio = mpage_bio_submit(READ, bio);
  262. alloc_new:
  263. if (bio == NULL) {
  264. bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
  265. min_t(int, nr_pages, bio_get_nr_vecs(bdev)),
  266. GFP_KERNEL);
  267. if (bio == NULL)
  268. goto confused;
  269. }
  270. length = first_hole << blkbits;
  271. if (bio_add_page(bio, page, length, 0) < length) {
  272. bio = mpage_bio_submit(READ, bio);
  273. goto alloc_new;
  274. }
  275. if (buffer_boundary(&bh) || (first_hole != blocks_per_page))
  276. bio = mpage_bio_submit(READ, bio);
  277. else
  278. *last_block_in_bio = blocks[blocks_per_page - 1];
  279. out:
  280. return bio;
  281. confused:
  282. if (bio)
  283. bio = mpage_bio_submit(READ, bio);
  284. if (!PageUptodate(page))
  285. block_read_full_page(page, get_block);
  286. else
  287. unlock_page(page);
  288. goto out;
  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. return __mpage_writepages(mapping, wbc, get_block,
  572. mapping->a_ops->writepage);
  573. }
  574. int
  575. __mpage_writepages(struct address_space *mapping,
  576. struct writeback_control *wbc, get_block_t get_block,
  577. writepage_t writepage_fn)
  578. {
  579. struct backing_dev_info *bdi = mapping->backing_dev_info;
  580. struct bio *bio = NULL;
  581. sector_t last_block_in_bio = 0;
  582. int ret = 0;
  583. int done = 0;
  584. int (*writepage)(struct page *page, struct writeback_control *wbc);
  585. struct pagevec pvec;
  586. int nr_pages;
  587. pgoff_t index;
  588. pgoff_t end = -1; /* Inclusive */
  589. int scanned = 0;
  590. int is_range = 0;
  591. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  592. wbc->encountered_congestion = 1;
  593. return 0;
  594. }
  595. writepage = NULL;
  596. if (get_block == NULL)
  597. writepage = mapping->a_ops->writepage;
  598. pagevec_init(&pvec, 0);
  599. if (wbc->sync_mode == WB_SYNC_NONE) {
  600. index = mapping->writeback_index; /* Start from prev offset */
  601. } else {
  602. index = 0; /* whole-file sweep */
  603. scanned = 1;
  604. }
  605. if (wbc->start || wbc->end) {
  606. index = wbc->start >> PAGE_CACHE_SHIFT;
  607. end = wbc->end >> PAGE_CACHE_SHIFT;
  608. is_range = 1;
  609. scanned = 1;
  610. }
  611. retry:
  612. while (!done && (index <= end) &&
  613. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  614. PAGECACHE_TAG_DIRTY,
  615. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  616. unsigned i;
  617. scanned = 1;
  618. for (i = 0; i < nr_pages; i++) {
  619. struct page *page = pvec.pages[i];
  620. /*
  621. * At this point we hold neither mapping->tree_lock nor
  622. * lock on the page itself: the page may be truncated or
  623. * invalidated (changing page->mapping to NULL), or even
  624. * swizzled back from swapper_space to tmpfs file
  625. * mapping
  626. */
  627. lock_page(page);
  628. if (unlikely(page->mapping != mapping)) {
  629. unlock_page(page);
  630. continue;
  631. }
  632. if (unlikely(is_range) && page->index > end) {
  633. done = 1;
  634. unlock_page(page);
  635. continue;
  636. }
  637. if (wbc->sync_mode != WB_SYNC_NONE)
  638. wait_on_page_writeback(page);
  639. if (PageWriteback(page) ||
  640. !clear_page_dirty_for_io(page)) {
  641. unlock_page(page);
  642. continue;
  643. }
  644. if (writepage) {
  645. ret = (*writepage)(page, wbc);
  646. if (ret) {
  647. if (ret == -ENOSPC)
  648. set_bit(AS_ENOSPC,
  649. &mapping->flags);
  650. else
  651. set_bit(AS_EIO,
  652. &mapping->flags);
  653. }
  654. } else {
  655. bio = __mpage_writepage(bio, page, get_block,
  656. &last_block_in_bio, &ret, wbc,
  657. writepage_fn);
  658. }
  659. if (ret || (--(wbc->nr_to_write) <= 0))
  660. done = 1;
  661. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  662. wbc->encountered_congestion = 1;
  663. done = 1;
  664. }
  665. }
  666. pagevec_release(&pvec);
  667. cond_resched();
  668. }
  669. if (!scanned && !done) {
  670. /*
  671. * We hit the last page and there is more work to be done: wrap
  672. * back to the start of the file
  673. */
  674. scanned = 1;
  675. index = 0;
  676. goto retry;
  677. }
  678. if (!is_range)
  679. mapping->writeback_index = index;
  680. if (bio)
  681. mpage_bio_submit(WRITE, bio);
  682. return ret;
  683. }
  684. EXPORT_SYMBOL(mpage_writepages);
  685. EXPORT_SYMBOL(__mpage_writepages);
  686. int mpage_writepage(struct page *page, get_block_t get_block,
  687. struct writeback_control *wbc)
  688. {
  689. int ret = 0;
  690. struct bio *bio;
  691. sector_t last_block_in_bio = 0;
  692. bio = __mpage_writepage(NULL, page, get_block,
  693. &last_block_in_bio, &ret, wbc, NULL);
  694. if (bio)
  695. mpage_bio_submit(WRITE, bio);
  696. return ret;
  697. }
  698. EXPORT_SYMBOL(mpage_writepage);