libzfs_changelist.c revision fa9e4066f08beec538e775443c5be79dd423fcab
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER START
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The contents of this file are subject to the terms of the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Common Development and Distribution License, Version 1.0 only
fa9e4066f08beec538e775443c5be79dd423fcabahrens * (the "License"). You may not use this file except in compliance
fa9e4066f08beec538e775443c5be79dd423fcabahrens * with the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fa9e4066f08beec538e775443c5be79dd423fcabahrens * See the License for the specific language governing permissions
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and limitations under the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * When distributing Covered Code, include this CDDL HEADER in each
fa9e4066f08beec538e775443c5be79dd423fcabahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If applicable, add the following below this CDDL HEADER, with the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * fields enclosed by brackets "[]" replaced with your own identifying
fa9e4066f08beec538e775443c5be79dd423fcabahrens * information: Portions Copyright [yyyy] [name of copyright owner]
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER END
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Use is subject to license terms.
fa9e4066f08beec538e775443c5be79dd423fcabahrens#pragma ident "%Z%%M% %I% %E% SMI"
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Structure to keep track of dataset state. Before changing the 'sharenfs' or
fa9e4066f08beec538e775443c5be79dd423fcabahrens * 'mountpoint' property, we record whether the filesystem was previously
fa9e4066f08beec538e775443c5be79dd423fcabahrens * mounted/shared. This prior state dictates whether we remount/reshare the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * dataset after the property has been changed.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The interface consists of the following sequence of functions:
fa9e4066f08beec538e775443c5be79dd423fcabahrens * changelist_gather()
fa9e4066f08beec538e775443c5be79dd423fcabahrens * changelist_prefix()
fa9e4066f08beec538e775443c5be79dd423fcabahrens * < change property >
fa9e4066f08beec538e775443c5be79dd423fcabahrens * changelist_postfix()
fa9e4066f08beec538e775443c5be79dd423fcabahrens * changelist_free()
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Other interfaces:
fa9e4066f08beec538e775443c5be79dd423fcabahrens * changelist_rename() - renames all datasets appropriately when doing a rename
fa9e4066f08beec538e775443c5be79dd423fcabahrens * changelist_unshare() - unshares all the nodes in a given changelist
fa9e4066f08beec538e775443c5be79dd423fcabahrens * changelist_haszonedchild() - check if there is any child exported to
fa9e4066f08beec538e775443c5be79dd423fcabahrens * a local zone
fa9e4066f08beec538e775443c5be79dd423fcabahrenstypedef struct prop_changenode {
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If the property is 'mountpoint', go through and unmount filesystems as
fa9e4066f08beec538e775443c5be79dd423fcabahrens * necessary. We don't do the same for 'sharenfs', because we can just re-share
fa9e4066f08beec538e775443c5be79dd423fcabahrens * with different options without interrupting service.
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens * if we are in a global zone, but this dataset is exported to
fa9e4066f08beec538e775443c5be79dd423fcabahrens * a local zone, do nothing.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If we have a volume and this was a rename, remove the
fa9e4066f08beec538e775443c5be79dd423fcabahrens } else if (zfs_unmount(cn->cn_handle, NULL, clp->cl_flags) != 0)
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If the proeprty is 'mountpoint' or 'sharenfs', go through and remount and/or
fa9e4066f08beec538e775443c5be79dd423fcabahrens * reshare the filesystems as necessary. In changelist_gather() we recorded
fa9e4066f08beec538e775443c5be79dd423fcabahrens * whether the filesystem was previously shared or mounted. The action we take
fa9e4066f08beec538e775443c5be79dd423fcabahrens * depends on the previous state, and whether the value was previously 'legacy'.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * For non-legacy properties, we only remount/reshare the filesystem if it was
fa9e4066f08beec538e775443c5be79dd423fcabahrens * previously mounted/shared. Otherwise, we always remount/reshare the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * filesystem.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If we're changing the mountpoint, attempt to destroy the underlying
fa9e4066f08beec538e775443c5be79dd423fcabahrens * mountpoint. All other datasets will have inherited from this dataset
fa9e4066f08beec538e775443c5be79dd423fcabahrens * (in which case their mountpoints exist in the filesystem in the new
fa9e4066f08beec538e775443c5be79dd423fcabahrens * location), or have explicit mountpoints set (in which case they won't
fa9e4066f08beec538e775443c5be79dd423fcabahrens * be in the changelist).
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens * We walk the datasets in reverse, because we want to mount any parent
fa9e4066f08beec538e775443c5be79dd423fcabahrens * datasets before mounting the children.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * if we are in a global zone, but this dataset is exported to
fa9e4066f08beec538e775443c5be79dd423fcabahrens * a local zone, do nothing.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If this is a volume and we're doing a rename, recreate the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * We always re-share even if the filesystem is currently
fa9e4066f08beec538e775443c5be79dd423fcabahrens * shared, so that we can adopt any new options.
fa9e4066f08beec538e775443c5be79dd423fcabahrens (clp->cl_prop == ZFS_PROP_SHARENFS && clp->cl_waslegacy))) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If we rename a filesystem, and child filesystem handles are no longer valid,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * since we identify datasets by their name in the ZFS namespace. So, we have
fa9e4066f08beec538e775443c5be79dd423fcabahrens * to go through and fix up all the names appropriately. We could do this
fa9e4066f08beec538e775443c5be79dd423fcabahrens * automatically if libzfs kept track of all open handles, but this is a lot
fa9e4066f08beec538e775443c5be79dd423fcabahrens * less work.
fa9e4066f08beec538e775443c5be79dd423fcabahrenschangelist_rename(prop_changelist_t *clp, const char *src, const char *dst)
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Destroy the previous mountpoint if needed.
fa9e4066f08beec538e775443c5be79dd423fcabahrens (void) strcat(newname, cn->cn_handle->zfs_name + strlen(src));
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Given a gathered changelist for the "sharenfs" property,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * unshare all the nodes in the list.
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Check if there is any child exported to a local zone in a
fa9e4066f08beec538e775443c5be79dd423fcabahrens * given changelist. This information has already been recorded
fa9e4066f08beec538e775443c5be79dd423fcabahrens * while gathering the changelist via changelist_gather().
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Release any memory associated with a changelist.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * We only want to unmount/unshare those filesystems which may
fa9e4066f08beec538e775443c5be79dd423fcabahrens * inherit from the target filesystem. If we find any filesystem
fa9e4066f08beec538e775443c5be79dd423fcabahrens * with a locally set mountpoint, we ignore any children since changing
fa9e4066f08beec538e775443c5be79dd423fcabahrens * the property will not affect them. If this is a rename, we iterate
fa9e4066f08beec538e775443c5be79dd423fcabahrens * over all children regardless, since we need them unmounted in order
fa9e4066f08beec538e775443c5be79dd423fcabahrens * to do the rename. Also, if this is a volume and we're doing a
fa9e4066f08beec538e775443c5be79dd423fcabahrens * rename, then always add it to the changelist.
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (!(zhp->zfs_volblocksize && clp->cl_realprop == ZFS_PROP_NAME) &&
fa9e4066f08beec538e775443c5be79dd423fcabahrens sizeof (property), &sourcetype, where, sizeof (where),
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (clp->cl_allchildren || sourcetype == ZFS_SRC_DEFAULT ||
fa9e4066f08beec538e775443c5be79dd423fcabahrens cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* indicate if any child is exported to a local zone */
fa9e4066f08beec538e775443c5be79dd423fcabahrens uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Given a ZFS handle and a property, construct a complete list of datasets that
fa9e4066f08beec538e775443c5be79dd423fcabahrens * need to be modified as part of this process. For anything but the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * 'mountpoint' and 'sharenfs' properties, this just returns an empty list.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Otherwise, we iterate over all children and look for any datasets which
fa9e4066f08beec538e775443c5be79dd423fcabahrens * inherit this property. For each such dataset, we add it to the list and mark
fa9e4066f08beec538e775443c5be79dd423fcabahrens * whether it was shared beforehand.
fa9e4066f08beec538e775443c5be79dd423fcabahrenschangelist_gather(zfs_handle_t *zhp, zfs_prop_t prop, int flags)
fa9e4066f08beec538e775443c5be79dd423fcabahrens prop_changelist_t *clp = zfs_malloc(sizeof (prop_changelist_t));
fa9e4066f08beec538e775443c5be79dd423fcabahrens clp->cl_pool = uu_list_pool_create("changelist_pool",
fa9e4066f08beec538e775443c5be79dd423fcabahrens clp->cl_list = uu_list_create(clp->cl_pool, NULL, 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If this is a rename or the 'zoned' property, we pretend we're
fa9e4066f08beec538e775443c5be79dd423fcabahrens * changing the mountpoint and flag it so we can catch all children in
fa9e4066f08beec538e775443c5be79dd423fcabahrens * change_one().
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (prop == ZFS_PROP_NAME || prop == ZFS_PROP_ZONED) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens * We have to re-open ourselves because we auto-close all the handles
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and can't tell the difference.
fa9e4066f08beec538e775443c5be79dd423fcabahrens if ((temp = zfs_open(zfs_get_name(zhp), ZFS_TYPE_ANY)) == NULL) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Always add ourself to the list. We add ourselves to the end so that
fa9e4066f08beec538e775443c5be79dd423fcabahrens * we're the last to be unmounted.
fa9e4066f08beec538e775443c5be79dd423fcabahrens cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
fa9e4066f08beec538e775443c5be79dd423fcabahrens uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If the property was previously 'legacy' or 'none', record this fact,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * as the behavior of changelist_postfix() will be different.
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (zfs_prop_get(zhp, prop, property, sizeof (property),