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