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