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