libmutipath: deprecate delay_*_checks
[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;
436
437         dh_state = &handler[2];
438
439         vector_foreach_slot(mp->paths, pp, i)
440                 all_tpgs = all_tpgs && (path_get_tpgs(pp) > 0);
441         if (mp->retain_hwhandler != RETAIN_HWHANDLER_OFF) {
442                 vector_foreach_slot(mp->paths, pp, i) {
443                         if (get_dh_state(pp, dh_state, sizeof(handler) - 2) > 0
444                             && strcmp(dh_state, "detached")) {
445                                 memcpy(handler, "1 ", 2);
446                                 mp->hwhandler = handler;
447                                 origin = "(setting: retained by kernel driver)";
448                                 goto out;
449                         }
450                 }
451         }
452
453         mp_set_hwe(hwhandler);
454         mp_set_conf(hwhandler);
455         mp_set_default(hwhandler, DEFAULT_HWHANDLER);
456 out:
457         if (all_tpgs && !strcmp(mp->hwhandler, DEFAULT_HWHANDLER) &&
458                 origin == default_origin) {
459                 mp->hwhandler = alua_name;
460                 origin = tpgs_origin;
461         } else if (!all_tpgs && !strcmp(mp->hwhandler, alua_name)) {
462                 mp->hwhandler = DEFAULT_HWHANDLER;
463                 origin = tpgs_origin;
464         }
465         mp->hwhandler = STRDUP(mp->hwhandler);
466         condlog(3, "%s: hardware_handler = \"%s\" %s", mp->alias, mp->hwhandler,
467                 origin);
468         return 0;
469 }
470
471 /*
472  * Current RDAC (NetApp E-Series) firmware relies
473  * on periodic REPORT TARGET PORT GROUPS for
474  * internal load balancing.
475  * Using the sysfs priority checker defeats this purpose.
476  *
477  * Moreover, NetApp would also prefer the RDAC checker over ALUA.
478  * (https://www.redhat.com/archives/dm-devel/2017-September/msg00326.html)
479  */
480 static int
481 check_rdac(struct path * pp)
482 {
483         int len;
484         char buff[44];
485         const char *checker_name;
486
487         if (pp->bus != SYSFS_BUS_SCSI)
488                 return 0;
489         /* Avoid ioctl if this is likely not an RDAC array */
490         if (__do_set_from_hwe(checker_name, pp, checker_name) &&
491             strcmp(checker_name, RDAC))
492                 return 0;
493         len = get_vpd_sgio(pp->fd, 0xC9, buff, 44);
494         if (len <= 0)
495                 return 0;
496         return !(memcmp(buff + 4, "vac1", 4));
497 }
498
499 int select_checker(struct config *conf, struct path *pp)
500 {
501         const char *origin;
502         char *ckr_name;
503         struct checker * c = &pp->checker;
504
505         if (pp->detect_checker == DETECT_CHECKER_ON) {
506                 origin = autodetect_origin;
507                 if (check_rdac(pp)) {
508                         ckr_name = RDAC;
509                         goto out;
510                 } else if (path_get_tpgs(pp) != TPGS_NONE) {
511                         ckr_name = TUR;
512                         goto out;
513                 }
514         }
515         do_set(checker_name, conf->overrides, ckr_name, overrides_origin);
516         do_set_from_hwe(checker_name, pp, ckr_name, hwe_origin);
517         do_set(checker_name, conf, ckr_name, conf_origin);
518         do_default(ckr_name, DEFAULT_CHECKER);
519 out:
520         checker_get(conf->multipath_dir, c, ckr_name);
521         condlog(3, "%s: path_checker = %s %s", pp->dev,
522                 checker_name(c), origin);
523         if (conf->checker_timeout) {
524                 c->timeout = conf->checker_timeout;
525                 condlog(3, "%s: checker timeout = %u s %s",
526                         pp->dev, c->timeout, conf_origin);
527         }
528         else if (sysfs_get_timeout(pp, &c->timeout) > 0)
529                 condlog(3, "%s: checker timeout = %u s (setting: kernel sysfs)",
530                         pp->dev, c->timeout);
531         else {
532                 c->timeout = DEF_TIMEOUT;
533                 condlog(3, "%s: checker timeout = %u s %s",
534                         pp->dev, c->timeout, default_origin);
535         }
536         return 0;
537 }
538
539 int select_getuid(struct config *conf, struct path *pp)
540 {
541         const char *origin;
542
543         pp->uid_attribute = get_uid_attribute_by_attrs(conf, pp->dev);
544         if (pp->uid_attribute) {
545                 origin = "(setting: multipath.conf defaults section / uid_attrs)";
546                 goto out;
547         }
548
549         pp_set_ovr(getuid);
550         pp_set_ovr(uid_attribute);
551         pp_set_hwe(getuid);
552         pp_set_hwe(uid_attribute);
553         pp_set_conf(getuid);
554         pp_set_conf(uid_attribute);
555         pp_set_default(uid_attribute, DEFAULT_UID_ATTRIBUTE);
556 out:
557         if (pp->uid_attribute)
558                 condlog(3, "%s: uid_attribute = %s %s", pp->dev,
559                         pp->uid_attribute, origin);
560         else if (pp->getuid)
561                 condlog(3, "%s: getuid = \"%s\" %s", pp->dev, pp->getuid,
562                         origin);
563         return 0;
564 }
565
566 void
567 detect_prio(struct config *conf, struct path * pp)
568 {
569         struct prio *p = &pp->prio;
570         char buff[512];
571         char *default_prio;
572         int tpgs;
573
574         switch(pp->bus) {
575         case SYSFS_BUS_NVME:
576                 if (nvme_id_ctrl_ana(pp->fd, NULL) == 0)
577                         return;
578                 default_prio = PRIO_ANA;
579                 break;
580         case SYSFS_BUS_SCSI:
581                 tpgs = path_get_tpgs(pp);
582                 if (tpgs == TPGS_NONE)
583                         return;
584                 if ((tpgs == TPGS_EXPLICIT || !check_rdac(pp)) &&
585                     sysfs_get_asymmetric_access_state(pp, buff, 512) >= 0)
586                         default_prio = PRIO_SYSFS;
587                 else
588                         default_prio = PRIO_ALUA;
589                 break;
590         default:
591                 return;
592         }
593         prio_get(conf->multipath_dir, p, default_prio, DEFAULT_PRIO_ARGS);
594 }
595
596 #define set_prio(dir, src, msg)                                 \
597 do {                                                                    \
598         if (src && src->prio_name) {                                    \
599                 prio_get(dir, p, src->prio_name, src->prio_args);       \
600                 origin = msg;                                           \
601                 goto out;                                               \
602         }                                                               \
603 } while(0)
604
605 #define set_prio_from_vec(type, dir, src, msg, p)                       \
606 do {                                                                    \
607         type *_p;                                                       \
608         int i;                                                          \
609         char *prio_name = NULL, *prio_args = NULL;                      \
610                                                                         \
611         vector_foreach_slot(src, _p, i) {                               \
612                 if (prio_name == NULL && _p->prio_name)         \
613                         prio_name = _p->prio_name;                      \
614                 if (prio_args == NULL && _p->prio_args)         \
615                         prio_args = _p->prio_args;                      \
616         }                                                               \
617         if (prio_name != NULL) {                                        \
618                 prio_get(dir, p, prio_name, prio_args);                 \
619                 origin = msg;                                           \
620                 goto out;                                               \
621         }                                                               \
622 } while (0)
623
624 int select_prio(struct config *conf, struct path *pp)
625 {
626         const char *origin;
627         struct mpentry * mpe;
628         struct prio * p = &pp->prio;
629         int log_prio = 3;
630
631         if (pp->detect_prio == DETECT_PRIO_ON) {
632                 detect_prio(conf, pp);
633                 if (prio_selected(p)) {
634                         origin = autodetect_origin;
635                         goto out;
636                 }
637         }
638         mpe = find_mpe(conf->mptable, pp->wwid);
639         set_prio(conf->multipath_dir, mpe, multipaths_origin);
640         set_prio(conf->multipath_dir, conf->overrides, overrides_origin);
641         set_prio_from_vec(struct hwentry, conf->multipath_dir,
642                           pp->hwe, hwe_origin, p);
643         set_prio(conf->multipath_dir, conf, conf_origin);
644         prio_get(conf->multipath_dir, p, DEFAULT_PRIO, DEFAULT_PRIO_ARGS);
645         origin = default_origin;
646 out:
647         /*
648          * fetch tpgs mode for alua, if its not already obtained
649          */
650         if (!strncmp(prio_name(p), PRIO_ALUA, PRIO_NAME_LEN)) {
651                 int tpgs = path_get_tpgs(pp);
652
653                 if (tpgs == TPGS_NONE) {
654                         prio_get(conf->multipath_dir,
655                                  p, DEFAULT_PRIO, DEFAULT_PRIO_ARGS);
656                         origin = "(setting: emergency fallback - alua failed)";
657                         log_prio = 1;
658                 }
659         }
660         condlog(log_prio, "%s: prio = %s %s", pp->dev, prio_name(p), origin);
661         condlog(3, "%s: prio args = \"%s\" %s", pp->dev, prio_args(p), origin);
662         return 0;
663 }
664
665 int select_no_path_retry(struct config *conf, struct multipath *mp)
666 {
667         const char *origin = NULL;
668         char buff[12];
669
670         if (mp->disable_queueing) {
671                 condlog(0, "%s: queueing disabled", mp->alias);
672                 mp->no_path_retry = NO_PATH_RETRY_FAIL;
673                 return 0;
674         }
675         mp_set_mpe(no_path_retry);
676         mp_set_ovr(no_path_retry);
677         mp_set_hwe(no_path_retry);
678         mp_set_conf(no_path_retry);
679 out:
680         print_no_path_retry(buff, 12, mp->no_path_retry);
681         if (origin)
682                 condlog(3, "%s: no_path_retry = %s %s", mp->alias, buff,
683                         origin);
684         else
685                 condlog(3, "%s: no_path_retry = undef %s",
686                         mp->alias, default_origin);
687         return 0;
688 }
689
690 int
691 select_minio_rq (struct config *conf, struct multipath * mp)
692 {
693         const char *origin;
694
695         do_set(minio_rq, mp->mpe, mp->minio, multipaths_origin);
696         do_set(minio_rq, conf->overrides, mp->minio, overrides_origin);
697         do_set_from_hwe(minio_rq, mp, mp->minio, hwe_origin);
698         do_set(minio_rq, conf, mp->minio, conf_origin);
699         do_default(mp->minio, DEFAULT_MINIO_RQ);
700 out:
701         condlog(3, "%s: minio = %i %s", mp->alias, mp->minio, origin);
702         return 0;
703 }
704
705 int
706 select_minio_bio (struct config *conf, struct multipath * mp)
707 {
708         const char *origin;
709
710         mp_set_mpe(minio);
711         mp_set_ovr(minio);
712         mp_set_hwe(minio);
713         mp_set_conf(minio);
714         mp_set_default(minio, DEFAULT_MINIO);
715 out:
716         condlog(3, "%s: minio = %i %s", mp->alias, mp->minio, origin);
717         return 0;
718 }
719
720 int select_minio(struct config *conf, struct multipath *mp)
721 {
722         unsigned int minv_dmrq[3] = {1, 1, 0};
723
724         if (VERSION_GE(conf->version, minv_dmrq))
725                 return select_minio_rq(conf, mp);
726         else
727                 return select_minio_bio(conf, mp);
728 }
729
730 int select_fast_io_fail(struct config *conf, struct multipath *mp)
731 {
732         const char *origin;
733         char buff[12];
734
735         mp_set_ovr(fast_io_fail);
736         mp_set_hwe(fast_io_fail);
737         mp_set_conf(fast_io_fail);
738         mp_set_default(fast_io_fail, DEFAULT_FAST_IO_FAIL);
739 out:
740         print_fast_io_fail(buff, 12, mp->fast_io_fail);
741         condlog(3, "%s: fast_io_fail_tmo = %s %s", mp->alias, buff, origin);
742         return 0;
743 }
744
745 int select_dev_loss(struct config *conf, struct multipath *mp)
746 {
747         const char *origin;
748         char buff[12];
749
750         mp_set_ovr(dev_loss);
751         mp_set_hwe(dev_loss);
752         mp_set_conf(dev_loss);
753         mp->dev_loss = 0;
754         return 0;
755 out:
756         print_dev_loss(buff, 12, mp->dev_loss);
757         condlog(3, "%s: dev_loss_tmo = %s %s", mp->alias, buff, origin);
758         return 0;
759 }
760
761 int select_flush_on_last_del(struct config *conf, struct multipath *mp)
762 {
763         const char *origin;
764
765         mp_set_mpe(flush_on_last_del);
766         mp_set_ovr(flush_on_last_del);
767         mp_set_hwe(flush_on_last_del);
768         mp_set_conf(flush_on_last_del);
769         mp_set_default(flush_on_last_del, DEFAULT_FLUSH);
770 out:
771         condlog(3, "%s: flush_on_last_del = %s %s", mp->alias,
772                 (mp->flush_on_last_del == FLUSH_ENABLED)? "yes" : "no", origin);
773         return 0;
774 }
775
776 int select_reservation_key(struct config *conf, struct multipath *mp)
777 {
778         const char *origin;
779         char buff[PRKEY_SIZE];
780         char *from_file = "";
781         uint64_t prkey = 0;
782
783         do_prkey_set(mp->mpe, multipaths_origin);
784         do_prkey_set(conf, conf_origin);
785         put_be64(mp->reservation_key, 0);
786         mp->sa_flags = 0;
787         mp->prkey_source = PRKEY_SOURCE_NONE;
788         return 0;
789 out:
790         if (mp->prkey_source == PRKEY_SOURCE_FILE) {
791                 from_file = " (from prkeys file)";
792                 if (get_prkey(conf, mp, &prkey, &mp->sa_flags) != 0)
793                         put_be64(mp->reservation_key, 0);
794                 else
795                         put_be64(mp->reservation_key, prkey);
796         }
797         print_reservation_key(buff, PRKEY_SIZE, mp->reservation_key,
798                               mp->sa_flags, mp->prkey_source);
799         condlog(3, "%s: reservation_key = %s %s%s", mp->alias, buff, origin,
800                 from_file);
801         return 0;
802 }
803
804 int select_retain_hwhandler(struct config *conf, struct multipath *mp)
805 {
806         const char *origin;
807         unsigned int minv_dm_retain[3] = {1, 5, 0};
808
809         if (!VERSION_GE(conf->version, minv_dm_retain)) {
810                 mp->retain_hwhandler = RETAIN_HWHANDLER_OFF;
811                 origin = "(setting: WARNING, requires kernel dm-mpath version >= 1.5.0)";
812                 goto out;
813         }
814         if (get_linux_version_code() >= KERNEL_VERSION(4, 3, 0)) {
815                 mp->retain_hwhandler = RETAIN_HWHANDLER_ON;
816                 origin = "(setting: implied in kernel >= 4.3.0)";
817                 goto out;
818         }
819         mp_set_ovr(retain_hwhandler);
820         mp_set_hwe(retain_hwhandler);
821         mp_set_conf(retain_hwhandler);
822         mp_set_default(retain_hwhandler, DEFAULT_RETAIN_HWHANDLER);
823 out:
824         condlog(3, "%s: retain_attached_hw_handler = %s %s", mp->alias,
825                 (mp->retain_hwhandler == RETAIN_HWHANDLER_ON)? "yes" : "no",
826                 origin);
827         return 0;
828 }
829
830 int select_detect_prio(struct config *conf, struct path *pp)
831 {
832         const char *origin;
833
834         pp_set_ovr(detect_prio);
835         pp_set_hwe(detect_prio);
836         pp_set_conf(detect_prio);
837         pp_set_default(detect_prio, DEFAULT_DETECT_PRIO);
838 out:
839         condlog(3, "%s: detect_prio = %s %s", pp->dev,
840                 (pp->detect_prio == DETECT_PRIO_ON)? "yes" : "no", origin);
841         return 0;
842 }
843
844 int select_detect_checker(struct config *conf, struct path *pp)
845 {
846         const char *origin;
847
848         pp_set_ovr(detect_checker);
849         pp_set_hwe(detect_checker);
850         pp_set_conf(detect_checker);
851         pp_set_default(detect_checker, DEFAULT_DETECT_CHECKER);
852 out:
853         condlog(3, "%s: detect_checker = %s %s", pp->dev,
854                 (pp->detect_checker == DETECT_CHECKER_ON)? "yes" : "no",
855                 origin);
856         return 0;
857 }
858
859 int select_deferred_remove(struct config *conf, struct multipath *mp)
860 {
861         const char *origin;
862
863 #ifndef LIBDM_API_DEFERRED
864         mp->deferred_remove = DEFERRED_REMOVE_OFF;
865         origin = "(setting: WARNING, not compiled with support)";
866         goto out;
867 #endif
868         if (mp->deferred_remove == DEFERRED_REMOVE_IN_PROGRESS) {
869                 condlog(3, "%s: deferred remove in progress", mp->alias);
870                 return 0;
871         }
872         mp_set_mpe(deferred_remove);
873         mp_set_ovr(deferred_remove);
874         mp_set_hwe(deferred_remove);
875         mp_set_conf(deferred_remove);
876         mp_set_default(deferred_remove, DEFAULT_DEFERRED_REMOVE);
877 out:
878         condlog(3, "%s: deferred_remove = %s %s", mp->alias,
879                 (mp->deferred_remove == DEFERRED_REMOVE_ON)? "yes" : "no",
880                 origin);
881         return 0;
882 }
883
884 static inline int san_path_check_options_set(const struct multipath *mp)
885 {
886         return mp->san_path_err_threshold > 0 ||
887                mp->san_path_err_forget_rate > 0 ||
888                mp->san_path_err_recovery_time > 0;
889 }
890
891 static int
892 use_delay_watch_checks(struct config *conf, struct multipath *mp)
893 {
894         int value = NU_UNDEF;
895         const char *origin = default_origin;
896         char buff[12];
897
898         do_set(delay_watch_checks, mp->mpe, value, multipaths_origin);
899         do_set(delay_watch_checks, conf->overrides, value, overrides_origin);
900         do_set_from_hwe(delay_watch_checks, mp, value, hwe_origin);
901         do_set(delay_watch_checks, conf, value, conf_origin);
902 out:
903         if (print_off_int_undef(buff, 12, value) != 0)
904                 condlog(3, "%s: delay_watch_checks = %s %s", mp->alias, buff,
905                         origin);
906         return value;
907 }
908
909 static int
910 use_delay_wait_checks(struct config *conf, struct multipath *mp)
911 {
912         int value = NU_UNDEF;
913         const char *origin = default_origin;
914         char buff[12];
915
916         do_set(delay_wait_checks, mp->mpe, value, multipaths_origin);
917         do_set(delay_wait_checks, conf->overrides, value, overrides_origin);
918         do_set_from_hwe(delay_wait_checks, mp, value, hwe_origin);
919         do_set(delay_wait_checks, conf, value, conf_origin);
920 out:
921         if (print_off_int_undef(buff, 12, value) != 0)
922                 condlog(3, "%s: delay_wait_checks = %s %s", mp->alias, buff,
923                         origin);
924         return value;
925 }
926
927 int select_delay_checks(struct config *conf, struct multipath *mp)
928 {
929         int watch_checks, wait_checks;
930         char buff[12];
931
932         watch_checks = use_delay_watch_checks(conf, mp);
933         wait_checks = use_delay_wait_checks(conf, mp);
934         if (watch_checks <= 0 && wait_checks <= 0)
935                 return 0;
936         if (san_path_check_options_set(mp)) {
937                 condlog(3, "%s: both marginal_path and delay_checks error detection options selected", mp->alias);
938                 condlog(3, "%s: ignoring delay_checks options", mp->alias);
939                 return 0;
940         }
941         mp->san_path_err_threshold = 1;
942         condlog(3, "%s: san_path_err_threshold = 1 %s", mp->alias,
943                 (watch_checks > 0)? delay_watch_origin : delay_wait_origin);
944         if (watch_checks > 0) {
945                 mp->san_path_err_forget_rate = watch_checks;
946                 print_off_int_undef(buff, 12, mp->san_path_err_forget_rate);
947                 condlog(3, "%s: san_path_err_forget_rate = %s %s", mp->alias,
948                         buff, delay_watch_origin);
949         }
950         if (wait_checks > 0) {
951                 mp->san_path_err_recovery_time = wait_checks *
952                                                  conf->max_checkint;
953                 print_off_int_undef(buff, 12, mp->san_path_err_recovery_time);
954                 condlog(3, "%s: san_path_err_recovery_time = %s %s", mp->alias,
955                         buff, delay_wait_origin);
956         }
957         return 0;
958 }
959
960 static int san_path_deprecated_warned;
961 #define warn_san_path_deprecated(v, x)                                  \
962         do {                                                            \
963                 if (v->x > 0 && !san_path_deprecated_warned) {          \
964                 san_path_deprecated_warned = 1;                         \
965                 condlog(1, "WARNING: option %s is deprecated, "         \
966                         "please use marginal_path options instead",     \
967                         #x);                                            \
968                 }                                                       \
969         } while(0)
970
971 int select_san_path_err_threshold(struct config *conf, struct multipath *mp)
972 {
973         const char *origin;
974         char buff[12];
975
976         if (marginal_path_check_enabled(mp)) {
977                 mp->san_path_err_threshold = NU_NO;
978                 origin = marginal_path_origin;
979                 goto out;
980         }
981         mp_set_mpe(san_path_err_threshold);
982         mp_set_ovr(san_path_err_threshold);
983         mp_set_hwe(san_path_err_threshold);
984         mp_set_conf(san_path_err_threshold);
985         mp_set_default(san_path_err_threshold, DEFAULT_ERR_CHECKS);
986 out:
987         if (print_off_int_undef(buff, 12, mp->san_path_err_threshold) != 0)
988                 condlog(3, "%s: san_path_err_threshold = %s %s",
989                         mp->alias, buff, origin);
990         warn_san_path_deprecated(mp, san_path_err_threshold);
991         return 0;
992 }
993
994 int select_san_path_err_forget_rate(struct config *conf, struct multipath *mp)
995 {
996         const char *origin;
997         char buff[12];
998
999         if (marginal_path_check_enabled(mp)) {
1000                 mp->san_path_err_forget_rate = NU_NO;
1001                 origin = marginal_path_origin;
1002                 goto out;
1003         }
1004         mp_set_mpe(san_path_err_forget_rate);
1005         mp_set_ovr(san_path_err_forget_rate);
1006         mp_set_hwe(san_path_err_forget_rate);
1007         mp_set_conf(san_path_err_forget_rate);
1008         mp_set_default(san_path_err_forget_rate, DEFAULT_ERR_CHECKS);
1009 out:
1010         if (print_off_int_undef(buff, 12, mp->san_path_err_forget_rate) != 0)
1011                 condlog(3, "%s: san_path_err_forget_rate = %s %s", mp->alias,
1012                         buff, origin);
1013         warn_san_path_deprecated(mp, san_path_err_forget_rate);
1014         return 0;
1015
1016 }
1017
1018 int select_san_path_err_recovery_time(struct config *conf, struct multipath *mp)
1019 {
1020         const char *origin;
1021         char buff[12];
1022
1023         if (marginal_path_check_enabled(mp)) {
1024                 mp->san_path_err_recovery_time = NU_NO;
1025                 origin = marginal_path_origin;
1026                 goto out;
1027         }
1028         mp_set_mpe(san_path_err_recovery_time);
1029         mp_set_ovr(san_path_err_recovery_time);
1030         mp_set_hwe(san_path_err_recovery_time);
1031         mp_set_conf(san_path_err_recovery_time);
1032         mp_set_default(san_path_err_recovery_time, DEFAULT_ERR_CHECKS);
1033 out:
1034         if (print_off_int_undef(buff, 12, mp->san_path_err_recovery_time) != 0)
1035                 condlog(3, "%s: san_path_err_recovery_time = %s %s", mp->alias,
1036                         buff, origin);
1037         warn_san_path_deprecated(mp, san_path_err_recovery_time);
1038         return 0;
1039
1040 }
1041
1042 int select_marginal_path_err_sample_time(struct config *conf, struct multipath *mp)
1043 {
1044         const char *origin;
1045         char buff[12];
1046
1047         mp_set_mpe(marginal_path_err_sample_time);
1048         mp_set_ovr(marginal_path_err_sample_time);
1049         mp_set_hwe(marginal_path_err_sample_time);
1050         mp_set_conf(marginal_path_err_sample_time);
1051         mp_set_default(marginal_path_err_sample_time, DEFAULT_ERR_CHECKS);
1052 out:
1053         if (print_off_int_undef(buff, 12, mp->marginal_path_err_sample_time)
1054             != 0)
1055                 condlog(3, "%s: marginal_path_err_sample_time = %s %s",
1056                         mp->alias, buff, origin);
1057         return 0;
1058 }
1059
1060 int select_marginal_path_err_rate_threshold(struct config *conf, struct multipath *mp)
1061 {
1062         const char *origin;
1063         char buff[12];
1064
1065         mp_set_mpe(marginal_path_err_rate_threshold);
1066         mp_set_ovr(marginal_path_err_rate_threshold);
1067         mp_set_hwe(marginal_path_err_rate_threshold);
1068         mp_set_conf(marginal_path_err_rate_threshold);
1069         mp_set_default(marginal_path_err_rate_threshold, DEFAULT_ERR_CHECKS);
1070 out:
1071         if (print_off_int_undef(buff, 12, mp->marginal_path_err_rate_threshold)
1072             != 0)
1073                 condlog(3, "%s: marginal_path_err_rate_threshold = %s %s",
1074                         mp->alias, buff, origin);
1075         return 0;
1076 }
1077
1078 int select_marginal_path_err_recheck_gap_time(struct config *conf, struct multipath *mp)
1079 {
1080         const char *origin;
1081         char buff[12];
1082
1083         mp_set_mpe(marginal_path_err_recheck_gap_time);
1084         mp_set_ovr(marginal_path_err_recheck_gap_time);
1085         mp_set_hwe(marginal_path_err_recheck_gap_time);
1086         mp_set_conf(marginal_path_err_recheck_gap_time);
1087         mp_set_default(marginal_path_err_recheck_gap_time, DEFAULT_ERR_CHECKS);
1088 out:
1089         if (print_off_int_undef(buff, 12,
1090                                 mp->marginal_path_err_recheck_gap_time) != 0)
1091                 condlog(3, "%s: marginal_path_err_recheck_gap_time = %s %s",
1092                         mp->alias, buff, origin);
1093         return 0;
1094 }
1095
1096 int select_marginal_path_double_failed_time(struct config *conf, struct multipath *mp)
1097 {
1098         const char *origin;
1099         char buff[12];
1100
1101         mp_set_mpe(marginal_path_double_failed_time);
1102         mp_set_ovr(marginal_path_double_failed_time);
1103         mp_set_hwe(marginal_path_double_failed_time);
1104         mp_set_conf(marginal_path_double_failed_time);
1105         mp_set_default(marginal_path_double_failed_time, DEFAULT_ERR_CHECKS);
1106 out:
1107         if (print_off_int_undef(buff, 12, mp->marginal_path_double_failed_time)
1108             != 0)
1109                 condlog(3, "%s: marginal_path_double_failed_time = %s %s",
1110                         mp->alias, buff, origin);
1111         return 0;
1112 }
1113
1114 int select_skip_kpartx (struct config *conf, struct multipath * mp)
1115 {
1116         const char *origin;
1117
1118         mp_set_mpe(skip_kpartx);
1119         mp_set_ovr(skip_kpartx);
1120         mp_set_hwe(skip_kpartx);
1121         mp_set_conf(skip_kpartx);
1122         mp_set_default(skip_kpartx, DEFAULT_SKIP_KPARTX);
1123 out:
1124         condlog(3, "%s: skip_kpartx = %s %s", mp->alias,
1125                 (mp->skip_kpartx == SKIP_KPARTX_ON)? "yes" : "no",
1126                 origin);
1127         return 0;
1128 }
1129
1130 int select_max_sectors_kb(struct config *conf, struct multipath * mp)
1131 {
1132         const char *origin;
1133
1134         mp_set_mpe(max_sectors_kb);
1135         mp_set_ovr(max_sectors_kb);
1136         mp_set_hwe(max_sectors_kb);
1137         mp_set_conf(max_sectors_kb);
1138         mp_set_default(max_sectors_kb, DEFAULT_MAX_SECTORS_KB);
1139         /*
1140          * In the default case, we will not modify max_sectors_kb in sysfs
1141          * (see sysfs_set_max_sectors_kb()).
1142          * Don't print a log message here to avoid user confusion.
1143          */
1144         return 0;
1145 out:
1146         condlog(3, "%s: max_sectors_kb = %i %s", mp->alias, mp->max_sectors_kb,
1147                 origin);
1148         return 0;
1149 }
1150
1151 int select_ghost_delay (struct config *conf, struct multipath * mp)
1152 {
1153         const char *origin;
1154         char buff[12];
1155
1156         mp_set_mpe(ghost_delay);
1157         mp_set_ovr(ghost_delay);
1158         mp_set_hwe(ghost_delay);
1159         mp_set_conf(ghost_delay);
1160         mp_set_default(ghost_delay, DEFAULT_GHOST_DELAY);
1161 out:
1162         if (print_off_int_undef(buff, 12, mp->ghost_delay) != 0)
1163                 condlog(3, "%s: ghost_delay = %s %s", mp->alias, buff, origin);
1164         return 0;
1165 }
1166
1167 int select_find_multipaths_timeout(struct config *conf, struct path *pp)
1168 {
1169         const char *origin;
1170
1171         pp_set_conf(find_multipaths_timeout);
1172         pp_set_default(find_multipaths_timeout,
1173                        DEFAULT_FIND_MULTIPATHS_TIMEOUT);
1174 out:
1175         /*
1176          * If configured value is negative, and this "unknown" hardware
1177          * (no hwentry), use very small timeout to avoid delays.
1178          */
1179         if (pp->find_multipaths_timeout < 0) {
1180                 pp->find_multipaths_timeout = -pp->find_multipaths_timeout;
1181                 if (!pp->hwe) {
1182                         pp->find_multipaths_timeout =
1183                                 DEFAULT_UNKNOWN_FIND_MULTIPATHS_TIMEOUT;
1184                         origin = "(default for unknown hardware)";
1185                 }
1186         }
1187         condlog(3, "%s: timeout for find_multipaths \"smart\" = %ds %s",
1188                 pp->dev, pp->find_multipaths_timeout, origin);
1189         return 0;
1190 }
1191
1192 int select_all_tg_pt (struct config *conf, struct multipath * mp)
1193 {
1194         const char *origin;
1195
1196         mp_set_ovr(all_tg_pt);
1197         mp_set_hwe(all_tg_pt);
1198         mp_set_conf(all_tg_pt);
1199         mp_set_default(all_tg_pt, DEFAULT_ALL_TG_PT);
1200 out:
1201         condlog(3, "%s: all_tg_pt = %s %s", mp->alias,
1202                 (mp->all_tg_pt == ALL_TG_PT_ON)? "yes" : "no",
1203                 origin);
1204         return 0;
1205 }