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#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/* ******************************************************************** */
2N/A/* Public Functions */
2N/A/* ******************************************************************** */
2N/A
2N/A/*
2N/A * Function: be_rename
2N/A * Description: Renames the BE from the original name to the new name
2N/A * passed in through be_attrs. Also the entries in vfstab are
2N/A * updated with the new name. Caller is responsible for
2N/A * updating the boot menu.
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
2N/A * this function:
2N/A *
2N/A * BE_ATTR_ORIG_BE_NAME *required
2N/A * BE_ATTR_NEW_BE_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/A
2N/Aint
2N/Abe_rename(nvlist_t *be_attrs)
2N/A{
2N/A be_transaction_data_t bt = { 0 };
2N/A be_transaction_data_t cbt = { 0 };
2N/A be_fs_list_data_t fld = { 0 };
2N/A zfs_handle_t *zhp = NULL;
2N/A char root_ds[MAXPATHLEN];
2N/A char *mp = NULL;
2N/A int zret = 0, ret = BE_SUCCESS;
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 if (in_ngz) {
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 to rename from */
2N/A if (nvlist_lookup_string(be_attrs, BE_ATTR_ORIG_BE_NAME, &bt.obe_name)
2N/A != 0) {
2N/A be_print_err(gettext("be_rename: 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 /* Get new BE name to rename to */
2N/A if (nvlist_lookup_string(be_attrs, BE_ATTR_NEW_BE_NAME, &bt.nbe_name)
2N/A != 0) {
2N/A be_print_err(gettext("be_rename: failed to "
2N/A "lookup BE_ATTR_NEW_BE_NAME attribute\n"));
2N/A be_zfs_fini();
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A /*
2N/A * Get the currently active BE and check to see if this
2N/A * is an attempt to rename the currently active BE.
2N/A */
2N/A if (be_find_current_be(&cbt) != BE_SUCCESS) {
2N/A be_print_err(gettext("be_rename: failed to find the currently "
2N/A "active BE\n"));
2N/A be_zfs_fini();
2N/A return (BE_ERR_CURR_BE_NOT_FOUND);
2N/A }
2N/A
2N/A if (strncmp(bt.obe_name, cbt.obe_name,
2N/A MAX(strlen(bt.obe_name), strlen(cbt.obe_name))) == 0) {
2N/A be_print_err(gettext("be_rename: This is an attempt to rename "
2N/A "the currently active BE, which is not supported\n"));
2N/A be_zfs_fini();
2N/A free(cbt.obe_name);
2N/A return (BE_ERR_RENAME_ACTIVE);
2N/A }
2N/A
2N/A /*
2N/A * If this is an attempt to rename a BE that is marked as active
2N/A * on reboot, then we fail since the zfs_rename() won't work due
2N/A * to 6472202.
2N/A */
2N/A if (be_is_active_on_boot(bt.obe_name)) {
2N/A be_print_err(gettext("be_rename: This is an attempt to rename "
2N/A "the active on boot BE, which is not supported\n"));
2N/A be_zfs_fini();
2N/A return (BE_ERR_RENAME_ACTIVE_ON_BOOT);
2N/A }
2N/A
2N/A /* Validate original BE name */
2N/A if (!be_valid_be_name(bt.obe_name)) {
2N/A be_print_err(gettext("be_rename: "
2N/A "invalid BE name %s\n"), bt.obe_name);
2N/A be_zfs_fini();
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A /* Validate new BE name */
2N/A if (!be_valid_be_name(bt.nbe_name)) {
2N/A be_print_err(gettext("be_rename: invalid BE name %s\n"),
2N/A bt.nbe_name);
2N/A be_zfs_fini();
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A /* Find which zpool the BE is in */
2N/A if ((zret = zpool_iter(g_zfs, be_find_zpool_callback, &bt)) == 0) {
2N/A be_print_err(gettext("be_rename: failed to "
2N/A "find zpool for BE (%s)\n"), bt.obe_name);
2N/A be_zfs_fini();
2N/A return (BE_ERR_BE_NOENT);
2N/A } else if (zret < 0) {
2N/A be_print_err(gettext("be_rename: zpool_iter failed: %s\n"),
2N/A libzfs_error_description(g_zfs));
2N/A ret = zfs_err_to_be_err(g_zfs);
2N/A be_zfs_fini();
2N/A return (ret);
2N/A }
2N/A
2N/A /* New BE will reside in the same zpool as orig BE */
2N/A bt.nbe_zpool = bt.obe_zpool;
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 = strdup(root_ds);
2N/A be_make_root_ds(bt.nbe_zpool, bt.nbe_name, root_ds, sizeof (root_ds));
2N/A bt.nbe_root_ds = strdup(root_ds);
2N/A
2N/A /*
2N/A * Generate a list of file systems from the BE that are legacy
2N/A * mounted before renaming. We use this list to determine which
2N/A * entries in the vfstab we need to update after we've renamed the BE.
2N/A */
2N/A if ((ret = be_get_legacy_fs(bt.obe_name, bt.obe_root_ds, NULL, NULL,
2N/A &fld)) != BE_SUCCESS) {
2N/A be_print_err(gettext("be_rename: failed to "
2N/A "get legacy mounted file system list for %s\n"),
2N/A bt.obe_name);
2N/A goto done;
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_FILESYSTEM))
2N/A == NULL) {
2N/A be_print_err(gettext("be_rename: failed to "
2N/A "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 /* Rename of BE's root dataset. */
2N/A if (zfs_rename(zhp, bt.nbe_root_ds, B_FALSE) != 0) {
2N/A be_print_err(gettext("be_rename: failed to "
2N/A "rename dataset (%s): %s\n"), bt.obe_root_ds,
2N/A 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 /* Refresh handle to BE's root dataset after the rename */
2N/A ZFS_CLOSE(zhp);
2N/A
2N/A if ((zhp = zfs_open(g_zfs, bt.nbe_root_ds, ZFS_TYPE_FILESYSTEM))
2N/A == NULL) {
2N/A be_print_err(gettext("be_rename: failed to "
2N/A "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 /* If BE is already mounted, get its mountpoint */
2N/A if (zfs_is_mounted(zhp, &mp) && mp == NULL) {
2N/A be_print_err(gettext("be_rename: failed to "
2N/A "get altroot of mounted BE %s: %s\n"),
2N/A bt.nbe_name, 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 if (!in_ngz) {
2N/A /* Update BE's vfstab */
2N/A if ((ret = be_update_vfstab(bt.nbe_name, bt.obe_zpool,
2N/A bt.nbe_zpool, &fld, mp)) != BE_SUCCESS) {
2N/A be_print_err(gettext("be_rename: failed to update "
2N/A "new BE's vfstab (%s)\n"), bt.nbe_name);
2N/A goto done;
2N/A }
2N/A }
2N/A
2N/Adone:
2N/A be_free_fs_list(&fld);
2N/A
2N/A ZFS_CLOSE(zhp);
2N/A
2N/A be_zfs_fini();
2N/A
2N/A free(bt.obe_root_ds);
2N/A free(bt.nbe_root_ds);
2N/A return (ret);
2N/A}