1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23 /*
24 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
26 */
27
28 /*
29 * Copyright (c) 2012 by Delphix. All rights reserved.
30 * Copyright (c) 2015 by Syneto S.R.L. All rights reserved.
31 * Copyright 2016 Nexenta Systems, Inc.
32 */
33
34 /*
35 * The pool configuration repository is stored in /etc/zfs/zpool.cache as a
36 * single packed nvlist. While it would be nice to just read in this
37 * file from userland, this wouldn't work from a local zone. So we have to have
38 * a zpool ioctl to return the complete configuration for all pools. In the
39 * global zone, this will be identical to reading the file and unpacking it in
40 * userland.
41 */
42
43 #include <errno.h>
44 #include <sys/stat.h>
45 #include <fcntl.h>
46 #include <stddef.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <libintl.h>
50 #include <libuutil.h>
51
52 #include "libzfs_impl.h"
53
54 typedef struct config_node {
55 char *cn_name;
56 nvlist_t *cn_config;
57 uu_avl_node_t cn_avl;
58 } config_node_t;
59
60 static int
config_node_compare(const void * a,const void * b,void * unused)61 config_node_compare(const void *a, const void *b, void *unused)
62 {
63 (void) unused;
64 const config_node_t *ca = (config_node_t *)a;
65 const config_node_t *cb = (config_node_t *)b;
66
67 int ret = strcmp(ca->cn_name, cb->cn_name);
68
69 if (ret < 0)
70 return (-1);
71 else if (ret > 0)
72 return (1);
73 else
74 return (0);
75 }
76
77 void
namespace_clear(libzfs_handle_t * hdl)78 namespace_clear(libzfs_handle_t *hdl)
79 {
80 if (hdl->libzfs_ns_avl) {
81 config_node_t *cn;
82 void *cookie = NULL;
83
84 while ((cn = uu_avl_teardown(hdl->libzfs_ns_avl,
85 &cookie)) != NULL) {
86 nvlist_free(cn->cn_config);
87 free(cn->cn_name);
88 free(cn);
89 }
90
91 uu_avl_destroy(hdl->libzfs_ns_avl);
92 hdl->libzfs_ns_avl = NULL;
93 }
94
95 if (hdl->libzfs_ns_avlpool) {
96 uu_avl_pool_destroy(hdl->libzfs_ns_avlpool);
97 hdl->libzfs_ns_avlpool = NULL;
98 }
99 }
100
101 /*
102 * Loads the pool namespace, or re-loads it if the cache has changed.
103 */
104 static int
namespace_reload(libzfs_handle_t * hdl)105 namespace_reload(libzfs_handle_t *hdl)
106 {
107 nvlist_t *config;
108 config_node_t *cn;
109 nvpair_t *elem;
110 zfs_cmd_t zc = {"\0"};
111 void *cookie;
112
113 if (hdl->libzfs_ns_gen == 0) {
114 /*
115 * This is the first time we've accessed the configuration
116 * cache. Initialize the AVL tree and then fall through to the
117 * common code.
118 */
119 if ((hdl->libzfs_ns_avlpool = uu_avl_pool_create("config_pool",
120 sizeof (config_node_t),
121 offsetof(config_node_t, cn_avl),
122 config_node_compare, UU_DEFAULT)) == NULL)
123 return (no_memory(hdl));
124
125 if ((hdl->libzfs_ns_avl = uu_avl_create(hdl->libzfs_ns_avlpool,
126 NULL, UU_DEFAULT)) == NULL)
127 return (no_memory(hdl));
128 }
129
130 zcmd_alloc_dst_nvlist(hdl, &zc, 0);
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 zcmd_expand_dst_nvlist(hdl, &zc);
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 cn = zfs_alloc(hdl, sizeof (config_node_t));
182 cn->cn_name = zfs_strdup(hdl, nvpair_name(elem));
183 child = fnvpair_value_nvlist(elem);
184 if (nvlist_dup(child, &cn->cn_config, 0) != 0) {
185 free(cn->cn_name);
186 free(cn);
187 nvlist_free(config);
188 return (no_memory(hdl));
189 }
190 verify(uu_avl_find(hdl->libzfs_ns_avl, cn, NULL, &where)
191 == NULL);
192
193 uu_avl_insert(hdl->libzfs_ns_avl, cn, where);
194 }
195
196 nvlist_free(config);
197 return (0);
198 }
199
200 /*
201 * Retrieve the configuration for the given pool. The configuration is an nvlist
202 * describing the vdevs, as well as the statistics associated with each one.
203 */
204 nvlist_t *
zpool_get_config(zpool_handle_t * zhp,nvlist_t ** oldconfig)205 zpool_get_config(zpool_handle_t *zhp, nvlist_t **oldconfig)
206 {
207 if (oldconfig)
208 *oldconfig = zhp->zpool_old_config;
209 return (zhp->zpool_config);
210 }
211
212 /*
213 * Retrieves a list of enabled features and their refcounts and caches it in
214 * the pool handle.
215 */
216 nvlist_t *
zpool_get_features(zpool_handle_t * zhp)217 zpool_get_features(zpool_handle_t *zhp)
218 {
219 nvlist_t *config, *features;
220
221 config = zpool_get_config(zhp, NULL);
222
223 if (config == NULL || !nvlist_exists(config,
224 ZPOOL_CONFIG_FEATURE_STATS)) {
225 int error;
226 boolean_t missing = B_FALSE;
227
228 error = zpool_refresh_stats(zhp, &missing);
229
230 if (error != 0 || missing)
231 return (NULL);
232
233 config = zpool_get_config(zhp, NULL);
234 }
235
236 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
237 &features) != 0)
238 return (NULL);
239
240 return (features);
241 }
242
243 /*
244 * Refresh the vdev statistics associated with the given pool. This is used in
245 * iostat to show configuration changes and determine the delta from the last
246 * time the function was called. This function can fail, in case the pool has
247 * been destroyed.
248 */
249 int
zpool_refresh_stats(zpool_handle_t * zhp,boolean_t * missing)250 zpool_refresh_stats(zpool_handle_t *zhp, boolean_t *missing)
251 {
252 zfs_cmd_t zc = {"\0"};
253 int error;
254 nvlist_t *config;
255 libzfs_handle_t *hdl = zhp->zpool_hdl;
256
257 *missing = B_FALSE;
258 (void) strcpy(zc.zc_name, zhp->zpool_name);
259
260 if (zhp->zpool_config_size == 0)
261 zhp->zpool_config_size = 1 << 16;
262
263 zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size);
264
265 for (;;) {
266 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_STATS,
267 &zc) == 0) {
268 /*
269 * The real error is returned in the zc_cookie field.
270 */
271 error = zc.zc_cookie;
272 break;
273 }
274
275 if (errno == ENOMEM)
276 zcmd_expand_dst_nvlist(hdl, &zc);
277 else {
278 zcmd_free_nvlists(&zc);
279 if (errno == ENOENT || errno == EINVAL)
280 *missing = B_TRUE;
281 zhp->zpool_state = POOL_STATE_UNAVAIL;
282 return (0);
283 }
284 }
285
286 if (zcmd_read_dst_nvlist(hdl, &zc, &config) != 0) {
287 zcmd_free_nvlists(&zc);
288 return (-1);
289 }
290
291 zcmd_free_nvlists(&zc);
292
293 zhp->zpool_config_size = zc.zc_nvlist_dst_size;
294
295 if (zhp->zpool_config != NULL) {
296 nvlist_free(zhp->zpool_old_config);
297
298 zhp->zpool_old_config = zhp->zpool_config;
299 }
300
301 zhp->zpool_config = config;
302 if (error)
303 zhp->zpool_state = POOL_STATE_UNAVAIL;
304 else
305 zhp->zpool_state = POOL_STATE_ACTIVE;
306
307 return (0);
308 }
309
310 /*
311 * The following environment variables are undocumented
312 * and should be used for testing purposes only:
313 *
314 * __ZFS_POOL_EXCLUDE - don't iterate over the pools it lists
315 * __ZFS_POOL_RESTRICT - iterate only over the pools it lists
316 *
317 * This function returns B_TRUE if the pool should be skipped
318 * during iteration.
319 */
320 boolean_t
zpool_skip_pool(const char * poolname)321 zpool_skip_pool(const char *poolname)
322 {
323 static boolean_t initialized = B_FALSE;
324 static const char *exclude = NULL;
325 static const char *restricted = NULL;
326
327 const char *cur, *end;
328 int len;
329 int namelen = strlen(poolname);
330
331 if (!initialized) {
332 initialized = B_TRUE;
333 exclude = getenv("__ZFS_POOL_EXCLUDE");
334 restricted = getenv("__ZFS_POOL_RESTRICT");
335 }
336
337 if (exclude != NULL) {
338 cur = exclude;
339 do {
340 end = strchr(cur, ' ');
341 len = (NULL == end) ? strlen(cur) : (end - cur);
342 if (len == namelen && 0 == strncmp(cur, poolname, len))
343 return (B_TRUE);
344 cur += (len + 1);
345 } while (NULL != end);
346 }
347
348 if (NULL == restricted)
349 return (B_FALSE);
350
351 cur = restricted;
352 do {
353 end = strchr(cur, ' ');
354 len = (NULL == end) ? strlen(cur) : (end - cur);
355
356 if (len == namelen && 0 == strncmp(cur, poolname, len)) {
357 return (B_FALSE);
358 }
359
360 cur += (len + 1);
361 } while (NULL != end);
362
363 return (B_TRUE);
364 }
365
366 /*
367 * Iterate over all pools in the system.
368 */
369 int
zpool_iter(libzfs_handle_t * hdl,zpool_iter_f func,void * data)370 zpool_iter(libzfs_handle_t *hdl, zpool_iter_f func, void *data)
371 {
372 config_node_t *cn;
373 zpool_handle_t *zhp;
374 int ret;
375
376 /*
377 * If someone makes a recursive call to zpool_iter(), we want to avoid
378 * refreshing the namespace because that will invalidate the parent
379 * context. We allow recursive calls, but simply re-use the same
380 * namespace AVL tree.
381 */
382 if (!hdl->libzfs_pool_iter && namespace_reload(hdl) != 0)
383 return (-1);
384
385 hdl->libzfs_pool_iter++;
386 for (cn = uu_avl_first(hdl->libzfs_ns_avl); cn != NULL;
387 cn = uu_avl_next(hdl->libzfs_ns_avl, cn)) {
388
389 if (zpool_skip_pool(cn->cn_name))
390 continue;
391
392 if (zpool_open_silent(hdl, cn->cn_name, &zhp) != 0) {
393 hdl->libzfs_pool_iter--;
394 return (-1);
395 }
396
397 if (zhp == NULL)
398 continue;
399
400 if ((ret = func(zhp, data)) != 0) {
401 hdl->libzfs_pool_iter--;
402 return (ret);
403 }
404 }
405 hdl->libzfs_pool_iter--;
406
407 return (0);
408 }
409
410 /*
411 * Iterate over root datasets, calling the given function for each. The zfs
412 * handle passed each time must be explicitly closed by the callback.
413 */
414 int
zfs_iter_root(libzfs_handle_t * hdl,zfs_iter_f func,void * data)415 zfs_iter_root(libzfs_handle_t *hdl, zfs_iter_f func, void *data)
416 {
417 config_node_t *cn;
418 zfs_handle_t *zhp;
419 int ret;
420
421 if (namespace_reload(hdl) != 0)
422 return (-1);
423
424 for (cn = uu_avl_first(hdl->libzfs_ns_avl); cn != NULL;
425 cn = uu_avl_next(hdl->libzfs_ns_avl, cn)) {
426
427 if (zpool_skip_pool(cn->cn_name))
428 continue;
429
430 if ((zhp = make_dataset_handle(hdl, cn->cn_name)) == NULL)
431 continue;
432
433 if ((ret = func(zhp, data)) != 0)
434 return (ret);
435 }
436
437 return (0);
438 }
439