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>
16 #include <sys/resource.h>
36 #include <blacklist.h>
37 #include <structs_vec.h>
39 #include <devmapper.h>
42 #include <discovery.h>
46 #include <switchgroup.h>
48 #include <configure.h>
50 #include <pgpolicies.h>
57 #include "cli_handlers.h"
61 #define FILE_NAME_SIZE 256
64 #define LOG_MSG(a,b) \
65 if (strlen(b)) condlog(a, "%s: %s - %s", pp->mpp->alias, pp->dev, b);
67 pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
68 pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
73 * global copy of vecs for use in sig handlers
75 struct vectors * gvecs;
78 need_switch_pathgroup (struct multipath * mpp, int refresh)
80 struct pathgroup * pgp;
84 if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
88 * Refresh path priority values
91 vector_foreach_slot (mpp->pg, pgp, i)
92 vector_foreach_slot (pgp->paths, pp, j)
93 pathinfo(pp, conf->hwtable, DI_PRIO);
95 mpp->bestpg = select_path_group(mpp);
97 if (mpp->bestpg != mpp->nextpg)
104 switch_pathgroup (struct multipath * mpp)
106 mpp->stat_switchgroup++;
107 dm_switchgroup(mpp->alias, mpp->bestpg);
108 condlog(2, "%s: switch to path group #%i",
109 mpp->alias, mpp->bestpg);
113 coalesce_maps(struct vectors *vecs, vector nmpv)
115 struct multipath * ompp;
116 vector ompv = vecs->mpvec;
120 vector_foreach_slot (ompv, ompp, i) {
121 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
123 * remove all current maps not allowed by the
124 * current configuration
126 if (dm_flush_map(ompp->alias)) {
127 condlog(0, "%s: unable to flush devmap",
130 * may be just because the device is open
132 if (!vector_alloc_slot(nmpv))
135 vector_set_slot(nmpv, ompp);
136 setup_multipath(vecs, ompp);
138 if ((j = find_slot(ompv, (void *)ompp)) != -1)
139 vector_del_slot(ompv, j);
145 condlog(2, "%s devmap removed", ompp->alias);
153 sync_map_state(struct multipath *mpp)
155 struct pathgroup *pgp;
162 vector_foreach_slot (mpp->pg, pgp, i){
163 vector_foreach_slot (pgp->paths, pp, j){
164 if (pp->state == PATH_UNCHECKED ||
165 pp->state == PATH_WILD)
167 if ((pp->dmstate == PSTATE_FAILED ||
168 pp->dmstate == PSTATE_UNDEF) &&
169 (pp->state == PATH_UP || pp->state == PATH_GHOST))
170 dm_reinstate_path(mpp->alias, pp->dev_t);
171 else if ((pp->dmstate == PSTATE_ACTIVE ||
172 pp->dmstate == PSTATE_UNDEF) &&
173 (pp->state == PATH_DOWN ||
174 pp->state == PATH_SHAKY))
175 dm_fail_path(mpp->alias, pp->dev_t);
181 sync_maps_state(vector mpvec)
184 struct multipath *mpp;
186 vector_foreach_slot (mpvec, mpp, i)
191 flush_map(struct multipath * mpp, struct vectors * vecs)
194 * clear references to this map before flushing so we can ignore
195 * the spurious uevent we may generate with the dm_flush_map call below
197 if (dm_flush_map(mpp->alias)) {
199 * May not really be an error -- if the map was already flushed
200 * from the device mapper by dmsetup(8) for instance.
202 condlog(0, "%s: can't flush", mpp->alias);
207 condlog(2, "%s: devmap removed", mpp->alias);
210 orphan_paths(vecs->pathvec, mpp);
211 remove_map_and_stop_waiter(mpp, vecs, 1);
217 uev_add_map (struct sysfs_device * dev, struct vectors * vecs)
219 condlog(2, "%s: add map (uevent)", dev->kernel);
220 return ev_add_map(dev, vecs);
224 ev_add_map (struct sysfs_device * dev, struct vectors * vecs)
230 struct multipath * mpp;
234 dev_t = sysfs_attr_get_value(dev->devpath, "dev");
236 if (!dev_t || sscanf(dev_t, "%d:%d", &major, &minor) != 2)
239 alias = dm_mapname(major, minor);
244 map_present = dm_map_present(alias);
246 if (map_present && dm_type(alias, TGT_MPATH) <= 0) {
247 condlog(4, "%s: not a multipath map", alias);
252 mpp = find_mp_by_alias(vecs->mpvec, alias);
256 * Not really an error -- we generate our own uevent
257 * if we create a multipath mapped device as a result
260 condlog(0, "%s: devmap already registered",
267 * now we can register the map
269 if (map_present && (mpp = add_map_without_path(vecs, minor, alias))) {
271 condlog(2, "%s: devmap %s added", alias, dev->kernel);
274 refwwid = get_refwwid(dev->kernel, DEV_DEVMAP, vecs->pathvec);
277 r = coalesce_paths(vecs, NULL, refwwid, 0);
282 condlog(2, "%s: devmap %s added", alias, dev->kernel);
284 condlog(0, "%s: uev_add_map %s failed", alias, dev->kernel);
292 uev_remove_map (struct sysfs_device * dev, struct vectors * vecs)
294 condlog(2, "%s: remove map (uevent)", dev->kernel);
295 return ev_remove_map(dev->kernel, vecs);
299 ev_remove_map (char * devname, struct vectors * vecs)
301 struct multipath * mpp;
303 mpp = find_mp_by_str(vecs->mpvec, devname);
306 condlog(2, "%s: devmap not registered, can't remove",
310 flush_map(mpp, vecs);
316 uev_umount_map (struct sysfs_device * dev, struct vectors * vecs)
318 struct multipath * mpp;
320 condlog(2, "%s: umount map (uevent)", dev->kernel);
322 mpp = find_mp_by_str(vecs->mpvec, dev->kernel);
327 update_mpp_paths(mpp, vecs->pathvec);
328 verify_paths(mpp, vecs, NULL);
330 if (!VECTOR_SIZE(mpp->paths))
331 flush_map(mpp, vecs);
337 uev_add_path (struct sysfs_device * dev, struct vectors * vecs)
339 condlog(2, "%s: add path (uevent)", dev->kernel);
340 return (ev_add_path(dev->kernel, vecs) != 1)? 0 : 1;
351 ev_add_path (char * devname, struct vectors * vecs)
353 struct multipath * mpp;
355 char empty_buff[WWID_SIZE] = {0};
357 if (strstr(devname, "..") != NULL) {
359 * Don't allow relative device names in the pathvec
361 condlog(0, "%s: path name is invalid", devname);
365 pp = find_path_by_dev(vecs->pathvec, devname);
368 condlog(0, "%s: spurious uevent, path already in pathvec",
375 * get path vital state
377 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
379 condlog(0, "%s: failed to store path info", devname);
382 pp->checkint = conf->checkint;
386 * need path UID to go any further
388 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
389 condlog(0, "%s: failed to get path uid", devname);
390 return 1; /* leave path added to pathvec */
392 if (filter_path(conf, pp) > 0){
393 int i = find_slot(vecs->pathvec, (void *)pp);
395 vector_del_slot(vecs->pathvec, i);
399 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
402 if ((!pp->size) || (mpp->size != pp->size)) {
404 condlog(0, "%s: failed to add new path %s, "
408 condlog(0, "%s: failed to add new path %s, "
409 "device size mismatch",
411 int i = find_slot(vecs->pathvec, (void *)pp);
413 vector_del_slot(vecs->pathvec, i);
418 condlog(4,"%s: adopting all paths for path %s",
419 mpp->alias, pp->dev);
420 if (adopt_paths(vecs->pathvec, mpp))
421 return 1; /* leave path added to pathvec */
423 verify_paths(mpp, vecs, NULL);
424 mpp->flush_on_last_del = FLUSH_UNDEF;
425 mpp->action = ACT_RELOAD;
429 condlog(0, "%s: failed to create new map,"
430 " %s device size is 0 ", devname, pp->dev);
431 int i = find_slot(vecs->pathvec, (void *)pp);
433 vector_del_slot(vecs->pathvec, i);
438 condlog(4,"%s: creating new map", pp->dev);
439 if ((mpp = add_map_with_path(vecs, pp, 1)))
440 mpp->action = ACT_CREATE;
442 return 1; /* leave path added to pathvec */
446 * push the map to the device-mapper
448 if (setup_map(mpp)) {
449 condlog(0, "%s: failed to setup map for addition of new "
450 "path %s", mpp->alias, devname);
454 * reload the map for the multipath mapped device
456 if (domap(mpp) <= 0) {
457 condlog(0, "%s: failed in domap for addition of new "
458 "path %s", mpp->alias, devname);
460 * deal with asynchronous uevents :((
462 if (mpp->action == ACT_RELOAD) {
463 condlog(0, "%s: uev_add_path sleep", mpp->alias);
465 update_mpp_paths(mpp, vecs->pathvec);
474 * update our state from kernel regardless of create or reload
476 if (setup_multipath(vecs, mpp))
481 if (mpp->action == ACT_CREATE &&
482 start_waiter_thread(mpp, vecs))
485 condlog(2, "%s path added to devmap %s", devname, mpp->alias);
489 remove_map(mpp, vecs, 1);
494 uev_remove_path (struct sysfs_device * dev, struct vectors * vecs)
498 condlog(2, "%s: remove path (uevent)", dev->kernel);
499 retval = ev_remove_path(dev->kernel, vecs);
501 sysfs_device_put(dev);
507 ev_remove_path (char * devname, struct vectors * vecs)
509 struct multipath * mpp;
513 pp = find_path_by_dev(vecs->pathvec, devname);
516 /* Not an error; path might have been purged earlier */
517 condlog(0, "%s: path already removed", devname);
522 * avoid referring to the map of an orphaned path
524 if ((mpp = pp->mpp)) {
526 * transform the mp->pg vector of vectors of paths
527 * into a mp->params string to feed the device-mapper
529 if (update_mpp_paths(mpp, vecs->pathvec)) {
530 condlog(0, "%s: failed to update paths",
534 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
535 vector_del_slot(mpp->paths, i);
538 * remove the map IFF removing the last path
540 if (VECTOR_SIZE(mpp->paths) == 0) {
541 char alias[WWID_SIZE];
544 * flush_map will fail if the device is open
546 strncpy(alias, mpp->alias, WWID_SIZE);
547 if (mpp->flush_on_last_del == FLUSH_ENABLED) {
548 condlog(2, "%s Last path deleted, disabling queueing", mpp->alias);
550 mpp->no_path_retry = NO_PATH_RETRY_FAIL;
551 mpp->flush_on_last_del = FLUSH_IN_PROGRESS;
552 dm_queue_if_no_path(mpp->alias, 0);
554 if (!flush_map(mpp, vecs)) {
555 condlog(2, "%s: removed map after"
556 " removing all paths",
562 * Not an error, continue
566 if (setup_map(mpp)) {
567 condlog(0, "%s: failed to setup map for"
568 " removal of path %s", mpp->alias,
575 mpp->action = ACT_RELOAD;
576 if (domap(mpp) <= 0) {
577 condlog(0, "%s: failed in domap for "
578 "removal of path %s",
579 mpp->alias, devname);
583 * update our state from kernel
585 if (setup_multipath(vecs, mpp)) {
590 condlog(2, "%s: path removed from map %s",
591 devname, mpp->alias);
596 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
597 vector_del_slot(vecs->pathvec, i);
604 remove_map_and_stop_waiter(mpp, vecs, 1);
609 map_discovery (struct vectors * vecs)
611 struct multipath * mpp;
614 if (dm_get_maps(vecs->mpvec))
617 vector_foreach_slot (vecs->mpvec, mpp, i)
618 if (setup_multipath(vecs, mpp))
625 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
627 struct vectors * vecs;
632 vecs = (struct vectors *)trigger_data;
634 pthread_cleanup_push(cleanup_lock, &vecs->lock);
637 r = parse_cmd(str, reply, len, vecs);
640 *reply = STRDUP("fail\n");
641 *len = strlen(*reply) + 1;
644 else if (!r && *len == 0) {
645 *reply = STRDUP("ok\n");
646 *len = strlen(*reply) + 1;
649 /* else if (r < 0) leave *reply alone */
651 lock_cleanup_pop(vecs->lock);
656 uev_discard(char * devpath)
662 * keep only block devices, discard partitions
664 tmp = strstr(devpath, "/block/");
666 condlog(4, "no /block/ in '%s'", devpath);
669 if (sscanf(tmp, "/block/%10s", a) != 1 ||
670 sscanf(tmp, "/block/%10[^/]/%10s", a, b) == 2) {
671 condlog(4, "discard event on %s", devpath);
678 uev_trigger (struct uevent * uev, void * trigger_data)
681 struct sysfs_device *sysdev;
682 struct vectors * vecs;
684 vecs = (struct vectors *)trigger_data;
686 if (uev_discard(uev->devpath))
689 sysdev = sysfs_device_get(uev->devpath);
697 * Add events are ignored here as the tables
698 * are not fully initialised then.
700 if (!strncmp(sysdev->kernel, "dm-", 3)) {
701 if (!strncmp(uev->action, "change", 6)) {
702 r = uev_add_map(sysdev, vecs);
705 if (!strncmp(uev->action, "remove", 6)) {
706 r = uev_remove_map(sysdev, vecs);
709 if (!strncmp(uev->action, "umount", 6)) {
710 r = uev_umount_map(sysdev, vecs);
717 * path add/remove event
719 if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
723 if (!strncmp(uev->action, "add", 3)) {
724 r = uev_add_path(sysdev, vecs);
727 if (!strncmp(uev->action, "remove", 6)) {
728 r = uev_remove_path(sysdev, vecs);
738 ueventloop (void * ap)
740 block_signal(SIGUSR1, NULL);
741 block_signal(SIGHUP, NULL);
743 if (uevent_listen(&uev_trigger, ap))
744 fprintf(stderr, "error starting uevent listener");
750 uxlsnrloop (void * ap)
752 block_signal(SIGUSR1, NULL);
753 block_signal(SIGHUP, NULL);
758 set_handler_callback(LIST+PATHS, cli_list_paths);
759 set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt);
760 set_handler_callback(LIST+MAPS, cli_list_maps);
761 set_handler_callback(LIST+STATUS, cli_list_status);
762 set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
763 set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
764 set_handler_callback(LIST+MAPS+FMT, cli_list_maps_fmt);
765 set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
766 set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
767 set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
768 set_handler_callback(LIST+CONFIG, cli_list_config);
769 set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
770 set_handler_callback(LIST+DEVICES, cli_list_devices);
771 set_handler_callback(LIST+WILDCARDS, cli_list_wildcards);
772 set_handler_callback(ADD+PATH, cli_add_path);
773 set_handler_callback(DEL+PATH, cli_del_path);
774 set_handler_callback(ADD+MAP, cli_add_map);
775 set_handler_callback(DEL+MAP, cli_del_map);
776 set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
777 set_handler_callback(RECONFIGURE, cli_reconfigure);
778 set_handler_callback(SUSPEND+MAP, cli_suspend);
779 set_handler_callback(RESUME+MAP, cli_resume);
780 set_handler_callback(RESIZE+MAP, cli_resize);
781 set_handler_callback(REINSTATE+PATH, cli_reinstate);
782 set_handler_callback(FAIL+PATH, cli_fail);
783 set_handler_callback(DISABLEQ+MAP, cli_disable_queueing);
784 set_handler_callback(RESTOREQ+MAP, cli_restore_queueing);
785 set_handler_callback(DISABLEQ+MAPS, cli_disable_all_queueing);
786 set_handler_callback(RESTOREQ+MAPS, cli_restore_all_queueing);
787 set_handler_callback(QUIT, cli_quit);
790 uxsock_listen(&uxsock_trigger, ap);
796 exit_daemon (int status)
799 fprintf(stderr, "bad exit status. see daemon.log\n");
801 condlog(3, "unlink pidfile");
802 unlink(DEFAULT_PIDFILE);
804 pthread_mutex_lock(&exit_mutex);
805 pthread_cond_signal(&exit_cond);
806 pthread_mutex_unlock(&exit_mutex);
812 fail_path (struct path * pp, int del_active)
817 condlog(2, "checker failed path %s in map %s",
818 pp->dev_t, pp->mpp->alias);
820 dm_fail_path(pp->mpp->alias, pp->dev_t);
822 update_queue_mode_del_path(pp->mpp);
826 * caller must have locked the path list before calling that function
829 reinstate_path (struct path * pp, int add_active)
834 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
835 condlog(0, "%s: reinstate failed", pp->dev_t);
837 condlog(2, "%s: reinstated", pp->dev_t);
839 update_queue_mode_add_path(pp->mpp);
844 enable_group(struct path * pp)
846 struct pathgroup * pgp;
849 * if path is added through uev_add_path, pgindex can be unset.
850 * next update_strings() will set it, upon map reload event.
852 * we can safely return here, because upon map reload, all
853 * PG will be enabled.
855 if (!pp->mpp->pg || !pp->pgindex)
858 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
860 if (pgp->status == PGSTATE_DISABLED) {
861 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
862 dm_enablegroup(pp->mpp->alias, pp->pgindex);
867 mpvec_garbage_collector (struct vectors * vecs)
869 struct multipath * mpp;
875 vector_foreach_slot (vecs->mpvec, mpp, i) {
876 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
877 condlog(2, "%s: remove dead map", mpp->alias);
878 remove_map_and_stop_waiter(mpp, vecs, 1);
885 defered_failback_tick (vector mpvec)
887 struct multipath * mpp;
890 vector_foreach_slot (mpvec, mpp, i) {
892 * defered failback getting sooner
894 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
895 mpp->failback_tick--;
897 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
898 switch_pathgroup(mpp);
904 retry_count_tick(vector mpvec)
906 struct multipath *mpp;
909 vector_foreach_slot (mpvec, mpp, i) {
910 if (mpp->retry_tick) {
911 mpp->stat_total_queueing_time++;
912 condlog(4, "%s: Retrying.. No active path", mpp->alias);
913 if(--mpp->retry_tick == 0) {
914 dm_queue_if_no_path(mpp->alias, 0);
915 condlog(2, "%s: Disable queueing", mpp->alias);
921 int update_path_groups(struct multipath *mpp, struct vectors *vecs)
926 update_mpp_paths(mpp, vecs->pathvec);
927 vector_foreach_slot (mpp->paths, pp, i)
928 pathinfo(pp, conf->hwtable, DI_PRIO);
930 mpp->action = ACT_RELOAD;
931 if (domap(mpp) <= 0) {
932 condlog(0, "%s: failed to update map : %s", mpp->alias,
937 setup_multipath(vecs, mpp);
944 check_path (struct vectors * vecs, struct path * pp)
952 if (pp->tick && --pp->tick)
953 return; /* don't check this path yet */
956 * provision a next check soonest,
957 * in case we exit abnormaly from here
959 pp->tick = conf->checkint;
961 newstate = get_state(pp, 1);
963 if (newstate == PATH_WILD || newstate == PATH_UNCHECKED) {
964 condlog(2, "%s: unusable path", pp->dev);
965 pathinfo(pp, conf->hwtable, 0);
969 * Async IO in flight. Keep the previous path state
970 * and reschedule as soon as possible
972 if (newstate == PATH_PENDING) {
976 if (newstate != pp->state) {
977 int oldstate = pp->state;
978 pp->state = newstate;
979 LOG_MSG(1, checker_message(&pp->checker));
982 * upon state change, reset the checkint
983 * to the shortest delay
985 pp->checkint = conf->checkint;
987 if (newstate == PATH_DOWN || newstate == PATH_SHAKY ||
988 update_multipath_strings(pp->mpp, vecs->pathvec)) {
990 * proactively fail path in the DM
992 if (oldstate == PATH_UP ||
993 oldstate == PATH_GHOST)
999 * cancel scheduled failback
1001 pp->mpp->failback_tick = 0;
1003 pp->mpp->stat_path_failures++;
1008 * reinstate this path
1010 if (oldstate != PATH_UP &&
1011 oldstate != PATH_GHOST)
1012 reinstate_path(pp, 1);
1014 reinstate_path(pp, 0);
1017 * schedule [defered] failback
1019 if (pp->mpp->pgfailback > 0)
1020 pp->mpp->failback_tick =
1021 pp->mpp->pgfailback + 1;
1022 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
1023 need_switch_pathgroup(pp->mpp, 1))
1024 switch_pathgroup(pp->mpp);
1027 * if at least one path is up in a group, and
1028 * the group is disabled, re-enable it
1030 if (newstate == PATH_UP)
1033 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1034 LOG_MSG(4, checker_message(&pp->checker));
1036 * double the next check delay.
1037 * max at conf->max_checkint
1039 if (pp->checkint < (conf->max_checkint / 2))
1040 pp->checkint = 2 * pp->checkint;
1042 pp->checkint = conf->max_checkint;
1044 pp->tick = pp->checkint;
1045 condlog(4, "%s: delay next check %is",
1046 pp->dev_t, pp->tick);
1048 else if (newstate == PATH_DOWN)
1049 LOG_MSG(2, checker_message(&pp->checker));
1051 pp->state = newstate;
1054 * path prio refreshing
1056 condlog(4, "path prio refresh");
1057 oldpriority = pp->priority;
1058 pathinfo(pp, conf->hwtable, DI_PRIO);
1061 * pathgroup failback policy
1063 if (pp->priority != oldpriority &&
1064 pp->mpp->pgpolicyfn == (pgpolicyfn *)group_by_prio)
1065 update_path_groups(pp->mpp, vecs);
1066 else if (need_switch_pathgroup(pp->mpp, 0)) {
1067 if (pp->mpp->pgfailback > 0 &&
1068 pp->mpp->failback_tick <= 0)
1069 pp->mpp->failback_tick =
1070 pp->mpp->pgfailback + 1;
1071 else if (pp->mpp->pgfailback ==
1072 -FAILBACK_IMMEDIATE)
1073 switch_pathgroup(pp->mpp);
1078 checkerloop (void *ap)
1080 struct vectors *vecs;
1086 mlockall(MCL_CURRENT | MCL_FUTURE);
1087 vecs = (struct vectors *)ap;
1088 condlog(2, "path checkers start up");
1091 * init the path check interval
1093 vector_foreach_slot (vecs->pathvec, pp, i) {
1094 pp->checkint = conf->checkint;
1098 block_signal(SIGHUP, &old);
1099 pthread_cleanup_push(cleanup_lock, &vecs->lock);
1103 if (vecs->pathvec) {
1104 vector_foreach_slot (vecs->pathvec, pp, i) {
1105 check_path(vecs, pp);
1109 defered_failback_tick(vecs->mpvec);
1110 retry_count_tick(vecs->mpvec);
1115 condlog(4, "map garbage collection");
1116 mpvec_garbage_collector(vecs);
1120 lock_cleanup_pop(vecs->lock);
1121 pthread_sigmask(SIG_SETMASK, &old, NULL);
1128 configure (struct vectors * vecs, int start_waiters)
1130 struct multipath * mpp;
1135 if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
1138 if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
1141 if (!(mpvec = vector_alloc()))
1145 * probe for current path (from sysfs) and map (from dm) sets
1147 path_discovery(vecs->pathvec, conf, DI_ALL);
1149 vector_foreach_slot (vecs->pathvec, pp, i){
1150 if (filter_path(conf, pp) > 0){
1151 vector_del_slot(vecs->pathvec, i);
1156 pp->checkint = conf->checkint;
1158 if (map_discovery(vecs))
1162 * create new set of maps & push changed ones into dm
1164 if (coalesce_paths(vecs, mpvec, NULL, 0))
1168 * may need to remove some maps which are no longer relevant
1169 * e.g., due to blacklist changes in conf file
1171 if (coalesce_maps(vecs, mpvec))
1176 sync_maps_state(mpvec);
1179 * purge dm of old maps
1184 * save new set of maps formed by considering current path state
1186 vector_free(vecs->mpvec);
1187 vecs->mpvec = mpvec;
1190 * start dm event waiter threads for these new maps
1192 vector_foreach_slot(vecs->mpvec, mpp, i) {
1193 if (setup_multipath(vecs, mpp))
1196 if (start_waiter_thread(mpp, vecs))
1203 reconfigure (struct vectors * vecs)
1205 struct config * old = conf;
1208 * free old map and path vectors ... they use old conf state
1210 if (VECTOR_SIZE(vecs->mpvec))
1211 remove_maps_and_stop_waiters(vecs);
1213 if (VECTOR_SIZE(vecs->pathvec))
1214 free_pathvec(vecs->pathvec, FREE_PATHS);
1216 vecs->pathvec = NULL;
1219 if (load_config(DEFAULT_CONFIGFILE))
1222 conf->verbosity = old->verbosity;
1224 if (!conf->checkint) {
1225 conf->checkint = DEFAULT_CHECKINT;
1226 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1234 static struct vectors *
1237 struct vectors * vecs;
1239 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1245 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1247 if (!vecs->lock.mutex)
1250 pthread_mutex_init(vecs->lock.mutex, NULL);
1251 vecs->lock.depth = 0;
1257 condlog(0, "failed to init paths");
1262 signal_set(int signo, void (*func) (int))
1265 struct sigaction sig;
1266 struct sigaction osig;
1268 sig.sa_handler = func;
1269 sigemptyset(&sig.sa_mask);
1272 r = sigaction(signo, &sig, &osig);
1277 return (osig.sa_handler);
1283 condlog(2, "reconfigure (SIGHUP)");
1287 unlock(gvecs->lock);
1290 dbg_free_final(NULL);
1303 condlog(3, "SIGUSR1 received");
1309 signal_set(SIGHUP, sighup);
1310 signal_set(SIGUSR1, sigusr1);
1311 signal_set(SIGINT, sigend);
1312 signal_set(SIGTERM, sigend);
1313 signal(SIGPIPE, SIG_IGN);
1320 static struct sched_param sched_param = {
1321 .sched_priority = 99
1324 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1327 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1332 set_oom_adj (int val)
1336 fp = fopen("/proc/self/oom_adj", "w");
1341 fprintf(fp, "%i", val);
1346 setup_thread_attr(pthread_attr_t *attr, size_t stacksize, int detached)
1348 if (pthread_attr_init(attr)) {
1349 fprintf(stderr, "can't initialize thread attr: %s\n",
1353 if (stacksize < PTHREAD_STACK_MIN)
1354 stacksize = PTHREAD_STACK_MIN;
1356 if (pthread_attr_setstacksize(attr, stacksize)) {
1357 fprintf(stderr, "can't set thread stack size to %lu: %s\n",
1358 (unsigned long)stacksize, strerror(errno));
1361 if (detached && pthread_attr_setdetachstate(attr,
1362 PTHREAD_CREATE_DETACHED)) {
1363 fprintf(stderr, "can't set thread to detached: %s\n",
1370 child (void * param)
1372 pthread_t check_thr, uevent_thr, uxlsnr_thr;
1373 pthread_attr_t log_attr, misc_attr;
1374 struct vectors * vecs;
1375 struct multipath * mpp;
1378 mlockall(MCL_CURRENT | MCL_FUTURE);
1380 setup_thread_attr(&misc_attr, 64 * 1024, 1);
1381 setup_thread_attr(&waiter_attr, 32 * 1024, 1);
1384 setup_thread_attr(&log_attr, 64 * 1024, 0);
1385 log_thread_start(&log_attr);
1386 pthread_attr_destroy(&log_attr);
1389 condlog(2, "--------start up--------");
1390 condlog(2, "read " DEFAULT_CONFIGFILE);
1392 if (load_config(DEFAULT_CONFIGFILE))
1395 if (init_checkers()) {
1396 condlog(0, "failed to initialize checkers");
1400 condlog(0, "failed to initialize prioritizers");
1404 setlogmask(LOG_UPTO(conf->verbosity + 3));
1407 * fill the voids left in the config file
1409 if (!conf->checkint) {
1410 conf->checkint = DEFAULT_CHECKINT;
1411 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1414 if (conf->max_fds) {
1415 struct rlimit fd_limit;
1417 fd_limit.rlim_cur = conf->max_fds;
1418 fd_limit.rlim_max = conf->max_fds;
1419 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0)
1420 condlog(0, "can't set open fds limit to %d : %s\n",
1421 conf->max_fds, strerror(errno));
1424 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1433 vecs = gvecs = init_vecs();
1438 if (sysfs_init(conf->sysfs_dir, FILE_NAME_SIZE)) {
1439 condlog(0, "can not find sysfs mount point");
1443 dm_udev_set_sync_support(0);
1445 * fetch and configure both paths and multipaths
1447 if (configure(vecs, 1)) {
1448 condlog(0, "failure during configuration");
1454 pthread_create(&check_thr, &misc_attr, checkerloop, vecs);
1455 pthread_create(&uevent_thr, &misc_attr, ueventloop, vecs);
1456 pthread_create(&uxlsnr_thr, &misc_attr, uxlsnrloop, vecs);
1457 pthread_attr_destroy(&misc_attr);
1459 pthread_mutex_lock(&exit_mutex);
1460 pthread_cond_wait(&exit_cond, &exit_mutex);
1465 block_signal(SIGHUP, NULL);
1467 if (conf->queue_without_daemon == QUE_NO_DAEMON_OFF)
1468 vector_foreach_slot(vecs->mpvec, mpp, i)
1469 dm_queue_if_no_path(mpp->alias, 0);
1470 remove_maps_and_stop_waiters(vecs);
1471 free_pathvec(vecs->pathvec, FREE_PATHS);
1473 pthread_cancel(check_thr);
1474 pthread_cancel(uevent_thr);
1475 pthread_cancel(uxlsnr_thr);
1481 free_handlers(handlers);
1486 /* Now all the waitevent threads will start rushing in. */
1487 while (vecs->lock.depth > 0) {
1488 sleep (1); /* This is weak. */
1489 condlog(3,"Have %d wait event checkers threads to de-alloc, waiting..\n", vecs->lock.depth);
1491 pthread_mutex_destroy(vecs->lock.mutex);
1492 FREE(vecs->lock.mutex);
1493 vecs->lock.depth = 0;
1494 vecs->lock.mutex = NULL;
1498 condlog(2, "--------shut down-------");
1509 * Freeing config must be done after condlog() and dm_lib_exit(),
1510 * because logging functions like dlog() and dm_write_log()
1511 * reference the config.
1517 dbg_free_final(NULL);
1529 if( (pid = fork()) < 0){
1530 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1538 if ( (pid = fork()) < 0)
1539 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1543 in_fd = open("/dev/null", O_RDONLY);
1545 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1549 out_fd = open("/dev/console", O_WRONLY);
1551 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1556 close(STDIN_FILENO);
1558 close(STDOUT_FILENO);
1560 close(STDERR_FILENO);
1566 fprintf(stderr, "cannot chdir to '/', continuing\n");
1572 main (int argc, char *argv[])
1574 extern char *optarg;
1582 if (getuid() != 0) {
1583 fprintf(stderr, "need to be root\n");
1587 /* make sure we don't lock any path */
1589 umask(umask(077) | 022);
1591 conf = alloc_config();
1596 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1600 //debug=1; /* ### comment me out ### */
1603 if (sizeof(optarg) > sizeof(char *) ||
1604 !isdigit(optarg[0]))
1607 conf->verbosity = atoi(optarg);
1616 if (optind < argc) {
1621 while (optind < argc) {
1622 if (strchr(argv[optind], ' '))
1623 c += snprintf(c, s + CMDSIZE - c, "\"%s\" ", argv[optind]);
1625 c += snprintf(c, s + CMDSIZE - c, "%s ", argv[optind]);
1628 c += snprintf(c, s + CMDSIZE - c, "\n");
1646 return (child(NULL));