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