3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * CDDL HEADER START
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The contents of this file are subject to the terms of the
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Common Development and Distribution License (the "License").
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * You may not use this file except in compliance with the License.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * or http://www.opensolaris.org/os/licensing.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * See the License for the specific language governing permissions
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * and limitations under the License.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * When distributing Covered Code, include this CDDL HEADER in each
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * If applicable, add the following below this CDDL HEADER, with the
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * fields enclosed by brackets "[]" replaced with your own identifying
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * CDDL HEADER END
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/zfs_context.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dsl_userhold.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dsl_dataset.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dsl_destroy.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dsl_synctask.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dmu_tx.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/zfs_onexit.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dsl_pool.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dsl_dir.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/zfs_ioctl.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/zap.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrenstypedef struct dsl_dataset_user_hold_arg {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *dduha_holds;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland nvlist_t *dduha_chkholds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *dduha_errlist;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens minor_t dduha_minor;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens} dsl_dataset_user_hold_arg_t;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * If you add new checks here, you may need to add additional checks to the
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * "temporary" case in snapshot_check() in dmu_objset.c.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_user_hold_check_one(dsl_dataset_t *ds, const char *htag,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens boolean_t temphold, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens objset_t *mos = dp->dp_meta_objset;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error = 0;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ASSERT(dsl_pool_config_held(dp));
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (strlen(htag) > MAXNAMELEN)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland return (SET_ERROR(E2BIG));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* Tempholds have a more restricted length */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (temphold && strlen(htag) + MAX_TAG_PREFIX_LEN >= MAXNAMELEN)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland return (SET_ERROR(E2BIG));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* tags must be unique (if ds already exists) */
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (ds != NULL && dsl_dataset_phys(ds)->ds_userrefs_obj != 0) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland uint64_t value;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs error = zap_lookup(mos, dsl_dataset_phys(ds)->ds_userrefs_obj,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland htag, 8, 1, &value);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (error == 0)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland error = SET_ERROR(EEXIST);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland else if (error == ENOENT)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland error = 0;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_user_hold_check(void *arg, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_user_hold_arg_t *dduha = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENOTSUP));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (!dmu_tx_is_syncing(tx))
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland return (0);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland for (nvpair_t *pair = nvlist_next_nvpair(dduha->dduha_holds, NULL);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland pair != NULL; pair = nvlist_next_nvpair(dduha->dduha_holds, pair)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland int error = 0;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland char *htag, *name;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* must be a snapshot */
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland name = nvpair_name(pair);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (strchr(name, '@') == NULL)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens error = SET_ERROR(EINVAL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error == 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = nvpair_value_string(pair, &htag);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (error == 0)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland error = dsl_dataset_hold(dp, name, FTAG, &ds);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error == 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_user_hold_check_one(ds, htag,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dduha->dduha_minor != 0, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (error == 0) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_add_string(dduha->dduha_chkholds, name, htag);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland } else {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland /*
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * We register ENOENT errors so they can be correctly
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * reported if needed, such as when all holds fail.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland */
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_add_int32(dduha->dduha_errlist, name, error);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (error != ENOENT)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlandstatic void
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlanddsl_dataset_user_hold_sync_one_impl(nvlist_t *tmpholds, dsl_dataset_t *ds,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland const char *htag, minor_t minor, uint64_t now, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens objset_t *mos = dp->dp_meta_objset;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t zapobj;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_userrefs_obj == 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * This is the first user hold for this dataset. Create
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * the userrefs zap object.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs zapobj = dsl_dataset_phys(ds)->ds_userrefs_obj =
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zap_create(mos, DMU_OT_USERREFS, DMU_OT_NONE, 0, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens } else {
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs zapobj = dsl_dataset_phys(ds)->ds_userrefs_obj;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ds->ds_userrefs++;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_add(mos, zapobj, htag, 8, 1, &now, tx));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (minor != 0) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland char name[MAXNAMELEN];
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland nvlist_t *tags;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_pool_user_hold(dp, ds->ds_object,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens htag, now, tx));
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland (void) snprintf(name, sizeof (name), "%llx",
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland (u_longlong_t)ds->ds_object);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (nvlist_lookup_nvlist(tmpholds, name, &tags) != 0) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland tags = fnvlist_alloc();
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_add_boolean(tags, htag);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_add_nvlist(tmpholds, name, tags);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_free(tags);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland } else {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_add_boolean(tags, htag);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_history_log_internal_ds(ds, "hold", tx,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens "tag=%s temp=%d refs=%llu",
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens htag, minor != 0, ds->ds_userrefs);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlandtypedef struct zfs_hold_cleanup_arg {
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char zhca_spaname[ZFS_MAX_DATASET_NAME_LEN];
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland uint64_t zhca_spa_load_guid;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland nvlist_t *zhca_holds;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland} zfs_hold_cleanup_arg_t;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlandstatic void
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlanddsl_dataset_user_release_onexit(void *arg)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland{
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland zfs_hold_cleanup_arg_t *ca = arg;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland spa_t *spa;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland int error;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland error = spa_open(ca->zhca_spaname, &spa, FTAG);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (error != 0) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland zfs_dbgmsg("couldn't release holds on pool=%s "
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland "because pool is no longer loaded",
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ca->zhca_spaname);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland return;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (spa_load_guid(spa) != ca->zhca_spa_load_guid) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland zfs_dbgmsg("couldn't release holds on pool=%s "
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland "because pool is no longer loaded (guid doesn't match)",
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ca->zhca_spaname);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland spa_close(spa, FTAG);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland return;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland (void) dsl_dataset_user_release_tmp(spa_get_dsl(spa), ca->zhca_holds);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_free(ca->zhca_holds);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland kmem_free(ca, sizeof (zfs_hold_cleanup_arg_t));
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland spa_close(spa, FTAG);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland}
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlandstatic void
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlanddsl_onexit_hold_cleanup(spa_t *spa, nvlist_t *holds, minor_t minor)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland{
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland zfs_hold_cleanup_arg_t *ca;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (minor == 0 || nvlist_empty(holds)) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_free(holds);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland return;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ASSERT(spa != NULL);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ca = kmem_alloc(sizeof (*ca), KM_SLEEP);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland (void) strlcpy(ca->zhca_spaname, spa_name(spa),
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland sizeof (ca->zhca_spaname));
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ca->zhca_spa_load_guid = spa_load_guid(spa);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ca->zhca_holds = holds;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland VERIFY0(zfs_onexit_add_cb(minor,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_dataset_user_release_onexit, ca, NULL));
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland}
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlandvoid
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlanddsl_dataset_user_hold_sync_one(dsl_dataset_t *ds, const char *htag,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland minor_t minor, uint64_t now, dmu_tx_t *tx)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland{
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland nvlist_t *tmpholds;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (minor != 0)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland tmpholds = fnvlist_alloc();
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland else
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland tmpholds = NULL;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_dataset_user_hold_sync_one_impl(tmpholds, ds, htag, minor, now, tx);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_onexit_hold_cleanup(dsl_dataset_get_spa(ds), tmpholds, minor);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland}
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_user_hold_sync(void *arg, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_user_hold_arg_t *dduha = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland nvlist_t *tmpholds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t now = gethrestime_sec();
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (dduha->dduha_minor != 0)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland tmpholds = fnvlist_alloc();
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland else
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland tmpholds = NULL;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland for (nvpair_t *pair = nvlist_next_nvpair(dduha->dduha_chkholds, NULL);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland pair != NULL;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland pair = nvlist_next_nvpair(dduha->dduha_chkholds, pair)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds));
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_dataset_user_hold_sync_one_impl(tmpholds, ds,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvpair_value_string(pair), dduha->dduha_minor, now, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_onexit_hold_cleanup(dp->dp_spa, tmpholds, dduha->dduha_minor);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * The full semantics of this function are described in the comment above
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * lzc_hold().
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * To summarize:
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * holds is nvl of snapname -> holdname
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * errlist will be filled in with snapname -> error
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * The snaphosts must all be in the same pool.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * Holds for snapshots that don't exist will be skipped.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * If none of the snapshots for requested holds exist then ENOENT will be
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * returned.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * If cleanup_minor is not 0, the holds will be temporary, which will be cleaned
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * up when the process exits.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * On success all the holds, for snapshots that existed, will be created and 0
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * will be returned.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * On failure no holds will be created, the errlist will be filled in,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * and an errno will returned.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * In all cases the errlist will contain entries for holds where the snapshot
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * didn't exist.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_user_hold(nvlist_t *holds, minor_t cleanup_minor, nvlist_t *errlist)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_user_hold_arg_t dduha;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvpair_t *pair;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland int ret;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens pair = nvlist_next_nvpair(holds, NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (pair == NULL)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dduha.dduha_holds = holds;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dduha.dduha_chkholds = fnvlist_alloc();
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dduha.dduha_errlist = errlist;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dduha.dduha_minor = cleanup_minor;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ret = dsl_sync_task(nvpair_name(pair), dsl_dataset_user_hold_check,
7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9eMatthew Ahrens dsl_dataset_user_hold_sync, &dduha,
7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9eMatthew Ahrens fnvlist_num_pairs(holds), ZFS_SPACE_CHECK_RESERVED);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_free(dduha.dduha_chkholds);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland return (ret);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlandtypedef int (dsl_holdfunc_t)(dsl_pool_t *dp, const char *name, void *tag,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_dataset_t **dsp);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrenstypedef struct dsl_dataset_user_release_arg {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_holdfunc_t *ddura_holdfunc;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *ddura_holds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *ddura_todelete;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *ddura_errlist;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland nvlist_t *ddura_chkholds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens} dsl_dataset_user_release_arg_t;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland/* Place a dataset hold on the snapshot identified by passed dsobj string */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic int
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlanddsl_dataset_hold_obj_string(dsl_pool_t *dp, const char *dsobj, void *tag,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_dataset_t **dsp)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland return (dsl_dataset_hold_obj(dp, strtonum(dsobj, NULL), tag, dsp));
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlandstatic int
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlanddsl_dataset_user_release_check_one(dsl_dataset_user_release_arg_t *ddura,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_dataset_t *ds, nvlist_t *holds, const char *snapname)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland{
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland uint64_t zapobj;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland nvlist_t *holds_found;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland objset_t *mos;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland int numholds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs if (!ds->ds_is_snapshot)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (nvlist_empty(holds))
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland return (0);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland numholds = 0;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland mos = ds->ds_dir->dd_pool->dp_meta_objset;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs zapobj = dsl_dataset_phys(ds)->ds_userrefs_obj;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland holds_found = fnvlist_alloc();
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland for (nvpair_t *pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens pair = nvlist_next_nvpair(holds, pair)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t tmp;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland int error;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland const char *holdname = nvpair_name(pair);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (zapobj != 0)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland error = zap_lookup(mos, zapobj, holdname, 8, 1, &tmp);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland else
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland error = SET_ERROR(ENOENT);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland /*
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * Non-existent holds are put on the errlist, but don't
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * cause an overall failure.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland */
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (error == ENOENT) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (ddura->ddura_errlist != NULL) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland char *errtag = kmem_asprintf("%s#%s",
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland snapname, holdname);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_add_int32(ddura->ddura_errlist, errtag,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ENOENT);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland strfree(errtag);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland continue;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (error != 0) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_free(holds_found);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_add_boolean(holds_found, holdname);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens numholds++;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (DS_IS_DEFER_DESTROY(ds) &&
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_num_children == 1 &&
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ds->ds_userrefs == numholds) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* we need to destroy the snapshot as well */
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (dsl_dataset_long_held(ds)) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_free(holds_found);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EBUSY));
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_add_boolean(ddura->ddura_todelete, snapname);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (numholds != 0) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_add_nvlist(ddura->ddura_chkholds, snapname,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland holds_found);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_free(holds_found);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_user_release_check(void *arg, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_dataset_user_release_arg_t *ddura;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_holdfunc_t *holdfunc;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_pool_t *dp;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (!dmu_tx_is_syncing(tx))
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dp = dmu_tx_pool(tx);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ddura = arg;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland holdfunc = ddura->ddura_holdfunc;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland for (nvpair_t *pair = nvlist_next_nvpair(ddura->ddura_holds, NULL);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland pair != NULL; pair = nvlist_next_nvpair(ddura->ddura_holds, pair)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *holds;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland const char *snapname = nvpair_name(pair);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = nvpair_value_nvlist(pair, &holds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland error = (SET_ERROR(EINVAL));
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland else
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland error = holdfunc(dp, snapname, FTAG, &ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error == 0) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland error = dsl_dataset_user_release_check_one(ddura, ds,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland holds, snapname);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ddura->ddura_errlist != NULL) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvlist_add_int32(ddura->ddura_errlist,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland snapname, error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland /*
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * Non-existent snapshots are put on the errlist,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * but don't cause an overall failure.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland */
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (error != ENOENT)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_user_release_sync_one(dsl_dataset_t *ds, nvlist_t *holds,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens objset_t *mos = dp->dp_meta_objset;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland for (nvpair_t *pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens pair = nvlist_next_nvpair(holds, pair)) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland int error;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland const char *holdname = nvpair_name(pair);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland /* Remove temporary hold if one exists. */
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland error = dsl_pool_user_release(dp, ds->ds_object, holdname, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY(error == 0 || error == ENOENT);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs VERIFY0(zap_remove(mos, dsl_dataset_phys(ds)->ds_userrefs_obj,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs holdname, tx));
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ds->ds_userrefs--;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_history_log_internal_ds(ds, "release", tx,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland "tag=%s refs=%lld", holdname, (longlong_t)ds->ds_userrefs);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_user_release_sync(void *arg, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_user_release_arg_t *ddura = arg;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_holdfunc_t *holdfunc = ddura->ddura_holdfunc;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland for (nvpair_t *pair = nvlist_next_nvpair(ddura->ddura_chkholds, NULL);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland pair != NULL; pair = nvlist_next_nvpair(ddura->ddura_chkholds,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland pair)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland const char *name = nvpair_name(pair);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland VERIFY0(holdfunc(dp, name, FTAG, &ds));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_user_release_sync_one(ds,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvpair_value_nvlist(pair), tx);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (nvlist_exists(ddura->ddura_todelete, name)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(ds->ds_userrefs == 0 &&
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_num_children == 1 &&
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens DS_IS_DEFER_DESTROY(ds));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_destroy_snapshot_sync_impl(ds, B_FALSE, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * The full semantics of this function are described in the comment above
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * lzc_release().
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * To summarize:
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * Releases holds specified in the nvl holds.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * holds is nvl of snapname -> { holdname, ... }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * errlist will be filled in with snapname -> error
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * If tmpdp is not NULL the names for holds should be the dsobj's of snapshots,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * otherwise they should be the names of shapshots.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * As a release may cause snapshots to be destroyed this trys to ensure they
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * aren't mounted.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * The release of non-existent holds are skipped.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * At least one hold must have been released for the this function to succeed
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * and return 0.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlandstatic int
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlanddsl_dataset_user_release_impl(nvlist_t *holds, nvlist_t *errlist,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_pool_t *tmpdp)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_user_release_arg_t ddura;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvpair_t *pair;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland char *pool;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens pair = nvlist_next_nvpair(holds, NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (pair == NULL)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland /*
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * The release may cause snapshots to be destroyed; make sure they
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * are not mounted.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland */
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (tmpdp != NULL) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland /* Temporary holds are specified by dsobj string. */
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ddura.ddura_holdfunc = dsl_dataset_hold_obj_string;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland pool = spa_name(tmpdp->dp_spa);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland#ifdef _KERNEL
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland pair = nvlist_next_nvpair(holds, pair)) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_dataset_t *ds;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
c50d56f667f119d78fa3d94d6bef2c298ba556f6Steven Hartland dsl_pool_config_enter(tmpdp, FTAG);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland error = dsl_dataset_hold_obj_string(tmpdp,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland nvpair_name(pair), FTAG, &ds);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (error == 0) {
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN];
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_dataset_name(ds, name);
c50d56f667f119d78fa3d94d6bef2c298ba556f6Steven Hartland dsl_pool_config_exit(tmpdp, FTAG);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_dataset_rele(ds, FTAG);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland (void) zfs_unmount_snap(name);
c50d56f667f119d78fa3d94d6bef2c298ba556f6Steven Hartland } else {
c50d56f667f119d78fa3d94d6bef2c298ba556f6Steven Hartland dsl_pool_config_exit(tmpdp, FTAG);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland#endif
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland } else {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland /* Non-temporary holds are specified by name. */
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ddura.ddura_holdfunc = dsl_dataset_hold;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland pool = nvpair_name(pair);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland#ifdef _KERNEL
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland pair = nvlist_next_nvpair(holds, pair)) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland (void) zfs_unmount_snap(nvpair_name(pair));
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland#endif
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddura.ddura_holds = holds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddura.ddura_errlist = errlist;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddura.ddura_todelete = fnvlist_alloc();
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ddura.ddura_chkholds = fnvlist_alloc();
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland error = dsl_sync_task(pool, dsl_dataset_user_release_check,
7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9eMatthew Ahrens dsl_dataset_user_release_sync, &ddura, 0, ZFS_SPACE_CHECK_NONE);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvlist_free(ddura.ddura_todelete);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_free(ddura.ddura_chkholds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * holds is nvl of snapname -> { holdname, ... }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * errlist will be filled in with snapname -> error
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlandint
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlanddsl_dataset_user_release(nvlist_t *holds, nvlist_t *errlist)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland return (dsl_dataset_user_release_impl(holds, errlist, NULL));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland/*
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * holds is nvl of snapdsobj -> { holdname, ... }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensvoid
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlanddsl_dataset_user_release_tmp(struct dsl_pool *dp, nvlist_t *holds)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ASSERT(dp != NULL);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland (void) dsl_dataset_user_release_impl(holds, NULL, dp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_get_holds(const char *dsname, nvlist_t *nvl)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int err;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = dsl_pool_hold(dsname, FTAG, &dp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (err);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = dsl_dataset_hold(dp, dsname, FTAG, &ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_rele(dp, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (err);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_userrefs_obj != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zap_attribute_t *za;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zap_cursor_t zc;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens for (zap_cursor_init(&zc, ds->ds_dir->dd_pool->dp_meta_objset,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_userrefs_obj);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zap_cursor_retrieve(&zc, za) == 0;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zap_cursor_advance(&zc)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvlist_add_uint64(nvl, za->za_name,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens za->za_first_integer);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zap_cursor_fini(&zc);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens kmem_free(za, sizeof (zap_attribute_t));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_rele(dp, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}