Imported Upstream version 0.7.0
[platform/upstream/multipath-tools.git] / multipathd / main.c
index bbcfe0d..0c61caa 100644 (file)
@@ -7,7 +7,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 #include <libdevmapper.h>
-#include <wait.h>
+#include <sys/wait.h>
 #include <sys/mman.h>
 #include <sys/types.h>
 #include <fcntl.h>
 #include <limits.h>
 #include <linux/oom.h>
 #include <libudev.h>
+#include <urcu.h>
 #ifdef USE_SYSTEMD
 #include <systemd/sd-daemon.h>
 #endif
 #include <semaphore.h>
-#include <mpath_cmd.h>
-#include <mpath_persist.h>
 #include <time.h>
+#include <stdbool.h>
+
+/*
+ * libmultipath
+ */
+#include "time-util.h"
 
 /*
  * libcheckers
  */
-#include <checkers.h>
+#include "checkers.h"
 
 #ifdef USE_SYSTEMD
 static int use_watchdog;
 #endif
 
+int uxsock_timeout;
+
 /*
  * libmultipath
  */
-#include <parser.h>
-#include <vector.h>
-#include <memory.h>
-#include <config.h>
-#include <util.h>
-#include <hwtable.h>
-#include <defaults.h>
-#include <structs.h>
-#include <blacklist.h>
-#include <structs_vec.h>
-#include <dmparser.h>
-#include <devmapper.h>
-#include <sysfs.h>
-#include <dict.h>
-#include <discovery.h>
-#include <debug.h>
-#include <propsel.h>
-#include <uevent.h>
-#include <switchgroup.h>
-#include <print.h>
-#include <configure.h>
-#include <prio.h>
-#include <wwids.h>
-#include <pgpolicies.h>
-#include <uevent.h>
-#include <log.h>
+#include "parser.h"
+#include "vector.h"
+#include "memory.h"
+#include "config.h"
+#include "util.h"
+#include "hwtable.h"
+#include "defaults.h"
+#include "structs.h"
+#include "blacklist.h"
+#include "structs_vec.h"
+#include "dmparser.h"
+#include "devmapper.h"
+#include "sysfs.h"
+#include "dict.h"
+#include "discovery.h"
+#include "debug.h"
+#include "propsel.h"
+#include "uevent.h"
+#include "switchgroup.h"
+#include "print.h"
+#include "configure.h"
+#include "prio.h"
+#include "wwids.h"
+#include "pgpolicies.h"
+#include "uevent.h"
+#include "log.h"
+
+#include "mpath_cmd.h"
+#include "mpath_persist.h"
+
 #include "prioritizers/alua_rtpg.h"
 
 #include "main.h"
@@ -74,6 +85,7 @@ static int use_watchdog;
 #include "lock.h"
 #include "waiter.h"
 #include "wwids.h"
+#include "../third-party/valgrind/drd.h"
 
 #define FILE_NAME_SIZE 256
 #define CMDSIZE 160
@@ -95,10 +107,14 @@ struct mpath_event_param
 unsigned int mpath_mx_alloc_len;
 
 int logsink;
-enum daemon_status running_state;
+int verbosity;
+int bindings_read_only;
+int ignore_new_devs;
+enum daemon_status running_state = DAEMON_INIT;
 pid_t daemon_pid;
+pthread_mutex_t config_lock = PTHREAD_MUTEX_INITIALIZER;
+pthread_cond_t config_cond;
 
-static sem_t exit_sem;
 /*
  * global copy of vecs for use in sig handlers
  */
@@ -106,12 +122,119 @@ struct vectors * gvecs;
 
 struct udev * udev;
 
+struct config *multipath_conf;
+
+/* Local variables */
+static volatile sig_atomic_t exit_sig;
+static volatile sig_atomic_t reconfig_sig;
+static volatile sig_atomic_t log_reset_sig;
+
+const char *
+daemon_status(void)
+{
+       switch (running_state) {
+       case DAEMON_INIT:
+               return "init";
+       case DAEMON_START:
+               return "startup";
+       case DAEMON_CONFIGURE:
+               return "configure";
+       case DAEMON_IDLE:
+               return "idle";
+       case DAEMON_RUNNING:
+               return "running";
+       case DAEMON_SHUTDOWN:
+               return "shutdown";
+       }
+       return NULL;
+}
+
+/*
+ * I love you too, systemd ...
+ */
+const char *
+sd_notify_status(void)
+{
+       switch (running_state) {
+       case DAEMON_INIT:
+               return "STATUS=init";
+       case DAEMON_START:
+               return "STATUS=startup";
+       case DAEMON_CONFIGURE:
+               return "STATUS=configure";
+       case DAEMON_IDLE:
+               return "STATUS=idle";
+       case DAEMON_RUNNING:
+               return "STATUS=running";
+       case DAEMON_SHUTDOWN:
+               return "STATUS=shutdown";
+       }
+       return NULL;
+}
+
+static void config_cleanup(void *arg)
+{
+       pthread_mutex_unlock(&config_lock);
+}
+
+void post_config_state(enum daemon_status state)
+{
+       pthread_mutex_lock(&config_lock);
+       if (state != running_state) {
+               running_state = state;
+               pthread_cond_broadcast(&config_cond);
+#ifdef USE_SYSTEMD
+               sd_notify(0, sd_notify_status());
+#endif
+       }
+       pthread_mutex_unlock(&config_lock);
+}
+
+int set_config_state(enum daemon_status state)
+{
+       int rc = 0;
+
+       pthread_cleanup_push(config_cleanup, NULL);
+       pthread_mutex_lock(&config_lock);
+       if (running_state != state) {
+               if (running_state != DAEMON_IDLE) {
+                       struct timespec ts;
+
+                       clock_gettime(CLOCK_MONOTONIC, &ts);
+                       ts.tv_sec += 1;
+                       rc = pthread_cond_timedwait(&config_cond,
+                                                   &config_lock, &ts);
+               }
+               if (!rc) {
+                       running_state = state;
+                       pthread_cond_broadcast(&config_cond);
+#ifdef USE_SYSTEMD
+                       sd_notify(0, sd_notify_status());
+#endif
+               }
+       }
+       pthread_cleanup_pop(1);
+       return rc;
+}
+
+struct config *get_multipath_config(void)
+{
+       rcu_read_lock();
+       return rcu_dereference(multipath_conf);
+}
+
+void put_multipath_config(struct config *conf)
+{
+       rcu_read_unlock();
+}
+
 static int
 need_switch_pathgroup (struct multipath * mpp, int refresh)
 {
        struct pathgroup * pgp;
        struct path * pp;
        unsigned int i, j;
+       struct config *conf;
 
        if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
                return 0;
@@ -119,10 +242,18 @@ need_switch_pathgroup (struct multipath * mpp, int refresh)
        /*
         * Refresh path priority values
         */
-       if (refresh)
-               vector_foreach_slot (mpp->pg, pgp, i)
-                       vector_foreach_slot (pgp->paths, pp, j)
-                               pathinfo(pp, conf->hwtable, DI_PRIO);
+       if (refresh) {
+               vector_foreach_slot (mpp->pg, pgp, i) {
+                       vector_foreach_slot (pgp->paths, pp, j) {
+                               conf = get_multipath_config();
+                               pathinfo(pp, conf, DI_PRIO);
+                               put_multipath_config(conf);
+                       }
+               }
+       }
+
+       if (!mpp->pg || VECTOR_SIZE(mpp->paths) == 0)
+               return 0;
 
        mpp->bestpg = select_path_group(mpp);
 
@@ -146,8 +277,12 @@ coalesce_maps(struct vectors *vecs, vector nmpv)
 {
        struct multipath * ompp;
        vector ompv = vecs->mpvec;
-       unsigned int i;
+       unsigned int i, reassign_maps;
+       struct config *conf;
 
+       conf = get_multipath_config();
+       reassign_maps = conf->reassign_maps;
+       put_multipath_config(conf);
        vector_foreach_slot (ompv, ompp, i) {
                condlog(3, "%s: coalesce map", ompp->alias);
                if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
@@ -177,7 +312,7 @@ coalesce_maps(struct vectors *vecs, vector nmpv)
                                dm_lib_release();
                                condlog(2, "%s devmap removed", ompp->alias);
                        }
-               } else if (conf->reassign_maps) {
+               } else if (reassign_maps) {
                        condlog(3, "%s: Reassign existing device-mapper"
                                " devices", ompp->alias);
                        dm_reassign(ompp->alias);
@@ -198,7 +333,7 @@ sync_map_state(struct multipath *mpp)
 
        vector_foreach_slot (mpp->pg, pgp, i){
                vector_foreach_slot (pgp->paths, pp, j){
-                       if (pp->state == PATH_UNCHECKED || 
+                       if (pp->state == PATH_UNCHECKED ||
                            pp->state == PATH_WILD ||
                            pp->state == PATH_DELAYED)
                                continue;
@@ -270,7 +405,7 @@ update_map (struct multipath *mpp, struct vectors *vecs)
 
 retry:
        condlog(4, "%s: updating new map", mpp->alias);
-       if (adopt_paths(vecs->pathvec, mpp, 1)) {
+       if (adopt_paths(vecs->pathvec, mpp)) {
                condlog(0, "%s: failed to adopt paths for new map update",
                        mpp->alias);
                retries = -1;
@@ -285,7 +420,7 @@ retry:
                retries = -1;
                goto fail;
        }
-       if (domap(mpp, params) <= 0 && retries-- > 0) {
+       if (domap(mpp, params, 1) <= 0 && retries-- > 0) {
                condlog(0, "%s: map_udate sleep", mpp->alias);
                sleep(1);
                goto retry;
@@ -322,7 +457,11 @@ uev_add_map (struct uevent * uev, struct vectors * vecs)
                        return 1;
                }
        }
+       pthread_cleanup_push(cleanup_lock, &vecs->lock);
+       lock(&vecs->lock);
+       pthread_testcancel();
        rc = ev_add_map(uev->kernel, alias, vecs);
+       lock_cleanup_pop(vecs->lock);
        FREE(alias);
        return rc;
 }
@@ -333,7 +472,8 @@ ev_add_map (char * dev, char * alias, struct vectors * vecs)
        char * refwwid;
        struct multipath * mpp;
        int map_present;
-       int r = 1;
+       int r = 1, delayed_reconfig, reassign_maps;
+       struct config *conf;
 
        map_present = dm_map_present(alias);
 
@@ -346,16 +486,22 @@ ev_add_map (char * dev, char * alias, struct vectors * vecs)
 
        if (mpp) {
                if (mpp->wait_for_udev > 1) {
+                       condlog(2, "%s: performing delayed actions",
+                               mpp->alias);
                        if (update_map(mpp, vecs))
-                       /* setup multipathd removed the map */
+                               /* setup multipathd removed the map */
                                return 1;
                }
+               conf = get_multipath_config();
+               delayed_reconfig = conf->delayed_reconfig;
+               reassign_maps = conf->reassign_maps;
+               put_multipath_config(conf);
                if (mpp->wait_for_udev) {
                        mpp->wait_for_udev = 0;
-                       if (conf->delayed_reconfig &&
+                       if (delayed_reconfig &&
                            !need_to_delay_reconfig(vecs)) {
                                condlog(2, "reconfigure (delayed)");
-                               reconfigure(vecs);
+                               set_config_state(DAEMON_CONFIGURE);
                                return 0;
                        }
                }
@@ -364,7 +510,7 @@ ev_add_map (char * dev, char * alias, struct vectors * vecs)
                 * if we create a multipath mapped device as a result
                 * of uev_add_path
                 */
-               if (conf->reassign_maps) {
+               if (reassign_maps) {
                        condlog(3, "%s: Reassign existing device-mapper devices",
                                alias);
                        dm_reassign(alias);
@@ -386,10 +532,11 @@ ev_add_map (char * dev, char * alias, struct vectors * vecs)
                        return 1;
                }
        }
-       r = get_refwwid(dev, DEV_DEVMAP, vecs->pathvec, &refwwid);
+       r = get_refwwid(CMD_NONE, dev, DEV_DEVMAP, vecs->pathvec, &refwwid);
 
        if (refwwid) {
-               r = coalesce_paths(vecs, NULL, refwwid, 0);
+               r = coalesce_paths(vecs, NULL, refwwid, FORCE_RELOAD_NONE,
+                                  CMD_NONE);
                dm_lib_release();
        }
 
@@ -418,6 +565,10 @@ uev_remove_map (struct uevent * uev, struct vectors * vecs)
                return 0;
        }
        minor = uevent_get_minor(uev);
+
+       pthread_cleanup_push(cleanup_lock, &vecs->lock);
+       lock(&vecs->lock);
+       pthread_testcancel();
        mpp = find_mp_by_minor(vecs->mpvec, minor);
 
        if (!mpp) {
@@ -434,10 +585,12 @@ uev_remove_map (struct uevent * uev, struct vectors * vecs)
        orphan_paths(vecs->pathvec, mpp);
        remove_map_and_stop_waiter(mpp, vecs, 1);
 out:
+       lock_cleanup_pop(vecs->lock);
        FREE(alias);
        return 0;
 }
 
+/* Called from CLI handler */
 int
 ev_remove_map (char * devname, char * alias, int minor, struct vectors * vecs)
 {
@@ -459,10 +612,11 @@ ev_remove_map (char * devname, char * alias, int minor, struct vectors * vecs)
 }
 
 static int
-uev_add_path (struct uevent *uev, struct vectors * vecs)
+uev_add_path (struct uevent *uev, struct vectors * vecs, int need_do_map)
 {
        struct path *pp;
        int ret = 0, i;
+       struct config *conf;
 
        condlog(2, "%s: add path (uevent)", uev->kernel);
        if (strstr(uev->kernel, "..") != NULL) {
@@ -473,6 +627,9 @@ uev_add_path (struct uevent *uev, struct vectors * vecs)
                return 1;
        }
 
+       pthread_cleanup_push(cleanup_lock, &vecs->lock);
+       lock(&vecs->lock);
+       pthread_testcancel();
        pp = find_path_by_dev(vecs->pathvec, uev->kernel);
        if (pp) {
                int r;
@@ -483,10 +640,12 @@ uev_add_path (struct uevent *uev, struct vectors * vecs)
                        condlog(3, "%s: reinitialize path", uev->kernel);
                        udev_device_unref(pp->udev);
                        pp->udev = udev_device_ref(uev->udev);
-                       r = pathinfo(pp, conf->hwtable,
+                       conf = get_multipath_config();
+                       r = pathinfo(pp, conf,
                                     DI_ALL | DI_BLACKLIST);
+                       put_multipath_config(conf);
                        if (r == PATHINFO_OK)
-                               ret = ev_add_path(pp, vecs);
+                               ret = ev_add_path(pp, vecs, need_do_map);
                        else if (r == PATHINFO_SKIPPED) {
                                condlog(3, "%s: remove blacklisted path",
                                        uev->kernel);
@@ -500,24 +659,33 @@ uev_add_path (struct uevent *uev, struct vectors * vecs)
                                ret = 1;
                        }
                }
-               return ret;
        }
+       lock_cleanup_pop(vecs->lock);
+       if (pp)
+               return ret;
 
        /*
         * get path vital state
         */
-       ret = alloc_path_with_pathinfo(conf->hwtable, uev->udev,
-                                      DI_ALL, &pp);
+       conf = get_multipath_config();
+       ret = alloc_path_with_pathinfo(conf, uev->udev,
+                                      uev->wwid, DI_ALL, &pp);
+       put_multipath_config(conf);
        if (!pp) {
                if (ret == PATHINFO_SKIPPED)
                        return 0;
                condlog(3, "%s: failed to get path info", uev->kernel);
                return 1;
        }
+       pthread_cleanup_push(cleanup_lock, &vecs->lock);
+       lock(&vecs->lock);
+       pthread_testcancel();
        ret = store_path(vecs->pathvec, pp);
        if (!ret) {
+               conf = get_multipath_config();
                pp->checkint = conf->checkint;
-               ret = ev_add_path(pp, vecs);
+               put_multipath_config(conf);
+               ret = ev_add_path(pp, vecs, need_do_map);
        } else {
                condlog(0, "%s: failed to store path info, "
                        "dropping event",
@@ -525,7 +693,7 @@ uev_add_path (struct uevent *uev, struct vectors * vecs)
                free_path(pp);
                ret = 1;
        }
-
+       lock_cleanup_pop(vecs->lock);
        return ret;
 }
 
@@ -535,7 +703,7 @@ uev_add_path (struct uevent *uev, struct vectors * vecs)
  * 1: error
  */
 int
-ev_add_path (struct path * pp, struct vectors * vecs)
+ev_add_path (struct path * pp, struct vectors * vecs, int need_do_map)
 {
        struct multipath * mpp;
        char params[PARAMS_SIZE] = {0};
@@ -551,7 +719,11 @@ ev_add_path (struct path * pp, struct vectors * vecs)
                goto fail; /* leave path added to pathvec */
        }
        mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
-       if (mpp && mpp->wait_for_udev) {
+       if (mpp && mpp->wait_for_udev &&
+           (pathcount(mpp, PATH_UP) > 0 ||
+            (pathcount(mpp, PATH_GHOST) > 0 && pp->tpgs != TPGS_IMPLICIT))) {
+               /* if wait_for_udev is set and valid paths exist */
+               condlog(2, "%s: delaying path addition until %s is fully initialized", pp->dev, mpp->alias);
                mpp->wait_for_udev = 2;
                orphan_path(pp, "waiting for create to complete");
                return 0;
@@ -560,7 +732,7 @@ ev_add_path (struct path * pp, struct vectors * vecs)
        pp->mpp = mpp;
 rescan:
        if (mpp) {
-               if (mpp->size != pp->size) {
+               if (pp->size && mpp->size != pp->size) {
                        condlog(0, "%s: failed to add new path %s, "
                                "device size mismatch",
                                mpp->alias, pp->dev);
@@ -573,7 +745,7 @@ rescan:
 
                condlog(4,"%s: adopting all paths for path %s",
                        mpp->alias, pp->dev);
-               if (adopt_paths(vecs->pathvec, mpp, 1))
+               if (adopt_paths(vecs->pathvec, mpp))
                        goto fail; /* leave path added to pathvec */
 
                verify_paths(mpp, vecs);
@@ -593,13 +765,20 @@ rescan:
                         */
                        start_waiter = 1;
                }
-               else
+               if (!start_waiter)
                        goto fail; /* leave path added to pathvec */
        }
 
-       /* persistent reseravtion check*/
-       mpath_pr_event_handle(pp);      
+       /* persistent reservation check*/
+       mpath_pr_event_handle(pp);
+
+       if (!need_do_map)
+               return 0;
 
+       if (!dm_map_present(mpp->alias)) {
+               mpp->action = ACT_CREATE;
+               start_waiter = 1;
+       }
        /*
         * push the map to the device-mapper
         */
@@ -612,7 +791,7 @@ rescan:
         * reload the map for the multipath mapped device
         */
 retry:
-       ret = domap(mpp, params);
+       ret = domap(mpp, params, 1);
        if (ret <= 0) {
                if (ret < 0 && retries-- > 0) {
                        condlog(0, "%s: retry domap for addition of new "
@@ -626,7 +805,7 @@ retry:
                 * deal with asynchronous uevents :((
                 */
                if (mpp->action == ACT_RELOAD && retries-- > 0) {
-                       condlog(0, "%s: uev_add_path sleep", mpp->alias);
+                       condlog(0, "%s: ev_add_path sleep", mpp->alias);
                        sleep(1);
                        update_mpp_paths(mpp, vecs->pathvec);
                        goto rescan;
@@ -655,8 +834,7 @@ retry:
                condlog(2, "%s [%s]: path added to devmap %s",
                        pp->dev, pp->dev_t, mpp->alias);
                return 0;
-       }
-       else
+       } else
                goto fail;
 
 fail_map:
@@ -667,24 +845,29 @@ fail:
 }
 
 static int
-uev_remove_path (struct uevent *uev, struct vectors * vecs)
+uev_remove_path (struct uevent *uev, struct vectors * vecs, int need_do_map)
 {
        struct path *pp;
+       int ret;
 
        condlog(2, "%s: remove path (uevent)", uev->kernel);
+       pthread_cleanup_push(cleanup_lock, &vecs->lock);
+       lock(&vecs->lock);
+       pthread_testcancel();
        pp = find_path_by_dev(vecs->pathvec, uev->kernel);
-
+       if (pp)
+               ret = ev_remove_path(pp, vecs, need_do_map);
+       lock_cleanup_pop(vecs->lock);
        if (!pp) {
                /* Not an error; path might have been purged earlier */
                condlog(0, "%s: path already removed", uev->kernel);
                return 0;
        }
-
-       return ev_remove_path(pp, vecs);
+       return ret;
 }
 
 int
-ev_remove_path (struct path *pp, struct vectors * vecs)
+ev_remove_path (struct path *pp, struct vectors * vecs, int need_do_map)
 {
        struct multipath * mpp;
        int i, retval = 0;
@@ -721,6 +904,7 @@ ev_remove_path (struct path *pp, struct vectors * vecs)
                                mpp->retry_tick = 0;
                                mpp->no_path_retry = NO_PATH_RETRY_FAIL;
                                mpp->flush_on_last_del = FLUSH_IN_PROGRESS;
+                               mpp->stat_map_failures++;
                                dm_queue_if_no_path(mpp->alias, 0);
                        }
                        if (!flush_map(mpp, vecs, 1)) {
@@ -746,11 +930,13 @@ ev_remove_path (struct path *pp, struct vectors * vecs)
                        goto out;
                }
 
+               if (!need_do_map)
+                       goto out;
                /*
                 * reload the map
                 */
                mpp->action = ACT_RELOAD;
-               if (domap(mpp, params) <= 0) {
+               if (domap(mpp, params, 1) <= 0) {
                        condlog(0, "%s: failed in domap for "
                                "removal of path %s",
                                mpp->alias, pp->dev);
@@ -786,36 +972,82 @@ uev_update_path (struct uevent *uev, struct vectors * vecs)
 {
        int ro, retval = 0;
        struct path * pp;
+       struct config *conf;
+       int disable_changed_wwids;
+       int needs_reinit = 0;
 
-       pp = find_path_by_dev(vecs->pathvec, uev->kernel);
-       if (!pp) {
-               condlog(0, "%s: spurious uevent, path not found",
-                       uev->kernel);
-               return 1;
-       }
-
-       if (pp->initialized == INIT_REQUESTED_UDEV)
-               return uev_add_path(uev, vecs);
+       conf = get_multipath_config();
+       disable_changed_wwids = conf->disable_changed_wwids;
+       put_multipath_config(conf);
 
        ro = uevent_get_disk_ro(uev);
 
-       if (ro >= 0) {
-               condlog(2, "%s: update path write_protect to '%d' (uevent)",
-                       uev->kernel, ro);
-               if (pp->mpp) {
-                       if (pp->mpp->wait_for_udev) {
-                               pp->mpp->wait_for_udev = 2;
-                               return 0;
+       pthread_cleanup_push(cleanup_lock, &vecs->lock);
+       lock(&vecs->lock);
+       pthread_testcancel();
+
+       pp = find_path_by_dev(vecs->pathvec, uev->kernel);
+       if (pp) {
+               struct multipath *mpp = pp->mpp;
+
+               if (disable_changed_wwids &&
+                   (strlen(pp->wwid) || pp->wwid_changed)) {
+                       char wwid[WWID_SIZE];
+
+                       strcpy(wwid, pp->wwid);
+                       get_uid(pp, pp->state, uev->udev);
+                       if (strcmp(wwid, pp->wwid) != 0) {
+                               condlog(0, "%s: path wwid changed from '%s' to '%s'. disallowing", uev->kernel, wwid, pp->wwid);
+                               strcpy(pp->wwid, wwid);
+                               if (!pp->wwid_changed) {
+                                       pp->wwid_changed = 1;
+                                       pp->tick = 1;
+                                       if (pp->mpp)
+                                               dm_fail_path(pp->mpp->alias, pp->dev_t);
+                               }
+                               goto out;
+                       } else
+                               pp->wwid_changed = 0;
+               }
+
+               if (pp->initialized == INIT_REQUESTED_UDEV)
+                       needs_reinit = 1;
+               else if (mpp && ro >= 0) {
+                       condlog(2, "%s: update path write_protect to '%d' (uevent)", uev->kernel, ro);
+
+                       if (mpp->wait_for_udev)
+                               mpp->wait_for_udev = 2;
+                       else {
+                               if (ro == 1)
+                                       pp->mpp->force_readonly = 1;
+                               retval = reload_map(vecs, mpp, 0, 1);
+                               pp->mpp->force_readonly = 0;
+                               condlog(2, "%s: map %s reloaded (retval %d)",
+                                       uev->kernel, mpp->alias, retval);
                        }
+               }
+       }
+out:
+       lock_cleanup_pop(vecs->lock);
+       if (!pp) {
+               /* If the path is blacklisted, print a debug/non-default verbosity message. */
+               if (uev->udev) {
+                       int flag = DI_SYSFS | DI_WWID;
 
-                       retval = reload_map(vecs, pp->mpp, 0);
+                       conf = get_multipath_config();
+                       retval = alloc_path_with_pathinfo(conf, uev->udev, uev->wwid, flag, NULL);
+                       put_multipath_config(conf);
 
-                       condlog(2, "%s: map %s reloaded (retval %d)",
-                               uev->kernel, pp->mpp->alias, retval);
+                       if (retval == PATHINFO_SKIPPED) {
+                               condlog(3, "%s: spurious uevent, path is blacklisted", uev->kernel);
+                               return 0;
+                       }
                }
 
+               condlog(0, "%s: spurious uevent, path not found", uev->kernel);
        }
-
+       if (needs_reinit)
+               retval = uev_add_path(uev, vecs, 1);
        return retval;
 }
 
@@ -830,13 +1062,14 @@ map_discovery (struct vectors * vecs)
 
        vector_foreach_slot (vecs->mpvec, mpp, i)
                if (setup_multipath(vecs, mpp))
-                       return 1;
+                       i--;
 
        return 0;
 }
 
 int
-uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
+uxsock_trigger (char * str, char ** reply, int * len, bool is_root,
+               void * trigger_data)
 {
        struct vectors * vecs;
        int r;
@@ -845,64 +1078,55 @@ uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
        *len = 0;
        vecs = (struct vectors *)trigger_data;
 
-       pthread_cleanup_push(cleanup_lock, &vecs->lock);
-       lock(vecs->lock);
-       pthread_testcancel();
+       if ((str != NULL) && (is_root == false) &&
+           (strncmp(str, "list", strlen("list")) != 0) &&
+           (strncmp(str, "show", strlen("show")) != 0)) {
+               *reply = STRDUP("permission deny: need to be root");
+               if (*reply)
+                       *len = strlen(*reply) + 1;
+               return 1;
+       }
 
-       r = parse_cmd(str, reply, len, vecs);
+       r = parse_cmd(str, reply, len, vecs, uxsock_timeout);
 
        if (r > 0) {
-               *reply = STRDUP("fail\n");
-               *len = strlen(*reply) + 1;
+               if (r == ETIMEDOUT)
+                       *reply = STRDUP("timeout\n");
+               else
+                       *reply = STRDUP("fail\n");
+               if (*reply)
+                       *len = strlen(*reply) + 1;
                r = 1;
        }
        else if (!r && *len == 0) {
                *reply = STRDUP("ok\n");
-               *len = strlen(*reply) + 1;
+               if (*reply)
+                       *len = strlen(*reply) + 1;
                r = 0;
        }
        /* else if (r < 0) leave *reply alone */
 
-       lock_cleanup_pop(vecs->lock);
        return r;
 }
 
-static int
-uev_discard(char * devpath)
-{
-       char *tmp;
-       char a[11], b[11];
-
-       /*
-        * keep only block devices, discard partitions
-        */
-       tmp = strstr(devpath, "/block/");
-       if (tmp == NULL){
-               condlog(4, "no /block/ in '%s'", devpath);
-               return 1;
-       }
-       if (sscanf(tmp, "/block/%10s", a) != 1 ||
-           sscanf(tmp, "/block/%10[^/]/%10s", a, b) == 2) {
-               condlog(4, "discard event on %s", devpath);
-               return 1;
-       }
-       return 0;
-}
-
 int
 uev_trigger (struct uevent * uev, void * trigger_data)
 {
        int r = 0;
        struct vectors * vecs;
+       struct uevent *merge_uev, *tmp;
 
        vecs = (struct vectors *)trigger_data;
 
-       if (uev_discard(uev->devpath))
-               return 0;
+       pthread_cleanup_push(config_cleanup, NULL);
+       pthread_mutex_lock(&config_lock);
+       if (running_state != DAEMON_IDLE &&
+           running_state != DAEMON_RUNNING)
+               pthread_cond_wait(&config_cond, &config_lock);
+       pthread_cleanup_pop(1);
 
-       pthread_cleanup_push(cleanup_lock, &vecs->lock);
-       lock(vecs->lock);
-       pthread_testcancel();
+       if (running_state == DAEMON_SHUTDOWN)
+               return 0;
 
        /*
         * device map event
@@ -922,79 +1146,93 @@ uev_trigger (struct uevent * uev, void * trigger_data)
        }
 
        /*
-        * path add/remove event
+        * path add/remove/change event, add/remove maybe merged
         */
-       if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
-                          uev->kernel) > 0)
-               goto out;
-
-       if (!strncmp(uev->action, "add", 3)) {
-               r = uev_add_path(uev, vecs);
-               goto out;
-       }
-       if (!strncmp(uev->action, "remove", 6)) {
-               r = uev_remove_path(uev, vecs);
-               goto out;
-       }
-       if (!strncmp(uev->action, "change", 6)) {
-               r = uev_update_path(uev, vecs);
-               goto out;
+       list_for_each_entry_safe(merge_uev, tmp, &uev->merge_node, node) {
+               if (!strncmp(merge_uev->action, "add", 3))
+                       r += uev_add_path(merge_uev, vecs, 0);
+               if (!strncmp(merge_uev->action, "remove", 6))
+                       r += uev_remove_path(merge_uev, vecs, 0);
        }
 
+       if (!strncmp(uev->action, "add", 3))
+               r += uev_add_path(uev, vecs, 1);
+       if (!strncmp(uev->action, "remove", 6))
+               r += uev_remove_path(uev, vecs, 1);
+       if (!strncmp(uev->action, "change", 6))
+               r += uev_update_path(uev, vecs);
+
 out:
-       lock_cleanup_pop(vecs->lock);
        return r;
 }
 
+static void rcu_unregister(void *param)
+{
+       rcu_unregister_thread();
+}
+
 static void *
 ueventloop (void * ap)
 {
        struct udev *udev = ap;
 
+       pthread_cleanup_push(rcu_unregister, NULL);
+       rcu_register_thread();
        if (uevent_listen(udev))
                condlog(0, "error starting uevent listener");
-
+       pthread_cleanup_pop(1);
        return NULL;
 }
 
 static void *
 uevqloop (void * ap)
 {
+       pthread_cleanup_push(rcu_unregister, NULL);
+       rcu_register_thread();
        if (uevent_dispatch(&uev_trigger, ap))
                condlog(0, "error starting uevent dispatcher");
-
+       pthread_cleanup_pop(1);
        return NULL;
 }
 static void *
 uxlsnrloop (void * ap)
 {
-       if (cli_init())
+       if (cli_init()) {
+               condlog(1, "Failed to init uxsock listener");
                return NULL;
-
+       }
+       pthread_cleanup_push(rcu_unregister, NULL);
+       rcu_register_thread();
        set_handler_callback(LIST+PATHS, cli_list_paths);
        set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt);
        set_handler_callback(LIST+PATHS+RAW+FMT, cli_list_paths_raw);
        set_handler_callback(LIST+PATH, cli_list_path);
        set_handler_callback(LIST+MAPS, cli_list_maps);
-       set_handler_callback(LIST+STATUS, cli_list_status);
-       set_handler_callback(LIST+DAEMON, cli_list_daemon);
+       set_unlocked_handler_callback(LIST+STATUS, cli_list_status);
+       set_unlocked_handler_callback(LIST+DAEMON, cli_list_daemon);
        set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
        set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
        set_handler_callback(LIST+MAPS+FMT, cli_list_maps_fmt);
        set_handler_callback(LIST+MAPS+RAW+FMT, cli_list_maps_raw);
        set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
        set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
+       set_handler_callback(LIST+MAPS+JSON, cli_list_maps_json);
        set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
+       set_handler_callback(LIST+MAP+FMT, cli_list_map_fmt);
+       set_handler_callback(LIST+MAP+RAW+FMT, cli_list_map_fmt);
+       set_handler_callback(LIST+MAP+JSON, cli_list_map_json);
        set_handler_callback(LIST+CONFIG, cli_list_config);
        set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
        set_handler_callback(LIST+DEVICES, cli_list_devices);
        set_handler_callback(LIST+WILDCARDS, cli_list_wildcards);
+       set_handler_callback(RESET+MAPS+STATS, cli_reset_maps_stats);
+       set_handler_callback(RESET+MAP+STATS, cli_reset_map_stats);
        set_handler_callback(ADD+PATH, cli_add_path);
        set_handler_callback(DEL+PATH, cli_del_path);
        set_handler_callback(ADD+MAP, cli_add_map);
        set_handler_callback(DEL+MAP, cli_del_map);
        set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
-       set_handler_callback(RECONFIGURE, cli_reconfigure);
+       set_unlocked_handler_callback(RECONFIGURE, cli_reconfigure);
        set_handler_callback(SUSPEND+MAP, cli_suspend);
        set_handler_callback(RESUME+MAP, cli_resume);
        set_handler_callback(RESIZE+MAP, cli_resize);
@@ -1006,8 +1244,8 @@ uxlsnrloop (void * ap)
        set_handler_callback(RESTOREQ+MAP, cli_restore_queueing);
        set_handler_callback(DISABLEQ+MAPS, cli_disable_all_queueing);
        set_handler_callback(RESTOREQ+MAPS, cli_restore_all_queueing);
-       set_handler_callback(QUIT, cli_quit);
-       set_handler_callback(SHUTDOWN, cli_shutdown);
+       set_unlocked_handler_callback(QUIT, cli_quit);
+       set_unlocked_handler_callback(SHUTDOWN, cli_shutdown);
        set_handler_callback(GETPRSTATUS+MAP, cli_getprstatus);
        set_handler_callback(SETPRSTATUS+MAP, cli_setprstatus);
        set_handler_callback(UNSETPRSTATUS+MAP, cli_unsetprstatus);
@@ -1016,32 +1254,14 @@ uxlsnrloop (void * ap)
 
        umask(077);
        uxsock_listen(&uxsock_trigger, ap);
-
+       pthread_cleanup_pop(1);
        return NULL;
 }
 
 void
 exit_daemon (void)
 {
-       sem_post(&exit_sem);
-}
-
-const char *
-daemon_status(void)
-{
-       switch (running_state) {
-       case DAEMON_INIT:
-               return "init";
-       case DAEMON_START:
-               return "startup";
-       case DAEMON_CONFIGURE:
-               return "configure";
-       case DAEMON_RUNNING:
-               return "running";
-       case DAEMON_SHUTDOWN:
-               return "shutdown";
-       }
-       return NULL;
+       post_config_state(DAEMON_SHUTDOWN);
 }
 
 static void
@@ -1152,7 +1372,8 @@ missing_uev_wait_tick(struct vectors *vecs)
 {
        struct multipath * mpp;
        unsigned int i;
-       int timed_out = 0;
+       int timed_out = 0, delayed_reconfig;
+       struct config *conf;
 
        vector_foreach_slot (vecs->mpvec, mpp, i) {
                if (mpp->wait_for_udev && --mpp->uev_wait_tick <= 0) {
@@ -1167,10 +1388,13 @@ missing_uev_wait_tick(struct vectors *vecs)
                }
        }
 
-       if (timed_out && conf->delayed_reconfig &&
+       conf = get_multipath_config();
+       delayed_reconfig = conf->delayed_reconfig;
+       put_multipath_config(conf);
+       if (timed_out && delayed_reconfig &&
            !need_to_delay_reconfig(vecs)) {
                condlog(2, "reconfigure (delayed)");
-               reconfigure(vecs);
+               set_config_state(DAEMON_CONFIGURE);
        }
 }
 
@@ -1200,10 +1424,11 @@ retry_count_tick(vector mpvec)
        unsigned int i;
 
        vector_foreach_slot (mpvec, mpp, i) {
-               if (mpp->retry_tick) {
+               if (mpp->retry_tick > 0) {
                        mpp->stat_total_queueing_time++;
                        condlog(4, "%s: Retrying.. No active path", mpp->alias);
                        if(--mpp->retry_tick == 0) {
+                               mpp->stat_map_failures++;
                                dm_queue_if_no_path(mpp->alias, 0);
                                condlog(2, "%s: Disable queueing", mpp->alias);
                        }
@@ -1217,12 +1442,15 @@ int update_prio(struct path *pp, int refresh_all)
        struct path *pp1;
        struct pathgroup * pgp;
        int i, j, changed = 0;
+       struct config *conf;
 
        if (refresh_all) {
                vector_foreach_slot (pp->mpp->pg, pgp, i) {
                        vector_foreach_slot (pgp->paths, pp1, j) {
                                oldpriority = pp1->priority;
-                               pathinfo(pp1, conf->hwtable, DI_PRIO);
+                               conf = get_multipath_config();
+                               pathinfo(pp1, conf, DI_PRIO);
+                               put_multipath_config(conf);
                                if (pp1->priority != oldpriority)
                                        changed = 1;
                        }
@@ -1230,7 +1458,10 @@ int update_prio(struct path *pp, int refresh_all)
                return changed;
        }
        oldpriority = pp->priority;
-       pathinfo(pp, conf->hwtable, DI_PRIO);
+       conf = get_multipath_config();
+       if (pp->state != PATH_DOWN)
+               pathinfo(pp, conf, DI_PRIO);
+       put_multipath_config(conf);
 
        if (pp->priority == oldpriority)
                return 0;
@@ -1239,7 +1470,7 @@ int update_prio(struct path *pp, int refresh_all)
 
 int update_path_groups(struct multipath *mpp, struct vectors *vecs, int refresh)
 {
-       if (reload_map(vecs, mpp, refresh))
+       if (reload_map(vecs, mpp, refresh, 1))
                return 1;
 
        dm_lib_release();
@@ -1250,11 +1481,98 @@ int update_path_groups(struct multipath *mpp, struct vectors *vecs, int refresh)
        return 0;
 }
 
+void repair_path(struct path * pp)
+{
+       if (pp->state != PATH_DOWN)
+               return;
+
+       checker_repair(&pp->checker);
+       LOG_MSG(1, checker_message(&pp->checker));
+}
+
+static int check_path_reinstate_state(struct path * pp) {
+       struct timespec curr_time;
+       if (!((pp->mpp->san_path_err_threshold > 0) &&
+                               (pp->mpp->san_path_err_forget_rate > 0) &&
+                               (pp->mpp->san_path_err_recovery_time >0))) {
+               return 0;
+       }
+
+       if (pp->disable_reinstate) {
+               /* If we don't know how much time has passed, automatically
+                * reinstate the path, just to be safe. Also, if there are
+                * no other usable paths, reinstate the path
+                */
+               if (clock_gettime(CLOCK_MONOTONIC, &curr_time) != 0 ||
+                               pp->mpp->nr_active == 0) {
+                       condlog(2, "%s : reinstating path early", pp->dev);
+                       goto reinstate_path;
+               }
+               if ((curr_time.tv_sec - pp->dis_reinstate_time ) > pp->mpp->san_path_err_recovery_time) {
+                       condlog(2,"%s : reinstate the path after err recovery time", pp->dev);
+                       goto reinstate_path;
+               }
+               return 1;
+       }
+       /* forget errors on a working path */
+       if ((pp->state == PATH_UP || pp->state == PATH_GHOST) &&
+                       pp->path_failures > 0) {
+               if (pp->san_path_err_forget_rate > 0){
+                       pp->san_path_err_forget_rate--;
+               } else {
+                       /* for every san_path_err_forget_rate number of
+                        * successful path checks decrement path_failures by 1
+                        */
+                       pp->path_failures--;
+                       pp->san_path_err_forget_rate = pp->mpp->san_path_err_forget_rate;
+               }
+               return 0;
+       }
+
+       /* If the path isn't recovering from a failed state, do nothing */
+       if (pp->state != PATH_DOWN && pp->state != PATH_SHAKY &&
+                       pp->state != PATH_TIMEOUT)
+               return 0;
+
+       if (pp->path_failures == 0)
+               pp->san_path_err_forget_rate = pp->mpp->san_path_err_forget_rate;
+
+       pp->path_failures++;
+
+       /* if we don't know the currently time, we don't know how long to
+        * delay the path, so there's no point in checking if we should
+        */
+
+       if (clock_gettime(CLOCK_MONOTONIC, &curr_time) != 0)
+               return 0;
+       /* when path failures has exceeded the san_path_err_threshold
+        * place the path in delayed state till san_path_err_recovery_time
+        * so that the cutomer can rectify the issue within this time. After
+        * the completion of san_path_err_recovery_time it should
+        * automatically reinstate the path
+        */
+       if (pp->path_failures > pp->mpp->san_path_err_threshold) {
+               condlog(2, "%s : hit error threshold. Delaying path reinstatement", pp->dev);
+               pp->dis_reinstate_time = curr_time.tv_sec;
+               pp->disable_reinstate = 1;
+               return 1;
+       } else {
+               return 0;
+       }
+
+reinstate_path:
+       pp->path_failures = 0;
+       pp->disable_reinstate = 0;
+       pp->san_path_err_forget_rate = 0;
+       return 0;
+}
+
 /*
- * Returns '1' if the path has been checked, '0' otherwise
+ * Returns '1' if the path has been checked, '-1' if it was blacklisted
+ * and '0' otherwise
  */
 int
-check_path (struct vectors * vecs, struct path * pp)
+check_path (struct vectors * vecs, struct path * pp, int ticks)
 {
        int newstate;
        int new_path_up = 0;
@@ -1262,16 +1580,25 @@ check_path (struct vectors * vecs, struct path * pp)
        int add_active;
        int disable_reinstate = 0;
        int oldchkrstate = pp->chkrstate;
+       int retrigger_tries, checkint;
+       struct config *conf;
+       int ret;
 
        if ((pp->initialized == INIT_OK ||
             pp->initialized == INIT_REQUESTED_UDEV) && !pp->mpp)
                return 0;
 
-       if (pp->tick && --pp->tick)
+       if (pp->tick)
+               pp->tick -= (pp->tick > ticks) ? ticks : pp->tick;
+       if (pp->tick)
                return 0; /* don't check this path yet */
 
+       conf = get_multipath_config();
+       retrigger_tries = conf->retrigger_tries;
+       checkint = conf->checkint;
+       put_multipath_config(conf);
        if (!pp->mpp && pp->initialized == INIT_MISSING_UDEV &&
-           pp->retriggers < conf->retrigger_tries) {
+           pp->retriggers < retrigger_tries) {
                condlog(2, "%s: triggering change event to reinitialize",
                        pp->dev);
                pp->initialized = INIT_REQUESTED_UDEV;
@@ -1279,13 +1606,13 @@ check_path (struct vectors * vecs, struct path * pp)
                sysfs_attr_set_value(pp->udev, "uevent", "change",
                                     strlen("change"));
                return 0;
-       } 
+       }
 
        /*
         * provision a next check soonest,
         * in case we exit abnormaly from here
         */
-       pp->tick = conf->checkint;
+       pp->tick = checkint;
 
        newstate = path_offline(pp);
        /*
@@ -1296,24 +1623,40 @@ check_path (struct vectors * vecs, struct path * pp)
        if (newstate == PATH_REMOVED)
                newstate = PATH_DOWN;
 
-       if (newstate == PATH_UP)
-               newstate = get_state(pp, 1);
-       else
+       if (newstate == PATH_UP) {
+               conf = get_multipath_config();
+               newstate = get_state(pp, conf, 1);
+               put_multipath_config(conf);
+       } else
                checker_clear_message(&pp->checker);
 
+       if (pp->wwid_changed) {
+               condlog(2, "%s: path wwid has changed. Refusing to use",
+                       pp->dev);
+               newstate = PATH_DOWN;
+       }
+
        if (newstate == PATH_WILD || newstate == PATH_UNCHECKED) {
                condlog(2, "%s: unusable path", pp->dev);
-               pathinfo(pp, conf->hwtable, 0);
+               conf = get_multipath_config();
+               pathinfo(pp, conf, 0);
+               put_multipath_config(conf);
                return 1;
        }
        if (!pp->mpp) {
                if (!strlen(pp->wwid) && pp->initialized != INIT_MISSING_UDEV &&
                    (newstate == PATH_UP || newstate == PATH_GHOST)) {
                        condlog(2, "%s: add missing path", pp->dev);
-                       if (pathinfo(pp, conf->hwtable, DI_ALL) == 0) {
-                               ev_add_path(pp, vecs);
+                       conf = get_multipath_config();
+                       ret = pathinfo(pp, conf, DI_ALL | DI_BLACKLIST);
+                       if (ret == PATHINFO_OK) {
+                               ev_add_path(pp, vecs, 1);
                                pp->tick = 1;
+                       } else if (ret == PATHINFO_SKIPPED) {
+                               put_multipath_config(conf);
+                               return -1;
                        }
+                       put_multipath_config(conf);
                }
                return 0;
        }
@@ -1328,7 +1671,7 @@ check_path (struct vectors * vecs, struct path * pp)
        /*
         * Synchronize with kernel state
         */
-       if (update_multipath_strings(pp->mpp, vecs->pathvec)) {
+       if (update_multipath_strings(pp->mpp, vecs->pathvec, 1)) {
                condlog(1, "%s: Could not synchronize with kernel state",
                        pp->dev);
                pp->dmstate = PSTATE_UNDEF;
@@ -1338,8 +1681,14 @@ check_path (struct vectors * vecs, struct path * pp)
                return 0;
 
        if ((newstate == PATH_UP || newstate == PATH_GHOST) &&
+                       check_path_reinstate_state(pp)) {
+               pp->state = PATH_DELAYED;
+               return 1;
+       }
+
+       if ((newstate == PATH_UP || newstate == PATH_GHOST) &&
             pp->wait_checks > 0) {
-               if (pp->mpp && pp->mpp->nr_active > 0) {
+               if (pp->mpp->nr_active > 0) {
                        pp->state = PATH_DELAYED;
                        pp->wait_checks--;
                        return 1;
@@ -1362,16 +1711,17 @@ check_path (struct vectors * vecs, struct path * pp)
                int oldstate = pp->state;
                pp->state = newstate;
 
-               if (strlen(checker_message(&pp->checker)))
-                       LOG_MSG(1, checker_message(&pp->checker));
+               LOG_MSG(1, checker_message(&pp->checker));
 
                /*
                 * upon state change, reset the checkint
                 * to the shortest delay
                 */
+               conf = get_multipath_config();
                pp->checkint = conf->checkint;
+               put_multipath_config(conf);
 
-               if (newstate == PATH_DOWN || newstate == PATH_SHAKY) {
+               if (newstate != PATH_UP && newstate != PATH_GHOST) {
                        /*
                         * proactively fail path in the DM
                         */
@@ -1392,6 +1742,7 @@ check_path (struct vectors * vecs, struct path * pp)
                        pp->mpp->failback_tick = 0;
 
                        pp->mpp->stat_path_failures++;
+                       repair_path(pp);
                        return 1;
                }
 
@@ -1421,7 +1772,7 @@ check_path (struct vectors * vecs, struct path * pp)
                }
                if (!disable_reinstate && reinstate_path(pp, add_active)) {
                        condlog(3, "%s: reload map", pp->dev);
-                       ev_add_path(pp, vecs);
+                       ev_add_path(pp, vecs, 1);
                        pp->tick = 1;
                        return 0;
                }
@@ -1444,21 +1795,25 @@ check_path (struct vectors * vecs, struct path * pp)
                        /* Clear IO errors */
                        if (reinstate_path(pp, 0)) {
                                condlog(3, "%s: reload map", pp->dev);
-                               ev_add_path(pp, vecs);
+                               ev_add_path(pp, vecs, 1);
                                pp->tick = 1;
                                return 0;
                        }
                } else {
+                       unsigned int max_checkint;
                        LOG_MSG(4, checker_message(&pp->checker));
-                       if (pp->checkint != conf->max_checkint) {
+                       conf = get_multipath_config();
+                       max_checkint = conf->max_checkint;
+                       put_multipath_config(conf);
+                       if (pp->checkint != max_checkint) {
                                /*
                                 * double the next check delay.
                                 * max at conf->max_checkint
                                 */
-                               if (pp->checkint < (conf->max_checkint / 2))
+                               if (pp->checkint < (max_checkint / 2))
                                        pp->checkint = 2 * pp->checkint;
                                else
-                                       pp->checkint = conf->max_checkint;
+                                       pp->checkint = max_checkint;
 
                                condlog(4, "%s: delay next check %is",
                                        pp->dev_t, pp->checkint);
@@ -1468,16 +1823,20 @@ check_path (struct vectors * vecs, struct path * pp)
                        pp->tick = pp->checkint;
                }
        }
-       else if (newstate == PATH_DOWN &&
-                strlen(checker_message(&pp->checker))) {
-               if (conf->log_checker_err == LOG_CHKR_ERR_ONCE)
+       else if (newstate == PATH_DOWN) {
+               int log_checker_err;
+
+               conf = get_multipath_config();
+               log_checker_err = conf->log_checker_err;
+               put_multipath_config(conf);
+               if (log_checker_err == LOG_CHKR_ERR_ONCE)
                        LOG_MSG(3, checker_message(&pp->checker));
                else
                        LOG_MSG(2, checker_message(&pp->checker));
        }
 
        pp->state = newstate;
-
+       repair_path(pp);
 
        if (pp->mpp->wait_for_udev)
                return 1;
@@ -1502,6 +1861,19 @@ check_path (struct vectors * vecs, struct path * pp)
        return 1;
 }
 
+static void init_path_check_interval(struct vectors *vecs)
+{
+       struct config *conf;
+       struct path *pp;
+       unsigned int i;
+
+       vector_foreach_slot (vecs->pathvec, pp, i) {
+               conf = get_multipath_config();
+               pp->checkint = conf->checkint;
+               put_multipath_config(conf);
+       }
+}
+
 static void *
 checkerloop (void *ap)
 {
@@ -1509,61 +1881,140 @@ checkerloop (void *ap)
        struct path *pp;
        int count = 0;
        unsigned int i;
+       struct itimerval timer_tick_it;
+       struct timespec last_time;
+       struct config *conf;
 
+       pthread_cleanup_push(rcu_unregister, NULL);
+       rcu_register_thread();
        mlockall(MCL_CURRENT | MCL_FUTURE);
        vecs = (struct vectors *)ap;
        condlog(2, "path checkers start up");
 
-       /*
-        * init the path check interval
-        */
-       vector_foreach_slot (vecs->pathvec, pp, i) {
-               pp->checkint = conf->checkint;
-       }
+       /* Tweak start time for initial path check */
+       if (clock_gettime(CLOCK_MONOTONIC, &last_time) != 0)
+               last_time.tv_sec = 0;
+       else
+               last_time.tv_sec -= 1;
 
        while (1) {
-               struct timeval diff_time, start_time, end_time;
-               int num_paths = 0;
+               struct timespec diff_time, start_time, end_time;
+               int num_paths = 0, ticks = 0, signo, strict_timing, rc = 0;
+               sigset_t mask;
 
-               if (gettimeofday(&start_time, NULL) != 0)
+               if (clock_gettime(CLOCK_MONOTONIC, &start_time) != 0)
                        start_time.tv_sec = 0;
-               pthread_cleanup_push(cleanup_lock, &vecs->lock);
-               lock(vecs->lock);
-               pthread_testcancel();
-               condlog(4, "tick");
+               if (start_time.tv_sec && last_time.tv_sec) {
+                       timespecsub(&start_time, &last_time, &diff_time);
+                       condlog(4, "tick (%lu.%06lu secs)",
+                               diff_time.tv_sec, diff_time.tv_nsec / 1000);
+                       last_time = start_time;
+                       ticks = diff_time.tv_sec;
+               } else {
+                       ticks = 1;
+                       condlog(4, "tick (%d ticks)", ticks);
+               }
 #ifdef USE_SYSTEMD
                if (use_watchdog)
                        sd_notify(0, "WATCHDOG=1");
 #endif
-               if (vecs->pathvec) {
-                       vector_foreach_slot (vecs->pathvec, pp, i) {
-                               num_paths += check_path(vecs, pp);
-                       }
+               rc = set_config_state(DAEMON_RUNNING);
+               if (rc == ETIMEDOUT) {
+                       condlog(4, "timeout waiting for DAEMON_IDLE");
+                       continue;
                }
-               if (vecs->mpvec) {
-                       defered_failback_tick(vecs->mpvec);
-                       retry_count_tick(vecs->mpvec);
-                       missing_uev_wait_tick(vecs);
+
+               pthread_cleanup_push(cleanup_lock, &vecs->lock);
+               lock(&vecs->lock);
+               pthread_testcancel();
+               vector_foreach_slot (vecs->pathvec, pp, i) {
+                       rc = check_path(vecs, pp, ticks);
+                       if (rc < 0) {
+                               vector_del_slot(vecs->pathvec, i);
+                               free_path(pp);
+                               i--;
+                       } else
+                               num_paths += rc;
                }
+               lock_cleanup_pop(vecs->lock);
+
+               pthread_cleanup_push(cleanup_lock, &vecs->lock);
+               lock(&vecs->lock);
+               pthread_testcancel();
+               defered_failback_tick(vecs->mpvec);
+               retry_count_tick(vecs->mpvec);
+               missing_uev_wait_tick(vecs);
+               lock_cleanup_pop(vecs->lock);
+
                if (count)
                        count--;
                else {
+                       pthread_cleanup_push(cleanup_lock, &vecs->lock);
+                       lock(&vecs->lock);
+                       pthread_testcancel();
                        condlog(4, "map garbage collection");
                        mpvec_garbage_collector(vecs);
                        count = MAPGCINT;
+                       lock_cleanup_pop(vecs->lock);
                }
 
-               lock_cleanup_pop(vecs->lock);
+               diff_time.tv_nsec = 0;
                if (start_time.tv_sec &&
-                   gettimeofday(&end_time, NULL) == 0 &&
-                   num_paths) {
-                       timersub(&end_time, &start_time, &diff_time);
-                       condlog(3, "checked %d path%s in %lu.%06lu secs",
-                               num_paths, num_paths > 1 ? "s" : "",
-                               diff_time.tv_sec, diff_time.tv_usec);
+                   clock_gettime(CLOCK_MONOTONIC, &end_time) == 0) {
+                       timespecsub(&end_time, &start_time, &diff_time);
+                       if (num_paths) {
+                               unsigned int max_checkint;
+
+                               condlog(3, "checked %d path%s in %lu.%06lu secs",
+                                       num_paths, num_paths > 1 ? "s" : "",
+                                       diff_time.tv_sec,
+                                       diff_time.tv_nsec / 1000);
+                               conf = get_multipath_config();
+                               max_checkint = conf->max_checkint;
+                               put_multipath_config(conf);
+                               if (diff_time.tv_sec > max_checkint)
+                                       condlog(1, "path checkers took longer "
+                                               "than %lu seconds, consider "
+                                               "increasing max_polling_interval",
+                                               diff_time.tv_sec);
+                       }
+               }
+
+               post_config_state(DAEMON_IDLE);
+               conf = get_multipath_config();
+               strict_timing = conf->strict_timing;
+               put_multipath_config(conf);
+               if (!strict_timing)
+                       sleep(1);
+               else {
+                       timer_tick_it.it_interval.tv_sec = 0;
+                       timer_tick_it.it_interval.tv_usec = 0;
+                       if (diff_time.tv_nsec) {
+                               timer_tick_it.it_value.tv_sec = 0;
+                               timer_tick_it.it_value.tv_usec =
+                                    1000UL * 1000 * 1000 - diff_time.tv_nsec;
+                       } else {
+                               timer_tick_it.it_value.tv_sec = 1;
+                               timer_tick_it.it_value.tv_usec = 0;
+                       }
+                       setitimer(ITIMER_REAL, &timer_tick_it, NULL);
+
+                       sigemptyset(&mask);
+                       sigaddset(&mask, SIGALRM);
+                       condlog(3, "waiting for %lu.%06lu secs",
+                               timer_tick_it.it_value.tv_sec,
+                               timer_tick_it.it_value.tv_usec);
+                       if (sigwait(&mask, &signo) != 0) {
+                               condlog(3, "sigwait failed with error %d",
+                                       errno);
+                               conf = get_multipath_config();
+                               conf->strict_timing = 0;
+                               put_multipath_config(conf);
+                               break;
+                       }
                }
-               sleep(1);
        }
+       pthread_cleanup_pop(1);
        return NULL;
 }
 
@@ -1574,24 +2025,35 @@ configure (struct vectors * vecs, int start_waiters)
        struct path * pp;
        vector mpvec;
        int i, ret;
+       struct config *conf;
+       static int force_reload = FORCE_RELOAD_WEAK;
 
-       if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
+       if (!vecs->pathvec && !(vecs->pathvec = vector_alloc())) {
+               condlog(0, "couldn't allocate path vec in configure");
                return 1;
+       }
 
-       if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
+       if (!vecs->mpvec && !(vecs->mpvec = vector_alloc())) {
+               condlog(0, "couldn't allocate multipath vec in configure");
                return 1;
+       }
 
-       if (!(mpvec = vector_alloc()))
+       if (!(mpvec = vector_alloc())) {
+               condlog(0, "couldn't allocate new maps vec in configure");
                return 1;
+       }
 
        /*
         * probe for current path (from sysfs) and map (from dm) sets
         */
-       ret = path_discovery(vecs->pathvec, conf, DI_ALL);
-       if (ret < 0)
+       ret = path_discovery(vecs->pathvec, DI_ALL);
+       if (ret < 0) {
+               condlog(0, "configure failed at path discovery");
                return 1;
+       }
 
        vector_foreach_slot (vecs->pathvec, pp, i){
+               conf = get_multipath_config();
                if (filter_path(conf, pp) > 0){
                        vector_del_slot(vecs->pathvec, i);
                        free_path(pp);
@@ -1599,22 +2061,35 @@ configure (struct vectors * vecs, int start_waiters)
                }
                else
                        pp->checkint = conf->checkint;
+               put_multipath_config(conf);
        }
-       if (map_discovery(vecs))
+       if (map_discovery(vecs)) {
+               condlog(0, "configure failed at map discovery");
                return 1;
+       }
 
        /*
         * create new set of maps & push changed ones into dm
+        * In the first call, use FORCE_RELOAD_WEAK to avoid making
+        * superfluous ACT_RELOAD ioctls. Later calls are done
+        * with FORCE_RELOAD_YES.
         */
-       if (coalesce_paths(vecs, mpvec, NULL, 1))
+       ret = coalesce_paths(vecs, mpvec, NULL, force_reload, CMD_NONE);
+       if (force_reload == FORCE_RELOAD_WEAK)
+               force_reload = FORCE_RELOAD_YES;
+       if (ret) {
+               condlog(0, "configure failed while coalescing paths");
                return 1;
+       }
 
        /*
         * may need to remove some maps which are no longer relevant
         * e.g., due to blacklist changes in conf file
         */
-       if (coalesce_maps(vecs, mpvec))
+       if (coalesce_maps(vecs, mpvec)) {
+               condlog(0, "configure failed while coalescing maps");
                return 1;
+       }
 
        dm_lib_release();
 
@@ -1639,11 +2114,16 @@ configure (struct vectors * vecs, int start_waiters)
         * start dm event waiter threads for these new maps
         */
        vector_foreach_slot(vecs->mpvec, mpp, i) {
-               if (setup_multipath(vecs, mpp))
-                       return 1;
-               if (start_waiters)
-                       if (start_waiter_thread(mpp, vecs))
-                               return 1;
+               if (setup_multipath(vecs, mpp)) {
+                       i--;
+                       continue;
+               }
+               if (start_waiters) {
+                       if (start_waiter_thread(mpp, vecs)) {
+                               remove_map(mpp, vecs, 1);
+                               i--;
+                       }
+               }
        }
        return 0;
 }
@@ -1664,13 +2144,21 @@ need_to_delay_reconfig(struct vectors * vecs)
        return 0;
 }
 
+void rcu_free_config(struct rcu_head *head)
+{
+       struct config *conf = container_of(head, struct config, rcu);
+
+       free_config(conf);
+}
+
 int
 reconfigure (struct vectors * vecs)
 {
-       struct config * old = conf;
-       int retval = 1;
+       struct config * old, *conf;
 
-       running_state = DAEMON_CONFIGURE;
+       conf = load_config(DEFAULT_CONFIGFILE);
+       if (!conf)
+               return 1;
 
        /*
         * free old map and path vectors ... they use old conf state
@@ -1678,29 +2166,33 @@ reconfigure (struct vectors * vecs)
        if (VECTOR_SIZE(vecs->mpvec))
                remove_maps_and_stop_waiters(vecs);
 
-       if (VECTOR_SIZE(vecs->pathvec))
-               free_pathvec(vecs->pathvec, FREE_PATHS);
-
+       free_pathvec(vecs->pathvec, FREE_PATHS);
        vecs->pathvec = NULL;
-       conf = NULL;
 
        /* Re-read any timezone changes */
        tzset();
 
-       if (!load_config(DEFAULT_CONFIGFILE, udev)) {
-               dm_drv_version(conf->version, TGT_MPATH);
-               conf->verbosity = old->verbosity;
-               conf->bindings_read_only = old->bindings_read_only;
-               conf->ignore_new_devs = old->ignore_new_devs;
-               conf->daemon = 1;
-               configure(vecs, 1);
-               free_config(old);
-               retval = 0;
+       dm_drv_version(conf->version, TGT_MPATH);
+       if (verbosity)
+               conf->verbosity = verbosity;
+       if (bindings_read_only)
+               conf->bindings_read_only = bindings_read_only;
+       if (conf->find_multipaths) {
+               condlog(2, "find_multipaths is set: -n is implied");
+               ignore_new_devs = 1;
        }
+       if (ignore_new_devs)
+               conf->ignore_new_devs = ignore_new_devs;
+       uxsock_timeout = conf->uxsock_timeout;
 
-       running_state = DAEMON_RUNNING;
+       old = rcu_dereference(multipath_conf);
+       rcu_assign_pointer(multipath_conf, conf);
+       call_rcu(&old->rcu, rcu_free_config);
 
-       return retval;
+       configure(vecs, 1);
+
+
+       return 0;
 }
 
 static struct vectors *
@@ -1713,21 +2205,9 @@ init_vecs (void)
        if (!vecs)
                return NULL;
 
-       vecs->lock.mutex =
-               (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
-
-       if (!vecs->lock.mutex)
-               goto out;
-
-       pthread_mutex_init(vecs->lock.mutex, NULL);
-       vecs->lock.depth = 0;
+       pthread_mutex_init(&vecs->lock.mutex, NULL);
 
        return vecs;
-
-out:
-       FREE(vecs);
-       condlog(0, "failed to init paths");
-       return NULL;
 }
 
 static void *
@@ -1752,20 +2232,13 @@ signal_set(int signo, void (*func) (int))
 void
 handle_signals(void)
 {
-       if (reconfig_sig && running_state == DAEMON_RUNNING) {
-               pthread_cleanup_push(cleanup_lock,
-                               &gvecs->lock);
-               lock(gvecs->lock);
-               pthread_testcancel();
-               if (need_to_delay_reconfig(gvecs)) {
-                       conf->delayed_reconfig = 1;
-                       condlog(2, "delaying reconfigure (signal)");
-               }
-               else {
-                       condlog(2, "reconfigure (signal)");
-                       reconfigure(gvecs);
-               }
-               lock_cleanup_pop(gvecs->lock);
+       if (exit_sig) {
+               condlog(2, "exit (signal)");
+               exit_daemon();
+       }
+       if (reconfig_sig) {
+               condlog(2, "reconfigure (signal)");
+               set_config_state(DAEMON_CONFIGURE);
        }
        if (log_reset_sig) {
                condlog(2, "reset log (signal)");
@@ -1773,6 +2246,7 @@ handle_signals(void)
                log_reset("multipathd");
                pthread_mutex_unlock(&logq_lock);
        }
+       exit_sig = 0;
        reconfig_sig = 0;
        log_reset_sig = 0;
 }
@@ -1786,7 +2260,7 @@ sighup (int sig)
 static void
 sigend (int sig)
 {
-       exit_daemon();
+       exit_sig = 1;
 }
 
 static void
@@ -1807,17 +2281,15 @@ signal_init(void)
        sigset_t set;
 
        sigemptyset(&set);
-       sigaddset(&set, SIGHUP);
-       sigaddset(&set, SIGUSR1);
        sigaddset(&set, SIGUSR2);
-       pthread_sigmask(SIG_BLOCK, &set, NULL);
+       pthread_sigmask(SIG_SETMASK, &set, NULL);
 
        signal_set(SIGHUP, sighup);
        signal_set(SIGUSR1, sigusr1);
        signal_set(SIGUSR2, sigusr2);
        signal_set(SIGINT, sigend);
        signal_set(SIGTERM, sigend);
-       signal(SIGPIPE, SIG_IGN);
+       signal_set(SIGPIPE, sigend);
 }
 
 static void
@@ -1893,18 +2365,19 @@ child (void * param)
        int i;
 #ifdef USE_SYSTEMD
        unsigned long checkint;
+       int startup_done = 0;
 #endif
        int rc;
+       int pid_fd = -1;
+       struct config *conf;
        char *envp;
 
        mlockall(MCL_CURRENT | MCL_FUTURE);
-       sem_init(&exit_sem, 0, 0);
        signal_init();
+       rcu_init();
 
-       udev = udev_new();
-
-       setup_thread_attr(&misc_attr, 64 * 1024, 1);
-       setup_thread_attr(&uevent_attr, DEFAULT_UEVENT_STACKSIZE * 1024, 1);
+       setup_thread_attr(&misc_attr, 64 * 1024, 0);
+       setup_thread_attr(&uevent_attr, DEFAULT_UEVENT_STACKSIZE * 1024, 0);
        setup_thread_attr(&waiter_attr, 32 * 1024, 1);
 
        if (logsink == 1) {
@@ -1912,30 +2385,38 @@ child (void * param)
                log_thread_start(&log_attr);
                pthread_attr_destroy(&log_attr);
        }
-       if (pidfile_create(DEFAULT_PIDFILE, daemon_pid)) {
+       pid_fd = pidfile_create(DEFAULT_PIDFILE, daemon_pid);
+       if (pid_fd < 0) {
                condlog(1, "failed to create pidfile");
                if (logsink == 1)
                        log_thread_stop();
                exit(1);
        }
 
-       running_state = DAEMON_START;
+       post_config_state(DAEMON_START);
 
-#ifdef USE_SYSTEMD
-       sd_notify(0, "STATUS=startup");
-#endif
        condlog(2, "--------start up--------");
        condlog(2, "read " DEFAULT_CONFIGFILE);
 
-       if (load_config(DEFAULT_CONFIGFILE, udev))
+       conf = load_config(DEFAULT_CONFIGFILE);
+       if (!conf)
                goto failed;
 
+       if (verbosity)
+               conf->verbosity = verbosity;
+       if (bindings_read_only)
+               conf->bindings_read_only = bindings_read_only;
+       if (ignore_new_devs)
+               conf->ignore_new_devs = ignore_new_devs;
+       uxsock_timeout = conf->uxsock_timeout;
+       rcu_assign_pointer(multipath_conf, conf);
+       dm_init(conf->verbosity);
        dm_drv_version(conf->version, TGT_MPATH);
-       if (init_checkers()) {
+       if (init_checkers(conf->multipath_dir)) {
                condlog(0, "failed to initialize checkers");
                goto failed;
        }
-       if (init_prio()) {
+       if (init_prio(conf->multipath_dir)) {
                condlog(0, "failed to initialize prioritizers");
                goto failed;
        }
@@ -1979,7 +2460,6 @@ child (void * param)
        setscheduler();
        set_oom_adj();
 
-       conf->daemon = 1;
        dm_udev_set_sync_support(0);
 #ifdef USE_SYSTEMD
        envp = getenv("WATCHDOG_USEC");
@@ -1997,6 +2477,18 @@ child (void * param)
        }
 #endif
        /*
+        * Startup done, invalidate configuration
+        */
+       conf = NULL;
+
+       /*
+        * Signal start of configuration
+        */
+       post_config_state(DAEMON_CONFIGURE);
+
+       init_path_check_interval(vecs);
+
+       /*
         * Start uevent listener early to catch events
         */
        if ((rc = pthread_create(&uevent_thr, &uevent_attr, ueventloop, udev))) {
@@ -2008,21 +2500,6 @@ child (void * param)
                condlog(0, "failed to create cli listener: %d", rc);
                goto failed;
        }
-       /*
-        * fetch and configure both paths and multipaths
-        */
-#ifdef USE_SYSTEMD
-       sd_notify(0, "STATUS=configure");
-#endif
-       running_state = DAEMON_CONFIGURE;
-
-       lock(vecs->lock);
-       if (configure(vecs, 1)) {
-               unlock(vecs->lock);
-               condlog(0, "failure during configuration");
-               goto failed;
-       }
-       unlock(vecs->lock);
 
        /*
         * start threads
@@ -2037,46 +2514,61 @@ child (void * param)
        }
        pthread_attr_destroy(&misc_attr);
 
-       running_state = DAEMON_RUNNING;
+       while (running_state != DAEMON_SHUTDOWN) {
+               pthread_cleanup_push(config_cleanup, NULL);
+               pthread_mutex_lock(&config_lock);
+               if (running_state != DAEMON_CONFIGURE &&
+                   running_state != DAEMON_SHUTDOWN) {
+                       pthread_cond_wait(&config_cond, &config_lock);
+               }
+               pthread_cleanup_pop(1);
+               if (running_state == DAEMON_CONFIGURE) {
+                       pthread_cleanup_push(cleanup_lock, &vecs->lock);
+                       lock(&vecs->lock);
+                       pthread_testcancel();
+                       if (!need_to_delay_reconfig(vecs)) {
+                               reconfigure(vecs);
+                       } else {
+                               conf = get_multipath_config();
+                               conf->delayed_reconfig = 1;
+                               put_multipath_config(conf);
+                       }
+                       lock_cleanup_pop(vecs->lock);
+                       post_config_state(DAEMON_IDLE);
 #ifdef USE_SYSTEMD
-       sd_notify(0, "READY=1\nSTATUS=running");
+                       if (!startup_done) {
+                               sd_notify(0, "READY=1");
+                               startup_done = 1;
+                       }
 #endif
+               }
+       }
 
-       /*
-        * exit path
-        */
-       while(sem_wait(&exit_sem) != 0); /* Do nothing */
-
-#ifdef USE_SYSTEMD
-       sd_notify(0, "STATUS=shutdown");
-#endif
-       running_state = DAEMON_SHUTDOWN;
-       lock(vecs->lock);
+       lock(&vecs->lock);
+       conf = get_multipath_config();
        if (conf->queue_without_daemon == QUE_NO_DAEMON_OFF)
                vector_foreach_slot(vecs->mpvec, mpp, i)
                        dm_queue_if_no_path(mpp->alias, 0);
+       put_multipath_config(conf);
        remove_maps_and_stop_waiters(vecs);
-       unlock(vecs->lock);
+       unlock(&vecs->lock);
 
        pthread_cancel(check_thr);
        pthread_cancel(uevent_thr);
        pthread_cancel(uxlsnr_thr);
        pthread_cancel(uevq_thr);
 
-       lock(vecs->lock);
+       pthread_join(check_thr, NULL);
+       pthread_join(uevent_thr, NULL);
+       pthread_join(uxlsnr_thr, NULL);
+       pthread_join(uevq_thr, NULL);
+
+       lock(&vecs->lock);
        free_pathvec(vecs->pathvec, FREE_PATHS);
        vecs->pathvec = NULL;
-       unlock(vecs->lock);
-       /* Now all the waitevent threads will start rushing in. */
-       while (vecs->lock.depth > 0) {
-               sleep (1); /* This is weak. */
-               condlog(3, "Have %d wait event checkers threads to de-alloc,"
-                       " waiting...", vecs->lock.depth);
-       }
-       pthread_mutex_destroy(vecs->lock.mutex);
-       FREE(vecs->lock.mutex);
-       vecs->lock.depth = 0;
-       vecs->lock.mutex = NULL;
+       unlock(&vecs->lock);
+
+       pthread_mutex_destroy(&vecs->lock.mutex);
        FREE(vecs);
        vecs = NULL;
 
@@ -2100,10 +2592,12 @@ child (void * param)
         * because logging functions like dlog() and dm_write_log()
         * reference the config.
         */
-       free_config(conf);
-       conf = NULL;
+       conf = rcu_dereference(multipath_conf);
+       rcu_assign_pointer(multipath_conf, NULL);
+       call_rcu(&conf->rcu, rcu_free_config);
        udev_unref(udev);
        udev = NULL;
+       pthread_attr_destroy(&waiter_attr);
 #ifdef _DEBUG_
        dbg_free_final(NULL);
 #endif
@@ -2117,6 +2611,8 @@ failed:
 #ifdef USE_SYSTEMD
        sd_notify(0, "ERRNO=1");
 #endif
+       if (pid_fd >= 0)
+               close(pid_fd);
        exit(1);
 }
 
@@ -2181,10 +2677,16 @@ main (int argc, char *argv[])
        int arg;
        int err;
        int foreground = 0;
+       struct config *conf;
+
+       ANNOTATE_BENIGN_RACE_SIZED(&multipath_conf, sizeof(multipath_conf),
+                                  "Manipulated through RCU");
+       ANNOTATE_BENIGN_RACE_SIZED(&running_state, sizeof(running_state),
+               "Suppress complaints about unprotected running_state reads");
+       ANNOTATE_BENIGN_RACE_SIZED(&uxsock_timeout, sizeof(uxsock_timeout),
+               "Suppress complaints about this scalar variable");
 
        logsink = 1;
-       running_state = DAEMON_INIT;
-       dm_init();
 
        if (getuid() != 0) {
                fprintf(stderr, "need to be root\n");
@@ -2197,13 +2699,12 @@ main (int argc, char *argv[])
                        strerror(errno));
        umask(umask(077) | 022);
 
-       conf = alloc_config();
+       pthread_cond_init_mono(&config_cond);
 
-       if (!conf)
-               exit(1);
+       udev = udev_new();
 
        while ((arg = getopt(argc, argv, ":dsv:k::Bn")) != EOF ) {
-       switch(arg) {
+               switch(arg) {
                case 'd':
                        foreground = 1;
                        if (logsink > 0)
@@ -2215,24 +2716,31 @@ main (int argc, char *argv[])
                            !isdigit(optarg[0]))
                                exit(1);
 
-                       conf->verbosity = atoi(optarg);
+                       verbosity = atoi(optarg);
                        break;
                case 's':
                        logsink = -1;
                        break;
                case 'k':
-                       if (load_config(DEFAULT_CONFIGFILE, udev_new()))
+                       conf = load_config(DEFAULT_CONFIGFILE);
+                       if (!conf)
                                exit(1);
-                       uxclnt(optarg, conf->uxsock_timeout);
+                       if (verbosity)
+                               conf->verbosity = verbosity;
+                       uxsock_timeout = conf->uxsock_timeout;
+                       uxclnt(optarg, uxsock_timeout + 100);
+                       free_config(conf);
                        exit(0);
                case 'B':
-                       conf->bindings_read_only = 1;
+                       bindings_read_only = 1;
                        break;
                case 'n':
-                       conf->ignore_new_devs = 1;
+                       ignore_new_devs = 1;
                        break;
                default:
-                       ;
+                       fprintf(stderr, "Invalid argument '-%c'\n",
+                               optopt);
+                       exit(1);
                }
        }
        if (optind < argc) {
@@ -2240,8 +2748,13 @@ main (int argc, char *argv[])
                char * s = cmd;
                char * c = s;
 
-               if (load_config(DEFAULT_CONFIGFILE, udev_new()))
+               conf = load_config(DEFAULT_CONFIGFILE);
+               if (!conf)
                        exit(1);
+               if (verbosity)
+                       conf->verbosity = verbosity;
+               uxsock_timeout = conf->uxsock_timeout;
+               memset(cmd, 0x0, CMDSIZE);
                while (optind < argc) {
                        if (strchr(argv[optind], ' '))
                                c += snprintf(c, s + CMDSIZE - c, "\"%s\" ", argv[optind]);
@@ -2250,7 +2763,8 @@ main (int argc, char *argv[])
                        optind++;
                }
                c += snprintf(c, s + CMDSIZE - c, "\n");
-               uxclnt(s, conf->uxsock_timeout);
+               uxclnt(s, uxsock_timeout + 100);
+               free_config(conf);
                exit(0);
        }
 
@@ -2385,4 +2899,3 @@ int mpath_pr_event_handle(struct path *pp)
        rc = pthread_join(thread, NULL);
        return 0;
 }
-