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);
104 devt2devname(conf->dev, buff);
106 if(safe_sprintf(pp->dev, "%s", buff)) {
107 fprintf(stderr, "pp->dev too small\n");
110 if (pathinfo(pp, conf->hwtable, DI_SYSFS | DI_WWID))
113 if (store_path(pathvec, pp)) {
119 refwwid = MALLOC(WWID_SIZE);
124 memcpy(refwwid, pp->wwid, WWID_SIZE);
127 if (conf->dev_type == DEV_DEVMAP) {
128 condlog(3, "limited scope = %s", conf->dev);
132 refwwid = get_mpe_wwid(conf->dev);
140 refwwid = MALLOC(WWID_SIZE);
145 strncpy(refwwid, conf->dev, WWID_SIZE);
152 print_path (struct path * pp, char * style)
154 char buff[MAX_LINE_LEN];
156 snprint_path(&buff[0], MAX_LINE_LEN, style, pp, &pl);
161 print_map (struct multipath * mpp)
163 if (mpp->size && mpp->params)
164 printf("0 %llu %s %s\n",
165 mpp->size, DEFAULT_TARGET, mpp->params);
170 print_all_paths (vector pathvec)
175 vector_foreach_slot (pathvec, pp, i)
176 print_path(pp, PRINT_PATH_LONG);
180 print_mp (struct multipath * mpp)
183 struct path * pp = NULL;
184 struct pathgroup * pgp = NULL;
186 if (mpp->action == ACT_NOTHING || conf->verbosity == 0)
189 if (conf->verbosity > 1) {
190 switch (mpp->action) {
192 printf("%s: ", ACT_RELOAD_STR);
196 printf("%s: ", ACT_CREATE_STR);
200 printf("%s: ", ACT_SWITCHPG_STR);
209 printf("%s", mpp->alias);
211 if (conf->verbosity == 1) {
215 if (strncmp(mpp->alias, mpp->wwid, WWID_SIZE))
216 printf(" (%s)", mpp->wwid);
220 if (mpp->size < (1 << 11))
221 printf("[size=%llu kB]", mpp->size >> 1);
222 else if (mpp->size < (1 << 21))
223 printf("[size=%llu MB]", mpp->size >> 11);
224 else if (mpp->size < (1 << 31))
225 printf("[size=%llu GB]", mpp->size >> 21);
227 printf("[size=%llu TB]", mpp->size >> 31);
230 printf("[features=\"%s\"]", mpp->features);
233 printf("[hwhandler=\"%s\"]", mpp->hwhandler);
235 fprintf(stdout, "\n");
240 vector_foreach_slot (mpp->pg, pgp, j) {
244 printf("%s ", mpp->selector);
246 /* align to path status info */
247 for (i = pl.hbtl_len + pl.dev_len + pl.dev_t_len + 4;
248 i > strlen(mpp->selector); i--)
253 printf("[prio=%i]", pgp->priority);
255 switch (pgp->status) {
256 case PGSTATE_ENABLED:
259 case PGSTATE_DISABLED:
260 printf("[disabled]");
270 vector_foreach_slot (pgp->paths, pp, i)
271 print_path(pp, PRINT_PATH_INDENT);
277 filter_pathvec (vector pathvec, char * refwwid)
282 if (!refwwid || !strlen(refwwid))
285 vector_foreach_slot (pathvec, pp, i) {
286 if (memcmp(pp->wwid, refwwid, WWID_SIZE) != 0) {
287 condlog(3, "skip path %s : out of scope", pp->dev);
289 vector_del_slot(pathvec, i);
297 * Transforms the path group vector into a proper device map string
300 assemble_map (struct multipath * mp)
305 struct pathgroup * pgp;
309 freechar = sizeof(mp->params);
311 shift = snprintf(p, freechar, "%s %s %i %i",
312 mp->features, mp->hwhandler,
313 VECTOR_SIZE(mp->pg), mp->nextpg);
315 if (shift >= freechar) {
316 fprintf(stderr, "mp->params too small\n");
322 vector_foreach_slot (mp->pg, pgp, i) {
323 pgp = VECTOR_SLOT(mp->pg, i);
324 shift = snprintf(p, freechar, " %s %i 1", mp->selector,
325 VECTOR_SIZE(pgp->paths));
326 if (shift >= freechar) {
327 fprintf(stderr, "mp->params too small\n");
333 vector_foreach_slot (pgp->paths, pp, j) {
334 shift = snprintf(p, freechar, " %s %d",
335 pp->dev_t, conf->minio);
336 if (shift >= freechar) {
337 fprintf(stderr, "mp->params too small\n");
345 fprintf(stderr, "mp->params too small\n");
348 snprintf(p, 1, "\n");
350 if (conf->verbosity > 2)
357 setup_map (struct multipath * mpp)
363 * don't bother if devmap size is unknown
365 if (mpp->size <= 0) {
366 condlog(3, "%s devmap size is unknown", mpp->alias);
371 * don't bother if a constituant path is claimed
372 * (not by the device mapper driver)
374 vector_foreach_slot (mpp->paths, pp, i) {
375 if (pp->claimed && pp->dmstate == PSTATE_UNDEF) {
376 condlog(3, "%s claimed", pp->dev);
382 * properties selectors
384 select_pgpolicy(mpp);
385 select_selector(mpp);
386 select_features(mpp);
387 select_hwhandler(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(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;
501 if (pathcount(mpp, PATH_UP) == 0) {
502 condlog(3, "no good path");
503 mpp->action = ACT_NOTHING;
506 if (cmpp->size != mpp->size) {
507 condlog(3, "size different than current");
508 mpp->action = ACT_RELOAD;
511 if (strncmp(cmpp->features, mpp->features,
512 strlen(mpp->features))) {
513 condlog(3, "features different than current");
514 mpp->action = ACT_RELOAD;
517 if (strncmp(cmpp->hwhandler, mpp->hwhandler,
518 strlen(mpp->hwhandler))) {
519 condlog(3, "hwhandler different than current");
520 mpp->action = ACT_RELOAD;
523 if (strncmp(cmpp->selector, mpp->selector,
524 strlen(mpp->selector))) {
525 condlog(3, "selector different than current");
526 mpp->action = ACT_RELOAD;
529 if (VECTOR_SIZE(cmpp->pg) != VECTOR_SIZE(mpp->pg)) {
530 condlog(3, "different number of PG");
531 mpp->action = ACT_RELOAD;
534 if (pgcmp(mpp, cmpp)) {
535 condlog(3, "different path group topology");
536 mpp->action = ACT_RELOAD;
539 if (cmpp->nextpg != mpp->nextpg) {
540 condlog(3, "nextpg different than current");
541 mpp->action = ACT_SWITCHPG;
544 mpp->action = ACT_NOTHING;
549 reinstate_paths (struct multipath * mpp)
552 struct pathgroup * pgp;
555 vector_foreach_slot (mpp->pg, pgp, i) {
556 vector_foreach_slot (pgp->paths, pp, j) {
557 if (pp->state != PATH_UP &&
558 (pgp->status == PGSTATE_DISABLED ||
559 pgp->status == PGSTATE_ACTIVE))
562 if (pp->dmstate == PSTATE_FAILED) {
563 if (dm_reinstate(mpp->alias, pp->dev_t))
564 condlog(0, "error reinstating %s",
573 domap (struct multipath * mpp)
575 int op = ACT_NOTHING;
581 * last chance to quit before touching the devmaps
586 switch (mpp->action) {
591 dm_switchgroup(mpp->alias, mpp->nextpg);
593 * we may have avoided reinstating paths because there where in
594 * active or disabled PG. Now that the topology has changed,
597 reinstate_paths(mpp);
601 op = DM_DEVICE_CREATE;
605 op = DM_DEVICE_RELOAD;
614 * device mapper creation or updating
615 * here we know we'll have garbage on stderr from
616 * libdevmapper. so shut it down temporarily.
618 dm_log_init_verbose(0);
620 r = dm_addmap(op, mpp->alias, DEFAULT_TARGET, mpp->params, mpp->size);
623 dm_simplecmd(DM_DEVICE_REMOVE, mpp->alias);
624 else if (op == DM_DEVICE_RELOAD)
625 dm_simplecmd(DM_DEVICE_RESUME, mpp->alias);
628 * PG order is random, so we need to set the primary one
629 * upon create or reload
631 dm_switchgroup(mpp->alias, mpp->nextpg);
633 dm_log_init_verbose(1);
639 coalesce_paths (vector curmp, vector pathvec)
642 char empty_buff[WWID_SIZE];
643 struct multipath * mpp;
647 memset(empty_buff, 0, WWID_SIZE);
649 vector_foreach_slot (pathvec, pp1, k) {
650 /* skip this path for some reason */
652 /* 1. if path has no unique id or wwid blacklisted */
653 if (memcmp(empty_buff, pp1->wwid, WWID_SIZE) == 0 ||
654 blacklist(conf->blist, pp1->wwid))
657 /* 2. if path already coalesced */
662 * at this point, we know we really got a new mp
664 mpp = alloc_multipath();
669 mpp->mpe = find_mpe(pp1->wwid);
674 strcpy(mpp->wwid, pp1->wwid);
675 mpp->size = pp1->size;
676 mpp->paths = vector_alloc();
678 if (pp1->priority < 0)
679 mpp->action = ACT_NOTHING;
684 if (store_path(mpp->paths, pp1))
687 for (i = k + 1; i < VECTOR_SIZE(pathvec); i++) {
688 pp2 = VECTOR_SLOT(pathvec, i);
690 if (strcmp(pp1->wwid, pp2->wwid))
695 if (pp2->size != mpp->size) {
697 * ouch, avoid feeding that to the DM
699 condlog(3, "path size mismatch : discard %s",
701 mpp->action = ACT_NOTHING;
703 if (pp2->priority < 0)
704 mpp->action = ACT_NOTHING;
706 if (store_path(mpp->paths, pp2))
709 if (setup_map(mpp)) {
710 free_multipath(mpp, KEEP_PATHS);
713 condlog(3, "action preset to %i", mpp->action);
715 if (mpp->action == ACT_UNDEF)
716 select_action(mpp, curmp);
718 condlog(3, "action set to %i", mpp->action);
721 free_multipath(mpp, KEEP_PATHS);
727 usage (char * progname)
729 fprintf (stderr, VERSION_STRING);
730 fprintf (stderr, "Usage: %s\t[-v level] [-d] [-l|-ll|-f|-F]\n",
733 "\t\t\t[-p failover|multibus|group_by_serial|group_by_prio]\n" \
736 "\t-v level\tverbosty level\n" \
737 "\t 0\t\t\tno output\n" \
738 "\t 1\t\t\tprint created devmap names only\n" \
739 "\t 2\t\t\tdefault verbosity\n" \
740 "\t 3\t\t\tprint debug information\n" \
741 "\t-d\t\tdry run, do not create or update devmaps\n" \
742 "\t-l\t\tshow multipath topology (sysfs and DM info)\n" \
743 "\t-ll\t\tshow multipath topology (maximum info)\n" \
744 "\t-F\t\tflush a multipath device map\n" \
745 "\t-F\t\tflush all multipath device maps\n" \
746 "\t-p policy\tforce all maps to specified policy :\n" \
747 "\t failover\t\t1 path per priority group\n" \
748 "\t multibus\t\tall paths in 1 priority group\n" \
749 "\t group_by_serial\t1 priority group per serial\n" \
750 "\t group_by_prio\t1 priority group per priority lvl\n" \
751 "\t group_by_node_name\t1 priority group per target node\n" \
753 "\tdevice\t\tlimit scope to the device's multipath\n" \
754 "\t\t\t(udev-style $DEVNAME reference, eg /dev/sdb\n" \
755 "\t\t\tor major:minor or a device map name)\n" \
762 update_pathvec (vector pathvec)
767 vector_foreach_slot (pathvec, pp, i) {
768 if (pp->dev && pp->dev_t && strlen(pp->dev) == 0) {
769 devt2devname(pp->dev, pp->dev_t);
770 pathinfo(pp, conf->hwtable,
771 DI_SYSFS | DI_CHECKER | DI_SERIAL | DI_PRIO);
773 if (pp->checkfn && pp->state == PATH_UNCHECKED)
774 pp->state = pp->checkfn(pp->fd, NULL, NULL);
780 get_dm_mpvec (vector curmp, vector pathvec, char * refwwid)
783 struct multipath * mpp;
786 if (dm_get_maps(curmp, DEFAULT_TARGET))
789 vector_foreach_slot (curmp, mpp, i) {
790 wwid = get_mpe_wwid(mpp->alias);
793 /* out of specified scope */
794 if (refwwid && strncmp(wwid, refwwid, WWID_SIZE))
799 condlog(3, "params = %s", mpp->params);
800 condlog(3, "status = %s", mpp->status);
802 /* will set mpp->wwid */
803 disassemble_map(pathvec, mpp->params, mpp);
806 * disassemble_map may have added new paths to pathvec.
807 * If not in "fast list mode", we need to fetch information
810 if (conf->list != 1) {
811 update_pathvec(pathvec);
812 select_path_group(mpp);
814 disassemble_status(mpp->status, mpp);
820 reinstate_paths(mpp);
826 main (int argc, char *argv[])
829 vector pathvec = NULL;
835 char * refwwid = NULL;
838 fprintf(stderr, "need to be root\n");
842 if (dm_prereq(DEFAULT_TARGET, 1, 0, 3))
845 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
846 condlog(0, "multipath tools need sysfs mounted");
849 if (load_config(DEFAULT_CONFIGFILE))
852 while ((arg = getopt(argc, argv, ":qdl::Ffi:M:v:p:")) != EOF ) {
854 case 1: printf("optarg : %s\n",optarg);
857 if (sizeof(optarg) > sizeof(char *) ||
861 conf->verbosity = atoi(optarg);
876 if (optarg && !strncmp(optarg, "l", 1))
882 debug = atoi(optarg);
886 conf->pgpolicy_flag = get_pgpolicy_id(optarg);
887 if (conf->pgpolicy_flag == -1) {
888 printf("'%s' is not a valid policy\n", optarg);
893 fprintf(stderr, "Missing option arguement\n");
896 fprintf(stderr, "Unknown switch: %s\n", optarg);
903 conf->dev = MALLOC(FILE_NAME_SIZE);
908 strncpy(conf->dev, argv[optind], FILE_NAME_SIZE);
910 if (filepresent(conf->dev))
911 conf->dev_type = DEV_DEVNODE;
912 else if (sscanf(conf->dev, "%d:%d", &i, &i) == 2)
913 conf->dev_type = DEV_DEVT;
915 conf->dev_type = DEV_DEVMAP;
919 if (conf->remove == FLUSH_ONE) {
920 if (conf->dev_type == DEV_DEVMAP)
921 dm_flush_map(conf->dev, DEFAULT_TARGET);
923 condlog(0, "must provide a map name to remove");
927 else if (conf->remove == FLUSH_ALL) {
928 dm_flush_maps(DEFAULT_TARGET);
933 * allocate core vectors to store paths and multipaths
935 curmp = vector_alloc();
936 pathvec = vector_alloc();
938 if (!curmp || !pathvec) {
939 condlog(0, "can not allocate memory");
944 * if we have a blacklisted device parameter, exit early
946 if (conf->dev && blacklist(conf->blist, conf->dev))
949 condlog(3, "load path identifiers cache");
952 if (conf->verbosity > 2) {
953 fprintf(stdout, "#\n# all paths in cache :\n#\n");
954 print_all_paths(pathvec);
964 /* extended path info '-ll' */
965 di_flag |= DI_SYSFS | DI_CHECKER;
967 /* minimum path info '-l' */
973 if (path_discovery(pathvec, conf, di_flag) || VECTOR_SIZE(pathvec) == 0)
976 if (conf->verbosity > 2) {
977 fprintf(stdout, "#\n# all paths :\n#\n");
978 print_all_paths(pathvec);
981 refwwid = get_refwwid(pathvec);
982 get_path_layout(&pl, pathvec);
984 if (get_dm_mpvec(curmp, pathvec, refwwid))
987 filter_pathvec(pathvec, refwwid);
993 * core logic entry point
995 coalesce_paths(curmp, pathvec);
1001 free_multipathvec(curmp, KEEP_PATHS);
1002 free_pathvec(pathvec, FREE_PATHS);
1007 dbg_free_final(NULL);