Print this page
6328 Fix cstyle errors in zfs codebase (fix studio)
6328 Fix cstyle errors in zfs codebase
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed by: Alex Reece <alex@delphix.com>
Reviewed by: Richard Elling <Richard.Elling@RichardElling.com>
Reviewed by: Jorgen Lundman <lundman@lundman.net>
Approved by: Robert Mustacchi <rm@joyent.com>
NEX-3669 Faults for fans that don't exist
Reviewed by: Jeffry Molanus <jeffry.molanus@nexenta.com>
NEX-3891 Hide the snapshots that belong to in-kernel autosnap-service
Reviewed by: Josef 'Jeff' Sipek <josef.sipek@nexenta.com>
Reviewed by: Alek Pinchuk <alek@nexenta.com>
re #13487 libscf: memory leaks in scf_simple_app_props_get()
re #13485 libzfs: memory leak in iter_dependents_cb()
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/lib/libzfs/common/libzfs_iter.c
+++ new/usr/src/lib/libzfs/common/libzfs_iter.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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright (c) 2013, 2015 by Delphix. All rights reserved.
25 25 * Copyright (c) 2012 Pawel Jakub Dawidek. All rights reserved.
26 26 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
27 27 */
28 28
29 29 #include <stdio.h>
30 30 #include <stdlib.h>
31 31 #include <strings.h>
32 32 #include <unistd.h>
33 33 #include <stddef.h>
34 34 #include <libintl.h>
35 35 #include <libzfs.h>
36 36
37 37 #include "libzfs_impl.h"
38 38
39 39 int
40 40 zfs_iter_clones(zfs_handle_t *zhp, zfs_iter_f func, void *data)
41 41 {
42 42 nvlist_t *nvl = zfs_get_clones_nvl(zhp);
43 43 nvpair_t *pair;
44 44
45 45 if (nvl == NULL)
46 46 return (0);
47 47
48 48 for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL;
49 49 pair = nvlist_next_nvpair(nvl, pair)) {
50 50 zfs_handle_t *clone = zfs_open(zhp->zfs_hdl, nvpair_name(pair),
51 51 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
52 52 if (clone != NULL) {
53 53 int err = func(clone, data);
54 54 if (err != 0)
55 55 return (err);
56 56 }
57 57 }
58 58 return (0);
59 59 }
60 60
61 61 static int
62 62 zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
63 63 {
64 64 int rc;
65 65 uint64_t orig_cookie;
66 66
67 67 orig_cookie = zc->zc_cookie;
68 68 top:
69 69 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
70 70 rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
71 71
72 72 if (rc == -1) {
73 73 switch (errno) {
74 74 case ENOMEM:
75 75 /* expand nvlist memory and try again */
76 76 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
77 77 zcmd_free_nvlists(zc);
78 78 return (-1);
79 79 }
80 80 zc->zc_cookie = orig_cookie;
81 81 goto top;
82 82 /*
83 83 * An errno value of ESRCH indicates normal completion.
84 84 * If ENOENT is returned, then the underlying dataset
85 85 * has been removed since we obtained the handle.
86 86 */
87 87 case ESRCH:
88 88 case ENOENT:
89 89 rc = 1;
90 90 break;
91 91 default:
92 92 rc = zfs_standard_error(zhp->zfs_hdl, errno,
93 93 dgettext(TEXT_DOMAIN,
94 94 "cannot iterate filesystems"));
95 95 break;
96 96 }
97 97 }
98 98 return (rc);
99 99 }
100 100
101 101 /*
102 102 * Iterate over all child filesystems
103 103 */
104 104 int
105 105 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
106 106 {
107 107 zfs_cmd_t zc = { 0 };
108 108 zfs_handle_t *nzhp;
109 109 int ret;
110 110
111 111 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
112 112 return (0);
113 113
114 114 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
115 115 return (-1);
116 116
117 117 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
118 118 &zc)) == 0) {
119 119 /*
120 120 * Silently ignore errors, as the only plausible explanation is
121 121 * that the pool has since been removed.
122 122 */
123 123 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
124 124 &zc)) == NULL) {
125 125 continue;
126 126 }
127 127
128 128 if ((ret = func(nzhp, data)) != 0) {
129 129 zcmd_free_nvlists(&zc);
130 130 return (ret);
|
↓ open down ↓ |
130 lines elided |
↑ open up ↑ |
131 131 }
132 132 }
133 133 zcmd_free_nvlists(&zc);
134 134 return ((ret < 0) ? ret : 0);
135 135 }
136 136
137 137 /*
138 138 * Iterate over all snapshots
139 139 */
140 140 int
141 -zfs_iter_snapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func,
142 - void *data)
141 +zfs_iter_snapshots_internal(zfs_handle_t *zhp, boolean_t simple,
142 + zfs_iter_f func, void *data, boolean_t autosnaps)
143 143 {
144 144 zfs_cmd_t zc = { 0 };
145 - zfs_handle_t *nzhp;
146 145 int ret;
147 146
148 147 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT ||
149 - zhp->zfs_type == ZFS_TYPE_BOOKMARK)
148 + zhp->zfs_type == ZFS_TYPE_BOOKMARK ||
149 + zhp->zfs_type == ZFS_TYPE_AUTOSNAP)
150 150 return (0);
151 151
152 152 zc.zc_simple = simple;
153 153
154 154 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
155 155 return (-1);
156 +
156 157 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
157 158 &zc)) == 0) {
159 + zfs_handle_t *nzhp;
158 160
159 161 if (simple)
160 162 nzhp = make_dataset_simple_handle_zc(zhp, &zc);
161 163 else
162 164 nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc);
163 165 if (nzhp == NULL)
164 166 continue;
165 167
168 + if (autosnaps) {
169 + if (zfs_get_type(nzhp) != ZFS_TYPE_AUTOSNAP) {
170 + zfs_close(nzhp);
171 + continue;
172 + }
173 + } else {
174 + if (zfs_get_type(nzhp) != ZFS_TYPE_SNAPSHOT) {
175 + zfs_close(nzhp);
176 + continue;
177 + }
178 + }
179 +
166 180 if ((ret = func(nzhp, data)) != 0) {
167 181 zcmd_free_nvlists(&zc);
168 182 return (ret);
169 183 }
170 184 }
185 +
171 186 zcmd_free_nvlists(&zc);
172 187 return ((ret < 0) ? ret : 0);
173 188 }
174 189
175 190 /*
191 + * Iterate over all snapshots
192 + */
193 +int
194 +zfs_iter_snapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func,
195 + void *data)
196 +{
197 + return (zfs_iter_snapshots_internal(zhp, simple, func, data, B_FALSE));
198 +}
199 +
200 +int
201 +zfs_iter_autosnapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func,
202 + void *data)
203 +{
204 + return (zfs_iter_snapshots_internal(zhp, simple, func, data, B_TRUE));
205 +}
206 +
207 +/*
176 208 * Iterate over all bookmarks
177 209 */
178 210 int
179 211 zfs_iter_bookmarks(zfs_handle_t *zhp, zfs_iter_f func, void *data)
180 212 {
181 213 zfs_handle_t *nzhp;
182 214 nvlist_t *props = NULL;
183 215 nvlist_t *bmarks = NULL;
184 216 int err;
185 217
186 - if ((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK)) != 0)
218 + if ((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT |
219 + ZFS_TYPE_BOOKMARK | ZFS_TYPE_AUTOSNAP)) != 0)
187 220 return (0);
188 221
189 222 /* Setup the requested properties nvlist. */
190 223 props = fnvlist_alloc();
191 224 fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_GUID));
192 225 fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATETXG));
193 226 fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATION));
194 227
195 228 if ((err = lzc_get_bookmarks(zhp->zfs_name, props, &bmarks)) != 0)
196 229 goto out;
197 230
198 231 for (nvpair_t *pair = nvlist_next_nvpair(bmarks, NULL);
199 232 pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) {
200 233 char name[ZFS_MAX_DATASET_NAME_LEN];
201 234 char *bmark_name;
202 235 nvlist_t *bmark_props;
203 236
204 237 bmark_name = nvpair_name(pair);
205 238 bmark_props = fnvpair_value_nvlist(pair);
206 239
207 240 (void) snprintf(name, sizeof (name), "%s#%s", zhp->zfs_name,
208 241 bmark_name);
209 242
210 243 nzhp = make_bookmark_handle(zhp, name, bmark_props);
211 244 if (nzhp == NULL)
212 245 continue;
213 246
214 247 if ((err = func(nzhp, data)) != 0)
215 248 goto out;
216 249 }
217 250
218 251 out:
219 252 fnvlist_free(props);
220 253 fnvlist_free(bmarks);
221 254
222 255 return (err);
223 256 }
224 257
225 258 /*
226 259 * Routines for dealing with the sorted snapshot functionality
227 260 */
228 261 typedef struct zfs_node {
229 262 zfs_handle_t *zn_handle;
230 263 avl_node_t zn_avlnode;
231 264 } zfs_node_t;
232 265
233 266 static int
234 267 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
235 268 {
236 269 avl_tree_t *avl = data;
237 270 zfs_node_t *node;
238 271 zfs_node_t search;
239 272
240 273 search.zn_handle = zhp;
241 274 node = avl_find(avl, &search, NULL);
242 275 if (node) {
243 276 /*
244 277 * If this snapshot was renamed while we were creating the
245 278 * AVL tree, it's possible that we already inserted it under
246 279 * its old name. Remove the old handle before adding the new
247 280 * one.
248 281 */
249 282 zfs_close(node->zn_handle);
250 283 avl_remove(avl, node);
251 284 free(node);
252 285 }
253 286
254 287 node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
255 288 node->zn_handle = zhp;
256 289 avl_add(avl, node);
257 290
258 291 return (0);
259 292 }
260 293
261 294 static int
262 295 zfs_snapshot_compare(const void *larg, const void *rarg)
263 296 {
264 297 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
265 298 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
266 299 uint64_t lcreate, rcreate;
267 300
268 301 /*
269 302 * Sort them according to creation time. We use the hidden
270 303 * CREATETXG property to get an absolute ordering of snapshots.
271 304 */
272 305 lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
273 306 rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
274 307
275 308 if (lcreate < rcreate)
276 309 return (-1);
277 310 else if (lcreate > rcreate)
278 311 return (+1);
279 312 else
280 313 return (0);
281 314 }
282 315
283 316 int
284 317 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
285 318 {
286 319 int ret = 0;
287 320 zfs_node_t *node;
288 321 avl_tree_t avl;
289 322 void *cookie = NULL;
290 323
291 324 avl_create(&avl, zfs_snapshot_compare,
292 325 sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
293 326
294 327 ret = zfs_iter_snapshots(zhp, B_FALSE, zfs_sort_snaps, &avl);
295 328
296 329 for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
297 330 ret |= callback(node->zn_handle, data);
298 331
299 332 while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
300 333 free(node);
301 334
302 335 avl_destroy(&avl);
303 336
304 337 return (ret);
305 338 }
306 339
307 340 typedef struct {
308 341 char *ssa_first;
309 342 char *ssa_last;
310 343 boolean_t ssa_seenfirst;
311 344 boolean_t ssa_seenlast;
312 345 zfs_iter_f ssa_func;
313 346 void *ssa_arg;
314 347 } snapspec_arg_t;
315 348
316 349 static int
317 350 snapspec_cb(zfs_handle_t *zhp, void *arg)
318 351 {
319 352 snapspec_arg_t *ssa = arg;
320 353 const char *shortsnapname;
321 354 int err = 0;
322 355
323 356 if (ssa->ssa_seenlast)
324 357 return (0);
325 358
326 359 shortsnapname = strchr(zfs_get_name(zhp), '@') + 1;
327 360 if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0)
328 361 ssa->ssa_seenfirst = B_TRUE;
329 362 if (strcmp(shortsnapname, ssa->ssa_last) == 0)
330 363 ssa->ssa_seenlast = B_TRUE;
331 364
332 365 if (ssa->ssa_seenfirst) {
333 366 err = ssa->ssa_func(zhp, ssa->ssa_arg);
334 367 } else {
335 368 zfs_close(zhp);
336 369 }
337 370
338 371 return (err);
339 372 }
340 373
341 374 /*
342 375 * spec is a string like "A,B%C,D"
343 376 *
344 377 * <snaps>, where <snaps> can be:
345 378 * <snap> (single snapshot)
346 379 * <snap>%<snap> (range of snapshots, inclusive)
347 380 * %<snap> (range of snapshots, starting with earliest)
348 381 * <snap>% (range of snapshots, ending with last)
349 382 * % (all snapshots)
350 383 * <snaps>[,...] (comma separated list of the above)
351 384 *
352 385 * If a snapshot can not be opened, continue trying to open the others, but
353 386 * return ENOENT at the end.
354 387 */
355 388 int
356 389 zfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig,
357 390 zfs_iter_f func, void *arg)
358 391 {
359 392 char *buf, *comma_separated, *cp;
360 393 int err = 0;
361 394 int ret = 0;
362 395
363 396 buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig);
364 397 cp = buf;
365 398
366 399 while ((comma_separated = strsep(&cp, ",")) != NULL) {
367 400 char *pct = strchr(comma_separated, '%');
368 401 if (pct != NULL) {
369 402 snapspec_arg_t ssa = { 0 };
370 403 ssa.ssa_func = func;
371 404 ssa.ssa_arg = arg;
372 405
373 406 if (pct == comma_separated)
374 407 ssa.ssa_seenfirst = B_TRUE;
375 408 else
376 409 ssa.ssa_first = comma_separated;
377 410 *pct = '\0';
378 411 ssa.ssa_last = pct + 1;
379 412
380 413 /*
381 414 * If there is a lastname specified, make sure it
382 415 * exists.
383 416 */
384 417 if (ssa.ssa_last[0] != '\0') {
385 418 char snapname[ZFS_MAX_DATASET_NAME_LEN];
386 419 (void) snprintf(snapname, sizeof (snapname),
387 420 "%s@%s", zfs_get_name(fs_zhp),
388 421 ssa.ssa_last);
389 422 if (!zfs_dataset_exists(fs_zhp->zfs_hdl,
390 423 snapname, ZFS_TYPE_SNAPSHOT)) {
391 424 ret = ENOENT;
392 425 continue;
393 426 }
394 427 }
395 428
396 429 err = zfs_iter_snapshots_sorted(fs_zhp,
397 430 snapspec_cb, &ssa);
398 431 if (ret == 0)
399 432 ret = err;
400 433 if (ret == 0 && (!ssa.ssa_seenfirst ||
401 434 (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) {
402 435 ret = ENOENT;
403 436 }
404 437 } else {
405 438 char snapname[ZFS_MAX_DATASET_NAME_LEN];
406 439 zfs_handle_t *snap_zhp;
407 440 (void) snprintf(snapname, sizeof (snapname), "%s@%s",
408 441 zfs_get_name(fs_zhp), comma_separated);
409 442 snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl,
410 443 snapname);
411 444 if (snap_zhp == NULL) {
412 445 ret = ENOENT;
413 446 continue;
414 447 }
415 448 err = func(snap_zhp, arg);
416 449 if (ret == 0)
|
↓ open down ↓ |
220 lines elided |
↑ open up ↑ |
417 450 ret = err;
418 451 }
419 452 }
420 453
421 454 free(buf);
422 455 return (ret);
423 456 }
424 457
425 458 /*
426 459 * Iterate over all children, snapshots and filesystems
427 - * Process snapshots before filesystems because they are nearer the input
428 - * handle: this is extremely important when used with zfs_iter_f functions
429 - * looking for data, following the logic that we would like to find it as soon
430 - * and as close as possible.
431 460 */
432 461 int
433 462 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
434 463 {
435 464 int ret;
436 465
437 - if ((ret = zfs_iter_snapshots(zhp, B_FALSE, func, data)) != 0)
466 + if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
438 467 return (ret);
439 468
440 - return (zfs_iter_filesystems(zhp, func, data));
469 + return (zfs_iter_snapshots(zhp, B_FALSE, func, data));
441 470 }
442 471
443 472
444 473 typedef struct iter_stack_frame {
445 474 struct iter_stack_frame *next;
446 475 zfs_handle_t *zhp;
447 476 } iter_stack_frame_t;
448 477
449 478 typedef struct iter_dependents_arg {
450 479 boolean_t first;
451 480 boolean_t allowrecursion;
452 481 iter_stack_frame_t *stack;
453 482 zfs_iter_f func;
454 483 void *data;
455 484 } iter_dependents_arg_t;
456 485
457 486 static int
458 487 iter_dependents_cb(zfs_handle_t *zhp, void *arg)
459 488 {
460 489 iter_dependents_arg_t *ida = arg;
461 490 int err = 0;
462 491 boolean_t first = ida->first;
463 492 ida->first = B_FALSE;
464 493
465 494 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
466 495 err = zfs_iter_clones(zhp, iter_dependents_cb, ida);
467 496 } else if (zhp->zfs_type != ZFS_TYPE_BOOKMARK) {
468 497 iter_stack_frame_t isf;
469 498 iter_stack_frame_t *f;
470 499
471 500 /*
472 501 * check if there is a cycle by seeing if this fs is already
473 502 * on the stack.
474 503 */
475 504 for (f = ida->stack; f != NULL; f = f->next) {
476 505 if (f->zhp->zfs_dmustats.dds_guid ==
477 506 zhp->zfs_dmustats.dds_guid) {
478 507 if (ida->allowrecursion) {
479 508 zfs_close(zhp);
480 509 return (0);
481 510 } else {
482 511 zfs_error_aux(zhp->zfs_hdl,
483 512 dgettext(TEXT_DOMAIN,
484 513 "recursive dependency at '%s'"),
485 514 zfs_get_name(zhp));
486 515 err = zfs_error(zhp->zfs_hdl,
487 516 EZFS_RECURSIVE,
488 517 dgettext(TEXT_DOMAIN,
489 518 "cannot determine dependent "
490 519 "datasets"));
491 520 zfs_close(zhp);
492 521 return (err);
493 522 }
494 523 }
|
↓ open down ↓ |
44 lines elided |
↑ open up ↑ |
495 524 }
496 525
497 526 isf.zhp = zhp;
498 527 isf.next = ida->stack;
499 528 ida->stack = &isf;
500 529 err = zfs_iter_filesystems(zhp, iter_dependents_cb, ida);
501 530 if (err == 0) {
502 531 err = zfs_iter_snapshots(zhp, B_FALSE,
503 532 iter_dependents_cb, ida);
504 533 }
534 +
535 + if (err == 0) {
536 + err = zfs_iter_autosnapshots(zhp, B_FALSE,
537 + iter_dependents_cb, ida);
538 + }
539 +
505 540 ida->stack = isf.next;
506 541 }
507 542
508 543 if (!first && err == 0)
509 544 err = ida->func(zhp, ida->data);
510 545 else
511 546 zfs_close(zhp);
512 547
513 548 return (err);
514 549 }
515 550
516 551 int
517 552 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
518 553 zfs_iter_f func, void *data)
519 554 {
520 555 iter_dependents_arg_t ida;
521 556 ida.allowrecursion = allowrecursion;
522 557 ida.stack = NULL;
523 558 ida.func = func;
524 559 ida.data = data;
525 560 ida.first = B_TRUE;
526 561 return (iter_dependents_cb(zfs_handle_dup(zhp), &ida));
527 562 }
|
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX