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