5d847f080376eab4b502ca26c5eaa15032094ff9
[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                         } else if ((!find_multipaths_on(conf) &&
589                                     ignore_wwids_on(conf)) ||
590                                    check_wwids_file(refwwid, 0) == 0)
591                                 r = 0;
592                         if (r == 0 ||
593                             !find_multipaths_on(conf) ||
594                             !ignore_wwids_on(conf)) {
595                                 goto print_valid;
596                         }
597                 }
598         }
599
600         /*
601          * get a path list
602          */
603         if (devpath)
604                 di_flag = DI_WWID;
605
606         if (cmd == CMD_LIST_LONG)
607                 /* extended path info '-ll' */
608                 di_flag |= DI_SYSFS | DI_CHECKER | DI_SERIAL;
609         else if (cmd == CMD_LIST_SHORT)
610                 /* minimum path info '-l' */
611                 di_flag |= DI_SYSFS;
612         else
613                 /* maximum info */
614                 di_flag = DI_ALL;
615
616         if (path_discovery(pathvec, di_flag) < 0)
617                 goto out;
618
619         if (conf->verbosity > 2)
620                 print_all_paths(pathvec, 1);
621
622         get_path_layout(pathvec, 0);
623         foreign_path_layout();
624
625         if (get_dm_mpvec(cmd, curmp, pathvec, refwwid))
626                 goto out;
627
628         filter_pathvec(pathvec, refwwid);
629
630
631         if (cmd == CMD_VALID_PATH) {
632                 /* This only happens if find_multipaths and
633                  * ignore_wwids is set.
634                  * If there is currently a multipath device matching
635                  * the refwwid, or there is more than one path matching
636                  * the refwwid, then the path is valid */
637                 if (VECTOR_SIZE(curmp) != 0 || VECTOR_SIZE(pathvec) > 1)
638                         r = 0;
639                 else
640                         /* Use r=2 as an indication for "maybe" */
641                         r = 2;
642                 goto print_valid;
643         }
644
645         if (cmd != CMD_CREATE && cmd != CMD_DRY_RUN) {
646                 r = 0;
647                 goto out;
648         }
649
650         /*
651          * core logic entry point
652          */
653         r = coalesce_paths(&vecs, NULL, refwwid,
654                            conf->force_reload, cmd);
655
656 print_valid:
657         if (cmd == CMD_VALID_PATH)
658                 r = print_cmd_valid(r, pathvec, conf);
659
660 out:
661         if (refwwid)
662                 FREE(refwwid);
663
664         free_multipathvec(curmp, KEEP_PATHS);
665         free_pathvec(pathvec, FREE_PATHS);
666
667         return r;
668 }
669
670 static int
671 dump_config (struct config *conf)
672 {
673         char * c, * tmp = NULL;
674         char * reply;
675         unsigned int maxlen = 256;
676         int again = 1;
677
678         reply = MALLOC(maxlen);
679
680         while (again) {
681                 if (!reply) {
682                         if (tmp)
683                                 free(tmp);
684                         return 1;
685                 }
686                 c = tmp = reply;
687                 c += snprint_defaults(conf, c, reply + maxlen - c);
688                 again = ((c - reply) == maxlen);
689                 if (again) {
690                         reply = REALLOC(reply, maxlen *= 2);
691                         continue;
692                 }
693                 c += snprint_blacklist(conf, c, reply + maxlen - c);
694                 again = ((c - reply) == maxlen);
695                 if (again) {
696                         reply = REALLOC(reply, maxlen *= 2);
697                         continue;
698                 }
699                 c += snprint_blacklist_except(conf, c, reply + maxlen - c);
700                 again = ((c - reply) == maxlen);
701                 if (again) {
702                         reply = REALLOC(reply, maxlen *= 2);
703                         continue;
704                 }
705                 c += snprint_hwtable(conf, c, reply + maxlen - c, conf->hwtable);
706                 again = ((c - reply) == maxlen);
707                 if (again) {
708                         reply = REALLOC(reply, maxlen *= 2);
709                         continue;
710                 }
711                 c += snprint_overrides(conf, c, reply + maxlen - c,
712                                        conf->overrides);
713                 again = ((c - reply) == maxlen);
714                 if (again) {
715                         reply = REALLOC(reply, maxlen *= 2);
716                         continue;
717                 }
718                 if (VECTOR_SIZE(conf->mptable) > 0) {
719                         c += snprint_mptable(conf, c, reply + maxlen - c,
720                                              conf->mptable);
721                         again = ((c - reply) == maxlen);
722                         if (again)
723                                 reply = REALLOC(reply, maxlen *= 2);
724                 }
725         }
726
727         printf("%s", reply);
728         FREE(reply);
729         return 0;
730 }
731
732 static int
733 get_dev_type(char *dev) {
734         struct stat buf;
735         int i;
736
737         if (stat(dev, &buf) == 0 && S_ISBLK(buf.st_mode)) {
738                 if (dm_is_dm_major(major(buf.st_rdev)))
739                         return DEV_DEVMAP;
740                 return DEV_DEVNODE;
741         }
742         else if (sscanf(dev, "%d:%d", &i, &i) == 2)
743                 return DEV_DEVT;
744         else if (valid_alias(dev))
745                 return DEV_DEVMAP;
746         return DEV_NONE;
747 }
748
749 /*
750  * Some multipath commands are dangerous to run while multipathd is running.
751  * For example, "multipath -r" may apply a modified configuration to the kernel,
752  * while multipathd is still using the old configuration, leading to
753  * inconsistent state.
754  *
755  * It is safer to use equivalent multipathd client commands instead.
756  */
757 int delegate_to_multipathd(enum mpath_cmds cmd, const char *dev,
758                            enum devtypes dev_type, const struct config *conf)
759 {
760         int fd;
761         char command[1024], *p, *reply = NULL;
762         int n, r = 0;
763
764         fd = mpath_connect();
765         if (fd == -1)
766                 return 0;
767
768         p = command;
769         *p = '\0';
770         n = sizeof(command);
771
772         if (cmd == CMD_CREATE && conf->force_reload == FORCE_RELOAD_YES) {
773                 p += snprintf(p, n, "reconfigure");
774         }
775         /* Add other translations here */
776
777         if (strlen(command) == 0)
778                 /* No command found, no need to delegate */
779                 return 0;
780         else if (p >= command + sizeof(command)) {
781                 condlog(0, "internal error - command buffer overflow");
782                 r = -1;
783                 goto out;
784         }
785
786         condlog(3, "delegating command to multipathd");
787         r = mpath_process_cmd(fd, command, &reply, conf->uxsock_timeout);
788
789         if (r == -1) {
790                 condlog(1, "error in multipath command %s: %s",
791                         command, strerror(errno));
792                 goto out;
793         }
794
795         if (reply != NULL && *reply != '\0' && strcmp(reply, "ok\n"))
796                 printf("%s", reply);
797         r = 1;
798
799 out:
800         FREE(reply);
801         close(fd);
802         if (r < 0)
803                 exit(1);
804         return r;
805 }
806
807 int
808 main (int argc, char *argv[])
809 {
810         int arg;
811         extern char *optarg;
812         extern int optind;
813         int r = 1;
814         enum mpath_cmds cmd = CMD_CREATE;
815         enum devtypes dev_type = DEV_NONE;
816         char *dev = NULL;
817         struct config *conf;
818         int retries = -1;
819
820         udev = udev_new();
821         logsink = 0;
822         conf = load_config(DEFAULT_CONFIGFILE);
823         if (!conf)
824                 exit(1);
825         multipath_conf = conf;
826         conf->retrigger_tries = 0;
827         while ((arg = getopt(argc, argv, ":adcChl::FfM:v:p:b:BrR:itquUwW")) != EOF ) {
828                 switch(arg) {
829                 case 1: printf("optarg : %s\n",optarg);
830                         break;
831                 case 'v':
832                         if (sizeof(optarg) > sizeof(char *) ||
833                             !isdigit(optarg[0])) {
834                                 usage (argv[0]);
835                                 exit(1);
836                         }
837
838                         conf->verbosity = atoi(optarg);
839                         break;
840                 case 'b':
841                         conf->bindings_file = strdup(optarg);
842                         break;
843                 case 'B':
844                         conf->bindings_read_only = 1;
845                         break;
846                 case 'q':
847                         conf->allow_queueing = 1;
848                         break;
849                 case 'c':
850                         cmd = CMD_VALID_PATH;
851                         break;
852                 case 'C':
853                         cmd = CMD_USABLE_PATHS;
854                         break;
855                 case 'd':
856                         if (cmd == CMD_CREATE)
857                                 cmd = CMD_DRY_RUN;
858                         break;
859                 case 'f':
860                         conf->remove = FLUSH_ONE;
861                         break;
862                 case 'F':
863                         conf->remove = FLUSH_ALL;
864                         break;
865                 case 'l':
866                         if (optarg && !strncmp(optarg, "l", 1))
867                                 cmd = CMD_LIST_LONG;
868                         else
869                                 cmd = CMD_LIST_SHORT;
870
871                         break;
872                 case 'M':
873 #if _DEBUG_
874                         debug = atoi(optarg);
875 #endif
876                         break;
877                 case 'p':
878                         conf->pgpolicy_flag = get_pgpolicy_id(optarg);
879                         if (conf->pgpolicy_flag == IOPOLICY_UNDEF) {
880                                 printf("'%s' is not a valid policy\n", optarg);
881                                 usage(argv[0]);
882                                 exit(1);
883                         }
884                         break;
885                 case 'r':
886                         conf->force_reload = FORCE_RELOAD_YES;
887                         break;
888                 case 'i':
889                         conf->find_multipaths |= _FIND_MULTIPATHS_I;
890                         break;
891                 case 't':
892                         r = dump_config(conf);
893                         goto out_free_config;
894                 case 'h':
895                         usage(argv[0]);
896                         exit(0);
897                 case 'u':
898                         cmd = CMD_VALID_PATH;
899                         dev_type = DEV_UEVENT;
900                         break;
901                 case 'U':
902                         cmd = CMD_USABLE_PATHS;
903                         dev_type = DEV_UEVENT;
904                         break;
905                 case 'w':
906                         cmd = CMD_REMOVE_WWID;
907                         break;
908                 case 'W':
909                         cmd = CMD_RESET_WWIDS;
910                         break;
911                 case 'a':
912                         cmd = CMD_ADD_WWID;
913                         break;
914                 case 'R':
915                         retries = atoi(optarg);
916                         break;
917                 case ':':
918                         fprintf(stderr, "Missing option argument\n");
919                         usage(argv[0]);
920                         exit(1);
921                 case '?':
922                         fprintf(stderr, "Unknown switch: %s\n", optarg);
923                         usage(argv[0]);
924                         exit(1);
925                 default:
926                         usage(argv[0]);
927                         exit(1);
928                 }
929         }
930
931         if (getuid() != 0) {
932                 fprintf(stderr, "need to be root\n");
933                 exit(1);
934         }
935
936         if (optind < argc) {
937                 dev = MALLOC(FILE_NAME_SIZE);
938
939                 if (!dev)
940                         goto out;
941
942                 strncpy(dev, argv[optind], FILE_NAME_SIZE);
943                 if (dev_type != DEV_UEVENT)
944                         dev_type = get_dev_type(dev);
945                 if (dev_type == DEV_NONE) {
946                         condlog(0, "'%s' is not a valid argument\n", dev);
947                         goto out;
948                 }
949         }
950         if (dev_type == DEV_UEVENT) {
951                 openlog("multipath", 0, LOG_DAEMON);
952                 setlogmask(LOG_UPTO(conf->verbosity + 3));
953                 logsink = 1;
954         }
955
956         if (conf->max_fds) {
957                 struct rlimit fd_limit;
958
959                 fd_limit.rlim_cur = conf->max_fds;
960                 fd_limit.rlim_max = conf->max_fds;
961                 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0)
962                         condlog(0, "can't set open fds limit to %d : %s",
963                                 conf->max_fds, strerror(errno));
964         }
965
966         libmp_udev_set_sync_support(1);
967
968         if (init_checkers(conf->multipath_dir)) {
969                 condlog(0, "failed to initialize checkers");
970                 goto out;
971         }
972         if (init_prio(conf->multipath_dir)) {
973                 condlog(0, "failed to initialize prioritizers");
974                 goto out;
975         }
976         /* Failing here is non-fatal */
977         init_foreign(conf->multipath_dir);
978         if (cmd == CMD_USABLE_PATHS) {
979                 r = check_usable_paths(conf, dev, dev_type);
980                 goto out;
981         }
982         if (cmd == CMD_VALID_PATH &&
983             (!dev || dev_type == DEV_DEVMAP)) {
984                 condlog(0, "the -c option requires a path to check");
985                 goto out;
986         }
987         if (cmd == CMD_VALID_PATH &&
988             dev_type == DEV_UEVENT) {
989                 int fd;
990
991                 fd = mpath_connect();
992                 if (fd == -1) {
993                         condlog(3, "%s: daemon is not running", dev);
994                         if (!systemd_service_enabled(dev)) {
995                                 r = print_cmd_valid(1, NULL, conf);
996                                 goto out;
997                         }
998                 } else
999                         mpath_disconnect(fd);
1000         }
1001
1002         if (cmd == CMD_REMOVE_WWID && !dev) {
1003                 condlog(0, "the -w option requires a device");
1004                 goto out;
1005         }
1006
1007         if (delegate_to_multipathd(cmd, dev, dev_type, conf))
1008                 exit(0);
1009
1010         if (cmd == CMD_RESET_WWIDS) {
1011                 struct multipath * mpp;
1012                 int i;
1013                 vector curmp;
1014
1015                 curmp = vector_alloc();
1016                 if (!curmp) {
1017                         condlog(0, "can't allocate memory for mp list");
1018                         goto out;
1019                 }
1020                 if (dm_get_maps(curmp) == 0)
1021                         r = replace_wwids(curmp);
1022                 if (r == 0)
1023                         printf("successfully reset wwids\n");
1024                 vector_foreach_slot_backwards(curmp, mpp, i) {
1025                         vector_del_slot(curmp, i);
1026                         free_multipath(mpp, KEEP_PATHS);
1027                 }
1028                 vector_free(curmp);
1029                 goto out;
1030         }
1031         if (retries < 0)
1032                 retries = conf->remove_retries;
1033         if (conf->remove == FLUSH_ONE) {
1034                 if (dev_type == DEV_DEVMAP) {
1035                         r = dm_suspend_and_flush_map(dev, retries);
1036                 } else
1037                         condlog(0, "must provide a map name to remove");
1038
1039                 goto out;
1040         }
1041         else if (conf->remove == FLUSH_ALL) {
1042                 r = dm_flush_maps(retries);
1043                 goto out;
1044         }
1045         while ((r = configure(conf, cmd, dev_type, dev)) < 0)
1046                 condlog(3, "restart multipath configuration process");
1047
1048 out:
1049         dm_lib_release();
1050         dm_lib_exit();
1051
1052         cleanup_foreign();
1053         cleanup_prio();
1054         cleanup_checkers();
1055
1056         if (dev_type == DEV_UEVENT)
1057                 closelog();
1058
1059 out_free_config:
1060         /*
1061          * Freeing config must be done after dm_lib_exit(), because
1062          * the logging function (dm_write_log()), which is called there,
1063          * references the config.
1064          */
1065         free_config(conf);
1066         conf = NULL;
1067         udev_unref(udev);
1068         if (dev)
1069                 FREE(dev);
1070 #ifdef _DEBUG_
1071         dbg_free_final(NULL);
1072 #endif
1073         return r;
1074 }