diff options
Diffstat (limited to 'fs/ext4/extents.c')
-rw-r--r-- | fs/ext4/extents.c | 2024 |
1 files changed, 1210 insertions, 814 deletions
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index 06328d3e5717..f815cc81e7a2 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c | |||
@@ -44,54 +44,14 @@ | |||
44 | #include "ext4_jbd2.h" | 44 | #include "ext4_jbd2.h" |
45 | #include "ext4_extents.h" | 45 | #include "ext4_extents.h" |
46 | 46 | ||
47 | #include <trace/events/ext4.h> | ||
47 | 48 | ||
48 | /* | 49 | static int ext4_split_extent(handle_t *handle, |
49 | * ext_pblock: | 50 | struct inode *inode, |
50 | * combine low and high parts of physical block number into ext4_fsblk_t | 51 | struct ext4_ext_path *path, |
51 | */ | 52 | struct ext4_map_blocks *map, |
52 | ext4_fsblk_t ext_pblock(struct ext4_extent *ex) | 53 | int split_flag, |
53 | { | 54 | int flags); |
54 | ext4_fsblk_t block; | ||
55 | |||
56 | block = le32_to_cpu(ex->ee_start_lo); | ||
57 | block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1; | ||
58 | return block; | ||
59 | } | ||
60 | |||
61 | /* | ||
62 | * idx_pblock: | ||
63 | * combine low and high parts of a leaf physical block number into ext4_fsblk_t | ||
64 | */ | ||
65 | ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix) | ||
66 | { | ||
67 | ext4_fsblk_t block; | ||
68 | |||
69 | block = le32_to_cpu(ix->ei_leaf_lo); | ||
70 | block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1; | ||
71 | return block; | ||
72 | } | ||
73 | |||
74 | /* | ||
75 | * ext4_ext_store_pblock: | ||
76 | * stores a large physical block number into an extent struct, | ||
77 | * breaking it into parts | ||
78 | */ | ||
79 | void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb) | ||
80 | { | ||
81 | ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff)); | ||
82 | ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff); | ||
83 | } | ||
84 | |||
85 | /* | ||
86 | * ext4_idx_store_pblock: | ||
87 | * stores a large physical block number into an index struct, | ||
88 | * breaking it into parts | ||
89 | */ | ||
90 | static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb) | ||
91 | { | ||
92 | ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff)); | ||
93 | ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff); | ||
94 | } | ||
95 | 55 | ||
96 | static int ext4_ext_truncate_extend_restart(handle_t *handle, | 56 | static int ext4_ext_truncate_extend_restart(handle_t *handle, |
97 | struct inode *inode, | 57 | struct inode *inode, |
@@ -166,10 +126,33 @@ static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, | |||
166 | struct ext4_extent *ex; | 126 | struct ext4_extent *ex; |
167 | depth = path->p_depth; | 127 | depth = path->p_depth; |
168 | 128 | ||
169 | /* try to predict block placement */ | 129 | /* |
130 | * Try to predict block placement assuming that we are | ||
131 | * filling in a file which will eventually be | ||
132 | * non-sparse --- i.e., in the case of libbfd writing | ||
133 | * an ELF object sections out-of-order but in a way | ||
134 | * the eventually results in a contiguous object or | ||
135 | * executable file, or some database extending a table | ||
136 | * space file. However, this is actually somewhat | ||
137 | * non-ideal if we are writing a sparse file such as | ||
138 | * qemu or KVM writing a raw image file that is going | ||
139 | * to stay fairly sparse, since it will end up | ||
140 | * fragmenting the file system's free space. Maybe we | ||
141 | * should have some hueristics or some way to allow | ||
142 | * userspace to pass a hint to file system, | ||
143 | * especially if the latter case turns out to be | ||
144 | * common. | ||
145 | */ | ||
170 | ex = path[depth].p_ext; | 146 | ex = path[depth].p_ext; |
171 | if (ex) | 147 | if (ex) { |
172 | return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block)); | 148 | ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex); |
149 | ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block); | ||
150 | |||
151 | if (block > ext_block) | ||
152 | return ext_pblk + (block - ext_block); | ||
153 | else | ||
154 | return ext_pblk - (ext_block - block); | ||
155 | } | ||
173 | 156 | ||
174 | /* it looks like index is empty; | 157 | /* it looks like index is empty; |
175 | * try to find starting block from index itself */ | 158 | * try to find starting block from index itself */ |
@@ -216,12 +199,13 @@ static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, | |||
216 | static ext4_fsblk_t | 199 | static ext4_fsblk_t |
217 | ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, | 200 | ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, |
218 | struct ext4_ext_path *path, | 201 | struct ext4_ext_path *path, |
219 | struct ext4_extent *ex, int *err) | 202 | struct ext4_extent *ex, int *err, unsigned int flags) |
220 | { | 203 | { |
221 | ext4_fsblk_t goal, newblock; | 204 | ext4_fsblk_t goal, newblock; |
222 | 205 | ||
223 | goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block)); | 206 | goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block)); |
224 | newblock = ext4_new_meta_blocks(handle, inode, goal, NULL, err); | 207 | newblock = ext4_new_meta_blocks(handle, inode, goal, flags, |
208 | NULL, err); | ||
225 | return newblock; | 209 | return newblock; |
226 | } | 210 | } |
227 | 211 | ||
@@ -292,7 +276,7 @@ static inline int ext4_ext_space_root_idx(struct inode *inode, int check) | |||
292 | * to allocate @blocks | 276 | * to allocate @blocks |
293 | * Worse case is one block per extent | 277 | * Worse case is one block per extent |
294 | */ | 278 | */ |
295 | int ext4_ext_calc_metadata_amount(struct inode *inode, sector_t lblock) | 279 | int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock) |
296 | { | 280 | { |
297 | struct ext4_inode_info *ei = EXT4_I(inode); | 281 | struct ext4_inode_info *ei = EXT4_I(inode); |
298 | int idxs, num = 0; | 282 | int idxs, num = 0; |
@@ -354,7 +338,7 @@ ext4_ext_max_entries(struct inode *inode, int depth) | |||
354 | 338 | ||
355 | static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext) | 339 | static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext) |
356 | { | 340 | { |
357 | ext4_fsblk_t block = ext_pblock(ext); | 341 | ext4_fsblk_t block = ext4_ext_pblock(ext); |
358 | int len = ext4_ext_get_actual_len(ext); | 342 | int len = ext4_ext_get_actual_len(ext); |
359 | 343 | ||
360 | return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len); | 344 | return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len); |
@@ -363,7 +347,7 @@ static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext) | |||
363 | static int ext4_valid_extent_idx(struct inode *inode, | 347 | static int ext4_valid_extent_idx(struct inode *inode, |
364 | struct ext4_extent_idx *ext_idx) | 348 | struct ext4_extent_idx *ext_idx) |
365 | { | 349 | { |
366 | ext4_fsblk_t block = idx_pblock(ext_idx); | 350 | ext4_fsblk_t block = ext4_idx_pblock(ext_idx); |
367 | 351 | ||
368 | return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1); | 352 | return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1); |
369 | } | 353 | } |
@@ -463,13 +447,13 @@ static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) | |||
463 | for (k = 0; k <= l; k++, path++) { | 447 | for (k = 0; k <= l; k++, path++) { |
464 | if (path->p_idx) { | 448 | if (path->p_idx) { |
465 | ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block), | 449 | ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block), |
466 | idx_pblock(path->p_idx)); | 450 | ext4_idx_pblock(path->p_idx)); |
467 | } else if (path->p_ext) { | 451 | } else if (path->p_ext) { |
468 | ext_debug(" %d:[%d]%d:%llu ", | 452 | ext_debug(" %d:[%d]%d:%llu ", |
469 | le32_to_cpu(path->p_ext->ee_block), | 453 | le32_to_cpu(path->p_ext->ee_block), |
470 | ext4_ext_is_uninitialized(path->p_ext), | 454 | ext4_ext_is_uninitialized(path->p_ext), |
471 | ext4_ext_get_actual_len(path->p_ext), | 455 | ext4_ext_get_actual_len(path->p_ext), |
472 | ext_pblock(path->p_ext)); | 456 | ext4_ext_pblock(path->p_ext)); |
473 | } else | 457 | } else |
474 | ext_debug(" []"); | 458 | ext_debug(" []"); |
475 | } | 459 | } |
@@ -494,13 +478,47 @@ static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) | |||
494 | for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { | 478 | for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { |
495 | ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block), | 479 | ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block), |
496 | ext4_ext_is_uninitialized(ex), | 480 | ext4_ext_is_uninitialized(ex), |
497 | ext4_ext_get_actual_len(ex), ext_pblock(ex)); | 481 | ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex)); |
498 | } | 482 | } |
499 | ext_debug("\n"); | 483 | ext_debug("\n"); |
500 | } | 484 | } |
485 | |||
486 | static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path, | ||
487 | ext4_fsblk_t newblock, int level) | ||
488 | { | ||
489 | int depth = ext_depth(inode); | ||
490 | struct ext4_extent *ex; | ||
491 | |||
492 | if (depth != level) { | ||
493 | struct ext4_extent_idx *idx; | ||
494 | idx = path[level].p_idx; | ||
495 | while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) { | ||
496 | ext_debug("%d: move %d:%llu in new index %llu\n", level, | ||
497 | le32_to_cpu(idx->ei_block), | ||
498 | ext4_idx_pblock(idx), | ||
499 | newblock); | ||
500 | idx++; | ||
501 | } | ||
502 | |||
503 | return; | ||
504 | } | ||
505 | |||
506 | ex = path[depth].p_ext; | ||
507 | while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) { | ||
508 | ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n", | ||
509 | le32_to_cpu(ex->ee_block), | ||
510 | ext4_ext_pblock(ex), | ||
511 | ext4_ext_is_uninitialized(ex), | ||
512 | ext4_ext_get_actual_len(ex), | ||
513 | newblock); | ||
514 | ex++; | ||
515 | } | ||
516 | } | ||
517 | |||
501 | #else | 518 | #else |
502 | #define ext4_ext_show_path(inode, path) | 519 | #define ext4_ext_show_path(inode, path) |
503 | #define ext4_ext_show_leaf(inode, path) | 520 | #define ext4_ext_show_leaf(inode, path) |
521 | #define ext4_ext_show_move(inode, path, newblock, level) | ||
504 | #endif | 522 | #endif |
505 | 523 | ||
506 | void ext4_ext_drop_refs(struct ext4_ext_path *path) | 524 | void ext4_ext_drop_refs(struct ext4_ext_path *path) |
@@ -545,7 +563,7 @@ ext4_ext_binsearch_idx(struct inode *inode, | |||
545 | 563 | ||
546 | path->p_idx = l - 1; | 564 | path->p_idx = l - 1; |
547 | ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block), | 565 | ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block), |
548 | idx_pblock(path->p_idx)); | 566 | ext4_idx_pblock(path->p_idx)); |
549 | 567 | ||
550 | #ifdef CHECK_BINSEARCH | 568 | #ifdef CHECK_BINSEARCH |
551 | { | 569 | { |
@@ -614,7 +632,7 @@ ext4_ext_binsearch(struct inode *inode, | |||
614 | path->p_ext = l - 1; | 632 | path->p_ext = l - 1; |
615 | ext_debug(" -> %d:%llu:[%d]%d ", | 633 | ext_debug(" -> %d:%llu:[%d]%d ", |
616 | le32_to_cpu(path->p_ext->ee_block), | 634 | le32_to_cpu(path->p_ext->ee_block), |
617 | ext_pblock(path->p_ext), | 635 | ext4_ext_pblock(path->p_ext), |
618 | ext4_ext_is_uninitialized(path->p_ext), | 636 | ext4_ext_is_uninitialized(path->p_ext), |
619 | ext4_ext_get_actual_len(path->p_ext)); | 637 | ext4_ext_get_actual_len(path->p_ext)); |
620 | 638 | ||
@@ -682,7 +700,7 @@ ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, | |||
682 | ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); | 700 | ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); |
683 | 701 | ||
684 | ext4_ext_binsearch_idx(inode, path + ppos, block); | 702 | ext4_ext_binsearch_idx(inode, path + ppos, block); |
685 | path[ppos].p_block = idx_pblock(path[ppos].p_idx); | 703 | path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx); |
686 | path[ppos].p_depth = i; | 704 | path[ppos].p_depth = i; |
687 | path[ppos].p_ext = NULL; | 705 | path[ppos].p_ext = NULL; |
688 | 706 | ||
@@ -690,6 +708,8 @@ ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, | |||
690 | if (unlikely(!bh)) | 708 | if (unlikely(!bh)) |
691 | goto err; | 709 | goto err; |
692 | if (!bh_uptodate_or_lock(bh)) { | 710 | if (!bh_uptodate_or_lock(bh)) { |
711 | trace_ext4_ext_load_extent(inode, block, | ||
712 | path[ppos].p_block); | ||
693 | if (bh_submit_read(bh) < 0) { | 713 | if (bh_submit_read(bh) < 0) { |
694 | put_bh(bh); | 714 | put_bh(bh); |
695 | goto err; | 715 | goto err; |
@@ -721,7 +741,7 @@ ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, | |||
721 | ext4_ext_binsearch(inode, path + ppos, block); | 741 | ext4_ext_binsearch(inode, path + ppos, block); |
722 | /* if not an empty leaf */ | 742 | /* if not an empty leaf */ |
723 | if (path[ppos].p_ext) | 743 | if (path[ppos].p_ext) |
724 | path[ppos].p_block = ext_pblock(path[ppos].p_ext); | 744 | path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext); |
725 | 745 | ||
726 | ext4_ext_show_path(inode, path); | 746 | ext4_ext_show_path(inode, path); |
727 | 747 | ||
@@ -739,9 +759,9 @@ err: | |||
739 | * insert new index [@logical;@ptr] into the block at @curp; | 759 | * insert new index [@logical;@ptr] into the block at @curp; |
740 | * check where to insert: before @curp or after @curp | 760 | * check where to insert: before @curp or after @curp |
741 | */ | 761 | */ |
742 | int ext4_ext_insert_index(handle_t *handle, struct inode *inode, | 762 | static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, |
743 | struct ext4_ext_path *curp, | 763 | struct ext4_ext_path *curp, |
744 | int logical, ext4_fsblk_t ptr) | 764 | int logical, ext4_fsblk_t ptr) |
745 | { | 765 | { |
746 | struct ext4_extent_idx *ix; | 766 | struct ext4_extent_idx *ix; |
747 | int len, err; | 767 | int len, err; |
@@ -814,14 +834,14 @@ int ext4_ext_insert_index(handle_t *handle, struct inode *inode, | |||
814 | * - initializes subtree | 834 | * - initializes subtree |
815 | */ | 835 | */ |
816 | static int ext4_ext_split(handle_t *handle, struct inode *inode, | 836 | static int ext4_ext_split(handle_t *handle, struct inode *inode, |
817 | struct ext4_ext_path *path, | 837 | unsigned int flags, |
818 | struct ext4_extent *newext, int at) | 838 | struct ext4_ext_path *path, |
839 | struct ext4_extent *newext, int at) | ||
819 | { | 840 | { |
820 | struct buffer_head *bh = NULL; | 841 | struct buffer_head *bh = NULL; |
821 | int depth = ext_depth(inode); | 842 | int depth = ext_depth(inode); |
822 | struct ext4_extent_header *neh; | 843 | struct ext4_extent_header *neh; |
823 | struct ext4_extent_idx *fidx; | 844 | struct ext4_extent_idx *fidx; |
824 | struct ext4_extent *ex; | ||
825 | int i = at, k, m, a; | 845 | int i = at, k, m, a; |
826 | ext4_fsblk_t newblock, oldblock; | 846 | ext4_fsblk_t newblock, oldblock; |
827 | __le32 border; | 847 | __le32 border; |
@@ -869,7 +889,7 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode, | |||
869 | ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); | 889 | ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); |
870 | for (a = 0; a < depth - at; a++) { | 890 | for (a = 0; a < depth - at; a++) { |
871 | newblock = ext4_ext_new_meta_block(handle, inode, path, | 891 | newblock = ext4_ext_new_meta_block(handle, inode, path, |
872 | newext, &err); | 892 | newext, &err, flags); |
873 | if (newblock == 0) | 893 | if (newblock == 0) |
874 | goto cleanup; | 894 | goto cleanup; |
875 | ablocks[a] = newblock; | 895 | ablocks[a] = newblock; |
@@ -898,7 +918,6 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode, | |||
898 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); | 918 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); |
899 | neh->eh_magic = EXT4_EXT_MAGIC; | 919 | neh->eh_magic = EXT4_EXT_MAGIC; |
900 | neh->eh_depth = 0; | 920 | neh->eh_depth = 0; |
901 | ex = EXT_FIRST_EXTENT(neh); | ||
902 | 921 | ||
903 | /* move remainder of path[depth] to the new leaf */ | 922 | /* move remainder of path[depth] to the new leaf */ |
904 | if (unlikely(path[depth].p_hdr->eh_entries != | 923 | if (unlikely(path[depth].p_hdr->eh_entries != |
@@ -910,25 +929,12 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode, | |||
910 | goto cleanup; | 929 | goto cleanup; |
911 | } | 930 | } |
912 | /* start copy from next extent */ | 931 | /* start copy from next extent */ |
913 | /* TODO: we could do it by single memmove */ | 932 | m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++; |
914 | m = 0; | 933 | ext4_ext_show_move(inode, path, newblock, depth); |
915 | path[depth].p_ext++; | ||
916 | while (path[depth].p_ext <= | ||
917 | EXT_MAX_EXTENT(path[depth].p_hdr)) { | ||
918 | ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n", | ||
919 | le32_to_cpu(path[depth].p_ext->ee_block), | ||
920 | ext_pblock(path[depth].p_ext), | ||
921 | ext4_ext_is_uninitialized(path[depth].p_ext), | ||
922 | ext4_ext_get_actual_len(path[depth].p_ext), | ||
923 | newblock); | ||
924 | /*memmove(ex++, path[depth].p_ext++, | ||
925 | sizeof(struct ext4_extent)); | ||
926 | neh->eh_entries++;*/ | ||
927 | path[depth].p_ext++; | ||
928 | m++; | ||
929 | } | ||
930 | if (m) { | 934 | if (m) { |
931 | memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m); | 935 | struct ext4_extent *ex; |
936 | ex = EXT_FIRST_EXTENT(neh); | ||
937 | memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m); | ||
932 | le16_add_cpu(&neh->eh_entries, m); | 938 | le16_add_cpu(&neh->eh_entries, m); |
933 | } | 939 | } |
934 | 940 | ||
@@ -990,12 +996,8 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode, | |||
990 | 996 | ||
991 | ext_debug("int.index at %d (block %llu): %u -> %llu\n", | 997 | ext_debug("int.index at %d (block %llu): %u -> %llu\n", |
992 | i, newblock, le32_to_cpu(border), oldblock); | 998 | i, newblock, le32_to_cpu(border), oldblock); |
993 | /* copy indexes */ | ||
994 | m = 0; | ||
995 | path[i].p_idx++; | ||
996 | 999 | ||
997 | ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, | 1000 | /* move remainder of path[i] to the new index block */ |
998 | EXT_MAX_INDEX(path[i].p_hdr)); | ||
999 | if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) != | 1001 | if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) != |
1000 | EXT_LAST_INDEX(path[i].p_hdr))) { | 1002 | EXT_LAST_INDEX(path[i].p_hdr))) { |
1001 | EXT4_ERROR_INODE(inode, | 1003 | EXT4_ERROR_INODE(inode, |
@@ -1004,20 +1006,13 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode, | |||
1004 | err = -EIO; | 1006 | err = -EIO; |
1005 | goto cleanup; | 1007 | goto cleanup; |
1006 | } | 1008 | } |
1007 | while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) { | 1009 | /* start copy indexes */ |
1008 | ext_debug("%d: move %d:%llu in new index %llu\n", i, | 1010 | m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++; |
1009 | le32_to_cpu(path[i].p_idx->ei_block), | 1011 | ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, |
1010 | idx_pblock(path[i].p_idx), | 1012 | EXT_MAX_INDEX(path[i].p_hdr)); |
1011 | newblock); | 1013 | ext4_ext_show_move(inode, path, newblock, i); |
1012 | /*memmove(++fidx, path[i].p_idx++, | ||
1013 | sizeof(struct ext4_extent_idx)); | ||
1014 | neh->eh_entries++; | ||
1015 | BUG_ON(neh->eh_entries > neh->eh_max);*/ | ||
1016 | path[i].p_idx++; | ||
1017 | m++; | ||
1018 | } | ||
1019 | if (m) { | 1014 | if (m) { |
1020 | memmove(++fidx, path[i].p_idx - m, | 1015 | memmove(++fidx, path[i].p_idx, |
1021 | sizeof(struct ext4_extent_idx) * m); | 1016 | sizeof(struct ext4_extent_idx) * m); |
1022 | le16_add_cpu(&neh->eh_entries, m); | 1017 | le16_add_cpu(&neh->eh_entries, m); |
1023 | } | 1018 | } |
@@ -1060,7 +1055,7 @@ cleanup: | |||
1060 | for (i = 0; i < depth; i++) { | 1055 | for (i = 0; i < depth; i++) { |
1061 | if (!ablocks[i]) | 1056 | if (!ablocks[i]) |
1062 | continue; | 1057 | continue; |
1063 | ext4_free_blocks(handle, inode, 0, ablocks[i], 1, | 1058 | ext4_free_blocks(handle, inode, NULL, ablocks[i], 1, |
1064 | EXT4_FREE_BLOCKS_METADATA); | 1059 | EXT4_FREE_BLOCKS_METADATA); |
1065 | } | 1060 | } |
1066 | } | 1061 | } |
@@ -1078,8 +1073,9 @@ cleanup: | |||
1078 | * just created block | 1073 | * just created block |
1079 | */ | 1074 | */ |
1080 | static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, | 1075 | static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, |
1081 | struct ext4_ext_path *path, | 1076 | unsigned int flags, |
1082 | struct ext4_extent *newext) | 1077 | struct ext4_ext_path *path, |
1078 | struct ext4_extent *newext) | ||
1083 | { | 1079 | { |
1084 | struct ext4_ext_path *curp = path; | 1080 | struct ext4_ext_path *curp = path; |
1085 | struct ext4_extent_header *neh; | 1081 | struct ext4_extent_header *neh; |
@@ -1087,7 +1083,8 @@ static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, | |||
1087 | ext4_fsblk_t newblock; | 1083 | ext4_fsblk_t newblock; |
1088 | int err = 0; | 1084 | int err = 0; |
1089 | 1085 | ||
1090 | newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err); | 1086 | newblock = ext4_ext_new_meta_block(handle, inode, path, |
1087 | newext, &err, flags); | ||
1091 | if (newblock == 0) | 1088 | if (newblock == 0) |
1092 | return err; | 1089 | return err; |
1093 | 1090 | ||
@@ -1146,7 +1143,7 @@ static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, | |||
1146 | ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", | 1143 | ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", |
1147 | le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), | 1144 | le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), |
1148 | le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block), | 1145 | le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block), |
1149 | idx_pblock(EXT_FIRST_INDEX(neh))); | 1146 | ext4_idx_pblock(EXT_FIRST_INDEX(neh))); |
1150 | 1147 | ||
1151 | neh->eh_depth = cpu_to_le16(path->p_depth + 1); | 1148 | neh->eh_depth = cpu_to_le16(path->p_depth + 1); |
1152 | err = ext4_ext_dirty(handle, inode, curp); | 1149 | err = ext4_ext_dirty(handle, inode, curp); |
@@ -1162,8 +1159,9 @@ out: | |||
1162 | * if no free index is found, then it requests in-depth growing. | 1159 | * if no free index is found, then it requests in-depth growing. |
1163 | */ | 1160 | */ |
1164 | static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, | 1161 | static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, |
1165 | struct ext4_ext_path *path, | 1162 | unsigned int flags, |
1166 | struct ext4_extent *newext) | 1163 | struct ext4_ext_path *path, |
1164 | struct ext4_extent *newext) | ||
1167 | { | 1165 | { |
1168 | struct ext4_ext_path *curp; | 1166 | struct ext4_ext_path *curp; |
1169 | int depth, i, err = 0; | 1167 | int depth, i, err = 0; |
@@ -1183,7 +1181,7 @@ repeat: | |||
1183 | if (EXT_HAS_FREE_INDEX(curp)) { | 1181 | if (EXT_HAS_FREE_INDEX(curp)) { |
1184 | /* if we found index with free entry, then use that | 1182 | /* if we found index with free entry, then use that |
1185 | * entry: create all needed subtree and add new leaf */ | 1183 | * entry: create all needed subtree and add new leaf */ |
1186 | err = ext4_ext_split(handle, inode, path, newext, i); | 1184 | err = ext4_ext_split(handle, inode, flags, path, newext, i); |
1187 | if (err) | 1185 | if (err) |
1188 | goto out; | 1186 | goto out; |
1189 | 1187 | ||
@@ -1196,7 +1194,8 @@ repeat: | |||
1196 | err = PTR_ERR(path); | 1194 | err = PTR_ERR(path); |
1197 | } else { | 1195 | } else { |
1198 | /* tree is full, time to grow in depth */ | 1196 | /* tree is full, time to grow in depth */ |
1199 | err = ext4_ext_grow_indepth(handle, inode, path, newext); | 1197 | err = ext4_ext_grow_indepth(handle, inode, flags, |
1198 | path, newext); | ||
1200 | if (err) | 1199 | if (err) |
1201 | goto out; | 1200 | goto out; |
1202 | 1201 | ||
@@ -1232,9 +1231,9 @@ out: | |||
1232 | * returns 0 at @phys | 1231 | * returns 0 at @phys |
1233 | * return value contains 0 (success) or error code | 1232 | * return value contains 0 (success) or error code |
1234 | */ | 1233 | */ |
1235 | int | 1234 | static int ext4_ext_search_left(struct inode *inode, |
1236 | ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path, | 1235 | struct ext4_ext_path *path, |
1237 | ext4_lblk_t *logical, ext4_fsblk_t *phys) | 1236 | ext4_lblk_t *logical, ext4_fsblk_t *phys) |
1238 | { | 1237 | { |
1239 | struct ext4_extent_idx *ix; | 1238 | struct ext4_extent_idx *ix; |
1240 | struct ext4_extent *ex; | 1239 | struct ext4_extent *ex; |
@@ -1286,7 +1285,7 @@ ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path, | |||
1286 | } | 1285 | } |
1287 | 1286 | ||
1288 | *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; | 1287 | *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; |
1289 | *phys = ext_pblock(ex) + ee_len - 1; | 1288 | *phys = ext4_ext_pblock(ex) + ee_len - 1; |
1290 | return 0; | 1289 | return 0; |
1291 | } | 1290 | } |
1292 | 1291 | ||
@@ -1297,9 +1296,9 @@ ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path, | |||
1297 | * returns 0 at @phys | 1296 | * returns 0 at @phys |
1298 | * return value contains 0 (success) or error code | 1297 | * return value contains 0 (success) or error code |
1299 | */ | 1298 | */ |
1300 | int | 1299 | static int ext4_ext_search_right(struct inode *inode, |
1301 | ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path, | 1300 | struct ext4_ext_path *path, |
1302 | ext4_lblk_t *logical, ext4_fsblk_t *phys) | 1301 | ext4_lblk_t *logical, ext4_fsblk_t *phys) |
1303 | { | 1302 | { |
1304 | struct buffer_head *bh = NULL; | 1303 | struct buffer_head *bh = NULL; |
1305 | struct ext4_extent_header *eh; | 1304 | struct ext4_extent_header *eh; |
@@ -1342,7 +1341,7 @@ ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path, | |||
1342 | } | 1341 | } |
1343 | } | 1342 | } |
1344 | *logical = le32_to_cpu(ex->ee_block); | 1343 | *logical = le32_to_cpu(ex->ee_block); |
1345 | *phys = ext_pblock(ex); | 1344 | *phys = ext4_ext_pblock(ex); |
1346 | return 0; | 1345 | return 0; |
1347 | } | 1346 | } |
1348 | 1347 | ||
@@ -1357,7 +1356,7 @@ ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path, | |||
1357 | /* next allocated block in this leaf */ | 1356 | /* next allocated block in this leaf */ |
1358 | ex++; | 1357 | ex++; |
1359 | *logical = le32_to_cpu(ex->ee_block); | 1358 | *logical = le32_to_cpu(ex->ee_block); |
1360 | *phys = ext_pblock(ex); | 1359 | *phys = ext4_ext_pblock(ex); |
1361 | return 0; | 1360 | return 0; |
1362 | } | 1361 | } |
1363 | 1362 | ||
@@ -1376,7 +1375,7 @@ got_index: | |||
1376 | * follow it and find the closest allocated | 1375 | * follow it and find the closest allocated |
1377 | * block to the right */ | 1376 | * block to the right */ |
1378 | ix++; | 1377 | ix++; |
1379 | block = idx_pblock(ix); | 1378 | block = ext4_idx_pblock(ix); |
1380 | while (++depth < path->p_depth) { | 1379 | while (++depth < path->p_depth) { |
1381 | bh = sb_bread(inode->i_sb, block); | 1380 | bh = sb_bread(inode->i_sb, block); |
1382 | if (bh == NULL) | 1381 | if (bh == NULL) |
@@ -1388,7 +1387,7 @@ got_index: | |||
1388 | return -EIO; | 1387 | return -EIO; |
1389 | } | 1388 | } |
1390 | ix = EXT_FIRST_INDEX(eh); | 1389 | ix = EXT_FIRST_INDEX(eh); |
1391 | block = idx_pblock(ix); | 1390 | block = ext4_idx_pblock(ix); |
1392 | put_bh(bh); | 1391 | put_bh(bh); |
1393 | } | 1392 | } |
1394 | 1393 | ||
@@ -1402,14 +1401,14 @@ got_index: | |||
1402 | } | 1401 | } |
1403 | ex = EXT_FIRST_EXTENT(eh); | 1402 | ex = EXT_FIRST_EXTENT(eh); |
1404 | *logical = le32_to_cpu(ex->ee_block); | 1403 | *logical = le32_to_cpu(ex->ee_block); |
1405 | *phys = ext_pblock(ex); | 1404 | *phys = ext4_ext_pblock(ex); |
1406 | put_bh(bh); | 1405 | put_bh(bh); |
1407 | return 0; | 1406 | return 0; |
1408 | } | 1407 | } |
1409 | 1408 | ||
1410 | /* | 1409 | /* |
1411 | * ext4_ext_next_allocated_block: | 1410 | * ext4_ext_next_allocated_block: |
1412 | * returns allocated block in subsequent extent or EXT_MAX_BLOCK. | 1411 | * returns allocated block in subsequent extent or EXT_MAX_BLOCKS. |
1413 | * NOTE: it considers block number from index entry as | 1412 | * NOTE: it considers block number from index entry as |
1414 | * allocated block. Thus, index entries have to be consistent | 1413 | * allocated block. Thus, index entries have to be consistent |
1415 | * with leaves. | 1414 | * with leaves. |
@@ -1423,7 +1422,7 @@ ext4_ext_next_allocated_block(struct ext4_ext_path *path) | |||
1423 | depth = path->p_depth; | 1422 | depth = path->p_depth; |
1424 | 1423 | ||
1425 | if (depth == 0 && path->p_ext == NULL) | 1424 | if (depth == 0 && path->p_ext == NULL) |
1426 | return EXT_MAX_BLOCK; | 1425 | return EXT_MAX_BLOCKS; |
1427 | 1426 | ||
1428 | while (depth >= 0) { | 1427 | while (depth >= 0) { |
1429 | if (depth == path->p_depth) { | 1428 | if (depth == path->p_depth) { |
@@ -1440,12 +1439,12 @@ ext4_ext_next_allocated_block(struct ext4_ext_path *path) | |||
1440 | depth--; | 1439 | depth--; |
1441 | } | 1440 | } |
1442 | 1441 | ||
1443 | return EXT_MAX_BLOCK; | 1442 | return EXT_MAX_BLOCKS; |
1444 | } | 1443 | } |
1445 | 1444 | ||
1446 | /* | 1445 | /* |
1447 | * ext4_ext_next_leaf_block: | 1446 | * ext4_ext_next_leaf_block: |
1448 | * returns first allocated block from next leaf or EXT_MAX_BLOCK | 1447 | * returns first allocated block from next leaf or EXT_MAX_BLOCKS |
1449 | */ | 1448 | */ |
1450 | static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode, | 1449 | static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode, |
1451 | struct ext4_ext_path *path) | 1450 | struct ext4_ext_path *path) |
@@ -1457,7 +1456,7 @@ static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode, | |||
1457 | 1456 | ||
1458 | /* zero-tree has no leaf blocks at all */ | 1457 | /* zero-tree has no leaf blocks at all */ |
1459 | if (depth == 0) | 1458 | if (depth == 0) |
1460 | return EXT_MAX_BLOCK; | 1459 | return EXT_MAX_BLOCKS; |
1461 | 1460 | ||
1462 | /* go to index block */ | 1461 | /* go to index block */ |
1463 | depth--; | 1462 | depth--; |
@@ -1470,7 +1469,7 @@ static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode, | |||
1470 | depth--; | 1469 | depth--; |
1471 | } | 1470 | } |
1472 | 1471 | ||
1473 | return EXT_MAX_BLOCK; | 1472 | return EXT_MAX_BLOCKS; |
1474 | } | 1473 | } |
1475 | 1474 | ||
1476 | /* | 1475 | /* |
@@ -1573,7 +1572,7 @@ ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1, | |||
1573 | return 0; | 1572 | return 0; |
1574 | #endif | 1573 | #endif |
1575 | 1574 | ||
1576 | if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2)) | 1575 | if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2)) |
1577 | return 1; | 1576 | return 1; |
1578 | return 0; | 1577 | return 0; |
1579 | } | 1578 | } |
@@ -1585,9 +1584,9 @@ ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1, | |||
1585 | * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns | 1584 | * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns |
1586 | * 1 if they got merged. | 1585 | * 1 if they got merged. |
1587 | */ | 1586 | */ |
1588 | int ext4_ext_try_to_merge(struct inode *inode, | 1587 | static int ext4_ext_try_to_merge_right(struct inode *inode, |
1589 | struct ext4_ext_path *path, | 1588 | struct ext4_ext_path *path, |
1590 | struct ext4_extent *ex) | 1589 | struct ext4_extent *ex) |
1591 | { | 1590 | { |
1592 | struct ext4_extent_header *eh; | 1591 | struct ext4_extent_header *eh; |
1593 | unsigned int depth, len; | 1592 | unsigned int depth, len; |
@@ -1625,6 +1624,31 @@ int ext4_ext_try_to_merge(struct inode *inode, | |||
1625 | } | 1624 | } |
1626 | 1625 | ||
1627 | /* | 1626 | /* |
1627 | * This function tries to merge the @ex extent to neighbours in the tree. | ||
1628 | * return 1 if merge left else 0. | ||
1629 | */ | ||
1630 | static int ext4_ext_try_to_merge(struct inode *inode, | ||
1631 | struct ext4_ext_path *path, | ||
1632 | struct ext4_extent *ex) { | ||
1633 | struct ext4_extent_header *eh; | ||
1634 | unsigned int depth; | ||
1635 | int merge_done = 0; | ||
1636 | int ret = 0; | ||
1637 | |||
1638 | depth = ext_depth(inode); | ||
1639 | BUG_ON(path[depth].p_hdr == NULL); | ||
1640 | eh = path[depth].p_hdr; | ||
1641 | |||
1642 | if (ex > EXT_FIRST_EXTENT(eh)) | ||
1643 | merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1); | ||
1644 | |||
1645 | if (!merge_done) | ||
1646 | ret = ext4_ext_try_to_merge_right(inode, path, ex); | ||
1647 | |||
1648 | return ret; | ||
1649 | } | ||
1650 | |||
1651 | /* | ||
1628 | * check if a portion of the "newext" extent overlaps with an | 1652 | * check if a portion of the "newext" extent overlaps with an |
1629 | * existing extent. | 1653 | * existing extent. |
1630 | * | 1654 | * |
@@ -1632,9 +1656,9 @@ int ext4_ext_try_to_merge(struct inode *inode, | |||
1632 | * such that there will be no overlap, and then returns 1. | 1656 | * such that there will be no overlap, and then returns 1. |
1633 | * If there is no overlap found, it returns 0. | 1657 | * If there is no overlap found, it returns 0. |
1634 | */ | 1658 | */ |
1635 | unsigned int ext4_ext_check_overlap(struct inode *inode, | 1659 | static unsigned int ext4_ext_check_overlap(struct inode *inode, |
1636 | struct ext4_extent *newext, | 1660 | struct ext4_extent *newext, |
1637 | struct ext4_ext_path *path) | 1661 | struct ext4_ext_path *path) |
1638 | { | 1662 | { |
1639 | ext4_lblk_t b1, b2; | 1663 | ext4_lblk_t b1, b2; |
1640 | unsigned int depth, len1; | 1664 | unsigned int depth, len1; |
@@ -1653,13 +1677,13 @@ unsigned int ext4_ext_check_overlap(struct inode *inode, | |||
1653 | */ | 1677 | */ |
1654 | if (b2 < b1) { | 1678 | if (b2 < b1) { |
1655 | b2 = ext4_ext_next_allocated_block(path); | 1679 | b2 = ext4_ext_next_allocated_block(path); |
1656 | if (b2 == EXT_MAX_BLOCK) | 1680 | if (b2 == EXT_MAX_BLOCKS) |
1657 | goto out; | 1681 | goto out; |
1658 | } | 1682 | } |
1659 | 1683 | ||
1660 | /* check for wrap through zero on extent logical start block*/ | 1684 | /* check for wrap through zero on extent logical start block*/ |
1661 | if (b1 + len1 < b1) { | 1685 | if (b1 + len1 < b1) { |
1662 | len1 = EXT_MAX_BLOCK - b1; | 1686 | len1 = EXT_MAX_BLOCKS - b1; |
1663 | newext->ee_len = cpu_to_le16(len1); | 1687 | newext->ee_len = cpu_to_le16(len1); |
1664 | ret = 1; | 1688 | ret = 1; |
1665 | } | 1689 | } |
@@ -1690,6 +1714,7 @@ int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, | |||
1690 | int depth, len, err; | 1714 | int depth, len, err; |
1691 | ext4_lblk_t next; | 1715 | ext4_lblk_t next; |
1692 | unsigned uninitialized = 0; | 1716 | unsigned uninitialized = 0; |
1717 | int flags = 0; | ||
1693 | 1718 | ||
1694 | if (unlikely(ext4_ext_get_actual_len(newext) == 0)) { | 1719 | if (unlikely(ext4_ext_get_actual_len(newext) == 0)) { |
1695 | EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0"); | 1720 | EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0"); |
@@ -1706,11 +1731,12 @@ int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, | |||
1706 | if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO) | 1731 | if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO) |
1707 | && ext4_can_extents_be_merged(inode, ex, newext)) { | 1732 | && ext4_can_extents_be_merged(inode, ex, newext)) { |
1708 | ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n", | 1733 | ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n", |
1709 | ext4_ext_is_uninitialized(newext), | 1734 | ext4_ext_is_uninitialized(newext), |
1710 | ext4_ext_get_actual_len(newext), | 1735 | ext4_ext_get_actual_len(newext), |
1711 | le32_to_cpu(ex->ee_block), | 1736 | le32_to_cpu(ex->ee_block), |
1712 | ext4_ext_is_uninitialized(ex), | 1737 | ext4_ext_is_uninitialized(ex), |
1713 | ext4_ext_get_actual_len(ex), ext_pblock(ex)); | 1738 | ext4_ext_get_actual_len(ex), |
1739 | ext4_ext_pblock(ex)); | ||
1714 | err = ext4_ext_get_access(handle, inode, path + depth); | 1740 | err = ext4_ext_get_access(handle, inode, path + depth); |
1715 | if (err) | 1741 | if (err) |
1716 | return err; | 1742 | return err; |
@@ -1741,7 +1767,7 @@ repeat: | |||
1741 | fex = EXT_LAST_EXTENT(eh); | 1767 | fex = EXT_LAST_EXTENT(eh); |
1742 | next = ext4_ext_next_leaf_block(inode, path); | 1768 | next = ext4_ext_next_leaf_block(inode, path); |
1743 | if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block) | 1769 | if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block) |
1744 | && next != EXT_MAX_BLOCK) { | 1770 | && next != EXT_MAX_BLOCKS) { |
1745 | ext_debug("next leaf block - %d\n", next); | 1771 | ext_debug("next leaf block - %d\n", next); |
1746 | BUG_ON(npath != NULL); | 1772 | BUG_ON(npath != NULL); |
1747 | npath = ext4_ext_find_extent(inode, next, NULL); | 1773 | npath = ext4_ext_find_extent(inode, next, NULL); |
@@ -1750,7 +1776,7 @@ repeat: | |||
1750 | BUG_ON(npath->p_depth != path->p_depth); | 1776 | BUG_ON(npath->p_depth != path->p_depth); |
1751 | eh = npath[depth].p_hdr; | 1777 | eh = npath[depth].p_hdr; |
1752 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { | 1778 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { |
1753 | ext_debug("next leaf isnt full(%d)\n", | 1779 | ext_debug("next leaf isn't full(%d)\n", |
1754 | le16_to_cpu(eh->eh_entries)); | 1780 | le16_to_cpu(eh->eh_entries)); |
1755 | path = npath; | 1781 | path = npath; |
1756 | goto repeat; | 1782 | goto repeat; |
@@ -1763,7 +1789,9 @@ repeat: | |||
1763 | * There is no free space in the found leaf. | 1789 | * There is no free space in the found leaf. |
1764 | * We're gonna add a new leaf in the tree. | 1790 | * We're gonna add a new leaf in the tree. |
1765 | */ | 1791 | */ |
1766 | err = ext4_ext_create_new_leaf(handle, inode, path, newext); | 1792 | if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) |
1793 | flags = EXT4_MB_USE_ROOT_BLOCKS; | ||
1794 | err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext); | ||
1767 | if (err) | 1795 | if (err) |
1768 | goto cleanup; | 1796 | goto cleanup; |
1769 | depth = ext_depth(inode); | 1797 | depth = ext_depth(inode); |
@@ -1780,7 +1808,7 @@ has_space: | |||
1780 | /* there is no extent in this leaf, create first one */ | 1808 | /* there is no extent in this leaf, create first one */ |
1781 | ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n", | 1809 | ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n", |
1782 | le32_to_cpu(newext->ee_block), | 1810 | le32_to_cpu(newext->ee_block), |
1783 | ext_pblock(newext), | 1811 | ext4_ext_pblock(newext), |
1784 | ext4_ext_is_uninitialized(newext), | 1812 | ext4_ext_is_uninitialized(newext), |
1785 | ext4_ext_get_actual_len(newext)); | 1813 | ext4_ext_get_actual_len(newext)); |
1786 | path[depth].p_ext = EXT_FIRST_EXTENT(eh); | 1814 | path[depth].p_ext = EXT_FIRST_EXTENT(eh); |
@@ -1794,7 +1822,7 @@ has_space: | |||
1794 | ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, " | 1822 | ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, " |
1795 | "move %d from 0x%p to 0x%p\n", | 1823 | "move %d from 0x%p to 0x%p\n", |
1796 | le32_to_cpu(newext->ee_block), | 1824 | le32_to_cpu(newext->ee_block), |
1797 | ext_pblock(newext), | 1825 | ext4_ext_pblock(newext), |
1798 | ext4_ext_is_uninitialized(newext), | 1826 | ext4_ext_is_uninitialized(newext), |
1799 | ext4_ext_get_actual_len(newext), | 1827 | ext4_ext_get_actual_len(newext), |
1800 | nearex, len, nearex + 1, nearex + 2); | 1828 | nearex, len, nearex + 1, nearex + 2); |
@@ -1808,7 +1836,7 @@ has_space: | |||
1808 | ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, " | 1836 | ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, " |
1809 | "move %d from 0x%p to 0x%p\n", | 1837 | "move %d from 0x%p to 0x%p\n", |
1810 | le32_to_cpu(newext->ee_block), | 1838 | le32_to_cpu(newext->ee_block), |
1811 | ext_pblock(newext), | 1839 | ext4_ext_pblock(newext), |
1812 | ext4_ext_is_uninitialized(newext), | 1840 | ext4_ext_is_uninitialized(newext), |
1813 | ext4_ext_get_actual_len(newext), | 1841 | ext4_ext_get_actual_len(newext), |
1814 | nearex, len, nearex + 1, nearex + 2); | 1842 | nearex, len, nearex + 1, nearex + 2); |
@@ -1819,7 +1847,7 @@ has_space: | |||
1819 | le16_add_cpu(&eh->eh_entries, 1); | 1847 | le16_add_cpu(&eh->eh_entries, 1); |
1820 | nearex = path[depth].p_ext; | 1848 | nearex = path[depth].p_ext; |
1821 | nearex->ee_block = newext->ee_block; | 1849 | nearex->ee_block = newext->ee_block; |
1822 | ext4_ext_store_pblock(nearex, ext_pblock(newext)); | 1850 | ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext)); |
1823 | nearex->ee_len = newext->ee_len; | 1851 | nearex->ee_len = newext->ee_len; |
1824 | 1852 | ||
1825 | merge: | 1853 | merge: |
@@ -1845,9 +1873,9 @@ cleanup: | |||
1845 | return err; | 1873 | return err; |
1846 | } | 1874 | } |
1847 | 1875 | ||
1848 | int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block, | 1876 | static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block, |
1849 | ext4_lblk_t num, ext_prepare_callback func, | 1877 | ext4_lblk_t num, ext_prepare_callback func, |
1850 | void *cbdata) | 1878 | void *cbdata) |
1851 | { | 1879 | { |
1852 | struct ext4_ext_path *path = NULL; | 1880 | struct ext4_ext_path *path = NULL; |
1853 | struct ext4_ext_cache cbex; | 1881 | struct ext4_ext_cache cbex; |
@@ -1859,7 +1887,7 @@ int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block, | |||
1859 | BUG_ON(func == NULL); | 1887 | BUG_ON(func == NULL); |
1860 | BUG_ON(inode == NULL); | 1888 | BUG_ON(inode == NULL); |
1861 | 1889 | ||
1862 | while (block < last && block != EXT_MAX_BLOCK) { | 1890 | while (block < last && block != EXT_MAX_BLOCKS) { |
1863 | num = last - block; | 1891 | num = last - block; |
1864 | /* find extent for this block */ | 1892 | /* find extent for this block */ |
1865 | down_read(&EXT4_I(inode)->i_data_sem); | 1893 | down_read(&EXT4_I(inode)->i_data_sem); |
@@ -1919,12 +1947,10 @@ int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block, | |||
1919 | cbex.ec_block = start; | 1947 | cbex.ec_block = start; |
1920 | cbex.ec_len = end - start; | 1948 | cbex.ec_len = end - start; |
1921 | cbex.ec_start = 0; | 1949 | cbex.ec_start = 0; |
1922 | cbex.ec_type = EXT4_EXT_CACHE_GAP; | ||
1923 | } else { | 1950 | } else { |
1924 | cbex.ec_block = le32_to_cpu(ex->ee_block); | 1951 | cbex.ec_block = le32_to_cpu(ex->ee_block); |
1925 | cbex.ec_len = ext4_ext_get_actual_len(ex); | 1952 | cbex.ec_len = ext4_ext_get_actual_len(ex); |
1926 | cbex.ec_start = ext_pblock(ex); | 1953 | cbex.ec_start = ext4_ext_pblock(ex); |
1927 | cbex.ec_type = EXT4_EXT_CACHE_EXTENT; | ||
1928 | } | 1954 | } |
1929 | 1955 | ||
1930 | if (unlikely(cbex.ec_len == 0)) { | 1956 | if (unlikely(cbex.ec_len == 0)) { |
@@ -1932,7 +1958,7 @@ int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block, | |||
1932 | err = -EIO; | 1958 | err = -EIO; |
1933 | break; | 1959 | break; |
1934 | } | 1960 | } |
1935 | err = func(inode, path, &cbex, ex, cbdata); | 1961 | err = func(inode, next, &cbex, ex, cbdata); |
1936 | ext4_ext_drop_refs(path); | 1962 | ext4_ext_drop_refs(path); |
1937 | 1963 | ||
1938 | if (err < 0) | 1964 | if (err < 0) |
@@ -1964,13 +1990,12 @@ int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block, | |||
1964 | 1990 | ||
1965 | static void | 1991 | static void |
1966 | ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block, | 1992 | ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block, |
1967 | __u32 len, ext4_fsblk_t start, int type) | 1993 | __u32 len, ext4_fsblk_t start) |
1968 | { | 1994 | { |
1969 | struct ext4_ext_cache *cex; | 1995 | struct ext4_ext_cache *cex; |
1970 | BUG_ON(len == 0); | 1996 | BUG_ON(len == 0); |
1971 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); | 1997 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
1972 | cex = &EXT4_I(inode)->i_cached_extent; | 1998 | cex = &EXT4_I(inode)->i_cached_extent; |
1973 | cex->ec_type = type; | ||
1974 | cex->ec_block = block; | 1999 | cex->ec_block = block; |
1975 | cex->ec_len = len; | 2000 | cex->ec_len = len; |
1976 | cex->ec_start = start; | 2001 | cex->ec_start = start; |
@@ -1995,7 +2020,7 @@ ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path, | |||
1995 | if (ex == NULL) { | 2020 | if (ex == NULL) { |
1996 | /* there is no extent yet, so gap is [0;-] */ | 2021 | /* there is no extent yet, so gap is [0;-] */ |
1997 | lblock = 0; | 2022 | lblock = 0; |
1998 | len = EXT_MAX_BLOCK; | 2023 | len = EXT_MAX_BLOCKS; |
1999 | ext_debug("cache gap(whole file):"); | 2024 | ext_debug("cache gap(whole file):"); |
2000 | } else if (block < le32_to_cpu(ex->ee_block)) { | 2025 | } else if (block < le32_to_cpu(ex->ee_block)) { |
2001 | lblock = block; | 2026 | lblock = block; |
@@ -2023,43 +2048,90 @@ ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path, | |||
2023 | } | 2048 | } |
2024 | 2049 | ||
2025 | ext_debug(" -> %u:%lu\n", lblock, len); | 2050 | ext_debug(" -> %u:%lu\n", lblock, len); |
2026 | ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP); | 2051 | ext4_ext_put_in_cache(inode, lblock, len, 0); |
2027 | } | 2052 | } |
2028 | 2053 | ||
2029 | static int | 2054 | /* |
2030 | ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block, | 2055 | * ext4_ext_in_cache() |
2031 | struct ext4_extent *ex) | 2056 | * Checks to see if the given block is in the cache. |
2032 | { | 2057 | * If it is, the cached extent is stored in the given |
2058 | * cache extent pointer. If the cached extent is a hole, | ||
2059 | * this routine should be used instead of | ||
2060 | * ext4_ext_in_cache if the calling function needs to | ||
2061 | * know the size of the hole. | ||
2062 | * | ||
2063 | * @inode: The files inode | ||
2064 | * @block: The block to look for in the cache | ||
2065 | * @ex: Pointer where the cached extent will be stored | ||
2066 | * if it contains block | ||
2067 | * | ||
2068 | * Return 0 if cache is invalid; 1 if the cache is valid | ||
2069 | */ | ||
2070 | static int ext4_ext_check_cache(struct inode *inode, ext4_lblk_t block, | ||
2071 | struct ext4_ext_cache *ex){ | ||
2033 | struct ext4_ext_cache *cex; | 2072 | struct ext4_ext_cache *cex; |
2034 | int ret = EXT4_EXT_CACHE_NO; | 2073 | struct ext4_sb_info *sbi; |
2074 | int ret = 0; | ||
2035 | 2075 | ||
2036 | /* | 2076 | /* |
2037 | * We borrow i_block_reservation_lock to protect i_cached_extent | 2077 | * We borrow i_block_reservation_lock to protect i_cached_extent |
2038 | */ | 2078 | */ |
2039 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); | 2079 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
2040 | cex = &EXT4_I(inode)->i_cached_extent; | 2080 | cex = &EXT4_I(inode)->i_cached_extent; |
2081 | sbi = EXT4_SB(inode->i_sb); | ||
2041 | 2082 | ||
2042 | /* has cache valid data? */ | 2083 | /* has cache valid data? */ |
2043 | if (cex->ec_type == EXT4_EXT_CACHE_NO) | 2084 | if (cex->ec_len == 0) |
2044 | goto errout; | 2085 | goto errout; |
2045 | 2086 | ||
2046 | BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP && | ||
2047 | cex->ec_type != EXT4_EXT_CACHE_EXTENT); | ||
2048 | if (in_range(block, cex->ec_block, cex->ec_len)) { | 2087 | if (in_range(block, cex->ec_block, cex->ec_len)) { |
2049 | ex->ee_block = cpu_to_le32(cex->ec_block); | 2088 | memcpy(ex, cex, sizeof(struct ext4_ext_cache)); |
2050 | ext4_ext_store_pblock(ex, cex->ec_start); | ||
2051 | ex->ee_len = cpu_to_le16(cex->ec_len); | ||
2052 | ext_debug("%u cached by %u:%u:%llu\n", | 2089 | ext_debug("%u cached by %u:%u:%llu\n", |
2053 | block, | 2090 | block, |
2054 | cex->ec_block, cex->ec_len, cex->ec_start); | 2091 | cex->ec_block, cex->ec_len, cex->ec_start); |
2055 | ret = cex->ec_type; | 2092 | ret = 1; |
2056 | } | 2093 | } |
2057 | errout: | 2094 | errout: |
2095 | if (!ret) | ||
2096 | sbi->extent_cache_misses++; | ||
2097 | else | ||
2098 | sbi->extent_cache_hits++; | ||
2058 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); | 2099 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
2059 | return ret; | 2100 | return ret; |
2060 | } | 2101 | } |
2061 | 2102 | ||
2062 | /* | 2103 | /* |
2104 | * ext4_ext_in_cache() | ||
2105 | * Checks to see if the given block is in the cache. | ||
2106 | * If it is, the cached extent is stored in the given | ||
2107 | * extent pointer. | ||
2108 | * | ||
2109 | * @inode: The files inode | ||
2110 | * @block: The block to look for in the cache | ||
2111 | * @ex: Pointer where the cached extent will be stored | ||
2112 | * if it contains block | ||
2113 | * | ||
2114 | * Return 0 if cache is invalid; 1 if the cache is valid | ||
2115 | */ | ||
2116 | static int | ||
2117 | ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block, | ||
2118 | struct ext4_extent *ex) | ||
2119 | { | ||
2120 | struct ext4_ext_cache cex; | ||
2121 | int ret = 0; | ||
2122 | |||
2123 | if (ext4_ext_check_cache(inode, block, &cex)) { | ||
2124 | ex->ee_block = cpu_to_le32(cex.ec_block); | ||
2125 | ext4_ext_store_pblock(ex, cex.ec_start); | ||
2126 | ex->ee_len = cpu_to_le16(cex.ec_len); | ||
2127 | ret = 1; | ||
2128 | } | ||
2129 | |||
2130 | return ret; | ||
2131 | } | ||
2132 | |||
2133 | |||
2134 | /* | ||
2063 | * ext4_ext_rm_idx: | 2135 | * ext4_ext_rm_idx: |
2064 | * removes index from the index block. | 2136 | * removes index from the index block. |
2065 | * It's used in truncate case only, thus all requests are for | 2137 | * It's used in truncate case only, thus all requests are for |
@@ -2073,7 +2145,7 @@ static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, | |||
2073 | 2145 | ||
2074 | /* free index block */ | 2146 | /* free index block */ |
2075 | path--; | 2147 | path--; |
2076 | leaf = idx_pblock(path->p_idx); | 2148 | leaf = ext4_idx_pblock(path->p_idx); |
2077 | if (unlikely(path->p_hdr->eh_entries == 0)) { | 2149 | if (unlikely(path->p_hdr->eh_entries == 0)) { |
2078 | EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0"); | 2150 | EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0"); |
2079 | return -EIO; | 2151 | return -EIO; |
@@ -2086,7 +2158,7 @@ static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, | |||
2086 | if (err) | 2158 | if (err) |
2087 | return err; | 2159 | return err; |
2088 | ext_debug("index is empty, remove it, free block %llu\n", leaf); | 2160 | ext_debug("index is empty, remove it, free block %llu\n", leaf); |
2089 | ext4_free_blocks(handle, inode, 0, leaf, 1, | 2161 | ext4_free_blocks(handle, inode, NULL, leaf, 1, |
2090 | EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); | 2162 | EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); |
2091 | return err; | 2163 | return err; |
2092 | } | 2164 | } |
@@ -2181,13 +2253,21 @@ static int ext4_remove_blocks(handle_t *handle, struct inode *inode, | |||
2181 | ext4_fsblk_t start; | 2253 | ext4_fsblk_t start; |
2182 | 2254 | ||
2183 | num = le32_to_cpu(ex->ee_block) + ee_len - from; | 2255 | num = le32_to_cpu(ex->ee_block) + ee_len - from; |
2184 | start = ext_pblock(ex) + ee_len - num; | 2256 | start = ext4_ext_pblock(ex) + ee_len - num; |
2185 | ext_debug("free last %u blocks starting %llu\n", num, start); | 2257 | ext_debug("free last %u blocks starting %llu\n", num, start); |
2186 | ext4_free_blocks(handle, inode, 0, start, num, flags); | 2258 | ext4_free_blocks(handle, inode, NULL, start, num, flags); |
2187 | } else if (from == le32_to_cpu(ex->ee_block) | 2259 | } else if (from == le32_to_cpu(ex->ee_block) |
2188 | && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) { | 2260 | && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) { |
2189 | printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n", | 2261 | /* head removal */ |
2190 | from, to, le32_to_cpu(ex->ee_block), ee_len); | 2262 | ext4_lblk_t num; |
2263 | ext4_fsblk_t start; | ||
2264 | |||
2265 | num = to - from; | ||
2266 | start = ext4_ext_pblock(ex); | ||
2267 | |||
2268 | ext_debug("free first %u blocks starting %llu\n", num, start); | ||
2269 | ext4_free_blocks(handle, inode, 0, start, num, flags); | ||
2270 | |||
2191 | } else { | 2271 | } else { |
2192 | printk(KERN_INFO "strange request: removal(2) " | 2272 | printk(KERN_INFO "strange request: removal(2) " |
2193 | "%u-%u from %u:%u\n", | 2273 | "%u-%u from %u:%u\n", |
@@ -2196,9 +2276,22 @@ static int ext4_remove_blocks(handle_t *handle, struct inode *inode, | |||
2196 | return 0; | 2276 | return 0; |
2197 | } | 2277 | } |
2198 | 2278 | ||
2279 | |||
2280 | /* | ||
2281 | * ext4_ext_rm_leaf() Removes the extents associated with the | ||
2282 | * blocks appearing between "start" and "end", and splits the extents | ||
2283 | * if "start" and "end" appear in the same extent | ||
2284 | * | ||
2285 | * @handle: The journal handle | ||
2286 | * @inode: The files inode | ||
2287 | * @path: The path to the leaf | ||
2288 | * @start: The first block to remove | ||
2289 | * @end: The last block to remove | ||
2290 | */ | ||
2199 | static int | 2291 | static int |
2200 | ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, | 2292 | ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, |
2201 | struct ext4_ext_path *path, ext4_lblk_t start) | 2293 | struct ext4_ext_path *path, ext4_lblk_t start, |
2294 | ext4_lblk_t end) | ||
2202 | { | 2295 | { |
2203 | int err = 0, correct_index = 0; | 2296 | int err = 0, correct_index = 0; |
2204 | int depth = ext_depth(inode), credits; | 2297 | int depth = ext_depth(inode), credits; |
@@ -2209,6 +2302,7 @@ ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, | |||
2209 | unsigned short ex_ee_len; | 2302 | unsigned short ex_ee_len; |
2210 | unsigned uninitialized = 0; | 2303 | unsigned uninitialized = 0; |
2211 | struct ext4_extent *ex; | 2304 | struct ext4_extent *ex; |
2305 | struct ext4_map_blocks map; | ||
2212 | 2306 | ||
2213 | /* the header must be checked already in ext4_ext_remove_space() */ | 2307 | /* the header must be checked already in ext4_ext_remove_space() */ |
2214 | ext_debug("truncate since %u in leaf\n", start); | 2308 | ext_debug("truncate since %u in leaf\n", start); |
@@ -2238,31 +2332,95 @@ ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, | |||
2238 | path[depth].p_ext = ex; | 2332 | path[depth].p_ext = ex; |
2239 | 2333 | ||
2240 | a = ex_ee_block > start ? ex_ee_block : start; | 2334 | a = ex_ee_block > start ? ex_ee_block : start; |
2241 | b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ? | 2335 | b = ex_ee_block+ex_ee_len - 1 < end ? |
2242 | ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK; | 2336 | ex_ee_block+ex_ee_len - 1 : end; |
2243 | 2337 | ||
2244 | ext_debug(" border %u:%u\n", a, b); | 2338 | ext_debug(" border %u:%u\n", a, b); |
2245 | 2339 | ||
2246 | if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) { | 2340 | /* If this extent is beyond the end of the hole, skip it */ |
2247 | block = 0; | 2341 | if (end <= ex_ee_block) { |
2248 | num = 0; | 2342 | ex--; |
2249 | BUG(); | 2343 | ex_ee_block = le32_to_cpu(ex->ee_block); |
2344 | ex_ee_len = ext4_ext_get_actual_len(ex); | ||
2345 | continue; | ||
2346 | } else if (a != ex_ee_block && | ||
2347 | b != ex_ee_block + ex_ee_len - 1) { | ||
2348 | /* | ||
2349 | * If this is a truncate, then this condition should | ||
2350 | * never happen because at least one of the end points | ||
2351 | * needs to be on the edge of the extent. | ||
2352 | */ | ||
2353 | if (end == EXT_MAX_BLOCKS - 1) { | ||
2354 | ext_debug(" bad truncate %u:%u\n", | ||
2355 | start, end); | ||
2356 | block = 0; | ||
2357 | num = 0; | ||
2358 | err = -EIO; | ||
2359 | goto out; | ||
2360 | } | ||
2361 | /* | ||
2362 | * else this is a hole punch, so the extent needs to | ||
2363 | * be split since neither edge of the hole is on the | ||
2364 | * extent edge | ||
2365 | */ | ||
2366 | else{ | ||
2367 | map.m_pblk = ext4_ext_pblock(ex); | ||
2368 | map.m_lblk = ex_ee_block; | ||
2369 | map.m_len = b - ex_ee_block; | ||
2370 | |||
2371 | err = ext4_split_extent(handle, | ||
2372 | inode, path, &map, 0, | ||
2373 | EXT4_GET_BLOCKS_PUNCH_OUT_EXT | | ||
2374 | EXT4_GET_BLOCKS_PRE_IO); | ||
2375 | |||
2376 | if (err < 0) | ||
2377 | goto out; | ||
2378 | |||
2379 | ex_ee_len = ext4_ext_get_actual_len(ex); | ||
2380 | |||
2381 | b = ex_ee_block+ex_ee_len - 1 < end ? | ||
2382 | ex_ee_block+ex_ee_len - 1 : end; | ||
2383 | |||
2384 | /* Then remove tail of this extent */ | ||
2385 | block = ex_ee_block; | ||
2386 | num = a - block; | ||
2387 | } | ||
2250 | } else if (a != ex_ee_block) { | 2388 | } else if (a != ex_ee_block) { |
2251 | /* remove tail of the extent */ | 2389 | /* remove tail of the extent */ |
2252 | block = ex_ee_block; | 2390 | block = ex_ee_block; |
2253 | num = a - block; | 2391 | num = a - block; |
2254 | } else if (b != ex_ee_block + ex_ee_len - 1) { | 2392 | } else if (b != ex_ee_block + ex_ee_len - 1) { |
2255 | /* remove head of the extent */ | 2393 | /* remove head of the extent */ |
2256 | block = a; | 2394 | block = b; |
2257 | num = b - a; | 2395 | num = ex_ee_block + ex_ee_len - b; |
2258 | /* there is no "make a hole" API yet */ | 2396 | |
2259 | BUG(); | 2397 | /* |
2398 | * If this is a truncate, this condition | ||
2399 | * should never happen | ||
2400 | */ | ||
2401 | if (end == EXT_MAX_BLOCKS - 1) { | ||
2402 | ext_debug(" bad truncate %u:%u\n", | ||
2403 | start, end); | ||
2404 | err = -EIO; | ||
2405 | goto out; | ||
2406 | } | ||
2260 | } else { | 2407 | } else { |
2261 | /* remove whole extent: excellent! */ | 2408 | /* remove whole extent: excellent! */ |
2262 | block = ex_ee_block; | 2409 | block = ex_ee_block; |
2263 | num = 0; | 2410 | num = 0; |
2264 | BUG_ON(a != ex_ee_block); | 2411 | if (a != ex_ee_block) { |
2265 | BUG_ON(b != ex_ee_block + ex_ee_len - 1); | 2412 | ext_debug(" bad truncate %u:%u\n", |
2413 | start, end); | ||
2414 | err = -EIO; | ||
2415 | goto out; | ||
2416 | } | ||
2417 | |||
2418 | if (b != ex_ee_block + ex_ee_len - 1) { | ||
2419 | ext_debug(" bad truncate %u:%u\n", | ||
2420 | start, end); | ||
2421 | err = -EIO; | ||
2422 | goto out; | ||
2423 | } | ||
2266 | } | 2424 | } |
2267 | 2425 | ||
2268 | /* | 2426 | /* |
@@ -2293,7 +2451,13 @@ ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, | |||
2293 | if (num == 0) { | 2451 | if (num == 0) { |
2294 | /* this extent is removed; mark slot entirely unused */ | 2452 | /* this extent is removed; mark slot entirely unused */ |
2295 | ext4_ext_store_pblock(ex, 0); | 2453 | ext4_ext_store_pblock(ex, 0); |
2296 | le16_add_cpu(&eh->eh_entries, -1); | 2454 | } else if (block != ex_ee_block) { |
2455 | /* | ||
2456 | * If this was a head removal, then we need to update | ||
2457 | * the physical block since it is now at a different | ||
2458 | * location | ||
2459 | */ | ||
2460 | ext4_ext_store_pblock(ex, ext4_ext_pblock(ex) + (b-a)); | ||
2297 | } | 2461 | } |
2298 | 2462 | ||
2299 | ex->ee_block = cpu_to_le32(block); | 2463 | ex->ee_block = cpu_to_le32(block); |
@@ -2309,8 +2473,29 @@ ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, | |||
2309 | if (err) | 2473 | if (err) |
2310 | goto out; | 2474 | goto out; |
2311 | 2475 | ||
2476 | /* | ||
2477 | * If the extent was completely released, | ||
2478 | * we need to remove it from the leaf | ||
2479 | */ | ||
2480 | if (num == 0) { | ||
2481 | if (end != EXT_MAX_BLOCKS - 1) { | ||
2482 | /* | ||
2483 | * For hole punching, we need to scoot all the | ||
2484 | * extents up when an extent is removed so that | ||
2485 | * we dont have blank extents in the middle | ||
2486 | */ | ||
2487 | memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) * | ||
2488 | sizeof(struct ext4_extent)); | ||
2489 | |||
2490 | /* Now get rid of the one at the end */ | ||
2491 | memset(EXT_LAST_EXTENT(eh), 0, | ||
2492 | sizeof(struct ext4_extent)); | ||
2493 | } | ||
2494 | le16_add_cpu(&eh->eh_entries, -1); | ||
2495 | } | ||
2496 | |||
2312 | ext_debug("new extent: %u:%u:%llu\n", block, num, | 2497 | ext_debug("new extent: %u:%u:%llu\n", block, num, |
2313 | ext_pblock(ex)); | 2498 | ext4_ext_pblock(ex)); |
2314 | ex--; | 2499 | ex--; |
2315 | ex_ee_block = le32_to_cpu(ex->ee_block); | 2500 | ex_ee_block = le32_to_cpu(ex->ee_block); |
2316 | ex_ee_len = ext4_ext_get_actual_len(ex); | 2501 | ex_ee_len = ext4_ext_get_actual_len(ex); |
@@ -2349,7 +2534,8 @@ ext4_ext_more_to_rm(struct ext4_ext_path *path) | |||
2349 | return 1; | 2534 | return 1; |
2350 | } | 2535 | } |
2351 | 2536 | ||
2352 | static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start) | 2537 | static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start, |
2538 | ext4_lblk_t end) | ||
2353 | { | 2539 | { |
2354 | struct super_block *sb = inode->i_sb; | 2540 | struct super_block *sb = inode->i_sb; |
2355 | int depth = ext_depth(inode); | 2541 | int depth = ext_depth(inode); |
@@ -2388,7 +2574,8 @@ again: | |||
2388 | while (i >= 0 && err == 0) { | 2574 | while (i >= 0 && err == 0) { |
2389 | if (i == depth) { | 2575 | if (i == depth) { |
2390 | /* this is leaf block */ | 2576 | /* this is leaf block */ |
2391 | err = ext4_ext_rm_leaf(handle, inode, path, start); | 2577 | err = ext4_ext_rm_leaf(handle, inode, path, |
2578 | start, end); | ||
2392 | /* root level has p_bh == NULL, brelse() eats this */ | 2579 | /* root level has p_bh == NULL, brelse() eats this */ |
2393 | brelse(path[i].p_bh); | 2580 | brelse(path[i].p_bh); |
2394 | path[i].p_bh = NULL; | 2581 | path[i].p_bh = NULL; |
@@ -2421,9 +2608,9 @@ again: | |||
2421 | struct buffer_head *bh; | 2608 | struct buffer_head *bh; |
2422 | /* go to the next level */ | 2609 | /* go to the next level */ |
2423 | ext_debug("move to level %d (block %llu)\n", | 2610 | ext_debug("move to level %d (block %llu)\n", |
2424 | i + 1, idx_pblock(path[i].p_idx)); | 2611 | i + 1, ext4_idx_pblock(path[i].p_idx)); |
2425 | memset(path + i + 1, 0, sizeof(*path)); | 2612 | memset(path + i + 1, 0, sizeof(*path)); |
2426 | bh = sb_bread(sb, idx_pblock(path[i].p_idx)); | 2613 | bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx)); |
2427 | if (!bh) { | 2614 | if (!bh) { |
2428 | /* should we reset i_size? */ | 2615 | /* should we reset i_size? */ |
2429 | err = -EIO; | 2616 | err = -EIO; |
@@ -2535,84 +2722,217 @@ void ext4_ext_release(struct super_block *sb) | |||
2535 | #endif | 2722 | #endif |
2536 | } | 2723 | } |
2537 | 2724 | ||
2538 | static void bi_complete(struct bio *bio, int error) | ||
2539 | { | ||
2540 | complete((struct completion *)bio->bi_private); | ||
2541 | } | ||
2542 | |||
2543 | /* FIXME!! we need to try to merge to left or right after zero-out */ | 2725 | /* FIXME!! we need to try to merge to left or right after zero-out */ |
2544 | static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) | 2726 | static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) |
2545 | { | 2727 | { |
2728 | ext4_fsblk_t ee_pblock; | ||
2729 | unsigned int ee_len; | ||
2546 | int ret; | 2730 | int ret; |
2547 | struct bio *bio; | ||
2548 | int blkbits, blocksize; | ||
2549 | sector_t ee_pblock; | ||
2550 | struct completion event; | ||
2551 | unsigned int ee_len, len, done, offset; | ||
2552 | 2731 | ||
2553 | |||
2554 | blkbits = inode->i_blkbits; | ||
2555 | blocksize = inode->i_sb->s_blocksize; | ||
2556 | ee_len = ext4_ext_get_actual_len(ex); | 2732 | ee_len = ext4_ext_get_actual_len(ex); |
2557 | ee_pblock = ext_pblock(ex); | 2733 | ee_pblock = ext4_ext_pblock(ex); |
2734 | |||
2735 | ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS); | ||
2736 | if (ret > 0) | ||
2737 | ret = 0; | ||
2738 | |||
2739 | return ret; | ||
2740 | } | ||
2741 | |||
2742 | /* | ||
2743 | * used by extent splitting. | ||
2744 | */ | ||
2745 | #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \ | ||
2746 | due to ENOSPC */ | ||
2747 | #define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */ | ||
2748 | #define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */ | ||
2749 | |||
2750 | /* | ||
2751 | * ext4_split_extent_at() splits an extent at given block. | ||
2752 | * | ||
2753 | * @handle: the journal handle | ||
2754 | * @inode: the file inode | ||
2755 | * @path: the path to the extent | ||
2756 | * @split: the logical block where the extent is splitted. | ||
2757 | * @split_flags: indicates if the extent could be zeroout if split fails, and | ||
2758 | * the states(init or uninit) of new extents. | ||
2759 | * @flags: flags used to insert new extent to extent tree. | ||
2760 | * | ||
2761 | * | ||
2762 | * Splits extent [a, b] into two extents [a, @split) and [@split, b], states | ||
2763 | * of which are deterimined by split_flag. | ||
2764 | * | ||
2765 | * There are two cases: | ||
2766 | * a> the extent are splitted into two extent. | ||
2767 | * b> split is not needed, and just mark the extent. | ||
2768 | * | ||
2769 | * return 0 on success. | ||
2770 | */ | ||
2771 | static int ext4_split_extent_at(handle_t *handle, | ||
2772 | struct inode *inode, | ||
2773 | struct ext4_ext_path *path, | ||
2774 | ext4_lblk_t split, | ||
2775 | int split_flag, | ||
2776 | int flags) | ||
2777 | { | ||
2778 | ext4_fsblk_t newblock; | ||
2779 | ext4_lblk_t ee_block; | ||
2780 | struct ext4_extent *ex, newex, orig_ex; | ||
2781 | struct ext4_extent *ex2 = NULL; | ||
2782 | unsigned int ee_len, depth; | ||
2783 | int err = 0; | ||
2784 | |||
2785 | ext_debug("ext4_split_extents_at: inode %lu, logical" | ||
2786 | "block %llu\n", inode->i_ino, (unsigned long long)split); | ||
2558 | 2787 | ||
2559 | /* convert ee_pblock to 512 byte sectors */ | 2788 | ext4_ext_show_leaf(inode, path); |
2560 | ee_pblock = ee_pblock << (blkbits - 9); | 2789 | |
2790 | depth = ext_depth(inode); | ||
2791 | ex = path[depth].p_ext; | ||
2792 | ee_block = le32_to_cpu(ex->ee_block); | ||
2793 | ee_len = ext4_ext_get_actual_len(ex); | ||
2794 | newblock = split - ee_block + ext4_ext_pblock(ex); | ||
2561 | 2795 | ||
2562 | while (ee_len > 0) { | 2796 | BUG_ON(split < ee_block || split >= (ee_block + ee_len)); |
2563 | 2797 | ||
2564 | if (ee_len > BIO_MAX_PAGES) | 2798 | err = ext4_ext_get_access(handle, inode, path + depth); |
2565 | len = BIO_MAX_PAGES; | 2799 | if (err) |
2800 | goto out; | ||
2801 | |||
2802 | if (split == ee_block) { | ||
2803 | /* | ||
2804 | * case b: block @split is the block that the extent begins with | ||
2805 | * then we just change the state of the extent, and splitting | ||
2806 | * is not needed. | ||
2807 | */ | ||
2808 | if (split_flag & EXT4_EXT_MARK_UNINIT2) | ||
2809 | ext4_ext_mark_uninitialized(ex); | ||
2566 | else | 2810 | else |
2567 | len = ee_len; | 2811 | ext4_ext_mark_initialized(ex); |
2568 | 2812 | ||
2569 | bio = bio_alloc(GFP_NOIO, len); | 2813 | if (!(flags & EXT4_GET_BLOCKS_PRE_IO)) |
2570 | if (!bio) | 2814 | ext4_ext_try_to_merge(inode, path, ex); |
2571 | return -ENOMEM; | ||
2572 | 2815 | ||
2573 | bio->bi_sector = ee_pblock; | 2816 | err = ext4_ext_dirty(handle, inode, path + depth); |
2574 | bio->bi_bdev = inode->i_sb->s_bdev; | 2817 | goto out; |
2818 | } | ||
2575 | 2819 | ||
2576 | done = 0; | 2820 | /* case a */ |
2577 | offset = 0; | 2821 | memcpy(&orig_ex, ex, sizeof(orig_ex)); |
2578 | while (done < len) { | 2822 | ex->ee_len = cpu_to_le16(split - ee_block); |
2579 | ret = bio_add_page(bio, ZERO_PAGE(0), | 2823 | if (split_flag & EXT4_EXT_MARK_UNINIT1) |
2580 | blocksize, offset); | 2824 | ext4_ext_mark_uninitialized(ex); |
2581 | if (ret != blocksize) { | ||
2582 | /* | ||
2583 | * We can't add any more pages because of | ||
2584 | * hardware limitations. Start a new bio. | ||
2585 | */ | ||
2586 | break; | ||
2587 | } | ||
2588 | done++; | ||
2589 | offset += blocksize; | ||
2590 | if (offset >= PAGE_CACHE_SIZE) | ||
2591 | offset = 0; | ||
2592 | } | ||
2593 | 2825 | ||
2594 | init_completion(&event); | 2826 | /* |
2595 | bio->bi_private = &event; | 2827 | * path may lead to new leaf, not to original leaf any more |
2596 | bio->bi_end_io = bi_complete; | 2828 | * after ext4_ext_insert_extent() returns, |
2597 | submit_bio(WRITE, bio); | 2829 | */ |
2598 | wait_for_completion(&event); | 2830 | err = ext4_ext_dirty(handle, inode, path + depth); |
2831 | if (err) | ||
2832 | goto fix_extent_len; | ||
2599 | 2833 | ||
2600 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) { | 2834 | ex2 = &newex; |
2601 | bio_put(bio); | 2835 | ex2->ee_block = cpu_to_le32(split); |
2602 | return -EIO; | 2836 | ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block)); |
2603 | } | 2837 | ext4_ext_store_pblock(ex2, newblock); |
2604 | bio_put(bio); | 2838 | if (split_flag & EXT4_EXT_MARK_UNINIT2) |
2605 | ee_len -= done; | 2839 | ext4_ext_mark_uninitialized(ex2); |
2606 | ee_pblock += done << (blkbits - 9); | 2840 | |
2841 | err = ext4_ext_insert_extent(handle, inode, path, &newex, flags); | ||
2842 | if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) { | ||
2843 | err = ext4_ext_zeroout(inode, &orig_ex); | ||
2844 | if (err) | ||
2845 | goto fix_extent_len; | ||
2846 | /* update the extent length and mark as initialized */ | ||
2847 | ex->ee_len = cpu_to_le32(ee_len); | ||
2848 | ext4_ext_try_to_merge(inode, path, ex); | ||
2849 | err = ext4_ext_dirty(handle, inode, path + depth); | ||
2850 | goto out; | ||
2851 | } else if (err) | ||
2852 | goto fix_extent_len; | ||
2853 | |||
2854 | out: | ||
2855 | ext4_ext_show_leaf(inode, path); | ||
2856 | return err; | ||
2857 | |||
2858 | fix_extent_len: | ||
2859 | ex->ee_len = orig_ex.ee_len; | ||
2860 | ext4_ext_dirty(handle, inode, path + depth); | ||
2861 | return err; | ||
2862 | } | ||
2863 | |||
2864 | /* | ||
2865 | * ext4_split_extents() splits an extent and mark extent which is covered | ||
2866 | * by @map as split_flags indicates | ||
2867 | * | ||
2868 | * It may result in splitting the extent into multiple extents (upto three) | ||
2869 | * There are three possibilities: | ||
2870 | * a> There is no split required | ||
2871 | * b> Splits in two extents: Split is happening at either end of the extent | ||
2872 | * c> Splits in three extents: Somone is splitting in middle of the extent | ||
2873 | * | ||
2874 | */ | ||
2875 | static int ext4_split_extent(handle_t *handle, | ||
2876 | struct inode *inode, | ||
2877 | struct ext4_ext_path *path, | ||
2878 | struct ext4_map_blocks *map, | ||
2879 | int split_flag, | ||
2880 | int flags) | ||
2881 | { | ||
2882 | ext4_lblk_t ee_block; | ||
2883 | struct ext4_extent *ex; | ||
2884 | unsigned int ee_len, depth; | ||
2885 | int err = 0; | ||
2886 | int uninitialized; | ||
2887 | int split_flag1, flags1; | ||
2888 | |||
2889 | depth = ext_depth(inode); | ||
2890 | ex = path[depth].p_ext; | ||
2891 | ee_block = le32_to_cpu(ex->ee_block); | ||
2892 | ee_len = ext4_ext_get_actual_len(ex); | ||
2893 | uninitialized = ext4_ext_is_uninitialized(ex); | ||
2894 | |||
2895 | if (map->m_lblk + map->m_len < ee_block + ee_len) { | ||
2896 | split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ? | ||
2897 | EXT4_EXT_MAY_ZEROOUT : 0; | ||
2898 | flags1 = flags | EXT4_GET_BLOCKS_PRE_IO; | ||
2899 | if (uninitialized) | ||
2900 | split_flag1 |= EXT4_EXT_MARK_UNINIT1 | | ||
2901 | EXT4_EXT_MARK_UNINIT2; | ||
2902 | err = ext4_split_extent_at(handle, inode, path, | ||
2903 | map->m_lblk + map->m_len, split_flag1, flags1); | ||
2904 | if (err) | ||
2905 | goto out; | ||
2607 | } | 2906 | } |
2608 | return 0; | 2907 | |
2908 | ext4_ext_drop_refs(path); | ||
2909 | path = ext4_ext_find_extent(inode, map->m_lblk, path); | ||
2910 | if (IS_ERR(path)) | ||
2911 | return PTR_ERR(path); | ||
2912 | |||
2913 | if (map->m_lblk >= ee_block) { | ||
2914 | split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ? | ||
2915 | EXT4_EXT_MAY_ZEROOUT : 0; | ||
2916 | if (uninitialized) | ||
2917 | split_flag1 |= EXT4_EXT_MARK_UNINIT1; | ||
2918 | if (split_flag & EXT4_EXT_MARK_UNINIT2) | ||
2919 | split_flag1 |= EXT4_EXT_MARK_UNINIT2; | ||
2920 | err = ext4_split_extent_at(handle, inode, path, | ||
2921 | map->m_lblk, split_flag1, flags); | ||
2922 | if (err) | ||
2923 | goto out; | ||
2924 | } | ||
2925 | |||
2926 | ext4_ext_show_leaf(inode, path); | ||
2927 | out: | ||
2928 | return err ? err : map->m_len; | ||
2609 | } | 2929 | } |
2610 | 2930 | ||
2611 | #define EXT4_EXT_ZERO_LEN 7 | 2931 | #define EXT4_EXT_ZERO_LEN 7 |
2612 | /* | 2932 | /* |
2613 | * This function is called by ext4_ext_map_blocks() if someone tries to write | 2933 | * This function is called by ext4_ext_map_blocks() if someone tries to write |
2614 | * to an uninitialized extent. It may result in splitting the uninitialized | 2934 | * to an uninitialized extent. It may result in splitting the uninitialized |
2615 | * extent into multiple extents (upto three - one initialized and two | 2935 | * extent into multiple extents (up to three - one initialized and two |
2616 | * uninitialized). | 2936 | * uninitialized). |
2617 | * There are three possibilities: | 2937 | * There are three possibilities: |
2618 | * a> There is no split required: Entire extent should be initialized | 2938 | * a> There is no split required: Entire extent should be initialized |
@@ -2624,17 +2944,13 @@ static int ext4_ext_convert_to_initialized(handle_t *handle, | |||
2624 | struct ext4_map_blocks *map, | 2944 | struct ext4_map_blocks *map, |
2625 | struct ext4_ext_path *path) | 2945 | struct ext4_ext_path *path) |
2626 | { | 2946 | { |
2627 | struct ext4_extent *ex, newex, orig_ex; | 2947 | struct ext4_map_blocks split_map; |
2628 | struct ext4_extent *ex1 = NULL; | 2948 | struct ext4_extent zero_ex; |
2629 | struct ext4_extent *ex2 = NULL; | 2949 | struct ext4_extent *ex; |
2630 | struct ext4_extent *ex3 = NULL; | ||
2631 | struct ext4_extent_header *eh; | ||
2632 | ext4_lblk_t ee_block, eof_block; | 2950 | ext4_lblk_t ee_block, eof_block; |
2633 | unsigned int allocated, ee_len, depth; | 2951 | unsigned int allocated, ee_len, depth; |
2634 | ext4_fsblk_t newblock; | ||
2635 | int err = 0; | 2952 | int err = 0; |
2636 | int ret = 0; | 2953 | int split_flag = 0; |
2637 | int may_zeroout; | ||
2638 | 2954 | ||
2639 | ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical" | 2955 | ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical" |
2640 | "block %llu, max_blocks %u\n", inode->i_ino, | 2956 | "block %llu, max_blocks %u\n", inode->i_ino, |
@@ -2646,279 +2962,86 @@ static int ext4_ext_convert_to_initialized(handle_t *handle, | |||
2646 | eof_block = map->m_lblk + map->m_len; | 2962 | eof_block = map->m_lblk + map->m_len; |
2647 | 2963 | ||
2648 | depth = ext_depth(inode); | 2964 | depth = ext_depth(inode); |
2649 | eh = path[depth].p_hdr; | ||
2650 | ex = path[depth].p_ext; | 2965 | ex = path[depth].p_ext; |
2651 | ee_block = le32_to_cpu(ex->ee_block); | 2966 | ee_block = le32_to_cpu(ex->ee_block); |
2652 | ee_len = ext4_ext_get_actual_len(ex); | 2967 | ee_len = ext4_ext_get_actual_len(ex); |
2653 | allocated = ee_len - (map->m_lblk - ee_block); | 2968 | allocated = ee_len - (map->m_lblk - ee_block); |
2654 | newblock = map->m_lblk - ee_block + ext_pblock(ex); | ||
2655 | |||
2656 | ex2 = ex; | ||
2657 | orig_ex.ee_block = ex->ee_block; | ||
2658 | orig_ex.ee_len = cpu_to_le16(ee_len); | ||
2659 | ext4_ext_store_pblock(&orig_ex, ext_pblock(ex)); | ||
2660 | 2969 | ||
2970 | WARN_ON(map->m_lblk < ee_block); | ||
2661 | /* | 2971 | /* |
2662 | * It is safe to convert extent to initialized via explicit | 2972 | * It is safe to convert extent to initialized via explicit |
2663 | * zeroout only if extent is fully insde i_size or new_size. | 2973 | * zeroout only if extent is fully insde i_size or new_size. |
2664 | */ | 2974 | */ |
2665 | may_zeroout = ee_block + ee_len <= eof_block; | 2975 | split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0; |
2666 | 2976 | ||
2667 | err = ext4_ext_get_access(handle, inode, path + depth); | ||
2668 | if (err) | ||
2669 | goto out; | ||
2670 | /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */ | 2977 | /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */ |
2671 | if (ee_len <= 2*EXT4_EXT_ZERO_LEN && may_zeroout) { | 2978 | if (ee_len <= 2*EXT4_EXT_ZERO_LEN && |
2672 | err = ext4_ext_zeroout(inode, &orig_ex); | 2979 | (EXT4_EXT_MAY_ZEROOUT & split_flag)) { |
2980 | err = ext4_ext_zeroout(inode, ex); | ||
2673 | if (err) | 2981 | if (err) |
2674 | goto fix_extent_len; | ||
2675 | /* update the extent length and mark as initialized */ | ||
2676 | ex->ee_block = orig_ex.ee_block; | ||
2677 | ex->ee_len = orig_ex.ee_len; | ||
2678 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | ||
2679 | ext4_ext_dirty(handle, inode, path + depth); | ||
2680 | /* zeroed the full extent */ | ||
2681 | return allocated; | ||
2682 | } | ||
2683 | |||
2684 | /* ex1: ee_block to map->m_lblk - 1 : uninitialized */ | ||
2685 | if (map->m_lblk > ee_block) { | ||
2686 | ex1 = ex; | ||
2687 | ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block); | ||
2688 | ext4_ext_mark_uninitialized(ex1); | ||
2689 | ex2 = &newex; | ||
2690 | } | ||
2691 | /* | ||
2692 | * for sanity, update the length of the ex2 extent before | ||
2693 | * we insert ex3, if ex1 is NULL. This is to avoid temporary | ||
2694 | * overlap of blocks. | ||
2695 | */ | ||
2696 | if (!ex1 && allocated > map->m_len) | ||
2697 | ex2->ee_len = cpu_to_le16(map->m_len); | ||
2698 | /* ex3: to ee_block + ee_len : uninitialised */ | ||
2699 | if (allocated > map->m_len) { | ||
2700 | unsigned int newdepth; | ||
2701 | /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */ | ||
2702 | if (allocated <= EXT4_EXT_ZERO_LEN && may_zeroout) { | ||
2703 | /* | ||
2704 | * map->m_lblk == ee_block is handled by the zerouout | ||
2705 | * at the beginning. | ||
2706 | * Mark first half uninitialized. | ||
2707 | * Mark second half initialized and zero out the | ||
2708 | * initialized extent | ||
2709 | */ | ||
2710 | ex->ee_block = orig_ex.ee_block; | ||
2711 | ex->ee_len = cpu_to_le16(ee_len - allocated); | ||
2712 | ext4_ext_mark_uninitialized(ex); | ||
2713 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | ||
2714 | ext4_ext_dirty(handle, inode, path + depth); | ||
2715 | |||
2716 | ex3 = &newex; | ||
2717 | ex3->ee_block = cpu_to_le32(map->m_lblk); | ||
2718 | ext4_ext_store_pblock(ex3, newblock); | ||
2719 | ex3->ee_len = cpu_to_le16(allocated); | ||
2720 | err = ext4_ext_insert_extent(handle, inode, path, | ||
2721 | ex3, 0); | ||
2722 | if (err == -ENOSPC) { | ||
2723 | err = ext4_ext_zeroout(inode, &orig_ex); | ||
2724 | if (err) | ||
2725 | goto fix_extent_len; | ||
2726 | ex->ee_block = orig_ex.ee_block; | ||
2727 | ex->ee_len = orig_ex.ee_len; | ||
2728 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | ||
2729 | ext4_ext_dirty(handle, inode, path + depth); | ||
2730 | /* blocks available from map->m_lblk */ | ||
2731 | return allocated; | ||
2732 | |||
2733 | } else if (err) | ||
2734 | goto fix_extent_len; | ||
2735 | |||
2736 | /* | ||
2737 | * We need to zero out the second half because | ||
2738 | * an fallocate request can update file size and | ||
2739 | * converting the second half to initialized extent | ||
2740 | * implies that we can leak some junk data to user | ||
2741 | * space. | ||
2742 | */ | ||
2743 | err = ext4_ext_zeroout(inode, ex3); | ||
2744 | if (err) { | ||
2745 | /* | ||
2746 | * We should actually mark the | ||
2747 | * second half as uninit and return error | ||
2748 | * Insert would have changed the extent | ||
2749 | */ | ||
2750 | depth = ext_depth(inode); | ||
2751 | ext4_ext_drop_refs(path); | ||
2752 | path = ext4_ext_find_extent(inode, map->m_lblk, | ||
2753 | path); | ||
2754 | if (IS_ERR(path)) { | ||
2755 | err = PTR_ERR(path); | ||
2756 | return err; | ||
2757 | } | ||
2758 | /* get the second half extent details */ | ||
2759 | ex = path[depth].p_ext; | ||
2760 | err = ext4_ext_get_access(handle, inode, | ||
2761 | path + depth); | ||
2762 | if (err) | ||
2763 | return err; | ||
2764 | ext4_ext_mark_uninitialized(ex); | ||
2765 | ext4_ext_dirty(handle, inode, path + depth); | ||
2766 | return err; | ||
2767 | } | ||
2768 | |||
2769 | /* zeroed the second half */ | ||
2770 | return allocated; | ||
2771 | } | ||
2772 | ex3 = &newex; | ||
2773 | ex3->ee_block = cpu_to_le32(map->m_lblk + map->m_len); | ||
2774 | ext4_ext_store_pblock(ex3, newblock + map->m_len); | ||
2775 | ex3->ee_len = cpu_to_le16(allocated - map->m_len); | ||
2776 | ext4_ext_mark_uninitialized(ex3); | ||
2777 | err = ext4_ext_insert_extent(handle, inode, path, ex3, 0); | ||
2778 | if (err == -ENOSPC && may_zeroout) { | ||
2779 | err = ext4_ext_zeroout(inode, &orig_ex); | ||
2780 | if (err) | ||
2781 | goto fix_extent_len; | ||
2782 | /* update the extent length and mark as initialized */ | ||
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_dirty(handle, inode, path + depth); | ||
2787 | /* zeroed the full extent */ | ||
2788 | /* blocks available from map->m_lblk */ | ||
2789 | return allocated; | ||
2790 | |||
2791 | } else if (err) | ||
2792 | goto fix_extent_len; | ||
2793 | /* | ||
2794 | * The depth, and hence eh & ex might change | ||
2795 | * as part of the insert above. | ||
2796 | */ | ||
2797 | newdepth = ext_depth(inode); | ||
2798 | /* | ||
2799 | * update the extent length after successful insert of the | ||
2800 | * split extent | ||
2801 | */ | ||
2802 | ee_len -= ext4_ext_get_actual_len(ex3); | ||
2803 | orig_ex.ee_len = cpu_to_le16(ee_len); | ||
2804 | may_zeroout = ee_block + ee_len <= eof_block; | ||
2805 | |||
2806 | depth = newdepth; | ||
2807 | ext4_ext_drop_refs(path); | ||
2808 | path = ext4_ext_find_extent(inode, map->m_lblk, path); | ||
2809 | if (IS_ERR(path)) { | ||
2810 | err = PTR_ERR(path); | ||
2811 | goto out; | 2982 | goto out; |
2812 | } | ||
2813 | eh = path[depth].p_hdr; | ||
2814 | ex = path[depth].p_ext; | ||
2815 | if (ex2 != &newex) | ||
2816 | ex2 = ex; | ||
2817 | 2983 | ||
2818 | err = ext4_ext_get_access(handle, inode, path + depth); | 2984 | err = ext4_ext_get_access(handle, inode, path + depth); |
2819 | if (err) | 2985 | if (err) |
2820 | goto out; | 2986 | goto out; |
2821 | 2987 | ext4_ext_mark_initialized(ex); | |
2822 | allocated = map->m_len; | 2988 | ext4_ext_try_to_merge(inode, path, ex); |
2823 | 2989 | err = ext4_ext_dirty(handle, inode, path + depth); | |
2824 | /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying | 2990 | goto out; |
2825 | * to insert a extent in the middle zerout directly | ||
2826 | * otherwise give the extent a chance to merge to left | ||
2827 | */ | ||
2828 | if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN && | ||
2829 | map->m_lblk != ee_block && may_zeroout) { | ||
2830 | err = ext4_ext_zeroout(inode, &orig_ex); | ||
2831 | if (err) | ||
2832 | goto fix_extent_len; | ||
2833 | /* update the extent length and mark as initialized */ | ||
2834 | ex->ee_block = orig_ex.ee_block; | ||
2835 | ex->ee_len = orig_ex.ee_len; | ||
2836 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | ||
2837 | ext4_ext_dirty(handle, inode, path + depth); | ||
2838 | /* zero out the first half */ | ||
2839 | /* blocks available from map->m_lblk */ | ||
2840 | return allocated; | ||
2841 | } | ||
2842 | } | ||
2843 | /* | ||
2844 | * If there was a change of depth as part of the | ||
2845 | * insertion of ex3 above, we need to update the length | ||
2846 | * of the ex1 extent again here | ||
2847 | */ | ||
2848 | if (ex1 && ex1 != ex) { | ||
2849 | ex1 = ex; | ||
2850 | ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block); | ||
2851 | ext4_ext_mark_uninitialized(ex1); | ||
2852 | ex2 = &newex; | ||
2853 | } | ||
2854 | /* ex2: map->m_lblk to map->m_lblk + maxblocks-1 : initialised */ | ||
2855 | ex2->ee_block = cpu_to_le32(map->m_lblk); | ||
2856 | ext4_ext_store_pblock(ex2, newblock); | ||
2857 | ex2->ee_len = cpu_to_le16(allocated); | ||
2858 | if (ex2 != ex) | ||
2859 | goto insert; | ||
2860 | /* | ||
2861 | * New (initialized) extent starts from the first block | ||
2862 | * in the current extent. i.e., ex2 == ex | ||
2863 | * We have to see if it can be merged with the extent | ||
2864 | * on the left. | ||
2865 | */ | ||
2866 | if (ex2 > EXT_FIRST_EXTENT(eh)) { | ||
2867 | /* | ||
2868 | * To merge left, pass "ex2 - 1" to try_to_merge(), | ||
2869 | * since it merges towards right _only_. | ||
2870 | */ | ||
2871 | ret = ext4_ext_try_to_merge(inode, path, ex2 - 1); | ||
2872 | if (ret) { | ||
2873 | err = ext4_ext_correct_indexes(handle, inode, path); | ||
2874 | if (err) | ||
2875 | goto out; | ||
2876 | depth = ext_depth(inode); | ||
2877 | ex2--; | ||
2878 | } | ||
2879 | } | 2991 | } |
2992 | |||
2880 | /* | 2993 | /* |
2881 | * Try to Merge towards right. This might be required | 2994 | * four cases: |
2882 | * only when the whole extent is being written to. | 2995 | * 1. split the extent into three extents. |
2883 | * i.e. ex2 == ex and ex3 == NULL. | 2996 | * 2. split the extent into two extents, zeroout the first half. |
2997 | * 3. split the extent into two extents, zeroout the second half. | ||
2998 | * 4. split the extent into two extents with out zeroout. | ||
2884 | */ | 2999 | */ |
2885 | if (!ex3) { | 3000 | split_map.m_lblk = map->m_lblk; |
2886 | ret = ext4_ext_try_to_merge(inode, path, ex2); | 3001 | split_map.m_len = map->m_len; |
2887 | if (ret) { | 3002 | |
2888 | err = ext4_ext_correct_indexes(handle, inode, path); | 3003 | if (allocated > map->m_len) { |
3004 | if (allocated <= EXT4_EXT_ZERO_LEN && | ||
3005 | (EXT4_EXT_MAY_ZEROOUT & split_flag)) { | ||
3006 | /* case 3 */ | ||
3007 | zero_ex.ee_block = | ||
3008 | cpu_to_le32(map->m_lblk); | ||
3009 | zero_ex.ee_len = cpu_to_le16(allocated); | ||
3010 | ext4_ext_store_pblock(&zero_ex, | ||
3011 | ext4_ext_pblock(ex) + map->m_lblk - ee_block); | ||
3012 | err = ext4_ext_zeroout(inode, &zero_ex); | ||
2889 | if (err) | 3013 | if (err) |
2890 | goto out; | 3014 | goto out; |
3015 | split_map.m_lblk = map->m_lblk; | ||
3016 | split_map.m_len = allocated; | ||
3017 | } else if ((map->m_lblk - ee_block + map->m_len < | ||
3018 | EXT4_EXT_ZERO_LEN) && | ||
3019 | (EXT4_EXT_MAY_ZEROOUT & split_flag)) { | ||
3020 | /* case 2 */ | ||
3021 | if (map->m_lblk != ee_block) { | ||
3022 | zero_ex.ee_block = ex->ee_block; | ||
3023 | zero_ex.ee_len = cpu_to_le16(map->m_lblk - | ||
3024 | ee_block); | ||
3025 | ext4_ext_store_pblock(&zero_ex, | ||
3026 | ext4_ext_pblock(ex)); | ||
3027 | err = ext4_ext_zeroout(inode, &zero_ex); | ||
3028 | if (err) | ||
3029 | goto out; | ||
3030 | } | ||
3031 | |||
3032 | split_map.m_lblk = ee_block; | ||
3033 | split_map.m_len = map->m_lblk - ee_block + map->m_len; | ||
3034 | allocated = map->m_len; | ||
2891 | } | 3035 | } |
2892 | } | 3036 | } |
2893 | /* Mark modified extent as dirty */ | 3037 | |
2894 | err = ext4_ext_dirty(handle, inode, path + depth); | 3038 | allocated = ext4_split_extent(handle, inode, path, |
2895 | goto out; | 3039 | &split_map, split_flag, 0); |
2896 | insert: | 3040 | if (allocated < 0) |
2897 | err = ext4_ext_insert_extent(handle, inode, path, &newex, 0); | 3041 | err = allocated; |
2898 | if (err == -ENOSPC && may_zeroout) { | 3042 | |
2899 | err = ext4_ext_zeroout(inode, &orig_ex); | ||
2900 | if (err) | ||
2901 | goto fix_extent_len; | ||
2902 | /* update the extent length and mark as initialized */ | ||
2903 | ex->ee_block = orig_ex.ee_block; | ||
2904 | ex->ee_len = orig_ex.ee_len; | ||
2905 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | ||
2906 | ext4_ext_dirty(handle, inode, path + depth); | ||
2907 | /* zero out the first half */ | ||
2908 | return allocated; | ||
2909 | } else if (err) | ||
2910 | goto fix_extent_len; | ||
2911 | out: | 3043 | out: |
2912 | ext4_ext_show_leaf(inode, path); | ||
2913 | return err ? err : allocated; | 3044 | return err ? err : allocated; |
2914 | |||
2915 | fix_extent_len: | ||
2916 | ex->ee_block = orig_ex.ee_block; | ||
2917 | ex->ee_len = orig_ex.ee_len; | ||
2918 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | ||
2919 | ext4_ext_mark_uninitialized(ex); | ||
2920 | ext4_ext_dirty(handle, inode, path + depth); | ||
2921 | return err; | ||
2922 | } | 3045 | } |
2923 | 3046 | ||
2924 | /* | 3047 | /* |
@@ -2926,15 +3049,15 @@ fix_extent_len: | |||
2926 | * ext4_get_blocks_dio_write() when DIO to write | 3049 | * ext4_get_blocks_dio_write() when DIO to write |
2927 | * to an uninitialized extent. | 3050 | * to an uninitialized extent. |
2928 | * | 3051 | * |
2929 | * Writing to an uninitized extent may result in splitting the uninitialized | 3052 | * Writing to an uninitialized extent may result in splitting the uninitialized |
2930 | * extent into multiple /intialized unintialized extents (up to three) | 3053 | * extent into multiple /initialized uninitialized extents (up to three) |
2931 | * There are three possibilities: | 3054 | * There are three possibilities: |
2932 | * a> There is no split required: Entire extent should be uninitialized | 3055 | * a> There is no split required: Entire extent should be uninitialized |
2933 | * b> Splits in two extents: Write is happening at either end of the extent | 3056 | * b> Splits in two extents: Write is happening at either end of the extent |
2934 | * c> Splits in three extents: Somone is writing in middle of the extent | 3057 | * c> Splits in three extents: Somone is writing in middle of the extent |
2935 | * | 3058 | * |
2936 | * One of more index blocks maybe needed if the extent tree grow after | 3059 | * One of more index blocks maybe needed if the extent tree grow after |
2937 | * the unintialized extent split. To prevent ENOSPC occur at the IO | 3060 | * the uninitialized extent split. To prevent ENOSPC occur at the IO |
2938 | * complete, we need to split the uninitialized extent before DIO submit | 3061 | * complete, we need to split the uninitialized extent before DIO submit |
2939 | * the IO. The uninitialized extent called at this time will be split | 3062 | * the IO. The uninitialized extent called at this time will be split |
2940 | * into three uninitialized extent(at most). After IO complete, the part | 3063 | * into three uninitialized extent(at most). After IO complete, the part |
@@ -2949,15 +3072,11 @@ static int ext4_split_unwritten_extents(handle_t *handle, | |||
2949 | struct ext4_ext_path *path, | 3072 | struct ext4_ext_path *path, |
2950 | int flags) | 3073 | int flags) |
2951 | { | 3074 | { |
2952 | struct ext4_extent *ex, newex, orig_ex; | 3075 | ext4_lblk_t eof_block; |
2953 | struct ext4_extent *ex1 = NULL; | 3076 | ext4_lblk_t ee_block; |
2954 | struct ext4_extent *ex2 = NULL; | 3077 | struct ext4_extent *ex; |
2955 | struct ext4_extent *ex3 = NULL; | 3078 | unsigned int ee_len; |
2956 | ext4_lblk_t ee_block, eof_block; | 3079 | int split_flag = 0, depth; |
2957 | unsigned int allocated, ee_len, depth; | ||
2958 | ext4_fsblk_t newblock; | ||
2959 | int err = 0; | ||
2960 | int may_zeroout; | ||
2961 | 3080 | ||
2962 | ext_debug("ext4_split_unwritten_extents: inode %lu, logical" | 3081 | ext_debug("ext4_split_unwritten_extents: inode %lu, logical" |
2963 | "block %llu, max_blocks %u\n", inode->i_ino, | 3082 | "block %llu, max_blocks %u\n", inode->i_ino, |
@@ -2967,156 +3086,22 @@ static int ext4_split_unwritten_extents(handle_t *handle, | |||
2967 | inode->i_sb->s_blocksize_bits; | 3086 | inode->i_sb->s_blocksize_bits; |
2968 | if (eof_block < map->m_lblk + map->m_len) | 3087 | if (eof_block < map->m_lblk + map->m_len) |
2969 | eof_block = map->m_lblk + map->m_len; | 3088 | eof_block = map->m_lblk + map->m_len; |
2970 | |||
2971 | depth = ext_depth(inode); | ||
2972 | ex = path[depth].p_ext; | ||
2973 | ee_block = le32_to_cpu(ex->ee_block); | ||
2974 | ee_len = ext4_ext_get_actual_len(ex); | ||
2975 | allocated = ee_len - (map->m_lblk - ee_block); | ||
2976 | newblock = map->m_lblk - ee_block + ext_pblock(ex); | ||
2977 | |||
2978 | ex2 = ex; | ||
2979 | orig_ex.ee_block = ex->ee_block; | ||
2980 | orig_ex.ee_len = cpu_to_le16(ee_len); | ||
2981 | ext4_ext_store_pblock(&orig_ex, ext_pblock(ex)); | ||
2982 | |||
2983 | /* | 3089 | /* |
2984 | * It is safe to convert extent to initialized via explicit | 3090 | * It is safe to convert extent to initialized via explicit |
2985 | * zeroout only if extent is fully insde i_size or new_size. | 3091 | * zeroout only if extent is fully insde i_size or new_size. |
2986 | */ | 3092 | */ |
2987 | may_zeroout = ee_block + ee_len <= eof_block; | 3093 | depth = ext_depth(inode); |
2988 | 3094 | ex = path[depth].p_ext; | |
2989 | /* | 3095 | ee_block = le32_to_cpu(ex->ee_block); |
2990 | * If the uninitialized extent begins at the same logical | 3096 | ee_len = ext4_ext_get_actual_len(ex); |
2991 | * block where the write begins, and the write completely | ||
2992 | * covers the extent, then we don't need to split it. | ||
2993 | */ | ||
2994 | if ((map->m_lblk == ee_block) && (allocated <= map->m_len)) | ||
2995 | return allocated; | ||
2996 | |||
2997 | err = ext4_ext_get_access(handle, inode, path + depth); | ||
2998 | if (err) | ||
2999 | goto out; | ||
3000 | /* ex1: ee_block to map->m_lblk - 1 : uninitialized */ | ||
3001 | if (map->m_lblk > ee_block) { | ||
3002 | ex1 = ex; | ||
3003 | ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block); | ||
3004 | ext4_ext_mark_uninitialized(ex1); | ||
3005 | ex2 = &newex; | ||
3006 | } | ||
3007 | /* | ||
3008 | * for sanity, update the length of the ex2 extent before | ||
3009 | * we insert ex3, if ex1 is NULL. This is to avoid temporary | ||
3010 | * overlap of blocks. | ||
3011 | */ | ||
3012 | if (!ex1 && allocated > map->m_len) | ||
3013 | ex2->ee_len = cpu_to_le16(map->m_len); | ||
3014 | /* ex3: to ee_block + ee_len : uninitialised */ | ||
3015 | if (allocated > map->m_len) { | ||
3016 | unsigned int newdepth; | ||
3017 | ex3 = &newex; | ||
3018 | ex3->ee_block = cpu_to_le32(map->m_lblk + map->m_len); | ||
3019 | ext4_ext_store_pblock(ex3, newblock + map->m_len); | ||
3020 | ex3->ee_len = cpu_to_le16(allocated - map->m_len); | ||
3021 | ext4_ext_mark_uninitialized(ex3); | ||
3022 | err = ext4_ext_insert_extent(handle, inode, path, ex3, flags); | ||
3023 | if (err == -ENOSPC && may_zeroout) { | ||
3024 | err = ext4_ext_zeroout(inode, &orig_ex); | ||
3025 | if (err) | ||
3026 | goto fix_extent_len; | ||
3027 | /* update the extent length and mark as initialized */ | ||
3028 | ex->ee_block = orig_ex.ee_block; | ||
3029 | ex->ee_len = orig_ex.ee_len; | ||
3030 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | ||
3031 | ext4_ext_dirty(handle, inode, path + depth); | ||
3032 | /* zeroed the full extent */ | ||
3033 | /* blocks available from map->m_lblk */ | ||
3034 | return allocated; | ||
3035 | |||
3036 | } else if (err) | ||
3037 | goto fix_extent_len; | ||
3038 | /* | ||
3039 | * The depth, and hence eh & ex might change | ||
3040 | * as part of the insert above. | ||
3041 | */ | ||
3042 | newdepth = ext_depth(inode); | ||
3043 | /* | ||
3044 | * update the extent length after successful insert of the | ||
3045 | * split extent | ||
3046 | */ | ||
3047 | ee_len -= ext4_ext_get_actual_len(ex3); | ||
3048 | orig_ex.ee_len = cpu_to_le16(ee_len); | ||
3049 | may_zeroout = ee_block + ee_len <= eof_block; | ||
3050 | |||
3051 | depth = newdepth; | ||
3052 | ext4_ext_drop_refs(path); | ||
3053 | path = ext4_ext_find_extent(inode, map->m_lblk, path); | ||
3054 | if (IS_ERR(path)) { | ||
3055 | err = PTR_ERR(path); | ||
3056 | goto out; | ||
3057 | } | ||
3058 | ex = path[depth].p_ext; | ||
3059 | if (ex2 != &newex) | ||
3060 | ex2 = ex; | ||
3061 | |||
3062 | err = ext4_ext_get_access(handle, inode, path + depth); | ||
3063 | if (err) | ||
3064 | goto out; | ||
3065 | 3097 | ||
3066 | allocated = map->m_len; | 3098 | split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0; |
3067 | } | 3099 | split_flag |= EXT4_EXT_MARK_UNINIT2; |
3068 | /* | ||
3069 | * If there was a change of depth as part of the | ||
3070 | * insertion of ex3 above, we need to update the length | ||
3071 | * of the ex1 extent again here | ||
3072 | */ | ||
3073 | if (ex1 && ex1 != ex) { | ||
3074 | ex1 = ex; | ||
3075 | ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block); | ||
3076 | ext4_ext_mark_uninitialized(ex1); | ||
3077 | ex2 = &newex; | ||
3078 | } | ||
3079 | /* | ||
3080 | * ex2: map->m_lblk to map->m_lblk + map->m_len-1 : to be written | ||
3081 | * using direct I/O, uninitialised still. | ||
3082 | */ | ||
3083 | ex2->ee_block = cpu_to_le32(map->m_lblk); | ||
3084 | ext4_ext_store_pblock(ex2, newblock); | ||
3085 | ex2->ee_len = cpu_to_le16(allocated); | ||
3086 | ext4_ext_mark_uninitialized(ex2); | ||
3087 | if (ex2 != ex) | ||
3088 | goto insert; | ||
3089 | /* Mark modified extent as dirty */ | ||
3090 | err = ext4_ext_dirty(handle, inode, path + depth); | ||
3091 | ext_debug("out here\n"); | ||
3092 | goto out; | ||
3093 | insert: | ||
3094 | err = ext4_ext_insert_extent(handle, inode, path, &newex, flags); | ||
3095 | if (err == -ENOSPC && may_zeroout) { | ||
3096 | err = ext4_ext_zeroout(inode, &orig_ex); | ||
3097 | if (err) | ||
3098 | goto fix_extent_len; | ||
3099 | /* update the extent length and mark as initialized */ | ||
3100 | ex->ee_block = orig_ex.ee_block; | ||
3101 | ex->ee_len = orig_ex.ee_len; | ||
3102 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | ||
3103 | ext4_ext_dirty(handle, inode, path + depth); | ||
3104 | /* zero out the first half */ | ||
3105 | return allocated; | ||
3106 | } else if (err) | ||
3107 | goto fix_extent_len; | ||
3108 | out: | ||
3109 | ext4_ext_show_leaf(inode, path); | ||
3110 | return err ? err : allocated; | ||
3111 | 3100 | ||
3112 | fix_extent_len: | 3101 | flags |= EXT4_GET_BLOCKS_PRE_IO; |
3113 | ex->ee_block = orig_ex.ee_block; | 3102 | return ext4_split_extent(handle, inode, path, map, split_flag, flags); |
3114 | ex->ee_len = orig_ex.ee_len; | ||
3115 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | ||
3116 | ext4_ext_mark_uninitialized(ex); | ||
3117 | ext4_ext_dirty(handle, inode, path + depth); | ||
3118 | return err; | ||
3119 | } | 3103 | } |
3104 | |||
3120 | static int ext4_convert_unwritten_extents_endio(handle_t *handle, | 3105 | static int ext4_convert_unwritten_extents_endio(handle_t *handle, |
3121 | struct inode *inode, | 3106 | struct inode *inode, |
3122 | struct ext4_ext_path *path) | 3107 | struct ext4_ext_path *path) |
@@ -3125,46 +3110,27 @@ static int ext4_convert_unwritten_extents_endio(handle_t *handle, | |||
3125 | struct ext4_extent_header *eh; | 3110 | struct ext4_extent_header *eh; |
3126 | int depth; | 3111 | int depth; |
3127 | int err = 0; | 3112 | int err = 0; |
3128 | int ret = 0; | ||
3129 | 3113 | ||
3130 | depth = ext_depth(inode); | 3114 | depth = ext_depth(inode); |
3131 | eh = path[depth].p_hdr; | 3115 | eh = path[depth].p_hdr; |
3132 | ex = path[depth].p_ext; | 3116 | ex = path[depth].p_ext; |
3133 | 3117 | ||
3118 | ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical" | ||
3119 | "block %llu, max_blocks %u\n", inode->i_ino, | ||
3120 | (unsigned long long)le32_to_cpu(ex->ee_block), | ||
3121 | ext4_ext_get_actual_len(ex)); | ||
3122 | |||
3134 | err = ext4_ext_get_access(handle, inode, path + depth); | 3123 | err = ext4_ext_get_access(handle, inode, path + depth); |
3135 | if (err) | 3124 | if (err) |
3136 | goto out; | 3125 | goto out; |
3137 | /* first mark the extent as initialized */ | 3126 | /* first mark the extent as initialized */ |
3138 | ext4_ext_mark_initialized(ex); | 3127 | ext4_ext_mark_initialized(ex); |
3139 | 3128 | ||
3140 | /* | 3129 | /* note: ext4_ext_correct_indexes() isn't needed here because |
3141 | * We have to see if it can be merged with the extent | 3130 | * borders are not changed |
3142 | * on the left. | ||
3143 | */ | ||
3144 | if (ex > EXT_FIRST_EXTENT(eh)) { | ||
3145 | /* | ||
3146 | * To merge left, pass "ex - 1" to try_to_merge(), | ||
3147 | * since it merges towards right _only_. | ||
3148 | */ | ||
3149 | ret = ext4_ext_try_to_merge(inode, path, ex - 1); | ||
3150 | if (ret) { | ||
3151 | err = ext4_ext_correct_indexes(handle, inode, path); | ||
3152 | if (err) | ||
3153 | goto out; | ||
3154 | depth = ext_depth(inode); | ||
3155 | ex--; | ||
3156 | } | ||
3157 | } | ||
3158 | /* | ||
3159 | * Try to Merge towards right. | ||
3160 | */ | 3131 | */ |
3161 | ret = ext4_ext_try_to_merge(inode, path, ex); | 3132 | ext4_ext_try_to_merge(inode, path, ex); |
3162 | if (ret) { | 3133 | |
3163 | err = ext4_ext_correct_indexes(handle, inode, path); | ||
3164 | if (err) | ||
3165 | goto out; | ||
3166 | depth = ext_depth(inode); | ||
3167 | } | ||
3168 | /* Mark modified extent as dirty */ | 3134 | /* Mark modified extent as dirty */ |
3169 | err = ext4_ext_dirty(handle, inode, path + depth); | 3135 | err = ext4_ext_dirty(handle, inode, path + depth); |
3170 | out: | 3136 | out: |
@@ -3180,6 +3146,56 @@ static void unmap_underlying_metadata_blocks(struct block_device *bdev, | |||
3180 | unmap_underlying_metadata(bdev, block + i); | 3146 | unmap_underlying_metadata(bdev, block + i); |
3181 | } | 3147 | } |
3182 | 3148 | ||
3149 | /* | ||
3150 | * Handle EOFBLOCKS_FL flag, clearing it if necessary | ||
3151 | */ | ||
3152 | static int check_eofblocks_fl(handle_t *handle, struct inode *inode, | ||
3153 | ext4_lblk_t lblk, | ||
3154 | struct ext4_ext_path *path, | ||
3155 | unsigned int len) | ||
3156 | { | ||
3157 | int i, depth; | ||
3158 | struct ext4_extent_header *eh; | ||
3159 | struct ext4_extent *last_ex; | ||
3160 | |||
3161 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS)) | ||
3162 | return 0; | ||
3163 | |||
3164 | depth = ext_depth(inode); | ||
3165 | eh = path[depth].p_hdr; | ||
3166 | |||
3167 | if (unlikely(!eh->eh_entries)) { | ||
3168 | EXT4_ERROR_INODE(inode, "eh->eh_entries == 0 and " | ||
3169 | "EOFBLOCKS_FL set"); | ||
3170 | return -EIO; | ||
3171 | } | ||
3172 | last_ex = EXT_LAST_EXTENT(eh); | ||
3173 | /* | ||
3174 | * We should clear the EOFBLOCKS_FL flag if we are writing the | ||
3175 | * last block in the last extent in the file. We test this by | ||
3176 | * first checking to see if the caller to | ||
3177 | * ext4_ext_get_blocks() was interested in the last block (or | ||
3178 | * a block beyond the last block) in the current extent. If | ||
3179 | * this turns out to be false, we can bail out from this | ||
3180 | * function immediately. | ||
3181 | */ | ||
3182 | if (lblk + len < le32_to_cpu(last_ex->ee_block) + | ||
3183 | ext4_ext_get_actual_len(last_ex)) | ||
3184 | return 0; | ||
3185 | /* | ||
3186 | * If the caller does appear to be planning to write at or | ||
3187 | * beyond the end of the current extent, we then test to see | ||
3188 | * if the current extent is the last extent in the file, by | ||
3189 | * checking to make sure it was reached via the rightmost node | ||
3190 | * at each level of the tree. | ||
3191 | */ | ||
3192 | for (i = depth-1; i >= 0; i--) | ||
3193 | if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr)) | ||
3194 | return 0; | ||
3195 | ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); | ||
3196 | return ext4_mark_inode_dirty(handle, inode); | ||
3197 | } | ||
3198 | |||
3183 | static int | 3199 | static int |
3184 | ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode, | 3200 | ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode, |
3185 | struct ext4_map_blocks *map, | 3201 | struct ext4_map_blocks *map, |
@@ -3202,12 +3218,13 @@ ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode, | |||
3202 | path, flags); | 3218 | path, flags); |
3203 | /* | 3219 | /* |
3204 | * Flag the inode(non aio case) or end_io struct (aio case) | 3220 | * Flag the inode(non aio case) or end_io struct (aio case) |
3205 | * that this IO needs to convertion to written when IO is | 3221 | * that this IO needs to conversion to written when IO is |
3206 | * completed | 3222 | * completed |
3207 | */ | 3223 | */ |
3208 | if (io) | 3224 | if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) { |
3209 | io->flag = EXT4_IO_UNWRITTEN; | 3225 | io->flag = EXT4_IO_END_UNWRITTEN; |
3210 | else | 3226 | atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten); |
3227 | } else | ||
3211 | ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); | 3228 | ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); |
3212 | if (ext4_should_dioread_nolock(inode)) | 3229 | if (ext4_should_dioread_nolock(inode)) |
3213 | map->m_flags |= EXT4_MAP_UNINIT; | 3230 | map->m_flags |= EXT4_MAP_UNINIT; |
@@ -3217,8 +3234,12 @@ ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode, | |||
3217 | if ((flags & EXT4_GET_BLOCKS_CONVERT)) { | 3234 | if ((flags & EXT4_GET_BLOCKS_CONVERT)) { |
3218 | ret = ext4_convert_unwritten_extents_endio(handle, inode, | 3235 | ret = ext4_convert_unwritten_extents_endio(handle, inode, |
3219 | path); | 3236 | path); |
3220 | if (ret >= 0) | 3237 | if (ret >= 0) { |
3221 | ext4_update_inode_fsync_trans(handle, inode, 1); | 3238 | ext4_update_inode_fsync_trans(handle, inode, 1); |
3239 | err = check_eofblocks_fl(handle, inode, map->m_lblk, | ||
3240 | path, map->m_len); | ||
3241 | } else | ||
3242 | err = ret; | ||
3222 | goto out2; | 3243 | goto out2; |
3223 | } | 3244 | } |
3224 | /* buffered IO case */ | 3245 | /* buffered IO case */ |
@@ -3244,8 +3265,14 @@ ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode, | |||
3244 | 3265 | ||
3245 | /* buffered write, writepage time, convert*/ | 3266 | /* buffered write, writepage time, convert*/ |
3246 | ret = ext4_ext_convert_to_initialized(handle, inode, map, path); | 3267 | ret = ext4_ext_convert_to_initialized(handle, inode, map, path); |
3247 | if (ret >= 0) | 3268 | if (ret >= 0) { |
3248 | ext4_update_inode_fsync_trans(handle, inode, 1); | 3269 | ext4_update_inode_fsync_trans(handle, inode, 1); |
3270 | err = check_eofblocks_fl(handle, inode, map->m_lblk, path, | ||
3271 | map->m_len); | ||
3272 | if (err < 0) | ||
3273 | goto out2; | ||
3274 | } | ||
3275 | |||
3249 | out: | 3276 | out: |
3250 | if (ret <= 0) { | 3277 | if (ret <= 0) { |
3251 | err = ret; | 3278 | err = ret; |
@@ -3292,6 +3319,7 @@ out2: | |||
3292 | } | 3319 | } |
3293 | return err ? err : allocated; | 3320 | return err ? err : allocated; |
3294 | } | 3321 | } |
3322 | |||
3295 | /* | 3323 | /* |
3296 | * Block allocation/map/preallocation routine for extents based files | 3324 | * Block allocation/map/preallocation routine for extents based files |
3297 | * | 3325 | * |
@@ -3314,21 +3342,24 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, | |||
3314 | struct ext4_map_blocks *map, int flags) | 3342 | struct ext4_map_blocks *map, int flags) |
3315 | { | 3343 | { |
3316 | struct ext4_ext_path *path = NULL; | 3344 | struct ext4_ext_path *path = NULL; |
3317 | struct ext4_extent_header *eh; | 3345 | struct ext4_extent newex, *ex; |
3318 | struct ext4_extent newex, *ex, *last_ex; | 3346 | ext4_fsblk_t newblock = 0; |
3319 | ext4_fsblk_t newblock; | 3347 | int err = 0, depth, ret; |
3320 | int i, err = 0, depth, ret, cache_type; | ||
3321 | unsigned int allocated = 0; | 3348 | unsigned int allocated = 0; |
3349 | unsigned int punched_out = 0; | ||
3350 | unsigned int result = 0; | ||
3322 | struct ext4_allocation_request ar; | 3351 | struct ext4_allocation_request ar; |
3323 | ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio; | 3352 | ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio; |
3353 | struct ext4_map_blocks punch_map; | ||
3324 | 3354 | ||
3325 | ext_debug("blocks %u/%u requested for inode %lu\n", | 3355 | ext_debug("blocks %u/%u requested for inode %lu\n", |
3326 | map->m_lblk, map->m_len, inode->i_ino); | 3356 | map->m_lblk, map->m_len, inode->i_ino); |
3357 | trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags); | ||
3327 | 3358 | ||
3328 | /* check in cache */ | 3359 | /* check in cache */ |
3329 | cache_type = ext4_ext_in_cache(inode, map->m_lblk, &newex); | 3360 | if (ext4_ext_in_cache(inode, map->m_lblk, &newex) && |
3330 | if (cache_type) { | 3361 | ((flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) == 0)) { |
3331 | if (cache_type == EXT4_EXT_CACHE_GAP) { | 3362 | if (!newex.ee_start_lo && !newex.ee_start_hi) { |
3332 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { | 3363 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
3333 | /* | 3364 | /* |
3334 | * block isn't allocated yet and | 3365 | * block isn't allocated yet and |
@@ -3337,17 +3368,15 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, | |||
3337 | goto out2; | 3368 | goto out2; |
3338 | } | 3369 | } |
3339 | /* we should allocate requested block */ | 3370 | /* we should allocate requested block */ |
3340 | } else if (cache_type == EXT4_EXT_CACHE_EXTENT) { | 3371 | } else { |
3341 | /* block is already allocated */ | 3372 | /* block is already allocated */ |
3342 | newblock = map->m_lblk | 3373 | newblock = map->m_lblk |
3343 | - le32_to_cpu(newex.ee_block) | 3374 | - le32_to_cpu(newex.ee_block) |
3344 | + ext_pblock(&newex); | 3375 | + ext4_ext_pblock(&newex); |
3345 | /* number of remaining blocks in the extent */ | 3376 | /* number of remaining blocks in the extent */ |
3346 | allocated = ext4_ext_get_actual_len(&newex) - | 3377 | allocated = ext4_ext_get_actual_len(&newex) - |
3347 | (map->m_lblk - le32_to_cpu(newex.ee_block)); | 3378 | (map->m_lblk - le32_to_cpu(newex.ee_block)); |
3348 | goto out; | 3379 | goto out; |
3349 | } else { | ||
3350 | BUG(); | ||
3351 | } | 3380 | } |
3352 | } | 3381 | } |
3353 | 3382 | ||
@@ -3374,12 +3403,11 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, | |||
3374 | err = -EIO; | 3403 | err = -EIO; |
3375 | goto out2; | 3404 | goto out2; |
3376 | } | 3405 | } |
3377 | eh = path[depth].p_hdr; | ||
3378 | 3406 | ||
3379 | ex = path[depth].p_ext; | 3407 | ex = path[depth].p_ext; |
3380 | if (ex) { | 3408 | if (ex) { |
3381 | ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); | 3409 | ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); |
3382 | ext4_fsblk_t ee_start = ext_pblock(ex); | 3410 | ext4_fsblk_t ee_start = ext4_ext_pblock(ex); |
3383 | unsigned short ee_len; | 3411 | unsigned short ee_len; |
3384 | 3412 | ||
3385 | /* | 3413 | /* |
@@ -3395,17 +3423,84 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, | |||
3395 | ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk, | 3423 | ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk, |
3396 | ee_block, ee_len, newblock); | 3424 | ee_block, ee_len, newblock); |
3397 | 3425 | ||
3398 | /* Do not put uninitialized extent in the cache */ | 3426 | if ((flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) == 0) { |
3399 | if (!ext4_ext_is_uninitialized(ex)) { | 3427 | /* |
3400 | ext4_ext_put_in_cache(inode, ee_block, | 3428 | * Do not put uninitialized extent |
3401 | ee_len, ee_start, | 3429 | * in the cache |
3402 | EXT4_EXT_CACHE_EXTENT); | 3430 | */ |
3403 | goto out; | 3431 | if (!ext4_ext_is_uninitialized(ex)) { |
3432 | ext4_ext_put_in_cache(inode, ee_block, | ||
3433 | ee_len, ee_start); | ||
3434 | goto out; | ||
3435 | } | ||
3436 | ret = ext4_ext_handle_uninitialized_extents( | ||
3437 | handle, inode, map, path, flags, | ||
3438 | allocated, newblock); | ||
3439 | return ret; | ||
3404 | } | 3440 | } |
3405 | ret = ext4_ext_handle_uninitialized_extents(handle, | 3441 | |
3406 | inode, map, path, flags, allocated, | 3442 | /* |
3407 | newblock); | 3443 | * Punch out the map length, but only to the |
3408 | return ret; | 3444 | * end of the extent |
3445 | */ | ||
3446 | punched_out = allocated < map->m_len ? | ||
3447 | allocated : map->m_len; | ||
3448 | |||
3449 | /* | ||
3450 | * Sense extents need to be converted to | ||
3451 | * uninitialized, they must fit in an | ||
3452 | * uninitialized extent | ||
3453 | */ | ||
3454 | if (punched_out > EXT_UNINIT_MAX_LEN) | ||
3455 | punched_out = EXT_UNINIT_MAX_LEN; | ||
3456 | |||
3457 | punch_map.m_lblk = map->m_lblk; | ||
3458 | punch_map.m_pblk = newblock; | ||
3459 | punch_map.m_len = punched_out; | ||
3460 | punch_map.m_flags = 0; | ||
3461 | |||
3462 | /* Check to see if the extent needs to be split */ | ||
3463 | if (punch_map.m_len != ee_len || | ||
3464 | punch_map.m_lblk != ee_block) { | ||
3465 | |||
3466 | ret = ext4_split_extent(handle, inode, | ||
3467 | path, &punch_map, 0, | ||
3468 | EXT4_GET_BLOCKS_PUNCH_OUT_EXT | | ||
3469 | EXT4_GET_BLOCKS_PRE_IO); | ||
3470 | |||
3471 | if (ret < 0) { | ||
3472 | err = ret; | ||
3473 | goto out2; | ||
3474 | } | ||
3475 | /* | ||
3476 | * find extent for the block at | ||
3477 | * the start of the hole | ||
3478 | */ | ||
3479 | ext4_ext_drop_refs(path); | ||
3480 | kfree(path); | ||
3481 | |||
3482 | path = ext4_ext_find_extent(inode, | ||
3483 | map->m_lblk, NULL); | ||
3484 | if (IS_ERR(path)) { | ||
3485 | err = PTR_ERR(path); | ||
3486 | path = NULL; | ||
3487 | goto out2; | ||
3488 | } | ||
3489 | |||
3490 | depth = ext_depth(inode); | ||
3491 | ex = path[depth].p_ext; | ||
3492 | ee_len = ext4_ext_get_actual_len(ex); | ||
3493 | ee_block = le32_to_cpu(ex->ee_block); | ||
3494 | ee_start = ext4_ext_pblock(ex); | ||
3495 | |||
3496 | } | ||
3497 | |||
3498 | ext4_ext_mark_uninitialized(ex); | ||
3499 | |||
3500 | err = ext4_ext_remove_space(inode, map->m_lblk, | ||
3501 | map->m_lblk + punched_out); | ||
3502 | |||
3503 | goto out2; | ||
3409 | } | 3504 | } |
3410 | } | 3505 | } |
3411 | 3506 | ||
@@ -3467,6 +3562,8 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, | |||
3467 | else | 3562 | else |
3468 | /* disable in-core preallocation for non-regular files */ | 3563 | /* disable in-core preallocation for non-regular files */ |
3469 | ar.flags = 0; | 3564 | ar.flags = 0; |
3565 | if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE) | ||
3566 | ar.flags |= EXT4_MB_HINT_NOPREALLOC; | ||
3470 | newblock = ext4_mb_new_blocks(handle, &ar, &err); | 3567 | newblock = ext4_mb_new_blocks(handle, &ar, &err); |
3471 | if (!newblock) | 3568 | if (!newblock) |
3472 | goto out2; | 3569 | goto out2; |
@@ -3481,15 +3578,16 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, | |||
3481 | ext4_ext_mark_uninitialized(&newex); | 3578 | ext4_ext_mark_uninitialized(&newex); |
3482 | /* | 3579 | /* |
3483 | * io_end structure was created for every IO write to an | 3580 | * io_end structure was created for every IO write to an |
3484 | * uninitialized extent. To avoid unecessary conversion, | 3581 | * uninitialized extent. To avoid unnecessary conversion, |
3485 | * here we flag the IO that really needs the conversion. | 3582 | * here we flag the IO that really needs the conversion. |
3486 | * For non asycn direct IO case, flag the inode state | 3583 | * For non asycn direct IO case, flag the inode state |
3487 | * that we need to perform convertion when IO is done. | 3584 | * that we need to perform conversion when IO is done. |
3488 | */ | 3585 | */ |
3489 | if ((flags & EXT4_GET_BLOCKS_PRE_IO)) { | 3586 | if ((flags & EXT4_GET_BLOCKS_PRE_IO)) { |
3490 | if (io) | 3587 | if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) { |
3491 | io->flag = EXT4_IO_UNWRITTEN; | 3588 | io->flag = EXT4_IO_END_UNWRITTEN; |
3492 | else | 3589 | atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten); |
3590 | } else | ||
3493 | ext4_set_inode_state(inode, | 3591 | ext4_set_inode_state(inode, |
3494 | EXT4_STATE_DIO_UNWRITTEN); | 3592 | EXT4_STATE_DIO_UNWRITTEN); |
3495 | } | 3593 | } |
@@ -3497,44 +3595,23 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, | |||
3497 | map->m_flags |= EXT4_MAP_UNINIT; | 3595 | map->m_flags |= EXT4_MAP_UNINIT; |
3498 | } | 3596 | } |
3499 | 3597 | ||
3500 | if (unlikely(ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))) { | 3598 | err = check_eofblocks_fl(handle, inode, map->m_lblk, path, ar.len); |
3501 | if (unlikely(!eh->eh_entries)) { | 3599 | if (err) |
3502 | EXT4_ERROR_INODE(inode, | 3600 | goto out2; |
3503 | "eh->eh_entries == 0 and " | 3601 | |
3504 | "EOFBLOCKS_FL set"); | ||
3505 | err = -EIO; | ||
3506 | goto out2; | ||
3507 | } | ||
3508 | last_ex = EXT_LAST_EXTENT(eh); | ||
3509 | /* | ||
3510 | * If the current leaf block was reached by looking at | ||
3511 | * the last index block all the way down the tree, and | ||
3512 | * we are extending the inode beyond the last extent | ||
3513 | * in the current leaf block, then clear the | ||
3514 | * EOFBLOCKS_FL flag. | ||
3515 | */ | ||
3516 | for (i = depth-1; i >= 0; i--) { | ||
3517 | if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr)) | ||
3518 | break; | ||
3519 | } | ||
3520 | if ((i < 0) && | ||
3521 | (map->m_lblk + ar.len > le32_to_cpu(last_ex->ee_block) + | ||
3522 | ext4_ext_get_actual_len(last_ex))) | ||
3523 | ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); | ||
3524 | } | ||
3525 | err = ext4_ext_insert_extent(handle, inode, path, &newex, flags); | 3602 | err = ext4_ext_insert_extent(handle, inode, path, &newex, flags); |
3526 | if (err) { | 3603 | if (err) { |
3527 | /* free data blocks we just allocated */ | 3604 | /* free data blocks we just allocated */ |
3528 | /* not a good idea to call discard here directly, | 3605 | /* not a good idea to call discard here directly, |
3529 | * but otherwise we'd need to call it every free() */ | 3606 | * but otherwise we'd need to call it every free() */ |
3530 | ext4_discard_preallocations(inode); | 3607 | ext4_discard_preallocations(inode); |
3531 | ext4_free_blocks(handle, inode, 0, ext_pblock(&newex), | 3608 | ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex), |
3532 | ext4_ext_get_actual_len(&newex), 0); | 3609 | ext4_ext_get_actual_len(&newex), 0); |
3533 | goto out2; | 3610 | goto out2; |
3534 | } | 3611 | } |
3535 | 3612 | ||
3536 | /* previous routine could use block we allocated */ | 3613 | /* previous routine could use block we allocated */ |
3537 | newblock = ext_pblock(&newex); | 3614 | newblock = ext4_ext_pblock(&newex); |
3538 | allocated = ext4_ext_get_actual_len(&newex); | 3615 | allocated = ext4_ext_get_actual_len(&newex); |
3539 | if (allocated > map->m_len) | 3616 | if (allocated > map->m_len) |
3540 | allocated = map->m_len; | 3617 | allocated = map->m_len; |
@@ -3552,8 +3629,7 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, | |||
3552 | * when it is _not_ an uninitialized extent. | 3629 | * when it is _not_ an uninitialized extent. |
3553 | */ | 3630 | */ |
3554 | if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) { | 3631 | if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) { |
3555 | ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock, | 3632 | ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock); |
3556 | EXT4_EXT_CACHE_EXTENT); | ||
3557 | ext4_update_inode_fsync_trans(handle, inode, 1); | 3633 | ext4_update_inode_fsync_trans(handle, inode, 1); |
3558 | } else | 3634 | } else |
3559 | ext4_update_inode_fsync_trans(handle, inode, 0); | 3635 | ext4_update_inode_fsync_trans(handle, inode, 0); |
@@ -3569,7 +3645,13 @@ out2: | |||
3569 | ext4_ext_drop_refs(path); | 3645 | ext4_ext_drop_refs(path); |
3570 | kfree(path); | 3646 | kfree(path); |
3571 | } | 3647 | } |
3572 | return err ? err : allocated; | 3648 | trace_ext4_ext_map_blocks_exit(inode, map->m_lblk, |
3649 | newblock, map->m_len, err ? err : allocated); | ||
3650 | |||
3651 | result = (flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) ? | ||
3652 | punched_out : allocated; | ||
3653 | |||
3654 | return err ? err : result; | ||
3573 | } | 3655 | } |
3574 | 3656 | ||
3575 | void ext4_ext_truncate(struct inode *inode) | 3657 | void ext4_ext_truncate(struct inode *inode) |
@@ -3581,6 +3663,12 @@ void ext4_ext_truncate(struct inode *inode) | |||
3581 | int err = 0; | 3663 | int err = 0; |
3582 | 3664 | ||
3583 | /* | 3665 | /* |
3666 | * finish any pending end_io work so we won't run the risk of | ||
3667 | * converting any truncated blocks to initialized later | ||
3668 | */ | ||
3669 | ext4_flush_completed_IO(inode); | ||
3670 | |||
3671 | /* | ||
3584 | * probably first extent we're gonna free will be last in block | 3672 | * probably first extent we're gonna free will be last in block |
3585 | */ | 3673 | */ |
3586 | err = ext4_writepage_trans_blocks(inode); | 3674 | err = ext4_writepage_trans_blocks(inode); |
@@ -3611,7 +3699,7 @@ void ext4_ext_truncate(struct inode *inode) | |||
3611 | 3699 | ||
3612 | last_block = (inode->i_size + sb->s_blocksize - 1) | 3700 | last_block = (inode->i_size + sb->s_blocksize - 1) |
3613 | >> EXT4_BLOCK_SIZE_BITS(sb); | 3701 | >> EXT4_BLOCK_SIZE_BITS(sb); |
3614 | err = ext4_ext_remove_space(inode, last_block); | 3702 | err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1); |
3615 | 3703 | ||
3616 | /* In a multi-transaction truncate, we only make the final | 3704 | /* In a multi-transaction truncate, we only make the final |
3617 | * transaction synchronous. | 3705 | * transaction synchronous. |
@@ -3619,8 +3707,9 @@ void ext4_ext_truncate(struct inode *inode) | |||
3619 | if (IS_SYNC(inode)) | 3707 | if (IS_SYNC(inode)) |
3620 | ext4_handle_sync(handle); | 3708 | ext4_handle_sync(handle); |
3621 | 3709 | ||
3622 | out_stop: | ||
3623 | up_write(&EXT4_I(inode)->i_data_sem); | 3710 | up_write(&EXT4_I(inode)->i_data_sem); |
3711 | |||
3712 | out_stop: | ||
3624 | /* | 3713 | /* |
3625 | * If this was a simple ftruncate() and the file will remain alive, | 3714 | * If this was a simple ftruncate() and the file will remain alive, |
3626 | * then we need to clear up the orphan record which we created above. | 3715 | * then we need to clear up the orphan record which we created above. |
@@ -3667,14 +3756,15 @@ static void ext4_falloc_update_inode(struct inode *inode, | |||
3667 | } | 3756 | } |
3668 | 3757 | ||
3669 | /* | 3758 | /* |
3670 | * preallocate space for a file. This implements ext4's fallocate inode | 3759 | * preallocate space for a file. This implements ext4's fallocate file |
3671 | * operation, which gets called from sys_fallocate system call. | 3760 | * operation, which gets called from sys_fallocate system call. |
3672 | * For block-mapped files, posix_fallocate should fall back to the method | 3761 | * For block-mapped files, posix_fallocate should fall back to the method |
3673 | * of writing zeroes to the required new blocks (the same behavior which is | 3762 | * of writing zeroes to the required new blocks (the same behavior which is |
3674 | * expected for file systems which do not support fallocate() system call). | 3763 | * expected for file systems which do not support fallocate() system call). |
3675 | */ | 3764 | */ |
3676 | long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len) | 3765 | long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len) |
3677 | { | 3766 | { |
3767 | struct inode *inode = file->f_path.dentry->d_inode; | ||
3678 | handle_t *handle; | 3768 | handle_t *handle; |
3679 | loff_t new_size; | 3769 | loff_t new_size; |
3680 | unsigned int max_blocks; | 3770 | unsigned int max_blocks; |
@@ -3691,10 +3781,14 @@ long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len) | |||
3691 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) | 3781 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
3692 | return -EOPNOTSUPP; | 3782 | return -EOPNOTSUPP; |
3693 | 3783 | ||
3694 | /* preallocation to directories is currently not supported */ | 3784 | /* Return error if mode is not supported */ |
3695 | if (S_ISDIR(inode->i_mode)) | 3785 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
3696 | return -ENODEV; | 3786 | return -EOPNOTSUPP; |
3787 | |||
3788 | if (mode & FALLOC_FL_PUNCH_HOLE) | ||
3789 | return ext4_punch_hole(file, offset, len); | ||
3697 | 3790 | ||
3791 | trace_ext4_fallocate_enter(inode, offset, len, mode); | ||
3698 | map.m_lblk = offset >> blkbits; | 3792 | map.m_lblk = offset >> blkbits; |
3699 | /* | 3793 | /* |
3700 | * We can't just convert len to max_blocks because | 3794 | * We can't just convert len to max_blocks because |
@@ -3710,6 +3804,7 @@ long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len) | |||
3710 | ret = inode_newsize_ok(inode, (len + offset)); | 3804 | ret = inode_newsize_ok(inode, (len + offset)); |
3711 | if (ret) { | 3805 | if (ret) { |
3712 | mutex_unlock(&inode->i_mutex); | 3806 | mutex_unlock(&inode->i_mutex); |
3807 | trace_ext4_fallocate_exit(inode, offset, max_blocks, ret); | ||
3713 | return ret; | 3808 | return ret; |
3714 | } | 3809 | } |
3715 | retry: | 3810 | retry: |
@@ -3722,14 +3817,15 @@ retry: | |||
3722 | break; | 3817 | break; |
3723 | } | 3818 | } |
3724 | ret = ext4_map_blocks(handle, inode, &map, | 3819 | ret = ext4_map_blocks(handle, inode, &map, |
3725 | EXT4_GET_BLOCKS_CREATE_UNINIT_EXT); | 3820 | EXT4_GET_BLOCKS_CREATE_UNINIT_EXT | |
3821 | EXT4_GET_BLOCKS_NO_NORMALIZE); | ||
3726 | if (ret <= 0) { | 3822 | if (ret <= 0) { |
3727 | #ifdef EXT4FS_DEBUG | 3823 | #ifdef EXT4FS_DEBUG |
3728 | WARN_ON(ret <= 0); | 3824 | WARN_ON(ret <= 0); |
3729 | printk(KERN_ERR "%s: ext4_ext_map_blocks " | 3825 | printk(KERN_ERR "%s: ext4_ext_map_blocks " |
3730 | "returned error inode#%lu, block=%u, " | 3826 | "returned error inode#%lu, block=%u, " |
3731 | "max_blocks=%u", __func__, | 3827 | "max_blocks=%u", __func__, |
3732 | inode->i_ino, block, max_blocks); | 3828 | inode->i_ino, map.m_lblk, max_blocks); |
3733 | #endif | 3829 | #endif |
3734 | ext4_mark_inode_dirty(handle, inode); | 3830 | ext4_mark_inode_dirty(handle, inode); |
3735 | ret2 = ext4_journal_stop(handle); | 3831 | ret2 = ext4_journal_stop(handle); |
@@ -3754,6 +3850,8 @@ retry: | |||
3754 | goto retry; | 3850 | goto retry; |
3755 | } | 3851 | } |
3756 | mutex_unlock(&inode->i_mutex); | 3852 | mutex_unlock(&inode->i_mutex); |
3853 | trace_ext4_fallocate_exit(inode, offset, max_blocks, | ||
3854 | ret > 0 ? ret2 : ret); | ||
3757 | return ret > 0 ? ret2 : ret; | 3855 | return ret > 0 ? ret2 : ret; |
3758 | } | 3856 | } |
3759 | 3857 | ||
@@ -3812,45 +3910,190 @@ int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset, | |||
3812 | } | 3910 | } |
3813 | return ret > 0 ? ret2 : ret; | 3911 | return ret > 0 ? ret2 : ret; |
3814 | } | 3912 | } |
3913 | |||
3815 | /* | 3914 | /* |
3816 | * Callback function called for each extent to gather FIEMAP information. | 3915 | * Callback function called for each extent to gather FIEMAP information. |
3817 | */ | 3916 | */ |
3818 | static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path, | 3917 | static int ext4_ext_fiemap_cb(struct inode *inode, ext4_lblk_t next, |
3819 | struct ext4_ext_cache *newex, struct ext4_extent *ex, | 3918 | struct ext4_ext_cache *newex, struct ext4_extent *ex, |
3820 | void *data) | 3919 | void *data) |
3821 | { | 3920 | { |
3822 | struct fiemap_extent_info *fieinfo = data; | ||
3823 | unsigned char blksize_bits = inode->i_sb->s_blocksize_bits; | ||
3824 | __u64 logical; | 3921 | __u64 logical; |
3825 | __u64 physical; | 3922 | __u64 physical; |
3826 | __u64 length; | 3923 | __u64 length; |
3827 | __u32 flags = 0; | 3924 | __u32 flags = 0; |
3828 | int error; | 3925 | int ret = 0; |
3926 | struct fiemap_extent_info *fieinfo = data; | ||
3927 | unsigned char blksize_bits; | ||
3829 | 3928 | ||
3830 | logical = (__u64)newex->ec_block << blksize_bits; | 3929 | blksize_bits = inode->i_sb->s_blocksize_bits; |
3930 | logical = (__u64)newex->ec_block << blksize_bits; | ||
3831 | 3931 | ||
3832 | if (newex->ec_type == EXT4_EXT_CACHE_GAP) { | 3932 | if (newex->ec_start == 0) { |
3833 | pgoff_t offset; | 3933 | /* |
3834 | struct page *page; | 3934 | * No extent in extent-tree contains block @newex->ec_start, |
3935 | * then the block may stay in 1)a hole or 2)delayed-extent. | ||
3936 | * | ||
3937 | * Holes or delayed-extents are processed as follows. | ||
3938 | * 1. lookup dirty pages with specified range in pagecache. | ||
3939 | * If no page is got, then there is no delayed-extent and | ||
3940 | * return with EXT_CONTINUE. | ||
3941 | * 2. find the 1st mapped buffer, | ||
3942 | * 3. check if the mapped buffer is both in the request range | ||
3943 | * and a delayed buffer. If not, there is no delayed-extent, | ||
3944 | * then return. | ||
3945 | * 4. a delayed-extent is found, the extent will be collected. | ||
3946 | */ | ||
3947 | ext4_lblk_t end = 0; | ||
3948 | pgoff_t last_offset; | ||
3949 | pgoff_t offset; | ||
3950 | pgoff_t index; | ||
3951 | pgoff_t start_index = 0; | ||
3952 | struct page **pages = NULL; | ||
3835 | struct buffer_head *bh = NULL; | 3953 | struct buffer_head *bh = NULL; |
3954 | struct buffer_head *head = NULL; | ||
3955 | unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *); | ||
3956 | |||
3957 | pages = kmalloc(PAGE_SIZE, GFP_KERNEL); | ||
3958 | if (pages == NULL) | ||
3959 | return -ENOMEM; | ||
3836 | 3960 | ||
3837 | offset = logical >> PAGE_SHIFT; | 3961 | offset = logical >> PAGE_SHIFT; |
3838 | page = find_get_page(inode->i_mapping, offset); | 3962 | repeat: |
3839 | if (!page || !page_has_buffers(page)) | 3963 | last_offset = offset; |
3840 | return EXT_CONTINUE; | 3964 | head = NULL; |
3965 | ret = find_get_pages_tag(inode->i_mapping, &offset, | ||
3966 | PAGECACHE_TAG_DIRTY, nr_pages, pages); | ||
3967 | |||
3968 | if (!(flags & FIEMAP_EXTENT_DELALLOC)) { | ||
3969 | /* First time, try to find a mapped buffer. */ | ||
3970 | if (ret == 0) { | ||
3971 | out: | ||
3972 | for (index = 0; index < ret; index++) | ||
3973 | page_cache_release(pages[index]); | ||
3974 | /* just a hole. */ | ||
3975 | kfree(pages); | ||
3976 | return EXT_CONTINUE; | ||
3977 | } | ||
3978 | index = 0; | ||
3841 | 3979 | ||
3842 | bh = page_buffers(page); | 3980 | next_page: |
3981 | /* Try to find the 1st mapped buffer. */ | ||
3982 | end = ((__u64)pages[index]->index << PAGE_SHIFT) >> | ||
3983 | blksize_bits; | ||
3984 | if (!page_has_buffers(pages[index])) | ||
3985 | goto out; | ||
3986 | head = page_buffers(pages[index]); | ||
3987 | if (!head) | ||
3988 | goto out; | ||
3843 | 3989 | ||
3844 | if (!bh) | 3990 | index++; |
3845 | return EXT_CONTINUE; | 3991 | bh = head; |
3992 | do { | ||
3993 | if (end >= newex->ec_block + | ||
3994 | newex->ec_len) | ||
3995 | /* The buffer is out of | ||
3996 | * the request range. | ||
3997 | */ | ||
3998 | goto out; | ||
3999 | |||
4000 | if (buffer_mapped(bh) && | ||
4001 | end >= newex->ec_block) { | ||
4002 | start_index = index - 1; | ||
4003 | /* get the 1st mapped buffer. */ | ||
4004 | goto found_mapped_buffer; | ||
4005 | } | ||
4006 | |||
4007 | bh = bh->b_this_page; | ||
4008 | end++; | ||
4009 | } while (bh != head); | ||
3846 | 4010 | ||
3847 | if (buffer_delay(bh)) { | 4011 | /* No mapped buffer in the range found in this page, |
3848 | flags |= FIEMAP_EXTENT_DELALLOC; | 4012 | * We need to look up next page. |
3849 | page_cache_release(page); | 4013 | */ |
4014 | if (index >= ret) { | ||
4015 | /* There is no page left, but we need to limit | ||
4016 | * newex->ec_len. | ||
4017 | */ | ||
4018 | newex->ec_len = end - newex->ec_block; | ||
4019 | goto out; | ||
4020 | } | ||
4021 | goto next_page; | ||
3850 | } else { | 4022 | } else { |
3851 | page_cache_release(page); | 4023 | /*Find contiguous delayed buffers. */ |
3852 | return EXT_CONTINUE; | 4024 | if (ret > 0 && pages[0]->index == last_offset) |
4025 | head = page_buffers(pages[0]); | ||
4026 | bh = head; | ||
4027 | index = 1; | ||
4028 | start_index = 0; | ||
4029 | } | ||
4030 | |||
4031 | found_mapped_buffer: | ||
4032 | if (bh != NULL && buffer_delay(bh)) { | ||
4033 | /* 1st or contiguous delayed buffer found. */ | ||
4034 | if (!(flags & FIEMAP_EXTENT_DELALLOC)) { | ||
4035 | /* | ||
4036 | * 1st delayed buffer found, record | ||
4037 | * the start of extent. | ||
4038 | */ | ||
4039 | flags |= FIEMAP_EXTENT_DELALLOC; | ||
4040 | newex->ec_block = end; | ||
4041 | logical = (__u64)end << blksize_bits; | ||
4042 | } | ||
4043 | /* Find contiguous delayed buffers. */ | ||
4044 | do { | ||
4045 | if (!buffer_delay(bh)) | ||
4046 | goto found_delayed_extent; | ||
4047 | bh = bh->b_this_page; | ||
4048 | end++; | ||
4049 | } while (bh != head); | ||
4050 | |||
4051 | for (; index < ret; index++) { | ||
4052 | if (!page_has_buffers(pages[index])) { | ||
4053 | bh = NULL; | ||
4054 | break; | ||
4055 | } | ||
4056 | head = page_buffers(pages[index]); | ||
4057 | if (!head) { | ||
4058 | bh = NULL; | ||
4059 | break; | ||
4060 | } | ||
4061 | |||
4062 | if (pages[index]->index != | ||
4063 | pages[start_index]->index + index | ||
4064 | - start_index) { | ||
4065 | /* Blocks are not contiguous. */ | ||
4066 | bh = NULL; | ||
4067 | break; | ||
4068 | } | ||
4069 | bh = head; | ||
4070 | do { | ||
4071 | if (!buffer_delay(bh)) | ||
4072 | /* Delayed-extent ends. */ | ||
4073 | goto found_delayed_extent; | ||
4074 | bh = bh->b_this_page; | ||
4075 | end++; | ||
4076 | } while (bh != head); | ||
4077 | } | ||
4078 | } else if (!(flags & FIEMAP_EXTENT_DELALLOC)) | ||
4079 | /* a hole found. */ | ||
4080 | goto out; | ||
4081 | |||
4082 | found_delayed_extent: | ||
4083 | newex->ec_len = min(end - newex->ec_block, | ||
4084 | (ext4_lblk_t)EXT_INIT_MAX_LEN); | ||
4085 | if (ret == nr_pages && bh != NULL && | ||
4086 | newex->ec_len < EXT_INIT_MAX_LEN && | ||
4087 | buffer_delay(bh)) { | ||
4088 | /* Have not collected an extent and continue. */ | ||
4089 | for (index = 0; index < ret; index++) | ||
4090 | page_cache_release(pages[index]); | ||
4091 | goto repeat; | ||
3853 | } | 4092 | } |
4093 | |||
4094 | for (index = 0; index < ret; index++) | ||
4095 | page_cache_release(pages[index]); | ||
4096 | kfree(pages); | ||
3854 | } | 4097 | } |
3855 | 4098 | ||
3856 | physical = (__u64)newex->ec_start << blksize_bits; | 4099 | physical = (__u64)newex->ec_start << blksize_bits; |
@@ -3859,32 +4102,15 @@ static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path, | |||
3859 | if (ex && ext4_ext_is_uninitialized(ex)) | 4102 | if (ex && ext4_ext_is_uninitialized(ex)) |
3860 | flags |= FIEMAP_EXTENT_UNWRITTEN; | 4103 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
3861 | 4104 | ||
3862 | /* | 4105 | if (next == EXT_MAX_BLOCKS) |
3863 | * If this extent reaches EXT_MAX_BLOCK, it must be last. | ||
3864 | * | ||
3865 | * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK, | ||
3866 | * this also indicates no more allocated blocks. | ||
3867 | * | ||
3868 | * XXX this might miss a single-block extent at EXT_MAX_BLOCK | ||
3869 | */ | ||
3870 | if (ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK || | ||
3871 | newex->ec_block + newex->ec_len - 1 == EXT_MAX_BLOCK) { | ||
3872 | loff_t size = i_size_read(inode); | ||
3873 | loff_t bs = EXT4_BLOCK_SIZE(inode->i_sb); | ||
3874 | |||
3875 | flags |= FIEMAP_EXTENT_LAST; | 4106 | flags |= FIEMAP_EXTENT_LAST; |
3876 | if ((flags & FIEMAP_EXTENT_DELALLOC) && | ||
3877 | logical+length > size) | ||
3878 | length = (size - logical + bs - 1) & ~(bs-1); | ||
3879 | } | ||
3880 | 4107 | ||
3881 | error = fiemap_fill_next_extent(fieinfo, logical, physical, | 4108 | ret = fiemap_fill_next_extent(fieinfo, logical, physical, |
3882 | length, flags); | 4109 | length, flags); |
3883 | if (error < 0) | 4110 | if (ret < 0) |
3884 | return error; | 4111 | return ret; |
3885 | if (error == 1) | 4112 | if (ret == 1) |
3886 | return EXT_BREAK; | 4113 | return EXT_BREAK; |
3887 | |||
3888 | return EXT_CONTINUE; | 4114 | return EXT_CONTINUE; |
3889 | } | 4115 | } |
3890 | 4116 | ||
@@ -3926,6 +4152,177 @@ static int ext4_xattr_fiemap(struct inode *inode, | |||
3926 | return (error < 0 ? error : 0); | 4152 | return (error < 0 ? error : 0); |
3927 | } | 4153 | } |
3928 | 4154 | ||
4155 | /* | ||
4156 | * ext4_ext_punch_hole | ||
4157 | * | ||
4158 | * Punches a hole of "length" bytes in a file starting | ||
4159 | * at byte "offset" | ||
4160 | * | ||
4161 | * @inode: The inode of the file to punch a hole in | ||
4162 | * @offset: The starting byte offset of the hole | ||
4163 | * @length: The length of the hole | ||
4164 | * | ||
4165 | * Returns the number of blocks removed or negative on err | ||
4166 | */ | ||
4167 | int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length) | ||
4168 | { | ||
4169 | struct inode *inode = file->f_path.dentry->d_inode; | ||
4170 | struct super_block *sb = inode->i_sb; | ||
4171 | struct ext4_ext_cache cache_ex; | ||
4172 | ext4_lblk_t first_block, last_block, num_blocks, iblock, max_blocks; | ||
4173 | struct address_space *mapping = inode->i_mapping; | ||
4174 | struct ext4_map_blocks map; | ||
4175 | handle_t *handle; | ||
4176 | loff_t first_block_offset, last_block_offset, block_len; | ||
4177 | loff_t first_page, last_page, first_page_offset, last_page_offset; | ||
4178 | int ret, credits, blocks_released, err = 0; | ||
4179 | |||
4180 | first_block = (offset + sb->s_blocksize - 1) >> | ||
4181 | EXT4_BLOCK_SIZE_BITS(sb); | ||
4182 | last_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); | ||
4183 | |||
4184 | first_block_offset = first_block << EXT4_BLOCK_SIZE_BITS(sb); | ||
4185 | last_block_offset = last_block << EXT4_BLOCK_SIZE_BITS(sb); | ||
4186 | |||
4187 | first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | ||
4188 | last_page = (offset + length) >> PAGE_CACHE_SHIFT; | ||
4189 | |||
4190 | first_page_offset = first_page << PAGE_CACHE_SHIFT; | ||
4191 | last_page_offset = last_page << PAGE_CACHE_SHIFT; | ||
4192 | |||
4193 | /* | ||
4194 | * Write out all dirty pages to avoid race conditions | ||
4195 | * Then release them. | ||
4196 | */ | ||
4197 | if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { | ||
4198 | err = filemap_write_and_wait_range(mapping, | ||
4199 | first_page_offset == 0 ? 0 : first_page_offset-1, | ||
4200 | last_page_offset); | ||
4201 | |||
4202 | if (err) | ||
4203 | return err; | ||
4204 | } | ||
4205 | |||
4206 | /* Now release the pages */ | ||
4207 | if (last_page_offset > first_page_offset) { | ||
4208 | truncate_inode_pages_range(mapping, first_page_offset, | ||
4209 | last_page_offset-1); | ||
4210 | } | ||
4211 | |||
4212 | /* finish any pending end_io work */ | ||
4213 | ext4_flush_completed_IO(inode); | ||
4214 | |||
4215 | credits = ext4_writepage_trans_blocks(inode); | ||
4216 | handle = ext4_journal_start(inode, credits); | ||
4217 | if (IS_ERR(handle)) | ||
4218 | return PTR_ERR(handle); | ||
4219 | |||
4220 | err = ext4_orphan_add(handle, inode); | ||
4221 | if (err) | ||
4222 | goto out; | ||
4223 | |||
4224 | /* | ||
4225 | * Now we need to zero out the un block aligned data. | ||
4226 | * If the file is smaller than a block, just | ||
4227 | * zero out the middle | ||
4228 | */ | ||
4229 | if (first_block > last_block) | ||
4230 | ext4_block_zero_page_range(handle, mapping, offset, length); | ||
4231 | else { | ||
4232 | /* zero out the head of the hole before the first block */ | ||
4233 | block_len = first_block_offset - offset; | ||
4234 | if (block_len > 0) | ||
4235 | ext4_block_zero_page_range(handle, mapping, | ||
4236 | offset, block_len); | ||
4237 | |||
4238 | /* zero out the tail of the hole after the last block */ | ||
4239 | block_len = offset + length - last_block_offset; | ||
4240 | if (block_len > 0) { | ||
4241 | ext4_block_zero_page_range(handle, mapping, | ||
4242 | last_block_offset, block_len); | ||
4243 | } | ||
4244 | } | ||
4245 | |||
4246 | /* If there are no blocks to remove, return now */ | ||
4247 | if (first_block >= last_block) | ||
4248 | goto out; | ||
4249 | |||
4250 | down_write(&EXT4_I(inode)->i_data_sem); | ||
4251 | ext4_ext_invalidate_cache(inode); | ||
4252 | ext4_discard_preallocations(inode); | ||
4253 | |||
4254 | /* | ||
4255 | * Loop over all the blocks and identify blocks | ||
4256 | * that need to be punched out | ||
4257 | */ | ||
4258 | iblock = first_block; | ||
4259 | blocks_released = 0; | ||
4260 | while (iblock < last_block) { | ||
4261 | max_blocks = last_block - iblock; | ||
4262 | num_blocks = 1; | ||
4263 | memset(&map, 0, sizeof(map)); | ||
4264 | map.m_lblk = iblock; | ||
4265 | map.m_len = max_blocks; | ||
4266 | ret = ext4_ext_map_blocks(handle, inode, &map, | ||
4267 | EXT4_GET_BLOCKS_PUNCH_OUT_EXT); | ||
4268 | |||
4269 | if (ret > 0) { | ||
4270 | blocks_released += ret; | ||
4271 | num_blocks = ret; | ||
4272 | } else if (ret == 0) { | ||
4273 | /* | ||
4274 | * If map blocks could not find the block, | ||
4275 | * then it is in a hole. If the hole was | ||
4276 | * not already cached, then map blocks should | ||
4277 | * put it in the cache. So we can get the hole | ||
4278 | * out of the cache | ||
4279 | */ | ||
4280 | memset(&cache_ex, 0, sizeof(cache_ex)); | ||
4281 | if ((ext4_ext_check_cache(inode, iblock, &cache_ex)) && | ||
4282 | !cache_ex.ec_start) { | ||
4283 | |||
4284 | /* The hole is cached */ | ||
4285 | num_blocks = cache_ex.ec_block + | ||
4286 | cache_ex.ec_len - iblock; | ||
4287 | |||
4288 | } else { | ||
4289 | /* The block could not be identified */ | ||
4290 | err = -EIO; | ||
4291 | break; | ||
4292 | } | ||
4293 | } else { | ||
4294 | /* Map blocks error */ | ||
4295 | err = ret; | ||
4296 | break; | ||
4297 | } | ||
4298 | |||
4299 | if (num_blocks == 0) { | ||
4300 | /* This condition should never happen */ | ||
4301 | ext_debug("Block lookup failed"); | ||
4302 | err = -EIO; | ||
4303 | break; | ||
4304 | } | ||
4305 | |||
4306 | iblock += num_blocks; | ||
4307 | } | ||
4308 | |||
4309 | if (blocks_released > 0) { | ||
4310 | ext4_ext_invalidate_cache(inode); | ||
4311 | ext4_discard_preallocations(inode); | ||
4312 | } | ||
4313 | |||
4314 | if (IS_SYNC(inode)) | ||
4315 | ext4_handle_sync(handle); | ||
4316 | |||
4317 | up_write(&EXT4_I(inode)->i_data_sem); | ||
4318 | |||
4319 | out: | ||
4320 | ext4_orphan_del(handle, inode); | ||
4321 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); | ||
4322 | ext4_mark_inode_dirty(handle, inode); | ||
4323 | ext4_journal_stop(handle); | ||
4324 | return err; | ||
4325 | } | ||
3929 | int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, | 4326 | int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
3930 | __u64 start, __u64 len) | 4327 | __u64 start, __u64 len) |
3931 | { | 4328 | { |
@@ -3948,8 +4345,8 @@ int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, | |||
3948 | 4345 | ||
3949 | start_blk = start >> inode->i_sb->s_blocksize_bits; | 4346 | start_blk = start >> inode->i_sb->s_blocksize_bits; |
3950 | last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits; | 4347 | last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits; |
3951 | if (last_blk >= EXT_MAX_BLOCK) | 4348 | if (last_blk >= EXT_MAX_BLOCKS) |
3952 | last_blk = EXT_MAX_BLOCK-1; | 4349 | last_blk = EXT_MAX_BLOCKS-1; |
3953 | len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1; | 4350 | len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1; |
3954 | 4351 | ||
3955 | /* | 4352 | /* |
@@ -3962,4 +4359,3 @@ int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, | |||
3962 | 4359 | ||
3963 | return error; | 4360 | return error; |
3964 | } | 4361 | } |
3965 | |||