[build] detect presence of dm_task_no_flush symbol in libdevmapper
[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 static int
569 dm_groupmsg (char * msg, char * mapname, int index)
570 {
571         char message[32];
572
573         if (snprintf(message, 32, "%s_group %i\n", msg, index) > 32)
574                 return 1;
575
576         return dm_message(mapname, message);
577 }
578
579 int
580 dm_switchgroup(char * mapname, int index)
581 {
582         return dm_groupmsg("switch", mapname, index);
583 }
584
585 int
586 dm_enablegroup(char * mapname, int index)
587 {
588         return dm_groupmsg("enable", mapname, index);
589 }
590
591 int
592 dm_disablegroup(char * mapname, int index)
593 {
594         return dm_groupmsg("disable", mapname, index);
595 }
596
597 int
598 dm_get_maps (vector mp, char * type)
599 {
600         struct multipath * mpp;
601         int r = 1;
602         int info;
603         struct dm_task *dmt;
604         struct dm_names *names;
605         unsigned next = 0;
606
607         if (!type || !mp)
608                 return 1;
609
610         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
611                 return 1;
612
613         dm_task_no_open_count(dmt);
614
615         if (!dm_task_run(dmt))
616                 goto out;
617
618         if (!(names = dm_task_get_names(dmt)))
619                 goto out;
620
621         if (!names->dev) {
622                 r = 0; /* this is perfectly valid */
623                 goto out;
624         }
625
626         do {
627                 info = dm_type(names->name, type);
628
629                 if (info <= 0)
630                         goto next;
631
632                 mpp = alloc_multipath();
633
634                 if (!mpp)
635                         goto out;
636
637                 mpp->alias = STRDUP(names->name);
638
639                 if (!mpp->alias)
640                         goto out1;
641
642                 if (info > 0) {
643                         if (dm_get_map(names->name, &mpp->size, mpp->params))
644                                 goto out1;
645
646                         if (dm_get_status(names->name, mpp->status))
647                                 goto out1;
648
649                         dm_get_uuid(names->name, mpp->wwid);
650                         dm_get_info(names->name, &mpp->dmi);
651                 }
652
653                 if (!vector_alloc_slot(mp))
654                         goto out1;
655
656                 vector_set_slot(mp, mpp);
657                 mpp = NULL;
658 next:
659                 next = names->next;
660                 names = (void *) names + next;
661         } while (next);
662
663         r = 0;
664         goto out;
665 out1:
666         free_multipath(mpp, KEEP_PATHS);
667 out:
668         dm_task_destroy (dmt);
669         return r;
670 }
671
672 extern int
673 dm_get_name(char *uuid, char *type, char *name)
674 {
675         vector vec;
676         struct multipath *mpp;
677         int i;
678
679         vec = vector_alloc();
680
681         if (!vec)
682                 return 0;
683
684         if (dm_get_maps(vec, type)) {
685                 vector_free(vec);
686                 return 0;
687         }
688
689         vector_foreach_slot(vec, mpp, i) {
690                 if (!strcmp(uuid, mpp->wwid)) {
691                         vector_free(vec);
692                         strcpy(name, mpp->alias);
693                         return 1;
694                 }
695         }
696
697         vector_free(vec);
698         return 0;
699 }
700
701 int
702 dm_geteventnr (char *name)
703 {
704         struct dm_task *dmt;
705         struct dm_info info;
706
707         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
708                 return 0;
709
710         if (!dm_task_set_name(dmt, name))
711                 goto out;
712
713         dm_task_no_open_count(dmt);
714
715         if (!dm_task_run(dmt))
716                 goto out;
717
718         if (!dm_task_get_info(dmt, &info)) {
719                 info.event_nr = 0;
720                 goto out;
721         }
722
723         if (!info.exists) {
724                 info.event_nr = 0;
725                 goto out;
726         }
727
728 out:
729         dm_task_destroy(dmt);
730
731         return info.event_nr;
732 }
733
734 char *
735 dm_mapname(int major, int minor)
736 {
737         char * response = NULL;
738         const char *map;
739         struct dm_task *dmt;
740         int r;
741         int loop = MAX_WAIT * LOOPS_PER_SEC;
742
743         if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
744                 return NULL;
745
746         if (!dm_task_set_major(dmt, major) ||
747             !dm_task_set_minor(dmt, minor))
748                 goto bad;
749
750         dm_task_no_open_count(dmt);
751
752         /*
753          * device map might not be ready when we get here from
754          * daemon uev_trigger -> uev_add_map
755          */
756         while (--loop) {
757                 dm_shut_log();
758                 r = dm_task_run(dmt);
759                 dm_restore_log();
760
761                 if (r)
762                         break;
763
764                 usleep(1000 * 1000 / LOOPS_PER_SEC);
765         }
766
767         if (!r) {
768                 condlog(0, "%i:%i: timeout fetching map name", major, minor);
769                 goto bad;
770         }
771
772         map = dm_task_get_name(dmt);
773         if (map && strlen(map))
774                 response = STRDUP((char *)dm_task_get_name(dmt));
775
776         dm_task_destroy(dmt);
777         return response;
778 bad:
779         dm_task_destroy(dmt);
780         condlog(0, "%i:%i: error fetching map name", major, minor);
781         return NULL;
782 }
783
784 int
785 dm_remove_partmaps (char * mapname)
786 {
787         struct dm_task *dmt;
788         struct dm_names *names;
789         unsigned next = 0;
790         char params[PARAMS_SIZE];
791         unsigned long long size;
792         char dev_t[32];
793         int r = 1;
794
795         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
796                 return 1;
797
798         dm_task_no_open_count(dmt);
799
800         if (!dm_task_run(dmt))
801                 goto out;
802
803         if (!(names = dm_task_get_names(dmt)))
804                 goto out;
805
806         if (!names->dev) {
807                 r = 0; /* this is perfectly valid */
808                 goto out;
809         }
810
811         if (dm_dev_t(mapname, &dev_t[0], 32))
812                 goto out;
813
814         do {
815                 if (
816                     /*
817                      * if devmap target is "linear"
818                      */
819                     (dm_type(names->name, "linear") > 0) &&
820
821                     /*
822                      * and the multipath mapname and the part mapname start
823                      * the same
824                      */
825                     !strncmp(names->name, mapname, strlen(mapname)) &&
826
827                     /*
828                      * and the opencount is 0 for us to allow removal
829                      */
830                     !dm_get_opencount(names->name) &&
831
832                     /*
833                      * and we can fetch the map table from the kernel
834                      */
835                     !dm_get_map(names->name, &size, &params[0]) &&
836
837                     /*
838                      * and the table maps over the multipath map
839                      */
840                     strstr(params, dev_t)
841                    ) {
842                                 /*
843                                  * then it's a kpartx generated partition.
844                                  * remove it.
845                                  */
846                                 condlog(4, "partition map %s removed",
847                                         names->name);
848                                 dm_simplecmd(DM_DEVICE_REMOVE, names->name);
849                    }
850
851                 next = names->next;
852                 names = (void *) names + next;
853         } while (next);
854
855         r = 0;
856 out:
857         dm_task_destroy (dmt);
858         return r;
859 }
860
861 static struct dm_info *
862 alloc_dminfo (void)
863 {
864         return MALLOC(sizeof(struct dm_info));
865 }
866
867 int
868 dm_get_info (char * mapname, struct dm_info ** dmi)
869 {
870         int r = 1;
871         struct dm_task *dmt = NULL;
872         
873         if (!mapname)
874                 return 1;
875
876         if (!*dmi)
877                 *dmi = alloc_dminfo();
878
879         if (!*dmi)
880                 return 1;
881
882         if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
883                 goto out;
884
885         if (!dm_task_set_name(dmt, mapname))
886                 goto out;
887
888         dm_task_no_open_count(dmt);
889
890         if (!dm_task_run(dmt))
891                 goto out;
892
893         if (!dm_task_get_info(dmt, *dmi))
894                 goto out;
895
896         r = 0;
897 out:
898         if (r)
899                 memset(*dmi, 0, sizeof(struct dm_info));
900
901         if (dmt)
902                 dm_task_destroy(dmt);
903
904         return r;
905 }
906
907 int
908 dm_rename_partmaps (char * old, char * new)
909 {
910         struct dm_task *dmt;
911         struct dm_names *names;
912         unsigned next = 0;
913         char buff[PARAMS_SIZE];
914         unsigned long long size;
915         char dev_t[32];
916         int r = 1;
917
918         if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
919                 return 1;
920
921         dm_task_no_open_count(dmt);
922
923         if (!dm_task_run(dmt))
924                 goto out;
925
926         if (!(names = dm_task_get_names(dmt)))
927                 goto out;
928
929         if (!names->dev) {
930                 r = 0; /* this is perfectly valid */
931                 goto out;
932         }
933
934         if (dm_dev_t(old, &dev_t[0], 32))
935                 goto out;
936
937         do {
938                 if (
939                     /*
940                      * if devmap target is "linear"
941                      */
942                     (dm_type(names->name, "linear") > 0) &&
943
944                     /*
945                      * and the multipath mapname and the part mapname start
946                      * the same
947                      */
948                     !strncmp(names->name, old, strlen(old)) &&
949
950                     /*
951                      * and we can fetch the map table from the kernel
952                      */
953                     !dm_get_map(names->name, &size, &buff[0]) &&
954
955                     /*
956                      * and the table maps over the multipath map
957                      */
958                     strstr(buff, dev_t)
959                    ) {
960                                 /*
961                                  * then it's a kpartx generated partition.
962                                  * Rename it.
963                                  */
964                                 snprintf(buff, PARAMS_SIZE, "%s%s",
965                                          new, names->name + strlen(old));
966                                 dm_rename(names->name, buff);
967                                 condlog(4, "partition map %s renamed",
968                                         names->name);
969                    }
970
971                 next = names->next;
972                 names = (void *) names + next;
973         } while (next);
974
975         r = 0;
976 out:
977         dm_task_destroy (dmt);
978         return r;
979 }
980
981 int
982 dm_rename (char * old, char * new)
983 {
984         int r = 0;
985         struct dm_task *dmt;
986
987         if (dm_rename_partmaps(old, new))
988                 return r;
989
990         if (!(dmt = dm_task_create(DM_DEVICE_RENAME)))
991                 return r;
992
993         if (!dm_task_set_name(dmt, old))
994                 goto out;
995
996         if (!dm_task_set_newname(dmt, new))
997                 goto out;
998         
999         dm_task_no_open_count(dmt);
1000
1001         if (!dm_task_run(dmt))
1002                 goto out;
1003
1004         r = 1;
1005 out:
1006         dm_task_destroy(dmt);
1007         return r;
1008 }