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