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>
58 #include "cli_handlers.h"
62 #define FILE_NAME_SIZE 256
65 #define LOG_MSG(a,b) \
66 if (strlen(b)) condlog(a, "%s: %s - %s", pp->mpp->alias, pp->dev, b);
68 pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
69 pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
74 * global copy of vecs for use in sig handlers
76 struct vectors * gvecs;
79 need_switch_pathgroup (struct multipath * mpp, int refresh)
81 struct pathgroup * pgp;
85 if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
89 * Refresh path priority values
92 vector_foreach_slot (mpp->pg, pgp, i)
93 vector_foreach_slot (pgp->paths, pp, j)
94 pathinfo(pp, conf->hwtable, DI_PRIO);
96 mpp->bestpg = select_path_group(mpp);
98 if (mpp->bestpg != mpp->nextpg)
105 switch_pathgroup (struct multipath * mpp)
107 mpp->stat_switchgroup++;
108 dm_switchgroup(mpp->alias, mpp->bestpg);
109 condlog(2, "%s: switch to path group #%i",
110 mpp->alias, mpp->bestpg);
114 coalesce_maps(struct vectors *vecs, vector nmpv)
116 struct multipath * ompp;
117 vector ompv = vecs->mpvec;
121 vector_foreach_slot (ompv, ompp, i) {
122 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
124 * remove all current maps not allowed by the
125 * current configuration
127 if (dm_flush_map(ompp->alias)) {
128 condlog(0, "%s: unable to flush devmap",
131 * may be just because the device is open
133 if (!vector_alloc_slot(nmpv))
136 vector_set_slot(nmpv, ompp);
137 setup_multipath(vecs, ompp);
139 if ((j = find_slot(ompv, (void *)ompp)) != -1)
140 vector_del_slot(ompv, j);
146 condlog(2, "%s devmap removed", ompp->alias);
154 sync_map_state(struct multipath *mpp)
156 struct pathgroup *pgp;
163 vector_foreach_slot (mpp->pg, pgp, i){
164 vector_foreach_slot (pgp->paths, pp, j){
165 if (pp->state == PATH_UNCHECKED ||
166 pp->state == PATH_WILD)
168 if ((pp->dmstate == PSTATE_FAILED ||
169 pp->dmstate == PSTATE_UNDEF) &&
170 (pp->state == PATH_UP || pp->state == PATH_GHOST))
171 dm_reinstate_path(mpp->alias, pp->dev_t);
172 else if ((pp->dmstate == PSTATE_ACTIVE ||
173 pp->dmstate == PSTATE_UNDEF) &&
174 (pp->state == PATH_DOWN ||
175 pp->state == PATH_SHAKY))
176 dm_fail_path(mpp->alias, pp->dev_t);
182 sync_maps_state(vector mpvec)
185 struct multipath *mpp;
187 vector_foreach_slot (mpvec, mpp, i)
192 flush_map(struct multipath * mpp, struct vectors * vecs)
195 * clear references to this map before flushing so we can ignore
196 * the spurious uevent we may generate with the dm_flush_map call below
198 if (dm_flush_map(mpp->alias)) {
200 * May not really be an error -- if the map was already flushed
201 * from the device mapper by dmsetup(8) for instance.
203 condlog(0, "%s: can't flush", mpp->alias);
208 condlog(2, "%s: devmap removed", mpp->alias);
211 orphan_paths(vecs->pathvec, mpp);
212 remove_map_and_stop_waiter(mpp, vecs, 1);
218 uev_add_map (struct uevent * uev, struct vectors * vecs)
221 int major = -1, minor = -1, rc;
223 condlog(2, "%s: add map (uevent)", uev->kernel);
224 alias = uevent_get_dm_name(uev);
226 condlog(3, "%s: No DM_NAME in uevent", uev->kernel);
227 major = uevent_get_major(uev);
228 minor = uevent_get_minor(uev);
229 alias = dm_mapname(major, minor);
231 condlog(2, "%s: mapname not found for %d:%d",
232 uev->kernel, major, minor);
236 rc = ev_add_map(uev->kernel, alias, vecs);
242 ev_add_map (char * dev, char * alias, struct vectors * vecs)
245 struct multipath * mpp;
249 map_present = dm_map_present(alias);
251 if (map_present && dm_type(alias, TGT_MPATH) <= 0) {
252 condlog(4, "%s: not a multipath map", alias);
256 mpp = find_mp_by_alias(vecs->mpvec, alias);
260 * Not really an error -- we generate our own uevent
261 * if we create a multipath mapped device as a result
264 condlog(0, "%s: devmap already registered", dev);
269 * now we can register the map
271 if (map_present && (mpp = add_map_without_path(vecs, alias))) {
273 condlog(2, "%s: devmap %s registered", alias, dev);
276 refwwid = get_refwwid(dev, DEV_DEVMAP, vecs->pathvec);
279 r = coalesce_paths(vecs, NULL, refwwid, 0);
284 condlog(2, "%s: devmap %s added", alias, dev);
286 condlog(0, "%s: uev_add_map %s failed", alias, dev);
293 uev_remove_map (struct uevent * uev, struct vectors * vecs)
298 condlog(2, "%s: remove map (uevent)", uev->kernel);
299 alias = uevent_get_dm_name(uev);
301 condlog(3, "%s: No DM_NAME in uevent, ignoring", uev->kernel);
304 minor = uevent_get_minor(uev);
305 rc = ev_remove_map(uev->kernel, alias, minor, vecs);
311 ev_remove_map (char * devname, char * alias, int minor, struct vectors * vecs)
313 struct multipath * mpp;
315 mpp = find_mp_by_minor(vecs->mpvec, minor);
318 condlog(2, "%s: devmap not registered, can't remove",
322 if (strcmp(mpp->alias, alias)) {
323 condlog(2, "%s: minor number mismatch (map %d, event %d)",
324 mpp->alias, mpp->dmi->minor, minor);
327 return flush_map(mpp, vecs);
331 uev_umount_map (struct uevent * uev, struct vectors * vecs)
333 struct multipath * mpp;
335 condlog(2, "%s: umount map (uevent)", uev->kernel);
337 mpp = find_mp_by_str(vecs->mpvec, uev->kernel);
342 update_mpp_paths(mpp, vecs->pathvec);
343 verify_paths(mpp, vecs, NULL);
345 if (!VECTOR_SIZE(mpp->paths))
346 flush_map(mpp, vecs);
353 uev_add_path (struct uevent *uev, struct vectors * vecs)
355 struct sysfs_device * dev;
357 dev = sysfs_device_get(uev->devpath);
359 condlog(2, "%s: not found in sysfs", uev->devpath);
362 condlog(2, "%s: add path (uevent)", dev->kernel);
363 return (ev_add_path(dev->kernel, vecs) != 1)? 0 : 1;
373 ev_add_path (char * devname, struct vectors * vecs)
375 struct multipath * mpp;
377 char empty_buff[WWID_SIZE] = {0};
378 char params[PARAMS_SIZE] = {0};
380 if (strstr(devname, "..") != NULL) {
382 * Don't allow relative device names in the pathvec
384 condlog(0, "%s: path name is invalid", devname);
388 pp = find_path_by_dev(vecs->pathvec, devname);
391 condlog(0, "%s: spurious uevent, path already in pathvec",
398 * get path vital state
400 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
402 condlog(0, "%s: failed to store path info", devname);
405 pp->checkint = conf->checkint;
409 * need path UID to go any further
411 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
412 condlog(0, "%s: failed to get path uid", devname);
413 goto fail; /* leave path added to pathvec */
415 if (filter_path(conf, pp) > 0){
416 int i = find_slot(vecs->pathvec, (void *)pp);
418 vector_del_slot(vecs->pathvec, i);
422 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
425 if ((!pp->size) || (mpp->size != pp->size)) {
427 condlog(0, "%s: failed to add new path %s, "
431 condlog(0, "%s: failed to add new path %s, "
432 "device size mismatch",
434 int i = find_slot(vecs->pathvec, (void *)pp);
436 vector_del_slot(vecs->pathvec, i);
441 condlog(4,"%s: adopting all paths for path %s",
442 mpp->alias, pp->dev);
443 if (adopt_paths(vecs->pathvec, mpp, 1))
444 goto fail; /* leave path added to pathvec */
446 verify_paths(mpp, vecs, NULL);
447 mpp->flush_on_last_del = FLUSH_UNDEF;
448 mpp->action = ACT_RELOAD;
452 condlog(0, "%s: failed to create new map,"
453 " %s device size is 0 ", devname, pp->dev);
454 int i = find_slot(vecs->pathvec, (void *)pp);
456 vector_del_slot(vecs->pathvec, i);
461 condlog(4,"%s: creating new map", pp->dev);
462 if ((mpp = add_map_with_path(vecs, pp, 1)))
463 mpp->action = ACT_CREATE;
465 goto fail; /* leave path added to pathvec */
469 * push the map to the device-mapper
471 if (setup_map(mpp, params, PARAMS_SIZE)) {
472 condlog(0, "%s: failed to setup map for addition of new "
473 "path %s", mpp->alias, devname);
477 * reload the map for the multipath mapped device
479 if (domap(mpp, params) <= 0) {
480 condlog(0, "%s: failed in domap for addition of new "
481 "path %s", mpp->alias, devname);
483 * deal with asynchronous uevents :((
485 if (mpp->action == ACT_RELOAD) {
486 condlog(0, "%s: uev_add_path sleep", mpp->alias);
488 update_mpp_paths(mpp, vecs->pathvec);
497 * update our state from kernel regardless of create or reload
499 if (setup_multipath(vecs, mpp))
504 if (mpp->action == ACT_CREATE &&
505 start_waiter_thread(mpp, vecs))
508 condlog(2, "%s path added to devmap %s", devname, mpp->alias);
512 remove_map(mpp, vecs, 1);
519 uev_remove_path (struct uevent *uev, struct vectors * vecs)
521 struct sysfs_device * dev;
524 dev = sysfs_device_get(uev->devpath);
526 condlog(2, "%s: not found in sysfs", uev->devpath);
529 condlog(2, "%s: remove path (uevent)", uev->kernel);
530 retval = ev_remove_path(uev->kernel, vecs);
533 sysfs_device_put(dev);
539 ev_remove_path (char * devname, struct vectors * vecs)
541 struct multipath * mpp;
544 char params[PARAMS_SIZE] = {0};
546 pp = find_path_by_dev(vecs->pathvec, devname);
549 /* Not an error; path might have been purged earlier */
550 condlog(0, "%s: path already removed", devname);
555 * avoid referring to the map of an orphaned path
557 if ((mpp = pp->mpp)) {
559 * transform the mp->pg vector of vectors of paths
560 * into a mp->params string to feed the device-mapper
562 if (update_mpp_paths(mpp, vecs->pathvec)) {
563 condlog(0, "%s: failed to update paths",
567 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
568 vector_del_slot(mpp->paths, i);
571 * remove the map IFF removing the last path
573 if (VECTOR_SIZE(mpp->paths) == 0) {
574 char alias[WWID_SIZE];
577 * flush_map will fail if the device is open
579 strncpy(alias, mpp->alias, WWID_SIZE);
580 if (mpp->flush_on_last_del == FLUSH_ENABLED) {
581 condlog(2, "%s Last path deleted, disabling queueing", mpp->alias);
583 mpp->no_path_retry = NO_PATH_RETRY_FAIL;
584 mpp->flush_on_last_del = FLUSH_IN_PROGRESS;
585 dm_queue_if_no_path(mpp->alias, 0);
587 if (!flush_map(mpp, vecs)) {
588 condlog(2, "%s: removed map after"
589 " removing all paths",
595 * Not an error, continue
599 if (setup_map(mpp, params, PARAMS_SIZE)) {
600 condlog(0, "%s: failed to setup map for"
601 " removal of path %s", mpp->alias,
608 mpp->action = ACT_RELOAD;
609 if (domap(mpp, params) <= 0) {
610 condlog(0, "%s: failed in domap for "
611 "removal of path %s",
612 mpp->alias, devname);
616 * update our state from kernel
618 if (setup_multipath(vecs, mpp)) {
623 condlog(2, "%s: path removed from map %s",
624 devname, mpp->alias);
629 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
630 vector_del_slot(vecs->pathvec, i);
637 remove_map_and_stop_waiter(mpp, vecs, 1);
642 map_discovery (struct vectors * vecs)
644 struct multipath * mpp;
647 if (dm_get_maps(vecs->mpvec))
650 vector_foreach_slot (vecs->mpvec, mpp, i)
651 if (setup_multipath(vecs, mpp))
658 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
660 struct vectors * vecs;
665 vecs = (struct vectors *)trigger_data;
667 pthread_cleanup_push(cleanup_lock, &vecs->lock);
670 r = parse_cmd(str, reply, len, vecs);
673 *reply = STRDUP("fail\n");
674 *len = strlen(*reply) + 1;
677 else if (!r && *len == 0) {
678 *reply = STRDUP("ok\n");
679 *len = strlen(*reply) + 1;
682 /* else if (r < 0) leave *reply alone */
684 lock_cleanup_pop(vecs->lock);
689 uev_discard(char * devpath)
695 * keep only block devices, discard partitions
697 tmp = strstr(devpath, "/block/");
699 condlog(4, "no /block/ in '%s'", devpath);
702 if (sscanf(tmp, "/block/%10s", a) != 1 ||
703 sscanf(tmp, "/block/%10[^/]/%10s", a, b) == 2) {
704 condlog(4, "discard event on %s", devpath);
711 uev_trigger (struct uevent * uev, void * trigger_data)
714 struct vectors * vecs;
716 vecs = (struct vectors *)trigger_data;
718 if (uev_discard(uev->devpath))
725 * Add events are ignored here as the tables
726 * are not fully initialised then.
728 if (!strncmp(uev->kernel, "dm-", 3)) {
729 if (!strncmp(uev->action, "change", 6)) {
730 r = uev_add_map(uev, vecs);
733 if (!strncmp(uev->action, "remove", 6)) {
734 r = uev_remove_map(uev, vecs);
737 if (!strncmp(uev->action, "umount", 6)) {
738 r = uev_umount_map(uev, vecs);
745 * path add/remove event
747 if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
751 if (!strncmp(uev->action, "add", 3)) {
752 r = uev_add_path(uev, vecs);
755 if (!strncmp(uev->action, "remove", 6)) {
756 r = uev_remove_path(uev, vecs);
766 ueventloop (void * ap)
768 block_signal(SIGUSR1, NULL);
769 block_signal(SIGHUP, NULL);
772 condlog(0, "error starting uevent listener");
780 block_signal(SIGUSR1, NULL);
781 block_signal(SIGHUP, NULL);
783 if (uevent_dispatch(&uev_trigger, ap))
784 condlog(0, "error starting uevent dispatcher");
789 uxlsnrloop (void * ap)
791 block_signal(SIGUSR1, NULL);
792 block_signal(SIGHUP, NULL);
797 set_handler_callback(LIST+PATHS, cli_list_paths);
798 set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt);
799 set_handler_callback(LIST+MAPS, cli_list_maps);
800 set_handler_callback(LIST+STATUS, cli_list_status);
801 set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
802 set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
803 set_handler_callback(LIST+MAPS+FMT, cli_list_maps_fmt);
804 set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
805 set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
806 set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
807 set_handler_callback(LIST+CONFIG, cli_list_config);
808 set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
809 set_handler_callback(LIST+DEVICES, cli_list_devices);
810 set_handler_callback(LIST+WILDCARDS, cli_list_wildcards);
811 set_handler_callback(ADD+PATH, cli_add_path);
812 set_handler_callback(DEL+PATH, cli_del_path);
813 set_handler_callback(ADD+MAP, cli_add_map);
814 set_handler_callback(DEL+MAP, cli_del_map);
815 set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
816 set_handler_callback(RECONFIGURE, cli_reconfigure);
817 set_handler_callback(SUSPEND+MAP, cli_suspend);
818 set_handler_callback(RESUME+MAP, cli_resume);
819 set_handler_callback(RESIZE+MAP, cli_resize);
820 set_handler_callback(REINSTATE+PATH, cli_reinstate);
821 set_handler_callback(FAIL+PATH, cli_fail);
822 set_handler_callback(DISABLEQ+MAP, cli_disable_queueing);
823 set_handler_callback(RESTOREQ+MAP, cli_restore_queueing);
824 set_handler_callback(DISABLEQ+MAPS, cli_disable_all_queueing);
825 set_handler_callback(RESTOREQ+MAPS, cli_restore_all_queueing);
826 set_handler_callback(QUIT, cli_quit);
829 uxsock_listen(&uxsock_trigger, ap);
835 exit_daemon (int status)
838 fprintf(stderr, "bad exit status. see daemon.log\n");
840 condlog(3, "unlink pidfile");
841 unlink(DEFAULT_PIDFILE);
843 pthread_mutex_lock(&exit_mutex);
844 pthread_cond_signal(&exit_cond);
845 pthread_mutex_unlock(&exit_mutex);
851 fail_path (struct path * pp, int del_active)
856 condlog(2, "checker failed path %s in map %s",
857 pp->dev_t, pp->mpp->alias);
859 dm_fail_path(pp->mpp->alias, pp->dev_t);
861 update_queue_mode_del_path(pp->mpp);
865 * caller must have locked the path list before calling that function
868 reinstate_path (struct path * pp, int add_active)
873 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
874 condlog(0, "%s: reinstate failed", pp->dev_t);
876 condlog(2, "%s: reinstated", pp->dev_t);
878 update_queue_mode_add_path(pp->mpp);
883 enable_group(struct path * pp)
885 struct pathgroup * pgp;
888 * if path is added through uev_add_path, pgindex can be unset.
889 * next update_strings() will set it, upon map reload event.
891 * we can safely return here, because upon map reload, all
892 * PG will be enabled.
894 if (!pp->mpp->pg || !pp->pgindex)
897 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
899 if (pgp->status == PGSTATE_DISABLED) {
900 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
901 dm_enablegroup(pp->mpp->alias, pp->pgindex);
906 mpvec_garbage_collector (struct vectors * vecs)
908 struct multipath * mpp;
914 vector_foreach_slot (vecs->mpvec, mpp, i) {
915 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
916 condlog(2, "%s: remove dead map", mpp->alias);
917 remove_map_and_stop_waiter(mpp, vecs, 1);
924 defered_failback_tick (vector mpvec)
926 struct multipath * mpp;
929 vector_foreach_slot (mpvec, mpp, i) {
931 * defered failback getting sooner
933 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
934 mpp->failback_tick--;
936 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
937 switch_pathgroup(mpp);
943 retry_count_tick(vector mpvec)
945 struct multipath *mpp;
948 vector_foreach_slot (mpvec, mpp, i) {
949 if (mpp->retry_tick) {
950 mpp->stat_total_queueing_time++;
951 condlog(4, "%s: Retrying.. No active path", mpp->alias);
952 if(--mpp->retry_tick == 0) {
953 dm_queue_if_no_path(mpp->alias, 0);
954 condlog(2, "%s: Disable queueing", mpp->alias);
960 int update_prio(struct path *pp, int refresh_all)
964 struct pathgroup * pgp;
965 int i, j, changed = 0;
968 vector_foreach_slot (pp->mpp->pg, pgp, i) {
969 vector_foreach_slot (pgp->paths, pp1, j) {
970 oldpriority = pp1->priority;
971 pathinfo(pp1, conf->hwtable, DI_PRIO);
972 if (pp1->priority != oldpriority)
978 oldpriority = pp->priority;
979 pathinfo(pp, conf->hwtable, DI_PRIO);
981 if (pp->priority == oldpriority)
986 int update_path_groups(struct multipath *mpp, struct vectors *vecs, int refresh)
990 char params[PARAMS_SIZE];
992 update_mpp_paths(mpp, vecs->pathvec);
994 vector_foreach_slot (mpp->paths, pp, i)
995 pathinfo(pp, conf->hwtable, DI_PRIO);
998 if (setup_map(mpp, params, PARAMS_SIZE))
1001 mpp->action = ACT_RELOAD;
1002 if (domap(mpp, params) <= 0) {
1003 condlog(0, "%s: failed to update map : %s", mpp->alias,
1008 setup_multipath(vecs, mpp);
1009 sync_map_state(mpp);
1015 check_path (struct vectors * vecs, struct path * pp)
1018 int new_path_up = 0;
1023 if (pp->tick && --pp->tick)
1024 return; /* don't check this path yet */
1027 * provision a next check soonest,
1028 * in case we exit abnormaly from here
1030 pp->tick = conf->checkint;
1032 newstate = get_state(pp, 1);
1034 if (newstate == PATH_WILD || newstate == PATH_UNCHECKED) {
1035 condlog(2, "%s: unusable path", pp->dev);
1036 pathinfo(pp, conf->hwtable, 0);
1040 * Async IO in flight. Keep the previous path state
1041 * and reschedule as soon as possible
1043 if (newstate == PATH_PENDING) {
1047 if (newstate != pp->state) {
1048 int oldstate = pp->state;
1049 pp->state = newstate;
1050 LOG_MSG(1, checker_message(&pp->checker));
1053 * upon state change, reset the checkint
1054 * to the shortest delay
1056 pp->checkint = conf->checkint;
1058 if (newstate == PATH_DOWN || newstate == PATH_SHAKY ||
1059 update_multipath_strings(pp->mpp, vecs->pathvec)) {
1061 * proactively fail path in the DM
1063 if (oldstate == PATH_UP ||
1064 oldstate == PATH_GHOST)
1070 * cancel scheduled failback
1072 pp->mpp->failback_tick = 0;
1074 pp->mpp->stat_path_failures++;
1079 * reinstate this path
1081 if (oldstate != PATH_UP &&
1082 oldstate != PATH_GHOST)
1083 reinstate_path(pp, 1);
1085 reinstate_path(pp, 0);
1090 * if at least one path is up in a group, and
1091 * the group is disabled, re-enable it
1093 if (newstate == PATH_UP)
1096 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1097 if (pp->dmstate == PSTATE_FAILED ||
1098 pp->dmstate == PSTATE_UNDEF) {
1099 /* Clear IO errors */
1100 reinstate_path(pp, 0);
1102 LOG_MSG(4, checker_message(&pp->checker));
1104 * double the next check delay.
1105 * max at conf->max_checkint
1107 if (pp->checkint < (conf->max_checkint / 2))
1108 pp->checkint = 2 * pp->checkint;
1110 pp->checkint = conf->max_checkint;
1112 pp->tick = pp->checkint;
1113 condlog(4, "%s: delay next check %is",
1114 pp->dev_t, pp->tick);
1117 else if (newstate == PATH_DOWN)
1118 LOG_MSG(2, checker_message(&pp->checker));
1120 pp->state = newstate;
1123 * path prio refreshing
1125 condlog(4, "path prio refresh");
1127 if (update_prio(pp, new_path_up) &&
1128 pp->mpp->pgpolicyfn == (pgpolicyfn *)group_by_prio)
1129 update_path_groups(pp->mpp, vecs, !new_path_up);
1130 else if (need_switch_pathgroup(pp->mpp, 0)) {
1131 if (pp->mpp->pgfailback > 0 &&
1132 (new_path_up || pp->mpp->failback_tick <= 0))
1133 pp->mpp->failback_tick =
1134 pp->mpp->pgfailback + 1;
1135 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE)
1136 switch_pathgroup(pp->mpp);
1141 checkerloop (void *ap)
1143 struct vectors *vecs;
1149 mlockall(MCL_CURRENT | MCL_FUTURE);
1150 vecs = (struct vectors *)ap;
1151 condlog(2, "path checkers start up");
1154 * init the path check interval
1156 vector_foreach_slot (vecs->pathvec, pp, i) {
1157 pp->checkint = conf->checkint;
1161 block_signal(SIGHUP, &old);
1162 pthread_cleanup_push(cleanup_lock, &vecs->lock);
1166 if (vecs->pathvec) {
1167 vector_foreach_slot (vecs->pathvec, pp, i) {
1168 check_path(vecs, pp);
1172 defered_failback_tick(vecs->mpvec);
1173 retry_count_tick(vecs->mpvec);
1178 condlog(4, "map garbage collection");
1179 mpvec_garbage_collector(vecs);
1183 lock_cleanup_pop(vecs->lock);
1184 pthread_sigmask(SIG_SETMASK, &old, NULL);
1191 configure (struct vectors * vecs, int start_waiters)
1193 struct multipath * mpp;
1198 if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
1201 if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
1204 if (!(mpvec = vector_alloc()))
1208 * probe for current path (from sysfs) and map (from dm) sets
1210 path_discovery(vecs->pathvec, conf, DI_ALL);
1212 vector_foreach_slot (vecs->pathvec, pp, i){
1213 if (filter_path(conf, pp) > 0){
1214 vector_del_slot(vecs->pathvec, i);
1219 pp->checkint = conf->checkint;
1221 if (map_discovery(vecs))
1225 * create new set of maps & push changed ones into dm
1227 if (coalesce_paths(vecs, mpvec, NULL, 1))
1231 * may need to remove some maps which are no longer relevant
1232 * e.g., due to blacklist changes in conf file
1234 if (coalesce_maps(vecs, mpvec))
1239 sync_maps_state(mpvec);
1242 * purge dm of old maps
1247 * save new set of maps formed by considering current path state
1249 vector_free(vecs->mpvec);
1250 vecs->mpvec = mpvec;
1253 * start dm event waiter threads for these new maps
1255 vector_foreach_slot(vecs->mpvec, mpp, i) {
1256 if (setup_multipath(vecs, mpp))
1259 if (start_waiter_thread(mpp, vecs))
1266 reconfigure (struct vectors * vecs)
1268 struct config * old = conf;
1271 * free old map and path vectors ... they use old conf state
1273 if (VECTOR_SIZE(vecs->mpvec))
1274 remove_maps_and_stop_waiters(vecs);
1276 if (VECTOR_SIZE(vecs->pathvec))
1277 free_pathvec(vecs->pathvec, FREE_PATHS);
1279 vecs->pathvec = NULL;
1282 if (load_config(DEFAULT_CONFIGFILE))
1285 conf->verbosity = old->verbosity;
1287 if (!conf->checkint) {
1288 conf->checkint = DEFAULT_CHECKINT;
1289 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1297 static struct vectors *
1300 struct vectors * vecs;
1302 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1308 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1310 if (!vecs->lock.mutex)
1313 pthread_mutex_init(vecs->lock.mutex, NULL);
1314 vecs->lock.depth = 0;
1320 condlog(0, "failed to init paths");
1325 signal_set(int signo, void (*func) (int))
1328 struct sigaction sig;
1329 struct sigaction osig;
1331 sig.sa_handler = func;
1332 sigemptyset(&sig.sa_mask);
1335 r = sigaction(signo, &sig, &osig);
1340 return (osig.sa_handler);
1346 condlog(2, "reconfigure (SIGHUP)");
1350 unlock(gvecs->lock);
1353 dbg_free_final(NULL);
1366 condlog(3, "SIGUSR1 received");
1372 signal_set(SIGHUP, sighup);
1373 signal_set(SIGUSR1, sigusr1);
1374 signal_set(SIGINT, sigend);
1375 signal_set(SIGTERM, sigend);
1376 signal(SIGPIPE, SIG_IGN);
1383 static struct sched_param sched_param = {
1384 .sched_priority = 99
1387 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1390 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1395 set_oom_adj (int val)
1399 fp = fopen("/proc/self/oom_adj", "w");
1404 fprintf(fp, "%i", val);
1409 child (void * param)
1411 pthread_t check_thr, uevent_thr, uxlsnr_thr, uevq_thr;
1412 pthread_attr_t log_attr, misc_attr;
1413 struct vectors * vecs;
1414 struct multipath * mpp;
1418 mlockall(MCL_CURRENT | MCL_FUTURE);
1420 setup_thread_attr(&misc_attr, 64 * 1024, 1);
1421 setup_thread_attr(&waiter_attr, 32 * 1024, 1);
1424 setup_thread_attr(&log_attr, 64 * 1024, 0);
1425 log_thread_start(&log_attr);
1426 pthread_attr_destroy(&log_attr);
1429 condlog(2, "--------start up--------");
1430 condlog(2, "read " DEFAULT_CONFIGFILE);
1432 if (load_config(DEFAULT_CONFIGFILE))
1435 if (init_checkers()) {
1436 condlog(0, "failed to initialize checkers");
1440 condlog(0, "failed to initialize prioritizers");
1444 setlogmask(LOG_UPTO(conf->verbosity + 3));
1447 * fill the voids left in the config file
1449 if (!conf->checkint) {
1450 conf->checkint = DEFAULT_CHECKINT;
1451 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1454 if (conf->max_fds) {
1455 struct rlimit fd_limit;
1457 fd_limit.rlim_cur = conf->max_fds;
1458 fd_limit.rlim_max = conf->max_fds;
1459 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0)
1460 condlog(0, "can't set open fds limit to %d : %s\n",
1461 conf->max_fds, strerror(errno));
1464 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1473 vecs = gvecs = init_vecs();
1478 if (sysfs_init(conf->sysfs_dir, FILE_NAME_SIZE)) {
1479 condlog(0, "can not find sysfs mount point");
1483 udev_set_sync_support(0);
1485 * Start uevent listener early to catch events
1487 if ((rc = pthread_create(&uevent_thr, &misc_attr, ueventloop, vecs))) {
1488 condlog(0, "failed to create uevent thread: %d", rc);
1492 * fetch and configure both paths and multipaths
1495 if (configure(vecs, 1)) {
1497 condlog(0, "failure during configuration");
1505 if ((rc = pthread_create(&check_thr, &misc_attr, checkerloop, vecs))) {
1506 condlog(0,"failed to create checker loop thread: %d", rc);
1509 if ((rc = pthread_create(&uxlsnr_thr, &misc_attr, uxlsnrloop, vecs))) {
1510 condlog(0, "failed to create cli listener: %d", rc);
1513 if ((rc = pthread_create(&uevq_thr, &misc_attr, uevqloop, vecs))) {
1514 condlog(0, "failed to create uevent dispatcher: %d", rc);
1517 pthread_attr_destroy(&misc_attr);
1519 pthread_mutex_lock(&exit_mutex);
1520 pthread_cond_wait(&exit_cond, &exit_mutex);
1525 block_signal(SIGHUP, NULL);
1527 if (conf->queue_without_daemon == QUE_NO_DAEMON_OFF)
1528 vector_foreach_slot(vecs->mpvec, mpp, i)
1529 dm_queue_if_no_path(mpp->alias, 0);
1530 remove_maps_and_stop_waiters(vecs);
1531 free_pathvec(vecs->pathvec, FREE_PATHS);
1533 pthread_cancel(check_thr);
1534 pthread_cancel(uevent_thr);
1535 pthread_cancel(uxlsnr_thr);
1536 pthread_cancel(uevq_thr);
1542 free_handlers(handlers);
1547 /* Now all the waitevent threads will start rushing in. */
1548 while (vecs->lock.depth > 0) {
1549 sleep (1); /* This is weak. */
1550 condlog(3,"Have %d wait event checkers threads to de-alloc, waiting..\n", vecs->lock.depth);
1552 pthread_mutex_destroy(vecs->lock.mutex);
1553 FREE(vecs->lock.mutex);
1554 vecs->lock.depth = 0;
1555 vecs->lock.mutex = NULL;
1562 condlog(2, "--------shut down-------");
1573 * Freeing config must be done after condlog() and dm_lib_exit(),
1574 * because logging functions like dlog() and dm_write_log()
1575 * reference the config.
1581 dbg_free_final(NULL);
1593 if( (pid = fork()) < 0){
1594 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1602 if ( (pid = fork()) < 0)
1603 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1607 in_fd = open("/dev/null", O_RDONLY);
1609 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1613 out_fd = open("/dev/console", O_WRONLY);
1615 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1620 close(STDIN_FILENO);
1622 close(STDOUT_FILENO);
1624 close(STDERR_FILENO);
1630 fprintf(stderr, "cannot chdir to '/', continuing\n");
1636 main (int argc, char *argv[])
1638 extern char *optarg;
1646 if (getuid() != 0) {
1647 fprintf(stderr, "need to be root\n");
1651 /* make sure we don't lock any path */
1653 umask(umask(077) | 022);
1655 conf = alloc_config();
1660 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1664 //debug=1; /* ### comment me out ### */
1667 if (sizeof(optarg) > sizeof(char *) ||
1668 !isdigit(optarg[0]))
1671 conf->verbosity = atoi(optarg);
1680 if (optind < argc) {
1685 while (optind < argc) {
1686 if (strchr(argv[optind], ' '))
1687 c += snprintf(c, s + CMDSIZE - c, "\"%s\" ", argv[optind]);
1689 c += snprintf(c, s + CMDSIZE - c, "%s ", argv[optind]);
1692 c += snprintf(c, s + CMDSIZE - c, "\n");
1710 return (child(NULL));