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 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
|
// SPDX-License-Identifier: CDDL-1.0
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or https://opensource.org/licenses/CDDL-1.0.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2018 Intel Corporation.
* Copyright (c) 2020 by Lawrence Livermore National Security, LLC.
*/
#include <stdio.h>
#include <zlib.h>
#include <zfs_fletcher.h>
#include <sys/vdev_draid.h>
#include <sys/nvpair.h>
#include <sys/stat.h>
/*
* The number of rows to generate for new permutation maps.
*/
#define MAP_ROWS_DEFAULT 256
/*
* Key values for dRAID maps when stored as nvlists.
*/
#define MAP_SEED "seed"
#define MAP_CHECKSUM "checksum"
#define MAP_WORST_RATIO "worst_ratio"
#define MAP_AVG_RATIO "avg_ratio"
#define MAP_CHILDREN "children"
#define MAP_NPERMS "nperms"
#define MAP_PERMS "perms"
static void
draid_usage(void)
{
(void) fprintf(stderr,
"usage: draid command args ...\n"
"Available commands are:\n"
"\n"
"\tdraid generate [-cv] [-m min] [-n max] [-p passes] FILE\n"
"\tdraid verify [-rv] FILE\n"
"\tdraid dump [-v] [-m min] [-n max] FILE\n"
"\tdraid table FILE\n"
"\tdraid merge FILE SRC SRC...\n");
exit(1);
}
static int
read_map(const char *filename, nvlist_t **allcfgs)
{
int block_size = 131072;
int buf_size = 131072;
int tmp_size, error;
char *tmp_buf;
struct stat64 stat;
if (lstat64(filename, &stat) != 0)
return (errno);
if (stat.st_size == 0 ||
!(S_ISREG(stat.st_mode) || S_ISLNK(stat.st_mode))) {
return (EINVAL);
}
gzFile fp = gzopen(filename, "rb");
if (fp == Z_NULL)
return (errno);
char *buf = malloc(buf_size);
if (buf == NULL) {
(void) gzclose(fp);
return (ENOMEM);
}
ssize_t rc, bytes = 0;
while (!gzeof(fp)) {
rc = gzread(fp, buf + bytes, block_size);
if ((rc < 0) || (rc == 0 && !gzeof(fp))) {
free(buf);
(void) gzerror(fp, &error);
(void) gzclose(fp);
return (error);
} else {
bytes += rc;
if (bytes + block_size >= buf_size) {
tmp_size = 2 * buf_size;
tmp_buf = malloc(tmp_size);
if (tmp_buf == NULL) {
free(buf);
(void) gzclose(fp);
return (ENOMEM);
}
memcpy(tmp_buf, buf, bytes);
free(buf);
buf = tmp_buf;
buf_size = tmp_size;
}
}
}
(void) gzclose(fp);
error = nvlist_unpack(buf, bytes, allcfgs, 0);
free(buf);
return (error);
}
/*
* Read a map from the specified filename. A file contains multiple maps
* which are indexed by the number of children. The caller is responsible
* for freeing the configuration returned.
*/
static int
read_map_key(const char *filename, const char *key, nvlist_t **cfg)
{
nvlist_t *allcfgs, *foundcfg = NULL;
int error;
error = read_map(filename, &allcfgs);
if (error != 0)
return (error);
(void) nvlist_lookup_nvlist(allcfgs, key, &foundcfg);
if (foundcfg != NULL) {
nvlist_dup(foundcfg, cfg, KM_SLEEP);
error = 0;
} else {
error = ENOENT;
}
nvlist_free(allcfgs);
return (error);
}
/*
* Write all mappings to the map file.
*/
static int
write_map(const char *filename, nvlist_t *allcfgs)
{
size_t buflen = 0;
int error;
error = nvlist_size(allcfgs, &buflen, NV_ENCODE_XDR);
if (error)
return (error);
char *buf = malloc(buflen);
if (buf == NULL)
return (ENOMEM);
error = nvlist_pack(allcfgs, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
if (error) {
free(buf);
return (error);
}
/*
* Atomically update the file using a temporary file and the
* traditional unlink then rename steps. This code provides
* no locking, it only guarantees the packed nvlist on disk
* is updated atomically and is internally consistent.
*/
char *tmpname = calloc(1, MAXPATHLEN);
if (tmpname == NULL) {
free(buf);
return (ENOMEM);
}
snprintf(tmpname, MAXPATHLEN - 1, "%s.XXXXXX", filename);
int fd = mkstemp(tmpname);
if (fd < 0) {
error = errno;
free(buf);
free(tmpname);
return (error);
}
(void) close(fd);
gzFile fp = gzopen(tmpname, "w9b");
if (fp == Z_NULL) {
error = errno;
free(buf);
free(tmpname);
return (errno);
}
ssize_t rc, bytes = 0;
while (bytes < buflen) {
size_t size = MIN(buflen - bytes, 131072);
rc = gzwrite(fp, buf + bytes, size);
if (rc < 0) {
free(buf);
(void) gzerror(fp, &error);
(void) gzclose(fp);
(void) unlink(tmpname);
free(tmpname);
return (error);
} else if (rc == 0) {
break;
} else {
bytes += rc;
}
}
free(buf);
(void) gzclose(fp);
if (bytes != buflen) {
(void) unlink(tmpname);
free(tmpname);
return (EIO);
}
/*
* Unlink the previous config file and replace it with the updated
* version. If we're able to unlink the file then directory is
* writable by us and the subsequent rename should never fail.
*/
error = unlink(filename);
if (error != 0 && errno != ENOENT) {
error = errno;
(void) unlink(tmpname);
free(tmpname);
return (error);
}
error = rename(tmpname, filename);
if (error != 0) {
error = errno;
(void) unlink(tmpname);
free(tmpname);
return (error);
}
free(tmpname);
return (0);
}
/*
* Add the dRAID map to the file and write it out.
*/
static int
write_map_key(const char *filename, char *key, draid_map_t *map,
double worst_ratio, double avg_ratio)
{
nvlist_t *nv_cfg, *allcfgs;
int error;
/*
* Add the configuration to an existing or new file. The new
* configuration will replace an existing configuration with the
* same key if it has a lower ratio and is therefore better.
*/
error = read_map(filename, &allcfgs);
if (error == ENOENT) {
allcfgs = fnvlist_alloc();
} else if (error != 0) {
return (error);
}
error = nvlist_lookup_nvlist(allcfgs, key, &nv_cfg);
if (error == 0) {
uint64_t nv_cfg_worst_ratio = fnvlist_lookup_uint64(nv_cfg,
MAP_WORST_RATIO);
double nv_worst_ratio = (double)nv_cfg_worst_ratio / 1000.0;
if (worst_ratio < nv_worst_ratio) {
/* Replace old map with the more balanced new map. */
fnvlist_remove(allcfgs, key);
} else {
/* The old map is preferable, keep it. */
nvlist_free(allcfgs);
return (EEXIST);
}
}
nvlist_t *cfg = fnvlist_alloc();
fnvlist_add_uint64(cfg, MAP_SEED, map->dm_seed);
fnvlist_add_uint64(cfg, MAP_CHECKSUM, map->dm_checksum);
fnvlist_add_uint64(cfg, MAP_CHILDREN, map->dm_children);
fnvlist_add_uint64(cfg, MAP_NPERMS, map->dm_nperms);
fnvlist_add_uint8_array(cfg, MAP_PERMS, map->dm_perms,
map->dm_children * map->dm_nperms * sizeof (uint8_t));
fnvlist_add_uint64(cfg, MAP_WORST_RATIO,
(uint64_t)(worst_ratio * 1000.0));
fnvlist_add_uint64(cfg, MAP_AVG_RATIO,
(uint64_t)(avg_ratio * 1000.0));
error = nvlist_add_nvlist(allcfgs, key, cfg);
if (error == 0)
error = write_map(filename, allcfgs);
nvlist_free(cfg);
nvlist_free(allcfgs);
return (error);
}
static void
dump_map(draid_map_t *map, const char *key, double worst_ratio,
double avg_ratio, int verbose)
{
if (verbose == 0) {
return;
} else if (verbose == 1) {
printf(" \"%s\": seed: 0x%016llx worst_ratio: %2.03f "
"avg_ratio: %2.03f\n", key, (u_longlong_t)map->dm_seed,
worst_ratio, avg_ratio);
return;
} else {
printf(" \"%s\":\n"
" seed: 0x%016llx\n"
" checksum: 0x%016llx\n"
" worst_ratio: %2.03f\n"
" avg_ratio: %2.03f\n"
" children: %llu\n"
" nperms: %llu\n",
key, (u_longlong_t)map->dm_seed,
(u_longlong_t)map->dm_checksum, worst_ratio, avg_ratio,
(u_longlong_t)map->dm_children,
(u_longlong_t)map->dm_nperms);
if (verbose > 2) {
printf(" perms = {\n");
for (int i = 0; i < map->dm_nperms; i++) {
printf(" { ");
for (int j = 0; j < map->dm_children; j++) {
printf("%3d%s ", map->dm_perms[
i * map->dm_children + j],
j < map->dm_children - 1 ?
"," : "");
}
printf(" },\n");
}
printf(" }\n");
} else if (verbose == 2) {
printf(" draid_perms = <omitted>\n");
}
}
}
static void
dump_map_nv(const char *key, nvlist_t *cfg, int verbose)
{
draid_map_t map;
uint_t c;
uint64_t worst_ratio = fnvlist_lookup_uint64(cfg, MAP_WORST_RATIO);
uint64_t avg_ratio = fnvlist_lookup_uint64(cfg, MAP_AVG_RATIO);
map.dm_seed = fnvlist_lookup_uint64(cfg, MAP_SEED);
map.dm_checksum = fnvlist_lookup_uint64(cfg, MAP_CHECKSUM);
map.dm_children = fnvlist_lookup_uint64(cfg, MAP_CHILDREN);
map.dm_nperms = fnvlist_lookup_uint64(cfg, MAP_NPERMS);
map.dm_perms = fnvlist_lookup_uint8_array(cfg, MAP_PERMS, &c);
dump_map(&map, key, (double)worst_ratio / 1000.0,
avg_ratio / 1000.0, verbose);
}
/*
* Print a summary of the mapping.
*/
static int
dump_map_key(const char *filename, const char *key, int verbose)
{
nvlist_t *cfg;
int error;
error = read_map_key(filename, key, &cfg);
if (error != 0)
return (error);
dump_map_nv(key, cfg, verbose);
return (0);
}
/*
* Allocate a new permutation map for evaluation.
*/
static int
alloc_new_map(uint64_t children, uint64_t nperms, uint64_t seed,
draid_map_t **mapp)
{
draid_map_t *map;
int error;
map = malloc(sizeof (draid_map_t));
if (map == NULL)
return (ENOMEM);
map->dm_children = children;
map->dm_nperms = nperms;
map->dm_seed = seed;
map->dm_checksum = 0;
error = vdev_draid_generate_perms(map, &map->dm_perms);
if (error) {
free(map);
return (error);
}
*mapp = map;
return (0);
}
/*
* Allocate the fixed permutation map for N children.
*/
static int
alloc_fixed_map(uint64_t children, draid_map_t **mapp)
{
const draid_map_t *fixed_map;
draid_map_t *map;
int error;
error = vdev_draid_lookup_map(children, &fixed_map);
if (error)
return (error);
map = malloc(sizeof (draid_map_t));
if (map == NULL)
return (ENOMEM);
memcpy(map, fixed_map, sizeof (draid_map_t));
VERIFY3U(map->dm_checksum, !=, 0);
error = vdev_draid_generate_perms(map, &map->dm_perms);
if (error) {
free(map);
return (error);
}
*mapp = map;
return (0);
}
/*
* Free a permutation map.
*/
static void
free_map(draid_map_t *map)
{
free(map->dm_perms);
free(map);
}
/*
* Check if dev is in the provided list of faulted devices.
*/
static inline boolean_t
is_faulted(int *faulted_devs, int nfaulted, int dev)
{
for (int i = 0; i < nfaulted; i++)
if (faulted_devs[i] == dev)
return (B_TRUE);
return (B_FALSE);
}
/*
* Evaluate how resilvering I/O will be distributed given a list of faulted
* vdevs. As a simplification we assume one IO is sufficient to repair each
* damaged device in a group.
*/
static double
eval_resilver(draid_map_t *map, uint64_t groupwidth, uint64_t nspares,
int *faulted_devs, int nfaulted, int *min_child_ios, int *max_child_ios)
{
uint64_t children = map->dm_children;
uint64_t ngroups = 1;
uint64_t ndisks = children - nspares;
/*
* Calculate the minimum number of groups required to fill a slice.
*/
while (ngroups * (groupwidth) % (children - nspares) != 0)
ngroups++;
int *ios = calloc(map->dm_children, sizeof (uint64_t));
ASSERT3P(ios, !=, NULL);
/* Resilver all rows */
for (int i = 0; i < map->dm_nperms; i++) {
uint8_t *row = &map->dm_perms[i * map->dm_children];
/* Resilver all groups with faulted drives */
for (int j = 0; j < ngroups; j++) {
uint64_t spareidx = map->dm_children - nspares;
boolean_t repair_needed = B_FALSE;
/* See if any devices in this group are faulted */
uint64_t groupstart = (j * groupwidth) % ndisks;
for (int k = 0; k < groupwidth; k++) {
uint64_t groupidx = (groupstart + k) % ndisks;
repair_needed = is_faulted(faulted_devs,
nfaulted, row[groupidx]);
if (repair_needed)
break;
}
if (repair_needed == B_FALSE)
continue;
/*
* This group is degraded. Calculate the number of
* reads the non-faulted drives require and the number
* of writes to the distributed hot spare for this row.
*/
for (int k = 0; k < groupwidth; k++) {
uint64_t groupidx = (groupstart + k) % ndisks;
if (!is_faulted(faulted_devs, nfaulted,
row[groupidx])) {
ios[row[groupidx]]++;
} else if (nspares > 0) {
while (is_faulted(faulted_devs,
nfaulted, row[spareidx])) {
spareidx++;
}
ASSERT3U(spareidx, <, map->dm_children);
ios[row[spareidx]]++;
spareidx++;
}
}
}
}
*min_child_ios = INT_MAX;
*max_child_ios = 0;
/*
* Find the drives with fewest and most required I/O. These values
* are used to calculate the imbalance ratio. To avoid returning an
* infinite value for permutations which have children that perform
* no IO a floor of 1 IO per child is set. This ensures a meaningful
* ratio is returned for comparison and it is not an uncommon when
* there are a large number of children.
*/
for (int i = 0; i < map->dm_children; i++) {
if (is_faulted(faulted_devs, nfaulted, i)) {
ASSERT0(ios[i]);
continue;
}
if (ios[i] == 0)
ios[i] = 1;
if (ios[i] < *min_child_ios)
*min_child_ios = ios[i];
if (ios[i] > *max_child_ios)
*max_child_ios = ios[i];
}
ASSERT3S(*min_child_ios, !=, INT_MAX);
ASSERT3S(*max_child_ios, !=, 0);
double ratio = (double)(*max_child_ios) / (double)(*min_child_ios);
free(ios);
return (ratio);
}
/*
* Evaluate the quality of the permutation mapping by considering possible
* device failures. Returns the imbalance ratio for the worst mapping which
* is defined to be the largest number of child IOs over the fewest number
* child IOs. A value of 1.0 indicates the mapping is perfectly balance and
* all children perform an equal amount of work during reconstruction.
*/
static void
eval_decluster(draid_map_t *map, double *worst_ratiop, double *avg_ratiop)
{
uint64_t children = map->dm_children;
double worst_ratio = 1.0;
double sum = 0;
int worst_min_ios = 0, worst_max_ios = 0;
int n = 0;
/*
* When there are only 2 children there can be no distributed
* spare and no resilver to evaluate. Default to a ratio of 1.0
* for this degenerate case.
*/
if (children == VDEV_DRAID_MIN_CHILDREN) {
*worst_ratiop = 1.0;
*avg_ratiop = 1.0;
return;
}
/*
* Score the mapping as if it had either 1 or 2 distributed spares.
*/
for (int nspares = 1; nspares <= 2; nspares++) {
uint64_t faults = nspares;
/*
* Score groupwidths up to 19. This value was chosen as the
* largest reasonable width (16d+3p). dRAID pools may be still
* be created with wider stripes but they are not considered in
* this analysis in order to optimize for the most common cases.
*/
for (uint64_t groupwidth = 2;
groupwidth <= MIN(children - nspares, 19);
groupwidth++) {
int faulted_devs[2];
int min_ios, max_ios;
/*
* Score possible devices faults. This is limited
* to exactly one fault per distributed spare for
* the purposes of this similation.
*/
for (int f1 = 0; f1 < children; f1++) {
faulted_devs[0] = f1;
double ratio;
if (faults == 1) {
ratio = eval_resilver(map, groupwidth,
nspares, faulted_devs, faults,
&min_ios, &max_ios);
if (ratio > worst_ratio) {
worst_ratio = ratio;
worst_min_ios = min_ios;
worst_max_ios = max_ios;
}
sum += ratio;
n++;
} else if (faults == 2) {
for (int f2 = f1 + 1; f2 < children;
f2++) {
faulted_devs[1] = f2;
ratio = eval_resilver(map,
groupwidth, nspares,
faulted_devs, faults,
&min_ios, &max_ios);
if (ratio > worst_ratio) {
worst_ratio = ratio;
worst_min_ios = min_ios;
worst_max_ios = max_ios;
}
sum += ratio;
n++;
}
}
}
}
}
*worst_ratiop = worst_ratio;
*avg_ratiop = sum / n;
/*
* Log the min/max io values for particularly unbalanced maps.
* Since the maps are generated entirely randomly these are possible
* be exceedingly unlikely. We log it for possible investigation.
*/
if (worst_ratio > 100.0) {
dump_map(map, "DEBUG", worst_ratio, *avg_ratiop, 2);
printf("worst_min_ios=%d worst_max_ios=%d\n",
worst_min_ios, worst_max_ios);
}
}
static int
eval_maps(uint64_t children, int passes, uint64_t *map_seed,
draid_map_t **best_mapp, double *best_ratiop, double *avg_ratiop)
{
draid_map_t *best_map = NULL;
double best_worst_ratio = 1000.0;
double best_avg_ratio = 1000.0;
/*
* Perform the requested number of passes evaluating randomly
* generated permutation maps. Only the best version is kept.
*/
for (int i = 0; i < passes; i++) {
double worst_ratio, avg_ratio;
draid_map_t *map;
int error;
/*
* Calculate the next seed and generate a new candidate map.
*/
error = alloc_new_map(children, MAP_ROWS_DEFAULT,
vdev_draid_rand(map_seed), &map);
if (error) {
if (best_map != NULL)
free_map(best_map);
return (error);
}
/*
* Consider maps with a lower worst_ratio to be of higher
* quality. Some maps may have a lower avg_ratio but they
* are discarded since they might include some particularly
* imbalanced permutations. The average is tracked to in
* order to get a sense of the average permutation quality.
*/
eval_decluster(map, &worst_ratio, &avg_ratio);
if (best_map == NULL || worst_ratio < best_worst_ratio) {
if (best_map != NULL)
free_map(best_map);
best_map = map;
best_worst_ratio = worst_ratio;
best_avg_ratio = avg_ratio;
} else {
free_map(map);
}
}
/*
* After determining the best map generate a checksum over the full
* permutation array. This checksum is verified when opening a dRAID
* pool to ensure the generated in memory permutations are correct.
*/
zio_cksum_t cksum;
fletcher_4_native_varsize(best_map->dm_perms,
sizeof (uint8_t) * best_map->dm_children * best_map->dm_nperms,
&cksum);
best_map->dm_checksum = cksum.zc_word[0];
*best_mapp = best_map;
*best_ratiop = best_worst_ratio;
*avg_ratiop = best_avg_ratio;
return (0);
}
static int
draid_generate(int argc, char *argv[])
{
char filename[MAXPATHLEN] = {0};
uint64_t map_seed[2];
int c, fd, error, verbose = 0, passes = 1, continuous = 0;
int min_children = VDEV_DRAID_MIN_CHILDREN;
int max_children = VDEV_DRAID_MAX_CHILDREN;
int restarts = 0;
while ((c = getopt(argc, argv, ":cm:n:p:v")) != -1) {
switch (c) {
case 'c':
continuous++;
break;
case 'm':
min_children = (int)strtol(optarg, NULL, 0);
if (min_children < VDEV_DRAID_MIN_CHILDREN) {
(void) fprintf(stderr, "A minimum of 2 "
"children are required.\n");
return (1);
}
break;
case 'n':
max_children = (int)strtol(optarg, NULL, 0);
if (max_children > VDEV_DRAID_MAX_CHILDREN) {
(void) fprintf(stderr, "A maximum of %d "
"children are allowed.\n",
VDEV_DRAID_MAX_CHILDREN);
return (1);
}
break;
case 'p':
passes = (int)strtol(optarg, NULL, 0);
break;
case 'v':
/*
* 0 - Only log when a better map is added to the file.
* 1 - Log the current best map for each child count.
* Minimal output on a single summary line.
* 2 - Log the current best map for each child count.
* More verbose includes most map fields.
* 3 - Log the current best map for each child count.
* Very verbose all fields including the full map.
*/
verbose++;
break;
case ':':
(void) fprintf(stderr,
"missing argument for '%c' option\n", optopt);
draid_usage();
break;
case '?':
(void) fprintf(stderr, "invalid option '%c'\n",
optopt);
draid_usage();
break;
}
}
if (argc > optind)
strlcpy(filename, argv[optind], sizeof (filename));
else {
(void) fprintf(stderr, "A FILE must be specified.\n");
return (1);
}
restart:
/*
* Start with a fresh seed from /dev/urandom.
*/
fd = open("/dev/urandom", O_RDONLY);
if (fd < 0) {
printf("Unable to open /dev/urandom: %s\n:", strerror(errno));
return (1);
} else {
ssize_t bytes = sizeof (map_seed);
ssize_t bytes_read = 0;
while (bytes_read < bytes) {
ssize_t rc = read(fd, ((char *)map_seed) + bytes_read,
bytes - bytes_read);
if (rc < 0) {
printf("Unable to read /dev/urandom: %s\n:",
strerror(errno));
close(fd);
return (1);
}
bytes_read += rc;
}
(void) close(fd);
}
if (restarts == 0)
printf("Writing generated mappings to '%s':\n", filename);
/*
* Generate maps for all requested child counts. The best map for
* each child count is written out to the specified file. If the file
* already contains a better mapping this map will not be added.
*/
for (uint64_t children = min_children;
children <= max_children; children++) {
char key[8] = { 0 };
draid_map_t *map;
double worst_ratio = 1000.0;
double avg_ratio = 1000.0;
error = eval_maps(children, passes, map_seed, &map,
&worst_ratio, &avg_ratio);
if (error) {
printf("Error eval_maps(): %s\n", strerror(error));
return (1);
}
if (worst_ratio < 1.0 || avg_ratio < 1.0) {
printf("Error ratio < 1.0: worst_ratio = %2.03f "
"avg_ratio = %2.03f\n", worst_ratio, avg_ratio);
return (1);
}
snprintf(key, 7, "%llu", (u_longlong_t)children);
error = write_map_key(filename, key, map, worst_ratio,
avg_ratio);
if (error == 0) {
/* The new map was added to the file. */
dump_map(map, key, worst_ratio, avg_ratio,
MAX(verbose, 1));
} else if (error == EEXIST) {
/* The existing map was preferable and kept. */
if (verbose > 0)
dump_map_key(filename, key, verbose);
} else {
printf("Error write_map_key(): %s\n", strerror(error));
return (1);
}
free_map(map);
}
/*
* When the continuous option is set restart at the minimum number of
* children instead of exiting. This option is useful as a mechanism
* to continuous try and refine the discovered permutations.
*/
if (continuous) {
restarts++;
printf("Restarting by request (-c): %d\n", restarts);
goto restart;
}
return (0);
}
/*
* Verify each map in the file by generating its in-memory permutation array
* and comfirming its checksum is correct.
*/
static int
draid_verify(int argc, char *argv[])
{
char filename[MAXPATHLEN] = {0};
int n = 0, c, error, verbose = 1;
int check_ratios = 0;
while ((c = getopt(argc, argv, ":rv")) != -1) {
switch (c) {
case 'r':
check_ratios++;
break;
case 'v':
verbose++;
break;
case ':':
(void) fprintf(stderr,
"missing argument for '%c' option\n", optopt);
draid_usage();
break;
case '?':
(void) fprintf(stderr, "invalid option '%c'\n",
optopt);
draid_usage();
break;
}
}
if (argc > optind) {
char *abspath = malloc(MAXPATHLEN);
if (abspath == NULL)
return (ENOMEM);
if (realpath(argv[optind], abspath) != NULL)
strlcpy(filename, abspath, sizeof (filename));
else
strlcpy(filename, argv[optind], sizeof (filename));
free(abspath);
} else {
(void) fprintf(stderr, "A FILE must be specified.\n");
return (1);
}
printf("Verifying permutation maps: '%s'\n", filename);
/*
* Lookup hardcoded permutation map for each valid number of children
* and verify a generated map has the correct checksum. Then compare
* the generated map values with the nvlist map values read from the
* reference file to cross-check the permutation.
*/
for (uint64_t children = VDEV_DRAID_MIN_CHILDREN;
children <= VDEV_DRAID_MAX_CHILDREN;
children++) {
draid_map_t *map;
char key[8] = {0};
snprintf(key, 8, "%llu", (u_longlong_t)children);
error = alloc_fixed_map(children, &map);
if (error) {
printf("Error alloc_fixed_map() failed: %s\n",
error == ECKSUM ? "Invalid checksum" :
strerror(error));
return (1);
}
uint64_t nv_seed, nv_checksum, nv_children, nv_nperms;
uint8_t *nv_perms;
nvlist_t *cfg;
uint_t c;
error = read_map_key(filename, key, &cfg);
if (error != 0) {
printf("Error read_map_key() failed: %s\n",
strerror(error));
free_map(map);
return (1);
}
nv_seed = fnvlist_lookup_uint64(cfg, MAP_SEED);
nv_checksum = fnvlist_lookup_uint64(cfg, MAP_CHECKSUM);
nv_children = fnvlist_lookup_uint64(cfg, MAP_CHILDREN);
nv_nperms = fnvlist_lookup_uint64(cfg, MAP_NPERMS);
nvlist_lookup_uint8_array(cfg, MAP_PERMS, &nv_perms, &c);
/*
* Compare draid_map_t and nvlist reference values.
*/
if (map->dm_seed != nv_seed) {
printf("Error different seeds: 0x%016llx != "
"0x%016llx\n", (u_longlong_t)map->dm_seed,
(u_longlong_t)nv_seed);
error = EINVAL;
}
if (map->dm_checksum != nv_checksum) {
printf("Error different checksums: 0x%016llx "
"!= 0x%016llx\n",
(u_longlong_t)map->dm_checksum,
(u_longlong_t)nv_checksum);
error = EINVAL;
}
if (map->dm_children != nv_children) {
printf("Error different children: %llu "
"!= %llu\n", (u_longlong_t)map->dm_children,
(u_longlong_t)nv_children);
error = EINVAL;
}
if (map->dm_nperms != nv_nperms) {
printf("Error different nperms: %llu "
"!= %llu\n", (u_longlong_t)map->dm_nperms,
(u_longlong_t)nv_nperms);
error = EINVAL;
}
for (uint64_t i = 0; i < nv_children * nv_nperms; i++) {
if (map->dm_perms[i] != nv_perms[i]) {
printf("Error different perms[%llu]: "
"%d != %d\n", (u_longlong_t)i,
(int)map->dm_perms[i],
(int)nv_perms[i]);
error = EINVAL;
break;
}
}
/*
* For good measure recalculate the worst and average
* ratios and confirm they match the nvlist values.
*/
if (check_ratios) {
uint64_t nv_worst_ratio, nv_avg_ratio;
double worst_ratio, avg_ratio;
eval_decluster(map, &worst_ratio, &avg_ratio);
nv_worst_ratio = fnvlist_lookup_uint64(cfg,
MAP_WORST_RATIO);
nv_avg_ratio = fnvlist_lookup_uint64(cfg,
MAP_AVG_RATIO);
if (worst_ratio < 1.0 || avg_ratio < 1.0) {
printf("Error ratio out of range %2.03f, "
"%2.03f\n", worst_ratio, avg_ratio);
error = EINVAL;
}
if ((uint64_t)(worst_ratio * 1000.0) !=
nv_worst_ratio) {
printf("Error different worst_ratio %2.03f "
"!= %2.03f\n", (double)nv_worst_ratio /
1000.0, worst_ratio);
error = EINVAL;
}
if ((uint64_t)(avg_ratio * 1000.0) != nv_avg_ratio) {
printf("Error different average_ratio %2.03f "
"!= %2.03f\n", (double)nv_avg_ratio /
1000.0, avg_ratio);
error = EINVAL;
}
}
if (error) {
free_map(map);
nvlist_free(cfg);
return (1);
}
if (verbose > 0) {
printf("- %llu children: good\n",
(u_longlong_t)children);
}
n++;
free_map(map);
nvlist_free(cfg);
}
if (n != (VDEV_DRAID_MAX_CHILDREN - 1)) {
printf("Error permutation maps missing: %d / %d checked\n",
n, VDEV_DRAID_MAX_CHILDREN - 1);
return (1);
}
printf("Successfully verified %d / %d permutation maps\n",
n, VDEV_DRAID_MAX_CHILDREN - 1);
return (0);
}
/*
* Dump the contents of the specified mapping(s) for inspection.
*/
static int
draid_dump(int argc, char *argv[])
{
char filename[MAXPATHLEN] = {0};
int c, error, verbose = 1;
int min_children = VDEV_DRAID_MIN_CHILDREN;
int max_children = VDEV_DRAID_MAX_CHILDREN;
while ((c = getopt(argc, argv, ":vm:n:")) != -1) {
switch (c) {
case 'm':
min_children = (int)strtol(optarg, NULL, 0);
if (min_children < 2) {
(void) fprintf(stderr, "A minimum of 2 "
"children are required.\n");
return (1);
}
break;
case 'n':
max_children = (int)strtol(optarg, NULL, 0);
if (max_children > VDEV_DRAID_MAX_CHILDREN) {
(void) fprintf(stderr, "A maximum of %d "
"children are allowed.\n",
VDEV_DRAID_MAX_CHILDREN);
return (1);
}
break;
case 'v':
verbose++;
break;
case ':':
(void) fprintf(stderr,
"missing argument for '%c' option\n", optopt);
draid_usage();
break;
case '?':
(void) fprintf(stderr, "invalid option '%c'\n",
optopt);
draid_usage();
break;
}
}
if (argc > optind)
strlcpy(filename, argv[optind], sizeof (filename));
else {
(void) fprintf(stderr, "A FILE must be specified.\n");
return (1);
}
/*
* Dump maps for the requested child counts.
*/
for (uint64_t children = min_children;
children <= max_children; children++) {
char key[8] = { 0 };
snprintf(key, 7, "%llu", (u_longlong_t)children);
error = dump_map_key(filename, key, verbose);
if (error) {
printf("Error dump_map_key(): %s\n", strerror(error));
return (1);
}
}
return (0);
}
/*
* Print all of the mappings as a C formatted draid_map_t array. This table
* is found in the module/zcommon/zfs_draid.c file and is the definitive
* source for all mapping used by dRAID. It cannot be updated without
* changing the dRAID on disk format.
*/
static int
draid_table(int argc, char *argv[])
{
char filename[MAXPATHLEN] = {0};
int error;
if (argc > optind)
strlcpy(filename, argv[optind], sizeof (filename));
else {
(void) fprintf(stderr, "A FILE must be specified.\n");
return (1);
}
printf("static const draid_map_t "
"draid_maps[VDEV_DRAID_MAX_MAPS] = {\n");
for (uint64_t children = VDEV_DRAID_MIN_CHILDREN;
children <= VDEV_DRAID_MAX_CHILDREN;
children++) {
uint64_t seed, checksum, nperms, avg_ratio;
nvlist_t *cfg;
char key[8] = {0};
snprintf(key, 8, "%llu", (u_longlong_t)children);
error = read_map_key(filename, key, &cfg);
if (error != 0) {
printf("Error read_map_key() failed: %s\n",
strerror(error));
return (1);
}
seed = fnvlist_lookup_uint64(cfg, MAP_SEED);
checksum = fnvlist_lookup_uint64(cfg, MAP_CHECKSUM);
children = fnvlist_lookup_uint64(cfg, MAP_CHILDREN);
nperms = fnvlist_lookup_uint64(cfg, MAP_NPERMS);
avg_ratio = fnvlist_lookup_uint64(cfg, MAP_AVG_RATIO);
printf("\t{ %3llu, %3llu, 0x%016llx, 0x%016llx },\t"
"/* %2.03f */\n", (u_longlong_t)children,
(u_longlong_t)nperms, (u_longlong_t)seed,
(u_longlong_t)checksum, (double)avg_ratio / 1000.0);
nvlist_free(cfg);
}
printf("};\n");
return (0);
}
static int
draid_merge_impl(nvlist_t *allcfgs, const char *srcfilename, int *mergedp)
{
nvlist_t *srccfgs;
nvpair_t *elem = NULL;
int error, merged = 0;
error = read_map(srcfilename, &srccfgs);
if (error != 0)
return (error);
while ((elem = nvlist_next_nvpair(srccfgs, elem)) != NULL) {
uint64_t nv_worst_ratio;
uint64_t allcfg_worst_ratio;
nvlist_t *cfg, *allcfg;
const char *key;
switch (nvpair_type(elem)) {
case DATA_TYPE_NVLIST:
(void) nvpair_value_nvlist(elem, &cfg);
key = nvpair_name(elem);
nv_worst_ratio = fnvlist_lookup_uint64(cfg,
MAP_WORST_RATIO);
error = nvlist_lookup_nvlist(allcfgs, key, &allcfg);
if (error == 0) {
allcfg_worst_ratio = fnvlist_lookup_uint64(
allcfg, MAP_WORST_RATIO);
if (nv_worst_ratio < allcfg_worst_ratio) {
fnvlist_remove(allcfgs, key);
fnvlist_add_nvlist(allcfgs, key, cfg);
merged++;
}
} else if (error == ENOENT) {
fnvlist_add_nvlist(allcfgs, key, cfg);
merged++;
} else {
return (error);
}
break;
default:
continue;
}
}
nvlist_free(srccfgs);
*mergedp = merged;
return (0);
}
/*
* Merge the best map for each child count found in the listed files into
* a new file. This allows 'draid generate' to be run in parallel and for
* the results maps to be combined.
*/
static int
draid_merge(int argc, char *argv[])
{
char filename[MAXPATHLEN] = {0};
int c, error, total_merged = 0;
nvlist_t *allcfgs;
while ((c = getopt(argc, argv, ":")) != -1) {
switch (c) {
case ':':
(void) fprintf(stderr,
"missing argument for '%c' option\n", optopt);
draid_usage();
break;
case '?':
(void) fprintf(stderr, "invalid option '%c'\n",
optopt);
draid_usage();
break;
}
}
if (argc < 4) {
(void) fprintf(stderr,
"A FILE and multiple SRCs must be specified.\n");
return (1);
}
strlcpy(filename, argv[optind], sizeof (filename));
optind++;
error = read_map(filename, &allcfgs);
if (error == ENOENT) {
allcfgs = fnvlist_alloc();
} else if (error != 0) {
printf("Error read_map(): %s\n", strerror(error));
return (error);
}
while (optind < argc) {
char srcfilename[MAXPATHLEN] = {0};
int merged = 0;
strlcpy(srcfilename, argv[optind], sizeof (srcfilename));
error = draid_merge_impl(allcfgs, srcfilename, &merged);
if (error) {
printf("Error draid_merge_impl(): %s\n",
strerror(error));
nvlist_free(allcfgs);
return (1);
}
total_merged += merged;
printf("Merged %d key(s) from '%s' into '%s'\n", merged,
srcfilename, filename);
optind++;
}
if (total_merged > 0)
write_map(filename, allcfgs);
printf("Merged a total of %d key(s) into '%s'\n", total_merged,
filename);
nvlist_free(allcfgs);
return (0);
}
int
main(int argc, char *argv[])
{
if (argc < 2)
draid_usage();
char *subcommand = argv[1];
if (strcmp(subcommand, "generate") == 0) {
return (draid_generate(argc - 1, argv + 1));
} else if (strcmp(subcommand, "verify") == 0) {
return (draid_verify(argc - 1, argv + 1));
} else if (strcmp(subcommand, "dump") == 0) {
return (draid_dump(argc - 1, argv + 1));
} else if (strcmp(subcommand, "table") == 0) {
return (draid_table(argc - 1, argv + 1));
} else if (strcmp(subcommand, "merge") == 0) {
return (draid_merge(argc - 1, argv + 1));
} else {
draid_usage();
}
}
|