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>
39 #include <blacklist.h>
40 #include <structs_vec.h>
42 #include <devmapper.h>
44 #include <discovery.h>
48 #include <switchgroup.h>
50 #include <configure.h>
57 #include "cli_handlers.h"
59 #define FILE_NAME_SIZE 256
62 #define LOG_MSG(a,b) \
63 if (strlen(b)) condlog(a, "%s: %s", pp->dev_t, b);
67 fprintf(stderr, "%s:%s(%i) lock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
70 fprintf(stderr, "%s:%s(%i) unlock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
71 pthread_mutex_unlock(a)
72 #define lock_cleanup_pop(a) \
73 fprintf(stderr, "%s:%s(%i) unlock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
74 pthread_cleanup_pop(1);
76 #define lock(a) pthread_mutex_lock(a)
77 #define unlock(a) pthread_mutex_unlock(a)
78 #define lock_cleanup_pop(a) pthread_cleanup_pop(1);
81 pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
82 pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
87 struct vectors * gvecs; /* global copy of vecs for use in sig handlers */
89 static struct event_thread *
93 struct event_thread * wp;
95 wp = (struct event_thread *)MALLOC(sizeof(struct event_thread));
101 free_waiter (void * data)
103 struct event_thread * wp = (struct event_thread *)data;
106 * indicate in mpp that the wp is already freed storage
108 lock(wp->vecs->lock);
112 * be careful, mpp may already be freed -- null if so
114 wp->mpp->waiter = NULL;
116 condlog(3, "free_waiter, mpp freed before wp=%p,", wp);
118 unlock(wp->vecs->lock);
121 dm_task_destroy(wp->dmt);
127 stop_waiter_thread (struct multipath * mpp, struct vectors * vecs)
129 struct event_thread * wp = (struct event_thread *)mpp->waiter;
132 condlog(3, "%s: no waiter thread", mpp->alias);
135 condlog(2, "%s: stop event checker thread", wp->mapname);
136 pthread_kill((pthread_t)wp->thread, SIGUSR1);
140 cleanup_lock (void * data)
142 unlock((pthread_mutex_t *)data);
146 * mpp->no_path_retry:
147 * -2 (QUEUE) : queue_if_no_path enabled, never turned off
148 * -1 (FAIL) : fail_if_no_path
149 * 0 (UNDEF) : nothing
150 * >0 : queue_if_no_path enabled, turned off after polling n times
153 update_queue_mode_del_path(struct multipath *mpp)
155 if (--mpp->nr_active == 0 && mpp->no_path_retry > 0) {
158 * meaning of +1: retry_tick may be decremented in
159 * checkerloop before starting retry.
161 mpp->stat_queueing_timeouts++;
162 mpp->retry_tick = mpp->no_path_retry * conf->checkint + 1;
163 condlog(1, "%s: Entering recovery mode: max_retries=%d",
164 mpp->alias, mpp->no_path_retry);
166 condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
170 update_queue_mode_add_path(struct multipath *mpp)
172 if (mpp->nr_active++ == 0 && mpp->no_path_retry > 0) {
173 /* come back to normal mode from retry mode */
175 dm_queue_if_no_path(mpp->alias, 1);
176 condlog(2, "%s: queue_if_no_path enabled", mpp->alias);
177 condlog(1, "%s: Recovered to normal mode", mpp->alias);
179 condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
183 need_switch_pathgroup (struct multipath * mpp, int refresh)
185 struct pathgroup * pgp;
189 if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
193 * Refresh path priority values
196 vector_foreach_slot (mpp->pg, pgp, i)
197 vector_foreach_slot (pgp->paths, pp, j)
198 pathinfo(pp, conf->hwtable, DI_PRIO);
200 mpp->bestpg = select_path_group(mpp);
202 if (mpp->bestpg != mpp->nextpg)
209 switch_pathgroup (struct multipath * mpp)
211 mpp->stat_switchgroup++;
212 dm_switchgroup(mpp->alias, mpp->bestpg);
213 condlog(2, "%s: switch to path group #%i",
214 mpp->alias, mpp->bestpg);
218 coalesce_maps(struct vectors *vecs, vector nmpv)
220 struct multipath * ompp;
221 vector ompv = vecs->mpvec;
224 vector_foreach_slot (ompv, ompp, i) {
225 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
227 * remove all current maps not allowed by the
228 * current configuration
230 if (dm_flush_map(ompp->alias, DEFAULT_TARGET)) {
231 condlog(0, "%s: unable to flush devmap",
234 * may be just because the device is open
236 if (!vector_alloc_slot(nmpv))
239 vector_set_slot(nmpv, ompp);
240 setup_multipath(vecs, ompp);
242 if ((j = find_slot(ompv, (void *)ompp)) != -1)
243 vector_del_slot(ompv, j);
249 condlog(3, "%s devmap removed", ompp->alias);
257 update_multipath (struct vectors *vecs, char *mapname)
259 struct multipath *mpp;
260 struct pathgroup *pgp;
265 mpp = find_mp_by_alias(vecs->mpvec, mapname);
270 free_pgvec(mpp->pg, KEEP_PATHS);
273 if (setup_multipath(vecs, mpp))
274 goto out; /* mpp freed in setup_multipath */
277 * compare checkers states with DM states
279 vector_foreach_slot (mpp->pg, pgp, i) {
280 vector_foreach_slot (pgp->paths, pp, j) {
281 if (pp->dmstate != PSTATE_FAILED)
284 if (pp->state != PATH_DOWN) {
285 int oldstate = pp->state;
286 condlog(2, "%s: mark as failed", pp->dev_t);
287 mpp->stat_path_failures++;
288 pp->state = PATH_DOWN;
289 if (oldstate == PATH_UP ||
290 oldstate == PATH_GHOST)
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 sync_map_state(struct multipath *mpp)
482 struct pathgroup *pgp;
485 vector_foreach_slot (mpp->pg, pgp, i){
486 vector_foreach_slot (pgp->paths, pp, j){
487 if (pp->state <= PATH_UNCHECKED)
489 if ((pp->dmstate == PSTATE_FAILED ||
490 pp->dmstate == PSTATE_UNDEF) &&
491 (pp->state == PATH_UP || pp->state == PATH_GHOST))
492 dm_reinstate_path(mpp->alias, pp->dev_t);
493 else if ((pp->dmstate == PSTATE_ACTIVE ||
494 pp->dmstate == PSTATE_UNDEF) &&
495 (pp->state == PATH_DOWN ||
496 pp->state == PATH_SHAKY))
497 dm_fail_path(mpp->alias, pp->dev_t);
503 sync_maps_state(vector mpvec)
506 struct multipath *mpp;
508 vector_foreach_slot (mpvec, mpp, i)
513 flush_map(struct multipath * mpp, struct vectors * vecs)
516 * clear references to this map before flushing so we can ignore
517 * the spurious uevent we may generate with the dm_flush_map call below
519 if (dm_flush_map(mpp->alias, DEFAULT_TARGET)) {
521 * May not really be an error -- if the map was already flushed
522 * from the device mapper by dmsetup(8) for instance.
524 condlog(0, "%s: can't flush", mpp->alias);
529 condlog(3, "%s: devmap removed", mpp->alias);
532 orphan_paths(vecs->pathvec, mpp);
533 remove_map(mpp, vecs, stop_waiter_thread, 1);
539 uev_add_map (char * devname, struct vectors * vecs)
541 condlog(2, "%s: add map (uevent)", devname);
542 return ev_add_map(devname, vecs);
546 ev_add_map (char * devname, struct vectors * vecs)
549 char dev_t[BLK_DEV_SIZE];
552 struct multipath * mpp;
556 if (sscanf(devname, "dm-%d", &minor) == 1 &&
557 !sysfs_get_dev(sysfs_path, devname, dev_t, BLK_DEV_SIZE) &&
558 sscanf(dev_t, "%d:%d", &major, &minor) == 2)
559 alias = dm_mapname(major, minor);
561 alias = STRDUP(devname);
566 map_present = dm_map_present(alias);
568 if (map_present && dm_type(alias, DEFAULT_TARGET) <= 0) {
569 condlog(4, "%s: not a multipath map", alias);
574 mpp = find_mp_by_alias(vecs->mpvec, alias);
578 * Not really an error -- we generate our own uevent
579 * if we create a multipath mapped device as a result
582 condlog(0, "%s: devmap already registered",
589 * now we can register the map
591 if (map_present && (mpp = add_map_without_path(vecs, minor, alias,
592 start_waiter_thread))) {
594 condlog(3, "%s: devmap %s added", alias, devname);
597 refwwid = get_refwwid(devname, DEV_DEVMAP, vecs->pathvec);
600 r = coalesce_paths(vecs, NULL, refwwid);
605 condlog(3, "%s: devmap %s added", alias, devname);
607 condlog(0, "%s: uev_add_map %s failed", alias, devname);
615 uev_remove_map (char * devname, struct vectors * vecs)
617 condlog(2, "%s: remove map (uevent)", devname);
618 return ev_remove_map(devname, vecs);
622 ev_remove_map (char * devname, struct vectors * vecs)
624 struct multipath * mpp;
626 mpp = find_mp_by_str(vecs->mpvec, devname);
629 condlog(3, "%s: devmap not registered, can't remove",
633 flush_map(mpp, vecs);
639 uev_umount_map (char * devname, struct vectors * vecs)
641 struct multipath * mpp;
643 condlog(2, "%s: umount map (uevent)", devname);
645 mpp = find_mp_by_str(vecs->mpvec, devname);
650 update_mpp_paths(mpp, vecs->pathvec);
651 verify_paths(mpp, vecs, NULL);
653 if (!VECTOR_SIZE(mpp->paths))
654 flush_map(mpp, vecs);
660 uev_add_path (char * devname, struct vectors * vecs)
662 condlog(2, "%s: add path (uevent)", devname);
663 return (ev_add_path(devname, vecs) != 1)? 0 : 1;
674 ev_add_path (char * devname, struct vectors * vecs)
676 struct multipath * mpp;
678 char empty_buff[WWID_SIZE] = {0};
680 pp = find_path_by_dev(vecs->pathvec, devname);
683 condlog(0, "%s: spurious uevent, path already in pathvec",
690 * get path vital state
692 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
694 condlog(0, "%s: failed to store path info", devname);
697 pp->checkint = conf->checkint;
701 * need path UID to go any further
703 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
704 condlog(0, "%s: failed to get path uid", devname);
705 return 1; /* leave path added to pathvec */
707 if (blacklist_path(conf, pp)){
708 int i = find_slot(vecs->pathvec, (void *)pp);
710 vector_del_slot(vecs->pathvec, i);
714 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
717 if (adopt_paths(vecs->pathvec, mpp))
718 return 1; /* leave path added to pathvec */
720 verify_paths(mpp, vecs, NULL);
721 mpp->action = ACT_RELOAD;
724 if ((mpp = add_map_with_path(vecs, pp, 1)))
725 mpp->action = ACT_CREATE;
727 return 1; /* leave path added to pathvec */
731 * push the map to the device-mapper
733 if (setup_map(mpp)) {
734 condlog(0, "%s: failed to setup map for addition of new "
735 "path %s", mpp->alias, devname);
739 * reload the map for the multipath mapped device
741 if (domap(mpp) <= 0) {
742 condlog(0, "%s: failed in domap for addition of new "
743 "path %s", mpp->alias, devname);
745 * deal with asynchronous uevents :((
747 if (mpp->action == ACT_RELOAD) {
748 condlog(0, "%s: uev_add_path sleep", mpp->alias);
750 update_mpp_paths(mpp, vecs->pathvec);
759 * update our state from kernel regardless of create or reload
761 if (setup_multipath(vecs, mpp))
766 if (mpp->action == ACT_CREATE &&
767 start_waiter_thread(mpp, vecs))
770 condlog(3, "%s path added to devmap %s", devname, mpp->alias);
774 remove_map(mpp, vecs, NULL, 1);
779 uev_remove_path (char * devname, struct vectors * vecs)
781 condlog(2, "%s: remove path (uevent)", devname);
782 return ev_remove_path(devname, vecs);
786 ev_remove_path (char * devname, struct vectors * vecs)
788 struct multipath * mpp;
793 pp = find_path_by_dev(vecs->pathvec, devname);
796 condlog(0, "%s: spurious uevent, path not in pathvec", devname);
801 * avoid referring to the map of an orphanned path
803 if ((mpp = pp->mpp)) {
806 * remove the map IFF removing the last path
808 if (pathcount(mpp, PATH_WILD) > 1) {
809 vector rpvec = vector_alloc();
812 * transform the mp->pg vector of vectors of paths
813 * into a mp->params string to feed the device-mapper
815 update_mpp_paths(mpp, vecs->pathvec);
816 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
817 vector_del_slot(mpp->paths, i);
819 if (VECTOR_SIZE(mpp->paths) == 0) {
820 char alias[WWID_SIZE];
823 * flush_map will fail if the device is open
825 strncpy(alias, mpp->alias, WWID_SIZE);
826 if (flush_map(mpp, vecs))
829 condlog(3, "%s: removed map after removing"
830 " multiple paths", alias);
833 if (setup_map(mpp)) {
834 condlog(0, "%s: failed to setup map for"
835 " removal of path %s", mpp->alias, devname);
836 free_pathvec(rpvec, KEEP_PATHS);
842 mpp->action = ACT_RELOAD;
843 if (domap(mpp) <= 0) {
844 condlog(0, "%s: failed in domap for "
845 "removal of path %s",
846 mpp->alias, devname);
848 * Delete path from pathvec so that
849 * update_mpp_paths wont find it later
850 * when/if another path is removed.
852 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
853 vector_del_slot(vecs->pathvec, i);
858 * update our state from kernel
860 if (setup_multipath(vecs, mpp)) {
861 free_pathvec(rpvec, KEEP_PATHS);
866 condlog(3, "%s path removed from devmap %s",
867 devname, mpp->alias);
869 free_pathvec(rpvec, KEEP_PATHS);
872 char alias[WWID_SIZE];
875 * flush_map will fail if the device is open
877 strncpy(alias, mpp->alias, WWID_SIZE);
878 if (flush_map(mpp, vecs))
881 condlog(3, "%s: removed map", alias);
886 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
887 vector_del_slot(vecs->pathvec, i);
894 remove_map(mpp, vecs, stop_waiter_thread, 1);
899 map_discovery (struct vectors * vecs)
902 struct multipath * mpp;
904 if (dm_get_maps(vecs->mpvec, "multipath"))
907 vector_foreach_slot (vecs->mpvec, mpp, i)
908 if (setup_multipath(vecs, mpp))
915 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
917 struct vectors * vecs;
922 vecs = (struct vectors *)trigger_data;
924 pthread_cleanup_push(cleanup_lock, vecs->lock);
927 r = parse_cmd(str, reply, len, vecs);
930 *reply = STRDUP("fail\n");
931 *len = strlen(*reply) + 1;
934 else if (!r && *len == 0) {
935 *reply = STRDUP("ok\n");
936 *len = strlen(*reply) + 1;
939 /* else if (r < 0) leave *reply alone */
941 lock_cleanup_pop(vecs->lock);
946 uev_discard(char * devpath)
951 * keep only block devices, discard partitions
953 if (sscanf(devpath, "/block/%10s", a) != 1 ||
954 sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
955 condlog(4, "discard event on %s", devpath);
962 uev_trigger (struct uevent * uev, void * trigger_data)
966 struct vectors * vecs;
968 vecs = (struct vectors *)trigger_data;
970 if (uev_discard(uev->devpath))
973 basename(uev->devpath, devname);
977 * device map add/remove event
979 if (!strncmp(devname, "dm-", 3)) {
980 if (!strncmp(uev->action, "add", 3)) {
981 r = uev_add_map(devname, vecs);
984 if (!strncmp(uev->action, "remove", 6)) {
985 r = uev_remove_map(devname, vecs);
988 if (!strncmp(uev->action, "umount", 6)) {
989 r = uev_umount_map(devname, vecs);
996 * path add/remove event
998 if (blacklist(conf->blist_devnode, devname))
1001 if (!strncmp(uev->action, "add", 3)) {
1002 r = uev_add_path(devname, vecs);
1005 if (!strncmp(uev->action, "remove", 6)) {
1006 r = uev_remove_path(devname, vecs);
1016 ueventloop (void * ap)
1018 if (uevent_listen(&uev_trigger, ap))
1019 fprintf(stderr, "error starting uevent listener");
1025 uxlsnrloop (void * ap)
1030 if (alloc_handlers())
1033 add_handler(LIST+PATHS, cli_list_paths);
1034 add_handler(LIST+MAPS, cli_list_maps);
1035 add_handler(LIST+MAPS+STATUS, cli_list_maps_status);
1036 add_handler(LIST+MAPS+STATS, cli_list_maps_stats);
1037 add_handler(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
1038 add_handler(LIST+TOPOLOGY, cli_list_maps_topology);
1039 add_handler(LIST+MAP+TOPOLOGY, cli_list_map_topology);
1040 add_handler(LIST+CONFIG, cli_list_config);
1041 add_handler(ADD+PATH, cli_add_path);
1042 add_handler(DEL+PATH, cli_del_path);
1043 add_handler(ADD+MAP, cli_add_map);
1044 add_handler(DEL+MAP, cli_del_map);
1045 add_handler(SWITCH+MAP+GROUP, cli_switch_group);
1046 add_handler(RECONFIGURE, cli_reconfigure);
1047 add_handler(SUSPEND+MAP, cli_suspend);
1048 add_handler(RESUME+MAP, cli_resume);
1049 add_handler(REINSTATE+PATH, cli_reinstate);
1050 add_handler(FAIL+PATH, cli_fail);
1052 uxsock_listen(&uxsock_trigger, ap);
1058 exit_daemon (int status)
1061 fprintf(stderr, "bad exit status. see daemon.log\n");
1063 condlog(3, "unlink pidfile");
1064 unlink(DEFAULT_PIDFILE);
1067 pthread_cond_signal(&exit_cond);
1068 unlock(&exit_mutex);
1074 fail_path (struct path * pp, int del_active)
1079 condlog(2, "checker failed path %s in map %s",
1080 pp->dev_t, pp->mpp->alias);
1082 dm_fail_path(pp->mpp->alias, pp->dev_t);
1084 update_queue_mode_del_path(pp->mpp);
1088 * caller must have locked the path list before calling that function
1091 reinstate_path (struct path * pp, int add_active)
1096 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
1097 condlog(0, "%s: reinstate failed", pp->dev_t);
1099 condlog(2, "%s: reinstated", pp->dev_t);
1101 update_queue_mode_add_path(pp->mpp);
1106 enable_group(struct path * pp)
1108 struct pathgroup * pgp;
1111 * if path is added through uev_add_path, pgindex can be unset.
1112 * next update_strings() will set it, upon map reload event.
1114 * we can safely return here, because upon map reload, all
1115 * PG will be enabled.
1117 if (!pp->mpp->pg || !pp->pgindex)
1120 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1122 if (pgp->status == PGSTATE_DISABLED) {
1123 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1124 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1129 mpvec_garbage_collector (struct vectors * vecs)
1131 struct multipath * mpp;
1134 vector_foreach_slot (vecs->mpvec, mpp, i) {
1135 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1136 condlog(2, "%s: remove dead map", mpp->alias);
1137 remove_map(mpp, vecs, stop_waiter_thread, 1);
1144 defered_failback_tick (vector mpvec)
1146 struct multipath * mpp;
1149 vector_foreach_slot (mpvec, mpp, i) {
1151 * defered failback getting sooner
1153 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1154 mpp->failback_tick--;
1156 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1157 switch_pathgroup(mpp);
1163 retry_count_tick(vector mpvec)
1165 struct multipath *mpp;
1168 vector_foreach_slot (mpvec, mpp, i) {
1169 if (mpp->retry_tick) {
1170 mpp->stat_total_queueing_time++;
1171 condlog(4, "%s: Retrying.. No active path", mpp->alias);
1172 if(--mpp->retry_tick == 0) {
1173 dm_queue_if_no_path(mpp->alias, 0);
1174 condlog(2, "%s: Disable queueing", mpp->alias);
1181 checkerloop (void *ap)
1183 struct vectors *vecs;
1188 mlockall(MCL_CURRENT | MCL_FUTURE);
1189 vecs = (struct vectors *)ap;
1190 condlog(2, "path checkers start up");
1193 * init the path check interval
1195 vector_foreach_slot (vecs->pathvec, pp, i) {
1196 pp->checkint = conf->checkint;
1200 pthread_cleanup_push(cleanup_lock, vecs->lock);
1204 vector_foreach_slot (vecs->pathvec, pp, i) {
1208 if (pp->tick && --pp->tick)
1209 continue; /* don't check this path yet */
1212 * provision a next check soonest,
1213 * in case we exit abnormaly from here
1215 pp->tick = conf->checkint;
1217 if (!checker_selected(&pp->checker)) {
1218 pathinfo(pp, conf->hwtable, DI_SYSFS);
1222 if (!checker_selected(&pp->checker)) {
1223 condlog(0, "%s: checker is not set", pp->dev);
1226 newstate = checker_check(&pp->checker);
1229 condlog(2, "%s: unusable path", pp->dev);
1230 pathinfo(pp, conf->hwtable, 0);
1234 if (newstate != pp->state) {
1235 int oldstate = pp->state;
1236 pp->state = newstate;
1237 LOG_MSG(1, checker_message(&pp->checker));
1240 * upon state change, reset the checkint
1241 * to the shortest delay
1243 pp->checkint = conf->checkint;
1245 if (newstate == PATH_DOWN ||
1246 newstate == PATH_SHAKY ||
1247 update_multipath_strings(pp->mpp,
1250 * proactively fail path in the DM
1252 if (oldstate == PATH_UP ||
1253 oldstate == PATH_GHOST)
1259 * cancel scheduled failback
1261 pp->mpp->failback_tick = 0;
1263 pp->mpp->stat_path_failures++;
1268 * reinstate this path
1270 if (oldstate != PATH_UP &&
1271 oldstate != PATH_GHOST)
1272 reinstate_path(pp, 1);
1274 reinstate_path(pp, 0);
1277 * schedule [defered] failback
1279 if (pp->mpp->pgfailback > 0)
1280 pp->mpp->failback_tick =
1281 pp->mpp->pgfailback + 1;
1282 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
1283 need_switch_pathgroup(pp->mpp, 1))
1284 switch_pathgroup(pp->mpp);
1287 * if at least one path is up in a group, and
1288 * the group is disabled, re-enable it
1290 if (newstate == PATH_UP)
1293 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1294 LOG_MSG(4, checker_message(&pp->checker));
1296 * double the next check delay.
1297 * max at conf->max_checkint
1299 if (pp->checkint < (conf->max_checkint / 2))
1300 pp->checkint = 2 * pp->checkint;
1302 pp->checkint = conf->max_checkint;
1304 pp->tick = pp->checkint;
1305 condlog(4, "%s: delay next check %is",
1306 pp->dev_t, pp->tick);
1308 else if (newstate == PATH_DOWN)
1309 LOG_MSG(2, checker_message(&pp->checker));
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 <= 0)
1322 pp->mpp->failback_tick =
1323 pp->mpp->pgfailback + 1;
1324 else if (pp->mpp->pgfailback ==
1325 -FAILBACK_IMMEDIATE)
1326 switch_pathgroup(pp->mpp);
1329 defered_failback_tick(vecs->mpvec);
1330 retry_count_tick(vecs->mpvec);
1335 condlog(4, "map garbage collection");
1336 mpvec_garbage_collector(vecs);
1340 lock_cleanup_pop(vecs->lock);
1347 configure (struct vectors * vecs, int start_waiters)
1349 struct multipath * mpp;
1354 if (!(vecs->pathvec = vector_alloc()))
1357 if (!(vecs->mpvec = vector_alloc()))
1360 if (!(mpvec = vector_alloc()))
1364 * probe for current path (from sysfs) and map (from dm) sets
1366 path_discovery(vecs->pathvec, conf, DI_ALL);
1368 vector_foreach_slot (vecs->pathvec, pp, i){
1369 if (blacklist_path(conf, pp)){
1370 vector_del_slot(vecs->pathvec, i);
1375 pp->checkint = conf->checkint;
1377 if (map_discovery(vecs))
1381 * create new set of maps & push changed ones into dm
1383 if (coalesce_paths(vecs, mpvec, NULL))
1387 * may need to remove some maps which are no longer relevant
1388 * e.g., due to blacklist changes in conf file
1390 if (coalesce_maps(vecs, mpvec))
1395 sync_maps_state(mpvec);
1397 if (conf->verbosity > 2)
1398 vector_foreach_slot(mpvec, mpp, i)
1402 * purge dm of old maps
1404 remove_maps(vecs, NULL);
1407 * save new set of maps formed by considering current path state
1409 vecs->mpvec = mpvec;
1412 * start dm event waiter threads for these new maps
1414 vector_foreach_slot(vecs->mpvec, mpp, i) {
1415 if (setup_multipath(vecs, mpp))
1418 if (start_waiter_thread(mpp, vecs))
1425 reconfigure (struct vectors * vecs)
1427 struct config * old = conf;
1430 * free old map and path vectors ... they use old conf state
1432 if (VECTOR_SIZE(vecs->mpvec))
1433 remove_maps(vecs, stop_waiter_thread);
1435 if (VECTOR_SIZE(vecs->pathvec))
1436 free_pathvec(vecs->pathvec, FREE_PATHS);
1440 if (load_config(DEFAULT_CONFIGFILE))
1443 conf->verbosity = old->verbosity;
1445 if (!conf->checkint) {
1446 conf->checkint = DEFAULT_CHECKINT;
1447 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1454 static struct vectors *
1457 struct vectors * vecs;
1459 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1465 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1470 vecs->pathvec = vector_alloc();
1475 vecs->mpvec = vector_alloc();
1480 pthread_mutex_init(vecs->lock, NULL);
1485 vector_free(vecs->pathvec);
1490 condlog(0, "failed to init paths");
1495 signal_set(int signo, void (*func) (int))
1498 struct sigaction sig;
1499 struct sigaction osig;
1501 sig.sa_handler = func;
1502 sigemptyset(&sig.sa_mask);
1505 r = sigaction(signo, &sig, &osig);
1510 return (osig.sa_handler);
1516 condlog(2, "reconfigure (SIGHUP)");
1520 unlock(gvecs->lock);
1523 dbg_free_final(NULL);
1536 condlog(3, "SIGUSR1 received");
1542 signal_set(SIGHUP, sighup);
1543 signal_set(SIGUSR1, sigusr1);
1544 signal_set(SIGINT, sigend);
1545 signal_set(SIGTERM, sigend);
1546 signal_set(SIGKILL, sigend);
1553 static struct sched_param sched_param = {
1557 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1560 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1565 set_oom_adj (int val)
1569 fp = fopen("/proc/self/oom_adj", "w");
1574 fprintf(fp, "%i", val);
1579 child (void * param)
1581 pthread_t check_thr, uevent_thr, uxlsnr_thr;
1582 pthread_attr_t attr;
1583 struct vectors * vecs;
1585 mlockall(MCL_CURRENT | MCL_FUTURE);
1590 condlog(2, "--------start up--------");
1591 condlog(2, "read " DEFAULT_CONFIGFILE);
1593 if (load_config(DEFAULT_CONFIGFILE))
1596 setlogmask(LOG_UPTO(conf->verbosity + 3));
1599 * fill the voids left in the config file
1601 if (!conf->checkint) {
1602 conf->checkint = DEFAULT_CHECKINT;
1603 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1606 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1615 vecs = gvecs = init_vecs();
1620 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
1621 condlog(0, "can not find sysfs mount point");
1626 * fetch and configure both paths and multipaths
1628 if (configure(vecs, 1)) {
1629 condlog(0, "failure during configuration");
1636 pthread_attr_init(&attr);
1637 pthread_attr_setstacksize(&attr, 64 * 1024);
1638 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1640 pthread_create(&check_thr, &attr, checkerloop, vecs);
1641 pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1642 pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1644 pthread_cond_wait(&exit_cond, &exit_mutex);
1650 remove_maps(vecs, stop_waiter_thread);
1651 free_pathvec(vecs->pathvec, FREE_PATHS);
1653 pthread_cancel(check_thr);
1654 pthread_cancel(uevent_thr);
1655 pthread_cancel(uxlsnr_thr);
1659 free_handlers(handlers);
1664 pthread_mutex_destroy(vecs->lock);
1672 condlog(2, "--------shut down-------");
1681 dbg_free_final(NULL);
1693 if( (pid = fork()) < 0){
1694 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1702 if ( (pid = fork()) < 0)
1703 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1707 in_fd = open("/dev/null", O_RDONLY);
1709 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1713 out_fd = open("/dev/console", O_WRONLY);
1715 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1720 close(STDIN_FILENO);
1722 close(STDOUT_FILENO);
1724 close(STDERR_FILENO);
1735 main (int argc, char *argv[])
1737 extern char *optarg;
1744 if (getuid() != 0) {
1745 fprintf(stderr, "need to be root\n");
1749 /* make sure we don't lock any path */
1751 umask(umask(077) | 022);
1753 conf = alloc_config();
1758 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1762 //debug=1; /* ### comment me out ### */
1765 if (sizeof(optarg) > sizeof(char *) ||
1766 !isdigit(optarg[0]))
1769 conf->verbosity = atoi(optarg);
1792 return (child(NULL));