xref: /titanic_41/usr/src/lib/libzfs/common/libzfs_config.c (revision 445f2479fe3d7435daab18bf2cdc310b86cd6738)
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 http://www.opensolaris.org/os/licensing.
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * The pool configuration repository is stored in /etc/zfs/zpool.cache as a
30  * single packed nvlist.  While it would be nice to just read in this
31  * file from userland, this wouldn't work from a local zone.  So we have to have
32  * a zpool ioctl to return the complete configuration for all pools.  In the
33  * global zone, this will be identical to reading the file and unpacking it in
34  * userland.
35  */
36 
37 #include <errno.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <stddef.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <libintl.h>
44 #include <libuutil.h>
45 
46 #include "libzfs_impl.h"
47 
48 static uu_avl_t *namespace_avl;
49 static uint64_t namespace_generation;
50 
51 typedef struct config_node {
52 	char		*cn_name;
53 	nvlist_t	*cn_config;
54 	uu_avl_node_t	cn_avl;
55 } config_node_t;
56 
57 /* ARGSUSED */
58 static int
59 config_node_compare(const void *a, const void *b, void *unused)
60 {
61 	int ret;
62 
63 	const config_node_t *ca = (config_node_t *)a;
64 	const config_node_t *cb = (config_node_t *)b;
65 
66 	ret = strcmp(ca->cn_name, cb->cn_name);
67 
68 	if (ret < 0)
69 		return (-1);
70 	else if (ret > 0)
71 		return (1);
72 	else
73 		return (0);
74 }
75 
76 /*
77  * Loads the pool namespace, or re-loads it if the cache has changed.
78  */
79 static void
80 namespace_reload()
81 {
82 	nvlist_t *config;
83 	config_node_t *cn;
84 	nvpair_t *elem;
85 	zfs_cmd_t zc = { 0 };
86 	uu_avl_walk_t *walk;
87 
88 	if (namespace_generation == 0) {
89 		/*
90 		 * This is the first time we've accessed the configuration
91 		 * cache.  Initialize the AVL tree and then fall through to the
92 		 * common code.
93 		 */
94 		uu_avl_pool_t *pool;
95 
96 		if ((pool = uu_avl_pool_create("config_pool",
97 		    sizeof (config_node_t),
98 		    offsetof(config_node_t, cn_avl),
99 		    config_node_compare, UU_DEFAULT)) == NULL)
100 			no_memory();
101 
102 		if ((namespace_avl = uu_avl_create(pool, NULL,
103 		    UU_DEFAULT)) == NULL)
104 			no_memory();
105 	}
106 
107 	/*
108 	 * Issue the ZFS_IOC_POOL_CONFIGS ioctl.
109 	 * This can fail for one of two reasons:
110 	 *
111 	 * 	EEXIST		The generation counts match, nothing to do.
112 	 * 	ENOMEM		The zc_config_dst buffer isn't large enough to
113 	 * 			hold the config; zc_config_dst_size will have
114 	 *			been modified to tell us how much to allocate.
115 	 */
116 	zc.zc_config_dst_size = 1024;
117 	zc.zc_config_dst = (uint64_t)(uintptr_t)
118 	    zfs_malloc(zc.zc_config_dst_size);
119 	for (;;) {
120 		zc.zc_cookie = namespace_generation;
121 		if (zfs_ioctl(ZFS_IOC_POOL_CONFIGS, &zc) != 0) {
122 			switch (errno) {
123 			case EEXIST:
124 				/*
125 				 * The namespace hasn't changed.
126 				 */
127 				free((void *)(uintptr_t)zc.zc_config_dst);
128 				return;
129 
130 			case ENOMEM:
131 				free((void *)(uintptr_t)zc.zc_config_dst);
132 				zc.zc_config_dst = (uint64_t)(uintptr_t)
133 				    zfs_malloc(zc.zc_config_dst_size);
134 				break;
135 
136 			default:
137 				zfs_baderror(errno);
138 			}
139 		} else {
140 			namespace_generation = zc.zc_cookie;
141 			break;
142 		}
143 	}
144 
145 	verify(nvlist_unpack((void *)(uintptr_t)zc.zc_config_dst,
146 	    zc.zc_config_dst_size, &config, 0) == 0);
147 
148 	free((void *)(uintptr_t)zc.zc_config_dst);
149 
150 	/*
151 	 * Clear out any existing configuration information.
152 	 */
153 	if ((walk = uu_avl_walk_start(namespace_avl, UU_WALK_ROBUST)) == NULL)
154 		no_memory();
155 
156 	while ((cn = uu_avl_walk_next(walk)) != NULL) {
157 		uu_avl_remove(namespace_avl, cn);
158 		nvlist_free(cn->cn_config);
159 		free(cn->cn_name);
160 		free(cn);
161 	}
162 
163 	elem = NULL;
164 	while ((elem = nvlist_next_nvpair(config, elem)) != NULL) {
165 		nvlist_t *child;
166 		uu_avl_index_t where;
167 
168 		cn = zfs_malloc(sizeof (config_node_t));
169 		cn->cn_name = zfs_strdup(nvpair_name(elem));
170 
171 		verify(nvpair_value_nvlist(elem, &child) == 0);
172 		verify(nvlist_dup(child, &cn->cn_config, 0) == 0);
173 		verify(uu_avl_find(namespace_avl, cn, NULL, &where) == NULL);
174 
175 		uu_avl_insert(namespace_avl, cn, where);
176 	}
177 
178 	nvlist_free(config);
179 }
180 
181 /*
182  * Retrive the configuration for the given pool.  The configuration is a nvlist
183  * describing the vdevs, as well as the statistics associated with each one.
184  */
185 nvlist_t *
186 zpool_get_config(zpool_handle_t *zhp, nvlist_t **oldconfig)
187 {
188 	if (oldconfig)
189 		*oldconfig = zhp->zpool_old_config;
190 	return (zhp->zpool_config);
191 }
192 
193 /*
194  * Refresh the vdev statistics associated with the given pool.  This is used in
195  * iostat to show configuration changes and determine the delta from the last
196  * time the function was called.  This function can fail, in case the pool has
197  * been destroyed.
198  */
199 int
200 zpool_refresh_stats(zpool_handle_t *zhp)
201 {
202 	zfs_cmd_t zc = { 0 };
203 	int error;
204 	nvlist_t *config;
205 
206 	(void) strcpy(zc.zc_name, zhp->zpool_name);
207 
208 	if (zhp->zpool_config_size == 0)
209 		zhp->zpool_config_size = 1 << 16;
210 
211 	zc.zc_config_dst_size = zhp->zpool_config_size;
212 	zc.zc_config_dst = (uint64_t)(uintptr_t)
213 	    zfs_malloc(zc.zc_config_dst_size);
214 
215 	for (;;) {
216 		if (zfs_ioctl(ZFS_IOC_POOL_STATS, &zc) == 0) {
217 			/*
218 			 * The real error is returned in the zc_cookie field.
219 			 */
220 			error = zc.zc_cookie;
221 			break;
222 		}
223 
224 		if (errno == ENOMEM) {
225 			free((void *)(uintptr_t)zc.zc_config_dst);
226 			zc.zc_config_dst = (uint64_t)(uintptr_t)
227 			    zfs_malloc(zc.zc_config_dst_size);
228 		} else {
229 			free((void *)(uintptr_t)zc.zc_config_dst);
230 			return (errno);
231 		}
232 	}
233 
234 	verify(nvlist_unpack((void *)(uintptr_t)zc.zc_config_dst,
235 	    zc.zc_config_dst_size, &config, 0) == 0);
236 
237 	zhp->zpool_config_size = zc.zc_config_dst_size;
238 	free((void *)(uintptr_t)zc.zc_config_dst);
239 
240 	set_pool_health(config);
241 
242 	if (zhp->zpool_config != NULL) {
243 		uint64_t oldtxg, newtxg;
244 
245 		verify(nvlist_lookup_uint64(zhp->zpool_config,
246 		    ZPOOL_CONFIG_POOL_TXG, &oldtxg) == 0);
247 		verify(nvlist_lookup_uint64(config,
248 		    ZPOOL_CONFIG_POOL_TXG, &newtxg) == 0);
249 
250 		if (zhp->zpool_old_config != NULL)
251 			nvlist_free(zhp->zpool_old_config);
252 
253 		if (oldtxg != newtxg) {
254 			nvlist_free(zhp->zpool_config);
255 			zhp->zpool_old_config = NULL;
256 		} else {
257 			zhp->zpool_old_config = zhp->zpool_config;
258 		}
259 	}
260 
261 	zhp->zpool_config = config;
262 
263 	return (error);
264 }
265 
266 /*
267  * Iterate over all pools in the system.
268  */
269 int
270 zpool_iter(zpool_iter_f func, void *data)
271 {
272 	config_node_t *cn;
273 	zpool_handle_t *zhp;
274 	int ret;
275 
276 	namespace_reload();
277 
278 	for (cn = uu_avl_first(namespace_avl); cn != NULL;
279 	    cn = uu_avl_next(namespace_avl, cn)) {
280 
281 		if ((zhp = zpool_open_silent(cn->cn_name)) == NULL)
282 			continue;
283 
284 		if ((ret = func(zhp, data)) != 0)
285 			return (ret);
286 	}
287 
288 	return (0);
289 }
290 
291 /*
292  * Iterate over root datasets, calling the given function for each.  The zfs
293  * handle passed each time must be explicitly closed by the callback.
294  */
295 int
296 zfs_iter_root(zfs_iter_f func, void *data)
297 {
298 	config_node_t *cn;
299 	zfs_handle_t *zhp;
300 	int ret;
301 
302 	namespace_reload();
303 
304 	for (cn = uu_avl_first(namespace_avl); cn != NULL;
305 	    cn = uu_avl_next(namespace_avl, cn)) {
306 
307 		if ((zhp = make_dataset_handle(cn->cn_name)) == NULL)
308 			continue;
309 
310 		if ((ret = func(zhp, data)) != 0)
311 			return (ret);
312 	}
313 
314 	return (0);
315 }
316