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>
21 #include <systemd/sd-daemon.h>
23 #include <semaphore.h>
24 #include <mpath_persist.h>
43 #include <blacklist.h>
44 #include <structs_vec.h>
46 #include <devmapper.h>
49 #include <discovery.h>
53 #include <switchgroup.h>
55 #include <configure.h>
57 #include <pgpolicies.h>
66 #include "cli_handlers.h"
71 #define FILE_NAME_SIZE 256
74 #define LOG_MSG(a, b) \
77 condlog(a, "%s: %s - path offline", pp->mpp->alias, pp->dev); \
79 condlog(a, "%s: %s - %s", pp->mpp->alias, pp->dev, b); \
82 struct mpath_event_param
85 struct multipath *mpp;
88 unsigned int mpath_mx_alloc_len;
91 enum daemon_status running_state;
94 static sem_t exit_sem;
96 * global copy of vecs for use in sig handlers
98 struct vectors * gvecs;
103 need_switch_pathgroup (struct multipath * mpp, int refresh)
105 struct pathgroup * pgp;
109 if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
113 * Refresh path priority values
116 vector_foreach_slot (mpp->pg, pgp, i)
117 vector_foreach_slot (pgp->paths, pp, j)
118 pathinfo(pp, conf->hwtable, DI_PRIO);
120 mpp->bestpg = select_path_group(mpp);
122 if (mpp->bestpg != mpp->nextpg)
129 switch_pathgroup (struct multipath * mpp)
131 mpp->stat_switchgroup++;
132 dm_switchgroup(mpp->alias, mpp->bestpg);
133 condlog(2, "%s: switch to path group #%i",
134 mpp->alias, mpp->bestpg);
138 coalesce_maps(struct vectors *vecs, vector nmpv)
140 struct multipath * ompp;
141 vector ompv = vecs->mpvec;
144 vector_foreach_slot (ompv, ompp, i) {
145 condlog(3, "%s: coalesce map", ompp->alias);
146 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
148 * remove all current maps not allowed by the
149 * current configuration
151 if (dm_flush_map(ompp->alias)) {
152 condlog(0, "%s: unable to flush devmap",
155 * may be just because the device is open
157 if (setup_multipath(vecs, ompp) != 0) {
161 if (!vector_alloc_slot(nmpv))
164 vector_set_slot(nmpv, ompp);
166 vector_del_slot(ompv, i);
171 condlog(2, "%s devmap removed", ompp->alias);
173 } else if (conf->reassign_maps) {
174 condlog(3, "%s: Reassign existing device-mapper"
175 " devices", ompp->alias);
176 dm_reassign(ompp->alias);
183 sync_map_state(struct multipath *mpp)
185 struct pathgroup *pgp;
192 vector_foreach_slot (mpp->pg, pgp, i){
193 vector_foreach_slot (pgp->paths, pp, j){
194 if (pp->state == PATH_UNCHECKED ||
195 pp->state == PATH_WILD)
197 if ((pp->dmstate == PSTATE_FAILED ||
198 pp->dmstate == PSTATE_UNDEF) &&
199 (pp->state == PATH_UP || pp->state == PATH_GHOST))
200 dm_reinstate_path(mpp->alias, pp->dev_t);
201 else if ((pp->dmstate == PSTATE_ACTIVE ||
202 pp->dmstate == PSTATE_UNDEF) &&
203 (pp->state == PATH_DOWN ||
204 pp->state == PATH_SHAKY))
205 dm_fail_path(mpp->alias, pp->dev_t);
211 sync_maps_state(vector mpvec)
214 struct multipath *mpp;
216 vector_foreach_slot (mpvec, mpp, i)
221 flush_map(struct multipath * mpp, struct vectors * vecs)
224 * clear references to this map before flushing so we can ignore
225 * the spurious uevent we may generate with the dm_flush_map call below
227 if (dm_flush_map(mpp->alias)) {
229 * May not really be an error -- if the map was already flushed
230 * from the device mapper by dmsetup(8) for instance.
232 condlog(0, "%s: can't flush", mpp->alias);
237 condlog(2, "%s: map flushed", mpp->alias);
240 orphan_paths(vecs->pathvec, mpp);
241 remove_map_and_stop_waiter(mpp, vecs, 1);
247 uev_add_map (struct uevent * uev, struct vectors * vecs)
250 int major = -1, minor = -1, rc;
252 condlog(3, "%s: add map (uevent)", uev->kernel);
253 alias = uevent_get_dm_name(uev);
255 condlog(3, "%s: No DM_NAME in uevent", uev->kernel);
256 major = uevent_get_major(uev);
257 minor = uevent_get_minor(uev);
258 alias = dm_mapname(major, minor);
260 condlog(2, "%s: mapname not found for %d:%d",
261 uev->kernel, major, minor);
265 rc = ev_add_map(uev->kernel, alias, vecs);
271 ev_add_map (char * dev, char * alias, struct vectors * vecs)
274 struct multipath * mpp;
278 map_present = dm_map_present(alias);
280 if (map_present && dm_type(alias, TGT_MPATH) <= 0) {
281 condlog(4, "%s: not a multipath map", alias);
285 mpp = find_mp_by_alias(vecs->mpvec, alias);
289 * Not really an error -- we generate our own uevent
290 * if we create a multipath mapped device as a result
293 if (conf->reassign_maps) {
294 condlog(3, "%s: Reassign existing device-mapper devices",
300 condlog(2, "%s: adding map", alias);
303 * now we can register the map
305 if (map_present && (mpp = add_map_without_path(vecs, alias))) {
307 condlog(2, "%s: devmap %s registered", alias, dev);
310 r = get_refwwid(dev, DEV_DEVMAP, vecs->pathvec, &refwwid);
313 r = coalesce_paths(vecs, NULL, refwwid, 0);
318 condlog(2, "%s: devmap %s added", alias, dev);
320 condlog(2, "%s: uev_add_map %s blacklisted", alias, dev);
322 condlog(0, "%s: uev_add_map %s failed", alias, dev);
329 uev_remove_map (struct uevent * uev, struct vectors * vecs)
333 struct multipath *mpp;
335 condlog(2, "%s: remove map (uevent)", uev->kernel);
336 alias = uevent_get_dm_name(uev);
338 condlog(3, "%s: No DM_NAME in uevent, ignoring", uev->kernel);
341 minor = uevent_get_minor(uev);
342 mpp = find_mp_by_minor(vecs->mpvec, minor);
345 condlog(2, "%s: devmap not registered, can't remove",
349 if (strcmp(mpp->alias, alias)) {
350 condlog(2, "%s: minor number mismatch (map %d, event %d)",
351 mpp->alias, mpp->dmi->minor, minor);
355 orphan_paths(vecs->pathvec, mpp);
356 remove_map_and_stop_waiter(mpp, vecs, 1);
363 ev_remove_map (char * devname, char * alias, int minor, struct vectors * vecs)
365 struct multipath * mpp;
367 mpp = find_mp_by_minor(vecs->mpvec, minor);
370 condlog(2, "%s: devmap not registered, can't remove",
374 if (strcmp(mpp->alias, alias)) {
375 condlog(2, "%s: minor number mismatch (map %d, event %d)",
376 mpp->alias, mpp->dmi->minor, minor);
379 return flush_map(mpp, vecs);
383 uev_add_path (struct uevent *uev, struct vectors * vecs)
388 condlog(2, "%s: add path (uevent)", uev->kernel);
389 if (strstr(uev->kernel, "..") != NULL) {
391 * Don't allow relative device names in the pathvec
393 condlog(0, "%s: path name is invalid", uev->kernel);
397 pp = find_path_by_dev(vecs->pathvec, uev->kernel);
399 condlog(0, "%s: spurious uevent, path already in pathvec",
403 if (!strlen(pp->wwid)) {
404 udev_device_unref(pp->udev);
405 pp->udev = udev_device_ref(uev->udev);
406 ret = pathinfo(pp, conf->hwtable,
407 DI_ALL | DI_BLACKLIST);
409 i = find_slot(vecs->pathvec, (void *)pp);
411 vector_del_slot(vecs->pathvec, i);
414 } else if (ret == 1) {
415 condlog(0, "%s: failed to reinitialize path",
422 * get path vital state
424 ret = store_pathinfo(vecs->pathvec, conf->hwtable,
425 uev->udev, DI_ALL, &pp);
429 condlog(0, "%s: failed to store path info",
433 pp->checkint = conf->checkint;
436 return ev_add_path(pp, vecs);
445 ev_add_path (struct path * pp, struct vectors * vecs)
447 struct multipath * mpp;
448 char empty_buff[WWID_SIZE] = {0};
449 char params[PARAMS_SIZE] = {0};
451 int start_waiter = 0;
454 * need path UID to go any further
456 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
457 condlog(0, "%s: failed to get path uid", pp->dev);
458 goto fail; /* leave path added to pathvec */
460 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
463 if ((!pp->size) || (mpp->size != pp->size)) {
465 condlog(0, "%s: failed to add new path %s, "
467 mpp->alias, pp->dev);
469 condlog(0, "%s: failed to add new path %s, "
470 "device size mismatch",
471 mpp->alias, pp->dev);
472 int i = find_slot(vecs->pathvec, (void *)pp);
474 vector_del_slot(vecs->pathvec, i);
479 condlog(4,"%s: adopting all paths for path %s",
480 mpp->alias, pp->dev);
481 if (adopt_paths(vecs->pathvec, mpp, 1))
482 goto fail; /* leave path added to pathvec */
484 verify_paths(mpp, vecs, NULL);
485 mpp->flush_on_last_del = FLUSH_UNDEF;
486 mpp->action = ACT_RELOAD;
490 condlog(0, "%s: failed to create new map,"
491 " device size is 0 ", pp->dev);
492 int i = find_slot(vecs->pathvec, (void *)pp);
494 vector_del_slot(vecs->pathvec, i);
499 condlog(4,"%s: creating new map", pp->dev);
500 if ((mpp = add_map_with_path(vecs, pp, 1))) {
501 mpp->action = ACT_CREATE;
503 * We don't depend on ACT_CREATE, as domap will
504 * set it to ACT_NOTHING when complete.
509 goto fail; /* leave path added to pathvec */
512 /* persistent reseravtion check*/
513 mpath_pr_event_handle(pp);
516 * push the map to the device-mapper
518 if (setup_map(mpp, params, PARAMS_SIZE)) {
519 condlog(0, "%s: failed to setup map for addition of new "
520 "path %s", mpp->alias, pp->dev);
524 * reload the map for the multipath mapped device
526 if (domap(mpp, params) <= 0) {
527 condlog(0, "%s: failed in domap for addition of new "
528 "path %s", mpp->alias, pp->dev);
530 * deal with asynchronous uevents :((
532 if (mpp->action == ACT_RELOAD && retries-- > 0) {
533 condlog(0, "%s: uev_add_path sleep", mpp->alias);
535 update_mpp_paths(mpp, vecs->pathvec);
538 else if (mpp->action == ACT_RELOAD)
539 condlog(0, "%s: giving up reload", mpp->alias);
546 * update our state from kernel regardless of create or reload
548 if (setup_multipath(vecs, mpp))
549 goto fail; /* if setup_multipath fails, it removes the map */
553 if ((mpp->action == ACT_CREATE ||
554 (mpp->action == ACT_NOTHING && start_waiter && !mpp->waiter)) &&
555 start_waiter_thread(mpp, vecs))
559 condlog(2, "%s [%s]: path added to devmap %s",
560 pp->dev, pp->dev_t, mpp->alias);
567 remove_map(mpp, vecs, 1);
569 orphan_path(pp, "failed to add path");
574 uev_remove_path (struct uevent *uev, struct vectors * vecs)
578 condlog(2, "%s: remove path (uevent)", uev->kernel);
579 pp = find_path_by_dev(vecs->pathvec, uev->kernel);
582 /* Not an error; path might have been purged earlier */
583 condlog(0, "%s: path already removed", uev->kernel);
587 return ev_remove_path(pp, vecs);
591 ev_remove_path (struct path *pp, struct vectors * vecs)
593 struct multipath * mpp;
595 char params[PARAMS_SIZE] = {0};
598 * avoid referring to the map of an orphaned path
600 if ((mpp = pp->mpp)) {
602 * transform the mp->pg vector of vectors of paths
603 * into a mp->params string to feed the device-mapper
605 if (update_mpp_paths(mpp, vecs->pathvec)) {
606 condlog(0, "%s: failed to update paths",
610 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
611 vector_del_slot(mpp->paths, i);
614 * remove the map IFF removing the last path
616 if (VECTOR_SIZE(mpp->paths) == 0) {
617 char alias[WWID_SIZE];
620 * flush_map will fail if the device is open
622 strncpy(alias, mpp->alias, WWID_SIZE);
623 if (mpp->flush_on_last_del == FLUSH_ENABLED) {
624 condlog(2, "%s Last path deleted, disabling queueing", mpp->alias);
626 mpp->no_path_retry = NO_PATH_RETRY_FAIL;
627 mpp->flush_on_last_del = FLUSH_IN_PROGRESS;
628 dm_queue_if_no_path(mpp->alias, 0);
630 if (!flush_map(mpp, vecs)) {
631 condlog(2, "%s: removed map after"
632 " removing all paths",
638 * Not an error, continue
642 if (setup_map(mpp, params, PARAMS_SIZE)) {
643 condlog(0, "%s: failed to setup map for"
644 " removal of path %s", mpp->alias, pp->dev);
650 mpp->action = ACT_RELOAD;
651 if (domap(mpp, params) <= 0) {
652 condlog(0, "%s: failed in domap for "
653 "removal of path %s",
654 mpp->alias, pp->dev);
658 * update our state from kernel
660 if (setup_multipath(vecs, mpp)) {
665 condlog(2, "%s [%s]: path removed from map %s",
666 pp->dev, pp->dev_t, mpp->alias);
671 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
672 vector_del_slot(vecs->pathvec, i);
679 remove_map_and_stop_waiter(mpp, vecs, 1);
684 uev_update_path (struct uevent *uev, struct vectors * vecs)
688 ro = uevent_get_disk_ro(uev);
693 condlog(2, "%s: update path write_protect to '%d' (uevent)",
695 pp = find_path_by_dev(vecs->pathvec, uev->kernel);
697 condlog(0, "%s: spurious uevent, path not found",
702 retval = reload_map(vecs, pp->mpp, 0);
704 condlog(2, "%s: map %s reloaded (retval %d)",
705 uev->kernel, pp->mpp->alias, retval);
714 map_discovery (struct vectors * vecs)
716 struct multipath * mpp;
719 if (dm_get_maps(vecs->mpvec))
722 vector_foreach_slot (vecs->mpvec, mpp, i)
723 if (setup_multipath(vecs, mpp))
730 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
732 struct vectors * vecs;
737 vecs = (struct vectors *)trigger_data;
739 pthread_cleanup_push(cleanup_lock, &vecs->lock);
741 pthread_testcancel();
743 r = parse_cmd(str, reply, len, vecs);
746 *reply = STRDUP("fail\n");
747 *len = strlen(*reply) + 1;
750 else if (!r && *len == 0) {
751 *reply = STRDUP("ok\n");
752 *len = strlen(*reply) + 1;
755 /* else if (r < 0) leave *reply alone */
757 lock_cleanup_pop(vecs->lock);
762 uev_discard(char * devpath)
768 * keep only block devices, discard partitions
770 tmp = strstr(devpath, "/block/");
772 condlog(4, "no /block/ in '%s'", devpath);
775 if (sscanf(tmp, "/block/%10s", a) != 1 ||
776 sscanf(tmp, "/block/%10[^/]/%10s", a, b) == 2) {
777 condlog(4, "discard event on %s", devpath);
784 uev_trigger (struct uevent * uev, void * trigger_data)
787 struct vectors * vecs;
789 vecs = (struct vectors *)trigger_data;
791 if (uev_discard(uev->devpath))
794 pthread_cleanup_push(cleanup_lock, &vecs->lock);
796 pthread_testcancel();
800 * Add events are ignored here as the tables
801 * are not fully initialised then.
803 if (!strncmp(uev->kernel, "dm-", 3)) {
804 if (!strncmp(uev->action, "change", 6)) {
805 r = uev_add_map(uev, vecs);
808 if (!strncmp(uev->action, "remove", 6)) {
809 r = uev_remove_map(uev, vecs);
816 * path add/remove event
818 if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
822 if (!strncmp(uev->action, "add", 3)) {
823 r = uev_add_path(uev, vecs);
826 if (!strncmp(uev->action, "remove", 6)) {
827 r = uev_remove_path(uev, vecs);
830 if (!strncmp(uev->action, "change", 6)) {
831 r = uev_update_path(uev, vecs);
836 lock_cleanup_pop(vecs->lock);
841 ueventloop (void * ap)
843 struct udev *udev = ap;
845 if (uevent_listen(udev))
846 condlog(0, "error starting uevent listener");
854 if (uevent_dispatch(&uev_trigger, ap))
855 condlog(0, "error starting uevent dispatcher");
860 uxlsnrloop (void * ap)
865 set_handler_callback(LIST+PATHS, cli_list_paths);
866 set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt);
867 set_handler_callback(LIST+MAPS, cli_list_maps);
868 set_handler_callback(LIST+STATUS, cli_list_status);
869 set_handler_callback(LIST+DAEMON, cli_list_daemon);
870 set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
871 set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
872 set_handler_callback(LIST+MAPS+FMT, cli_list_maps_fmt);
873 set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
874 set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
875 set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
876 set_handler_callback(LIST+CONFIG, cli_list_config);
877 set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
878 set_handler_callback(LIST+DEVICES, cli_list_devices);
879 set_handler_callback(LIST+WILDCARDS, cli_list_wildcards);
880 set_handler_callback(ADD+PATH, cli_add_path);
881 set_handler_callback(DEL+PATH, cli_del_path);
882 set_handler_callback(ADD+MAP, cli_add_map);
883 set_handler_callback(DEL+MAP, cli_del_map);
884 set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
885 set_handler_callback(RECONFIGURE, cli_reconfigure);
886 set_handler_callback(SUSPEND+MAP, cli_suspend);
887 set_handler_callback(RESUME+MAP, cli_resume);
888 set_handler_callback(RESIZE+MAP, cli_resize);
889 set_handler_callback(RELOAD+MAP, cli_reload);
890 set_handler_callback(RESET+MAP, cli_reassign);
891 set_handler_callback(REINSTATE+PATH, cli_reinstate);
892 set_handler_callback(FAIL+PATH, cli_fail);
893 set_handler_callback(DISABLEQ+MAP, cli_disable_queueing);
894 set_handler_callback(RESTOREQ+MAP, cli_restore_queueing);
895 set_handler_callback(DISABLEQ+MAPS, cli_disable_all_queueing);
896 set_handler_callback(RESTOREQ+MAPS, cli_restore_all_queueing);
897 set_handler_callback(QUIT, cli_quit);
898 set_handler_callback(SHUTDOWN, cli_shutdown);
899 set_handler_callback(GETPRSTATUS+MAP, cli_getprstatus);
900 set_handler_callback(SETPRSTATUS+MAP, cli_setprstatus);
901 set_handler_callback(UNSETPRSTATUS+MAP, cli_unsetprstatus);
902 set_handler_callback(FORCEQ+DAEMON, cli_force_no_daemon_q);
903 set_handler_callback(RESTOREQ+DAEMON, cli_restore_no_daemon_q);
906 uxsock_listen(&uxsock_trigger, ap);
920 switch (running_state) {
925 case DAEMON_CONFIGURE:
929 case DAEMON_SHUTDOWN:
936 fail_path (struct path * pp, int del_active)
941 condlog(2, "checker failed path %s in map %s",
942 pp->dev_t, pp->mpp->alias);
944 dm_fail_path(pp->mpp->alias, pp->dev_t);
946 update_queue_mode_del_path(pp->mpp);
950 * caller must have locked the path list before calling that function
953 reinstate_path (struct path * pp, int add_active)
958 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
959 condlog(0, "%s: reinstate failed", pp->dev_t);
961 condlog(2, "%s: reinstated", pp->dev_t);
963 update_queue_mode_add_path(pp->mpp);
968 enable_group(struct path * pp)
970 struct pathgroup * pgp;
973 * if path is added through uev_add_path, pgindex can be unset.
974 * next update_strings() will set it, upon map reload event.
976 * we can safely return here, because upon map reload, all
977 * PG will be enabled.
979 if (!pp->mpp->pg || !pp->pgindex)
982 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
984 if (pgp->status == PGSTATE_DISABLED) {
985 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
986 dm_enablegroup(pp->mpp->alias, pp->pgindex);
991 mpvec_garbage_collector (struct vectors * vecs)
993 struct multipath * mpp;
999 vector_foreach_slot (vecs->mpvec, mpp, i) {
1000 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1001 condlog(2, "%s: remove dead map", mpp->alias);
1002 remove_map_and_stop_waiter(mpp, vecs, 1);
1008 /* This is called after a path has started working again. It the multipath
1009 * device for this path uses the followover failback type, and this is the
1010 * best pathgroup, and this is the first path in the pathgroup to come back
1011 * up, then switch to this pathgroup */
1013 followover_should_failback(struct path * pp)
1015 struct pathgroup * pgp;
1019 if (pp->mpp->pgfailback != -FAILBACK_FOLLOWOVER ||
1020 !pp->mpp->pg || !pp->pgindex ||
1021 pp->pgindex != pp->mpp->bestpg)
1024 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1025 vector_foreach_slot(pgp->paths, pp1, i) {
1028 if (pp1->chkrstate != PATH_DOWN && pp1->chkrstate != PATH_SHAKY)
1035 defered_failback_tick (vector mpvec)
1037 struct multipath * mpp;
1040 vector_foreach_slot (mpvec, mpp, i) {
1042 * defered failback getting sooner
1044 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1045 mpp->failback_tick--;
1047 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1048 switch_pathgroup(mpp);
1054 retry_count_tick(vector mpvec)
1056 struct multipath *mpp;
1059 vector_foreach_slot (mpvec, mpp, i) {
1060 if (mpp->retry_tick) {
1061 mpp->stat_total_queueing_time++;
1062 condlog(4, "%s: Retrying.. No active path", mpp->alias);
1063 if(--mpp->retry_tick == 0) {
1064 dm_queue_if_no_path(mpp->alias, 0);
1065 condlog(2, "%s: Disable queueing", mpp->alias);
1071 int update_prio(struct path *pp, int refresh_all)
1075 struct pathgroup * pgp;
1076 int i, j, changed = 0;
1079 vector_foreach_slot (pp->mpp->pg, pgp, i) {
1080 vector_foreach_slot (pgp->paths, pp1, j) {
1081 oldpriority = pp1->priority;
1082 pathinfo(pp1, conf->hwtable, DI_PRIO);
1083 if (pp1->priority != oldpriority)
1089 oldpriority = pp->priority;
1090 pathinfo(pp, conf->hwtable, DI_PRIO);
1092 if (pp->priority == oldpriority)
1097 int update_path_groups(struct multipath *mpp, struct vectors *vecs, int refresh)
1099 if (reload_map(vecs, mpp, refresh))
1103 if (setup_multipath(vecs, mpp) != 0)
1105 sync_map_state(mpp);
1111 * Returns '1' if the path has been checked, '0' otherwise
1114 check_path (struct vectors * vecs, struct path * pp)
1117 int new_path_up = 0;
1118 int chkr_new_path_up = 0;
1119 int oldchkrstate = pp->chkrstate;
1124 if (pp->tick && --pp->tick)
1125 return 0; /* don't check this path yet */
1128 * provision a next check soonest,
1129 * in case we exit abnormaly from here
1131 pp->tick = conf->checkint;
1133 newstate = path_offline(pp);
1134 if (newstate == PATH_REMOVED) {
1135 condlog(2, "%s: remove path (checker)", pp->dev);
1136 ev_remove_path(pp, vecs);
1139 if (newstate == PATH_UP)
1140 newstate = get_state(pp, 1);
1142 checker_clear_message(&pp->checker);
1144 if (newstate == PATH_WILD || newstate == PATH_UNCHECKED) {
1145 condlog(2, "%s: unusable path", pp->dev);
1146 pathinfo(pp, conf->hwtable, 0);
1150 if (!strlen(pp->wwid) &&
1151 (newstate == PATH_UP || newstate == PATH_GHOST)) {
1152 condlog(2, "%s: add missing path", pp->dev);
1153 if (pathinfo(pp, conf->hwtable, DI_ALL) == 0) {
1154 ev_add_path(pp, vecs);
1161 * Async IO in flight. Keep the previous path state
1162 * and reschedule as soon as possible
1164 if (newstate == PATH_PENDING) {
1169 * Synchronize with kernel state
1171 if (update_multipath_strings(pp->mpp, vecs->pathvec)) {
1172 condlog(1, "%s: Could not synchronize with kernel state",
1174 pp->dmstate = PSTATE_UNDEF;
1176 pp->chkrstate = newstate;
1177 if (newstate != pp->state) {
1178 int oldstate = pp->state;
1179 pp->state = newstate;
1181 if (strlen(checker_message(&pp->checker)))
1182 LOG_MSG(1, checker_message(&pp->checker));
1185 * upon state change, reset the checkint
1186 * to the shortest delay
1188 pp->checkint = conf->checkint;
1190 if (newstate == PATH_DOWN || newstate == PATH_SHAKY) {
1192 * proactively fail path in the DM
1194 if (oldstate == PATH_UP ||
1195 oldstate == PATH_GHOST)
1201 * cancel scheduled failback
1203 pp->mpp->failback_tick = 0;
1205 pp->mpp->stat_path_failures++;
1209 if(newstate == PATH_UP || newstate == PATH_GHOST){
1210 if ( pp->mpp && pp->mpp->prflag ){
1212 * Check Persistent Reservation.
1214 condlog(2, "%s: checking persistent reservation "
1215 "registration", pp->dev);
1216 mpath_pr_event_handle(pp);
1221 * reinstate this path
1223 if (oldstate != PATH_UP &&
1224 oldstate != PATH_GHOST)
1225 reinstate_path(pp, 1);
1227 reinstate_path(pp, 0);
1231 if (oldchkrstate != PATH_UP && oldchkrstate != PATH_GHOST)
1232 chkr_new_path_up = 1;
1235 * if at least one path is up in a group, and
1236 * the group is disabled, re-enable it
1238 if (newstate == PATH_UP)
1241 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1242 if (pp->dmstate == PSTATE_FAILED ||
1243 pp->dmstate == PSTATE_UNDEF) {
1244 /* Clear IO errors */
1245 reinstate_path(pp, 0);
1247 LOG_MSG(4, checker_message(&pp->checker));
1248 if (pp->checkint != conf->max_checkint) {
1250 * double the next check delay.
1251 * max at conf->max_checkint
1253 if (pp->checkint < (conf->max_checkint / 2))
1254 pp->checkint = 2 * pp->checkint;
1256 pp->checkint = conf->max_checkint;
1258 condlog(4, "%s: delay next check %is",
1259 pp->dev_t, pp->checkint);
1261 pp->tick = pp->checkint;
1264 else if (newstate == PATH_DOWN &&
1265 strlen(checker_message(&pp->checker))) {
1266 if (conf->log_checker_err == LOG_CHKR_ERR_ONCE)
1267 LOG_MSG(3, checker_message(&pp->checker));
1269 LOG_MSG(2, checker_message(&pp->checker));
1272 pp->state = newstate;
1275 * path prio refreshing
1277 condlog(4, "path prio refresh");
1279 if (update_prio(pp, new_path_up) &&
1280 (pp->mpp->pgpolicyfn == (pgpolicyfn *)group_by_prio) &&
1281 pp->mpp->pgfailback == -FAILBACK_IMMEDIATE)
1282 update_path_groups(pp->mpp, vecs, !new_path_up);
1283 else if (need_switch_pathgroup(pp->mpp, 0)) {
1284 if (pp->mpp->pgfailback > 0 &&
1285 (new_path_up || pp->mpp->failback_tick <= 0))
1286 pp->mpp->failback_tick =
1287 pp->mpp->pgfailback + 1;
1288 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE ||
1289 (chkr_new_path_up && followover_should_failback(pp)))
1290 switch_pathgroup(pp->mpp);
1296 checkerloop (void *ap)
1298 struct vectors *vecs;
1303 mlockall(MCL_CURRENT | MCL_FUTURE);
1304 vecs = (struct vectors *)ap;
1305 condlog(2, "path checkers start up");
1308 * init the path check interval
1310 vector_foreach_slot (vecs->pathvec, pp, i) {
1311 pp->checkint = conf->checkint;
1315 struct timeval diff_time, start_time, end_time;
1318 if (gettimeofday(&start_time, NULL) != 0)
1319 start_time.tv_sec = 0;
1320 pthread_cleanup_push(cleanup_lock, &vecs->lock);
1322 pthread_testcancel();
1326 sd_notify(0, "WATCHDOG=1");
1328 if (vecs->pathvec) {
1329 vector_foreach_slot (vecs->pathvec, pp, i) {
1330 num_paths += check_path(vecs, pp);
1334 defered_failback_tick(vecs->mpvec);
1335 retry_count_tick(vecs->mpvec);
1340 condlog(4, "map garbage collection");
1341 mpvec_garbage_collector(vecs);
1345 lock_cleanup_pop(vecs->lock);
1346 if (start_time.tv_sec &&
1347 gettimeofday(&end_time, NULL) == 0 &&
1349 timersub(&end_time, &start_time, &diff_time);
1350 condlog(3, "checked %d path%s in %lu.%06lu secs",
1351 num_paths, num_paths > 1 ? "s" : "",
1352 diff_time.tv_sec, diff_time.tv_usec);
1360 configure (struct vectors * vecs, int start_waiters)
1362 struct multipath * mpp;
1367 if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
1370 if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
1373 if (!(mpvec = vector_alloc()))
1377 * probe for current path (from sysfs) and map (from dm) sets
1379 path_discovery(vecs->pathvec, conf, DI_ALL);
1381 vector_foreach_slot (vecs->pathvec, pp, i){
1382 if (filter_path(conf, pp) > 0){
1383 vector_del_slot(vecs->pathvec, i);
1388 pp->checkint = conf->checkint;
1390 if (map_discovery(vecs))
1394 * create new set of maps & push changed ones into dm
1396 if (coalesce_paths(vecs, mpvec, NULL, 1))
1400 * may need to remove some maps which are no longer relevant
1401 * e.g., due to blacklist changes in conf file
1403 if (coalesce_maps(vecs, mpvec))
1408 sync_maps_state(mpvec);
1409 vector_foreach_slot(mpvec, mpp, i){
1410 remember_wwid(mpp->wwid);
1415 * purge dm of old maps
1420 * save new set of maps formed by considering current path state
1422 vector_free(vecs->mpvec);
1423 vecs->mpvec = mpvec;
1426 * start dm event waiter threads for these new maps
1428 vector_foreach_slot(vecs->mpvec, mpp, i) {
1429 if (setup_multipath(vecs, mpp))
1432 if (start_waiter_thread(mpp, vecs))
1439 reconfigure (struct vectors * vecs)
1441 struct config * old = conf;
1445 * free old map and path vectors ... they use old conf state
1447 if (VECTOR_SIZE(vecs->mpvec))
1448 remove_maps_and_stop_waiters(vecs);
1450 if (VECTOR_SIZE(vecs->pathvec))
1451 free_pathvec(vecs->pathvec, FREE_PATHS);
1453 vecs->pathvec = NULL;
1456 /* Re-read any timezone changes */
1459 if (!load_config(DEFAULT_CONFIGFILE, udev)) {
1460 dm_drv_version(conf->version, TGT_MPATH);
1461 conf->verbosity = old->verbosity;
1471 static struct vectors *
1474 struct vectors * vecs;
1476 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1482 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1484 if (!vecs->lock.mutex)
1487 pthread_mutex_init(vecs->lock.mutex, NULL);
1488 vecs->lock.depth = 0;
1494 condlog(0, "failed to init paths");
1499 signal_set(int signo, void (*func) (int))
1502 struct sigaction sig;
1503 struct sigaction osig;
1505 sig.sa_handler = func;
1506 sigemptyset(&sig.sa_mask);
1509 r = sigaction(signo, &sig, &osig);
1514 return (osig.sa_handler);
1518 handle_signals(void)
1520 if (reconfig_sig && running_state == DAEMON_RUNNING) {
1521 condlog(2, "reconfigure (signal)");
1522 pthread_cleanup_push(cleanup_lock,
1525 pthread_testcancel();
1527 lock_cleanup_pop(gvecs->lock);
1529 if (log_reset_sig) {
1530 condlog(2, "reset log (signal)");
1531 pthread_mutex_lock(&logq_lock);
1532 log_reset("multipathd");
1533 pthread_mutex_unlock(&logq_lock);
1560 condlog(3, "SIGUSR2 received");
1569 sigaddset(&set, SIGHUP);
1570 sigaddset(&set, SIGUSR1);
1571 sigaddset(&set, SIGUSR2);
1572 pthread_sigmask(SIG_BLOCK, &set, NULL);
1574 signal_set(SIGHUP, sighup);
1575 signal_set(SIGUSR1, sigusr1);
1576 signal_set(SIGUSR2, sigusr2);
1577 signal_set(SIGINT, sigend);
1578 signal_set(SIGTERM, sigend);
1579 signal(SIGPIPE, SIG_IGN);
1586 static struct sched_param sched_param = {
1587 .sched_priority = 99
1590 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1593 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1600 #ifdef OOM_SCORE_ADJ_MIN
1602 char *file = "/proc/self/oom_score_adj";
1603 int score = OOM_SCORE_ADJ_MIN;
1606 char *file = "/proc/self/oom_adj";
1607 int score = OOM_ADJUST_MIN;
1613 envp = getenv("OOMScoreAdjust");
1615 condlog(3, "Using systemd provided OOMScoreAdjust");
1619 if (stat(file, &st) == 0){
1620 fp = fopen(file, "w");
1622 condlog(0, "couldn't fopen %s : %s", file,
1626 fprintf(fp, "%i", score);
1630 if (errno != ENOENT) {
1631 condlog(0, "couldn't stat %s : %s", file,
1635 #ifdef OOM_ADJUST_MIN
1636 file = "/proc/self/oom_adj";
1637 score = OOM_ADJUST_MIN;
1642 condlog(0, "couldn't adjust oom score");
1646 child (void * param)
1648 pthread_t check_thr, uevent_thr, uxlsnr_thr, uevq_thr;
1649 pthread_attr_t log_attr, misc_attr, uevent_attr;
1650 struct vectors * vecs;
1651 struct multipath * mpp;
1654 unsigned long checkint;
1659 mlockall(MCL_CURRENT | MCL_FUTURE);
1660 sem_init(&exit_sem, 0, 0);
1665 setup_thread_attr(&misc_attr, 64 * 1024, 1);
1666 setup_thread_attr(&uevent_attr, 128 * 1024, 1);
1667 setup_thread_attr(&waiter_attr, 32 * 1024, 1);
1670 setup_thread_attr(&log_attr, 64 * 1024, 0);
1671 log_thread_start(&log_attr);
1672 pthread_attr_destroy(&log_attr);
1675 running_state = DAEMON_START;
1678 sd_notify(0, "STATUS=startup");
1680 condlog(2, "--------start up--------");
1681 condlog(2, "read " DEFAULT_CONFIGFILE);
1683 if (load_config(DEFAULT_CONFIGFILE, udev))
1686 dm_drv_version(conf->version, TGT_MPATH);
1687 if (init_checkers()) {
1688 condlog(0, "failed to initialize checkers");
1692 condlog(0, "failed to initialize prioritizers");
1696 setlogmask(LOG_UPTO(conf->verbosity + 3));
1698 envp = getenv("LimitNOFILE");
1701 condlog(2,"Using systemd provided open fds limit of %s", envp);
1702 } else if (conf->max_fds) {
1703 struct rlimit fd_limit;
1705 if (getrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
1706 condlog(0, "can't get open fds limit: %s",
1708 fd_limit.rlim_cur = 0;
1709 fd_limit.rlim_max = 0;
1711 if (fd_limit.rlim_cur < conf->max_fds) {
1712 fd_limit.rlim_cur = conf->max_fds;
1713 if (fd_limit.rlim_max < conf->max_fds)
1714 fd_limit.rlim_max = conf->max_fds;
1715 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
1716 condlog(0, "can't set open fds limit to "
1718 fd_limit.rlim_cur, fd_limit.rlim_max,
1721 condlog(3, "set open fds limit to %lu/%lu",
1722 fd_limit.rlim_cur, fd_limit.rlim_max);
1728 vecs = gvecs = init_vecs();
1736 udev_set_sync_support(0);
1738 envp = getenv("WATCHDOG_USEC");
1739 if (envp && sscanf(envp, "%lu", &checkint) == 1) {
1740 /* Value is in microseconds */
1741 conf->max_checkint = checkint / 1000000;
1742 /* Rescale checkint */
1743 if (conf->checkint > conf->max_checkint)
1744 conf->checkint = conf->max_checkint;
1746 conf->checkint = conf->max_checkint / 4;
1747 condlog(3, "enabling watchdog, interval %d max %d",
1748 conf->checkint, conf->max_checkint);
1749 conf->watchdog = conf->checkint;
1753 * Start uevent listener early to catch events
1755 if ((rc = pthread_create(&uevent_thr, &uevent_attr, ueventloop, udev))) {
1756 condlog(0, "failed to create uevent thread: %d", rc);
1759 pthread_attr_destroy(&uevent_attr);
1760 if ((rc = pthread_create(&uxlsnr_thr, &misc_attr, uxlsnrloop, vecs))) {
1761 condlog(0, "failed to create cli listener: %d", rc);
1765 * fetch and configure both paths and multipaths
1768 sd_notify(0, "STATUS=configure");
1770 running_state = DAEMON_CONFIGURE;
1773 if (configure(vecs, 1)) {
1775 condlog(0, "failure during configuration");
1783 if ((rc = pthread_create(&check_thr, &misc_attr, checkerloop, vecs))) {
1784 condlog(0,"failed to create checker loop thread: %d", rc);
1787 if ((rc = pthread_create(&uevq_thr, &misc_attr, uevqloop, vecs))) {
1788 condlog(0, "failed to create uevent dispatcher: %d", rc);
1791 pthread_attr_destroy(&misc_attr);
1793 /* Startup complete, create logfile */
1794 pid_rc = pidfile_create(DEFAULT_PIDFILE, daemon_pid);
1795 /* Ignore errors, we can live without */
1797 running_state = DAEMON_RUNNING;
1799 sd_notify(0, "READY=1\nSTATUS=running");
1805 while(sem_wait(&exit_sem) != 0); /* Do nothing */
1808 sd_notify(0, "STATUS=shutdown");
1810 running_state = DAEMON_SHUTDOWN;
1812 if (conf->queue_without_daemon == QUE_NO_DAEMON_OFF)
1813 vector_foreach_slot(vecs->mpvec, mpp, i)
1814 dm_queue_if_no_path(mpp->alias, 0);
1815 remove_maps_and_stop_waiters(vecs);
1818 pthread_cancel(check_thr);
1819 pthread_cancel(uevent_thr);
1820 pthread_cancel(uxlsnr_thr);
1821 pthread_cancel(uevq_thr);
1824 free_pathvec(vecs->pathvec, FREE_PATHS);
1825 vecs->pathvec = NULL;
1827 /* Now all the waitevent threads will start rushing in. */
1828 while (vecs->lock.depth > 0) {
1829 sleep (1); /* This is weak. */
1830 condlog(3, "Have %d wait event checkers threads to de-alloc,"
1831 " waiting...", vecs->lock.depth);
1833 pthread_mutex_destroy(vecs->lock.mutex);
1834 FREE(vecs->lock.mutex);
1835 vecs->lock.depth = 0;
1836 vecs->lock.mutex = NULL;
1846 /* We're done here */
1848 condlog(3, "unlink pidfile");
1849 unlink(DEFAULT_PIDFILE);
1852 condlog(2, "--------shut down-------");
1858 * Freeing config must be done after condlog() and dm_lib_exit(),
1859 * because logging functions like dlog() and dm_write_log()
1860 * reference the config.
1867 dbg_free_final(NULL);
1871 sd_notify(0, "ERRNO=0");
1877 sd_notify(0, "ERRNO=1");
1888 if( (pid = fork()) < 0){
1889 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1897 if ( (pid = fork()) < 0)
1898 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1903 fprintf(stderr, "cannot chdir to '/', continuing\n");
1905 dev_null_fd = open("/dev/null", O_RDWR);
1906 if (dev_null_fd < 0){
1907 fprintf(stderr, "cannot open /dev/null for input & output : %s\n",
1912 close(STDIN_FILENO);
1913 if (dup(dev_null_fd) < 0) {
1914 fprintf(stderr, "cannot dup /dev/null to stdin : %s\n",
1918 close(STDOUT_FILENO);
1919 if (dup(dev_null_fd) < 0) {
1920 fprintf(stderr, "cannot dup /dev/null to stdout : %s\n",
1924 close(STDERR_FILENO);
1925 if (dup(dev_null_fd) < 0) {
1926 fprintf(stderr, "cannot dup /dev/null to stderr : %s\n",
1931 daemon_pid = getpid();
1936 main (int argc, char *argv[])
1938 extern char *optarg;
1944 running_state = DAEMON_INIT;
1947 if (getuid() != 0) {
1948 fprintf(stderr, "need to be root\n");
1952 /* make sure we don't lock any path */
1954 fprintf(stderr, "can't chdir to root directory : %s\n",
1956 umask(umask(077) | 022);
1958 conf = alloc_config();
1963 while ((arg = getopt(argc, argv, ":dsv:k::")) != EOF ) {
1967 //debug=1; /* ### comment me out ### */
1970 if (sizeof(optarg) > sizeof(char *) ||
1971 !isdigit(optarg[0]))
1974 conf->verbosity = atoi(optarg);
1986 if (optind < argc) {
1991 while (optind < argc) {
1992 if (strchr(argv[optind], ' '))
1993 c += snprintf(c, s + CMDSIZE - c, "\"%s\" ", argv[optind]);
1995 c += snprintf(c, s + CMDSIZE - c, "%s ", argv[optind]);
1998 c += snprintf(c, s + CMDSIZE - c, "\n");
2016 return (child(NULL));
2019 void * mpath_pr_event_handler_fn (void * pathp )
2021 struct multipath * mpp;
2022 int i,j, ret, isFound;
2023 struct path * pp = (struct path *)pathp;
2024 unsigned char *keyp;
2026 struct prout_param_descriptor *param;
2027 struct prin_resp *resp;
2031 resp = mpath_alloc_prin_response(MPATH_PRIN_RKEY_SA);
2033 condlog(0,"%s Alloc failed for prin response", pp->dev);
2037 ret = prin_do_scsi_ioctl(pp->dev, MPATH_PRIN_RKEY_SA, resp, 0);
2038 if (ret != MPATH_PR_SUCCESS )
2040 condlog(0,"%s : pr in read keys service action failed. Error=%d", pp->dev, ret);
2044 condlog(3, " event pr=%d addlen=%d",resp->prin_descriptor.prin_readkeys.prgeneration,
2045 resp->prin_descriptor.prin_readkeys.additional_length );
2047 if (resp->prin_descriptor.prin_readkeys.additional_length == 0 )
2049 condlog(1, "%s: No key found. Device may not be registered.", pp->dev);
2050 ret = MPATH_PR_SUCCESS;
2054 keyp = (unsigned char *)mpp->reservation_key;
2055 for (j = 0; j < 8; ++j) {
2061 condlog(2, "Multipath reservation_key: 0x%" PRIx64 " ", prkey);
2064 for (i = 0; i < resp->prin_descriptor.prin_readkeys.additional_length/8; i++ )
2066 condlog(2, "PR IN READKEYS[%d] reservation key:",i);
2067 dumpHex((char *)&resp->prin_descriptor.prin_readkeys.key_list[i*8], 8 , -1);
2068 if (!memcmp(mpp->reservation_key, &resp->prin_descriptor.prin_readkeys.key_list[i*8], 8))
2070 condlog(2, "%s: pr key found in prin readkeys response", mpp->alias);
2077 condlog(0, "%s: Either device not registered or ", pp->dev);
2078 condlog(0, "host is not authorised for registration. Skip path");
2079 ret = MPATH_PR_OTHER;
2083 param= malloc(sizeof(struct prout_param_descriptor));
2084 memset(param, 0 , sizeof(struct prout_param_descriptor));
2086 for (j = 7; j >= 0; --j) {
2087 param->sa_key[j] = (prkey & 0xff);
2090 param->num_transportid = 0;
2092 condlog(3, "device %s:%s", pp->dev, pp->mpp->wwid);
2094 ret = prout_do_scsi_ioctl(pp->dev, MPATH_PROUT_REG_IGN_SA, 0, 0, param, 0);
2095 if (ret != MPATH_PR_SUCCESS )
2097 condlog(0,"%s: Reservation registration failed. Error: %d", pp->dev, ret);
2107 int mpath_pr_event_handle(struct path *pp)
2111 pthread_attr_t attr;
2112 struct multipath * mpp;
2116 if (!mpp->reservation_key)
2119 pthread_attr_init(&attr);
2120 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
2122 rc = pthread_create(&thread, NULL , mpath_pr_event_handler_fn, pp);
2124 condlog(0, "%s: ERROR; return code from pthread_create() is %d", pp->dev, rc);
2127 pthread_attr_destroy(&attr);
2128 rc = pthread_join(thread, NULL);