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 */
98 char mapname[WWID_SIZE];
102 static struct event_thread *
106 struct event_thread * wp;
108 wp = (struct event_thread *)MALLOC(sizeof(struct event_thread));
114 free_waiter (void * data)
116 struct event_thread * wp = (struct event_thread *)data;
119 dm_task_destroy(wp->dmt);
124 stop_waiter_thread (struct multipath * mpp, struct vectors * vecs)
126 struct event_thread * wp = (struct event_thread *)mpp->waiter;
129 condlog(3, "%s: no waiter thread", mpp->alias);
132 condlog(2, "%s: stop event checker thread", wp->mapname);
133 pthread_kill((pthread_t)wp->thread, SIGUSR1);
137 cleanup_lock (void * data)
139 pthread_mutex_unlock((pthread_mutex_t *)data);
143 * mpp->no_path_retry:
144 * -2 (QUEUE) : queue_if_no_path enabled, never turned off
145 * -1 (FAIL) : fail_if_no_path
146 * 0 (UNDEF) : nothing
147 * >0 : queue_if_no_path enabled, turned off after polling n times
150 update_queue_mode_del_path(struct multipath *mpp)
152 if (--mpp->nr_active == 0 && mpp->no_path_retry > 0) {
155 * meaning of +1: retry_tick may be decremented in
156 * checkerloop before starting retry.
158 mpp->retry_tick = mpp->no_path_retry * conf->checkint + 1;
159 condlog(1, "%s: Entering recovery mode: max_retries=%d",
160 mpp->alias, mpp->no_path_retry);
162 condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
166 update_queue_mode_add_path(struct multipath *mpp)
168 if (mpp->nr_active++ == 0 && mpp->no_path_retry > 0) {
169 /* come back to normal mode from retry mode */
171 dm_queue_if_no_path(mpp->alias, 1);
172 condlog(2, "%s: queue_if_no_path enabled", mpp->alias);
173 condlog(1, "%s: Recovered to normal mode", mpp->alias);
175 condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
179 need_switch_pathgroup (struct multipath * mpp, int refresh)
181 struct pathgroup * pgp;
185 if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
189 * Refresh path priority values
192 vector_foreach_slot (mpp->pg, pgp, i)
193 vector_foreach_slot (pgp->paths, pp, j)
194 pathinfo(pp, conf->hwtable, DI_PRIO);
196 mpp->bestpg = select_path_group(mpp);
198 if (mpp->bestpg != mpp->nextpg)
205 switch_pathgroup (struct multipath * mpp)
207 dm_switchgroup(mpp->alias, mpp->bestpg);
208 condlog(2, "%s: switch to path group #%i",
209 mpp->alias, mpp->bestpg);
213 coalesce_maps(struct vectors *vecs, vector nmpv)
215 struct multipath * ompp;
216 vector ompv = vecs->mpvec;
219 condlog(3, "coalesce_maps vs = %u", VECTOR_SIZE(ompv));
220 condlog(3, "coalesce_maps vs = %u", VECTOR_SIZE(nmpv));
222 vector_foreach_slot (ompv, ompp, i) {
223 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
225 * remove all current maps not allowed by the
226 * current configuration
228 if (dm_flush_map(ompp->alias, DEFAULT_TARGET)) {
229 condlog(0, "%s: unable to flush devmap",
232 * may be just because the device is open
234 if (!vector_alloc_slot(nmpv))
237 vector_set_slot(nmpv, ompp);
238 setup_multipath(vecs, ompp);
240 if ((j = find_slot(ompv, (void *)ompp)) != -1)
241 vector_del_slot(ompv, j);
246 condlog(3, "%s devmap removed", ompp->alias);
253 update_multipath (struct vectors *vecs, char *mapname)
255 struct multipath *mpp;
256 struct pathgroup *pgp;
261 mpp = find_mp_by_alias(vecs->mpvec, mapname);
266 free_pgvec(mpp->pg, KEEP_PATHS);
269 if (setup_multipath(vecs, mpp))
270 goto out; /* mpp freed in setup_multipath */
273 * compare checkers states with DM states
275 vector_foreach_slot (mpp->pg, pgp, i) {
276 vector_foreach_slot (pgp->paths, pp, j) {
277 if (pp->dmstate != PSTATE_FAILED)
280 if (pp->state != PATH_DOWN) {
281 condlog(2, "%s: mark as failed", pp->dev_t);
282 pp->state = PATH_DOWN;
283 update_queue_mode_del_path(mpp);
287 * schedule the next check earlier
289 if (pp->tick > conf->checkint)
290 pp->tick = conf->checkint;
297 condlog(0, "failed to update multipath");
302 static sigset_t unblock_signals(void)
307 sigaddset(&set, SIGHUP);
308 sigaddset(&set, SIGUSR1);
309 pthread_sigmask(SIG_UNBLOCK, &set, &old);
314 * returns the reschedule delay
315 * negative means *stop*
318 waiteventloop (struct event_thread * waiter)
324 if (!waiter->event_nr)
325 waiter->event_nr = dm_geteventnr(waiter->mapname);
327 if (!(waiter->dmt = dm_task_create(DM_DEVICE_WAITEVENT))) {
328 condlog(0, "%s: devmap event #%i dm_task_create error",
329 waiter->mapname, waiter->event_nr);
333 if (!dm_task_set_name(waiter->dmt, waiter->mapname)) {
334 condlog(0, "%s: devmap event #%i dm_task_set_name error",
335 waiter->mapname, waiter->event_nr);
336 dm_task_destroy(waiter->dmt);
340 if (waiter->event_nr && !dm_task_set_event_nr(waiter->dmt,
342 condlog(0, "%s: devmap event #%i dm_task_set_event_nr error",
343 waiter->mapname, waiter->event_nr);
344 dm_task_destroy(waiter->dmt);
348 dm_task_no_open_count(waiter->dmt);
350 /* accept wait interruption */
351 set = unblock_signals();
353 /* interruption spits messages */
357 r = dm_task_run(waiter->dmt);
359 /* wait is over : event or interrupt */
360 pthread_sigmask(SIG_SETMASK, &set, NULL);
363 if (!r) /* wait interrupted by signal */
366 dm_task_destroy(waiter->dmt);
374 condlog(3, "%s: devmap event #%i",
375 waiter->mapname, waiter->event_nr);
380 * 1) a table reload, which means our mpp structure is
381 * obsolete : refresh it through update_multipath()
382 * 2) a path failed by DM : mark as such through
384 * 3) map has gone away : stop the thread.
385 * 4) a path reinstate : nothing to do
386 * 5) a switch group : nothing to do
388 pthread_cleanup_push(cleanup_lock, waiter->vecs->lock);
389 lock(waiter->vecs->lock);
390 r = update_multipath(waiter->vecs, waiter->mapname);
391 lock_cleanup_pop(waiter->vecs->lock);
394 return -1; /* stop the thread */
396 event_nr = dm_geteventnr(waiter->mapname);
398 if (waiter->event_nr == event_nr)
399 return 1; /* upon problem reschedule 1s later */
401 waiter->event_nr = event_nr;
403 return -1; /* never reach there */
407 waitevent (void * et)
410 struct event_thread *waiter;
412 mlockall(MCL_CURRENT | MCL_FUTURE);
414 waiter = (struct event_thread *)et;
415 pthread_cleanup_push(free_waiter, et);
418 r = waiteventloop(waiter);
426 pthread_cleanup_pop(1);
431 start_waiter_thread (struct multipath * mpp, struct vectors * vecs)
434 struct event_thread * wp;
439 if (pthread_attr_init(&attr))
442 pthread_attr_setstacksize(&attr, 32 * 1024);
443 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
450 mpp->waiter = (void *)wp;
451 strncpy(wp->mapname, mpp->alias, WWID_SIZE);
454 if (pthread_create(&wp->thread, &attr, waitevent, wp)) {
455 condlog(0, "%s: cannot create event checker", wp->mapname);
458 condlog(2, "%s: event checker started", wp->mapname);
465 condlog(0, "failed to start waiter thread");
470 uev_add_map (char * devname, struct vectors * vecs)
473 char dev_t[BLK_DEV_SIZE];
475 struct multipath * mpp;
477 if (sscanf(devname, "dm-%d", &minor) == 1 &&
478 !sysfs_get_dev(sysfs_path, devname, dev_t, BLK_DEV_SIZE) &&
479 sscanf(dev_t, "%d:%d", &major, &minor) == 2)
480 alias = dm_mapname(major, minor);
482 alias = STRDUP(devname);
487 if (!dm_type(alias, DEFAULT_TARGET)) {
488 condlog(4, "%s: not a multipath map", alias);
493 mpp = find_mp_by_alias(vecs->mpvec, alias);
497 * Not really an error -- we generate our own uevent
498 * if we create a multipath mapped device as a result
501 condlog(0, "%s: spurious uevent, devmap already registered",
508 * now we can register the map
510 if ((mpp = add_map_without_path(vecs, minor, alias,
511 start_waiter_thread))) {
512 condlog(3, "%s devmap %s added", devname, alias);
516 condlog(0, "%s: uev_add_map failed", alias);
521 uev_remove_map (char * devname, struct vectors * vecs)
524 struct multipath * mpp;
526 if (sscanf(devname, "dm-%d", &minor) == 1)
527 mpp = find_mp_by_minor(vecs->mpvec, minor);
529 mpp = find_mp_by_alias(vecs->mpvec, devname);
532 condlog(3, "%s: devmap not registered, can't remove",
537 condlog(2, "remove %s devmap", mpp->alias);
538 remove_map(mpp, vecs, stop_waiter_thread, 1);
544 uev_add_path (char * devname, struct vectors * vecs)
548 pp = find_path_by_dev(vecs->pathvec, devname);
551 condlog(3, "%s: already in pathvec", devname);
554 pp = store_pathinfo(vecs->pathvec, conf->hwtable,
555 devname, DI_SYSFS | DI_WWID);
558 condlog(0, "%s: failed to store path info", devname);
562 condlog(2, "%s: path checker registered", devname);
563 pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
566 condlog(4, "%s: ownership set to %s",
567 pp->dev_t, pp->mpp->alias);
569 condlog(4, "%s: orphaned", pp->dev_t);
577 uev_remove_path (char * devname, struct vectors * vecs)
582 pp = find_path_by_dev(vecs->pathvec, devname);
585 condlog(3, "%s: not in pathvec", devname);
589 if (pp->mpp && pp->state == PATH_UP)
590 update_queue_mode_del_path(pp->mpp);
592 condlog(2, "remove %s path checker", devname);
593 i = find_slot(vecs->pathvec, (void *)pp);
594 vector_del_slot(vecs->pathvec, i);
601 show_paths (char ** r, int * len, struct vectors * vecs)
607 int maxlen = INITIAL_REPLY_LEN;
610 get_path_layout(vecs->pathvec);
611 reply = MALLOC(maxlen);
619 if (VECTOR_SIZE(vecs->pathvec) > 0)
620 c += snprint_path_header(c, reply + maxlen - c,
623 vector_foreach_slot(vecs->pathvec, pp, i)
624 c += snprint_path(c, reply + maxlen - c,
625 PRINT_PATH_CHECKER, pp);
627 again = ((c - reply) == (maxlen - 1));
630 reply = REALLOC(reply, maxlen *= 2);
634 *len = (int)(c - reply + 1);
639 show_maps (char ** r, int *len, struct vectors * vecs)
642 struct multipath * mpp;
645 int maxlen = INITIAL_REPLY_LEN;
648 get_map_layout(vecs->mpvec);
649 reply = MALLOC(maxlen);
656 if (VECTOR_SIZE(vecs->mpvec) > 0)
657 c += snprint_map_header(c, reply + maxlen - c,
660 vector_foreach_slot(vecs->mpvec, mpp, i)
661 c += snprint_map(c, reply + maxlen - c,
662 PRINT_MAP_FAILBACK, mpp);
664 again = ((c - reply) == (maxlen - 1));
667 reply = REALLOC(reply, maxlen *= 2);
670 *len = (int)(c - reply + 1);
675 dump_pathvec (char ** r, int * len, struct vectors * vecs)
682 *len = VECTOR_SIZE(vecs->pathvec) * sizeof(struct path);
683 reply = (char *)MALLOC(*len);
691 vector_foreach_slot (vecs->pathvec, pp, i) {
692 memcpy((void *)p, pp, sizeof(struct path));
693 p += sizeof(struct path);
696 /* return negative to hint caller not to add "ok" to the dump */
701 map_discovery (struct vectors * vecs)
704 struct multipath * mpp;
706 if (dm_get_maps(vecs->mpvec, "multipath"))
709 vector_foreach_slot (vecs->mpvec, mpp, i) {
710 if (setup_multipath(vecs, mpp))
712 if (start_waiter_thread(mpp, vecs))
720 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
722 struct vectors * vecs;
727 vecs = (struct vectors *)trigger_data;
729 pthread_cleanup_push(cleanup_lock, vecs->lock);
732 r = parse_cmd(str, reply, len, vecs);
735 *reply = STRDUP("fail\n");
736 *len = strlen(*reply) + 1;
739 else if (!r && *len == 0) {
740 *reply = STRDUP("ok\n");
741 *len = strlen(*reply) + 1;
744 /* else if (r < 0) leave *reply alone */
746 lock_cleanup_pop(vecs->lock);
751 uev_discard(char * devpath)
756 * keep only block devices, discard partitions
758 if (sscanf(devpath, "/block/%10s", a) != 1 ||
759 sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
760 condlog(4, "discard event on %s", devpath);
767 uev_trigger (struct uevent * uev, void * trigger_data)
771 struct vectors * vecs;
773 vecs = (struct vectors *)trigger_data;
775 if (uev_discard(uev->devpath))
778 basename(uev->devpath, devname);
782 * device map add/remove event
784 if (!strncmp(devname, "dm-", 3)) {
785 if (!strncmp(uev->action, "add", 3)) {
786 r = uev_add_map(devname, vecs);
790 if (!strncmp(uev->action, "remove", 6)) {
791 r = uev_remove_map(devname, vecs);
799 * path add/remove event
801 if (blacklist(conf->blist, devname))
804 if (!strncmp(uev->action, "add", 3)) {
805 r = uev_add_path(devname, vecs);
808 if (!strncmp(uev->action, "remove", 6)) {
809 r = uev_remove_path(devname, vecs);
819 ueventloop (void * ap)
821 if (uevent_listen(&uev_trigger, ap))
822 fprintf(stderr, "error starting uevent listener");
828 uxlsnrloop (void * ap)
833 if (alloc_handlers())
836 add_handler(LIST+PATHS, cli_list_paths);
837 add_handler(LIST+MAPS, cli_list_maps);
838 add_handler(ADD+PATH, cli_add_path);
839 add_handler(DEL+PATH, cli_del_path);
840 add_handler(ADD+MAP, cli_add_map);
841 add_handler(DEL+MAP, cli_del_map);
842 add_handler(SWITCH+MAP+GROUP, cli_switch_group);
843 add_handler(DUMP+PATHVEC, cli_dump_pathvec);
844 add_handler(RECONFIGURE, cli_reconfigure);
845 add_handler(SUSPEND+MAP, cli_suspend);
846 add_handler(RESUME+MAP, cli_resume);
847 add_handler(REINSTATE+PATH, cli_reinstate);
848 add_handler(FAIL+PATH, cli_fail);
850 uxsock_listen(&uxsock_trigger, ap);
856 exit_daemon (int status)
859 fprintf(stderr, "bad exit status. see daemon.log\n");
861 condlog(3, "unlink pidfile");
862 unlink(DEFAULT_PIDFILE);
865 pthread_cond_signal(&exit_cond);
872 fail_path (struct path * pp)
877 condlog(2, "checker failed path %s in map %s",
878 pp->dev_t, pp->mpp->alias);
880 dm_fail_path(pp->mpp->alias, pp->dev_t);
881 update_queue_mode_del_path(pp->mpp);
885 * caller must have locked the path list before calling that function
888 reinstate_path (struct path * pp)
893 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
894 condlog(0, "%s: reinstate failed", pp->dev_t);
896 condlog(2, "%s: reinstated", pp->dev_t);
897 update_queue_mode_add_path(pp->mpp);
902 enable_group(struct path * pp)
904 struct pathgroup * pgp;
907 * if path is added through uev_add_path, pgindex can be unset.
908 * next update_strings() will set it, upon map reload event.
910 * we can safely return here, because upon map reload, all
911 * PG will be enabled.
913 if (!pp->mpp->pg || !pp->pgindex)
916 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
918 if (pgp->status == PGSTATE_DISABLED) {
919 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
920 dm_enablegroup(pp->mpp->alias, pp->pgindex);
925 mpvec_garbage_collector (struct vectors * vecs)
927 struct multipath * mpp;
930 vector_foreach_slot (vecs->mpvec, mpp, i) {
931 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
932 condlog(2, "%s: remove dead map", mpp->alias);
933 remove_map(mpp, vecs, stop_waiter_thread, 1);
940 defered_failback_tick (vector mpvec)
942 struct multipath * mpp;
945 vector_foreach_slot (mpvec, mpp, i) {
947 * defered failback getting sooner
949 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
950 mpp->failback_tick--;
952 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
953 switch_pathgroup(mpp);
959 retry_count_tick(vector mpvec)
961 struct multipath *mpp;
964 vector_foreach_slot (mpvec, mpp, i) {
965 if (mpp->retry_tick) {
966 condlog(4, "%s: Retrying.. No active path", mpp->alias);
967 if(--mpp->retry_tick == 0) {
968 dm_queue_if_no_path(mpp->alias, 0);
969 condlog(2, "%s: Disable queueing", mpp->alias);
976 checkerloop (void *ap)
978 struct vectors *vecs;
982 char checker_msg[MAX_CHECKER_MSG_SIZE];
984 mlockall(MCL_CURRENT | MCL_FUTURE);
986 memset(checker_msg, 0, MAX_CHECKER_MSG_SIZE);
987 vecs = (struct vectors *)ap;
989 condlog(2, "path checkers start up");
992 * init the path check interval
994 vector_foreach_slot (vecs->pathvec, pp, i) {
995 pp->checkint = conf->checkint;
999 pthread_cleanup_push(cleanup_lock, vecs->lock);
1003 vector_foreach_slot (vecs->pathvec, pp, i) {
1007 if (pp->tick && --pp->tick)
1008 continue; /* don't check this path yet */
1011 * provision a next check soonest,
1012 * in case we exit abnormaly from here
1014 pp->tick = conf->checkint;
1017 pathinfo(pp, conf->hwtable, DI_SYSFS);
1022 condlog(0, "%s: checkfn is void", pp->dev);
1025 newstate = pp->checkfn(pp->fd, checker_msg,
1026 &pp->checker_context);
1029 condlog(2, "%s: unusable path", pp->dev);
1030 pathinfo(pp, conf->hwtable, 0);
1034 if (newstate != pp->state) {
1035 pp->state = newstate;
1036 LOG_MSG(1, checker_msg);
1039 * upon state change, reset the checkint
1040 * to the shortest delay
1042 pp->checkint = conf->checkint;
1044 if (newstate == PATH_DOWN ||
1045 newstate == PATH_SHAKY ||
1046 update_multipath_strings(pp->mpp,
1049 * proactively fail path in the DM
1054 * cancel scheduled failback
1056 pp->mpp->failback_tick = 0;
1062 * reinstate this path
1067 * schedule [defered] failback
1069 if (pp->mpp->pgfailback > 0)
1070 pp->mpp->failback_tick =
1071 pp->mpp->pgfailback + 1;
1072 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
1073 need_switch_pathgroup(pp->mpp, 1))
1074 switch_pathgroup(pp->mpp);
1077 * if at least one path is up in a group, and
1078 * the group is disabled, re-enable it
1080 if (newstate == PATH_UP)
1083 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1084 LOG_MSG(4, checker_msg);
1086 * double the next check delay.
1087 * max at conf->max_checkint
1089 if (pp->checkint < (conf->max_checkint / 2))
1090 pp->checkint = 2 * pp->checkint;
1092 pp->checkint = conf->max_checkint;
1094 pp->tick = pp->checkint;
1095 condlog(4, "%s: delay next check %is",
1096 pp->dev_t, pp->tick);
1099 pp->state = newstate;
1102 * path prio refreshing
1104 condlog(4, "path prio refresh");
1105 pathinfo(pp, conf->hwtable, DI_PRIO);
1107 if (need_switch_pathgroup(pp->mpp, 0)) {
1108 if (pp->mpp->pgfailback > 0)
1109 pp->mpp->failback_tick =
1110 pp->mpp->pgfailback + 1;
1111 else if (pp->mpp->pgfailback ==
1112 -FAILBACK_IMMEDIATE)
1113 switch_pathgroup(pp->mpp);
1116 defered_failback_tick(vecs->mpvec);
1117 retry_count_tick(vecs->mpvec);
1122 condlog(4, "map garbage collection");
1123 mpvec_garbage_collector(vecs);
1127 lock_cleanup_pop(vecs->lock);
1134 configure (struct vectors * vecs, int start_waiters)
1136 struct multipath * mpp;
1141 if (!(vecs->pathvec = vector_alloc()))
1144 if (!(vecs->mpvec = vector_alloc()))
1147 if (!(mpvec = vector_alloc()))
1151 * probe for current path (from sysfs) and map (from dm) sets
1153 path_discovery(vecs->pathvec, conf, DI_ALL);
1155 vector_foreach_slot (vecs->pathvec, pp, i)
1156 pp->checkint = conf->checkint;
1158 if (map_discovery(vecs))
1162 * create new set of maps & push changed ones into dm
1164 if (coalesce_paths(vecs, mpvec))
1168 * may need to remove some maps which are no longer relevant
1169 * e.g., due to blacklist changes in conf file
1171 if (coalesce_maps(vecs, mpvec))
1174 if (conf->verbosity > 2)
1175 vector_foreach_slot(mpvec, mpp, i)
1179 * purge dm of old maps
1181 remove_maps(vecs, NULL);
1184 * save new set of maps formed by considering current path state
1186 vecs->mpvec = mpvec;
1189 * start dm event waiter threads for these new maps
1191 vector_foreach_slot(vecs->mpvec, mpp, i) {
1192 if (setup_multipath(vecs, mpp))
1195 if (start_waiter_thread(mpp, vecs))
1202 reconfigure (struct vectors * vecs)
1204 struct config * old = conf;
1206 condlog(0, "reconfigure");
1209 * free old map and path vectors ... they use old conf state
1211 if (VECTOR_SIZE(vecs->mpvec))
1212 remove_maps(vecs, stop_waiter_thread);
1214 if (VECTOR_SIZE(vecs->pathvec))
1215 free_pathvec(vecs->pathvec, FREE_PATHS);
1219 if (load_config(DEFAULT_CONFIGFILE))
1222 conf->verbosity = old->verbosity;
1224 if (!conf->checkint) {
1225 conf->checkint = CHECKINT;
1226 conf->max_checkint = MAX_CHECKINT;
1233 static struct vectors *
1236 struct vectors * vecs;
1238 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1244 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1249 vecs->pathvec = vector_alloc();
1254 vecs->mpvec = vector_alloc();
1259 pthread_mutex_init(vecs->lock, NULL);
1264 vector_free(vecs->pathvec);
1269 condlog(0, "failed to init paths");
1274 signal_set(int signo, void (*func) (int))
1277 struct sigaction sig;
1278 struct sigaction osig;
1280 sig.sa_handler = func;
1281 sigemptyset(&sig.sa_mask);
1284 r = sigaction(signo, &sig, &osig);
1289 return (osig.sa_handler);
1295 condlog(3, "SIGHUP received");
1299 unlock(gvecs->lock);
1302 dbg_free_final(NULL);
1315 condlog(3, "SIGUSR1 received");
1321 signal_set(SIGHUP, sighup);
1322 signal_set(SIGUSR1, sigusr1);
1323 signal_set(SIGINT, sigend);
1324 signal_set(SIGTERM, sigend);
1325 signal_set(SIGKILL, sigend);
1332 static struct sched_param sched_param = {
1336 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1339 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1344 set_oom_adj (int val)
1348 fp = fopen("/proc/self/oom_adj", "w");
1353 fprintf(fp, "%i", val);
1358 child (void * param)
1360 pthread_t check_thr, uevent_thr, uxlsnr_thr;
1361 pthread_attr_t attr;
1362 struct vectors * vecs;
1364 mlockall(MCL_CURRENT | MCL_FUTURE);
1369 condlog(2, "--------start up--------");
1370 condlog(2, "read " DEFAULT_CONFIGFILE);
1372 if (load_config(DEFAULT_CONFIGFILE))
1375 setlogmask(LOG_UPTO(conf->verbosity + 3));
1378 * fill the voids left in the config file
1380 if (!conf->checkint) {
1381 conf->checkint = CHECKINT;
1382 conf->max_checkint = MAX_CHECKINT;
1385 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1394 vecs = gvecs = init_vecs();
1399 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
1400 condlog(0, "can not find sysfs mount point");
1405 * fetch and configure both paths and multipaths
1407 if (configure(vecs, 1)) {
1408 condlog(0, "failure during configuration");
1415 pthread_attr_init(&attr);
1416 pthread_attr_setstacksize(&attr, 64 * 1024);
1417 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1419 pthread_create(&check_thr, &attr, checkerloop, vecs);
1420 pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1421 pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1423 pthread_cond_wait(&exit_cond, &exit_mutex);
1429 remove_maps(vecs, stop_waiter_thread);
1430 free_pathvec(vecs->pathvec, FREE_PATHS);
1432 pthread_cancel(check_thr);
1433 pthread_cancel(uevent_thr);
1434 pthread_cancel(uxlsnr_thr);
1438 free_handlers(handlers);
1443 pthread_mutex_destroy(vecs->lock);
1451 condlog(2, "--------shut down-------");
1460 dbg_free_final(NULL);
1472 if( (pid = fork()) < 0){
1473 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1481 if ( (pid = fork()) < 0)
1482 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1486 in_fd = open("/dev/null", O_RDONLY);
1488 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1492 out_fd = open("/dev/console", O_WRONLY);
1494 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1499 close(STDIN_FILENO);
1501 close(STDOUT_FILENO);
1503 close(STDERR_FILENO);
1514 main (int argc, char *argv[])
1516 extern char *optarg;
1523 if (getuid() != 0) {
1524 fprintf(stderr, "need to be root\n");
1528 /* make sure we don't lock any path */
1530 umask(umask(077) | 022);
1532 conf = alloc_config();
1537 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1541 //debug=1; /* ### comment me out ### */
1544 if (sizeof(optarg) > sizeof(char *) ||
1545 !isdigit(optarg[0]))
1548 conf->verbosity = atoi(optarg);
1571 return (child(NULL));