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>
15 #include <sys/sysmacros.h>
22 #include "devmapper.h"
26 #include "log_pthread.h"
27 #include <sys/types.h>
31 #define LOOPS_PER_SEC 5
33 #define UUID_PREFIX "mpath-"
34 #define UUID_PREFIX_LEN 6
36 static int dm_conf_verbosity;
38 #ifdef LIBDM_API_DEFERRED
39 static int dm_cancel_remove_partmaps(const char * mapname);
42 static int do_foreach_partmaps(const char * mapname,
43 int (*partmap_func)(const char *, void *),
46 #ifndef LIBDM_API_COOKIE
47 static inline int dm_task_set_cookie(struct dm_task *dmt, uint32_t *c, int a)
52 void dm_udev_wait(unsigned int c)
56 void dm_udev_set_sync_support(int c)
63 dm_write_log (int level, const char *file, int line, const char *f, ...)
71 thres = dm_conf_verbosity;
72 if (thres <= 3 || level > thres)
78 time_t t = time(NULL);
79 struct tm *tb = localtime(&t);
82 strftime(buff, sizeof(buff), "%b %d %H:%M:%S", tb);
83 buff[sizeof(buff)-1] = '\0';
85 fprintf(stdout, "%s | ", buff);
87 fprintf(stdout, "libdevmapper: %s(%i): ", file, line);
88 vfprintf(stdout, f, ap);
89 fprintf(stdout, "\n");
91 condlog(level, "libdevmapper: %s(%i): ", file, line);
92 log_safe(level + 3, f, ap);
101 dm_log_init(&dm_write_log);
102 dm_log_init_verbose(v + 3);
110 #if defined(LIBDM_API_DEFERRED)
111 int minv[3] = {1, 2, 89};
112 #elif defined(DM_SUBSYSTEM_UDEV_FLAG0)
113 int minv[3] = {1, 2, 82};
114 #elif defined(LIBDM_API_COOKIE)
115 int minv[3] = {1, 2, 38};
117 int minv[3] = {1, 2, 8};
120 dm_get_library_version(version, sizeof(version));
121 condlog(3, "libdevmapper version %s", version);
122 if (sscanf(version, "%d.%d.%d ", &v[0], &v[1], &v[2]) != 3) {
123 condlog(0, "invalid libdevmapper version %s", version);
127 if VERSION_GE(v, minv)
129 condlog(0, "libdevmapper version must be >= %d.%.2d.%.2d",
130 minv[0], minv[1], minv[2]);
135 dm_drv_version (unsigned int * version, char * str)
139 struct dm_versions *target;
140 struct dm_versions *last_target;
147 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
150 dm_task_no_open_count(dmt);
152 if (!dm_task_run(dmt)) {
153 condlog(0, "Can not communicate with kernel DM");
156 target = dm_task_get_versions(dmt);
159 last_target = target;
160 if (!strncmp(str, target->name, strlen(str))) {
164 target = (void *) target + target->next;
165 } while (last_target != target);
168 condlog(0, "DM %s kernel driver not loaded", str);
177 dm_task_destroy(dmt);
182 dm_drv_prereq (unsigned int *ver)
184 unsigned int minv[3] = {1, 0, 3};
185 unsigned int version[3] = {0, 0, 0};
186 unsigned int * v = version;
188 if (dm_drv_version(v, TGT_MPATH)) {
189 /* in doubt return not capable */
193 /* test request based multipath capability */
194 condlog(3, "DM multipath kernel driver v%u.%u.%u",
197 if (VERSION_GE(v, minv)) {
204 condlog(0, "DM multipath kernel driver must be >= v%u.%u.%u",
205 minv[0], minv[1], minv[2]);
209 static int dm_prereq(unsigned int *v)
213 return dm_drv_prereq(v);
216 static int libmp_dm_udev_sync = 0;
218 void libmp_udev_set_sync_support(int on)
220 libmp_dm_udev_sync = !!on;
223 void libmp_dm_init(void)
227 conf = get_multipath_config();
228 dm_init(conf->verbosity);
229 if (dm_prereq(conf->version))
231 put_multipath_config(conf);
232 dm_udev_set_sync_support(libmp_dm_udev_sync);
236 libmp_dm_task_create(int task)
238 static pthread_once_t dm_initialized = PTHREAD_ONCE_INIT;
240 pthread_once(&dm_initialized, libmp_dm_init);
241 return dm_task_create(task);
244 #define do_deferred(x) ((x) == DEFERRED_REMOVE_ON || (x) == DEFERRED_REMOVE_IN_PROGRESS)
247 dm_simplecmd (int task, const char *name, int no_flush, int need_sync, uint16_t udev_flags, int deferred_remove) {
249 int udev_wait_flag = ((need_sync || udev_flags) &&
250 (task == DM_DEVICE_RESUME ||
251 task == DM_DEVICE_REMOVE));
255 if (!(dmt = libmp_dm_task_create (task)))
258 if (!dm_task_set_name (dmt, name))
261 dm_task_no_open_count(dmt);
262 dm_task_skip_lockfs(dmt); /* for DM_DEVICE_RESUME */
263 #ifdef LIBDM_API_FLUSH
265 dm_task_no_flush(dmt); /* for DM_DEVICE_SUSPEND/RESUME */
267 #ifdef LIBDM_API_DEFERRED
268 if (do_deferred(deferred_remove))
269 dm_task_deferred_remove(dmt);
271 if (udev_wait_flag &&
272 !dm_task_set_cookie(dmt, &cookie,
273 DM_UDEV_DISABLE_LIBRARY_FALLBACK | udev_flags))
276 r = dm_task_run (dmt);
279 dm_udev_wait(cookie);
281 dm_task_destroy (dmt);
285 int dm_simplecmd_flush (int task, const char *name, uint16_t udev_flags)
287 return dm_simplecmd(task, name, 0, 1, udev_flags, 0);
290 int dm_simplecmd_noflush (int task, const char *name, uint16_t udev_flags)
292 return dm_simplecmd(task, name, 1, 1, udev_flags, 0);
296 dm_device_remove (const char *name, int needsync, int deferred_remove) {
297 return dm_simplecmd(DM_DEVICE_REMOVE, name, 0, needsync, 0,
302 dm_addmap (int task, const char *target, struct multipath *mpp,
303 char * params, int ro, uint16_t udev_flags) {
306 char *prefixed_uuid = NULL;
309 /* Need to add this here to allow 0 to be passed in udev_flags */
310 udev_flags |= DM_UDEV_DISABLE_LIBRARY_FALLBACK;
312 if (!(dmt = libmp_dm_task_create (task)))
315 if (!dm_task_set_name (dmt, mpp->alias))
318 if (!dm_task_add_target (dmt, 0, mpp->size, target, params))
324 if (task == DM_DEVICE_CREATE) {
325 if (strlen(mpp->wwid) > 0) {
326 prefixed_uuid = MALLOC(UUID_PREFIX_LEN +
327 strlen(mpp->wwid) + 1);
328 if (!prefixed_uuid) {
329 condlog(0, "cannot create prefixed uuid : %s",
333 sprintf(prefixed_uuid, UUID_PREFIX "%s", mpp->wwid);
334 if (!dm_task_set_uuid(dmt, prefixed_uuid))
337 dm_task_skip_lockfs(dmt);
338 #ifdef LIBDM_API_FLUSH
339 dm_task_no_flush(dmt);
343 if (mpp->attribute_flags & (1 << ATTR_MODE) &&
344 !dm_task_set_mode(dmt, mpp->mode))
346 if (mpp->attribute_flags & (1 << ATTR_UID) &&
347 !dm_task_set_uid(dmt, mpp->uid))
349 if (mpp->attribute_flags & (1 << ATTR_GID) &&
350 !dm_task_set_gid(dmt, mpp->gid))
352 condlog(4, "%s: %s [0 %llu %s %s]", mpp->alias,
353 task == DM_DEVICE_RELOAD ? "reload" : "addmap", mpp->size,
356 dm_task_no_open_count(dmt);
358 if (task == DM_DEVICE_CREATE &&
359 !dm_task_set_cookie(dmt, &cookie, udev_flags))
362 r = dm_task_run (dmt);
364 if (task == DM_DEVICE_CREATE)
365 dm_udev_wait(cookie);
371 dm_task_destroy (dmt);
376 static uint16_t build_udev_flags(const struct multipath *mpp, int reload)
378 /* DM_UDEV_DISABLE_LIBRARY_FALLBACK is added in dm_addmap */
379 return (mpp->skip_kpartx == SKIP_KPARTX_ON ?
380 MPATH_UDEV_NO_KPARTX_FLAG : 0) |
381 (mpp->nr_active == 0 ?
382 MPATH_UDEV_NO_PATHS_FLAG : 0) |
383 (reload && !mpp->force_udev_reload ?
384 MPATH_UDEV_RELOAD_FLAG : 0);
387 int dm_addmap_create (struct multipath *mpp, char * params)
390 uint16_t udev_flags = build_udev_flags(mpp, 0);
392 for (ro = 0; ro <= 1; ro++) {
395 if (dm_addmap(DM_DEVICE_CREATE, TGT_MPATH, mpp, params, ro,
399 * DM_DEVICE_CREATE is actually DM_DEV_CREATE + DM_TABLE_LOAD.
400 * Failing the second part leaves an empty map. Clean it up.
403 if (dm_map_present(mpp->alias)) {
404 condlog(3, "%s: failed to load map (a path might be in use)", mpp->alias);
405 dm_flush_map_nosync(mpp->alias);
408 condlog(3, "%s: failed to load map, error %d",
419 int dm_addmap_reload(struct multipath *mpp, char *params, int flush)
422 uint16_t udev_flags = build_udev_flags(mpp, 1);
425 * DM_DEVICE_RELOAD cannot wait on a cookie, as
426 * the cookie will only ever be released after an
427 * DM_DEVICE_RESUME. So call DM_DEVICE_RESUME
428 * after each successful call to DM_DEVICE_RELOAD.
430 if (!mpp->force_readonly)
431 r = dm_addmap(DM_DEVICE_RELOAD, TGT_MPATH, mpp, params,
434 if (!mpp->force_readonly && errno != EROFS)
436 r = dm_addmap(DM_DEVICE_RELOAD, TGT_MPATH, mpp,
437 params, ADDMAP_RO, 0);
440 r = dm_simplecmd(DM_DEVICE_RESUME, mpp->alias, !flush,
445 /* If the resume failed, dm will leave the device suspended, and
446 * drop the new table, so doing a second resume will try using
447 * the original table */
448 if (dm_is_suspended(mpp->alias))
449 dm_simplecmd(DM_DEVICE_RESUME, mpp->alias, !flush, 1,
455 do_get_info(const char *name, struct dm_info *info)
460 if (!(dmt = libmp_dm_task_create(DM_DEVICE_INFO)))
463 if (!dm_task_set_name(dmt, name))
466 dm_task_no_open_count(dmt);
468 if (!dm_task_run(dmt))
471 if (!dm_task_get_info(dmt, info))
479 dm_task_destroy(dmt);
483 int dm_map_present(const char * str)
487 return (do_get_info(str, &info) == 0);
490 int dm_get_map(const char *name, unsigned long long *size, char *outparams)
494 uint64_t start, length;
495 char *target_type = NULL;
498 if (!(dmt = libmp_dm_task_create(DM_DEVICE_TABLE)))
501 if (!dm_task_set_name(dmt, name))
504 dm_task_no_open_count(dmt);
506 if (!dm_task_run(dmt))
509 /* Fetch 1st target */
510 dm_get_next_target(dmt, NULL, &start, &length,
511 &target_type, ¶ms);
520 if (snprintf(outparams, PARAMS_SIZE, "%s", params) <= PARAMS_SIZE)
523 dm_task_destroy(dmt);
528 dm_get_prefixed_uuid(const char *name, char *uuid)
534 dmt = libmp_dm_task_create(DM_DEVICE_INFO);
538 if (!dm_task_set_name (dmt, name))
541 if (!dm_task_run(dmt))
544 uuidtmp = dm_task_get_uuid(dmt);
546 strcpy(uuid, uuidtmp);
552 dm_task_destroy(dmt);
556 int dm_get_uuid(const char *name, char *uuid)
558 if (dm_get_prefixed_uuid(name, uuid))
561 if (!strncmp(uuid, UUID_PREFIX, UUID_PREFIX_LEN))
562 memmove(uuid, uuid + UUID_PREFIX_LEN,
563 strlen(uuid + UUID_PREFIX_LEN) + 1);
568 is_mpath_part(const char *part_name, const char *map_name)
571 char part_uuid[WWID_SIZE], map_uuid[WWID_SIZE];
573 if (dm_get_prefixed_uuid(part_name, part_uuid))
576 if (dm_get_prefixed_uuid(map_name, map_uuid))
579 if (strncmp(part_uuid, "part", 4) != 0)
582 p = strstr(part_uuid, UUID_PREFIX);
583 if (p && !strcmp(p, map_uuid))
589 int dm_get_status(char *name, char *outstatus)
593 uint64_t start, length;
594 char *target_type = NULL;
597 if (!(dmt = libmp_dm_task_create(DM_DEVICE_STATUS)))
600 if (!dm_task_set_name(dmt, name))
603 dm_task_no_open_count(dmt);
605 if (!dm_task_run(dmt))
608 /* Fetch 1st target */
609 dm_get_next_target(dmt, NULL, &start, &length,
610 &target_type, &status);
612 condlog(2, "get null status.");
616 if (snprintf(outstatus, PARAMS_SIZE, "%s", status) <= PARAMS_SIZE)
620 condlog(0, "%s: error getting map status string", name);
622 dm_task_destroy(dmt);
630 * -1 : empty map, or more than 1 target
632 int dm_type(const char *name, char *type)
636 uint64_t start, length;
637 char *target_type = NULL;
640 if (!(dmt = libmp_dm_task_create(DM_DEVICE_TABLE)))
643 if (!dm_task_set_name(dmt, name))
646 dm_task_no_open_count(dmt);
648 if (!dm_task_run(dmt))
651 /* Fetch 1st target */
652 if (dm_get_next_target(dmt, NULL, &start, &length,
653 &target_type, ¶ms) != NULL)
654 /* multiple targets */
656 else if (!target_type)
658 else if (!strcmp(target_type, type))
662 dm_task_destroy(dmt);
666 int dm_is_mpath(const char *name)
671 uint64_t start, length;
672 char *target_type = NULL;
676 if (!(dmt = libmp_dm_task_create(DM_DEVICE_TABLE)))
679 if (!dm_task_set_name(dmt, name))
682 dm_task_no_open_count(dmt);
684 if (!dm_task_run(dmt))
687 if (!dm_task_get_info(dmt, &info) || !info.exists)
690 uuid = dm_task_get_uuid(dmt);
692 if (!uuid || strncmp(uuid, UUID_PREFIX, UUID_PREFIX_LEN) != 0)
695 /* Fetch 1st target */
696 dm_get_next_target(dmt, NULL, &start, &length, &target_type, ¶ms);
698 if (!target_type || strcmp(target_type, TGT_MPATH) != 0)
703 dm_task_destroy(dmt);
708 dm_dev_t (const char * mapname, char * dev_t, int len)
712 if (do_get_info(mapname, &info) != 0)
715 if (snprintf(dev_t, len, "%i:%i", info.major, info.minor) > len)
722 dm_get_opencount (const char * mapname)
728 if (!(dmt = libmp_dm_task_create(DM_DEVICE_INFO)))
731 if (!dm_task_set_name(dmt, mapname))
734 if (!dm_task_run(dmt))
737 if (!dm_task_get_info(dmt, &info))
745 dm_task_destroy(dmt);
750 dm_get_major_minor(const char *name, int *major, int *minor)
754 if (do_get_info(name, &info) != 0)
763 has_partmap(const char *name, void *data)
769 partmap_in_use(const char *name, void *data)
771 int part_count, *ret_count = (int *)data;
772 int open_count = dm_get_opencount(name);
778 if (do_foreach_partmaps(name, partmap_in_use, &part_count))
780 if (open_count != part_count) {
781 condlog(2, "%s: map in use", name);
788 int _dm_flush_map (const char * mapname, int need_sync, int deferred_remove,
789 int need_suspend, int retries)
792 int queue_if_no_path = 0;
794 unsigned long long mapsize;
795 char params[PARAMS_SIZE] = {0};
797 if (!dm_is_mpath(mapname))
798 return 0; /* nothing to do */
800 /* if the device currently has no partitions, do not
801 run kpartx on it if you fail to delete it */
802 if (do_foreach_partmaps(mapname, has_partmap, NULL) == 0)
803 udev_flags |= MPATH_UDEV_NO_KPARTX_FLAG;
805 /* If you aren't doing a deferred remove, make sure that no
806 * devices are in use */
807 if (!do_deferred(deferred_remove) && partmap_in_use(mapname, NULL))
811 !dm_get_map(mapname, &mapsize, params) &&
812 strstr(params, "queue_if_no_path")) {
813 if (!dm_queue_if_no_path((char *)mapname, 0))
814 queue_if_no_path = 1;
816 /* Leave queue_if_no_path alone if unset failed */
817 queue_if_no_path = -1;
820 if (dm_remove_partmaps(mapname, need_sync, deferred_remove))
823 if (!do_deferred(deferred_remove) && dm_get_opencount(mapname)) {
824 condlog(2, "%s: map in use", mapname);
829 if (need_suspend && queue_if_no_path != -1)
830 dm_simplecmd_flush(DM_DEVICE_SUSPEND, mapname, 0);
832 r = dm_device_remove(mapname, need_sync, deferred_remove);
835 if (do_deferred(deferred_remove)
836 && dm_map_present(mapname)) {
837 condlog(4, "multipath map %s remove deferred",
841 condlog(4, "multipath map %s removed", mapname);
844 condlog(2, "failed to remove multipath map %s",
846 if (need_suspend && queue_if_no_path != -1) {
847 dm_simplecmd_noflush(DM_DEVICE_RESUME,
848 mapname, udev_flags);
853 } while (retries-- > 0);
855 if (queue_if_no_path == 1)
856 dm_queue_if_no_path((char *)mapname, 1);
861 #ifdef LIBDM_API_DEFERRED
864 dm_flush_map_nopaths(const char * mapname, int deferred_remove)
866 return _dm_flush_map(mapname, 1, deferred_remove, 0, 0);
872 dm_flush_map_nopaths(const char * mapname, int deferred_remove)
874 return _dm_flush_map(mapname, 1, 0, 0, 0);
879 int dm_flush_maps (int retries)
883 struct dm_names *names;
886 if (!(dmt = libmp_dm_task_create (DM_DEVICE_LIST)))
889 dm_task_no_open_count(dmt);
891 if (!dm_task_run (dmt))
894 if (!(names = dm_task_get_names (dmt)))
901 r |= dm_suspend_and_flush_map(names->name, retries);
903 names = (void *) names + next;
907 dm_task_destroy (dmt);
912 dm_message(const char * mapname, char * message)
917 if (!(dmt = libmp_dm_task_create(DM_DEVICE_TARGET_MSG)))
920 if (!dm_task_set_name(dmt, mapname))
923 if (!dm_task_set_sector(dmt, 0))
926 if (!dm_task_set_message(dmt, message))
929 dm_task_no_open_count(dmt);
931 if (!dm_task_run(dmt))
937 condlog(0, "DM message failed [%s]", message);
939 dm_task_destroy(dmt);
944 dm_fail_path(char * mapname, char * path)
948 if (snprintf(message, 32, "fail_path %s", path) > 32)
951 return dm_message(mapname, message);
955 dm_reinstate_path(char * mapname, char * path)
959 if (snprintf(message, 32, "reinstate_path %s", path) > 32)
962 return dm_message(mapname, message);
966 dm_queue_if_no_path(char *mapname, int enable)
971 message = "queue_if_no_path";
973 message = "fail_if_no_path";
975 return dm_message(mapname, message);
979 dm_groupmsg (char * msg, char * mapname, int index)
983 if (snprintf(message, 32, "%s_group %i", msg, index) > 32)
986 return dm_message(mapname, message);
990 dm_switchgroup(char * mapname, int index)
992 return dm_groupmsg("switch", mapname, index);
996 dm_enablegroup(char * mapname, int index)
998 return dm_groupmsg("enable", mapname, index);
1002 dm_disablegroup(char * mapname, int index)
1004 return dm_groupmsg("disable", mapname, index);
1007 struct multipath *dm_get_multipath(const char *name)
1009 struct multipath *mpp = NULL;
1011 mpp = alloc_multipath();
1015 mpp->alias = STRDUP(name);
1020 if (dm_get_map(name, &mpp->size, NULL))
1023 dm_get_uuid(name, mpp->wwid);
1024 dm_get_info(name, &mpp->dmi);
1028 free_multipath(mpp, KEEP_PATHS);
1033 dm_get_maps (vector mp)
1035 struct multipath * mpp;
1037 struct dm_task *dmt;
1038 struct dm_names *names;
1044 if (!(dmt = libmp_dm_task_create(DM_DEVICE_LIST)))
1047 dm_task_no_open_count(dmt);
1049 if (!dm_task_run(dmt))
1052 if (!(names = dm_task_get_names(dmt)))
1056 r = 0; /* this is perfectly valid */
1061 if (!dm_is_mpath(names->name))
1064 mpp = dm_get_multipath(names->name);
1068 if (!vector_alloc_slot(mp))
1071 vector_set_slot(mp, mpp);
1075 names = (void *) names + next;
1081 dm_task_destroy (dmt);
1086 dm_geteventnr (char *name)
1088 struct dm_info info;
1090 if (do_get_info(name, &info) != 0)
1093 return info.event_nr;
1097 dm_is_suspended(const char *name)
1099 struct dm_info info;
1101 if (do_get_info(name, &info) != 0)
1104 return info.suspended;
1108 dm_mapname(int major, int minor)
1110 char * response = NULL;
1112 struct dm_task *dmt;
1114 int loop = MAX_WAIT * LOOPS_PER_SEC;
1116 if (!(dmt = libmp_dm_task_create(DM_DEVICE_STATUS)))
1119 if (!dm_task_set_major(dmt, major) ||
1120 !dm_task_set_minor(dmt, minor))
1123 dm_task_no_open_count(dmt);
1126 * device map might not be ready when we get here from
1127 * daemon uev_trigger -> uev_add_map
1130 r = dm_task_run(dmt);
1135 usleep(1000 * 1000 / LOOPS_PER_SEC);
1139 condlog(0, "%i:%i: timeout fetching map name", major, minor);
1143 map = dm_task_get_name(dmt);
1144 if (map && strlen(map))
1145 response = STRDUP((char *)dm_task_get_name(dmt));
1147 dm_task_destroy(dmt);
1150 dm_task_destroy(dmt);
1151 condlog(0, "%i:%i: error fetching map name", major, minor);
1156 do_foreach_partmaps (const char * mapname,
1157 int (*partmap_func)(const char *, void *),
1160 struct dm_task *dmt;
1161 struct dm_names *names;
1163 char params[PARAMS_SIZE];
1164 unsigned long long size;
1169 if (!(dmt = libmp_dm_task_create(DM_DEVICE_LIST)))
1172 dm_task_no_open_count(dmt);
1174 if (!dm_task_run(dmt))
1177 if (!(names = dm_task_get_names(dmt)))
1181 r = 0; /* this is perfectly valid */
1185 if (dm_dev_t(mapname, &dev_t[0], 32))
1191 * if there is only a single "linear" target
1193 (dm_type(names->name, TGT_PART) == 1) &&
1196 * and the uuid of the target is a partition of the
1197 * uuid of the multipath device
1199 is_mpath_part(names->name, mapname) &&
1202 * and we can fetch the map table from the kernel
1204 !dm_get_map(names->name, &size, ¶ms[0]) &&
1207 * and the table maps over the multipath map
1209 (p = strstr(params, dev_t)) &&
1210 !isdigit(*(p + strlen(dev_t)))
1212 if (partmap_func(names->name, data) != 0)
1217 names = (void *) names + next;
1222 dm_task_destroy (dmt);
1226 struct remove_data {
1228 int deferred_remove;
1232 remove_partmap(const char *name, void *data)
1234 struct remove_data *rd = (struct remove_data *)data;
1236 if (dm_get_opencount(name)) {
1237 dm_remove_partmaps(name, rd->need_sync, rd->deferred_remove);
1238 if (!do_deferred(rd->deferred_remove) &&
1239 dm_get_opencount(name)) {
1240 condlog(2, "%s: map in use", name);
1244 condlog(4, "partition map %s removed", name);
1245 dm_device_remove(name, rd->need_sync, rd->deferred_remove);
1250 dm_remove_partmaps (const char * mapname, int need_sync, int deferred_remove)
1252 struct remove_data rd = { need_sync, deferred_remove };
1253 return do_foreach_partmaps(mapname, remove_partmap, &rd);
1256 #ifdef LIBDM_API_DEFERRED
1259 cancel_remove_partmap (const char *name, void *unused)
1261 if (dm_get_opencount(name))
1262 dm_cancel_remove_partmaps(name);
1263 if (dm_message(name, "@cancel_deferred_remove") != 0)
1264 condlog(0, "%s: can't cancel deferred remove: %s", name,
1270 dm_get_deferred_remove (char * mapname)
1272 struct dm_info info;
1274 if (do_get_info(mapname, &info) != 0)
1277 return info.deferred_remove;
1281 dm_cancel_remove_partmaps(const char * mapname) {
1282 return do_foreach_partmaps(mapname, cancel_remove_partmap, NULL);
1286 dm_cancel_deferred_remove (struct multipath *mpp)
1290 if (!dm_get_deferred_remove(mpp->alias))
1292 if (mpp->deferred_remove == DEFERRED_REMOVE_IN_PROGRESS)
1293 mpp->deferred_remove = DEFERRED_REMOVE_ON;
1295 dm_cancel_remove_partmaps(mpp->alias);
1296 r = dm_message(mpp->alias, "@cancel_deferred_remove");
1298 condlog(0, "%s: can't cancel deferred remove: %s", mpp->alias,
1301 condlog(2, "%s: canceled deferred remove", mpp->alias);
1308 dm_cancel_deferred_remove (struct multipath *mpp)
1315 static struct dm_info *
1318 return MALLOC(sizeof(struct dm_info));
1322 dm_get_info (const char * mapname, struct dm_info ** dmi)
1328 *dmi = alloc_dminfo();
1333 if (do_get_info(mapname, *dmi) != 0) {
1334 memset(*dmi, 0, sizeof(struct dm_info));
1342 struct rename_data {
1349 rename_partmap (const char *name, void *data)
1351 char buff[PARAMS_SIZE];
1353 struct rename_data *rd = (struct rename_data *)data;
1355 if (strncmp(name, rd->old, strlen(rd->old)) != 0)
1357 for (offset = strlen(rd->old); name[offset] && !(isdigit(name[offset])); offset++); /* do nothing */
1358 snprintf(buff, PARAMS_SIZE, "%s%s%s", rd->new, rd->delim,
1360 dm_rename(name, buff, rd->delim, SKIP_KPARTX_OFF);
1361 condlog(4, "partition map %s renamed", name);
1366 dm_rename_partmaps (const char * old, char * new, char *delim)
1368 struct rename_data rd;
1376 if (isdigit(new[strlen(new)-1]))
1381 return do_foreach_partmaps(old, rename_partmap, &rd);
1385 dm_rename (const char * old, char * new, char *delim, int skip_kpartx)
1388 struct dm_task *dmt;
1389 uint32_t cookie = 0;
1390 uint16_t udev_flags = DM_UDEV_DISABLE_LIBRARY_FALLBACK | ((skip_kpartx == SKIP_KPARTX_ON)? MPATH_UDEV_NO_KPARTX_FLAG : 0);
1392 if (dm_rename_partmaps(old, new, delim))
1395 if (!(dmt = libmp_dm_task_create(DM_DEVICE_RENAME)))
1398 if (!dm_task_set_name(dmt, old))
1401 if (!dm_task_set_newname(dmt, new))
1404 dm_task_no_open_count(dmt);
1406 if (!dm_task_set_cookie(dmt, &cookie, udev_flags))
1408 r = dm_task_run(dmt);
1410 dm_udev_wait(cookie);
1413 dm_task_destroy(dmt);
1418 void dm_reassign_deps(char *table, char *dep, char *newdep)
1423 newtable = strdup(table);
1426 p = strstr(newtable, dep);
1427 n = table + (p - newtable);
1429 n += strlen(newdep);
1435 int dm_reassign_table(const char *name, char *old, char *new)
1437 int r = 0, modified = 0;
1438 uint64_t start, length;
1439 struct dm_task *dmt, *reload_dmt;
1440 char *target, *params = NULL;
1444 if (!(dmt = libmp_dm_task_create(DM_DEVICE_TABLE)))
1447 if (!dm_task_set_name(dmt, name))
1450 dm_task_no_open_count(dmt);
1452 if (!dm_task_run(dmt))
1454 if (!(reload_dmt = libmp_dm_task_create(DM_DEVICE_RELOAD)))
1456 if (!dm_task_set_name(reload_dmt, name))
1460 next = dm_get_next_target(dmt, next, &start, &length,
1462 buff = strdup(params);
1464 condlog(3, "%s: failed to replace target %s, "
1465 "out of memory", name, target);
1468 if (strcmp(target, TGT_MPATH) && strstr(params, old)) {
1469 condlog(3, "%s: replace target %s %s",
1470 name, target, buff);
1471 dm_reassign_deps(buff, old, new);
1472 condlog(3, "%s: with target %s %s",
1473 name, target, buff);
1476 dm_task_add_target(reload_dmt, start, length, target, buff);
1481 dm_task_no_open_count(reload_dmt);
1483 if (!dm_task_run(reload_dmt)) {
1484 condlog(3, "%s: failed to reassign targets", name);
1487 dm_simplecmd_noflush(DM_DEVICE_RESUME, name,
1488 MPATH_UDEV_RELOAD_FLAG);
1493 dm_task_destroy(reload_dmt);
1495 dm_task_destroy(dmt);
1501 * Reassign existing device-mapper table(s) to not use
1502 * the block devices but point to the multipathed
1505 int dm_reassign(const char *mapname)
1507 struct dm_deps *deps;
1508 struct dm_task *dmt;
1509 struct dm_info info;
1510 char dev_t[32], dm_dep[32];
1513 if (dm_dev_t(mapname, &dev_t[0], 32)) {
1514 condlog(3, "%s: failed to get device number", mapname);
1518 if (!(dmt = libmp_dm_task_create(DM_DEVICE_DEPS))) {
1519 condlog(3, "%s: couldn't make dm task", mapname);
1523 if (!dm_task_set_name(dmt, mapname))
1526 dm_task_no_open_count(dmt);
1528 if (!dm_task_run(dmt))
1531 if (!dm_task_get_info(dmt, &info))
1534 if (!(deps = dm_task_get_deps(dmt)))
1540 for (i = 0; i < deps->count; i++) {
1541 sprintf(dm_dep, "%d:%d",
1542 major(deps->device[i]),
1543 minor(deps->device[i]));
1544 sysfs_check_holders(dm_dep, dev_t);
1549 dm_task_destroy (dmt);
1553 int dm_setgeometry(struct multipath *mpp)
1555 struct dm_task *dmt;
1557 char heads[4], sectors[4];
1558 char cylinders[10], start[32];
1564 pp = first_path(mpp);
1566 condlog(3, "%s: no path for geometry", mpp->alias);
1569 if (pp->geom.cylinders == 0 ||
1570 pp->geom.heads == 0 ||
1571 pp->geom.sectors == 0) {
1572 condlog(3, "%s: invalid geometry on %s", mpp->alias, pp->dev);
1576 if (!(dmt = libmp_dm_task_create(DM_DEVICE_SET_GEOMETRY)))
1579 if (!dm_task_set_name(dmt, mpp->alias))
1582 dm_task_no_open_count(dmt);
1584 /* What a sick interface ... */
1585 snprintf(heads, 4, "%u", pp->geom.heads);
1586 snprintf(sectors, 4, "%u", pp->geom.sectors);
1587 snprintf(cylinders, 10, "%u", pp->geom.cylinders);
1588 snprintf(start, 32, "%lu", pp->geom.start);
1589 if (!dm_task_set_geometry(dmt, cylinders, heads, sectors, start)) {
1590 condlog(3, "%s: Failed to set geometry", mpp->alias);
1594 r = dm_task_run(dmt);
1596 dm_task_destroy(dmt);