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