*
* Copyright (C) 2004, Christophe Saout <christophe@saout.de>
* Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
- * Copyright (C) 2009-2011, Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
if (crypt_maj >= 1 && crypt_min >= 8)
_dm_crypt_flags |= DM_PLAIN64_SUPPORTED;
+ if (crypt_maj >= 1 && crypt_min >= 11)
+ _dm_crypt_flags |= DM_DISCARDS_SUPPORTED;
+
/* Repeat test if dm-crypt is not present */
if (crypt_maj > 0)
_dm_crypt_checked = 1;
}
/* Return path to DM device */
-char *dm_device_path(int major, int minor)
+char *dm_device_path(const char *prefix, int major, int minor)
{
struct dm_task *dmt;
const char *name;
return NULL;
}
- if (snprintf(path, sizeof(path), "/dev/mapper/%s", name) < 0)
+ if (snprintf(path, sizeof(path), "%s%s", prefix ?: "", name) < 0)
path[0] = '\0';
dm_task_destroy(dmt);
sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
}
-static char *get_params(const char *device, uint64_t skip, uint64_t offset,
- const char *cipher, size_t key_size, const char *key)
+static char *get_params(struct crypt_dm_active_device *dmd)
{
- char *params;
- char *hexkey;
+ int r, max_size, null_cipher = 0;
+ char *params, *hexkey;
+ const char *features = "";
+
+ if (dmd->flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) {
+ if (dm_flags() & DM_DISCARDS_SUPPORTED) {
+ features = " 1 allow_discards";
+ log_dbg("Discard/TRIM is allowed.");
+ } else
+ log_dbg("Discard/TRIM is not supported by the kernel.");
+ }
+
+ if (!strncmp(dmd->cipher, "cipher_null-", 12))
+ null_cipher = 1;
- hexkey = crypt_safe_alloc(key_size * 2 + 1);
+ hexkey = crypt_safe_alloc(null_cipher ? 2 : (dmd->vk->keylength * 2 + 1));
if (!hexkey)
return NULL;
- hex_key(hexkey, key_size, key);
+ if (null_cipher)
+ strncpy(hexkey, "-", 2);
+ else
+ hex_key(hexkey, dmd->vk->keylength, dmd->vk->key);
- params = crypt_safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
+ max_size = strlen(hexkey) + strlen(dmd->cipher) +
+ strlen(dmd->device) + strlen(features) + 64;
+ params = crypt_safe_alloc(max_size);
if (!params)
goto out;
- sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
- cipher, hexkey, skip, device, offset);
-
+ r = snprintf(params, max_size, "%s %s %" PRIu64 " %s %" PRIu64 "%s",
+ dmd->cipher, hexkey, dmd->iv_offset, dmd->device,
+ dmd->offset, features);
+ if (r < 0 || r >= max_size) {
+ crypt_safe_free(params);
+ params = NULL;
+ }
out:
crypt_safe_free(hexkey);
return params;
if (name && !dm_task_set_name(dmt, name))
goto out;
+#if HAVE_DECL_DM_TASK_RETRY_REMOVE
+ /* Used only in DM_DEVICE_REMOVE */
+ if (name && !dm_task_retry_remove(dmt))
+ goto out;
+#endif
if (udev_wait && !_dm_task_set_cookie(dmt, &cookie, 0))
goto out;
}
int dm_create_device(const char *name,
- const char *device,
- const char *cipher,
const char *type,
- const char *uuid,
- uint64_t size,
- uint64_t skip,
- uint64_t offset,
- size_t key_size,
- const char *key,
- int read_only,
+ struct crypt_dm_active_device *dmd,
int reload)
{
struct dm_task *dmt = NULL;
struct dm_info dmi;
char *params = NULL;
- char *error = NULL;
char dev_uuid[DM_UUID_LEN] = {0};
int r = -EINVAL;
uint32_t read_ahead = 0;
uint32_t cookie = 0;
uint16_t udev_flags = 0;
- params = get_params(device, skip, offset, cipher, key_size, key);
+ params = get_params(dmd);
if (!params)
goto out_no_removal;
- if (type && !strncmp(type, "TEMP", 4))
+ if (dmd->flags & CRYPT_ACTIVATE_PRIVATE)
udev_flags = CRYPT_TEMP_UDEV_FLAGS;
/* All devices must have DM_UUID, only resize on old device is exception */
if (!dm_task_set_name(dmt, name))
goto out_no_removal;
} else {
- dm_prepare_uuid(name, type, uuid, dev_uuid, sizeof(dev_uuid));
+ dm_prepare_uuid(name, type, dmd->uuid, dev_uuid, sizeof(dev_uuid));
if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
goto out_no_removal;
if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
goto out_no_removal;
- if (read_only && !dm_task_set_ro(dmt))
+ if ((dmd->flags & CRYPT_ACTIVATE_READONLY) && !dm_task_set_ro(dmt))
goto out_no_removal;
- if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
+ if (!dm_task_add_target(dmt, 0, dmd->size, DM_CRYPT_TARGET, params))
goto out_no_removal;
#ifdef DM_READ_AHEAD_MINIMUM_FLAG
- if (device_read_ahead(device, &read_ahead) &&
+ if (device_read_ahead(dmd->device, &read_ahead) &&
!dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
goto out_no_removal;
#endif
goto out;
if (!dm_task_set_name(dmt, name))
goto out;
- if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
+ if (dmd->uuid && !dm_task_set_uuid(dmt, dev_uuid))
goto out;
if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
goto out;
cookie = 0;
}
- if (r < 0 && !reload) {
- if (get_error())
- error = strdup(get_error());
-
+ if (r < 0 && !reload)
dm_remove_device(name, 0, 0);
- if (error) {
- set_error(error);
- free(error);
- }
- }
-
out_no_removal:
if (cookie && _dm_use_udev())
(void)_dm_udev_wait(cookie);
return r;
}
-int dm_status_device(const char *name)
+static int dm_status_dmi(const char *name, struct dm_info *dmi)
{
struct dm_task *dmt;
- struct dm_info dmi;
uint64_t start, length;
char *target_type, *params;
void *next = NULL;
if (!dm_task_run(dmt))
goto out;
- if (!dm_task_get_info(dmt, &dmi))
+ if (!dm_task_get_info(dmt, dmi))
goto out;
- if (!dmi.exists) {
+ if (!dmi->exists) {
r = -ENODEV;
goto out;
}
start != 0 || next)
r = -EINVAL;
else
- r = (dmi.open_count > 0);
+ r = 0;
out:
if (dmt)
dm_task_destroy(dmt);
return r;
}
-int dm_query_device(const char *name,
- char **device,
- uint64_t *size,
- uint64_t *skip,
- uint64_t *offset,
- char **cipher,
- int *key_size,
- char **key,
- int *read_only,
- int *suspended,
- char **uuid)
+int dm_status_device(const char *name)
+{
+ int r;
+ struct dm_info dmi;
+
+ r = dm_status_dmi(name, &dmi);
+ if (r < 0)
+ return r;
+
+ return (dmi.open_count > 0);
+}
+
+int dm_status_suspended(const char *name)
+{
+ int r;
+ struct dm_info dmi;
+
+ r = dm_status_dmi(name, &dmi);
+ if (r < 0)
+ return r;
+
+ return dmi.suspended ? 1 : 0;
+}
+
+int dm_query_device(const char *name, uint32_t get_flags,
+ struct crypt_dm_active_device *dmd)
{
struct dm_task *dmt;
struct dm_info dmi;
uint64_t start, length, val64;
- char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3];
+ char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *arg;
const char *tmp_uuid;
void *next = NULL;
- int i, r = -EINVAL;
+ unsigned int i;
+ int r = -EINVAL;
+
+ memset(dmd, 0, sizeof(*dmd));
if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
goto out;
goto out;
}
+ tmp_uuid = dm_task_get_uuid(dmt);
+
next = dm_get_next_target(dmt, next, &start, &length,
&target_type, ¶ms);
if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
start != 0 || next)
goto out;
- if (size)
- *size = length;
+ dmd->size = length;
rcipher = strsep(¶ms, " ");
/* cipher */
- if (cipher)
- *cipher = strdup(rcipher);
+ if (get_flags & DM_ACTIVE_CIPHER)
+ dmd->cipher = strdup(rcipher);
/* skip */
key_ = strsep(¶ms, " ");
if (*params != ' ')
goto out;
params++;
- if (skip)
- *skip = val64;
+
+ dmd->iv_offset = val64;
/* device */
rdevice = strsep(¶ms, " ");
- if (device)
- *device = crypt_lookup_dev(rdevice);
+ if (get_flags & DM_ACTIVE_DEVICE)
+ dmd->device = crypt_lookup_dev(rdevice);
/*offset */
if (!params)
goto out;
val64 = strtoull(params, ¶ms, 10);
- if (*params)
- goto out;
- if (offset)
- *offset = val64;
+ dmd->offset = val64;
+
+ /* Features section, available since crypt target version 1.11 */
+ if (*params) {
+ if (*params != ' ')
+ goto out;
+ params++;
+
+ /* Number of arguments */
+ val64 = strtoull(params, ¶ms, 10);
+ if (*params != ' ')
+ goto out;
+ params++;
- /* key_size */
- if (key_size)
- *key_size = strlen(key_) / 2;
+ for (i = 0; i < val64; i++) {
+ if (!params)
+ goto out;
+ arg = strsep(¶ms, " ");
+ if (!strcasecmp(arg, "allow_discards"))
+ dmd->flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
+ else /* unknown option */
+ goto out;
+ }
+
+ /* All parameters shold be processed */
+ if (params)
+ goto out;
+ }
- /* key */
- if (key_size && key) {
- *key = crypt_safe_alloc(*key_size);
- if (!*key) {
+ /* Never allow to return empty key */
+ if ((get_flags & DM_ACTIVE_KEY) && dmi.suspended) {
+ log_dbg("Cannot read volume key while suspended.");
+ r = -EINVAL;
+ goto out;
+ }
+
+ if (get_flags & DM_ACTIVE_KEYSIZE) {
+ dmd->vk = crypt_alloc_volume_key(strlen(key_) / 2, NULL);
+ if (!dmd->vk) {
r = -ENOMEM;
goto out;
}
- buffer[2] = '\0';
- for(i = 0; i < *key_size; i++) {
- memcpy(buffer, &key_[i * 2], 2);
- (*key)[i] = strtoul(buffer, &endp, 16);
- if (endp != &buffer[2]) {
- crypt_safe_free(key);
- *key = NULL;
- goto out;
+ if (get_flags & DM_ACTIVE_KEY) {
+ buffer[2] = '\0';
+ for(i = 0; i < dmd->vk->keylength; i++) {
+ memcpy(buffer, &key_[i * 2], 2);
+ dmd->vk->key[i] = strtoul(buffer, &endp, 16);
+ if (endp != &buffer[2]) {
+ crypt_free_volume_key(dmd->vk);
+ dmd->vk = NULL;
+ r = -EINVAL;
+ goto out;
+ }
}
}
}
memset(key_, 0, strlen(key_));
- if (read_only)
- *read_only = dmi.read_only;
-
- if (suspended)
- *suspended = dmi.suspended;
+ if (dmi.read_only)
+ dmd->flags |= CRYPT_ACTIVATE_READONLY;
- if (uuid && (tmp_uuid = dm_task_get_uuid(dmt)) &&
- !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
- *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
+ if (!tmp_uuid)
+ dmd->flags |= CRYPT_ACTIVATE_NO_UUID;
+ else if (get_flags & DM_ACTIVE_UUID) {
+ if (!strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
+ dmd->uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
+ }
r = (dmi.open_count > 0);
out:
{
return strncmp(name, "dm-", 3) ? 0 : 1;
}
+
+int dm_check_segment(const char *name, uint64_t offset, uint64_t size)
+{
+ struct crypt_dm_active_device dmd;
+ int r;
+
+ log_dbg("Checking segments for device %s.", name);
+
+ r = dm_query_device(name, 0, &dmd);
+ if (r < 0)
+ return r;
+
+ if (offset >= (dmd.offset + dmd.size) || (offset + size) <= dmd.offset)
+ r = 0;
+ else
+ r = -EBUSY;
+
+ log_dbg("seg: %" PRIu64 " - %" PRIu64 ", new %" PRIu64 " - %" PRIu64 "%s",
+ dmd.offset, dmd.offset + dmd.size, offset, offset + size,
+ r ? " (overlapping)" : " (ok)");
+
+ return r;
+}