diff options
Diffstat (limited to 'fs/ext4/extents.c')
| -rw-r--r-- | fs/ext4/extents.c | 444 | 
1 files changed, 406 insertions, 38 deletions
| diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index 7a3832577923..10539e364283 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c | |||
| @@ -723,7 +723,7 @@ err: | |||
| 723 | * insert new index [@logical;@ptr] into the block at @curp; | 723 | * insert new index [@logical;@ptr] into the block at @curp; | 
| 724 | * check where to insert: before @curp or after @curp | 724 | * check where to insert: before @curp or after @curp | 
| 725 | */ | 725 | */ | 
| 726 | static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, | 726 | int ext4_ext_insert_index(handle_t *handle, struct inode *inode, | 
| 727 | struct ext4_ext_path *curp, | 727 | struct ext4_ext_path *curp, | 
| 728 | int logical, ext4_fsblk_t ptr) | 728 | int logical, ext4_fsblk_t ptr) | 
| 729 | { | 729 | { | 
| @@ -1586,7 +1586,7 @@ out: | |||
| 1586 | */ | 1586 | */ | 
| 1587 | int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, | 1587 | int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, | 
| 1588 | struct ext4_ext_path *path, | 1588 | struct ext4_ext_path *path, | 
| 1589 | struct ext4_extent *newext) | 1589 | struct ext4_extent *newext, int flag) | 
| 1590 | { | 1590 | { | 
| 1591 | struct ext4_extent_header *eh; | 1591 | struct ext4_extent_header *eh; | 
| 1592 | struct ext4_extent *ex, *fex; | 1592 | struct ext4_extent *ex, *fex; | 
| @@ -1602,7 +1602,8 @@ int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, | |||
| 1602 | BUG_ON(path[depth].p_hdr == NULL); | 1602 | BUG_ON(path[depth].p_hdr == NULL); | 
| 1603 | 1603 | ||
| 1604 | /* try to insert block into found extent and return */ | 1604 | /* try to insert block into found extent and return */ | 
| 1605 | if (ex && ext4_can_extents_be_merged(inode, ex, newext)) { | 1605 | if (ex && (flag != EXT4_GET_BLOCKS_DIO_CREATE_EXT) | 
| 1606 | && ext4_can_extents_be_merged(inode, ex, newext)) { | ||
| 1606 | ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n", | 1607 | ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n", | 
| 1607 | ext4_ext_is_uninitialized(newext), | 1608 | ext4_ext_is_uninitialized(newext), | 
| 1608 | ext4_ext_get_actual_len(newext), | 1609 | ext4_ext_get_actual_len(newext), | 
| @@ -1722,7 +1723,8 @@ has_space: | |||
| 1722 | 1723 | ||
| 1723 | merge: | 1724 | merge: | 
| 1724 | /* try to merge extents to the right */ | 1725 | /* try to merge extents to the right */ | 
| 1725 | ext4_ext_try_to_merge(inode, path, nearex); | 1726 | if (flag != EXT4_GET_BLOCKS_DIO_CREATE_EXT) | 
| 1727 | ext4_ext_try_to_merge(inode, path, nearex); | ||
| 1726 | 1728 | ||
| 1727 | /* try to merge extents to the left */ | 1729 | /* try to merge extents to the left */ | 
| 1728 | 1730 | ||
| @@ -2378,6 +2380,7 @@ void ext4_ext_init(struct super_block *sb) | |||
| 2378 | */ | 2380 | */ | 
| 2379 | 2381 | ||
| 2380 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { | 2382 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { | 
| 2383 | #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS) | ||
| 2381 | printk(KERN_INFO "EXT4-fs: file extents enabled"); | 2384 | printk(KERN_INFO "EXT4-fs: file extents enabled"); | 
| 2382 | #ifdef AGGRESSIVE_TEST | 2385 | #ifdef AGGRESSIVE_TEST | 
| 2383 | printk(", aggressive tests"); | 2386 | printk(", aggressive tests"); | 
| @@ -2389,6 +2392,7 @@ void ext4_ext_init(struct super_block *sb) | |||
| 2389 | printk(", stats"); | 2392 | printk(", stats"); | 
| 2390 | #endif | 2393 | #endif | 
| 2391 | printk("\n"); | 2394 | printk("\n"); | 
| 2395 | #endif | ||
| 2392 | #ifdef EXTENTS_STATS | 2396 | #ifdef EXTENTS_STATS | 
| 2393 | spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); | 2397 | spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); | 
| 2394 | EXT4_SB(sb)->s_ext_min = 1 << 30; | 2398 | EXT4_SB(sb)->s_ext_min = 1 << 30; | 
| @@ -2490,7 +2494,6 @@ static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) | |||
| 2490 | } | 2494 | } | 
| 2491 | 2495 | ||
| 2492 | #define EXT4_EXT_ZERO_LEN 7 | 2496 | #define EXT4_EXT_ZERO_LEN 7 | 
| 2493 | |||
| 2494 | /* | 2497 | /* | 
| 2495 | * This function is called by ext4_ext_get_blocks() if someone tries to write | 2498 | * This function is called by ext4_ext_get_blocks() if someone tries to write | 
| 2496 | * to an uninitialized extent. It may result in splitting the uninitialized | 2499 | * to an uninitialized extent. It may result in splitting the uninitialized | 
| @@ -2583,7 +2586,8 @@ static int ext4_ext_convert_to_initialized(handle_t *handle, | |||
| 2583 | ex3->ee_block = cpu_to_le32(iblock); | 2586 | ex3->ee_block = cpu_to_le32(iblock); | 
| 2584 | ext4_ext_store_pblock(ex3, newblock); | 2587 | ext4_ext_store_pblock(ex3, newblock); | 
| 2585 | ex3->ee_len = cpu_to_le16(allocated); | 2588 | ex3->ee_len = cpu_to_le16(allocated); | 
| 2586 | err = ext4_ext_insert_extent(handle, inode, path, ex3); | 2589 | err = ext4_ext_insert_extent(handle, inode, path, | 
| 2590 | ex3, 0); | ||
| 2587 | if (err == -ENOSPC) { | 2591 | if (err == -ENOSPC) { | 
| 2588 | err = ext4_ext_zeroout(inode, &orig_ex); | 2592 | err = ext4_ext_zeroout(inode, &orig_ex); | 
| 2589 | if (err) | 2593 | if (err) | 
| @@ -2639,7 +2643,7 @@ static int ext4_ext_convert_to_initialized(handle_t *handle, | |||
| 2639 | ext4_ext_store_pblock(ex3, newblock + max_blocks); | 2643 | ext4_ext_store_pblock(ex3, newblock + max_blocks); | 
| 2640 | ex3->ee_len = cpu_to_le16(allocated - max_blocks); | 2644 | ex3->ee_len = cpu_to_le16(allocated - max_blocks); | 
| 2641 | ext4_ext_mark_uninitialized(ex3); | 2645 | ext4_ext_mark_uninitialized(ex3); | 
| 2642 | err = ext4_ext_insert_extent(handle, inode, path, ex3); | 2646 | err = ext4_ext_insert_extent(handle, inode, path, ex3, 0); | 
| 2643 | if (err == -ENOSPC) { | 2647 | if (err == -ENOSPC) { | 
| 2644 | err = ext4_ext_zeroout(inode, &orig_ex); | 2648 | err = ext4_ext_zeroout(inode, &orig_ex); | 
| 2645 | if (err) | 2649 | if (err) | 
| @@ -2757,7 +2761,7 @@ static int ext4_ext_convert_to_initialized(handle_t *handle, | |||
| 2757 | err = ext4_ext_dirty(handle, inode, path + depth); | 2761 | err = ext4_ext_dirty(handle, inode, path + depth); | 
| 2758 | goto out; | 2762 | goto out; | 
| 2759 | insert: | 2763 | insert: | 
| 2760 | err = ext4_ext_insert_extent(handle, inode, path, &newex); | 2764 | err = ext4_ext_insert_extent(handle, inode, path, &newex, 0); | 
| 2761 | if (err == -ENOSPC) { | 2765 | if (err == -ENOSPC) { | 
| 2762 | err = ext4_ext_zeroout(inode, &orig_ex); | 2766 | err = ext4_ext_zeroout(inode, &orig_ex); | 
| 2763 | if (err) | 2767 | if (err) | 
| @@ -2785,6 +2789,324 @@ fix_extent_len: | |||
| 2785 | } | 2789 | } | 
| 2786 | 2790 | ||
| 2787 | /* | 2791 | /* | 
| 2792 | * This function is called by ext4_ext_get_blocks() from | ||
| 2793 | * ext4_get_blocks_dio_write() when DIO to write | ||
| 2794 | * to an uninitialized extent. | ||
| 2795 | * | ||
| 2796 | * Writing to an uninitized extent may result in splitting the uninitialized | ||
| 2797 | * extent into multiple /intialized unintialized extents (up to three) | ||
| 2798 | * There are three possibilities: | ||
| 2799 | * a> There is no split required: Entire extent should be uninitialized | ||
| 2800 | * b> Splits in two extents: Write is happening at either end of the extent | ||
| 2801 | * c> Splits in three extents: Somone is writing in middle of the extent | ||
| 2802 | * | ||
| 2803 | * One of more index blocks maybe needed if the extent tree grow after | ||
| 2804 | * the unintialized extent split. To prevent ENOSPC occur at the IO | ||
| 2805 | * complete, we need to split the uninitialized extent before DIO submit | ||
| 2806 | * the IO. The uninitilized extent called at this time will be split | ||
| 2807 | * into three uninitialized extent(at most). After IO complete, the part | ||
| 2808 | * being filled will be convert to initialized by the end_io callback function | ||
| 2809 | * via ext4_convert_unwritten_extents(). | ||
| 2810 | */ | ||
| 2811 | static int ext4_split_unwritten_extents(handle_t *handle, | ||
| 2812 | struct inode *inode, | ||
| 2813 | struct ext4_ext_path *path, | ||
| 2814 | ext4_lblk_t iblock, | ||
| 2815 | unsigned int max_blocks, | ||
| 2816 | int flags) | ||
| 2817 | { | ||
| 2818 | struct ext4_extent *ex, newex, orig_ex; | ||
| 2819 | struct ext4_extent *ex1 = NULL; | ||
| 2820 | struct ext4_extent *ex2 = NULL; | ||
| 2821 | struct ext4_extent *ex3 = NULL; | ||
| 2822 | struct ext4_extent_header *eh; | ||
| 2823 | ext4_lblk_t ee_block; | ||
| 2824 | unsigned int allocated, ee_len, depth; | ||
| 2825 | ext4_fsblk_t newblock; | ||
| 2826 | int err = 0; | ||
| 2827 | int ret = 0; | ||
| 2828 | |||
| 2829 | ext_debug("ext4_split_unwritten_extents: inode %lu," | ||
| 2830 | "iblock %llu, max_blocks %u\n", inode->i_ino, | ||
| 2831 | (unsigned long long)iblock, max_blocks); | ||
| 2832 | depth = ext_depth(inode); | ||
| 2833 | eh = path[depth].p_hdr; | ||
| 2834 | ex = path[depth].p_ext; | ||
| 2835 | ee_block = le32_to_cpu(ex->ee_block); | ||
| 2836 | ee_len = ext4_ext_get_actual_len(ex); | ||
| 2837 | allocated = ee_len - (iblock - ee_block); | ||
| 2838 | newblock = iblock - ee_block + ext_pblock(ex); | ||
| 2839 | ex2 = ex; | ||
| 2840 | orig_ex.ee_block = ex->ee_block; | ||
| 2841 | orig_ex.ee_len = cpu_to_le16(ee_len); | ||
| 2842 | ext4_ext_store_pblock(&orig_ex, ext_pblock(ex)); | ||
| 2843 | |||
| 2844 | /* | ||
| 2845 | * if the entire unintialized extent length less than | ||
| 2846 | * the size of extent to write, there is no need to split | ||
| 2847 | * uninitialized extent | ||
| 2848 | */ | ||
| 2849 | if (allocated <= max_blocks) | ||
| 2850 | return ret; | ||
| 2851 | |||
| 2852 | err = ext4_ext_get_access(handle, inode, path + depth); | ||
| 2853 | if (err) | ||
| 2854 | goto out; | ||
| 2855 | /* ex1: ee_block to iblock - 1 : uninitialized */ | ||
| 2856 | if (iblock > ee_block) { | ||
| 2857 | ex1 = ex; | ||
| 2858 | ex1->ee_len = cpu_to_le16(iblock - ee_block); | ||
| 2859 | ext4_ext_mark_uninitialized(ex1); | ||
| 2860 | ex2 = &newex; | ||
| 2861 | } | ||
| 2862 | /* | ||
| 2863 | * for sanity, update the length of the ex2 extent before | ||
| 2864 | * we insert ex3, if ex1 is NULL. This is to avoid temporary | ||
| 2865 | * overlap of blocks. | ||
| 2866 | */ | ||
| 2867 | if (!ex1 && allocated > max_blocks) | ||
| 2868 | ex2->ee_len = cpu_to_le16(max_blocks); | ||
| 2869 | /* ex3: to ee_block + ee_len : uninitialised */ | ||
| 2870 | if (allocated > max_blocks) { | ||
| 2871 | unsigned int newdepth; | ||
| 2872 | ex3 = &newex; | ||
| 2873 | ex3->ee_block = cpu_to_le32(iblock + max_blocks); | ||
| 2874 | ext4_ext_store_pblock(ex3, newblock + max_blocks); | ||
| 2875 | ex3->ee_len = cpu_to_le16(allocated - max_blocks); | ||
| 2876 | ext4_ext_mark_uninitialized(ex3); | ||
| 2877 | err = ext4_ext_insert_extent(handle, inode, path, ex3, flags); | ||
| 2878 | if (err == -ENOSPC) { | ||
| 2879 | err = ext4_ext_zeroout(inode, &orig_ex); | ||
| 2880 | if (err) | ||
| 2881 | goto fix_extent_len; | ||
| 2882 | /* update the extent length and mark as initialized */ | ||
| 2883 | ex->ee_block = orig_ex.ee_block; | ||
| 2884 | ex->ee_len = orig_ex.ee_len; | ||
| 2885 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | ||
| 2886 | ext4_ext_dirty(handle, inode, path + depth); | ||
| 2887 | /* zeroed the full extent */ | ||
| 2888 | /* blocks available from iblock */ | ||
| 2889 | return allocated; | ||
| 2890 | |||
| 2891 | } else if (err) | ||
| 2892 | goto fix_extent_len; | ||
| 2893 | /* | ||
| 2894 | * The depth, and hence eh & ex might change | ||
| 2895 | * as part of the insert above. | ||
| 2896 | */ | ||
| 2897 | newdepth = ext_depth(inode); | ||
| 2898 | /* | ||
| 2899 | * update the extent length after successful insert of the | ||
| 2900 | * split extent | ||
| 2901 | */ | ||
| 2902 | orig_ex.ee_len = cpu_to_le16(ee_len - | ||
| 2903 | ext4_ext_get_actual_len(ex3)); | ||
| 2904 | depth = newdepth; | ||
| 2905 | ext4_ext_drop_refs(path); | ||
| 2906 | path = ext4_ext_find_extent(inode, iblock, path); | ||
| 2907 | if (IS_ERR(path)) { | ||
| 2908 | err = PTR_ERR(path); | ||
| 2909 | goto out; | ||
| 2910 | } | ||
| 2911 | eh = path[depth].p_hdr; | ||
| 2912 | ex = path[depth].p_ext; | ||
| 2913 | if (ex2 != &newex) | ||
| 2914 | ex2 = ex; | ||
| 2915 | |||
| 2916 | err = ext4_ext_get_access(handle, inode, path + depth); | ||
| 2917 | if (err) | ||
| 2918 | goto out; | ||
| 2919 | |||
| 2920 | allocated = max_blocks; | ||
| 2921 | } | ||
| 2922 | /* | ||
| 2923 | * If there was a change of depth as part of the | ||
| 2924 | * insertion of ex3 above, we need to update the length | ||
| 2925 | * of the ex1 extent again here | ||
| 2926 | */ | ||
| 2927 | if (ex1 && ex1 != ex) { | ||
| 2928 | ex1 = ex; | ||
| 2929 | ex1->ee_len = cpu_to_le16(iblock - ee_block); | ||
| 2930 | ext4_ext_mark_uninitialized(ex1); | ||
| 2931 | ex2 = &newex; | ||
| 2932 | } | ||
| 2933 | /* | ||
| 2934 | * ex2: iblock to iblock + maxblocks-1 : to be direct IO written, | ||
| 2935 | * uninitialised still. | ||
| 2936 | */ | ||
| 2937 | ex2->ee_block = cpu_to_le32(iblock); | ||
| 2938 | ext4_ext_store_pblock(ex2, newblock); | ||
| 2939 | ex2->ee_len = cpu_to_le16(allocated); | ||
| 2940 | ext4_ext_mark_uninitialized(ex2); | ||
| 2941 | if (ex2 != ex) | ||
| 2942 | goto insert; | ||
| 2943 | /* Mark modified extent as dirty */ | ||
| 2944 | err = ext4_ext_dirty(handle, inode, path + depth); | ||
| 2945 | ext_debug("out here\n"); | ||
| 2946 | goto out; | ||
| 2947 | insert: | ||
| 2948 | err = ext4_ext_insert_extent(handle, inode, path, &newex, flags); | ||
| 2949 | if (err == -ENOSPC) { | ||
| 2950 | err = ext4_ext_zeroout(inode, &orig_ex); | ||
| 2951 | if (err) | ||
| 2952 | goto fix_extent_len; | ||
| 2953 | /* update the extent length and mark as initialized */ | ||
| 2954 | ex->ee_block = orig_ex.ee_block; | ||
| 2955 | ex->ee_len = orig_ex.ee_len; | ||
| 2956 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | ||
| 2957 | ext4_ext_dirty(handle, inode, path + depth); | ||
| 2958 | /* zero out the first half */ | ||
| 2959 | return allocated; | ||
| 2960 | } else if (err) | ||
| 2961 | goto fix_extent_len; | ||
| 2962 | out: | ||
| 2963 | ext4_ext_show_leaf(inode, path); | ||
| 2964 | return err ? err : allocated; | ||
| 2965 | |||
| 2966 | fix_extent_len: | ||
| 2967 | ex->ee_block = orig_ex.ee_block; | ||
| 2968 | ex->ee_len = orig_ex.ee_len; | ||
| 2969 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | ||
| 2970 | ext4_ext_mark_uninitialized(ex); | ||
| 2971 | ext4_ext_dirty(handle, inode, path + depth); | ||
| 2972 | return err; | ||
| 2973 | } | ||
| 2974 | static int ext4_convert_unwritten_extents_dio(handle_t *handle, | ||
| 2975 | struct inode *inode, | ||
| 2976 | struct ext4_ext_path *path) | ||
| 2977 | { | ||
| 2978 | struct ext4_extent *ex; | ||
| 2979 | struct ext4_extent_header *eh; | ||
| 2980 | int depth; | ||
| 2981 | int err = 0; | ||
| 2982 | int ret = 0; | ||
| 2983 | |||
| 2984 | depth = ext_depth(inode); | ||
| 2985 | eh = path[depth].p_hdr; | ||
| 2986 | ex = path[depth].p_ext; | ||
| 2987 | |||
| 2988 | err = ext4_ext_get_access(handle, inode, path + depth); | ||
| 2989 | if (err) | ||
| 2990 | goto out; | ||
| 2991 | /* first mark the extent as initialized */ | ||
| 2992 | ext4_ext_mark_initialized(ex); | ||
| 2993 | |||
| 2994 | /* | ||
| 2995 | * We have to see if it can be merged with the extent | ||
| 2996 | * on the left. | ||
| 2997 | */ | ||
| 2998 | if (ex > EXT_FIRST_EXTENT(eh)) { | ||
| 2999 | /* | ||
| 3000 | * To merge left, pass "ex - 1" to try_to_merge(), | ||
| 3001 | * since it merges towards right _only_. | ||
| 3002 | */ | ||
| 3003 | ret = ext4_ext_try_to_merge(inode, path, ex - 1); | ||
| 3004 | if (ret) { | ||
| 3005 | err = ext4_ext_correct_indexes(handle, inode, path); | ||
| 3006 | if (err) | ||
| 3007 | goto out; | ||
| 3008 | depth = ext_depth(inode); | ||
| 3009 | ex--; | ||
| 3010 | } | ||
| 3011 | } | ||
| 3012 | /* | ||
| 3013 | * Try to Merge towards right. | ||
| 3014 | */ | ||
| 3015 | ret = ext4_ext_try_to_merge(inode, path, ex); | ||
| 3016 | if (ret) { | ||
| 3017 | err = ext4_ext_correct_indexes(handle, inode, path); | ||
| 3018 | if (err) | ||
| 3019 | goto out; | ||
| 3020 | depth = ext_depth(inode); | ||
| 3021 | } | ||
| 3022 | /* Mark modified extent as dirty */ | ||
| 3023 | err = ext4_ext_dirty(handle, inode, path + depth); | ||
| 3024 | out: | ||
| 3025 | ext4_ext_show_leaf(inode, path); | ||
| 3026 | return err; | ||
| 3027 | } | ||
| 3028 | |||
| 3029 | static int | ||
| 3030 | ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode, | ||
| 3031 | ext4_lblk_t iblock, unsigned int max_blocks, | ||
| 3032 | struct ext4_ext_path *path, int flags, | ||
| 3033 | unsigned int allocated, struct buffer_head *bh_result, | ||
| 3034 | ext4_fsblk_t newblock) | ||
| 3035 | { | ||
| 3036 | int ret = 0; | ||
| 3037 | int err = 0; | ||
| 3038 | ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio; | ||
| 3039 | |||
| 3040 | ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical" | ||
| 3041 | "block %llu, max_blocks %u, flags %d, allocated %u", | ||
| 3042 | inode->i_ino, (unsigned long long)iblock, max_blocks, | ||
| 3043 | flags, allocated); | ||
| 3044 | ext4_ext_show_leaf(inode, path); | ||
| 3045 | |||
| 3046 | /* DIO get_block() before submit the IO, split the extent */ | ||
| 3047 | if (flags == EXT4_GET_BLOCKS_DIO_CREATE_EXT) { | ||
| 3048 | ret = ext4_split_unwritten_extents(handle, | ||
| 3049 | inode, path, iblock, | ||
| 3050 | max_blocks, flags); | ||
| 3051 | /* flag the io_end struct that we need convert when IO done */ | ||
| 3052 | if (io) | ||
| 3053 | io->flag = DIO_AIO_UNWRITTEN; | ||
| 3054 | goto out; | ||
| 3055 | } | ||
| 3056 | /* DIO end_io complete, convert the filled extent to written */ | ||
| 3057 | if (flags == EXT4_GET_BLOCKS_DIO_CONVERT_EXT) { | ||
| 3058 | ret = ext4_convert_unwritten_extents_dio(handle, inode, | ||
| 3059 | path); | ||
| 3060 | goto out2; | ||
| 3061 | } | ||
| 3062 | /* buffered IO case */ | ||
| 3063 | /* | ||
| 3064 | * repeat fallocate creation request | ||
| 3065 | * we already have an unwritten extent | ||
| 3066 | */ | ||
| 3067 | if (flags & EXT4_GET_BLOCKS_UNINIT_EXT) | ||
| 3068 | goto map_out; | ||
| 3069 | |||
| 3070 | /* buffered READ or buffered write_begin() lookup */ | ||
| 3071 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { | ||
| 3072 | /* | ||
| 3073 | * We have blocks reserved already. We | ||
| 3074 | * return allocated blocks so that delalloc | ||
| 3075 | * won't do block reservation for us. But | ||
| 3076 | * the buffer head will be unmapped so that | ||
| 3077 | * a read from the block returns 0s. | ||
| 3078 | */ | ||
| 3079 | set_buffer_unwritten(bh_result); | ||
| 3080 | goto out1; | ||
| 3081 | } | ||
| 3082 | |||
| 3083 | /* buffered write, writepage time, convert*/ | ||
| 3084 | ret = ext4_ext_convert_to_initialized(handle, inode, | ||
| 3085 | path, iblock, | ||
| 3086 | max_blocks); | ||
| 3087 | out: | ||
| 3088 | if (ret <= 0) { | ||
| 3089 | err = ret; | ||
| 3090 | goto out2; | ||
| 3091 | } else | ||
| 3092 | allocated = ret; | ||
| 3093 | set_buffer_new(bh_result); | ||
| 3094 | map_out: | ||
| 3095 | set_buffer_mapped(bh_result); | ||
| 3096 | out1: | ||
| 3097 | if (allocated > max_blocks) | ||
| 3098 | allocated = max_blocks; | ||
| 3099 | ext4_ext_show_leaf(inode, path); | ||
| 3100 | bh_result->b_bdev = inode->i_sb->s_bdev; | ||
| 3101 | bh_result->b_blocknr = newblock; | ||
| 3102 | out2: | ||
| 3103 | if (path) { | ||
| 3104 | ext4_ext_drop_refs(path); | ||
| 3105 | kfree(path); | ||
| 3106 | } | ||
| 3107 | return err ? err : allocated; | ||
| 3108 | } | ||
| 3109 | /* | ||
| 2788 | * Block allocation/map/preallocation routine for extents based files | 3110 | * Block allocation/map/preallocation routine for extents based files | 
| 2789 | * | 3111 | * | 
| 2790 | * | 3112 | * | 
| @@ -2814,6 +3136,7 @@ int ext4_ext_get_blocks(handle_t *handle, struct inode *inode, | |||
| 2814 | int err = 0, depth, ret, cache_type; | 3136 | int err = 0, depth, ret, cache_type; | 
| 2815 | unsigned int allocated = 0; | 3137 | unsigned int allocated = 0; | 
| 2816 | struct ext4_allocation_request ar; | 3138 | struct ext4_allocation_request ar; | 
| 3139 | ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio; | ||
| 2817 | 3140 | ||
| 2818 | __clear_bit(BH_New, &bh_result->b_state); | 3141 | __clear_bit(BH_New, &bh_result->b_state); | 
| 2819 | ext_debug("blocks %u/%u requested for inode %lu\n", | 3142 | ext_debug("blocks %u/%u requested for inode %lu\n", | 
| @@ -2889,33 +3212,10 @@ int ext4_ext_get_blocks(handle_t *handle, struct inode *inode, | |||
| 2889 | EXT4_EXT_CACHE_EXTENT); | 3212 | EXT4_EXT_CACHE_EXTENT); | 
| 2890 | goto out; | 3213 | goto out; | 
| 2891 | } | 3214 | } | 
| 2892 | if (flags & EXT4_GET_BLOCKS_UNINIT_EXT) | 3215 | ret = ext4_ext_handle_uninitialized_extents(handle, | 
| 2893 | goto out; | 3216 | inode, iblock, max_blocks, path, | 
| 2894 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { | 3217 | flags, allocated, bh_result, newblock); | 
| 2895 | if (allocated > max_blocks) | 3218 | return ret; | 
| 2896 | allocated = max_blocks; | ||
| 2897 | /* | ||
| 2898 | * We have blocks reserved already. We | ||
| 2899 | * return allocated blocks so that delalloc | ||
| 2900 | * won't do block reservation for us. But | ||
| 2901 | * the buffer head will be unmapped so that | ||
| 2902 | * a read from the block returns 0s. | ||
| 2903 | */ | ||
| 2904 | set_buffer_unwritten(bh_result); | ||
| 2905 | bh_result->b_bdev = inode->i_sb->s_bdev; | ||
| 2906 | bh_result->b_blocknr = newblock; | ||
| 2907 | goto out2; | ||
| 2908 | } | ||
| 2909 | |||
| 2910 | ret = ext4_ext_convert_to_initialized(handle, inode, | ||
| 2911 | path, iblock, | ||
| 2912 | max_blocks); | ||
| 2913 | if (ret <= 0) { | ||
| 2914 | err = ret; | ||
| 2915 | goto out2; | ||
| 2916 | } else | ||
| 2917 | allocated = ret; | ||
| 2918 | goto outnew; | ||
| 2919 | } | 3219 | } | 
| 2920 | } | 3220 | } | 
| 2921 | 3221 | ||
| @@ -2986,9 +3286,21 @@ int ext4_ext_get_blocks(handle_t *handle, struct inode *inode, | |||
| 2986 | /* try to insert new extent into found leaf and return */ | 3286 | /* try to insert new extent into found leaf and return */ | 
| 2987 | ext4_ext_store_pblock(&newex, newblock); | 3287 | ext4_ext_store_pblock(&newex, newblock); | 
| 2988 | newex.ee_len = cpu_to_le16(ar.len); | 3288 | newex.ee_len = cpu_to_le16(ar.len); | 
| 2989 | if (flags & EXT4_GET_BLOCKS_UNINIT_EXT) /* Mark uninitialized */ | 3289 | /* Mark uninitialized */ | 
| 3290 | if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){ | ||
| 2990 | ext4_ext_mark_uninitialized(&newex); | 3291 | ext4_ext_mark_uninitialized(&newex); | 
| 2991 | err = ext4_ext_insert_extent(handle, inode, path, &newex); | 3292 | /* | 
| 3293 | * io_end structure was created for every async | ||
| 3294 | * direct IO write to the middle of the file. | ||
| 3295 | * To avoid unecessary convertion for every aio dio rewrite | ||
| 3296 | * to the mid of file, here we flag the IO that is really | ||
| 3297 | * need the convertion. | ||
| 3298 | * | ||
| 3299 | */ | ||
| 3300 | if (io && flags == EXT4_GET_BLOCKS_DIO_CREATE_EXT) | ||
| 3301 | io->flag = DIO_AIO_UNWRITTEN; | ||
| 3302 | } | ||
| 3303 | err = ext4_ext_insert_extent(handle, inode, path, &newex, flags); | ||
| 2992 | if (err) { | 3304 | if (err) { | 
| 2993 | /* free data blocks we just allocated */ | 3305 | /* free data blocks we just allocated */ | 
| 2994 | /* not a good idea to call discard here directly, | 3306 | /* not a good idea to call discard here directly, | 
| @@ -3002,7 +3314,6 @@ int ext4_ext_get_blocks(handle_t *handle, struct inode *inode, | |||
| 3002 | /* previous routine could use block we allocated */ | 3314 | /* previous routine could use block we allocated */ | 
| 3003 | newblock = ext_pblock(&newex); | 3315 | newblock = ext_pblock(&newex); | 
| 3004 | allocated = ext4_ext_get_actual_len(&newex); | 3316 | allocated = ext4_ext_get_actual_len(&newex); | 
| 3005 | outnew: | ||
| 3006 | set_buffer_new(bh_result); | 3317 | set_buffer_new(bh_result); | 
| 3007 | 3318 | ||
| 3008 | /* Cache only when it is _not_ an uninitialized extent */ | 3319 | /* Cache only when it is _not_ an uninitialized extent */ | 
| @@ -3201,6 +3512,63 @@ retry: | |||
| 3201 | } | 3512 | } | 
| 3202 | 3513 | ||
| 3203 | /* | 3514 | /* | 
| 3515 | * This function convert a range of blocks to written extents | ||
| 3516 | * The caller of this function will pass the start offset and the size. | ||
| 3517 | * all unwritten extents within this range will be converted to | ||
| 3518 | * written extents. | ||
| 3519 | * | ||
| 3520 | * This function is called from the direct IO end io call back | ||
| 3521 | * function, to convert the fallocated extents after IO is completed. | ||
| 3522 | */ | ||
| 3523 | int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset, | ||
| 3524 | loff_t len) | ||
| 3525 | { | ||
| 3526 | handle_t *handle; | ||
| 3527 | ext4_lblk_t block; | ||
| 3528 | unsigned int max_blocks; | ||
| 3529 | int ret = 0; | ||
| 3530 | int ret2 = 0; | ||
| 3531 | struct buffer_head map_bh; | ||
| 3532 | unsigned int credits, blkbits = inode->i_blkbits; | ||
| 3533 | |||
| 3534 | block = offset >> blkbits; | ||
| 3535 | /* | ||
| 3536 | * We can't just convert len to max_blocks because | ||
| 3537 | * If blocksize = 4096 offset = 3072 and len = 2048 | ||
| 3538 | */ | ||
| 3539 | max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) | ||
| 3540 | - block; | ||
| 3541 | /* | ||
| 3542 | * credits to insert 1 extent into extent tree | ||
| 3543 | */ | ||
| 3544 | credits = ext4_chunk_trans_blocks(inode, max_blocks); | ||
| 3545 | while (ret >= 0 && ret < max_blocks) { | ||
| 3546 | block = block + ret; | ||
| 3547 | max_blocks = max_blocks - ret; | ||
| 3548 | handle = ext4_journal_start(inode, credits); | ||
| 3549 | if (IS_ERR(handle)) { | ||
| 3550 | ret = PTR_ERR(handle); | ||
| 3551 | break; | ||
| 3552 | } | ||
| 3553 | map_bh.b_state = 0; | ||
| 3554 | ret = ext4_get_blocks(handle, inode, block, | ||
| 3555 | max_blocks, &map_bh, | ||
| 3556 | EXT4_GET_BLOCKS_DIO_CONVERT_EXT); | ||
| 3557 | if (ret <= 0) { | ||
| 3558 | WARN_ON(ret <= 0); | ||
| 3559 | printk(KERN_ERR "%s: ext4_ext_get_blocks " | ||
| 3560 | "returned error inode#%lu, block=%u, " | ||
| 3561 | "max_blocks=%u", __func__, | ||
| 3562 | inode->i_ino, block, max_blocks); | ||
| 3563 | } | ||
| 3564 | ext4_mark_inode_dirty(handle, inode); | ||
| 3565 | ret2 = ext4_journal_stop(handle); | ||
| 3566 | if (ret <= 0 || ret2 ) | ||
| 3567 | break; | ||
| 3568 | } | ||
| 3569 | return ret > 0 ? ret2 : ret; | ||
| 3570 | } | ||
| 3571 | /* | ||
| 3204 | * Callback function called for each extent to gather FIEMAP information. | 3572 | * Callback function called for each extent to gather FIEMAP information. | 
| 3205 | */ | 3573 | */ | 
| 3206 | static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path, | 3574 | static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path, | 
