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 0; /* nothing to do */
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, rc = 0;
768
769         vec = vector_alloc();
770
771         if (!vec)
772                 return 0;
773
774         if (dm_get_maps(vec)) {
775                 goto out;
776         }
777
778         vector_foreach_slot(vec, mpp, i) {
779                 if (!strcmp(uuid, mpp->wwid)) {
780                         strcpy(name, mpp->alias);
781                         rc=1;
782                         break;
783                 }
784         }
785 out:
786         vector_foreach_slot(vec, mpp, i) {
787                 free_multipath(mpp, KEEP_PATHS);
788         }
789         vector_free(vec);
790         return rc;
791 }
792
793 int
794 dm_geteventnr (char *name)
795 {
796         struct dm_task *dmt;
797         struct dm_info info;
798
799         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
800                 return 0;
801
802         if (!dm_task_set_name(dmt, name))
803                 goto out;
804
805         dm_task_no_open_count(dmt);
806
807         if (!dm_task_run(dmt))
808                 goto out;
809
810         if (!dm_task_get_info(dmt, &info)) {
811                 info.event_nr = 0;
812                 goto out;
813         }
814
815         if (!info.exists) {
816                 info.event_nr = 0;
817                 goto out;
818         }
819
820 out:
821         dm_task_destroy(dmt);
822
823         return info.event_nr;
824 }
825
826 char *
827 dm_mapname(int major, int minor)
828 {
829         char * response = NULL;
830         const char *map;
831         struct dm_task *dmt;
832         int r;
833         int loop = MAX_WAIT * LOOPS_PER_SEC;
834
835         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
836                 return NULL;
837
838         if (!dm_task_set_major(dmt, major) ||
839             !dm_task_set_minor(dmt, minor))
840                 goto bad;
841
842         dm_task_no_open_count(dmt);
843
844         /*
845          * device map might not be ready when we get here from
846          * daemon uev_trigger -> uev_add_map
847          */
848         while (--loop) {
849                 r = dm_task_run(dmt);
850
851                 if (r)
852                         break;
853
854                 usleep(1000 * 1000 / LOOPS_PER_SEC);
855         }
856
857         if (!r) {
858                 condlog(0, "%i:%i: timeout fetching map name", major, minor);
859                 goto bad;
860         }
861
862         map = dm_task_get_name(dmt);
863         if (map && strlen(map))
864                 response = STRDUP((char *)dm_task_get_name(dmt));
865
866         dm_task_destroy(dmt);
867         return response;
868 bad:
869         dm_task_destroy(dmt);
870         condlog(0, "%i:%i: error fetching map name", major, minor);
871         return NULL;
872 }
873
874 int
875 dm_remove_partmaps (const char * mapname)
876 {
877         struct dm_task *dmt;
878         struct dm_names *names;
879         unsigned next = 0;
880         char params[PARAMS_SIZE];
881         unsigned long long size;
882         char dev_t[32];
883         int r = 1;
884
885         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
886                 return 1;
887
888         dm_task_no_open_count(dmt);
889
890         if (!dm_task_run(dmt))
891                 goto out;
892
893         if (!(names = dm_task_get_names(dmt)))
894                 goto out;
895
896         if (!names->dev) {
897                 r = 0; /* this is perfectly valid */
898                 goto out;
899         }
900
901         if (dm_dev_t(mapname, &dev_t[0], 32))
902                 goto out;
903
904         do {
905                 if (
906                     /*
907                      * if devmap target is "linear"
908                      */
909                     (dm_type(names->name, TGT_PART) > 0) &&
910
911                     /*
912                      * and the multipath mapname and the part mapname start
913                      * the same
914                      */
915                     !strncmp(names->name, mapname, strlen(mapname)) &&
916
917                     /*
918                      * and the opencount is 0 for us to allow removal
919                      */
920                     !dm_get_opencount(names->name) &&
921
922                     /*
923                      * and we can fetch the map table from the kernel
924                      */
925                     !dm_get_map(names->name, &size, &params[0]) &&
926
927                     /*
928                      * and the table maps over the multipath map
929                      */
930                     strstr(params, dev_t)
931                    ) {
932                                 /*
933                                  * then it's a kpartx generated partition.
934                                  * remove it.
935                                  */
936                                 condlog(4, "partition map %s removed",
937                                         names->name);
938                                 dm_simplecmd(DM_DEVICE_REMOVE, names->name);
939                    }
940
941                 next = names->next;
942                 names = (void *) names + next;
943         } while (next);
944
945         r = 0;
946 out:
947         dm_task_destroy (dmt);
948         return r;
949 }
950
951 static struct dm_info *
952 alloc_dminfo (void)
953 {
954         return MALLOC(sizeof(struct dm_info));
955 }
956
957 int
958 dm_get_info (char * mapname, struct dm_info ** dmi)
959 {
960         int r = 1;
961         struct dm_task *dmt = NULL;
962
963         if (!mapname)
964                 return 1;
965
966         if (!*dmi)
967                 *dmi = alloc_dminfo();
968
969         if (!*dmi)
970                 return 1;
971
972         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
973                 goto out;
974
975         if (!dm_task_set_name(dmt, mapname))
976                 goto out;
977
978         dm_task_no_open_count(dmt);
979
980         if (!dm_task_run(dmt))
981                 goto out;
982
983         if (!dm_task_get_info(dmt, *dmi))
984                 goto out;
985
986         r = 0;
987 out:
988         if (r)
989                 memset(*dmi, 0, sizeof(struct dm_info));
990
991         if (dmt)
992                 dm_task_destroy(dmt);
993
994         return r;
995 }
996
997 int
998 dm_rename_partmaps (char * old, char * new)
999 {
1000         struct dm_task *dmt;
1001         struct dm_names *names;
1002         unsigned next = 0;
1003         char buff[PARAMS_SIZE];
1004         unsigned long long size;
1005         char dev_t[32];
1006         int r = 1;
1007
1008         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
1009                 return 1;
1010
1011         dm_task_no_open_count(dmt);
1012
1013         if (!dm_task_run(dmt))
1014                 goto out;
1015
1016         if (!(names = dm_task_get_names(dmt)))
1017                 goto out;
1018
1019         if (!names->dev) {
1020                 r = 0; /* this is perfectly valid */
1021                 goto out;
1022         }
1023
1024         if (dm_dev_t(old, &dev_t[0], 32))
1025                 goto out;
1026
1027         do {
1028                 if (
1029                     /*
1030                      * if devmap target is "linear"
1031                      */
1032                     (dm_type(names->name, TGT_PART) > 0) &&
1033
1034                     /*
1035                      * and the multipath mapname and the part mapname start
1036                      * the same
1037                      */
1038                     !strncmp(names->name, old, strlen(old)) &&
1039
1040                     /*
1041                      * and we can fetch the map table from the kernel
1042                      */
1043                     !dm_get_map(names->name, &size, &buff[0]) &&
1044
1045                     /*
1046                      * and the table maps over the multipath map
1047                      */
1048                     strstr(buff, dev_t)
1049                    ) {
1050                                 /*
1051                                  * then it's a kpartx generated partition.
1052                                  * Rename it.
1053                                  */
1054                                 snprintf(buff, PARAMS_SIZE, "%s%s",
1055                                          new, names->name + strlen(old));
1056                                 dm_rename(names->name, buff);
1057                                 condlog(4, "partition map %s renamed",
1058                                         names->name);
1059                    }
1060
1061                 next = names->next;
1062                 names = (void *) names + next;
1063         } while (next);
1064
1065         r = 0;
1066 out:
1067         dm_task_destroy (dmt);
1068         return r;
1069 }
1070
1071 int
1072 dm_rename (char * old, char * new)
1073 {
1074         int r = 0;
1075         struct dm_task *dmt;
1076
1077         if (dm_rename_partmaps(old, new))
1078                 return r;
1079
1080         if (!(dmt = dm_task_create(DM_DEVICE_RENAME)))
1081                 return r;
1082
1083         if (!dm_task_set_name(dmt, old))
1084                 goto out;
1085
1086         if (!dm_task_set_newname(dmt, new))
1087                 goto out;
1088
1089         dm_task_no_open_count(dmt);
1090
1091         if (!dm_task_run(dmt))
1092                 goto out;
1093
1094         r = 1;
1095 out:
1096         dm_task_destroy(dmt);
1097         return r;
1098 }