Miscellaneous multipath bugfixes
[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 <linux/kdev_t.h>
14 #include <unistd.h>
15 #include <errno.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 "config.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 void
36 dm_write_log (int level, const char *file, int line, const char *f, ...)
37 {
38         va_list ap;
39         int thres;
40
41         if (level > 6)
42                 level = 6;
43
44         thres = (conf) ? conf->verbosity : 0;
45         if (thres <= 3 || level > thres)
46                 return;
47
48         va_start(ap, f);
49         if (!logsink) {
50                 time_t t = time(NULL);
51                 struct tm *tb = localtime(&t);
52                 char buff[16];
53
54                 strftime(buff, sizeof(buff), "%b %d %H:%M:%S", tb);
55                 buff[sizeof(buff)-1] = '\0';
56
57                 fprintf(stdout, "%s | ", buff);
58                 fprintf(stdout, "libdevmapper: %s(%i): ", file, line);
59                 vfprintf(stdout, f, ap);
60                 fprintf(stdout, "\n");
61         } else {
62                 condlog(level, "libdevmapper: %s(%i): ", file, line);
63                 log_safe(level + 3, f, ap);
64         }
65         va_end(ap);
66
67         return;
68 }
69
70 extern void
71 dm_init(void) {
72         dm_log_init(&dm_write_log);
73         dm_log_init_verbose(conf ? conf->verbosity + 3 : 0);
74 }
75
76 static int
77 dm_libprereq (void)
78 {
79         char version[64];
80         int v[3];
81         int minv[3] = {1, 2, 8};
82
83         dm_get_library_version(version, sizeof(version));
84         condlog(3, "libdevmapper version %s", version);
85         sscanf(version, "%d.%d.%d ", &v[0], &v[1], &v[2]);
86
87         if ((v[0] > minv[0]) ||
88             ((v[0] ==  minv[0]) && (v[1] > minv[1])) ||
89             ((v[0] == minv[0]) && (v[1] == minv[1]) && (v[2] >= minv[2])))
90                 return 0;
91         condlog(0, "libdevmapper version must be >= %d.%.2d.%.2d",
92                 minv[0], minv[1], minv[2]);
93         return 1;
94 }
95
96 static int
97 dm_drvprereq (char * str)
98 {
99         int r = 2;
100         struct dm_task *dmt;
101         struct dm_versions *target;
102         struct dm_versions *last_target;
103         int minv[3] = {1, 0, 3};
104         unsigned int *v;
105
106         if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
107                 return 3;
108
109         dm_task_no_open_count(dmt);
110
111         if (!dm_task_run(dmt)) {
112                 condlog(0, "Can not communicate with kernel DM");
113                 goto out;
114         }
115         target = dm_task_get_versions(dmt);
116
117         do {
118                 last_target = target;
119                 if (!strncmp(str, target->name, strlen(str))) {
120                         r = 1;
121                         break;
122                 }
123                 target = (void *) target + target->next;
124         } while (last_target != target);
125
126         if (r == 2) {
127                 condlog(0, "DM multipath kernel driver not loaded");
128                 goto out;
129         }
130         v = target->version;
131         if ((v[0] > minv[0]) ||
132             ((v[0] == minv[0]) && (v[1] > minv[1])) ||
133             ((v[0] == minv[0]) && (v[1] == minv[1]) && (v[2] >= minv[2]))) {
134                 r = 0;
135                 goto out;
136         }
137         condlog(0, "DM multipath kernel driver must be >= %u.%.2u.%.2u",
138                 minv[0], minv[1], minv[2]);
139 out:
140         dm_task_destroy(dmt);
141         return r;
142 }
143
144 extern int
145 dm_prereq (void)
146 {
147         if (dm_libprereq())
148                 return 1;
149         return dm_drvprereq(TGT_MPATH);
150 }
151
152 static int
153 dm_simplecmd (int task, const char *name, int no_flush) {
154         int r = 0;
155         struct dm_task *dmt;
156
157         if (!(dmt = dm_task_create (task)))
158                 return 0;
159
160         if (!dm_task_set_name (dmt, name))
161                 goto out;
162
163         dm_task_no_open_count(dmt);
164         dm_task_skip_lockfs(dmt);       /* for DM_DEVICE_RESUME */
165 #ifdef LIBDM_API_FLUSH
166         if (no_flush)
167                 dm_task_no_flush(dmt);          /* for DM_DEVICE_SUSPEND/RESUME */
168 #endif
169
170         r = dm_task_run (dmt);
171
172         out:
173         dm_task_destroy (dmt);
174         return r;
175 }
176
177 extern int
178 dm_simplecmd_flush (int task, const char *name) {
179         return dm_simplecmd(task, name, 0);
180 }
181
182 extern int
183 dm_simplecmd_noflush (int task, const char *name) {
184         return dm_simplecmd(task, name, 1);
185 }
186
187 extern int
188 dm_addmap (int task, const char *name, const char *target,
189            const char *params, unsigned long long size, const char *uuid,
190            int ro) {
191         int r = 0;
192         struct dm_task *dmt;
193         char *prefixed_uuid = NULL;
194
195         if (!(dmt = dm_task_create (task)))
196                 return 0;
197
198         if (!dm_task_set_name (dmt, name))
199                 goto addout;
200
201         if (!dm_task_add_target (dmt, 0, size, target, params))
202                 goto addout;
203
204         if (ro)
205                 dm_task_set_ro(dmt);
206
207         if (uuid){
208                 prefixed_uuid = MALLOC(UUID_PREFIX_LEN + strlen(uuid) + 1);
209                 if (!prefixed_uuid) {
210                         condlog(0, "cannot create prefixed uuid : %s\n",
211                                 strerror(errno));
212                         goto addout;
213                 }
214                 sprintf(prefixed_uuid, UUID_PREFIX "%s", uuid);
215                 if (!dm_task_set_uuid(dmt, prefixed_uuid))
216                         goto freeout;
217         }
218
219         dm_task_no_open_count(dmt);
220
221         r = dm_task_run (dmt);
222
223         freeout:
224         if (prefixed_uuid)
225                 FREE(prefixed_uuid);
226
227         addout:
228         dm_task_destroy (dmt);
229
230         return r;
231 }
232
233 static int
234 _dm_addmap_create (const char *name, const char *params,
235                   unsigned long long size, const char *uuid, int ro) {
236         int r;
237         r = dm_addmap(DM_DEVICE_CREATE, name, TGT_MPATH, params, size, uuid,
238                       ro);
239         /*
240          * DM_DEVICE_CREATE is actually DM_DEV_CREATE + DM_TABLE_LOAD.
241          * Failing the second part leaves an empty map. Clean it up.
242          */
243         if (!r && dm_map_present(name)) {
244                 condlog(3, "%s: failed to load map (a path might be in use)",
245                         name);
246                 dm_flush_map(name);
247         }
248         return r;
249 }
250
251 #define ADDMAP_RW 0
252 #define ADDMAP_RO 1
253
254 extern int
255 dm_addmap_create (const char *name, const char *params,
256                   unsigned long long size, const char *uuid) {
257         return _dm_addmap_create(name, params, size, uuid, ADDMAP_RW);
258 }
259
260 extern int
261 dm_addmap_create_ro (const char *name, const char *params,
262                   unsigned long long size, const char *uuid) {
263         return _dm_addmap_create(name, params, size, uuid, ADDMAP_RO);
264 }
265
266 extern int
267 dm_addmap_reload (const char *name, const char *params,
268                   unsigned long long size, const char *uuid) {
269         return dm_addmap(DM_DEVICE_RELOAD, name, TGT_MPATH, params, size, uuid,
270                          ADDMAP_RW);
271 }
272
273 extern int
274 dm_addmap_reload_ro (const char *name, const char *params,
275                      unsigned long long size, const char *uuid) {
276         return dm_addmap(DM_DEVICE_RELOAD, name, TGT_MPATH, params, size, uuid,
277                          ADDMAP_RO);
278 }
279
280 extern int
281 dm_map_present (const char * str)
282 {
283         int r = 0;
284         struct dm_task *dmt;
285         struct dm_info info;
286
287         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
288                 return 0;
289
290         if (!dm_task_set_name(dmt, str))
291                 goto out;
292
293         dm_task_no_open_count(dmt);
294
295         if (!dm_task_run(dmt))
296                 goto out;
297
298         if (!dm_task_get_info(dmt, &info))
299                 goto out;
300
301         if (info.exists)
302                 r = 1;
303 out:
304         dm_task_destroy(dmt);
305         return r;
306 }
307
308 extern int
309 dm_get_map(char * name, unsigned long long * size, char * outparams)
310 {
311         int r = 1;
312         struct dm_task *dmt;
313         void *next = NULL;
314         uint64_t start, length;
315         char *target_type = NULL;
316         char *params = NULL;
317
318         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
319                 return 1;
320
321         if (!dm_task_set_name(dmt, name))
322                 goto out;
323
324         dm_task_no_open_count(dmt);
325
326         if (!dm_task_run(dmt))
327                 goto out;
328
329         /* Fetch 1st target */
330         next = dm_get_next_target(dmt, next, &start, &length,
331                                   &target_type, &params);
332
333         if (size)
334                 *size = length;
335
336         if (snprintf(outparams, PARAMS_SIZE, "%s", params) <= PARAMS_SIZE)
337                 r = 0;
338 out:
339         dm_task_destroy(dmt);
340         return r;
341 }
342
343 extern int
344 dm_get_uuid(char *name, char *uuid)
345 {
346         struct dm_task *dmt;
347         const char *uuidtmp;
348         int r = 1;
349
350         dmt = dm_task_create(DM_DEVICE_INFO);
351         if (!dmt)
352                 return 1;
353
354         if (!dm_task_set_name (dmt, name))
355                 goto uuidout;
356
357         if (!dm_task_run(dmt))
358                 goto uuidout;
359
360         uuidtmp = dm_task_get_uuid(dmt);
361         if (uuidtmp) {
362                 if (!strncmp(uuidtmp, UUID_PREFIX, UUID_PREFIX_LEN))
363                         strcpy(uuid, uuidtmp + UUID_PREFIX_LEN);
364                 else
365                         strcpy(uuid, uuidtmp);
366         }
367         else
368                 uuid[0] = '\0';
369
370         r = 0;
371 uuidout:
372         dm_task_destroy(dmt);
373         return r;
374 }
375
376 extern int
377 dm_get_status(char * name, char * outstatus)
378 {
379         int r = 1;
380         struct dm_task *dmt;
381         void *next = NULL;
382         uint64_t start, length;
383         char *target_type;
384         char *status;
385
386         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
387                 return 1;
388
389         if (!dm_task_set_name(dmt, name))
390                 goto out;
391
392         dm_task_no_open_count(dmt);
393
394         if (!dm_task_run(dmt))
395                 goto out;
396
397         /* Fetch 1st target */
398         next = dm_get_next_target(dmt, next, &start, &length,
399                                   &target_type, &status);
400
401         if (snprintf(outstatus, PARAMS_SIZE, "%s", status) <= PARAMS_SIZE)
402                 r = 0;
403 out:
404         if (r)
405                 condlog(0, "%s: error getting map status string", name);
406
407         dm_task_destroy(dmt);
408         return r;
409 }
410
411 /*
412  * returns:
413  *    1 : match
414  *    0 : no match
415  *   -1 : empty map
416  */
417 extern int
418 dm_type(const char * name, char * type)
419 {
420         int r = 0;
421         struct dm_task *dmt;
422         void *next = NULL;
423         uint64_t start, length;
424         char *target_type = NULL;
425         char *params;
426
427         if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
428                 return 0;
429
430         if (!dm_task_set_name(dmt, name))
431                 goto out;
432
433         dm_task_no_open_count(dmt);
434
435         if (!dm_task_run(dmt))
436                 goto out;
437
438         /* Fetch 1st target */
439         next = dm_get_next_target(dmt, next, &start, &length,
440                                   &target_type, &params);
441
442         if (!target_type)
443                 r = -1;
444         else if (!strcmp(target_type, type))
445                 r = 1;
446
447 out:
448         dm_task_destroy(dmt);
449         return r;
450 }
451
452 static int
453 dm_dev_t (const char * mapname, char * dev_t, int len)
454 {
455         int r = 1;
456         struct dm_task *dmt;
457         struct dm_info info;
458
459         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
460                 return 0;
461
462         if (!dm_task_set_name(dmt, mapname))
463                 goto out;
464
465         if (!dm_task_run(dmt))
466                 goto out;
467
468         if (!dm_task_get_info(dmt, &info))
469                 goto out;
470
471         r = info.open_count;
472         if (snprintf(dev_t, len, "%i:%i", info.major, info.minor) > len)
473                     goto out;
474
475         r = 0;
476 out:
477         dm_task_destroy(dmt);
478         return r;
479 }
480
481 int
482 dm_get_opencount (const char * mapname)
483 {
484         int r = -1;
485         struct dm_task *dmt;
486         struct dm_info info;
487
488         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
489                 return 0;
490
491         if (!dm_task_set_name(dmt, mapname))
492                 goto out;
493
494         if (!dm_task_run(dmt))
495                 goto out;
496
497         if (!dm_task_get_info(dmt, &info))
498                 goto out;
499
500         r = info.open_count;
501 out:
502         dm_task_destroy(dmt);
503         return r;
504 }
505
506 int
507 dm_get_minor (char * mapname)
508 {
509         int r = -1;
510         struct dm_task *dmt;
511         struct dm_info info;
512
513         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
514                 return 0;
515
516         if (!dm_task_set_name(dmt, mapname))
517                 goto out;
518
519         if (!dm_task_run(dmt))
520                 goto out;
521
522         if (!dm_task_get_info(dmt, &info))
523                 goto out;
524
525         r = info.minor;
526 out:
527         dm_task_destroy(dmt);
528         return r;
529 }
530
531 extern int
532 dm_flush_map (const char * mapname)
533 {
534         int r;
535
536         if (!dm_map_present(mapname))
537                 return 0;
538
539         if (dm_type(mapname, TGT_MPATH) <= 0)
540                 return 0; /* nothing to do */
541
542         if (dm_remove_partmaps(mapname))
543                 return 1;
544
545         if (dm_get_opencount(mapname)) {
546                 condlog(2, "%s: map in use", mapname);
547                 return 1;
548         }
549
550         r = dm_simplecmd_flush(DM_DEVICE_REMOVE, mapname);
551
552         if (r) {
553                 condlog(4, "multipath map %s removed", mapname);
554                 return 0;
555         }
556         return 1;
557 }
558
559 extern int
560 dm_flush_maps (void)
561 {
562         int r = 0;
563         struct dm_task *dmt;
564         struct dm_names *names;
565         unsigned next = 0;
566
567         if (!(dmt = dm_task_create (DM_DEVICE_LIST)))
568                 return 0;
569
570         dm_task_no_open_count(dmt);
571
572         if (!dm_task_run (dmt))
573                 goto out;
574
575         if (!(names = dm_task_get_names (dmt)))
576                 goto out;
577
578         if (!names->dev)
579                 goto out;
580
581         do {
582                 r |= dm_flush_map(names->name);
583                 next = names->next;
584                 names = (void *) names + next;
585         } while (next);
586
587         out:
588         dm_task_destroy (dmt);
589         return r;
590 }
591
592 int
593 dm_message(char * mapname, char * message)
594 {
595         int r = 1;
596         struct dm_task *dmt;
597
598         if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
599                 return 1;
600
601         if (!dm_task_set_name(dmt, mapname))
602                 goto out;
603
604         if (!dm_task_set_sector(dmt, 0))
605                 goto out;
606
607         if (!dm_task_set_message(dmt, message))
608                 goto out;
609
610         dm_task_no_open_count(dmt);
611
612         if (!dm_task_run(dmt))
613                 goto out;
614
615         r = 0;
616 out:
617         if (r)
618                 condlog(0, "DM message failed [%s]", message);
619
620         dm_task_destroy(dmt);
621         return r;
622 }
623
624 int
625 dm_fail_path(char * mapname, char * path)
626 {
627         char message[32];
628
629         if (snprintf(message, 32, "fail_path %s\n", path) > 32)
630                 return 1;
631
632         return dm_message(mapname, message);
633 }
634
635 int
636 dm_reinstate_path(char * mapname, char * path)
637 {
638         char message[32];
639
640         if (snprintf(message, 32, "reinstate_path %s\n", path) > 32)
641                 return 1;
642
643         return dm_message(mapname, message);
644 }
645
646 int
647 dm_queue_if_no_path(char *mapname, int enable)
648 {
649         char *message;
650
651         if (enable)
652                 message = "queue_if_no_path\n";
653         else
654                 message = "fail_if_no_path\n";
655
656         return dm_message(mapname, message);
657 }
658
659 int
660 dm_set_pg_timeout(char *mapname, int timeout_val)
661 {
662         char message[24];
663
664         if (snprintf(message, 24, "set_pg_timeout %d", timeout_val) >= 24)
665                 return 1;
666         return dm_message(mapname, message);
667 }
668
669 static int
670 dm_groupmsg (char * msg, char * mapname, int index)
671 {
672         char message[32];
673
674         if (snprintf(message, 32, "%s_group %i\n", msg, index) > 32)
675                 return 1;
676
677         return dm_message(mapname, message);
678 }
679
680 int
681 dm_switchgroup(char * mapname, int index)
682 {
683         return dm_groupmsg("switch", mapname, index);
684 }
685
686 int
687 dm_enablegroup(char * mapname, int index)
688 {
689         return dm_groupmsg("enable", mapname, index);
690 }
691
692 int
693 dm_disablegroup(char * mapname, int index)
694 {
695         return dm_groupmsg("disable", mapname, index);
696 }
697
698 int
699 dm_get_maps (vector mp)
700 {
701         struct multipath * mpp;
702         int r = 1;
703         int info;
704         struct dm_task *dmt;
705         struct dm_names *names;
706         unsigned next = 0;
707
708         if (!mp)
709                 return 1;
710
711         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
712                 return 1;
713
714         dm_task_no_open_count(dmt);
715
716         if (!dm_task_run(dmt))
717                 goto out;
718
719         if (!(names = dm_task_get_names(dmt)))
720                 goto out;
721
722         if (!names->dev) {
723                 r = 0; /* this is perfectly valid */
724                 goto out;
725         }
726
727         do {
728                 info = dm_type(names->name, TGT_MPATH);
729
730                 if (info <= 0)
731                         goto next;
732
733                 mpp = alloc_multipath();
734
735                 if (!mpp)
736                         goto out;
737
738                 mpp->alias = STRDUP(names->name);
739
740                 if (!mpp->alias)
741                         goto out1;
742
743                 if (info > 0) {
744                         if (dm_get_map(names->name, &mpp->size, mpp->params))
745                                 goto out1;
746
747                         if (dm_get_status(names->name, mpp->status))
748                                 goto out1;
749
750                         dm_get_uuid(names->name, mpp->wwid);
751                         dm_get_info(names->name, &mpp->dmi);
752                 }
753
754                 if (!vector_alloc_slot(mp))
755                         goto out1;
756
757                 vector_set_slot(mp, mpp);
758                 mpp = NULL;
759 next:
760                 next = names->next;
761                 names = (void *) names + next;
762         } while (next);
763
764         r = 0;
765         goto out;
766 out1:
767         free_multipath(mpp, KEEP_PATHS);
768 out:
769         dm_task_destroy (dmt);
770         return r;
771 }
772
773 extern int
774 dm_get_name(char *uuid, char *name)
775 {
776         vector vec;
777         struct multipath *mpp;
778         int i, rc = 0;
779
780         vec = vector_alloc();
781
782         if (!vec)
783                 return 0;
784
785         if (dm_get_maps(vec)) {
786                 goto out;
787         }
788
789         vector_foreach_slot(vec, mpp, i) {
790                 if (!strcmp(uuid, mpp->wwid)) {
791                         strcpy(name, mpp->alias);
792                         rc=1;
793                         break;
794                 }
795         }
796 out:
797         vector_foreach_slot(vec, mpp, i) {
798                 free_multipath(mpp, KEEP_PATHS);
799         }
800         vector_free(vec);
801         return rc;
802 }
803
804 int
805 dm_geteventnr (char *name)
806 {
807         struct dm_task *dmt;
808         struct dm_info info;
809
810         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
811                 return 0;
812
813         if (!dm_task_set_name(dmt, name))
814                 goto out;
815
816         dm_task_no_open_count(dmt);
817
818         if (!dm_task_run(dmt))
819                 goto out;
820
821         if (!dm_task_get_info(dmt, &info)) {
822                 info.event_nr = 0;
823                 goto out;
824         }
825
826         if (!info.exists) {
827                 info.event_nr = 0;
828                 goto out;
829         }
830
831 out:
832         dm_task_destroy(dmt);
833
834         return info.event_nr;
835 }
836
837 char *
838 dm_mapname(int major, int minor)
839 {
840         char * response = NULL;
841         const char *map;
842         struct dm_task *dmt;
843         int r;
844         int loop = MAX_WAIT * LOOPS_PER_SEC;
845
846         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
847                 return NULL;
848
849         if (!dm_task_set_major(dmt, major) ||
850             !dm_task_set_minor(dmt, minor))
851                 goto bad;
852
853         dm_task_no_open_count(dmt);
854
855         /*
856          * device map might not be ready when we get here from
857          * daemon uev_trigger -> uev_add_map
858          */
859         while (--loop) {
860                 r = dm_task_run(dmt);
861
862                 if (r)
863                         break;
864
865                 usleep(1000 * 1000 / LOOPS_PER_SEC);
866         }
867
868         if (!r) {
869                 condlog(0, "%i:%i: timeout fetching map name", major, minor);
870                 goto bad;
871         }
872
873         map = dm_task_get_name(dmt);
874         if (map && strlen(map))
875                 response = STRDUP((char *)dm_task_get_name(dmt));
876
877         dm_task_destroy(dmt);
878         return response;
879 bad:
880         dm_task_destroy(dmt);
881         condlog(0, "%i:%i: error fetching map name", major, minor);
882         return NULL;
883 }
884
885 int
886 dm_remove_partmaps (const char * mapname)
887 {
888         struct dm_task *dmt;
889         struct dm_names *names;
890         unsigned next = 0;
891         char params[PARAMS_SIZE];
892         unsigned long long size;
893         char dev_t[32];
894         int r = 1;
895
896         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
897                 return 1;
898
899         dm_task_no_open_count(dmt);
900
901         if (!dm_task_run(dmt))
902                 goto out;
903
904         if (!(names = dm_task_get_names(dmt)))
905                 goto out;
906
907         if (!names->dev) {
908                 r = 0; /* this is perfectly valid */
909                 goto out;
910         }
911
912         if (dm_dev_t(mapname, &dev_t[0], 32))
913                 goto out;
914
915         do {
916                 if (
917                     /*
918                      * if devmap target is "linear"
919                      */
920                     (dm_type(names->name, TGT_PART) > 0) &&
921
922                     /*
923                      * and the multipath mapname and the part mapname start
924                      * the same
925                      */
926                     !strncmp(names->name, mapname, strlen(mapname)) &&
927
928                     /*
929                      * and the opencount is 0 for us to allow removal
930                      */
931                     !dm_get_opencount(names->name) &&
932
933                     /*
934                      * and we can fetch the map table from the kernel
935                      */
936                     !dm_get_map(names->name, &size, &params[0]) &&
937
938                     /*
939                      * and the table maps over the multipath map
940                      */
941                     strstr(params, dev_t)
942                    ) {
943                                 /*
944                                  * then it's a kpartx generated partition.
945                                  * remove it.
946                                  */
947                                 condlog(4, "partition map %s removed",
948                                         names->name);
949                                 dm_simplecmd_flush(DM_DEVICE_REMOVE, names->name);
950                    }
951
952                 next = names->next;
953                 names = (void *) names + next;
954         } while (next);
955
956         r = 0;
957 out:
958         dm_task_destroy (dmt);
959         return r;
960 }
961
962 static struct dm_info *
963 alloc_dminfo (void)
964 {
965         return MALLOC(sizeof(struct dm_info));
966 }
967
968 int
969 dm_get_info (char * mapname, struct dm_info ** dmi)
970 {
971         int r = 1;
972         struct dm_task *dmt = NULL;
973
974         if (!mapname)
975                 return 1;
976
977         if (!*dmi)
978                 *dmi = alloc_dminfo();
979
980         if (!*dmi)
981                 return 1;
982
983         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
984                 goto out;
985
986         if (!dm_task_set_name(dmt, mapname))
987                 goto out;
988
989         dm_task_no_open_count(dmt);
990
991         if (!dm_task_run(dmt))
992                 goto out;
993
994         if (!dm_task_get_info(dmt, *dmi))
995                 goto out;
996
997         r = 0;
998 out:
999         if (r)
1000                 memset(*dmi, 0, sizeof(struct dm_info));
1001
1002         if (dmt)
1003                 dm_task_destroy(dmt);
1004
1005         return r;
1006 }
1007
1008 int
1009 dm_rename_partmaps (char * old, char * new)
1010 {
1011         struct dm_task *dmt;
1012         struct dm_names *names;
1013         unsigned next = 0;
1014         char buff[PARAMS_SIZE];
1015         unsigned long long size;
1016         char dev_t[32];
1017         int r = 1;
1018
1019         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
1020                 return 1;
1021
1022         dm_task_no_open_count(dmt);
1023
1024         if (!dm_task_run(dmt))
1025                 goto out;
1026
1027         if (!(names = dm_task_get_names(dmt)))
1028                 goto out;
1029
1030         if (!names->dev) {
1031                 r = 0; /* this is perfectly valid */
1032                 goto out;
1033         }
1034
1035         if (dm_dev_t(old, &dev_t[0], 32))
1036                 goto out;
1037
1038         do {
1039                 if (
1040                     /*
1041                      * if devmap target is "linear"
1042                      */
1043                     (dm_type(names->name, TGT_PART) > 0) &&
1044
1045                     /*
1046                      * and the multipath mapname and the part mapname start
1047                      * the same
1048                      */
1049                     !strncmp(names->name, old, strlen(old)) &&
1050
1051                     /*
1052                      * and we can fetch the map table from the kernel
1053                      */
1054                     !dm_get_map(names->name, &size, &buff[0]) &&
1055
1056                     /*
1057                      * and the table maps over the multipath map
1058                      */
1059                     strstr(buff, dev_t)
1060                    ) {
1061                                 /*
1062                                  * then it's a kpartx generated partition.
1063                                  * Rename it.
1064                                  */
1065                                 snprintf(buff, PARAMS_SIZE, "%s%s",
1066                                          new, names->name + strlen(old));
1067                                 dm_rename(names->name, buff);
1068                                 condlog(4, "partition map %s renamed",
1069                                         names->name);
1070                    }
1071
1072                 next = names->next;
1073                 names = (void *) names + next;
1074         } while (next);
1075
1076         r = 0;
1077 out:
1078         dm_task_destroy (dmt);
1079         return r;
1080 }
1081
1082 int
1083 dm_rename (char * old, char * new)
1084 {
1085         int r = 0;
1086         struct dm_task *dmt;
1087
1088         if (dm_rename_partmaps(old, new))
1089                 return r;
1090
1091         if (!(dmt = dm_task_create(DM_DEVICE_RENAME)))
1092                 return r;
1093
1094         if (!dm_task_set_name(dmt, old))
1095                 goto out;
1096
1097         if (!dm_task_set_newname(dmt, new))
1098                 goto out;
1099
1100         dm_task_no_open_count(dmt);
1101
1102         if (!dm_task_run(dmt))
1103                 goto out;
1104
1105         r = 1;
1106 out:
1107         dm_task_destroy(dmt);
1108         return r;
1109 }