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