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