fdb5953a4581c137ab03a5f55e377942f16bb247
[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
424         vector_foreach_slot(mp->paths, pp, i)
425                 all_tpgs = all_tpgs && (pp->tpgs > 0);
426         if (mp->retain_hwhandler != RETAIN_HWHANDLER_OFF) {
427                 vector_foreach_slot(mp->paths, pp, i) {
428                         if (get_dh_state(pp, dh_state, sizeof(handler) - 2) > 0
429                             && strcmp(dh_state, "detached")) {
430                                 memcpy(handler, "1 ", 2);
431                                 mp->hwhandler = handler;
432                                 origin = "(setting: retained by kernel driver)";
433                                 goto out;
434                         }
435                 }
436         }
437
438         mp_set_hwe(hwhandler);
439         mp_set_conf(hwhandler);
440         mp_set_default(hwhandler, DEFAULT_HWHANDLER);
441 out:
442         if (all_tpgs && !strcmp(mp->hwhandler, DEFAULT_HWHANDLER) &&
443                 origin == default_origin) {
444                 mp->hwhandler = alua_name;
445                 origin = tpgs_origin;
446         } else if (!all_tpgs && !strcmp(mp->hwhandler, alua_name)) {
447                 mp->hwhandler = DEFAULT_HWHANDLER;
448                 origin = tpgs_origin;
449         }
450         mp->hwhandler = STRDUP(mp->hwhandler);
451         condlog(3, "%s: hardware_handler = \"%s\" %s", mp->alias, mp->hwhandler,
452                 origin);
453         return 0;
454 }
455
456 /*
457  * Current RDAC (NetApp E-Series) firmware relies
458  * on periodic REPORT TARGET PORT GROUPS for
459  * internal load balancing.
460  * Using the sysfs priority checker defeats this purpose.
461  *
462  * Moreover, NetApp would also prefer the RDAC checker over ALUA.
463  * (https://www.redhat.com/archives/dm-devel/2017-September/msg00326.html)
464  */
465 static int
466 check_rdac(struct path * pp)
467 {
468         int len;
469         char buff[44];
470
471         if (pp->bus != SYSFS_BUS_SCSI)
472                 return 0;
473         len = get_vpd_sgio(pp->fd, 0xC9, buff, 44);
474         if (len <= 0)
475                 return 0;
476         return !(memcmp(buff + 4, "vac1", 4));
477 }
478
479 int select_checker(struct config *conf, struct path *pp)
480 {
481         const char *origin;
482         char *checker_name;
483         struct checker * c = &pp->checker;
484
485         if (pp->detect_checker == DETECT_CHECKER_ON) {
486                 origin = autodetect_origin;
487                 if (check_rdac(pp)) {
488                         checker_name = RDAC;
489                         goto out;
490                 } else if (pp->tpgs > 0) {
491                         checker_name = TUR;
492                         goto out;
493                 }
494         }
495         do_set(checker_name, conf->overrides, checker_name, overrides_origin);
496         do_set_from_hwe(checker_name, pp, checker_name, hwe_origin);
497         do_set(checker_name, conf, checker_name, conf_origin);
498         do_default(checker_name, DEFAULT_CHECKER);
499 out:
500         checker_get(conf->multipath_dir, c, checker_name);
501         condlog(3, "%s: path_checker = %s %s", pp->dev, c->name, origin);
502         if (conf->checker_timeout) {
503                 c->timeout = conf->checker_timeout;
504                 condlog(3, "%s: checker timeout = %u s %s",
505                         pp->dev, c->timeout, conf_origin);
506         }
507         else if (sysfs_get_timeout(pp, &c->timeout) > 0)
508                 condlog(3, "%s: checker timeout = %u s (setting: kernel sysfs)",
509                         pp->dev, c->timeout);
510         else {
511                 c->timeout = DEF_TIMEOUT;
512                 condlog(3, "%s: checker timeout = %u s %s",
513                         pp->dev, c->timeout, default_origin);
514         }
515         return 0;
516 }
517
518 int select_getuid(struct config *conf, struct path *pp)
519 {
520         const char *origin;
521
522         pp->uid_attribute = parse_uid_attribute_by_attrs(conf->uid_attrs, pp->dev);
523         if (pp->uid_attribute) {
524                 origin = "(setting: multipath.conf defaults section)";
525                 goto out;
526         }
527
528         pp_set_ovr(getuid);
529         pp_set_ovr(uid_attribute);
530         pp_set_hwe(getuid);
531         pp_set_hwe(uid_attribute);
532         pp_set_conf(getuid);
533         pp_set_conf(uid_attribute);
534         pp_set_default(uid_attribute, DEFAULT_UID_ATTRIBUTE);
535 out:
536         if (pp->uid_attribute)
537                 condlog(3, "%s: uid_attribute = %s %s", pp->dev,
538                         pp->uid_attribute, origin);
539         else if (pp->getuid)
540                 condlog(3, "%s: getuid = \"%s\" %s", pp->dev, pp->getuid,
541                         origin);
542         return 0;
543 }
544
545 void
546 detect_prio(struct config *conf, struct path * pp)
547 {
548         struct prio *p = &pp->prio;
549         char buff[512];
550         char *default_prio = PRIO_ALUA;
551
552         if (pp->tpgs <= 0)
553                 return;
554         if (pp->tpgs == 2 || !check_rdac(pp)) {
555                 if (sysfs_get_asymmetric_access_state(pp, buff, 512) >= 0)
556                         default_prio = PRIO_SYSFS;
557         }
558         prio_get(conf->multipath_dir, p, default_prio, DEFAULT_PRIO_ARGS);
559 }
560
561 #define set_prio(dir, src, msg)                                 \
562 do {                                                                    \
563         if (src && src->prio_name) {                                    \
564                 prio_get(dir, p, src->prio_name, src->prio_args);       \
565                 origin = msg;                                           \
566                 goto out;                                               \
567         }                                                               \
568 } while(0)
569
570 #define set_prio_from_vec(type, dir, src, msg, p)                       \
571 do {                                                                    \
572         type *_p;                                                       \
573         int i;                                                          \
574         char *prio_name = NULL, *prio_args = NULL;                      \
575                                                                         \
576         vector_foreach_slot(src, _p, i) {                               \
577                 if (prio_name == NULL && _p->prio_name)         \
578                         prio_name = _p->prio_name;                      \
579                 if (prio_args == NULL && _p->prio_args)         \
580                         prio_args = _p->prio_args;                      \
581         }                                                               \
582         if (prio_name != NULL) {                                        \
583                 prio_get(dir, p, prio_name, prio_args);                 \
584                 origin = msg;                                           \
585                 goto out;                                               \
586         }                                                               \
587 } while (0)
588
589 int select_prio(struct config *conf, struct path *pp)
590 {
591         const char *origin;
592         struct mpentry * mpe;
593         struct prio * p = &pp->prio;
594
595         if (pp->detect_prio == DETECT_PRIO_ON) {
596                 detect_prio(conf, pp);
597                 if (prio_selected(p)) {
598                         origin = autodetect_origin;
599                         goto out;
600                 }
601         }
602         mpe = find_mpe(conf->mptable, pp->wwid);
603         set_prio(conf->multipath_dir, mpe, multipaths_origin);
604         set_prio(conf->multipath_dir, conf->overrides, overrides_origin);
605         set_prio_from_vec(struct hwentry, conf->multipath_dir,
606                           pp->hwe, hwe_origin, p);
607         set_prio(conf->multipath_dir, conf, conf_origin);
608         prio_get(conf->multipath_dir, p, DEFAULT_PRIO, DEFAULT_PRIO_ARGS);
609         origin = default_origin;
610 out:
611         /*
612          * fetch tpgs mode for alua, if its not already obtained
613          */
614         if (!strncmp(prio_name(p), PRIO_ALUA, PRIO_NAME_LEN)) {
615                 int tpgs = 0;
616                 unsigned int timeout = conf->checker_timeout;
617
618                 if(!pp->tpgs &&
619                    (tpgs = get_target_port_group_support(pp->fd, timeout)) >= 0)
620                         pp->tpgs = tpgs;
621         }
622         condlog(3, "%s: prio = %s %s", pp->dev, prio_name(p), origin);
623         condlog(3, "%s: prio args = \"%s\" %s", pp->dev, prio_args(p), origin);
624         return 0;
625 }
626
627 int select_no_path_retry(struct config *conf, struct multipath *mp)
628 {
629         const char *origin = NULL;
630         char buff[12];
631
632         if (mp->disable_queueing) {
633                 condlog(0, "%s: queueing disabled", mp->alias);
634                 mp->no_path_retry = NO_PATH_RETRY_FAIL;
635                 return 0;
636         }
637         mp_set_mpe(no_path_retry);
638         mp_set_ovr(no_path_retry);
639         mp_set_hwe(no_path_retry);
640         mp_set_conf(no_path_retry);
641 out:
642         print_no_path_retry(buff, 12, mp->no_path_retry);
643         if (origin)
644                 condlog(3, "%s: no_path_retry = %s %s", mp->alias, buff,
645                         origin);
646         else
647                 condlog(3, "%s: no_path_retry = undef %s",
648                         mp->alias, default_origin);
649         return 0;
650 }
651
652 int
653 select_minio_rq (struct config *conf, struct multipath * mp)
654 {
655         const char *origin;
656
657         do_set(minio_rq, mp->mpe, mp->minio, multipaths_origin);
658         do_set(minio_rq, conf->overrides, mp->minio, overrides_origin);
659         do_set_from_hwe(minio_rq, mp, mp->minio, hwe_origin);
660         do_set(minio_rq, conf, mp->minio, conf_origin);
661         do_default(mp->minio, DEFAULT_MINIO_RQ);
662 out:
663         condlog(3, "%s: minio = %i %s", mp->alias, mp->minio, origin);
664         return 0;
665 }
666
667 int
668 select_minio_bio (struct config *conf, struct multipath * mp)
669 {
670         const char *origin;
671
672         mp_set_mpe(minio);
673         mp_set_ovr(minio);
674         mp_set_hwe(minio);
675         mp_set_conf(minio);
676         mp_set_default(minio, DEFAULT_MINIO);
677 out:
678         condlog(3, "%s: minio = %i %s", mp->alias, mp->minio, origin);
679         return 0;
680 }
681
682 int select_minio(struct config *conf, struct multipath *mp)
683 {
684         unsigned int minv_dmrq[3] = {1, 1, 0};
685
686         if (VERSION_GE(conf->version, minv_dmrq))
687                 return select_minio_rq(conf, mp);
688         else
689                 return select_minio_bio(conf, mp);
690 }
691
692 int select_fast_io_fail(struct config *conf, struct multipath *mp)
693 {
694         const char *origin;
695         char buff[12];
696
697         mp_set_ovr(fast_io_fail);
698         mp_set_hwe(fast_io_fail);
699         mp_set_conf(fast_io_fail);
700         mp_set_default(fast_io_fail, DEFAULT_FAST_IO_FAIL);
701 out:
702         print_fast_io_fail(buff, 12, mp->fast_io_fail);
703         condlog(3, "%s: fast_io_fail_tmo = %s %s", mp->alias, buff, origin);
704         return 0;
705 }
706
707 int select_dev_loss(struct config *conf, struct multipath *mp)
708 {
709         const char *origin;
710         char buff[12];
711
712         mp_set_ovr(dev_loss);
713         mp_set_hwe(dev_loss);
714         mp_set_conf(dev_loss);
715         mp->dev_loss = 0;
716         return 0;
717 out:
718         print_dev_loss(buff, 12, mp->dev_loss);
719         condlog(3, "%s: dev_loss_tmo = %s %s", mp->alias, buff, origin);
720         return 0;
721 }
722
723 int select_flush_on_last_del(struct config *conf, struct multipath *mp)
724 {
725         const char *origin;
726
727         mp_set_mpe(flush_on_last_del);
728         mp_set_ovr(flush_on_last_del);
729         mp_set_hwe(flush_on_last_del);
730         mp_set_conf(flush_on_last_del);
731         mp_set_default(flush_on_last_del, DEFAULT_FLUSH);
732 out:
733         condlog(3, "%s: flush_on_last_del = %s %s", mp->alias,
734                 (mp->flush_on_last_del == FLUSH_ENABLED)? "yes" : "no", origin);
735         return 0;
736 }
737
738 int select_reservation_key(struct config *conf, struct multipath *mp)
739 {
740         const char *origin;
741         char buff[PRKEY_SIZE];
742         char *from_file = "";
743         uint64_t prkey = 0;
744
745         do_prkey_set(mp->mpe, multipaths_origin);
746         do_prkey_set(conf, conf_origin);
747         put_be64(mp->reservation_key, 0);
748         mp->sa_flags = 0;
749         mp->prkey_source = PRKEY_SOURCE_NONE;
750         return 0;
751 out:
752         if (mp->prkey_source == PRKEY_SOURCE_FILE) {
753                 from_file = " (from prkeys file)";
754                 if (get_prkey(conf, mp, &prkey, &mp->sa_flags) != 0)
755                         put_be64(mp->reservation_key, 0);
756                 else
757                         put_be64(mp->reservation_key, prkey);
758         }
759         print_reservation_key(buff, PRKEY_SIZE, mp->reservation_key,
760                               mp->sa_flags, mp->prkey_source);
761         condlog(3, "%s: reservation_key = %s %s%s", mp->alias, buff, origin,
762                 from_file);
763         return 0;
764 }
765
766 int select_retain_hwhandler(struct config *conf, struct multipath *mp)
767 {
768         const char *origin;
769         unsigned int minv_dm_retain[3] = {1, 5, 0};
770
771         if (!VERSION_GE(conf->version, minv_dm_retain)) {
772                 mp->retain_hwhandler = RETAIN_HWHANDLER_OFF;
773                 origin = "(setting: WARNING, requires kernel dm-mpath version >= 1.5.0)";
774                 goto out;
775         }
776         if (get_linux_version_code() >= KERNEL_VERSION(4, 3, 0)) {
777                 mp->retain_hwhandler = RETAIN_HWHANDLER_ON;
778                 origin = "(setting: implied in kernel >= 4.3.0)";
779                 goto out;
780         }
781         mp_set_ovr(retain_hwhandler);
782         mp_set_hwe(retain_hwhandler);
783         mp_set_conf(retain_hwhandler);
784         mp_set_default(retain_hwhandler, DEFAULT_RETAIN_HWHANDLER);
785 out:
786         condlog(3, "%s: retain_attached_hw_handler = %s %s", mp->alias,
787                 (mp->retain_hwhandler == RETAIN_HWHANDLER_ON)? "yes" : "no",
788                 origin);
789         return 0;
790 }
791
792 int select_detect_prio(struct config *conf, struct path *pp)
793 {
794         const char *origin;
795
796         pp_set_ovr(detect_prio);
797         pp_set_hwe(detect_prio);
798         pp_set_conf(detect_prio);
799         pp_set_default(detect_prio, DEFAULT_DETECT_PRIO);
800 out:
801         condlog(3, "%s: detect_prio = %s %s", pp->dev,
802                 (pp->detect_prio == DETECT_PRIO_ON)? "yes" : "no", origin);
803         return 0;
804 }
805
806 int select_detect_checker(struct config *conf, struct path *pp)
807 {
808         const char *origin;
809
810         pp_set_ovr(detect_checker);
811         pp_set_hwe(detect_checker);
812         pp_set_conf(detect_checker);
813         pp_set_default(detect_checker, DEFAULT_DETECT_CHECKER);
814 out:
815         condlog(3, "%s: detect_checker = %s %s", pp->dev,
816                 (pp->detect_checker == DETECT_CHECKER_ON)? "yes" : "no",
817                 origin);
818         return 0;
819 }
820
821 int select_deferred_remove(struct config *conf, struct multipath *mp)
822 {
823         const char *origin;
824
825 #ifndef LIBDM_API_DEFERRED
826         mp->deferred_remove = DEFERRED_REMOVE_OFF;
827         origin = "(setting: WARNING, not compiled with support)";
828         goto out;
829 #endif
830         if (mp->deferred_remove == DEFERRED_REMOVE_IN_PROGRESS) {
831                 condlog(3, "%s: deferred remove in progress", mp->alias);
832                 return 0;
833         }
834         mp_set_mpe(deferred_remove);
835         mp_set_ovr(deferred_remove);
836         mp_set_hwe(deferred_remove);
837         mp_set_conf(deferred_remove);
838         mp_set_default(deferred_remove, DEFAULT_DEFERRED_REMOVE);
839 out:
840         condlog(3, "%s: deferred_remove = %s %s", mp->alias,
841                 (mp->deferred_remove == DEFERRED_REMOVE_ON)? "yes" : "no",
842                 origin);
843         return 0;
844 }
845
846 int select_delay_watch_checks(struct config *conf, struct multipath *mp)
847 {
848         const char *origin;
849         char buff[12];
850
851         mp_set_mpe(delay_watch_checks);
852         mp_set_ovr(delay_watch_checks);
853         mp_set_hwe(delay_watch_checks);
854         mp_set_conf(delay_watch_checks);
855         mp_set_default(delay_watch_checks, DEFAULT_DELAY_CHECKS);
856 out:
857         print_off_int_undef(buff, 12, mp->delay_watch_checks);
858         condlog(3, "%s: delay_watch_checks = %s %s", mp->alias, buff, origin);
859         return 0;
860 }
861
862 int select_delay_wait_checks(struct config *conf, struct multipath *mp)
863 {
864         const char *origin;
865         char buff[12];
866
867         mp_set_mpe(delay_wait_checks);
868         mp_set_ovr(delay_wait_checks);
869         mp_set_hwe(delay_wait_checks);
870         mp_set_conf(delay_wait_checks);
871         mp_set_default(delay_wait_checks, DEFAULT_DELAY_CHECKS);
872 out:
873         print_off_int_undef(buff, 12, mp->delay_wait_checks);
874         condlog(3, "%s: delay_wait_checks = %s %s", mp->alias, buff, origin);
875         return 0;
876
877 }
878
879 int select_marginal_path_err_sample_time(struct config *conf, struct multipath *mp)
880 {
881         const char *origin;
882         char buff[12];
883
884         mp_set_mpe(marginal_path_err_sample_time);
885         mp_set_ovr(marginal_path_err_sample_time);
886         mp_set_hwe(marginal_path_err_sample_time);
887         mp_set_conf(marginal_path_err_sample_time);
888         mp_set_default(marginal_path_err_sample_time, DEFAULT_ERR_CHECKS);
889 out:
890         print_off_int_undef(buff, 12, mp->marginal_path_err_sample_time);
891         condlog(3, "%s: marginal_path_err_sample_time = %s %s", mp->alias, buff,
892                         origin);
893         return 0;
894 }
895
896 int select_marginal_path_err_rate_threshold(struct config *conf, struct multipath *mp)
897 {
898         const char *origin;
899         char buff[12];
900
901         mp_set_mpe(marginal_path_err_rate_threshold);
902         mp_set_ovr(marginal_path_err_rate_threshold);
903         mp_set_hwe(marginal_path_err_rate_threshold);
904         mp_set_conf(marginal_path_err_rate_threshold);
905         mp_set_default(marginal_path_err_rate_threshold, DEFAULT_ERR_CHECKS);
906 out:
907         print_off_int_undef(buff, 12, mp->marginal_path_err_rate_threshold);
908         condlog(3, "%s: marginal_path_err_rate_threshold = %s %s", mp->alias, buff,
909                         origin);
910         return 0;
911 }
912
913 int select_marginal_path_err_recheck_gap_time(struct config *conf, struct multipath *mp)
914 {
915         const char *origin;
916         char buff[12];
917
918         mp_set_mpe(marginal_path_err_recheck_gap_time);
919         mp_set_ovr(marginal_path_err_recheck_gap_time);
920         mp_set_hwe(marginal_path_err_recheck_gap_time);
921         mp_set_conf(marginal_path_err_recheck_gap_time);
922         mp_set_default(marginal_path_err_recheck_gap_time, DEFAULT_ERR_CHECKS);
923 out:
924         print_off_int_undef(buff, 12, mp->marginal_path_err_recheck_gap_time);
925         condlog(3, "%s: marginal_path_err_recheck_gap_time = %s %s", mp->alias, buff,
926                         origin);
927         return 0;
928 }
929
930 int select_marginal_path_double_failed_time(struct config *conf, struct multipath *mp)
931 {
932         const char *origin;
933         char buff[12];
934
935         mp_set_mpe(marginal_path_double_failed_time);
936         mp_set_ovr(marginal_path_double_failed_time);
937         mp_set_hwe(marginal_path_double_failed_time);
938         mp_set_conf(marginal_path_double_failed_time);
939         mp_set_default(marginal_path_double_failed_time, DEFAULT_ERR_CHECKS);
940 out:
941         print_off_int_undef(buff, 12, mp->marginal_path_double_failed_time);
942         condlog(3, "%s: marginal_path_double_failed_time = %s %s", mp->alias, buff,
943                         origin);
944         return 0;
945 }
946
947 int select_skip_kpartx (struct config *conf, struct multipath * mp)
948 {
949         const char *origin;
950
951         mp_set_mpe(skip_kpartx);
952         mp_set_ovr(skip_kpartx);
953         mp_set_hwe(skip_kpartx);
954         mp_set_conf(skip_kpartx);
955         mp_set_default(skip_kpartx, DEFAULT_SKIP_KPARTX);
956 out:
957         condlog(3, "%s: skip_kpartx = %s %s", mp->alias,
958                 (mp->skip_kpartx == SKIP_KPARTX_ON)? "yes" : "no",
959                 origin);
960         return 0;
961 }
962
963 int select_max_sectors_kb(struct config *conf, struct multipath * mp)
964 {
965         const char *origin;
966
967         mp_set_mpe(max_sectors_kb);
968         mp_set_ovr(max_sectors_kb);
969         mp_set_hwe(max_sectors_kb);
970         mp_set_conf(max_sectors_kb);
971         mp_set_default(max_sectors_kb, DEFAULT_MAX_SECTORS_KB);
972         /*
973          * In the default case, we will not modify max_sectors_kb in sysfs
974          * (see sysfs_set_max_sectors_kb()).
975          * Don't print a log message here to avoid user confusion.
976          */
977         return 0;
978 out:
979         condlog(3, "%s: max_sectors_kb = %i %s", mp->alias, mp->max_sectors_kb,
980                 origin);
981         return 0;
982 }
983
984 int select_ghost_delay (struct config *conf, struct multipath * mp)
985 {
986         const char *origin;
987         char buff[12];
988
989         mp_set_mpe(ghost_delay);
990         mp_set_ovr(ghost_delay);
991         mp_set_hwe(ghost_delay);
992         mp_set_conf(ghost_delay);
993         mp_set_default(ghost_delay, DEFAULT_GHOST_DELAY);
994 out:
995         print_off_int_undef(buff, 12, mp->ghost_delay);
996         condlog(3, "%s: ghost_delay = %s %s", mp->alias, buff, origin);
997         return 0;
998 }
999
1000 int select_find_multipaths_timeout(struct config *conf, struct path *pp)
1001 {
1002         const char *origin;
1003
1004         pp_set_conf(find_multipaths_timeout);
1005         pp_set_default(find_multipaths_timeout,
1006                        DEFAULT_FIND_MULTIPATHS_TIMEOUT);
1007 out:
1008         /*
1009          * If configured value is negative, and this "unknown" hardware
1010          * (no hwentry), use very small timeout to avoid delays.
1011          */
1012         if (pp->find_multipaths_timeout < 0) {
1013                 pp->find_multipaths_timeout = -pp->find_multipaths_timeout;
1014                 if (!pp->hwe) {
1015                         pp->find_multipaths_timeout =
1016                                 DEFAULT_UNKNOWN_FIND_MULTIPATHS_TIMEOUT;
1017                         origin = "(default for unknown hardware)";
1018                 }
1019         }
1020         condlog(3, "%s: timeout for find_multipaths \"smart\" = %ds %s",
1021                 pp->dev, pp->find_multipaths_timeout, origin);
1022         return 0;
1023 }
1024
1025 int select_all_tg_pt (struct config *conf, struct multipath * mp)
1026 {
1027         const char *origin;
1028
1029         mp_set_ovr(all_tg_pt);
1030         mp_set_hwe(all_tg_pt);
1031         mp_set_conf(all_tg_pt);
1032         mp_set_default(all_tg_pt, DEFAULT_ALL_TG_PT);
1033 out:
1034         condlog(3, "%s: all_tg_pt = %s %s", mp->alias,
1035                 (mp->all_tg_pt == ALL_TG_PT_ON)? "yes" : "no",
1036                 origin);
1037         return 0;
1038 }