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 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);
255 condlog(3, "%s devmap removed", ompp->alias);
263 update_multipath (struct vectors *vecs, char *mapname)
265 struct multipath *mpp;
266 struct pathgroup *pgp;
271 mpp = find_mp_by_alias(vecs->mpvec, mapname);
276 free_pgvec(mpp->pg, KEEP_PATHS);
279 if (setup_multipath(vecs, mpp))
280 goto out; /* mpp freed in setup_multipath */
283 * compare checkers states with DM states
285 vector_foreach_slot (mpp->pg, pgp, i) {
286 vector_foreach_slot (pgp->paths, pp, j) {
287 if (pp->dmstate != PSTATE_FAILED)
290 if (pp->state != PATH_DOWN) {
291 condlog(2, "%s: mark as failed", pp->dev_t);
292 pp->state = PATH_DOWN;
293 update_queue_mode_del_path(mpp);
297 * schedule the next check earlier
299 if (pp->tick > conf->checkint)
300 pp->tick = conf->checkint;
307 condlog(0, "failed to update multipath");
312 static sigset_t unblock_signals(void)
317 sigaddset(&set, SIGHUP);
318 sigaddset(&set, SIGUSR1);
319 pthread_sigmask(SIG_UNBLOCK, &set, &old);
324 * returns the reschedule delay
325 * negative means *stop*
328 waiteventloop (struct event_thread * waiter)
334 if (!waiter->event_nr)
335 waiter->event_nr = dm_geteventnr(waiter->mapname);
337 if (!(waiter->dmt = dm_task_create(DM_DEVICE_WAITEVENT))) {
338 condlog(0, "%s: devmap event #%i dm_task_create error",
339 waiter->mapname, waiter->event_nr);
343 if (!dm_task_set_name(waiter->dmt, waiter->mapname)) {
344 condlog(0, "%s: devmap event #%i dm_task_set_name error",
345 waiter->mapname, waiter->event_nr);
346 dm_task_destroy(waiter->dmt);
350 if (waiter->event_nr && !dm_task_set_event_nr(waiter->dmt,
352 condlog(0, "%s: devmap event #%i dm_task_set_event_nr error",
353 waiter->mapname, waiter->event_nr);
354 dm_task_destroy(waiter->dmt);
358 dm_task_no_open_count(waiter->dmt);
360 /* accept wait interruption */
361 set = unblock_signals();
363 /* interruption spits messages */
367 r = dm_task_run(waiter->dmt);
369 /* wait is over : event or interrupt */
370 pthread_sigmask(SIG_SETMASK, &set, NULL);
373 if (!r) /* wait interrupted by signal */
376 dm_task_destroy(waiter->dmt);
384 condlog(3, "%s: devmap event #%i",
385 waiter->mapname, waiter->event_nr);
390 * 1) a table reload, which means our mpp structure is
391 * obsolete : refresh it through update_multipath()
392 * 2) a path failed by DM : mark as such through
394 * 3) map has gone away : stop the thread.
395 * 4) a path reinstate : nothing to do
396 * 5) a switch group : nothing to do
398 pthread_cleanup_push(cleanup_lock, waiter->vecs->lock);
399 lock(waiter->vecs->lock);
400 r = update_multipath(waiter->vecs, waiter->mapname);
401 lock_cleanup_pop(waiter->vecs->lock);
404 return -1; /* stop the thread */
406 event_nr = dm_geteventnr(waiter->mapname);
408 if (waiter->event_nr == event_nr)
409 return 1; /* upon problem reschedule 1s later */
411 waiter->event_nr = event_nr;
413 return -1; /* never reach there */
417 waitevent (void * et)
420 struct event_thread *waiter;
422 mlockall(MCL_CURRENT | MCL_FUTURE);
424 waiter = (struct event_thread *)et;
425 pthread_cleanup_push(free_waiter, et);
428 r = waiteventloop(waiter);
436 pthread_cleanup_pop(1);
441 start_waiter_thread (struct multipath * mpp, struct vectors * vecs)
444 struct event_thread * wp;
449 if (pthread_attr_init(&attr))
452 pthread_attr_setstacksize(&attr, 32 * 1024);
453 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
460 mpp->waiter = (void *)wp;
461 strncpy(wp->mapname, mpp->alias, WWID_SIZE);
465 if (pthread_create(&wp->thread, &attr, waitevent, wp)) {
466 condlog(0, "%s: cannot create event checker", wp->mapname);
469 condlog(2, "%s: event checker started", wp->mapname);
476 condlog(0, "failed to start waiter thread");
481 flush_map(struct multipath * mpp, struct vectors * vecs)
484 * clear references to this map before flushing so we can ignore
485 * the spurious uevent we may generate with the dm_flush_map call below
487 if (dm_flush_map(mpp->alias, DEFAULT_TARGET)) {
489 * May not really be an error -- if the map was already flushed
490 * from the device mapper by dmsetup(8) for instance.
492 condlog(0, "%s: can't flush", mpp->alias);
497 condlog(3, "%s: devmap removed", mpp->alias);
500 orphan_paths(vecs->pathvec, mpp);
501 remove_map(mpp, vecs, stop_waiter_thread, 1);
507 uev_add_map (char * devname, struct vectors * vecs)
510 char dev_t[BLK_DEV_SIZE];
513 struct multipath * mpp;
517 condlog(3, "%s: uev_add_map", devname);
519 if (sscanf(devname, "dm-%d", &minor) == 1 &&
520 !sysfs_get_dev(sysfs_path, devname, dev_t, BLK_DEV_SIZE) &&
521 sscanf(dev_t, "%d:%d", &major, &minor) == 2)
522 alias = dm_mapname(major, minor);
524 alias = STRDUP(devname);
529 map_present = dm_map_present(alias);
531 if (map_present && dm_type(alias, DEFAULT_TARGET) <= 0) {
532 condlog(4, "%s: not a multipath map", alias);
537 mpp = find_mp_by_alias(vecs->mpvec, alias);
541 * Not really an error -- we generate our own uevent
542 * if we create a multipath mapped device as a result
545 condlog(0, "%s: spurious uevent, devmap already registered",
552 * now we can register the map
554 if (map_present && (mpp = add_map_without_path(vecs, minor, alias,
555 start_waiter_thread))) {
556 condlog(3, "%s: devmap %s added", alias, devname);
559 refwwid = get_refwwid(devname, DEV_DEVMAP, vecs->pathvec);
562 r = coalesce_paths(vecs, NULL, refwwid);
567 condlog(3, "%s devmap %s added", devname, alias);
569 condlog(0, "%s: uev_add_map failed", alias);
577 uev_remove_map (char * devname, struct vectors * vecs)
580 struct multipath * mpp;
582 condlog(3, "%s: uev_remove_map", devname);
584 if (sscanf(devname, "dm-%d", &minor) == 1)
585 mpp = find_mp_by_minor(vecs->mpvec, minor);
587 mpp = find_mp_by_alias(vecs->mpvec, devname);
590 condlog(3, "%s: devmap not registered, can't remove",
595 condlog(2, "%s: remove devmap", mpp->alias);
596 flush_map(mpp, vecs);
602 uev_umount_map (char * devname, struct vectors * vecs)
605 struct multipath * mpp;
607 condlog(3, "%s: uev_umount_map", devname);
609 if (sscanf(devname, "dm-%d", &minor) == 1)
610 mpp = find_mp_by_minor(vecs->mpvec, minor);
612 mpp = find_mp_by_alias(vecs->mpvec, devname);
617 update_mpp_paths(mpp, vecs->pathvec);
618 verify_paths(mpp, vecs, NULL);
620 if (!VECTOR_SIZE(mpp->paths))
621 flush_map(mpp, vecs);
627 uev_add_path (char * devname, struct vectors * vecs)
629 struct multipath * mpp;
631 char empty_buff[WWID_SIZE] = {0};
633 condlog(3, "%s: uev_add_path", devname);
635 pp = find_path_by_dev(vecs->pathvec, devname);
637 condlog(0, "%s: spurious uevent, path already in pathvec, %p",
640 * allow reconfig of orphaned path here
642 if (pp->mpp) return 1;
646 * get path vital state
648 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
650 condlog(0, "%s: failed to store path info", devname);
653 pp->checkint = conf->checkint;
657 * need path UID to go any further
659 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
660 condlog(0, "%s: failed to get path uid", devname);
661 return 1; /* leave path added to pathvec */
664 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
667 if (adopt_paths(vecs->pathvec, mpp))
668 return 1; /* leave path added to pathvec */
670 verify_paths(mpp, vecs, NULL);
671 condlog(0, "%s: ownership set to %s", pp->dev_t, mpp->alias);
672 mpp->action = ACT_RELOAD;
675 if ((mpp = add_map_with_path(vecs, pp, 1)))
676 mpp->action = ACT_CREATE;
678 return 1; /* leave path added to pathvec */
682 * push the map to the device-mapper
684 if (setup_map(mpp)) {
685 condlog(0, "%s: failed to setup map for addition of new "
686 "path %s", mpp->alias, devname);
690 * reload the map for the multipath mapped device
692 if (domap(mpp) <= 0) {
693 condlog(0, "%s: failed in domap for addition of new "
694 "path %s", mpp->alias, devname);
696 * deal with asynchronous uevents :((
698 if (mpp->action == ACT_RELOAD) {
699 condlog(0, "%s: uev_add_path sleep", mpp->alias);
701 update_mpp_paths(mpp, vecs->pathvec);
710 * update our state from kernel regardless of create or reload
712 if (setup_multipath(vecs, mpp))
715 if (mpp->action == ACT_CREATE &&
716 start_waiter_thread(mpp, vecs))
719 condlog(3, "%s path added to devmap %s", devname, mpp->alias);
723 remove_map(mpp, vecs, NULL, 1);
728 uev_remove_path (char * devname, struct vectors * vecs)
730 struct multipath * mpp;
735 condlog(3, "%s: uev_remove_path", devname);
737 pp = find_path_by_dev(vecs->pathvec, devname);
739 condlog(0, "%s: spurious uevent, path not in pathvec", devname);
744 * avoid referring to the map of an orphanned path
746 if ((mpp = pp->mpp)) {
749 * remove the map IFF removing the last path
751 if (pathcount(mpp, PATH_WILD) > 1) {
752 vector rpvec = vector_alloc();
755 * transform the mp->pg vector of vectors of paths
756 * into a mp->params string to feed the device-mapper
758 update_mpp_paths(mpp, vecs->pathvec);
759 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
760 vector_del_slot(mpp->paths, i);
762 if (VECTOR_SIZE(mpp->paths) == 0) {
763 char alias[WWID_SIZE];
766 * flush_map will fail if the device is open
768 strncpy(alias, mpp->alias, WWID_SIZE);
769 if (flush_map(mpp, vecs))
772 condlog(3, "%s: removed map after removing"
773 " multiple paths", alias);
776 if (setup_map(mpp)) {
777 condlog(0, "%s: failed to setup map for"
778 " removal of path %s", mpp->alias, devname);
779 free_pathvec(rpvec, KEEP_PATHS);
785 mpp->action = ACT_RELOAD;
786 if (domap(mpp) <= 0) {
787 condlog(0, "%s: failed in domap for "
788 "removal of path %s",
789 mpp->alias, devname);
791 * Delete path from pathvec so that
792 * update_mpp_paths wont find it later
793 * when/if another path is removed.
795 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
796 vector_del_slot(vecs->pathvec, i);
801 * update our state from kernel
803 if (setup_multipath(vecs, mpp)) {
804 free_pathvec(rpvec, KEEP_PATHS);
808 condlog(3, "%s path removed from devmap %s",
809 devname, mpp->alias);
811 free_pathvec(rpvec, KEEP_PATHS);
814 char alias[WWID_SIZE];
817 * flush_map will fail if the device is open
819 strncpy(alias, mpp->alias, WWID_SIZE);
820 if (flush_map(mpp, vecs))
823 condlog(3, "%s: removed map", alias);
828 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
829 vector_del_slot(vecs->pathvec, i);
836 remove_map(mpp, vecs, stop_waiter_thread, 1);
841 show_paths (char ** r, int * len, struct vectors * vecs)
847 int maxlen = INITIAL_REPLY_LEN;
850 get_path_layout(vecs->pathvec);
851 reply = MALLOC(maxlen);
859 if (VECTOR_SIZE(vecs->pathvec) > 0)
860 c += snprint_path_header(c, reply + maxlen - c,
863 vector_foreach_slot(vecs->pathvec, pp, i)
864 c += snprint_path(c, reply + maxlen - c,
865 PRINT_PATH_CHECKER, pp);
867 again = ((c - reply) == (maxlen - 1));
870 reply = REALLOC(reply, maxlen *= 2);
874 *len = (int)(c - reply + 1);
879 show_maps (char ** r, int *len, struct vectors * vecs)
882 struct multipath * mpp;
885 int maxlen = INITIAL_REPLY_LEN;
888 get_map_layout(vecs->mpvec);
889 reply = MALLOC(maxlen);
896 if (VECTOR_SIZE(vecs->mpvec) > 0)
897 c += snprint_map_header(c, reply + maxlen - c,
900 vector_foreach_slot(vecs->mpvec, mpp, i)
901 c += snprint_map(c, reply + maxlen - c,
902 PRINT_MAP_FAILBACK, mpp);
904 again = ((c - reply) == (maxlen - 1));
907 reply = REALLOC(reply, maxlen *= 2);
910 *len = (int)(c - reply + 1);
915 dump_pathvec (char ** r, int * len, struct vectors * vecs)
922 *len = VECTOR_SIZE(vecs->pathvec) * sizeof(struct path);
923 reply = (char *)MALLOC(*len);
931 vector_foreach_slot (vecs->pathvec, pp, i) {
932 memcpy((void *)p, pp, sizeof(struct path));
933 p += sizeof(struct path);
936 /* return negative to hint caller not to add "ok" to the dump */
941 map_discovery (struct vectors * vecs)
944 struct multipath * mpp;
946 if (dm_get_maps(vecs->mpvec, "multipath"))
949 vector_foreach_slot (vecs->mpvec, mpp, i)
950 if (setup_multipath(vecs, mpp))
957 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
959 struct vectors * vecs;
964 vecs = (struct vectors *)trigger_data;
966 pthread_cleanup_push(cleanup_lock, vecs->lock);
969 r = parse_cmd(str, reply, len, vecs);
972 *reply = STRDUP("fail\n");
973 *len = strlen(*reply) + 1;
976 else if (!r && *len == 0) {
977 *reply = STRDUP("ok\n");
978 *len = strlen(*reply) + 1;
981 /* else if (r < 0) leave *reply alone */
983 lock_cleanup_pop(vecs->lock);
988 uev_discard(char * devpath)
993 * keep only block devices, discard partitions
995 if (sscanf(devpath, "/block/%10s", a) != 1 ||
996 sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
997 condlog(4, "discard event on %s", devpath);
1004 uev_trigger (struct uevent * uev, void * trigger_data)
1008 struct vectors * vecs;
1010 vecs = (struct vectors *)trigger_data;
1012 if (uev_discard(uev->devpath))
1015 basename(uev->devpath, devname);
1019 * device map add/remove event
1021 if (!strncmp(devname, "dm-", 3)) {
1022 if (!strncmp(uev->action, "add", 3)) {
1023 r = uev_add_map(devname, vecs);
1026 if (!strncmp(uev->action, "remove", 6)) {
1027 r = uev_remove_map(devname, vecs);
1030 if (!strncmp(uev->action, "umount", 6)) {
1031 r = uev_umount_map(devname, vecs);
1038 * path add/remove event
1040 if (blacklist(conf->blist, devname))
1043 if (!strncmp(uev->action, "add", 3)) {
1044 r = uev_add_path(devname, vecs);
1047 if (!strncmp(uev->action, "remove", 6)) {
1048 r = uev_remove_path(devname, vecs);
1058 ueventloop (void * ap)
1060 if (uevent_listen(&uev_trigger, ap))
1061 fprintf(stderr, "error starting uevent listener");
1067 uxlsnrloop (void * ap)
1072 if (alloc_handlers())
1075 add_handler(LIST+PATHS, cli_list_paths);
1076 add_handler(LIST+MAPS, cli_list_maps);
1077 add_handler(ADD+PATH, cli_add_path);
1078 add_handler(DEL+PATH, cli_del_path);
1079 add_handler(ADD+MAP, cli_add_map);
1080 add_handler(DEL+MAP, cli_del_map);
1081 add_handler(SWITCH+MAP+GROUP, cli_switch_group);
1082 add_handler(DUMP+PATHVEC, cli_dump_pathvec);
1083 add_handler(RECONFIGURE, cli_reconfigure);
1084 add_handler(SUSPEND+MAP, cli_suspend);
1085 add_handler(RESUME+MAP, cli_resume);
1086 add_handler(REINSTATE+PATH, cli_reinstate);
1087 add_handler(FAIL+PATH, cli_fail);
1089 uxsock_listen(&uxsock_trigger, ap);
1095 exit_daemon (int status)
1098 fprintf(stderr, "bad exit status. see daemon.log\n");
1100 condlog(3, "unlink pidfile");
1101 unlink(DEFAULT_PIDFILE);
1104 pthread_cond_signal(&exit_cond);
1105 unlock(&exit_mutex);
1111 fail_path (struct path * pp)
1116 condlog(2, "checker failed path %s in map %s",
1117 pp->dev_t, pp->mpp->alias);
1119 dm_fail_path(pp->mpp->alias, pp->dev_t);
1120 update_queue_mode_del_path(pp->mpp);
1124 * caller must have locked the path list before calling that function
1127 reinstate_path (struct path * pp)
1132 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
1133 condlog(0, "%s: reinstate failed", pp->dev_t);
1135 condlog(2, "%s: reinstated", pp->dev_t);
1136 update_queue_mode_add_path(pp->mpp);
1141 enable_group(struct path * pp)
1143 struct pathgroup * pgp;
1146 * if path is added through uev_add_path, pgindex can be unset.
1147 * next update_strings() will set it, upon map reload event.
1149 * we can safely return here, because upon map reload, all
1150 * PG will be enabled.
1152 if (!pp->mpp->pg || !pp->pgindex)
1155 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1157 if (pgp->status == PGSTATE_DISABLED) {
1158 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1159 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1164 mpvec_garbage_collector (struct vectors * vecs)
1166 struct multipath * mpp;
1169 vector_foreach_slot (vecs->mpvec, mpp, i) {
1170 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1171 condlog(2, "%s: remove dead map", mpp->alias);
1172 remove_map(mpp, vecs, stop_waiter_thread, 1);
1179 defered_failback_tick (vector mpvec)
1181 struct multipath * mpp;
1184 vector_foreach_slot (mpvec, mpp, i) {
1186 * defered failback getting sooner
1188 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1189 mpp->failback_tick--;
1191 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1192 switch_pathgroup(mpp);
1198 retry_count_tick(vector mpvec)
1200 struct multipath *mpp;
1203 vector_foreach_slot (mpvec, mpp, i) {
1204 if (mpp->retry_tick) {
1205 condlog(4, "%s: Retrying.. No active path", mpp->alias);
1206 if(--mpp->retry_tick == 0) {
1207 dm_queue_if_no_path(mpp->alias, 0);
1208 condlog(2, "%s: Disable queueing", mpp->alias);
1215 checkerloop (void *ap)
1217 struct vectors *vecs;
1221 char checker_msg[MAX_CHECKER_MSG_SIZE];
1223 mlockall(MCL_CURRENT | MCL_FUTURE);
1225 memset(checker_msg, 0, MAX_CHECKER_MSG_SIZE);
1226 vecs = (struct vectors *)ap;
1228 condlog(2, "path checkers start up");
1231 * init the path check interval
1233 vector_foreach_slot (vecs->pathvec, pp, i) {
1234 pp->checkint = conf->checkint;
1238 pthread_cleanup_push(cleanup_lock, vecs->lock);
1242 vector_foreach_slot (vecs->pathvec, pp, i) {
1246 if (pp->tick && --pp->tick)
1247 continue; /* don't check this path yet */
1250 * provision a next check soonest,
1251 * in case we exit abnormaly from here
1253 pp->tick = conf->checkint;
1256 pathinfo(pp, conf->hwtable, DI_SYSFS);
1261 condlog(0, "%s: checkfn is void", pp->dev);
1264 newstate = pp->checkfn(pp->fd, checker_msg,
1265 &pp->checker_context);
1268 condlog(2, "%s: unusable path", pp->dev);
1269 pathinfo(pp, conf->hwtable, 0);
1273 if (newstate != pp->state) {
1274 pp->state = newstate;
1275 LOG_MSG(1, checker_msg);
1278 * upon state change, reset the checkint
1279 * to the shortest delay
1281 pp->checkint = conf->checkint;
1283 if (newstate == PATH_DOWN ||
1284 newstate == PATH_SHAKY ||
1285 update_multipath_strings(pp->mpp,
1288 * proactively fail path in the DM
1293 * cancel scheduled failback
1295 pp->mpp->failback_tick = 0;
1301 * reinstate this path
1306 * schedule [defered] failback
1308 if (pp->mpp->pgfailback > 0)
1309 pp->mpp->failback_tick =
1310 pp->mpp->pgfailback + 1;
1311 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
1312 need_switch_pathgroup(pp->mpp, 1))
1313 switch_pathgroup(pp->mpp);
1316 * if at least one path is up in a group, and
1317 * the group is disabled, re-enable it
1319 if (newstate == PATH_UP)
1322 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1323 LOG_MSG(4, checker_msg);
1325 * double the next check delay.
1326 * max at conf->max_checkint
1328 if (pp->checkint < (conf->max_checkint / 2))
1329 pp->checkint = 2 * pp->checkint;
1331 pp->checkint = conf->max_checkint;
1333 pp->tick = pp->checkint;
1334 condlog(4, "%s: delay next check %is",
1335 pp->dev_t, pp->tick);
1338 pp->state = newstate;
1341 * path prio refreshing
1343 condlog(4, "path prio refresh");
1344 pathinfo(pp, conf->hwtable, DI_PRIO);
1346 if (need_switch_pathgroup(pp->mpp, 0)) {
1347 if (pp->mpp->pgfailback > 0)
1348 pp->mpp->failback_tick =
1349 pp->mpp->pgfailback + 1;
1350 else if (pp->mpp->pgfailback ==
1351 -FAILBACK_IMMEDIATE)
1352 switch_pathgroup(pp->mpp);
1355 defered_failback_tick(vecs->mpvec);
1356 retry_count_tick(vecs->mpvec);
1361 condlog(4, "map garbage collection");
1362 mpvec_garbage_collector(vecs);
1366 lock_cleanup_pop(vecs->lock);
1373 configure (struct vectors * vecs, int start_waiters)
1375 struct multipath * mpp;
1380 if (!(vecs->pathvec = vector_alloc()))
1383 if (!(vecs->mpvec = vector_alloc()))
1386 if (!(mpvec = vector_alloc()))
1390 * probe for current path (from sysfs) and map (from dm) sets
1392 path_discovery(vecs->pathvec, conf, DI_ALL);
1394 vector_foreach_slot (vecs->pathvec, pp, i)
1395 pp->checkint = conf->checkint;
1397 if (map_discovery(vecs))
1401 * create new set of maps & push changed ones into dm
1403 if (coalesce_paths(vecs, mpvec, NULL))
1407 * may need to remove some maps which are no longer relevant
1408 * e.g., due to blacklist changes in conf file
1410 if (coalesce_maps(vecs, mpvec))
1415 if (conf->verbosity > 2)
1416 vector_foreach_slot(mpvec, mpp, i)
1420 * purge dm of old maps
1422 remove_maps(vecs, NULL);
1425 * save new set of maps formed by considering current path state
1427 vecs->mpvec = mpvec;
1430 * start dm event waiter threads for these new maps
1432 vector_foreach_slot(vecs->mpvec, mpp, i) {
1433 if (setup_multipath(vecs, mpp))
1436 if (start_waiter_thread(mpp, vecs))
1443 reconfigure (struct vectors * vecs)
1445 struct config * old = conf;
1447 condlog(0, "reconfigure");
1450 * free old map and path vectors ... they use old conf state
1452 if (VECTOR_SIZE(vecs->mpvec))
1453 remove_maps(vecs, stop_waiter_thread);
1455 if (VECTOR_SIZE(vecs->pathvec))
1456 free_pathvec(vecs->pathvec, FREE_PATHS);
1460 if (load_config(DEFAULT_CONFIGFILE))
1463 conf->verbosity = old->verbosity;
1465 if (!conf->checkint) {
1466 conf->checkint = CHECKINT;
1467 conf->max_checkint = MAX_CHECKINT;
1474 static struct vectors *
1477 struct vectors * vecs;
1479 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1485 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1490 vecs->pathvec = vector_alloc();
1495 vecs->mpvec = vector_alloc();
1500 pthread_mutex_init(vecs->lock, NULL);
1505 vector_free(vecs->pathvec);
1510 condlog(0, "failed to init paths");
1515 signal_set(int signo, void (*func) (int))
1518 struct sigaction sig;
1519 struct sigaction osig;
1521 sig.sa_handler = func;
1522 sigemptyset(&sig.sa_mask);
1525 r = sigaction(signo, &sig, &osig);
1530 return (osig.sa_handler);
1536 condlog(3, "SIGHUP received");
1540 unlock(gvecs->lock);
1543 dbg_free_final(NULL);
1556 condlog(3, "SIGUSR1 received");
1562 signal_set(SIGHUP, sighup);
1563 signal_set(SIGUSR1, sigusr1);
1564 signal_set(SIGINT, sigend);
1565 signal_set(SIGTERM, sigend);
1566 signal_set(SIGKILL, sigend);
1573 static struct sched_param sched_param = {
1577 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1580 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1585 set_oom_adj (int val)
1589 fp = fopen("/proc/self/oom_adj", "w");
1594 fprintf(fp, "%i", val);
1599 child (void * param)
1601 pthread_t check_thr, uevent_thr, uxlsnr_thr;
1602 pthread_attr_t attr;
1603 struct vectors * vecs;
1605 mlockall(MCL_CURRENT | MCL_FUTURE);
1610 condlog(2, "--------start up--------");
1611 condlog(2, "read " DEFAULT_CONFIGFILE);
1613 if (load_config(DEFAULT_CONFIGFILE))
1616 setlogmask(LOG_UPTO(conf->verbosity + 3));
1619 * fill the voids left in the config file
1621 if (!conf->checkint) {
1622 conf->checkint = CHECKINT;
1623 conf->max_checkint = MAX_CHECKINT;
1626 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1635 vecs = gvecs = init_vecs();
1640 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
1641 condlog(0, "can not find sysfs mount point");
1646 * fetch and configure both paths and multipaths
1648 if (configure(vecs, 1)) {
1649 condlog(0, "failure during configuration");
1656 pthread_attr_init(&attr);
1657 pthread_attr_setstacksize(&attr, 64 * 1024);
1658 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1660 pthread_create(&check_thr, &attr, checkerloop, vecs);
1661 pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1662 pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1664 pthread_cond_wait(&exit_cond, &exit_mutex);
1670 remove_maps(vecs, stop_waiter_thread);
1671 free_pathvec(vecs->pathvec, FREE_PATHS);
1673 pthread_cancel(check_thr);
1674 pthread_cancel(uevent_thr);
1675 pthread_cancel(uxlsnr_thr);
1679 free_handlers(handlers);
1684 pthread_mutex_destroy(vecs->lock);
1692 condlog(2, "--------shut down-------");
1701 dbg_free_final(NULL);
1713 if( (pid = fork()) < 0){
1714 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1722 if ( (pid = fork()) < 0)
1723 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1727 in_fd = open("/dev/null", O_RDONLY);
1729 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1733 out_fd = open("/dev/console", O_WRONLY);
1735 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1740 close(STDIN_FILENO);
1742 close(STDOUT_FILENO);
1744 close(STDERR_FILENO);
1755 main (int argc, char *argv[])
1757 extern char *optarg;
1764 if (getuid() != 0) {
1765 fprintf(stderr, "need to be root\n");
1769 /* make sure we don't lock any path */
1771 umask(umask(077) | 022);
1773 conf = alloc_config();
1778 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1782 //debug=1; /* ### comment me out ### */
1785 if (sizeof(optarg) > sizeof(char *) ||
1786 !isdigit(optarg[0]))
1789 conf->verbosity = atoi(optarg);
1812 return (child(NULL));