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