libzfs_iter.c revision 248571
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. 24228103Smm * Copyright 2010 Nexenta Systems, Inc. All rights reserved. 25248571Smm * Copyright (c) 2012 by Delphix. All rights reserved. 26230438Spjd * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>. 27230438Spjd * 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 149228103Smm if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) 150228103Smm return (0); 151228103Smm 152230438Spjd zc.zc_simple = simple; 153230438Spjd 154228103Smm if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 155228103Smm return (-1); 156228103Smm while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT, 157228103Smm &zc)) == 0) { 158228103Smm 159230438Spjd if (simple) 160230438Spjd nzhp = make_dataset_simple_handle_zc(zhp, &zc); 161230438Spjd else 162230438Spjd nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc); 163230438Spjd if (nzhp == NULL) 164228103Smm continue; 165228103Smm 166228103Smm if ((ret = func(nzhp, data)) != 0) { 167228103Smm zcmd_free_nvlists(&zc); 168228103Smm return (ret); 169228103Smm } 170228103Smm } 171228103Smm zcmd_free_nvlists(&zc); 172228103Smm return ((ret < 0) ? ret : 0); 173228103Smm} 174228103Smm 175228103Smm/* 176228103Smm * Routines for dealing with the sorted snapshot functionality 177228103Smm */ 178228103Smmtypedef struct zfs_node { 179228103Smm zfs_handle_t *zn_handle; 180228103Smm avl_node_t zn_avlnode; 181228103Smm} zfs_node_t; 182228103Smm 183228103Smmstatic int 184228103Smmzfs_sort_snaps(zfs_handle_t *zhp, void *data) 185228103Smm{ 186228103Smm avl_tree_t *avl = data; 187228103Smm zfs_node_t *node; 188228103Smm zfs_node_t search; 189228103Smm 190228103Smm search.zn_handle = zhp; 191228103Smm node = avl_find(avl, &search, NULL); 192228103Smm if (node) { 193228103Smm /* 194228103Smm * If this snapshot was renamed while we were creating the 195228103Smm * AVL tree, it's possible that we already inserted it under 196228103Smm * its old name. Remove the old handle before adding the new 197228103Smm * one. 198228103Smm */ 199228103Smm zfs_close(node->zn_handle); 200228103Smm avl_remove(avl, node); 201228103Smm free(node); 202228103Smm } 203228103Smm 204228103Smm node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t)); 205228103Smm node->zn_handle = zhp; 206228103Smm avl_add(avl, node); 207228103Smm 208228103Smm return (0); 209228103Smm} 210228103Smm 211228103Smmstatic int 212228103Smmzfs_snapshot_compare(const void *larg, const void *rarg) 213228103Smm{ 214228103Smm zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle; 215228103Smm zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle; 216228103Smm uint64_t lcreate, rcreate; 217228103Smm 218228103Smm /* 219228103Smm * Sort them according to creation time. We use the hidden 220228103Smm * CREATETXG property to get an absolute ordering of snapshots. 221228103Smm */ 222228103Smm lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG); 223228103Smm rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG); 224228103Smm 225228103Smm if (lcreate < rcreate) 226228103Smm return (-1); 227228103Smm else if (lcreate > rcreate) 228228103Smm return (+1); 229228103Smm else 230228103Smm return (0); 231228103Smm} 232228103Smm 233228103Smmint 234228103Smmzfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data) 235228103Smm{ 236228103Smm int ret = 0; 237228103Smm zfs_node_t *node; 238228103Smm avl_tree_t avl; 239228103Smm void *cookie = NULL; 240228103Smm 241228103Smm avl_create(&avl, zfs_snapshot_compare, 242228103Smm sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode)); 243228103Smm 244230438Spjd ret = zfs_iter_snapshots(zhp, B_FALSE, zfs_sort_snaps, &avl); 245228103Smm 246228103Smm for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node)) 247228103Smm ret |= callback(node->zn_handle, data); 248228103Smm 249228103Smm while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL) 250228103Smm free(node); 251228103Smm 252228103Smm avl_destroy(&avl); 253228103Smm 254228103Smm return (ret); 255228103Smm} 256228103Smm 257228103Smmtypedef struct { 258228103Smm char *ssa_first; 259228103Smm char *ssa_last; 260228103Smm boolean_t ssa_seenfirst; 261228103Smm boolean_t ssa_seenlast; 262228103Smm zfs_iter_f ssa_func; 263228103Smm void *ssa_arg; 264228103Smm} snapspec_arg_t; 265228103Smm 266228103Smmstatic int 267228103Smmsnapspec_cb(zfs_handle_t *zhp, void *arg) { 268228103Smm snapspec_arg_t *ssa = arg; 269228103Smm char *shortsnapname; 270228103Smm int err = 0; 271228103Smm 272228103Smm if (ssa->ssa_seenlast) 273228103Smm return (0); 274228103Smm shortsnapname = zfs_strdup(zhp->zfs_hdl, 275228103Smm strchr(zfs_get_name(zhp), '@') + 1); 276228103Smm 277228103Smm if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0) 278228103Smm ssa->ssa_seenfirst = B_TRUE; 279228103Smm 280228103Smm if (ssa->ssa_seenfirst) { 281228103Smm err = ssa->ssa_func(zhp, ssa->ssa_arg); 282228103Smm } else { 283228103Smm zfs_close(zhp); 284228103Smm } 285228103Smm 286228103Smm if (strcmp(shortsnapname, ssa->ssa_last) == 0) 287228103Smm ssa->ssa_seenlast = B_TRUE; 288228103Smm free(shortsnapname); 289228103Smm 290228103Smm return (err); 291228103Smm} 292228103Smm 293228103Smm/* 294228103Smm * spec is a string like "A,B%C,D" 295228103Smm * 296228103Smm * <snaps>, where <snaps> can be: 297228103Smm * <snap> (single snapshot) 298228103Smm * <snap>%<snap> (range of snapshots, inclusive) 299228103Smm * %<snap> (range of snapshots, starting with earliest) 300228103Smm * <snap>% (range of snapshots, ending with last) 301228103Smm * % (all snapshots) 302228103Smm * <snaps>[,...] (comma separated list of the above) 303228103Smm * 304228103Smm * If a snapshot can not be opened, continue trying to open the others, but 305228103Smm * return ENOENT at the end. 306228103Smm */ 307228103Smmint 308228103Smmzfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig, 309228103Smm zfs_iter_f func, void *arg) 310228103Smm{ 311248571Smm char *buf, *comma_separated, *cp; 312228103Smm int err = 0; 313228103Smm int ret = 0; 314228103Smm 315248571Smm buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig); 316228103Smm cp = buf; 317228103Smm 318228103Smm while ((comma_separated = strsep(&cp, ",")) != NULL) { 319228103Smm char *pct = strchr(comma_separated, '%'); 320228103Smm if (pct != NULL) { 321228103Smm snapspec_arg_t ssa = { 0 }; 322228103Smm ssa.ssa_func = func; 323228103Smm ssa.ssa_arg = arg; 324228103Smm 325228103Smm if (pct == comma_separated) 326228103Smm ssa.ssa_seenfirst = B_TRUE; 327228103Smm else 328228103Smm ssa.ssa_first = comma_separated; 329228103Smm *pct = '\0'; 330228103Smm ssa.ssa_last = pct + 1; 331228103Smm 332228103Smm /* 333228103Smm * If there is a lastname specified, make sure it 334228103Smm * exists. 335228103Smm */ 336228103Smm if (ssa.ssa_last[0] != '\0') { 337228103Smm char snapname[ZFS_MAXNAMELEN]; 338228103Smm (void) snprintf(snapname, sizeof (snapname), 339228103Smm "%s@%s", zfs_get_name(fs_zhp), 340228103Smm ssa.ssa_last); 341228103Smm if (!zfs_dataset_exists(fs_zhp->zfs_hdl, 342228103Smm snapname, ZFS_TYPE_SNAPSHOT)) { 343228103Smm ret = ENOENT; 344228103Smm continue; 345228103Smm } 346228103Smm } 347228103Smm 348228103Smm err = zfs_iter_snapshots_sorted(fs_zhp, 349228103Smm snapspec_cb, &ssa); 350228103Smm if (ret == 0) 351228103Smm ret = err; 352228103Smm if (ret == 0 && (!ssa.ssa_seenfirst || 353228103Smm (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) { 354228103Smm ret = ENOENT; 355228103Smm } 356228103Smm } else { 357228103Smm char snapname[ZFS_MAXNAMELEN]; 358228103Smm zfs_handle_t *snap_zhp; 359228103Smm (void) snprintf(snapname, sizeof (snapname), "%s@%s", 360228103Smm zfs_get_name(fs_zhp), comma_separated); 361228103Smm snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl, 362228103Smm snapname); 363228103Smm if (snap_zhp == NULL) { 364228103Smm ret = ENOENT; 365228103Smm continue; 366228103Smm } 367228103Smm err = func(snap_zhp, arg); 368228103Smm if (ret == 0) 369228103Smm ret = err; 370228103Smm } 371228103Smm } 372228103Smm 373248571Smm free(buf); 374228103Smm return (ret); 375228103Smm} 376228103Smm 377228103Smm/* 378228103Smm * Iterate over all children, snapshots and filesystems 379228103Smm */ 380228103Smmint 381228103Smmzfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 382228103Smm{ 383228103Smm int ret; 384228103Smm 385228103Smm if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 386228103Smm return (ret); 387228103Smm 388230438Spjd return (zfs_iter_snapshots(zhp, B_FALSE, func, data)); 389228103Smm} 390228103Smm 391228103Smm 392228103Smmtypedef struct iter_stack_frame { 393228103Smm struct iter_stack_frame *next; 394228103Smm zfs_handle_t *zhp; 395228103Smm} iter_stack_frame_t; 396228103Smm 397228103Smmtypedef struct iter_dependents_arg { 398228103Smm boolean_t first; 399228103Smm boolean_t allowrecursion; 400228103Smm iter_stack_frame_t *stack; 401228103Smm zfs_iter_f func; 402228103Smm void *data; 403228103Smm} iter_dependents_arg_t; 404228103Smm 405228103Smmstatic int 406228103Smmiter_dependents_cb(zfs_handle_t *zhp, void *arg) 407228103Smm{ 408228103Smm iter_dependents_arg_t *ida = arg; 409228103Smm int err; 410228103Smm boolean_t first = ida->first; 411228103Smm ida->first = B_FALSE; 412228103Smm 413228103Smm if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 414228103Smm err = zfs_iter_clones(zhp, iter_dependents_cb, ida); 415228103Smm } else { 416228103Smm iter_stack_frame_t isf; 417228103Smm iter_stack_frame_t *f; 418228103Smm 419228103Smm /* 420228103Smm * check if there is a cycle by seeing if this fs is already 421228103Smm * on the stack. 422228103Smm */ 423228103Smm for (f = ida->stack; f != NULL; f = f->next) { 424228103Smm if (f->zhp->zfs_dmustats.dds_guid == 425228103Smm zhp->zfs_dmustats.dds_guid) { 426228103Smm if (ida->allowrecursion) { 427228103Smm zfs_close(zhp); 428228103Smm return (0); 429228103Smm } else { 430228103Smm zfs_error_aux(zhp->zfs_hdl, 431228103Smm dgettext(TEXT_DOMAIN, 432228103Smm "recursive dependency at '%s'"), 433228103Smm zfs_get_name(zhp)); 434228103Smm err = zfs_error(zhp->zfs_hdl, 435228103Smm EZFS_RECURSIVE, 436228103Smm dgettext(TEXT_DOMAIN, 437228103Smm "cannot determine dependent " 438228103Smm "datasets")); 439228103Smm zfs_close(zhp); 440228103Smm return (err); 441228103Smm } 442228103Smm } 443228103Smm } 444228103Smm 445228103Smm isf.zhp = zhp; 446228103Smm isf.next = ida->stack; 447228103Smm ida->stack = &isf; 448228103Smm err = zfs_iter_filesystems(zhp, iter_dependents_cb, ida); 449230438Spjd if (err == 0) { 450230438Spjd err = zfs_iter_snapshots(zhp, B_FALSE, 451230438Spjd iter_dependents_cb, ida); 452230438Spjd } 453228103Smm ida->stack = isf.next; 454228103Smm } 455228103Smm if (!first && err == 0) 456228103Smm err = ida->func(zhp, ida->data); 457228103Smm return (err); 458228103Smm} 459228103Smm 460228103Smmint 461228103Smmzfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 462228103Smm zfs_iter_f func, void *data) 463228103Smm{ 464228103Smm iter_dependents_arg_t ida; 465228103Smm ida.allowrecursion = allowrecursion; 466228103Smm ida.stack = NULL; 467228103Smm ida.func = func; 468228103Smm ida.data = data; 469228103Smm ida.first = B_TRUE; 470228103Smm return (iter_dependents_cb(zfs_handle_dup(zhp), &ida)); 471228103Smm} 472