2 * Soft: multipath device mapper target autoconfig
4 * Version: $Id: main.h,v 0.0.1 2003/09/18 15:13:38 cvaroqui Exp $
6 * Author: Copyright (C) 2003 Christophe Varoqui
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 * See the GNU General Public License for more details.
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
29 #include <libdevmapper.h>
30 #include <devmapper.h>
32 #include <path_state.h>
33 #include <blacklist.h>
42 #include <discovery.h>
44 #include <switchgroup.h>
45 #include <sysfs/libsysfs.h>
49 #include "pgpolicies.h"
52 /* for column aligned output */
53 struct path_layout pl;
56 get_refwwid (vector pathvec)
59 char buff[FILE_NAME_SIZE];
62 if (conf->dev_type == DEV_NONE)
65 if (conf->dev_type == DEV_DEVNODE) {
66 condlog(3, "limited scope = %s", conf->dev);
67 basename(conf->dev, buff);
68 pp = find_path_by_dev(pathvec, buff);
76 strncpy(pp->dev, buff, FILE_NAME_SIZE);
78 if (pathinfo(pp, conf->hwtable, DI_SYSFS | DI_WWID))
81 if (store_path(pathvec, pp)) {
87 refwwid = MALLOC(WWID_SIZE);
92 memcpy(refwwid, pp->wwid, WWID_SIZE);
96 if (conf->dev_type == DEV_DEVT) {
97 condlog(3, "limited scope = %s", conf->dev);
98 pp = find_path_by_devt(pathvec, conf->dev);
101 if (devt2devname(conf->dev, buff))
109 if(safe_sprintf(pp->dev, "%s", buff)) {
110 fprintf(stderr, "pp->dev too small\n");
113 if (pathinfo(pp, conf->hwtable, DI_SYSFS | DI_WWID))
116 if (store_path(pathvec, pp)) {
122 refwwid = MALLOC(WWID_SIZE);
127 memcpy(refwwid, pp->wwid, WWID_SIZE);
130 if (conf->dev_type == DEV_DEVMAP) {
131 condlog(3, "limited scope = %s", conf->dev);
135 refwwid = get_mpe_wwid(conf->dev);
138 return STRDUP(refwwid);
143 refwwid = MALLOC(WWID_SIZE);
148 strncpy(refwwid, conf->dev, WWID_SIZE);
155 print_path (struct path * pp, char * style)
157 char buff[MAX_LINE_LEN];
159 snprint_path(&buff[0], MAX_LINE_LEN, style, pp, &pl);
164 print_map (struct multipath * mpp)
166 if (mpp->size && mpp->params)
167 printf("0 %llu %s %s\n",
168 mpp->size, DEFAULT_TARGET, mpp->params);
173 print_all_paths (vector pathvec)
178 vector_foreach_slot (pathvec, pp, i)
179 print_path(pp, PRINT_PATH_LONG);
183 print_mp (struct multipath * mpp)
186 struct path * pp = NULL;
187 struct pathgroup * pgp = NULL;
189 if (mpp->action == ACT_NOTHING || !conf->verbosity || !mpp->size)
192 if (conf->verbosity > 1) {
193 switch (mpp->action) {
195 printf("%s: ", ACT_RELOAD_STR);
199 printf("%s: ", ACT_CREATE_STR);
203 printf("%s: ", ACT_SWITCHPG_STR);
212 printf("%s", mpp->alias);
214 if (conf->verbosity == 1) {
218 if (strncmp(mpp->alias, mpp->wwid, WWID_SIZE))
219 printf(" (%s)", mpp->wwid);
223 if (mpp->size < (1 << 11))
224 printf("[size=%llu kB]", mpp->size >> 1);
225 else if (mpp->size < (1 << 21))
226 printf("[size=%llu MB]", mpp->size >> 11);
227 else if (mpp->size < (1 << 31))
228 printf("[size=%llu GB]", mpp->size >> 21);
230 printf("[size=%llu TB]", mpp->size >> 31);
233 printf("[features=\"%s\"]", mpp->features);
236 printf("[hwhandler=\"%s\"]", mpp->hwhandler);
238 fprintf(stdout, "\n");
243 vector_foreach_slot (mpp->pg, pgp, j) {
247 printf("%s ", mpp->selector);
249 /* align to path status info */
250 for (i = pl.hbtl_len + pl.dev_len + pl.dev_t_len + 4;
251 i > strlen(mpp->selector); i--)
256 printf("[prio=%i]", pgp->priority);
258 switch (pgp->status) {
259 case PGSTATE_ENABLED:
262 case PGSTATE_DISABLED:
263 printf("[disabled]");
273 vector_foreach_slot (pgp->paths, pp, i)
274 print_path(pp, PRINT_PATH_INDENT);
280 filter_pathvec (vector pathvec, char * refwwid)
285 if (!refwwid || !strlen(refwwid))
288 vector_foreach_slot (pathvec, pp, i) {
289 if (memcmp(pp->wwid, refwwid, WWID_SIZE) != 0) {
290 condlog(3, "skip path %s : out of scope", pp->dev);
292 vector_del_slot(pathvec, i);
300 * Transforms the path group vector into a proper device map string
303 assemble_map (struct multipath * mp)
309 struct pathgroup * pgp;
313 freechar = sizeof(mp->params);
315 shift = snprintf(p, freechar, "%s %s %i %i",
316 mp->features, mp->hwhandler,
317 VECTOR_SIZE(mp->pg), mp->nextpg);
319 if (shift >= freechar) {
320 fprintf(stderr, "mp->params too small\n");
326 vector_foreach_slot (mp->pg, pgp, i) {
327 pgp = VECTOR_SLOT(mp->pg, i);
328 shift = snprintf(p, freechar, " %s %i 1", mp->selector,
329 VECTOR_SIZE(pgp->paths));
330 if (shift >= freechar) {
331 fprintf(stderr, "mp->params too small\n");
337 vector_foreach_slot (pgp->paths, pp, j) {
340 if (mp->rr_weight == RR_WEIGHT_PRIO && pp->priority)
341 minio *= pp->priority;
343 shift = snprintf(p, freechar, " %s %d",
345 if (shift >= freechar) {
346 fprintf(stderr, "mp->params too small\n");
354 fprintf(stderr, "mp->params too small\n");
357 snprintf(p, 1, "\n");
359 if (conf->verbosity > 2)
366 setup_map (struct multipath * mpp)
372 * don't bother if devmap size is unknown
374 if (mpp->size <= 0) {
375 condlog(3, "%s devmap size is unknown", mpp->alias);
380 * don't bother if a constituant path is claimed
381 * (not by the device mapper driver)
383 vector_foreach_slot (mpp->paths, pp, i) {
384 if (pp->claimed && pp->dmstate == PSTATE_UNDEF) {
385 condlog(3, "%s claimed", pp->dev);
391 * properties selectors
393 select_pgpolicy(mpp);
394 select_selector(mpp);
395 select_features(mpp);
396 select_hwhandler(mpp);
397 select_rr_weight(mpp);
398 select_no_path_retry(mpp);
401 * apply selected grouping policy to valid paths
403 switch (mpp->pgpolicy) {
408 one_path_per_group(mpp);
410 case GROUP_BY_SERIAL:
411 group_by_serial(mpp);
416 case GROUP_BY_NODE_NAME:
417 group_by_node_name(mpp);
423 if (mpp->pg == NULL) {
424 condlog(3, "pgpolicy failed to produce a pg vector");
429 * ponders each path group and determine highest prio pg
430 * to switch over (default to first)
432 select_path_group(mpp);
435 * transform the mp->pg vector of vectors of paths
436 * into a mp->params strings to feed the device-mapper
438 if (assemble_map(mpp)) {
439 condlog(3, "problem assembing map");
446 pathcount (struct multipath * mpp, int state)
448 struct pathgroup *pgp;
453 vector_foreach_slot (mpp->pg, pgp, i)
454 vector_foreach_slot (pgp->paths, pp, j)
455 if (pp->state == state)
461 compute_pgid(struct pathgroup * pgp)
466 vector_foreach_slot (pgp->paths, pp, i)
471 pgcmp (struct multipath * mpp, struct multipath * cmpp)
474 struct pathgroup * pgp;
475 struct pathgroup * cpgp;
478 vector_foreach_slot (mpp->pg, pgp, i) {
481 vector_foreach_slot (cmpp->pg, cpgp, j) {
482 if (pgp->id == cpgp->id) {
495 select_action (struct multipath * mpp, vector curmp)
497 struct multipath * cmpp;
499 cmpp = find_mp(curmp, mpp->alias);
502 cmpp = find_mp_by_wwid(curmp, mpp->wwid);
504 if (cmpp && !conf->dry_run) {
505 condlog(2, "remove: %s (dup of %s)",
506 cmpp->alias, mpp->alias);
507 dm_flush_map(cmpp->alias, DEFAULT_TARGET);
509 mpp->action = ACT_CREATE;
510 condlog(3, "set ACT_CREATE: map does not exists");
514 if (!find_mp_by_wwid(curmp, mpp->wwid)) {
515 condlog(2, "remove: %s (wwid changed)", cmpp->alias);
516 dm_flush_map(mpp->alias, NULL);
517 strncat(cmpp->wwid, mpp->wwid, WWID_SIZE);
518 drop_multipath(curmp, cmpp->wwid, KEEP_PATHS);
519 mpp->action = ACT_CREATE;
520 condlog(3, "set ACT_CREATE: map wwid change");
524 if (pathcount(mpp, PATH_UP) == 0) {
525 mpp->action = ACT_NOTHING;
526 condlog(3, "set ACT_NOTHING: no usable path");
529 if (cmpp->size != mpp->size) {
530 mpp->action = ACT_RELOAD;
531 condlog(3, "set ACT_RELOAD: size change");
534 if (strncmp(cmpp->features, mpp->features,
535 strlen(mpp->features))) {
536 mpp->action = ACT_RELOAD;
537 condlog(3, "set ACT_RELOAD: features change");
540 if (strncmp(cmpp->hwhandler, mpp->hwhandler,
541 strlen(mpp->hwhandler))) {
542 mpp->action = ACT_RELOAD;
543 condlog(3, "set ACT_RELOAD: hwhandler change");
546 if (strncmp(cmpp->selector, mpp->selector,
547 strlen(mpp->selector))) {
548 mpp->action = ACT_RELOAD;
549 condlog(3, "set ACT_RELOAD: selector change");
552 if (VECTOR_SIZE(cmpp->pg) != VECTOR_SIZE(mpp->pg)) {
553 mpp->action = ACT_RELOAD;
554 condlog(3, "set ACT_RELOAD: number of path group change");
557 if (pgcmp(mpp, cmpp)) {
558 mpp->action = ACT_RELOAD;
559 condlog(3, "set ACT_RELOAD: path group topology change");
562 if (cmpp->nextpg != mpp->nextpg) {
563 mpp->action = ACT_SWITCHPG;
564 condlog(3, "set ACT_SWITCHPG: next path group change");
567 mpp->action = ACT_NOTHING;
568 condlog(3, "set ACT_NOTHING: map unchanged");
573 reinstate_paths (struct multipath * mpp)
576 struct pathgroup * pgp;
582 vector_foreach_slot (mpp->pg, pgp, i) {
586 vector_foreach_slot (pgp->paths, pp, j) {
587 if (pp->state != PATH_UP &&
588 (pgp->status == PGSTATE_DISABLED ||
589 pgp->status == PGSTATE_ACTIVE))
592 if (pp->dmstate == PSTATE_FAILED) {
593 if (dm_reinstate(mpp->alias, pp->dev_t))
594 condlog(0, "error reinstating %s",
602 int lock_multipath (struct multipath * mpp, int lock)
604 struct pathgroup * pgp;
608 if (!mpp || !mpp->pg)
611 vector_foreach_slot (mpp->pg, pgp, i) {
614 vector_foreach_slot(pgp->paths, pp, j) {
615 if (lock && flock(pp->fd, LOCK_EX | LOCK_NB) &&
616 errno == EWOULDBLOCK)
619 flock(pp->fd, LOCK_UN);
628 * 0: DM_DEVICE_CREATE or DM_DEVICE_RELOAD failed, or dry_run mode.
629 * 1: DM_DEVICE_CREATE or DM_DEVICE_RELOAD succeeded.
630 * 2: Map is already existing.
633 domap (struct multipath * mpp)
638 * last chance to quit before touching the devmaps
643 switch (mpp->action) {
648 dm_switchgroup(mpp->alias, mpp->nextpg);
650 * we may have avoided reinstating paths because there where in
651 * active or disabled PG. Now that the topology has changed,
654 reinstate_paths(mpp);
658 if (lock_multipath(mpp, 1)) {
659 condlog(3, "%s: in use", mpp->alias);
663 if (dm_map_present(mpp->alias))
666 r = dm_addmap(DM_DEVICE_CREATE, mpp->alias, DEFAULT_TARGET,
667 mpp->params, mpp->size, mpp->wwid);
671 r = (dm_addmap(DM_DEVICE_RELOAD, mpp->alias, DEFAULT_TARGET,
672 mpp->params, mpp->size, NULL) &&
673 dm_simplecmd(DM_DEVICE_RESUME, mpp->alias));
682 * DM_DEVICE_CREATE or DM_DEVICE_RELOAD succeeded
684 dm_switchgroup(mpp->alias, mpp->nextpg);
692 deadmap (struct multipath * mpp)
695 struct pathgroup * pgp;
701 vector_foreach_slot (mpp->pg, pgp, i) {
705 vector_foreach_slot (pgp->paths, pp, j)
707 return 0; /* alive */
714 coalesce_paths (vector curmp, vector pathvec)
718 char empty_buff[WWID_SIZE];
719 struct multipath * mpp;
723 memset(empty_buff, 0, WWID_SIZE);
725 vector_foreach_slot (pathvec, pp1, k) {
726 /* skip this path for some reason */
728 /* 1. if path has no unique id or wwid blacklisted */
729 if (memcmp(empty_buff, pp1->wwid, WWID_SIZE) == 0 ||
730 blacklist(conf->blist, pp1->wwid))
733 /* 2. if path already coalesced */
738 * at this point, we know we really got a new mp
740 mpp = alloc_multipath();
745 mpp->mpe = find_mpe(pp1->wwid);
750 strcpy(mpp->wwid, pp1->wwid);
751 mpp->size = pp1->size;
752 mpp->paths = vector_alloc();
754 if (pp1->priority < 0)
755 mpp->action = ACT_NOTHING;
760 if (store_path(mpp->paths, pp1))
763 for (i = k + 1; i < VECTOR_SIZE(pathvec); i++) {
764 pp2 = VECTOR_SLOT(pathvec, i);
766 if (strcmp(pp1->wwid, pp2->wwid))
771 if (pp2->size != mpp->size) {
773 * ouch, avoid feeding that to the DM
775 condlog(3, "path size mismatch : discard %s",
777 mpp->action = ACT_NOTHING;
779 if (pp2->priority < 0)
780 mpp->action = ACT_NOTHING;
782 if (store_path(mpp->paths, pp2))
788 if (mpp->action == ACT_UNDEF)
789 select_action(mpp, curmp);
796 if (r && mpp->no_path_retry != NO_PATH_RETRY_UNDEF) {
797 if (mpp->no_path_retry == NO_PATH_RETRY_FAIL)
798 dm_queue_if_no_path(mpp->alias, 0);
800 dm_queue_if_no_path(mpp->alias, 1);
804 drop_multipath(curmp, mpp->wwid, KEEP_PATHS);
805 free_multipath(mpp, KEEP_PATHS);
808 * Flush maps with only dead paths (ie not in sysfs)
809 * Keep maps with only failed paths
811 vector_foreach_slot (curmp, mpp, i) {
815 if (dm_flush_map(mpp->alias, DEFAULT_TARGET))
816 condlog(2, "remove: %s (dead) failed!",
819 condlog(2, "remove: %s (dead)", mpp->alias);
825 usage (char * progname)
827 fprintf (stderr, VERSION_STRING);
828 fprintf (stderr, "Usage: %s\t[-v level] [-d] [-l|-ll|-f|-F]\n",
831 "\t\t\t[-p failover|multibus|group_by_serial|group_by_prio]\n" \
834 "\t-v level\tverbosty level\n" \
835 "\t 0\t\t\tno output\n" \
836 "\t 1\t\t\tprint created devmap names only\n" \
837 "\t 2\t\t\tdefault verbosity\n" \
838 "\t 3\t\t\tprint debug information\n" \
839 "\t-d\t\tdry run, do not create or update devmaps\n" \
840 "\t-l\t\tshow multipath topology (sysfs and DM info)\n" \
841 "\t-ll\t\tshow multipath topology (maximum info)\n" \
842 "\t-F\t\tflush a multipath device map\n" \
843 "\t-F\t\tflush all multipath device maps\n" \
844 "\t-p policy\tforce all maps to specified policy :\n" \
845 "\t failover\t\t1 path per priority group\n" \
846 "\t multibus\t\tall paths in 1 priority group\n" \
847 "\t group_by_serial\t1 priority group per serial\n" \
848 "\t group_by_prio\t1 priority group per priority lvl\n" \
849 "\t group_by_node_name\t1 priority group per target node\n" \
851 "\tdevice\t\tlimit scope to the device's multipath\n" \
852 "\t\t\t(udev-style $DEVNAME reference, eg /dev/sdb\n" \
853 "\t\t\tor major:minor or a device map name)\n" \
860 update_paths (struct multipath * mpp)
863 struct pathgroup * pgp;
869 vector_foreach_slot (mpp->pg, pgp, i) {
873 vector_foreach_slot (pgp->paths, pp, j) {
874 if (!strlen(pp->dev)) {
875 if (devt2devname(pp->dev_t, pp->dev)) {
877 * path is not in sysfs anymore
879 pp->state = PATH_DOWN;
882 pathinfo(pp, conf->hwtable,
883 DI_SYSFS | DI_CHECKER | \
884 DI_SERIAL | DI_PRIO);
887 if (pp->state == PATH_UNCHECKED)
888 pathinfo(pp, conf->hwtable, DI_CHECKER);
891 pathinfo(pp, conf->hwtable, DI_PRIO);
898 get_dm_mpvec (vector curmp, vector pathvec, char * refwwid)
901 struct multipath * mpp;
903 if (dm_get_maps(curmp, DEFAULT_TARGET))
906 vector_foreach_slot (curmp, mpp, i) {
908 * discard out of scope maps
910 if (mpp->wwid && refwwid &&
911 strncmp(mpp->wwid, refwwid, WWID_SIZE)) {
912 condlog(3, "skip map %s: out of scope", mpp->alias);
913 free_multipath(mpp, KEEP_PATHS);
914 vector_del_slot(curmp, i);
919 condlog(3, "params = %s", mpp->params);
920 condlog(3, "status = %s", mpp->status);
922 disassemble_map(pathvec, mpp->params, mpp);
925 * disassemble_map() can add new paths to pathvec.
926 * If not in "fast list mode", we need to fetch information
933 select_path_group(mpp);
935 disassemble_status(mpp->status, mpp);
941 reinstate_paths(mpp);
957 vector pathvec = NULL;
960 char * refwwid = NULL;
963 * allocate core vectors to store paths and multipaths
965 curmp = vector_alloc();
966 pathvec = vector_alloc();
968 if (!curmp || !pathvec) {
969 condlog(0, "can not allocate memory");
974 * if we have a blacklisted device parameter, exit early
976 if (conf->dev && blacklist(conf->blist, conf->dev))
979 condlog(3, "load path identifiers cache");
982 if (conf->verbosity > 2) {
983 fprintf(stdout, "#\n# all paths in cache :\n#\n");
984 print_all_paths(pathvec);
994 /* extended path info '-ll' */
995 di_flag |= DI_SYSFS | DI_CHECKER;
997 /* minimum path info '-l' */
1003 if (path_discovery(pathvec, conf, di_flag))
1006 if (conf->verbosity > 2) {
1007 fprintf(stdout, "#\n# all paths :\n#\n");
1008 print_all_paths(pathvec);
1012 * scope limiting must be translated into a wwid
1013 * failing the translation is fatal (by policy)
1016 refwwid = get_refwwid(pathvec);
1019 condlog(3, "scope is nul");
1024 get_path_layout(&pl, pathvec);
1026 if (get_dm_mpvec(curmp, pathvec, refwwid))
1029 filter_pathvec(pathvec, refwwid);
1035 * core logic entry point
1037 r = coalesce_paths(curmp, pathvec);
1043 free_multipathvec(curmp, KEEP_PATHS);
1044 free_pathvec(pathvec, FREE_PATHS);
1050 main (int argc, char *argv[])
1053 extern char *optarg;
1057 if (getuid() != 0) {
1058 fprintf(stderr, "need to be root\n");
1062 if (dm_prereq(DEFAULT_TARGET, 1, 0, 3))
1065 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
1066 condlog(0, "multipath tools need sysfs mounted");
1069 if (load_config(DEFAULT_CONFIGFILE))
1072 while ((arg = getopt(argc, argv, ":qdl::Ffi:M:v:p:")) != EOF ) {
1074 case 1: printf("optarg : %s\n",optarg);
1077 if (sizeof(optarg) > sizeof(char *) ||
1078 !isdigit(optarg[0]))
1081 conf->verbosity = atoi(optarg);
1096 if (optarg && !strncmp(optarg, "l", 1))
1102 debug = atoi(optarg);
1106 conf->pgpolicy_flag = get_pgpolicy_id(optarg);
1107 if (conf->pgpolicy_flag == -1) {
1108 printf("'%s' is not a valid policy\n", optarg);
1113 fprintf(stderr, "Missing option arguement\n");
1116 fprintf(stderr, "Unknown switch: %s\n", optarg);
1122 if (optind < argc) {
1123 conf->dev = MALLOC(FILE_NAME_SIZE);
1128 strncpy(conf->dev, argv[optind], FILE_NAME_SIZE);
1130 if (filepresent(conf->dev))
1131 conf->dev_type = DEV_DEVNODE;
1132 else if (sscanf(conf->dev, "%d:%d", &i, &i) == 2)
1133 conf->dev_type = DEV_DEVT;
1135 conf->dev_type = DEV_DEVMAP;
1139 if (conf->remove == FLUSH_ONE) {
1140 if (conf->dev_type == DEV_DEVMAP)
1141 dm_flush_map(conf->dev, DEFAULT_TARGET);
1143 condlog(0, "must provide a map name to remove");
1147 else if (conf->remove == FLUSH_ALL) {
1148 dm_flush_maps(DEFAULT_TARGET);
1151 while ((r = configure()) < 0)
1152 condlog(3, "restart multipath configuration process");
1159 dbg_free_final(NULL);