inode.c (de0039f69c95692539bcb7cfc3b01f31e1c9f342) | inode.c (15648d599cd1c15cc678039dcab65599276fe407) |
---|---|
1// SPDX-License-Identifier: GPL-2.0 2/* 3 * linux/fs/ext4/inode.c 4 * 5 * Copyright (C) 1992, 1993, 1994, 1995 6 * Remy Card (card@masi.ibp.fr) 7 * Laboratoire MASI - Institut Blaise Pascal 8 * Universite Pierre et Marie Curie (Paris VI) --- 1536 unchanged lines hidden (view full) --- 1545 dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1546} 1547 1548/* 1549 * Delayed allocation stuff 1550 */ 1551 1552struct mpage_da_data { | 1// SPDX-License-Identifier: GPL-2.0 2/* 3 * linux/fs/ext4/inode.c 4 * 5 * Copyright (C) 1992, 1993, 1994, 1995 6 * Remy Card (card@masi.ibp.fr) 7 * Laboratoire MASI - Institut Blaise Pascal 8 * Universite Pierre et Marie Curie (Paris VI) --- 1536 unchanged lines hidden (view full) --- 1545 dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1546} 1547 1548/* 1549 * Delayed allocation stuff 1550 */ 1551 1552struct mpage_da_data { |
1553 /* These are input fields for ext4_do_writepages() */ |
|
1553 struct inode *inode; 1554 struct writeback_control *wbc; | 1554 struct inode *inode; 1555 struct writeback_control *wbc; |
1556 unsigned int can_map:1; /* Can writepages call map blocks? */ |
|
1555 | 1557 |
1558 /* These are internal state of ext4_do_writepages() */ |
|
1556 pgoff_t first_page; /* The first page to write */ 1557 pgoff_t next_page; /* Current page to examine */ 1558 pgoff_t last_page; /* Last page to examine */ 1559 /* 1560 * Extent to map - this can be after first_page because that can be 1561 * fully mapped. We somewhat abuse m_flags to store whether the extent 1562 * is delalloc or unwritten. 1563 */ 1564 struct ext4_map_blocks map; 1565 struct ext4_io_submit io_submit; /* IO submission data */ 1566 unsigned int do_map:1; | 1559 pgoff_t first_page; /* The first page to write */ 1560 pgoff_t next_page; /* Current page to examine */ 1561 pgoff_t last_page; /* Last page to examine */ 1562 /* 1563 * Extent to map - this can be after first_page because that can be 1564 * fully mapped. We somewhat abuse m_flags to store whether the extent 1565 * is delalloc or unwritten. 1566 */ 1567 struct ext4_map_blocks map; 1568 struct ext4_io_submit io_submit; /* IO submission data */ 1569 unsigned int do_map:1; |
1567 unsigned int can_map:1; /* Can writepages call map blocks? */ | |
1568 unsigned int scanned_until_end:1; 1569}; 1570 1571static void mpage_release_unused_pages(struct mpage_da_data *mpd, 1572 bool invalidate) 1573{ 1574 unsigned nr, i; 1575 pgoff_t index, end; --- 1129 unchanged lines hidden (view full) --- 2705 } 2706 mpd->scanned_until_end = 1; 2707 return 0; 2708out: 2709 pagevec_release(&pvec); 2710 return err; 2711} 2712 | 1570 unsigned int scanned_until_end:1; 1571}; 1572 1573static void mpage_release_unused_pages(struct mpage_da_data *mpd, 1574 bool invalidate) 1575{ 1576 unsigned nr, i; 1577 pgoff_t index, end; --- 1129 unchanged lines hidden (view full) --- 2707 } 2708 mpd->scanned_until_end = 1; 2709 return 0; 2710out: 2711 pagevec_release(&pvec); 2712 return err; 2713} 2714 |
2713static int ext4_writepages(struct address_space *mapping, 2714 struct writeback_control *wbc) | 2715static int ext4_do_writepages(struct mpage_da_data *mpd) |
2715{ | 2716{ |
2717 struct writeback_control *wbc = mpd->wbc; |
|
2716 pgoff_t writeback_index = 0; 2717 long nr_to_write = wbc->nr_to_write; 2718 int range_whole = 0; 2719 int cycled = 1; 2720 handle_t *handle = NULL; | 2718 pgoff_t writeback_index = 0; 2719 long nr_to_write = wbc->nr_to_write; 2720 int range_whole = 0; 2721 int cycled = 1; 2722 handle_t *handle = NULL; |
2721 struct mpage_da_data mpd; 2722 struct inode *inode = mapping->host; | 2723 struct inode *inode = mpd->inode; 2724 struct address_space *mapping = inode->i_mapping; |
2723 int needed_blocks, rsv_blocks = 0, ret = 0; 2724 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 2725 struct blk_plug plug; 2726 bool give_up_on_write = false; 2727 2728 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 2729 return -EIO; 2730 --- 58 unchanged lines hidden (view full) --- 2789 2790 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 2791 range_whole = 1; 2792 2793 if (wbc->range_cyclic) { 2794 writeback_index = mapping->writeback_index; 2795 if (writeback_index) 2796 cycled = 0; | 2725 int needed_blocks, rsv_blocks = 0, ret = 0; 2726 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 2727 struct blk_plug plug; 2728 bool give_up_on_write = false; 2729 2730 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 2731 return -EIO; 2732 --- 58 unchanged lines hidden (view full) --- 2791 2792 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 2793 range_whole = 1; 2794 2795 if (wbc->range_cyclic) { 2796 writeback_index = mapping->writeback_index; 2797 if (writeback_index) 2798 cycled = 0; |
2797 mpd.first_page = writeback_index; 2798 mpd.last_page = -1; | 2799 mpd->first_page = writeback_index; 2800 mpd->last_page = -1; |
2799 } else { | 2801 } else { |
2800 mpd.first_page = wbc->range_start >> PAGE_SHIFT; 2801 mpd.last_page = wbc->range_end >> PAGE_SHIFT; | 2802 mpd->first_page = wbc->range_start >> PAGE_SHIFT; 2803 mpd->last_page = wbc->range_end >> PAGE_SHIFT; |
2802 } 2803 | 2804 } 2805 |
2804 mpd.inode = inode; 2805 mpd.wbc = wbc; 2806 ext4_io_submit_init(&mpd.io_submit, wbc); | 2806 ext4_io_submit_init(&mpd->io_submit, wbc); |
2807retry: 2808 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) | 2807retry: 2808 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
2809 tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page); | 2809 tag_pages_for_writeback(mapping, mpd->first_page, 2810 mpd->last_page); |
2810 blk_start_plug(&plug); 2811 2812 /* 2813 * First writeback pages that don't need mapping - we can avoid 2814 * starting a transaction unnecessarily and also avoid being blocked 2815 * in the block layer on device congestion while having transaction 2816 * started. 2817 */ | 2811 blk_start_plug(&plug); 2812 2813 /* 2814 * First writeback pages that don't need mapping - we can avoid 2815 * starting a transaction unnecessarily and also avoid being blocked 2816 * in the block layer on device congestion while having transaction 2817 * started. 2818 */ |
2818 mpd.do_map = 0; 2819 mpd.scanned_until_end = 0; 2820 mpd.can_map = 1; 2821 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 2822 if (!mpd.io_submit.io_end) { | 2819 mpd->do_map = 0; 2820 mpd->scanned_until_end = 0; 2821 mpd->io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 2822 if (!mpd->io_submit.io_end) { |
2823 ret = -ENOMEM; 2824 goto unplug; 2825 } | 2823 ret = -ENOMEM; 2824 goto unplug; 2825 } |
2826 ret = mpage_prepare_extent_to_map(&mpd); | 2826 ret = mpage_prepare_extent_to_map(mpd); |
2827 /* Unlock pages we didn't use */ | 2827 /* Unlock pages we didn't use */ |
2828 mpage_release_unused_pages(&mpd, false); | 2828 mpage_release_unused_pages(mpd, false); |
2829 /* Submit prepared bio */ | 2829 /* Submit prepared bio */ |
2830 ext4_io_submit(&mpd.io_submit); 2831 ext4_put_io_end_defer(mpd.io_submit.io_end); 2832 mpd.io_submit.io_end = NULL; | 2830 ext4_io_submit(&mpd->io_submit); 2831 ext4_put_io_end_defer(mpd->io_submit.io_end); 2832 mpd->io_submit.io_end = NULL; |
2833 if (ret < 0) 2834 goto unplug; 2835 | 2833 if (ret < 0) 2834 goto unplug; 2835 |
2836 while (!mpd.scanned_until_end && wbc->nr_to_write > 0) { | 2836 while (!mpd->scanned_until_end && wbc->nr_to_write > 0) { |
2837 /* For each extent of pages we use new io_end */ | 2837 /* For each extent of pages we use new io_end */ |
2838 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 2839 if (!mpd.io_submit.io_end) { | 2838 mpd->io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL); 2839 if (!mpd->io_submit.io_end) { |
2840 ret = -ENOMEM; 2841 break; 2842 } 2843 2844 WARN_ON_ONCE(!mpd->can_map); 2845 /* 2846 * We have two constraints: We find one extent to map and we 2847 * must always write out whole page (makes a difference when --- 8 unchanged lines hidden (view full) --- 2856 handle = ext4_journal_start_with_reserve(inode, 2857 EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks); 2858 if (IS_ERR(handle)) { 2859 ret = PTR_ERR(handle); 2860 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2861 "%ld pages, ino %lu; err %d", __func__, 2862 wbc->nr_to_write, inode->i_ino, ret); 2863 /* Release allocated io_end */ | 2840 ret = -ENOMEM; 2841 break; 2842 } 2843 2844 WARN_ON_ONCE(!mpd->can_map); 2845 /* 2846 * We have two constraints: We find one extent to map and we 2847 * must always write out whole page (makes a difference when --- 8 unchanged lines hidden (view full) --- 2856 handle = ext4_journal_start_with_reserve(inode, 2857 EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks); 2858 if (IS_ERR(handle)) { 2859 ret = PTR_ERR(handle); 2860 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2861 "%ld pages, ino %lu; err %d", __func__, 2862 wbc->nr_to_write, inode->i_ino, ret); 2863 /* Release allocated io_end */ |
2864 ext4_put_io_end(mpd.io_submit.io_end); 2865 mpd.io_submit.io_end = NULL; | 2864 ext4_put_io_end(mpd->io_submit.io_end); 2865 mpd->io_submit.io_end = NULL; |
2866 break; 2867 } | 2866 break; 2867 } |
2868 mpd.do_map = 1; | 2868 mpd->do_map = 1; |
2869 | 2869 |
2870 trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc); 2871 ret = mpage_prepare_extent_to_map(&mpd); 2872 if (!ret && mpd.map.m_len) 2873 ret = mpage_map_and_submit_extent(handle, &mpd, | 2870 trace_ext4_da_write_pages(inode, mpd->first_page, wbc); 2871 ret = mpage_prepare_extent_to_map(mpd); 2872 if (!ret && mpd->map.m_len) 2873 ret = mpage_map_and_submit_extent(handle, mpd, |
2874 &give_up_on_write); 2875 /* 2876 * Caution: If the handle is synchronous, 2877 * ext4_journal_stop() can wait for transaction commit 2878 * to finish which may depend on writeback of pages to 2879 * complete or on page lock to be released. In that 2880 * case, we have to wait until after we have 2881 * submitted all the IO, released page locks we hold, 2882 * and dropped io_end reference (for extent conversion 2883 * to be able to complete) before stopping the handle. 2884 */ 2885 if (!ext4_handle_valid(handle) || handle->h_sync == 0) { 2886 ext4_journal_stop(handle); 2887 handle = NULL; | 2874 &give_up_on_write); 2875 /* 2876 * Caution: If the handle is synchronous, 2877 * ext4_journal_stop() can wait for transaction commit 2878 * to finish which may depend on writeback of pages to 2879 * complete or on page lock to be released. In that 2880 * case, we have to wait until after we have 2881 * submitted all the IO, released page locks we hold, 2882 * and dropped io_end reference (for extent conversion 2883 * to be able to complete) before stopping the handle. 2884 */ 2885 if (!ext4_handle_valid(handle) || handle->h_sync == 0) { 2886 ext4_journal_stop(handle); 2887 handle = NULL; |
2888 mpd.do_map = 0; | 2888 mpd->do_map = 0; |
2889 } 2890 /* Unlock pages we didn't use */ | 2889 } 2890 /* Unlock pages we didn't use */ |
2891 mpage_release_unused_pages(&mpd, give_up_on_write); | 2891 mpage_release_unused_pages(mpd, give_up_on_write); |
2892 /* Submit prepared bio */ | 2892 /* Submit prepared bio */ |
2893 ext4_io_submit(&mpd.io_submit); | 2893 ext4_io_submit(&mpd->io_submit); |
2894 2895 /* 2896 * Drop our io_end reference we got from init. We have 2897 * to be careful and use deferred io_end finishing if 2898 * we are still holding the transaction as we can 2899 * release the last reference to io_end which may end 2900 * up doing unwritten extent conversion. 2901 */ 2902 if (handle) { | 2894 2895 /* 2896 * Drop our io_end reference we got from init. We have 2897 * to be careful and use deferred io_end finishing if 2898 * we are still holding the transaction as we can 2899 * release the last reference to io_end which may end 2900 * up doing unwritten extent conversion. 2901 */ 2902 if (handle) { |
2903 ext4_put_io_end_defer(mpd.io_submit.io_end); | 2903 ext4_put_io_end_defer(mpd->io_submit.io_end); |
2904 ext4_journal_stop(handle); 2905 } else | 2904 ext4_journal_stop(handle); 2905 } else |
2906 ext4_put_io_end(mpd.io_submit.io_end); 2907 mpd.io_submit.io_end = NULL; | 2906 ext4_put_io_end(mpd->io_submit.io_end); 2907 mpd->io_submit.io_end = NULL; |
2908 2909 if (ret == -ENOSPC && sbi->s_journal) { 2910 /* 2911 * Commit the transaction which would 2912 * free blocks released in the transaction 2913 * and try again 2914 */ 2915 jbd2_journal_force_commit_nested(sbi->s_journal); 2916 ret = 0; 2917 continue; 2918 } 2919 /* Fatal error - ENOMEM, EIO... */ 2920 if (ret) 2921 break; 2922 } 2923unplug: 2924 blk_finish_plug(&plug); 2925 if (!ret && !cycled && wbc->nr_to_write > 0) { 2926 cycled = 1; | 2908 2909 if (ret == -ENOSPC && sbi->s_journal) { 2910 /* 2911 * Commit the transaction which would 2912 * free blocks released in the transaction 2913 * and try again 2914 */ 2915 jbd2_journal_force_commit_nested(sbi->s_journal); 2916 ret = 0; 2917 continue; 2918 } 2919 /* Fatal error - ENOMEM, EIO... */ 2920 if (ret) 2921 break; 2922 } 2923unplug: 2924 blk_finish_plug(&plug); 2925 if (!ret && !cycled && wbc->nr_to_write > 0) { 2926 cycled = 1; |
2927 mpd.last_page = writeback_index - 1; 2928 mpd.first_page = 0; | 2927 mpd->last_page = writeback_index - 1; 2928 mpd->first_page = 0; |
2929 goto retry; 2930 } 2931 2932 /* Update index */ 2933 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 2934 /* 2935 * Set the writeback_index so that range_cyclic 2936 * mode will write it back later 2937 */ | 2929 goto retry; 2930 } 2931 2932 /* Update index */ 2933 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 2934 /* 2935 * Set the writeback_index so that range_cyclic 2936 * mode will write it back later 2937 */ |
2938 mapping->writeback_index = mpd.first_page; | 2938 mapping->writeback_index = mpd->first_page; |
2939 2940out_writepages: 2941 trace_ext4_writepages_result(inode, wbc, ret, 2942 nr_to_write - wbc->nr_to_write); 2943 percpu_up_read(&sbi->s_writepages_rwsem); 2944 return ret; 2945} 2946 | 2939 2940out_writepages: 2941 trace_ext4_writepages_result(inode, wbc, ret, 2942 nr_to_write - wbc->nr_to_write); 2943 percpu_up_read(&sbi->s_writepages_rwsem); 2944 return ret; 2945} 2946 |
2947static int ext4_writepages(struct address_space *mapping, 2948 struct writeback_control *wbc) 2949{ 2950 struct mpage_da_data mpd = { 2951 .inode = mapping->host, 2952 .wbc = wbc, 2953 .can_map = 1, 2954 }; 2955 2956 return ext4_do_writepages(&mpd); 2957} 2958 |
|
2947static int ext4_dax_writepages(struct address_space *mapping, 2948 struct writeback_control *wbc) 2949{ 2950 int ret; 2951 long nr_to_write = wbc->nr_to_write; 2952 struct inode *inode = mapping->host; 2953 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 2954 --- 3352 unchanged lines hidden --- | 2959static int ext4_dax_writepages(struct address_space *mapping, 2960 struct writeback_control *wbc) 2961{ 2962 int ret; 2963 long nr_to_write = wbc->nr_to_write; 2964 struct inode *inode = mapping->host; 2965 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 2966 --- 3352 unchanged lines hidden --- |