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