Imported Upstream version 0.7.5
[platform/upstream/multipath-tools.git] / libmultipath / devmapper.c
index 36c1a20..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 "debug.h"
 #include "memory.h"
 #include "devmapper.h"
-#include "config.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
 static int dm_cancel_remove_partmaps(const char * mapname);
@@ -65,7 +66,7 @@ dm_write_log (int level, const char *file, int line, const char *f, ...)
        if (level > 6)
                level = 6;
 
-       thres = (conf) ? conf->verbosity : 0;
+       thres = dm_conf_verbosity;
        if (thres <= 3 || level > thres)
                return;
 
@@ -93,10 +94,10 @@ dm_write_log (int level, const char *file, int line, const char *f, ...)
        return;
 }
 
-extern void
-dm_init(void) {
+void dm_init(int v)
+{
        dm_log_init(&dm_write_log);
-       dm_log_init_verbose(conf ? conf->verbosity + 3 : 0);
+       dm_log_init_verbose(v + 3);
 }
 
 static int
@@ -116,7 +117,10 @@ dm_lib_prereq (void)
 
        dm_get_library_version(version, sizeof(version));
        condlog(3, "libdevmapper version %s", version);
-       sscanf(version, "%d.%d.%d ", &v[0], &v[1], &v[2]);
+       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;
@@ -163,9 +167,9 @@ dm_drv_version (unsigned int * version, char * str)
                goto out;
        }
        v = target->version;
-        version[0] = v[0];
-        version[1] = v[1];
-        version[2] = v[2];
+       version[0] = v[0];
+       version[1] = v[1];
+       version[2] = v[2];
        r = 0;
 out:
        dm_task_destroy(dmt);
@@ -173,11 +177,11 @@ 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};
-        unsigned int * v = version;
+       unsigned int * v = version;
 
        if (dm_drv_version(v, TGT_MPATH)) {
                /* in doubt return not capable */
@@ -188,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)
@@ -209,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))
@@ -230,31 +266,28 @@ dm_simplecmd (int task, const char *name, int no_flush, int need_sync, uint16_t
        if (do_deferred(deferred_remove))
                dm_task_deferred_remove(dmt);
 #endif
-       if (udev_wait_flag && !dm_task_set_cookie(dmt, &cookie, ((conf->daemon)? DM_UDEV_DISABLE_LIBRARY_FALLBACK : 0) | udev_flags)) {
-               dm_udev_complete(cookie);
+       if (udev_wait_flag &&
+           !dm_task_set_cookie(dmt, &cookie,
+                               DM_UDEV_DISABLE_LIBRARY_FALLBACK | udev_flags))
                goto out;
-       }
+
        r = dm_task_run (dmt);
 
-       if (udev_wait_flag) {
-               if (!r)
-                       dm_udev_complete(cookie);
-               else
+       if (udev_wait_flag)
                        dm_udev_wait(cookie);
-       }
-       out:
+out:
        dm_task_destroy (dmt);
        return r;
 }
 
-extern int
-dm_simplecmd_flush (int task, const char *name, int needsync, uint16_t udev_flags) {
-       return dm_simplecmd(task, name, 0, needsync, udev_flags, 0);
+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, int needsync, uint16_t udev_flags) {
-       return dm_simplecmd(task, name, 1, needsync, 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
@@ -263,15 +296,18 @@ dm_device_remove (const char *name, int needsync, int deferred_remove) {
                            deferred_remove);
 }
 
-extern int
-dm_addmap (int task, const char *target, struct multipath *mpp, char * params,
-          int use_uuid, int ro) {
+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, mpp->alias))
@@ -283,16 +319,23 @@ dm_addmap (int task, const char *target, struct multipath *mpp, char * params,
        if (ro)
                dm_task_set_ro(dmt);
 
-       if (use_uuid && 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;
+       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;
                }
-               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) &&
@@ -304,43 +347,51 @@ dm_addmap (int task, const char *target, struct multipath *mpp, char * params,
        if (mpp->attribute_flags & (1 << ATTR_GID) &&
            !dm_task_set_gid(dmt, mpp->gid))
                goto freeout;
-       condlog(4, "%s: addmap [0 %llu %s %s]", mpp->alias, mpp->size,
+       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, (conf->daemon)? DM_UDEV_DISABLE_LIBRARY_FALLBACK : 0)) {
-               dm_udev_complete(cookie);
+           !dm_task_set_cookie(dmt, &cookie, udev_flags))
                goto freeout;
-       }
+
        r = dm_task_run (dmt);
 
-       if (task == DM_DEVICE_CREATE) {
-               if (!r)
-                       dm_udev_complete(cookie);
-               else
+       if (task == DM_DEVICE_CREATE)
                        dm_udev_wait(cookie);
-       }
-       freeout:
+freeout:
        if (prefixed_uuid)
                FREE(prefixed_uuid);
 
-       addout:
+addout:
        dm_task_destroy (dmt);
 
        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, 1, 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.
@@ -363,26 +414,51 @@ 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) {
-       if (dm_addmap(DM_DEVICE_RELOAD, TGT_MPATH, mpp, params, 0, ADDMAP_RW))
-               return 1;
-       if (errno != EROFS)
-               return 0;
-       return dm_addmap(DM_DEVICE_RELOAD, TGT_MPATH, mpp, params, 0, ADDMAP_RO);
+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;
 }
 
-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);
@@ -390,27 +466,34 @@ 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;
-       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))
@@ -422,8 +505,8 @@ dm_get_map(const 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;
@@ -446,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;
 
@@ -468,58 +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;
-       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))
@@ -531,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;
@@ -548,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(const 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))
@@ -572,10 +647,11 @@ dm_type(const 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;
@@ -585,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;
@@ -596,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))
@@ -630,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
@@ -662,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))
@@ -684,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
@@ -759,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;
 
@@ -780,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);
-
-       if (r) {
-               if (do_deferred(deferred_remove) && dm_map_present(mapname)) {
-                       condlog(4, "multipath map %s remove deferred",
+       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);
-                       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;
 }
 
@@ -799,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
@@ -807,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, 1, 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, 1, 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);
@@ -869,12 +896,12 @@ 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);
 
-       out:
+out:
        dm_task_destroy (dmt);
        return r;
 }
@@ -885,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))
@@ -912,7 +939,7 @@ out:
 }
 
 int
-dm_fail_path(char * mapname, char * path)
+dm_fail_path(const char * mapname, char * path)
 {
        char message[32];
 
@@ -923,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];
 
@@ -934,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;
 
@@ -947,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];
 
@@ -958,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)
 {
@@ -987,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);
@@ -1007,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;
@@ -1035,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 *
@@ -1081,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) ||
@@ -1110,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;
@@ -1132,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);
@@ -1155,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
@@ -1173,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;
@@ -1233,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
@@ -1299,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;
 
@@ -1313,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 {
@@ -1359,39 +1355,42 @@ 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);
+       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)
+dm_rename_partmaps (const char * old, char * new, char *delim)
 {
        struct rename_data rd;
 
        rd.old = old;
        rd.new = new;
 
-       if (conf->partition_delim)
-               rd.delim = conf->partition_delim;
-       if (isdigit(new[strlen(new)-1]))
-               rd.delim = "p";
-       else
-               rd.delim = "";
+       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)
+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))
+       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))
@@ -1402,14 +1401,11 @@ dm_rename (const char * old, char * new)
 
        dm_task_no_open_count(dmt);
 
-       if (!dm_task_set_cookie(dmt, &cookie, (conf->daemon)? DM_UDEV_DISABLE_LIBRARY_FALLBACK : 0))
+       if (!dm_task_set_cookie(dmt, &cookie, udev_flags))
                goto out;
        r = dm_task_run(dmt);
 
-       if (!r)
-               dm_udev_complete(cookie);
-       else
-               dm_udev_wait(cookie);
+       dm_udev_wait(cookie);
 
 out:
        dm_task_destroy(dmt);
@@ -1417,30 +1413,33 @@ 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[PARAMS_SIZE];
+       char *n;
+       const char *p, *newtable;
 
-       strcpy(newtable, table);
+       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, modified = 0;
+       int r = 0, modified = 0;
        uint64_t start, length;
        struct dm_task *dmt, *reload_dmt;
        char *target, *params = NULL;
-       char buff[PARAMS_SIZE];
+       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))
@@ -1450,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;
@@ -1458,8 +1457,12 @@ int dm_reassign_table(const char *name, char *old, char *new)
        do {
                next = dm_get_next_target(dmt, next, &start, &length,
                                          &target, &params);
-               memset(buff, 0, PARAMS_SIZE);
-               strcpy(buff, 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);
@@ -1469,6 +1472,7 @@ int dm_reassign_table(const char *name, char *old, char *new)
                        modified++;
                }
                dm_task_add_target(reload_dmt, start, length, target, buff);
+               free(buff);
        } while (next);
 
        if (modified) {
@@ -1478,7 +1482,8 @@ int dm_reassign_table(const char *name, char *old, char *new)
                        condlog(3, "%s: failed to reassign targets", name);
                        goto out_reload;
                }
-               dm_simplecmd_noflush(DM_DEVICE_RESUME, name, 1, MPATH_UDEV_RELOAD_FLAG);
+               dm_simplecmd_noflush(DM_DEVICE_RESUME, name,
+                                    MPATH_UDEV_RELOAD_FLAG);
        }
        r = 1;
 
@@ -1508,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;
        }
@@ -1537,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;
 }
 
@@ -1567,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))