--- old/usr/src/uts/common/fs/zfs/dsl_dir.c	Tue Nov  6 13:39:29 2012
+++ new/usr/src/uts/common/fs/zfs/dsl_dir.c	Tue Nov  6 13:39:29 2012
@@ -21,6 +21,7 @@
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  * Copyright (c) 2012 by Delphix. All rights reserved.
+ * Copyright (c) 2012 Joyent, Inc. All rights reserved.
  */
 
 #include <sys/dmu.h>
@@ -38,12 +39,85 @@
 #include <sys/arc.h>
 #include <sys/sunddi.h>
 #include <sys/zfs_zone.h>
+#include <sys/zfeature.h>
 #include "zfs_namecheck.h"
+#include "zfs_prop.h"
 
+/*
+ * Filesystem and Snapshot Limits
+ * ------------------------------
+ *
+ * These limits are used to restrict the number of filesystems and/or snapshots
+ * that can be created at a given level in the tree or below. The standard
+ * use-case is with a delegated dataset where the administrator wants to ensure
+ * that a user within the zone is not creating too many additional filesystems
+ * or snapshots, even though they're not exceeding their space quota.
+ *
+ * The count of filesystems and snapshots is stored in the dsl_dir_phys_t which
+ * impacts the on-disk format. As such, this capability is controlled by a
+ * feature flag and must be enabled to be used. Once enabled, the feature is
+ * not active until the first limit is set. At that point, future operations to
+ * create/destroy filesystems or snapshots will validate and update the counts.
+ *
+ * Because the on-disk counts will be uninitialized (0) before the feature is
+ * active, the counts are updated when a limit is first set on an uninitialized
+ * node (The filesystem/snapshot counts on a node includes all of the nested
+ * filesystems/snapshots, plus the node itself. Thus, a new leaf node has a
+ * filesystem count of 1 and a snapshot count of 0. A filesystem count of 0 on
+ * a node indicates uninitialized counts on that node.) When setting a limit on
+ * an uninitialized node, the code starts at the filesystem with the new limit
+ * and descends into all sub-filesystems and updates the counts to be accurate.
+ * In practice this is lightweight since a limit is typically set when the
+ * filesystem is created and thus has no children. Once valid, changing the
+ * limit value won't require a re-traversal since the counts are already valid.
+ * When recursively fixing the counts, if a node with a limit is encountered
+ * during the descent, the counts are known to be valid and there is no need to
+ * descend into that filesystem's children. The counts on filesystems above the
+ * one with the new limit will still be uninitialized (0), unless a limit is
+ * eventually set on one of those filesystems. It is possible for the counts
+ * to appear initialized, but be invalid, if the feature was previously active
+ * but then deactivated. For this reason, the counts are always recursively
+ * updated when a limit is set on a dataset, unless there is already a limit.
+ * When a new limit value is set on a filesystem with an existing limit, the
+ * new limit must be greater than the current count at that level or an error
+ * is returned and the limit is not changed.
+ *
+ * Once the feature is active, then whenever a filesystem or snapshot is
+ * created, the code recurses up the tree, validating the new count against the
+ * limit at each initialized level. In practice, most levels will not have a
+ * limit set. If there is a limit at any initialized level up the tree, the
+ * check must pass or the creation will fail. Likewise, when a filesystem or
+ * snapshot is destroyed, the counts are recursively adjusted all the way up
+ * the initizized nodes in the tree. Renaming a filesystem into different point
+ * in the tree will first validate, then update the counts on each branch up to
+ * the common ancestor. A receive will also validate the counts and then update
+ * them.
+ *
+ * An exception to the above behavior is that the limits are never enforced
+ * for the administrative user in the global zone. This is primarily so that
+ * recursive snapshots in the global zone always work. We want to prevent a
+ * denial-of-service in which a lower level delegated dataset could max out its
+ * limit and thus block recursive snapshots from being taken in the global zone.
+ * Because of this, it is possible for the snapshot count to be over the limit
+ * and snapshots taken in the global zone could cause a lower level dataset to
+ * hit or exceed its limit. The administrator taking the global zone recursive
+ * snapshot should be aware of this side-effect and behave accordingly.
+ * For consistency, the filesystem limit is also not enforced for the admin
+ * user in the global zone.
+ *
+ * The filesystem limit is validated by dsl_dir_fscount_check() and updated by
+ * dsl_dir_fscount_adjust(). The snapshot limit is validated by
+ * dsl_snapcount_check() and updated by dsl_snapcount_adjust().
+ * A new limit value is validated in dsl_dir_validate_fs_ss_limit() and the
+ * filesystem counts are adjusted, if necessary, by dsl_dir_set_fs_ss_count().
+ */
+
 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
 static void dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd,
     uint64_t value, dmu_tx_t *tx);
 
+extern dsl_syncfunc_t dsl_prop_set_sync;
+
 /* ARGSUSED */
 static void
 dsl_dir_evict(dmu_buf_t *db, void *arg)
@@ -407,6 +481,365 @@
 	return (dsl_dir_open_spa(NULL, name, tag, ddp, tailp));
 }
 
+/*
+ * Check if the counts are already valid for this filesystem and its
+ * descendants. The counts on this filesystem, and those below, may be
+ * uninitialized due to either the use of a pre-existing pool which did not
+ * support the filesystem/snapshot limit feature, or one in which the feature
+ * had not yet been enabled. The counts can also be invalid if the feature was
+ * previously active but then deactivated.
+ *
+ * Recursively descend the filesystem tree and update the filesystem/snapshot
+ * counts on each filesystem below, then update the cumulative count on the
+ * current filesystem. If the filesystem already has a limit set on it,
+ * then we know that its counts, and the counts on the filesystems below it,
+ * have been updated to be correct, so we can skip this filesystem.
+ */
+static void
+dsl_dir_set_fs_ss_count(const char *nm, dsl_dir_t *dd, dmu_tx_t *tx,
+    uint64_t *fscnt, uint64_t *sscnt)
+{
+	uint64_t my_fs_cnt = 0;
+	uint64_t my_ss_cnt = 0;
+	objset_t *os = dd->dd_pool->dp_meta_objset;
+	zap_cursor_t *zc;
+	zap_attribute_t *za;
+	char *namebuf;
+	int err;
+	boolean_t limit_set = B_FALSE;
+	uint64_t fslimit, sslimit;
+	dsl_dataset_t *ds;
+
+	err = dsl_prop_get_dd(dd, zfs_prop_to_name(ZFS_PROP_FILESYSTEM_LIMIT),
+	    8, 1, &fslimit, NULL, B_FALSE);
+	if (err == 0 && fslimit != MAXLIMIT)
+		limit_set = B_TRUE;
+
+	if (!limit_set) {
+		err = dsl_prop_get_dd(dd,
+		    zfs_prop_to_name(ZFS_PROP_SNAPSHOT_LIMIT), 8, 1, &sslimit,
+		    NULL, B_FALSE);
+		if (err == 0 && sslimit != MAXLIMIT)
+			limit_set = B_TRUE;
+	}
+
+	/*
+	 * If the dd has a limit, we know its count is already good and we
+	 * don't need to recurse down any further.
+	 *
+	 * We can't check for an initialized (non-0) count since the feature
+	 * might have been previously active, then deactivated and is now
+	 * being activated again.
+	 */
+	if (limit_set) {
+		*fscnt = dd->dd_phys->dd_filesystem_count;
+		*sscnt = dd->dd_phys->dd_snapshot_count;
+		return;
+	}
+
+	zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
+	za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
+	namebuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
+
+	mutex_enter(&dd->dd_lock);
+
+	/* Iterate datasets */
+	for (zap_cursor_init(zc, os, dd->dd_phys->dd_child_dir_zapobj);
+	    zap_cursor_retrieve(zc, za) == 0;
+	    zap_cursor_advance(zc)) {
+		dsl_dir_t *chld_dd;
+		uint64_t chld_fs_cnt = 0;
+		uint64_t chld_ss_cnt = 0;
+
+		(void) snprintf(namebuf, MAXPATHLEN, "%s/%s", nm, za->za_name);
+
+		if (dsl_dir_open(namebuf, FTAG, &chld_dd, NULL))
+			continue;
+
+		dsl_dir_set_fs_ss_count(namebuf, chld_dd, tx, &chld_fs_cnt,
+		    &chld_ss_cnt);
+
+		dsl_dir_close(chld_dd, FTAG);
+
+		my_fs_cnt += chld_fs_cnt;
+		my_ss_cnt += chld_ss_cnt;
+	}
+	zap_cursor_fini(zc);
+
+	kmem_free(namebuf, MAXPATHLEN);
+
+	/* Iterate snapshots */
+	if (dsl_dataset_hold(nm, FTAG, &ds) == 0) {
+		for (zap_cursor_init(zc, os, ds->ds_phys->ds_snapnames_zapobj);
+		    zap_cursor_retrieve(zc, za) == 0;
+		    zap_cursor_advance(zc)) {
+			my_ss_cnt++;
+		}
+		zap_cursor_fini(zc);
+		dsl_dataset_rele(ds, FTAG);
+	}
+
+	kmem_free(zc, sizeof (zap_cursor_t));
+	kmem_free(za, sizeof (zap_attribute_t));
+
+	/* Add 1 for self */
+	my_fs_cnt++;
+
+#ifdef _KERNEL
+	extern void __dtrace_probe_zfs__fs__fix__count(char *, uint64_t,
+	    uint64_t);
+	__dtrace_probe_zfs__fs__fix__count((char *)nm, my_fs_cnt, my_ss_cnt);
+#endif
+
+	/* save updated counts */
+	dmu_buf_will_dirty(dd->dd_dbuf, tx);
+	dd->dd_phys->dd_filesystem_count = my_fs_cnt;
+	dd->dd_phys->dd_snapshot_count = my_ss_cnt;
+
+	mutex_exit(&dd->dd_lock);
+
+	/* Return child dataset count plus self */
+	*fscnt = my_fs_cnt;
+	*sscnt = my_ss_cnt;
+}
+
+/*
+ * Return ENOSPC if new limit is less than the existing count, otherwise return
+ * -1 to force the zfs_set_prop_nvlist code down the default path to set the
+ * value in the nvlist.
+ */
+int
+dsl_dir_validate_fs_ss_limit(const char *ddname, uint64_t limit,
+    zfs_prop_t ptype)
+{
+	dsl_dir_t *dd;
+	dsl_dataset_t *ds;
+	int err = -1;
+	uint64_t count;
+	dmu_tx_t *tx;
+	uint64_t my_fs_cnt = 0;
+	uint64_t my_ss_cnt = 0;
+	uint64_t curr_limit;
+	spa_t *spa;
+	zfeature_info_t *limit_feat =
+	    &spa_feature_table[SPA_FEATURE_FS_SS_LIMIT];
+
+	if (dsl_dataset_hold(ddname, FTAG, &ds))
+		return (EACCES);
+
+	spa = dsl_dataset_get_spa(ds);
+	if (!spa_feature_is_enabled(spa,
+	    &spa_feature_table[SPA_FEATURE_FS_SS_LIMIT])) {
+		dsl_dataset_rele(ds, FTAG);
+		return (ENOTSUP);
+	}
+
+	if (dsl_dir_open(ddname, FTAG, &dd, NULL)) {
+		dsl_dataset_rele(ds, FTAG);
+		return (EACCES);
+	}
+
+	ASSERT(ds->ds_dir == dd);
+
+	if (dsl_prop_get_dd(dd, zfs_prop_to_name(ptype), 8, 1, &curr_limit,
+	    NULL, B_FALSE) != 0)
+		curr_limit = MAXLIMIT;
+
+	tx = dmu_tx_create_dd(dd);
+	if (dmu_tx_assign(tx, TXG_WAIT)) {
+		dmu_tx_abort(tx);
+		dsl_dir_close(dd, FTAG);
+		dsl_dataset_rele(ds, FTAG);
+		return (ENOSPC);
+	}
+
+	if (limit == MAXLIMIT) {
+		/*
+		 * If we had a limit, since we're now removing that limit,
+		 * decrement the feature-active counter so that the feature
+		 * becomes inactive (only enabled) if we remove the last limit.
+		 */
+		if (curr_limit != MAXLIMIT)
+			spa_feature_decr(spa, limit_feat, tx);
+
+		dmu_tx_commit(tx);
+		dsl_dir_close(dd, FTAG);
+		dsl_dataset_rele(ds, FTAG);
+		return (-1);
+	}
+
+	/*
+	 * Since we are now setting a non-MAXLIMIT on the filesystem, we need
+	 * to ensure the counts are correct. Descend down the tree from this
+	 * point and update all of the counts to be accurate.
+	 */
+	rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
+	dsl_dir_set_fs_ss_count(ddname, dd, tx, &my_fs_cnt, &my_ss_cnt);
+	rw_exit(&dd->dd_pool->dp_config_rwlock);
+
+	if (ptype == ZFS_PROP_FILESYSTEM_LIMIT)
+		count = dd->dd_phys->dd_filesystem_count;
+	else
+		count = dd->dd_phys->dd_snapshot_count;
+
+	if (limit < count) {
+		err = ENOSPC;
+	} else {
+		/*
+		 * If we had no limit, since we're now setting a limit
+		 * increment the feature-active counter so that the feature
+		 * either becomes active for the first time, or the count
+		 * simply increases so that we can decrement it when we remove
+		 * the limit.
+		 */
+		if (curr_limit == MAXLIMIT)
+			spa_feature_incr(spa, limit_feat, tx);
+	}
+
+	dmu_tx_commit(tx);
+
+	dsl_dir_close(dd, FTAG);
+	dsl_dataset_rele(ds, FTAG);
+
+	return (err);
+}
+
+/*
+ * Check if adding additional child filesystem(s) would exceed any filesystem
+ * limits. Note that all filesystem limits up to the root (or the highest
+ * initialized) filesystem or the given ancestor must be satisfied.
+ */
+int
+dsl_dir_fscount_check(dsl_dir_t *dd, uint64_t cnt, dsl_dir_t *ancestor)
+{
+	uint64_t limit;
+	int err = 0;
+
+	VERIFY(RW_LOCK_HELD(&dd->dd_pool->dp_config_rwlock));
+
+	/*
+	 * The limit is never enforced for the admin user in global zone.
+	 * If we're not in the global zone then we need to run this check in
+	 * open context, since thats when we know what zone we're in and
+	 * syncing is only performed in the global zone.
+	 */
+	if (INGLOBALZONE(curproc))
+		return (0);
+
+	/*
+	 * If an ancestor has been provided, stop checking the limit once we
+	 * hit that dir. We need this during rename so that we don't overcount
+	 * the check once we recurse up to the common ancestor.
+	 */
+	if (ancestor == dd)
+		return (0);
+
+	/*
+	 * If we hit an uninitialized node while recursing up the tree, we can
+	 * stop since we know the counts are not valid on this node and we
+	 * know we won't touch this node's counts.
+	 */
+	if (dd->dd_phys->dd_filesystem_count == 0)
+		return (0);
+
+	/*
+	 * If there's no value for this property, there's no need to enforce a
+	 * filesystem limit.
+	 */
+	err = dsl_prop_get_dd(dd, zfs_prop_to_name(ZFS_PROP_FILESYSTEM_LIMIT),
+	    8, 1, &limit, NULL, B_FALSE);
+	if (err == ENOENT)
+		return (0);
+	else if (err != 0)
+		return (err);
+
+#ifdef _KERNEL
+	extern void __dtrace_probe_zfs__fs__limit(uint64_t, uint64_t, char *);
+	__dtrace_probe_zfs__fs__limit(
+	    (uint64_t)dd->dd_phys->dd_filesystem_count, (uint64_t)limit,
+	    dd->dd_myname);
+#endif
+
+	if (limit != MAXLIMIT &&
+	    (dd->dd_phys->dd_filesystem_count + cnt) > limit)
+		return (EDQUOT);
+
+	if (dd->dd_parent != NULL)
+		err = dsl_dir_fscount_check(dd->dd_parent, cnt, ancestor);
+
+	return (err);
+}
+
+/*
+ * Adjust the filesystem count for the specified dsl_dir_t and all parent
+ * filesystems. When a new filesystem is created, increment the count on all
+ * parents, and when a filesystem is destroyed, decrement the count.
+ */
+void
+dsl_dir_fscount_adjust(dsl_dir_t *dd, dmu_tx_t *tx, int64_t delta,
+    boolean_t syncing, boolean_t first)
+{
+	VERIFY(RW_LOCK_HELD(&dd->dd_pool->dp_config_rwlock));
+	if (syncing)
+		VERIFY(dmu_tx_is_syncing(tx));
+
+	/*
+	 * There is a special case where we are receiving a filesystem that
+	 * already exists. In this case a temporary clone name of %X is created
+	 * (see dmu_recv_begin). In dmu_recv_existing_end we destroy this
+	 * temporary clone. We never update the filesystem counts for temporary
+	 * clones. To detect this case we check the filesystem name to see if
+	 * its a hidden filesystem (%X).
+	 */
+	if (dd->dd_myname[0] == '%')
+		return;
+
+	/*
+	 * If we hit an uninitialized node while recursing up the tree, we can
+	 * stop since we know the counts are not valid on this node and we
+	 * know we shouldn't touch this node's counts. An uninitialized count
+	 * on the node indicates that either the feature has not yet been
+	 * activated or there are no limits on this part of the tree.
+	 */
+	if (dd->dd_phys->dd_filesystem_count == 0)
+		return;
+
+	/*
+	 * The feature might have previously been active, so there could be
+	 * non-0 counts on the nodes, but it might now be inactive.
+	 *
+	 * On initial entry we need to check if this feature is active, but
+	 * we don't want to re-check this on each recursive call. Note: the
+	 * feature cannot be active if its not enabled. If the feature is not
+	 * active, don't touch the on-disk count fields.
+	 */
+	if (first) {
+		dsl_dataset_t *ds = NULL;
+		spa_t *spa;
+		zfeature_info_t *quota_feat =
+		    &spa_feature_table[SPA_FEATURE_FS_SS_LIMIT];
+
+		VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
+		    dd->dd_phys->dd_head_dataset_obj, FTAG, &ds));
+		spa = dsl_dataset_get_spa(ds);
+		dsl_dataset_rele(ds, FTAG);
+		if (!spa_feature_is_active(spa, quota_feat))
+			return;
+	}
+
+	dmu_buf_will_dirty(dd->dd_dbuf, tx);
+
+	mutex_enter(&dd->dd_lock);
+
+	dd->dd_phys->dd_filesystem_count += delta;
+
+	if (dd->dd_parent != NULL)
+		dsl_dir_fscount_adjust(dd->dd_parent, tx, delta, syncing,
+		    B_FALSE);
+
+	mutex_exit(&dd->dd_lock);
+}
+
 uint64_t
 dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
     dmu_tx_t *tx)
@@ -415,7 +848,10 @@
 	uint64_t ddobj;
 	dsl_dir_phys_t *ddphys;
 	dmu_buf_t *dbuf;
+	zfeature_info_t *limit_feat =
+	    &spa_feature_table[SPA_FEATURE_FS_SS_LIMIT];
 
+
 	ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
 	    DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
 	if (pds) {
@@ -431,6 +867,9 @@
 	ddphys = dbuf->db_data;
 
 	ddphys->dd_creation_time = gethrestime_sec();
+	/* Only initialize the count if the limit feature is active */
+	if (spa_feature_is_active(dp->dp_spa, limit_feat))
+		ddphys->dd_filesystem_count = 1;
 	if (pds)
 		ddphys->dd_parent_obj = pds->dd_object;
 	ddphys->dd_props_zapobj = zap_create(mos,
@@ -487,6 +926,10 @@
 	ASSERT(RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock));
 	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
 
+	/* Decrement the filesystem count for all parent filesystems. */
+	if (dd->dd_parent != NULL)
+		dsl_dir_fscount_adjust(dd->dd_parent, tx, -1, B_TRUE, B_TRUE);
+
 	/*
 	 * Remove our reservation. The impl() routine avoids setting the
 	 * actual property, which would require the (already destroyed) ds.
@@ -1036,8 +1479,6 @@
 	return (err);
 }
 
-extern dsl_syncfunc_t dsl_prop_set_sync;
-
 static void
 dsl_dir_set_quota_sync(void *arg1, void *arg2, dmu_tx_t *tx)
 {
@@ -1278,7 +1719,7 @@
 			return (EINVAL);
 
 		if (err = dsl_dir_transfer_possible(dd->dd_parent,
-		    ra->newparent, myspace))
+		    ra->newparent, dd, myspace, tx))
 			return (err);
 	}
 
@@ -1303,6 +1744,20 @@
 	    "-> %s/%s", namebuf, ra->mynewname);
 
 	if (ra->newparent != dd->dd_parent) {
+		int cnt;
+
+		mutex_enter(&dd->dd_lock);
+
+		cnt = dd->dd_phys->dd_filesystem_count;
+		dsl_dir_fscount_adjust(dd->dd_parent, tx, -cnt, B_TRUE, B_TRUE);
+		dsl_dir_fscount_adjust(ra->newparent, tx, cnt, B_TRUE, B_TRUE);
+
+		cnt = dd->dd_phys->dd_snapshot_count;
+		dsl_snapcount_adjust(dd->dd_parent, tx, -cnt, B_TRUE);
+		dsl_snapcount_adjust(ra->newparent, tx, cnt, B_TRUE);
+
+		mutex_exit(&dd->dd_lock);
+
 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
 		    -dd->dd_phys->dd_used_bytes,
 		    -dd->dd_phys->dd_compressed_bytes,
@@ -1375,11 +1830,13 @@
 }
 
 int
-dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, uint64_t space)
+dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, dsl_dir_t *moving_dd,
+    uint64_t space, dmu_tx_t *tx)
 {
 	dsl_dir_t *ancestor;
 	int64_t adelta;
 	uint64_t avail;
+	int err;
 
 	ancestor = closest_common_ancestor(sdd, tdd);
 	adelta = would_change(sdd, -space, ancestor);
@@ -1387,6 +1844,17 @@
 	if (avail < space)
 		return (ENOSPC);
 
+	if (sdd != moving_dd) {
+		err = dsl_dir_fscount_check(tdd,
+		    moving_dd->dd_phys->dd_filesystem_count, ancestor);
+		if (err != 0)
+			return (err);
+	}
+	err = dsl_snapcount_check(tdd, moving_dd->dd_phys->dd_snapshot_count,
+	    ancestor);
+	if (err != 0)
+		return (err);
+
 	return (0);
 }
 
