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