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