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>
46 #include "pgpolicies.h"
50 get_refwwid (vector pathvec)
53 char buff[FILE_NAME_SIZE];
56 if (conf->dev_type == DEV_NONE)
59 if (conf->dev_type == DEV_DEVNODE) {
60 condlog(3, "limited scope = %s", conf->dev);
61 basename(conf->dev, buff);
62 pp = find_path_by_dev(pathvec, buff);
70 strncpy(pp->dev, buff, FILE_NAME_SIZE);
72 if (pathinfo(pp, conf->hwtable, DI_SYSFS | DI_WWID))
75 if (store_path(pathvec, pp)) {
81 refwwid = MALLOC(WWID_SIZE);
86 memcpy(refwwid, pp->wwid, WWID_SIZE);
90 if (conf->dev_type == DEV_DEVT) {
91 condlog(3, "limited scope = %s", conf->dev);
92 pp = find_path_by_devt(pathvec, conf->dev);
100 devt2devname(conf->dev, buff);
102 if(safe_sprintf(pp->dev, "%s", buff)) {
103 fprintf(stderr, "pp->dev too small\n");
106 if (pathinfo(pp, conf->hwtable, DI_SYSFS | DI_WWID))
109 if (store_path(pathvec, pp)) {
115 refwwid = MALLOC(WWID_SIZE);
120 memcpy(refwwid, pp->wwid, WWID_SIZE);
123 if (conf->dev_type == DEV_DEVMAP) {
124 condlog(3, "limited scope = %s", conf->dev);
128 refwwid = get_mpe_wwid(conf->dev);
136 refwwid = MALLOC(WWID_SIZE);
141 strncpy(refwwid, conf->dev, WWID_SIZE);
150 #define PRINT_PATH_ALL 0
151 #define PRINT_PATH_SHORT 1
154 print_path (struct path * pp, int style)
156 if (style != PRINT_PATH_SHORT && pp->wwid)
157 printf ("%s ", pp->wwid);
161 if (pp->sg_id.host_no < 0)
164 printf("%i:%i:%i:%i ",
171 printf("%-4s ", pp->dev);
174 printf("%-7s ", pp->dev_t);
176 if (conf->list > 1) {
195 switch (pp->dmstate) {
208 if (style != PRINT_PATH_SHORT && pp->product_id)
209 printf("[%.16s]", pp->product_id);
211 fprintf(stdout, "\n");
215 print_map (struct multipath * mpp)
217 if (mpp->size && mpp->params)
218 printf("0 %lu %s %s\n",
219 mpp->size, DEFAULT_TARGET, mpp->params);
224 print_all_paths (vector pathvec)
229 vector_foreach_slot (pathvec, pp, i)
230 print_path(pp, PRINT_PATH_ALL);
234 print_mp (struct multipath * mpp)
237 struct path * pp = NULL;
238 struct pathgroup * pgp = NULL;
240 if (mpp->action == ACT_NOTHING || conf->verbosity == 0)
243 if (conf->verbosity > 1) {
244 switch (mpp->action) {
246 printf("%s: ", ACT_RELOAD_STR);
250 printf("%s: ", ACT_CREATE_STR);
254 printf("%s: ", ACT_SWITCHPG_STR);
263 printf("%s", mpp->alias);
265 if (conf->verbosity == 1) {
269 if (strncmp(mpp->alias, mpp->wwid, WWID_SIZE))
270 printf(" (%s)", mpp->wwid);
274 if (mpp->size < 2000)
275 printf("[size=%lu kB]", mpp->size / 2);
276 else if (mpp->size < (2000 * 1024))
277 printf("[size=%lu MB]", mpp->size / 2 / 1024);
278 else if (mpp->size < (2000 * 1024 * 1024))
279 printf("[size=%lu GB]", mpp->size / 2 / 1024 / 1024);
281 printf("[size=%lu TB]", mpp->size / 2 / 1024 / 1024 / 1024);
284 printf("[features=\"%s\"]", mpp->features);
287 printf("[hwhandler=\"%s\"]", mpp->hwhandler);
289 fprintf(stdout, "\n");
294 vector_foreach_slot (mpp->pg, pgp, j) {
298 printf("%s ", mpp->selector);
300 switch (pgp->status) {
301 case PGSTATE_ENABLED:
304 case PGSTATE_DISABLED:
305 printf("[disabled]");
315 vector_foreach_slot (pgp->paths, pp, i)
316 print_path(pp, PRINT_PATH_SHORT);
322 filter_pathvec (vector pathvec, char * refwwid)
327 if (!refwwid || !strlen(refwwid))
330 vector_foreach_slot (pathvec, pp, i) {
331 if (memcmp(pp->wwid, refwwid, WWID_SIZE) != 0) {
332 condlog(3, "skip path %s : out of scope", pp->dev);
334 vector_del_slot(pathvec, i);
342 * Transforms the path group vector into a proper device map string
345 assemble_map (struct multipath * mp)
350 struct pathgroup * pgp;
354 freechar = sizeof(mp->params);
356 shift = snprintf(p, freechar, "%s %s %i %i",
357 mp->features, mp->hwhandler,
358 VECTOR_SIZE(mp->pg), mp->nextpg);
360 if (shift >= freechar) {
361 fprintf(stderr, "mp->params too small\n");
367 vector_foreach_slot (mp->pg, pgp, i) {
368 pgp = VECTOR_SLOT(mp->pg, i);
369 shift = snprintf(p, freechar, " %s %i 1", mp->selector,
370 VECTOR_SIZE(pgp->paths));
371 if (shift >= freechar) {
372 fprintf(stderr, "mp->params too small\n");
378 vector_foreach_slot (pgp->paths, pp, j) {
379 shift = snprintf(p, freechar, " %s %d",
380 pp->dev_t, conf->minio);
381 if (shift >= freechar) {
382 fprintf(stderr, "mp->params too small\n");
390 fprintf(stderr, "mp->params too small\n");
393 snprintf(p, 1, "\n");
395 if (conf->verbosity > 2)
402 setup_map (struct multipath * mpp)
408 * don't bother if devmap size is unknown
410 if (mpp->size <= 0) {
411 condlog(3, "%s devmap size is unknown", mpp->alias);
416 * don't bother if a constituant path is claimed
417 * FIXME : claimed detection broken, always unclaimed for now
419 vector_foreach_slot (mpp->paths, pp, i) {
421 condlog(3, "%s claimed", pp->dev);
427 * properties selectors
429 select_pgpolicy(mpp);
430 select_selector(mpp);
431 select_features(mpp);
432 select_hwhandler(mpp);
435 * apply selected grouping policy to valid paths
437 switch (mpp->pgpolicy) {
442 one_path_per_group(mpp);
444 case GROUP_BY_SERIAL:
445 group_by_serial(mpp);
450 case GROUP_BY_NODE_NAME:
451 group_by_node_name(mpp);
457 if (mpp->pg == NULL) {
458 condlog(3, "pgpolicy failed to produce a pg vector");
463 * ponders each path group and determine highest prio pg
464 * to switch over (default to first)
466 select_path_group(mpp);
469 * transform the mp->pg vector of vectors of paths
470 * into a mp->params strings to feed the device-mapper
472 if (assemble_map(mpp)) {
473 condlog(3, "problem assembing map");
480 pathcount (struct multipath * mpp, int state)
482 struct pathgroup *pgp;
487 vector_foreach_slot (mpp->pg, pgp, i)
488 vector_foreach_slot (pgp->paths, pp, j)
489 if (pp->state == state)
495 compute_pgid(struct pathgroup * pgp)
500 vector_foreach_slot (pgp->paths, pp, i)
505 pgcmp (struct multipath * mpp, struct multipath * cmpp)
508 struct pathgroup * pgp;
509 struct pathgroup * cpgp;
512 vector_foreach_slot (mpp->pg, pgp, i) {
515 vector_foreach_slot (cmpp->pg, cpgp, j) {
516 if (pgp->id == cpgp->id) {
529 select_action (struct multipath * mpp, vector curmp)
531 struct multipath * cmpp;
533 cmpp = find_mp(curmp, mpp->alias);
536 mpp->action = ACT_CREATE;
539 if (pathcount(mpp, PATH_UP) == 0) {
540 condlog(3, "no good path");
541 mpp->action = ACT_NOTHING;
544 if (cmpp->size != mpp->size) {
545 condlog(3, "size different than current");
546 mpp->action = ACT_RELOAD;
549 if (strncmp(cmpp->features, mpp->features,
550 strlen(mpp->features))) {
551 condlog(3, "features different than current");
552 mpp->action = ACT_RELOAD;
555 if (strncmp(cmpp->hwhandler, mpp->hwhandler,
556 strlen(mpp->hwhandler))) {
557 condlog(3, "hwhandler different than current");
558 mpp->action = ACT_RELOAD;
561 if (strncmp(cmpp->selector, mpp->selector,
562 strlen(mpp->selector))) {
563 condlog(3, "selector different than current");
564 mpp->action = ACT_RELOAD;
567 if (VECTOR_SIZE(cmpp->pg) != VECTOR_SIZE(mpp->pg)) {
568 condlog(3, "different number of PG");
569 mpp->action = ACT_RELOAD;
572 if (pgcmp(mpp, cmpp)) {
573 condlog(3, "different path group topology");
574 mpp->action = ACT_RELOAD;
577 if (cmpp->nextpg != mpp->nextpg) {
578 condlog(3, "nextpg different than current");
579 mpp->action = ACT_SWITCHPG;
582 mpp->action = ACT_NOTHING;
587 reinstate_paths (struct multipath * mpp)
590 struct pathgroup * pgp;
593 vector_foreach_slot (mpp->pg, pgp, i) {
594 vector_foreach_slot (pgp->paths, pp, j) {
595 if (pp->state != PATH_UP &&
596 (pgp->status == PGSTATE_DISABLED ||
597 pgp->status == PGSTATE_ACTIVE))
600 if (pp->dmstate == PSTATE_FAILED) {
601 if (dm_reinstate(mpp->alias, pp->dev_t))
602 condlog(0, "error reinstating %s",
611 domap (struct multipath * mpp)
613 int op = ACT_NOTHING;
619 * last chance to quit before touching the devmaps
621 if (conf->dry_run || mpp->action == ACT_NOTHING)
624 if (mpp->action == ACT_SWITCHPG) {
625 dm_switchgroup(mpp->alias, mpp->nextpg);
627 * we may have avoided reinstating paths because there where in
628 * active or disabled PG. Now that the topology has changed,
631 reinstate_paths(mpp);
634 if (mpp->action == ACT_CREATE)
635 op = DM_DEVICE_CREATE;
637 if (mpp->action == ACT_RELOAD)
638 op = DM_DEVICE_RELOAD;
642 * device mapper creation or updating
643 * here we know we'll have garbage on stderr from
644 * libdevmapper. so shut it down temporarily.
646 dm_log_init_verbose(0);
648 r = dm_addmap(op, mpp->alias, DEFAULT_TARGET, mpp->params, mpp->size);
651 dm_simplecmd(DM_DEVICE_REMOVE, mpp->alias);
652 else if (op == DM_DEVICE_RELOAD)
653 dm_simplecmd(DM_DEVICE_RESUME, mpp->alias);
656 * PG order is random, so we need to set the primary one
657 * upon create or reload
659 dm_switchgroup(mpp->alias, mpp->nextpg);
661 dm_log_init_verbose(1);
667 coalesce_paths (vector curmp, vector pathvec)
670 char empty_buff[WWID_SIZE];
671 struct multipath * mpp;
675 memset(empty_buff, 0, WWID_SIZE);
677 vector_foreach_slot (pathvec, pp1, k) {
678 /* skip this path for some reason */
680 /* 1. if path has no unique id or wwid blacklisted */
681 if (memcmp(empty_buff, pp1->wwid, WWID_SIZE) == 0 ||
682 blacklist(conf->blist, pp1->wwid))
685 /* 2. if path already coalesced */
690 * at this point, we know we really got a new mp
692 mpp = alloc_multipath();
697 mpp->mpe = find_mpe(pp1->wwid);
702 strcpy(mpp->wwid, pp1->wwid);
703 mpp->size = pp1->size;
704 mpp->paths = vector_alloc();
706 if (pp1->priority < 0)
707 mpp->action = ACT_NOTHING;
712 if (store_path(mpp->paths, pp1))
715 for (i = k + 1; i < VECTOR_SIZE(pathvec); i++) {
716 pp2 = VECTOR_SLOT(pathvec, i);
718 if (strcmp(pp1->wwid, pp2->wwid))
723 if (pp2->size != mpp->size) {
725 * ouch, avoid feeding that to the DM
727 condlog(3, "path size mismatch : discard %s",
729 mpp->action = ACT_NOTHING;
731 if (pp2->priority < 0)
732 mpp->action = ACT_NOTHING;
734 if (store_path(mpp->paths, pp2))
737 if (setup_map(mpp)) {
738 free_multipath(mpp, KEEP_PATHS);
741 condlog(3, "action preset to %i", mpp->action);
743 if (mpp->action == ACT_UNDEF)
744 select_action(mpp, curmp);
746 condlog(3, "action set to %i", mpp->action);
749 free_multipath(mpp, KEEP_PATHS);
755 usage (char * progname)
757 fprintf (stderr, VERSION_STRING);
758 fprintf (stderr, "Usage: %s\t[-v level] [-d] [-l|-ll|-f|-F]\n",
761 "\t\t\t[-p failover|multibus|group_by_serial|group_by_prio]\n" \
764 "\t-v level\tverbosty level\n" \
765 "\t 0\t\t\tno output\n" \
766 "\t 1\t\t\tprint created devmap names only\n" \
767 "\t 2\t\t\tdefault verbosity\n" \
768 "\t 3\t\t\tprint debug information\n" \
769 "\t-d\t\tdry run, do not create or update devmaps\n" \
770 "\t-l\t\tshow multipath topology (sysfs and DM info)\n" \
771 "\t-ll\t\tshow multipath topology (maximum info)\n" \
772 "\t-F\t\tflush a multipath device map\n" \
773 "\t-F\t\tflush all multipath device maps\n" \
774 "\t-p policy\tforce all maps to specified policy :\n" \
775 "\t failover\t\t1 path per priority group\n" \
776 "\t multibus\t\tall paths in 1 priority group\n" \
777 "\t group_by_serial\t1 priority group per serial\n" \
778 "\t group_by_prio\t1 priority group per priority lvl\n" \
779 "\t group_by_node_name\t1 priority group per target node\n" \
781 "\tdevice\t\tlimit scope to the device's multipath\n" \
782 "\t\t\t(udev-style $DEVNAME reference, eg /dev/sdb\n" \
783 "\t\t\tor major:minor or a device map name)\n" \
790 update_pathvec (vector pathvec)
795 vector_foreach_slot (pathvec, pp, i) {
796 if (pp->dev && pp->dev_t && strlen(pp->dev) == 0) {
797 devt2devname(pp->dev, pp->dev_t);
798 pathinfo(pp, conf->hwtable,
799 DI_SYSFS | DI_CHECKER | DI_SERIAL | DI_PRIO);
801 if (pp->checkfn && pp->state == PATH_UNCHECKED)
802 pp->state = pp->checkfn(pp->fd, NULL, NULL);
808 get_dm_mpvec (vector curmp, vector pathvec, char * refwwid)
811 struct multipath * mpp;
814 if (dm_get_maps(curmp, DEFAULT_TARGET))
817 vector_foreach_slot (curmp, mpp, i) {
818 wwid = get_mpe_wwid(mpp->alias);
821 strncpy(mpp->wwid, wwid, WWID_SIZE);
824 strncpy(mpp->wwid, mpp->alias, WWID_SIZE);
826 if (refwwid && strncmp(mpp->wwid, refwwid, WWID_SIZE))
829 condlog(3, "params = %s", mpp->params);
830 condlog(3, "status = %s", mpp->status);
831 disassemble_map(pathvec, mpp->params, mpp);
834 * disassemble_map may have added new paths to pathvec.
835 * If not in "fast list mode", we need to fetch information
839 update_pathvec(pathvec);
841 disassemble_status(mpp->status, mpp);
847 reinstate_paths(mpp);
853 main (int argc, char *argv[])
856 vector pathvec = NULL;
862 char * refwwid = NULL;
865 fprintf(stderr, "need to be root\n");
869 if (dm_prereq(DEFAULT_TARGET, 1, 0, 3)) {
870 condlog(0, "device mapper prerequisites not met");
873 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
874 condlog(0, "multipath tools need sysfs mounted");
877 if (load_config(DEFAULT_CONFIGFILE))
880 while ((arg = getopt(argc, argv, ":qdl::Ffi:M:v:p:")) != EOF ) {
882 case 1: printf("optarg : %s\n",optarg);
885 if (sizeof(optarg) > sizeof(char *) ||
889 conf->verbosity = atoi(optarg);
904 if (optarg && !strncmp(optarg, "l", 1))
910 debug = atoi(optarg);
914 conf->pgpolicy_flag = get_pgpolicy_id(optarg);
915 if (conf->pgpolicy_flag == -1) {
916 printf("'%s' is not a valid policy\n", optarg);
921 fprintf(stderr, "Missing option arguement\n");
924 fprintf(stderr, "Unknown switch: %s\n", optarg);
931 conf->dev = MALLOC(FILE_NAME_SIZE);
936 strncpy(conf->dev, argv[optind], FILE_NAME_SIZE);
938 if (filepresent(conf->dev))
939 conf->dev_type = DEV_DEVNODE;
940 else if (sscanf(conf->dev, "%d:%d", &i, &i) == 2)
941 conf->dev_type = DEV_DEVT;
943 conf->dev_type = DEV_DEVMAP;
947 if (conf->remove == FLUSH_ONE) {
948 if (conf->dev_type == DEV_DEVMAP)
949 dm_flush_map(conf->dev, DEFAULT_TARGET);
951 condlog(0, "must provide a map name to remove");
955 else if (conf->remove == FLUSH_ALL) {
956 dm_flush_maps(DEFAULT_TARGET);
961 * allocate core vectors to store paths and multipaths
963 curmp = vector_alloc();
964 pathvec = vector_alloc();
966 if (!curmp || !pathvec) {
967 condlog(0, "can not allocate memory");
972 * if we have a blacklisted device parameter, exit early
974 if (conf->dev && blacklist(conf->blist, conf->dev))
977 if (!cache_cold(CACHE_EXPIRE)) {
978 condlog(3, "load path identifiers cache");
989 /* extended path info '-ll' */
990 di_flag |= DI_SYSFS | DI_CHECKER;
992 /* minimum path info '-l' */
998 if (path_discovery(pathvec, conf, di_flag) || VECTOR_SIZE(pathvec) == 0)
1001 if (conf->verbosity > 2) {
1002 fprintf(stdout, "#\n# all paths :\n#\n");
1003 print_all_paths(pathvec);
1006 refwwid = get_refwwid(pathvec);
1008 if (get_dm_mpvec(curmp, pathvec, refwwid))
1011 cache_dump(pathvec);
1012 filter_pathvec(pathvec, refwwid);
1018 * core logic entry point
1020 coalesce_paths(curmp, pathvec);
1026 free_multipathvec(curmp, KEEP_PATHS);
1027 free_pathvec(pathvec, FREE_PATHS);
1032 dbg_free_final(NULL);