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;
171 pp->getprio_selected = 0;
180 orphan_paths (struct vectors * vecs, struct multipath * mpp)
185 vector_foreach_slot (vecs->pathvec, pp, i) {
186 if (pp->mpp == mpp) {
187 condlog(4, "%s is orphaned", pp->dev_t);
194 update_multipath_table (struct multipath *mpp, vector pathvec)
199 if (dm_get_map(mpp->alias, &mpp->size, mpp->params))
202 if (disassemble_map(pathvec, mpp->params, mpp))
209 update_multipath_status (struct multipath *mpp)
214 if(dm_get_status(mpp->alias, mpp->status))
217 if (disassemble_status(mpp->status, mpp))
224 update_multipath_strings (struct multipath *mpp, vector pathvec)
226 free_multipath_attributes(mpp);
227 free_pgvec(mpp->pg, KEEP_PATHS);
230 if (update_multipath_table(mpp, pathvec))
233 if (update_multipath_status(mpp))
240 set_multipath_wwid (struct multipath * mpp)
245 dm_get_uuid(mpp->alias, mpp->wwid);
249 * mpp->no_path_retry:
250 * -2 (QUEUE) : queue_if_no_path enabled, never turned off
251 * -1 (FAIL) : fail_if_no_path
252 * 0 (UNDEF) : nothing
253 * >0 : queue_if_no_path enabled, turned off after polling n times
256 update_queue_mode_del_path(struct multipath *mpp)
258 if (--mpp->nr_active == 0 && mpp->no_path_retry > 0) {
261 * meaning of +1: retry_tick may be decremented in
262 * checkerloop before starting retry.
264 mpp->retry_tick = mpp->no_path_retry * conf->checkint + 1;
265 condlog(1, "%s: Entering recovery mode: max_retries=%d",
266 mpp->alias, mpp->no_path_retry);
268 condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
272 update_queue_mode_add_path(struct multipath *mpp)
274 if (mpp->nr_active++ == 0 && mpp->no_path_retry > 0) {
275 /* come back to normal mode from retry mode */
277 dm_queue_if_no_path(mpp->alias, 1);
278 condlog(2, "%s: queue_if_no_path enabled", mpp->alias);
279 condlog(1, "%s: Recovered to normal mode", mpp->alias);
281 condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
285 set_no_path_retry(struct multipath *mpp)
288 mpp->nr_active = pathcount(mpp, PATH_UP);
289 select_no_path_retry(mpp);
291 switch (mpp->no_path_retry) {
292 case NO_PATH_RETRY_UNDEF:
294 case NO_PATH_RETRY_FAIL:
295 dm_queue_if_no_path(mpp->alias, 0);
297 case NO_PATH_RETRY_QUEUE:
298 dm_queue_if_no_path(mpp->alias, 1);
301 dm_queue_if_no_path(mpp->alias, 1);
302 if (mpp->nr_active == 0) {
303 /* Enter retry mode */
304 mpp->retry_tick = mpp->no_path_retry * conf->checkint;
305 condlog(1, "%s: Entering recovery mode: max_retries=%d",
306 mpp->alias, mpp->no_path_retry);
312 static struct hwentry *
313 extract_hwe_from_path(struct multipath * mpp)
316 struct pathgroup * pgp;
318 pgp = VECTOR_SLOT(mpp->pg, 0);
319 pp = VECTOR_SLOT(pgp->paths, 0);
325 remove_map (struct multipath * mpp, struct vectors * vecs)
329 stop_waiter_thread(mpp, vecs);
332 * clear references to this map
334 orphan_paths(vecs, mpp);
337 * purge the multipath vector
339 i = find_slot(vecs->mpvec, (void *)mpp);
340 vector_del_slot(vecs->mpvec, i);
345 free_multipath(mpp, KEEP_PATHS);
350 remove_maps (struct vectors * vecs)
353 struct multipath * mpp;
355 vector_foreach_slot (vecs->mpvec, mpp, i) {
356 remove_map(mpp, vecs);
360 vector_free(vecs->mpvec);
365 setup_multipath (struct vectors * vecs, struct multipath * mpp)
367 if (dm_get_info(mpp->alias, &mpp->dmi))
370 set_multipath_wwid(mpp);
371 mpp->mpe = find_mpe(mpp->wwid);
372 condlog(4, "discovered map %s", mpp->alias);
374 if (update_multipath_strings(mpp, vecs->pathvec))
377 adopt_paths(vecs, mpp);
378 mpp->hwe = extract_hwe_from_path(mpp);
379 select_pgfailback(mpp);
380 set_no_path_retry(mpp);
384 condlog(0, "%s: failed to setup multipath", mpp->alias);
385 remove_map(mpp, vecs);
390 need_switch_pathgroup (struct multipath * mpp, int refresh)
392 struct pathgroup * pgp;
396 if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
400 * Refresh path priority values
403 vector_foreach_slot (mpp->pg, pgp, i)
404 vector_foreach_slot (pgp->paths, pp, j)
405 pathinfo(pp, conf->hwtable, DI_PRIO);
407 mpp->bestpg = select_path_group(mpp);
409 if (mpp->bestpg != mpp->nextpg)
416 switch_pathgroup (struct multipath * mpp)
418 dm_switchgroup(mpp->alias, mpp->bestpg);
419 condlog(2, "%s: switch to path group #%i",
420 mpp->alias, mpp->bestpg);
424 update_multipath (struct vectors *vecs, char *mapname)
426 struct multipath *mpp;
427 struct pathgroup *pgp;
432 mpp = find_mp_by_alias(vecs->mpvec, mapname);
437 free_pgvec(mpp->pg, KEEP_PATHS);
440 if (setup_multipath(vecs, mpp))
441 goto out; /* mpp freed in setup_multipath */
444 * compare checkers states with DM states
446 vector_foreach_slot (mpp->pg, pgp, i) {
447 vector_foreach_slot (pgp->paths, pp, j) {
448 if (pp->dmstate != PSTATE_FAILED)
451 if (pp->state != PATH_DOWN) {
452 condlog(2, "%s: mark as failed", pp->dev_t);
453 pp->state = PATH_DOWN;
454 update_queue_mode_del_path(mpp);
458 * schedule the next check earlier
460 if (pp->tick > conf->checkint)
461 pp->tick = conf->checkint;
468 condlog(0, "failed to update multipath");
473 static sigset_t unblock_sighup(void)
478 sigaddset(&set, SIGHUP);
479 pthread_sigmask(SIG_UNBLOCK, &set, &old);
484 * returns the reschedule delay
485 * negative means *stop*
488 waiteventloop (struct event_thread * waiter)
494 if (!waiter->event_nr)
495 waiter->event_nr = dm_geteventnr(waiter->mapname);
497 if (!(waiter->dmt = dm_task_create(DM_DEVICE_WAITEVENT)))
500 if (!dm_task_set_name(waiter->dmt, waiter->mapname)) {
501 dm_task_destroy(waiter->dmt);
505 if (waiter->event_nr && !dm_task_set_event_nr(waiter->dmt,
507 dm_task_destroy(waiter->dmt);
511 dm_task_no_open_count(waiter->dmt);
513 /* accept wait interruption */
514 set = unblock_sighup();
516 /* interruption spits messages */
520 r = dm_task_run(waiter->dmt);
522 /* wait is over : event or interrupt */
523 pthread_sigmask(SIG_SETMASK, &set, NULL);
526 if (!r) /* wait interrupted by signal */
529 dm_task_destroy(waiter->dmt);
537 condlog(3, "%s: devmap event #%i",
538 waiter->mapname, waiter->event_nr);
543 * 1) a table reload, which means our mpp structure is
544 * obsolete : refresh it through update_multipath()
545 * 2) a path failed by DM : mark as such through
547 * 3) map has gone away : stop the thread.
548 * 4) a path reinstate : nothing to do
549 * 5) a switch group : nothing to do
551 pthread_cleanup_push(cleanup_lock, waiter->vecs->lock);
552 lock(waiter->vecs->lock);
553 r = update_multipath(waiter->vecs, waiter->mapname);
554 lock_cleanup_pop(waiter->vecs->lock);
557 return -1; /* stop the thread */
559 event_nr = dm_geteventnr(waiter->mapname);
561 if (waiter->event_nr == event_nr)
562 return 1; /* upon problem reschedule 1s later */
564 waiter->event_nr = event_nr;
566 return -1; /* never reach there */
570 waitevent (void * et)
573 struct event_thread *waiter;
575 mlockall(MCL_CURRENT | MCL_FUTURE);
577 waiter = (struct event_thread *)et;
578 pthread_cleanup_push(free_waiter, et);
581 r = waiteventloop(waiter);
589 pthread_cleanup_pop(1);
594 start_waiter_thread (struct multipath * mpp, struct vectors * vecs)
597 struct event_thread * wp;
602 if (pthread_attr_init(&attr))
605 pthread_attr_setstacksize(&attr, 32 * 1024);
606 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
613 mpp->waiter = (void *)wp;
614 strncpy(wp->mapname, mpp->alias, WWID_SIZE);
617 if (pthread_create(&wp->thread, &attr, waitevent, wp)) {
618 condlog(0, "%s: cannot create event checker", wp->mapname);
621 condlog(2, "%s: event checker started", wp->mapname);
628 condlog(0, "failed to start waiter thread");
633 uev_add_map (char * devname, struct vectors * vecs)
636 char dev_t[BLK_DEV_SIZE];
638 struct multipath * mpp;
640 if (sscanf(devname, "dm-%d", &minor) == 1 &&
641 !sysfs_get_dev(sysfs_path, devname, dev_t, BLK_DEV_SIZE) &&
642 sscanf(dev_t, "%d:%d", &major, &minor) == 2)
643 alias = dm_mapname(major, minor);
645 alias = STRDUP(devname);
650 if (!dm_type(alias, DEFAULT_TARGET)) {
651 condlog(4, "%s: not a multipath map", alias);
656 mpp = find_mp_by_alias(vecs->mpvec, alias);
660 * this should not happen,
661 * we missed a remove map event (not sent ?)
663 condlog(2, "%s: already registered", alias);
664 remove_map(mpp, vecs);
668 * now we can allocate
670 mpp = alloc_multipath();
677 if (setup_multipath(vecs, mpp))
678 return 1; /* mpp freed in setup_multipath */
680 if (!vector_alloc_slot(vecs->mpvec))
683 vector_set_slot(vecs->mpvec, mpp);
684 adopt_paths(vecs, mpp);
686 if (start_waiter_thread(mpp, vecs))
691 condlog(2, "%s: add devmap failed", mpp->alias);
692 remove_map(mpp, vecs);
697 uev_remove_map (char * devname, struct vectors * vecs)
700 struct multipath * mpp;
702 if (sscanf(devname, "dm-%d", &minor) == 1)
703 mpp = find_mp_by_minor(vecs->mpvec, minor);
705 mpp = find_mp_by_alias(vecs->mpvec, devname);
708 condlog(3, "%s: devmap not registered, can't remove",
713 condlog(2, "remove %s devmap", mpp->alias);
714 remove_map(mpp, vecs);
720 uev_add_path (char * devname, struct vectors * vecs)
724 pp = find_path_by_dev(vecs->pathvec, devname);
727 condlog(3, "%s: already in pathvec");
730 pp = store_pathinfo(vecs->pathvec, conf->hwtable,
731 devname, DI_SYSFS | DI_WWID);
734 condlog(0, "%s: failed to store path info", devname);
738 condlog(2, "%s: path checker registered", devname);
739 pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
742 condlog(4, "%s: ownership set to %s",
743 pp->dev_t, pp->mpp->alias);
745 condlog(4, "%s: orphaned", pp->dev_t);
753 uev_remove_path (char * devname, struct vectors * vecs)
758 pp = find_path_by_dev(vecs->pathvec, devname);
761 condlog(3, "%s: not in pathvec");
765 if (pp->mpp && pp->state == PATH_UP)
766 update_queue_mode_del_path(pp->mpp);
768 condlog(2, "remove %s path checker", devname);
769 i = find_slot(vecs->pathvec, (void *)pp);
770 vector_del_slot(vecs->pathvec, i);
777 show_paths (char ** r, int * len, struct vectors * vecs)
783 int maxlen = INITIAL_REPLY_LEN;
786 get_path_layout(vecs->pathvec);
787 reply = MALLOC(maxlen);
795 if (VECTOR_SIZE(vecs->pathvec) > 0)
796 c += snprint_path_header(c, reply + maxlen - c,
799 vector_foreach_slot(vecs->pathvec, pp, i)
800 c += snprint_path(c, reply + maxlen - c,
801 PRINT_PATH_CHECKER, pp);
803 again = ((c - reply) == (maxlen - 1));
806 reply = REALLOC(reply, maxlen *= 2);
810 *len = (int)(c - reply + 1);
815 show_maps (char ** r, int *len, struct vectors * vecs)
818 struct multipath * mpp;
821 int maxlen = INITIAL_REPLY_LEN;
824 get_map_layout(vecs->mpvec);
825 reply = MALLOC(maxlen);
832 if (VECTOR_SIZE(vecs->mpvec) > 0)
833 c += snprint_map_header(c, reply + maxlen - c,
836 vector_foreach_slot(vecs->mpvec, mpp, i)
837 c += snprint_map(c, reply + maxlen - c,
838 PRINT_MAP_FAILBACK, mpp);
840 again = ((c - reply) == (maxlen - 1));
843 reply = REALLOC(reply, maxlen *= 2);
846 *len = (int)(c - reply + 1);
851 dump_pathvec (char ** r, int * len, struct vectors * vecs)
858 *len = VECTOR_SIZE(vecs->pathvec) * sizeof(struct path);
859 reply = (char *)MALLOC(*len);
867 vector_foreach_slot (vecs->pathvec, pp, i) {
868 memcpy((void *)p, pp, sizeof(struct path));
869 p += sizeof(struct path);
872 /* return negative to hint caller not to add "ok" to the dump */
877 map_discovery (struct vectors * vecs)
880 struct multipath * mpp;
882 if (dm_get_maps(vecs->mpvec, "multipath"))
885 vector_foreach_slot (vecs->mpvec, mpp, i) {
886 if (setup_multipath(vecs, mpp))
888 start_waiter_thread(mpp, vecs);
895 reconfigure (struct vectors * vecs)
897 struct config * old = conf;
898 struct multipath * mpp;
904 if (load_config(DEFAULT_CONFIGFILE)) {
906 condlog(2, "reconfigure failed, continue with old config");
909 conf->verbosity = old->verbosity;
912 vector_foreach_slot (vecs->mpvec, mpp, i) {
913 mpp->mpe = find_mpe(mpp->wwid);
914 mpp->hwe = extract_hwe_from_path(mpp);
915 adopt_paths(vecs, mpp);
916 set_no_path_retry(mpp);
918 vector_foreach_slot (vecs->pathvec, pp, i) {
923 condlog(2, "reconfigured");
928 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
930 struct vectors * vecs;
935 vecs = (struct vectors *)trigger_data;
937 pthread_cleanup_push(cleanup_lock, vecs->lock);
940 r = parse_cmd(str, reply, len, vecs);
943 *reply = STRDUP("fail\n");
944 *len = strlen(*reply) + 1;
947 else if (!r && *len == 0) {
948 *reply = STRDUP("ok\n");
949 *len = strlen(*reply) + 1;
952 /* else if (r < 0) leave *reply alone */
954 lock_cleanup_pop(vecs->lock);
959 uev_discard(char * devpath)
964 * keep only block devices, discard partitions
966 if (sscanf(devpath, "/block/%10s", a) != 1 ||
967 sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
968 condlog(4, "discard event on %s", devpath);
975 uev_trigger (struct uevent * uev, void * trigger_data)
979 struct vectors * vecs;
981 vecs = (struct vectors *)trigger_data;
983 if (uev_discard(uev->devpath))
986 basename(uev->devpath, devname);
990 * device map add/remove event
992 if (!strncmp(devname, "dm-", 3)) {
993 if (!strncmp(uev->action, "add", 3)) {
994 r = uev_add_map(devname, vecs);
998 if (!strncmp(uev->action, "remove", 6)) {
999 r = uev_remove_map(devname, vecs);
1007 * path add/remove event
1009 if (blacklist(conf->blist, devname))
1012 if (!strncmp(uev->action, "add", 3)) {
1013 r = uev_add_path(devname, vecs);
1016 if (!strncmp(uev->action, "remove", 6)) {
1017 r = uev_remove_path(devname, vecs);
1027 ueventloop (void * ap)
1029 if (uevent_listen(&uev_trigger, ap))
1030 fprintf(stderr, "error starting uevent listener");
1036 uxlsnrloop (void * ap)
1041 if (alloc_handlers())
1044 add_handler(LIST+PATHS, cli_list_paths);
1045 add_handler(LIST+MAPS, cli_list_maps);
1046 add_handler(ADD+PATH, cli_add_path);
1047 add_handler(DEL+PATH, cli_del_path);
1048 add_handler(ADD+MAP, cli_add_map);
1049 add_handler(DEL+MAP, cli_del_map);
1050 add_handler(SWITCH+MAP+GROUP, cli_switch_group);
1051 add_handler(DUMP+PATHVEC, cli_dump_pathvec);
1052 add_handler(RECONFIGURE, cli_reconfigure);
1053 add_handler(SUSPEND+MAP, cli_suspend);
1054 add_handler(RESUME+MAP, cli_resume);
1055 add_handler(REINSTATE+PATH, cli_reinstate);
1056 add_handler(FAIL+PATH, cli_fail);
1058 uxsock_listen(&uxsock_trigger, ap);
1064 exit_daemon (int status)
1067 fprintf(stderr, "bad exit status. see daemon.log\n");
1069 condlog(3, "unlink pidfile");
1070 unlink(DEFAULT_PIDFILE);
1073 pthread_cond_signal(&exit_cond);
1074 unlock(&exit_mutex);
1080 fail_path (struct path * pp)
1085 condlog(2, "checker failed path %s in map %s",
1086 pp->dev_t, pp->mpp->alias);
1088 dm_fail_path(pp->mpp->alias, pp->dev_t);
1089 update_queue_mode_del_path(pp->mpp);
1093 * caller must have locked the path list before calling that function
1096 reinstate_path (struct path * pp)
1101 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
1102 condlog(0, "%s: reinstate failed", pp->dev_t);
1104 condlog(2, "%s: reinstated", pp->dev_t);
1105 update_queue_mode_add_path(pp->mpp);
1110 enable_group(struct path * pp)
1112 struct pathgroup * pgp;
1115 * if path is added through uev_add_path, pgindex can be unset.
1116 * next update_strings() will set it, upon map reload event.
1118 * we can safely return here, because upon map reload, all
1119 * PG will be enabled.
1121 if (!pp->mpp->pg || !pp->pgindex)
1124 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1126 if (pgp->status == PGSTATE_DISABLED) {
1127 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1128 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1133 mpvec_garbage_collector (struct vectors * vecs)
1135 struct multipath * mpp;
1138 vector_foreach_slot (vecs->mpvec, mpp, i) {
1139 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1140 condlog(2, "%s: remove dead map", mpp->alias);
1141 remove_map(mpp, vecs);
1148 defered_failback_tick (vector mpvec)
1150 struct multipath * mpp;
1153 vector_foreach_slot (mpvec, mpp, i) {
1155 * defered failback getting sooner
1157 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1158 mpp->failback_tick--;
1160 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1161 switch_pathgroup(mpp);
1167 retry_count_tick(vector mpvec)
1169 struct multipath *mpp;
1172 vector_foreach_slot (mpvec, mpp, i) {
1173 if (mpp->retry_tick) {
1174 condlog(4, "%s: Retrying.. No active path", mpp->alias);
1175 if(--mpp->retry_tick == 0) {
1176 dm_queue_if_no_path(mpp->alias, 0);
1177 condlog(2, "%s: Disable queueing", mpp->alias);
1184 checkerloop (void *ap)
1186 struct vectors *vecs;
1190 char checker_msg[MAX_CHECKER_MSG_SIZE];
1192 mlockall(MCL_CURRENT | MCL_FUTURE);
1194 memset(checker_msg, 0, MAX_CHECKER_MSG_SIZE);
1195 vecs = (struct vectors *)ap;
1197 condlog(2, "path checkers start up");
1200 * init the path check interval
1202 vector_foreach_slot (vecs->pathvec, pp, i) {
1203 pp->checkint = conf->checkint;
1207 pthread_cleanup_push(cleanup_lock, vecs->lock);
1211 vector_foreach_slot (vecs->pathvec, pp, i) {
1215 if (pp->tick && --pp->tick)
1216 continue; /* don't check this path yet */
1219 * provision a next check soonest,
1220 * in case we exit abnormaly from here
1222 pp->tick = conf->checkint;
1225 pathinfo(pp, conf->hwtable, DI_SYSFS);
1230 condlog(0, "%s: checkfn is void", pp->dev);
1233 newstate = pp->checkfn(pp->fd, checker_msg,
1234 &pp->checker_context);
1237 condlog(2, "%s: unusable path", pp->dev);
1238 pathinfo(pp, conf->hwtable, 0);
1242 if (newstate != pp->state) {
1243 pp->state = newstate;
1244 LOG_MSG(1, checker_msg);
1247 * upon state change, reset the checkint
1248 * to the shortest delay
1250 pp->checkint = conf->checkint;
1252 if (newstate == PATH_DOWN ||
1253 newstate == PATH_SHAKY ||
1254 update_multipath_strings(pp->mpp,
1257 * proactively fail path in the DM
1262 * cancel scheduled failback
1264 pp->mpp->failback_tick = 0;
1270 * reinstate this path
1275 * schedule [defered] failback
1277 if (pp->mpp->pgfailback > 0)
1278 pp->mpp->failback_tick =
1279 pp->mpp->pgfailback + 1;
1280 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
1281 need_switch_pathgroup(pp->mpp, 1))
1282 switch_pathgroup(pp->mpp);
1285 * if at least one path is up in a group, and
1286 * the group is disabled, re-enable it
1288 if (newstate == PATH_UP)
1291 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1292 LOG_MSG(4, checker_msg);
1294 * double the next check delay.
1295 * max at conf->max_checkint
1297 if (pp->checkint < (conf->max_checkint / 2))
1298 pp->checkint = 2 * pp->checkint;
1300 pp->checkint = conf->max_checkint;
1302 pp->tick = pp->checkint;
1303 condlog(4, "%s: delay next check %is",
1304 pp->dev_t, pp->tick);
1307 pp->state = newstate;
1310 * path prio refreshing
1312 condlog(4, "path prio refresh");
1313 pathinfo(pp, conf->hwtable, DI_PRIO);
1315 if (need_switch_pathgroup(pp->mpp, 0)) {
1316 if (pp->mpp->pgfailback > 0)
1317 pp->mpp->failback_tick =
1318 pp->mpp->pgfailback + 1;
1319 else if (pp->mpp->pgfailback ==
1320 -FAILBACK_IMMEDIATE)
1321 switch_pathgroup(pp->mpp);
1324 defered_failback_tick(vecs->mpvec);
1325 retry_count_tick(vecs->mpvec);
1330 condlog(4, "map garbage collection");
1331 mpvec_garbage_collector(vecs);
1335 lock_cleanup_pop(vecs->lock);
1341 static struct vectors *
1344 struct vectors * vecs;
1346 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1352 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1357 vecs->pathvec = vector_alloc();
1362 vecs->mpvec = vector_alloc();
1367 pthread_mutex_init(vecs->lock, NULL);
1372 vector_free(vecs->pathvec);
1377 condlog(0, "failed to init paths");
1382 signal_set(int signo, void (*func) (int))
1385 struct sigaction sig;
1386 struct sigaction osig;
1388 sig.sa_handler = func;
1389 sigemptyset(&sig.sa_mask);
1392 r = sigaction(signo, &sig, &osig);
1397 return (osig.sa_handler);
1403 condlog(3, "SIGHUP received");
1406 dbg_free_final(NULL);
1419 signal_set(SIGHUP, sighup);
1420 signal_set(SIGINT, sigend);
1421 signal_set(SIGTERM, sigend);
1422 signal_set(SIGKILL, sigend);
1429 static struct sched_param sched_param = {
1433 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1436 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1441 set_oom_adj (int val)
1445 fp = fopen("/proc/self/oom_adj", "w");
1450 fprintf(fp, "%i", val);
1455 child (void * param)
1457 pthread_t check_thr, uevent_thr, uxlsnr_thr;
1458 pthread_attr_t attr;
1459 struct vectors * vecs;
1461 mlockall(MCL_CURRENT | MCL_FUTURE);
1466 condlog(2, "--------start up--------");
1467 condlog(2, "read " DEFAULT_CONFIGFILE);
1469 if (load_config(DEFAULT_CONFIGFILE))
1472 setlogmask(LOG_UPTO(conf->verbosity + 3));
1475 * fill the voids left in the config file
1477 if (!conf->checkint) {
1478 conf->checkint = CHECKINT;
1479 conf->max_checkint = MAX_CHECKINT;
1482 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1496 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
1497 condlog(0, "can not find sysfs mount point");
1502 * fetch paths and multipaths lists
1503 * no paths and/or no multipaths are valid scenarii
1504 * vectors maintenance will be driven by events
1506 path_discovery(vecs->pathvec, conf, DI_SYSFS | DI_WWID | DI_CHECKER);
1507 map_discovery(vecs);
1512 pthread_attr_init(&attr);
1513 pthread_attr_setstacksize(&attr, 64 * 1024);
1514 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1516 pthread_create(&check_thr, &attr, checkerloop, vecs);
1517 pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1518 pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1520 pthread_cond_wait(&exit_cond, &exit_mutex);
1527 free_pathvec(vecs->pathvec, FREE_PATHS);
1529 pthread_cancel(check_thr);
1530 pthread_cancel(uevent_thr);
1531 pthread_cancel(uxlsnr_thr);
1535 free_handlers(handlers);
1540 pthread_mutex_destroy(vecs->lock);
1548 condlog(2, "--------shut down-------");
1557 dbg_free_final(NULL);
1569 if( (pid = fork()) < 0){
1570 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1578 if ( (pid = fork()) < 0)
1579 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1583 in_fd = open("/dev/null", O_RDONLY);
1585 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1589 out_fd = open("/dev/console", O_WRONLY);
1591 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1596 close(STDIN_FILENO);
1598 close(STDOUT_FILENO);
1600 close(STDERR_FILENO);
1611 main (int argc, char *argv[])
1613 extern char *optarg;
1620 if (getuid() != 0) {
1621 fprintf(stderr, "need to be root\n");
1625 /* make sure we don't lock any path */
1627 umask(umask(077) | 022);
1629 conf = alloc_config();
1634 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1638 //debug=1; /* ### comment me out ### */
1641 if (sizeof(optarg) > sizeof(char *) ||
1642 !isdigit(optarg[0]))
1645 conf->verbosity = atoi(optarg);
1668 return (child(NULL));