9c0b2405d030bb7e860e0802241c53f894722a9e
[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;
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
379         /*
380          * DM_DEVICE_RELOAD cannot wait on a cookie, as
381          * the cookie will only ever be released after an
382          * DM_DEVICE_RESUME. So call DM_DEVICE_RESUME
383          * after each successful call to DM_DEVICE_RELOAD.
384          */
385         r = dm_addmap(DM_DEVICE_RELOAD, TGT_MPATH, mpp, params, ADDMAP_RW,
386                       SKIP_KPARTX_OFF);
387         if (!r) {
388                 if (errno != EROFS)
389                         return 0;
390                 r = dm_addmap(DM_DEVICE_RELOAD, TGT_MPATH, mpp,
391                               params, ADDMAP_RO, SKIP_KPARTX_OFF);
392         }
393         if (r)
394                 r = dm_simplecmd(DM_DEVICE_RESUME, mpp->alias, flush,
395                                  1, udev_flags, 0);
396         return r;
397 }
398
399 int dm_map_present(const char * str)
400 {
401         int r = 0;
402         struct dm_task *dmt;
403         struct dm_info info;
404
405         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
406                 return 0;
407
408         if (!dm_task_set_name(dmt, str))
409                 goto out;
410
411         dm_task_no_open_count(dmt);
412
413         if (!dm_task_run(dmt))
414                 goto out;
415
416         if (!dm_task_get_info(dmt, &info))
417                 goto out;
418
419         if (info.exists)
420                 r = 1;
421 out:
422         dm_task_destroy(dmt);
423         return r;
424 }
425
426 int dm_get_map(const char *name, unsigned long long *size, char *outparams)
427 {
428         int r = 1;
429         struct dm_task *dmt;
430         uint64_t start, length;
431         char *target_type = NULL;
432         char *params = NULL;
433
434         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
435                 return 1;
436
437         if (!dm_task_set_name(dmt, name))
438                 goto out;
439
440         dm_task_no_open_count(dmt);
441
442         if (!dm_task_run(dmt))
443                 goto out;
444
445         /* Fetch 1st target */
446         dm_get_next_target(dmt, NULL, &start, &length,
447                            &target_type, &params);
448
449         if (size)
450                 *size = length;
451
452         if (!outparams) {
453                 r = 0;
454                 goto out;
455         }
456         if (snprintf(outparams, PARAMS_SIZE, "%s", params) <= PARAMS_SIZE)
457                 r = 0;
458 out:
459         dm_task_destroy(dmt);
460         return r;
461 }
462
463 static int
464 dm_get_prefixed_uuid(const char *name, char *uuid)
465 {
466         struct dm_task *dmt;
467         const char *uuidtmp;
468         int r = 1;
469
470         dmt = dm_task_create(DM_DEVICE_INFO);
471         if (!dmt)
472                 return 1;
473
474         if (!dm_task_set_name (dmt, name))
475                 goto uuidout;
476
477         if (!dm_task_run(dmt))
478                 goto uuidout;
479
480         uuidtmp = dm_task_get_uuid(dmt);
481         if (uuidtmp)
482                 strcpy(uuid, uuidtmp);
483         else
484                 uuid[0] = '\0';
485
486         r = 0;
487 uuidout:
488         dm_task_destroy(dmt);
489         return r;
490 }
491
492 int dm_get_uuid(char *name, char *uuid)
493 {
494         char uuidtmp[WWID_SIZE];
495
496         if (dm_get_prefixed_uuid(name, uuidtmp))
497                 return 1;
498
499         if (!strncmp(uuidtmp, UUID_PREFIX, UUID_PREFIX_LEN))
500                 strcpy(uuid, uuidtmp + UUID_PREFIX_LEN);
501         else
502                 strcpy(uuid, uuidtmp);
503
504         return 0;
505 }
506
507 /*
508  * returns:
509  *    0 : if both uuids end with same suffix which starts with UUID_PREFIX
510  *    1 : otherwise
511  */
512 int
513 dm_compare_uuid(const char* mapname1, const char* mapname2)
514 {
515         char *p1, *p2;
516         char uuid1[WWID_SIZE], uuid2[WWID_SIZE];
517
518         if (dm_get_prefixed_uuid(mapname1, uuid1))
519                 return 1;
520
521         if (dm_get_prefixed_uuid(mapname2, uuid2))
522                 return 1;
523
524         p1 = strstr(uuid1, UUID_PREFIX);
525         p2 = strstr(uuid2, UUID_PREFIX);
526         if (p1 && p2 && !strcmp(p1, p2))
527                 return 0;
528
529         return 1;
530 }
531
532 int dm_get_status(char *name, char *outstatus)
533 {
534         int r = 1;
535         struct dm_task *dmt;
536         uint64_t start, length;
537         char *target_type = NULL;
538         char *status = NULL;
539
540         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
541                 return 1;
542
543         if (!dm_task_set_name(dmt, name))
544                 goto out;
545
546         dm_task_no_open_count(dmt);
547
548         if (!dm_task_run(dmt))
549                 goto out;
550
551         /* Fetch 1st target */
552         dm_get_next_target(dmt, NULL, &start, &length,
553                            &target_type, &status);
554         if (!status) {
555                 condlog(2, "get null status.");
556                 goto out;
557         }
558
559         if (snprintf(outstatus, PARAMS_SIZE, "%s", status) <= PARAMS_SIZE)
560                 r = 0;
561 out:
562         if (r)
563                 condlog(0, "%s: error getting map status string", name);
564
565         dm_task_destroy(dmt);
566         return r;
567 }
568
569 /*
570  * returns:
571  *    1 : match
572  *    0 : no match
573  *   -1 : empty map
574  */
575 int dm_type(const char *name, char *type)
576 {
577         int r = 0;
578         struct dm_task *dmt;
579         uint64_t start, length;
580         char *target_type = NULL;
581         char *params;
582
583         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
584                 return 0;
585
586         if (!dm_task_set_name(dmt, name))
587                 goto out;
588
589         dm_task_no_open_count(dmt);
590
591         if (!dm_task_run(dmt))
592                 goto out;
593
594         /* Fetch 1st target */
595         dm_get_next_target(dmt, NULL, &start, &length,
596                            &target_type, &params);
597
598         if (!target_type)
599                 r = -1;
600         else if (!strcmp(target_type, type))
601                 r = 1;
602
603 out:
604         dm_task_destroy(dmt);
605         return r;
606 }
607
608 int dm_is_mpath(const char *name)
609 {
610         int r = 0;
611         struct dm_task *dmt;
612         struct dm_info info;
613         uint64_t start, length;
614         char *target_type = NULL;
615         char *params;
616         const char *uuid;
617
618         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
619                 return 0;
620
621         if (!dm_task_set_name(dmt, name))
622                 goto out;
623
624         dm_task_no_open_count(dmt);
625
626         if (!dm_task_run(dmt))
627                 goto out;
628
629         if (!dm_task_get_info(dmt, &info) || !info.exists)
630                 goto out;
631
632         uuid = dm_task_get_uuid(dmt);
633
634         if (!uuid || strncmp(uuid, UUID_PREFIX, UUID_PREFIX_LEN) != 0)
635                 goto out;
636
637         /* Fetch 1st target */
638         dm_get_next_target(dmt, NULL, &start, &length, &target_type, &params);
639
640         if (!target_type || strcmp(target_type, TGT_MPATH) != 0)
641                 goto out;
642
643         r = 1;
644 out:
645         dm_task_destroy(dmt);
646         return r;
647 }
648
649 static int
650 dm_dev_t (const char * mapname, char * dev_t, int len)
651 {
652         int r = 1;
653         struct dm_task *dmt;
654         struct dm_info info;
655
656         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
657                 return 0;
658
659         if (!dm_task_set_name(dmt, mapname))
660                 goto out;
661
662         if (!dm_task_run(dmt))
663                 goto out;
664
665         if (!dm_task_get_info(dmt, &info) || !info.exists)
666                 goto out;
667
668         if (snprintf(dev_t, len, "%i:%i", info.major, info.minor) > len)
669                 goto out;
670
671         r = 0;
672 out:
673         dm_task_destroy(dmt);
674         return r;
675 }
676
677 int
678 dm_get_opencount (const char * mapname)
679 {
680         int r = -1;
681         struct dm_task *dmt;
682         struct dm_info info;
683
684         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
685                 return 0;
686
687         if (!dm_task_set_name(dmt, mapname))
688                 goto out;
689
690         if (!dm_task_run(dmt))
691                 goto out;
692
693         if (!dm_task_get_info(dmt, &info))
694                 goto out;
695
696         if (!info.exists)
697                 goto out;
698
699         r = info.open_count;
700 out:
701         dm_task_destroy(dmt);
702         return r;
703 }
704
705 int
706 dm_get_major (char * mapname)
707 {
708         int r = -1;
709         struct dm_task *dmt;
710         struct dm_info info;
711
712         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
713                 return 0;
714
715         if (!dm_task_set_name(dmt, mapname))
716                 goto out;
717
718         if (!dm_task_run(dmt))
719                 goto out;
720
721         if (!dm_task_get_info(dmt, &info))
722                 goto out;
723
724         if (!info.exists)
725                 goto out;
726
727         r = info.major;
728 out:
729         dm_task_destroy(dmt);
730         return r;
731 }
732
733 int
734 dm_get_minor (char * mapname)
735 {
736         int r = -1;
737         struct dm_task *dmt;
738         struct dm_info info;
739
740         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
741                 return 0;
742
743         if (!dm_task_set_name(dmt, mapname))
744                 goto out;
745
746         if (!dm_task_run(dmt))
747                 goto out;
748
749         if (!dm_task_get_info(dmt, &info))
750                 goto out;
751
752         if (!info.exists)
753                 goto out;
754
755         r = info.minor;
756 out:
757         dm_task_destroy(dmt);
758         return r;
759 }
760
761 static int
762 has_partmap(const char *name, void *data)
763 {
764         return 1;
765 }
766
767 static int
768 partmap_in_use(const char *name, void *data)
769 {
770         int part_count, *ret_count = (int *)data;
771         int open_count = dm_get_opencount(name);
772
773         if (ret_count)
774                 (*ret_count)++;
775         part_count = 0;
776         if (open_count) {
777                 if (do_foreach_partmaps(name, partmap_in_use, &part_count))
778                         return 1;
779                 if (open_count != part_count) {
780                         condlog(2, "%s: map in use", name);
781                         return 1;
782                 }
783         }
784         return 0;
785 }
786
787 int _dm_flush_map(const char *mapname, int need_sync, int deferred_remove)
788 {
789         int r;
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 (dm_remove_partmaps(mapname, need_sync, deferred_remove))
800                 return 1;
801
802         if (!do_deferred(deferred_remove) && dm_get_opencount(mapname)) {
803                 condlog(2, "%s: map in use", mapname);
804                 return 1;
805         }
806
807         r = dm_device_remove(mapname, need_sync, deferred_remove);
808
809         if (r) {
810                 if (do_deferred(deferred_remove) && dm_map_present(mapname)) {
811                         condlog(4, "multipath map %s remove deferred",
812                                 mapname);
813                         return 2;
814                 }
815                 condlog(4, "multipath map %s removed", mapname);
816                 return 0;
817         }
818         return 1;
819 }
820
821 #ifdef LIBDM_API_DEFERRED
822
823 int
824 dm_flush_map_nopaths(const char * mapname, int deferred_remove)
825 {
826         return _dm_flush_map(mapname, 1, deferred_remove);
827 }
828
829 #else
830
831 int
832 dm_flush_map_nopaths(const char * mapname, int deferred_remove)
833 {
834         return _dm_flush_map(mapname, 1, 0);
835 }
836
837 #endif
838
839 int dm_suspend_and_flush_map (const char * mapname, int retries)
840 {
841         int need_reset = 0, queue_if_no_path = 0;
842         unsigned long long mapsize;
843         char params[PARAMS_SIZE] = {0};
844         int udev_flags = 0;
845
846         if (!dm_is_mpath(mapname))
847                 return 0; /* nothing to do */
848
849         /* if the device currently has no partitions, do not
850            run kpartx on it if you fail to delete it */
851         if (do_foreach_partmaps(mapname, has_partmap, NULL) == 0)
852                 udev_flags |= MPATH_UDEV_NO_KPARTX_FLAG;
853
854         if (!dm_get_map(mapname, &mapsize, params)) {
855                 if (strstr(params, "queue_if_no_path"))
856                         queue_if_no_path = 1;
857         }
858
859         if (queue_if_no_path && dm_queue_if_no_path((char *)mapname, 0) == 0)
860                 need_reset = 1;
861
862         do {
863                 if (!queue_if_no_path || need_reset)
864                         dm_simplecmd_flush(DM_DEVICE_SUSPEND, mapname, 0);
865
866                 if (!dm_flush_map(mapname)) {
867                         condlog(4, "multipath map %s removed", mapname);
868                         return 0;
869                 }
870                 dm_simplecmd_noflush(DM_DEVICE_RESUME, mapname, udev_flags);
871                 if (retries)
872                         sleep(1);
873         } while (retries-- > 0);
874         condlog(2, "failed to remove multipath map %s", mapname);
875         if (need_reset)
876                 dm_queue_if_no_path((char *)mapname, 1);
877         return 1;
878 }
879
880 int dm_flush_maps (int retries)
881 {
882         int r = 0;
883         struct dm_task *dmt;
884         struct dm_names *names;
885         unsigned next = 0;
886
887         if (!(dmt = dm_task_create (DM_DEVICE_LIST)))
888                 return 0;
889
890         dm_task_no_open_count(dmt);
891
892         if (!dm_task_run (dmt))
893                 goto out;
894
895         if (!(names = dm_task_get_names (dmt)))
896                 goto out;
897
898         if (!names->dev)
899                 goto out;
900
901         do {
902                 r |= dm_suspend_and_flush_map(names->name, retries);
903                 next = names->next;
904                 names = (void *) names + next;
905         } while (next);
906
907 out:
908         dm_task_destroy (dmt);
909         return r;
910 }
911
912 int
913 dm_message(const char * mapname, char * message)
914 {
915         int r = 1;
916         struct dm_task *dmt;
917
918         if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
919                 return 1;
920
921         if (!dm_task_set_name(dmt, mapname))
922                 goto out;
923
924         if (!dm_task_set_sector(dmt, 0))
925                 goto out;
926
927         if (!dm_task_set_message(dmt, message))
928                 goto out;
929
930         dm_task_no_open_count(dmt);
931
932         if (!dm_task_run(dmt))
933                 goto out;
934
935         r = 0;
936 out:
937         if (r)
938                 condlog(0, "DM message failed [%s]", message);
939
940         dm_task_destroy(dmt);
941         return r;
942 }
943
944 int
945 dm_fail_path(char * mapname, char * path)
946 {
947         char message[32];
948
949         if (snprintf(message, 32, "fail_path %s", path) > 32)
950                 return 1;
951
952         return dm_message(mapname, message);
953 }
954
955 int
956 dm_reinstate_path(char * mapname, char * path)
957 {
958         char message[32];
959
960         if (snprintf(message, 32, "reinstate_path %s", path) > 32)
961                 return 1;
962
963         return dm_message(mapname, message);
964 }
965
966 int
967 dm_queue_if_no_path(char *mapname, int enable)
968 {
969         char *message;
970
971         if (enable)
972                 message = "queue_if_no_path";
973         else
974                 message = "fail_if_no_path";
975
976         return dm_message(mapname, message);
977 }
978
979 static int
980 dm_groupmsg (char * msg, char * mapname, int index)
981 {
982         char message[32];
983
984         if (snprintf(message, 32, "%s_group %i", msg, index) > 32)
985                 return 1;
986
987         return dm_message(mapname, message);
988 }
989
990 int
991 dm_switchgroup(char * mapname, int index)
992 {
993         return dm_groupmsg("switch", mapname, index);
994 }
995
996 int
997 dm_enablegroup(char * mapname, int index)
998 {
999         return dm_groupmsg("enable", mapname, index);
1000 }
1001
1002 int
1003 dm_disablegroup(char * mapname, int index)
1004 {
1005         return dm_groupmsg("disable", mapname, index);
1006 }
1007
1008 int
1009 dm_get_maps (vector mp)
1010 {
1011         struct multipath * mpp;
1012         int r = 1;
1013         struct dm_task *dmt;
1014         struct dm_names *names;
1015         unsigned next = 0;
1016
1017         if (!mp)
1018                 return 1;
1019
1020         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
1021                 return 1;
1022
1023         dm_task_no_open_count(dmt);
1024
1025         if (!dm_task_run(dmt))
1026                 goto out;
1027
1028         if (!(names = dm_task_get_names(dmt)))
1029                 goto out;
1030
1031         if (!names->dev) {
1032                 r = 0; /* this is perfectly valid */
1033                 goto out;
1034         }
1035
1036         do {
1037                 if (!dm_is_mpath(names->name))
1038                         goto next;
1039
1040                 mpp = alloc_multipath();
1041
1042                 if (!mpp)
1043                         goto out;
1044
1045                 mpp->alias = STRDUP(names->name);
1046
1047                 if (!mpp->alias)
1048                         goto out1;
1049
1050                 if (dm_get_map(names->name, &mpp->size, NULL))
1051                         goto out1;
1052
1053                 dm_get_uuid(names->name, mpp->wwid);
1054                 dm_get_info(names->name, &mpp->dmi);
1055
1056                 if (!vector_alloc_slot(mp))
1057                         goto out1;
1058
1059                 vector_set_slot(mp, mpp);
1060                 mpp = NULL;
1061 next:
1062                 next = names->next;
1063                 names = (void *) names + next;
1064         } while (next);
1065
1066         r = 0;
1067         goto out;
1068 out1:
1069         free_multipath(mpp, KEEP_PATHS);
1070 out:
1071         dm_task_destroy (dmt);
1072         return r;
1073 }
1074
1075 int
1076 dm_geteventnr (char *name)
1077 {
1078         struct dm_task *dmt;
1079         struct dm_info info;
1080         int event = -1;
1081
1082         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
1083                 return -1;
1084
1085         if (!dm_task_set_name(dmt, name))
1086                 goto out;
1087
1088         dm_task_no_open_count(dmt);
1089
1090         if (!dm_task_run(dmt))
1091                 goto out;
1092
1093         if (!dm_task_get_info(dmt, &info))
1094                 goto out;
1095
1096         if (info.exists)
1097                 event = info.event_nr;
1098
1099 out:
1100         dm_task_destroy(dmt);
1101
1102         return event;
1103 }
1104
1105 char *
1106 dm_mapname(int major, int minor)
1107 {
1108         char * response = NULL;
1109         const char *map;
1110         struct dm_task *dmt;
1111         int r;
1112         int loop = MAX_WAIT * LOOPS_PER_SEC;
1113
1114         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
1115                 return NULL;
1116
1117         if (!dm_task_set_major(dmt, major) ||
1118             !dm_task_set_minor(dmt, minor))
1119                 goto bad;
1120
1121         dm_task_no_open_count(dmt);
1122
1123         /*
1124          * device map might not be ready when we get here from
1125          * daemon uev_trigger -> uev_add_map
1126          */
1127         while (--loop) {
1128                 r = dm_task_run(dmt);
1129
1130                 if (r)
1131                         break;
1132
1133                 usleep(1000 * 1000 / LOOPS_PER_SEC);
1134         }
1135
1136         if (!r) {
1137                 condlog(0, "%i:%i: timeout fetching map name", major, minor);
1138                 goto bad;
1139         }
1140
1141         map = dm_task_get_name(dmt);
1142         if (map && strlen(map))
1143                 response = STRDUP((char *)dm_task_get_name(dmt));
1144
1145         dm_task_destroy(dmt);
1146         return response;
1147 bad:
1148         dm_task_destroy(dmt);
1149         condlog(0, "%i:%i: error fetching map name", major, minor);
1150         return NULL;
1151 }
1152
1153 static int
1154 do_foreach_partmaps (const char * mapname,
1155                      int (*partmap_func)(const char *, void *),
1156                      void *data)
1157 {
1158         struct dm_task *dmt;
1159         struct dm_names *names;
1160         unsigned next = 0;
1161         char params[PARAMS_SIZE];
1162         unsigned long long size;
1163         char dev_t[32];
1164         int r = 1;
1165
1166         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
1167                 return 1;
1168
1169         dm_task_no_open_count(dmt);
1170
1171         if (!dm_task_run(dmt))
1172                 goto out;
1173
1174         if (!(names = dm_task_get_names(dmt)))
1175                 goto out;
1176
1177         if (!names->dev) {
1178                 r = 0; /* this is perfectly valid */
1179                 goto out;
1180         }
1181
1182         if (dm_dev_t(mapname, &dev_t[0], 32))
1183                 goto out;
1184
1185         do {
1186                 if (
1187                     /*
1188                      * if devmap target is "linear"
1189                      */
1190                     (dm_type(names->name, TGT_PART) > 0) &&
1191
1192                     /*
1193                      * and both uuid end with same suffix starting
1194                      * at UUID_PREFIX
1195                      */
1196                     (!dm_compare_uuid(names->name, mapname)) &&
1197
1198                     /*
1199                      * and we can fetch the map table from the kernel
1200                      */
1201                     !dm_get_map(names->name, &size, &params[0]) &&
1202
1203                     /*
1204                      * and the table maps over the multipath map
1205                      */
1206                     strstr(params, dev_t)
1207                    ) {
1208                         if (partmap_func(names->name, data) != 0)
1209                                 goto out;
1210                 }
1211
1212                 next = names->next;
1213                 names = (void *) names + next;
1214         } while (next);
1215
1216         r = 0;
1217 out:
1218         dm_task_destroy (dmt);
1219         return r;
1220 }
1221
1222 struct remove_data {
1223         int need_sync;
1224         int deferred_remove;
1225 };
1226
1227 static int
1228 remove_partmap(const char *name, void *data)
1229 {
1230         struct remove_data *rd = (struct remove_data *)data;
1231
1232         if (dm_get_opencount(name)) {
1233                 dm_remove_partmaps(name, rd->need_sync, rd->deferred_remove);
1234                 if (!do_deferred(rd->deferred_remove) &&
1235                     dm_get_opencount(name)) {
1236                         condlog(2, "%s: map in use", name);
1237                         return 1;
1238                 }
1239         }
1240         condlog(4, "partition map %s removed", name);
1241         dm_device_remove(name, rd->need_sync, rd->deferred_remove);
1242         return 0;
1243 }
1244
1245 int
1246 dm_remove_partmaps (const char * mapname, int need_sync, int deferred_remove)
1247 {
1248         struct remove_data rd = { need_sync, deferred_remove };
1249         return do_foreach_partmaps(mapname, remove_partmap, &rd);
1250 }
1251
1252 #ifdef LIBDM_API_DEFERRED
1253
1254 static int
1255 cancel_remove_partmap (const char *name, void *unused)
1256 {
1257         if (dm_get_opencount(name))
1258                 dm_cancel_remove_partmaps(name);
1259         if (dm_message(name, "@cancel_deferred_remove") != 0)
1260                 condlog(0, "%s: can't cancel deferred remove: %s", name,
1261                         strerror(errno));
1262         return 0;
1263 }
1264
1265 static int
1266 dm_get_deferred_remove (char * mapname)
1267 {
1268         int r = -1;
1269         struct dm_task *dmt;
1270         struct dm_info info;
1271
1272         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
1273                 return -1;
1274
1275         if (!dm_task_set_name(dmt, mapname))
1276                 goto out;
1277
1278         if (!dm_task_run(dmt))
1279                 goto out;
1280
1281         if (!dm_task_get_info(dmt, &info))
1282                 goto out;
1283
1284         r = info.deferred_remove;
1285 out:
1286         dm_task_destroy(dmt);
1287         return r;
1288 }
1289
1290 static int
1291 dm_cancel_remove_partmaps(const char * mapname) {
1292         return do_foreach_partmaps(mapname, cancel_remove_partmap, NULL);
1293 }
1294
1295 int
1296 dm_cancel_deferred_remove (struct multipath *mpp)
1297 {
1298         int r = 0;
1299
1300         if (!dm_get_deferred_remove(mpp->alias))
1301                 return 0;
1302         if (mpp->deferred_remove == DEFERRED_REMOVE_IN_PROGRESS)
1303                 mpp->deferred_remove = DEFERRED_REMOVE_ON;
1304
1305         dm_cancel_remove_partmaps(mpp->alias);
1306         r = dm_message(mpp->alias, "@cancel_deferred_remove");
1307         if (r)
1308                 condlog(0, "%s: can't cancel deferred remove: %s", mpp->alias,
1309                                 strerror(errno));
1310         else
1311                 condlog(2, "%s: canceled deferred remove", mpp->alias);
1312         return r;
1313 }
1314
1315 #else
1316
1317 int
1318 dm_cancel_deferred_remove (struct multipath *mpp)
1319 {
1320         return 0;
1321 }
1322
1323 #endif
1324
1325 static struct dm_info *
1326 alloc_dminfo (void)
1327 {
1328         return MALLOC(sizeof(struct dm_info));
1329 }
1330
1331 int
1332 dm_get_info (char * mapname, struct dm_info ** dmi)
1333 {
1334         int r = 1;
1335         struct dm_task *dmt = NULL;
1336
1337         if (!mapname)
1338                 return 1;
1339
1340         if (!*dmi)
1341                 *dmi = alloc_dminfo();
1342
1343         if (!*dmi)
1344                 return 1;
1345
1346         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
1347                 goto out;
1348
1349         if (!dm_task_set_name(dmt, mapname))
1350                 goto out;
1351
1352         dm_task_no_open_count(dmt);
1353
1354         if (!dm_task_run(dmt))
1355                 goto out;
1356
1357         if (!dm_task_get_info(dmt, *dmi))
1358                 goto out;
1359
1360         r = 0;
1361 out:
1362         if (r) {
1363                 memset(*dmi, 0, sizeof(struct dm_info));
1364                 FREE(*dmi);
1365                 *dmi = NULL;
1366         }
1367
1368         if (dmt)
1369                 dm_task_destroy(dmt);
1370
1371         return r;
1372 }
1373
1374 struct rename_data {
1375         const char *old;
1376         char *new;
1377         char *delim;
1378 };
1379
1380 static int
1381 rename_partmap (const char *name, void *data)
1382 {
1383         char buff[PARAMS_SIZE];
1384         int offset;
1385         struct rename_data *rd = (struct rename_data *)data;
1386
1387         if (strncmp(name, rd->old, strlen(rd->old)) != 0)
1388                 return 0;
1389         for (offset = strlen(rd->old); name[offset] && !(isdigit(name[offset])); offset++); /* do nothing */
1390         snprintf(buff, PARAMS_SIZE, "%s%s%s", rd->new, rd->delim,
1391                  name + offset);
1392         dm_rename(name, buff, rd->delim, SKIP_KPARTX_OFF);
1393         condlog(4, "partition map %s renamed", name);
1394         return 0;
1395 }
1396
1397 int
1398 dm_rename_partmaps (const char * old, char * new, char *delim)
1399 {
1400         struct rename_data rd;
1401
1402         rd.old = old;
1403         rd.new = new;
1404
1405         if (delim)
1406                 rd.delim = delim;
1407         if (isdigit(new[strlen(new)-1]))
1408                 rd.delim = "p";
1409         else
1410                 rd.delim = "";
1411         return do_foreach_partmaps(old, rename_partmap, &rd);
1412 }
1413
1414 int
1415 dm_rename (const char * old, char * new, char *delim, int skip_kpartx)
1416 {
1417         int r = 0;
1418         struct dm_task *dmt;
1419         uint32_t cookie = 0;
1420         uint16_t udev_flags = DM_UDEV_DISABLE_LIBRARY_FALLBACK | ((skip_kpartx == SKIP_KPARTX_ON)? MPATH_UDEV_NO_KPARTX_FLAG : 0);
1421
1422         if (dm_rename_partmaps(old, new, delim))
1423                 return r;
1424
1425         if (!(dmt = dm_task_create(DM_DEVICE_RENAME)))
1426                 return r;
1427
1428         if (!dm_task_set_name(dmt, old))
1429                 goto out;
1430
1431         if (!dm_task_set_newname(dmt, new))
1432                 goto out;
1433
1434         dm_task_no_open_count(dmt);
1435
1436         if (!dm_task_set_cookie(dmt, &cookie, udev_flags))
1437                 goto out;
1438         r = dm_task_run(dmt);
1439
1440         dm_udev_wait(cookie);
1441
1442 out:
1443         dm_task_destroy(dmt);
1444
1445         return r;
1446 }
1447
1448 void dm_reassign_deps(char *table, char *dep, char *newdep)
1449 {
1450         char *p, *n;
1451         char *newtable;
1452
1453         newtable = strdup(table);
1454         if (!newtable)
1455                 return;
1456         p = strstr(newtable, dep);
1457         n = table + (p - newtable);
1458         strcpy(n, newdep);
1459         n += strlen(newdep);
1460         p += strlen(dep);
1461         strcat(n, p);
1462         free(newtable);
1463 }
1464
1465 int dm_reassign_table(const char *name, char *old, char *new)
1466 {
1467         int r = 0, modified = 0;
1468         uint64_t start, length;
1469         struct dm_task *dmt, *reload_dmt;
1470         char *target, *params = NULL;
1471         char *buff;
1472         void *next = NULL;
1473
1474         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
1475                 return 0;
1476
1477         if (!dm_task_set_name(dmt, name))
1478                 goto out;
1479
1480         dm_task_no_open_count(dmt);
1481
1482         if (!dm_task_run(dmt))
1483                 goto out;
1484         if (!(reload_dmt = dm_task_create(DM_DEVICE_RELOAD)))
1485                 goto out;
1486         if (!dm_task_set_name(reload_dmt, name))
1487                 goto out_reload;
1488
1489         do {
1490                 next = dm_get_next_target(dmt, next, &start, &length,
1491                                           &target, &params);
1492                 buff = strdup(params);
1493                 if (!buff) {
1494                         condlog(3, "%s: failed to replace target %s, "
1495                                 "out of memory", name, target);
1496                         goto out_reload;
1497                 }
1498                 if (strcmp(target, TGT_MPATH) && strstr(params, old)) {
1499                         condlog(3, "%s: replace target %s %s",
1500                                 name, target, buff);
1501                         dm_reassign_deps(buff, old, new);
1502                         condlog(3, "%s: with target %s %s",
1503                                 name, target, buff);
1504                         modified++;
1505                 }
1506                 dm_task_add_target(reload_dmt, start, length, target, buff);
1507                 free(buff);
1508         } while (next);
1509
1510         if (modified) {
1511                 dm_task_no_open_count(reload_dmt);
1512
1513                 if (!dm_task_run(reload_dmt)) {
1514                         condlog(3, "%s: failed to reassign targets", name);
1515                         goto out_reload;
1516                 }
1517                 dm_simplecmd_noflush(DM_DEVICE_RESUME, name,
1518                                      MPATH_UDEV_RELOAD_FLAG);
1519         }
1520         r = 1;
1521
1522 out_reload:
1523         dm_task_destroy(reload_dmt);
1524 out:
1525         dm_task_destroy(dmt);
1526         return r;
1527 }
1528
1529
1530 /*
1531  * Reassign existing device-mapper table(s) to not use
1532  * the block devices but point to the multipathed
1533  * device instead
1534  */
1535 int dm_reassign(const char *mapname)
1536 {
1537         struct dm_deps *deps;
1538         struct dm_task *dmt;
1539         struct dm_info info;
1540         char dev_t[32], dm_dep[32];
1541         int r = 0, i;
1542
1543         if (dm_dev_t(mapname, &dev_t[0], 32)) {
1544                 condlog(3, "%s: failed to get device number", mapname);
1545                 return 1;
1546         }
1547
1548         if (!(dmt = dm_task_create(DM_DEVICE_DEPS))) {
1549                 condlog(3, "%s: couldn't make dm task", mapname);
1550                 return 0;
1551         }
1552
1553         if (!dm_task_set_name(dmt, mapname))
1554                 goto out;
1555
1556         dm_task_no_open_count(dmt);
1557
1558         if (!dm_task_run(dmt))
1559                 goto out;
1560
1561         if (!dm_task_get_info(dmt, &info))
1562                 goto out;
1563
1564         if (!(deps = dm_task_get_deps(dmt)))
1565                 goto out;
1566
1567         if (!info.exists)
1568                 goto out;
1569
1570         for (i = 0; i < deps->count; i++) {
1571                 sprintf(dm_dep, "%d:%d",
1572                         major(deps->device[i]),
1573                         minor(deps->device[i]));
1574                 sysfs_check_holders(dm_dep, dev_t);
1575         }
1576
1577         r = 1;
1578 out:
1579         dm_task_destroy (dmt);
1580         return r;
1581 }
1582
1583 int dm_setgeometry(struct multipath *mpp)
1584 {
1585         struct dm_task *dmt;
1586         struct path *pp;
1587         char heads[4], sectors[4];
1588         char cylinders[10], start[32];
1589         int r = 0;
1590
1591         if (!mpp)
1592                 return 1;
1593
1594         pp = first_path(mpp);
1595         if (!pp) {
1596                 condlog(3, "%s: no path for geometry", mpp->alias);
1597                 return 1;
1598         }
1599         if (pp->geom.cylinders == 0 ||
1600             pp->geom.heads == 0 ||
1601             pp->geom.sectors == 0) {
1602                 condlog(3, "%s: invalid geometry on %s", mpp->alias, pp->dev);
1603                 return 1;
1604         }
1605
1606         if (!(dmt = dm_task_create(DM_DEVICE_SET_GEOMETRY)))
1607                 return 0;
1608
1609         if (!dm_task_set_name(dmt, mpp->alias))
1610                 goto out;
1611
1612         dm_task_no_open_count(dmt);
1613
1614         /* What a sick interface ... */
1615         snprintf(heads, 4, "%u", pp->geom.heads);
1616         snprintf(sectors, 4, "%u", pp->geom.sectors);
1617         snprintf(cylinders, 10, "%u", pp->geom.cylinders);
1618         snprintf(start, 32, "%lu", pp->geom.start);
1619         if (!dm_task_set_geometry(dmt, cylinders, heads, sectors, start)) {
1620                 condlog(3, "%s: Failed to set geometry", mpp->alias);
1621                 goto out;
1622         }
1623
1624         r = dm_task_run(dmt);
1625 out:
1626         dm_task_destroy(dmt);
1627
1628         return r;
1629 }