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