page-io.c 14 KB

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