3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * CDDL HEADER START
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 * 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 * 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 * CDDL HEADER END
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 * 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 Ahrensdsl_dataset_user_hold_check_one(dsl_dataset_t *ds, const char *htag,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* Tempholds have a more restricted length */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (temphold && strlen(htag) + MAX_TAG_PREFIX_LEN >= MAXNAMELEN)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* tags must be unique (if ds already exists) */
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (ds != NULL && dsl_dataset_phys(ds)->ds_userrefs_obj != 0) {
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs error = zap_lookup(mos, dsl_dataset_phys(ds)->ds_userrefs_obj,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_user_hold_check(void *arg, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS)
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 /* must be a snapshot */
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland error = dsl_dataset_hold(dp, name, FTAG, &ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_user_hold_check_one(ds, htag,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_add_string(dduha->dduha_chkholds, name, htag);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * We register ENOENT errors so they can be correctly
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * reported if needed, such as when all holds fail.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_add_int32(dduha->dduha_errlist, name, error);
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)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_userrefs_obj == 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * This is the first user hold for this dataset. Create
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * the userrefs zap object.
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs zapobj = dsl_dataset_phys(ds)->ds_userrefs_obj =
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zap_create(mos, DMU_OT_USERREFS, DMU_OT_NONE, 0, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs zapobj = dsl_dataset_phys(ds)->ds_userrefs_obj;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_add(mos, zapobj, htag, 8, 1, &now, tx));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_pool_user_hold(dp, ds->ds_object,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland (void) snprintf(name, sizeof (name), "%llx",
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (nvlist_lookup_nvlist(tmpholds, name, &tags) != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens "tag=%s temp=%d refs=%llu",
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland error = spa_open(ca->zhca_spaname, &spa, FTAG);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland zfs_dbgmsg("couldn't release holds on pool=%s "
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland "because pool is no longer loaded",
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 (void) dsl_dataset_user_release_tmp(spa_get_dsl(spa), ca->zhca_holds);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland kmem_free(ca, sizeof (zfs_hold_cleanup_arg_t));
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlanddsl_onexit_hold_cleanup(spa_t *spa, nvlist_t *holds, minor_t minor)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland (void) strlcpy(ca->zhca_spaname, spa_name(spa),
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ca->zhca_spa_load_guid = spa_load_guid(spa);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_dataset_user_release_onexit, ca, NULL));
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlanddsl_dataset_user_hold_sync_one(dsl_dataset_t *ds, const char *htag,
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);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_user_hold_sync(void *arg, dmu_tx_t *tx)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland for (nvpair_t *pair = nvlist_next_nvpair(dduha->dduha_chkholds, NULL);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland pair = nvlist_next_nvpair(dduha->dduha_chkholds, pair)) {
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);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_onexit_hold_cleanup(dp->dp_spa, tmpholds, dduha->dduha_minor);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * The full semantics of this function are described in the comment above
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * lzc_hold().
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * To summarize:
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * holds is nvl of snapname -> holdname
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * errlist will be filled in with snapname -> error
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * The snaphosts must all be in the same pool.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * Holds for snapshots that don't exist will be skipped.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * If none of the snapshots for requested holds exist then ENOENT will be
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 * On success all the holds, for snapshots that existed, will be created and 0
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * will be returned.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * On failure no holds will be created, the errlist will be filled in,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * and an errno will returned.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * In all cases the errlist will contain entries for holds where the snapshot
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * didn't exist.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_user_hold(nvlist_t *holds, minor_t cleanup_minor, nvlist_t *errlist)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ret = dsl_sync_task(nvpair_name(pair), dsl_dataset_user_hold_check,
7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9eMatthew Ahrens fnvlist_num_pairs(holds), ZFS_SPACE_CHECK_RESERVED);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlandtypedef int (dsl_holdfunc_t)(dsl_pool_t *dp, const char *name, void *tag,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland/* Place a dataset hold on the snapshot identified by passed dsobj string */
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlanddsl_dataset_hold_obj_string(dsl_pool_t *dp, const char *dsobj, void *tag,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland return (dsl_dataset_hold_obj(dp, strtonum(dsobj, NULL), tag, dsp));
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)
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs zapobj = dsl_dataset_phys(ds)->ds_userrefs_obj;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland for (nvpair_t *pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland error = zap_lookup(mos, zapobj, holdname, 8, 1, &tmp);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * Non-existent holds are put on the errlist, but don't
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * cause an overall failure.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_add_int32(ddura->ddura_errlist, errtag,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_num_children == 1 &&
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* we need to destroy the snapshot as well */
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_add_boolean(ddura->ddura_todelete, snapname);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_add_nvlist(ddura->ddura_chkholds, snapname,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_user_release_check(void *arg, dmu_tx_t *tx)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
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)) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland error = dsl_dataset_user_release_check_one(ddura, ds,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * Non-existent snapshots are put on the errlist,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * but don't cause an overall failure.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_user_release_sync_one(dsl_dataset_t *ds, nvlist_t *holds,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland for (nvpair_t *pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland /* Remove temporary hold if one exists. */
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland error = dsl_pool_user_release(dp, ds->ds_object, holdname, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs VERIFY0(zap_remove(mos, dsl_dataset_phys(ds)->ds_userrefs_obj,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_history_log_internal_ds(ds, "release", tx,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland "tag=%s refs=%lld", holdname, (longlong_t)ds->ds_userrefs);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_user_release_sync(void *arg, dmu_tx_t *tx)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_holdfunc_t *holdfunc = ddura->ddura_holdfunc;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
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 if (nvlist_exists(ddura->ddura_todelete, name)) {
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_num_children == 1 &&
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_destroy_snapshot_sync_impl(ds, B_FALSE, tx);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * The full semantics of this function are described in the comment above
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * lzc_release().
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * To summarize:
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * Releases holds specified in the nvl holds.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * holds is nvl of snapname -> { holdname, ... }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * errlist will be filled in with snapname -> error
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 * As a release may cause snapshots to be destroyed this trys to ensure they
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * aren't mounted.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * The release of non-existent holds are skipped.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * At least one hold must have been released for the this function to succeed
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * and return 0.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlanddsl_dataset_user_release_impl(nvlist_t *holds, nvlist_t *errlist,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * The release may cause snapshots to be destroyed; make sure they
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * are not mounted.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland /* Temporary holds are specified by dsobj string. */
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland ddura.ddura_holdfunc = dsl_dataset_hold_obj_string;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland /* Non-temporary holds are specified by name. */
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
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);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * holds is nvl of snapname -> { holdname, ... }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * errlist will be filled in with snapname -> error
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlanddsl_dataset_user_release(nvlist_t *holds, nvlist_t *errlist)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland return (dsl_dataset_user_release_impl(holds, errlist, NULL));
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * holds is nvl of snapdsobj -> { holdname, ... }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlanddsl_dataset_user_release_tmp(struct dsl_pool *dp, nvlist_t *holds)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland (void) dsl_dataset_user_release_impl(holds, NULL, dp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_get_holds(const char *dsname, nvlist_t *nvl)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = dsl_dataset_hold(dp, dsname, FTAG, &ds);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_userrefs_obj != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);