1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>. 28 */ 29 30 #include <libintl.h> 31 #include <libuutil.h> 32 #include <stddef.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <strings.h> 36 #include <thread_pool.h> 37 38 #include <libzfs.h> 39 #include <libzutil.h> 40 #include <sys/zfs_context.h> 41 #include <sys/wait.h> 42 43 #include "zpool_util.h" 44 45 /* 46 * Private interface for iterating over pools specified on the command line. 47 * Most consumers will call for_each_pool, but in order to support iostat, we 48 * allow fined grained control through the zpool_list_t interface. 49 */ 50 51 typedef struct zpool_node { 52 zpool_handle_t *zn_handle; 53 uu_avl_node_t zn_avlnode; 54 int zn_mark; 55 } zpool_node_t; 56 57 struct zpool_list { 58 boolean_t zl_findall; 59 boolean_t zl_literal; 60 uu_avl_t *zl_avl; 61 uu_avl_pool_t *zl_pool; 62 zprop_list_t **zl_proplist; 63 }; 64 65 /* ARGSUSED */ 66 static int 67 zpool_compare(const void *larg, const void *rarg, void *unused) 68 { 69 zpool_handle_t *l = ((zpool_node_t *)larg)->zn_handle; 70 zpool_handle_t *r = ((zpool_node_t *)rarg)->zn_handle; 71 const char *lname = zpool_get_name(l); 72 const char *rname = zpool_get_name(r); 73 74 return (strcmp(lname, rname)); 75 } 76 77 /* 78 * Callback function for pool_list_get(). Adds the given pool to the AVL tree 79 * of known pools. 80 */ 81 static int 82 add_pool(zpool_handle_t *zhp, void *data) 83 { 84 zpool_list_t *zlp = data; 85 zpool_node_t *node = safe_malloc(sizeof (zpool_node_t)); 86 uu_avl_index_t idx; 87 88 node->zn_handle = zhp; 89 uu_avl_node_init(node, &node->zn_avlnode, zlp->zl_pool); 90 if (uu_avl_find(zlp->zl_avl, node, NULL, &idx) == NULL) { 91 if (zlp->zl_proplist && 92 zpool_expand_proplist(zhp, zlp->zl_proplist, 93 zlp->zl_literal) 94 != 0) { 95 zpool_close(zhp); 96 free(node); 97 return (-1); 98 } 99 uu_avl_insert(zlp->zl_avl, node, idx); 100 } else { 101 zpool_close(zhp); 102 free(node); 103 return (-1); 104 } 105 106 return (0); 107 } 108 109 /* 110 * Create a list of pools based on the given arguments. If we're given no 111 * arguments, then iterate over all pools in the system and add them to the AVL 112 * tree. Otherwise, add only those pool explicitly specified on the command 113 * line. 114 */ 115 zpool_list_t * 116 pool_list_get(int argc, char **argv, zprop_list_t **proplist, 117 boolean_t literal, int *err) 118 { 119 zpool_list_t *zlp; 120 121 zlp = safe_malloc(sizeof (zpool_list_t)); 122 123 zlp->zl_pool = uu_avl_pool_create("zfs_pool", sizeof (zpool_node_t), 124 offsetof(zpool_node_t, zn_avlnode), zpool_compare, UU_DEFAULT); 125 126 if (zlp->zl_pool == NULL) 127 zpool_no_memory(); 128 129 if ((zlp->zl_avl = uu_avl_create(zlp->zl_pool, NULL, 130 UU_DEFAULT)) == NULL) 131 zpool_no_memory(); 132 133 zlp->zl_proplist = proplist; 134 135 zlp->zl_literal = literal; 136 137 if (argc == 0) { 138 (void) zpool_iter(g_zfs, add_pool, zlp); 139 zlp->zl_findall = B_TRUE; 140 } else { 141 int i; 142 143 for (i = 0; i < argc; i++) { 144 zpool_handle_t *zhp; 145 146 if ((zhp = zpool_open_canfail(g_zfs, argv[i])) != 147 NULL) { 148 if (add_pool(zhp, zlp) != 0) 149 *err = B_TRUE; 150 } else { 151 *err = B_TRUE; 152 } 153 } 154 } 155 156 return (zlp); 157 } 158 159 /* 160 * Search for any new pools, adding them to the list. We only add pools when no 161 * options were given on the command line. Otherwise, we keep the list fixed as 162 * those that were explicitly specified. 163 */ 164 void 165 pool_list_update(zpool_list_t *zlp) 166 { 167 if (zlp->zl_findall) 168 (void) zpool_iter(g_zfs, add_pool, zlp); 169 } 170 171 /* 172 * Iterate over all pools in the list, executing the callback for each 173 */ 174 int 175 pool_list_iter(zpool_list_t *zlp, int unavail, zpool_iter_f func, 176 void *data) 177 { 178 zpool_node_t *node, *next_node; 179 int ret = 0; 180 181 for (node = uu_avl_first(zlp->zl_avl); node != NULL; node = next_node) { 182 next_node = uu_avl_next(zlp->zl_avl, node); 183 if (zpool_get_state(node->zn_handle) != POOL_STATE_UNAVAIL || 184 unavail) 185 ret |= func(node->zn_handle, data); 186 } 187 188 return (ret); 189 } 190 191 /* 192 * Remove the given pool from the list. When running iostat, we want to remove 193 * those pools that no longer exist. 194 */ 195 void 196 pool_list_remove(zpool_list_t *zlp, zpool_handle_t *zhp) 197 { 198 zpool_node_t search, *node; 199 200 search.zn_handle = zhp; 201 if ((node = uu_avl_find(zlp->zl_avl, &search, NULL, NULL)) != NULL) { 202 uu_avl_remove(zlp->zl_avl, node); 203 zpool_close(node->zn_handle); 204 free(node); 205 } 206 } 207 208 /* 209 * Free all the handles associated with this list. 210 */ 211 void 212 pool_list_free(zpool_list_t *zlp) 213 { 214 uu_avl_walk_t *walk; 215 zpool_node_t *node; 216 217 if ((walk = uu_avl_walk_start(zlp->zl_avl, UU_WALK_ROBUST)) == NULL) { 218 (void) fprintf(stderr, 219 gettext("internal error: out of memory")); 220 exit(1); 221 } 222 223 while ((node = uu_avl_walk_next(walk)) != NULL) { 224 uu_avl_remove(zlp->zl_avl, node); 225 zpool_close(node->zn_handle); 226 free(node); 227 } 228 229 uu_avl_walk_end(walk); 230 uu_avl_destroy(zlp->zl_avl); 231 uu_avl_pool_destroy(zlp->zl_pool); 232 233 free(zlp); 234 } 235 236 /* 237 * Returns the number of elements in the pool list. 238 */ 239 int 240 pool_list_count(zpool_list_t *zlp) 241 { 242 return (uu_avl_numnodes(zlp->zl_avl)); 243 } 244 245 /* 246 * High level function which iterates over all pools given on the command line, 247 * using the pool_list_* interfaces. 248 */ 249 int 250 for_each_pool(int argc, char **argv, boolean_t unavail, 251 zprop_list_t **proplist, boolean_t literal, zpool_iter_f func, void *data) 252 { 253 zpool_list_t *list; 254 int ret = 0; 255 256 if ((list = pool_list_get(argc, argv, proplist, literal, &ret)) == NULL) 257 return (1); 258 259 if (pool_list_iter(list, unavail, func, data) != 0) 260 ret = 1; 261 262 pool_list_free(list); 263 264 return (ret); 265 } 266 267 /* 268 * This is the equivalent of for_each_pool() for vdevs. It iterates thorough 269 * all vdevs in the pool, ignoring root vdevs and holes, calling func() on 270 * each one. 271 * 272 * @zhp: Zpool handle 273 * @func: Function to call on each vdev 274 * @data: Custom data to pass to the function 275 */ 276 int 277 for_each_vdev(zpool_handle_t *zhp, pool_vdev_iter_f func, void *data) 278 { 279 nvlist_t *config, *nvroot = NULL; 280 281 if ((config = zpool_get_config(zhp, NULL)) != NULL) { 282 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 283 &nvroot) == 0); 284 } 285 return (for_each_vdev_cb((void *) zhp, nvroot, func, data)); 286 } 287 288 /* 289 * Process the vcdl->vdev_cmd_data[] array to figure out all the unique column 290 * names and their widths. When this function is done, vcdl->uniq_cols, 291 * vcdl->uniq_cols_cnt, and vcdl->uniq_cols_width will be filled in. 292 */ 293 static void 294 process_unique_cmd_columns(vdev_cmd_data_list_t *vcdl) 295 { 296 char **uniq_cols = NULL, **tmp = NULL; 297 int *uniq_cols_width; 298 vdev_cmd_data_t *data; 299 int cnt = 0; 300 int k; 301 302 /* For each vdev */ 303 for (int i = 0; i < vcdl->count; i++) { 304 data = &vcdl->data[i]; 305 /* For each column the vdev reported */ 306 for (int j = 0; j < data->cols_cnt; j++) { 307 /* Is this column in our list of unique column names? */ 308 for (k = 0; k < cnt; k++) { 309 if (strcmp(data->cols[j], uniq_cols[k]) == 0) 310 break; /* yes it is */ 311 } 312 if (k == cnt) { 313 /* No entry for column, add to list */ 314 tmp = realloc(uniq_cols, sizeof (*uniq_cols) * 315 (cnt + 1)); 316 if (tmp == NULL) 317 break; /* Nothing we can do... */ 318 uniq_cols = tmp; 319 uniq_cols[cnt] = data->cols[j]; 320 cnt++; 321 } 322 } 323 } 324 325 /* 326 * We now have a list of all the unique column names. Figure out the 327 * max width of each column by looking at the column name and all its 328 * values. 329 */ 330 uniq_cols_width = safe_malloc(sizeof (*uniq_cols_width) * cnt); 331 for (int i = 0; i < cnt; i++) { 332 /* Start off with the column title's width */ 333 uniq_cols_width[i] = strlen(uniq_cols[i]); 334 /* For each vdev */ 335 for (int j = 0; j < vcdl->count; j++) { 336 /* For each of the vdev's values in a column */ 337 data = &vcdl->data[j]; 338 for (k = 0; k < data->cols_cnt; k++) { 339 /* Does this vdev have a value for this col? */ 340 if (strcmp(data->cols[k], uniq_cols[i]) == 0) { 341 /* Is the value width larger? */ 342 uniq_cols_width[i] = 343 MAX(uniq_cols_width[i], 344 strlen(data->lines[k])); 345 } 346 } 347 } 348 } 349 350 vcdl->uniq_cols = uniq_cols; 351 vcdl->uniq_cols_cnt = cnt; 352 vcdl->uniq_cols_width = uniq_cols_width; 353 } 354 355 356 /* 357 * Process a line of command output 358 * 359 * When running 'zpool iostat|status -c' the lines of output can either be 360 * in the form of: 361 * 362 * column_name=value 363 * 364 * Or just: 365 * 366 * value 367 * 368 * Process the column_name (if any) and value. 369 * 370 * Returns 0 if line was processed, and there are more lines can still be 371 * processed. 372 * 373 * Returns 1 if this was the last line to process, or error. 374 */ 375 static int 376 vdev_process_cmd_output(vdev_cmd_data_t *data, char *line) 377 { 378 char *col = NULL; 379 char *val = line; 380 char *equals; 381 char **tmp; 382 383 if (line == NULL) 384 return (1); 385 386 equals = strchr(line, '='); 387 if (equals != NULL) { 388 /* 389 * We have a 'column=value' type line. Split it into the 390 * column and value strings by turning the '=' into a '\0'. 391 */ 392 *equals = '\0'; 393 col = line; 394 val = equals + 1; 395 } else { 396 val = line; 397 } 398 399 /* Do we already have a column by this name? If so, skip it. */ 400 if (col != NULL) { 401 for (int i = 0; i < data->cols_cnt; i++) { 402 if (strcmp(col, data->cols[i]) == 0) 403 return (0); /* Duplicate, skip */ 404 } 405 } 406 407 if (val != NULL) { 408 tmp = realloc(data->lines, 409 (data->lines_cnt + 1) * sizeof (*data->lines)); 410 if (tmp == NULL) 411 return (1); 412 413 data->lines = tmp; 414 data->lines[data->lines_cnt] = strdup(val); 415 data->lines_cnt++; 416 } 417 418 if (col != NULL) { 419 tmp = realloc(data->cols, 420 (data->cols_cnt + 1) * sizeof (*data->cols)); 421 if (tmp == NULL) 422 return (1); 423 424 data->cols = tmp; 425 data->cols[data->cols_cnt] = strdup(col); 426 data->cols_cnt++; 427 } 428 429 if (val != NULL && col == NULL) 430 return (1); 431 432 return (0); 433 } 434 435 /* 436 * Run the cmd and store results in *data. 437 */ 438 static void 439 vdev_run_cmd(vdev_cmd_data_t *data, char *cmd) 440 { 441 int rc; 442 char *argv[2] = {cmd, 0}; 443 char *env[5] = {"PATH=/bin:/sbin:/usr/bin:/usr/sbin", NULL, NULL, NULL, 444 NULL}; 445 char **lines = NULL; 446 int lines_cnt = 0; 447 int i; 448 449 /* Setup our custom environment variables */ 450 rc = asprintf(&env[1], "VDEV_PATH=%s", 451 data->path ? data->path : ""); 452 if (rc == -1) { 453 env[1] = NULL; 454 goto out; 455 } 456 457 rc = asprintf(&env[2], "VDEV_UPATH=%s", 458 data->upath ? data->upath : ""); 459 if (rc == -1) { 460 env[2] = NULL; 461 goto out; 462 } 463 464 rc = asprintf(&env[3], "VDEV_ENC_SYSFS_PATH=%s", 465 data->vdev_enc_sysfs_path ? 466 data->vdev_enc_sysfs_path : ""); 467 if (rc == -1) { 468 env[3] = NULL; 469 goto out; 470 } 471 472 /* Run the command */ 473 rc = libzfs_run_process_get_stdout_nopath(cmd, argv, env, &lines, 474 &lines_cnt); 475 if (rc != 0) 476 goto out; 477 478 /* Process the output we got */ 479 for (i = 0; i < lines_cnt; i++) 480 if (vdev_process_cmd_output(data, lines[i]) != 0) 481 break; 482 483 out: 484 if (lines != NULL) 485 libzfs_free_str_array(lines, lines_cnt); 486 487 /* Start with i = 1 since env[0] was statically allocated */ 488 for (i = 1; i < ARRAY_SIZE(env); i++) 489 free(env[i]); 490 } 491 492 /* 493 * Generate the search path for zpool iostat/status -c scripts. 494 * The string returned must be freed. 495 */ 496 char * 497 zpool_get_cmd_search_path(void) 498 { 499 const char *env; 500 char *sp = NULL; 501 502 env = getenv("ZPOOL_SCRIPTS_PATH"); 503 if (env != NULL) 504 return (strdup(env)); 505 506 env = getenv("HOME"); 507 if (env != NULL) { 508 if (asprintf(&sp, "%s/.zpool.d:%s", 509 env, ZPOOL_SCRIPTS_DIR) != -1) { 510 return (sp); 511 } 512 } 513 514 if (asprintf(&sp, "%s", ZPOOL_SCRIPTS_DIR) != -1) 515 return (sp); 516 517 return (NULL); 518 } 519 520 /* Thread function run for each vdev */ 521 static void 522 vdev_run_cmd_thread(void *cb_cmd_data) 523 { 524 vdev_cmd_data_t *data = cb_cmd_data; 525 char *cmd = NULL, *cmddup, *cmdrest; 526 527 cmddup = strdup(data->cmd); 528 if (cmddup == NULL) 529 return; 530 531 cmdrest = cmddup; 532 while ((cmd = strtok_r(cmdrest, ",", &cmdrest))) { 533 char *dir = NULL, *sp, *sprest; 534 char fullpath[MAXPATHLEN]; 535 536 if (strchr(cmd, '/') != NULL) 537 continue; 538 539 sp = zpool_get_cmd_search_path(); 540 if (sp == NULL) 541 continue; 542 543 sprest = sp; 544 while ((dir = strtok_r(sprest, ":", &sprest))) { 545 if (snprintf(fullpath, sizeof (fullpath), 546 "%s/%s", dir, cmd) == -1) 547 continue; 548 549 if (access(fullpath, X_OK) == 0) { 550 vdev_run_cmd(data, fullpath); 551 break; 552 } 553 } 554 free(sp); 555 } 556 free(cmddup); 557 } 558 559 /* For each vdev in the pool run a command */ 560 static int 561 for_each_vdev_run_cb(void *zhp_data, nvlist_t *nv, void *cb_vcdl) 562 { 563 vdev_cmd_data_list_t *vcdl = cb_vcdl; 564 vdev_cmd_data_t *data; 565 char *path = NULL; 566 char *vname = NULL; 567 char *vdev_enc_sysfs_path = NULL; 568 int i, match = 0; 569 zpool_handle_t *zhp = zhp_data; 570 571 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0) 572 return (1); 573 574 nvlist_lookup_string(nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH, 575 &vdev_enc_sysfs_path); 576 577 /* Spares show more than once if they're in use, so skip if exists */ 578 for (i = 0; i < vcdl->count; i++) { 579 if ((strcmp(vcdl->data[i].path, path) == 0) && 580 (strcmp(vcdl->data[i].pool, zpool_get_name(zhp)) == 0)) { 581 /* vdev already exists, skip it */ 582 return (0); 583 } 584 } 585 586 /* Check for selected vdevs here, if any */ 587 for (i = 0; i < vcdl->vdev_names_count; i++) { 588 vname = zpool_vdev_name(g_zfs, zhp, nv, vcdl->cb_name_flags); 589 if (strcmp(vcdl->vdev_names[i], vname) == 0) { 590 free(vname); 591 match = 1; 592 break; /* match */ 593 } 594 free(vname); 595 } 596 597 /* If we selected vdevs, and this isn't one of them, then bail out */ 598 if (!match && vcdl->vdev_names_count) 599 return (0); 600 601 /* 602 * Resize our array and add in the new element. 603 */ 604 if (!(vcdl->data = realloc(vcdl->data, 605 sizeof (*vcdl->data) * (vcdl->count + 1)))) 606 return (ENOMEM); /* couldn't realloc */ 607 608 data = &vcdl->data[vcdl->count]; 609 610 data->pool = strdup(zpool_get_name(zhp)); 611 data->path = strdup(path); 612 data->upath = zfs_get_underlying_path(path); 613 data->cmd = vcdl->cmd; 614 data->lines = data->cols = NULL; 615 data->lines_cnt = data->cols_cnt = 0; 616 if (vdev_enc_sysfs_path) 617 data->vdev_enc_sysfs_path = strdup(vdev_enc_sysfs_path); 618 else 619 data->vdev_enc_sysfs_path = NULL; 620 621 vcdl->count++; 622 623 return (0); 624 } 625 626 /* Get the names and count of the vdevs */ 627 static int 628 all_pools_for_each_vdev_gather_cb(zpool_handle_t *zhp, void *cb_vcdl) 629 { 630 return (for_each_vdev(zhp, for_each_vdev_run_cb, cb_vcdl)); 631 } 632 633 /* 634 * Now that vcdl is populated with our complete list of vdevs, spawn 635 * off the commands. 636 */ 637 static void 638 all_pools_for_each_vdev_run_vcdl(vdev_cmd_data_list_t *vcdl) 639 { 640 tpool_t *t; 641 642 t = tpool_create(1, 5 * sysconf(_SC_NPROCESSORS_ONLN), 0, NULL); 643 if (t == NULL) 644 return; 645 646 /* Spawn off the command for each vdev */ 647 for (int i = 0; i < vcdl->count; i++) { 648 (void) tpool_dispatch(t, vdev_run_cmd_thread, 649 (void *) &vcdl->data[i]); 650 } 651 652 /* Wait for threads to finish */ 653 tpool_wait(t); 654 tpool_destroy(t); 655 } 656 657 /* 658 * Run command 'cmd' on all vdevs in all pools in argv. Saves the first line of 659 * output from the command in vcdk->data[].line for all vdevs. If you want 660 * to run the command on only certain vdevs, fill in g_zfs, vdev_names, 661 * vdev_names_count, and cb_name_flags. Otherwise leave them as zero. 662 * 663 * Returns a vdev_cmd_data_list_t that must be freed with 664 * free_vdev_cmd_data_list(); 665 */ 666 vdev_cmd_data_list_t * 667 all_pools_for_each_vdev_run(int argc, char **argv, char *cmd, 668 libzfs_handle_t *g_zfs, char **vdev_names, int vdev_names_count, 669 int cb_name_flags) 670 { 671 vdev_cmd_data_list_t *vcdl; 672 vcdl = safe_malloc(sizeof (vdev_cmd_data_list_t)); 673 vcdl->cmd = cmd; 674 675 vcdl->vdev_names = vdev_names; 676 vcdl->vdev_names_count = vdev_names_count; 677 vcdl->cb_name_flags = cb_name_flags; 678 vcdl->g_zfs = g_zfs; 679 680 /* Gather our list of all vdevs in all pools */ 681 for_each_pool(argc, argv, B_TRUE, NULL, B_FALSE, 682 all_pools_for_each_vdev_gather_cb, vcdl); 683 684 /* Run command on all vdevs in all pools */ 685 all_pools_for_each_vdev_run_vcdl(vcdl); 686 687 /* 688 * vcdl->data[] now contains all the column names and values for each 689 * vdev. We need to process that into a master list of unique column 690 * names, and figure out the width of each column. 691 */ 692 process_unique_cmd_columns(vcdl); 693 694 return (vcdl); 695 } 696 697 /* 698 * Free the vdev_cmd_data_list_t created by all_pools_for_each_vdev_run() 699 */ 700 void 701 free_vdev_cmd_data_list(vdev_cmd_data_list_t *vcdl) 702 { 703 free(vcdl->uniq_cols); 704 free(vcdl->uniq_cols_width); 705 706 for (int i = 0; i < vcdl->count; i++) { 707 free(vcdl->data[i].path); 708 free(vcdl->data[i].pool); 709 free(vcdl->data[i].upath); 710 711 for (int j = 0; j < vcdl->data[i].lines_cnt; j++) 712 free(vcdl->data[i].lines[j]); 713 714 free(vcdl->data[i].lines); 715 716 for (int j = 0; j < vcdl->data[i].cols_cnt; j++) 717 free(vcdl->data[i].cols[j]); 718 719 free(vcdl->data[i].cols); 720 free(vcdl->data[i].vdev_enc_sysfs_path); 721 } 722 free(vcdl->data); 723 free(vcdl); 724 } 725