xref: /titanic_52/usr/src/uts/common/fs/zfs/dsl_dir.c (revision c1ecd8b9404ee0d96d93f02e82c441b9bb149a3d)
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_pool_t *dp, dsl_dir_t *pds, const char *name,
410     dmu_tx_t *tx)
411 {
412 	objset_t *mos = dp->dp_meta_objset;
413 	uint64_t ddobj;
414 	dsl_dir_phys_t *dsphys;
415 	dmu_buf_t *dbuf;
416 
417 	ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
418 	    DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
419 	if (pds) {
420 		VERIFY(0 == zap_add(mos, pds->dd_phys->dd_child_dir_zapobj,
421 		    name, sizeof (uint64_t), 1, &ddobj, tx));
422 	} else {
423 		/* it's the root dir */
424 		VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
425 		    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
426 	}
427 	VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
428 	dmu_buf_will_dirty(dbuf, tx);
429 	dsphys = dbuf->db_data;
430 
431 	dsphys->dd_creation_time = gethrestime_sec();
432 	if (pds)
433 		dsphys->dd_parent_obj = pds->dd_object;
434 	dsphys->dd_props_zapobj = zap_create(mos,
435 	    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
436 	dsphys->dd_child_dir_zapobj = zap_create(mos,
437 	    DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
438 	dmu_buf_rele(dbuf, FTAG);
439 
440 	return (ddobj);
441 }
442 
443 /* ARGSUSED */
444 int
445 dsl_dir_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
446 {
447 	dsl_dir_t *dd = arg1;
448 	dsl_pool_t *dp = dd->dd_pool;
449 	objset_t *mos = dp->dp_meta_objset;
450 	int err;
451 	uint64_t count;
452 
453 	/*
454 	 * There should be exactly two holds, both from
455 	 * dsl_dataset_destroy: one on the dd directory, and one on its
456 	 * head ds.  Otherwise, someone is trying to lookup something
457 	 * inside this dir while we want to destroy it.  The
458 	 * config_rwlock ensures that nobody else opens it after we
459 	 * check.
460 	 */
461 	if (dmu_buf_refcount(dd->dd_dbuf) > 2)
462 		return (EBUSY);
463 
464 	err = zap_count(mos, dd->dd_phys->dd_child_dir_zapobj, &count);
465 	if (err)
466 		return (err);
467 	if (count != 0)
468 		return (EEXIST);
469 
470 	return (0);
471 }
472 
473 void
474 dsl_dir_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx)
475 {
476 	dsl_dir_t *dd = arg1;
477 	objset_t *mos = dd->dd_pool->dp_meta_objset;
478 	uint64_t val, obj;
479 
480 	ASSERT(RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock));
481 	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
482 
483 	/* Remove our reservation. */
484 	val = 0;
485 	dsl_dir_set_reservation_sync(dd, &val, cr, tx);
486 	ASSERT3U(dd->dd_used_bytes, ==, 0);
487 	ASSERT3U(dd->dd_phys->dd_reserved, ==, 0);
488 
489 	VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
490 	VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
491 	VERIFY(0 == dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx));
492 	VERIFY(0 == zap_remove(mos,
493 	    dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
494 
495 	obj = dd->dd_object;
496 	dsl_dir_close(dd, tag);
497 	VERIFY(0 == dmu_object_free(mos, obj, tx));
498 }
499 
500 boolean_t
501 dsl_dir_is_clone(dsl_dir_t *dd)
502 {
503 	return (dd->dd_phys->dd_origin_obj &&
504 	    (dd->dd_pool->dp_origin_snap == NULL ||
505 	    dd->dd_phys->dd_origin_obj !=
506 	    dd->dd_pool->dp_origin_snap->ds_object));
507 }
508 
509 void
510 dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
511 {
512 	mutex_enter(&dd->dd_lock);
513 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, dd->dd_used_bytes);
514 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA, dd->dd_phys->dd_quota);
515 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
516 	    dd->dd_phys->dd_reserved);
517 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
518 	    dd->dd_phys->dd_compressed_bytes == 0 ? 100 :
519 	    (dd->dd_phys->dd_uncompressed_bytes * 100 /
520 	    dd->dd_phys->dd_compressed_bytes));
521 	mutex_exit(&dd->dd_lock);
522 
523 	rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
524 	if (dsl_dir_is_clone(dd)) {
525 		dsl_dataset_t *ds;
526 		char buf[MAXNAMELEN];
527 
528 		VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
529 		    dd->dd_phys->dd_origin_obj, FTAG, &ds));
530 		dsl_dataset_name(ds, buf);
531 		dsl_dataset_rele(ds, FTAG);
532 		dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
533 	}
534 	rw_exit(&dd->dd_pool->dp_config_rwlock);
535 }
536 
537 void
538 dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
539 {
540 	dsl_pool_t *dp = dd->dd_pool;
541 
542 	ASSERT(dd->dd_phys);
543 
544 	if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg) == 0) {
545 		/* up the hold count until we can be written out */
546 		dmu_buf_add_ref(dd->dd_dbuf, dd);
547 	}
548 }
549 
550 static int64_t
551 parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
552 {
553 	uint64_t old_accounted = MAX(used, dd->dd_phys->dd_reserved);
554 	uint64_t new_accounted = MAX(used + delta, dd->dd_phys->dd_reserved);
555 	return (new_accounted - old_accounted);
556 }
557 
558 void
559 dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
560 {
561 	ASSERT(dmu_tx_is_syncing(tx));
562 
563 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
564 
565 	mutex_enter(&dd->dd_lock);
566 	ASSERT3U(dd->dd_tempreserved[tx->tx_txg&TXG_MASK], ==, 0);
567 	dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
568 	    dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
569 	dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
570 	dd->dd_phys->dd_used_bytes = dd->dd_used_bytes;
571 	mutex_exit(&dd->dd_lock);
572 
573 	/* release the hold from dsl_dir_dirty */
574 	dmu_buf_rele(dd->dd_dbuf, dd);
575 }
576 
577 static uint64_t
578 dsl_dir_space_towrite(dsl_dir_t *dd)
579 {
580 	uint64_t space = 0;
581 	int i;
582 
583 	ASSERT(MUTEX_HELD(&dd->dd_lock));
584 
585 	for (i = 0; i < TXG_SIZE; i++) {
586 		space += dd->dd_space_towrite[i&TXG_MASK];
587 		ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
588 	}
589 	return (space);
590 }
591 
592 /*
593  * How much space would dd have available if ancestor had delta applied
594  * to it?  If ondiskonly is set, we're only interested in what's
595  * on-disk, not estimated pending changes.
596  */
597 uint64_t
598 dsl_dir_space_available(dsl_dir_t *dd,
599     dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
600 {
601 	uint64_t parentspace, myspace, quota, used;
602 
603 	/*
604 	 * If there are no restrictions otherwise, assume we have
605 	 * unlimited space available.
606 	 */
607 	quota = UINT64_MAX;
608 	parentspace = UINT64_MAX;
609 
610 	if (dd->dd_parent != NULL) {
611 		parentspace = dsl_dir_space_available(dd->dd_parent,
612 		    ancestor, delta, ondiskonly);
613 	}
614 
615 	mutex_enter(&dd->dd_lock);
616 	if (dd->dd_phys->dd_quota != 0)
617 		quota = dd->dd_phys->dd_quota;
618 	used = dd->dd_used_bytes;
619 	if (!ondiskonly)
620 		used += dsl_dir_space_towrite(dd);
621 	if (dd == ancestor)
622 		used += delta;
623 
624 	if (dd->dd_parent == NULL) {
625 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
626 		quota = MIN(quota, poolsize);
627 	}
628 
629 	if (dd->dd_phys->dd_reserved > used && parentspace != UINT64_MAX) {
630 		/*
631 		 * We have some space reserved, in addition to what our
632 		 * parent gave us.
633 		 */
634 		parentspace += dd->dd_phys->dd_reserved - used;
635 	}
636 
637 	if (used > quota) {
638 		/* over quota */
639 		myspace = 0;
640 
641 		/*
642 		 * While it's OK to be a little over quota, if
643 		 * we think we are using more space than there
644 		 * is in the pool (which is already 1.6% more than
645 		 * dsl_pool_adjustedsize()), something is very
646 		 * wrong.
647 		 */
648 		ASSERT3U(used, <=, spa_get_space(dd->dd_pool->dp_spa));
649 	} else {
650 		/*
651 		 * the lesser of the space provided by our parent and
652 		 * the space left in our quota
653 		 */
654 		myspace = MIN(parentspace, quota - used);
655 	}
656 
657 	mutex_exit(&dd->dd_lock);
658 
659 	return (myspace);
660 }
661 
662 struct tempreserve {
663 	list_node_t tr_node;
664 	dsl_pool_t *tr_dp;
665 	dsl_dir_t *tr_ds;
666 	uint64_t tr_size;
667 };
668 
669 static int
670 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
671     boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
672     dmu_tx_t *tx, boolean_t first)
673 {
674 	uint64_t txg = tx->tx_txg;
675 	uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
676 	struct tempreserve *tr;
677 	int enospc = EDQUOT;
678 	int txgidx = txg & TXG_MASK;
679 	int i;
680 	uint64_t ref_rsrv = 0;
681 
682 	ASSERT3U(txg, !=, 0);
683 	ASSERT3S(asize, >, 0);
684 
685 	mutex_enter(&dd->dd_lock);
686 
687 	/*
688 	 * Check against the dsl_dir's quota.  We don't add in the delta
689 	 * when checking for over-quota because they get one free hit.
690 	 */
691 	est_inflight = dsl_dir_space_towrite(dd);
692 	for (i = 0; i < TXG_SIZE; i++)
693 		est_inflight += dd->dd_tempreserved[i];
694 	used_on_disk = dd->dd_used_bytes;
695 
696 	/*
697 	 * On the first iteration, fetch the dataset's used-on-disk and
698 	 * refreservation values. Also, if checkrefquota is set, test if
699 	 * allocating this space would exceed the dataset's refquota.
700 	 */
701 	if (first && tx->tx_objset) {
702 		int error;
703 		dsl_dataset_t *ds = tx->tx_objset->os->os_dsl_dataset;
704 
705 		error = dsl_dataset_check_quota(ds, checkrefquota,
706 		    asize, est_inflight, &used_on_disk, &ref_rsrv);
707 		if (error) {
708 			mutex_exit(&dd->dd_lock);
709 			return (error);
710 		}
711 	}
712 
713 	/*
714 	 * If this transaction will result in a net free of space,
715 	 * we want to let it through.
716 	 */
717 	if (ignorequota || netfree || dd->dd_phys->dd_quota == 0)
718 		quota = UINT64_MAX;
719 	else
720 		quota = dd->dd_phys->dd_quota;
721 
722 	/*
723 	 * Adjust the quota against the actual pool size at the root.
724 	 * To ensure that it's possible to remove files from a full
725 	 * pool without inducing transient overcommits, we throttle
726 	 * netfree transactions against a quota that is slightly larger,
727 	 * but still within the pool's allocation slop.  In cases where
728 	 * we're very close to full, this will allow a steady trickle of
729 	 * removes to get through.
730 	 */
731 	if (dd->dd_parent == NULL) {
732 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
733 		if (poolsize < quota) {
734 			quota = poolsize;
735 			enospc = ENOSPC;
736 		}
737 	}
738 
739 	/*
740 	 * If they are requesting more space, and our current estimate
741 	 * is over quota, they get to try again unless the actual
742 	 * on-disk is over quota and there are no pending changes (which
743 	 * may free up space for us).
744 	 */
745 	if (used_on_disk + est_inflight > quota) {
746 		if (est_inflight > 0 || used_on_disk < quota)
747 			enospc = ERESTART;
748 		dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
749 		    "quota=%lluK tr=%lluK err=%d\n",
750 		    used_on_disk>>10, est_inflight>>10,
751 		    quota>>10, asize>>10, enospc);
752 		mutex_exit(&dd->dd_lock);
753 		return (enospc);
754 	}
755 
756 	/* We need to up our estimated delta before dropping dd_lock */
757 	dd->dd_tempreserved[txgidx] += asize;
758 
759 	parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
760 	    asize - ref_rsrv);
761 	mutex_exit(&dd->dd_lock);
762 
763 	tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
764 	tr->tr_ds = dd;
765 	tr->tr_size = asize;
766 	list_insert_tail(tr_list, tr);
767 
768 	/* see if it's OK with our parent */
769 	if (dd->dd_parent && parent_rsrv) {
770 		boolean_t ismos = (dd->dd_phys->dd_head_dataset_obj == 0);
771 
772 		return (dsl_dir_tempreserve_impl(dd->dd_parent,
773 		    parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE));
774 	} else {
775 		return (0);
776 	}
777 }
778 
779 /*
780  * Reserve space in this dsl_dir, to be used in this tx's txg.
781  * After the space has been dirtied (and dsl_dir_willuse_space()
782  * has been called), the reservation should be canceled, using
783  * dsl_dir_tempreserve_clear().
784  */
785 int
786 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
787     uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
788 {
789 	int err;
790 	list_t *tr_list;
791 
792 	if (asize == 0) {
793 		*tr_cookiep = NULL;
794 		return (0);
795 	}
796 
797 	tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
798 	list_create(tr_list, sizeof (struct tempreserve),
799 	    offsetof(struct tempreserve, tr_node));
800 	ASSERT3S(asize, >, 0);
801 	ASSERT3S(fsize, >=, 0);
802 
803 	err = arc_tempreserve_space(lsize, tx->tx_txg);
804 	if (err == 0) {
805 		struct tempreserve *tr;
806 
807 		tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
808 		tr->tr_size = lsize;
809 		list_insert_tail(tr_list, tr);
810 
811 		err = dsl_pool_tempreserve_space(dd->dd_pool, asize, tx);
812 	} else {
813 		if (err == EAGAIN) {
814 			txg_delay(dd->dd_pool, tx->tx_txg, 1);
815 			err = ERESTART;
816 		}
817 		dsl_pool_memory_pressure(dd->dd_pool);
818 	}
819 
820 	if (err == 0) {
821 		struct tempreserve *tr;
822 
823 		tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
824 		tr->tr_dp = dd->dd_pool;
825 		tr->tr_size = asize;
826 		list_insert_tail(tr_list, tr);
827 
828 		err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
829 		    FALSE, asize > usize, tr_list, tx, TRUE);
830 	}
831 
832 	if (err)
833 		dsl_dir_tempreserve_clear(tr_list, tx);
834 	else
835 		*tr_cookiep = tr_list;
836 
837 	return (err);
838 }
839 
840 /*
841  * Clear a temporary reservation that we previously made with
842  * dsl_dir_tempreserve_space().
843  */
844 void
845 dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
846 {
847 	int txgidx = tx->tx_txg & TXG_MASK;
848 	list_t *tr_list = tr_cookie;
849 	struct tempreserve *tr;
850 
851 	ASSERT3U(tx->tx_txg, !=, 0);
852 
853 	if (tr_cookie == NULL)
854 		return;
855 
856 	while (tr = list_head(tr_list)) {
857 		if (tr->tr_dp) {
858 			dsl_pool_tempreserve_clear(tr->tr_dp, tr->tr_size, tx);
859 		} else if (tr->tr_ds) {
860 			mutex_enter(&tr->tr_ds->dd_lock);
861 			ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
862 			    tr->tr_size);
863 			tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
864 			mutex_exit(&tr->tr_ds->dd_lock);
865 		} else {
866 			arc_tempreserve_clear(tr->tr_size);
867 		}
868 		list_remove(tr_list, tr);
869 		kmem_free(tr, sizeof (struct tempreserve));
870 	}
871 
872 	kmem_free(tr_list, sizeof (list_t));
873 }
874 
875 static void
876 dsl_dir_willuse_space_impl(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
877 {
878 	int64_t parent_space;
879 	uint64_t est_used;
880 
881 	mutex_enter(&dd->dd_lock);
882 	if (space > 0)
883 		dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
884 
885 	est_used = dsl_dir_space_towrite(dd) + dd->dd_used_bytes;
886 	parent_space = parent_delta(dd, est_used, space);
887 	mutex_exit(&dd->dd_lock);
888 
889 	/* Make sure that we clean up dd_space_to* */
890 	dsl_dir_dirty(dd, tx);
891 
892 	/* XXX this is potentially expensive and unnecessary... */
893 	if (parent_space && dd->dd_parent)
894 		dsl_dir_willuse_space_impl(dd->dd_parent, parent_space, tx);
895 }
896 
897 /*
898  * Call in open context when we think we're going to write/free space,
899  * eg. when dirtying data.  Be conservative (ie. OK to write less than
900  * this or free more than this, but don't write more or free less).
901  */
902 void
903 dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
904 {
905 	dsl_pool_willuse_space(dd->dd_pool, space, tx);
906 	dsl_dir_willuse_space_impl(dd, space, tx);
907 }
908 
909 /* call from syncing context when we actually write/free space for this dd */
910 void
911 dsl_dir_diduse_space(dsl_dir_t *dd,
912     int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
913 {
914 	int64_t accounted_delta;
915 
916 	ASSERT(dmu_tx_is_syncing(tx));
917 
918 	dsl_dir_dirty(dd, tx);
919 
920 	mutex_enter(&dd->dd_lock);
921 	accounted_delta = parent_delta(dd, dd->dd_used_bytes, used);
922 	ASSERT(used >= 0 || dd->dd_used_bytes >= -used);
923 	ASSERT(compressed >= 0 ||
924 	    dd->dd_phys->dd_compressed_bytes >= -compressed);
925 	ASSERT(uncompressed >= 0 ||
926 	    dd->dd_phys->dd_uncompressed_bytes >= -uncompressed);
927 	dd->dd_used_bytes += used;
928 	dd->dd_phys->dd_uncompressed_bytes += uncompressed;
929 	dd->dd_phys->dd_compressed_bytes += compressed;
930 	mutex_exit(&dd->dd_lock);
931 
932 	if (dd->dd_parent != NULL) {
933 		dsl_dir_diduse_space(dd->dd_parent,
934 		    accounted_delta, compressed, uncompressed, tx);
935 	}
936 }
937 
938 static int
939 dsl_dir_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
940 {
941 	dsl_dir_t *dd = arg1;
942 	uint64_t *quotap = arg2;
943 	uint64_t new_quota = *quotap;
944 	int err = 0;
945 	uint64_t towrite;
946 
947 	if (new_quota == 0)
948 		return (0);
949 
950 	mutex_enter(&dd->dd_lock);
951 	/*
952 	 * If we are doing the preliminary check in open context, and
953 	 * there are pending changes, then don't fail it, since the
954 	 * pending changes could under-estimate the amount of space to be
955 	 * freed up.
956 	 */
957 	towrite = dsl_dir_space_towrite(dd);
958 	if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
959 	    (new_quota < dd->dd_phys->dd_reserved ||
960 	    new_quota < dd->dd_used_bytes + towrite)) {
961 		err = ENOSPC;
962 	}
963 	mutex_exit(&dd->dd_lock);
964 	return (err);
965 }
966 
967 /* ARGSUSED */
968 static void
969 dsl_dir_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
970 {
971 	dsl_dir_t *dd = arg1;
972 	uint64_t *quotap = arg2;
973 	uint64_t new_quota = *quotap;
974 
975 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
976 
977 	mutex_enter(&dd->dd_lock);
978 	dd->dd_phys->dd_quota = new_quota;
979 	mutex_exit(&dd->dd_lock);
980 
981 	spa_history_internal_log(LOG_DS_QUOTA, dd->dd_pool->dp_spa,
982 	    tx, cr, "%lld dataset = %llu ",
983 	    (longlong_t)new_quota, dd->dd_phys->dd_head_dataset_obj);
984 }
985 
986 int
987 dsl_dir_set_quota(const char *ddname, uint64_t quota)
988 {
989 	dsl_dir_t *dd;
990 	int err;
991 
992 	err = dsl_dir_open(ddname, FTAG, &dd, NULL);
993 	if (err)
994 		return (err);
995 
996 	if (quota != dd->dd_phys->dd_quota) {
997 		/*
998 		 * If someone removes a file, then tries to set the quota, we
999 		 * want to make sure the file freeing takes effect.
1000 		 */
1001 		txg_wait_open(dd->dd_pool, 0);
1002 
1003 		err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_quota_check,
1004 		    dsl_dir_set_quota_sync, dd, &quota, 0);
1005 	}
1006 	dsl_dir_close(dd, FTAG);
1007 	return (err);
1008 }
1009 
1010 int
1011 dsl_dir_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
1012 {
1013 	dsl_dir_t *dd = arg1;
1014 	uint64_t *reservationp = arg2;
1015 	uint64_t new_reservation = *reservationp;
1016 	uint64_t used, avail;
1017 	int64_t delta;
1018 
1019 	if (new_reservation > INT64_MAX)
1020 		return (EOVERFLOW);
1021 
1022 	/*
1023 	 * If we are doing the preliminary check in open context, the
1024 	 * space estimates may be inaccurate.
1025 	 */
1026 	if (!dmu_tx_is_syncing(tx))
1027 		return (0);
1028 
1029 	mutex_enter(&dd->dd_lock);
1030 	used = dd->dd_used_bytes;
1031 	delta = MAX(used, new_reservation) -
1032 	    MAX(used, dd->dd_phys->dd_reserved);
1033 	mutex_exit(&dd->dd_lock);
1034 
1035 	if (dd->dd_parent) {
1036 		avail = dsl_dir_space_available(dd->dd_parent,
1037 		    NULL, 0, FALSE);
1038 	} else {
1039 		avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1040 	}
1041 
1042 	if (delta > 0 && delta > avail)
1043 		return (ENOSPC);
1044 	if (delta > 0 && dd->dd_phys->dd_quota > 0 &&
1045 	    new_reservation > dd->dd_phys->dd_quota)
1046 		return (ENOSPC);
1047 	return (0);
1048 }
1049 
1050 /* ARGSUSED */
1051 static void
1052 dsl_dir_set_reservation_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1053 {
1054 	dsl_dir_t *dd = arg1;
1055 	uint64_t *reservationp = arg2;
1056 	uint64_t new_reservation = *reservationp;
1057 	uint64_t used;
1058 	int64_t delta;
1059 
1060 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1061 
1062 	mutex_enter(&dd->dd_lock);
1063 	used = dd->dd_used_bytes;
1064 	delta = MAX(used, new_reservation) -
1065 	    MAX(used, dd->dd_phys->dd_reserved);
1066 	dd->dd_phys->dd_reserved = new_reservation;
1067 	mutex_exit(&dd->dd_lock);
1068 
1069 	if (dd->dd_parent != NULL) {
1070 		/* Roll up this additional usage into our ancestors */
1071 		dsl_dir_diduse_space(dd->dd_parent, delta, 0, 0, tx);
1072 	}
1073 
1074 	spa_history_internal_log(LOG_DS_RESERVATION, dd->dd_pool->dp_spa,
1075 	    tx, cr, "%lld dataset = %llu",
1076 	    (longlong_t)new_reservation, dd->dd_phys->dd_head_dataset_obj);
1077 }
1078 
1079 int
1080 dsl_dir_set_reservation(const char *ddname, uint64_t reservation)
1081 {
1082 	dsl_dir_t *dd;
1083 	int err;
1084 
1085 	err = dsl_dir_open(ddname, FTAG, &dd, NULL);
1086 	if (err)
1087 		return (err);
1088 	err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_reservation_check,
1089 	    dsl_dir_set_reservation_sync, dd, &reservation, 0);
1090 	dsl_dir_close(dd, FTAG);
1091 	return (err);
1092 }
1093 
1094 static dsl_dir_t *
1095 closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1096 {
1097 	for (; ds1; ds1 = ds1->dd_parent) {
1098 		dsl_dir_t *dd;
1099 		for (dd = ds2; dd; dd = dd->dd_parent) {
1100 			if (ds1 == dd)
1101 				return (dd);
1102 		}
1103 	}
1104 	return (NULL);
1105 }
1106 
1107 /*
1108  * If delta is applied to dd, how much of that delta would be applied to
1109  * ancestor?  Syncing context only.
1110  */
1111 static int64_t
1112 would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1113 {
1114 	if (dd == ancestor)
1115 		return (delta);
1116 
1117 	mutex_enter(&dd->dd_lock);
1118 	delta = parent_delta(dd, dd->dd_used_bytes, delta);
1119 	mutex_exit(&dd->dd_lock);
1120 	return (would_change(dd->dd_parent, delta, ancestor));
1121 }
1122 
1123 struct renamearg {
1124 	dsl_dir_t *newparent;
1125 	const char *mynewname;
1126 };
1127 
1128 /*ARGSUSED*/
1129 static int
1130 dsl_dir_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
1131 {
1132 	dsl_dir_t *dd = arg1;
1133 	struct renamearg *ra = arg2;
1134 	dsl_pool_t *dp = dd->dd_pool;
1135 	objset_t *mos = dp->dp_meta_objset;
1136 	int err;
1137 	uint64_t val;
1138 
1139 	/* There should be 2 references: the open and the dirty */
1140 	if (dmu_buf_refcount(dd->dd_dbuf) > 2)
1141 		return (EBUSY);
1142 
1143 	/* check for existing name */
1144 	err = zap_lookup(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
1145 	    ra->mynewname, 8, 1, &val);
1146 	if (err == 0)
1147 		return (EEXIST);
1148 	if (err != ENOENT)
1149 		return (err);
1150 
1151 	if (ra->newparent != dd->dd_parent) {
1152 		/* is there enough space? */
1153 		uint64_t myspace =
1154 		    MAX(dd->dd_used_bytes, dd->dd_phys->dd_reserved);
1155 
1156 		/* no rename into our descendant */
1157 		if (closest_common_ancestor(dd, ra->newparent) == dd)
1158 			return (EINVAL);
1159 
1160 		if (err = dsl_dir_transfer_possible(dd->dd_parent,
1161 		    ra->newparent, myspace))
1162 			return (err);
1163 	}
1164 
1165 	return (0);
1166 }
1167 
1168 static void
1169 dsl_dir_rename_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1170 {
1171 	dsl_dir_t *dd = arg1;
1172 	struct renamearg *ra = arg2;
1173 	dsl_pool_t *dp = dd->dd_pool;
1174 	objset_t *mos = dp->dp_meta_objset;
1175 	int err;
1176 
1177 	ASSERT(dmu_buf_refcount(dd->dd_dbuf) <= 2);
1178 
1179 	if (ra->newparent != dd->dd_parent) {
1180 		uint64_t myspace =
1181 		    MAX(dd->dd_used_bytes, dd->dd_phys->dd_reserved);
1182 
1183 		dsl_dir_diduse_space(dd->dd_parent, -myspace,
1184 		    -dd->dd_phys->dd_compressed_bytes,
1185 		    -dd->dd_phys->dd_uncompressed_bytes, tx);
1186 		dsl_dir_diduse_space(ra->newparent, myspace,
1187 		    dd->dd_phys->dd_compressed_bytes,
1188 		    dd->dd_phys->dd_uncompressed_bytes, tx);
1189 	}
1190 
1191 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1192 
1193 	/* remove from old parent zapobj */
1194 	err = zap_remove(mos, dd->dd_parent->dd_phys->dd_child_dir_zapobj,
1195 	    dd->dd_myname, tx);
1196 	ASSERT3U(err, ==, 0);
1197 
1198 	(void) strcpy(dd->dd_myname, ra->mynewname);
1199 	dsl_dir_close(dd->dd_parent, dd);
1200 	dd->dd_phys->dd_parent_obj = ra->newparent->dd_object;
1201 	VERIFY(0 == dsl_dir_open_obj(dd->dd_pool,
1202 	    ra->newparent->dd_object, NULL, dd, &dd->dd_parent));
1203 
1204 	/* add to new parent zapobj */
1205 	err = zap_add(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
1206 	    dd->dd_myname, 8, 1, &dd->dd_object, tx);
1207 	ASSERT3U(err, ==, 0);
1208 
1209 	spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa,
1210 	    tx, cr, "dataset = %llu", dd->dd_phys->dd_head_dataset_obj);
1211 }
1212 
1213 int
1214 dsl_dir_rename(dsl_dir_t *dd, const char *newname)
1215 {
1216 	struct renamearg ra;
1217 	int err;
1218 
1219 	/* new parent should exist */
1220 	err = dsl_dir_open(newname, FTAG, &ra.newparent, &ra.mynewname);
1221 	if (err)
1222 		return (err);
1223 
1224 	/* can't rename to different pool */
1225 	if (dd->dd_pool != ra.newparent->dd_pool) {
1226 		err = ENXIO;
1227 		goto out;
1228 	}
1229 
1230 	/* new name should not already exist */
1231 	if (ra.mynewname == NULL) {
1232 		err = EEXIST;
1233 		goto out;
1234 	}
1235 
1236 	err = dsl_sync_task_do(dd->dd_pool,
1237 	    dsl_dir_rename_check, dsl_dir_rename_sync, dd, &ra, 3);
1238 
1239 out:
1240 	dsl_dir_close(ra.newparent, FTAG);
1241 	return (err);
1242 }
1243 
1244 int
1245 dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, uint64_t space)
1246 {
1247 	dsl_dir_t *ancestor;
1248 	int64_t adelta;
1249 	uint64_t avail;
1250 
1251 	ancestor = closest_common_ancestor(sdd, tdd);
1252 	adelta = would_change(sdd, -space, ancestor);
1253 	avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
1254 	if (avail < space)
1255 		return (ENOSPC);
1256 
1257 	return (0);
1258 }
1259