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