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