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