xref: /titanic_52/usr/src/lib/libzfs/common/libzfs_config.c (revision 0173c38a73f34277e0c97a19fedfd25d81ba8380)
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 typedef struct config_node {
49 	char		*cn_name;
50 	nvlist_t	*cn_config;
51 	uu_avl_node_t	cn_avl;
52 } config_node_t;
53 
54 /* ARGSUSED */
55 static int
56 config_node_compare(const void *a, const void *b, void *unused)
57 {
58 	int ret;
59 
60 	const config_node_t *ca = (config_node_t *)a;
61 	const config_node_t *cb = (config_node_t *)b;
62 
63 	ret = strcmp(ca->cn_name, cb->cn_name);
64 
65 	if (ret < 0)
66 		return (-1);
67 	else if (ret > 0)
68 		return (1);
69 	else
70 		return (0);
71 }
72 
73 void
74 namespace_clear(libzfs_handle_t *hdl)
75 {
76 	if (hdl->libzfs_ns_avl) {
77 		uu_avl_walk_t *walk;
78 		config_node_t *cn;
79 
80 		if ((walk = uu_avl_walk_start(hdl->libzfs_ns_avl,
81 		    UU_WALK_ROBUST)) == NULL)
82 			return;
83 
84 		while ((cn = uu_avl_walk_next(walk)) != NULL) {
85 			uu_avl_remove(hdl->libzfs_ns_avl, cn);
86 			nvlist_free(cn->cn_config);
87 			free(cn->cn_name);
88 			free(cn);
89 		}
90 
91 		uu_avl_walk_end(walk);
92 
93 		uu_avl_destroy(hdl->libzfs_ns_avl);
94 		hdl->libzfs_ns_avl = NULL;
95 	}
96 
97 	if (hdl->libzfs_ns_avlpool) {
98 		uu_avl_pool_destroy(hdl->libzfs_ns_avlpool);
99 		hdl->libzfs_ns_avlpool = NULL;
100 	}
101 }
102 
103 /*
104  * Loads the pool namespace, or re-loads it if the cache has changed.
105  */
106 static int
107 namespace_reload(libzfs_handle_t *hdl)
108 {
109 	nvlist_t *config;
110 	config_node_t *cn;
111 	nvpair_t *elem;
112 	zfs_cmd_t zc = { 0 };
113 	uu_avl_walk_t *walk;
114 
115 	if (hdl->libzfs_ns_gen == 0) {
116 		/*
117 		 * This is the first time we've accessed the configuration
118 		 * cache.  Initialize the AVL tree and then fall through to the
119 		 * common code.
120 		 */
121 		if ((hdl->libzfs_ns_avlpool = uu_avl_pool_create("config_pool",
122 		    sizeof (config_node_t),
123 		    offsetof(config_node_t, cn_avl),
124 		    config_node_compare, UU_DEFAULT)) == NULL)
125 			return (no_memory(hdl));
126 
127 		if ((hdl->libzfs_ns_avl = uu_avl_create(hdl->libzfs_ns_avlpool,
128 		    NULL, UU_DEFAULT)) == NULL)
129 			return (no_memory(hdl));
130 	}
131 
132 	/*
133 	 * Issue the ZFS_IOC_POOL_CONFIGS ioctl.
134 	 * This can fail for one of two reasons:
135 	 *
136 	 * 	EEXIST		The generation counts match, nothing to do.
137 	 * 	ENOMEM		The zc_config_dst buffer isn't large enough to
138 	 * 			hold the config; zc_config_dst_size will have
139 	 *			been modified to tell us how much to allocate.
140 	 */
141 	zc.zc_config_dst_size = 1024;
142 	if ((zc.zc_config_dst = (uint64_t)(uintptr_t)
143 	    zfs_alloc(hdl, zc.zc_config_dst_size)) == NULL)
144 		return (-1);
145 	for (;;) {
146 		zc.zc_cookie = hdl->libzfs_ns_gen;
147 		if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_CONFIGS, &zc) != 0) {
148 			switch (errno) {
149 			case EEXIST:
150 				/*
151 				 * The namespace hasn't changed.
152 				 */
153 				free((void *)(uintptr_t)zc.zc_config_dst);
154 				return (0);
155 
156 			case ENOMEM:
157 				free((void *)(uintptr_t)zc.zc_config_dst);
158 				if ((zc.zc_config_dst = (uint64_t)(uintptr_t)
159 				    zfs_alloc(hdl, zc.zc_config_dst_size))
160 				    == NULL)
161 					return (-1);
162 				break;
163 
164 			default:
165 				return (zfs_standard_error(hdl, errno,
166 				    dgettext(TEXT_DOMAIN, "failed to read "
167 				    "pool configuration")));
168 			}
169 		} else {
170 			hdl->libzfs_ns_gen = zc.zc_cookie;
171 			break;
172 		}
173 	}
174 
175 	if (nvlist_unpack((void *)(uintptr_t)zc.zc_config_dst,
176 	    zc.zc_config_dst_size, &config, 0) != 0) {
177 		free((void *)(uintptr_t)zc.zc_config_dst);
178 		return (no_memory(hdl));
179 	}
180 
181 	free((void *)(uintptr_t)zc.zc_config_dst);
182 
183 	/*
184 	 * Clear out any existing configuration information.
185 	 */
186 	if ((walk = uu_avl_walk_start(hdl->libzfs_ns_avl,
187 	    UU_WALK_ROBUST)) == NULL) {
188 		nvlist_free(config);
189 		return (no_memory(hdl));
190 	}
191 
192 	while ((cn = uu_avl_walk_next(walk)) != NULL) {
193 		uu_avl_remove(hdl->libzfs_ns_avl, cn);
194 		nvlist_free(cn->cn_config);
195 		free(cn->cn_name);
196 		free(cn);
197 	}
198 
199 	uu_avl_walk_end(walk);
200 
201 	elem = NULL;
202 	while ((elem = nvlist_next_nvpair(config, elem)) != NULL) {
203 		nvlist_t *child;
204 		uu_avl_index_t where;
205 
206 		if ((cn = zfs_alloc(hdl, sizeof (config_node_t))) == NULL) {
207 			nvlist_free(config);
208 			return (-1);
209 		}
210 
211 		if ((cn->cn_name = zfs_strdup(hdl,
212 		    nvpair_name(elem))) == NULL) {
213 			free(cn);
214 			nvlist_free(config);
215 			return (-1);
216 		}
217 
218 		verify(nvpair_value_nvlist(elem, &child) == 0);
219 		if (nvlist_dup(child, &cn->cn_config, 0) != 0) {
220 			free(cn->cn_name);
221 			free(cn);
222 			nvlist_free(config);
223 			return (no_memory(hdl));
224 		}
225 		verify(uu_avl_find(hdl->libzfs_ns_avl, cn, NULL, &where)
226 		    == NULL);
227 
228 		uu_avl_insert(hdl->libzfs_ns_avl, cn, where);
229 	}
230 
231 	nvlist_free(config);
232 	return (0);
233 }
234 
235 /*
236  * Retrive the configuration for the given pool.  The configuration is a nvlist
237  * describing the vdevs, as well as the statistics associated with each one.
238  */
239 nvlist_t *
240 zpool_get_config(zpool_handle_t *zhp, nvlist_t **oldconfig)
241 {
242 	if (oldconfig)
243 		*oldconfig = zhp->zpool_old_config;
244 	return (zhp->zpool_config);
245 }
246 
247 /*
248  * Refresh the vdev statistics associated with the given pool.  This is used in
249  * iostat to show configuration changes and determine the delta from the last
250  * time the function was called.  This function can fail, in case the pool has
251  * been destroyed.
252  */
253 int
254 zpool_refresh_stats(zpool_handle_t *zhp, boolean_t *missing)
255 {
256 	zfs_cmd_t zc = { 0 };
257 	int error;
258 	nvlist_t *config;
259 
260 	*missing = B_FALSE;
261 	(void) strcpy(zc.zc_name, zhp->zpool_name);
262 
263 	if (zhp->zpool_config_size == 0)
264 		zhp->zpool_config_size = 1 << 16;
265 
266 	zc.zc_config_dst_size = zhp->zpool_config_size;
267 	if ((zc.zc_config_dst = (uint64_t)(uintptr_t)
268 	    zfs_alloc(zhp->zpool_hdl, zc.zc_config_dst_size)) == NULL)
269 		return (-1);
270 
271 	for (;;) {
272 		if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_POOL_STATS,
273 		    &zc) == 0) {
274 			/*
275 			 * The real error is returned in the zc_cookie field.
276 			 */
277 			error = zc.zc_cookie;
278 			break;
279 		}
280 
281 		if (errno == ENOMEM) {
282 			free((void *)(uintptr_t)zc.zc_config_dst);
283 			if ((zc.zc_config_dst = (uint64_t)(uintptr_t)
284 			    zfs_alloc(zhp->zpool_hdl,
285 			    zc.zc_config_dst_size)) == NULL)
286 				return (-1);
287 		} else {
288 			free((void *)(uintptr_t)zc.zc_config_dst);
289 			if (errno == ENOENT || errno == EINVAL)
290 				*missing = B_TRUE;
291 			zhp->zpool_state = POOL_STATE_UNAVAIL;
292 			return (0);
293 		}
294 	}
295 
296 	if (nvlist_unpack((void *)(uintptr_t)zc.zc_config_dst,
297 	    zc.zc_config_dst_size, &config, 0) != 0) {
298 		free((void *)(uintptr_t)zc.zc_config_dst);
299 		return (no_memory(zhp->zpool_hdl));
300 	}
301 
302 	zhp->zpool_config_size = zc.zc_config_dst_size;
303 	free((void *)(uintptr_t)zc.zc_config_dst);
304 
305 	if (set_pool_health(config) != 0) {
306 		nvlist_free(config);
307 		return (no_memory(zhp->zpool_hdl));
308 	}
309 
310 	if (zhp->zpool_config != NULL) {
311 		uint64_t oldtxg, newtxg;
312 
313 		verify(nvlist_lookup_uint64(zhp->zpool_config,
314 		    ZPOOL_CONFIG_POOL_TXG, &oldtxg) == 0);
315 		verify(nvlist_lookup_uint64(config,
316 		    ZPOOL_CONFIG_POOL_TXG, &newtxg) == 0);
317 
318 		if (zhp->zpool_old_config != NULL)
319 			nvlist_free(zhp->zpool_old_config);
320 
321 		if (oldtxg != newtxg) {
322 			nvlist_free(zhp->zpool_config);
323 			zhp->zpool_old_config = NULL;
324 		} else {
325 			zhp->zpool_old_config = zhp->zpool_config;
326 		}
327 	}
328 
329 	zhp->zpool_config = config;
330 	if (error)
331 		zhp->zpool_state = POOL_STATE_UNAVAIL;
332 	else
333 		zhp->zpool_state = POOL_STATE_ACTIVE;
334 
335 	return (0);
336 }
337 
338 /*
339  * Iterate over all pools in the system.
340  */
341 int
342 zpool_iter(libzfs_handle_t *hdl, zpool_iter_f func, void *data)
343 {
344 	config_node_t *cn;
345 	zpool_handle_t *zhp;
346 	int ret;
347 
348 	if (namespace_reload(hdl) != 0)
349 		return (-1);
350 
351 	for (cn = uu_avl_first(hdl->libzfs_ns_avl); cn != NULL;
352 	    cn = uu_avl_next(hdl->libzfs_ns_avl, cn)) {
353 
354 		if (zpool_open_silent(hdl, cn->cn_name, &zhp) != 0)
355 			return (-1);
356 
357 		if (zhp == NULL)
358 			continue;
359 
360 		if ((ret = func(zhp, data)) != 0)
361 			return (ret);
362 	}
363 
364 	return (0);
365 }
366 
367 /*
368  * Iterate over root datasets, calling the given function for each.  The zfs
369  * handle passed each time must be explicitly closed by the callback.
370  */
371 int
372 zfs_iter_root(libzfs_handle_t *hdl, zfs_iter_f func, void *data)
373 {
374 	config_node_t *cn;
375 	zfs_handle_t *zhp;
376 	int ret;
377 
378 	if (namespace_reload(hdl) != 0)
379 		return (-1);
380 
381 	for (cn = uu_avl_first(hdl->libzfs_ns_avl); cn != NULL;
382 	    cn = uu_avl_next(hdl->libzfs_ns_avl, cn)) {
383 
384 		if ((zhp = make_dataset_handle(hdl, cn->cn_name)) == NULL)
385 			continue;
386 
387 		if ((ret = func(zhp, data)) != 0)
388 			return (ret);
389 	}
390 
391 	return (0);
392 }
393