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