Imported Upstream version 0.7.0
[platform/upstream/multipath-tools.git] / multipathd / main.c
index aec89e7..0c61caa 100644 (file)
@@ -23,6 +23,7 @@
 #endif
 #include <semaphore.h>
 #include <time.h>
+#include <stdbool.h>
 
 /*
  * libmultipath
@@ -485,6 +486,8 @@ 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 */
                                return 1;
@@ -532,7 +535,8 @@ ev_add_map (char * dev, char * alias, struct vectors * vecs)
        r = get_refwwid(CMD_NONE, dev, DEV_DEVMAP, vecs->pathvec, &refwwid);
 
        if (refwwid) {
-               r = coalesce_paths(vecs, NULL, refwwid, 0, CMD_NONE);
+               r = coalesce_paths(vecs, NULL, refwwid, FORCE_RELOAD_NONE,
+                                  CMD_NONE);
                dm_lib_release();
        }
 
@@ -608,7 +612,7 @@ 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;
@@ -641,7 +645,7 @@ uev_add_path (struct uevent *uev, struct vectors * vecs)
                                     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);
@@ -665,7 +669,7 @@ uev_add_path (struct uevent *uev, struct vectors * vecs)
         */
        conf = get_multipath_config();
        ret = alloc_path_with_pathinfo(conf, uev->udev,
-                                      DI_ALL, &pp);
+                                      uev->wwid, DI_ALL, &pp);
        put_multipath_config(conf);
        if (!pp) {
                if (ret == PATHINFO_SKIPPED)
@@ -681,7 +685,7 @@ uev_add_path (struct uevent *uev, struct vectors * vecs)
                conf = get_multipath_config();
                pp->checkint = conf->checkint;
                put_multipath_config(conf);
-               ret = ev_add_path(pp, vecs);
+               ret = ev_add_path(pp, vecs, need_do_map);
        } else {
                condlog(0, "%s: failed to store path info, "
                        "dropping event",
@@ -699,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};
@@ -719,6 +723,7 @@ ev_add_path (struct path * pp, struct vectors * vecs)
            (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;
@@ -767,6 +772,13 @@ rescan:
        /* 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
         */
@@ -833,7 +845,7 @@ 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;
@@ -844,7 +856,7 @@ uev_remove_path (struct uevent *uev, struct vectors * vecs)
        pthread_testcancel();
        pp = find_path_by_dev(vecs->pathvec, uev->kernel);
        if (pp)
-               ret = ev_remove_path(pp, vecs);
+               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 */
@@ -855,7 +867,7 @@ uev_remove_path (struct uevent *uev, struct vectors * vecs)
 }
 
 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;
@@ -918,6 +930,8 @@ ev_remove_path (struct path *pp, struct vectors * vecs)
                        goto out;
                }
 
+               if (!need_do_map)
+                       goto out;
                /*
                 * reload the map
                 */
@@ -960,6 +974,7 @@ uev_update_path (struct uevent *uev, struct vectors * vecs)
        struct path * pp;
        struct config *conf;
        int disable_changed_wwids;
+       int needs_reinit = 0;
 
        conf = get_multipath_config();
        disable_changed_wwids = conf->disable_changed_wwids;
@@ -987,7 +1002,8 @@ uev_update_path (struct uevent *uev, struct vectors * vecs)
                                if (!pp->wwid_changed) {
                                        pp->wwid_changed = 1;
                                        pp->tick = 1;
-                                       dm_fail_path(pp->mpp->alias, pp->dev_t);
+                                       if (pp->mpp)
+                                               dm_fail_path(pp->mpp->alias, pp->dev_t);
                                }
                                goto out;
                        } else
@@ -995,14 +1011,17 @@ uev_update_path (struct uevent *uev, struct vectors * vecs)
                }
 
                if (pp->initialized == INIT_REQUESTED_UDEV)
-                       retval = uev_add_path(uev, vecs);
+                       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);
                        }
@@ -1010,9 +1029,25 @@ uev_update_path (struct uevent *uev, struct vectors * vecs)
        }
 out:
        lock_cleanup_pop(vecs->lock);
-       if (!pp)
-               condlog(0, "%s: spurious uevent, path not found", uev->kernel);
+       if (!pp) {
+               /* If the path is blacklisted, print a debug/non-default verbosity message. */
+               if (uev->udev) {
+                       int flag = DI_SYSFS | DI_WWID;
+
+                       conf = get_multipath_config();
+                       retval = alloc_path_with_pathinfo(conf, uev->udev, uev->wwid, flag, NULL);
+                       put_multipath_config(conf);
 
+                       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;
 }
 
@@ -1033,7 +1068,8 @@ map_discovery (struct vectors * vecs)
 }
 
 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;
@@ -1042,19 +1078,30 @@ uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
        *len = 0;
        vecs = (struct vectors *)trigger_data;
 
-       r = parse_cmd(str, reply, len, vecs, uxsock_timeout / 1000);
+       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, uxsock_timeout);
 
        if (r > 0) {
                if (r == ETIMEDOUT)
                        *reply = STRDUP("timeout\n");
                else
                        *reply = STRDUP("fail\n");
-               *len = strlen(*reply) + 1;
+               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 */
@@ -1062,40 +1109,15 @@ uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
        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 config *conf;
+       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 &&
@@ -1124,28 +1146,21 @@ uev_trigger (struct uevent * uev, void * trigger_data)
        }
 
        /*
-        * path add/remove event
+        * path add/remove/change event, add/remove maybe merged
         */
-       conf = get_multipath_config();
-       if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
-                          uev->kernel) > 0) {
-               put_multipath_config(conf);
-               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);
        }
-       put_multipath_config(conf);
 
-       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;
-       }
+       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:
        return r;
@@ -1210,6 +1225,8 @@ uxlsnrloop (void * ap)
        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);
@@ -1442,7 +1459,8 @@ int update_prio(struct path *pp, int refresh_all)
        }
        oldpriority = pp->priority;
        conf = get_multipath_config();
-       pathinfo(pp, conf, DI_PRIO);
+       if (pp->state != PATH_DOWN)
+               pathinfo(pp, conf, DI_PRIO);
        put_multipath_config(conf);
 
        if (pp->priority == oldpriority)
@@ -1472,6 +1490,83 @@ void repair_path(struct path * pp)
        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, '-1' if it was blacklisted
  * and '0' otherwise
@@ -1555,7 +1650,7 @@ check_path (struct vectors * vecs, struct path * pp, int ticks)
                        conf = get_multipath_config();
                        ret = pathinfo(pp, conf, DI_ALL | DI_BLACKLIST);
                        if (ret == PATHINFO_OK) {
-                               ev_add_path(pp, vecs);
+                               ev_add_path(pp, vecs, 1);
                                pp->tick = 1;
                        } else if (ret == PATHINFO_SKIPPED) {
                                put_multipath_config(conf);
@@ -1586,6 +1681,12 @@ check_path (struct vectors * vecs, struct path * pp, int ticks)
                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->nr_active > 0) {
                        pp->state = PATH_DELAYED;
@@ -1620,7 +1721,7 @@ check_path (struct vectors * vecs, struct path * pp, int ticks)
                pp->checkint = conf->checkint;
                put_multipath_config(conf);
 
-               if (newstate == PATH_DOWN || newstate == PATH_SHAKY || newstate == PATH_TIMEOUT) {
+               if (newstate != PATH_UP && newstate != PATH_GHOST) {
                        /*
                         * proactively fail path in the DM
                         */
@@ -1671,7 +1772,7 @@ check_path (struct vectors * vecs, struct path * pp, int ticks)
                }
                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;
                }
@@ -1694,7 +1795,7 @@ check_path (struct vectors * vecs, struct path * pp, int ticks)
                        /* 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;
                        }
@@ -1925,6 +2026,7 @@ configure (struct vectors * vecs, int start_waiters)
        vector mpvec;
        int i, ret;
        struct config *conf;
+       static int force_reload = FORCE_RELOAD_WEAK;
 
        if (!vecs->pathvec && !(vecs->pathvec = vector_alloc())) {
                condlog(0, "couldn't allocate path vec in configure");
@@ -1968,8 +2070,14 @@ configure (struct vectors * vecs, int start_waiters)
 
        /*
         * 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, CMD_NONE)) {
+       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;
        }
@@ -2069,6 +2177,10 @@ reconfigure (struct vectors * vecs)
                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;
@@ -2166,6 +2278,12 @@ sigusr2 (int sig)
 static void
 signal_init(void)
 {
+       sigset_t set;
+
+       sigemptyset(&set);
+       sigaddset(&set, SIGUSR2);
+       pthread_sigmask(SIG_SETMASK, &set, NULL);
+
        signal_set(SIGHUP, sighup);
        signal_set(SIGUSR1, sigusr1);
        signal_set(SIGUSR2, sigusr2);
@@ -2247,6 +2365,7 @@ child (void * param)
        int i;
 #ifdef USE_SYSTEMD
        unsigned long checkint;
+       int startup_done = 0;
 #endif
        int rc;
        int pid_fd = -1;
@@ -2395,10 +2514,6 @@ child (void * param)
        }
        pthread_attr_destroy(&misc_attr);
 
-#ifdef USE_SYSTEMD
-       sd_notify(0, "READY=1");
-#endif
-
        while (running_state != DAEMON_SHUTDOWN) {
                pthread_cleanup_push(config_cleanup, NULL);
                pthread_mutex_lock(&config_lock);
@@ -2420,6 +2535,12 @@ child (void * param)
                        }
                        lock_cleanup_pop(vecs->lock);
                        post_config_state(DAEMON_IDLE);
+#ifdef USE_SYSTEMD
+                       if (!startup_done) {
+                               sd_notify(0, "READY=1");
+                               startup_done = 1;
+                       }
+#endif
                }
        }
 
@@ -2579,7 +2700,7 @@ main (int argc, char *argv[])
        umask(umask(077) | 022);
 
        pthread_cond_init_mono(&config_cond);
-       
+
        udev = udev_new();
 
        while ((arg = getopt(argc, argv, ":dsv:k::Bn")) != EOF ) {
@@ -2608,6 +2729,7 @@ main (int argc, char *argv[])
                                conf->verbosity = verbosity;
                        uxsock_timeout = conf->uxsock_timeout;
                        uxclnt(optarg, uxsock_timeout + 100);
+                       free_config(conf);
                        exit(0);
                case 'B':
                        bindings_read_only = 1;
@@ -2642,6 +2764,7 @@ main (int argc, char *argv[])
                }
                c += snprintf(c, s + CMDSIZE - c, "\n");
                uxclnt(s, uxsock_timeout + 100);
+               free_config(conf);
                exit(0);
        }