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
25 #include <sys/types.h>
35 #include <libdevmapper.h>
36 #include <devmapper.h>
40 #include <structs_vec.h>
44 #include <blacklist.h>
45 #include <discovery.h>
47 #include <switchgroup.h>
50 #include <configure.h>
51 #include <pgpolicies.h>
55 #include <sys/resource.h>
62 filter_pathvec (vector pathvec, char * refwwid)
67 if (!refwwid || !strlen(refwwid))
70 vector_foreach_slot (pathvec, pp, i) {
71 if (strncmp(pp->wwid, refwwid, WWID_SIZE) != 0) {
72 condlog(3, "skip path %s : out of scope", pp->dev);
74 vector_del_slot(pathvec, i);
82 usage (char * progname)
84 fprintf (stderr, VERSION_STRING);
85 fprintf (stderr, "Usage:\n");
86 fprintf (stderr, " %s [-c] [-d] [-r] [-v lvl] [-p pol] [-b fil] [-q] [dev]\n", progname);
87 fprintf (stderr, " %s -l|-ll|-f [-v lvl] [-b fil] [dev]\n", progname);
88 fprintf (stderr, " %s -F [-v lvl]\n", progname);
89 fprintf (stderr, " %s -t\n", progname);
90 fprintf (stderr, " %s -h\n", progname);
94 " -h print this usage text\n" \
95 " -l show multipath topology (sysfs and DM info)\n" \
96 " -ll show multipath topology (maximum info)\n" \
97 " -f flush a multipath device map\n" \
98 " -F flush all multipath device maps\n" \
99 " -c check if a device should be a path in a multipath device\n" \
100 " -q allow queue_if_no_path when multipathd is not running\n"\
101 " -d dry run, do not create or update devmaps\n" \
102 " -t dump internal hardware table\n" \
103 " -r force devmap reload\n" \
104 " -B treat the bindings file as read only\n" \
105 " -p policy failover|multibus|group_by_serial|group_by_prio\n" \
106 " -b fil bindings file location\n" \
107 " -p pol force all maps to specified path grouping policy :\n" \
108 " . failover one path per priority group\n" \
109 " . multibus all paths in one priority group\n" \
110 " . group_by_serial one priority group per serial\n" \
111 " . group_by_prio one priority group per priority lvl\n" \
112 " . group_by_node_name one priority group per target node\n" \
113 " -v lvl verbosity level\n" \
115 " . 1 print created devmap names only\n" \
116 " . 2 default verbosity\n" \
117 " . 3 print debug information\n" \
118 " dev action limited to:\n" \
119 " . multipath named 'dev' (ex: mpath0) or\n" \
120 " . multipath whose wwid is 'dev' (ex: 60051..)\n" \
121 " . multipath including the path named 'dev' (ex: /dev/sda)\n" \
122 " . multipath including the path with maj:min 'dev' (ex: 8:0)\n" \
128 update_paths (struct multipath * mpp)
131 struct pathgroup * pgp;
137 vector_foreach_slot (mpp->pg, pgp, i) {
141 vector_foreach_slot (pgp->paths, pp, j) {
142 if (!strlen(pp->dev)) {
143 if (devt2devname(pp->dev, FILE_NAME_SIZE,
146 * path is not in sysfs anymore
148 pp->chkrstate = pp->state = PATH_DOWN;
152 pathinfo(pp, conf->hwtable, DI_ALL);
156 if (pp->state == PATH_UNCHECKED ||
157 pp->state == PATH_WILD)
158 pathinfo(pp, conf->hwtable, DI_CHECKER);
160 if (pp->priority == PRIO_UNDEF)
161 pathinfo(pp, conf->hwtable, DI_PRIO);
168 get_dm_mpvec (vector curmp, vector pathvec, char * refwwid)
171 struct multipath * mpp;
172 char params[PARAMS_SIZE], status[PARAMS_SIZE];
174 if (dm_get_maps(curmp))
177 vector_foreach_slot (curmp, mpp, i) {
179 * discard out of scope maps
181 if (mpp->wwid && refwwid &&
182 strncmp(mpp->wwid, refwwid, WWID_SIZE)) {
183 condlog(3, "skip map %s: out of scope", mpp->alias);
184 free_multipath(mpp, KEEP_PATHS);
185 vector_del_slot(curmp, i);
190 dm_get_map(mpp->alias, &mpp->size, params);
191 condlog(3, "params = %s", params);
192 dm_get_status(mpp->alias, status);
193 condlog(3, "status = %s", status);
195 disassemble_map(pathvec, params, mpp);
198 * disassemble_map() can add new paths to pathvec.
199 * If not in "fast list mode", we need to fetch information
206 mpp->bestpg = select_path_group(mpp);
208 disassemble_status(status, mpp);
211 print_multipath_topology(mpp, conf->verbosity);
214 reinstate_paths(mpp);
215 remember_wwid(mpp->wwid);
231 vector pathvec = NULL;
235 char * refwwid = NULL;
239 * allocate core vectors to store paths and multipaths
241 curmp = vector_alloc();
242 pathvec = vector_alloc();
244 if (!curmp || !pathvec) {
245 condlog(0, "can not allocate memory");
248 vecs.pathvec = pathvec;
252 * dev is "/dev/" . "sysfs block dev"
255 if (!strncmp(conf->dev, "/dev/", 5) &&
256 strlen(conf->dev) > 5)
263 * if we have a blacklisted device parameter, exit early
265 if (dev && conf->dev_type == DEV_DEVNODE &&
266 (filter_devnode(conf->blist_devnode,
267 conf->elist_devnode, dev) > 0)) {
268 if (conf->dry_run == 2)
269 printf("%s is not a valid multipath device path\n",
274 * scope limiting must be translated into a wwid
275 * failing the translation is fatal (by policy)
278 int failed = get_refwwid(conf->dev, conf->dev_type, pathvec,
281 if (failed == 2 && conf->dry_run == 2)
282 printf("%s is not a valid multipath device path\n", conf->dev);
284 condlog(3, "scope is nul");
287 condlog(3, "scope limited to %s", refwwid);
288 if (conf->dry_run == 2) {
289 if (check_wwids_file(refwwid, 0) == 0){
290 printf("%s is a valid multipath device path\n", conf->dev);
294 printf("%s is not a valid multipath device path\n", conf->dev);
306 /* extended path info '-ll' */
307 di_flag |= DI_SYSFS | DI_CHECKER;
309 /* minimum path info '-l' */
315 if (path_discovery(pathvec, conf, di_flag))
318 if (conf->verbosity > 2)
319 print_all_paths(pathvec, 1);
321 get_path_layout(pathvec, 0);
323 if (get_dm_mpvec(curmp, pathvec, refwwid))
326 filter_pathvec(pathvec, refwwid);
334 * core logic entry point
336 r = coalesce_paths(&vecs, NULL, NULL, conf->force_reload);
342 free_multipathvec(curmp, KEEP_PATHS);
343 free_pathvec(pathvec, FREE_PATHS);
353 unsigned int maxlen = 256;
356 reply = MALLOC(maxlen);
362 c += snprint_defaults(c, reply + maxlen - c);
363 again = ((c - reply) == maxlen);
365 reply = REALLOC(reply, maxlen *= 2);
368 c += snprint_blacklist(c, reply + maxlen - c);
369 again = ((c - reply) == maxlen);
371 reply = REALLOC(reply, maxlen *= 2);
374 c += snprint_blacklist_except(c, reply + maxlen - c);
375 again = ((c - reply) == maxlen);
377 reply = REALLOC(reply, maxlen *= 2);
380 c += snprint_hwtable(c, reply + maxlen - c, conf->hwtable);
381 again = ((c - reply) == maxlen);
383 reply = REALLOC(reply, maxlen *= 2);
386 c += snprint_mptable(c, reply + maxlen - c, conf->mptable);
387 again = ((c - reply) == maxlen);
389 reply = REALLOC(reply, maxlen *= 2);
398 get_dev_type(char *dev) {
402 if (stat(dev, &buf) == 0 && S_ISBLK(buf.st_mode)) {
403 if (dm_is_dm_major(MAJOR(buf.st_rdev)))
407 else if (sscanf(dev, "%d:%d", &i, &i) == 2)
414 convert_dev(char *dev)
416 char *ptr = strstr(dev, "cciss/");
424 main (int argc, char *argv[])
432 fprintf(stderr, "need to be root\n");
439 if (load_config(DEFAULT_CONFIGFILE))
442 while ((arg = getopt(argc, argv, ":dchl::FfM:v:p:b:Brtq")) != EOF ) {
444 case 1: printf("optarg : %s\n",optarg);
447 if (sizeof(optarg) > sizeof(char *) ||
448 !isdigit(optarg[0])) {
453 conf->verbosity = atoi(optarg);
456 conf->bindings_file = strdup(optarg);
459 conf->bindings_read_only = 1;
462 conf->allow_queueing = 1;
472 conf->remove = FLUSH_ONE;
475 conf->remove = FLUSH_ALL;
481 if (optarg && !strncmp(optarg, "l", 1))
487 debug = atoi(optarg);
491 conf->pgpolicy_flag = get_pgpolicy_id(optarg);
492 if (conf->pgpolicy_flag == -1) {
493 printf("'%s' is not a valid policy\n", optarg);
499 conf->force_reload = 1;
508 fprintf(stderr, "Missing option argument\n");
512 fprintf(stderr, "Unknown switch: %s\n", optarg);
521 conf->dev = MALLOC(FILE_NAME_SIZE);
526 strncpy(conf->dev, argv[optind], FILE_NAME_SIZE);
527 conf->dev_type = get_dev_type(conf->dev);
528 if (conf->dev_type == DEV_DEVNODE)
529 convert_dev(conf->dev);
534 struct rlimit fd_limit;
536 fd_limit.rlim_cur = conf->max_fds;
537 fd_limit.rlim_max = conf->max_fds;
538 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0)
539 condlog(0, "can't set open fds limit to %d : %s",
540 conf->max_fds, strerror(errno));
543 if (init_checkers()) {
544 condlog(0, "failed to initialize checkers");
548 condlog(0, "failed to initialize prioritizers");
553 if (conf->dry_run == 2 &&
554 (!conf->dev || conf->dev_type == DEV_DEVMAP)) {
555 condlog(0, "the -c option requires a path to check");
558 if (conf->remove == FLUSH_ONE) {
559 if (conf->dev_type == DEV_DEVMAP) {
560 r = dm_suspend_and_flush_map(conf->dev);
562 condlog(0, "must provide a map name to remove");
566 else if (conf->remove == FLUSH_ALL) {
570 while ((r = configure()) < 0)
571 condlog(3, "restart multipath configuration process");
574 udev_wait(conf->cookie);
582 * Freeing config must be done after dm_lib_exit(), because
583 * the logging function (dm_write_log()), which is called there,
584 * references the config.
590 dbg_free_final(NULL);