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