xref: /freebsd/sys/contrib/openzfs/lib/libzfs/libzfs_config.c (revision 45dd2eaac379e5576f745380260470204c49beac)
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 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Copyright (c) 2012 by Delphix. All rights reserved.
29  * Copyright (c) 2015 by Syneto S.R.L. All rights reserved.
30  * Copyright 2016 Nexenta Systems, Inc.
31  */
32 
33 /*
34  * The pool configuration repository is stored in /etc/zfs/zpool.cache as a
35  * single packed nvlist.  While it would be nice to just read in this
36  * file from userland, this wouldn't work from a local zone.  So we have to have
37  * a zpool ioctl to return the complete configuration for all pools.  In the
38  * global zone, this will be identical to reading the file and unpacking it in
39  * userland.
40  */
41 
42 #include <errno.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <stddef.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <libintl.h>
49 #include <libuutil.h>
50 
51 #include "libzfs_impl.h"
52 
53 typedef struct config_node {
54 	char		*cn_name;
55 	nvlist_t	*cn_config;
56 	uu_avl_node_t	cn_avl;
57 } config_node_t;
58 
59 static int
60 config_node_compare(const void *a, const void *b, void *unused)
61 {
62 	(void) unused;
63 	const config_node_t *ca = (config_node_t *)a;
64 	const config_node_t *cb = (config_node_t *)b;
65 
66 	int 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 void
77 namespace_clear(libzfs_handle_t *hdl)
78 {
79 	if (hdl->libzfs_ns_avl) {
80 		config_node_t *cn;
81 		void *cookie = NULL;
82 
83 		while ((cn = uu_avl_teardown(hdl->libzfs_ns_avl,
84 		    &cookie)) != NULL) {
85 			nvlist_free(cn->cn_config);
86 			free(cn->cn_name);
87 			free(cn);
88 		}
89 
90 		uu_avl_destroy(hdl->libzfs_ns_avl);
91 		hdl->libzfs_ns_avl = NULL;
92 	}
93 
94 	if (hdl->libzfs_ns_avlpool) {
95 		uu_avl_pool_destroy(hdl->libzfs_ns_avlpool);
96 		hdl->libzfs_ns_avlpool = NULL;
97 	}
98 }
99 
100 /*
101  * Loads the pool namespace, or re-loads it if the cache has changed.
102  */
103 static int
104 namespace_reload(libzfs_handle_t *hdl)
105 {
106 	nvlist_t *config;
107 	config_node_t *cn;
108 	nvpair_t *elem;
109 	zfs_cmd_t zc = {"\0"};
110 	void *cookie;
111 
112 	if (hdl->libzfs_ns_gen == 0) {
113 		/*
114 		 * This is the first time we've accessed the configuration
115 		 * cache.  Initialize the AVL tree and then fall through to the
116 		 * common code.
117 		 */
118 		if ((hdl->libzfs_ns_avlpool = uu_avl_pool_create("config_pool",
119 		    sizeof (config_node_t),
120 		    offsetof(config_node_t, cn_avl),
121 		    config_node_compare, UU_DEFAULT)) == NULL)
122 			return (no_memory(hdl));
123 
124 		if ((hdl->libzfs_ns_avl = uu_avl_create(hdl->libzfs_ns_avlpool,
125 		    NULL, UU_DEFAULT)) == NULL)
126 			return (no_memory(hdl));
127 	}
128 
129 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
130 		return (-1);
131 
132 	for (;;) {
133 		zc.zc_cookie = hdl->libzfs_ns_gen;
134 		if (zfs_ioctl(hdl, ZFS_IOC_POOL_CONFIGS, &zc) != 0) {
135 			switch (errno) {
136 			case EEXIST:
137 				/*
138 				 * The namespace hasn't changed.
139 				 */
140 				zcmd_free_nvlists(&zc);
141 				return (0);
142 
143 			case ENOMEM:
144 				if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
145 					zcmd_free_nvlists(&zc);
146 					return (-1);
147 				}
148 				break;
149 
150 			default:
151 				zcmd_free_nvlists(&zc);
152 				return (zfs_standard_error(hdl, errno,
153 				    dgettext(TEXT_DOMAIN, "failed to read "
154 				    "pool configuration")));
155 			}
156 		} else {
157 			hdl->libzfs_ns_gen = zc.zc_cookie;
158 			break;
159 		}
160 	}
161 
162 	if (zcmd_read_dst_nvlist(hdl, &zc, &config) != 0) {
163 		zcmd_free_nvlists(&zc);
164 		return (-1);
165 	}
166 
167 	zcmd_free_nvlists(&zc);
168 
169 	/*
170 	 * Clear out any existing configuration information.
171 	 */
172 	cookie = NULL;
173 	while ((cn = uu_avl_teardown(hdl->libzfs_ns_avl, &cookie)) != NULL) {
174 		nvlist_free(cn->cn_config);
175 		free(cn->cn_name);
176 		free(cn);
177 	}
178 
179 	elem = NULL;
180 	while ((elem = nvlist_next_nvpair(config, elem)) != NULL) {
181 		nvlist_t *child;
182 		uu_avl_index_t where;
183 
184 		if ((cn = zfs_alloc(hdl, sizeof (config_node_t))) == NULL) {
185 			nvlist_free(config);
186 			return (-1);
187 		}
188 
189 		if ((cn->cn_name = zfs_strdup(hdl,
190 		    nvpair_name(elem))) == NULL) {
191 			free(cn);
192 			nvlist_free(config);
193 			return (-1);
194 		}
195 
196 		child = fnvpair_value_nvlist(elem);
197 		if (nvlist_dup(child, &cn->cn_config, 0) != 0) {
198 			free(cn->cn_name);
199 			free(cn);
200 			nvlist_free(config);
201 			return (no_memory(hdl));
202 		}
203 		verify(uu_avl_find(hdl->libzfs_ns_avl, cn, NULL, &where)
204 		    == NULL);
205 
206 		uu_avl_insert(hdl->libzfs_ns_avl, cn, where);
207 	}
208 
209 	nvlist_free(config);
210 	return (0);
211 }
212 
213 /*
214  * Retrieve the configuration for the given pool. The configuration is an nvlist
215  * describing the vdevs, as well as the statistics associated with each one.
216  */
217 nvlist_t *
218 zpool_get_config(zpool_handle_t *zhp, nvlist_t **oldconfig)
219 {
220 	if (oldconfig)
221 		*oldconfig = zhp->zpool_old_config;
222 	return (zhp->zpool_config);
223 }
224 
225 /*
226  * Retrieves a list of enabled features and their refcounts and caches it in
227  * the pool handle.
228  */
229 nvlist_t *
230 zpool_get_features(zpool_handle_t *zhp)
231 {
232 	nvlist_t *config, *features;
233 
234 	config = zpool_get_config(zhp, NULL);
235 
236 	if (config == NULL || !nvlist_exists(config,
237 	    ZPOOL_CONFIG_FEATURE_STATS)) {
238 		int error;
239 		boolean_t missing = B_FALSE;
240 
241 		error = zpool_refresh_stats(zhp, &missing);
242 
243 		if (error != 0 || missing)
244 			return (NULL);
245 
246 		config = zpool_get_config(zhp, NULL);
247 	}
248 
249 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
250 	    &features) != 0)
251 		return (NULL);
252 
253 	return (features);
254 }
255 
256 /*
257  * Refresh the vdev statistics associated with the given pool.  This is used in
258  * iostat to show configuration changes and determine the delta from the last
259  * time the function was called.  This function can fail, in case the pool has
260  * been destroyed.
261  */
262 int
263 zpool_refresh_stats(zpool_handle_t *zhp, boolean_t *missing)
264 {
265 	zfs_cmd_t zc = {"\0"};
266 	int error;
267 	nvlist_t *config;
268 	libzfs_handle_t *hdl = zhp->zpool_hdl;
269 
270 	*missing = B_FALSE;
271 	(void) strcpy(zc.zc_name, zhp->zpool_name);
272 
273 	if (zhp->zpool_config_size == 0)
274 		zhp->zpool_config_size = 1 << 16;
275 
276 	if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size) != 0)
277 		return (-1);
278 
279 	for (;;) {
280 		if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_STATS,
281 		    &zc) == 0) {
282 			/*
283 			 * The real error is returned in the zc_cookie field.
284 			 */
285 			error = zc.zc_cookie;
286 			break;
287 		}
288 
289 		if (errno == ENOMEM) {
290 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
291 				zcmd_free_nvlists(&zc);
292 				return (-1);
293 			}
294 		} else {
295 			zcmd_free_nvlists(&zc);
296 			if (errno == ENOENT || errno == EINVAL)
297 				*missing = B_TRUE;
298 			zhp->zpool_state = POOL_STATE_UNAVAIL;
299 			return (0);
300 		}
301 	}
302 
303 	if (zcmd_read_dst_nvlist(hdl, &zc, &config) != 0) {
304 		zcmd_free_nvlists(&zc);
305 		return (-1);
306 	}
307 
308 	zcmd_free_nvlists(&zc);
309 
310 	zhp->zpool_config_size = zc.zc_nvlist_dst_size;
311 
312 	if (zhp->zpool_config != NULL) {
313 		nvlist_free(zhp->zpool_old_config);
314 
315 		zhp->zpool_old_config = zhp->zpool_config;
316 	}
317 
318 	zhp->zpool_config = config;
319 	if (error)
320 		zhp->zpool_state = POOL_STATE_UNAVAIL;
321 	else
322 		zhp->zpool_state = POOL_STATE_ACTIVE;
323 
324 	return (0);
325 }
326 
327 /*
328  * The following environment variables are undocumented
329  * and should be used for testing purposes only:
330  *
331  * __ZFS_POOL_EXCLUDE - don't iterate over the pools it lists
332  * __ZFS_POOL_RESTRICT - iterate only over the pools it lists
333  *
334  * This function returns B_TRUE if the pool should be skipped
335  * during iteration.
336  */
337 boolean_t
338 zpool_skip_pool(const char *poolname)
339 {
340 	static boolean_t initialized = B_FALSE;
341 	static const char *exclude = NULL;
342 	static const char *restricted = NULL;
343 
344 	const char *cur, *end;
345 	int len;
346 	int namelen = strlen(poolname);
347 
348 	if (!initialized) {
349 		initialized = B_TRUE;
350 		exclude = getenv("__ZFS_POOL_EXCLUDE");
351 		restricted = getenv("__ZFS_POOL_RESTRICT");
352 	}
353 
354 	if (exclude != NULL) {
355 		cur = exclude;
356 		do {
357 			end = strchr(cur, ' ');
358 			len = (NULL == end) ? strlen(cur) : (end - cur);
359 			if (len == namelen && 0 == strncmp(cur, poolname, len))
360 				return (B_TRUE);
361 			cur += (len + 1);
362 		} while (NULL != end);
363 	}
364 
365 	if (NULL == restricted)
366 		return (B_FALSE);
367 
368 	cur = restricted;
369 	do {
370 		end = strchr(cur, ' ');
371 		len = (NULL == end) ? strlen(cur) : (end - cur);
372 
373 		if (len == namelen && 0 == strncmp(cur, poolname, len)) {
374 			return (B_FALSE);
375 		}
376 
377 		cur += (len + 1);
378 	} while (NULL != end);
379 
380 	return (B_TRUE);
381 }
382 
383 /*
384  * Iterate over all pools in the system.
385  */
386 int
387 zpool_iter(libzfs_handle_t *hdl, zpool_iter_f func, void *data)
388 {
389 	config_node_t *cn;
390 	zpool_handle_t *zhp;
391 	int ret;
392 
393 	/*
394 	 * If someone makes a recursive call to zpool_iter(), we want to avoid
395 	 * refreshing the namespace because that will invalidate the parent
396 	 * context.  We allow recursive calls, but simply re-use the same
397 	 * namespace AVL tree.
398 	 */
399 	if (!hdl->libzfs_pool_iter && namespace_reload(hdl) != 0)
400 		return (-1);
401 
402 	hdl->libzfs_pool_iter++;
403 	for (cn = uu_avl_first(hdl->libzfs_ns_avl); cn != NULL;
404 	    cn = uu_avl_next(hdl->libzfs_ns_avl, cn)) {
405 
406 		if (zpool_skip_pool(cn->cn_name))
407 			continue;
408 
409 		if (zpool_open_silent(hdl, cn->cn_name, &zhp) != 0) {
410 			hdl->libzfs_pool_iter--;
411 			return (-1);
412 		}
413 
414 		if (zhp == NULL)
415 			continue;
416 
417 		if ((ret = func(zhp, data)) != 0) {
418 			hdl->libzfs_pool_iter--;
419 			return (ret);
420 		}
421 	}
422 	hdl->libzfs_pool_iter--;
423 
424 	return (0);
425 }
426 
427 /*
428  * Iterate over root datasets, calling the given function for each.  The zfs
429  * handle passed each time must be explicitly closed by the callback.
430  */
431 int
432 zfs_iter_root(libzfs_handle_t *hdl, zfs_iter_f func, void *data)
433 {
434 	config_node_t *cn;
435 	zfs_handle_t *zhp;
436 	int ret;
437 
438 	if (namespace_reload(hdl) != 0)
439 		return (-1);
440 
441 	for (cn = uu_avl_first(hdl->libzfs_ns_avl); cn != NULL;
442 	    cn = uu_avl_next(hdl->libzfs_ns_avl, cn)) {
443 
444 		if (zpool_skip_pool(cn->cn_name))
445 			continue;
446 
447 		if ((zhp = make_dataset_handle(hdl, cn->cn_name)) == NULL)
448 			continue;
449 
450 		if ((ret = func(zhp, data)) != 0)
451 			return (ret);
452 	}
453 
454 	return (0);
455 }
456