libext2fs: ext2fs_extents_fix_parents() should not modify the handle location

Previously, ext2fs_extent_fix_parents() would only avoid modifying the
cursor location associated with the extent handle the cursor was
pointed at a leaf node in the extent tree.  This is because it saved
the starting logical block number of the current extent, but not the
"level" of the extent (where level 0 is the leaf node, level 1 is the
interior node which points at blocks containing leaf nodes, etc.)

Fix ext2fs_extent_fix_parents() so it is guaranteed to not change the
current extent in the handle even if the current extent is not at the
bottom of the tree.

Also add a fix_extent command to the tst_extents program to make it
easier to test this function.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
diff --git a/lib/ext2fs/extent.c b/lib/ext2fs/extent.c
index 95a0a86..c6716bc 100644
--- a/lib/ext2fs/extent.c
+++ b/lib/ext2fs/extent.c
@@ -709,9 +709,11 @@
 errcode_t ext2fs_extent_fix_parents(ext2_extent_handle_t handle)
 {
 	int				retval = 0;
+	int				orig_height;
 	blk64_t				start;
 	struct extent_path		*path;
 	struct ext2fs_extent		extent;
+	struct ext2_extent_info		info;
 
 	EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EXTENT_HANDLE);
 
@@ -732,6 +734,10 @@
 	/* modified node's start block */
 	start = extent.e_lblk;
 
+	if ((retval = ext2fs_extent_get_info(handle, &info)))
+		return retval;
+	orig_height = info.max_depth - info.curr_level;
+
 	/* traverse up until index not first, or startblk matches, or top */
 	while (handle->level > 0 &&
 	       (path->left == path->entries - 1)) {
@@ -750,7 +756,7 @@
 	}
 
 	/* put handle back to where we started */
-	retval = ext2fs_extent_goto(handle, start);
+	retval = extent_goto(handle, orig_height, start);
 done:
 	return retval;
 }
@@ -1943,6 +1949,23 @@
 	}
 }
 
+void do_fix_parents(int argc, char **argv)
+{
+	struct ext2fs_extent	extent;
+	struct ext2_extent_info	info;
+	errcode_t		retval;
+
+	if (common_extent_args_process(argc, argv, 1, 1, "fix_parents", "",
+				       CHECK_FS_RW))
+		return;
+
+	retval = ext2fs_extent_fix_parents(current_handle);
+	if (retval) {
+		com_err(argv[0], retval, 0);
+		return;
+	}
+}
+
 void do_info(int argc, char **argv)
 {
 	struct ext2fs_extent	extent;
diff --git a/lib/ext2fs/extent_dbg.ct b/lib/ext2fs/extent_dbg.ct
index d0571f4..c1d8033 100644
--- a/lib/ext2fs/extent_dbg.ct
+++ b/lib/ext2fs/extent_dbg.ct
@@ -55,6 +55,9 @@
 request do_split_node, "Split node",
 	split_node, split;
 
+request do_fix_parents, "Fix parents",
+	fix_parents, fixp;
+
 request do_set_bmap, "Set block mapping",
 	set_bmap;