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