aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ext4/extents.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ext4/extents.c')
-rw-r--r--fs/ext4/extents.c458
1 files changed, 420 insertions, 38 deletions
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 7a3832577923..715264b4bae4 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 */
726static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, 726int 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 */
1587int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, 1587int 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
1723merge: 1724merge:
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,192 @@ 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;
2759insert: 2763insert:
2760 err = ext4_ext_insert_extent(handle, inode, path, &newex); 2764 err = ext4_ext_insert_extent(handle, inode, path, &newex, 0);
2765 if (err == -ENOSPC) {
2766 err = ext4_ext_zeroout(inode, &orig_ex);
2767 if (err)
2768 goto fix_extent_len;
2769 /* update the extent length and mark as initialized */
2770 ex->ee_block = orig_ex.ee_block;
2771 ex->ee_len = orig_ex.ee_len;
2772 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2773 ext4_ext_dirty(handle, inode, path + depth);
2774 /* zero out the first half */
2775 return allocated;
2776 } else if (err)
2777 goto fix_extent_len;
2778out:
2779 ext4_ext_show_leaf(inode, path);
2780 return err ? err : allocated;
2781
2782fix_extent_len:
2783 ex->ee_block = orig_ex.ee_block;
2784 ex->ee_len = orig_ex.ee_len;
2785 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2786 ext4_ext_mark_uninitialized(ex);
2787 ext4_ext_dirty(handle, inode, path + depth);
2788 return err;
2789}
2790
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 * Returns the size of uninitialized extent to be written on success.
2812 */
2813static int ext4_split_unwritten_extents(handle_t *handle,
2814 struct inode *inode,
2815 struct ext4_ext_path *path,
2816 ext4_lblk_t iblock,
2817 unsigned int max_blocks,
2818 int flags)
2819{
2820 struct ext4_extent *ex, newex, orig_ex;
2821 struct ext4_extent *ex1 = NULL;
2822 struct ext4_extent *ex2 = NULL;
2823 struct ext4_extent *ex3 = NULL;
2824 struct ext4_extent_header *eh;
2825 ext4_lblk_t ee_block;
2826 unsigned int allocated, ee_len, depth;
2827 ext4_fsblk_t newblock;
2828 int err = 0;
2829
2830 ext_debug("ext4_split_unwritten_extents: inode %lu,"
2831 "iblock %llu, max_blocks %u\n", inode->i_ino,
2832 (unsigned long long)iblock, max_blocks);
2833 depth = ext_depth(inode);
2834 eh = path[depth].p_hdr;
2835 ex = path[depth].p_ext;
2836 ee_block = le32_to_cpu(ex->ee_block);
2837 ee_len = ext4_ext_get_actual_len(ex);
2838 allocated = ee_len - (iblock - ee_block);
2839 newblock = iblock - ee_block + ext_pblock(ex);
2840 ex2 = ex;
2841 orig_ex.ee_block = ex->ee_block;
2842 orig_ex.ee_len = cpu_to_le16(ee_len);
2843 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
2844
2845 /*
2846 * If the uninitialized extent begins at the same logical
2847 * block where the write begins, and the write completely
2848 * covers the extent, then we don't need to split it.
2849 */
2850 if ((iblock == ee_block) && (allocated <= max_blocks))
2851 return allocated;
2852
2853 err = ext4_ext_get_access(handle, inode, path + depth);
2854 if (err)
2855 goto out;
2856 /* ex1: ee_block to iblock - 1 : uninitialized */
2857 if (iblock > ee_block) {
2858 ex1 = ex;
2859 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2860 ext4_ext_mark_uninitialized(ex1);
2861 ex2 = &newex;
2862 }
2863 /*
2864 * for sanity, update the length of the ex2 extent before
2865 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2866 * overlap of blocks.
2867 */
2868 if (!ex1 && allocated > max_blocks)
2869 ex2->ee_len = cpu_to_le16(max_blocks);
2870 /* ex3: to ee_block + ee_len : uninitialised */
2871 if (allocated > max_blocks) {
2872 unsigned int newdepth;
2873 ex3 = &newex;
2874 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2875 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2876 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2877 ext4_ext_mark_uninitialized(ex3);
2878 err = ext4_ext_insert_extent(handle, inode, path, ex3, flags);
2879 if (err == -ENOSPC) {
2880 err = ext4_ext_zeroout(inode, &orig_ex);
2881 if (err)
2882 goto fix_extent_len;
2883 /* update the extent length and mark as initialized */
2884 ex->ee_block = orig_ex.ee_block;
2885 ex->ee_len = orig_ex.ee_len;
2886 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2887 ext4_ext_dirty(handle, inode, path + depth);
2888 /* zeroed the full extent */
2889 /* blocks available from iblock */
2890 return allocated;
2891
2892 } else if (err)
2893 goto fix_extent_len;
2894 /*
2895 * The depth, and hence eh & ex might change
2896 * as part of the insert above.
2897 */
2898 newdepth = ext_depth(inode);
2899 /*
2900 * update the extent length after successful insert of the
2901 * split extent
2902 */
2903 orig_ex.ee_len = cpu_to_le16(ee_len -
2904 ext4_ext_get_actual_len(ex3));
2905 depth = newdepth;
2906 ext4_ext_drop_refs(path);
2907 path = ext4_ext_find_extent(inode, iblock, path);
2908 if (IS_ERR(path)) {
2909 err = PTR_ERR(path);
2910 goto out;
2911 }
2912 eh = path[depth].p_hdr;
2913 ex = path[depth].p_ext;
2914 if (ex2 != &newex)
2915 ex2 = ex;
2916
2917 err = ext4_ext_get_access(handle, inode, path + depth);
2918 if (err)
2919 goto out;
2920
2921 allocated = max_blocks;
2922 }
2923 /*
2924 * If there was a change of depth as part of the
2925 * insertion of ex3 above, we need to update the length
2926 * of the ex1 extent again here
2927 */
2928 if (ex1 && ex1 != ex) {
2929 ex1 = ex;
2930 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2931 ext4_ext_mark_uninitialized(ex1);
2932 ex2 = &newex;
2933 }
2934 /*
2935 * ex2: iblock to iblock + maxblocks-1 : to be direct IO written,
2936 * uninitialised still.
2937 */
2938 ex2->ee_block = cpu_to_le32(iblock);
2939 ext4_ext_store_pblock(ex2, newblock);
2940 ex2->ee_len = cpu_to_le16(allocated);
2941 ext4_ext_mark_uninitialized(ex2);
2942 if (ex2 != ex)
2943 goto insert;
2944 /* Mark modified extent as dirty */
2945 err = ext4_ext_dirty(handle, inode, path + depth);
2946 ext_debug("out here\n");
2947 goto out;
2948insert:
2949 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2761 if (err == -ENOSPC) { 2950 if (err == -ENOSPC) {
2762 err = ext4_ext_zeroout(inode, &orig_ex); 2951 err = ext4_ext_zeroout(inode, &orig_ex);
2763 if (err) 2952 if (err)
@@ -2783,7 +2972,147 @@ fix_extent_len:
2783 ext4_ext_dirty(handle, inode, path + depth); 2972 ext4_ext_dirty(handle, inode, path + depth);
2784 return err; 2973 return err;
2785} 2974}
2975static int ext4_convert_unwritten_extents_dio(handle_t *handle,
2976 struct inode *inode,
2977 struct ext4_ext_path *path)
2978{
2979 struct ext4_extent *ex;
2980 struct ext4_extent_header *eh;
2981 int depth;
2982 int err = 0;
2983 int ret = 0;
2984
2985 depth = ext_depth(inode);
2986 eh = path[depth].p_hdr;
2987 ex = path[depth].p_ext;
2988
2989 err = ext4_ext_get_access(handle, inode, path + depth);
2990 if (err)
2991 goto out;
2992 /* first mark the extent as initialized */
2993 ext4_ext_mark_initialized(ex);
2994
2995 /*
2996 * We have to see if it can be merged with the extent
2997 * on the left.
2998 */
2999 if (ex > EXT_FIRST_EXTENT(eh)) {
3000 /*
3001 * To merge left, pass "ex - 1" to try_to_merge(),
3002 * since it merges towards right _only_.
3003 */
3004 ret = ext4_ext_try_to_merge(inode, path, ex - 1);
3005 if (ret) {
3006 err = ext4_ext_correct_indexes(handle, inode, path);
3007 if (err)
3008 goto out;
3009 depth = ext_depth(inode);
3010 ex--;
3011 }
3012 }
3013 /*
3014 * Try to Merge towards right.
3015 */
3016 ret = ext4_ext_try_to_merge(inode, path, ex);
3017 if (ret) {
3018 err = ext4_ext_correct_indexes(handle, inode, path);
3019 if (err)
3020 goto out;
3021 depth = ext_depth(inode);
3022 }
3023 /* Mark modified extent as dirty */
3024 err = ext4_ext_dirty(handle, inode, path + depth);
3025out:
3026 ext4_ext_show_leaf(inode, path);
3027 return err;
3028}
3029
3030static int
3031ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
3032 ext4_lblk_t iblock, unsigned int max_blocks,
3033 struct ext4_ext_path *path, int flags,
3034 unsigned int allocated, struct buffer_head *bh_result,
3035 ext4_fsblk_t newblock)
3036{
3037 int ret = 0;
3038 int err = 0;
3039 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
3040
3041 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3042 "block %llu, max_blocks %u, flags %d, allocated %u",
3043 inode->i_ino, (unsigned long long)iblock, max_blocks,
3044 flags, allocated);
3045 ext4_ext_show_leaf(inode, path);
2786 3046
3047 /* DIO get_block() before submit the IO, split the extent */
3048 if (flags == EXT4_GET_BLOCKS_DIO_CREATE_EXT) {
3049 ret = ext4_split_unwritten_extents(handle,
3050 inode, path, iblock,
3051 max_blocks, flags);
3052 /*
3053 * Flag the inode(non aio case) or end_io struct (aio case)
3054 * that this IO needs to convertion to written when IO is
3055 * completed
3056 */
3057 if (io)
3058 io->flag = DIO_AIO_UNWRITTEN;
3059 else
3060 EXT4_I(inode)->i_state |= EXT4_STATE_DIO_UNWRITTEN;
3061 goto out;
3062 }
3063 /* async DIO end_io complete, convert the filled extent to written */
3064 if (flags == EXT4_GET_BLOCKS_DIO_CONVERT_EXT) {
3065 ret = ext4_convert_unwritten_extents_dio(handle, inode,
3066 path);
3067 goto out2;
3068 }
3069 /* buffered IO case */
3070 /*
3071 * repeat fallocate creation request
3072 * we already have an unwritten extent
3073 */
3074 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3075 goto map_out;
3076
3077 /* buffered READ or buffered write_begin() lookup */
3078 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3079 /*
3080 * We have blocks reserved already. We
3081 * return allocated blocks so that delalloc
3082 * won't do block reservation for us. But
3083 * the buffer head will be unmapped so that
3084 * a read from the block returns 0s.
3085 */
3086 set_buffer_unwritten(bh_result);
3087 goto out1;
3088 }
3089
3090 /* buffered write, writepage time, convert*/
3091 ret = ext4_ext_convert_to_initialized(handle, inode,
3092 path, iblock,
3093 max_blocks);
3094out:
3095 if (ret <= 0) {
3096 err = ret;
3097 goto out2;
3098 } else
3099 allocated = ret;
3100 set_buffer_new(bh_result);
3101map_out:
3102 set_buffer_mapped(bh_result);
3103out1:
3104 if (allocated > max_blocks)
3105 allocated = max_blocks;
3106 ext4_ext_show_leaf(inode, path);
3107 bh_result->b_bdev = inode->i_sb->s_bdev;
3108 bh_result->b_blocknr = newblock;
3109out2:
3110 if (path) {
3111 ext4_ext_drop_refs(path);
3112 kfree(path);
3113 }
3114 return err ? err : allocated;
3115}
2787/* 3116/*
2788 * Block allocation/map/preallocation routine for extents based files 3117 * Block allocation/map/preallocation routine for extents based files
2789 * 3118 *
@@ -2814,6 +3143,7 @@ int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
2814 int err = 0, depth, ret, cache_type; 3143 int err = 0, depth, ret, cache_type;
2815 unsigned int allocated = 0; 3144 unsigned int allocated = 0;
2816 struct ext4_allocation_request ar; 3145 struct ext4_allocation_request ar;
3146 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
2817 3147
2818 __clear_bit(BH_New, &bh_result->b_state); 3148 __clear_bit(BH_New, &bh_result->b_state);
2819 ext_debug("blocks %u/%u requested for inode %lu\n", 3149 ext_debug("blocks %u/%u requested for inode %lu\n",
@@ -2889,33 +3219,10 @@ int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
2889 EXT4_EXT_CACHE_EXTENT); 3219 EXT4_EXT_CACHE_EXTENT);
2890 goto out; 3220 goto out;
2891 } 3221 }
2892 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT) 3222 ret = ext4_ext_handle_uninitialized_extents(handle,
2893 goto out; 3223 inode, iblock, max_blocks, path,
2894 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { 3224 flags, allocated, bh_result, newblock);
2895 if (allocated > max_blocks) 3225 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 } 3226 }
2920 } 3227 }
2921 3228
@@ -2986,9 +3293,27 @@ int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
2986 /* try to insert new extent into found leaf and return */ 3293 /* try to insert new extent into found leaf and return */
2987 ext4_ext_store_pblock(&newex, newblock); 3294 ext4_ext_store_pblock(&newex, newblock);
2988 newex.ee_len = cpu_to_le16(ar.len); 3295 newex.ee_len = cpu_to_le16(ar.len);
2989 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT) /* Mark uninitialized */ 3296 /* Mark uninitialized */
3297 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
2990 ext4_ext_mark_uninitialized(&newex); 3298 ext4_ext_mark_uninitialized(&newex);
2991 err = ext4_ext_insert_extent(handle, inode, path, &newex); 3299 /*
3300 * io_end structure was created for every async
3301 * direct IO write to the middle of the file.
3302 * To avoid unecessary convertion for every aio dio rewrite
3303 * to the mid of file, here we flag the IO that is really
3304 * need the convertion.
3305 * For non asycn direct IO case, flag the inode state
3306 * that we need to perform convertion when IO is done.
3307 */
3308 if (flags == EXT4_GET_BLOCKS_DIO_CREATE_EXT) {
3309 if (io)
3310 io->flag = DIO_AIO_UNWRITTEN;
3311 else
3312 EXT4_I(inode)->i_state |=
3313 EXT4_STATE_DIO_UNWRITTEN;;
3314 }
3315 }
3316 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2992 if (err) { 3317 if (err) {
2993 /* free data blocks we just allocated */ 3318 /* free data blocks we just allocated */
2994 /* not a good idea to call discard here directly, 3319 /* not a good idea to call discard here directly,
@@ -3002,7 +3327,6 @@ int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
3002 /* previous routine could use block we allocated */ 3327 /* previous routine could use block we allocated */
3003 newblock = ext_pblock(&newex); 3328 newblock = ext_pblock(&newex);
3004 allocated = ext4_ext_get_actual_len(&newex); 3329 allocated = ext4_ext_get_actual_len(&newex);
3005outnew:
3006 set_buffer_new(bh_result); 3330 set_buffer_new(bh_result);
3007 3331
3008 /* Cache only when it is _not_ an uninitialized extent */ 3332 /* Cache only when it is _not_ an uninitialized extent */
@@ -3201,6 +3525,64 @@ retry:
3201} 3525}
3202 3526
3203/* 3527/*
3528 * This function convert a range of blocks to written extents
3529 * The caller of this function will pass the start offset and the size.
3530 * all unwritten extents within this range will be converted to
3531 * written extents.
3532 *
3533 * This function is called from the direct IO end io call back
3534 * function, to convert the fallocated extents after IO is completed.
3535 * Returns 0 on success.
3536 */
3537int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
3538 loff_t len)
3539{
3540 handle_t *handle;
3541 ext4_lblk_t block;
3542 unsigned int max_blocks;
3543 int ret = 0;
3544 int ret2 = 0;
3545 struct buffer_head map_bh;
3546 unsigned int credits, blkbits = inode->i_blkbits;
3547
3548 block = offset >> blkbits;
3549 /*
3550 * We can't just convert len to max_blocks because
3551 * If blocksize = 4096 offset = 3072 and len = 2048
3552 */
3553 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
3554 - block;
3555 /*
3556 * credits to insert 1 extent into extent tree
3557 */
3558 credits = ext4_chunk_trans_blocks(inode, max_blocks);
3559 while (ret >= 0 && ret < max_blocks) {
3560 block = block + ret;
3561 max_blocks = max_blocks - ret;
3562 handle = ext4_journal_start(inode, credits);
3563 if (IS_ERR(handle)) {
3564 ret = PTR_ERR(handle);
3565 break;
3566 }
3567 map_bh.b_state = 0;
3568 ret = ext4_get_blocks(handle, inode, block,
3569 max_blocks, &map_bh,
3570 EXT4_GET_BLOCKS_DIO_CONVERT_EXT);
3571 if (ret <= 0) {
3572 WARN_ON(ret <= 0);
3573 printk(KERN_ERR "%s: ext4_ext_get_blocks "
3574 "returned error inode#%lu, block=%u, "
3575 "max_blocks=%u", __func__,
3576 inode->i_ino, block, max_blocks);
3577 }
3578 ext4_mark_inode_dirty(handle, inode);
3579 ret2 = ext4_journal_stop(handle);
3580 if (ret <= 0 || ret2 )
3581 break;
3582 }
3583 return ret > 0 ? ret2 : ret;
3584}
3585/*
3204 * Callback function called for each extent to gather FIEMAP information. 3586 * Callback function called for each extent to gather FIEMAP information.
3205 */ 3587 */
3206static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path, 3588static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,