2 * Copyright (c) 2004, 2005 Christophe Varoqui
3 * Copyright (c) 2005 Kiyoshi Ueda, NEC
4 * Copyright (c) 2005 Benjamin Marzinski, Redhat
5 * Copyright (c) 2005 Edward Goggin, EMC
9 #include <libdevmapper.h>
12 #include <sys/types.h>
19 #include <sysfs/libsysfs.h>
20 #include <sysfs/dlist.h>
39 #include <blacklist.h>
40 #include <structs_vec.h>
42 #include <devmapper.h>
44 #include <discovery.h>
48 #include <switchgroup.h>
50 #include <configure.h>
57 #include "cli_handlers.h"
59 #define FILE_NAME_SIZE 256
62 #define LOG_MSG(a,b) \
63 if (strlen(b)) condlog(a, "%s: %s", pp->dev_t, b);
67 fprintf(stderr, "%s:%s(%i) lock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
70 fprintf(stderr, "%s:%s(%i) unlock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
71 pthread_mutex_unlock(a)
72 #define lock_cleanup_pop(a) \
73 fprintf(stderr, "%s:%s(%i) unlock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
74 pthread_cleanup_pop(1);
76 #define lock(a) pthread_mutex_lock(a)
77 #define unlock(a) pthread_mutex_unlock(a)
78 #define lock_cleanup_pop(a) pthread_cleanup_pop(1);
81 pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
82 pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
87 struct vectors * gvecs; /* global copy of vecs for use in sig handlers */
89 static struct event_thread *
93 struct event_thread * wp;
95 wp = (struct event_thread *)MALLOC(sizeof(struct event_thread));
101 free_waiter (void * data)
103 struct event_thread * wp = (struct event_thread *)data;
106 * indicate in mpp that the wp is already freed storage
108 lock(wp->vecs->lock);
112 * be careful, mpp may already be freed -- null if so
114 wp->mpp->waiter = NULL;
116 condlog(3, "free_waiter, mpp freed before wp=%p,", wp);
118 unlock(wp->vecs->lock);
121 dm_task_destroy(wp->dmt);
127 stop_waiter_thread (struct multipath * mpp, struct vectors * vecs)
129 struct event_thread * wp = (struct event_thread *)mpp->waiter;
132 condlog(3, "%s: no waiter thread", mpp->alias);
135 condlog(2, "%s: stop event checker thread", wp->mapname);
136 pthread_kill((pthread_t)wp->thread, SIGUSR1);
140 cleanup_lock (void * data)
142 unlock((pthread_mutex_t *)data);
146 * mpp->no_path_retry:
147 * -2 (QUEUE) : queue_if_no_path enabled, never turned off
148 * -1 (FAIL) : fail_if_no_path
149 * 0 (UNDEF) : nothing
150 * >0 : queue_if_no_path enabled, turned off after polling n times
153 update_queue_mode_del_path(struct multipath *mpp)
155 if (--mpp->nr_active == 0 && mpp->no_path_retry > 0) {
158 * meaning of +1: retry_tick may be decremented in
159 * checkerloop before starting retry.
161 mpp->stat_queueing_timeouts++;
162 mpp->retry_tick = mpp->no_path_retry * conf->checkint + 1;
163 condlog(1, "%s: Entering recovery mode: max_retries=%d",
164 mpp->alias, mpp->no_path_retry);
166 condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
170 update_queue_mode_add_path(struct multipath *mpp)
172 if (mpp->nr_active++ == 0 && mpp->no_path_retry > 0) {
173 /* come back to normal mode from retry mode */
175 dm_queue_if_no_path(mpp->alias, 1);
176 condlog(2, "%s: queue_if_no_path enabled", mpp->alias);
177 condlog(1, "%s: Recovered to normal mode", mpp->alias);
179 condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
183 need_switch_pathgroup (struct multipath * mpp, int refresh)
185 struct pathgroup * pgp;
189 if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
193 * Refresh path priority values
196 vector_foreach_slot (mpp->pg, pgp, i)
197 vector_foreach_slot (pgp->paths, pp, j)
198 pathinfo(pp, conf->hwtable, DI_PRIO);
200 mpp->bestpg = select_path_group(mpp);
202 if (mpp->bestpg != mpp->nextpg)
209 switch_pathgroup (struct multipath * mpp)
211 mpp->stat_switchgroup++;
212 dm_switchgroup(mpp->alias, mpp->bestpg);
213 condlog(2, "%s: switch to path group #%i",
214 mpp->alias, mpp->bestpg);
218 coalesce_maps(struct vectors *vecs, vector nmpv)
220 struct multipath * ompp;
221 vector ompv = vecs->mpvec;
224 vector_foreach_slot (ompv, ompp, i) {
225 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
227 * remove all current maps not allowed by the
228 * current configuration
230 if (dm_flush_map(ompp->alias, DEFAULT_TARGET)) {
231 condlog(0, "%s: unable to flush devmap",
234 * may be just because the device is open
236 if (!vector_alloc_slot(nmpv))
239 vector_set_slot(nmpv, ompp);
240 setup_multipath(vecs, ompp);
242 if ((j = find_slot(ompv, (void *)ompp)) != -1)
243 vector_del_slot(ompv, j);
249 condlog(3, "%s devmap removed", ompp->alias);
257 update_multipath (struct vectors *vecs, char *mapname)
259 struct multipath *mpp;
260 struct pathgroup *pgp;
265 mpp = find_mp_by_alias(vecs->mpvec, mapname);
270 free_pgvec(mpp->pg, KEEP_PATHS);
273 if (setup_multipath(vecs, mpp))
274 goto out; /* mpp freed in setup_multipath */
277 * compare checkers states with DM states
279 vector_foreach_slot (mpp->pg, pgp, i) {
280 vector_foreach_slot (pgp->paths, pp, j) {
281 if (pp->dmstate != PSTATE_FAILED)
284 if (pp->state != PATH_DOWN) {
285 condlog(2, "%s: mark as failed", pp->dev_t);
286 mpp->stat_path_failures++;
287 pp->state = PATH_DOWN;
288 update_queue_mode_del_path(mpp);
292 * schedule the next check earlier
294 if (pp->tick > conf->checkint)
295 pp->tick = conf->checkint;
302 condlog(0, "failed to update multipath");
307 static sigset_t unblock_signals(void)
312 sigaddset(&set, SIGHUP);
313 sigaddset(&set, SIGUSR1);
314 pthread_sigmask(SIG_UNBLOCK, &set, &old);
319 * returns the reschedule delay
320 * negative means *stop*
323 waiteventloop (struct event_thread * waiter)
329 if (!waiter->event_nr)
330 waiter->event_nr = dm_geteventnr(waiter->mapname);
332 if (!(waiter->dmt = dm_task_create(DM_DEVICE_WAITEVENT))) {
333 condlog(0, "%s: devmap event #%i dm_task_create error",
334 waiter->mapname, waiter->event_nr);
338 if (!dm_task_set_name(waiter->dmt, waiter->mapname)) {
339 condlog(0, "%s: devmap event #%i dm_task_set_name error",
340 waiter->mapname, waiter->event_nr);
341 dm_task_destroy(waiter->dmt);
345 if (waiter->event_nr && !dm_task_set_event_nr(waiter->dmt,
347 condlog(0, "%s: devmap event #%i dm_task_set_event_nr error",
348 waiter->mapname, waiter->event_nr);
349 dm_task_destroy(waiter->dmt);
353 dm_task_no_open_count(waiter->dmt);
355 /* accept wait interruption */
356 set = unblock_signals();
358 /* interruption spits messages */
362 r = dm_task_run(waiter->dmt);
364 /* wait is over : event or interrupt */
365 pthread_sigmask(SIG_SETMASK, &set, NULL);
368 if (!r) /* wait interrupted by signal */
371 dm_task_destroy(waiter->dmt);
379 condlog(3, "%s: devmap event #%i",
380 waiter->mapname, waiter->event_nr);
385 * 1) a table reload, which means our mpp structure is
386 * obsolete : refresh it through update_multipath()
387 * 2) a path failed by DM : mark as such through
389 * 3) map has gone away : stop the thread.
390 * 4) a path reinstate : nothing to do
391 * 5) a switch group : nothing to do
393 pthread_cleanup_push(cleanup_lock, waiter->vecs->lock);
394 lock(waiter->vecs->lock);
395 r = update_multipath(waiter->vecs, waiter->mapname);
396 lock_cleanup_pop(waiter->vecs->lock);
399 return -1; /* stop the thread */
401 event_nr = dm_geteventnr(waiter->mapname);
403 if (waiter->event_nr == event_nr)
404 return 1; /* upon problem reschedule 1s later */
406 waiter->event_nr = event_nr;
408 return -1; /* never reach there */
412 waitevent (void * et)
415 struct event_thread *waiter;
417 mlockall(MCL_CURRENT | MCL_FUTURE);
419 waiter = (struct event_thread *)et;
420 pthread_cleanup_push(free_waiter, et);
423 r = waiteventloop(waiter);
431 pthread_cleanup_pop(1);
436 start_waiter_thread (struct multipath * mpp, struct vectors * vecs)
439 struct event_thread * wp;
444 if (pthread_attr_init(&attr))
447 pthread_attr_setstacksize(&attr, 32 * 1024);
448 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
455 mpp->waiter = (void *)wp;
456 strncpy(wp->mapname, mpp->alias, WWID_SIZE);
460 if (pthread_create(&wp->thread, &attr, waitevent, wp)) {
461 condlog(0, "%s: cannot create event checker", wp->mapname);
464 condlog(2, "%s: event checker started", wp->mapname);
471 condlog(0, "failed to start waiter thread");
476 flush_map(struct multipath * mpp, struct vectors * vecs)
479 * clear references to this map before flushing so we can ignore
480 * the spurious uevent we may generate with the dm_flush_map call below
482 if (dm_flush_map(mpp->alias, DEFAULT_TARGET)) {
484 * May not really be an error -- if the map was already flushed
485 * from the device mapper by dmsetup(8) for instance.
487 condlog(0, "%s: can't flush", mpp->alias);
492 condlog(3, "%s: devmap removed", mpp->alias);
495 orphan_paths(vecs->pathvec, mpp);
496 remove_map(mpp, vecs, stop_waiter_thread, 1);
502 uev_add_map (char * devname, struct vectors * vecs)
504 condlog(2, "%s: add map (uevent)", devname);
505 return ev_add_map(devname, vecs);
509 ev_add_map (char * devname, struct vectors * vecs)
512 char dev_t[BLK_DEV_SIZE];
515 struct multipath * mpp;
519 if (sscanf(devname, "dm-%d", &minor) == 1 &&
520 !sysfs_get_dev(sysfs_path, devname, dev_t, BLK_DEV_SIZE) &&
521 sscanf(dev_t, "%d:%d", &major, &minor) == 2)
522 alias = dm_mapname(major, minor);
524 alias = STRDUP(devname);
529 map_present = dm_map_present(alias);
531 if (map_present && dm_type(alias, DEFAULT_TARGET) <= 0) {
532 condlog(4, "%s: not a multipath map", alias);
537 mpp = find_mp_by_alias(vecs->mpvec, alias);
541 * Not really an error -- we generate our own uevent
542 * if we create a multipath mapped device as a result
545 condlog(0, "%s: devmap already registered",
552 * now we can register the map
554 if (map_present && (mpp = add_map_without_path(vecs, minor, alias,
555 start_waiter_thread))) {
556 condlog(3, "%s: devmap %s added", alias, devname);
559 refwwid = get_refwwid(devname, DEV_DEVMAP, vecs->pathvec);
562 r = coalesce_paths(vecs, NULL, refwwid);
567 condlog(3, "%s: devmap %s added", alias, devname);
569 condlog(0, "%s: uev_add_map %s failed", alias, devname);
577 uev_remove_map (char * devname, struct vectors * vecs)
579 condlog(2, "%s: remove map (uevent)", devname);
580 return ev_remove_map(devname, vecs);
584 ev_remove_map (char * devname, struct vectors * vecs)
586 struct multipath * mpp;
588 mpp = find_mp_by_str(vecs->mpvec, devname);
591 condlog(3, "%s: devmap not registered, can't remove",
595 flush_map(mpp, vecs);
601 uev_umount_map (char * devname, struct vectors * vecs)
603 struct multipath * mpp;
605 condlog(2, "%s: umount map (uevent)", devname);
607 mpp = find_mp_by_str(vecs->mpvec, devname);
612 update_mpp_paths(mpp, vecs->pathvec);
613 verify_paths(mpp, vecs, NULL);
615 if (!VECTOR_SIZE(mpp->paths))
616 flush_map(mpp, vecs);
622 uev_add_path (char * devname, struct vectors * vecs)
624 condlog(2, "%s: add path (uevent)", devname);
625 return (ev_add_path(devname, vecs) != 1)? 0 : 1;
636 ev_add_path (char * devname, struct vectors * vecs)
638 struct multipath * mpp;
640 char empty_buff[WWID_SIZE] = {0};
642 pp = find_path_by_dev(vecs->pathvec, devname);
645 condlog(0, "%s: spurious uevent, path already in pathvec, %p",
648 * allow reconfig of orphaned path here
650 if (pp->mpp) return 1;
654 * get path vital state
656 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
658 condlog(0, "%s: failed to store path info", devname);
661 pp->checkint = conf->checkint;
665 * need path UID to go any further
667 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
668 condlog(0, "%s: failed to get path uid", devname);
669 return 1; /* leave path added to pathvec */
671 if (blacklist_path(conf, pp)){
672 int i = find_slot(vecs->pathvec, (void *)pp);
674 vector_del_slot(vecs->pathvec, i);
678 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
681 if (adopt_paths(vecs->pathvec, mpp))
682 return 1; /* leave path added to pathvec */
684 verify_paths(mpp, vecs, NULL);
685 mpp->action = ACT_RELOAD;
688 if ((mpp = add_map_with_path(vecs, pp, 1)))
689 mpp->action = ACT_CREATE;
691 return 1; /* leave path added to pathvec */
695 * push the map to the device-mapper
697 if (setup_map(mpp)) {
698 condlog(0, "%s: failed to setup map for addition of new "
699 "path %s", mpp->alias, devname);
703 * reload the map for the multipath mapped device
705 if (domap(mpp) <= 0) {
706 condlog(0, "%s: failed in domap for addition of new "
707 "path %s", mpp->alias, devname);
709 * deal with asynchronous uevents :((
711 if (mpp->action == ACT_RELOAD) {
712 condlog(0, "%s: uev_add_path sleep", mpp->alias);
714 update_mpp_paths(mpp, vecs->pathvec);
723 * update our state from kernel regardless of create or reload
725 if (setup_multipath(vecs, mpp))
728 if (mpp->action == ACT_CREATE &&
729 start_waiter_thread(mpp, vecs))
732 condlog(3, "%s path added to devmap %s", devname, mpp->alias);
736 remove_map(mpp, vecs, NULL, 1);
741 uev_remove_path (char * devname, struct vectors * vecs)
743 condlog(2, "%s: remove path (uevent)", devname);
744 return ev_remove_path(devname, vecs);
748 ev_remove_path (char * devname, struct vectors * vecs)
750 struct multipath * mpp;
755 pp = find_path_by_dev(vecs->pathvec, devname);
758 condlog(0, "%s: spurious uevent, path not in pathvec", devname);
763 * avoid referring to the map of an orphanned path
765 if ((mpp = pp->mpp)) {
768 * remove the map IFF removing the last path
770 if (pathcount(mpp, PATH_WILD) > 1) {
771 vector rpvec = vector_alloc();
774 * transform the mp->pg vector of vectors of paths
775 * into a mp->params string to feed the device-mapper
777 update_mpp_paths(mpp, vecs->pathvec);
778 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
779 vector_del_slot(mpp->paths, i);
781 if (VECTOR_SIZE(mpp->paths) == 0) {
782 char alias[WWID_SIZE];
785 * flush_map will fail if the device is open
787 strncpy(alias, mpp->alias, WWID_SIZE);
788 if (flush_map(mpp, vecs))
791 condlog(3, "%s: removed map after removing"
792 " multiple paths", alias);
795 if (setup_map(mpp)) {
796 condlog(0, "%s: failed to setup map for"
797 " removal of path %s", mpp->alias, devname);
798 free_pathvec(rpvec, KEEP_PATHS);
804 mpp->action = ACT_RELOAD;
805 if (domap(mpp) <= 0) {
806 condlog(0, "%s: failed in domap for "
807 "removal of path %s",
808 mpp->alias, devname);
810 * Delete path from pathvec so that
811 * update_mpp_paths wont find it later
812 * when/if another path is removed.
814 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
815 vector_del_slot(vecs->pathvec, i);
820 * update our state from kernel
822 if (setup_multipath(vecs, mpp)) {
823 free_pathvec(rpvec, KEEP_PATHS);
827 condlog(3, "%s path removed from devmap %s",
828 devname, mpp->alias);
830 free_pathvec(rpvec, KEEP_PATHS);
833 char alias[WWID_SIZE];
836 * flush_map will fail if the device is open
838 strncpy(alias, mpp->alias, WWID_SIZE);
839 if (flush_map(mpp, vecs))
842 condlog(3, "%s: removed map", alias);
847 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
848 vector_del_slot(vecs->pathvec, i);
855 remove_map(mpp, vecs, stop_waiter_thread, 1);
860 map_discovery (struct vectors * vecs)
863 struct multipath * mpp;
865 if (dm_get_maps(vecs->mpvec, "multipath"))
868 vector_foreach_slot (vecs->mpvec, mpp, i)
869 if (setup_multipath(vecs, mpp))
876 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
878 struct vectors * vecs;
883 vecs = (struct vectors *)trigger_data;
885 pthread_cleanup_push(cleanup_lock, vecs->lock);
888 r = parse_cmd(str, reply, len, vecs);
891 *reply = STRDUP("fail\n");
892 *len = strlen(*reply) + 1;
895 else if (!r && *len == 0) {
896 *reply = STRDUP("ok\n");
897 *len = strlen(*reply) + 1;
900 /* else if (r < 0) leave *reply alone */
902 lock_cleanup_pop(vecs->lock);
907 uev_discard(char * devpath)
912 * keep only block devices, discard partitions
914 if (sscanf(devpath, "/block/%10s", a) != 1 ||
915 sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
916 condlog(4, "discard event on %s", devpath);
923 uev_trigger (struct uevent * uev, void * trigger_data)
927 struct vectors * vecs;
929 vecs = (struct vectors *)trigger_data;
931 if (uev_discard(uev->devpath))
934 basename(uev->devpath, devname);
938 * device map add/remove event
940 if (!strncmp(devname, "dm-", 3)) {
941 if (!strncmp(uev->action, "add", 3)) {
942 r = uev_add_map(devname, vecs);
945 if (!strncmp(uev->action, "remove", 6)) {
946 r = uev_remove_map(devname, vecs);
949 if (!strncmp(uev->action, "umount", 6)) {
950 r = uev_umount_map(devname, vecs);
957 * path add/remove event
959 if (blacklist(conf->blist_devnode, devname))
962 if (!strncmp(uev->action, "add", 3)) {
963 r = uev_add_path(devname, vecs);
966 if (!strncmp(uev->action, "remove", 6)) {
967 r = uev_remove_path(devname, vecs);
977 ueventloop (void * ap)
979 if (uevent_listen(&uev_trigger, ap))
980 fprintf(stderr, "error starting uevent listener");
986 uxlsnrloop (void * ap)
991 if (alloc_handlers())
994 add_handler(LIST+PATHS, cli_list_paths);
995 add_handler(LIST+MAPS, cli_list_maps);
996 add_handler(LIST+MAPS+STATUS, cli_list_maps_status);
997 add_handler(LIST+MAPS+STATS, cli_list_maps_stats);
998 add_handler(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
999 add_handler(LIST+TOPOLOGY, cli_list_maps_topology);
1000 add_handler(LIST+MAP+TOPOLOGY, cli_list_map_topology);
1001 add_handler(LIST+CONFIG, cli_list_config);
1002 add_handler(ADD+PATH, cli_add_path);
1003 add_handler(DEL+PATH, cli_del_path);
1004 add_handler(ADD+MAP, cli_add_map);
1005 add_handler(DEL+MAP, cli_del_map);
1006 add_handler(SWITCH+MAP+GROUP, cli_switch_group);
1007 add_handler(RECONFIGURE, cli_reconfigure);
1008 add_handler(SUSPEND+MAP, cli_suspend);
1009 add_handler(RESUME+MAP, cli_resume);
1010 add_handler(REINSTATE+PATH, cli_reinstate);
1011 add_handler(FAIL+PATH, cli_fail);
1013 uxsock_listen(&uxsock_trigger, ap);
1019 exit_daemon (int status)
1022 fprintf(stderr, "bad exit status. see daemon.log\n");
1024 condlog(3, "unlink pidfile");
1025 unlink(DEFAULT_PIDFILE);
1028 pthread_cond_signal(&exit_cond);
1029 unlock(&exit_mutex);
1035 fail_path (struct path * pp)
1040 condlog(2, "checker failed path %s in map %s",
1041 pp->dev_t, pp->mpp->alias);
1043 dm_fail_path(pp->mpp->alias, pp->dev_t);
1044 update_queue_mode_del_path(pp->mpp);
1048 * caller must have locked the path list before calling that function
1051 reinstate_path (struct path * pp)
1056 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
1057 condlog(0, "%s: reinstate failed", pp->dev_t);
1059 condlog(2, "%s: reinstated", pp->dev_t);
1060 update_queue_mode_add_path(pp->mpp);
1065 enable_group(struct path * pp)
1067 struct pathgroup * pgp;
1070 * if path is added through uev_add_path, pgindex can be unset.
1071 * next update_strings() will set it, upon map reload event.
1073 * we can safely return here, because upon map reload, all
1074 * PG will be enabled.
1076 if (!pp->mpp->pg || !pp->pgindex)
1079 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1081 if (pgp->status == PGSTATE_DISABLED) {
1082 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1083 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1088 mpvec_garbage_collector (struct vectors * vecs)
1090 struct multipath * mpp;
1093 vector_foreach_slot (vecs->mpvec, mpp, i) {
1094 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1095 condlog(2, "%s: remove dead map", mpp->alias);
1096 remove_map(mpp, vecs, stop_waiter_thread, 1);
1103 defered_failback_tick (vector mpvec)
1105 struct multipath * mpp;
1108 vector_foreach_slot (mpvec, mpp, i) {
1110 * defered failback getting sooner
1112 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1113 mpp->failback_tick--;
1115 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1116 switch_pathgroup(mpp);
1122 retry_count_tick(vector mpvec)
1124 struct multipath *mpp;
1127 vector_foreach_slot (mpvec, mpp, i) {
1128 if (mpp->retry_tick) {
1129 mpp->stat_total_queueing_time++;
1130 condlog(4, "%s: Retrying.. No active path", mpp->alias);
1131 if(--mpp->retry_tick == 0) {
1132 dm_queue_if_no_path(mpp->alias, 0);
1133 condlog(2, "%s: Disable queueing", mpp->alias);
1140 checkerloop (void *ap)
1142 struct vectors *vecs;
1147 mlockall(MCL_CURRENT | MCL_FUTURE);
1148 vecs = (struct vectors *)ap;
1149 condlog(2, "path checkers start up");
1152 * init the path check interval
1154 vector_foreach_slot (vecs->pathvec, pp, i) {
1155 pp->checkint = conf->checkint;
1159 pthread_cleanup_push(cleanup_lock, vecs->lock);
1163 vector_foreach_slot (vecs->pathvec, pp, i) {
1167 if (pp->tick && --pp->tick)
1168 continue; /* don't check this path yet */
1171 * provision a next check soonest,
1172 * in case we exit abnormaly from here
1174 pp->tick = conf->checkint;
1176 if (!checker_selected(&pp->checker)) {
1177 pathinfo(pp, conf->hwtable, DI_SYSFS);
1181 if (!checker_selected(&pp->checker)) {
1182 condlog(0, "%s: checker is not set", pp->dev);
1185 newstate = checker_check(&pp->checker);
1188 condlog(2, "%s: unusable path", pp->dev);
1189 pathinfo(pp, conf->hwtable, 0);
1193 if (newstate != pp->state) {
1194 pp->state = newstate;
1195 LOG_MSG(1, checker_message(&pp->checker));
1198 * upon state change, reset the checkint
1199 * to the shortest delay
1201 pp->checkint = conf->checkint;
1203 if (newstate == PATH_DOWN ||
1204 newstate == PATH_SHAKY ||
1205 update_multipath_strings(pp->mpp,
1208 * proactively fail path in the DM
1213 * cancel scheduled failback
1215 pp->mpp->failback_tick = 0;
1217 pp->mpp->stat_path_failures++;
1222 * reinstate this path
1227 * schedule [defered] failback
1229 if (pp->mpp->pgfailback > 0)
1230 pp->mpp->failback_tick =
1231 pp->mpp->pgfailback + 1;
1232 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
1233 need_switch_pathgroup(pp->mpp, 1))
1234 switch_pathgroup(pp->mpp);
1237 * if at least one path is up in a group, and
1238 * the group is disabled, re-enable it
1240 if (newstate == PATH_UP)
1243 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1244 LOG_MSG(4, checker_message(&pp->checker));
1246 * double the next check delay.
1247 * max at conf->max_checkint
1249 if (pp->checkint < (conf->max_checkint / 2))
1250 pp->checkint = 2 * pp->checkint;
1252 pp->checkint = conf->max_checkint;
1254 pp->tick = pp->checkint;
1255 condlog(4, "%s: delay next check %is",
1256 pp->dev_t, pp->tick);
1259 pp->state = newstate;
1262 * path prio refreshing
1264 condlog(4, "path prio refresh");
1265 pathinfo(pp, conf->hwtable, DI_PRIO);
1267 if (need_switch_pathgroup(pp->mpp, 0)) {
1268 if (pp->mpp->pgfailback > 0 &&
1269 pp->mpp->failback_tick <= 0)
1270 pp->mpp->failback_tick =
1271 pp->mpp->pgfailback + 1;
1272 else if (pp->mpp->pgfailback ==
1273 -FAILBACK_IMMEDIATE)
1274 switch_pathgroup(pp->mpp);
1277 defered_failback_tick(vecs->mpvec);
1278 retry_count_tick(vecs->mpvec);
1283 condlog(4, "map garbage collection");
1284 mpvec_garbage_collector(vecs);
1288 lock_cleanup_pop(vecs->lock);
1295 configure (struct vectors * vecs, int start_waiters)
1297 struct multipath * mpp;
1302 if (!(vecs->pathvec = vector_alloc()))
1305 if (!(vecs->mpvec = vector_alloc()))
1308 if (!(mpvec = vector_alloc()))
1312 * probe for current path (from sysfs) and map (from dm) sets
1314 path_discovery(vecs->pathvec, conf, DI_ALL);
1316 vector_foreach_slot (vecs->pathvec, pp, i){
1317 if (blacklist_path(conf, pp)){
1318 vector_del_slot(vecs->pathvec, i);
1323 pp->checkint = conf->checkint;
1325 if (map_discovery(vecs))
1329 * create new set of maps & push changed ones into dm
1331 if (coalesce_paths(vecs, mpvec, NULL))
1335 * may need to remove some maps which are no longer relevant
1336 * e.g., due to blacklist changes in conf file
1338 if (coalesce_maps(vecs, mpvec))
1343 if (conf->verbosity > 2)
1344 vector_foreach_slot(mpvec, mpp, i)
1348 * purge dm of old maps
1350 remove_maps(vecs, NULL);
1353 * save new set of maps formed by considering current path state
1355 vecs->mpvec = mpvec;
1358 * start dm event waiter threads for these new maps
1360 vector_foreach_slot(vecs->mpvec, mpp, i) {
1361 if (setup_multipath(vecs, mpp))
1364 if (start_waiter_thread(mpp, vecs))
1371 reconfigure (struct vectors * vecs)
1373 struct config * old = conf;
1376 * free old map and path vectors ... they use old conf state
1378 if (VECTOR_SIZE(vecs->mpvec))
1379 remove_maps(vecs, stop_waiter_thread);
1381 if (VECTOR_SIZE(vecs->pathvec))
1382 free_pathvec(vecs->pathvec, FREE_PATHS);
1386 if (load_config(DEFAULT_CONFIGFILE))
1389 conf->verbosity = old->verbosity;
1391 if (!conf->checkint) {
1392 conf->checkint = DEFAULT_CHECKINT;
1393 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1400 static struct vectors *
1403 struct vectors * vecs;
1405 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1411 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1416 vecs->pathvec = vector_alloc();
1421 vecs->mpvec = vector_alloc();
1426 pthread_mutex_init(vecs->lock, NULL);
1431 vector_free(vecs->pathvec);
1436 condlog(0, "failed to init paths");
1441 signal_set(int signo, void (*func) (int))
1444 struct sigaction sig;
1445 struct sigaction osig;
1447 sig.sa_handler = func;
1448 sigemptyset(&sig.sa_mask);
1451 r = sigaction(signo, &sig, &osig);
1456 return (osig.sa_handler);
1462 condlog(2, "reconfigure (SIGHUP)");
1466 unlock(gvecs->lock);
1469 dbg_free_final(NULL);
1482 condlog(3, "SIGUSR1 received");
1488 signal_set(SIGHUP, sighup);
1489 signal_set(SIGUSR1, sigusr1);
1490 signal_set(SIGINT, sigend);
1491 signal_set(SIGTERM, sigend);
1492 signal_set(SIGKILL, sigend);
1499 static struct sched_param sched_param = {
1503 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1506 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1511 set_oom_adj (int val)
1515 fp = fopen("/proc/self/oom_adj", "w");
1520 fprintf(fp, "%i", val);
1525 child (void * param)
1527 pthread_t check_thr, uevent_thr, uxlsnr_thr;
1528 pthread_attr_t attr;
1529 struct vectors * vecs;
1531 mlockall(MCL_CURRENT | MCL_FUTURE);
1536 condlog(2, "--------start up--------");
1537 condlog(2, "read " DEFAULT_CONFIGFILE);
1539 if (load_config(DEFAULT_CONFIGFILE))
1542 setlogmask(LOG_UPTO(conf->verbosity + 3));
1545 * fill the voids left in the config file
1547 if (!conf->checkint) {
1548 conf->checkint = DEFAULT_CHECKINT;
1549 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1552 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1561 vecs = gvecs = init_vecs();
1566 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
1567 condlog(0, "can not find sysfs mount point");
1572 * fetch and configure both paths and multipaths
1574 if (configure(vecs, 1)) {
1575 condlog(0, "failure during configuration");
1582 pthread_attr_init(&attr);
1583 pthread_attr_setstacksize(&attr, 64 * 1024);
1584 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1586 pthread_create(&check_thr, &attr, checkerloop, vecs);
1587 pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1588 pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1590 pthread_cond_wait(&exit_cond, &exit_mutex);
1596 remove_maps(vecs, stop_waiter_thread);
1597 free_pathvec(vecs->pathvec, FREE_PATHS);
1599 pthread_cancel(check_thr);
1600 pthread_cancel(uevent_thr);
1601 pthread_cancel(uxlsnr_thr);
1605 free_handlers(handlers);
1610 pthread_mutex_destroy(vecs->lock);
1618 condlog(2, "--------shut down-------");
1627 dbg_free_final(NULL);
1639 if( (pid = fork()) < 0){
1640 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1648 if ( (pid = fork()) < 0)
1649 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1653 in_fd = open("/dev/null", O_RDONLY);
1655 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1659 out_fd = open("/dev/console", O_WRONLY);
1661 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1666 close(STDIN_FILENO);
1668 close(STDOUT_FILENO);
1670 close(STDERR_FILENO);
1681 main (int argc, char *argv[])
1683 extern char *optarg;
1690 if (getuid() != 0) {
1691 fprintf(stderr, "need to be root\n");
1695 /* make sure we don't lock any path */
1697 umask(umask(077) | 022);
1699 conf = alloc_config();
1704 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1708 //debug=1; /* ### comment me out ### */
1711 if (sizeof(optarg) > sizeof(char *) ||
1712 !isdigit(optarg[0]))
1715 conf->verbosity = atoi(optarg);
1738 return (child(NULL));