libmultipath: add size argument to dm_get_uuid()
[multipath-tools/.git] / libmultipath / devmapper.c
1 /*
2  * snippets copied from device-mapper dmsetup.c
3  * Copyright (c) 2004, 2005 Christophe Varoqui
4  * Copyright (c) 2005 Kiyoshi Ueda, NEC
5  * Copyright (c) 2005 Patrick Caulfield, Redhat
6  */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <stdarg.h>
10 #include <string.h>
11 #include <libdevmapper.h>
12 #include <ctype.h>
13 #include <unistd.h>
14 #include <errno.h>
15 #include <sys/sysmacros.h>
16 #include <linux/dm-ioctl.h>
17
18 #include "util.h"
19 #include "checkers.h"
20 #include "vector.h"
21 #include "structs.h"
22 #include "debug.h"
23 #include "memory.h"
24 #include "devmapper.h"
25 #include "sysfs.h"
26 #include "config.h"
27 #include "wwids.h"
28
29 #include "log_pthread.h"
30 #include <sys/types.h>
31 #include <time.h>
32
33 #define MAX_WAIT 5
34 #define LOOPS_PER_SEC 5
35
36 static int dm_conf_verbosity;
37
38 #ifdef LIBDM_API_DEFERRED
39 static int dm_cancel_remove_partmaps(const char * mapname);
40 #endif
41
42 static int do_foreach_partmaps(const char * mapname,
43                                int (*partmap_func)(const char *, void *),
44                                void *data);
45
46 #ifndef LIBDM_API_COOKIE
47 static inline int dm_task_set_cookie(struct dm_task *dmt, uint32_t *c, int a)
48 {
49         return 1;
50 }
51
52 void dm_udev_wait(unsigned int c)
53 {
54 }
55
56 void dm_udev_set_sync_support(int c)
57 {
58 }
59
60 #endif
61
62 static void
63 dm_write_log (int level, const char *file, int line, const char *f, ...)
64 {
65         va_list ap;
66         int thres;
67
68         if (level > 6)
69                 level = 6;
70
71         thres = dm_conf_verbosity;
72         if (thres <= 3 || level > thres)
73                 return;
74
75         va_start(ap, f);
76         if (logsink < 1) {
77                 if (logsink == 0) {
78                         time_t t = time(NULL);
79                         struct tm *tb = localtime(&t);
80                         char buff[16];
81
82                         strftime(buff, sizeof(buff), "%b %d %H:%M:%S", tb);
83                         buff[sizeof(buff)-1] = '\0';
84
85                         fprintf(stderr, "%s | ", buff);
86                 }
87                 fprintf(stderr, "libdevmapper: %s(%i): ", file, line);
88                 vfprintf(stderr, f, ap);
89                 fprintf(stderr, "\n");
90         } else {
91                 condlog(level, "libdevmapper: %s(%i): ", file, line);
92                 log_safe(level + 3, f, ap);
93         }
94         va_end(ap);
95
96         return;
97 }
98
99 void dm_init(int v)
100 {
101         dm_conf_verbosity = v;
102         dm_log_init(&dm_write_log);
103         dm_log_init_verbose(v + 3);
104 }
105
106 static int
107 dm_lib_prereq (void)
108 {
109         char version[64];
110         int v[3];
111 #if defined(LIBDM_API_DEFERRED)
112         int minv[3] = {1, 2, 89};
113 #elif defined(DM_SUBSYSTEM_UDEV_FLAG0)
114         int minv[3] = {1, 2, 82};
115 #elif defined(LIBDM_API_COOKIE)
116         int minv[3] = {1, 2, 38};
117 #else
118         int minv[3] = {1, 2, 8};
119 #endif
120
121         dm_get_library_version(version, sizeof(version));
122         condlog(3, "libdevmapper version %s", version);
123         if (sscanf(version, "%d.%d.%d ", &v[0], &v[1], &v[2]) != 3) {
124                 condlog(0, "invalid libdevmapper version %s", version);
125                 return 1;
126         }
127
128         if VERSION_GE(v, minv)
129                 return 0;
130         condlog(0, "libdevmapper version must be >= %d.%.2d.%.2d",
131                 minv[0], minv[1], minv[2]);
132         return 1;
133 }
134
135 int
136 dm_drv_version(unsigned int *v)
137 {
138         char buff[64];
139
140         v[0] = 0;
141         v[1] = 0;
142         v[2] = 0;
143
144         if (!dm_driver_version(buff, sizeof(buff))) {
145                 condlog(0, "cannot get kernel dm version");
146                 return 1;
147         }
148         if (sscanf(buff, "%u.%u.%u ", &v[0], &v[1], &v[2]) != 3) {
149                 condlog(0, "invalid kernel dm version '%s'", buff);
150                 return 1;
151         }
152         return 0;
153 }
154
155 int
156 dm_tgt_version (unsigned int * version, char * str)
157 {
158         int r = 2;
159         struct dm_task *dmt;
160         struct dm_versions *target;
161         struct dm_versions *last_target;
162         unsigned int *v;
163
164         version[0] = 0;
165         version[1] = 0;
166         version[2] = 0;
167
168         if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
169                 return 1;
170
171         dm_task_no_open_count(dmt);
172
173         if (!dm_task_run(dmt)) {
174                 condlog(0, "Can not communicate with kernel DM");
175                 goto out;
176         }
177         target = dm_task_get_versions(dmt);
178
179         do {
180                 last_target = target;
181                 if (!strncmp(str, target->name, strlen(str))) {
182                         r = 1;
183                         break;
184                 }
185                 target = (void *) target + target->next;
186         } while (last_target != target);
187
188         if (r == 2) {
189                 condlog(0, "DM %s kernel driver not loaded", str);
190                 goto out;
191         }
192         v = target->version;
193         version[0] = v[0];
194         version[1] = v[1];
195         version[2] = v[2];
196         r = 0;
197 out:
198         dm_task_destroy(dmt);
199         return r;
200 }
201
202 static int
203 dm_tgt_prereq (unsigned int *ver)
204 {
205         unsigned int minv[3] = {1, 0, 3};
206         unsigned int version[3] = {0, 0, 0};
207         unsigned int * v = version;
208
209         if (dm_tgt_version(v, TGT_MPATH)) {
210                 /* in doubt return not capable */
211                 return 1;
212         }
213
214         /* test request based multipath capability */
215         condlog(3, "DM multipath kernel driver v%u.%u.%u",
216                 v[0], v[1], v[2]);
217
218         if (VERSION_GE(v, minv)) {
219                 ver[0] = v[0];
220                 ver[1] = v[1];
221                 ver[2] = v[2];
222                 return 0;
223         }
224
225         condlog(0, "DM multipath kernel driver must be >= v%u.%u.%u",
226                 minv[0], minv[1], minv[2]);
227         return 1;
228 }
229
230 static int dm_prereq(unsigned int *v)
231 {
232         if (dm_lib_prereq())
233                 return 1;
234         return dm_tgt_prereq(v);
235 }
236
237 static int libmp_dm_udev_sync = 0;
238
239 void libmp_udev_set_sync_support(int on)
240 {
241         libmp_dm_udev_sync = !!on;
242 }
243
244 void libmp_dm_init(void)
245 {
246         struct config *conf;
247         int verbosity;
248         unsigned int version[3];
249
250         if (dm_prereq(version))
251                 exit(1);
252         conf = get_multipath_config();
253         verbosity = conf->verbosity;
254         memcpy(conf->version, version, sizeof(version));
255         put_multipath_config(conf);
256         dm_init(verbosity);
257         dm_udev_set_sync_support(libmp_dm_udev_sync);
258 }
259
260 struct dm_task*
261 libmp_dm_task_create(int task)
262 {
263         static pthread_once_t dm_initialized = PTHREAD_ONCE_INIT;
264
265         pthread_once(&dm_initialized, libmp_dm_init);
266         return dm_task_create(task);
267 }
268
269 #define do_deferred(x) ((x) == DEFERRED_REMOVE_ON || (x) == DEFERRED_REMOVE_IN_PROGRESS)
270
271 static int
272 dm_simplecmd (int task, const char *name, int no_flush, int need_sync, uint16_t udev_flags, int deferred_remove) {
273         int r = 0;
274         int udev_wait_flag = ((need_sync || udev_flags) &&
275                               (task == DM_DEVICE_RESUME ||
276                                task == DM_DEVICE_REMOVE));
277         uint32_t cookie = 0;
278         struct dm_task *dmt;
279
280         if (!(dmt = libmp_dm_task_create (task)))
281                 return 0;
282
283         if (!dm_task_set_name (dmt, name))
284                 goto out;
285
286         dm_task_no_open_count(dmt);
287         dm_task_skip_lockfs(dmt);       /* for DM_DEVICE_RESUME */
288 #ifdef LIBDM_API_FLUSH
289         if (no_flush)
290                 dm_task_no_flush(dmt);          /* for DM_DEVICE_SUSPEND/RESUME */
291 #endif
292 #ifdef LIBDM_API_DEFERRED
293         if (do_deferred(deferred_remove))
294                 dm_task_deferred_remove(dmt);
295 #endif
296         if (udev_wait_flag &&
297             !dm_task_set_cookie(dmt, &cookie,
298                                 DM_UDEV_DISABLE_LIBRARY_FALLBACK | udev_flags))
299                 goto out;
300
301         r = dm_task_run (dmt);
302
303         if (udev_wait_flag)
304                         dm_udev_wait(cookie);
305 out:
306         dm_task_destroy (dmt);
307         return r;
308 }
309
310 int dm_simplecmd_flush (int task, const char *name, uint16_t udev_flags)
311 {
312         return dm_simplecmd(task, name, 0, 1, udev_flags, 0);
313 }
314
315 int dm_simplecmd_noflush (int task, const char *name, uint16_t udev_flags)
316 {
317         return dm_simplecmd(task, name, 1, 1, udev_flags, 0);
318 }
319
320 static int
321 dm_device_remove (const char *name, int needsync, int deferred_remove) {
322         return dm_simplecmd(DM_DEVICE_REMOVE, name, 0, needsync, 0,
323                             deferred_remove);
324 }
325
326 static int
327 dm_addmap (int task, const char *target, struct multipath *mpp,
328            char * params, int ro, uint16_t udev_flags) {
329         int r = 0;
330         struct dm_task *dmt;
331         char *prefixed_uuid = NULL;
332         uint32_t cookie = 0;
333
334         /* Need to add this here to allow 0 to be passed in udev_flags */
335         udev_flags |= DM_UDEV_DISABLE_LIBRARY_FALLBACK;
336
337         if (!(dmt = libmp_dm_task_create (task)))
338                 return 0;
339
340         if (!dm_task_set_name (dmt, mpp->alias))
341                 goto addout;
342
343         if (!dm_task_add_target (dmt, 0, mpp->size, target, params))
344                 goto addout;
345
346         if (ro)
347                 dm_task_set_ro(dmt);
348
349         if (task == DM_DEVICE_CREATE) {
350                 if (strlen(mpp->wwid) > 0) {
351                         prefixed_uuid = MALLOC(UUID_PREFIX_LEN +
352                                                strlen(mpp->wwid) + 1);
353                         if (!prefixed_uuid) {
354                                 condlog(0, "cannot create prefixed uuid : %s",
355                                         strerror(errno));
356                                 goto addout;
357                         }
358                         sprintf(prefixed_uuid, UUID_PREFIX "%s", mpp->wwid);
359                         if (!dm_task_set_uuid(dmt, prefixed_uuid))
360                                 goto freeout;
361                 }
362                 dm_task_skip_lockfs(dmt);
363 #ifdef LIBDM_API_FLUSH
364                 dm_task_no_flush(dmt);
365 #endif
366         }
367
368         if (mpp->attribute_flags & (1 << ATTR_MODE) &&
369             !dm_task_set_mode(dmt, mpp->mode))
370                 goto freeout;
371         if (mpp->attribute_flags & (1 << ATTR_UID) &&
372             !dm_task_set_uid(dmt, mpp->uid))
373                 goto freeout;
374         if (mpp->attribute_flags & (1 << ATTR_GID) &&
375             !dm_task_set_gid(dmt, mpp->gid))
376                 goto freeout;
377         condlog(4, "%s: %s [0 %llu %s %s]", mpp->alias,
378                 task == DM_DEVICE_RELOAD ? "reload" : "addmap", mpp->size,
379                 target, params);
380
381         dm_task_no_open_count(dmt);
382
383         if (task == DM_DEVICE_CREATE &&
384             !dm_task_set_cookie(dmt, &cookie, udev_flags))
385                 goto freeout;
386
387         r = dm_task_run (dmt);
388
389         if (task == DM_DEVICE_CREATE)
390                         dm_udev_wait(cookie);
391 freeout:
392         if (prefixed_uuid)
393                 FREE(prefixed_uuid);
394
395 addout:
396         dm_task_destroy (dmt);
397
398         return r;
399 }
400
401 static uint16_t build_udev_flags(const struct multipath *mpp, int reload)
402 {
403         /* DM_UDEV_DISABLE_LIBRARY_FALLBACK is added in dm_addmap */
404         return  (mpp->skip_kpartx == SKIP_KPARTX_ON ?
405                  MPATH_UDEV_NO_KPARTX_FLAG : 0) |
406                 ((mpp->nr_active == 0 || mpp->ghost_delay_tick > 0)?
407                  MPATH_UDEV_NO_PATHS_FLAG : 0) |
408                 (reload && !mpp->force_udev_reload ?
409                  MPATH_UDEV_RELOAD_FLAG : 0);
410 }
411
412 int dm_addmap_create (struct multipath *mpp, char * params)
413 {
414         int ro;
415         uint16_t udev_flags = build_udev_flags(mpp, 0);
416
417         for (ro = 0; ro <= 1; ro++) {
418                 int err;
419
420                 if (dm_addmap(DM_DEVICE_CREATE, TGT_MPATH, mpp, params, ro,
421                               udev_flags)) {
422                         if (unmark_failed_wwid(mpp->wwid) ==
423                             WWID_FAILED_CHANGED)
424                                 mpp->needs_paths_uevent = 1;
425                         return 1;
426                 }
427                 /*
428                  * DM_DEVICE_CREATE is actually DM_DEV_CREATE + DM_TABLE_LOAD.
429                  * Failing the second part leaves an empty map. Clean it up.
430                  */
431                 err = errno;
432                 if (dm_map_present(mpp->alias)) {
433                         condlog(3, "%s: failed to load map (a path might be in use)", mpp->alias);
434                         dm_flush_map_nosync(mpp->alias);
435                 }
436                 if (err != EROFS) {
437                         condlog(3, "%s: failed to load map, error %d",
438                                 mpp->alias, err);
439                         break;
440                 }
441         }
442         if (mark_failed_wwid(mpp->wwid) == WWID_FAILED_CHANGED)
443                 mpp->needs_paths_uevent = 1;
444         return 0;
445 }
446
447 #define ADDMAP_RW 0
448 #define ADDMAP_RO 1
449
450 int dm_addmap_reload(struct multipath *mpp, char *params, int flush)
451 {
452         int r = 0;
453         uint16_t udev_flags = build_udev_flags(mpp, 1);
454
455         /*
456          * DM_DEVICE_RELOAD cannot wait on a cookie, as
457          * the cookie will only ever be released after an
458          * DM_DEVICE_RESUME. So call DM_DEVICE_RESUME
459          * after each successful call to DM_DEVICE_RELOAD.
460          */
461         if (!mpp->force_readonly)
462                 r = dm_addmap(DM_DEVICE_RELOAD, TGT_MPATH, mpp, params,
463                               ADDMAP_RW, 0);
464         if (!r) {
465                 if (!mpp->force_readonly && errno != EROFS)
466                         return 0;
467                 r = dm_addmap(DM_DEVICE_RELOAD, TGT_MPATH, mpp,
468                               params, ADDMAP_RO, 0);
469         }
470         if (r)
471                 r = dm_simplecmd(DM_DEVICE_RESUME, mpp->alias, !flush,
472                                  1, udev_flags, 0);
473         if (r)
474                 return r;
475
476         /* If the resume failed, dm will leave the device suspended, and
477          * drop the new table, so doing a second resume will try using
478          * the original table */
479         if (dm_is_suspended(mpp->alias))
480                 dm_simplecmd(DM_DEVICE_RESUME, mpp->alias, !flush, 1,
481                              udev_flags, 0);
482         return 0;
483 }
484
485 static int
486 do_get_info(const char *name, struct dm_info *info)
487 {
488         int r = -1;
489         struct dm_task *dmt;
490
491         if (!(dmt = libmp_dm_task_create(DM_DEVICE_INFO)))
492                 return r;
493
494         if (!dm_task_set_name(dmt, name))
495                 goto out;
496
497         dm_task_no_open_count(dmt);
498
499         if (!dm_task_run(dmt))
500                 goto out;
501
502         if (!dm_task_get_info(dmt, info))
503                 goto out;
504
505         if (!info->exists)
506                 goto out;
507
508         r = 0;
509 out:
510         dm_task_destroy(dmt);
511         return r;
512 }
513
514 int dm_map_present(const char * str)
515 {
516         struct dm_info info;
517
518         return (do_get_info(str, &info) == 0);
519 }
520
521 int dm_get_map(const char *name, unsigned long long *size, char *outparams)
522 {
523         int r = 1;
524         struct dm_task *dmt;
525         uint64_t start, length;
526         char *target_type = NULL;
527         char *params = NULL;
528
529         if (!(dmt = libmp_dm_task_create(DM_DEVICE_TABLE)))
530                 return 1;
531
532         if (!dm_task_set_name(dmt, name))
533                 goto out;
534
535         dm_task_no_open_count(dmt);
536
537         if (!dm_task_run(dmt))
538                 goto out;
539
540         /* Fetch 1st target */
541         dm_get_next_target(dmt, NULL, &start, &length,
542                            &target_type, &params);
543
544         if (size)
545                 *size = length;
546
547         if (!outparams) {
548                 r = 0;
549                 goto out;
550         }
551         if (snprintf(outparams, PARAMS_SIZE, "%s", params) <= PARAMS_SIZE)
552                 r = 0;
553 out:
554         dm_task_destroy(dmt);
555         return r;
556 }
557
558 static int
559 dm_get_prefixed_uuid(const char *name, char *uuid, int uuid_len)
560 {
561         struct dm_task *dmt;
562         const char *uuidtmp;
563         int r = 1;
564
565         dmt = libmp_dm_task_create(DM_DEVICE_INFO);
566         if (!dmt)
567                 return 1;
568
569         if (!dm_task_set_name (dmt, name))
570                 goto uuidout;
571
572         if (!dm_task_run(dmt))
573                 goto uuidout;
574
575         uuidtmp = dm_task_get_uuid(dmt);
576         if (uuidtmp)
577                 strlcpy(uuid, uuidtmp, uuid_len);
578         else
579                 uuid[0] = '\0';
580
581         r = 0;
582 uuidout:
583         dm_task_destroy(dmt);
584         return r;
585 }
586
587 int dm_get_uuid(const char *name, char *uuid, int uuid_len)
588 {
589         char tmp[DM_UUID_LEN];
590
591         if (dm_get_prefixed_uuid(name, tmp, sizeof(tmp)))
592                 return 1;
593
594         if (!strncmp(tmp, UUID_PREFIX, UUID_PREFIX_LEN))
595                 strlcpy(uuid, tmp + UUID_PREFIX_LEN, uuid_len);
596         else
597                 uuid[0] = '\0';
598
599         return 0;
600 }
601
602 static int
603 is_mpath_part(const char *part_name, const char *map_name)
604 {
605         char *p;
606         char part_uuid[DM_UUID_LEN], map_uuid[DM_UUID_LEN];
607
608         if (dm_get_prefixed_uuid(part_name, part_uuid, sizeof(part_uuid)))
609                 return 0;
610
611         if (dm_get_prefixed_uuid(map_name, map_uuid, sizeof(map_uuid)))
612                 return 0;
613
614         if (strncmp(part_uuid, "part", 4) != 0)
615                 return 0;
616
617         p = strstr(part_uuid, UUID_PREFIX);
618         if (p && !strcmp(p, map_uuid))
619                 return 1;
620
621         return 0;
622 }
623
624 int dm_get_status(const char *name, char *outstatus)
625 {
626         int r = 1;
627         struct dm_task *dmt;
628         uint64_t start, length;
629         char *target_type = NULL;
630         char *status = NULL;
631
632         if (!(dmt = libmp_dm_task_create(DM_DEVICE_STATUS)))
633                 return 1;
634
635         if (!dm_task_set_name(dmt, name))
636                 goto out;
637
638         dm_task_no_open_count(dmt);
639
640         if (!dm_task_run(dmt))
641                 goto out;
642
643         /* Fetch 1st target */
644         dm_get_next_target(dmt, NULL, &start, &length,
645                            &target_type, &status);
646         if (!status) {
647                 condlog(2, "get null status.");
648                 goto out;
649         }
650
651         if (snprintf(outstatus, PARAMS_SIZE, "%s", status) <= PARAMS_SIZE)
652                 r = 0;
653 out:
654         if (r)
655                 condlog(0, "%s: error getting map status string", name);
656
657         dm_task_destroy(dmt);
658         return r;
659 }
660
661 /*
662  * returns:
663  *    1 : match
664  *    0 : no match
665  *   -1 : empty map, or more than 1 target
666  */
667 int dm_type(const char *name, char *type)
668 {
669         int r = 0;
670         struct dm_task *dmt;
671         uint64_t start, length;
672         char *target_type = NULL;
673         char *params;
674
675         if (!(dmt = libmp_dm_task_create(DM_DEVICE_TABLE)))
676                 return 0;
677
678         if (!dm_task_set_name(dmt, name))
679                 goto out;
680
681         dm_task_no_open_count(dmt);
682
683         if (!dm_task_run(dmt))
684                 goto out;
685
686         /* Fetch 1st target */
687         if (dm_get_next_target(dmt, NULL, &start, &length,
688                                &target_type, &params) != NULL)
689                 /* multiple targets */
690                 r = -1;
691         else if (!target_type)
692                 r = -1;
693         else if (!strcmp(target_type, type))
694                 r = 1;
695
696 out:
697         dm_task_destroy(dmt);
698         return r;
699 }
700
701 /*
702  * returns:
703  * 1  : is multipath device
704  * 0  : is not multipath device
705  * -1 : error
706  */
707 int dm_is_mpath(const char *name)
708 {
709         int r = -1;
710         struct dm_task *dmt;
711         struct dm_info info;
712         uint64_t start, length;
713         char *target_type = NULL;
714         char *params;
715         const char *uuid;
716
717         if (!(dmt = libmp_dm_task_create(DM_DEVICE_TABLE)))
718                 goto out;
719
720         if (!dm_task_set_name(dmt, name))
721                 goto out_task;
722
723         dm_task_no_open_count(dmt);
724
725         if (!dm_task_run(dmt))
726                 goto out_task;
727
728         if (!dm_task_get_info(dmt, &info))
729                 goto out_task;
730
731         r = 0;
732
733         if (!info.exists)
734                 goto out_task;
735
736         uuid = dm_task_get_uuid(dmt);
737
738         if (!uuid || strncmp(uuid, UUID_PREFIX, UUID_PREFIX_LEN) != 0)
739                 goto out_task;
740
741         /* Fetch 1st target */
742         if (dm_get_next_target(dmt, NULL, &start, &length, &target_type,
743                                &params) != NULL)
744                 /* multiple targets */
745                 goto out_task;
746
747         if (!target_type || strcmp(target_type, TGT_MPATH) != 0)
748                 goto out_task;
749
750         r = 1;
751 out_task:
752         dm_task_destroy(dmt);
753 out:
754         if (r < 0)
755                 condlog(3, "%s: dm command failed in %s: %s", name, __FUNCTION__, strerror(errno));
756         return r;
757 }
758
759 static int
760 dm_dev_t (const char * mapname, char * dev_t, int len)
761 {
762         struct dm_info info;
763
764         if (do_get_info(mapname, &info) != 0)
765                 return 1;
766
767         if (snprintf(dev_t, len, "%i:%i", info.major, info.minor) > len)
768                 return 1;
769
770         return 0;
771 }
772
773 int
774 dm_get_opencount (const char * mapname)
775 {
776         int r = -1;
777         struct dm_task *dmt;
778         struct dm_info info;
779
780         if (!(dmt = libmp_dm_task_create(DM_DEVICE_INFO)))
781                 return 0;
782
783         if (!dm_task_set_name(dmt, mapname))
784                 goto out;
785
786         if (!dm_task_run(dmt))
787                 goto out;
788
789         if (!dm_task_get_info(dmt, &info))
790                 goto out;
791
792         if (!info.exists)
793                 goto out;
794
795         r = info.open_count;
796 out:
797         dm_task_destroy(dmt);
798         return r;
799 }
800
801 int
802 dm_get_major_minor(const char *name, int *major, int *minor)
803 {
804         struct dm_info info;
805
806         if (do_get_info(name, &info) != 0)
807                 return -1;
808
809         *major = info.major;
810         *minor = info.minor;
811         return 0;
812 }
813
814 static int
815 has_partmap(const char *name, void *data)
816 {
817         return 1;
818 }
819
820 static int
821 partmap_in_use(const char *name, void *data)
822 {
823         int part_count, *ret_count = (int *)data;
824         int open_count = dm_get_opencount(name);
825
826         if (ret_count)
827                 (*ret_count)++;
828         part_count = 0;
829         if (open_count) {
830                 if (do_foreach_partmaps(name, partmap_in_use, &part_count))
831                         return 1;
832                 if (open_count != part_count) {
833                         condlog(2, "%s: map in use", name);
834                         return 1;
835                 }
836         }
837         return 0;
838 }
839
840 int _dm_flush_map (const char * mapname, int need_sync, int deferred_remove,
841                    int need_suspend, int retries)
842 {
843         int r;
844         int queue_if_no_path = 0;
845         int udev_flags = 0;
846         unsigned long long mapsize;
847         char params[PARAMS_SIZE] = {0};
848
849         if (dm_is_mpath(mapname) != 1)
850                 return 0; /* nothing to do */
851
852         /* if the device currently has no partitions, do not
853            run kpartx on it if you fail to delete it */
854         if (do_foreach_partmaps(mapname, has_partmap, NULL) == 0)
855                 udev_flags |= MPATH_UDEV_NO_KPARTX_FLAG;
856
857         /* If you aren't doing a deferred remove, make sure that no
858          * devices are in use */
859         if (!do_deferred(deferred_remove) && partmap_in_use(mapname, NULL))
860                         return 1;
861
862         if (need_suspend &&
863             !dm_get_map(mapname, &mapsize, params) &&
864             strstr(params, "queue_if_no_path")) {
865                 if (!dm_queue_if_no_path(mapname, 0))
866                         queue_if_no_path = 1;
867                 else
868                         /* Leave queue_if_no_path alone if unset failed */
869                         queue_if_no_path = -1;
870         }
871
872         if (dm_remove_partmaps(mapname, need_sync, deferred_remove))
873                 return 1;
874
875         if (!do_deferred(deferred_remove) && dm_get_opencount(mapname)) {
876                 condlog(2, "%s: map in use", mapname);
877                 return 1;
878         }
879
880         do {
881                 if (need_suspend && queue_if_no_path != -1)
882                         dm_simplecmd_flush(DM_DEVICE_SUSPEND, mapname, 0);
883
884                 r = dm_device_remove(mapname, need_sync, deferred_remove);
885
886                 if (r) {
887                         if (do_deferred(deferred_remove)
888                             && dm_map_present(mapname)) {
889                                 condlog(4, "multipath map %s remove deferred",
890                                         mapname);
891                                 return 2;
892                         }
893                         condlog(4, "multipath map %s removed", mapname);
894                         return 0;
895                 } else {
896                         condlog(2, "failed to remove multipath map %s",
897                                 mapname);
898                         if (need_suspend && queue_if_no_path != -1) {
899                                 dm_simplecmd_noflush(DM_DEVICE_RESUME,
900                                                      mapname, udev_flags);
901                         }
902                 }
903                 if (retries)
904                         sleep(1);
905         } while (retries-- > 0);
906
907         if (queue_if_no_path == 1)
908                 dm_queue_if_no_path(mapname, 1);
909
910         return 1;
911 }
912
913 #ifdef LIBDM_API_DEFERRED
914
915 int
916 dm_flush_map_nopaths(const char * mapname, int deferred_remove)
917 {
918         return _dm_flush_map(mapname, 1, deferred_remove, 0, 0);
919 }
920
921 #else
922
923 int
924 dm_flush_map_nopaths(const char * mapname, int deferred_remove)
925 {
926         return _dm_flush_map(mapname, 1, 0, 0, 0);
927 }
928
929 #endif
930
931 int dm_flush_maps (int retries)
932 {
933         int r = 0;
934         struct dm_task *dmt;
935         struct dm_names *names;
936         unsigned next = 0;
937
938         if (!(dmt = libmp_dm_task_create (DM_DEVICE_LIST)))
939                 return 0;
940
941         dm_task_no_open_count(dmt);
942
943         if (!dm_task_run (dmt))
944                 goto out;
945
946         if (!(names = dm_task_get_names (dmt)))
947                 goto out;
948
949         if (!names->dev)
950                 goto out;
951
952         do {
953                 r |= dm_suspend_and_flush_map(names->name, retries);
954                 next = names->next;
955                 names = (void *) names + next;
956         } while (next);
957
958 out:
959         dm_task_destroy (dmt);
960         return r;
961 }
962
963 int
964 dm_message(const char * mapname, char * message)
965 {
966         int r = 1;
967         struct dm_task *dmt;
968
969         if (!(dmt = libmp_dm_task_create(DM_DEVICE_TARGET_MSG)))
970                 return 1;
971
972         if (!dm_task_set_name(dmt, mapname))
973                 goto out;
974
975         if (!dm_task_set_sector(dmt, 0))
976                 goto out;
977
978         if (!dm_task_set_message(dmt, message))
979                 goto out;
980
981         dm_task_no_open_count(dmt);
982
983         if (!dm_task_run(dmt))
984                 goto out;
985
986         r = 0;
987 out:
988         if (r)
989                 condlog(0, "DM message failed [%s]", message);
990
991         dm_task_destroy(dmt);
992         return r;
993 }
994
995 int
996 dm_fail_path(const char * mapname, char * path)
997 {
998         char message[32];
999
1000         if (snprintf(message, 32, "fail_path %s", path) > 32)
1001                 return 1;
1002
1003         return dm_message(mapname, message);
1004 }
1005
1006 int
1007 dm_reinstate_path(const char * mapname, char * path)
1008 {
1009         char message[32];
1010
1011         if (snprintf(message, 32, "reinstate_path %s", path) > 32)
1012                 return 1;
1013
1014         return dm_message(mapname, message);
1015 }
1016
1017 int
1018 dm_queue_if_no_path(const char *mapname, int enable)
1019 {
1020         char *message;
1021
1022         if (enable)
1023                 message = "queue_if_no_path";
1024         else
1025                 message = "fail_if_no_path";
1026
1027         return dm_message(mapname, message);
1028 }
1029
1030 static int
1031 dm_groupmsg (const char * msg, const char * mapname, int index)
1032 {
1033         char message[32];
1034
1035         if (snprintf(message, 32, "%s_group %i", msg, index) > 32)
1036                 return 1;
1037
1038         return dm_message(mapname, message);
1039 }
1040
1041 int
1042 dm_switchgroup(const char * mapname, int index)
1043 {
1044         return dm_groupmsg("switch", mapname, index);
1045 }
1046
1047 int
1048 dm_enablegroup(const char * mapname, int index)
1049 {
1050         return dm_groupmsg("enable", mapname, index);
1051 }
1052
1053 int
1054 dm_disablegroup(const char * mapname, int index)
1055 {
1056         return dm_groupmsg("disable", mapname, index);
1057 }
1058
1059 struct multipath *dm_get_multipath(const char *name)
1060 {
1061         struct multipath *mpp = NULL;
1062
1063         mpp = alloc_multipath();
1064         if (!mpp)
1065                 return NULL;
1066
1067         mpp->alias = STRDUP(name);
1068
1069         if (!mpp->alias)
1070                 goto out;
1071
1072         if (dm_get_map(name, &mpp->size, NULL))
1073                 goto out;
1074
1075         dm_get_uuid(name, mpp->wwid, WWID_SIZE);
1076         dm_get_info(name, &mpp->dmi);
1077
1078         return mpp;
1079 out:
1080         free_multipath(mpp, KEEP_PATHS);
1081         return NULL;
1082 }
1083
1084 int
1085 dm_get_maps (vector mp)
1086 {
1087         struct multipath * mpp;
1088         int r = 1;
1089         struct dm_task *dmt;
1090         struct dm_names *names;
1091         unsigned next = 0;
1092
1093         if (!mp)
1094                 return 1;
1095
1096         if (!(dmt = libmp_dm_task_create(DM_DEVICE_LIST)))
1097                 return 1;
1098
1099         dm_task_no_open_count(dmt);
1100
1101         if (!dm_task_run(dmt))
1102                 goto out;
1103
1104         if (!(names = dm_task_get_names(dmt)))
1105                 goto out;
1106
1107         if (!names->dev) {
1108                 r = 0; /* this is perfectly valid */
1109                 goto out;
1110         }
1111
1112         do {
1113                 if (dm_is_mpath(names->name) != 1)
1114                         goto next;
1115
1116                 mpp = dm_get_multipath(names->name);
1117                 if (!mpp)
1118                         goto out;
1119
1120                 if (!vector_alloc_slot(mp))
1121                         goto out;
1122
1123                 vector_set_slot(mp, mpp);
1124                 mpp = NULL;
1125 next:
1126                 next = names->next;
1127                 names = (void *) names + next;
1128         } while (next);
1129
1130         r = 0;
1131         goto out;
1132 out:
1133         dm_task_destroy (dmt);
1134         return r;
1135 }
1136
1137 int
1138 dm_geteventnr (const char *name)
1139 {
1140         struct dm_info info;
1141
1142         if (do_get_info(name, &info) != 0)
1143                 return -1;
1144
1145         return info.event_nr;
1146 }
1147
1148 int
1149 dm_is_suspended(const char *name)
1150 {
1151         struct dm_info info;
1152
1153         if (do_get_info(name, &info) != 0)
1154                 return -1;
1155
1156         return info.suspended;
1157 }
1158
1159 char *
1160 dm_mapname(int major, int minor)
1161 {
1162         char * response = NULL;
1163         const char *map;
1164         struct dm_task *dmt;
1165         int r;
1166         int loop = MAX_WAIT * LOOPS_PER_SEC;
1167
1168         if (!(dmt = libmp_dm_task_create(DM_DEVICE_STATUS)))
1169                 return NULL;
1170
1171         if (!dm_task_set_major(dmt, major) ||
1172             !dm_task_set_minor(dmt, minor))
1173                 goto bad;
1174
1175         dm_task_no_open_count(dmt);
1176
1177         /*
1178          * device map might not be ready when we get here from
1179          * daemon uev_trigger -> uev_add_map
1180          */
1181         while (--loop) {
1182                 r = dm_task_run(dmt);
1183
1184                 if (r)
1185                         break;
1186
1187                 usleep(1000 * 1000 / LOOPS_PER_SEC);
1188         }
1189
1190         if (!r) {
1191                 condlog(0, "%i:%i: timeout fetching map name", major, minor);
1192                 goto bad;
1193         }
1194
1195         map = dm_task_get_name(dmt);
1196         if (map && strlen(map))
1197                 response = STRDUP((const char *)dm_task_get_name(dmt));
1198
1199         dm_task_destroy(dmt);
1200         return response;
1201 bad:
1202         dm_task_destroy(dmt);
1203         condlog(0, "%i:%i: error fetching map name", major, minor);
1204         return NULL;
1205 }
1206
1207 static int
1208 do_foreach_partmaps (const char * mapname,
1209                      int (*partmap_func)(const char *, void *),
1210                      void *data)
1211 {
1212         struct dm_task *dmt;
1213         struct dm_names *names;
1214         unsigned next = 0;
1215         char params[PARAMS_SIZE];
1216         unsigned long long size;
1217         char dev_t[32];
1218         int r = 1;
1219         char *p;
1220
1221         if (!(dmt = libmp_dm_task_create(DM_DEVICE_LIST)))
1222                 return 1;
1223
1224         dm_task_no_open_count(dmt);
1225
1226         if (!dm_task_run(dmt))
1227                 goto out;
1228
1229         if (!(names = dm_task_get_names(dmt)))
1230                 goto out;
1231
1232         if (!names->dev) {
1233                 r = 0; /* this is perfectly valid */
1234                 goto out;
1235         }
1236
1237         if (dm_dev_t(mapname, &dev_t[0], 32))
1238                 goto out;
1239
1240         do {
1241                 if (
1242                     /*
1243                      * if there is only a single "linear" target
1244                      */
1245                     (dm_type(names->name, TGT_PART) == 1) &&
1246
1247                     /*
1248                      * and the uuid of the target is a partition of the
1249                      * uuid of the multipath device
1250                      */
1251                     is_mpath_part(names->name, mapname) &&
1252
1253                     /*
1254                      * and we can fetch the map table from the kernel
1255                      */
1256                     !dm_get_map(names->name, &size, &params[0]) &&
1257
1258                     /*
1259                      * and the table maps over the multipath map
1260                      */
1261                     (p = strstr(params, dev_t)) &&
1262                     !isdigit(*(p + strlen(dev_t)))
1263                    ) {
1264                         if (partmap_func(names->name, data) != 0)
1265                                 goto out;
1266                 }
1267
1268                 next = names->next;
1269                 names = (void *) names + next;
1270         } while (next);
1271
1272         r = 0;
1273 out:
1274         dm_task_destroy (dmt);
1275         return r;
1276 }
1277
1278 struct remove_data {
1279         int need_sync;
1280         int deferred_remove;
1281 };
1282
1283 static int
1284 remove_partmap(const char *name, void *data)
1285 {
1286         struct remove_data *rd = (struct remove_data *)data;
1287
1288         if (dm_get_opencount(name)) {
1289                 dm_remove_partmaps(name, rd->need_sync, rd->deferred_remove);
1290                 if (!do_deferred(rd->deferred_remove) &&
1291                     dm_get_opencount(name)) {
1292                         condlog(2, "%s: map in use", name);
1293                         return 1;
1294                 }
1295         }
1296         condlog(4, "partition map %s removed", name);
1297         dm_device_remove(name, rd->need_sync, rd->deferred_remove);
1298         return 0;
1299 }
1300
1301 int
1302 dm_remove_partmaps (const char * mapname, int need_sync, int deferred_remove)
1303 {
1304         struct remove_data rd = { need_sync, deferred_remove };
1305         return do_foreach_partmaps(mapname, remove_partmap, &rd);
1306 }
1307
1308 #ifdef LIBDM_API_DEFERRED
1309
1310 static int
1311 cancel_remove_partmap (const char *name, void *unused)
1312 {
1313         if (dm_get_opencount(name))
1314                 dm_cancel_remove_partmaps(name);
1315         if (dm_message(name, "@cancel_deferred_remove") != 0)
1316                 condlog(0, "%s: can't cancel deferred remove: %s", name,
1317                         strerror(errno));
1318         return 0;
1319 }
1320
1321 static int
1322 dm_get_deferred_remove (const char * mapname)
1323 {
1324         struct dm_info info;
1325
1326         if (do_get_info(mapname, &info) != 0)
1327                 return -1;
1328
1329         return info.deferred_remove;
1330 }
1331
1332 static int
1333 dm_cancel_remove_partmaps(const char * mapname) {
1334         return do_foreach_partmaps(mapname, cancel_remove_partmap, NULL);
1335 }
1336
1337 int
1338 dm_cancel_deferred_remove (struct multipath *mpp)
1339 {
1340         int r = 0;
1341
1342         if (!dm_get_deferred_remove(mpp->alias))
1343                 return 0;
1344         if (mpp->deferred_remove == DEFERRED_REMOVE_IN_PROGRESS)
1345                 mpp->deferred_remove = DEFERRED_REMOVE_ON;
1346
1347         dm_cancel_remove_partmaps(mpp->alias);
1348         r = dm_message(mpp->alias, "@cancel_deferred_remove");
1349         if (r)
1350                 condlog(0, "%s: can't cancel deferred remove: %s", mpp->alias,
1351                                 strerror(errno));
1352         else
1353                 condlog(2, "%s: canceled deferred remove", mpp->alias);
1354         return r;
1355 }
1356
1357 #else
1358
1359 int
1360 dm_cancel_deferred_remove (struct multipath *mpp)
1361 {
1362         return 0;
1363 }
1364
1365 #endif
1366
1367 static struct dm_info *
1368 alloc_dminfo (void)
1369 {
1370         return MALLOC(sizeof(struct dm_info));
1371 }
1372
1373 int
1374 dm_get_info (const char * mapname, struct dm_info ** dmi)
1375 {
1376         if (!mapname)
1377                 return 1;
1378
1379         if (!*dmi)
1380                 *dmi = alloc_dminfo();
1381
1382         if (!*dmi)
1383                 return 1;
1384
1385         if (do_get_info(mapname, *dmi) != 0) {
1386                 memset(*dmi, 0, sizeof(struct dm_info));
1387                 FREE(*dmi);
1388                 *dmi = NULL;
1389                 return 1;
1390         }
1391         return 0;
1392 }
1393
1394 struct rename_data {
1395         const char *old;
1396         char *new;
1397         char *delim;
1398 };
1399
1400 static int
1401 rename_partmap (const char *name, void *data)
1402 {
1403         char buff[PARAMS_SIZE];
1404         int offset;
1405         struct rename_data *rd = (struct rename_data *)data;
1406
1407         if (strncmp(name, rd->old, strlen(rd->old)) != 0)
1408                 return 0;
1409         for (offset = strlen(rd->old); name[offset] && !(isdigit(name[offset])); offset++); /* do nothing */
1410         snprintf(buff, PARAMS_SIZE, "%s%s%s", rd->new, rd->delim,
1411                  name + offset);
1412         dm_rename(name, buff, rd->delim, SKIP_KPARTX_OFF);
1413         condlog(4, "partition map %s renamed", name);
1414         return 0;
1415 }
1416
1417 int
1418 dm_rename_partmaps (const char * old, char * new, char *delim)
1419 {
1420         struct rename_data rd;
1421
1422         rd.old = old;
1423         rd.new = new;
1424
1425         if (delim)
1426                 rd.delim = delim;
1427         else {
1428                 if (isdigit(new[strlen(new)-1]))
1429                         rd.delim = "p";
1430                 else
1431                         rd.delim = "";
1432         }
1433         return do_foreach_partmaps(old, rename_partmap, &rd);
1434 }
1435
1436 int
1437 dm_rename (const char * old, char * new, char *delim, int skip_kpartx)
1438 {
1439         int r = 0;
1440         struct dm_task *dmt;
1441         uint32_t cookie = 0;
1442         uint16_t udev_flags = DM_UDEV_DISABLE_LIBRARY_FALLBACK | ((skip_kpartx == SKIP_KPARTX_ON)? MPATH_UDEV_NO_KPARTX_FLAG : 0);
1443
1444         if (dm_rename_partmaps(old, new, delim))
1445                 return r;
1446
1447         if (!(dmt = libmp_dm_task_create(DM_DEVICE_RENAME)))
1448                 return r;
1449
1450         if (!dm_task_set_name(dmt, old))
1451                 goto out;
1452
1453         if (!dm_task_set_newname(dmt, new))
1454                 goto out;
1455
1456         dm_task_no_open_count(dmt);
1457
1458         if (!dm_task_set_cookie(dmt, &cookie, udev_flags))
1459                 goto out;
1460         r = dm_task_run(dmt);
1461
1462         dm_udev_wait(cookie);
1463
1464 out:
1465         dm_task_destroy(dmt);
1466
1467         return r;
1468 }
1469
1470 void dm_reassign_deps(char *table, const char *dep, const char *newdep)
1471 {
1472         char *n, *newtable;
1473         const char *p;
1474
1475         newtable = strdup(table);
1476         if (!newtable)
1477                 return;
1478         p = strstr(newtable, dep);
1479         n = table + (p - newtable);
1480         strcpy(n, newdep);
1481         n += strlen(newdep);
1482         p += strlen(dep);
1483         strcat(n, p);
1484         FREE(newtable);
1485 }
1486
1487 int dm_reassign_table(const char *name, char *old, char *new)
1488 {
1489         int r = 0, modified = 0;
1490         uint64_t start, length;
1491         struct dm_task *dmt, *reload_dmt;
1492         char *target, *params = NULL;
1493         char *buff;
1494         void *next = NULL;
1495
1496         if (!(dmt = libmp_dm_task_create(DM_DEVICE_TABLE)))
1497                 return 0;
1498
1499         if (!dm_task_set_name(dmt, name))
1500                 goto out;
1501
1502         dm_task_no_open_count(dmt);
1503
1504         if (!dm_task_run(dmt))
1505                 goto out;
1506         if (!(reload_dmt = libmp_dm_task_create(DM_DEVICE_RELOAD)))
1507                 goto out;
1508         if (!dm_task_set_name(reload_dmt, name))
1509                 goto out_reload;
1510
1511         do {
1512                 next = dm_get_next_target(dmt, next, &start, &length,
1513                                           &target, &params);
1514                 buff = strdup(params);
1515                 if (!buff) {
1516                         condlog(3, "%s: failed to replace target %s, "
1517                                 "out of memory", name, target);
1518                         goto out_reload;
1519                 }
1520                 if (strcmp(target, TGT_MPATH) && strstr(params, old)) {
1521                         condlog(3, "%s: replace target %s %s",
1522                                 name, target, buff);
1523                         dm_reassign_deps(buff, old, new);
1524                         condlog(3, "%s: with target %s %s",
1525                                 name, target, buff);
1526                         modified++;
1527                 }
1528                 dm_task_add_target(reload_dmt, start, length, target, buff);
1529                 free(buff);
1530         } while (next);
1531
1532         if (modified) {
1533                 dm_task_no_open_count(reload_dmt);
1534
1535                 if (!dm_task_run(reload_dmt)) {
1536                         condlog(3, "%s: failed to reassign targets", name);
1537                         goto out_reload;
1538                 }
1539                 dm_simplecmd_noflush(DM_DEVICE_RESUME, name,
1540                                      MPATH_UDEV_RELOAD_FLAG);
1541         }
1542         r = 1;
1543
1544 out_reload:
1545         dm_task_destroy(reload_dmt);
1546 out:
1547         dm_task_destroy(dmt);
1548         return r;
1549 }
1550
1551
1552 /*
1553  * Reassign existing device-mapper table(s) to not use
1554  * the block devices but point to the multipathed
1555  * device instead
1556  */
1557 int dm_reassign(const char *mapname)
1558 {
1559         struct dm_deps *deps;
1560         struct dm_task *dmt;
1561         struct dm_info info;
1562         char dev_t[32], dm_dep[32];
1563         int r = 0, i;
1564
1565         if (dm_dev_t(mapname, &dev_t[0], 32)) {
1566                 condlog(3, "%s: failed to get device number", mapname);
1567                 return 1;
1568         }
1569
1570         if (!(dmt = libmp_dm_task_create(DM_DEVICE_DEPS))) {
1571                 condlog(3, "%s: couldn't make dm task", mapname);
1572                 return 0;
1573         }
1574
1575         if (!dm_task_set_name(dmt, mapname))
1576                 goto out;
1577
1578         dm_task_no_open_count(dmt);
1579
1580         if (!dm_task_run(dmt))
1581                 goto out;
1582
1583         if (!dm_task_get_info(dmt, &info))
1584                 goto out;
1585
1586         if (!(deps = dm_task_get_deps(dmt)))
1587                 goto out;
1588
1589         if (!info.exists)
1590                 goto out;
1591
1592         for (i = 0; i < deps->count; i++) {
1593                 sprintf(dm_dep, "%d:%d",
1594                         major(deps->device[i]),
1595                         minor(deps->device[i]));
1596                 sysfs_check_holders(dm_dep, dev_t);
1597         }
1598
1599         r = 1;
1600 out:
1601         dm_task_destroy (dmt);
1602         return r;
1603 }
1604
1605 int dm_setgeometry(struct multipath *mpp)
1606 {
1607         struct dm_task *dmt;
1608         struct path *pp;
1609         char heads[4], sectors[4];
1610         char cylinders[10], start[32];
1611         int r = 0;
1612
1613         if (!mpp)
1614                 return 1;
1615
1616         pp = first_path(mpp);
1617         if (!pp) {
1618                 condlog(3, "%s: no path for geometry", mpp->alias);
1619                 return 1;
1620         }
1621         if (pp->geom.cylinders == 0 ||
1622             pp->geom.heads == 0 ||
1623             pp->geom.sectors == 0) {
1624                 condlog(3, "%s: invalid geometry on %s", mpp->alias, pp->dev);
1625                 return 1;
1626         }
1627
1628         if (!(dmt = libmp_dm_task_create(DM_DEVICE_SET_GEOMETRY)))
1629                 return 0;
1630
1631         if (!dm_task_set_name(dmt, mpp->alias))
1632                 goto out;
1633
1634         dm_task_no_open_count(dmt);
1635
1636         /* What a sick interface ... */
1637         snprintf(heads, 4, "%u", pp->geom.heads);
1638         snprintf(sectors, 4, "%u", pp->geom.sectors);
1639         snprintf(cylinders, 10, "%u", pp->geom.cylinders);
1640         snprintf(start, 32, "%lu", pp->geom.start);
1641         if (!dm_task_set_geometry(dmt, cylinders, heads, sectors, start)) {
1642                 condlog(3, "%s: Failed to set geometry", mpp->alias);
1643                 goto out;
1644         }
1645
1646         r = dm_task_run(dmt);
1647 out:
1648         dm_task_destroy(dmt);
1649
1650         return r;
1651 }