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