mpage.c 22 KB

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