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>
61 filter_pathvec (vector pathvec, char * refwwid)
66 if (!refwwid || !strlen(refwwid))
69 vector_foreach_slot (pathvec, pp, i) {
70 if (strncmp(pp->wwid, refwwid, WWID_SIZE) != 0) {
71 condlog(3, "skip path %s : out of scope", pp->dev);
73 vector_del_slot(pathvec, i);
81 usage (char * progname)
83 fprintf (stderr, VERSION_STRING);
84 fprintf (stderr, "Usage:\n");
85 fprintf (stderr, " %s [-d] [-r] [-v lvl] [-p pol] [-b fil] [-q] [dev]\n", progname);
86 fprintf (stderr, " %s -l|-ll|-f [-v lvl] [-b fil] [dev]\n", progname);
87 fprintf (stderr, " %s -F [-v lvl]\n", progname);
88 fprintf (stderr, " %s -t\n", progname);
89 fprintf (stderr, " %s -h\n", progname);
93 " -h print this usage text\n" \
94 " -l show multipath topology (sysfs and DM info)\n" \
95 " -ll show multipath topology (maximum info)\n" \
96 " -f flush a multipath device map\n" \
97 " -F flush all multipath device maps\n" \
98 " -q allow queue_if_no_path when multipathd is not running\n"\
99 " -d dry run, do not create or update devmaps\n" \
100 " -t dump internal hardware table\n" \
101 " -r force devmap reload\n" \
102 " -p policy failover|multibus|group_by_serial|group_by_prio\n" \
103 " -b fil bindings file location\n" \
104 " -p pol force all maps to specified path grouping policy :\n" \
105 " . failover one path per priority group\n" \
106 " . multibus all paths in one priority group\n" \
107 " . group_by_serial one priority group per serial\n" \
108 " . group_by_prio one priority group per priority lvl\n" \
109 " . group_by_node_name one priority group per target node\n" \
110 " -v lvl verbosity level\n" \
112 " . 1 print created devmap names only\n" \
113 " . 2 default verbosity\n" \
114 " . 3 print debug information\n" \
115 " dev action limited to:\n" \
116 " . multipath named 'dev' (ex: mpath0) or\n" \
117 " . multipath whose wwid is 'dev' (ex: 60051..)\n" \
118 " . multipath including the path named 'dev' (ex: /dev/sda)\n" \
119 " . multipath including the path with maj:min 'dev' (ex: 8:0)\n" \
125 update_paths (struct multipath * mpp)
128 struct pathgroup * pgp;
134 vector_foreach_slot (mpp->pg, pgp, i) {
138 vector_foreach_slot (pgp->paths, pp, j) {
139 if (!strlen(pp->dev)) {
140 if (devt2devname(pp->dev, FILE_NAME_SIZE,
143 * path is not in sysfs anymore
145 pp->state = PATH_DOWN;
149 pathinfo(pp, conf->hwtable, DI_ALL);
153 if (pp->state == PATH_UNCHECKED ||
154 pp->state == PATH_WILD)
155 pathinfo(pp, conf->hwtable, DI_CHECKER);
157 if (pp->priority == PRIO_UNDEF)
158 pathinfo(pp, conf->hwtable, DI_PRIO);
165 get_dm_mpvec (vector curmp, vector pathvec, char * refwwid)
168 struct multipath * mpp;
169 char params[PARAMS_SIZE], status[PARAMS_SIZE];
171 if (dm_get_maps(curmp))
174 vector_foreach_slot (curmp, mpp, i) {
176 * discard out of scope maps
178 if (mpp->wwid && refwwid &&
179 strncmp(mpp->wwid, refwwid, WWID_SIZE)) {
180 condlog(3, "skip map %s: out of scope", mpp->alias);
181 free_multipath(mpp, KEEP_PATHS);
182 vector_del_slot(curmp, i);
187 dm_get_map(mpp->alias, &mpp->size, params);
188 condlog(3, "params = %s", params);
189 dm_get_status(mpp->alias, status);
190 condlog(3, "status = %s", status);
192 disassemble_map(pathvec, params, mpp);
195 * disassemble_map() can add new paths to pathvec.
196 * If not in "fast list mode", we need to fetch information
203 mpp->bestpg = select_path_group(mpp);
205 disassemble_status(status, mpp);
208 print_multipath_topology(mpp, conf->verbosity);
211 reinstate_paths(mpp);
227 vector pathvec = NULL;
231 char * refwwid = NULL;
235 * allocate core vectors to store paths and multipaths
237 curmp = vector_alloc();
238 pathvec = vector_alloc();
240 if (!curmp || !pathvec) {
241 condlog(0, "can not allocate memory");
244 vecs.pathvec = pathvec;
248 * dev is "/dev/" . "sysfs block dev"
251 if (!strncmp(conf->dev, "/dev/", 5) &&
252 strlen(conf->dev) > 5)
259 * if we have a blacklisted device parameter, exit early
262 (filter_devnode(conf->blist_devnode, conf->elist_devnode, dev) > 0))
266 * scope limiting must be translated into a wwid
267 * failing the translation is fatal (by policy)
270 refwwid = get_refwwid(conf->dev, conf->dev_type, pathvec);
273 condlog(3, "scope is nul");
276 condlog(3, "scope limited to %s", refwwid);
277 if (filter_wwid(conf->blist_wwid, conf->elist_wwid,
289 /* extended path info '-ll' */
290 di_flag |= DI_SYSFS | DI_CHECKER;
292 /* minimum path info '-l' */
298 if (path_discovery(pathvec, conf, di_flag))
301 if (conf->verbosity > 2)
302 print_all_paths(pathvec, 1);
304 get_path_layout(pathvec, 0);
306 if (get_dm_mpvec(curmp, pathvec, refwwid))
309 filter_pathvec(pathvec, refwwid);
317 * core logic entry point
319 r = coalesce_paths(&vecs, NULL, NULL, conf->force_reload);
325 free_multipathvec(curmp, KEEP_PATHS);
326 free_pathvec(pathvec, FREE_PATHS);
336 unsigned int maxlen = 256;
339 reply = MALLOC(maxlen);
345 c += snprint_defaults(c, reply + maxlen - c);
346 again = ((c - reply) == maxlen);
348 reply = REALLOC(reply, maxlen *= 2);
351 c += snprint_blacklist(c, reply + maxlen - c);
352 again = ((c - reply) == maxlen);
354 reply = REALLOC(reply, maxlen *= 2);
357 c += snprint_blacklist_except(c, reply + maxlen - c);
358 again = ((c - reply) == maxlen);
360 reply = REALLOC(reply, maxlen *= 2);
363 c += snprint_hwtable(c, reply + maxlen - c, conf->hwtable);
364 again = ((c - reply) == maxlen);
366 reply = REALLOC(reply, maxlen *= 2);
369 c += snprint_mptable(c, reply + maxlen - c, conf->mptable);
370 again = ((c - reply) == maxlen);
372 reply = REALLOC(reply, maxlen *= 2);
381 get_dev_type(char *dev) {
385 if (stat(dev, &buf) == 0 && S_ISBLK(buf.st_mode)) {
386 if (dm_is_dm_major(MAJOR(buf.st_rdev)))
390 else if (sscanf(dev, "%d:%d", &i, &i) == 2)
397 main (int argc, char *argv[])
405 fprintf(stderr, "need to be root\n");
412 if (load_config(DEFAULT_CONFIGFILE))
415 if (sysfs_init(conf->sysfs_dir, FILE_NAME_SIZE)) {
416 condlog(0, "multipath tools need sysfs mounted");
419 while ((arg = getopt(argc, argv, ":dhl::FfM:v:p:b:Brtq")) != EOF ) {
421 case 1: printf("optarg : %s\n",optarg);
424 if (sizeof(optarg) > sizeof(char *) ||
425 !isdigit(optarg[0])) {
430 conf->verbosity = atoi(optarg);
433 conf->bindings_file = strdup(optarg);
436 conf->bindings_read_only = 1;
439 conf->allow_queueing = 1;
445 conf->remove = FLUSH_ONE;
448 conf->remove = FLUSH_ALL;
454 if (optarg && !strncmp(optarg, "l", 1))
460 debug = atoi(optarg);
464 conf->pgpolicy_flag = get_pgpolicy_id(optarg);
465 if (conf->pgpolicy_flag == -1) {
466 printf("'%s' is not a valid policy\n", optarg);
472 conf->force_reload = 1;
481 fprintf(stderr, "Missing option arguement\n");
485 fprintf(stderr, "Unknown switch: %s\n", optarg);
494 conf->dev = MALLOC(FILE_NAME_SIZE);
499 strncpy(conf->dev, argv[optind], FILE_NAME_SIZE);
500 conf->dev_type = get_dev_type(conf->dev);
505 struct rlimit fd_limit;
507 fd_limit.rlim_cur = conf->max_fds;
508 fd_limit.rlim_max = conf->max_fds;
509 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0)
510 condlog(0, "can't set open fds limit to %d : %s\n",
511 conf->max_fds, strerror(errno));
514 if (init_checkers()) {
515 condlog(0, "failed to initialize checkers");
519 condlog(0, "failed to initialize prioritizers");
524 if (conf->remove == FLUSH_ONE) {
525 if (conf->dev_type == DEV_DEVMAP)
526 r = dm_flush_map(conf->dev);
528 condlog(0, "must provide a map name to remove");
532 else if (conf->remove == FLUSH_ALL) {
536 while ((r = configure()) < 0)
537 condlog(3, "restart multipath configuration process");
540 udev_wait(conf->cookie);
548 * Freeing config must be done after dm_lib_exit(), because
549 * the logging function (dm_write_log()), which is called there,
550 * references the config.
556 dbg_free_final(NULL);