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