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