libzfs_iter.c revision 263407
1228103Smm/* 2228103Smm * CDDL HEADER START 3228103Smm * 4228103Smm * The contents of this file are subject to the terms of the 5228103Smm * Common Development and Distribution License (the "License"). 6228103Smm * You may not use this file except in compliance with the License. 7228103Smm * 8228103Smm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9228103Smm * or http://www.opensolaris.org/os/licensing. 10228103Smm * See the License for the specific language governing permissions 11228103Smm * and limitations under the License. 12228103Smm * 13228103Smm * When distributing Covered Code, include this CDDL HEADER in each 14228103Smm * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15228103Smm * If applicable, add the following below this CDDL HEADER, with the 16228103Smm * fields enclosed by brackets "[]" replaced with your own identifying 17228103Smm * information: Portions Copyright [yyyy] [name of copyright owner] 18228103Smm * 19228103Smm * CDDL HEADER END 20228103Smm */ 21228103Smm 22228103Smm/* 23228103Smm * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24263407Sdelphij * Copyright (c) 2013 by Delphix. All rights reserved. 25230438Spjd * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>. 26230438Spjd * All rights reserved. 27254755Sdelphij * Copyright 2013 Nexenta Systems, Inc. All rights reserved. 28228103Smm */ 29228103Smm 30228103Smm#include <stdio.h> 31228103Smm#include <stdlib.h> 32228103Smm#include <strings.h> 33228103Smm#include <unistd.h> 34228103Smm#include <stddef.h> 35228103Smm#include <libintl.h> 36228103Smm#include <libzfs.h> 37228103Smm 38228103Smm#include "libzfs_impl.h" 39228103Smm 40228103Smmint 41228103Smmzfs_iter_clones(zfs_handle_t *zhp, zfs_iter_f func, void *data) 42228103Smm{ 43228103Smm nvlist_t *nvl = zfs_get_clones_nvl(zhp); 44228103Smm nvpair_t *pair; 45228103Smm 46228103Smm if (nvl == NULL) 47228103Smm return (0); 48228103Smm 49228103Smm for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL; 50228103Smm pair = nvlist_next_nvpair(nvl, pair)) { 51228103Smm zfs_handle_t *clone = zfs_open(zhp->zfs_hdl, nvpair_name(pair), 52228103Smm ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 53228103Smm if (clone != NULL) { 54228103Smm int err = func(clone, data); 55228103Smm if (err != 0) 56228103Smm return (err); 57228103Smm } 58228103Smm } 59228103Smm return (0); 60228103Smm} 61228103Smm 62228103Smmstatic int 63228103Smmzfs_do_list_ioctl(zfs_handle_t *zhp, unsigned long arg, zfs_cmd_t *zc) 64228103Smm{ 65228103Smm int rc; 66228103Smm uint64_t orig_cookie; 67228103Smm 68228103Smm orig_cookie = zc->zc_cookie; 69228103Smmtop: 70228103Smm (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); 71228103Smm rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc); 72228103Smm 73228103Smm if (rc == -1) { 74228103Smm switch (errno) { 75228103Smm case ENOMEM: 76228103Smm /* expand nvlist memory and try again */ 77228103Smm if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) { 78228103Smm zcmd_free_nvlists(zc); 79228103Smm return (-1); 80228103Smm } 81228103Smm zc->zc_cookie = orig_cookie; 82228103Smm goto top; 83228103Smm /* 84228103Smm * An errno value of ESRCH indicates normal completion. 85228103Smm * If ENOENT is returned, then the underlying dataset 86228103Smm * has been removed since we obtained the handle. 87228103Smm */ 88228103Smm case ESRCH: 89228103Smm case ENOENT: 90228103Smm rc = 1; 91228103Smm break; 92228103Smm default: 93228103Smm rc = zfs_standard_error(zhp->zfs_hdl, errno, 94228103Smm dgettext(TEXT_DOMAIN, 95228103Smm "cannot iterate filesystems")); 96228103Smm break; 97228103Smm } 98228103Smm } 99228103Smm return (rc); 100228103Smm} 101228103Smm 102228103Smm/* 103228103Smm * Iterate over all child filesystems 104228103Smm */ 105228103Smmint 106228103Smmzfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 107228103Smm{ 108228103Smm zfs_cmd_t zc = { 0 }; 109228103Smm zfs_handle_t *nzhp; 110228103Smm int ret; 111228103Smm 112228103Smm if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM) 113228103Smm return (0); 114228103Smm 115228103Smm if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 116228103Smm return (-1); 117228103Smm 118228103Smm while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT, 119228103Smm &zc)) == 0) { 120228103Smm /* 121228103Smm * Silently ignore errors, as the only plausible explanation is 122228103Smm * that the pool has since been removed. 123228103Smm */ 124228103Smm if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl, 125228103Smm &zc)) == NULL) { 126228103Smm continue; 127228103Smm } 128228103Smm 129228103Smm if ((ret = func(nzhp, data)) != 0) { 130228103Smm zcmd_free_nvlists(&zc); 131228103Smm return (ret); 132228103Smm } 133228103Smm } 134228103Smm zcmd_free_nvlists(&zc); 135228103Smm return ((ret < 0) ? ret : 0); 136228103Smm} 137228103Smm 138228103Smm/* 139228103Smm * Iterate over all snapshots 140228103Smm */ 141228103Smmint 142230438Spjdzfs_iter_snapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func, 143230438Spjd void *data) 144228103Smm{ 145228103Smm zfs_cmd_t zc = { 0 }; 146228103Smm zfs_handle_t *nzhp; 147228103Smm int ret; 148228103Smm 149263407Sdelphij if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT || 150263407Sdelphij zhp->zfs_type == ZFS_TYPE_BOOKMARK) 151228103Smm return (0); 152228103Smm 153230438Spjd zc.zc_simple = simple; 154230438Spjd 155228103Smm if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 156228103Smm return (-1); 157228103Smm while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT, 158228103Smm &zc)) == 0) { 159228103Smm 160230438Spjd if (simple) 161230438Spjd nzhp = make_dataset_simple_handle_zc(zhp, &zc); 162230438Spjd else 163230438Spjd nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc); 164230438Spjd if (nzhp == NULL) 165228103Smm continue; 166228103Smm 167228103Smm if ((ret = func(nzhp, data)) != 0) { 168228103Smm zcmd_free_nvlists(&zc); 169228103Smm return (ret); 170228103Smm } 171228103Smm } 172228103Smm zcmd_free_nvlists(&zc); 173228103Smm return ((ret < 0) ? ret : 0); 174228103Smm} 175228103Smm 176228103Smm/* 177263407Sdelphij * Iterate over all bookmarks 178263407Sdelphij */ 179263407Sdelphijint 180263407Sdelphijzfs_iter_bookmarks(zfs_handle_t *zhp, zfs_iter_f func, void *data) 181263407Sdelphij{ 182263407Sdelphij zfs_handle_t *nzhp; 183263407Sdelphij nvlist_t *props = NULL; 184263407Sdelphij nvlist_t *bmarks = NULL; 185263407Sdelphij int err; 186263407Sdelphij 187263407Sdelphij if ((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK)) != 0) 188263407Sdelphij return (0); 189263407Sdelphij 190263407Sdelphij /* Setup the requested properties nvlist. */ 191263407Sdelphij props = fnvlist_alloc(); 192263407Sdelphij fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_GUID)); 193263407Sdelphij fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATETXG)); 194263407Sdelphij fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATION)); 195263407Sdelphij 196263407Sdelphij /* Allocate an nvlist to hold the bookmarks. */ 197263407Sdelphij bmarks = fnvlist_alloc(); 198263407Sdelphij 199263407Sdelphij if ((err = lzc_get_bookmarks(zhp->zfs_name, props, &bmarks)) != 0) 200263407Sdelphij goto out; 201263407Sdelphij 202263407Sdelphij for (nvpair_t *pair = nvlist_next_nvpair(bmarks, NULL); 203263407Sdelphij pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) { 204263407Sdelphij char name[ZFS_MAXNAMELEN]; 205263407Sdelphij char *bmark_name; 206263407Sdelphij nvlist_t *bmark_props; 207263407Sdelphij 208263407Sdelphij bmark_name = nvpair_name(pair); 209263407Sdelphij bmark_props = fnvpair_value_nvlist(pair); 210263407Sdelphij 211263407Sdelphij (void) snprintf(name, sizeof (name), "%s#%s", zhp->zfs_name, 212263407Sdelphij bmark_name); 213263407Sdelphij 214263407Sdelphij nzhp = make_bookmark_handle(zhp, name, bmark_props); 215263407Sdelphij if (nzhp == NULL) 216263407Sdelphij continue; 217263407Sdelphij 218263407Sdelphij if ((err = func(nzhp, data)) != 0) 219263407Sdelphij goto out; 220263407Sdelphij } 221263407Sdelphij 222263407Sdelphijout: 223263407Sdelphij fnvlist_free(props); 224263407Sdelphij fnvlist_free(bmarks); 225263407Sdelphij 226263407Sdelphij return (err); 227263407Sdelphij} 228263407Sdelphij 229263407Sdelphij/* 230228103Smm * Routines for dealing with the sorted snapshot functionality 231228103Smm */ 232228103Smmtypedef struct zfs_node { 233228103Smm zfs_handle_t *zn_handle; 234228103Smm avl_node_t zn_avlnode; 235228103Smm} zfs_node_t; 236228103Smm 237228103Smmstatic int 238228103Smmzfs_sort_snaps(zfs_handle_t *zhp, void *data) 239228103Smm{ 240228103Smm avl_tree_t *avl = data; 241228103Smm zfs_node_t *node; 242228103Smm zfs_node_t search; 243228103Smm 244228103Smm search.zn_handle = zhp; 245228103Smm node = avl_find(avl, &search, NULL); 246228103Smm if (node) { 247228103Smm /* 248228103Smm * If this snapshot was renamed while we were creating the 249228103Smm * AVL tree, it's possible that we already inserted it under 250228103Smm * its old name. Remove the old handle before adding the new 251228103Smm * one. 252228103Smm */ 253228103Smm zfs_close(node->zn_handle); 254228103Smm avl_remove(avl, node); 255228103Smm free(node); 256228103Smm } 257228103Smm 258228103Smm node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t)); 259228103Smm node->zn_handle = zhp; 260228103Smm avl_add(avl, node); 261228103Smm 262228103Smm return (0); 263228103Smm} 264228103Smm 265228103Smmstatic int 266228103Smmzfs_snapshot_compare(const void *larg, const void *rarg) 267228103Smm{ 268228103Smm zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle; 269228103Smm zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle; 270228103Smm uint64_t lcreate, rcreate; 271228103Smm 272228103Smm /* 273228103Smm * Sort them according to creation time. We use the hidden 274228103Smm * CREATETXG property to get an absolute ordering of snapshots. 275228103Smm */ 276228103Smm lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG); 277228103Smm rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG); 278228103Smm 279228103Smm if (lcreate < rcreate) 280228103Smm return (-1); 281228103Smm else if (lcreate > rcreate) 282228103Smm return (+1); 283228103Smm else 284228103Smm return (0); 285228103Smm} 286228103Smm 287228103Smmint 288228103Smmzfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data) 289228103Smm{ 290228103Smm int ret = 0; 291228103Smm zfs_node_t *node; 292228103Smm avl_tree_t avl; 293228103Smm void *cookie = NULL; 294228103Smm 295228103Smm avl_create(&avl, zfs_snapshot_compare, 296228103Smm sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode)); 297228103Smm 298230438Spjd ret = zfs_iter_snapshots(zhp, B_FALSE, zfs_sort_snaps, &avl); 299228103Smm 300228103Smm for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node)) 301228103Smm ret |= callback(node->zn_handle, data); 302228103Smm 303228103Smm while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL) 304228103Smm free(node); 305228103Smm 306228103Smm avl_destroy(&avl); 307228103Smm 308228103Smm return (ret); 309228103Smm} 310228103Smm 311228103Smmtypedef struct { 312228103Smm char *ssa_first; 313228103Smm char *ssa_last; 314228103Smm boolean_t ssa_seenfirst; 315228103Smm boolean_t ssa_seenlast; 316228103Smm zfs_iter_f ssa_func; 317228103Smm void *ssa_arg; 318228103Smm} snapspec_arg_t; 319228103Smm 320228103Smmstatic int 321228103Smmsnapspec_cb(zfs_handle_t *zhp, void *arg) { 322228103Smm snapspec_arg_t *ssa = arg; 323228103Smm char *shortsnapname; 324228103Smm int err = 0; 325228103Smm 326228103Smm if (ssa->ssa_seenlast) 327228103Smm return (0); 328228103Smm shortsnapname = zfs_strdup(zhp->zfs_hdl, 329228103Smm strchr(zfs_get_name(zhp), '@') + 1); 330228103Smm 331228103Smm if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0) 332228103Smm ssa->ssa_seenfirst = B_TRUE; 333228103Smm 334228103Smm if (ssa->ssa_seenfirst) { 335228103Smm err = ssa->ssa_func(zhp, ssa->ssa_arg); 336228103Smm } else { 337228103Smm zfs_close(zhp); 338228103Smm } 339228103Smm 340228103Smm if (strcmp(shortsnapname, ssa->ssa_last) == 0) 341228103Smm ssa->ssa_seenlast = B_TRUE; 342228103Smm free(shortsnapname); 343228103Smm 344228103Smm return (err); 345228103Smm} 346228103Smm 347228103Smm/* 348228103Smm * spec is a string like "A,B%C,D" 349228103Smm * 350228103Smm * <snaps>, where <snaps> can be: 351228103Smm * <snap> (single snapshot) 352228103Smm * <snap>%<snap> (range of snapshots, inclusive) 353228103Smm * %<snap> (range of snapshots, starting with earliest) 354228103Smm * <snap>% (range of snapshots, ending with last) 355228103Smm * % (all snapshots) 356228103Smm * <snaps>[,...] (comma separated list of the above) 357228103Smm * 358228103Smm * If a snapshot can not be opened, continue trying to open the others, but 359228103Smm * return ENOENT at the end. 360228103Smm */ 361228103Smmint 362228103Smmzfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig, 363228103Smm zfs_iter_f func, void *arg) 364228103Smm{ 365248571Smm char *buf, *comma_separated, *cp; 366228103Smm int err = 0; 367228103Smm int ret = 0; 368228103Smm 369248571Smm buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig); 370228103Smm cp = buf; 371228103Smm 372228103Smm while ((comma_separated = strsep(&cp, ",")) != NULL) { 373228103Smm char *pct = strchr(comma_separated, '%'); 374228103Smm if (pct != NULL) { 375228103Smm snapspec_arg_t ssa = { 0 }; 376228103Smm ssa.ssa_func = func; 377228103Smm ssa.ssa_arg = arg; 378228103Smm 379228103Smm if (pct == comma_separated) 380228103Smm ssa.ssa_seenfirst = B_TRUE; 381228103Smm else 382228103Smm ssa.ssa_first = comma_separated; 383228103Smm *pct = '\0'; 384228103Smm ssa.ssa_last = pct + 1; 385228103Smm 386228103Smm /* 387228103Smm * If there is a lastname specified, make sure it 388228103Smm * exists. 389228103Smm */ 390228103Smm if (ssa.ssa_last[0] != '\0') { 391228103Smm char snapname[ZFS_MAXNAMELEN]; 392228103Smm (void) snprintf(snapname, sizeof (snapname), 393228103Smm "%s@%s", zfs_get_name(fs_zhp), 394228103Smm ssa.ssa_last); 395228103Smm if (!zfs_dataset_exists(fs_zhp->zfs_hdl, 396228103Smm snapname, ZFS_TYPE_SNAPSHOT)) { 397228103Smm ret = ENOENT; 398228103Smm continue; 399228103Smm } 400228103Smm } 401228103Smm 402228103Smm err = zfs_iter_snapshots_sorted(fs_zhp, 403228103Smm snapspec_cb, &ssa); 404228103Smm if (ret == 0) 405228103Smm ret = err; 406228103Smm if (ret == 0 && (!ssa.ssa_seenfirst || 407228103Smm (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) { 408228103Smm ret = ENOENT; 409228103Smm } 410228103Smm } else { 411228103Smm char snapname[ZFS_MAXNAMELEN]; 412228103Smm zfs_handle_t *snap_zhp; 413228103Smm (void) snprintf(snapname, sizeof (snapname), "%s@%s", 414228103Smm zfs_get_name(fs_zhp), comma_separated); 415228103Smm snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl, 416228103Smm snapname); 417228103Smm if (snap_zhp == NULL) { 418228103Smm ret = ENOENT; 419228103Smm continue; 420228103Smm } 421228103Smm err = func(snap_zhp, arg); 422228103Smm if (ret == 0) 423228103Smm ret = err; 424228103Smm } 425228103Smm } 426228103Smm 427248571Smm free(buf); 428228103Smm return (ret); 429228103Smm} 430228103Smm 431228103Smm/* 432228103Smm * Iterate over all children, snapshots and filesystems 433228103Smm */ 434228103Smmint 435228103Smmzfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 436228103Smm{ 437228103Smm int ret; 438228103Smm 439228103Smm if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 440228103Smm return (ret); 441228103Smm 442230438Spjd return (zfs_iter_snapshots(zhp, B_FALSE, func, data)); 443228103Smm} 444228103Smm 445228103Smm 446228103Smmtypedef struct iter_stack_frame { 447228103Smm struct iter_stack_frame *next; 448228103Smm zfs_handle_t *zhp; 449228103Smm} iter_stack_frame_t; 450228103Smm 451228103Smmtypedef struct iter_dependents_arg { 452228103Smm boolean_t first; 453228103Smm boolean_t allowrecursion; 454228103Smm iter_stack_frame_t *stack; 455228103Smm zfs_iter_f func; 456228103Smm void *data; 457228103Smm} iter_dependents_arg_t; 458228103Smm 459228103Smmstatic int 460228103Smmiter_dependents_cb(zfs_handle_t *zhp, void *arg) 461228103Smm{ 462228103Smm iter_dependents_arg_t *ida = arg; 463263407Sdelphij int err = 0; 464228103Smm boolean_t first = ida->first; 465228103Smm ida->first = B_FALSE; 466228103Smm 467228103Smm if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 468228103Smm err = zfs_iter_clones(zhp, iter_dependents_cb, ida); 469263407Sdelphij } else if (zhp->zfs_type != ZFS_TYPE_BOOKMARK) { 470228103Smm iter_stack_frame_t isf; 471228103Smm iter_stack_frame_t *f; 472228103Smm 473228103Smm /* 474228103Smm * check if there is a cycle by seeing if this fs is already 475228103Smm * on the stack. 476228103Smm */ 477228103Smm for (f = ida->stack; f != NULL; f = f->next) { 478228103Smm if (f->zhp->zfs_dmustats.dds_guid == 479228103Smm zhp->zfs_dmustats.dds_guid) { 480228103Smm if (ida->allowrecursion) { 481228103Smm zfs_close(zhp); 482228103Smm return (0); 483228103Smm } else { 484228103Smm zfs_error_aux(zhp->zfs_hdl, 485228103Smm dgettext(TEXT_DOMAIN, 486228103Smm "recursive dependency at '%s'"), 487228103Smm zfs_get_name(zhp)); 488228103Smm err = zfs_error(zhp->zfs_hdl, 489228103Smm EZFS_RECURSIVE, 490228103Smm dgettext(TEXT_DOMAIN, 491228103Smm "cannot determine dependent " 492228103Smm "datasets")); 493228103Smm zfs_close(zhp); 494228103Smm return (err); 495228103Smm } 496228103Smm } 497228103Smm } 498228103Smm 499228103Smm isf.zhp = zhp; 500228103Smm isf.next = ida->stack; 501228103Smm ida->stack = &isf; 502228103Smm err = zfs_iter_filesystems(zhp, iter_dependents_cb, ida); 503230438Spjd if (err == 0) { 504230438Spjd err = zfs_iter_snapshots(zhp, B_FALSE, 505230438Spjd iter_dependents_cb, ida); 506230438Spjd } 507228103Smm ida->stack = isf.next; 508228103Smm } 509254755Sdelphij 510228103Smm if (!first && err == 0) 511228103Smm err = ida->func(zhp, ida->data); 512254755Sdelphij else 513254755Sdelphij zfs_close(zhp); 514254755Sdelphij 515228103Smm return (err); 516228103Smm} 517228103Smm 518228103Smmint 519228103Smmzfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 520228103Smm zfs_iter_f func, void *data) 521228103Smm{ 522228103Smm iter_dependents_arg_t ida; 523228103Smm ida.allowrecursion = allowrecursion; 524228103Smm ida.stack = NULL; 525228103Smm ida.func = func; 526228103Smm ida.data = data; 527228103Smm ida.first = B_TRUE; 528228103Smm return (iter_dependents_cb(zfs_handle_dup(zhp), &ida)); 529228103Smm} 530