xref: /freebsd/sys/contrib/openzfs/cmd/zpool/zpool_iter.c (revision df58e8b1506f241670be86a560fb6e8432043aee)
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;
383 	char *val;
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 		col = NULL;
401 		val = line;
402 	}
403 
404 	/* Do we already have a column by this name?  If so, skip it. */
405 	if (col != NULL) {
406 		for (int i = 0; i < data->cols_cnt; i++) {
407 			if (strcmp(col, data->cols[i]) == 0)
408 				return (0); /* Duplicate, skip */
409 		}
410 	}
411 
412 	if (val != NULL) {
413 		tmp = realloc(data->lines,
414 		    (data->lines_cnt + 1) * sizeof (*data->lines));
415 		if (tmp == NULL)
416 			return (1);
417 
418 		data->lines = tmp;
419 		data->lines[data->lines_cnt] = strdup(val);
420 		data->lines_cnt++;
421 	}
422 
423 	if (col != NULL) {
424 		tmp = realloc(data->cols,
425 		    (data->cols_cnt + 1) * sizeof (*data->cols));
426 		if (tmp == NULL)
427 			return (1);
428 
429 		data->cols = tmp;
430 		data->cols[data->cols_cnt] = strdup(col);
431 		data->cols_cnt++;
432 	}
433 
434 	if (val != NULL && col == NULL)
435 		return (1);
436 
437 	return (0);
438 }
439 
440 /*
441  * Run the cmd and store results in *data.
442  */
443 static void
vdev_run_cmd(vdev_cmd_data_t * data,char * cmd)444 vdev_run_cmd(vdev_cmd_data_t *data, char *cmd)
445 {
446 	int rc;
447 	char *argv[2] = {cmd};
448 	char **env;
449 	char **lines = NULL;
450 	int lines_cnt = 0;
451 	int i;
452 
453 	env = zpool_vdev_script_alloc_env(data->pool, data->path, data->upath,
454 	    data->vdev_enc_sysfs_path, NULL, NULL);
455 	if (env == NULL)
456 		goto out;
457 
458 	/* Run the command */
459 	rc = libzfs_run_process_get_stdout_nopath(cmd, argv, env, &lines,
460 	    &lines_cnt);
461 
462 	zpool_vdev_script_free_env(env);
463 
464 	if (rc != 0)
465 		goto out;
466 
467 	/* Process the output we got */
468 	for (i = 0; i < lines_cnt; i++)
469 		if (vdev_process_cmd_output(data, lines[i]) != 0)
470 			break;
471 
472 out:
473 	if (lines != NULL)
474 		libzfs_free_str_array(lines, lines_cnt);
475 }
476 
477 /*
478  * Generate the search path for zpool iostat/status -c scripts.
479  * The string returned must be freed.
480  */
481 char *
zpool_get_cmd_search_path(void)482 zpool_get_cmd_search_path(void)
483 {
484 	const char *env;
485 	char *sp = NULL;
486 
487 	env = getenv("ZPOOL_SCRIPTS_PATH");
488 	if (env != NULL)
489 		return (strdup(env));
490 
491 	env = getenv("HOME");
492 	if (env != NULL) {
493 		if (asprintf(&sp, "%s/.zpool.d:%s",
494 		    env, ZPOOL_SCRIPTS_DIR) != -1) {
495 			return (sp);
496 		}
497 	}
498 
499 	if (asprintf(&sp, "%s", ZPOOL_SCRIPTS_DIR) != -1)
500 		return (sp);
501 
502 	return (NULL);
503 }
504 
505 /* Thread function run for each vdev */
506 static void
vdev_run_cmd_thread(void * cb_cmd_data)507 vdev_run_cmd_thread(void *cb_cmd_data)
508 {
509 	vdev_cmd_data_t *data = cb_cmd_data;
510 	char *cmd = NULL, *cmddup, *cmdrest;
511 
512 	cmddup = strdup(data->cmd);
513 	if (cmddup == NULL)
514 		return;
515 
516 	cmdrest = cmddup;
517 	while ((cmd = strtok_r(cmdrest, ",", &cmdrest))) {
518 		char *dir = NULL, *sp, *sprest;
519 		char fullpath[MAXPATHLEN];
520 
521 		if (strchr(cmd, '/') != NULL)
522 			continue;
523 
524 		sp = zpool_get_cmd_search_path();
525 		if (sp == NULL)
526 			continue;
527 
528 		sprest = sp;
529 		while ((dir = strtok_r(sprest, ":", &sprest))) {
530 			if (snprintf(fullpath, sizeof (fullpath),
531 			    "%s/%s", dir, cmd) == -1)
532 				continue;
533 
534 			if (access(fullpath, X_OK) == 0) {
535 				vdev_run_cmd(data, fullpath);
536 				break;
537 			}
538 		}
539 		free(sp);
540 	}
541 	free(cmddup);
542 }
543 
544 /* For each vdev in the pool run a command */
545 static int
for_each_vdev_run_cb(void * zhp_data,nvlist_t * nv,void * cb_vcdl)546 for_each_vdev_run_cb(void *zhp_data, nvlist_t *nv, void *cb_vcdl)
547 {
548 	vdev_cmd_data_list_t *vcdl = cb_vcdl;
549 	vdev_cmd_data_t *data;
550 	const char *path = NULL;
551 	char *vname = NULL;
552 	const char *vdev_enc_sysfs_path = NULL;
553 	int i, match = 0;
554 	zpool_handle_t *zhp = zhp_data;
555 
556 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
557 		return (1);
558 
559 	/* Make sure we're getting the updated enclosure sysfs path */
560 	update_vdev_config_dev_sysfs_path(nv, path,
561 	    ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH);
562 
563 	nvlist_lookup_string(nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH,
564 	    &vdev_enc_sysfs_path);
565 
566 	/* Spares show more than once if they're in use, so skip if exists */
567 	for (i = 0; i < vcdl->count; i++) {
568 		if ((strcmp(vcdl->data[i].path, path) == 0) &&
569 		    (strcmp(vcdl->data[i].pool, zpool_get_name(zhp)) == 0)) {
570 			/* vdev already exists, skip it */
571 			return (0);
572 		}
573 	}
574 
575 	/* Check for selected vdevs here, if any */
576 	for (i = 0; i < vcdl->vdev_names_count; i++) {
577 		vname = zpool_vdev_name(g_zfs, zhp, nv, vcdl->cb_name_flags);
578 		if (strcmp(vcdl->vdev_names[i], vname) == 0) {
579 			free(vname);
580 			match = 1;
581 			break; /* match */
582 		}
583 		free(vname);
584 	}
585 
586 	/* If we selected vdevs, and this isn't one of them, then bail out */
587 	if (!match && vcdl->vdev_names_count)
588 		return (0);
589 
590 	/*
591 	 * Resize our array and add in the new element.
592 	 */
593 	if (!(vcdl->data = realloc(vcdl->data,
594 	    sizeof (*vcdl->data) * (vcdl->count + 1))))
595 		return (ENOMEM);	/* couldn't realloc */
596 
597 	data = &vcdl->data[vcdl->count];
598 
599 	data->pool = strdup(zpool_get_name(zhp));
600 	data->path = strdup(path);
601 	data->upath = zfs_get_underlying_path(path);
602 	data->cmd = vcdl->cmd;
603 	data->lines = data->cols = NULL;
604 	data->lines_cnt = data->cols_cnt = 0;
605 	if (vdev_enc_sysfs_path)
606 		data->vdev_enc_sysfs_path = strdup(vdev_enc_sysfs_path);
607 	else
608 		data->vdev_enc_sysfs_path = NULL;
609 
610 	vcdl->count++;
611 
612 	return (0);
613 }
614 
615 /* Get the names and count of the vdevs */
616 static int
all_pools_for_each_vdev_gather_cb(zpool_handle_t * zhp,void * cb_vcdl)617 all_pools_for_each_vdev_gather_cb(zpool_handle_t *zhp, void *cb_vcdl)
618 {
619 	return (for_each_vdev(zhp, for_each_vdev_run_cb, cb_vcdl));
620 }
621 
622 /*
623  * Now that vcdl is populated with our complete list of vdevs, spawn
624  * off the commands.
625  */
626 static void
all_pools_for_each_vdev_run_vcdl(vdev_cmd_data_list_t * vcdl)627 all_pools_for_each_vdev_run_vcdl(vdev_cmd_data_list_t *vcdl)
628 {
629 	tpool_t *t;
630 
631 	t = tpool_create(1, 5 * sysconf(_SC_NPROCESSORS_ONLN), 0, NULL);
632 	if (t == NULL)
633 		return;
634 
635 	/* Spawn off the command for each vdev */
636 	for (int i = 0; i < vcdl->count; i++) {
637 		(void) tpool_dispatch(t, vdev_run_cmd_thread,
638 		    (void *) &vcdl->data[i]);
639 	}
640 
641 	/* Wait for threads to finish */
642 	tpool_wait(t);
643 	tpool_destroy(t);
644 }
645 
646 /*
647  * Run command 'cmd' on all vdevs in all pools in argv.  Saves the first line of
648  * output from the command in vcdk->data[].line for all vdevs.  If you want
649  * to run the command on only certain vdevs, fill in g_zfs, vdev_names,
650  * vdev_names_count, and cb_name_flags.  Otherwise leave them as zero.
651  *
652  * Returns a vdev_cmd_data_list_t that must be freed with
653  * free_vdev_cmd_data_list();
654  */
655 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)656 all_pools_for_each_vdev_run(int argc, char **argv, char *cmd,
657     libzfs_handle_t *g_zfs, char **vdev_names, int vdev_names_count,
658     int cb_name_flags)
659 {
660 	vdev_cmd_data_list_t *vcdl;
661 	vcdl = safe_malloc(sizeof (vdev_cmd_data_list_t));
662 	vcdl->cmd = cmd;
663 
664 	vcdl->vdev_names = vdev_names;
665 	vcdl->vdev_names_count = vdev_names_count;
666 	vcdl->cb_name_flags = cb_name_flags;
667 	vcdl->g_zfs = g_zfs;
668 
669 	/* Gather our list of all vdevs in all pools */
670 	for_each_pool(argc, argv, B_TRUE, NULL, ZFS_TYPE_POOL,
671 	    B_FALSE, all_pools_for_each_vdev_gather_cb, vcdl);
672 
673 	/* Run command on all vdevs in all pools */
674 	all_pools_for_each_vdev_run_vcdl(vcdl);
675 
676 	/*
677 	 * vcdl->data[] now contains all the column names and values for each
678 	 * vdev.  We need to process that into a master list of unique column
679 	 * names, and figure out the width of each column.
680 	 */
681 	process_unique_cmd_columns(vcdl);
682 
683 	return (vcdl);
684 }
685 
686 /*
687  * Free the vdev_cmd_data_list_t created by all_pools_for_each_vdev_run()
688  */
689 void
free_vdev_cmd_data_list(vdev_cmd_data_list_t * vcdl)690 free_vdev_cmd_data_list(vdev_cmd_data_list_t *vcdl)
691 {
692 	free(vcdl->uniq_cols);
693 	free(vcdl->uniq_cols_width);
694 
695 	for (int i = 0; i < vcdl->count; i++) {
696 		free(vcdl->data[i].path);
697 		free(vcdl->data[i].pool);
698 		free(vcdl->data[i].upath);
699 
700 		for (int j = 0; j < vcdl->data[i].lines_cnt; j++)
701 			free(vcdl->data[i].lines[j]);
702 
703 		free(vcdl->data[i].lines);
704 
705 		for (int j = 0; j < vcdl->data[i].cols_cnt; j++)
706 			free(vcdl->data[i].cols[j]);
707 
708 		free(vcdl->data[i].cols);
709 		free(vcdl->data[i].vdev_enc_sysfs_path);
710 	}
711 	free(vcdl->data);
712 	free(vcdl);
713 }
714