diff options
author | Darrick J. Wong <darrick.wong@oracle.com> | 2014-02-06 15:34:00 -0500 |
---|---|---|
committer | Theodore Ts'o <tytso@mit.edu> | 2014-02-06 15:34:00 -0500 |
commit | 156a151f3177a826f8ad7c360440abf6656f5aae (patch) | |
tree | 5fdf29365148efe78b764e3156e95963210b80d1 /lib | |
parent | 6225a154d2616edcd13885797ed5e1c0cc51e0b7 (diff) | |
download | e2fsprogs-156a151f3177a826f8ad7c360440abf6656f5aae.tar.gz |
libext2fs: try to roll back when splitting an extent fails
If a client asks us to remap a block in the middle of an extent, we
potentially have to allocate a fair number of blocks to handle extent
tree splits. A failure in either of the ext2fs_extent_insert calls
leaves us with an extent tree that no longer maps the logical block in
question and everything that came after it! Therefore, try to roll
back the extent tree changes before returning an error code.
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: Zheng Liu <wenqing.lz@taobao.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ext2fs/extent.c | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/lib/ext2fs/extent.c b/lib/ext2fs/extent.c index 6f4f1d21..0bf82ea4 100644 --- a/lib/ext2fs/extent.c +++ b/lib/ext2fs/extent.c @@ -1419,13 +1419,17 @@ errcode_t ext2fs_extent_set_bmap(ext2_extent_handle_t handle, goto done; } else { __u32 orig_length; + blk64_t orig_lblk; + struct ext2fs_extent orig_extent; + errcode_t r2; #ifdef DEBUG printf("(re/un)mapping in middle of extent\n"); #endif /* need to split this extent; later */ - + orig_lblk = extent.e_lblk; orig_length = extent.e_len; + orig_extent = extent; /* shorten pre-split extent */ extent.e_len = (logical - extent.e_lblk); @@ -1437,8 +1441,13 @@ errcode_t ext2fs_extent_set_bmap(ext2_extent_handle_t handle, /* insert new extent after current */ retval = ext2fs_extent_insert(handle, EXT2_EXTENT_INSERT_AFTER, &newextent); - if (retval) + if (retval) { + r2 = ext2fs_extent_goto(handle, orig_lblk); + if (r2 == 0) + ext2fs_extent_replace(handle, 0, + &orig_extent); goto done; + } } /* add post-split extent */ extent.e_pblk += extent.e_len + 1; @@ -1446,8 +1455,18 @@ errcode_t ext2fs_extent_set_bmap(ext2_extent_handle_t handle, extent.e_len = orig_length - extent.e_len - 1; retval = ext2fs_extent_insert(handle, EXT2_EXTENT_INSERT_AFTER, &extent); - if (retval) + if (retval) { + if (physical) { + r2 = ext2fs_extent_goto(handle, + newextent.e_lblk); + if (r2 == 0) + ext2fs_extent_delete(handle, 0); + } + r2 = ext2fs_extent_goto(handle, orig_lblk); + if (r2 == 0) + ext2fs_extent_replace(handle, 0, &orig_extent); goto done; + } } done: |