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