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>
21 #include "devmapper.h"
24 #include "log_pthread.h"
25 #include <sys/types.h>
29 #define LOOPS_PER_SEC 5
31 #define UUID_PREFIX "mpath-"
32 #define UUID_PREFIX_LEN 6
35 dm_write_log (int level, const char *file, int line, const char *f, ...)
43 thres = (conf) ? conf->verbosity : 0;
44 if (thres <= 3 || level > thres)
49 time_t t = time(NULL);
50 struct tm *tb = localtime(&t);
53 strftime(buff, sizeof(buff), "%b %d %H:%M:%S", tb);
54 buff[sizeof(buff)-1] = '\0';
56 fprintf(stdout, "%s | ", buff);
57 fprintf(stdout, "libdevmapper: %s(%i): ", file, line);
58 vfprintf(stdout, f, ap);
59 fprintf(stdout, "\n");
61 condlog(level, "libdevmapper: %s(%i): ", file, line);
62 log_safe(level + 3, f, ap);
71 dm_log_init(&dm_write_log);
72 dm_log_init_verbose(conf ? conf->verbosity + 3 : 0);
80 int minv[3] = {1, 2, 8};
82 dm_get_library_version(version, sizeof(version));
83 condlog(3, "libdevmapper version %s", version);
84 sscanf(version, "%d.%d.%d ", &v[0], &v[1], &v[2]);
86 if ((v[0] > minv[0]) ||
87 ((v[0] == minv[0]) && (v[1] > minv[1])) ||
88 ((v[0] == minv[0]) && (v[1] == minv[1]) && (v[2] >= minv[2])))
90 condlog(0, "libdevmapper version must be >= %d.%.2d.%.2d",
91 minv[0], minv[1], minv[2]);
96 dm_drvprereq (char * str)
100 struct dm_versions *target;
101 struct dm_versions *last_target;
102 int minv[3] = {1, 0, 3};
105 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
108 dm_task_no_open_count(dmt);
110 if (!dm_task_run(dmt)) {
111 condlog(0, "Can not communicate with kernel DM");
114 target = dm_task_get_versions(dmt);
117 last_target = target;
118 if (!strncmp(str, target->name, strlen(str))) {
122 target = (void *) target + target->next;
123 } while (last_target != target);
126 condlog(0, "DM multipath kernel driver not loaded");
130 if ((v[0] > minv[0]) ||
131 ((v[0] == minv[0]) && (v[1] > minv[1])) ||
132 ((v[0] == minv[0]) && (v[1] == minv[1]) && (v[2] >= minv[2]))) {
136 condlog(0, "DM multipath kernel driver must be >= %u.%.2u.%.2u",
137 minv[0], minv[1], minv[2]);
139 dm_task_destroy(dmt);
148 return dm_drvprereq(TGT_MPATH);
152 dm_simplecmd (int task, const char *name, int no_flush) {
156 if (!(dmt = dm_task_create (task)))
159 if (!dm_task_set_name (dmt, name))
162 dm_task_no_open_count(dmt);
163 dm_task_skip_lockfs(dmt); /* for DM_DEVICE_RESUME */
164 #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_simplecmd_flush (int task, const char *name) {
178 return dm_simplecmd(task, name, 0);
182 dm_simplecmd_noflush (int task, const char *name) {
183 return dm_simplecmd(task, name, 1);
187 dm_addmap (int task, const char *name, const char *target,
188 const char *params, unsigned long long size, const char *uuid,
192 char *prefixed_uuid = NULL;
194 if (!(dmt = dm_task_create (task)))
197 if (!dm_task_set_name (dmt, name))
200 if (!dm_task_add_target (dmt, 0, size, target, params))
207 prefixed_uuid = MALLOC(UUID_PREFIX_LEN + strlen(uuid) + 1);
208 if (!prefixed_uuid) {
209 condlog(0, "cannot create prefixed uuid : %s\n",
213 sprintf(prefixed_uuid, UUID_PREFIX "%s", uuid);
214 if (!dm_task_set_uuid(dmt, prefixed_uuid))
218 dm_task_no_open_count(dmt);
220 r = dm_task_run (dmt);
227 dm_task_destroy (dmt);
233 _dm_addmap_create (const char *name, const char *params,
234 unsigned long long size, const char *uuid, int ro) {
236 r = dm_addmap(DM_DEVICE_CREATE, name, TGT_MPATH, params, size, uuid,
239 * DM_DEVICE_CREATE is actually DM_DEV_CREATE + DM_TABLE_LOAD.
240 * Failing the second part leaves an empty map. Clean it up.
242 if (!r && dm_map_present(name)) {
243 condlog(3, "%s: failed to load map (a path might be in use)",
254 dm_addmap_create (const char *name, const char *params,
255 unsigned long long size, const char *uuid) {
256 return _dm_addmap_create(name, params, size, uuid, ADDMAP_RW);
260 dm_addmap_create_ro (const char *name, const char *params,
261 unsigned long long size, const char *uuid) {
262 return _dm_addmap_create(name, params, size, uuid, ADDMAP_RO);
266 dm_addmap_reload (const char *name, const char *params,
267 unsigned long long size, const char *uuid) {
268 return dm_addmap(DM_DEVICE_RELOAD, name, TGT_MPATH, params, size, uuid,
273 dm_addmap_reload_ro (const char *name, const char *params,
274 unsigned long long size, const char *uuid) {
275 return dm_addmap(DM_DEVICE_RELOAD, name, TGT_MPATH, params, size, uuid,
280 dm_map_present (const char * str)
286 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
289 if (!dm_task_set_name(dmt, str))
292 dm_task_no_open_count(dmt);
294 if (!dm_task_run(dmt))
297 if (!dm_task_get_info(dmt, &info))
303 dm_task_destroy(dmt);
308 dm_get_map(char * name, unsigned long long * size, char * outparams)
313 uint64_t start, length;
314 char *target_type = NULL;
317 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
320 if (!dm_task_set_name(dmt, name))
323 dm_task_no_open_count(dmt);
325 if (!dm_task_run(dmt))
328 /* Fetch 1st target */
329 next = dm_get_next_target(dmt, next, &start, &length,
330 &target_type, ¶ms);
335 if (snprintf(outparams, PARAMS_SIZE, "%s", params) <= PARAMS_SIZE)
338 dm_task_destroy(dmt);
343 dm_get_uuid(char *name, char *uuid)
349 dmt = dm_task_create(DM_DEVICE_INFO);
353 if (!dm_task_set_name (dmt, name))
356 if (!dm_task_run(dmt))
359 uuidtmp = dm_task_get_uuid(dmt);
361 if (!strncmp(uuidtmp, UUID_PREFIX, UUID_PREFIX_LEN))
362 strcpy(uuid, uuidtmp + UUID_PREFIX_LEN);
364 strcpy(uuid, uuidtmp);
371 dm_task_destroy(dmt);
376 dm_get_status(char * name, char * outstatus)
381 uint64_t start, length;
385 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
388 if (!dm_task_set_name(dmt, name))
391 dm_task_no_open_count(dmt);
393 if (!dm_task_run(dmt))
396 /* Fetch 1st target */
397 next = dm_get_next_target(dmt, next, &start, &length,
398 &target_type, &status);
400 if (snprintf(outstatus, PARAMS_SIZE, "%s", status) <= PARAMS_SIZE)
404 condlog(0, "%s: error getting map status string", name);
406 dm_task_destroy(dmt);
417 dm_type(const char * name, char * type)
422 uint64_t start, length;
423 char *target_type = NULL;
426 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
429 if (!dm_task_set_name(dmt, name))
432 dm_task_no_open_count(dmt);
434 if (!dm_task_run(dmt))
437 /* Fetch 1st target */
438 next = dm_get_next_target(dmt, next, &start, &length,
439 &target_type, ¶ms);
443 else if (!strcmp(target_type, type))
447 dm_task_destroy(dmt);
452 dm_dev_t (const char * mapname, char * dev_t, int len)
458 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
461 if (!dm_task_set_name(dmt, mapname))
464 if (!dm_task_run(dmt))
467 if (!dm_task_get_info(dmt, &info))
471 if (snprintf(dev_t, len, "%i:%i", info.major, info.minor) > len)
476 dm_task_destroy(dmt);
481 dm_get_opencount (const char * mapname)
487 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
490 if (!dm_task_set_name(dmt, mapname))
493 if (!dm_task_run(dmt))
496 if (!dm_task_get_info(dmt, &info))
501 dm_task_destroy(dmt);
506 dm_get_minor (char * mapname)
512 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
515 if (!dm_task_set_name(dmt, mapname))
518 if (!dm_task_run(dmt))
521 if (!dm_task_get_info(dmt, &info))
526 dm_task_destroy(dmt);
531 dm_flush_map (const char * mapname)
535 if (!dm_map_present(mapname))
538 if (dm_type(mapname, TGT_MPATH) <= 0)
539 return 0; /* nothing to do */
541 if (dm_remove_partmaps(mapname))
544 if (dm_get_opencount(mapname)) {
545 condlog(2, "%s: map in use", mapname);
549 r = dm_simplecmd_flush(DM_DEVICE_REMOVE, mapname);
552 condlog(4, "multipath map %s removed", mapname);
563 struct dm_names *names;
566 if (!(dmt = dm_task_create (DM_DEVICE_LIST)))
569 dm_task_no_open_count(dmt);
571 if (!dm_task_run (dmt))
574 if (!(names = dm_task_get_names (dmt)))
581 r |= dm_flush_map(names->name);
583 names = (void *) names + next;
587 dm_task_destroy (dmt);
592 dm_message(char * mapname, char * message)
597 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
600 if (!dm_task_set_name(dmt, mapname))
603 if (!dm_task_set_sector(dmt, 0))
606 if (!dm_task_set_message(dmt, message))
609 dm_task_no_open_count(dmt);
611 if (!dm_task_run(dmt))
617 condlog(0, "DM message failed [%s]", message);
619 dm_task_destroy(dmt);
624 dm_fail_path(char * mapname, char * path)
628 if (snprintf(message, 32, "fail_path %s\n", path) > 32)
631 return dm_message(mapname, message);
635 dm_reinstate_path(char * mapname, char * path)
639 if (snprintf(message, 32, "reinstate_path %s\n", path) > 32)
642 return dm_message(mapname, message);
646 dm_queue_if_no_path(char *mapname, int enable)
651 message = "queue_if_no_path\n";
653 message = "fail_if_no_path\n";
655 return dm_message(mapname, message);
659 dm_set_pg_timeout(char *mapname, int timeout_val)
663 if (snprintf(message, 24, "set_pg_timeout %d", timeout_val) >= 24)
665 return dm_message(mapname, message);
669 dm_groupmsg (char * msg, char * mapname, int index)
673 if (snprintf(message, 32, "%s_group %i\n", msg, index) > 32)
676 return dm_message(mapname, message);
680 dm_switchgroup(char * mapname, int index)
682 return dm_groupmsg("switch", mapname, index);
686 dm_enablegroup(char * mapname, int index)
688 return dm_groupmsg("enable", mapname, index);
692 dm_disablegroup(char * mapname, int index)
694 return dm_groupmsg("disable", mapname, index);
698 dm_get_maps (vector mp)
700 struct multipath * mpp;
704 struct dm_names *names;
710 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
713 dm_task_no_open_count(dmt);
715 if (!dm_task_run(dmt))
718 if (!(names = dm_task_get_names(dmt)))
722 r = 0; /* this is perfectly valid */
727 info = dm_type(names->name, TGT_MPATH);
732 mpp = alloc_multipath();
737 mpp->alias = STRDUP(names->name);
743 if (dm_get_map(names->name, &mpp->size, mpp->params))
746 if (dm_get_status(names->name, mpp->status))
749 dm_get_uuid(names->name, mpp->wwid);
750 dm_get_info(names->name, &mpp->dmi);
753 if (!vector_alloc_slot(mp))
756 vector_set_slot(mp, mpp);
760 names = (void *) names + next;
766 free_multipath(mpp, KEEP_PATHS);
768 dm_task_destroy (dmt);
773 dm_get_name(char *uuid, char *name)
776 struct multipath *mpp;
779 vec = vector_alloc();
784 if (dm_get_maps(vec)) {
788 vector_foreach_slot(vec, mpp, i) {
789 if (!strcmp(uuid, mpp->wwid)) {
790 strcpy(name, mpp->alias);
796 vector_foreach_slot(vec, mpp, i) {
797 free_multipath(mpp, KEEP_PATHS);
804 dm_geteventnr (char *name)
809 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
812 if (!dm_task_set_name(dmt, name))
815 dm_task_no_open_count(dmt);
817 if (!dm_task_run(dmt))
820 if (!dm_task_get_info(dmt, &info)) {
831 dm_task_destroy(dmt);
833 return info.event_nr;
837 dm_mapname(int major, int minor)
839 char * response = NULL;
843 int loop = MAX_WAIT * LOOPS_PER_SEC;
845 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
848 if (!dm_task_set_major(dmt, major) ||
849 !dm_task_set_minor(dmt, minor))
852 dm_task_no_open_count(dmt);
855 * device map might not be ready when we get here from
856 * daemon uev_trigger -> uev_add_map
859 r = dm_task_run(dmt);
864 usleep(1000 * 1000 / LOOPS_PER_SEC);
868 condlog(0, "%i:%i: timeout fetching map name", major, minor);
872 map = dm_task_get_name(dmt);
873 if (map && strlen(map))
874 response = STRDUP((char *)dm_task_get_name(dmt));
876 dm_task_destroy(dmt);
879 dm_task_destroy(dmt);
880 condlog(0, "%i:%i: error fetching map name", major, minor);
885 dm_remove_partmaps (const char * mapname)
888 struct dm_names *names;
890 char params[PARAMS_SIZE];
891 unsigned long long size;
895 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
898 dm_task_no_open_count(dmt);
900 if (!dm_task_run(dmt))
903 if (!(names = dm_task_get_names(dmt)))
907 r = 0; /* this is perfectly valid */
911 if (dm_dev_t(mapname, &dev_t[0], 32))
917 * if devmap target is "linear"
919 (dm_type(names->name, TGT_PART) > 0) &&
922 * and the multipath mapname and the part mapname start
925 !strncmp(names->name, mapname, strlen(mapname)) &&
928 * and the opencount is 0 for us to allow removal
930 !dm_get_opencount(names->name) &&
933 * and we can fetch the map table from the kernel
935 !dm_get_map(names->name, &size, ¶ms[0]) &&
938 * and the table maps over the multipath map
940 strstr(params, dev_t)
943 * then it's a kpartx generated partition.
946 condlog(4, "partition map %s removed",
948 dm_simplecmd_flush(DM_DEVICE_REMOVE, names->name);
952 names = (void *) names + next;
957 dm_task_destroy (dmt);
961 static struct dm_info *
964 return MALLOC(sizeof(struct dm_info));
968 dm_get_info (char * mapname, struct dm_info ** dmi)
971 struct dm_task *dmt = NULL;
977 *dmi = alloc_dminfo();
982 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
985 if (!dm_task_set_name(dmt, mapname))
988 dm_task_no_open_count(dmt);
990 if (!dm_task_run(dmt))
993 if (!dm_task_get_info(dmt, *dmi))
999 memset(*dmi, 0, sizeof(struct dm_info));
1005 dm_task_destroy(dmt);
1011 dm_rename_partmaps (char * old, char * new)
1013 struct dm_task *dmt;
1014 struct dm_names *names;
1016 char buff[PARAMS_SIZE];
1017 unsigned long long size;
1021 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
1024 dm_task_no_open_count(dmt);
1026 if (!dm_task_run(dmt))
1029 if (!(names = dm_task_get_names(dmt)))
1033 r = 0; /* this is perfectly valid */
1037 if (dm_dev_t(old, &dev_t[0], 32))
1043 * if devmap target is "linear"
1045 (dm_type(names->name, TGT_PART) > 0) &&
1048 * and the multipath mapname and the part mapname start
1051 !strncmp(names->name, old, strlen(old)) &&
1054 * and we can fetch the map table from the kernel
1056 !dm_get_map(names->name, &size, &buff[0]) &&
1059 * and the table maps over the multipath map
1064 * then it's a kpartx generated partition.
1067 snprintf(buff, PARAMS_SIZE, "%s%s",
1068 new, names->name + strlen(old));
1069 dm_rename(names->name, buff);
1070 condlog(4, "partition map %s renamed",
1075 names = (void *) names + next;
1080 dm_task_destroy (dmt);
1085 dm_rename (char * old, char * new)
1088 struct dm_task *dmt;
1090 if (dm_rename_partmaps(old, new))
1093 if (!(dmt = dm_task_create(DM_DEVICE_RENAME)))
1096 if (!dm_task_set_name(dmt, old))
1099 if (!dm_task_set_newname(dmt, new))
1102 dm_task_no_open_count(dmt);
1104 if (!dm_task_run(dmt))
1109 dm_task_destroy(dmt);