linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Darrick J. Wong" <darrick.wong@oracle.com>
To: tytso@mit.edu, darrick.wong@oracle.com
Cc: linux-ext4@vger.kernel.org
Subject: [PATCH 6/6] libext2fs: try to roll back when splitting an extent fails
Date: Mon, 20 Jan 2014 22:22:17 -0800	[thread overview]
Message-ID: <20140121062217.20507.51789.stgit@birch.djwong.org> (raw)
In-Reply-To: <20140121062138.20507.60259.stgit@birch.djwong.org>

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>
---
 lib/ext2fs/extent.c |   25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)


diff --git a/lib/ext2fs/extent.c b/lib/ext2fs/extent.c
index 2928695..87921e6 100644
--- a/lib/ext2fs/extent.c
+++ b/lib/ext2fs/extent.c
@@ -1438,13 +1438,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);
@@ -1456,8 +1460,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;
@@ -1465,8 +1474,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:


  parent reply	other threads:[~2014-01-21  6:22 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-21  6:21 [PATCH 0/6] miscellaneous e2fsprogs fixes Darrick J. Wong
2014-01-21  6:21 ` [PATCH 1/6] misc: fix resource leaks in e2fsprogs Darrick J. Wong
2014-01-21  7:25   ` Zheng Liu
2014-01-21 18:18     ` Darrick J. Wong
2014-01-21  6:21 ` [PATCH 2/6] mke2fs: clean up kernel version tests Darrick J. Wong
2014-01-21  7:32   ` Zheng Liu
2014-02-06 20:28     ` Theodore Ts'o
2014-01-21  6:21 ` [PATCH 3/6] libext2fs: iterate past lower extents during punch Darrick J. Wong
2014-01-21  7:40   ` Zheng Liu
2014-02-06 20:29     ` Theodore Ts'o
2014-01-21  6:22 ` [PATCH 4/6] libext2fs: during punch, fix parent extents after modifying extent Darrick J. Wong
2014-01-21  7:43   ` Zheng Liu
2014-02-06 20:31     ` Theodore Ts'o
2014-01-21  6:22 ` [PATCH 5/6] libext2fs: don't hang on to unmapped block if extent tree update fails Darrick J. Wong
2014-01-21  7:45   ` Zheng Liu
2014-01-21  6:22 ` Darrick J. Wong [this message]
2014-01-21  7:49   ` [PATCH 6/6] libext2fs: try to roll back when splitting an extent fails Zheng Liu
2014-02-06 20:34     ` Theodore Ts'o

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140121062217.20507.51789.stgit@birch.djwong.org \
    --to=darrick.wong@oracle.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=tytso@mit.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).