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