libzfs_changelist.c revision 3bb79bece53191f2cf27aa61a72ea1784a7ce700
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER START
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The contents of this file are subject to the terms of the
99653d4ee642c6528e88224f12409a5f23060994eschrock * Common Development and Distribution License (the "License").
99653d4ee642c6528e88224f12409a5f23060994eschrock * You may not use this file except in compliance 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
b12a1c38fc215cc54fa6014069fd2b8dbb496646lling * Copyright 2006 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:
b12a1c38fc215cc54fa6014069fd2b8dbb496646lling * changelist_remove() - remove a node from a gathered list
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))) {
b12a1c38fc215cc54fa6014069fd2b8dbb496646lling * Is this "dataset" a child of "parent"?
b12a1c38fc215cc54fa6014069fd2b8dbb496646lling /* snapshot does not have a child */
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)
b12a1c38fc215cc54fa6014069fd2b8dbb496646lling * Do not rename a clone that's not in the source hierarchy.
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().
b12a1c38fc215cc54fa6014069fd2b8dbb496646lling * Remove a node from a gathered list.
b12a1c38fc215cc54fa6014069fd2b8dbb496646llingchangelist_remove(zfs_handle_t *zhp, prop_changelist_t *clp)
b12a1c38fc215cc54fa6014069fd2b8dbb496646lling if (strcmp(cn->cn_handle->zfs_name, zhp->zfs_name) == 0) {
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);
b12a1c38fc215cc54fa6014069fd2b8dbb496646lling sourcetype == ZFS_SRC_DEFAULT || sourcetype == ZFS_SRC_INHERITED) {
99653d4ee642c6528e88224f12409a5f23060994eschrock return (-1);
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);
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock/*ARGSUSED*/
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrockcompare_mountpoints(const void *a, const void *b, void *unused)
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock * When unsharing or unmounting filesystems, we need to do it in
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock * mountpoint order. This allows the user to have a mountpoint
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock * hierarchy that is different from the dataset hierarchy, and still
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock * allow it to be changed. However, if either dataset doesn't have a
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock * mountpoint (because it is a volume or a snapshot), we place it at the
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock * end of the list, because it doesn't affect our change at all.
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock hasmounta = (zfs_prop_get(ca->cn_handle, ZFS_PROP_MOUNTPOINT, mounta,
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock hasmountb = (zfs_prop_get(cb->cn_handle, ZFS_PROP_MOUNTPOINT, mountb,
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock return (-1);
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock return (1);
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock 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)
99653d4ee642c6528e88224f12409a5f23060994eschrock if ((clp = zfs_alloc(zhp->zfs_hdl, sizeof (prop_changelist_t))) == NULL)
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock * For mountpoint-related tasks, we want to sort everything by
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock * mountpoint, so that we mount and unmount them in the appropriate
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock * order, regardless of their position in the hierarchy.
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock if (prop == ZFS_PROP_NAME || prop == ZFS_PROP_ZONED ||
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock prop == ZFS_PROP_MOUNTPOINT || prop == ZFS_PROP_SHARENFS) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens clp->cl_pool = uu_list_pool_create("changelist_pool",
94de1d4cf6ec0a3bf040dcc4b8df107c4ed36b51eschrock (void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
94de1d4cf6ec0a3bf040dcc4b8df107c4ed36b51eschrock (void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
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().
b12a1c38fc215cc54fa6014069fd2b8dbb496646lling * Flag cl_alldependents to catch all children plus the
b12a1c38fc215cc54fa6014069fd2b8dbb496646lling * dependents (clones) that are not in the hierarchy.
3bb79bece53191f2cf27aa61a72ea1784a7ce700eschrock if (zfs_iter_dependents(zhp, B_TRUE, change_one, clp) != 0) {
b12a1c38fc215cc54fa6014069fd2b8dbb496646lling } else if (zfs_iter_children(zhp, change_one, clp) != 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens * We have to re-open ourselves because we auto-close all the handles
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and can't tell the difference.
99653d4ee642c6528e88224f12409a5f23060994eschrock if ((temp = zfs_open(zhp->zfs_hdl, zfs_get_name(zhp),
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),