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>
18 #include <linux/oom.h>
20 #include <mpath_persist.h>
39 #include <blacklist.h>
40 #include <structs_vec.h>
42 #include <devmapper.h>
45 #include <discovery.h>
49 #include <switchgroup.h>
51 #include <configure.h>
53 #include <pgpolicies.h>
61 #include "cli_handlers.h"
65 #define FILE_NAME_SIZE 256
68 #define LOG_MSG(a, b) \
71 condlog(a, "%s: %s - path offline", pp->mpp->alias, pp->dev); \
73 condlog(a, "%s: %s - %s", pp->mpp->alias, pp->dev, b); \
76 struct mpath_event_param
79 struct multipath *mpp;
82 unsigned int mpath_mx_alloc_len;
84 pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
85 pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
88 enum daemon_status running_state;
92 * global copy of vecs for use in sig handlers
94 struct vectors * gvecs;
97 need_switch_pathgroup (struct multipath * mpp, int refresh)
99 struct pathgroup * pgp;
103 if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
107 * Refresh path priority values
110 vector_foreach_slot (mpp->pg, pgp, i)
111 vector_foreach_slot (pgp->paths, pp, j)
112 pathinfo(pp, conf->hwtable, DI_PRIO);
114 mpp->bestpg = select_path_group(mpp);
116 if (mpp->bestpg != mpp->nextpg)
123 switch_pathgroup (struct multipath * mpp)
125 mpp->stat_switchgroup++;
126 dm_switchgroup(mpp->alias, mpp->bestpg);
127 condlog(2, "%s: switch to path group #%i",
128 mpp->alias, mpp->bestpg);
132 coalesce_maps(struct vectors *vecs, vector nmpv)
134 struct multipath * ompp;
135 vector ompv = vecs->mpvec;
139 vector_foreach_slot (ompv, ompp, i) {
140 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
142 * remove all current maps not allowed by the
143 * current configuration
145 if (dm_flush_map(ompp->alias)) {
146 condlog(0, "%s: unable to flush devmap",
149 * may be just because the device is open
151 if (!vector_alloc_slot(nmpv))
154 vector_set_slot(nmpv, ompp);
155 setup_multipath(vecs, ompp);
157 if ((j = find_slot(ompv, (void *)ompp)) != -1)
158 vector_del_slot(ompv, j);
164 condlog(2, "%s devmap removed", ompp->alias);
166 } else if (conf->reassign_maps) {
167 condlog(3, "%s: Reassign existing device-mapper"
168 " devices", ompp->alias);
169 dm_reassign(ompp->alias);
176 sync_map_state(struct multipath *mpp)
178 struct pathgroup *pgp;
185 vector_foreach_slot (mpp->pg, pgp, i){
186 vector_foreach_slot (pgp->paths, pp, j){
187 if (pp->state == PATH_UNCHECKED ||
188 pp->state == PATH_WILD)
190 if ((pp->dmstate == PSTATE_FAILED ||
191 pp->dmstate == PSTATE_UNDEF) &&
192 (pp->state == PATH_UP || pp->state == PATH_GHOST))
193 dm_reinstate_path(mpp->alias, pp->dev_t);
194 else if ((pp->dmstate == PSTATE_ACTIVE ||
195 pp->dmstate == PSTATE_UNDEF) &&
196 (pp->state == PATH_DOWN ||
197 pp->state == PATH_SHAKY))
198 dm_fail_path(mpp->alias, pp->dev_t);
204 sync_maps_state(vector mpvec)
207 struct multipath *mpp;
209 vector_foreach_slot (mpvec, mpp, i)
214 flush_map(struct multipath * mpp, struct vectors * vecs)
217 * clear references to this map before flushing so we can ignore
218 * the spurious uevent we may generate with the dm_flush_map call below
220 if (dm_flush_map(mpp->alias)) {
222 * May not really be an error -- if the map was already flushed
223 * from the device mapper by dmsetup(8) for instance.
225 condlog(0, "%s: can't flush", mpp->alias);
230 condlog(2, "%s: devmap removed", mpp->alias);
233 orphan_paths(vecs->pathvec, mpp);
234 remove_map_and_stop_waiter(mpp, vecs, 1);
240 uev_add_map (struct uevent * uev, struct vectors * vecs)
243 int major = -1, minor = -1, rc;
245 condlog(3, "%s: add map (uevent)", uev->kernel);
246 alias = uevent_get_dm_name(uev);
248 condlog(3, "%s: No DM_NAME in uevent", uev->kernel);
249 major = uevent_get_major(uev);
250 minor = uevent_get_minor(uev);
251 alias = dm_mapname(major, minor);
253 condlog(2, "%s: mapname not found for %d:%d",
254 uev->kernel, major, minor);
258 rc = ev_add_map(uev->kernel, alias, vecs);
264 ev_add_map (char * dev, char * alias, struct vectors * vecs)
267 struct multipath * mpp;
271 map_present = dm_map_present(alias);
273 if (map_present && dm_type(alias, TGT_MPATH) <= 0) {
274 condlog(4, "%s: not a multipath map", alias);
278 mpp = find_mp_by_alias(vecs->mpvec, alias);
282 * Not really an error -- we generate our own uevent
283 * if we create a multipath mapped device as a result
286 if (conf->reassign_maps) {
287 condlog(3, "%s: Reassign existing device-mapper devices",
293 condlog(2, "%s: adding map", alias);
296 * now we can register the map
298 if (map_present && (mpp = add_map_without_path(vecs, alias))) {
300 condlog(2, "%s: devmap %s registered", alias, dev);
303 refwwid = get_refwwid(dev, DEV_DEVMAP, vecs->pathvec);
306 r = coalesce_paths(vecs, NULL, refwwid, 0);
311 condlog(2, "%s: devmap %s added", alias, dev);
313 condlog(0, "%s: uev_add_map %s failed", alias, dev);
320 uev_remove_map (struct uevent * uev, struct vectors * vecs)
324 struct multipath *mpp;
326 condlog(2, "%s: remove map (uevent)", uev->kernel);
327 alias = uevent_get_dm_name(uev);
329 condlog(3, "%s: No DM_NAME in uevent, ignoring", uev->kernel);
332 minor = uevent_get_minor(uev);
333 mpp = find_mp_by_minor(vecs->mpvec, minor);
336 condlog(2, "%s: devmap not registered, can't remove",
340 if (strcmp(mpp->alias, alias)) {
341 condlog(2, "%s: minor number mismatch (map %d, event %d)",
342 mpp->alias, mpp->dmi->minor, minor);
346 orphan_paths(vecs->pathvec, mpp);
347 remove_map_and_stop_waiter(mpp, vecs, 1);
354 ev_remove_map (char * devname, char * alias, int minor, struct vectors * vecs)
356 struct multipath * mpp;
358 mpp = find_mp_by_minor(vecs->mpvec, minor);
361 condlog(2, "%s: devmap not registered, can't remove",
365 if (strcmp(mpp->alias, alias)) {
366 condlog(2, "%s: minor number mismatch (map %d, event %d)",
367 mpp->alias, mpp->dmi->minor, minor);
370 return flush_map(mpp, vecs);
374 uev_add_path (struct uevent *uev, struct vectors * vecs)
378 condlog(2, "%s: add path (uevent)", uev->kernel);
379 if (strstr(uev->kernel, "..") != NULL) {
381 * Don't allow relative device names in the pathvec
383 condlog(0, "%s: path name is invalid", uev->kernel);
387 pp = find_path_by_dev(vecs->pathvec, uev->kernel);
389 condlog(0, "%s: spurious uevent, path already in pathvec",
395 * get path vital state
397 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
398 uev->udev, DI_ALL))) {
399 condlog(0, "%s: failed to store path info",
403 pp->checkint = conf->checkint;
406 return (ev_add_path(pp, vecs) != 1)? 0 : 1;
416 ev_add_path (struct path * pp, struct vectors * vecs)
418 struct multipath * mpp;
419 char empty_buff[WWID_SIZE] = {0};
420 char params[PARAMS_SIZE] = {0};
422 int start_waiter = 0;
425 * need path UID to go any further
427 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
428 condlog(0, "%s: failed to get path uid", pp->dev);
429 goto fail; /* leave path added to pathvec */
431 if (filter_path(conf, pp) > 0){
432 int i = find_slot(vecs->pathvec, (void *)pp);
434 vector_del_slot(vecs->pathvec, i);
438 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
441 if ((!pp->size) || (mpp->size != pp->size)) {
443 condlog(0, "%s: failed to add new path %s, "
445 mpp->alias, pp->dev);
447 condlog(0, "%s: failed to add new path %s, "
448 "device size mismatch",
449 mpp->alias, pp->dev);
450 int i = find_slot(vecs->pathvec, (void *)pp);
452 vector_del_slot(vecs->pathvec, i);
457 condlog(4,"%s: adopting all paths for path %s",
458 mpp->alias, pp->dev);
459 if (adopt_paths(vecs->pathvec, mpp, 1))
460 goto fail; /* leave path added to pathvec */
462 verify_paths(mpp, vecs, NULL);
463 mpp->flush_on_last_del = FLUSH_UNDEF;
464 mpp->action = ACT_RELOAD;
468 condlog(0, "%s: failed to create new map,"
469 " device size is 0 ", pp->dev);
470 int i = find_slot(vecs->pathvec, (void *)pp);
472 vector_del_slot(vecs->pathvec, i);
477 condlog(4,"%s: creating new map", pp->dev);
478 if ((mpp = add_map_with_path(vecs, pp, 1))) {
479 mpp->action = ACT_CREATE;
481 * We don't depend on ACT_CREATE, as domap will
482 * set it to ACT_NOTHING when complete.
487 goto fail; /* leave path added to pathvec */
490 /* persistent reseravtion check*/
491 mpath_pr_event_handle(pp);
494 * push the map to the device-mapper
496 if (setup_map(mpp, params, PARAMS_SIZE)) {
497 condlog(0, "%s: failed to setup map for addition of new "
498 "path %s", mpp->alias, pp->dev);
502 * reload the map for the multipath mapped device
504 if (domap(mpp, params) <= 0) {
505 condlog(0, "%s: failed in domap for addition of new "
506 "path %s", mpp->alias, pp->dev);
508 * deal with asynchronous uevents :((
510 if (mpp->action == ACT_RELOAD && retries-- > 0) {
511 condlog(0, "%s: uev_add_path sleep", mpp->alias);
513 update_mpp_paths(mpp, vecs->pathvec);
516 else if (mpp->action == ACT_RELOAD)
517 condlog(0, "%s: giving up reload", mpp->alias);
524 * update our state from kernel regardless of create or reload
526 if (setup_multipath(vecs, mpp))
527 goto fail; /* if setup_multipath fails, it removes the map */
531 if ((mpp->action == ACT_CREATE ||
532 (mpp->action == ACT_NOTHING && start_waiter && !mpp->waiter)) &&
533 start_waiter_thread(mpp, vecs))
537 condlog(2, "%s path added to devmap %s", pp->dev, mpp->alias);
544 remove_map(mpp, vecs, 1);
551 uev_remove_path (struct uevent *uev, struct vectors * vecs)
555 condlog(2, "%s: remove path (uevent)", uev->kernel);
556 pp = find_path_by_dev(vecs->pathvec, uev->kernel);
559 /* Not an error; path might have been purged earlier */
560 condlog(0, "%s: path already removed", uev->kernel);
564 return ev_remove_path(pp, vecs);
568 ev_remove_path (struct path *pp, struct vectors * vecs)
570 struct multipath * mpp;
572 char params[PARAMS_SIZE] = {0};
575 * avoid referring to the map of an orphaned path
577 if ((mpp = pp->mpp)) {
579 * transform the mp->pg vector of vectors of paths
580 * into a mp->params string to feed the device-mapper
582 if (update_mpp_paths(mpp, vecs->pathvec)) {
583 condlog(0, "%s: failed to update paths",
587 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
588 vector_del_slot(mpp->paths, i);
591 * remove the map IFF removing the last path
593 if (VECTOR_SIZE(mpp->paths) == 0) {
594 char alias[WWID_SIZE];
597 * flush_map will fail if the device is open
599 strncpy(alias, mpp->alias, WWID_SIZE);
600 if (mpp->flush_on_last_del == FLUSH_ENABLED) {
601 condlog(2, "%s Last path deleted, disabling queueing", mpp->alias);
603 mpp->no_path_retry = NO_PATH_RETRY_FAIL;
604 mpp->flush_on_last_del = FLUSH_IN_PROGRESS;
605 dm_queue_if_no_path(mpp->alias, 0);
607 if (!flush_map(mpp, vecs)) {
608 condlog(2, "%s: removed map after"
609 " removing all paths",
615 * Not an error, continue
619 if (setup_map(mpp, params, PARAMS_SIZE)) {
620 condlog(0, "%s: failed to setup map for"
621 " removal of path %s", mpp->alias, pp->dev);
627 mpp->action = ACT_RELOAD;
628 if (domap(mpp, params) <= 0) {
629 condlog(0, "%s: failed in domap for "
630 "removal of path %s",
631 mpp->alias, pp->dev);
635 * update our state from kernel
637 if (setup_multipath(vecs, mpp)) {
642 condlog(2, "%s: path removed from map %s",
643 pp->dev, mpp->alias);
648 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
649 vector_del_slot(vecs->pathvec, i);
656 remove_map_and_stop_waiter(mpp, vecs, 1);
661 uev_update_path (struct uevent *uev, struct vectors * vecs)
665 ro = uevent_get_disk_ro(uev);
670 condlog(2, "%s: update path write_protect to '%d' (uevent)",
672 pp = find_path_by_dev(vecs->pathvec, uev->kernel);
674 condlog(0, "%s: spurious uevent, path not found",
679 retval = reload_map(vecs, pp->mpp);
681 condlog(2, "%s: map %s reloaded (retval %d)",
682 uev->kernel, pp->mpp->alias, retval);
690 map_discovery (struct vectors * vecs)
692 struct multipath * mpp;
695 if (dm_get_maps(vecs->mpvec))
698 vector_foreach_slot (vecs->mpvec, mpp, i)
699 if (setup_multipath(vecs, mpp))
706 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
708 struct vectors * vecs;
713 vecs = (struct vectors *)trigger_data;
715 pthread_cleanup_push(cleanup_lock, &vecs->lock);
717 pthread_testcancel();
719 r = parse_cmd(str, reply, len, vecs);
722 *reply = STRDUP("fail\n");
723 *len = strlen(*reply) + 1;
726 else if (!r && *len == 0) {
727 *reply = STRDUP("ok\n");
728 *len = strlen(*reply) + 1;
731 /* else if (r < 0) leave *reply alone */
733 lock_cleanup_pop(vecs->lock);
738 uev_discard(char * devpath)
744 * keep only block devices, discard partitions
746 tmp = strstr(devpath, "/block/");
748 condlog(4, "no /block/ in '%s'", devpath);
751 if (sscanf(tmp, "/block/%10s", a) != 1 ||
752 sscanf(tmp, "/block/%10[^/]/%10s", a, b) == 2) {
753 condlog(4, "discard event on %s", devpath);
760 uev_trigger (struct uevent * uev, void * trigger_data)
763 struct vectors * vecs;
765 vecs = (struct vectors *)trigger_data;
767 if (uev_discard(uev->devpath))
770 pthread_cleanup_push(cleanup_lock, &vecs->lock);
772 pthread_testcancel();
776 * Add events are ignored here as the tables
777 * are not fully initialised then.
779 if (!strncmp(uev->kernel, "dm-", 3)) {
780 if (!strncmp(uev->action, "change", 6)) {
781 r = uev_add_map(uev, vecs);
784 if (!strncmp(uev->action, "remove", 6)) {
785 r = uev_remove_map(uev, vecs);
792 * path add/remove event
794 if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
798 if (!strncmp(uev->action, "add", 3)) {
799 r = uev_add_path(uev, vecs);
802 if (!strncmp(uev->action, "remove", 6)) {
803 r = uev_remove_path(uev, vecs);
806 if (!strncmp(uev->action, "change", 6)) {
807 r = uev_update_path(uev, vecs);
812 lock_cleanup_pop(vecs->lock);
817 ueventloop (void * ap)
819 block_signal(SIGUSR1, NULL);
820 block_signal(SIGHUP, NULL);
823 condlog(0, "error starting uevent listener");
831 block_signal(SIGUSR1, NULL);
832 block_signal(SIGHUP, NULL);
834 if (uevent_dispatch(&uev_trigger, ap))
835 condlog(0, "error starting uevent dispatcher");
840 uxlsnrloop (void * ap)
842 block_signal(SIGUSR1, NULL);
843 block_signal(SIGHUP, NULL);
848 set_handler_callback(LIST+PATHS, cli_list_paths);
849 set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt);
850 set_handler_callback(LIST+MAPS, cli_list_maps);
851 set_handler_callback(LIST+STATUS, cli_list_status);
852 set_handler_callback(LIST+DAEMON, cli_list_daemon);
853 set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
854 set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
855 set_handler_callback(LIST+MAPS+FMT, cli_list_maps_fmt);
856 set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
857 set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
858 set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
859 set_handler_callback(LIST+CONFIG, cli_list_config);
860 set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
861 set_handler_callback(LIST+DEVICES, cli_list_devices);
862 set_handler_callback(LIST+WILDCARDS, cli_list_wildcards);
863 set_handler_callback(ADD+PATH, cli_add_path);
864 set_handler_callback(DEL+PATH, cli_del_path);
865 set_handler_callback(ADD+MAP, cli_add_map);
866 set_handler_callback(DEL+MAP, cli_del_map);
867 set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
868 set_handler_callback(RECONFIGURE, cli_reconfigure);
869 set_handler_callback(SUSPEND+MAP, cli_suspend);
870 set_handler_callback(RESUME+MAP, cli_resume);
871 set_handler_callback(RESIZE+MAP, cli_resize);
872 set_handler_callback(RELOAD+MAP, cli_reload);
873 set_handler_callback(RESET+MAP, cli_reassign);
874 set_handler_callback(REINSTATE+PATH, cli_reinstate);
875 set_handler_callback(FAIL+PATH, cli_fail);
876 set_handler_callback(DISABLEQ+MAP, cli_disable_queueing);
877 set_handler_callback(RESTOREQ+MAP, cli_restore_queueing);
878 set_handler_callback(DISABLEQ+MAPS, cli_disable_all_queueing);
879 set_handler_callback(RESTOREQ+MAPS, cli_restore_all_queueing);
880 set_handler_callback(QUIT, cli_quit);
881 set_handler_callback(SHUTDOWN, cli_shutdown);
882 set_handler_callback(GETPRSTATUS+MAP, cli_getprstatus);
883 set_handler_callback(SETPRSTATUS+MAP, cli_setprstatus);
884 set_handler_callback(UNSETPRSTATUS+MAP, cli_unsetprstatus);
887 uxsock_listen(&uxsock_trigger, ap);
893 exit_daemon (int status)
896 fprintf(stderr, "bad exit status. see daemon.log\n");
898 condlog(3, "unlink pidfile");
899 unlink(DEFAULT_PIDFILE);
901 pthread_mutex_lock(&exit_mutex);
902 pthread_cond_signal(&exit_cond);
903 pthread_mutex_unlock(&exit_mutex);
911 switch (running_state) {
916 case DAEMON_CONFIGURE:
920 case DAEMON_SHUTDOWN:
927 fail_path (struct path * pp, int del_active)
932 condlog(2, "checker failed path %s in map %s",
933 pp->dev_t, pp->mpp->alias);
935 dm_fail_path(pp->mpp->alias, pp->dev_t);
937 update_queue_mode_del_path(pp->mpp);
941 * caller must have locked the path list before calling that function
944 reinstate_path (struct path * pp, int add_active)
949 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
950 condlog(0, "%s: reinstate failed", pp->dev_t);
952 condlog(2, "%s: reinstated", pp->dev_t);
954 update_queue_mode_add_path(pp->mpp);
959 enable_group(struct path * pp)
961 struct pathgroup * pgp;
964 * if path is added through uev_add_path, pgindex can be unset.
965 * next update_strings() will set it, upon map reload event.
967 * we can safely return here, because upon map reload, all
968 * PG will be enabled.
970 if (!pp->mpp->pg || !pp->pgindex)
973 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
975 if (pgp->status == PGSTATE_DISABLED) {
976 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
977 dm_enablegroup(pp->mpp->alias, pp->pgindex);
982 mpvec_garbage_collector (struct vectors * vecs)
984 struct multipath * mpp;
990 vector_foreach_slot (vecs->mpvec, mpp, i) {
991 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
992 condlog(2, "%s: remove dead map", mpp->alias);
993 remove_map_and_stop_waiter(mpp, vecs, 1);
1000 defered_failback_tick (vector mpvec)
1002 struct multipath * mpp;
1005 vector_foreach_slot (mpvec, mpp, i) {
1007 * defered failback getting sooner
1009 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1010 mpp->failback_tick--;
1012 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1013 switch_pathgroup(mpp);
1019 retry_count_tick(vector mpvec)
1021 struct multipath *mpp;
1024 vector_foreach_slot (mpvec, mpp, i) {
1025 if (mpp->retry_tick) {
1026 mpp->stat_total_queueing_time++;
1027 condlog(4, "%s: Retrying.. No active path", mpp->alias);
1028 if(--mpp->retry_tick == 0) {
1029 dm_queue_if_no_path(mpp->alias, 0);
1030 condlog(2, "%s: Disable queueing", mpp->alias);
1036 int update_prio(struct path *pp, int refresh_all)
1040 struct pathgroup * pgp;
1041 int i, j, changed = 0;
1044 vector_foreach_slot (pp->mpp->pg, pgp, i) {
1045 vector_foreach_slot (pgp->paths, pp1, j) {
1046 oldpriority = pp1->priority;
1047 pathinfo(pp1, conf->hwtable, DI_PRIO);
1048 if (pp1->priority != oldpriority)
1054 oldpriority = pp->priority;
1055 pathinfo(pp, conf->hwtable, DI_PRIO);
1057 if (pp->priority == oldpriority)
1062 int update_path_groups(struct multipath *mpp, struct vectors *vecs, int refresh)
1066 char params[PARAMS_SIZE];
1068 update_mpp_paths(mpp, vecs->pathvec);
1070 vector_foreach_slot (mpp->paths, pp, i)
1071 pathinfo(pp, conf->hwtable, DI_PRIO);
1074 if (setup_map(mpp, params, PARAMS_SIZE))
1077 mpp->action = ACT_RELOAD;
1078 if (domap(mpp, params) <= 0) {
1079 condlog(0, "%s: failed to update map : %s", mpp->alias,
1084 if (setup_multipath(vecs, mpp) != 0)
1086 sync_map_state(mpp);
1092 check_path (struct vectors * vecs, struct path * pp)
1095 int new_path_up = 0;
1100 if (pp->tick && --pp->tick)
1101 return; /* don't check this path yet */
1104 * provision a next check soonest,
1105 * in case we exit abnormaly from here
1107 pp->tick = conf->checkint;
1109 newstate = path_offline(pp);
1110 if (newstate == PATH_UP)
1111 newstate = get_state(pp, 1);
1113 if (newstate == PATH_WILD || newstate == PATH_UNCHECKED) {
1114 condlog(2, "%s: unusable path", pp->dev);
1115 pathinfo(pp, conf->hwtable, 0);
1119 * Async IO in flight. Keep the previous path state
1120 * and reschedule as soon as possible
1122 if (newstate == PATH_PENDING) {
1127 * Synchronize with kernel state
1129 if (update_multipath_strings(pp->mpp, vecs->pathvec)) {
1130 condlog(1, "%s: Could not synchronize with kernel state\n",
1132 pp->dmstate = PSTATE_UNDEF;
1134 if (newstate != pp->state) {
1135 int oldstate = pp->state;
1136 pp->state = newstate;
1137 LOG_MSG(1, checker_message(&pp->checker));
1140 * upon state change, reset the checkint
1141 * to the shortest delay
1143 pp->checkint = conf->checkint;
1145 if (newstate == PATH_DOWN || newstate == PATH_SHAKY) {
1147 * proactively fail path in the DM
1149 if (oldstate == PATH_UP ||
1150 oldstate == PATH_GHOST)
1156 * cancel scheduled failback
1158 pp->mpp->failback_tick = 0;
1160 pp->mpp->stat_path_failures++;
1164 if(newstate == PATH_UP || newstate == PATH_GHOST){
1165 if ( pp->mpp && pp->mpp->prflag ){
1167 * Check Persistent Reservation.
1169 condlog(2, "%s: checking persistent reservation "
1170 "registration", pp->dev);
1171 mpath_pr_event_handle(pp);
1176 * reinstate this path
1178 if (oldstate != PATH_UP &&
1179 oldstate != PATH_GHOST)
1180 reinstate_path(pp, 1);
1182 reinstate_path(pp, 0);
1187 * if at least one path is up in a group, and
1188 * the group is disabled, re-enable it
1190 if (newstate == PATH_UP)
1193 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1194 if (pp->dmstate == PSTATE_FAILED ||
1195 pp->dmstate == PSTATE_UNDEF) {
1196 /* Clear IO errors */
1197 reinstate_path(pp, 0);
1199 LOG_MSG(4, checker_message(&pp->checker));
1201 * double the next check delay.
1202 * max at conf->max_checkint
1204 if (pp->checkint < (conf->max_checkint / 2))
1205 pp->checkint = 2 * pp->checkint;
1207 pp->checkint = conf->max_checkint;
1209 pp->tick = pp->checkint;
1210 condlog(4, "%s: delay next check %is",
1211 pp->dev_t, pp->tick);
1214 else if (newstate == PATH_DOWN) {
1215 if (conf->log_checker_err == LOG_CHKR_ERR_ONCE)
1216 LOG_MSG(3, checker_message(&pp->checker));
1218 LOG_MSG(2, checker_message(&pp->checker));
1221 pp->state = newstate;
1224 * path prio refreshing
1226 condlog(4, "path prio refresh");
1228 if (update_prio(pp, new_path_up) &&
1229 (pp->mpp->pgpolicyfn == (pgpolicyfn *)group_by_prio) &&
1230 pp->mpp->pgfailback == -FAILBACK_IMMEDIATE)
1231 update_path_groups(pp->mpp, vecs, !new_path_up);
1232 else if (need_switch_pathgroup(pp->mpp, 0)) {
1233 if (pp->mpp->pgfailback > 0 &&
1234 (new_path_up || pp->mpp->failback_tick <= 0))
1235 pp->mpp->failback_tick =
1236 pp->mpp->pgfailback + 1;
1237 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE)
1238 switch_pathgroup(pp->mpp);
1243 checkerloop (void *ap)
1245 struct vectors *vecs;
1251 mlockall(MCL_CURRENT | MCL_FUTURE);
1252 vecs = (struct vectors *)ap;
1253 condlog(2, "path checkers start up");
1256 * init the path check interval
1258 vector_foreach_slot (vecs->pathvec, pp, i) {
1259 pp->checkint = conf->checkint;
1263 block_signal(SIGHUP, &old);
1264 pthread_cleanup_push(cleanup_lock, &vecs->lock);
1266 pthread_testcancel();
1269 if (vecs->pathvec) {
1270 vector_foreach_slot (vecs->pathvec, pp, i) {
1271 check_path(vecs, pp);
1275 defered_failback_tick(vecs->mpvec);
1276 retry_count_tick(vecs->mpvec);
1281 condlog(4, "map garbage collection");
1282 mpvec_garbage_collector(vecs);
1286 lock_cleanup_pop(vecs->lock);
1287 pthread_sigmask(SIG_SETMASK, &old, NULL);
1294 configure (struct vectors * vecs, int start_waiters)
1296 struct multipath * mpp;
1301 if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
1304 if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
1307 if (!(mpvec = vector_alloc()))
1311 * probe for current path (from sysfs) and map (from dm) sets
1313 path_discovery(vecs->pathvec, conf, DI_ALL);
1315 vector_foreach_slot (vecs->pathvec, pp, i){
1316 if (filter_path(conf, pp) > 0){
1317 vector_del_slot(vecs->pathvec, i);
1322 pp->checkint = conf->checkint;
1324 if (map_discovery(vecs))
1328 * create new set of maps & push changed ones into dm
1330 if (coalesce_paths(vecs, mpvec, NULL, 1))
1334 * may need to remove some maps which are no longer relevant
1335 * e.g., due to blacklist changes in conf file
1337 if (coalesce_maps(vecs, mpvec))
1342 sync_maps_state(mpvec);
1343 vector_foreach_slot(mpvec, mpp, i){
1348 * purge dm of old maps
1353 * save new set of maps formed by considering current path state
1355 vector_free(vecs->mpvec);
1356 vecs->mpvec = mpvec;
1359 * start dm event waiter threads for these new maps
1361 vector_foreach_slot(vecs->mpvec, mpp, i) {
1362 if (setup_multipath(vecs, mpp))
1365 if (start_waiter_thread(mpp, vecs))
1372 reconfigure (struct vectors * vecs)
1374 struct config * old = conf;
1378 * free old map and path vectors ... they use old conf state
1380 if (VECTOR_SIZE(vecs->mpvec))
1381 remove_maps_and_stop_waiters(vecs);
1383 if (VECTOR_SIZE(vecs->pathvec))
1384 free_pathvec(vecs->pathvec, FREE_PATHS);
1386 vecs->pathvec = NULL;
1389 if (!load_config(DEFAULT_CONFIGFILE)) {
1390 conf->verbosity = old->verbosity;
1400 static struct vectors *
1403 struct vectors * vecs;
1405 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1411 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1413 if (!vecs->lock.mutex)
1416 pthread_mutex_init(vecs->lock.mutex, NULL);
1417 vecs->lock.depth = 0;
1423 condlog(0, "failed to init paths");
1428 signal_set(int signo, void (*func) (int))
1431 struct sigaction sig;
1432 struct sigaction osig;
1434 sig.sa_handler = func;
1435 sigemptyset(&sig.sa_mask);
1438 r = sigaction(signo, &sig, &osig);
1443 return (osig.sa_handler);
1449 condlog(2, "reconfigure (SIGHUP)");
1451 if (running_state != DAEMON_RUNNING)
1456 unlock(gvecs->lock);
1459 dbg_free_final(NULL);
1472 condlog(3, "SIGUSR1 received");
1478 signal_set(SIGHUP, sighup);
1479 signal_set(SIGUSR1, sigusr1);
1480 signal_set(SIGINT, sigend);
1481 signal_set(SIGTERM, sigend);
1482 signal(SIGPIPE, SIG_IGN);
1489 static struct sched_param sched_param = {
1490 .sched_priority = 99
1493 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1496 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1503 #ifdef OOM_SCORE_ADJ_MIN
1505 char *file = "/proc/self/oom_score_adj";
1506 int score = OOM_SCORE_ADJ_MIN;
1509 char *file = "/proc/self/oom_adj";
1510 int score = OOM_ADJUST_MIN;
1516 if (stat(file, &st) == 0){
1517 fp = fopen(file, "w");
1519 condlog(0, "couldn't fopen %s : %s", file,
1523 fprintf(fp, "%i", score);
1527 if (errno != ENOENT) {
1528 condlog(0, "couldn't stat %s : %s", file,
1532 file = "/proc/self/oom_adj";
1533 score = OOM_ADJUST_MIN;
1535 condlog(0, "couldn't adjust oom score");
1539 child (void * param)
1541 pthread_t check_thr, uevent_thr, uxlsnr_thr, uevq_thr;
1542 pthread_attr_t log_attr, misc_attr;
1543 struct vectors * vecs;
1544 struct multipath * mpp;
1548 mlockall(MCL_CURRENT | MCL_FUTURE);
1550 setup_thread_attr(&misc_attr, 64 * 1024, 1);
1551 setup_thread_attr(&waiter_attr, 32 * 1024, 1);
1554 setup_thread_attr(&log_attr, 64 * 1024, 0);
1555 log_thread_start(&log_attr);
1556 pthread_attr_destroy(&log_attr);
1559 running_state = DAEMON_START;
1561 condlog(2, "--------start up--------");
1562 condlog(2, "read " DEFAULT_CONFIGFILE);
1564 if (load_config(DEFAULT_CONFIGFILE))
1567 if (init_checkers()) {
1568 condlog(0, "failed to initialize checkers");
1572 condlog(0, "failed to initialize prioritizers");
1576 setlogmask(LOG_UPTO(conf->verbosity + 3));
1578 if (conf->max_fds) {
1579 struct rlimit fd_limit;
1581 if (getrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
1582 condlog(0, "can't get open fds limit: %s\n",
1584 fd_limit.rlim_cur = 0;
1585 fd_limit.rlim_max = 0;
1587 if (fd_limit.rlim_cur < conf->max_fds) {
1588 fd_limit.rlim_cur = conf->max_fds;
1589 if (fd_limit.rlim_max < conf->max_fds)
1590 fd_limit.rlim_max = conf->max_fds;
1591 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
1592 condlog(0, "can't set open fds limit to "
1594 fd_limit.rlim_cur, fd_limit.rlim_max,
1597 condlog(3, "set open fds limit to %lu/%lu\n",
1598 fd_limit.rlim_cur, fd_limit.rlim_max);
1607 vecs = gvecs = init_vecs();
1613 udev_set_sync_support(0);
1615 * Start uevent listener early to catch events
1617 if ((rc = pthread_create(&uevent_thr, &misc_attr, ueventloop, vecs))) {
1618 condlog(0, "failed to create uevent thread: %d", rc);
1621 if ((rc = pthread_create(&uxlsnr_thr, &misc_attr, uxlsnrloop, vecs))) {
1622 condlog(0, "failed to create cli listener: %d", rc);
1626 * fetch and configure both paths and multipaths
1629 running_state = DAEMON_CONFIGURE;
1631 if (configure(vecs, 1)) {
1633 condlog(0, "failure during configuration");
1641 if ((rc = pthread_create(&check_thr, &misc_attr, checkerloop, vecs))) {
1642 condlog(0,"failed to create checker loop thread: %d", rc);
1645 if ((rc = pthread_create(&uevq_thr, &misc_attr, uevqloop, vecs))) {
1646 condlog(0, "failed to create uevent dispatcher: %d", rc);
1649 pthread_attr_destroy(&misc_attr);
1651 pthread_mutex_lock(&exit_mutex);
1652 /* Startup complete, create logfile */
1653 if (pidfile_create(DEFAULT_PIDFILE, daemon_pid))
1654 /* Ignore errors, we can live without */
1655 condlog(1, "failed to create pidfile");
1657 running_state = DAEMON_RUNNING;
1658 pthread_cond_wait(&exit_cond, &exit_mutex);
1663 running_state = DAEMON_SHUTDOWN;
1664 block_signal(SIGHUP, NULL);
1666 if (conf->queue_without_daemon == QUE_NO_DAEMON_OFF)
1667 vector_foreach_slot(vecs->mpvec, mpp, i)
1668 dm_queue_if_no_path(mpp->alias, 0);
1669 remove_maps_and_stop_waiters(vecs);
1672 pthread_cancel(check_thr);
1673 pthread_cancel(uevent_thr);
1674 pthread_cancel(uxlsnr_thr);
1675 pthread_cancel(uevq_thr);
1678 free_pathvec(vecs->pathvec, FREE_PATHS);
1679 vecs->pathvec = NULL;
1681 /* Now all the waitevent threads will start rushing in. */
1682 while (vecs->lock.depth > 0) {
1683 sleep (1); /* This is weak. */
1684 condlog(3,"Have %d wait event checkers threads to de-alloc, waiting..\n", vecs->lock.depth);
1686 pthread_mutex_destroy(vecs->lock.mutex);
1687 FREE(vecs->lock.mutex);
1688 vecs->lock.depth = 0;
1689 vecs->lock.mutex = NULL;
1699 /* We're done here */
1700 condlog(3, "unlink pidfile");
1701 unlink(DEFAULT_PIDFILE);
1703 condlog(2, "--------shut down-------");
1709 * Freeing config must be done after condlog() and dm_lib_exit(),
1710 * because logging functions like dlog() and dm_write_log()
1711 * reference the config.
1717 dbg_free_final(NULL);
1729 if( (pid = fork()) < 0){
1730 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1738 if ( (pid = fork()) < 0)
1739 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1744 fprintf(stderr, "cannot chdir to '/', continuing\n");
1746 dev_null_fd = open("/dev/null", O_RDWR);
1747 if (dev_null_fd < 0){
1748 fprintf(stderr, "cannot open /dev/null for input & output : %s\n",
1753 close(STDIN_FILENO);
1754 if (dup(dev_null_fd) < 0) {
1755 fprintf(stderr, "cannot dup /dev/null to stdin : %s\n",
1759 close(STDOUT_FILENO);
1760 if (dup(dev_null_fd) < 0) {
1761 fprintf(stderr, "cannot dup /dev/null to stdout : %s\n",
1765 close(STDERR_FILENO);
1766 if (dup(dev_null_fd) < 0) {
1767 fprintf(stderr, "cannot dup /dev/null to stderr : %s\n",
1772 daemon_pid = getpid();
1777 main (int argc, char *argv[])
1779 extern char *optarg;
1785 running_state = DAEMON_INIT;
1788 if (getuid() != 0) {
1789 fprintf(stderr, "need to be root\n");
1793 /* make sure we don't lock any path */
1795 fprintf(stderr, "can't chdir to root directory : %s\n",
1797 umask(umask(077) | 022);
1799 conf = alloc_config();
1804 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1808 //debug=1; /* ### comment me out ### */
1811 if (sizeof(optarg) > sizeof(char *) ||
1812 !isdigit(optarg[0]))
1815 conf->verbosity = atoi(optarg);
1824 if (optind < argc) {
1829 while (optind < argc) {
1830 if (strchr(argv[optind], ' '))
1831 c += snprintf(c, s + CMDSIZE - c, "\"%s\" ", argv[optind]);
1833 c += snprintf(c, s + CMDSIZE - c, "%s ", argv[optind]);
1836 c += snprintf(c, s + CMDSIZE - c, "\n");
1854 return (child(NULL));
1857 void * mpath_pr_event_handler_fn (void * pathp )
1859 struct multipath * mpp;
1860 int i,j, ret, isFound;
1861 struct path * pp = (struct path *)pathp;
1862 unsigned char *keyp;
1864 struct prout_param_descriptor *param;
1865 struct prin_resp *resp;
1869 resp = mpath_alloc_prin_response(MPATH_PRIN_RKEY_SA);
1871 condlog(0,"%s Alloc failed for prin response \n", pp->dev);
1875 ret = prin_do_scsi_ioctl(pp->dev, MPATH_PRIN_RKEY_SA, resp, 0);
1876 if (ret != MPATH_PR_SUCCESS )
1878 condlog(0,"%s : pr in read keys service action failed. Error=%d\n", pp->dev, ret);
1882 condlog(3, " event pr=%d addlen=%d\n",resp->prin_descriptor.prin_readkeys.prgeneration,
1883 resp->prin_descriptor.prin_readkeys.additional_length );
1885 if (resp->prin_descriptor.prin_readkeys.additional_length == 0 )
1887 condlog(1, "%s: No key found. Device may not be registered.", pp->dev);
1888 ret = MPATH_PR_SUCCESS;
1892 keyp = (unsigned char *)mpp->reservation_key;
1893 for (j = 0; j < 8; ++j) {
1899 condlog(2, "Multipath reservation_key: 0x%" PRIx64 " ", prkey);
1902 for (i = 0; i < resp->prin_descriptor.prin_readkeys.additional_length/8; i++ )
1904 condlog(2, "PR IN READKEYS[%d] reservation key:\n",i);
1905 dumpHex((char *)&resp->prin_descriptor.prin_readkeys.key_list[i*8], 8 , -1);
1906 if (!memcmp(mpp->reservation_key, &resp->prin_descriptor.prin_readkeys.key_list[i*8], 8))
1908 condlog(2, "%s: pr key found in prin readkeys response", mpp->alias);
1915 condlog(0, "%s: Either device not registered or ", pp->dev);
1916 condlog(0, "host is not authorised for registration. Skip path\n");
1917 ret = MPATH_PR_OTHER;
1921 param= malloc(sizeof(struct prout_param_descriptor));
1922 memset(param, 0 , sizeof(struct prout_param_descriptor));
1924 for (j = 7; j >= 0; --j) {
1925 param->sa_key[j] = (prkey & 0xff);
1928 param->num_transportid = 0;
1930 condlog(3, "device %s:%s \n", pp->dev, pp->mpp->wwid);
1932 ret = prout_do_scsi_ioctl(pp->dev, MPATH_PROUT_REG_IGN_SA, 0, 0, param, 0);
1933 if (ret != MPATH_PR_SUCCESS )
1935 condlog(0,"%s: Reservation registration failed. Error: %d\n", pp->dev, ret);
1945 int mpath_pr_event_handle(struct path *pp)
1949 pthread_attr_t attr;
1950 struct multipath * mpp;
1954 if (!mpp->reservation_key)
1957 pthread_attr_init(&attr);
1958 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1960 rc = pthread_create(&thread, NULL , mpath_pr_event_handler_fn, pp);
1962 condlog(0, "%s: ERROR; return code from pthread_create() is %d\n", pp->dev, rc);
1965 pthread_attr_destroy(&attr);
1966 rc = pthread_join(thread, NULL);