mpage.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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 axboe@suse.de
  12. * use bio_add_page() to build bio's just the right size
  13. */
  14. /*
  15. * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * as published by the Free Software Foundation; either version 2
  20. * of the License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  29. */
  30. /************************************************************************/
  31. /* */
  32. /* PROJECT : exFAT & FAT12/16/32 File System */
  33. /* FILE : core.c */
  34. /* PURPOSE : sdFAT glue layer for supporting VFS */
  35. /* */
  36. /*----------------------------------------------------------------------*/
  37. /* NOTES */
  38. /* */
  39. /* */
  40. /************************************************************************/
  41. #include <linux/version.h>
  42. #include <linux/module.h>
  43. #include <linux/time.h>
  44. #include <linux/buffer_head.h>
  45. #include <linux/exportfs.h>
  46. #include <linux/mount.h>
  47. #include <linux/vfs.h>
  48. #include <linux/parser.h>
  49. #include <linux/uio.h>
  50. #include <linux/writeback.h>
  51. #include <linux/log2.h>
  52. #include <linux/hash.h>
  53. #include <linux/backing-dev.h>
  54. #include <linux/sched.h>
  55. #include <linux/fs_struct.h>
  56. #include <linux/namei.h>
  57. #include <linux/bio.h>
  58. #include <linux/blkdev.h>
  59. #include <linux/swap.h> /* for mark_page_accessed() */
  60. #include <asm/current.h>
  61. #include <asm/unaligned.h>
  62. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
  63. #include <linux/aio.h>
  64. #endif
  65. #include "sdfat.h"
  66. #ifdef CONFIG_SDFAT_ALIGNED_MPAGE_WRITE
  67. #define MIN_ALIGNED_SIZE (PAGE_SIZE)
  68. #define MIN_ALIGNED_SIZE_MASK (MIN_ALIGNED_SIZE - 1)
  69. /*************************************************************************
  70. * INNER FUNCTIONS FOR FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY
  71. *************************************************************************/
  72. static void __mpage_write_end_io(struct bio *bio, int err);
  73. /*************************************************************************
  74. * FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY
  75. *************************************************************************/
  76. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
  77. /* EMPTY */
  78. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 0) */
  79. static inline void bio_set_dev(struct bio *bio, struct block_device *bdev)
  80. {
  81. bio->bi_bdev = bdev;
  82. }
  83. #endif
  84. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)
  85. static inline void __sdfat_clean_bdev_aliases(struct block_device *bdev, sector_t block)
  86. {
  87. clean_bdev_aliases(bdev, block, 1);
  88. }
  89. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4,10,0) */
  90. static inline void __sdfat_clean_bdev_aliases(struct block_device *bdev, sector_t block)
  91. {
  92. unmap_underlying_metadata(bdev, block);
  93. }
  94. static inline int wbc_to_write_flags(struct writeback_control *wbc)
  95. {
  96. if (wbc->sync_mode == WB_SYNC_ALL)
  97. return WRITE_SYNC;
  98. return 0;
  99. }
  100. #endif
  101. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
  102. static inline void __sdfat_submit_bio_write2(int flags, struct bio *bio)
  103. {
  104. bio_set_op_attrs(bio, REQ_OP_WRITE, flags);
  105. submit_bio(bio);
  106. }
  107. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4,8,0) */
  108. static inline void __sdfat_submit_bio_write2(int flags, struct bio *bio)
  109. {
  110. submit_bio(WRITE | flags, bio);
  111. }
  112. #endif
  113. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)
  114. static inline int bio_get_nr_vecs(struct block_device *bdev)
  115. {
  116. return BIO_MAX_PAGES;
  117. }
  118. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4,1,0) */
  119. /* EMPTY */
  120. #endif
  121. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
  122. static inline sector_t __sdfat_bio_sector(struct bio *bio)
  123. {
  124. return bio->bi_iter.bi_sector;
  125. }
  126. static inline void __sdfat_set_bio_sector(struct bio *bio, sector_t sector)
  127. {
  128. bio->bi_iter.bi_sector = sector;
  129. }
  130. static inline unsigned int __sdfat_bio_size(struct bio *bio)
  131. {
  132. return bio->bi_iter.bi_size;
  133. }
  134. static inline void __sdfat_set_bio_size(struct bio *bio, unsigned int size)
  135. {
  136. bio->bi_iter.bi_size = size;
  137. }
  138. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0) */
  139. static inline sector_t __sdfat_bio_sector(struct bio *bio)
  140. {
  141. return bio->bi_sector;
  142. }
  143. static inline void __sdfat_set_bio_sector(struct bio *bio, sector_t sector)
  144. {
  145. bio->bi_sector = sector;
  146. }
  147. static inline unsigned int __sdfat_bio_size(struct bio *bio)
  148. {
  149. return bio->bi_size;
  150. }
  151. static inline void __sdfat_set_bio_size(struct bio *bio, unsigned int size)
  152. {
  153. bio->bi_size = size;
  154. }
  155. #endif
  156. /*************************************************************************
  157. * MORE FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY
  158. *************************************************************************/
  159. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 13, 0)
  160. static void mpage_write_end_io(struct bio *bio)
  161. {
  162. __mpage_write_end_io(bio, blk_status_to_errno(bio->bi_status));
  163. }
  164. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
  165. static void mpage_write_end_io(struct bio *bio)
  166. {
  167. __mpage_write_end_io(bio, bio->bi_error);
  168. }
  169. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4,3,0) */
  170. static void mpage_write_end_io(struct bio *bio, int err)
  171. {
  172. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  173. err = 0;
  174. __mpage_write_end_io(bio, err);
  175. }
  176. #endif
  177. /* __check_dfr_on() and __dfr_writepage_end_io() functions
  178. * are copied from sdfat.c
  179. * Each function should be same perfectly
  180. */
  181. static inline int __check_dfr_on(struct inode *inode, loff_t start, loff_t end, const char *fname)
  182. {
  183. #ifdef CONFIG_SDFAT_DFR
  184. struct defrag_info *ino_dfr = &(SDFAT_I(inode)->dfr_info);
  185. if ((atomic_read(&ino_dfr->stat) == DFR_INO_STAT_REQ) &&
  186. fsapi_dfr_check_dfr_on(inode, start, end, 0, fname))
  187. return 1;
  188. #endif
  189. return 0;
  190. }
  191. static inline int __dfr_writepage_end_io(struct page *page)
  192. {
  193. #ifdef CONFIG_SDFAT_DFR
  194. struct defrag_info *ino_dfr = &(SDFAT_I(page->mapping->host)->dfr_info);
  195. if (atomic_read(&ino_dfr->stat) == DFR_INO_STAT_REQ)
  196. fsapi_dfr_writepage_endio(page);
  197. #endif
  198. return 0;
  199. }
  200. static inline unsigned int __calc_size_to_align(struct super_block *sb)
  201. {
  202. struct block_device *bdev = sb->s_bdev;
  203. struct gendisk *disk;
  204. struct request_queue *queue;
  205. struct queue_limits *limit;
  206. unsigned int max_sectors;
  207. unsigned int aligned = 0;
  208. disk = bdev->bd_disk;
  209. if (!disk)
  210. goto out;
  211. queue = disk->queue;
  212. if (!queue)
  213. goto out;
  214. limit = &queue->limits;
  215. max_sectors = limit->max_sectors;
  216. aligned = 1 << ilog2(max_sectors);
  217. if (aligned && (max_sectors & (aligned - 1)))
  218. aligned = 0;
  219. if (aligned && aligned < (MIN_ALIGNED_SIZE >> SECTOR_SIZE_BITS))
  220. aligned = 0;
  221. out:
  222. return aligned;
  223. }
  224. struct mpage_data {
  225. struct bio *bio;
  226. sector_t last_block_in_bio;
  227. get_block_t *get_block;
  228. unsigned int use_writepage;
  229. unsigned int size_to_align;
  230. };
  231. /*
  232. * After completing I/O on a page, call this routine to update the page
  233. * flags appropriately
  234. */
  235. static void __page_write_endio(struct page *page, int err)
  236. {
  237. if (err) {
  238. struct address_space *mapping;
  239. SetPageError(page);
  240. mapping = page_mapping(page);
  241. if (mapping)
  242. mapping_set_error(mapping, err);
  243. }
  244. __dfr_writepage_end_io(page);
  245. end_page_writeback(page);
  246. }
  247. /*
  248. * I/O completion handler for multipage BIOs.
  249. *
  250. * The mpage code never puts partial pages into a BIO (except for end-of-file).
  251. * If a page does not map to a contiguous run of blocks then it simply falls
  252. * back to block_read_full_page().
  253. *
  254. * Why is this? If a page's completion depends on a number of different BIOs
  255. * which can complete in any order (or at the same time) then determining the
  256. * status of that page is hard. See end_buffer_async_read() for the details.
  257. * There is no point in duplicating all that complexity.
  258. */
  259. static void __mpage_write_end_io(struct bio *bio, int err)
  260. {
  261. struct bio_vec *bv;
  262. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 2, 0)
  263. struct bvec_iter_all iter_all;
  264. ASSERT(bio_data_dir(bio) == WRITE); /* only write */
  265. /* Use bio_for_each_segemnt_all() to support multi-page bvec */
  266. bio_for_each_segment_all(bv, bio, iter_all)
  267. __page_write_endio(bv->bv_page, err);
  268. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0)
  269. struct bvec_iter_all iter_all;
  270. int i;
  271. ASSERT(bio_data_dir(bio) == WRITE); /* only write */
  272. /* Use bio_for_each_segemnt_all() to support multi-page bvec */
  273. bio_for_each_segment_all(bv, bio, i, iter_all)
  274. __page_write_endio(bv->bv_page, err);
  275. #else
  276. ASSERT(bio_data_dir(bio) == WRITE); /* only write */
  277. bv = bio->bi_io_vec + bio->bi_vcnt - 1;
  278. do {
  279. struct page *page = bv->bv_page;
  280. if (--bv >= bio->bi_io_vec)
  281. prefetchw(&bv->bv_page->flags);
  282. __page_write_endio(page, err);
  283. } while (bv >= bio->bi_io_vec);
  284. #endif
  285. bio_put(bio);
  286. }
  287. static struct bio *mpage_bio_submit_write(int flags, struct bio *bio)
  288. {
  289. bio->bi_end_io = mpage_write_end_io;
  290. __sdfat_submit_bio_write2(flags, bio);
  291. return NULL;
  292. }
  293. static struct bio *
  294. mpage_alloc(struct block_device *bdev,
  295. sector_t first_sector, int nr_vecs,
  296. gfp_t gfp_flags)
  297. {
  298. struct bio *bio;
  299. bio = bio_alloc(gfp_flags, nr_vecs);
  300. if (bio == NULL && (current->flags & PF_MEMALLOC)) {
  301. while (!bio && (nr_vecs /= 2))
  302. bio = bio_alloc(gfp_flags, nr_vecs);
  303. }
  304. if (bio) {
  305. bio_set_dev(bio, bdev);
  306. __sdfat_set_bio_sector(bio, first_sector);
  307. }
  308. return bio;
  309. }
  310. #if IS_BUILTIN(CONFIG_SDFAT_FS)
  311. #define __write_boundary_block write_boundary_block
  312. #define sdfat_buffer_heads_over_limit buffer_heads_over_limit
  313. #else
  314. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  315. /*
  316. * Called when we've recently written block `bblock', and it is known that
  317. * `bblock' was for a buffer_boundary() buffer. This means that the block at
  318. * `bblock + 1' is probably a dirty indirect block. Hunt it down and, if it's
  319. * dirty, schedule it for IO. So that indirects merge nicely with their data.
  320. */
  321. static void __write_boundary_block(struct block_device *bdev,
  322. sector_t bblock, unsigned int blocksize)
  323. {
  324. struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
  325. if (bh) {
  326. if (buffer_dirty(bh))
  327. ll_rw_block(REQ_OP_WRITE, 0, 1, &bh);
  328. put_bh(bh);
  329. }
  330. }
  331. #else
  332. #warning "Need an alternative of write_boundary_block function"
  333. #define __write_boundary_block write_boundary_block
  334. #endif
  335. #warning "sdfat could not check buffer_heads_over_limit on module. Assumed zero"
  336. #define sdfat_buffer_heads_over_limit (0)
  337. #endif
  338. static void clean_buffers(struct page *page, unsigned int first_unmapped)
  339. {
  340. unsigned int buffer_counter = 0;
  341. struct buffer_head *bh, *head;
  342. if (!page_has_buffers(page))
  343. return;
  344. head = page_buffers(page);
  345. bh = head;
  346. do {
  347. if (buffer_counter++ == first_unmapped)
  348. break;
  349. clear_buffer_dirty(bh);
  350. bh = bh->b_this_page;
  351. } while (bh != head);
  352. /*
  353. * we cannot drop the bh if the page is not uptodate or a concurrent
  354. * readpage would fail to serialize with the bh and it would read from
  355. * disk before we reach the platter.
  356. */
  357. if (sdfat_buffer_heads_over_limit && PageUptodate(page))
  358. try_to_free_buffers(page);
  359. }
  360. static int sdfat_mpage_writepage(struct page *page,
  361. struct writeback_control *wbc, void *data)
  362. {
  363. struct mpage_data *mpd = data;
  364. struct bio *bio = mpd->bio;
  365. struct address_space *mapping = page->mapping;
  366. struct inode *inode = page->mapping->host;
  367. const unsigned int blkbits = inode->i_blkbits;
  368. const unsigned int blocks_per_page = PAGE_SIZE >> blkbits;
  369. sector_t last_block;
  370. sector_t block_in_file;
  371. sector_t blocks[MAX_BUF_PER_PAGE];
  372. unsigned int page_block;
  373. unsigned int first_unmapped = blocks_per_page;
  374. struct block_device *bdev = NULL;
  375. int boundary = 0;
  376. sector_t boundary_block = 0;
  377. struct block_device *boundary_bdev = NULL;
  378. int length;
  379. struct buffer_head map_bh;
  380. loff_t i_size = i_size_read(inode);
  381. unsigned long end_index = i_size >> PAGE_SHIFT;
  382. int ret = 0;
  383. int op_flags = wbc_to_write_flags(wbc);
  384. if (page_has_buffers(page)) {
  385. struct buffer_head *head = page_buffers(page);
  386. struct buffer_head *bh = head;
  387. /* If they're all mapped and dirty, do it */
  388. page_block = 0;
  389. do {
  390. BUG_ON(buffer_locked(bh));
  391. if (!buffer_mapped(bh)) {
  392. /*
  393. * unmapped dirty buffers are created by
  394. * __set_page_dirty_buffers -> mmapped data
  395. */
  396. if (buffer_dirty(bh))
  397. goto confused;
  398. if (first_unmapped == blocks_per_page)
  399. first_unmapped = page_block;
  400. continue;
  401. }
  402. if (first_unmapped != blocks_per_page)
  403. goto confused; /* hole -> non-hole */
  404. if (!buffer_dirty(bh) || !buffer_uptodate(bh))
  405. goto confused;
  406. /* bh should be mapped if delay is set */
  407. if (buffer_delay(bh)) {
  408. sector_t blk_in_file =
  409. (sector_t)(page->index << (PAGE_SHIFT - blkbits)) + page_block;
  410. BUG_ON(bh->b_size != (1 << blkbits));
  411. if (page->index > end_index) {
  412. MMSG("%s(inode:%p) "
  413. "over end with delayed buffer"
  414. "(page_idx:%u, end_idx:%u)\n",
  415. __func__, inode,
  416. (u32)page->index,
  417. (u32)end_index);
  418. goto confused;
  419. }
  420. ret = mpd->get_block(inode, blk_in_file, bh, 1);
  421. if (ret) {
  422. MMSG("%s(inode:%p) "
  423. "failed to getblk(ret:%d)\n",
  424. __func__, inode, ret);
  425. goto confused;
  426. }
  427. BUG_ON(buffer_delay(bh));
  428. if (buffer_new(bh)) {
  429. clear_buffer_new(bh);
  430. __sdfat_clean_bdev_aliases(bh->b_bdev, bh->b_blocknr);
  431. }
  432. }
  433. if (page_block) {
  434. if (bh->b_blocknr != blocks[page_block-1] + 1) {
  435. MMSG("%s(inode:%p) pblk(%d) "
  436. "no_seq(prev:%lld, new:%lld)\n",
  437. __func__, inode, page_block,
  438. (u64)blocks[page_block-1],
  439. (u64)bh->b_blocknr);
  440. goto confused;
  441. }
  442. }
  443. blocks[page_block++] = bh->b_blocknr;
  444. boundary = buffer_boundary(bh);
  445. if (boundary) {
  446. boundary_block = bh->b_blocknr;
  447. boundary_bdev = bh->b_bdev;
  448. }
  449. bdev = bh->b_bdev;
  450. } while ((bh = bh->b_this_page) != head);
  451. if (first_unmapped)
  452. goto page_is_mapped;
  453. /*
  454. * Page has buffers, but they are all unmapped. The page was
  455. * created by pagein or read over a hole which was handled by
  456. * block_read_full_page(). If this address_space is also
  457. * using mpage_readpages then this can rarely happen.
  458. */
  459. goto confused;
  460. }
  461. /*
  462. * The page has no buffers: map it to disk
  463. */
  464. BUG_ON(!PageUptodate(page));
  465. block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
  466. last_block = (i_size - 1) >> blkbits;
  467. map_bh.b_page = page;
  468. for (page_block = 0; page_block < blocks_per_page; ) {
  469. map_bh.b_state = 0;
  470. map_bh.b_size = 1 << blkbits;
  471. if (mpd->get_block(inode, block_in_file, &map_bh, 1))
  472. goto confused;
  473. if (buffer_new(&map_bh))
  474. __sdfat_clean_bdev_aliases(map_bh.b_bdev, map_bh.b_blocknr);
  475. if (buffer_boundary(&map_bh)) {
  476. boundary_block = map_bh.b_blocknr;
  477. boundary_bdev = map_bh.b_bdev;
  478. }
  479. if (page_block) {
  480. if (map_bh.b_blocknr != blocks[page_block-1] + 1)
  481. goto confused;
  482. }
  483. blocks[page_block++] = map_bh.b_blocknr;
  484. boundary = buffer_boundary(&map_bh);
  485. bdev = map_bh.b_bdev;
  486. if (block_in_file == last_block)
  487. break;
  488. block_in_file++;
  489. }
  490. BUG_ON(page_block == 0);
  491. first_unmapped = page_block;
  492. page_is_mapped:
  493. if (page->index >= end_index) {
  494. /*
  495. * The page straddles i_size. It must be zeroed out on each
  496. * and every writepage invocation because it may be mmapped.
  497. * "A file is mapped in multiples of the page size. For a file
  498. * that is not a multiple of the page size, the remaining memory
  499. * is zeroed when mapped, and writes to that region are not
  500. * written out to the file."
  501. */
  502. unsigned int offset = i_size & (PAGE_SIZE - 1);
  503. if (page->index > end_index || !offset) {
  504. MMSG("%s(inode:%p) over end "
  505. "(page_idx:%u, end_idx:%u off:%u)\n",
  506. __func__, inode, (u32)page->index,
  507. (u32)end_index, (u32)offset);
  508. goto confused;
  509. }
  510. zero_user_segment(page, offset, PAGE_SIZE);
  511. }
  512. /*
  513. * This page will go to BIO. Do we need to send this BIO off first?
  514. *
  515. * REMARK : added ELSE_IF for ALIGNMENT_MPAGE_WRITE of SDFAT
  516. */
  517. if (bio) {
  518. if (mpd->last_block_in_bio != blocks[0] - 1) {
  519. bio = mpage_bio_submit_write(op_flags, bio);
  520. } else if (mpd->size_to_align) {
  521. unsigned int mask = mpd->size_to_align - 1;
  522. sector_t max_end_block =
  523. (__sdfat_bio_sector(bio) & ~(mask)) + mask;
  524. if ((__sdfat_bio_size(bio) & MIN_ALIGNED_SIZE_MASK) &&
  525. (mpd->last_block_in_bio == max_end_block)) {
  526. int op_nomerge = op_flags | REQ_NOMERGE;
  527. MMSG("%s(inode:%p) alignment mpage_bio_submit"
  528. "(start:%u, len:%u size:%u aligned:%u)\n",
  529. __func__, inode,
  530. (unsigned int)__sdfat_bio_sector(bio),
  531. (unsigned int)(mpd->last_block_in_bio -
  532. __sdfat_bio_sector(bio) + 1),
  533. (unsigned int)__sdfat_bio_size(bio),
  534. (unsigned int)mpd->size_to_align);
  535. bio = mpage_bio_submit_write(op_nomerge, bio);
  536. }
  537. }
  538. }
  539. alloc_new:
  540. if (!bio) {
  541. bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
  542. bio_get_nr_vecs(bdev), GFP_NOFS|__GFP_HIGH);
  543. if (!bio)
  544. goto confused;
  545. }
  546. /*
  547. * Must try to add the page before marking the buffer clean or
  548. * the confused fail path above (OOM) will be very confused when
  549. * it finds all bh marked clean (i.e. it will not write anything)
  550. */
  551. length = first_unmapped << blkbits;
  552. if (bio_add_page(bio, page, length, 0) < length) {
  553. bio = mpage_bio_submit_write(op_flags, bio);
  554. goto alloc_new;
  555. }
  556. /*
  557. * OK, we have our BIO, so we can now mark the buffers clean. Make
  558. * sure to only clean buffers which we know we'll be writing.
  559. */
  560. clean_buffers(page, first_unmapped);
  561. BUG_ON(PageWriteback(page));
  562. set_page_writeback(page);
  563. /*
  564. * FIXME FOR DEFRAGMENTATION : CODE REVIEW IS REQUIRED
  565. *
  566. * Turn off MAPPED flag in victim's bh if defrag on.
  567. * Another write_begin can starts after get_block for defrag victims
  568. * called.
  569. * In this case, write_begin calls get_block and get original block
  570. * number and previous defrag will be canceled.
  571. */
  572. if (unlikely(__check_dfr_on(inode, (loff_t)(page->index << PAGE_SHIFT),
  573. (loff_t)((page->index + 1) << PAGE_SHIFT), __func__))) {
  574. struct buffer_head *head = page_buffers(page);
  575. struct buffer_head *bh = head;
  576. do {
  577. clear_buffer_mapped(bh);
  578. bh = bh->b_this_page;
  579. } while (bh != head);
  580. }
  581. unlock_page(page);
  582. if (boundary || (first_unmapped != blocks_per_page)) {
  583. bio = mpage_bio_submit_write(op_flags, bio);
  584. if (boundary_block) {
  585. __write_boundary_block(boundary_bdev,
  586. boundary_block, 1 << blkbits);
  587. }
  588. } else {
  589. mpd->last_block_in_bio = blocks[blocks_per_page - 1];
  590. }
  591. goto out;
  592. confused:
  593. if (bio)
  594. bio = mpage_bio_submit_write(op_flags, bio);
  595. if (mpd->use_writepage) {
  596. ret = mapping->a_ops->writepage(page, wbc);
  597. } else {
  598. ret = -EAGAIN;
  599. goto out;
  600. }
  601. /*
  602. * The caller has a ref on the inode, so *mapping is stable
  603. */
  604. mapping_set_error(mapping, ret);
  605. out:
  606. mpd->bio = bio;
  607. return ret;
  608. }
  609. int sdfat_mpage_writepages(struct address_space *mapping,
  610. struct writeback_control *wbc, get_block_t *get_block)
  611. {
  612. struct blk_plug plug;
  613. int ret;
  614. struct mpage_data mpd = {
  615. .bio = NULL,
  616. .last_block_in_bio = 0,
  617. .get_block = get_block,
  618. .use_writepage = 1,
  619. .size_to_align = __calc_size_to_align(mapping->host->i_sb),
  620. };
  621. BUG_ON(!get_block);
  622. blk_start_plug(&plug);
  623. ret = write_cache_pages(mapping, wbc, sdfat_mpage_writepage, &mpd);
  624. if (mpd.bio) {
  625. int op_flags = wbc_to_write_flags(wbc);
  626. mpage_bio_submit_write(op_flags, mpd.bio);
  627. }
  628. blk_finish_plug(&plug);
  629. return ret;
  630. }
  631. #endif /* CONFIG_SDFAT_ALIGNED_MPAGE_WRITE */