page-io.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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/sched/mm.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 folio_iter fi;
  90. bio_for_each_folio_all(fi, bio) {
  91. struct folio *folio = fi.folio;
  92. struct folio *io_folio = NULL;
  93. struct buffer_head *bh, *head;
  94. size_t bio_start = fi.offset;
  95. size_t bio_end = bio_start + fi.length;
  96. unsigned under_io = 0;
  97. unsigned long flags;
  98. if (fscrypt_is_bounce_folio(folio)) {
  99. io_folio = folio;
  100. folio = fscrypt_pagecache_folio(folio);
  101. }
  102. if (bio->bi_status) {
  103. int err = blk_status_to_errno(bio->bi_status);
  104. mapping_set_error(folio->mapping, err);
  105. }
  106. bh = head = folio_buffers(folio);
  107. /*
  108. * We check all buffers in the folio under b_uptodate_lock
  109. * to avoid races with other end io clearing async_write flags
  110. */
  111. spin_lock_irqsave(&head->b_uptodate_lock, flags);
  112. do {
  113. if (bh_offset(bh) < bio_start ||
  114. bh_offset(bh) + bh->b_size > bio_end) {
  115. if (buffer_async_write(bh))
  116. under_io++;
  117. continue;
  118. }
  119. clear_buffer_async_write(bh);
  120. if (bio->bi_status) {
  121. set_buffer_write_io_error(bh);
  122. buffer_io_error(bh);
  123. }
  124. } while ((bh = bh->b_this_page) != head);
  125. spin_unlock_irqrestore(&head->b_uptodate_lock, flags);
  126. if (!under_io) {
  127. fscrypt_free_bounce_page(&io_folio->page);
  128. folio_end_writeback(folio);
  129. }
  130. }
  131. }
  132. static void ext4_release_io_end(ext4_io_end_t *io_end)
  133. {
  134. struct bio *bio, *next_bio;
  135. BUG_ON(!list_empty(&io_end->list));
  136. BUG_ON(io_end->flag & EXT4_IO_END_UNWRITTEN);
  137. WARN_ON(io_end->handle);
  138. for (bio = io_end->bio; bio; bio = next_bio) {
  139. next_bio = bio->bi_private;
  140. ext4_finish_bio(bio);
  141. bio_put(bio);
  142. }
  143. ext4_free_io_end_vec(io_end);
  144. kmem_cache_free(io_end_cachep, io_end);
  145. }
  146. /*
  147. * Check a range of space and convert unwritten extents to written. Note that
  148. * we are protected from truncate touching same part of extent tree by the
  149. * fact that truncate code waits for all DIO to finish (thus exclusion from
  150. * direct IO is achieved) and also waits for PageWriteback bits. Thus we
  151. * cannot get to ext4_ext_truncate() before all IOs overlapping that range are
  152. * completed (happens from ext4_free_ioend()).
  153. */
  154. static int ext4_end_io_end(ext4_io_end_t *io_end)
  155. {
  156. struct inode *inode = io_end->inode;
  157. handle_t *handle = io_end->handle;
  158. int ret = 0;
  159. ext4_debug("ext4_end_io_nolock: io_end 0x%p from inode %lu,list->next 0x%p,"
  160. "list->prev 0x%p\n",
  161. io_end, inode->i_ino, io_end->list.next, io_end->list.prev);
  162. /*
  163. * Do not convert the unwritten extents if data writeback fails,
  164. * or stale data may be exposed.
  165. */
  166. io_end->handle = NULL; /* Following call will use up the handle */
  167. if (unlikely(io_end->flag & EXT4_IO_END_FAILED)) {
  168. ret = -EIO;
  169. if (handle)
  170. jbd2_journal_free_reserved(handle);
  171. } else {
  172. ret = ext4_convert_unwritten_io_end_vec(handle, io_end);
  173. }
  174. if (ret < 0 && !ext4_forced_shutdown(inode->i_sb)) {
  175. ext4_msg(inode->i_sb, KERN_EMERG,
  176. "failed to convert unwritten extents to written "
  177. "extents -- potential data loss! "
  178. "(inode %lu, error %d)", inode->i_ino, ret);
  179. }
  180. ext4_clear_io_unwritten_flag(io_end);
  181. ext4_release_io_end(io_end);
  182. return ret;
  183. }
  184. static void dump_completed_IO(struct inode *inode, struct list_head *head)
  185. {
  186. #ifdef EXT4FS_DEBUG
  187. struct list_head *cur, *before, *after;
  188. ext4_io_end_t *io_end, *io_end0, *io_end1;
  189. if (list_empty(head))
  190. return;
  191. ext4_debug("Dump inode %lu completed io list\n", inode->i_ino);
  192. list_for_each_entry(io_end, head, list) {
  193. cur = &io_end->list;
  194. before = cur->prev;
  195. io_end0 = container_of(before, ext4_io_end_t, list);
  196. after = cur->next;
  197. io_end1 = container_of(after, ext4_io_end_t, list);
  198. ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
  199. io_end, inode->i_ino, io_end0, io_end1);
  200. }
  201. #endif
  202. }
  203. /* Add the io_end to per-inode completed end_io list. */
  204. static void ext4_add_complete_io(ext4_io_end_t *io_end)
  205. {
  206. struct ext4_inode_info *ei = EXT4_I(io_end->inode);
  207. struct ext4_sb_info *sbi = EXT4_SB(io_end->inode->i_sb);
  208. struct workqueue_struct *wq;
  209. unsigned long flags;
  210. /* Only reserved conversions from writeback should enter here */
  211. WARN_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
  212. WARN_ON(!io_end->handle && sbi->s_journal);
  213. spin_lock_irqsave(&ei->i_completed_io_lock, flags);
  214. wq = sbi->rsv_conversion_wq;
  215. if (list_empty(&ei->i_rsv_conversion_list))
  216. queue_work(wq, &ei->i_rsv_conversion_work);
  217. list_add_tail(&io_end->list, &ei->i_rsv_conversion_list);
  218. spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
  219. }
  220. static int ext4_do_flush_completed_IO(struct inode *inode,
  221. struct list_head *head)
  222. {
  223. ext4_io_end_t *io_end;
  224. struct list_head unwritten;
  225. unsigned long flags;
  226. struct ext4_inode_info *ei = EXT4_I(inode);
  227. int err, ret = 0;
  228. spin_lock_irqsave(&ei->i_completed_io_lock, flags);
  229. dump_completed_IO(inode, head);
  230. list_replace_init(head, &unwritten);
  231. spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
  232. while (!list_empty(&unwritten)) {
  233. io_end = list_entry(unwritten.next, ext4_io_end_t, list);
  234. BUG_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
  235. list_del_init(&io_end->list);
  236. err = ext4_end_io_end(io_end);
  237. if (unlikely(!ret && err))
  238. ret = err;
  239. }
  240. return ret;
  241. }
  242. /*
  243. * work on completed IO, to convert unwritten extents to extents
  244. */
  245. void ext4_end_io_rsv_work(struct work_struct *work)
  246. {
  247. struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
  248. i_rsv_conversion_work);
  249. ext4_do_flush_completed_IO(&ei->vfs_inode, &ei->i_rsv_conversion_list);
  250. }
  251. ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
  252. {
  253. ext4_io_end_t *io_end = kmem_cache_zalloc(io_end_cachep, flags);
  254. if (io_end) {
  255. io_end->inode = inode;
  256. INIT_LIST_HEAD(&io_end->list);
  257. INIT_LIST_HEAD(&io_end->list_vec);
  258. refcount_set(&io_end->count, 1);
  259. }
  260. return io_end;
  261. }
  262. void ext4_put_io_end_defer(ext4_io_end_t *io_end)
  263. {
  264. if (refcount_dec_and_test(&io_end->count)) {
  265. if (!(io_end->flag & EXT4_IO_END_UNWRITTEN) ||
  266. list_empty(&io_end->list_vec)) {
  267. ext4_release_io_end(io_end);
  268. return;
  269. }
  270. ext4_add_complete_io(io_end);
  271. }
  272. }
  273. int ext4_put_io_end(ext4_io_end_t *io_end)
  274. {
  275. int err = 0;
  276. if (refcount_dec_and_test(&io_end->count)) {
  277. if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
  278. err = ext4_convert_unwritten_io_end_vec(io_end->handle,
  279. io_end);
  280. io_end->handle = NULL;
  281. ext4_clear_io_unwritten_flag(io_end);
  282. }
  283. ext4_release_io_end(io_end);
  284. }
  285. return err;
  286. }
  287. ext4_io_end_t *ext4_get_io_end(ext4_io_end_t *io_end)
  288. {
  289. refcount_inc(&io_end->count);
  290. return io_end;
  291. }
  292. /* BIO completion function for page writeback */
  293. static void ext4_end_bio(struct bio *bio)
  294. {
  295. ext4_io_end_t *io_end = bio->bi_private;
  296. sector_t bi_sector = bio->bi_iter.bi_sector;
  297. if (WARN_ONCE(!io_end, "io_end is NULL: %pg: sector %Lu len %u err %d\n",
  298. bio->bi_bdev,
  299. (long long) bio->bi_iter.bi_sector,
  300. (unsigned) bio_sectors(bio),
  301. bio->bi_status)) {
  302. ext4_finish_bio(bio);
  303. bio_put(bio);
  304. return;
  305. }
  306. bio->bi_end_io = NULL;
  307. if (bio->bi_status) {
  308. struct inode *inode = io_end->inode;
  309. ext4_warning(inode->i_sb, "I/O error %d writing to inode %lu "
  310. "starting block %llu)",
  311. bio->bi_status, inode->i_ino,
  312. (unsigned long long)
  313. bi_sector >> (inode->i_blkbits - 9));
  314. io_end->flag |= EXT4_IO_END_FAILED;
  315. mapping_set_error(inode->i_mapping,
  316. blk_status_to_errno(bio->bi_status));
  317. }
  318. if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
  319. /*
  320. * Link bio into list hanging from io_end. We have to do it
  321. * atomically as bio completions can be racing against each
  322. * other.
  323. */
  324. bio->bi_private = xchg(&io_end->bio, bio);
  325. ext4_put_io_end_defer(io_end);
  326. } else {
  327. /*
  328. * Drop io_end reference early. Inode can get freed once
  329. * we finish the bio.
  330. */
  331. ext4_put_io_end_defer(io_end);
  332. ext4_finish_bio(bio);
  333. bio_put(bio);
  334. }
  335. }
  336. void ext4_io_submit(struct ext4_io_submit *io)
  337. {
  338. struct bio *bio = io->io_bio;
  339. if (bio) {
  340. if (io->io_wbc->sync_mode == WB_SYNC_ALL)
  341. io->io_bio->bi_opf |= REQ_SYNC;
  342. submit_bio(io->io_bio);
  343. }
  344. io->io_bio = NULL;
  345. }
  346. void ext4_io_submit_init(struct ext4_io_submit *io,
  347. struct writeback_control *wbc)
  348. {
  349. io->io_wbc = wbc;
  350. io->io_bio = NULL;
  351. io->io_end = NULL;
  352. }
  353. static void io_submit_init_bio(struct ext4_io_submit *io,
  354. struct buffer_head *bh)
  355. {
  356. struct bio *bio;
  357. /*
  358. * bio_alloc will _always_ be able to allocate a bio if
  359. * __GFP_DIRECT_RECLAIM is set, see comments for bio_alloc_bioset().
  360. */
  361. bio = bio_alloc(bh->b_bdev, BIO_MAX_VECS, REQ_OP_WRITE, GFP_NOIO);
  362. fscrypt_set_bio_crypt_ctx_bh(bio, bh, GFP_NOIO);
  363. bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  364. bio->bi_end_io = ext4_end_bio;
  365. bio->bi_private = ext4_get_io_end(io->io_end);
  366. io->io_bio = bio;
  367. io->io_next_block = bh->b_blocknr;
  368. wbc_init_bio(io->io_wbc, bio);
  369. }
  370. static void io_submit_add_bh(struct ext4_io_submit *io,
  371. struct inode *inode,
  372. struct folio *folio,
  373. struct folio *io_folio,
  374. struct buffer_head *bh)
  375. {
  376. if (io->io_bio && (bh->b_blocknr != io->io_next_block ||
  377. !fscrypt_mergeable_bio_bh(io->io_bio, bh))) {
  378. submit_and_retry:
  379. ext4_io_submit(io);
  380. }
  381. if (io->io_bio == NULL)
  382. io_submit_init_bio(io, bh);
  383. if (!bio_add_folio(io->io_bio, io_folio, bh->b_size, bh_offset(bh)))
  384. goto submit_and_retry;
  385. wbc_account_cgroup_owner(io->io_wbc, folio, bh->b_size);
  386. io->io_next_block++;
  387. }
  388. int ext4_bio_write_folio(struct ext4_io_submit *io, struct folio *folio,
  389. size_t len)
  390. {
  391. struct folio *io_folio = folio;
  392. struct inode *inode = folio->mapping->host;
  393. unsigned block_start;
  394. struct buffer_head *bh, *head;
  395. int ret = 0;
  396. int nr_to_submit = 0;
  397. struct writeback_control *wbc = io->io_wbc;
  398. bool keep_towrite = false;
  399. BUG_ON(!folio_test_locked(folio));
  400. BUG_ON(folio_test_writeback(folio));
  401. /*
  402. * Comments copied from block_write_full_folio:
  403. *
  404. * The folio straddles i_size. It must be zeroed out on each and every
  405. * writepage invocation because it may be mmapped. "A file is mapped
  406. * in multiples of the page size. For a file that is not a multiple of
  407. * the page size, the remaining memory is zeroed when mapped, and
  408. * writes to that region are not written out to the file."
  409. */
  410. if (len < folio_size(folio))
  411. folio_zero_segment(folio, len, folio_size(folio));
  412. /*
  413. * In the first loop we prepare and mark buffers to submit. We have to
  414. * mark all buffers in the folio before submitting so that
  415. * folio_end_writeback() cannot be called from ext4_end_bio() when IO
  416. * on the first buffer finishes and we are still working on submitting
  417. * the second buffer.
  418. */
  419. bh = head = folio_buffers(folio);
  420. do {
  421. block_start = bh_offset(bh);
  422. if (block_start >= len) {
  423. clear_buffer_dirty(bh);
  424. set_buffer_uptodate(bh);
  425. continue;
  426. }
  427. if (!buffer_dirty(bh) || buffer_delay(bh) ||
  428. !buffer_mapped(bh) || buffer_unwritten(bh)) {
  429. /* A hole? We can safely clear the dirty bit */
  430. if (!buffer_mapped(bh))
  431. clear_buffer_dirty(bh);
  432. /*
  433. * Keeping dirty some buffer we cannot write? Make sure
  434. * to redirty the folio and keep TOWRITE tag so that
  435. * racing WB_SYNC_ALL writeback does not skip the folio.
  436. * This happens e.g. when doing writeout for
  437. * transaction commit or when journalled data is not
  438. * yet committed.
  439. */
  440. if (buffer_dirty(bh) ||
  441. (buffer_jbd(bh) && buffer_jbddirty(bh))) {
  442. if (!folio_test_dirty(folio))
  443. folio_redirty_for_writepage(wbc, folio);
  444. keep_towrite = true;
  445. }
  446. continue;
  447. }
  448. if (buffer_new(bh))
  449. clear_buffer_new(bh);
  450. set_buffer_async_write(bh);
  451. clear_buffer_dirty(bh);
  452. nr_to_submit++;
  453. } while ((bh = bh->b_this_page) != head);
  454. /* Nothing to submit? Just unlock the folio... */
  455. if (!nr_to_submit)
  456. return 0;
  457. bh = head = folio_buffers(folio);
  458. /*
  459. * If any blocks are being written to an encrypted file, encrypt them
  460. * into a bounce page. For simplicity, just encrypt until the last
  461. * block which might be needed. This may cause some unneeded blocks
  462. * (e.g. holes) to be unnecessarily encrypted, but this is rare and
  463. * can't happen in the common case of blocksize == PAGE_SIZE.
  464. */
  465. if (fscrypt_inode_uses_fs_layer_crypto(inode)) {
  466. gfp_t gfp_flags = GFP_NOFS;
  467. unsigned int enc_bytes = round_up(len, i_blocksize(inode));
  468. struct page *bounce_page;
  469. /*
  470. * Since bounce page allocation uses a mempool, we can only use
  471. * a waiting mask (i.e. request guaranteed allocation) on the
  472. * first page of the bio. Otherwise it can deadlock.
  473. */
  474. if (io->io_bio)
  475. gfp_flags = GFP_NOWAIT | __GFP_NOWARN;
  476. retry_encrypt:
  477. bounce_page = fscrypt_encrypt_pagecache_blocks(&folio->page,
  478. enc_bytes, 0, gfp_flags);
  479. if (IS_ERR(bounce_page)) {
  480. ret = PTR_ERR(bounce_page);
  481. if (ret == -ENOMEM &&
  482. (io->io_bio || wbc->sync_mode == WB_SYNC_ALL)) {
  483. gfp_t new_gfp_flags = GFP_NOFS;
  484. if (io->io_bio)
  485. ext4_io_submit(io);
  486. else
  487. new_gfp_flags |= __GFP_NOFAIL;
  488. memalloc_retry_wait(gfp_flags);
  489. gfp_flags = new_gfp_flags;
  490. goto retry_encrypt;
  491. }
  492. printk_ratelimited(KERN_ERR "%s: ret = %d\n", __func__, ret);
  493. folio_redirty_for_writepage(wbc, folio);
  494. do {
  495. if (buffer_async_write(bh)) {
  496. clear_buffer_async_write(bh);
  497. set_buffer_dirty(bh);
  498. }
  499. bh = bh->b_this_page;
  500. } while (bh != head);
  501. return ret;
  502. }
  503. io_folio = page_folio(bounce_page);
  504. }
  505. __folio_start_writeback(folio, keep_towrite);
  506. /* Now submit buffers to write */
  507. do {
  508. if (!buffer_async_write(bh))
  509. continue;
  510. io_submit_add_bh(io, inode, folio, io_folio, bh);
  511. } while ((bh = bh->b_this_page) != head);
  512. return 0;
  513. }