Switch on retry on device remove for libdevmapper.
[platform/upstream/cryptsetup.git] / lib / libdevmapper.c
1 /*
2  * libdevmapper - device-mapper backend for cryptsetup
3  *
4  * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5  * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6  * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <stdio.h>
23 #include <dirent.h>
24 #include <errno.h>
25 #include <libdevmapper.h>
26 #include <fcntl.h>
27 #include <linux/fs.h>
28 #include <uuid/uuid.h>
29
30 #include "internal.h"
31 #include "luks.h"
32
33 #define DM_UUID_LEN             129
34 #define DM_UUID_PREFIX          "CRYPT-"
35 #define DM_UUID_PREFIX_LEN      6
36 #define DM_CRYPT_TARGET         "crypt"
37 #define RETRY_COUNT             5
38
39 /* Set if dm-crypt version was probed */
40 static int _dm_crypt_checked = 0;
41 static uint32_t _dm_crypt_flags = 0;
42
43 static int _dm_use_count = 0;
44 static struct crypt_device *_context = NULL;
45
46 /* Check if we have DM flag to instruct kernel to force wipe buffers */
47 #if !HAVE_DECL_DM_TASK_SECURE_DATA
48 static int dm_task_secure_data(struct dm_task *dmt) { return 1; }
49 #endif
50
51 /* Compatibility for old device-mapper without udev support */
52 #if HAVE_DECL_DM_UDEV_DISABLE_DISK_RULES_FLAG
53 #define CRYPT_TEMP_UDEV_FLAGS   DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG | \
54                                 DM_UDEV_DISABLE_DISK_RULES_FLAG | \
55                                 DM_UDEV_DISABLE_OTHER_RULES_FLAG
56 #define _dm_task_set_cookie     dm_task_set_cookie
57 #define _dm_udev_wait           dm_udev_wait
58 #else
59 #define CRYPT_TEMP_UDEV_FLAGS   0
60 static int _dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags) { return 0; }
61 static int _dm_udev_wait(uint32_t cookie) { return 0; };
62 #endif
63
64 static int _dm_use_udev(void)
65 {
66 #ifdef USE_UDEV /* cannot be enabled if devmapper is too old */
67         return dm_udev_get_sync_support();
68 #else
69         return 0;
70 #endif
71 }
72
73 __attribute__((format(printf, 4, 5)))
74 static void set_dm_error(int level,
75                          const char *file __attribute__((unused)),
76                          int line __attribute__((unused)),
77                          const char *f, ...)
78 {
79         char *msg = NULL;
80         va_list va;
81
82         va_start(va, f);
83         if (vasprintf(&msg, f, va) > 0) {
84                 if (level < 4) {
85                         log_err(_context, msg);
86                         log_err(_context, "\n");
87                 } else
88                         log_dbg(msg);
89         }
90         free(msg);
91         va_end(va);
92 }
93
94 static int _dm_simple(int task, const char *name, int udev_wait);
95
96 static void _dm_set_crypt_compat(const char *dm_version, unsigned crypt_maj,
97                                  unsigned crypt_min, unsigned crypt_patch)
98 {
99         unsigned dm_maj, dm_min, dm_patch;
100
101         if (sscanf(dm_version, "%u.%u.%u", &dm_maj, &dm_min, &dm_patch) != 3)
102                 dm_maj = dm_min = dm_patch = 0;
103
104         log_dbg("Detected dm-crypt version %i.%i.%i, dm-ioctl version %u.%u.%u.",
105                 crypt_maj, crypt_min, crypt_patch, dm_maj, dm_min, dm_patch);
106
107         if (crypt_maj >= 1 && crypt_min >= 2)
108                 _dm_crypt_flags |= DM_KEY_WIPE_SUPPORTED;
109         else
110                 log_dbg("Suspend and resume disabled, no wipe key support.");
111
112         if (crypt_maj >= 1 && crypt_min >= 10)
113                 _dm_crypt_flags |= DM_LMK_SUPPORTED;
114
115         if (dm_maj >= 4 && dm_min >= 20)
116                 _dm_crypt_flags |= DM_SECURE_SUPPORTED;
117
118         /* not perfect, 2.6.33 supports with 1.7.0 */
119         if (crypt_maj >= 1 && crypt_min >= 8)
120                 _dm_crypt_flags |= DM_PLAIN64_SUPPORTED;
121
122         if (crypt_maj >= 1 && crypt_min >= 11)
123                 _dm_crypt_flags |= DM_DISCARDS_SUPPORTED;
124
125         /* Repeat test if dm-crypt is not present */
126         if (crypt_maj > 0)
127                 _dm_crypt_checked = 1;
128 }
129
130 static int _dm_check_versions(void)
131 {
132         struct dm_task *dmt;
133         struct dm_versions *target, *last_target;
134         char dm_version[16];
135
136         if (_dm_crypt_checked)
137                 return 1;
138
139         /* FIXME: add support to DM so it forces crypt target module load here */
140         if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
141                 return 0;
142
143         if (!dm_task_run(dmt)) {
144                 dm_task_destroy(dmt);
145                 return 0;
146         }
147
148         if (!dm_task_get_driver_version(dmt, dm_version, sizeof(dm_version))) {
149                 dm_task_destroy(dmt);
150                 return 0;
151         }
152
153         target = dm_task_get_versions(dmt);
154         do {
155                 last_target = target;
156                 if (!strcmp(DM_CRYPT_TARGET, target->name)) {
157                         _dm_set_crypt_compat(dm_version,
158                                              (unsigned)target->version[0],
159                                              (unsigned)target->version[1],
160                                              (unsigned)target->version[2]);
161                 }
162                 target = (struct dm_versions *)((char *) target + target->next);
163         } while (last_target != target);
164
165         dm_task_destroy(dmt);
166         return 1;
167 }
168
169 uint32_t dm_flags(void)
170 {
171         if (!_dm_crypt_checked)
172                 _dm_check_versions();
173
174         return _dm_crypt_flags;
175 }
176
177 int dm_init(struct crypt_device *context, int check_kernel)
178 {
179         if (!_dm_use_count++) {
180                 log_dbg("Initialising device-mapper backend%s, UDEV is %sabled.",
181                         check_kernel ? "" : " (NO kernel check requested)",
182                         _dm_use_udev() ? "en" : "dis");
183                 if (check_kernel && !_dm_check_versions()) {
184                         log_err(context, _("Cannot initialize device-mapper. Is dm_mod kernel module loaded?\n"));
185                         return -1;
186                 }
187                 if (getuid() || geteuid())
188                         log_dbg(("WARNING: Running as a non-root user. Functionality may be unavailable."));
189                 dm_log_init(set_dm_error);
190                 dm_log_init_verbose(10);
191         }
192
193         // FIXME: global context is not safe
194         if (context)
195                 _context = context;
196
197         return 1;       /* unsafe memory */
198 }
199
200 void dm_exit(void)
201 {
202         if (_dm_use_count && (!--_dm_use_count)) {
203                 log_dbg("Releasing device-mapper backend.");
204                 dm_log_init_verbose(0);
205                 dm_log_init(NULL);
206                 dm_lib_release();
207                 _context = NULL;
208         }
209 }
210
211 /* Return path to DM device */
212 char *dm_device_path(const char *prefix, int major, int minor)
213 {
214         struct dm_task *dmt;
215         const char *name;
216         char path[PATH_MAX];
217
218         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
219                 return NULL;
220         if (!dm_task_set_minor(dmt, minor) ||
221             !dm_task_set_major(dmt, major) ||
222             !dm_task_run(dmt) ||
223             !(name = dm_task_get_name(dmt))) {
224                 dm_task_destroy(dmt);
225                 return NULL;
226         }
227
228         if (snprintf(path, sizeof(path), "%s%s", prefix ?: "", name) < 0)
229                 path[0] = '\0';
230
231         dm_task_destroy(dmt);
232
233         return strdup(path);
234 }
235
236 static void hex_key(char *hexkey, size_t key_size, const char *key)
237 {
238         unsigned i;
239
240         for(i = 0; i < key_size; i++)
241                 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
242 }
243
244 static char *get_params(struct crypt_dm_active_device *dmd)
245 {
246         int r, max_size;
247         char *params, *hexkey;
248         const char *features = "";
249
250         if (dmd->flags & CRYPT_ACTIVATE_ALLOW_DISCARDS) {
251                 if (dm_flags() & DM_DISCARDS_SUPPORTED) {
252                         features = " 1 allow_discards";
253                         log_dbg("Discard/TRIM is allowed.");
254                 } else
255                         log_dbg("Discard/TRIM is not supported by the kernel.");
256         }
257
258         hexkey = crypt_safe_alloc(dmd->vk->keylength * 2 + 1);
259         if (!hexkey)
260                 return NULL;
261
262         hex_key(hexkey, dmd->vk->keylength, dmd->vk->key);
263
264         max_size = strlen(hexkey) + strlen(dmd->cipher) +
265                    strlen(dmd->device) + strlen(features) + 64;
266         params = crypt_safe_alloc(max_size);
267         if (!params)
268                 goto out;
269
270         r = snprintf(params, max_size, "%s %s %" PRIu64 " %s %" PRIu64 "%s",
271                      dmd->cipher, hexkey, dmd->iv_offset, dmd->device,
272                      dmd->offset, features);
273         if (r < 0 || r >= max_size) {
274                 crypt_safe_free(params);
275                 params = NULL;
276         }
277 out:
278         crypt_safe_free(hexkey);
279         return params;
280 }
281
282 /* DM helpers */
283 static int _dm_simple(int task, const char *name, int udev_wait)
284 {
285         int r = 0;
286         struct dm_task *dmt;
287         uint32_t cookie = 0;
288
289         if (!_dm_use_udev())
290                 udev_wait = 0;
291
292         if (!(dmt = dm_task_create(task)))
293                 return 0;
294
295         if (name && !dm_task_set_name(dmt, name))
296                 goto out;
297
298 #if HAVE_DECL_DM_TASK_RETRY_REMOVE
299         /* Used only in DM_DEVICE_REMOVE */
300         if (name && !dm_task_retry_remove(dmt))
301                 goto out;
302 #endif
303         if (udev_wait && !_dm_task_set_cookie(dmt, &cookie, 0))
304                 goto out;
305
306         r = dm_task_run(dmt);
307
308         if (udev_wait)
309                 (void)_dm_udev_wait(cookie);
310
311       out:
312         dm_task_destroy(dmt);
313         return r;
314 }
315
316 static int _error_device(const char *name, size_t size)
317 {
318         struct dm_task *dmt;
319         int r = 0;
320
321         if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
322                 return 0;
323
324         if (!dm_task_set_name(dmt, name))
325                 goto error;
326
327         if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
328                 goto error;
329
330         if (!dm_task_set_ro(dmt))
331                 goto error;
332
333         if (!dm_task_no_open_count(dmt))
334                 goto error;
335
336         if (!dm_task_run(dmt))
337                 goto error;
338
339         if (!_dm_simple(DM_DEVICE_RESUME, name, 1)) {
340                 _dm_simple(DM_DEVICE_CLEAR, name, 0);
341                 goto error;
342         }
343
344         r = 1;
345
346 error:
347         dm_task_destroy(dmt);
348         return r;
349 }
350
351 int dm_remove_device(const char *name, int force, uint64_t size)
352 {
353         int r = -EINVAL;
354         int retries = force ? RETRY_COUNT : 1;
355         int error_target = 0;
356
357         if (!name || (force && !size))
358                 return -EINVAL;
359
360         do {
361                 r = _dm_simple(DM_DEVICE_REMOVE, name, 1) ? 0 : -EINVAL;
362                 if (--retries && r) {
363                         log_dbg("WARNING: other process locked internal device %s, %s.",
364                                 name, retries ? "retrying remove" : "giving up");
365                         if (force && (crypt_get_debug_level() == CRYPT_LOG_DEBUG))
366                                 debug_processes_using_device(name);
367                         sleep(1);
368                         if (force && !error_target) {
369                                 /* If force flag is set, replace device with error, read-only target.
370                                  * it should stop processes from reading it and also removed underlying
371                                  * device from mapping, so it is usable again.
372                                  * Force flag should be used only for temporary devices, which are
373                                  * intended to work inside cryptsetup only!
374                                  * Anyway, if some process try to read temporary cryptsetup device,
375                                  * it is bug - no other process should try touch it (e.g. udev).
376                                  */
377                                 _error_device(name, size);
378                                 error_target = 1;
379                         }
380                 }
381         } while (r == -EINVAL && retries);
382
383         dm_task_update_nodes();
384
385         return r;
386 }
387
388 #define UUID_LEN 37 /* 36 + \0, libuuid ... */
389 /*
390  * UUID has format: CRYPT-<devicetype>-[<uuid>-]<device name>
391  * CRYPT-PLAIN-name
392  * CRYPT-LUKS1-00000000000000000000000000000000-name
393  * CRYPT-TEMP-name
394  */
395 static void dm_prepare_uuid(const char *name, const char *type, const char *uuid, char *buf, size_t buflen)
396 {
397         char *ptr, uuid2[UUID_LEN] = {0};
398         uuid_t uu;
399         unsigned i = 0;
400
401         /* Remove '-' chars */
402         if (uuid && !uuid_parse(uuid, uu)) {
403                 for (ptr = uuid2, i = 0; i < UUID_LEN; i++)
404                         if (uuid[i] != '-') {
405                                 *ptr = uuid[i];
406                                 ptr++;
407                         }
408         }
409
410         i = snprintf(buf, buflen, DM_UUID_PREFIX "%s%s%s%s%s",
411                 type ?: "", type ? "-" : "",
412                 uuid2[0] ? uuid2 : "", uuid2[0] ? "-" : "",
413                 name);
414
415         log_dbg("DM-UUID is %s", buf);
416         if (i >= buflen)
417                 log_err(NULL, _("DM-UUID for device %s was truncated.\n"), name);
418 }
419
420 int dm_create_device(const char *name,
421                      const char *type,
422                      struct crypt_dm_active_device *dmd,
423                      int reload)
424 {
425         struct dm_task *dmt = NULL;
426         struct dm_info dmi;
427         char *params = NULL;
428         char dev_uuid[DM_UUID_LEN] = {0};
429         int r = -EINVAL;
430         uint32_t read_ahead = 0;
431         uint32_t cookie = 0;
432         uint16_t udev_flags = 0;
433
434         params = get_params(dmd);
435         if (!params)
436                 goto out_no_removal;
437
438         if (type && !strncmp(type, "TEMP", 4))
439                 udev_flags = CRYPT_TEMP_UDEV_FLAGS;
440
441         /* All devices must have DM_UUID, only resize on old device is exception */
442         if (reload) {
443                 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
444                         goto out_no_removal;
445
446                 if (!dm_task_set_name(dmt, name))
447                         goto out_no_removal;
448         } else {
449                 dm_prepare_uuid(name, type, dmd->uuid, dev_uuid, sizeof(dev_uuid));
450
451                 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
452                         goto out_no_removal;
453
454                 if (!dm_task_set_name(dmt, name))
455                         goto out_no_removal;
456
457                 if (!dm_task_set_uuid(dmt, dev_uuid))
458                         goto out_no_removal;
459
460                 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
461                         goto out_no_removal;
462         }
463
464         if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
465                 goto out_no_removal;
466         if ((dmd->flags & CRYPT_ACTIVATE_READONLY) && !dm_task_set_ro(dmt))
467                 goto out_no_removal;
468         if (!dm_task_add_target(dmt, 0, dmd->size, DM_CRYPT_TARGET, params))
469                 goto out_no_removal;
470
471 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
472         if (device_read_ahead(dmd->device, &read_ahead) &&
473             !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
474                 goto out_no_removal;
475 #endif
476
477         if (!dm_task_run(dmt))
478                 goto out_no_removal;
479
480         if (reload) {
481                 dm_task_destroy(dmt);
482                 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
483                         goto out;
484                 if (!dm_task_set_name(dmt, name))
485                         goto out;
486                 if (dmd->uuid && !dm_task_set_uuid(dmt, dev_uuid))
487                         goto out;
488                 if (_dm_use_udev() && !_dm_task_set_cookie(dmt, &cookie, udev_flags))
489                         goto out;
490                 if (!dm_task_run(dmt))
491                         goto out;
492         }
493
494         if (!dm_task_get_info(dmt, &dmi))
495                 goto out;
496
497         r = 0;
498 out:
499         if (_dm_use_udev()) {
500                 (void)_dm_udev_wait(cookie);
501                 cookie = 0;
502         }
503
504         if (r < 0 && !reload)
505                 dm_remove_device(name, 0, 0);
506
507 out_no_removal:
508         if (cookie && _dm_use_udev())
509                 (void)_dm_udev_wait(cookie);
510
511         if (params)
512                 crypt_safe_free(params);
513         if (dmt)
514                 dm_task_destroy(dmt);
515
516         dm_task_update_nodes();
517         return r;
518 }
519
520 static int dm_status_dmi(const char *name, struct dm_info *dmi)
521 {
522         struct dm_task *dmt;
523         uint64_t start, length;
524         char *target_type, *params;
525         void *next = NULL;
526         int r = -EINVAL;
527
528         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
529                 goto out;
530
531         if (!dm_task_set_name(dmt, name))
532                 goto out;
533
534         if (!dm_task_run(dmt))
535                 goto out;
536
537         if (!dm_task_get_info(dmt, dmi))
538                 goto out;
539
540         if (!dmi->exists) {
541                 r = -ENODEV;
542                 goto out;
543         }
544
545         next = dm_get_next_target(dmt, next, &start, &length,
546                                   &target_type, &params);
547         if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
548             start != 0 || next)
549                 r = -EINVAL;
550         else
551                 r = 0;
552 out:
553         if (dmt)
554                 dm_task_destroy(dmt);
555
556         return r;
557 }
558
559 int dm_status_device(const char *name)
560 {
561         int r;
562         struct dm_info dmi;
563
564         r = dm_status_dmi(name, &dmi);
565         if (r < 0)
566                 return r;
567
568         return (dmi.open_count > 0);
569 }
570
571 int dm_status_suspended(const char *name)
572 {
573         int r;
574         struct dm_info dmi;
575
576         r = dm_status_dmi(name, &dmi);
577         if (r < 0)
578                 return r;
579
580         return dmi.suspended ? 1 : 0;
581 }
582
583 int dm_query_device(const char *name, uint32_t get_flags,
584                     struct crypt_dm_active_device *dmd)
585 {
586         struct dm_task *dmt;
587         struct dm_info dmi;
588         uint64_t start, length, val64;
589         char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *arg;
590         const char *tmp_uuid;
591         void *next = NULL;
592         unsigned int i;
593         int r = -EINVAL;
594
595         memset(dmd, 0, sizeof(*dmd));
596
597         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
598                 goto out;
599         if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
600                 goto out;
601         if (!dm_task_set_name(dmt, name))
602                 goto out;
603         r = -ENODEV;
604         if (!dm_task_run(dmt))
605                 goto out;
606
607         r = -EINVAL;
608         if (!dm_task_get_info(dmt, &dmi))
609                 goto out;
610
611         if (!dmi.exists) {
612                 r = -ENODEV;
613                 goto out;
614         }
615
616         tmp_uuid = dm_task_get_uuid(dmt);
617
618         next = dm_get_next_target(dmt, next, &start, &length,
619                                   &target_type, &params);
620         if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
621             start != 0 || next)
622                 goto out;
623
624         dmd->size = length;
625
626         rcipher = strsep(&params, " ");
627         /* cipher */
628         if (get_flags & DM_ACTIVE_CIPHER)
629                 dmd->cipher = strdup(rcipher);
630
631         /* skip */
632         key_ = strsep(&params, " ");
633         if (!params)
634                 goto out;
635         val64 = strtoull(params, &params, 10);
636         if (*params != ' ')
637                 goto out;
638         params++;
639
640         dmd->iv_offset = val64;
641
642         /* device */
643         rdevice = strsep(&params, " ");
644         if (get_flags & DM_ACTIVE_DEVICE)
645                 dmd->device = crypt_lookup_dev(rdevice);
646
647         /*offset */
648         if (!params)
649                 goto out;
650         val64 = strtoull(params, &params, 10);
651         dmd->offset = val64;
652
653         /* Features section, available since crypt target version 1.11 */
654         if (*params) {
655                 if (*params != ' ')
656                         goto out;
657                 params++;
658
659                 /* Number of arguments */
660                 val64 = strtoull(params, &params, 10);
661                 if (*params != ' ')
662                         goto out;
663                 params++;
664
665                 for (i = 0; i < val64; i++) {
666                         if (!params)
667                                 goto out;
668                         arg = strsep(&params, " ");
669                         if (!strcasecmp(arg, "allow_discards"))
670                                 dmd->flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
671                         else /* unknown option */
672                                 goto out;
673                 }
674
675                 /* All parameters shold be processed */
676                 if (params)
677                         goto out;
678         }
679
680         /* Never allow to return empty key */
681         if ((get_flags & DM_ACTIVE_KEY) && dmi.suspended) {
682                 log_dbg("Cannot read volume key while suspended.");
683                 r = -EINVAL;
684                 goto out;
685         }
686
687         if (get_flags & DM_ACTIVE_KEYSIZE) {
688                 dmd->vk = crypt_alloc_volume_key(strlen(key_) / 2, NULL);
689                 if (!dmd->vk) {
690                         r = -ENOMEM;
691                         goto out;
692                 }
693
694                 if (get_flags & DM_ACTIVE_KEY) {
695                         buffer[2] = '\0';
696                         for(i = 0; i < dmd->vk->keylength; i++) {
697                                 memcpy(buffer, &key_[i * 2], 2);
698                                 dmd->vk->key[i] = strtoul(buffer, &endp, 16);
699                                 if (endp != &buffer[2]) {
700                                         crypt_free_volume_key(dmd->vk);
701                                         dmd->vk = NULL;
702                                         r = -EINVAL;
703                                         goto out;
704                                 }
705                         }
706                 }
707         }
708         memset(key_, 0, strlen(key_));
709
710         if (dmi.read_only)
711                 dmd->flags |= CRYPT_ACTIVATE_READONLY;
712
713         if (!tmp_uuid)
714                 dmd->flags |= CRYPT_ACTIVATE_NO_UUID;
715         else if (get_flags & DM_ACTIVE_UUID) {
716                 if (!strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN))
717                         dmd->uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
718         }
719
720         r = (dmi.open_count > 0);
721 out:
722         if (dmt)
723                 dm_task_destroy(dmt);
724
725         return r;
726 }
727
728 static int _dm_message(const char *name, const char *msg)
729 {
730         int r = 0;
731         struct dm_task *dmt;
732
733         if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
734                 return 0;
735
736         if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
737                 goto out;
738
739         if (name && !dm_task_set_name(dmt, name))
740                 goto out;
741
742         if (!dm_task_set_sector(dmt, (uint64_t) 0))
743                 goto out;
744
745         if (!dm_task_set_message(dmt, msg))
746                 goto out;
747
748         r = dm_task_run(dmt);
749
750       out:
751         dm_task_destroy(dmt);
752         return r;
753 }
754
755 int dm_suspend_and_wipe_key(const char *name)
756 {
757         if (!_dm_check_versions())
758                 return -ENOTSUP;
759
760         if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
761                 return -ENOTSUP;
762
763         if (!_dm_simple(DM_DEVICE_SUSPEND, name, 0))
764                 return -EINVAL;
765
766         if (!_dm_message(name, "key wipe")) {
767                 _dm_simple(DM_DEVICE_RESUME, name, 1);
768                 return -EINVAL;
769         }
770
771         return 0;
772 }
773
774 int dm_resume_and_reinstate_key(const char *name,
775                                 size_t key_size,
776                                 const char *key)
777 {
778         int msg_size = key_size * 2 + 10; // key set <key>
779         char *msg;
780         int r = 0;
781
782         if (!_dm_check_versions())
783                 return -ENOTSUP;
784
785         if (!(_dm_crypt_flags & DM_KEY_WIPE_SUPPORTED))
786                 return -ENOTSUP;
787
788         msg = crypt_safe_alloc(msg_size);
789         if (!msg)
790                 return -ENOMEM;
791
792         memset(msg, 0, msg_size);
793         strcpy(msg, "key set ");
794         hex_key(&msg[8], key_size, key);
795
796         if (!_dm_message(name, msg) ||
797             !_dm_simple(DM_DEVICE_RESUME, name, 1))
798                 r = -EINVAL;
799
800         crypt_safe_free(msg);
801         return r;
802 }
803
804 const char *dm_get_dir(void)
805 {
806         return dm_dir();
807 }
808
809 int dm_is_dm_device(int major, int minor)
810 {
811         return dm_is_dm_major((uint32_t)major);
812 }
813
814 int dm_is_dm_kernel_name(const char *name)
815 {
816         return strncmp(name, "dm-", 3) ? 0 : 1;
817 }
818
819 int dm_check_segment(const char *name, uint64_t offset, uint64_t size)
820 {
821         struct crypt_dm_active_device dmd;
822         int r;
823
824         log_dbg("Checking segments for device %s.", name);
825
826         r = dm_query_device(name, 0, &dmd);
827         if (r < 0)
828                 return r;
829
830         if (offset >= (dmd.offset + dmd.size) || (offset + size) <= dmd.offset)
831                 r = 0;
832         else
833                 r = -EBUSY;
834
835         log_dbg("seg: %" PRIu64 " - %" PRIu64 ", new %" PRIu64 " - %" PRIu64 "%s",
836                dmd.offset, dmd.offset + dmd.size, offset, offset + size,
837                r ? " (overlapping)" : " (ok)");
838
839         return r;
840 }