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