xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_dir.c (revision 2cb5535af222653abf2eba5c180ded4a7b85d8b6)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/dmu.h>
29 #include <sys/dmu_objset.h>
30 #include <sys/dmu_tx.h>
31 #include <sys/dsl_dataset.h>
32 #include <sys/dsl_dir.h>
33 #include <sys/dsl_prop.h>
34 #include <sys/dsl_synctask.h>
35 #include <sys/dsl_deleg.h>
36 #include <sys/spa.h>
37 #include <sys/zap.h>
38 #include <sys/zio.h>
39 #include <sys/arc.h>
40 #include <sys/sunddi.h>
41 #include "zfs_namecheck.h"
42 
43 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
44 static void dsl_dir_set_reservation_sync(void *arg1, void *arg2,
45     cred_t *cr, dmu_tx_t *tx);
46 
47 
48 /* ARGSUSED */
49 static void
50 dsl_dir_evict(dmu_buf_t *db, void *arg)
51 {
52 	dsl_dir_t *dd = arg;
53 	dsl_pool_t *dp = dd->dd_pool;
54 	int t;
55 
56 	for (t = 0; t < TXG_SIZE; t++) {
57 		ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
58 		ASSERT(dd->dd_tempreserved[t] == 0);
59 		ASSERT(dd->dd_space_towrite[t] == 0);
60 	}
61 
62 	ASSERT3U(dd->dd_used_bytes, ==, dd->dd_phys->dd_used_bytes);
63 
64 	if (dd->dd_parent)
65 		dsl_dir_close(dd->dd_parent, dd);
66 
67 	spa_close(dd->dd_pool->dp_spa, dd);
68 
69 	/*
70 	 * The props callback list should be empty since they hold the
71 	 * dir open.
72 	 */
73 	list_destroy(&dd->dd_prop_cbs);
74 	mutex_destroy(&dd->dd_lock);
75 	kmem_free(dd, sizeof (dsl_dir_t));
76 }
77 
78 int
79 dsl_dir_open_obj(dsl_pool_t *dp, uint64_t ddobj,
80     const char *tail, void *tag, dsl_dir_t **ddp)
81 {
82 	dmu_buf_t *dbuf;
83 	dsl_dir_t *dd;
84 	int err;
85 
86 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
87 	    dsl_pool_sync_context(dp));
88 
89 	err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
90 	if (err)
91 		return (err);
92 	dd = dmu_buf_get_user(dbuf);
93 #ifdef ZFS_DEBUG
94 	{
95 		dmu_object_info_t doi;
96 		dmu_object_info_from_db(dbuf, &doi);
97 		ASSERT3U(doi.doi_type, ==, DMU_OT_DSL_DIR);
98 	}
99 #endif
100 	/* XXX assert bonus buffer size is correct */
101 	if (dd == NULL) {
102 		dsl_dir_t *winner;
103 		int err;
104 
105 		dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
106 		dd->dd_object = ddobj;
107 		dd->dd_dbuf = dbuf;
108 		dd->dd_pool = dp;
109 		dd->dd_phys = dbuf->db_data;
110 		dd->dd_used_bytes = dd->dd_phys->dd_used_bytes;
111 		mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
112 
113 		list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t),
114 		    offsetof(dsl_prop_cb_record_t, cbr_node));
115 
116 		if (dd->dd_phys->dd_parent_obj) {
117 			err = dsl_dir_open_obj(dp, dd->dd_phys->dd_parent_obj,
118 			    NULL, dd, &dd->dd_parent);
119 			if (err) {
120 				mutex_destroy(&dd->dd_lock);
121 				kmem_free(dd, sizeof (dsl_dir_t));
122 				dmu_buf_rele(dbuf, tag);
123 				return (err);
124 			}
125 			if (tail) {
126 #ifdef ZFS_DEBUG
127 				uint64_t foundobj;
128 
129 				err = zap_lookup(dp->dp_meta_objset,
130 				    dd->dd_parent->dd_phys->dd_child_dir_zapobj,
131 				    tail, sizeof (foundobj), 1, &foundobj);
132 				ASSERT(err || foundobj == ddobj);
133 #endif
134 				(void) strcpy(dd->dd_myname, tail);
135 			} else {
136 				err = zap_value_search(dp->dp_meta_objset,
137 				    dd->dd_parent->dd_phys->dd_child_dir_zapobj,
138 				    ddobj, 0, dd->dd_myname);
139 			}
140 			if (err) {
141 				dsl_dir_close(dd->dd_parent, dd);
142 				mutex_destroy(&dd->dd_lock);
143 				kmem_free(dd, sizeof (dsl_dir_t));
144 				dmu_buf_rele(dbuf, tag);
145 				return (err);
146 			}
147 		} else {
148 			(void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
149 		}
150 
151 		winner = dmu_buf_set_user_ie(dbuf, dd, &dd->dd_phys,
152 		    dsl_dir_evict);
153 		if (winner) {
154 			if (dd->dd_parent)
155 				dsl_dir_close(dd->dd_parent, dd);
156 			mutex_destroy(&dd->dd_lock);
157 			kmem_free(dd, sizeof (dsl_dir_t));
158 			dd = winner;
159 		} else {
160 			spa_open_ref(dp->dp_spa, dd);
161 		}
162 	}
163 
164 	/*
165 	 * The dsl_dir_t has both open-to-close and instantiate-to-evict
166 	 * holds on the spa.  We need the open-to-close holds because
167 	 * otherwise the spa_refcnt wouldn't change when we open a
168 	 * dir which the spa also has open, so we could incorrectly
169 	 * think it was OK to unload/export/destroy the pool.  We need
170 	 * the instantiate-to-evict hold because the dsl_dir_t has a
171 	 * pointer to the dd_pool, which has a pointer to the spa_t.
172 	 */
173 	spa_open_ref(dp->dp_spa, tag);
174 	ASSERT3P(dd->dd_pool, ==, dp);
175 	ASSERT3U(dd->dd_object, ==, ddobj);
176 	ASSERT3P(dd->dd_dbuf, ==, dbuf);
177 	*ddp = dd;
178 	return (0);
179 }
180 
181 void
182 dsl_dir_close(dsl_dir_t *dd, void *tag)
183 {
184 	dprintf_dd(dd, "%s\n", "");
185 	spa_close(dd->dd_pool->dp_spa, tag);
186 	dmu_buf_rele(dd->dd_dbuf, tag);
187 }
188 
189 /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */
190 void
191 dsl_dir_name(dsl_dir_t *dd, char *buf)
192 {
193 	if (dd->dd_parent) {
194 		dsl_dir_name(dd->dd_parent, buf);
195 		(void) strcat(buf, "/");
196 	} else {
197 		buf[0] = '\0';
198 	}
199 	if (!MUTEX_HELD(&dd->dd_lock)) {
200 		/*
201 		 * recursive mutex so that we can use
202 		 * dprintf_dd() with dd_lock held
203 		 */
204 		mutex_enter(&dd->dd_lock);
205 		(void) strcat(buf, dd->dd_myname);
206 		mutex_exit(&dd->dd_lock);
207 	} else {
208 		(void) strcat(buf, dd->dd_myname);
209 	}
210 }
211 
212 /* Calculate name legnth, avoiding all the strcat calls of dsl_dir_name */
213 int
214 dsl_dir_namelen(dsl_dir_t *dd)
215 {
216 	int result = 0;
217 
218 	if (dd->dd_parent) {
219 		/* parent's name + 1 for the "/" */
220 		result = dsl_dir_namelen(dd->dd_parent) + 1;
221 	}
222 
223 	if (!MUTEX_HELD(&dd->dd_lock)) {
224 		/* see dsl_dir_name */
225 		mutex_enter(&dd->dd_lock);
226 		result += strlen(dd->dd_myname);
227 		mutex_exit(&dd->dd_lock);
228 	} else {
229 		result += strlen(dd->dd_myname);
230 	}
231 
232 	return (result);
233 }
234 
235 int
236 dsl_dir_is_private(dsl_dir_t *dd)
237 {
238 	int rv = FALSE;
239 
240 	if (dd->dd_parent && dsl_dir_is_private(dd->dd_parent))
241 		rv = TRUE;
242 	if (dataset_name_hidden(dd->dd_myname))
243 		rv = TRUE;
244 	return (rv);
245 }
246 
247 
248 static int
249 getcomponent(const char *path, char *component, const char **nextp)
250 {
251 	char *p;
252 	if (path == NULL)
253 		return (ENOENT);
254 	/* This would be a good place to reserve some namespace... */
255 	p = strpbrk(path, "/@");
256 	if (p && (p[1] == '/' || p[1] == '@')) {
257 		/* two separators in a row */
258 		return (EINVAL);
259 	}
260 	if (p == NULL || p == path) {
261 		/*
262 		 * if the first thing is an @ or /, it had better be an
263 		 * @ and it had better not have any more ats or slashes,
264 		 * and it had better have something after the @.
265 		 */
266 		if (p != NULL &&
267 		    (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
268 			return (EINVAL);
269 		if (strlen(path) >= MAXNAMELEN)
270 			return (ENAMETOOLONG);
271 		(void) strcpy(component, path);
272 		p = NULL;
273 	} else if (p[0] == '/') {
274 		if (p-path >= MAXNAMELEN)
275 			return (ENAMETOOLONG);
276 		(void) strncpy(component, path, p - path);
277 		component[p-path] = '\0';
278 		p++;
279 	} else if (p[0] == '@') {
280 		/*
281 		 * if the next separator is an @, there better not be
282 		 * any more slashes.
283 		 */
284 		if (strchr(path, '/'))
285 			return (EINVAL);
286 		if (p-path >= MAXNAMELEN)
287 			return (ENAMETOOLONG);
288 		(void) strncpy(component, path, p - path);
289 		component[p-path] = '\0';
290 	} else {
291 		ASSERT(!"invalid p");
292 	}
293 	*nextp = p;
294 	return (0);
295 }
296 
297 /*
298  * same as dsl_open_dir, ignore the first component of name and use the
299  * spa instead
300  */
301 int
302 dsl_dir_open_spa(spa_t *spa, const char *name, void *tag,
303     dsl_dir_t **ddp, const char **tailp)
304 {
305 	char buf[MAXNAMELEN];
306 	const char *next, *nextnext = NULL;
307 	int err;
308 	dsl_dir_t *dd;
309 	dsl_pool_t *dp;
310 	uint64_t ddobj;
311 	int openedspa = FALSE;
312 
313 	dprintf("%s\n", name);
314 
315 	err = getcomponent(name, buf, &next);
316 	if (err)
317 		return (err);
318 	if (spa == NULL) {
319 		err = spa_open(buf, &spa, FTAG);
320 		if (err) {
321 			dprintf("spa_open(%s) failed\n", buf);
322 			return (err);
323 		}
324 		openedspa = TRUE;
325 
326 		/* XXX this assertion belongs in spa_open */
327 		ASSERT(!dsl_pool_sync_context(spa_get_dsl(spa)));
328 	}
329 
330 	dp = spa_get_dsl(spa);
331 
332 	rw_enter(&dp->dp_config_rwlock, RW_READER);
333 	err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
334 	if (err) {
335 		rw_exit(&dp->dp_config_rwlock);
336 		if (openedspa)
337 			spa_close(spa, FTAG);
338 		return (err);
339 	}
340 
341 	while (next != NULL) {
342 		dsl_dir_t *child_ds;
343 		err = getcomponent(next, buf, &nextnext);
344 		if (err)
345 			break;
346 		ASSERT(next[0] != '\0');
347 		if (next[0] == '@')
348 			break;
349 		dprintf("looking up %s in obj%lld\n",
350 		    buf, dd->dd_phys->dd_child_dir_zapobj);
351 
352 		err = zap_lookup(dp->dp_meta_objset,
353 		    dd->dd_phys->dd_child_dir_zapobj,
354 		    buf, sizeof (ddobj), 1, &ddobj);
355 		if (err) {
356 			if (err == ENOENT)
357 				err = 0;
358 			break;
359 		}
360 
361 		err = dsl_dir_open_obj(dp, ddobj, buf, tag, &child_ds);
362 		if (err)
363 			break;
364 		dsl_dir_close(dd, tag);
365 		dd = child_ds;
366 		next = nextnext;
367 	}
368 	rw_exit(&dp->dp_config_rwlock);
369 
370 	if (err) {
371 		dsl_dir_close(dd, tag);
372 		if (openedspa)
373 			spa_close(spa, FTAG);
374 		return (err);
375 	}
376 
377 	/*
378 	 * It's an error if there's more than one component left, or
379 	 * tailp==NULL and there's any component left.
380 	 */
381 	if (next != NULL &&
382 	    (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
383 		/* bad path name */
384 		dsl_dir_close(dd, tag);
385 		dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
386 		err = ENOENT;
387 	}
388 	if (tailp)
389 		*tailp = next;
390 	if (openedspa)
391 		spa_close(spa, FTAG);
392 	*ddp = dd;
393 	return (err);
394 }
395 
396 /*
397  * Return the dsl_dir_t, and possibly the last component which couldn't
398  * be found in *tail.  Return NULL if the path is bogus, or if
399  * tail==NULL and we couldn't parse the whole name.  (*tail)[0] == '@'
400  * means that the last component is a snapshot.
401  */
402 int
403 dsl_dir_open(const char *name, void *tag, dsl_dir_t **ddp, const char **tailp)
404 {
405 	return (dsl_dir_open_spa(NULL, name, tag, ddp, tailp));
406 }
407 
408 uint64_t
409 dsl_dir_create_sync(dsl_dir_t *pds, const char *name, dmu_tx_t *tx)
410 {
411 	objset_t *mos = pds->dd_pool->dp_meta_objset;
412 	uint64_t ddobj;
413 	dsl_dir_phys_t *dsphys;
414 	dmu_buf_t *dbuf;
415 
416 	ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
417 	    DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
418 	VERIFY(0 == zap_add(mos, pds->dd_phys->dd_child_dir_zapobj,
419 	    name, sizeof (uint64_t), 1, &ddobj, tx));
420 	VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
421 	dmu_buf_will_dirty(dbuf, tx);
422 	dsphys = dbuf->db_data;
423 
424 	dsphys->dd_creation_time = gethrestime_sec();
425 	dsphys->dd_parent_obj = pds->dd_object;
426 	dsphys->dd_props_zapobj = zap_create(mos,
427 	    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
428 	dsphys->dd_child_dir_zapobj = zap_create(mos,
429 	    DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
430 	dmu_buf_rele(dbuf, FTAG);
431 
432 	return (ddobj);
433 }
434 
435 /* ARGSUSED */
436 int
437 dsl_dir_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
438 {
439 	dsl_dir_t *dd = arg1;
440 	dsl_pool_t *dp = dd->dd_pool;
441 	objset_t *mos = dp->dp_meta_objset;
442 	int err;
443 	uint64_t count;
444 
445 	/*
446 	 * There should be exactly two holds, both from
447 	 * dsl_dataset_destroy: one on the dd directory, and one on its
448 	 * head ds.  Otherwise, someone is trying to lookup something
449 	 * inside this dir while we want to destroy it.  The
450 	 * config_rwlock ensures that nobody else opens it after we
451 	 * check.
452 	 */
453 	if (dmu_buf_refcount(dd->dd_dbuf) > 2)
454 		return (EBUSY);
455 
456 	err = zap_count(mos, dd->dd_phys->dd_child_dir_zapobj, &count);
457 	if (err)
458 		return (err);
459 	if (count != 0)
460 		return (EEXIST);
461 
462 	return (0);
463 }
464 
465 void
466 dsl_dir_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx)
467 {
468 	dsl_dir_t *dd = arg1;
469 	objset_t *mos = dd->dd_pool->dp_meta_objset;
470 	uint64_t val, obj;
471 
472 	ASSERT(RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock));
473 	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
474 
475 	/* Remove our reservation. */
476 	val = 0;
477 	dsl_dir_set_reservation_sync(dd, &val, cr, tx);
478 	ASSERT3U(dd->dd_used_bytes, ==, 0);
479 	ASSERT3U(dd->dd_phys->dd_reserved, ==, 0);
480 
481 	VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
482 	VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
483 	VERIFY(0 == dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx));
484 	VERIFY(0 == zap_remove(mos,
485 	    dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
486 
487 	obj = dd->dd_object;
488 	dsl_dir_close(dd, tag);
489 	VERIFY(0 == dmu_object_free(mos, obj, tx));
490 }
491 
492 void
493 dsl_dir_create_root(objset_t *mos, uint64_t *ddobjp, dmu_tx_t *tx)
494 {
495 	dsl_dir_phys_t *dsp;
496 	dmu_buf_t *dbuf;
497 	int error;
498 
499 	*ddobjp = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
500 	    DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
501 
502 	error = zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ROOT_DATASET,
503 	    sizeof (uint64_t), 1, ddobjp, tx);
504 	ASSERT3U(error, ==, 0);
505 
506 	VERIFY(0 == dmu_bonus_hold(mos, *ddobjp, FTAG, &dbuf));
507 	dmu_buf_will_dirty(dbuf, tx);
508 	dsp = dbuf->db_data;
509 
510 	dsp->dd_creation_time = gethrestime_sec();
511 	dsp->dd_props_zapobj = zap_create(mos,
512 	    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
513 	dsp->dd_child_dir_zapobj = zap_create(mos,
514 	    DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
515 
516 	dmu_buf_rele(dbuf, FTAG);
517 }
518 
519 void
520 dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
521 {
522 	mutex_enter(&dd->dd_lock);
523 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, dd->dd_used_bytes);
524 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA, dd->dd_phys->dd_quota);
525 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
526 	    dd->dd_phys->dd_reserved);
527 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
528 	    dd->dd_phys->dd_compressed_bytes == 0 ? 100 :
529 	    (dd->dd_phys->dd_uncompressed_bytes * 100 /
530 	    dd->dd_phys->dd_compressed_bytes));
531 	mutex_exit(&dd->dd_lock);
532 
533 	rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
534 	if (dd->dd_phys->dd_origin_obj) {
535 		dsl_dataset_t *ds;
536 		char buf[MAXNAMELEN];
537 
538 		VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool,
539 		    dd->dd_phys->dd_origin_obj,
540 		    NULL, DS_MODE_NONE, FTAG, &ds));
541 		dsl_dataset_name(ds, buf);
542 		dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
543 		dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
544 	}
545 	rw_exit(&dd->dd_pool->dp_config_rwlock);
546 }
547 
548 void
549 dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
550 {
551 	dsl_pool_t *dp = dd->dd_pool;
552 
553 	ASSERT(dd->dd_phys);
554 
555 	if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg) == 0) {
556 		/* up the hold count until we can be written out */
557 		dmu_buf_add_ref(dd->dd_dbuf, dd);
558 	}
559 }
560 
561 static int64_t
562 parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
563 {
564 	uint64_t old_accounted = MAX(used, dd->dd_phys->dd_reserved);
565 	uint64_t new_accounted = MAX(used + delta, dd->dd_phys->dd_reserved);
566 	return (new_accounted - old_accounted);
567 }
568 
569 void
570 dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
571 {
572 	ASSERT(dmu_tx_is_syncing(tx));
573 
574 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
575 
576 	mutex_enter(&dd->dd_lock);
577 	ASSERT3U(dd->dd_tempreserved[tx->tx_txg&TXG_MASK], ==, 0);
578 	dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
579 	    dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
580 	dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
581 	dd->dd_phys->dd_used_bytes = dd->dd_used_bytes;
582 	mutex_exit(&dd->dd_lock);
583 
584 	/* release the hold from dsl_dir_dirty */
585 	dmu_buf_rele(dd->dd_dbuf, dd);
586 }
587 
588 static uint64_t
589 dsl_dir_space_towrite(dsl_dir_t *dd)
590 {
591 	uint64_t space = 0;
592 	int i;
593 
594 	ASSERT(MUTEX_HELD(&dd->dd_lock));
595 
596 	for (i = 0; i < TXG_SIZE; i++) {
597 		space += dd->dd_space_towrite[i&TXG_MASK];
598 		ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
599 	}
600 	return (space);
601 }
602 
603 /*
604  * How much space would dd have available if ancestor had delta applied
605  * to it?  If ondiskonly is set, we're only interested in what's
606  * on-disk, not estimated pending changes.
607  */
608 uint64_t
609 dsl_dir_space_available(dsl_dir_t *dd,
610     dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
611 {
612 	uint64_t parentspace, myspace, quota, used;
613 
614 	/*
615 	 * If there are no restrictions otherwise, assume we have
616 	 * unlimited space available.
617 	 */
618 	quota = UINT64_MAX;
619 	parentspace = UINT64_MAX;
620 
621 	if (dd->dd_parent != NULL) {
622 		parentspace = dsl_dir_space_available(dd->dd_parent,
623 		    ancestor, delta, ondiskonly);
624 	}
625 
626 	mutex_enter(&dd->dd_lock);
627 	if (dd->dd_phys->dd_quota != 0)
628 		quota = dd->dd_phys->dd_quota;
629 	used = dd->dd_used_bytes;
630 	if (!ondiskonly)
631 		used += dsl_dir_space_towrite(dd);
632 	if (dd == ancestor)
633 		used += delta;
634 
635 	if (dd->dd_parent == NULL) {
636 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
637 		quota = MIN(quota, poolsize);
638 	}
639 
640 	if (dd->dd_phys->dd_reserved > used && parentspace != UINT64_MAX) {
641 		/*
642 		 * We have some space reserved, in addition to what our
643 		 * parent gave us.
644 		 */
645 		parentspace += dd->dd_phys->dd_reserved - used;
646 	}
647 
648 	if (used > quota) {
649 		/* over quota */
650 		myspace = 0;
651 
652 		/*
653 		 * While it's OK to be a little over quota, if
654 		 * we think we are using more space than there
655 		 * is in the pool (which is already 1.6% more than
656 		 * dsl_pool_adjustedsize()), something is very
657 		 * wrong.
658 		 */
659 		ASSERT3U(used, <=, spa_get_space(dd->dd_pool->dp_spa));
660 	} else {
661 		/*
662 		 * the lesser of the space provided by our parent and
663 		 * the space left in our quota
664 		 */
665 		myspace = MIN(parentspace, quota - used);
666 	}
667 
668 	mutex_exit(&dd->dd_lock);
669 
670 	return (myspace);
671 }
672 
673 struct tempreserve {
674 	list_node_t tr_node;
675 	dsl_pool_t *tr_dp;
676 	dsl_dir_t *tr_ds;
677 	uint64_t tr_size;
678 };
679 
680 static int
681 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
682     boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
683     dmu_tx_t *tx)
684 {
685 	uint64_t txg = tx->tx_txg;
686 	uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
687 	struct tempreserve *tr;
688 	int enospc = EDQUOT;
689 	int txgidx = txg & TXG_MASK;
690 	int i;
691 	uint64_t ref_rsrv = 0;
692 
693 	ASSERT3U(txg, !=, 0);
694 	ASSERT3S(asize, >, 0);
695 
696 	mutex_enter(&dd->dd_lock);
697 
698 	/*
699 	 * Check against the dsl_dir's quota.  We don't add in the delta
700 	 * when checking for over-quota because they get one free hit.
701 	 */
702 	est_inflight = dsl_dir_space_towrite(dd);
703 	for (i = 0; i < TXG_SIZE; i++)
704 		est_inflight += dd->dd_tempreserved[i];
705 	used_on_disk = dd->dd_used_bytes;
706 
707 	/*
708 	 * Check for dataset reference quota on first iteration.
709 	 */
710 	if (list_head(tr_list) == NULL && tx->tx_objset) {
711 		int error;
712 		dsl_dataset_t *ds = tx->tx_objset->os->os_dsl_dataset;
713 
714 		error = dsl_dataset_check_quota(ds, checkrefquota,
715 		    asize, est_inflight, &used_on_disk, &ref_rsrv);
716 		if (error) {
717 			mutex_exit(&dd->dd_lock);
718 			return (error);
719 		}
720 	}
721 
722 	/*
723 	 * If this transaction will result in a net free of space,
724 	 * we want to let it through.
725 	 */
726 	if (ignorequota || netfree || dd->dd_phys->dd_quota == 0)
727 		quota = UINT64_MAX;
728 	else
729 		quota = dd->dd_phys->dd_quota;
730 
731 	/*
732 	 * Adjust the quota against the actual pool size at the root.
733 	 * To ensure that it's possible to remove files from a full
734 	 * pool without inducing transient overcommits, we throttle
735 	 * netfree transactions against a quota that is slightly larger,
736 	 * but still within the pool's allocation slop.  In cases where
737 	 * we're very close to full, this will allow a steady trickle of
738 	 * removes to get through.
739 	 */
740 	if (dd->dd_parent == NULL) {
741 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
742 		if (poolsize < quota) {
743 			quota = poolsize;
744 			enospc = ENOSPC;
745 		}
746 	}
747 
748 	/*
749 	 * If they are requesting more space, and our current estimate
750 	 * is over quota, they get to try again unless the actual
751 	 * on-disk is over quota and there are no pending changes (which
752 	 * may free up space for us).
753 	 */
754 	if (used_on_disk + est_inflight > quota) {
755 		if (est_inflight > 0 || used_on_disk < quota)
756 			enospc = ERESTART;
757 		dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
758 		    "quota=%lluK tr=%lluK err=%d\n",
759 		    used_on_disk>>10, est_inflight>>10,
760 		    quota>>10, asize>>10, enospc);
761 		mutex_exit(&dd->dd_lock);
762 		return (enospc);
763 	}
764 
765 	/* We need to up our estimated delta before dropping dd_lock */
766 	dd->dd_tempreserved[txgidx] += asize;
767 
768 	parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
769 	    asize - ref_rsrv);
770 	mutex_exit(&dd->dd_lock);
771 
772 	tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
773 	tr->tr_ds = dd;
774 	tr->tr_size = asize;
775 	list_insert_tail(tr_list, tr);
776 
777 	/* see if it's OK with our parent */
778 	if (dd->dd_parent && parent_rsrv) {
779 		boolean_t ismos = (dd->dd_phys->dd_head_dataset_obj == 0);
780 
781 		return (dsl_dir_tempreserve_impl(dd->dd_parent,
782 		    parent_rsrv, netfree, ismos, TRUE, tr_list, tx));
783 	} else {
784 		return (0);
785 	}
786 }
787 
788 /*
789  * Reserve space in this dsl_dir, to be used in this tx's txg.
790  * After the space has been dirtied (and dsl_dir_willuse_space()
791  * has been called), the reservation should be canceled, using
792  * dsl_dir_tempreserve_clear().
793  */
794 int
795 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
796     uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
797 {
798 	int err;
799 	list_t *tr_list;
800 
801 	if (asize == 0) {
802 		*tr_cookiep = NULL;
803 		return (0);
804 	}
805 
806 	tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
807 	list_create(tr_list, sizeof (struct tempreserve),
808 	    offsetof(struct tempreserve, tr_node));
809 	ASSERT3S(asize, >, 0);
810 	ASSERT3S(fsize, >=, 0);
811 
812 	err = arc_tempreserve_space(lsize, tx->tx_txg);
813 	if (err == 0) {
814 		struct tempreserve *tr;
815 
816 		tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
817 		tr->tr_size = lsize;
818 		list_insert_tail(tr_list, tr);
819 
820 		err = dsl_pool_tempreserve_space(dd->dd_pool, asize, tx);
821 	} else {
822 		if (err == EAGAIN) {
823 			txg_delay(dd->dd_pool, tx->tx_txg, 1);
824 			err = ERESTART;
825 		}
826 		dsl_pool_memory_pressure(dd->dd_pool);
827 	}
828 
829 	if (err == 0) {
830 		struct tempreserve *tr;
831 
832 		tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
833 		tr->tr_dp = dd->dd_pool;
834 		tr->tr_size = asize;
835 		list_insert_tail(tr_list, tr);
836 
837 		err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
838 		    FALSE, asize > usize, tr_list, tx);
839 	}
840 
841 	if (err)
842 		dsl_dir_tempreserve_clear(tr_list, tx);
843 	else
844 		*tr_cookiep = tr_list;
845 
846 	return (err);
847 }
848 
849 /*
850  * Clear a temporary reservation that we previously made with
851  * dsl_dir_tempreserve_space().
852  */
853 void
854 dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
855 {
856 	int txgidx = tx->tx_txg & TXG_MASK;
857 	list_t *tr_list = tr_cookie;
858 	struct tempreserve *tr;
859 
860 	ASSERT3U(tx->tx_txg, !=, 0);
861 
862 	if (tr_cookie == NULL)
863 		return;
864 
865 	while (tr = list_head(tr_list)) {
866 		if (tr->tr_dp) {
867 			dsl_pool_tempreserve_clear(tr->tr_dp, tr->tr_size, tx);
868 		} else if (tr->tr_ds) {
869 			mutex_enter(&tr->tr_ds->dd_lock);
870 			ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
871 			    tr->tr_size);
872 			tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
873 			mutex_exit(&tr->tr_ds->dd_lock);
874 		} else {
875 			arc_tempreserve_clear(tr->tr_size);
876 		}
877 		list_remove(tr_list, tr);
878 		kmem_free(tr, sizeof (struct tempreserve));
879 	}
880 
881 	kmem_free(tr_list, sizeof (list_t));
882 }
883 
884 static void
885 dsl_dir_willuse_space_impl(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
886 {
887 	int64_t parent_space;
888 	uint64_t est_used;
889 
890 	mutex_enter(&dd->dd_lock);
891 	if (space > 0)
892 		dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
893 
894 	est_used = dsl_dir_space_towrite(dd) + dd->dd_used_bytes;
895 	parent_space = parent_delta(dd, est_used, space);
896 	mutex_exit(&dd->dd_lock);
897 
898 	/* Make sure that we clean up dd_space_to* */
899 	dsl_dir_dirty(dd, tx);
900 
901 	/* XXX this is potentially expensive and unnecessary... */
902 	if (parent_space && dd->dd_parent)
903 		dsl_dir_willuse_space_impl(dd->dd_parent, parent_space, tx);
904 }
905 
906 /*
907  * Call in open context when we think we're going to write/free space,
908  * eg. when dirtying data.  Be conservative (ie. OK to write less than
909  * this or free more than this, but don't write more or free less).
910  */
911 void
912 dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
913 {
914 	dsl_pool_willuse_space(dd->dd_pool, space, tx);
915 	dsl_dir_willuse_space_impl(dd, space, tx);
916 }
917 
918 /* call from syncing context when we actually write/free space for this dd */
919 void
920 dsl_dir_diduse_space(dsl_dir_t *dd,
921     int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
922 {
923 	int64_t accounted_delta;
924 
925 	ASSERT(dmu_tx_is_syncing(tx));
926 
927 	dsl_dir_dirty(dd, tx);
928 
929 	mutex_enter(&dd->dd_lock);
930 	accounted_delta = parent_delta(dd, dd->dd_used_bytes, used);
931 	ASSERT(used >= 0 || dd->dd_used_bytes >= -used);
932 	ASSERT(compressed >= 0 ||
933 	    dd->dd_phys->dd_compressed_bytes >= -compressed);
934 	ASSERT(uncompressed >= 0 ||
935 	    dd->dd_phys->dd_uncompressed_bytes >= -uncompressed);
936 	dd->dd_used_bytes += used;
937 	dd->dd_phys->dd_uncompressed_bytes += uncompressed;
938 	dd->dd_phys->dd_compressed_bytes += compressed;
939 	mutex_exit(&dd->dd_lock);
940 
941 	if (dd->dd_parent != NULL) {
942 		dsl_dir_diduse_space(dd->dd_parent,
943 		    accounted_delta, compressed, uncompressed, tx);
944 	}
945 }
946 
947 static int
948 dsl_dir_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
949 {
950 	dsl_dir_t *dd = arg1;
951 	uint64_t *quotap = arg2;
952 	uint64_t new_quota = *quotap;
953 	int err = 0;
954 	uint64_t towrite;
955 
956 	if (new_quota == 0)
957 		return (0);
958 
959 	mutex_enter(&dd->dd_lock);
960 	/*
961 	 * If we are doing the preliminary check in open context, and
962 	 * there are pending changes, then don't fail it, since the
963 	 * pending changes could under-estimate the amount of space to be
964 	 * freed up.
965 	 */
966 	towrite = dsl_dir_space_towrite(dd);
967 	if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
968 	    (new_quota < dd->dd_phys->dd_reserved ||
969 	    new_quota < dd->dd_used_bytes + towrite)) {
970 		err = ENOSPC;
971 	}
972 	mutex_exit(&dd->dd_lock);
973 	return (err);
974 }
975 
976 /* ARGSUSED */
977 static void
978 dsl_dir_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
979 {
980 	dsl_dir_t *dd = arg1;
981 	uint64_t *quotap = arg2;
982 	uint64_t new_quota = *quotap;
983 
984 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
985 
986 	mutex_enter(&dd->dd_lock);
987 	dd->dd_phys->dd_quota = new_quota;
988 	mutex_exit(&dd->dd_lock);
989 
990 	spa_history_internal_log(LOG_DS_QUOTA, dd->dd_pool->dp_spa,
991 	    tx, cr, "%lld dataset = %llu ",
992 	    (longlong_t)new_quota, dd->dd_phys->dd_head_dataset_obj);
993 }
994 
995 int
996 dsl_dir_set_quota(const char *ddname, uint64_t quota)
997 {
998 	dsl_dir_t *dd;
999 	int err;
1000 
1001 	err = dsl_dir_open(ddname, FTAG, &dd, NULL);
1002 	if (err)
1003 		return (err);
1004 
1005 	if (quota != dd->dd_phys->dd_quota) {
1006 		/*
1007 		 * If someone removes a file, then tries to set the quota, we
1008 		 * want to make sure the file freeing takes effect.
1009 		 */
1010 		txg_wait_open(dd->dd_pool, 0);
1011 
1012 		err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_quota_check,
1013 		    dsl_dir_set_quota_sync, dd, &quota, 0);
1014 	}
1015 	dsl_dir_close(dd, FTAG);
1016 	return (err);
1017 }
1018 
1019 int
1020 dsl_dir_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
1021 {
1022 	dsl_dir_t *dd = arg1;
1023 	uint64_t *reservationp = arg2;
1024 	uint64_t new_reservation = *reservationp;
1025 	uint64_t used, avail;
1026 	int64_t delta;
1027 
1028 	if (new_reservation > INT64_MAX)
1029 		return (EOVERFLOW);
1030 
1031 	/*
1032 	 * If we are doing the preliminary check in open context, the
1033 	 * space estimates may be inaccurate.
1034 	 */
1035 	if (!dmu_tx_is_syncing(tx))
1036 		return (0);
1037 
1038 	mutex_enter(&dd->dd_lock);
1039 	used = dd->dd_used_bytes;
1040 	delta = MAX(used, new_reservation) -
1041 	    MAX(used, dd->dd_phys->dd_reserved);
1042 	mutex_exit(&dd->dd_lock);
1043 
1044 	if (dd->dd_parent) {
1045 		avail = dsl_dir_space_available(dd->dd_parent,
1046 		    NULL, 0, FALSE);
1047 	} else {
1048 		avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1049 	}
1050 
1051 	if (delta > 0 && delta > avail)
1052 		return (ENOSPC);
1053 	if (delta > 0 && dd->dd_phys->dd_quota > 0 &&
1054 	    new_reservation > dd->dd_phys->dd_quota)
1055 		return (ENOSPC);
1056 	return (0);
1057 }
1058 
1059 /* ARGSUSED */
1060 static void
1061 dsl_dir_set_reservation_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1062 {
1063 	dsl_dir_t *dd = arg1;
1064 	uint64_t *reservationp = arg2;
1065 	uint64_t new_reservation = *reservationp;
1066 	uint64_t used;
1067 	int64_t delta;
1068 
1069 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1070 
1071 	mutex_enter(&dd->dd_lock);
1072 	used = dd->dd_used_bytes;
1073 	delta = MAX(used, new_reservation) -
1074 	    MAX(used, dd->dd_phys->dd_reserved);
1075 	dd->dd_phys->dd_reserved = new_reservation;
1076 	mutex_exit(&dd->dd_lock);
1077 
1078 	if (dd->dd_parent != NULL) {
1079 		/* Roll up this additional usage into our ancestors */
1080 		dsl_dir_diduse_space(dd->dd_parent, delta, 0, 0, tx);
1081 	}
1082 
1083 	spa_history_internal_log(LOG_DS_RESERVATION, dd->dd_pool->dp_spa,
1084 	    tx, cr, "%lld dataset = %llu",
1085 	    (longlong_t)new_reservation, dd->dd_phys->dd_head_dataset_obj);
1086 }
1087 
1088 int
1089 dsl_dir_set_reservation(const char *ddname, uint64_t reservation)
1090 {
1091 	dsl_dir_t *dd;
1092 	int err;
1093 
1094 	err = dsl_dir_open(ddname, FTAG, &dd, NULL);
1095 	if (err)
1096 		return (err);
1097 	err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_reservation_check,
1098 	    dsl_dir_set_reservation_sync, dd, &reservation, 0);
1099 	dsl_dir_close(dd, FTAG);
1100 	return (err);
1101 }
1102 
1103 static dsl_dir_t *
1104 closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1105 {
1106 	for (; ds1; ds1 = ds1->dd_parent) {
1107 		dsl_dir_t *dd;
1108 		for (dd = ds2; dd; dd = dd->dd_parent) {
1109 			if (ds1 == dd)
1110 				return (dd);
1111 		}
1112 	}
1113 	return (NULL);
1114 }
1115 
1116 /*
1117  * If delta is applied to dd, how much of that delta would be applied to
1118  * ancestor?  Syncing context only.
1119  */
1120 static int64_t
1121 would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1122 {
1123 	if (dd == ancestor)
1124 		return (delta);
1125 
1126 	mutex_enter(&dd->dd_lock);
1127 	delta = parent_delta(dd, dd->dd_used_bytes, delta);
1128 	mutex_exit(&dd->dd_lock);
1129 	return (would_change(dd->dd_parent, delta, ancestor));
1130 }
1131 
1132 struct renamearg {
1133 	dsl_dir_t *newparent;
1134 	const char *mynewname;
1135 };
1136 
1137 /*ARGSUSED*/
1138 static int
1139 dsl_dir_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
1140 {
1141 	dsl_dir_t *dd = arg1;
1142 	struct renamearg *ra = arg2;
1143 	dsl_pool_t *dp = dd->dd_pool;
1144 	objset_t *mos = dp->dp_meta_objset;
1145 	int err;
1146 	uint64_t val;
1147 
1148 	/* There should be 2 references: the open and the dirty */
1149 	if (dmu_buf_refcount(dd->dd_dbuf) > 2)
1150 		return (EBUSY);
1151 
1152 	/* check for existing name */
1153 	err = zap_lookup(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
1154 	    ra->mynewname, 8, 1, &val);
1155 	if (err == 0)
1156 		return (EEXIST);
1157 	if (err != ENOENT)
1158 		return (err);
1159 
1160 	if (ra->newparent != dd->dd_parent) {
1161 		/* is there enough space? */
1162 		uint64_t myspace =
1163 		    MAX(dd->dd_used_bytes, dd->dd_phys->dd_reserved);
1164 
1165 		/* no rename into our descendant */
1166 		if (closest_common_ancestor(dd, ra->newparent) == dd)
1167 			return (EINVAL);
1168 
1169 		if (err = dsl_dir_transfer_possible(dd->dd_parent,
1170 		    ra->newparent, myspace))
1171 			return (err);
1172 	}
1173 
1174 	return (0);
1175 }
1176 
1177 static void
1178 dsl_dir_rename_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1179 {
1180 	dsl_dir_t *dd = arg1;
1181 	struct renamearg *ra = arg2;
1182 	dsl_pool_t *dp = dd->dd_pool;
1183 	objset_t *mos = dp->dp_meta_objset;
1184 	int err;
1185 
1186 	ASSERT(dmu_buf_refcount(dd->dd_dbuf) <= 2);
1187 
1188 	if (ra->newparent != dd->dd_parent) {
1189 		uint64_t myspace =
1190 		    MAX(dd->dd_used_bytes, dd->dd_phys->dd_reserved);
1191 
1192 		dsl_dir_diduse_space(dd->dd_parent, -myspace,
1193 		    -dd->dd_phys->dd_compressed_bytes,
1194 		    -dd->dd_phys->dd_uncompressed_bytes, tx);
1195 		dsl_dir_diduse_space(ra->newparent, myspace,
1196 		    dd->dd_phys->dd_compressed_bytes,
1197 		    dd->dd_phys->dd_uncompressed_bytes, tx);
1198 	}
1199 
1200 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1201 
1202 	/* remove from old parent zapobj */
1203 	err = zap_remove(mos, dd->dd_parent->dd_phys->dd_child_dir_zapobj,
1204 	    dd->dd_myname, tx);
1205 	ASSERT3U(err, ==, 0);
1206 
1207 	(void) strcpy(dd->dd_myname, ra->mynewname);
1208 	dsl_dir_close(dd->dd_parent, dd);
1209 	dd->dd_phys->dd_parent_obj = ra->newparent->dd_object;
1210 	VERIFY(0 == dsl_dir_open_obj(dd->dd_pool,
1211 	    ra->newparent->dd_object, NULL, dd, &dd->dd_parent));
1212 
1213 	/* add to new parent zapobj */
1214 	err = zap_add(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
1215 	    dd->dd_myname, 8, 1, &dd->dd_object, tx);
1216 	ASSERT3U(err, ==, 0);
1217 
1218 	spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa,
1219 	    tx, cr, "dataset = %llu", dd->dd_phys->dd_head_dataset_obj);
1220 }
1221 
1222 int
1223 dsl_dir_rename(dsl_dir_t *dd, const char *newname)
1224 {
1225 	struct renamearg ra;
1226 	int err;
1227 
1228 	/* new parent should exist */
1229 	err = dsl_dir_open(newname, FTAG, &ra.newparent, &ra.mynewname);
1230 	if (err)
1231 		return (err);
1232 
1233 	/* can't rename to different pool */
1234 	if (dd->dd_pool != ra.newparent->dd_pool) {
1235 		err = ENXIO;
1236 		goto out;
1237 	}
1238 
1239 	/* new name should not already exist */
1240 	if (ra.mynewname == NULL) {
1241 		err = EEXIST;
1242 		goto out;
1243 	}
1244 
1245 	err = dsl_sync_task_do(dd->dd_pool,
1246 	    dsl_dir_rename_check, dsl_dir_rename_sync, dd, &ra, 3);
1247 
1248 out:
1249 	dsl_dir_close(ra.newparent, FTAG);
1250 	return (err);
1251 }
1252 
1253 int
1254 dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, uint64_t space)
1255 {
1256 	dsl_dir_t *ancestor;
1257 	int64_t adelta;
1258 	uint64_t avail;
1259 
1260 	ancestor = closest_common_ancestor(sdd, tdd);
1261 	adelta = would_change(sdd, -space, ancestor);
1262 	avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
1263 	if (avail < space)
1264 		return (ENOSPC);
1265 
1266 	return (0);
1267 }
1268