1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
|
/*
* Copyright (c) 2004, 2005 Christophe Varoqui
* Copyright (c) 2005 Benjamin Marzinski, Redhat
* Copyright (c) 2005 Edward Goggin, EMC
*/
#include <stdio.h>
#include <string.h>
#include <libudev.h>
#include <dirent.h>
#include <limits.h>
#include <errno.h>
#include "checkers.h"
#include "util.h"
#include "debug.h"
#include "parser.h"
#include "dict.h"
#include "hwtable.h"
#include "vector.h"
#include "structs.h"
#include "config.h"
#include "blacklist.h"
#include "defaults.h"
#include "prio.h"
#include "devmapper.h"
#include "mpath_cmd.h"
#include "propsel.h"
#include "foreign.h"
/*
* We don't support re-initialization after
* libmultipath_exit().
*/
static bool libmultipath_exit_called;
static pthread_once_t _init_once = PTHREAD_ONCE_INIT;
static pthread_once_t _exit_once = PTHREAD_ONCE_INIT;
struct udev *udev;
static void _udev_init(void)
{
if (udev)
udev_ref(udev);
else
udev = udev_new();
if (!udev)
condlog(0, "%s: failed to initialize udev", __func__);
}
static bool _is_libmultipath_initialized(void)
{
return !libmultipath_exit_called && !!udev;
}
int libmultipath_init(void)
{
pthread_once(&_init_once, _udev_init);
return !_is_libmultipath_initialized();
}
static void _libmultipath_exit(void)
{
libmultipath_exit_called = true;
cleanup_foreign();
cleanup_checkers();
cleanup_prio();
libmp_dm_exit();
udev_unref(udev);
}
void libmultipath_exit(void)
{
pthread_once(&_exit_once, _libmultipath_exit);
}
static struct config internal_config;
struct config *libmp_get_multipath_config(void)
{
if (!internal_config.hwtable)
/* not initialized */
return NULL;
return &internal_config;
}
struct config *get_multipath_config(void)
__attribute__((weak, alias("libmp_get_multipath_config")));
void libmp_put_multipath_config(void *conf __attribute__((unused)))
{
/* empty */
}
void put_multipath_config(void *conf)
__attribute__((weak, alias("libmp_put_multipath_config")));
static int
hwe_strmatch (const struct hwentry *hwe1, const struct hwentry *hwe2)
{
if ((hwe2->vendor && !hwe1->vendor) ||
(hwe1->vendor && (!hwe2->vendor ||
strcmp(hwe1->vendor, hwe2->vendor))))
return 1;
if ((hwe2->product && !hwe1->product) ||
(hwe1->product && (!hwe2->product ||
strcmp(hwe1->product, hwe2->product))))
return 1;
if ((hwe2->revision && !hwe1->revision) ||
(hwe1->revision && (!hwe2->revision ||
strcmp(hwe1->revision, hwe2->revision))))
return 1;
return 0;
}
static struct hwentry *
find_hwe_strmatch (const struct vector_s *hwtable, const struct hwentry *hwe)
{
int i;
struct hwentry *tmp, *ret = NULL;
vector_foreach_slot (hwtable, tmp, i) {
if (hwe_strmatch(tmp, hwe))
continue;
ret = tmp;
break;
}
return ret;
}
static int
hwe_regmatch (const struct hwentry *hwe1, const char *vendor,
const char *product, const char *revision)
{
regex_t vre, pre, rre;
int retval = 1;
if (hwe1->vendor &&
regcomp(&vre, hwe1->vendor, REG_EXTENDED|REG_NOSUB))
goto out;
if (hwe1->product &&
regcomp(&pre, hwe1->product, REG_EXTENDED|REG_NOSUB))
goto out_vre;
if (hwe1->revision &&
regcomp(&rre, hwe1->revision, REG_EXTENDED|REG_NOSUB))
goto out_pre;
if ((vendor || product || revision) &&
(!hwe1->vendor || !vendor ||
!regexec(&vre, vendor, 0, NULL, 0)) &&
(!hwe1->product || !product ||
!regexec(&pre, product, 0, NULL, 0)) &&
(!hwe1->revision || !revision ||
!regexec(&rre, revision, 0, NULL, 0)))
retval = 0;
if (hwe1->revision)
regfree(&rre);
out_pre:
if (hwe1->product)
regfree(&pre);
out_vre:
if (hwe1->vendor)
regfree(&vre);
out:
return retval;
}
static void _log_match(const char *fn, const struct hwentry *h,
const char *vendor, const char *product,
const char *revision)
{
condlog(4, "%s: found match /%s:%s:%s/ for '%s:%s:%s'", fn,
h->vendor, h->product, h->revision,
vendor, product, revision);
}
#define log_match(h, v, p, r) _log_match(__func__, (h), (v), (p), (r))
int
find_hwe (const struct vector_s *hwtable,
const char * vendor, const char * product, const char * revision,
vector result)
{
int i, n = 0;
struct hwentry *tmp;
/*
* Search backwards here, and add forward.
* User modified entries are attached at the end of
* the list, so we have to check them first before
* continuing to the generic entries
*/
vector_reset(result);
vector_foreach_slot_backwards (hwtable, tmp, i) {
if (hwe_regmatch(tmp, vendor, product, revision))
continue;
if (vector_alloc_slot(result)) {
vector_set_slot(result, tmp);
n++;
}
log_match(tmp, vendor, product, revision);
}
condlog(n > 1 ? 3 : 4, "%s: found %d hwtable matches for %s:%s:%s",
__func__, n, vendor, product, revision);
return n;
}
struct mpentry *find_mpe(vector mptable, char *wwid)
{
int i;
struct mpentry * mpe;
if (!wwid || !*wwid)
return NULL;
vector_foreach_slot (mptable, mpe, i)
if (mpe->wwid && !strcmp(mpe->wwid, wwid))
return mpe;
return NULL;
}
const char *get_mpe_wwid(const struct vector_s *mptable, const char *alias)
{
int i;
struct mpentry * mpe;
if (!alias)
return NULL;
vector_foreach_slot (mptable, mpe, i)
if (mpe->alias && strcmp(mpe->alias, alias) == 0)
return mpe->wwid;
return NULL;
}
static void
free_pctable (vector pctable)
{
int i;
struct pcentry *pce;
vector_foreach_slot(pctable, pce, i)
free(pce);
vector_free(pctable);
}
void
free_hwe (struct hwentry * hwe)
{
if (!hwe)
return;
if (hwe->vendor)
free(hwe->vendor);
if (hwe->product)
free(hwe->product);
if (hwe->revision)
free(hwe->revision);
if (hwe->uid_attribute)
free(hwe->uid_attribute);
if (hwe->features)
free(hwe->features);
if (hwe->hwhandler)
free(hwe->hwhandler);
if (hwe->selector)
free(hwe->selector);
if (hwe->checker_name)
free(hwe->checker_name);
if (hwe->prio_name)
free(hwe->prio_name);
if (hwe->prio_args)
free(hwe->prio_args);
if (hwe->alias_prefix)
free(hwe->alias_prefix);
if (hwe->bl_product)
free(hwe->bl_product);
if (hwe->pctable)
free_pctable(hwe->pctable);
free(hwe);
}
void
free_hwtable (vector hwtable)
{
int i;
struct hwentry * hwe;
if (!hwtable)
return;
vector_foreach_slot (hwtable, hwe, i)
free_hwe(hwe);
vector_free(hwtable);
}
void
free_mpe (struct mpentry * mpe)
{
if (!mpe)
return;
if (mpe->wwid)
free(mpe->wwid);
if (mpe->selector)
free(mpe->selector);
if (mpe->uid_attribute)
free(mpe->uid_attribute);
if (mpe->alias)
free(mpe->alias);
if (mpe->prio_name)
free(mpe->prio_name);
if (mpe->prio_args)
free(mpe->prio_args);
free(mpe);
}
void
free_mptable (vector mptable)
{
int i;
struct mpentry * mpe;
if (!mptable)
return;
vector_foreach_slot (mptable, mpe, i)
free_mpe(mpe);
vector_free(mptable);
}
struct mpentry *
alloc_mpe (void)
{
struct mpentry * mpe = (struct mpentry *)
calloc(1, sizeof(struct mpentry));
return mpe;
}
struct hwentry *
alloc_hwe (void)
{
struct hwentry * hwe = (struct hwentry *)
calloc(1, sizeof(struct hwentry));
return hwe;
}
struct pcentry *
alloc_pce (void)
{
struct pcentry *pce = (struct pcentry *)
calloc(1, sizeof(struct pcentry));
pce->type = PCE_INVALID;
return pce;
}
static char *
set_param_str(const char * str)
{
char * dst;
int len;
if (!str)
return NULL;
len = strlen(str);
if (!len)
return NULL;
dst = (char *)calloc(1, len + 1);
if (!dst)
return NULL;
strcpy(dst, str);
return dst;
}
#define merge_str(s) \
if (!dst->s && src->s && strlen(src->s)) { \
dst->s = src->s; \
src->s = NULL; \
}
#define merge_num(s) \
if (!dst->s && src->s) \
dst->s = src->s
static void
merge_pce(struct pcentry *dst, struct pcentry *src)
{
merge_num(fast_io_fail);
merge_num(dev_loss);
merge_num(eh_deadline);
}
static void
merge_hwe (struct hwentry * dst, struct hwentry * src)
{
char id[SCSI_VENDOR_SIZE+PATH_PRODUCT_SIZE];
merge_str(vendor);
merge_str(product);
merge_str(revision);
merge_str(uid_attribute);
merge_str(features);
merge_str(hwhandler);
merge_str(selector);
merge_str(checker_name);
merge_str(prio_name);
merge_str(prio_args);
merge_str(alias_prefix);
merge_str(bl_product);
merge_num(pgpolicy);
merge_num(pgfailback);
merge_num(rr_weight);
merge_num(no_path_retry);
merge_num(minio);
merge_num(minio_rq);
merge_num(flush_on_last_del);
merge_num(fast_io_fail);
merge_num(dev_loss);
merge_num(eh_deadline);
merge_num(user_friendly_names);
merge_num(retain_hwhandler);
merge_num(detect_prio);
merge_num(detect_checker);
merge_num(detect_pgpolicy);
merge_num(detect_pgpolicy_use_tpg);
merge_num(deferred_remove);
merge_num(delay_watch_checks);
merge_num(delay_wait_checks);
merge_num(skip_kpartx);
merge_num(max_sectors_kb);
merge_num(ghost_delay);
merge_num(all_tg_pt);
merge_num(recheck_wwid);
merge_num(vpd_vendor_id);
merge_num(san_path_err_threshold);
merge_num(san_path_err_forget_rate);
merge_num(san_path_err_recovery_time);
merge_num(marginal_path_err_sample_time);
merge_num(marginal_path_err_rate_threshold);
merge_num(marginal_path_err_recheck_gap_time);
merge_num(marginal_path_double_failed_time);
snprintf(id, sizeof(id), "%s/%s", dst->vendor, dst->product);
reconcile_features_with_options(id, &dst->features,
&dst->no_path_retry,
&dst->retain_hwhandler);
}
static void
merge_mpe(struct mpentry *dst, struct mpentry *src)
{
merge_str(alias);
merge_str(uid_attribute);
merge_str(selector);
merge_str(features);
merge_str(prio_name);
merge_str(prio_args);
if (dst->prkey_source == PRKEY_SOURCE_NONE &&
src->prkey_source != PRKEY_SOURCE_NONE) {
dst->prkey_source = src->prkey_source;
dst->sa_flags = src->sa_flags;
memcpy(&dst->reservation_key, &src->reservation_key,
sizeof(dst->reservation_key));
}
merge_num(pgpolicy);
merge_num(pgfailback);
merge_num(rr_weight);
merge_num(no_path_retry);
merge_num(minio);
merge_num(minio_rq);
merge_num(flush_on_last_del);
merge_num(attribute_flags);
merge_num(user_friendly_names);
merge_num(deferred_remove);
merge_num(delay_watch_checks);
merge_num(delay_wait_checks);
merge_num(san_path_err_threshold);
merge_num(san_path_err_forget_rate);
merge_num(san_path_err_recovery_time);
merge_num(marginal_path_err_sample_time);
merge_num(marginal_path_err_rate_threshold);
merge_num(marginal_path_err_recheck_gap_time);
merge_num(marginal_path_double_failed_time);
merge_num(skip_kpartx);
merge_num(max_sectors_kb);
merge_num(ghost_delay);
merge_num(uid);
merge_num(gid);
merge_num(mode);
}
static int wwid_compar(const void *p1, const void *p2)
{
const char *wwid1 = (*(struct mpentry * const *)p1)->wwid;
const char *wwid2 = (*(struct mpentry * const *)p2)->wwid;
return strcmp(wwid1, wwid2);
}
void merge_mptable(vector mptable)
{
struct mpentry *mp1, *mp2;
int i, j;
vector_foreach_slot(mptable, mp1, i) {
/* drop invalid multipath configs */
if (!mp1->wwid) {
condlog(0, "multipaths config section missing wwid");
vector_del_slot(mptable, i--);
free_mpe(mp1);
continue;
}
}
vector_sort(mptable, wwid_compar);
vector_foreach_slot(mptable, mp1, i) {
j = i + 1;
vector_foreach_slot_after(mptable, mp2, j) {
if (strcmp(mp1->wwid, mp2->wwid))
break;
condlog(1, "%s: duplicate multipath config section for %s",
__func__, mp1->wwid);
merge_mpe(mp2, mp1);
free_mpe(mp1);
vector_del_slot(mptable, i);
i--;
break;
}
}
}
int
store_hwe (vector hwtable, struct hwentry * dhwe)
{
struct hwentry * hwe;
if (find_hwe_strmatch(hwtable, dhwe))
return 0;
if (!(hwe = alloc_hwe()))
return 1;
if (!dhwe->vendor || !(hwe->vendor = set_param_str(dhwe->vendor)))
goto out;
if (!dhwe->product || !(hwe->product = set_param_str(dhwe->product)))
goto out;
if (dhwe->revision && !(hwe->revision = set_param_str(dhwe->revision)))
goto out;
if (dhwe->uid_attribute && !(hwe->uid_attribute = set_param_str(dhwe->uid_attribute)))
goto out;
if (dhwe->features && !(hwe->features = set_param_str(dhwe->features)))
goto out;
if (dhwe->hwhandler && !(hwe->hwhandler = set_param_str(dhwe->hwhandler)))
goto out;
if (dhwe->selector && !(hwe->selector = set_param_str(dhwe->selector)))
goto out;
if (dhwe->checker_name && !(hwe->checker_name = set_param_str(dhwe->checker_name)))
goto out;
if (dhwe->prio_name && !(hwe->prio_name = set_param_str(dhwe->prio_name)))
goto out;
if (dhwe->prio_args && !(hwe->prio_args = set_param_str(dhwe->prio_args)))
goto out;
if (dhwe->alias_prefix && !(hwe->alias_prefix = set_param_str(dhwe->alias_prefix)))
goto out;
hwe->pgpolicy = dhwe->pgpolicy;
hwe->pgfailback = dhwe->pgfailback;
hwe->rr_weight = dhwe->rr_weight;
hwe->no_path_retry = dhwe->no_path_retry;
hwe->minio = dhwe->minio;
hwe->minio_rq = dhwe->minio_rq;
hwe->flush_on_last_del = dhwe->flush_on_last_del;
hwe->fast_io_fail = dhwe->fast_io_fail;
hwe->dev_loss = dhwe->dev_loss;
hwe->eh_deadline = dhwe->eh_deadline;
hwe->user_friendly_names = dhwe->user_friendly_names;
hwe->retain_hwhandler = dhwe->retain_hwhandler;
hwe->detect_prio = dhwe->detect_prio;
hwe->detect_checker = dhwe->detect_checker;
hwe->detect_pgpolicy = dhwe->detect_pgpolicy;
hwe->detect_pgpolicy_use_tpg = dhwe->detect_pgpolicy_use_tpg;
hwe->ghost_delay = dhwe->ghost_delay;
hwe->vpd_vendor_id = dhwe->vpd_vendor_id;
if (dhwe->bl_product && !(hwe->bl_product = set_param_str(dhwe->bl_product)))
goto out;
if (!vector_alloc_slot(hwtable))
goto out;
vector_set_slot(hwtable, hwe);
return 0;
out:
free_hwe(hwe);
return 1;
}
static void
validate_pctable(struct hwentry *ovr, int idx, const char *table_desc)
{
struct pcentry *pce;
if (!ovr || !ovr->pctable)
return;
vector_foreach_slot_after(ovr->pctable, pce, idx) {
if (pce->type == PCE_INVALID) {
condlog(0, "protocol section in %s missing type",
table_desc);
vector_del_slot(ovr->pctable, idx--);
free(pce);
}
}
if (VECTOR_SIZE(ovr->pctable) == 0) {
vector_free(ovr->pctable);
ovr->pctable = NULL;
}
}
static void
merge_pctable(struct hwentry *ovr)
{
struct pcentry *pce1, *pce2;
int i, j;
if (!ovr || !ovr->pctable)
return;
vector_foreach_slot(ovr->pctable, pce1, i) {
j = i + 1;
vector_foreach_slot_after(ovr->pctable, pce2, j) {
if (pce1->type != pce2->type)
continue;
merge_pce(pce2,pce1);
vector_del_slot(ovr->pctable, i--);
free(pce1);
break;
}
}
}
static void
factorize_hwtable (vector hw, int n, const char *table_desc)
{
struct hwentry *hwe1, *hwe2;
int i, j;
restart:
vector_foreach_slot(hw, hwe1, i) {
/* drop invalid device configs */
if (i >= n && (!hwe1->vendor || !hwe1->product)) {
condlog(0, "device config in %s missing vendor or product parameter",
table_desc);
vector_del_slot(hw, i--);
free_hwe(hwe1);
continue;
}
j = n > i + 1 ? n : i + 1;
vector_foreach_slot_after(hw, hwe2, j) {
if (hwe_strmatch(hwe2, hwe1) == 0) {
condlog(i >= n ? 1 : 3,
"%s: duplicate device section for %s:%s:%s in %s",
__func__, hwe1->vendor, hwe1->product,
hwe1->revision, table_desc);
vector_del_slot(hw, i);
merge_hwe(hwe2, hwe1);
free_hwe(hwe1);
if (i < n)
n -= 1;
/*
* Play safe here; we have modified
* the original vector so the outer
* vector_foreach_slot() might
* become confused.
*/
goto restart;
}
}
}
return;
}
static struct config *alloc_config (void)
{
return (struct config *)calloc(1, sizeof(struct config));
}
static void _uninit_config(struct config *conf)
{
void *ptr;
int i;
if (!conf)
conf = &internal_config;
if (conf->selector)
free(conf->selector);
if (conf->uid_attribute)
free(conf->uid_attribute);
vector_foreach_slot(&conf->uid_attrs, ptr, i)
free(ptr);
vector_reset(&conf->uid_attrs);
if (conf->features)
free(conf->features);
if (conf->hwhandler)
free(conf->hwhandler);
if (conf->prio_name)
free(conf->prio_name);
if (conf->alias_prefix)
free(conf->alias_prefix);
if (conf->partition_delim)
free(conf->partition_delim);
if (conf->prio_args)
free(conf->prio_args);
if (conf->checker_name)
free(conf->checker_name);
if (conf->enable_foreign)
free(conf->enable_foreign);
free_blacklist(conf->blist_devnode);
free_blacklist(conf->blist_wwid);
free_blacklist(conf->blist_property);
free_blacklist(conf->blist_protocol);
free_blacklist_device(conf->blist_device);
free_blacklist(conf->elist_devnode);
free_blacklist(conf->elist_wwid);
free_blacklist(conf->elist_property);
free_blacklist(conf->elist_protocol);
free_blacklist_device(conf->elist_device);
free_mptable(conf->mptable);
free_hwtable(conf->hwtable);
free_hwe(conf->overrides);
free_keywords(conf->keywords);
memset(conf, 0, sizeof(*conf));
}
void uninit_config(void)
{
_uninit_config(&internal_config);
}
void free_config(struct config *conf)
{
if (!conf)
return;
else if (conf == &internal_config) {
condlog(0, "ERROR: %s called for internal config. Use uninit_config() instead",
__func__);
return;
}
_uninit_config(conf);
free(conf);
}
/* if multipath fails to process the config directory, it should continue,
* with just a warning message */
static void
process_config_dir(struct config *conf, char *dir)
{
struct dirent **namelist;
struct scandir_result sr;
int i, n;
char path[LINE_MAX];
int old_hwtable_size;
int old_pctable_size = 0;
if (dir[0] != '/') {
condlog(1, "config_dir '%s' must be a fully qualified path",
dir);
return;
}
n = scandir(dir, &namelist, NULL, alphasort);
if (n < 0) {
if (errno == ENOENT)
condlog(3, "No configuration dir '%s'", dir);
else
condlog(0, "couldn't open configuration dir '%s': %s",
dir, strerror(errno));
return;
} else if (n == 0)
return;
sr.di = namelist;
sr.n = n;
pthread_cleanup_push_cast(free_scandir_result, &sr);
for (i = 0; i < n; i++) {
char *ext = strrchr(namelist[i]->d_name, '.');
if (!ext || strcmp(ext, ".conf"))
continue;
old_hwtable_size = VECTOR_SIZE(conf->hwtable);
old_pctable_size = conf->overrides ?
VECTOR_SIZE(conf->overrides->pctable) : 0;
snprintf(path, LINE_MAX, "%s/%s", dir, namelist[i]->d_name);
path[LINE_MAX-1] = '\0';
process_file(conf, path);
factorize_hwtable(conf->hwtable, old_hwtable_size,
namelist[i]->d_name);
validate_pctable(conf->overrides, old_pctable_size,
namelist[i]->d_name);
}
pthread_cleanup_pop(1);
}
static int init_config__ (const char *file, struct config *conf);
int init_config(const char *file)
{
return init_config__(file, &internal_config);
}
struct config *load_config(const char *file)
{
struct config *conf = alloc_config();
if (conf && !init_config__(file, conf))
return conf;
free(conf);
return NULL;
}
int init_config__ (const char *file, struct config *conf)
{
if (!conf)
conf = &internal_config;
/*
* Processing the config file will overwrite conf->verbosity if set
* When we return, we'll copy the config value back
*/
conf->verbosity = libmp_verbosity;
/*
* internal defaults
*/
get_sys_max_fds(&conf->max_fds);
conf->attribute_flags = 0;
conf->reassign_maps = DEFAULT_REASSIGN_MAPS;
conf->checkint = CHECKINT_UNDEF;
conf->max_checkint = 0;
conf->force_sync = DEFAULT_FORCE_SYNC;
conf->partition_delim = (default_partition_delim != NULL ?
strdup(default_partition_delim) : NULL);
conf->processed_main_config = 0;
conf->find_multipaths = DEFAULT_FIND_MULTIPATHS;
conf->uxsock_timeout = DEFAULT_REPLY_TIMEOUT;
conf->retrigger_tries = DEFAULT_RETRIGGER_TRIES;
conf->retrigger_delay = DEFAULT_RETRIGGER_DELAY;
conf->uev_wait_timeout = DEFAULT_UEV_WAIT_TIMEOUT;
conf->auto_resize = DEFAULT_AUTO_RESIZE;
conf->remove_retries = 0;
conf->ghost_delay = DEFAULT_GHOST_DELAY;
conf->all_tg_pt = DEFAULT_ALL_TG_PT;
conf->recheck_wwid = DEFAULT_RECHECK_WWID;
/*
* preload default hwtable
*/
conf->hwtable = vector_alloc();
if (!conf->hwtable)
goto out;
if (setup_default_hwtable(conf->hwtable))
goto out;
#ifdef CHECK_BUILTIN_HWTABLE
factorize_hwtable(conf->hwtable, 0, "builtin");
#endif
/*
* read the config file
*/
conf->keywords = vector_alloc();
init_keywords(conf->keywords);
if (filepresent(file)) {
int builtin_hwtable_size;
builtin_hwtable_size = VECTOR_SIZE(conf->hwtable);
if (process_file(conf, file)) {
condlog(0, "error parsing config file");
goto out;
}
factorize_hwtable(conf->hwtable, builtin_hwtable_size, file);
validate_pctable(conf->overrides, 0, file);
}
conf->processed_main_config = 1;
process_config_dir(conf, CONFIG_DIR);
/*
* fill the voids left in the config file
*/
if (conf->max_checkint == 0) {
if (conf->checkint == CHECKINT_UNDEF)
conf->checkint = DEFAULT_CHECKINT;
conf->max_checkint = (conf->checkint < UINT_MAX / 4 ?
conf->checkint * 4 : UINT_MAX);
} else if (conf->checkint == CHECKINT_UNDEF)
conf->checkint = (conf->max_checkint >= 4 ?
conf->max_checkint / 4 : 1);
else if (conf->checkint > conf->max_checkint)
conf->checkint = conf->max_checkint;
condlog(3, "polling interval: %d, max: %d",
conf->checkint, conf->max_checkint);
/*
* make sure that that adjust_int is a multiple of all possible values
* of pp->checkint.
*/
if (conf->max_checkint % conf->checkint == 0) {
conf->adjust_int = conf->max_checkint;
} else {
conf->adjust_int = conf->checkint;
while (2 * conf->adjust_int < conf->max_checkint)
conf->adjust_int *= 2;
conf->adjust_int *= conf->max_checkint;
}
if (conf->blist_devnode == NULL) {
conf->blist_devnode = vector_alloc();
if (!conf->blist_devnode)
goto out;
}
if (conf->blist_wwid == NULL) {
conf->blist_wwid = vector_alloc();
if (!conf->blist_wwid)
goto out;
}
if (conf->blist_device == NULL) {
conf->blist_device = vector_alloc();
if (!conf->blist_device)
goto out;
}
if (conf->blist_property == NULL) {
conf->blist_property = vector_alloc();
if (!conf->blist_property)
goto out;
}
if (conf->blist_protocol == NULL) {
conf->blist_protocol = vector_alloc();
if (!conf->blist_protocol)
goto out;
}
if (conf->elist_devnode == NULL) {
conf->elist_devnode = vector_alloc();
if (!conf->elist_devnode)
goto out;
}
if (conf->elist_wwid == NULL) {
conf->elist_wwid = vector_alloc();
if (!conf->elist_wwid)
goto out;
}
if (conf->elist_device == NULL) {
conf->elist_device = vector_alloc();
if (!conf->elist_device)
goto out;
}
if (conf->elist_property == NULL) {
conf->elist_property = vector_alloc();
if (!conf->elist_property)
goto out;
}
if (conf->elist_protocol == NULL) {
conf->elist_protocol = vector_alloc();
if (!conf->elist_protocol)
goto out;
}
if (setup_default_blist(conf))
goto out;
if (conf->mptable == NULL) {
conf->mptable = vector_alloc();
if (!conf->mptable)
goto out;
}
merge_pctable(conf->overrides);
merge_mptable(conf->mptable);
merge_blacklist(conf->blist_devnode);
merge_blacklist(conf->blist_property);
merge_blacklist(conf->blist_wwid);
merge_blacklist_device(conf->blist_device);
merge_blacklist(conf->elist_devnode);
merge_blacklist(conf->elist_property);
merge_blacklist(conf->elist_wwid);
merge_blacklist_device(conf->elist_device);
libmp_verbosity = conf->verbosity;
return 0;
out:
_uninit_config(conf);
return 1;
}
const char *get_uid_attribute_by_attrs(const struct config *conf,
const char *path_dev)
{
const struct vector_s *uid_attrs = &conf->uid_attrs;
int j;
char *att, *col;
vector_foreach_slot(uid_attrs, att, j) {
col = strrchr(att, ':');
if (!col)
continue;
if (!strncmp(path_dev, att, col - att))
return col + 1;
}
return NULL;
}
int parse_uid_attrs(char *uid_attrs, struct config *conf)
{
vector attrs = &conf->uid_attrs;
char *uid_attr_record, *tmp;
int ret = 0, count;
if (!uid_attrs)
return 1;
count = get_word(uid_attrs, &uid_attr_record);
while (uid_attr_record) {
tmp = strchr(uid_attr_record, ':');
if (!tmp) {
condlog(2, "invalid record in uid_attrs: %s",
uid_attr_record);
free(uid_attr_record);
ret = 1;
} else if (!vector_alloc_slot(attrs)) {
free(uid_attr_record);
ret = 1;
} else
vector_set_slot(attrs, uid_attr_record);
if (!count)
break;
uid_attrs += count;
count = get_word(uid_attrs, &uid_attr_record);
}
return ret;
}
|