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 <semaphore.h>
21 #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>
62 #include "cli_handlers.h"
67 #define FILE_NAME_SIZE 256
70 #define LOG_MSG(a, b) \
73 condlog(a, "%s: %s - path offline", pp->mpp->alias, pp->dev); \
75 condlog(a, "%s: %s - %s", pp->mpp->alias, pp->dev, b); \
78 struct mpath_event_param
81 struct multipath *mpp;
84 unsigned int mpath_mx_alloc_len;
87 enum daemon_status running_state;
90 static sem_t exit_sem;
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 r = get_refwwid(dev, DEV_DEVMAP, vecs->pathvec, &refwwid);
306 r = coalesce_paths(vecs, NULL, refwwid, 0);
311 condlog(2, "%s: devmap %s added", alias, dev);
313 condlog(2, "%s: uev_add_map %s blacklisted", alias, dev);
315 condlog(0, "%s: uev_add_map %s failed", alias, dev);
322 uev_remove_map (struct uevent * uev, struct vectors * vecs)
326 struct multipath *mpp;
328 condlog(2, "%s: remove map (uevent)", uev->kernel);
329 alias = uevent_get_dm_name(uev);
331 condlog(3, "%s: No DM_NAME in uevent, ignoring", uev->kernel);
334 minor = uevent_get_minor(uev);
335 mpp = find_mp_by_minor(vecs->mpvec, minor);
338 condlog(2, "%s: devmap not registered, can't remove",
342 if (strcmp(mpp->alias, alias)) {
343 condlog(2, "%s: minor number mismatch (map %d, event %d)",
344 mpp->alias, mpp->dmi->minor, minor);
348 orphan_paths(vecs->pathvec, mpp);
349 remove_map_and_stop_waiter(mpp, vecs, 1);
356 ev_remove_map (char * devname, char * alias, int minor, struct vectors * vecs)
358 struct multipath * mpp;
360 mpp = find_mp_by_minor(vecs->mpvec, minor);
363 condlog(2, "%s: devmap not registered, can't remove",
367 if (strcmp(mpp->alias, alias)) {
368 condlog(2, "%s: minor number mismatch (map %d, event %d)",
369 mpp->alias, mpp->dmi->minor, minor);
372 return flush_map(mpp, vecs);
376 uev_add_path (struct uevent *uev, struct vectors * vecs)
381 condlog(2, "%s: add path (uevent)", uev->kernel);
382 if (strstr(uev->kernel, "..") != NULL) {
384 * Don't allow relative device names in the pathvec
386 condlog(0, "%s: path name is invalid", uev->kernel);
390 pp = find_path_by_dev(vecs->pathvec, uev->kernel);
392 condlog(0, "%s: spurious uevent, path already in pathvec",
396 if (!strlen(pp->wwid)) {
397 udev_device_unref(pp->udev);
398 pp->udev = udev_device_ref(uev->udev);
399 ret = pathinfo(pp, conf->hwtable,
400 DI_ALL | DI_BLACKLIST);
402 i = find_slot(vecs->pathvec, (void *)pp);
404 vector_del_slot(vecs->pathvec, i);
407 } else if (ret == 1) {
408 condlog(0, "%s: failed to reinitialize path",
415 * get path vital state
417 ret = store_pathinfo(vecs->pathvec, conf->hwtable,
418 uev->udev, DI_ALL, &pp);
422 condlog(0, "%s: failed to store path info",
426 pp->checkint = conf->checkint;
429 return ev_add_path(pp, vecs);
438 ev_add_path (struct path * pp, struct vectors * vecs)
440 struct multipath * mpp;
441 char empty_buff[WWID_SIZE] = {0};
442 char params[PARAMS_SIZE] = {0};
444 int start_waiter = 0;
447 * need path UID to go any further
449 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
450 condlog(0, "%s: failed to get path uid", pp->dev);
451 goto fail; /* leave path added to pathvec */
453 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
456 if ((!pp->size) || (mpp->size != pp->size)) {
458 condlog(0, "%s: failed to add new path %s, "
460 mpp->alias, pp->dev);
462 condlog(0, "%s: failed to add new path %s, "
463 "device size mismatch",
464 mpp->alias, pp->dev);
465 int i = find_slot(vecs->pathvec, (void *)pp);
467 vector_del_slot(vecs->pathvec, i);
472 condlog(4,"%s: adopting all paths for path %s",
473 mpp->alias, pp->dev);
474 if (adopt_paths(vecs->pathvec, mpp, 1))
475 goto fail; /* leave path added to pathvec */
477 verify_paths(mpp, vecs, NULL);
478 mpp->flush_on_last_del = FLUSH_UNDEF;
479 mpp->action = ACT_RELOAD;
483 condlog(0, "%s: failed to create new map,"
484 " device size is 0 ", pp->dev);
485 int i = find_slot(vecs->pathvec, (void *)pp);
487 vector_del_slot(vecs->pathvec, i);
492 condlog(4,"%s: creating new map", pp->dev);
493 if ((mpp = add_map_with_path(vecs, pp, 1))) {
494 mpp->action = ACT_CREATE;
496 * We don't depend on ACT_CREATE, as domap will
497 * set it to ACT_NOTHING when complete.
502 goto fail; /* leave path added to pathvec */
505 /* persistent reseravtion check*/
506 mpath_pr_event_handle(pp);
509 * push the map to the device-mapper
511 if (setup_map(mpp, params, PARAMS_SIZE)) {
512 condlog(0, "%s: failed to setup map for addition of new "
513 "path %s", mpp->alias, pp->dev);
517 * reload the map for the multipath mapped device
519 if (domap(mpp, params) <= 0) {
520 condlog(0, "%s: failed in domap for addition of new "
521 "path %s", mpp->alias, pp->dev);
523 * deal with asynchronous uevents :((
525 if (mpp->action == ACT_RELOAD && retries-- > 0) {
526 condlog(0, "%s: uev_add_path sleep", mpp->alias);
528 update_mpp_paths(mpp, vecs->pathvec);
531 else if (mpp->action == ACT_RELOAD)
532 condlog(0, "%s: giving up reload", mpp->alias);
539 * update our state from kernel regardless of create or reload
541 if (setup_multipath(vecs, mpp))
542 goto fail; /* if setup_multipath fails, it removes the map */
546 if ((mpp->action == ACT_CREATE ||
547 (mpp->action == ACT_NOTHING && start_waiter && !mpp->waiter)) &&
548 start_waiter_thread(mpp, vecs))
552 condlog(2, "%s [%s]: path added to devmap %s",
553 pp->dev, pp->dev_t, mpp->alias);
560 remove_map(mpp, vecs, 1);
567 uev_remove_path (struct uevent *uev, struct vectors * vecs)
571 condlog(2, "%s: remove path (uevent)", uev->kernel);
572 pp = find_path_by_dev(vecs->pathvec, uev->kernel);
575 /* Not an error; path might have been purged earlier */
576 condlog(0, "%s: path already removed", uev->kernel);
580 return ev_remove_path(pp, vecs);
584 ev_remove_path (struct path *pp, struct vectors * vecs)
586 struct multipath * mpp;
588 char params[PARAMS_SIZE] = {0};
591 * avoid referring to the map of an orphaned path
593 if ((mpp = pp->mpp)) {
595 * transform the mp->pg vector of vectors of paths
596 * into a mp->params string to feed the device-mapper
598 if (update_mpp_paths(mpp, vecs->pathvec)) {
599 condlog(0, "%s: failed to update paths",
603 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
604 vector_del_slot(mpp->paths, i);
607 * remove the map IFF removing the last path
609 if (VECTOR_SIZE(mpp->paths) == 0) {
610 char alias[WWID_SIZE];
613 * flush_map will fail if the device is open
615 strncpy(alias, mpp->alias, WWID_SIZE);
616 if (mpp->flush_on_last_del == FLUSH_ENABLED) {
617 condlog(2, "%s Last path deleted, disabling queueing", mpp->alias);
619 mpp->no_path_retry = NO_PATH_RETRY_FAIL;
620 mpp->flush_on_last_del = FLUSH_IN_PROGRESS;
621 dm_queue_if_no_path(mpp->alias, 0);
623 if (!flush_map(mpp, vecs)) {
624 condlog(2, "%s: removed map after"
625 " removing all paths",
631 * Not an error, continue
635 if (setup_map(mpp, params, PARAMS_SIZE)) {
636 condlog(0, "%s: failed to setup map for"
637 " removal of path %s", mpp->alias, pp->dev);
643 mpp->action = ACT_RELOAD;
644 if (domap(mpp, params) <= 0) {
645 condlog(0, "%s: failed in domap for "
646 "removal of path %s",
647 mpp->alias, pp->dev);
651 * update our state from kernel
653 if (setup_multipath(vecs, mpp)) {
658 condlog(2, "%s [%s]: path removed from map %s",
659 pp->dev, pp->dev_t, mpp->alias);
664 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
665 vector_del_slot(vecs->pathvec, i);
672 remove_map_and_stop_waiter(mpp, vecs, 1);
677 uev_update_path (struct uevent *uev, struct vectors * vecs)
681 ro = uevent_get_disk_ro(uev);
686 condlog(2, "%s: update path write_protect to '%d' (uevent)",
688 pp = find_path_by_dev(vecs->pathvec, uev->kernel);
690 condlog(0, "%s: spurious uevent, path not found",
695 retval = reload_map(vecs, pp->mpp, 0);
697 condlog(2, "%s: map %s reloaded (retval %d)",
698 uev->kernel, pp->mpp->alias, retval);
707 map_discovery (struct vectors * vecs)
709 struct multipath * mpp;
712 if (dm_get_maps(vecs->mpvec))
715 vector_foreach_slot (vecs->mpvec, mpp, i)
716 if (setup_multipath(vecs, mpp))
723 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
725 struct vectors * vecs;
730 vecs = (struct vectors *)trigger_data;
732 pthread_cleanup_push(cleanup_lock, &vecs->lock);
734 pthread_testcancel();
736 r = parse_cmd(str, reply, len, vecs);
739 *reply = STRDUP("fail\n");
740 *len = strlen(*reply) + 1;
743 else if (!r && *len == 0) {
744 *reply = STRDUP("ok\n");
745 *len = strlen(*reply) + 1;
748 /* else if (r < 0) leave *reply alone */
750 lock_cleanup_pop(vecs->lock);
755 uev_discard(char * devpath)
761 * keep only block devices, discard partitions
763 tmp = strstr(devpath, "/block/");
765 condlog(4, "no /block/ in '%s'", devpath);
768 if (sscanf(tmp, "/block/%10s", a) != 1 ||
769 sscanf(tmp, "/block/%10[^/]/%10s", a, b) == 2) {
770 condlog(4, "discard event on %s", devpath);
777 uev_trigger (struct uevent * uev, void * trigger_data)
780 struct vectors * vecs;
782 vecs = (struct vectors *)trigger_data;
784 if (uev_discard(uev->devpath))
787 pthread_cleanup_push(cleanup_lock, &vecs->lock);
789 pthread_testcancel();
793 * Add events are ignored here as the tables
794 * are not fully initialised then.
796 if (!strncmp(uev->kernel, "dm-", 3)) {
797 if (!strncmp(uev->action, "change", 6)) {
798 r = uev_add_map(uev, vecs);
801 if (!strncmp(uev->action, "remove", 6)) {
802 r = uev_remove_map(uev, vecs);
809 * path add/remove event
811 if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
815 if (!strncmp(uev->action, "add", 3)) {
816 r = uev_add_path(uev, vecs);
819 if (!strncmp(uev->action, "remove", 6)) {
820 r = uev_remove_path(uev, vecs);
823 if (!strncmp(uev->action, "change", 6)) {
824 r = uev_update_path(uev, vecs);
829 lock_cleanup_pop(vecs->lock);
834 ueventloop (void * ap)
837 condlog(0, "error starting uevent listener");
845 if (uevent_dispatch(&uev_trigger, ap))
846 condlog(0, "error starting uevent dispatcher");
851 uxlsnrloop (void * ap)
856 set_handler_callback(LIST+PATHS, cli_list_paths);
857 set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt);
858 set_handler_callback(LIST+MAPS, cli_list_maps);
859 set_handler_callback(LIST+STATUS, cli_list_status);
860 set_handler_callback(LIST+DAEMON, cli_list_daemon);
861 set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
862 set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
863 set_handler_callback(LIST+MAPS+FMT, cli_list_maps_fmt);
864 set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
865 set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
866 set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
867 set_handler_callback(LIST+CONFIG, cli_list_config);
868 set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
869 set_handler_callback(LIST+DEVICES, cli_list_devices);
870 set_handler_callback(LIST+WILDCARDS, cli_list_wildcards);
871 set_handler_callback(ADD+PATH, cli_add_path);
872 set_handler_callback(DEL+PATH, cli_del_path);
873 set_handler_callback(ADD+MAP, cli_add_map);
874 set_handler_callback(DEL+MAP, cli_del_map);
875 set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
876 set_handler_callback(RECONFIGURE, cli_reconfigure);
877 set_handler_callback(SUSPEND+MAP, cli_suspend);
878 set_handler_callback(RESUME+MAP, cli_resume);
879 set_handler_callback(RESIZE+MAP, cli_resize);
880 set_handler_callback(RELOAD+MAP, cli_reload);
881 set_handler_callback(RESET+MAP, cli_reassign);
882 set_handler_callback(REINSTATE+PATH, cli_reinstate);
883 set_handler_callback(FAIL+PATH, cli_fail);
884 set_handler_callback(DISABLEQ+MAP, cli_disable_queueing);
885 set_handler_callback(RESTOREQ+MAP, cli_restore_queueing);
886 set_handler_callback(DISABLEQ+MAPS, cli_disable_all_queueing);
887 set_handler_callback(RESTOREQ+MAPS, cli_restore_all_queueing);
888 set_handler_callback(QUIT, cli_quit);
889 set_handler_callback(SHUTDOWN, cli_shutdown);
890 set_handler_callback(GETPRSTATUS+MAP, cli_getprstatus);
891 set_handler_callback(SETPRSTATUS+MAP, cli_setprstatus);
892 set_handler_callback(UNSETPRSTATUS+MAP, cli_unsetprstatus);
893 set_handler_callback(FORCEQ+DAEMON, cli_force_no_daemon_q);
894 set_handler_callback(RESTOREQ+DAEMON, cli_restore_no_daemon_q);
897 uxsock_listen(&uxsock_trigger, ap);
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);
999 /* This is called after a path has started working again. It the multipath
1000 * device for this path uses the followover failback type, and this is the
1001 * best pathgroup, and this is the first path in the pathgroup to come back
1002 * up, then switch to this pathgroup */
1004 followover_should_failback(struct path * pp)
1006 struct pathgroup * pgp;
1010 if (pp->mpp->pgfailback != -FAILBACK_FOLLOWOVER ||
1011 !pp->mpp->pg || !pp->pgindex ||
1012 pp->pgindex != pp->mpp->bestpg)
1015 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1016 vector_foreach_slot(pgp->paths, pp1, i) {
1019 if (pp1->chkrstate != PATH_DOWN && pp1->chkrstate != PATH_SHAKY)
1026 defered_failback_tick (vector mpvec)
1028 struct multipath * mpp;
1031 vector_foreach_slot (mpvec, mpp, i) {
1033 * defered failback getting sooner
1035 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1036 mpp->failback_tick--;
1038 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1039 switch_pathgroup(mpp);
1045 retry_count_tick(vector mpvec)
1047 struct multipath *mpp;
1050 vector_foreach_slot (mpvec, mpp, i) {
1051 if (mpp->retry_tick) {
1052 mpp->stat_total_queueing_time++;
1053 condlog(4, "%s: Retrying.. No active path", mpp->alias);
1054 if(--mpp->retry_tick == 0) {
1055 dm_queue_if_no_path(mpp->alias, 0);
1056 condlog(2, "%s: Disable queueing", mpp->alias);
1062 int update_prio(struct path *pp, int refresh_all)
1066 struct pathgroup * pgp;
1067 int i, j, changed = 0;
1070 vector_foreach_slot (pp->mpp->pg, pgp, i) {
1071 vector_foreach_slot (pgp->paths, pp1, j) {
1072 oldpriority = pp1->priority;
1073 pathinfo(pp1, conf->hwtable, DI_PRIO);
1074 if (pp1->priority != oldpriority)
1080 oldpriority = pp->priority;
1081 pathinfo(pp, conf->hwtable, DI_PRIO);
1083 if (pp->priority == oldpriority)
1088 int update_path_groups(struct multipath *mpp, struct vectors *vecs, int refresh)
1090 if (reload_map(vecs, mpp, refresh))
1094 if (setup_multipath(vecs, mpp) != 0)
1096 sync_map_state(mpp);
1102 check_path (struct vectors * vecs, struct path * pp)
1105 int new_path_up = 0;
1106 int chkr_new_path_up = 0;
1107 int oldchkrstate = pp->chkrstate;
1112 if (pp->tick && --pp->tick)
1113 return; /* don't check this path yet */
1116 * provision a next check soonest,
1117 * in case we exit abnormaly from here
1119 pp->tick = conf->checkint;
1121 newstate = path_offline(pp);
1122 if (newstate == PATH_UP)
1123 newstate = get_state(pp, 1);
1125 if (newstate == PATH_WILD || newstate == PATH_UNCHECKED) {
1126 condlog(2, "%s: unusable path", pp->dev);
1127 pathinfo(pp, conf->hwtable, 0);
1131 * Async IO in flight. Keep the previous path state
1132 * and reschedule as soon as possible
1134 if (newstate == PATH_PENDING) {
1139 * Synchronize with kernel state
1141 if (update_multipath_strings(pp->mpp, vecs->pathvec)) {
1142 condlog(1, "%s: Could not synchronize with kernel state",
1144 pp->dmstate = PSTATE_UNDEF;
1146 pp->chkrstate = newstate;
1147 if (newstate != pp->state) {
1148 int oldstate = pp->state;
1149 pp->state = newstate;
1150 LOG_MSG(1, checker_message(&pp->checker));
1153 * upon state change, reset the checkint
1154 * to the shortest delay
1156 pp->checkint = conf->checkint;
1158 if (newstate == PATH_DOWN || newstate == PATH_SHAKY) {
1160 * proactively fail path in the DM
1162 if (oldstate == PATH_UP ||
1163 oldstate == PATH_GHOST)
1169 * cancel scheduled failback
1171 pp->mpp->failback_tick = 0;
1173 pp->mpp->stat_path_failures++;
1177 if(newstate == PATH_UP || newstate == PATH_GHOST){
1178 if ( pp->mpp && pp->mpp->prflag ){
1180 * Check Persistent Reservation.
1182 condlog(2, "%s: checking persistent reservation "
1183 "registration", pp->dev);
1184 mpath_pr_event_handle(pp);
1189 * reinstate this path
1191 if (oldstate != PATH_UP &&
1192 oldstate != PATH_GHOST)
1193 reinstate_path(pp, 1);
1195 reinstate_path(pp, 0);
1199 if (oldchkrstate != PATH_UP && oldchkrstate != PATH_GHOST)
1200 chkr_new_path_up = 1;
1203 * if at least one path is up in a group, and
1204 * the group is disabled, re-enable it
1206 if (newstate == PATH_UP)
1209 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1210 if (pp->dmstate == PSTATE_FAILED ||
1211 pp->dmstate == PSTATE_UNDEF) {
1212 /* Clear IO errors */
1213 reinstate_path(pp, 0);
1215 LOG_MSG(4, checker_message(&pp->checker));
1216 if (pp->checkint != conf->max_checkint) {
1218 * double the next check delay.
1219 * max at conf->max_checkint
1221 if (pp->checkint < (conf->max_checkint / 2))
1222 pp->checkint = 2 * pp->checkint;
1224 pp->checkint = conf->max_checkint;
1226 condlog(4, "%s: delay next check %is",
1227 pp->dev_t, pp->checkint);
1229 pp->tick = pp->checkint;
1232 else if (newstate == PATH_DOWN) {
1233 if (conf->log_checker_err == LOG_CHKR_ERR_ONCE)
1234 LOG_MSG(3, checker_message(&pp->checker));
1236 LOG_MSG(2, checker_message(&pp->checker));
1239 pp->state = newstate;
1242 * path prio refreshing
1244 condlog(4, "path prio refresh");
1246 if (update_prio(pp, new_path_up) &&
1247 (pp->mpp->pgpolicyfn == (pgpolicyfn *)group_by_prio) &&
1248 pp->mpp->pgfailback == -FAILBACK_IMMEDIATE)
1249 update_path_groups(pp->mpp, vecs, !new_path_up);
1250 else if (need_switch_pathgroup(pp->mpp, 0)) {
1251 if (pp->mpp->pgfailback > 0 &&
1252 (new_path_up || pp->mpp->failback_tick <= 0))
1253 pp->mpp->failback_tick =
1254 pp->mpp->pgfailback + 1;
1255 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE ||
1256 (chkr_new_path_up && followover_should_failback(pp)))
1257 switch_pathgroup(pp->mpp);
1262 checkerloop (void *ap)
1264 struct vectors *vecs;
1269 mlockall(MCL_CURRENT | MCL_FUTURE);
1270 vecs = (struct vectors *)ap;
1271 condlog(2, "path checkers start up");
1274 * init the path check interval
1276 vector_foreach_slot (vecs->pathvec, pp, i) {
1277 pp->checkint = conf->checkint;
1281 pthread_cleanup_push(cleanup_lock, &vecs->lock);
1283 pthread_testcancel();
1286 if (vecs->pathvec) {
1287 vector_foreach_slot (vecs->pathvec, pp, i) {
1288 check_path(vecs, pp);
1292 defered_failback_tick(vecs->mpvec);
1293 retry_count_tick(vecs->mpvec);
1298 condlog(4, "map garbage collection");
1299 mpvec_garbage_collector(vecs);
1303 lock_cleanup_pop(vecs->lock);
1310 configure (struct vectors * vecs, int start_waiters)
1312 struct multipath * mpp;
1317 if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
1320 if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
1323 if (!(mpvec = vector_alloc()))
1327 * probe for current path (from sysfs) and map (from dm) sets
1329 path_discovery(vecs->pathvec, conf, DI_ALL);
1331 vector_foreach_slot (vecs->pathvec, pp, i){
1332 if (filter_path(conf, pp) > 0){
1333 vector_del_slot(vecs->pathvec, i);
1338 pp->checkint = conf->checkint;
1340 if (map_discovery(vecs))
1344 * create new set of maps & push changed ones into dm
1346 if (coalesce_paths(vecs, mpvec, NULL, 1))
1350 * may need to remove some maps which are no longer relevant
1351 * e.g., due to blacklist changes in conf file
1353 if (coalesce_maps(vecs, mpvec))
1358 sync_maps_state(mpvec);
1359 vector_foreach_slot(mpvec, mpp, i){
1360 remember_wwid(mpp->wwid);
1365 * purge dm of old maps
1370 * save new set of maps formed by considering current path state
1372 vector_free(vecs->mpvec);
1373 vecs->mpvec = mpvec;
1376 * start dm event waiter threads for these new maps
1378 vector_foreach_slot(vecs->mpvec, mpp, i) {
1379 if (setup_multipath(vecs, mpp))
1382 if (start_waiter_thread(mpp, vecs))
1389 reconfigure (struct vectors * vecs)
1391 struct config * old = conf;
1395 * free old map and path vectors ... they use old conf state
1397 if (VECTOR_SIZE(vecs->mpvec))
1398 remove_maps_and_stop_waiters(vecs);
1400 if (VECTOR_SIZE(vecs->pathvec))
1401 free_pathvec(vecs->pathvec, FREE_PATHS);
1403 vecs->pathvec = NULL;
1406 if (!load_config(DEFAULT_CONFIGFILE)) {
1407 conf->verbosity = old->verbosity;
1417 static struct vectors *
1420 struct vectors * vecs;
1422 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1428 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1430 if (!vecs->lock.mutex)
1433 pthread_mutex_init(vecs->lock.mutex, NULL);
1434 vecs->lock.depth = 0;
1440 condlog(0, "failed to init paths");
1445 signal_set(int signo, void (*func) (int))
1448 struct sigaction sig;
1449 struct sigaction osig;
1451 sig.sa_handler = func;
1452 sigemptyset(&sig.sa_mask);
1455 r = sigaction(signo, &sig, &osig);
1460 return (osig.sa_handler);
1464 handle_signals(void)
1466 if (reconfig_sig && running_state == DAEMON_RUNNING) {
1467 condlog(2, "reconfigure (signal)");
1468 pthread_cleanup_push(cleanup_lock,
1471 pthread_testcancel();
1473 lock_cleanup_pop(gvecs->lock);
1475 if (log_reset_sig) {
1476 condlog(2, "reset log (signal)");
1477 pthread_mutex_lock(&logq_lock);
1478 log_reset("multipathd");
1479 pthread_mutex_unlock(&logq_lock);
1509 sigaddset(&set, SIGHUP);
1510 sigaddset(&set, SIGUSR1);
1511 pthread_sigmask(SIG_BLOCK, &set, NULL);
1513 signal_set(SIGHUP, sighup);
1514 signal_set(SIGUSR1, sigusr1);
1515 signal_set(SIGINT, sigend);
1516 signal_set(SIGTERM, sigend);
1517 signal(SIGPIPE, SIG_IGN);
1524 static struct sched_param sched_param = {
1525 .sched_priority = 99
1528 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1531 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1538 #ifdef OOM_SCORE_ADJ_MIN
1540 char *file = "/proc/self/oom_score_adj";
1541 int score = OOM_SCORE_ADJ_MIN;
1544 char *file = "/proc/self/oom_adj";
1545 int score = OOM_ADJUST_MIN;
1551 if (stat(file, &st) == 0){
1552 fp = fopen(file, "w");
1554 condlog(0, "couldn't fopen %s : %s", file,
1558 fprintf(fp, "%i", score);
1562 if (errno != ENOENT) {
1563 condlog(0, "couldn't stat %s : %s", file,
1567 #ifdef OOM_ADJUST_MIN
1568 file = "/proc/self/oom_adj";
1569 score = OOM_ADJUST_MIN;
1574 condlog(0, "couldn't adjust oom score");
1578 child (void * param)
1580 pthread_t check_thr, uevent_thr, uxlsnr_thr, uevq_thr;
1581 pthread_attr_t log_attr, misc_attr;
1582 struct vectors * vecs;
1583 struct multipath * mpp;
1587 mlockall(MCL_CURRENT | MCL_FUTURE);
1588 sem_init(&exit_sem, 0, 0);
1591 setup_thread_attr(&misc_attr, 64 * 1024, 1);
1592 setup_thread_attr(&waiter_attr, 32 * 1024, 1);
1595 setup_thread_attr(&log_attr, 64 * 1024, 0);
1596 log_thread_start(&log_attr);
1597 pthread_attr_destroy(&log_attr);
1600 running_state = DAEMON_START;
1602 condlog(2, "--------start up--------");
1603 condlog(2, "read " DEFAULT_CONFIGFILE);
1605 if (load_config(DEFAULT_CONFIGFILE))
1608 if (init_checkers()) {
1609 condlog(0, "failed to initialize checkers");
1613 condlog(0, "failed to initialize prioritizers");
1617 setlogmask(LOG_UPTO(conf->verbosity + 3));
1619 if (conf->max_fds) {
1620 struct rlimit fd_limit;
1622 if (getrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
1623 condlog(0, "can't get open fds limit: %s",
1625 fd_limit.rlim_cur = 0;
1626 fd_limit.rlim_max = 0;
1628 if (fd_limit.rlim_cur < conf->max_fds) {
1629 fd_limit.rlim_cur = conf->max_fds;
1630 if (fd_limit.rlim_max < conf->max_fds)
1631 fd_limit.rlim_max = conf->max_fds;
1632 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
1633 condlog(0, "can't set open fds limit to "
1635 fd_limit.rlim_cur, fd_limit.rlim_max,
1638 condlog(3, "set open fds limit to %lu/%lu",
1639 fd_limit.rlim_cur, fd_limit.rlim_max);
1645 vecs = gvecs = init_vecs();
1653 udev_set_sync_support(0);
1655 * Start uevent listener early to catch events
1657 if ((rc = pthread_create(&uevent_thr, &misc_attr, ueventloop, vecs))) {
1658 condlog(0, "failed to create uevent thread: %d", rc);
1661 if ((rc = pthread_create(&uxlsnr_thr, &misc_attr, uxlsnrloop, vecs))) {
1662 condlog(0, "failed to create cli listener: %d", rc);
1666 * fetch and configure both paths and multipaths
1668 running_state = DAEMON_CONFIGURE;
1671 if (configure(vecs, 1)) {
1673 condlog(0, "failure during configuration");
1681 if ((rc = pthread_create(&check_thr, &misc_attr, checkerloop, vecs))) {
1682 condlog(0,"failed to create checker loop thread: %d", rc);
1685 if ((rc = pthread_create(&uevq_thr, &misc_attr, uevqloop, vecs))) {
1686 condlog(0, "failed to create uevent dispatcher: %d", rc);
1689 pthread_attr_destroy(&misc_attr);
1691 /* Startup complete, create logfile */
1692 pid_rc = pidfile_create(DEFAULT_PIDFILE, daemon_pid);
1693 /* Ignore errors, we can live without */
1695 running_state = DAEMON_RUNNING;
1700 while(sem_wait(&exit_sem) != 0); /* Do nothing */
1701 running_state = DAEMON_SHUTDOWN;
1703 if (conf->queue_without_daemon == QUE_NO_DAEMON_OFF)
1704 vector_foreach_slot(vecs->mpvec, mpp, i)
1705 dm_queue_if_no_path(mpp->alias, 0);
1706 remove_maps_and_stop_waiters(vecs);
1709 pthread_cancel(check_thr);
1710 pthread_cancel(uevent_thr);
1711 pthread_cancel(uxlsnr_thr);
1712 pthread_cancel(uevq_thr);
1715 free_pathvec(vecs->pathvec, FREE_PATHS);
1716 vecs->pathvec = NULL;
1718 /* Now all the waitevent threads will start rushing in. */
1719 while (vecs->lock.depth > 0) {
1720 sleep (1); /* This is weak. */
1721 condlog(3, "Have %d wait event checkers threads to de-alloc,"
1722 " waiting...", vecs->lock.depth);
1724 pthread_mutex_destroy(vecs->lock.mutex);
1725 FREE(vecs->lock.mutex);
1726 vecs->lock.depth = 0;
1727 vecs->lock.mutex = NULL;
1737 /* We're done here */
1739 condlog(3, "unlink pidfile");
1740 unlink(DEFAULT_PIDFILE);
1743 condlog(2, "--------shut down-------");
1749 * Freeing config must be done after condlog() and dm_lib_exit(),
1750 * because logging functions like dlog() and dm_write_log()
1751 * reference the config.
1757 dbg_free_final(NULL);
1769 if( (pid = fork()) < 0){
1770 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1778 if ( (pid = fork()) < 0)
1779 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1784 fprintf(stderr, "cannot chdir to '/', continuing\n");
1786 dev_null_fd = open("/dev/null", O_RDWR);
1787 if (dev_null_fd < 0){
1788 fprintf(stderr, "cannot open /dev/null for input & output : %s\n",
1793 close(STDIN_FILENO);
1794 if (dup(dev_null_fd) < 0) {
1795 fprintf(stderr, "cannot dup /dev/null to stdin : %s\n",
1799 close(STDOUT_FILENO);
1800 if (dup(dev_null_fd) < 0) {
1801 fprintf(stderr, "cannot dup /dev/null to stdout : %s\n",
1805 close(STDERR_FILENO);
1806 if (dup(dev_null_fd) < 0) {
1807 fprintf(stderr, "cannot dup /dev/null to stderr : %s\n",
1812 daemon_pid = getpid();
1817 main (int argc, char *argv[])
1819 extern char *optarg;
1825 running_state = DAEMON_INIT;
1828 if (getuid() != 0) {
1829 fprintf(stderr, "need to be root\n");
1833 /* make sure we don't lock any path */
1835 fprintf(stderr, "can't chdir to root directory : %s\n",
1837 umask(umask(077) | 022);
1839 conf = alloc_config();
1844 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1848 //debug=1; /* ### comment me out ### */
1851 if (sizeof(optarg) > sizeof(char *) ||
1852 !isdigit(optarg[0]))
1855 conf->verbosity = atoi(optarg);
1864 if (optind < argc) {
1869 while (optind < argc) {
1870 if (strchr(argv[optind], ' '))
1871 c += snprintf(c, s + CMDSIZE - c, "\"%s\" ", argv[optind]);
1873 c += snprintf(c, s + CMDSIZE - c, "%s ", argv[optind]);
1876 c += snprintf(c, s + CMDSIZE - c, "\n");
1894 return (child(NULL));
1897 void * mpath_pr_event_handler_fn (void * pathp )
1899 struct multipath * mpp;
1900 int i,j, ret, isFound;
1901 struct path * pp = (struct path *)pathp;
1902 unsigned char *keyp;
1904 struct prout_param_descriptor *param;
1905 struct prin_resp *resp;
1909 resp = mpath_alloc_prin_response(MPATH_PRIN_RKEY_SA);
1911 condlog(0,"%s Alloc failed for prin response", pp->dev);
1915 ret = prin_do_scsi_ioctl(pp->dev, MPATH_PRIN_RKEY_SA, resp, 0);
1916 if (ret != MPATH_PR_SUCCESS )
1918 condlog(0,"%s : pr in read keys service action failed. Error=%d", pp->dev, ret);
1922 condlog(3, " event pr=%d addlen=%d",resp->prin_descriptor.prin_readkeys.prgeneration,
1923 resp->prin_descriptor.prin_readkeys.additional_length );
1925 if (resp->prin_descriptor.prin_readkeys.additional_length == 0 )
1927 condlog(1, "%s: No key found. Device may not be registered.", pp->dev);
1928 ret = MPATH_PR_SUCCESS;
1932 keyp = (unsigned char *)mpp->reservation_key;
1933 for (j = 0; j < 8; ++j) {
1939 condlog(2, "Multipath reservation_key: 0x%" PRIx64 " ", prkey);
1942 for (i = 0; i < resp->prin_descriptor.prin_readkeys.additional_length/8; i++ )
1944 condlog(2, "PR IN READKEYS[%d] reservation key:",i);
1945 dumpHex((char *)&resp->prin_descriptor.prin_readkeys.key_list[i*8], 8 , -1);
1946 if (!memcmp(mpp->reservation_key, &resp->prin_descriptor.prin_readkeys.key_list[i*8], 8))
1948 condlog(2, "%s: pr key found in prin readkeys response", mpp->alias);
1955 condlog(0, "%s: Either device not registered or ", pp->dev);
1956 condlog(0, "host is not authorised for registration. Skip path");
1957 ret = MPATH_PR_OTHER;
1961 param= malloc(sizeof(struct prout_param_descriptor));
1962 memset(param, 0 , sizeof(struct prout_param_descriptor));
1964 for (j = 7; j >= 0; --j) {
1965 param->sa_key[j] = (prkey & 0xff);
1968 param->num_transportid = 0;
1970 condlog(3, "device %s:%s", pp->dev, pp->mpp->wwid);
1972 ret = prout_do_scsi_ioctl(pp->dev, MPATH_PROUT_REG_IGN_SA, 0, 0, param, 0);
1973 if (ret != MPATH_PR_SUCCESS )
1975 condlog(0,"%s: Reservation registration failed. Error: %d", pp->dev, ret);
1985 int mpath_pr_event_handle(struct path *pp)
1989 pthread_attr_t attr;
1990 struct multipath * mpp;
1994 if (!mpp->reservation_key)
1997 pthread_attr_init(&attr);
1998 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
2000 rc = pthread_create(&thread, NULL , mpath_pr_event_handler_fn, pp);
2002 condlog(0, "%s: ERROR; return code from pthread_create() is %d", pp->dev, rc);
2005 pthread_attr_destroy(&attr);
2006 rc = pthread_join(thread, NULL);