From 0a905364fafb1b7e6927c8eec842cb6033500104 Mon Sep 17 00:00:00 2001 From: Milan Broz Date: Sat, 29 Jan 2011 15:55:27 +0000 Subject: [PATCH 1/1] Fix mapping removal if device disappeared but node still exists. git-svn-id: https://cryptsetup.googlecode.com/svn/trunk@420 36d66b0a-2a48-0410-832c-cd162a569da5 --- ChangeLog | 3 +++ lib/setup.c | 14 +++++++++++--- lib/utils.c | 2 +- tests/compat-test | 10 ++++++++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2f0ed03..16d9baf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +2011-01-29 Milan Broz + * Fix mapping removal if device disappeared but node still exists. + 2011-01-25 Milan Broz * Add loop-AES handling (loopaesOpen and loopaesClose commands). (requires kernel 2.6.38 and above) diff --git a/lib/setup.c b/lib/setup.c index 01b202e..a264fa2 100644 --- a/lib/setup.c +++ b/lib/setup.c @@ -1024,13 +1024,21 @@ int crypt_init_by_name(struct crypt_device **cd, const char *name) if (r < 0) goto out; + *cd = NULL; + r = crypt_init(cd, device); + /* Underlying device disappeared but mapping still active */ - if (!device) + if (!device || r == -ENOTBLK) log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"), name); - *cd = NULL; - r = crypt_init(cd, device); + /* Underlying device is not readable but crypt mapping exists */ + if (r == -ENOTBLK) { + free(device); + device = NULL; + r = crypt_init(cd, NULL); + } + if (r < 0) goto out; diff --git a/lib/utils.c b/lib/utils.c index d950a42..a7408e3 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -260,7 +260,7 @@ int device_ready(struct crypt_device *cd, const char *device, int mode) /* Try to read first sector */ s = read_blockwise(devfd, buf, sizeof(buf)); if (s < 0 || s != sizeof(buf)) { - log_err(cd, _("Cannot read device %s.\n"), device); + log_verbose(cd, _("Cannot read device %s.\n"), device); r = 0; } diff --git a/tests/compat-test b/tests/compat-test index 29cb943..c073765 100755 --- a/tests/compat-test +++ b/tests/compat-test @@ -278,5 +278,15 @@ echo "bad" | $CRYPTSETUP luksDump $LOOPDEV --dump-master-key 2>/dev/null && fail echo "key0" | $CRYPTSETUP luksDump $LOOPDEV --dump-master-key | grep -q "MK dump:" || fail $CRYPTSETUP luksDump -q $LOOPDEV --dump-master-key -d $KEY1 | grep -q "MK dump:" || fail +prepare "[22] remove disappeared device" wipe +dmsetup create $DEV_NAME --table "0 5000 linear $LOOPDEV 2" || fail +echo "key0" | $CRYPTSETUP -q -i 0 luksFormat /dev/mapper/$DEV_NAME || fail +echo "key0" | $CRYPTSETUP -q luksOpen /dev/mapper/$DEV_NAME $DEV_NAME2 || fail +# underlying device now returns error but node is still present +dmsetup load $DEV_NAME --table "0 5000 error" || fail +dmsetup resume $DEV_NAME || fail +$CRYPTSETUP -q luksClose $DEV_NAME2 || fail +dmsetup remove $DEV_NAME || fail + remove_mapping exit 0 -- 2.7.4