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