Add compability flag LIBDM_API_COOKIE
[platform/upstream/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
16 #include "checkers.h"
17 #include "vector.h"
18 #include "structs.h"
19 #include "debug.h"
20 #include "memory.h"
21 #include "devmapper.h"
22 #include "config.h"
23
24 #include "log_pthread.h"
25 #include <sys/types.h>
26 #include <time.h>
27
28 #define MAX_WAIT 5
29 #define LOOPS_PER_SEC 5
30
31 #define UUID_PREFIX "mpath-"
32 #define UUID_PREFIX_LEN 6
33
34 #ifndef LIBDM_API_COOKIE
35 static inline int dm_task_set_cookie(struct dm_task *dmt, uint32_t *c, int a)
36 {
37         return 1;
38 }
39
40 void udev_wait(unsigned int c)
41 {
42 }
43
44 void udev_set_sync_support(int c)
45 {
46 }
47 #else
48 void udev_wait(unsigned int c)
49 {
50         dm_udev_wait(c);
51 }
52
53 void udev_set_sync_support(int c)
54 {
55         dm_udev_set_sync_support(c);
56 }
57 #endif
58
59 static void
60 dm_write_log (int level, const char *file, int line, const char *f, ...)
61 {
62         va_list ap;
63         int thres;
64
65         if (level > 6)
66                 level = 6;
67
68         thres = (conf) ? conf->verbosity : 0;
69         if (thres <= 3 || level > thres)
70                 return;
71
72         va_start(ap, f);
73         if (!logsink) {
74                 time_t t = time(NULL);
75                 struct tm *tb = localtime(&t);
76                 char buff[16];
77
78                 strftime(buff, sizeof(buff), "%b %d %H:%M:%S", tb);
79                 buff[sizeof(buff)-1] = '\0';
80
81                 fprintf(stdout, "%s | ", buff);
82                 fprintf(stdout, "libdevmapper: %s(%i): ", file, line);
83                 vfprintf(stdout, f, ap);
84                 fprintf(stdout, "\n");
85         } else {
86                 condlog(level, "libdevmapper: %s(%i): ", file, line);
87                 log_safe(level + 3, f, ap);
88         }
89         va_end(ap);
90
91         return;
92 }
93
94 extern void
95 dm_init(void) {
96         dm_log_init(&dm_write_log);
97         dm_log_init_verbose(conf ? conf->verbosity + 3 : 0);
98 }
99
100 #define VERSION_GE(v, minv) ( \
101  (v[0] > minv[0]) || \
102  ((v[0] == minv[0]) && (v[1] > minv[1])) || \
103  ((v[0] == minv[0]) && (v[1] == minv[1]) && (v[2] >= minv[2])) \
104 )
105
106 static int
107 dm_lib_prereq (void)
108 {
109         char version[64];
110         int v[3];
111 #ifdef LIBDM_API_COOKIE
112         int minv[3] = {1, 2, 38};
113 #else
114         int minv[3] = {1, 2, 8};
115 #endif
116
117         dm_get_library_version(version, sizeof(version));
118         condlog(3, "libdevmapper version %s", version);
119         sscanf(version, "%d.%d.%d ", &v[0], &v[1], &v[2]);
120
121         if VERSION_GE(v, minv)
122                 return 0;
123         condlog(0, "libdevmapper version must be >= %d.%.2d.%.2d",
124                 minv[0], minv[1], minv[2]);
125         return 1;
126 }
127
128 static int
129 dm_drv_version (unsigned int * version, char * str)
130 {
131         int r = 2;
132         struct dm_task *dmt;
133         struct dm_versions *target;
134         struct dm_versions *last_target;
135         unsigned int *v;
136
137         if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
138                 return 1;
139
140         dm_task_no_open_count(dmt);
141
142         if (!dm_task_run(dmt)) {
143                 condlog(0, "Can not communicate with kernel DM");
144                 goto out;
145         }
146         target = dm_task_get_versions(dmt);
147
148         do {
149                 last_target = target;
150                 if (!strncmp(str, target->name, strlen(str))) {
151                         r = 1;
152                         break;
153                 }
154                 target = (void *) target + target->next;
155         } while (last_target != target);
156
157         if (r == 2) {
158                 condlog(0, "DM %s kernel driver not loaded", str);
159                 goto out;
160         }
161         v = target->version;
162         version[0] = v[0];
163         version[1] = v[1];
164         version[2] = v[2];
165         r = 0;
166 out:
167         dm_task_destroy(dmt);
168         return r;
169 }
170
171 int
172 dm_drv_get_rq (void)
173 {
174         unsigned int minv_dmrq[3] = {1, 1, 0};
175         unsigned int version[3] = {0, 0, 0};
176         unsigned int * v = version;
177
178         if (dm_drv_version(v, TGT_MPATH)) {
179                 /* in doubt return least capable */
180                 return 0;
181         }
182
183         /* test request based multipath capability */
184         if VERSION_GE(v, minv_dmrq) {
185                 condlog(3, "activate request-based multipathing mode "
186                            "(driver >= v%u.%u.%u)",
187                         minv_dmrq[0], minv_dmrq[1], minv_dmrq[2]);
188                 return 1;
189         }
190         return 0;
191 }
192
193 static int
194 dm_drv_prereq (void)
195 {
196         unsigned int minv[3] = {1, 0, 3};
197         unsigned int version[3] = {0, 0, 0};
198         unsigned int * v = version;
199
200         if (dm_drv_version(v, TGT_MPATH)) {
201                 /* in doubt return not capable */
202                 return 1;
203         }
204
205         /* test request based multipath capability */
206         condlog(3, "DM multipath kernel driver v%u.%u.%u",
207                 v[0], v[1], v[2]);
208
209         if VERSION_GE(v, minv)
210                 return 0;
211
212         condlog(0, "DM multipath kernel driver must be >= v%u.%u.%u",
213                 minv[0], minv[1], minv[2]);
214         return 1;
215 }
216
217 extern int
218 dm_prereq (void)
219 {
220         if (dm_lib_prereq())
221                 return 1;
222         return dm_drv_prereq();
223 }
224
225 static int
226 dm_simplecmd (int task, const char *name, int no_flush, int need_sync) {
227         int r = 0;
228         int udev_wait_flag = (need_sync && (task == DM_DEVICE_RESUME ||
229                                             task == DM_DEVICE_REMOVE));
230         struct dm_task *dmt;
231
232         if (!(dmt = dm_task_create (task)))
233                 return 0;
234
235         if (!dm_task_set_name (dmt, name))
236                 goto out;
237
238         dm_task_no_open_count(dmt);
239         dm_task_skip_lockfs(dmt);       /* for DM_DEVICE_RESUME */
240 #ifdef LIBDM_API_FLUSH
241         if (no_flush)
242                 dm_task_no_flush(dmt);          /* for DM_DEVICE_SUSPEND/RESUME */
243 #endif
244
245         if (udev_wait_flag && !dm_task_set_cookie(dmt, &conf->cookie, 0))
246                 goto out;
247         r = dm_task_run (dmt);
248
249         out:
250         dm_task_destroy (dmt);
251         return r;
252 }
253
254 extern int
255 dm_simplecmd_flush (int task, const char *name, int needsync) {
256         return dm_simplecmd(task, name, 0, needsync);
257 }
258
259 extern int
260 dm_simplecmd_noflush (int task, const char *name) {
261         return dm_simplecmd(task, name, 1, 1);
262 }
263
264 extern int
265 dm_addmap (int task, const char *target, struct multipath *mpp, int use_uuid,
266            int ro) {
267         int r = 0;
268         struct dm_task *dmt;
269         char *prefixed_uuid = NULL;
270
271         if (!(dmt = dm_task_create (task)))
272                 return 0;
273
274         if (!dm_task_set_name (dmt, mpp->alias))
275                 goto addout;
276
277         if (!dm_task_add_target (dmt, 0, mpp->size, target, mpp->params))
278                 goto addout;
279
280         if (ro)
281                 dm_task_set_ro(dmt);
282
283         if (use_uuid && mpp->wwid){
284                 prefixed_uuid = MALLOC(UUID_PREFIX_LEN + strlen(mpp->wwid) + 1);
285                 if (!prefixed_uuid) {
286                         condlog(0, "cannot create prefixed uuid : %s\n",
287                                 strerror(errno));
288                         goto addout;
289                 }
290                 sprintf(prefixed_uuid, UUID_PREFIX "%s", mpp->wwid);
291                 if (!dm_task_set_uuid(dmt, prefixed_uuid))
292                         goto freeout;
293         }
294
295         if (mpp->attribute_flags & (1 << ATTR_MODE) &&
296             !dm_task_set_mode(dmt, mpp->mode))
297                 goto freeout;
298         if (mpp->attribute_flags & (1 << ATTR_UID) &&
299             !dm_task_set_uid(dmt, mpp->uid))
300                 goto freeout;
301         if (mpp->attribute_flags & (1 << ATTR_GID) &&
302             !dm_task_set_gid(dmt, mpp->gid))
303                 goto freeout;
304
305         dm_task_no_open_count(dmt);
306
307         if (task == DM_DEVICE_CREATE &&
308             !dm_task_set_cookie(dmt, &conf->cookie, 0))
309                 goto freeout;
310         r = dm_task_run (dmt);
311
312         freeout:
313         if (prefixed_uuid)
314                 FREE(prefixed_uuid);
315
316         addout:
317         dm_task_destroy (dmt);
318
319         return r;
320 }
321
322 static int
323 _dm_addmap_create (struct multipath *mpp, int ro) {
324         int r;
325         r = dm_addmap(DM_DEVICE_CREATE, TGT_MPATH, mpp, 1, ro);
326         /*
327          * DM_DEVICE_CREATE is actually DM_DEV_CREATE + DM_TABLE_LOAD.
328          * Failing the second part leaves an empty map. Clean it up.
329          */
330         if (!r && dm_map_present(mpp->alias)) {
331                 condlog(3, "%s: failed to load map (a path might be in use)",
332                         mpp->alias);
333                 dm_flush_map_nosync(mpp->alias);
334         }
335         return r;
336 }
337
338 #define ADDMAP_RW 0
339 #define ADDMAP_RO 1
340
341 extern int
342 dm_addmap_create (struct multipath *mpp) {
343         return _dm_addmap_create(mpp, ADDMAP_RW);
344 }
345
346 extern int
347 dm_addmap_create_ro (struct multipath *mpp) {
348         return _dm_addmap_create(mpp, ADDMAP_RO);
349 }
350
351 extern int
352 dm_addmap_reload (struct multipath *mpp) {
353         return dm_addmap(DM_DEVICE_RELOAD, TGT_MPATH, mpp, 0, ADDMAP_RW);
354 }
355
356 extern int
357 dm_addmap_reload_ro (struct multipath *mpp) {
358         return dm_addmap(DM_DEVICE_RELOAD, TGT_MPATH, mpp, 0, ADDMAP_RO);
359 }
360
361 extern int
362 dm_map_present (const char * str)
363 {
364         int r = 0;
365         struct dm_task *dmt;
366         struct dm_info info;
367
368         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
369                 return 0;
370
371         if (!dm_task_set_name(dmt, str))
372                 goto out;
373
374         dm_task_no_open_count(dmt);
375
376         if (!dm_task_run(dmt))
377                 goto out;
378
379         if (!dm_task_get_info(dmt, &info))
380                 goto out;
381
382         if (info.exists)
383                 r = 1;
384 out:
385         dm_task_destroy(dmt);
386         return r;
387 }
388
389 extern int
390 dm_get_map(char * name, unsigned long long * size, char * outparams)
391 {
392         int r = 1;
393         struct dm_task *dmt;
394         void *next = NULL;
395         uint64_t start, length;
396         char *target_type = NULL;
397         char *params = NULL;
398
399         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
400                 return 1;
401
402         if (!dm_task_set_name(dmt, name))
403                 goto out;
404
405         dm_task_no_open_count(dmt);
406
407         if (!dm_task_run(dmt))
408                 goto out;
409
410         /* Fetch 1st target */
411         next = dm_get_next_target(dmt, next, &start, &length,
412                                   &target_type, &params);
413
414         if (size)
415                 *size = length;
416
417         if (snprintf(outparams, PARAMS_SIZE, "%s", params) <= PARAMS_SIZE)
418                 r = 0;
419 out:
420         dm_task_destroy(dmt);
421         return r;
422 }
423
424 extern int
425 dm_get_uuid(char *name, char *uuid)
426 {
427         struct dm_task *dmt;
428         const char *uuidtmp;
429         int r = 1;
430
431         dmt = dm_task_create(DM_DEVICE_INFO);
432         if (!dmt)
433                 return 1;
434
435         if (!dm_task_set_name (dmt, name))
436                 goto uuidout;
437
438         if (!dm_task_run(dmt))
439                 goto uuidout;
440
441         uuidtmp = dm_task_get_uuid(dmt);
442         if (uuidtmp) {
443                 if (!strncmp(uuidtmp, UUID_PREFIX, UUID_PREFIX_LEN))
444                         strcpy(uuid, uuidtmp + UUID_PREFIX_LEN);
445                 else
446                         strcpy(uuid, uuidtmp);
447         }
448         else
449                 uuid[0] = '\0';
450
451         r = 0;
452 uuidout:
453         dm_task_destroy(dmt);
454         return r;
455 }
456
457 extern int
458 dm_get_status(char * name, char * outstatus)
459 {
460         int r = 1;
461         struct dm_task *dmt;
462         void *next = NULL;
463         uint64_t start, length;
464         char *target_type;
465         char *status;
466
467         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
468                 return 1;
469
470         if (!dm_task_set_name(dmt, name))
471                 goto out;
472
473         dm_task_no_open_count(dmt);
474
475         if (!dm_task_run(dmt))
476                 goto out;
477
478         /* Fetch 1st target */
479         next = dm_get_next_target(dmt, next, &start, &length,
480                                   &target_type, &status);
481
482         if (snprintf(outstatus, PARAMS_SIZE, "%s", status) <= PARAMS_SIZE)
483                 r = 0;
484 out:
485         if (r)
486                 condlog(0, "%s: error getting map status string", name);
487
488         dm_task_destroy(dmt);
489         return r;
490 }
491
492 /*
493  * returns:
494  *    1 : match
495  *    0 : no match
496  *   -1 : empty map
497  */
498 extern int
499 dm_type(const char * name, char * type)
500 {
501         int r = 0;
502         struct dm_task *dmt;
503         void *next = NULL;
504         uint64_t start, length;
505         char *target_type = NULL;
506         char *params;
507
508         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
509                 return 0;
510
511         if (!dm_task_set_name(dmt, name))
512                 goto out;
513
514         dm_task_no_open_count(dmt);
515
516         if (!dm_task_run(dmt))
517                 goto out;
518
519         /* Fetch 1st target */
520         next = dm_get_next_target(dmt, next, &start, &length,
521                                   &target_type, &params);
522
523         if (!target_type)
524                 r = -1;
525         else if (!strcmp(target_type, type))
526                 r = 1;
527
528 out:
529         dm_task_destroy(dmt);
530         return r;
531 }
532
533 static int
534 dm_dev_t (const char * mapname, char * dev_t, int len)
535 {
536         int r = 1;
537         struct dm_task *dmt;
538         struct dm_info info;
539
540         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
541                 return 0;
542
543         if (!dm_task_set_name(dmt, mapname))
544                 goto out;
545
546         if (!dm_task_run(dmt))
547                 goto out;
548
549         if (!dm_task_get_info(dmt, &info))
550                 goto out;
551
552         r = info.open_count;
553         if (snprintf(dev_t, len, "%i:%i", info.major, info.minor) > len)
554                     goto out;
555
556         r = 0;
557 out:
558         dm_task_destroy(dmt);
559         return r;
560 }
561
562 int
563 dm_get_opencount (const char * mapname)
564 {
565         int r = -1;
566         struct dm_task *dmt;
567         struct dm_info info;
568
569         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
570                 return 0;
571
572         if (!dm_task_set_name(dmt, mapname))
573                 goto out;
574
575         if (!dm_task_run(dmt))
576                 goto out;
577
578         if (!dm_task_get_info(dmt, &info))
579                 goto out;
580
581         r = info.open_count;
582 out:
583         dm_task_destroy(dmt);
584         return r;
585 }
586
587 int
588 dm_get_minor (char * mapname)
589 {
590         int r = -1;
591         struct dm_task *dmt;
592         struct dm_info info;
593
594         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
595                 return 0;
596
597         if (!dm_task_set_name(dmt, mapname))
598                 goto out;
599
600         if (!dm_task_run(dmt))
601                 goto out;
602
603         if (!dm_task_get_info(dmt, &info))
604                 goto out;
605
606         r = info.minor;
607 out:
608         dm_task_destroy(dmt);
609         return r;
610 }
611
612 extern int
613 _dm_flush_map (const char * mapname, int need_sync)
614 {
615         int r;
616
617         if (!dm_map_present(mapname))
618                 return 0;
619
620         if (dm_type(mapname, TGT_MPATH) <= 0)
621                 return 0; /* nothing to do */
622
623         if (dm_remove_partmaps(mapname, need_sync))
624                 return 1;
625
626         if (dm_get_opencount(mapname)) {
627                 condlog(2, "%s: map in use", mapname);
628                 return 1;
629         }
630
631         r = dm_simplecmd_flush(DM_DEVICE_REMOVE, mapname, need_sync);
632
633         if (r) {
634                 condlog(4, "multipath map %s removed", mapname);
635                 return 0;
636         }
637         return 1;
638 }
639
640 extern int
641 dm_flush_maps (void)
642 {
643         int r = 0;
644         struct dm_task *dmt;
645         struct dm_names *names;
646         unsigned next = 0;
647
648         if (!(dmt = dm_task_create (DM_DEVICE_LIST)))
649                 return 0;
650
651         dm_task_no_open_count(dmt);
652
653         if (!dm_task_run (dmt))
654                 goto out;
655
656         if (!(names = dm_task_get_names (dmt)))
657                 goto out;
658
659         if (!names->dev)
660                 goto out;
661
662         do {
663                 r |= dm_flush_map(names->name);
664                 next = names->next;
665                 names = (void *) names + next;
666         } while (next);
667
668         out:
669         dm_task_destroy (dmt);
670         return r;
671 }
672
673 int
674 dm_message(char * mapname, char * message)
675 {
676         int r = 1;
677         struct dm_task *dmt;
678
679         if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
680                 return 1;
681
682         if (!dm_task_set_name(dmt, mapname))
683                 goto out;
684
685         if (!dm_task_set_sector(dmt, 0))
686                 goto out;
687
688         if (!dm_task_set_message(dmt, message))
689                 goto out;
690
691         dm_task_no_open_count(dmt);
692
693         if (!dm_task_run(dmt))
694                 goto out;
695
696         r = 0;
697 out:
698         if (r)
699                 condlog(0, "DM message failed [%s]", message);
700
701         dm_task_destroy(dmt);
702         return r;
703 }
704
705 int
706 dm_fail_path(char * mapname, char * path)
707 {
708         char message[32];
709
710         if (snprintf(message, 32, "fail_path %s\n", path) > 32)
711                 return 1;
712
713         return dm_message(mapname, message);
714 }
715
716 int
717 dm_reinstate_path(char * mapname, char * path)
718 {
719         char message[32];
720
721         if (snprintf(message, 32, "reinstate_path %s\n", path) > 32)
722                 return 1;
723
724         return dm_message(mapname, message);
725 }
726
727 int
728 dm_queue_if_no_path(char *mapname, int enable)
729 {
730         char *message;
731
732         if (enable)
733                 message = "queue_if_no_path\n";
734         else
735                 message = "fail_if_no_path\n";
736
737         return dm_message(mapname, message);
738 }
739
740 int
741 dm_set_pg_timeout(char *mapname, int timeout_val)
742 {
743         char message[24];
744
745         if (snprintf(message, 24, "set_pg_timeout %d", timeout_val) >= 24)
746                 return 1;
747         return dm_message(mapname, message);
748 }
749
750 static int
751 dm_groupmsg (char * msg, char * mapname, int index)
752 {
753         char message[32];
754
755         if (snprintf(message, 32, "%s_group %i\n", msg, index) > 32)
756                 return 1;
757
758         return dm_message(mapname, message);
759 }
760
761 int
762 dm_switchgroup(char * mapname, int index)
763 {
764         return dm_groupmsg("switch", mapname, index);
765 }
766
767 int
768 dm_enablegroup(char * mapname, int index)
769 {
770         return dm_groupmsg("enable", mapname, index);
771 }
772
773 int
774 dm_disablegroup(char * mapname, int index)
775 {
776         return dm_groupmsg("disable", mapname, index);
777 }
778
779 int
780 dm_get_maps (vector mp)
781 {
782         struct multipath * mpp;
783         int r = 1;
784         int info;
785         struct dm_task *dmt;
786         struct dm_names *names;
787         unsigned next = 0;
788
789         if (!mp)
790                 return 1;
791
792         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
793                 return 1;
794
795         dm_task_no_open_count(dmt);
796
797         if (!dm_task_run(dmt))
798                 goto out;
799
800         if (!(names = dm_task_get_names(dmt)))
801                 goto out;
802
803         if (!names->dev) {
804                 r = 0; /* this is perfectly valid */
805                 goto out;
806         }
807
808         do {
809                 info = dm_type(names->name, TGT_MPATH);
810
811                 if (info <= 0)
812                         goto next;
813
814                 mpp = alloc_multipath();
815
816                 if (!mpp)
817                         goto out;
818
819                 mpp->alias = STRDUP(names->name);
820
821                 if (!mpp->alias)
822                         goto out1;
823
824                 if (info > 0) {
825                         if (dm_get_map(names->name, &mpp->size, mpp->params))
826                                 goto out1;
827
828                         if (dm_get_status(names->name, mpp->status))
829                                 goto out1;
830
831                         dm_get_uuid(names->name, mpp->wwid);
832                         dm_get_info(names->name, &mpp->dmi);
833                 }
834
835                 if (!vector_alloc_slot(mp))
836                         goto out1;
837
838                 vector_set_slot(mp, mpp);
839                 mpp = NULL;
840 next:
841                 next = names->next;
842                 names = (void *) names + next;
843         } while (next);
844
845         r = 0;
846         goto out;
847 out1:
848         free_multipath(mpp, KEEP_PATHS);
849 out:
850         dm_task_destroy (dmt);
851         return r;
852 }
853
854 extern char *
855 dm_get_name(char *uuid)
856 {
857         struct dm_task *dmt;
858         struct dm_info info;
859         char *prefixed_uuid, *name = NULL;
860         const char *nametmp;
861
862         dmt = dm_task_create(DM_DEVICE_INFO);
863         if (!dmt)
864                 return NULL;
865
866         prefixed_uuid = MALLOC(UUID_PREFIX_LEN + strlen(uuid) + 1);
867         if (!prefixed_uuid) {
868                 condlog(0, "cannot create prefixed uuid : %s\n",
869                         strerror(errno));
870                 goto freeout;
871         }
872         sprintf(prefixed_uuid, UUID_PREFIX "%s", uuid);
873         if (!dm_task_set_uuid(dmt, prefixed_uuid))
874                 goto freeout;
875
876         if (!dm_task_run(dmt))
877                 goto freeout;
878
879         if (!dm_task_get_info(dmt, &info) || !info.exists)
880                 goto freeout;
881
882         nametmp = dm_task_get_name(dmt);
883         if (nametmp && strlen(nametmp)) {
884                 name = MALLOC(strlen(nametmp) + 1);
885                 if (name)
886                         strcpy(name, nametmp);
887         } else {
888                 condlog(2, "%s: no device-mapper name found", uuid);
889         }
890
891 freeout:
892         if (prefixed_uuid)
893                 FREE(prefixed_uuid);
894         dm_task_destroy(dmt);
895
896         return name;
897 }
898
899 int
900 dm_geteventnr (char *name)
901 {
902         struct dm_task *dmt;
903         struct dm_info info;
904
905         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
906                 return 0;
907
908         if (!dm_task_set_name(dmt, name))
909                 goto out;
910
911         dm_task_no_open_count(dmt);
912
913         if (!dm_task_run(dmt))
914                 goto out;
915
916         if (!dm_task_get_info(dmt, &info)) {
917                 info.event_nr = 0;
918                 goto out;
919         }
920
921         if (!info.exists) {
922                 info.event_nr = 0;
923                 goto out;
924         }
925
926 out:
927         dm_task_destroy(dmt);
928
929         return info.event_nr;
930 }
931
932 char *
933 dm_mapname(int major, int minor)
934 {
935         char * response = NULL;
936         const char *map;
937         struct dm_task *dmt;
938         int r;
939         int loop = MAX_WAIT * LOOPS_PER_SEC;
940
941         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
942                 return NULL;
943
944         if (!dm_task_set_major(dmt, major) ||
945             !dm_task_set_minor(dmt, minor))
946                 goto bad;
947
948         dm_task_no_open_count(dmt);
949
950         /*
951          * device map might not be ready when we get here from
952          * daemon uev_trigger -> uev_add_map
953          */
954         while (--loop) {
955                 r = dm_task_run(dmt);
956
957                 if (r)
958                         break;
959
960                 usleep(1000 * 1000 / LOOPS_PER_SEC);
961         }
962
963         if (!r) {
964                 condlog(0, "%i:%i: timeout fetching map name", major, minor);
965                 goto bad;
966         }
967
968         map = dm_task_get_name(dmt);
969         if (map && strlen(map))
970                 response = STRDUP((char *)dm_task_get_name(dmt));
971
972         dm_task_destroy(dmt);
973         return response;
974 bad:
975         dm_task_destroy(dmt);
976         condlog(0, "%i:%i: error fetching map name", major, minor);
977         return NULL;
978 }
979
980 int
981 dm_remove_partmaps (const char * mapname, int need_sync)
982 {
983         struct dm_task *dmt;
984         struct dm_names *names;
985         unsigned next = 0;
986         char params[PARAMS_SIZE];
987         unsigned long long size;
988         char dev_t[32];
989         int r = 1;
990
991         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
992                 return 1;
993
994         dm_task_no_open_count(dmt);
995
996         if (!dm_task_run(dmt))
997                 goto out;
998
999         if (!(names = dm_task_get_names(dmt)))
1000                 goto out;
1001
1002         if (!names->dev) {
1003                 r = 0; /* this is perfectly valid */
1004                 goto out;
1005         }
1006
1007         if (dm_dev_t(mapname, &dev_t[0], 32))
1008                 goto out;
1009
1010         do {
1011                 if (
1012                     /*
1013                      * if devmap target is "linear"
1014                      */
1015                     (dm_type(names->name, TGT_PART) > 0) &&
1016
1017                     /*
1018                      * and the multipath mapname and the part mapname start
1019                      * the same
1020                      */
1021                     !strncmp(names->name, mapname, strlen(mapname)) &&
1022
1023                     /*
1024                      * and the opencount is 0 for us to allow removal
1025                      */
1026                     !dm_get_opencount(names->name) &&
1027
1028                     /*
1029                      * and we can fetch the map table from the kernel
1030                      */
1031                     !dm_get_map(names->name, &size, &params[0]) &&
1032
1033                     /*
1034                      * and the table maps over the multipath map
1035                      */
1036                     strstr(params, dev_t)
1037                    ) {
1038                                 /*
1039                                  * then it's a kpartx generated partition.
1040                                  * remove it.
1041                                  */
1042                                 condlog(4, "partition map %s removed",
1043                                         names->name);
1044                                 dm_simplecmd_flush(DM_DEVICE_REMOVE, names->name, need_sync);
1045                    }
1046
1047                 next = names->next;
1048                 names = (void *) names + next;
1049         } while (next);
1050
1051         r = 0;
1052 out:
1053         dm_task_destroy (dmt);
1054         return r;
1055 }
1056
1057 static struct dm_info *
1058 alloc_dminfo (void)
1059 {
1060         return MALLOC(sizeof(struct dm_info));
1061 }
1062
1063 int
1064 dm_get_info (char * mapname, struct dm_info ** dmi)
1065 {
1066         int r = 1;
1067         struct dm_task *dmt = NULL;
1068
1069         if (!mapname)
1070                 return 1;
1071
1072         if (!*dmi)
1073                 *dmi = alloc_dminfo();
1074
1075         if (!*dmi)
1076                 return 1;
1077
1078         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
1079                 goto out;
1080
1081         if (!dm_task_set_name(dmt, mapname))
1082                 goto out;
1083
1084         dm_task_no_open_count(dmt);
1085
1086         if (!dm_task_run(dmt))
1087                 goto out;
1088
1089         if (!dm_task_get_info(dmt, *dmi))
1090                 goto out;
1091
1092         r = 0;
1093 out:
1094         if (r) {
1095                 memset(*dmi, 0, sizeof(struct dm_info));
1096                 FREE(*dmi);
1097                 *dmi = NULL;
1098         }
1099
1100         if (dmt)
1101                 dm_task_destroy(dmt);
1102
1103         return r;
1104 }
1105
1106 int
1107 dm_rename_partmaps (char * old, char * new)
1108 {
1109         struct dm_task *dmt;
1110         struct dm_names *names;
1111         unsigned next = 0;
1112         char buff[PARAMS_SIZE];
1113         unsigned long long size;
1114         char dev_t[32];
1115         int r = 1;
1116
1117         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
1118                 return 1;
1119
1120         dm_task_no_open_count(dmt);
1121
1122         if (!dm_task_run(dmt))
1123                 goto out;
1124
1125         if (!(names = dm_task_get_names(dmt)))
1126                 goto out;
1127
1128         if (!names->dev) {
1129                 r = 0; /* this is perfectly valid */
1130                 goto out;
1131         }
1132
1133         if (dm_dev_t(old, &dev_t[0], 32))
1134                 goto out;
1135
1136         do {
1137                 if (
1138                     /*
1139                      * if devmap target is "linear"
1140                      */
1141                     (dm_type(names->name, TGT_PART) > 0) &&
1142
1143                     /*
1144                      * and the multipath mapname and the part mapname start
1145                      * the same
1146                      */
1147                     !strncmp(names->name, old, strlen(old)) &&
1148
1149                     /*
1150                      * and we can fetch the map table from the kernel
1151                      */
1152                     !dm_get_map(names->name, &size, &buff[0]) &&
1153
1154                     /*
1155                      * and the table maps over the multipath map
1156                      */
1157                     strstr(buff, dev_t)
1158                    ) {
1159                                 /*
1160                                  * then it's a kpartx generated partition.
1161                                  * Rename it.
1162                                  */
1163                                 snprintf(buff, PARAMS_SIZE, "%s%s",
1164                                          new, names->name + strlen(old));
1165                                 dm_rename(names->name, buff);
1166                                 condlog(4, "partition map %s renamed",
1167                                         names->name);
1168                    }
1169
1170                 next = names->next;
1171                 names = (void *) names + next;
1172         } while (next);
1173
1174         r = 0;
1175 out:
1176         dm_task_destroy (dmt);
1177         return r;
1178 }
1179
1180 int
1181 dm_rename (char * old, char * new)
1182 {
1183         int r = 0;
1184         struct dm_task *dmt;
1185
1186         if (dm_rename_partmaps(old, new))
1187                 return r;
1188
1189         if (!(dmt = dm_task_create(DM_DEVICE_RENAME)))
1190                 return r;
1191
1192         if (!dm_task_set_name(dmt, old))
1193                 goto out;
1194
1195         if (!dm_task_set_newname(dmt, new))
1196                 goto out;
1197
1198         dm_task_no_open_count(dmt);
1199
1200         if (!dm_task_set_cookie(dmt, &conf->cookie, 0))
1201                 goto out;
1202         if (!dm_task_run(dmt))
1203                 goto out;
1204
1205         r = 1;
1206 out:
1207         dm_task_destroy(dmt);
1208         return r;
1209 }
1210
1211 int dm_setgeometry(struct multipath *mpp)
1212 {
1213         struct dm_task *dmt;
1214         struct path *pp;
1215         char heads[4], sectors[4];
1216         char cylinders[10], start[32];
1217         int r = 0;
1218
1219         if (!mpp)
1220                 return 1;
1221
1222         pp = first_path(mpp);
1223         if (!pp) {
1224                 condlog(3, "%s: no path for geometry", mpp->alias);
1225                 return 1;
1226         }
1227         if (pp->geom.cylinders == 0 ||
1228             pp->geom.heads == 0 ||
1229             pp->geom.sectors == 0) {
1230                 condlog(3, "%s: invalid geometry on %s", mpp->alias, pp->dev);
1231                 return 1;
1232         }
1233
1234         if (!(dmt = dm_task_create(DM_DEVICE_SET_GEOMETRY)))
1235                 return 0;
1236
1237         if (!dm_task_set_name(dmt, mpp->alias))
1238                 goto out;
1239
1240         dm_task_no_open_count(dmt);
1241
1242         /* What a sick interface ... */
1243         snprintf(heads, 4, "%u", pp->geom.heads);
1244         snprintf(sectors, 4, "%u", pp->geom.sectors);
1245         snprintf(cylinders, 10, "%u", pp->geom.cylinders);
1246         snprintf(start, 32, "%lu", pp->geom.start);
1247         if (!dm_task_set_geometry(dmt, cylinders, heads, sectors, start)) {
1248                 condlog(3, "%s: Failed to set geometry", mpp->alias);
1249                 goto out;
1250         }
1251
1252         r = dm_task_run(dmt);
1253 out:
1254         dm_task_destroy(dmt);
1255
1256         return r;
1257 }