Imported Upstream version 0.7.5
[platform/upstream/multipath-tools.git] / libmultipath / devmapper.c
index 5eb1713..607aea8 100644 (file)
@@ -12,6 +12,7 @@
 #include <ctype.h>
 #include <unistd.h>
 #include <errno.h>
+#include <sys/sysmacros.h>
 
 #include "checkers.h"
 #include "vector.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
 
-#define UUID_PREFIX "mpath-"
-#define UUID_PREFIX_LEN 6
-
 static int dm_conf_verbosity;
 
 #ifdef LIBDM_API_DEFERRED
@@ -94,8 +94,8 @@ dm_write_log (int level, const char *file, int line, const char *f, ...)
        return;
 }
 
-extern void
-dm_init(int v) {
+void dm_init(int v)
+{
        dm_log_init(&dm_write_log);
        dm_log_init_verbose(v + 3);
 }
@@ -177,7 +177,7 @@ out:
 }
 
 static int
-dm_drv_prereq (void)
+dm_drv_prereq (unsigned int *ver)
 {
        unsigned int minv[3] = {1, 0, 3};
        unsigned int version[3] = {0, 0, 0};
@@ -192,20 +192,51 @@ dm_drv_prereq (void)
        condlog(3, "DM multipath kernel driver v%u.%u.%u",
                v[0], v[1], v[2]);
 
-       if VERSION_GE(v, minv)
+       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;
 }
 
-extern int
-dm_prereq (void)
+static int dm_prereq(unsigned int *v)
 {
        if (dm_lib_prereq())
                return 1;
-       return dm_drv_prereq();
+       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)
@@ -213,12 +244,13 @@ dm_prereq (void)
 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 && (task == DM_DEVICE_RESUME ||
-                                           task == DM_DEVICE_REMOVE));
+       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))
@@ -248,13 +280,13 @@ out:
        return r;
 }
 
-extern int
-dm_simplecmd_flush (int task, const char *name, uint16_t udev_flags) {
+int dm_simplecmd_flush (int task, const char *name, uint16_t udev_flags)
+{
        return dm_simplecmd(task, name, 0, 1, udev_flags, 0);
 }
 
-extern int
-dm_simplecmd_noflush (int task, const char *name, uint16_t udev_flags) {
+int dm_simplecmd_noflush (int task, const char *name, uint16_t udev_flags)
+{
        return dm_simplecmd(task, name, 1, 1, udev_flags, 0);
 }
 
@@ -266,13 +298,16 @@ dm_device_remove (const char *name, int needsync, int deferred_remove) {
 
 static int
 dm_addmap (int task, const char *target, struct multipath *mpp,
-          char * params, int ro) {
+          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, mpp->alias))
@@ -319,8 +354,7 @@ dm_addmap (int task, const char *target, struct multipath *mpp,
        dm_task_no_open_count(dmt);
 
        if (task == DM_DEVICE_CREATE &&
-           !dm_task_set_cookie(dmt, &cookie,
-                               DM_UDEV_DISABLE_LIBRARY_FALLBACK))
+           !dm_task_set_cookie(dmt, &cookie, udev_flags))
                goto freeout;
 
        r = dm_task_run (dmt);
@@ -337,14 +371,27 @@ addout:
        return r;
 }
 
-extern int
-dm_addmap_create (struct multipath *mpp, char * params) {
+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))
+               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.
@@ -367,11 +414,10 @@ dm_addmap_create (struct multipath *mpp, char * params) {
 #define ADDMAP_RW 0
 #define ADDMAP_RO 1
 
-extern int
-dm_addmap_reload (struct multipath *mpp, char *params, int flush)
+int dm_addmap_reload(struct multipath *mpp, char *params, int flush)
 {
-       int r;
-       uint16_t udev_flags = flush ? 0 : MPATH_UDEV_RELOAD_FLAG;
+       int r = 0;
+       uint16_t udev_flags = build_udev_flags(mpp, 1);
 
        /*
         * DM_DEVICE_RELOAD cannot wait on a cookie, as
@@ -379,30 +425,40 @@ dm_addmap_reload (struct multipath *mpp, char *params, int flush)
         * DM_DEVICE_RESUME. So call DM_DEVICE_RESUME
         * after each successful call to DM_DEVICE_RELOAD.
         */
-       r = dm_addmap(DM_DEVICE_RELOAD, TGT_MPATH, mpp, params, ADDMAP_RW);
+       if (!mpp->force_readonly)
+               r = dm_addmap(DM_DEVICE_RELOAD, TGT_MPATH, mpp, params,
+                             ADDMAP_RW, 0);
        if (!r) {
-               if (errno != EROFS)
+               if (!mpp->force_readonly && errno != EROFS)
                        return 0;
                r = dm_addmap(DM_DEVICE_RELOAD, TGT_MPATH, mpp,
-                             params, ADDMAP_RO);
+                             params, ADDMAP_RO, 0);
        }
        if (r)
-               r = dm_simplecmd(DM_DEVICE_RESUME, mpp->alias, flush,
+               r = dm_simplecmd(DM_DEVICE_RESUME, mpp->alias, !flush,
                                 1, udev_flags, 0);
-       return r;
+       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;
 }
 
-extern int
-dm_map_present (const char * str)
+static int
+do_get_info(const char *name, struct dm_info *info)
 {
-       int r = 0;
+       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);
@@ -410,18 +466,26 @@ dm_map_present (const 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(const 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;
@@ -429,7 +493,7 @@ dm_get_map(const char * name, unsigned long long * size, char * outparams)
        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))
@@ -465,7 +529,7 @@ dm_get_prefixed_uuid(const char *name, char *uuid)
        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;
 
@@ -487,57 +551,48 @@ uuidout:
        return r;
 }
 
-extern int
-dm_get_uuid(char *name, char *uuid)
+int dm_get_uuid(const char *name, char *uuid)
 {
-       char uuidtmp[WWID_SIZE];
-
-       if (dm_get_prefixed_uuid(name, uuidtmp))
+       if (dm_get_prefixed_uuid(name, uuid))
                return 1;
 
-       if (!strncmp(uuidtmp, UUID_PREFIX, UUID_PREFIX_LEN))
-               strcpy(uuid, uuidtmp + UUID_PREFIX_LEN);
-       else
-               strcpy(uuid, uuidtmp);
-
+       if (!strncmp(uuid, UUID_PREFIX, UUID_PREFIX_LEN))
+               memmove(uuid, uuid + UUID_PREFIX_LEN,
+                       strlen(uuid + UUID_PREFIX_LEN) + 1);
        return 0;
 }
 
-/*
- * returns:
- *    0 : if both uuids end with same suffix which starts with UUID_PREFIX
- *    1 : otherwise
- */
-int
-dm_compare_uuid(const char* mapname1, const char* mapname2)
+static int
+is_mpath_part(const char *part_name, const char *map_name)
 {
-       char *p1, *p2;
-       char uuid1[WWID_SIZE], uuid2[WWID_SIZE];
+       char *p;
+       char part_uuid[WWID_SIZE], map_uuid[WWID_SIZE];
 
-       if (dm_get_prefixed_uuid(mapname1, uuid1))
-               return 1;
+       if (dm_get_prefixed_uuid(part_name, part_uuid))
+               return 0;
 
-       if (dm_get_prefixed_uuid(mapname2, uuid2))
-               return 1;
+       if (dm_get_prefixed_uuid(map_name, map_uuid))
+               return 0;
 
-       p1 = strstr(uuid1, UUID_PREFIX);
-       p2 = strstr(uuid2, UUID_PREFIX);
-       if (p1 && p2 && !strcmp(p1, p2))
+       if (strncmp(part_uuid, "part", 4) != 0)
                return 0;
 
-       return 1;
+       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;
        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))
@@ -551,6 +606,10 @@ dm_get_status(char * name, char * outstatus)
        /* Fetch 1st target */
        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;
@@ -566,10 +625,9 @@ out:
  * returns:
  *    1 : match
  *    0 : no match
- *   -1 : empty map
+ *   -1 : empty map, or more than 1 target
  */
-extern int
-dm_type(const char * name, char * type)
+int dm_type(const char *name, char *type)
 {
        int r = 0;
        struct dm_task *dmt;
@@ -577,7 +635,7 @@ dm_type(const char * name, char * type)
        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))
@@ -589,10 +647,11 @@ dm_type(const char * name, char * type)
                goto out;
 
        /* Fetch 1st target */
-       dm_get_next_target(dmt, NULL, &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;
@@ -602,8 +661,7 @@ out:
        return r;
 }
 
-extern int
-dm_is_mpath(const char * name)
+int dm_is_mpath(const char *name)
 {
        int r = 0;
        struct dm_task *dmt;
@@ -613,7 +671,7 @@ dm_is_mpath(const char * name)
        char *params;
        const char *uuid;
 
-       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))
@@ -647,29 +705,15 @@ out:
 static int
 dm_dev_t (const char * mapname, char * dev_t, int len)
 {
-       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 (!dm_task_run(dmt))
-               goto out;
-
-       if (!dm_task_get_info(dmt, &info) || !info.exists)
-               goto out;
+       if (do_get_info(mapname, &info) != 0)
+               return 1;
 
        if (snprintf(dev_t, len, "%i:%i", info.major, info.minor) > len)
-               goto out;
+               return 1;
 
-       r = 0;
-out:
-       dm_task_destroy(dmt);
-       return r;
+       return 0;
 }
 
 int
@@ -679,7 +723,7 @@ dm_get_opencount (const char * mapname)
        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))
@@ -701,59 +745,22 @@ out:
 }
 
 int
-dm_get_major (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 (!dm_task_run(dmt))
-               goto out;
-
-       if (!dm_task_get_info(dmt, &info))
-               goto out;
-
-       if (!info.exists)
-               goto out;
+       if (do_get_info(name, &info) != 0)
+               return -1;
 
-       r = info.major;
-out:
-       dm_task_destroy(dmt);
-       return r;
+       *major = info.major;
+       *minor = info.minor;
+       return 0;
 }
 
-int
-dm_get_minor (char * mapname)
+static int
+has_partmap(const char *name, void *data)
 {
-       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 (!dm_task_run(dmt))
-               goto out;
-
-       if (!dm_task_get_info(dmt, &info))
-               goto out;
-
-       if (!info.exists)
-               goto out;
-
-       r = info.minor;
-out:
-       dm_task_destroy(dmt);
-       return r;
+       return 1;
 }
 
 static int
@@ -776,19 +783,38 @@ partmap_in_use(const char *name, void *data)
        return 0;
 }
 
-extern int
-_dm_flush_map (const char * mapname, int need_sync, int deferred_remove)
+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_remove_partmaps(mapname, need_sync, deferred_remove))
                return 1;
 
@@ -797,17 +823,36 @@ _dm_flush_map (const char * mapname, int need_sync, int deferred_remove)
                return 1;
        }
 
-       r = dm_device_remove(mapname, need_sync, deferred_remove);
+       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",
+               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);
-                       return 2;
+                       if (need_suspend && queue_if_no_path != -1) {
+                               dm_simplecmd_noflush(DM_DEVICE_RESUME,
+                                                    mapname, udev_flags);
+                       }
                }
-               condlog(4, "multipath map %s removed", mapname);
-               return 0;
-       }
+               if (retries)
+                       sleep(1);
+       } while (retries-- > 0);
+
+       if (queue_if_no_path == 1)
+               dm_queue_if_no_path(mapname, 1);
+
        return 1;
 }
 
@@ -816,7 +861,7 @@ _dm_flush_map (const char * mapname, int need_sync, int deferred_remove)
 int
 dm_flush_map_nopaths(const char * mapname, int deferred_remove)
 {
-       return _dm_flush_map(mapname, 1, deferred_remove);
+       return _dm_flush_map(mapname, 1, deferred_remove, 0, 0);
 }
 
 #else
@@ -824,54 +869,19 @@ dm_flush_map_nopaths(const char * mapname, int deferred_remove)
 int
 dm_flush_map_nopaths(const char * mapname, int deferred_remove)
 {
-       return _dm_flush_map(mapname, 1, 0);
+       return _dm_flush_map(mapname, 1, 0, 0, 0);
 }
 
 #endif
 
-extern int
-dm_suspend_and_flush_map (const char * mapname)
-{
-       int s = 0, queue_if_no_path = 0;
-       unsigned long long mapsize;
-       char params[PARAMS_SIZE] = {0};
-
-       if (!dm_is_mpath(mapname))
-               return 0; /* nothing to do */
-
-       if (!dm_get_map(mapname, &mapsize, params)) {
-               if (strstr(params, "queue_if_no_path"))
-                       queue_if_no_path = 1;
-       }
-
-       if (queue_if_no_path)
-               s = dm_queue_if_no_path((char *)mapname, 0);
-       /* Leave queue_if_no_path alone if unset failed */
-       if (s)
-               queue_if_no_path = 0;
-       else
-               s = dm_simplecmd_flush(DM_DEVICE_SUSPEND, mapname, 0);
-
-       if (!dm_flush_map(mapname)) {
-               condlog(4, "multipath map %s removed", mapname);
-               return 0;
-       }
-       condlog(2, "failed to remove multipath map %s", mapname);
-       dm_simplecmd_noflush(DM_DEVICE_RESUME, mapname, 0);
-       if (queue_if_no_path)
-               s = dm_queue_if_no_path((char *)mapname, 1);
-       return 1;
-}
-
-extern int
-dm_flush_maps (void)
+int dm_flush_maps (int retries)
 {
        int r = 0;
        struct dm_task *dmt;
        struct dm_names *names;
        unsigned next = 0;
 
-       if (!(dmt = dm_task_create (DM_DEVICE_LIST)))
+       if (!(dmt = libmp_dm_task_create (DM_DEVICE_LIST)))
                return 0;
 
        dm_task_no_open_count(dmt);
@@ -886,7 +896,7 @@ dm_flush_maps (void)
                goto out;
 
        do {
-               r |= dm_suspend_and_flush_map(names->name);
+               r |= dm_suspend_and_flush_map(names->name, retries);
                next = names->next;
                names = (void *) names + next;
        } while (next);
@@ -902,7 +912,7 @@ dm_message(const char * mapname, char * message)
        int r = 1;
        struct dm_task *dmt;
 
-       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))
@@ -929,7 +939,7 @@ out:
 }
 
 int
-dm_fail_path(char * mapname, char * path)
+dm_fail_path(const char * mapname, char * path)
 {
        char message[32];
 
@@ -940,7 +950,7 @@ dm_fail_path(char * mapname, char * path)
 }
 
 int
-dm_reinstate_path(char * mapname, char * path)
+dm_reinstate_path(const char * mapname, char * path)
 {
        char message[32];
 
@@ -951,7 +961,7 @@ dm_reinstate_path(char * mapname, char * path)
 }
 
 int
-dm_queue_if_no_path(char *mapname, int enable)
+dm_queue_if_no_path(const char *mapname, int enable)
 {
        char *message;
 
@@ -964,7 +974,7 @@ dm_queue_if_no_path(char *mapname, int enable)
 }
 
 static int
-dm_groupmsg (char * msg, char * mapname, int index)
+dm_groupmsg (const char * msg, const char * mapname, int index)
 {
        char message[32];
 
@@ -975,23 +985,48 @@ dm_groupmsg (char * msg, char * mapname, int index)
 }
 
 int
-dm_switchgroup(char * mapname, int index)
+dm_switchgroup(const char * mapname, int 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);
 }
 
 int
-dm_disablegroup(char * mapname, int index)
+dm_disablegroup(const char * mapname, int 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)
 {
@@ -1004,7 +1039,7 @@ dm_get_maps (vector 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);
@@ -1024,24 +1059,12 @@ dm_get_maps (vector mp)
                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 (dm_get_map(names->name, &mpp->size, NULL))
-                       goto out1;
-
-               dm_get_uuid(names->name, mpp->wwid);
-               dm_get_info(names->name, &mpp->dmi);
-
                if (!vector_alloc_slot(mp))
-                       goto out1;
+                       goto out;
 
                vector_set_slot(mp, mpp);
                mpp = NULL;
@@ -1052,41 +1075,31 @@ 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;
-       int event = -1;
 
-       if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
+       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))
-               goto out;
+       return info.event_nr;
+}
 
-       if (info.exists)
-               event = info.event_nr;
+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 event;
+       return info.suspended;
 }
 
 char *
@@ -1098,7 +1111,7 @@ dm_mapname(int major, int minor)
        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) ||
@@ -1127,7 +1140,7 @@ dm_mapname(int major, int minor)
 
        map = dm_task_get_name(dmt);
        if (map && strlen(map))
-               response = STRDUP((char *)dm_task_get_name(dmt));
+               response = STRDUP((const char *)dm_task_get_name(dmt));
 
        dm_task_destroy(dmt);
        return response;
@@ -1149,8 +1162,9 @@ do_foreach_partmaps (const 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);
@@ -1172,15 +1186,15 @@ do_foreach_partmaps (const char * mapname,
        do {
                if (
                    /*
-                    * if devmap target is "linear"
+                    * if there is only a single "linear" target
                     */
-                   (dm_type(names->name, TGT_PART) > 0) &&
+                   (dm_type(names->name, TGT_PART) == 1) &&
 
                    /*
-                    * and both uuid end with same suffix starting
-                    * at UUID_PREFIX
+                    * and the uuid of the target is a partition of the
+                    * uuid of the multipath device
                     */
-                   (!dm_compare_uuid(names->name, mapname)) &&
+                   is_mpath_part(names->name, mapname) &&
 
                    /*
                     * and we can fetch the map table from the kernel
@@ -1190,7 +1204,8 @@ do_foreach_partmaps (const char * mapname,
                    /*
                     * and the table maps over the multipath map
                     */
-                   strstr(params, dev_t)
+                   (p = strstr(params, dev_t)) &&
+                   !isdigit(*(p + strlen(dev_t)))
                   ) {
                        if (partmap_func(names->name, data) != 0)
                                goto out;
@@ -1250,28 +1265,14 @@ cancel_remove_partmap (const char *name, void *unused)
 }
 
 static int
-dm_get_deferred_remove (char * mapname)
+dm_get_deferred_remove (const char * mapname)
 {
-       int r = -1;
-       struct dm_task *dmt;
        struct dm_info info;
 
-       if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
+       if (do_get_info(mapname, &info) != 0)
                return -1;
 
-       if (!dm_task_set_name(dmt, mapname))
-               goto out;
-
-       if (!dm_task_run(dmt))
-               goto out;
-
-       if (!dm_task_get_info(dmt, &info))
-               goto out;
-
-       r = info.deferred_remove;
-out:
-       dm_task_destroy(dmt);
-       return r;
+       return info.deferred_remove;
 }
 
 static int
@@ -1316,11 +1317,8 @@ alloc_dminfo (void)
 }
 
 int
-dm_get_info (char * mapname, struct dm_info ** dmi)
+dm_get_info (const char * mapname, struct dm_info ** dmi)
 {
-       int r = 1;
-       struct dm_task *dmt = NULL;
-
        if (!mapname)
                return 1;
 
@@ -1330,32 +1328,13 @@ dm_get_info (char * mapname, struct dm_info ** dmi)
        if (!*dmi)
                return 1;
 
-       if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
-               goto out;
-
-       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, *dmi))
-               goto out;
-
-       r = 0;
-out:
-       if (r) {
+       if (do_get_info(mapname, *dmi) != 0) {
                memset(*dmi, 0, sizeof(struct dm_info));
                FREE(*dmi);
                *dmi = NULL;
+               return 1;
        }
-
-       if (dmt)
-               dm_task_destroy(dmt);
-
-       return r;
+       return 0;
 }
 
 struct rename_data {
@@ -1376,7 +1355,7 @@ rename_partmap (const char *name, void *data)
        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);
+       dm_rename(name, buff, rd->delim, SKIP_KPARTX_OFF);
        condlog(4, "partition map %s renamed", name);
        return 0;
 }
@@ -1391,24 +1370,27 @@ dm_rename_partmaps (const char * old, char * new, char *delim)
 
        if (delim)
                rd.delim = delim;
-       if (isdigit(new[strlen(new)-1]))
-               rd.delim = "p";
-       else
-               rd.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)
+dm_rename (const char * old, char * new, char *delim, int skip_kpartx)
 {
        int r = 0;
        struct dm_task *dmt;
-       uint32_t cookie;
+       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 = dm_task_create(DM_DEVICE_RENAME)))
+       if (!(dmt = libmp_dm_task_create(DM_DEVICE_RENAME)))
                return r;
 
        if (!dm_task_set_name(dmt, old))
@@ -1419,8 +1401,7 @@ dm_rename (const char * old, char * new, char *delim)
 
        dm_task_no_open_count(dmt);
 
-       if (!dm_task_set_cookie(dmt, &cookie,
-                               DM_UDEV_DISABLE_LIBRARY_FALLBACK))
+       if (!dm_task_set_cookie(dmt, &cookie, udev_flags))
                goto out;
        r = dm_task_run(dmt);
 
@@ -1432,10 +1413,10 @@ out:
        return r;
 }
 
-void dm_reassign_deps(char *table, char *dep, char *newdep)
+void dm_reassign_deps(char *table, const char *dep, const char *newdep)
 {
-       char *p, *n;
-       char *newtable;
+       char *n;
+       const char *p, *newtable;
 
        newtable = strdup(table);
        if (!newtable)
@@ -1446,7 +1427,7 @@ void dm_reassign_deps(char *table, char *dep, char *newdep)
        n += strlen(newdep);
        p += strlen(dep);
        strcat(n, p);
-       free(newtable);
+       FREE_CONST(newtable);
 }
 
 int dm_reassign_table(const char *name, char *old, char *new)
@@ -1458,7 +1439,7 @@ int dm_reassign_table(const char *name, char *old, char *new)
        char *buff;
        void *next = NULL;
 
-       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))
@@ -1468,7 +1449,7 @@ int dm_reassign_table(const char *name, char *old, char *new)
 
        if (!dm_task_run(dmt))
                goto out;
-       if (!(reload_dmt = dm_task_create(DM_DEVICE_RELOAD)))
+       if (!(reload_dmt = libmp_dm_task_create(DM_DEVICE_RELOAD)))
                goto out;
        if (!dm_task_set_name(reload_dmt, name))
                goto out_reload;
@@ -1532,7 +1513,7 @@ int dm_reassign(const char *mapname)
                return 1;
        }
 
-       if (!(dmt = dm_task_create(DM_DEVICE_DEPS))) {
+       if (!(dmt = libmp_dm_task_create(DM_DEVICE_DEPS))) {
                condlog(3, "%s: couldn't make dm task", mapname);
                return 0;
        }
@@ -1561,10 +1542,9 @@ int dm_reassign(const char *mapname)
                sysfs_check_holders(dm_dep, dev_t);
        }
 
-       dm_task_destroy (dmt);
-
        r = 1;
 out:
+       dm_task_destroy (dmt);
        return r;
 }
 
@@ -1591,7 +1571,7 @@ int dm_setgeometry(struct multipath *mpp)
                return 1;
        }
 
-       if (!(dmt = dm_task_create(DM_DEVICE_SET_GEOMETRY)))
+       if (!(dmt = libmp_dm_task_create(DM_DEVICE_SET_GEOMETRY)))
                return 0;
 
        if (!dm_task_set_name(dmt, mpp->alias))