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);
154 #define PRINT_PATH_ALL 0
155 #define PRINT_PATH_SHORT 1
158 print_path (struct path * pp, int style)
160 char buff[MAX_LINE_LEN];
162 if (style != PRINT_PATH_SHORT && pp->wwid)
163 printf ("%s ", pp->wwid);
167 snprint_path(&buff[0], MAX_LINE_LEN, pp, &pl);
168 printf("%s\n", buff);
172 print_map (struct multipath * mpp)
174 if (mpp->size && mpp->params)
175 printf("0 %llu %s %s\n",
176 mpp->size, DEFAULT_TARGET, mpp->params);
181 print_all_paths (vector pathvec)
186 vector_foreach_slot (pathvec, pp, i)
187 print_path(pp, PRINT_PATH_ALL);
191 print_mp (struct multipath * mpp)
194 struct path * pp = NULL;
195 struct pathgroup * pgp = NULL;
197 if (mpp->action == ACT_NOTHING || conf->verbosity == 0)
200 if (conf->verbosity > 1) {
201 switch (mpp->action) {
203 printf("%s: ", ACT_RELOAD_STR);
207 printf("%s: ", ACT_CREATE_STR);
211 printf("%s: ", ACT_SWITCHPG_STR);
220 printf("%s", mpp->alias);
222 if (conf->verbosity == 1) {
226 if (strncmp(mpp->alias, mpp->wwid, WWID_SIZE))
227 printf(" (%s)", mpp->wwid);
231 if (mpp->size < (1 << 11))
232 printf("[size=%llu kB]", mpp->size >> 1);
233 else if (mpp->size < (1 << 21))
234 printf("[size=%llu MB]", mpp->size >> 11);
235 else if (mpp->size < (1 << 31))
236 printf("[size=%llu GB]", mpp->size >> 21);
238 printf("[size=%llu TB]", mpp->size >> 31);
241 printf("[features=\"%s\"]", mpp->features);
244 printf("[hwhandler=\"%s\"]", mpp->hwhandler);
246 fprintf(stdout, "\n");
251 vector_foreach_slot (mpp->pg, pgp, j) {
255 printf("%s ", mpp->selector);
257 /* align to path status info */
258 for (i = pl.hbtl_len + pl.dev_len + pl.dev_t_len + 4;
259 i > strlen(mpp->selector); i--)
264 switch (pgp->status) {
265 case PGSTATE_ENABLED:
268 case PGSTATE_DISABLED:
269 printf("[disabled]");
279 vector_foreach_slot (pgp->paths, pp, i)
280 print_path(pp, PRINT_PATH_SHORT);
286 filter_pathvec (vector pathvec, char * refwwid)
291 if (!refwwid || !strlen(refwwid))
294 vector_foreach_slot (pathvec, pp, i) {
295 if (memcmp(pp->wwid, refwwid, WWID_SIZE) != 0) {
296 condlog(3, "skip path %s : out of scope", pp->dev);
298 vector_del_slot(pathvec, i);
306 * Transforms the path group vector into a proper device map string
309 assemble_map (struct multipath * mp)
314 struct pathgroup * pgp;
318 freechar = sizeof(mp->params);
320 shift = snprintf(p, freechar, "%s %s %i %i",
321 mp->features, mp->hwhandler,
322 VECTOR_SIZE(mp->pg), mp->nextpg);
324 if (shift >= freechar) {
325 fprintf(stderr, "mp->params too small\n");
331 vector_foreach_slot (mp->pg, pgp, i) {
332 pgp = VECTOR_SLOT(mp->pg, i);
333 shift = snprintf(p, freechar, " %s %i 1", mp->selector,
334 VECTOR_SIZE(pgp->paths));
335 if (shift >= freechar) {
336 fprintf(stderr, "mp->params too small\n");
342 vector_foreach_slot (pgp->paths, pp, j) {
343 shift = snprintf(p, freechar, " %s %d",
344 pp->dev_t, conf->minio);
345 if (shift >= freechar) {
346 fprintf(stderr, "mp->params too small\n");
354 fprintf(stderr, "mp->params too small\n");
357 snprintf(p, 1, "\n");
359 if (conf->verbosity > 2)
366 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 * don't bother if a constituant path is claimed
381 * FIXME : claimed detection broken, always unclaimed for now
383 vector_foreach_slot (mpp->paths, pp, i) {
385 condlog(3, "%s claimed", pp->dev);
391 * properties selectors
393 select_pgpolicy(mpp);
394 select_selector(mpp);
395 select_features(mpp);
396 select_hwhandler(mpp);
399 * apply selected grouping policy to valid paths
401 switch (mpp->pgpolicy) {
406 one_path_per_group(mpp);
408 case GROUP_BY_SERIAL:
409 group_by_serial(mpp);
414 case GROUP_BY_NODE_NAME:
415 group_by_node_name(mpp);
421 if (mpp->pg == NULL) {
422 condlog(3, "pgpolicy failed to produce a pg vector");
427 * ponders each path group and determine highest prio pg
428 * to switch over (default to first)
430 select_path_group(mpp);
433 * transform the mp->pg vector of vectors of paths
434 * into a mp->params strings to feed the device-mapper
436 if (assemble_map(mpp)) {
437 condlog(3, "problem assembing map");
444 pathcount (struct multipath * mpp, int state)
446 struct pathgroup *pgp;
451 vector_foreach_slot (mpp->pg, pgp, i)
452 vector_foreach_slot (pgp->paths, pp, j)
453 if (pp->state == state)
459 compute_pgid(struct pathgroup * pgp)
464 vector_foreach_slot (pgp->paths, pp, i)
469 pgcmp (struct multipath * mpp, struct multipath * cmpp)
472 struct pathgroup * pgp;
473 struct pathgroup * cpgp;
476 vector_foreach_slot (mpp->pg, pgp, i) {
479 vector_foreach_slot (cmpp->pg, cpgp, j) {
480 if (pgp->id == cpgp->id) {
493 select_action (struct multipath * mpp, vector curmp)
495 struct multipath * cmpp;
497 cmpp = find_mp(curmp, mpp->alias);
500 cmpp = find_mp_by_wwid(curmp, mpp->wwid);
502 if (cmpp && !conf->dry_run) {
503 condlog(2, "remove: %s (dup of %s)",
504 cmpp->alias, mpp->alias);
505 dm_flush_map(cmpp->alias, DEFAULT_TARGET);
507 mpp->action = ACT_CREATE;
510 if (pathcount(mpp, PATH_UP) == 0) {
511 condlog(3, "no good path");
512 mpp->action = ACT_NOTHING;
515 if (cmpp->size != mpp->size) {
516 condlog(3, "size different than current");
517 mpp->action = ACT_RELOAD;
520 if (strncmp(cmpp->features, mpp->features,
521 strlen(mpp->features))) {
522 condlog(3, "features different than current");
523 mpp->action = ACT_RELOAD;
526 if (strncmp(cmpp->hwhandler, mpp->hwhandler,
527 strlen(mpp->hwhandler))) {
528 condlog(3, "hwhandler different than current");
529 mpp->action = ACT_RELOAD;
532 if (strncmp(cmpp->selector, mpp->selector,
533 strlen(mpp->selector))) {
534 condlog(3, "selector different than current");
535 mpp->action = ACT_RELOAD;
538 if (VECTOR_SIZE(cmpp->pg) != VECTOR_SIZE(mpp->pg)) {
539 condlog(3, "different number of PG");
540 mpp->action = ACT_RELOAD;
543 if (pgcmp(mpp, cmpp)) {
544 condlog(3, "different path group topology");
545 mpp->action = ACT_RELOAD;
548 if (cmpp->nextpg != mpp->nextpg) {
549 condlog(3, "nextpg different than current");
550 mpp->action = ACT_SWITCHPG;
553 mpp->action = ACT_NOTHING;
558 reinstate_paths (struct multipath * mpp)
561 struct pathgroup * pgp;
564 vector_foreach_slot (mpp->pg, pgp, i) {
565 vector_foreach_slot (pgp->paths, pp, j) {
566 if (pp->state != PATH_UP &&
567 (pgp->status == PGSTATE_DISABLED ||
568 pgp->status == PGSTATE_ACTIVE))
571 if (pp->dmstate == PSTATE_FAILED) {
572 if (dm_reinstate(mpp->alias, pp->dev_t))
573 condlog(0, "error reinstating %s",
582 domap (struct multipath * mpp)
584 int op = ACT_NOTHING;
590 * last chance to quit before touching the devmaps
595 switch (mpp->action) {
600 dm_switchgroup(mpp->alias, mpp->nextpg);
602 * we may have avoided reinstating paths because there where in
603 * active or disabled PG. Now that the topology has changed,
606 reinstate_paths(mpp);
610 op = DM_DEVICE_CREATE;
614 op = DM_DEVICE_RELOAD;
623 * device mapper creation or updating
624 * here we know we'll have garbage on stderr from
625 * libdevmapper. so shut it down temporarily.
627 dm_log_init_verbose(0);
629 r = dm_addmap(op, mpp->alias, DEFAULT_TARGET, mpp->params, mpp->size);
632 dm_simplecmd(DM_DEVICE_REMOVE, mpp->alias);
633 else if (op == DM_DEVICE_RELOAD)
634 dm_simplecmd(DM_DEVICE_RESUME, mpp->alias);
637 * PG order is random, so we need to set the primary one
638 * upon create or reload
640 dm_switchgroup(mpp->alias, mpp->nextpg);
642 dm_log_init_verbose(1);
648 coalesce_paths (vector curmp, vector pathvec)
651 char empty_buff[WWID_SIZE];
652 struct multipath * mpp;
656 memset(empty_buff, 0, WWID_SIZE);
658 vector_foreach_slot (pathvec, pp1, k) {
659 /* skip this path for some reason */
661 /* 1. if path has no unique id or wwid blacklisted */
662 if (memcmp(empty_buff, pp1->wwid, WWID_SIZE) == 0 ||
663 blacklist(conf->blist, pp1->wwid))
666 /* 2. if path already coalesced */
671 * at this point, we know we really got a new mp
673 mpp = alloc_multipath();
678 mpp->mpe = find_mpe(pp1->wwid);
683 strcpy(mpp->wwid, pp1->wwid);
684 mpp->size = pp1->size;
685 mpp->paths = vector_alloc();
687 if (pp1->priority < 0)
688 mpp->action = ACT_NOTHING;
693 if (store_path(mpp->paths, pp1))
696 for (i = k + 1; i < VECTOR_SIZE(pathvec); i++) {
697 pp2 = VECTOR_SLOT(pathvec, i);
699 if (strcmp(pp1->wwid, pp2->wwid))
704 if (pp2->size != mpp->size) {
706 * ouch, avoid feeding that to the DM
708 condlog(3, "path size mismatch : discard %s",
710 mpp->action = ACT_NOTHING;
712 if (pp2->priority < 0)
713 mpp->action = ACT_NOTHING;
715 if (store_path(mpp->paths, pp2))
718 if (setup_map(mpp)) {
719 free_multipath(mpp, KEEP_PATHS);
722 condlog(3, "action preset to %i", mpp->action);
724 if (mpp->action == ACT_UNDEF)
725 select_action(mpp, curmp);
727 condlog(3, "action set to %i", mpp->action);
730 free_multipath(mpp, KEEP_PATHS);
736 usage (char * progname)
738 fprintf (stderr, VERSION_STRING);
739 fprintf (stderr, "Usage: %s\t[-v level] [-d] [-l|-ll|-f|-F]\n",
742 "\t\t\t[-p failover|multibus|group_by_serial|group_by_prio]\n" \
745 "\t-v level\tverbosty level\n" \
746 "\t 0\t\t\tno output\n" \
747 "\t 1\t\t\tprint created devmap names only\n" \
748 "\t 2\t\t\tdefault verbosity\n" \
749 "\t 3\t\t\tprint debug information\n" \
750 "\t-d\t\tdry run, do not create or update devmaps\n" \
751 "\t-l\t\tshow multipath topology (sysfs and DM info)\n" \
752 "\t-ll\t\tshow multipath topology (maximum info)\n" \
753 "\t-F\t\tflush a multipath device map\n" \
754 "\t-F\t\tflush all multipath device maps\n" \
755 "\t-p policy\tforce all maps to specified policy :\n" \
756 "\t failover\t\t1 path per priority group\n" \
757 "\t multibus\t\tall paths in 1 priority group\n" \
758 "\t group_by_serial\t1 priority group per serial\n" \
759 "\t group_by_prio\t1 priority group per priority lvl\n" \
760 "\t group_by_node_name\t1 priority group per target node\n" \
762 "\tdevice\t\tlimit scope to the device's multipath\n" \
763 "\t\t\t(udev-style $DEVNAME reference, eg /dev/sdb\n" \
764 "\t\t\tor major:minor or a device map name)\n" \
771 update_pathvec (vector pathvec)
776 vector_foreach_slot (pathvec, pp, i) {
777 if (pp->dev && pp->dev_t && strlen(pp->dev) == 0) {
778 devt2devname(pp->dev, pp->dev_t);
779 pathinfo(pp, conf->hwtable,
780 DI_SYSFS | DI_CHECKER | DI_SERIAL | DI_PRIO);
782 if (pp->checkfn && pp->state == PATH_UNCHECKED)
783 pp->state = pp->checkfn(pp->fd, NULL, NULL);
789 get_dm_mpvec (vector curmp, vector pathvec, char * refwwid)
792 struct multipath * mpp;
795 if (dm_get_maps(curmp, DEFAULT_TARGET))
798 vector_foreach_slot (curmp, mpp, i) {
799 wwid = get_mpe_wwid(mpp->alias);
802 /* out of specified scope */
803 if (refwwid && strncmp(wwid, refwwid, WWID_SIZE))
808 condlog(3, "params = %s", mpp->params);
809 condlog(3, "status = %s", mpp->status);
811 /* will set mpp->wwid */
812 disassemble_map(pathvec, mpp->params, mpp);
815 * disassemble_map may have added new paths to pathvec.
816 * If not in "fast list mode", we need to fetch information
820 update_pathvec(pathvec);
822 disassemble_status(mpp->status, mpp);
828 reinstate_paths(mpp);
834 main (int argc, char *argv[])
837 vector pathvec = NULL;
843 char * refwwid = NULL;
846 fprintf(stderr, "need to be root\n");
850 if (dm_prereq(DEFAULT_TARGET, 1, 0, 3))
853 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
854 condlog(0, "multipath tools need sysfs mounted");
857 if (load_config(DEFAULT_CONFIGFILE))
860 while ((arg = getopt(argc, argv, ":qdl::Ffi:M:v:p:")) != EOF ) {
862 case 1: printf("optarg : %s\n",optarg);
865 if (sizeof(optarg) > sizeof(char *) ||
869 conf->verbosity = atoi(optarg);
884 if (optarg && !strncmp(optarg, "l", 1))
890 debug = atoi(optarg);
894 conf->pgpolicy_flag = get_pgpolicy_id(optarg);
895 if (conf->pgpolicy_flag == -1) {
896 printf("'%s' is not a valid policy\n", optarg);
901 fprintf(stderr, "Missing option arguement\n");
904 fprintf(stderr, "Unknown switch: %s\n", optarg);
911 conf->dev = MALLOC(FILE_NAME_SIZE);
916 strncpy(conf->dev, argv[optind], FILE_NAME_SIZE);
918 if (filepresent(conf->dev))
919 conf->dev_type = DEV_DEVNODE;
920 else if (sscanf(conf->dev, "%d:%d", &i, &i) == 2)
921 conf->dev_type = DEV_DEVT;
923 conf->dev_type = DEV_DEVMAP;
927 if (conf->remove == FLUSH_ONE) {
928 if (conf->dev_type == DEV_DEVMAP)
929 dm_flush_map(conf->dev, DEFAULT_TARGET);
931 condlog(0, "must provide a map name to remove");
935 else if (conf->remove == FLUSH_ALL) {
936 dm_flush_maps(DEFAULT_TARGET);
941 * allocate core vectors to store paths and multipaths
943 curmp = vector_alloc();
944 pathvec = vector_alloc();
946 if (!curmp || !pathvec) {
947 condlog(0, "can not allocate memory");
952 * if we have a blacklisted device parameter, exit early
954 if (conf->dev && blacklist(conf->blist, conf->dev))
957 condlog(3, "load path identifiers cache");
960 if (conf->verbosity > 2) {
961 fprintf(stdout, "#\n# all paths in cache :\n#\n");
962 print_all_paths(pathvec);
972 /* extended path info '-ll' */
973 di_flag |= DI_SYSFS | DI_CHECKER;
975 /* minimum path info '-l' */
981 if (path_discovery(pathvec, conf, di_flag) || VECTOR_SIZE(pathvec) == 0)
984 if (conf->verbosity > 2) {
985 fprintf(stdout, "#\n# all paths :\n#\n");
986 print_all_paths(pathvec);
989 refwwid = get_refwwid(pathvec);
990 get_path_layout(&pl, pathvec);
992 if (get_dm_mpvec(curmp, pathvec, refwwid))
995 filter_pathvec(pathvec, refwwid);
1001 * core logic entry point
1003 coalesce_paths(curmp, pathvec);
1009 free_multipathvec(curmp, KEEP_PATHS);
1010 free_pathvec(pathvec, FREE_PATHS);
1015 dbg_free_final(NULL);