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>
50 #include "pgpolicies.h"
53 /* for column aligned output */
54 struct path_layout pl;
57 get_refwwid (vector pathvec)
60 char buff[FILE_NAME_SIZE];
63 if (conf->dev_type == DEV_NONE)
66 if (conf->dev_type == DEV_DEVNODE) {
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)) {
90 if (conf->dev_type == DEV_DEVT) {
91 pp = find_path_by_devt(pathvec, conf->dev);
94 if (devt2devname(buff, conf->dev))
102 strncpy(pp->dev, buff, FILE_NAME_SIZE);
104 if (pathinfo(pp, conf->hwtable, DI_SYSFS | DI_WWID))
107 if (store_path(pathvec, pp)) {
115 if (conf->dev_type == DEV_DEVMAP) {
119 refwwid = get_user_friendly_wwid(conf->dev,
120 conf->bindings_file);
128 refwwid = get_mpe_wwid(conf->dev);
137 if (refwwid && strlen(refwwid))
138 return STRDUP(refwwid);
144 print_path (struct path * pp, char * style)
146 char line[MAX_LINE_LEN];
148 snprint_path(&line[0], MAX_LINE_LEN, style, pp, &pl);
153 print_map (struct multipath * mpp)
155 if (mpp->size && mpp->params)
156 printf("0 %llu %s %s\n",
157 mpp->size, DEFAULT_TARGET, mpp->params);
162 print_all_paths (vector pathvec, int banner)
166 char line[MAX_LINE_LEN];
168 if (!VECTOR_SIZE(pathvec)) {
170 fprintf(stdout, "===== no paths =====\n");
175 fprintf(stdout, "===== paths list =====\n");
177 get_path_layout(&pl, pathvec);
178 snprint_path_header(line, MAX_LINE_LEN, PRINT_PATH_LONG, &pl);
179 fprintf(stdout, "%s", line);
181 vector_foreach_slot (pathvec, pp, i)
182 print_path(pp, PRINT_PATH_LONG);
186 print_mp (struct multipath * mpp)
189 struct path * pp = NULL;
190 struct pathgroup * pgp = NULL;
192 if (mpp->action == ACT_NOTHING || !conf->verbosity || !mpp->size)
195 if (conf->verbosity > 1) {
196 switch (mpp->action) {
198 printf("%s: ", ACT_RELOAD_STR);
202 printf("%s: ", ACT_CREATE_STR);
206 printf("%s: ", ACT_SWITCHPG_STR);
215 printf("%s", mpp->alias);
217 if (conf->verbosity == 1) {
221 if (strncmp(mpp->alias, mpp->wwid, WWID_SIZE))
222 printf(" (%s)", mpp->wwid);
226 if (mpp->size < (1 << 11))
227 printf("[size=%llu kB]", mpp->size >> 1);
228 else if (mpp->size < (1 << 21))
229 printf("[size=%llu MB]", mpp->size >> 11);
230 else if (mpp->size < (1 << 31))
231 printf("[size=%llu GB]", mpp->size >> 21);
233 printf("[size=%llu TB]", mpp->size >> 31);
236 printf("[features=\"%s\"]", mpp->features);
239 printf("[hwhandler=\"%s\"]", mpp->hwhandler);
241 fprintf(stdout, "\n");
246 vector_foreach_slot (mpp->pg, pgp, j) {
250 printf("%s ", mpp->selector);
252 /* align to path status info */
253 for (i = pl.hbtl_len + pl.dev_len + pl.dev_t_len + 4;
254 i > strlen(mpp->selector); i--)
259 printf("[prio=%i]", pgp->priority);
261 switch (pgp->status) {
262 case PGSTATE_ENABLED:
265 case PGSTATE_DISABLED:
266 printf("[disabled]");
276 vector_foreach_slot (pgp->paths, pp, i)
277 print_path(pp, PRINT_PATH_INDENT);
283 filter_pathvec (vector pathvec, char * refwwid)
288 if (!refwwid || !strlen(refwwid))
291 vector_foreach_slot (pathvec, pp, i) {
292 if (strncmp(pp->wwid, refwwid, WWID_SIZE) != 0) {
293 condlog(3, "skip path %s : out of scope", pp->dev);
295 vector_del_slot(pathvec, i);
303 * Transforms the path group vector into a proper device map string
306 assemble_map (struct multipath * mp)
312 struct pathgroup * pgp;
316 freechar = sizeof(mp->params);
318 shift = snprintf(p, freechar, "%s %s %i %i",
319 mp->features, mp->hwhandler,
320 VECTOR_SIZE(mp->pg), mp->nextpg);
322 if (shift >= freechar) {
323 fprintf(stderr, "mp->params too small\n");
329 vector_foreach_slot (mp->pg, pgp, i) {
330 pgp = VECTOR_SLOT(mp->pg, i);
331 shift = snprintf(p, freechar, " %s %i 1", mp->selector,
332 VECTOR_SIZE(pgp->paths));
333 if (shift >= freechar) {
334 fprintf(stderr, "mp->params too small\n");
340 vector_foreach_slot (pgp->paths, pp, j) {
343 if (mp->rr_weight == RR_WEIGHT_PRIO && pp->priority)
344 minio *= pp->priority;
346 shift = snprintf(p, freechar, " %s %d",
348 if (shift >= freechar) {
349 fprintf(stderr, "mp->params too small\n");
357 fprintf(stderr, "mp->params too small\n");
360 snprintf(p, 1, "\n");
362 if (conf->verbosity > 2)
369 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 * properties selectors
382 select_pgpolicy(mpp);
383 select_selector(mpp);
384 select_features(mpp);
385 select_hwhandler(mpp);
386 select_rr_weight(mpp);
387 select_no_path_retry(mpp);
390 * apply selected grouping policy to valid paths
392 switch (mpp->pgpolicy) {
397 one_path_per_group(mpp);
399 case GROUP_BY_SERIAL:
400 group_by_serial(mpp);
405 case GROUP_BY_NODE_NAME:
406 group_by_node_name(mpp);
412 if (mpp->pg == NULL) {
413 condlog(3, "pgpolicy failed to produce a pg vector");
418 * ponders each path group and determine highest prio pg
419 * to switch over (default to first)
421 select_path_group(mpp);
424 * transform the mp->pg vector of vectors of paths
425 * into a mp->params strings to feed the device-mapper
427 if (assemble_map(mpp)) {
428 condlog(3, "problem assembing map");
435 pathcount (struct multipath * mpp, int state)
437 struct pathgroup *pgp;
442 vector_foreach_slot (mpp->pg, pgp, i)
443 vector_foreach_slot (pgp->paths, pp, j)
444 if (pp->state == state)
450 compute_pgid(struct pathgroup * pgp)
455 vector_foreach_slot (pgp->paths, pp, i)
460 pgcmp (struct multipath * mpp, struct multipath * cmpp)
463 struct pathgroup * pgp;
464 struct pathgroup * cpgp;
467 vector_foreach_slot (mpp->pg, pgp, i) {
470 vector_foreach_slot (cmpp->pg, cpgp, j) {
471 if (pgp->id == cpgp->id) {
484 select_action (struct multipath * mpp, vector curmp)
486 struct multipath * cmpp;
488 cmpp = find_mp_by_alias(curmp, mpp->alias);
491 cmpp = find_mp_by_wwid(curmp, mpp->wwid);
493 if (cmpp && !conf->dry_run) {
494 condlog(2, "remove: %s (dup of %s)",
495 cmpp->alias, mpp->alias);
496 dm_flush_map(cmpp->alias, DEFAULT_TARGET);
498 mpp->action = ACT_CREATE;
499 condlog(3, "set ACT_CREATE: map does not exists");
503 if (!find_mp_by_wwid(curmp, mpp->wwid)) {
504 condlog(2, "remove: %s (wwid changed)", cmpp->alias);
505 dm_flush_map(mpp->alias, NULL);
506 strncat(cmpp->wwid, mpp->wwid, WWID_SIZE);
507 drop_multipath(curmp, cmpp->wwid, KEEP_PATHS);
508 mpp->action = ACT_CREATE;
509 condlog(3, "set ACT_CREATE: map wwid change");
513 if (pathcount(mpp, PATH_UP) == 0) {
514 mpp->action = ACT_NOTHING;
515 condlog(3, "set ACT_NOTHING: no usable path");
518 if (cmpp->size != mpp->size) {
519 mpp->action = ACT_RELOAD;
520 condlog(3, "set ACT_RELOAD: size change");
523 if (!mpp->no_path_retry && /* let features be handled by the daemon */
524 strncmp(cmpp->features, mpp->features, strlen(mpp->features))) {
525 mpp->action = ACT_RELOAD;
526 condlog(3, "set ACT_RELOAD: features change");
529 if (strncmp(cmpp->hwhandler, mpp->hwhandler,
530 strlen(mpp->hwhandler))) {
531 mpp->action = ACT_RELOAD;
532 condlog(3, "set ACT_RELOAD: hwhandler change");
535 if (strncmp(cmpp->selector, mpp->selector,
536 strlen(mpp->selector))) {
537 mpp->action = ACT_RELOAD;
538 condlog(3, "set ACT_RELOAD: selector change");
541 if (VECTOR_SIZE(cmpp->pg) != VECTOR_SIZE(mpp->pg)) {
542 mpp->action = ACT_RELOAD;
543 condlog(3, "set ACT_RELOAD: number of path group change");
546 if (pgcmp(mpp, cmpp)) {
547 mpp->action = ACT_RELOAD;
548 condlog(3, "set ACT_RELOAD: path group topology change");
551 if (cmpp->nextpg != mpp->nextpg) {
552 mpp->action = ACT_SWITCHPG;
553 condlog(3, "set ACT_SWITCHPG: next path group change");
556 mpp->action = ACT_NOTHING;
557 condlog(3, "set ACT_NOTHING: map unchanged");
562 reinstate_paths (struct multipath * mpp)
565 struct pathgroup * pgp;
571 vector_foreach_slot (mpp->pg, pgp, i) {
575 vector_foreach_slot (pgp->paths, pp, j) {
576 if (pp->state != PATH_UP &&
577 (pgp->status == PGSTATE_DISABLED ||
578 pgp->status == PGSTATE_ACTIVE))
581 if (pp->dmstate == PSTATE_FAILED) {
582 if (dm_reinstate(mpp->alias, pp->dev_t))
583 condlog(0, "error reinstating %s",
591 int lock_multipath (struct multipath * mpp, int lock)
593 struct pathgroup * pgp;
597 if (!mpp || !mpp->pg)
600 vector_foreach_slot (mpp->pg, pgp, i) {
603 vector_foreach_slot(pgp->paths, pp, j) {
604 if (lock && flock(pp->fd, LOCK_EX | LOCK_NB) &&
605 errno == EWOULDBLOCK)
608 flock(pp->fd, LOCK_UN);
617 * 0: DM_DEVICE_CREATE or DM_DEVICE_RELOAD failed, or dry_run mode.
618 * 1: DM_DEVICE_CREATE or DM_DEVICE_RELOAD succeeded.
619 * 2: Map is already existing.
622 domap (struct multipath * mpp)
627 * last chance to quit before touching the devmaps
632 switch (mpp->action) {
637 dm_switchgroup(mpp->alias, mpp->nextpg);
639 * we may have avoided reinstating paths because there where in
640 * active or disabled PG. Now that the topology has changed,
643 reinstate_paths(mpp);
647 if (lock_multipath(mpp, 1)) {
648 condlog(3, "%s: in use", mpp->alias);
653 if (dm_map_present(mpp->alias))
656 r = dm_addmap(DM_DEVICE_CREATE, mpp->alias, DEFAULT_TARGET,
657 mpp->params, mpp->size, mpp->wwid);
660 * DM_DEVICE_CREATE is actually DM_DEV_CREATE plus
661 * DM_TABLE_LOAD. Failing the second part leaves an
662 * empty map. Clean it up.
664 if (!r && dm_map_present(mpp->alias)) {
665 condlog(3, "%s: failed to load map "
666 "(a path might be in use)",
668 dm_flush_map(mpp->alias, NULL);
671 lock_multipath(mpp, 0);
676 r = (dm_addmap(DM_DEVICE_RELOAD, mpp->alias, DEFAULT_TARGET,
677 mpp->params, mpp->size, NULL) &&
678 dm_simplecmd(DM_DEVICE_RESUME, mpp->alias));
687 * DM_DEVICE_CREATE or DM_DEVICE_RELOAD succeeded
689 dm_switchgroup(mpp->alias, mpp->nextpg);
697 deadmap (struct multipath * mpp)
700 struct pathgroup * pgp;
706 vector_foreach_slot (mpp->pg, pgp, i) {
710 vector_foreach_slot (pgp->paths, pp, j)
712 return 0; /* alive */
719 coalesce_paths (vector curmp, vector pathvec)
723 char empty_buff[WWID_SIZE];
724 struct multipath * mpp;
728 memset(empty_buff, 0, WWID_SIZE);
730 vector_foreach_slot (pathvec, pp1, k) {
731 /* skip this path for some reason */
733 /* 1. if path has no unique id or wwid blacklisted */
734 if (memcmp(empty_buff, pp1->wwid, WWID_SIZE) == 0 ||
735 blacklist(conf->blist, pp1->wwid))
738 /* 2. if path already coalesced */
743 * at this point, we know we really got a new mp
745 mpp = alloc_multipath();
750 mpp->mpe = find_mpe(pp1->wwid);
752 strcpy(mpp->wwid, pp1->wwid);
756 mpp->size = pp1->size;
757 mpp->paths = vector_alloc();
759 if (pp1->priority < 0)
760 mpp->action = ACT_NOTHING;
765 if (store_path(mpp->paths, pp1))
768 for (i = k + 1; i < VECTOR_SIZE(pathvec); i++) {
769 pp2 = VECTOR_SLOT(pathvec, i);
771 if (strcmp(pp1->wwid, pp2->wwid))
776 if (pp2->size != mpp->size) {
778 * ouch, avoid feeding that to the DM
780 condlog(3, "path size mismatch : discard %s",
782 mpp->action = ACT_NOTHING;
784 if (pp2->priority < 0)
785 mpp->action = ACT_NOTHING;
787 if (store_path(mpp->paths, pp2))
793 if (mpp->action == ACT_UNDEF)
794 select_action(mpp, curmp);
801 if (r && mpp->no_path_retry != NO_PATH_RETRY_UNDEF) {
802 if (mpp->no_path_retry == NO_PATH_RETRY_FAIL)
803 dm_queue_if_no_path(mpp->alias, 0);
805 dm_queue_if_no_path(mpp->alias, 1);
809 drop_multipath(curmp, mpp->wwid, KEEP_PATHS);
810 free_multipath(mpp, KEEP_PATHS);
813 * Flush maps with only dead paths (ie not in sysfs)
814 * Keep maps with only failed paths
816 vector_foreach_slot (curmp, mpp, i) {
820 if (dm_flush_map(mpp->alias, DEFAULT_TARGET))
821 condlog(2, "remove: %s (dead) failed!",
824 condlog(2, "remove: %s (dead)", mpp->alias);
830 usage (char * progname)
832 fprintf (stderr, VERSION_STRING);
833 fprintf (stderr, "Usage: %s\t[-v level] [-d] [-l|-ll|-f|-F]\n",
836 "\t\t\t[-p failover|multibus|group_by_serial|group_by_prio]\n" \
839 "\t-v level\tverbosity level\n" \
840 "\t 0\t\t\tno output\n" \
841 "\t 1\t\t\tprint created devmap names only\n" \
842 "\t 2\t\t\tdefault verbosity\n" \
843 "\t 3\t\t\tprint debug information\n" \
844 "\t-b file\t\tbindings file location\n" \
845 "\t-d\t\tdry run, do not create or update devmaps\n" \
846 "\t-l\t\tshow multipath topology (sysfs and DM info)\n" \
847 "\t-ll\t\tshow multipath topology (maximum info)\n" \
848 "\t-f\t\tflush a multipath device map\n" \
849 "\t-F\t\tflush all multipath device maps\n" \
850 "\t-p policy\tforce all maps to specified policy :\n" \
851 "\t failover\t\t1 path per priority group\n" \
852 "\t multibus\t\tall paths in 1 priority group\n" \
853 "\t group_by_serial\t1 priority group per serial\n" \
854 "\t group_by_prio\t1 priority group per priority lvl\n" \
855 "\t group_by_node_name\t1 priority group per target node\n" \
857 "\tdevice\t\tlimit scope to the device's multipath\n" \
858 "\t\t\t(udev-style $DEVNAME reference, eg /dev/sdb\n" \
859 "\t\t\tor major:minor or a device map name)\n" \
866 update_paths (struct multipath * mpp)
869 struct pathgroup * pgp;
875 vector_foreach_slot (mpp->pg, pgp, i) {
879 vector_foreach_slot (pgp->paths, pp, j) {
880 if (!strlen(pp->dev)) {
881 if (devt2devname(pp->dev, pp->dev_t)) {
883 * path is not in sysfs anymore
885 pp->state = PATH_DOWN;
888 pathinfo(pp, conf->hwtable,
889 DI_SYSFS | DI_CHECKER | \
890 DI_SERIAL | DI_PRIO);
893 if (pp->state == PATH_UNCHECKED)
894 pathinfo(pp, conf->hwtable, DI_CHECKER);
897 pathinfo(pp, conf->hwtable, DI_PRIO);
904 get_dm_mpvec (vector curmp, vector pathvec, char * refwwid)
907 struct multipath * mpp;
909 if (dm_get_maps(curmp, DEFAULT_TARGET))
912 vector_foreach_slot (curmp, mpp, i) {
914 * discard out of scope maps
916 if (mpp->wwid && refwwid &&
917 strncmp(mpp->wwid, refwwid, WWID_SIZE)) {
918 condlog(3, "skip map %s: out of scope", mpp->alias);
919 free_multipath(mpp, KEEP_PATHS);
920 vector_del_slot(curmp, i);
925 condlog(3, "params = %s", mpp->params);
926 condlog(3, "status = %s", mpp->status);
928 disassemble_map(pathvec, mpp->params, mpp);
931 * disassemble_map() can add new paths to pathvec.
932 * If not in "fast list mode", we need to fetch information
939 select_path_group(mpp);
941 disassemble_status(mpp->status, mpp);
947 reinstate_paths(mpp);
963 vector pathvec = NULL;
966 char * refwwid = NULL;
970 * allocate core vectors to store paths and multipaths
972 curmp = vector_alloc();
973 pathvec = vector_alloc();
975 if (!curmp || !pathvec) {
976 condlog(0, "can not allocate memory");
981 * if we have a blacklisted device parameter, exit early
984 if (!strncmp(conf->dev, "/dev/", 5) &&
985 strlen(conf->dev) > 5)
991 if (dev && blacklist(conf->blist, dev))
994 condlog(3, "load path identifiers cache");
997 if (conf->verbosity > 2)
998 print_all_paths(pathvec, 1);
1001 * scope limiting must be translated into a wwid
1002 * failing the translation is fatal (by policy)
1005 refwwid = get_refwwid(pathvec);
1008 condlog(3, "scope is nul");
1011 condlog(3, "scope limited to %s", refwwid);
1021 /* extended path info '-ll' */
1022 di_flag |= DI_SYSFS | DI_CHECKER;
1023 else if (conf->list)
1024 /* minimum path info '-l' */
1025 di_flag |= DI_SYSFS;
1030 if (path_discovery(pathvec, conf, di_flag))
1033 if (conf->verbosity > 2)
1034 print_all_paths(pathvec, 1);
1036 get_path_layout(&pl, pathvec);
1038 if (get_dm_mpvec(curmp, pathvec, refwwid))
1041 filter_pathvec(pathvec, refwwid);
1047 * core logic entry point
1049 r = coalesce_paths(curmp, pathvec);
1055 free_multipathvec(curmp, KEEP_PATHS);
1056 free_pathvec(pathvec, FREE_PATHS);
1062 main (int argc, char *argv[])
1065 extern char *optarg;
1069 if (getuid() != 0) {
1070 fprintf(stderr, "need to be root\n");
1074 if (dm_prereq(DEFAULT_TARGET, 1, 0, 3))
1077 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
1078 condlog(0, "multipath tools need sysfs mounted");
1081 if (load_config(DEFAULT_CONFIGFILE))
1084 while ((arg = getopt(argc, argv, ":qdl::Ffi:M:v:p:b:")) != EOF ) {
1086 case 1: printf("optarg : %s\n",optarg);
1089 if (sizeof(optarg) > sizeof(char *) ||
1090 !isdigit(optarg[0]))
1093 conf->verbosity = atoi(optarg);
1096 conf->bindings_file = optarg;
1102 conf->remove = FLUSH_ONE;
1105 conf->remove = FLUSH_ALL;
1111 if (optarg && !strncmp(optarg, "l", 1))
1117 debug = atoi(optarg);
1121 conf->pgpolicy_flag = get_pgpolicy_id(optarg);
1122 if (conf->pgpolicy_flag == -1) {
1123 printf("'%s' is not a valid policy\n", optarg);
1128 fprintf(stderr, "Missing option arguement\n");
1131 fprintf(stderr, "Unknown switch: %s\n", optarg);
1137 if (optind < argc) {
1138 conf->dev = MALLOC(FILE_NAME_SIZE);
1143 strncpy(conf->dev, argv[optind], FILE_NAME_SIZE);
1145 if (filepresent(conf->dev))
1146 conf->dev_type = DEV_DEVNODE;
1147 else if (sscanf(conf->dev, "%d:%d", &i, &i) == 2)
1148 conf->dev_type = DEV_DEVT;
1150 conf->dev_type = DEV_DEVMAP;
1154 if (conf->remove == FLUSH_ONE) {
1155 if (conf->dev_type == DEV_DEVMAP)
1156 dm_flush_map(conf->dev, DEFAULT_TARGET);
1158 condlog(0, "must provide a map name to remove");
1162 else if (conf->remove == FLUSH_ALL) {
1163 dm_flush_maps(DEFAULT_TARGET);
1166 while ((r = configure()) < 0)
1167 condlog(3, "restart multipath configuration process");
1174 dbg_free_final(NULL);