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