2 * Copyright (c) 2004, 2005 Christophe Varoqui
3 * Copyright (c) 2005 Kiyoshi Ueda, NEC
4 * Copyright (c) 2005 Benjamin Marzinski, Redhat
5 * Copyright (c) 2005 Edward Goggin, EMC
9 #include <libdevmapper.h>
12 #include <sys/types.h>
16 #include <sys/resource.h>
18 #include <linux/oom.h>
22 #include <systemd/sd-daemon.h>
24 #include <semaphore.h>
33 static int use_watchdog;
49 #include "blacklist.h"
50 #include "structs_vec.h"
52 #include "devmapper.h"
55 #include "discovery.h"
59 #include "switchgroup.h"
61 #include "configure.h"
64 #include "pgpolicies.h"
68 #include "mpath_cmd.h"
69 #include "mpath_persist.h"
71 #include "prioritizers/alua_rtpg.h"
78 #include "cli_handlers.h"
83 #define FILE_NAME_SIZE 256
86 #define LOG_MSG(a, b) \
89 condlog(a, "%s: %s - path offline", pp->mpp->alias, pp->dev); \
91 condlog(a, "%s: %s - %s", pp->mpp->alias, pp->dev, b); \
94 struct mpath_event_param
97 struct multipath *mpp;
100 unsigned int mpath_mx_alloc_len;
104 int bindings_read_only;
106 enum daemon_status running_state = DAEMON_INIT;
108 pthread_mutex_t config_lock = PTHREAD_MUTEX_INITIALIZER;
109 pthread_cond_t config_cond = PTHREAD_COND_INITIALIZER;
112 * global copy of vecs for use in sig handlers
114 struct vectors * gvecs;
118 struct config *multipath_conf;
123 switch (running_state) {
128 case DAEMON_CONFIGURE:
134 case DAEMON_SHUTDOWN:
141 * I love you too, systemd ...
144 sd_notify_status(void)
146 switch (running_state) {
148 return "STATUS=init";
150 return "STATUS=startup";
151 case DAEMON_CONFIGURE:
152 return "STATUS=configure";
154 return "STATUS=idle";
156 return "STATUS=running";
157 case DAEMON_SHUTDOWN:
158 return "STATUS=shutdown";
163 static void config_cleanup(void *arg)
165 pthread_mutex_unlock(&config_lock);
168 void post_config_state(enum daemon_status state)
170 pthread_mutex_lock(&config_lock);
171 if (state != running_state) {
172 running_state = state;
173 pthread_cond_broadcast(&config_cond);
175 sd_notify(0, sd_notify_status());
178 pthread_mutex_unlock(&config_lock);
181 int set_config_state(enum daemon_status state)
185 pthread_cleanup_push(config_cleanup, NULL);
186 pthread_mutex_lock(&config_lock);
187 if (running_state != state) {
188 if (running_state != DAEMON_IDLE) {
191 clock_gettime(CLOCK_REALTIME, &ts);
193 rc = pthread_cond_timedwait(&config_cond,
197 running_state = state;
198 pthread_cond_broadcast(&config_cond);
200 sd_notify(0, sd_notify_status());
204 pthread_cleanup_pop(1);
208 struct config *get_multipath_config(void)
211 return rcu_dereference(multipath_conf);
214 void put_multipath_config(struct config *conf)
220 need_switch_pathgroup (struct multipath * mpp, int refresh)
222 struct pathgroup * pgp;
227 if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
231 * Refresh path priority values
234 vector_foreach_slot (mpp->pg, pgp, i) {
235 vector_foreach_slot (pgp->paths, pp, j) {
236 conf = get_multipath_config();
237 pathinfo(pp, conf, DI_PRIO);
238 put_multipath_config(conf);
243 if (!mpp->pg || VECTOR_SIZE(mpp->paths) == 0)
246 mpp->bestpg = select_path_group(mpp);
248 if (mpp->bestpg != mpp->nextpg)
255 switch_pathgroup (struct multipath * mpp)
257 mpp->stat_switchgroup++;
258 dm_switchgroup(mpp->alias, mpp->bestpg);
259 condlog(2, "%s: switch to path group #%i",
260 mpp->alias, mpp->bestpg);
264 coalesce_maps(struct vectors *vecs, vector nmpv)
266 struct multipath * ompp;
267 vector ompv = vecs->mpvec;
268 unsigned int i, reassign_maps;
271 conf = get_multipath_config();
272 reassign_maps = conf->reassign_maps;
273 put_multipath_config(conf);
274 vector_foreach_slot (ompv, ompp, i) {
275 condlog(3, "%s: coalesce map", ompp->alias);
276 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
278 * remove all current maps not allowed by the
279 * current configuration
281 if (dm_flush_map(ompp->alias)) {
282 condlog(0, "%s: unable to flush devmap",
285 * may be just because the device is open
287 if (setup_multipath(vecs, ompp) != 0) {
291 if (!vector_alloc_slot(nmpv))
294 vector_set_slot(nmpv, ompp);
296 vector_del_slot(ompv, i);
301 condlog(2, "%s devmap removed", ompp->alias);
303 } else if (reassign_maps) {
304 condlog(3, "%s: Reassign existing device-mapper"
305 " devices", ompp->alias);
306 dm_reassign(ompp->alias);
313 sync_map_state(struct multipath *mpp)
315 struct pathgroup *pgp;
322 vector_foreach_slot (mpp->pg, pgp, i){
323 vector_foreach_slot (pgp->paths, pp, j){
324 if (pp->state == PATH_UNCHECKED ||
325 pp->state == PATH_WILD ||
326 pp->state == PATH_DELAYED)
328 if ((pp->dmstate == PSTATE_FAILED ||
329 pp->dmstate == PSTATE_UNDEF) &&
330 (pp->state == PATH_UP || pp->state == PATH_GHOST))
331 dm_reinstate_path(mpp->alias, pp->dev_t);
332 else if ((pp->dmstate == PSTATE_ACTIVE ||
333 pp->dmstate == PSTATE_UNDEF) &&
334 (pp->state == PATH_DOWN ||
335 pp->state == PATH_SHAKY))
336 dm_fail_path(mpp->alias, pp->dev_t);
342 sync_maps_state(vector mpvec)
345 struct multipath *mpp;
347 vector_foreach_slot (mpvec, mpp, i)
352 flush_map(struct multipath * mpp, struct vectors * vecs, int nopaths)
357 r = dm_flush_map_nopaths(mpp->alias, mpp->deferred_remove);
359 r = dm_flush_map(mpp->alias);
361 * clear references to this map before flushing so we can ignore
362 * the spurious uevent we may generate with the dm_flush_map call below
366 * May not really be an error -- if the map was already flushed
367 * from the device mapper by dmsetup(8) for instance.
370 condlog(0, "%s: can't flush", mpp->alias);
372 condlog(2, "%s: devmap deferred remove", mpp->alias);
373 mpp->deferred_remove = DEFERRED_REMOVE_IN_PROGRESS;
379 condlog(2, "%s: map flushed", mpp->alias);
382 orphan_paths(vecs->pathvec, mpp);
383 remove_map_and_stop_waiter(mpp, vecs, 1);
389 update_map (struct multipath *mpp, struct vectors *vecs)
392 char params[PARAMS_SIZE] = {0};
395 condlog(4, "%s: updating new map", mpp->alias);
396 if (adopt_paths(vecs->pathvec, mpp)) {
397 condlog(0, "%s: failed to adopt paths for new map update",
402 verify_paths(mpp, vecs);
403 mpp->flush_on_last_del = FLUSH_UNDEF;
404 mpp->action = ACT_RELOAD;
406 if (setup_map(mpp, params, PARAMS_SIZE)) {
407 condlog(0, "%s: failed to setup new map in update", mpp->alias);
411 if (domap(mpp, params, 1) <= 0 && retries-- > 0) {
412 condlog(0, "%s: map_udate sleep", mpp->alias);
419 if (setup_multipath(vecs, mpp))
425 condlog(0, "%s: failed reload in new map update", mpp->alias);
430 uev_add_map (struct uevent * uev, struct vectors * vecs)
433 int major = -1, minor = -1, rc;
435 condlog(3, "%s: add map (uevent)", uev->kernel);
436 alias = uevent_get_dm_name(uev);
438 condlog(3, "%s: No DM_NAME in uevent", uev->kernel);
439 major = uevent_get_major(uev);
440 minor = uevent_get_minor(uev);
441 alias = dm_mapname(major, minor);
443 condlog(2, "%s: mapname not found for %d:%d",
444 uev->kernel, major, minor);
448 pthread_cleanup_push(cleanup_lock, &vecs->lock);
450 pthread_testcancel();
451 rc = ev_add_map(uev->kernel, alias, vecs);
452 lock_cleanup_pop(vecs->lock);
458 ev_add_map (char * dev, char * alias, struct vectors * vecs)
461 struct multipath * mpp;
463 int r = 1, delayed_reconfig, reassign_maps;
466 map_present = dm_map_present(alias);
468 if (map_present && !dm_is_mpath(alias)) {
469 condlog(4, "%s: not a multipath map", alias);
473 mpp = find_mp_by_alias(vecs->mpvec, alias);
476 if (mpp->wait_for_udev > 1) {
477 if (update_map(mpp, vecs))
478 /* setup multipathd removed the map */
481 conf = get_multipath_config();
482 delayed_reconfig = conf->delayed_reconfig;
483 reassign_maps = conf->reassign_maps;
484 put_multipath_config(conf);
485 if (mpp->wait_for_udev) {
486 mpp->wait_for_udev = 0;
487 if (delayed_reconfig &&
488 !need_to_delay_reconfig(vecs)) {
489 condlog(2, "reconfigure (delayed)");
490 set_config_state(DAEMON_CONFIGURE);
495 * Not really an error -- we generate our own uevent
496 * if we create a multipath mapped device as a result
500 condlog(3, "%s: Reassign existing device-mapper devices",
506 condlog(2, "%s: adding map", alias);
509 * now we can register the map
512 if ((mpp = add_map_without_path(vecs, alias))) {
514 condlog(2, "%s: devmap %s registered", alias, dev);
517 condlog(2, "%s: uev_add_map failed", dev);
521 r = get_refwwid(CMD_NONE, dev, DEV_DEVMAP, vecs->pathvec, &refwwid);
524 r = coalesce_paths(vecs, NULL, refwwid, 0, CMD_NONE);
529 condlog(2, "%s: devmap %s added", alias, dev);
531 condlog(2, "%s: uev_add_map %s blacklisted", alias, dev);
533 condlog(0, "%s: uev_add_map %s failed", alias, dev);
540 uev_remove_map (struct uevent * uev, struct vectors * vecs)
544 struct multipath *mpp;
546 condlog(2, "%s: remove map (uevent)", uev->kernel);
547 alias = uevent_get_dm_name(uev);
549 condlog(3, "%s: No DM_NAME in uevent, ignoring", uev->kernel);
552 minor = uevent_get_minor(uev);
554 pthread_cleanup_push(cleanup_lock, &vecs->lock);
556 pthread_testcancel();
557 mpp = find_mp_by_minor(vecs->mpvec, minor);
560 condlog(2, "%s: devmap not registered, can't remove",
564 if (strcmp(mpp->alias, alias)) {
565 condlog(2, "%s: minor number mismatch (map %d, event %d)",
566 mpp->alias, mpp->dmi->minor, minor);
570 orphan_paths(vecs->pathvec, mpp);
571 remove_map_and_stop_waiter(mpp, vecs, 1);
573 lock_cleanup_pop(vecs->lock);
578 /* Called from CLI handler */
580 ev_remove_map (char * devname, char * alias, int minor, struct vectors * vecs)
582 struct multipath * mpp;
584 mpp = find_mp_by_minor(vecs->mpvec, minor);
587 condlog(2, "%s: devmap not registered, can't remove",
591 if (strcmp(mpp->alias, alias)) {
592 condlog(2, "%s: minor number mismatch (map %d, event %d)",
593 mpp->alias, mpp->dmi->minor, minor);
596 return flush_map(mpp, vecs, 0);
600 uev_add_path (struct uevent *uev, struct vectors * vecs)
606 condlog(2, "%s: add path (uevent)", uev->kernel);
607 if (strstr(uev->kernel, "..") != NULL) {
609 * Don't allow relative device names in the pathvec
611 condlog(0, "%s: path name is invalid", uev->kernel);
615 pthread_cleanup_push(cleanup_lock, &vecs->lock);
617 pthread_testcancel();
618 pp = find_path_by_dev(vecs->pathvec, uev->kernel);
622 condlog(0, "%s: spurious uevent, path already in pathvec",
624 if (!pp->mpp && !strlen(pp->wwid)) {
625 condlog(3, "%s: reinitialize path", uev->kernel);
626 udev_device_unref(pp->udev);
627 pp->udev = udev_device_ref(uev->udev);
628 conf = get_multipath_config();
629 r = pathinfo(pp, conf,
630 DI_ALL | DI_BLACKLIST);
631 put_multipath_config(conf);
632 if (r == PATHINFO_OK)
633 ret = ev_add_path(pp, vecs);
634 else if (r == PATHINFO_SKIPPED) {
635 condlog(3, "%s: remove blacklisted path",
637 i = find_slot(vecs->pathvec, (void *)pp);
639 vector_del_slot(vecs->pathvec, i);
642 condlog(0, "%s: failed to reinitialize path",
648 lock_cleanup_pop(vecs->lock);
653 * get path vital state
655 conf = get_multipath_config();
656 ret = alloc_path_with_pathinfo(conf, uev->udev,
658 put_multipath_config(conf);
660 if (ret == PATHINFO_SKIPPED)
662 condlog(3, "%s: failed to get path info", uev->kernel);
665 pthread_cleanup_push(cleanup_lock, &vecs->lock);
667 pthread_testcancel();
668 ret = store_path(vecs->pathvec, pp);
670 conf = get_multipath_config();
671 pp->checkint = conf->checkint;
672 put_multipath_config(conf);
673 ret = ev_add_path(pp, vecs);
675 condlog(0, "%s: failed to store path info, "
681 lock_cleanup_pop(vecs->lock);
691 ev_add_path (struct path * pp, struct vectors * vecs)
693 struct multipath * mpp;
694 char params[PARAMS_SIZE] = {0};
696 int start_waiter = 0;
700 * need path UID to go any further
702 if (strlen(pp->wwid) == 0) {
703 condlog(0, "%s: failed to get path uid", pp->dev);
704 goto fail; /* leave path added to pathvec */
706 mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
707 if (mpp && mpp->wait_for_udev) {
708 mpp->wait_for_udev = 2;
709 orphan_path(pp, "waiting for create to complete");
716 if (pp->size && mpp->size != pp->size) {
717 condlog(0, "%s: failed to add new path %s, "
718 "device size mismatch",
719 mpp->alias, pp->dev);
720 int i = find_slot(vecs->pathvec, (void *)pp);
722 vector_del_slot(vecs->pathvec, i);
727 condlog(4,"%s: adopting all paths for path %s",
728 mpp->alias, pp->dev);
729 if (adopt_paths(vecs->pathvec, mpp))
730 goto fail; /* leave path added to pathvec */
732 verify_paths(mpp, vecs);
733 mpp->flush_on_last_del = FLUSH_UNDEF;
734 mpp->action = ACT_RELOAD;
736 if (!should_multipath(pp, vecs->pathvec)) {
737 orphan_path(pp, "only one path");
740 condlog(4,"%s: creating new map", pp->dev);
741 if ((mpp = add_map_with_path(vecs, pp, 1))) {
742 mpp->action = ACT_CREATE;
744 * We don't depend on ACT_CREATE, as domap will
745 * set it to ACT_NOTHING when complete.
750 goto fail; /* leave path added to pathvec */
753 /* persistent reservation check*/
754 mpath_pr_event_handle(pp);
757 * push the map to the device-mapper
759 if (setup_map(mpp, params, PARAMS_SIZE)) {
760 condlog(0, "%s: failed to setup map for addition of new "
761 "path %s", mpp->alias, pp->dev);
765 * reload the map for the multipath mapped device
768 ret = domap(mpp, params, 1);
770 if (ret < 0 && retries-- > 0) {
771 condlog(0, "%s: retry domap for addition of new "
772 "path %s", mpp->alias, pp->dev);
776 condlog(0, "%s: failed in domap for addition of new "
777 "path %s", mpp->alias, pp->dev);
779 * deal with asynchronous uevents :((
781 if (mpp->action == ACT_RELOAD && retries-- > 0) {
782 condlog(0, "%s: ev_add_path sleep", mpp->alias);
784 update_mpp_paths(mpp, vecs->pathvec);
787 else if (mpp->action == ACT_RELOAD)
788 condlog(0, "%s: giving up reload", mpp->alias);
795 * update our state from kernel regardless of create or reload
797 if (setup_multipath(vecs, mpp))
798 goto fail; /* if setup_multipath fails, it removes the map */
802 if ((mpp->action == ACT_CREATE ||
803 (mpp->action == ACT_NOTHING && start_waiter && !mpp->waiter)) &&
804 start_waiter_thread(mpp, vecs))
808 condlog(2, "%s [%s]: path added to devmap %s",
809 pp->dev, pp->dev_t, mpp->alias);
815 remove_map(mpp, vecs, 1);
817 orphan_path(pp, "failed to add path");
822 uev_remove_path (struct uevent *uev, struct vectors * vecs)
827 condlog(2, "%s: remove path (uevent)", uev->kernel);
828 pthread_cleanup_push(cleanup_lock, &vecs->lock);
830 pthread_testcancel();
831 pp = find_path_by_dev(vecs->pathvec, uev->kernel);
833 ret = ev_remove_path(pp, vecs);
834 lock_cleanup_pop(vecs->lock);
836 /* Not an error; path might have been purged earlier */
837 condlog(0, "%s: path already removed", uev->kernel);
844 ev_remove_path (struct path *pp, struct vectors * vecs)
846 struct multipath * mpp;
848 char params[PARAMS_SIZE] = {0};
851 * avoid referring to the map of an orphaned path
853 if ((mpp = pp->mpp)) {
855 * transform the mp->pg vector of vectors of paths
856 * into a mp->params string to feed the device-mapper
858 if (update_mpp_paths(mpp, vecs->pathvec)) {
859 condlog(0, "%s: failed to update paths",
863 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
864 vector_del_slot(mpp->paths, i);
867 * remove the map IFF removing the last path
869 if (VECTOR_SIZE(mpp->paths) == 0) {
870 char alias[WWID_SIZE];
873 * flush_map will fail if the device is open
875 strncpy(alias, mpp->alias, WWID_SIZE);
876 if (mpp->flush_on_last_del == FLUSH_ENABLED) {
877 condlog(2, "%s Last path deleted, disabling queueing", mpp->alias);
879 mpp->no_path_retry = NO_PATH_RETRY_FAIL;
880 mpp->flush_on_last_del = FLUSH_IN_PROGRESS;
881 dm_queue_if_no_path(mpp->alias, 0);
883 if (!flush_map(mpp, vecs, 1)) {
884 condlog(2, "%s: removed map after"
885 " removing all paths",
891 * Not an error, continue
895 if (setup_map(mpp, params, PARAMS_SIZE)) {
896 condlog(0, "%s: failed to setup map for"
897 " removal of path %s", mpp->alias, pp->dev);
901 if (mpp->wait_for_udev) {
902 mpp->wait_for_udev = 2;
909 mpp->action = ACT_RELOAD;
910 if (domap(mpp, params, 1) <= 0) {
911 condlog(0, "%s: failed in domap for "
912 "removal of path %s",
913 mpp->alias, pp->dev);
917 * update our state from kernel
919 if (setup_multipath(vecs, mpp))
923 condlog(2, "%s [%s]: path removed from map %s",
924 pp->dev, pp->dev_t, mpp->alias);
929 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
930 vector_del_slot(vecs->pathvec, i);
937 remove_map_and_stop_waiter(mpp, vecs, 1);
942 uev_update_path (struct uevent *uev, struct vectors * vecs)
946 ro = uevent_get_disk_ro(uev);
950 struct multipath *mpp = NULL;
952 condlog(2, "%s: update path write_protect to '%d' (uevent)",
954 pthread_cleanup_push(cleanup_lock, &vecs->lock);
956 pthread_testcancel();
958 * pthread_mutex_lock() and pthread_mutex_unlock()
959 * need to be at the same indentation level, hence
960 * this slightly convoluted codepath.
962 pp = find_path_by_dev(vecs->pathvec, uev->kernel);
964 if (pp->initialized == INIT_REQUESTED_UDEV) {
968 if (mpp && mpp->wait_for_udev) {
969 mpp->wait_for_udev = 2;
975 retval = reload_map(vecs, mpp, 0, 1);
977 condlog(2, "%s: map %s reloaded (retval %d)",
978 uev->kernel, mpp->alias, retval);
981 lock_cleanup_pop(vecs->lock);
983 condlog(0, "%s: spurious uevent, path not found",
988 return uev_add_path(uev, vecs);
995 map_discovery (struct vectors * vecs)
997 struct multipath * mpp;
1000 if (dm_get_maps(vecs->mpvec))
1003 vector_foreach_slot (vecs->mpvec, mpp, i)
1004 if (setup_multipath(vecs, mpp))
1011 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
1013 struct vectors * vecs;
1018 vecs = (struct vectors *)trigger_data;
1020 r = parse_cmd(str, reply, len, vecs, uxsock_timeout / 1000);
1024 *reply = STRDUP("timeout\n");
1026 *reply = STRDUP("fail\n");
1027 *len = strlen(*reply) + 1;
1030 else if (!r && *len == 0) {
1031 *reply = STRDUP("ok\n");
1032 *len = strlen(*reply) + 1;
1035 /* else if (r < 0) leave *reply alone */
1041 uev_discard(char * devpath)
1047 * keep only block devices, discard partitions
1049 tmp = strstr(devpath, "/block/");
1051 condlog(4, "no /block/ in '%s'", devpath);
1054 if (sscanf(tmp, "/block/%10s", a) != 1 ||
1055 sscanf(tmp, "/block/%10[^/]/%10s", a, b) == 2) {
1056 condlog(4, "discard event on %s", devpath);
1063 uev_trigger (struct uevent * uev, void * trigger_data)
1066 struct vectors * vecs;
1067 struct config *conf;
1069 vecs = (struct vectors *)trigger_data;
1071 if (uev_discard(uev->devpath))
1074 pthread_cleanup_push(config_cleanup, NULL);
1075 pthread_mutex_lock(&config_lock);
1076 if (running_state != DAEMON_IDLE &&
1077 running_state != DAEMON_RUNNING)
1078 pthread_cond_wait(&config_cond, &config_lock);
1079 pthread_cleanup_pop(1);
1081 if (running_state == DAEMON_SHUTDOWN)
1086 * Add events are ignored here as the tables
1087 * are not fully initialised then.
1089 if (!strncmp(uev->kernel, "dm-", 3)) {
1090 if (!strncmp(uev->action, "change", 6)) {
1091 r = uev_add_map(uev, vecs);
1094 if (!strncmp(uev->action, "remove", 6)) {
1095 r = uev_remove_map(uev, vecs);
1102 * path add/remove event
1104 conf = get_multipath_config();
1105 if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
1107 put_multipath_config(conf);
1110 put_multipath_config(conf);
1112 if (!strncmp(uev->action, "add", 3)) {
1113 r = uev_add_path(uev, vecs);
1116 if (!strncmp(uev->action, "remove", 6)) {
1117 r = uev_remove_path(uev, vecs);
1120 if (!strncmp(uev->action, "change", 6)) {
1121 r = uev_update_path(uev, vecs);
1129 static void *rcu_unregister(void *param)
1131 rcu_unregister_thread();
1136 ueventloop (void * ap)
1138 struct udev *udev = ap;
1140 pthread_cleanup_push(rcu_unregister, NULL);
1141 rcu_register_thread();
1142 if (uevent_listen(udev))
1143 condlog(0, "error starting uevent listener");
1144 pthread_cleanup_pop(1);
1149 uevqloop (void * ap)
1151 pthread_cleanup_push(rcu_unregister, NULL);
1152 rcu_register_thread();
1153 if (uevent_dispatch(&uev_trigger, ap))
1154 condlog(0, "error starting uevent dispatcher");
1155 pthread_cleanup_pop(1);
1159 uxlsnrloop (void * ap)
1162 condlog(1, "Failed to init uxsock listener");
1165 pthread_cleanup_push(rcu_unregister, NULL);
1166 rcu_register_thread();
1167 set_handler_callback(LIST+PATHS, cli_list_paths);
1168 set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt);
1169 set_handler_callback(LIST+PATHS+RAW+FMT, cli_list_paths_raw);
1170 set_handler_callback(LIST+PATH, cli_list_path);
1171 set_handler_callback(LIST+MAPS, cli_list_maps);
1172 set_unlocked_handler_callback(LIST+STATUS, cli_list_status);
1173 set_unlocked_handler_callback(LIST+DAEMON, cli_list_daemon);
1174 set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
1175 set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
1176 set_handler_callback(LIST+MAPS+FMT, cli_list_maps_fmt);
1177 set_handler_callback(LIST+MAPS+RAW+FMT, cli_list_maps_raw);
1178 set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
1179 set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
1180 set_handler_callback(LIST+MAPS+JSON, cli_list_maps_json);
1181 set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
1182 set_handler_callback(LIST+MAP+FMT, cli_list_map_fmt);
1183 set_handler_callback(LIST+MAP+RAW+FMT, cli_list_map_fmt);
1184 set_handler_callback(LIST+MAP+JSON, cli_list_map_json);
1185 set_handler_callback(LIST+CONFIG, cli_list_config);
1186 set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
1187 set_handler_callback(LIST+DEVICES, cli_list_devices);
1188 set_handler_callback(LIST+WILDCARDS, cli_list_wildcards);
1189 set_handler_callback(ADD+PATH, cli_add_path);
1190 set_handler_callback(DEL+PATH, cli_del_path);
1191 set_handler_callback(ADD+MAP, cli_add_map);
1192 set_handler_callback(DEL+MAP, cli_del_map);
1193 set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
1194 set_unlocked_handler_callback(RECONFIGURE, cli_reconfigure);
1195 set_handler_callback(SUSPEND+MAP, cli_suspend);
1196 set_handler_callback(RESUME+MAP, cli_resume);
1197 set_handler_callback(RESIZE+MAP, cli_resize);
1198 set_handler_callback(RELOAD+MAP, cli_reload);
1199 set_handler_callback(RESET+MAP, cli_reassign);
1200 set_handler_callback(REINSTATE+PATH, cli_reinstate);
1201 set_handler_callback(FAIL+PATH, cli_fail);
1202 set_handler_callback(DISABLEQ+MAP, cli_disable_queueing);
1203 set_handler_callback(RESTOREQ+MAP, cli_restore_queueing);
1204 set_handler_callback(DISABLEQ+MAPS, cli_disable_all_queueing);
1205 set_handler_callback(RESTOREQ+MAPS, cli_restore_all_queueing);
1206 set_unlocked_handler_callback(QUIT, cli_quit);
1207 set_unlocked_handler_callback(SHUTDOWN, cli_shutdown);
1208 set_handler_callback(GETPRSTATUS+MAP, cli_getprstatus);
1209 set_handler_callback(SETPRSTATUS+MAP, cli_setprstatus);
1210 set_handler_callback(UNSETPRSTATUS+MAP, cli_unsetprstatus);
1211 set_handler_callback(FORCEQ+DAEMON, cli_force_no_daemon_q);
1212 set_handler_callback(RESTOREQ+DAEMON, cli_restore_no_daemon_q);
1215 uxsock_listen(&uxsock_trigger, ap);
1216 pthread_cleanup_pop(1);
1223 post_config_state(DAEMON_SHUTDOWN);
1227 fail_path (struct path * pp, int del_active)
1232 condlog(2, "checker failed path %s in map %s",
1233 pp->dev_t, pp->mpp->alias);
1235 dm_fail_path(pp->mpp->alias, pp->dev_t);
1237 update_queue_mode_del_path(pp->mpp);
1241 * caller must have locked the path list before calling that function
1244 reinstate_path (struct path * pp, int add_active)
1251 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t)) {
1252 condlog(0, "%s: reinstate failed", pp->dev_t);
1255 condlog(2, "%s: reinstated", pp->dev_t);
1257 update_queue_mode_add_path(pp->mpp);
1263 enable_group(struct path * pp)
1265 struct pathgroup * pgp;
1268 * if path is added through uev_add_path, pgindex can be unset.
1269 * next update_strings() will set it, upon map reload event.
1271 * we can safely return here, because upon map reload, all
1272 * PG will be enabled.
1274 if (!pp->mpp->pg || !pp->pgindex)
1277 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1279 if (pgp->status == PGSTATE_DISABLED) {
1280 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1281 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1286 mpvec_garbage_collector (struct vectors * vecs)
1288 struct multipath * mpp;
1294 vector_foreach_slot (vecs->mpvec, mpp, i) {
1295 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1296 condlog(2, "%s: remove dead map", mpp->alias);
1297 remove_map_and_stop_waiter(mpp, vecs, 1);
1303 /* This is called after a path has started working again. It the multipath
1304 * device for this path uses the followover failback type, and this is the
1305 * best pathgroup, and this is the first path in the pathgroup to come back
1306 * up, then switch to this pathgroup */
1308 followover_should_failback(struct path * pp)
1310 struct pathgroup * pgp;
1314 if (pp->mpp->pgfailback != -FAILBACK_FOLLOWOVER ||
1315 !pp->mpp->pg || !pp->pgindex ||
1316 pp->pgindex != pp->mpp->bestpg)
1319 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1320 vector_foreach_slot(pgp->paths, pp1, i) {
1323 if (pp1->chkrstate != PATH_DOWN && pp1->chkrstate != PATH_SHAKY)
1330 missing_uev_wait_tick(struct vectors *vecs)
1332 struct multipath * mpp;
1334 int timed_out = 0, delayed_reconfig;
1335 struct config *conf;
1337 vector_foreach_slot (vecs->mpvec, mpp, i) {
1338 if (mpp->wait_for_udev && --mpp->uev_wait_tick <= 0) {
1340 condlog(0, "%s: timeout waiting on creation uevent. enabling reloads", mpp->alias);
1341 if (mpp->wait_for_udev > 1 && update_map(mpp, vecs)) {
1342 /* update_map removed map */
1346 mpp->wait_for_udev = 0;
1350 conf = get_multipath_config();
1351 delayed_reconfig = conf->delayed_reconfig;
1352 put_multipath_config(conf);
1353 if (timed_out && delayed_reconfig &&
1354 !need_to_delay_reconfig(vecs)) {
1355 condlog(2, "reconfigure (delayed)");
1356 set_config_state(DAEMON_CONFIGURE);
1361 defered_failback_tick (vector mpvec)
1363 struct multipath * mpp;
1366 vector_foreach_slot (mpvec, mpp, i) {
1368 * defered failback getting sooner
1370 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1371 mpp->failback_tick--;
1373 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1374 switch_pathgroup(mpp);
1380 retry_count_tick(vector mpvec)
1382 struct multipath *mpp;
1385 vector_foreach_slot (mpvec, mpp, i) {
1386 if (mpp->retry_tick > 0) {
1387 mpp->stat_total_queueing_time++;
1388 condlog(4, "%s: Retrying.. No active path", mpp->alias);
1389 if(--mpp->retry_tick == 0) {
1390 dm_queue_if_no_path(mpp->alias, 0);
1391 condlog(2, "%s: Disable queueing", mpp->alias);
1397 int update_prio(struct path *pp, int refresh_all)
1401 struct pathgroup * pgp;
1402 int i, j, changed = 0;
1403 struct config *conf;
1406 vector_foreach_slot (pp->mpp->pg, pgp, i) {
1407 vector_foreach_slot (pgp->paths, pp1, j) {
1408 oldpriority = pp1->priority;
1409 conf = get_multipath_config();
1410 pathinfo(pp1, conf, DI_PRIO);
1411 put_multipath_config(conf);
1412 if (pp1->priority != oldpriority)
1418 oldpriority = pp->priority;
1419 conf = get_multipath_config();
1420 pathinfo(pp, conf, DI_PRIO);
1421 put_multipath_config(conf);
1423 if (pp->priority == oldpriority)
1428 int update_path_groups(struct multipath *mpp, struct vectors *vecs, int refresh)
1430 if (reload_map(vecs, mpp, refresh, 1))
1434 if (setup_multipath(vecs, mpp) != 0)
1436 sync_map_state(mpp);
1442 * Returns '1' if the path has been checked, '-1' if it was blacklisted
1446 check_path (struct vectors * vecs, struct path * pp, int ticks)
1449 int new_path_up = 0;
1450 int chkr_new_path_up = 0;
1452 int disable_reinstate = 0;
1453 int oldchkrstate = pp->chkrstate;
1454 int retrigger_tries, checkint;
1455 struct config *conf;
1458 if ((pp->initialized == INIT_OK ||
1459 pp->initialized == INIT_REQUESTED_UDEV) && !pp->mpp)
1463 pp->tick -= (pp->tick > ticks) ? ticks : pp->tick;
1465 return 0; /* don't check this path yet */
1467 conf = get_multipath_config();
1468 retrigger_tries = conf->retrigger_tries;
1469 checkint = conf->checkint;
1470 put_multipath_config(conf);
1471 if (!pp->mpp && pp->initialized == INIT_MISSING_UDEV &&
1472 pp->retriggers < retrigger_tries) {
1473 condlog(2, "%s: triggering change event to reinitialize",
1475 pp->initialized = INIT_REQUESTED_UDEV;
1477 sysfs_attr_set_value(pp->udev, "uevent", "change",
1483 * provision a next check soonest,
1484 * in case we exit abnormaly from here
1486 pp->tick = checkint;
1488 newstate = path_offline(pp);
1490 * Wait for uevent for removed paths;
1491 * some LLDDs like zfcp keep paths unavailable
1492 * without sending uevents.
1494 if (newstate == PATH_REMOVED)
1495 newstate = PATH_DOWN;
1497 if (newstate == PATH_UP) {
1498 conf = get_multipath_config();
1499 newstate = get_state(pp, conf, 1);
1500 put_multipath_config(conf);
1502 checker_clear_message(&pp->checker);
1504 if (newstate == PATH_WILD || newstate == PATH_UNCHECKED) {
1505 condlog(2, "%s: unusable path", pp->dev);
1506 conf = get_multipath_config();
1507 pathinfo(pp, conf, 0);
1508 put_multipath_config(conf);
1512 if (!strlen(pp->wwid) && pp->initialized != INIT_MISSING_UDEV &&
1513 (newstate == PATH_UP || newstate == PATH_GHOST)) {
1514 condlog(2, "%s: add missing path", pp->dev);
1515 conf = get_multipath_config();
1516 ret = pathinfo(pp, conf, DI_ALL | DI_BLACKLIST);
1517 if (ret == PATHINFO_OK) {
1518 ev_add_path(pp, vecs);
1520 } else if (ret == PATHINFO_SKIPPED) {
1521 put_multipath_config(conf);
1524 put_multipath_config(conf);
1529 * Async IO in flight. Keep the previous path state
1530 * and reschedule as soon as possible
1532 if (newstate == PATH_PENDING) {
1537 * Synchronize with kernel state
1539 if (update_multipath_strings(pp->mpp, vecs->pathvec, 1)) {
1540 condlog(1, "%s: Could not synchronize with kernel state",
1542 pp->dmstate = PSTATE_UNDEF;
1544 /* if update_multipath_strings orphaned the path, quit early */
1548 if ((newstate == PATH_UP || newstate == PATH_GHOST) &&
1549 pp->wait_checks > 0) {
1550 if (pp->mpp && pp->mpp->nr_active > 0) {
1551 pp->state = PATH_DELAYED;
1555 pp->wait_checks = 0;
1559 * don't reinstate failed path, if its in stand-by
1560 * and if target supports only implicit tpgs mode.
1561 * this will prevent unnecessary i/o by dm on stand-by
1562 * paths if there are no other active paths in map.
1564 disable_reinstate = (newstate == PATH_GHOST &&
1565 pp->mpp->nr_active == 0 &&
1566 pp->tpgs == TPGS_IMPLICIT) ? 1 : 0;
1568 pp->chkrstate = newstate;
1569 if (newstate != pp->state) {
1570 int oldstate = pp->state;
1571 pp->state = newstate;
1573 if (strlen(checker_message(&pp->checker)))
1574 LOG_MSG(1, checker_message(&pp->checker));
1577 * upon state change, reset the checkint
1578 * to the shortest delay
1580 conf = get_multipath_config();
1581 pp->checkint = conf->checkint;
1582 put_multipath_config(conf);
1584 if (newstate == PATH_DOWN || newstate == PATH_SHAKY) {
1586 * proactively fail path in the DM
1588 if (oldstate == PATH_UP ||
1589 oldstate == PATH_GHOST) {
1591 if (pp->mpp->delay_wait_checks > 0 &&
1592 pp->watch_checks > 0) {
1593 pp->wait_checks = pp->mpp->delay_wait_checks;
1594 pp->watch_checks = 0;
1600 * cancel scheduled failback
1602 pp->mpp->failback_tick = 0;
1604 pp->mpp->stat_path_failures++;
1608 if(newstate == PATH_UP || newstate == PATH_GHOST){
1609 if ( pp->mpp && pp->mpp->prflag ){
1611 * Check Persistent Reservation.
1613 condlog(2, "%s: checking persistent reservation "
1614 "registration", pp->dev);
1615 mpath_pr_event_handle(pp);
1620 * reinstate this path
1622 if (oldstate != PATH_UP &&
1623 oldstate != PATH_GHOST) {
1624 if (pp->mpp->delay_watch_checks > 0)
1625 pp->watch_checks = pp->mpp->delay_watch_checks;
1628 if (pp->watch_checks > 0)
1632 if (!disable_reinstate && reinstate_path(pp, add_active)) {
1633 condlog(3, "%s: reload map", pp->dev);
1634 ev_add_path(pp, vecs);
1640 if (oldchkrstate != PATH_UP && oldchkrstate != PATH_GHOST)
1641 chkr_new_path_up = 1;
1644 * if at least one path is up in a group, and
1645 * the group is disabled, re-enable it
1647 if (newstate == PATH_UP)
1650 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1651 if ((pp->dmstate == PSTATE_FAILED ||
1652 pp->dmstate == PSTATE_UNDEF) &&
1653 !disable_reinstate) {
1654 /* Clear IO errors */
1655 if (reinstate_path(pp, 0)) {
1656 condlog(3, "%s: reload map", pp->dev);
1657 ev_add_path(pp, vecs);
1662 unsigned int max_checkint;
1663 LOG_MSG(4, checker_message(&pp->checker));
1664 conf = get_multipath_config();
1665 max_checkint = conf->max_checkint;
1666 put_multipath_config(conf);
1667 if (pp->checkint != max_checkint) {
1669 * double the next check delay.
1670 * max at conf->max_checkint
1672 if (pp->checkint < (max_checkint / 2))
1673 pp->checkint = 2 * pp->checkint;
1675 pp->checkint = max_checkint;
1677 condlog(4, "%s: delay next check %is",
1678 pp->dev_t, pp->checkint);
1680 if (pp->watch_checks > 0)
1682 pp->tick = pp->checkint;
1685 else if (newstate == PATH_DOWN &&
1686 strlen(checker_message(&pp->checker))) {
1687 int log_checker_err;
1689 conf = get_multipath_config();
1690 log_checker_err = conf->log_checker_err;
1691 put_multipath_config(conf);
1692 if (log_checker_err == LOG_CHKR_ERR_ONCE)
1693 LOG_MSG(3, checker_message(&pp->checker));
1695 LOG_MSG(2, checker_message(&pp->checker));
1698 pp->state = newstate;
1701 if (pp->mpp->wait_for_udev)
1704 * path prio refreshing
1706 condlog(4, "path prio refresh");
1708 if (update_prio(pp, new_path_up) &&
1709 (pp->mpp->pgpolicyfn == (pgpolicyfn *)group_by_prio) &&
1710 pp->mpp->pgfailback == -FAILBACK_IMMEDIATE)
1711 update_path_groups(pp->mpp, vecs, !new_path_up);
1712 else if (need_switch_pathgroup(pp->mpp, 0)) {
1713 if (pp->mpp->pgfailback > 0 &&
1714 (new_path_up || pp->mpp->failback_tick <= 0))
1715 pp->mpp->failback_tick =
1716 pp->mpp->pgfailback + 1;
1717 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE ||
1718 (chkr_new_path_up && followover_should_failback(pp)))
1719 switch_pathgroup(pp->mpp);
1725 checkerloop (void *ap)
1727 struct vectors *vecs;
1731 struct itimerval timer_tick_it;
1732 struct timeval last_time;
1733 struct config *conf;
1735 pthread_cleanup_push(rcu_unregister, NULL);
1736 rcu_register_thread();
1737 mlockall(MCL_CURRENT | MCL_FUTURE);
1738 vecs = (struct vectors *)ap;
1739 condlog(2, "path checkers start up");
1742 * init the path check interval
1744 vector_foreach_slot (vecs->pathvec, pp, i) {
1745 conf = get_multipath_config();
1746 pp->checkint = conf->checkint;
1747 put_multipath_config(conf);
1750 /* Tweak start time for initial path check */
1751 if (gettimeofday(&last_time, NULL) != 0)
1752 last_time.tv_sec = 0;
1754 last_time.tv_sec -= 1;
1757 struct timeval diff_time, start_time, end_time;
1758 int num_paths = 0, ticks = 0, signo, strict_timing, rc = 0;
1761 if (gettimeofday(&start_time, NULL) != 0)
1762 start_time.tv_sec = 0;
1763 if (start_time.tv_sec && last_time.tv_sec) {
1764 timersub(&start_time, &last_time, &diff_time);
1765 condlog(4, "tick (%lu.%06lu secs)",
1766 diff_time.tv_sec, diff_time.tv_usec);
1767 last_time.tv_sec = start_time.tv_sec;
1768 last_time.tv_usec = start_time.tv_usec;
1769 ticks = diff_time.tv_sec;
1772 condlog(4, "tick (%d ticks)", ticks);
1776 sd_notify(0, "WATCHDOG=1");
1778 rc = set_config_state(DAEMON_RUNNING);
1779 if (rc == ETIMEDOUT) {
1780 condlog(4, "timeout waiting for DAEMON_IDLE");
1783 if (vecs->pathvec) {
1784 pthread_cleanup_push(cleanup_lock, &vecs->lock);
1786 pthread_testcancel();
1787 vector_foreach_slot (vecs->pathvec, pp, i) {
1788 rc = check_path(vecs, pp, ticks);
1790 vector_del_slot(vecs->pathvec, i);
1796 lock_cleanup_pop(vecs->lock);
1799 pthread_cleanup_push(cleanup_lock, &vecs->lock);
1801 pthread_testcancel();
1802 defered_failback_tick(vecs->mpvec);
1803 retry_count_tick(vecs->mpvec);
1804 missing_uev_wait_tick(vecs);
1805 lock_cleanup_pop(vecs->lock);
1810 pthread_cleanup_push(cleanup_lock, &vecs->lock);
1812 pthread_testcancel();
1813 condlog(4, "map garbage collection");
1814 mpvec_garbage_collector(vecs);
1816 lock_cleanup_pop(vecs->lock);
1819 diff_time.tv_usec = 0;
1820 if (start_time.tv_sec &&
1821 gettimeofday(&end_time, NULL) == 0) {
1822 timersub(&end_time, &start_time, &diff_time);
1824 unsigned int max_checkint;
1826 condlog(3, "checked %d path%s in %lu.%06lu secs",
1827 num_paths, num_paths > 1 ? "s" : "",
1828 diff_time.tv_sec, diff_time.tv_usec);
1829 conf = get_multipath_config();
1830 max_checkint = conf->max_checkint;
1831 put_multipath_config(conf);
1832 if (diff_time.tv_sec > max_checkint)
1833 condlog(1, "path checkers took longer "
1834 "than %lu seconds, consider "
1835 "increasing max_polling_interval",
1840 post_config_state(DAEMON_IDLE);
1841 conf = get_multipath_config();
1842 strict_timing = conf->strict_timing;
1843 put_multipath_config(conf);
1847 timer_tick_it.it_interval.tv_sec = 0;
1848 timer_tick_it.it_interval.tv_usec = 0;
1849 if (diff_time.tv_usec) {
1850 timer_tick_it.it_value.tv_sec = 0;
1851 timer_tick_it.it_value.tv_usec =
1852 (unsigned long)1000000 - diff_time.tv_usec;
1854 timer_tick_it.it_value.tv_sec = 1;
1855 timer_tick_it.it_value.tv_usec = 0;
1857 setitimer(ITIMER_REAL, &timer_tick_it, NULL);
1860 sigaddset(&mask, SIGALRM);
1861 condlog(3, "waiting for %lu.%06lu secs",
1862 timer_tick_it.it_value.tv_sec,
1863 timer_tick_it.it_value.tv_usec);
1864 if (sigwait(&mask, &signo) != 0) {
1865 condlog(3, "sigwait failed with error %d",
1867 conf = get_multipath_config();
1868 conf->strict_timing = 0;
1869 put_multipath_config(conf);
1874 pthread_cleanup_pop(1);
1879 configure (struct vectors * vecs, int start_waiters)
1881 struct multipath * mpp;
1885 struct config *conf;
1887 if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
1890 if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
1893 if (!(mpvec = vector_alloc()))
1897 * probe for current path (from sysfs) and map (from dm) sets
1899 ret = path_discovery(vecs->pathvec, DI_ALL);
1903 vector_foreach_slot (vecs->pathvec, pp, i){
1904 conf = get_multipath_config();
1905 if (filter_path(conf, pp) > 0){
1906 vector_del_slot(vecs->pathvec, i);
1911 pp->checkint = conf->checkint;
1912 put_multipath_config(conf);
1914 if (map_discovery(vecs))
1918 * create new set of maps & push changed ones into dm
1920 if (coalesce_paths(vecs, mpvec, NULL, 1, CMD_NONE))
1924 * may need to remove some maps which are no longer relevant
1925 * e.g., due to blacklist changes in conf file
1927 if (coalesce_maps(vecs, mpvec))
1932 sync_maps_state(mpvec);
1933 vector_foreach_slot(mpvec, mpp, i){
1934 remember_wwid(mpp->wwid);
1939 * purge dm of old maps
1944 * save new set of maps formed by considering current path state
1946 vector_free(vecs->mpvec);
1947 vecs->mpvec = mpvec;
1950 * start dm event waiter threads for these new maps
1952 vector_foreach_slot(vecs->mpvec, mpp, i) {
1953 if (setup_multipath(vecs, mpp))
1956 if (start_waiter_thread(mpp, vecs))
1963 need_to_delay_reconfig(struct vectors * vecs)
1965 struct multipath *mpp;
1968 if (!VECTOR_SIZE(vecs->mpvec))
1971 vector_foreach_slot(vecs->mpvec, mpp, i) {
1972 if (mpp->wait_for_udev)
1978 void rcu_free_config(struct rcu_head *head)
1980 struct config *conf = container_of(head, struct config, rcu);
1986 reconfigure (struct vectors * vecs)
1988 struct config * old, *conf;
1990 conf = load_config(DEFAULT_CONFIGFILE);
1995 * free old map and path vectors ... they use old conf state
1997 if (VECTOR_SIZE(vecs->mpvec))
1998 remove_maps_and_stop_waiters(vecs);
2000 free_pathvec(vecs->pathvec, FREE_PATHS);
2001 vecs->pathvec = NULL;
2003 /* Re-read any timezone changes */
2006 dm_drv_version(conf->version, TGT_MPATH);
2008 conf->verbosity = verbosity;
2009 if (bindings_read_only)
2010 conf->bindings_read_only = bindings_read_only;
2011 if (ignore_new_devs)
2012 conf->ignore_new_devs = ignore_new_devs;
2013 uxsock_timeout = conf->uxsock_timeout;
2015 old = rcu_dereference(multipath_conf);
2016 rcu_assign_pointer(multipath_conf, conf);
2017 call_rcu(&old->rcu, rcu_free_config);
2025 static struct vectors *
2028 struct vectors * vecs;
2030 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
2036 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
2038 if (!vecs->lock.mutex)
2041 pthread_mutex_init(vecs->lock.mutex, NULL);
2042 vecs->lock.depth = 0;
2048 condlog(0, "failed to init paths");
2053 signal_set(int signo, void (*func) (int))
2056 struct sigaction sig;
2057 struct sigaction osig;
2059 sig.sa_handler = func;
2060 sigemptyset(&sig.sa_mask);
2063 r = sigaction(signo, &sig, &osig);
2068 return (osig.sa_handler);
2072 handle_signals(void)
2075 condlog(2, "reconfigure (signal)");
2076 set_config_state(DAEMON_CONFIGURE);
2078 if (log_reset_sig) {
2079 condlog(2, "reset log (signal)");
2080 pthread_mutex_lock(&logq_lock);
2081 log_reset("multipathd");
2082 pthread_mutex_unlock(&logq_lock);
2109 condlog(3, "SIGUSR2 received");
2118 sigaddset(&set, SIGHUP);
2119 sigaddset(&set, SIGUSR1);
2120 sigaddset(&set, SIGUSR2);
2121 sigaddset(&set, SIGALRM);
2122 pthread_sigmask(SIG_BLOCK, &set, NULL);
2124 signal_set(SIGHUP, sighup);
2125 signal_set(SIGUSR1, sigusr1);
2126 signal_set(SIGUSR2, sigusr2);
2127 signal_set(SIGINT, sigend);
2128 signal_set(SIGTERM, sigend);
2129 signal(SIGPIPE, SIG_IGN);
2136 static struct sched_param sched_param = {
2137 .sched_priority = 99
2140 res = sched_setscheduler (0, SCHED_RR, &sched_param);
2143 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
2150 #ifdef OOM_SCORE_ADJ_MIN
2152 char *file = "/proc/self/oom_score_adj";
2153 int score = OOM_SCORE_ADJ_MIN;
2156 char *file = "/proc/self/oom_adj";
2157 int score = OOM_ADJUST_MIN;
2163 envp = getenv("OOMScoreAdjust");
2165 condlog(3, "Using systemd provided OOMScoreAdjust");
2169 if (stat(file, &st) == 0){
2170 fp = fopen(file, "w");
2172 condlog(0, "couldn't fopen %s : %s", file,
2176 fprintf(fp, "%i", score);
2180 if (errno != ENOENT) {
2181 condlog(0, "couldn't stat %s : %s", file,
2185 #ifdef OOM_ADJUST_MIN
2186 file = "/proc/self/oom_adj";
2187 score = OOM_ADJUST_MIN;
2192 condlog(0, "couldn't adjust oom score");
2196 child (void * param)
2198 pthread_t check_thr, uevent_thr, uxlsnr_thr, uevq_thr;
2199 pthread_attr_t log_attr, misc_attr, uevent_attr;
2200 struct vectors * vecs;
2201 struct multipath * mpp;
2204 unsigned long checkint;
2208 struct config *conf;
2211 mlockall(MCL_CURRENT | MCL_FUTURE);
2215 setup_thread_attr(&misc_attr, 64 * 1024, 1);
2216 setup_thread_attr(&uevent_attr, DEFAULT_UEVENT_STACKSIZE * 1024, 1);
2217 setup_thread_attr(&waiter_attr, 32 * 1024, 1);
2220 setup_thread_attr(&log_attr, 64 * 1024, 0);
2221 log_thread_start(&log_attr);
2222 pthread_attr_destroy(&log_attr);
2224 pid_fd = pidfile_create(DEFAULT_PIDFILE, daemon_pid);
2226 condlog(1, "failed to create pidfile");
2232 post_config_state(DAEMON_START);
2234 condlog(2, "--------start up--------");
2235 condlog(2, "read " DEFAULT_CONFIGFILE);
2237 conf = load_config(DEFAULT_CONFIGFILE);
2242 conf->verbosity = verbosity;
2243 if (bindings_read_only)
2244 conf->bindings_read_only = bindings_read_only;
2245 if (ignore_new_devs)
2246 conf->ignore_new_devs = ignore_new_devs;
2247 uxsock_timeout = conf->uxsock_timeout;
2248 multipath_conf = conf;
2249 dm_init(conf->verbosity);
2250 dm_drv_version(conf->version, TGT_MPATH);
2251 if (init_checkers(conf->multipath_dir)) {
2252 condlog(0, "failed to initialize checkers");
2255 if (init_prio(conf->multipath_dir)) {
2256 condlog(0, "failed to initialize prioritizers");
2260 setlogmask(LOG_UPTO(conf->verbosity + 3));
2262 envp = getenv("LimitNOFILE");
2265 condlog(2,"Using systemd provided open fds limit of %s", envp);
2266 } else if (conf->max_fds) {
2267 struct rlimit fd_limit;
2269 if (getrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
2270 condlog(0, "can't get open fds limit: %s",
2272 fd_limit.rlim_cur = 0;
2273 fd_limit.rlim_max = 0;
2275 if (fd_limit.rlim_cur < conf->max_fds) {
2276 fd_limit.rlim_cur = conf->max_fds;
2277 if (fd_limit.rlim_max < conf->max_fds)
2278 fd_limit.rlim_max = conf->max_fds;
2279 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
2280 condlog(0, "can't set open fds limit to "
2282 fd_limit.rlim_cur, fd_limit.rlim_max,
2285 condlog(3, "set open fds limit to %lu/%lu",
2286 fd_limit.rlim_cur, fd_limit.rlim_max);
2292 vecs = gvecs = init_vecs();
2299 dm_udev_set_sync_support(0);
2301 envp = getenv("WATCHDOG_USEC");
2302 if (envp && sscanf(envp, "%lu", &checkint) == 1) {
2303 /* Value is in microseconds */
2304 conf->max_checkint = checkint / 1000000;
2305 /* Rescale checkint */
2306 if (conf->checkint > conf->max_checkint)
2307 conf->checkint = conf->max_checkint;
2309 conf->checkint = conf->max_checkint / 4;
2310 condlog(3, "enabling watchdog, interval %d max %d",
2311 conf->checkint, conf->max_checkint);
2312 use_watchdog = conf->checkint;
2316 * Signal start of configuration
2318 post_config_state(DAEMON_CONFIGURE);
2321 * Start uevent listener early to catch events
2323 if ((rc = pthread_create(&uevent_thr, &uevent_attr, ueventloop, udev))) {
2324 condlog(0, "failed to create uevent thread: %d", rc);
2327 pthread_attr_destroy(&uevent_attr);
2328 if ((rc = pthread_create(&uxlsnr_thr, &misc_attr, uxlsnrloop, vecs))) {
2329 condlog(0, "failed to create cli listener: %d", rc);
2336 if ((rc = pthread_create(&check_thr, &misc_attr, checkerloop, vecs))) {
2337 condlog(0,"failed to create checker loop thread: %d", rc);
2340 if ((rc = pthread_create(&uevq_thr, &misc_attr, uevqloop, vecs))) {
2341 condlog(0, "failed to create uevent dispatcher: %d", rc);
2344 pthread_attr_destroy(&misc_attr);
2347 sd_notify(0, "READY=1");
2350 while (running_state != DAEMON_SHUTDOWN) {
2351 pthread_cleanup_push(config_cleanup, NULL);
2352 pthread_mutex_lock(&config_lock);
2353 if (running_state != DAEMON_CONFIGURE &&
2354 running_state != DAEMON_SHUTDOWN) {
2355 pthread_cond_wait(&config_cond, &config_lock);
2357 pthread_cleanup_pop(1);
2358 if (running_state == DAEMON_CONFIGURE) {
2359 pthread_cleanup_push(cleanup_lock, &vecs->lock);
2361 pthread_testcancel();
2362 if (!need_to_delay_reconfig(vecs)) {
2365 conf->delayed_reconfig = 1;
2367 lock_cleanup_pop(vecs->lock);
2368 post_config_state(DAEMON_IDLE);
2373 if (conf->queue_without_daemon == QUE_NO_DAEMON_OFF)
2374 vector_foreach_slot(vecs->mpvec, mpp, i)
2375 dm_queue_if_no_path(mpp->alias, 0);
2376 remove_maps_and_stop_waiters(vecs);
2379 pthread_cancel(check_thr);
2380 pthread_cancel(uevent_thr);
2381 pthread_cancel(uxlsnr_thr);
2382 pthread_cancel(uevq_thr);
2385 free_pathvec(vecs->pathvec, FREE_PATHS);
2386 vecs->pathvec = NULL;
2388 /* Now all the waitevent threads will start rushing in. */
2389 while (vecs->lock.depth > 0) {
2390 sleep (1); /* This is weak. */
2391 condlog(3, "Have %d wait event checkers threads to de-alloc,"
2392 " waiting...", vecs->lock.depth);
2394 pthread_mutex_destroy(vecs->lock.mutex);
2395 FREE(vecs->lock.mutex);
2396 vecs->lock.depth = 0;
2397 vecs->lock.mutex = NULL;
2407 /* We're done here */
2408 condlog(3, "unlink pidfile");
2409 unlink(DEFAULT_PIDFILE);
2411 condlog(2, "--------shut down-------");
2417 * Freeing config must be done after condlog() and dm_lib_exit(),
2418 * because logging functions like dlog() and dm_write_log()
2419 * reference the config.
2425 pthread_attr_destroy(&waiter_attr);
2427 dbg_free_final(NULL);
2431 sd_notify(0, "ERRNO=0");
2437 sd_notify(0, "ERRNO=1");
2450 if( (pid = fork()) < 0){
2451 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
2459 if ( (pid = fork()) < 0)
2460 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
2465 fprintf(stderr, "cannot chdir to '/', continuing\n");
2467 dev_null_fd = open("/dev/null", O_RDWR);
2468 if (dev_null_fd < 0){
2469 fprintf(stderr, "cannot open /dev/null for input & output : %s\n",
2474 close(STDIN_FILENO);
2475 if (dup(dev_null_fd) < 0) {
2476 fprintf(stderr, "cannot dup /dev/null to stdin : %s\n",
2480 close(STDOUT_FILENO);
2481 if (dup(dev_null_fd) < 0) {
2482 fprintf(stderr, "cannot dup /dev/null to stdout : %s\n",
2486 close(STDERR_FILENO);
2487 if (dup(dev_null_fd) < 0) {
2488 fprintf(stderr, "cannot dup /dev/null to stderr : %s\n",
2493 daemon_pid = getpid();
2498 main (int argc, char *argv[])
2500 extern char *optarg;
2505 struct config *conf;
2509 if (getuid() != 0) {
2510 fprintf(stderr, "need to be root\n");
2514 /* make sure we don't lock any path */
2516 fprintf(stderr, "can't chdir to root directory : %s\n",
2518 umask(umask(077) | 022);
2522 while ((arg = getopt(argc, argv, ":dsv:k::Bn")) != EOF ) {
2528 //debug=1; /* ### comment me out ### */
2531 if (sizeof(optarg) > sizeof(char *) ||
2532 !isdigit(optarg[0]))
2535 verbosity = atoi(optarg);
2541 conf = load_config(DEFAULT_CONFIGFILE);
2545 conf->verbosity = verbosity;
2546 uxclnt(optarg, uxsock_timeout + 100);
2549 bindings_read_only = 1;
2552 ignore_new_devs = 1;
2555 fprintf(stderr, "Invalid argument '-%c'\n",
2560 if (optind < argc) {
2565 conf = load_config(DEFAULT_CONFIGFILE);
2569 conf->verbosity = verbosity;
2570 memset(cmd, 0x0, CMDSIZE);
2571 while (optind < argc) {
2572 if (strchr(argv[optind], ' '))
2573 c += snprintf(c, s + CMDSIZE - c, "\"%s\" ", argv[optind]);
2575 c += snprintf(c, s + CMDSIZE - c, "%s ", argv[optind]);
2578 c += snprintf(c, s + CMDSIZE - c, "\n");
2579 uxclnt(s, uxsock_timeout + 100);
2584 if (!isatty(fileno(stdout)))
2585 setbuf(stdout, NULL);
2587 daemon_pid = getpid();
2599 return (child(NULL));
2602 void * mpath_pr_event_handler_fn (void * pathp )
2604 struct multipath * mpp;
2605 int i,j, ret, isFound;
2606 struct path * pp = (struct path *)pathp;
2607 unsigned char *keyp;
2609 struct prout_param_descriptor *param;
2610 struct prin_resp *resp;
2614 resp = mpath_alloc_prin_response(MPATH_PRIN_RKEY_SA);
2616 condlog(0,"%s Alloc failed for prin response", pp->dev);
2620 ret = prin_do_scsi_ioctl(pp->dev, MPATH_PRIN_RKEY_SA, resp, 0);
2621 if (ret != MPATH_PR_SUCCESS )
2623 condlog(0,"%s : pr in read keys service action failed. Error=%d", pp->dev, ret);
2627 condlog(3, " event pr=%d addlen=%d",resp->prin_descriptor.prin_readkeys.prgeneration,
2628 resp->prin_descriptor.prin_readkeys.additional_length );
2630 if (resp->prin_descriptor.prin_readkeys.additional_length == 0 )
2632 condlog(1, "%s: No key found. Device may not be registered.", pp->dev);
2633 ret = MPATH_PR_SUCCESS;
2637 keyp = (unsigned char *)mpp->reservation_key;
2638 for (j = 0; j < 8; ++j) {
2644 condlog(2, "Multipath reservation_key: 0x%" PRIx64 " ", prkey);
2647 for (i = 0; i < resp->prin_descriptor.prin_readkeys.additional_length/8; i++ )
2649 condlog(2, "PR IN READKEYS[%d] reservation key:",i);
2650 dumpHex((char *)&resp->prin_descriptor.prin_readkeys.key_list[i*8], 8 , -1);
2651 if (!memcmp(mpp->reservation_key, &resp->prin_descriptor.prin_readkeys.key_list[i*8], 8))
2653 condlog(2, "%s: pr key found in prin readkeys response", mpp->alias);
2660 condlog(0, "%s: Either device not registered or ", pp->dev);
2661 condlog(0, "host is not authorised for registration. Skip path");
2662 ret = MPATH_PR_OTHER;
2666 param= malloc(sizeof(struct prout_param_descriptor));
2667 memset(param, 0 , sizeof(struct prout_param_descriptor));
2669 for (j = 7; j >= 0; --j) {
2670 param->sa_key[j] = (prkey & 0xff);
2673 param->num_transportid = 0;
2675 condlog(3, "device %s:%s", pp->dev, pp->mpp->wwid);
2677 ret = prout_do_scsi_ioctl(pp->dev, MPATH_PROUT_REG_IGN_SA, 0, 0, param, 0);
2678 if (ret != MPATH_PR_SUCCESS )
2680 condlog(0,"%s: Reservation registration failed. Error: %d", pp->dev, ret);
2690 int mpath_pr_event_handle(struct path *pp)
2694 pthread_attr_t attr;
2695 struct multipath * mpp;
2699 if (!mpp->reservation_key)
2702 pthread_attr_init(&attr);
2703 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
2705 rc = pthread_create(&thread, NULL , mpath_pr_event_handler_fn, pp);
2707 condlog(0, "%s: ERROR; return code from pthread_create() is %d", pp->dev, rc);
2710 pthread_attr_destroy(&attr);
2711 rc = pthread_join(thread, NULL);