Imported Upstream version 0.7.5
[platform/upstream/multipath-tools.git] / libmultipath / devmapper.c
index 20ecabb..607aea8 100644 (file)
+/*
+ * snippets copied from device-mapper dmsetup.c
+ * Copyright (c) 2004, 2005 Christophe Varoqui
+ * Copyright (c) 2005 Kiyoshi Ueda, NEC
+ * Copyright (c) 2005 Patrick Caulfield, Redhat
+ */
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <string.h>
 #include <libdevmapper.h>
 #include <ctype.h>
-#include <linux/kdev_t.h>
 #include <unistd.h>
+#include <errno.h>
+#include <sys/sysmacros.h>
 
+#include "checkers.h"
 #include "vector.h"
 #include "structs.h"
 #include "debug.h"
 #include "memory.h"
 #include "devmapper.h"
+#include "sysfs.h"
+#include "config.h"
 
+#include "log_pthread.h"
+#include <sys/types.h>
+#include <time.h>
+
+#define FREE_CONST(p) do { free((void*)(unsigned long)p); p = NULL; } while(0)
 #define MAX_WAIT 5
 #define LOOPS_PER_SEC 5
 
+static int dm_conf_verbosity;
+
+#ifdef LIBDM_API_DEFERRED
+static int dm_cancel_remove_partmaps(const char * mapname);
+#endif
+
+static int do_foreach_partmaps(const char * mapname,
+                              int (*partmap_func)(const char *, void *),
+                              void *data);
+
+#ifndef LIBDM_API_COOKIE
+static inline int dm_task_set_cookie(struct dm_task *dmt, uint32_t *c, int a)
+{
+       return 1;
+}
+
+void dm_udev_wait(unsigned int c)
+{
+}
+
+void dm_udev_set_sync_support(int c)
+{
+}
+
+#endif
+
 static void
-dm_dummy_log (int level, const char *file, int line, const char *f, ...)
+dm_write_log (int level, const char *file, int line, const char *f, ...)
 {
+       va_list ap;
+       int thres;
+
+       if (level > 6)
+               level = 6;
+
+       thres = dm_conf_verbosity;
+       if (thres <= 3 || level > thres)
+               return;
+
+       va_start(ap, f);
+       if (logsink < 1) {
+               if (logsink == 0) {
+                       time_t t = time(NULL);
+                       struct tm *tb = localtime(&t);
+                       char buff[16];
+
+                       strftime(buff, sizeof(buff), "%b %d %H:%M:%S", tb);
+                       buff[sizeof(buff)-1] = '\0';
+
+                       fprintf(stdout, "%s | ", buff);
+               }
+               fprintf(stdout, "libdevmapper: %s(%i): ", file, line);
+               vfprintf(stdout, f, ap);
+               fprintf(stdout, "\n");
+       } else {
+               condlog(level, "libdevmapper: %s(%i): ", file, line);
+               log_safe(level + 3, f, ap);
+       }
+       va_end(ap);
+
        return;
 }
 
-static void
-dm_restore_log (void)
+void dm_init(int v)
 {
-       dm_log_init(NULL);
+       dm_log_init(&dm_write_log);
+       dm_log_init_verbose(v + 3);
 }
 
-static void
-dm_shut_log (void)
+static int
+dm_lib_prereq (void)
 {
-       dm_log_init(&dm_dummy_log);
+       char version[64];
+       int v[3];
+#if defined(LIBDM_API_DEFERRED)
+       int minv[3] = {1, 2, 89};
+#elif defined(DM_SUBSYSTEM_UDEV_FLAG0)
+       int minv[3] = {1, 2, 82};
+#elif defined(LIBDM_API_COOKIE)
+       int minv[3] = {1, 2, 38};
+#else
+       int minv[3] = {1, 2, 8};
+#endif
+
+       dm_get_library_version(version, sizeof(version));
+       condlog(3, "libdevmapper version %s", version);
+       if (sscanf(version, "%d.%d.%d ", &v[0], &v[1], &v[2]) != 3) {
+               condlog(0, "invalid libdevmapper version %s", version);
+               return 1;
+       }
+
+       if VERSION_GE(v, minv)
+               return 0;
+       condlog(0, "libdevmapper version must be >= %d.%.2d.%.2d",
+               minv[0], minv[1], minv[2]);
+       return 1;
 }
 
-extern int
-dm_prereq (char * str, int x, int y, int z)
+int
+dm_drv_version (unsigned int * version, char * str)
 {
        int r = 2;
        struct dm_task *dmt;
        struct dm_versions *target;
        struct dm_versions *last_target;
+       unsigned int *v;
+
+       version[0] = 0;
+       version[1] = 0;
+       version[2] = 0;
 
        if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
-               return 3;
+               return 1;
 
        dm_task_no_open_count(dmt);
 
@@ -50,94 +151,314 @@ dm_prereq (char * str, int x, int y, int z)
                condlog(0, "Can not communicate with kernel DM");
                goto out;
        }
-
        target = dm_task_get_versions(dmt);
 
        do {
                last_target = target;
-
                if (!strncmp(str, target->name, strlen(str))) {
-                       r--;
-                       
-                       if (target->version[0] >= x &&
-                           target->version[1] >= y &&
-                           target->version[2] >= z)
-                               r--;
-
+                       r = 1;
                        break;
                }
-
                target = (void *) target + target->next;
        } while (last_target != target);
 
-       if (r == 2)
-               condlog(0, "DM multipath kernel driver not loaded");
-       else if (r == 1)
-               condlog(0, "DM multipath kernel driver version too old");
-
+       if (r == 2) {
+               condlog(0, "DM %s kernel driver not loaded", str);
+               goto out;
+       }
+       v = target->version;
+       version[0] = v[0];
+       version[1] = v[1];
+       version[2] = v[2];
+       r = 0;
 out:
        dm_task_destroy(dmt);
        return r;
 }
 
-extern int
-dm_simplecmd (int task, const char *name) {
+static int
+dm_drv_prereq (unsigned int *ver)
+{
+       unsigned int minv[3] = {1, 0, 3};
+       unsigned int version[3] = {0, 0, 0};
+       unsigned int * v = version;
+
+       if (dm_drv_version(v, TGT_MPATH)) {
+               /* in doubt return not capable */
+               return 1;
+       }
+
+       /* test request based multipath capability */
+       condlog(3, "DM multipath kernel driver v%u.%u.%u",
+               v[0], v[1], v[2]);
+
+       if (VERSION_GE(v, minv)) {
+               ver[0] = v[0];
+               ver[1] = v[1];
+               ver[2] = v[2];
+               return 0;
+       }
+
+       condlog(0, "DM multipath kernel driver must be >= v%u.%u.%u",
+               minv[0], minv[1], minv[2]);
+       return 1;
+}
+
+static int dm_prereq(unsigned int *v)
+{
+       if (dm_lib_prereq())
+               return 1;
+       return dm_drv_prereq(v);
+}
+
+static int libmp_dm_udev_sync = 0;
+
+void libmp_udev_set_sync_support(int on)
+{
+       libmp_dm_udev_sync = !!on;
+}
+
+void libmp_dm_init(void)
+{
+       struct config *conf;
+
+       conf = get_multipath_config();
+       dm_init(conf->verbosity);
+       if (dm_prereq(conf->version))
+               exit(1);
+       put_multipath_config(conf);
+       dm_udev_set_sync_support(libmp_dm_udev_sync);
+}
+
+struct dm_task*
+libmp_dm_task_create(int task)
+{
+       static pthread_once_t dm_initialized = PTHREAD_ONCE_INIT;
+
+       pthread_once(&dm_initialized, libmp_dm_init);
+       return dm_task_create(task);
+}
+
+#define do_deferred(x) ((x) == DEFERRED_REMOVE_ON || (x) == DEFERRED_REMOVE_IN_PROGRESS)
+
+static int
+dm_simplecmd (int task, const char *name, int no_flush, int need_sync, uint16_t udev_flags, int deferred_remove) {
        int r = 0;
+       int udev_wait_flag = ((need_sync || udev_flags) &&
+                             (task == DM_DEVICE_RESUME ||
+                              task == DM_DEVICE_REMOVE));
+       uint32_t cookie = 0;
        struct dm_task *dmt;
 
-       if (!(dmt = dm_task_create (task)))
+       if (!(dmt = libmp_dm_task_create (task)))
                return 0;
 
        if (!dm_task_set_name (dmt, name))
                goto out;
 
        dm_task_no_open_count(dmt);
+       dm_task_skip_lockfs(dmt);       /* for DM_DEVICE_RESUME */
+#ifdef LIBDM_API_FLUSH
+       if (no_flush)
+               dm_task_no_flush(dmt);          /* for DM_DEVICE_SUSPEND/RESUME */
+#endif
+#ifdef LIBDM_API_DEFERRED
+       if (do_deferred(deferred_remove))
+               dm_task_deferred_remove(dmt);
+#endif
+       if (udev_wait_flag &&
+           !dm_task_set_cookie(dmt, &cookie,
+                               DM_UDEV_DISABLE_LIBRARY_FALLBACK | udev_flags))
+               goto out;
 
        r = dm_task_run (dmt);
 
-       out:
+       if (udev_wait_flag)
+                       dm_udev_wait(cookie);
+out:
        dm_task_destroy (dmt);
        return r;
 }
 
-extern int
-dm_addmap (int task, const char *name, const char *target,
-          const char *params, unsigned long long size, const char *uuid) {
+int dm_simplecmd_flush (int task, const char *name, uint16_t udev_flags)
+{
+       return dm_simplecmd(task, name, 0, 1, udev_flags, 0);
+}
+
+int dm_simplecmd_noflush (int task, const char *name, uint16_t udev_flags)
+{
+       return dm_simplecmd(task, name, 1, 1, udev_flags, 0);
+}
+
+static int
+dm_device_remove (const char *name, int needsync, int deferred_remove) {
+       return dm_simplecmd(DM_DEVICE_REMOVE, name, 0, needsync, 0,
+                           deferred_remove);
+}
+
+static int
+dm_addmap (int task, const char *target, struct multipath *mpp,
+          char * params, int ro, uint16_t udev_flags) {
        int r = 0;
        struct dm_task *dmt;
+       char *prefixed_uuid = NULL;
+       uint32_t cookie = 0;
 
-       if (!(dmt = dm_task_create (task)))
+       /* Need to add this here to allow 0 to be passed in udev_flags */
+       udev_flags |= DM_UDEV_DISABLE_LIBRARY_FALLBACK;
+
+       if (!(dmt = libmp_dm_task_create (task)))
                return 0;
 
-       if (!dm_task_set_name (dmt, name))
+       if (!dm_task_set_name (dmt, mpp->alias))
                goto addout;
 
-       if (!dm_task_add_target (dmt, 0, size, target, params))
+       if (!dm_task_add_target (dmt, 0, mpp->size, target, params))
                goto addout;
 
-       if (uuid && !dm_task_set_uuid(dmt, uuid))
-               goto addout;
+       if (ro)
+               dm_task_set_ro(dmt);
+
+       if (task == DM_DEVICE_CREATE) {
+               if (strlen(mpp->wwid) > 0) {
+                       prefixed_uuid = MALLOC(UUID_PREFIX_LEN +
+                                              strlen(mpp->wwid) + 1);
+                       if (!prefixed_uuid) {
+                               condlog(0, "cannot create prefixed uuid : %s",
+                                       strerror(errno));
+                               goto addout;
+                       }
+                       sprintf(prefixed_uuid, UUID_PREFIX "%s", mpp->wwid);
+                       if (!dm_task_set_uuid(dmt, prefixed_uuid))
+                               goto freeout;
+               }
+               dm_task_skip_lockfs(dmt);
+#ifdef LIBDM_API_FLUSH
+               dm_task_no_flush(dmt);
+#endif
+       }
+
+       if (mpp->attribute_flags & (1 << ATTR_MODE) &&
+           !dm_task_set_mode(dmt, mpp->mode))
+               goto freeout;
+       if (mpp->attribute_flags & (1 << ATTR_UID) &&
+           !dm_task_set_uid(dmt, mpp->uid))
+               goto freeout;
+       if (mpp->attribute_flags & (1 << ATTR_GID) &&
+           !dm_task_set_gid(dmt, mpp->gid))
+               goto freeout;
+       condlog(4, "%s: %s [0 %llu %s %s]", mpp->alias,
+               task == DM_DEVICE_RELOAD ? "reload" : "addmap", mpp->size,
+               target, params);
 
        dm_task_no_open_count(dmt);
 
+       if (task == DM_DEVICE_CREATE &&
+           !dm_task_set_cookie(dmt, &cookie, udev_flags))
+               goto freeout;
+
        r = dm_task_run (dmt);
 
-       addout:
+       if (task == DM_DEVICE_CREATE)
+                       dm_udev_wait(cookie);
+freeout:
+       if (prefixed_uuid)
+               FREE(prefixed_uuid);
+
+addout:
        dm_task_destroy (dmt);
+
        return r;
 }
 
-extern int
-dm_map_present (char * str)
+static uint16_t build_udev_flags(const struct multipath *mpp, int reload)
+{
+       /* DM_UDEV_DISABLE_LIBRARY_FALLBACK is added in dm_addmap */
+       return  (mpp->skip_kpartx == SKIP_KPARTX_ON ?
+                MPATH_UDEV_NO_KPARTX_FLAG : 0) |
+               ((mpp->nr_active == 0 || mpp->ghost_delay_tick > 0)?
+                MPATH_UDEV_NO_PATHS_FLAG : 0) |
+               (reload && !mpp->force_udev_reload ?
+                MPATH_UDEV_RELOAD_FLAG : 0);
+}
+
+int dm_addmap_create (struct multipath *mpp, char * params)
+{
+       int ro;
+       uint16_t udev_flags = build_udev_flags(mpp, 0);
+
+       for (ro = 0; ro <= 1; ro++) {
+               int err;
+
+               if (dm_addmap(DM_DEVICE_CREATE, TGT_MPATH, mpp, params, ro,
+                             udev_flags))
+                       return 1;
+               /*
+                * DM_DEVICE_CREATE is actually DM_DEV_CREATE + DM_TABLE_LOAD.
+                * Failing the second part leaves an empty map. Clean it up.
+                */
+               err = errno;
+               if (dm_map_present(mpp->alias)) {
+                       condlog(3, "%s: failed to load map (a path might be in use)", mpp->alias);
+                       dm_flush_map_nosync(mpp->alias);
+               }
+               if (err != EROFS) {
+                       condlog(3, "%s: failed to load map, error %d",
+                               mpp->alias, err);
+                       break;
+               }
+       }
+       return 0;
+}
+
+#define ADDMAP_RW 0
+#define ADDMAP_RO 1
+
+int dm_addmap_reload(struct multipath *mpp, char *params, int flush)
 {
        int r = 0;
+       uint16_t udev_flags = build_udev_flags(mpp, 1);
+
+       /*
+        * DM_DEVICE_RELOAD cannot wait on a cookie, as
+        * the cookie will only ever be released after an
+        * DM_DEVICE_RESUME. So call DM_DEVICE_RESUME
+        * after each successful call to DM_DEVICE_RELOAD.
+        */
+       if (!mpp->force_readonly)
+               r = dm_addmap(DM_DEVICE_RELOAD, TGT_MPATH, mpp, params,
+                             ADDMAP_RW, 0);
+       if (!r) {
+               if (!mpp->force_readonly && errno != EROFS)
+                       return 0;
+               r = dm_addmap(DM_DEVICE_RELOAD, TGT_MPATH, mpp,
+                             params, ADDMAP_RO, 0);
+       }
+       if (r)
+               r = dm_simplecmd(DM_DEVICE_RESUME, mpp->alias, !flush,
+                                1, udev_flags, 0);
+       if (r)
+               return r;
+
+       /* If the resume failed, dm will leave the device suspended, and
+        * drop the new table, so doing a second resume will try using
+        * the original table */
+       if (dm_is_suspended(mpp->alias))
+               dm_simplecmd(DM_DEVICE_RESUME, mpp->alias, !flush, 1,
+                            udev_flags, 0);
+       return 0;
+}
+
+static int
+do_get_info(const char *name, struct dm_info *info)
+{
+       int r = -1;
        struct dm_task *dmt;
-       struct dm_info info;
 
-       if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
-               return 0;
+       if (!(dmt = libmp_dm_task_create(DM_DEVICE_INFO)))
+               return r;
 
-       if (!dm_task_set_name(dmt, str))
+       if (!dm_task_set_name(dmt, name))
                goto out;
 
        dm_task_no_open_count(dmt);
@@ -145,27 +466,34 @@ dm_map_present (char * str)
        if (!dm_task_run(dmt))
                goto out;
 
-       if (!dm_task_get_info(dmt, &info))
+       if (!dm_task_get_info(dmt, info))
                goto out;
 
-       if (info.exists)
-               r = 1;
+       if (!info->exists)
+               goto out;
+
+       r = 0;
 out:
        dm_task_destroy(dmt);
        return r;
 }
 
-extern int
-dm_get_map(char * name, unsigned long long * size, char * outparams)
+int dm_map_present(const char * str)
+{
+       struct dm_info info;
+
+       return (do_get_info(str, &info) == 0);
+}
+
+int dm_get_map(const char *name, unsigned long long *size, char *outparams)
 {
        int r = 1;
        struct dm_task *dmt;
-       void *next = NULL;
        uint64_t start, length;
        char *target_type = NULL;
        char *params = NULL;
 
-       if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
+       if (!(dmt = libmp_dm_task_create(DM_DEVICE_TABLE)))
                return 1;
 
        if (!dm_task_set_name(dmt, name))
@@ -177,12 +505,16 @@ dm_get_map(char * name, unsigned long long * size, char * outparams)
                goto out;
 
        /* Fetch 1st target */
-       next = dm_get_next_target(dmt, next, &start, &length,
-                                 &target_type, &params);
+       dm_get_next_target(dmt, NULL, &start, &length,
+                          &target_type, &params);
 
        if (size)
                *size = length;
 
+       if (!outparams) {
+               r = 0;
+               goto out;
+       }
        if (snprintf(outparams, PARAMS_SIZE, "%s", params) <= PARAMS_SIZE)
                r = 0;
 out:
@@ -190,21 +522,22 @@ out:
        return r;
 }
 
-extern int
-dm_get_uuid(char *name, char *uuid)
+static int
+dm_get_prefixed_uuid(const char *name, char *uuid)
 {
        struct dm_task *dmt;
        const char *uuidtmp;
+       int r = 1;
 
-       dmt = dm_task_create(DM_DEVICE_INFO);
+       dmt = libmp_dm_task_create(DM_DEVICE_INFO);
        if (!dmt)
                return 1;
 
-        if (!dm_task_set_name (dmt, name))
-                goto uuidout;
+       if (!dm_task_set_name (dmt, name))
+               goto uuidout;
 
        if (!dm_task_run(dmt))
-                goto uuidout;
+               goto uuidout;
 
        uuidtmp = dm_task_get_uuid(dmt);
        if (uuidtmp)
@@ -212,23 +545,54 @@ dm_get_uuid(char *name, char *uuid)
        else
                uuid[0] = '\0';
 
+       r = 0;
 uuidout:
        dm_task_destroy(dmt);
+       return r;
+}
+
+int dm_get_uuid(const char *name, char *uuid)
+{
+       if (dm_get_prefixed_uuid(name, uuid))
+               return 1;
+
+       if (!strncmp(uuid, UUID_PREFIX, UUID_PREFIX_LEN))
+               memmove(uuid, uuid + UUID_PREFIX_LEN,
+                       strlen(uuid + UUID_PREFIX_LEN) + 1);
+       return 0;
+}
+
+static int
+is_mpath_part(const char *part_name, const char *map_name)
+{
+       char *p;
+       char part_uuid[WWID_SIZE], map_uuid[WWID_SIZE];
+
+       if (dm_get_prefixed_uuid(part_name, part_uuid))
+               return 0;
+
+       if (dm_get_prefixed_uuid(map_name, map_uuid))
+               return 0;
+
+       if (strncmp(part_uuid, "part", 4) != 0)
+               return 0;
+
+       p = strstr(part_uuid, UUID_PREFIX);
+       if (p && !strcmp(p, map_uuid))
+               return 1;
 
        return 0;
 }
 
-extern int
-dm_get_status(char * name, char * outstatus)
+int dm_get_status(const char *name, char *outstatus)
 {
        int r = 1;
        struct dm_task *dmt;
-       void *next = NULL;
        uint64_t start, length;
-       char *target_type;
-       char *status;
+       char *target_type = NULL;
+       char *status = NULL;
 
-       if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
+       if (!(dmt = libmp_dm_task_create(DM_DEVICE_STATUS)))
                return 1;
 
        if (!dm_task_set_name(dmt, name))
@@ -240,8 +604,12 @@ dm_get_status(char * name, char * outstatus)
                goto out;
 
        /* Fetch 1st target */
-       next = dm_get_next_target(dmt, next, &start, &length,
-                                 &target_type, &status);
+       dm_get_next_target(dmt, NULL, &start, &length,
+                          &target_type, &status);
+       if (!status) {
+               condlog(2, "get null status.");
+               goto out;
+       }
 
        if (snprintf(outstatus, PARAMS_SIZE, "%s", status) <= PARAMS_SIZE)
                r = 0;
@@ -257,19 +625,17 @@ out:
  * returns:
  *    1 : match
  *    0 : no match
- *   -1 : empty map
+ *   -1 : empty map, or more than 1 target
  */
-extern int
-dm_type(char * name, char * type)
+int dm_type(const char *name, char *type)
 {
        int r = 0;
        struct dm_task *dmt;
-       void *next = NULL;
        uint64_t start, length;
        char *target_type = NULL;
        char *params;
 
-       if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
+       if (!(dmt = libmp_dm_task_create(DM_DEVICE_TABLE)))
                return 0;
 
        if (!dm_task_set_name(dmt, name))
@@ -281,10 +647,11 @@ dm_type(char * name, char * type)
                goto out;
 
        /* Fetch 1st target */
-       next = dm_get_next_target(dmt, next, &start, &length,
-                                 &target_type, &params);
-
-       if (!target_type)
+       if (dm_get_next_target(dmt, NULL, &start, &length,
+                              &target_type, &params) != NULL)
+               /* multiple targets */
+               r = -1;
+       else if (!target_type)
                r = -1;
        else if (!strcmp(target_type, type))
                r = 1;
@@ -294,43 +661,69 @@ out:
        return r;
 }
 
-static int
-dm_dev_t (char * mapname, char * dev_t, int len)
+int dm_is_mpath(const char *name)
 {
-       int r = 1;
+       int r = 0;
        struct dm_task *dmt;
        struct dm_info info;
+       uint64_t start, length;
+       char *target_type = NULL;
+       char *params;
+       const char *uuid;
 
-       if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
+       if (!(dmt = libmp_dm_task_create(DM_DEVICE_TABLE)))
                return 0;
 
-       if (!dm_task_set_name(dmt, mapname))
+       if (!dm_task_set_name(dmt, name))
                goto out;
 
+       dm_task_no_open_count(dmt);
+
        if (!dm_task_run(dmt))
                goto out;
 
-       if (!dm_task_get_info(dmt, &info))
+       if (!dm_task_get_info(dmt, &info) || !info.exists)
                goto out;
 
-       r = info.open_count;
-       if (snprintf(dev_t, len, "%i:%i", info.major, info.minor) > len)
-                   goto out;
+       uuid = dm_task_get_uuid(dmt);
 
-       r = 0;
+       if (!uuid || strncmp(uuid, UUID_PREFIX, UUID_PREFIX_LEN) != 0)
+               goto out;
+
+       /* Fetch 1st target */
+       dm_get_next_target(dmt, NULL, &start, &length, &target_type, &params);
+
+       if (!target_type || strcmp(target_type, TGT_MPATH) != 0)
+               goto out;
+
+       r = 1;
 out:
        dm_task_destroy(dmt);
        return r;
 }
-       
+
+static int
+dm_dev_t (const char * mapname, char * dev_t, int len)
+{
+       struct dm_info info;
+
+       if (do_get_info(mapname, &info) != 0)
+               return 1;
+
+       if (snprintf(dev_t, len, "%i:%i", info.major, info.minor) > len)
+               return 1;
+
+       return 0;
+}
+
 int
-dm_get_opencount (char * mapname)
+dm_get_opencount (const char * mapname)
 {
        int r = -1;
        struct dm_task *dmt;
        struct dm_info info;
 
-       if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
+       if (!(dmt = libmp_dm_task_create(DM_DEVICE_INFO)))
                return 0;
 
        if (!dm_task_set_name(dmt, mapname))
@@ -342,77 +735,158 @@ dm_get_opencount (char * mapname)
        if (!dm_task_get_info(dmt, &info))
                goto out;
 
+       if (!info.exists)
+               goto out;
+
        r = info.open_count;
 out:
        dm_task_destroy(dmt);
        return r;
 }
-       
+
 int
-dm_get_minor (char * mapname)
+dm_get_major_minor(const char *name, int *major, int *minor)
 {
-       int r = -1;
-       struct dm_task *dmt;
        struct dm_info info;
 
-       if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
-               return 0;
-
-       if (!dm_task_set_name(dmt, mapname))
-               goto out;
+       if (do_get_info(name, &info) != 0)
+               return -1;
 
-       if (!dm_task_run(dmt))
-               goto out;
+       *major = info.major;
+       *minor = info.minor;
+       return 0;
+}
 
-       if (!dm_task_get_info(dmt, &info))
-               goto out;
+static int
+has_partmap(const char *name, void *data)
+{
+       return 1;
+}
 
-       r = info.minor;
-out:
-       dm_task_destroy(dmt);
-       return r;
+static int
+partmap_in_use(const char *name, void *data)
+{
+       int part_count, *ret_count = (int *)data;
+       int open_count = dm_get_opencount(name);
+
+       if (ret_count)
+               (*ret_count)++;
+       part_count = 0;
+       if (open_count) {
+               if (do_foreach_partmaps(name, partmap_in_use, &part_count))
+                       return 1;
+               if (open_count != part_count) {
+                       condlog(2, "%s: map in use", name);
+                       return 1;
+               }
+       }
+       return 0;
 }
-       
-extern int
-dm_flush_map (char * mapname, char * type)
+
+int _dm_flush_map (const char * mapname, int need_sync, int deferred_remove,
+                  int need_suspend, int retries)
 {
        int r;
+       int queue_if_no_path = 0;
+       int udev_flags = 0;
+       unsigned long long mapsize;
+       char params[PARAMS_SIZE] = {0};
+
+       if (!dm_is_mpath(mapname))
+               return 0; /* nothing to do */
+
+       /* if the device currently has no partitions, do not
+          run kpartx on it if you fail to delete it */
+       if (do_foreach_partmaps(mapname, has_partmap, NULL) == 0)
+               udev_flags |= MPATH_UDEV_NO_KPARTX_FLAG;
+
+       /* If you aren't doing a deferred remove, make sure that no
+        * devices are in use */
+       if (!do_deferred(deferred_remove) && partmap_in_use(mapname, NULL))
+                       return 1;
+
+       if (need_suspend &&
+           !dm_get_map(mapname, &mapsize, params) &&
+           strstr(params, "queue_if_no_path")) {
+               if (!dm_queue_if_no_path(mapname, 0))
+                       queue_if_no_path = 1;
+               else
+                       /* Leave queue_if_no_path alone if unset failed */
+                       queue_if_no_path = -1;
+       }
 
-       if (!dm_map_present(mapname))
-               return 0;
-
-       if (!dm_type(mapname, type))
+       if (dm_remove_partmaps(mapname, need_sync, deferred_remove))
                return 1;
 
-       if (dm_remove_partmaps(mapname))
+       if (!do_deferred(deferred_remove) && dm_get_opencount(mapname)) {
+               condlog(2, "%s: map in use", mapname);
                return 1;
+       }
 
-       if (dm_get_opencount(mapname))
-               return 1;
+       do {
+               if (need_suspend && queue_if_no_path != -1)
+                       dm_simplecmd_flush(DM_DEVICE_SUSPEND, mapname, 0);
+
+               r = dm_device_remove(mapname, need_sync, deferred_remove);
+
+               if (r) {
+                       if (do_deferred(deferred_remove)
+                           && dm_map_present(mapname)) {
+                               condlog(4, "multipath map %s remove deferred",
+                                       mapname);
+                               return 2;
+                       }
+                       condlog(4, "multipath map %s removed", mapname);
+                       return 0;
+               } else {
+                       condlog(2, "failed to remove multipath map %s",
+                               mapname);
+                       if (need_suspend && queue_if_no_path != -1) {
+                               dm_simplecmd_noflush(DM_DEVICE_RESUME,
+                                                    mapname, udev_flags);
+                       }
+               }
+               if (retries)
+                       sleep(1);
+       } while (retries-- > 0);
 
-       r = dm_simplecmd(DM_DEVICE_REMOVE, mapname);
+       if (queue_if_no_path == 1)
+               dm_queue_if_no_path(mapname, 1);
 
-       if (r) {
-               condlog(4, "multipath map %s removed", mapname);
-               return 0;
-       }
        return 1;
 }
 
-extern int
-dm_flush_maps (char * type)
-{
-       int r = 0;
-       struct dm_task *dmt;
-       struct dm_names *names;
-       unsigned next = 0;
+#ifdef LIBDM_API_DEFERRED
 
-       if (!(dmt = dm_task_create (DM_DEVICE_LIST)))
-               return 0;
+int
+dm_flush_map_nopaths(const char * mapname, int deferred_remove)
+{
+       return _dm_flush_map(mapname, 1, deferred_remove, 0, 0);
+}
 
-       dm_task_no_open_count(dmt);
+#else
 
-       if (!dm_task_run (dmt))
+int
+dm_flush_map_nopaths(const char * mapname, int deferred_remove)
+{
+       return _dm_flush_map(mapname, 1, 0, 0, 0);
+}
+
+#endif
+
+int dm_flush_maps (int retries)
+{
+       int r = 0;
+       struct dm_task *dmt;
+       struct dm_names *names;
+       unsigned next = 0;
+
+       if (!(dmt = libmp_dm_task_create (DM_DEVICE_LIST)))
+               return 0;
+
+       dm_task_no_open_count(dmt);
+
+       if (!dm_task_run (dmt))
                goto out;
 
        if (!(names = dm_task_get_names (dmt)))
@@ -422,24 +896,23 @@ dm_flush_maps (char * type)
                goto out;
 
        do {
-               r += dm_flush_map(names->name, type);
+               r |= dm_suspend_and_flush_map(names->name, retries);
                next = names->next;
                names = (void *) names + next;
        } while (next);
 
-       out:
+out:
        dm_task_destroy (dmt);
        return r;
 }
 
 int
-dm_fail_path(char * mapname, char * path)
+dm_message(const char * mapname, char * message)
 {
        int r = 1;
        struct dm_task *dmt;
-       char str[32];
 
-       if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
+       if (!(dmt = libmp_dm_task_create(DM_DEVICE_TARGET_MSG)))
                return 1;
 
        if (!dm_task_set_name(dmt, mapname))
@@ -448,10 +921,7 @@ dm_fail_path(char * mapname, char * path)
        if (!dm_task_set_sector(dmt, 0))
                goto out;
 
-       if (snprintf(str, 32, "fail_path %s\n", path) > 32)
-               goto out;
-
-       if (!dm_task_set_message(dmt, str))
+       if (!dm_task_set_message(dmt, message))
                goto out;
 
        dm_task_no_open_count(dmt);
@@ -461,148 +931,115 @@ dm_fail_path(char * mapname, char * path)
 
        r = 0;
 out:
+       if (r)
+               condlog(0, "DM message failed [%s]", message);
+
        dm_task_destroy(dmt);
        return r;
 }
 
 int
-dm_reinstate(char * mapname, char * path)
+dm_fail_path(const char * mapname, char * path)
 {
-       int r = 1;
-       struct dm_task *dmt;
-       char str[32];
+       char message[32];
 
-       if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
+       if (snprintf(message, 32, "fail_path %s", path) > 32)
                return 1;
 
-       if (!dm_task_set_name(dmt, mapname))
-               goto out;
-
-       if (!dm_task_set_sector(dmt, 0))
-               goto out;
-
-       if (snprintf(str, 32, "reinstate_path %s\n", path) > 32)
-               goto out;
-
-       if (!dm_task_set_message(dmt, str))
-               goto out;
+       return dm_message(mapname, message);
+}
 
-       dm_task_no_open_count(dmt);
+int
+dm_reinstate_path(const char * mapname, char * path)
+{
+       char message[32];
 
-       if (!dm_task_run(dmt))
-               goto out;
+       if (snprintf(message, 32, "reinstate_path %s", path) > 32)
+               return 1;
 
-       r = 0;
-out:
-       dm_task_destroy(dmt);
-       return r;
+       return dm_message(mapname, message);
 }
 
 int
-dm_queue_if_no_path(char *mapname, int enable)
+dm_queue_if_no_path(const char *mapname, int enable)
 {
-       int r = 1;
-       struct dm_task *dmt;
-       char *str;
+       char *message;
 
        if (enable)
-               str = "queue_if_no_path\n";
+               message = "queue_if_no_path";
        else
-               str = "fail_if_no_path\n";
-
-       if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
-               return 1;
-
-       if (!dm_task_set_name(dmt, mapname))
-               goto out;
-
-       if (!dm_task_set_sector(dmt, 0))
-               goto out;
-
-       if (!dm_task_set_message(dmt, str))
-               goto out;
-
-       dm_task_no_open_count(dmt);
-
-       if (!dm_task_run(dmt))
-               goto out;
+               message = "fail_if_no_path";
 
-       r = 0;
-out:
-       dm_task_destroy(dmt);
-       return r;
+       return dm_message(mapname, message);
 }
 
 static int
-dm_groupmsg (char * msg, char * mapname, int index)
+dm_groupmsg (const char * msg, const char * mapname, int index)
 {
-       int r = 0;
-       struct dm_task *dmt;
-       char str[24];
-
-       if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
-               return 0;
-
-       if (!dm_task_set_name(dmt, mapname))
-               goto out;
-
-       if (!dm_task_set_sector(dmt, 0))
-               goto out;
-
-       snprintf(str, 24, "%s_group %i\n", msg, index);
-
-       if (!dm_task_set_message(dmt, str))
-               goto out;
-
-       dm_task_no_open_count(dmt);
-
-       if (!dm_task_run(dmt))
-               goto out;
-
-       condlog(3, "message %s 0 %s", mapname, str);
-       r = 1;
-
-       out:
-       if (!r)
-               condlog(3, "message %s 0 %s failed", mapname, str);
+       char message[32];
 
-       dm_task_destroy(dmt);
+       if (snprintf(message, 32, "%s_group %i", msg, index) > 32)
+               return 1;
 
-       return r;
+       return dm_message(mapname, message);
 }
 
 int
-dm_switchgroup(char * mapname, int index)
+dm_switchgroup(const char * mapname, int index)
 {
-       return dm_groupmsg("switch", mapname,index);
+       return dm_groupmsg("switch", mapname, index);
 }
 
 int
-dm_enablegroup(char * mapname, int index)
+dm_enablegroup(const char * mapname, int index)
 {
-       return dm_groupmsg("enable", mapname,index);
+       return dm_groupmsg("enable", mapname, index);
 }
 
 int
-dm_disablegroup(char * mapname, int index)
+dm_disablegroup(const char * mapname, int index)
 {
-       return dm_groupmsg("disable", mapname,index);
+       return dm_groupmsg("disable", mapname, index);
+}
+
+struct multipath *dm_get_multipath(const char *name)
+{
+       struct multipath *mpp = NULL;
+
+       mpp = alloc_multipath();
+       if (!mpp)
+               return NULL;
+
+       mpp->alias = STRDUP(name);
+
+       if (!mpp->alias)
+               goto out;
+
+       if (dm_get_map(name, &mpp->size, NULL))
+               goto out;
+
+       dm_get_uuid(name, mpp->wwid);
+       dm_get_info(name, &mpp->dmi);
+
+       return mpp;
+out:
+       free_multipath(mpp, KEEP_PATHS);
+       return NULL;
 }
 
 int
-dm_get_maps (vector mp, char * type)
+dm_get_maps (vector mp)
 {
        struct multipath * mpp;
        int r = 1;
-       int info;
        struct dm_task *dmt;
        struct dm_names *names;
        unsigned next = 0;
 
-       if (!type || !mp)
+       if (!mp)
                return 1;
 
-       if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
+       if (!(dmt = libmp_dm_task_create(DM_DEVICE_LIST)))
                return 1;
 
        dm_task_no_open_count(dmt);
@@ -619,92 +1056,62 @@ dm_get_maps (vector mp, char * type)
        }
 
        do {
-               info = dm_type(names->name, type);
-
-               if (!info)
+               if (!dm_is_mpath(names->name))
                        goto next;
 
-               mpp = alloc_multipath();
-
+               mpp = dm_get_multipath(names->name);
                if (!mpp)
                        goto out;
 
-               mpp->alias = STRDUP(names->name);
-
-               if (!mpp->alias)
-                       goto out1;
-
-               if (info > 0) {
-                       if (dm_get_map(names->name, &mpp->size, mpp->params))
-                               goto out1;
-
-                       if (dm_get_status(names->name, mpp->status))
-                               goto out1;
-
-                       dm_get_uuid(names->name, mpp->wwid);
-               }
-
                if (!vector_alloc_slot(mp))
-                       goto out1;
+                       goto out;
 
                vector_set_slot(mp, mpp);
                mpp = NULL;
 next:
-                next = names->next;
-                names = (void *) names + next;
+               next = names->next;
+               names = (void *) names + next;
        } while (next);
 
        r = 0;
        goto out;
-out1:
-       free_multipath(mpp, KEEP_PATHS);
 out:
        dm_task_destroy (dmt);
        return r;
 }
 
 int
-dm_geteventnr (char *name)
+dm_geteventnr (const char *name)
 {
-       struct dm_task *dmt;
        struct dm_info info;
 
-       if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
-               return 0;
+       if (do_get_info(name, &info) != 0)
+               return -1;
 
-       if (!dm_task_set_name(dmt, name))
-               goto out;
-
-       dm_task_no_open_count(dmt);
-
-       if (!dm_task_run(dmt))
-               goto out;
-
-       if (!dm_task_get_info(dmt, &info)) {
-               info.event_nr = 0;
-               goto out;
-       }
+       return info.event_nr;
+}
 
-       if (!info.exists) {
-               info.event_nr = 0;
-               goto out;
-       }
+int
+dm_is_suspended(const char *name)
+{
+       struct dm_info info;
 
-out:
-       dm_task_destroy(dmt);
+       if (do_get_info(name, &info) != 0)
+               return -1;
 
-       return info.event_nr;
+       return info.suspended;
 }
 
 char *
 dm_mapname(int major, int minor)
 {
-       char * response;
+       char * response = NULL;
+       const char *map;
        struct dm_task *dmt;
        int r;
        int loop = MAX_WAIT * LOOPS_PER_SEC;
 
-       if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
+       if (!(dmt = libmp_dm_task_create(DM_DEVICE_STATUS)))
                return NULL;
 
        if (!dm_task_set_major(dmt, major) ||
@@ -718,9 +1125,7 @@ dm_mapname(int major, int minor)
         * daemon uev_trigger -> uev_add_map
         */
        while (--loop) {
-               dm_shut_log();
                r = dm_task_run(dmt);
-               dm_restore_log();
 
                if (r)
                        break;
@@ -733,7 +1138,10 @@ dm_mapname(int major, int minor)
                goto bad;
        }
 
-       response = STRDUP((char *)dm_task_get_name(dmt));
+       map = dm_task_get_name(dmt);
+       if (map && strlen(map))
+               response = STRDUP((const char *)dm_task_get_name(dmt));
+
        dm_task_destroy(dmt);
        return response;
 bad:
@@ -742,8 +1150,10 @@ bad:
        return NULL;
 }
 
-int
-dm_remove_partmaps (char * mapname)
+static int
+do_foreach_partmaps (const char * mapname,
+                    int (*partmap_func)(const char *, void *),
+                    void *data)
 {
        struct dm_task *dmt;
        struct dm_names *names;
@@ -752,8 +1162,9 @@ dm_remove_partmaps (char * mapname)
        unsigned long long size;
        char dev_t[32];
        int r = 1;
+       char *p;
 
-       if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
+       if (!(dmt = libmp_dm_task_create(DM_DEVICE_LIST)))
                return 1;
 
        dm_task_no_open_count(dmt);
@@ -775,20 +1186,15 @@ dm_remove_partmaps (char * mapname)
        do {
                if (
                    /*
-                    * if devmap target is "linear"
-                    */
-                   dm_type(names->name, "linear") &&
-
-                   /*
-                    * and the multipath mapname and the part mapname start
-                    * the same
+                    * if there is only a single "linear" target
                     */
-                   !strncmp(names->name, mapname, strlen(mapname)) &&
+                   (dm_type(names->name, TGT_PART) == 1) &&
 
                    /*
-                    * and the opencount is 0 for us to allow removal
+                    * and the uuid of the target is a partition of the
+                    * uuid of the multipath device
                     */
-                   !dm_get_opencount(names->name) &&
+                   is_mpath_part(names->name, mapname) &&
 
                    /*
                     * and we can fetch the map table from the kernel
@@ -798,16 +1204,12 @@ dm_remove_partmaps (char * mapname)
                    /*
                     * and the table maps over the multipath map
                     */
-                   strstr(params, dev_t)
+                   (p = strstr(params, dev_t)) &&
+                   !isdigit(*(p + strlen(dev_t)))
                   ) {
-                               /*
-                                * then it's a kpartx generated partition.
-                                * remove it.
-                                */
-                               condlog(4, "partition map %s removed",
-                                       names->name);
-                               dm_simplecmd(DM_DEVICE_REMOVE, names->name);
-                  }
+                       if (partmap_func(names->name, data) != 0)
+                               goto out;
+               }
 
                next = names->next;
                names = (void *) names + next;
@@ -819,30 +1221,377 @@ out:
        return r;
 }
 
-#if 0
+struct remove_data {
+       int need_sync;
+       int deferred_remove;
+};
+
+static int
+remove_partmap(const char *name, void *data)
+{
+       struct remove_data *rd = (struct remove_data *)data;
+
+       if (dm_get_opencount(name)) {
+               dm_remove_partmaps(name, rd->need_sync, rd->deferred_remove);
+               if (!do_deferred(rd->deferred_remove) &&
+                   dm_get_opencount(name)) {
+                       condlog(2, "%s: map in use", name);
+                       return 1;
+               }
+       }
+       condlog(4, "partition map %s removed", name);
+       dm_device_remove(name, rd->need_sync, rd->deferred_remove);
+       return 0;
+}
+
 int
-dm_rename (char * old, char * new)
+dm_remove_partmaps (const char * mapname, int need_sync, int deferred_remove)
 {
-       int r = 1;
-       struct dm_task *dmt;
+       struct remove_data rd = { need_sync, deferred_remove };
+       return do_foreach_partmaps(mapname, remove_partmap, &rd);
+}
+
+#ifdef LIBDM_API_DEFERRED
+
+static int
+cancel_remove_partmap (const char *name, void *unused)
+{
+       if (dm_get_opencount(name))
+               dm_cancel_remove_partmaps(name);
+       if (dm_message(name, "@cancel_deferred_remove") != 0)
+               condlog(0, "%s: can't cancel deferred remove: %s", name,
+                       strerror(errno));
+       return 0;
+}
 
-       if (!(dmt = dm_task_create(DM_DEVICE_RENAME)))
+static int
+dm_get_deferred_remove (const char * mapname)
+{
+       struct dm_info info;
+
+       if (do_get_info(mapname, &info) != 0)
+               return -1;
+
+       return info.deferred_remove;
+}
+
+static int
+dm_cancel_remove_partmaps(const char * mapname) {
+       return do_foreach_partmaps(mapname, cancel_remove_partmap, NULL);
+}
+
+int
+dm_cancel_deferred_remove (struct multipath *mpp)
+{
+       int r = 0;
+
+       if (!dm_get_deferred_remove(mpp->alias))
                return 0;
+       if (mpp->deferred_remove == DEFERRED_REMOVE_IN_PROGRESS)
+               mpp->deferred_remove = DEFERRED_REMOVE_ON;
+
+       dm_cancel_remove_partmaps(mpp->alias);
+       r = dm_message(mpp->alias, "@cancel_deferred_remove");
+       if (r)
+               condlog(0, "%s: can't cancel deferred remove: %s", mpp->alias,
+                               strerror(errno));
+       else
+               condlog(2, "%s: canceled deferred remove", mpp->alias);
+       return r;
+}
+
+#else
+
+int
+dm_cancel_deferred_remove (struct multipath *mpp)
+{
+       return 0;
+}
+
+#endif
+
+static struct dm_info *
+alloc_dminfo (void)
+{
+       return MALLOC(sizeof(struct dm_info));
+}
+
+int
+dm_get_info (const char * mapname, struct dm_info ** dmi)
+{
+       if (!mapname)
+               return 1;
+
+       if (!*dmi)
+               *dmi = alloc_dminfo();
+
+       if (!*dmi)
+               return 1;
+
+       if (do_get_info(mapname, *dmi) != 0) {
+               memset(*dmi, 0, sizeof(struct dm_info));
+               FREE(*dmi);
+               *dmi = NULL;
+               return 1;
+       }
+       return 0;
+}
+
+struct rename_data {
+       const char *old;
+       char *new;
+       char *delim;
+};
+
+static int
+rename_partmap (const char *name, void *data)
+{
+       char buff[PARAMS_SIZE];
+       int offset;
+       struct rename_data *rd = (struct rename_data *)data;
+
+       if (strncmp(name, rd->old, strlen(rd->old)) != 0)
+               return 0;
+       for (offset = strlen(rd->old); name[offset] && !(isdigit(name[offset])); offset++); /* do nothing */
+       snprintf(buff, PARAMS_SIZE, "%s%s%s", rd->new, rd->delim,
+                name + offset);
+       dm_rename(name, buff, rd->delim, SKIP_KPARTX_OFF);
+       condlog(4, "partition map %s renamed", name);
+       return 0;
+}
+
+int
+dm_rename_partmaps (const char * old, char * new, char *delim)
+{
+       struct rename_data rd;
+
+       rd.old = old;
+       rd.new = new;
+
+       if (delim)
+               rd.delim = delim;
+       else {
+               if (isdigit(new[strlen(new)-1]))
+                       rd.delim = "p";
+               else
+                       rd.delim = "";
+       }
+       return do_foreach_partmaps(old, rename_partmap, &rd);
+}
+
+int
+dm_rename (const char * old, char * new, char *delim, int skip_kpartx)
+{
+       int r = 0;
+       struct dm_task *dmt;
+       uint32_t cookie = 0;
+       uint16_t udev_flags = DM_UDEV_DISABLE_LIBRARY_FALLBACK | ((skip_kpartx == SKIP_KPARTX_ON)? MPATH_UDEV_NO_KPARTX_FLAG : 0);
+
+       if (dm_rename_partmaps(old, new, delim))
+               return r;
+
+       if (!(dmt = libmp_dm_task_create(DM_DEVICE_RENAME)))
+               return r;
 
        if (!dm_task_set_name(dmt, old))
                goto out;
 
        if (!dm_task_set_newname(dmt, new))
                goto out;
-       
+
+       dm_task_no_open_count(dmt);
+
+       if (!dm_task_set_cookie(dmt, &cookie, udev_flags))
+               goto out;
+       r = dm_task_run(dmt);
+
+       dm_udev_wait(cookie);
+
+out:
+       dm_task_destroy(dmt);
+
+       return r;
+}
+
+void dm_reassign_deps(char *table, const char *dep, const char *newdep)
+{
+       char *n;
+       const char *p, *newtable;
+
+       newtable = strdup(table);
+       if (!newtable)
+               return;
+       p = strstr(newtable, dep);
+       n = table + (p - newtable);
+       strcpy(n, newdep);
+       n += strlen(newdep);
+       p += strlen(dep);
+       strcat(n, p);
+       FREE_CONST(newtable);
+}
+
+int dm_reassign_table(const char *name, char *old, char *new)
+{
+       int r = 0, modified = 0;
+       uint64_t start, length;
+       struct dm_task *dmt, *reload_dmt;
+       char *target, *params = NULL;
+       char *buff;
+       void *next = NULL;
+
+       if (!(dmt = libmp_dm_task_create(DM_DEVICE_TABLE)))
+               return 0;
+
+       if (!dm_task_set_name(dmt, name))
+               goto out;
+
        dm_task_no_open_count(dmt);
 
        if (!dm_task_run(dmt))
                goto out;
+       if (!(reload_dmt = libmp_dm_task_create(DM_DEVICE_RELOAD)))
+               goto out;
+       if (!dm_task_set_name(reload_dmt, name))
+               goto out_reload;
 
-       r = 0;
+       do {
+               next = dm_get_next_target(dmt, next, &start, &length,
+                                         &target, &params);
+               buff = strdup(params);
+               if (!buff) {
+                       condlog(3, "%s: failed to replace target %s, "
+                               "out of memory", name, target);
+                       goto out_reload;
+               }
+               if (strcmp(target, TGT_MPATH) && strstr(params, old)) {
+                       condlog(3, "%s: replace target %s %s",
+                               name, target, buff);
+                       dm_reassign_deps(buff, old, new);
+                       condlog(3, "%s: with target %s %s",
+                               name, target, buff);
+                       modified++;
+               }
+               dm_task_add_target(reload_dmt, start, length, target, buff);
+               free(buff);
+       } while (next);
+
+       if (modified) {
+               dm_task_no_open_count(reload_dmt);
+
+               if (!dm_task_run(reload_dmt)) {
+                       condlog(3, "%s: failed to reassign targets", name);
+                       goto out_reload;
+               }
+               dm_simplecmd_noflush(DM_DEVICE_RESUME, name,
+                                    MPATH_UDEV_RELOAD_FLAG);
+       }
+       r = 1;
+
+out_reload:
+       dm_task_destroy(reload_dmt);
 out:
        dm_task_destroy(dmt);
        return r;
 }
-#endif
+
+
+/*
+ * Reassign existing device-mapper table(s) to not use
+ * the block devices but point to the multipathed
+ * device instead
+ */
+int dm_reassign(const char *mapname)
+{
+       struct dm_deps *deps;
+       struct dm_task *dmt;
+       struct dm_info info;
+       char dev_t[32], dm_dep[32];
+       int r = 0, i;
+
+       if (dm_dev_t(mapname, &dev_t[0], 32)) {
+               condlog(3, "%s: failed to get device number", mapname);
+               return 1;
+       }
+
+       if (!(dmt = libmp_dm_task_create(DM_DEVICE_DEPS))) {
+               condlog(3, "%s: couldn't make dm task", mapname);
+               return 0;
+       }
+
+       if (!dm_task_set_name(dmt, mapname))
+               goto out;
+
+       dm_task_no_open_count(dmt);
+
+       if (!dm_task_run(dmt))
+               goto out;
+
+       if (!dm_task_get_info(dmt, &info))
+               goto out;
+
+       if (!(deps = dm_task_get_deps(dmt)))
+               goto out;
+
+       if (!info.exists)
+               goto out;
+
+       for (i = 0; i < deps->count; i++) {
+               sprintf(dm_dep, "%d:%d",
+                       major(deps->device[i]),
+                       minor(deps->device[i]));
+               sysfs_check_holders(dm_dep, dev_t);
+       }
+
+       r = 1;
+out:
+       dm_task_destroy (dmt);
+       return r;
+}
+
+int dm_setgeometry(struct multipath *mpp)
+{
+       struct dm_task *dmt;
+       struct path *pp;
+       char heads[4], sectors[4];
+       char cylinders[10], start[32];
+       int r = 0;
+
+       if (!mpp)
+               return 1;
+
+       pp = first_path(mpp);
+       if (!pp) {
+               condlog(3, "%s: no path for geometry", mpp->alias);
+               return 1;
+       }
+       if (pp->geom.cylinders == 0 ||
+           pp->geom.heads == 0 ||
+           pp->geom.sectors == 0) {
+               condlog(3, "%s: invalid geometry on %s", mpp->alias, pp->dev);
+               return 1;
+       }
+
+       if (!(dmt = libmp_dm_task_create(DM_DEVICE_SET_GEOMETRY)))
+               return 0;
+
+       if (!dm_task_set_name(dmt, mpp->alias))
+               goto out;
+
+       dm_task_no_open_count(dmt);
+
+       /* What a sick interface ... */
+       snprintf(heads, 4, "%u", pp->geom.heads);
+       snprintf(sectors, 4, "%u", pp->geom.sectors);
+       snprintf(cylinders, 10, "%u", pp->geom.cylinders);
+       snprintf(start, 32, "%lu", pp->geom.start);
+       if (!dm_task_set_geometry(dmt, cylinders, heads, sectors, start)) {
+               condlog(3, "%s: Failed to set geometry", mpp->alias);
+               goto out;
+       }
+
+       r = dm_task_run(dmt);
+out:
+       dm_task_destroy(dmt);
+
+       return r;
+}