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