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