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 */
94 static struct event_thread *
98 struct event_thread * wp;
100 wp = (struct event_thread *)MALLOC(sizeof(struct event_thread));
106 free_waiter (void * data)
108 struct event_thread * wp = (struct event_thread *)data;
111 * indicate in mpp that the wp is already freed storage
113 lock(wp->vecs->lock);
117 * be careful, mpp may already be freed -- null if so
119 wp->mpp->waiter = NULL;
121 condlog(3, "free_waiter, mpp freed before wp=%p,", wp);
123 unlock(wp->vecs->lock);
126 dm_task_destroy(wp->dmt);
132 stop_waiter_thread (struct multipath * mpp, struct vectors * vecs)
134 struct event_thread * wp = (struct event_thread *)mpp->waiter;
137 condlog(3, "%s: no waiter thread", mpp->alias);
140 condlog(2, "%s: stop event checker thread", wp->mapname);
141 pthread_kill((pthread_t)wp->thread, SIGUSR1);
145 cleanup_lock (void * data)
147 unlock((pthread_mutex_t *)data);
151 * mpp->no_path_retry:
152 * -2 (QUEUE) : queue_if_no_path enabled, never turned off
153 * -1 (FAIL) : fail_if_no_path
154 * 0 (UNDEF) : nothing
155 * >0 : queue_if_no_path enabled, turned off after polling n times
158 update_queue_mode_del_path(struct multipath *mpp)
160 if (--mpp->nr_active == 0 && mpp->no_path_retry > 0) {
163 * meaning of +1: retry_tick may be decremented in
164 * checkerloop before starting retry.
166 mpp->retry_tick = mpp->no_path_retry * conf->checkint + 1;
167 condlog(1, "%s: Entering recovery mode: max_retries=%d",
168 mpp->alias, mpp->no_path_retry);
170 condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
174 update_queue_mode_add_path(struct multipath *mpp)
176 if (mpp->nr_active++ == 0 && mpp->no_path_retry > 0) {
177 /* come back to normal mode from retry mode */
179 dm_queue_if_no_path(mpp->alias, 1);
180 condlog(2, "%s: queue_if_no_path enabled", mpp->alias);
181 condlog(1, "%s: Recovered to normal mode", mpp->alias);
183 condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
187 need_switch_pathgroup (struct multipath * mpp, int refresh)
189 struct pathgroup * pgp;
193 if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
197 * Refresh path priority values
200 vector_foreach_slot (mpp->pg, pgp, i)
201 vector_foreach_slot (pgp->paths, pp, j)
202 pathinfo(pp, conf->hwtable, DI_PRIO);
204 mpp->bestpg = select_path_group(mpp);
206 if (mpp->bestpg != mpp->nextpg)
213 switch_pathgroup (struct multipath * mpp)
215 dm_switchgroup(mpp->alias, mpp->bestpg);
216 condlog(2, "%s: switch to path group #%i",
217 mpp->alias, mpp->bestpg);
221 coalesce_maps(struct vectors *vecs, vector nmpv)
223 struct multipath * ompp;
224 vector ompv = vecs->mpvec;
227 condlog(3, "coalesce_maps vs = %u", VECTOR_SIZE(ompv));
228 condlog(3, "coalesce_maps vs = %u", VECTOR_SIZE(nmpv));
230 vector_foreach_slot (ompv, ompp, i) {
231 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
233 * remove all current maps not allowed by the
234 * current configuration
236 if (dm_flush_map(ompp->alias, DEFAULT_TARGET)) {
237 condlog(0, "%s: unable to flush devmap",
240 * may be just because the device is open
242 if (!vector_alloc_slot(nmpv))
245 vector_set_slot(nmpv, ompp);
246 setup_multipath(vecs, ompp);
248 if ((j = find_slot(ompv, (void *)ompp)) != -1)
249 vector_del_slot(ompv, j);
255 condlog(3, "%s devmap removed", ompp->alias);
263 update_multipath (struct vectors *vecs, char *mapname)
265 struct multipath *mpp;
266 struct pathgroup *pgp;
271 mpp = find_mp_by_alias(vecs->mpvec, mapname);
276 free_pgvec(mpp->pg, KEEP_PATHS);
279 if (setup_multipath(vecs, mpp))
280 goto out; /* mpp freed in setup_multipath */
283 * compare checkers states with DM states
285 vector_foreach_slot (mpp->pg, pgp, i) {
286 vector_foreach_slot (pgp->paths, pp, j) {
287 if (pp->dmstate != PSTATE_FAILED)
290 if (pp->state != PATH_DOWN) {
291 condlog(2, "%s: mark as failed", pp->dev_t);
292 pp->state = PATH_DOWN;
293 update_queue_mode_del_path(mpp);
297 * schedule the next check earlier
299 if (pp->tick > conf->checkint)
300 pp->tick = conf->checkint;
307 condlog(0, "failed to update multipath");
312 static sigset_t unblock_signals(void)
317 sigaddset(&set, SIGHUP);
318 sigaddset(&set, SIGUSR1);
319 pthread_sigmask(SIG_UNBLOCK, &set, &old);
324 * returns the reschedule delay
325 * negative means *stop*
328 waiteventloop (struct event_thread * waiter)
334 if (!waiter->event_nr)
335 waiter->event_nr = dm_geteventnr(waiter->mapname);
337 if (!(waiter->dmt = dm_task_create(DM_DEVICE_WAITEVENT))) {
338 condlog(0, "%s: devmap event #%i dm_task_create error",
339 waiter->mapname, waiter->event_nr);
343 if (!dm_task_set_name(waiter->dmt, waiter->mapname)) {
344 condlog(0, "%s: devmap event #%i dm_task_set_name error",
345 waiter->mapname, waiter->event_nr);
346 dm_task_destroy(waiter->dmt);
350 if (waiter->event_nr && !dm_task_set_event_nr(waiter->dmt,
352 condlog(0, "%s: devmap event #%i dm_task_set_event_nr error",
353 waiter->mapname, waiter->event_nr);
354 dm_task_destroy(waiter->dmt);
358 dm_task_no_open_count(waiter->dmt);
360 /* accept wait interruption */
361 set = unblock_signals();
363 /* interruption spits messages */
367 r = dm_task_run(waiter->dmt);
369 /* wait is over : event or interrupt */
370 pthread_sigmask(SIG_SETMASK, &set, NULL);
373 if (!r) /* wait interrupted by signal */
376 dm_task_destroy(waiter->dmt);
384 condlog(3, "%s: devmap event #%i",
385 waiter->mapname, waiter->event_nr);
390 * 1) a table reload, which means our mpp structure is
391 * obsolete : refresh it through update_multipath()
392 * 2) a path failed by DM : mark as such through
394 * 3) map has gone away : stop the thread.
395 * 4) a path reinstate : nothing to do
396 * 5) a switch group : nothing to do
398 pthread_cleanup_push(cleanup_lock, waiter->vecs->lock);
399 lock(waiter->vecs->lock);
400 r = update_multipath(waiter->vecs, waiter->mapname);
401 lock_cleanup_pop(waiter->vecs->lock);
404 return -1; /* stop the thread */
406 event_nr = dm_geteventnr(waiter->mapname);
408 if (waiter->event_nr == event_nr)
409 return 1; /* upon problem reschedule 1s later */
411 waiter->event_nr = event_nr;
413 return -1; /* never reach there */
417 waitevent (void * et)
420 struct event_thread *waiter;
422 mlockall(MCL_CURRENT | MCL_FUTURE);
424 waiter = (struct event_thread *)et;
425 pthread_cleanup_push(free_waiter, et);
428 r = waiteventloop(waiter);
436 pthread_cleanup_pop(1);
441 start_waiter_thread (struct multipath * mpp, struct vectors * vecs)
444 struct event_thread * wp;
449 if (pthread_attr_init(&attr))
452 pthread_attr_setstacksize(&attr, 32 * 1024);
453 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
460 mpp->waiter = (void *)wp;
461 strncpy(wp->mapname, mpp->alias, WWID_SIZE);
465 if (pthread_create(&wp->thread, &attr, waitevent, wp)) {
466 condlog(0, "%s: cannot create event checker", wp->mapname);
469 condlog(2, "%s: event checker started", wp->mapname);
476 condlog(0, "failed to start waiter thread");
481 flush_map(struct multipath * mpp, struct vectors * vecs)
484 * clear references to this map before flushing so we can ignore
485 * the spurious uevent we may generate with the dm_flush_map call below
487 if (dm_flush_map(mpp->alias, DEFAULT_TARGET)) {
489 * May not really be an error -- if the map was already flushed
490 * from the device mapper by dmsetup(8) for instance.
492 condlog(0, "%s: can't flush", mpp->alias);
497 condlog(3, "%s: devmap removed", mpp->alias);
500 orphan_paths(vecs->pathvec, mpp);
501 remove_map(mpp, vecs, stop_waiter_thread, 1);
507 uev_add_map (char * devname, struct vectors * vecs)
510 char dev_t[BLK_DEV_SIZE];
513 struct multipath * mpp;
517 condlog(3, "%s: uev_add_map", devname);
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: spurious uevent, 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", devname, alias);
569 condlog(0, "%s: uev_add_map failed", alias);
577 uev_remove_map (char * devname, struct vectors * vecs)
580 struct multipath * mpp;
582 condlog(3, "%s: uev_remove_map", devname);
584 if (sscanf(devname, "dm-%d", &minor) == 1)
585 mpp = find_mp_by_minor(vecs->mpvec, minor);
587 mpp = find_mp_by_alias(vecs->mpvec, devname);
590 condlog(3, "%s: devmap not registered, can't remove",
595 condlog(2, "%s: remove devmap", mpp->alias);
596 flush_map(mpp, vecs);
602 uev_umount_map (char * devname, struct vectors * vecs)
605 struct multipath * mpp;
607 condlog(3, "%s: uev_umount_map", devname);
609 if (sscanf(devname, "dm-%d", &minor) == 1)
610 mpp = find_mp_by_minor(vecs->mpvec, minor);
612 mpp = find_mp_by_alias(vecs->mpvec, devname);
617 update_mpp_paths(mpp);
618 verify_paths(mpp, vecs, NULL);
620 if (!VECTOR_SIZE(mpp->paths))
621 flush_map(mpp, vecs);
627 uev_add_path (char * devname, struct vectors * vecs)
629 struct multipath * mpp;
631 char empty_buff[WWID_SIZE] = {0};
633 condlog(3, "%s: uev_add_path", devname);
635 pp = find_path_by_dev(vecs->pathvec, devname);
637 condlog(0, "%s: spurious uevent, path already in pathvec, %p",
640 * allow reconfig of orphaned path here
642 if (pp->mpp) return 1;
646 * get path vital state
648 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
650 condlog(0, "%s: failed to store path info", devname);
653 pp->checkint = conf->checkint;
657 * need path UID to go any further
659 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
660 condlog(0, "%s: failed to get path uid", devname);
661 return 1; /* leave path added to pathvec */
664 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
667 if (adopt_paths(vecs->pathvec, mpp))
668 return 1; /* leave path added to pathvec */
670 verify_paths(mpp, vecs, NULL);
671 condlog(0, "%s: ownership set to %s", pp->dev_t, mpp->alias);
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);
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 struct multipath * mpp;
735 condlog(3, "%s: uev_remove_path", devname);
737 pp = find_path_by_dev(vecs->pathvec, devname);
739 condlog(0, "%s: spurious uevent, path not in pathvec", devname);
744 * avoid referring to the map of an orphanned path
746 if ((mpp = pp->mpp)) {
749 * remove the map IFF removing the last path
751 if (pathcount(mpp, PATH_WILD) > 1) {
752 vector rpvec = vector_alloc();
757 * transform the mp->pg vector of vectors of paths
758 * into a mp->params string to feed the device-mapper
760 update_mpp_paths(mpp);
761 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
762 vector_del_slot(mpp->paths, i);
765 redo = verify_paths(mpp, vecs, rpvec);
766 if (VECTOR_SIZE(mpp->paths) == 0) {
767 char alias[WWID_SIZE];
770 * flush_map will fail if the device is open
772 strncpy(alias, mpp->alias, WWID_SIZE);
773 if (flush_map(mpp, vecs)) {
775 vector_foreach_slot(rpvec, mypp, i)
776 if (store_path(mpp->paths, mypp))
780 vector_foreach_slot(rpvec, mypp, i) {
781 if ((j = find_slot(vecs->pathvec,
782 (void *)mypp)) != -1) {
783 vector_del_slot(vecs->pathvec, j);
787 condlog(3, "%s: removed map after removing"
788 " multiple paths", alias);
792 vector_foreach_slot(rpvec, mypp, i) {
793 if ((j = find_slot(vecs->pathvec,
794 (void *)mypp)) != -1) {
795 vector_del_slot(vecs->pathvec, j);
800 if (setup_map(mpp)) {
801 condlog(0, "%s: failed to setup map for"
802 " removal of path %s", mpp->alias, devname);
803 free_pathvec(rpvec, KEEP_PATHS);
809 mpp->action = ACT_RELOAD;
810 if (domap(mpp) <= 0) {
811 condlog(0, "%s: failed in domap for "
812 "removal of path %s",
813 mpp->alias, devname);
815 * deal with asynchronous uevents :((
818 condlog(3, "%s: uev_remove_path sleep",
822 update_mpp_paths(mpp);
823 free_pathvec(rpvec, KEEP_PATHS);
824 rpvec = vector_alloc();
828 * update our state from kernel
830 if (setup_multipath(vecs, mpp)) {
831 free_pathvec(rpvec, KEEP_PATHS);
835 condlog(3, "%s path removed from devmap %s",
836 devname, mpp->alias);
838 free_pathvec(rpvec, KEEP_PATHS);
841 char alias[WWID_SIZE];
844 * flush_map will fail if the device is open
846 strncpy(alias, mpp->alias, WWID_SIZE);
847 if (flush_map(mpp, vecs)) {
851 condlog(3, "%s: removed map", alias);
856 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
857 vector_del_slot(vecs->pathvec, i);
864 remove_map(mpp, vecs, stop_waiter_thread, 1);
869 show_paths (char ** r, int * len, struct vectors * vecs)
875 int maxlen = INITIAL_REPLY_LEN;
878 get_path_layout(vecs->pathvec);
879 reply = MALLOC(maxlen);
887 if (VECTOR_SIZE(vecs->pathvec) > 0)
888 c += snprint_path_header(c, reply + maxlen - c,
891 vector_foreach_slot(vecs->pathvec, pp, i)
892 c += snprint_path(c, reply + maxlen - c,
893 PRINT_PATH_CHECKER, pp);
895 again = ((c - reply) == (maxlen - 1));
898 reply = REALLOC(reply, maxlen *= 2);
902 *len = (int)(c - reply + 1);
907 show_maps (char ** r, int *len, struct vectors * vecs)
910 struct multipath * mpp;
913 int maxlen = INITIAL_REPLY_LEN;
916 get_map_layout(vecs->mpvec);
917 reply = MALLOC(maxlen);
924 if (VECTOR_SIZE(vecs->mpvec) > 0)
925 c += snprint_map_header(c, reply + maxlen - c,
928 vector_foreach_slot(vecs->mpvec, mpp, i)
929 c += snprint_map(c, reply + maxlen - c,
930 PRINT_MAP_FAILBACK, mpp);
932 again = ((c - reply) == (maxlen - 1));
935 reply = REALLOC(reply, maxlen *= 2);
938 *len = (int)(c - reply + 1);
943 dump_pathvec (char ** r, int * len, struct vectors * vecs)
950 *len = VECTOR_SIZE(vecs->pathvec) * sizeof(struct path);
951 reply = (char *)MALLOC(*len);
959 vector_foreach_slot (vecs->pathvec, pp, i) {
960 memcpy((void *)p, pp, sizeof(struct path));
961 p += sizeof(struct path);
964 /* return negative to hint caller not to add "ok" to the dump */
969 map_discovery (struct vectors * vecs)
972 struct multipath * mpp;
974 if (dm_get_maps(vecs->mpvec, "multipath"))
977 vector_foreach_slot (vecs->mpvec, mpp, i)
978 if (setup_multipath(vecs, mpp))
985 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
987 struct vectors * vecs;
992 vecs = (struct vectors *)trigger_data;
994 pthread_cleanup_push(cleanup_lock, vecs->lock);
997 r = parse_cmd(str, reply, len, vecs);
1000 *reply = STRDUP("fail\n");
1001 *len = strlen(*reply) + 1;
1004 else if (!r && *len == 0) {
1005 *reply = STRDUP("ok\n");
1006 *len = strlen(*reply) + 1;
1009 /* else if (r < 0) leave *reply alone */
1011 lock_cleanup_pop(vecs->lock);
1016 uev_discard(char * devpath)
1021 * keep only block devices, discard partitions
1023 if (sscanf(devpath, "/block/%10s", a) != 1 ||
1024 sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
1025 condlog(4, "discard event on %s", devpath);
1032 uev_trigger (struct uevent * uev, void * trigger_data)
1036 struct vectors * vecs;
1038 vecs = (struct vectors *)trigger_data;
1040 if (uev_discard(uev->devpath))
1043 basename(uev->devpath, devname);
1047 * device map add/remove event
1049 if (!strncmp(devname, "dm-", 3)) {
1050 if (!strncmp(uev->action, "add", 3)) {
1051 r = uev_add_map(devname, vecs);
1054 if (!strncmp(uev->action, "remove", 6)) {
1055 r = uev_remove_map(devname, vecs);
1058 if (!strncmp(uev->action, "umount", 6)) {
1059 r = uev_umount_map(devname, vecs);
1066 * path add/remove event
1068 if (blacklist(conf->blist, devname))
1071 if (!strncmp(uev->action, "add", 3)) {
1072 r = uev_add_path(devname, vecs);
1075 if (!strncmp(uev->action, "remove", 6)) {
1076 r = uev_remove_path(devname, vecs);
1086 ueventloop (void * ap)
1088 if (uevent_listen(&uev_trigger, ap))
1089 fprintf(stderr, "error starting uevent listener");
1095 uxlsnrloop (void * ap)
1100 if (alloc_handlers())
1103 add_handler(LIST+PATHS, cli_list_paths);
1104 add_handler(LIST+MAPS, cli_list_maps);
1105 add_handler(ADD+PATH, cli_add_path);
1106 add_handler(DEL+PATH, cli_del_path);
1107 add_handler(ADD+MAP, cli_add_map);
1108 add_handler(DEL+MAP, cli_del_map);
1109 add_handler(SWITCH+MAP+GROUP, cli_switch_group);
1110 add_handler(DUMP+PATHVEC, cli_dump_pathvec);
1111 add_handler(RECONFIGURE, cli_reconfigure);
1112 add_handler(SUSPEND+MAP, cli_suspend);
1113 add_handler(RESUME+MAP, cli_resume);
1114 add_handler(REINSTATE+PATH, cli_reinstate);
1115 add_handler(FAIL+PATH, cli_fail);
1117 uxsock_listen(&uxsock_trigger, ap);
1123 exit_daemon (int status)
1126 fprintf(stderr, "bad exit status. see daemon.log\n");
1128 condlog(3, "unlink pidfile");
1129 unlink(DEFAULT_PIDFILE);
1132 pthread_cond_signal(&exit_cond);
1133 unlock(&exit_mutex);
1139 fail_path (struct path * pp)
1144 condlog(2, "checker failed path %s in map %s",
1145 pp->dev_t, pp->mpp->alias);
1147 dm_fail_path(pp->mpp->alias, pp->dev_t);
1148 update_queue_mode_del_path(pp->mpp);
1152 * caller must have locked the path list before calling that function
1155 reinstate_path (struct path * pp)
1160 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
1161 condlog(0, "%s: reinstate failed", pp->dev_t);
1163 condlog(2, "%s: reinstated", pp->dev_t);
1164 update_queue_mode_add_path(pp->mpp);
1169 enable_group(struct path * pp)
1171 struct pathgroup * pgp;
1174 * if path is added through uev_add_path, pgindex can be unset.
1175 * next update_strings() will set it, upon map reload event.
1177 * we can safely return here, because upon map reload, all
1178 * PG will be enabled.
1180 if (!pp->mpp->pg || !pp->pgindex)
1183 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1185 if (pgp->status == PGSTATE_DISABLED) {
1186 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1187 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1192 mpvec_garbage_collector (struct vectors * vecs)
1194 struct multipath * mpp;
1197 vector_foreach_slot (vecs->mpvec, mpp, i) {
1198 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1199 condlog(2, "%s: remove dead map", mpp->alias);
1200 remove_map(mpp, vecs, stop_waiter_thread, 1);
1207 defered_failback_tick (vector mpvec)
1209 struct multipath * mpp;
1212 vector_foreach_slot (mpvec, mpp, i) {
1214 * defered failback getting sooner
1216 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1217 mpp->failback_tick--;
1219 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1220 switch_pathgroup(mpp);
1226 retry_count_tick(vector mpvec)
1228 struct multipath *mpp;
1231 vector_foreach_slot (mpvec, mpp, i) {
1232 if (mpp->retry_tick) {
1233 condlog(4, "%s: Retrying.. No active path", mpp->alias);
1234 if(--mpp->retry_tick == 0) {
1235 dm_queue_if_no_path(mpp->alias, 0);
1236 condlog(2, "%s: Disable queueing", mpp->alias);
1243 checkerloop (void *ap)
1245 struct vectors *vecs;
1249 char checker_msg[MAX_CHECKER_MSG_SIZE];
1251 mlockall(MCL_CURRENT | MCL_FUTURE);
1253 memset(checker_msg, 0, MAX_CHECKER_MSG_SIZE);
1254 vecs = (struct vectors *)ap;
1256 condlog(2, "path checkers start up");
1259 * init the path check interval
1261 vector_foreach_slot (vecs->pathvec, pp, i) {
1262 pp->checkint = conf->checkint;
1266 pthread_cleanup_push(cleanup_lock, vecs->lock);
1270 vector_foreach_slot (vecs->pathvec, pp, i) {
1274 if (pp->tick && --pp->tick)
1275 continue; /* don't check this path yet */
1278 * provision a next check soonest,
1279 * in case we exit abnormaly from here
1281 pp->tick = conf->checkint;
1284 pathinfo(pp, conf->hwtable, DI_SYSFS);
1289 condlog(0, "%s: checkfn is void", pp->dev);
1292 newstate = pp->checkfn(pp->fd, checker_msg,
1293 &pp->checker_context);
1296 condlog(2, "%s: unusable path", pp->dev);
1297 pathinfo(pp, conf->hwtable, 0);
1301 if (newstate != pp->state) {
1302 pp->state = newstate;
1303 LOG_MSG(1, checker_msg);
1306 * upon state change, reset the checkint
1307 * to the shortest delay
1309 pp->checkint = conf->checkint;
1311 if (newstate == PATH_DOWN ||
1312 newstate == PATH_SHAKY ||
1313 update_multipath_strings(pp->mpp,
1316 * proactively fail path in the DM
1321 * cancel scheduled failback
1323 pp->mpp->failback_tick = 0;
1329 * reinstate this path
1334 * schedule [defered] failback
1336 if (pp->mpp->pgfailback > 0)
1337 pp->mpp->failback_tick =
1338 pp->mpp->pgfailback + 1;
1339 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
1340 need_switch_pathgroup(pp->mpp, 1))
1341 switch_pathgroup(pp->mpp);
1344 * if at least one path is up in a group, and
1345 * the group is disabled, re-enable it
1347 if (newstate == PATH_UP)
1350 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1351 LOG_MSG(4, checker_msg);
1353 * double the next check delay.
1354 * max at conf->max_checkint
1356 if (pp->checkint < (conf->max_checkint / 2))
1357 pp->checkint = 2 * pp->checkint;
1359 pp->checkint = conf->max_checkint;
1361 pp->tick = pp->checkint;
1362 condlog(4, "%s: delay next check %is",
1363 pp->dev_t, pp->tick);
1366 pp->state = newstate;
1369 * path prio refreshing
1371 condlog(4, "path prio refresh");
1372 pathinfo(pp, conf->hwtable, DI_PRIO);
1374 if (need_switch_pathgroup(pp->mpp, 0)) {
1375 if (pp->mpp->pgfailback > 0)
1376 pp->mpp->failback_tick =
1377 pp->mpp->pgfailback + 1;
1378 else if (pp->mpp->pgfailback ==
1379 -FAILBACK_IMMEDIATE)
1380 switch_pathgroup(pp->mpp);
1383 defered_failback_tick(vecs->mpvec);
1384 retry_count_tick(vecs->mpvec);
1389 condlog(4, "map garbage collection");
1390 mpvec_garbage_collector(vecs);
1394 lock_cleanup_pop(vecs->lock);
1401 configure (struct vectors * vecs, int start_waiters)
1403 struct multipath * mpp;
1408 if (!(vecs->pathvec = vector_alloc()))
1411 if (!(vecs->mpvec = vector_alloc()))
1414 if (!(mpvec = vector_alloc()))
1418 * probe for current path (from sysfs) and map (from dm) sets
1420 path_discovery(vecs->pathvec, conf, DI_ALL);
1422 vector_foreach_slot (vecs->pathvec, pp, i)
1423 pp->checkint = conf->checkint;
1425 if (map_discovery(vecs))
1429 * create new set of maps & push changed ones into dm
1431 if (coalesce_paths(vecs, mpvec, NULL))
1435 * may need to remove some maps which are no longer relevant
1436 * e.g., due to blacklist changes in conf file
1438 if (coalesce_maps(vecs, mpvec))
1443 if (conf->verbosity > 2)
1444 vector_foreach_slot(mpvec, mpp, i)
1448 * purge dm of old maps
1450 remove_maps(vecs, NULL);
1453 * save new set of maps formed by considering current path state
1455 vecs->mpvec = mpvec;
1458 * start dm event waiter threads for these new maps
1460 vector_foreach_slot(vecs->mpvec, mpp, i) {
1461 if (setup_multipath(vecs, mpp))
1464 if (start_waiter_thread(mpp, vecs))
1471 reconfigure (struct vectors * vecs)
1473 struct config * old = conf;
1475 condlog(0, "reconfigure");
1478 * free old map and path vectors ... they use old conf state
1480 if (VECTOR_SIZE(vecs->mpvec))
1481 remove_maps(vecs, stop_waiter_thread);
1483 if (VECTOR_SIZE(vecs->pathvec))
1484 free_pathvec(vecs->pathvec, FREE_PATHS);
1488 if (load_config(DEFAULT_CONFIGFILE))
1491 conf->verbosity = old->verbosity;
1493 if (!conf->checkint) {
1494 conf->checkint = CHECKINT;
1495 conf->max_checkint = MAX_CHECKINT;
1502 static struct vectors *
1505 struct vectors * vecs;
1507 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1513 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1518 vecs->pathvec = vector_alloc();
1523 vecs->mpvec = vector_alloc();
1528 pthread_mutex_init(vecs->lock, NULL);
1533 vector_free(vecs->pathvec);
1538 condlog(0, "failed to init paths");
1543 signal_set(int signo, void (*func) (int))
1546 struct sigaction sig;
1547 struct sigaction osig;
1549 sig.sa_handler = func;
1550 sigemptyset(&sig.sa_mask);
1553 r = sigaction(signo, &sig, &osig);
1558 return (osig.sa_handler);
1564 condlog(3, "SIGHUP received");
1568 unlock(gvecs->lock);
1571 dbg_free_final(NULL);
1584 condlog(3, "SIGUSR1 received");
1590 signal_set(SIGHUP, sighup);
1591 signal_set(SIGUSR1, sigusr1);
1592 signal_set(SIGINT, sigend);
1593 signal_set(SIGTERM, sigend);
1594 signal_set(SIGKILL, sigend);
1601 static struct sched_param sched_param = {
1605 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1608 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1613 set_oom_adj (int val)
1617 fp = fopen("/proc/self/oom_adj", "w");
1622 fprintf(fp, "%i", val);
1627 child (void * param)
1629 pthread_t check_thr, uevent_thr, uxlsnr_thr;
1630 pthread_attr_t attr;
1631 struct vectors * vecs;
1633 mlockall(MCL_CURRENT | MCL_FUTURE);
1638 condlog(2, "--------start up--------");
1639 condlog(2, "read " DEFAULT_CONFIGFILE);
1641 if (load_config(DEFAULT_CONFIGFILE))
1644 setlogmask(LOG_UPTO(conf->verbosity + 3));
1647 * fill the voids left in the config file
1649 if (!conf->checkint) {
1650 conf->checkint = CHECKINT;
1651 conf->max_checkint = MAX_CHECKINT;
1654 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1663 vecs = gvecs = init_vecs();
1668 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
1669 condlog(0, "can not find sysfs mount point");
1674 * fetch and configure both paths and multipaths
1676 if (configure(vecs, 1)) {
1677 condlog(0, "failure during configuration");
1684 pthread_attr_init(&attr);
1685 pthread_attr_setstacksize(&attr, 64 * 1024);
1686 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1688 pthread_create(&check_thr, &attr, checkerloop, vecs);
1689 pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1690 pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1692 pthread_cond_wait(&exit_cond, &exit_mutex);
1698 remove_maps(vecs, stop_waiter_thread);
1699 free_pathvec(vecs->pathvec, FREE_PATHS);
1701 pthread_cancel(check_thr);
1702 pthread_cancel(uevent_thr);
1703 pthread_cancel(uxlsnr_thr);
1707 free_handlers(handlers);
1712 pthread_mutex_destroy(vecs->lock);
1720 condlog(2, "--------shut down-------");
1729 dbg_free_final(NULL);
1741 if( (pid = fork()) < 0){
1742 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1750 if ( (pid = fork()) < 0)
1751 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1755 in_fd = open("/dev/null", O_RDONLY);
1757 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1761 out_fd = open("/dev/console", O_WRONLY);
1763 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1768 close(STDIN_FILENO);
1770 close(STDOUT_FILENO);
1772 close(STDERR_FILENO);
1783 main (int argc, char *argv[])
1785 extern char *optarg;
1792 if (getuid() != 0) {
1793 fprintf(stderr, "need to be root\n");
1797 /* make sure we don't lock any path */
1799 umask(umask(077) | 022);
1801 conf = alloc_config();
1806 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1810 //debug=1; /* ### comment me out ### */
1813 if (sizeof(optarg) > sizeof(char *) ||
1814 !isdigit(optarg[0]))
1817 conf->verbosity = atoi(optarg);
1840 return (child(NULL));