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