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
33 #include <libdevmapper.h>
34 #include <devmapper.h>
38 #include <structs_vec.h>
42 #include <blacklist.h>
43 #include <discovery.h>
45 #include <switchgroup.h>
48 #include <configure.h>
49 #include <pgpolicies.h>
53 #include <sys/resource.h>
58 filter_pathvec (vector pathvec, char * refwwid)
63 if (!refwwid || !strlen(refwwid))
66 vector_foreach_slot (pathvec, pp, i) {
67 if (strncmp(pp->wwid, refwwid, WWID_SIZE) != 0) {
68 condlog(3, "skip path %s : out of scope", pp->dev);
70 vector_del_slot(pathvec, i);
78 usage (char * progname)
80 fprintf (stderr, VERSION_STRING);
81 fprintf (stderr, "Usage:\n");
82 fprintf (stderr, " %s [-d] [-r] [-v lvl] [-p pol] [-b fil] [dev]\n", progname);
83 fprintf (stderr, " %s -l|-ll|-f [-v lvl] [-b fil] [dev]\n", progname);
84 fprintf (stderr, " %s -F [-v lvl]\n", progname);
85 fprintf (stderr, " %s -t\n", progname);
86 fprintf (stderr, " %s -h\n", progname);
90 " -h print this usage text\n" \
91 " -l show multipath topology (sysfs and DM info)\n" \
92 " -ll show multipath topology (maximum info)\n" \
93 " -f flush a multipath device map\n" \
94 " -F flush all multipath device maps\n" \
95 " -d dry run, do not create or update devmaps\n" \
96 " -t dump internal hardware table\n" \
97 " -r force devmap reload\n" \
98 " -p policy failover|multibus|group_by_serial|group_by_prio\n" \
99 " -b fil bindings file location\n" \
100 " -p pol force all maps to specified path grouping policy :\n" \
101 " . failover one path per priority group\n" \
102 " . multibus all paths in one priority group\n" \
103 " . group_by_serial one priority group per serial\n" \
104 " . group_by_prio one priority group per priority lvl\n" \
105 " . group_by_node_name one priority group per target node\n" \
106 " -v lvl verbosity level\n" \
108 " . 1 print created devmap names only\n" \
109 " . 2 default verbosity\n" \
110 " . 3 print debug information\n" \
111 " dev action limited to:\n" \
112 " . multipath named 'dev' (ex: mpath0) or\n" \
113 " . multipath whose wwid is 'dev' (ex: 60051..)\n" \
114 " . multipath including the path named 'dev' (ex: /dev/sda)\n" \
115 " . multipath including the path with maj:min 'dev' (ex: 8:0)\n" \
122 update_paths (struct multipath * mpp)
125 struct pathgroup * pgp;
131 vector_foreach_slot (mpp->pg, pgp, i) {
135 vector_foreach_slot (pgp->paths, pp, j) {
136 if (!strlen(pp->dev)) {
137 if (devt2devname(pp->dev, FILE_NAME_SIZE,
140 * path is not in sysfs anymore
142 pp->state = PATH_DOWN;
146 pathinfo(pp, conf->hwtable, DI_ALL);
150 if (pp->state == PATH_UNCHECKED ||
151 pp->state == PATH_WILD)
152 pathinfo(pp, conf->hwtable, DI_CHECKER);
154 if (pp->priority == PRIO_UNDEF)
155 pathinfo(pp, conf->hwtable, DI_PRIO);
162 get_dm_mpvec (vector curmp, vector pathvec, char * refwwid)
165 struct multipath * mpp;
166 char params[PARAMS_SIZE], status[PARAMS_SIZE];
168 if (dm_get_maps(curmp))
171 vector_foreach_slot (curmp, mpp, i) {
173 * discard out of scope maps
175 if (mpp->wwid && refwwid &&
176 strncmp(mpp->wwid, refwwid, WWID_SIZE)) {
177 condlog(3, "skip map %s: out of scope", mpp->alias);
178 free_multipath(mpp, KEEP_PATHS);
179 vector_del_slot(curmp, i);
184 dm_get_map(mpp->alias, &mpp->size, params);
185 condlog(3, "params = %s", params);
186 dm_get_status(mpp->alias, status);
187 condlog(3, "status = %s", status);
189 disassemble_map(pathvec, params, mpp);
192 * disassemble_map() can add new paths to pathvec.
193 * If not in "fast list mode", we need to fetch information
200 mpp->bestpg = select_path_group(mpp);
202 disassemble_status(status, mpp);
205 print_multipath_topology(mpp, conf->verbosity);
208 reinstate_paths(mpp);
224 vector pathvec = NULL;
228 char * refwwid = NULL;
232 * allocate core vectors to store paths and multipaths
234 curmp = vector_alloc();
235 pathvec = vector_alloc();
237 if (!curmp || !pathvec) {
238 condlog(0, "can not allocate memory");
241 vecs.pathvec = pathvec;
245 * dev is "/dev/" . "sysfs block dev"
248 if (!strncmp(conf->dev, "/dev/", 5) &&
249 strlen(conf->dev) > 5)
256 * if we have a blacklisted device parameter, exit early
259 (filter_devnode(conf->blist_devnode, conf->elist_devnode, dev) > 0))
263 * scope limiting must be translated into a wwid
264 * failing the translation is fatal (by policy)
267 refwwid = get_refwwid(conf->dev, conf->dev_type, pathvec);
270 condlog(3, "scope is nul");
273 condlog(3, "scope limited to %s", refwwid);
274 if (filter_wwid(conf->blist_wwid, conf->elist_wwid,
286 /* extended path info '-ll' */
287 di_flag |= DI_SYSFS | DI_CHECKER;
289 /* minimum path info '-l' */
295 if (path_discovery(pathvec, conf, di_flag))
298 if (conf->verbosity > 2)
299 print_all_paths(pathvec, 1);
301 get_path_layout(pathvec, 0);
303 if (get_dm_mpvec(curmp, pathvec, refwwid))
306 filter_pathvec(pathvec, refwwid);
314 * core logic entry point
316 r = coalesce_paths(&vecs, NULL, NULL, conf->force_reload);
322 free_multipathvec(curmp, KEEP_PATHS);
323 free_pathvec(pathvec, FREE_PATHS);
333 unsigned int maxlen = 256;
336 reply = MALLOC(maxlen);
342 c += snprint_defaults(c, reply + maxlen - c);
343 again = ((c - reply) == maxlen);
345 reply = REALLOC(reply, maxlen *= 2);
348 c += snprint_blacklist(c, reply + maxlen - c);
349 again = ((c - reply) == maxlen);
351 reply = REALLOC(reply, maxlen *= 2);
354 c += snprint_blacklist_except(c, reply + maxlen - c);
355 again = ((c - reply) == maxlen);
357 reply = REALLOC(reply, maxlen *= 2);
360 c += snprint_hwtable(c, reply + maxlen - c, conf->hwtable);
361 again = ((c - reply) == maxlen);
363 reply = REALLOC(reply, maxlen *= 2);
366 c += snprint_mptable(c, reply + maxlen - c, conf->mptable);
367 again = ((c - reply) == maxlen);
369 reply = REALLOC(reply, maxlen *= 2);
378 main (int argc, char *argv[])
386 fprintf(stderr, "need to be root\n");
393 if (load_config(DEFAULT_CONFIGFILE))
396 if (sysfs_init(conf->sysfs_dir, FILE_NAME_SIZE)) {
397 condlog(0, "multipath tools need sysfs mounted");
400 while ((arg = getopt(argc, argv, ":dhl::FfM:v:p:b:Brt")) != EOF ) {
402 case 1: printf("optarg : %s\n",optarg);
405 if (sizeof(optarg) > sizeof(char *) ||
409 conf->verbosity = atoi(optarg);
412 conf->bindings_file = strdup(optarg);
415 conf->bindings_read_only = 1;
421 conf->remove = FLUSH_ONE;
424 conf->remove = FLUSH_ALL;
430 if (optarg && !strncmp(optarg, "l", 1))
436 debug = atoi(optarg);
440 conf->pgpolicy_flag = get_pgpolicy_id(optarg);
441 if (conf->pgpolicy_flag == -1) {
442 printf("'%s' is not a valid policy\n", optarg);
447 conf->force_reload = 1;
455 fprintf(stderr, "Missing option arguement\n");
458 fprintf(stderr, "Unknown switch: %s\n", optarg);
465 conf->dev = MALLOC(FILE_NAME_SIZE);
470 strncpy(conf->dev, argv[optind], FILE_NAME_SIZE);
472 if (filepresent(conf->dev))
473 conf->dev_type = DEV_DEVNODE;
474 else if (sscanf(conf->dev, "%d:%d", &i, &i) == 2)
475 conf->dev_type = DEV_DEVT;
477 conf->dev_type = DEV_DEVMAP;
483 struct rlimit fd_limit;
485 fd_limit.rlim_cur = conf->max_fds;
486 fd_limit.rlim_max = conf->max_fds;
487 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0)
488 condlog(0, "can't set open fds limit to %d : %s\n",
489 conf->max_fds, strerror(errno));
492 if (init_checkers()) {
493 condlog(0, "failed to initialize checkers");
497 condlog(0, "failed to initialize prioritizers");
502 if (conf->remove == FLUSH_ONE) {
503 if (conf->dev_type == DEV_DEVMAP)
504 r = dm_flush_map(conf->dev);
506 condlog(0, "must provide a map name to remove");
510 else if (conf->remove == FLUSH_ALL) {
514 while ((r = configure()) < 0)
515 condlog(3, "restart multipath configuration process");
518 udev_wait(conf->cookie);
527 * Freeing config must be done after dm_lib_exit(), because
528 * the logging function (dm_write_log()), which is called there,
529 * references the config.
535 dbg_free_final(NULL);