diff options
Diffstat (limited to 'fs/ext4/extents.c')
-rw-r--r-- | fs/ext4/extents.c | 619 |
1 files changed, 403 insertions, 216 deletions
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index 74292a71b384..37043d0b2be8 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c | |||
@@ -73,8 +73,7 @@ static int ext4_extent_block_csum_verify(struct inode *inode, | |||
73 | { | 73 | { |
74 | struct ext4_extent_tail *et; | 74 | struct ext4_extent_tail *et; |
75 | 75 | ||
76 | if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, | 76 | if (!ext4_has_metadata_csum(inode->i_sb)) |
77 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) | ||
78 | return 1; | 77 | return 1; |
79 | 78 | ||
80 | et = find_ext4_extent_tail(eh); | 79 | et = find_ext4_extent_tail(eh); |
@@ -88,8 +87,7 @@ static void ext4_extent_block_csum_set(struct inode *inode, | |||
88 | { | 87 | { |
89 | struct ext4_extent_tail *et; | 88 | struct ext4_extent_tail *et; |
90 | 89 | ||
91 | if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, | 90 | if (!ext4_has_metadata_csum(inode->i_sb)) |
92 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) | ||
93 | return; | 91 | return; |
94 | 92 | ||
95 | et = find_ext4_extent_tail(eh); | 93 | et = find_ext4_extent_tail(eh); |
@@ -98,14 +96,14 @@ static void ext4_extent_block_csum_set(struct inode *inode, | |||
98 | 96 | ||
99 | static int ext4_split_extent(handle_t *handle, | 97 | static int ext4_split_extent(handle_t *handle, |
100 | struct inode *inode, | 98 | struct inode *inode, |
101 | struct ext4_ext_path *path, | 99 | struct ext4_ext_path **ppath, |
102 | struct ext4_map_blocks *map, | 100 | struct ext4_map_blocks *map, |
103 | int split_flag, | 101 | int split_flag, |
104 | int flags); | 102 | int flags); |
105 | 103 | ||
106 | static int ext4_split_extent_at(handle_t *handle, | 104 | static int ext4_split_extent_at(handle_t *handle, |
107 | struct inode *inode, | 105 | struct inode *inode, |
108 | struct ext4_ext_path *path, | 106 | struct ext4_ext_path **ppath, |
109 | ext4_lblk_t split, | 107 | ext4_lblk_t split, |
110 | int split_flag, | 108 | int split_flag, |
111 | int flags); | 109 | int flags); |
@@ -291,6 +289,20 @@ static inline int ext4_ext_space_root_idx(struct inode *inode, int check) | |||
291 | return size; | 289 | return size; |
292 | } | 290 | } |
293 | 291 | ||
292 | static inline int | ||
293 | ext4_force_split_extent_at(handle_t *handle, struct inode *inode, | ||
294 | struct ext4_ext_path **ppath, ext4_lblk_t lblk, | ||
295 | int nofail) | ||
296 | { | ||
297 | struct ext4_ext_path *path = *ppath; | ||
298 | int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext); | ||
299 | |||
300 | return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ? | ||
301 | EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0, | ||
302 | EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO | | ||
303 | (nofail ? EXT4_GET_BLOCKS_METADATA_NOFAIL:0)); | ||
304 | } | ||
305 | |||
294 | /* | 306 | /* |
295 | * Calculate the number of metadata blocks needed | 307 | * Calculate the number of metadata blocks needed |
296 | * to allocate @blocks | 308 | * to allocate @blocks |
@@ -695,9 +707,11 @@ static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path, | |||
695 | 707 | ||
696 | void ext4_ext_drop_refs(struct ext4_ext_path *path) | 708 | void ext4_ext_drop_refs(struct ext4_ext_path *path) |
697 | { | 709 | { |
698 | int depth = path->p_depth; | 710 | int depth, i; |
699 | int i; | ||
700 | 711 | ||
712 | if (!path) | ||
713 | return; | ||
714 | depth = path->p_depth; | ||
701 | for (i = 0; i <= depth; i++, path++) | 715 | for (i = 0; i <= depth; i++, path++) |
702 | if (path->p_bh) { | 716 | if (path->p_bh) { |
703 | brelse(path->p_bh); | 717 | brelse(path->p_bh); |
@@ -841,24 +855,32 @@ int ext4_ext_tree_init(handle_t *handle, struct inode *inode) | |||
841 | } | 855 | } |
842 | 856 | ||
843 | struct ext4_ext_path * | 857 | struct ext4_ext_path * |
844 | ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, | 858 | ext4_find_extent(struct inode *inode, ext4_lblk_t block, |
845 | struct ext4_ext_path *path, int flags) | 859 | struct ext4_ext_path **orig_path, int flags) |
846 | { | 860 | { |
847 | struct ext4_extent_header *eh; | 861 | struct ext4_extent_header *eh; |
848 | struct buffer_head *bh; | 862 | struct buffer_head *bh; |
849 | short int depth, i, ppos = 0, alloc = 0; | 863 | struct ext4_ext_path *path = orig_path ? *orig_path : NULL; |
864 | short int depth, i, ppos = 0; | ||
850 | int ret; | 865 | int ret; |
851 | 866 | ||
852 | eh = ext_inode_hdr(inode); | 867 | eh = ext_inode_hdr(inode); |
853 | depth = ext_depth(inode); | 868 | depth = ext_depth(inode); |
854 | 869 | ||
855 | /* account possible depth increase */ | 870 | if (path) { |
871 | ext4_ext_drop_refs(path); | ||
872 | if (depth > path[0].p_maxdepth) { | ||
873 | kfree(path); | ||
874 | *orig_path = path = NULL; | ||
875 | } | ||
876 | } | ||
856 | if (!path) { | 877 | if (!path) { |
878 | /* account possible depth increase */ | ||
857 | path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2), | 879 | path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2), |
858 | GFP_NOFS); | 880 | GFP_NOFS); |
859 | if (!path) | 881 | if (unlikely(!path)) |
860 | return ERR_PTR(-ENOMEM); | 882 | return ERR_PTR(-ENOMEM); |
861 | alloc = 1; | 883 | path[0].p_maxdepth = depth + 1; |
862 | } | 884 | } |
863 | path[0].p_hdr = eh; | 885 | path[0].p_hdr = eh; |
864 | path[0].p_bh = NULL; | 886 | path[0].p_bh = NULL; |
@@ -876,7 +898,7 @@ ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, | |||
876 | 898 | ||
877 | bh = read_extent_tree_block(inode, path[ppos].p_block, --i, | 899 | bh = read_extent_tree_block(inode, path[ppos].p_block, --i, |
878 | flags); | 900 | flags); |
879 | if (IS_ERR(bh)) { | 901 | if (unlikely(IS_ERR(bh))) { |
880 | ret = PTR_ERR(bh); | 902 | ret = PTR_ERR(bh); |
881 | goto err; | 903 | goto err; |
882 | } | 904 | } |
@@ -910,8 +932,9 @@ ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, | |||
910 | 932 | ||
911 | err: | 933 | err: |
912 | ext4_ext_drop_refs(path); | 934 | ext4_ext_drop_refs(path); |
913 | if (alloc) | 935 | kfree(path); |
914 | kfree(path); | 936 | if (orig_path) |
937 | *orig_path = NULL; | ||
915 | return ERR_PTR(ret); | 938 | return ERR_PTR(ret); |
916 | } | 939 | } |
917 | 940 | ||
@@ -1238,16 +1261,24 @@ cleanup: | |||
1238 | * just created block | 1261 | * just created block |
1239 | */ | 1262 | */ |
1240 | static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, | 1263 | static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, |
1241 | unsigned int flags, | 1264 | unsigned int flags) |
1242 | struct ext4_extent *newext) | ||
1243 | { | 1265 | { |
1244 | struct ext4_extent_header *neh; | 1266 | struct ext4_extent_header *neh; |
1245 | struct buffer_head *bh; | 1267 | struct buffer_head *bh; |
1246 | ext4_fsblk_t newblock; | 1268 | ext4_fsblk_t newblock, goal = 0; |
1269 | struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; | ||
1247 | int err = 0; | 1270 | int err = 0; |
1248 | 1271 | ||
1249 | newblock = ext4_ext_new_meta_block(handle, inode, NULL, | 1272 | /* Try to prepend new index to old one */ |
1250 | newext, &err, flags); | 1273 | if (ext_depth(inode)) |
1274 | goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode))); | ||
1275 | if (goal > le32_to_cpu(es->s_first_data_block)) { | ||
1276 | flags |= EXT4_MB_HINT_TRY_GOAL; | ||
1277 | goal--; | ||
1278 | } else | ||
1279 | goal = ext4_inode_to_goal_block(inode); | ||
1280 | newblock = ext4_new_meta_blocks(handle, inode, goal, flags, | ||
1281 | NULL, &err); | ||
1251 | if (newblock == 0) | 1282 | if (newblock == 0) |
1252 | return err; | 1283 | return err; |
1253 | 1284 | ||
@@ -1314,9 +1345,10 @@ out: | |||
1314 | static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, | 1345 | static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, |
1315 | unsigned int mb_flags, | 1346 | unsigned int mb_flags, |
1316 | unsigned int gb_flags, | 1347 | unsigned int gb_flags, |
1317 | struct ext4_ext_path *path, | 1348 | struct ext4_ext_path **ppath, |
1318 | struct ext4_extent *newext) | 1349 | struct ext4_extent *newext) |
1319 | { | 1350 | { |
1351 | struct ext4_ext_path *path = *ppath; | ||
1320 | struct ext4_ext_path *curp; | 1352 | struct ext4_ext_path *curp; |
1321 | int depth, i, err = 0; | 1353 | int depth, i, err = 0; |
1322 | 1354 | ||
@@ -1340,23 +1372,21 @@ repeat: | |||
1340 | goto out; | 1372 | goto out; |
1341 | 1373 | ||
1342 | /* refill path */ | 1374 | /* refill path */ |
1343 | ext4_ext_drop_refs(path); | 1375 | path = ext4_find_extent(inode, |
1344 | path = ext4_ext_find_extent(inode, | ||
1345 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), | 1376 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
1346 | path, gb_flags); | 1377 | ppath, gb_flags); |
1347 | if (IS_ERR(path)) | 1378 | if (IS_ERR(path)) |
1348 | err = PTR_ERR(path); | 1379 | err = PTR_ERR(path); |
1349 | } else { | 1380 | } else { |
1350 | /* tree is full, time to grow in depth */ | 1381 | /* tree is full, time to grow in depth */ |
1351 | err = ext4_ext_grow_indepth(handle, inode, mb_flags, newext); | 1382 | err = ext4_ext_grow_indepth(handle, inode, mb_flags); |
1352 | if (err) | 1383 | if (err) |
1353 | goto out; | 1384 | goto out; |
1354 | 1385 | ||
1355 | /* refill path */ | 1386 | /* refill path */ |
1356 | ext4_ext_drop_refs(path); | 1387 | path = ext4_find_extent(inode, |
1357 | path = ext4_ext_find_extent(inode, | ||
1358 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), | 1388 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
1359 | path, gb_flags); | 1389 | ppath, gb_flags); |
1360 | if (IS_ERR(path)) { | 1390 | if (IS_ERR(path)) { |
1361 | err = PTR_ERR(path); | 1391 | err = PTR_ERR(path); |
1362 | goto out; | 1392 | goto out; |
@@ -1559,7 +1589,7 @@ found_extent: | |||
1559 | * allocated block. Thus, index entries have to be consistent | 1589 | * allocated block. Thus, index entries have to be consistent |
1560 | * with leaves. | 1590 | * with leaves. |
1561 | */ | 1591 | */ |
1562 | static ext4_lblk_t | 1592 | ext4_lblk_t |
1563 | ext4_ext_next_allocated_block(struct ext4_ext_path *path) | 1593 | ext4_ext_next_allocated_block(struct ext4_ext_path *path) |
1564 | { | 1594 | { |
1565 | int depth; | 1595 | int depth; |
@@ -1802,6 +1832,7 @@ static void ext4_ext_try_to_merge_up(handle_t *handle, | |||
1802 | sizeof(struct ext4_extent_idx); | 1832 | sizeof(struct ext4_extent_idx); |
1803 | s += sizeof(struct ext4_extent_header); | 1833 | s += sizeof(struct ext4_extent_header); |
1804 | 1834 | ||
1835 | path[1].p_maxdepth = path[0].p_maxdepth; | ||
1805 | memcpy(path[0].p_hdr, path[1].p_hdr, s); | 1836 | memcpy(path[0].p_hdr, path[1].p_hdr, s); |
1806 | path[0].p_depth = 0; | 1837 | path[0].p_depth = 0; |
1807 | path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) + | 1838 | path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) + |
@@ -1896,9 +1927,10 @@ out: | |||
1896 | * creating new leaf in the no-space case. | 1927 | * creating new leaf in the no-space case. |
1897 | */ | 1928 | */ |
1898 | int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, | 1929 | int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, |
1899 | struct ext4_ext_path *path, | 1930 | struct ext4_ext_path **ppath, |
1900 | struct ext4_extent *newext, int gb_flags) | 1931 | struct ext4_extent *newext, int gb_flags) |
1901 | { | 1932 | { |
1933 | struct ext4_ext_path *path = *ppath; | ||
1902 | struct ext4_extent_header *eh; | 1934 | struct ext4_extent_header *eh; |
1903 | struct ext4_extent *ex, *fex; | 1935 | struct ext4_extent *ex, *fex; |
1904 | struct ext4_extent *nearex; /* nearest extent */ | 1936 | struct ext4_extent *nearex; /* nearest extent */ |
@@ -1907,6 +1939,8 @@ int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, | |||
1907 | ext4_lblk_t next; | 1939 | ext4_lblk_t next; |
1908 | int mb_flags = 0, unwritten; | 1940 | int mb_flags = 0, unwritten; |
1909 | 1941 | ||
1942 | if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) | ||
1943 | mb_flags |= EXT4_MB_DELALLOC_RESERVED; | ||
1910 | if (unlikely(ext4_ext_get_actual_len(newext) == 0)) { | 1944 | if (unlikely(ext4_ext_get_actual_len(newext) == 0)) { |
1911 | EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0"); | 1945 | EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0"); |
1912 | return -EIO; | 1946 | return -EIO; |
@@ -1925,7 +1959,7 @@ int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, | |||
1925 | /* | 1959 | /* |
1926 | * Try to see whether we should rather test the extent on | 1960 | * Try to see whether we should rather test the extent on |
1927 | * right from ex, or from the left of ex. This is because | 1961 | * right from ex, or from the left of ex. This is because |
1928 | * ext4_ext_find_extent() can return either extent on the | 1962 | * ext4_find_extent() can return either extent on the |
1929 | * left, or on the right from the searched position. This | 1963 | * left, or on the right from the searched position. This |
1930 | * will make merging more effective. | 1964 | * will make merging more effective. |
1931 | */ | 1965 | */ |
@@ -2008,7 +2042,7 @@ prepend: | |||
2008 | if (next != EXT_MAX_BLOCKS) { | 2042 | if (next != EXT_MAX_BLOCKS) { |
2009 | ext_debug("next leaf block - %u\n", next); | 2043 | ext_debug("next leaf block - %u\n", next); |
2010 | BUG_ON(npath != NULL); | 2044 | BUG_ON(npath != NULL); |
2011 | npath = ext4_ext_find_extent(inode, next, NULL, 0); | 2045 | npath = ext4_find_extent(inode, next, NULL, 0); |
2012 | if (IS_ERR(npath)) | 2046 | if (IS_ERR(npath)) |
2013 | return PTR_ERR(npath); | 2047 | return PTR_ERR(npath); |
2014 | BUG_ON(npath->p_depth != path->p_depth); | 2048 | BUG_ON(npath->p_depth != path->p_depth); |
@@ -2028,9 +2062,9 @@ prepend: | |||
2028 | * We're gonna add a new leaf in the tree. | 2062 | * We're gonna add a new leaf in the tree. |
2029 | */ | 2063 | */ |
2030 | if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL) | 2064 | if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL) |
2031 | mb_flags = EXT4_MB_USE_RESERVED; | 2065 | mb_flags |= EXT4_MB_USE_RESERVED; |
2032 | err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags, | 2066 | err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags, |
2033 | path, newext); | 2067 | ppath, newext); |
2034 | if (err) | 2068 | if (err) |
2035 | goto cleanup; | 2069 | goto cleanup; |
2036 | depth = ext_depth(inode); | 2070 | depth = ext_depth(inode); |
@@ -2108,10 +2142,8 @@ merge: | |||
2108 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); | 2142 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
2109 | 2143 | ||
2110 | cleanup: | 2144 | cleanup: |
2111 | if (npath) { | 2145 | ext4_ext_drop_refs(npath); |
2112 | ext4_ext_drop_refs(npath); | 2146 | kfree(npath); |
2113 | kfree(npath); | ||
2114 | } | ||
2115 | return err; | 2147 | return err; |
2116 | } | 2148 | } |
2117 | 2149 | ||
@@ -2133,13 +2165,7 @@ static int ext4_fill_fiemap_extents(struct inode *inode, | |||
2133 | /* find extent for this block */ | 2165 | /* find extent for this block */ |
2134 | down_read(&EXT4_I(inode)->i_data_sem); | 2166 | down_read(&EXT4_I(inode)->i_data_sem); |
2135 | 2167 | ||
2136 | if (path && ext_depth(inode) != depth) { | 2168 | path = ext4_find_extent(inode, block, &path, 0); |
2137 | /* depth was changed. we have to realloc path */ | ||
2138 | kfree(path); | ||
2139 | path = NULL; | ||
2140 | } | ||
2141 | |||
2142 | path = ext4_ext_find_extent(inode, block, path, 0); | ||
2143 | if (IS_ERR(path)) { | 2169 | if (IS_ERR(path)) { |
2144 | up_read(&EXT4_I(inode)->i_data_sem); | 2170 | up_read(&EXT4_I(inode)->i_data_sem); |
2145 | err = PTR_ERR(path); | 2171 | err = PTR_ERR(path); |
@@ -2156,7 +2182,6 @@ static int ext4_fill_fiemap_extents(struct inode *inode, | |||
2156 | } | 2182 | } |
2157 | ex = path[depth].p_ext; | 2183 | ex = path[depth].p_ext; |
2158 | next = ext4_ext_next_allocated_block(path); | 2184 | next = ext4_ext_next_allocated_block(path); |
2159 | ext4_ext_drop_refs(path); | ||
2160 | 2185 | ||
2161 | flags = 0; | 2186 | flags = 0; |
2162 | exists = 0; | 2187 | exists = 0; |
@@ -2266,11 +2291,8 @@ static int ext4_fill_fiemap_extents(struct inode *inode, | |||
2266 | block = es.es_lblk + es.es_len; | 2291 | block = es.es_lblk + es.es_len; |
2267 | } | 2292 | } |
2268 | 2293 | ||
2269 | if (path) { | 2294 | ext4_ext_drop_refs(path); |
2270 | ext4_ext_drop_refs(path); | 2295 | kfree(path); |
2271 | kfree(path); | ||
2272 | } | ||
2273 | |||
2274 | return err; | 2296 | return err; |
2275 | } | 2297 | } |
2276 | 2298 | ||
@@ -2826,7 +2848,7 @@ again: | |||
2826 | ext4_lblk_t ee_block; | 2848 | ext4_lblk_t ee_block; |
2827 | 2849 | ||
2828 | /* find extent for this block */ | 2850 | /* find extent for this block */ |
2829 | path = ext4_ext_find_extent(inode, end, NULL, EXT4_EX_NOCACHE); | 2851 | path = ext4_find_extent(inode, end, NULL, EXT4_EX_NOCACHE); |
2830 | if (IS_ERR(path)) { | 2852 | if (IS_ERR(path)) { |
2831 | ext4_journal_stop(handle); | 2853 | ext4_journal_stop(handle); |
2832 | return PTR_ERR(path); | 2854 | return PTR_ERR(path); |
@@ -2854,24 +2876,14 @@ again: | |||
2854 | */ | 2876 | */ |
2855 | if (end >= ee_block && | 2877 | if (end >= ee_block && |
2856 | end < ee_block + ext4_ext_get_actual_len(ex) - 1) { | 2878 | end < ee_block + ext4_ext_get_actual_len(ex) - 1) { |
2857 | int split_flag = 0; | ||
2858 | |||
2859 | if (ext4_ext_is_unwritten(ex)) | ||
2860 | split_flag = EXT4_EXT_MARK_UNWRIT1 | | ||
2861 | EXT4_EXT_MARK_UNWRIT2; | ||
2862 | |||
2863 | /* | 2879 | /* |
2864 | * Split the extent in two so that 'end' is the last | 2880 | * Split the extent in two so that 'end' is the last |
2865 | * block in the first new extent. Also we should not | 2881 | * block in the first new extent. Also we should not |
2866 | * fail removing space due to ENOSPC so try to use | 2882 | * fail removing space due to ENOSPC so try to use |
2867 | * reserved block if that happens. | 2883 | * reserved block if that happens. |
2868 | */ | 2884 | */ |
2869 | err = ext4_split_extent_at(handle, inode, path, | 2885 | err = ext4_force_split_extent_at(handle, inode, &path, |
2870 | end + 1, split_flag, | 2886 | end + 1, 1); |
2871 | EXT4_EX_NOCACHE | | ||
2872 | EXT4_GET_BLOCKS_PRE_IO | | ||
2873 | EXT4_GET_BLOCKS_METADATA_NOFAIL); | ||
2874 | |||
2875 | if (err < 0) | 2887 | if (err < 0) |
2876 | goto out; | 2888 | goto out; |
2877 | } | 2889 | } |
@@ -2893,7 +2905,7 @@ again: | |||
2893 | ext4_journal_stop(handle); | 2905 | ext4_journal_stop(handle); |
2894 | return -ENOMEM; | 2906 | return -ENOMEM; |
2895 | } | 2907 | } |
2896 | path[0].p_depth = depth; | 2908 | path[0].p_maxdepth = path[0].p_depth = depth; |
2897 | path[0].p_hdr = ext_inode_hdr(inode); | 2909 | path[0].p_hdr = ext_inode_hdr(inode); |
2898 | i = 0; | 2910 | i = 0; |
2899 | 2911 | ||
@@ -3013,10 +3025,9 @@ again: | |||
3013 | out: | 3025 | out: |
3014 | ext4_ext_drop_refs(path); | 3026 | ext4_ext_drop_refs(path); |
3015 | kfree(path); | 3027 | kfree(path); |
3016 | if (err == -EAGAIN) { | 3028 | path = NULL; |
3017 | path = NULL; | 3029 | if (err == -EAGAIN) |
3018 | goto again; | 3030 | goto again; |
3019 | } | ||
3020 | ext4_journal_stop(handle); | 3031 | ext4_journal_stop(handle); |
3021 | 3032 | ||
3022 | return err; | 3033 | return err; |
@@ -3130,11 +3141,12 @@ static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) | |||
3130 | */ | 3141 | */ |
3131 | static int ext4_split_extent_at(handle_t *handle, | 3142 | static int ext4_split_extent_at(handle_t *handle, |
3132 | struct inode *inode, | 3143 | struct inode *inode, |
3133 | struct ext4_ext_path *path, | 3144 | struct ext4_ext_path **ppath, |
3134 | ext4_lblk_t split, | 3145 | ext4_lblk_t split, |
3135 | int split_flag, | 3146 | int split_flag, |
3136 | int flags) | 3147 | int flags) |
3137 | { | 3148 | { |
3149 | struct ext4_ext_path *path = *ppath; | ||
3138 | ext4_fsblk_t newblock; | 3150 | ext4_fsblk_t newblock; |
3139 | ext4_lblk_t ee_block; | 3151 | ext4_lblk_t ee_block; |
3140 | struct ext4_extent *ex, newex, orig_ex, zero_ex; | 3152 | struct ext4_extent *ex, newex, orig_ex, zero_ex; |
@@ -3205,7 +3217,7 @@ static int ext4_split_extent_at(handle_t *handle, | |||
3205 | if (split_flag & EXT4_EXT_MARK_UNWRIT2) | 3217 | if (split_flag & EXT4_EXT_MARK_UNWRIT2) |
3206 | ext4_ext_mark_unwritten(ex2); | 3218 | ext4_ext_mark_unwritten(ex2); |
3207 | 3219 | ||
3208 | err = ext4_ext_insert_extent(handle, inode, path, &newex, flags); | 3220 | err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags); |
3209 | if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) { | 3221 | if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) { |
3210 | if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) { | 3222 | if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) { |
3211 | if (split_flag & EXT4_EXT_DATA_VALID1) { | 3223 | if (split_flag & EXT4_EXT_DATA_VALID1) { |
@@ -3271,11 +3283,12 @@ fix_extent_len: | |||
3271 | */ | 3283 | */ |
3272 | static int ext4_split_extent(handle_t *handle, | 3284 | static int ext4_split_extent(handle_t *handle, |
3273 | struct inode *inode, | 3285 | struct inode *inode, |
3274 | struct ext4_ext_path *path, | 3286 | struct ext4_ext_path **ppath, |
3275 | struct ext4_map_blocks *map, | 3287 | struct ext4_map_blocks *map, |
3276 | int split_flag, | 3288 | int split_flag, |
3277 | int flags) | 3289 | int flags) |
3278 | { | 3290 | { |
3291 | struct ext4_ext_path *path = *ppath; | ||
3279 | ext4_lblk_t ee_block; | 3292 | ext4_lblk_t ee_block; |
3280 | struct ext4_extent *ex; | 3293 | struct ext4_extent *ex; |
3281 | unsigned int ee_len, depth; | 3294 | unsigned int ee_len, depth; |
@@ -3298,7 +3311,7 @@ static int ext4_split_extent(handle_t *handle, | |||
3298 | EXT4_EXT_MARK_UNWRIT2; | 3311 | EXT4_EXT_MARK_UNWRIT2; |
3299 | if (split_flag & EXT4_EXT_DATA_VALID2) | 3312 | if (split_flag & EXT4_EXT_DATA_VALID2) |
3300 | split_flag1 |= EXT4_EXT_DATA_VALID1; | 3313 | split_flag1 |= EXT4_EXT_DATA_VALID1; |
3301 | err = ext4_split_extent_at(handle, inode, path, | 3314 | err = ext4_split_extent_at(handle, inode, ppath, |
3302 | map->m_lblk + map->m_len, split_flag1, flags1); | 3315 | map->m_lblk + map->m_len, split_flag1, flags1); |
3303 | if (err) | 3316 | if (err) |
3304 | goto out; | 3317 | goto out; |
@@ -3309,8 +3322,7 @@ static int ext4_split_extent(handle_t *handle, | |||
3309 | * Update path is required because previous ext4_split_extent_at() may | 3322 | * Update path is required because previous ext4_split_extent_at() may |
3310 | * result in split of original leaf or extent zeroout. | 3323 | * result in split of original leaf or extent zeroout. |
3311 | */ | 3324 | */ |
3312 | ext4_ext_drop_refs(path); | 3325 | path = ext4_find_extent(inode, map->m_lblk, ppath, 0); |
3313 | path = ext4_ext_find_extent(inode, map->m_lblk, path, 0); | ||
3314 | if (IS_ERR(path)) | 3326 | if (IS_ERR(path)) |
3315 | return PTR_ERR(path); | 3327 | return PTR_ERR(path); |
3316 | depth = ext_depth(inode); | 3328 | depth = ext_depth(inode); |
@@ -3330,7 +3342,7 @@ static int ext4_split_extent(handle_t *handle, | |||
3330 | split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT | | 3342 | split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT | |
3331 | EXT4_EXT_MARK_UNWRIT2); | 3343 | EXT4_EXT_MARK_UNWRIT2); |
3332 | } | 3344 | } |
3333 | err = ext4_split_extent_at(handle, inode, path, | 3345 | err = ext4_split_extent_at(handle, inode, ppath, |
3334 | map->m_lblk, split_flag1, flags); | 3346 | map->m_lblk, split_flag1, flags); |
3335 | if (err) | 3347 | if (err) |
3336 | goto out; | 3348 | goto out; |
@@ -3364,9 +3376,10 @@ out: | |||
3364 | static int ext4_ext_convert_to_initialized(handle_t *handle, | 3376 | static int ext4_ext_convert_to_initialized(handle_t *handle, |
3365 | struct inode *inode, | 3377 | struct inode *inode, |
3366 | struct ext4_map_blocks *map, | 3378 | struct ext4_map_blocks *map, |
3367 | struct ext4_ext_path *path, | 3379 | struct ext4_ext_path **ppath, |
3368 | int flags) | 3380 | int flags) |
3369 | { | 3381 | { |
3382 | struct ext4_ext_path *path = *ppath; | ||
3370 | struct ext4_sb_info *sbi; | 3383 | struct ext4_sb_info *sbi; |
3371 | struct ext4_extent_header *eh; | 3384 | struct ext4_extent_header *eh; |
3372 | struct ext4_map_blocks split_map; | 3385 | struct ext4_map_blocks split_map; |
@@ -3590,7 +3603,7 @@ static int ext4_ext_convert_to_initialized(handle_t *handle, | |||
3590 | } | 3603 | } |
3591 | } | 3604 | } |
3592 | 3605 | ||
3593 | allocated = ext4_split_extent(handle, inode, path, | 3606 | allocated = ext4_split_extent(handle, inode, ppath, |
3594 | &split_map, split_flag, flags); | 3607 | &split_map, split_flag, flags); |
3595 | if (allocated < 0) | 3608 | if (allocated < 0) |
3596 | err = allocated; | 3609 | err = allocated; |
@@ -3629,9 +3642,10 @@ out: | |||
3629 | static int ext4_split_convert_extents(handle_t *handle, | 3642 | static int ext4_split_convert_extents(handle_t *handle, |
3630 | struct inode *inode, | 3643 | struct inode *inode, |
3631 | struct ext4_map_blocks *map, | 3644 | struct ext4_map_blocks *map, |
3632 | struct ext4_ext_path *path, | 3645 | struct ext4_ext_path **ppath, |
3633 | int flags) | 3646 | int flags) |
3634 | { | 3647 | { |
3648 | struct ext4_ext_path *path = *ppath; | ||
3635 | ext4_lblk_t eof_block; | 3649 | ext4_lblk_t eof_block; |
3636 | ext4_lblk_t ee_block; | 3650 | ext4_lblk_t ee_block; |
3637 | struct ext4_extent *ex; | 3651 | struct ext4_extent *ex; |
@@ -3665,74 +3679,15 @@ static int ext4_split_convert_extents(handle_t *handle, | |||
3665 | split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2); | 3679 | split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2); |
3666 | } | 3680 | } |
3667 | flags |= EXT4_GET_BLOCKS_PRE_IO; | 3681 | flags |= EXT4_GET_BLOCKS_PRE_IO; |
3668 | return ext4_split_extent(handle, inode, path, map, split_flag, flags); | 3682 | return ext4_split_extent(handle, inode, ppath, map, split_flag, flags); |
3669 | } | ||
3670 | |||
3671 | static int ext4_convert_initialized_extents(handle_t *handle, | ||
3672 | struct inode *inode, | ||
3673 | struct ext4_map_blocks *map, | ||
3674 | struct ext4_ext_path *path) | ||
3675 | { | ||
3676 | struct ext4_extent *ex; | ||
3677 | ext4_lblk_t ee_block; | ||
3678 | unsigned int ee_len; | ||
3679 | int depth; | ||
3680 | int err = 0; | ||
3681 | |||
3682 | depth = ext_depth(inode); | ||
3683 | ex = path[depth].p_ext; | ||
3684 | ee_block = le32_to_cpu(ex->ee_block); | ||
3685 | ee_len = ext4_ext_get_actual_len(ex); | ||
3686 | |||
3687 | ext_debug("%s: inode %lu, logical" | ||
3688 | "block %llu, max_blocks %u\n", __func__, inode->i_ino, | ||
3689 | (unsigned long long)ee_block, ee_len); | ||
3690 | |||
3691 | if (ee_block != map->m_lblk || ee_len > map->m_len) { | ||
3692 | err = ext4_split_convert_extents(handle, inode, map, path, | ||
3693 | EXT4_GET_BLOCKS_CONVERT_UNWRITTEN); | ||
3694 | if (err < 0) | ||
3695 | goto out; | ||
3696 | ext4_ext_drop_refs(path); | ||
3697 | path = ext4_ext_find_extent(inode, map->m_lblk, path, 0); | ||
3698 | if (IS_ERR(path)) { | ||
3699 | err = PTR_ERR(path); | ||
3700 | goto out; | ||
3701 | } | ||
3702 | depth = ext_depth(inode); | ||
3703 | ex = path[depth].p_ext; | ||
3704 | if (!ex) { | ||
3705 | EXT4_ERROR_INODE(inode, "unexpected hole at %lu", | ||
3706 | (unsigned long) map->m_lblk); | ||
3707 | err = -EIO; | ||
3708 | goto out; | ||
3709 | } | ||
3710 | } | ||
3711 | |||
3712 | err = ext4_ext_get_access(handle, inode, path + depth); | ||
3713 | if (err) | ||
3714 | goto out; | ||
3715 | /* first mark the extent as unwritten */ | ||
3716 | ext4_ext_mark_unwritten(ex); | ||
3717 | |||
3718 | /* note: ext4_ext_correct_indexes() isn't needed here because | ||
3719 | * borders are not changed | ||
3720 | */ | ||
3721 | ext4_ext_try_to_merge(handle, inode, path, ex); | ||
3722 | |||
3723 | /* Mark modified extent as dirty */ | ||
3724 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); | ||
3725 | out: | ||
3726 | ext4_ext_show_leaf(inode, path); | ||
3727 | return err; | ||
3728 | } | 3683 | } |
3729 | 3684 | ||
3730 | |||
3731 | static int ext4_convert_unwritten_extents_endio(handle_t *handle, | 3685 | static int ext4_convert_unwritten_extents_endio(handle_t *handle, |
3732 | struct inode *inode, | 3686 | struct inode *inode, |
3733 | struct ext4_map_blocks *map, | 3687 | struct ext4_map_blocks *map, |
3734 | struct ext4_ext_path *path) | 3688 | struct ext4_ext_path **ppath) |
3735 | { | 3689 | { |
3690 | struct ext4_ext_path *path = *ppath; | ||
3736 | struct ext4_extent *ex; | 3691 | struct ext4_extent *ex; |
3737 | ext4_lblk_t ee_block; | 3692 | ext4_lblk_t ee_block; |
3738 | unsigned int ee_len; | 3693 | unsigned int ee_len; |
@@ -3761,16 +3716,13 @@ static int ext4_convert_unwritten_extents_endio(handle_t *handle, | |||
3761 | inode->i_ino, (unsigned long long)ee_block, ee_len, | 3716 | inode->i_ino, (unsigned long long)ee_block, ee_len, |
3762 | (unsigned long long)map->m_lblk, map->m_len); | 3717 | (unsigned long long)map->m_lblk, map->m_len); |
3763 | #endif | 3718 | #endif |
3764 | err = ext4_split_convert_extents(handle, inode, map, path, | 3719 | err = ext4_split_convert_extents(handle, inode, map, ppath, |
3765 | EXT4_GET_BLOCKS_CONVERT); | 3720 | EXT4_GET_BLOCKS_CONVERT); |
3766 | if (err < 0) | 3721 | if (err < 0) |
3767 | goto out; | 3722 | return err; |
3768 | ext4_ext_drop_refs(path); | 3723 | path = ext4_find_extent(inode, map->m_lblk, ppath, 0); |
3769 | path = ext4_ext_find_extent(inode, map->m_lblk, path, 0); | 3724 | if (IS_ERR(path)) |
3770 | if (IS_ERR(path)) { | 3725 | return PTR_ERR(path); |
3771 | err = PTR_ERR(path); | ||
3772 | goto out; | ||
3773 | } | ||
3774 | depth = ext_depth(inode); | 3726 | depth = ext_depth(inode); |
3775 | ex = path[depth].p_ext; | 3727 | ex = path[depth].p_ext; |
3776 | } | 3728 | } |
@@ -3963,12 +3915,16 @@ get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start, | |||
3963 | } | 3915 | } |
3964 | 3916 | ||
3965 | static int | 3917 | static int |
3966 | ext4_ext_convert_initialized_extent(handle_t *handle, struct inode *inode, | 3918 | convert_initialized_extent(handle_t *handle, struct inode *inode, |
3967 | struct ext4_map_blocks *map, | 3919 | struct ext4_map_blocks *map, |
3968 | struct ext4_ext_path *path, int flags, | 3920 | struct ext4_ext_path **ppath, int flags, |
3969 | unsigned int allocated, ext4_fsblk_t newblock) | 3921 | unsigned int allocated, ext4_fsblk_t newblock) |
3970 | { | 3922 | { |
3971 | int ret = 0; | 3923 | struct ext4_ext_path *path = *ppath; |
3924 | struct ext4_extent *ex; | ||
3925 | ext4_lblk_t ee_block; | ||
3926 | unsigned int ee_len; | ||
3927 | int depth; | ||
3972 | int err = 0; | 3928 | int err = 0; |
3973 | 3929 | ||
3974 | /* | 3930 | /* |
@@ -3978,28 +3934,67 @@ ext4_ext_convert_initialized_extent(handle_t *handle, struct inode *inode, | |||
3978 | if (map->m_len > EXT_UNWRITTEN_MAX_LEN) | 3934 | if (map->m_len > EXT_UNWRITTEN_MAX_LEN) |
3979 | map->m_len = EXT_UNWRITTEN_MAX_LEN / 2; | 3935 | map->m_len = EXT_UNWRITTEN_MAX_LEN / 2; |
3980 | 3936 | ||
3981 | ret = ext4_convert_initialized_extents(handle, inode, map, | 3937 | depth = ext_depth(inode); |
3982 | path); | 3938 | ex = path[depth].p_ext; |
3983 | if (ret >= 0) { | 3939 | ee_block = le32_to_cpu(ex->ee_block); |
3984 | ext4_update_inode_fsync_trans(handle, inode, 1); | 3940 | ee_len = ext4_ext_get_actual_len(ex); |
3985 | err = check_eofblocks_fl(handle, inode, map->m_lblk, | 3941 | |
3986 | path, map->m_len); | 3942 | ext_debug("%s: inode %lu, logical" |
3987 | } else | 3943 | "block %llu, max_blocks %u\n", __func__, inode->i_ino, |
3988 | err = ret; | 3944 | (unsigned long long)ee_block, ee_len); |
3945 | |||
3946 | if (ee_block != map->m_lblk || ee_len > map->m_len) { | ||
3947 | err = ext4_split_convert_extents(handle, inode, map, ppath, | ||
3948 | EXT4_GET_BLOCKS_CONVERT_UNWRITTEN); | ||
3949 | if (err < 0) | ||
3950 | return err; | ||
3951 | path = ext4_find_extent(inode, map->m_lblk, ppath, 0); | ||
3952 | if (IS_ERR(path)) | ||
3953 | return PTR_ERR(path); | ||
3954 | depth = ext_depth(inode); | ||
3955 | ex = path[depth].p_ext; | ||
3956 | if (!ex) { | ||
3957 | EXT4_ERROR_INODE(inode, "unexpected hole at %lu", | ||
3958 | (unsigned long) map->m_lblk); | ||
3959 | return -EIO; | ||
3960 | } | ||
3961 | } | ||
3962 | |||
3963 | err = ext4_ext_get_access(handle, inode, path + depth); | ||
3964 | if (err) | ||
3965 | return err; | ||
3966 | /* first mark the extent as unwritten */ | ||
3967 | ext4_ext_mark_unwritten(ex); | ||
3968 | |||
3969 | /* note: ext4_ext_correct_indexes() isn't needed here because | ||
3970 | * borders are not changed | ||
3971 | */ | ||
3972 | ext4_ext_try_to_merge(handle, inode, path, ex); | ||
3973 | |||
3974 | /* Mark modified extent as dirty */ | ||
3975 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); | ||
3976 | if (err) | ||
3977 | return err; | ||
3978 | ext4_ext_show_leaf(inode, path); | ||
3979 | |||
3980 | ext4_update_inode_fsync_trans(handle, inode, 1); | ||
3981 | err = check_eofblocks_fl(handle, inode, map->m_lblk, path, map->m_len); | ||
3982 | if (err) | ||
3983 | return err; | ||
3989 | map->m_flags |= EXT4_MAP_UNWRITTEN; | 3984 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
3990 | if (allocated > map->m_len) | 3985 | if (allocated > map->m_len) |
3991 | allocated = map->m_len; | 3986 | allocated = map->m_len; |
3992 | map->m_len = allocated; | 3987 | map->m_len = allocated; |
3993 | 3988 | return allocated; | |
3994 | return err ? err : allocated; | ||
3995 | } | 3989 | } |
3996 | 3990 | ||
3997 | static int | 3991 | static int |
3998 | ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode, | 3992 | ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode, |
3999 | struct ext4_map_blocks *map, | 3993 | struct ext4_map_blocks *map, |
4000 | struct ext4_ext_path *path, int flags, | 3994 | struct ext4_ext_path **ppath, int flags, |
4001 | unsigned int allocated, ext4_fsblk_t newblock) | 3995 | unsigned int allocated, ext4_fsblk_t newblock) |
4002 | { | 3996 | { |
3997 | struct ext4_ext_path *path = *ppath; | ||
4003 | int ret = 0; | 3998 | int ret = 0; |
4004 | int err = 0; | 3999 | int err = 0; |
4005 | ext4_io_end_t *io = ext4_inode_aio(inode); | 4000 | ext4_io_end_t *io = ext4_inode_aio(inode); |
@@ -4021,8 +4016,8 @@ ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode, | |||
4021 | 4016 | ||
4022 | /* get_block() before submit the IO, split the extent */ | 4017 | /* get_block() before submit the IO, split the extent */ |
4023 | if (flags & EXT4_GET_BLOCKS_PRE_IO) { | 4018 | if (flags & EXT4_GET_BLOCKS_PRE_IO) { |
4024 | ret = ext4_split_convert_extents(handle, inode, map, | 4019 | ret = ext4_split_convert_extents(handle, inode, map, ppath, |
4025 | path, flags | EXT4_GET_BLOCKS_CONVERT); | 4020 | flags | EXT4_GET_BLOCKS_CONVERT); |
4026 | if (ret <= 0) | 4021 | if (ret <= 0) |
4027 | goto out; | 4022 | goto out; |
4028 | /* | 4023 | /* |
@@ -4040,7 +4035,7 @@ ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode, | |||
4040 | /* IO end_io complete, convert the filled extent to written */ | 4035 | /* IO end_io complete, convert the filled extent to written */ |
4041 | if (flags & EXT4_GET_BLOCKS_CONVERT) { | 4036 | if (flags & EXT4_GET_BLOCKS_CONVERT) { |
4042 | ret = ext4_convert_unwritten_extents_endio(handle, inode, map, | 4037 | ret = ext4_convert_unwritten_extents_endio(handle, inode, map, |
4043 | path); | 4038 | ppath); |
4044 | if (ret >= 0) { | 4039 | if (ret >= 0) { |
4045 | ext4_update_inode_fsync_trans(handle, inode, 1); | 4040 | ext4_update_inode_fsync_trans(handle, inode, 1); |
4046 | err = check_eofblocks_fl(handle, inode, map->m_lblk, | 4041 | err = check_eofblocks_fl(handle, inode, map->m_lblk, |
@@ -4078,7 +4073,7 @@ ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode, | |||
4078 | } | 4073 | } |
4079 | 4074 | ||
4080 | /* buffered write, writepage time, convert*/ | 4075 | /* buffered write, writepage time, convert*/ |
4081 | ret = ext4_ext_convert_to_initialized(handle, inode, map, path, flags); | 4076 | ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags); |
4082 | if (ret >= 0) | 4077 | if (ret >= 0) |
4083 | ext4_update_inode_fsync_trans(handle, inode, 1); | 4078 | ext4_update_inode_fsync_trans(handle, inode, 1); |
4084 | out: | 4079 | out: |
@@ -4279,7 +4274,7 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, | |||
4279 | trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags); | 4274 | trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags); |
4280 | 4275 | ||
4281 | /* find extent for this block */ | 4276 | /* find extent for this block */ |
4282 | path = ext4_ext_find_extent(inode, map->m_lblk, NULL, 0); | 4277 | path = ext4_find_extent(inode, map->m_lblk, NULL, 0); |
4283 | if (IS_ERR(path)) { | 4278 | if (IS_ERR(path)) { |
4284 | err = PTR_ERR(path); | 4279 | err = PTR_ERR(path); |
4285 | path = NULL; | 4280 | path = NULL; |
@@ -4291,7 +4286,7 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, | |||
4291 | /* | 4286 | /* |
4292 | * consistent leaf must not be empty; | 4287 | * consistent leaf must not be empty; |
4293 | * this situation is possible, though, _during_ tree modification; | 4288 | * this situation is possible, though, _during_ tree modification; |
4294 | * this is why assert can't be put in ext4_ext_find_extent() | 4289 | * this is why assert can't be put in ext4_find_extent() |
4295 | */ | 4290 | */ |
4296 | if (unlikely(path[depth].p_ext == NULL && depth != 0)) { | 4291 | if (unlikely(path[depth].p_ext == NULL && depth != 0)) { |
4297 | EXT4_ERROR_INODE(inode, "bad extent address " | 4292 | EXT4_ERROR_INODE(inode, "bad extent address " |
@@ -4331,15 +4326,15 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, | |||
4331 | */ | 4326 | */ |
4332 | if ((!ext4_ext_is_unwritten(ex)) && | 4327 | if ((!ext4_ext_is_unwritten(ex)) && |
4333 | (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) { | 4328 | (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) { |
4334 | allocated = ext4_ext_convert_initialized_extent( | 4329 | allocated = convert_initialized_extent( |
4335 | handle, inode, map, path, flags, | 4330 | handle, inode, map, &path, |
4336 | allocated, newblock); | 4331 | flags, allocated, newblock); |
4337 | goto out2; | 4332 | goto out2; |
4338 | } else if (!ext4_ext_is_unwritten(ex)) | 4333 | } else if (!ext4_ext_is_unwritten(ex)) |
4339 | goto out; | 4334 | goto out; |
4340 | 4335 | ||
4341 | ret = ext4_ext_handle_unwritten_extents( | 4336 | ret = ext4_ext_handle_unwritten_extents( |
4342 | handle, inode, map, path, flags, | 4337 | handle, inode, map, &path, flags, |
4343 | allocated, newblock); | 4338 | allocated, newblock); |
4344 | if (ret < 0) | 4339 | if (ret < 0) |
4345 | err = ret; | 4340 | err = ret; |
@@ -4376,7 +4371,7 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, | |||
4376 | 4371 | ||
4377 | /* | 4372 | /* |
4378 | * If we are doing bigalloc, check to see if the extent returned | 4373 | * If we are doing bigalloc, check to see if the extent returned |
4379 | * by ext4_ext_find_extent() implies a cluster we can use. | 4374 | * by ext4_find_extent() implies a cluster we can use. |
4380 | */ | 4375 | */ |
4381 | if (cluster_offset && ex && | 4376 | if (cluster_offset && ex && |
4382 | get_implied_cluster_alloc(inode->i_sb, map, ex, path)) { | 4377 | get_implied_cluster_alloc(inode->i_sb, map, ex, path)) { |
@@ -4451,6 +4446,8 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, | |||
4451 | ar.flags = 0; | 4446 | ar.flags = 0; |
4452 | if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE) | 4447 | if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE) |
4453 | ar.flags |= EXT4_MB_HINT_NOPREALLOC; | 4448 | ar.flags |= EXT4_MB_HINT_NOPREALLOC; |
4449 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) | ||
4450 | ar.flags |= EXT4_MB_DELALLOC_RESERVED; | ||
4454 | newblock = ext4_mb_new_blocks(handle, &ar, &err); | 4451 | newblock = ext4_mb_new_blocks(handle, &ar, &err); |
4455 | if (!newblock) | 4452 | if (!newblock) |
4456 | goto out2; | 4453 | goto out2; |
@@ -4486,7 +4483,7 @@ got_allocated_blocks: | |||
4486 | err = check_eofblocks_fl(handle, inode, map->m_lblk, | 4483 | err = check_eofblocks_fl(handle, inode, map->m_lblk, |
4487 | path, ar.len); | 4484 | path, ar.len); |
4488 | if (!err) | 4485 | if (!err) |
4489 | err = ext4_ext_insert_extent(handle, inode, path, | 4486 | err = ext4_ext_insert_extent(handle, inode, &path, |
4490 | &newex, flags); | 4487 | &newex, flags); |
4491 | 4488 | ||
4492 | if (!err && set_unwritten) { | 4489 | if (!err && set_unwritten) { |
@@ -4619,10 +4616,8 @@ out: | |||
4619 | map->m_pblk = newblock; | 4616 | map->m_pblk = newblock; |
4620 | map->m_len = allocated; | 4617 | map->m_len = allocated; |
4621 | out2: | 4618 | out2: |
4622 | if (path) { | 4619 | ext4_ext_drop_refs(path); |
4623 | ext4_ext_drop_refs(path); | 4620 | kfree(path); |
4624 | kfree(path); | ||
4625 | } | ||
4626 | 4621 | ||
4627 | trace_ext4_ext_map_blocks_exit(inode, flags, map, | 4622 | trace_ext4_ext_map_blocks_exit(inode, flags, map, |
4628 | err ? err : allocated); | 4623 | err ? err : allocated); |
@@ -4799,7 +4794,8 @@ static long ext4_zero_range(struct file *file, loff_t offset, | |||
4799 | max_blocks -= lblk; | 4794 | max_blocks -= lblk; |
4800 | 4795 | ||
4801 | flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT | | 4796 | flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT | |
4802 | EXT4_GET_BLOCKS_CONVERT_UNWRITTEN; | 4797 | EXT4_GET_BLOCKS_CONVERT_UNWRITTEN | |
4798 | EXT4_EX_NOCACHE; | ||
4803 | if (mode & FALLOC_FL_KEEP_SIZE) | 4799 | if (mode & FALLOC_FL_KEEP_SIZE) |
4804 | flags |= EXT4_GET_BLOCKS_KEEP_SIZE; | 4800 | flags |= EXT4_GET_BLOCKS_KEEP_SIZE; |
4805 | 4801 | ||
@@ -4837,15 +4833,21 @@ static long ext4_zero_range(struct file *file, loff_t offset, | |||
4837 | ext4_inode_block_unlocked_dio(inode); | 4833 | ext4_inode_block_unlocked_dio(inode); |
4838 | inode_dio_wait(inode); | 4834 | inode_dio_wait(inode); |
4839 | 4835 | ||
4836 | ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, | ||
4837 | flags, mode); | ||
4838 | if (ret) | ||
4839 | goto out_dio; | ||
4840 | /* | 4840 | /* |
4841 | * Remove entire range from the extent status tree. | 4841 | * Remove entire range from the extent status tree. |
4842 | * | ||
4843 | * ext4_es_remove_extent(inode, lblk, max_blocks) is | ||
4844 | * NOT sufficient. I'm not sure why this is the case, | ||
4845 | * but let's be conservative and remove the extent | ||
4846 | * status tree for the entire inode. There should be | ||
4847 | * no outstanding delalloc extents thanks to the | ||
4848 | * filemap_write_and_wait_range() call above. | ||
4842 | */ | 4849 | */ |
4843 | ret = ext4_es_remove_extent(inode, lblk, max_blocks); | 4850 | ret = ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS); |
4844 | if (ret) | ||
4845 | goto out_dio; | ||
4846 | |||
4847 | ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, | ||
4848 | flags, mode); | ||
4849 | if (ret) | 4851 | if (ret) |
4850 | goto out_dio; | 4852 | goto out_dio; |
4851 | } | 4853 | } |
@@ -5304,36 +5306,31 @@ ext4_ext_shift_extents(struct inode *inode, handle_t *handle, | |||
5304 | struct ext4_ext_path *path; | 5306 | struct ext4_ext_path *path; |
5305 | int ret = 0, depth; | 5307 | int ret = 0, depth; |
5306 | struct ext4_extent *extent; | 5308 | struct ext4_extent *extent; |
5307 | ext4_lblk_t stop_block, current_block; | 5309 | ext4_lblk_t stop_block; |
5308 | ext4_lblk_t ex_start, ex_end; | 5310 | ext4_lblk_t ex_start, ex_end; |
5309 | 5311 | ||
5310 | /* Let path point to the last extent */ | 5312 | /* Let path point to the last extent */ |
5311 | path = ext4_ext_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL, 0); | 5313 | path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL, 0); |
5312 | if (IS_ERR(path)) | 5314 | if (IS_ERR(path)) |
5313 | return PTR_ERR(path); | 5315 | return PTR_ERR(path); |
5314 | 5316 | ||
5315 | depth = path->p_depth; | 5317 | depth = path->p_depth; |
5316 | extent = path[depth].p_ext; | 5318 | extent = path[depth].p_ext; |
5317 | if (!extent) { | 5319 | if (!extent) |
5318 | ext4_ext_drop_refs(path); | 5320 | goto out; |
5319 | kfree(path); | ||
5320 | return ret; | ||
5321 | } | ||
5322 | 5321 | ||
5323 | stop_block = le32_to_cpu(extent->ee_block) + | 5322 | stop_block = le32_to_cpu(extent->ee_block) + |
5324 | ext4_ext_get_actual_len(extent); | 5323 | ext4_ext_get_actual_len(extent); |
5325 | ext4_ext_drop_refs(path); | ||
5326 | kfree(path); | ||
5327 | 5324 | ||
5328 | /* Nothing to shift, if hole is at the end of file */ | 5325 | /* Nothing to shift, if hole is at the end of file */ |
5329 | if (start >= stop_block) | 5326 | if (start >= stop_block) |
5330 | return ret; | 5327 | goto out; |
5331 | 5328 | ||
5332 | /* | 5329 | /* |
5333 | * Don't start shifting extents until we make sure the hole is big | 5330 | * Don't start shifting extents until we make sure the hole is big |
5334 | * enough to accomodate the shift. | 5331 | * enough to accomodate the shift. |
5335 | */ | 5332 | */ |
5336 | path = ext4_ext_find_extent(inode, start - 1, NULL, 0); | 5333 | path = ext4_find_extent(inode, start - 1, &path, 0); |
5337 | if (IS_ERR(path)) | 5334 | if (IS_ERR(path)) |
5338 | return PTR_ERR(path); | 5335 | return PTR_ERR(path); |
5339 | depth = path->p_depth; | 5336 | depth = path->p_depth; |
@@ -5346,8 +5343,6 @@ ext4_ext_shift_extents(struct inode *inode, handle_t *handle, | |||
5346 | ex_start = 0; | 5343 | ex_start = 0; |
5347 | ex_end = 0; | 5344 | ex_end = 0; |
5348 | } | 5345 | } |
5349 | ext4_ext_drop_refs(path); | ||
5350 | kfree(path); | ||
5351 | 5346 | ||
5352 | if ((start == ex_start && shift > ex_start) || | 5347 | if ((start == ex_start && shift > ex_start) || |
5353 | (shift > start - ex_end)) | 5348 | (shift > start - ex_end)) |
@@ -5355,7 +5350,7 @@ ext4_ext_shift_extents(struct inode *inode, handle_t *handle, | |||
5355 | 5350 | ||
5356 | /* Its safe to start updating extents */ | 5351 | /* Its safe to start updating extents */ |
5357 | while (start < stop_block) { | 5352 | while (start < stop_block) { |
5358 | path = ext4_ext_find_extent(inode, start, NULL, 0); | 5353 | path = ext4_find_extent(inode, start, &path, 0); |
5359 | if (IS_ERR(path)) | 5354 | if (IS_ERR(path)) |
5360 | return PTR_ERR(path); | 5355 | return PTR_ERR(path); |
5361 | depth = path->p_depth; | 5356 | depth = path->p_depth; |
@@ -5365,27 +5360,23 @@ ext4_ext_shift_extents(struct inode *inode, handle_t *handle, | |||
5365 | (unsigned long) start); | 5360 | (unsigned long) start); |
5366 | return -EIO; | 5361 | return -EIO; |
5367 | } | 5362 | } |
5368 | 5363 | if (start > le32_to_cpu(extent->ee_block)) { | |
5369 | current_block = le32_to_cpu(extent->ee_block); | ||
5370 | if (start > current_block) { | ||
5371 | /* Hole, move to the next extent */ | 5364 | /* Hole, move to the next extent */ |
5372 | ret = mext_next_extent(inode, path, &extent); | 5365 | if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) { |
5373 | if (ret != 0) { | 5366 | path[depth].p_ext++; |
5374 | ext4_ext_drop_refs(path); | 5367 | } else { |
5375 | kfree(path); | 5368 | start = ext4_ext_next_allocated_block(path); |
5376 | if (ret == 1) | 5369 | continue; |
5377 | ret = 0; | ||
5378 | break; | ||
5379 | } | 5370 | } |
5380 | } | 5371 | } |
5381 | ret = ext4_ext_shift_path_extents(path, shift, inode, | 5372 | ret = ext4_ext_shift_path_extents(path, shift, inode, |
5382 | handle, &start); | 5373 | handle, &start); |
5383 | ext4_ext_drop_refs(path); | ||
5384 | kfree(path); | ||
5385 | if (ret) | 5374 | if (ret) |
5386 | break; | 5375 | break; |
5387 | } | 5376 | } |
5388 | 5377 | out: | |
5378 | ext4_ext_drop_refs(path); | ||
5379 | kfree(path); | ||
5389 | return ret; | 5380 | return ret; |
5390 | } | 5381 | } |
5391 | 5382 | ||
@@ -5508,3 +5499,199 @@ out_mutex: | |||
5508 | mutex_unlock(&inode->i_mutex); | 5499 | mutex_unlock(&inode->i_mutex); |
5509 | return ret; | 5500 | return ret; |
5510 | } | 5501 | } |
5502 | |||
5503 | /** | ||
5504 | * ext4_swap_extents - Swap extents between two inodes | ||
5505 | * | ||
5506 | * @inode1: First inode | ||
5507 | * @inode2: Second inode | ||
5508 | * @lblk1: Start block for first inode | ||
5509 | * @lblk2: Start block for second inode | ||
5510 | * @count: Number of blocks to swap | ||
5511 | * @mark_unwritten: Mark second inode's extents as unwritten after swap | ||
5512 | * @erp: Pointer to save error value | ||
5513 | * | ||
5514 | * This helper routine does exactly what is promise "swap extents". All other | ||
5515 | * stuff such as page-cache locking consistency, bh mapping consistency or | ||
5516 | * extent's data copying must be performed by caller. | ||
5517 | * Locking: | ||
5518 | * i_mutex is held for both inodes | ||
5519 | * i_data_sem is locked for write for both inodes | ||
5520 | * Assumptions: | ||
5521 | * All pages from requested range are locked for both inodes | ||
5522 | */ | ||
5523 | int | ||
5524 | ext4_swap_extents(handle_t *handle, struct inode *inode1, | ||
5525 | struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2, | ||
5526 | ext4_lblk_t count, int unwritten, int *erp) | ||
5527 | { | ||
5528 | struct ext4_ext_path *path1 = NULL; | ||
5529 | struct ext4_ext_path *path2 = NULL; | ||
5530 | int replaced_count = 0; | ||
5531 | |||
5532 | BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem)); | ||
5533 | BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem)); | ||
5534 | BUG_ON(!mutex_is_locked(&inode1->i_mutex)); | ||
5535 | BUG_ON(!mutex_is_locked(&inode1->i_mutex)); | ||
5536 | |||
5537 | *erp = ext4_es_remove_extent(inode1, lblk1, count); | ||
5538 | if (unlikely(*erp)) | ||
5539 | return 0; | ||
5540 | *erp = ext4_es_remove_extent(inode2, lblk2, count); | ||
5541 | if (unlikely(*erp)) | ||
5542 | return 0; | ||
5543 | |||
5544 | while (count) { | ||
5545 | struct ext4_extent *ex1, *ex2, tmp_ex; | ||
5546 | ext4_lblk_t e1_blk, e2_blk; | ||
5547 | int e1_len, e2_len, len; | ||
5548 | int split = 0; | ||
5549 | |||
5550 | path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE); | ||
5551 | if (unlikely(IS_ERR(path1))) { | ||
5552 | *erp = PTR_ERR(path1); | ||
5553 | path1 = NULL; | ||
5554 | finish: | ||
5555 | count = 0; | ||
5556 | goto repeat; | ||
5557 | } | ||
5558 | path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE); | ||
5559 | if (unlikely(IS_ERR(path2))) { | ||
5560 | *erp = PTR_ERR(path2); | ||
5561 | path2 = NULL; | ||
5562 | goto finish; | ||
5563 | } | ||
5564 | ex1 = path1[path1->p_depth].p_ext; | ||
5565 | ex2 = path2[path2->p_depth].p_ext; | ||
5566 | /* Do we have somthing to swap ? */ | ||
5567 | if (unlikely(!ex2 || !ex1)) | ||
5568 | goto finish; | ||
5569 | |||
5570 | e1_blk = le32_to_cpu(ex1->ee_block); | ||
5571 | e2_blk = le32_to_cpu(ex2->ee_block); | ||
5572 | e1_len = ext4_ext_get_actual_len(ex1); | ||
5573 | e2_len = ext4_ext_get_actual_len(ex2); | ||
5574 | |||
5575 | /* Hole handling */ | ||
5576 | if (!in_range(lblk1, e1_blk, e1_len) || | ||
5577 | !in_range(lblk2, e2_blk, e2_len)) { | ||
5578 | ext4_lblk_t next1, next2; | ||
5579 | |||
5580 | /* if hole after extent, then go to next extent */ | ||
5581 | next1 = ext4_ext_next_allocated_block(path1); | ||
5582 | next2 = ext4_ext_next_allocated_block(path2); | ||
5583 | /* If hole before extent, then shift to that extent */ | ||
5584 | if (e1_blk > lblk1) | ||
5585 | next1 = e1_blk; | ||
5586 | if (e2_blk > lblk2) | ||
5587 | next2 = e1_blk; | ||
5588 | /* Do we have something to swap */ | ||
5589 | if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS) | ||
5590 | goto finish; | ||
5591 | /* Move to the rightest boundary */ | ||
5592 | len = next1 - lblk1; | ||
5593 | if (len < next2 - lblk2) | ||
5594 | len = next2 - lblk2; | ||
5595 | if (len > count) | ||
5596 | len = count; | ||
5597 | lblk1 += len; | ||
5598 | lblk2 += len; | ||
5599 | count -= len; | ||
5600 | goto repeat; | ||
5601 | } | ||
5602 | |||
5603 | /* Prepare left boundary */ | ||
5604 | if (e1_blk < lblk1) { | ||
5605 | split = 1; | ||
5606 | *erp = ext4_force_split_extent_at(handle, inode1, | ||
5607 | &path1, lblk1, 0); | ||
5608 | if (unlikely(*erp)) | ||
5609 | goto finish; | ||
5610 | } | ||
5611 | if (e2_blk < lblk2) { | ||
5612 | split = 1; | ||
5613 | *erp = ext4_force_split_extent_at(handle, inode2, | ||
5614 | &path2, lblk2, 0); | ||
5615 | if (unlikely(*erp)) | ||
5616 | goto finish; | ||
5617 | } | ||
5618 | /* ext4_split_extent_at() may result in leaf extent split, | ||
5619 | * path must to be revalidated. */ | ||
5620 | if (split) | ||
5621 | goto repeat; | ||
5622 | |||
5623 | /* Prepare right boundary */ | ||
5624 | len = count; | ||
5625 | if (len > e1_blk + e1_len - lblk1) | ||
5626 | len = e1_blk + e1_len - lblk1; | ||
5627 | if (len > e2_blk + e2_len - lblk2) | ||
5628 | len = e2_blk + e2_len - lblk2; | ||
5629 | |||
5630 | if (len != e1_len) { | ||
5631 | split = 1; | ||
5632 | *erp = ext4_force_split_extent_at(handle, inode1, | ||
5633 | &path1, lblk1 + len, 0); | ||
5634 | if (unlikely(*erp)) | ||
5635 | goto finish; | ||
5636 | } | ||
5637 | if (len != e2_len) { | ||
5638 | split = 1; | ||
5639 | *erp = ext4_force_split_extent_at(handle, inode2, | ||
5640 | &path2, lblk2 + len, 0); | ||
5641 | if (*erp) | ||
5642 | goto finish; | ||
5643 | } | ||
5644 | /* ext4_split_extent_at() may result in leaf extent split, | ||
5645 | * path must to be revalidated. */ | ||
5646 | if (split) | ||
5647 | goto repeat; | ||
5648 | |||
5649 | BUG_ON(e2_len != e1_len); | ||
5650 | *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth); | ||
5651 | if (unlikely(*erp)) | ||
5652 | goto finish; | ||
5653 | *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth); | ||
5654 | if (unlikely(*erp)) | ||
5655 | goto finish; | ||
5656 | |||
5657 | /* Both extents are fully inside boundaries. Swap it now */ | ||
5658 | tmp_ex = *ex1; | ||
5659 | ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2)); | ||
5660 | ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex)); | ||
5661 | ex1->ee_len = cpu_to_le16(e2_len); | ||
5662 | ex2->ee_len = cpu_to_le16(e1_len); | ||
5663 | if (unwritten) | ||
5664 | ext4_ext_mark_unwritten(ex2); | ||
5665 | if (ext4_ext_is_unwritten(&tmp_ex)) | ||
5666 | ext4_ext_mark_unwritten(ex1); | ||
5667 | |||
5668 | ext4_ext_try_to_merge(handle, inode2, path2, ex2); | ||
5669 | ext4_ext_try_to_merge(handle, inode1, path1, ex1); | ||
5670 | *erp = ext4_ext_dirty(handle, inode2, path2 + | ||
5671 | path2->p_depth); | ||
5672 | if (unlikely(*erp)) | ||
5673 | goto finish; | ||
5674 | *erp = ext4_ext_dirty(handle, inode1, path1 + | ||
5675 | path1->p_depth); | ||
5676 | /* | ||
5677 | * Looks scarry ah..? second inode already points to new blocks, | ||
5678 | * and it was successfully dirtied. But luckily error may happen | ||
5679 | * only due to journal error, so full transaction will be | ||
5680 | * aborted anyway. | ||
5681 | */ | ||
5682 | if (unlikely(*erp)) | ||
5683 | goto finish; | ||
5684 | lblk1 += len; | ||
5685 | lblk2 += len; | ||
5686 | replaced_count += len; | ||
5687 | count -= len; | ||
5688 | |||
5689 | repeat: | ||
5690 | ext4_ext_drop_refs(path1); | ||
5691 | kfree(path1); | ||
5692 | ext4_ext_drop_refs(path2); | ||
5693 | kfree(path2); | ||
5694 | path1 = path2 = NULL; | ||
5695 | } | ||
5696 | return replaced_count; | ||
5697 | } | ||