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