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>
31 #include "time-util.h"
39 static int use_watchdog;
53 #include "blacklist.h"
54 #include "structs_vec.h"
56 #include "devmapper.h"
59 #include "discovery.h"
63 #include "switchgroup.h"
65 #include "configure.h"
68 #include "pgpolicies.h"
72 #include "mpath_cmd.h"
73 #include "mpath_persist.h"
75 #include "prioritizers/alua_rtpg.h"
82 #include "cli_handlers.h"
86 #include "io_err_stat.h"
89 #include "../third-party/valgrind/drd.h"
91 #define FILE_NAME_SIZE 256
94 #define LOG_MSG(a, b) \
97 condlog(a, "%s: %s - path offline", pp->mpp->alias, pp->dev); \
99 condlog(a, "%s: %s - %s", pp->mpp->alias, pp->dev, b); \
102 struct mpath_event_param
105 struct multipath *mpp;
111 int bindings_read_only;
113 #ifdef NO_DMEVENTS_POLL
114 int poll_dmevents = 0;
116 int poll_dmevents = 1;
118 enum daemon_status running_state = DAEMON_INIT;
120 pthread_mutex_t config_lock = PTHREAD_MUTEX_INITIALIZER;
121 pthread_cond_t config_cond;
124 * global copy of vecs for use in sig handlers
126 struct vectors * gvecs;
130 struct config *multipath_conf;
132 /* Local variables */
133 static volatile sig_atomic_t exit_sig;
134 static volatile sig_atomic_t reconfig_sig;
135 static volatile sig_atomic_t log_reset_sig;
140 switch (running_state) {
145 case DAEMON_CONFIGURE:
151 case DAEMON_SHUTDOWN:
158 * I love you too, systemd ...
161 sd_notify_status(void)
163 switch (running_state) {
165 return "STATUS=init";
167 return "STATUS=startup";
168 case DAEMON_CONFIGURE:
169 return "STATUS=configure";
173 case DAEMON_SHUTDOWN:
174 return "STATUS=shutdown";
180 static void do_sd_notify(enum daemon_status old_state)
183 * Checkerloop switches back and forth between idle and running state.
184 * No need to tell systemd each time.
185 * These notifications cause a lot of overhead on dbus.
187 if ((running_state == DAEMON_IDLE || running_state == DAEMON_RUNNING) &&
188 (old_state == DAEMON_IDLE || old_state == DAEMON_RUNNING))
190 sd_notify(0, sd_notify_status());
194 static void config_cleanup(void *arg)
196 pthread_mutex_unlock(&config_lock);
199 void post_config_state(enum daemon_status state)
201 pthread_mutex_lock(&config_lock);
202 if (state != running_state) {
203 enum daemon_status old_state = running_state;
205 running_state = state;
206 pthread_cond_broadcast(&config_cond);
208 do_sd_notify(old_state);
211 pthread_mutex_unlock(&config_lock);
214 int set_config_state(enum daemon_status state)
218 pthread_cleanup_push(config_cleanup, NULL);
219 pthread_mutex_lock(&config_lock);
220 if (running_state != state) {
221 enum daemon_status old_state = running_state;
223 if (running_state != DAEMON_IDLE) {
226 clock_gettime(CLOCK_MONOTONIC, &ts);
228 rc = pthread_cond_timedwait(&config_cond,
232 running_state = state;
233 pthread_cond_broadcast(&config_cond);
235 do_sd_notify(old_state);
239 pthread_cleanup_pop(1);
243 struct config *get_multipath_config(void)
246 return rcu_dereference(multipath_conf);
249 void put_multipath_config(void *arg)
255 need_switch_pathgroup (struct multipath * mpp, int refresh)
257 struct pathgroup * pgp;
267 * Refresh path priority values
270 vector_foreach_slot (mpp->pg, pgp, i) {
271 vector_foreach_slot (pgp->paths, pp, j) {
272 conf = get_multipath_config();
273 pthread_cleanup_push(put_multipath_config,
275 pathinfo(pp, conf, DI_PRIO);
276 pthread_cleanup_pop(1);
281 if (!mpp->pg || VECTOR_SIZE(mpp->paths) == 0)
284 bestpg = select_path_group(mpp);
285 if (mpp->pgfailback == -FAILBACK_MANUAL)
288 mpp->bestpg = bestpg;
289 if (mpp->bestpg != mpp->nextpg)
296 switch_pathgroup (struct multipath * mpp)
298 mpp->stat_switchgroup++;
299 dm_switchgroup(mpp->alias, mpp->bestpg);
300 condlog(2, "%s: switch to path group #%i",
301 mpp->alias, mpp->bestpg);
305 wait_for_events(struct multipath *mpp, struct vectors *vecs)
308 return watch_dmevents(mpp->alias);
310 return start_waiter_thread(mpp, vecs);
314 remove_map_and_stop_waiter(struct multipath *mpp, struct vectors *vecs)
316 /* devices are automatically removed by the dmevent polling code,
317 * so they don't need to be manually removed here */
319 stop_waiter_thread(mpp, vecs);
320 remove_map(mpp, vecs, PURGE_VEC);
324 remove_maps_and_stop_waiters(struct vectors *vecs)
327 struct multipath * mpp;
332 if (!poll_dmevents) {
333 vector_foreach_slot(vecs->mpvec, mpp, i)
334 stop_waiter_thread(mpp, vecs);
337 unwatch_all_dmevents();
343 set_multipath_wwid (struct multipath * mpp)
345 if (strlen(mpp->wwid))
348 dm_get_uuid(mpp->alias, mpp->wwid);
351 static void set_no_path_retry(struct multipath *mpp)
353 char is_queueing = 0;
355 mpp->nr_active = pathcount(mpp, PATH_UP) + pathcount(mpp, PATH_GHOST);
356 if (mpp->features && strstr(mpp->features, "queue_if_no_path"))
359 switch (mpp->no_path_retry) {
360 case NO_PATH_RETRY_UNDEF:
362 case NO_PATH_RETRY_FAIL:
364 dm_queue_if_no_path(mpp->alias, 0);
366 case NO_PATH_RETRY_QUEUE:
368 dm_queue_if_no_path(mpp->alias, 1);
371 if (mpp->nr_active > 0) {
373 dm_queue_if_no_path(mpp->alias, 1);
374 } else if (is_queueing && mpp->retry_tick == 0)
375 enter_recovery_mode(mpp);
380 int __setup_multipath(struct vectors *vecs, struct multipath *mpp,
383 if (dm_get_info(mpp->alias, &mpp->dmi)) {
384 /* Error accessing table */
385 condlog(3, "%s: cannot access table", mpp->alias);
389 if (update_multipath_strings(mpp, vecs->pathvec, 1)) {
390 condlog(0, "%s: failed to setup multipath", mpp->alias);
395 set_no_path_retry(mpp);
396 if (VECTOR_SIZE(mpp->paths) != 0)
397 dm_cancel_deferred_remove(mpp);
402 remove_map_and_stop_waiter(mpp, vecs);
406 int update_multipath (struct vectors *vecs, char *mapname, int reset)
408 struct multipath *mpp;
409 struct pathgroup *pgp;
413 mpp = find_mp_by_alias(vecs->mpvec, mapname);
416 condlog(3, "%s: multipath map not found", mapname);
420 if (__setup_multipath(vecs, mpp, reset))
421 return 1; /* mpp freed in setup_multipath */
424 * compare checkers states with DM states
426 vector_foreach_slot (mpp->pg, pgp, i) {
427 vector_foreach_slot (pgp->paths, pp, j) {
428 if (pp->dmstate != PSTATE_FAILED)
431 if (pp->state != PATH_DOWN) {
432 struct config *conf = get_multipath_config();
433 int oldstate = pp->state;
436 conf = get_multipath_config();
437 checkint = conf->checkint;
438 put_multipath_config(conf);
439 condlog(2, "%s: mark as failed", pp->dev);
440 mpp->stat_path_failures++;
441 pp->state = PATH_DOWN;
442 if (oldstate == PATH_UP ||
443 oldstate == PATH_GHOST)
444 update_queue_mode_del_path(mpp);
448 * schedule the next check earlier
450 if (pp->tick > checkint)
459 update_map (struct multipath *mpp, struct vectors *vecs, int new_map)
462 char params[PARAMS_SIZE] = {0};
465 condlog(4, "%s: updating new map", mpp->alias);
466 if (adopt_paths(vecs->pathvec, mpp)) {
467 condlog(0, "%s: failed to adopt paths for new map update",
472 verify_paths(mpp, vecs);
473 mpp->action = ACT_RELOAD;
475 extract_hwe_from_path(mpp);
476 if (setup_map(mpp, params, PARAMS_SIZE, vecs)) {
477 condlog(0, "%s: failed to setup new map in update", mpp->alias);
481 if (domap(mpp, params, 1) <= 0 && retries-- > 0) {
482 condlog(0, "%s: map_udate sleep", mpp->alias);
489 if (new_map && (retries < 0 || wait_for_events(mpp, vecs))) {
490 condlog(0, "%s: failed to create new map", mpp->alias);
491 remove_map(mpp, vecs, 1);
495 if (setup_multipath(vecs, mpp))
501 condlog(0, "%s: failed reload in new map update", mpp->alias);
505 static struct multipath *
506 add_map_without_path (struct vectors *vecs, const char *alias)
508 struct multipath * mpp = alloc_multipath();
518 mpp->alias = STRDUP(alias);
520 if (dm_get_info(mpp->alias, &mpp->dmi)) {
521 condlog(3, "%s: cannot access table", mpp->alias);
524 set_multipath_wwid(mpp);
525 conf = get_multipath_config();
526 mpp->mpe = find_mpe(conf->mptable, mpp->wwid);
527 put_multipath_config(conf);
529 if (update_multipath_table(mpp, vecs->pathvec, 1))
531 if (update_multipath_status(mpp))
534 if (!vector_alloc_slot(vecs->mpvec))
537 vector_set_slot(vecs->mpvec, mpp);
539 if (update_map(mpp, vecs, 1) != 0) /* map removed */
544 remove_map(mpp, vecs, PURGE_VEC);
549 coalesce_maps(struct vectors *vecs, vector nmpv)
551 struct multipath * ompp;
552 vector ompv = vecs->mpvec;
553 unsigned int i, reassign_maps;
556 conf = get_multipath_config();
557 reassign_maps = conf->reassign_maps;
558 put_multipath_config(conf);
559 vector_foreach_slot (ompv, ompp, i) {
560 condlog(3, "%s: coalesce map", ompp->alias);
561 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
563 * remove all current maps not allowed by the
564 * current configuration
566 if (dm_flush_map(ompp->alias)) {
567 condlog(0, "%s: unable to flush devmap",
570 * may be just because the device is open
572 if (setup_multipath(vecs, ompp) != 0) {
576 if (!vector_alloc_slot(nmpv))
579 vector_set_slot(nmpv, ompp);
581 vector_del_slot(ompv, i);
586 condlog(2, "%s devmap removed", ompp->alias);
588 } else if (reassign_maps) {
589 condlog(3, "%s: Reassign existing device-mapper"
590 " devices", ompp->alias);
591 dm_reassign(ompp->alias);
598 sync_maps_state(vector mpvec)
601 struct multipath *mpp;
603 vector_foreach_slot (mpvec, mpp, i)
608 flush_map(struct multipath * mpp, struct vectors * vecs, int nopaths)
613 r = dm_flush_map_nopaths(mpp->alias, mpp->deferred_remove);
615 r = dm_flush_map(mpp->alias);
617 * clear references to this map before flushing so we can ignore
618 * the spurious uevent we may generate with the dm_flush_map call below
622 * May not really be an error -- if the map was already flushed
623 * from the device mapper by dmsetup(8) for instance.
626 condlog(0, "%s: can't flush", mpp->alias);
628 condlog(2, "%s: devmap deferred remove", mpp->alias);
629 mpp->deferred_remove = DEFERRED_REMOVE_IN_PROGRESS;
635 condlog(2, "%s: map flushed", mpp->alias);
638 orphan_paths(vecs->pathvec, mpp);
639 remove_map_and_stop_waiter(mpp, vecs);
645 uev_add_map (struct uevent * uev, struct vectors * vecs)
648 int major = -1, minor = -1, rc;
650 condlog(3, "%s: add map (uevent)", uev->kernel);
651 alias = uevent_get_dm_name(uev);
653 condlog(3, "%s: No DM_NAME in uevent", uev->kernel);
654 major = uevent_get_major(uev);
655 minor = uevent_get_minor(uev);
656 alias = dm_mapname(major, minor);
658 condlog(2, "%s: mapname not found for %d:%d",
659 uev->kernel, major, minor);
663 pthread_cleanup_push(cleanup_lock, &vecs->lock);
665 pthread_testcancel();
666 rc = ev_add_map(uev->kernel, alias, vecs);
667 lock_cleanup_pop(vecs->lock);
673 * ev_add_map expects that the multipath device already exists in kernel
674 * before it is called. It just adds a device to multipathd or updates an
678 ev_add_map (char * dev, const char * alias, struct vectors * vecs)
680 struct multipath * mpp;
681 int delayed_reconfig, reassign_maps;
684 if (!dm_is_mpath(alias)) {
685 condlog(4, "%s: not a multipath map", alias);
689 mpp = find_mp_by_alias(vecs->mpvec, alias);
692 if (mpp->wait_for_udev > 1) {
693 condlog(2, "%s: performing delayed actions",
695 if (update_map(mpp, vecs, 0))
696 /* setup multipathd removed the map */
699 conf = get_multipath_config();
700 delayed_reconfig = conf->delayed_reconfig;
701 reassign_maps = conf->reassign_maps;
702 put_multipath_config(conf);
703 if (mpp->wait_for_udev) {
704 mpp->wait_for_udev = 0;
705 if (delayed_reconfig &&
706 !need_to_delay_reconfig(vecs)) {
707 condlog(2, "reconfigure (delayed)");
708 set_config_state(DAEMON_CONFIGURE);
713 * Not really an error -- we generate our own uevent
714 * if we create a multipath mapped device as a result
718 condlog(3, "%s: Reassign existing device-mapper devices",
724 condlog(2, "%s: adding map", alias);
727 * now we can register the map
729 if ((mpp = add_map_without_path(vecs, alias))) {
731 condlog(2, "%s: devmap %s registered", alias, dev);
734 condlog(2, "%s: ev_add_map failed", dev);
740 uev_remove_map (struct uevent * uev, struct vectors * vecs)
744 struct multipath *mpp;
746 condlog(2, "%s: remove map (uevent)", uev->kernel);
747 alias = uevent_get_dm_name(uev);
749 condlog(3, "%s: No DM_NAME in uevent, ignoring", uev->kernel);
752 minor = uevent_get_minor(uev);
754 pthread_cleanup_push(cleanup_lock, &vecs->lock);
756 pthread_testcancel();
757 mpp = find_mp_by_minor(vecs->mpvec, minor);
760 condlog(2, "%s: devmap not registered, can't remove",
764 if (strcmp(mpp->alias, alias)) {
765 condlog(2, "%s: minor number mismatch (map %d, event %d)",
766 mpp->alias, mpp->dmi->minor, minor);
770 orphan_paths(vecs->pathvec, mpp);
771 remove_map_and_stop_waiter(mpp, vecs);
773 lock_cleanup_pop(vecs->lock);
778 /* Called from CLI handler */
780 ev_remove_map (char * devname, char * alias, int minor, struct vectors * vecs)
782 struct multipath * mpp;
784 mpp = find_mp_by_minor(vecs->mpvec, minor);
787 condlog(2, "%s: devmap not registered, can't remove",
791 if (strcmp(mpp->alias, alias)) {
792 condlog(2, "%s: minor number mismatch (map %d, event %d)",
793 mpp->alias, mpp->dmi->minor, minor);
796 return flush_map(mpp, vecs, 0);
800 uev_add_path (struct uevent *uev, struct vectors * vecs, int need_do_map)
806 condlog(2, "%s: add path (uevent)", uev->kernel);
807 if (strstr(uev->kernel, "..") != NULL) {
809 * Don't allow relative device names in the pathvec
811 condlog(0, "%s: path name is invalid", uev->kernel);
815 pthread_cleanup_push(cleanup_lock, &vecs->lock);
817 pthread_testcancel();
818 pp = find_path_by_dev(vecs->pathvec, uev->kernel);
822 condlog(3, "%s: spurious uevent, path already in pathvec",
824 if (!pp->mpp && !strlen(pp->wwid)) {
825 condlog(3, "%s: reinitialize path", uev->kernel);
826 udev_device_unref(pp->udev);
827 pp->udev = udev_device_ref(uev->udev);
828 conf = get_multipath_config();
829 pthread_cleanup_push(put_multipath_config, conf);
830 r = pathinfo(pp, conf,
831 DI_ALL | DI_BLACKLIST);
832 pthread_cleanup_pop(1);
833 if (r == PATHINFO_OK)
834 ret = ev_add_path(pp, vecs, need_do_map);
835 else if (r == PATHINFO_SKIPPED) {
836 condlog(3, "%s: remove blacklisted path",
838 i = find_slot(vecs->pathvec, (void *)pp);
840 vector_del_slot(vecs->pathvec, i);
843 condlog(0, "%s: failed to reinitialize path",
849 lock_cleanup_pop(vecs->lock);
854 * get path vital state
856 conf = get_multipath_config();
857 pthread_cleanup_push(put_multipath_config, conf);
858 ret = alloc_path_with_pathinfo(conf, uev->udev,
859 uev->wwid, DI_ALL, &pp);
860 pthread_cleanup_pop(1);
862 if (ret == PATHINFO_SKIPPED)
864 condlog(3, "%s: failed to get path info", uev->kernel);
867 pthread_cleanup_push(cleanup_lock, &vecs->lock);
869 pthread_testcancel();
870 ret = store_path(vecs->pathvec, pp);
872 conf = get_multipath_config();
873 pp->checkint = conf->checkint;
874 put_multipath_config(conf);
875 ret = ev_add_path(pp, vecs, need_do_map);
877 condlog(0, "%s: failed to store path info, "
883 lock_cleanup_pop(vecs->lock);
893 ev_add_path (struct path * pp, struct vectors * vecs, int need_do_map)
895 struct multipath * mpp;
896 char params[PARAMS_SIZE] = {0};
898 int start_waiter = 0;
902 * need path UID to go any further
904 if (strlen(pp->wwid) == 0) {
905 condlog(0, "%s: failed to get path uid", pp->dev);
906 goto fail; /* leave path added to pathvec */
908 mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
909 if (mpp && mpp->wait_for_udev &&
910 (pathcount(mpp, PATH_UP) > 0 ||
911 (pathcount(mpp, PATH_GHOST) > 0 && pp->tpgs != TPGS_IMPLICIT &&
912 mpp->ghost_delay_tick <= 0))) {
913 /* if wait_for_udev is set and valid paths exist */
914 condlog(2, "%s: delaying path addition until %s is fully initialized", pp->dev, mpp->alias);
915 mpp->wait_for_udev = 2;
916 orphan_path(pp, "waiting for create to complete");
923 if (pp->size && mpp->size != pp->size) {
924 condlog(0, "%s: failed to add new path %s, "
925 "device size mismatch",
926 mpp->alias, pp->dev);
927 int i = find_slot(vecs->pathvec, (void *)pp);
929 vector_del_slot(vecs->pathvec, i);
934 condlog(4,"%s: adopting all paths for path %s",
935 mpp->alias, pp->dev);
936 if (adopt_paths(vecs->pathvec, mpp))
937 goto fail; /* leave path added to pathvec */
939 verify_paths(mpp, vecs);
940 mpp->action = ACT_RELOAD;
941 extract_hwe_from_path(mpp);
943 if (!should_multipath(pp, vecs->pathvec, vecs->mpvec)) {
944 orphan_path(pp, "only one path");
947 condlog(4,"%s: creating new map", pp->dev);
948 if ((mpp = add_map_with_path(vecs, pp, 1))) {
949 mpp->action = ACT_CREATE;
951 * We don't depend on ACT_CREATE, as domap will
952 * set it to ACT_NOTHING when complete.
957 goto fail; /* leave path added to pathvec */
960 /* persistent reservation check*/
961 mpath_pr_event_handle(pp);
966 if (!dm_map_present(mpp->alias)) {
967 mpp->action = ACT_CREATE;
971 * push the map to the device-mapper
973 if (setup_map(mpp, params, PARAMS_SIZE, vecs)) {
974 condlog(0, "%s: failed to setup map for addition of new "
975 "path %s", mpp->alias, pp->dev);
979 * reload the map for the multipath mapped device
982 ret = domap(mpp, params, 1);
984 if (ret < 0 && retries-- > 0) {
985 condlog(0, "%s: retry domap for addition of new "
986 "path %s", mpp->alias, pp->dev);
990 condlog(0, "%s: failed in domap for addition of new "
991 "path %s", mpp->alias, pp->dev);
993 * deal with asynchronous uevents :((
995 if (mpp->action == ACT_RELOAD && retries-- > 0) {
996 condlog(0, "%s: ev_add_path sleep", mpp->alias);
998 update_mpp_paths(mpp, vecs->pathvec);
1001 else if (mpp->action == ACT_RELOAD)
1002 condlog(0, "%s: giving up reload", mpp->alias);
1008 if ((mpp->action == ACT_CREATE ||
1009 (mpp->action == ACT_NOTHING && start_waiter && !mpp->waiter)) &&
1010 wait_for_events(mpp, vecs))
1014 * update our state from kernel regardless of create or reload
1016 if (setup_multipath(vecs, mpp))
1017 goto fail; /* if setup_multipath fails, it removes the map */
1019 sync_map_state(mpp);
1022 condlog(2, "%s [%s]: path added to devmap %s",
1023 pp->dev, pp->dev_t, mpp->alias);
1029 remove_map(mpp, vecs, 1);
1031 orphan_path(pp, "failed to add path");
1036 uev_remove_path (struct uevent *uev, struct vectors * vecs, int need_do_map)
1041 condlog(2, "%s: remove path (uevent)", uev->kernel);
1042 delete_foreign(uev->udev);
1044 pthread_cleanup_push(cleanup_lock, &vecs->lock);
1046 pthread_testcancel();
1047 pp = find_path_by_dev(vecs->pathvec, uev->kernel);
1049 ret = ev_remove_path(pp, vecs, need_do_map);
1050 lock_cleanup_pop(vecs->lock);
1052 /* Not an error; path might have been purged earlier */
1053 condlog(0, "%s: path already removed", uev->kernel);
1060 ev_remove_path (struct path *pp, struct vectors * vecs, int need_do_map)
1062 struct multipath * mpp;
1064 char params[PARAMS_SIZE] = {0};
1067 * avoid referring to the map of an orphaned path
1069 if ((mpp = pp->mpp)) {
1071 * transform the mp->pg vector of vectors of paths
1072 * into a mp->params string to feed the device-mapper
1074 if (update_mpp_paths(mpp, vecs->pathvec)) {
1075 condlog(0, "%s: failed to update paths",
1081 * Make sure mpp->hwe doesn't point to freed memory
1082 * We call extract_hwe_from_path() below to restore mpp->hwe
1084 if (mpp->hwe == pp->hwe)
1087 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
1088 vector_del_slot(mpp->paths, i);
1091 * remove the map IF removing the last path
1093 if (VECTOR_SIZE(mpp->paths) == 0) {
1094 char alias[WWID_SIZE];
1097 * flush_map will fail if the device is open
1099 strncpy(alias, mpp->alias, WWID_SIZE);
1100 if (mpp->flush_on_last_del == FLUSH_ENABLED) {
1101 condlog(2, "%s Last path deleted, disabling queueing", mpp->alias);
1102 mpp->retry_tick = 0;
1103 mpp->no_path_retry = NO_PATH_RETRY_FAIL;
1104 mpp->disable_queueing = 1;
1105 mpp->stat_map_failures++;
1106 dm_queue_if_no_path(mpp->alias, 0);
1108 if (!flush_map(mpp, vecs, 1)) {
1109 condlog(2, "%s: removed map after"
1110 " removing all paths",
1116 * Not an error, continue
1120 if (mpp->hwe == NULL)
1121 extract_hwe_from_path(mpp);
1123 if (setup_map(mpp, params, PARAMS_SIZE, vecs)) {
1124 condlog(0, "%s: failed to setup map for"
1125 " removal of path %s", mpp->alias, pp->dev);
1129 if (mpp->wait_for_udev) {
1130 mpp->wait_for_udev = 2;
1139 mpp->action = ACT_RELOAD;
1140 if (domap(mpp, params, 1) <= 0) {
1141 condlog(0, "%s: failed in domap for "
1142 "removal of path %s",
1143 mpp->alias, pp->dev);
1147 * update our state from kernel
1149 if (setup_multipath(vecs, mpp))
1151 sync_map_state(mpp);
1153 condlog(2, "%s [%s]: path removed from map %s",
1154 pp->dev, pp->dev_t, mpp->alias);
1159 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
1160 vector_del_slot(vecs->pathvec, i);
1167 remove_map_and_stop_waiter(mpp, vecs);
1172 uev_update_path (struct uevent *uev, struct vectors * vecs)
1174 int ro, retval = 0, rc;
1176 struct config *conf;
1177 int disable_changed_wwids;
1178 int needs_reinit = 0;
1180 switch ((rc = change_foreign(uev->udev))) {
1182 /* known foreign path, ignore event */
1184 case FOREIGN_IGNORED:
1187 condlog(3, "%s: error in change_foreign", __func__);
1190 condlog(1, "%s: return code %d of change_forein is unsupported",
1195 conf = get_multipath_config();
1196 disable_changed_wwids = conf->disable_changed_wwids;
1197 put_multipath_config(conf);
1199 ro = uevent_get_disk_ro(uev);
1201 pthread_cleanup_push(cleanup_lock, &vecs->lock);
1203 pthread_testcancel();
1205 pp = find_path_by_dev(vecs->pathvec, uev->kernel);
1207 struct multipath *mpp = pp->mpp;
1208 char wwid[WWID_SIZE];
1210 strcpy(wwid, pp->wwid);
1211 get_uid(pp, pp->state, uev->udev);
1213 if (strncmp(wwid, pp->wwid, WWID_SIZE) != 0) {
1214 condlog(0, "%s: path wwid changed from '%s' to '%s'. %s",
1215 uev->kernel, wwid, pp->wwid,
1216 (disable_changed_wwids ? "disallowing" :
1218 if (disable_changed_wwids &&
1219 (strlen(wwid) || pp->wwid_changed)) {
1220 strcpy(pp->wwid, wwid);
1221 if (!pp->wwid_changed) {
1222 pp->wwid_changed = 1;
1225 dm_fail_path(pp->mpp->alias, pp->dev_t);
1228 } else if (!disable_changed_wwids)
1229 strcpy(pp->wwid, wwid);
1231 pp->wwid_changed = 0;
1233 udev_device_unref(pp->udev);
1234 pp->udev = udev_device_ref(uev->udev);
1235 conf = get_multipath_config();
1236 pthread_cleanup_push(put_multipath_config, conf);
1237 if (pathinfo(pp, conf, DI_SYSFS|DI_NOIO) != PATHINFO_OK)
1238 condlog(1, "%s: pathinfo failed after change uevent",
1240 pthread_cleanup_pop(1);
1243 if (pp->initialized == INIT_REQUESTED_UDEV)
1245 else if (mpp && ro >= 0) {
1246 condlog(2, "%s: update path write_protect to '%d' (uevent)", uev->kernel, ro);
1248 if (mpp->wait_for_udev)
1249 mpp->wait_for_udev = 2;
1252 pp->mpp->force_readonly = 1;
1253 retval = reload_map(vecs, mpp, 0, 1);
1254 pp->mpp->force_readonly = 0;
1255 condlog(2, "%s: map %s reloaded (retval %d)",
1256 uev->kernel, mpp->alias, retval);
1261 lock_cleanup_pop(vecs->lock);
1263 /* If the path is blacklisted, print a debug/non-default verbosity message. */
1265 int flag = DI_SYSFS | DI_WWID;
1267 conf = get_multipath_config();
1268 pthread_cleanup_push(put_multipath_config, conf);
1269 retval = alloc_path_with_pathinfo(conf, uev->udev, uev->wwid, flag, NULL);
1270 pthread_cleanup_pop(1);
1272 if (retval == PATHINFO_SKIPPED) {
1273 condlog(3, "%s: spurious uevent, path is blacklisted", uev->kernel);
1278 condlog(0, "%s: spurious uevent, path not found", uev->kernel);
1281 retval = uev_add_path(uev, vecs, 1);
1286 uev_pathfail_check(struct uevent *uev, struct vectors *vecs)
1288 char *action = NULL, *devt = NULL;
1292 action = uevent_get_dm_action(uev);
1295 if (strncmp(action, "PATH_FAILED", 11))
1297 devt = uevent_get_dm_path(uev);
1299 condlog(3, "%s: No DM_PATH in uevent", uev->kernel);
1303 pthread_cleanup_push(cleanup_lock, &vecs->lock);
1305 pthread_testcancel();
1306 pp = find_path_by_devt(vecs->pathvec, devt);
1309 r = io_err_stat_handle_pathfail(pp);
1311 condlog(3, "io_err_stat: %s: cannot handle pathfail uevent",
1314 lock_cleanup_pop(vecs->lock);
1324 map_discovery (struct vectors * vecs)
1326 struct multipath * mpp;
1329 if (dm_get_maps(vecs->mpvec))
1332 vector_foreach_slot (vecs->mpvec, mpp, i)
1333 if (update_multipath_table(mpp, vecs->pathvec, 1) ||
1334 update_multipath_status(mpp)) {
1335 remove_map(mpp, vecs, 1);
1343 uxsock_trigger (char * str, char ** reply, int * len, bool is_root,
1344 void * trigger_data)
1346 struct vectors * vecs;
1351 vecs = (struct vectors *)trigger_data;
1353 if ((str != NULL) && (is_root == false) &&
1354 (strncmp(str, "list", strlen("list")) != 0) &&
1355 (strncmp(str, "show", strlen("show")) != 0)) {
1356 *reply = STRDUP("permission deny: need to be root");
1358 *len = strlen(*reply) + 1;
1362 r = parse_cmd(str, reply, len, vecs, uxsock_timeout / 1000);
1366 *reply = STRDUP("timeout\n");
1368 *reply = STRDUP("fail\n");
1370 *len = strlen(*reply) + 1;
1373 else if (!r && *len == 0) {
1374 *reply = STRDUP("ok\n");
1376 *len = strlen(*reply) + 1;
1379 /* else if (r < 0) leave *reply alone */
1385 uev_trigger (struct uevent * uev, void * trigger_data)
1388 struct vectors * vecs;
1389 struct uevent *merge_uev, *tmp;
1391 vecs = (struct vectors *)trigger_data;
1393 pthread_cleanup_push(config_cleanup, NULL);
1394 pthread_mutex_lock(&config_lock);
1395 if (running_state != DAEMON_IDLE &&
1396 running_state != DAEMON_RUNNING)
1397 pthread_cond_wait(&config_cond, &config_lock);
1398 pthread_cleanup_pop(1);
1400 if (running_state == DAEMON_SHUTDOWN)
1405 * Add events are ignored here as the tables
1406 * are not fully initialised then.
1408 if (!strncmp(uev->kernel, "dm-", 3)) {
1409 if (!uevent_is_mpath(uev)) {
1410 if (!strncmp(uev->action, "change", 6))
1411 (void)add_foreign(uev->udev);
1412 else if (!strncmp(uev->action, "remove", 6))
1413 (void)delete_foreign(uev->udev);
1416 if (!strncmp(uev->action, "change", 6)) {
1417 r = uev_add_map(uev, vecs);
1420 * the kernel-side dm-mpath issues a PATH_FAILED event
1421 * when it encounters a path IO error. It is reason-
1422 * able be the entry of path IO error accounting pro-
1425 uev_pathfail_check(uev, vecs);
1426 } else if (!strncmp(uev->action, "remove", 6)) {
1427 r = uev_remove_map(uev, vecs);
1433 * path add/remove/change event, add/remove maybe merged
1435 list_for_each_entry_safe(merge_uev, tmp, &uev->merge_node, node) {
1436 if (!strncmp(merge_uev->action, "add", 3))
1437 r += uev_add_path(merge_uev, vecs, 0);
1438 if (!strncmp(merge_uev->action, "remove", 6))
1439 r += uev_remove_path(merge_uev, vecs, 0);
1442 if (!strncmp(uev->action, "add", 3))
1443 r += uev_add_path(uev, vecs, 1);
1444 if (!strncmp(uev->action, "remove", 6))
1445 r += uev_remove_path(uev, vecs, 1);
1446 if (!strncmp(uev->action, "change", 6))
1447 r += uev_update_path(uev, vecs);
1453 static void rcu_unregister(void *param)
1455 rcu_unregister_thread();
1459 ueventloop (void * ap)
1461 struct udev *udev = ap;
1463 pthread_cleanup_push(rcu_unregister, NULL);
1464 rcu_register_thread();
1465 if (uevent_listen(udev))
1466 condlog(0, "error starting uevent listener");
1467 pthread_cleanup_pop(1);
1472 uevqloop (void * ap)
1474 pthread_cleanup_push(rcu_unregister, NULL);
1475 rcu_register_thread();
1476 if (uevent_dispatch(&uev_trigger, ap))
1477 condlog(0, "error starting uevent dispatcher");
1478 pthread_cleanup_pop(1);
1482 uxlsnrloop (void * ap)
1485 condlog(1, "Failed to init uxsock listener");
1488 pthread_cleanup_push(rcu_unregister, NULL);
1489 rcu_register_thread();
1490 set_handler_callback(LIST+PATHS, cli_list_paths);
1491 set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt);
1492 set_handler_callback(LIST+PATHS+RAW+FMT, cli_list_paths_raw);
1493 set_handler_callback(LIST+PATH, cli_list_path);
1494 set_handler_callback(LIST+MAPS, cli_list_maps);
1495 set_handler_callback(LIST+STATUS, cli_list_status);
1496 set_unlocked_handler_callback(LIST+DAEMON, cli_list_daemon);
1497 set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
1498 set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
1499 set_handler_callback(LIST+MAPS+FMT, cli_list_maps_fmt);
1500 set_handler_callback(LIST+MAPS+RAW+FMT, cli_list_maps_raw);
1501 set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
1502 set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
1503 set_handler_callback(LIST+MAPS+JSON, cli_list_maps_json);
1504 set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
1505 set_handler_callback(LIST+MAP+FMT, cli_list_map_fmt);
1506 set_handler_callback(LIST+MAP+RAW+FMT, cli_list_map_fmt);
1507 set_handler_callback(LIST+MAP+JSON, cli_list_map_json);
1508 set_handler_callback(LIST+CONFIG+LOCAL, cli_list_config_local);
1509 set_handler_callback(LIST+CONFIG, cli_list_config);
1510 set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
1511 set_handler_callback(LIST+DEVICES, cli_list_devices);
1512 set_handler_callback(LIST+WILDCARDS, cli_list_wildcards);
1513 set_handler_callback(RESET+MAPS+STATS, cli_reset_maps_stats);
1514 set_handler_callback(RESET+MAP+STATS, cli_reset_map_stats);
1515 set_handler_callback(ADD+PATH, cli_add_path);
1516 set_handler_callback(DEL+PATH, cli_del_path);
1517 set_handler_callback(ADD+MAP, cli_add_map);
1518 set_handler_callback(DEL+MAP, cli_del_map);
1519 set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
1520 set_unlocked_handler_callback(RECONFIGURE, cli_reconfigure);
1521 set_handler_callback(SUSPEND+MAP, cli_suspend);
1522 set_handler_callback(RESUME+MAP, cli_resume);
1523 set_handler_callback(RESIZE+MAP, cli_resize);
1524 set_handler_callback(RELOAD+MAP, cli_reload);
1525 set_handler_callback(RESET+MAP, cli_reassign);
1526 set_handler_callback(REINSTATE+PATH, cli_reinstate);
1527 set_handler_callback(FAIL+PATH, cli_fail);
1528 set_handler_callback(DISABLEQ+MAP, cli_disable_queueing);
1529 set_handler_callback(RESTOREQ+MAP, cli_restore_queueing);
1530 set_handler_callback(DISABLEQ+MAPS, cli_disable_all_queueing);
1531 set_handler_callback(RESTOREQ+MAPS, cli_restore_all_queueing);
1532 set_unlocked_handler_callback(QUIT, cli_quit);
1533 set_unlocked_handler_callback(SHUTDOWN, cli_shutdown);
1534 set_handler_callback(GETPRSTATUS+MAP, cli_getprstatus);
1535 set_handler_callback(SETPRSTATUS+MAP, cli_setprstatus);
1536 set_handler_callback(UNSETPRSTATUS+MAP, cli_unsetprstatus);
1537 set_handler_callback(FORCEQ+DAEMON, cli_force_no_daemon_q);
1538 set_handler_callback(RESTOREQ+DAEMON, cli_restore_no_daemon_q);
1539 set_handler_callback(GETPRKEY+MAP, cli_getprkey);
1540 set_handler_callback(SETPRKEY+MAP+KEY, cli_setprkey);
1541 set_handler_callback(UNSETPRKEY+MAP, cli_unsetprkey);
1544 uxsock_listen(&uxsock_trigger, ap);
1545 pthread_cleanup_pop(1);
1552 post_config_state(DAEMON_SHUTDOWN);
1556 fail_path (struct path * pp, int del_active)
1561 condlog(2, "checker failed path %s in map %s",
1562 pp->dev_t, pp->mpp->alias);
1564 dm_fail_path(pp->mpp->alias, pp->dev_t);
1566 update_queue_mode_del_path(pp->mpp);
1570 * caller must have locked the path list before calling that function
1573 reinstate_path (struct path * pp, int add_active)
1580 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t)) {
1581 condlog(0, "%s: reinstate failed", pp->dev_t);
1584 condlog(2, "%s: reinstated", pp->dev_t);
1586 update_queue_mode_add_path(pp->mpp);
1592 enable_group(struct path * pp)
1594 struct pathgroup * pgp;
1597 * if path is added through uev_add_path, pgindex can be unset.
1598 * next update_strings() will set it, upon map reload event.
1600 * we can safely return here, because upon map reload, all
1601 * PG will be enabled.
1603 if (!pp->mpp->pg || !pp->pgindex)
1606 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1608 if (pgp->status == PGSTATE_DISABLED) {
1609 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1610 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1615 mpvec_garbage_collector (struct vectors * vecs)
1617 struct multipath * mpp;
1623 vector_foreach_slot (vecs->mpvec, mpp, i) {
1624 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1625 condlog(2, "%s: remove dead map", mpp->alias);
1626 remove_map_and_stop_waiter(mpp, vecs);
1632 /* This is called after a path has started working again. It the multipath
1633 * device for this path uses the followover failback type, and this is the
1634 * best pathgroup, and this is the first path in the pathgroup to come back
1635 * up, then switch to this pathgroup */
1637 followover_should_failback(struct path * pp)
1639 struct pathgroup * pgp;
1643 if (pp->mpp->pgfailback != -FAILBACK_FOLLOWOVER ||
1644 !pp->mpp->pg || !pp->pgindex ||
1645 pp->pgindex != pp->mpp->bestpg)
1648 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1649 vector_foreach_slot(pgp->paths, pp1, i) {
1652 if (pp1->chkrstate != PATH_DOWN && pp1->chkrstate != PATH_SHAKY)
1659 missing_uev_wait_tick(struct vectors *vecs)
1661 struct multipath * mpp;
1663 int timed_out = 0, delayed_reconfig;
1664 struct config *conf;
1666 vector_foreach_slot (vecs->mpvec, mpp, i) {
1667 if (mpp->wait_for_udev && --mpp->uev_wait_tick <= 0) {
1669 condlog(0, "%s: timeout waiting on creation uevent. enabling reloads", mpp->alias);
1670 if (mpp->wait_for_udev > 1 &&
1671 update_map(mpp, vecs, 0)) {
1672 /* update_map removed map */
1676 mpp->wait_for_udev = 0;
1680 conf = get_multipath_config();
1681 delayed_reconfig = conf->delayed_reconfig;
1682 put_multipath_config(conf);
1683 if (timed_out && delayed_reconfig &&
1684 !need_to_delay_reconfig(vecs)) {
1685 condlog(2, "reconfigure (delayed)");
1686 set_config_state(DAEMON_CONFIGURE);
1691 ghost_delay_tick(struct vectors *vecs)
1693 struct multipath * mpp;
1696 vector_foreach_slot (vecs->mpvec, mpp, i) {
1697 if (mpp->ghost_delay_tick <= 0)
1699 if (--mpp->ghost_delay_tick <= 0) {
1700 condlog(0, "%s: timed out waiting for active path",
1702 mpp->force_udev_reload = 1;
1703 if (update_map(mpp, vecs, 0) != 0) {
1704 /* update_map removed map */
1713 defered_failback_tick (vector mpvec)
1715 struct multipath * mpp;
1718 vector_foreach_slot (mpvec, mpp, i) {
1720 * deferred failback getting sooner
1722 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1723 mpp->failback_tick--;
1725 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1726 switch_pathgroup(mpp);
1732 retry_count_tick(vector mpvec)
1734 struct multipath *mpp;
1737 vector_foreach_slot (mpvec, mpp, i) {
1738 if (mpp->retry_tick > 0) {
1739 mpp->stat_total_queueing_time++;
1740 condlog(4, "%s: Retrying.. No active path", mpp->alias);
1741 if(--mpp->retry_tick == 0) {
1742 mpp->stat_map_failures++;
1743 dm_queue_if_no_path(mpp->alias, 0);
1744 condlog(2, "%s: Disable queueing", mpp->alias);
1750 int update_prio(struct path *pp, int refresh_all)
1754 struct pathgroup * pgp;
1755 int i, j, changed = 0;
1756 struct config *conf;
1759 vector_foreach_slot (pp->mpp->pg, pgp, i) {
1760 vector_foreach_slot (pgp->paths, pp1, j) {
1761 oldpriority = pp1->priority;
1762 conf = get_multipath_config();
1763 pthread_cleanup_push(put_multipath_config,
1765 pathinfo(pp1, conf, DI_PRIO);
1766 pthread_cleanup_pop(1);
1767 if (pp1->priority != oldpriority)
1773 oldpriority = pp->priority;
1774 conf = get_multipath_config();
1775 pthread_cleanup_push(put_multipath_config, conf);
1776 if (pp->state != PATH_DOWN)
1777 pathinfo(pp, conf, DI_PRIO);
1778 pthread_cleanup_pop(1);
1780 if (pp->priority == oldpriority)
1785 int update_path_groups(struct multipath *mpp, struct vectors *vecs, int refresh)
1787 if (reload_map(vecs, mpp, refresh, 1))
1791 if (setup_multipath(vecs, mpp) != 0)
1793 sync_map_state(mpp);
1799 * Returns '1' if the path has been checked, '-1' if it was blacklisted
1803 check_path (struct vectors * vecs, struct path * pp, int ticks)
1806 int new_path_up = 0;
1807 int chkr_new_path_up = 0;
1809 int disable_reinstate = 0;
1810 int oldchkrstate = pp->chkrstate;
1811 int retrigger_tries, checkint;
1812 struct config *conf;
1815 if ((pp->initialized == INIT_OK ||
1816 pp->initialized == INIT_REQUESTED_UDEV) && !pp->mpp)
1820 pp->tick -= (pp->tick > ticks) ? ticks : pp->tick;
1822 return 0; /* don't check this path yet */
1824 conf = get_multipath_config();
1825 retrigger_tries = conf->retrigger_tries;
1826 checkint = conf->checkint;
1827 put_multipath_config(conf);
1828 if (!pp->mpp && pp->initialized == INIT_MISSING_UDEV &&
1829 pp->retriggers < retrigger_tries) {
1830 condlog(2, "%s: triggering change event to reinitialize",
1832 pp->initialized = INIT_REQUESTED_UDEV;
1834 sysfs_attr_set_value(pp->udev, "uevent", "change",
1840 * provision a next check soonest,
1841 * in case we exit abnormaly from here
1843 pp->tick = checkint;
1845 newstate = path_offline(pp);
1847 * Wait for uevent for removed paths;
1848 * some LLDDs like zfcp keep paths unavailable
1849 * without sending uevents.
1851 if (newstate == PATH_REMOVED)
1852 newstate = PATH_DOWN;
1854 if (newstate == PATH_UP) {
1855 conf = get_multipath_config();
1856 pthread_cleanup_push(put_multipath_config, conf);
1857 newstate = get_state(pp, conf, 1, newstate);
1858 pthread_cleanup_pop(1);
1860 checker_clear_message(&pp->checker);
1862 if (pp->wwid_changed) {
1863 condlog(2, "%s: path wwid has changed. Refusing to use",
1865 newstate = PATH_DOWN;
1868 if (newstate == PATH_WILD || newstate == PATH_UNCHECKED) {
1869 condlog(2, "%s: unusable path", pp->dev);
1870 conf = get_multipath_config();
1871 pthread_cleanup_push(put_multipath_config, conf);
1872 pathinfo(pp, conf, 0);
1873 pthread_cleanup_pop(1);
1877 if (!strlen(pp->wwid) && pp->initialized != INIT_MISSING_UDEV &&
1878 (newstate == PATH_UP || newstate == PATH_GHOST)) {
1879 condlog(2, "%s: add missing path", pp->dev);
1880 conf = get_multipath_config();
1881 pthread_cleanup_push(put_multipath_config, conf);
1882 ret = pathinfo(pp, conf, DI_ALL | DI_BLACKLIST);
1883 pthread_cleanup_pop(1);
1884 if (ret == PATHINFO_OK) {
1885 ev_add_path(pp, vecs, 1);
1887 } else if (ret == PATHINFO_SKIPPED)
1893 * Async IO in flight. Keep the previous path state
1894 * and reschedule as soon as possible
1896 if (newstate == PATH_PENDING) {
1901 * Synchronize with kernel state
1903 if (update_multipath_strings(pp->mpp, vecs->pathvec, 1)) {
1904 condlog(1, "%s: Could not synchronize with kernel state",
1906 pp->dmstate = PSTATE_UNDEF;
1908 /* if update_multipath_strings orphaned the path, quit early */
1912 if (pp->io_err_disable_reinstate && hit_io_err_recheck_time(pp)) {
1913 pp->state = PATH_SHAKY;
1915 * to reschedule as soon as possible,so that this path can
1916 * be recoverd in time
1922 if ((newstate == PATH_UP || newstate == PATH_GHOST) &&
1923 pp->wait_checks > 0) {
1924 if (pp->mpp->nr_active > 0) {
1925 pp->state = PATH_DELAYED;
1929 pp->wait_checks = 0;
1933 * don't reinstate failed path, if its in stand-by
1934 * and if target supports only implicit tpgs mode.
1935 * this will prevent unnecessary i/o by dm on stand-by
1936 * paths if there are no other active paths in map.
1938 disable_reinstate = (newstate == PATH_GHOST &&
1939 pp->mpp->nr_active == 0 &&
1940 pp->tpgs == TPGS_IMPLICIT) ? 1 : 0;
1942 pp->chkrstate = newstate;
1943 if (newstate != pp->state) {
1944 int oldstate = pp->state;
1945 pp->state = newstate;
1947 LOG_MSG(1, checker_message(&pp->checker));
1950 * upon state change, reset the checkint
1951 * to the shortest delay
1953 conf = get_multipath_config();
1954 pp->checkint = conf->checkint;
1955 put_multipath_config(conf);
1957 if (newstate != PATH_UP && newstate != PATH_GHOST) {
1959 * proactively fail path in the DM
1961 if (oldstate == PATH_UP ||
1962 oldstate == PATH_GHOST) {
1964 if (pp->mpp->delay_wait_checks > 0 &&
1965 pp->watch_checks > 0) {
1966 pp->wait_checks = pp->mpp->delay_wait_checks;
1967 pp->watch_checks = 0;
1973 * cancel scheduled failback
1975 pp->mpp->failback_tick = 0;
1977 pp->mpp->stat_path_failures++;
1981 if(newstate == PATH_UP || newstate == PATH_GHOST){
1982 if ( pp->mpp && pp->mpp->prflag ){
1984 * Check Persistent Reservation.
1986 condlog(2, "%s: checking persistent reservation "
1987 "registration", pp->dev);
1988 mpath_pr_event_handle(pp);
1993 * reinstate this path
1995 if (oldstate != PATH_UP &&
1996 oldstate != PATH_GHOST) {
1997 if (pp->mpp->delay_watch_checks > 0)
1998 pp->watch_checks = pp->mpp->delay_watch_checks;
2001 if (pp->watch_checks > 0)
2005 if (!disable_reinstate && reinstate_path(pp, add_active)) {
2006 condlog(3, "%s: reload map", pp->dev);
2007 ev_add_path(pp, vecs, 1);
2013 if (oldchkrstate != PATH_UP && oldchkrstate != PATH_GHOST)
2014 chkr_new_path_up = 1;
2017 * if at least one path is up in a group, and
2018 * the group is disabled, re-enable it
2020 if (newstate == PATH_UP)
2023 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
2024 if ((pp->dmstate == PSTATE_FAILED ||
2025 pp->dmstate == PSTATE_UNDEF) &&
2026 !disable_reinstate) {
2027 /* Clear IO errors */
2028 if (reinstate_path(pp, 0)) {
2029 condlog(3, "%s: reload map", pp->dev);
2030 ev_add_path(pp, vecs, 1);
2035 unsigned int max_checkint;
2036 LOG_MSG(4, checker_message(&pp->checker));
2037 conf = get_multipath_config();
2038 max_checkint = conf->max_checkint;
2039 put_multipath_config(conf);
2040 if (pp->checkint != max_checkint) {
2042 * double the next check delay.
2043 * max at conf->max_checkint
2045 if (pp->checkint < (max_checkint / 2))
2046 pp->checkint = 2 * pp->checkint;
2048 pp->checkint = max_checkint;
2050 condlog(4, "%s: delay next check %is",
2051 pp->dev_t, pp->checkint);
2053 if (pp->watch_checks > 0)
2055 pp->tick = pp->checkint;
2058 else if (newstate != PATH_UP && newstate != PATH_GHOST) {
2059 if (pp->dmstate == PSTATE_ACTIVE ||
2060 pp->dmstate == PSTATE_UNDEF)
2062 if (newstate == PATH_DOWN) {
2063 int log_checker_err;
2065 conf = get_multipath_config();
2066 log_checker_err = conf->log_checker_err;
2067 put_multipath_config(conf);
2068 if (log_checker_err == LOG_CHKR_ERR_ONCE)
2069 LOG_MSG(3, checker_message(&pp->checker));
2071 LOG_MSG(2, checker_message(&pp->checker));
2075 pp->state = newstate;
2077 if (pp->mpp->wait_for_udev)
2080 * path prio refreshing
2082 condlog(4, "path prio refresh");
2084 if (update_prio(pp, new_path_up) &&
2085 (pp->mpp->pgpolicyfn == (pgpolicyfn *)group_by_prio) &&
2086 pp->mpp->pgfailback == -FAILBACK_IMMEDIATE)
2087 update_path_groups(pp->mpp, vecs, !new_path_up);
2088 else if (need_switch_pathgroup(pp->mpp, 0)) {
2089 if (pp->mpp->pgfailback > 0 &&
2090 (new_path_up || pp->mpp->failback_tick <= 0))
2091 pp->mpp->failback_tick =
2092 pp->mpp->pgfailback + 1;
2093 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE ||
2094 (chkr_new_path_up && followover_should_failback(pp)))
2095 switch_pathgroup(pp->mpp);
2100 static void init_path_check_interval(struct vectors *vecs)
2102 struct config *conf;
2106 vector_foreach_slot (vecs->pathvec, pp, i) {
2107 conf = get_multipath_config();
2108 pp->checkint = conf->checkint;
2109 put_multipath_config(conf);
2114 checkerloop (void *ap)
2116 struct vectors *vecs;
2120 struct timespec last_time;
2121 struct config *conf;
2123 pthread_cleanup_push(rcu_unregister, NULL);
2124 rcu_register_thread();
2125 mlockall(MCL_CURRENT | MCL_FUTURE);
2126 vecs = (struct vectors *)ap;
2127 condlog(2, "path checkers start up");
2129 /* Tweak start time for initial path check */
2130 if (clock_gettime(CLOCK_MONOTONIC, &last_time) != 0)
2131 last_time.tv_sec = 0;
2133 last_time.tv_sec -= 1;
2136 struct timespec diff_time, start_time, end_time;
2137 int num_paths = 0, ticks = 0, strict_timing, rc = 0;
2139 if (clock_gettime(CLOCK_MONOTONIC, &start_time) != 0)
2140 start_time.tv_sec = 0;
2141 if (start_time.tv_sec && last_time.tv_sec) {
2142 timespecsub(&start_time, &last_time, &diff_time);
2143 condlog(4, "tick (%lu.%06lu secs)",
2144 diff_time.tv_sec, diff_time.tv_nsec / 1000);
2145 last_time = start_time;
2146 ticks = diff_time.tv_sec;
2149 condlog(4, "tick (%d ticks)", ticks);
2153 sd_notify(0, "WATCHDOG=1");
2155 rc = set_config_state(DAEMON_RUNNING);
2156 if (rc == ETIMEDOUT) {
2157 condlog(4, "timeout waiting for DAEMON_IDLE");
2161 pthread_cleanup_push(cleanup_lock, &vecs->lock);
2163 pthread_testcancel();
2164 vector_foreach_slot (vecs->pathvec, pp, i) {
2165 rc = check_path(vecs, pp, ticks);
2167 vector_del_slot(vecs->pathvec, i);
2173 lock_cleanup_pop(vecs->lock);
2175 pthread_cleanup_push(cleanup_lock, &vecs->lock);
2177 pthread_testcancel();
2178 defered_failback_tick(vecs->mpvec);
2179 retry_count_tick(vecs->mpvec);
2180 missing_uev_wait_tick(vecs);
2181 ghost_delay_tick(vecs);
2182 lock_cleanup_pop(vecs->lock);
2187 pthread_cleanup_push(cleanup_lock, &vecs->lock);
2189 pthread_testcancel();
2190 condlog(4, "map garbage collection");
2191 mpvec_garbage_collector(vecs);
2193 lock_cleanup_pop(vecs->lock);
2196 diff_time.tv_nsec = 0;
2197 if (start_time.tv_sec &&
2198 clock_gettime(CLOCK_MONOTONIC, &end_time) == 0) {
2199 timespecsub(&end_time, &start_time, &diff_time);
2201 unsigned int max_checkint;
2203 condlog(3, "checked %d path%s in %lu.%06lu secs",
2204 num_paths, num_paths > 1 ? "s" : "",
2206 diff_time.tv_nsec / 1000);
2207 conf = get_multipath_config();
2208 max_checkint = conf->max_checkint;
2209 put_multipath_config(conf);
2210 if (diff_time.tv_sec > max_checkint)
2211 condlog(1, "path checkers took longer "
2212 "than %lu seconds, consider "
2213 "increasing max_polling_interval",
2218 post_config_state(DAEMON_IDLE);
2219 conf = get_multipath_config();
2220 strict_timing = conf->strict_timing;
2221 put_multipath_config(conf);
2225 if (diff_time.tv_nsec) {
2226 diff_time.tv_sec = 0;
2228 1000UL * 1000 * 1000 - diff_time.tv_nsec;
2230 diff_time.tv_sec = 1;
2232 condlog(3, "waiting for %lu.%06lu secs",
2234 diff_time.tv_nsec / 1000);
2235 if (nanosleep(&diff_time, NULL) != 0) {
2236 condlog(3, "nanosleep failed with error %d",
2238 conf = get_multipath_config();
2239 conf->strict_timing = 0;
2240 put_multipath_config(conf);
2245 pthread_cleanup_pop(1);
2250 configure (struct vectors * vecs)
2252 struct multipath * mpp;
2256 struct config *conf;
2257 static int force_reload = FORCE_RELOAD_WEAK;
2259 if (!vecs->pathvec && !(vecs->pathvec = vector_alloc())) {
2260 condlog(0, "couldn't allocate path vec in configure");
2264 if (!vecs->mpvec && !(vecs->mpvec = vector_alloc())) {
2265 condlog(0, "couldn't allocate multipath vec in configure");
2269 if (!(mpvec = vector_alloc())) {
2270 condlog(0, "couldn't allocate new maps vec in configure");
2275 * probe for current path (from sysfs) and map (from dm) sets
2277 ret = path_discovery(vecs->pathvec, DI_ALL);
2279 condlog(0, "configure failed at path discovery");
2283 vector_foreach_slot (vecs->pathvec, pp, i){
2284 conf = get_multipath_config();
2285 pthread_cleanup_push(put_multipath_config, conf);
2286 if (filter_path(conf, pp) > 0){
2287 vector_del_slot(vecs->pathvec, i);
2292 pp->checkint = conf->checkint;
2293 pthread_cleanup_pop(1);
2295 if (map_discovery(vecs)) {
2296 condlog(0, "configure failed at map discovery");
2301 * create new set of maps & push changed ones into dm
2302 * In the first call, use FORCE_RELOAD_WEAK to avoid making
2303 * superfluous ACT_RELOAD ioctls. Later calls are done
2304 * with FORCE_RELOAD_YES.
2306 ret = coalesce_paths(vecs, mpvec, NULL, force_reload, CMD_NONE);
2307 if (force_reload == FORCE_RELOAD_WEAK)
2308 force_reload = FORCE_RELOAD_YES;
2310 condlog(0, "configure failed while coalescing paths");
2315 * may need to remove some maps which are no longer relevant
2316 * e.g., due to blacklist changes in conf file
2318 if (coalesce_maps(vecs, mpvec)) {
2319 condlog(0, "configure failed while coalescing maps");
2325 sync_maps_state(mpvec);
2326 vector_foreach_slot(mpvec, mpp, i){
2327 if (remember_wwid(mpp->wwid) == 1)
2328 trigger_paths_udev_change(mpp, true);
2333 * purge dm of old maps
2338 * save new set of maps formed by considering current path state
2340 vector_free(vecs->mpvec);
2341 vecs->mpvec = mpvec;
2344 * start dm event waiter threads for these new maps
2346 vector_foreach_slot(vecs->mpvec, mpp, i) {
2347 if (wait_for_events(mpp, vecs)) {
2348 remove_map(mpp, vecs, 1);
2352 if (setup_multipath(vecs, mpp))
2359 need_to_delay_reconfig(struct vectors * vecs)
2361 struct multipath *mpp;
2364 if (!VECTOR_SIZE(vecs->mpvec))
2367 vector_foreach_slot(vecs->mpvec, mpp, i) {
2368 if (mpp->wait_for_udev)
2374 void rcu_free_config(struct rcu_head *head)
2376 struct config *conf = container_of(head, struct config, rcu);
2382 reconfigure (struct vectors * vecs)
2384 struct config * old, *conf;
2386 conf = load_config(DEFAULT_CONFIGFILE);
2391 * free old map and path vectors ... they use old conf state
2393 if (VECTOR_SIZE(vecs->mpvec))
2394 remove_maps_and_stop_waiters(vecs);
2396 free_pathvec(vecs->pathvec, FREE_PATHS);
2397 vecs->pathvec = NULL;
2398 delete_all_foreign();
2400 /* Re-read any timezone changes */
2403 dm_tgt_version(conf->version, TGT_MPATH);
2405 conf->verbosity = verbosity;
2406 if (bindings_read_only)
2407 conf->bindings_read_only = bindings_read_only;
2408 uxsock_timeout = conf->uxsock_timeout;
2410 old = rcu_dereference(multipath_conf);
2411 rcu_assign_pointer(multipath_conf, conf);
2412 call_rcu(&old->rcu, rcu_free_config);
2420 static struct vectors *
2423 struct vectors * vecs;
2425 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
2430 pthread_mutex_init(&vecs->lock.mutex, NULL);
2436 signal_set(int signo, void (*func) (int))
2439 struct sigaction sig;
2440 struct sigaction osig;
2442 sig.sa_handler = func;
2443 sigemptyset(&sig.sa_mask);
2446 r = sigaction(signo, &sig, &osig);
2451 return (osig.sa_handler);
2455 handle_signals(bool nonfatal)
2458 condlog(2, "exit (signal)");
2465 condlog(2, "reconfigure (signal)");
2466 set_config_state(DAEMON_CONFIGURE);
2468 if (log_reset_sig) {
2469 condlog(2, "reset log (signal)");
2498 condlog(3, "SIGUSR2 received");
2506 /* block all signals */
2508 /* SIGPIPE occurs if logging fails */
2509 sigdelset(&set, SIGPIPE);
2510 pthread_sigmask(SIG_SETMASK, &set, NULL);
2512 /* Other signals will be unblocked in the uxlsnr thread */
2513 signal_set(SIGHUP, sighup);
2514 signal_set(SIGUSR1, sigusr1);
2515 signal_set(SIGUSR2, sigusr2);
2516 signal_set(SIGINT, sigend);
2517 signal_set(SIGTERM, sigend);
2518 signal_set(SIGPIPE, sigend);
2525 static struct sched_param sched_param = {
2526 .sched_priority = 99
2529 res = sched_setscheduler (0, SCHED_RR, &sched_param);
2532 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
2539 #ifdef OOM_SCORE_ADJ_MIN
2541 char *file = "/proc/self/oom_score_adj";
2542 int score = OOM_SCORE_ADJ_MIN;
2545 char *file = "/proc/self/oom_adj";
2546 int score = OOM_ADJUST_MIN;
2552 envp = getenv("OOMScoreAdjust");
2554 condlog(3, "Using systemd provided OOMScoreAdjust");
2558 if (stat(file, &st) == 0){
2559 fp = fopen(file, "w");
2561 condlog(0, "couldn't fopen %s : %s", file,
2565 fprintf(fp, "%i", score);
2569 if (errno != ENOENT) {
2570 condlog(0, "couldn't stat %s : %s", file,
2574 #ifdef OOM_ADJUST_MIN
2575 file = "/proc/self/oom_adj";
2576 score = OOM_ADJUST_MIN;
2581 condlog(0, "couldn't adjust oom score");
2585 child (void * param)
2587 pthread_t check_thr, uevent_thr, uxlsnr_thr, uevq_thr, dmevent_thr;
2588 pthread_attr_t log_attr, misc_attr, uevent_attr;
2589 struct vectors * vecs;
2590 struct multipath * mpp;
2593 unsigned long checkint;
2594 int startup_done = 0;
2598 struct config *conf;
2600 int queue_without_daemon;
2602 mlockall(MCL_CURRENT | MCL_FUTURE);
2606 setup_thread_attr(&misc_attr, 64 * 1024, 0);
2607 setup_thread_attr(&uevent_attr, DEFAULT_UEVENT_STACKSIZE * 1024, 0);
2608 setup_thread_attr(&waiter_attr, 32 * 1024, 1);
2609 setup_thread_attr(&io_err_stat_attr, 32 * 1024, 0);
2612 setup_thread_attr(&log_attr, 64 * 1024, 0);
2613 log_thread_start(&log_attr);
2614 pthread_attr_destroy(&log_attr);
2616 pid_fd = pidfile_create(DEFAULT_PIDFILE, daemon_pid);
2618 condlog(1, "failed to create pidfile");
2624 post_config_state(DAEMON_START);
2626 condlog(2, "--------start up--------");
2627 condlog(2, "read " DEFAULT_CONFIGFILE);
2629 conf = load_config(DEFAULT_CONFIGFILE);
2634 conf->verbosity = verbosity;
2635 if (bindings_read_only)
2636 conf->bindings_read_only = bindings_read_only;
2637 uxsock_timeout = conf->uxsock_timeout;
2638 rcu_assign_pointer(multipath_conf, conf);
2639 if (init_checkers(conf->multipath_dir)) {
2640 condlog(0, "failed to initialize checkers");
2643 if (init_prio(conf->multipath_dir)) {
2644 condlog(0, "failed to initialize prioritizers");
2647 /* Failing this is non-fatal */
2649 init_foreign(conf->multipath_dir);
2652 poll_dmevents = dmevent_poll_supported();
2653 setlogmask(LOG_UPTO(conf->verbosity + 3));
2655 envp = getenv("LimitNOFILE");
2658 condlog(2,"Using systemd provided open fds limit of %s", envp);
2659 } else if (conf->max_fds) {
2660 struct rlimit fd_limit;
2662 if (getrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
2663 condlog(0, "can't get open fds limit: %s",
2665 fd_limit.rlim_cur = 0;
2666 fd_limit.rlim_max = 0;
2668 if (fd_limit.rlim_cur < conf->max_fds) {
2669 fd_limit.rlim_cur = conf->max_fds;
2670 if (fd_limit.rlim_max < conf->max_fds)
2671 fd_limit.rlim_max = conf->max_fds;
2672 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
2673 condlog(0, "can't set open fds limit to "
2675 fd_limit.rlim_cur, fd_limit.rlim_max,
2678 condlog(3, "set open fds limit to %lu/%lu",
2679 fd_limit.rlim_cur, fd_limit.rlim_max);
2685 vecs = gvecs = init_vecs();
2693 envp = getenv("WATCHDOG_USEC");
2694 if (envp && sscanf(envp, "%lu", &checkint) == 1) {
2695 /* Value is in microseconds */
2696 conf->max_checkint = checkint / 1000000;
2697 /* Rescale checkint */
2698 if (conf->checkint > conf->max_checkint)
2699 conf->checkint = conf->max_checkint;
2701 conf->checkint = conf->max_checkint / 4;
2702 condlog(3, "enabling watchdog, interval %d max %d",
2703 conf->checkint, conf->max_checkint);
2704 use_watchdog = conf->checkint;
2708 * Startup done, invalidate configuration
2713 * Signal start of configuration
2715 post_config_state(DAEMON_CONFIGURE);
2717 init_path_check_interval(vecs);
2719 if (poll_dmevents) {
2720 if (init_dmevent_waiter(vecs)) {
2721 condlog(0, "failed to allocate dmevents waiter info");
2724 if ((rc = pthread_create(&dmevent_thr, &misc_attr,
2725 wait_dmevents, NULL))) {
2726 condlog(0, "failed to create dmevent waiter thread: %d",
2733 * Start uevent listener early to catch events
2735 if ((rc = pthread_create(&uevent_thr, &uevent_attr, ueventloop, udev))) {
2736 condlog(0, "failed to create uevent thread: %d", rc);
2739 pthread_attr_destroy(&uevent_attr);
2740 if ((rc = pthread_create(&uxlsnr_thr, &misc_attr, uxlsnrloop, vecs))) {
2741 condlog(0, "failed to create cli listener: %d", rc);
2748 if ((rc = pthread_create(&check_thr, &misc_attr, checkerloop, vecs))) {
2749 condlog(0,"failed to create checker loop thread: %d", rc);
2752 if ((rc = pthread_create(&uevq_thr, &misc_attr, uevqloop, vecs))) {
2753 condlog(0, "failed to create uevent dispatcher: %d", rc);
2756 pthread_attr_destroy(&misc_attr);
2758 while (running_state != DAEMON_SHUTDOWN) {
2759 pthread_cleanup_push(config_cleanup, NULL);
2760 pthread_mutex_lock(&config_lock);
2761 if (running_state != DAEMON_CONFIGURE &&
2762 running_state != DAEMON_SHUTDOWN) {
2763 pthread_cond_wait(&config_cond, &config_lock);
2765 pthread_cleanup_pop(1);
2766 if (running_state == DAEMON_CONFIGURE) {
2767 pthread_cleanup_push(cleanup_lock, &vecs->lock);
2769 pthread_testcancel();
2770 if (!need_to_delay_reconfig(vecs)) {
2773 conf = get_multipath_config();
2774 conf->delayed_reconfig = 1;
2775 put_multipath_config(conf);
2777 lock_cleanup_pop(vecs->lock);
2778 post_config_state(DAEMON_IDLE);
2780 if (!startup_done) {
2781 sd_notify(0, "READY=1");
2789 conf = get_multipath_config();
2790 queue_without_daemon = conf->queue_without_daemon;
2791 put_multipath_config(conf);
2792 if (queue_without_daemon == QUE_NO_DAEMON_OFF)
2793 vector_foreach_slot(vecs->mpvec, mpp, i)
2794 dm_queue_if_no_path(mpp->alias, 0);
2795 remove_maps_and_stop_waiters(vecs);
2796 unlock(&vecs->lock);
2798 pthread_cancel(check_thr);
2799 pthread_cancel(uevent_thr);
2800 pthread_cancel(uxlsnr_thr);
2801 pthread_cancel(uevq_thr);
2803 pthread_cancel(dmevent_thr);
2805 pthread_join(check_thr, NULL);
2806 pthread_join(uevent_thr, NULL);
2807 pthread_join(uxlsnr_thr, NULL);
2808 pthread_join(uevq_thr, NULL);
2810 pthread_join(dmevent_thr, NULL);
2812 stop_io_err_stat_thread();
2815 free_pathvec(vecs->pathvec, FREE_PATHS);
2816 vecs->pathvec = NULL;
2817 unlock(&vecs->lock);
2819 pthread_mutex_destroy(&vecs->lock.mutex);
2827 cleanup_dmevent_waiter();
2832 /* We're done here */
2833 condlog(3, "unlink pidfile");
2834 unlink(DEFAULT_PIDFILE);
2836 condlog(2, "--------shut down-------");
2842 * Freeing config must be done after condlog() and dm_lib_exit(),
2843 * because logging functions like dlog() and dm_write_log()
2844 * reference the config.
2846 conf = rcu_dereference(multipath_conf);
2847 rcu_assign_pointer(multipath_conf, NULL);
2848 call_rcu(&conf->rcu, rcu_free_config);
2851 pthread_attr_destroy(&waiter_attr);
2852 pthread_attr_destroy(&io_err_stat_attr);
2854 dbg_free_final(NULL);
2858 sd_notify(0, "ERRNO=0");
2864 sd_notify(0, "ERRNO=1");
2877 if( (pid = fork()) < 0){
2878 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
2886 if ( (pid = fork()) < 0)
2887 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
2892 fprintf(stderr, "cannot chdir to '/', continuing\n");
2894 dev_null_fd = open("/dev/null", O_RDWR);
2895 if (dev_null_fd < 0){
2896 fprintf(stderr, "cannot open /dev/null for input & output : %s\n",
2901 close(STDIN_FILENO);
2902 if (dup(dev_null_fd) < 0) {
2903 fprintf(stderr, "cannot dup /dev/null to stdin : %s\n",
2907 close(STDOUT_FILENO);
2908 if (dup(dev_null_fd) < 0) {
2909 fprintf(stderr, "cannot dup /dev/null to stdout : %s\n",
2913 close(STDERR_FILENO);
2914 if (dup(dev_null_fd) < 0) {
2915 fprintf(stderr, "cannot dup /dev/null to stderr : %s\n",
2920 daemon_pid = getpid();
2925 main (int argc, char *argv[])
2927 extern char *optarg;
2932 struct config *conf;
2934 ANNOTATE_BENIGN_RACE_SIZED(&multipath_conf, sizeof(multipath_conf),
2935 "Manipulated through RCU");
2936 ANNOTATE_BENIGN_RACE_SIZED(&running_state, sizeof(running_state),
2937 "Suppress complaints about unprotected running_state reads");
2938 ANNOTATE_BENIGN_RACE_SIZED(&uxsock_timeout, sizeof(uxsock_timeout),
2939 "Suppress complaints about this scalar variable");
2943 if (getuid() != 0) {
2944 fprintf(stderr, "need to be root\n");
2948 /* make sure we don't lock any path */
2950 fprintf(stderr, "can't chdir to root directory : %s\n",
2952 umask(umask(077) | 022);
2954 pthread_cond_init_mono(&config_cond);
2957 libmp_udev_set_sync_support(0);
2959 while ((arg = getopt(argc, argv, ":dsv:k::Bniw")) != EOF ) {
2965 //debug=1; /* ### comment me out ### */
2968 if (sizeof(optarg) > sizeof(char *) ||
2969 !isdigit(optarg[0]))
2972 verbosity = atoi(optarg);
2978 conf = load_config(DEFAULT_CONFIGFILE);
2982 conf->verbosity = verbosity;
2983 uxsock_timeout = conf->uxsock_timeout;
2984 uxclnt(optarg, uxsock_timeout + 100);
2988 bindings_read_only = 1;
2991 condlog(0, "WARNING: ignoring deprecated option -n, use 'ignore_wwids = no' instead");
2997 fprintf(stderr, "Invalid argument '-%c'\n",
3002 if (optind < argc) {
3007 conf = load_config(DEFAULT_CONFIGFILE);
3011 conf->verbosity = verbosity;
3012 uxsock_timeout = conf->uxsock_timeout;
3013 memset(cmd, 0x0, CMDSIZE);
3014 while (optind < argc) {
3015 if (strchr(argv[optind], ' '))
3016 c += snprintf(c, s + CMDSIZE - c, "\"%s\" ", argv[optind]);
3018 c += snprintf(c, s + CMDSIZE - c, "%s ", argv[optind]);
3021 c += snprintf(c, s + CMDSIZE - c, "\n");
3022 uxclnt(s, uxsock_timeout + 100);
3028 if (!isatty(fileno(stdout)))
3029 setbuf(stdout, NULL);
3031 daemon_pid = getpid();
3043 return (child(NULL));
3046 void * mpath_pr_event_handler_fn (void * pathp )
3048 struct multipath * mpp;
3049 int i, ret, isFound;
3050 struct path * pp = (struct path *)pathp;
3051 struct prout_param_descriptor *param;
3052 struct prin_resp *resp;
3054 rcu_register_thread();
3057 resp = mpath_alloc_prin_response(MPATH_PRIN_RKEY_SA);
3059 condlog(0,"%s Alloc failed for prin response", pp->dev);
3063 ret = prin_do_scsi_ioctl(pp->dev, MPATH_PRIN_RKEY_SA, resp, 0);
3064 if (ret != MPATH_PR_SUCCESS )
3066 condlog(0,"%s : pr in read keys service action failed. Error=%d", pp->dev, ret);
3070 condlog(3, " event pr=%d addlen=%d",resp->prin_descriptor.prin_readkeys.prgeneration,
3071 resp->prin_descriptor.prin_readkeys.additional_length );
3073 if (resp->prin_descriptor.prin_readkeys.additional_length == 0 )
3075 condlog(1, "%s: No key found. Device may not be registered.", pp->dev);
3076 ret = MPATH_PR_SUCCESS;
3079 condlog(2, "Multipath reservation_key: 0x%" PRIx64 " ",
3080 get_be64(mpp->reservation_key));
3083 for (i = 0; i < resp->prin_descriptor.prin_readkeys.additional_length/8; i++ )
3085 condlog(2, "PR IN READKEYS[%d] reservation key:",i);
3086 dumpHex((char *)&resp->prin_descriptor.prin_readkeys.key_list[i*8], 8 , -1);
3087 if (!memcmp(&mpp->reservation_key, &resp->prin_descriptor.prin_readkeys.key_list[i*8], 8))
3089 condlog(2, "%s: pr key found in prin readkeys response", mpp->alias);
3096 condlog(0, "%s: Either device not registered or ", pp->dev);
3097 condlog(0, "host is not authorised for registration. Skip path");
3098 ret = MPATH_PR_OTHER;
3102 param= malloc(sizeof(struct prout_param_descriptor));
3103 memset(param, 0 , sizeof(struct prout_param_descriptor));
3104 param->sa_flags = mpp->sa_flags;
3105 memcpy(param->sa_key, &mpp->reservation_key, 8);
3106 param->num_transportid = 0;
3108 condlog(3, "device %s:%s", pp->dev, pp->mpp->wwid);
3110 ret = prout_do_scsi_ioctl(pp->dev, MPATH_PROUT_REG_IGN_SA, 0, 0, param, 0);
3111 if (ret != MPATH_PR_SUCCESS )
3113 condlog(0,"%s: Reservation registration failed. Error: %d", pp->dev, ret);
3121 rcu_unregister_thread();
3125 int mpath_pr_event_handle(struct path *pp)
3129 pthread_attr_t attr;
3130 struct multipath * mpp;
3134 if (!get_be64(mpp->reservation_key))
3137 pthread_attr_init(&attr);
3138 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
3140 rc = pthread_create(&thread, NULL , mpath_pr_event_handler_fn, pp);
3142 condlog(0, "%s: ERROR; return code from pthread_create() is %d", pp->dev, rc);
3145 pthread_attr_destroy(&attr);
3146 rc = pthread_join(thread, NULL);