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
34 #ifndef LIBDM_API_COOKIE
35 static inline int dm_task_set_cookie(struct dm_task *dmt, uint32_t *c, int a)
40 void udev_wait(unsigned int c)
44 void udev_set_sync_support(int c)
48 void udev_wait(unsigned int c)
53 void udev_set_sync_support(int c)
55 dm_udev_set_sync_support(c);
60 dm_write_log (int level, const char *file, int line, const char *f, ...)
68 thres = (conf) ? conf->verbosity : 0;
69 if (thres <= 3 || level > thres)
74 time_t t = time(NULL);
75 struct tm *tb = localtime(&t);
78 strftime(buff, sizeof(buff), "%b %d %H:%M:%S", tb);
79 buff[sizeof(buff)-1] = '\0';
81 fprintf(stdout, "%s | ", buff);
82 fprintf(stdout, "libdevmapper: %s(%i): ", file, line);
83 vfprintf(stdout, f, ap);
84 fprintf(stdout, "\n");
86 condlog(level, "libdevmapper: %s(%i): ", file, line);
87 log_safe(level + 3, f, ap);
96 dm_log_init(&dm_write_log);
97 dm_log_init_verbose(conf ? conf->verbosity + 3 : 0);
100 #define VERSION_GE(v, minv) ( \
101 (v[0] > minv[0]) || \
102 ((v[0] == minv[0]) && (v[1] > minv[1])) || \
103 ((v[0] == minv[0]) && (v[1] == minv[1]) && (v[2] >= minv[2])) \
111 #ifdef LIBDM_API_COOKIE
112 int minv[3] = {1, 2, 38};
114 int minv[3] = {1, 2, 8};
117 dm_get_library_version(version, sizeof(version));
118 condlog(3, "libdevmapper version %s", version);
119 sscanf(version, "%d.%d.%d ", &v[0], &v[1], &v[2]);
121 if VERSION_GE(v, minv)
123 condlog(0, "libdevmapper version must be >= %d.%.2d.%.2d",
124 minv[0], minv[1], minv[2]);
129 dm_drv_version (unsigned int * version, char * str)
133 struct dm_versions *target;
134 struct dm_versions *last_target;
137 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
140 dm_task_no_open_count(dmt);
142 if (!dm_task_run(dmt)) {
143 condlog(0, "Can not communicate with kernel DM");
146 target = dm_task_get_versions(dmt);
149 last_target = target;
150 if (!strncmp(str, target->name, strlen(str))) {
154 target = (void *) target + target->next;
155 } while (last_target != target);
158 condlog(0, "DM %s kernel driver not loaded", str);
167 dm_task_destroy(dmt);
174 unsigned int minv_dmrq[3] = {1, 1, 0};
175 unsigned int version[3] = {0, 0, 0};
176 unsigned int * v = version;
178 if (dm_drv_version(v, TGT_MPATH)) {
179 /* in doubt return least capable */
183 /* test request based multipath capability */
184 if VERSION_GE(v, minv_dmrq) {
185 condlog(3, "activate request-based multipathing mode "
186 "(driver >= v%u.%u.%u)",
187 minv_dmrq[0], minv_dmrq[1], minv_dmrq[2]);
196 unsigned int minv[3] = {1, 0, 3};
197 unsigned int version[3] = {0, 0, 0};
198 unsigned int * v = version;
200 if (dm_drv_version(v, TGT_MPATH)) {
201 /* in doubt return not capable */
205 /* test request based multipath capability */
206 condlog(3, "DM multipath kernel driver v%u.%u.%u",
209 if VERSION_GE(v, minv)
212 condlog(0, "DM multipath kernel driver must be >= v%u.%u.%u",
213 minv[0], minv[1], minv[2]);
222 return dm_drv_prereq();
226 dm_simplecmd (int task, const char *name, int no_flush, int need_sync) {
228 int udev_wait_flag = (need_sync && (task == DM_DEVICE_RESUME ||
229 task == DM_DEVICE_REMOVE));
232 if (!(dmt = dm_task_create (task)))
235 if (!dm_task_set_name (dmt, name))
238 dm_task_no_open_count(dmt);
239 dm_task_skip_lockfs(dmt); /* for DM_DEVICE_RESUME */
240 #ifdef LIBDM_API_FLUSH
242 dm_task_no_flush(dmt); /* for DM_DEVICE_SUSPEND/RESUME */
245 if (udev_wait_flag && !dm_task_set_cookie(dmt, &conf->cookie, 0))
247 r = dm_task_run (dmt);
250 dm_task_destroy (dmt);
255 dm_simplecmd_flush (int task, const char *name, int needsync) {
256 return dm_simplecmd(task, name, 0, needsync);
260 dm_simplecmd_noflush (int task, const char *name) {
261 return dm_simplecmd(task, name, 1, 1);
265 dm_addmap (int task, const char *target, struct multipath *mpp, char * params,
266 int use_uuid, int ro) {
269 char *prefixed_uuid = NULL;
271 if (!(dmt = dm_task_create (task)))
274 if (!dm_task_set_name (dmt, mpp->alias))
277 if (!dm_task_add_target (dmt, 0, mpp->size, target, params))
283 if (use_uuid && mpp->wwid){
284 prefixed_uuid = MALLOC(UUID_PREFIX_LEN + strlen(mpp->wwid) + 1);
285 if (!prefixed_uuid) {
286 condlog(0, "cannot create prefixed uuid : %s\n",
290 sprintf(prefixed_uuid, UUID_PREFIX "%s", mpp->wwid);
291 if (!dm_task_set_uuid(dmt, prefixed_uuid))
295 if (mpp->attribute_flags & (1 << ATTR_MODE) &&
296 !dm_task_set_mode(dmt, mpp->mode))
298 if (mpp->attribute_flags & (1 << ATTR_UID) &&
299 !dm_task_set_uid(dmt, mpp->uid))
301 if (mpp->attribute_flags & (1 << ATTR_GID) &&
302 !dm_task_set_gid(dmt, mpp->gid))
304 condlog(4, "%s: addmap [0 %llu %s %s]\n", mpp->alias, mpp->size,
307 dm_task_no_open_count(dmt);
309 if (task == DM_DEVICE_CREATE &&
310 !dm_task_set_cookie(dmt, &conf->cookie, 0))
312 r = dm_task_run (dmt);
319 dm_task_destroy (dmt);
325 _dm_addmap_create (struct multipath *mpp, char * params, int ro) {
327 r = dm_addmap(DM_DEVICE_CREATE, TGT_MPATH, mpp, params, 1, ro);
329 * DM_DEVICE_CREATE is actually DM_DEV_CREATE + DM_TABLE_LOAD.
330 * Failing the second part leaves an empty map. Clean it up.
332 if (!r && dm_map_present(mpp->alias)) {
333 condlog(3, "%s: failed to load map (a path might be in use)",
335 dm_flush_map_nosync(mpp->alias);
344 dm_addmap_create (struct multipath *mpp, char *params) {
345 return _dm_addmap_create(mpp, params, ADDMAP_RW);
349 dm_addmap_create_ro (struct multipath *mpp, char *params) {
350 return _dm_addmap_create(mpp, params, ADDMAP_RO);
354 dm_addmap_reload (struct multipath *mpp, char *params) {
355 return dm_addmap(DM_DEVICE_RELOAD, TGT_MPATH, mpp, params, 0, ADDMAP_RW);
359 dm_addmap_reload_ro (struct multipath *mpp, char *params) {
360 return dm_addmap(DM_DEVICE_RELOAD, TGT_MPATH, mpp, params, 0, ADDMAP_RO);
364 dm_map_present (const char * str)
370 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
373 if (!dm_task_set_name(dmt, str))
376 dm_task_no_open_count(dmt);
378 if (!dm_task_run(dmt))
381 if (!dm_task_get_info(dmt, &info))
387 dm_task_destroy(dmt);
392 dm_get_map(char * name, unsigned long long * size, char * outparams)
397 uint64_t start, length;
398 char *target_type = NULL;
401 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
404 if (!dm_task_set_name(dmt, name))
407 dm_task_no_open_count(dmt);
409 if (!dm_task_run(dmt))
412 /* Fetch 1st target */
413 next = dm_get_next_target(dmt, next, &start, &length,
414 &target_type, ¶ms);
423 if (snprintf(outparams, PARAMS_SIZE, "%s", params) <= PARAMS_SIZE)
426 dm_task_destroy(dmt);
431 dm_get_prefixed_uuid(const char *name, char *uuid)
437 dmt = dm_task_create(DM_DEVICE_INFO);
441 if (!dm_task_set_name (dmt, name))
444 if (!dm_task_run(dmt))
447 uuidtmp = dm_task_get_uuid(dmt);
449 strcpy(uuid, uuidtmp);
455 dm_task_destroy(dmt);
460 dm_get_uuid(char *name, char *uuid)
462 char uuidtmp[WWID_SIZE];
464 if (dm_get_prefixed_uuid(name, uuidtmp))
467 if (!strncmp(uuidtmp, UUID_PREFIX, UUID_PREFIX_LEN))
468 strcpy(uuid, uuidtmp + UUID_PREFIX_LEN);
470 strcpy(uuid, uuidtmp);
477 * 0 : if both uuids end with same suffix which starts with UUID_PREFIX
481 dm_compare_uuid(const char* mapname1, const char* mapname2)
484 char uuid1[WWID_SIZE], uuid2[WWID_SIZE];
486 if (dm_get_prefixed_uuid(mapname1, uuid1))
489 if (dm_get_prefixed_uuid(mapname2, uuid2))
492 p1 = strstr(uuid1, UUID_PREFIX);
493 p2 = strstr(uuid2, UUID_PREFIX);
494 if (p1 && p2 && !strcmp(p1, p2))
501 dm_get_status(char * name, char * outstatus)
506 uint64_t start, length;
510 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
513 if (!dm_task_set_name(dmt, name))
516 dm_task_no_open_count(dmt);
518 if (!dm_task_run(dmt))
521 /* Fetch 1st target */
522 next = dm_get_next_target(dmt, next, &start, &length,
523 &target_type, &status);
525 if (snprintf(outstatus, PARAMS_SIZE, "%s", status) <= PARAMS_SIZE)
529 condlog(0, "%s: error getting map status string", name);
531 dm_task_destroy(dmt);
542 dm_type(const char * name, char * type)
547 uint64_t start, length;
548 char *target_type = NULL;
551 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
554 if (!dm_task_set_name(dmt, name))
557 dm_task_no_open_count(dmt);
559 if (!dm_task_run(dmt))
562 /* Fetch 1st target */
563 next = dm_get_next_target(dmt, next, &start, &length,
564 &target_type, ¶ms);
568 else if (!strcmp(target_type, type))
572 dm_task_destroy(dmt);
577 dm_dev_t (const char * mapname, char * dev_t, int len)
583 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
586 if (!dm_task_set_name(dmt, mapname))
589 if (!dm_task_run(dmt))
592 if (!dm_task_get_info(dmt, &info))
596 if (snprintf(dev_t, len, "%i:%i", info.major, info.minor) > len)
601 dm_task_destroy(dmt);
606 dm_get_opencount (const char * mapname)
612 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
615 if (!dm_task_set_name(dmt, mapname))
618 if (!dm_task_run(dmt))
621 if (!dm_task_get_info(dmt, &info))
626 dm_task_destroy(dmt);
631 dm_get_minor (char * mapname)
637 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
640 if (!dm_task_set_name(dmt, mapname))
643 if (!dm_task_run(dmt))
646 if (!dm_task_get_info(dmt, &info))
651 dm_task_destroy(dmt);
656 _dm_flush_map (const char * mapname, int need_sync)
660 if (!dm_map_present(mapname))
663 if (dm_type(mapname, TGT_MPATH) <= 0)
664 return 0; /* nothing to do */
666 if (dm_remove_partmaps(mapname, need_sync))
669 if (dm_get_opencount(mapname)) {
670 condlog(2, "%s: map in use", mapname);
674 r = dm_simplecmd_flush(DM_DEVICE_REMOVE, mapname, need_sync);
677 condlog(4, "multipath map %s removed", mapname);
688 struct dm_names *names;
691 if (!(dmt = dm_task_create (DM_DEVICE_LIST)))
694 dm_task_no_open_count(dmt);
696 if (!dm_task_run (dmt))
699 if (!(names = dm_task_get_names (dmt)))
706 r |= dm_flush_map(names->name);
708 names = (void *) names + next;
712 dm_task_destroy (dmt);
717 dm_message(char * mapname, char * message)
722 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
725 if (!dm_task_set_name(dmt, mapname))
728 if (!dm_task_set_sector(dmt, 0))
731 if (!dm_task_set_message(dmt, message))
734 dm_task_no_open_count(dmt);
736 if (!dm_task_run(dmt))
742 condlog(0, "DM message failed [%s]", message);
744 dm_task_destroy(dmt);
749 dm_fail_path(char * mapname, char * path)
753 if (snprintf(message, 32, "fail_path %s\n", path) > 32)
756 return dm_message(mapname, message);
760 dm_reinstate_path(char * mapname, char * path)
764 if (snprintf(message, 32, "reinstate_path %s\n", path) > 32)
767 return dm_message(mapname, message);
771 dm_queue_if_no_path(char *mapname, int enable)
776 message = "queue_if_no_path\n";
778 message = "fail_if_no_path\n";
780 return dm_message(mapname, message);
784 dm_set_pg_timeout(char *mapname, int timeout_val)
788 if (snprintf(message, 24, "set_pg_timeout %d", timeout_val) >= 24)
790 return dm_message(mapname, message);
794 dm_groupmsg (char * msg, char * mapname, int index)
798 if (snprintf(message, 32, "%s_group %i\n", msg, index) > 32)
801 return dm_message(mapname, message);
805 dm_switchgroup(char * mapname, int index)
807 return dm_groupmsg("switch", mapname, index);
811 dm_enablegroup(char * mapname, int index)
813 return dm_groupmsg("enable", mapname, index);
817 dm_disablegroup(char * mapname, int index)
819 return dm_groupmsg("disable", mapname, index);
823 dm_get_maps (vector mp)
825 struct multipath * mpp;
829 struct dm_names *names;
835 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
838 dm_task_no_open_count(dmt);
840 if (!dm_task_run(dmt))
843 if (!(names = dm_task_get_names(dmt)))
847 r = 0; /* this is perfectly valid */
852 info = dm_type(names->name, TGT_MPATH);
857 mpp = alloc_multipath();
862 mpp->alias = STRDUP(names->name);
868 if (dm_get_map(names->name, &mpp->size, NULL))
871 dm_get_uuid(names->name, mpp->wwid);
872 dm_get_info(names->name, &mpp->dmi);
875 if (!vector_alloc_slot(mp))
878 vector_set_slot(mp, mpp);
882 names = (void *) names + next;
888 free_multipath(mpp, KEEP_PATHS);
890 dm_task_destroy (dmt);
895 dm_get_name(char *uuid)
899 char *prefixed_uuid, *name = NULL;
902 dmt = dm_task_create(DM_DEVICE_INFO);
906 prefixed_uuid = MALLOC(UUID_PREFIX_LEN + strlen(uuid) + 1);
907 if (!prefixed_uuid) {
908 condlog(0, "cannot create prefixed uuid : %s\n",
912 sprintf(prefixed_uuid, UUID_PREFIX "%s", uuid);
913 if (!dm_task_set_uuid(dmt, prefixed_uuid))
916 if (!dm_task_run(dmt))
919 if (!dm_task_get_info(dmt, &info) || !info.exists)
922 nametmp = dm_task_get_name(dmt);
923 if (nametmp && strlen(nametmp)) {
924 name = MALLOC(strlen(nametmp) + 1);
926 strcpy(name, nametmp);
928 condlog(2, "%s: no device-mapper name found", uuid);
934 dm_task_destroy(dmt);
940 dm_geteventnr (char *name)
945 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
948 if (!dm_task_set_name(dmt, name))
951 dm_task_no_open_count(dmt);
953 if (!dm_task_run(dmt))
956 if (!dm_task_get_info(dmt, &info)) {
967 dm_task_destroy(dmt);
969 return info.event_nr;
973 dm_mapname(int major, int minor)
975 char * response = NULL;
979 int loop = MAX_WAIT * LOOPS_PER_SEC;
981 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
984 if (!dm_task_set_major(dmt, major) ||
985 !dm_task_set_minor(dmt, minor))
988 dm_task_no_open_count(dmt);
991 * device map might not be ready when we get here from
992 * daemon uev_trigger -> uev_add_map
995 r = dm_task_run(dmt);
1000 usleep(1000 * 1000 / LOOPS_PER_SEC);
1004 condlog(0, "%i:%i: timeout fetching map name", major, minor);
1008 map = dm_task_get_name(dmt);
1009 if (map && strlen(map))
1010 response = STRDUP((char *)dm_task_get_name(dmt));
1012 dm_task_destroy(dmt);
1015 dm_task_destroy(dmt);
1016 condlog(0, "%i:%i: error fetching map name", major, minor);
1021 dm_remove_partmaps (const char * mapname, int need_sync)
1023 struct dm_task *dmt;
1024 struct dm_names *names;
1026 char params[PARAMS_SIZE];
1027 unsigned long long size;
1031 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
1034 dm_task_no_open_count(dmt);
1036 if (!dm_task_run(dmt))
1039 if (!(names = dm_task_get_names(dmt)))
1043 r = 0; /* this is perfectly valid */
1047 if (dm_dev_t(mapname, &dev_t[0], 32))
1053 * if devmap target is "linear"
1055 (dm_type(names->name, TGT_PART) > 0) &&
1058 * and both uuid end with same suffix starting
1061 (!dm_compare_uuid(names->name, mapname)) &&
1064 * and we can fetch the map table from the kernel
1066 !dm_get_map(names->name, &size, ¶ms[0]) &&
1069 * and the table maps over the multipath map
1071 strstr(params, dev_t)
1074 * then it's a kpartx generated partition.
1078 * if the opencount is 0 maybe some other
1079 * partitions depend on it.
1081 if (dm_get_opencount(names->name)) {
1082 dm_remove_partmaps(names->name, need_sync);
1083 if (dm_get_opencount(names->name)) {
1084 condlog(2, "%s: map in use",
1089 condlog(4, "partition map %s removed",
1091 dm_simplecmd_flush(DM_DEVICE_REMOVE, names->name,
1096 names = (void *) names + next;
1101 dm_task_destroy (dmt);
1105 static struct dm_info *
1108 return MALLOC(sizeof(struct dm_info));
1112 dm_get_info (char * mapname, struct dm_info ** dmi)
1115 struct dm_task *dmt = NULL;
1121 *dmi = alloc_dminfo();
1126 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
1129 if (!dm_task_set_name(dmt, mapname))
1132 dm_task_no_open_count(dmt);
1134 if (!dm_task_run(dmt))
1137 if (!dm_task_get_info(dmt, *dmi))
1143 memset(*dmi, 0, sizeof(struct dm_info));
1149 dm_task_destroy(dmt);
1155 dm_rename_partmaps (char * old, char * new)
1157 struct dm_task *dmt;
1158 struct dm_names *names;
1160 char buff[PARAMS_SIZE];
1161 unsigned long long size;
1165 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
1168 dm_task_no_open_count(dmt);
1170 if (!dm_task_run(dmt))
1173 if (!(names = dm_task_get_names(dmt)))
1177 r = 0; /* this is perfectly valid */
1181 if (dm_dev_t(old, &dev_t[0], 32))
1187 * if devmap target is "linear"
1189 (dm_type(names->name, TGT_PART) > 0) &&
1192 * and the multipath mapname and the part mapname start
1195 !strncmp(names->name, old, strlen(old)) &&
1198 * and we can fetch the map table from the kernel
1200 !dm_get_map(names->name, &size, &buff[0]) &&
1203 * and the table maps over the multipath map
1208 * then it's a kpartx generated partition.
1211 snprintf(buff, PARAMS_SIZE, "%s%s",
1212 new, names->name + strlen(old));
1213 dm_rename(names->name, buff);
1214 condlog(4, "partition map %s renamed",
1219 names = (void *) names + next;
1224 dm_task_destroy (dmt);
1229 dm_rename (char * old, char * new)
1232 struct dm_task *dmt;
1234 if (dm_rename_partmaps(old, new))
1237 if (!(dmt = dm_task_create(DM_DEVICE_RENAME)))
1240 if (!dm_task_set_name(dmt, old))
1243 if (!dm_task_set_newname(dmt, new))
1246 dm_task_no_open_count(dmt);
1248 if (!dm_task_set_cookie(dmt, &conf->cookie, 0))
1250 if (!dm_task_run(dmt))
1255 dm_task_destroy(dmt);
1259 int dm_setgeometry(struct multipath *mpp)
1261 struct dm_task *dmt;
1263 char heads[4], sectors[4];
1264 char cylinders[10], start[32];
1270 pp = first_path(mpp);
1272 condlog(3, "%s: no path for geometry", mpp->alias);
1275 if (pp->geom.cylinders == 0 ||
1276 pp->geom.heads == 0 ||
1277 pp->geom.sectors == 0) {
1278 condlog(3, "%s: invalid geometry on %s", mpp->alias, pp->dev);
1282 if (!(dmt = dm_task_create(DM_DEVICE_SET_GEOMETRY)))
1285 if (!dm_task_set_name(dmt, mpp->alias))
1288 dm_task_no_open_count(dmt);
1290 /* What a sick interface ... */
1291 snprintf(heads, 4, "%u", pp->geom.heads);
1292 snprintf(sectors, 4, "%u", pp->geom.sectors);
1293 snprintf(cylinders, 10, "%u", pp->geom.cylinders);
1294 snprintf(start, 32, "%lu", pp->geom.start);
1295 if (!dm_task_set_geometry(dmt, cylinders, heads, sectors, start)) {
1296 condlog(3, "%s: Failed to set geometry", mpp->alias);
1300 r = dm_task_run(dmt);
1302 dm_task_destroy(dmt);