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,
34 static int _dm_simple(int task, const char *name);
36 int dm_init(struct crypt_device *context, int check_kernel)
38 if (!_dm_use_count++) {
39 if (check_kernel && !_dm_simple(DM_DEVICE_LIST_VERSIONS, NULL))
41 dm_log_init(set_dm_error);
42 dm_log_init_verbose(10);
48 return 1; /* unsafe memory */
53 if (_dm_use_count && (!--_dm_use_count)) {
54 dm_log_init_verbose(0);
61 static char *__lookup_dev(char *path, dev_t dev)
70 path[PATH_MAX - 1] = '\0';
71 ptr = path + strlen(path);
74 space = PATH_MAX - (ptr - path);
80 while((entry = readdir(dir))) {
81 if (entry->d_name[0] == '.' &&
82 (entry->d_name[1] == '\0' || (entry->d_name[1] == '.' &&
83 entry->d_name[2] == '\0')))
86 strncpy(ptr, entry->d_name, space);
87 if (lstat(path, &st) < 0)
90 if (S_ISDIR(st.st_mode)) {
91 result = __lookup_dev(path, dev);
94 } else if (S_ISBLK(st.st_mode)) {
95 if (st.st_rdev == dev) {
96 result = strdup(path);
107 static char *lookup_dev(const char *dev)
109 uint32_t major, minor;
110 char buf[PATH_MAX + 1];
112 if (sscanf(dev, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
115 strncpy(buf, DEVICE_DIR, PATH_MAX);
116 buf[PATH_MAX] = '\0';
118 return __lookup_dev(buf, makedev(major, minor));
121 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
124 long read_ahead_long;
126 if ((fd = open(dev, O_RDONLY)) < 0)
129 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
133 *read_ahead = (uint32_t) read_ahead_long;
138 static char *get_params(const char *device, uint64_t skip, uint64_t offset,
139 const char *cipher, size_t key_size, const char *key)
145 hexkey = safe_alloc(key_size * 2 + 1);
149 for(i = 0; i < key_size; i++)
150 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
152 params = safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64);
156 sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
157 cipher, hexkey, skip, device, offset);
165 static int _dm_simple(int task, const char *name)
170 if (!(dmt = dm_task_create(task)))
173 if (name && !dm_task_set_name(dmt, name))
176 r = dm_task_run(dmt);
179 dm_task_destroy(dmt);
183 static int _error_device(const char *name, size_t size)
188 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
191 if (!dm_task_set_name(dmt, name))
194 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
197 if (!dm_task_set_ro(dmt))
200 if (!dm_task_no_open_count(dmt))
203 if (!dm_task_run(dmt))
206 if (!_dm_simple(DM_DEVICE_RESUME, name)) {
207 _dm_simple(DM_DEVICE_CLEAR, name);
214 dm_task_destroy(dmt);
218 int dm_remove_device(const char *name, int force, uint64_t size)
221 int retries = force ? RETRY_COUNT : 1;
223 if (!name || (force && !size))
226 /* If force flag is set, replace device with error, read-only target.
227 * it should stop processes from reading it and also removed underlying
228 * device from mapping, so it is usable again.
229 * Force flag should be used only for temporary devices, which are
230 * intended to work inside cryptsetup only!
231 * Anyway, if some process try to read temporary cryptsetup device,
232 * it is bug - no other process should try touch it (e.g. udev).
235 _error_device(name, size);
236 retries = RETRY_COUNT;
240 r = _dm_simple(DM_DEVICE_REMOVE, name) ? 0 : -EINVAL;
241 if (--retries && r) {
244 } while (r == -EINVAL && retries);
246 dm_task_update_nodes();
251 int dm_create_device(const char *name,
263 struct dm_task *dmt = NULL;
264 struct dm_task *dmt_query = NULL;
268 char dev_uuid[DM_UUID_PREFIX_LEN + DM_UUID_LEN + 1] = {0};
270 uint32_t read_ahead = 0;
272 params = get_params(device, skip, offset, cipher, key_size, key);
277 strncpy(dev_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN);
278 strncpy(dev_uuid + DM_UUID_PREFIX_LEN, uuid, DM_UUID_LEN);
279 dev_uuid[DM_UUID_PREFIX_LEN + DM_UUID_LEN] = '\0';
282 if (!(dmt = dm_task_create(reload ? DM_DEVICE_RELOAD
283 : DM_DEVICE_CREATE)))
285 if (!dm_task_set_name(dmt, name))
287 if (read_only && !dm_task_set_ro(dmt))
289 if (!dm_task_add_target(dmt, 0, size, DM_CRYPT_TARGET, params))
292 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
293 if (_dev_read_ahead(device, &read_ahead) &&
294 !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
298 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
301 if (!dm_task_run(dmt))
305 dm_task_destroy(dmt);
306 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
308 if (!dm_task_set_name(dmt, name))
310 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
312 if (!dm_task_run(dmt))
316 if (!dm_task_get_info(dmt, &dmi))
321 if (r < 0 && !reload) {
323 error = strdup(get_error());
325 dm_remove_device(name, 0, 0);
337 dm_task_destroy(dmt);
339 dm_task_destroy(dmt_query);
340 dm_task_update_nodes();
344 int dm_status_device(const char *name)
348 uint64_t start, length;
349 char *target_type, *params;
353 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
356 if (!dm_task_set_name(dmt, name)) {
361 if (!dm_task_run(dmt)) {
366 if (!dm_task_get_info(dmt, &dmi)) {
376 next = dm_get_next_target(dmt, next, &start, &length,
377 &target_type, ¶ms);
378 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
382 r = (dmi.open_count > 0);
385 dm_task_destroy(dmt);
390 int dm_query_device(const char *name,
402 uint64_t start, length, val64;
403 char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3];
407 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
409 if (!dm_task_set_name(dmt, name))
412 if (!dm_task_run(dmt))
416 if (!dm_task_get_info(dmt, &dmi))
424 next = dm_get_next_target(dmt, next, &start, &length,
425 &target_type, ¶ms);
426 if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
433 rcipher = strsep(¶ms, " ");
436 *cipher = strdup(rcipher);
439 key_ = strsep(¶ms, " ");
442 val64 = strtoull(params, ¶ms, 10);
450 rdevice = strsep(¶ms, " ");
452 *device = lookup_dev(rdevice);
457 val64 = strtoull(params, ¶ms, 10);
465 *key_size = strlen(key_) / 2;
468 if (key_size && key) {
469 *key = safe_alloc(*key_size);
476 for(i = 0; i < *key_size; i++) {
477 memcpy(buffer, &key_[i * 2], 2);
478 (*key)[i] = strtoul(buffer, &endp, 16);
479 if (endp != &buffer[2]) {
486 memset(key_, 0, strlen(key_));
490 *read_only = dmi.read_only;
492 r = (dmi.open_count > 0);
495 dm_task_destroy(dmt);
500 const char *dm_get_dir(void)