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
ab003da878e3fe36b164e1856f9e15a78384c9ebJim Dunham * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Use is subject to license terms.
2b61b799b28717ea21febf9eab30c7867477f280mmusante * Portions Copyright 2007 Ramprakash Jelari
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens * Copyright (c) 2014, 2015 by Delphix. All rights reserved.
88f61dee20b358671b1b643e9d1dbf220a1d69beIgor Kozhukhov * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
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
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw zfs_prop_t cl_shareprop; /* used with sharenfs/sharesmb */
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
d5b4fe4dcc1b674d5e848db057a30cf882f73b22dougm * with different options without interrupting service. We do handle 'sharesmb'
d5b4fe4dcc1b674d5e848db057a30cf882f73b22dougm * since there may be old resource names that need to be removed.
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
cc4231e55a9c2eb07b3aa5b3e602a93d2415d284mmusante /* if a previous loop failed, set the remaining to false */
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * If we are in the global zone, but this dataset is exported
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * to a local zone, do nothing.
d5b4fe4dcc1b674d5e848db057a30cf882f73b22dougm * Do the property specific processing.
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * If the property 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);
6733190958bbcc0bd6d1d601e7ae0a6994dafb45dougm * It is possible that the changelist_prefix() used libshare
6733190958bbcc0bd6d1d601e7ae0a6994dafb45dougm * to unshare some entries. Since libshare caches data, an
6733190958bbcc0bd6d1d601e7ae0a6994dafb45dougm * attempt to reshare during postfix can fail unless libshare
6733190958bbcc0bd6d1d601e7ae0a6994dafb45dougm * is uninitialized here so that it will reinitialize later.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * We walk the datasets in reverse, because we want to mount any parent
3d7934e1eaaab784a1928c5c9bdb6273eeb7275drm * datasets before mounting the children. We walk all datasets even if
3d7934e1eaaab784a1928c5c9bdb6273eeb7275drm * there are errors.
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * If we are in the global zone, but this dataset is exported
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * to a local zone, do nothing.
cc4231e55a9c2eb07b3aa5b3e602a93d2415d284mmusante /* Only do post-processing if it's required */
3a55fc6b85230272df26c2c8c82f1176c0503e3arm * Remount if previously mounted or mountpoint was legacy,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * or sharenfs or sharesmb property is set.
3a55fc6b85230272df26c2c8c82f1176c0503e3arm sharenfs = ((zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARENFS,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw sharesmb = ((zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARESMB,
d96d44073b0cd1434844dd5dc34d08b1b6199f49William Gorrell mounted = zfs_is_mounted(cn->cn_handle, NULL);
d96d44073b0cd1434844dd5dc34d08b1b6199f49William Gorrell ((sharenfs || sharesmb || clp->cl_waslegacy) &&
d96d44073b0cd1434844dd5dc34d08b1b6199f49William Gorrell * If the file system is mounted we always re-share even
d96d44073b0cd1434844dd5dc34d08b1b6199f49William Gorrell * if the filesystem is currently shared, so that we can
d96d44073b0cd1434844dd5dc34d08b1b6199f49William Gorrell * adopt any new options.
b12a1c38fc215cc54fa6014069fd2b8dbb496646lling * Is this "dataset" a child of "parent"?
55406565bb21da3b769184726b3306b334df62e9mmusanteisa_child_of(const char *dataset, const char *parent)
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * If we rename a filesystem, child filesystem handles are no longer valid
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * since we identify each dataset by its name in the ZFS namespace. As a
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * result, we have to go through and fix up all the names appropriately. We
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * could do this automatically if libzfs kept track of all open handles, but
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * this is a lot 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));
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Given a gathered changelist for the 'sharenfs' or 'sharesmb' property,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * unshare all the datasets in the list.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwchangelist_unshare(prop_changelist_t *clp, zfs_share_proto_t *proto)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * Check if there is any child exported to a local zone in a given changelist.
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * This information has already been recorded while gathering the changelist
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * via changelist_gather().
b12a1c38fc215cc54fa6014069fd2b8dbb496646lling * Remove a node from a gathered list.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrenschangelist_remove(prop_changelist_t *clp, const char *name)
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Release any memory associated with a changelist.
68f944b71d3feda491e30f04cc360be6cdb79f21eschrock while ((cn = uu_list_teardown(clp->cl_list, &cookie)) != NULL) {
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * We only want to unmount/unshare those filesystems that may inherit
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * from the target filesystem. If we find any filesystem with a
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * locally set mountpoint, we ignore any children since changing the
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * property will not affect them. If this is a rename, we iterate
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * over all children regardless, since we need them unmounted in
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * order to do the rename. Also, if this is a volume and we're doing
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * a rename, then always add it to the changelist.
e9dbad6f263d5570ed7ff5443ec5b958af8c24d7eschrock if (!(ZFS_IS_VOLUME(zhp) && clp->cl_realprop == ZFS_PROP_NAME) &&
fa9e4066f08beec538e775443c5be79dd423fcabahrens sizeof (property), &sourcetype, where, sizeof (where),
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If we are "watching" sharenfs or sharesmb
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * then check out the companion property which is tracked
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * in cl_shareprop
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw sizeof (property), &share_sourcetype, where, sizeof (where),
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (0);
99653d4ee642c6528e88224f12409a5f23060994eschrock return (-1);
0069fd67511146f5f43175204d5b71d2b357be71Tim Haley cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
fa9e4066f08beec538e775443c5be79dd423fcabahrens cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl /* Indicate if any child is exported to a local zone. */
fa9e4066f08beec538e775443c5be79dd423fcabahrens uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
3cc4a7920cf40de22a5c8c465a4676b2b7f620ddWilliam Gorrell * Add this child to beginning of the list. Children
3cc4a7920cf40de22a5c8c465a4676b2b7f620ddWilliam Gorrell * below this one in the hierarchy will get added above
3cc4a7920cf40de22a5c8c465a4676b2b7f620ddWilliam Gorrell * this one in the list. This produces a list in
3cc4a7920cf40de22a5c8c465a4676b2b7f620ddWilliam Gorrell * reverse dataset name order.
3cc4a7920cf40de22a5c8c465a4676b2b7f620ddWilliam Gorrell * This is necessary when the original mountpoint
3cc4a7920cf40de22a5c8c465a4676b2b7f620ddWilliam Gorrell * is legacy or none.
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);
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * Given a ZFS handle and a property, construct a complete list of datasets
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * that need to be modified as part of this process. For anything but the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * 'mountpoint' and 'sharenfs' properties, this just returns an empty list.
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * Otherwise, we iterate over all children and look for any datasets that
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * inherit the property. For each such dataset, we add it to the list and
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * mark whether it was shared beforehand.
0069fd67511146f5f43175204d5b71d2b357be71Tim Haleychangelist_gather(zfs_handle_t *zhp, zfs_prop_t prop, int gather_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 ||
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw 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().
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * Flag cl_alldependents to catch all children plus the dependents
f3861e1a2ceec23a5b699c24d814b7775a9e0b52ahl * (clones) that are not in the hierarchy.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If watching SHARENFS or SHARESMB then
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * also watch its companion property.
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.
0069fd67511146f5f43175204d5b71d2b357be71Tim Haley cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
fa9e4066f08beec538e775443c5be79dd423fcabahrens cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
fa9e4066f08beec538e775443c5be79dd423fcabahrens uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
3cc4a7920cf40de22a5c8c465a4676b2b7f620ddWilliam Gorrell * Add the target dataset to the end of the list.
3cc4a7920cf40de22a5c8c465a4676b2b7f620ddWilliam Gorrell * The list is not really unsorted. The list will be
3cc4a7920cf40de22a5c8c465a4676b2b7f620ddWilliam Gorrell * in reverse dataset name order. This is necessary
3cc4a7920cf40de22a5c8c465a4676b2b7f620ddWilliam Gorrell * when the original mountpoint is legacy or none.
3a55fc6b85230272df26c2c8c82f1176c0503e3arm * If the mountpoint property was previously 'legacy', or 'none',
3a55fc6b85230272df26c2c8c82f1176c0503e3arm * record it as the behavior of changelist_postfix() will be different.
3cc4a7920cf40de22a5c8c465a4676b2b7f620ddWilliam Gorrell if ((clp->cl_prop == ZFS_PROP_MOUNTPOINT) && legacy) {
a227b7f4f323ad89c40a86c430a5e891504a8e8bhs * do not automatically mount ex-legacy datasets if
a227b7f4f323ad89c40a86c430a5e891504a8e8bhs * we specifically set canmount to noauto