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