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