8 #include <sys/sysmacros.h>
13 #include <libdevmapper.h>
17 #include "libcryptsetup.h"
20 #define DEVICE_DIR "/dev"
22 #define CRYPT_TARGET "crypt"
25 static void set_dm_error(int level, const char *file, int line,
38 static int _dm_simple(int task, const char *name);
40 static int dm_init(void)
42 dm_log_init(set_dm_error);
43 if (!_dm_simple(DM_DEVICE_LIST_VERSIONS, "test")) {
44 set_error("Cannot communicate with device-mapper. Is the dm_mod module loaded?");
48 return 1; /* unsafe memory */
51 static void dm_exit(void)
57 static char *__lookup_dev(char *path, dev_t dev)
66 path[PATH_MAX - 1] = '\0';
67 ptr = path + strlen(path);
70 space = PATH_MAX - (ptr - path);
76 while((entry = readdir(dir))) {
77 if (entry->d_name[0] == '.' &&
78 (entry->d_name[1] == '\0' || (entry->d_name[1] == '.' &&
79 entry->d_name[2] == '\0')))
82 strncpy(ptr, entry->d_name, space);
83 if (lstat(path, &st) < 0)
86 if (S_ISDIR(st.st_mode)) {
87 result = __lookup_dev(path, dev);
90 } else if (S_ISBLK(st.st_mode)) {
91 if (st.st_rdev == dev) {
92 result = strdup(path);
103 static char *lookup_dev(const char *dev)
105 uint32_t major, minor;
106 char buf[PATH_MAX + 1];
108 if (sscanf(dev, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
111 strncpy(buf, DEVICE_DIR, PATH_MAX);
112 buf[PATH_MAX] = '\0';
114 return __lookup_dev(buf, makedev(major, minor));
117 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
120 long read_ahead_long;
122 if ((fd = open(dev, O_RDONLY)) < 0)
125 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
129 *read_ahead = (uint32_t) read_ahead_long;
134 static char *get_params(struct crypt_options *options, const char *key)
140 hexkey = safe_alloc(options->key_size * 2 + 1);
142 set_error("Memory allocation problem");
146 for(i = 0; i < options->key_size; i++)
147 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
149 params = safe_alloc(strlen(hexkey) + strlen(options->cipher) +
150 strlen(options->device) + 64);
152 set_error("Memory allocation problem");
156 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
157 options->cipher, hexkey, options->skip,
158 options->device, options->offset);
167 static int _dm_simple(int task, const char *name)
172 if (!(dmt = dm_task_create(task)))
175 if (!dm_task_set_name(dmt, name))
178 r = dm_task_run(dmt);
181 dm_task_destroy(dmt);
185 static int _error_device(struct crypt_options *options)
190 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
193 if (!dm_task_set_name(dmt, options->name))
196 if (!dm_task_add_target(dmt, UINT64_C(0), options->size, "error", ""))
199 if (!dm_task_set_ro(dmt))
202 if (!dm_task_no_open_count(dmt))
205 if (!dm_task_run(dmt))
208 if (!_dm_simple(DM_DEVICE_RESUME, options->name)) {
209 _dm_simple(DM_DEVICE_CLEAR, options->name);
216 dm_task_destroy(dmt);
220 static int _dm_remove(struct crypt_options *options, int force)
223 int retries = force ? RETRY_COUNT : 1;
225 /* If force flag is set, replace device with error, read-only target.
226 * it should stop processes from reading it and also removed underlying
227 * device from mapping, so it is usable again.
228 * Force flag should be used only for temporary devices, which are
229 * intended to work inside cryptsetup only!
230 * Anyway, if some process try to read temporary cryptsetup device,
231 * it is bug - no other process should try touch it (e.g. udev).
234 _error_device(options);
235 retries = RETRY_COUNT;
239 r = _dm_simple(DM_DEVICE_REMOVE, options->name) ? 0 : -EINVAL;
242 } while (r == -EINVAL && retries);
244 dm_task_update_nodes();
249 static int dm_create_device(int reload, struct crypt_options *options,
252 struct dm_task *dmt = NULL;
253 struct dm_task *dmt_query = NULL;
258 uint32_t read_ahead = 0;
260 params = get_params(options, key);
263 if (!(dmt = dm_task_create(reload ? DM_DEVICE_RELOAD
264 : DM_DEVICE_CREATE)))
266 if (!dm_task_set_name(dmt, options->name))
268 if (options->flags & CRYPT_FLAG_READONLY && !dm_task_set_ro(dmt))
270 if (!dm_task_add_target(dmt, 0, options->size, CRYPT_TARGET, params))
273 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
274 if (_dev_read_ahead(options->device, &read_ahead) &&
275 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
278 if (!dm_task_run(dmt))
282 dm_task_destroy(dmt);
283 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
285 if (!dm_task_set_name(dmt, options->name))
287 if (!dm_task_run(dmt))
291 if (!dm_task_get_info(dmt, &dmi))
294 options->flags |= CRYPT_FLAG_READONLY;
298 if (r < 0 && !reload) {
300 error = strdup(get_error());
302 _dm_remove(options, 0);
314 dm_task_destroy(dmt);
316 dm_task_destroy(dmt_query);
317 dm_task_update_nodes();
321 static int dm_query_device(int details, struct crypt_options *options,
326 uint64_t start, length;
327 char *target_type, *params;
331 if (!(dmt = dm_task_create(details ? DM_DEVICE_TABLE
332 : DM_DEVICE_STATUS)))
334 if (!dm_task_set_name(dmt, options->name))
337 if (!dm_task_run(dmt))
341 if (!dm_task_get_info(dmt, &dmi))
349 next = dm_get_next_target(dmt, next, &start, &length,
350 &target_type, ¶ms);
351 if (!target_type || strcmp(target_type, CRYPT_TARGET) != 0 ||
355 options->hash = NULL;
356 options->cipher = NULL;
359 options->size = length;
361 char *cipher, *key_, *device;
364 set_error("Invalid dm table");
366 cipher = strsep(¶ms, " ");
367 key_ = strsep(¶ms, " ");
371 val64 = strtoull(params, ¶ms, 10);
375 options->skip = val64;
377 device = strsep(¶ms, " ");
381 val64 = strtoull(params, ¶ms, 10);
384 options->offset = val64;
386 options->cipher = strdup(cipher);
387 options->key_size = strlen(key_) / 2;
393 *key = safe_alloc(options->key_size);
395 set_error("Out of memory");
401 for(i = 0; i < options->key_size; i++) {
402 memcpy(buffer, &key_[i * 2], 2);
403 (*key)[i] = strtoul(buffer, &endp, 16);
404 if (endp != &buffer[2]) {
411 memset(key_, 0, strlen(key_));
412 options->device = lookup_dev(device);
417 r = (dmi.open_count > 0);
421 dm_task_destroy(dmt);
424 options->flags |= CRYPT_FLAG_FREE_DEVICE;
426 options->flags |= CRYPT_FLAG_FREE_CIPHER;
427 options->flags &= ~CRYPT_FLAG_READONLY;
429 options->flags |= CRYPT_FLAG_READONLY;
431 if (options->device) {
432 free((char *)options->device);
433 options->device = NULL;
434 options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
436 if (options->cipher) {
437 free((char *)options->cipher);
438 options->cipher = NULL;
439 options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
445 static int dm_remove_device(int force, struct crypt_options *options)
447 if (!options || !options->name)
450 return _dm_remove(options, force);;
454 static const char *dm_get_dir(void)
459 struct setup_backend setup_libdevmapper_backend = {
463 .create = dm_create_device,
464 .status = dm_query_device,
465 .remove = dm_remove_device,