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