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 int oldstate = pp->state;
286 condlog(2, "%s: mark as failed", pp->dev_t);
287 mpp->stat_path_failures++;
288 pp->state = PATH_DOWN;
289 if (oldstate == PATH_UP ||
290 oldstate == PATH_GHOST)
291 update_queue_mode_del_path(mpp);
295 * schedule the next check earlier
297 if (pp->tick > conf->checkint)
298 pp->tick = conf->checkint;
305 condlog(0, "failed to update multipath");
310 static sigset_t unblock_signals(void)
315 sigaddset(&set, SIGHUP);
316 sigaddset(&set, SIGUSR1);
317 pthread_sigmask(SIG_UNBLOCK, &set, &old);
322 * returns the reschedule delay
323 * negative means *stop*
326 waiteventloop (struct event_thread * waiter)
332 if (!waiter->event_nr)
333 waiter->event_nr = dm_geteventnr(waiter->mapname);
335 if (!(waiter->dmt = dm_task_create(DM_DEVICE_WAITEVENT))) {
336 condlog(0, "%s: devmap event #%i dm_task_create error",
337 waiter->mapname, waiter->event_nr);
341 if (!dm_task_set_name(waiter->dmt, waiter->mapname)) {
342 condlog(0, "%s: devmap event #%i dm_task_set_name error",
343 waiter->mapname, waiter->event_nr);
344 dm_task_destroy(waiter->dmt);
348 if (waiter->event_nr && !dm_task_set_event_nr(waiter->dmt,
350 condlog(0, "%s: devmap event #%i dm_task_set_event_nr error",
351 waiter->mapname, waiter->event_nr);
352 dm_task_destroy(waiter->dmt);
356 dm_task_no_open_count(waiter->dmt);
358 /* accept wait interruption */
359 set = unblock_signals();
361 /* interruption spits messages */
365 r = dm_task_run(waiter->dmt);
367 /* wait is over : event or interrupt */
368 pthread_sigmask(SIG_SETMASK, &set, NULL);
371 if (!r) /* wait interrupted by signal */
374 dm_task_destroy(waiter->dmt);
382 condlog(3, "%s: devmap event #%i",
383 waiter->mapname, waiter->event_nr);
388 * 1) a table reload, which means our mpp structure is
389 * obsolete : refresh it through update_multipath()
390 * 2) a path failed by DM : mark as such through
392 * 3) map has gone away : stop the thread.
393 * 4) a path reinstate : nothing to do
394 * 5) a switch group : nothing to do
396 pthread_cleanup_push(cleanup_lock, waiter->vecs->lock);
397 lock(waiter->vecs->lock);
398 r = update_multipath(waiter->vecs, waiter->mapname);
399 lock_cleanup_pop(waiter->vecs->lock);
402 return -1; /* stop the thread */
404 event_nr = dm_geteventnr(waiter->mapname);
406 if (waiter->event_nr == event_nr)
407 return 1; /* upon problem reschedule 1s later */
409 waiter->event_nr = event_nr;
411 return -1; /* never reach there */
415 waitevent (void * et)
418 struct event_thread *waiter;
420 mlockall(MCL_CURRENT | MCL_FUTURE);
422 waiter = (struct event_thread *)et;
423 pthread_cleanup_push(free_waiter, et);
426 r = waiteventloop(waiter);
434 pthread_cleanup_pop(1);
439 start_waiter_thread (struct multipath * mpp, struct vectors * vecs)
442 struct event_thread * wp;
447 if (pthread_attr_init(&attr))
450 pthread_attr_setstacksize(&attr, 32 * 1024);
451 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
458 mpp->waiter = (void *)wp;
459 strncpy(wp->mapname, mpp->alias, WWID_SIZE);
463 if (pthread_create(&wp->thread, &attr, waitevent, wp)) {
464 condlog(0, "%s: cannot create event checker", wp->mapname);
467 condlog(2, "%s: event checker started", wp->mapname);
474 condlog(0, "failed to start waiter thread");
479 flush_map(struct multipath * mpp, struct vectors * vecs)
482 * clear references to this map before flushing so we can ignore
483 * the spurious uevent we may generate with the dm_flush_map call below
485 if (dm_flush_map(mpp->alias, DEFAULT_TARGET)) {
487 * May not really be an error -- if the map was already flushed
488 * from the device mapper by dmsetup(8) for instance.
490 condlog(0, "%s: can't flush", mpp->alias);
495 condlog(3, "%s: devmap removed", mpp->alias);
498 orphan_paths(vecs->pathvec, mpp);
499 remove_map(mpp, vecs, stop_waiter_thread, 1);
505 uev_add_map (char * devname, struct vectors * vecs)
507 condlog(2, "%s: add map (uevent)", devname);
508 return ev_add_map(devname, vecs);
512 ev_add_map (char * devname, struct vectors * vecs)
515 char dev_t[BLK_DEV_SIZE];
518 struct multipath * mpp;
522 if (sscanf(devname, "dm-%d", &minor) == 1 &&
523 !sysfs_get_dev(sysfs_path, devname, dev_t, BLK_DEV_SIZE) &&
524 sscanf(dev_t, "%d:%d", &major, &minor) == 2)
525 alias = dm_mapname(major, minor);
527 alias = STRDUP(devname);
532 map_present = dm_map_present(alias);
534 if (map_present && dm_type(alias, DEFAULT_TARGET) <= 0) {
535 condlog(4, "%s: not a multipath map", alias);
540 mpp = find_mp_by_alias(vecs->mpvec, alias);
544 * Not really an error -- we generate our own uevent
545 * if we create a multipath mapped device as a result
548 condlog(0, "%s: devmap already registered",
555 * now we can register the map
557 if (map_present && (mpp = add_map_without_path(vecs, minor, alias,
558 start_waiter_thread))) {
559 condlog(3, "%s: devmap %s added", alias, devname);
562 refwwid = get_refwwid(devname, DEV_DEVMAP, vecs->pathvec);
565 r = coalesce_paths(vecs, NULL, refwwid);
570 condlog(3, "%s: devmap %s added", alias, devname);
572 condlog(0, "%s: uev_add_map %s failed", alias, devname);
580 uev_remove_map (char * devname, struct vectors * vecs)
582 condlog(2, "%s: remove map (uevent)", devname);
583 return ev_remove_map(devname, vecs);
587 ev_remove_map (char * devname, struct vectors * vecs)
589 struct multipath * mpp;
591 mpp = find_mp_by_str(vecs->mpvec, devname);
594 condlog(3, "%s: devmap not registered, can't remove",
598 flush_map(mpp, vecs);
604 uev_umount_map (char * devname, struct vectors * vecs)
606 struct multipath * mpp;
608 condlog(2, "%s: umount map (uevent)", devname);
610 mpp = find_mp_by_str(vecs->mpvec, devname);
615 update_mpp_paths(mpp, vecs->pathvec);
616 verify_paths(mpp, vecs, NULL);
618 if (!VECTOR_SIZE(mpp->paths))
619 flush_map(mpp, vecs);
625 uev_add_path (char * devname, struct vectors * vecs)
627 condlog(2, "%s: add path (uevent)", devname);
628 return (ev_add_path(devname, vecs) != 1)? 0 : 1;
639 ev_add_path (char * devname, struct vectors * vecs)
641 struct multipath * mpp;
643 char empty_buff[WWID_SIZE] = {0};
645 pp = find_path_by_dev(vecs->pathvec, devname);
648 condlog(0, "%s: spurious uevent, path already in pathvec, %p",
651 * allow reconfig of orphaned path here
653 if (pp->mpp) return 1;
657 * get path vital state
659 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
661 condlog(0, "%s: failed to store path info", devname);
664 pp->checkint = conf->checkint;
668 * need path UID to go any further
670 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
671 condlog(0, "%s: failed to get path uid", devname);
672 return 1; /* leave path added to pathvec */
674 if (blacklist_path(conf, pp)){
675 int i = find_slot(vecs->pathvec, (void *)pp);
677 vector_del_slot(vecs->pathvec, i);
681 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
684 if (adopt_paths(vecs->pathvec, mpp))
685 return 1; /* leave path added to pathvec */
687 verify_paths(mpp, vecs, NULL);
688 mpp->action = ACT_RELOAD;
691 if ((mpp = add_map_with_path(vecs, pp, 1)))
692 mpp->action = ACT_CREATE;
694 return 1; /* leave path added to pathvec */
698 * push the map to the device-mapper
700 if (setup_map(mpp)) {
701 condlog(0, "%s: failed to setup map for addition of new "
702 "path %s", mpp->alias, devname);
706 * reload the map for the multipath mapped device
708 if (domap(mpp) <= 0) {
709 condlog(0, "%s: failed in domap for addition of new "
710 "path %s", mpp->alias, devname);
712 * deal with asynchronous uevents :((
714 if (mpp->action == ACT_RELOAD) {
715 condlog(0, "%s: uev_add_path sleep", mpp->alias);
717 update_mpp_paths(mpp, vecs->pathvec);
726 * update our state from kernel regardless of create or reload
728 if (setup_multipath(vecs, mpp))
731 if (mpp->action == ACT_CREATE &&
732 start_waiter_thread(mpp, vecs))
735 condlog(3, "%s path added to devmap %s", devname, mpp->alias);
739 remove_map(mpp, vecs, NULL, 1);
744 uev_remove_path (char * devname, struct vectors * vecs)
746 condlog(2, "%s: remove path (uevent)", devname);
747 return ev_remove_path(devname, vecs);
751 ev_remove_path (char * devname, struct vectors * vecs)
753 struct multipath * mpp;
758 pp = find_path_by_dev(vecs->pathvec, devname);
761 condlog(0, "%s: spurious uevent, path not in pathvec", devname);
766 * avoid referring to the map of an orphanned path
768 if ((mpp = pp->mpp)) {
771 * remove the map IFF removing the last path
773 if (pathcount(mpp, PATH_WILD) > 1) {
774 vector rpvec = vector_alloc();
777 * transform the mp->pg vector of vectors of paths
778 * into a mp->params string to feed the device-mapper
780 update_mpp_paths(mpp, vecs->pathvec);
781 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
782 vector_del_slot(mpp->paths, i);
784 if (VECTOR_SIZE(mpp->paths) == 0) {
785 char alias[WWID_SIZE];
788 * flush_map will fail if the device is open
790 strncpy(alias, mpp->alias, WWID_SIZE);
791 if (flush_map(mpp, vecs))
794 condlog(3, "%s: removed map after removing"
795 " multiple paths", alias);
798 if (setup_map(mpp)) {
799 condlog(0, "%s: failed to setup map for"
800 " removal of path %s", mpp->alias, devname);
801 free_pathvec(rpvec, KEEP_PATHS);
807 mpp->action = ACT_RELOAD;
808 if (domap(mpp) <= 0) {
809 condlog(0, "%s: failed in domap for "
810 "removal of path %s",
811 mpp->alias, devname);
813 * Delete path from pathvec so that
814 * update_mpp_paths wont find it later
815 * when/if another path is removed.
817 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
818 vector_del_slot(vecs->pathvec, i);
823 * update our state from kernel
825 if (setup_multipath(vecs, mpp)) {
826 free_pathvec(rpvec, KEEP_PATHS);
830 condlog(3, "%s path removed from devmap %s",
831 devname, mpp->alias);
833 free_pathvec(rpvec, KEEP_PATHS);
836 char alias[WWID_SIZE];
839 * flush_map will fail if the device is open
841 strncpy(alias, mpp->alias, WWID_SIZE);
842 if (flush_map(mpp, vecs))
845 condlog(3, "%s: removed map", alias);
850 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
851 vector_del_slot(vecs->pathvec, i);
858 remove_map(mpp, vecs, stop_waiter_thread, 1);
863 map_discovery (struct vectors * vecs)
866 struct multipath * mpp;
868 if (dm_get_maps(vecs->mpvec, "multipath"))
871 vector_foreach_slot (vecs->mpvec, mpp, i)
872 if (setup_multipath(vecs, mpp))
879 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
881 struct vectors * vecs;
886 vecs = (struct vectors *)trigger_data;
888 pthread_cleanup_push(cleanup_lock, vecs->lock);
891 r = parse_cmd(str, reply, len, vecs);
894 *reply = STRDUP("fail\n");
895 *len = strlen(*reply) + 1;
898 else if (!r && *len == 0) {
899 *reply = STRDUP("ok\n");
900 *len = strlen(*reply) + 1;
903 /* else if (r < 0) leave *reply alone */
905 lock_cleanup_pop(vecs->lock);
910 uev_discard(char * devpath)
915 * keep only block devices, discard partitions
917 if (sscanf(devpath, "/block/%10s", a) != 1 ||
918 sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
919 condlog(4, "discard event on %s", devpath);
926 uev_trigger (struct uevent * uev, void * trigger_data)
930 struct vectors * vecs;
932 vecs = (struct vectors *)trigger_data;
934 if (uev_discard(uev->devpath))
937 basename(uev->devpath, devname);
941 * device map add/remove event
943 if (!strncmp(devname, "dm-", 3)) {
944 if (!strncmp(uev->action, "add", 3)) {
945 r = uev_add_map(devname, vecs);
948 if (!strncmp(uev->action, "remove", 6)) {
949 r = uev_remove_map(devname, vecs);
952 if (!strncmp(uev->action, "umount", 6)) {
953 r = uev_umount_map(devname, vecs);
960 * path add/remove event
962 if (blacklist(conf->blist_devnode, devname))
965 if (!strncmp(uev->action, "add", 3)) {
966 r = uev_add_path(devname, vecs);
969 if (!strncmp(uev->action, "remove", 6)) {
970 r = uev_remove_path(devname, vecs);
980 ueventloop (void * ap)
982 if (uevent_listen(&uev_trigger, ap))
983 fprintf(stderr, "error starting uevent listener");
989 uxlsnrloop (void * ap)
994 if (alloc_handlers())
997 add_handler(LIST+PATHS, cli_list_paths);
998 add_handler(LIST+MAPS, cli_list_maps);
999 add_handler(LIST+MAPS+STATUS, cli_list_maps_status);
1000 add_handler(LIST+MAPS+STATS, cli_list_maps_stats);
1001 add_handler(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
1002 add_handler(LIST+TOPOLOGY, cli_list_maps_topology);
1003 add_handler(LIST+MAP+TOPOLOGY, cli_list_map_topology);
1004 add_handler(LIST+CONFIG, cli_list_config);
1005 add_handler(ADD+PATH, cli_add_path);
1006 add_handler(DEL+PATH, cli_del_path);
1007 add_handler(ADD+MAP, cli_add_map);
1008 add_handler(DEL+MAP, cli_del_map);
1009 add_handler(SWITCH+MAP+GROUP, cli_switch_group);
1010 add_handler(RECONFIGURE, cli_reconfigure);
1011 add_handler(SUSPEND+MAP, cli_suspend);
1012 add_handler(RESUME+MAP, cli_resume);
1013 add_handler(REINSTATE+PATH, cli_reinstate);
1014 add_handler(FAIL+PATH, cli_fail);
1016 uxsock_listen(&uxsock_trigger, ap);
1022 exit_daemon (int status)
1025 fprintf(stderr, "bad exit status. see daemon.log\n");
1027 condlog(3, "unlink pidfile");
1028 unlink(DEFAULT_PIDFILE);
1031 pthread_cond_signal(&exit_cond);
1032 unlock(&exit_mutex);
1038 fail_path (struct path * pp, int del_active)
1043 condlog(2, "checker failed path %s in map %s",
1044 pp->dev_t, pp->mpp->alias);
1046 dm_fail_path(pp->mpp->alias, pp->dev_t);
1048 update_queue_mode_del_path(pp->mpp);
1052 * caller must have locked the path list before calling that function
1055 reinstate_path (struct path * pp, int add_active)
1060 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
1061 condlog(0, "%s: reinstate failed", pp->dev_t);
1063 condlog(2, "%s: reinstated", pp->dev_t);
1065 update_queue_mode_add_path(pp->mpp);
1070 enable_group(struct path * pp)
1072 struct pathgroup * pgp;
1075 * if path is added through uev_add_path, pgindex can be unset.
1076 * next update_strings() will set it, upon map reload event.
1078 * we can safely return here, because upon map reload, all
1079 * PG will be enabled.
1081 if (!pp->mpp->pg || !pp->pgindex)
1084 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1086 if (pgp->status == PGSTATE_DISABLED) {
1087 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1088 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1093 mpvec_garbage_collector (struct vectors * vecs)
1095 struct multipath * mpp;
1098 vector_foreach_slot (vecs->mpvec, mpp, i) {
1099 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1100 condlog(2, "%s: remove dead map", mpp->alias);
1101 remove_map(mpp, vecs, stop_waiter_thread, 1);
1108 defered_failback_tick (vector mpvec)
1110 struct multipath * mpp;
1113 vector_foreach_slot (mpvec, mpp, i) {
1115 * defered failback getting sooner
1117 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1118 mpp->failback_tick--;
1120 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1121 switch_pathgroup(mpp);
1127 retry_count_tick(vector mpvec)
1129 struct multipath *mpp;
1132 vector_foreach_slot (mpvec, mpp, i) {
1133 if (mpp->retry_tick) {
1134 mpp->stat_total_queueing_time++;
1135 condlog(4, "%s: Retrying.. No active path", mpp->alias);
1136 if(--mpp->retry_tick == 0) {
1137 dm_queue_if_no_path(mpp->alias, 0);
1138 condlog(2, "%s: Disable queueing", mpp->alias);
1145 checkerloop (void *ap)
1147 struct vectors *vecs;
1152 mlockall(MCL_CURRENT | MCL_FUTURE);
1153 vecs = (struct vectors *)ap;
1154 condlog(2, "path checkers start up");
1157 * init the path check interval
1159 vector_foreach_slot (vecs->pathvec, pp, i) {
1160 pp->checkint = conf->checkint;
1164 pthread_cleanup_push(cleanup_lock, vecs->lock);
1168 vector_foreach_slot (vecs->pathvec, pp, i) {
1172 if (pp->tick && --pp->tick)
1173 continue; /* don't check this path yet */
1176 * provision a next check soonest,
1177 * in case we exit abnormaly from here
1179 pp->tick = conf->checkint;
1181 if (!checker_selected(&pp->checker)) {
1182 pathinfo(pp, conf->hwtable, DI_SYSFS);
1186 if (!checker_selected(&pp->checker)) {
1187 condlog(0, "%s: checker is not set", pp->dev);
1190 newstate = checker_check(&pp->checker);
1193 condlog(2, "%s: unusable path", pp->dev);
1194 pathinfo(pp, conf->hwtable, 0);
1198 if (newstate != pp->state) {
1199 int oldstate = pp->state;
1200 pp->state = newstate;
1201 LOG_MSG(1, checker_message(&pp->checker));
1204 * upon state change, reset the checkint
1205 * to the shortest delay
1207 pp->checkint = conf->checkint;
1209 if (newstate == PATH_DOWN ||
1210 newstate == PATH_SHAKY ||
1211 update_multipath_strings(pp->mpp,
1214 * proactively fail path in the DM
1216 if (oldstate == PATH_UP ||
1217 oldstate == PATH_GHOST)
1223 * cancel scheduled failback
1225 pp->mpp->failback_tick = 0;
1227 pp->mpp->stat_path_failures++;
1232 * reinstate this path
1234 if (oldstate != PATH_UP &&
1235 oldstate != PATH_GHOST)
1236 reinstate_path(pp, 1);
1238 reinstate_path(pp, 0);
1241 * schedule [defered] failback
1243 if (pp->mpp->pgfailback > 0)
1244 pp->mpp->failback_tick =
1245 pp->mpp->pgfailback + 1;
1246 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
1247 need_switch_pathgroup(pp->mpp, 1))
1248 switch_pathgroup(pp->mpp);
1251 * if at least one path is up in a group, and
1252 * the group is disabled, re-enable it
1254 if (newstate == PATH_UP)
1257 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1258 LOG_MSG(4, checker_message(&pp->checker));
1260 * double the next check delay.
1261 * max at conf->max_checkint
1263 if (pp->checkint < (conf->max_checkint / 2))
1264 pp->checkint = 2 * pp->checkint;
1266 pp->checkint = conf->max_checkint;
1268 pp->tick = pp->checkint;
1269 condlog(4, "%s: delay next check %is",
1270 pp->dev_t, pp->tick);
1273 pp->state = newstate;
1276 * path prio refreshing
1278 condlog(4, "path prio refresh");
1279 pathinfo(pp, conf->hwtable, DI_PRIO);
1281 if (need_switch_pathgroup(pp->mpp, 0)) {
1282 if (pp->mpp->pgfailback > 0 &&
1283 pp->mpp->failback_tick <= 0)
1284 pp->mpp->failback_tick =
1285 pp->mpp->pgfailback + 1;
1286 else if (pp->mpp->pgfailback ==
1287 -FAILBACK_IMMEDIATE)
1288 switch_pathgroup(pp->mpp);
1291 defered_failback_tick(vecs->mpvec);
1292 retry_count_tick(vecs->mpvec);
1297 condlog(4, "map garbage collection");
1298 mpvec_garbage_collector(vecs);
1302 lock_cleanup_pop(vecs->lock);
1309 configure (struct vectors * vecs, int start_waiters)
1311 struct multipath * mpp;
1316 if (!(vecs->pathvec = vector_alloc()))
1319 if (!(vecs->mpvec = vector_alloc()))
1322 if (!(mpvec = vector_alloc()))
1326 * probe for current path (from sysfs) and map (from dm) sets
1328 path_discovery(vecs->pathvec, conf, DI_ALL);
1330 vector_foreach_slot (vecs->pathvec, pp, i){
1331 if (blacklist_path(conf, pp)){
1332 vector_del_slot(vecs->pathvec, i);
1337 pp->checkint = conf->checkint;
1339 if (map_discovery(vecs))
1343 * create new set of maps & push changed ones into dm
1345 if (coalesce_paths(vecs, mpvec, NULL))
1349 * may need to remove some maps which are no longer relevant
1350 * e.g., due to blacklist changes in conf file
1352 if (coalesce_maps(vecs, mpvec))
1357 if (conf->verbosity > 2)
1358 vector_foreach_slot(mpvec, mpp, i)
1362 * purge dm of old maps
1364 remove_maps(vecs, NULL);
1367 * save new set of maps formed by considering current path state
1369 vecs->mpvec = mpvec;
1372 * start dm event waiter threads for these new maps
1374 vector_foreach_slot(vecs->mpvec, mpp, i) {
1375 if (setup_multipath(vecs, mpp))
1378 if (start_waiter_thread(mpp, vecs))
1385 reconfigure (struct vectors * vecs)
1387 struct config * old = conf;
1390 * free old map and path vectors ... they use old conf state
1392 if (VECTOR_SIZE(vecs->mpvec))
1393 remove_maps(vecs, stop_waiter_thread);
1395 if (VECTOR_SIZE(vecs->pathvec))
1396 free_pathvec(vecs->pathvec, FREE_PATHS);
1400 if (load_config(DEFAULT_CONFIGFILE))
1403 conf->verbosity = old->verbosity;
1405 if (!conf->checkint) {
1406 conf->checkint = DEFAULT_CHECKINT;
1407 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1414 static struct vectors *
1417 struct vectors * vecs;
1419 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1425 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1430 vecs->pathvec = vector_alloc();
1435 vecs->mpvec = vector_alloc();
1440 pthread_mutex_init(vecs->lock, NULL);
1445 vector_free(vecs->pathvec);
1450 condlog(0, "failed to init paths");
1455 signal_set(int signo, void (*func) (int))
1458 struct sigaction sig;
1459 struct sigaction osig;
1461 sig.sa_handler = func;
1462 sigemptyset(&sig.sa_mask);
1465 r = sigaction(signo, &sig, &osig);
1470 return (osig.sa_handler);
1476 condlog(2, "reconfigure (SIGHUP)");
1480 unlock(gvecs->lock);
1483 dbg_free_final(NULL);
1496 condlog(3, "SIGUSR1 received");
1502 signal_set(SIGHUP, sighup);
1503 signal_set(SIGUSR1, sigusr1);
1504 signal_set(SIGINT, sigend);
1505 signal_set(SIGTERM, sigend);
1506 signal_set(SIGKILL, sigend);
1513 static struct sched_param sched_param = {
1517 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1520 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1525 set_oom_adj (int val)
1529 fp = fopen("/proc/self/oom_adj", "w");
1534 fprintf(fp, "%i", val);
1539 child (void * param)
1541 pthread_t check_thr, uevent_thr, uxlsnr_thr;
1542 pthread_attr_t attr;
1543 struct vectors * vecs;
1545 mlockall(MCL_CURRENT | MCL_FUTURE);
1550 condlog(2, "--------start up--------");
1551 condlog(2, "read " DEFAULT_CONFIGFILE);
1553 if (load_config(DEFAULT_CONFIGFILE))
1556 setlogmask(LOG_UPTO(conf->verbosity + 3));
1559 * fill the voids left in the config file
1561 if (!conf->checkint) {
1562 conf->checkint = DEFAULT_CHECKINT;
1563 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1566 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1575 vecs = gvecs = init_vecs();
1580 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
1581 condlog(0, "can not find sysfs mount point");
1586 * fetch and configure both paths and multipaths
1588 if (configure(vecs, 1)) {
1589 condlog(0, "failure during configuration");
1596 pthread_attr_init(&attr);
1597 pthread_attr_setstacksize(&attr, 64 * 1024);
1598 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1600 pthread_create(&check_thr, &attr, checkerloop, vecs);
1601 pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1602 pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1604 pthread_cond_wait(&exit_cond, &exit_mutex);
1610 remove_maps(vecs, stop_waiter_thread);
1611 free_pathvec(vecs->pathvec, FREE_PATHS);
1613 pthread_cancel(check_thr);
1614 pthread_cancel(uevent_thr);
1615 pthread_cancel(uxlsnr_thr);
1619 free_handlers(handlers);
1624 pthread_mutex_destroy(vecs->lock);
1632 condlog(2, "--------shut down-------");
1641 dbg_free_final(NULL);
1653 if( (pid = fork()) < 0){
1654 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1662 if ( (pid = fork()) < 0)
1663 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1667 in_fd = open("/dev/null", O_RDONLY);
1669 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1673 out_fd = open("/dev/console", O_WRONLY);
1675 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1680 close(STDIN_FILENO);
1682 close(STDOUT_FILENO);
1684 close(STDERR_FILENO);
1695 main (int argc, char *argv[])
1697 extern char *optarg;
1704 if (getuid() != 0) {
1705 fprintf(stderr, "need to be root\n");
1709 /* make sure we don't lock any path */
1711 umask(umask(077) | 022);
1713 conf = alloc_config();
1718 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1722 //debug=1; /* ### comment me out ### */
1725 if (sizeof(optarg) > sizeof(char *) ||
1726 !isdigit(optarg[0]))
1729 conf->verbosity = atoi(optarg);
1752 return (child(NULL));