OSDN Git Service

libext2fs: fix punching extents when there are no left extents
authorDarrick J. Wong <darrick.wong@oracle.com>
Thu, 12 Dec 2013 18:23:51 +0000 (13:23 -0500)
committerTheodore Ts'o <tytso@mit.edu>
Thu, 12 Dec 2013 18:23:54 +0000 (13:23 -0500)
When deleting an entire extent, we cannot always slip to the previous
leaf extent because there might not /be/ a previous extent.
Attempting to correct for that error by asking for the 'current' leaf
extent also doesn't work, because the failed attempt to change to the
previous extent leaves us with no current extent.

Fix this problem by recording the lblk of the next extent before
deleting the current extent and _goto()ing to the next extent after
the deletion.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
lib/ext2fs/punch.c

index ceec336..9dfba2e 100644 (file)
@@ -267,22 +267,41 @@ static errcode_t ext2fs_punch_extent(ext2_filsys fs, ext2_ino_t ino,
                        retval = ext2fs_extent_replace(handle, 0, &extent);
                } else {
                        struct ext2fs_extent    newex;
+                       blk64_t                 old_lblk, next_lblk;
                        dbg_printf("deleting current extent%s\n", "");
-                       retval = ext2fs_extent_delete(handle, 0);
-                       if (retval)
-                               goto errout;
+
                        /*
-                        * We just moved the next extent into the current
-                        * extent's position, so re-read the extent next time.
+                        * Save the location of the next leaf, then slip
+                        * back to the current extent.
                         */
+                       retval = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT,
+                                                  &newex);
+                       if (retval)
+                               goto errout;
+                       old_lblk = newex.e_lblk;
+
                        retval = ext2fs_extent_get(handle,
-                                                  EXT2_EXTENT_PREV_LEAF,
+                                                  EXT2_EXTENT_NEXT_LEAF,
                                                   &newex);
-                       /* Can't go back? Just reread current. */
-                       if (retval == EXT2_ET_EXTENT_NO_PREV) {
-                               retval = 0;
-                               op = EXT2_EXTENT_CURRENT;
-                       }
+                       if (retval == EXT2_ET_EXTENT_NO_NEXT)
+                               next_lblk = old_lblk;
+                       else if (retval)
+                               goto errout;
+                       else
+                               next_lblk = newex.e_lblk;
+
+                       retval = ext2fs_extent_goto(handle, old_lblk);
+                       if (retval)
+                               goto errout;
+
+                       /* Now delete the extent. */
+                       retval = ext2fs_extent_delete(handle, 0);
+                       if (retval)
+                               goto errout;
+
+                       /* Jump forward to the next extent. */
+                       ext2fs_extent_goto(handle, next_lblk);
+                       op = EXT2_EXTENT_CURRENT;
                }
                if (retval)
                        goto errout;