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>
19 #include <sysfs/libsysfs.h>
20 #include <sysfs/dlist.h>
26 #include <path_state.h>
37 #include <blacklist.h>
41 #include <structs_vec.h>
43 #include <devmapper.h>
45 #include <discovery.h>
49 #include <switchgroup.h>
50 #include <path_state.h>
52 #include <configure.h>
59 #include "cli_handlers.h"
61 #define FILE_NAME_SIZE 256
64 #define LOG_MSG(a,b) \
66 condlog(a, "%s: %s", pp->dev_t, b); \
67 memset(b, 0, MAX_CHECKER_MSG_SIZE); \
72 fprintf(stderr, "%s:%s(%i) lock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
75 fprintf(stderr, "%s:%s(%i) unlock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
76 pthread_mutex_unlock(a)
77 #define lock_cleanup_pop(a) \
78 fprintf(stderr, "%s:%s(%i) unlock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
79 pthread_cleanup_pop(1);
81 #define lock(a) pthread_mutex_lock(a)
82 #define unlock(a) pthread_mutex_unlock(a)
83 #define lock_cleanup_pop(a) pthread_cleanup_pop(1);
86 pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
87 pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
92 struct vectors * gvecs; /* global copy of vecs for use in sig handlers */
94 static struct event_thread *
98 struct event_thread * wp;
100 wp = (struct event_thread *)MALLOC(sizeof(struct event_thread));
106 free_waiter (void * data)
108 struct event_thread * wp = (struct event_thread *)data;
111 * indicate in mpp that the wp is already freed storage
113 lock(wp->vecs->lock);
117 * be careful, mpp may already be freed -- null if so
119 wp->mpp->waiter = NULL;
121 condlog(3, "free_waiter, mpp freed before wp=%p,", wp);
123 unlock(wp->vecs->lock);
126 dm_task_destroy(wp->dmt);
132 stop_waiter_thread (struct multipath * mpp, struct vectors * vecs)
134 struct event_thread * wp = (struct event_thread *)mpp->waiter;
137 condlog(3, "%s: no waiter thread", mpp->alias);
140 condlog(2, "%s: stop event checker thread", wp->mapname);
141 pthread_kill((pthread_t)wp->thread, SIGUSR1);
145 cleanup_lock (void * data)
147 pthread_mutex_unlock((pthread_mutex_t *)data);
151 * mpp->no_path_retry:
152 * -2 (QUEUE) : queue_if_no_path enabled, never turned off
153 * -1 (FAIL) : fail_if_no_path
154 * 0 (UNDEF) : nothing
155 * >0 : queue_if_no_path enabled, turned off after polling n times
158 update_queue_mode_del_path(struct multipath *mpp)
160 if (--mpp->nr_active == 0 && mpp->no_path_retry > 0) {
163 * meaning of +1: retry_tick may be decremented in
164 * checkerloop before starting retry.
166 mpp->retry_tick = mpp->no_path_retry * conf->checkint + 1;
167 condlog(1, "%s: Entering recovery mode: max_retries=%d",
168 mpp->alias, mpp->no_path_retry);
170 condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
174 update_queue_mode_add_path(struct multipath *mpp)
176 if (mpp->nr_active++ == 0 && mpp->no_path_retry > 0) {
177 /* come back to normal mode from retry mode */
179 dm_queue_if_no_path(mpp->alias, 1);
180 condlog(2, "%s: queue_if_no_path enabled", mpp->alias);
181 condlog(1, "%s: Recovered to normal mode", mpp->alias);
183 condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
187 need_switch_pathgroup (struct multipath * mpp, int refresh)
189 struct pathgroup * pgp;
193 if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
197 * Refresh path priority values
200 vector_foreach_slot (mpp->pg, pgp, i)
201 vector_foreach_slot (pgp->paths, pp, j)
202 pathinfo(pp, conf->hwtable, DI_PRIO);
204 mpp->bestpg = select_path_group(mpp);
206 if (mpp->bestpg != mpp->nextpg)
213 switch_pathgroup (struct multipath * mpp)
215 dm_switchgroup(mpp->alias, mpp->bestpg);
216 condlog(2, "%s: switch to path group #%i",
217 mpp->alias, mpp->bestpg);
221 coalesce_maps(struct vectors *vecs, vector nmpv)
223 struct multipath * ompp;
224 vector ompv = vecs->mpvec;
227 condlog(3, "coalesce_maps vs = %u", VECTOR_SIZE(ompv));
228 condlog(3, "coalesce_maps vs = %u", VECTOR_SIZE(nmpv));
230 vector_foreach_slot (ompv, ompp, i) {
231 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
233 * remove all current maps not allowed by the
234 * current configuration
236 if (dm_flush_map(ompp->alias, DEFAULT_TARGET)) {
237 condlog(0, "%s: unable to flush devmap",
240 * may be just because the device is open
242 if (!vector_alloc_slot(nmpv))
245 vector_set_slot(nmpv, ompp);
246 setup_multipath(vecs, ompp);
248 if ((j = find_slot(ompv, (void *)ompp)) != -1)
249 vector_del_slot(ompv, j);
254 condlog(3, "%s devmap removed", ompp->alias);
261 update_multipath (struct vectors *vecs, char *mapname)
263 struct multipath *mpp;
264 struct pathgroup *pgp;
269 mpp = find_mp_by_alias(vecs->mpvec, mapname);
274 free_pgvec(mpp->pg, KEEP_PATHS);
277 if (setup_multipath(vecs, mpp))
278 goto out; /* mpp freed in setup_multipath */
281 * compare checkers states with DM states
283 vector_foreach_slot (mpp->pg, pgp, i) {
284 vector_foreach_slot (pgp->paths, pp, j) {
285 if (pp->dmstate != PSTATE_FAILED)
288 if (pp->state != PATH_DOWN) {
289 condlog(2, "%s: mark as failed", pp->dev_t);
290 pp->state = PATH_DOWN;
291 update_queue_mode_del_path(mpp);
295 * schedule the next check earlier
297 if (pp->tick > conf->checkint)
298 pp->tick = conf->checkint;
305 condlog(0, "failed to update multipath");
310 static sigset_t unblock_signals(void)
315 sigaddset(&set, SIGHUP);
316 sigaddset(&set, SIGUSR1);
317 pthread_sigmask(SIG_UNBLOCK, &set, &old);
322 * returns the reschedule delay
323 * negative means *stop*
326 waiteventloop (struct event_thread * waiter)
332 if (!waiter->event_nr)
333 waiter->event_nr = dm_geteventnr(waiter->mapname);
335 if (!(waiter->dmt = dm_task_create(DM_DEVICE_WAITEVENT))) {
336 condlog(0, "%s: devmap event #%i dm_task_create error",
337 waiter->mapname, waiter->event_nr);
341 if (!dm_task_set_name(waiter->dmt, waiter->mapname)) {
342 condlog(0, "%s: devmap event #%i dm_task_set_name error",
343 waiter->mapname, waiter->event_nr);
344 dm_task_destroy(waiter->dmt);
348 if (waiter->event_nr && !dm_task_set_event_nr(waiter->dmt,
350 condlog(0, "%s: devmap event #%i dm_task_set_event_nr error",
351 waiter->mapname, waiter->event_nr);
352 dm_task_destroy(waiter->dmt);
356 dm_task_no_open_count(waiter->dmt);
358 /* accept wait interruption */
359 set = unblock_signals();
361 /* interruption spits messages */
365 r = dm_task_run(waiter->dmt);
367 /* wait is over : event or interrupt */
368 pthread_sigmask(SIG_SETMASK, &set, NULL);
371 if (!r) /* wait interrupted by signal */
374 dm_task_destroy(waiter->dmt);
382 condlog(3, "%s: devmap event #%i",
383 waiter->mapname, waiter->event_nr);
388 * 1) a table reload, which means our mpp structure is
389 * obsolete : refresh it through update_multipath()
390 * 2) a path failed by DM : mark as such through
392 * 3) map has gone away : stop the thread.
393 * 4) a path reinstate : nothing to do
394 * 5) a switch group : nothing to do
396 pthread_cleanup_push(cleanup_lock, waiter->vecs->lock);
397 lock(waiter->vecs->lock);
398 r = update_multipath(waiter->vecs, waiter->mapname);
399 lock_cleanup_pop(waiter->vecs->lock);
402 return -1; /* stop the thread */
404 event_nr = dm_geteventnr(waiter->mapname);
406 if (waiter->event_nr == event_nr)
407 return 1; /* upon problem reschedule 1s later */
409 waiter->event_nr = event_nr;
411 return -1; /* never reach there */
415 waitevent (void * et)
418 struct event_thread *waiter;
420 mlockall(MCL_CURRENT | MCL_FUTURE);
422 waiter = (struct event_thread *)et;
423 pthread_cleanup_push(free_waiter, et);
426 r = waiteventloop(waiter);
434 pthread_cleanup_pop(1);
439 start_waiter_thread (struct multipath * mpp, struct vectors * vecs)
442 struct event_thread * wp;
447 if (pthread_attr_init(&attr))
450 pthread_attr_setstacksize(&attr, 32 * 1024);
451 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
458 mpp->waiter = (void *)wp;
459 strncpy(wp->mapname, mpp->alias, WWID_SIZE);
463 if (pthread_create(&wp->thread, &attr, waitevent, wp)) {
464 condlog(0, "%s: cannot create event checker", wp->mapname);
467 condlog(2, "%s: event checker started", wp->mapname);
474 condlog(0, "failed to start waiter thread");
479 flush_map(struct multipath * mpp, char * devname, struct vectors * vecs)
482 * clear references to this map before flushing so we can ignore
483 * the spurious uevent we may generate with the dm_flush_map call below
485 if (dm_flush_map(mpp->alias, DEFAULT_TARGET)) {
487 * May not really be an error -- if the map was already flushed
488 * from the device mapper by dmsetup(8) for instance.
490 condlog(0, "%s: can't flush", devname);
494 condlog(3, "%s: devmap %s removed", devname, mpp->alias);
496 orphan_paths(vecs->pathvec, mpp);
497 remove_map(mpp, vecs, stop_waiter_thread, 1);
503 uev_add_map (char * devname, struct vectors * vecs)
506 char dev_t[BLK_DEV_SIZE];
508 struct multipath * mpp;
510 if (sscanf(devname, "dm-%d", &minor) == 1 &&
511 !sysfs_get_dev(sysfs_path, devname, dev_t, BLK_DEV_SIZE) &&
512 sscanf(dev_t, "%d:%d", &major, &minor) == 2)
513 alias = dm_mapname(major, minor);
515 alias = STRDUP(devname);
520 if (dm_type(alias, DEFAULT_TARGET) <= 0) {
521 condlog(4, "%s: not a multipath map", alias);
526 mpp = find_mp_by_alias(vecs->mpvec, alias);
530 * Not really an error -- we generate our own uevent
531 * if we create a multipath mapped device as a result
534 condlog(0, "%s: spurious uevent, devmap already registered",
541 * now we can register the map
543 if ((mpp = add_map_without_path(vecs, minor, alias,
544 start_waiter_thread))) {
545 condlog(3, "%s devmap %s added", devname, alias);
549 condlog(0, "%s: uev_add_map failed", alias);
554 uev_remove_map (char * devname, struct vectors * vecs)
557 struct multipath * mpp;
559 if (sscanf(devname, "dm-%d", &minor) == 1)
560 mpp = find_mp_by_minor(vecs->mpvec, minor);
562 mpp = find_mp_by_alias(vecs->mpvec, devname);
565 condlog(3, "%s: devmap not registered, can't remove",
570 condlog(2, "remove %s devmap", mpp->alias);
571 remove_map(mpp, vecs, stop_waiter_thread, 1);
577 uev_add_path (char * devname, struct vectors * vecs)
579 struct multipath * mpp;
581 char empty_buff[WWID_SIZE] = {0};
583 condlog(3, "%s: uev_add_path", devname);
585 pp = find_path_by_dev(vecs->pathvec, devname);
587 condlog(0, "%s: spurious uevent, path already in pathvec, %p",
590 * allow reconfig of orphaned path here
592 if (pp->mpp) return 1;
596 * get path vital state
598 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
600 condlog(0, "%s: failed to store path info", devname);
603 pp->checkint = conf->checkint;
607 * need path UID to go any further
609 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
610 condlog(0, "%s: failed to get path uid", devname);
611 return 1; /* leave path added to pathvec */
614 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
617 if (adopt_paths(vecs->pathvec, mpp))
618 return 1; /* leave path added to pathvec */
620 verify_paths(mpp, vecs, NULL);
621 condlog(0, "%s: ownership set to %s", pp->dev_t, mpp->alias);
622 mpp->action = ACT_RELOAD;
625 if ((mpp = add_map_with_path(vecs, pp, 1)))
626 mpp->action = ACT_CREATE;
628 return 1; /* leave path added to pathvec */
632 * push the map to the device-mapper
634 if (setup_map(mpp)) {
635 condlog(0, "%s: failed to setup map for addition of new "
636 "path %s", mpp->alias, devname);
640 * reload the map for the multipath mapped device
642 if (domap(mpp) <= 0) {
643 condlog(0, "%s: failed in domap for addition of new "
644 "path %s", mpp->alias, devname);
646 * deal with asynchronous uevents :((
648 if (mpp->action == ACT_RELOAD) {
649 condlog(0, "%s: uev_add_path sleep", mpp->alias);
651 update_mpp_paths(mpp);
659 * update our state from kernel regardless of create or reload
661 if (setup_multipath(vecs, mpp))
664 if (mpp->action == ACT_CREATE &&
665 start_waiter_thread(mpp, vecs))
668 condlog(3, "%s path added to devmap %s", devname, mpp->alias);
672 remove_map(mpp, vecs, NULL, 1);
677 uev_remove_path (char * devname, struct vectors * vecs)
679 struct multipath * mpp;
684 condlog(3, "%s: uev_remove_path", devname);
686 pp = find_path_by_dev(vecs->pathvec, devname);
688 condlog(0, "%s: spurious uevent, path not in pathvec", devname);
693 * avoid referring to the map of an orphanned path
695 if ((mpp = pp->mpp)) {
698 * remove the map IFF removing the last path
700 if (pathcount(mpp, PATH_WILD) > 1) {
701 vector rpvec = vector_alloc();
706 * transform the mp->pg vector of vectors of paths
707 * into a mp->params string to feed the device-mapper
709 update_mpp_paths(mpp);
710 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
711 vector_del_slot(mpp->paths, i);
714 redo = verify_paths(mpp, vecs, rpvec);
715 if (VECTOR_SIZE(mpp->paths) == 0) {
716 char alias[WWID_SIZE];
719 * flush_map will fail if the device is open
721 strncpy(alias, mpp->alias, WWID_SIZE);
722 if (flush_map(mpp, devname, vecs)) {
724 vector_foreach_slot(rpvec, mypp, i)
725 if (store_path(mpp->paths, mypp))
729 vector_foreach_slot(rpvec, mypp, i) {
730 if ((j = find_slot(vecs->pathvec,
731 (void *)mypp)) != -1) {
732 vector_del_slot(vecs->pathvec, j);
736 condlog(3, "%s: removed map after removing"
737 " multiple paths", alias);
741 vector_foreach_slot(rpvec, mypp, i) {
742 if ((j = find_slot(vecs->pathvec,
743 (void *)mypp)) != -1) {
744 vector_del_slot(vecs->pathvec, j);
749 if (setup_map(mpp)) {
750 condlog(0, "%s: failed to setup map for"
751 " removal of path %s", mpp->alias, devname);
752 free_pathvec(rpvec, KEEP_PATHS);
758 mpp->action = ACT_RELOAD;
759 if (domap(mpp) <= 0) {
760 condlog(0, "%s: failed in domap for "
761 "removal of path %s",
762 mpp->alias, devname);
764 * deal with asynchronous uevents :((
767 condlog(3, "%s: uev_remove_path sleep",
771 update_mpp_paths(mpp);
772 free_pathvec(rpvec, KEEP_PATHS);
773 rpvec = vector_alloc();
777 * update our state from kernel
779 if (setup_multipath(vecs, mpp)) {
780 free_pathvec(rpvec, KEEP_PATHS);
784 condlog(3, "%s path removed from devmap %s",
785 devname, mpp->alias);
787 free_pathvec(rpvec, KEEP_PATHS);
790 char alias[WWID_SIZE];
793 * flush_map will fail if the device is open
795 strncpy(alias, mpp->alias, WWID_SIZE);
796 if (flush_map(mpp, devname, vecs)) {
800 condlog(3, "%s: removed map", alias);
805 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
806 vector_del_slot(vecs->pathvec, i);
813 remove_map(mpp, vecs, stop_waiter_thread, 1);
818 show_paths (char ** r, int * len, struct vectors * vecs)
824 int maxlen = INITIAL_REPLY_LEN;
827 get_path_layout(vecs->pathvec);
828 reply = MALLOC(maxlen);
836 if (VECTOR_SIZE(vecs->pathvec) > 0)
837 c += snprint_path_header(c, reply + maxlen - c,
840 vector_foreach_slot(vecs->pathvec, pp, i)
841 c += snprint_path(c, reply + maxlen - c,
842 PRINT_PATH_CHECKER, pp);
844 again = ((c - reply) == (maxlen - 1));
847 reply = REALLOC(reply, maxlen *= 2);
851 *len = (int)(c - reply + 1);
856 show_maps (char ** r, int *len, struct vectors * vecs)
859 struct multipath * mpp;
862 int maxlen = INITIAL_REPLY_LEN;
865 get_map_layout(vecs->mpvec);
866 reply = MALLOC(maxlen);
873 if (VECTOR_SIZE(vecs->mpvec) > 0)
874 c += snprint_map_header(c, reply + maxlen - c,
877 vector_foreach_slot(vecs->mpvec, mpp, i)
878 c += snprint_map(c, reply + maxlen - c,
879 PRINT_MAP_FAILBACK, mpp);
881 again = ((c - reply) == (maxlen - 1));
884 reply = REALLOC(reply, maxlen *= 2);
887 *len = (int)(c - reply + 1);
892 dump_pathvec (char ** r, int * len, struct vectors * vecs)
899 *len = VECTOR_SIZE(vecs->pathvec) * sizeof(struct path);
900 reply = (char *)MALLOC(*len);
908 vector_foreach_slot (vecs->pathvec, pp, i) {
909 memcpy((void *)p, pp, sizeof(struct path));
910 p += sizeof(struct path);
913 /* return negative to hint caller not to add "ok" to the dump */
918 map_discovery (struct vectors * vecs)
921 struct multipath * mpp;
923 if (dm_get_maps(vecs->mpvec, "multipath"))
926 vector_foreach_slot (vecs->mpvec, mpp, i)
927 if (setup_multipath(vecs, mpp))
934 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
936 struct vectors * vecs;
941 vecs = (struct vectors *)trigger_data;
943 pthread_cleanup_push(cleanup_lock, vecs->lock);
946 r = parse_cmd(str, reply, len, vecs);
949 *reply = STRDUP("fail\n");
950 *len = strlen(*reply) + 1;
953 else if (!r && *len == 0) {
954 *reply = STRDUP("ok\n");
955 *len = strlen(*reply) + 1;
958 /* else if (r < 0) leave *reply alone */
960 lock_cleanup_pop(vecs->lock);
965 uev_discard(char * devpath)
970 * keep only block devices, discard partitions
972 if (sscanf(devpath, "/block/%10s", a) != 1 ||
973 sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
974 condlog(4, "discard event on %s", devpath);
981 uev_trigger (struct uevent * uev, void * trigger_data)
985 struct vectors * vecs;
987 vecs = (struct vectors *)trigger_data;
989 if (uev_discard(uev->devpath))
992 basename(uev->devpath, devname);
996 * device map add/remove event
998 if (!strncmp(devname, "dm-", 3)) {
999 if (!strncmp(uev->action, "add", 3)) {
1000 r = uev_add_map(devname, vecs);
1003 if (!strncmp(uev->action, "remove", 6)) {
1004 r = uev_remove_map(devname, vecs);
1011 * path add/remove event
1013 if (blacklist(conf->blist, devname))
1016 if (!strncmp(uev->action, "add", 3)) {
1017 r = uev_add_path(devname, vecs);
1020 if (!strncmp(uev->action, "remove", 6)) {
1021 r = uev_remove_path(devname, vecs);
1031 ueventloop (void * ap)
1033 if (uevent_listen(&uev_trigger, ap))
1034 fprintf(stderr, "error starting uevent listener");
1040 uxlsnrloop (void * ap)
1045 if (alloc_handlers())
1048 add_handler(LIST+PATHS, cli_list_paths);
1049 add_handler(LIST+MAPS, cli_list_maps);
1050 add_handler(ADD+PATH, cli_add_path);
1051 add_handler(DEL+PATH, cli_del_path);
1052 add_handler(ADD+MAP, cli_add_map);
1053 add_handler(DEL+MAP, cli_del_map);
1054 add_handler(SWITCH+MAP+GROUP, cli_switch_group);
1055 add_handler(DUMP+PATHVEC, cli_dump_pathvec);
1056 add_handler(RECONFIGURE, cli_reconfigure);
1057 add_handler(SUSPEND+MAP, cli_suspend);
1058 add_handler(RESUME+MAP, cli_resume);
1059 add_handler(REINSTATE+PATH, cli_reinstate);
1060 add_handler(FAIL+PATH, cli_fail);
1062 uxsock_listen(&uxsock_trigger, ap);
1068 exit_daemon (int status)
1071 fprintf(stderr, "bad exit status. see daemon.log\n");
1073 condlog(3, "unlink pidfile");
1074 unlink(DEFAULT_PIDFILE);
1077 pthread_cond_signal(&exit_cond);
1078 unlock(&exit_mutex);
1084 fail_path (struct path * pp)
1089 condlog(2, "checker failed path %s in map %s",
1090 pp->dev_t, pp->mpp->alias);
1092 dm_fail_path(pp->mpp->alias, pp->dev_t);
1093 update_queue_mode_del_path(pp->mpp);
1097 * caller must have locked the path list before calling that function
1100 reinstate_path (struct path * pp)
1105 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
1106 condlog(0, "%s: reinstate failed", pp->dev_t);
1108 condlog(2, "%s: reinstated", pp->dev_t);
1109 update_queue_mode_add_path(pp->mpp);
1114 enable_group(struct path * pp)
1116 struct pathgroup * pgp;
1119 * if path is added through uev_add_path, pgindex can be unset.
1120 * next update_strings() will set it, upon map reload event.
1122 * we can safely return here, because upon map reload, all
1123 * PG will be enabled.
1125 if (!pp->mpp->pg || !pp->pgindex)
1128 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1130 if (pgp->status == PGSTATE_DISABLED) {
1131 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1132 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1137 mpvec_garbage_collector (struct vectors * vecs)
1139 struct multipath * mpp;
1142 vector_foreach_slot (vecs->mpvec, mpp, i) {
1143 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1144 condlog(2, "%s: remove dead map", mpp->alias);
1145 remove_map(mpp, vecs, stop_waiter_thread, 1);
1152 defered_failback_tick (vector mpvec)
1154 struct multipath * mpp;
1157 vector_foreach_slot (mpvec, mpp, i) {
1159 * defered failback getting sooner
1161 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1162 mpp->failback_tick--;
1164 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1165 switch_pathgroup(mpp);
1171 retry_count_tick(vector mpvec)
1173 struct multipath *mpp;
1176 vector_foreach_slot (mpvec, mpp, i) {
1177 if (mpp->retry_tick) {
1178 condlog(4, "%s: Retrying.. No active path", mpp->alias);
1179 if(--mpp->retry_tick == 0) {
1180 dm_queue_if_no_path(mpp->alias, 0);
1181 condlog(2, "%s: Disable queueing", mpp->alias);
1188 checkerloop (void *ap)
1190 struct vectors *vecs;
1194 char checker_msg[MAX_CHECKER_MSG_SIZE];
1196 mlockall(MCL_CURRENT | MCL_FUTURE);
1198 memset(checker_msg, 0, MAX_CHECKER_MSG_SIZE);
1199 vecs = (struct vectors *)ap;
1201 condlog(2, "path checkers start up");
1204 * init the path check interval
1206 vector_foreach_slot (vecs->pathvec, pp, i) {
1207 pp->checkint = conf->checkint;
1211 pthread_cleanup_push(cleanup_lock, vecs->lock);
1215 vector_foreach_slot (vecs->pathvec, pp, i) {
1219 if (pp->tick && --pp->tick)
1220 continue; /* don't check this path yet */
1223 * provision a next check soonest,
1224 * in case we exit abnormaly from here
1226 pp->tick = conf->checkint;
1229 pathinfo(pp, conf->hwtable, DI_SYSFS);
1234 condlog(0, "%s: checkfn is void", pp->dev);
1237 newstate = pp->checkfn(pp->fd, checker_msg,
1238 &pp->checker_context);
1241 condlog(2, "%s: unusable path", pp->dev);
1242 pathinfo(pp, conf->hwtable, 0);
1246 if (newstate != pp->state) {
1247 pp->state = newstate;
1248 LOG_MSG(1, checker_msg);
1251 * upon state change, reset the checkint
1252 * to the shortest delay
1254 pp->checkint = conf->checkint;
1256 if (newstate == PATH_DOWN ||
1257 newstate == PATH_SHAKY ||
1258 update_multipath_strings(pp->mpp,
1261 * proactively fail path in the DM
1266 * cancel scheduled failback
1268 pp->mpp->failback_tick = 0;
1274 * reinstate this path
1279 * schedule [defered] failback
1281 if (pp->mpp->pgfailback > 0)
1282 pp->mpp->failback_tick =
1283 pp->mpp->pgfailback + 1;
1284 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
1285 need_switch_pathgroup(pp->mpp, 1))
1286 switch_pathgroup(pp->mpp);
1289 * if at least one path is up in a group, and
1290 * the group is disabled, re-enable it
1292 if (newstate == PATH_UP)
1295 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1296 LOG_MSG(4, checker_msg);
1298 * double the next check delay.
1299 * max at conf->max_checkint
1301 if (pp->checkint < (conf->max_checkint / 2))
1302 pp->checkint = 2 * pp->checkint;
1304 pp->checkint = conf->max_checkint;
1306 pp->tick = pp->checkint;
1307 condlog(4, "%s: delay next check %is",
1308 pp->dev_t, pp->tick);
1311 pp->state = newstate;
1314 * path prio refreshing
1316 condlog(4, "path prio refresh");
1317 pathinfo(pp, conf->hwtable, DI_PRIO);
1319 if (need_switch_pathgroup(pp->mpp, 0)) {
1320 if (pp->mpp->pgfailback > 0)
1321 pp->mpp->failback_tick =
1322 pp->mpp->pgfailback + 1;
1323 else if (pp->mpp->pgfailback ==
1324 -FAILBACK_IMMEDIATE)
1325 switch_pathgroup(pp->mpp);
1328 defered_failback_tick(vecs->mpvec);
1329 retry_count_tick(vecs->mpvec);
1334 condlog(4, "map garbage collection");
1335 mpvec_garbage_collector(vecs);
1339 lock_cleanup_pop(vecs->lock);
1346 configure (struct vectors * vecs, int start_waiters)
1348 struct multipath * mpp;
1353 if (!(vecs->pathvec = vector_alloc()))
1356 if (!(vecs->mpvec = vector_alloc()))
1359 if (!(mpvec = vector_alloc()))
1363 * probe for current path (from sysfs) and map (from dm) sets
1365 path_discovery(vecs->pathvec, conf, DI_ALL);
1367 vector_foreach_slot (vecs->pathvec, pp, i)
1368 pp->checkint = conf->checkint;
1370 if (map_discovery(vecs))
1374 * create new set of maps & push changed ones into dm
1376 if (coalesce_paths(vecs, mpvec))
1380 * may need to remove some maps which are no longer relevant
1381 * e.g., due to blacklist changes in conf file
1383 if (coalesce_maps(vecs, mpvec))
1386 if (conf->verbosity > 2)
1387 vector_foreach_slot(mpvec, mpp, i)
1391 * purge dm of old maps
1393 remove_maps(vecs, NULL);
1396 * save new set of maps formed by considering current path state
1398 vecs->mpvec = mpvec;
1401 * start dm event waiter threads for these new maps
1403 vector_foreach_slot(vecs->mpvec, mpp, i) {
1404 if (setup_multipath(vecs, mpp))
1407 if (start_waiter_thread(mpp, vecs))
1414 reconfigure (struct vectors * vecs)
1416 struct config * old = conf;
1418 condlog(0, "reconfigure");
1421 * free old map and path vectors ... they use old conf state
1423 if (VECTOR_SIZE(vecs->mpvec))
1424 remove_maps(vecs, stop_waiter_thread);
1426 if (VECTOR_SIZE(vecs->pathvec))
1427 free_pathvec(vecs->pathvec, FREE_PATHS);
1431 if (load_config(DEFAULT_CONFIGFILE))
1434 conf->verbosity = old->verbosity;
1436 if (!conf->checkint) {
1437 conf->checkint = CHECKINT;
1438 conf->max_checkint = MAX_CHECKINT;
1445 static struct vectors *
1448 struct vectors * vecs;
1450 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1456 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1461 vecs->pathvec = vector_alloc();
1466 vecs->mpvec = vector_alloc();
1471 pthread_mutex_init(vecs->lock, NULL);
1476 vector_free(vecs->pathvec);
1481 condlog(0, "failed to init paths");
1486 signal_set(int signo, void (*func) (int))
1489 struct sigaction sig;
1490 struct sigaction osig;
1492 sig.sa_handler = func;
1493 sigemptyset(&sig.sa_mask);
1496 r = sigaction(signo, &sig, &osig);
1501 return (osig.sa_handler);
1507 condlog(3, "SIGHUP received");
1511 unlock(gvecs->lock);
1514 dbg_free_final(NULL);
1527 condlog(3, "SIGUSR1 received");
1533 signal_set(SIGHUP, sighup);
1534 signal_set(SIGUSR1, sigusr1);
1535 signal_set(SIGINT, sigend);
1536 signal_set(SIGTERM, sigend);
1537 signal_set(SIGKILL, sigend);
1544 static struct sched_param sched_param = {
1548 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1551 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1556 set_oom_adj (int val)
1560 fp = fopen("/proc/self/oom_adj", "w");
1565 fprintf(fp, "%i", val);
1570 child (void * param)
1572 pthread_t check_thr, uevent_thr, uxlsnr_thr;
1573 pthread_attr_t attr;
1574 struct vectors * vecs;
1576 mlockall(MCL_CURRENT | MCL_FUTURE);
1581 condlog(2, "--------start up--------");
1582 condlog(2, "read " DEFAULT_CONFIGFILE);
1584 if (load_config(DEFAULT_CONFIGFILE))
1587 setlogmask(LOG_UPTO(conf->verbosity + 3));
1590 * fill the voids left in the config file
1592 if (!conf->checkint) {
1593 conf->checkint = CHECKINT;
1594 conf->max_checkint = MAX_CHECKINT;
1597 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1606 vecs = gvecs = init_vecs();
1611 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
1612 condlog(0, "can not find sysfs mount point");
1617 * fetch and configure both paths and multipaths
1619 if (configure(vecs, 1)) {
1620 condlog(0, "failure during configuration");
1627 pthread_attr_init(&attr);
1628 pthread_attr_setstacksize(&attr, 64 * 1024);
1629 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1631 pthread_create(&check_thr, &attr, checkerloop, vecs);
1632 pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1633 pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1635 pthread_cond_wait(&exit_cond, &exit_mutex);
1641 remove_maps(vecs, stop_waiter_thread);
1642 free_pathvec(vecs->pathvec, FREE_PATHS);
1644 pthread_cancel(check_thr);
1645 pthread_cancel(uevent_thr);
1646 pthread_cancel(uxlsnr_thr);
1650 free_handlers(handlers);
1655 pthread_mutex_destroy(vecs->lock);
1663 condlog(2, "--------shut down-------");
1672 dbg_free_final(NULL);
1684 if( (pid = fork()) < 0){
1685 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1693 if ( (pid = fork()) < 0)
1694 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1698 in_fd = open("/dev/null", O_RDONLY);
1700 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1704 out_fd = open("/dev/console", O_WRONLY);
1706 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1711 close(STDIN_FILENO);
1713 close(STDOUT_FILENO);
1715 close(STDERR_FILENO);
1726 main (int argc, char *argv[])
1728 extern char *optarg;
1735 if (getuid() != 0) {
1736 fprintf(stderr, "need to be root\n");
1740 /* make sure we don't lock any path */
1742 umask(umask(077) | 022);
1744 conf = alloc_config();
1749 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1753 //debug=1; /* ### comment me out ### */
1756 if (sizeof(optarg) > sizeof(char *) ||
1757 !isdigit(optarg[0]))
1760 conf->verbosity = atoi(optarg);
1783 return (child(NULL));