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
10 #include <libdevmapper.h>
12 #include <linux/kdev_t.h>
19 #include "devmapper.h"
22 #define LOOPS_PER_SEC 5
25 dm_dummy_log (int level, const char *file, int line, const char *f, ...)
39 dm_log_init(&dm_dummy_log);
43 dm_prereq (char * str, int x, int y, int z)
47 struct dm_versions *target;
48 struct dm_versions *last_target;
50 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
53 dm_task_no_open_count(dmt);
55 if (!dm_task_run(dmt)) {
56 condlog(0, "Can not communicate with kernel DM");
60 target = dm_task_get_versions(dmt);
65 if (!strncmp(str, target->name, strlen(str))) {
68 if (target->version[0] >= x &&
69 target->version[1] >= y &&
70 target->version[2] >= z)
76 target = (void *) target + target->next;
77 } while (last_target != target);
80 condlog(0, "DM multipath kernel driver not loaded");
82 condlog(0, "DM multipath kernel driver version too old");
90 dm_simplecmd (int task, const char *name) {
94 if (!(dmt = dm_task_create (task)))
97 if (!dm_task_set_name (dmt, name))
100 dm_task_no_open_count(dmt);
102 r = dm_task_run (dmt);
105 dm_task_destroy (dmt);
110 dm_addmap (int task, const char *name, const char *target,
111 const char *params, unsigned long long size, const char *uuid) {
115 if (!(dmt = dm_task_create (task)))
118 if (!dm_task_set_name (dmt, name))
121 if (!dm_task_add_target (dmt, 0, size, target, params))
124 if (uuid && !dm_task_set_uuid(dmt, uuid))
127 dm_task_no_open_count(dmt);
129 r = dm_task_run (dmt);
132 dm_task_destroy (dmt);
137 dm_map_present (char * str)
143 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
146 if (!dm_task_set_name(dmt, str))
149 dm_task_no_open_count(dmt);
151 if (!dm_task_run(dmt))
154 if (!dm_task_get_info(dmt, &info))
160 dm_task_destroy(dmt);
165 dm_get_map(char * name, unsigned long long * size, char * outparams)
170 uint64_t start, length;
171 char *target_type = NULL;
174 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
177 if (!dm_task_set_name(dmt, name))
180 dm_task_no_open_count(dmt);
182 if (!dm_task_run(dmt))
185 /* Fetch 1st target */
186 next = dm_get_next_target(dmt, next, &start, &length,
187 &target_type, ¶ms);
192 if (snprintf(outparams, PARAMS_SIZE, "%s", params) <= PARAMS_SIZE)
195 dm_task_destroy(dmt);
200 dm_get_uuid(char *name, char *uuid)
205 dmt = dm_task_create(DM_DEVICE_INFO);
209 if (!dm_task_set_name (dmt, name))
212 if (!dm_task_run(dmt))
215 uuidtmp = dm_task_get_uuid(dmt);
217 strcpy(uuid, uuidtmp);
222 dm_task_destroy(dmt);
228 dm_get_status(char * name, char * outstatus)
233 uint64_t start, length;
237 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
240 if (!dm_task_set_name(dmt, name))
243 dm_task_no_open_count(dmt);
245 if (!dm_task_run(dmt))
248 /* Fetch 1st target */
249 next = dm_get_next_target(dmt, next, &start, &length,
250 &target_type, &status);
252 if (snprintf(outstatus, PARAMS_SIZE, "%s", status) <= PARAMS_SIZE)
256 condlog(0, "%s: error getting map status string", name);
258 dm_task_destroy(dmt);
269 dm_type(char * name, char * type)
274 uint64_t start, length;
275 char *target_type = NULL;
278 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
281 if (!dm_task_set_name(dmt, name))
284 dm_task_no_open_count(dmt);
286 if (!dm_task_run(dmt))
289 /* Fetch 1st target */
290 next = dm_get_next_target(dmt, next, &start, &length,
291 &target_type, ¶ms);
295 else if (!strcmp(target_type, type))
299 dm_task_destroy(dmt);
304 dm_dev_t (char * mapname, char * dev_t, int len)
310 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
313 if (!dm_task_set_name(dmt, mapname))
316 if (!dm_task_run(dmt))
319 if (!dm_task_get_info(dmt, &info))
323 if (snprintf(dev_t, len, "%i:%i", info.major, info.minor) > len)
328 dm_task_destroy(dmt);
333 dm_get_opencount (char * mapname)
339 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
342 if (!dm_task_set_name(dmt, mapname))
345 if (!dm_task_run(dmt))
348 if (!dm_task_get_info(dmt, &info))
353 dm_task_destroy(dmt);
358 dm_get_minor (char * mapname)
364 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
367 if (!dm_task_set_name(dmt, mapname))
370 if (!dm_task_run(dmt))
373 if (!dm_task_get_info(dmt, &info))
378 dm_task_destroy(dmt);
383 dm_flush_map (char * mapname, char * type)
387 if (!dm_map_present(mapname))
390 if (dm_type(mapname, type) <= 0)
393 if (dm_remove_partmaps(mapname))
396 if (dm_get_opencount(mapname)) {
397 condlog(2, "%s: map in use", mapname);
401 r = dm_simplecmd(DM_DEVICE_REMOVE, mapname);
404 condlog(4, "multipath map %s removed", mapname);
411 dm_flush_maps (char * type)
415 struct dm_names *names;
418 if (!(dmt = dm_task_create (DM_DEVICE_LIST)))
421 dm_task_no_open_count(dmt);
423 if (!dm_task_run (dmt))
426 if (!(names = dm_task_get_names (dmt)))
433 r += dm_flush_map(names->name, type);
435 names = (void *) names + next;
439 dm_task_destroy (dmt);
444 dm_message(char * mapname, char * message)
449 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
452 if (!dm_task_set_name(dmt, mapname))
455 if (!dm_task_set_sector(dmt, 0))
458 if (!dm_task_set_message(dmt, message))
461 dm_task_no_open_count(dmt);
463 if (!dm_task_run(dmt))
469 condlog(0, "DM message failed [%s]", message);
471 dm_task_destroy(dmt);
476 dm_fail_path(char * mapname, char * path)
480 if (snprintf(message, 32, "fail_path %s\n", path) > 32)
483 return dm_message(mapname, message);
487 dm_reinstate_path(char * mapname, char * path)
491 if (snprintf(message, 32, "reinstate_path %s\n", path) > 32)
494 return dm_message(mapname, message);
498 dm_queue_if_no_path(char *mapname, int enable)
503 message = "queue_if_no_path\n";
505 message = "fail_if_no_path\n";
507 return dm_message(mapname, message);
511 dm_groupmsg (char * msg, char * mapname, int index)
515 if (snprintf(message, 32, "%s_group %i\n", msg, index) > 32)
518 return dm_message(mapname, message);
522 dm_switchgroup(char * mapname, int index)
524 return dm_groupmsg("switch", mapname, index);
528 dm_enablegroup(char * mapname, int index)
530 return dm_groupmsg("enable", mapname, index);
534 dm_disablegroup(char * mapname, int index)
536 return dm_groupmsg("disable", mapname, index);
540 dm_get_maps (vector mp, char * type)
542 struct multipath * mpp;
546 struct dm_names *names;
552 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
555 dm_task_no_open_count(dmt);
557 if (!dm_task_run(dmt))
560 if (!(names = dm_task_get_names(dmt)))
564 r = 0; /* this is perfectly valid */
569 info = dm_type(names->name, type);
574 mpp = alloc_multipath();
579 mpp->alias = STRDUP(names->name);
585 if (dm_get_map(names->name, &mpp->size, mpp->params))
588 if (dm_get_status(names->name, mpp->status))
591 dm_get_uuid(names->name, mpp->wwid);
594 if (!vector_alloc_slot(mp))
597 vector_set_slot(mp, mpp);
601 names = (void *) names + next;
607 free_multipath(mpp, KEEP_PATHS);
609 dm_task_destroy (dmt);
614 dm_get_name(char *uuid, char *type, char *name)
617 struct multipath *mpp;
620 vec = vector_alloc();
625 if (dm_get_maps(vec, type)) {
630 vector_foreach_slot(vec, mpp, i) {
631 if (!strcmp(uuid, mpp->wwid)) {
633 strcpy(name, mpp->alias);
643 dm_geteventnr (char *name)
648 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
651 if (!dm_task_set_name(dmt, name))
654 dm_task_no_open_count(dmt);
656 if (!dm_task_run(dmt))
659 if (!dm_task_get_info(dmt, &info)) {
670 dm_task_destroy(dmt);
672 return info.event_nr;
676 dm_mapname(int major, int minor)
678 char * response = NULL;
682 int loop = MAX_WAIT * LOOPS_PER_SEC;
684 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
687 if (!dm_task_set_major(dmt, major) ||
688 !dm_task_set_minor(dmt, minor))
691 dm_task_no_open_count(dmt);
694 * device map might not be ready when we get here from
695 * daemon uev_trigger -> uev_add_map
699 r = dm_task_run(dmt);
705 usleep(1000 * 1000 / LOOPS_PER_SEC);
709 condlog(0, "%i:%i: timeout fetching map name", major, minor);
713 map = dm_task_get_name(dmt);
714 if (map && strlen(map))
715 response = STRDUP((char *)dm_task_get_name(dmt));
717 dm_task_destroy(dmt);
720 dm_task_destroy(dmt);
721 condlog(0, "%i:%i: error fetching map name", major, minor);
726 dm_remove_partmaps (char * mapname)
729 struct dm_names *names;
731 char params[PARAMS_SIZE];
732 unsigned long long size;
736 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
739 dm_task_no_open_count(dmt);
741 if (!dm_task_run(dmt))
744 if (!(names = dm_task_get_names(dmt)))
748 r = 0; /* this is perfectly valid */
752 if (dm_dev_t(mapname, &dev_t[0], 32))
758 * if devmap target is "linear"
760 (dm_type(names->name, "linear") > 0) &&
763 * and the multipath mapname and the part mapname start
766 !strncmp(names->name, mapname, strlen(mapname)) &&
769 * and the opencount is 0 for us to allow removal
771 !dm_get_opencount(names->name) &&
774 * and we can fetch the map table from the kernel
776 !dm_get_map(names->name, &size, ¶ms[0]) &&
779 * and the table maps over the multipath map
781 strstr(params, dev_t)
784 * then it's a kpartx generated partition.
787 condlog(4, "partition map %s removed",
789 dm_simplecmd(DM_DEVICE_REMOVE, names->name);
793 names = (void *) names + next;
798 dm_task_destroy (dmt);
802 static struct dm_info *
805 return MALLOC(sizeof(struct dm_info));
809 dm_get_info (char * mapname, struct dm_info ** dmi)
812 struct dm_task *dmt = NULL;
818 *dmi = alloc_dminfo();
823 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
826 if (!dm_task_set_name(dmt, mapname))
829 dm_task_no_open_count(dmt);
831 if (!dm_task_run(dmt))
834 if (!dm_task_get_info(dmt, *dmi))
840 memset(*dmi, 0, sizeof(struct dm_info));
843 dm_task_destroy(dmt);
849 dm_rename (char * old, char * new)
854 if (!(dmt = dm_task_create(DM_DEVICE_RENAME)))
857 if (!dm_task_set_name(dmt, old))
860 if (!dm_task_set_newname(dmt, new))
863 dm_task_no_open_count(dmt);
865 if (!dm_task_run(dmt))
870 dm_task_destroy(dmt);