xref: /onnv-gate/usr/src/uts/common/fs/zfs/dsl_dir.c (revision 10373:bcf97ee54990)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
228525SEric.Schrock@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <sys/dmu.h>
275378Sck153898 #include <sys/dmu_objset.h>
28789Sahrens #include <sys/dmu_tx.h>
29789Sahrens #include <sys/dsl_dataset.h>
30789Sahrens #include <sys/dsl_dir.h>
31789Sahrens #include <sys/dsl_prop.h>
322199Sahrens #include <sys/dsl_synctask.h>
334543Smarks #include <sys/dsl_deleg.h>
34789Sahrens #include <sys/spa.h>
35789Sahrens #include <sys/zap.h>
36789Sahrens #include <sys/zio.h>
37789Sahrens #include <sys/arc.h>
384543Smarks #include <sys/sunddi.h>
39789Sahrens #include "zfs_namecheck.h"
40789Sahrens 
415378Sck153898 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
424543Smarks static void dsl_dir_set_reservation_sync(void *arg1, void *arg2,
434543Smarks     cred_t *cr, dmu_tx_t *tx);
44789Sahrens 
45789Sahrens 
46789Sahrens /* ARGSUSED */
47789Sahrens static void
48789Sahrens dsl_dir_evict(dmu_buf_t *db, void *arg)
49789Sahrens {
50789Sahrens 	dsl_dir_t *dd = arg;
51789Sahrens 	dsl_pool_t *dp = dd->dd_pool;
52789Sahrens 	int t;
53789Sahrens 
54789Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
55789Sahrens 		ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
56789Sahrens 		ASSERT(dd->dd_tempreserved[t] == 0);
57789Sahrens 		ASSERT(dd->dd_space_towrite[t] == 0);
58789Sahrens 	}
59789Sahrens 
60789Sahrens 	if (dd->dd_parent)
61789Sahrens 		dsl_dir_close(dd->dd_parent, dd);
62789Sahrens 
63789Sahrens 	spa_close(dd->dd_pool->dp_spa, dd);
64789Sahrens 
65789Sahrens 	/*
66789Sahrens 	 * The props callback list should be empty since they hold the
67789Sahrens 	 * dir open.
68789Sahrens 	 */
69789Sahrens 	list_destroy(&dd->dd_prop_cbs);
702856Snd150628 	mutex_destroy(&dd->dd_lock);
71789Sahrens 	kmem_free(dd, sizeof (dsl_dir_t));
72789Sahrens }
73789Sahrens 
741544Seschrock int
75789Sahrens dsl_dir_open_obj(dsl_pool_t *dp, uint64_t ddobj,
761544Seschrock     const char *tail, void *tag, dsl_dir_t **ddp)
77789Sahrens {
78789Sahrens 	dmu_buf_t *dbuf;
79789Sahrens 	dsl_dir_t *dd;
801544Seschrock 	int err;
81789Sahrens 
82789Sahrens 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
83789Sahrens 	    dsl_pool_sync_context(dp));
84789Sahrens 
851544Seschrock 	err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
861544Seschrock 	if (err)
871544Seschrock 		return (err);
88789Sahrens 	dd = dmu_buf_get_user(dbuf);
89789Sahrens #ifdef ZFS_DEBUG
90789Sahrens 	{
91789Sahrens 		dmu_object_info_t doi;
92789Sahrens 		dmu_object_info_from_db(dbuf, &doi);
93928Stabriz 		ASSERT3U(doi.doi_type, ==, DMU_OT_DSL_DIR);
947390SMatthew.Ahrens@Sun.COM 		ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
95789Sahrens 	}
96789Sahrens #endif
97789Sahrens 	if (dd == NULL) {
98789Sahrens 		dsl_dir_t *winner;
99789Sahrens 
100789Sahrens 		dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
101789Sahrens 		dd->dd_object = ddobj;
102789Sahrens 		dd->dd_dbuf = dbuf;
103789Sahrens 		dd->dd_pool = dp;
104789Sahrens 		dd->dd_phys = dbuf->db_data;
1052856Snd150628 		mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
106789Sahrens 
107789Sahrens 		list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t),
108789Sahrens 		    offsetof(dsl_prop_cb_record_t, cbr_node));
109789Sahrens 
110*10373Schris.kirby@sun.com 		dsl_dir_snap_cmtime_update(dd);
111*10373Schris.kirby@sun.com 
112789Sahrens 		if (dd->dd_phys->dd_parent_obj) {
1131544Seschrock 			err = dsl_dir_open_obj(dp, dd->dd_phys->dd_parent_obj,
1141544Seschrock 			    NULL, dd, &dd->dd_parent);
1157390SMatthew.Ahrens@Sun.COM 			if (err)
1167390SMatthew.Ahrens@Sun.COM 				goto errout;
117789Sahrens 			if (tail) {
118789Sahrens #ifdef ZFS_DEBUG
119789Sahrens 				uint64_t foundobj;
120789Sahrens 
121789Sahrens 				err = zap_lookup(dp->dp_meta_objset,
1224577Sahrens 				    dd->dd_parent->dd_phys->dd_child_dir_zapobj,
123789Sahrens 				    tail, sizeof (foundobj), 1, &foundobj);
1241544Seschrock 				ASSERT(err || foundobj == ddobj);
125789Sahrens #endif
126789Sahrens 				(void) strcpy(dd->dd_myname, tail);
127789Sahrens 			} else {
128789Sahrens 				err = zap_value_search(dp->dp_meta_objset,
1294577Sahrens 				    dd->dd_parent->dd_phys->dd_child_dir_zapobj,
1304577Sahrens 				    ddobj, 0, dd->dd_myname);
1311544Seschrock 			}
1327390SMatthew.Ahrens@Sun.COM 			if (err)
1337390SMatthew.Ahrens@Sun.COM 				goto errout;
134789Sahrens 		} else {
135789Sahrens 			(void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
136789Sahrens 		}
137789Sahrens 
138789Sahrens 		winner = dmu_buf_set_user_ie(dbuf, dd, &dd->dd_phys,
139789Sahrens 		    dsl_dir_evict);
140789Sahrens 		if (winner) {
141789Sahrens 			if (dd->dd_parent)
142789Sahrens 				dsl_dir_close(dd->dd_parent, dd);
1432856Snd150628 			mutex_destroy(&dd->dd_lock);
144789Sahrens 			kmem_free(dd, sizeof (dsl_dir_t));
145789Sahrens 			dd = winner;
146789Sahrens 		} else {
147789Sahrens 			spa_open_ref(dp->dp_spa, dd);
148789Sahrens 		}
149789Sahrens 	}
150789Sahrens 
151789Sahrens 	/*
152789Sahrens 	 * The dsl_dir_t has both open-to-close and instantiate-to-evict
153789Sahrens 	 * holds on the spa.  We need the open-to-close holds because
154789Sahrens 	 * otherwise the spa_refcnt wouldn't change when we open a
155789Sahrens 	 * dir which the spa also has open, so we could incorrectly
156789Sahrens 	 * think it was OK to unload/export/destroy the pool.  We need
157789Sahrens 	 * the instantiate-to-evict hold because the dsl_dir_t has a
158789Sahrens 	 * pointer to the dd_pool, which has a pointer to the spa_t.
159789Sahrens 	 */
160789Sahrens 	spa_open_ref(dp->dp_spa, tag);
161789Sahrens 	ASSERT3P(dd->dd_pool, ==, dp);
162789Sahrens 	ASSERT3U(dd->dd_object, ==, ddobj);
163789Sahrens 	ASSERT3P(dd->dd_dbuf, ==, dbuf);
1641544Seschrock 	*ddp = dd;
1651544Seschrock 	return (0);
1667390SMatthew.Ahrens@Sun.COM 
1677390SMatthew.Ahrens@Sun.COM errout:
1687390SMatthew.Ahrens@Sun.COM 	if (dd->dd_parent)
1697390SMatthew.Ahrens@Sun.COM 		dsl_dir_close(dd->dd_parent, dd);
1707390SMatthew.Ahrens@Sun.COM 	mutex_destroy(&dd->dd_lock);
1717390SMatthew.Ahrens@Sun.COM 	kmem_free(dd, sizeof (dsl_dir_t));
1727390SMatthew.Ahrens@Sun.COM 	dmu_buf_rele(dbuf, tag);
1737390SMatthew.Ahrens@Sun.COM 	return (err);
1747390SMatthew.Ahrens@Sun.COM 
175789Sahrens }
176789Sahrens 
177789Sahrens void
178789Sahrens dsl_dir_close(dsl_dir_t *dd, void *tag)
179789Sahrens {
180789Sahrens 	dprintf_dd(dd, "%s\n", "");
181789Sahrens 	spa_close(dd->dd_pool->dp_spa, tag);
1821544Seschrock 	dmu_buf_rele(dd->dd_dbuf, tag);
183789Sahrens }
184789Sahrens 
1852467Sek110237 /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */
186789Sahrens void
187789Sahrens dsl_dir_name(dsl_dir_t *dd, char *buf)
188789Sahrens {
189789Sahrens 	if (dd->dd_parent) {
190789Sahrens 		dsl_dir_name(dd->dd_parent, buf);
191789Sahrens 		(void) strcat(buf, "/");
192789Sahrens 	} else {
193789Sahrens 		buf[0] = '\0';
194789Sahrens 	}
195789Sahrens 	if (!MUTEX_HELD(&dd->dd_lock)) {
196789Sahrens 		/*
197789Sahrens 		 * recursive mutex so that we can use
198789Sahrens 		 * dprintf_dd() with dd_lock held
199789Sahrens 		 */
200789Sahrens 		mutex_enter(&dd->dd_lock);
201789Sahrens 		(void) strcat(buf, dd->dd_myname);
202789Sahrens 		mutex_exit(&dd->dd_lock);
203789Sahrens 	} else {
204789Sahrens 		(void) strcat(buf, dd->dd_myname);
205789Sahrens 	}
206789Sahrens }
207789Sahrens 
2083978Smmusante /* Calculate name legnth, avoiding all the strcat calls of dsl_dir_name */
2093978Smmusante int
2103978Smmusante dsl_dir_namelen(dsl_dir_t *dd)
2113978Smmusante {
2123978Smmusante 	int result = 0;
2133978Smmusante 
2143978Smmusante 	if (dd->dd_parent) {
2153978Smmusante 		/* parent's name + 1 for the "/" */
2163978Smmusante 		result = dsl_dir_namelen(dd->dd_parent) + 1;
2173978Smmusante 	}
2183978Smmusante 
2193978Smmusante 	if (!MUTEX_HELD(&dd->dd_lock)) {
2203978Smmusante 		/* see dsl_dir_name */
2213978Smmusante 		mutex_enter(&dd->dd_lock);
2223978Smmusante 		result += strlen(dd->dd_myname);
2233978Smmusante 		mutex_exit(&dd->dd_lock);
2243978Smmusante 	} else {
2253978Smmusante 		result += strlen(dd->dd_myname);
2263978Smmusante 	}
2273978Smmusante 
2283978Smmusante 	return (result);
2293978Smmusante }
2303978Smmusante 
231789Sahrens static int
232789Sahrens getcomponent(const char *path, char *component, const char **nextp)
233789Sahrens {
234789Sahrens 	char *p;
2358924SRichard.Morris@Sun.COM 	if ((path == NULL) || (path[0] == '\0'))
2362731Snd150628 		return (ENOENT);
237789Sahrens 	/* This would be a good place to reserve some namespace... */
238789Sahrens 	p = strpbrk(path, "/@");
239789Sahrens 	if (p && (p[1] == '/' || p[1] == '@')) {
240789Sahrens 		/* two separators in a row */
241789Sahrens 		return (EINVAL);
242789Sahrens 	}
243789Sahrens 	if (p == NULL || p == path) {
244789Sahrens 		/*
245789Sahrens 		 * if the first thing is an @ or /, it had better be an
246789Sahrens 		 * @ and it had better not have any more ats or slashes,
247789Sahrens 		 * and it had better have something after the @.
248789Sahrens 		 */
249789Sahrens 		if (p != NULL &&
250789Sahrens 		    (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
251789Sahrens 			return (EINVAL);
252789Sahrens 		if (strlen(path) >= MAXNAMELEN)
253789Sahrens 			return (ENAMETOOLONG);
254789Sahrens 		(void) strcpy(component, path);
255789Sahrens 		p = NULL;
256789Sahrens 	} else if (p[0] == '/') {
257789Sahrens 		if (p-path >= MAXNAMELEN)
258789Sahrens 			return (ENAMETOOLONG);
259789Sahrens 		(void) strncpy(component, path, p - path);
260789Sahrens 		component[p-path] = '\0';
261789Sahrens 		p++;
262789Sahrens 	} else if (p[0] == '@') {
263789Sahrens 		/*
264789Sahrens 		 * if the next separator is an @, there better not be
265789Sahrens 		 * any more slashes.
266789Sahrens 		 */
267789Sahrens 		if (strchr(path, '/'))
268789Sahrens 			return (EINVAL);
269789Sahrens 		if (p-path >= MAXNAMELEN)
270789Sahrens 			return (ENAMETOOLONG);
271789Sahrens 		(void) strncpy(component, path, p - path);
272789Sahrens 		component[p-path] = '\0';
273789Sahrens 	} else {
274789Sahrens 		ASSERT(!"invalid p");
275789Sahrens 	}
276789Sahrens 	*nextp = p;
277789Sahrens 	return (0);
278789Sahrens }
279789Sahrens 
280789Sahrens /*
281789Sahrens  * same as dsl_open_dir, ignore the first component of name and use the
282789Sahrens  * spa instead
283789Sahrens  */
2841544Seschrock int
2851544Seschrock dsl_dir_open_spa(spa_t *spa, const char *name, void *tag,
2861544Seschrock     dsl_dir_t **ddp, const char **tailp)
287789Sahrens {
288789Sahrens 	char buf[MAXNAMELEN];
289789Sahrens 	const char *next, *nextnext = NULL;
290789Sahrens 	int err;
291789Sahrens 	dsl_dir_t *dd;
292789Sahrens 	dsl_pool_t *dp;
293789Sahrens 	uint64_t ddobj;
294789Sahrens 	int openedspa = FALSE;
295789Sahrens 
296789Sahrens 	dprintf("%s\n", name);
297789Sahrens 
298789Sahrens 	err = getcomponent(name, buf, &next);
299789Sahrens 	if (err)
3001544Seschrock 		return (err);
301789Sahrens 	if (spa == NULL) {
302789Sahrens 		err = spa_open(buf, &spa, FTAG);
303789Sahrens 		if (err) {
304789Sahrens 			dprintf("spa_open(%s) failed\n", buf);
3051544Seschrock 			return (err);
306789Sahrens 		}
307789Sahrens 		openedspa = TRUE;
308789Sahrens 
309789Sahrens 		/* XXX this assertion belongs in spa_open */
310789Sahrens 		ASSERT(!dsl_pool_sync_context(spa_get_dsl(spa)));
311789Sahrens 	}
312789Sahrens 
313789Sahrens 	dp = spa_get_dsl(spa);
314789Sahrens 
315789Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_READER);
3161544Seschrock 	err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
3171544Seschrock 	if (err) {
3181544Seschrock 		rw_exit(&dp->dp_config_rwlock);
3191544Seschrock 		if (openedspa)
3201544Seschrock 			spa_close(spa, FTAG);
3211544Seschrock 		return (err);
3221544Seschrock 	}
3231544Seschrock 
324789Sahrens 	while (next != NULL) {
325789Sahrens 		dsl_dir_t *child_ds;
326789Sahrens 		err = getcomponent(next, buf, &nextnext);
3271544Seschrock 		if (err)
3281544Seschrock 			break;
329789Sahrens 		ASSERT(next[0] != '\0');
330789Sahrens 		if (next[0] == '@')
331789Sahrens 			break;
332789Sahrens 		dprintf("looking up %s in obj%lld\n",
333789Sahrens 		    buf, dd->dd_phys->dd_child_dir_zapobj);
334789Sahrens 
335789Sahrens 		err = zap_lookup(dp->dp_meta_objset,
336789Sahrens 		    dd->dd_phys->dd_child_dir_zapobj,
337789Sahrens 		    buf, sizeof (ddobj), 1, &ddobj);
3381544Seschrock 		if (err) {
3391544Seschrock 			if (err == ENOENT)
3401544Seschrock 				err = 0;
341789Sahrens 			break;
342789Sahrens 		}
343789Sahrens 
3441544Seschrock 		err = dsl_dir_open_obj(dp, ddobj, buf, tag, &child_ds);
3451544Seschrock 		if (err)
3461544Seschrock 			break;
347789Sahrens 		dsl_dir_close(dd, tag);
348789Sahrens 		dd = child_ds;
349789Sahrens 		next = nextnext;
350789Sahrens 	}
351789Sahrens 	rw_exit(&dp->dp_config_rwlock);
352789Sahrens 
3531544Seschrock 	if (err) {
3541544Seschrock 		dsl_dir_close(dd, tag);
3551544Seschrock 		if (openedspa)
3561544Seschrock 			spa_close(spa, FTAG);
3571544Seschrock 		return (err);
3581544Seschrock 	}
3591544Seschrock 
360789Sahrens 	/*
361789Sahrens 	 * It's an error if there's more than one component left, or
362789Sahrens 	 * tailp==NULL and there's any component left.
363789Sahrens 	 */
364789Sahrens 	if (next != NULL &&
365789Sahrens 	    (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
366789Sahrens 		/* bad path name */
367789Sahrens 		dsl_dir_close(dd, tag);
368789Sahrens 		dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
3691544Seschrock 		err = ENOENT;
370789Sahrens 	}
371789Sahrens 	if (tailp)
372789Sahrens 		*tailp = next;
373789Sahrens 	if (openedspa)
374789Sahrens 		spa_close(spa, FTAG);
3751544Seschrock 	*ddp = dd;
3761544Seschrock 	return (err);
377789Sahrens }
378789Sahrens 
379789Sahrens /*
380789Sahrens  * Return the dsl_dir_t, and possibly the last component which couldn't
381789Sahrens  * be found in *tail.  Return NULL if the path is bogus, or if
382789Sahrens  * tail==NULL and we couldn't parse the whole name.  (*tail)[0] == '@'
383789Sahrens  * means that the last component is a snapshot.
384789Sahrens  */
3851544Seschrock int
3861544Seschrock dsl_dir_open(const char *name, void *tag, dsl_dir_t **ddp, const char **tailp)
387789Sahrens {
3881544Seschrock 	return (dsl_dir_open_spa(NULL, name, tag, ddp, tailp));
389789Sahrens }
390789Sahrens 
3912199Sahrens uint64_t
3927046Sahrens dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
3937046Sahrens     dmu_tx_t *tx)
394789Sahrens {
3957046Sahrens 	objset_t *mos = dp->dp_meta_objset;
396789Sahrens 	uint64_t ddobj;
397789Sahrens 	dsl_dir_phys_t *dsphys;
398789Sahrens 	dmu_buf_t *dbuf;
399789Sahrens 
400928Stabriz 	ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
401928Stabriz 	    DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
4027046Sahrens 	if (pds) {
4037046Sahrens 		VERIFY(0 == zap_add(mos, pds->dd_phys->dd_child_dir_zapobj,
4047046Sahrens 		    name, sizeof (uint64_t), 1, &ddobj, tx));
4057046Sahrens 	} else {
4067046Sahrens 		/* it's the root dir */
4077046Sahrens 		VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
4087046Sahrens 		    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
4097046Sahrens 	}
4101544Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
411789Sahrens 	dmu_buf_will_dirty(dbuf, tx);
412789Sahrens 	dsphys = dbuf->db_data;
413789Sahrens 
414789Sahrens 	dsphys->dd_creation_time = gethrestime_sec();
4157046Sahrens 	if (pds)
4167046Sahrens 		dsphys->dd_parent_obj = pds->dd_object;
417789Sahrens 	dsphys->dd_props_zapobj = zap_create(mos,
418789Sahrens 	    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
419789Sahrens 	dsphys->dd_child_dir_zapobj = zap_create(mos,
420885Sahrens 	    DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
4217390SMatthew.Ahrens@Sun.COM 	if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
4227390SMatthew.Ahrens@Sun.COM 		dsphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
4231544Seschrock 	dmu_buf_rele(dbuf, FTAG);
424789Sahrens 
4252199Sahrens 	return (ddobj);
4262199Sahrens }
4272199Sahrens 
4282199Sahrens /* ARGSUSED */
4292199Sahrens int
4302199Sahrens dsl_dir_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
4312199Sahrens {
4322199Sahrens 	dsl_dir_t *dd = arg1;
4332199Sahrens 	dsl_pool_t *dp = dd->dd_pool;
4342199Sahrens 	objset_t *mos = dp->dp_meta_objset;
4352199Sahrens 	int err;
4362199Sahrens 	uint64_t count;
4372199Sahrens 
4382199Sahrens 	/*
4392199Sahrens 	 * There should be exactly two holds, both from
4402199Sahrens 	 * dsl_dataset_destroy: one on the dd directory, and one on its
4412199Sahrens 	 * head ds.  Otherwise, someone is trying to lookup something
4422199Sahrens 	 * inside this dir while we want to destroy it.  The
4432199Sahrens 	 * config_rwlock ensures that nobody else opens it after we
4442199Sahrens 	 * check.
4452199Sahrens 	 */
4462199Sahrens 	if (dmu_buf_refcount(dd->dd_dbuf) > 2)
4472199Sahrens 		return (EBUSY);
4482199Sahrens 
4492199Sahrens 	err = zap_count(mos, dd->dd_phys->dd_child_dir_zapobj, &count);
4502199Sahrens 	if (err)
4512199Sahrens 		return (err);
4522199Sahrens 	if (count != 0)
4532199Sahrens 		return (EEXIST);
454789Sahrens 
455789Sahrens 	return (0);
456789Sahrens }
457789Sahrens 
4582199Sahrens void
4594543Smarks dsl_dir_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx)
460789Sahrens {
4612199Sahrens 	dsl_dir_t *dd = arg1;
4622199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
4632199Sahrens 	uint64_t val, obj;
4647390SMatthew.Ahrens@Sun.COM 	dd_used_t t;
465789Sahrens 
4662199Sahrens 	ASSERT(RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock));
467789Sahrens 	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
468789Sahrens 
4692199Sahrens 	/* Remove our reservation. */
470789Sahrens 	val = 0;
4714543Smarks 	dsl_dir_set_reservation_sync(dd, &val, cr, tx);
4727390SMatthew.Ahrens@Sun.COM 	ASSERT3U(dd->dd_phys->dd_used_bytes, ==, 0);
473789Sahrens 	ASSERT3U(dd->dd_phys->dd_reserved, ==, 0);
4747390SMatthew.Ahrens@Sun.COM 	for (t = 0; t < DD_USED_NUM; t++)
4757390SMatthew.Ahrens@Sun.COM 		ASSERT3U(dd->dd_phys->dd_used_breakdown[t], ==, 0);
476789Sahrens 
4772199Sahrens 	VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
4782199Sahrens 	VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
4794543Smarks 	VERIFY(0 == dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx));
4802199Sahrens 	VERIFY(0 == zap_remove(mos,
4812199Sahrens 	    dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
482789Sahrens 
4832199Sahrens 	obj = dd->dd_object;
4842199Sahrens 	dsl_dir_close(dd, tag);
4852199Sahrens 	VERIFY(0 == dmu_object_free(mos, obj, tx));
486789Sahrens }
487789Sahrens 
4887046Sahrens boolean_t
4897046Sahrens dsl_dir_is_clone(dsl_dir_t *dd)
490789Sahrens {
4917046Sahrens 	return (dd->dd_phys->dd_origin_obj &&
4927046Sahrens 	    (dd->dd_pool->dp_origin_snap == NULL ||
4937046Sahrens 	    dd->dd_phys->dd_origin_obj !=
4947046Sahrens 	    dd->dd_pool->dp_origin_snap->ds_object));
495789Sahrens }
496789Sahrens 
497789Sahrens void
4982885Sahrens dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
499789Sahrens {
500789Sahrens 	mutex_enter(&dd->dd_lock);
5017390SMatthew.Ahrens@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
5027390SMatthew.Ahrens@Sun.COM 	    dd->dd_phys->dd_used_bytes);
5035378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA, dd->dd_phys->dd_quota);
5042885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
5052885Sahrens 	    dd->dd_phys->dd_reserved);
5062885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
5072885Sahrens 	    dd->dd_phys->dd_compressed_bytes == 0 ? 100 :
5082885Sahrens 	    (dd->dd_phys->dd_uncompressed_bytes * 100 /
5092885Sahrens 	    dd->dd_phys->dd_compressed_bytes));
5107390SMatthew.Ahrens@Sun.COM 	if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
5117390SMatthew.Ahrens@Sun.COM 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
5127390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]);
5137390SMatthew.Ahrens@Sun.COM 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
5147390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_breakdown[DD_USED_HEAD]);
5157390SMatthew.Ahrens@Sun.COM 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
5167390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_breakdown[DD_USED_REFRSRV]);
5177390SMatthew.Ahrens@Sun.COM 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
5187390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_breakdown[DD_USED_CHILD] +
5197390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_breakdown[DD_USED_CHILD_RSRV]);
5207390SMatthew.Ahrens@Sun.COM 	}
521789Sahrens 	mutex_exit(&dd->dd_lock);
522789Sahrens 
5235446Sahrens 	rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
5247046Sahrens 	if (dsl_dir_is_clone(dd)) {
525789Sahrens 		dsl_dataset_t *ds;
5262885Sahrens 		char buf[MAXNAMELEN];
527789Sahrens 
5286689Smaybee 		VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
5296689Smaybee 		    dd->dd_phys->dd_origin_obj, FTAG, &ds));
5302885Sahrens 		dsl_dataset_name(ds, buf);
5316689Smaybee 		dsl_dataset_rele(ds, FTAG);
5322885Sahrens 		dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
533789Sahrens 	}
5345446Sahrens 	rw_exit(&dd->dd_pool->dp_config_rwlock);
535789Sahrens }
536789Sahrens 
537789Sahrens void
538789Sahrens dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
539789Sahrens {
540789Sahrens 	dsl_pool_t *dp = dd->dd_pool;
541789Sahrens 
542789Sahrens 	ASSERT(dd->dd_phys);
543789Sahrens 
544789Sahrens 	if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg) == 0) {
545789Sahrens 		/* up the hold count until we can be written out */
546789Sahrens 		dmu_buf_add_ref(dd->dd_dbuf, dd);
547789Sahrens 	}
548789Sahrens }
549789Sahrens 
550789Sahrens static int64_t
551789Sahrens parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
552789Sahrens {
553789Sahrens 	uint64_t old_accounted = MAX(used, dd->dd_phys->dd_reserved);
554789Sahrens 	uint64_t new_accounted = MAX(used + delta, dd->dd_phys->dd_reserved);
555789Sahrens 	return (new_accounted - old_accounted);
556789Sahrens }
557789Sahrens 
558789Sahrens void
559789Sahrens dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
560789Sahrens {
561789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
562789Sahrens 
563789Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
564789Sahrens 
565789Sahrens 	mutex_enter(&dd->dd_lock);
566789Sahrens 	ASSERT3U(dd->dd_tempreserved[tx->tx_txg&TXG_MASK], ==, 0);
567789Sahrens 	dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
568789Sahrens 	    dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
569789Sahrens 	dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
570789Sahrens 	mutex_exit(&dd->dd_lock);
571789Sahrens 
572789Sahrens 	/* release the hold from dsl_dir_dirty */
5731544Seschrock 	dmu_buf_rele(dd->dd_dbuf, dd);
574789Sahrens }
575789Sahrens 
576789Sahrens static uint64_t
5775378Sck153898 dsl_dir_space_towrite(dsl_dir_t *dd)
578789Sahrens {
5795378Sck153898 	uint64_t space = 0;
580789Sahrens 	int i;
581789Sahrens 
582789Sahrens 	ASSERT(MUTEX_HELD(&dd->dd_lock));
583789Sahrens 
584789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
585789Sahrens 		space += dd->dd_space_towrite[i&TXG_MASK];
586789Sahrens 		ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
587789Sahrens 	}
588789Sahrens 	return (space);
589789Sahrens }
590789Sahrens 
591789Sahrens /*
592789Sahrens  * How much space would dd have available if ancestor had delta applied
593789Sahrens  * to it?  If ondiskonly is set, we're only interested in what's
594789Sahrens  * on-disk, not estimated pending changes.
595789Sahrens  */
5962885Sahrens uint64_t
597789Sahrens dsl_dir_space_available(dsl_dir_t *dd,
598789Sahrens     dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
599789Sahrens {
600789Sahrens 	uint64_t parentspace, myspace, quota, used;
601789Sahrens 
602789Sahrens 	/*
603789Sahrens 	 * If there are no restrictions otherwise, assume we have
604789Sahrens 	 * unlimited space available.
605789Sahrens 	 */
606789Sahrens 	quota = UINT64_MAX;
607789Sahrens 	parentspace = UINT64_MAX;
608789Sahrens 
609789Sahrens 	if (dd->dd_parent != NULL) {
610789Sahrens 		parentspace = dsl_dir_space_available(dd->dd_parent,
611789Sahrens 		    ancestor, delta, ondiskonly);
612789Sahrens 	}
613789Sahrens 
614789Sahrens 	mutex_enter(&dd->dd_lock);
615789Sahrens 	if (dd->dd_phys->dd_quota != 0)
616789Sahrens 		quota = dd->dd_phys->dd_quota;
6177390SMatthew.Ahrens@Sun.COM 	used = dd->dd_phys->dd_used_bytes;
6185378Sck153898 	if (!ondiskonly)
6195378Sck153898 		used += dsl_dir_space_towrite(dd);
620789Sahrens 
621789Sahrens 	if (dd->dd_parent == NULL) {
6222082Seschrock 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
623789Sahrens 		quota = MIN(quota, poolsize);
624789Sahrens 	}
625789Sahrens 
626789Sahrens 	if (dd->dd_phys->dd_reserved > used && parentspace != UINT64_MAX) {
627789Sahrens 		/*
628789Sahrens 		 * We have some space reserved, in addition to what our
629789Sahrens 		 * parent gave us.
630789Sahrens 		 */
631789Sahrens 		parentspace += dd->dd_phys->dd_reserved - used;
632789Sahrens 	}
633789Sahrens 
6347390SMatthew.Ahrens@Sun.COM 	if (dd == ancestor) {
6357390SMatthew.Ahrens@Sun.COM 		ASSERT(delta <= 0);
6367390SMatthew.Ahrens@Sun.COM 		ASSERT(used >= -delta);
6377390SMatthew.Ahrens@Sun.COM 		used += delta;
6387390SMatthew.Ahrens@Sun.COM 		if (parentspace != UINT64_MAX)
6397390SMatthew.Ahrens@Sun.COM 			parentspace -= delta;
6407390SMatthew.Ahrens@Sun.COM 	}
6417390SMatthew.Ahrens@Sun.COM 
642789Sahrens 	if (used > quota) {
643789Sahrens 		/* over quota */
644789Sahrens 		myspace = 0;
6452082Seschrock 
6462082Seschrock 		/*
6472082Seschrock 		 * While it's OK to be a little over quota, if
6482082Seschrock 		 * we think we are using more space than there
6492082Seschrock 		 * is in the pool (which is already 1.6% more than
6502082Seschrock 		 * dsl_pool_adjustedsize()), something is very
6512082Seschrock 		 * wrong.
6522082Seschrock 		 */
6532082Seschrock 		ASSERT3U(used, <=, spa_get_space(dd->dd_pool->dp_spa));
654789Sahrens 	} else {
655789Sahrens 		/*
6562082Seschrock 		 * the lesser of the space provided by our parent and
6572082Seschrock 		 * the space left in our quota
658789Sahrens 		 */
659789Sahrens 		myspace = MIN(parentspace, quota - used);
660789Sahrens 	}
661789Sahrens 
662789Sahrens 	mutex_exit(&dd->dd_lock);
663789Sahrens 
664789Sahrens 	return (myspace);
665789Sahrens }
666789Sahrens 
667789Sahrens struct tempreserve {
668789Sahrens 	list_node_t tr_node;
6696245Smaybee 	dsl_pool_t *tr_dp;
670789Sahrens 	dsl_dir_t *tr_ds;
671789Sahrens 	uint64_t tr_size;
672789Sahrens };
673789Sahrens 
674789Sahrens static int
6755378Sck153898 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
6765378Sck153898     boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
6776300Sck153898     dmu_tx_t *tx, boolean_t first)
678789Sahrens {
679789Sahrens 	uint64_t txg = tx->tx_txg;
6805378Sck153898 	uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
6815378Sck153898 	struct tempreserve *tr;
6825392Smaybee 	int enospc = EDQUOT;
683789Sahrens 	int txgidx = txg & TXG_MASK;
684789Sahrens 	int i;
6855831Sck153898 	uint64_t ref_rsrv = 0;
686789Sahrens 
687789Sahrens 	ASSERT3U(txg, !=, 0);
6885378Sck153898 	ASSERT3S(asize, >, 0);
689789Sahrens 
690789Sahrens 	mutex_enter(&dd->dd_lock);
6915378Sck153898 
692789Sahrens 	/*
693789Sahrens 	 * Check against the dsl_dir's quota.  We don't add in the delta
694789Sahrens 	 * when checking for over-quota because they get one free hit.
695789Sahrens 	 */
6965378Sck153898 	est_inflight = dsl_dir_space_towrite(dd);
697789Sahrens 	for (i = 0; i < TXG_SIZE; i++)
6985378Sck153898 		est_inflight += dd->dd_tempreserved[i];
6997390SMatthew.Ahrens@Sun.COM 	used_on_disk = dd->dd_phys->dd_used_bytes;
700789Sahrens 
7014709Smaybee 	/*
7026300Sck153898 	 * On the first iteration, fetch the dataset's used-on-disk and
7036300Sck153898 	 * refreservation values. Also, if checkrefquota is set, test if
7046300Sck153898 	 * allocating this space would exceed the dataset's refquota.
7054709Smaybee 	 */
7066300Sck153898 	if (first && tx->tx_objset) {
7075392Smaybee 		int error;
70810298SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
7095392Smaybee 
7105378Sck153898 		error = dsl_dataset_check_quota(ds, checkrefquota,
7115831Sck153898 		    asize, est_inflight, &used_on_disk, &ref_rsrv);
7125378Sck153898 		if (error) {
7135378Sck153898 			mutex_exit(&dd->dd_lock);
7145378Sck153898 			return (error);
7155378Sck153898 		}
7165378Sck153898 	}
7175378Sck153898 
7185378Sck153898 	/*
7195378Sck153898 	 * If this transaction will result in a net free of space,
7205378Sck153898 	 * we want to let it through.
7215378Sck153898 	 */
7225378Sck153898 	if (ignorequota || netfree || dd->dd_phys->dd_quota == 0)
7234709Smaybee 		quota = UINT64_MAX;
7244709Smaybee 	else
725789Sahrens 		quota = dd->dd_phys->dd_quota;
726789Sahrens 
727789Sahrens 	/*
7284709Smaybee 	 * Adjust the quota against the actual pool size at the root.
7294709Smaybee 	 * To ensure that it's possible to remove files from a full
7304709Smaybee 	 * pool without inducing transient overcommits, we throttle
731789Sahrens 	 * netfree transactions against a quota that is slightly larger,
732789Sahrens 	 * but still within the pool's allocation slop.  In cases where
733789Sahrens 	 * we're very close to full, this will allow a steady trickle of
734789Sahrens 	 * removes to get through.
735789Sahrens 	 */
7364944Smaybee 	if (dd->dd_parent == NULL) {
737789Sahrens 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
738789Sahrens 		if (poolsize < quota) {
739789Sahrens 			quota = poolsize;
7405392Smaybee 			enospc = ENOSPC;
741789Sahrens 		}
742789Sahrens 	}
743789Sahrens 
744789Sahrens 	/*
745789Sahrens 	 * If they are requesting more space, and our current estimate
7465378Sck153898 	 * is over quota, they get to try again unless the actual
7471544Seschrock 	 * on-disk is over quota and there are no pending changes (which
7481544Seschrock 	 * may free up space for us).
749789Sahrens 	 */
7505378Sck153898 	if (used_on_disk + est_inflight > quota) {
7515378Sck153898 		if (est_inflight > 0 || used_on_disk < quota)
7525392Smaybee 			enospc = ERESTART;
7535378Sck153898 		dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
754789Sahrens 		    "quota=%lluK tr=%lluK err=%d\n",
7555378Sck153898 		    used_on_disk>>10, est_inflight>>10,
7565392Smaybee 		    quota>>10, asize>>10, enospc);
757789Sahrens 		mutex_exit(&dd->dd_lock);
7585392Smaybee 		return (enospc);
759789Sahrens 	}
760789Sahrens 
761789Sahrens 	/* We need to up our estimated delta before dropping dd_lock */
762789Sahrens 	dd->dd_tempreserved[txgidx] += asize;
763789Sahrens 
7645831Sck153898 	parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
7655831Sck153898 	    asize - ref_rsrv);
766789Sahrens 	mutex_exit(&dd->dd_lock);
767789Sahrens 
7686245Smaybee 	tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
769789Sahrens 	tr->tr_ds = dd;
770789Sahrens 	tr->tr_size = asize;
771789Sahrens 	list_insert_tail(tr_list, tr);
772789Sahrens 
773789Sahrens 	/* see if it's OK with our parent */
7744944Smaybee 	if (dd->dd_parent && parent_rsrv) {
7754944Smaybee 		boolean_t ismos = (dd->dd_phys->dd_head_dataset_obj == 0);
7764944Smaybee 
777789Sahrens 		return (dsl_dir_tempreserve_impl(dd->dd_parent,
7786300Sck153898 		    parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE));
779789Sahrens 	} else {
780789Sahrens 		return (0);
781789Sahrens 	}
782789Sahrens }
783789Sahrens 
784789Sahrens /*
785789Sahrens  * Reserve space in this dsl_dir, to be used in this tx's txg.
7865378Sck153898  * After the space has been dirtied (and dsl_dir_willuse_space()
7875378Sck153898  * has been called), the reservation should be canceled, using
7885378Sck153898  * dsl_dir_tempreserve_clear().
789789Sahrens  */
790789Sahrens int
7915378Sck153898 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
7925378Sck153898     uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
793789Sahrens {
7946245Smaybee 	int err;
795789Sahrens 	list_t *tr_list;
796789Sahrens 
7975378Sck153898 	if (asize == 0) {
7985378Sck153898 		*tr_cookiep = NULL;
7995378Sck153898 		return (0);
8005378Sck153898 	}
8015378Sck153898 
802789Sahrens 	tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
803789Sahrens 	list_create(tr_list, sizeof (struct tempreserve),
804789Sahrens 	    offsetof(struct tempreserve, tr_node));
8055378Sck153898 	ASSERT3S(asize, >, 0);
8061544Seschrock 	ASSERT3S(fsize, >=, 0);
807789Sahrens 
8086245Smaybee 	err = arc_tempreserve_space(lsize, tx->tx_txg);
8096245Smaybee 	if (err == 0) {
8106245Smaybee 		struct tempreserve *tr;
8116245Smaybee 
8126245Smaybee 		tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
8136245Smaybee 		tr->tr_size = lsize;
8146245Smaybee 		list_insert_tail(tr_list, tr);
8156245Smaybee 
8166245Smaybee 		err = dsl_pool_tempreserve_space(dd->dd_pool, asize, tx);
8176245Smaybee 	} else {
8186245Smaybee 		if (err == EAGAIN) {
8196245Smaybee 			txg_delay(dd->dd_pool, tx->tx_txg, 1);
8206245Smaybee 			err = ERESTART;
8216245Smaybee 		}
8226245Smaybee 		dsl_pool_memory_pressure(dd->dd_pool);
8236245Smaybee 	}
824789Sahrens 
825789Sahrens 	if (err == 0) {
826789Sahrens 		struct tempreserve *tr;
827789Sahrens 
8286245Smaybee 		tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
8296245Smaybee 		tr->tr_dp = dd->dd_pool;
8306245Smaybee 		tr->tr_size = asize;
8316245Smaybee 		list_insert_tail(tr_list, tr);
8326245Smaybee 
8336245Smaybee 		err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
8346300Sck153898 		    FALSE, asize > usize, tr_list, tx, TRUE);
835789Sahrens 	}
836789Sahrens 
837789Sahrens 	if (err)
838789Sahrens 		dsl_dir_tempreserve_clear(tr_list, tx);
839789Sahrens 	else
840789Sahrens 		*tr_cookiep = tr_list;
8416245Smaybee 
842789Sahrens 	return (err);
843789Sahrens }
844789Sahrens 
845789Sahrens /*
846789Sahrens  * Clear a temporary reservation that we previously made with
847789Sahrens  * dsl_dir_tempreserve_space().
848789Sahrens  */
849789Sahrens void
850789Sahrens dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
851789Sahrens {
852789Sahrens 	int txgidx = tx->tx_txg & TXG_MASK;
853789Sahrens 	list_t *tr_list = tr_cookie;
854789Sahrens 	struct tempreserve *tr;
855789Sahrens 
856789Sahrens 	ASSERT3U(tx->tx_txg, !=, 0);
857789Sahrens 
8585378Sck153898 	if (tr_cookie == NULL)
8595378Sck153898 		return;
8605378Sck153898 
861789Sahrens 	while (tr = list_head(tr_list)) {
8626245Smaybee 		if (tr->tr_dp) {
8636245Smaybee 			dsl_pool_tempreserve_clear(tr->tr_dp, tr->tr_size, tx);
8646245Smaybee 		} else if (tr->tr_ds) {
865789Sahrens 			mutex_enter(&tr->tr_ds->dd_lock);
866789Sahrens 			ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
867789Sahrens 			    tr->tr_size);
868789Sahrens 			tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
869789Sahrens 			mutex_exit(&tr->tr_ds->dd_lock);
8706245Smaybee 		} else {
8716245Smaybee 			arc_tempreserve_clear(tr->tr_size);
872789Sahrens 		}
873789Sahrens 		list_remove(tr_list, tr);
874789Sahrens 		kmem_free(tr, sizeof (struct tempreserve));
875789Sahrens 	}
876789Sahrens 
877789Sahrens 	kmem_free(tr_list, sizeof (list_t));
878789Sahrens }
879789Sahrens 
8806245Smaybee static void
8816245Smaybee dsl_dir_willuse_space_impl(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
882789Sahrens {
883789Sahrens 	int64_t parent_space;
884789Sahrens 	uint64_t est_used;
885789Sahrens 
886789Sahrens 	mutex_enter(&dd->dd_lock);
887789Sahrens 	if (space > 0)
888789Sahrens 		dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
889789Sahrens 
8907390SMatthew.Ahrens@Sun.COM 	est_used = dsl_dir_space_towrite(dd) + dd->dd_phys->dd_used_bytes;
891789Sahrens 	parent_space = parent_delta(dd, est_used, space);
892789Sahrens 	mutex_exit(&dd->dd_lock);
893789Sahrens 
894789Sahrens 	/* Make sure that we clean up dd_space_to* */
895789Sahrens 	dsl_dir_dirty(dd, tx);
896789Sahrens 
897789Sahrens 	/* XXX this is potentially expensive and unnecessary... */
898789Sahrens 	if (parent_space && dd->dd_parent)
8996245Smaybee 		dsl_dir_willuse_space_impl(dd->dd_parent, parent_space, tx);
9006245Smaybee }
9016245Smaybee 
9026245Smaybee /*
9036245Smaybee  * Call in open context when we think we're going to write/free space,
9046245Smaybee  * eg. when dirtying data.  Be conservative (ie. OK to write less than
9056245Smaybee  * this or free more than this, but don't write more or free less).
9066245Smaybee  */
9076245Smaybee void
9086245Smaybee dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
9096245Smaybee {
9106245Smaybee 	dsl_pool_willuse_space(dd->dd_pool, space, tx);
9116245Smaybee 	dsl_dir_willuse_space_impl(dd, space, tx);
912789Sahrens }
913789Sahrens 
914789Sahrens /* call from syncing context when we actually write/free space for this dd */
915789Sahrens void
9167390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
917789Sahrens     int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
918789Sahrens {
919789Sahrens 	int64_t accounted_delta;
9207595SMatthew.Ahrens@Sun.COM 	boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
921789Sahrens 
922789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
9237390SMatthew.Ahrens@Sun.COM 	ASSERT(type < DD_USED_NUM);
924789Sahrens 
925789Sahrens 	dsl_dir_dirty(dd, tx);
926789Sahrens 
9277595SMatthew.Ahrens@Sun.COM 	if (needlock)
9287595SMatthew.Ahrens@Sun.COM 		mutex_enter(&dd->dd_lock);
9297390SMatthew.Ahrens@Sun.COM 	accounted_delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, used);
9307390SMatthew.Ahrens@Sun.COM 	ASSERT(used >= 0 || dd->dd_phys->dd_used_bytes >= -used);
931789Sahrens 	ASSERT(compressed >= 0 ||
932789Sahrens 	    dd->dd_phys->dd_compressed_bytes >= -compressed);
933789Sahrens 	ASSERT(uncompressed >= 0 ||
934789Sahrens 	    dd->dd_phys->dd_uncompressed_bytes >= -uncompressed);
9357390SMatthew.Ahrens@Sun.COM 	dd->dd_phys->dd_used_bytes += used;
936789Sahrens 	dd->dd_phys->dd_uncompressed_bytes += uncompressed;
937789Sahrens 	dd->dd_phys->dd_compressed_bytes += compressed;
9387390SMatthew.Ahrens@Sun.COM 
9397390SMatthew.Ahrens@Sun.COM 	if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
9407390SMatthew.Ahrens@Sun.COM 		ASSERT(used > 0 ||
9417390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_breakdown[type] >= -used);
9427390SMatthew.Ahrens@Sun.COM 		dd->dd_phys->dd_used_breakdown[type] += used;
9437390SMatthew.Ahrens@Sun.COM #ifdef DEBUG
9447390SMatthew.Ahrens@Sun.COM 		dd_used_t t;
9457390SMatthew.Ahrens@Sun.COM 		uint64_t u = 0;
9467390SMatthew.Ahrens@Sun.COM 		for (t = 0; t < DD_USED_NUM; t++)
9477390SMatthew.Ahrens@Sun.COM 			u += dd->dd_phys->dd_used_breakdown[t];
9487390SMatthew.Ahrens@Sun.COM 		ASSERT3U(u, ==, dd->dd_phys->dd_used_bytes);
9497390SMatthew.Ahrens@Sun.COM #endif
9507390SMatthew.Ahrens@Sun.COM 	}
9517595SMatthew.Ahrens@Sun.COM 	if (needlock)
9527595SMatthew.Ahrens@Sun.COM 		mutex_exit(&dd->dd_lock);
953789Sahrens 
954789Sahrens 	if (dd->dd_parent != NULL) {
9557390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
956789Sahrens 		    accounted_delta, compressed, uncompressed, tx);
9577390SMatthew.Ahrens@Sun.COM 		dsl_dir_transfer_space(dd->dd_parent,
9587390SMatthew.Ahrens@Sun.COM 		    used - accounted_delta,
9597390SMatthew.Ahrens@Sun.COM 		    DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
960789Sahrens 	}
961789Sahrens }
962789Sahrens 
9637390SMatthew.Ahrens@Sun.COM void
9647390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
9657390SMatthew.Ahrens@Sun.COM     dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
9667390SMatthew.Ahrens@Sun.COM {
9677595SMatthew.Ahrens@Sun.COM 	boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
9687595SMatthew.Ahrens@Sun.COM 
9697390SMatthew.Ahrens@Sun.COM 	ASSERT(dmu_tx_is_syncing(tx));
9707390SMatthew.Ahrens@Sun.COM 	ASSERT(oldtype < DD_USED_NUM);
9717390SMatthew.Ahrens@Sun.COM 	ASSERT(newtype < DD_USED_NUM);
9727390SMatthew.Ahrens@Sun.COM 
9737390SMatthew.Ahrens@Sun.COM 	if (delta == 0 || !(dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN))
9747390SMatthew.Ahrens@Sun.COM 		return;
9757390SMatthew.Ahrens@Sun.COM 
9767390SMatthew.Ahrens@Sun.COM 	dsl_dir_dirty(dd, tx);
9777595SMatthew.Ahrens@Sun.COM 	if (needlock)
9787595SMatthew.Ahrens@Sun.COM 		mutex_enter(&dd->dd_lock);
9797390SMatthew.Ahrens@Sun.COM 	ASSERT(delta > 0 ?
9807390SMatthew.Ahrens@Sun.COM 	    dd->dd_phys->dd_used_breakdown[oldtype] >= delta :
9817390SMatthew.Ahrens@Sun.COM 	    dd->dd_phys->dd_used_breakdown[newtype] >= -delta);
9827390SMatthew.Ahrens@Sun.COM 	ASSERT(dd->dd_phys->dd_used_bytes >= ABS(delta));
9837390SMatthew.Ahrens@Sun.COM 	dd->dd_phys->dd_used_breakdown[oldtype] -= delta;
9847390SMatthew.Ahrens@Sun.COM 	dd->dd_phys->dd_used_breakdown[newtype] += delta;
9857595SMatthew.Ahrens@Sun.COM 	if (needlock)
9867595SMatthew.Ahrens@Sun.COM 		mutex_exit(&dd->dd_lock);
9877390SMatthew.Ahrens@Sun.COM }
9887390SMatthew.Ahrens@Sun.COM 
989789Sahrens static int
9902199Sahrens dsl_dir_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
991789Sahrens {
9922199Sahrens 	dsl_dir_t *dd = arg1;
9932199Sahrens 	uint64_t *quotap = arg2;
994789Sahrens 	uint64_t new_quota = *quotap;
995789Sahrens 	int err = 0;
9962199Sahrens 	uint64_t towrite;
9972199Sahrens 
9982199Sahrens 	if (new_quota == 0)
9992199Sahrens 		return (0);
10002199Sahrens 
10012199Sahrens 	mutex_enter(&dd->dd_lock);
10022199Sahrens 	/*
10032199Sahrens 	 * If we are doing the preliminary check in open context, and
10042199Sahrens 	 * there are pending changes, then don't fail it, since the
10055378Sck153898 	 * pending changes could under-estimate the amount of space to be
10062199Sahrens 	 * freed up.
10072199Sahrens 	 */
10085378Sck153898 	towrite = dsl_dir_space_towrite(dd);
10092199Sahrens 	if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
10102199Sahrens 	    (new_quota < dd->dd_phys->dd_reserved ||
10117390SMatthew.Ahrens@Sun.COM 	    new_quota < dd->dd_phys->dd_used_bytes + towrite)) {
10122199Sahrens 		err = ENOSPC;
10132199Sahrens 	}
10142199Sahrens 	mutex_exit(&dd->dd_lock);
10152199Sahrens 	return (err);
10162199Sahrens }
10172199Sahrens 
10184543Smarks /* ARGSUSED */
10192199Sahrens static void
10204543Smarks dsl_dir_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
10212199Sahrens {
10222199Sahrens 	dsl_dir_t *dd = arg1;
10232199Sahrens 	uint64_t *quotap = arg2;
10242199Sahrens 	uint64_t new_quota = *quotap;
1025789Sahrens 
1026789Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1027789Sahrens 
1028789Sahrens 	mutex_enter(&dd->dd_lock);
10292199Sahrens 	dd->dd_phys->dd_quota = new_quota;
1030789Sahrens 	mutex_exit(&dd->dd_lock);
10314543Smarks 
10324543Smarks 	spa_history_internal_log(LOG_DS_QUOTA, dd->dd_pool->dp_spa,
10334543Smarks 	    tx, cr, "%lld dataset = %llu ",
10344543Smarks 	    (longlong_t)new_quota, dd->dd_phys->dd_head_dataset_obj);
1035789Sahrens }
1036789Sahrens 
1037789Sahrens int
1038789Sahrens dsl_dir_set_quota(const char *ddname, uint64_t quota)
1039789Sahrens {
1040789Sahrens 	dsl_dir_t *dd;
1041789Sahrens 	int err;
1042789Sahrens 
10431544Seschrock 	err = dsl_dir_open(ddname, FTAG, &dd, NULL);
10441544Seschrock 	if (err)
10451544Seschrock 		return (err);
1046789Sahrens 
10475481Sck153898 	if (quota != dd->dd_phys->dd_quota) {
10485481Sck153898 		/*
10495481Sck153898 		 * If someone removes a file, then tries to set the quota, we
10505481Sck153898 		 * want to make sure the file freeing takes effect.
10515481Sck153898 		 */
10525481Sck153898 		txg_wait_open(dd->dd_pool, 0);
10535481Sck153898 
10545481Sck153898 		err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_quota_check,
10555481Sck153898 		    dsl_dir_set_quota_sync, dd, &quota, 0);
10565481Sck153898 	}
1057789Sahrens 	dsl_dir_close(dd, FTAG);
1058789Sahrens 	return (err);
1059789Sahrens }
1060789Sahrens 
10615378Sck153898 int
10622199Sahrens dsl_dir_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
1063789Sahrens {
10642199Sahrens 	dsl_dir_t *dd = arg1;
10652199Sahrens 	uint64_t *reservationp = arg2;
1066789Sahrens 	uint64_t new_reservation = *reservationp;
1067789Sahrens 	uint64_t used, avail;
1068789Sahrens 
10692199Sahrens 	/*
10702199Sahrens 	 * If we are doing the preliminary check in open context, the
10712199Sahrens 	 * space estimates may be inaccurate.
10722199Sahrens 	 */
10732199Sahrens 	if (!dmu_tx_is_syncing(tx))
10742199Sahrens 		return (0);
10752199Sahrens 
1076789Sahrens 	mutex_enter(&dd->dd_lock);
10777390SMatthew.Ahrens@Sun.COM 	used = dd->dd_phys->dd_used_bytes;
1078789Sahrens 	mutex_exit(&dd->dd_lock);
1079789Sahrens 
1080789Sahrens 	if (dd->dd_parent) {
1081789Sahrens 		avail = dsl_dir_space_available(dd->dd_parent,
1082789Sahrens 		    NULL, 0, FALSE);
1083789Sahrens 	} else {
1084789Sahrens 		avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1085789Sahrens 	}
1086789Sahrens 
10878525SEric.Schrock@Sun.COM 	if (MAX(used, new_reservation) > MAX(used, dd->dd_phys->dd_reserved)) {
10888525SEric.Schrock@Sun.COM 		uint64_t delta = MAX(used, new_reservation) -
10898525SEric.Schrock@Sun.COM 		    MAX(used, dd->dd_phys->dd_reserved);
10908525SEric.Schrock@Sun.COM 
10918525SEric.Schrock@Sun.COM 		if (delta > avail)
10928525SEric.Schrock@Sun.COM 			return (ENOSPC);
10938525SEric.Schrock@Sun.COM 		if (dd->dd_phys->dd_quota > 0 &&
10948525SEric.Schrock@Sun.COM 		    new_reservation > dd->dd_phys->dd_quota)
10958525SEric.Schrock@Sun.COM 			return (ENOSPC);
10968525SEric.Schrock@Sun.COM 	}
10978525SEric.Schrock@Sun.COM 
10982199Sahrens 	return (0);
10992199Sahrens }
11002199Sahrens 
11014543Smarks /* ARGSUSED */
11022199Sahrens static void
11034543Smarks dsl_dir_set_reservation_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
11042199Sahrens {
11052199Sahrens 	dsl_dir_t *dd = arg1;
11062199Sahrens 	uint64_t *reservationp = arg2;
11072199Sahrens 	uint64_t new_reservation = *reservationp;
11082199Sahrens 	uint64_t used;
11092199Sahrens 	int64_t delta;
11102199Sahrens 
11115378Sck153898 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
11125378Sck153898 
11132199Sahrens 	mutex_enter(&dd->dd_lock);
11147390SMatthew.Ahrens@Sun.COM 	used = dd->dd_phys->dd_used_bytes;
11152199Sahrens 	delta = MAX(used, new_reservation) -
11162199Sahrens 	    MAX(used, dd->dd_phys->dd_reserved);
11175378Sck153898 	dd->dd_phys->dd_reserved = new_reservation;
1118789Sahrens 
1119789Sahrens 	if (dd->dd_parent != NULL) {
1120789Sahrens 		/* Roll up this additional usage into our ancestors */
11217390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
11227390SMatthew.Ahrens@Sun.COM 		    delta, 0, 0, tx);
1123789Sahrens 	}
11247595SMatthew.Ahrens@Sun.COM 	mutex_exit(&dd->dd_lock);
11254543Smarks 
11264543Smarks 	spa_history_internal_log(LOG_DS_RESERVATION, dd->dd_pool->dp_spa,
11274543Smarks 	    tx, cr, "%lld dataset = %llu",
11284543Smarks 	    (longlong_t)new_reservation, dd->dd_phys->dd_head_dataset_obj);
1129789Sahrens }
1130789Sahrens 
1131789Sahrens int
1132789Sahrens dsl_dir_set_reservation(const char *ddname, uint64_t reservation)
1133789Sahrens {
1134789Sahrens 	dsl_dir_t *dd;
1135789Sahrens 	int err;
1136789Sahrens 
11371544Seschrock 	err = dsl_dir_open(ddname, FTAG, &dd, NULL);
11381544Seschrock 	if (err)
11391544Seschrock 		return (err);
11402199Sahrens 	err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_reservation_check,
11412199Sahrens 	    dsl_dir_set_reservation_sync, dd, &reservation, 0);
1142789Sahrens 	dsl_dir_close(dd, FTAG);
1143789Sahrens 	return (err);
1144789Sahrens }
1145789Sahrens 
1146789Sahrens static dsl_dir_t *
1147789Sahrens closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1148789Sahrens {
1149789Sahrens 	for (; ds1; ds1 = ds1->dd_parent) {
1150789Sahrens 		dsl_dir_t *dd;
1151789Sahrens 		for (dd = ds2; dd; dd = dd->dd_parent) {
1152789Sahrens 			if (ds1 == dd)
1153789Sahrens 				return (dd);
1154789Sahrens 		}
1155789Sahrens 	}
1156789Sahrens 	return (NULL);
1157789Sahrens }
1158789Sahrens 
1159789Sahrens /*
1160789Sahrens  * If delta is applied to dd, how much of that delta would be applied to
1161789Sahrens  * ancestor?  Syncing context only.
1162789Sahrens  */
1163789Sahrens static int64_t
1164789Sahrens would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1165789Sahrens {
1166789Sahrens 	if (dd == ancestor)
1167789Sahrens 		return (delta);
1168789Sahrens 
1169789Sahrens 	mutex_enter(&dd->dd_lock);
11707390SMatthew.Ahrens@Sun.COM 	delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, delta);
1171789Sahrens 	mutex_exit(&dd->dd_lock);
1172789Sahrens 	return (would_change(dd->dd_parent, delta, ancestor));
1173789Sahrens }
1174789Sahrens 
11752199Sahrens struct renamearg {
11762199Sahrens 	dsl_dir_t *newparent;
11772199Sahrens 	const char *mynewname;
11782199Sahrens };
11792199Sahrens 
11804543Smarks /*ARGSUSED*/
11812199Sahrens static int
11822199Sahrens dsl_dir_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
1183789Sahrens {
11842199Sahrens 	dsl_dir_t *dd = arg1;
11852199Sahrens 	struct renamearg *ra = arg2;
1186789Sahrens 	dsl_pool_t *dp = dd->dd_pool;
1187789Sahrens 	objset_t *mos = dp->dp_meta_objset;
11882199Sahrens 	int err;
11892199Sahrens 	uint64_t val;
11902199Sahrens 
11912199Sahrens 	/* There should be 2 references: the open and the dirty */
11922199Sahrens 	if (dmu_buf_refcount(dd->dd_dbuf) > 2)
11932199Sahrens 		return (EBUSY);
1194789Sahrens 
11952199Sahrens 	/* check for existing name */
11962199Sahrens 	err = zap_lookup(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
11972199Sahrens 	    ra->mynewname, 8, 1, &val);
11982199Sahrens 	if (err == 0)
11992199Sahrens 		return (EEXIST);
12002199Sahrens 	if (err != ENOENT)
12011544Seschrock 		return (err);
1202789Sahrens 
12032199Sahrens 	if (ra->newparent != dd->dd_parent) {
12042082Seschrock 		/* is there enough space? */
12052082Seschrock 		uint64_t myspace =
12067390SMatthew.Ahrens@Sun.COM 		    MAX(dd->dd_phys->dd_used_bytes, dd->dd_phys->dd_reserved);
1207789Sahrens 
12082199Sahrens 		/* no rename into our descendant */
12092199Sahrens 		if (closest_common_ancestor(dd, ra->newparent) == dd)
1210789Sahrens 			return (EINVAL);
12112199Sahrens 
12122199Sahrens 		if (err = dsl_dir_transfer_possible(dd->dd_parent,
12132199Sahrens 		    ra->newparent, myspace))
12142199Sahrens 			return (err);
12152199Sahrens 	}
12162199Sahrens 
12172199Sahrens 	return (0);
12182199Sahrens }
1219789Sahrens 
12202199Sahrens static void
12214543Smarks dsl_dir_rename_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
12222199Sahrens {
12232199Sahrens 	dsl_dir_t *dd = arg1;
12242199Sahrens 	struct renamearg *ra = arg2;
12252199Sahrens 	dsl_pool_t *dp = dd->dd_pool;
12262199Sahrens 	objset_t *mos = dp->dp_meta_objset;
12272199Sahrens 	int err;
1228789Sahrens 
12292199Sahrens 	ASSERT(dmu_buf_refcount(dd->dd_dbuf) <= 2);
12302199Sahrens 
12312199Sahrens 	if (ra->newparent != dd->dd_parent) {
12327390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
12337390SMatthew.Ahrens@Sun.COM 		    -dd->dd_phys->dd_used_bytes,
1234789Sahrens 		    -dd->dd_phys->dd_compressed_bytes,
1235789Sahrens 		    -dd->dd_phys->dd_uncompressed_bytes, tx);
12367390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD,
12377390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_bytes,
1238789Sahrens 		    dd->dd_phys->dd_compressed_bytes,
1239789Sahrens 		    dd->dd_phys->dd_uncompressed_bytes, tx);
12407390SMatthew.Ahrens@Sun.COM 
12417390SMatthew.Ahrens@Sun.COM 		if (dd->dd_phys->dd_reserved > dd->dd_phys->dd_used_bytes) {
12427390SMatthew.Ahrens@Sun.COM 			uint64_t unused_rsrv = dd->dd_phys->dd_reserved -
12437390SMatthew.Ahrens@Sun.COM 			    dd->dd_phys->dd_used_bytes;
12447390SMatthew.Ahrens@Sun.COM 
12457390SMatthew.Ahrens@Sun.COM 			dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
12467390SMatthew.Ahrens@Sun.COM 			    -unused_rsrv, 0, 0, tx);
12477390SMatthew.Ahrens@Sun.COM 			dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD_RSRV,
12487390SMatthew.Ahrens@Sun.COM 			    unused_rsrv, 0, 0, tx);
12497390SMatthew.Ahrens@Sun.COM 		}
1250789Sahrens 	}
1251789Sahrens 
1252789Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1253789Sahrens 
1254789Sahrens 	/* remove from old parent zapobj */
1255789Sahrens 	err = zap_remove(mos, dd->dd_parent->dd_phys->dd_child_dir_zapobj,
1256789Sahrens 	    dd->dd_myname, tx);
1257789Sahrens 	ASSERT3U(err, ==, 0);
1258789Sahrens 
12592199Sahrens 	(void) strcpy(dd->dd_myname, ra->mynewname);
1260789Sahrens 	dsl_dir_close(dd->dd_parent, dd);
12612199Sahrens 	dd->dd_phys->dd_parent_obj = ra->newparent->dd_object;
12621544Seschrock 	VERIFY(0 == dsl_dir_open_obj(dd->dd_pool,
12632199Sahrens 	    ra->newparent->dd_object, NULL, dd, &dd->dd_parent));
1264789Sahrens 
1265789Sahrens 	/* add to new parent zapobj */
12662199Sahrens 	err = zap_add(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
1267789Sahrens 	    dd->dd_myname, 8, 1, &dd->dd_object, tx);
1268789Sahrens 	ASSERT3U(err, ==, 0);
12694543Smarks 
12704543Smarks 	spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa,
12714543Smarks 	    tx, cr, "dataset = %llu", dd->dd_phys->dd_head_dataset_obj);
12722199Sahrens }
1273789Sahrens 
12742199Sahrens int
12752199Sahrens dsl_dir_rename(dsl_dir_t *dd, const char *newname)
12762199Sahrens {
12772199Sahrens 	struct renamearg ra;
12782199Sahrens 	int err;
12792199Sahrens 
12802199Sahrens 	/* new parent should exist */
12812199Sahrens 	err = dsl_dir_open(newname, FTAG, &ra.newparent, &ra.mynewname);
12822199Sahrens 	if (err)
12832199Sahrens 		return (err);
12842199Sahrens 
12852199Sahrens 	/* can't rename to different pool */
12862199Sahrens 	if (dd->dd_pool != ra.newparent->dd_pool) {
12872199Sahrens 		err = ENXIO;
12882199Sahrens 		goto out;
12892199Sahrens 	}
12902199Sahrens 
12912199Sahrens 	/* new name should not already exist */
12922199Sahrens 	if (ra.mynewname == NULL) {
12932199Sahrens 		err = EEXIST;
12942199Sahrens 		goto out;
12952199Sahrens 	}
12962199Sahrens 
12972199Sahrens 	err = dsl_sync_task_do(dd->dd_pool,
12982199Sahrens 	    dsl_dir_rename_check, dsl_dir_rename_sync, dd, &ra, 3);
12992199Sahrens 
13002199Sahrens out:
13012199Sahrens 	dsl_dir_close(ra.newparent, FTAG);
13022199Sahrens 	return (err);
1303789Sahrens }
13042082Seschrock 
13052082Seschrock int
13062082Seschrock dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, uint64_t space)
13072082Seschrock {
13082082Seschrock 	dsl_dir_t *ancestor;
13092082Seschrock 	int64_t adelta;
13102082Seschrock 	uint64_t avail;
13112082Seschrock 
13122082Seschrock 	ancestor = closest_common_ancestor(sdd, tdd);
13132082Seschrock 	adelta = would_change(sdd, -space, ancestor);
13142082Seschrock 	avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
13152082Seschrock 	if (avail < space)
13162082Seschrock 		return (ENOSPC);
13172082Seschrock 
13182082Seschrock 	return (0);
13192082Seschrock }
1320*10373Schris.kirby@sun.com 
1321*10373Schris.kirby@sun.com timestruc_t
1322*10373Schris.kirby@sun.com dsl_dir_snap_cmtime(dsl_dir_t *dd)
1323*10373Schris.kirby@sun.com {
1324*10373Schris.kirby@sun.com 	timestruc_t t;
1325*10373Schris.kirby@sun.com 
1326*10373Schris.kirby@sun.com 	mutex_enter(&dd->dd_lock);
1327*10373Schris.kirby@sun.com 	t = dd->dd_snap_cmtime;
1328*10373Schris.kirby@sun.com 	mutex_exit(&dd->dd_lock);
1329*10373Schris.kirby@sun.com 
1330*10373Schris.kirby@sun.com 	return (t);
1331*10373Schris.kirby@sun.com }
1332*10373Schris.kirby@sun.com 
1333*10373Schris.kirby@sun.com void
1334*10373Schris.kirby@sun.com dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
1335*10373Schris.kirby@sun.com {
1336*10373Schris.kirby@sun.com 	timestruc_t t;
1337*10373Schris.kirby@sun.com 
1338*10373Schris.kirby@sun.com 	gethrestime(&t);
1339*10373Schris.kirby@sun.com 	mutex_enter(&dd->dd_lock);
1340*10373Schris.kirby@sun.com 	dd->dd_snap_cmtime = t;
1341*10373Schris.kirby@sun.com 	mutex_exit(&dd->dd_lock);
1342*10373Schris.kirby@sun.com }
1343