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