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: 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.
18 * Copyright (c) 2003, 2004, 2005 Christophe Varoqui
19 * Copyright (c) 2005 Benjamin Marzinski, Redhat
20 * Copyright (c) 2005 Kiyoshi Ueda, NEC
21 * Copyright (c) 2005 Patrick Caulfield, Redhat
22 * Copyright (c) 2005 Edward Goggin, EMC
28 #include <sysfs/libsysfs.h>
33 #include <libdevmapper.h>
34 #include <devmapper.h>
38 #include <structs_vec.h>
41 #include <blacklist.h>
42 #include <discovery.h>
44 #include <switchgroup.h>
47 #include <configure.h>
48 #include <pgpolicies.h>
52 filter_pathvec (vector pathvec, char * refwwid)
57 if (!refwwid || !strlen(refwwid))
60 vector_foreach_slot (pathvec, pp, i) {
61 if (strncmp(pp->wwid, refwwid, WWID_SIZE) != 0) {
62 condlog(3, "skip path %s : out of scope", pp->dev);
64 vector_del_slot(pathvec, i);
72 usage (char * progname)
74 fprintf (stderr, VERSION_STRING);
75 fprintf (stderr, "Usage: %s\t[-v level] [-d] [-h|-l|-ll|-f|-F]\n",
78 "\t\t\t[-p failover|multibus|group_by_serial|group_by_prio]\n" \
81 "\t-v level\tverbosity level\n" \
82 "\t 0\t\t\tno output\n" \
83 "\t 1\t\t\tprint created devmap names only\n" \
84 "\t 2\t\t\tdefault verbosity\n" \
85 "\t 3\t\t\tprint debug information\n" \
86 "\t-h\t\tprint this usage text\n" \
87 "\t-b file\t\tbindings file location\n" \
88 "\t-d\t\tdry run, do not create or update devmaps\n" \
89 "\t-l\t\tshow multipath topology (sysfs and DM info)\n" \
90 "\t-ll\t\tshow multipath topology (maximum info)\n" \
91 "\t-f\t\tflush a multipath device map\n" \
92 "\t-F\t\tflush all multipath device maps\n" \
93 "\t-p policy\tforce all maps to specified policy :\n" \
94 "\t failover\t\t1 path per priority group\n" \
95 "\t multibus\t\tall paths in 1 priority group\n" \
96 "\t group_by_serial\t1 priority group per serial\n" \
97 "\t group_by_prio\t1 priority group per priority lvl\n" \
98 "\t group_by_node_name\t1 priority group per target node\n" \
100 "\tdevice\t\tlimit scope to the device's multipath\n" \
101 "\t\t\t(udev-style $DEVNAME reference, eg /dev/sdb\n" \
102 "\t\t\tor major:minor or a device map name)\n" \
109 update_paths (struct multipath * mpp)
112 struct pathgroup * pgp;
118 vector_foreach_slot (mpp->pg, pgp, i) {
122 vector_foreach_slot (pgp->paths, pp, j) {
123 if (!strlen(pp->dev)) {
124 if (devt2devname(pp->dev, pp->dev_t)) {
126 * path is not in sysfs anymore
128 pp->state = PATH_DOWN;
132 pathinfo(pp, conf->hwtable, DI_ALL);
136 if (pp->state == PATH_UNCHECKED)
137 pathinfo(pp, conf->hwtable, DI_CHECKER);
139 if (pp->priority == PRIO_UNDEF)
140 pathinfo(pp, conf->hwtable, DI_PRIO);
147 get_dm_mpvec (vector curmp, vector pathvec, char * refwwid)
150 struct multipath * mpp;
152 if (dm_get_maps(curmp, DEFAULT_TARGET))
155 vector_foreach_slot (curmp, mpp, i) {
157 * discard out of scope maps
159 if (mpp->wwid && refwwid &&
160 strncmp(mpp->wwid, refwwid, WWID_SIZE)) {
161 condlog(3, "skip map %s: out of scope", mpp->alias);
162 free_multipath(mpp, KEEP_PATHS);
163 vector_del_slot(curmp, i);
168 condlog(3, "params = %s", mpp->params);
169 condlog(3, "status = %s", mpp->status);
171 disassemble_map(pathvec, mpp->params, mpp);
174 * disassemble_map() can add new paths to pathvec.
175 * If not in "fast list mode", we need to fetch information
182 mpp->bestpg = select_path_group(mpp);
184 disassemble_status(mpp->status, mpp);
187 print_multipath_topology(mpp, conf->verbosity);
190 reinstate_paths(mpp);
206 vector pathvec = NULL;
210 char * refwwid = NULL;
214 * allocate core vectors to store paths and multipaths
216 curmp = vector_alloc();
217 pathvec = vector_alloc();
219 if (!curmp || !pathvec) {
220 condlog(0, "can not allocate memory");
223 vecs.pathvec = pathvec;
227 * if we have a blacklisted device parameter, exit early
230 if (!strncmp(conf->dev, "/dev/", 5) &&
231 strlen(conf->dev) > 5)
237 if (dev && blacklist(conf->blist_devnode, dev))
241 * scope limiting must be translated into a wwid
242 * failing the translation is fatal (by policy)
245 refwwid = get_refwwid(conf->dev, conf->dev_type, pathvec);
248 condlog(3, "scope is nul");
251 condlog(3, "scope limited to %s", refwwid);
253 if (blacklist(conf->blist_wwid, refwwid))
264 /* extended path info '-ll' */
265 di_flag |= DI_SYSFS | DI_CHECKER;
267 /* minimum path info '-l' */
273 if (path_discovery(pathvec, conf, di_flag))
276 if (conf->verbosity > 2)
277 print_all_paths(pathvec, 1);
279 get_path_layout(pathvec);
281 if (get_dm_mpvec(curmp, pathvec, refwwid))
284 filter_pathvec(pathvec, refwwid);
292 * core logic entry point
294 r = coalesce_paths(&vecs, NULL, NULL);
300 free_multipathvec(curmp, KEEP_PATHS);
301 free_pathvec(pathvec, FREE_PATHS);
307 main (int argc, char *argv[])
315 fprintf(stderr, "need to be root\n");
319 if (dm_prereq(DEFAULT_TARGET))
322 if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
323 condlog(0, "multipath tools need sysfs mounted");
326 if (load_config(DEFAULT_CONFIGFILE))
329 while ((arg = getopt(argc, argv, ":dhl::FfM:v:p:b:")) != EOF ) {
331 case 1: printf("optarg : %s\n",optarg);
334 if (sizeof(optarg) > sizeof(char *) ||
338 conf->verbosity = atoi(optarg);
341 conf->bindings_file = optarg;
347 conf->remove = FLUSH_ONE;
350 conf->remove = FLUSH_ALL;
356 if (optarg && !strncmp(optarg, "l", 1))
362 debug = atoi(optarg);
366 conf->pgpolicy_flag = get_pgpolicy_id(optarg);
367 if (conf->pgpolicy_flag == -1) {
368 printf("'%s' is not a valid policy\n", optarg);
375 fprintf(stderr, "Missing option arguement\n");
378 fprintf(stderr, "Unknown switch: %s\n", optarg);
385 conf->dev = MALLOC(FILE_NAME_SIZE);
390 strncpy(conf->dev, argv[optind], FILE_NAME_SIZE);
392 if (filepresent(conf->dev))
393 conf->dev_type = DEV_DEVNODE;
394 else if (sscanf(conf->dev, "%d:%d", &i, &i) == 2)
395 conf->dev_type = DEV_DEVT;
397 conf->dev_type = DEV_DEVMAP;
401 if (conf->remove == FLUSH_ONE) {
402 if (conf->dev_type == DEV_DEVMAP)
403 dm_flush_map(conf->dev, DEFAULT_TARGET);
405 condlog(0, "must provide a map name to remove");
409 else if (conf->remove == FLUSH_ALL) {
410 dm_flush_maps(DEFAULT_TARGET);
413 while ((r = configure()) < 0)
414 condlog(3, "restart multipath configuration process");
421 dbg_free_final(NULL);