40b7a7682dfb2f3d50ad5bda6d2f259b80952f39
[multipath-tools/.git] / multipath / main.c
1 /*
2  * Soft:        multipath device mapper target autoconfig
3  *
4  * Version:     $Id: main.h,v 0.0.1 2003/09/18 15:13:38 cvaroqui Exp $
5  *
6  * Author:      Christophe Varoqui
7  *
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.
12  *
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.
17  *
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
23  */
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <ctype.h>
30 #include <libudev.h>
31 #include <syslog.h>
32
33 #include <checkers.h>
34 #include <prio.h>
35 #include <vector.h>
36 #include <memory.h>
37 #include <libdevmapper.h>
38 #include <devmapper.h>
39 #include <util.h>
40 #include <defaults.h>
41 #include <config.h>
42 #include <structs.h>
43 #include <structs_vec.h>
44 #include <dmparser.h>
45 #include <sysfs.h>
46 #include <blacklist.h>
47 #include <discovery.h>
48 #include <debug.h>
49 #include <switchgroup.h>
50 #include <print.h>
51 #include <alias.h>
52 #include <configure.h>
53 #include <pgpolicies.h>
54 #include <version.h>
55 #include <errno.h>
56 #include <sys/time.h>
57 #include <sys/resource.h>
58 #include <wwids.h>
59 #include <uxsock.h>
60 #include <mpath_cmd.h>
61
62 int logsink;
63 struct udev *udev;
64 struct config *multipath_conf;
65
66 struct config *get_multipath_config(void)
67 {
68         return multipath_conf;
69 }
70
71 void put_multipath_config(struct config *conf)
72 {
73         /* Noop for now */
74 }
75
76 static int
77 filter_pathvec (vector pathvec, char * refwwid)
78 {
79         int i;
80         struct path * pp;
81
82         if (!refwwid || !strlen(refwwid))
83                 return 0;
84
85         vector_foreach_slot (pathvec, pp, i) {
86                 if (strncmp(pp->wwid, refwwid, WWID_SIZE) != 0) {
87                         condlog(3, "skip path %s : out of scope", pp->dev);
88                         free_path(pp);
89                         vector_del_slot(pathvec, i);
90                         i--;
91                 }
92         }
93         return 0;
94 }
95
96 static void
97 usage (char * progname)
98 {
99         fprintf (stderr, VERSION_STRING);
100         fprintf (stderr, "Usage:\n");
101         fprintf (stderr, "  %s [-a|-c|-w|-W] [-d] [-r] [-i] [-v lvl] [-p pol] [-b fil] [-q] [dev]\n", progname);
102         fprintf (stderr, "  %s -l|-ll|-f [-v lvl] [-b fil] [dev]\n", progname);
103         fprintf (stderr, "  %s -F [-v lvl]\n", progname);
104         fprintf (stderr, "  %s -t\n", progname);
105         fprintf (stderr, "  %s -h\n", progname);
106         fprintf (stderr,
107                 "\n"
108                 "Where:\n"
109                 "  -h      print this usage text\n" \
110                 "  -l      show multipath topology (sysfs and DM info)\n" \
111                 "  -ll     show multipath topology (maximum info)\n" \
112                 "  -f      flush a multipath device map\n" \
113                 "  -F      flush all multipath device maps\n" \
114                 "  -a      add a device wwid to the wwids file\n" \
115                 "  -c      check if a device should be a path in a multipath device\n" \
116                 "  -q      allow queue_if_no_path when multipathd is not running\n"\
117                 "  -d      dry run, do not create or update devmaps\n" \
118                 "  -t      dump internal hardware table\n" \
119                 "  -r      force devmap reload\n" \
120                 "  -i      ignore wwids file\n" \
121                 "  -B      treat the bindings file as read only\n" \
122                 "  -b fil  bindings file location\n" \
123                 "  -w      remove a device from the wwids file\n" \
124                 "  -W      reset the wwids file include only the current devices\n" \
125                 "  -p pol  force all maps to specified path grouping policy :\n" \
126                 "          . failover            one path per priority group\n" \
127                 "          . multibus            all paths in one priority group\n" \
128                 "          . group_by_serial     one priority group per serial\n" \
129                 "          . group_by_prio       one priority group per priority lvl\n" \
130                 "          . group_by_node_name  one priority group per target node\n" \
131                 "  -v lvl  verbosity level\n" \
132                 "          . 0 no output\n" \
133                 "          . 1 print created devmap names only\n" \
134                 "          . 2 default verbosity\n" \
135                 "          . 3 print debug information\n" \
136                 "  dev     action limited to:\n" \
137                 "          . multipath named 'dev' (ex: mpath0) or\n" \
138                 "          . multipath whose wwid is 'dev' (ex: 60051..)\n" \
139                 "          . multipath including the path named 'dev' (ex: /dev/sda)\n" \
140                 "          . multipath including the path with maj:min 'dev' (ex: 8:0)\n" \
141                 );
142
143 }
144
145 static int
146 update_paths (struct multipath * mpp)
147 {
148         int i, j;
149         struct pathgroup * pgp;
150         struct path * pp;
151
152         if (!mpp->pg)
153                 return 0;
154
155         vector_foreach_slot (mpp->pg, pgp, i) {
156                 if (!pgp->paths)
157                         continue;
158
159                 vector_foreach_slot (pgp->paths, pp, j) {
160                         if (!strlen(pp->dev)) {
161                                 if (devt2devname(pp->dev, FILE_NAME_SIZE,
162                                                  pp->dev_t)) {
163                                         /*
164                                          * path is not in sysfs anymore
165                                          */
166                                         pp->chkrstate = pp->state = PATH_DOWN;
167                                         continue;
168                                 }
169                                 pp->mpp = mpp;
170                                 if (pathinfo(pp, conf, DI_ALL))
171                                         pp->state = PATH_UNCHECKED;
172                                 continue;
173                         }
174                         pp->mpp = mpp;
175                         if (pp->state == PATH_UNCHECKED ||
176                             pp->state == PATH_WILD) {
177                                 if (pathinfo(pp, conf, DI_CHECKER))
178                                         pp->state = PATH_UNCHECKED;
179                         }
180
181                         if (pp->priority == PRIO_UNDEF) {
182                                 if (pathinfo(pp, conf, DI_PRIO))
183                                         pp->priority = PRIO_UNDEF;
184                         }
185                 }
186         }
187         return 0;
188 }
189
190 static int
191 get_dm_mpvec (enum mpath_cmds cmd, vector curmp, vector pathvec, char * refwwid)
192 {
193         int i;
194         struct multipath * mpp;
195         char params[PARAMS_SIZE], status[PARAMS_SIZE];
196
197         if (dm_get_maps(curmp))
198                 return 1;
199
200         vector_foreach_slot (curmp, mpp, i) {
201                 /*
202                  * discard out of scope maps
203                  */
204                 if (mpp->wwid && refwwid &&
205                     strncmp(mpp->wwid, refwwid, WWID_SIZE)) {
206                         condlog(3, "skip map %s: out of scope", mpp->alias);
207                         free_multipath(mpp, KEEP_PATHS);
208                         vector_del_slot(curmp, i);
209                         i--;
210                         continue;
211                 }
212
213                 if (cmd == CMD_VALID_PATH)
214                         continue;
215
216                 dm_get_map(mpp->alias, &mpp->size, params);
217                 condlog(3, "params = %s", params);
218                 dm_get_status(mpp->alias, status);
219                 condlog(3, "status = %s", status);
220
221                 disassemble_map(pathvec, params, mpp, 0);
222
223                 /*
224                  * disassemble_map() can add new paths to pathvec.
225                  * If not in "fast list mode", we need to fetch information
226                  * about them
227                  */
228                 if (cmd != CMD_LIST_SHORT)
229                         update_paths(mpp);
230
231                 if (cmd == CMD_LIST_LONG)
232                         mpp->bestpg = select_path_group(mpp);
233
234                 disassemble_status(status, mpp);
235
236                 if (cmd == CMD_LIST_SHORT ||
237                     cmd == CMD_LIST_LONG)
238                         print_multipath_topology(mpp, conf->verbosity);
239
240                 if (cmd == CMD_CREATE)
241                         reinstate_paths(mpp);
242         }
243         return 0;
244 }
245
246
247 /*
248  * Return value:
249  *  -1: Retry
250  *   0: Success
251  *   1: Failure
252  */
253 static int
254 configure (enum mpath_cmds cmd, enum devtypes dev_type, char *devpath)
255 {
256         vector curmp = NULL;
257         vector pathvec = NULL;
258         struct vectors vecs;
259         int r = 1;
260         int di_flag = 0;
261         char * refwwid = NULL;
262         char * dev = NULL;
263
264         /*
265          * allocate core vectors to store paths and multipaths
266          */
267         curmp = vector_alloc();
268         pathvec = vector_alloc();
269
270         if (!curmp || !pathvec) {
271                 condlog(0, "can not allocate memory");
272                 goto out;
273         }
274         vecs.pathvec = pathvec;
275         vecs.mpvec = curmp;
276
277         dev = convert_dev(devpath, (dev_type == DEV_DEVNODE));
278
279         /*
280          * if we have a blacklisted device parameter, exit early
281          */
282         if (dev && dev_type == DEV_DEVNODE &&
283             cmd != CMD_REMOVE_WWID &&
284             (filter_devnode(conf->blist_devnode,
285                             conf->elist_devnode, dev) > 0)) {
286                 if (cmd == CMD_VALID_PATH)
287                         printf("%s is not a valid multipath device path\n",
288                                devpath);
289                 goto out;
290         }
291         /*
292          * scope limiting must be translated into a wwid
293          * failing the translation is fatal (by policy)
294          */
295         if (devpath) {
296                 int failed = get_refwwid(cmd, devpath, dev_type,
297                                          pathvec, &refwwid);
298                 if (!refwwid) {
299                         condlog(4, "%s: failed to get wwid", devpath);
300                         if (failed == 2 && cmd == CMD_VALID_PATH)
301                                 printf("%s is not a valid multipath device path\n", devpath);
302                         else
303                                 condlog(3, "scope is nul");
304                         goto out;
305                 }
306                 if (cmd == CMD_REMOVE_WWID) {
307                         r = remove_wwid(refwwid);
308                         if (r == 0)
309                                 printf("wwid '%s' removed\n", refwwid);
310                         else if (r == 1) {
311                                 printf("wwid '%s' not in wwids file\n",
312                                         refwwid);
313                                 r = 0;
314                         }
315                         goto out;
316                 }
317                 if (cmd == CMD_ADD_WWID) {
318                         r = remember_wwid(refwwid);
319                         if (r == 0)
320                                 printf("wwid '%s' added\n", refwwid);
321                         else
322                                 printf("failed adding '%s' to wwids file\n",
323                                        refwwid);
324                         goto out;
325                 }
326                 condlog(3, "scope limited to %s", refwwid);
327                 /* If you are ignoring the wwids file and find_multipaths is
328                  * set, you need to actually check if there are two available
329                  * paths to determine if this path should be multipathed. To
330                  * do this, we put off the check until after discovering all
331                  * the paths */
332                 if (cmd == CMD_VALID_PATH &&
333                     (!conf->find_multipaths || !conf->ignore_wwids)) {
334                         if (conf->ignore_wwids ||
335                             check_wwids_file(refwwid, 0) == 0)
336                                 r = 0;
337
338                         printf("%s %s a valid multipath device path\n",
339                                devpath, r == 0 ? "is" : "is not");
340                         goto out;
341                 }
342         }
343
344         /*
345          * get a path list
346          */
347         if (devpath)
348                 di_flag = DI_WWID;
349
350         if (cmd == CMD_LIST_LONG)
351                 /* extended path info '-ll' */
352                 di_flag |= DI_SYSFS | DI_CHECKER;
353         else if (cmd == CMD_LIST_SHORT)
354                 /* minimum path info '-l' */
355                 di_flag |= DI_SYSFS;
356         else
357                 /* maximum info */
358                 di_flag = DI_ALL;
359
360         if (path_discovery(pathvec, di_flag) < 0)
361                 goto out;
362
363         if (conf->verbosity > 2)
364                 print_all_paths(pathvec, 1);
365
366         get_path_layout(pathvec, 0);
367
368         if (get_dm_mpvec(cmd, curmp, pathvec, refwwid))
369                 goto out;
370
371         filter_pathvec(pathvec, refwwid);
372
373
374         if (cmd == CMD_VALID_PATH) {
375                 /* This only happens if find_multipaths is and
376                  * ignore_wwids is set.
377                  * If there is currently a multipath device matching
378                  * the refwwid, or there is more than one path matching
379                  * the refwwid, then the path is valid */
380                 if (VECTOR_SIZE(curmp) != 0 || VECTOR_SIZE(pathvec) > 1)
381                         r = 0;
382                 printf("%s %s a valid multipath device path\n",
383                        devpath, r == 0 ? "is" : "is not");
384                 goto out;
385         }
386
387         if (cmd != CMD_CREATE && cmd != CMD_DRY_RUN) {
388                 r = 0;
389                 goto out;
390         }
391
392         /*
393          * core logic entry point
394          */
395         r = coalesce_paths(&vecs, NULL, refwwid,
396                            conf->force_reload, cmd);
397
398 out:
399         if (refwwid)
400                 FREE(refwwid);
401
402         free_multipathvec(curmp, KEEP_PATHS);
403         free_pathvec(pathvec, FREE_PATHS);
404
405         return r;
406 }
407
408 static int
409 dump_config (struct config *conf)
410 {
411         char * c, * tmp = NULL;
412         char * reply;
413         unsigned int maxlen = 256;
414         int again = 1;
415
416         reply = MALLOC(maxlen);
417
418         while (again) {
419                 if (!reply) {
420                         if (tmp)
421                                 free(tmp);
422                         return 1;
423                 }
424                 c = tmp = reply;
425                 c += snprint_defaults(conf, c, reply + maxlen - c);
426                 again = ((c - reply) == maxlen);
427                 if (again) {
428                         reply = REALLOC(reply, maxlen *= 2);
429                         continue;
430                 }
431                 c += snprint_blacklist(conf, c, reply + maxlen - c);
432                 again = ((c - reply) == maxlen);
433                 if (again) {
434                         reply = REALLOC(reply, maxlen *= 2);
435                         continue;
436                 }
437                 c += snprint_blacklist_except(conf, c, reply + maxlen - c);
438                 again = ((c - reply) == maxlen);
439                 if (again) {
440                         reply = REALLOC(reply, maxlen *= 2);
441                         continue;
442                 }
443                 c += snprint_hwtable(c, reply + maxlen - c, conf->hwtable);
444                 again = ((c - reply) == maxlen);
445                 if (again) {
446                         reply = REALLOC(reply, maxlen *= 2);
447                         continue;
448                 }
449                 c += snprint_overrides(c, reply + maxlen - c, conf->overrides);
450                 again = ((c - reply) == maxlen);
451                 if (again) {
452                         reply = REALLOC(reply, maxlen *= 2);
453                         continue;
454                 }
455                 if (VECTOR_SIZE(conf->mptable) > 0) {
456                         c += snprint_mptable(c, reply + maxlen - c,
457                                              conf->mptable);
458                         again = ((c - reply) == maxlen);
459                         if (again)
460                                 reply = REALLOC(reply, maxlen *= 2);
461                 }
462         }
463
464         printf("%s", reply);
465         FREE(reply);
466         return 0;
467 }
468
469 static int
470 get_dev_type(char *dev) {
471         struct stat buf;
472         int i;
473
474         if (stat(dev, &buf) == 0 && S_ISBLK(buf.st_mode)) {
475                 if (dm_is_dm_major(major(buf.st_rdev)))
476                         return DEV_DEVMAP;
477                 return DEV_DEVNODE;
478         }
479         else if (sscanf(dev, "%d:%d", &i, &i) == 2)
480                 return DEV_DEVT;
481         else if (valid_alias(dev))
482                 return DEV_DEVMAP;
483         return DEV_NONE;
484 }
485
486 int
487 main (int argc, char *argv[])
488 {
489         int arg;
490         extern char *optarg;
491         extern int optind;
492         int r = 1;
493         enum mpath_cmds cmd = CMD_CREATE;
494         enum devtypes dev_type;
495         char *dev = NULL;
496
497         udev = udev_new();
498         logsink = 0;
499         if (load_config(DEFAULT_CONFIGFILE))
500                 exit(1);
501         multipath_conf = conf;
502         while ((arg = getopt(argc, argv, ":adchl::FfM:v:p:b:BritquwW")) != EOF ) {
503                 switch(arg) {
504                 case 1: printf("optarg : %s\n",optarg);
505                         break;
506                 case 'v':
507                         if (sizeof(optarg) > sizeof(char *) ||
508                             !isdigit(optarg[0])) {
509                                 usage (argv[0]);
510                                 exit(1);
511                         }
512
513                         conf->verbosity = atoi(optarg);
514                         break;
515                 case 'b':
516                         conf->bindings_file = strdup(optarg);
517                         break;
518                 case 'B':
519                         conf->bindings_read_only = 1;
520                         break;
521                 case 'q':
522                         conf->allow_queueing = 1;
523                         break;
524                 case 'c':
525                         cmd = CMD_VALID_PATH;
526                         break;
527                 case 'd':
528                         if (cmd == CMD_CREATE)
529                                 cmd = CMD_DRY_RUN;
530                         break;
531                 case 'f':
532                         conf->remove = FLUSH_ONE;
533                         break;
534                 case 'F':
535                         conf->remove = FLUSH_ALL;
536                         break;
537                 case 'l':
538                         if (optarg && !strncmp(optarg, "l", 1))
539                                 cmd = CMD_LIST_LONG;
540                         else
541                                 cmd = CMD_LIST_SHORT;
542
543                         break;
544                 case 'M':
545 #if _DEBUG_
546                         debug = atoi(optarg);
547 #endif
548                         break;
549                 case 'p':
550                         conf->pgpolicy_flag = get_pgpolicy_id(optarg);
551                         if (conf->pgpolicy_flag == -1) {
552                                 printf("'%s' is not a valid policy\n", optarg);
553                                 usage(argv[0]);
554                                 exit(1);
555                         }
556                         break;
557                 case 'r':
558                         conf->force_reload = 1;
559                         break;
560                 case 'i':
561                         conf->ignore_wwids = 1;
562                         break;
563                 case 't':
564                         r = dump_config(conf);
565                         goto out_free_config;
566                 case 'h':
567                         usage(argv[0]);
568                         exit(0);
569                 case 'u':
570                         cmd = CMD_VALID_PATH;
571                         dev_type = DEV_UEVENT;
572                         break;
573                 case 'w':
574                         cmd = CMD_REMOVE_WWID;
575                         break;
576                 case 'W':
577                         cmd = CMD_RESET_WWIDS;
578                         break;
579                 case 'a':
580                         cmd = CMD_ADD_WWID;
581                         break;
582                 case ':':
583                         fprintf(stderr, "Missing option argument\n");
584                         usage(argv[0]);
585                         exit(1);
586                 case '?':
587                         fprintf(stderr, "Unknown switch: %s\n", optarg);
588                         usage(argv[0]);
589                         exit(1);
590                 default:
591                         usage(argv[0]);
592                         exit(1);
593                 }
594         }
595
596         if (getuid() != 0) {
597                 fprintf(stderr, "need to be root\n");
598                 exit(1);
599         }
600
601         dm_init(conf->verbosity);
602         if (dm_prereq())
603                 exit(1);
604         dm_drv_version(conf->version, TGT_MPATH);
605         dm_udev_set_sync_support(1);
606
607         if (optind < argc) {
608                 dev = MALLOC(FILE_NAME_SIZE);
609
610                 if (!dev)
611                         goto out;
612
613                 strncpy(dev, argv[optind], FILE_NAME_SIZE);
614                 if (dev_type != DEV_UEVENT)
615                         dev_type = get_dev_type(dev);
616                 if (dev_type == DEV_NONE) {
617                         condlog(0, "'%s' is not a valid argument\n", dev);
618                         goto out;
619                 }
620         }
621         if (dev_type == DEV_UEVENT) {
622                 openlog("multipath", 0, LOG_DAEMON);
623                 setlogmask(LOG_UPTO(conf->verbosity + 3));
624                 logsink = 1;
625         }
626
627         if (conf->max_fds) {
628                 struct rlimit fd_limit;
629
630                 fd_limit.rlim_cur = conf->max_fds;
631                 fd_limit.rlim_max = conf->max_fds;
632                 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0)
633                         condlog(0, "can't set open fds limit to %d : %s",
634                                 conf->max_fds, strerror(errno));
635         }
636
637         if (init_checkers(conf->multipath_dir)) {
638                 condlog(0, "failed to initialize checkers");
639                 goto out;
640         }
641         if (init_prio(conf->multipath_dir)) {
642                 condlog(0, "failed to initialize prioritizers");
643                 goto out;
644         }
645
646         if (cmd == CMD_VALID_PATH &&
647             (!dev || dev_type == DEV_DEVMAP)) {
648                 condlog(0, "the -c option requires a path to check");
649                 goto out;
650         }
651         if (cmd == CMD_VALID_PATH &&
652             dev_type == DEV_UEVENT) {
653                 int fd;
654
655                 fd = mpath_connect();
656                 if (fd == -1) {
657                         printf("%s is not a valid multipath device path\n",
658                                 dev);
659                         goto out;
660                 }
661                 mpath_disconnect(fd);
662         }
663         if (cmd == CMD_REMOVE_WWID && !dev) {
664                 condlog(0, "the -w option requires a device");
665                 goto out;
666         }
667         if (cmd == CMD_RESET_WWIDS) {
668                 struct multipath * mpp;
669                 int i;
670                 vector curmp;
671
672                 curmp = vector_alloc();
673                 if (!curmp) {
674                         condlog(0, "can't allocate memory for mp list");
675                         goto out;
676                 }
677                 if (dm_get_maps(curmp) == 0)
678                         r = replace_wwids(curmp);
679                 if (r == 0)
680                         printf("successfully reset wwids\n");
681                 vector_foreach_slot_backwards(curmp, mpp, i) {
682                         vector_del_slot(curmp, i);
683                         free_multipath(mpp, KEEP_PATHS);
684                 }
685                 vector_free(curmp);
686                 goto out;
687         }
688         if (conf->remove == FLUSH_ONE) {
689                 if (dev_type == DEV_DEVMAP) {
690                         r = dm_suspend_and_flush_map(dev);
691                 } else
692                         condlog(0, "must provide a map name to remove");
693
694                 goto out;
695         }
696         else if (conf->remove == FLUSH_ALL) {
697                 r = dm_flush_maps();
698                 goto out;
699         }
700         while ((r = configure(cmd, dev_type, dev)) < 0)
701                 condlog(3, "restart multipath configuration process");
702
703 out:
704         dm_lib_release();
705         dm_lib_exit();
706
707         cleanup_prio();
708         cleanup_checkers();
709
710         if (dev_type == DEV_UEVENT)
711                 closelog();
712
713 out_free_config:
714         /*
715          * Freeing config must be done after dm_lib_exit(), because
716          * the logging function (dm_write_log()), which is called there,
717          * references the config.
718          */
719         free_config(conf);
720         conf = NULL;
721         udev_unref(udev);
722         if (dev)
723                 FREE(dev);
724 #ifdef _DEBUG_
725         dbg_free_final(NULL);
726 #endif
727         return r;
728 }