diff options
Diffstat (limited to 'fs/ext4/extents.c')
-rw-r--r-- | fs/ext4/extents.c | 556 |
1 files changed, 477 insertions, 79 deletions
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index 73ebfb44ad75..10539e364283 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c | |||
@@ -93,7 +93,9 @@ static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb) | |||
93 | ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff); | 93 | ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff); |
94 | } | 94 | } |
95 | 95 | ||
96 | static int ext4_ext_journal_restart(handle_t *handle, int needed) | 96 | static int ext4_ext_truncate_extend_restart(handle_t *handle, |
97 | struct inode *inode, | ||
98 | int needed) | ||
97 | { | 99 | { |
98 | int err; | 100 | int err; |
99 | 101 | ||
@@ -104,7 +106,14 @@ static int ext4_ext_journal_restart(handle_t *handle, int needed) | |||
104 | err = ext4_journal_extend(handle, needed); | 106 | err = ext4_journal_extend(handle, needed); |
105 | if (err <= 0) | 107 | if (err <= 0) |
106 | return err; | 108 | return err; |
107 | return ext4_journal_restart(handle, needed); | 109 | err = ext4_truncate_restart_trans(handle, inode, needed); |
110 | /* | ||
111 | * We have dropped i_data_sem so someone might have cached again | ||
112 | * an extent we are going to truncate. | ||
113 | */ | ||
114 | ext4_ext_invalidate_cache(inode); | ||
115 | |||
116 | return err; | ||
108 | } | 117 | } |
109 | 118 | ||
110 | /* | 119 | /* |
@@ -220,57 +229,65 @@ ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, | |||
220 | return newblock; | 229 | return newblock; |
221 | } | 230 | } |
222 | 231 | ||
223 | static int ext4_ext_space_block(struct inode *inode) | 232 | static inline int ext4_ext_space_block(struct inode *inode, int check) |
224 | { | 233 | { |
225 | int size; | 234 | int size; |
226 | 235 | ||
227 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) | 236 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
228 | / sizeof(struct ext4_extent); | 237 | / sizeof(struct ext4_extent); |
238 | if (!check) { | ||
229 | #ifdef AGGRESSIVE_TEST | 239 | #ifdef AGGRESSIVE_TEST |
230 | if (size > 6) | 240 | if (size > 6) |
231 | size = 6; | 241 | size = 6; |
232 | #endif | 242 | #endif |
243 | } | ||
233 | return size; | 244 | return size; |
234 | } | 245 | } |
235 | 246 | ||
236 | static int ext4_ext_space_block_idx(struct inode *inode) | 247 | static inline int ext4_ext_space_block_idx(struct inode *inode, int check) |
237 | { | 248 | { |
238 | int size; | 249 | int size; |
239 | 250 | ||
240 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) | 251 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
241 | / sizeof(struct ext4_extent_idx); | 252 | / sizeof(struct ext4_extent_idx); |
253 | if (!check) { | ||
242 | #ifdef AGGRESSIVE_TEST | 254 | #ifdef AGGRESSIVE_TEST |
243 | if (size > 5) | 255 | if (size > 5) |
244 | size = 5; | 256 | size = 5; |
245 | #endif | 257 | #endif |
258 | } | ||
246 | return size; | 259 | return size; |
247 | } | 260 | } |
248 | 261 | ||
249 | static int ext4_ext_space_root(struct inode *inode) | 262 | static inline int ext4_ext_space_root(struct inode *inode, int check) |
250 | { | 263 | { |
251 | int size; | 264 | int size; |
252 | 265 | ||
253 | size = sizeof(EXT4_I(inode)->i_data); | 266 | size = sizeof(EXT4_I(inode)->i_data); |
254 | size -= sizeof(struct ext4_extent_header); | 267 | size -= sizeof(struct ext4_extent_header); |
255 | size /= sizeof(struct ext4_extent); | 268 | size /= sizeof(struct ext4_extent); |
269 | if (!check) { | ||
256 | #ifdef AGGRESSIVE_TEST | 270 | #ifdef AGGRESSIVE_TEST |
257 | if (size > 3) | 271 | if (size > 3) |
258 | size = 3; | 272 | size = 3; |
259 | #endif | 273 | #endif |
274 | } | ||
260 | return size; | 275 | return size; |
261 | } | 276 | } |
262 | 277 | ||
263 | static int ext4_ext_space_root_idx(struct inode *inode) | 278 | static inline int ext4_ext_space_root_idx(struct inode *inode, int check) |
264 | { | 279 | { |
265 | int size; | 280 | int size; |
266 | 281 | ||
267 | size = sizeof(EXT4_I(inode)->i_data); | 282 | size = sizeof(EXT4_I(inode)->i_data); |
268 | size -= sizeof(struct ext4_extent_header); | 283 | size -= sizeof(struct ext4_extent_header); |
269 | size /= sizeof(struct ext4_extent_idx); | 284 | size /= sizeof(struct ext4_extent_idx); |
285 | if (!check) { | ||
270 | #ifdef AGGRESSIVE_TEST | 286 | #ifdef AGGRESSIVE_TEST |
271 | if (size > 4) | 287 | if (size > 4) |
272 | size = 4; | 288 | size = 4; |
273 | #endif | 289 | #endif |
290 | } | ||
274 | return size; | 291 | return size; |
275 | } | 292 | } |
276 | 293 | ||
@@ -284,9 +301,9 @@ int ext4_ext_calc_metadata_amount(struct inode *inode, int blocks) | |||
284 | int lcap, icap, rcap, leafs, idxs, num; | 301 | int lcap, icap, rcap, leafs, idxs, num; |
285 | int newextents = blocks; | 302 | int newextents = blocks; |
286 | 303 | ||
287 | rcap = ext4_ext_space_root_idx(inode); | 304 | rcap = ext4_ext_space_root_idx(inode, 0); |
288 | lcap = ext4_ext_space_block(inode); | 305 | lcap = ext4_ext_space_block(inode, 0); |
289 | icap = ext4_ext_space_block_idx(inode); | 306 | icap = ext4_ext_space_block_idx(inode, 0); |
290 | 307 | ||
291 | /* number of new leaf blocks needed */ | 308 | /* number of new leaf blocks needed */ |
292 | num = leafs = (newextents + lcap - 1) / lcap; | 309 | num = leafs = (newextents + lcap - 1) / lcap; |
@@ -311,14 +328,14 @@ ext4_ext_max_entries(struct inode *inode, int depth) | |||
311 | 328 | ||
312 | if (depth == ext_depth(inode)) { | 329 | if (depth == ext_depth(inode)) { |
313 | if (depth == 0) | 330 | if (depth == 0) |
314 | max = ext4_ext_space_root(inode); | 331 | max = ext4_ext_space_root(inode, 1); |
315 | else | 332 | else |
316 | max = ext4_ext_space_root_idx(inode); | 333 | max = ext4_ext_space_root_idx(inode, 1); |
317 | } else { | 334 | } else { |
318 | if (depth == 0) | 335 | if (depth == 0) |
319 | max = ext4_ext_space_block(inode); | 336 | max = ext4_ext_space_block(inode, 1); |
320 | else | 337 | else |
321 | max = ext4_ext_space_block_idx(inode); | 338 | max = ext4_ext_space_block_idx(inode, 1); |
322 | } | 339 | } |
323 | 340 | ||
324 | return max; | 341 | return max; |
@@ -437,8 +454,9 @@ static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) | |||
437 | ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block), | 454 | ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block), |
438 | idx_pblock(path->p_idx)); | 455 | idx_pblock(path->p_idx)); |
439 | } else if (path->p_ext) { | 456 | } else if (path->p_ext) { |
440 | ext_debug(" %d:%d:%llu ", | 457 | ext_debug(" %d:[%d]%d:%llu ", |
441 | le32_to_cpu(path->p_ext->ee_block), | 458 | le32_to_cpu(path->p_ext->ee_block), |
459 | ext4_ext_is_uninitialized(path->p_ext), | ||
442 | ext4_ext_get_actual_len(path->p_ext), | 460 | ext4_ext_get_actual_len(path->p_ext), |
443 | ext_pblock(path->p_ext)); | 461 | ext_pblock(path->p_ext)); |
444 | } else | 462 | } else |
@@ -460,8 +478,11 @@ static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) | |||
460 | eh = path[depth].p_hdr; | 478 | eh = path[depth].p_hdr; |
461 | ex = EXT_FIRST_EXTENT(eh); | 479 | ex = EXT_FIRST_EXTENT(eh); |
462 | 480 | ||
481 | ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino); | ||
482 | |||
463 | for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { | 483 | for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { |
464 | ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block), | 484 | ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block), |
485 | ext4_ext_is_uninitialized(ex), | ||
465 | ext4_ext_get_actual_len(ex), ext_pblock(ex)); | 486 | ext4_ext_get_actual_len(ex), ext_pblock(ex)); |
466 | } | 487 | } |
467 | ext_debug("\n"); | 488 | ext_debug("\n"); |
@@ -580,9 +601,10 @@ ext4_ext_binsearch(struct inode *inode, | |||
580 | } | 601 | } |
581 | 602 | ||
582 | path->p_ext = l - 1; | 603 | path->p_ext = l - 1; |
583 | ext_debug(" -> %d:%llu:%d ", | 604 | ext_debug(" -> %d:%llu:[%d]%d ", |
584 | le32_to_cpu(path->p_ext->ee_block), | 605 | le32_to_cpu(path->p_ext->ee_block), |
585 | ext_pblock(path->p_ext), | 606 | ext_pblock(path->p_ext), |
607 | ext4_ext_is_uninitialized(path->p_ext), | ||
586 | ext4_ext_get_actual_len(path->p_ext)); | 608 | ext4_ext_get_actual_len(path->p_ext)); |
587 | 609 | ||
588 | #ifdef CHECK_BINSEARCH | 610 | #ifdef CHECK_BINSEARCH |
@@ -612,7 +634,7 @@ int ext4_ext_tree_init(handle_t *handle, struct inode *inode) | |||
612 | eh->eh_depth = 0; | 634 | eh->eh_depth = 0; |
613 | eh->eh_entries = 0; | 635 | eh->eh_entries = 0; |
614 | eh->eh_magic = EXT4_EXT_MAGIC; | 636 | eh->eh_magic = EXT4_EXT_MAGIC; |
615 | eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode)); | 637 | eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0)); |
616 | ext4_mark_inode_dirty(handle, inode); | 638 | ext4_mark_inode_dirty(handle, inode); |
617 | ext4_ext_invalidate_cache(inode); | 639 | ext4_ext_invalidate_cache(inode); |
618 | return 0; | 640 | return 0; |
@@ -701,7 +723,7 @@ err: | |||
701 | * insert new index [@logical;@ptr] into the block at @curp; | 723 | * insert new index [@logical;@ptr] into the block at @curp; |
702 | * check where to insert: before @curp or after @curp | 724 | * check where to insert: before @curp or after @curp |
703 | */ | 725 | */ |
704 | static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, | 726 | int ext4_ext_insert_index(handle_t *handle, struct inode *inode, |
705 | struct ext4_ext_path *curp, | 727 | struct ext4_ext_path *curp, |
706 | int logical, ext4_fsblk_t ptr) | 728 | int logical, ext4_fsblk_t ptr) |
707 | { | 729 | { |
@@ -837,7 +859,7 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode, | |||
837 | 859 | ||
838 | neh = ext_block_hdr(bh); | 860 | neh = ext_block_hdr(bh); |
839 | neh->eh_entries = 0; | 861 | neh->eh_entries = 0; |
840 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode)); | 862 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); |
841 | neh->eh_magic = EXT4_EXT_MAGIC; | 863 | neh->eh_magic = EXT4_EXT_MAGIC; |
842 | neh->eh_depth = 0; | 864 | neh->eh_depth = 0; |
843 | ex = EXT_FIRST_EXTENT(neh); | 865 | ex = EXT_FIRST_EXTENT(neh); |
@@ -850,9 +872,10 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode, | |||
850 | path[depth].p_ext++; | 872 | path[depth].p_ext++; |
851 | while (path[depth].p_ext <= | 873 | while (path[depth].p_ext <= |
852 | EXT_MAX_EXTENT(path[depth].p_hdr)) { | 874 | EXT_MAX_EXTENT(path[depth].p_hdr)) { |
853 | ext_debug("move %d:%llu:%d in new leaf %llu\n", | 875 | ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n", |
854 | le32_to_cpu(path[depth].p_ext->ee_block), | 876 | le32_to_cpu(path[depth].p_ext->ee_block), |
855 | ext_pblock(path[depth].p_ext), | 877 | ext_pblock(path[depth].p_ext), |
878 | ext4_ext_is_uninitialized(path[depth].p_ext), | ||
856 | ext4_ext_get_actual_len(path[depth].p_ext), | 879 | ext4_ext_get_actual_len(path[depth].p_ext), |
857 | newblock); | 880 | newblock); |
858 | /*memmove(ex++, path[depth].p_ext++, | 881 | /*memmove(ex++, path[depth].p_ext++, |
@@ -912,7 +935,7 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode, | |||
912 | neh = ext_block_hdr(bh); | 935 | neh = ext_block_hdr(bh); |
913 | neh->eh_entries = cpu_to_le16(1); | 936 | neh->eh_entries = cpu_to_le16(1); |
914 | neh->eh_magic = EXT4_EXT_MAGIC; | 937 | neh->eh_magic = EXT4_EXT_MAGIC; |
915 | neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode)); | 938 | neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0)); |
916 | neh->eh_depth = cpu_to_le16(depth - i); | 939 | neh->eh_depth = cpu_to_le16(depth - i); |
917 | fidx = EXT_FIRST_INDEX(neh); | 940 | fidx = EXT_FIRST_INDEX(neh); |
918 | fidx->ei_block = border; | 941 | fidx->ei_block = border; |
@@ -1037,9 +1060,9 @@ static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, | |||
1037 | /* old root could have indexes or leaves | 1060 | /* old root could have indexes or leaves |
1038 | * so calculate e_max right way */ | 1061 | * so calculate e_max right way */ |
1039 | if (ext_depth(inode)) | 1062 | if (ext_depth(inode)) |
1040 | neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode)); | 1063 | neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0)); |
1041 | else | 1064 | else |
1042 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode)); | 1065 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); |
1043 | neh->eh_magic = EXT4_EXT_MAGIC; | 1066 | neh->eh_magic = EXT4_EXT_MAGIC; |
1044 | set_buffer_uptodate(bh); | 1067 | set_buffer_uptodate(bh); |
1045 | unlock_buffer(bh); | 1068 | unlock_buffer(bh); |
@@ -1054,7 +1077,7 @@ static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, | |||
1054 | goto out; | 1077 | goto out; |
1055 | 1078 | ||
1056 | curp->p_hdr->eh_magic = EXT4_EXT_MAGIC; | 1079 | curp->p_hdr->eh_magic = EXT4_EXT_MAGIC; |
1057 | curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode)); | 1080 | curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0)); |
1058 | curp->p_hdr->eh_entries = cpu_to_le16(1); | 1081 | curp->p_hdr->eh_entries = cpu_to_le16(1); |
1059 | curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr); | 1082 | curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr); |
1060 | 1083 | ||
@@ -1563,7 +1586,7 @@ out: | |||
1563 | */ | 1586 | */ |
1564 | int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, | 1587 | int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, |
1565 | struct ext4_ext_path *path, | 1588 | struct ext4_ext_path *path, |
1566 | struct ext4_extent *newext) | 1589 | struct ext4_extent *newext, int flag) |
1567 | { | 1590 | { |
1568 | struct ext4_extent_header *eh; | 1591 | struct ext4_extent_header *eh; |
1569 | struct ext4_extent *ex, *fex; | 1592 | struct ext4_extent *ex, *fex; |
@@ -1579,10 +1602,13 @@ int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, | |||
1579 | BUG_ON(path[depth].p_hdr == NULL); | 1602 | BUG_ON(path[depth].p_hdr == NULL); |
1580 | 1603 | ||
1581 | /* try to insert block into found extent and return */ | 1604 | /* try to insert block into found extent and return */ |
1582 | if (ex && ext4_can_extents_be_merged(inode, ex, newext)) { | 1605 | if (ex && (flag != EXT4_GET_BLOCKS_DIO_CREATE_EXT) |
1583 | ext_debug("append %d block to %d:%d (from %llu)\n", | 1606 | && ext4_can_extents_be_merged(inode, ex, newext)) { |
1607 | ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n", | ||
1608 | ext4_ext_is_uninitialized(newext), | ||
1584 | ext4_ext_get_actual_len(newext), | 1609 | ext4_ext_get_actual_len(newext), |
1585 | le32_to_cpu(ex->ee_block), | 1610 | le32_to_cpu(ex->ee_block), |
1611 | ext4_ext_is_uninitialized(ex), | ||
1586 | ext4_ext_get_actual_len(ex), ext_pblock(ex)); | 1612 | ext4_ext_get_actual_len(ex), ext_pblock(ex)); |
1587 | err = ext4_ext_get_access(handle, inode, path + depth); | 1613 | err = ext4_ext_get_access(handle, inode, path + depth); |
1588 | if (err) | 1614 | if (err) |
@@ -1651,9 +1677,10 @@ has_space: | |||
1651 | 1677 | ||
1652 | if (!nearex) { | 1678 | if (!nearex) { |
1653 | /* there is no extent in this leaf, create first one */ | 1679 | /* there is no extent in this leaf, create first one */ |
1654 | ext_debug("first extent in the leaf: %d:%llu:%d\n", | 1680 | ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n", |
1655 | le32_to_cpu(newext->ee_block), | 1681 | le32_to_cpu(newext->ee_block), |
1656 | ext_pblock(newext), | 1682 | ext_pblock(newext), |
1683 | ext4_ext_is_uninitialized(newext), | ||
1657 | ext4_ext_get_actual_len(newext)); | 1684 | ext4_ext_get_actual_len(newext)); |
1658 | path[depth].p_ext = EXT_FIRST_EXTENT(eh); | 1685 | path[depth].p_ext = EXT_FIRST_EXTENT(eh); |
1659 | } else if (le32_to_cpu(newext->ee_block) | 1686 | } else if (le32_to_cpu(newext->ee_block) |
@@ -1663,10 +1690,11 @@ has_space: | |||
1663 | len = EXT_MAX_EXTENT(eh) - nearex; | 1690 | len = EXT_MAX_EXTENT(eh) - nearex; |
1664 | len = (len - 1) * sizeof(struct ext4_extent); | 1691 | len = (len - 1) * sizeof(struct ext4_extent); |
1665 | len = len < 0 ? 0 : len; | 1692 | len = len < 0 ? 0 : len; |
1666 | ext_debug("insert %d:%llu:%d after: nearest 0x%p, " | 1693 | ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, " |
1667 | "move %d from 0x%p to 0x%p\n", | 1694 | "move %d from 0x%p to 0x%p\n", |
1668 | le32_to_cpu(newext->ee_block), | 1695 | le32_to_cpu(newext->ee_block), |
1669 | ext_pblock(newext), | 1696 | ext_pblock(newext), |
1697 | ext4_ext_is_uninitialized(newext), | ||
1670 | ext4_ext_get_actual_len(newext), | 1698 | ext4_ext_get_actual_len(newext), |
1671 | nearex, len, nearex + 1, nearex + 2); | 1699 | nearex, len, nearex + 1, nearex + 2); |
1672 | memmove(nearex + 2, nearex + 1, len); | 1700 | memmove(nearex + 2, nearex + 1, len); |
@@ -1676,10 +1704,11 @@ has_space: | |||
1676 | BUG_ON(newext->ee_block == nearex->ee_block); | 1704 | BUG_ON(newext->ee_block == nearex->ee_block); |
1677 | len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent); | 1705 | len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent); |
1678 | len = len < 0 ? 0 : len; | 1706 | len = len < 0 ? 0 : len; |
1679 | ext_debug("insert %d:%llu:%d before: nearest 0x%p, " | 1707 | ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, " |
1680 | "move %d from 0x%p to 0x%p\n", | 1708 | "move %d from 0x%p to 0x%p\n", |
1681 | le32_to_cpu(newext->ee_block), | 1709 | le32_to_cpu(newext->ee_block), |
1682 | ext_pblock(newext), | 1710 | ext_pblock(newext), |
1711 | ext4_ext_is_uninitialized(newext), | ||
1683 | ext4_ext_get_actual_len(newext), | 1712 | ext4_ext_get_actual_len(newext), |
1684 | nearex, len, nearex + 1, nearex + 2); | 1713 | nearex, len, nearex + 1, nearex + 2); |
1685 | memmove(nearex + 1, nearex, len); | 1714 | memmove(nearex + 1, nearex, len); |
@@ -1694,7 +1723,8 @@ has_space: | |||
1694 | 1723 | ||
1695 | merge: | 1724 | merge: |
1696 | /* try to merge extents to the right */ | 1725 | /* try to merge extents to the right */ |
1697 | ext4_ext_try_to_merge(inode, path, nearex); | 1726 | if (flag != EXT4_GET_BLOCKS_DIO_CREATE_EXT) |
1727 | ext4_ext_try_to_merge(inode, path, nearex); | ||
1698 | 1728 | ||
1699 | /* try to merge extents to the left */ | 1729 | /* try to merge extents to the left */ |
1700 | 1730 | ||
@@ -2094,7 +2124,8 @@ ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, | |||
2094 | else | 2124 | else |
2095 | uninitialized = 0; | 2125 | uninitialized = 0; |
2096 | 2126 | ||
2097 | ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len); | 2127 | ext_debug("remove ext %u:[%d]%d\n", ex_ee_block, |
2128 | uninitialized, ex_ee_len); | ||
2098 | path[depth].p_ext = ex; | 2129 | path[depth].p_ext = ex; |
2099 | 2130 | ||
2100 | a = ex_ee_block > start ? ex_ee_block : start; | 2131 | a = ex_ee_block > start ? ex_ee_block : start; |
@@ -2138,7 +2169,7 @@ ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, | |||
2138 | } | 2169 | } |
2139 | credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb); | 2170 | credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb); |
2140 | 2171 | ||
2141 | err = ext4_ext_journal_restart(handle, credits); | 2172 | err = ext4_ext_truncate_extend_restart(handle, inode, credits); |
2142 | if (err) | 2173 | if (err) |
2143 | goto out; | 2174 | goto out; |
2144 | 2175 | ||
@@ -2327,7 +2358,7 @@ static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start) | |||
2327 | if (err == 0) { | 2358 | if (err == 0) { |
2328 | ext_inode_hdr(inode)->eh_depth = 0; | 2359 | ext_inode_hdr(inode)->eh_depth = 0; |
2329 | ext_inode_hdr(inode)->eh_max = | 2360 | ext_inode_hdr(inode)->eh_max = |
2330 | cpu_to_le16(ext4_ext_space_root(inode)); | 2361 | cpu_to_le16(ext4_ext_space_root(inode, 0)); |
2331 | err = ext4_ext_dirty(handle, inode, path); | 2362 | err = ext4_ext_dirty(handle, inode, path); |
2332 | } | 2363 | } |
2333 | } | 2364 | } |
@@ -2349,6 +2380,7 @@ void ext4_ext_init(struct super_block *sb) | |||
2349 | */ | 2380 | */ |
2350 | 2381 | ||
2351 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { | 2382 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { |
2383 | #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS) | ||
2352 | printk(KERN_INFO "EXT4-fs: file extents enabled"); | 2384 | printk(KERN_INFO "EXT4-fs: file extents enabled"); |
2353 | #ifdef AGGRESSIVE_TEST | 2385 | #ifdef AGGRESSIVE_TEST |
2354 | printk(", aggressive tests"); | 2386 | printk(", aggressive tests"); |
@@ -2360,6 +2392,7 @@ void ext4_ext_init(struct super_block *sb) | |||
2360 | printk(", stats"); | 2392 | printk(", stats"); |
2361 | #endif | 2393 | #endif |
2362 | printk("\n"); | 2394 | printk("\n"); |
2395 | #endif | ||
2363 | #ifdef EXTENTS_STATS | 2396 | #ifdef EXTENTS_STATS |
2364 | spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); | 2397 | spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); |
2365 | EXT4_SB(sb)->s_ext_min = 1 << 30; | 2398 | EXT4_SB(sb)->s_ext_min = 1 << 30; |
@@ -2461,7 +2494,6 @@ static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) | |||
2461 | } | 2494 | } |
2462 | 2495 | ||
2463 | #define EXT4_EXT_ZERO_LEN 7 | 2496 | #define EXT4_EXT_ZERO_LEN 7 |
2464 | |||
2465 | /* | 2497 | /* |
2466 | * This function is called by ext4_ext_get_blocks() if someone tries to write | 2498 | * This function is called by ext4_ext_get_blocks() if someone tries to write |
2467 | * to an uninitialized extent. It may result in splitting the uninitialized | 2499 | * to an uninitialized extent. It may result in splitting the uninitialized |
@@ -2554,7 +2586,8 @@ static int ext4_ext_convert_to_initialized(handle_t *handle, | |||
2554 | ex3->ee_block = cpu_to_le32(iblock); | 2586 | ex3->ee_block = cpu_to_le32(iblock); |
2555 | ext4_ext_store_pblock(ex3, newblock); | 2587 | ext4_ext_store_pblock(ex3, newblock); |
2556 | ex3->ee_len = cpu_to_le16(allocated); | 2588 | ex3->ee_len = cpu_to_le16(allocated); |
2557 | err = ext4_ext_insert_extent(handle, inode, path, ex3); | 2589 | err = ext4_ext_insert_extent(handle, inode, path, |
2590 | ex3, 0); | ||
2558 | if (err == -ENOSPC) { | 2591 | if (err == -ENOSPC) { |
2559 | err = ext4_ext_zeroout(inode, &orig_ex); | 2592 | err = ext4_ext_zeroout(inode, &orig_ex); |
2560 | if (err) | 2593 | if (err) |
@@ -2610,7 +2643,7 @@ static int ext4_ext_convert_to_initialized(handle_t *handle, | |||
2610 | ext4_ext_store_pblock(ex3, newblock + max_blocks); | 2643 | ext4_ext_store_pblock(ex3, newblock + max_blocks); |
2611 | ex3->ee_len = cpu_to_le16(allocated - max_blocks); | 2644 | ex3->ee_len = cpu_to_le16(allocated - max_blocks); |
2612 | ext4_ext_mark_uninitialized(ex3); | 2645 | ext4_ext_mark_uninitialized(ex3); |
2613 | err = ext4_ext_insert_extent(handle, inode, path, ex3); | 2646 | err = ext4_ext_insert_extent(handle, inode, path, ex3, 0); |
2614 | if (err == -ENOSPC) { | 2647 | if (err == -ENOSPC) { |
2615 | err = ext4_ext_zeroout(inode, &orig_ex); | 2648 | err = ext4_ext_zeroout(inode, &orig_ex); |
2616 | if (err) | 2649 | if (err) |
@@ -2728,7 +2761,191 @@ static int ext4_ext_convert_to_initialized(handle_t *handle, | |||
2728 | err = ext4_ext_dirty(handle, inode, path + depth); | 2761 | err = ext4_ext_dirty(handle, inode, path + depth); |
2729 | goto out; | 2762 | goto out; |
2730 | insert: | 2763 | insert: |
2731 | err = ext4_ext_insert_extent(handle, inode, path, &newex); | 2764 | err = ext4_ext_insert_extent(handle, inode, path, &newex, 0); |
2765 | if (err == -ENOSPC) { | ||
2766 | err = ext4_ext_zeroout(inode, &orig_ex); | ||
2767 | if (err) | ||
2768 | goto fix_extent_len; | ||
2769 | /* update the extent length and mark as initialized */ | ||
2770 | ex->ee_block = orig_ex.ee_block; | ||
2771 | ex->ee_len = orig_ex.ee_len; | ||
2772 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | ||
2773 | ext4_ext_dirty(handle, inode, path + depth); | ||
2774 | /* zero out the first half */ | ||
2775 | return allocated; | ||
2776 | } else if (err) | ||
2777 | goto fix_extent_len; | ||
2778 | out: | ||
2779 | ext4_ext_show_leaf(inode, path); | ||
2780 | return err ? err : allocated; | ||
2781 | |||
2782 | fix_extent_len: | ||
2783 | ex->ee_block = orig_ex.ee_block; | ||
2784 | ex->ee_len = orig_ex.ee_len; | ||
2785 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | ||
2786 | ext4_ext_mark_uninitialized(ex); | ||
2787 | ext4_ext_dirty(handle, inode, path + depth); | ||
2788 | return err; | ||
2789 | } | ||
2790 | |||
2791 | /* | ||
2792 | * This function is called by ext4_ext_get_blocks() from | ||
2793 | * ext4_get_blocks_dio_write() when DIO to write | ||
2794 | * to an uninitialized extent. | ||
2795 | * | ||
2796 | * Writing to an uninitized extent may result in splitting the uninitialized | ||
2797 | * extent into multiple /intialized unintialized extents (up to three) | ||
2798 | * There are three possibilities: | ||
2799 | * a> There is no split required: Entire extent should be uninitialized | ||
2800 | * b> Splits in two extents: Write is happening at either end of the extent | ||
2801 | * c> Splits in three extents: Somone is writing in middle of the extent | ||
2802 | * | ||
2803 | * One of more index blocks maybe needed if the extent tree grow after | ||
2804 | * the unintialized extent split. To prevent ENOSPC occur at the IO | ||
2805 | * complete, we need to split the uninitialized extent before DIO submit | ||
2806 | * the IO. The uninitilized extent called at this time will be split | ||
2807 | * into three uninitialized extent(at most). After IO complete, the part | ||
2808 | * being filled will be convert to initialized by the end_io callback function | ||
2809 | * via ext4_convert_unwritten_extents(). | ||
2810 | */ | ||
2811 | static int ext4_split_unwritten_extents(handle_t *handle, | ||
2812 | struct inode *inode, | ||
2813 | struct ext4_ext_path *path, | ||
2814 | ext4_lblk_t iblock, | ||
2815 | unsigned int max_blocks, | ||
2816 | int flags) | ||
2817 | { | ||
2818 | struct ext4_extent *ex, newex, orig_ex; | ||
2819 | struct ext4_extent *ex1 = NULL; | ||
2820 | struct ext4_extent *ex2 = NULL; | ||
2821 | struct ext4_extent *ex3 = NULL; | ||
2822 | struct ext4_extent_header *eh; | ||
2823 | ext4_lblk_t ee_block; | ||
2824 | unsigned int allocated, ee_len, depth; | ||
2825 | ext4_fsblk_t newblock; | ||
2826 | int err = 0; | ||
2827 | int ret = 0; | ||
2828 | |||
2829 | ext_debug("ext4_split_unwritten_extents: inode %lu," | ||
2830 | "iblock %llu, max_blocks %u\n", inode->i_ino, | ||
2831 | (unsigned long long)iblock, max_blocks); | ||
2832 | depth = ext_depth(inode); | ||
2833 | eh = path[depth].p_hdr; | ||
2834 | ex = path[depth].p_ext; | ||
2835 | ee_block = le32_to_cpu(ex->ee_block); | ||
2836 | ee_len = ext4_ext_get_actual_len(ex); | ||
2837 | allocated = ee_len - (iblock - ee_block); | ||
2838 | newblock = iblock - ee_block + ext_pblock(ex); | ||
2839 | ex2 = ex; | ||
2840 | orig_ex.ee_block = ex->ee_block; | ||
2841 | orig_ex.ee_len = cpu_to_le16(ee_len); | ||
2842 | ext4_ext_store_pblock(&orig_ex, ext_pblock(ex)); | ||
2843 | |||
2844 | /* | ||
2845 | * if the entire unintialized extent length less than | ||
2846 | * the size of extent to write, there is no need to split | ||
2847 | * uninitialized extent | ||
2848 | */ | ||
2849 | if (allocated <= max_blocks) | ||
2850 | return ret; | ||
2851 | |||
2852 | err = ext4_ext_get_access(handle, inode, path + depth); | ||
2853 | if (err) | ||
2854 | goto out; | ||
2855 | /* ex1: ee_block to iblock - 1 : uninitialized */ | ||
2856 | if (iblock > ee_block) { | ||
2857 | ex1 = ex; | ||
2858 | ex1->ee_len = cpu_to_le16(iblock - ee_block); | ||
2859 | ext4_ext_mark_uninitialized(ex1); | ||
2860 | ex2 = &newex; | ||
2861 | } | ||
2862 | /* | ||
2863 | * for sanity, update the length of the ex2 extent before | ||
2864 | * we insert ex3, if ex1 is NULL. This is to avoid temporary | ||
2865 | * overlap of blocks. | ||
2866 | */ | ||
2867 | if (!ex1 && allocated > max_blocks) | ||
2868 | ex2->ee_len = cpu_to_le16(max_blocks); | ||
2869 | /* ex3: to ee_block + ee_len : uninitialised */ | ||
2870 | if (allocated > max_blocks) { | ||
2871 | unsigned int newdepth; | ||
2872 | ex3 = &newex; | ||
2873 | ex3->ee_block = cpu_to_le32(iblock + max_blocks); | ||
2874 | ext4_ext_store_pblock(ex3, newblock + max_blocks); | ||
2875 | ex3->ee_len = cpu_to_le16(allocated - max_blocks); | ||
2876 | ext4_ext_mark_uninitialized(ex3); | ||
2877 | err = ext4_ext_insert_extent(handle, inode, path, ex3, flags); | ||
2878 | if (err == -ENOSPC) { | ||
2879 | err = ext4_ext_zeroout(inode, &orig_ex); | ||
2880 | if (err) | ||
2881 | goto fix_extent_len; | ||
2882 | /* update the extent length and mark as initialized */ | ||
2883 | ex->ee_block = orig_ex.ee_block; | ||
2884 | ex->ee_len = orig_ex.ee_len; | ||
2885 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | ||
2886 | ext4_ext_dirty(handle, inode, path + depth); | ||
2887 | /* zeroed the full extent */ | ||
2888 | /* blocks available from iblock */ | ||
2889 | return allocated; | ||
2890 | |||
2891 | } else if (err) | ||
2892 | goto fix_extent_len; | ||
2893 | /* | ||
2894 | * The depth, and hence eh & ex might change | ||
2895 | * as part of the insert above. | ||
2896 | */ | ||
2897 | newdepth = ext_depth(inode); | ||
2898 | /* | ||
2899 | * update the extent length after successful insert of the | ||
2900 | * split extent | ||
2901 | */ | ||
2902 | orig_ex.ee_len = cpu_to_le16(ee_len - | ||
2903 | ext4_ext_get_actual_len(ex3)); | ||
2904 | depth = newdepth; | ||
2905 | ext4_ext_drop_refs(path); | ||
2906 | path = ext4_ext_find_extent(inode, iblock, path); | ||
2907 | if (IS_ERR(path)) { | ||
2908 | err = PTR_ERR(path); | ||
2909 | goto out; | ||
2910 | } | ||
2911 | eh = path[depth].p_hdr; | ||
2912 | ex = path[depth].p_ext; | ||
2913 | if (ex2 != &newex) | ||
2914 | ex2 = ex; | ||
2915 | |||
2916 | err = ext4_ext_get_access(handle, inode, path + depth); | ||
2917 | if (err) | ||
2918 | goto out; | ||
2919 | |||
2920 | allocated = max_blocks; | ||
2921 | } | ||
2922 | /* | ||
2923 | * If there was a change of depth as part of the | ||
2924 | * insertion of ex3 above, we need to update the length | ||
2925 | * of the ex1 extent again here | ||
2926 | */ | ||
2927 | if (ex1 && ex1 != ex) { | ||
2928 | ex1 = ex; | ||
2929 | ex1->ee_len = cpu_to_le16(iblock - ee_block); | ||
2930 | ext4_ext_mark_uninitialized(ex1); | ||
2931 | ex2 = &newex; | ||
2932 | } | ||
2933 | /* | ||
2934 | * ex2: iblock to iblock + maxblocks-1 : to be direct IO written, | ||
2935 | * uninitialised still. | ||
2936 | */ | ||
2937 | ex2->ee_block = cpu_to_le32(iblock); | ||
2938 | ext4_ext_store_pblock(ex2, newblock); | ||
2939 | ex2->ee_len = cpu_to_le16(allocated); | ||
2940 | ext4_ext_mark_uninitialized(ex2); | ||
2941 | if (ex2 != ex) | ||
2942 | goto insert; | ||
2943 | /* Mark modified extent as dirty */ | ||
2944 | err = ext4_ext_dirty(handle, inode, path + depth); | ||
2945 | ext_debug("out here\n"); | ||
2946 | goto out; | ||
2947 | insert: | ||
2948 | err = ext4_ext_insert_extent(handle, inode, path, &newex, flags); | ||
2732 | if (err == -ENOSPC) { | 2949 | if (err == -ENOSPC) { |
2733 | err = ext4_ext_zeroout(inode, &orig_ex); | 2950 | err = ext4_ext_zeroout(inode, &orig_ex); |
2734 | if (err) | 2951 | if (err) |
@@ -2743,6 +2960,7 @@ insert: | |||
2743 | } else if (err) | 2960 | } else if (err) |
2744 | goto fix_extent_len; | 2961 | goto fix_extent_len; |
2745 | out: | 2962 | out: |
2963 | ext4_ext_show_leaf(inode, path); | ||
2746 | return err ? err : allocated; | 2964 | return err ? err : allocated; |
2747 | 2965 | ||
2748 | fix_extent_len: | 2966 | fix_extent_len: |
@@ -2753,7 +2971,141 @@ fix_extent_len: | |||
2753 | ext4_ext_dirty(handle, inode, path + depth); | 2971 | ext4_ext_dirty(handle, inode, path + depth); |
2754 | return err; | 2972 | return err; |
2755 | } | 2973 | } |
2974 | static int ext4_convert_unwritten_extents_dio(handle_t *handle, | ||
2975 | struct inode *inode, | ||
2976 | struct ext4_ext_path *path) | ||
2977 | { | ||
2978 | struct ext4_extent *ex; | ||
2979 | struct ext4_extent_header *eh; | ||
2980 | int depth; | ||
2981 | int err = 0; | ||
2982 | int ret = 0; | ||
2983 | |||
2984 | depth = ext_depth(inode); | ||
2985 | eh = path[depth].p_hdr; | ||
2986 | ex = path[depth].p_ext; | ||
2987 | |||
2988 | err = ext4_ext_get_access(handle, inode, path + depth); | ||
2989 | if (err) | ||
2990 | goto out; | ||
2991 | /* first mark the extent as initialized */ | ||
2992 | ext4_ext_mark_initialized(ex); | ||
2993 | |||
2994 | /* | ||
2995 | * We have to see if it can be merged with the extent | ||
2996 | * on the left. | ||
2997 | */ | ||
2998 | if (ex > EXT_FIRST_EXTENT(eh)) { | ||
2999 | /* | ||
3000 | * To merge left, pass "ex - 1" to try_to_merge(), | ||
3001 | * since it merges towards right _only_. | ||
3002 | */ | ||
3003 | ret = ext4_ext_try_to_merge(inode, path, ex - 1); | ||
3004 | if (ret) { | ||
3005 | err = ext4_ext_correct_indexes(handle, inode, path); | ||
3006 | if (err) | ||
3007 | goto out; | ||
3008 | depth = ext_depth(inode); | ||
3009 | ex--; | ||
3010 | } | ||
3011 | } | ||
3012 | /* | ||
3013 | * Try to Merge towards right. | ||
3014 | */ | ||
3015 | ret = ext4_ext_try_to_merge(inode, path, ex); | ||
3016 | if (ret) { | ||
3017 | err = ext4_ext_correct_indexes(handle, inode, path); | ||
3018 | if (err) | ||
3019 | goto out; | ||
3020 | depth = ext_depth(inode); | ||
3021 | } | ||
3022 | /* Mark modified extent as dirty */ | ||
3023 | err = ext4_ext_dirty(handle, inode, path + depth); | ||
3024 | out: | ||
3025 | ext4_ext_show_leaf(inode, path); | ||
3026 | return err; | ||
3027 | } | ||
3028 | |||
3029 | static int | ||
3030 | ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode, | ||
3031 | ext4_lblk_t iblock, unsigned int max_blocks, | ||
3032 | struct ext4_ext_path *path, int flags, | ||
3033 | unsigned int allocated, struct buffer_head *bh_result, | ||
3034 | ext4_fsblk_t newblock) | ||
3035 | { | ||
3036 | int ret = 0; | ||
3037 | int err = 0; | ||
3038 | ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio; | ||
3039 | |||
3040 | ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical" | ||
3041 | "block %llu, max_blocks %u, flags %d, allocated %u", | ||
3042 | inode->i_ino, (unsigned long long)iblock, max_blocks, | ||
3043 | flags, allocated); | ||
3044 | ext4_ext_show_leaf(inode, path); | ||
3045 | |||
3046 | /* DIO get_block() before submit the IO, split the extent */ | ||
3047 | if (flags == EXT4_GET_BLOCKS_DIO_CREATE_EXT) { | ||
3048 | ret = ext4_split_unwritten_extents(handle, | ||
3049 | inode, path, iblock, | ||
3050 | max_blocks, flags); | ||
3051 | /* flag the io_end struct that we need convert when IO done */ | ||
3052 | if (io) | ||
3053 | io->flag = DIO_AIO_UNWRITTEN; | ||
3054 | goto out; | ||
3055 | } | ||
3056 | /* DIO end_io complete, convert the filled extent to written */ | ||
3057 | if (flags == EXT4_GET_BLOCKS_DIO_CONVERT_EXT) { | ||
3058 | ret = ext4_convert_unwritten_extents_dio(handle, inode, | ||
3059 | path); | ||
3060 | goto out2; | ||
3061 | } | ||
3062 | /* buffered IO case */ | ||
3063 | /* | ||
3064 | * repeat fallocate creation request | ||
3065 | * we already have an unwritten extent | ||
3066 | */ | ||
3067 | if (flags & EXT4_GET_BLOCKS_UNINIT_EXT) | ||
3068 | goto map_out; | ||
3069 | |||
3070 | /* buffered READ or buffered write_begin() lookup */ | ||
3071 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { | ||
3072 | /* | ||
3073 | * We have blocks reserved already. We | ||
3074 | * return allocated blocks so that delalloc | ||
3075 | * won't do block reservation for us. But | ||
3076 | * the buffer head will be unmapped so that | ||
3077 | * a read from the block returns 0s. | ||
3078 | */ | ||
3079 | set_buffer_unwritten(bh_result); | ||
3080 | goto out1; | ||
3081 | } | ||
2756 | 3082 | ||
3083 | /* buffered write, writepage time, convert*/ | ||
3084 | ret = ext4_ext_convert_to_initialized(handle, inode, | ||
3085 | path, iblock, | ||
3086 | max_blocks); | ||
3087 | out: | ||
3088 | if (ret <= 0) { | ||
3089 | err = ret; | ||
3090 | goto out2; | ||
3091 | } else | ||
3092 | allocated = ret; | ||
3093 | set_buffer_new(bh_result); | ||
3094 | map_out: | ||
3095 | set_buffer_mapped(bh_result); | ||
3096 | out1: | ||
3097 | if (allocated > max_blocks) | ||
3098 | allocated = max_blocks; | ||
3099 | ext4_ext_show_leaf(inode, path); | ||
3100 | bh_result->b_bdev = inode->i_sb->s_bdev; | ||
3101 | bh_result->b_blocknr = newblock; | ||
3102 | out2: | ||
3103 | if (path) { | ||
3104 | ext4_ext_drop_refs(path); | ||
3105 | kfree(path); | ||
3106 | } | ||
3107 | return err ? err : allocated; | ||
3108 | } | ||
2757 | /* | 3109 | /* |
2758 | * Block allocation/map/preallocation routine for extents based files | 3110 | * Block allocation/map/preallocation routine for extents based files |
2759 | * | 3111 | * |
@@ -2784,9 +3136,10 @@ int ext4_ext_get_blocks(handle_t *handle, struct inode *inode, | |||
2784 | int err = 0, depth, ret, cache_type; | 3136 | int err = 0, depth, ret, cache_type; |
2785 | unsigned int allocated = 0; | 3137 | unsigned int allocated = 0; |
2786 | struct ext4_allocation_request ar; | 3138 | struct ext4_allocation_request ar; |
3139 | ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio; | ||
2787 | 3140 | ||
2788 | __clear_bit(BH_New, &bh_result->b_state); | 3141 | __clear_bit(BH_New, &bh_result->b_state); |
2789 | ext_debug("blocks %u/%u requested for inode %u\n", | 3142 | ext_debug("blocks %u/%u requested for inode %lu\n", |
2790 | iblock, max_blocks, inode->i_ino); | 3143 | iblock, max_blocks, inode->i_ino); |
2791 | 3144 | ||
2792 | /* check in cache */ | 3145 | /* check in cache */ |
@@ -2849,7 +3202,7 @@ int ext4_ext_get_blocks(handle_t *handle, struct inode *inode, | |||
2849 | newblock = iblock - ee_block + ee_start; | 3202 | newblock = iblock - ee_block + ee_start; |
2850 | /* number of remaining blocks in the extent */ | 3203 | /* number of remaining blocks in the extent */ |
2851 | allocated = ee_len - (iblock - ee_block); | 3204 | allocated = ee_len - (iblock - ee_block); |
2852 | ext_debug("%u fit into %lu:%d -> %llu\n", iblock, | 3205 | ext_debug("%u fit into %u:%d -> %llu\n", iblock, |
2853 | ee_block, ee_len, newblock); | 3206 | ee_block, ee_len, newblock); |
2854 | 3207 | ||
2855 | /* Do not put uninitialized extent in the cache */ | 3208 | /* Do not put uninitialized extent in the cache */ |
@@ -2859,33 +3212,10 @@ int ext4_ext_get_blocks(handle_t *handle, struct inode *inode, | |||
2859 | EXT4_EXT_CACHE_EXTENT); | 3212 | EXT4_EXT_CACHE_EXTENT); |
2860 | goto out; | 3213 | goto out; |
2861 | } | 3214 | } |
2862 | if (flags & EXT4_GET_BLOCKS_UNINIT_EXT) | 3215 | ret = ext4_ext_handle_uninitialized_extents(handle, |
2863 | goto out; | 3216 | inode, iblock, max_blocks, path, |
2864 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { | 3217 | flags, allocated, bh_result, newblock); |
2865 | if (allocated > max_blocks) | 3218 | return ret; |
2866 | allocated = max_blocks; | ||
2867 | /* | ||
2868 | * We have blocks reserved already. We | ||
2869 | * return allocated blocks so that delalloc | ||
2870 | * won't do block reservation for us. But | ||
2871 | * the buffer head will be unmapped so that | ||
2872 | * a read from the block returns 0s. | ||
2873 | */ | ||
2874 | set_buffer_unwritten(bh_result); | ||
2875 | bh_result->b_bdev = inode->i_sb->s_bdev; | ||
2876 | bh_result->b_blocknr = newblock; | ||
2877 | goto out2; | ||
2878 | } | ||
2879 | |||
2880 | ret = ext4_ext_convert_to_initialized(handle, inode, | ||
2881 | path, iblock, | ||
2882 | max_blocks); | ||
2883 | if (ret <= 0) { | ||
2884 | err = ret; | ||
2885 | goto out2; | ||
2886 | } else | ||
2887 | allocated = ret; | ||
2888 | goto outnew; | ||
2889 | } | 3219 | } |
2890 | } | 3220 | } |
2891 | 3221 | ||
@@ -2950,15 +3280,27 @@ int ext4_ext_get_blocks(handle_t *handle, struct inode *inode, | |||
2950 | newblock = ext4_mb_new_blocks(handle, &ar, &err); | 3280 | newblock = ext4_mb_new_blocks(handle, &ar, &err); |
2951 | if (!newblock) | 3281 | if (!newblock) |
2952 | goto out2; | 3282 | goto out2; |
2953 | ext_debug("allocate new block: goal %llu, found %llu/%lu\n", | 3283 | ext_debug("allocate new block: goal %llu, found %llu/%u\n", |
2954 | ar.goal, newblock, allocated); | 3284 | ar.goal, newblock, allocated); |
2955 | 3285 | ||
2956 | /* try to insert new extent into found leaf and return */ | 3286 | /* try to insert new extent into found leaf and return */ |
2957 | ext4_ext_store_pblock(&newex, newblock); | 3287 | ext4_ext_store_pblock(&newex, newblock); |
2958 | newex.ee_len = cpu_to_le16(ar.len); | 3288 | newex.ee_len = cpu_to_le16(ar.len); |
2959 | if (flags & EXT4_GET_BLOCKS_UNINIT_EXT) /* Mark uninitialized */ | 3289 | /* Mark uninitialized */ |
3290 | if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){ | ||
2960 | ext4_ext_mark_uninitialized(&newex); | 3291 | ext4_ext_mark_uninitialized(&newex); |
2961 | err = ext4_ext_insert_extent(handle, inode, path, &newex); | 3292 | /* |
3293 | * io_end structure was created for every async | ||
3294 | * direct IO write to the middle of the file. | ||
3295 | * To avoid unecessary convertion for every aio dio rewrite | ||
3296 | * to the mid of file, here we flag the IO that is really | ||
3297 | * need the convertion. | ||
3298 | * | ||
3299 | */ | ||
3300 | if (io && flags == EXT4_GET_BLOCKS_DIO_CREATE_EXT) | ||
3301 | io->flag = DIO_AIO_UNWRITTEN; | ||
3302 | } | ||
3303 | err = ext4_ext_insert_extent(handle, inode, path, &newex, flags); | ||
2962 | if (err) { | 3304 | if (err) { |
2963 | /* free data blocks we just allocated */ | 3305 | /* free data blocks we just allocated */ |
2964 | /* not a good idea to call discard here directly, | 3306 | /* not a good idea to call discard here directly, |
@@ -2972,7 +3314,6 @@ int ext4_ext_get_blocks(handle_t *handle, struct inode *inode, | |||
2972 | /* previous routine could use block we allocated */ | 3314 | /* previous routine could use block we allocated */ |
2973 | newblock = ext_pblock(&newex); | 3315 | newblock = ext_pblock(&newex); |
2974 | allocated = ext4_ext_get_actual_len(&newex); | 3316 | allocated = ext4_ext_get_actual_len(&newex); |
2975 | outnew: | ||
2976 | set_buffer_new(bh_result); | 3317 | set_buffer_new(bh_result); |
2977 | 3318 | ||
2978 | /* Cache only when it is _not_ an uninitialized extent */ | 3319 | /* Cache only when it is _not_ an uninitialized extent */ |
@@ -3171,6 +3512,63 @@ retry: | |||
3171 | } | 3512 | } |
3172 | 3513 | ||
3173 | /* | 3514 | /* |
3515 | * This function convert a range of blocks to written extents | ||
3516 | * The caller of this function will pass the start offset and the size. | ||
3517 | * all unwritten extents within this range will be converted to | ||
3518 | * written extents. | ||
3519 | * | ||
3520 | * This function is called from the direct IO end io call back | ||
3521 | * function, to convert the fallocated extents after IO is completed. | ||
3522 | */ | ||
3523 | int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset, | ||
3524 | loff_t len) | ||
3525 | { | ||
3526 | handle_t *handle; | ||
3527 | ext4_lblk_t block; | ||
3528 | unsigned int max_blocks; | ||
3529 | int ret = 0; | ||
3530 | int ret2 = 0; | ||
3531 | struct buffer_head map_bh; | ||
3532 | unsigned int credits, blkbits = inode->i_blkbits; | ||
3533 | |||
3534 | block = offset >> blkbits; | ||
3535 | /* | ||
3536 | * We can't just convert len to max_blocks because | ||
3537 | * If blocksize = 4096 offset = 3072 and len = 2048 | ||
3538 | */ | ||
3539 | max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) | ||
3540 | - block; | ||
3541 | /* | ||
3542 | * credits to insert 1 extent into extent tree | ||
3543 | */ | ||
3544 | credits = ext4_chunk_trans_blocks(inode, max_blocks); | ||
3545 | while (ret >= 0 && ret < max_blocks) { | ||
3546 | block = block + ret; | ||
3547 | max_blocks = max_blocks - ret; | ||
3548 | handle = ext4_journal_start(inode, credits); | ||
3549 | if (IS_ERR(handle)) { | ||
3550 | ret = PTR_ERR(handle); | ||
3551 | break; | ||
3552 | } | ||
3553 | map_bh.b_state = 0; | ||
3554 | ret = ext4_get_blocks(handle, inode, block, | ||
3555 | max_blocks, &map_bh, | ||
3556 | EXT4_GET_BLOCKS_DIO_CONVERT_EXT); | ||
3557 | if (ret <= 0) { | ||
3558 | WARN_ON(ret <= 0); | ||
3559 | printk(KERN_ERR "%s: ext4_ext_get_blocks " | ||
3560 | "returned error inode#%lu, block=%u, " | ||
3561 | "max_blocks=%u", __func__, | ||
3562 | inode->i_ino, block, max_blocks); | ||
3563 | } | ||
3564 | ext4_mark_inode_dirty(handle, inode); | ||
3565 | ret2 = ext4_journal_stop(handle); | ||
3566 | if (ret <= 0 || ret2 ) | ||
3567 | break; | ||
3568 | } | ||
3569 | return ret > 0 ? ret2 : ret; | ||
3570 | } | ||
3571 | /* | ||
3174 | * Callback function called for each extent to gather FIEMAP information. | 3572 | * Callback function called for each extent to gather FIEMAP information. |
3175 | */ | 3573 | */ |
3176 | static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path, | 3574 | static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path, |