2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/*
2N/A * System includes
2N/A */
2N/A#include <assert.h>
2N/A#include <libintl.h>
2N/A#include <libnvpair.h>
2N/A#include <libzfs.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <sys/types.h>
2N/A#include <sys/stat.h>
2N/A#include <unistd.h>
2N/A
2N/A#include <libbe.h>
2N/A#include <libbe_priv.h>
2N/A
2N/A/* Private function prototypes */
2N/Astatic int be_rollback_check_callback(zfs_handle_t *, void *);
2N/Astatic int be_rollback_callback(zfs_handle_t *, void *);
2N/A
2N/A
2N/A/* ******************************************************************** */
2N/A/* Public Functions */
2N/A/* ******************************************************************** */
2N/A
2N/A/*
2N/A * Function: be_create_snapshot
2N/A * Description: Creates a recursive snapshot of all the datasets within a BE.
2N/A * If the name of the BE to snapshot is not provided, it assumes
2N/A * we're snapshotting the currently running BE. If the snapshot
2N/A * name is not provided it creates an auto named snapshot, which
2N/A * will be returned to the caller upon success.
2N/A * Parameters:
2N/A * be_attrs - pointer to nvlist_t of attributes being passed in.
2N/A * The following attributes are used by this function:
2N/A *
2N/A * BE_ATTR_ORIG_BE_NAME *optional
2N/A * BE_ATTR_ALT_POOL *optional
2N/A * BE_ATTR_SNAP_NAME *optional
2N/A * BE_ATTR_POLICY *optional
2N/A *
2N/A * If the BE_ATTR_SNAP_NAME was not passed in, upon
2N/A * successful BE snapshot creation, the following
2N/A * attribute value will be returned to the caller by
2N/A * setting it in the be_attrs parameter passed in:
2N/A *
2N/A * BE_ATTR_SNAP_NAME
2N/A *
2N/A * Return:
2N/A * BE_SUCCESS - Success
2N/A * be_errno_t - Failure
2N/A * Scope:
2N/A * Public
2N/A */
2N/Aint
2N/Abe_create_snapshot(nvlist_t *be_attrs)
2N/A{
2N/A char *be_name = NULL;
2N/A char *altpool = NULL;
2N/A char *snap_name = NULL;
2N/A char *policy = NULL;
2N/A boolean_t autoname = B_FALSE;
2N/A int ret = BE_SUCCESS;
2N/A
2N/A if (getzoneid() != GLOBAL_ZONEID) {
2N/A /*
2N/A * Check to see if we have write access to the root filesystem
2N/A */
2N/A ret = be_check_rozr();
2N/A if (ret != BE_SUCCESS)
2N/A return (ret);
2N/A }
2N/A
2N/A /* Initialize libzfs handle */
2N/A if (!be_zfs_init())
2N/A return (BE_ERR_INIT);
2N/A
2N/A /* Get original BE name if one was provided */
2N/A if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK,
2N/A BE_ATTR_ORIG_BE_NAME, DATA_TYPE_STRING, &be_name, NULL) != 0) {
2N/A be_print_err(gettext("be_create_snapshot: failed to "
2N/A "lookup BE_ATTR_ORIG_BE_NAME attribute\n"));
2N/A be_zfs_fini();
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A /* Validate original BE name if one was provided */
2N/A if (be_name != NULL && !be_valid_be_name(be_name)) {
2N/A be_print_err(gettext("be_create_snapshot: "
2N/A "invalid BE name %s\n"), be_name);
2N/A be_zfs_fini();
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A /* Get alternate "pool" */
2N/A if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK,
2N/A BE_ATTR_ALT_POOL, DATA_TYPE_STRING, &altpool, NULL) != 0) {
2N/A be_print_err(gettext("be_create_snapshot: failed to lookup "
2N/A "BE_ATTR_ALT_POOL attribute\n"));
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A /* Get snapshot name to create if one was provided */
2N/A if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK,
2N/A BE_ATTR_SNAP_NAME, DATA_TYPE_STRING, &snap_name, NULL) != 0) {
2N/A be_print_err(gettext("be_create_snapshot: "
2N/A "failed to lookup BE_ATTR_SNAP_NAME attribute\n"));
2N/A be_zfs_fini();
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A /* Get BE policy to create this snapshot under */
2N/A if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK,
2N/A BE_ATTR_POLICY, DATA_TYPE_STRING, &policy, NULL) != 0) {
2N/A be_print_err(gettext("be_create_snapshot: "
2N/A "failed to lookup BE_ATTR_POLICY attribute\n"));
2N/A be_zfs_fini();
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A /*
2N/A * If no snap_name ws provided, we're going to create an
2N/A * auto named snapshot. Set flag so that we know to pass
2N/A * the auto named snapshot to the caller later.
2N/A */
2N/A if (snap_name == NULL)
2N/A autoname = B_TRUE;
2N/A
2N/A if ((ret = _be_create_snapshot(be_name, altpool, &snap_name, policy))
2N/A == BE_SUCCESS) {
2N/A if (autoname == B_TRUE) {
2N/A /*
2N/A * Set auto named snapshot name in the
2N/A * nvlist passed in by the caller.
2N/A */
2N/A if (nvlist_add_string(be_attrs, BE_ATTR_SNAP_NAME,
2N/A snap_name) != 0) {
2N/A be_print_err(gettext("be_create_snapshot: "
2N/A "failed to add auto snap name (%s) to "
2N/A "be_attrs\n"), snap_name);
2N/A ret = BE_ERR_NOMEM;
2N/A }
2N/A }
2N/A }
2N/A
2N/A be_zfs_fini();
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_destroy_snapshot
2N/A * Description: Iterates through all the datasets of the BE and deletes
2N/A * the snapshots of each one with the specified name. If the
2N/A * BE name is not provided, it assumes we're operating on the
2N/A * currently running BE. The name of the snapshot name to
2N/A * destroy must be provided.
2N/A * Parameters:
2N/A * be_attrs - pointer to nvlist_t of attributes being passed in.
2N/A * The following attribute values are used by this
2N/A * function:
2N/A *
2N/A * BE_ATTR_ORIG_BE_NAME *optional
2N/A * BE_ATTR_SNAP_NAME *required
2N/A * Return:
2N/A * BE_SUCCESS - Success
2N/A * be_errno_t - Failure
2N/A * Scope:
2N/A * Public
2N/A */
2N/Aint
2N/Abe_destroy_snapshot(nvlist_t *be_attrs)
2N/A{
2N/A char *be_name = NULL;
2N/A char *snap_name = NULL;
2N/A int ret = BE_SUCCESS;
2N/A
2N/A if (getzoneid() != GLOBAL_ZONEID) {
2N/A /*
2N/A * Check to see if we have write access to the root filesystem
2N/A */
2N/A ret = be_check_rozr();
2N/A if (ret != BE_SUCCESS)
2N/A return (ret);
2N/A }
2N/A
2N/A /* Initialize libzfs handle */
2N/A if (!be_zfs_init())
2N/A return (BE_ERR_INIT);
2N/A
2N/A /* Get original BE name if one was provided */
2N/A if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK,
2N/A BE_ATTR_ORIG_BE_NAME, DATA_TYPE_STRING, &be_name, NULL) != 0) {
2N/A be_print_err(gettext("be_destroy_snapshot: "
2N/A "failed to lookup BE_ATTR_ORIG_BE_NAME attribute\n"));
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A /* Validate original BE name if one was provided */
2N/A if (be_name != NULL && !be_valid_be_name(be_name)) {
2N/A be_print_err(gettext("be_destroy_snapshot: "
2N/A "invalid BE name %s\n"), be_name);
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A /* Get snapshot name to destroy */
2N/A if (nvlist_lookup_string(be_attrs, BE_ATTR_SNAP_NAME, &snap_name)
2N/A != 0) {
2N/A be_print_err(gettext("be_destroy_snapshot: "
2N/A "failed to lookup BE_ATTR_SNAP_NAME attribute.\n"));
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A ret = _be_destroy_snapshot(be_name, snap_name);
2N/A
2N/A be_zfs_fini();
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_rollback
2N/A * Description: Rolls back a BE and all of its children datasets to the
2N/A * named snapshot. All of the BE's datasets must have the
2N/A * named snapshot for this function to succeed. If the name
2N/A * of the BE is not passed in, this function assumes we're
2N/A * operating on the currently booted live BE.
2N/A *
2N/A * Note - This function does not check if the BE has any
2N/A * younger snapshots than the one we're trying to rollback to.
2N/A * If it does, then those younger snapshots and their dependent
2N/A * clone file systems will get destroyed in the process of
2N/A * rolling back.
2N/A *
2N/A * Parameters:
2N/A * be_attrs - pointer to nvlist_t of attributes being passed in.
2N/A * The following attributes are used by this function:
2N/A *
2N/A * BE_ATTR_ORIG_BE_NAME *optional
2N/A * BE_ATTR_SNAP_NAME *required
2N/A *
2N/A * Returns:
2N/A * BE_SUCCESS - Success
2N/A * be_errno_t - Failure
2N/A * Scope:
2N/A * Public
2N/A */
2N/Aint
2N/Abe_rollback(nvlist_t *be_attrs)
2N/A{
2N/A be_transaction_data_t bt = { 0 };
2N/A zfs_handle_t *zhp = NULL;
2N/A char obe_root_ds[MAXPATHLEN];
2N/A int zret = 0, ret = BE_SUCCESS;
2N/A
2N/A if (getzoneid() != GLOBAL_ZONEID) {
2N/A /*
2N/A * Check to see if we have write access to the root filesystem
2N/A */
2N/A ret = be_check_rozr();
2N/A if (ret != BE_SUCCESS)
2N/A return (ret);
2N/A }
2N/A
2N/A /* Initialize libzfs handle */
2N/A if (!be_zfs_init())
2N/A return (BE_ERR_INIT);
2N/A
2N/A /* Get original BE name if one was provided */
2N/A if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK,
2N/A BE_ATTR_ORIG_BE_NAME, DATA_TYPE_STRING, &bt.obe_name, NULL) != 0) {
2N/A be_print_err(gettext("be_rollback: "
2N/A "failed to lookup BE_ATTR_ORIG_BE_NAME attribute\n"));
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A /* If original BE name not provided, use current BE */
2N/A if (bt.obe_name == NULL) {
2N/A if ((ret = be_find_current_be(&bt)) != BE_SUCCESS) {
2N/A return (ret);
2N/A }
2N/A } else {
2N/A /* Validate original BE name */
2N/A if (!be_valid_be_name(bt.obe_name)) {
2N/A be_print_err(gettext("be_rollback: "
2N/A "invalid BE name %s\n"), bt.obe_name);
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A }
2N/A
2N/A /* Get snapshot name to rollback to */
2N/A if (nvlist_lookup_string(be_attrs, BE_ATTR_SNAP_NAME, &bt.obe_snap_name)
2N/A != 0) {
2N/A be_print_err(gettext("be_rollback: "
2N/A "failed to lookup BE_ATTR_SNAP_NAME attribute.\n"));
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A /* Find which zpool obe_name lives in */
2N/A if ((zret = zpool_iter(g_zfs, be_find_zpool_callback, &bt)) == 0) {
2N/A be_print_err(gettext("be_rollback: "
2N/A "failed to find zpool for BE (%s)\n"), bt.obe_name);
2N/A return (BE_ERR_BE_NOENT);
2N/A } else if (zret < 0) {
2N/A be_print_err(gettext("be_rollback: "
2N/A "zpool_iter failed: %s\n"),
2N/A libzfs_error_description(g_zfs));
2N/A return (zfs_err_to_be_err(g_zfs));
2N/A }
2N/A
2N/A /* Generate string for BE's root dataset */
2N/A be_make_root_ds(bt.obe_zpool, bt.obe_name, obe_root_ds,
2N/A sizeof (obe_root_ds));
2N/A bt.obe_root_ds = obe_root_ds;
2N/A
2N/A /* Get handle to BE's root dataset */
2N/A if ((zhp = zfs_open(g_zfs, bt.obe_root_ds, ZFS_TYPE_DATASET)) == NULL) {
2N/A be_print_err(gettext("be_rollback: "
2N/A "failed to open BE root dataset (%s): %s\n"),
2N/A bt.obe_root_ds, libzfs_error_description(g_zfs));
2N/A return (zfs_err_to_be_err(g_zfs));
2N/A }
2N/A
2N/A /*
2N/A * Check that snapshot name exists for this BE and all of its
2N/A * children file systems. This call will end up closing the
2N/A * zfs handle passed in whether it succeeds or fails.
2N/A */
2N/A if ((ret = be_rollback_check_callback(zhp, bt.obe_snap_name)) != 0) {
2N/A zhp = NULL;
2N/A return (ret);
2N/A }
2N/A
2N/A /* Get handle to BE's root dataset */
2N/A if ((zhp = zfs_open(g_zfs, bt.obe_root_ds, ZFS_TYPE_DATASET)) == NULL) {
2N/A be_print_err(gettext("be_rollback: "
2N/A "failed to open BE root dataset (%s): %s\n"),
2N/A bt.obe_root_ds, libzfs_error_description(g_zfs));
2N/A return (zfs_err_to_be_err(g_zfs));
2N/A }
2N/A
2N/A /*
2N/A * Iterate through a BE's datasets and roll them all back to
2N/A * the specified snapshot. This call will end up closing the
2N/A * zfs handle passed in whether it succeeds or fails.
2N/A */
2N/A if ((ret = be_rollback_callback(zhp, bt.obe_snap_name)) != 0) {
2N/A zhp = NULL;
2N/A be_print_err(gettext("be_rollback: "
2N/A "failed to rollback BE %s to %s\n"), bt.obe_name,
2N/A bt.obe_snap_name);
2N/A return (ret);
2N/A }
2N/A zhp = NULL;
2N/A be_zfs_fini();
2N/A return (BE_SUCCESS);
2N/A}
2N/A
2N/A
2N/A/* ******************************************************************** */
2N/A/* Semi-Private Functions */
2N/A/* ******************************************************************** */
2N/A
2N/A/*
2N/A * Function: _be_create_snapshot
2N/A * Description: see be_create_snapshot
2N/A * Parameters:
2N/A * be_name - The name of the BE that we're taking a snapshot of.
2N/A * altpool - Pointer to alternate "pool" area to find the BE.
2N/A * snap_name - The name of the snapshot we're creating. If
2N/A * snap_name is NULL an auto generated name will be used,
2N/A * and upon success, will return that name via this
2N/A * reference pointer. The caller is responsible for
2N/A * freeing the returned name.
2N/A * policy - The clean-up policy type. (library wide use only)
2N/A * Return:
2N/A * BE_SUCCESS - Success
2N/A * be_errno_t - Failure
2N/A * Scope:
2N/A * Semi-private (library wide use only)
2N/A */
2N/Aint
2N/A_be_create_snapshot(char *be_name, char *altpool, char **snap_name,
2N/A char *policy)
2N/A{
2N/A be_transaction_data_t bt = { 0 };
2N/A zfs_handle_t *zhp = NULL;
2N/A nvlist_t *ss_props = NULL;
2N/A char ss[MAXPATHLEN];
2N/A char root_ds[MAXPATHLEN];
2N/A int pool_version = 0;
2N/A int i = 0;
2N/A int zret = 0, ret = BE_SUCCESS;
2N/A boolean_t autoname = B_FALSE;
2N/A boolean_t in_ngz = B_FALSE;
2N/A
2N/A /*
2N/A * Check to see if we're operating inside a Solaris Container
2N/A * or the Global Zone.
2N/A */
2N/A if (getzoneid() != GLOBAL_ZONEID)
2N/A in_ngz = B_TRUE;
2N/A
2N/A /* Set parameters in bt structure */
2N/A bt.obe_name = be_name;
2N/A bt.obe_snap_name = *snap_name;
2N/A bt.policy = policy;
2N/A
2N/A /* If original BE name not supplied, use current BE */
2N/A if (bt.obe_name == NULL) {
2N/A if ((ret = be_find_current_be(&bt)) != BE_SUCCESS) {
2N/A return (ret);
2N/A }
2N/A }
2N/A
2N/A /* Find which zpool obe_name lives in */
2N/A if (altpool == NULL) {
2N/A if ((zret = zpool_iter(g_zfs, be_find_zpool_callback, &bt))
2N/A == 0) {
2N/A be_print_err(gettext("be_create_snapshot: failed to "
2N/A "find zpool for BE (%s)\n"), bt.obe_name);
2N/A return (BE_ERR_BE_NOENT);
2N/A } else if (zret < 0) {
2N/A be_print_err(gettext("be_create_snapshot: "
2N/A "zpool_iter failed: %s\n"),
2N/A libzfs_error_description(g_zfs));
2N/A return (zfs_err_to_be_err(g_zfs));
2N/A }
2N/A } else {
2N/A bt.obe_zpool = altpool;
2N/A }
2N/A
2N/A be_make_root_ds(bt.obe_zpool, bt.obe_name, root_ds,
2N/A sizeof (root_ds));
2N/A bt.obe_root_ds = root_ds;
2N/A
2N/A /*
2N/A * Check to see if this is an attempt to create a BE
2N/A * snapshot of a zone BE that is not associated with the
2N/A * currently active global zone.
2N/A */
2N/A if (in_ngz) {
2N/A if (!be_zone_is_bootable(bt.obe_root_ds)) {
2N/A be_print_err(gettext("be_create_snapshot: Operation "
2N/A "not supported on unbootable BE\n"));
2N/A return (BE_ERR_ZONE_NOTSUP);
2N/A }
2N/A }
2N/A
2N/A /* If BE policy not specified, use the default policy */
2N/A if (bt.policy == NULL) {
2N/A bt.policy = be_default_policy();
2N/A } else {
2N/A /* Validate policy type */
2N/A if (!valid_be_policy(bt.policy)) {
2N/A be_print_err(gettext("be_create_snapshot: "
2N/A "invalid BE policy type (%s)\n"), bt.policy);
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * If snapshot name not specified, set auto name flag and
2N/A * generate auto snapshot name.
2N/A */
2N/A if (bt.obe_snap_name == NULL) {
2N/A autoname = B_TRUE;
2N/A if ((bt.obe_snap_name = be_auto_snap_name())
2N/A == NULL) {
2N/A be_print_err(gettext("be_create_snapshot: "
2N/A "failed to create auto snapshot name\n"));
2N/A ret = BE_ERR_AUTONAME;
2N/A goto done;
2N/A }
2N/A }
2N/A
2N/A /* Generate the name of the snapshot to take. */
2N/A (void) snprintf(ss, sizeof (ss), "%s@%s", bt.obe_root_ds,
2N/A bt.obe_snap_name);
2N/A
2N/A /* Get handle to BE's root dataset */
2N/A if ((zhp = zfs_open(g_zfs, bt.obe_root_ds, ZFS_TYPE_DATASET))
2N/A == NULL) {
2N/A be_print_err(gettext("be_create_snapshot: "
2N/A "failed to open BE root dataset (%s): %s\n"),
2N/A bt.obe_root_ds, libzfs_error_description(g_zfs));
2N/A ret = zfs_err_to_be_err(g_zfs);
2N/A goto done;
2N/A }
2N/A
2N/A /* Get the ZFS pool version of the pool where this dataset resides */
2N/A if (zfs_spa_version(zhp, &pool_version) != 0) {
2N/A be_print_err(gettext("be_create_snapshot: failed to "
2N/A "get ZFS pool version for %s: %s\n"), zfs_get_name(zhp),
2N/A libzfs_error_description(g_zfs));
2N/A }
2N/A
2N/A if (!in_ngz) {
2N/A /*
2N/A * If ZFS pool version supports snapshot user properties, store
2N/A * cleanup policy there. Otherwise don't set one - this
2N/A * snapshot will always inherit the cleanup policy from
2N/A * its parent.
2N/A */
2N/A if (pool_version >= SPA_VERSION_SNAP_PROPS) {
2N/A if (nvlist_alloc(&ss_props, NV_UNIQUE_NAME, 0) != 0) {
2N/A be_print_err(gettext("be_create_snapshot: "
2N/A "internal error: out of memory\n"));
2N/A return (BE_ERR_NOMEM);
2N/A }
2N/A if (nvlist_add_string(ss_props, BE_POLICY_PROPERTY,
2N/A bt.policy) != 0) {
2N/A be_print_err(gettext("be_create_snapshot: "
2N/A "internal error: out of memory\n"));
2N/A nvlist_free(ss_props);
2N/A return (BE_ERR_NOMEM);
2N/A }
2N/A } else if (policy != NULL) {
2N/A /*
2N/A * If an explicit cleanup policy was requested
2N/A * by the caller and we don't support it, error out.
2N/A */
2N/A be_print_err(gettext("be_create_snapshot: cannot set "
2N/A "cleanup policy: ZFS pool version is %d\n"),
2N/A pool_version);
2N/A return (BE_ERR_NOTSUP);
2N/A }
2N/A }
2N/A
2N/A /* Create the snapshots recursively */
2N/A if (zfs_snapshot(g_zfs, ss, B_TRUE, ss_props) != 0) {
2N/A if (!autoname || libzfs_errno(g_zfs) != EZFS_EXISTS) {
2N/A be_print_err(gettext("be_create_snapshot: "
2N/A "recursive snapshot of %s failed: %s\n"),
2N/A ss, libzfs_error_description(g_zfs));
2N/A
2N/A if (libzfs_errno(g_zfs) == EZFS_EXISTS)
2N/A ret = BE_ERR_SS_EXISTS;
2N/A else
2N/A ret = zfs_err_to_be_err(g_zfs);
2N/A
2N/A goto done;
2N/A } else {
2N/A for (i = 1; i < BE_AUTO_NAME_MAX_TRY; i++) {
2N/A
2N/A /* Sleep 1 before retrying */
2N/A (void) sleep(1);
2N/A
2N/A /* Generate new auto snapshot name. */
2N/A free(bt.obe_snap_name);
2N/A if ((bt.obe_snap_name =
2N/A be_auto_snap_name()) == NULL) {
2N/A be_print_err(gettext(
2N/A "be_create_snapshot: failed to "
2N/A "create auto snapshot name\n"));
2N/A ret = BE_ERR_AUTONAME;
2N/A goto done;
2N/A }
2N/A
2N/A /* Generate string of the snapshot to take. */
2N/A (void) snprintf(ss, sizeof (ss), "%s@%s",
2N/A bt.obe_root_ds, bt.obe_snap_name);
2N/A
2N/A /* Create the snapshots recursively */
2N/A if (zfs_snapshot(g_zfs, ss, B_TRUE, ss_props)
2N/A != 0) {
2N/A if (libzfs_errno(g_zfs) !=
2N/A EZFS_EXISTS) {
2N/A be_print_err(gettext(
2N/A "be_create_snapshot: "
2N/A "recursive snapshot of %s "
2N/A "failed: %s\n"), ss,
2N/A libzfs_error_description(
2N/A g_zfs));
2N/A ret = zfs_err_to_be_err(g_zfs);
2N/A goto done;
2N/A }
2N/A } else {
2N/A break;
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * If we exhausted the maximum number of tries,
2N/A * free the auto snap name and set error.
2N/A */
2N/A if (i == BE_AUTO_NAME_MAX_TRY) {
2N/A be_print_err(gettext("be_create_snapshot: "
2N/A "failed to create unique auto snapshot "
2N/A "name\n"));
2N/A free(bt.obe_snap_name);
2N/A bt.obe_snap_name = NULL;
2N/A ret = BE_ERR_AUTONAME;
2N/A }
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * If we succeeded in creating an auto named snapshot, store
2N/A * the name in the nvlist passed in by the caller.
2N/A */
2N/A if (autoname && bt.obe_snap_name) {
2N/A *snap_name = bt.obe_snap_name;
2N/A }
2N/A
2N/Adone:
2N/A ZFS_CLOSE(zhp);
2N/A
2N/A if (ss_props != NULL)
2N/A nvlist_free(ss_props);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Function: _be_destroy_snapshot
2N/A * Description: see be_destroy_snapshot
2N/A * Parameters:
2N/A * be_name - The name of the BE that the snapshot belongs to.
2N/A * snap_name - The name of the snapshot we're destroying.
2N/A * Return:
2N/A * BE_SUCCESS - Success
2N/A * be_errno_t - Failure
2N/A * Scope:
2N/A * Semi-private (library wide use only)
2N/A */
2N/Aint
2N/A_be_destroy_snapshot(char *be_name, char *snap_name)
2N/A{
2N/A be_transaction_data_t bt = { 0 };
2N/A zfs_handle_t *zhp;
2N/A char ss[MAXPATHLEN];
2N/A char root_ds[MAXPATHLEN];
2N/A int err = BE_SUCCESS, ret = BE_SUCCESS;
2N/A
2N/A /* Make sure we actaully have a snapshot name */
2N/A if (snap_name == NULL) {
2N/A be_print_err(gettext("be_destroy_snapshot: "
2N/A "invalid snapshot name\n"));
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A /* Set parameters in bt structure */
2N/A bt.obe_name = be_name;
2N/A bt.obe_snap_name = snap_name;
2N/A
2N/A /* If original BE name not supplied, use current BE */
2N/A if (bt.obe_name == NULL) {
2N/A if ((err = be_find_current_be(&bt)) != BE_SUCCESS) {
2N/A return (err);
2N/A }
2N/A }
2N/A
2N/A /* Find which zpool be_name lives in */
2N/A if ((ret = zpool_iter(g_zfs, be_find_zpool_callback, &bt)) == 0) {
2N/A be_print_err(gettext("be_destroy_snapshot: "
2N/A "failed to find zpool for BE (%s)\n"), bt.obe_name);
2N/A return (BE_ERR_BE_NOENT);
2N/A } else if (ret < 0) {
2N/A be_print_err(gettext("be_destroy_snapshot: "
2N/A "zpool_iter failed: %s\n"),
2N/A libzfs_error_description(g_zfs));
2N/A return (zfs_err_to_be_err(g_zfs));
2N/A }
2N/A
2N/A be_make_root_ds(bt.obe_zpool, bt.obe_name, root_ds, sizeof (root_ds));
2N/A bt.obe_root_ds = root_ds;
2N/A
2N/A /*
2N/A * Detect if the BE snapshot to destroy has the 'active on boot'
2N/A * property set.
2N/A */
2N/A if (getzoneid() != GLOBAL_ZONEID) {
2N/A if (!be_zone_is_bootable(bt.obe_root_ds)) {
2N/A be_print_err(gettext("be_destroy_snapshot: Operation "
2N/A "not supported on unbootable BE\n"));
2N/A return (BE_ERR_NOTSUP);
2N/A }
2N/A }
2N/A
2N/A zhp = zfs_open(g_zfs, bt.obe_root_ds, ZFS_TYPE_DATASET);
2N/A if (zhp == NULL) {
2N/A /*
2N/A * The zfs_open failed, return an error.
2N/A */
2N/A be_print_err(gettext("be_destroy_snapshot: "
2N/A "failed to open BE root dataset (%s): %s\n"),
2N/A bt.obe_root_ds, libzfs_error_description(g_zfs));
2N/A err = zfs_err_to_be_err(g_zfs);
2N/A } else {
2N/A /*
2N/A * Generate the name of the snapshot to take.
2N/A */
2N/A (void) snprintf(ss, sizeof (ss), "%s@%s", bt.obe_name,
2N/A bt.obe_snap_name);
2N/A
2N/A /*
2N/A * destroy the snapshot.
2N/A */
2N/A /*
2N/A * The boolean set to B_FALSE and passed to zfs_destroy_snaps()
2N/A * tells zfs to process and destroy the snapshots now.
2N/A * Otherwise the call will potentially return where the
2N/A * snapshot isn't actually destroyed yet, and ZFS is waiting
2N/A * until all the references to the snapshot have been
2N/A * released before actually destroying the snapshot.
2N/A */
2N/A if (zfs_destroy_snaps(zhp, bt.obe_snap_name, B_FALSE) != 0) {
2N/A err = zfs_err_to_be_err(g_zfs);
2N/A be_print_err(gettext("be_destroy_snapshot: "
2N/A "failed to destroy snapshot %s: %s\n"), ss,
2N/A libzfs_error_description(g_zfs));
2N/A }
2N/A }
2N/A
2N/A ZFS_CLOSE(zhp);
2N/A
2N/A return (err);
2N/A}
2N/A
2N/A/* ******************************************************************** */
2N/A/* Private Functions */
2N/A/* ******************************************************************** */
2N/A
2N/A/*
2N/A * Function: be_rollback_check_callback
2N/A * Description: Callback function used to iterate through a BE's filesystems
2N/A * to check if a given snapshot name exists.
2N/A * Parameters:
2N/A * zhp - zfs_handle_t pointer to filesystem being processed.
2N/A * data - name of the snapshot to check for.
2N/A * Returns:
2N/A * 0 - Success, snapshot name exists for all filesystems.
2N/A * be_errno_t - Failure, snapshot name does not exist for all
2N/A * filesystems.
2N/A * Scope:
2N/A * Private
2N/A */
2N/Astatic int
2N/Abe_rollback_check_callback(zfs_handle_t *zhp, void *data)
2N/A{
2N/A char *snap_name = data;
2N/A char ss[MAXPATHLEN];
2N/A int ret = BE_SUCCESS;
2N/A
2N/A /* Generate string for this filesystem's snapshot name */
2N/A (void) snprintf(ss, sizeof (ss), "%s@%s", zfs_get_name(zhp), snap_name);
2N/A
2N/A /* Check if snapshot exists */
2N/A if (!zfs_dataset_exists(g_zfs, ss, ZFS_TYPE_SNAPSHOT)) {
2N/A be_print_err(gettext("be_rollback_check_callback: "
2N/A "snapshot does not exist %s\n"), ss);
2N/A ZFS_CLOSE(zhp);
2N/A return (BE_ERR_SS_NOENT);
2N/A }
2N/A
2N/A /* Iterate this dataset's children and check them */
2N/A if ((ret = zfs_iter_filesystems(zhp, be_rollback_check_callback,
2N/A snap_name)) != 0) {
2N/A ZFS_CLOSE(zhp);
2N/A return (ret);
2N/A }
2N/A
2N/A ZFS_CLOSE(zhp);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_rollback_callback
2N/A * Description: Callback function used to iterate through a BE's filesystems
2N/A * and roll them all back to the specified snapshot name.
2N/A * Parameters:
2N/A * zhp - zfs_handle_t pointer to filesystem being processed.
2N/A * data - name of snapshot to rollback to.
2N/A * Returns:
2N/A * 0 - Success
2N/A * be_errno_t - Failure
2N/A * Scope:
2N/A * Private
2N/A */
2N/Astatic int
2N/Abe_rollback_callback(zfs_handle_t *zhp, void *data)
2N/A{
2N/A zfs_handle_t *zhp_snap = NULL;
2N/A char *snap_name = data;
2N/A char ss[MAXPATHLEN];
2N/A int ret = 0;
2N/A
2N/A /* Generate string for this filesystem's snapshot name */
2N/A (void) snprintf(ss, sizeof (ss), "%s@%s", zfs_get_name(zhp), snap_name);
2N/A
2N/A /* Get handle to this filesystem's snapshot */
2N/A if ((zhp_snap = zfs_open(g_zfs, ss, ZFS_TYPE_SNAPSHOT)) == NULL) {
2N/A be_print_err(gettext("be_rollback_callback: "
2N/A "failed to open snapshot %s: %s\n"), zfs_get_name(zhp),
2N/A libzfs_error_description(g_zfs));
2N/A ret = zfs_err_to_be_err(g_zfs);
2N/A ZFS_CLOSE(zhp);
2N/A return (ret);
2N/A }
2N/A
2N/A /* Rollback dataset */
2N/A if (zfs_rollback(zhp, zhp_snap, B_FALSE) != 0) {
2N/A be_print_err(gettext("be_rollback_callback: "
2N/A "failed to rollback BE dataset %s to snapshot %s: %s\n"),
2N/A zfs_get_name(zhp), ss, libzfs_error_description(g_zfs));
2N/A ret = zfs_err_to_be_err(g_zfs);
2N/A ZFS_CLOSE(zhp_snap);
2N/A ZFS_CLOSE(zhp);
2N/A return (ret);
2N/A }
2N/A
2N/A ZFS_CLOSE(zhp_snap);
2N/A /* Iterate this dataset's children and roll them back */
2N/A if ((ret = zfs_iter_filesystems(zhp, be_rollback_callback,
2N/A snap_name)) != 0) {
2N/A ZFS_CLOSE(zhp);
2N/A return (ret);
2N/A }
2N/A
2N/A ZFS_CLOSE(zhp);
2N/A return (0);
2N/A}