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