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);
629 ev_add_path (char * devname, struct vectors * vecs)
631 struct multipath * mpp;
633 char empty_buff[WWID_SIZE] = {0};
635 pp = find_path_by_dev(vecs->pathvec, devname);
638 condlog(0, "%s: spurious uevent, path already in pathvec, %p",
641 * allow reconfig of orphaned path here
643 if (pp->mpp) return 1;
647 * get path vital state
649 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
651 condlog(0, "%s: failed to store path info", devname);
654 pp->checkint = conf->checkint;
658 * need path UID to go any further
660 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
661 condlog(0, "%s: failed to get path uid", devname);
662 return 1; /* leave path added to pathvec */
665 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
668 if (adopt_paths(vecs->pathvec, mpp))
669 return 1; /* leave path added to pathvec */
671 verify_paths(mpp, vecs, NULL);
672 mpp->action = ACT_RELOAD;
675 if ((mpp = add_map_with_path(vecs, pp, 1)))
676 mpp->action = ACT_CREATE;
678 return 1; /* leave path added to pathvec */
682 * push the map to the device-mapper
684 if (setup_map(mpp)) {
685 condlog(0, "%s: failed to setup map for addition of new "
686 "path %s", mpp->alias, devname);
690 * reload the map for the multipath mapped device
692 if (domap(mpp) <= 0) {
693 condlog(0, "%s: failed in domap for addition of new "
694 "path %s", mpp->alias, devname);
696 * deal with asynchronous uevents :((
698 if (mpp->action == ACT_RELOAD) {
699 condlog(0, "%s: uev_add_path sleep", mpp->alias);
701 update_mpp_paths(mpp, vecs->pathvec);
710 * update our state from kernel regardless of create or reload
712 if (setup_multipath(vecs, mpp))
715 if (mpp->action == ACT_CREATE &&
716 start_waiter_thread(mpp, vecs))
719 condlog(3, "%s path added to devmap %s", devname, mpp->alias);
723 remove_map(mpp, vecs, NULL, 1);
728 uev_remove_path (char * devname, struct vectors * vecs)
730 condlog(2, "%s: remove path (uevent)", devname);
731 return ev_remove_path(devname, vecs);
735 ev_remove_path (char * devname, struct vectors * vecs)
737 struct multipath * mpp;
742 pp = find_path_by_dev(vecs->pathvec, devname);
745 condlog(0, "%s: spurious uevent, path not in pathvec", devname);
750 * avoid referring to the map of an orphanned path
752 if ((mpp = pp->mpp)) {
755 * remove the map IFF removing the last path
757 if (pathcount(mpp, PATH_WILD) > 1) {
758 vector rpvec = vector_alloc();
761 * transform the mp->pg vector of vectors of paths
762 * into a mp->params string to feed the device-mapper
764 update_mpp_paths(mpp, vecs->pathvec);
765 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
766 vector_del_slot(mpp->paths, i);
768 if (VECTOR_SIZE(mpp->paths) == 0) {
769 char alias[WWID_SIZE];
772 * flush_map will fail if the device is open
774 strncpy(alias, mpp->alias, WWID_SIZE);
775 if (flush_map(mpp, vecs))
778 condlog(3, "%s: removed map after removing"
779 " multiple paths", alias);
782 if (setup_map(mpp)) {
783 condlog(0, "%s: failed to setup map for"
784 " removal of path %s", mpp->alias, devname);
785 free_pathvec(rpvec, KEEP_PATHS);
791 mpp->action = ACT_RELOAD;
792 if (domap(mpp) <= 0) {
793 condlog(0, "%s: failed in domap for "
794 "removal of path %s",
795 mpp->alias, devname);
797 * Delete path from pathvec so that
798 * update_mpp_paths wont find it later
799 * when/if another path is removed.
801 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
802 vector_del_slot(vecs->pathvec, i);
807 * update our state from kernel
809 if (setup_multipath(vecs, mpp)) {
810 free_pathvec(rpvec, KEEP_PATHS);
814 condlog(3, "%s path removed from devmap %s",
815 devname, mpp->alias);
817 free_pathvec(rpvec, KEEP_PATHS);
820 char alias[WWID_SIZE];
823 * flush_map will fail if the device is open
825 strncpy(alias, mpp->alias, WWID_SIZE);
826 if (flush_map(mpp, vecs))
829 condlog(3, "%s: removed map", alias);
834 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
835 vector_del_slot(vecs->pathvec, i);
842 remove_map(mpp, vecs, stop_waiter_thread, 1);
847 map_discovery (struct vectors * vecs)
850 struct multipath * mpp;
852 if (dm_get_maps(vecs->mpvec, "multipath"))
855 vector_foreach_slot (vecs->mpvec, mpp, i)
856 if (setup_multipath(vecs, mpp))
863 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
865 struct vectors * vecs;
870 vecs = (struct vectors *)trigger_data;
872 pthread_cleanup_push(cleanup_lock, vecs->lock);
875 r = parse_cmd(str, reply, len, vecs);
878 *reply = STRDUP("fail\n");
879 *len = strlen(*reply) + 1;
882 else if (!r && *len == 0) {
883 *reply = STRDUP("ok\n");
884 *len = strlen(*reply) + 1;
887 /* else if (r < 0) leave *reply alone */
889 lock_cleanup_pop(vecs->lock);
894 uev_discard(char * devpath)
899 * keep only block devices, discard partitions
901 if (sscanf(devpath, "/block/%10s", a) != 1 ||
902 sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
903 condlog(4, "discard event on %s", devpath);
910 uev_trigger (struct uevent * uev, void * trigger_data)
914 struct vectors * vecs;
916 vecs = (struct vectors *)trigger_data;
918 if (uev_discard(uev->devpath))
921 basename(uev->devpath, devname);
925 * device map add/remove event
927 if (!strncmp(devname, "dm-", 3)) {
928 if (!strncmp(uev->action, "add", 3)) {
929 r = uev_add_map(devname, vecs);
932 if (!strncmp(uev->action, "remove", 6)) {
933 r = uev_remove_map(devname, vecs);
936 if (!strncmp(uev->action, "umount", 6)) {
937 r = uev_umount_map(devname, vecs);
944 * path add/remove event
946 if (blacklist(conf->blist_devnode, devname))
949 if (!strncmp(uev->action, "add", 3)) {
950 r = uev_add_path(devname, vecs);
953 if (!strncmp(uev->action, "remove", 6)) {
954 r = uev_remove_path(devname, vecs);
964 ueventloop (void * ap)
966 if (uevent_listen(&uev_trigger, ap))
967 fprintf(stderr, "error starting uevent listener");
973 uxlsnrloop (void * ap)
978 if (alloc_handlers())
981 add_handler(LIST+PATHS, cli_list_paths);
982 add_handler(LIST+MAPS, cli_list_maps);
983 add_handler(LIST+MAPS+STATUS, cli_list_maps_status);
984 add_handler(LIST+MAPS+STATS, cli_list_maps_stats);
985 add_handler(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
986 add_handler(LIST+TOPOLOGY, cli_list_maps_topology);
987 add_handler(LIST+MAP+TOPOLOGY, cli_list_map_topology);
988 add_handler(LIST+CONFIG, cli_list_config);
989 add_handler(ADD+PATH, cli_add_path);
990 add_handler(DEL+PATH, cli_del_path);
991 add_handler(ADD+MAP, cli_add_map);
992 add_handler(DEL+MAP, cli_del_map);
993 add_handler(SWITCH+MAP+GROUP, cli_switch_group);
994 add_handler(RECONFIGURE, cli_reconfigure);
995 add_handler(SUSPEND+MAP, cli_suspend);
996 add_handler(RESUME+MAP, cli_resume);
997 add_handler(REINSTATE+PATH, cli_reinstate);
998 add_handler(FAIL+PATH, cli_fail);
1000 uxsock_listen(&uxsock_trigger, ap);
1006 exit_daemon (int status)
1009 fprintf(stderr, "bad exit status. see daemon.log\n");
1011 condlog(3, "unlink pidfile");
1012 unlink(DEFAULT_PIDFILE);
1015 pthread_cond_signal(&exit_cond);
1016 unlock(&exit_mutex);
1022 fail_path (struct path * pp)
1027 condlog(2, "checker failed path %s in map %s",
1028 pp->dev_t, pp->mpp->alias);
1030 dm_fail_path(pp->mpp->alias, pp->dev_t);
1031 update_queue_mode_del_path(pp->mpp);
1035 * caller must have locked the path list before calling that function
1038 reinstate_path (struct path * pp)
1043 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
1044 condlog(0, "%s: reinstate failed", pp->dev_t);
1046 condlog(2, "%s: reinstated", pp->dev_t);
1047 update_queue_mode_add_path(pp->mpp);
1052 enable_group(struct path * pp)
1054 struct pathgroup * pgp;
1057 * if path is added through uev_add_path, pgindex can be unset.
1058 * next update_strings() will set it, upon map reload event.
1060 * we can safely return here, because upon map reload, all
1061 * PG will be enabled.
1063 if (!pp->mpp->pg || !pp->pgindex)
1066 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1068 if (pgp->status == PGSTATE_DISABLED) {
1069 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1070 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1075 mpvec_garbage_collector (struct vectors * vecs)
1077 struct multipath * mpp;
1080 vector_foreach_slot (vecs->mpvec, mpp, i) {
1081 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1082 condlog(2, "%s: remove dead map", mpp->alias);
1083 remove_map(mpp, vecs, stop_waiter_thread, 1);
1090 defered_failback_tick (vector mpvec)
1092 struct multipath * mpp;
1095 vector_foreach_slot (mpvec, mpp, i) {
1097 * defered failback getting sooner
1099 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1100 mpp->failback_tick--;
1102 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1103 switch_pathgroup(mpp);
1109 retry_count_tick(vector mpvec)
1111 struct multipath *mpp;
1114 vector_foreach_slot (mpvec, mpp, i) {
1115 if (mpp->retry_tick) {
1116 mpp->stat_total_queueing_time++;
1117 condlog(4, "%s: Retrying.. No active path", mpp->alias);
1118 if(--mpp->retry_tick == 0) {
1119 dm_queue_if_no_path(mpp->alias, 0);
1120 condlog(2, "%s: Disable queueing", mpp->alias);
1127 checkerloop (void *ap)
1129 struct vectors *vecs;
1134 mlockall(MCL_CURRENT | MCL_FUTURE);
1135 vecs = (struct vectors *)ap;
1136 condlog(2, "path checkers start up");
1139 * init the path check interval
1141 vector_foreach_slot (vecs->pathvec, pp, i) {
1142 pp->checkint = conf->checkint;
1146 pthread_cleanup_push(cleanup_lock, vecs->lock);
1150 vector_foreach_slot (vecs->pathvec, pp, i) {
1154 if (pp->tick && --pp->tick)
1155 continue; /* don't check this path yet */
1158 * provision a next check soonest,
1159 * in case we exit abnormaly from here
1161 pp->tick = conf->checkint;
1163 if (!checker_selected(&pp->checker)) {
1164 pathinfo(pp, conf->hwtable, DI_SYSFS);
1168 if (!checker_selected(&pp->checker)) {
1169 condlog(0, "%s: checker is not set", pp->dev);
1172 newstate = checker_check(&pp->checker);
1175 condlog(2, "%s: unusable path", pp->dev);
1176 pathinfo(pp, conf->hwtable, 0);
1180 if (newstate != pp->state) {
1181 pp->state = newstate;
1182 LOG_MSG(1, checker_message(&pp->checker));
1185 * upon state change, reset the checkint
1186 * to the shortest delay
1188 pp->checkint = conf->checkint;
1190 if (newstate == PATH_DOWN ||
1191 newstate == PATH_SHAKY ||
1192 update_multipath_strings(pp->mpp,
1195 * proactively fail path in the DM
1200 * cancel scheduled failback
1202 pp->mpp->failback_tick = 0;
1204 pp->mpp->stat_path_failures++;
1209 * reinstate this path
1214 * schedule [defered] failback
1216 if (pp->mpp->pgfailback > 0)
1217 pp->mpp->failback_tick =
1218 pp->mpp->pgfailback + 1;
1219 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
1220 need_switch_pathgroup(pp->mpp, 1))
1221 switch_pathgroup(pp->mpp);
1224 * if at least one path is up in a group, and
1225 * the group is disabled, re-enable it
1227 if (newstate == PATH_UP)
1230 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1231 LOG_MSG(4, checker_message(&pp->checker));
1233 * double the next check delay.
1234 * max at conf->max_checkint
1236 if (pp->checkint < (conf->max_checkint / 2))
1237 pp->checkint = 2 * pp->checkint;
1239 pp->checkint = conf->max_checkint;
1241 pp->tick = pp->checkint;
1242 condlog(4, "%s: delay next check %is",
1243 pp->dev_t, pp->tick);
1246 pp->state = newstate;
1249 * path prio refreshing
1251 condlog(4, "path prio refresh");
1252 pathinfo(pp, conf->hwtable, DI_PRIO);
1254 if (need_switch_pathgroup(pp->mpp, 0)) {
1255 if (pp->mpp->pgfailback > 0)
1256 pp->mpp->failback_tick =
1257 pp->mpp->pgfailback + 1;
1258 else if (pp->mpp->pgfailback ==
1259 -FAILBACK_IMMEDIATE)
1260 switch_pathgroup(pp->mpp);
1263 defered_failback_tick(vecs->mpvec);
1264 retry_count_tick(vecs->mpvec);
1269 condlog(4, "map garbage collection");
1270 mpvec_garbage_collector(vecs);
1274 lock_cleanup_pop(vecs->lock);
1281 configure (struct vectors * vecs, int start_waiters)
1283 struct multipath * mpp;
1288 if (!(vecs->pathvec = vector_alloc()))
1291 if (!(vecs->mpvec = vector_alloc()))
1294 if (!(mpvec = vector_alloc()))
1298 * probe for current path (from sysfs) and map (from dm) sets
1300 path_discovery(vecs->pathvec, conf, DI_ALL);
1302 vector_foreach_slot (vecs->pathvec, pp, i)
1303 pp->checkint = conf->checkint;
1305 if (map_discovery(vecs))
1309 * create new set of maps & push changed ones into dm
1311 if (coalesce_paths(vecs, mpvec, NULL))
1315 * may need to remove some maps which are no longer relevant
1316 * e.g., due to blacklist changes in conf file
1318 if (coalesce_maps(vecs, mpvec))
1323 if (conf->verbosity > 2)
1324 vector_foreach_slot(mpvec, mpp, i)
1328 * purge dm of old maps
1330 remove_maps(vecs, NULL);
1333 * save new set of maps formed by considering current path state
1335 vecs->mpvec = mpvec;
1338 * start dm event waiter threads for these new maps
1340 vector_foreach_slot(vecs->mpvec, mpp, i) {
1341 if (setup_multipath(vecs, mpp))
1344 if (start_waiter_thread(mpp, vecs))
1351 reconfigure (struct vectors * vecs)
1353 struct config * old = conf;
1356 * free old map and path vectors ... they use old conf state
1358 if (VECTOR_SIZE(vecs->mpvec))
1359 remove_maps(vecs, stop_waiter_thread);
1361 if (VECTOR_SIZE(vecs->pathvec))
1362 free_pathvec(vecs->pathvec, FREE_PATHS);
1366 if (load_config(DEFAULT_CONFIGFILE))
1369 conf->verbosity = old->verbosity;
1371 if (!conf->checkint) {
1372 conf->checkint = DEFAULT_CHECKINT;
1373 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1380 static struct vectors *
1383 struct vectors * vecs;
1385 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1391 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1396 vecs->pathvec = vector_alloc();
1401 vecs->mpvec = vector_alloc();
1406 pthread_mutex_init(vecs->lock, NULL);
1411 vector_free(vecs->pathvec);
1416 condlog(0, "failed to init paths");
1421 signal_set(int signo, void (*func) (int))
1424 struct sigaction sig;
1425 struct sigaction osig;
1427 sig.sa_handler = func;
1428 sigemptyset(&sig.sa_mask);
1431 r = sigaction(signo, &sig, &osig);
1436 return (osig.sa_handler);
1442 condlog(2, "reconfigure (SIGHUP)");
1446 unlock(gvecs->lock);
1449 dbg_free_final(NULL);
1462 condlog(3, "SIGUSR1 received");
1468 signal_set(SIGHUP, sighup);
1469 signal_set(SIGUSR1, sigusr1);
1470 signal_set(SIGINT, sigend);
1471 signal_set(SIGTERM, sigend);
1472 signal_set(SIGKILL, sigend);
1479 static struct sched_param sched_param = {
1483 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1486 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1491 set_oom_adj (int val)
1495 fp = fopen("/proc/self/oom_adj", "w");
1500 fprintf(fp, "%i", val);
1505 child (void * param)
1507 pthread_t check_thr, uevent_thr, uxlsnr_thr;
1508 pthread_attr_t attr;
1509 struct vectors * vecs;
1511 mlockall(MCL_CURRENT | MCL_FUTURE);
1516 condlog(2, "--------start up--------");
1517 condlog(2, "read " DEFAULT_CONFIGFILE);
1519 if (load_config(DEFAULT_CONFIGFILE))
1522 setlogmask(LOG_UPTO(conf->verbosity + 3));
1525 * fill the voids left in the config file
1527 if (!conf->checkint) {
1528 conf->checkint = DEFAULT_CHECKINT;
1529 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1532 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1541 vecs = gvecs = init_vecs();
1546 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
1547 condlog(0, "can not find sysfs mount point");
1552 * fetch and configure both paths and multipaths
1554 if (configure(vecs, 1)) {
1555 condlog(0, "failure during configuration");
1562 pthread_attr_init(&attr);
1563 pthread_attr_setstacksize(&attr, 64 * 1024);
1564 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1566 pthread_create(&check_thr, &attr, checkerloop, vecs);
1567 pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1568 pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1570 pthread_cond_wait(&exit_cond, &exit_mutex);
1576 remove_maps(vecs, stop_waiter_thread);
1577 free_pathvec(vecs->pathvec, FREE_PATHS);
1579 pthread_cancel(check_thr);
1580 pthread_cancel(uevent_thr);
1581 pthread_cancel(uxlsnr_thr);
1585 free_handlers(handlers);
1590 pthread_mutex_destroy(vecs->lock);
1598 condlog(2, "--------shut down-------");
1607 dbg_free_final(NULL);
1619 if( (pid = fork()) < 0){
1620 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1628 if ( (pid = fork()) < 0)
1629 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1633 in_fd = open("/dev/null", O_RDONLY);
1635 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1639 out_fd = open("/dev/console", O_WRONLY);
1641 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1646 close(STDIN_FILENO);
1648 close(STDOUT_FILENO);
1650 close(STDERR_FILENO);
1661 main (int argc, char *argv[])
1663 extern char *optarg;
1670 if (getuid() != 0) {
1671 fprintf(stderr, "need to be root\n");
1675 /* make sure we don't lock any path */
1677 umask(umask(077) | 022);
1679 conf = alloc_config();
1684 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1688 //debug=1; /* ### comment me out ### */
1691 if (sizeof(optarg) > sizeof(char *) ||
1692 !isdigit(optarg[0]))
1695 conf->verbosity = atoi(optarg);
1718 return (child(NULL));