page-io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * linux/fs/ext4/page-io.c
  3. *
  4. * This contains the new page_io functions for ext4
  5. *
  6. * Written by Theodore Ts'o, 2010.
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/time.h>
  10. #include <linux/highuid.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/quotaops.h>
  13. #include <linux/string.h>
  14. #include <linux/buffer_head.h>
  15. #include <linux/writeback.h>
  16. #include <linux/pagevec.h>
  17. #include <linux/mpage.h>
  18. #include <linux/namei.h>
  19. #include <linux/uio.h>
  20. #include <linux/bio.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/kernel.h>
  23. #include <linux/slab.h>
  24. #include <linux/mm.h>
  25. #include <linux/backing-dev.h>
  26. #include "ext4_jbd2.h"
  27. #include "xattr.h"
  28. #include "acl.h"
  29. static struct kmem_cache *io_end_cachep;
  30. int __init ext4_init_pageio(void)
  31. {
  32. io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT);
  33. if (io_end_cachep == NULL)
  34. return -ENOMEM;
  35. return 0;
  36. }
  37. void ext4_exit_pageio(void)
  38. {
  39. kmem_cache_destroy(io_end_cachep);
  40. }
  41. /*
  42. * Print an buffer I/O error compatible with the fs/buffer.c. This
  43. * provides compatibility with dmesg scrapers that look for a specific
  44. * buffer I/O error message. We really need a unified error reporting
  45. * structure to userspace ala Digital Unix's uerf system, but it's
  46. * probably not going to happen in my lifetime, due to LKML politics...
  47. */
  48. static void buffer_io_error(struct buffer_head *bh)
  49. {
  50. printk_ratelimited(KERN_ERR "Buffer I/O error on device %pg, logical block %llu\n",
  51. bh->b_bdev,
  52. (unsigned long long)bh->b_blocknr);
  53. }
  54. static void ext4_finish_bio(struct bio *bio)
  55. {
  56. int i;
  57. struct bio_vec *bvec;
  58. bio_for_each_segment_all(bvec, bio, i) {
  59. struct page *page = bvec->bv_page;
  60. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  61. struct page *data_page = NULL;
  62. struct ext4_crypto_ctx *ctx = NULL;
  63. #endif
  64. struct buffer_head *bh, *head;
  65. unsigned bio_start = bvec->bv_offset;
  66. unsigned bio_end = bio_start + bvec->bv_len;
  67. unsigned under_io = 0;
  68. unsigned long flags;
  69. if (!page)
  70. continue;
  71. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  72. if (!page->mapping) {
  73. /* The bounce data pages are unmapped. */
  74. data_page = page;
  75. ctx = (struct ext4_crypto_ctx *)page_private(data_page);
  76. page = ctx->w.control_page;
  77. }
  78. #endif
  79. if (bio->bi_error) {
  80. SetPageError(page);
  81. set_bit(AS_EIO, &page->mapping->flags);
  82. }
  83. bh = head = page_buffers(page);
  84. /*
  85. * We check all buffers in the page under BH_Uptodate_Lock
  86. * to avoid races with other end io clearing async_write flags
  87. */
  88. local_irq_save(flags);
  89. bit_spin_lock(BH_Uptodate_Lock, &head->b_state);
  90. do {
  91. if (bh_offset(bh) < bio_start ||
  92. bh_offset(bh) + bh->b_size > bio_end) {
  93. if (buffer_async_write(bh))
  94. under_io++;
  95. continue;
  96. }
  97. clear_buffer_async_write(bh);
  98. if (bio->bi_error)
  99. buffer_io_error(bh);
  100. } while ((bh = bh->b_this_page) != head);
  101. bit_spin_unlock(BH_Uptodate_Lock, &head->b_state);
  102. local_irq_restore(flags);
  103. if (!under_io) {
  104. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  105. if (ctx)
  106. ext4_restore_control_page(data_page);
  107. #endif
  108. end_page_writeback(page);
  109. }
  110. }
  111. }
  112. static void ext4_release_io_end(ext4_io_end_t *io_end)
  113. {
  114. struct bio *bio, *next_bio;
  115. BUG_ON(!list_empty(&io_end->list));
  116. BUG_ON(io_end->flag & EXT4_IO_END_UNWRITTEN);
  117. WARN_ON(io_end->handle);
  118. for (bio = io_end->bio; bio; bio = next_bio) {
  119. next_bio = bio->bi_private;
  120. ext4_finish_bio(bio);
  121. bio_put(bio);
  122. }
  123. kmem_cache_free(io_end_cachep, io_end);
  124. }
  125. /*
  126. * Check a range of space and convert unwritten extents to written. Note that
  127. * we are protected from truncate touching same part of extent tree by the
  128. * fact that truncate code waits for all DIO to finish (thus exclusion from
  129. * direct IO is achieved) and also waits for PageWriteback bits. Thus we
  130. * cannot get to ext4_ext_truncate() before all IOs overlapping that range are
  131. * completed (happens from ext4_free_ioend()).
  132. */
  133. static int ext4_end_io(ext4_io_end_t *io)
  134. {
  135. struct inode *inode = io->inode;
  136. loff_t offset = io->offset;
  137. ssize_t size = io->size;
  138. handle_t *handle = io->handle;
  139. int ret = 0;
  140. ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
  141. "list->prev 0x%p\n",
  142. io, inode->i_ino, io->list.next, io->list.prev);
  143. io->handle = NULL; /* Following call will use up the handle */
  144. ret = ext4_convert_unwritten_extents(handle, inode, offset, size);
  145. if (ret < 0) {
  146. ext4_msg(inode->i_sb, KERN_EMERG,
  147. "failed to convert unwritten extents to written "
  148. "extents -- potential data loss! "
  149. "(inode %lu, offset %llu, size %zd, error %d)",
  150. inode->i_ino, offset, size, ret);
  151. }
  152. ext4_clear_io_unwritten_flag(io);
  153. ext4_release_io_end(io);
  154. return ret;
  155. }
  156. static void dump_completed_IO(struct inode *inode, struct list_head *head)
  157. {
  158. #ifdef EXT4FS_DEBUG
  159. struct list_head *cur, *before, *after;
  160. ext4_io_end_t *io, *io0, *io1;
  161. if (list_empty(head))
  162. return;
  163. ext4_debug("Dump inode %lu completed io list\n", inode->i_ino);
  164. list_for_each_entry(io, head, list) {
  165. cur = &io->list;
  166. before = cur->prev;
  167. io0 = container_of(before, ext4_io_end_t, list);
  168. after = cur->next;
  169. io1 = container_of(after, ext4_io_end_t, list);
  170. ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
  171. io, inode->i_ino, io0, io1);
  172. }
  173. #endif
  174. }
  175. /* Add the io_end to per-inode completed end_io list. */
  176. static void ext4_add_complete_io(ext4_io_end_t *io_end)
  177. {
  178. struct ext4_inode_info *ei = EXT4_I(io_end->inode);
  179. struct ext4_sb_info *sbi = EXT4_SB(io_end->inode->i_sb);
  180. struct workqueue_struct *wq;
  181. unsigned long flags;
  182. /* Only reserved conversions from writeback should enter here */
  183. WARN_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
  184. WARN_ON(!io_end->handle && sbi->s_journal);
  185. spin_lock_irqsave(&ei->i_completed_io_lock, flags);
  186. wq = sbi->rsv_conversion_wq;
  187. if (list_empty(&ei->i_rsv_conversion_list))
  188. queue_work(wq, &ei->i_rsv_conversion_work);
  189. list_add_tail(&io_end->list, &ei->i_rsv_conversion_list);
  190. spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
  191. }
  192. static int ext4_do_flush_completed_IO(struct inode *inode,
  193. struct list_head *head)
  194. {
  195. ext4_io_end_t *io;
  196. struct list_head unwritten;
  197. unsigned long flags;
  198. struct ext4_inode_info *ei = EXT4_I(inode);
  199. int err, ret = 0;
  200. spin_lock_irqsave(&ei->i_completed_io_lock, flags);
  201. dump_completed_IO(inode, head);
  202. list_replace_init(head, &unwritten);
  203. spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
  204. while (!list_empty(&unwritten)) {
  205. io = list_entry(unwritten.next, ext4_io_end_t, list);
  206. BUG_ON(!(io->flag & EXT4_IO_END_UNWRITTEN));
  207. list_del_init(&io->list);
  208. err = ext4_end_io(io);
  209. if (unlikely(!ret && err))
  210. ret = err;
  211. }
  212. return ret;
  213. }
  214. /*
  215. * work on completed IO, to convert unwritten extents to extents
  216. */
  217. void ext4_end_io_rsv_work(struct work_struct *work)
  218. {
  219. struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
  220. i_rsv_conversion_work);
  221. ext4_do_flush_completed_IO(&ei->vfs_inode, &ei->i_rsv_conversion_list);
  222. }
  223. ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
  224. {
  225. ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
  226. if (io) {
  227. io->inode = inode;
  228. INIT_LIST_HEAD(&io->list);
  229. atomic_set(&io->count, 1);
  230. }
  231. return io;
  232. }
  233. void ext4_put_io_end_defer(ext4_io_end_t *io_end)
  234. {
  235. if (atomic_dec_and_test(&io_end->count)) {
  236. if (!(io_end->flag & EXT4_IO_END_UNWRITTEN) || !io_end->size) {
  237. ext4_release_io_end(io_end);
  238. return;
  239. }
  240. ext4_add_complete_io(io_end);
  241. }
  242. }
  243. int ext4_put_io_end(ext4_io_end_t *io_end)
  244. {
  245. int err = 0;
  246. if (atomic_dec_and_test(&io_end->count)) {
  247. if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
  248. err = ext4_convert_unwritten_extents(io_end->handle,
  249. io_end->inode, io_end->offset,
  250. io_end->size);
  251. io_end->handle = NULL;
  252. ext4_clear_io_unwritten_flag(io_end);
  253. }
  254. ext4_release_io_end(io_end);
  255. }
  256. return err;
  257. }
  258. ext4_io_end_t *ext4_get_io_end(ext4_io_end_t *io_end)
  259. {
  260. atomic_inc(&io_end->count);
  261. return io_end;
  262. }
  263. /* BIO completion function for page writeback */
  264. static void ext4_end_bio(struct bio *bio)
  265. {
  266. ext4_io_end_t *io_end = bio->bi_private;
  267. sector_t bi_sector = bio->bi_iter.bi_sector;
  268. BUG_ON(!io_end);
  269. bio->bi_end_io = NULL;
  270. if (bio->bi_error) {
  271. struct inode *inode = io_end->inode;
  272. ext4_warning(inode->i_sb, "I/O error %d writing to inode %lu "
  273. "(offset %llu size %ld starting block %llu)",
  274. bio->bi_error, inode->i_ino,
  275. (unsigned long long) io_end->offset,
  276. (long) io_end->size,
  277. (unsigned long long)
  278. bi_sector >> (inode->i_blkbits - 9));
  279. mapping_set_error(inode->i_mapping, bio->bi_error);
  280. }
  281. if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
  282. /*
  283. * Link bio into list hanging from io_end. We have to do it
  284. * atomically as bio completions can be racing against each
  285. * other.
  286. */
  287. bio->bi_private = xchg(&io_end->bio, bio);
  288. ext4_put_io_end_defer(io_end);
  289. } else {
  290. /*
  291. * Drop io_end reference early. Inode can get freed once
  292. * we finish the bio.
  293. */
  294. ext4_put_io_end_defer(io_end);
  295. ext4_finish_bio(bio);
  296. bio_put(bio);
  297. }
  298. }
  299. void ext4_io_submit(struct ext4_io_submit *io)
  300. {
  301. struct bio *bio = io->io_bio;
  302. if (bio) {
  303. int io_op = io->io_wbc->sync_mode == WB_SYNC_ALL ?
  304. WRITE_SYNC : WRITE;
  305. submit_bio(io_op, io->io_bio);
  306. }
  307. io->io_bio = NULL;
  308. }
  309. void ext4_io_submit_init(struct ext4_io_submit *io,
  310. struct writeback_control *wbc)
  311. {
  312. io->io_wbc = wbc;
  313. io->io_bio = NULL;
  314. io->io_end = NULL;
  315. }
  316. static int io_submit_init_bio(struct ext4_io_submit *io,
  317. struct buffer_head *bh)
  318. {
  319. struct bio *bio;
  320. bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
  321. if (!bio)
  322. return -ENOMEM;
  323. wbc_init_bio(io->io_wbc, bio);
  324. bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  325. bio->bi_bdev = bh->b_bdev;
  326. bio->bi_end_io = ext4_end_bio;
  327. bio->bi_private = ext4_get_io_end(io->io_end);
  328. io->io_bio = bio;
  329. io->io_next_block = bh->b_blocknr;
  330. return 0;
  331. }
  332. static int io_submit_add_bh(struct ext4_io_submit *io,
  333. struct inode *inode,
  334. struct page *page,
  335. struct buffer_head *bh)
  336. {
  337. int ret;
  338. if (io->io_bio && bh->b_blocknr != io->io_next_block) {
  339. submit_and_retry:
  340. ext4_io_submit(io);
  341. }
  342. if (io->io_bio == NULL) {
  343. ret = io_submit_init_bio(io, bh);
  344. if (ret)
  345. return ret;
  346. }
  347. ret = bio_add_page(io->io_bio, page, bh->b_size, bh_offset(bh));
  348. if (ret != bh->b_size)
  349. goto submit_and_retry;
  350. wbc_account_io(io->io_wbc, page, bh->b_size);
  351. io->io_next_block++;
  352. return 0;
  353. }
  354. int ext4_bio_write_page(struct ext4_io_submit *io,
  355. struct page *page,
  356. int len,
  357. struct writeback_control *wbc,
  358. bool keep_towrite)
  359. {
  360. struct page *data_page = NULL;
  361. struct inode *inode = page->mapping->host;
  362. unsigned block_start, blocksize;
  363. struct buffer_head *bh, *head;
  364. int ret = 0;
  365. int nr_submitted = 0;
  366. int nr_to_submit = 0;
  367. blocksize = 1 << inode->i_blkbits;
  368. BUG_ON(!PageLocked(page));
  369. BUG_ON(PageWriteback(page));
  370. if (keep_towrite)
  371. set_page_writeback_keepwrite(page);
  372. else
  373. set_page_writeback(page);
  374. ClearPageError(page);
  375. /*
  376. * Comments copied from block_write_full_page:
  377. *
  378. * The page straddles i_size. It must be zeroed out on each and every
  379. * writepage invocation because it may be mmapped. "A file is mapped
  380. * in multiples of the page size. For a file that is not a multiple of
  381. * the page size, the remaining memory is zeroed when mapped, and
  382. * writes to that region are not written out to the file."
  383. */
  384. if (len < PAGE_SIZE)
  385. zero_user_segment(page, len, PAGE_SIZE);
  386. /*
  387. * In the first loop we prepare and mark buffers to submit. We have to
  388. * mark all buffers in the page before submitting so that
  389. * end_page_writeback() cannot be called from ext4_bio_end_io() when IO
  390. * on the first buffer finishes and we are still working on submitting
  391. * the second buffer.
  392. */
  393. bh = head = page_buffers(page);
  394. do {
  395. block_start = bh_offset(bh);
  396. if (block_start >= len) {
  397. clear_buffer_dirty(bh);
  398. set_buffer_uptodate(bh);
  399. continue;
  400. }
  401. if (!buffer_dirty(bh) || buffer_delay(bh) ||
  402. !buffer_mapped(bh) || buffer_unwritten(bh)) {
  403. /* A hole? We can safely clear the dirty bit */
  404. if (!buffer_mapped(bh))
  405. clear_buffer_dirty(bh);
  406. if (io->io_bio)
  407. ext4_io_submit(io);
  408. continue;
  409. }
  410. if (buffer_new(bh)) {
  411. clear_buffer_new(bh);
  412. unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  413. }
  414. set_buffer_async_write(bh);
  415. nr_to_submit++;
  416. } while ((bh = bh->b_this_page) != head);
  417. bh = head = page_buffers(page);
  418. if (ext4_encrypted_inode(inode) && S_ISREG(inode->i_mode) &&
  419. nr_to_submit) {
  420. gfp_t gfp_flags = GFP_NOFS;
  421. retry_encrypt:
  422. data_page = ext4_encrypt(inode, page, gfp_flags);
  423. if (IS_ERR(data_page)) {
  424. ret = PTR_ERR(data_page);
  425. if (ret == -ENOMEM && wbc->sync_mode == WB_SYNC_ALL) {
  426. if (io->io_bio) {
  427. ext4_io_submit(io);
  428. congestion_wait(BLK_RW_ASYNC, HZ/50);
  429. }
  430. gfp_flags |= __GFP_NOFAIL;
  431. goto retry_encrypt;
  432. }
  433. data_page = NULL;
  434. goto out;
  435. }
  436. }
  437. /* Now submit buffers to write */
  438. do {
  439. if (!buffer_async_write(bh))
  440. continue;
  441. ret = io_submit_add_bh(io, inode,
  442. data_page ? data_page : page, bh);
  443. if (ret) {
  444. /*
  445. * We only get here on ENOMEM. Not much else
  446. * we can do but mark the page as dirty, and
  447. * better luck next time.
  448. */
  449. break;
  450. }
  451. nr_submitted++;
  452. clear_buffer_dirty(bh);
  453. } while ((bh = bh->b_this_page) != head);
  454. /* Error stopped previous loop? Clean up buffers... */
  455. if (ret) {
  456. out:
  457. if (data_page)
  458. ext4_restore_control_page(data_page);
  459. printk_ratelimited(KERN_ERR "%s: ret = %d\n", __func__, ret);
  460. redirty_page_for_writepage(wbc, page);
  461. do {
  462. clear_buffer_async_write(bh);
  463. bh = bh->b_this_page;
  464. } while (bh != head);
  465. }
  466. unlock_page(page);
  467. /* Nothing submitted - we have to end page writeback */
  468. if (!nr_submitted)
  469. end_page_writeback(page);
  470. return ret;
  471. }