xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_dir.c (revision ed093b41a93e8563e6e1e5dae0768dda2a7bcc27)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
24  * Copyright (c) 2013 Martin Matuska. All rights reserved.
25  * Copyright (c) 2014 Joyent, Inc. All rights reserved.
26  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
28  */
29 
30 #include <sys/dmu.h>
31 #include <sys/dmu_objset.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dsl_dataset.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dsl_prop.h>
36 #include <sys/dsl_synctask.h>
37 #include <sys/dsl_deleg.h>
38 #include <sys/dmu_impl.h>
39 #include <sys/spa.h>
40 #include <sys/spa_impl.h>
41 #include <sys/metaslab.h>
42 #include <sys/zap.h>
43 #include <sys/zio.h>
44 #include <sys/arc.h>
45 #include <sys/sunddi.h>
46 #include <sys/zfeature.h>
47 #include <sys/policy.h>
48 #include <sys/zfs_znode.h>
49 #include "zfs_namecheck.h"
50 #include "zfs_prop.h"
51 
52 /*
53  * Filesystem and Snapshot Limits
54  * ------------------------------
55  *
56  * These limits are used to restrict the number of filesystems and/or snapshots
57  * that can be created at a given level in the tree or below. A typical
58  * use-case is with a delegated dataset where the administrator wants to ensure
59  * that a user within the zone is not creating too many additional filesystems
60  * or snapshots, even though they're not exceeding their space quota.
61  *
62  * The filesystem and snapshot counts are stored as extensible properties. This
63  * capability is controlled by a feature flag and must be enabled to be used.
64  * Once enabled, the feature is not active until the first limit is set. At
65  * that point, future operations to create/destroy filesystems or snapshots
66  * will validate and update the counts.
67  *
68  * Because the count properties will not exist before the feature is active,
69  * the counts are updated when a limit is first set on an uninitialized
70  * dsl_dir node in the tree (The filesystem/snapshot count on a node includes
71  * all of the nested filesystems/snapshots. Thus, a new leaf node has a
72  * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and
73  * snapshot count properties on a node indicate uninitialized counts on that
74  * node.) When first setting a limit on an uninitialized node, the code starts
75  * at the filesystem with the new limit and descends into all sub-filesystems
76  * to add the count properties.
77  *
78  * In practice this is lightweight since a limit is typically set when the
79  * filesystem is created and thus has no children. Once valid, changing the
80  * limit value won't require a re-traversal since the counts are already valid.
81  * When recursively fixing the counts, if a node with a limit is encountered
82  * during the descent, the counts are known to be valid and there is no need to
83  * descend into that filesystem's children. The counts on filesystems above the
84  * one with the new limit will still be uninitialized, unless a limit is
85  * eventually set on one of those filesystems. The counts are always recursively
86  * updated when a limit is set on a dataset, unless there is already a limit.
87  * When a new limit value is set on a filesystem with an existing limit, it is
88  * possible for the new limit to be less than the current count at that level
89  * since a user who can change the limit is also allowed to exceed the limit.
90  *
91  * Once the feature is active, then whenever a filesystem or snapshot is
92  * created, the code recurses up the tree, validating the new count against the
93  * limit at each initialized level. In practice, most levels will not have a
94  * limit set. If there is a limit at any initialized level up the tree, the
95  * check must pass or the creation will fail. Likewise, when a filesystem or
96  * snapshot is destroyed, the counts are recursively adjusted all the way up
97  * the initizized nodes in the tree. Renaming a filesystem into different point
98  * in the tree will first validate, then update the counts on each branch up to
99  * the common ancestor. A receive will also validate the counts and then update
100  * them.
101  *
102  * An exception to the above behavior is that the limit is not enforced if the
103  * user has permission to modify the limit. This is primarily so that
104  * recursive snapshots in the global zone always work. We want to prevent a
105  * denial-of-service in which a lower level delegated dataset could max out its
106  * limit and thus block recursive snapshots from being taken in the global zone.
107  * Because of this, it is possible for the snapshot count to be over the limit
108  * and snapshots taken in the global zone could cause a lower level dataset to
109  * hit or exceed its limit. The administrator taking the global zone recursive
110  * snapshot should be aware of this side-effect and behave accordingly.
111  * For consistency, the filesystem limit is also not enforced if the user can
112  * modify the limit.
113  *
114  * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check()
115  * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in
116  * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by
117  * dsl_dir_init_fs_ss_count().
118  *
119  * There is a special case when we receive a filesystem that already exists. In
120  * this case a temporary clone name of %X is created (see dmu_recv_begin). We
121  * never update the filesystem counts for temporary clones.
122  *
123  * Likewise, we do not update the snapshot counts for temporary snapshots,
124  * such as those created by zfs diff.
125  */
126 
127 extern inline dsl_dir_phys_t *dsl_dir_phys(dsl_dir_t *dd);
128 
129 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
130 
131 typedef struct ddulrt_arg {
132 	dsl_dir_t	*ddulrta_dd;
133 	uint64_t	ddlrta_txg;
134 } ddulrt_arg_t;
135 
136 static void
137 dsl_dir_evict_async(void *dbu)
138 {
139 	dsl_dir_t *dd = dbu;
140 	dsl_pool_t *dp = dd->dd_pool;
141 	int t;
142 
143 	dd->dd_dbuf = NULL;
144 
145 	for (t = 0; t < TXG_SIZE; t++) {
146 		ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
147 		ASSERT(dd->dd_tempreserved[t] == 0);
148 		ASSERT(dd->dd_space_towrite[t] == 0);
149 	}
150 
151 	if (dd->dd_parent)
152 		dsl_dir_async_rele(dd->dd_parent, dd);
153 
154 	spa_async_close(dd->dd_pool->dp_spa, dd);
155 
156 	dsl_prop_fini(dd);
157 	mutex_destroy(&dd->dd_lock);
158 	kmem_free(dd, sizeof (dsl_dir_t));
159 }
160 
161 int
162 dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
163     const char *tail, void *tag, dsl_dir_t **ddp)
164 {
165 	dmu_buf_t *dbuf;
166 	dsl_dir_t *dd;
167 	dmu_object_info_t doi;
168 	int err;
169 
170 	ASSERT(dsl_pool_config_held(dp));
171 
172 	err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
173 	if (err != 0)
174 		return (err);
175 	dd = dmu_buf_get_user(dbuf);
176 
177 	dmu_object_info_from_db(dbuf, &doi);
178 	ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR);
179 	ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
180 
181 	if (dd == NULL) {
182 		dsl_dir_t *winner;
183 
184 		dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
185 		dd->dd_object = ddobj;
186 		dd->dd_dbuf = dbuf;
187 		dd->dd_pool = dp;
188 
189 		if (dsl_dir_is_zapified(dd) &&
190 		    zap_contains(dp->dp_meta_objset, ddobj,
191 		    DD_FIELD_CRYPTO_KEY_OBJ) == 0) {
192 			VERIFY0(zap_lookup(dp->dp_meta_objset,
193 			    ddobj, DD_FIELD_CRYPTO_KEY_OBJ,
194 			    sizeof (uint64_t), 1, &dd->dd_crypto_obj));
195 
196 			/* check for on-disk format errata */
197 			if (dsl_dir_incompatible_encryption_version(dd)) {
198 				dp->dp_spa->spa_errata =
199 				    ZPOOL_ERRATA_ZOL_6845_ENCRYPTION;
200 			}
201 		}
202 
203 		mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
204 		dsl_prop_init(dd);
205 
206 		dsl_dir_snap_cmtime_update(dd);
207 
208 		if (dsl_dir_phys(dd)->dd_parent_obj) {
209 			err = dsl_dir_hold_obj(dp,
210 			    dsl_dir_phys(dd)->dd_parent_obj, NULL, dd,
211 			    &dd->dd_parent);
212 			if (err != 0)
213 				goto errout;
214 			if (tail) {
215 #ifdef ZFS_DEBUG
216 				uint64_t foundobj;
217 
218 				err = zap_lookup(dp->dp_meta_objset,
219 				    dsl_dir_phys(dd->dd_parent)->
220 				    dd_child_dir_zapobj, tail,
221 				    sizeof (foundobj), 1, &foundobj);
222 				ASSERT(err || foundobj == ddobj);
223 #endif
224 				(void) strcpy(dd->dd_myname, tail);
225 			} else {
226 				err = zap_value_search(dp->dp_meta_objset,
227 				    dsl_dir_phys(dd->dd_parent)->
228 				    dd_child_dir_zapobj,
229 				    ddobj, 0, dd->dd_myname);
230 			}
231 			if (err != 0)
232 				goto errout;
233 		} else {
234 			(void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
235 		}
236 
237 		if (dsl_dir_is_clone(dd)) {
238 			dmu_buf_t *origin_bonus;
239 			dsl_dataset_phys_t *origin_phys;
240 
241 			/*
242 			 * We can't open the origin dataset, because
243 			 * that would require opening this dsl_dir.
244 			 * Just look at its phys directly instead.
245 			 */
246 			err = dmu_bonus_hold(dp->dp_meta_objset,
247 			    dsl_dir_phys(dd)->dd_origin_obj, FTAG,
248 			    &origin_bonus);
249 			if (err != 0)
250 				goto errout;
251 			origin_phys = origin_bonus->db_data;
252 			dd->dd_origin_txg =
253 			    origin_phys->ds_creation_txg;
254 			dmu_buf_rele(origin_bonus, FTAG);
255 		}
256 
257 		dmu_buf_init_user(&dd->dd_dbu, NULL, dsl_dir_evict_async,
258 		    &dd->dd_dbuf);
259 		winner = dmu_buf_set_user_ie(dbuf, &dd->dd_dbu);
260 		if (winner != NULL) {
261 			if (dd->dd_parent)
262 				dsl_dir_rele(dd->dd_parent, dd);
263 			dsl_prop_fini(dd);
264 			mutex_destroy(&dd->dd_lock);
265 			kmem_free(dd, sizeof (dsl_dir_t));
266 			dd = winner;
267 		} else {
268 			spa_open_ref(dp->dp_spa, dd);
269 		}
270 	}
271 
272 	/*
273 	 * The dsl_dir_t has both open-to-close and instantiate-to-evict
274 	 * holds on the spa.  We need the open-to-close holds because
275 	 * otherwise the spa_refcnt wouldn't change when we open a
276 	 * dir which the spa also has open, so we could incorrectly
277 	 * think it was OK to unload/export/destroy the pool.  We need
278 	 * the instantiate-to-evict hold because the dsl_dir_t has a
279 	 * pointer to the dd_pool, which has a pointer to the spa_t.
280 	 */
281 	spa_open_ref(dp->dp_spa, tag);
282 	ASSERT3P(dd->dd_pool, ==, dp);
283 	ASSERT3U(dd->dd_object, ==, ddobj);
284 	ASSERT3P(dd->dd_dbuf, ==, dbuf);
285 	*ddp = dd;
286 	return (0);
287 
288 errout:
289 	if (dd->dd_parent)
290 		dsl_dir_rele(dd->dd_parent, dd);
291 	dsl_prop_fini(dd);
292 	mutex_destroy(&dd->dd_lock);
293 	kmem_free(dd, sizeof (dsl_dir_t));
294 	dmu_buf_rele(dbuf, tag);
295 	return (err);
296 }
297 
298 void
299 dsl_dir_rele(dsl_dir_t *dd, void *tag)
300 {
301 	dprintf_dd(dd, "%s\n", "");
302 	spa_close(dd->dd_pool->dp_spa, tag);
303 	dmu_buf_rele(dd->dd_dbuf, tag);
304 }
305 
306 /*
307  * Remove a reference to the given dsl dir that is being asynchronously
308  * released.  Async releases occur from a taskq performing eviction of
309  * dsl datasets and dirs.  This process is identical to a normal release
310  * with the exception of using the async API for releasing the reference on
311  * the spa.
312  */
313 void
314 dsl_dir_async_rele(dsl_dir_t *dd, void *tag)
315 {
316 	dprintf_dd(dd, "%s\n", "");
317 	spa_async_close(dd->dd_pool->dp_spa, tag);
318 	dmu_buf_rele(dd->dd_dbuf, tag);
319 }
320 
321 /* buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes */
322 void
323 dsl_dir_name(dsl_dir_t *dd, char *buf)
324 {
325 	if (dd->dd_parent) {
326 		dsl_dir_name(dd->dd_parent, buf);
327 		VERIFY3U(strlcat(buf, "/", ZFS_MAX_DATASET_NAME_LEN), <,
328 		    ZFS_MAX_DATASET_NAME_LEN);
329 	} else {
330 		buf[0] = '\0';
331 	}
332 	if (!MUTEX_HELD(&dd->dd_lock)) {
333 		/*
334 		 * recursive mutex so that we can use
335 		 * dprintf_dd() with dd_lock held
336 		 */
337 		mutex_enter(&dd->dd_lock);
338 		VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
339 		    <, ZFS_MAX_DATASET_NAME_LEN);
340 		mutex_exit(&dd->dd_lock);
341 	} else {
342 		VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
343 		    <, ZFS_MAX_DATASET_NAME_LEN);
344 	}
345 }
346 
347 /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
348 int
349 dsl_dir_namelen(dsl_dir_t *dd)
350 {
351 	int result = 0;
352 
353 	if (dd->dd_parent) {
354 		/* parent's name + 1 for the "/" */
355 		result = dsl_dir_namelen(dd->dd_parent) + 1;
356 	}
357 
358 	if (!MUTEX_HELD(&dd->dd_lock)) {
359 		/* see dsl_dir_name */
360 		mutex_enter(&dd->dd_lock);
361 		result += strlen(dd->dd_myname);
362 		mutex_exit(&dd->dd_lock);
363 	} else {
364 		result += strlen(dd->dd_myname);
365 	}
366 
367 	return (result);
368 }
369 
370 static int
371 getcomponent(const char *path, char *component, const char **nextp)
372 {
373 	char *p;
374 
375 	if ((path == NULL) || (path[0] == '\0'))
376 		return (SET_ERROR(ENOENT));
377 	/* This would be a good place to reserve some namespace... */
378 	p = strpbrk(path, "/@");
379 	if (p && (p[1] == '/' || p[1] == '@')) {
380 		/* two separators in a row */
381 		return (SET_ERROR(EINVAL));
382 	}
383 	if (p == NULL || p == path) {
384 		/*
385 		 * if the first thing is an @ or /, it had better be an
386 		 * @ and it had better not have any more ats or slashes,
387 		 * and it had better have something after the @.
388 		 */
389 		if (p != NULL &&
390 		    (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
391 			return (SET_ERROR(EINVAL));
392 		if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN)
393 			return (SET_ERROR(ENAMETOOLONG));
394 		(void) strcpy(component, path);
395 		p = NULL;
396 	} else if (p[0] == '/') {
397 		if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
398 			return (SET_ERROR(ENAMETOOLONG));
399 		(void) strncpy(component, path, p - path);
400 		component[p - path] = '\0';
401 		p++;
402 	} else if (p[0] == '@') {
403 		/*
404 		 * if the next separator is an @, there better not be
405 		 * any more slashes.
406 		 */
407 		if (strchr(path, '/'))
408 			return (SET_ERROR(EINVAL));
409 		if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
410 			return (SET_ERROR(ENAMETOOLONG));
411 		(void) strncpy(component, path, p - path);
412 		component[p - path] = '\0';
413 	} else {
414 		panic("invalid p=%p", (void *)p);
415 	}
416 	*nextp = p;
417 	return (0);
418 }
419 
420 /*
421  * Return the dsl_dir_t, and possibly the last component which couldn't
422  * be found in *tail.  The name must be in the specified dsl_pool_t.  This
423  * thread must hold the dp_config_rwlock for the pool.  Returns NULL if the
424  * path is bogus, or if tail==NULL and we couldn't parse the whole name.
425  * (*tail)[0] == '@' means that the last component is a snapshot.
426  */
427 int
428 dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag,
429     dsl_dir_t **ddp, const char **tailp)
430 {
431 	char buf[ZFS_MAX_DATASET_NAME_LEN];
432 	const char *spaname, *next, *nextnext = NULL;
433 	int err;
434 	dsl_dir_t *dd;
435 	uint64_t ddobj;
436 
437 	err = getcomponent(name, buf, &next);
438 	if (err != 0)
439 		return (err);
440 
441 	/* Make sure the name is in the specified pool. */
442 	spaname = spa_name(dp->dp_spa);
443 	if (strcmp(buf, spaname) != 0)
444 		return (SET_ERROR(EXDEV));
445 
446 	ASSERT(dsl_pool_config_held(dp));
447 
448 	err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
449 	if (err != 0) {
450 		return (err);
451 	}
452 
453 	while (next != NULL) {
454 		dsl_dir_t *child_dd;
455 		err = getcomponent(next, buf, &nextnext);
456 		if (err != 0)
457 			break;
458 		ASSERT(next[0] != '\0');
459 		if (next[0] == '@')
460 			break;
461 		dprintf("looking up %s in obj%lld\n",
462 		    buf, dsl_dir_phys(dd)->dd_child_dir_zapobj);
463 
464 		err = zap_lookup(dp->dp_meta_objset,
465 		    dsl_dir_phys(dd)->dd_child_dir_zapobj,
466 		    buf, sizeof (ddobj), 1, &ddobj);
467 		if (err != 0) {
468 			if (err == ENOENT)
469 				err = 0;
470 			break;
471 		}
472 
473 		err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_dd);
474 		if (err != 0)
475 			break;
476 		dsl_dir_rele(dd, tag);
477 		dd = child_dd;
478 		next = nextnext;
479 	}
480 
481 	if (err != 0) {
482 		dsl_dir_rele(dd, tag);
483 		return (err);
484 	}
485 
486 	/*
487 	 * It's an error if there's more than one component left, or
488 	 * tailp==NULL and there's any component left.
489 	 */
490 	if (next != NULL &&
491 	    (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
492 		/* bad path name */
493 		dsl_dir_rele(dd, tag);
494 		dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
495 		err = SET_ERROR(ENOENT);
496 	}
497 	if (tailp != NULL)
498 		*tailp = next;
499 	*ddp = dd;
500 	return (err);
501 }
502 
503 /*
504  * If the counts are already initialized for this filesystem and its
505  * descendants then do nothing, otherwise initialize the counts.
506  *
507  * The counts on this filesystem, and those below, may be uninitialized due to
508  * either the use of a pre-existing pool which did not support the
509  * filesystem/snapshot limit feature, or one in which the feature had not yet
510  * been enabled.
511  *
512  * Recursively descend the filesystem tree and update the filesystem/snapshot
513  * counts on each filesystem below, then update the cumulative count on the
514  * current filesystem. If the filesystem already has a count set on it,
515  * then we know that its counts, and the counts on the filesystems below it,
516  * are already correct, so we don't have to update this filesystem.
517  */
518 static void
519 dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx)
520 {
521 	uint64_t my_fs_cnt = 0;
522 	uint64_t my_ss_cnt = 0;
523 	dsl_pool_t *dp = dd->dd_pool;
524 	objset_t *os = dp->dp_meta_objset;
525 	zap_cursor_t *zc;
526 	zap_attribute_t *za;
527 	dsl_dataset_t *ds;
528 
529 	ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT));
530 	ASSERT(dsl_pool_config_held(dp));
531 	ASSERT(dmu_tx_is_syncing(tx));
532 
533 	dsl_dir_zapify(dd, tx);
534 
535 	/*
536 	 * If the filesystem count has already been initialized then we
537 	 * don't need to recurse down any further.
538 	 */
539 	if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0)
540 		return;
541 
542 	zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
543 	za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
544 
545 	/* Iterate my child dirs */
546 	for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj);
547 	    zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) {
548 		dsl_dir_t *chld_dd;
549 		uint64_t count;
550 
551 		VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG,
552 		    &chld_dd));
553 
554 		/*
555 		 * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets and
556 		 * temporary datasets.
557 		 */
558 		if (chld_dd->dd_myname[0] == '$' ||
559 		    chld_dd->dd_myname[0] == '%') {
560 			dsl_dir_rele(chld_dd, FTAG);
561 			continue;
562 		}
563 
564 		my_fs_cnt++;	/* count this child */
565 
566 		dsl_dir_init_fs_ss_count(chld_dd, tx);
567 
568 		VERIFY0(zap_lookup(os, chld_dd->dd_object,
569 		    DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count));
570 		my_fs_cnt += count;
571 		VERIFY0(zap_lookup(os, chld_dd->dd_object,
572 		    DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count));
573 		my_ss_cnt += count;
574 
575 		dsl_dir_rele(chld_dd, FTAG);
576 	}
577 	zap_cursor_fini(zc);
578 	/* Count my snapshots (we counted children's snapshots above) */
579 	VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
580 	    dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds));
581 
582 	for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj);
583 	    zap_cursor_retrieve(zc, za) == 0;
584 	    zap_cursor_advance(zc)) {
585 		/* Don't count temporary snapshots */
586 		if (za->za_name[0] != '%')
587 			my_ss_cnt++;
588 	}
589 	zap_cursor_fini(zc);
590 
591 	dsl_dataset_rele(ds, FTAG);
592 
593 	kmem_free(zc, sizeof (zap_cursor_t));
594 	kmem_free(za, sizeof (zap_attribute_t));
595 
596 	/* we're in a sync task, update counts */
597 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
598 	VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
599 	    sizeof (my_fs_cnt), 1, &my_fs_cnt, tx));
600 	VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
601 	    sizeof (my_ss_cnt), 1, &my_ss_cnt, tx));
602 }
603 
604 static int
605 dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx)
606 {
607 	char *ddname = (char *)arg;
608 	dsl_pool_t *dp = dmu_tx_pool(tx);
609 	dsl_dataset_t *ds;
610 	dsl_dir_t *dd;
611 	int error;
612 
613 	error = dsl_dataset_hold(dp, ddname, FTAG, &ds);
614 	if (error != 0)
615 		return (error);
616 
617 	if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
618 		dsl_dataset_rele(ds, FTAG);
619 		return (SET_ERROR(ENOTSUP));
620 	}
621 
622 	dd = ds->ds_dir;
623 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) &&
624 	    dsl_dir_is_zapified(dd) &&
625 	    zap_contains(dp->dp_meta_objset, dd->dd_object,
626 	    DD_FIELD_FILESYSTEM_COUNT) == 0) {
627 		dsl_dataset_rele(ds, FTAG);
628 		return (SET_ERROR(EALREADY));
629 	}
630 
631 	dsl_dataset_rele(ds, FTAG);
632 	return (0);
633 }
634 
635 static void
636 dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx)
637 {
638 	char *ddname = (char *)arg;
639 	dsl_pool_t *dp = dmu_tx_pool(tx);
640 	dsl_dataset_t *ds;
641 	spa_t *spa;
642 
643 	VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds));
644 
645 	spa = dsl_dataset_get_spa(ds);
646 
647 	if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) {
648 		/*
649 		 * Since the feature was not active and we're now setting a
650 		 * limit, increment the feature-active counter so that the
651 		 * feature becomes active for the first time.
652 		 *
653 		 * We are already in a sync task so we can update the MOS.
654 		 */
655 		spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx);
656 	}
657 
658 	/*
659 	 * Since we are now setting a non-UINT64_MAX limit on the filesystem,
660 	 * we need to ensure the counts are correct. Descend down the tree from
661 	 * this point and update all of the counts to be accurate.
662 	 */
663 	dsl_dir_init_fs_ss_count(ds->ds_dir, tx);
664 
665 	dsl_dataset_rele(ds, FTAG);
666 }
667 
668 /*
669  * Make sure the feature is enabled and activate it if necessary.
670  * Since we're setting a limit, ensure the on-disk counts are valid.
671  * This is only called by the ioctl path when setting a limit value.
672  *
673  * We do not need to validate the new limit, since users who can change the
674  * limit are also allowed to exceed the limit.
675  */
676 int
677 dsl_dir_activate_fs_ss_limit(const char *ddname)
678 {
679 	int error;
680 
681 	error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check,
682 	    dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0,
683 	    ZFS_SPACE_CHECK_RESERVED);
684 
685 	if (error == EALREADY)
686 		error = 0;
687 
688 	return (error);
689 }
690 
691 /*
692  * Used to determine if the filesystem_limit or snapshot_limit should be
693  * enforced. We allow the limit to be exceeded if the user has permission to
694  * write the property value. We pass in the creds that we got in the open
695  * context since we will always be the GZ root in syncing context. We also have
696  * to handle the case where we are allowed to change the limit on the current
697  * dataset, but there may be another limit in the tree above.
698  *
699  * We can never modify these two properties within a non-global zone. In
700  * addition, the other checks are modeled on zfs_secpolicy_write_perms. We
701  * can't use that function since we are already holding the dp_config_rwlock.
702  * In addition, we already have the dd and dealing with snapshots is simplified
703  * in this code.
704  */
705 
706 typedef enum {
707 	ENFORCE_ALWAYS,
708 	ENFORCE_NEVER,
709 	ENFORCE_ABOVE
710 } enforce_res_t;
711 
712 static enforce_res_t
713 dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop, cred_t *cr)
714 {
715 	enforce_res_t enforce = ENFORCE_ALWAYS;
716 	uint64_t obj;
717 	dsl_dataset_t *ds;
718 	uint64_t zoned;
719 
720 	ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
721 	    prop == ZFS_PROP_SNAPSHOT_LIMIT);
722 
723 #ifdef _KERNEL
724 	if (crgetzoneid(cr) != GLOBAL_ZONEID)
725 		return (ENFORCE_ALWAYS);
726 
727 	if (secpolicy_zfs(cr) == 0)
728 		return (ENFORCE_NEVER);
729 #endif
730 
731 	if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0)
732 		return (ENFORCE_ALWAYS);
733 
734 	ASSERT(dsl_pool_config_held(dd->dd_pool));
735 
736 	if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0)
737 		return (ENFORCE_ALWAYS);
738 
739 	if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL) || zoned) {
740 		/* Only root can access zoned fs's from the GZ */
741 		enforce = ENFORCE_ALWAYS;
742 	} else {
743 		if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0)
744 			enforce = ENFORCE_ABOVE;
745 	}
746 
747 	dsl_dataset_rele(ds, FTAG);
748 	return (enforce);
749 }
750 
751 static void
752 dsl_dir_update_last_remap_txg_sync(void *varg, dmu_tx_t *tx)
753 {
754 	ddulrt_arg_t *arg = varg;
755 	uint64_t last_remap_txg;
756 	dsl_dir_t *dd = arg->ddulrta_dd;
757 	objset_t *mos = dd->dd_pool->dp_meta_objset;
758 
759 	dsl_dir_zapify(dd, tx);
760 	if (zap_lookup(mos, dd->dd_object, DD_FIELD_LAST_REMAP_TXG,
761 	    sizeof (last_remap_txg), 1, &last_remap_txg) != 0 ||
762 	    last_remap_txg < arg->ddlrta_txg) {
763 		VERIFY0(zap_update(mos, dd->dd_object, DD_FIELD_LAST_REMAP_TXG,
764 		    sizeof (arg->ddlrta_txg), 1, &arg->ddlrta_txg, tx));
765 	}
766 }
767 
768 int
769 dsl_dir_update_last_remap_txg(dsl_dir_t *dd, uint64_t txg)
770 {
771 	ddulrt_arg_t arg;
772 	arg.ddulrta_dd = dd;
773 	arg.ddlrta_txg = txg;
774 
775 	return (dsl_sync_task(spa_name(dd->dd_pool->dp_spa),
776 	    NULL, dsl_dir_update_last_remap_txg_sync, &arg,
777 	    1, ZFS_SPACE_CHECK_RESERVED));
778 }
779 
780 /*
781  * Check if adding additional child filesystem(s) would exceed any filesystem
782  * limits or adding additional snapshot(s) would exceed any snapshot limits.
783  * The prop argument indicates which limit to check.
784  *
785  * Note that all filesystem limits up to the root (or the highest
786  * initialized) filesystem or the given ancestor must be satisfied.
787  */
788 int
789 dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop,
790     dsl_dir_t *ancestor, cred_t *cr)
791 {
792 	objset_t *os = dd->dd_pool->dp_meta_objset;
793 	uint64_t limit, count;
794 	char *count_prop;
795 	enforce_res_t enforce;
796 	int err = 0;
797 
798 	ASSERT(dsl_pool_config_held(dd->dd_pool));
799 	ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
800 	    prop == ZFS_PROP_SNAPSHOT_LIMIT);
801 
802 	/*
803 	 * If we're allowed to change the limit, don't enforce the limit
804 	 * e.g. this can happen if a snapshot is taken by an administrative
805 	 * user in the global zone (i.e. a recursive snapshot by root).
806 	 * However, we must handle the case of delegated permissions where we
807 	 * are allowed to change the limit on the current dataset, but there
808 	 * is another limit in the tree above.
809 	 */
810 	enforce = dsl_enforce_ds_ss_limits(dd, prop, cr);
811 	if (enforce == ENFORCE_NEVER)
812 		return (0);
813 
814 	/*
815 	 * e.g. if renaming a dataset with no snapshots, count adjustment
816 	 * is 0.
817 	 */
818 	if (delta == 0)
819 		return (0);
820 
821 	if (prop == ZFS_PROP_SNAPSHOT_LIMIT) {
822 		/*
823 		 * We don't enforce the limit for temporary snapshots. This is
824 		 * indicated by a NULL cred_t argument.
825 		 */
826 		if (cr == NULL)
827 			return (0);
828 
829 		count_prop = DD_FIELD_SNAPSHOT_COUNT;
830 	} else {
831 		count_prop = DD_FIELD_FILESYSTEM_COUNT;
832 	}
833 
834 	/*
835 	 * If an ancestor has been provided, stop checking the limit once we
836 	 * hit that dir. We need this during rename so that we don't overcount
837 	 * the check once we recurse up to the common ancestor.
838 	 */
839 	if (ancestor == dd)
840 		return (0);
841 
842 	/*
843 	 * If we hit an uninitialized node while recursing up the tree, we can
844 	 * stop since we know there is no limit here (or above). The counts are
845 	 * not valid on this node and we know we won't touch this node's counts.
846 	 */
847 	if (!dsl_dir_is_zapified(dd) || zap_lookup(os, dd->dd_object,
848 	    count_prop, sizeof (count), 1, &count) == ENOENT)
849 		return (0);
850 
851 	err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL,
852 	    B_FALSE);
853 	if (err != 0)
854 		return (err);
855 
856 	/* Is there a limit which we've hit? */
857 	if (enforce == ENFORCE_ALWAYS && (count + delta) > limit)
858 		return (SET_ERROR(EDQUOT));
859 
860 	if (dd->dd_parent != NULL)
861 		err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop,
862 		    ancestor, cr);
863 
864 	return (err);
865 }
866 
867 /*
868  * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all
869  * parents. When a new filesystem/snapshot is created, increment the count on
870  * all parents, and when a filesystem/snapshot is destroyed, decrement the
871  * count.
872  */
873 void
874 dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop,
875     dmu_tx_t *tx)
876 {
877 	int err;
878 	objset_t *os = dd->dd_pool->dp_meta_objset;
879 	uint64_t count;
880 
881 	ASSERT(dsl_pool_config_held(dd->dd_pool));
882 	ASSERT(dmu_tx_is_syncing(tx));
883 	ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 ||
884 	    strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0);
885 
886 	/*
887 	 * When we receive an incremental stream into a filesystem that already
888 	 * exists, a temporary clone is created.  We don't count this temporary
889 	 * clone, whose name begins with a '%'. We also ignore hidden ($FREE,
890 	 * $MOS & $ORIGIN) objsets.
891 	 */
892 	if ((dd->dd_myname[0] == '%' || dd->dd_myname[0] == '$') &&
893 	    strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0)
894 		return;
895 
896 	/*
897 	 * e.g. if renaming a dataset with no snapshots, count adjustment is 0
898 	 */
899 	if (delta == 0)
900 		return;
901 
902 	/*
903 	 * If we hit an uninitialized node while recursing up the tree, we can
904 	 * stop since we know the counts are not valid on this node and we
905 	 * know we shouldn't touch this node's counts. An uninitialized count
906 	 * on the node indicates that either the feature has not yet been
907 	 * activated or there are no limits on this part of the tree.
908 	 */
909 	if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object,
910 	    prop, sizeof (count), 1, &count)) == ENOENT)
911 		return;
912 	VERIFY0(err);
913 
914 	count += delta;
915 	/* Use a signed verify to make sure we're not neg. */
916 	VERIFY3S(count, >=, 0);
917 
918 	VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count,
919 	    tx));
920 
921 	/* Roll up this additional count into our ancestors */
922 	if (dd->dd_parent != NULL)
923 		dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx);
924 }
925 
926 uint64_t
927 dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
928     dmu_tx_t *tx)
929 {
930 	objset_t *mos = dp->dp_meta_objset;
931 	uint64_t ddobj;
932 	dsl_dir_phys_t *ddphys;
933 	dmu_buf_t *dbuf;
934 
935 	ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
936 	    DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
937 	if (pds) {
938 		VERIFY0(zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj,
939 		    name, sizeof (uint64_t), 1, &ddobj, tx));
940 	} else {
941 		/* it's the root dir */
942 		VERIFY0(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
943 		    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
944 	}
945 	VERIFY0(dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
946 	dmu_buf_will_dirty(dbuf, tx);
947 	ddphys = dbuf->db_data;
948 
949 	ddphys->dd_creation_time = gethrestime_sec();
950 	if (pds) {
951 		ddphys->dd_parent_obj = pds->dd_object;
952 
953 		/* update the filesystem counts */
954 		dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx);
955 	}
956 	ddphys->dd_props_zapobj = zap_create(mos,
957 	    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
958 	ddphys->dd_child_dir_zapobj = zap_create(mos,
959 	    DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
960 	if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
961 		ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
962 
963 	dmu_buf_rele(dbuf, FTAG);
964 
965 	return (ddobj);
966 }
967 
968 boolean_t
969 dsl_dir_is_clone(dsl_dir_t *dd)
970 {
971 	return (dsl_dir_phys(dd)->dd_origin_obj &&
972 	    (dd->dd_pool->dp_origin_snap == NULL ||
973 	    dsl_dir_phys(dd)->dd_origin_obj !=
974 	    dd->dd_pool->dp_origin_snap->ds_object));
975 }
976 
977 
978 uint64_t
979 dsl_dir_get_used(dsl_dir_t *dd)
980 {
981 	return (dsl_dir_phys(dd)->dd_used_bytes);
982 }
983 
984 uint64_t
985 dsl_dir_get_compressed(dsl_dir_t *dd)
986 {
987 	return (dsl_dir_phys(dd)->dd_compressed_bytes);
988 }
989 
990 uint64_t
991 dsl_dir_get_quota(dsl_dir_t *dd)
992 {
993 	return (dsl_dir_phys(dd)->dd_quota);
994 }
995 
996 uint64_t
997 dsl_dir_get_reservation(dsl_dir_t *dd)
998 {
999 	return (dsl_dir_phys(dd)->dd_reserved);
1000 }
1001 
1002 uint64_t
1003 dsl_dir_get_compressratio(dsl_dir_t *dd)
1004 {
1005 	/* a fixed point number, 100x the ratio */
1006 	return (dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 :
1007 	    (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 /
1008 	    dsl_dir_phys(dd)->dd_compressed_bytes));
1009 }
1010 
1011 uint64_t
1012 dsl_dir_get_logicalused(dsl_dir_t *dd)
1013 {
1014 	return (dsl_dir_phys(dd)->dd_uncompressed_bytes);
1015 }
1016 
1017 uint64_t
1018 dsl_dir_get_usedsnap(dsl_dir_t *dd)
1019 {
1020 	return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]);
1021 }
1022 
1023 uint64_t
1024 dsl_dir_get_usedds(dsl_dir_t *dd)
1025 {
1026 	return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]);
1027 }
1028 
1029 uint64_t
1030 dsl_dir_get_usedrefreserv(dsl_dir_t *dd)
1031 {
1032 	return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]);
1033 }
1034 
1035 uint64_t
1036 dsl_dir_get_usedchild(dsl_dir_t *dd)
1037 {
1038 	return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] +
1039 	    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]);
1040 }
1041 
1042 void
1043 dsl_dir_get_origin(dsl_dir_t *dd, char *buf)
1044 {
1045 	dsl_dataset_t *ds;
1046 	VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
1047 	    dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds));
1048 
1049 	dsl_dataset_name(ds, buf);
1050 
1051 	dsl_dataset_rele(ds, FTAG);
1052 }
1053 
1054 int
1055 dsl_dir_get_filesystem_count(dsl_dir_t *dd, uint64_t *count)
1056 {
1057 	if (dsl_dir_is_zapified(dd)) {
1058 		objset_t *os = dd->dd_pool->dp_meta_objset;
1059 		return (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
1060 		    sizeof (*count), 1, count));
1061 	} else {
1062 		return (ENOENT);
1063 	}
1064 }
1065 
1066 int
1067 dsl_dir_get_snapshot_count(dsl_dir_t *dd, uint64_t *count)
1068 {
1069 	if (dsl_dir_is_zapified(dd)) {
1070 		objset_t *os = dd->dd_pool->dp_meta_objset;
1071 		return (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
1072 		    sizeof (*count), 1, count));
1073 	} else {
1074 		return (ENOENT);
1075 	}
1076 }
1077 
1078 int
1079 dsl_dir_get_remaptxg(dsl_dir_t *dd, uint64_t *count)
1080 {
1081 	if (dsl_dir_is_zapified(dd)) {
1082 		objset_t *os = dd->dd_pool->dp_meta_objset;
1083 		return (zap_lookup(os, dd->dd_object, DD_FIELD_LAST_REMAP_TXG,
1084 		    sizeof (*count), 1, count));
1085 	} else {
1086 		return (ENOENT);
1087 	}
1088 }
1089 
1090 void
1091 dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
1092 {
1093 	mutex_enter(&dd->dd_lock);
1094 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
1095 	    dsl_dir_get_quota(dd));
1096 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
1097 	    dsl_dir_get_reservation(dd));
1098 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
1099 	    dsl_dir_get_logicalused(dd));
1100 	if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1101 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
1102 		    dsl_dir_get_usedsnap(dd));
1103 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
1104 		    dsl_dir_get_usedds(dd));
1105 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
1106 		    dsl_dir_get_usedrefreserv(dd));
1107 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
1108 		    dsl_dir_get_usedchild(dd));
1109 	}
1110 	mutex_exit(&dd->dd_lock);
1111 
1112 	uint64_t count;
1113 	if (dsl_dir_get_filesystem_count(dd, &count) == 0) {
1114 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_FILESYSTEM_COUNT,
1115 		    count);
1116 	}
1117 	if (dsl_dir_get_snapshot_count(dd, &count) == 0) {
1118 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_SNAPSHOT_COUNT,
1119 		    count);
1120 	}
1121 	if (dsl_dir_get_remaptxg(dd, &count) == 0) {
1122 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REMAPTXG,
1123 		    count);
1124 	}
1125 
1126 	if (dsl_dir_is_clone(dd)) {
1127 		char buf[ZFS_MAX_DATASET_NAME_LEN];
1128 		dsl_dir_get_origin(dd, buf);
1129 		dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
1130 	}
1131 
1132 }
1133 
1134 void
1135 dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
1136 {
1137 	dsl_pool_t *dp = dd->dd_pool;
1138 
1139 	ASSERT(dsl_dir_phys(dd));
1140 
1141 	if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {
1142 		/* up the hold count until we can be written out */
1143 		dmu_buf_add_ref(dd->dd_dbuf, dd);
1144 	}
1145 }
1146 
1147 static int64_t
1148 parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
1149 {
1150 	uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved);
1151 	uint64_t new_accounted =
1152 	    MAX(used + delta, dsl_dir_phys(dd)->dd_reserved);
1153 	return (new_accounted - old_accounted);
1154 }
1155 
1156 void
1157 dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
1158 {
1159 	ASSERT(dmu_tx_is_syncing(tx));
1160 
1161 	mutex_enter(&dd->dd_lock);
1162 	ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]);
1163 	dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
1164 	    dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
1165 	dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
1166 	mutex_exit(&dd->dd_lock);
1167 
1168 	/* release the hold from dsl_dir_dirty */
1169 	dmu_buf_rele(dd->dd_dbuf, dd);
1170 }
1171 
1172 static uint64_t
1173 dsl_dir_space_towrite(dsl_dir_t *dd)
1174 {
1175 	uint64_t space = 0;
1176 
1177 	ASSERT(MUTEX_HELD(&dd->dd_lock));
1178 
1179 	for (int i = 0; i < TXG_SIZE; i++) {
1180 		space += dd->dd_space_towrite[i & TXG_MASK];
1181 		ASSERT3U(dd->dd_space_towrite[i & TXG_MASK], >=, 0);
1182 	}
1183 	return (space);
1184 }
1185 
1186 /*
1187  * How much space would dd have available if ancestor had delta applied
1188  * to it?  If ondiskonly is set, we're only interested in what's
1189  * on-disk, not estimated pending changes.
1190  */
1191 uint64_t
1192 dsl_dir_space_available(dsl_dir_t *dd,
1193     dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
1194 {
1195 	uint64_t parentspace, myspace, quota, used;
1196 
1197 	/*
1198 	 * If there are no restrictions otherwise, assume we have
1199 	 * unlimited space available.
1200 	 */
1201 	quota = UINT64_MAX;
1202 	parentspace = UINT64_MAX;
1203 
1204 	if (dd->dd_parent != NULL) {
1205 		parentspace = dsl_dir_space_available(dd->dd_parent,
1206 		    ancestor, delta, ondiskonly);
1207 	}
1208 
1209 	mutex_enter(&dd->dd_lock);
1210 	if (dsl_dir_phys(dd)->dd_quota != 0)
1211 		quota = dsl_dir_phys(dd)->dd_quota;
1212 	used = dsl_dir_phys(dd)->dd_used_bytes;
1213 	if (!ondiskonly)
1214 		used += dsl_dir_space_towrite(dd);
1215 
1216 	if (dd->dd_parent == NULL) {
1217 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool,
1218 		    ZFS_SPACE_CHECK_NORMAL);
1219 		quota = MIN(quota, poolsize);
1220 	}
1221 
1222 	if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) {
1223 		/*
1224 		 * We have some space reserved, in addition to what our
1225 		 * parent gave us.
1226 		 */
1227 		parentspace += dsl_dir_phys(dd)->dd_reserved - used;
1228 	}
1229 
1230 	if (dd == ancestor) {
1231 		ASSERT(delta <= 0);
1232 		ASSERT(used >= -delta);
1233 		used += delta;
1234 		if (parentspace != UINT64_MAX)
1235 			parentspace -= delta;
1236 	}
1237 
1238 	if (used > quota) {
1239 		/* over quota */
1240 		myspace = 0;
1241 	} else {
1242 		/*
1243 		 * the lesser of the space provided by our parent and
1244 		 * the space left in our quota
1245 		 */
1246 		myspace = MIN(parentspace, quota - used);
1247 	}
1248 
1249 	mutex_exit(&dd->dd_lock);
1250 
1251 	return (myspace);
1252 }
1253 
1254 struct tempreserve {
1255 	list_node_t tr_node;
1256 	dsl_dir_t *tr_ds;
1257 	uint64_t tr_size;
1258 };
1259 
1260 static int
1261 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
1262     boolean_t ignorequota, list_t *tr_list,
1263     dmu_tx_t *tx, boolean_t first)
1264 {
1265 	uint64_t txg = tx->tx_txg;
1266 	uint64_t quota;
1267 	struct tempreserve *tr;
1268 	int retval = EDQUOT;
1269 	uint64_t ref_rsrv = 0;
1270 
1271 	ASSERT3U(txg, !=, 0);
1272 	ASSERT3S(asize, >, 0);
1273 
1274 	mutex_enter(&dd->dd_lock);
1275 
1276 	/*
1277 	 * Check against the dsl_dir's quota.  We don't add in the delta
1278 	 * when checking for over-quota because they get one free hit.
1279 	 */
1280 	uint64_t est_inflight = dsl_dir_space_towrite(dd);
1281 	for (int i = 0; i < TXG_SIZE; i++)
1282 		est_inflight += dd->dd_tempreserved[i];
1283 	uint64_t used_on_disk = dsl_dir_phys(dd)->dd_used_bytes;
1284 
1285 	/*
1286 	 * On the first iteration, fetch the dataset's used-on-disk and
1287 	 * refreservation values. Also, if checkrefquota is set, test if
1288 	 * allocating this space would exceed the dataset's refquota.
1289 	 */
1290 	if (first && tx->tx_objset) {
1291 		int error;
1292 		dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
1293 
1294 		error = dsl_dataset_check_quota(ds, !netfree,
1295 		    asize, est_inflight, &used_on_disk, &ref_rsrv);
1296 		if (error != 0) {
1297 			mutex_exit(&dd->dd_lock);
1298 			return (error);
1299 		}
1300 	}
1301 
1302 	/*
1303 	 * If this transaction will result in a net free of space,
1304 	 * we want to let it through.
1305 	 */
1306 	if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0)
1307 		quota = UINT64_MAX;
1308 	else
1309 		quota = dsl_dir_phys(dd)->dd_quota;
1310 
1311 	/*
1312 	 * Adjust the quota against the actual pool size at the root
1313 	 * minus any outstanding deferred frees.
1314 	 * To ensure that it's possible to remove files from a full
1315 	 * pool without inducing transient overcommits, we throttle
1316 	 * netfree transactions against a quota that is slightly larger,
1317 	 * but still within the pool's allocation slop.  In cases where
1318 	 * we're very close to full, this will allow a steady trickle of
1319 	 * removes to get through.
1320 	 */
1321 	uint64_t deferred = 0;
1322 	if (dd->dd_parent == NULL) {
1323 		uint64_t avail = dsl_pool_unreserved_space(dd->dd_pool,
1324 		    (netfree) ?
1325 		    ZFS_SPACE_CHECK_RESERVED : ZFS_SPACE_CHECK_NORMAL);
1326 
1327 		if (avail < quota) {
1328 			quota = avail;
1329 			retval = ENOSPC;
1330 		}
1331 	}
1332 
1333 	/*
1334 	 * If they are requesting more space, and our current estimate
1335 	 * is over quota, they get to try again unless the actual
1336 	 * on-disk is over quota and there are no pending changes (which
1337 	 * may free up space for us).
1338 	 */
1339 	if (used_on_disk + est_inflight >= quota) {
1340 		if (est_inflight > 0 || used_on_disk < quota ||
1341 		    (retval == ENOSPC && used_on_disk < quota + deferred))
1342 			retval = ERESTART;
1343 		dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
1344 		    "quota=%lluK tr=%lluK err=%d\n",
1345 		    used_on_disk>>10, est_inflight>>10,
1346 		    quota>>10, asize>>10, retval);
1347 		mutex_exit(&dd->dd_lock);
1348 		return (SET_ERROR(retval));
1349 	}
1350 
1351 	/* We need to up our estimated delta before dropping dd_lock */
1352 	dd->dd_tempreserved[txg & TXG_MASK] += asize;
1353 
1354 	uint64_t parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
1355 	    asize - ref_rsrv);
1356 	mutex_exit(&dd->dd_lock);
1357 
1358 	tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1359 	tr->tr_ds = dd;
1360 	tr->tr_size = asize;
1361 	list_insert_tail(tr_list, tr);
1362 
1363 	/* see if it's OK with our parent */
1364 	if (dd->dd_parent != NULL && parent_rsrv != 0) {
1365 		boolean_t ismos = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
1366 
1367 		return (dsl_dir_tempreserve_impl(dd->dd_parent,
1368 		    parent_rsrv, netfree, ismos, tr_list, tx, B_FALSE));
1369 	} else {
1370 		return (0);
1371 	}
1372 }
1373 
1374 /*
1375  * Reserve space in this dsl_dir, to be used in this tx's txg.
1376  * After the space has been dirtied (and dsl_dir_willuse_space()
1377  * has been called), the reservation should be canceled, using
1378  * dsl_dir_tempreserve_clear().
1379  */
1380 int
1381 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
1382     boolean_t netfree, void **tr_cookiep, dmu_tx_t *tx)
1383 {
1384 	int err;
1385 	list_t *tr_list;
1386 
1387 	if (asize == 0) {
1388 		*tr_cookiep = NULL;
1389 		return (0);
1390 	}
1391 
1392 	tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
1393 	list_create(tr_list, sizeof (struct tempreserve),
1394 	    offsetof(struct tempreserve, tr_node));
1395 	ASSERT3S(asize, >, 0);
1396 
1397 	err = arc_tempreserve_space(dd->dd_pool->dp_spa, lsize, tx->tx_txg);
1398 	if (err == 0) {
1399 		struct tempreserve *tr;
1400 
1401 		tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1402 		tr->tr_size = lsize;
1403 		list_insert_tail(tr_list, tr);
1404 	} else {
1405 		if (err == EAGAIN) {
1406 			/*
1407 			 * If arc_memory_throttle() detected that pageout
1408 			 * is running and we are low on memory, we delay new
1409 			 * non-pageout transactions to give pageout an
1410 			 * advantage.
1411 			 *
1412 			 * It is unfortunate to be delaying while the caller's
1413 			 * locks are held.
1414 			 */
1415 			txg_delay(dd->dd_pool, tx->tx_txg,
1416 			    MSEC2NSEC(10), MSEC2NSEC(10));
1417 			err = SET_ERROR(ERESTART);
1418 		}
1419 	}
1420 
1421 	if (err == 0) {
1422 		err = dsl_dir_tempreserve_impl(dd, asize, netfree,
1423 		    B_FALSE, tr_list, tx, B_TRUE);
1424 	}
1425 
1426 	if (err != 0)
1427 		dsl_dir_tempreserve_clear(tr_list, tx);
1428 	else
1429 		*tr_cookiep = tr_list;
1430 
1431 	return (err);
1432 }
1433 
1434 /*
1435  * Clear a temporary reservation that we previously made with
1436  * dsl_dir_tempreserve_space().
1437  */
1438 void
1439 dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
1440 {
1441 	int txgidx = tx->tx_txg & TXG_MASK;
1442 	list_t *tr_list = tr_cookie;
1443 	struct tempreserve *tr;
1444 
1445 	ASSERT3U(tx->tx_txg, !=, 0);
1446 
1447 	if (tr_cookie == NULL)
1448 		return;
1449 
1450 	while ((tr = list_head(tr_list)) != NULL) {
1451 		if (tr->tr_ds) {
1452 			mutex_enter(&tr->tr_ds->dd_lock);
1453 			ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
1454 			    tr->tr_size);
1455 			tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
1456 			mutex_exit(&tr->tr_ds->dd_lock);
1457 		} else {
1458 			arc_tempreserve_clear(tr->tr_size);
1459 		}
1460 		list_remove(tr_list, tr);
1461 		kmem_free(tr, sizeof (struct tempreserve));
1462 	}
1463 
1464 	kmem_free(tr_list, sizeof (list_t));
1465 }
1466 
1467 /*
1468  * This should be called from open context when we think we're going to write
1469  * or free space, for example when dirtying data. Be conservative; it's okay
1470  * to write less space or free more, but we don't want to write more or free
1471  * less than the amount specified.
1472  */
1473 void
1474 dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
1475 {
1476 	int64_t parent_space;
1477 	uint64_t est_used;
1478 
1479 	mutex_enter(&dd->dd_lock);
1480 	if (space > 0)
1481 		dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
1482 
1483 	est_used = dsl_dir_space_towrite(dd) + dsl_dir_phys(dd)->dd_used_bytes;
1484 	parent_space = parent_delta(dd, est_used, space);
1485 	mutex_exit(&dd->dd_lock);
1486 
1487 	/* Make sure that we clean up dd_space_to* */
1488 	dsl_dir_dirty(dd, tx);
1489 
1490 	/* XXX this is potentially expensive and unnecessary... */
1491 	if (parent_space && dd->dd_parent)
1492 		dsl_dir_willuse_space(dd->dd_parent, parent_space, tx);
1493 }
1494 
1495 /* call from syncing context when we actually write/free space for this dd */
1496 void
1497 dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
1498     int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
1499 {
1500 	int64_t accounted_delta;
1501 
1502 	/*
1503 	 * dsl_dataset_set_refreservation_sync_impl() calls this with
1504 	 * dd_lock held, so that it can atomically update
1505 	 * ds->ds_reserved and the dsl_dir accounting, so that
1506 	 * dsl_dataset_check_quota() can see dataset and dir accounting
1507 	 * consistently.
1508 	 */
1509 	boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
1510 
1511 	ASSERT(dmu_tx_is_syncing(tx));
1512 	ASSERT(type < DD_USED_NUM);
1513 
1514 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1515 
1516 	if (needlock)
1517 		mutex_enter(&dd->dd_lock);
1518 	accounted_delta =
1519 	    parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, used);
1520 	ASSERT(used >= 0 || dsl_dir_phys(dd)->dd_used_bytes >= -used);
1521 	ASSERT(compressed >= 0 ||
1522 	    dsl_dir_phys(dd)->dd_compressed_bytes >= -compressed);
1523 	ASSERT(uncompressed >= 0 ||
1524 	    dsl_dir_phys(dd)->dd_uncompressed_bytes >= -uncompressed);
1525 	dsl_dir_phys(dd)->dd_used_bytes += used;
1526 	dsl_dir_phys(dd)->dd_uncompressed_bytes += uncompressed;
1527 	dsl_dir_phys(dd)->dd_compressed_bytes += compressed;
1528 
1529 	if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1530 		ASSERT(used > 0 ||
1531 		    dsl_dir_phys(dd)->dd_used_breakdown[type] >= -used);
1532 		dsl_dir_phys(dd)->dd_used_breakdown[type] += used;
1533 #ifdef DEBUG
1534 		dd_used_t t;
1535 		uint64_t u = 0;
1536 		for (t = 0; t < DD_USED_NUM; t++)
1537 			u += dsl_dir_phys(dd)->dd_used_breakdown[t];
1538 		ASSERT3U(u, ==, dsl_dir_phys(dd)->dd_used_bytes);
1539 #endif
1540 	}
1541 	if (needlock)
1542 		mutex_exit(&dd->dd_lock);
1543 
1544 	if (dd->dd_parent != NULL) {
1545 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1546 		    accounted_delta, compressed, uncompressed, tx);
1547 		dsl_dir_transfer_space(dd->dd_parent,
1548 		    used - accounted_delta,
1549 		    DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
1550 	}
1551 }
1552 
1553 void
1554 dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
1555     dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
1556 {
1557 	ASSERT(dmu_tx_is_syncing(tx));
1558 	ASSERT(oldtype < DD_USED_NUM);
1559 	ASSERT(newtype < DD_USED_NUM);
1560 
1561 	if (delta == 0 ||
1562 	    !(dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN))
1563 		return;
1564 
1565 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1566 	mutex_enter(&dd->dd_lock);
1567 	ASSERT(delta > 0 ?
1568 	    dsl_dir_phys(dd)->dd_used_breakdown[oldtype] >= delta :
1569 	    dsl_dir_phys(dd)->dd_used_breakdown[newtype] >= -delta);
1570 	ASSERT(dsl_dir_phys(dd)->dd_used_bytes >= ABS(delta));
1571 	dsl_dir_phys(dd)->dd_used_breakdown[oldtype] -= delta;
1572 	dsl_dir_phys(dd)->dd_used_breakdown[newtype] += delta;
1573 	mutex_exit(&dd->dd_lock);
1574 }
1575 
1576 typedef struct dsl_dir_set_qr_arg {
1577 	const char *ddsqra_name;
1578 	zprop_source_t ddsqra_source;
1579 	uint64_t ddsqra_value;
1580 } dsl_dir_set_qr_arg_t;
1581 
1582 static int
1583 dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)
1584 {
1585 	dsl_dir_set_qr_arg_t *ddsqra = arg;
1586 	dsl_pool_t *dp = dmu_tx_pool(tx);
1587 	dsl_dataset_t *ds;
1588 	int error;
1589 	uint64_t towrite, newval;
1590 
1591 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1592 	if (error != 0)
1593 		return (error);
1594 
1595 	error = dsl_prop_predict(ds->ds_dir, "quota",
1596 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1597 	if (error != 0) {
1598 		dsl_dataset_rele(ds, FTAG);
1599 		return (error);
1600 	}
1601 
1602 	if (newval == 0) {
1603 		dsl_dataset_rele(ds, FTAG);
1604 		return (0);
1605 	}
1606 
1607 	mutex_enter(&ds->ds_dir->dd_lock);
1608 	/*
1609 	 * If we are doing the preliminary check in open context, and
1610 	 * there are pending changes, then don't fail it, since the
1611 	 * pending changes could under-estimate the amount of space to be
1612 	 * freed up.
1613 	 */
1614 	towrite = dsl_dir_space_towrite(ds->ds_dir);
1615 	if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1616 	    (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved ||
1617 	    newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) {
1618 		error = SET_ERROR(ENOSPC);
1619 	}
1620 	mutex_exit(&ds->ds_dir->dd_lock);
1621 	dsl_dataset_rele(ds, FTAG);
1622 	return (error);
1623 }
1624 
1625 static void
1626 dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)
1627 {
1628 	dsl_dir_set_qr_arg_t *ddsqra = arg;
1629 	dsl_pool_t *dp = dmu_tx_pool(tx);
1630 	dsl_dataset_t *ds;
1631 	uint64_t newval;
1632 
1633 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1634 
1635 	if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1636 		dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),
1637 		    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1638 		    &ddsqra->ddsqra_value, tx);
1639 
1640 		VERIFY0(dsl_prop_get_int_ds(ds,
1641 		    zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));
1642 	} else {
1643 		newval = ddsqra->ddsqra_value;
1644 		spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1645 		    zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);
1646 	}
1647 
1648 	dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
1649 	mutex_enter(&ds->ds_dir->dd_lock);
1650 	dsl_dir_phys(ds->ds_dir)->dd_quota = newval;
1651 	mutex_exit(&ds->ds_dir->dd_lock);
1652 	dsl_dataset_rele(ds, FTAG);
1653 }
1654 
1655 int
1656 dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1657 {
1658 	dsl_dir_set_qr_arg_t ddsqra;
1659 
1660 	ddsqra.ddsqra_name = ddname;
1661 	ddsqra.ddsqra_source = source;
1662 	ddsqra.ddsqra_value = quota;
1663 
1664 	return (dsl_sync_task(ddname, dsl_dir_set_quota_check,
1665 	    dsl_dir_set_quota_sync, &ddsqra, 0,
1666 	    ZFS_SPACE_CHECK_EXTRA_RESERVED));
1667 }
1668 
1669 int
1670 dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)
1671 {
1672 	dsl_dir_set_qr_arg_t *ddsqra = arg;
1673 	dsl_pool_t *dp = dmu_tx_pool(tx);
1674 	dsl_dataset_t *ds;
1675 	dsl_dir_t *dd;
1676 	uint64_t newval, used, avail;
1677 	int error;
1678 
1679 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1680 	if (error != 0)
1681 		return (error);
1682 	dd = ds->ds_dir;
1683 
1684 	/*
1685 	 * If we are doing the preliminary check in open context, the
1686 	 * space estimates may be inaccurate.
1687 	 */
1688 	if (!dmu_tx_is_syncing(tx)) {
1689 		dsl_dataset_rele(ds, FTAG);
1690 		return (0);
1691 	}
1692 
1693 	error = dsl_prop_predict(ds->ds_dir,
1694 	    zfs_prop_to_name(ZFS_PROP_RESERVATION),
1695 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1696 	if (error != 0) {
1697 		dsl_dataset_rele(ds, FTAG);
1698 		return (error);
1699 	}
1700 
1701 	mutex_enter(&dd->dd_lock);
1702 	used = dsl_dir_phys(dd)->dd_used_bytes;
1703 	mutex_exit(&dd->dd_lock);
1704 
1705 	if (dd->dd_parent) {
1706 		avail = dsl_dir_space_available(dd->dd_parent,
1707 		    NULL, 0, FALSE);
1708 	} else {
1709 		avail = dsl_pool_adjustedsize(dd->dd_pool,
1710 		    ZFS_SPACE_CHECK_NORMAL) - used;
1711 	}
1712 
1713 	if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) {
1714 		uint64_t delta = MAX(used, newval) -
1715 		    MAX(used, dsl_dir_phys(dd)->dd_reserved);
1716 
1717 		if (delta > avail ||
1718 		    (dsl_dir_phys(dd)->dd_quota > 0 &&
1719 		    newval > dsl_dir_phys(dd)->dd_quota))
1720 			error = SET_ERROR(ENOSPC);
1721 	}
1722 
1723 	dsl_dataset_rele(ds, FTAG);
1724 	return (error);
1725 }
1726 
1727 void
1728 dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
1729 {
1730 	uint64_t used;
1731 	int64_t delta;
1732 
1733 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1734 
1735 	mutex_enter(&dd->dd_lock);
1736 	used = dsl_dir_phys(dd)->dd_used_bytes;
1737 	delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved);
1738 	dsl_dir_phys(dd)->dd_reserved = value;
1739 
1740 	if (dd->dd_parent != NULL) {
1741 		/* Roll up this additional usage into our ancestors */
1742 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1743 		    delta, 0, 0, tx);
1744 	}
1745 	mutex_exit(&dd->dd_lock);
1746 }
1747 
1748 
1749 static void
1750 dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)
1751 {
1752 	dsl_dir_set_qr_arg_t *ddsqra = arg;
1753 	dsl_pool_t *dp = dmu_tx_pool(tx);
1754 	dsl_dataset_t *ds;
1755 	uint64_t newval;
1756 
1757 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1758 
1759 	if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1760 		dsl_prop_set_sync_impl(ds,
1761 		    zfs_prop_to_name(ZFS_PROP_RESERVATION),
1762 		    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1763 		    &ddsqra->ddsqra_value, tx);
1764 
1765 		VERIFY0(dsl_prop_get_int_ds(ds,
1766 		    zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));
1767 	} else {
1768 		newval = ddsqra->ddsqra_value;
1769 		spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1770 		    zfs_prop_to_name(ZFS_PROP_RESERVATION),
1771 		    (longlong_t)newval);
1772 	}
1773 
1774 	dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);
1775 	dsl_dataset_rele(ds, FTAG);
1776 }
1777 
1778 int
1779 dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
1780     uint64_t reservation)
1781 {
1782 	dsl_dir_set_qr_arg_t ddsqra;
1783 
1784 	ddsqra.ddsqra_name = ddname;
1785 	ddsqra.ddsqra_source = source;
1786 	ddsqra.ddsqra_value = reservation;
1787 
1788 	return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,
1789 	    dsl_dir_set_reservation_sync, &ddsqra, 0,
1790 	    ZFS_SPACE_CHECK_EXTRA_RESERVED));
1791 }
1792 
1793 static dsl_dir_t *
1794 closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1795 {
1796 	for (; ds1; ds1 = ds1->dd_parent) {
1797 		dsl_dir_t *dd;
1798 		for (dd = ds2; dd; dd = dd->dd_parent) {
1799 			if (ds1 == dd)
1800 				return (dd);
1801 		}
1802 	}
1803 	return (NULL);
1804 }
1805 
1806 /*
1807  * If delta is applied to dd, how much of that delta would be applied to
1808  * ancestor?  Syncing context only.
1809  */
1810 static int64_t
1811 would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1812 {
1813 	if (dd == ancestor)
1814 		return (delta);
1815 
1816 	mutex_enter(&dd->dd_lock);
1817 	delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta);
1818 	mutex_exit(&dd->dd_lock);
1819 	return (would_change(dd->dd_parent, delta, ancestor));
1820 }
1821 
1822 typedef struct dsl_dir_rename_arg {
1823 	const char *ddra_oldname;
1824 	const char *ddra_newname;
1825 	cred_t *ddra_cred;
1826 } dsl_dir_rename_arg_t;
1827 
1828 typedef struct dsl_valid_rename_arg {
1829 	int char_delta;
1830 	int nest_delta;
1831 } dsl_valid_rename_arg_t;
1832 
1833 /* ARGSUSED */
1834 static int
1835 dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1836 {
1837 	dsl_valid_rename_arg_t *dvra = arg;
1838 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
1839 
1840 	dsl_dataset_name(ds, namebuf);
1841 
1842 	ASSERT3U(strnlen(namebuf, ZFS_MAX_DATASET_NAME_LEN),
1843 	    <, ZFS_MAX_DATASET_NAME_LEN);
1844 	int namelen = strlen(namebuf) + dvra->char_delta;
1845 	int depth = get_dataset_depth(namebuf) + dvra->nest_delta;
1846 
1847 	if (namelen >= ZFS_MAX_DATASET_NAME_LEN)
1848 		return (SET_ERROR(ENAMETOOLONG));
1849 	if (dvra->nest_delta > 0 && depth >= zfs_max_dataset_nesting)
1850 		return (SET_ERROR(ENAMETOOLONG));
1851 	return (0);
1852 }
1853 
1854 static int
1855 dsl_dir_rename_check(void *arg, dmu_tx_t *tx)
1856 {
1857 	dsl_dir_rename_arg_t *ddra = arg;
1858 	dsl_pool_t *dp = dmu_tx_pool(tx);
1859 	dsl_dir_t *dd, *newparent;
1860 	dsl_valid_rename_arg_t dvra;
1861 	const char *mynewname;
1862 	int error;
1863 
1864 	/* target dir should exist */
1865 	error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);
1866 	if (error != 0)
1867 		return (error);
1868 
1869 	/* new parent should exist */
1870 	error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,
1871 	    &newparent, &mynewname);
1872 	if (error != 0) {
1873 		dsl_dir_rele(dd, FTAG);
1874 		return (error);
1875 	}
1876 
1877 	/* can't rename to different pool */
1878 	if (dd->dd_pool != newparent->dd_pool) {
1879 		dsl_dir_rele(newparent, FTAG);
1880 		dsl_dir_rele(dd, FTAG);
1881 		return (SET_ERROR(ENXIO));
1882 	}
1883 
1884 	/* new name should not already exist */
1885 	if (mynewname == NULL) {
1886 		dsl_dir_rele(newparent, FTAG);
1887 		dsl_dir_rele(dd, FTAG);
1888 		return (SET_ERROR(EEXIST));
1889 	}
1890 
1891 	ASSERT3U(strnlen(ddra->ddra_newname, ZFS_MAX_DATASET_NAME_LEN),
1892 	    <, ZFS_MAX_DATASET_NAME_LEN);
1893 	ASSERT3U(strnlen(ddra->ddra_oldname, ZFS_MAX_DATASET_NAME_LEN),
1894 	    <, ZFS_MAX_DATASET_NAME_LEN);
1895 	dvra.char_delta = strlen(ddra->ddra_newname)
1896 	    - strlen(ddra->ddra_oldname);
1897 	dvra.nest_delta = get_dataset_depth(ddra->ddra_newname)
1898 	    - get_dataset_depth(ddra->ddra_oldname);
1899 
1900 	/* if the name length is growing, validate child name lengths */
1901 	if (dvra.char_delta > 0 || dvra.nest_delta > 0) {
1902 		error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,
1903 		    &dvra, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1904 		if (error != 0) {
1905 			dsl_dir_rele(newparent, FTAG);
1906 			dsl_dir_rele(dd, FTAG);
1907 			return (error);
1908 		}
1909 	}
1910 
1911 	if (dmu_tx_is_syncing(tx)) {
1912 		if (spa_feature_is_active(dp->dp_spa,
1913 		    SPA_FEATURE_FS_SS_LIMIT)) {
1914 			/*
1915 			 * Although this is the check function and we don't
1916 			 * normally make on-disk changes in check functions,
1917 			 * we need to do that here.
1918 			 *
1919 			 * Ensure this portion of the tree's counts have been
1920 			 * initialized in case the new parent has limits set.
1921 			 */
1922 			dsl_dir_init_fs_ss_count(dd, tx);
1923 		}
1924 	}
1925 
1926 	if (newparent != dd->dd_parent) {
1927 		/* is there enough space? */
1928 		uint64_t myspace =
1929 		    MAX(dsl_dir_phys(dd)->dd_used_bytes,
1930 		    dsl_dir_phys(dd)->dd_reserved);
1931 		objset_t *os = dd->dd_pool->dp_meta_objset;
1932 		uint64_t fs_cnt = 0;
1933 		uint64_t ss_cnt = 0;
1934 
1935 		if (dsl_dir_is_zapified(dd)) {
1936 			int err;
1937 
1938 			err = zap_lookup(os, dd->dd_object,
1939 			    DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1940 			    &fs_cnt);
1941 			if (err != ENOENT && err != 0) {
1942 				dsl_dir_rele(newparent, FTAG);
1943 				dsl_dir_rele(dd, FTAG);
1944 				return (err);
1945 			}
1946 
1947 			/*
1948 			 * have to add 1 for the filesystem itself that we're
1949 			 * moving
1950 			 */
1951 			fs_cnt++;
1952 
1953 			err = zap_lookup(os, dd->dd_object,
1954 			    DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1955 			    &ss_cnt);
1956 			if (err != ENOENT && err != 0) {
1957 				dsl_dir_rele(newparent, FTAG);
1958 				dsl_dir_rele(dd, FTAG);
1959 				return (err);
1960 			}
1961 		}
1962 
1963 		/* check for encryption errors */
1964 		error = dsl_dir_rename_crypt_check(dd, newparent);
1965 		if (error != 0) {
1966 			dsl_dir_rele(newparent, FTAG);
1967 			dsl_dir_rele(dd, FTAG);
1968 			return (SET_ERROR(EACCES));
1969 		}
1970 
1971 		/* no rename into our descendant */
1972 		if (closest_common_ancestor(dd, newparent) == dd) {
1973 			dsl_dir_rele(newparent, FTAG);
1974 			dsl_dir_rele(dd, FTAG);
1975 			return (SET_ERROR(EINVAL));
1976 		}
1977 
1978 		error = dsl_dir_transfer_possible(dd->dd_parent,
1979 		    newparent, fs_cnt, ss_cnt, myspace, ddra->ddra_cred);
1980 		if (error != 0) {
1981 			dsl_dir_rele(newparent, FTAG);
1982 			dsl_dir_rele(dd, FTAG);
1983 			return (error);
1984 		}
1985 	}
1986 
1987 	dsl_dir_rele(newparent, FTAG);
1988 	dsl_dir_rele(dd, FTAG);
1989 	return (0);
1990 }
1991 
1992 static void
1993 dsl_dir_rename_sync(void *arg, dmu_tx_t *tx)
1994 {
1995 	dsl_dir_rename_arg_t *ddra = arg;
1996 	dsl_pool_t *dp = dmu_tx_pool(tx);
1997 	dsl_dir_t *dd, *newparent;
1998 	const char *mynewname;
1999 	int error;
2000 	objset_t *mos = dp->dp_meta_objset;
2001 
2002 	VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL));
2003 	VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent,
2004 	    &mynewname));
2005 
2006 	/* Log this before we change the name. */
2007 	spa_history_log_internal_dd(dd, "rename", tx,
2008 	    "-> %s", ddra->ddra_newname);
2009 
2010 	if (newparent != dd->dd_parent) {
2011 		objset_t *os = dd->dd_pool->dp_meta_objset;
2012 		uint64_t fs_cnt = 0;
2013 		uint64_t ss_cnt = 0;
2014 
2015 		/*
2016 		 * We already made sure the dd counts were initialized in the
2017 		 * check function.
2018 		 */
2019 		if (spa_feature_is_active(dp->dp_spa,
2020 		    SPA_FEATURE_FS_SS_LIMIT)) {
2021 			VERIFY0(zap_lookup(os, dd->dd_object,
2022 			    DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
2023 			    &fs_cnt));
2024 			/* add 1 for the filesystem itself that we're moving */
2025 			fs_cnt++;
2026 
2027 			VERIFY0(zap_lookup(os, dd->dd_object,
2028 			    DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
2029 			    &ss_cnt));
2030 		}
2031 
2032 		dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt,
2033 		    DD_FIELD_FILESYSTEM_COUNT, tx);
2034 		dsl_fs_ss_count_adjust(newparent, fs_cnt,
2035 		    DD_FIELD_FILESYSTEM_COUNT, tx);
2036 
2037 		dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt,
2038 		    DD_FIELD_SNAPSHOT_COUNT, tx);
2039 		dsl_fs_ss_count_adjust(newparent, ss_cnt,
2040 		    DD_FIELD_SNAPSHOT_COUNT, tx);
2041 
2042 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
2043 		    -dsl_dir_phys(dd)->dd_used_bytes,
2044 		    -dsl_dir_phys(dd)->dd_compressed_bytes,
2045 		    -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
2046 		dsl_dir_diduse_space(newparent, DD_USED_CHILD,
2047 		    dsl_dir_phys(dd)->dd_used_bytes,
2048 		    dsl_dir_phys(dd)->dd_compressed_bytes,
2049 		    dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
2050 
2051 		if (dsl_dir_phys(dd)->dd_reserved >
2052 		    dsl_dir_phys(dd)->dd_used_bytes) {
2053 			uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved -
2054 			    dsl_dir_phys(dd)->dd_used_bytes;
2055 
2056 			dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
2057 			    -unused_rsrv, 0, 0, tx);
2058 			dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV,
2059 			    unused_rsrv, 0, 0, tx);
2060 		}
2061 	}
2062 
2063 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
2064 
2065 	/* remove from old parent zapobj */
2066 	error = zap_remove(mos,
2067 	    dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
2068 	    dd->dd_myname, tx);
2069 	ASSERT0(error);
2070 
2071 	(void) strcpy(dd->dd_myname, mynewname);
2072 	dsl_dir_rele(dd->dd_parent, dd);
2073 	dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object;
2074 	VERIFY0(dsl_dir_hold_obj(dp,
2075 	    newparent->dd_object, NULL, dd, &dd->dd_parent));
2076 
2077 	/* add to new parent zapobj */
2078 	VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj,
2079 	    dd->dd_myname, 8, 1, &dd->dd_object, tx));
2080 
2081 	dsl_prop_notify_all(dd);
2082 
2083 	dsl_dir_rele(newparent, FTAG);
2084 	dsl_dir_rele(dd, FTAG);
2085 }
2086 
2087 int
2088 dsl_dir_rename(const char *oldname, const char *newname)
2089 {
2090 	dsl_dir_rename_arg_t ddra;
2091 
2092 	ddra.ddra_oldname = oldname;
2093 	ddra.ddra_newname = newname;
2094 	ddra.ddra_cred = CRED();
2095 
2096 	return (dsl_sync_task(oldname,
2097 	    dsl_dir_rename_check, dsl_dir_rename_sync, &ddra,
2098 	    3, ZFS_SPACE_CHECK_RESERVED));
2099 }
2100 
2101 int
2102 dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
2103     uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *cr)
2104 {
2105 	dsl_dir_t *ancestor;
2106 	int64_t adelta;
2107 	uint64_t avail;
2108 	int err;
2109 
2110 	ancestor = closest_common_ancestor(sdd, tdd);
2111 	adelta = would_change(sdd, -space, ancestor);
2112 	avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
2113 	if (avail < space)
2114 		return (SET_ERROR(ENOSPC));
2115 
2116 	err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT,
2117 	    ancestor, cr);
2118 	if (err != 0)
2119 		return (err);
2120 	err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT,
2121 	    ancestor, cr);
2122 	if (err != 0)
2123 		return (err);
2124 
2125 	return (0);
2126 }
2127 
2128 timestruc_t
2129 dsl_dir_snap_cmtime(dsl_dir_t *dd)
2130 {
2131 	timestruc_t t;
2132 
2133 	mutex_enter(&dd->dd_lock);
2134 	t = dd->dd_snap_cmtime;
2135 	mutex_exit(&dd->dd_lock);
2136 
2137 	return (t);
2138 }
2139 
2140 void
2141 dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
2142 {
2143 	timestruc_t t;
2144 
2145 	gethrestime(&t);
2146 	mutex_enter(&dd->dd_lock);
2147 	dd->dd_snap_cmtime = t;
2148 	mutex_exit(&dd->dd_lock);
2149 }
2150 
2151 void
2152 dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx)
2153 {
2154 	objset_t *mos = dd->dd_pool->dp_meta_objset;
2155 	dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx);
2156 }
2157 
2158 boolean_t
2159 dsl_dir_is_zapified(dsl_dir_t *dd)
2160 {
2161 	dmu_object_info_t doi;
2162 
2163 	dmu_object_info_from_db(dd->dd_dbuf, &doi);
2164 	return (doi.doi_type == DMU_OTN_ZAP_METADATA);
2165 }
2166