* Portions Copyright 2007 Ramprakash Jelari
* Copyright (c) 2014, 2015 by Delphix. All rights reserved.
* Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
+ * Copyright (c) 2018 Datto Inc.
*/
#include <libintl.h>
int cn_mounted;
int cn_zoned;
boolean_t cn_needpost; /* is postfix() needed? */
- uu_list_node_t cn_listnode;
+ uu_avl_node_t cn_treenode;
} prop_changenode_t;
struct prop_changelist {
zfs_prop_t cl_prop;
zfs_prop_t cl_realprop;
zfs_prop_t cl_shareprop; /* used with sharenfs/sharesmb */
- uu_list_pool_t *cl_pool;
- uu_list_t *cl_list;
+ uu_avl_pool_t *cl_pool;
+ uu_avl_t *cl_tree;
boolean_t cl_waslegacy;
boolean_t cl_allchildren;
boolean_t cl_alldependents;
int cl_mflags; /* Mount flags */
int cl_gflags; /* Gather request flags */
boolean_t cl_haszonedchild;
- boolean_t cl_sorted;
};
/*
changelist_prefix(prop_changelist_t *clp)
{
prop_changenode_t *cn;
+ uu_avl_walk_t *walk;
int ret = 0;
if (clp->cl_prop != ZFS_PROP_MOUNTPOINT &&
clp->cl_prop != ZFS_PROP_SHARESMB)
return (0);
- for (cn = uu_list_first(clp->cl_list); cn != NULL;
- cn = uu_list_next(clp->cl_list, cn)) {
+ if ((walk = uu_avl_walk_start(clp->cl_tree, UU_WALK_ROBUST)) == NULL)
+ return (-1);
+
+ while ((cn = uu_avl_walk_next(walk)) != NULL) {
/* if a previous loop failed, set the remaining to false */
if (ret == -1) {
}
}
+ uu_avl_walk_end(walk);
+
if (ret == -1)
(void) changelist_postfix(clp);
changelist_postfix(prop_changelist_t *clp)
{
prop_changenode_t *cn;
+ uu_avl_walk_t *walk;
char shareopts[ZFS_MAXPROPLEN];
int errors = 0;
libzfs_handle_t *hdl;
* location), or have explicit mountpoints set (in which case they won't
* be in the changelist).
*/
- if ((cn = uu_list_last(clp->cl_list)) == NULL)
+ if ((cn = uu_avl_last(clp->cl_tree)) == NULL)
return (0);
if (clp->cl_prop == ZFS_PROP_MOUNTPOINT)
* datasets before mounting the children. We walk all datasets even if
* there are errors.
*/
- for (cn = uu_list_last(clp->cl_list); cn != NULL;
- cn = uu_list_prev(clp->cl_list, cn)) {
+ if ((walk = uu_avl_walk_start(clp->cl_tree,
+ UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
+ return (-1);
+
+ while ((cn = uu_avl_walk_next(walk)) != NULL) {
boolean_t sharenfs;
boolean_t sharesmb;
errors += zfs_unshare_smb(cn->cn_handle, NULL);
}
+ uu_avl_walk_end(walk);
+
return (errors ? -1 : 0);
}
changelist_rename(prop_changelist_t *clp, const char *src, const char *dst)
{
prop_changenode_t *cn;
+ uu_avl_walk_t *walk;
char newname[ZFS_MAX_DATASET_NAME_LEN];
- for (cn = uu_list_first(clp->cl_list); cn != NULL;
- cn = uu_list_next(clp->cl_list, cn)) {
+ if ((walk = uu_avl_walk_start(clp->cl_tree, UU_WALK_ROBUST)) == NULL)
+ return;
+
+ while ((cn = uu_avl_walk_next(walk)) != NULL) {
/*
* Do not rename a clone that's not in the source hierarchy.
*/
(void) strlcpy(cn->cn_handle->zfs_name, newname,
sizeof (cn->cn_handle->zfs_name));
}
+
+ uu_avl_walk_end(walk);
}
/*
changelist_unshare(prop_changelist_t *clp, zfs_share_proto_t *proto)
{
prop_changenode_t *cn;
+ uu_avl_walk_t *walk;
int ret = 0;
if (clp->cl_prop != ZFS_PROP_SHARENFS &&
clp->cl_prop != ZFS_PROP_SHARESMB)
return (0);
- for (cn = uu_list_first(clp->cl_list); cn != NULL;
- cn = uu_list_next(clp->cl_list, cn)) {
+ if ((walk = uu_avl_walk_start(clp->cl_tree, UU_WALK_ROBUST)) == NULL)
+ return (-1);
+
+ while ((cn = uu_avl_walk_next(walk)) != NULL) {
if (zfs_unshare_proto(cn->cn_handle, NULL, proto) != 0)
ret = -1;
}
+ uu_avl_walk_end(walk);
+
return (ret);
}
changelist_remove(prop_changelist_t *clp, const char *name)
{
prop_changenode_t *cn;
+ uu_avl_walk_t *walk;
- for (cn = uu_list_first(clp->cl_list); cn != NULL;
- cn = uu_list_next(clp->cl_list, cn)) {
+ if ((walk = uu_avl_walk_start(clp->cl_tree, UU_WALK_ROBUST)) == NULL)
+ return;
+ while ((cn = uu_avl_walk_next(walk)) != NULL) {
if (strcmp(cn->cn_handle->zfs_name, name) == 0) {
- uu_list_remove(clp->cl_list, cn);
+ uu_avl_remove(clp->cl_tree, cn);
zfs_close(cn->cn_handle);
free(cn);
+ uu_avl_walk_end(walk);
return;
}
}
+
+ uu_avl_walk_end(walk);
}
/*
changelist_free(prop_changelist_t *clp)
{
prop_changenode_t *cn;
- void *cookie;
- if (clp->cl_list) {
- cookie = NULL;
- while ((cn = uu_list_teardown(clp->cl_list, &cookie)) != NULL) {
+ if (clp->cl_tree) {
+ uu_avl_walk_t *walk;
+
+ if ((walk = uu_avl_walk_start(clp->cl_tree,
+ UU_WALK_ROBUST)) == NULL)
+ return;
+
+ while ((cn = uu_avl_walk_next(walk)) != NULL) {
+ uu_avl_remove(clp->cl_tree, cn);
zfs_close(cn->cn_handle);
free(cn);
}
- uu_list_destroy(clp->cl_list);
+ uu_avl_walk_end(walk);
+ uu_avl_destroy(clp->cl_tree);
}
if (clp->cl_pool)
- uu_list_pool_destroy(clp->cl_pool);
+ uu_avl_pool_destroy(clp->cl_pool);
free(clp);
}
+/*
+ * Add one dataset to changelist
+ */
+static int
+changelist_add_mounted(zfs_handle_t *zhp, void *data)
+{
+ prop_changelist_t *clp = data;
+ prop_changenode_t *cn;
+ uu_avl_index_t idx;
+
+ ASSERT3U(clp->cl_prop, ==, ZFS_PROP_MOUNTPOINT);
+
+ if ((cn = zfs_alloc(zfs_get_handle(zhp),
+ sizeof (prop_changenode_t))) == NULL) {
+ zfs_close(zhp);
+ return (ENOMEM);
+ }
+
+ cn->cn_handle = zhp;
+ cn->cn_mounted = zfs_is_mounted(zhp, NULL);
+ ASSERT3U(cn->cn_mounted, ==, B_TRUE);
+ cn->cn_shared = zfs_is_shared(zhp);
+ cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
+ cn->cn_needpost = B_TRUE;
+
+ /* Indicate if any child is exported to a local zone. */
+ if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
+ clp->cl_haszonedchild = B_TRUE;
+
+ uu_avl_node_init(cn, &cn->cn_treenode, clp->cl_pool);
+
+ if (uu_avl_find(clp->cl_tree, cn, NULL, &idx) == NULL) {
+ uu_avl_insert(clp->cl_tree, cn, idx);
+ } else {
+ free(cn);
+ zfs_close(zhp);
+ }
+
+ return (0);
+}
+
static int
change_one(zfs_handle_t *zhp, void *data)
{
if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
clp->cl_haszonedchild = B_TRUE;
- uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
+ uu_avl_node_init(cn, &cn->cn_treenode, clp->cl_pool);
- if (clp->cl_sorted) {
- uu_list_index_t idx;
+ uu_avl_index_t idx;
- (void) uu_list_find(clp->cl_list, cn, NULL,
- &idx);
- uu_list_insert(clp->cl_list, cn, idx);
+ if (uu_avl_find(clp->cl_tree, cn, NULL, &idx) == NULL) {
+ uu_avl_insert(clp->cl_tree, cn, idx);
} else {
- /*
- * Add this child to beginning of the list. Children
- * below this one in the hierarchy will get added above
- * this one in the list. This produces a list in
- * reverse dataset name order.
- * This is necessary when the original mountpoint
- * is legacy or none.
- */
- ASSERT(!clp->cl_alldependents);
- verify(uu_list_insert_before(clp->cl_list,
- uu_list_first(clp->cl_list), cn) == 0);
+ free(cn);
+ zfs_close(zhp);
}
if (!clp->cl_alldependents)
prop_changenode_t *cn;
zfs_handle_t *temp;
char property[ZFS_MAXPROPLEN];
- uu_compare_fn_t *compare = NULL;
boolean_t legacy = B_FALSE;
if ((clp = zfs_alloc(zhp->zfs_hdl, sizeof (prop_changelist_t))) == NULL)
NULL, NULL, 0, B_FALSE) == 0 &&
(strcmp(property, "legacy") == 0 ||
strcmp(property, "none") == 0)) {
-
legacy = B_TRUE;
}
- if (!legacy) {
- compare = compare_mountpoints;
- clp->cl_sorted = B_TRUE;
- }
}
- clp->cl_pool = uu_list_pool_create("changelist_pool",
+ clp->cl_pool = uu_avl_pool_create("changelist_pool",
sizeof (prop_changenode_t),
- offsetof(prop_changenode_t, cn_listnode),
- compare, 0);
+ offsetof(prop_changenode_t, cn_treenode),
+ compare_mountpoints, 0);
if (clp->cl_pool == NULL) {
assert(uu_error() == UU_ERROR_NO_MEMORY);
(void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
return (NULL);
}
- clp->cl_list = uu_list_create(clp->cl_pool, NULL,
- clp->cl_sorted ? UU_LIST_SORTED : 0);
+ clp->cl_tree = uu_avl_create(clp->cl_pool, NULL, UU_DEFAULT);
clp->cl_gflags = gather_flags;
clp->cl_mflags = mnt_flags;
- if (clp->cl_list == NULL) {
+ if (clp->cl_tree == NULL) {
assert(uu_error() == UU_ERROR_NO_MEMORY);
(void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
changelist_free(clp);
else if (clp->cl_prop == ZFS_PROP_SHARESMB)
clp->cl_shareprop = ZFS_PROP_SHARENFS;
- if (clp->cl_alldependents) {
+ if (clp->cl_prop == ZFS_PROP_MOUNTPOINT &&
+ (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) == 0) {
+ /*
+ * Instead of iterating through all of the dataset children we
+ * gather mounted dataset children from MNTTAB
+ */
+ if (zfs_iter_mounted(zhp, changelist_add_mounted, clp) != 0) {
+ changelist_free(clp);
+ return (NULL);
+ }
+ } else if (clp->cl_alldependents) {
if (zfs_iter_dependents(zhp, B_TRUE, change_one, clp) != 0) {
changelist_free(clp);
return (NULL);
cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
cn->cn_needpost = B_TRUE;
- uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
- if (clp->cl_sorted) {
- uu_list_index_t idx;
- (void) uu_list_find(clp->cl_list, cn, NULL, &idx);
- uu_list_insert(clp->cl_list, cn, idx);
+ uu_avl_node_init(cn, &cn->cn_treenode, clp->cl_pool);
+ uu_avl_index_t idx;
+ if (uu_avl_find(clp->cl_tree, cn, NULL, &idx) == NULL) {
+ uu_avl_insert(clp->cl_tree, cn, idx);
} else {
- /*
- * Add the target dataset to the end of the list.
- * The list is not really unsorted. The list will be
- * in reverse dataset name order. This is necessary
- * when the original mountpoint is legacy or none.
- */
- verify(uu_list_insert_after(clp->cl_list,
- uu_list_last(clp->cl_list), cn) == 0);
+ free(cn);
+ zfs_close(temp);
}
/*
--- /dev/null
+#!/bin/ksh -p
+#
+# CDDL HEADER START
+#
+# This file and its contents are supplied under the terms of the
+# Common Development and Distribution License ("CDDL"), version 1.0.
+# You may only use this file in accordance with the terms of version
+# 1.0 of the CDDL.
+#
+# A full copy of the text of the CDDL should have accompanied this
+# source. A copy is of the CDDL is also available via the Internet
+# at http://www.illumos.org/license/CDDL.
+#
+# CDDL HEADER END
+#
+
+#
+# Copyright (c) 2018 Datto Inc.
+#
+
+. $STF_SUITE/include/libtest.shlib
+
+#
+# DESCRIPTION:
+# zfs unmount should work on nested datasets
+#
+# STRATEGY:
+# 1. Create a set of nested datasets
+# 2. Unmount a nested dataset and make sure it is unmounted
+# 3. Ensure the dataset deeper than the one above is also unmounted
+# 4. Ensure the datasets shallower than the unmounted one is still mounted
+# 5. Repeat from step 2 with other mountpoint values and shallower nesting
+#
+
+verify_runnable "both"
+
+function nesting_cleanup
+{
+ log_must zfs destroy -fR $TESTPOOL/a
+ log_must zfs destroy -fR $TESTPOOL/b
+ log_must zfs destroy -fR $TESTPOOL/c
+ log_must zfs destroy -fR $TESTPOOL/d
+}
+
+log_onexit nesting_cleanup
+
+set -A test_depths 30 16 3
+
+dsA32=$(printf 'a/%.0s' {1..32})"a"
+log_must zfs create -p $TESTPOOL/$dsA32
+
+dsB32=$(printf 'b/%.0s' {1..32})"b"
+log_must zfs create -o mountpoint=none -p $TESTPOOL/$dsB32
+log_mustnot mount -t zfs $TESTPOOL/$dsB32 /mnt
+
+dsC32=$(printf 'c/%.0s' {1..32})"c"
+log_must zfs create -o mountpoint=legacy -p $TESTPOOL/$dsC32
+log_must mount -t zfs $TESTPOOL/$dsC32 /mnt
+
+dsD32=$(printf 'd/%.0s' {1..32})"d"
+log_must zfs create -o mountpoint=/$TESTPOOL/mnt -p $TESTPOOL/$dsD32
+
+
+for d in ${test_depths[@]}; do
+ # default mountpoint
+ ds_pre=$(printf 'a/%.0s' {1..$(($d-2))})"a"
+ ds=$(printf 'a/%.0s' {1..$(($d-1))})"a"
+ ds_post=$(printf 'a/%.0s' {1..$(($d))})"a"
+ if ! ismounted $TESTPOOL/$ds_pre; then
+ log_fail "$TESTPOOL/$ds_pre (pre) not initially mounted"
+ fi
+ if ! ismounted $TESTPOOL/$ds; then
+ log_fail "$TESTPOOL/$ds not initially mounted"
+ fi
+ if ! ismounted $TESTPOOL/$ds_post; then
+ log_fail "$TESTPOOL/$ds_post (post) not initially mounted"
+ fi
+
+ log_must zfs snapshot $TESTPOOL/$ds@snap
+ # force snapshot mount in .zfs
+ log_must ls /$TESTPOOL/$ds/.zfs/snapshot/snap
+ log_must zfs unmount $TESTPOOL/$ds
+
+ if ! ismounted $TESTPOOL/$ds_pre; then
+ log_fail "$ds_pre is not mounted"
+ fi
+ if ismounted $TESTPOOL/$ds; then
+ log_fail "$ds is mounted"
+ fi
+ if ismounted $TESTPOOL/$ds_post; then
+ log_fail "$ds_post (post) is mounted"
+ fi
+
+
+ # mountpoint=none
+ ds_pre=$(printf 'b/%.0s' {1..$(($d-2))})"b"
+ ds=$(printf 'b/%.0s' {1..$(($d-1))})"b"
+ ds_post=$(printf 'b/%.0s' {1..$(($d))})"b"
+ if ! ismounted $TESTPOOL/$ds_pre; then
+ log_fail "$TESTPOOL/$ds_pre (pre) not initially mounted"
+ fi
+ if ! ismounted $TESTPOOL/$ds; then
+ log_fail "$TESTPOOL/$ds not initially mounted"
+ fi
+ if ! ismounted $TESTPOOL/$ds_post; then
+ log_fail "$TESTPOOL/$ds_post (post) not initially mounted"
+ fi
+
+ log_must zfs snapshot $TESTPOOL/$ds@snap
+ # force snapshot mount in .zfs
+ log_must ls /$TESTPOOL/$ds/.zfs/snapshot/snap
+ log_must zfs unmount $TESTPOOL/$ds
+
+ if ! ismounted $TESTPOOL/$ds_pre; then
+ log_fail "$TESTPOOL/$ds_pre (pre) not mounted"
+ fi
+ if ismounted $TESTPOOL/$ds; then
+ log_fail "$TESTPOOL/$ds is mounted"
+ fi
+ if ismounted $TESTPOOL/$ds_post; then
+ log_fail "$TESTPOOL/$ds_post (post) is mounted"
+ fi
+
+
+ # mountpoint=legacy
+ ds_pre=$(printf 'c/%.0s' {1..$(($d-2))})"c"
+ ds=$(printf 'c/%.0s' {1..$(($d-1))})"c"
+ ds_post=$(printf 'c/%.0s' {1..$(($d))})"c"
+ if ! ismounted $TESTPOOL/$ds_pre; then
+ log_fail "$TESTPOOL/$ds_pre (pre) not initially mounted"
+ fi
+ if ! ismounted $TESTPOOL/$ds; then
+ log_fail "$TESTPOOL/$ds not initially mounted"
+ fi
+ if ! ismounted $TESTPOOL/$ds_post; then
+ log_fail "$TESTPOOL/$ds_post (post) not initially mounted"
+ fi
+
+ log_must zfs snapshot $TESTPOOL/$ds@snap
+ # force snapshot mount in .zfs
+ log_must ls /$TESTPOOL/$ds/.zfs/snapshot/snap
+ log_must zfs unmount $TESTPOOL/$ds
+
+ if ! ismounted $TESTPOOL/$ds_pre; then
+ log_fail "$TESTPOOL/$ds_pre (pre) not mounted"
+ fi
+ if ismounted $TESTPOOL/$ds; then
+ log_fail "$TESTPOOL/$ds is mounted"
+ fi
+ if ismounted $TESTPOOL/$ds_post; then
+ log_fail "$TESTPOOL/$ds_post (post) is mounted"
+ fi
+
+
+ # mountpoint=testpool/mnt
+ ds_pre=$(printf 'd/%.0s' {1..$(($d-2))})"d"
+ ds=$(printf 'd/%.0s' {1..$(($d-1))})"d"
+ ds_post=$(printf 'd/%.0s' {1..$(($d))})"d"
+ if ! ismounted $TESTPOOL/$ds_pre; then
+ log_fail "$TESTPOOL/$ds_pre (pre) not initially mounted"
+ fi
+ if ! ismounted $TESTPOOL/$ds; then
+ log_fail "$TESTPOOL/$ds not initially mounted"
+ fi
+ if ! ismounted $TESTPOOL/$ds_post; then
+ log_fail "$TESTPOOL/$ds_post (post) not initially mounted"
+ fi
+
+ log_must zfs snapshot $TESTPOOL/$ds@snap
+ # force snapshot mount in .zfs
+ log_must ls /$TESTPOOL/$ds/.zfs/snapshot/snap
+ log_must zfs unmount $TESTPOOL/$ds
+
+ if ! ismounted $TESTPOOL/$ds_pre; then
+ log_fail "$ds_pre is not mounted"
+ fi
+ if ismounted $TESTPOOL/$ds; then
+ log_fail "$ds is mounted"
+ fi
+ if ismounted $TESTPOOL/$ds_post; then
+ log_fail "$ds_post (post) is mounted"
+ fi
+done
+
+log_must zpool export $TESTPOOL
+log_must rmdir /testpool/mnt # remove the mountpoint we created
+log_must zpool import $TESTPOOL
+
+log_pass "Verified nested dataset are unmounted."