page-io.c 15 KB

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