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