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)
583 int op = ACT_NOTHING;
589 * last chance to quit before touching the devmaps
594 switch (mpp->action) {
599 dm_switchgroup(mpp->alias, mpp->nextpg);
601 * we may have avoided reinstating paths because there where in
602 * active or disabled PG. Now that the topology has changed,
605 reinstate_paths(mpp);
609 op = DM_DEVICE_CREATE;
613 op = DM_DEVICE_RELOAD;
622 * device mapper creation or updating
623 * here we know we'll have garbage on stderr from
624 * libdevmapper. so shut it down temporarily.
626 dm_log_init_verbose(0);
628 r = dm_addmap(op, mpp->alias, DEFAULT_TARGET, mpp->params, mpp->size, mpp->wwid);
631 dm_simplecmd(DM_DEVICE_REMOVE, mpp->alias);
632 else if (op == DM_DEVICE_RELOAD)
633 dm_simplecmd(DM_DEVICE_RESUME, mpp->alias);
636 * PG order is random, so we need to set the primary one
637 * upon create or reload
639 dm_switchgroup(mpp->alias, mpp->nextpg);
641 dm_log_init_verbose(1);
647 coalesce_paths (vector curmp, vector pathvec)
650 char empty_buff[WWID_SIZE];
651 struct multipath * mpp;
655 memset(empty_buff, 0, WWID_SIZE);
657 vector_foreach_slot (pathvec, pp1, k) {
658 /* skip this path for some reason */
660 /* 1. if path has no unique id or wwid blacklisted */
661 if (memcmp(empty_buff, pp1->wwid, WWID_SIZE) == 0 ||
662 blacklist(conf->blist, pp1->wwid))
665 /* 2. if path already coalesced */
670 * at this point, we know we really got a new mp
672 mpp = alloc_multipath();
677 mpp->mpe = find_mpe(pp1->wwid);
682 strcpy(mpp->wwid, pp1->wwid);
683 mpp->size = pp1->size;
684 mpp->paths = vector_alloc();
686 if (pp1->priority < 0)
687 mpp->action = ACT_NOTHING;
692 if (store_path(mpp->paths, pp1))
695 for (i = k + 1; i < VECTOR_SIZE(pathvec); i++) {
696 pp2 = VECTOR_SLOT(pathvec, i);
698 if (strcmp(pp1->wwid, pp2->wwid))
703 if (pp2->size != mpp->size) {
705 * ouch, avoid feeding that to the DM
707 condlog(3, "path size mismatch : discard %s",
709 mpp->action = ACT_NOTHING;
711 if (pp2->priority < 0)
712 mpp->action = ACT_NOTHING;
714 if (store_path(mpp->paths, pp2))
717 if (setup_map(mpp)) {
718 free_multipath(mpp, KEEP_PATHS);
721 condlog(3, "action preset to %i", mpp->action);
723 if (mpp->action == ACT_UNDEF)
724 select_action(mpp, curmp);
726 condlog(3, "action set to %i", mpp->action);
729 free_multipath(mpp, KEEP_PATHS);
735 usage (char * progname)
737 fprintf (stderr, VERSION_STRING);
738 fprintf (stderr, "Usage: %s\t[-v level] [-d] [-l|-ll|-f|-F]\n",
741 "\t\t\t[-p failover|multibus|group_by_serial|group_by_prio]\n" \
744 "\t-v level\tverbosty level\n" \
745 "\t 0\t\t\tno output\n" \
746 "\t 1\t\t\tprint created devmap names only\n" \
747 "\t 2\t\t\tdefault verbosity\n" \
748 "\t 3\t\t\tprint debug information\n" \
749 "\t-d\t\tdry run, do not create or update devmaps\n" \
750 "\t-l\t\tshow multipath topology (sysfs and DM info)\n" \
751 "\t-ll\t\tshow multipath topology (maximum info)\n" \
752 "\t-F\t\tflush a multipath device map\n" \
753 "\t-F\t\tflush all multipath device maps\n" \
754 "\t-p policy\tforce all maps to specified policy :\n" \
755 "\t failover\t\t1 path per priority group\n" \
756 "\t multibus\t\tall paths in 1 priority group\n" \
757 "\t group_by_serial\t1 priority group per serial\n" \
758 "\t group_by_prio\t1 priority group per priority lvl\n" \
759 "\t group_by_node_name\t1 priority group per target node\n" \
761 "\tdevice\t\tlimit scope to the device's multipath\n" \
762 "\t\t\t(udev-style $DEVNAME reference, eg /dev/sdb\n" \
763 "\t\t\tor major:minor or a device map name)\n" \
770 update_paths (struct multipath * mpp)
773 struct pathgroup * pgp;
776 vector_foreach_slot (mpp->pg, pgp, i) {
777 vector_foreach_slot (pgp->paths, pp, j) {
778 if (!strlen(pp->dev)) {
779 if (devt2devname(pp->dev_t, pp->dev)) {
781 * path is not in sysfs anymore
783 pp->state = PATH_DOWN;
786 pathinfo(pp, conf->hwtable,
787 DI_SYSFS | DI_CHECKER | \
788 DI_SERIAL | DI_PRIO);
791 if (pp->state == PATH_UNCHECKED)
792 pathinfo(pp, conf->hwtable, DI_CHECKER);
795 pathinfo(pp, conf->hwtable, DI_PRIO);
802 get_dm_mpvec (vector curmp, vector pathvec, char * refwwid)
805 struct multipath * mpp;
808 if (dm_get_maps(curmp, DEFAULT_TARGET))
811 vector_foreach_slot (curmp, mpp, i) {
812 wwid = get_mpe_wwid(mpp->alias);
815 /* out of specified scope */
816 if (refwwid && strncmp(wwid, refwwid, WWID_SIZE))
821 condlog(3, "params = %s", mpp->params);
822 condlog(3, "status = %s", mpp->status);
824 /* will set mpp->wwid */
825 disassemble_map(pathvec, mpp->params, mpp);
828 * disassemble_map may have added new paths to pathvec.
829 * If not in "fast list mode", we need to fetch information
836 select_path_group(mpp);
838 disassemble_status(mpp->status, mpp);
844 reinstate_paths(mpp);
850 main (int argc, char *argv[])
853 vector pathvec = NULL;
859 char * refwwid = NULL;
862 fprintf(stderr, "need to be root\n");
866 if (dm_prereq(DEFAULT_TARGET, 1, 0, 3))
869 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
870 condlog(0, "multipath tools need sysfs mounted");
873 if (load_config(DEFAULT_CONFIGFILE))
876 while ((arg = getopt(argc, argv, ":qdl::Ffi:M:v:p:")) != EOF ) {
878 case 1: printf("optarg : %s\n",optarg);
881 if (sizeof(optarg) > sizeof(char *) ||
885 conf->verbosity = atoi(optarg);
900 if (optarg && !strncmp(optarg, "l", 1))
906 debug = atoi(optarg);
910 conf->pgpolicy_flag = get_pgpolicy_id(optarg);
911 if (conf->pgpolicy_flag == -1) {
912 printf("'%s' is not a valid policy\n", optarg);
917 fprintf(stderr, "Missing option arguement\n");
920 fprintf(stderr, "Unknown switch: %s\n", optarg);
927 conf->dev = MALLOC(FILE_NAME_SIZE);
932 strncpy(conf->dev, argv[optind], FILE_NAME_SIZE);
934 if (filepresent(conf->dev))
935 conf->dev_type = DEV_DEVNODE;
936 else if (sscanf(conf->dev, "%d:%d", &i, &i) == 2)
937 conf->dev_type = DEV_DEVT;
939 conf->dev_type = DEV_DEVMAP;
943 if (conf->remove == FLUSH_ONE) {
944 if (conf->dev_type == DEV_DEVMAP)
945 dm_flush_map(conf->dev, DEFAULT_TARGET);
947 condlog(0, "must provide a map name to remove");
951 else if (conf->remove == FLUSH_ALL) {
952 dm_flush_maps(DEFAULT_TARGET);
957 * allocate core vectors to store paths and multipaths
959 curmp = vector_alloc();
960 pathvec = vector_alloc();
962 if (!curmp || !pathvec) {
963 condlog(0, "can not allocate memory");
968 * if we have a blacklisted device parameter, exit early
970 if (conf->dev && blacklist(conf->blist, conf->dev))
973 condlog(3, "load path identifiers cache");
976 if (conf->verbosity > 2) {
977 fprintf(stdout, "#\n# all paths in cache :\n#\n");
978 print_all_paths(pathvec);
988 /* extended path info '-ll' */
989 di_flag |= DI_SYSFS | DI_CHECKER;
991 /* minimum path info '-l' */
997 if (path_discovery(pathvec, conf, di_flag) || VECTOR_SIZE(pathvec) == 0)
1000 if (conf->verbosity > 2) {
1001 fprintf(stdout, "#\n# all paths :\n#\n");
1002 print_all_paths(pathvec);
1006 * scope limiting must be translated into a wwid
1007 * failing the translation is fatal (by policy)
1010 refwwid = get_refwwid(pathvec);
1013 condlog(3, "scope is nul");
1018 get_path_layout(&pl, pathvec);
1020 if (get_dm_mpvec(curmp, pathvec, refwwid))
1023 filter_pathvec(pathvec, refwwid);
1029 * core logic entry point
1031 coalesce_paths(curmp, pathvec);
1037 free_multipathvec(curmp, KEEP_PATHS);
1038 free_pathvec(pathvec, FREE_PATHS);
1043 dbg_free_final(NULL);