xref: /onnv-gate/usr/src/uts/common/fs/zfs/dsl_dir.c (revision 7390:6d408f0a5fbd)
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 /*
225831Sck153898  * Copyright 2008 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);
94*7390SMatthew.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 		int err;
100789Sahrens 
101789Sahrens 		dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
102789Sahrens 		dd->dd_object = ddobj;
103789Sahrens 		dd->dd_dbuf = dbuf;
104789Sahrens 		dd->dd_pool = dp;
105789Sahrens 		dd->dd_phys = dbuf->db_data;
1062856Snd150628 		mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
107789Sahrens 
108789Sahrens 		list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t),
109789Sahrens 		    offsetof(dsl_prop_cb_record_t, cbr_node));
110789Sahrens 
111789Sahrens 		if (dd->dd_phys->dd_parent_obj) {
1121544Seschrock 			err = dsl_dir_open_obj(dp, dd->dd_phys->dd_parent_obj,
1131544Seschrock 			    NULL, dd, &dd->dd_parent);
114*7390SMatthew.Ahrens@Sun.COM 			if (err)
115*7390SMatthew.Ahrens@Sun.COM 				goto errout;
116789Sahrens 			if (tail) {
117789Sahrens #ifdef ZFS_DEBUG
118789Sahrens 				uint64_t foundobj;
119789Sahrens 
120789Sahrens 				err = zap_lookup(dp->dp_meta_objset,
1214577Sahrens 				    dd->dd_parent->dd_phys->dd_child_dir_zapobj,
122789Sahrens 				    tail, sizeof (foundobj), 1, &foundobj);
1231544Seschrock 				ASSERT(err || foundobj == ddobj);
124789Sahrens #endif
125789Sahrens 				(void) strcpy(dd->dd_myname, tail);
126789Sahrens 			} else {
127789Sahrens 				err = zap_value_search(dp->dp_meta_objset,
1284577Sahrens 				    dd->dd_parent->dd_phys->dd_child_dir_zapobj,
1294577Sahrens 				    ddobj, 0, dd->dd_myname);
1301544Seschrock 			}
131*7390SMatthew.Ahrens@Sun.COM 			if (err)
132*7390SMatthew.Ahrens@Sun.COM 				goto errout;
133789Sahrens 		} else {
134789Sahrens 			(void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
135789Sahrens 		}
136789Sahrens 
137789Sahrens 		winner = dmu_buf_set_user_ie(dbuf, dd, &dd->dd_phys,
138789Sahrens 		    dsl_dir_evict);
139789Sahrens 		if (winner) {
140789Sahrens 			if (dd->dd_parent)
141789Sahrens 				dsl_dir_close(dd->dd_parent, dd);
1422856Snd150628 			mutex_destroy(&dd->dd_lock);
143789Sahrens 			kmem_free(dd, sizeof (dsl_dir_t));
144789Sahrens 			dd = winner;
145789Sahrens 		} else {
146789Sahrens 			spa_open_ref(dp->dp_spa, dd);
147789Sahrens 		}
148789Sahrens 	}
149789Sahrens 
150789Sahrens 	/*
151789Sahrens 	 * The dsl_dir_t has both open-to-close and instantiate-to-evict
152789Sahrens 	 * holds on the spa.  We need the open-to-close holds because
153789Sahrens 	 * otherwise the spa_refcnt wouldn't change when we open a
154789Sahrens 	 * dir which the spa also has open, so we could incorrectly
155789Sahrens 	 * think it was OK to unload/export/destroy the pool.  We need
156789Sahrens 	 * the instantiate-to-evict hold because the dsl_dir_t has a
157789Sahrens 	 * pointer to the dd_pool, which has a pointer to the spa_t.
158789Sahrens 	 */
159789Sahrens 	spa_open_ref(dp->dp_spa, tag);
160789Sahrens 	ASSERT3P(dd->dd_pool, ==, dp);
161789Sahrens 	ASSERT3U(dd->dd_object, ==, ddobj);
162789Sahrens 	ASSERT3P(dd->dd_dbuf, ==, dbuf);
1631544Seschrock 	*ddp = dd;
1641544Seschrock 	return (0);
165*7390SMatthew.Ahrens@Sun.COM 
166*7390SMatthew.Ahrens@Sun.COM errout:
167*7390SMatthew.Ahrens@Sun.COM 	if (dd->dd_parent)
168*7390SMatthew.Ahrens@Sun.COM 		dsl_dir_close(dd->dd_parent, dd);
169*7390SMatthew.Ahrens@Sun.COM 	mutex_destroy(&dd->dd_lock);
170*7390SMatthew.Ahrens@Sun.COM 	kmem_free(dd, sizeof (dsl_dir_t));
171*7390SMatthew.Ahrens@Sun.COM 	dmu_buf_rele(dbuf, tag);
172*7390SMatthew.Ahrens@Sun.COM 	return (err);
173*7390SMatthew.Ahrens@Sun.COM 
174789Sahrens }
175789Sahrens 
176789Sahrens void
177789Sahrens dsl_dir_close(dsl_dir_t *dd, void *tag)
178789Sahrens {
179789Sahrens 	dprintf_dd(dd, "%s\n", "");
180789Sahrens 	spa_close(dd->dd_pool->dp_spa, tag);
1811544Seschrock 	dmu_buf_rele(dd->dd_dbuf, tag);
182789Sahrens }
183789Sahrens 
1842467Sek110237 /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */
185789Sahrens void
186789Sahrens dsl_dir_name(dsl_dir_t *dd, char *buf)
187789Sahrens {
188789Sahrens 	if (dd->dd_parent) {
189789Sahrens 		dsl_dir_name(dd->dd_parent, buf);
190789Sahrens 		(void) strcat(buf, "/");
191789Sahrens 	} else {
192789Sahrens 		buf[0] = '\0';
193789Sahrens 	}
194789Sahrens 	if (!MUTEX_HELD(&dd->dd_lock)) {
195789Sahrens 		/*
196789Sahrens 		 * recursive mutex so that we can use
197789Sahrens 		 * dprintf_dd() with dd_lock held
198789Sahrens 		 */
199789Sahrens 		mutex_enter(&dd->dd_lock);
200789Sahrens 		(void) strcat(buf, dd->dd_myname);
201789Sahrens 		mutex_exit(&dd->dd_lock);
202789Sahrens 	} else {
203789Sahrens 		(void) strcat(buf, dd->dd_myname);
204789Sahrens 	}
205789Sahrens }
206789Sahrens 
2073978Smmusante /* Calculate name legnth, avoiding all the strcat calls of dsl_dir_name */
2083978Smmusante int
2093978Smmusante dsl_dir_namelen(dsl_dir_t *dd)
2103978Smmusante {
2113978Smmusante 	int result = 0;
2123978Smmusante 
2133978Smmusante 	if (dd->dd_parent) {
2143978Smmusante 		/* parent's name + 1 for the "/" */
2153978Smmusante 		result = dsl_dir_namelen(dd->dd_parent) + 1;
2163978Smmusante 	}
2173978Smmusante 
2183978Smmusante 	if (!MUTEX_HELD(&dd->dd_lock)) {
2193978Smmusante 		/* see dsl_dir_name */
2203978Smmusante 		mutex_enter(&dd->dd_lock);
2213978Smmusante 		result += strlen(dd->dd_myname);
2223978Smmusante 		mutex_exit(&dd->dd_lock);
2233978Smmusante 	} else {
2243978Smmusante 		result += strlen(dd->dd_myname);
2253978Smmusante 	}
2263978Smmusante 
2273978Smmusante 	return (result);
2283978Smmusante }
2293978Smmusante 
230789Sahrens int
231789Sahrens dsl_dir_is_private(dsl_dir_t *dd)
232789Sahrens {
233789Sahrens 	int rv = FALSE;
234789Sahrens 
235789Sahrens 	if (dd->dd_parent && dsl_dir_is_private(dd->dd_parent))
236789Sahrens 		rv = TRUE;
237789Sahrens 	if (dataset_name_hidden(dd->dd_myname))
238789Sahrens 		rv = TRUE;
239789Sahrens 	return (rv);
240789Sahrens }
241789Sahrens 
242789Sahrens 
243789Sahrens static int
244789Sahrens getcomponent(const char *path, char *component, const char **nextp)
245789Sahrens {
246789Sahrens 	char *p;
247789Sahrens 	if (path == NULL)
2482731Snd150628 		return (ENOENT);
249789Sahrens 	/* This would be a good place to reserve some namespace... */
250789Sahrens 	p = strpbrk(path, "/@");
251789Sahrens 	if (p && (p[1] == '/' || p[1] == '@')) {
252789Sahrens 		/* two separators in a row */
253789Sahrens 		return (EINVAL);
254789Sahrens 	}
255789Sahrens 	if (p == NULL || p == path) {
256789Sahrens 		/*
257789Sahrens 		 * if the first thing is an @ or /, it had better be an
258789Sahrens 		 * @ and it had better not have any more ats or slashes,
259789Sahrens 		 * and it had better have something after the @.
260789Sahrens 		 */
261789Sahrens 		if (p != NULL &&
262789Sahrens 		    (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
263789Sahrens 			return (EINVAL);
264789Sahrens 		if (strlen(path) >= MAXNAMELEN)
265789Sahrens 			return (ENAMETOOLONG);
266789Sahrens 		(void) strcpy(component, path);
267789Sahrens 		p = NULL;
268789Sahrens 	} else if (p[0] == '/') {
269789Sahrens 		if (p-path >= MAXNAMELEN)
270789Sahrens 			return (ENAMETOOLONG);
271789Sahrens 		(void) strncpy(component, path, p - path);
272789Sahrens 		component[p-path] = '\0';
273789Sahrens 		p++;
274789Sahrens 	} else if (p[0] == '@') {
275789Sahrens 		/*
276789Sahrens 		 * if the next separator is an @, there better not be
277789Sahrens 		 * any more slashes.
278789Sahrens 		 */
279789Sahrens 		if (strchr(path, '/'))
280789Sahrens 			return (EINVAL);
281789Sahrens 		if (p-path >= MAXNAMELEN)
282789Sahrens 			return (ENAMETOOLONG);
283789Sahrens 		(void) strncpy(component, path, p - path);
284789Sahrens 		component[p-path] = '\0';
285789Sahrens 	} else {
286789Sahrens 		ASSERT(!"invalid p");
287789Sahrens 	}
288789Sahrens 	*nextp = p;
289789Sahrens 	return (0);
290789Sahrens }
291789Sahrens 
292789Sahrens /*
293789Sahrens  * same as dsl_open_dir, ignore the first component of name and use the
294789Sahrens  * spa instead
295789Sahrens  */
2961544Seschrock int
2971544Seschrock dsl_dir_open_spa(spa_t *spa, const char *name, void *tag,
2981544Seschrock     dsl_dir_t **ddp, const char **tailp)
299789Sahrens {
300789Sahrens 	char buf[MAXNAMELEN];
301789Sahrens 	const char *next, *nextnext = NULL;
302789Sahrens 	int err;
303789Sahrens 	dsl_dir_t *dd;
304789Sahrens 	dsl_pool_t *dp;
305789Sahrens 	uint64_t ddobj;
306789Sahrens 	int openedspa = FALSE;
307789Sahrens 
308789Sahrens 	dprintf("%s\n", name);
309789Sahrens 
310789Sahrens 	err = getcomponent(name, buf, &next);
311789Sahrens 	if (err)
3121544Seschrock 		return (err);
313789Sahrens 	if (spa == NULL) {
314789Sahrens 		err = spa_open(buf, &spa, FTAG);
315789Sahrens 		if (err) {
316789Sahrens 			dprintf("spa_open(%s) failed\n", buf);
3171544Seschrock 			return (err);
318789Sahrens 		}
319789Sahrens 		openedspa = TRUE;
320789Sahrens 
321789Sahrens 		/* XXX this assertion belongs in spa_open */
322789Sahrens 		ASSERT(!dsl_pool_sync_context(spa_get_dsl(spa)));
323789Sahrens 	}
324789Sahrens 
325789Sahrens 	dp = spa_get_dsl(spa);
326789Sahrens 
327789Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_READER);
3281544Seschrock 	err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
3291544Seschrock 	if (err) {
3301544Seschrock 		rw_exit(&dp->dp_config_rwlock);
3311544Seschrock 		if (openedspa)
3321544Seschrock 			spa_close(spa, FTAG);
3331544Seschrock 		return (err);
3341544Seschrock 	}
3351544Seschrock 
336789Sahrens 	while (next != NULL) {
337789Sahrens 		dsl_dir_t *child_ds;
338789Sahrens 		err = getcomponent(next, buf, &nextnext);
3391544Seschrock 		if (err)
3401544Seschrock 			break;
341789Sahrens 		ASSERT(next[0] != '\0');
342789Sahrens 		if (next[0] == '@')
343789Sahrens 			break;
344789Sahrens 		dprintf("looking up %s in obj%lld\n",
345789Sahrens 		    buf, dd->dd_phys->dd_child_dir_zapobj);
346789Sahrens 
347789Sahrens 		err = zap_lookup(dp->dp_meta_objset,
348789Sahrens 		    dd->dd_phys->dd_child_dir_zapobj,
349789Sahrens 		    buf, sizeof (ddobj), 1, &ddobj);
3501544Seschrock 		if (err) {
3511544Seschrock 			if (err == ENOENT)
3521544Seschrock 				err = 0;
353789Sahrens 			break;
354789Sahrens 		}
355789Sahrens 
3561544Seschrock 		err = dsl_dir_open_obj(dp, ddobj, buf, tag, &child_ds);
3571544Seschrock 		if (err)
3581544Seschrock 			break;
359789Sahrens 		dsl_dir_close(dd, tag);
360789Sahrens 		dd = child_ds;
361789Sahrens 		next = nextnext;
362789Sahrens 	}
363789Sahrens 	rw_exit(&dp->dp_config_rwlock);
364789Sahrens 
3651544Seschrock 	if (err) {
3661544Seschrock 		dsl_dir_close(dd, tag);
3671544Seschrock 		if (openedspa)
3681544Seschrock 			spa_close(spa, FTAG);
3691544Seschrock 		return (err);
3701544Seschrock 	}
3711544Seschrock 
372789Sahrens 	/*
373789Sahrens 	 * It's an error if there's more than one component left, or
374789Sahrens 	 * tailp==NULL and there's any component left.
375789Sahrens 	 */
376789Sahrens 	if (next != NULL &&
377789Sahrens 	    (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
378789Sahrens 		/* bad path name */
379789Sahrens 		dsl_dir_close(dd, tag);
380789Sahrens 		dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
3811544Seschrock 		err = ENOENT;
382789Sahrens 	}
383789Sahrens 	if (tailp)
384789Sahrens 		*tailp = next;
385789Sahrens 	if (openedspa)
386789Sahrens 		spa_close(spa, FTAG);
3871544Seschrock 	*ddp = dd;
3881544Seschrock 	return (err);
389789Sahrens }
390789Sahrens 
391789Sahrens /*
392789Sahrens  * Return the dsl_dir_t, and possibly the last component which couldn't
393789Sahrens  * be found in *tail.  Return NULL if the path is bogus, or if
394789Sahrens  * tail==NULL and we couldn't parse the whole name.  (*tail)[0] == '@'
395789Sahrens  * means that the last component is a snapshot.
396789Sahrens  */
3971544Seschrock int
3981544Seschrock dsl_dir_open(const char *name, void *tag, dsl_dir_t **ddp, const char **tailp)
399789Sahrens {
4001544Seschrock 	return (dsl_dir_open_spa(NULL, name, tag, ddp, tailp));
401789Sahrens }
402789Sahrens 
4032199Sahrens uint64_t
4047046Sahrens dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
4057046Sahrens     dmu_tx_t *tx)
406789Sahrens {
4077046Sahrens 	objset_t *mos = dp->dp_meta_objset;
408789Sahrens 	uint64_t ddobj;
409789Sahrens 	dsl_dir_phys_t *dsphys;
410789Sahrens 	dmu_buf_t *dbuf;
411789Sahrens 
412928Stabriz 	ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
413928Stabriz 	    DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
4147046Sahrens 	if (pds) {
4157046Sahrens 		VERIFY(0 == zap_add(mos, pds->dd_phys->dd_child_dir_zapobj,
4167046Sahrens 		    name, sizeof (uint64_t), 1, &ddobj, tx));
4177046Sahrens 	} else {
4187046Sahrens 		/* it's the root dir */
4197046Sahrens 		VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
4207046Sahrens 		    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
4217046Sahrens 	}
4221544Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
423789Sahrens 	dmu_buf_will_dirty(dbuf, tx);
424789Sahrens 	dsphys = dbuf->db_data;
425789Sahrens 
426789Sahrens 	dsphys->dd_creation_time = gethrestime_sec();
4277046Sahrens 	if (pds)
4287046Sahrens 		dsphys->dd_parent_obj = pds->dd_object;
429789Sahrens 	dsphys->dd_props_zapobj = zap_create(mos,
430789Sahrens 	    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
431789Sahrens 	dsphys->dd_child_dir_zapobj = zap_create(mos,
432885Sahrens 	    DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
433*7390SMatthew.Ahrens@Sun.COM 	if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
434*7390SMatthew.Ahrens@Sun.COM 		dsphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
4351544Seschrock 	dmu_buf_rele(dbuf, FTAG);
436789Sahrens 
4372199Sahrens 	return (ddobj);
4382199Sahrens }
4392199Sahrens 
4402199Sahrens /* ARGSUSED */
4412199Sahrens int
4422199Sahrens dsl_dir_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
4432199Sahrens {
4442199Sahrens 	dsl_dir_t *dd = arg1;
4452199Sahrens 	dsl_pool_t *dp = dd->dd_pool;
4462199Sahrens 	objset_t *mos = dp->dp_meta_objset;
4472199Sahrens 	int err;
4482199Sahrens 	uint64_t count;
4492199Sahrens 
4502199Sahrens 	/*
4512199Sahrens 	 * There should be exactly two holds, both from
4522199Sahrens 	 * dsl_dataset_destroy: one on the dd directory, and one on its
4532199Sahrens 	 * head ds.  Otherwise, someone is trying to lookup something
4542199Sahrens 	 * inside this dir while we want to destroy it.  The
4552199Sahrens 	 * config_rwlock ensures that nobody else opens it after we
4562199Sahrens 	 * check.
4572199Sahrens 	 */
4582199Sahrens 	if (dmu_buf_refcount(dd->dd_dbuf) > 2)
4592199Sahrens 		return (EBUSY);
4602199Sahrens 
4612199Sahrens 	err = zap_count(mos, dd->dd_phys->dd_child_dir_zapobj, &count);
4622199Sahrens 	if (err)
4632199Sahrens 		return (err);
4642199Sahrens 	if (count != 0)
4652199Sahrens 		return (EEXIST);
466789Sahrens 
467789Sahrens 	return (0);
468789Sahrens }
469789Sahrens 
4702199Sahrens void
4714543Smarks dsl_dir_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx)
472789Sahrens {
4732199Sahrens 	dsl_dir_t *dd = arg1;
4742199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
4752199Sahrens 	uint64_t val, obj;
476*7390SMatthew.Ahrens@Sun.COM 	dd_used_t t;
477789Sahrens 
4782199Sahrens 	ASSERT(RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock));
479789Sahrens 	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
480789Sahrens 
4812199Sahrens 	/* Remove our reservation. */
482789Sahrens 	val = 0;
4834543Smarks 	dsl_dir_set_reservation_sync(dd, &val, cr, tx);
484*7390SMatthew.Ahrens@Sun.COM 	ASSERT3U(dd->dd_phys->dd_used_bytes, ==, 0);
485789Sahrens 	ASSERT3U(dd->dd_phys->dd_reserved, ==, 0);
486*7390SMatthew.Ahrens@Sun.COM 	for (t = 0; t < DD_USED_NUM; t++)
487*7390SMatthew.Ahrens@Sun.COM 		ASSERT3U(dd->dd_phys->dd_used_breakdown[t], ==, 0);
488789Sahrens 
4892199Sahrens 	VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
4902199Sahrens 	VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
4914543Smarks 	VERIFY(0 == dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx));
4922199Sahrens 	VERIFY(0 == zap_remove(mos,
4932199Sahrens 	    dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
494789Sahrens 
4952199Sahrens 	obj = dd->dd_object;
4962199Sahrens 	dsl_dir_close(dd, tag);
4972199Sahrens 	VERIFY(0 == dmu_object_free(mos, obj, tx));
498789Sahrens }
499789Sahrens 
5007046Sahrens boolean_t
5017046Sahrens dsl_dir_is_clone(dsl_dir_t *dd)
502789Sahrens {
5037046Sahrens 	return (dd->dd_phys->dd_origin_obj &&
5047046Sahrens 	    (dd->dd_pool->dp_origin_snap == NULL ||
5057046Sahrens 	    dd->dd_phys->dd_origin_obj !=
5067046Sahrens 	    dd->dd_pool->dp_origin_snap->ds_object));
507789Sahrens }
508789Sahrens 
509789Sahrens void
5102885Sahrens dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
511789Sahrens {
512789Sahrens 	mutex_enter(&dd->dd_lock);
513*7390SMatthew.Ahrens@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
514*7390SMatthew.Ahrens@Sun.COM 	    dd->dd_phys->dd_used_bytes);
5155378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA, dd->dd_phys->dd_quota);
5162885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
5172885Sahrens 	    dd->dd_phys->dd_reserved);
5182885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
5192885Sahrens 	    dd->dd_phys->dd_compressed_bytes == 0 ? 100 :
5202885Sahrens 	    (dd->dd_phys->dd_uncompressed_bytes * 100 /
5212885Sahrens 	    dd->dd_phys->dd_compressed_bytes));
522*7390SMatthew.Ahrens@Sun.COM 	if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
523*7390SMatthew.Ahrens@Sun.COM 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
524*7390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]);
525*7390SMatthew.Ahrens@Sun.COM 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
526*7390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_breakdown[DD_USED_HEAD]);
527*7390SMatthew.Ahrens@Sun.COM 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
528*7390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_breakdown[DD_USED_REFRSRV]);
529*7390SMatthew.Ahrens@Sun.COM 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
530*7390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_breakdown[DD_USED_CHILD] +
531*7390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_breakdown[DD_USED_CHILD_RSRV]);
532*7390SMatthew.Ahrens@Sun.COM 	}
533789Sahrens 	mutex_exit(&dd->dd_lock);
534789Sahrens 
5355446Sahrens 	rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
5367046Sahrens 	if (dsl_dir_is_clone(dd)) {
537789Sahrens 		dsl_dataset_t *ds;
5382885Sahrens 		char buf[MAXNAMELEN];
539789Sahrens 
5406689Smaybee 		VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
5416689Smaybee 		    dd->dd_phys->dd_origin_obj, FTAG, &ds));
5422885Sahrens 		dsl_dataset_name(ds, buf);
5436689Smaybee 		dsl_dataset_rele(ds, FTAG);
5442885Sahrens 		dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
545789Sahrens 	}
5465446Sahrens 	rw_exit(&dd->dd_pool->dp_config_rwlock);
547789Sahrens }
548789Sahrens 
549789Sahrens void
550789Sahrens dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
551789Sahrens {
552789Sahrens 	dsl_pool_t *dp = dd->dd_pool;
553789Sahrens 
554789Sahrens 	ASSERT(dd->dd_phys);
555789Sahrens 
556789Sahrens 	if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg) == 0) {
557789Sahrens 		/* up the hold count until we can be written out */
558789Sahrens 		dmu_buf_add_ref(dd->dd_dbuf, dd);
559789Sahrens 	}
560789Sahrens }
561789Sahrens 
562789Sahrens static int64_t
563789Sahrens parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
564789Sahrens {
565789Sahrens 	uint64_t old_accounted = MAX(used, dd->dd_phys->dd_reserved);
566789Sahrens 	uint64_t new_accounted = MAX(used + delta, dd->dd_phys->dd_reserved);
567789Sahrens 	return (new_accounted - old_accounted);
568789Sahrens }
569789Sahrens 
570789Sahrens void
571789Sahrens dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
572789Sahrens {
573789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
574789Sahrens 
575789Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
576789Sahrens 
577789Sahrens 	mutex_enter(&dd->dd_lock);
578789Sahrens 	ASSERT3U(dd->dd_tempreserved[tx->tx_txg&TXG_MASK], ==, 0);
579789Sahrens 	dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
580789Sahrens 	    dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
581789Sahrens 	dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
582789Sahrens 	mutex_exit(&dd->dd_lock);
583789Sahrens 
584789Sahrens 	/* release the hold from dsl_dir_dirty */
5851544Seschrock 	dmu_buf_rele(dd->dd_dbuf, dd);
586789Sahrens }
587789Sahrens 
588789Sahrens static uint64_t
5895378Sck153898 dsl_dir_space_towrite(dsl_dir_t *dd)
590789Sahrens {
5915378Sck153898 	uint64_t space = 0;
592789Sahrens 	int i;
593789Sahrens 
594789Sahrens 	ASSERT(MUTEX_HELD(&dd->dd_lock));
595789Sahrens 
596789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
597789Sahrens 		space += dd->dd_space_towrite[i&TXG_MASK];
598789Sahrens 		ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
599789Sahrens 	}
600789Sahrens 	return (space);
601789Sahrens }
602789Sahrens 
603789Sahrens /*
604789Sahrens  * How much space would dd have available if ancestor had delta applied
605789Sahrens  * to it?  If ondiskonly is set, we're only interested in what's
606789Sahrens  * on-disk, not estimated pending changes.
607789Sahrens  */
6082885Sahrens uint64_t
609789Sahrens dsl_dir_space_available(dsl_dir_t *dd,
610789Sahrens     dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
611789Sahrens {
612789Sahrens 	uint64_t parentspace, myspace, quota, used;
613789Sahrens 
614789Sahrens 	/*
615789Sahrens 	 * If there are no restrictions otherwise, assume we have
616789Sahrens 	 * unlimited space available.
617789Sahrens 	 */
618789Sahrens 	quota = UINT64_MAX;
619789Sahrens 	parentspace = UINT64_MAX;
620789Sahrens 
621789Sahrens 	if (dd->dd_parent != NULL) {
622789Sahrens 		parentspace = dsl_dir_space_available(dd->dd_parent,
623789Sahrens 		    ancestor, delta, ondiskonly);
624789Sahrens 	}
625789Sahrens 
626789Sahrens 	mutex_enter(&dd->dd_lock);
627789Sahrens 	if (dd->dd_phys->dd_quota != 0)
628789Sahrens 		quota = dd->dd_phys->dd_quota;
629*7390SMatthew.Ahrens@Sun.COM 	used = dd->dd_phys->dd_used_bytes;
6305378Sck153898 	if (!ondiskonly)
6315378Sck153898 		used += dsl_dir_space_towrite(dd);
632789Sahrens 
633789Sahrens 	if (dd->dd_parent == NULL) {
6342082Seschrock 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
635789Sahrens 		quota = MIN(quota, poolsize);
636789Sahrens 	}
637789Sahrens 
638789Sahrens 	if (dd->dd_phys->dd_reserved > used && parentspace != UINT64_MAX) {
639789Sahrens 		/*
640789Sahrens 		 * We have some space reserved, in addition to what our
641789Sahrens 		 * parent gave us.
642789Sahrens 		 */
643789Sahrens 		parentspace += dd->dd_phys->dd_reserved - used;
644789Sahrens 	}
645789Sahrens 
646*7390SMatthew.Ahrens@Sun.COM 	if (dd == ancestor) {
647*7390SMatthew.Ahrens@Sun.COM 		ASSERT(delta <= 0);
648*7390SMatthew.Ahrens@Sun.COM 		ASSERT(used >= -delta);
649*7390SMatthew.Ahrens@Sun.COM 		used += delta;
650*7390SMatthew.Ahrens@Sun.COM 		if (parentspace != UINT64_MAX)
651*7390SMatthew.Ahrens@Sun.COM 			parentspace -= delta;
652*7390SMatthew.Ahrens@Sun.COM 	}
653*7390SMatthew.Ahrens@Sun.COM 
654789Sahrens 	if (used > quota) {
655789Sahrens 		/* over quota */
656789Sahrens 		myspace = 0;
6572082Seschrock 
6582082Seschrock 		/*
6592082Seschrock 		 * While it's OK to be a little over quota, if
6602082Seschrock 		 * we think we are using more space than there
6612082Seschrock 		 * is in the pool (which is already 1.6% more than
6622082Seschrock 		 * dsl_pool_adjustedsize()), something is very
6632082Seschrock 		 * wrong.
6642082Seschrock 		 */
6652082Seschrock 		ASSERT3U(used, <=, spa_get_space(dd->dd_pool->dp_spa));
666789Sahrens 	} else {
667789Sahrens 		/*
6682082Seschrock 		 * the lesser of the space provided by our parent and
6692082Seschrock 		 * the space left in our quota
670789Sahrens 		 */
671789Sahrens 		myspace = MIN(parentspace, quota - used);
672789Sahrens 	}
673789Sahrens 
674789Sahrens 	mutex_exit(&dd->dd_lock);
675789Sahrens 
676789Sahrens 	return (myspace);
677789Sahrens }
678789Sahrens 
679789Sahrens struct tempreserve {
680789Sahrens 	list_node_t tr_node;
6816245Smaybee 	dsl_pool_t *tr_dp;
682789Sahrens 	dsl_dir_t *tr_ds;
683789Sahrens 	uint64_t tr_size;
684789Sahrens };
685789Sahrens 
686789Sahrens static int
6875378Sck153898 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
6885378Sck153898     boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
6896300Sck153898     dmu_tx_t *tx, boolean_t first)
690789Sahrens {
691789Sahrens 	uint64_t txg = tx->tx_txg;
6925378Sck153898 	uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
6935378Sck153898 	struct tempreserve *tr;
6945392Smaybee 	int enospc = EDQUOT;
695789Sahrens 	int txgidx = txg & TXG_MASK;
696789Sahrens 	int i;
6975831Sck153898 	uint64_t ref_rsrv = 0;
698789Sahrens 
699789Sahrens 	ASSERT3U(txg, !=, 0);
7005378Sck153898 	ASSERT3S(asize, >, 0);
701789Sahrens 
702789Sahrens 	mutex_enter(&dd->dd_lock);
7035378Sck153898 
704789Sahrens 	/*
705789Sahrens 	 * Check against the dsl_dir's quota.  We don't add in the delta
706789Sahrens 	 * when checking for over-quota because they get one free hit.
707789Sahrens 	 */
7085378Sck153898 	est_inflight = dsl_dir_space_towrite(dd);
709789Sahrens 	for (i = 0; i < TXG_SIZE; i++)
7105378Sck153898 		est_inflight += dd->dd_tempreserved[i];
711*7390SMatthew.Ahrens@Sun.COM 	used_on_disk = dd->dd_phys->dd_used_bytes;
712789Sahrens 
7134709Smaybee 	/*
7146300Sck153898 	 * On the first iteration, fetch the dataset's used-on-disk and
7156300Sck153898 	 * refreservation values. Also, if checkrefquota is set, test if
7166300Sck153898 	 * allocating this space would exceed the dataset's refquota.
7174709Smaybee 	 */
7186300Sck153898 	if (first && tx->tx_objset) {
7195392Smaybee 		int error;
7205831Sck153898 		dsl_dataset_t *ds = tx->tx_objset->os->os_dsl_dataset;
7215392Smaybee 
7225378Sck153898 		error = dsl_dataset_check_quota(ds, checkrefquota,
7235831Sck153898 		    asize, est_inflight, &used_on_disk, &ref_rsrv);
7245378Sck153898 		if (error) {
7255378Sck153898 			mutex_exit(&dd->dd_lock);
7265378Sck153898 			return (error);
7275378Sck153898 		}
7285378Sck153898 	}
7295378Sck153898 
7305378Sck153898 	/*
7315378Sck153898 	 * If this transaction will result in a net free of space,
7325378Sck153898 	 * we want to let it through.
7335378Sck153898 	 */
7345378Sck153898 	if (ignorequota || netfree || dd->dd_phys->dd_quota == 0)
7354709Smaybee 		quota = UINT64_MAX;
7364709Smaybee 	else
737789Sahrens 		quota = dd->dd_phys->dd_quota;
738789Sahrens 
739789Sahrens 	/*
7404709Smaybee 	 * Adjust the quota against the actual pool size at the root.
7414709Smaybee 	 * To ensure that it's possible to remove files from a full
7424709Smaybee 	 * pool without inducing transient overcommits, we throttle
743789Sahrens 	 * netfree transactions against a quota that is slightly larger,
744789Sahrens 	 * but still within the pool's allocation slop.  In cases where
745789Sahrens 	 * we're very close to full, this will allow a steady trickle of
746789Sahrens 	 * removes to get through.
747789Sahrens 	 */
7484944Smaybee 	if (dd->dd_parent == NULL) {
749789Sahrens 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
750789Sahrens 		if (poolsize < quota) {
751789Sahrens 			quota = poolsize;
7525392Smaybee 			enospc = ENOSPC;
753789Sahrens 		}
754789Sahrens 	}
755789Sahrens 
756789Sahrens 	/*
757789Sahrens 	 * If they are requesting more space, and our current estimate
7585378Sck153898 	 * is over quota, they get to try again unless the actual
7591544Seschrock 	 * on-disk is over quota and there are no pending changes (which
7601544Seschrock 	 * may free up space for us).
761789Sahrens 	 */
7625378Sck153898 	if (used_on_disk + est_inflight > quota) {
7635378Sck153898 		if (est_inflight > 0 || used_on_disk < quota)
7645392Smaybee 			enospc = ERESTART;
7655378Sck153898 		dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
766789Sahrens 		    "quota=%lluK tr=%lluK err=%d\n",
7675378Sck153898 		    used_on_disk>>10, est_inflight>>10,
7685392Smaybee 		    quota>>10, asize>>10, enospc);
769789Sahrens 		mutex_exit(&dd->dd_lock);
7705392Smaybee 		return (enospc);
771789Sahrens 	}
772789Sahrens 
773789Sahrens 	/* We need to up our estimated delta before dropping dd_lock */
774789Sahrens 	dd->dd_tempreserved[txgidx] += asize;
775789Sahrens 
7765831Sck153898 	parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
7775831Sck153898 	    asize - ref_rsrv);
778789Sahrens 	mutex_exit(&dd->dd_lock);
779789Sahrens 
7806245Smaybee 	tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
781789Sahrens 	tr->tr_ds = dd;
782789Sahrens 	tr->tr_size = asize;
783789Sahrens 	list_insert_tail(tr_list, tr);
784789Sahrens 
785789Sahrens 	/* see if it's OK with our parent */
7864944Smaybee 	if (dd->dd_parent && parent_rsrv) {
7874944Smaybee 		boolean_t ismos = (dd->dd_phys->dd_head_dataset_obj == 0);
7884944Smaybee 
789789Sahrens 		return (dsl_dir_tempreserve_impl(dd->dd_parent,
7906300Sck153898 		    parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE));
791789Sahrens 	} else {
792789Sahrens 		return (0);
793789Sahrens 	}
794789Sahrens }
795789Sahrens 
796789Sahrens /*
797789Sahrens  * Reserve space in this dsl_dir, to be used in this tx's txg.
7985378Sck153898  * After the space has been dirtied (and dsl_dir_willuse_space()
7995378Sck153898  * has been called), the reservation should be canceled, using
8005378Sck153898  * dsl_dir_tempreserve_clear().
801789Sahrens  */
802789Sahrens int
8035378Sck153898 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
8045378Sck153898     uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
805789Sahrens {
8066245Smaybee 	int err;
807789Sahrens 	list_t *tr_list;
808789Sahrens 
8095378Sck153898 	if (asize == 0) {
8105378Sck153898 		*tr_cookiep = NULL;
8115378Sck153898 		return (0);
8125378Sck153898 	}
8135378Sck153898 
814789Sahrens 	tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
815789Sahrens 	list_create(tr_list, sizeof (struct tempreserve),
816789Sahrens 	    offsetof(struct tempreserve, tr_node));
8175378Sck153898 	ASSERT3S(asize, >, 0);
8181544Seschrock 	ASSERT3S(fsize, >=, 0);
819789Sahrens 
8206245Smaybee 	err = arc_tempreserve_space(lsize, tx->tx_txg);
8216245Smaybee 	if (err == 0) {
8226245Smaybee 		struct tempreserve *tr;
8236245Smaybee 
8246245Smaybee 		tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
8256245Smaybee 		tr->tr_size = lsize;
8266245Smaybee 		list_insert_tail(tr_list, tr);
8276245Smaybee 
8286245Smaybee 		err = dsl_pool_tempreserve_space(dd->dd_pool, asize, tx);
8296245Smaybee 	} else {
8306245Smaybee 		if (err == EAGAIN) {
8316245Smaybee 			txg_delay(dd->dd_pool, tx->tx_txg, 1);
8326245Smaybee 			err = ERESTART;
8336245Smaybee 		}
8346245Smaybee 		dsl_pool_memory_pressure(dd->dd_pool);
8356245Smaybee 	}
836789Sahrens 
837789Sahrens 	if (err == 0) {
838789Sahrens 		struct tempreserve *tr;
839789Sahrens 
8406245Smaybee 		tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
8416245Smaybee 		tr->tr_dp = dd->dd_pool;
8426245Smaybee 		tr->tr_size = asize;
8436245Smaybee 		list_insert_tail(tr_list, tr);
8446245Smaybee 
8456245Smaybee 		err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
8466300Sck153898 		    FALSE, asize > usize, tr_list, tx, TRUE);
847789Sahrens 	}
848789Sahrens 
849789Sahrens 	if (err)
850789Sahrens 		dsl_dir_tempreserve_clear(tr_list, tx);
851789Sahrens 	else
852789Sahrens 		*tr_cookiep = tr_list;
8536245Smaybee 
854789Sahrens 	return (err);
855789Sahrens }
856789Sahrens 
857789Sahrens /*
858789Sahrens  * Clear a temporary reservation that we previously made with
859789Sahrens  * dsl_dir_tempreserve_space().
860789Sahrens  */
861789Sahrens void
862789Sahrens dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
863789Sahrens {
864789Sahrens 	int txgidx = tx->tx_txg & TXG_MASK;
865789Sahrens 	list_t *tr_list = tr_cookie;
866789Sahrens 	struct tempreserve *tr;
867789Sahrens 
868789Sahrens 	ASSERT3U(tx->tx_txg, !=, 0);
869789Sahrens 
8705378Sck153898 	if (tr_cookie == NULL)
8715378Sck153898 		return;
8725378Sck153898 
873789Sahrens 	while (tr = list_head(tr_list)) {
8746245Smaybee 		if (tr->tr_dp) {
8756245Smaybee 			dsl_pool_tempreserve_clear(tr->tr_dp, tr->tr_size, tx);
8766245Smaybee 		} else if (tr->tr_ds) {
877789Sahrens 			mutex_enter(&tr->tr_ds->dd_lock);
878789Sahrens 			ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
879789Sahrens 			    tr->tr_size);
880789Sahrens 			tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
881789Sahrens 			mutex_exit(&tr->tr_ds->dd_lock);
8826245Smaybee 		} else {
8836245Smaybee 			arc_tempreserve_clear(tr->tr_size);
884789Sahrens 		}
885789Sahrens 		list_remove(tr_list, tr);
886789Sahrens 		kmem_free(tr, sizeof (struct tempreserve));
887789Sahrens 	}
888789Sahrens 
889789Sahrens 	kmem_free(tr_list, sizeof (list_t));
890789Sahrens }
891789Sahrens 
8926245Smaybee static void
8936245Smaybee dsl_dir_willuse_space_impl(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
894789Sahrens {
895789Sahrens 	int64_t parent_space;
896789Sahrens 	uint64_t est_used;
897789Sahrens 
898789Sahrens 	mutex_enter(&dd->dd_lock);
899789Sahrens 	if (space > 0)
900789Sahrens 		dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
901789Sahrens 
902*7390SMatthew.Ahrens@Sun.COM 	est_used = dsl_dir_space_towrite(dd) + dd->dd_phys->dd_used_bytes;
903789Sahrens 	parent_space = parent_delta(dd, est_used, space);
904789Sahrens 	mutex_exit(&dd->dd_lock);
905789Sahrens 
906789Sahrens 	/* Make sure that we clean up dd_space_to* */
907789Sahrens 	dsl_dir_dirty(dd, tx);
908789Sahrens 
909789Sahrens 	/* XXX this is potentially expensive and unnecessary... */
910789Sahrens 	if (parent_space && dd->dd_parent)
9116245Smaybee 		dsl_dir_willuse_space_impl(dd->dd_parent, parent_space, tx);
9126245Smaybee }
9136245Smaybee 
9146245Smaybee /*
9156245Smaybee  * Call in open context when we think we're going to write/free space,
9166245Smaybee  * eg. when dirtying data.  Be conservative (ie. OK to write less than
9176245Smaybee  * this or free more than this, but don't write more or free less).
9186245Smaybee  */
9196245Smaybee void
9206245Smaybee dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
9216245Smaybee {
9226245Smaybee 	dsl_pool_willuse_space(dd->dd_pool, space, tx);
9236245Smaybee 	dsl_dir_willuse_space_impl(dd, space, tx);
924789Sahrens }
925789Sahrens 
926789Sahrens /* call from syncing context when we actually write/free space for this dd */
927789Sahrens void
928*7390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
929789Sahrens     int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
930789Sahrens {
931789Sahrens 	int64_t accounted_delta;
932789Sahrens 
933789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
934*7390SMatthew.Ahrens@Sun.COM 	ASSERT(type < DD_USED_NUM);
935789Sahrens 
936789Sahrens 	dsl_dir_dirty(dd, tx);
937789Sahrens 
938789Sahrens 	mutex_enter(&dd->dd_lock);
939*7390SMatthew.Ahrens@Sun.COM 	accounted_delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, used);
940*7390SMatthew.Ahrens@Sun.COM 	ASSERT(used >= 0 || dd->dd_phys->dd_used_bytes >= -used);
941789Sahrens 	ASSERT(compressed >= 0 ||
942789Sahrens 	    dd->dd_phys->dd_compressed_bytes >= -compressed);
943789Sahrens 	ASSERT(uncompressed >= 0 ||
944789Sahrens 	    dd->dd_phys->dd_uncompressed_bytes >= -uncompressed);
945*7390SMatthew.Ahrens@Sun.COM 	dd->dd_phys->dd_used_bytes += used;
946789Sahrens 	dd->dd_phys->dd_uncompressed_bytes += uncompressed;
947789Sahrens 	dd->dd_phys->dd_compressed_bytes += compressed;
948*7390SMatthew.Ahrens@Sun.COM 
949*7390SMatthew.Ahrens@Sun.COM 	if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
950*7390SMatthew.Ahrens@Sun.COM 		ASSERT(used > 0 ||
951*7390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_breakdown[type] >= -used);
952*7390SMatthew.Ahrens@Sun.COM 		dd->dd_phys->dd_used_breakdown[type] += used;
953*7390SMatthew.Ahrens@Sun.COM #ifdef DEBUG
954*7390SMatthew.Ahrens@Sun.COM 		dd_used_t t;
955*7390SMatthew.Ahrens@Sun.COM 		uint64_t u = 0;
956*7390SMatthew.Ahrens@Sun.COM 		for (t = 0; t < DD_USED_NUM; t++)
957*7390SMatthew.Ahrens@Sun.COM 			u += dd->dd_phys->dd_used_breakdown[t];
958*7390SMatthew.Ahrens@Sun.COM 		ASSERT3U(u, ==, dd->dd_phys->dd_used_bytes);
959*7390SMatthew.Ahrens@Sun.COM #endif
960*7390SMatthew.Ahrens@Sun.COM 	}
961789Sahrens 	mutex_exit(&dd->dd_lock);
962789Sahrens 
963789Sahrens 	if (dd->dd_parent != NULL) {
964*7390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
965789Sahrens 		    accounted_delta, compressed, uncompressed, tx);
966*7390SMatthew.Ahrens@Sun.COM 		dsl_dir_transfer_space(dd->dd_parent,
967*7390SMatthew.Ahrens@Sun.COM 		    used - accounted_delta,
968*7390SMatthew.Ahrens@Sun.COM 		    DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
969789Sahrens 	}
970789Sahrens }
971789Sahrens 
972*7390SMatthew.Ahrens@Sun.COM void
973*7390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
974*7390SMatthew.Ahrens@Sun.COM     dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
975*7390SMatthew.Ahrens@Sun.COM {
976*7390SMatthew.Ahrens@Sun.COM 	ASSERT(dmu_tx_is_syncing(tx));
977*7390SMatthew.Ahrens@Sun.COM 	ASSERT(oldtype < DD_USED_NUM);
978*7390SMatthew.Ahrens@Sun.COM 	ASSERT(newtype < DD_USED_NUM);
979*7390SMatthew.Ahrens@Sun.COM 
980*7390SMatthew.Ahrens@Sun.COM 	if (delta == 0 || !(dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN))
981*7390SMatthew.Ahrens@Sun.COM 		return;
982*7390SMatthew.Ahrens@Sun.COM 
983*7390SMatthew.Ahrens@Sun.COM 	dsl_dir_dirty(dd, tx);
984*7390SMatthew.Ahrens@Sun.COM 	mutex_enter(&dd->dd_lock);
985*7390SMatthew.Ahrens@Sun.COM 	ASSERT(delta > 0 ?
986*7390SMatthew.Ahrens@Sun.COM 	    dd->dd_phys->dd_used_breakdown[oldtype] >= delta :
987*7390SMatthew.Ahrens@Sun.COM 	    dd->dd_phys->dd_used_breakdown[newtype] >= -delta);
988*7390SMatthew.Ahrens@Sun.COM 	ASSERT(dd->dd_phys->dd_used_bytes >= ABS(delta));
989*7390SMatthew.Ahrens@Sun.COM 	dd->dd_phys->dd_used_breakdown[oldtype] -= delta;
990*7390SMatthew.Ahrens@Sun.COM 	dd->dd_phys->dd_used_breakdown[newtype] += delta;
991*7390SMatthew.Ahrens@Sun.COM 	mutex_exit(&dd->dd_lock);
992*7390SMatthew.Ahrens@Sun.COM }
993*7390SMatthew.Ahrens@Sun.COM 
994*7390SMatthew.Ahrens@Sun.COM 
995789Sahrens static int
9962199Sahrens dsl_dir_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
997789Sahrens {
9982199Sahrens 	dsl_dir_t *dd = arg1;
9992199Sahrens 	uint64_t *quotap = arg2;
1000789Sahrens 	uint64_t new_quota = *quotap;
1001789Sahrens 	int err = 0;
10022199Sahrens 	uint64_t towrite;
10032199Sahrens 
10042199Sahrens 	if (new_quota == 0)
10052199Sahrens 		return (0);
10062199Sahrens 
10072199Sahrens 	mutex_enter(&dd->dd_lock);
10082199Sahrens 	/*
10092199Sahrens 	 * If we are doing the preliminary check in open context, and
10102199Sahrens 	 * there are pending changes, then don't fail it, since the
10115378Sck153898 	 * pending changes could under-estimate the amount of space to be
10122199Sahrens 	 * freed up.
10132199Sahrens 	 */
10145378Sck153898 	towrite = dsl_dir_space_towrite(dd);
10152199Sahrens 	if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
10162199Sahrens 	    (new_quota < dd->dd_phys->dd_reserved ||
1017*7390SMatthew.Ahrens@Sun.COM 	    new_quota < dd->dd_phys->dd_used_bytes + towrite)) {
10182199Sahrens 		err = ENOSPC;
10192199Sahrens 	}
10202199Sahrens 	mutex_exit(&dd->dd_lock);
10212199Sahrens 	return (err);
10222199Sahrens }
10232199Sahrens 
10244543Smarks /* ARGSUSED */
10252199Sahrens static void
10264543Smarks dsl_dir_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
10272199Sahrens {
10282199Sahrens 	dsl_dir_t *dd = arg1;
10292199Sahrens 	uint64_t *quotap = arg2;
10302199Sahrens 	uint64_t new_quota = *quotap;
1031789Sahrens 
1032789Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1033789Sahrens 
1034789Sahrens 	mutex_enter(&dd->dd_lock);
10352199Sahrens 	dd->dd_phys->dd_quota = new_quota;
1036789Sahrens 	mutex_exit(&dd->dd_lock);
10374543Smarks 
10384543Smarks 	spa_history_internal_log(LOG_DS_QUOTA, dd->dd_pool->dp_spa,
10394543Smarks 	    tx, cr, "%lld dataset = %llu ",
10404543Smarks 	    (longlong_t)new_quota, dd->dd_phys->dd_head_dataset_obj);
1041789Sahrens }
1042789Sahrens 
1043789Sahrens int
1044789Sahrens dsl_dir_set_quota(const char *ddname, uint64_t quota)
1045789Sahrens {
1046789Sahrens 	dsl_dir_t *dd;
1047789Sahrens 	int err;
1048789Sahrens 
10491544Seschrock 	err = dsl_dir_open(ddname, FTAG, &dd, NULL);
10501544Seschrock 	if (err)
10511544Seschrock 		return (err);
1052789Sahrens 
10535481Sck153898 	if (quota != dd->dd_phys->dd_quota) {
10545481Sck153898 		/*
10555481Sck153898 		 * If someone removes a file, then tries to set the quota, we
10565481Sck153898 		 * want to make sure the file freeing takes effect.
10575481Sck153898 		 */
10585481Sck153898 		txg_wait_open(dd->dd_pool, 0);
10595481Sck153898 
10605481Sck153898 		err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_quota_check,
10615481Sck153898 		    dsl_dir_set_quota_sync, dd, &quota, 0);
10625481Sck153898 	}
1063789Sahrens 	dsl_dir_close(dd, FTAG);
1064789Sahrens 	return (err);
1065789Sahrens }
1066789Sahrens 
10675378Sck153898 int
10682199Sahrens dsl_dir_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
1069789Sahrens {
10702199Sahrens 	dsl_dir_t *dd = arg1;
10712199Sahrens 	uint64_t *reservationp = arg2;
1072789Sahrens 	uint64_t new_reservation = *reservationp;
1073789Sahrens 	uint64_t used, avail;
1074789Sahrens 	int64_t delta;
1075789Sahrens 
1076789Sahrens 	if (new_reservation > INT64_MAX)
1077789Sahrens 		return (EOVERFLOW);
1078789Sahrens 
10792199Sahrens 	/*
10802199Sahrens 	 * If we are doing the preliminary check in open context, the
10812199Sahrens 	 * space estimates may be inaccurate.
10822199Sahrens 	 */
10832199Sahrens 	if (!dmu_tx_is_syncing(tx))
10842199Sahrens 		return (0);
10852199Sahrens 
1086789Sahrens 	mutex_enter(&dd->dd_lock);
1087*7390SMatthew.Ahrens@Sun.COM 	used = dd->dd_phys->dd_used_bytes;
1088789Sahrens 	delta = MAX(used, new_reservation) -
1089789Sahrens 	    MAX(used, dd->dd_phys->dd_reserved);
1090789Sahrens 	mutex_exit(&dd->dd_lock);
1091789Sahrens 
1092789Sahrens 	if (dd->dd_parent) {
1093789Sahrens 		avail = dsl_dir_space_available(dd->dd_parent,
1094789Sahrens 		    NULL, 0, FALSE);
1095789Sahrens 	} else {
1096789Sahrens 		avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1097789Sahrens 	}
1098789Sahrens 
1099789Sahrens 	if (delta > 0 && delta > avail)
1100789Sahrens 		return (ENOSPC);
1101789Sahrens 	if (delta > 0 && dd->dd_phys->dd_quota > 0 &&
1102789Sahrens 	    new_reservation > dd->dd_phys->dd_quota)
1103789Sahrens 		return (ENOSPC);
11042199Sahrens 	return (0);
11052199Sahrens }
11062199Sahrens 
11074543Smarks /* ARGSUSED */
11082199Sahrens static void
11094543Smarks dsl_dir_set_reservation_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
11102199Sahrens {
11112199Sahrens 	dsl_dir_t *dd = arg1;
11122199Sahrens 	uint64_t *reservationp = arg2;
11132199Sahrens 	uint64_t new_reservation = *reservationp;
11142199Sahrens 	uint64_t used;
11152199Sahrens 	int64_t delta;
11162199Sahrens 
11175378Sck153898 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
11185378Sck153898 
11192199Sahrens 	mutex_enter(&dd->dd_lock);
1120*7390SMatthew.Ahrens@Sun.COM 	used = dd->dd_phys->dd_used_bytes;
11212199Sahrens 	delta = MAX(used, new_reservation) -
11222199Sahrens 	    MAX(used, dd->dd_phys->dd_reserved);
11235378Sck153898 	dd->dd_phys->dd_reserved = new_reservation;
11242199Sahrens 	mutex_exit(&dd->dd_lock);
1125789Sahrens 
1126789Sahrens 	if (dd->dd_parent != NULL) {
1127789Sahrens 		/* Roll up this additional usage into our ancestors */
1128*7390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1129*7390SMatthew.Ahrens@Sun.COM 		    delta, 0, 0, tx);
1130789Sahrens 	}
11314543Smarks 
11324543Smarks 	spa_history_internal_log(LOG_DS_RESERVATION, dd->dd_pool->dp_spa,
11334543Smarks 	    tx, cr, "%lld dataset = %llu",
11344543Smarks 	    (longlong_t)new_reservation, dd->dd_phys->dd_head_dataset_obj);
1135789Sahrens }
1136789Sahrens 
1137789Sahrens int
1138789Sahrens dsl_dir_set_reservation(const char *ddname, uint64_t reservation)
1139789Sahrens {
1140789Sahrens 	dsl_dir_t *dd;
1141789Sahrens 	int err;
1142789Sahrens 
11431544Seschrock 	err = dsl_dir_open(ddname, FTAG, &dd, NULL);
11441544Seschrock 	if (err)
11451544Seschrock 		return (err);
11462199Sahrens 	err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_reservation_check,
11472199Sahrens 	    dsl_dir_set_reservation_sync, dd, &reservation, 0);
1148789Sahrens 	dsl_dir_close(dd, FTAG);
1149789Sahrens 	return (err);
1150789Sahrens }
1151789Sahrens 
1152789Sahrens static dsl_dir_t *
1153789Sahrens closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1154789Sahrens {
1155789Sahrens 	for (; ds1; ds1 = ds1->dd_parent) {
1156789Sahrens 		dsl_dir_t *dd;
1157789Sahrens 		for (dd = ds2; dd; dd = dd->dd_parent) {
1158789Sahrens 			if (ds1 == dd)
1159789Sahrens 				return (dd);
1160789Sahrens 		}
1161789Sahrens 	}
1162789Sahrens 	return (NULL);
1163789Sahrens }
1164789Sahrens 
1165789Sahrens /*
1166789Sahrens  * If delta is applied to dd, how much of that delta would be applied to
1167789Sahrens  * ancestor?  Syncing context only.
1168789Sahrens  */
1169789Sahrens static int64_t
1170789Sahrens would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1171789Sahrens {
1172789Sahrens 	if (dd == ancestor)
1173789Sahrens 		return (delta);
1174789Sahrens 
1175789Sahrens 	mutex_enter(&dd->dd_lock);
1176*7390SMatthew.Ahrens@Sun.COM 	delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, delta);
1177789Sahrens 	mutex_exit(&dd->dd_lock);
1178789Sahrens 	return (would_change(dd->dd_parent, delta, ancestor));
1179789Sahrens }
1180789Sahrens 
11812199Sahrens struct renamearg {
11822199Sahrens 	dsl_dir_t *newparent;
11832199Sahrens 	const char *mynewname;
11842199Sahrens };
11852199Sahrens 
11864543Smarks /*ARGSUSED*/
11872199Sahrens static int
11882199Sahrens dsl_dir_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
1189789Sahrens {
11902199Sahrens 	dsl_dir_t *dd = arg1;
11912199Sahrens 	struct renamearg *ra = arg2;
1192789Sahrens 	dsl_pool_t *dp = dd->dd_pool;
1193789Sahrens 	objset_t *mos = dp->dp_meta_objset;
11942199Sahrens 	int err;
11952199Sahrens 	uint64_t val;
11962199Sahrens 
11972199Sahrens 	/* There should be 2 references: the open and the dirty */
11982199Sahrens 	if (dmu_buf_refcount(dd->dd_dbuf) > 2)
11992199Sahrens 		return (EBUSY);
1200789Sahrens 
12012199Sahrens 	/* check for existing name */
12022199Sahrens 	err = zap_lookup(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
12032199Sahrens 	    ra->mynewname, 8, 1, &val);
12042199Sahrens 	if (err == 0)
12052199Sahrens 		return (EEXIST);
12062199Sahrens 	if (err != ENOENT)
12071544Seschrock 		return (err);
1208789Sahrens 
12092199Sahrens 	if (ra->newparent != dd->dd_parent) {
12102082Seschrock 		/* is there enough space? */
12112082Seschrock 		uint64_t myspace =
1212*7390SMatthew.Ahrens@Sun.COM 		    MAX(dd->dd_phys->dd_used_bytes, dd->dd_phys->dd_reserved);
1213789Sahrens 
12142199Sahrens 		/* no rename into our descendant */
12152199Sahrens 		if (closest_common_ancestor(dd, ra->newparent) == dd)
1216789Sahrens 			return (EINVAL);
12172199Sahrens 
12182199Sahrens 		if (err = dsl_dir_transfer_possible(dd->dd_parent,
12192199Sahrens 		    ra->newparent, myspace))
12202199Sahrens 			return (err);
12212199Sahrens 	}
12222199Sahrens 
12232199Sahrens 	return (0);
12242199Sahrens }
1225789Sahrens 
12262199Sahrens static void
12274543Smarks dsl_dir_rename_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
12282199Sahrens {
12292199Sahrens 	dsl_dir_t *dd = arg1;
12302199Sahrens 	struct renamearg *ra = arg2;
12312199Sahrens 	dsl_pool_t *dp = dd->dd_pool;
12322199Sahrens 	objset_t *mos = dp->dp_meta_objset;
12332199Sahrens 	int err;
1234789Sahrens 
12352199Sahrens 	ASSERT(dmu_buf_refcount(dd->dd_dbuf) <= 2);
12362199Sahrens 
12372199Sahrens 	if (ra->newparent != dd->dd_parent) {
1238*7390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1239*7390SMatthew.Ahrens@Sun.COM 		    -dd->dd_phys->dd_used_bytes,
1240789Sahrens 		    -dd->dd_phys->dd_compressed_bytes,
1241789Sahrens 		    -dd->dd_phys->dd_uncompressed_bytes, tx);
1242*7390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD,
1243*7390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_bytes,
1244789Sahrens 		    dd->dd_phys->dd_compressed_bytes,
1245789Sahrens 		    dd->dd_phys->dd_uncompressed_bytes, tx);
1246*7390SMatthew.Ahrens@Sun.COM 
1247*7390SMatthew.Ahrens@Sun.COM 		if (dd->dd_phys->dd_reserved > dd->dd_phys->dd_used_bytes) {
1248*7390SMatthew.Ahrens@Sun.COM 			uint64_t unused_rsrv = dd->dd_phys->dd_reserved -
1249*7390SMatthew.Ahrens@Sun.COM 			    dd->dd_phys->dd_used_bytes;
1250*7390SMatthew.Ahrens@Sun.COM 
1251*7390SMatthew.Ahrens@Sun.COM 			dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1252*7390SMatthew.Ahrens@Sun.COM 			    -unused_rsrv, 0, 0, tx);
1253*7390SMatthew.Ahrens@Sun.COM 			dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD_RSRV,
1254*7390SMatthew.Ahrens@Sun.COM 			    unused_rsrv, 0, 0, tx);
1255*7390SMatthew.Ahrens@Sun.COM 		}
1256789Sahrens 	}
1257789Sahrens 
1258789Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1259789Sahrens 
1260789Sahrens 	/* remove from old parent zapobj */
1261789Sahrens 	err = zap_remove(mos, dd->dd_parent->dd_phys->dd_child_dir_zapobj,
1262789Sahrens 	    dd->dd_myname, tx);
1263789Sahrens 	ASSERT3U(err, ==, 0);
1264789Sahrens 
12652199Sahrens 	(void) strcpy(dd->dd_myname, ra->mynewname);
1266789Sahrens 	dsl_dir_close(dd->dd_parent, dd);
12672199Sahrens 	dd->dd_phys->dd_parent_obj = ra->newparent->dd_object;
12681544Seschrock 	VERIFY(0 == dsl_dir_open_obj(dd->dd_pool,
12692199Sahrens 	    ra->newparent->dd_object, NULL, dd, &dd->dd_parent));
1270789Sahrens 
1271789Sahrens 	/* add to new parent zapobj */
12722199Sahrens 	err = zap_add(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
1273789Sahrens 	    dd->dd_myname, 8, 1, &dd->dd_object, tx);
1274789Sahrens 	ASSERT3U(err, ==, 0);
12754543Smarks 
12764543Smarks 	spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa,
12774543Smarks 	    tx, cr, "dataset = %llu", dd->dd_phys->dd_head_dataset_obj);
12782199Sahrens }
1279789Sahrens 
12802199Sahrens int
12812199Sahrens dsl_dir_rename(dsl_dir_t *dd, const char *newname)
12822199Sahrens {
12832199Sahrens 	struct renamearg ra;
12842199Sahrens 	int err;
12852199Sahrens 
12862199Sahrens 	/* new parent should exist */
12872199Sahrens 	err = dsl_dir_open(newname, FTAG, &ra.newparent, &ra.mynewname);
12882199Sahrens 	if (err)
12892199Sahrens 		return (err);
12902199Sahrens 
12912199Sahrens 	/* can't rename to different pool */
12922199Sahrens 	if (dd->dd_pool != ra.newparent->dd_pool) {
12932199Sahrens 		err = ENXIO;
12942199Sahrens 		goto out;
12952199Sahrens 	}
12962199Sahrens 
12972199Sahrens 	/* new name should not already exist */
12982199Sahrens 	if (ra.mynewname == NULL) {
12992199Sahrens 		err = EEXIST;
13002199Sahrens 		goto out;
13012199Sahrens 	}
13022199Sahrens 
13032199Sahrens 	err = dsl_sync_task_do(dd->dd_pool,
13042199Sahrens 	    dsl_dir_rename_check, dsl_dir_rename_sync, dd, &ra, 3);
13052199Sahrens 
13062199Sahrens out:
13072199Sahrens 	dsl_dir_close(ra.newparent, FTAG);
13082199Sahrens 	return (err);
1309789Sahrens }
13102082Seschrock 
13112082Seschrock int
13122082Seschrock dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, uint64_t space)
13132082Seschrock {
13142082Seschrock 	dsl_dir_t *ancestor;
13152082Seschrock 	int64_t adelta;
13162082Seschrock 	uint64_t avail;
13172082Seschrock 
13182082Seschrock 	ancestor = closest_common_ancestor(sdd, tdd);
13192082Seschrock 	adelta = would_change(sdd, -space, ancestor);
13202082Seschrock 	avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
13212082Seschrock 	if (avail < space)
13222082Seschrock 		return (ENOSPC);
13232082Seschrock 
13242082Seschrock 	return (0);
13252082Seschrock }
1326