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_uuid(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 if (!strncmp(uuidtmp, UUID_PREFIX, UUID_PREFIX_LEN))
450 strcpy(uuid, uuidtmp + UUID_PREFIX_LEN);
452 strcpy(uuid, uuidtmp);
459 dm_task_destroy(dmt);
464 dm_get_status(char * name, char * outstatus)
469 uint64_t start, length;
473 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
476 if (!dm_task_set_name(dmt, name))
479 dm_task_no_open_count(dmt);
481 if (!dm_task_run(dmt))
484 /* Fetch 1st target */
485 next = dm_get_next_target(dmt, next, &start, &length,
486 &target_type, &status);
488 if (snprintf(outstatus, PARAMS_SIZE, "%s", status) <= PARAMS_SIZE)
492 condlog(0, "%s: error getting map status string", name);
494 dm_task_destroy(dmt);
505 dm_type(const char * name, char * type)
510 uint64_t start, length;
511 char *target_type = NULL;
514 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
517 if (!dm_task_set_name(dmt, name))
520 dm_task_no_open_count(dmt);
522 if (!dm_task_run(dmt))
525 /* Fetch 1st target */
526 next = dm_get_next_target(dmt, next, &start, &length,
527 &target_type, ¶ms);
531 else if (!strcmp(target_type, type))
535 dm_task_destroy(dmt);
540 dm_dev_t (const char * mapname, char * dev_t, int len)
546 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
549 if (!dm_task_set_name(dmt, mapname))
552 if (!dm_task_run(dmt))
555 if (!dm_task_get_info(dmt, &info))
559 if (snprintf(dev_t, len, "%i:%i", info.major, info.minor) > len)
564 dm_task_destroy(dmt);
569 dm_get_opencount (const char * mapname)
575 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
578 if (!dm_task_set_name(dmt, mapname))
581 if (!dm_task_run(dmt))
584 if (!dm_task_get_info(dmt, &info))
589 dm_task_destroy(dmt);
594 dm_get_minor (char * mapname)
600 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
603 if (!dm_task_set_name(dmt, mapname))
606 if (!dm_task_run(dmt))
609 if (!dm_task_get_info(dmt, &info))
614 dm_task_destroy(dmt);
619 _dm_flush_map (const char * mapname, int need_sync)
623 if (!dm_map_present(mapname))
626 if (dm_type(mapname, TGT_MPATH) <= 0)
627 return 0; /* nothing to do */
629 if (dm_remove_partmaps(mapname, need_sync))
632 if (dm_get_opencount(mapname)) {
633 condlog(2, "%s: map in use", mapname);
637 r = dm_simplecmd_flush(DM_DEVICE_REMOVE, mapname, need_sync);
640 condlog(4, "multipath map %s removed", mapname);
651 struct dm_names *names;
654 if (!(dmt = dm_task_create (DM_DEVICE_LIST)))
657 dm_task_no_open_count(dmt);
659 if (!dm_task_run (dmt))
662 if (!(names = dm_task_get_names (dmt)))
669 r |= dm_flush_map(names->name);
671 names = (void *) names + next;
675 dm_task_destroy (dmt);
680 dm_message(char * mapname, char * message)
685 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
688 if (!dm_task_set_name(dmt, mapname))
691 if (!dm_task_set_sector(dmt, 0))
694 if (!dm_task_set_message(dmt, message))
697 dm_task_no_open_count(dmt);
699 if (!dm_task_run(dmt))
705 condlog(0, "DM message failed [%s]", message);
707 dm_task_destroy(dmt);
712 dm_fail_path(char * mapname, char * path)
716 if (snprintf(message, 32, "fail_path %s\n", path) > 32)
719 return dm_message(mapname, message);
723 dm_reinstate_path(char * mapname, char * path)
727 if (snprintf(message, 32, "reinstate_path %s\n", path) > 32)
730 return dm_message(mapname, message);
734 dm_queue_if_no_path(char *mapname, int enable)
739 message = "queue_if_no_path\n";
741 message = "fail_if_no_path\n";
743 return dm_message(mapname, message);
747 dm_set_pg_timeout(char *mapname, int timeout_val)
751 if (snprintf(message, 24, "set_pg_timeout %d", timeout_val) >= 24)
753 return dm_message(mapname, message);
757 dm_groupmsg (char * msg, char * mapname, int index)
761 if (snprintf(message, 32, "%s_group %i\n", msg, index) > 32)
764 return dm_message(mapname, message);
768 dm_switchgroup(char * mapname, int index)
770 return dm_groupmsg("switch", mapname, index);
774 dm_enablegroup(char * mapname, int index)
776 return dm_groupmsg("enable", mapname, index);
780 dm_disablegroup(char * mapname, int index)
782 return dm_groupmsg("disable", mapname, index);
786 dm_get_maps (vector mp)
788 struct multipath * mpp;
792 struct dm_names *names;
798 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
801 dm_task_no_open_count(dmt);
803 if (!dm_task_run(dmt))
806 if (!(names = dm_task_get_names(dmt)))
810 r = 0; /* this is perfectly valid */
815 info = dm_type(names->name, TGT_MPATH);
820 mpp = alloc_multipath();
825 mpp->alias = STRDUP(names->name);
831 if (dm_get_map(names->name, &mpp->size, NULL))
834 if (dm_get_status(names->name, mpp->status))
837 dm_get_uuid(names->name, mpp->wwid);
838 dm_get_info(names->name, &mpp->dmi);
841 if (!vector_alloc_slot(mp))
844 vector_set_slot(mp, mpp);
848 names = (void *) names + next;
854 free_multipath(mpp, KEEP_PATHS);
856 dm_task_destroy (dmt);
861 dm_get_name(char *uuid)
865 char *prefixed_uuid, *name = NULL;
868 dmt = dm_task_create(DM_DEVICE_INFO);
872 prefixed_uuid = MALLOC(UUID_PREFIX_LEN + strlen(uuid) + 1);
873 if (!prefixed_uuid) {
874 condlog(0, "cannot create prefixed uuid : %s\n",
878 sprintf(prefixed_uuid, UUID_PREFIX "%s", uuid);
879 if (!dm_task_set_uuid(dmt, prefixed_uuid))
882 if (!dm_task_run(dmt))
885 if (!dm_task_get_info(dmt, &info) || !info.exists)
888 nametmp = dm_task_get_name(dmt);
889 if (nametmp && strlen(nametmp)) {
890 name = MALLOC(strlen(nametmp) + 1);
892 strcpy(name, nametmp);
894 condlog(2, "%s: no device-mapper name found", uuid);
900 dm_task_destroy(dmt);
906 dm_geteventnr (char *name)
911 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
914 if (!dm_task_set_name(dmt, name))
917 dm_task_no_open_count(dmt);
919 if (!dm_task_run(dmt))
922 if (!dm_task_get_info(dmt, &info)) {
933 dm_task_destroy(dmt);
935 return info.event_nr;
939 dm_mapname(int major, int minor)
941 char * response = NULL;
945 int loop = MAX_WAIT * LOOPS_PER_SEC;
947 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
950 if (!dm_task_set_major(dmt, major) ||
951 !dm_task_set_minor(dmt, minor))
954 dm_task_no_open_count(dmt);
957 * device map might not be ready when we get here from
958 * daemon uev_trigger -> uev_add_map
961 r = dm_task_run(dmt);
966 usleep(1000 * 1000 / LOOPS_PER_SEC);
970 condlog(0, "%i:%i: timeout fetching map name", major, minor);
974 map = dm_task_get_name(dmt);
975 if (map && strlen(map))
976 response = STRDUP((char *)dm_task_get_name(dmt));
978 dm_task_destroy(dmt);
981 dm_task_destroy(dmt);
982 condlog(0, "%i:%i: error fetching map name", major, minor);
987 dm_remove_partmaps (const char * mapname, int need_sync)
990 struct dm_names *names;
992 char params[PARAMS_SIZE];
993 unsigned long long size;
997 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
1000 dm_task_no_open_count(dmt);
1002 if (!dm_task_run(dmt))
1005 if (!(names = dm_task_get_names(dmt)))
1009 r = 0; /* this is perfectly valid */
1013 if (dm_dev_t(mapname, &dev_t[0], 32))
1019 * if devmap target is "linear"
1021 (dm_type(names->name, TGT_PART) > 0) &&
1024 * and the multipath mapname and the part mapname start
1027 !strncmp(names->name, mapname, strlen(mapname)) &&
1030 * and the opencount is 0 for us to allow removal
1032 !dm_get_opencount(names->name) &&
1035 * and we can fetch the map table from the kernel
1037 !dm_get_map(names->name, &size, ¶ms[0]) &&
1040 * and the table maps over the multipath map
1042 strstr(params, dev_t)
1045 * then it's a kpartx generated partition.
1048 condlog(4, "partition map %s removed",
1050 dm_simplecmd_flush(DM_DEVICE_REMOVE, names->name, need_sync);
1054 names = (void *) names + next;
1059 dm_task_destroy (dmt);
1063 static struct dm_info *
1066 return MALLOC(sizeof(struct dm_info));
1070 dm_get_info (char * mapname, struct dm_info ** dmi)
1073 struct dm_task *dmt = NULL;
1079 *dmi = alloc_dminfo();
1084 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
1087 if (!dm_task_set_name(dmt, mapname))
1090 dm_task_no_open_count(dmt);
1092 if (!dm_task_run(dmt))
1095 if (!dm_task_get_info(dmt, *dmi))
1101 memset(*dmi, 0, sizeof(struct dm_info));
1107 dm_task_destroy(dmt);
1113 dm_rename_partmaps (char * old, char * new)
1115 struct dm_task *dmt;
1116 struct dm_names *names;
1118 char buff[PARAMS_SIZE];
1119 unsigned long long size;
1123 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
1126 dm_task_no_open_count(dmt);
1128 if (!dm_task_run(dmt))
1131 if (!(names = dm_task_get_names(dmt)))
1135 r = 0; /* this is perfectly valid */
1139 if (dm_dev_t(old, &dev_t[0], 32))
1145 * if devmap target is "linear"
1147 (dm_type(names->name, TGT_PART) > 0) &&
1150 * and the multipath mapname and the part mapname start
1153 !strncmp(names->name, old, strlen(old)) &&
1156 * and we can fetch the map table from the kernel
1158 !dm_get_map(names->name, &size, &buff[0]) &&
1161 * and the table maps over the multipath map
1166 * then it's a kpartx generated partition.
1169 snprintf(buff, PARAMS_SIZE, "%s%s",
1170 new, names->name + strlen(old));
1171 dm_rename(names->name, buff);
1172 condlog(4, "partition map %s renamed",
1177 names = (void *) names + next;
1182 dm_task_destroy (dmt);
1187 dm_rename (char * old, char * new)
1190 struct dm_task *dmt;
1192 if (dm_rename_partmaps(old, new))
1195 if (!(dmt = dm_task_create(DM_DEVICE_RENAME)))
1198 if (!dm_task_set_name(dmt, old))
1201 if (!dm_task_set_newname(dmt, new))
1204 dm_task_no_open_count(dmt);
1206 if (!dm_task_set_cookie(dmt, &conf->cookie, 0))
1208 if (!dm_task_run(dmt))
1213 dm_task_destroy(dmt);
1217 int dm_setgeometry(struct multipath *mpp)
1219 struct dm_task *dmt;
1221 char heads[4], sectors[4];
1222 char cylinders[10], start[32];
1228 pp = first_path(mpp);
1230 condlog(3, "%s: no path for geometry", mpp->alias);
1233 if (pp->geom.cylinders == 0 ||
1234 pp->geom.heads == 0 ||
1235 pp->geom.sectors == 0) {
1236 condlog(3, "%s: invalid geometry on %s", mpp->alias, pp->dev);
1240 if (!(dmt = dm_task_create(DM_DEVICE_SET_GEOMETRY)))
1243 if (!dm_task_set_name(dmt, mpp->alias))
1246 dm_task_no_open_count(dmt);
1248 /* What a sick interface ... */
1249 snprintf(heads, 4, "%u", pp->geom.heads);
1250 snprintf(sectors, 4, "%u", pp->geom.sectors);
1251 snprintf(cylinders, 10, "%u", pp->geom.cylinders);
1252 snprintf(start, 32, "%lu", pp->geom.start);
1253 if (!dm_task_set_geometry(dmt, cylinders, heads, sectors, start)) {
1254 condlog(3, "%s: Failed to set geometry", mpp->alias);
1258 r = dm_task_run(dmt);
1260 dm_task_destroy(dmt);