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->stat_queueing_timeouts++;
167 mpp->retry_tick = mpp->no_path_retry * conf->checkint + 1;
168 condlog(1, "%s: Entering recovery mode: max_retries=%d",
169 mpp->alias, mpp->no_path_retry);
171 condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
175 update_queue_mode_add_path(struct multipath *mpp)
177 if (mpp->nr_active++ == 0 && mpp->no_path_retry > 0) {
178 /* come back to normal mode from retry mode */
180 dm_queue_if_no_path(mpp->alias, 1);
181 condlog(2, "%s: queue_if_no_path enabled", mpp->alias);
182 condlog(1, "%s: Recovered to normal mode", mpp->alias);
184 condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
188 need_switch_pathgroup (struct multipath * mpp, int refresh)
190 struct pathgroup * pgp;
194 if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
198 * Refresh path priority values
201 vector_foreach_slot (mpp->pg, pgp, i)
202 vector_foreach_slot (pgp->paths, pp, j)
203 pathinfo(pp, conf->hwtable, DI_PRIO);
205 mpp->bestpg = select_path_group(mpp);
207 if (mpp->bestpg != mpp->nextpg)
214 switch_pathgroup (struct multipath * mpp)
216 mpp->stat_switchgroup++;
217 dm_switchgroup(mpp->alias, mpp->bestpg);
218 condlog(2, "%s: switch to path group #%i",
219 mpp->alias, mpp->bestpg);
223 coalesce_maps(struct vectors *vecs, vector nmpv)
225 struct multipath * ompp;
226 vector ompv = vecs->mpvec;
229 condlog(3, "coalesce_maps vs = %u", VECTOR_SIZE(ompv));
230 condlog(3, "coalesce_maps vs = %u", VECTOR_SIZE(nmpv));
232 vector_foreach_slot (ompv, ompp, i) {
233 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
235 * remove all current maps not allowed by the
236 * current configuration
238 if (dm_flush_map(ompp->alias, DEFAULT_TARGET)) {
239 condlog(0, "%s: unable to flush devmap",
242 * may be just because the device is open
244 if (!vector_alloc_slot(nmpv))
247 vector_set_slot(nmpv, ompp);
248 setup_multipath(vecs, ompp);
250 if ((j = find_slot(ompv, (void *)ompp)) != -1)
251 vector_del_slot(ompv, j);
257 condlog(3, "%s devmap removed", ompp->alias);
265 update_multipath (struct vectors *vecs, char *mapname)
267 struct multipath *mpp;
268 struct pathgroup *pgp;
273 mpp = find_mp_by_alias(vecs->mpvec, mapname);
278 free_pgvec(mpp->pg, KEEP_PATHS);
281 if (setup_multipath(vecs, mpp))
282 goto out; /* mpp freed in setup_multipath */
285 * compare checkers states with DM states
287 vector_foreach_slot (mpp->pg, pgp, i) {
288 vector_foreach_slot (pgp->paths, pp, j) {
289 if (pp->dmstate != PSTATE_FAILED)
292 if (pp->state != PATH_DOWN) {
293 condlog(2, "%s: mark as failed", pp->dev_t);
294 mpp->stat_path_failures++;
295 pp->state = PATH_DOWN;
296 update_queue_mode_del_path(mpp);
300 * schedule the next check earlier
302 if (pp->tick > conf->checkint)
303 pp->tick = conf->checkint;
310 condlog(0, "failed to update multipath");
315 static sigset_t unblock_signals(void)
320 sigaddset(&set, SIGHUP);
321 sigaddset(&set, SIGUSR1);
322 pthread_sigmask(SIG_UNBLOCK, &set, &old);
327 * returns the reschedule delay
328 * negative means *stop*
331 waiteventloop (struct event_thread * waiter)
337 if (!waiter->event_nr)
338 waiter->event_nr = dm_geteventnr(waiter->mapname);
340 if (!(waiter->dmt = dm_task_create(DM_DEVICE_WAITEVENT))) {
341 condlog(0, "%s: devmap event #%i dm_task_create error",
342 waiter->mapname, waiter->event_nr);
346 if (!dm_task_set_name(waiter->dmt, waiter->mapname)) {
347 condlog(0, "%s: devmap event #%i dm_task_set_name error",
348 waiter->mapname, waiter->event_nr);
349 dm_task_destroy(waiter->dmt);
353 if (waiter->event_nr && !dm_task_set_event_nr(waiter->dmt,
355 condlog(0, "%s: devmap event #%i dm_task_set_event_nr error",
356 waiter->mapname, waiter->event_nr);
357 dm_task_destroy(waiter->dmt);
361 dm_task_no_open_count(waiter->dmt);
363 /* accept wait interruption */
364 set = unblock_signals();
366 /* interruption spits messages */
370 r = dm_task_run(waiter->dmt);
372 /* wait is over : event or interrupt */
373 pthread_sigmask(SIG_SETMASK, &set, NULL);
376 if (!r) /* wait interrupted by signal */
379 dm_task_destroy(waiter->dmt);
387 condlog(3, "%s: devmap event #%i",
388 waiter->mapname, waiter->event_nr);
393 * 1) a table reload, which means our mpp structure is
394 * obsolete : refresh it through update_multipath()
395 * 2) a path failed by DM : mark as such through
397 * 3) map has gone away : stop the thread.
398 * 4) a path reinstate : nothing to do
399 * 5) a switch group : nothing to do
401 pthread_cleanup_push(cleanup_lock, waiter->vecs->lock);
402 lock(waiter->vecs->lock);
403 r = update_multipath(waiter->vecs, waiter->mapname);
404 lock_cleanup_pop(waiter->vecs->lock);
407 return -1; /* stop the thread */
409 event_nr = dm_geteventnr(waiter->mapname);
411 if (waiter->event_nr == event_nr)
412 return 1; /* upon problem reschedule 1s later */
414 waiter->event_nr = event_nr;
416 return -1; /* never reach there */
420 waitevent (void * et)
423 struct event_thread *waiter;
425 mlockall(MCL_CURRENT | MCL_FUTURE);
427 waiter = (struct event_thread *)et;
428 pthread_cleanup_push(free_waiter, et);
431 r = waiteventloop(waiter);
439 pthread_cleanup_pop(1);
444 start_waiter_thread (struct multipath * mpp, struct vectors * vecs)
447 struct event_thread * wp;
452 if (pthread_attr_init(&attr))
455 pthread_attr_setstacksize(&attr, 32 * 1024);
456 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
463 mpp->waiter = (void *)wp;
464 strncpy(wp->mapname, mpp->alias, WWID_SIZE);
468 if (pthread_create(&wp->thread, &attr, waitevent, wp)) {
469 condlog(0, "%s: cannot create event checker", wp->mapname);
472 condlog(2, "%s: event checker started", wp->mapname);
479 condlog(0, "failed to start waiter thread");
484 flush_map(struct multipath * mpp, struct vectors * vecs)
487 * clear references to this map before flushing so we can ignore
488 * the spurious uevent we may generate with the dm_flush_map call below
490 if (dm_flush_map(mpp->alias, DEFAULT_TARGET)) {
492 * May not really be an error -- if the map was already flushed
493 * from the device mapper by dmsetup(8) for instance.
495 condlog(0, "%s: can't flush", mpp->alias);
500 condlog(3, "%s: devmap removed", mpp->alias);
503 orphan_paths(vecs->pathvec, mpp);
504 remove_map(mpp, vecs, stop_waiter_thread, 1);
510 uev_add_map (char * devname, struct vectors * vecs)
512 condlog(2, "%s: add map (uevent)", devname);
513 return ev_add_map(devname, vecs);
517 ev_add_map (char * devname, struct vectors * vecs)
520 char dev_t[BLK_DEV_SIZE];
523 struct multipath * mpp;
527 if (sscanf(devname, "dm-%d", &minor) == 1 &&
528 !sysfs_get_dev(sysfs_path, devname, dev_t, BLK_DEV_SIZE) &&
529 sscanf(dev_t, "%d:%d", &major, &minor) == 2)
530 alias = dm_mapname(major, minor);
532 alias = STRDUP(devname);
537 map_present = dm_map_present(alias);
539 if (map_present && dm_type(alias, DEFAULT_TARGET) <= 0) {
540 condlog(4, "%s: not a multipath map", alias);
545 mpp = find_mp_by_alias(vecs->mpvec, alias);
549 * Not really an error -- we generate our own uevent
550 * if we create a multipath mapped device as a result
553 condlog(0, "%s: devmap already registered",
560 * now we can register the map
562 if (map_present && (mpp = add_map_without_path(vecs, minor, alias,
563 start_waiter_thread))) {
564 condlog(3, "%s: devmap %s added", alias, devname);
567 refwwid = get_refwwid(devname, DEV_DEVMAP, vecs->pathvec);
570 r = coalesce_paths(vecs, NULL, refwwid);
575 condlog(3, "%s: devmap %s added", alias, devname);
577 condlog(0, "%s: uev_add_map %s failed", alias, devname);
585 uev_remove_map (char * devname, struct vectors * vecs)
587 condlog(2, "%s: remove map (uevent)", devname);
588 return ev_remove_map(devname, vecs);
592 ev_remove_map (char * devname, struct vectors * vecs)
595 struct multipath * mpp;
597 if (sscanf(devname, "dm-%d", &minor) == 1)
598 mpp = find_mp_by_minor(vecs->mpvec, minor);
600 mpp = find_mp_by_alias(vecs->mpvec, devname);
603 condlog(3, "%s: devmap not registered, can't remove",
607 flush_map(mpp, vecs);
613 uev_umount_map (char * devname, struct vectors * vecs)
616 struct multipath * mpp;
618 condlog(2, "%s: umount map (uevent)", devname);
620 if (sscanf(devname, "dm-%d", &minor) == 1)
621 mpp = find_mp_by_minor(vecs->mpvec, minor);
623 mpp = find_mp_by_alias(vecs->mpvec, devname);
628 update_mpp_paths(mpp, vecs->pathvec);
629 verify_paths(mpp, vecs, NULL);
631 if (!VECTOR_SIZE(mpp->paths))
632 flush_map(mpp, vecs);
638 uev_add_path (char * devname, struct vectors * vecs)
640 condlog(2, "%s: add path (uevent)", devname);
641 return ev_add_path(devname, vecs);
645 ev_add_path (char * devname, struct vectors * vecs)
647 struct multipath * mpp;
649 char empty_buff[WWID_SIZE] = {0};
651 pp = find_path_by_dev(vecs->pathvec, devname);
654 condlog(0, "%s: spurious uevent, path already in pathvec, %p",
657 * allow reconfig of orphaned path here
659 if (pp->mpp) return 1;
663 * get path vital state
665 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
667 condlog(0, "%s: failed to store path info", devname);
670 pp->checkint = conf->checkint;
674 * need path UID to go any further
676 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
677 condlog(0, "%s: failed to get path uid", devname);
678 return 1; /* leave path added to pathvec */
681 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
684 if (adopt_paths(vecs->pathvec, mpp))
685 return 1; /* leave path added to pathvec */
687 verify_paths(mpp, vecs, NULL);
688 condlog(0, "%s: ownership set to %s", pp->dev_t, mpp->alias);
689 mpp->action = ACT_RELOAD;
692 if ((mpp = add_map_with_path(vecs, pp, 1)))
693 mpp->action = ACT_CREATE;
695 return 1; /* leave path added to pathvec */
699 * push the map to the device-mapper
701 if (setup_map(mpp)) {
702 condlog(0, "%s: failed to setup map for addition of new "
703 "path %s", mpp->alias, devname);
707 * reload the map for the multipath mapped device
709 if (domap(mpp) <= 0) {
710 condlog(0, "%s: failed in domap for addition of new "
711 "path %s", mpp->alias, devname);
713 * deal with asynchronous uevents :((
715 if (mpp->action == ACT_RELOAD) {
716 condlog(0, "%s: uev_add_path sleep", mpp->alias);
718 update_mpp_paths(mpp, vecs->pathvec);
727 * update our state from kernel regardless of create or reload
729 if (setup_multipath(vecs, mpp))
732 if (mpp->action == ACT_CREATE &&
733 start_waiter_thread(mpp, vecs))
736 condlog(3, "%s path added to devmap %s", devname, mpp->alias);
740 remove_map(mpp, vecs, NULL, 1);
745 uev_remove_path (char * devname, struct vectors * vecs)
747 condlog(2, "%s: remove path (uevent)", devname);
748 return ev_remove_path(devname, vecs);
752 ev_remove_path (char * devname, struct vectors * vecs)
754 struct multipath * mpp;
759 pp = find_path_by_dev(vecs->pathvec, devname);
762 condlog(0, "%s: spurious uevent, path not in pathvec", devname);
767 * avoid referring to the map of an orphanned path
769 if ((mpp = pp->mpp)) {
772 * remove the map IFF removing the last path
774 if (pathcount(mpp, PATH_WILD) > 1) {
775 vector rpvec = vector_alloc();
778 * transform the mp->pg vector of vectors of paths
779 * into a mp->params string to feed the device-mapper
781 update_mpp_paths(mpp, vecs->pathvec);
782 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
783 vector_del_slot(mpp->paths, i);
785 if (VECTOR_SIZE(mpp->paths) == 0) {
786 char alias[WWID_SIZE];
789 * flush_map will fail if the device is open
791 strncpy(alias, mpp->alias, WWID_SIZE);
792 if (flush_map(mpp, vecs))
795 condlog(3, "%s: removed map after removing"
796 " multiple paths", alias);
799 if (setup_map(mpp)) {
800 condlog(0, "%s: failed to setup map for"
801 " removal of path %s", mpp->alias, devname);
802 free_pathvec(rpvec, KEEP_PATHS);
808 mpp->action = ACT_RELOAD;
809 if (domap(mpp) <= 0) {
810 condlog(0, "%s: failed in domap for "
811 "removal of path %s",
812 mpp->alias, devname);
814 * Delete path from pathvec so that
815 * update_mpp_paths wont find it later
816 * when/if another path is removed.
818 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
819 vector_del_slot(vecs->pathvec, i);
824 * update our state from kernel
826 if (setup_multipath(vecs, mpp)) {
827 free_pathvec(rpvec, KEEP_PATHS);
831 condlog(3, "%s path removed from devmap %s",
832 devname, mpp->alias);
834 free_pathvec(rpvec, KEEP_PATHS);
837 char alias[WWID_SIZE];
840 * flush_map will fail if the device is open
842 strncpy(alias, mpp->alias, WWID_SIZE);
843 if (flush_map(mpp, vecs))
846 condlog(3, "%s: removed map", alias);
851 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
852 vector_del_slot(vecs->pathvec, i);
859 remove_map(mpp, vecs, stop_waiter_thread, 1);
864 map_discovery (struct vectors * vecs)
867 struct multipath * mpp;
869 if (dm_get_maps(vecs->mpvec, "multipath"))
872 vector_foreach_slot (vecs->mpvec, mpp, i)
873 if (setup_multipath(vecs, mpp))
880 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
882 struct vectors * vecs;
887 vecs = (struct vectors *)trigger_data;
889 pthread_cleanup_push(cleanup_lock, vecs->lock);
892 r = parse_cmd(str, reply, len, vecs);
895 *reply = STRDUP("fail\n");
896 *len = strlen(*reply) + 1;
899 else if (!r && *len == 0) {
900 *reply = STRDUP("ok\n");
901 *len = strlen(*reply) + 1;
904 /* else if (r < 0) leave *reply alone */
906 lock_cleanup_pop(vecs->lock);
911 uev_discard(char * devpath)
916 * keep only block devices, discard partitions
918 if (sscanf(devpath, "/block/%10s", a) != 1 ||
919 sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
920 condlog(4, "discard event on %s", devpath);
927 uev_trigger (struct uevent * uev, void * trigger_data)
931 struct vectors * vecs;
933 vecs = (struct vectors *)trigger_data;
935 if (uev_discard(uev->devpath))
938 basename(uev->devpath, devname);
942 * device map add/remove event
944 if (!strncmp(devname, "dm-", 3)) {
945 if (!strncmp(uev->action, "add", 3)) {
946 r = uev_add_map(devname, vecs);
949 if (!strncmp(uev->action, "remove", 6)) {
950 r = uev_remove_map(devname, vecs);
953 if (!strncmp(uev->action, "umount", 6)) {
954 r = uev_umount_map(devname, vecs);
961 * path add/remove event
963 if (blacklist(conf->blist, devname))
966 if (!strncmp(uev->action, "add", 3)) {
967 r = uev_add_path(devname, vecs);
970 if (!strncmp(uev->action, "remove", 6)) {
971 r = uev_remove_path(devname, vecs);
981 ueventloop (void * ap)
983 if (uevent_listen(&uev_trigger, ap))
984 fprintf(stderr, "error starting uevent listener");
990 uxlsnrloop (void * ap)
995 if (alloc_handlers())
998 add_handler(LIST+PATHS, cli_list_paths);
999 add_handler(LIST+MAPS, cli_list_maps);
1000 add_handler(LIST+MAPS+STATS, cli_list_maps_stats);
1001 add_handler(ADD+PATH, cli_add_path);
1002 add_handler(DEL+PATH, cli_del_path);
1003 add_handler(ADD+MAP, cli_add_map);
1004 add_handler(DEL+MAP, cli_del_map);
1005 add_handler(SWITCH+MAP+GROUP, cli_switch_group);
1006 add_handler(RECONFIGURE, cli_reconfigure);
1007 add_handler(SUSPEND+MAP, cli_suspend);
1008 add_handler(RESUME+MAP, cli_resume);
1009 add_handler(REINSTATE+PATH, cli_reinstate);
1010 add_handler(FAIL+PATH, cli_fail);
1012 uxsock_listen(&uxsock_trigger, ap);
1018 exit_daemon (int status)
1021 fprintf(stderr, "bad exit status. see daemon.log\n");
1023 condlog(3, "unlink pidfile");
1024 unlink(DEFAULT_PIDFILE);
1027 pthread_cond_signal(&exit_cond);
1028 unlock(&exit_mutex);
1034 fail_path (struct path * pp)
1039 condlog(2, "checker failed path %s in map %s",
1040 pp->dev_t, pp->mpp->alias);
1042 dm_fail_path(pp->mpp->alias, pp->dev_t);
1043 update_queue_mode_del_path(pp->mpp);
1047 * caller must have locked the path list before calling that function
1050 reinstate_path (struct path * pp)
1055 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
1056 condlog(0, "%s: reinstate failed", pp->dev_t);
1058 condlog(2, "%s: reinstated", pp->dev_t);
1059 update_queue_mode_add_path(pp->mpp);
1064 enable_group(struct path * pp)
1066 struct pathgroup * pgp;
1069 * if path is added through uev_add_path, pgindex can be unset.
1070 * next update_strings() will set it, upon map reload event.
1072 * we can safely return here, because upon map reload, all
1073 * PG will be enabled.
1075 if (!pp->mpp->pg || !pp->pgindex)
1078 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1080 if (pgp->status == PGSTATE_DISABLED) {
1081 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1082 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1087 mpvec_garbage_collector (struct vectors * vecs)
1089 struct multipath * mpp;
1092 vector_foreach_slot (vecs->mpvec, mpp, i) {
1093 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1094 condlog(2, "%s: remove dead map", mpp->alias);
1095 remove_map(mpp, vecs, stop_waiter_thread, 1);
1102 defered_failback_tick (vector mpvec)
1104 struct multipath * mpp;
1107 vector_foreach_slot (mpvec, mpp, i) {
1109 * defered failback getting sooner
1111 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1112 mpp->failback_tick--;
1114 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1115 switch_pathgroup(mpp);
1121 retry_count_tick(vector mpvec)
1123 struct multipath *mpp;
1126 vector_foreach_slot (mpvec, mpp, i) {
1127 if (mpp->retry_tick) {
1128 mpp->stat_total_queueing_time++;
1129 condlog(4, "%s: Retrying.. No active path", mpp->alias);
1130 if(--mpp->retry_tick == 0) {
1131 dm_queue_if_no_path(mpp->alias, 0);
1132 condlog(2, "%s: Disable queueing", mpp->alias);
1139 checkerloop (void *ap)
1141 struct vectors *vecs;
1145 char checker_msg[MAX_CHECKER_MSG_SIZE];
1147 mlockall(MCL_CURRENT | MCL_FUTURE);
1149 memset(checker_msg, 0, MAX_CHECKER_MSG_SIZE);
1150 vecs = (struct vectors *)ap;
1152 condlog(2, "path checkers start up");
1155 * init the path check interval
1157 vector_foreach_slot (vecs->pathvec, pp, i) {
1158 pp->checkint = conf->checkint;
1162 pthread_cleanup_push(cleanup_lock, vecs->lock);
1166 vector_foreach_slot (vecs->pathvec, pp, i) {
1170 if (pp->tick && --pp->tick)
1171 continue; /* don't check this path yet */
1174 * provision a next check soonest,
1175 * in case we exit abnormaly from here
1177 pp->tick = conf->checkint;
1180 pathinfo(pp, conf->hwtable, DI_SYSFS);
1185 condlog(0, "%s: checkfn is void", pp->dev);
1188 newstate = pp->checkfn(pp->fd, checker_msg,
1189 &pp->checker_context);
1192 condlog(2, "%s: unusable path", pp->dev);
1193 pathinfo(pp, conf->hwtable, 0);
1197 if (newstate != pp->state) {
1198 pp->state = newstate;
1199 LOG_MSG(1, checker_msg);
1202 * upon state change, reset the checkint
1203 * to the shortest delay
1205 pp->checkint = conf->checkint;
1207 if (newstate == PATH_DOWN ||
1208 newstate == PATH_SHAKY ||
1209 update_multipath_strings(pp->mpp,
1212 * proactively fail path in the DM
1217 * cancel scheduled failback
1219 pp->mpp->failback_tick = 0;
1221 pp->mpp->stat_path_failures++;
1226 * reinstate this path
1231 * schedule [defered] failback
1233 if (pp->mpp->pgfailback > 0)
1234 pp->mpp->failback_tick =
1235 pp->mpp->pgfailback + 1;
1236 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
1237 need_switch_pathgroup(pp->mpp, 1))
1238 switch_pathgroup(pp->mpp);
1241 * if at least one path is up in a group, and
1242 * the group is disabled, re-enable it
1244 if (newstate == PATH_UP)
1247 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1248 LOG_MSG(4, checker_msg);
1250 * double the next check delay.
1251 * max at conf->max_checkint
1253 if (pp->checkint < (conf->max_checkint / 2))
1254 pp->checkint = 2 * pp->checkint;
1256 pp->checkint = conf->max_checkint;
1258 pp->tick = pp->checkint;
1259 condlog(4, "%s: delay next check %is",
1260 pp->dev_t, pp->tick);
1263 pp->state = newstate;
1266 * path prio refreshing
1268 condlog(4, "path prio refresh");
1269 pathinfo(pp, conf->hwtable, DI_PRIO);
1271 if (need_switch_pathgroup(pp->mpp, 0)) {
1272 if (pp->mpp->pgfailback > 0)
1273 pp->mpp->failback_tick =
1274 pp->mpp->pgfailback + 1;
1275 else if (pp->mpp->pgfailback ==
1276 -FAILBACK_IMMEDIATE)
1277 switch_pathgroup(pp->mpp);
1280 defered_failback_tick(vecs->mpvec);
1281 retry_count_tick(vecs->mpvec);
1286 condlog(4, "map garbage collection");
1287 mpvec_garbage_collector(vecs);
1291 lock_cleanup_pop(vecs->lock);
1298 configure (struct vectors * vecs, int start_waiters)
1300 struct multipath * mpp;
1305 if (!(vecs->pathvec = vector_alloc()))
1308 if (!(vecs->mpvec = vector_alloc()))
1311 if (!(mpvec = vector_alloc()))
1315 * probe for current path (from sysfs) and map (from dm) sets
1317 path_discovery(vecs->pathvec, conf, DI_ALL);
1319 vector_foreach_slot (vecs->pathvec, pp, i)
1320 pp->checkint = conf->checkint;
1322 if (map_discovery(vecs))
1326 * create new set of maps & push changed ones into dm
1328 if (coalesce_paths(vecs, mpvec, NULL))
1332 * may need to remove some maps which are no longer relevant
1333 * e.g., due to blacklist changes in conf file
1335 if (coalesce_maps(vecs, mpvec))
1340 if (conf->verbosity > 2)
1341 vector_foreach_slot(mpvec, mpp, i)
1345 * purge dm of old maps
1347 remove_maps(vecs, NULL);
1350 * save new set of maps formed by considering current path state
1352 vecs->mpvec = mpvec;
1355 * start dm event waiter threads for these new maps
1357 vector_foreach_slot(vecs->mpvec, mpp, i) {
1358 if (setup_multipath(vecs, mpp))
1361 if (start_waiter_thread(mpp, vecs))
1368 reconfigure (struct vectors * vecs)
1370 struct config * old = conf;
1373 * free old map and path vectors ... they use old conf state
1375 if (VECTOR_SIZE(vecs->mpvec))
1376 remove_maps(vecs, stop_waiter_thread);
1378 if (VECTOR_SIZE(vecs->pathvec))
1379 free_pathvec(vecs->pathvec, FREE_PATHS);
1383 if (load_config(DEFAULT_CONFIGFILE))
1386 conf->verbosity = old->verbosity;
1388 if (!conf->checkint) {
1389 conf->checkint = CHECKINT;
1390 conf->max_checkint = MAX_CHECKINT;
1397 static struct vectors *
1400 struct vectors * vecs;
1402 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1408 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1413 vecs->pathvec = vector_alloc();
1418 vecs->mpvec = vector_alloc();
1423 pthread_mutex_init(vecs->lock, NULL);
1428 vector_free(vecs->pathvec);
1433 condlog(0, "failed to init paths");
1438 signal_set(int signo, void (*func) (int))
1441 struct sigaction sig;
1442 struct sigaction osig;
1444 sig.sa_handler = func;
1445 sigemptyset(&sig.sa_mask);
1448 r = sigaction(signo, &sig, &osig);
1453 return (osig.sa_handler);
1459 condlog(2, "reconfigure (SIGHUP)");
1463 unlock(gvecs->lock);
1466 dbg_free_final(NULL);
1479 condlog(3, "SIGUSR1 received");
1485 signal_set(SIGHUP, sighup);
1486 signal_set(SIGUSR1, sigusr1);
1487 signal_set(SIGINT, sigend);
1488 signal_set(SIGTERM, sigend);
1489 signal_set(SIGKILL, sigend);
1496 static struct sched_param sched_param = {
1500 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1503 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1508 set_oom_adj (int val)
1512 fp = fopen("/proc/self/oom_adj", "w");
1517 fprintf(fp, "%i", val);
1522 child (void * param)
1524 pthread_t check_thr, uevent_thr, uxlsnr_thr;
1525 pthread_attr_t attr;
1526 struct vectors * vecs;
1528 mlockall(MCL_CURRENT | MCL_FUTURE);
1533 condlog(2, "--------start up--------");
1534 condlog(2, "read " DEFAULT_CONFIGFILE);
1536 if (load_config(DEFAULT_CONFIGFILE))
1539 setlogmask(LOG_UPTO(conf->verbosity + 3));
1542 * fill the voids left in the config file
1544 if (!conf->checkint) {
1545 conf->checkint = CHECKINT;
1546 conf->max_checkint = MAX_CHECKINT;
1549 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1558 vecs = gvecs = init_vecs();
1563 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
1564 condlog(0, "can not find sysfs mount point");
1569 * fetch and configure both paths and multipaths
1571 if (configure(vecs, 1)) {
1572 condlog(0, "failure during configuration");
1579 pthread_attr_init(&attr);
1580 pthread_attr_setstacksize(&attr, 64 * 1024);
1581 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1583 pthread_create(&check_thr, &attr, checkerloop, vecs);
1584 pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1585 pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1587 pthread_cond_wait(&exit_cond, &exit_mutex);
1593 remove_maps(vecs, stop_waiter_thread);
1594 free_pathvec(vecs->pathvec, FREE_PATHS);
1596 pthread_cancel(check_thr);
1597 pthread_cancel(uevent_thr);
1598 pthread_cancel(uxlsnr_thr);
1602 free_handlers(handlers);
1607 pthread_mutex_destroy(vecs->lock);
1615 condlog(2, "--------shut down-------");
1624 dbg_free_final(NULL);
1636 if( (pid = fork()) < 0){
1637 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1645 if ( (pid = fork()) < 0)
1646 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1650 in_fd = open("/dev/null", O_RDONLY);
1652 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1656 out_fd = open("/dev/console", O_WRONLY);
1658 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1663 close(STDIN_FILENO);
1665 close(STDOUT_FILENO);
1667 close(STDERR_FILENO);
1678 main (int argc, char *argv[])
1680 extern char *optarg;
1687 if (getuid() != 0) {
1688 fprintf(stderr, "need to be root\n");
1692 /* make sure we don't lock any path */
1694 umask(umask(077) | 022);
1696 conf = alloc_config();
1701 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1705 //debug=1; /* ### comment me out ### */
1708 if (sizeof(optarg) > sizeof(char *) ||
1709 !isdigit(optarg[0]))
1712 conf->verbosity = atoi(optarg);
1735 return (child(NULL));