page-io.c 15 KB

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