1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
599653d4eschrock * Common Development and Distribution License (the "License").
699653d4eschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
21fa9e406ahrens/*
22b1b8ab3lling * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23fa9e406ahrens * Use is subject to license terms.
24fa9e406ahrens */
25b327cd3Igor Kozhukhov/*
26b327cd3Igor Kozhukhov * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
27b327cd3Igor Kozhukhov */
28fa9e406ahrens
29fa9e406ahrens#include <libintl.h>
30fa9e406ahrens#include <libuutil.h>
31fa9e406ahrens#include <stddef.h>
32fa9e406ahrens#include <stdio.h>
33fa9e406ahrens#include <stdlib.h>
34fa9e406ahrens#include <strings.h>
35dd50e0cTony Hutter#include <sys/sysmacros.h>
36fa9e406ahrens
37fa9e406ahrens#include <libzfs.h>
38d8ab6e1Don Brady#include <libzutil.h>
39fa9e406ahrens
40fa9e406ahrens#include "zpool_util.h"
41fa9e406ahrens
42fa9e406ahrens/*
43fa9e406ahrens * Private interface for iterating over pools specified on the command line.
44fa9e406ahrens * Most consumers will call for_each_pool, but in order to support iostat, we
45fa9e406ahrens * allow fined grained control through the zpool_list_t interface.
46fa9e406ahrens */
47fa9e406ahrens
48fa9e406ahrenstypedef struct zpool_node {
49fa9e406ahrens	zpool_handle_t	*zn_handle;
50fa9e406ahrens	uu_avl_node_t	zn_avlnode;
51fa9e406ahrens	int		zn_mark;
52fa9e406ahrens} zpool_node_t;
53fa9e406ahrens
54fa9e406ahrensstruct zpool_list {
5599653d4eschrock	boolean_t	zl_findall;
56fa9e406ahrens	uu_avl_t	*zl_avl;
57fa9e406ahrens	uu_avl_pool_t	*zl_pool;
58990b485lling	zprop_list_t	**zl_proplist;
59fa9e406ahrens};
60fa9e406ahrens
61fa9e406ahrens/* ARGSUSED */
62fa9e406ahrensstatic int
63fa9e406ahrenszpool_compare(const void *larg, const void *rarg, void *unused)
64fa9e406ahrens{
65fa9e406ahrens	zpool_handle_t *l = ((zpool_node_t *)larg)->zn_handle;
66fa9e406ahrens	zpool_handle_t *r = ((zpool_node_t *)rarg)->zn_handle;
67fa9e406ahrens	const char *lname = zpool_get_name(l);
68fa9e406ahrens	const char *rname = zpool_get_name(r);
69fa9e406ahrens
70fa9e406ahrens	return (strcmp(lname, rname));
71fa9e406ahrens}
72fa9e406ahrens
73fa9e406ahrens/*
74fa9e406ahrens * Callback function for pool_list_get().  Adds the given pool to the AVL tree
75fa9e406ahrens * of known pools.
76fa9e406ahrens */
77fa9e406ahrensstatic int
78fa9e406ahrensadd_pool(zpool_handle_t *zhp, void *data)
79fa9e406ahrens{
80fa9e406ahrens	zpool_list_t *zlp = data;
81fa9e406ahrens	zpool_node_t *node = safe_malloc(sizeof (zpool_node_t));
82fa9e406ahrens	uu_avl_index_t idx;
83fa9e406ahrens
84fa9e406ahrens	node->zn_handle = zhp;
85fa9e406ahrens	uu_avl_node_init(node, &node->zn_avlnode, zlp->zl_pool);
86fa9e406ahrens	if (uu_avl_find(zlp->zl_avl, node, NULL, &idx) == NULL) {
87990b485lling		if (zlp->zl_proplist &&
88990b485lling		    zpool_expand_proplist(zhp, zlp->zl_proplist) != 0) {
89990b485lling			zpool_close(zhp);
90990b485lling			free(node);
91990b485lling			return (-1);
92990b485lling		}
93fa9e406ahrens		uu_avl_insert(zlp->zl_avl, node, idx);
94fa9e406ahrens	} else {
95fa9e406ahrens		zpool_close(zhp);
96fa9e406ahrens		free(node);
97b1b8ab3lling		return (-1);
98fa9e406ahrens	}
99fa9e406ahrens
100fa9e406ahrens	return (0);
101fa9e406ahrens}
102fa9e406ahrens
103fa9e406ahrens/*
104fa9e406ahrens * Create a list of pools based on the given arguments.  If we're given no
105fa9e406ahrens * arguments, then iterate over all pools in the system and add them to the AVL
106fa9e406ahrens * tree.  Otherwise, add only those pool explicitly specified on the command
107fa9e406ahrens * line.
108fa9e406ahrens */
109fa9e406ahrenszpool_list_t *
110990b485llingpool_list_get(int argc, char **argv, zprop_list_t **proplist, int *err)
111fa9e406ahrens{
112fa9e406ahrens	zpool_list_t *zlp;
113fa9e406ahrens
114fa9e406ahrens	zlp = safe_malloc(sizeof (zpool_list_t));
115fa9e406ahrens
116fa9e406ahrens	zlp->zl_pool = uu_avl_pool_create("zfs_pool", sizeof (zpool_node_t),
117fa9e406ahrens	    offsetof(zpool_node_t, zn_avlnode), zpool_compare, UU_DEFAULT);
118fa9e406ahrens
119fa9e406ahrens	if (zlp->zl_pool == NULL)
1205ad8204nd		zpool_no_memory();
121fa9e406ahrens
122fa9e406ahrens	if ((zlp->zl_avl = uu_avl_create(zlp->zl_pool, NULL,
123fa9e406ahrens	    UU_DEFAULT)) == NULL)
1245ad8204nd		zpool_no_memory();
125fa9e406ahrens
126990b485lling	zlp->zl_proplist = proplist;
127990b485lling
128fa9e406ahrens	if (argc == 0) {
12999653d4eschrock		(void) zpool_iter(g_zfs, add_pool, zlp);
13099653d4eschrock		zlp->zl_findall = B_TRUE;
131fa9e406ahrens	} else {
132fa9e406ahrens		int i;
133fa9e406ahrens
134fa9e406ahrens		for (i = 0; i < argc; i++) {
135fa9e406ahrens			zpool_handle_t *zhp;
136fa9e406ahrens
137b327cd3Igor Kozhukhov			if ((zhp = zpool_open_canfail(g_zfs, argv[i])) !=
138b327cd3Igor Kozhukhov			    NULL) {
139990b485lling				if (add_pool(zhp, zlp) != 0)
140b1b8ab3lling					*err = B_TRUE;
141990b485lling			} else {
14299653d4eschrock				*err = B_TRUE;
143990b485lling			}
144fa9e406ahrens		}
145fa9e406ahrens	}
146fa9e406ahrens
147fa9e406ahrens	return (zlp);
148fa9e406ahrens}
149fa9e406ahrens
150fa9e406ahrens/*
151fa9e406ahrens * Search for any new pools, adding them to the list.  We only add pools when no
152fa9e406ahrens * options were given on the command line.  Otherwise, we keep the list fixed as
153fa9e406ahrens * those that were explicitly specified.
154fa9e406ahrens */
155fa9e406ahrensvoid
156fa9e406ahrenspool_list_update(zpool_list_t *zlp)
157fa9e406ahrens{
158fa9e406ahrens	if (zlp->zl_findall)
15999653d4eschrock		(void) zpool_iter(g_zfs, add_pool, zlp);
160fa9e406ahrens}
161fa9e406ahrens
162fa9e406ahrens/*
163fa9e406ahrens * Iterate over all pools in the list, executing the callback for each
164fa9e406ahrens */
165fa9e406ahrensint
166fa9e406ahrenspool_list_iter(zpool_list_t *zlp, int unavail, zpool_iter_f func,
167fa9e406ahrens    void *data)
168fa9e406ahrens{
169fa9e406ahrens	zpool_node_t *node, *next_node;
170fa9e406ahrens	int ret = 0;
171fa9e406ahrens
172fa9e406ahrens	for (node = uu_avl_first(zlp->zl_avl); node != NULL; node = next_node) {
173fa9e406ahrens		next_node = uu_avl_next(zlp->zl_avl, node);
174fa9e406ahrens		if (zpool_get_state(node->zn_handle) != POOL_STATE_UNAVAIL ||
175fa9e406ahrens		    unavail)
176fa9e406ahrens			ret |= func(node->zn_handle, data);
177fa9e406ahrens	}
178fa9e406ahrens
179fa9e406ahrens	return (ret);
180fa9e406ahrens}
181fa9e406ahrens
182fa9e406ahrens/*
183fa9e406ahrens * Remove the given pool from the list.  When running iostat, we want to remove
184fa9e406ahrens * those pools that no longer exist.
185fa9e406ahrens */
186fa9e406ahrensvoid
187fa9e406ahrenspool_list_remove(zpool_list_t *zlp, zpool_handle_t *zhp)
188fa9e406ahrens{
189fa9e406ahrens	zpool_node_t search, *node;
190fa9e406ahrens
191fa9e406ahrens	search.zn_handle = zhp;
192fa9e406ahrens	if ((node = uu_avl_find(zlp->zl_avl, &search, NULL, NULL)) != NULL) {
193fa9e406ahrens		uu_avl_remove(zlp->zl_avl, node);
194fa9e406ahrens		zpool_close(node->zn_handle);
195fa9e406ahrens		free(node);
196fa9e406ahrens	}
197fa9e406ahrens}
198fa9e406ahrens
199fa9e406ahrens/*
200fa9e406ahrens * Free all the handles associated with this list.
201fa9e406ahrens */
202fa9e406ahrensvoid
203fa9e406ahrenspool_list_free(zpool_list_t *zlp)
204fa9e406ahrens{
205fa9e406ahrens	uu_avl_walk_t *walk;
206fa9e406ahrens	zpool_node_t *node;
207fa9e406ahrens
208fa9e406ahrens	if ((walk = uu_avl_walk_start(zlp->zl_avl, UU_WALK_ROBUST)) == NULL) {
209fa9e406ahrens		(void) fprintf(stderr,
210fa9e406ahrens		    gettext("internal error: out of memory"));
211fa9e406ahrens		exit(1);
212fa9e406ahrens	}
213fa9e406ahrens
214fa9e406ahrens	while ((node = uu_avl_walk_next(walk)) != NULL) {
215fa9e406ahrens		uu_avl_remove(zlp->zl_avl, node);
216fa9e406ahrens		zpool_close(node->zn_handle);
217fa9e406ahrens		free(node);
218fa9e406ahrens	}
219fa9e406ahrens
220fa9e406ahrens	uu_avl_walk_end(walk);
221fa9e406ahrens	uu_avl_destroy(zlp->zl_avl);
222fa9e406ahrens	uu_avl_pool_destroy(zlp->zl_pool);
223fa9e406ahrens
224fa9e406ahrens	free(zlp);
225fa9e406ahrens}
226fa9e406ahrens
227fa9e406ahrens/*
228fa9e406ahrens * Returns the number of elements in the pool list.
229fa9e406ahrens */
230fa9e406ahrensint
231fa9e406ahrenspool_list_count(zpool_list_t *zlp)
232fa9e406ahrens{
233fa9e406ahrens	return (uu_avl_numnodes(zlp->zl_avl));
234fa9e406ahrens}
235fa9e406ahrens
236fa9e406ahrens/*
237fa9e406ahrens * High level function which iterates over all pools given on the command line,
238fa9e406ahrens * using the pool_list_* interfaces.
239fa9e406ahrens */
240fa9e406ahrensint
241b1b8ab3llingfor_each_pool(int argc, char **argv, boolean_t unavail,
242990b485lling    zprop_list_t **proplist, zpool_iter_f func, void *data)
243fa9e406ahrens{
244fa9e406ahrens	zpool_list_t *list;
245fa9e406ahrens	int ret = 0;
246fa9e406ahrens
247b1b8ab3lling	if ((list = pool_list_get(argc, argv, proplist, &ret)) == NULL)
248fa9e406ahrens		return (1);
249fa9e406ahrens
250fa9e406ahrens	if (pool_list_iter(list, unavail, func, data) != 0)
251fa9e406ahrens		ret = 1;
252fa9e406ahrens
253fa9e406ahrens	pool_list_free(list);
254fa9e406ahrens
255fa9e406ahrens	return (ret);
256fa9e406ahrens}
257dd50e0cTony Hutter
258dd50e0cTony Hutterstatic int
259dd50e0cTony Hutterfor_each_vdev_cb(zpool_handle_t *zhp, nvlist_t *nv, pool_vdev_iter_f func,
260dd50e0cTony Hutter    void *data)
261dd50e0cTony Hutter{
262dd50e0cTony Hutter	nvlist_t **child;
263dd50e0cTony Hutter	uint_t c, children;
264dd50e0cTony Hutter	int ret = 0;
265dd50e0cTony Hutter	int i;
266dd50e0cTony Hutter	char *type;
267dd50e0cTony Hutter
268dd50e0cTony Hutter	const char *list[] = {
269dd50e0cTony Hutter	    ZPOOL_CONFIG_SPARES,
270dd50e0cTony Hutter	    ZPOOL_CONFIG_L2CACHE,
271dd50e0cTony Hutter	    ZPOOL_CONFIG_CHILDREN
272dd50e0cTony Hutter	};
273dd50e0cTony Hutter
274dd50e0cTony Hutter	for (i = 0; i < ARRAY_SIZE(list); i++) {
275dd50e0cTony Hutter		if (nvlist_lookup_nvlist_array(nv, list[i], &child,
276dd50e0cTony Hutter		    &children) == 0) {
277dd50e0cTony Hutter			for (c = 0; c < children; c++) {
278dd50e0cTony Hutter				uint64_t ishole = 0;
279dd50e0cTony Hutter
280dd50e0cTony Hutter				(void) nvlist_lookup_uint64(child[c],
281dd50e0cTony Hutter				    ZPOOL_CONFIG_IS_HOLE, &ishole);
282dd50e0cTony Hutter
283dd50e0cTony Hutter				if (ishole)
284dd50e0cTony Hutter					continue;
285dd50e0cTony Hutter
286dd50e0cTony Hutter				ret |= for_each_vdev_cb(zhp, child[c], func,
287dd50e0cTony Hutter				    data);
288dd50e0cTony Hutter			}
289dd50e0cTony Hutter		}
290dd50e0cTony Hutter	}
291dd50e0cTony Hutter
292dd50e0cTony Hutter	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
293dd50e0cTony Hutter		return (ret);
294dd50e0cTony Hutter
295dd50e0cTony Hutter	/* Don't run our function on root vdevs */
296dd50e0cTony Hutter	if (strcmp(type, VDEV_TYPE_ROOT) != 0) {
297dd50e0cTony Hutter		ret |= func(zhp, nv, data);
298dd50e0cTony Hutter	}
299dd50e0cTony Hutter
300dd50e0cTony Hutter	return (ret);
301dd50e0cTony Hutter}
302dd50e0cTony Hutter
303dd50e0cTony Hutter/*
304dd50e0cTony Hutter * This is the equivalent of for_each_pool() for vdevs.  It iterates thorough
305dd50e0cTony Hutter * all vdevs in the pool, ignoring root vdevs and holes, calling func() on
306dd50e0cTony Hutter * each one.
307dd50e0cTony Hutter *
308dd50e0cTony Hutter * @zhp:	Zpool handle
309dd50e0cTony Hutter * @func:	Function to call on each vdev
310dd50e0cTony Hutter * @data:	Custom data to pass to the function
311dd50e0cTony Hutter */
312dd50e0cTony Hutterint
313dd50e0cTony Hutterfor_each_vdev(zpool_handle_t *zhp, pool_vdev_iter_f func, void *data)
314dd50e0cTony Hutter{
315dd50e0cTony Hutter	nvlist_t *config, *nvroot;
316dd50e0cTony Hutter
317dd50e0cTony Hutter	if ((config = zpool_get_config(zhp, NULL)) != NULL) {
318dd50e0cTony Hutter		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
319dd50e0cTony Hutter		    &nvroot) == 0);
320dd50e0cTony Hutter	}
321dd50e0cTony Hutter	return (for_each_vdev_cb(zhp, nvroot, func, data));
322dd50e0cTony Hutter}
323