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>
35fa9e406ahrens
36fa9e406ahrens#include <libzfs.h>
37fa9e406ahrens
38fa9e406ahrens#include "zpool_util.h"
39fa9e406ahrens
40fa9e406ahrens/*
41fa9e406ahrens * Private interface for iterating over pools specified on the command line.
42fa9e406ahrens * Most consumers will call for_each_pool, but in order to support iostat, we
43fa9e406ahrens * allow fined grained control through the zpool_list_t interface.
44fa9e406ahrens */
45fa9e406ahrens
46fa9e406ahrenstypedef struct zpool_node {
47fa9e406ahrens	zpool_handle_t	*zn_handle;
48fa9e406ahrens	uu_avl_node_t	zn_avlnode;
49fa9e406ahrens	int		zn_mark;
50fa9e406ahrens} zpool_node_t;
51fa9e406ahrens
52fa9e406ahrensstruct zpool_list {
5399653d4eschrock	boolean_t	zl_findall;
54fa9e406ahrens	uu_avl_t	*zl_avl;
55fa9e406ahrens	uu_avl_pool_t	*zl_pool;
56990b485lling	zprop_list_t	**zl_proplist;
57fa9e406ahrens};
58fa9e406ahrens
59fa9e406ahrens/* ARGSUSED */
60fa9e406ahrensstatic int
61fa9e406ahrenszpool_compare(const void *larg, const void *rarg, void *unused)
62fa9e406ahrens{
63fa9e406ahrens	zpool_handle_t *l = ((zpool_node_t *)larg)->zn_handle;
64fa9e406ahrens	zpool_handle_t *r = ((zpool_node_t *)rarg)->zn_handle;
65fa9e406ahrens	const char *lname = zpool_get_name(l);
66fa9e406ahrens	const char *rname = zpool_get_name(r);
67fa9e406ahrens
68fa9e406ahrens	return (strcmp(lname, rname));
69fa9e406ahrens}
70fa9e406ahrens
71fa9e406ahrens/*
72fa9e406ahrens * Callback function for pool_list_get().  Adds the given pool to the AVL tree
73fa9e406ahrens * of known pools.
74fa9e406ahrens */
75fa9e406ahrensstatic int
76fa9e406ahrensadd_pool(zpool_handle_t *zhp, void *data)
77fa9e406ahrens{
78fa9e406ahrens	zpool_list_t *zlp = data;
79fa9e406ahrens	zpool_node_t *node = safe_malloc(sizeof (zpool_node_t));
80fa9e406ahrens	uu_avl_index_t idx;
81fa9e406ahrens
82fa9e406ahrens	node->zn_handle = zhp;
83fa9e406ahrens	uu_avl_node_init(node, &node->zn_avlnode, zlp->zl_pool);
84fa9e406ahrens	if (uu_avl_find(zlp->zl_avl, node, NULL, &idx) == NULL) {
85990b485lling		if (zlp->zl_proplist &&
86990b485lling		    zpool_expand_proplist(zhp, zlp->zl_proplist) != 0) {
87990b485lling			zpool_close(zhp);
88990b485lling			free(node);
89990b485lling			return (-1);
90990b485lling		}
91fa9e406ahrens		uu_avl_insert(zlp->zl_avl, node, idx);
92fa9e406ahrens	} else {
93fa9e406ahrens		zpool_close(zhp);
94fa9e406ahrens		free(node);
95b1b8ab3lling		return (-1);
96fa9e406ahrens	}
97fa9e406ahrens
98fa9e406ahrens	return (0);
99fa9e406ahrens}
100fa9e406ahrens
101fa9e406ahrens/*
102fa9e406ahrens * Create a list of pools based on the given arguments.  If we're given no
103fa9e406ahrens * arguments, then iterate over all pools in the system and add them to the AVL
104fa9e406ahrens * tree.  Otherwise, add only those pool explicitly specified on the command
105fa9e406ahrens * line.
106fa9e406ahrens */
107fa9e406ahrenszpool_list_t *
108990b485llingpool_list_get(int argc, char **argv, zprop_list_t **proplist, int *err)
109fa9e406ahrens{
110fa9e406ahrens	zpool_list_t *zlp;
111fa9e406ahrens
112fa9e406ahrens	zlp = safe_malloc(sizeof (zpool_list_t));
113fa9e406ahrens
114fa9e406ahrens	zlp->zl_pool = uu_avl_pool_create("zfs_pool", sizeof (zpool_node_t),
115fa9e406ahrens	    offsetof(zpool_node_t, zn_avlnode), zpool_compare, UU_DEFAULT);
116fa9e406ahrens
117fa9e406ahrens	if (zlp->zl_pool == NULL)
1185ad8204nd		zpool_no_memory();
119fa9e406ahrens
120fa9e406ahrens	if ((zlp->zl_avl = uu_avl_create(zlp->zl_pool, NULL,
121fa9e406ahrens	    UU_DEFAULT)) == NULL)
1225ad8204nd		zpool_no_memory();
123fa9e406ahrens
124990b485lling	zlp->zl_proplist = proplist;
125990b485lling
126fa9e406ahrens	if (argc == 0) {
12799653d4eschrock		(void) zpool_iter(g_zfs, add_pool, zlp);
12899653d4eschrock		zlp->zl_findall = B_TRUE;
129fa9e406ahrens	} else {
130fa9e406ahrens		int i;
131fa9e406ahrens
132fa9e406ahrens		for (i = 0; i < argc; i++) {
133fa9e406ahrens			zpool_handle_t *zhp;
134fa9e406ahrens
135b327cd3Igor Kozhukhov			if ((zhp = zpool_open_canfail(g_zfs, argv[i])) !=
136b327cd3Igor Kozhukhov			    NULL) {
137990b485lling				if (add_pool(zhp, zlp) != 0)
138b1b8ab3lling					*err = B_TRUE;
139990b485lling			} else {
14099653d4eschrock				*err = B_TRUE;
141990b485lling			}
142fa9e406ahrens		}
143fa9e406ahrens	}
144fa9e406ahrens
145fa9e406ahrens	return (zlp);
146fa9e406ahrens}
147fa9e406ahrens
148fa9e406ahrens/*
149fa9e406ahrens * Search for any new pools, adding them to the list.  We only add pools when no
150fa9e406ahrens * options were given on the command line.  Otherwise, we keep the list fixed as
151fa9e406ahrens * those that were explicitly specified.
152fa9e406ahrens */
153fa9e406ahrensvoid
154fa9e406ahrenspool_list_update(zpool_list_t *zlp)
155fa9e406ahrens{
156fa9e406ahrens	if (zlp->zl_findall)
15799653d4eschrock		(void) zpool_iter(g_zfs, add_pool, zlp);
158fa9e406ahrens}
159fa9e406ahrens
160fa9e406ahrens/*
161fa9e406ahrens * Iterate over all pools in the list, executing the callback for each
162fa9e406ahrens */
163fa9e406ahrensint
164fa9e406ahrenspool_list_iter(zpool_list_t *zlp, int unavail, zpool_iter_f func,
165fa9e406ahrens    void *data)
166fa9e406ahrens{
167fa9e406ahrens	zpool_node_t *node, *next_node;
168fa9e406ahrens	int ret = 0;
169fa9e406ahrens
170fa9e406ahrens	for (node = uu_avl_first(zlp->zl_avl); node != NULL; node = next_node) {
171fa9e406ahrens		next_node = uu_avl_next(zlp->zl_avl, node);
172fa9e406ahrens		if (zpool_get_state(node->zn_handle) != POOL_STATE_UNAVAIL ||
173fa9e406ahrens		    unavail)
174fa9e406ahrens			ret |= func(node->zn_handle, data);
175fa9e406ahrens	}
176fa9e406ahrens
177fa9e406ahrens	return (ret);
178fa9e406ahrens}
179fa9e406ahrens
180fa9e406ahrens/*
181fa9e406ahrens * Remove the given pool from the list.  When running iostat, we want to remove
182fa9e406ahrens * those pools that no longer exist.
183fa9e406ahrens */
184fa9e406ahrensvoid
185fa9e406ahrenspool_list_remove(zpool_list_t *zlp, zpool_handle_t *zhp)
186fa9e406ahrens{
187fa9e406ahrens	zpool_node_t search, *node;
188fa9e406ahrens
189fa9e406ahrens	search.zn_handle = zhp;
190fa9e406ahrens	if ((node = uu_avl_find(zlp->zl_avl, &search, NULL, NULL)) != NULL) {
191fa9e406ahrens		uu_avl_remove(zlp->zl_avl, node);
192fa9e406ahrens		zpool_close(node->zn_handle);
193fa9e406ahrens		free(node);
194fa9e406ahrens	}
195fa9e406ahrens}
196fa9e406ahrens
197fa9e406ahrens/*
198fa9e406ahrens * Free all the handles associated with this list.
199fa9e406ahrens */
200fa9e406ahrensvoid
201fa9e406ahrenspool_list_free(zpool_list_t *zlp)
202fa9e406ahrens{
203fa9e406ahrens	uu_avl_walk_t *walk;
204fa9e406ahrens	zpool_node_t *node;
205fa9e406ahrens
206fa9e406ahrens	if ((walk = uu_avl_walk_start(zlp->zl_avl, UU_WALK_ROBUST)) == NULL) {
207fa9e406ahrens		(void) fprintf(stderr,
208fa9e406ahrens		    gettext("internal error: out of memory"));
209fa9e406ahrens		exit(1);
210fa9e406ahrens	}
211fa9e406ahrens
212fa9e406ahrens	while ((node = uu_avl_walk_next(walk)) != NULL) {
213fa9e406ahrens		uu_avl_remove(zlp->zl_avl, node);
214fa9e406ahrens		zpool_close(node->zn_handle);
215fa9e406ahrens		free(node);
216fa9e406ahrens	}
217fa9e406ahrens
218fa9e406ahrens	uu_avl_walk_end(walk);
219fa9e406ahrens	uu_avl_destroy(zlp->zl_avl);
220fa9e406ahrens	uu_avl_pool_destroy(zlp->zl_pool);
221fa9e406ahrens
222fa9e406ahrens	free(zlp);
223fa9e406ahrens}
224fa9e406ahrens
225fa9e406ahrens/*
226fa9e406ahrens * Returns the number of elements in the pool list.
227fa9e406ahrens */
228fa9e406ahrensint
229fa9e406ahrenspool_list_count(zpool_list_t *zlp)
230fa9e406ahrens{
231fa9e406ahrens	return (uu_avl_numnodes(zlp->zl_avl));
232fa9e406ahrens}
233fa9e406ahrens
234fa9e406ahrens/*
235fa9e406ahrens * High level function which iterates over all pools given on the command line,
236fa9e406ahrens * using the pool_list_* interfaces.
237fa9e406ahrens */
238fa9e406ahrensint
239b1b8ab3llingfor_each_pool(int argc, char **argv, boolean_t unavail,
240990b485lling    zprop_list_t **proplist, zpool_iter_f func, void *data)
241fa9e406ahrens{
242fa9e406ahrens	zpool_list_t *list;
243fa9e406ahrens	int ret = 0;
244fa9e406ahrens
245b1b8ab3lling	if ((list = pool_list_get(argc, argv, proplist, &ret)) == NULL)
246fa9e406ahrens		return (1);
247fa9e406ahrens
248fa9e406ahrens	if (pool_list_iter(list, unavail, func, data) != 0)
249fa9e406ahrens		ret = 1;
250fa9e406ahrens
251fa9e406ahrens	pool_list_free(list);
252fa9e406ahrens
253fa9e406ahrens	return (ret);
254fa9e406ahrens}
255