Correctly remove logical partition maps
[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, char * params,
266            int use_uuid, 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, 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         condlog(4, "%s: addmap [0 %llu %s %s]\n", mpp->alias, mpp->size,
305                 target, params);
306
307         dm_task_no_open_count(dmt);
308
309         if (task == DM_DEVICE_CREATE &&
310             !dm_task_set_cookie(dmt, &conf->cookie, 0))
311                 goto freeout;
312         r = dm_task_run (dmt);
313
314         freeout:
315         if (prefixed_uuid)
316                 FREE(prefixed_uuid);
317
318         addout:
319         dm_task_destroy (dmt);
320
321         return r;
322 }
323
324 static int
325 _dm_addmap_create (struct multipath *mpp, char * params, int ro) {
326         int r;
327         r = dm_addmap(DM_DEVICE_CREATE, TGT_MPATH, mpp, params, 1, ro);
328         /*
329          * DM_DEVICE_CREATE is actually DM_DEV_CREATE + DM_TABLE_LOAD.
330          * Failing the second part leaves an empty map. Clean it up.
331          */
332         if (!r && dm_map_present(mpp->alias)) {
333                 condlog(3, "%s: failed to load map (a path might be in use)",
334                         mpp->alias);
335                 dm_flush_map_nosync(mpp->alias);
336         }
337         return r;
338 }
339
340 #define ADDMAP_RW 0
341 #define ADDMAP_RO 1
342
343 extern int
344 dm_addmap_create (struct multipath *mpp, char *params) {
345         return _dm_addmap_create(mpp, params, ADDMAP_RW);
346 }
347
348 extern int
349 dm_addmap_create_ro (struct multipath *mpp, char *params) {
350         return _dm_addmap_create(mpp, params, ADDMAP_RO);
351 }
352
353 extern int
354 dm_addmap_reload (struct multipath *mpp, char *params) {
355         return dm_addmap(DM_DEVICE_RELOAD, TGT_MPATH, mpp, params, 0, ADDMAP_RW);
356 }
357
358 extern int
359 dm_addmap_reload_ro (struct multipath *mpp, char *params) {
360         return dm_addmap(DM_DEVICE_RELOAD, TGT_MPATH, mpp, params, 0, ADDMAP_RO);
361 }
362
363 extern int
364 dm_map_present (const char * str)
365 {
366         int r = 0;
367         struct dm_task *dmt;
368         struct dm_info info;
369
370         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
371                 return 0;
372
373         if (!dm_task_set_name(dmt, str))
374                 goto out;
375
376         dm_task_no_open_count(dmt);
377
378         if (!dm_task_run(dmt))
379                 goto out;
380
381         if (!dm_task_get_info(dmt, &info))
382                 goto out;
383
384         if (info.exists)
385                 r = 1;
386 out:
387         dm_task_destroy(dmt);
388         return r;
389 }
390
391 extern int
392 dm_get_map(char * name, unsigned long long * size, char * outparams)
393 {
394         int r = 1;
395         struct dm_task *dmt;
396         void *next = NULL;
397         uint64_t start, length;
398         char *target_type = NULL;
399         char *params = NULL;
400
401         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
402                 return 1;
403
404         if (!dm_task_set_name(dmt, name))
405                 goto out;
406
407         dm_task_no_open_count(dmt);
408
409         if (!dm_task_run(dmt))
410                 goto out;
411
412         /* Fetch 1st target */
413         next = dm_get_next_target(dmt, next, &start, &length,
414                                   &target_type, &params);
415
416         if (size)
417                 *size = length;
418
419         if (!outparams) {
420                 r = 0;
421                 goto out;
422         }
423         if (snprintf(outparams, PARAMS_SIZE, "%s", params) <= PARAMS_SIZE)
424                 r = 0;
425 out:
426         dm_task_destroy(dmt);
427         return r;
428 }
429
430 static int
431 dm_get_prefixed_uuid(const char *name, char *uuid)
432 {
433         struct dm_task *dmt;
434         const char *uuidtmp;
435         int r = 1;
436
437         dmt = dm_task_create(DM_DEVICE_INFO);
438         if (!dmt)
439                 return 1;
440
441         if (!dm_task_set_name (dmt, name))
442                 goto uuidout;
443
444         if (!dm_task_run(dmt))
445                 goto uuidout;
446
447         uuidtmp = dm_task_get_uuid(dmt);
448         if (uuidtmp)
449                 strcpy(uuid, uuidtmp);
450         else
451                 uuid[0] = '\0';
452
453         r = 0;
454 uuidout:
455         dm_task_destroy(dmt);
456         return r;
457 }
458
459 extern int
460 dm_get_uuid(char *name, char *uuid)
461 {
462         char uuidtmp[WWID_SIZE];
463
464         if (dm_get_prefixed_uuid(name, uuidtmp))
465                 return 1;
466
467         if (!strncmp(uuidtmp, UUID_PREFIX, UUID_PREFIX_LEN))
468                 strcpy(uuid, uuidtmp + UUID_PREFIX_LEN);
469         else
470                 strcpy(uuid, uuidtmp);
471
472         return 0;
473 }
474
475 /*
476  * returns:
477  *    0 : if both uuids end with same suffix which starts with UUID_PREFIX
478  *    1 : otherwise
479  */
480 int
481 dm_compare_uuid(const char* mapname1, const char* mapname2)
482 {
483         char *p1, *p2;
484         char uuid1[WWID_SIZE], uuid2[WWID_SIZE];
485
486         if (dm_get_prefixed_uuid(mapname1, uuid1))
487                 return 1;
488
489         if (dm_get_prefixed_uuid(mapname2, uuid2))
490                 return 1;
491
492         p1 = strstr(uuid1, UUID_PREFIX);
493         p2 = strstr(uuid2, UUID_PREFIX);
494         if (p1 && p2 && !strcmp(p1, p2))
495                 return 0;
496
497         return 1;
498 }
499
500 extern int
501 dm_get_status(char * name, char * outstatus)
502 {
503         int r = 1;
504         struct dm_task *dmt;
505         void *next = NULL;
506         uint64_t start, length;
507         char *target_type;
508         char *status;
509
510         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
511                 return 1;
512
513         if (!dm_task_set_name(dmt, name))
514                 goto out;
515
516         dm_task_no_open_count(dmt);
517
518         if (!dm_task_run(dmt))
519                 goto out;
520
521         /* Fetch 1st target */
522         next = dm_get_next_target(dmt, next, &start, &length,
523                                   &target_type, &status);
524
525         if (snprintf(outstatus, PARAMS_SIZE, "%s", status) <= PARAMS_SIZE)
526                 r = 0;
527 out:
528         if (r)
529                 condlog(0, "%s: error getting map status string", name);
530
531         dm_task_destroy(dmt);
532         return r;
533 }
534
535 /*
536  * returns:
537  *    1 : match
538  *    0 : no match
539  *   -1 : empty map
540  */
541 extern int
542 dm_type(const char * name, char * type)
543 {
544         int r = 0;
545         struct dm_task *dmt;
546         void *next = NULL;
547         uint64_t start, length;
548         char *target_type = NULL;
549         char *params;
550
551         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
552                 return 0;
553
554         if (!dm_task_set_name(dmt, name))
555                 goto out;
556
557         dm_task_no_open_count(dmt);
558
559         if (!dm_task_run(dmt))
560                 goto out;
561
562         /* Fetch 1st target */
563         next = dm_get_next_target(dmt, next, &start, &length,
564                                   &target_type, &params);
565
566         if (!target_type)
567                 r = -1;
568         else if (!strcmp(target_type, type))
569                 r = 1;
570
571 out:
572         dm_task_destroy(dmt);
573         return r;
574 }
575
576 static int
577 dm_dev_t (const char * mapname, char * dev_t, int len)
578 {
579         int r = 1;
580         struct dm_task *dmt;
581         struct dm_info info;
582
583         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
584                 return 0;
585
586         if (!dm_task_set_name(dmt, mapname))
587                 goto out;
588
589         if (!dm_task_run(dmt))
590                 goto out;
591
592         if (!dm_task_get_info(dmt, &info))
593                 goto out;
594
595         r = info.open_count;
596         if (snprintf(dev_t, len, "%i:%i", info.major, info.minor) > len)
597                     goto out;
598
599         r = 0;
600 out:
601         dm_task_destroy(dmt);
602         return r;
603 }
604
605 int
606 dm_get_opencount (const char * mapname)
607 {
608         int r = -1;
609         struct dm_task *dmt;
610         struct dm_info info;
611
612         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
613                 return 0;
614
615         if (!dm_task_set_name(dmt, mapname))
616                 goto out;
617
618         if (!dm_task_run(dmt))
619                 goto out;
620
621         if (!dm_task_get_info(dmt, &info))
622                 goto out;
623
624         r = info.open_count;
625 out:
626         dm_task_destroy(dmt);
627         return r;
628 }
629
630 int
631 dm_get_minor (char * mapname)
632 {
633         int r = -1;
634         struct dm_task *dmt;
635         struct dm_info info;
636
637         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
638                 return 0;
639
640         if (!dm_task_set_name(dmt, mapname))
641                 goto out;
642
643         if (!dm_task_run(dmt))
644                 goto out;
645
646         if (!dm_task_get_info(dmt, &info))
647                 goto out;
648
649         r = info.minor;
650 out:
651         dm_task_destroy(dmt);
652         return r;
653 }
654
655 extern int
656 _dm_flush_map (const char * mapname, int need_sync)
657 {
658         int r;
659
660         if (!dm_map_present(mapname))
661                 return 0;
662
663         if (dm_type(mapname, TGT_MPATH) <= 0)
664                 return 0; /* nothing to do */
665
666         if (dm_remove_partmaps(mapname, need_sync))
667                 return 1;
668
669         if (dm_get_opencount(mapname)) {
670                 condlog(2, "%s: map in use", mapname);
671                 return 1;
672         }
673
674         r = dm_simplecmd_flush(DM_DEVICE_REMOVE, mapname, need_sync);
675
676         if (r) {
677                 condlog(4, "multipath map %s removed", mapname);
678                 return 0;
679         }
680         return 1;
681 }
682
683 extern int
684 dm_flush_maps (void)
685 {
686         int r = 0;
687         struct dm_task *dmt;
688         struct dm_names *names;
689         unsigned next = 0;
690
691         if (!(dmt = dm_task_create (DM_DEVICE_LIST)))
692                 return 0;
693
694         dm_task_no_open_count(dmt);
695
696         if (!dm_task_run (dmt))
697                 goto out;
698
699         if (!(names = dm_task_get_names (dmt)))
700                 goto out;
701
702         if (!names->dev)
703                 goto out;
704
705         do {
706                 r |= dm_flush_map(names->name);
707                 next = names->next;
708                 names = (void *) names + next;
709         } while (next);
710
711         out:
712         dm_task_destroy (dmt);
713         return r;
714 }
715
716 int
717 dm_message(char * mapname, char * message)
718 {
719         int r = 1;
720         struct dm_task *dmt;
721
722         if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
723                 return 1;
724
725         if (!dm_task_set_name(dmt, mapname))
726                 goto out;
727
728         if (!dm_task_set_sector(dmt, 0))
729                 goto out;
730
731         if (!dm_task_set_message(dmt, message))
732                 goto out;
733
734         dm_task_no_open_count(dmt);
735
736         if (!dm_task_run(dmt))
737                 goto out;
738
739         r = 0;
740 out:
741         if (r)
742                 condlog(0, "DM message failed [%s]", message);
743
744         dm_task_destroy(dmt);
745         return r;
746 }
747
748 int
749 dm_fail_path(char * mapname, char * path)
750 {
751         char message[32];
752
753         if (snprintf(message, 32, "fail_path %s\n", path) > 32)
754                 return 1;
755
756         return dm_message(mapname, message);
757 }
758
759 int
760 dm_reinstate_path(char * mapname, char * path)
761 {
762         char message[32];
763
764         if (snprintf(message, 32, "reinstate_path %s\n", path) > 32)
765                 return 1;
766
767         return dm_message(mapname, message);
768 }
769
770 int
771 dm_queue_if_no_path(char *mapname, int enable)
772 {
773         char *message;
774
775         if (enable)
776                 message = "queue_if_no_path\n";
777         else
778                 message = "fail_if_no_path\n";
779
780         return dm_message(mapname, message);
781 }
782
783 int
784 dm_set_pg_timeout(char *mapname, int timeout_val)
785 {
786         char message[24];
787
788         if (snprintf(message, 24, "set_pg_timeout %d", timeout_val) >= 24)
789                 return 1;
790         return dm_message(mapname, message);
791 }
792
793 static int
794 dm_groupmsg (char * msg, char * mapname, int index)
795 {
796         char message[32];
797
798         if (snprintf(message, 32, "%s_group %i\n", msg, index) > 32)
799                 return 1;
800
801         return dm_message(mapname, message);
802 }
803
804 int
805 dm_switchgroup(char * mapname, int index)
806 {
807         return dm_groupmsg("switch", mapname, index);
808 }
809
810 int
811 dm_enablegroup(char * mapname, int index)
812 {
813         return dm_groupmsg("enable", mapname, index);
814 }
815
816 int
817 dm_disablegroup(char * mapname, int index)
818 {
819         return dm_groupmsg("disable", mapname, index);
820 }
821
822 int
823 dm_get_maps (vector mp)
824 {
825         struct multipath * mpp;
826         int r = 1;
827         int info;
828         struct dm_task *dmt;
829         struct dm_names *names;
830         unsigned next = 0;
831
832         if (!mp)
833                 return 1;
834
835         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
836                 return 1;
837
838         dm_task_no_open_count(dmt);
839
840         if (!dm_task_run(dmt))
841                 goto out;
842
843         if (!(names = dm_task_get_names(dmt)))
844                 goto out;
845
846         if (!names->dev) {
847                 r = 0; /* this is perfectly valid */
848                 goto out;
849         }
850
851         do {
852                 info = dm_type(names->name, TGT_MPATH);
853
854                 if (info <= 0)
855                         goto next;
856
857                 mpp = alloc_multipath();
858
859                 if (!mpp)
860                         goto out;
861
862                 mpp->alias = STRDUP(names->name);
863
864                 if (!mpp->alias)
865                         goto out1;
866
867                 if (info > 0) {
868                         if (dm_get_map(names->name, &mpp->size, NULL))
869                                 goto out1;
870
871                         dm_get_uuid(names->name, mpp->wwid);
872                         dm_get_info(names->name, &mpp->dmi);
873                 }
874
875                 if (!vector_alloc_slot(mp))
876                         goto out1;
877
878                 vector_set_slot(mp, mpp);
879                 mpp = NULL;
880 next:
881                 next = names->next;
882                 names = (void *) names + next;
883         } while (next);
884
885         r = 0;
886         goto out;
887 out1:
888         free_multipath(mpp, KEEP_PATHS);
889 out:
890         dm_task_destroy (dmt);
891         return r;
892 }
893
894 extern char *
895 dm_get_name(char *uuid)
896 {
897         struct dm_task *dmt;
898         struct dm_info info;
899         char *prefixed_uuid, *name = NULL;
900         const char *nametmp;
901
902         dmt = dm_task_create(DM_DEVICE_INFO);
903         if (!dmt)
904                 return NULL;
905
906         prefixed_uuid = MALLOC(UUID_PREFIX_LEN + strlen(uuid) + 1);
907         if (!prefixed_uuid) {
908                 condlog(0, "cannot create prefixed uuid : %s\n",
909                         strerror(errno));
910                 goto freeout;
911         }
912         sprintf(prefixed_uuid, UUID_PREFIX "%s", uuid);
913         if (!dm_task_set_uuid(dmt, prefixed_uuid))
914                 goto freeout;
915
916         if (!dm_task_run(dmt))
917                 goto freeout;
918
919         if (!dm_task_get_info(dmt, &info) || !info.exists)
920                 goto freeout;
921
922         nametmp = dm_task_get_name(dmt);
923         if (nametmp && strlen(nametmp)) {
924                 name = MALLOC(strlen(nametmp) + 1);
925                 if (name)
926                         strcpy(name, nametmp);
927         } else {
928                 condlog(2, "%s: no device-mapper name found", uuid);
929         }
930
931 freeout:
932         if (prefixed_uuid)
933                 FREE(prefixed_uuid);
934         dm_task_destroy(dmt);
935
936         return name;
937 }
938
939 int
940 dm_geteventnr (char *name)
941 {
942         struct dm_task *dmt;
943         struct dm_info info;
944
945         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
946                 return 0;
947
948         if (!dm_task_set_name(dmt, name))
949                 goto out;
950
951         dm_task_no_open_count(dmt);
952
953         if (!dm_task_run(dmt))
954                 goto out;
955
956         if (!dm_task_get_info(dmt, &info)) {
957                 info.event_nr = 0;
958                 goto out;
959         }
960
961         if (!info.exists) {
962                 info.event_nr = 0;
963                 goto out;
964         }
965
966 out:
967         dm_task_destroy(dmt);
968
969         return info.event_nr;
970 }
971
972 char *
973 dm_mapname(int major, int minor)
974 {
975         char * response = NULL;
976         const char *map;
977         struct dm_task *dmt;
978         int r;
979         int loop = MAX_WAIT * LOOPS_PER_SEC;
980
981         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
982                 return NULL;
983
984         if (!dm_task_set_major(dmt, major) ||
985             !dm_task_set_minor(dmt, minor))
986                 goto bad;
987
988         dm_task_no_open_count(dmt);
989
990         /*
991          * device map might not be ready when we get here from
992          * daemon uev_trigger -> uev_add_map
993          */
994         while (--loop) {
995                 r = dm_task_run(dmt);
996
997                 if (r)
998                         break;
999
1000                 usleep(1000 * 1000 / LOOPS_PER_SEC);
1001         }
1002
1003         if (!r) {
1004                 condlog(0, "%i:%i: timeout fetching map name", major, minor);
1005                 goto bad;
1006         }
1007
1008         map = dm_task_get_name(dmt);
1009         if (map && strlen(map))
1010                 response = STRDUP((char *)dm_task_get_name(dmt));
1011
1012         dm_task_destroy(dmt);
1013         return response;
1014 bad:
1015         dm_task_destroy(dmt);
1016         condlog(0, "%i:%i: error fetching map name", major, minor);
1017         return NULL;
1018 }
1019
1020 int
1021 dm_remove_partmaps (const char * mapname, int need_sync)
1022 {
1023         struct dm_task *dmt;
1024         struct dm_names *names;
1025         unsigned next = 0;
1026         char params[PARAMS_SIZE];
1027         unsigned long long size;
1028         char dev_t[32];
1029         int r = 1;
1030
1031         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
1032                 return 1;
1033
1034         dm_task_no_open_count(dmt);
1035
1036         if (!dm_task_run(dmt))
1037                 goto out;
1038
1039         if (!(names = dm_task_get_names(dmt)))
1040                 goto out;
1041
1042         if (!names->dev) {
1043                 r = 0; /* this is perfectly valid */
1044                 goto out;
1045         }
1046
1047         if (dm_dev_t(mapname, &dev_t[0], 32))
1048                 goto out;
1049
1050         do {
1051                 if (
1052                     /*
1053                      * if devmap target is "linear"
1054                      */
1055                     (dm_type(names->name, TGT_PART) > 0) &&
1056
1057                     /*
1058                      * and both uuid end with same suffix starting
1059                      * at UUID_PREFIX
1060                      */
1061                     (!dm_compare_uuid(names->name, mapname)) &&
1062
1063                     /*
1064                      * and we can fetch the map table from the kernel
1065                      */
1066                     !dm_get_map(names->name, &size, &params[0]) &&
1067
1068                     /*
1069                      * and the table maps over the multipath map
1070                      */
1071                     strstr(params, dev_t)
1072                    ) {
1073                         /*
1074                          * then it's a kpartx generated partition.
1075                          * remove it.
1076                          */
1077                         /*
1078                          * if the opencount is 0 maybe some other
1079                          * partitions depend on it.
1080                          */
1081                         if (dm_get_opencount(names->name)) {
1082                                 dm_remove_partmaps(names->name, need_sync);
1083                                 if (dm_get_opencount(names->name)) {
1084                                         condlog(2, "%s: map in use",
1085                                                 names->name);
1086                                         goto out;
1087                                 }
1088                         }
1089                         condlog(4, "partition map %s removed",
1090                                 names->name);
1091                         dm_simplecmd_flush(DM_DEVICE_REMOVE, names->name,
1092                                            need_sync);
1093                 }
1094
1095                 next = names->next;
1096                 names = (void *) names + next;
1097         } while (next);
1098
1099         r = 0;
1100 out:
1101         dm_task_destroy (dmt);
1102         return r;
1103 }
1104
1105 static struct dm_info *
1106 alloc_dminfo (void)
1107 {
1108         return MALLOC(sizeof(struct dm_info));
1109 }
1110
1111 int
1112 dm_get_info (char * mapname, struct dm_info ** dmi)
1113 {
1114         int r = 1;
1115         struct dm_task *dmt = NULL;
1116
1117         if (!mapname)
1118                 return 1;
1119
1120         if (!*dmi)
1121                 *dmi = alloc_dminfo();
1122
1123         if (!*dmi)
1124                 return 1;
1125
1126         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
1127                 goto out;
1128
1129         if (!dm_task_set_name(dmt, mapname))
1130                 goto out;
1131
1132         dm_task_no_open_count(dmt);
1133
1134         if (!dm_task_run(dmt))
1135                 goto out;
1136
1137         if (!dm_task_get_info(dmt, *dmi))
1138                 goto out;
1139
1140         r = 0;
1141 out:
1142         if (r) {
1143                 memset(*dmi, 0, sizeof(struct dm_info));
1144                 FREE(*dmi);
1145                 *dmi = NULL;
1146         }
1147
1148         if (dmt)
1149                 dm_task_destroy(dmt);
1150
1151         return r;
1152 }
1153
1154 int
1155 dm_rename_partmaps (char * old, char * new)
1156 {
1157         struct dm_task *dmt;
1158         struct dm_names *names;
1159         unsigned next = 0;
1160         char buff[PARAMS_SIZE];
1161         unsigned long long size;
1162         char dev_t[32];
1163         int r = 1;
1164
1165         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
1166                 return 1;
1167
1168         dm_task_no_open_count(dmt);
1169
1170         if (!dm_task_run(dmt))
1171                 goto out;
1172
1173         if (!(names = dm_task_get_names(dmt)))
1174                 goto out;
1175
1176         if (!names->dev) {
1177                 r = 0; /* this is perfectly valid */
1178                 goto out;
1179         }
1180
1181         if (dm_dev_t(old, &dev_t[0], 32))
1182                 goto out;
1183
1184         do {
1185                 if (
1186                     /*
1187                      * if devmap target is "linear"
1188                      */
1189                     (dm_type(names->name, TGT_PART) > 0) &&
1190
1191                     /*
1192                      * and the multipath mapname and the part mapname start
1193                      * the same
1194                      */
1195                     !strncmp(names->name, old, strlen(old)) &&
1196
1197                     /*
1198                      * and we can fetch the map table from the kernel
1199                      */
1200                     !dm_get_map(names->name, &size, &buff[0]) &&
1201
1202                     /*
1203                      * and the table maps over the multipath map
1204                      */
1205                     strstr(buff, dev_t)
1206                    ) {
1207                                 /*
1208                                  * then it's a kpartx generated partition.
1209                                  * Rename it.
1210                                  */
1211                                 snprintf(buff, PARAMS_SIZE, "%s%s",
1212                                          new, names->name + strlen(old));
1213                                 dm_rename(names->name, buff);
1214                                 condlog(4, "partition map %s renamed",
1215                                         names->name);
1216                    }
1217
1218                 next = names->next;
1219                 names = (void *) names + next;
1220         } while (next);
1221
1222         r = 0;
1223 out:
1224         dm_task_destroy (dmt);
1225         return r;
1226 }
1227
1228 int
1229 dm_rename (char * old, char * new)
1230 {
1231         int r = 0;
1232         struct dm_task *dmt;
1233
1234         if (dm_rename_partmaps(old, new))
1235                 return r;
1236
1237         if (!(dmt = dm_task_create(DM_DEVICE_RENAME)))
1238                 return r;
1239
1240         if (!dm_task_set_name(dmt, old))
1241                 goto out;
1242
1243         if (!dm_task_set_newname(dmt, new))
1244                 goto out;
1245
1246         dm_task_no_open_count(dmt);
1247
1248         if (!dm_task_set_cookie(dmt, &conf->cookie, 0))
1249                 goto out;
1250         if (!dm_task_run(dmt))
1251                 goto out;
1252
1253         r = 1;
1254 out:
1255         dm_task_destroy(dmt);
1256         return r;
1257 }
1258
1259 int dm_setgeometry(struct multipath *mpp)
1260 {
1261         struct dm_task *dmt;
1262         struct path *pp;
1263         char heads[4], sectors[4];
1264         char cylinders[10], start[32];
1265         int r = 0;
1266
1267         if (!mpp)
1268                 return 1;
1269
1270         pp = first_path(mpp);
1271         if (!pp) {
1272                 condlog(3, "%s: no path for geometry", mpp->alias);
1273                 return 1;
1274         }
1275         if (pp->geom.cylinders == 0 ||
1276             pp->geom.heads == 0 ||
1277             pp->geom.sectors == 0) {
1278                 condlog(3, "%s: invalid geometry on %s", mpp->alias, pp->dev);
1279                 return 1;
1280         }
1281
1282         if (!(dmt = dm_task_create(DM_DEVICE_SET_GEOMETRY)))
1283                 return 0;
1284
1285         if (!dm_task_set_name(dmt, mpp->alias))
1286                 goto out;
1287
1288         dm_task_no_open_count(dmt);
1289
1290         /* What a sick interface ... */
1291         snprintf(heads, 4, "%u", pp->geom.heads);
1292         snprintf(sectors, 4, "%u", pp->geom.sectors);
1293         snprintf(cylinders, 10, "%u", pp->geom.cylinders);
1294         snprintf(start, 32, "%lu", pp->geom.start);
1295         if (!dm_task_set_geometry(dmt, cylinders, heads, sectors, start)) {
1296                 condlog(3, "%s: Failed to set geometry", mpp->alias);
1297                 goto out;
1298         }
1299
1300         r = dm_task_run(dmt);
1301 out:
1302         dm_task_destroy(dmt);
1303
1304         return r;
1305 }