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 pp = find_path_by_dev(vecs->pathvec, devname);
359 condlog(0, "%s: spurious uevent, path already in pathvec",
366 * get path vital state
368 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
370 condlog(0, "%s: failed to store path info", devname);
373 pp->checkint = conf->checkint;
377 * need path UID to go any further
379 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
380 condlog(0, "%s: failed to get path uid", devname);
381 return 1; /* leave path added to pathvec */
383 if (filter_path(conf, pp) > 0){
384 int i = find_slot(vecs->pathvec, (void *)pp);
386 vector_del_slot(vecs->pathvec, i);
390 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
393 if (adopt_paths(vecs->pathvec, mpp))
394 return 1; /* leave path added to pathvec */
396 verify_paths(mpp, vecs, NULL);
397 mpp->action = ACT_RELOAD;
400 if ((mpp = add_map_with_path(vecs, pp, 1)))
401 mpp->action = ACT_CREATE;
403 return 1; /* leave path added to pathvec */
407 * push the map to the device-mapper
409 if (setup_map(mpp)) {
410 condlog(0, "%s: failed to setup map for addition of new "
411 "path %s", mpp->alias, devname);
415 * reload the map for the multipath mapped device
417 if (domap(mpp) <= 0) {
418 condlog(0, "%s: failed in domap for addition of new "
419 "path %s", mpp->alias, devname);
421 * deal with asynchronous uevents :((
423 if (mpp->action == ACT_RELOAD) {
424 condlog(0, "%s: uev_add_path sleep", mpp->alias);
426 update_mpp_paths(mpp, vecs->pathvec);
435 * update our state from kernel regardless of create or reload
437 if (setup_multipath(vecs, mpp))
442 if (mpp->action == ACT_CREATE &&
443 start_waiter_thread(mpp, vecs))
446 condlog(2, "%s path added to devmap %s", devname, mpp->alias);
450 remove_map(mpp, vecs, 1);
455 uev_remove_path (struct sysfs_device * dev, struct vectors * vecs)
459 condlog(2, "%s: remove path (uevent)", dev->kernel);
460 retval = ev_remove_path(dev->kernel, vecs);
462 sysfs_device_put(dev);
468 ev_remove_path (char * devname, struct vectors * vecs)
470 struct multipath * mpp;
474 pp = find_path_by_dev(vecs->pathvec, devname);
477 condlog(0, "%s: spurious uevent, path not in pathvec", devname);
482 * avoid referring to the map of an orphaned path
484 if ((mpp = pp->mpp)) {
486 * transform the mp->pg vector of vectors of paths
487 * into a mp->params string to feed the device-mapper
489 if (update_mpp_paths(mpp, vecs->pathvec)) {
490 condlog(0, "%s: failed to update paths",
494 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
495 vector_del_slot(mpp->paths, i);
498 * remove the map IFF removing the last path
500 if (VECTOR_SIZE(mpp->paths) == 0) {
501 char alias[WWID_SIZE];
504 * flush_map will fail if the device is open
506 strncpy(alias, mpp->alias, WWID_SIZE);
507 if (!flush_map(mpp, vecs)) {
508 condlog(2, "%s: removed map after"
509 " removing all paths",
515 * Not an error, continue
519 if (setup_map(mpp)) {
520 condlog(0, "%s: failed to setup map for"
521 " removal of path %s", mpp->alias,
528 mpp->action = ACT_RELOAD;
529 if (domap(mpp) <= 0) {
530 condlog(0, "%s: failed in domap for "
531 "removal of path %s",
532 mpp->alias, devname);
536 * update our state from kernel
538 if (setup_multipath(vecs, mpp)) {
543 condlog(2, "%s: path removed from map %s",
544 devname, mpp->alias);
549 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
550 vector_del_slot(vecs->pathvec, i);
557 remove_map_and_stop_waiter(mpp, vecs, 1);
562 map_discovery (struct vectors * vecs)
564 struct multipath * mpp;
567 if (dm_get_maps(vecs->mpvec))
570 vector_foreach_slot (vecs->mpvec, mpp, i)
571 if (setup_multipath(vecs, mpp))
578 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
580 struct vectors * vecs;
585 vecs = (struct vectors *)trigger_data;
587 pthread_cleanup_push(cleanup_lock, &vecs->lock);
590 r = parse_cmd(str, reply, len, vecs);
593 *reply = STRDUP("fail\n");
594 *len = strlen(*reply) + 1;
597 else if (!r && *len == 0) {
598 *reply = STRDUP("ok\n");
599 *len = strlen(*reply) + 1;
602 /* else if (r < 0) leave *reply alone */
604 lock_cleanup_pop(vecs->lock);
609 uev_discard(char * devpath)
615 * keep only block devices, discard partitions
617 tmp = strstr(devpath, "/block/");
619 condlog(4, "no /block/ in '%s'", devpath);
622 if (sscanf(tmp, "/block/%10s", a) != 1 ||
623 sscanf(tmp, "/block/%10[^/]/%10s", a, b) == 2) {
624 condlog(4, "discard event on %s", devpath);
631 uev_trigger (struct uevent * uev, void * trigger_data)
634 struct sysfs_device *sysdev;
635 struct vectors * vecs;
637 vecs = (struct vectors *)trigger_data;
639 if (uev_discard(uev->devpath))
642 sysdev = sysfs_device_get(uev->devpath);
650 * Add events are ignored here as the tables
651 * are not fully initialised then.
653 if (!strncmp(sysdev->kernel, "dm-", 3)) {
654 if (!strncmp(uev->action, "change", 6)) {
655 r = uev_add_map(sysdev, vecs);
658 if (!strncmp(uev->action, "remove", 6)) {
659 r = uev_remove_map(sysdev, vecs);
662 if (!strncmp(uev->action, "umount", 6)) {
663 r = uev_umount_map(sysdev, vecs);
670 * path add/remove event
672 if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
676 if (!strncmp(uev->action, "add", 3)) {
677 r = uev_add_path(sysdev, vecs);
680 if (!strncmp(uev->action, "remove", 6)) {
681 r = uev_remove_path(sysdev, vecs);
691 ueventloop (void * ap)
693 block_signal(SIGUSR1, NULL);
694 block_signal(SIGHUP, NULL);
696 if (uevent_listen(&uev_trigger, ap))
697 fprintf(stderr, "error starting uevent listener");
703 uxlsnrloop (void * ap)
705 block_signal(SIGUSR1, NULL);
706 block_signal(SIGHUP, NULL);
711 set_handler_callback(LIST+PATHS, cli_list_paths);
712 set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt);
713 set_handler_callback(LIST+MAPS, cli_list_maps);
714 set_handler_callback(LIST+STATUS, cli_list_status);
715 set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
716 set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
717 set_handler_callback(LIST+MAPS+FMT, cli_list_maps_fmt);
718 set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
719 set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
720 set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
721 set_handler_callback(LIST+CONFIG, cli_list_config);
722 set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
723 set_handler_callback(LIST+DEVICES, cli_list_devices);
724 set_handler_callback(LIST+WILDCARDS, cli_list_wildcards);
725 set_handler_callback(ADD+PATH, cli_add_path);
726 set_handler_callback(DEL+PATH, cli_del_path);
727 set_handler_callback(ADD+MAP, cli_add_map);
728 set_handler_callback(DEL+MAP, cli_del_map);
729 set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
730 set_handler_callback(RECONFIGURE, cli_reconfigure);
731 set_handler_callback(SUSPEND+MAP, cli_suspend);
732 set_handler_callback(RESUME+MAP, cli_resume);
733 set_handler_callback(RESIZE+MAP, cli_resize);
734 set_handler_callback(REINSTATE+PATH, cli_reinstate);
735 set_handler_callback(FAIL+PATH, cli_fail);
736 set_handler_callback(QUIT, cli_quit);
738 uxsock_listen(&uxsock_trigger, ap);
744 exit_daemon (int status)
747 fprintf(stderr, "bad exit status. see daemon.log\n");
749 condlog(3, "unlink pidfile");
750 unlink(DEFAULT_PIDFILE);
752 pthread_mutex_lock(&exit_mutex);
753 pthread_cond_signal(&exit_cond);
754 pthread_mutex_unlock(&exit_mutex);
760 fail_path (struct path * pp, int del_active)
765 condlog(2, "checker failed path %s in map %s",
766 pp->dev_t, pp->mpp->alias);
768 dm_fail_path(pp->mpp->alias, pp->dev_t);
770 update_queue_mode_del_path(pp->mpp);
774 * caller must have locked the path list before calling that function
777 reinstate_path (struct path * pp, int add_active)
782 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
783 condlog(0, "%s: reinstate failed", pp->dev_t);
785 condlog(2, "%s: reinstated", pp->dev_t);
787 update_queue_mode_add_path(pp->mpp);
792 enable_group(struct path * pp)
794 struct pathgroup * pgp;
797 * if path is added through uev_add_path, pgindex can be unset.
798 * next update_strings() will set it, upon map reload event.
800 * we can safely return here, because upon map reload, all
801 * PG will be enabled.
803 if (!pp->mpp->pg || !pp->pgindex)
806 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
808 if (pgp->status == PGSTATE_DISABLED) {
809 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
810 dm_enablegroup(pp->mpp->alias, pp->pgindex);
815 mpvec_garbage_collector (struct vectors * vecs)
817 struct multipath * mpp;
820 vector_foreach_slot (vecs->mpvec, mpp, i) {
821 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
822 condlog(2, "%s: remove dead map", mpp->alias);
823 remove_map_and_stop_waiter(mpp, vecs, 1);
830 defered_failback_tick (vector mpvec)
832 struct multipath * mpp;
835 vector_foreach_slot (mpvec, mpp, i) {
837 * defered failback getting sooner
839 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
840 mpp->failback_tick--;
842 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
843 switch_pathgroup(mpp);
849 retry_count_tick(vector mpvec)
851 struct multipath *mpp;
854 vector_foreach_slot (mpvec, mpp, i) {
855 if (mpp->retry_tick) {
856 mpp->stat_total_queueing_time++;
857 condlog(4, "%s: Retrying.. No active path", mpp->alias);
858 if(--mpp->retry_tick == 0) {
859 dm_queue_if_no_path(mpp->alias, 0);
860 condlog(2, "%s: Disable queueing", mpp->alias);
867 check_path (struct vectors * vecs, struct path * pp)
874 if (pp->tick && --pp->tick)
875 return; /* don't check this path yet */
878 * provision a next check soonest,
879 * in case we exit abnormaly from here
881 pp->tick = conf->checkint;
883 if (!checker_selected(&pp->checker)) {
884 pathinfo(pp, conf->hwtable, DI_SYSFS);
887 if (!checker_selected(&pp->checker)) {
888 condlog(0, "%s: checker is not set", pp->dev);
892 * Set checker in async mode.
893 * Honored only by checker implementing the said mode.
895 checker_set_async(&pp->checker);
897 if (path_offline(pp))
898 newstate = PATH_DOWN;
900 newstate = checker_check(&pp->checker);
903 condlog(2, "%s: unusable path", pp->dev);
904 pathinfo(pp, conf->hwtable, 0);
908 * Async IO in flight. Keep the previous path state
909 * and reschedule as soon as possible
911 if (newstate == PATH_PENDING) {
915 if (newstate != pp->state) {
916 int oldstate = pp->state;
917 pp->state = newstate;
918 LOG_MSG(1, checker_message(&pp->checker));
921 * upon state change, reset the checkint
922 * to the shortest delay
924 pp->checkint = conf->checkint;
926 if (newstate == PATH_DOWN || newstate == PATH_SHAKY ||
927 update_multipath_strings(pp->mpp, vecs->pathvec)) {
929 * proactively fail path in the DM
931 if (oldstate == PATH_UP ||
932 oldstate == PATH_GHOST)
938 * cancel scheduled failback
940 pp->mpp->failback_tick = 0;
942 pp->mpp->stat_path_failures++;
947 * reinstate this path
949 if (oldstate != PATH_UP &&
950 oldstate != PATH_GHOST)
951 reinstate_path(pp, 1);
953 reinstate_path(pp, 0);
956 * schedule [defered] failback
958 if (pp->mpp->pgfailback > 0)
959 pp->mpp->failback_tick =
960 pp->mpp->pgfailback + 1;
961 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
962 need_switch_pathgroup(pp->mpp, 1))
963 switch_pathgroup(pp->mpp);
966 * if at least one path is up in a group, and
967 * the group is disabled, re-enable it
969 if (newstate == PATH_UP)
972 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
973 LOG_MSG(4, checker_message(&pp->checker));
975 * double the next check delay.
976 * max at conf->max_checkint
978 if (pp->checkint < (conf->max_checkint / 2))
979 pp->checkint = 2 * pp->checkint;
981 pp->checkint = conf->max_checkint;
983 pp->tick = pp->checkint;
984 condlog(4, "%s: delay next check %is",
985 pp->dev_t, pp->tick);
987 else if (newstate == PATH_DOWN)
988 LOG_MSG(2, checker_message(&pp->checker));
990 pp->state = newstate;
993 * path prio refreshing
995 condlog(4, "path prio refresh");
996 pathinfo(pp, conf->hwtable, DI_PRIO);
999 * pathgroup failback policy
1001 if (need_switch_pathgroup(pp->mpp, 0)) {
1002 if (pp->mpp->pgfailback > 0 &&
1003 pp->mpp->failback_tick <= 0)
1004 pp->mpp->failback_tick =
1005 pp->mpp->pgfailback + 1;
1006 else if (pp->mpp->pgfailback ==
1007 -FAILBACK_IMMEDIATE)
1008 switch_pathgroup(pp->mpp);
1013 checkerloop (void *ap)
1015 struct vectors *vecs;
1021 mlockall(MCL_CURRENT | MCL_FUTURE);
1022 vecs = (struct vectors *)ap;
1023 condlog(2, "path checkers start up");
1026 * init the path check interval
1028 vector_foreach_slot (vecs->pathvec, pp, i) {
1029 pp->checkint = conf->checkint;
1033 block_signal(SIGHUP, &old);
1034 pthread_cleanup_push(cleanup_lock, &vecs->lock);
1038 if (vecs->pathvec) {
1039 vector_foreach_slot (vecs->pathvec, pp, i) {
1040 check_path(vecs, pp);
1044 defered_failback_tick(vecs->mpvec);
1045 retry_count_tick(vecs->mpvec);
1050 condlog(4, "map garbage collection");
1051 mpvec_garbage_collector(vecs);
1055 lock_cleanup_pop(vecs->lock);
1056 pthread_sigmask(SIG_SETMASK, &old, NULL);
1063 configure (struct vectors * vecs, int start_waiters)
1065 struct multipath * mpp;
1070 if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
1073 if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
1076 if (!(mpvec = vector_alloc()))
1080 * probe for current path (from sysfs) and map (from dm) sets
1082 path_discovery(vecs->pathvec, conf, DI_ALL);
1084 vector_foreach_slot (vecs->pathvec, pp, i){
1085 if (filter_path(conf, pp) > 0){
1086 vector_del_slot(vecs->pathvec, i);
1091 pp->checkint = conf->checkint;
1093 if (map_discovery(vecs))
1097 * create new set of maps & push changed ones into dm
1099 if (coalesce_paths(vecs, mpvec, NULL, 0))
1103 * may need to remove some maps which are no longer relevant
1104 * e.g., due to blacklist changes in conf file
1106 if (coalesce_maps(vecs, mpvec))
1111 sync_maps_state(mpvec);
1114 * purge dm of old maps
1119 * save new set of maps formed by considering current path state
1121 vector_free(vecs->mpvec);
1122 vecs->mpvec = mpvec;
1125 * start dm event waiter threads for these new maps
1127 vector_foreach_slot(vecs->mpvec, mpp, i) {
1128 if (setup_multipath(vecs, mpp))
1131 if (start_waiter_thread(mpp, vecs))
1138 reconfigure (struct vectors * vecs)
1140 struct config * old = conf;
1143 * free old map and path vectors ... they use old conf state
1145 if (VECTOR_SIZE(vecs->mpvec))
1146 remove_maps_and_stop_waiters(vecs);
1148 if (VECTOR_SIZE(vecs->pathvec))
1149 free_pathvec(vecs->pathvec, FREE_PATHS);
1151 vecs->pathvec = NULL;
1154 if (load_config(DEFAULT_CONFIGFILE))
1157 conf->verbosity = old->verbosity;
1159 if (!conf->checkint) {
1160 conf->checkint = DEFAULT_CHECKINT;
1161 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1168 static struct vectors *
1171 struct vectors * vecs;
1173 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1179 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1181 if (!vecs->lock.mutex)
1184 pthread_mutex_init(vecs->lock.mutex, NULL);
1185 vecs->lock.depth = 0;
1191 condlog(0, "failed to init paths");
1196 signal_set(int signo, void (*func) (int))
1199 struct sigaction sig;
1200 struct sigaction osig;
1202 sig.sa_handler = func;
1203 sigemptyset(&sig.sa_mask);
1206 r = sigaction(signo, &sig, &osig);
1211 return (osig.sa_handler);
1217 condlog(2, "reconfigure (SIGHUP)");
1221 unlock(gvecs->lock);
1224 dbg_free_final(NULL);
1237 condlog(3, "SIGUSR1 received");
1243 signal_set(SIGHUP, sighup);
1244 signal_set(SIGUSR1, sigusr1);
1245 signal_set(SIGINT, sigend);
1246 signal_set(SIGTERM, sigend);
1247 signal(SIGPIPE, SIG_IGN);
1254 static struct sched_param sched_param = {
1255 .sched_priority = 99
1258 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1261 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1266 set_oom_adj (int val)
1270 fp = fopen("/proc/self/oom_adj", "w");
1275 fprintf(fp, "%i", val);
1280 setup_thread_attr(pthread_attr_t *attr, size_t stacksize, int detached)
1282 if (pthread_attr_init(attr)) {
1283 fprintf(stderr, "can't initialize thread attr: %s\n",
1287 if (stacksize < PTHREAD_STACK_MIN)
1288 stacksize = PTHREAD_STACK_MIN;
1290 if (pthread_attr_setstacksize(attr, stacksize)) {
1291 fprintf(stderr, "can't set thread stack size to %lu: %s\n",
1292 (unsigned long)stacksize, strerror(errno));
1295 if (detached && pthread_attr_setdetachstate(attr,
1296 PTHREAD_CREATE_DETACHED)) {
1297 fprintf(stderr, "can't set thread to detached: %s\n",
1304 child (void * param)
1306 pthread_t check_thr, uevent_thr, uxlsnr_thr;
1307 pthread_attr_t log_attr, misc_attr;
1308 struct vectors * vecs;
1310 mlockall(MCL_CURRENT | MCL_FUTURE);
1312 setup_thread_attr(&misc_attr, 64 * 1024, 1);
1313 setup_thread_attr(&waiter_attr, 32 * 1024, 1);
1316 setup_thread_attr(&log_attr, 64 * 1024, 0);
1317 log_thread_start(&log_attr);
1318 pthread_attr_destroy(&log_attr);
1321 condlog(2, "--------start up--------");
1322 condlog(2, "read " DEFAULT_CONFIGFILE);
1324 if (load_config(DEFAULT_CONFIGFILE))
1327 if (init_checkers()) {
1328 condlog(0, "failed to initialize checkers");
1332 condlog(0, "failed to initialize prioritizers");
1336 setlogmask(LOG_UPTO(conf->verbosity + 3));
1339 * fill the voids left in the config file
1341 if (!conf->checkint) {
1342 conf->checkint = DEFAULT_CHECKINT;
1343 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1346 if (conf->max_fds) {
1347 struct rlimit fd_limit;
1348 if (conf->max_fds > 0) {
1349 fd_limit.rlim_cur = conf->max_fds;
1350 fd_limit.rlim_max = conf->max_fds;
1353 fd_limit.rlim_cur = RLIM_INFINITY;
1354 fd_limit.rlim_max = RLIM_INFINITY;
1356 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0)
1357 condlog(0, "can't set open fds limit to %d : %s\n",
1358 conf->max_fds, strerror(errno));
1361 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1370 vecs = gvecs = init_vecs();
1375 if (sysfs_init(conf->sysfs_dir, FILE_NAME_SIZE)) {
1376 condlog(0, "can not find sysfs mount point");
1381 * fetch and configure both paths and multipaths
1383 if (configure(vecs, 1)) {
1384 condlog(0, "failure during configuration");
1390 pthread_create(&check_thr, &misc_attr, checkerloop, vecs);
1391 pthread_create(&uevent_thr, &misc_attr, ueventloop, vecs);
1392 pthread_create(&uxlsnr_thr, &misc_attr, uxlsnrloop, vecs);
1393 pthread_attr_destroy(&misc_attr);
1395 pthread_cond_wait(&exit_cond, &exit_mutex);
1400 block_signal(SIGHUP, NULL);
1402 remove_maps_and_stop_waiters(vecs);
1403 free_pathvec(vecs->pathvec, FREE_PATHS);
1405 pthread_cancel(check_thr);
1406 pthread_cancel(uevent_thr);
1407 pthread_cancel(uxlsnr_thr);
1413 free_handlers(handlers);
1418 /* Now all the waitevent threads will start rushing in. */
1419 while (vecs->lock.depth > 0) {
1420 sleep (1); /* This is weak. */
1421 condlog(3,"Have %d wait event checkers threads to de-alloc, waiting..\n", vecs->lock.depth);
1423 pthread_mutex_destroy(vecs->lock.mutex);
1424 FREE(vecs->lock.mutex);
1425 vecs->lock.depth = 0;
1426 vecs->lock.mutex = NULL;
1430 condlog(2, "--------shut down-------");
1441 * Freeing config must be done after condlog() and dm_lib_exit(),
1442 * because logging functions like dlog() and dm_write_log()
1443 * reference the config.
1449 dbg_free_final(NULL);
1461 if( (pid = fork()) < 0){
1462 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1470 if ( (pid = fork()) < 0)
1471 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1475 in_fd = open("/dev/null", O_RDONLY);
1477 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1481 out_fd = open("/dev/console", O_WRONLY);
1483 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1488 close(STDIN_FILENO);
1490 close(STDOUT_FILENO);
1492 close(STDERR_FILENO);
1498 fprintf(stderr, "cannot chdir to '/', continuing\n");
1504 main (int argc, char *argv[])
1506 extern char *optarg;
1514 if (getuid() != 0) {
1515 fprintf(stderr, "need to be root\n");
1519 /* make sure we don't lock any path */
1521 umask(umask(077) | 022);
1523 conf = alloc_config();
1528 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1532 //debug=1; /* ### comment me out ### */
1535 if (sizeof(optarg) > sizeof(char *) ||
1536 !isdigit(optarg[0]))
1539 conf->verbosity = atoi(optarg);
1548 if (optind < argc) {
1553 while (optind < argc) {
1554 if (strchr(argv[optind], ' '))
1555 c += snprintf(c, s + CMDSIZE - c, "\"%s\" ", argv[optind]);
1557 c += snprintf(c, s + CMDSIZE - c, "%s ", argv[optind]);
1560 c += snprintf(c, s + CMDSIZE - c, "\n");
1578 return (child(NULL));