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",
394 struct udev_device *udevice;
397 * get path vital state
399 udevice = udev_device_ref(uev->udev);
400 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
402 condlog(0, "%s: failed to store path info",
404 udev_device_unref(udevice);
407 pp->checkint = conf->checkint;
410 return (ev_add_path(pp, vecs) != 1)? 0 : 1;
420 ev_add_path (struct path * pp, struct vectors * vecs)
422 struct multipath * mpp;
423 char empty_buff[WWID_SIZE] = {0};
424 char params[PARAMS_SIZE] = {0};
426 int start_waiter = 0;
429 * need path UID to go any further
431 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
432 condlog(0, "%s: failed to get path uid", pp->dev);
433 goto fail; /* leave path added to pathvec */
435 if (filter_path(conf, pp) > 0){
436 int i = find_slot(vecs->pathvec, (void *)pp);
438 vector_del_slot(vecs->pathvec, i);
442 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
445 if ((!pp->size) || (mpp->size != pp->size)) {
447 condlog(0, "%s: failed to add new path %s, "
449 mpp->alias, pp->dev);
451 condlog(0, "%s: failed to add new path %s, "
452 "device size mismatch",
453 mpp->alias, pp->dev);
454 int i = find_slot(vecs->pathvec, (void *)pp);
456 vector_del_slot(vecs->pathvec, i);
461 condlog(4,"%s: adopting all paths for path %s",
462 mpp->alias, pp->dev);
463 if (adopt_paths(vecs->pathvec, mpp, 1))
464 goto fail; /* leave path added to pathvec */
466 verify_paths(mpp, vecs, NULL);
467 mpp->flush_on_last_del = FLUSH_UNDEF;
468 mpp->action = ACT_RELOAD;
472 condlog(0, "%s: failed to create new map,"
473 " device size is 0 ", pp->dev);
474 int i = find_slot(vecs->pathvec, (void *)pp);
476 vector_del_slot(vecs->pathvec, i);
481 condlog(4,"%s: creating new map", pp->dev);
482 if ((mpp = add_map_with_path(vecs, pp, 1))) {
483 mpp->action = ACT_CREATE;
485 * We don't depend on ACT_CREATE, as domap will
486 * set it to ACT_NOTHING when complete.
491 goto fail; /* leave path added to pathvec */
494 /* persistent reseravtion check*/
495 mpath_pr_event_handle(pp);
498 * push the map to the device-mapper
500 if (setup_map(mpp, params, PARAMS_SIZE)) {
501 condlog(0, "%s: failed to setup map for addition of new "
502 "path %s", mpp->alias, pp->dev);
506 * reload the map for the multipath mapped device
508 if (domap(mpp, params) <= 0) {
509 condlog(0, "%s: failed in domap for addition of new "
510 "path %s", mpp->alias, pp->dev);
512 * deal with asynchronous uevents :((
514 if (mpp->action == ACT_RELOAD && retries-- > 0) {
515 condlog(0, "%s: uev_add_path sleep", mpp->alias);
517 update_mpp_paths(mpp, vecs->pathvec);
520 else if (mpp->action == ACT_RELOAD)
521 condlog(0, "%s: giving up reload", mpp->alias);
528 * update our state from kernel regardless of create or reload
530 if (setup_multipath(vecs, mpp))
531 goto fail; /* if setup_multipath fails, it removes the map */
535 if ((mpp->action == ACT_CREATE ||
536 (mpp->action == ACT_NOTHING && start_waiter && !mpp->waiter)) &&
537 start_waiter_thread(mpp, vecs))
541 condlog(2, "%s path added to devmap %s", pp->dev, mpp->alias);
548 remove_map(mpp, vecs, 1);
555 uev_remove_path (struct uevent *uev, struct vectors * vecs)
559 condlog(2, "%s: remove path (uevent)", uev->kernel);
560 pp = find_path_by_dev(vecs->pathvec, uev->kernel);
563 /* Not an error; path might have been purged earlier */
564 condlog(0, "%s: path already removed", uev->kernel);
568 return ev_remove_path(pp, vecs);
572 ev_remove_path (struct path *pp, struct vectors * vecs)
574 struct multipath * mpp;
576 char params[PARAMS_SIZE] = {0};
579 * avoid referring to the map of an orphaned path
581 if ((mpp = pp->mpp)) {
583 * transform the mp->pg vector of vectors of paths
584 * into a mp->params string to feed the device-mapper
586 if (update_mpp_paths(mpp, vecs->pathvec)) {
587 condlog(0, "%s: failed to update paths",
591 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
592 vector_del_slot(mpp->paths, i);
595 * remove the map IFF removing the last path
597 if (VECTOR_SIZE(mpp->paths) == 0) {
598 char alias[WWID_SIZE];
601 * flush_map will fail if the device is open
603 strncpy(alias, mpp->alias, WWID_SIZE);
604 if (mpp->flush_on_last_del == FLUSH_ENABLED) {
605 condlog(2, "%s Last path deleted, disabling queueing", mpp->alias);
607 mpp->no_path_retry = NO_PATH_RETRY_FAIL;
608 mpp->flush_on_last_del = FLUSH_IN_PROGRESS;
609 dm_queue_if_no_path(mpp->alias, 0);
611 if (!flush_map(mpp, vecs)) {
612 condlog(2, "%s: removed map after"
613 " removing all paths",
619 * Not an error, continue
623 if (setup_map(mpp, params, PARAMS_SIZE)) {
624 condlog(0, "%s: failed to setup map for"
625 " removal of path %s", mpp->alias, pp->dev);
631 mpp->action = ACT_RELOAD;
632 if (domap(mpp, params) <= 0) {
633 condlog(0, "%s: failed in domap for "
634 "removal of path %s",
635 mpp->alias, pp->dev);
639 * update our state from kernel
641 if (setup_multipath(vecs, mpp)) {
646 condlog(2, "%s: path removed from map %s",
647 pp->dev, mpp->alias);
652 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
653 vector_del_slot(vecs->pathvec, i);
660 remove_map_and_stop_waiter(mpp, vecs, 1);
665 uev_update_path (struct uevent *uev, struct vectors * vecs)
669 ro = uevent_get_disk_ro(uev);
674 condlog(2, "%s: update path write_protect to '%d' (uevent)",
676 pp = find_path_by_dev(vecs->pathvec, uev->kernel);
678 condlog(0, "%s: spurious uevent, path not found",
683 retval = reload_map(vecs, pp->mpp);
685 condlog(2, "%s: map %s reloaded (retval %d)",
686 uev->kernel, pp->mpp->alias, retval);
694 map_discovery (struct vectors * vecs)
696 struct multipath * mpp;
699 if (dm_get_maps(vecs->mpvec))
702 vector_foreach_slot (vecs->mpvec, mpp, i)
703 if (setup_multipath(vecs, mpp))
710 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
712 struct vectors * vecs;
717 vecs = (struct vectors *)trigger_data;
719 pthread_cleanup_push(cleanup_lock, &vecs->lock);
721 pthread_testcancel();
723 r = parse_cmd(str, reply, len, vecs);
726 *reply = STRDUP("fail\n");
727 *len = strlen(*reply) + 1;
730 else if (!r && *len == 0) {
731 *reply = STRDUP("ok\n");
732 *len = strlen(*reply) + 1;
735 /* else if (r < 0) leave *reply alone */
737 lock_cleanup_pop(vecs->lock);
742 uev_discard(char * devpath)
748 * keep only block devices, discard partitions
750 tmp = strstr(devpath, "/block/");
752 condlog(4, "no /block/ in '%s'", devpath);
755 if (sscanf(tmp, "/block/%10s", a) != 1 ||
756 sscanf(tmp, "/block/%10[^/]/%10s", a, b) == 2) {
757 condlog(4, "discard event on %s", devpath);
764 uev_trigger (struct uevent * uev, void * trigger_data)
767 struct vectors * vecs;
769 vecs = (struct vectors *)trigger_data;
771 if (uev_discard(uev->devpath))
774 pthread_cleanup_push(cleanup_lock, &vecs->lock);
776 pthread_testcancel();
780 * Add events are ignored here as the tables
781 * are not fully initialised then.
783 if (!strncmp(uev->kernel, "dm-", 3)) {
784 if (!strncmp(uev->action, "change", 6)) {
785 r = uev_add_map(uev, vecs);
788 if (!strncmp(uev->action, "remove", 6)) {
789 r = uev_remove_map(uev, vecs);
796 * path add/remove event
798 if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
802 if (!strncmp(uev->action, "add", 3)) {
803 r = uev_add_path(uev, vecs);
806 if (!strncmp(uev->action, "remove", 6)) {
807 r = uev_remove_path(uev, vecs);
810 if (!strncmp(uev->action, "change", 6)) {
811 r = uev_update_path(uev, vecs);
816 lock_cleanup_pop(vecs->lock);
821 ueventloop (void * ap)
823 block_signal(SIGUSR1, NULL);
824 block_signal(SIGHUP, NULL);
827 condlog(0, "error starting uevent listener");
835 block_signal(SIGUSR1, NULL);
836 block_signal(SIGHUP, NULL);
838 if (uevent_dispatch(&uev_trigger, ap))
839 condlog(0, "error starting uevent dispatcher");
844 uxlsnrloop (void * ap)
846 block_signal(SIGUSR1, NULL);
847 block_signal(SIGHUP, NULL);
852 set_handler_callback(LIST+PATHS, cli_list_paths);
853 set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt);
854 set_handler_callback(LIST+MAPS, cli_list_maps);
855 set_handler_callback(LIST+STATUS, cli_list_status);
856 set_handler_callback(LIST+DAEMON, cli_list_daemon);
857 set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
858 set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
859 set_handler_callback(LIST+MAPS+FMT, cli_list_maps_fmt);
860 set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
861 set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
862 set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
863 set_handler_callback(LIST+CONFIG, cli_list_config);
864 set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
865 set_handler_callback(LIST+DEVICES, cli_list_devices);
866 set_handler_callback(LIST+WILDCARDS, cli_list_wildcards);
867 set_handler_callback(ADD+PATH, cli_add_path);
868 set_handler_callback(DEL+PATH, cli_del_path);
869 set_handler_callback(ADD+MAP, cli_add_map);
870 set_handler_callback(DEL+MAP, cli_del_map);
871 set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
872 set_handler_callback(RECONFIGURE, cli_reconfigure);
873 set_handler_callback(SUSPEND+MAP, cli_suspend);
874 set_handler_callback(RESUME+MAP, cli_resume);
875 set_handler_callback(RESIZE+MAP, cli_resize);
876 set_handler_callback(RELOAD+MAP, cli_reload);
877 set_handler_callback(RESET+MAP, cli_reassign);
878 set_handler_callback(REINSTATE+PATH, cli_reinstate);
879 set_handler_callback(FAIL+PATH, cli_fail);
880 set_handler_callback(DISABLEQ+MAP, cli_disable_queueing);
881 set_handler_callback(RESTOREQ+MAP, cli_restore_queueing);
882 set_handler_callback(DISABLEQ+MAPS, cli_disable_all_queueing);
883 set_handler_callback(RESTOREQ+MAPS, cli_restore_all_queueing);
884 set_handler_callback(QUIT, cli_quit);
885 set_handler_callback(SHUTDOWN, cli_shutdown);
886 set_handler_callback(GETPRSTATUS+MAP, cli_getprstatus);
887 set_handler_callback(SETPRSTATUS+MAP, cli_setprstatus);
888 set_handler_callback(UNSETPRSTATUS+MAP, cli_unsetprstatus);
891 uxsock_listen(&uxsock_trigger, ap);
897 exit_daemon (int status)
900 fprintf(stderr, "bad exit status. see daemon.log\n");
902 condlog(3, "unlink pidfile");
903 unlink(DEFAULT_PIDFILE);
905 pthread_mutex_lock(&exit_mutex);
906 pthread_cond_signal(&exit_cond);
907 pthread_mutex_unlock(&exit_mutex);
915 switch (running_state) {
920 case DAEMON_CONFIGURE:
924 case DAEMON_SHUTDOWN:
931 fail_path (struct path * pp, int del_active)
936 condlog(2, "checker failed path %s in map %s",
937 pp->dev_t, pp->mpp->alias);
939 dm_fail_path(pp->mpp->alias, pp->dev_t);
941 update_queue_mode_del_path(pp->mpp);
945 * caller must have locked the path list before calling that function
948 reinstate_path (struct path * pp, int add_active)
953 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
954 condlog(0, "%s: reinstate failed", pp->dev_t);
956 condlog(2, "%s: reinstated", pp->dev_t);
958 update_queue_mode_add_path(pp->mpp);
963 enable_group(struct path * pp)
965 struct pathgroup * pgp;
968 * if path is added through uev_add_path, pgindex can be unset.
969 * next update_strings() will set it, upon map reload event.
971 * we can safely return here, because upon map reload, all
972 * PG will be enabled.
974 if (!pp->mpp->pg || !pp->pgindex)
977 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
979 if (pgp->status == PGSTATE_DISABLED) {
980 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
981 dm_enablegroup(pp->mpp->alias, pp->pgindex);
986 mpvec_garbage_collector (struct vectors * vecs)
988 struct multipath * mpp;
994 vector_foreach_slot (vecs->mpvec, mpp, i) {
995 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
996 condlog(2, "%s: remove dead map", mpp->alias);
997 remove_map_and_stop_waiter(mpp, vecs, 1);
1004 defered_failback_tick (vector mpvec)
1006 struct multipath * mpp;
1009 vector_foreach_slot (mpvec, mpp, i) {
1011 * defered failback getting sooner
1013 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1014 mpp->failback_tick--;
1016 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1017 switch_pathgroup(mpp);
1023 retry_count_tick(vector mpvec)
1025 struct multipath *mpp;
1028 vector_foreach_slot (mpvec, mpp, i) {
1029 if (mpp->retry_tick) {
1030 mpp->stat_total_queueing_time++;
1031 condlog(4, "%s: Retrying.. No active path", mpp->alias);
1032 if(--mpp->retry_tick == 0) {
1033 dm_queue_if_no_path(mpp->alias, 0);
1034 condlog(2, "%s: Disable queueing", mpp->alias);
1040 int update_prio(struct path *pp, int refresh_all)
1044 struct pathgroup * pgp;
1045 int i, j, changed = 0;
1048 vector_foreach_slot (pp->mpp->pg, pgp, i) {
1049 vector_foreach_slot (pgp->paths, pp1, j) {
1050 oldpriority = pp1->priority;
1051 pathinfo(pp1, conf->hwtable, DI_PRIO);
1052 if (pp1->priority != oldpriority)
1058 oldpriority = pp->priority;
1059 pathinfo(pp, conf->hwtable, DI_PRIO);
1061 if (pp->priority == oldpriority)
1066 int update_path_groups(struct multipath *mpp, struct vectors *vecs, int refresh)
1070 char params[PARAMS_SIZE];
1072 update_mpp_paths(mpp, vecs->pathvec);
1074 vector_foreach_slot (mpp->paths, pp, i)
1075 pathinfo(pp, conf->hwtable, DI_PRIO);
1078 if (setup_map(mpp, params, PARAMS_SIZE))
1081 mpp->action = ACT_RELOAD;
1082 if (domap(mpp, params) <= 0) {
1083 condlog(0, "%s: failed to update map : %s", mpp->alias,
1088 if (setup_multipath(vecs, mpp) != 0)
1090 sync_map_state(mpp);
1096 check_path (struct vectors * vecs, struct path * pp)
1099 int new_path_up = 0;
1104 if (pp->tick && --pp->tick)
1105 return; /* don't check this path yet */
1108 * provision a next check soonest,
1109 * in case we exit abnormaly from here
1111 pp->tick = conf->checkint;
1113 newstate = path_offline(pp);
1114 if (newstate == PATH_UP)
1115 newstate = get_state(pp, 1);
1117 if (newstate == PATH_WILD || newstate == PATH_UNCHECKED) {
1118 condlog(2, "%s: unusable path", pp->dev);
1119 pathinfo(pp, conf->hwtable, 0);
1123 * Async IO in flight. Keep the previous path state
1124 * and reschedule as soon as possible
1126 if (newstate == PATH_PENDING) {
1131 * Synchronize with kernel state
1133 if (update_multipath_strings(pp->mpp, vecs->pathvec)) {
1134 condlog(1, "%s: Could not synchronize with kernel state\n",
1136 pp->dmstate = PSTATE_UNDEF;
1138 if (newstate != pp->state) {
1139 int oldstate = pp->state;
1140 pp->state = newstate;
1141 LOG_MSG(1, checker_message(&pp->checker));
1144 * upon state change, reset the checkint
1145 * to the shortest delay
1147 pp->checkint = conf->checkint;
1149 if (newstate == PATH_DOWN || newstate == PATH_SHAKY) {
1151 * proactively fail path in the DM
1153 if (oldstate == PATH_UP ||
1154 oldstate == PATH_GHOST)
1160 * cancel scheduled failback
1162 pp->mpp->failback_tick = 0;
1164 pp->mpp->stat_path_failures++;
1168 if(newstate == PATH_UP || newstate == PATH_GHOST){
1169 if ( pp->mpp && pp->mpp->prflag ){
1171 * Check Persistent Reservation.
1173 condlog(2, "%s: checking persistent reservation "
1174 "registration", pp->dev);
1175 mpath_pr_event_handle(pp);
1180 * reinstate this path
1182 if (oldstate != PATH_UP &&
1183 oldstate != PATH_GHOST)
1184 reinstate_path(pp, 1);
1186 reinstate_path(pp, 0);
1191 * if at least one path is up in a group, and
1192 * the group is disabled, re-enable it
1194 if (newstate == PATH_UP)
1197 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1198 if (pp->dmstate == PSTATE_FAILED ||
1199 pp->dmstate == PSTATE_UNDEF) {
1200 /* Clear IO errors */
1201 reinstate_path(pp, 0);
1203 LOG_MSG(4, checker_message(&pp->checker));
1205 * double the next check delay.
1206 * max at conf->max_checkint
1208 if (pp->checkint < (conf->max_checkint / 2))
1209 pp->checkint = 2 * pp->checkint;
1211 pp->checkint = conf->max_checkint;
1213 pp->tick = pp->checkint;
1214 condlog(4, "%s: delay next check %is",
1215 pp->dev_t, pp->tick);
1218 else if (newstate == PATH_DOWN) {
1219 if (conf->log_checker_err == LOG_CHKR_ERR_ONCE)
1220 LOG_MSG(3, checker_message(&pp->checker));
1222 LOG_MSG(2, checker_message(&pp->checker));
1225 pp->state = newstate;
1228 * path prio refreshing
1230 condlog(4, "path prio refresh");
1232 if (update_prio(pp, new_path_up) &&
1233 (pp->mpp->pgpolicyfn == (pgpolicyfn *)group_by_prio) &&
1234 pp->mpp->pgfailback == -FAILBACK_IMMEDIATE)
1235 update_path_groups(pp->mpp, vecs, !new_path_up);
1236 else if (need_switch_pathgroup(pp->mpp, 0)) {
1237 if (pp->mpp->pgfailback > 0 &&
1238 (new_path_up || pp->mpp->failback_tick <= 0))
1239 pp->mpp->failback_tick =
1240 pp->mpp->pgfailback + 1;
1241 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE)
1242 switch_pathgroup(pp->mpp);
1247 checkerloop (void *ap)
1249 struct vectors *vecs;
1255 mlockall(MCL_CURRENT | MCL_FUTURE);
1256 vecs = (struct vectors *)ap;
1257 condlog(2, "path checkers start up");
1260 * init the path check interval
1262 vector_foreach_slot (vecs->pathvec, pp, i) {
1263 pp->checkint = conf->checkint;
1267 block_signal(SIGHUP, &old);
1268 pthread_cleanup_push(cleanup_lock, &vecs->lock);
1270 pthread_testcancel();
1273 if (vecs->pathvec) {
1274 vector_foreach_slot (vecs->pathvec, pp, i) {
1275 check_path(vecs, pp);
1279 defered_failback_tick(vecs->mpvec);
1280 retry_count_tick(vecs->mpvec);
1285 condlog(4, "map garbage collection");
1286 mpvec_garbage_collector(vecs);
1290 lock_cleanup_pop(vecs->lock);
1291 pthread_sigmask(SIG_SETMASK, &old, NULL);
1298 configure (struct vectors * vecs, int start_waiters)
1300 struct multipath * mpp;
1305 if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
1308 if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
1311 if (!(mpvec = vector_alloc()))
1315 * probe for current path (from sysfs) and map (from dm) sets
1317 path_discovery(vecs->pathvec, conf, DI_ALL);
1319 vector_foreach_slot (vecs->pathvec, pp, i){
1320 if (filter_path(conf, pp) > 0){
1321 vector_del_slot(vecs->pathvec, i);
1326 pp->checkint = conf->checkint;
1328 if (map_discovery(vecs))
1332 * create new set of maps & push changed ones into dm
1334 if (coalesce_paths(vecs, mpvec, NULL, 1))
1338 * may need to remove some maps which are no longer relevant
1339 * e.g., due to blacklist changes in conf file
1341 if (coalesce_maps(vecs, mpvec))
1346 sync_maps_state(mpvec);
1347 vector_foreach_slot(mpvec, mpp, i){
1352 * purge dm of old maps
1357 * save new set of maps formed by considering current path state
1359 vector_free(vecs->mpvec);
1360 vecs->mpvec = mpvec;
1363 * start dm event waiter threads for these new maps
1365 vector_foreach_slot(vecs->mpvec, mpp, i) {
1366 if (setup_multipath(vecs, mpp))
1369 if (start_waiter_thread(mpp, vecs))
1376 reconfigure (struct vectors * vecs)
1378 struct config * old = conf;
1382 * free old map and path vectors ... they use old conf state
1384 if (VECTOR_SIZE(vecs->mpvec))
1385 remove_maps_and_stop_waiters(vecs);
1387 if (VECTOR_SIZE(vecs->pathvec))
1388 free_pathvec(vecs->pathvec, FREE_PATHS);
1390 vecs->pathvec = NULL;
1393 if (!load_config(DEFAULT_CONFIGFILE)) {
1394 conf->verbosity = old->verbosity;
1404 static struct vectors *
1407 struct vectors * vecs;
1409 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1415 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1417 if (!vecs->lock.mutex)
1420 pthread_mutex_init(vecs->lock.mutex, NULL);
1421 vecs->lock.depth = 0;
1427 condlog(0, "failed to init paths");
1432 signal_set(int signo, void (*func) (int))
1435 struct sigaction sig;
1436 struct sigaction osig;
1438 sig.sa_handler = func;
1439 sigemptyset(&sig.sa_mask);
1442 r = sigaction(signo, &sig, &osig);
1447 return (osig.sa_handler);
1453 condlog(2, "reconfigure (SIGHUP)");
1455 if (running_state != DAEMON_RUNNING)
1460 unlock(gvecs->lock);
1463 dbg_free_final(NULL);
1476 condlog(3, "SIGUSR1 received");
1482 signal_set(SIGHUP, sighup);
1483 signal_set(SIGUSR1, sigusr1);
1484 signal_set(SIGINT, sigend);
1485 signal_set(SIGTERM, sigend);
1486 signal(SIGPIPE, SIG_IGN);
1493 static struct sched_param sched_param = {
1494 .sched_priority = 99
1497 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1500 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1507 #ifdef OOM_SCORE_ADJ_MIN
1509 char *file = "/proc/self/oom_score_adj";
1510 int score = OOM_SCORE_ADJ_MIN;
1513 char *file = "/proc/self/oom_adj";
1514 int score = OOM_ADJUST_MIN;
1520 if (stat(file, &st) == 0){
1521 fp = fopen(file, "w");
1523 condlog(0, "couldn't fopen %s : %s", file,
1527 fprintf(fp, "%i", score);
1531 if (errno != ENOENT) {
1532 condlog(0, "couldn't stat %s : %s", file,
1536 file = "/proc/self/oom_adj";
1537 score = OOM_ADJUST_MIN;
1539 condlog(0, "couldn't adjust oom score");
1543 child (void * param)
1545 pthread_t check_thr, uevent_thr, uxlsnr_thr, uevq_thr;
1546 pthread_attr_t log_attr, misc_attr;
1547 struct vectors * vecs;
1548 struct multipath * mpp;
1552 mlockall(MCL_CURRENT | MCL_FUTURE);
1554 setup_thread_attr(&misc_attr, 64 * 1024, 1);
1555 setup_thread_attr(&waiter_attr, 32 * 1024, 1);
1558 setup_thread_attr(&log_attr, 64 * 1024, 0);
1559 log_thread_start(&log_attr);
1560 pthread_attr_destroy(&log_attr);
1563 running_state = DAEMON_START;
1565 condlog(2, "--------start up--------");
1566 condlog(2, "read " DEFAULT_CONFIGFILE);
1568 if (load_config(DEFAULT_CONFIGFILE))
1571 if (init_checkers()) {
1572 condlog(0, "failed to initialize checkers");
1576 condlog(0, "failed to initialize prioritizers");
1580 setlogmask(LOG_UPTO(conf->verbosity + 3));
1582 if (conf->max_fds) {
1583 struct rlimit fd_limit;
1585 if (getrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
1586 condlog(0, "can't get open fds limit: %s\n",
1588 fd_limit.rlim_cur = 0;
1589 fd_limit.rlim_max = 0;
1591 if (fd_limit.rlim_cur < conf->max_fds) {
1592 fd_limit.rlim_cur = conf->max_fds;
1593 if (fd_limit.rlim_max < conf->max_fds)
1594 fd_limit.rlim_max = conf->max_fds;
1595 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
1596 condlog(0, "can't set open fds limit to "
1598 fd_limit.rlim_cur, fd_limit.rlim_max,
1601 condlog(3, "set open fds limit to %lu/%lu\n",
1602 fd_limit.rlim_cur, fd_limit.rlim_max);
1611 vecs = gvecs = init_vecs();
1617 udev_set_sync_support(0);
1619 * Start uevent listener early to catch events
1621 if ((rc = pthread_create(&uevent_thr, &misc_attr, ueventloop, vecs))) {
1622 condlog(0, "failed to create uevent thread: %d", rc);
1625 if ((rc = pthread_create(&uxlsnr_thr, &misc_attr, uxlsnrloop, vecs))) {
1626 condlog(0, "failed to create cli listener: %d", rc);
1630 * fetch and configure both paths and multipaths
1633 running_state = DAEMON_CONFIGURE;
1635 if (configure(vecs, 1)) {
1637 condlog(0, "failure during configuration");
1645 if ((rc = pthread_create(&check_thr, &misc_attr, checkerloop, vecs))) {
1646 condlog(0,"failed to create checker loop thread: %d", rc);
1649 if ((rc = pthread_create(&uevq_thr, &misc_attr, uevqloop, vecs))) {
1650 condlog(0, "failed to create uevent dispatcher: %d", rc);
1653 pthread_attr_destroy(&misc_attr);
1655 pthread_mutex_lock(&exit_mutex);
1656 /* Startup complete, create logfile */
1657 if (pidfile_create(DEFAULT_PIDFILE, daemon_pid))
1658 /* Ignore errors, we can live without */
1659 condlog(1, "failed to create pidfile");
1661 running_state = DAEMON_RUNNING;
1662 pthread_cond_wait(&exit_cond, &exit_mutex);
1667 running_state = DAEMON_SHUTDOWN;
1668 block_signal(SIGHUP, NULL);
1670 if (conf->queue_without_daemon == QUE_NO_DAEMON_OFF)
1671 vector_foreach_slot(vecs->mpvec, mpp, i)
1672 dm_queue_if_no_path(mpp->alias, 0);
1673 remove_maps_and_stop_waiters(vecs);
1676 pthread_cancel(check_thr);
1677 pthread_cancel(uevent_thr);
1678 pthread_cancel(uxlsnr_thr);
1679 pthread_cancel(uevq_thr);
1682 free_pathvec(vecs->pathvec, FREE_PATHS);
1683 vecs->pathvec = NULL;
1685 /* Now all the waitevent threads will start rushing in. */
1686 while (vecs->lock.depth > 0) {
1687 sleep (1); /* This is weak. */
1688 condlog(3,"Have %d wait event checkers threads to de-alloc, waiting..\n", vecs->lock.depth);
1690 pthread_mutex_destroy(vecs->lock.mutex);
1691 FREE(vecs->lock.mutex);
1692 vecs->lock.depth = 0;
1693 vecs->lock.mutex = NULL;
1703 /* We're done here */
1704 condlog(3, "unlink pidfile");
1705 unlink(DEFAULT_PIDFILE);
1707 condlog(2, "--------shut down-------");
1713 * Freeing config must be done after condlog() and dm_lib_exit(),
1714 * because logging functions like dlog() and dm_write_log()
1715 * reference the config.
1721 dbg_free_final(NULL);
1733 if( (pid = fork()) < 0){
1734 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1742 if ( (pid = fork()) < 0)
1743 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1748 fprintf(stderr, "cannot chdir to '/', continuing\n");
1750 dev_null_fd = open("/dev/null", O_RDWR);
1751 if (dev_null_fd < 0){
1752 fprintf(stderr, "cannot open /dev/null for input & output : %s\n",
1757 close(STDIN_FILENO);
1758 if (dup(dev_null_fd) < 0) {
1759 fprintf(stderr, "cannot dup /dev/null to stdin : %s\n",
1763 close(STDOUT_FILENO);
1764 if (dup(dev_null_fd) < 0) {
1765 fprintf(stderr, "cannot dup /dev/null to stdout : %s\n",
1769 close(STDERR_FILENO);
1770 if (dup(dev_null_fd) < 0) {
1771 fprintf(stderr, "cannot dup /dev/null to stderr : %s\n",
1776 daemon_pid = getpid();
1781 main (int argc, char *argv[])
1783 extern char *optarg;
1789 running_state = DAEMON_INIT;
1792 if (getuid() != 0) {
1793 fprintf(stderr, "need to be root\n");
1797 /* make sure we don't lock any path */
1799 fprintf(stderr, "can't chdir to root directory : %s\n",
1801 umask(umask(077) | 022);
1803 conf = alloc_config();
1808 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1812 //debug=1; /* ### comment me out ### */
1815 if (sizeof(optarg) > sizeof(char *) ||
1816 !isdigit(optarg[0]))
1819 conf->verbosity = atoi(optarg);
1828 if (optind < argc) {
1833 while (optind < argc) {
1834 if (strchr(argv[optind], ' '))
1835 c += snprintf(c, s + CMDSIZE - c, "\"%s\" ", argv[optind]);
1837 c += snprintf(c, s + CMDSIZE - c, "%s ", argv[optind]);
1840 c += snprintf(c, s + CMDSIZE - c, "\n");
1858 return (child(NULL));
1861 void * mpath_pr_event_handler_fn (void * pathp )
1863 struct multipath * mpp;
1864 int i,j, ret, isFound;
1865 struct path * pp = (struct path *)pathp;
1866 unsigned char *keyp;
1868 struct prout_param_descriptor *param;
1869 struct prin_resp *resp;
1873 resp = mpath_alloc_prin_response(MPATH_PRIN_RKEY_SA);
1875 condlog(0,"%s Alloc failed for prin response \n", pp->dev);
1879 ret = prin_do_scsi_ioctl(pp->dev, MPATH_PRIN_RKEY_SA, resp, 0);
1880 if (ret != MPATH_PR_SUCCESS )
1882 condlog(0,"%s : pr in read keys service action failed. Error=%d\n", pp->dev, ret);
1886 condlog(3, " event pr=%d addlen=%d\n",resp->prin_descriptor.prin_readkeys.prgeneration,
1887 resp->prin_descriptor.prin_readkeys.additional_length );
1889 if (resp->prin_descriptor.prin_readkeys.additional_length == 0 )
1891 condlog(1, "%s: No key found. Device may not be registered.", pp->dev);
1892 ret = MPATH_PR_SUCCESS;
1896 keyp = (unsigned char *)mpp->reservation_key;
1897 for (j = 0; j < 8; ++j) {
1903 condlog(2, "Multipath reservation_key: 0x%" PRIx64 " ", prkey);
1906 for (i = 0; i < resp->prin_descriptor.prin_readkeys.additional_length/8; i++ )
1908 condlog(2, "PR IN READKEYS[%d] reservation key:\n",i);
1909 dumpHex((char *)&resp->prin_descriptor.prin_readkeys.key_list[i*8], 8 , -1);
1910 if (!memcmp(mpp->reservation_key, &resp->prin_descriptor.prin_readkeys.key_list[i*8], 8))
1912 condlog(2, "%s: pr key found in prin readkeys response", mpp->alias);
1919 condlog(0, "%s: Either device not registered or ", pp->dev);
1920 condlog(0, "host is not authorised for registration. Skip path\n");
1921 ret = MPATH_PR_OTHER;
1925 param= malloc(sizeof(struct prout_param_descriptor));
1926 memset(param, 0 , sizeof(struct prout_param_descriptor));
1928 for (j = 7; j >= 0; --j) {
1929 param->sa_key[j] = (prkey & 0xff);
1932 param->num_transportid = 0;
1934 condlog(3, "device %s:%s \n", pp->dev, pp->mpp->wwid);
1936 ret = prout_do_scsi_ioctl(pp->dev, MPATH_PROUT_REG_IGN_SA, 0, 0, param, 0);
1937 if (ret != MPATH_PR_SUCCESS )
1939 condlog(0,"%s: Reservation registration failed. Error: %d\n", pp->dev, ret);
1949 int mpath_pr_event_handle(struct path *pp)
1953 pthread_attr_t attr;
1954 struct multipath * mpp;
1958 if (!mpp->reservation_key)
1961 pthread_attr_init(&attr);
1962 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1964 rc = pthread_create(&thread, NULL , mpath_pr_event_handler_fn, pp);
1966 condlog(0, "%s: ERROR; return code from pthread_create() is %d\n", pp->dev, rc);
1969 pthread_attr_destroy(&attr);
1970 rc = pthread_join(thread, NULL);