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>
56 #include "cli_handlers.h"
60 #define FILE_NAME_SIZE 256
63 #define LOG_MSG(a,b) \
64 if (strlen(b)) condlog(a, "%s: %s", pp->dev, b);
66 pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
67 pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
72 * global copy of vecs for use in sig handlers
74 struct vectors * gvecs;
77 need_switch_pathgroup (struct multipath * mpp, int refresh)
79 struct pathgroup * pgp;
83 if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
87 * Refresh path priority values
90 vector_foreach_slot (mpp->pg, pgp, i)
91 vector_foreach_slot (pgp->paths, pp, j)
92 pathinfo(pp, conf->hwtable, DI_PRIO);
94 mpp->bestpg = select_path_group(mpp);
96 if (mpp->bestpg != mpp->nextpg)
103 switch_pathgroup (struct multipath * mpp)
105 mpp->stat_switchgroup++;
106 dm_switchgroup(mpp->alias, mpp->bestpg);
107 condlog(2, "%s: switch to path group #%i",
108 mpp->alias, mpp->bestpg);
112 coalesce_maps(struct vectors *vecs, vector nmpv)
114 struct multipath * ompp;
115 vector ompv = vecs->mpvec;
119 vector_foreach_slot (ompv, ompp, i) {
120 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
122 * remove all current maps not allowed by the
123 * current configuration
125 if (dm_flush_map(ompp->alias)) {
126 condlog(0, "%s: unable to flush devmap",
129 * may be just because the device is open
131 if (!vector_alloc_slot(nmpv))
134 vector_set_slot(nmpv, ompp);
135 setup_multipath(vecs, ompp);
137 if ((j = find_slot(ompv, (void *)ompp)) != -1)
138 vector_del_slot(ompv, j);
144 condlog(2, "%s devmap removed", ompp->alias);
152 sync_map_state(struct multipath *mpp)
154 struct pathgroup *pgp;
161 vector_foreach_slot (mpp->pg, pgp, i){
162 vector_foreach_slot (pgp->paths, pp, j){
163 if (pp->state == PATH_UNCHECKED ||
164 pp->state == PATH_WILD)
166 if ((pp->dmstate == PSTATE_FAILED ||
167 pp->dmstate == PSTATE_UNDEF) &&
168 (pp->state == PATH_UP || pp->state == PATH_GHOST))
169 dm_reinstate_path(mpp->alias, pp->dev_t);
170 else if ((pp->dmstate == PSTATE_ACTIVE ||
171 pp->dmstate == PSTATE_UNDEF) &&
172 (pp->state == PATH_DOWN ||
173 pp->state == PATH_SHAKY))
174 dm_fail_path(mpp->alias, pp->dev_t);
180 sync_maps_state(vector mpvec)
183 struct multipath *mpp;
185 vector_foreach_slot (mpvec, mpp, i)
190 flush_map(struct multipath * mpp, struct vectors * vecs)
193 * clear references to this map before flushing so we can ignore
194 * the spurious uevent we may generate with the dm_flush_map call below
196 if (dm_flush_map(mpp->alias)) {
198 * May not really be an error -- if the map was already flushed
199 * from the device mapper by dmsetup(8) for instance.
201 condlog(0, "%s: can't flush", mpp->alias);
206 condlog(2, "%s: devmap removed", mpp->alias);
209 orphan_paths(vecs->pathvec, mpp);
210 remove_map_and_stop_waiter(mpp, vecs, 1);
216 uev_add_map (struct sysfs_device * dev, struct vectors * vecs)
218 condlog(2, "%s: add map (uevent)", dev->kernel);
219 return ev_add_map(dev, vecs);
223 ev_add_map (struct sysfs_device * dev, struct vectors * vecs)
229 struct multipath * mpp;
233 dev_t = sysfs_attr_get_value(dev->devpath, "dev");
235 if (!dev_t || sscanf(dev_t, "%d:%d", &major, &minor) != 2)
238 alias = dm_mapname(major, minor);
243 map_present = dm_map_present(alias);
245 if (map_present && dm_type(alias, TGT_MPATH) <= 0) {
246 condlog(4, "%s: not a multipath map", alias);
251 mpp = find_mp_by_alias(vecs->mpvec, alias);
255 * Not really an error -- we generate our own uevent
256 * if we create a multipath mapped device as a result
259 condlog(0, "%s: devmap already registered",
266 * now we can register the map
268 if (map_present && (mpp = add_map_without_path(vecs, minor, alias))) {
270 condlog(2, "%s: devmap %s added", alias, dev->kernel);
273 refwwid = get_refwwid(dev->kernel, DEV_DEVMAP, vecs->pathvec);
276 r = coalesce_paths(vecs, NULL, refwwid, 0);
281 condlog(2, "%s: devmap %s added", alias, dev->kernel);
283 condlog(0, "%s: uev_add_map %s failed", alias, dev->kernel);
291 uev_remove_map (struct sysfs_device * dev, struct vectors * vecs)
293 condlog(2, "%s: remove map (uevent)", dev->kernel);
294 return ev_remove_map(dev->kernel, vecs);
298 ev_remove_map (char * devname, struct vectors * vecs)
300 struct multipath * mpp;
302 mpp = find_mp_by_str(vecs->mpvec, devname);
305 condlog(2, "%s: devmap not registered, can't remove",
309 flush_map(mpp, vecs);
315 uev_umount_map (struct sysfs_device * dev, struct vectors * vecs)
317 struct multipath * mpp;
319 condlog(2, "%s: umount map (uevent)", dev->kernel);
321 mpp = find_mp_by_str(vecs->mpvec, dev->kernel);
326 update_mpp_paths(mpp, vecs->pathvec);
327 verify_paths(mpp, vecs, NULL);
329 if (!VECTOR_SIZE(mpp->paths))
330 flush_map(mpp, vecs);
336 uev_add_path (struct sysfs_device * dev, struct vectors * vecs)
338 condlog(2, "%s: add path (uevent)", dev->kernel);
339 return (ev_add_path(dev->kernel, vecs) != 1)? 0 : 1;
350 ev_add_path (char * devname, struct vectors * vecs)
352 struct multipath * mpp;
354 char empty_buff[WWID_SIZE] = {0};
356 if (strstr(devname, "..") != NULL) {
358 * Don't allow relative device names in the pathvec
360 condlog(0, "%s: path name is invalid", devname);
364 pp = find_path_by_dev(vecs->pathvec, devname);
367 condlog(0, "%s: spurious uevent, path already in pathvec",
374 * get path vital state
376 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
378 condlog(0, "%s: failed to store path info", devname);
381 pp->checkint = conf->checkint;
385 * need path UID to go any further
387 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
388 condlog(0, "%s: failed to get path uid", devname);
389 return 1; /* leave path added to pathvec */
391 if (filter_path(conf, pp) > 0){
392 int i = find_slot(vecs->pathvec, (void *)pp);
394 vector_del_slot(vecs->pathvec, i);
398 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
401 if ((!pp->size) || (mpp->size != pp->size)) {
403 condlog(0, "%s: failed to add new path %s, "
407 condlog(0, "%s: failed to add new path %s, "
408 "device size mismatch",
410 int i = find_slot(vecs->pathvec, (void *)pp);
412 vector_del_slot(vecs->pathvec, i);
417 condlog(4,"%s: adopting all paths for path %s",
418 mpp->alias, pp->dev);
419 if (adopt_paths(vecs->pathvec, mpp))
420 return 1; /* leave path added to pathvec */
422 verify_paths(mpp, vecs, NULL);
423 mpp->flush_on_last_del = FLUSH_UNDEF;
424 mpp->action = ACT_RELOAD;
428 condlog(0, "%s: failed to create new map,"
429 " %s device size is 0 ", devname, pp->dev);
430 int i = find_slot(vecs->pathvec, (void *)pp);
432 vector_del_slot(vecs->pathvec, i);
437 condlog(4,"%s: creating new map", pp->dev);
438 if ((mpp = add_map_with_path(vecs, pp, 1)))
439 mpp->action = ACT_CREATE;
441 return 1; /* leave path added to pathvec */
445 * push the map to the device-mapper
447 if (setup_map(mpp)) {
448 condlog(0, "%s: failed to setup map for addition of new "
449 "path %s", mpp->alias, devname);
453 * reload the map for the multipath mapped device
455 if (domap(mpp) <= 0) {
456 condlog(0, "%s: failed in domap for addition of new "
457 "path %s", mpp->alias, devname);
459 * deal with asynchronous uevents :((
461 if (mpp->action == ACT_RELOAD) {
462 condlog(0, "%s: uev_add_path sleep", mpp->alias);
464 update_mpp_paths(mpp, vecs->pathvec);
473 * update our state from kernel regardless of create or reload
475 if (setup_multipath(vecs, mpp))
480 if (mpp->action == ACT_CREATE &&
481 start_waiter_thread(mpp, vecs))
484 condlog(2, "%s path added to devmap %s", devname, mpp->alias);
488 remove_map(mpp, vecs, 1);
493 uev_remove_path (struct sysfs_device * dev, struct vectors * vecs)
497 condlog(2, "%s: remove path (uevent)", dev->kernel);
498 retval = ev_remove_path(dev->kernel, vecs);
500 sysfs_device_put(dev);
506 ev_remove_path (char * devname, struct vectors * vecs)
508 struct multipath * mpp;
512 pp = find_path_by_dev(vecs->pathvec, devname);
515 /* Not an error; path might have been purged earlier */
516 condlog(0, "%s: path already removed", devname);
521 * avoid referring to the map of an orphaned path
523 if ((mpp = pp->mpp)) {
525 * transform the mp->pg vector of vectors of paths
526 * into a mp->params string to feed the device-mapper
528 if (update_mpp_paths(mpp, vecs->pathvec)) {
529 condlog(0, "%s: failed to update paths",
533 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
534 vector_del_slot(mpp->paths, i);
537 * remove the map IFF removing the last path
539 if (VECTOR_SIZE(mpp->paths) == 0) {
540 char alias[WWID_SIZE];
543 * flush_map will fail if the device is open
545 strncpy(alias, mpp->alias, WWID_SIZE);
546 if (mpp->flush_on_last_del == FLUSH_ENABLED) {
547 condlog(2, "%s Last path deleted, disabling queueing", mpp->alias);
549 mpp->no_path_retry = NO_PATH_RETRY_FAIL;
550 mpp->flush_on_last_del = FLUSH_IN_PROGRESS;
551 dm_queue_if_no_path(mpp->alias, 0);
553 if (!flush_map(mpp, vecs)) {
554 condlog(2, "%s: removed map after"
555 " removing all paths",
561 * Not an error, continue
565 if (setup_map(mpp)) {
566 condlog(0, "%s: failed to setup map for"
567 " removal of path %s", mpp->alias,
574 mpp->action = ACT_RELOAD;
575 if (domap(mpp) <= 0) {
576 condlog(0, "%s: failed in domap for "
577 "removal of path %s",
578 mpp->alias, devname);
582 * update our state from kernel
584 if (setup_multipath(vecs, mpp)) {
589 condlog(2, "%s: path removed from map %s",
590 devname, mpp->alias);
595 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
596 vector_del_slot(vecs->pathvec, i);
603 remove_map_and_stop_waiter(mpp, vecs, 1);
608 map_discovery (struct vectors * vecs)
610 struct multipath * mpp;
613 if (dm_get_maps(vecs->mpvec))
616 vector_foreach_slot (vecs->mpvec, mpp, i)
617 if (setup_multipath(vecs, mpp))
624 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
626 struct vectors * vecs;
631 vecs = (struct vectors *)trigger_data;
633 pthread_cleanup_push(cleanup_lock, &vecs->lock);
636 r = parse_cmd(str, reply, len, vecs);
639 *reply = STRDUP("fail\n");
640 *len = strlen(*reply) + 1;
643 else if (!r && *len == 0) {
644 *reply = STRDUP("ok\n");
645 *len = strlen(*reply) + 1;
648 /* else if (r < 0) leave *reply alone */
650 lock_cleanup_pop(vecs->lock);
655 uev_discard(char * devpath)
661 * keep only block devices, discard partitions
663 tmp = strstr(devpath, "/block/");
665 condlog(4, "no /block/ in '%s'", devpath);
668 if (sscanf(tmp, "/block/%10s", a) != 1 ||
669 sscanf(tmp, "/block/%10[^/]/%10s", a, b) == 2) {
670 condlog(4, "discard event on %s", devpath);
677 uev_trigger (struct uevent * uev, void * trigger_data)
680 struct sysfs_device *sysdev;
681 struct vectors * vecs;
683 vecs = (struct vectors *)trigger_data;
685 if (uev_discard(uev->devpath))
688 sysdev = sysfs_device_get(uev->devpath);
696 * Add events are ignored here as the tables
697 * are not fully initialised then.
699 if (!strncmp(sysdev->kernel, "dm-", 3)) {
700 if (!strncmp(uev->action, "change", 6)) {
701 r = uev_add_map(sysdev, vecs);
704 if (!strncmp(uev->action, "remove", 6)) {
705 r = uev_remove_map(sysdev, vecs);
708 if (!strncmp(uev->action, "umount", 6)) {
709 r = uev_umount_map(sysdev, vecs);
716 * path add/remove event
718 if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
722 if (!strncmp(uev->action, "add", 3)) {
723 r = uev_add_path(sysdev, vecs);
726 if (!strncmp(uev->action, "remove", 6)) {
727 r = uev_remove_path(sysdev, vecs);
737 ueventloop (void * ap)
739 block_signal(SIGUSR1, NULL);
740 block_signal(SIGHUP, NULL);
742 if (uevent_listen(&uev_trigger, ap))
743 fprintf(stderr, "error starting uevent listener");
749 uxlsnrloop (void * ap)
751 block_signal(SIGUSR1, NULL);
752 block_signal(SIGHUP, NULL);
757 set_handler_callback(LIST+PATHS, cli_list_paths);
758 set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt);
759 set_handler_callback(LIST+MAPS, cli_list_maps);
760 set_handler_callback(LIST+STATUS, cli_list_status);
761 set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
762 set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
763 set_handler_callback(LIST+MAPS+FMT, cli_list_maps_fmt);
764 set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
765 set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
766 set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
767 set_handler_callback(LIST+CONFIG, cli_list_config);
768 set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
769 set_handler_callback(LIST+DEVICES, cli_list_devices);
770 set_handler_callback(LIST+WILDCARDS, cli_list_wildcards);
771 set_handler_callback(ADD+PATH, cli_add_path);
772 set_handler_callback(DEL+PATH, cli_del_path);
773 set_handler_callback(ADD+MAP, cli_add_map);
774 set_handler_callback(DEL+MAP, cli_del_map);
775 set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
776 set_handler_callback(RECONFIGURE, cli_reconfigure);
777 set_handler_callback(SUSPEND+MAP, cli_suspend);
778 set_handler_callback(RESUME+MAP, cli_resume);
779 set_handler_callback(RESIZE+MAP, cli_resize);
780 set_handler_callback(REINSTATE+PATH, cli_reinstate);
781 set_handler_callback(FAIL+PATH, cli_fail);
782 set_handler_callback(DISABLEQ+MAP, cli_disable_queueing);
783 set_handler_callback(RESTOREQ+MAP, cli_restore_queueing);
784 set_handler_callback(DISABLEQ+MAPS, cli_disable_all_queueing);
785 set_handler_callback(RESTOREQ+MAPS, cli_restore_all_queueing);
786 set_handler_callback(QUIT, cli_quit);
789 uxsock_listen(&uxsock_trigger, ap);
795 exit_daemon (int status)
798 fprintf(stderr, "bad exit status. see daemon.log\n");
800 condlog(3, "unlink pidfile");
801 unlink(DEFAULT_PIDFILE);
803 pthread_mutex_lock(&exit_mutex);
804 pthread_cond_signal(&exit_cond);
805 pthread_mutex_unlock(&exit_mutex);
811 fail_path (struct path * pp, int del_active)
816 condlog(2, "checker failed path %s in map %s",
817 pp->dev_t, pp->mpp->alias);
819 dm_fail_path(pp->mpp->alias, pp->dev_t);
821 update_queue_mode_del_path(pp->mpp);
825 * caller must have locked the path list before calling that function
828 reinstate_path (struct path * pp, int add_active)
833 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
834 condlog(0, "%s: reinstate failed", pp->dev_t);
836 condlog(2, "%s: reinstated", pp->dev_t);
838 update_queue_mode_add_path(pp->mpp);
843 enable_group(struct path * pp)
845 struct pathgroup * pgp;
848 * if path is added through uev_add_path, pgindex can be unset.
849 * next update_strings() will set it, upon map reload event.
851 * we can safely return here, because upon map reload, all
852 * PG will be enabled.
854 if (!pp->mpp->pg || !pp->pgindex)
857 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
859 if (pgp->status == PGSTATE_DISABLED) {
860 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
861 dm_enablegroup(pp->mpp->alias, pp->pgindex);
866 mpvec_garbage_collector (struct vectors * vecs)
868 struct multipath * mpp;
874 vector_foreach_slot (vecs->mpvec, mpp, i) {
875 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
876 condlog(2, "%s: remove dead map", mpp->alias);
877 remove_map_and_stop_waiter(mpp, vecs, 1);
884 defered_failback_tick (vector mpvec)
886 struct multipath * mpp;
889 vector_foreach_slot (mpvec, mpp, i) {
891 * defered failback getting sooner
893 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
894 mpp->failback_tick--;
896 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
897 switch_pathgroup(mpp);
903 retry_count_tick(vector mpvec)
905 struct multipath *mpp;
908 vector_foreach_slot (mpvec, mpp, i) {
909 if (mpp->retry_tick) {
910 mpp->stat_total_queueing_time++;
911 condlog(4, "%s: Retrying.. No active path", mpp->alias);
912 if(--mpp->retry_tick == 0) {
913 dm_queue_if_no_path(mpp->alias, 0);
914 condlog(2, "%s: Disable queueing", mpp->alias);
921 check_path (struct vectors * vecs, struct path * pp)
928 if (pp->tick && --pp->tick)
929 return; /* don't check this path yet */
932 * provision a next check soonest,
933 * in case we exit abnormaly from here
935 pp->tick = conf->checkint;
937 if (!checker_selected(&pp->checker)) {
938 pathinfo(pp, conf->hwtable, DI_SYSFS);
941 if (!checker_selected(&pp->checker)) {
942 condlog(0, "%s: checker is not set", pp->dev);
946 * Set checker in async mode.
947 * Honored only by checker implementing the said mode.
949 checker_set_async(&pp->checker);
951 if (path_offline(pp))
952 newstate = PATH_DOWN;
954 newstate = checker_check(&pp->checker);
957 condlog(2, "%s: unusable path", pp->dev);
958 pathinfo(pp, conf->hwtable, 0);
962 * Async IO in flight. Keep the previous path state
963 * and reschedule as soon as possible
965 if (newstate == PATH_PENDING) {
969 if (newstate != pp->state) {
970 int oldstate = pp->state;
971 pp->state = newstate;
972 LOG_MSG(1, checker_message(&pp->checker));
975 * upon state change, reset the checkint
976 * to the shortest delay
978 pp->checkint = conf->checkint;
980 if (newstate == PATH_DOWN || newstate == PATH_SHAKY ||
981 update_multipath_strings(pp->mpp, vecs->pathvec)) {
983 * proactively fail path in the DM
985 if (oldstate == PATH_UP ||
986 oldstate == PATH_GHOST)
992 * cancel scheduled failback
994 pp->mpp->failback_tick = 0;
996 pp->mpp->stat_path_failures++;
1001 * reinstate this path
1003 if (oldstate != PATH_UP &&
1004 oldstate != PATH_GHOST)
1005 reinstate_path(pp, 1);
1007 reinstate_path(pp, 0);
1010 * schedule [defered] failback
1012 if (pp->mpp->pgfailback > 0)
1013 pp->mpp->failback_tick =
1014 pp->mpp->pgfailback + 1;
1015 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
1016 need_switch_pathgroup(pp->mpp, 1))
1017 switch_pathgroup(pp->mpp);
1020 * if at least one path is up in a group, and
1021 * the group is disabled, re-enable it
1023 if (newstate == PATH_UP)
1026 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1027 LOG_MSG(4, checker_message(&pp->checker));
1029 * double the next check delay.
1030 * max at conf->max_checkint
1032 if (pp->checkint < (conf->max_checkint / 2))
1033 pp->checkint = 2 * pp->checkint;
1035 pp->checkint = conf->max_checkint;
1037 pp->tick = pp->checkint;
1038 condlog(4, "%s: delay next check %is",
1039 pp->dev_t, pp->tick);
1041 else if (newstate == PATH_DOWN)
1042 LOG_MSG(2, checker_message(&pp->checker));
1044 pp->state = newstate;
1047 * path prio refreshing
1049 condlog(4, "path prio refresh");
1050 pathinfo(pp, conf->hwtable, DI_PRIO);
1053 * pathgroup failback policy
1055 if (need_switch_pathgroup(pp->mpp, 0)) {
1056 if (pp->mpp->pgfailback > 0 &&
1057 pp->mpp->failback_tick <= 0)
1058 pp->mpp->failback_tick =
1059 pp->mpp->pgfailback + 1;
1060 else if (pp->mpp->pgfailback ==
1061 -FAILBACK_IMMEDIATE)
1062 switch_pathgroup(pp->mpp);
1067 checkerloop (void *ap)
1069 struct vectors *vecs;
1075 mlockall(MCL_CURRENT | MCL_FUTURE);
1076 vecs = (struct vectors *)ap;
1077 condlog(2, "path checkers start up");
1080 * init the path check interval
1082 vector_foreach_slot (vecs->pathvec, pp, i) {
1083 pp->checkint = conf->checkint;
1087 block_signal(SIGHUP, &old);
1088 pthread_cleanup_push(cleanup_lock, &vecs->lock);
1092 if (vecs->pathvec) {
1093 vector_foreach_slot (vecs->pathvec, pp, i) {
1094 check_path(vecs, pp);
1098 defered_failback_tick(vecs->mpvec);
1099 retry_count_tick(vecs->mpvec);
1104 condlog(4, "map garbage collection");
1105 mpvec_garbage_collector(vecs);
1109 lock_cleanup_pop(vecs->lock);
1110 pthread_sigmask(SIG_SETMASK, &old, NULL);
1117 configure (struct vectors * vecs, int start_waiters)
1119 struct multipath * mpp;
1124 if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
1127 if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
1130 if (!(mpvec = vector_alloc()))
1134 * probe for current path (from sysfs) and map (from dm) sets
1136 path_discovery(vecs->pathvec, conf, DI_ALL);
1138 vector_foreach_slot (vecs->pathvec, pp, i){
1139 if (filter_path(conf, pp) > 0){
1140 vector_del_slot(vecs->pathvec, i);
1145 pp->checkint = conf->checkint;
1147 if (map_discovery(vecs))
1151 * create new set of maps & push changed ones into dm
1153 if (coalesce_paths(vecs, mpvec, NULL, 0))
1157 * may need to remove some maps which are no longer relevant
1158 * e.g., due to blacklist changes in conf file
1160 if (coalesce_maps(vecs, mpvec))
1165 sync_maps_state(mpvec);
1168 * purge dm of old maps
1173 * save new set of maps formed by considering current path state
1175 vector_free(vecs->mpvec);
1176 vecs->mpvec = mpvec;
1179 * start dm event waiter threads for these new maps
1181 vector_foreach_slot(vecs->mpvec, mpp, i) {
1182 if (setup_multipath(vecs, mpp))
1185 if (start_waiter_thread(mpp, vecs))
1192 reconfigure (struct vectors * vecs)
1194 struct config * old = conf;
1197 * free old map and path vectors ... they use old conf state
1199 if (VECTOR_SIZE(vecs->mpvec))
1200 remove_maps_and_stop_waiters(vecs);
1202 if (VECTOR_SIZE(vecs->pathvec))
1203 free_pathvec(vecs->pathvec, FREE_PATHS);
1205 vecs->pathvec = NULL;
1208 if (load_config(DEFAULT_CONFIGFILE))
1211 conf->verbosity = old->verbosity;
1213 if (!conf->checkint) {
1214 conf->checkint = DEFAULT_CHECKINT;
1215 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1222 static struct vectors *
1225 struct vectors * vecs;
1227 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1233 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1235 if (!vecs->lock.mutex)
1238 pthread_mutex_init(vecs->lock.mutex, NULL);
1239 vecs->lock.depth = 0;
1245 condlog(0, "failed to init paths");
1250 signal_set(int signo, void (*func) (int))
1253 struct sigaction sig;
1254 struct sigaction osig;
1256 sig.sa_handler = func;
1257 sigemptyset(&sig.sa_mask);
1260 r = sigaction(signo, &sig, &osig);
1265 return (osig.sa_handler);
1271 condlog(2, "reconfigure (SIGHUP)");
1275 unlock(gvecs->lock);
1278 dbg_free_final(NULL);
1291 condlog(3, "SIGUSR1 received");
1297 signal_set(SIGHUP, sighup);
1298 signal_set(SIGUSR1, sigusr1);
1299 signal_set(SIGINT, sigend);
1300 signal_set(SIGTERM, sigend);
1301 signal(SIGPIPE, SIG_IGN);
1308 static struct sched_param sched_param = {
1309 .sched_priority = 99
1312 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1315 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1320 set_oom_adj (int val)
1324 fp = fopen("/proc/self/oom_adj", "w");
1329 fprintf(fp, "%i", val);
1334 setup_thread_attr(pthread_attr_t *attr, size_t stacksize, int detached)
1336 if (pthread_attr_init(attr)) {
1337 fprintf(stderr, "can't initialize thread attr: %s\n",
1341 if (stacksize < PTHREAD_STACK_MIN)
1342 stacksize = PTHREAD_STACK_MIN;
1344 if (pthread_attr_setstacksize(attr, stacksize)) {
1345 fprintf(stderr, "can't set thread stack size to %lu: %s\n",
1346 (unsigned long)stacksize, strerror(errno));
1349 if (detached && pthread_attr_setdetachstate(attr,
1350 PTHREAD_CREATE_DETACHED)) {
1351 fprintf(stderr, "can't set thread to detached: %s\n",
1358 child (void * param)
1360 pthread_t check_thr, uevent_thr, uxlsnr_thr;
1361 pthread_attr_t log_attr, misc_attr;
1362 struct vectors * vecs;
1364 mlockall(MCL_CURRENT | MCL_FUTURE);
1366 setup_thread_attr(&misc_attr, 64 * 1024, 1);
1367 setup_thread_attr(&waiter_attr, 32 * 1024, 1);
1370 setup_thread_attr(&log_attr, 64 * 1024, 0);
1371 log_thread_start(&log_attr);
1372 pthread_attr_destroy(&log_attr);
1375 condlog(2, "--------start up--------");
1376 condlog(2, "read " DEFAULT_CONFIGFILE);
1378 if (load_config(DEFAULT_CONFIGFILE))
1381 if (init_checkers()) {
1382 condlog(0, "failed to initialize checkers");
1386 condlog(0, "failed to initialize prioritizers");
1390 setlogmask(LOG_UPTO(conf->verbosity + 3));
1393 * fill the voids left in the config file
1395 if (!conf->checkint) {
1396 conf->checkint = DEFAULT_CHECKINT;
1397 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1400 if (conf->max_fds) {
1401 struct rlimit fd_limit;
1403 fd_limit.rlim_cur = conf->max_fds;
1404 fd_limit.rlim_max = conf->max_fds;
1405 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0)
1406 condlog(0, "can't set open fds limit to %d : %s\n",
1407 conf->max_fds, strerror(errno));
1410 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1419 vecs = gvecs = init_vecs();
1424 if (sysfs_init(conf->sysfs_dir, FILE_NAME_SIZE)) {
1425 condlog(0, "can not find sysfs mount point");
1430 * fetch and configure both paths and multipaths
1432 if (configure(vecs, 1)) {
1433 condlog(0, "failure during configuration");
1439 pthread_create(&check_thr, &misc_attr, checkerloop, vecs);
1440 pthread_create(&uevent_thr, &misc_attr, ueventloop, vecs);
1441 pthread_create(&uxlsnr_thr, &misc_attr, uxlsnrloop, vecs);
1442 pthread_attr_destroy(&misc_attr);
1444 pthread_cond_wait(&exit_cond, &exit_mutex);
1449 block_signal(SIGHUP, NULL);
1451 remove_maps_and_stop_waiters(vecs);
1452 free_pathvec(vecs->pathvec, FREE_PATHS);
1454 pthread_cancel(check_thr);
1455 pthread_cancel(uevent_thr);
1456 pthread_cancel(uxlsnr_thr);
1462 free_handlers(handlers);
1467 /* Now all the waitevent threads will start rushing in. */
1468 while (vecs->lock.depth > 0) {
1469 sleep (1); /* This is weak. */
1470 condlog(3,"Have %d wait event checkers threads to de-alloc, waiting..\n", vecs->lock.depth);
1472 pthread_mutex_destroy(vecs->lock.mutex);
1473 FREE(vecs->lock.mutex);
1474 vecs->lock.depth = 0;
1475 vecs->lock.mutex = NULL;
1479 condlog(2, "--------shut down-------");
1490 * Freeing config must be done after condlog() and dm_lib_exit(),
1491 * because logging functions like dlog() and dm_write_log()
1492 * reference the config.
1498 dbg_free_final(NULL);
1510 if( (pid = fork()) < 0){
1511 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1519 if ( (pid = fork()) < 0)
1520 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1524 in_fd = open("/dev/null", O_RDONLY);
1526 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1530 out_fd = open("/dev/console", O_WRONLY);
1532 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1537 close(STDIN_FILENO);
1539 close(STDOUT_FILENO);
1541 close(STDERR_FILENO);
1547 fprintf(stderr, "cannot chdir to '/', continuing\n");
1553 main (int argc, char *argv[])
1555 extern char *optarg;
1563 if (getuid() != 0) {
1564 fprintf(stderr, "need to be root\n");
1568 /* make sure we don't lock any path */
1570 umask(umask(077) | 022);
1572 conf = alloc_config();
1577 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1581 //debug=1; /* ### comment me out ### */
1584 if (sizeof(optarg) > sizeof(char *) ||
1585 !isdigit(optarg[0]))
1588 conf->verbosity = atoi(optarg);
1597 if (optind < argc) {
1602 while (optind < argc) {
1603 if (strchr(argv[optind], ' '))
1604 c += snprintf(c, s + CMDSIZE - c, "\"%s\" ", argv[optind]);
1606 c += snprintf(c, s + CMDSIZE - c, "%s ", argv[optind]);
1609 c += snprintf(c, s + CMDSIZE - c, "\n");
1627 return (child(NULL));