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
11 #include <libdevmapper.h>
13 #include <linux/kdev_t.h>
22 #include "devmapper.h"
25 #include "log_pthread.h"
26 #include <sys/types.h>
30 #define LOOPS_PER_SEC 5
32 #define UUID_PREFIX "mpath-"
33 #define UUID_PREFIX_LEN 6
36 dm_write_log (int level, const char *file, int line, const char *f, ...)
44 thres = (conf) ? conf->verbosity : 0;
45 if (thres <= 3 || level > thres)
50 time_t t = time(NULL);
51 struct tm *tb = localtime(&t);
54 strftime(buff, sizeof(buff), "%b %d %H:%M:%S", tb);
55 buff[sizeof(buff)-1] = '\0';
57 fprintf(stdout, "%s | ", buff);
58 fprintf(stdout, "libdevmapper: %s(%i): ", file, line);
59 vfprintf(stdout, f, ap);
60 fprintf(stdout, "\n");
62 condlog(level, "libdevmapper: %s(%i): ", file, line);
63 log_safe(level + 3, f, ap);
72 dm_log_init(&dm_write_log);
73 dm_log_init_verbose(conf ? conf->verbosity + 3 : 0);
81 int minv[3] = {1, 2, 8};
83 dm_get_library_version(version, sizeof(version));
84 condlog(3, "libdevmapper version %s", version);
85 sscanf(version, "%d.%d.%d ", &v[0], &v[1], &v[2]);
87 if ((v[0] > minv[0]) ||
88 ((v[0] == minv[0]) && (v[1] > minv[1])) ||
89 ((v[0] == minv[0]) && (v[1] == minv[1]) && (v[2] >= minv[2])))
91 condlog(0, "libdevmapper version must be >= %d.%.2d.%.2d",
92 minv[0], minv[1], minv[2]);
97 dm_drvprereq (char * str)
101 struct dm_versions *target;
102 struct dm_versions *last_target;
103 int minv[3] = {1, 0, 3};
106 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
109 dm_task_no_open_count(dmt);
111 if (!dm_task_run(dmt)) {
112 condlog(0, "Can not communicate with kernel DM");
115 target = dm_task_get_versions(dmt);
118 last_target = target;
119 if (!strncmp(str, target->name, strlen(str))) {
123 target = (void *) target + target->next;
124 } while (last_target != target);
127 condlog(0, "DM multipath kernel driver not loaded");
131 if ((v[0] > minv[0]) ||
132 ((v[0] == minv[0]) && (v[1] > minv[1])) ||
133 ((v[0] == minv[0]) && (v[1] == minv[1]) && (v[2] >= minv[2]))) {
137 condlog(0, "DM multipath kernel driver must be >= %u.%.2u.%.2u",
138 minv[0], minv[1], minv[2]);
140 dm_task_destroy(dmt);
149 return dm_drvprereq(TGT_MPATH);
153 dm_simplecmd (int task, const char *name) {
157 if (!(dmt = dm_task_create (task)))
160 if (!dm_task_set_name (dmt, name))
163 dm_task_no_open_count(dmt);
164 dm_task_skip_lockfs(dmt); /* for DM_DEVICE_RESUME */
165 #ifdef LIBDM_API_FLUSH
166 dm_task_no_flush(dmt); /* for DM_DEVICE_SUSPEND/RESUME */
169 r = dm_task_run (dmt);
172 dm_task_destroy (dmt);
177 dm_addmap (int task, const char *name, const char *target,
178 const char *params, unsigned long long size, const char *uuid) {
181 char *prefixed_uuid = NULL;
183 if (!(dmt = dm_task_create (task)))
186 if (!dm_task_set_name (dmt, name))
189 if (!dm_task_add_target (dmt, 0, size, target, params))
193 prefixed_uuid = MALLOC(UUID_PREFIX_LEN + strlen(uuid) + 1);
194 if (!prefixed_uuid) {
195 condlog(0, "cannot create prefixed uuid : %s\n",
199 sprintf(prefixed_uuid, UUID_PREFIX "%s", uuid);
200 if (!dm_task_set_uuid(dmt, prefixed_uuid))
204 dm_task_no_open_count(dmt);
206 r = dm_task_run (dmt);
213 dm_task_destroy (dmt);
219 dm_addmap_create (const char *name, const char *params,
220 unsigned long long size, const char *uuid) {
222 r = dm_addmap(DM_DEVICE_CREATE, name, TGT_MPATH, params, size, uuid);
224 * DM_DEVICE_CREATE is actually DM_DEV_CREATE + DM_TABLE_LOAD.
225 * Failing the second part leaves an empty map. Clean it up.
227 if (!r && dm_map_present(name)) {
228 condlog(3, "%s: failed to load map (a path might be in use)",
236 dm_addmap_reload (const char *name, const char *params,
237 unsigned long long size, const char *uuid) {
239 r = dm_addmap(DM_DEVICE_RELOAD, name, TGT_MPATH, params, size, uuid);
244 dm_map_present (const char * str)
250 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
253 if (!dm_task_set_name(dmt, str))
256 dm_task_no_open_count(dmt);
258 if (!dm_task_run(dmt))
261 if (!dm_task_get_info(dmt, &info))
267 dm_task_destroy(dmt);
272 dm_get_map(char * name, unsigned long long * size, char * outparams)
277 uint64_t start, length;
278 char *target_type = NULL;
281 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
284 if (!dm_task_set_name(dmt, name))
287 dm_task_no_open_count(dmt);
289 if (!dm_task_run(dmt))
292 /* Fetch 1st target */
293 next = dm_get_next_target(dmt, next, &start, &length,
294 &target_type, ¶ms);
299 if (snprintf(outparams, PARAMS_SIZE, "%s", params) <= PARAMS_SIZE)
302 dm_task_destroy(dmt);
307 dm_get_uuid(char *name, char *uuid)
313 dmt = dm_task_create(DM_DEVICE_INFO);
317 if (!dm_task_set_name (dmt, name))
320 if (!dm_task_run(dmt))
323 uuidtmp = dm_task_get_uuid(dmt);
325 if (!strncmp(uuidtmp, UUID_PREFIX, UUID_PREFIX_LEN))
326 strcpy(uuid, uuidtmp + UUID_PREFIX_LEN);
328 strcpy(uuid, uuidtmp);
335 dm_task_destroy(dmt);
340 dm_get_status(char * name, char * outstatus)
345 uint64_t start, length;
349 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
352 if (!dm_task_set_name(dmt, name))
355 dm_task_no_open_count(dmt);
357 if (!dm_task_run(dmt))
360 /* Fetch 1st target */
361 next = dm_get_next_target(dmt, next, &start, &length,
362 &target_type, &status);
364 if (snprintf(outstatus, PARAMS_SIZE, "%s", status) <= PARAMS_SIZE)
368 condlog(0, "%s: error getting map status string", name);
370 dm_task_destroy(dmt);
381 dm_type(const char * name, char * type)
386 uint64_t start, length;
387 char *target_type = NULL;
390 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
393 if (!dm_task_set_name(dmt, name))
396 dm_task_no_open_count(dmt);
398 if (!dm_task_run(dmt))
401 /* Fetch 1st target */
402 next = dm_get_next_target(dmt, next, &start, &length,
403 &target_type, ¶ms);
407 else if (!strcmp(target_type, type))
411 dm_task_destroy(dmt);
416 dm_dev_t (const char * mapname, char * dev_t, int len)
422 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
425 if (!dm_task_set_name(dmt, mapname))
428 if (!dm_task_run(dmt))
431 if (!dm_task_get_info(dmt, &info))
435 if (snprintf(dev_t, len, "%i:%i", info.major, info.minor) > len)
440 dm_task_destroy(dmt);
445 dm_get_opencount (const char * mapname)
451 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
454 if (!dm_task_set_name(dmt, mapname))
457 if (!dm_task_run(dmt))
460 if (!dm_task_get_info(dmt, &info))
465 dm_task_destroy(dmt);
470 dm_get_minor (char * mapname)
476 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
479 if (!dm_task_set_name(dmt, mapname))
482 if (!dm_task_run(dmt))
485 if (!dm_task_get_info(dmt, &info))
490 dm_task_destroy(dmt);
495 dm_flush_map (const char * mapname)
499 if (!dm_map_present(mapname))
502 if (dm_type(mapname, TGT_MPATH) <= 0)
505 if (dm_remove_partmaps(mapname))
508 if (dm_get_opencount(mapname)) {
509 condlog(2, "%s: map in use", mapname);
513 r = dm_simplecmd(DM_DEVICE_REMOVE, mapname);
516 condlog(4, "multipath map %s removed", mapname);
527 struct dm_names *names;
530 if (!(dmt = dm_task_create (DM_DEVICE_LIST)))
533 dm_task_no_open_count(dmt);
535 if (!dm_task_run (dmt))
538 if (!(names = dm_task_get_names (dmt)))
545 r += dm_flush_map(names->name);
547 names = (void *) names + next;
551 dm_task_destroy (dmt);
556 dm_message(char * mapname, char * message)
561 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
564 if (!dm_task_set_name(dmt, mapname))
567 if (!dm_task_set_sector(dmt, 0))
570 if (!dm_task_set_message(dmt, message))
573 dm_task_no_open_count(dmt);
575 if (!dm_task_run(dmt))
581 condlog(0, "DM message failed [%s]", message);
583 dm_task_destroy(dmt);
588 dm_fail_path(char * mapname, char * path)
592 if (snprintf(message, 32, "fail_path %s\n", path) > 32)
595 return dm_message(mapname, message);
599 dm_reinstate_path(char * mapname, char * path)
603 if (snprintf(message, 32, "reinstate_path %s\n", path) > 32)
606 return dm_message(mapname, message);
610 dm_queue_if_no_path(char *mapname, int enable)
615 message = "queue_if_no_path\n";
617 message = "fail_if_no_path\n";
619 return dm_message(mapname, message);
623 dm_set_pg_timeout(char *mapname, int timeout_val)
627 if (snprintf(message, 24, "set_pg_timeout %d", timeout_val) >= 24)
629 return dm_message(mapname, message);
633 dm_groupmsg (char * msg, char * mapname, int index)
637 if (snprintf(message, 32, "%s_group %i\n", msg, index) > 32)
640 return dm_message(mapname, message);
644 dm_switchgroup(char * mapname, int index)
646 return dm_groupmsg("switch", mapname, index);
650 dm_enablegroup(char * mapname, int index)
652 return dm_groupmsg("enable", mapname, index);
656 dm_disablegroup(char * mapname, int index)
658 return dm_groupmsg("disable", mapname, index);
662 dm_get_maps (vector mp)
664 struct multipath * mpp;
668 struct dm_names *names;
674 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
677 dm_task_no_open_count(dmt);
679 if (!dm_task_run(dmt))
682 if (!(names = dm_task_get_names(dmt)))
686 r = 0; /* this is perfectly valid */
691 info = dm_type(names->name, TGT_MPATH);
696 mpp = alloc_multipath();
701 mpp->alias = STRDUP(names->name);
707 if (dm_get_map(names->name, &mpp->size, mpp->params))
710 if (dm_get_status(names->name, mpp->status))
713 dm_get_uuid(names->name, mpp->wwid);
714 dm_get_info(names->name, &mpp->dmi);
717 if (!vector_alloc_slot(mp))
720 vector_set_slot(mp, mpp);
724 names = (void *) names + next;
730 free_multipath(mpp, KEEP_PATHS);
732 dm_task_destroy (dmt);
737 dm_get_name(char *uuid, char *name)
740 struct multipath *mpp;
743 vec = vector_alloc();
748 if (dm_get_maps(vec)) {
753 vector_foreach_slot(vec, mpp, i) {
754 if (!strcmp(uuid, mpp->wwid)) {
756 strcpy(name, mpp->alias);
766 dm_geteventnr (char *name)
771 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
774 if (!dm_task_set_name(dmt, name))
777 dm_task_no_open_count(dmt);
779 if (!dm_task_run(dmt))
782 if (!dm_task_get_info(dmt, &info)) {
793 dm_task_destroy(dmt);
795 return info.event_nr;
799 dm_mapname(int major, int minor)
801 char * response = NULL;
805 int loop = MAX_WAIT * LOOPS_PER_SEC;
807 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
810 if (!dm_task_set_major(dmt, major) ||
811 !dm_task_set_minor(dmt, minor))
814 dm_task_no_open_count(dmt);
817 * device map might not be ready when we get here from
818 * daemon uev_trigger -> uev_add_map
821 r = dm_task_run(dmt);
826 usleep(1000 * 1000 / LOOPS_PER_SEC);
830 condlog(0, "%i:%i: timeout fetching map name", major, minor);
834 map = dm_task_get_name(dmt);
835 if (map && strlen(map))
836 response = STRDUP((char *)dm_task_get_name(dmt));
838 dm_task_destroy(dmt);
841 dm_task_destroy(dmt);
842 condlog(0, "%i:%i: error fetching map name", major, minor);
847 dm_remove_partmaps (const char * mapname)
850 struct dm_names *names;
852 char params[PARAMS_SIZE];
853 unsigned long long size;
857 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
860 dm_task_no_open_count(dmt);
862 if (!dm_task_run(dmt))
865 if (!(names = dm_task_get_names(dmt)))
869 r = 0; /* this is perfectly valid */
873 if (dm_dev_t(mapname, &dev_t[0], 32))
879 * if devmap target is "linear"
881 (dm_type(names->name, TGT_PART) > 0) &&
884 * and the multipath mapname and the part mapname start
887 !strncmp(names->name, mapname, strlen(mapname)) &&
890 * and the opencount is 0 for us to allow removal
892 !dm_get_opencount(names->name) &&
895 * and we can fetch the map table from the kernel
897 !dm_get_map(names->name, &size, ¶ms[0]) &&
900 * and the table maps over the multipath map
902 strstr(params, dev_t)
905 * then it's a kpartx generated partition.
908 condlog(4, "partition map %s removed",
910 dm_simplecmd(DM_DEVICE_REMOVE, names->name);
914 names = (void *) names + next;
919 dm_task_destroy (dmt);
923 static struct dm_info *
926 return MALLOC(sizeof(struct dm_info));
930 dm_get_info (char * mapname, struct dm_info ** dmi)
933 struct dm_task *dmt = NULL;
939 *dmi = alloc_dminfo();
944 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
947 if (!dm_task_set_name(dmt, mapname))
950 dm_task_no_open_count(dmt);
952 if (!dm_task_run(dmt))
955 if (!dm_task_get_info(dmt, *dmi))
961 memset(*dmi, 0, sizeof(struct dm_info));
964 dm_task_destroy(dmt);
970 dm_rename_partmaps (char * old, char * new)
973 struct dm_names *names;
975 char buff[PARAMS_SIZE];
976 unsigned long long size;
980 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
983 dm_task_no_open_count(dmt);
985 if (!dm_task_run(dmt))
988 if (!(names = dm_task_get_names(dmt)))
992 r = 0; /* this is perfectly valid */
996 if (dm_dev_t(old, &dev_t[0], 32))
1002 * if devmap target is "linear"
1004 (dm_type(names->name, TGT_PART) > 0) &&
1007 * and the multipath mapname and the part mapname start
1010 !strncmp(names->name, old, strlen(old)) &&
1013 * and we can fetch the map table from the kernel
1015 !dm_get_map(names->name, &size, &buff[0]) &&
1018 * and the table maps over the multipath map
1023 * then it's a kpartx generated partition.
1026 snprintf(buff, PARAMS_SIZE, "%s%s",
1027 new, names->name + strlen(old));
1028 dm_rename(names->name, buff);
1029 condlog(4, "partition map %s renamed",
1034 names = (void *) names + next;
1039 dm_task_destroy (dmt);
1044 dm_rename (char * old, char * new)
1047 struct dm_task *dmt;
1049 if (dm_rename_partmaps(old, new))
1052 if (!(dmt = dm_task_create(DM_DEVICE_RENAME)))
1055 if (!dm_task_set_name(dmt, old))
1058 if (!dm_task_set_newname(dmt, new))
1061 dm_task_no_open_count(dmt);
1063 if (!dm_task_run(dmt))
1068 dm_task_destroy(dmt);