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|-w|-W] [-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 " -w remove a device from the wwids file\n" \
108 " -W reset the wwids file include only the current devices\n" \
109 " -p pol force all maps to specified path grouping policy :\n" \
110 " . failover one path per priority group\n" \
111 " . multibus all paths in one priority group\n" \
112 " . group_by_serial one priority group per serial\n" \
113 " . group_by_prio one priority group per priority lvl\n" \
114 " . group_by_node_name one priority group per target node\n" \
115 " -v lvl verbosity level\n" \
117 " . 1 print created devmap names only\n" \
118 " . 2 default verbosity\n" \
119 " . 3 print debug information\n" \
120 " dev action limited to:\n" \
121 " . multipath named 'dev' (ex: mpath0) or\n" \
122 " . multipath whose wwid is 'dev' (ex: 60051..)\n" \
123 " . multipath including the path named 'dev' (ex: /dev/sda)\n" \
124 " . multipath including the path with maj:min 'dev' (ex: 8:0)\n" \
130 update_paths (struct multipath * mpp)
133 struct pathgroup * pgp;
139 vector_foreach_slot (mpp->pg, pgp, i) {
143 vector_foreach_slot (pgp->paths, pp, j) {
144 if (!strlen(pp->dev)) {
145 if (devt2devname(pp->dev, FILE_NAME_SIZE,
148 * path is not in sysfs anymore
150 pp->chkrstate = pp->state = PATH_DOWN;
154 pathinfo(pp, conf->hwtable, DI_ALL);
158 if (pp->state == PATH_UNCHECKED ||
159 pp->state == PATH_WILD)
160 pathinfo(pp, conf->hwtable, DI_CHECKER);
162 if (pp->priority == PRIO_UNDEF)
163 pathinfo(pp, conf->hwtable, DI_PRIO);
170 get_dm_mpvec (vector curmp, vector pathvec, char * refwwid)
173 struct multipath * mpp;
174 char params[PARAMS_SIZE], status[PARAMS_SIZE];
176 if (dm_get_maps(curmp))
179 vector_foreach_slot (curmp, mpp, i) {
181 * discard out of scope maps
183 if (mpp->wwid && refwwid &&
184 strncmp(mpp->wwid, refwwid, WWID_SIZE)) {
185 condlog(3, "skip map %s: out of scope", mpp->alias);
186 free_multipath(mpp, KEEP_PATHS);
187 vector_del_slot(curmp, i);
192 dm_get_map(mpp->alias, &mpp->size, params);
193 condlog(3, "params = %s", params);
194 dm_get_status(mpp->alias, status);
195 condlog(3, "status = %s", status);
197 disassemble_map(pathvec, params, mpp);
200 * disassemble_map() can add new paths to pathvec.
201 * If not in "fast list mode", we need to fetch information
208 mpp->bestpg = select_path_group(mpp);
210 disassemble_status(status, mpp);
213 print_multipath_topology(mpp, conf->verbosity);
216 reinstate_paths(mpp);
232 vector pathvec = NULL;
236 char * refwwid = NULL;
240 * allocate core vectors to store paths and multipaths
242 curmp = vector_alloc();
243 pathvec = vector_alloc();
245 if (!curmp || !pathvec) {
246 condlog(0, "can not allocate memory");
249 vecs.pathvec = pathvec;
253 * dev is "/dev/" . "sysfs block dev"
256 if (!strncmp(conf->dev, "/dev/", 5) &&
257 strlen(conf->dev) > 5)
264 * if we have a blacklisted device parameter, exit early
266 if (dev && conf->dev_type == DEV_DEVNODE && conf->dry_run != 3 &&
267 (filter_devnode(conf->blist_devnode,
268 conf->elist_devnode, dev) > 0)) {
269 if (conf->dry_run == 2)
270 printf("%s is not a valid multipath device path\n",
275 * scope limiting must be translated into a wwid
276 * failing the translation is fatal (by policy)
279 int failed = get_refwwid(conf->dev, conf->dev_type, pathvec,
282 if (failed == 2 && conf->dry_run == 2)
283 printf("%s is not a valid multipath device path\n", conf->dev);
285 condlog(3, "scope is nul");
288 if (conf->dry_run == 3) {
289 r = remove_wwid(refwwid);
291 printf("wwid '%s' removed\n", refwwid);
293 printf("wwid '%s' not in wwids file\n",
299 condlog(3, "scope limited to %s", refwwid);
300 if (conf->dry_run == 2) {
301 if (check_wwids_file(refwwid, 0) == 0){
302 printf("%s is a valid multipath device path\n", conf->dev);
306 printf("%s is not a valid multipath device path\n", conf->dev);
318 /* extended path info '-ll' */
319 di_flag |= DI_SYSFS | DI_CHECKER;
321 /* minimum path info '-l' */
327 if (path_discovery(pathvec, conf, di_flag))
330 if (conf->verbosity > 2)
331 print_all_paths(pathvec, 1);
333 get_path_layout(pathvec, 0);
335 if (get_dm_mpvec(curmp, pathvec, refwwid))
338 filter_pathvec(pathvec, refwwid);
346 * core logic entry point
348 r = coalesce_paths(&vecs, NULL, NULL, conf->force_reload);
354 free_multipathvec(curmp, KEEP_PATHS);
355 free_pathvec(pathvec, FREE_PATHS);
365 unsigned int maxlen = 256;
368 reply = MALLOC(maxlen);
374 c += snprint_defaults(c, reply + maxlen - c);
375 again = ((c - reply) == maxlen);
377 reply = REALLOC(reply, maxlen *= 2);
380 c += snprint_blacklist(c, reply + maxlen - c);
381 again = ((c - reply) == maxlen);
383 reply = REALLOC(reply, maxlen *= 2);
386 c += snprint_blacklist_except(c, reply + maxlen - c);
387 again = ((c - reply) == maxlen);
389 reply = REALLOC(reply, maxlen *= 2);
392 c += snprint_hwtable(c, reply + maxlen - c, conf->hwtable);
393 again = ((c - reply) == maxlen);
395 reply = REALLOC(reply, maxlen *= 2);
398 c += snprint_mptable(c, reply + maxlen - c, conf->mptable);
399 again = ((c - reply) == maxlen);
401 reply = REALLOC(reply, maxlen *= 2);
410 get_dev_type(char *dev) {
414 if (stat(dev, &buf) == 0 && S_ISBLK(buf.st_mode)) {
415 if (dm_is_dm_major(MAJOR(buf.st_rdev)))
419 else if (sscanf(dev, "%d:%d", &i, &i) == 2)
426 convert_dev(char *dev)
428 char *ptr = strstr(dev, "cciss/");
436 main (int argc, char *argv[])
444 fprintf(stderr, "need to be root\n");
451 if (load_config(DEFAULT_CONFIGFILE))
454 while ((arg = getopt(argc, argv, ":dchl::FfM:v:p:b:BrtqwW")) != EOF ) {
456 case 1: printf("optarg : %s\n",optarg);
459 if (sizeof(optarg) > sizeof(char *) ||
460 !isdigit(optarg[0])) {
465 conf->verbosity = atoi(optarg);
468 conf->bindings_file = strdup(optarg);
471 conf->bindings_read_only = 1;
474 conf->allow_queueing = 1;
484 conf->remove = FLUSH_ONE;
487 conf->remove = FLUSH_ALL;
493 if (optarg && !strncmp(optarg, "l", 1))
499 debug = atoi(optarg);
503 conf->pgpolicy_flag = get_pgpolicy_id(optarg);
504 if (conf->pgpolicy_flag == -1) {
505 printf("'%s' is not a valid policy\n", optarg);
511 conf->force_reload = 1;
526 fprintf(stderr, "Missing option argument\n");
530 fprintf(stderr, "Unknown switch: %s\n", optarg);
539 conf->dev = MALLOC(FILE_NAME_SIZE);
544 strncpy(conf->dev, argv[optind], FILE_NAME_SIZE);
545 conf->dev_type = get_dev_type(conf->dev);
546 if (conf->dev_type == DEV_DEVNODE)
547 convert_dev(conf->dev);
552 struct rlimit fd_limit;
554 fd_limit.rlim_cur = conf->max_fds;
555 fd_limit.rlim_max = conf->max_fds;
556 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0)
557 condlog(0, "can't set open fds limit to %d : %s",
558 conf->max_fds, strerror(errno));
561 if (init_checkers()) {
562 condlog(0, "failed to initialize checkers");
566 condlog(0, "failed to initialize prioritizers");
571 if (conf->dry_run == 2 &&
572 (!conf->dev || conf->dev_type == DEV_DEVMAP)) {
573 condlog(0, "the -c option requires a path to check");
576 if (conf->dry_run == 3 && !conf->dev) {
577 condlog(0, "the -w option requires a device");
580 if (conf->dry_run == 4) {
581 struct multipath * mpp;
585 curmp = vector_alloc();
587 condlog(0, "can't allocate memory for mp list");
590 if (dm_get_maps(curmp) == 0)
591 r = replace_wwids(curmp);
593 printf("successfully reset wwids\n");
594 vector_foreach_slot_backwards(curmp, mpp, i) {
595 vector_del_slot(curmp, i);
596 free_multipath(mpp, KEEP_PATHS);
601 if (conf->remove == FLUSH_ONE) {
602 if (conf->dev_type == DEV_DEVMAP) {
603 r = dm_suspend_and_flush_map(conf->dev);
605 condlog(0, "must provide a map name to remove");
609 else if (conf->remove == FLUSH_ALL) {
613 while ((r = configure()) < 0)
614 condlog(3, "restart multipath configuration process");
617 udev_wait(conf->cookie);
625 * Freeing config must be done after dm_lib_exit(), because
626 * the logging function (dm_write_log()), which is called there,
627 * references the config.
633 dbg_free_final(NULL);