TCRYPT: support proper device removal

This commit is contained in:
Milan Broz
2012-11-22 14:19:43 +01:00
parent c26bb0f38a
commit 3faaa8b227
3 changed files with 64 additions and 2 deletions

View File

@@ -573,3 +573,52 @@ int TCRYPT_activate(struct crypt_device *cd,
crypt_free_volume_key(dmd.u.crypt.vk);
return r;
}
static int remove_one(struct crypt_device *cd, const char *name,
const char *base_uuid, int index)
{
struct crypt_dm_active_device dmd = {};
char dm_name[PATH_MAX];
int r;
if (snprintf(dm_name, sizeof(dm_name), "%s_%d", name, index) < 0)
return -ENOMEM;
r = dm_status_device(cd, dm_name);
if (r < 0)
return r;
r = dm_query_device(cd, dm_name, DM_ACTIVE_UUID, &dmd);
if (!r && !strncmp(dmd.uuid, base_uuid, strlen(base_uuid)))
r = dm_remove_device(cd, dm_name, 0, 0);
free(CONST_CAST(void*)dmd.uuid);
return r;
}
int TCRYPT_deactivate(struct crypt_device *cd, const char *name)
{
struct crypt_dm_active_device dmd = {};
int r;
r = dm_query_device(cd, name, DM_ACTIVE_UUID, &dmd);
if (r < 0)
return r;
if (!dmd.uuid)
return -EINVAL;
r = dm_remove_device(cd, name, 0, 0);
if (r < 0)
goto out;
r = remove_one(cd, name, dmd.uuid, 1);
if (r < 0)
goto out;
r = remove_one(cd, name, dmd.uuid, 2);
if (r < 0)
goto out;
out:
free(CONST_CAST(void*)dmd.uuid);
return (r == -ENODEV) ? 0 : r;
}