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.
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.
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]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
36 #include <thread_pool.h>
40 #include <sys/zfs_context.h>
43 #include "zpool_util.h"
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.
51 typedef struct zpool_node
{
52 zpool_handle_t
*zn_handle
;
53 uu_avl_node_t zn_avlnode
;
60 uu_avl_pool_t
*zl_pool
;
61 zprop_list_t
**zl_proplist
;
66 zpool_compare(const void *larg
, const void *rarg
, void *unused
)
68 zpool_handle_t
*l
= ((zpool_node_t
*)larg
)->zn_handle
;
69 zpool_handle_t
*r
= ((zpool_node_t
*)rarg
)->zn_handle
;
70 const char *lname
= zpool_get_name(l
);
71 const char *rname
= zpool_get_name(r
);
73 return (strcmp(lname
, rname
));
77 * Callback function for pool_list_get(). Adds the given pool to the AVL tree
81 add_pool(zpool_handle_t
*zhp
, void *data
)
83 zpool_list_t
*zlp
= data
;
84 zpool_node_t
*node
= safe_malloc(sizeof (zpool_node_t
));
87 node
->zn_handle
= zhp
;
88 uu_avl_node_init(node
, &node
->zn_avlnode
, zlp
->zl_pool
);
89 if (uu_avl_find(zlp
->zl_avl
, node
, NULL
, &idx
) == NULL
) {
90 if (zlp
->zl_proplist
&&
91 zpool_expand_proplist(zhp
, zlp
->zl_proplist
) != 0) {
96 uu_avl_insert(zlp
->zl_avl
, node
, idx
);
107 * Create a list of pools based on the given arguments. If we're given no
108 * arguments, then iterate over all pools in the system and add them to the AVL
109 * tree. Otherwise, add only those pool explicitly specified on the command
113 pool_list_get(int argc
, char **argv
, zprop_list_t
**proplist
, int *err
)
117 zlp
= safe_malloc(sizeof (zpool_list_t
));
119 zlp
->zl_pool
= uu_avl_pool_create("zfs_pool", sizeof (zpool_node_t
),
120 offsetof(zpool_node_t
, zn_avlnode
), zpool_compare
, UU_DEFAULT
);
122 if (zlp
->zl_pool
== NULL
)
125 if ((zlp
->zl_avl
= uu_avl_create(zlp
->zl_pool
, NULL
,
126 UU_DEFAULT
)) == NULL
)
129 zlp
->zl_proplist
= proplist
;
132 (void) zpool_iter(g_zfs
, add_pool
, zlp
);
133 zlp
->zl_findall
= B_TRUE
;
137 for (i
= 0; i
< argc
; i
++) {
140 if ((zhp
= zpool_open_canfail(g_zfs
, argv
[i
])) !=
142 if (add_pool(zhp
, zlp
) != 0)
154 * Search for any new pools, adding them to the list. We only add pools when no
155 * options were given on the command line. Otherwise, we keep the list fixed as
156 * those that were explicitly specified.
159 pool_list_update(zpool_list_t
*zlp
)
162 (void) zpool_iter(g_zfs
, add_pool
, zlp
);
166 * Iterate over all pools in the list, executing the callback for each
169 pool_list_iter(zpool_list_t
*zlp
, int unavail
, zpool_iter_f func
,
172 zpool_node_t
*node
, *next_node
;
175 for (node
= uu_avl_first(zlp
->zl_avl
); node
!= NULL
; node
= next_node
) {
176 next_node
= uu_avl_next(zlp
->zl_avl
, node
);
177 if (zpool_get_state(node
->zn_handle
) != POOL_STATE_UNAVAIL
||
179 ret
|= func(node
->zn_handle
, data
);
186 * Remove the given pool from the list. When running iostat, we want to remove
187 * those pools that no longer exist.
190 pool_list_remove(zpool_list_t
*zlp
, zpool_handle_t
*zhp
)
192 zpool_node_t search
, *node
;
194 search
.zn_handle
= zhp
;
195 if ((node
= uu_avl_find(zlp
->zl_avl
, &search
, NULL
, NULL
)) != NULL
) {
196 uu_avl_remove(zlp
->zl_avl
, node
);
197 zpool_close(node
->zn_handle
);
203 * Free all the handles associated with this list.
206 pool_list_free(zpool_list_t
*zlp
)
211 if ((walk
= uu_avl_walk_start(zlp
->zl_avl
, UU_WALK_ROBUST
)) == NULL
) {
212 (void) fprintf(stderr
,
213 gettext("internal error: out of memory"));
217 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
218 uu_avl_remove(zlp
->zl_avl
, node
);
219 zpool_close(node
->zn_handle
);
223 uu_avl_walk_end(walk
);
224 uu_avl_destroy(zlp
->zl_avl
);
225 uu_avl_pool_destroy(zlp
->zl_pool
);
231 * Returns the number of elements in the pool list.
234 pool_list_count(zpool_list_t
*zlp
)
236 return (uu_avl_numnodes(zlp
->zl_avl
));
240 * High level function which iterates over all pools given on the command line,
241 * using the pool_list_* interfaces.
244 for_each_pool(int argc
, char **argv
, boolean_t unavail
,
245 zprop_list_t
**proplist
, zpool_iter_f func
, void *data
)
250 if ((list
= pool_list_get(argc
, argv
, proplist
, &ret
)) == NULL
)
253 if (pool_list_iter(list
, unavail
, func
, data
) != 0)
256 pool_list_free(list
);
262 for_each_vdev_cb(zpool_handle_t
*zhp
, nvlist_t
*nv
, pool_vdev_iter_f func
,
271 const char *list
[] = {
273 ZPOOL_CONFIG_L2CACHE
,
274 ZPOOL_CONFIG_CHILDREN
277 for (i
= 0; i
< ARRAY_SIZE(list
); i
++) {
278 if (nvlist_lookup_nvlist_array(nv
, list
[i
], &child
,
280 for (c
= 0; c
< children
; c
++) {
283 (void) nvlist_lookup_uint64(child
[c
],
284 ZPOOL_CONFIG_IS_HOLE
, &ishole
);
289 ret
|= for_each_vdev_cb(zhp
, child
[c
], func
,
295 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
298 /* Don't run our function on root vdevs */
299 if (strcmp(type
, VDEV_TYPE_ROOT
) != 0) {
300 ret
|= func(zhp
, nv
, data
);
307 * This is the equivalent of for_each_pool() for vdevs. It iterates thorough
308 * all vdevs in the pool, ignoring root vdevs and holes, calling func() on
312 * @func: Function to call on each vdev
313 * @data: Custom data to pass to the function
316 for_each_vdev(zpool_handle_t
*zhp
, pool_vdev_iter_f func
, void *data
)
318 nvlist_t
*config
, *nvroot
= NULL
;
320 if ((config
= zpool_get_config(zhp
, NULL
)) != NULL
) {
321 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
324 return (for_each_vdev_cb(zhp
, nvroot
, func
, data
));
328 * Process the vcdl->vdev_cmd_data[] array to figure out all the unique column
329 * names and their widths. When this function is done, vcdl->uniq_cols,
330 * vcdl->uniq_cols_cnt, and vcdl->uniq_cols_width will be filled in.
333 process_unique_cmd_columns(vdev_cmd_data_list_t
*vcdl
)
335 char **uniq_cols
= NULL
, **tmp
= NULL
;
336 int *uniq_cols_width
;
337 vdev_cmd_data_t
*data
;
342 for (int i
= 0; i
< vcdl
->count
; i
++) {
343 data
= &vcdl
->data
[i
];
344 /* For each column the vdev reported */
345 for (int j
= 0; j
< data
->cols_cnt
; j
++) {
346 /* Is this column in our list of unique column names? */
347 for (k
= 0; k
< cnt
; k
++) {
348 if (strcmp(data
->cols
[j
], uniq_cols
[k
]) == 0)
349 break; /* yes it is */
352 /* No entry for column, add to list */
353 tmp
= realloc(uniq_cols
, sizeof (*uniq_cols
) *
356 break; /* Nothing we can do... */
358 uniq_cols
[cnt
] = data
->cols
[j
];
365 * We now have a list of all the unique column names. Figure out the
366 * max width of each column by looking at the column name and all its
369 uniq_cols_width
= safe_malloc(sizeof (*uniq_cols_width
) * cnt
);
370 for (int i
= 0; i
< cnt
; i
++) {
371 /* Start off with the column title's width */
372 uniq_cols_width
[i
] = strlen(uniq_cols
[i
]);
374 for (int j
= 0; j
< vcdl
->count
; j
++) {
375 /* For each of the vdev's values in a column */
376 data
= &vcdl
->data
[j
];
377 for (k
= 0; k
< data
->cols_cnt
; k
++) {
378 /* Does this vdev have a value for this col? */
379 if (strcmp(data
->cols
[k
], uniq_cols
[i
]) == 0) {
380 /* Is the value width larger? */
382 MAX(uniq_cols_width
[i
],
383 strlen(data
->lines
[k
]));
389 vcdl
->uniq_cols
= uniq_cols
;
390 vcdl
->uniq_cols_cnt
= cnt
;
391 vcdl
->uniq_cols_width
= uniq_cols_width
;
396 * Process a line of command output
398 * When running 'zpool iostat|status -c' the lines of output can either be
407 * Process the column_name (if any) and value.
409 * Returns 0 if line was processed, and there are more lines can still be
412 * Returns 1 if this was the last line to process, or error.
415 vdev_process_cmd_output(vdev_cmd_data_t
*data
, char *line
)
425 equals
= strchr(line
, '=');
426 if (equals
!= NULL
) {
428 * We have a 'column=value' type line. Split it into the
429 * column and value strings by turning the '=' into a '\0'.
438 /* Do we already have a column by this name? If so, skip it. */
440 for (int i
= 0; i
< data
->cols_cnt
; i
++) {
441 if (strcmp(col
, data
->cols
[i
]) == 0)
442 return (0); /* Duplicate, skip */
447 tmp
= realloc(data
->lines
,
448 (data
->lines_cnt
+ 1) * sizeof (*data
->lines
));
453 data
->lines
[data
->lines_cnt
] = strdup(val
);
458 tmp
= realloc(data
->cols
,
459 (data
->cols_cnt
+ 1) * sizeof (*data
->cols
));
464 data
->cols
[data
->cols_cnt
] = strdup(col
);
468 if (val
!= NULL
&& col
== NULL
)
475 * Run the cmd and store results in *data.
478 vdev_run_cmd(vdev_cmd_data_t
*data
, char *cmd
)
481 char *argv
[2] = {cmd
, 0};
482 char *env
[5] = {"PATH=/bin:/sbin:/usr/bin:/usr/sbin", NULL
, NULL
, NULL
,
488 /* Setup our custom environment variables */
489 rc
= asprintf(&env
[1], "VDEV_PATH=%s",
490 data
->path
? data
->path
: "");
494 rc
= asprintf(&env
[2], "VDEV_UPATH=%s",
495 data
->upath
? data
->upath
: "");
499 rc
= asprintf(&env
[3], "VDEV_ENC_SYSFS_PATH=%s",
500 data
->vdev_enc_sysfs_path
?
501 data
->vdev_enc_sysfs_path
: "");
505 /* Run the command */
506 rc
= libzfs_run_process_get_stdout_nopath(cmd
, argv
, env
, &lines
,
511 /* Process the output we got */
512 for (i
= 0; i
< lines_cnt
; i
++)
513 if (vdev_process_cmd_output(data
, lines
[i
]) != 0)
518 libzfs_free_str_array(lines
, lines_cnt
);
520 /* Start with i = 1 since env[0] was statically allocated */
521 for (i
= 1; i
< ARRAY_SIZE(env
); i
++)
527 * Generate the search path for zpool iostat/status -c scripts.
528 * The string returned must be freed.
531 zpool_get_cmd_search_path(void)
536 env
= getenv("ZPOOL_SCRIPTS_PATH");
538 return (strdup(env
));
540 env
= getenv("HOME");
542 if (asprintf(&sp
, "%s/.zpool.d:%s",
543 env
, ZPOOL_SCRIPTS_DIR
) != -1) {
548 if (asprintf(&sp
, "%s", ZPOOL_SCRIPTS_DIR
) != -1)
554 /* Thread function run for each vdev */
556 vdev_run_cmd_thread(void *cb_cmd_data
)
558 vdev_cmd_data_t
*data
= cb_cmd_data
;
559 char *cmd
= NULL
, *cmddup
, *cmdrest
;
561 cmddup
= strdup(data
->cmd
);
566 while ((cmd
= strtok_r(cmdrest
, ",", &cmdrest
))) {
567 char *dir
= NULL
, *sp
, *sprest
;
568 char fullpath
[MAXPATHLEN
];
570 if (strchr(cmd
, '/') != NULL
)
573 sp
= zpool_get_cmd_search_path();
578 while ((dir
= strtok_r(sprest
, ":", &sprest
))) {
579 if (snprintf(fullpath
, sizeof (fullpath
),
580 "%s/%s", dir
, cmd
) == -1)
583 if (access(fullpath
, X_OK
) == 0) {
584 vdev_run_cmd(data
, fullpath
);
593 /* For each vdev in the pool run a command */
595 for_each_vdev_run_cb(zpool_handle_t
*zhp
, nvlist_t
*nv
, void *cb_vcdl
)
597 vdev_cmd_data_list_t
*vcdl
= cb_vcdl
;
598 vdev_cmd_data_t
*data
;
601 char *vdev_enc_sysfs_path
= NULL
;
604 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) != 0)
607 nvlist_lookup_string(nv
, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH
,
608 &vdev_enc_sysfs_path
);
610 /* Spares show more than once if they're in use, so skip if exists */
611 for (i
= 0; i
< vcdl
->count
; i
++) {
612 if ((strcmp(vcdl
->data
[i
].path
, path
) == 0) &&
613 (strcmp(vcdl
->data
[i
].pool
, zpool_get_name(zhp
)) == 0)) {
614 /* vdev already exists, skip it */
619 /* Check for whitelisted vdevs here, if any */
620 for (i
= 0; i
< vcdl
->vdev_names_count
; i
++) {
621 vname
= zpool_vdev_name(g_zfs
, zhp
, nv
, vcdl
->cb_name_flags
);
622 if (strcmp(vcdl
->vdev_names
[i
], vname
) == 0) {
630 /* If we whitelisted vdevs, and this isn't one of them, then bail out */
631 if (!match
&& vcdl
->vdev_names_count
)
635 * Resize our array and add in the new element.
637 if (!(vcdl
->data
= realloc(vcdl
->data
,
638 sizeof (*vcdl
->data
) * (vcdl
->count
+ 1))))
639 return (ENOMEM
); /* couldn't realloc */
641 data
= &vcdl
->data
[vcdl
->count
];
643 data
->pool
= strdup(zpool_get_name(zhp
));
644 data
->path
= strdup(path
);
645 data
->upath
= zfs_get_underlying_path(path
);
646 data
->cmd
= vcdl
->cmd
;
647 data
->lines
= data
->cols
= NULL
;
648 data
->lines_cnt
= data
->cols_cnt
= 0;
649 if (vdev_enc_sysfs_path
)
650 data
->vdev_enc_sysfs_path
= strdup(vdev_enc_sysfs_path
);
652 data
->vdev_enc_sysfs_path
= NULL
;
659 /* Get the names and count of the vdevs */
661 all_pools_for_each_vdev_gather_cb(zpool_handle_t
*zhp
, void *cb_vcdl
)
663 return (for_each_vdev(zhp
, for_each_vdev_run_cb
, cb_vcdl
));
667 * Now that vcdl is populated with our complete list of vdevs, spawn
671 all_pools_for_each_vdev_run_vcdl(vdev_cmd_data_list_t
*vcdl
)
675 t
= tpool_create(1, 5 * sysconf(_SC_NPROCESSORS_ONLN
), 0, NULL
);
679 /* Spawn off the command for each vdev */
680 for (int i
= 0; i
< vcdl
->count
; i
++) {
681 (void) tpool_dispatch(t
, vdev_run_cmd_thread
,
682 (void *) &vcdl
->data
[i
]);
685 /* Wait for threads to finish */
691 * Run command 'cmd' on all vdevs in all pools in argv. Saves the first line of
692 * output from the command in vcdk->data[].line for all vdevs. If you want
693 * to run the command on only certain vdevs, fill in g_zfs, vdev_names,
694 * vdev_names_count, and cb_name_flags. Otherwise leave them as zero.
696 * Returns a vdev_cmd_data_list_t that must be freed with
697 * free_vdev_cmd_data_list();
699 vdev_cmd_data_list_t
*
700 all_pools_for_each_vdev_run(int argc
, char **argv
, char *cmd
,
701 libzfs_handle_t
*g_zfs
, char **vdev_names
, int vdev_names_count
,
704 vdev_cmd_data_list_t
*vcdl
;
705 vcdl
= safe_malloc(sizeof (vdev_cmd_data_list_t
));
708 vcdl
->vdev_names
= vdev_names
;
709 vcdl
->vdev_names_count
= vdev_names_count
;
710 vcdl
->cb_name_flags
= cb_name_flags
;
713 /* Gather our list of all vdevs in all pools */
714 for_each_pool(argc
, argv
, B_TRUE
, NULL
,
715 all_pools_for_each_vdev_gather_cb
, vcdl
);
717 /* Run command on all vdevs in all pools */
718 all_pools_for_each_vdev_run_vcdl(vcdl
);
721 * vcdl->data[] now contains all the column names and values for each
722 * vdev. We need to process that into a master list of unique column
723 * names, and figure out the width of each column.
725 process_unique_cmd_columns(vcdl
);
731 * Free the vdev_cmd_data_list_t created by all_pools_for_each_vdev_run()
734 free_vdev_cmd_data_list(vdev_cmd_data_list_t
*vcdl
)
736 free(vcdl
->uniq_cols
);
737 free(vcdl
->uniq_cols_width
);
739 for (int i
= 0; i
< vcdl
->count
; i
++) {
740 free(vcdl
->data
[i
].path
);
741 free(vcdl
->data
[i
].pool
);
742 free(vcdl
->data
[i
].upath
);
744 for (int j
= 0; j
< vcdl
->data
[i
].lines_cnt
; j
++)
745 free(vcdl
->data
[i
].lines
[j
]);
747 free(vcdl
->data
[i
].lines
);
749 for (int j
= 0; j
< vcdl
->data
[i
].cols_cnt
; j
++)
750 free(vcdl
->data
[i
].cols
[j
]);
752 free(vcdl
->data
[i
].cols
);
753 free(vcdl
->data
[i
].vdev_enc_sysfs_path
);