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>
42 #include <devmapper.h>
44 #include <discovery.h>
48 #include <switchgroup.h>
49 #include <path_state.h>
57 #include "cli_handlers.h"
59 #define FILE_NAME_SIZE 256
62 #define LOG_MSG(a,b) \
64 condlog(a, "%s: %s", pp->dev_t, b); \
65 memset(b, 0, MAX_CHECKER_MSG_SIZE); \
70 fprintf(stderr, "%s:%s(%i) lock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
73 fprintf(stderr, "%s:%s(%i) unlock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
74 pthread_mutex_unlock(a)
75 #define lock_cleanup_pop(a) \
76 fprintf(stderr, "%s:%s(%i) unlock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
77 pthread_cleanup_pop(1);
79 #define lock(a) pthread_mutex_lock(a)
80 #define unlock(a) pthread_mutex_unlock(a)
81 #define lock_cleanup_pop(a) pthread_cleanup_pop(1);
84 pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
85 pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
94 char mapname[WWID_SIZE];
98 static struct event_thread *
102 struct event_thread * wp;
104 wp = (struct event_thread *)MALLOC(sizeof(struct event_thread));
110 free_waiter (void * data)
112 struct event_thread * wp = (struct event_thread *)data;
115 dm_task_destroy(wp->dmt);
120 stop_waiter_thread (struct multipath * mpp, struct vectors * vecs)
122 struct event_thread * wp = (struct event_thread *)mpp->waiter;
126 condlog(3, "%s: no waiter thread", mpp->alias);
132 condlog(3, "%s: thread not started", mpp->alias);
135 condlog(2, "%s: stop event checker thread", wp->mapname);
136 pthread_kill(thread, SIGHUP);
140 cleanup_lock (void * data)
142 pthread_mutex_unlock((pthread_mutex_t *)data);
146 adopt_paths (struct vectors * vecs, struct multipath * mpp)
154 vector_foreach_slot (vecs->pathvec, pp, i) {
155 if (!strncmp(mpp->wwid, pp->wwid, WWID_SIZE)) {
156 condlog(4, "%s ownership set", pp->dev_t);
163 orphan_path (struct path * pp)
167 pp->dmstate = PSTATE_UNDEF;
168 pp->checker_context = NULL;
179 orphan_paths (struct vectors * vecs, struct multipath * mpp)
184 vector_foreach_slot (vecs->pathvec, pp, i) {
185 if (pp->mpp == mpp) {
186 condlog(4, "%s is orphaned", pp->dev_t);
193 update_multipath_table (struct multipath *mpp, vector pathvec)
198 if (dm_get_map(mpp->alias, &mpp->size, mpp->params))
201 if (disassemble_map(pathvec, mpp->params, mpp))
208 update_multipath_status (struct multipath *mpp)
213 if(dm_get_status(mpp->alias, mpp->status))
216 if (disassemble_status(mpp->status, mpp))
223 update_multipath_strings (struct multipath *mpp, vector pathvec)
227 mpp->selector = NULL;
232 mpp->features = NULL;
235 if (mpp->hwhandler) {
236 FREE(mpp->hwhandler);
237 mpp->hwhandler = NULL;
240 free_pgvec(mpp->pg, KEEP_PATHS);
243 if (update_multipath_table(mpp, pathvec))
246 if (update_multipath_status(mpp))
253 set_multipath_wwid (struct multipath * mpp)
258 dm_get_uuid(mpp->alias, mpp->wwid);
262 pathcount (struct multipath *mpp, int state)
264 struct pathgroup *pgp;
269 vector_foreach_slot (mpp->pg, pgp, i)
270 vector_foreach_slot (pgp->paths, pp, j)
271 if (pp->state == state)
277 * mpp->no_path_retry:
278 * -2 (QUEUE) : queue_if_no_path enabled, never turned off
279 * -1 (FAIL) : fail_if_no_path
280 * 0 (UNDEF) : nothing
281 * >0 : queue_if_no_path enabled, turned off after polling n times
284 update_queue_mode_del_path(struct multipath *mpp)
286 if (--mpp->nr_active == 0 && mpp->no_path_retry > 0) {
289 * meaning of +1: retry_tick may be decremented in
290 * checkerloop before starting retry.
292 mpp->retry_tick = mpp->no_path_retry * conf->checkint + 1;
293 condlog(1, "%s: Entering recovery mode: max_retries=%d",
294 mpp->alias, mpp->no_path_retry);
296 condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
300 update_queue_mode_add_path(struct multipath *mpp)
302 if (mpp->nr_active++ == 0 && mpp->no_path_retry > 0) {
303 /* come back to normal mode from retry mode */
305 dm_queue_if_no_path(mpp->alias, 1);
306 condlog(2, "%s: queue_if_no_path enabled", mpp->alias);
307 condlog(1, "%s: Recovered to normal mode", mpp->alias);
309 condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
313 set_no_path_retry(struct multipath *mpp)
316 mpp->nr_active = pathcount(mpp, PATH_UP);
317 select_no_path_retry(mpp);
319 switch (mpp->no_path_retry) {
320 case NO_PATH_RETRY_UNDEF:
322 case NO_PATH_RETRY_FAIL:
323 dm_queue_if_no_path(mpp->alias, 0);
325 case NO_PATH_RETRY_QUEUE:
326 dm_queue_if_no_path(mpp->alias, 1);
329 dm_queue_if_no_path(mpp->alias, 1);
330 if (mpp->nr_active == 0) {
331 /* Enter retry mode */
332 mpp->retry_tick = mpp->no_path_retry * conf->checkint;
333 condlog(1, "%s: Entering recovery mode: max_retries=%d",
334 mpp->alias, mpp->no_path_retry);
340 static struct hwentry *
341 extract_hwe_from_path(struct multipath * mpp)
344 struct pathgroup * pgp;
346 pgp = VECTOR_SLOT(mpp->pg, 0);
347 pp = VECTOR_SLOT(pgp->paths, 0);
353 remove_map (struct multipath * mpp, struct vectors * vecs)
357 stop_waiter_thread(mpp, vecs);
360 * clear references to this map
362 orphan_paths(vecs, mpp);
365 * purge the multipath vector
367 i = find_slot(vecs->mpvec, (void *)mpp);
368 vector_del_slot(vecs->mpvec, i);
373 free_multipath(mpp, KEEP_PATHS);
378 remove_maps (struct vectors * vecs)
381 struct multipath * mpp;
383 vector_foreach_slot (vecs->mpvec, mpp, i) {
384 remove_map(mpp, vecs);
388 vector_free(vecs->mpvec);
393 setup_multipath (struct vectors * vecs, struct multipath * mpp)
395 if (dm_get_info(mpp->alias, &mpp->dmi))
398 set_multipath_wwid(mpp);
399 mpp->mpe = find_mpe(mpp->wwid);
400 condlog(4, "discovered map %s", mpp->alias);
402 if (update_multipath_strings(mpp, vecs->pathvec))
405 adopt_paths(vecs, mpp);
406 mpp->hwe = extract_hwe_from_path(mpp);
407 select_pgfailback(mpp);
408 set_no_path_retry(mpp);
412 condlog(0, "%s: failed to setup multipath", mpp->alias);
413 remove_map(mpp, vecs);
418 need_switch_pathgroup (struct multipath * mpp, int refresh)
420 struct pathgroup * pgp;
424 if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
428 * Refresh path priority values
431 vector_foreach_slot (mpp->pg, pgp, i)
432 vector_foreach_slot (pgp->paths, pp, j)
433 pathinfo(pp, conf->hwtable, DI_PRIO);
435 mpp->bestpg = select_path_group(mpp);
437 if (mpp->bestpg != mpp->nextpg)
444 switch_pathgroup (struct multipath * mpp)
446 dm_switchgroup(mpp->alias, mpp->bestpg);
447 condlog(2, "%s: switch to path group #%i",
448 mpp->alias, mpp->bestpg);
452 update_multipath (struct vectors *vecs, char *mapname)
454 struct multipath *mpp;
455 struct pathgroup *pgp;
460 mpp = find_mp_by_alias(vecs->mpvec, mapname);
465 free_pgvec(mpp->pg, KEEP_PATHS);
468 if (setup_multipath(vecs, mpp))
469 goto out; /* mpp freed in setup_multipath */
472 * compare checkers states with DM states
474 vector_foreach_slot (mpp->pg, pgp, i) {
475 vector_foreach_slot (pgp->paths, pp, j) {
476 if (pp->dmstate != PSTATE_FAILED)
479 if (pp->state != PATH_DOWN) {
480 condlog(2, "%s: mark as failed", pp->dev_t);
481 pp->state = PATH_DOWN;
482 update_queue_mode_del_path(mpp);
486 * schedule the next check earlier
488 if (pp->tick > conf->checkint)
489 pp->tick = conf->checkint;
496 condlog(0, "failed to update multipath");
501 static sigset_t unblock_sighup(void)
506 sigaddset(&set, SIGHUP);
507 pthread_sigmask(SIG_UNBLOCK, &set, &old);
512 * returns the reschedule delay
513 * negative means *stop*
516 waiteventloop (struct event_thread * waiter)
522 if (!waiter->event_nr)
523 waiter->event_nr = dm_geteventnr(waiter->mapname);
525 if (!(waiter->dmt = dm_task_create(DM_DEVICE_WAITEVENT)))
528 if (!dm_task_set_name(waiter->dmt, waiter->mapname)) {
529 dm_task_destroy(waiter->dmt);
533 if (waiter->event_nr && !dm_task_set_event_nr(waiter->dmt,
535 dm_task_destroy(waiter->dmt);
539 dm_task_no_open_count(waiter->dmt);
541 /* accept wait interruption */
542 set = unblock_sighup();
544 /* interruption spits messages */
548 r = dm_task_run(waiter->dmt);
550 /* wait is over : event or interrupt */
551 pthread_sigmask(SIG_SETMASK, &set, NULL);
554 if (!r) /* wait interrupted by signal */
557 dm_task_destroy(waiter->dmt);
565 condlog(3, "%s: devmap event #%i",
566 waiter->mapname, waiter->event_nr);
571 * 1) a table reload, which means our mpp structure is
572 * obsolete : refresh it through update_multipath()
573 * 2) a path failed by DM : mark as such through
575 * 3) map has gone away : stop the thread.
576 * 4) a path reinstate : nothing to do
577 * 5) a switch group : nothing to do
579 pthread_cleanup_push(cleanup_lock, waiter->vecs->lock);
580 lock(waiter->vecs->lock);
581 r = update_multipath(waiter->vecs, waiter->mapname);
582 lock_cleanup_pop(waiter->vecs->lock);
585 return -1; /* stop the thread */
587 event_nr = dm_geteventnr(waiter->mapname);
589 if (waiter->event_nr == event_nr)
590 return 1; /* upon problem reschedule 1s later */
592 waiter->event_nr = event_nr;
594 return -1; /* never reach there */
598 waitevent (void * et)
601 struct event_thread *waiter;
603 mlockall(MCL_CURRENT | MCL_FUTURE);
605 waiter = (struct event_thread *)et;
606 pthread_cleanup_push(free_waiter, et);
609 r = waiteventloop(waiter);
617 pthread_cleanup_pop(1);
622 start_waiter_thread (struct multipath * mpp, struct vectors * vecs)
625 struct event_thread * wp;
630 if (pthread_attr_init(&attr))
633 pthread_attr_setstacksize(&attr, 32 * 1024);
634 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
641 mpp->waiter = (void *)wp;
642 strncpy(wp->mapname, mpp->alias, WWID_SIZE);
645 if (pthread_create(&wp->thread, &attr, waitevent, wp)) {
646 condlog(0, "%s: cannot create event checker", wp->mapname);
649 condlog(2, "%s: event checker started", wp->mapname);
656 condlog(0, "failed to start waiter thread");
661 uev_add_map (char * devname, struct vectors * vecs)
664 char dev_t[BLK_DEV_SIZE];
666 struct multipath * mpp;
668 if (sscanf(devname, "dm-%d", &minor) == 1 &&
669 !sysfs_get_dev(sysfs_path, devname, dev_t, BLK_DEV_SIZE) &&
670 sscanf(dev_t, "%d:%d", &major, &minor) == 2)
671 alias = dm_mapname(major, minor);
673 alias = STRDUP(devname);
678 if (!dm_type(alias, DEFAULT_TARGET)) {
679 condlog(4, "%s: not a multipath map", alias);
684 mpp = find_mp_by_alias(vecs->mpvec, alias);
688 * this should not happen,
689 * we missed a remove map event (not sent ?)
691 condlog(2, "%s: already registered", alias);
692 remove_map(mpp, vecs);
696 * now we can allocate
698 mpp = alloc_multipath();
705 if (setup_multipath(vecs, mpp))
706 return 1; /* mpp freed in setup_multipath */
708 if (!vector_alloc_slot(vecs->mpvec))
711 vector_set_slot(vecs->mpvec, mpp);
712 adopt_paths(vecs, mpp);
714 if (start_waiter_thread(mpp, vecs))
719 condlog(2, "%s: add devmap failed", mpp->alias);
720 remove_map(mpp, vecs);
725 uev_remove_map (char * devname, struct vectors * vecs)
728 struct multipath * mpp;
730 if (sscanf(devname, "dm-%d", &minor) == 1)
731 mpp = find_mp_by_minor(vecs->mpvec, minor);
733 mpp = find_mp_by_alias(vecs->mpvec, devname);
736 condlog(3, "%s: devmap not registered, can't remove",
741 condlog(2, "remove %s devmap", mpp->alias);
742 remove_map(mpp, vecs);
748 uev_add_path (char * devname, struct vectors * vecs)
752 pp = find_path_by_dev(vecs->pathvec, devname);
755 condlog(3, "%s: already in pathvec");
758 pp = store_pathinfo(vecs->pathvec, conf->hwtable,
759 devname, DI_SYSFS | DI_WWID);
762 condlog(0, "%s: failed to store path info", devname);
766 condlog(2, "%s: path checker registered", devname);
767 pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
770 condlog(4, "%s: ownership set to %s",
771 pp->dev_t, pp->mpp->alias);
773 condlog(4, "%s: orphaned", pp->dev_t);
781 uev_remove_path (char * devname, struct vectors * vecs)
786 pp = find_path_by_dev(vecs->pathvec, devname);
789 condlog(3, "%s: not in pathvec");
793 if (pp->mpp && pp->state == PATH_UP)
794 update_queue_mode_del_path(pp->mpp);
796 condlog(2, "remove %s path checker", devname);
797 i = find_slot(vecs->pathvec, (void *)pp);
798 vector_del_slot(vecs->pathvec, i);
805 show_paths (char ** r, int * len, struct vectors * vecs)
811 struct path_layout pl;
813 get_path_layout(&pl, vecs->pathvec);
814 reply = MALLOC(MAX_REPLY_LEN);
821 if (VECTOR_SIZE(vecs->pathvec) > 0)
822 c += snprint_path_header(c, reply + MAX_REPLY_LEN - c,
823 PRINT_PATH_CHECKER, &pl);
825 vector_foreach_slot(vecs->pathvec, pp, i)
826 c += snprint_path(c, reply + MAX_REPLY_LEN - c,
827 PRINT_PATH_CHECKER, pp, &pl);
830 *len = (int)(c - reply + 1);
835 show_maps (char ** r, int *len, struct vectors * vecs)
838 struct multipath * mpp;
841 struct map_layout ml;
843 get_map_layout(&ml, vecs->mpvec);
844 reply = MALLOC(MAX_REPLY_LEN);
850 if (VECTOR_SIZE(vecs->mpvec) > 0)
851 c += snprint_map_header(c, reply + MAX_REPLY_LEN - c,
852 PRINT_MAP_FAILBACK, &ml);
854 vector_foreach_slot(vecs->mpvec, mpp, i)
855 c += snprint_map(c, reply + MAX_REPLY_LEN - c,
856 PRINT_MAP_FAILBACK, mpp, &ml);
859 *len = (int)(c - reply + 1);
864 dump_pathvec (char ** r, int * len, struct vectors * vecs)
871 *len = VECTOR_SIZE(vecs->pathvec) * sizeof(struct path);
872 reply = (char *)MALLOC(*len);
880 vector_foreach_slot (vecs->pathvec, pp, i) {
881 memcpy((void *)p, pp, sizeof(struct path));
882 p += sizeof(struct path);
885 /* return negative to hint caller not to add "ok" to the dump */
890 map_discovery (struct vectors * vecs)
893 struct multipath * mpp;
895 if (dm_get_maps(vecs->mpvec, "multipath"))
898 vector_foreach_slot (vecs->mpvec, mpp, i) {
899 if (setup_multipath(vecs, mpp))
901 start_waiter_thread(mpp, vecs);
908 reconfigure (struct vectors * vecs)
910 struct config * old = conf;
911 struct multipath * mpp;
917 if (load_config(DEFAULT_CONFIGFILE)) {
919 condlog(2, "reconfigure failed, continue with old config");
922 conf->verbosity = old->verbosity;
925 vector_foreach_slot (vecs->mpvec, mpp, i) {
926 mpp->mpe = find_mpe(mpp->wwid);
927 mpp->hwe = extract_hwe_from_path(mpp);
928 adopt_paths(vecs, mpp);
929 set_no_path_retry(mpp);
931 vector_foreach_slot (vecs->pathvec, pp, i) {
936 condlog(2, "reconfigured");
941 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
943 struct vectors * vecs;
948 vecs = (struct vectors *)trigger_data;
950 pthread_cleanup_push(cleanup_lock, vecs->lock);
953 r = parse_cmd(str, reply, len, vecs);
956 *reply = STRDUP("fail\n");
957 *len = strlen(*reply) + 1;
960 else if (!r && *len == 0) {
961 *reply = STRDUP("ok\n");
962 *len = strlen(*reply) + 1;
965 /* else if (r < 0) leave *reply alone */
967 lock_cleanup_pop(vecs->lock);
972 uev_discard(char * devpath)
977 * keep only block devices, discard partitions
979 if (sscanf(devpath, "/block/%10s", a) != 1 ||
980 sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
981 condlog(4, "discard event on %s", devpath);
988 uev_trigger (struct uevent * uev, void * trigger_data)
992 struct vectors * vecs;
994 vecs = (struct vectors *)trigger_data;
996 if (uev_discard(uev->devpath))
999 basename(uev->devpath, devname);
1003 * device map add/remove event
1005 if (!strncmp(devname, "dm-", 3)) {
1006 if (!strncmp(uev->action, "add", 3)) {
1007 r = uev_add_map(devname, vecs);
1011 if (!strncmp(uev->action, "remove", 6)) {
1012 r = uev_remove_map(devname, vecs);
1020 * path add/remove event
1022 if (blacklist(conf->blist, devname))
1025 if (!strncmp(uev->action, "add", 3)) {
1026 r = uev_add_path(devname, vecs);
1029 if (!strncmp(uev->action, "remove", 6)) {
1030 r = uev_remove_path(devname, vecs);
1040 ueventloop (void * ap)
1042 if (uevent_listen(&uev_trigger, ap))
1043 fprintf(stderr, "error starting uevent listener");
1049 uxlsnrloop (void * ap)
1054 if (alloc_handlers())
1057 add_handler(LIST+PATHS, cli_list_paths);
1058 add_handler(LIST+MAPS, cli_list_maps);
1059 add_handler(ADD+PATH, cli_add_path);
1060 add_handler(DEL+PATH, cli_del_path);
1061 add_handler(ADD+MAP, cli_add_map);
1062 add_handler(DEL+MAP, cli_del_map);
1063 add_handler(SWITCH+MAP+GROUP, cli_switch_group);
1064 add_handler(DUMP+PATHVEC, cli_dump_pathvec);
1065 add_handler(RECONFIGURE, cli_reconfigure);
1066 add_handler(SUSPEND+MAP, cli_suspend);
1067 add_handler(RESUME+MAP, cli_resume);
1068 add_handler(REINSTATE+PATH, cli_reinstate);
1069 add_handler(FAIL+PATH, cli_fail);
1071 uxsock_listen(&uxsock_trigger, ap);
1077 exit_daemon (int status)
1080 fprintf(stderr, "bad exit status. see daemon.log\n");
1082 condlog(3, "unlink pidfile");
1083 unlink(DEFAULT_PIDFILE);
1086 pthread_cond_signal(&exit_cond);
1087 unlock(&exit_mutex);
1093 fail_path (struct path * pp)
1098 condlog(2, "checker failed path %s in map %s",
1099 pp->dev_t, pp->mpp->alias);
1101 dm_fail_path(pp->mpp->alias, pp->dev_t);
1102 update_queue_mode_del_path(pp->mpp);
1106 * caller must have locked the path list before calling that function
1109 reinstate_path (struct path * pp)
1114 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
1115 condlog(0, "%s: reinstate failed", pp->dev_t);
1117 condlog(2, "%s: reinstated", pp->dev_t);
1118 update_queue_mode_add_path(pp->mpp);
1123 enable_group(struct path * pp)
1125 struct pathgroup * pgp;
1128 * if path is added through uev_add_path, pgindex can be unset.
1129 * next update_strings() will set it, upon map reload event.
1131 * we can safely return here, because upon map reload, all
1132 * PG will be enabled.
1137 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1139 if (pgp->status == PGSTATE_DISABLED) {
1140 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1141 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1146 mpvec_garbage_collector (struct vectors * vecs)
1148 struct multipath * mpp;
1151 vector_foreach_slot (vecs->mpvec, mpp, i) {
1152 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1153 condlog(2, "%s: remove dead map", mpp->alias);
1154 remove_map(mpp, vecs);
1161 defered_failback_tick (vector mpvec)
1163 struct multipath * mpp;
1166 vector_foreach_slot (mpvec, mpp, i) {
1168 * defered failback getting sooner
1170 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1171 mpp->failback_tick--;
1173 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1174 switch_pathgroup(mpp);
1180 retry_count_tick(vector mpvec)
1182 struct multipath *mpp;
1185 vector_foreach_slot (mpvec, mpp, i) {
1186 if (mpp->retry_tick) {
1187 condlog(4, "%s: Retrying.. No active path", mpp->alias);
1188 if(--mpp->retry_tick == 0) {
1189 dm_queue_if_no_path(mpp->alias, 0);
1190 condlog(2, "%s: Disable queueing", mpp->alias);
1197 checkerloop (void *ap)
1199 struct vectors *vecs;
1203 char checker_msg[MAX_CHECKER_MSG_SIZE];
1205 mlockall(MCL_CURRENT | MCL_FUTURE);
1207 memset(checker_msg, 0, MAX_CHECKER_MSG_SIZE);
1208 vecs = (struct vectors *)ap;
1210 condlog(2, "path checkers start up");
1213 * init the path check interval
1215 vector_foreach_slot (vecs->pathvec, pp, i) {
1216 pp->checkint = conf->checkint;
1220 pthread_cleanup_push(cleanup_lock, vecs->lock);
1224 vector_foreach_slot (vecs->pathvec, pp, i) {
1228 if (pp->tick && --pp->tick)
1229 continue; /* don't check this path yet */
1232 * provision a next check soonest,
1233 * in case we exit abnormaly from here
1235 pp->tick = conf->checkint;
1238 pathinfo(pp, conf->hwtable, DI_SYSFS);
1243 condlog(0, "%s: checkfn is void", pp->dev);
1246 newstate = pp->checkfn(pp->fd, checker_msg,
1247 &pp->checker_context);
1250 condlog(2, "%s: unusable path", pp->dev);
1251 pathinfo(pp, conf->hwtable, 0);
1255 if (newstate != pp->state) {
1256 pp->state = newstate;
1257 LOG_MSG(1, checker_msg);
1260 * upon state change, reset the checkint
1261 * to the shortest delay
1263 pp->checkint = conf->checkint;
1265 if (newstate == PATH_DOWN ||
1266 newstate == PATH_SHAKY) {
1268 * proactively fail path in the DM
1273 * cancel scheduled failback
1275 pp->mpp->failback_tick = 0;
1281 * reinstate this path
1286 * need to switch group ?
1288 update_multipath_strings(pp->mpp,
1292 * schedule defered failback
1294 if (pp->mpp->pgfailback > 0)
1295 pp->mpp->failback_tick =
1296 pp->mpp->pgfailback + 1;
1297 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
1298 need_switch_pathgroup(pp->mpp, 1))
1299 switch_pathgroup(pp->mpp);
1302 * if at least one path is up in a group, and
1303 * the group is disabled, re-enable it
1305 if (newstate == PATH_UP)
1308 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1309 LOG_MSG(4, checker_msg);
1311 * double the next check delay.
1312 * max at conf->max_checkint
1314 if (pp->checkint < (conf->max_checkint / 2))
1315 pp->checkint = 2 * pp->checkint;
1317 pp->checkint = conf->max_checkint;
1319 pp->tick = pp->checkint;
1320 condlog(4, "%s: delay next check %is",
1321 pp->dev_t, pp->tick);
1324 pp->state = newstate;
1327 * path prio refreshing
1329 condlog(4, "path prio refresh");
1330 pathinfo(pp, conf->hwtable, DI_PRIO);
1332 if (need_switch_pathgroup(pp->mpp, 0)) {
1333 if (pp->mpp->pgfailback > 0)
1334 pp->mpp->failback_tick =
1335 pp->mpp->pgfailback + 1;
1336 else if (pp->mpp->pgfailback ==
1337 -FAILBACK_IMMEDIATE)
1338 switch_pathgroup(pp->mpp);
1341 defered_failback_tick(vecs->mpvec);
1342 retry_count_tick(vecs->mpvec);
1347 condlog(4, "map garbage collection");
1348 mpvec_garbage_collector(vecs);
1352 lock_cleanup_pop(vecs->lock);
1358 static struct vectors *
1361 struct vectors * vecs;
1363 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1369 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1374 vecs->pathvec = vector_alloc();
1379 vecs->mpvec = vector_alloc();
1384 pthread_mutex_init(vecs->lock, NULL);
1389 vector_free(vecs->pathvec);
1394 condlog(0, "failed to init paths");
1399 signal_set(int signo, void (*func) (int))
1402 struct sigaction sig;
1403 struct sigaction osig;
1405 sig.sa_handler = func;
1406 sigemptyset(&sig.sa_mask);
1409 r = sigaction(signo, &sig, &osig);
1414 return (osig.sa_handler);
1420 condlog(3, "SIGHUP received");
1423 dbg_free_final(NULL);
1436 signal_set(SIGHUP, sighup);
1437 signal_set(SIGINT, sigend);
1438 signal_set(SIGTERM, sigend);
1439 signal_set(SIGKILL, sigend);
1446 static struct sched_param sched_param = {
1450 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1453 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1458 set_oom_adj (int val)
1462 fp = fopen("/proc/self/oom_adj", "w");
1467 fprintf(fp, "%i", val);
1472 child (void * param)
1474 pthread_t check_thr, uevent_thr, uxlsnr_thr;
1475 pthread_attr_t attr;
1476 struct vectors * vecs;
1478 mlockall(MCL_CURRENT | MCL_FUTURE);
1483 condlog(2, "--------start up--------");
1484 condlog(2, "read " DEFAULT_CONFIGFILE);
1486 if (load_config(DEFAULT_CONFIGFILE))
1489 setlogmask(LOG_UPTO(conf->verbosity + 3));
1492 * fill the voids left in the config file
1494 if (!conf->checkint) {
1495 conf->checkint = CHECKINT;
1496 conf->max_checkint = MAX_CHECKINT;
1499 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1508 vecs = init_paths();
1513 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
1514 condlog(0, "can not find sysfs mount point");
1519 * fetch paths and multipaths lists
1520 * no paths and/or no multipaths are valid scenarii
1521 * vectors maintenance will be driven by events
1523 path_discovery(vecs->pathvec, conf, DI_SYSFS | DI_WWID | DI_CHECKER);
1524 map_discovery(vecs);
1529 pthread_attr_init(&attr);
1530 pthread_attr_setstacksize(&attr, 64 * 1024);
1531 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1533 pthread_create(&check_thr, &attr, checkerloop, vecs);
1534 pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1535 pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1537 pthread_cond_wait(&exit_cond, &exit_mutex);
1544 free_pathvec(vecs->pathvec, FREE_PATHS);
1546 pthread_cancel(check_thr);
1547 pthread_cancel(uevent_thr);
1548 pthread_cancel(uxlsnr_thr);
1552 free_handlers(handlers);
1557 pthread_mutex_destroy(vecs->lock);
1565 condlog(2, "--------shut down-------");
1571 dbg_free_final(NULL);
1583 if( (pid = fork()) < 0){
1584 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1592 if ( (pid = fork()) < 0)
1593 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1597 in_fd = open("/dev/null", O_RDONLY);
1599 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1603 out_fd = open("/dev/console", O_WRONLY);
1605 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1610 close(STDIN_FILENO);
1612 close(STDOUT_FILENO);
1614 close(STDERR_FILENO);
1625 main (int argc, char *argv[])
1627 extern char *optarg;
1634 if (getuid() != 0) {
1635 fprintf(stderr, "need to be root\n");
1639 /* make sure we don't lock any path */
1641 umask(umask(077) | 022);
1643 conf = alloc_config();
1648 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1652 //debug=1; /* ### comment me out ### */
1655 if (sizeof(optarg) > sizeof(char *) ||
1656 !isdigit(optarg[0]))
1659 conf->verbosity = atoi(optarg);
1682 return (child(NULL));