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, 2012, 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 <errno.h>
2N/A#include <libintl.h>
2N/A#include <libgen.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/mntent.h>
2N/A#include <sys/mnttab.h>
2N/A#include <sys/mount.h>
2N/A#include <sys/stat.h>
2N/A#include <sys/types.h>
2N/A#include <sys/vfstab.h>
2N/A#include <sys/zone.h>
2N/A#include <sys/mkdev.h>
2N/A#include <unistd.h>
2N/A
2N/A#include <libbe.h>
2N/A#include <libbe_priv.h>
2N/A
2N/A#define BE_TMP_MNTPNT "/tmp/.be.XXXXXX"
2N/A
2N/Atypedef struct dir_data {
2N/A char *dir;
2N/A char *ds;
2N/A} dir_data_t;
2N/A
2N/A/* Private function prototypes */
2N/Astatic int be_mount_callback(zfs_handle_t *, void *);
2N/Astatic int be_unmount_callback(zfs_handle_t *, void *);
2N/Astatic int be_get_legacy_fs_callback(zfs_handle_t *, void *);
2N/Astatic int fix_mountpoint(zfs_handle_t *);
2N/Astatic int fix_mountpoint_callback(zfs_handle_t *, void *);
2N/Astatic int get_mountpoint_from_vfstab(char *, const char *, char *, size_t,
2N/A boolean_t);
2N/Astatic int loopback_mount_shared_fs(zfs_handle_t *, be_mount_data_t *);
2N/Astatic int loopback_mount_zonepath(const char *, be_mount_data_t *, boolean_t);
2N/Astatic int iter_shared_fs_callback(zfs_handle_t *, void *);
2N/Astatic int zpool_shared_fs_callback(zpool_handle_t *, void *);
2N/Astatic int unmount_shared_fs(be_unmount_data_t *);
2N/Astatic int add_to_fs_list(be_fs_list_data_t *, const char *);
2N/Astatic int be_mount_root(zfs_handle_t *, char *);
2N/Astatic int be_unmount_root(zfs_handle_t *, be_unmount_data_t *);
2N/Astatic int be_mount_zones(zfs_handle_t *, be_mount_data_t *, const char *);
2N/Astatic int be_unmount_zones(be_unmount_data_t *);
2N/Astatic int be_mount_one_zone(zfs_handle_t *, be_mount_data_t *, char *, char *,
2N/A char *);
2N/Astatic int be_unmount_one_zone(be_unmount_data_t *, char *, char *, char *);
2N/Astatic int be_get_ds_from_dir_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_mount
2N/A * Description: Mounts a BE and its subordinate datasets at a given mountpoint.
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 *required
2N/A * BE_ATTR_MOUNTPOINT *required
2N/A * BE_ATTR_ALT_POOL *optional
2N/A * BE_ATTR_MOUNT_FLAGS *optional
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_mount(nvlist_t *be_attrs)
2N/A{
2N/A char *be_name = NULL;
2N/A char *mountpoint = NULL;
2N/A char *altpool = NULL;
2N/A uint16_t flags = 0;
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 */
2N/A if (nvlist_lookup_string(be_attrs, BE_ATTR_ORIG_BE_NAME, &be_name)
2N/A != 0) {
2N/A be_print_err(gettext("be_mount: failed to lookup "
2N/A "BE_ATTR_ORIG_BE_NAME attribute\n"));
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A /* Validate original BE name */
2N/A if (!be_valid_be_name(be_name)) {
2N/A be_print_err(gettext("be_mount: invalid BE name %s\n"),
2N/A be_name);
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A /* Get mountpoint */
2N/A if (nvlist_lookup_string(be_attrs, BE_ATTR_MOUNTPOINT, &mountpoint)
2N/A != 0) {
2N/A be_print_err(gettext("be_mount: failed to lookup "
2N/A "BE_ATTR_MOUNTPOINT attribute\n"));
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_mount: failed to lookup "
2N/A "BE_ATTR_ALT_POOL attribute\n"));
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A /* Get flags */
2N/A if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK,
2N/A BE_ATTR_MOUNT_FLAGS, DATA_TYPE_UINT16, &flags, NULL) != 0) {
2N/A be_print_err(gettext("be_mount: failed to lookup "
2N/A "BE_ATTR_MOUNT_FLAGS attribute\n"));
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A ret = _be_mount(be_name, altpool, &mountpoint, flags);
2N/A
2N/A be_zfs_fini();
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_unmount
2N/A * Description: Unmounts a BE and its subordinate datasets.
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 *required
2N/A * BE_ATTR_ALT_POOL *optional
2N/A * BE_ATTR_UNMOUNT_FLAGS *optional
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_unmount(nvlist_t *be_attrs)
2N/A{
2N/A char *be_name = NULL;
2N/A char *altpool = NULL;
2N/A uint16_t flags = 0;
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 */
2N/A if (nvlist_lookup_string(be_attrs, BE_ATTR_ORIG_BE_NAME, &be_name)
2N/A != 0) {
2N/A be_print_err(gettext("be_unmount: failed to lookup "
2N/A "BE_ATTR_ORIG_BE_NAME attribute\n"));
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A /* Validate original BE name */
2N/A if (!be_valid_be_name(be_name)) {
2N/A be_print_err(gettext("be_unmount: invalid BE name %s\n"),
2N/A be_name);
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A /* Get alternate "pool" area */
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_unmount: failed to lookup "
2N/A "BE_ATTR_ALT_POOL attribute\n"));
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A /* Get unmount flags */
2N/A if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK,
2N/A BE_ATTR_UNMOUNT_FLAGS, DATA_TYPE_UINT16, &flags, NULL) != 0) {
2N/A be_print_err(gettext("be_unmount: failed to loookup "
2N/A "BE_ATTR_UNMOUNT_FLAGS attribute\n"));
2N/A return (BE_ERR_INVAL);
2N/A }
2N/A
2N/A ret = _be_unmount(be_name, altpool, flags);
2N/A
2N/A be_zfs_fini();
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/* ******************************************************************** */
2N/A/* Semi-Private Functions */
2N/A/* ******************************************************************** */
2N/A
2N/A/*
2N/A * Function: _be_mount
2N/A * Description: Mounts a BE. If the altroot is not provided, this function
2N/A * will generate a temporary mountpoint to mount the BE at. It
2N/A * will return this temporary mountpoint to the caller via the
2N/A * altroot reference pointer passed in. This returned value is
2N/A * allocated on heap storage and is the repsonsibility of the
2N/A * caller to free.
2N/A * Parameters:
2N/A * be_name - pointer to name of BE to mount.
2N/A * altpool - pointer to alternate "pool" area to find the BE.
2N/A * altroot - reference pointer to altroot of where to mount BE.
2N/A * flags - flag indicating special handling for mounting the BE
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_mount(char *be_name, char *altpool, char **altroot, int flags)
2N/A{
2N/A be_transaction_data_t bt = { 0 };
2N/A be_mount_data_t md = { 0 };
2N/A zfs_handle_t *zhp;
2N/A char obe_root_ds[MAXPATHLEN];
2N/A char *mp = NULL;
2N/A char *tmp_altroot = NULL;
2N/A int ret = BE_SUCCESS, err = 0;
2N/A uuid_t uu = { 0 };
2N/A boolean_t gen_tmp_altroot = 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 if (be_name == NULL || altroot == NULL)
2N/A return (BE_ERR_INVAL);
2N/A
2N/A /* Set be_name as obe_name in bt structure */
2N/A bt.obe_name = be_name;
2N/A
2N/A /* Find which zpool obe_name lives in if alternate "pool" not provide */
2N/A if (altpool == NULL) {
2N/A if ((err = zpool_iter(g_zfs, be_find_zpool_callback, &bt))
2N/A == 0) {
2N/A be_print_err(gettext("be_mount: failed to "
2N/A "find zpool for BE (%s)\n"), bt.obe_name);
2N/A return (BE_ERR_BE_NOENT);
2N/A } else if (err < 0) {
2N/A be_print_err(gettext("be_mount: zpool_iter failed: "
2N/A "%s\n"), 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 /* Generate string for obe_name'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_FILESYSTEM)) ==
2N/A NULL) {
2N/A be_print_err(gettext("be_mount: failed to "
2N/A "open BE root dataset (%s): %s\n"), bt.obe_root_ds,
2N/A libzfs_error_description(g_zfs));
2N/A return (zfs_err_to_be_err(g_zfs));
2N/A }
2N/A
2N/A /* Make sure BE's root dataset isn't already mounted somewhere */
2N/A if (zfs_is_mounted(zhp, &mp)) {
2N/A ZFS_CLOSE(zhp);
2N/A be_print_err(gettext("be_mount: %s is already mounted "
2N/A "at %s\n"), bt.obe_name, mp != NULL ? mp : "");
2N/A free(mp);
2N/A return (BE_ERR_MOUNTED);
2N/A }
2N/A
2N/A /*
2N/A * Fix this BE's mountpoint if its root dataset isn't set to
2N/A * either 'legacy' or '/'.
2N/A */
2N/A if ((ret = fix_mountpoint(zhp)) != BE_SUCCESS) {
2N/A be_print_err(gettext("be_mount: mountpoint check "
2N/A "failed for %s\n"), bt.obe_root_ds);
2N/A ZFS_CLOSE(zhp);
2N/A return (ret);
2N/A }
2N/A
2N/A /*
2N/A * If altroot not provided, create a temporary alternate root
2N/A * to mount on
2N/A */
2N/A if (*altroot == NULL) {
2N/A if ((ret = be_make_tmp_mountpoint(&tmp_altroot))
2N/A != BE_SUCCESS) {
2N/A be_print_err(gettext("be_mount: failed to "
2N/A "make temporary mountpoint\n"));
2N/A ZFS_CLOSE(zhp);
2N/A return (ret);
2N/A }
2N/A gen_tmp_altroot = B_TRUE;
2N/A } else {
2N/A tmp_altroot = *altroot;
2N/A }
2N/A
2N/A /* Mount the BE's root file system */
2N/A if ((ret = be_mount_root(zhp, tmp_altroot)) != BE_SUCCESS) {
2N/A be_print_err(gettext("be_mount: failed to "
2N/A "mount BE root file system\n"));
2N/A if (gen_tmp_altroot)
2N/A free(tmp_altroot);
2N/A ZFS_CLOSE(zhp);
2N/A return (ret);
2N/A }
2N/A
2N/A /* Iterate through BE's children filesystems */
2N/A if ((err = zfs_iter_filesystems(zhp, be_mount_callback,
2N/A tmp_altroot)) != 0) {
2N/A be_print_err(gettext("be_mount: failed to "
2N/A "mount BE (%s) on %s\n"), bt.obe_name, tmp_altroot);
2N/A if (gen_tmp_altroot)
2N/A free(tmp_altroot);
2N/A ZFS_CLOSE(zhp);
2N/A return (err);
2N/A }
2N/A
2N/A md.altroot = tmp_altroot;
2N/A md.shared_fs = flags & BE_MOUNT_FLAG_SHARED_FS;
2N/A md.shared_rw = flags & BE_MOUNT_FLAG_SHARED_RW;
2N/A
2N/A /*
2N/A * Mount shared file systems if mount flag says so.
2N/A */
2N/A if (md.shared_fs) {
2N/A /*
2N/A * Mount all ZFS file systems not under the BE's
2N/A * root dataset.
2N/A */
2N/A (void) zpool_iter(g_zfs, zpool_shared_fs_callback, &md);
2N/A }
2N/A
2N/A /*
2N/A * If we're in the global zone and the global zone has
2N/A * a valid uuid, mount all supported non-global zones.
2N/A */
2N/A if (!in_ngz && !(flags & BE_MOUNT_FLAG_NO_ZONES) &&
2N/A be_get_uuid(bt.obe_root_ds, &uu) == BE_SUCCESS) {
2N/A if ((ret = be_mount_zones(zhp, &md, be_name))
2N/A != BE_SUCCESS) {
2N/A (void) _be_unmount(bt.obe_name, altpool,
2N/A BE_UNMOUNT_FLAG_NULL);
2N/A if (gen_tmp_altroot)
2N/A free(tmp_altroot);
2N/A ZFS_CLOSE(zhp);
2N/A return (ret);
2N/A }
2N/A }
2N/A
2N/A ZFS_CLOSE(zhp);
2N/A
2N/A /*
2N/A * If a NULL altroot was passed in, pass the generated altroot
2N/A * back to the caller in altroot.
2N/A */
2N/A if (gen_tmp_altroot)
2N/A *altroot = tmp_altroot;
2N/A
2N/A return (BE_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * Function: _be_unmount
2N/A * Description: Unmount a BE.
2N/A * Parameters:
2N/A * be_name - pointer to name of BE to unmount.
2N/A * altpool - pointer to alternate "pool" area to find the BE.
2N/A * flags - flags for unmounting the BE.
2N/A * Returns:
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_unmount(char *be_name, char *altpool, int flags)
2N/A{
2N/A be_transaction_data_t bt = { 0 };
2N/A be_unmount_data_t ud = { 0 };
2N/A zfs_handle_t *zhp;
2N/A uuid_t uu = { 0 };
2N/A char obe_root_ds[MAXPATHLEN];
2N/A char mountpoint[MAXPATHLEN];
2N/A char *mp = NULL;
2N/A int ret = BE_SUCCESS;
2N/A int zret = 0;
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 (be_name == NULL)
2N/A return (BE_ERR_INVAL);
2N/A
2N/A /* Set be_name as obe_name in bt structure */
2N/A bt.obe_name = be_name;
2N/A
2N/A /* Find which zpool obe_name lives in if alternate "pool" not provide */
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_unmount: 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_unmount: "
2N/A "zpool_iter failed: %s\n"),
2N/A libzfs_error_description(g_zfs));
2N/A ret = zfs_err_to_be_err(g_zfs);
2N/A return (ret);
2N/A }
2N/A } else {
2N/A bt.obe_zpool = altpool;
2N/A }
2N/A
2N/A /* Generate string for obe_name'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_FILESYSTEM)) ==
2N/A NULL) {
2N/A be_print_err(gettext("be_unmount: failed to "
2N/A "open BE root 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 return (ret);
2N/A }
2N/A
2N/A /* Make sure BE's root dataset is mounted somewhere */
2N/A if (!zfs_is_mounted(zhp, &mp)) {
2N/A
2N/A be_print_err(gettext("be_unmount: "
2N/A "(%s) not mounted\n"), bt.obe_name);
2N/A
2N/A /*
2N/A * BE is not mounted, fix this BE's mountpoint if its root
2N/A * dataset isn't set to either 'legacy' or '/'.
2N/A */
2N/A if ((ret = fix_mountpoint(zhp)) != BE_SUCCESS) {
2N/A be_print_err(gettext("be_unmount: mountpoint check "
2N/A "failed for %s\n"), bt.obe_root_ds);
2N/A ZFS_CLOSE(zhp);
2N/A return (ret);
2N/A }
2N/A
2N/A ZFS_CLOSE(zhp);
2N/A return (BE_SUCCESS);
2N/A }
2N/A
2N/A /*
2N/A * If we didn't get a mountpoint from the zfs_is_mounted call,
2N/A * try and get it from its property.
2N/A */
2N/A if (mp == NULL) {
2N/A if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
2N/A sizeof (mountpoint), NULL, NULL, 0, B_FALSE) != 0) {
2N/A be_print_err(gettext("be_unmount: failed to "
2N/A "get mountpoint of (%s)\n"), bt.obe_name);
2N/A ZFS_CLOSE(zhp);
2N/A return (BE_ERR_ZFS);
2N/A }
2N/A } else {
2N/A (void) strlcpy(mountpoint, mp, sizeof (mountpoint));
2N/A free(mp);
2N/A }
2N/A
2N/A /* If BE mounted as current root, fail */
2N/A if (strcmp(mountpoint, "/") == 0) {
2N/A be_print_err(gettext("be_unmount: "
2N/A "cannot unmount currently running BE\n"));
2N/A ZFS_CLOSE(zhp);
2N/A return (BE_ERR_UMOUNT_CURR_BE);
2N/A }
2N/A
2N/A ud.altroot = mountpoint;
2N/A ud.force = flags & BE_UNMOUNT_FLAG_FORCE;
2N/A
2N/A /* Unmount all supported non-global zones if we're in the global zone */
2N/A if (!in_ngz && !(flags & BE_UNMOUNT_FLAG_NO_ZONES) &&
2N/A be_get_uuid(bt.obe_root_ds, &uu) == BE_SUCCESS) {
2N/A if ((ret = be_unmount_zones(&ud)) != BE_SUCCESS) {
2N/A ZFS_CLOSE(zhp);
2N/A return (ret);
2N/A }
2N/A }
2N/A
2N/A /* TODO: Unmount all non-ZFS file systems - Not supported yet */
2N/A
2N/A /* Unmount all ZFS file systems not under the BE root dataset */
2N/A if ((ret = unmount_shared_fs(&ud)) != BE_SUCCESS) {
2N/A be_print_err(gettext("be_unmount: failed to "
2N/A "unmount shared file systems\n"));
2N/A ZFS_CLOSE(zhp);
2N/A return (ret);
2N/A }
2N/A
2N/A /* Unmount all children datasets under the BE's root dataset */
2N/A if ((zret = zfs_iter_filesystems(zhp, be_unmount_callback,
2N/A &ud)) != 0) {
2N/A be_print_err(gettext("be_unmount: failed to "
2N/A "unmount BE (%s)\n"), bt.obe_name);
2N/A ZFS_CLOSE(zhp);
2N/A return (zret);
2N/A }
2N/A
2N/A /* Unmount this BE's root filesystem */
2N/A if ((ret = be_unmount_root(zhp, &ud)) != BE_SUCCESS) {
2N/A ZFS_CLOSE(zhp);
2N/A return (ret);
2N/A }
2N/A
2N/A ZFS_CLOSE(zhp);
2N/A
2N/A return (BE_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_mount_zone_root
2N/A * Description: Mounts the zone root dataset for a zone.
2N/A * Parameters:
2N/A * zfs - zfs_handle_t pointer to zone root dataset
2N/A * md - be_mount_data_t pointer to data for zone to be mounted
2N/A * Returns:
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/Abe_mount_zone_root(zfs_handle_t *zhp, be_mount_data_t *md)
2N/A{
2N/A char mountpoint[MAXPATHLEN];
2N/A char options[MAXPATHLEN + sizeof (MNTOPT_ZFS_MOUNTPOINT) + 1];
2N/A char *mp = NULL;
2N/A
2N/A /* Get mountpoint property of dataset */
2N/A if (zfs_prop_get_persistent(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
2N/A sizeof (mountpoint), NULL, NULL, 0, B_FALSE, NULL) != 0) {
2N/A be_print_err(gettext("be_mount_zone_root: failed to "
2N/A "get mountpoint property for %s: %s\n"), zfs_get_name(zhp),
2N/A libzfs_error_description(g_zfs));
2N/A return (zfs_err_to_be_err(g_zfs));
2N/A }
2N/A
2N/A /*
2N/A * Make sure zone's root dataset is set to /. The ZFS temporary
2N/A * mountpoint feature is used to mount at the zone root in the
2N/A * global zone.
2N/A */
2N/A if (strcmp(mountpoint, "/") != 0) {
2N/A be_print_err(gettext("be_mount_zone_root: "
2N/A "zone root dataset mountpoint is not '/'\n"));
2N/A return (BE_ERR_ZONE_ROOT_NOT_SLASH);
2N/A }
2N/A
2N/A /*
2N/A * Check to see if the zone root is already mounted, perhaps
2N/A * within the zone itself.
2N/A */
2N/A if (zfs_is_mounted(zhp, &mp)) {
2N/A be_print_err(gettext("be_mount_zone_root: could not "
2N/A "mount zone root dataset %s \nbecause it is "
2N/A "already mounted at %s. This is likely due to "
2N/A "the zone root\nbeing mounted within the zone "
2N/A "itself.\n"), zfs_get_name(zhp), mp);
2N/A return (BE_ERR_MOUNT);
2N/A }
2N/A
2N/A /*
2N/A * Temporary mount the zone root dataset.
2N/A */
2N/A (void) snprintf(options, sizeof (options), MNTOPT_ZFS_MOUNTPOINT "=%s",
2N/A md->altroot);
2N/A if (zfs_mount(zhp, options, 0) != 0) {
2N/A be_print_err(gettext("be_mount_zone_root: failed to "
2N/A "temporary mount zone root dataset (%s) at %s\n"),
2N/A zfs_get_name(zhp), md->altroot);
2N/A return (zfs_err_to_be_err(g_zfs));
2N/A }
2N/A
2N/A return (BE_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_unmount_zone_root
2N/A * Description: Unmounts the zone root dataset for a zone.
2N/A * Parameters:
2N/A * zhp - zfs_handle_t pointer to zone root dataset
2N/A * ud - be_unmount_data_t pointer to data for zone to be unmounted
2N/A * Returns:
2N/A * BE_SUCCESS - Success
2N/A * be_errno_t - Failure
2N/A * Scope:
2N/A * Semi-private (library wise use only)
2N/A */
2N/Aint
2N/Abe_unmount_zone_root(zfs_handle_t *zhp, be_unmount_data_t *ud)
2N/A{
2N/A char mountpoint[MAXPATHLEN];
2N/A
2N/A /* Unmount the dataset */
2N/A if (zfs_unmount(zhp, NULL, ud->force ? MS_FORCE : 0) != 0) {
2N/A be_print_err(gettext("be_unmount_zone_root: failed to "
2N/A "unmount zone root dataset %s: %s\n"), zfs_get_name(zhp),
2N/A libzfs_error_description(g_zfs));
2N/A return (zfs_err_to_be_err(g_zfs));
2N/A }
2N/A
2N/A /* Get the current mountpoint property for the zone root dataset */
2N/A if (zfs_prop_get_persistent(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
2N/A sizeof (mountpoint), NULL, NULL, 0, B_FALSE, NULL) != 0) {
2N/A be_print_err(gettext("be_unmount_zone_root: failed to "
2N/A "get mountpoint property for zone root dataset (%s): %s\n"),
2N/A zfs_get_name(zhp), libzfs_error_description(g_zfs));
2N/A return (zfs_err_to_be_err(g_zfs));
2N/A }
2N/A
2N/A /* If mountpoint not already set to '/', set it to '/' */
2N/A if (strcmp(mountpoint, "/") != 0) {
2N/A if (zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
2N/A "/") != 0) {
2N/A be_print_err(gettext("be_unmount_zone_root: "
2N/A "failed to set mountpoint of zone root dataset "
2N/A "%s to '/': %s\n"), zfs_get_name(zhp),
2N/A libzfs_error_description(g_zfs));
2N/A return (zfs_err_to_be_err(g_zfs));
2N/A }
2N/A }
2N/A
2N/A return (BE_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_get_legacy_fs
2N/A * Description: This function iterates through all non-shared file systems
2N/A * of a BE and finds the ones with a legacy mountpoint. For
2N/A * those file systems, it reads the BE's vfstab to get the
2N/A * mountpoint. If found, it adds that file system to the
2N/A * be_fs_list_data_t passed in.
2N/A *
2N/A * This function can be used to gather legacy mounted file systems
2N/A * for both global BEs and non-global zone BEs. To get data for
2N/A * a non-global zone BE, the zoneroot_ds and zoneroot parameters
2N/A * will be specified, otherwise they should be set to NULL.
2N/A * Parameters:
2N/A * be_name - global BE name from which to get legacy file
2N/A * system list.
2N/A * be_root_ds - root dataset of global BE.
2N/A * zoneroot_ds - root dataset of zone.
2N/A * zoneroot - zoneroot path of zone.
2N/A * fld - be_fs_list_data_t pointer.
2N/A * Returns:
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/Abe_get_legacy_fs(char *be_name, char *be_root_ds, char *zoneroot_ds,
2N/A char *zoneroot, be_fs_list_data_t *fld)
2N/A{
2N/A zfs_handle_t *zhp = NULL;
2N/A char mountpoint[MAXPATHLEN];
2N/A boolean_t mounted_here = B_FALSE;
2N/A boolean_t zone_mounted_here = B_FALSE;
2N/A int ret = BE_SUCCESS, err = 0;
2N/A uint16_t umnt_flags = BE_UNMOUNT_FLAG_NULL;
2N/A
2N/A if (be_name == NULL || be_root_ds == NULL || fld == NULL)
2N/A return (BE_ERR_INVAL);
2N/A
2N/A /* Get handle to BE's root dataset */
2N/A if ((zhp = zfs_open(g_zfs, be_root_ds, ZFS_TYPE_FILESYSTEM))
2N/A == NULL) {
2N/A be_print_err(gettext("be_get_legacy_fs: failed to "
2N/A "open BE root dataset (%s): %s\n"), be_root_ds,
2N/A libzfs_error_description(g_zfs));
2N/A ret = zfs_err_to_be_err(g_zfs);
2N/A return (ret);
2N/A }
2N/A
2N/A /* If BE is not already mounted, mount it. */
2N/A if (!zfs_is_mounted(zhp, &fld->altroot)) {
2N/A if ((ret = _be_mount(be_name, NULL, &fld->altroot,
2N/A zoneroot_ds ? BE_MOUNT_FLAG_NULL :
2N/A BE_MOUNT_FLAG_NO_ZONES)) != BE_SUCCESS) {
2N/A be_print_err(gettext("be_get_legacy_fs: "
2N/A "failed to mount BE %s\n"), be_name);
2N/A goto cleanup;
2N/A }
2N/A mounted_here = B_TRUE;
2N/A if (zoneroot_ds == NULL)
2N/A umnt_flags |= BE_UNMOUNT_FLAG_NO_ZONES;
2N/A } else if (fld->altroot == NULL) {
2N/A be_print_err(gettext("be_get_legacy_fs: failed to "
2N/A "get altroot of mounted BE %s: %s\n"),
2N/A be_name, libzfs_error_description(g_zfs));
2N/A ret = zfs_err_to_be_err(g_zfs);
2N/A goto cleanup;
2N/A }
2N/A
2N/A /*
2N/A * If a zone root dataset was passed in, we're wanting to get
2N/A * legacy mounted file systems for that zone, not the global
2N/A * BE.
2N/A */
2N/A if (zoneroot_ds != NULL) {
2N/A be_mount_data_t zone_md = { 0 };
2N/A
2N/A /* Close off handle to global BE's root dataset */
2N/A ZFS_CLOSE(zhp);
2N/A
2N/A /* Get handle to zone's root dataset */
2N/A if ((zhp = zfs_open(g_zfs, zoneroot_ds,
2N/A ZFS_TYPE_FILESYSTEM)) == NULL) {
2N/A be_print_err(gettext("be_get_legacy_fs: failed to "
2N/A "open zone BE root dataset (%s): %s\n"),
2N/A zoneroot_ds, libzfs_error_description(g_zfs));
2N/A ret = zfs_err_to_be_err(g_zfs);
2N/A goto cleanup;
2N/A }
2N/A
2N/A /* Make sure the zone we're looking for is mounted */
2N/A if (!zfs_is_mounted(zhp, &zone_md.altroot)) {
2N/A char zone_altroot[MAXPATHLEN];
2N/A
2N/A /* Generate alternate root path for zone */
2N/A (void) snprintf(zone_altroot, sizeof (zone_altroot),
2N/A "%s%s", fld->altroot, zoneroot);
2N/A if ((zone_md.altroot = strdup(zone_altroot)) == NULL) {
2N/A be_print_err(gettext("be_get_legacy_fs: "
2N/A "memory allocation failed\n"));
2N/A ret = BE_ERR_NOMEM;
2N/A goto cleanup;
2N/A }
2N/A
2N/A if ((ret = be_mount_zone_root(zhp, &zone_md))
2N/A != BE_SUCCESS) {
2N/A be_print_err(gettext("be_get_legacy_fs: "
2N/A "failed to mount zone root %s\n"),
2N/A zoneroot_ds);
2N/A free(zone_md.altroot);
2N/A zone_md.altroot = NULL;
2N/A goto cleanup;
2N/A }
2N/A zone_mounted_here = B_TRUE;
2N/A }
2N/A
2N/A free(fld->altroot);
2N/A fld->altroot = zone_md.altroot;
2N/A }
2N/A
2N/A /*
2N/A * If the root dataset is in the vfstab with a mountpoint of "/",
2N/A * add it to the list
2N/A */
2N/A if (get_mountpoint_from_vfstab(fld->altroot, zfs_get_name(zhp),
2N/A mountpoint, sizeof (mountpoint), B_FALSE) == BE_SUCCESS) {
2N/A if (strcmp(mountpoint, "/") == 0) {
2N/A if (add_to_fs_list(fld, zfs_get_name(zhp))
2N/A != BE_SUCCESS) {
2N/A be_print_err(gettext("be_get_legacy_fs: "
2N/A "failed to add %s to fs list\n"),
2N/A zfs_get_name(zhp));
2N/A ret = BE_ERR_INVAL;
2N/A goto cleanup;
2N/A }
2N/A }
2N/A }
2N/A
2N/A /* Iterate subordinate file systems looking for legacy mounts */
2N/A if ((ret = zfs_iter_filesystems(zhp, be_get_legacy_fs_callback,
2N/A fld)) != 0) {
2N/A be_print_err(gettext("be_get_legacy_fs: "
2N/A "failed to iterate %s to get legacy mounts\n"),
2N/A zfs_get_name(zhp));
2N/A }
2N/A
2N/Acleanup:
2N/A /* If we mounted the zone BE, unmount it */
2N/A if (zone_mounted_here) {
2N/A be_unmount_data_t zone_ud = { 0 };
2N/A
2N/A zone_ud.altroot = fld->altroot;
2N/A zone_ud.force = B_TRUE;
2N/A if ((err = be_unmount_zone_root(zhp, &zone_ud)) != BE_SUCCESS) {
2N/A be_print_err(gettext("be_get_legacy_fs: "
2N/A "failed to unmount zone root %s\n"),
2N/A zoneroot_ds);
2N/A if (ret == BE_SUCCESS)
2N/A ret = err;
2N/A }
2N/A }
2N/A
2N/A /* If we mounted this BE, unmount it */
2N/A if (mounted_here) {
2N/A if ((err = _be_unmount(be_name, NULL, umnt_flags))
2N/A != BE_SUCCESS) {
2N/A be_print_err(gettext("be_get_legacy_fs: "
2N/A "failed to unmount %s\n"), be_name);
2N/A if (ret == BE_SUCCESS)
2N/A ret = err;
2N/A }
2N/A }
2N/A
2N/A ZFS_CLOSE(zhp);
2N/A
2N/A free(fld->altroot);
2N/A fld->altroot = NULL;
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_free_fs_list
2N/A * Description: Function used to free the members of a be_fs_list_data_t
2N/A * structure.
2N/A * Parameters:
2N/A * fld - be_fs_list_data_t pointer to free.
2N/A * Returns:
2N/A * None
2N/A * Scope:
2N/A * Semi-private (library wide use only)
2N/A */
2N/Avoid
2N/Abe_free_fs_list(be_fs_list_data_t *fld)
2N/A{
2N/A int i;
2N/A
2N/A if (fld == NULL)
2N/A return;
2N/A
2N/A free(fld->altroot);
2N/A
2N/A if (fld->fs_list == NULL)
2N/A return;
2N/A
2N/A for (i = 0; i < fld->fs_num; i++)
2N/A free(fld->fs_list[i]);
2N/A
2N/A free(fld->fs_list);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_get_ds_from_dir(char *dir)
2N/A * Description: Given a directory path, find the underlying dataset mounted
2N/A * at that directory path if there is one. The returned name
2N/A * is allocated in heap storage, so the caller is responsible
2N/A * for freeing it.
2N/A * Parameters:
2N/A * dir - char pointer of directory to find.
2N/A * Returns:
2N/A * NULL - if directory is not mounted from a dataset.
2N/A * name of dataset mounted at dir.
2N/A * Scope:
2N/A * Semi-private (library wide use only)
2N/A */
2N/Achar *
2N/Abe_get_ds_from_dir(char *dir)
2N/A{
2N/A dir_data_t dd = { 0 };
2N/A char resolved_dir[MAXPATHLEN];
2N/A
2N/A /* Make sure length of dir is within the max length */
2N/A if (dir == NULL || strlen(dir) >= MAXPATHLEN)
2N/A return (NULL);
2N/A
2N/A /* Resolve dir in case its lofs mounted */
2N/A (void) strlcpy(resolved_dir, dir, sizeof (resolved_dir));
2N/A z_resolve_lofs(resolved_dir, sizeof (resolved_dir));
2N/A
2N/A dd.dir = resolved_dir;
2N/A
2N/A (void) zfs_iter_root(g_zfs, be_get_ds_from_dir_callback, &dd);
2N/A
2N/A return (dd.ds);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_make_tmp_mountpoint
2N/A * Description: This function generates a random temporary mountpoint
2N/A * and creates that mountpoint directory. It returns the
2N/A * mountpoint in heap storage, so the caller is responsible
2N/A * for freeing it.
2N/A * Parameters:
2N/A * tmp_mp - reference to pointer of where to store generated
2N/A * temporary mountpoint.
2N/A * Returns:
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/Abe_make_tmp_mountpoint(char **tmp_mp)
2N/A{
2N/A int err = 0;
2N/A
2N/A if ((*tmp_mp = (char *)calloc(1, sizeof (BE_TMP_MNTPNT) + 1)) == NULL) {
2N/A be_print_err(gettext("be_make_tmp_mountpoint: "
2N/A "malloc failed\n"));
2N/A return (BE_ERR_NOMEM);
2N/A }
2N/A (void) strlcpy(*tmp_mp, BE_TMP_MNTPNT, sizeof (BE_TMP_MNTPNT) + 1);
2N/A if (mkdtemp(*tmp_mp) == NULL) {
2N/A err = errno;
2N/A be_print_err(gettext("be_make_tmp_mountpoint: mkdtemp() failed "
2N/A "for %s: %s\n"), *tmp_mp, strerror(err));
2N/A free(*tmp_mp);
2N/A *tmp_mp = NULL;
2N/A return (errno_to_be_err(err));
2N/A }
2N/A
2N/A return (BE_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_tmp_mount_ds
2N/A * Description: This function will temporarily mount a dataset on a randomly
2N/A * generated tmp mountpoint.
2N/A * Parameters:
2N/A * zhp - handle to the dataset to mount.
2N/A * Returns:
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/Abe_tmp_mount_ds(zfs_handle_t *zhp)
2N/A{
2N/A char options[MAXPATHLEN + sizeof (MNTOPT_ZFS_MOUNTPOINT) + 1];
2N/A char *tmp_mntpnt = NULL;
2N/A int ret = 0;
2N/A
2N/A if (zfs_is_mounted(zhp, NULL)) {
2N/A /* Dataset already mounted, return success */
2N/A return (BE_SUCCESS);
2N/A }
2N/A
2N/A /*
2N/A * Attempt to mount on a temp mountpoint
2N/A */
2N/A if ((ret = be_make_tmp_mountpoint(&tmp_mntpnt)) != BE_SUCCESS) {
2N/A be_print_err(gettext("be_tmp_mount_ds: failed "
2N/A "to make temporary mountpoint\n"));
2N/A return (ret);
2N/A }
2N/A
2N/A
2N/A /* Temporarily mount this filesystem */
2N/A (void) snprintf(options, sizeof (options), MNTOPT_ZFS_MOUNTPOINT "=%s",
2N/A tmp_mntpnt);
2N/A if (zfs_mount(zhp, options, 0) != 0) {
2N/A be_print_err(gettext("be_tmp_mount_ds: failed to "
2N/A "mount dataset %s at %s: %s\n"), zfs_get_name(zhp),
2N/A tmp_mntpnt, libzfs_error_description(g_zfs));
2N/A return (BE_ERR_MOUNT);
2N/A }
2N/A
2N/A return (BE_SUCCESS);
2N/A}
2N/A
2N/A/* ******************************************************************** */
2N/A/* Private Functions */
2N/A/* ******************************************************************** */
2N/A
2N/A/*
2N/A * Function: be_mount_callback
2N/A * Description: Callback function used to iterate through all of a BE's
2N/A * subordinate file systems and to mount them accordingly.
2N/A * Parameters:
2N/A * zhp - zfs_handle_t pointer to current file system being
2N/A * processed.
2N/A * data - pointer to the altroot of where to mount BE.
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_mount_callback(zfs_handle_t *zhp, void *data)
2N/A{
2N/A zprop_source_t sourcetype;
2N/A const char *fs_name = zfs_get_name(zhp);
2N/A char source[ZFS_MAXNAMELEN];
2N/A char *altroot = data;
2N/A char zhp_mountpoint[MAXPATHLEN];
2N/A char mountpoint[MAXPATHLEN];
2N/A char options[MAXPATHLEN +
2N/A sizeof (MNTOPT_ZFS_MOUNTPOINT) + 1];
2N/A int ret = 0;
2N/A int err = 0;
2N/A
2N/A /* Get dataset's persistent mountpoint and source values */
2N/A if (zfs_prop_get_persistent(zhp, ZFS_PROP_MOUNTPOINT, zhp_mountpoint,
2N/A sizeof (zhp_mountpoint), &sourcetype, source, sizeof (source),
2N/A B_FALSE, NULL) != 0) {
2N/A be_print_err(gettext("be_mount_callback: failed to "
2N/A "get mountpoint and sourcetype for %s\n"),
2N/A fs_name);
2N/A ZFS_CLOSE(zhp);
2N/A return (BE_ERR_ZFS);
2N/A }
2N/A
2N/A /*
2N/A * Set this filesystem's 'canmount' property to 'noauto' just incase
2N/A * it's been set 'on'. We do this so that when we change its
2N/A * mountpoint zfs won't immediately try to mount it.
2N/A */
2N/A if (zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_CANMOUNT), "noauto")) {
2N/A be_print_err(gettext("be_mount_callback: failed to "
2N/A "set canmount to 'noauto' (%s)\n"), fs_name);
2N/A ZFS_CLOSE(zhp);
2N/A return (BE_ERR_ZFS);
2N/A }
2N/A
2N/A /*
2N/A * If the mountpoint is none, there's nothing to do, goto next.
2N/A * If the mountpoint is legacy, legacy mount it with mount(2).
2N/A * If the mountpoint is inherited, its mountpoint should
2N/A * already be set. If it's not, then explicitly fix-up
2N/A * the mountpoint now by appending its explicitly set
2N/A * mountpoint value to the BE mountpoint.
2N/A */
2N/A if (strcmp(zhp_mountpoint, ZFS_MOUNTPOINT_NONE) == 0) {
2N/A goto next;
2N/A } else if (strcmp(zhp_mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
2N/A /*
2N/A * If the mountpoint is set to 'legacy', we need to
2N/A * dig into this BE's vfstab to figure out where to
2N/A * mount it, and just mount it via mount(2) relative
2N/A * to altroot.
2N/A */
2N/A if (get_mountpoint_from_vfstab(altroot, fs_name,
2N/A mountpoint, sizeof (mountpoint), B_TRUE) == BE_SUCCESS) {
2N/A
2N/A /* Legacy mount the file system */
2N/A if (mount(fs_name, mountpoint, MS_DATA,
2N/A MNTTYPE_ZFS, NULL, 0, NULL, 0) != 0) {
2N/A be_print_err(
2N/A gettext("be_mount_callback: "
2N/A "failed to mount %s on %s\n"),
2N/A fs_name, mountpoint);
2N/A }
2N/A } else {
2N/A be_print_err(
2N/A gettext("be_mount_callback: "
2N/A "no entry for %s in vfstab, "
2N/A "skipping ...\n"), fs_name);
2N/A }
2N/A
2N/A goto next;
2N/A } else if (sourcetype & ZPROP_SRC_INHERITED ||
2N/A sourcetype & ZPROP_SRC_LOCAL || sourcetype & ZPROP_SRC_RECEIVED) {
2N/A /*
2N/A * Else process dataset mountpoint property relative to altroot.
2N/A */
2N/A (void) snprintf(mountpoint, sizeof (mountpoint),
2N/A "%s%s", altroot, zhp_mountpoint);
2N/A } else {
2N/A be_print_err(gettext("be_mount_callback: "
2N/A "mountpoint sourcetype of %s is %d, skipping ...\n"),
2N/A fs_name, sourcetype);
2N/A
2N/A goto next;
2N/A }
2N/A
2N/A /*
2N/A * Check to see if the mountpoint we need to mount at
2N/A * exists and if not, create it.
2N/A */
2N/A errno = 0;
2N/A if (mkdirp(mountpoint,
2N/A S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1 &&
2N/A errno != EEXIST) {
2N/A err = errno;
2N/A be_print_err(gettext("be_mount_callback: failed to "
2N/A "create mount point %s\n"), mountpoint);
2N/A ZFS_CLOSE(zhp);
2N/A return (errno_to_be_err(err));
2N/A }
2N/A
2N/A /* Temporarily mount this filesystem */
2N/A (void) snprintf(options, sizeof (options), MNTOPT_ZFS_MOUNTPOINT "=%s",
2N/A mountpoint);
2N/A if (zfs_mount(zhp, options, 0) != 0) {
2N/A be_print_err(gettext("be_mount_callback: failed to "
2N/A "mount dataset %s at %s: %s\n"), fs_name, mountpoint,
2N/A libzfs_error_description(g_zfs));
2N/A ZFS_CLOSE(zhp);
2N/A return (BE_ERR_MOUNT);
2N/A }
2N/A
2N/Anext:
2N/A /* Iterate through this dataset's children and mount them */
2N/A if ((ret = zfs_iter_filesystems(zhp, be_mount_callback,
2N/A altroot)) != 0) {
2N/A ZFS_CLOSE(zhp);
2N/A return (ret);
2N/A }
2N/A
2N/A
2N/A ZFS_CLOSE(zhp);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_unmount_callback
2N/A * Description: Callback function used to iterate through all of a BE's
2N/A * subordinate file systems and to unmount them.
2N/A * Parameters:
2N/A * zhp - zfs_handle_t pointer to current file system being
2N/A * processed.
2N/A * data - pointer to the mountpoint of where BE is mounted.
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_unmount_callback(zfs_handle_t *zhp, void *data)
2N/A{
2N/A be_unmount_data_t *ud = data;
2N/A const char *fs_name = zfs_get_name(zhp);
2N/A int ret = 0;
2N/A
2N/A /* Iterate down this dataset's children first */
2N/A if (zfs_iter_filesystems(zhp, be_unmount_callback, ud)) {
2N/A ret = BE_ERR_UMOUNT;
2N/A goto done;
2N/A }
2N/A
2N/A /* Is dataset even mounted ? */
2N/A if (!zfs_is_mounted(zhp, NULL))
2N/A goto done;
2N/A
2N/A /* Unmount this file system */
2N/A if (zfs_unmount(zhp, NULL, ud->force ? MS_FORCE : 0) != 0) {
2N/A be_print_err(gettext("be_unmount_callback: "
2N/A "failed to unmount %s: %s\n"), fs_name,
2N/A libzfs_error_description(g_zfs));
2N/A ret = zfs_err_to_be_err(g_zfs);
2N/A }
2N/A
2N/Adone:
2N/A /* Set this filesystem's 'canmount' property to 'noauto' */
2N/A if (zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_CANMOUNT), "noauto")) {
2N/A be_print_err(gettext("be_unmount_callback: "
2N/A "failed to set canmount to 'noauto' (%s)\n"), fs_name);
2N/A if (ret == 0)
2N/A ret = BE_ERR_ZFS;
2N/A }
2N/A
2N/A ZFS_CLOSE(zhp);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_get_legacy_fs_callback
2N/A * Description: The callback function is used to iterate through all
2N/A * non-shared file systems of a BE, finding ones that have
2N/A * a legacy mountpoint and an entry in the BE's vfstab.
2N/A * It adds these file systems to the callback data.
2N/A * Parameters:
2N/A * zhp - zfs_handle_t pointer to current file system being
2N/A * processed.
2N/A * data - be_fs_list_data_t pointer
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_get_legacy_fs_callback(zfs_handle_t *zhp, void *data)
2N/A{
2N/A be_fs_list_data_t *fld = data;
2N/A const char *fs_name = zfs_get_name(zhp);
2N/A char zhp_mountpoint[MAXPATHLEN];
2N/A char mountpoint[MAXPATHLEN];
2N/A int ret = 0;
2N/A
2N/A /* Get this dataset's persistent mountpoint property */
2N/A if (zfs_prop_get_persistent(zhp, ZFS_PROP_MOUNTPOINT, zhp_mountpoint,
2N/A sizeof (zhp_mountpoint), NULL, NULL, 0, B_FALSE, NULL) != 0) {
2N/A be_print_err(gettext("be_get_legacy_fs_callback: "
2N/A "failed to get mountpoint for %s: %s\n"),
2N/A fs_name, 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 /*
2N/A * If mountpoint is legacy, try to get its mountpoint from this BE's
2N/A * vfstab. If it exists in the vfstab, add this file system to the
2N/A * callback data.
2N/A */
2N/A if (strcmp(zhp_mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
2N/A if (get_mountpoint_from_vfstab(fld->altroot, fs_name,
2N/A mountpoint, sizeof (mountpoint), B_FALSE) != BE_SUCCESS) {
2N/A be_print_err(gettext("be_get_legacy_fs_callback: "
2N/A "no entry for %s in vfstab, "
2N/A "skipping ...\n"), fs_name);
2N/A
2N/A goto next;
2N/A }
2N/A
2N/A /* Record file system into the callback data. */
2N/A if (add_to_fs_list(fld, zfs_get_name(zhp)) != BE_SUCCESS) {
2N/A be_print_err(gettext("be_get_legacy_fs_callback: "
2N/A "failed to add %s to fs list\n"), mountpoint);
2N/A ZFS_CLOSE(zhp);
2N/A return (BE_ERR_NOMEM);
2N/A }
2N/A }
2N/A
2N/Anext:
2N/A /* Iterate through this dataset's children file systems */
2N/A if ((ret = zfs_iter_filesystems(zhp, be_get_legacy_fs_callback,
2N/A fld)) != 0) {
2N/A ZFS_CLOSE(zhp);
2N/A return (ret);
2N/A }
2N/A ZFS_CLOSE(zhp);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Function: add_to_fs_list
2N/A * Description: Function used to add a file system to the fs_list array in
2N/A * a be_fs_list_data_t structure.
2N/A * Parameters:
2N/A * fld - be_fs_list_data_t pointer
2N/A * fs - file system to add
2N/A * Returns:
2N/A * BE_SUCCESS - Success
2N/A * 1 - Failure
2N/A * Scope:
2N/A * Private
2N/A */
2N/Astatic int
2N/Aadd_to_fs_list(be_fs_list_data_t *fld, const char *fs)
2N/A{
2N/A if (fld == NULL || fs == NULL)
2N/A return (1);
2N/A
2N/A if ((fld->fs_list = (char **)realloc(fld->fs_list,
2N/A sizeof (char *)*(fld->fs_num + 1))) == NULL) {
2N/A be_print_err(gettext("add_to_fs_list: "
2N/A "memory allocation failed\n"));
2N/A return (1);
2N/A }
2N/A
2N/A if ((fld->fs_list[fld->fs_num++] = strdup(fs)) == NULL) {
2N/A be_print_err(gettext("add_to_fs_list: "
2N/A "memory allocation failed\n"));
2N/A return (1);
2N/A }
2N/A
2N/A return (BE_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * Function: zpool_shared_fs_callback
2N/A * Description: Callback function used to iterate through all existing pools
2N/A * to find and mount all shared filesystems. This function
2N/A * processes the pool's "pool data" dataset, then uses
2N/A * iter_shared_fs_callback to iterate through the pool's
2N/A * datasets.
2N/A * Parameters:
2N/A * zlp - zpool_handle_t pointer to the current pool being
2N/A * looked at.
2N/A * data - be_mount_data_t pointer
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/Azpool_shared_fs_callback(zpool_handle_t *zlp, void *data)
2N/A{
2N/A be_mount_data_t *md = data;
2N/A zfs_handle_t *zhp = NULL;
2N/A const char *zpool = zpool_get_name(zlp);
2N/A int ret = 0;
2N/A
2N/A /*
2N/A * Get handle to pool's "pool data" dataset
2N/A */
2N/A if ((zhp = zfs_open(g_zfs, zpool, ZFS_TYPE_FILESYSTEM)) == NULL) {
2N/A be_print_err(gettext("zpool_shared_fs: "
2N/A "failed to open pool dataset %s: %s\n"), zpool,
2N/A libzfs_error_description(g_zfs));
2N/A ret = zfs_err_to_be_err(g_zfs);
2N/A zpool_close(zlp);
2N/A return (ret);
2N/A }
2N/A
2N/A /* Process this pool's "pool data" dataset */
2N/A (void) loopback_mount_shared_fs(zhp, md);
2N/A
2N/A /* Interate through this pool's children */
2N/A (void) zfs_iter_filesystems(zhp, iter_shared_fs_callback, md);
2N/A
2N/A ZFS_CLOSE(zhp);
2N/A zpool_close(zlp);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Function: iter_shared_fs_callback
2N/A * Description: Callback function used to iterate through a pool's datasets
2N/A * to find and mount all shared filesystems. It makes sure to
2N/A * find the BE container dataset of the pool, if it exists, and
2N/A * does not process and iterate down that path.
2N/A *
2N/A * Note - This function iterates linearly down the
2N/A * hierarchical dataset paths and mounts things as it goes
2N/A * along. It does not make sure that something deeper down
2N/A * a dataset path has an interim mountpoint for something
2N/A * processed earlier.
2N/A *
2N/A * Parameters:
2N/A * zhp - zfs_handle_t pointer to the current dataset being
2N/A * processed.
2N/A * data - be_mount_data_t pointer
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/Aiter_shared_fs_callback(zfs_handle_t *zhp, void *data)
2N/A{
2N/A be_mount_data_t *md = data;
2N/A const char *name = zfs_get_name(zhp);
2N/A char container_ds[MAXPATHLEN];
2N/A char tmp_name[MAXPATHLEN];
2N/A char *pool;
2N/A
2N/A /* Get the pool's name */
2N/A (void) strlcpy(tmp_name, name, sizeof (tmp_name));
2N/A pool = strtok(tmp_name, "/");
2N/A
2N/A if (pool) {
2N/A /* Get the name of this pool's container dataset */
2N/A be_make_container_ds(pool, container_ds,
2N/A sizeof (container_ds));
2N/A
2N/A /*
2N/A * If what we're processing is this pool's BE container
2N/A * dataset, skip it.
2N/A */
2N/A if (strcmp(name, container_ds) == 0) {
2N/A ZFS_CLOSE(zhp);
2N/A return (0);
2N/A }
2N/A } else {
2N/A /* Getting the pool name failed, return error */
2N/A be_print_err(gettext("iter_shared_fs_callback: "
2N/A "failed to get pool name from %s\n"), name);
2N/A ZFS_CLOSE(zhp);
2N/A return (BE_ERR_POOL_NOENT);
2N/A }
2N/A
2N/A /* Mount this shared filesystem */
2N/A (void) loopback_mount_shared_fs(zhp, md);
2N/A
2N/A /* Iterate this dataset's children file systems */
2N/A (void) zfs_iter_filesystems(zhp, iter_shared_fs_callback, md);
2N/A ZFS_CLOSE(zhp);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Function: loopback_mount_shared_fs
2N/A * Description: This function loopback mounts a file system into the altroot
2N/A * area of the BE being mounted. Since these are shared file
2N/A * systems, they are expected to be already mounted for the
2N/A * current BE, and this function just loopback mounts them into
2N/A * the BE mountpoint. If they are not mounted for the current
2N/A * live system, they are skipped and not mounted into the BE
2N/A * we're mounting.
2N/A * Parameters:
2N/A * zhp - zfs_handle_t pointer to the dataset to loopback mount
2N/A * md - be_mount_data_t pointer
2N/A * Returns:
2N/A * BE_SUCCESS - Success
2N/A * be_errno_t - Failure
2N/A * Scope:
2N/A * Private
2N/A */
2N/Astatic int
2N/Aloopback_mount_shared_fs(zfs_handle_t *zhp, be_mount_data_t *md)
2N/A{
2N/A char zhp_mountpoint[MAXPATHLEN];
2N/A char mountpoint[MAXPATHLEN];
2N/A char *mp = NULL;
2N/A char optstr[MAX_MNTOPT_STR];
2N/A int mflag = MS_OPTIONSTR;
2N/A int err;
2N/A
2N/A /*
2N/A * Check if file system is currently mounted and not delegated
2N/A * to a non-global zone (if we're in the global zone)
2N/A */
2N/A if (zfs_is_mounted(zhp, &mp) && (getzoneid() != GLOBAL_ZONEID ||
2N/A !zfs_prop_get_int(zhp, ZFS_PROP_ZONED))) {
2N/A /*
2N/A * If we didn't get a mountpoint from the zfs_is_mounted call,
2N/A * get it from the mountpoint property.
2N/A */
2N/A if (mp == NULL) {
2N/A if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
2N/A zhp_mountpoint, sizeof (zhp_mountpoint), NULL,
2N/A NULL, 0, B_FALSE) != 0) {
2N/A be_print_err(
2N/A gettext("loopback_mount_shared_fs: "
2N/A "failed to get mountpoint property\n"));
2N/A return (BE_ERR_ZFS);
2N/A }
2N/A } else {
2N/A (void) strlcpy(zhp_mountpoint, mp,
2N/A sizeof (zhp_mountpoint));
2N/A free(mp);
2N/A }
2N/A
2N/A (void) snprintf(mountpoint, sizeof (mountpoint), "%s%s",
2N/A md->altroot, zhp_mountpoint);
2N/A
2N/A /* Mount it read-only if read-write was not requested */
2N/A if (!md->shared_rw) {
2N/A mflag |= MS_RDONLY;
2N/A }
2N/A
2N/A /* Add the "nosub" option to the mount options string */
2N/A (void) strlcpy(optstr, MNTOPT_NOSUB, sizeof (optstr));
2N/A
2N/A /* Loopback mount this dataset at the altroot */
2N/A if (mount(zhp_mountpoint, mountpoint, mflag, MNTTYPE_LOFS,
2N/A NULL, 0, optstr, sizeof (optstr)) != 0) {
2N/A err = errno;
2N/A be_print_err(gettext("loopback_mount_shared_fs: "
2N/A "failed to loopback mount %s at %s: %s\n"),
2N/A zhp_mountpoint, mountpoint, strerror(err));
2N/A return (BE_ERR_MOUNT);
2N/A }
2N/A }
2N/A
2N/A return (BE_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * Function: loopback_mount_zonepath
2N/A * Description: This function loopback mounts a zonepath into the altroot
2N/A * area of the BE being mounted. Since these are shared file
2N/A * systems, they are expected to be already mounted for the
2N/A * current BE, and this function just loopback mounts them into
2N/A * the BE mountpoint.
2N/A * Parameters:
2N/A * zonepath - pointer to zone path in the current BE
2N/A * md - be_mount_data_t pointer
2N/A * use_tmpfs - mount using tmpfs instead of lofs
2N/A * Returns:
2N/A * BE_SUCCESS - Success
2N/A * be_errno_t - Failure
2N/A * Scope:
2N/A * Private
2N/A */
2N/Astatic int
2N/Aloopback_mount_zonepath(
2N/A const char *zonepath,
2N/A be_mount_data_t *md,
2N/A boolean_t use_tmpfs)
2N/A{
2N/A FILE *fp = (FILE *)NULL;
2N/A struct stat st;
2N/A char *p;
2N/A char *p1;
2N/A char *parent_dir;
2N/A struct extmnttab extmtab;
2N/A dev_t dev = NODEV;
2N/A char *parentmnt;
2N/A char alt_parentmnt[MAXPATHLEN];
2N/A struct mnttab mntref;
2N/A char altzonepath[MAXPATHLEN];
2N/A char optstr[MAX_MNTOPT_STR];
2N/A int mflag = MS_OPTIONSTR;
2N/A int ret;
2N/A int err;
2N/A
2N/A fp = fopen(MNTTAB, "r");
2N/A if (fp == NULL) {
2N/A err = errno;
2N/A be_print_err(gettext("loopback_mount_zonepath: "
2N/A "failed to open /etc/mnttab\n"));
2N/A return (errno_to_be_err(err));
2N/A }
2N/A
2N/A /*
2N/A * before attempting the loopback mount of zonepath under altroot,
2N/A * we need to make sure that all intermediate file systems in the
2N/A * zone path are also mounted under altroot
2N/A */
2N/A
2N/A /* get the parent directory for zonepath */
2N/A p = strrchr(zonepath, '/');
2N/A if (p != NULL && p != zonepath) {
2N/A if ((parent_dir = (char *)calloc(sizeof (char),
2N/A p - zonepath + 1)) == NULL) {
2N/A ret = BE_ERR_NOMEM;
2N/A goto done;
2N/A }
2N/A (void) strlcpy(parent_dir, zonepath, p - zonepath + 1);
2N/A if (stat(parent_dir, &st) < 0) {
2N/A ret = errno_to_be_err(errno);
2N/A be_print_err(gettext("loopback_mount_zonepath: "
2N/A "failed to stat %s"),
2N/A parent_dir);
2N/A free(parent_dir);
2N/A goto done;
2N/A }
2N/A free(parent_dir);
2N/A
2N/A /*
2N/A * After the above stat call, st.st_dev contains ID of the
2N/A * device over which parent dir resides.
2N/A * Now, search mnttab and find mount point of parent dir device.
2N/A */
2N/A
2N/A resetmnttab(fp);
2N/A while (getextmntent(fp, &extmtab, sizeof (extmtab)) == 0) {
2N/A dev = makedev(extmtab.mnt_major, extmtab.mnt_minor);
2N/A if (st.st_dev == dev && strcmp(extmtab.mnt_fstype,
2N/A MNTTYPE_ZFS) == 0) {
2N/A p1 = strchr(extmtab.mnt_special, '/');
2N/A if (p1 == NULL || strncmp(p1 + 1,
2N/A BE_CONTAINER_DS_NAME, 4) != 0 ||
2N/A (*(p1 + 5) != '/' && *(p1 + 5) != '\0')) {
2N/A /*
2N/A * if parent dir is in a shared file
2N/A * system, check whether it is already
2N/A * loopback mounted under altroot or
2N/A * not. It would have been mounted
2N/A * already under altroot if it is in
2N/A * a non-shared filesystem.
2N/A */
2N/A parentmnt = strdup(extmtab.mnt_mountp);
2N/A (void) snprintf(alt_parentmnt,
2N/A sizeof (alt_parentmnt), "%s%s",
2N/A md->altroot, parentmnt);
2N/A mntref.mnt_mountp = alt_parentmnt;
2N/A mntref.mnt_special = parentmnt;
2N/A mntref.mnt_fstype = MNTTYPE_LOFS;
2N/A mntref.mnt_mntopts = NULL;
2N/A mntref.mnt_time = NULL;
2N/A resetmnttab(fp);
2N/A if (getmntany(fp, (struct mnttab *)
2N/A &extmtab, &mntref) != 0) {
2N/A ret = loopback_mount_zonepath(
2N/A parentmnt, md, B_FALSE);
2N/A if (ret != BE_SUCCESS) {
2N/A free(parentmnt);
2N/A goto done;
2N/A }
2N/A }
2N/A free(parentmnt);
2N/A }
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A
2N/A
2N/A if (!md->shared_rw) {
2N/A mflag |= MS_RDONLY;
2N/A }
2N/A
2N/A (void) snprintf(altzonepath, sizeof (altzonepath), "%s%s",
2N/A md->altroot, zonepath);
2N/A
2N/A /* Add the "nosub" option to the mount options string */
2N/A (void) strlcpy(optstr, MNTOPT_NOSUB, sizeof (optstr));
2N/A
2N/A if (use_tmpfs) {
2N/A if (mount("swap", altzonepath, MS_OPTIONSTR, MNTTYPE_TMPFS,
2N/A NULL, 0, optstr, sizeof (optstr)) != 0) {
2N/A err = errno;
2N/A be_print_err(gettext("loopback_mount_zonepath: "
2N/A "failed to tmpfs mount zone path %s\n"),
2N/A altzonepath);
2N/A ret = (errno_to_be_err(err));
2N/A goto done;
2N/A }
2N/A /*
2N/A * We need to fixup the permissions for the zonepath
2N/A * we just mounted in the altroot so that the zoneroot
2N/A * isn't available to everyone on the system
2N/A */
2N/A if (chmod(altzonepath, S_IRWXU) != 0) {
2N/A err = errno;
2N/A be_print_err(gettext("loopback_mount_zonepath: "
2N/A "failed to chmod zone path %s\n"),
2N/A altzonepath);
2N/A ret = (errno_to_be_err(err));
2N/A goto done;
2N/A }
2N/A } else {
2N/A /* Loopback mount this dataset at the altroot */
2N/A if (mount(zonepath, altzonepath, mflag, MNTTYPE_LOFS,
2N/A NULL, 0, optstr, sizeof (optstr)) != 0) {
2N/A err = errno;
2N/A be_print_err(gettext("loopback_mount_zonepath: "
2N/A "failed to loopback mount %s at %s: %s\n"),
2N/A zonepath, altzonepath, strerror(err));
2N/A ret = BE_ERR_MOUNT;
2N/A goto done;
2N/A }
2N/A }
2N/A ret = BE_SUCCESS;
2N/A
2N/Adone :
2N/A (void) fclose(fp);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Function: unmount_shared_fs
2N/A * Description: This function iterates through the mnttab and finds all
2N/A * loopback mount entries that reside within the altroot of
2N/A * where the BE is mounted, and unmounts it.
2N/A * Parameters:
2N/A * ud - be_unmount_data_t pointer
2N/A * Returns:
2N/A * BE_SUCCESS - Success
2N/A * be_errno_t - Failure
2N/A * Scope:
2N/A * Private
2N/A */
2N/Astatic int
2N/Aunmount_shared_fs(be_unmount_data_t *ud)
2N/A{
2N/A FILE *fp = NULL;
2N/A struct mnttab *table = NULL;
2N/A struct mnttab ent;
2N/A struct mnttab *entp = NULL;
2N/A size_t size = 0;
2N/A int read_chunk = 32;
2N/A int i;
2N/A int altroot_len;
2N/A int err = 0;
2N/A
2N/A errno = 0;
2N/A
2N/A /* Read in the mnttab into a table */
2N/A if ((fp = fopen(MNTTAB, "r")) == NULL) {
2N/A err = errno;
2N/A be_print_err(gettext("unmount_shared_fs: "
2N/A "failed to open mnttab\n"));
2N/A return (errno_to_be_err(err));
2N/A }
2N/A
2N/A while (getmntent(fp, &ent) == 0) {
2N/A if (size % read_chunk == 0) {
2N/A table = (struct mnttab *)realloc(table,
2N/A (size + read_chunk) * sizeof (ent));
2N/A }
2N/A entp = &table[size++];
2N/A
2N/A /*
2N/A * Copy over the current mnttab entry into our table,
2N/A * copying only the fields that we care about.
2N/A */
2N/A (void) memset(entp, 0, sizeof (*entp));
2N/A if ((entp->mnt_mountp = strdup(ent.mnt_mountp)) == NULL ||
2N/A (entp->mnt_fstype = strdup(ent.mnt_fstype)) == NULL) {
2N/A be_print_err(gettext("unmount_shared_fs: "
2N/A "memory allocation failed\n"));
2N/A return (BE_ERR_NOMEM);
2N/A }
2N/A }
2N/A (void) fclose(fp);
2N/A
2N/A /*
2N/A * Process the mnttab entries in reverse order, looking for
2N/A * loopback mount entries mounted under our altroot.
2N/A */
2N/A altroot_len = strlen(ud->altroot);
2N/A for (i = size; i > 0; i--) {
2N/A entp = &table[i - 1];
2N/A
2N/A /* If not of type lofs, skip */
2N/A if (strcmp(entp->mnt_fstype, MNTTYPE_LOFS) != 0)
2N/A continue;
2N/A
2N/A /* If inside the altroot, unmount it */
2N/A if (strncmp(entp->mnt_mountp, ud->altroot, altroot_len) == 0 &&
2N/A entp->mnt_mountp[altroot_len] == '/') {
2N/A if (umount(entp->mnt_mountp) != 0) {
2N/A err = errno;
2N/A if (err == EBUSY) {
2N/A (void) sleep(1);
2N/A err = errno = 0;
2N/A if (umount(entp->mnt_mountp) != 0)
2N/A err = errno;
2N/A }
2N/A if (err != 0) {
2N/A be_print_err(gettext(
2N/A "unmount_shared_fs: "
2N/A "failed to unmount shared file "
2N/A "system %s: %s\n"),
2N/A entp->mnt_mountp, strerror(err));
2N/A return (errno_to_be_err(err));
2N/A }
2N/A }
2N/A }
2N/A }
2N/A
2N/A return (BE_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * Function: get_mountpoint_from_vfstab
2N/A * Description: This function digs into the vfstab in the given altroot,
2N/A * and searches for an entry for the fs passed in. If found,
2N/A * it returns the mountpoint of that fs in the mountpoint
2N/A * buffer passed in. If the get_alt_mountpoint flag is set,
2N/A * it returns the mountpoint with the altroot prepended.
2N/A * Parameters:
2N/A * altroot - pointer to the alternate root location
2N/A * fs - pointer to the file system name to look for in the
2N/A * vfstab in altroot
2N/A * mountpoint - pointer to buffer of where the mountpoint of
2N/A * fs will be returned.
2N/A * size_mp - size of mountpoint argument
2N/A * get_alt_mountpoint - flag to indicate whether or not the
2N/A * mountpoint should be populated with the altroot
2N/A * prepended.
2N/A * Returns:
2N/A * BE_SUCCESS - Success
2N/A * 1 - Failure
2N/A * Scope:
2N/A * Private
2N/A */
2N/Astatic int
2N/Aget_mountpoint_from_vfstab(char *altroot, const char *fs, char *mountpoint,
2N/A size_t size_mp, boolean_t get_alt_mountpoint)
2N/A{
2N/A struct vfstab vp;
2N/A FILE *fp = NULL;
2N/A char alt_vfstab[MAXPATHLEN];
2N/A
2N/A /* Generate path to alternate root vfstab */
2N/A (void) snprintf(alt_vfstab, sizeof (alt_vfstab), "%s/etc/vfstab",
2N/A altroot);
2N/A
2N/A /* Open alternate root vfstab */
2N/A if ((fp = fopen(alt_vfstab, "r")) == NULL) {
2N/A be_print_err(gettext("get_mountpoint_from_vfstab: "
2N/A "failed to open vfstab (%s)\n"), alt_vfstab);
2N/A return (1);
2N/A }
2N/A
2N/A if (getvfsspec(fp, &vp, (char *)fs) == 0) {
2N/A /*
2N/A * Found entry for fs, grab its mountpoint.
2N/A * If the flag to prepend the altroot into the mountpoint
2N/A * is set, prepend it. Otherwise, just return the mountpoint.
2N/A */
2N/A if (get_alt_mountpoint) {
2N/A (void) snprintf(mountpoint, size_mp, "%s%s", altroot,
2N/A vp.vfs_mountp);
2N/A } else {
2N/A (void) strlcpy(mountpoint, vp.vfs_mountp, size_mp);
2N/A }
2N/A } else {
2N/A (void) fclose(fp);
2N/A return (1);
2N/A }
2N/A
2N/A (void) fclose(fp);
2N/A
2N/A return (BE_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * Function: fix_mountpoint_callback
2N/A * Description: This callback function is used to iterate through a BE's
2N/A * children filesystems to check if its mountpoint is currently
2N/A * set to be mounted at some specified altroot. If so, fix it by
2N/A * removing altroot from the beginning of its mountpoint.
2N/A *
2N/A * Note - There's no way to tell if a child filesystem's
2N/A * mountpoint isn't broken, and just happens to begin with
2N/A * the altroot we're looking for. In this case, this function
2N/A * will errantly remove the altroot portion from the beginning
2N/A * of this filesystem's mountpoint.
2N/A *
2N/A * Parameters:
2N/A * zhp - zfs_handle_t pointer to filesystem being processed.
2N/A * data - altroot of where BE is to be mounted.
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/Afix_mountpoint_callback(zfs_handle_t *zhp, void *data)
2N/A{
2N/A zprop_source_t sourcetype;
2N/A char source[ZFS_MAXNAMELEN];
2N/A char mountpoint[MAXPATHLEN];
2N/A char *zhp_mountpoint = NULL;
2N/A char *altroot = data;
2N/A int ret = 0;
2N/A
2N/A /* Get dataset's persistent mountpoint and source values */
2N/A if (zfs_prop_get_persistent(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
2N/A sizeof (mountpoint), &sourcetype, source, sizeof (source),
2N/A B_FALSE, NULL) != 0) {
2N/A be_print_err(gettext("fix_mountpoint_callback: "
2N/A "failed to get mountpoint and sourcetype for %s\n"),
2N/A zfs_get_name(zhp));
2N/A ZFS_CLOSE(zhp);
2N/A return (BE_ERR_ZFS);
2N/A }
2N/A
2N/A /*
2N/A * If the mountpoint is not inherited and the mountpoint is not
2N/A * 'legacy', this file system potentially needs its mountpoint
2N/A * fixed.
2N/A */
2N/A if (!(sourcetype & ZPROP_SRC_INHERITED) &&
2N/A strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) != 0) {
2N/A
2N/A /*
2N/A * Check if this file system's current mountpoint is
2N/A * under the altroot we're fixing it against.
2N/A */
2N/A if (strncmp(mountpoint, altroot, strlen(altroot)) == 0 &&
2N/A mountpoint[strlen(altroot)] == '/') {
2N/A
2N/A /*
2N/A * Get this dataset's mountpoint relative to the
2N/A * altroot.
2N/A */
2N/A zhp_mountpoint = mountpoint + strlen(altroot);
2N/A
2N/A /* Fix this dataset's mountpoint value */
2N/A if (zfs_prop_set(zhp,
2N/A zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
2N/A zhp_mountpoint)) {
2N/A be_print_err(gettext("fix_mountpoint_callback: "
2N/A "failed to set mountpoint for %s to "
2N/A "%s: %s\n"), zfs_get_name(zhp),
2N/A zhp_mountpoint,
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 }
2N/A
2N/A /* Iterate through this dataset's children and fix them */
2N/A if ((ret = zfs_iter_filesystems(zhp, fix_mountpoint_callback,
2N/A altroot)) != 0) {
2N/A ZFS_CLOSE(zhp);
2N/A return (ret);
2N/A }
2N/A
2N/A
2N/A ZFS_CLOSE(zhp);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_mount_root
2N/A * Description: This function mounts the root dataset of a BE at the
2N/A * specified altroot.
2N/A * Parameters:
2N/A * zhp - zfs_handle_t pointer to root dataset of a BE that is
2N/A * to be mounted at altroot.
2N/A * altroot - location of where to mount the BE root.
2N/A * Return:
2N/A * BE_SUCCESS - Success
2N/A * be_errno_t - Failure
2N/A * Scope:
2N/A * Private
2N/A */
2N/Astatic int
2N/Abe_mount_root(zfs_handle_t *zhp, char *altroot)
2N/A{
2N/A char options[MAXPATHLEN + sizeof (MNTOPT_ZFS_MOUNTPOINT) + 1];
2N/A int err;
2N/A
2N/A /*
2N/A * Check to see if the mountpoint we've been given exists and if
2N/A * not, create it.
2N/A */
2N/A errno = 0;
2N/A if (mkdirp(altroot,
2N/A S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1 &&
2N/A errno != EEXIST) {
2N/A err = errno;
2N/A be_print_err(gettext("be_mount_root: failed to create "
2N/A "alternate mountpoint %s\n"), altroot);
2N/A return (errno_to_be_err(err));
2N/A }
2N/A
2N/A /* Temporary mount the BE's root filesystem */
2N/A (void) snprintf(options, sizeof (options), MNTOPT_ZFS_MOUNTPOINT "=%s",
2N/A altroot);
2N/A if (zfs_mount(zhp, options, 0) != 0) {
2N/A be_print_err(gettext("be_mount_root: failed to "
2N/A "mount dataset %s at %s: %s\n"), zfs_get_name(zhp),
2N/A altroot, libzfs_error_description(g_zfs));
2N/A return (zfs_err_to_be_err(g_zfs));
2N/A }
2N/A
2N/A return (BE_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_unmount_root
2N/A * Description: This function unmounts the root dataset of a BE, but before
2N/A * unmounting, it looks at the BE's vfstab to determine
2N/A * if the root dataset mountpoint should be left as 'legacy'
2N/A * or '/'. If the vfstab contains an entry for this root
2N/A * dataset with a mountpoint of '/', it sets the mountpoint
2N/A * property to 'legacy'.
2N/A *
2N/A * Parameters:
2N/A * zhp - zfs_handle_t pointer of the BE root dataset that
2N/A * is currently mounted.
2N/A * ud - be_unmount_data_t pointer providing unmount data
2N/A * for the given BE root dataset.
2N/A * Returns:
2N/A * BE_SUCCESS - Success
2N/A * be_errno_t - Failure
2N/A * Scope:
2N/A * Private
2N/A */
2N/Astatic int
2N/Abe_unmount_root(zfs_handle_t *zhp, be_unmount_data_t *ud)
2N/A{
2N/A char mountpoint[MAXPATHLEN];
2N/A boolean_t is_legacy = B_FALSE;
2N/A
2N/A /* See if this is a legacy mounted root */
2N/A if (get_mountpoint_from_vfstab(ud->altroot, zfs_get_name(zhp),
2N/A mountpoint, sizeof (mountpoint), B_FALSE) == BE_SUCCESS &&
2N/A strcmp(mountpoint, "/") == 0) {
2N/A is_legacy = B_TRUE;
2N/A }
2N/A
2N/A /* Unmount the dataset */
2N/A if (zfs_unmount(zhp, NULL, ud->force ? MS_FORCE : 0) != 0) {
2N/A be_print_err(gettext("be_unmount_root: failed to "
2N/A "unmount BE root dataset %s: %s\n"), zfs_get_name(zhp),
2N/A libzfs_error_description(g_zfs));
2N/A return (zfs_err_to_be_err(g_zfs));
2N/A }
2N/A
2N/A /* Set canmount property for this BE's root filesystem to noauto */
2N/A if (zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_CANMOUNT), "noauto")
2N/A != 0) {
2N/A be_print_err(gettext("be_unmount_root: failed to "
2N/A "set canmount property for %s to 'noauto': %s\n"),
2N/A zfs_get_name(zhp), libzfs_error_description(g_zfs));
2N/A return (zfs_err_to_be_err(g_zfs));
2N/A }
2N/A
2N/A /*
2N/A * Set mountpoint for BE's root dataset back to '/', or 'legacy'
2N/A * if its a legacy mounted root.
2N/A */
2N/A if (zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
2N/A is_legacy ? ZFS_MOUNTPOINT_LEGACY : "/") != 0) {
2N/A be_print_err(gettext("be_unmount_root: failed to "
2N/A "set mountpoint of %s to %s\n"), zfs_get_name(zhp),
2N/A is_legacy ? ZFS_MOUNTPOINT_LEGACY : "/");
2N/A return (zfs_err_to_be_err(g_zfs));
2N/A }
2N/A
2N/A return (BE_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * Function: fix_mountpoint
2N/A * Description: This function checks the mountpoint of an unmounted BE to make
2N/A * sure that it is set to either 'legacy' or '/'. If it's not,
2N/A * then we're in a situation where an unmounted BE has some random
2N/A * mountpoint set for it. (This could happen if the system was
2N/A * rebooted while an inactive BE was mounted). This function
2N/A * attempts to fix its mountpoints.
2N/A * Parameters:
2N/A * zhp - zfs_handle_t pointer to root dataset of the BE
2N/A * whose mountpoint needs to be checked.
2N/A * Return:
2N/A * BE_SUCCESS - Success
2N/A * be_errno_t - Failure
2N/A * Scope:
2N/A * Private
2N/A */
2N/Astatic int
2N/Afix_mountpoint(zfs_handle_t *zhp)
2N/A{
2N/A be_unmount_data_t ud = { 0 };
2N/A char *altroot = NULL;
2N/A char mountpoint[MAXPATHLEN];
2N/A int ret = BE_SUCCESS;
2N/A
2N/A /*
2N/A * Record what this BE's root dataset mountpoint property is currently
2N/A * set to.
2N/A */
2N/A if (zfs_prop_get_persistent(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
2N/A sizeof (mountpoint), NULL, NULL, 0, B_FALSE, NULL) != 0) {
2N/A be_print_err(gettext("fix_mountpoint: failed to get "
2N/A "mountpoint property of (%s): %s\n"), zfs_get_name(zhp),
2N/A libzfs_error_description(g_zfs));
2N/A return (BE_ERR_ZFS);
2N/A }
2N/A
2N/A /*
2N/A * If the root dataset mountpoint is set to 'legacy' or '/', we're okay.
2N/A */
2N/A if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0 ||
2N/A strcmp(mountpoint, "/") == 0) {
2N/A return (BE_SUCCESS);
2N/A }
2N/A
2N/A /*
2N/A * Iterate through this BE's children datasets and fix
2N/A * them if they need fixing.
2N/A */
2N/A if (zfs_iter_filesystems(zhp, fix_mountpoint_callback, mountpoint)
2N/A != 0) {
2N/A return (BE_ERR_ZFS);
2N/A }
2N/A
2N/A /*
2N/A * The process of mounting and unmounting the root file system
2N/A * will fix its mountpoint to correctly be either 'legacy' or '/'
2N/A * since be_unmount_root will do the right thing by looking at
2N/A * its vfstab.
2N/A */
2N/A
2N/A /* Generate temporary altroot to mount the root file system */
2N/A if ((ret = be_make_tmp_mountpoint(&altroot)) != BE_SUCCESS) {
2N/A be_print_err(gettext("fix_mountpoint: failed to "
2N/A "make temporary mountpoint\n"));
2N/A return (ret);
2N/A }
2N/A
2N/A /* Mount and unmount the root. */
2N/A if ((ret = be_mount_root(zhp, altroot)) != BE_SUCCESS) {
2N/A be_print_err(gettext("fix_mountpoint: failed to "
2N/A "mount BE root file system\n"));
2N/A goto cleanup;
2N/A }
2N/A ud.altroot = altroot;
2N/A if ((ret = be_unmount_root(zhp, &ud)) != BE_SUCCESS) {
2N/A be_print_err(gettext("fix_mountpoint: failed to "
2N/A "unmount BE root file system\n"));
2N/A goto cleanup;
2N/A }
2N/A
2N/Acleanup:
2N/A free(altroot);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_mount_zones
2N/A * Description: This function finds all supported non-global zones in the
2N/A * given global BE and mounts them with respect to where the
2N/A * global BE is currently mounted. The global BE datasets
2N/A * (including its shared datasets) are expected to already
2N/A * be mounted.
2N/A * Parameters:
2N/A * be_zhp - zfs_handle_t pointer to the root dataset of the
2N/A * global BE.
2N/A * md - be_mount_data_t pointer to data for global BE.
2N/A * be_name - name of the BE.
2N/A * Returns:
2N/A * BE_SUCCESS - Success
2N/A * be_errno_t - Failure
2N/A * Scope:
2N/A * Private
2N/A */
2N/Astatic int
2N/Abe_mount_zones(zfs_handle_t *be_zhp, be_mount_data_t *md, const char *be_name)
2N/A{
2N/A zoneBrandList_t *brands = NULL;
2N/A zoneList_t zlst = NULL;
2N/A char *zonename = NULL;
2N/A char *zonepath = NULL;
2N/A char *zonepath_ds = NULL;
2N/A int k;
2N/A int ret = BE_SUCCESS;
2N/A
2N/A z_set_zone_root(md->altroot);
2N/A
2N/A if ((brands = be_get_supported_brandlist()) == NULL) {
2N/A be_print_err(gettext("be_mount_zones: "
2N/A "no supported brands\n"));
2N/A return (BE_SUCCESS);
2N/A }
2N/A
2N/A zlst = z_get_nonglobal_zone_list_by_brand(brands);
2N/A if (zlst == NULL) {
2N/A z_free_brand_list(brands);
2N/A return (BE_SUCCESS);
2N/A }
2N/A
2N/A for (k = 0; (zonename = z_zlist_get_zonename(zlst, k)) != NULL; k++) {
2N/A if (z_zlist_get_current_state(zlst, k) ==
2N/A ZONE_STATE_INSTALLED) {
2N/A zonepath = z_zlist_get_zonepath(zlst, k);
2N/A
2N/A /*
2N/A * Get the dataset of this zonepath in current BE.
2N/A * If not a dataset or otherwise not available skip it
2N/A * but do not fail the BE operation.
2N/A */
2N/A if ((zonepath_ds = be_get_ds_from_dir(zonepath))
2N/A == NULL) {
2N/A be_print_err(gettext("be_mount_zones: "
2N/A "failed to mount zone %s under altroot "
2N/A "%s.\npkg(1) operations which attempt to "
2N/A "modify ABE %s may fail\nUse 'zoneadm -R"
2N/A "%s -z %s mark unavailable' to manually "
2N/A "mark the zone as unavailable.\n"),
2N/A zonename, md->altroot, be_name,
2N/A md->altroot, zonename);
2N/A continue;
2N/A }
2N/A /*
2N/A * Check if this zone is supported based on
2N/A * the dataset of its zonepath
2N/A */
2N/A if (!be_zone_supported(zonepath_ds)) {
2N/A free(zonepath_ds);
2N/A zonepath_ds = NULL;
2N/A continue;
2N/A }
2N/A
2N/A /*
2N/A * if BE's shared file systems are already mounted,
2N/A * zone path dataset would have already been lofs
2N/A * mounted under altroot. Otherwise, we need to do
2N/A * it here.
2N/A */
2N/A if (!md->shared_fs) {
2N/A ret = loopback_mount_zonepath(zonepath, md,
2N/A B_TRUE);
2N/A if (ret != BE_SUCCESS)
2N/A goto done;
2N/A }
2N/A
2N/A /* Mount this zone */
2N/A ret = be_mount_one_zone(be_zhp, md, zonename,
2N/A zonepath, zonepath_ds);
2N/A
2N/A free(zonepath_ds);
2N/A zonepath_ds = NULL;
2N/A
2N/A if (ret != BE_SUCCESS) {
2N/A be_print_err(gettext("be_mount_zones: "
2N/A "failed to mount zone %s under "
2N/A "altroot %s\n"), zonename, md->altroot);
2N/A goto done;
2N/A }
2N/A }
2N/A }
2N/A
2N/Adone:
2N/A z_free_brand_list(brands);
2N/A z_free_zone_list(zlst);
2N/A /*
2N/A * libinstzones caches mnttab and uses cached version for resolving lofs
2N/A * mounts when we call z_resolve_lofs. It creates the cached version
2N/A * when the first call to z_resolve_lofs happens. So, library's cached
2N/A * mnttab doesn't contain entries for lofs mounts created in the above
2N/A * loop. Because of this, subsequent calls to z_resolve_lofs would fail
2N/A * to resolve these lofs mounts. So, here we destroy library's cached
2N/A * mnttab to force its recreation when the next call to z_resolve_lofs
2N/A * happens.
2N/A */
2N/A z_destroyMountTable();
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_unmount_zones
2N/A * Description: This function finds all supported non-global zones in the
2N/A * given mounted global BE and unmounts them.
2N/A * Parameters:
2N/A * ud - unmount_data_t pointer data for the global BE.
2N/A * Returns:
2N/A * BE_SUCCESS - Success
2N/A * be_errno_t - Failure
2N/A * Scope:
2N/A * Private
2N/A */
2N/Astatic int
2N/Abe_unmount_zones(be_unmount_data_t *ud)
2N/A{
2N/A zoneBrandList_t *brands = NULL;
2N/A zoneList_t zlst = NULL;
2N/A char *zonename = NULL;
2N/A char *zonepath = NULL;
2N/A char alt_zonepath[MAXPATHLEN];
2N/A char *zonepath_ds = NULL;
2N/A int k;
2N/A int ret = BE_SUCCESS;
2N/A
2N/A z_set_zone_root(ud->altroot);
2N/A
2N/A if ((brands = be_get_supported_brandlist()) == NULL) {
2N/A be_print_err(gettext("be_unmount_zones: "
2N/A "no supported brands\n"));
2N/A return (BE_SUCCESS);
2N/A }
2N/A
2N/A zlst = z_get_nonglobal_zone_list_by_brand(brands);
2N/A if (zlst == NULL) {
2N/A z_free_brand_list(brands);
2N/A return (BE_SUCCESS);
2N/A }
2N/A
2N/A for (k = 0; (zonename = z_zlist_get_zonename(zlst, k)) != NULL; k++) {
2N/A if (z_zlist_get_current_state(zlst, k) ==
2N/A ZONE_STATE_INSTALLED) {
2N/A zonepath = z_zlist_get_zonepath(zlst, k);
2N/A
2N/A /* Build zone's zonepath wrt the global BE altroot */
2N/A (void) snprintf(alt_zonepath, sizeof (alt_zonepath),
2N/A "%s%s", ud->altroot, zonepath);
2N/A /*
2N/A * Get the dataset of the zonepath. If its not
2N/A * a dataset, skip it.
2N/A *
2N/A * Since the alternate zone path is a tmpfs
2N/A * mount we need to look for the zonepath
2N/A * dataset based on the zonepath not the
2N/A * alt_zonepath.
2N/A */
2N/A if ((zonepath_ds =
2N/A be_get_ds_from_dir(zonepath)) == NULL) {
2N/A continue;
2N/A }
2N/A
2N/A /*
2N/A * Check if this zone is supported based on the
2N/A * dataset of its zonepath.
2N/A */
2N/A if (!be_zone_supported(zonepath_ds)) {
2N/A free(zonepath_ds);
2N/A zonepath_ds = NULL;
2N/A continue;
2N/A }
2N/A
2N/A /* Unmount this zone */
2N/A ret = be_unmount_one_zone(ud, zonename, zonepath,
2N/A zonepath_ds);
2N/A
2N/A free(zonepath_ds);
2N/A zonepath_ds = NULL;
2N/A
2N/A if (ret != BE_SUCCESS) {
2N/A be_print_err(gettext("be_unmount_zones:"
2N/A " failed to unmount zone %s from "
2N/A "altroot %s\n"), zonename, ud->altroot);
2N/A goto done;
2N/A }
2N/A }
2N/A }
2N/A
2N/Adone:
2N/A z_free_brand_list(brands);
2N/A z_free_zone_list(zlst);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_mount_one_zone
2N/A * Description: This function is called to mount one zone for a given
2N/A * global BE.
2N/A * Parameters:
2N/A * be_zhp - zfs_handle_t pointer to the root dataset of the
2N/A * global BE
2N/A * md - be_mount_data_t pointer to data for global BE
2N/A * zonename - name of zone to mount
2N/A * zonepath - zonepath of zone to mount
2N/A * zonepath_ds - dataset for the zonepath
2N/A * Returns:
2N/A * BE_SUCCESS - Success
2N/A * be_errno_t - Failure
2N/A * Scope:
2N/A * Private
2N/A */
2N/Astatic int
2N/Abe_mount_one_zone(zfs_handle_t *be_zhp, be_mount_data_t *md, char *zonename,
2N/A char *zonepath, char *zonepath_ds)
2N/A{
2N/A be_mount_data_t zone_md = { 0 };
2N/A zfs_handle_t *zone_zhp = NULL;
2N/A char zone_altroot[MAXPATHLEN];
2N/A char zoneroot[MAXPATHLEN];
2N/A char zoneroot_ds[MAXPATHLEN];
2N/A int ret = BE_SUCCESS;
2N/A int err = 0;
2N/A
2N/A errno = 0;
2N/A
2N/A /* Find the active zone root dataset for this zone for this BE */
2N/A if ((ret = be_find_active_zone_root(be_zhp, zonepath_ds, zoneroot_ds,
2N/A sizeof (zoneroot_ds))) == BE_ERR_ZONE_NO_ACTIVE_ROOT) {
2N/A be_print_err(gettext("be_mount_one_zone: did not "
2N/A "find active zone root for zone %s, skipping ...\n"),
2N/A zonename);
2N/A return (BE_SUCCESS);
2N/A } else if (ret != BE_SUCCESS) {
2N/A be_print_err(gettext("be_mount_one_zone: failed to "
2N/A "find active zone root for zone %s\n"), zonename);
2N/A return (ret);
2N/A }
2N/A
2N/A /* Get handle to active zoneroot dataset */
2N/A if ((zone_zhp = zfs_open(g_zfs, zoneroot_ds, ZFS_TYPE_FILESYSTEM))
2N/A == NULL) {
2N/A be_print_err(gettext("be_mount_one_zone: failed to "
2N/A "open zone root dataset (%s): %s\n"), zoneroot_ds,
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 zone's altroot path */
2N/A be_make_zoneroot(zonepath, zoneroot, sizeof (zoneroot));
2N/A (void) strlcpy(zone_altroot, md->altroot, sizeof (zone_altroot));
2N/A (void) strlcat(zone_altroot, zoneroot, sizeof (zone_altroot));
2N/A
2N/A if (mkdirp(zone_altroot,
2N/A S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1 &&
2N/A errno != EEXIST) {
2N/A err = errno;
2N/A be_print_err(gettext("be_mount_one_zone: failed to "
2N/A "create mount point %s\n"), zone_altroot);
2N/A ret = (errno_to_be_err(err));
2N/A goto done;
2N/A }
2N/A
2N/A /* Build mount_data for the zone */
2N/A zone_md.altroot = zone_altroot;
2N/A zone_md.shared_fs = md->shared_fs;
2N/A zone_md.shared_rw = md->shared_rw;
2N/A
2N/A /* Mount the zone's root file system */
2N/A if ((ret = be_mount_zone_root(zone_zhp, &zone_md)) != BE_SUCCESS) {
2N/A be_print_err(gettext("be_mount_one_zone: failed to "
2N/A "mount zone root file system at %s\n"), zone_altroot);
2N/A goto done;
2N/A }
2N/A
2N/A /* Iterate through zone's children filesystems */
2N/A if ((ret = zfs_iter_filesystems(zone_zhp, be_mount_callback,
2N/A zone_altroot)) != 0) {
2N/A be_print_err(gettext("be_mount_one_zone: failed to "
2N/A "mount zone subordinate file systems at %s\n"),
2N/A zone_altroot);
2N/A goto done;
2N/A }
2N/A
2N/A /* TODO: Mount all shared file systems for this zone */
2N/A
2N/Adone:
2N/A ZFS_CLOSE(zone_zhp);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_unmount_one_zone
2N/A * Description: This function unmount one zone for a give global BE.
2N/A * Parameters:
2N/A * ud - be_unmount_data_t pointer to data for global BE
2N/A * zonename - name of zone to unmount
2N/A * zonepath - zonepath of the zone to unmount
2N/A * zonepath_ds - dataset for the zonepath
2N/A * Returns:
2N/A * BE_SUCCESS - Success
2N/A * be_errno_t - Failure
2N/A * Scope:
2N/A * Private
2N/A */
2N/Astatic int
2N/Abe_unmount_one_zone(be_unmount_data_t *ud, char *zonename, char *zonepath,
2N/A char *zonepath_ds)
2N/A{
2N/A be_unmount_data_t zone_ud = { 0 };
2N/A zfs_handle_t *zone_zhp = NULL;
2N/A char zone_altpath[MAXPATHLEN];
2N/A char zone_altroot[MAXPATHLEN];
2N/A char zoneroot[MAXPATHLEN];
2N/A char zoneroot_ds[MAXPATHLEN];
2N/A int ret = BE_SUCCESS;
2N/A int err = 0;
2N/A
2N/A errno = 0;
2N/A
2N/A /* Generate string for zone's alternate root path */
2N/A be_make_zoneroot(zonepath, zoneroot, sizeof (zoneroot));
2N/A (void) strlcpy(zone_altroot, ud->altroot, sizeof (zone_altroot));
2N/A (void) strlcat(zone_altroot, zoneroot, sizeof (zone_altroot));
2N/A
2N/A /* Generate string for zone's alternate zone path */
2N/A (void) strlcpy(zone_altpath, ud->altroot, sizeof (zone_altpath));
2N/A (void) strlcat(zone_altpath, zonepath, sizeof (zone_altpath));
2N/A
2N/A /* Build be_unmount_data for zone */
2N/A zone_ud.altroot = zone_altroot;
2N/A zone_ud.force = ud->force;
2N/A
2N/A /* Find the mounted zone root dataset for this zone for this BE */
2N/A if ((ret = be_find_mounted_zone_root(zone_altroot, zonepath_ds,
2N/A zoneroot_ds, sizeof (zoneroot_ds))) == BE_ERR_NO_MOUNTED_ZONE) {
2N/A be_print_err(gettext("be_unmount_one_zone: did not "
2N/A "find any zone root mounted for zone %s\n"), zonename);
2N/A ret = BE_SUCCESS;
2N/A goto unmount_zonepath;
2N/A } else if (ret != BE_SUCCESS) {
2N/A be_print_err(gettext("be_unmount_one_zone: failed to "
2N/A "find mounted zone root for zone %s\n"), zonename);
2N/A return (ret);
2N/A }
2N/A
2N/A /* Get handle to zoneroot dataset mounted for this BE */
2N/A if ((zone_zhp = zfs_open(g_zfs, zoneroot_ds, ZFS_TYPE_FILESYSTEM))
2N/A == NULL) {
2N/A be_print_err(gettext("be_unmount_one_zone: failed to "
2N/A "open mounted zone root dataset (%s): %s\n"), zoneroot_ds,
2N/A libzfs_error_description(g_zfs));
2N/A return (zfs_err_to_be_err(g_zfs));
2N/A }
2N/A
2N/A /* TODO: Unmount all shared file systems for this zone */
2N/A
2N/A /* Iterate through zone's children filesystems and unmount them */
2N/A if ((ret = zfs_iter_filesystems(zone_zhp, be_unmount_callback,
2N/A &zone_ud)) != 0) {
2N/A be_print_err(gettext("be_unmount_one_zone: failed to "
2N/A "unmount zone subordinate file systems at %s\n"),
2N/A zone_altroot);
2N/A goto done;
2N/A }
2N/A
2N/A /* Unmount the zone's root filesystem */
2N/A if ((ret = be_unmount_zone_root(zone_zhp, &zone_ud)) != BE_SUCCESS) {
2N/A be_print_err(gettext("be_unmount_one_zone: failed to "
2N/A "unmount zone root file system at %s\n"), zone_altroot);
2N/A goto done;
2N/A }
2N/A
2N/Aunmount_zonepath:
2N/A /*
2N/A * We want to unmount this here since we mounted this as a tmpfs mount
2N/A * in our zone mount code.
2N/A */
2N/A if ((ret = umount(zone_altpath)) != 0) {
2N/A err = errno;
2N/A /*
2N/A * Check to see if zonepath unmount generated EINVAL which
2N/A * indicates that the zonepath we're trying to unmount was
2N/A * never in fact mounted (because we mount zones in a for-loop
2N/A * but if any individual zone mount encounters an error we stop
2N/A * processing). In which case, just move along.
2N/A */
2N/A if (err == EINVAL) {
2N/A ret = 0;
2N/A goto done;
2N/A }
2N/A be_print_err(gettext("be_unmount_one_zone: failed to "
2N/A "unmount alternate zone path (%s): %s\n"), zone_altpath,
2N/A strerror(err));
2N/A ret = errno_to_be_err(err);
2N/A }
2N/A
2N/Adone:
2N/A ZFS_CLOSE(zone_zhp);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Function: be_get_ds_from_dir_callback
2N/A * Description: This is a callback function used to iterate all datasets
2N/A * to find the one that is currently mounted at the directory
2N/A * being searched for. If matched, the name of the dataset is
2N/A * returned in heap storage, so the caller is responsible for
2N/A * freeing it.
2N/A * Parameters:
2N/A * zhp - zfs_handle_t pointer to current dataset being processed.
2N/A * data - dir_data_t pointer providing name of directory being
2N/A * searched for.
2N/A * Returns:
2N/A * 1 - This dataset is mounted at directory being searched for.
2N/A * 0 - This dataset is not mounted at directory being searched for.
2N/A * Scope:
2N/A * Private
2N/A */
2N/Astatic int
2N/Abe_get_ds_from_dir_callback(zfs_handle_t *zhp, void *data)
2N/A{
2N/A dir_data_t *dd = data;
2N/A char *mp = NULL;
2N/A int zret = 0;
2N/A
2N/A if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2N/A ZFS_CLOSE(zhp);
2N/A return (0);
2N/A }
2N/A
2N/A if (zfs_is_mounted(zhp, &mp) && mp != NULL &&
2N/A strcmp(mp, dd->dir) == 0) {
2N/A if ((dd->ds = strdup(zfs_get_name(zhp))) == NULL) {
2N/A be_print_err(gettext("be_get_ds_from_dir_callback: "
2N/A "memory allocation failed\n"));
2N/A ZFS_CLOSE(zhp);
2N/A return (0);
2N/A }
2N/A ZFS_CLOSE(zhp);
2N/A return (1);
2N/A }
2N/A
2N/A zret = zfs_iter_filesystems(zhp, be_get_ds_from_dir_callback, dd);
2N/A
2N/A ZFS_CLOSE(zhp);
2N/A
2N/A return (zret);
2N/A}