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.
27 #include <libdevmapper.h>
28 #include <devmapper.h>
30 #include <path_state.h>
31 #include <blacklist.h>
40 #include <discovery.h>
42 #include <switchgroup.h>
43 #include <sysfs/libsysfs.h>
47 #include "pgpolicies.h"
50 /* for column aligned output */
51 struct path_layout pl;
54 get_refwwid (vector pathvec)
57 char buff[FILE_NAME_SIZE];
60 if (conf->dev_type == DEV_NONE)
63 if (conf->dev_type == DEV_DEVNODE) {
64 condlog(3, "limited scope = %s", conf->dev);
65 basename(conf->dev, buff);
66 pp = find_path_by_dev(pathvec, buff);
74 strncpy(pp->dev, buff, FILE_NAME_SIZE);
76 if (pathinfo(pp, conf->hwtable, DI_SYSFS | DI_WWID))
79 if (store_path(pathvec, pp)) {
85 refwwid = MALLOC(WWID_SIZE);
90 memcpy(refwwid, pp->wwid, WWID_SIZE);
94 if (conf->dev_type == DEV_DEVT) {
95 condlog(3, "limited scope = %s", conf->dev);
96 pp = find_path_by_devt(pathvec, conf->dev);
99 if (devt2devname(conf->dev, buff))
107 if(safe_sprintf(pp->dev, "%s", buff)) {
108 fprintf(stderr, "pp->dev too small\n");
111 if (pathinfo(pp, conf->hwtable, DI_SYSFS | DI_WWID))
114 if (store_path(pathvec, pp)) {
120 refwwid = MALLOC(WWID_SIZE);
125 memcpy(refwwid, pp->wwid, WWID_SIZE);
128 if (conf->dev_type == DEV_DEVMAP) {
129 condlog(3, "limited scope = %s", conf->dev);
133 refwwid = get_mpe_wwid(conf->dev);
141 refwwid = MALLOC(WWID_SIZE);
146 strncpy(refwwid, conf->dev, WWID_SIZE);
153 print_path (struct path * pp, char * style)
155 char buff[MAX_LINE_LEN];
157 snprint_path(&buff[0], MAX_LINE_LEN, style, pp, &pl);
162 print_map (struct multipath * mpp)
164 if (mpp->size && mpp->params)
165 printf("0 %llu %s %s\n",
166 mpp->size, DEFAULT_TARGET, mpp->params);
171 print_all_paths (vector pathvec)
176 vector_foreach_slot (pathvec, pp, i)
177 print_path(pp, PRINT_PATH_LONG);
181 print_mp (struct multipath * mpp)
184 struct path * pp = NULL;
185 struct pathgroup * pgp = NULL;
187 if (mpp->action == ACT_NOTHING || conf->verbosity == 0)
190 if (conf->verbosity > 1) {
191 switch (mpp->action) {
193 printf("%s: ", ACT_RELOAD_STR);
197 printf("%s: ", ACT_CREATE_STR);
201 printf("%s: ", ACT_SWITCHPG_STR);
210 printf("%s", mpp->alias);
212 if (conf->verbosity == 1) {
216 if (strncmp(mpp->alias, mpp->wwid, WWID_SIZE))
217 printf(" (%s)", mpp->wwid);
221 if (mpp->size < (1 << 11))
222 printf("[size=%llu kB]", mpp->size >> 1);
223 else if (mpp->size < (1 << 21))
224 printf("[size=%llu MB]", mpp->size >> 11);
225 else if (mpp->size < (1 << 31))
226 printf("[size=%llu GB]", mpp->size >> 21);
228 printf("[size=%llu TB]", mpp->size >> 31);
231 printf("[features=\"%s\"]", mpp->features);
234 printf("[hwhandler=\"%s\"]", mpp->hwhandler);
236 fprintf(stdout, "\n");
241 vector_foreach_slot (mpp->pg, pgp, j) {
245 printf("%s ", mpp->selector);
247 /* align to path status info */
248 for (i = pl.hbtl_len + pl.dev_len + pl.dev_t_len + 4;
249 i > strlen(mpp->selector); i--)
254 printf("[prio=%i]", pgp->priority);
256 switch (pgp->status) {
257 case PGSTATE_ENABLED:
260 case PGSTATE_DISABLED:
261 printf("[disabled]");
271 vector_foreach_slot (pgp->paths, pp, i)
272 print_path(pp, PRINT_PATH_INDENT);
278 filter_pathvec (vector pathvec, char * refwwid)
283 if (!refwwid || !strlen(refwwid))
286 vector_foreach_slot (pathvec, pp, i) {
287 if (memcmp(pp->wwid, refwwid, WWID_SIZE) != 0) {
288 condlog(3, "skip path %s : out of scope", pp->dev);
290 vector_del_slot(pathvec, i);
298 * Transforms the path group vector into a proper device map string
301 assemble_map (struct multipath * mp)
307 struct pathgroup * pgp;
311 freechar = sizeof(mp->params);
313 shift = snprintf(p, freechar, "%s %s %i %i",
314 mp->features, mp->hwhandler,
315 VECTOR_SIZE(mp->pg), mp->nextpg);
317 if (shift >= freechar) {
318 fprintf(stderr, "mp->params too small\n");
324 vector_foreach_slot (mp->pg, pgp, i) {
325 pgp = VECTOR_SLOT(mp->pg, i);
326 shift = snprintf(p, freechar, " %s %i 1", mp->selector,
327 VECTOR_SIZE(pgp->paths));
328 if (shift >= freechar) {
329 fprintf(stderr, "mp->params too small\n");
335 vector_foreach_slot (pgp->paths, pp, j) {
338 if (mp->rr_weight == RR_WEIGHT_PRIO && pp->priority)
339 minio *= pp->priority;
341 shift = snprintf(p, freechar, " %s %d",
343 if (shift >= freechar) {
344 fprintf(stderr, "mp->params too small\n");
352 fprintf(stderr, "mp->params too small\n");
355 snprintf(p, 1, "\n");
357 if (conf->verbosity > 2)
364 setup_map (struct multipath * mpp)
370 * don't bother if devmap size is unknown
372 if (mpp->size <= 0) {
373 condlog(3, "%s devmap size is unknown", mpp->alias);
378 * don't bother if a constituant path is claimed
379 * (not by the device mapper driver)
381 vector_foreach_slot (mpp->paths, pp, i) {
382 if (pp->claimed && pp->dmstate == PSTATE_UNDEF) {
383 condlog(3, "%s claimed", pp->dev);
389 * properties selectors
391 select_pgpolicy(mpp);
392 select_selector(mpp);
393 select_features(mpp);
394 select_hwhandler(mpp);
395 select_rr_weight(mpp);
398 * apply selected grouping policy to valid paths
400 switch (mpp->pgpolicy) {
405 one_path_per_group(mpp);
407 case GROUP_BY_SERIAL:
408 group_by_serial(mpp);
413 case GROUP_BY_NODE_NAME:
414 group_by_node_name(mpp);
420 if (mpp->pg == NULL) {
421 condlog(3, "pgpolicy failed to produce a pg vector");
426 * ponders each path group and determine highest prio pg
427 * to switch over (default to first)
429 select_path_group(mpp);
432 * transform the mp->pg vector of vectors of paths
433 * into a mp->params strings to feed the device-mapper
435 if (assemble_map(mpp)) {
436 condlog(3, "problem assembing map");
443 pathcount (struct multipath * mpp, int state)
445 struct pathgroup *pgp;
450 vector_foreach_slot (mpp->pg, pgp, i)
451 vector_foreach_slot (pgp->paths, pp, j)
452 if (pp->state == state)
458 compute_pgid(struct pathgroup * pgp)
463 vector_foreach_slot (pgp->paths, pp, i)
468 pgcmp (struct multipath * mpp, struct multipath * cmpp)
471 struct pathgroup * pgp;
472 struct pathgroup * cpgp;
475 vector_foreach_slot (mpp->pg, pgp, i) {
478 vector_foreach_slot (cmpp->pg, cpgp, j) {
479 if (pgp->id == cpgp->id) {
492 select_action (struct multipath * mpp, vector curmp)
494 struct multipath * cmpp;
496 cmpp = find_mp(curmp, mpp->alias);
499 cmpp = find_mp_by_wwid(curmp, mpp->wwid);
501 if (cmpp && !conf->dry_run) {
502 condlog(2, "remove: %s (dup of %s)",
503 cmpp->alias, mpp->alias);
504 dm_flush_map(cmpp->alias, DEFAULT_TARGET);
506 mpp->action = ACT_CREATE;
509 if (pathcount(mpp, PATH_UP) == 0) {
510 condlog(3, "no good path");
511 mpp->action = ACT_NOTHING;
514 if (cmpp->size != mpp->size) {
515 condlog(3, "size different than current");
516 mpp->action = ACT_RELOAD;
519 if (strncmp(cmpp->features, mpp->features,
520 strlen(mpp->features))) {
521 condlog(3, "features different than current");
522 mpp->action = ACT_RELOAD;
525 if (strncmp(cmpp->hwhandler, mpp->hwhandler,
526 strlen(mpp->hwhandler))) {
527 condlog(3, "hwhandler different than current");
528 mpp->action = ACT_RELOAD;
531 if (strncmp(cmpp->selector, mpp->selector,
532 strlen(mpp->selector))) {
533 condlog(3, "selector different than current");
534 mpp->action = ACT_RELOAD;
537 if (VECTOR_SIZE(cmpp->pg) != VECTOR_SIZE(mpp->pg)) {
538 condlog(3, "different number of PG");
539 mpp->action = ACT_RELOAD;
542 if (pgcmp(mpp, cmpp)) {
543 condlog(3, "different path group topology");
544 mpp->action = ACT_RELOAD;
547 if (cmpp->nextpg != mpp->nextpg) {
548 condlog(3, "nextpg different than current");
549 mpp->action = ACT_SWITCHPG;
552 mpp->action = ACT_NOTHING;
557 reinstate_paths (struct multipath * mpp)
560 struct pathgroup * pgp;
563 vector_foreach_slot (mpp->pg, pgp, i) {
564 vector_foreach_slot (pgp->paths, pp, j) {
565 if (pp->state != PATH_UP &&
566 (pgp->status == PGSTATE_DISABLED ||
567 pgp->status == PGSTATE_ACTIVE))
570 if (pp->dmstate == PSTATE_FAILED) {
571 if (dm_reinstate(mpp->alias, pp->dev_t))
572 condlog(0, "error reinstating %s",
581 domap (struct multipath * mpp)
588 * last chance to quit before touching the devmaps
593 switch (mpp->action) {
598 dm_switchgroup(mpp->alias, mpp->nextpg);
600 * we may have avoided reinstating paths because there where in
601 * active or disabled PG. Now that the topology has changed,
604 reinstate_paths(mpp);
608 r = dm_addmap(DM_DEVICE_CREATE, mpp->alias, DEFAULT_TARGET,
609 mpp->params, mpp->size, mpp->wwid);
613 r = dm_addmap(DM_DEVICE_RELOAD, mpp->alias, DEFAULT_TARGET,
614 mpp->params, mpp->size, NULL);
623 * DM_DEVICE_CREATE or DM_DEVICE_RELOAD succeeded
625 dm_simplecmd(DM_DEVICE_RESUME, mpp->alias);
626 dm_switchgroup(mpp->alias, mpp->nextpg);
633 coalesce_paths (vector curmp, vector pathvec)
636 char empty_buff[WWID_SIZE];
637 struct multipath * mpp;
641 memset(empty_buff, 0, WWID_SIZE);
643 vector_foreach_slot (pathvec, pp1, k) {
644 /* skip this path for some reason */
646 /* 1. if path has no unique id or wwid blacklisted */
647 if (memcmp(empty_buff, pp1->wwid, WWID_SIZE) == 0 ||
648 blacklist(conf->blist, pp1->wwid))
651 /* 2. if path already coalesced */
656 * at this point, we know we really got a new mp
658 mpp = alloc_multipath();
663 mpp->mpe = find_mpe(pp1->wwid);
668 strcpy(mpp->wwid, pp1->wwid);
669 mpp->size = pp1->size;
670 mpp->paths = vector_alloc();
672 if (pp1->priority < 0)
673 mpp->action = ACT_NOTHING;
678 if (store_path(mpp->paths, pp1))
681 for (i = k + 1; i < VECTOR_SIZE(pathvec); i++) {
682 pp2 = VECTOR_SLOT(pathvec, i);
684 if (strcmp(pp1->wwid, pp2->wwid))
689 if (pp2->size != mpp->size) {
691 * ouch, avoid feeding that to the DM
693 condlog(3, "path size mismatch : discard %s",
695 mpp->action = ACT_NOTHING;
697 if (pp2->priority < 0)
698 mpp->action = ACT_NOTHING;
700 if (store_path(mpp->paths, pp2))
703 if (setup_map(mpp)) {
704 free_multipath(mpp, KEEP_PATHS);
707 condlog(3, "action preset to %i", mpp->action);
709 if (mpp->action == ACT_UNDEF)
710 select_action(mpp, curmp);
712 condlog(3, "action set to %i", mpp->action);
715 free_multipath(mpp, KEEP_PATHS);
721 usage (char * progname)
723 fprintf (stderr, VERSION_STRING);
724 fprintf (stderr, "Usage: %s\t[-v level] [-d] [-l|-ll|-f|-F]\n",
727 "\t\t\t[-p failover|multibus|group_by_serial|group_by_prio]\n" \
730 "\t-v level\tverbosty level\n" \
731 "\t 0\t\t\tno output\n" \
732 "\t 1\t\t\tprint created devmap names only\n" \
733 "\t 2\t\t\tdefault verbosity\n" \
734 "\t 3\t\t\tprint debug information\n" \
735 "\t-d\t\tdry run, do not create or update devmaps\n" \
736 "\t-l\t\tshow multipath topology (sysfs and DM info)\n" \
737 "\t-ll\t\tshow multipath topology (maximum info)\n" \
738 "\t-F\t\tflush a multipath device map\n" \
739 "\t-F\t\tflush all multipath device maps\n" \
740 "\t-p policy\tforce all maps to specified policy :\n" \
741 "\t failover\t\t1 path per priority group\n" \
742 "\t multibus\t\tall paths in 1 priority group\n" \
743 "\t group_by_serial\t1 priority group per serial\n" \
744 "\t group_by_prio\t1 priority group per priority lvl\n" \
745 "\t group_by_node_name\t1 priority group per target node\n" \
747 "\tdevice\t\tlimit scope to the device's multipath\n" \
748 "\t\t\t(udev-style $DEVNAME reference, eg /dev/sdb\n" \
749 "\t\t\tor major:minor or a device map name)\n" \
756 update_paths (struct multipath * mpp)
759 struct pathgroup * pgp;
762 vector_foreach_slot (mpp->pg, pgp, i) {
763 vector_foreach_slot (pgp->paths, pp, j) {
764 if (!strlen(pp->dev)) {
765 if (devt2devname(pp->dev_t, pp->dev)) {
767 * path is not in sysfs anymore
769 pp->state = PATH_DOWN;
772 pathinfo(pp, conf->hwtable,
773 DI_SYSFS | DI_CHECKER | \
774 DI_SERIAL | DI_PRIO);
777 if (pp->state == PATH_UNCHECKED)
778 pathinfo(pp, conf->hwtable, DI_CHECKER);
781 pathinfo(pp, conf->hwtable, DI_PRIO);
788 get_dm_mpvec (vector curmp, vector pathvec, char * refwwid)
791 struct multipath * mpp;
794 if (dm_get_maps(curmp, DEFAULT_TARGET))
797 vector_foreach_slot (curmp, mpp, i) {
798 wwid = get_mpe_wwid(mpp->alias);
801 /* out of specified scope */
802 if (refwwid && strncmp(wwid, refwwid, WWID_SIZE))
807 condlog(3, "params = %s", mpp->params);
808 condlog(3, "status = %s", mpp->status);
810 /* will set mpp->wwid */
811 disassemble_map(pathvec, mpp->params, mpp);
814 * disassemble_map may have added new paths to pathvec.
815 * If not in "fast list mode", we need to fetch information
822 select_path_group(mpp);
824 disassemble_status(mpp->status, mpp);
830 reinstate_paths(mpp);
836 main (int argc, char *argv[])
839 vector pathvec = NULL;
845 char * refwwid = NULL;
848 fprintf(stderr, "need to be root\n");
852 if (dm_prereq(DEFAULT_TARGET, 1, 0, 3))
855 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
856 condlog(0, "multipath tools need sysfs mounted");
859 if (load_config(DEFAULT_CONFIGFILE))
862 while ((arg = getopt(argc, argv, ":qdl::Ffi:M:v:p:")) != EOF ) {
864 case 1: printf("optarg : %s\n",optarg);
867 if (sizeof(optarg) > sizeof(char *) ||
871 conf->verbosity = atoi(optarg);
886 if (optarg && !strncmp(optarg, "l", 1))
892 debug = atoi(optarg);
896 conf->pgpolicy_flag = get_pgpolicy_id(optarg);
897 if (conf->pgpolicy_flag == -1) {
898 printf("'%s' is not a valid policy\n", optarg);
903 fprintf(stderr, "Missing option arguement\n");
906 fprintf(stderr, "Unknown switch: %s\n", optarg);
913 conf->dev = MALLOC(FILE_NAME_SIZE);
918 strncpy(conf->dev, argv[optind], FILE_NAME_SIZE);
920 if (filepresent(conf->dev))
921 conf->dev_type = DEV_DEVNODE;
922 else if (sscanf(conf->dev, "%d:%d", &i, &i) == 2)
923 conf->dev_type = DEV_DEVT;
925 conf->dev_type = DEV_DEVMAP;
929 if (conf->remove == FLUSH_ONE) {
930 if (conf->dev_type == DEV_DEVMAP)
931 dm_flush_map(conf->dev, DEFAULT_TARGET);
933 condlog(0, "must provide a map name to remove");
937 else if (conf->remove == FLUSH_ALL) {
938 dm_flush_maps(DEFAULT_TARGET);
943 * allocate core vectors to store paths and multipaths
945 curmp = vector_alloc();
946 pathvec = vector_alloc();
948 if (!curmp || !pathvec) {
949 condlog(0, "can not allocate memory");
954 * if we have a blacklisted device parameter, exit early
956 if (conf->dev && blacklist(conf->blist, conf->dev))
959 condlog(3, "load path identifiers cache");
962 if (conf->verbosity > 2) {
963 fprintf(stdout, "#\n# all paths in cache :\n#\n");
964 print_all_paths(pathvec);
974 /* extended path info '-ll' */
975 di_flag |= DI_SYSFS | DI_CHECKER;
977 /* minimum path info '-l' */
983 if (path_discovery(pathvec, conf, di_flag))
986 if (conf->verbosity > 2) {
987 fprintf(stdout, "#\n# all paths :\n#\n");
988 print_all_paths(pathvec);
992 * scope limiting must be translated into a wwid
993 * failing the translation is fatal (by policy)
996 refwwid = get_refwwid(pathvec);
999 condlog(3, "scope is nul");
1004 get_path_layout(&pl, pathvec);
1006 if (get_dm_mpvec(curmp, pathvec, refwwid))
1009 filter_pathvec(pathvec, refwwid);
1015 * core logic entry point
1017 coalesce_paths(curmp, pathvec);
1023 free_multipathvec(curmp, KEEP_PATHS);
1024 free_pathvec(pathvec, FREE_PATHS);
1029 dbg_free_final(NULL);