4 #include <libdevmapper.h>
11 #define DEVICE_DIR "/dev"
12 #define DM_UUID_PREFIX "CRYPT-"
13 #define DM_UUID_PREFIX_LEN 6
14 #define DM_UUID_LEN UUID_STRING_L
15 #define DM_CRYPT_TARGET "crypt"
18 static int _dm_use_count = 0;
19 static struct crypt_device *_context = NULL;
21 static void set_dm_error(int level, const char *file, int line,
28 if (vasprintf(&msg, f, va) > 0) {
30 log_err(_context, msg);
31 log_err(_context, "\n");
39 static int _dm_simple(int task, const char *name);
41 int dm_init(struct crypt_device *context, int check_kernel)
43 if (!_dm_use_count++) {
44 log_dbg("Initialising device-mapper backend%s.",
45 check_kernel ? "" : " (NO kernel check requested)");
46 if (check_kernel && !_dm_simple(DM_DEVICE_LIST_VERSIONS, NULL))
48 dm_log_init(set_dm_error);
49 dm_log_init_verbose(10);
55 return 1; /* unsafe memory */
60 if (_dm_use_count && (!--_dm_use_count)) {
61 log_dbg("Releasing device-mapper backend.");
62 dm_log_init_verbose(0);
69 static char *__lookup_dev(char *path, dev_t dev)
78 path[PATH_MAX - 1] = '\0';
79 ptr = path + strlen(path);
82 space = PATH_MAX - (ptr - path);
88 while((entry = readdir(dir))) {
89 if (entry->d_name[0] == '.' &&
90 (entry->d_name[1] == '\0' || (entry->d_name[1] == '.' &&
91 entry->d_name[2] == '\0')))
94 strncpy(ptr, entry->d_name, space);
95 if (lstat(path, &st) < 0)
98 if (S_ISDIR(st.st_mode)) {
99 result = __lookup_dev(path, dev);
102 } else if (S_ISBLK(st.st_mode)) {
103 if (st.st_rdev == dev) {
104 result = strdup(path);
115 static char *lookup_dev(const char *dev)
117 uint32_t major, minor;
118 char buf[PATH_MAX + 1];
120 if (sscanf(dev, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
123 strncpy(buf, DEVICE_DIR, PATH_MAX);
124 buf[PATH_MAX] = '\0';
126 return __lookup_dev(buf, makedev(major, minor));
129 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
132 long read_ahead_long;
134 if ((fd = open(dev, O_RDONLY)) < 0)
137 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
141 *read_ahead = (uint32_t) read_ahead_long;
146 static char *get_params(const char *device, uint64_t skip, uint64_t offset,
147 const char *cipher, size_t key_size, const char *key)
153 hexkey = safe_alloc(key_size * 2 + 1);
157 for(i = 0; i < key_size; i++)
158 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
160 params = safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
164 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
165 cipher, hexkey, skip, device, offset);
173 static int _dm_simple(int task, const char *name)
178 if (!(dmt = dm_task_create(task)))
181 if (name && !dm_task_set_name(dmt, name))
184 r = dm_task_run(dmt);
187 dm_task_destroy(dmt);
191 static int _error_device(const char *name, size_t size)
196 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
199 if (!dm_task_set_name(dmt, name))
202 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
205 if (!dm_task_set_ro(dmt))
208 if (!dm_task_no_open_count(dmt))
211 if (!dm_task_run(dmt))
214 if (!_dm_simple(DM_DEVICE_RESUME, name)) {
215 _dm_simple(DM_DEVICE_CLEAR, name);
222 dm_task_destroy(dmt);
226 int dm_remove_device(const char *name, int force, uint64_t size)
229 int retries = force ? RETRY_COUNT : 1;
231 if (!name || (force && !size))
234 /* If force flag is set, replace device with error, read-only target.
235 * it should stop processes from reading it and also removed underlying
236 * device from mapping, so it is usable again.
237 * Force flag should be used only for temporary devices, which are
238 * intended to work inside cryptsetup only!
239 * Anyway, if some process try to read temporary cryptsetup device,
240 * it is bug - no other process should try touch it (e.g. udev).
243 _error_device(name, size);
244 retries = RETRY_COUNT;
248 r = _dm_simple(DM_DEVICE_REMOVE, name) ? 0 : -EINVAL;
249 if (--retries && r) {
250 log_dbg("WARNING: other process locked internal device %s, %s.",
251 name, retries ? "retrying remove" : "giving up");
254 } while (r == -EINVAL && retries);
256 dm_task_update_nodes();
261 int dm_create_device(const char *name,
273 struct dm_task *dmt = NULL;
274 struct dm_task *dmt_query = NULL;
278 char dev_uuid[DM_UUID_PREFIX_LEN + DM_UUID_LEN + 1] = {0};
280 uint32_t read_ahead = 0;
282 params = get_params(device, skip, offset, cipher, key_size, key);
287 strncpy(dev_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN);
288 strncpy(dev_uuid + DM_UUID_PREFIX_LEN, uuid, DM_UUID_LEN);
289 dev_uuid[DM_UUID_PREFIX_LEN + DM_UUID_LEN] = '\0';
292 if (!(dmt = dm_task_create(reload ? DM_DEVICE_RELOAD
293 : DM_DEVICE_CREATE)))
295 if (!dm_task_set_name(dmt, name))
297 if (read_only && !dm_task_set_ro(dmt))
299 if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
302 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
303 if (_dev_read_ahead(device, &read_ahead) &&
304 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
308 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
311 if (!dm_task_run(dmt))
315 dm_task_destroy(dmt);
316 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
318 if (!dm_task_set_name(dmt, name))
320 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
322 if (!dm_task_run(dmt))
326 if (!dm_task_get_info(dmt, &dmi))
331 if (r < 0 && !reload) {
333 error = strdup(get_error());
335 dm_remove_device(name, 0, 0);
347 dm_task_destroy(dmt);
349 dm_task_destroy(dmt_query);
350 dm_task_update_nodes();
354 int dm_status_device(const char *name)
358 uint64_t start, length;
359 char *target_type, *params;
363 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
366 if (!dm_task_set_name(dmt, name)) {
371 if (!dm_task_run(dmt)) {
376 if (!dm_task_get_info(dmt, &dmi)) {
386 next = dm_get_next_target(dmt, next, &start, &length,
387 &target_type, ¶ms);
388 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
392 r = (dmi.open_count > 0);
395 dm_task_destroy(dmt);
400 int dm_query_device(const char *name,
412 uint64_t start, length, val64;
413 char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3];
417 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
419 if (!dm_task_set_name(dmt, name))
422 if (!dm_task_run(dmt))
426 if (!dm_task_get_info(dmt, &dmi))
434 next = dm_get_next_target(dmt, next, &start, &length,
435 &target_type, ¶ms);
436 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
443 rcipher = strsep(¶ms, " ");
446 *cipher = strdup(rcipher);
449 key_ = strsep(¶ms, " ");
452 val64 = strtoull(params, ¶ms, 10);
460 rdevice = strsep(¶ms, " ");
462 *device = lookup_dev(rdevice);
467 val64 = strtoull(params, ¶ms, 10);
475 *key_size = strlen(key_) / 2;
478 if (key_size && key) {
479 *key = safe_alloc(*key_size);
486 for(i = 0; i < *key_size; i++) {
487 memcpy(buffer, &key_[i * 2], 2);
488 (*key)[i] = strtoul(buffer, &endp, 16);
489 if (endp != &buffer[2]) {
496 memset(key_, 0, strlen(key_));
500 *read_only = dmi.read_only;
502 r = (dmi.open_count > 0);
505 dm_task_destroy(dmt);
510 const char *dm_get_dir(void)