61ba90a65eba437d07097dab9232cf34d4038a9d
[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 #include <fcntl.h>
33
34 #include "checkers.h"
35 #include "prio.h"
36 #include "vector.h"
37 #include "memory.h"
38 #include <libdevmapper.h>
39 #include "devmapper.h"
40 #include "util.h"
41 #include "defaults.h"
42 #include "config.h"
43 #include "structs.h"
44 #include "structs_vec.h"
45 #include "dmparser.h"
46 #include "sysfs.h"
47 #include "blacklist.h"
48 #include "discovery.h"
49 #include "debug.h"
50 #include "switchgroup.h"
51 #include "dm-generic.h"
52 #include "print.h"
53 #include "alias.h"
54 #include "configure.h"
55 #include "pgpolicies.h"
56 #include "version.h"
57 #include <errno.h>
58 #include <sys/time.h>
59 #include <sys/resource.h>
60 #include "wwids.h"
61 #include "uxsock.h"
62 #include "mpath_cmd.h"
63 #include "foreign.h"
64 #include "propsel.h"
65 #include "time-util.h"
66 #include "file.h"
67
68 int logsink;
69 struct udev *udev;
70 struct config *multipath_conf;
71
72 struct config *get_multipath_config(void)
73 {
74         return multipath_conf;
75 }
76
77 void put_multipath_config(void *arg)
78 {
79         /* Noop for now */
80 }
81
82 void rcu_register_thread_memb(void) {}
83
84 void rcu_unregister_thread_memb(void) {}
85
86 static int
87 filter_pathvec (vector pathvec, char * refwwid)
88 {
89         int i;
90         struct path * pp;
91
92         if (!refwwid || !strlen(refwwid))
93                 return 0;
94
95         vector_foreach_slot (pathvec, pp, i) {
96                 if (strncmp(pp->wwid, refwwid, WWID_SIZE) != 0) {
97                         condlog(3, "skip path %s : out of scope", pp->dev);
98                         free_path(pp);
99                         vector_del_slot(pathvec, i);
100                         i--;
101                 }
102         }
103         return 0;
104 }
105
106 static void
107 usage (char * progname)
108 {
109         fprintf (stderr, VERSION_STRING);
110         fprintf (stderr, "Usage:\n");
111         fprintf (stderr, "  %s [-a|-c|-w|-W] [-d] [-r] [-i] [-v lvl] [-p pol] [-b fil] [-q] [dev]\n", progname);
112         fprintf (stderr, "  %s -l|-ll|-f [-v lvl] [-b fil] [-R num] [dev]\n", progname);
113         fprintf (stderr, "  %s -F [-v lvl] [-R num]\n", progname);
114         fprintf (stderr, "  %s -t\n", progname);
115         fprintf (stderr, "  %s -h\n", progname);
116         fprintf (stderr,
117                 "\n"
118                 "Where:\n"
119                 "  -h      print this usage text\n"
120                 "  -l      show multipath topology (sysfs and DM info)\n"
121                 "  -ll     show multipath topology (maximum info)\n"
122                 "  -f      flush a multipath device map\n"
123                 "  -F      flush all multipath device maps\n"
124                 "  -a      add a device wwid to the wwids file\n"
125                 "  -c      check if a device should be a path in a multipath device\n"
126                 "  -C      check if a multipath device has usable paths\n"
127                 "  -q      allow queue_if_no_path when multipathd is not running\n"
128                 "  -d      dry run, do not create or update devmaps\n"
129                 "  -t      display the currently used multipathd configuration\n"
130                 "  -r      force devmap reload\n"
131                 "  -i      ignore wwids file\n"
132                 "  -B      treat the bindings file as read only\n"
133                 "  -b fil  bindings file location\n"
134                 "  -w      remove a device from the wwids file\n"
135                 "  -W      reset the wwids file include only the current devices\n"
136                 "  -p pol  force all maps to specified path grouping policy :\n"
137                 "          . failover            one path per priority group\n"
138                 "          . multibus            all paths in one priority group\n"
139                 "          . group_by_serial     one priority group per serial\n"
140                 "          . group_by_prio       one priority group per priority lvl\n"
141                 "          . group_by_node_name  one priority group per target node\n"
142                 "  -v lvl  verbosity level\n"
143                 "          . 0 no output\n"
144                 "          . 1 print created devmap names only\n"
145                 "          . 2 default verbosity\n"
146                 "          . 3 print debug information\n"
147                 "  -R num  number of times to retry removes of in-use devices\n"
148                 "  dev     action limited to:\n"
149                 "          . multipath named 'dev' (ex: mpath0) or\n"
150                 "          . multipath whose wwid is 'dev' (ex: 60051..)\n"
151                 "          . multipath including the path named 'dev' (ex: /dev/sda)\n"
152                 "          . multipath including the path with maj:min 'dev' (ex: 8:0)\n"
153                 );
154
155 }
156
157 static int
158 update_paths (struct multipath * mpp, int quick)
159 {
160         int i, j;
161         struct pathgroup * pgp;
162         struct path * pp;
163         struct config *conf;
164
165         if (!mpp->pg)
166                 return 0;
167
168         vector_foreach_slot (mpp->pg, pgp, i) {
169                 if (!pgp->paths)
170                         continue;
171
172                 vector_foreach_slot (pgp->paths, pp, j) {
173                         if (!strlen(pp->dev)) {
174                                 if (devt2devname(pp->dev, FILE_NAME_SIZE,
175                                                  pp->dev_t)) {
176                                         /*
177                                          * path is not in sysfs anymore
178                                          */
179                                         pp->chkrstate = pp->state = PATH_DOWN;
180                                         pp->offline = 1;
181                                         continue;
182                                 }
183                                 pp->mpp = mpp;
184                                 if (quick)
185                                         continue;
186                                 conf = get_multipath_config();
187                                 if (pathinfo(pp, conf, DI_ALL))
188                                         pp->state = PATH_UNCHECKED;
189                                 put_multipath_config(conf);
190                                 continue;
191                         }
192                         pp->mpp = mpp;
193                         if (quick)
194                                 continue;
195                         if (pp->state == PATH_UNCHECKED ||
196                             pp->state == PATH_WILD) {
197                                 conf = get_multipath_config();
198                                 if (pathinfo(pp, conf, DI_CHECKER))
199                                         pp->state = PATH_UNCHECKED;
200                                 put_multipath_config(conf);
201                         }
202
203                         if (pp->priority == PRIO_UNDEF) {
204                                 conf = get_multipath_config();
205                                 if (pathinfo(pp, conf, DI_PRIO))
206                                         pp->priority = PRIO_UNDEF;
207                                 put_multipath_config(conf);
208                         }
209                 }
210         }
211         return 0;
212 }
213
214 static int
215 get_dm_mpvec (enum mpath_cmds cmd, vector curmp, vector pathvec, char * refwwid)
216 {
217         int i;
218         struct multipath * mpp;
219         char params[PARAMS_SIZE], status[PARAMS_SIZE];
220
221         if (dm_get_maps(curmp))
222                 return 1;
223
224         vector_foreach_slot (curmp, mpp, i) {
225                 /*
226                  * discard out of scope maps
227                  */
228                 if (refwwid && strlen(refwwid) &&
229                     strncmp(mpp->wwid, refwwid, WWID_SIZE)) {
230                         condlog(3, "skip map %s: out of scope", mpp->alias);
231                         free_multipath(mpp, KEEP_PATHS);
232                         vector_del_slot(curmp, i);
233                         i--;
234                         continue;
235                 }
236
237                 if (cmd == CMD_VALID_PATH)
238                         continue;
239
240                 dm_get_map(mpp->alias, &mpp->size, params);
241                 condlog(3, "params = %s", params);
242                 dm_get_status(mpp->alias, status);
243                 condlog(3, "status = %s", status);
244
245                 disassemble_map(pathvec, params, mpp, 0);
246
247                 /*
248                  * disassemble_map() can add new paths to pathvec.
249                  * If not in "fast list mode", we need to fetch information
250                  * about them
251                  */
252                 update_paths(mpp, (cmd == CMD_LIST_SHORT));
253
254                 if (cmd == CMD_LIST_LONG)
255                         mpp->bestpg = select_path_group(mpp);
256
257                 disassemble_status(status, mpp);
258
259                 if (cmd == CMD_LIST_SHORT ||
260                     cmd == CMD_LIST_LONG) {
261                         struct config *conf = get_multipath_config();
262                         print_multipath_topology(mpp, conf->verbosity);
263                         put_multipath_config(conf);
264                 }
265
266                 if (cmd == CMD_CREATE)
267                         reinstate_paths(mpp);
268         }
269
270         if (cmd == CMD_LIST_SHORT || cmd == CMD_LIST_LONG) {
271                 struct config *conf = get_multipath_config();
272
273                 print_foreign_topology(conf->verbosity);
274                 put_multipath_config(conf);
275         }
276
277         return 0;
278 }
279
280 static int check_usable_paths(struct config *conf,
281                               const char *devpath, enum devtypes dev_type)
282 {
283         struct udev_device *ud = NULL;
284         struct multipath *mpp = NULL;
285         struct pathgroup *pg;
286         struct path *pp;
287         char *mapname;
288         vector pathvec = NULL;
289         char params[PARAMS_SIZE], status[PARAMS_SIZE];
290         dev_t devt;
291         int r = 1, i, j;
292
293         ud = get_udev_device(devpath, dev_type);
294         if (ud == NULL)
295                 return r;
296
297         devt = udev_device_get_devnum(ud);
298         if (!dm_is_dm_major(major(devt))) {
299                 condlog(1, "%s is not a dm device", devpath);
300                 goto out;
301         }
302
303         mapname = dm_mapname(major(devt), minor(devt));
304         if (mapname == NULL) {
305                 condlog(1, "dm device not found: %s", devpath);
306                 goto out;
307         }
308
309         if (!dm_is_mpath(mapname)) {
310                 condlog(1, "%s is not a multipath map", devpath);
311                 goto free;
312         }
313
314         /* pathvec is needed for disassemble_map */
315         pathvec = vector_alloc();
316         if (pathvec == NULL)
317                 goto free;
318
319         mpp = dm_get_multipath(mapname);
320         if (mpp == NULL)
321                 goto free;
322
323         dm_get_map(mpp->alias, &mpp->size, params);
324         dm_get_status(mpp->alias, status);
325         disassemble_map(pathvec, params, mpp, 0);
326         disassemble_status(status, mpp);
327
328         vector_foreach_slot (mpp->pg, pg, i) {
329                 vector_foreach_slot (pg->paths, pp, j) {
330                         pp->udev = get_udev_device(pp->dev_t, DEV_DEVT);
331                         if (pp->udev == NULL)
332                                 continue;
333                         if (pathinfo(pp, conf, DI_SYSFS|DI_NOIO) != PATHINFO_OK)
334                                 continue;
335
336                         if (pp->state == PATH_UP &&
337                             pp->dmstate == PSTATE_ACTIVE) {
338                                 condlog(3, "%s: path %s is usable",
339                                         devpath, pp->dev);
340                                 r = 0;
341                                 goto found;
342                         }
343                 }
344         }
345 found:
346         condlog(r == 0 ? 3 : 2, "%s:%s usable paths found",
347                 devpath, r == 0 ? "" : " no");
348 free:
349         FREE(mapname);
350         free_multipath(mpp, FREE_PATHS);
351         vector_free(pathvec);
352 out:
353         udev_device_unref(ud);
354         return r;
355 }
356
357 enum {
358         FIND_MULTIPATHS_WAIT_DONE = 0,
359         FIND_MULTIPATHS_WAITING = 1,
360         FIND_MULTIPATHS_ERROR = -1,
361         FIND_MULTIPATHS_NEVER = -2,
362 };
363
364 static const char shm_find_mp_dir[] = MULTIPATH_SHM_BASE "find_multipaths";
365 static void close_fd(void *arg)
366 {
367         close((long)arg);
368 }
369
370 /**
371  * find_multipaths_check_timeout(wwid, tmo)
372  * Helper for "find_multipaths smart"
373  *
374  * @param[in] pp: path to check / record
375  * @param[in] tmo: configured timeout for this WWID, or value <= 0 for checking
376  * @param[out] until: timestamp until we must wait, CLOCK_REALTIME, if return
377  *             value is FIND_MULTIPATHS_WAITING
378  * @returns: FIND_MULTIPATHS_WAIT_DONE, if waiting has finished
379  * @returns: FIND_MULTIPATHS_ERROR, if internal error occurred
380  * @returns: FIND_MULTIPATHS_NEVER, if tmo is 0 and we didn't wait for this
381  *           device
382  * @returns: FIND_MULTIPATHS_WAITING, if timeout hasn't expired
383  */
384 static int find_multipaths_check_timeout(const struct path *pp, long tmo,
385                                          struct timespec *until)
386 {
387         char path[PATH_MAX];
388         struct timespec now, ftimes[2], tdiff;
389         struct stat st;
390         long fd;
391         int r, err, retries = 0;
392
393         clock_gettime(CLOCK_REALTIME, &now);
394
395         if (snprintf(path, sizeof(path), "%s/%s", shm_find_mp_dir, pp->dev_t)
396             >= sizeof(path)) {
397                 condlog(1, "%s: path name overflow", __func__);
398                 return FIND_MULTIPATHS_ERROR;
399         }
400
401         if (ensure_directories_exist(path, 0700)) {
402                 condlog(1, "%s: error creating directories", __func__);
403                 return FIND_MULTIPATHS_ERROR;
404         }
405
406 retry:
407         fd = open(path, O_RDONLY);
408         if (fd != -1) {
409                 pthread_cleanup_push(close_fd, (void *)fd);
410                 r = fstat(fd, &st);
411                 if (r != 0)
412                         err = errno;
413                 pthread_cleanup_pop(1);
414
415         } else if (tmo > 0) {
416                 if (errno == ENOENT)
417                         fd = open(path, O_RDWR|O_EXCL|O_CREAT, 0644);
418                 if (fd == -1) {
419                         if (errno == EEXIST && !retries++)
420                                 /* We could have raced with another process */
421                                 goto retry;
422                         condlog(1, "%s: error opening %s: %s",
423                                 __func__, path, strerror(errno));
424                         return FIND_MULTIPATHS_ERROR;
425                 };
426
427                 pthread_cleanup_push(close_fd, (void *)fd);
428                 /*
429                  * We just created the file. Set st_mtim to our desired
430                  * expiry time.
431                  */
432                 ftimes[0].tv_sec = 0;
433                 ftimes[0].tv_nsec = UTIME_OMIT;
434                 ftimes[1].tv_sec = now.tv_sec + tmo;
435                 ftimes[1].tv_nsec = now.tv_nsec;
436                 if (futimens(fd, ftimes) != 0) {
437                         condlog(1, "%s: error in futimens(%s): %s", __func__,
438                                 path, strerror(errno));
439                 }
440                 r = fstat(fd, &st);
441                 if (r != 0)
442                         err = errno;
443                 pthread_cleanup_pop(1);
444         } else
445                 return FIND_MULTIPATHS_NEVER;
446
447         if (r != 0) {
448                 condlog(1, "%s: error in fstat for %s: %s", __func__,
449                         path, strerror(err));
450                 return FIND_MULTIPATHS_ERROR;
451         }
452
453         timespecsub(&st.st_mtim, &now, &tdiff);
454
455         if (tdiff.tv_sec <= 0)
456                 return FIND_MULTIPATHS_WAIT_DONE;
457
458         *until = tdiff;
459         return FIND_MULTIPATHS_WAITING;
460 }
461
462 static int print_cmd_valid(int k, const vector pathvec,
463                            struct config *conf)
464 {
465         static const int vals[] = { 1, 0, 2 };
466         int wait = FIND_MULTIPATHS_NEVER;
467         struct timespec until;
468         struct path *pp;
469
470         if (k < 0 || k >= sizeof(vals))
471                 return 1;
472
473         if (k == 2) {
474                 /*
475                  * Caller ensures that pathvec[0] is the path to
476                  * examine.
477                  */
478                 pp = VECTOR_SLOT(pathvec, 0);
479                 select_find_multipaths_timeout(conf, pp);
480                 wait = find_multipaths_check_timeout(
481                         pp, pp->find_multipaths_timeout, &until);
482                 if (wait != FIND_MULTIPATHS_WAITING)
483                         k = 1;
484         } else if (pathvec != NULL) {
485                 pp = VECTOR_SLOT(pathvec, 0);
486                 wait = find_multipaths_check_timeout(pp, 0, &until);
487         }
488         if (wait == FIND_MULTIPATHS_WAITING)
489                 printf("FIND_MULTIPATHS_WAIT_UNTIL=\"%ld.%06ld\"\n",
490                                until.tv_sec, until.tv_nsec/1000);
491         else if (wait == FIND_MULTIPATHS_WAIT_DONE)
492                 printf("FIND_MULTIPATHS_WAIT_UNTIL=\"0\"\n");
493         printf("DM_MULTIPATH_DEVICE_PATH=\"%d\"\n", vals[k]);
494         return k == 1;
495 }
496
497 /*
498  * Return value:
499  *  -1: Retry
500  *   0: Success
501  *   1: Failure
502  */
503 static int
504 configure (struct config *conf, enum mpath_cmds cmd,
505            enum devtypes dev_type, char *devpath)
506 {
507         vector curmp = NULL;
508         vector pathvec = NULL;
509         struct vectors vecs;
510         int r = 1;
511         int di_flag = 0;
512         char * refwwid = NULL;
513         char * dev = NULL;
514
515         /*
516          * allocate core vectors to store paths and multipaths
517          */
518         curmp = vector_alloc();
519         pathvec = vector_alloc();
520
521         if (!curmp || !pathvec) {
522                 condlog(0, "can not allocate memory");
523                 goto out;
524         }
525         vecs.pathvec = pathvec;
526         vecs.mpvec = curmp;
527
528         dev = convert_dev(devpath, (dev_type == DEV_DEVNODE));
529
530         /*
531          * if we have a blacklisted device parameter, exit early
532          */
533         if (dev && (dev_type == DEV_DEVNODE ||
534                     dev_type == DEV_UEVENT) &&
535             cmd != CMD_REMOVE_WWID &&
536             (filter_devnode(conf->blist_devnode,
537                             conf->elist_devnode, dev) > 0)) {
538                 goto print_valid;
539         }
540
541         /*
542          * scope limiting must be translated into a wwid
543          * failing the translation is fatal (by policy)
544          */
545         if (devpath) {
546                 int failed = get_refwwid(cmd, devpath, dev_type,
547                                          pathvec, &refwwid);
548                 if (!refwwid) {
549                         condlog(4, "%s: failed to get wwid", devpath);
550                         if (failed == 2 && cmd == CMD_VALID_PATH)
551                                 goto print_valid;
552                         else
553                                 condlog(3, "scope is null");
554                         goto out;
555                 }
556                 if (cmd == CMD_REMOVE_WWID) {
557                         r = remove_wwid(refwwid);
558                         if (r == 0)
559                                 printf("wwid '%s' removed\n", refwwid);
560                         else if (r == 1) {
561                                 printf("wwid '%s' not in wwids file\n",
562                                         refwwid);
563                                 r = 0;
564                         }
565                         goto out;
566                 }
567                 if (cmd == CMD_ADD_WWID) {
568                         r = remember_wwid(refwwid);
569                         if (r >= 0)
570                                 printf("wwid '%s' added\n", refwwid);
571                         else
572                                 printf("failed adding '%s' to wwids file\n",
573                                        refwwid);
574                         goto out;
575                 }
576                 condlog(3, "scope limited to %s", refwwid);
577                 /* If you are ignoring the wwids file and find_multipaths is
578                  * set, you need to actually check if there are two available
579                  * paths to determine if this path should be multipathed. To
580                  * do this, we put off the check until after discovering all
581                  * the paths.
582                  * Paths listed in the wwids file are always considered valid.
583                  */
584                 if (cmd == CMD_VALID_PATH) {
585                         if (is_failed_wwid(refwwid) == WWID_IS_FAILED) {
586                                 r = 1;
587                                 goto print_valid;
588                         }
589                         if ((!find_multipaths_on(conf) &&
590                                     ignore_wwids_on(conf)) ||
591                                    check_wwids_file(refwwid, 0) == 0)
592                                 r = 0;
593                         if (!ignore_wwids_on(conf))
594                                 goto print_valid;
595                         /* At this point, either r==0 or find_multipaths_on. */
596                         if (r == 0)
597                                 goto print_valid;
598                         /* find_multipaths_on: Fall through to path detection */
599                 }
600         }
601
602         /*
603          * get a path list
604          */
605         if (devpath)
606                 di_flag = DI_WWID;
607
608         if (cmd == CMD_LIST_LONG)
609                 /* extended path info '-ll' */
610                 di_flag |= DI_SYSFS | DI_CHECKER | DI_SERIAL;
611         else if (cmd == CMD_LIST_SHORT)
612                 /* minimum path info '-l' */
613                 di_flag |= DI_SYSFS;
614         else
615                 /* maximum info */
616                 di_flag = DI_ALL;
617
618         if (path_discovery(pathvec, di_flag) < 0)
619                 goto out;
620
621         if (conf->verbosity > 2)
622                 print_all_paths(pathvec, 1);
623
624         get_path_layout(pathvec, 0);
625         foreign_path_layout();
626
627         if (get_dm_mpvec(cmd, curmp, pathvec, refwwid))
628                 goto out;
629
630         filter_pathvec(pathvec, refwwid);
631
632
633         if (cmd == CMD_VALID_PATH) {
634                 /* This only happens if find_multipaths and
635                  * ignore_wwids is set.
636                  * If there is currently a multipath device matching
637                  * the refwwid, or there is more than one path matching
638                  * the refwwid, then the path is valid */
639                 if (VECTOR_SIZE(curmp) != 0 || VECTOR_SIZE(pathvec) > 1)
640                         r = 0;
641                 else
642                         /* Use r=2 as an indication for "maybe" */
643                         r = 2;
644                 goto print_valid;
645         }
646
647         if (cmd != CMD_CREATE && cmd != CMD_DRY_RUN) {
648                 r = 0;
649                 goto out;
650         }
651
652         /*
653          * core logic entry point
654          */
655         r = coalesce_paths(&vecs, NULL, refwwid,
656                            conf->force_reload, cmd);
657
658 print_valid:
659         if (cmd == CMD_VALID_PATH)
660                 r = print_cmd_valid(r, pathvec, conf);
661
662 out:
663         if (refwwid)
664                 FREE(refwwid);
665
666         free_multipathvec(curmp, KEEP_PATHS);
667         free_pathvec(pathvec, FREE_PATHS);
668
669         return r;
670 }
671
672 static int
673 dump_config (struct config *conf)
674 {
675         char * c, * tmp = NULL;
676         char * reply;
677         unsigned int maxlen = 256;
678         int again = 1;
679
680         reply = MALLOC(maxlen);
681
682         while (again) {
683                 if (!reply) {
684                         if (tmp)
685                                 free(tmp);
686                         return 1;
687                 }
688                 c = tmp = reply;
689                 c += snprint_defaults(conf, c, reply + maxlen - c);
690                 again = ((c - reply) == maxlen);
691                 if (again) {
692                         reply = REALLOC(reply, maxlen *= 2);
693                         continue;
694                 }
695                 c += snprint_blacklist(conf, c, reply + maxlen - c);
696                 again = ((c - reply) == maxlen);
697                 if (again) {
698                         reply = REALLOC(reply, maxlen *= 2);
699                         continue;
700                 }
701                 c += snprint_blacklist_except(conf, c, reply + maxlen - c);
702                 again = ((c - reply) == maxlen);
703                 if (again) {
704                         reply = REALLOC(reply, maxlen *= 2);
705                         continue;
706                 }
707                 c += snprint_hwtable(conf, c, reply + maxlen - c, conf->hwtable);
708                 again = ((c - reply) == maxlen);
709                 if (again) {
710                         reply = REALLOC(reply, maxlen *= 2);
711                         continue;
712                 }
713                 c += snprint_overrides(conf, c, reply + maxlen - c,
714                                        conf->overrides);
715                 again = ((c - reply) == maxlen);
716                 if (again) {
717                         reply = REALLOC(reply, maxlen *= 2);
718                         continue;
719                 }
720                 if (VECTOR_SIZE(conf->mptable) > 0) {
721                         c += snprint_mptable(conf, c, reply + maxlen - c,
722                                              conf->mptable);
723                         again = ((c - reply) == maxlen);
724                         if (again)
725                                 reply = REALLOC(reply, maxlen *= 2);
726                 }
727         }
728
729         printf("%s", reply);
730         FREE(reply);
731         return 0;
732 }
733
734 static int
735 get_dev_type(char *dev) {
736         struct stat buf;
737         int i;
738
739         if (stat(dev, &buf) == 0 && S_ISBLK(buf.st_mode)) {
740                 if (dm_is_dm_major(major(buf.st_rdev)))
741                         return DEV_DEVMAP;
742                 return DEV_DEVNODE;
743         }
744         else if (sscanf(dev, "%d:%d", &i, &i) == 2)
745                 return DEV_DEVT;
746         else if (valid_alias(dev))
747                 return DEV_DEVMAP;
748         return DEV_NONE;
749 }
750
751 /*
752  * Some multipath commands are dangerous to run while multipathd is running.
753  * For example, "multipath -r" may apply a modified configuration to the kernel,
754  * while multipathd is still using the old configuration, leading to
755  * inconsistent state.
756  *
757  * It is safer to use equivalent multipathd client commands instead.
758  */
759 int delegate_to_multipathd(enum mpath_cmds cmd, const char *dev,
760                            enum devtypes dev_type, const struct config *conf)
761 {
762         int fd;
763         char command[1024], *p, *reply = NULL;
764         int n, r = 0;
765
766         fd = mpath_connect();
767         if (fd == -1)
768                 return 0;
769
770         p = command;
771         *p = '\0';
772         n = sizeof(command);
773
774         if (cmd == CMD_CREATE && conf->force_reload == FORCE_RELOAD_YES) {
775                 p += snprintf(p, n, "reconfigure");
776         }
777         /* Add other translations here */
778
779         if (strlen(command) == 0)
780                 /* No command found, no need to delegate */
781                 return 0;
782         else if (p >= command + sizeof(command)) {
783                 condlog(0, "internal error - command buffer overflow");
784                 r = -1;
785                 goto out;
786         }
787
788         condlog(3, "delegating command to multipathd");
789         r = mpath_process_cmd(fd, command, &reply, conf->uxsock_timeout);
790
791         if (r == -1) {
792                 condlog(1, "error in multipath command %s: %s",
793                         command, strerror(errno));
794                 goto out;
795         }
796
797         if (reply != NULL && *reply != '\0' && strcmp(reply, "ok\n"))
798                 printf("%s", reply);
799         r = 1;
800
801 out:
802         FREE(reply);
803         close(fd);
804         if (r < 0)
805                 exit(1);
806         return r;
807 }
808
809 int
810 main (int argc, char *argv[])
811 {
812         int arg;
813         extern char *optarg;
814         extern int optind;
815         int r = 1;
816         enum mpath_cmds cmd = CMD_CREATE;
817         enum devtypes dev_type = DEV_NONE;
818         char *dev = NULL;
819         struct config *conf;
820         int retries = -1;
821
822         udev = udev_new();
823         logsink = 0;
824         conf = load_config(DEFAULT_CONFIGFILE);
825         if (!conf)
826                 exit(1);
827         multipath_conf = conf;
828         conf->retrigger_tries = 0;
829         while ((arg = getopt(argc, argv, ":adcChl::FfM:v:p:b:BrR:itquUwW")) != EOF ) {
830                 switch(arg) {
831                 case 1: printf("optarg : %s\n",optarg);
832                         break;
833                 case 'v':
834                         if (sizeof(optarg) > sizeof(char *) ||
835                             !isdigit(optarg[0])) {
836                                 usage (argv[0]);
837                                 exit(1);
838                         }
839
840                         conf->verbosity = atoi(optarg);
841                         break;
842                 case 'b':
843                         conf->bindings_file = strdup(optarg);
844                         break;
845                 case 'B':
846                         conf->bindings_read_only = 1;
847                         break;
848                 case 'q':
849                         conf->allow_queueing = 1;
850                         break;
851                 case 'c':
852                         cmd = CMD_VALID_PATH;
853                         break;
854                 case 'C':
855                         cmd = CMD_USABLE_PATHS;
856                         break;
857                 case 'd':
858                         if (cmd == CMD_CREATE)
859                                 cmd = CMD_DRY_RUN;
860                         break;
861                 case 'f':
862                         conf->remove = FLUSH_ONE;
863                         break;
864                 case 'F':
865                         conf->remove = FLUSH_ALL;
866                         break;
867                 case 'l':
868                         if (optarg && !strncmp(optarg, "l", 1))
869                                 cmd = CMD_LIST_LONG;
870                         else
871                                 cmd = CMD_LIST_SHORT;
872
873                         break;
874                 case 'M':
875 #if _DEBUG_
876                         debug = atoi(optarg);
877 #endif
878                         break;
879                 case 'p':
880                         conf->pgpolicy_flag = get_pgpolicy_id(optarg);
881                         if (conf->pgpolicy_flag == IOPOLICY_UNDEF) {
882                                 printf("'%s' is not a valid policy\n", optarg);
883                                 usage(argv[0]);
884                                 exit(1);
885                         }
886                         break;
887                 case 'r':
888                         conf->force_reload = FORCE_RELOAD_YES;
889                         break;
890                 case 'i':
891                         conf->find_multipaths |= _FIND_MULTIPATHS_I;
892                         break;
893                 case 't':
894                         r = dump_config(conf);
895                         goto out_free_config;
896                 case 'h':
897                         usage(argv[0]);
898                         exit(0);
899                 case 'u':
900                         cmd = CMD_VALID_PATH;
901                         dev_type = DEV_UEVENT;
902                         break;
903                 case 'U':
904                         cmd = CMD_USABLE_PATHS;
905                         dev_type = DEV_UEVENT;
906                         break;
907                 case 'w':
908                         cmd = CMD_REMOVE_WWID;
909                         break;
910                 case 'W':
911                         cmd = CMD_RESET_WWIDS;
912                         break;
913                 case 'a':
914                         cmd = CMD_ADD_WWID;
915                         break;
916                 case 'R':
917                         retries = atoi(optarg);
918                         break;
919                 case ':':
920                         fprintf(stderr, "Missing option argument\n");
921                         usage(argv[0]);
922                         exit(1);
923                 case '?':
924                         fprintf(stderr, "Unknown switch: %s\n", optarg);
925                         usage(argv[0]);
926                         exit(1);
927                 default:
928                         usage(argv[0]);
929                         exit(1);
930                 }
931         }
932
933         if (getuid() != 0) {
934                 fprintf(stderr, "need to be root\n");
935                 exit(1);
936         }
937
938         if (optind < argc) {
939                 dev = MALLOC(FILE_NAME_SIZE);
940
941                 if (!dev)
942                         goto out;
943
944                 strncpy(dev, argv[optind], FILE_NAME_SIZE);
945                 if (dev_type != DEV_UEVENT)
946                         dev_type = get_dev_type(dev);
947                 if (dev_type == DEV_NONE) {
948                         condlog(0, "'%s' is not a valid argument\n", dev);
949                         goto out;
950                 }
951         }
952         if (dev_type == DEV_UEVENT) {
953                 openlog("multipath", 0, LOG_DAEMON);
954                 setlogmask(LOG_UPTO(conf->verbosity + 3));
955                 logsink = 1;
956         }
957
958         if (conf->max_fds) {
959                 struct rlimit fd_limit;
960
961                 fd_limit.rlim_cur = conf->max_fds;
962                 fd_limit.rlim_max = conf->max_fds;
963                 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0)
964                         condlog(0, "can't set open fds limit to %d : %s",
965                                 conf->max_fds, strerror(errno));
966         }
967
968         libmp_udev_set_sync_support(1);
969
970         if (init_checkers(conf->multipath_dir)) {
971                 condlog(0, "failed to initialize checkers");
972                 goto out;
973         }
974         if (init_prio(conf->multipath_dir)) {
975                 condlog(0, "failed to initialize prioritizers");
976                 goto out;
977         }
978         /* Failing here is non-fatal */
979         init_foreign(conf->multipath_dir);
980         if (cmd == CMD_USABLE_PATHS) {
981                 r = check_usable_paths(conf, dev, dev_type);
982                 goto out;
983         }
984         if (cmd == CMD_VALID_PATH &&
985             (!dev || dev_type == DEV_DEVMAP)) {
986                 condlog(0, "the -c option requires a path to check");
987                 goto out;
988         }
989         if (cmd == CMD_VALID_PATH &&
990             dev_type == DEV_UEVENT) {
991                 int fd;
992
993                 fd = mpath_connect();
994                 if (fd == -1) {
995                         condlog(3, "%s: daemon is not running", dev);
996                         if (!systemd_service_enabled(dev)) {
997                                 r = print_cmd_valid(1, NULL, conf);
998                                 goto out;
999                         }
1000                 } else
1001                         mpath_disconnect(fd);
1002         }
1003
1004         if (cmd == CMD_REMOVE_WWID && !dev) {
1005                 condlog(0, "the -w option requires a device");
1006                 goto out;
1007         }
1008
1009         if (delegate_to_multipathd(cmd, dev, dev_type, conf))
1010                 exit(0);
1011
1012         if (cmd == CMD_RESET_WWIDS) {
1013                 struct multipath * mpp;
1014                 int i;
1015                 vector curmp;
1016
1017                 curmp = vector_alloc();
1018                 if (!curmp) {
1019                         condlog(0, "can't allocate memory for mp list");
1020                         goto out;
1021                 }
1022                 if (dm_get_maps(curmp) == 0)
1023                         r = replace_wwids(curmp);
1024                 if (r == 0)
1025                         printf("successfully reset wwids\n");
1026                 vector_foreach_slot_backwards(curmp, mpp, i) {
1027                         vector_del_slot(curmp, i);
1028                         free_multipath(mpp, KEEP_PATHS);
1029                 }
1030                 vector_free(curmp);
1031                 goto out;
1032         }
1033         if (retries < 0)
1034                 retries = conf->remove_retries;
1035         if (conf->remove == FLUSH_ONE) {
1036                 if (dev_type == DEV_DEVMAP) {
1037                         r = dm_suspend_and_flush_map(dev, retries);
1038                 } else
1039                         condlog(0, "must provide a map name to remove");
1040
1041                 goto out;
1042         }
1043         else if (conf->remove == FLUSH_ALL) {
1044                 r = dm_flush_maps(retries);
1045                 goto out;
1046         }
1047         while ((r = configure(conf, cmd, dev_type, dev)) < 0)
1048                 condlog(3, "restart multipath configuration process");
1049
1050 out:
1051         dm_lib_release();
1052         dm_lib_exit();
1053
1054         cleanup_foreign();
1055         cleanup_prio();
1056         cleanup_checkers();
1057
1058         if (dev_type == DEV_UEVENT)
1059                 closelog();
1060
1061 out_free_config:
1062         /*
1063          * Freeing config must be done after dm_lib_exit(), because
1064          * the logging function (dm_write_log()), which is called there,
1065          * references the config.
1066          */
1067         free_config(conf);
1068         conf = NULL;
1069         udev_unref(udev);
1070         if (dev)
1071                 FREE(dev);
1072 #ifdef _DEBUG_
1073         dbg_free_final(NULL);
1074 #endif
1075         return r;
1076 }