libmultipath: fix ALUA autodetection when paths are down
[multipath-tools/.git] / libmultipath / propsel.c
1 /*
2  * Copyright (c) 2004, 2005 Christophe Varoqui
3  * Copyright (c) 2005 Benjamin Marzinski, Redhat
4  * Copyright (c) 2005 Kiyoshi Ueda, NEC
5  */
6 #include <stdio.h>
7
8 #include "nvme-lib.h"
9 #include "checkers.h"
10 #include "memory.h"
11 #include "vector.h"
12 #include "structs.h"
13 #include "config.h"
14 #include "debug.h"
15 #include "pgpolicies.h"
16 #include "alias.h"
17 #include "defaults.h"
18 #include "devmapper.h"
19 #include "prio.h"
20 #include "discovery.h"
21 #include "dict.h"
22 #include "util.h"
23 #include "sysfs.h"
24 #include "prioritizers/alua_rtpg.h"
25 #include "prkey.h"
26 #include "propsel.h"
27 #include <inttypes.h>
28 #include <libudev.h>
29
30 pgpolicyfn *pgpolicies[] = {
31         NULL,
32         one_path_per_group,
33         one_group,
34         group_by_serial,
35         group_by_prio,
36         group_by_node_name
37 };
38
39 #define do_set(var, src, dest, msg)                                     \
40 do {                                                                    \
41         if (src && src->var) {                                          \
42                 dest = src->var;                                        \
43                 origin = msg;                                           \
44                 goto out;                                               \
45         }                                                               \
46 } while(0)
47
48 #define __do_set_from_vec(type, var, src, dest)                         \
49 ({                                                                      \
50         type *_p;                                                       \
51         bool _found = false;                                            \
52         int i;                                                          \
53                                                                         \
54         vector_foreach_slot(src, _p, i) {                               \
55                 if (_p->var) {                                          \
56                         dest = _p->var;                                 \
57                         _found = true;                                  \
58                         break;                                          \
59                 }                                                       \
60         }                                                               \
61         _found;                                                         \
62 })
63
64 #define __do_set_from_hwe(var, src, dest) \
65         __do_set_from_vec(struct hwentry, var, (src)->hwe, dest)
66
67 #define do_set_from_hwe(var, src, dest, msg)                            \
68         if (__do_set_from_hwe(var, src, dest)) {                        \
69                 origin = msg;                                           \
70                 goto out;                                               \
71         }
72
73 static const char default_origin[] = "(setting: multipath internal)";
74 static const char hwe_origin[] =
75         "(setting: storage device configuration)";
76 static const char multipaths_origin[] =
77         "(setting: multipath.conf multipaths section)";
78 static const char conf_origin[] =
79         "(setting: multipath.conf defaults/devices section)";
80 static const char overrides_origin[] =
81         "(setting: multipath.conf overrides section)";
82 static const char cmdline_origin[] =
83         "(setting: multipath command line [-p] flag)";
84 static const char autodetect_origin[] =
85         "(setting: storage device autodetected)";
86 static const char marginal_path_origin[] =
87         "(setting: implied by marginal_path check)";
88 static const char delay_watch_origin[] =
89         "(setting: implied by delay_watch_checks)";
90 static const char delay_wait_origin[] =
91         "(setting: implied by delay_wait_checks)";
92
93 #define do_default(dest, value)                                         \
94 do {                                                                    \
95         dest = value;                                                   \
96         origin = default_origin;                                        \
97 } while(0)
98
99 #define mp_set_mpe(var)                                                 \
100 do_set(var, mp->mpe, mp->var, multipaths_origin)
101 #define mp_set_hwe(var)                                                 \
102 do_set_from_hwe(var, mp, mp->var, hwe_origin)
103 #define mp_set_ovr(var)                                                 \
104 do_set(var, conf->overrides, mp->var, overrides_origin)
105 #define mp_set_conf(var)                                                \
106 do_set(var, conf, mp->var, conf_origin)
107 #define mp_set_default(var, value)                                      \
108 do_default(mp->var, value)
109
110 #define pp_set_mpe(var)                                                 \
111 do_set(var, mpe, pp->var, multipaths_origin)
112 #define pp_set_hwe(var)                                                 \
113 do_set_from_hwe(var, pp, pp->var, hwe_origin)
114 #define pp_set_conf(var)                                                \
115 do_set(var, conf, pp->var, conf_origin)
116 #define pp_set_ovr(var)                                                 \
117 do_set(var, conf->overrides, pp->var, overrides_origin)
118 #define pp_set_default(var, value)                                      \
119 do_default(pp->var, value)
120
121 #define do_attr_set(var, src, shift, msg)                               \
122 do {                                                                    \
123         if (src && (src->attribute_flags & (1 << shift))) {             \
124                 mp->attribute_flags |= (1 << shift);                    \
125                 mp->var = src->var;                                     \
126                 origin = msg;                                           \
127                 goto out;                                               \
128         }                                                               \
129 } while(0)
130
131 #define set_attr_mpe(var, shift)                                        \
132 do_attr_set(var, mp->mpe, shift, "(setting: multipath.conf multipaths section)")
133 #define set_attr_conf(var, shift)                                       \
134 do_attr_set(var, conf, shift, "(setting: multipath.conf defaults/devices section)")
135
136 #define do_prkey_set(src, msg)                                          \
137 do {                                                                    \
138         if (src && src->prkey_source != PRKEY_SOURCE_NONE) {            \
139                 mp->prkey_source = src->prkey_source;                   \
140                 mp->reservation_key = src->reservation_key;             \
141                 mp->sa_flags = src->sa_flags;                           \
142                 origin = msg;                                           \
143                 goto out;                                               \
144         }                                                               \
145 } while (0)
146
147 int select_mode(struct config *conf, struct multipath *mp)
148 {
149         const char *origin;
150
151         set_attr_mpe(mode, ATTR_MODE);
152         set_attr_conf(mode, ATTR_MODE);
153         mp->attribute_flags &= ~(1 << ATTR_MODE);
154         return 0;
155 out:
156         condlog(3, "%s: mode = 0%o %s", mp->alias, mp->mode, origin);
157         return 0;
158 }
159
160 int select_uid(struct config *conf, struct multipath *mp)
161 {
162         const char *origin;
163
164         set_attr_mpe(uid, ATTR_UID);
165         set_attr_conf(uid, ATTR_UID);
166         mp->attribute_flags &= ~(1 << ATTR_UID);
167         return 0;
168 out:
169         condlog(3, "%s: uid = 0%o %s", mp->alias, mp->uid, origin);
170         return 0;
171 }
172
173 int select_gid(struct config *conf, struct multipath *mp)
174 {
175         const char *origin;
176
177         set_attr_mpe(gid, ATTR_GID);
178         set_attr_conf(gid, ATTR_GID);
179         mp->attribute_flags &= ~(1 << ATTR_GID);
180         return 0;
181 out:
182         condlog(3, "%s: gid = 0%o %s", mp->alias, mp->gid, origin);
183         return 0;
184 }
185
186 /*
187  * selectors :
188  * traverse the configuration layers from most specific to most generic
189  * stop at first explicit setting found
190  */
191 int select_rr_weight(struct config *conf, struct multipath * mp)
192 {
193         const char *origin;
194         char buff[13];
195
196         mp_set_mpe(rr_weight);
197         mp_set_ovr(rr_weight);
198         mp_set_hwe(rr_weight);
199         mp_set_conf(rr_weight);
200         mp_set_default(rr_weight, DEFAULT_RR_WEIGHT);
201 out:
202         print_rr_weight(buff, 13, mp->rr_weight);
203         condlog(3, "%s: rr_weight = %s %s", mp->alias, buff, origin);
204         return 0;
205 }
206
207 int select_pgfailback(struct config *conf, struct multipath * mp)
208 {
209         const char *origin;
210         char buff[13];
211
212         mp_set_mpe(pgfailback);
213         mp_set_ovr(pgfailback);
214         mp_set_hwe(pgfailback);
215         mp_set_conf(pgfailback);
216         mp_set_default(pgfailback, DEFAULT_FAILBACK);
217 out:
218         print_pgfailback(buff, 13, mp->pgfailback);
219         condlog(3, "%s: failback = %s %s", mp->alias, buff, origin);
220         return 0;
221 }
222
223 int select_pgpolicy(struct config *conf, struct multipath * mp)
224 {
225         const char *origin;
226         char buff[POLICY_NAME_SIZE];
227
228         if (conf->pgpolicy_flag > 0) {
229                 mp->pgpolicy = conf->pgpolicy_flag;
230                 origin = cmdline_origin;
231                 goto out;
232         }
233         mp_set_mpe(pgpolicy);
234         mp_set_ovr(pgpolicy);
235         mp_set_hwe(pgpolicy);
236         mp_set_conf(pgpolicy);
237         mp_set_default(pgpolicy, DEFAULT_PGPOLICY);
238 out:
239         mp->pgpolicyfn = pgpolicies[mp->pgpolicy];
240         get_pgpolicy_name(buff, POLICY_NAME_SIZE, mp->pgpolicy);
241         condlog(3, "%s: path_grouping_policy = %s %s", mp->alias, buff, origin);
242         return 0;
243 }
244
245 int select_selector(struct config *conf, struct multipath * mp)
246 {
247         const char *origin;
248
249         mp_set_mpe(selector);
250         mp_set_ovr(selector);
251         mp_set_hwe(selector);
252         mp_set_conf(selector);
253         mp_set_default(selector, DEFAULT_SELECTOR);
254 out:
255         mp->selector = STRDUP(mp->selector);
256         condlog(3, "%s: path_selector = \"%s\" %s", mp->alias, mp->selector,
257                 origin);
258         return 0;
259 }
260
261 static void
262 select_alias_prefix (struct config *conf, struct multipath * mp)
263 {
264         const char *origin;
265
266         mp_set_ovr(alias_prefix);
267         mp_set_hwe(alias_prefix);
268         mp_set_conf(alias_prefix);
269         mp_set_default(alias_prefix, DEFAULT_ALIAS_PREFIX);
270 out:
271         condlog(3, "%s: alias_prefix = %s %s", mp->wwid, mp->alias_prefix,
272                 origin);
273 }
274
275 static int
276 want_user_friendly_names(struct config *conf, struct multipath * mp)
277 {
278
279         const char *origin;
280         int user_friendly_names;
281
282         do_set(user_friendly_names, mp->mpe, user_friendly_names,
283                multipaths_origin);
284         do_set(user_friendly_names, conf->overrides, user_friendly_names,
285                overrides_origin);
286         do_set_from_hwe(user_friendly_names, mp, user_friendly_names,
287                         hwe_origin);
288         do_set(user_friendly_names, conf, user_friendly_names,
289                conf_origin);
290         do_default(user_friendly_names, DEFAULT_USER_FRIENDLY_NAMES);
291 out:
292         condlog(3, "%s: user_friendly_names = %s %s", mp->wwid,
293                 (user_friendly_names == USER_FRIENDLY_NAMES_ON)? "yes" : "no",
294                 origin);
295         return (user_friendly_names == USER_FRIENDLY_NAMES_ON);
296 }
297
298 int select_alias(struct config *conf, struct multipath * mp)
299 {
300         const char *origin = NULL;
301
302         if (mp->mpe && mp->mpe->alias) {
303                 mp->alias = STRDUP(mp->mpe->alias);
304                 origin = multipaths_origin;
305                 goto out;
306         }
307
308         mp->alias = NULL;
309         if (!want_user_friendly_names(conf, mp))
310                 goto out;
311
312         select_alias_prefix(conf, mp);
313
314         if (strlen(mp->alias_old) > 0) {
315                 mp->alias = use_existing_alias(mp->wwid, conf->bindings_file,
316                                 mp->alias_old, mp->alias_prefix,
317                                 conf->bindings_read_only);
318                 memset (mp->alias_old, 0, WWID_SIZE);
319                 origin = "(setting: using existing alias)";
320         }
321
322         if (mp->alias == NULL) {
323                 mp->alias = get_user_friendly_alias(mp->wwid,
324                                 conf->bindings_file, mp->alias_prefix, conf->bindings_read_only);
325                 origin = "(setting: user_friendly_name)";
326         }
327 out:
328         if (mp->alias == NULL) {
329                 mp->alias = STRDUP(mp->wwid);
330                 origin = "(setting: default to WWID)";
331         }
332         if (mp->alias)
333                 condlog(3, "%s: alias = %s %s", mp->wwid, mp->alias, origin);
334         return mp->alias ? 0 : 1;
335 }
336
337 void reconcile_features_with_options(const char *id, char **features, int* no_path_retry,
338                   int *retain_hwhandler)
339 {
340         static const char q_i_n_p[] = "queue_if_no_path";
341         static const char r_a_h_h[] = "retain_attached_hw_handler";
342         char buff[12];
343
344         if (*features == NULL)
345                 return;
346         if (id == NULL)
347                 id = "UNKNOWN";
348
349         /*
350          * We only use no_path_retry internally. The "queue_if_no_path"
351          * device-mapper feature is derived from it when the map is loaded.
352          * For consistency, "queue_if_no_path" is removed from the
353          * internal libmultipath features string.
354          * For backward compatibility we allow 'features "1 queue_if_no_path"';
355          * it's translated into "no_path_retry queue" here.
356          */
357         if (strstr(*features, q_i_n_p)) {
358                 condlog(0, "%s: option 'features \"1 %s\"' is deprecated, "
359                         "please use 'no_path_retry queue' instead",
360                         id, q_i_n_p);
361                 if (*no_path_retry == NO_PATH_RETRY_UNDEF) {
362                         *no_path_retry = NO_PATH_RETRY_QUEUE;
363                         print_no_path_retry(buff, sizeof(buff),
364                                             *no_path_retry);
365                         condlog(3, "%s: no_path_retry = %s (inherited setting from feature '%s')",
366                                 id, buff, q_i_n_p);
367                 };
368                 /* Warn only if features string is overridden */
369                 if (*no_path_retry != NO_PATH_RETRY_QUEUE) {
370                         print_no_path_retry(buff, sizeof(buff),
371                                             *no_path_retry);
372                         condlog(2, "%s: ignoring feature '%s' because no_path_retry is set to '%s'",
373                                 id, q_i_n_p, buff);
374                 }
375                 remove_feature(features, q_i_n_p);
376         }
377         if (strstr(*features, r_a_h_h)) {
378                 condlog(0, "%s: option 'features \"1 %s\"' is deprecated",
379                         id, r_a_h_h);
380                 if (*retain_hwhandler == RETAIN_HWHANDLER_UNDEF) {
381                         condlog(3, "%s: %s = on (inherited setting from feature '%s')",
382                                 id, r_a_h_h, r_a_h_h);
383                         *retain_hwhandler = RETAIN_HWHANDLER_ON;
384                 } else if (*retain_hwhandler == RETAIN_HWHANDLER_OFF)
385                         condlog(2, "%s: ignoring feature '%s' because %s is set to 'off'",
386                                 id, r_a_h_h, r_a_h_h);
387                 remove_feature(features, r_a_h_h);
388         }
389 }
390
391 int select_features(struct config *conf, struct multipath *mp)
392 {
393         const char *origin;
394
395         mp_set_mpe(features);
396         mp_set_ovr(features);
397         mp_set_hwe(features);
398         mp_set_conf(features);
399         mp_set_default(features, DEFAULT_FEATURES);
400 out:
401         mp->features = STRDUP(mp->features);
402
403         reconcile_features_with_options(mp->alias, &mp->features,
404                                         &mp->no_path_retry,
405                                         &mp->retain_hwhandler);
406         condlog(3, "%s: features = \"%s\" %s", mp->alias, mp->features, origin);
407         return 0;
408 }
409
410 static int get_dh_state(struct path *pp, char *value, size_t value_len)
411 {
412         struct udev_device *ud;
413
414         if (pp->udev == NULL)
415                 return -1;
416
417         ud = udev_device_get_parent_with_subsystem_devtype(
418                 pp->udev, "scsi", "scsi_device");
419         if (ud == NULL)
420                 return -1;
421
422         return sysfs_attr_get_value(ud, "dh_state", value, value_len);
423 }
424
425 int select_hwhandler(struct config *conf, struct multipath *mp)
426 {
427         const char *origin;
428         struct path *pp;
429         /* dh_state is no longer than "detached" */
430         char handler[12];
431         static char alua_name[] = "1 alua";
432         static const char tpgs_origin[]= "(setting: autodetected from TPGS)";
433         char *dh_state;
434         int i;
435         bool all_tpgs = true, one_tpgs = false;
436
437         dh_state = &handler[2];
438
439         /*
440          * TPGS_UNDEF means that ALUA support couldn't determined either way
441          * yet, probably because the path was always down.
442          * If at least one path does have TPGS support, and no path has
443          * TPGS_NONE, assume that TPGS would be supported by all paths if
444          * all were up.
445          */
446         vector_foreach_slot(mp->paths, pp, i) {
447                 int tpgs = path_get_tpgs(pp);
448
449                 all_tpgs = all_tpgs && tpgs != TPGS_NONE;
450                 one_tpgs = one_tpgs ||
451                         (tpgs != TPGS_NONE && tpgs != TPGS_UNDEF);
452         }
453         all_tpgs = all_tpgs && one_tpgs;
454
455         if (mp->retain_hwhandler != RETAIN_HWHANDLER_OFF) {
456                 vector_foreach_slot(mp->paths, pp, i) {
457                         if (get_dh_state(pp, dh_state, sizeof(handler) - 2) > 0
458                             && strcmp(dh_state, "detached")) {
459                                 memcpy(handler, "1 ", 2);
460                                 mp->hwhandler = handler;
461                                 origin = "(setting: retained by kernel driver)";
462                                 goto out;
463                         }
464                 }
465         }
466
467         mp_set_hwe(hwhandler);
468         mp_set_conf(hwhandler);
469         mp_set_default(hwhandler, DEFAULT_HWHANDLER);
470 out:
471         if (all_tpgs && !strcmp(mp->hwhandler, DEFAULT_HWHANDLER) &&
472                 origin == default_origin) {
473                 mp->hwhandler = alua_name;
474                 origin = tpgs_origin;
475         } else if (!all_tpgs && !strcmp(mp->hwhandler, alua_name)) {
476                 mp->hwhandler = DEFAULT_HWHANDLER;
477                 origin = tpgs_origin;
478         }
479         mp->hwhandler = STRDUP(mp->hwhandler);
480         condlog(3, "%s: hardware_handler = \"%s\" %s", mp->alias, mp->hwhandler,
481                 origin);
482         return 0;
483 }
484
485 /*
486  * Current RDAC (NetApp E-Series) firmware relies
487  * on periodic REPORT TARGET PORT GROUPS for
488  * internal load balancing.
489  * Using the sysfs priority checker defeats this purpose.
490  *
491  * Moreover, NetApp would also prefer the RDAC checker over ALUA.
492  * (https://www.redhat.com/archives/dm-devel/2017-September/msg00326.html)
493  */
494 static int
495 check_rdac(struct path * pp)
496 {
497         int len;
498         char buff[44];
499         const char *checker_name;
500
501         if (pp->bus != SYSFS_BUS_SCSI)
502                 return 0;
503         /* Avoid ioctl if this is likely not an RDAC array */
504         if (__do_set_from_hwe(checker_name, pp, checker_name) &&
505             strcmp(checker_name, RDAC))
506                 return 0;
507         len = get_vpd_sgio(pp->fd, 0xC9, 0, buff, 44);
508         if (len <= 0)
509                 return 0;
510         return !(memcmp(buff + 4, "vac1", 4));
511 }
512
513 int select_checker(struct config *conf, struct path *pp)
514 {
515         const char *origin;
516         char *ckr_name;
517         struct checker * c = &pp->checker;
518
519         if (pp->detect_checker == DETECT_CHECKER_ON) {
520                 origin = autodetect_origin;
521                 if (check_rdac(pp)) {
522                         ckr_name = RDAC;
523                         goto out;
524                 } else if (path_get_tpgs(pp) != TPGS_NONE) {
525                         ckr_name = TUR;
526                         goto out;
527                 }
528         }
529         do_set(checker_name, conf->overrides, ckr_name, overrides_origin);
530         do_set_from_hwe(checker_name, pp, ckr_name, hwe_origin);
531         do_set(checker_name, conf, ckr_name, conf_origin);
532         do_default(ckr_name, DEFAULT_CHECKER);
533 out:
534         checker_get(conf->multipath_dir, c, ckr_name);
535         condlog(3, "%s: path_checker = %s %s", pp->dev,
536                 checker_name(c), origin);
537         if (conf->checker_timeout) {
538                 c->timeout = conf->checker_timeout;
539                 condlog(3, "%s: checker timeout = %u s %s",
540                         pp->dev, c->timeout, conf_origin);
541         }
542         else if (sysfs_get_timeout(pp, &c->timeout) > 0)
543                 condlog(3, "%s: checker timeout = %u s (setting: kernel sysfs)",
544                         pp->dev, c->timeout);
545         else {
546                 c->timeout = DEF_TIMEOUT;
547                 condlog(3, "%s: checker timeout = %u s %s",
548                         pp->dev, c->timeout, default_origin);
549         }
550         return 0;
551 }
552
553 int select_getuid(struct config *conf, struct path *pp)
554 {
555         const char *origin;
556
557         pp->uid_attribute = get_uid_attribute_by_attrs(conf, pp->dev);
558         if (pp->uid_attribute) {
559                 origin = "(setting: multipath.conf defaults section / uid_attrs)";
560                 goto out;
561         }
562
563         pp_set_ovr(getuid);
564         pp_set_ovr(uid_attribute);
565         pp_set_hwe(getuid);
566         pp_set_hwe(uid_attribute);
567         pp_set_conf(getuid);
568         pp_set_conf(uid_attribute);
569         pp_set_default(uid_attribute, DEFAULT_UID_ATTRIBUTE);
570 out:
571         if (pp->uid_attribute)
572                 condlog(3, "%s: uid_attribute = %s %s", pp->dev,
573                         pp->uid_attribute, origin);
574         else if (pp->getuid)
575                 condlog(3, "%s: getuid = \"%s\" %s", pp->dev, pp->getuid,
576                         origin);
577         return 0;
578 }
579
580 void
581 detect_prio(struct config *conf, struct path * pp)
582 {
583         struct prio *p = &pp->prio;
584         char buff[512];
585         char *default_prio;
586         int tpgs;
587
588         switch(pp->bus) {
589         case SYSFS_BUS_NVME:
590                 if (nvme_id_ctrl_ana(pp->fd, NULL) == 0)
591                         return;
592                 default_prio = PRIO_ANA;
593                 break;
594         case SYSFS_BUS_SCSI:
595                 tpgs = path_get_tpgs(pp);
596                 if (tpgs == TPGS_NONE)
597                         return;
598                 if ((tpgs == TPGS_EXPLICIT || !check_rdac(pp)) &&
599                     sysfs_get_asymmetric_access_state(pp, buff, 512) >= 0)
600                         default_prio = PRIO_SYSFS;
601                 else
602                         default_prio = PRIO_ALUA;
603                 break;
604         default:
605                 return;
606         }
607         prio_get(conf->multipath_dir, p, default_prio, DEFAULT_PRIO_ARGS);
608 }
609
610 #define set_prio(dir, src, msg)                                 \
611 do {                                                                    \
612         if (src && src->prio_name) {                                    \
613                 prio_get(dir, p, src->prio_name, src->prio_args);       \
614                 origin = msg;                                           \
615                 goto out;                                               \
616         }                                                               \
617 } while(0)
618
619 #define set_prio_from_vec(type, dir, src, msg, p)                       \
620 do {                                                                    \
621         type *_p;                                                       \
622         int i;                                                          \
623         char *prio_name = NULL, *prio_args = NULL;                      \
624                                                                         \
625         vector_foreach_slot(src, _p, i) {                               \
626                 if (prio_name == NULL && _p->prio_name)         \
627                         prio_name = _p->prio_name;                      \
628                 if (prio_args == NULL && _p->prio_args)         \
629                         prio_args = _p->prio_args;                      \
630         }                                                               \
631         if (prio_name != NULL) {                                        \
632                 prio_get(dir, p, prio_name, prio_args);                 \
633                 origin = msg;                                           \
634                 goto out;                                               \
635         }                                                               \
636 } while (0)
637
638 int select_prio(struct config *conf, struct path *pp)
639 {
640         const char *origin;
641         struct mpentry * mpe;
642         struct prio * p = &pp->prio;
643         int log_prio = 3;
644
645         if (pp->detect_prio == DETECT_PRIO_ON) {
646                 detect_prio(conf, pp);
647                 if (prio_selected(p)) {
648                         origin = autodetect_origin;
649                         goto out;
650                 }
651         }
652         mpe = find_mpe(conf->mptable, pp->wwid);
653         set_prio(conf->multipath_dir, mpe, multipaths_origin);
654         set_prio(conf->multipath_dir, conf->overrides, overrides_origin);
655         set_prio_from_vec(struct hwentry, conf->multipath_dir,
656                           pp->hwe, hwe_origin, p);
657         set_prio(conf->multipath_dir, conf, conf_origin);
658         prio_get(conf->multipath_dir, p, DEFAULT_PRIO, DEFAULT_PRIO_ARGS);
659         origin = default_origin;
660 out:
661         /*
662          * fetch tpgs mode for alua, if its not already obtained
663          */
664         if (!strncmp(prio_name(p), PRIO_ALUA, PRIO_NAME_LEN)) {
665                 int tpgs = path_get_tpgs(pp);
666
667                 if (tpgs == TPGS_NONE) {
668                         prio_get(conf->multipath_dir,
669                                  p, DEFAULT_PRIO, DEFAULT_PRIO_ARGS);
670                         origin = "(setting: emergency fallback - alua failed)";
671                         log_prio = 1;
672                 }
673         }
674         condlog(log_prio, "%s: prio = %s %s", pp->dev, prio_name(p), origin);
675         condlog(3, "%s: prio args = \"%s\" %s", pp->dev, prio_args(p), origin);
676         return 0;
677 }
678
679 int select_no_path_retry(struct config *conf, struct multipath *mp)
680 {
681         const char *origin = NULL;
682         char buff[12];
683
684         if (mp->disable_queueing) {
685                 condlog(0, "%s: queueing disabled", mp->alias);
686                 mp->no_path_retry = NO_PATH_RETRY_FAIL;
687                 return 0;
688         }
689         mp_set_mpe(no_path_retry);
690         mp_set_ovr(no_path_retry);
691         mp_set_hwe(no_path_retry);
692         mp_set_conf(no_path_retry);
693 out:
694         print_no_path_retry(buff, 12, mp->no_path_retry);
695         if (origin)
696                 condlog(3, "%s: no_path_retry = %s %s", mp->alias, buff,
697                         origin);
698         else
699                 condlog(3, "%s: no_path_retry = undef %s",
700                         mp->alias, default_origin);
701         return 0;
702 }
703
704 int
705 select_minio_rq (struct config *conf, struct multipath * mp)
706 {
707         const char *origin;
708
709         do_set(minio_rq, mp->mpe, mp->minio, multipaths_origin);
710         do_set(minio_rq, conf->overrides, mp->minio, overrides_origin);
711         do_set_from_hwe(minio_rq, mp, mp->minio, hwe_origin);
712         do_set(minio_rq, conf, mp->minio, conf_origin);
713         do_default(mp->minio, DEFAULT_MINIO_RQ);
714 out:
715         condlog(3, "%s: minio = %i %s", mp->alias, mp->minio, origin);
716         return 0;
717 }
718
719 int
720 select_minio_bio (struct config *conf, struct multipath * mp)
721 {
722         const char *origin;
723
724         mp_set_mpe(minio);
725         mp_set_ovr(minio);
726         mp_set_hwe(minio);
727         mp_set_conf(minio);
728         mp_set_default(minio, DEFAULT_MINIO);
729 out:
730         condlog(3, "%s: minio = %i %s", mp->alias, mp->minio, origin);
731         return 0;
732 }
733
734 int select_minio(struct config *conf, struct multipath *mp)
735 {
736         unsigned int minv_dmrq[3] = {1, 1, 0};
737
738         if (VERSION_GE(conf->version, minv_dmrq))
739                 return select_minio_rq(conf, mp);
740         else
741                 return select_minio_bio(conf, mp);
742 }
743
744 int select_fast_io_fail(struct config *conf, struct multipath *mp)
745 {
746         const char *origin;
747         char buff[12];
748
749         mp_set_ovr(fast_io_fail);
750         mp_set_hwe(fast_io_fail);
751         mp_set_conf(fast_io_fail);
752         mp_set_default(fast_io_fail, DEFAULT_FAST_IO_FAIL);
753 out:
754         print_fast_io_fail(buff, 12, mp->fast_io_fail);
755         condlog(3, "%s: fast_io_fail_tmo = %s %s", mp->alias, buff, origin);
756         return 0;
757 }
758
759 int select_dev_loss(struct config *conf, struct multipath *mp)
760 {
761         const char *origin;
762         char buff[12];
763
764         mp_set_ovr(dev_loss);
765         mp_set_hwe(dev_loss);
766         mp_set_conf(dev_loss);
767         mp->dev_loss = 0;
768         return 0;
769 out:
770         print_dev_loss(buff, 12, mp->dev_loss);
771         condlog(3, "%s: dev_loss_tmo = %s %s", mp->alias, buff, origin);
772         return 0;
773 }
774
775 int select_flush_on_last_del(struct config *conf, struct multipath *mp)
776 {
777         const char *origin;
778
779         mp_set_mpe(flush_on_last_del);
780         mp_set_ovr(flush_on_last_del);
781         mp_set_hwe(flush_on_last_del);
782         mp_set_conf(flush_on_last_del);
783         mp_set_default(flush_on_last_del, DEFAULT_FLUSH);
784 out:
785         condlog(3, "%s: flush_on_last_del = %s %s", mp->alias,
786                 (mp->flush_on_last_del == FLUSH_ENABLED)? "yes" : "no", origin);
787         return 0;
788 }
789
790 int select_reservation_key(struct config *conf, struct multipath *mp)
791 {
792         const char *origin;
793         char buff[PRKEY_SIZE];
794         char *from_file = "";
795         uint64_t prkey = 0;
796
797         do_prkey_set(mp->mpe, multipaths_origin);
798         do_prkey_set(conf, conf_origin);
799         put_be64(mp->reservation_key, 0);
800         mp->sa_flags = 0;
801         mp->prkey_source = PRKEY_SOURCE_NONE;
802         return 0;
803 out:
804         if (mp->prkey_source == PRKEY_SOURCE_FILE) {
805                 from_file = " (from prkeys file)";
806                 if (get_prkey(conf, mp, &prkey, &mp->sa_flags) != 0)
807                         put_be64(mp->reservation_key, 0);
808                 else
809                         put_be64(mp->reservation_key, prkey);
810         }
811         print_reservation_key(buff, PRKEY_SIZE, mp->reservation_key,
812                               mp->sa_flags, mp->prkey_source);
813         condlog(3, "%s: reservation_key = %s %s%s", mp->alias, buff, origin,
814                 from_file);
815         return 0;
816 }
817
818 int select_retain_hwhandler(struct config *conf, struct multipath *mp)
819 {
820         const char *origin;
821         unsigned int minv_dm_retain[3] = {1, 5, 0};
822
823         if (!VERSION_GE(conf->version, minv_dm_retain)) {
824                 mp->retain_hwhandler = RETAIN_HWHANDLER_OFF;
825                 origin = "(setting: WARNING, requires kernel dm-mpath version >= 1.5.0)";
826                 goto out;
827         }
828         if (get_linux_version_code() >= KERNEL_VERSION(4, 3, 0)) {
829                 mp->retain_hwhandler = RETAIN_HWHANDLER_ON;
830                 origin = "(setting: implied in kernel >= 4.3.0)";
831                 goto out;
832         }
833         mp_set_ovr(retain_hwhandler);
834         mp_set_hwe(retain_hwhandler);
835         mp_set_conf(retain_hwhandler);
836         mp_set_default(retain_hwhandler, DEFAULT_RETAIN_HWHANDLER);
837 out:
838         condlog(3, "%s: retain_attached_hw_handler = %s %s", mp->alias,
839                 (mp->retain_hwhandler == RETAIN_HWHANDLER_ON)? "yes" : "no",
840                 origin);
841         return 0;
842 }
843
844 int select_detect_prio(struct config *conf, struct path *pp)
845 {
846         const char *origin;
847
848         pp_set_ovr(detect_prio);
849         pp_set_hwe(detect_prio);
850         pp_set_conf(detect_prio);
851         pp_set_default(detect_prio, DEFAULT_DETECT_PRIO);
852 out:
853         condlog(3, "%s: detect_prio = %s %s", pp->dev,
854                 (pp->detect_prio == DETECT_PRIO_ON)? "yes" : "no", origin);
855         return 0;
856 }
857
858 int select_detect_checker(struct config *conf, struct path *pp)
859 {
860         const char *origin;
861
862         pp_set_ovr(detect_checker);
863         pp_set_hwe(detect_checker);
864         pp_set_conf(detect_checker);
865         pp_set_default(detect_checker, DEFAULT_DETECT_CHECKER);
866 out:
867         condlog(3, "%s: detect_checker = %s %s", pp->dev,
868                 (pp->detect_checker == DETECT_CHECKER_ON)? "yes" : "no",
869                 origin);
870         return 0;
871 }
872
873 int select_deferred_remove(struct config *conf, struct multipath *mp)
874 {
875         const char *origin;
876
877 #ifndef LIBDM_API_DEFERRED
878         mp->deferred_remove = DEFERRED_REMOVE_OFF;
879         origin = "(setting: WARNING, not compiled with support)";
880         goto out;
881 #endif
882         if (mp->deferred_remove == DEFERRED_REMOVE_IN_PROGRESS) {
883                 condlog(3, "%s: deferred remove in progress", mp->alias);
884                 return 0;
885         }
886         mp_set_mpe(deferred_remove);
887         mp_set_ovr(deferred_remove);
888         mp_set_hwe(deferred_remove);
889         mp_set_conf(deferred_remove);
890         mp_set_default(deferred_remove, DEFAULT_DEFERRED_REMOVE);
891 out:
892         condlog(3, "%s: deferred_remove = %s %s", mp->alias,
893                 (mp->deferred_remove == DEFERRED_REMOVE_ON)? "yes" : "no",
894                 origin);
895         return 0;
896 }
897
898 static inline int san_path_check_options_set(const struct multipath *mp)
899 {
900         return mp->san_path_err_threshold > 0 ||
901                mp->san_path_err_forget_rate > 0 ||
902                mp->san_path_err_recovery_time > 0;
903 }
904
905 static int
906 use_delay_watch_checks(struct config *conf, struct multipath *mp)
907 {
908         int value = NU_UNDEF;
909         const char *origin = default_origin;
910         char buff[12];
911
912         do_set(delay_watch_checks, mp->mpe, value, multipaths_origin);
913         do_set(delay_watch_checks, conf->overrides, value, overrides_origin);
914         do_set_from_hwe(delay_watch_checks, mp, value, hwe_origin);
915         do_set(delay_watch_checks, conf, value, conf_origin);
916 out:
917         if (print_off_int_undef(buff, 12, value) != 0)
918                 condlog(3, "%s: delay_watch_checks = %s %s", mp->alias, buff,
919                         origin);
920         return value;
921 }
922
923 static int
924 use_delay_wait_checks(struct config *conf, struct multipath *mp)
925 {
926         int value = NU_UNDEF;
927         const char *origin = default_origin;
928         char buff[12];
929
930         do_set(delay_wait_checks, mp->mpe, value, multipaths_origin);
931         do_set(delay_wait_checks, conf->overrides, value, overrides_origin);
932         do_set_from_hwe(delay_wait_checks, mp, value, hwe_origin);
933         do_set(delay_wait_checks, conf, value, conf_origin);
934 out:
935         if (print_off_int_undef(buff, 12, value) != 0)
936                 condlog(3, "%s: delay_wait_checks = %s %s", mp->alias, buff,
937                         origin);
938         return value;
939 }
940
941 int select_delay_checks(struct config *conf, struct multipath *mp)
942 {
943         int watch_checks, wait_checks;
944         char buff[12];
945
946         watch_checks = use_delay_watch_checks(conf, mp);
947         wait_checks = use_delay_wait_checks(conf, mp);
948         if (watch_checks <= 0 && wait_checks <= 0)
949                 return 0;
950         if (san_path_check_options_set(mp)) {
951                 condlog(3, "%s: both marginal_path and delay_checks error detection options selected", mp->alias);
952                 condlog(3, "%s: ignoring delay_checks options", mp->alias);
953                 return 0;
954         }
955         mp->san_path_err_threshold = 1;
956         condlog(3, "%s: san_path_err_threshold = 1 %s", mp->alias,
957                 (watch_checks > 0)? delay_watch_origin : delay_wait_origin);
958         if (watch_checks > 0) {
959                 mp->san_path_err_forget_rate = watch_checks;
960                 print_off_int_undef(buff, 12, mp->san_path_err_forget_rate);
961                 condlog(3, "%s: san_path_err_forget_rate = %s %s", mp->alias,
962                         buff, delay_watch_origin);
963         }
964         if (wait_checks > 0) {
965                 mp->san_path_err_recovery_time = wait_checks *
966                                                  conf->max_checkint;
967                 print_off_int_undef(buff, 12, mp->san_path_err_recovery_time);
968                 condlog(3, "%s: san_path_err_recovery_time = %s %s", mp->alias,
969                         buff, delay_wait_origin);
970         }
971         return 0;
972 }
973
974 static int san_path_deprecated_warned;
975 #define warn_san_path_deprecated(v, x)                                  \
976         do {                                                            \
977                 if (v->x > 0 && !san_path_deprecated_warned) {          \
978                 san_path_deprecated_warned = 1;                         \
979                 condlog(1, "WARNING: option %s is deprecated, "         \
980                         "please use marginal_path options instead",     \
981                         #x);                                            \
982                 }                                                       \
983         } while(0)
984
985 int select_san_path_err_threshold(struct config *conf, struct multipath *mp)
986 {
987         const char *origin;
988         char buff[12];
989
990         if (marginal_path_check_enabled(mp)) {
991                 mp->san_path_err_threshold = NU_NO;
992                 origin = marginal_path_origin;
993                 goto out;
994         }
995         mp_set_mpe(san_path_err_threshold);
996         mp_set_ovr(san_path_err_threshold);
997         mp_set_hwe(san_path_err_threshold);
998         mp_set_conf(san_path_err_threshold);
999         mp_set_default(san_path_err_threshold, DEFAULT_ERR_CHECKS);
1000 out:
1001         if (print_off_int_undef(buff, 12, mp->san_path_err_threshold) != 0)
1002                 condlog(3, "%s: san_path_err_threshold = %s %s",
1003                         mp->alias, buff, origin);
1004         warn_san_path_deprecated(mp, san_path_err_threshold);
1005         return 0;
1006 }
1007
1008 int select_san_path_err_forget_rate(struct config *conf, struct multipath *mp)
1009 {
1010         const char *origin;
1011         char buff[12];
1012
1013         if (marginal_path_check_enabled(mp)) {
1014                 mp->san_path_err_forget_rate = NU_NO;
1015                 origin = marginal_path_origin;
1016                 goto out;
1017         }
1018         mp_set_mpe(san_path_err_forget_rate);
1019         mp_set_ovr(san_path_err_forget_rate);
1020         mp_set_hwe(san_path_err_forget_rate);
1021         mp_set_conf(san_path_err_forget_rate);
1022         mp_set_default(san_path_err_forget_rate, DEFAULT_ERR_CHECKS);
1023 out:
1024         if (print_off_int_undef(buff, 12, mp->san_path_err_forget_rate) != 0)
1025                 condlog(3, "%s: san_path_err_forget_rate = %s %s", mp->alias,
1026                         buff, origin);
1027         warn_san_path_deprecated(mp, san_path_err_forget_rate);
1028         return 0;
1029
1030 }
1031
1032 int select_san_path_err_recovery_time(struct config *conf, struct multipath *mp)
1033 {
1034         const char *origin;
1035         char buff[12];
1036
1037         if (marginal_path_check_enabled(mp)) {
1038                 mp->san_path_err_recovery_time = NU_NO;
1039                 origin = marginal_path_origin;
1040                 goto out;
1041         }
1042         mp_set_mpe(san_path_err_recovery_time);
1043         mp_set_ovr(san_path_err_recovery_time);
1044         mp_set_hwe(san_path_err_recovery_time);
1045         mp_set_conf(san_path_err_recovery_time);
1046         mp_set_default(san_path_err_recovery_time, DEFAULT_ERR_CHECKS);
1047 out:
1048         if (print_off_int_undef(buff, 12, mp->san_path_err_recovery_time) != 0)
1049                 condlog(3, "%s: san_path_err_recovery_time = %s %s", mp->alias,
1050                         buff, origin);
1051         warn_san_path_deprecated(mp, san_path_err_recovery_time);
1052         return 0;
1053
1054 }
1055
1056 int select_marginal_path_err_sample_time(struct config *conf, struct multipath *mp)
1057 {
1058         const char *origin;
1059         char buff[12];
1060
1061         mp_set_mpe(marginal_path_err_sample_time);
1062         mp_set_ovr(marginal_path_err_sample_time);
1063         mp_set_hwe(marginal_path_err_sample_time);
1064         mp_set_conf(marginal_path_err_sample_time);
1065         mp_set_default(marginal_path_err_sample_time, DEFAULT_ERR_CHECKS);
1066 out:
1067         if (print_off_int_undef(buff, 12, mp->marginal_path_err_sample_time)
1068             != 0)
1069                 condlog(3, "%s: marginal_path_err_sample_time = %s %s",
1070                         mp->alias, buff, origin);
1071         return 0;
1072 }
1073
1074 int select_marginal_path_err_rate_threshold(struct config *conf, struct multipath *mp)
1075 {
1076         const char *origin;
1077         char buff[12];
1078
1079         mp_set_mpe(marginal_path_err_rate_threshold);
1080         mp_set_ovr(marginal_path_err_rate_threshold);
1081         mp_set_hwe(marginal_path_err_rate_threshold);
1082         mp_set_conf(marginal_path_err_rate_threshold);
1083         mp_set_default(marginal_path_err_rate_threshold, DEFAULT_ERR_CHECKS);
1084 out:
1085         if (print_off_int_undef(buff, 12, mp->marginal_path_err_rate_threshold)
1086             != 0)
1087                 condlog(3, "%s: marginal_path_err_rate_threshold = %s %s",
1088                         mp->alias, buff, origin);
1089         return 0;
1090 }
1091
1092 int select_marginal_path_err_recheck_gap_time(struct config *conf, struct multipath *mp)
1093 {
1094         const char *origin;
1095         char buff[12];
1096
1097         mp_set_mpe(marginal_path_err_recheck_gap_time);
1098         mp_set_ovr(marginal_path_err_recheck_gap_time);
1099         mp_set_hwe(marginal_path_err_recheck_gap_time);
1100         mp_set_conf(marginal_path_err_recheck_gap_time);
1101         mp_set_default(marginal_path_err_recheck_gap_time, DEFAULT_ERR_CHECKS);
1102 out:
1103         if (print_off_int_undef(buff, 12,
1104                                 mp->marginal_path_err_recheck_gap_time) != 0)
1105                 condlog(3, "%s: marginal_path_err_recheck_gap_time = %s %s",
1106                         mp->alias, buff, origin);
1107         return 0;
1108 }
1109
1110 int select_marginal_path_double_failed_time(struct config *conf, struct multipath *mp)
1111 {
1112         const char *origin;
1113         char buff[12];
1114
1115         mp_set_mpe(marginal_path_double_failed_time);
1116         mp_set_ovr(marginal_path_double_failed_time);
1117         mp_set_hwe(marginal_path_double_failed_time);
1118         mp_set_conf(marginal_path_double_failed_time);
1119         mp_set_default(marginal_path_double_failed_time, DEFAULT_ERR_CHECKS);
1120 out:
1121         if (print_off_int_undef(buff, 12, mp->marginal_path_double_failed_time)
1122             != 0)
1123                 condlog(3, "%s: marginal_path_double_failed_time = %s %s",
1124                         mp->alias, buff, origin);
1125         return 0;
1126 }
1127
1128 int select_skip_kpartx (struct config *conf, struct multipath * mp)
1129 {
1130         const char *origin;
1131
1132         mp_set_mpe(skip_kpartx);
1133         mp_set_ovr(skip_kpartx);
1134         mp_set_hwe(skip_kpartx);
1135         mp_set_conf(skip_kpartx);
1136         mp_set_default(skip_kpartx, DEFAULT_SKIP_KPARTX);
1137 out:
1138         condlog(3, "%s: skip_kpartx = %s %s", mp->alias,
1139                 (mp->skip_kpartx == SKIP_KPARTX_ON)? "yes" : "no",
1140                 origin);
1141         return 0;
1142 }
1143
1144 int select_max_sectors_kb(struct config *conf, struct multipath * mp)
1145 {
1146         const char *origin;
1147
1148         mp_set_mpe(max_sectors_kb);
1149         mp_set_ovr(max_sectors_kb);
1150         mp_set_hwe(max_sectors_kb);
1151         mp_set_conf(max_sectors_kb);
1152         mp_set_default(max_sectors_kb, DEFAULT_MAX_SECTORS_KB);
1153         /*
1154          * In the default case, we will not modify max_sectors_kb in sysfs
1155          * (see sysfs_set_max_sectors_kb()).
1156          * Don't print a log message here to avoid user confusion.
1157          */
1158         return 0;
1159 out:
1160         condlog(3, "%s: max_sectors_kb = %i %s", mp->alias, mp->max_sectors_kb,
1161                 origin);
1162         return 0;
1163 }
1164
1165 int select_ghost_delay (struct config *conf, struct multipath * mp)
1166 {
1167         const char *origin;
1168         char buff[12];
1169
1170         mp_set_mpe(ghost_delay);
1171         mp_set_ovr(ghost_delay);
1172         mp_set_hwe(ghost_delay);
1173         mp_set_conf(ghost_delay);
1174         mp_set_default(ghost_delay, DEFAULT_GHOST_DELAY);
1175 out:
1176         if (print_off_int_undef(buff, 12, mp->ghost_delay) != 0)
1177                 condlog(3, "%s: ghost_delay = %s %s", mp->alias, buff, origin);
1178         return 0;
1179 }
1180
1181 int select_find_multipaths_timeout(struct config *conf, struct path *pp)
1182 {
1183         const char *origin;
1184
1185         pp_set_conf(find_multipaths_timeout);
1186         pp_set_default(find_multipaths_timeout,
1187                        DEFAULT_FIND_MULTIPATHS_TIMEOUT);
1188 out:
1189         /*
1190          * If configured value is negative, and this "unknown" hardware
1191          * (no hwentry), use very small timeout to avoid delays.
1192          */
1193         if (pp->find_multipaths_timeout < 0) {
1194                 pp->find_multipaths_timeout = -pp->find_multipaths_timeout;
1195                 if (!pp->hwe) {
1196                         pp->find_multipaths_timeout =
1197                                 DEFAULT_UNKNOWN_FIND_MULTIPATHS_TIMEOUT;
1198                         origin = "(default for unknown hardware)";
1199                 }
1200         }
1201         condlog(3, "%s: timeout for find_multipaths \"smart\" = %ds %s",
1202                 pp->dev, pp->find_multipaths_timeout, origin);
1203         return 0;
1204 }
1205
1206 int select_all_tg_pt (struct config *conf, struct multipath * mp)
1207 {
1208         const char *origin;
1209
1210         mp_set_ovr(all_tg_pt);
1211         mp_set_hwe(all_tg_pt);
1212         mp_set_conf(all_tg_pt);
1213         mp_set_default(all_tg_pt, DEFAULT_ALL_TG_PT);
1214 out:
1215         condlog(3, "%s: all_tg_pt = %s %s", mp->alias,
1216                 (mp->all_tg_pt == ALL_TG_PT_ON)? "yes" : "no",
1217                 origin);
1218         return 0;
1219 }
1220
1221 int select_vpd_vendor_id (struct path *pp)
1222 {
1223         const char *origin;
1224
1225         pp_set_hwe(vpd_vendor_id);
1226         pp_set_default(vpd_vendor_id, 0);
1227 out:
1228         if (pp->vpd_vendor_id < 0 || pp->vpd_vendor_id >= VPD_VP_ARRAY_SIZE) {
1229                 condlog(3, "%s: vpd_vendor_id = %d (invalid, setting to 0)",
1230                         pp->dev, pp->vpd_vendor_id);
1231                 pp->vpd_vendor_id = 0;
1232         }
1233         condlog(3, "%s: vpd_vendor_id = %d \"%s\" %s", pp->dev,
1234                 pp->vpd_vendor_id, vpd_vendor_pages[pp->vpd_vendor_id].name,
1235                 origin);
1236         return 0;
1237 }