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