xref: /onnv-gate/usr/src/uts/common/fs/zfs/dsl_dir.c (revision 11022:63ab26072e41)
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>
3510922SJeff.Bonwick@Sun.COM #include <sys/metaslab.h>
36789Sahrens #include <sys/zap.h>
37789Sahrens #include <sys/zio.h>
38789Sahrens #include <sys/arc.h>
394543Smarks #include <sys/sunddi.h>
40789Sahrens #include "zfs_namecheck.h"
41789Sahrens 
425378Sck153898 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
434543Smarks static void dsl_dir_set_reservation_sync(void *arg1, void *arg2,
444543Smarks     cred_t *cr, dmu_tx_t *tx);
45789Sahrens 
46789Sahrens 
47789Sahrens /* ARGSUSED */
48789Sahrens static void
49789Sahrens dsl_dir_evict(dmu_buf_t *db, void *arg)
50789Sahrens {
51789Sahrens 	dsl_dir_t *dd = arg;
52789Sahrens 	dsl_pool_t *dp = dd->dd_pool;
53789Sahrens 	int t;
54789Sahrens 
55789Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
56789Sahrens 		ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
57789Sahrens 		ASSERT(dd->dd_tempreserved[t] == 0);
58789Sahrens 		ASSERT(dd->dd_space_towrite[t] == 0);
59789Sahrens 	}
60789Sahrens 
61789Sahrens 	if (dd->dd_parent)
62789Sahrens 		dsl_dir_close(dd->dd_parent, dd);
63789Sahrens 
64789Sahrens 	spa_close(dd->dd_pool->dp_spa, dd);
65789Sahrens 
66789Sahrens 	/*
67789Sahrens 	 * The props callback list should be empty since they hold the
68789Sahrens 	 * dir open.
69789Sahrens 	 */
70789Sahrens 	list_destroy(&dd->dd_prop_cbs);
712856Snd150628 	mutex_destroy(&dd->dd_lock);
72789Sahrens 	kmem_free(dd, sizeof (dsl_dir_t));
73789Sahrens }
74789Sahrens 
751544Seschrock int
76789Sahrens dsl_dir_open_obj(dsl_pool_t *dp, uint64_t ddobj,
771544Seschrock     const char *tail, void *tag, dsl_dir_t **ddp)
78789Sahrens {
79789Sahrens 	dmu_buf_t *dbuf;
80789Sahrens 	dsl_dir_t *dd;
811544Seschrock 	int err;
82789Sahrens 
83789Sahrens 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
84789Sahrens 	    dsl_pool_sync_context(dp));
85789Sahrens 
861544Seschrock 	err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
871544Seschrock 	if (err)
881544Seschrock 		return (err);
89789Sahrens 	dd = dmu_buf_get_user(dbuf);
90789Sahrens #ifdef ZFS_DEBUG
91789Sahrens 	{
92789Sahrens 		dmu_object_info_t doi;
93789Sahrens 		dmu_object_info_from_db(dbuf, &doi);
94928Stabriz 		ASSERT3U(doi.doi_type, ==, DMU_OT_DSL_DIR);
957390SMatthew.Ahrens@Sun.COM 		ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
96789Sahrens 	}
97789Sahrens #endif
98789Sahrens 	if (dd == NULL) {
99789Sahrens 		dsl_dir_t *winner;
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 
11110373Schris.kirby@sun.com 		dsl_dir_snap_cmtime_update(dd);
11210373Schris.kirby@sun.com 
113789Sahrens 		if (dd->dd_phys->dd_parent_obj) {
1141544Seschrock 			err = dsl_dir_open_obj(dp, dd->dd_phys->dd_parent_obj,
1151544Seschrock 			    NULL, dd, &dd->dd_parent);
1167390SMatthew.Ahrens@Sun.COM 			if (err)
1177390SMatthew.Ahrens@Sun.COM 				goto errout;
118789Sahrens 			if (tail) {
119789Sahrens #ifdef ZFS_DEBUG
120789Sahrens 				uint64_t foundobj;
121789Sahrens 
122789Sahrens 				err = zap_lookup(dp->dp_meta_objset,
1234577Sahrens 				    dd->dd_parent->dd_phys->dd_child_dir_zapobj,
124789Sahrens 				    tail, sizeof (foundobj), 1, &foundobj);
1251544Seschrock 				ASSERT(err || foundobj == ddobj);
126789Sahrens #endif
127789Sahrens 				(void) strcpy(dd->dd_myname, tail);
128789Sahrens 			} else {
129789Sahrens 				err = zap_value_search(dp->dp_meta_objset,
1304577Sahrens 				    dd->dd_parent->dd_phys->dd_child_dir_zapobj,
1314577Sahrens 				    ddobj, 0, dd->dd_myname);
1321544Seschrock 			}
1337390SMatthew.Ahrens@Sun.COM 			if (err)
1347390SMatthew.Ahrens@Sun.COM 				goto errout;
135789Sahrens 		} else {
136789Sahrens 			(void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
137789Sahrens 		}
138789Sahrens 
139789Sahrens 		winner = dmu_buf_set_user_ie(dbuf, dd, &dd->dd_phys,
140789Sahrens 		    dsl_dir_evict);
141789Sahrens 		if (winner) {
142789Sahrens 			if (dd->dd_parent)
143789Sahrens 				dsl_dir_close(dd->dd_parent, dd);
1442856Snd150628 			mutex_destroy(&dd->dd_lock);
145789Sahrens 			kmem_free(dd, sizeof (dsl_dir_t));
146789Sahrens 			dd = winner;
147789Sahrens 		} else {
148789Sahrens 			spa_open_ref(dp->dp_spa, dd);
149789Sahrens 		}
150789Sahrens 	}
151789Sahrens 
152789Sahrens 	/*
153789Sahrens 	 * The dsl_dir_t has both open-to-close and instantiate-to-evict
154789Sahrens 	 * holds on the spa.  We need the open-to-close holds because
155789Sahrens 	 * otherwise the spa_refcnt wouldn't change when we open a
156789Sahrens 	 * dir which the spa also has open, so we could incorrectly
157789Sahrens 	 * think it was OK to unload/export/destroy the pool.  We need
158789Sahrens 	 * the instantiate-to-evict hold because the dsl_dir_t has a
159789Sahrens 	 * pointer to the dd_pool, which has a pointer to the spa_t.
160789Sahrens 	 */
161789Sahrens 	spa_open_ref(dp->dp_spa, tag);
162789Sahrens 	ASSERT3P(dd->dd_pool, ==, dp);
163789Sahrens 	ASSERT3U(dd->dd_object, ==, ddobj);
164789Sahrens 	ASSERT3P(dd->dd_dbuf, ==, dbuf);
1651544Seschrock 	*ddp = dd;
1661544Seschrock 	return (0);
1677390SMatthew.Ahrens@Sun.COM 
1687390SMatthew.Ahrens@Sun.COM errout:
1697390SMatthew.Ahrens@Sun.COM 	if (dd->dd_parent)
1707390SMatthew.Ahrens@Sun.COM 		dsl_dir_close(dd->dd_parent, dd);
1717390SMatthew.Ahrens@Sun.COM 	mutex_destroy(&dd->dd_lock);
1727390SMatthew.Ahrens@Sun.COM 	kmem_free(dd, sizeof (dsl_dir_t));
1737390SMatthew.Ahrens@Sun.COM 	dmu_buf_rele(dbuf, tag);
1747390SMatthew.Ahrens@Sun.COM 	return (err);
1757390SMatthew.Ahrens@Sun.COM 
176789Sahrens }
177789Sahrens 
178789Sahrens void
179789Sahrens dsl_dir_close(dsl_dir_t *dd, void *tag)
180789Sahrens {
181789Sahrens 	dprintf_dd(dd, "%s\n", "");
182789Sahrens 	spa_close(dd->dd_pool->dp_spa, tag);
1831544Seschrock 	dmu_buf_rele(dd->dd_dbuf, tag);
184789Sahrens }
185789Sahrens 
1862467Sek110237 /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */
187789Sahrens void
188789Sahrens dsl_dir_name(dsl_dir_t *dd, char *buf)
189789Sahrens {
190789Sahrens 	if (dd->dd_parent) {
191789Sahrens 		dsl_dir_name(dd->dd_parent, buf);
192789Sahrens 		(void) strcat(buf, "/");
193789Sahrens 	} else {
194789Sahrens 		buf[0] = '\0';
195789Sahrens 	}
196789Sahrens 	if (!MUTEX_HELD(&dd->dd_lock)) {
197789Sahrens 		/*
198789Sahrens 		 * recursive mutex so that we can use
199789Sahrens 		 * dprintf_dd() with dd_lock held
200789Sahrens 		 */
201789Sahrens 		mutex_enter(&dd->dd_lock);
202789Sahrens 		(void) strcat(buf, dd->dd_myname);
203789Sahrens 		mutex_exit(&dd->dd_lock);
204789Sahrens 	} else {
205789Sahrens 		(void) strcat(buf, dd->dd_myname);
206789Sahrens 	}
207789Sahrens }
208789Sahrens 
2093978Smmusante /* Calculate name legnth, avoiding all the strcat calls of dsl_dir_name */
2103978Smmusante int
2113978Smmusante dsl_dir_namelen(dsl_dir_t *dd)
2123978Smmusante {
2133978Smmusante 	int result = 0;
2143978Smmusante 
2153978Smmusante 	if (dd->dd_parent) {
2163978Smmusante 		/* parent's name + 1 for the "/" */
2173978Smmusante 		result = dsl_dir_namelen(dd->dd_parent) + 1;
2183978Smmusante 	}
2193978Smmusante 
2203978Smmusante 	if (!MUTEX_HELD(&dd->dd_lock)) {
2213978Smmusante 		/* see dsl_dir_name */
2223978Smmusante 		mutex_enter(&dd->dd_lock);
2233978Smmusante 		result += strlen(dd->dd_myname);
2243978Smmusante 		mutex_exit(&dd->dd_lock);
2253978Smmusante 	} else {
2263978Smmusante 		result += strlen(dd->dd_myname);
2273978Smmusante 	}
2283978Smmusante 
2293978Smmusante 	return (result);
2303978Smmusante }
2313978Smmusante 
232789Sahrens static int
233789Sahrens getcomponent(const char *path, char *component, const char **nextp)
234789Sahrens {
235789Sahrens 	char *p;
2368924SRichard.Morris@Sun.COM 	if ((path == NULL) || (path[0] == '\0'))
2372731Snd150628 		return (ENOENT);
238789Sahrens 	/* This would be a good place to reserve some namespace... */
239789Sahrens 	p = strpbrk(path, "/@");
240789Sahrens 	if (p && (p[1] == '/' || p[1] == '@')) {
241789Sahrens 		/* two separators in a row */
242789Sahrens 		return (EINVAL);
243789Sahrens 	}
244789Sahrens 	if (p == NULL || p == path) {
245789Sahrens 		/*
246789Sahrens 		 * if the first thing is an @ or /, it had better be an
247789Sahrens 		 * @ and it had better not have any more ats or slashes,
248789Sahrens 		 * and it had better have something after the @.
249789Sahrens 		 */
250789Sahrens 		if (p != NULL &&
251789Sahrens 		    (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
252789Sahrens 			return (EINVAL);
253789Sahrens 		if (strlen(path) >= MAXNAMELEN)
254789Sahrens 			return (ENAMETOOLONG);
255789Sahrens 		(void) strcpy(component, path);
256789Sahrens 		p = NULL;
257789Sahrens 	} else if (p[0] == '/') {
258789Sahrens 		if (p-path >= MAXNAMELEN)
259789Sahrens 			return (ENAMETOOLONG);
260789Sahrens 		(void) strncpy(component, path, p - path);
261789Sahrens 		component[p-path] = '\0';
262789Sahrens 		p++;
263789Sahrens 	} else if (p[0] == '@') {
264789Sahrens 		/*
265789Sahrens 		 * if the next separator is an @, there better not be
266789Sahrens 		 * any more slashes.
267789Sahrens 		 */
268789Sahrens 		if (strchr(path, '/'))
269789Sahrens 			return (EINVAL);
270789Sahrens 		if (p-path >= MAXNAMELEN)
271789Sahrens 			return (ENAMETOOLONG);
272789Sahrens 		(void) strncpy(component, path, p - path);
273789Sahrens 		component[p-path] = '\0';
274789Sahrens 	} else {
275789Sahrens 		ASSERT(!"invalid p");
276789Sahrens 	}
277789Sahrens 	*nextp = p;
278789Sahrens 	return (0);
279789Sahrens }
280789Sahrens 
281789Sahrens /*
282789Sahrens  * same as dsl_open_dir, ignore the first component of name and use the
283789Sahrens  * spa instead
284789Sahrens  */
2851544Seschrock int
2861544Seschrock dsl_dir_open_spa(spa_t *spa, const char *name, void *tag,
2871544Seschrock     dsl_dir_t **ddp, const char **tailp)
288789Sahrens {
289789Sahrens 	char buf[MAXNAMELEN];
290789Sahrens 	const char *next, *nextnext = NULL;
291789Sahrens 	int err;
292789Sahrens 	dsl_dir_t *dd;
293789Sahrens 	dsl_pool_t *dp;
294789Sahrens 	uint64_t ddobj;
295789Sahrens 	int openedspa = FALSE;
296789Sahrens 
297789Sahrens 	dprintf("%s\n", name);
298789Sahrens 
299789Sahrens 	err = getcomponent(name, buf, &next);
300789Sahrens 	if (err)
3011544Seschrock 		return (err);
302789Sahrens 	if (spa == NULL) {
303789Sahrens 		err = spa_open(buf, &spa, FTAG);
304789Sahrens 		if (err) {
305789Sahrens 			dprintf("spa_open(%s) failed\n", buf);
3061544Seschrock 			return (err);
307789Sahrens 		}
308789Sahrens 		openedspa = TRUE;
309789Sahrens 
310789Sahrens 		/* XXX this assertion belongs in spa_open */
311789Sahrens 		ASSERT(!dsl_pool_sync_context(spa_get_dsl(spa)));
312789Sahrens 	}
313789Sahrens 
314789Sahrens 	dp = spa_get_dsl(spa);
315789Sahrens 
316789Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_READER);
3171544Seschrock 	err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
3181544Seschrock 	if (err) {
3191544Seschrock 		rw_exit(&dp->dp_config_rwlock);
3201544Seschrock 		if (openedspa)
3211544Seschrock 			spa_close(spa, FTAG);
3221544Seschrock 		return (err);
3231544Seschrock 	}
3241544Seschrock 
325789Sahrens 	while (next != NULL) {
326789Sahrens 		dsl_dir_t *child_ds;
327789Sahrens 		err = getcomponent(next, buf, &nextnext);
3281544Seschrock 		if (err)
3291544Seschrock 			break;
330789Sahrens 		ASSERT(next[0] != '\0');
331789Sahrens 		if (next[0] == '@')
332789Sahrens 			break;
333789Sahrens 		dprintf("looking up %s in obj%lld\n",
334789Sahrens 		    buf, dd->dd_phys->dd_child_dir_zapobj);
335789Sahrens 
336789Sahrens 		err = zap_lookup(dp->dp_meta_objset,
337789Sahrens 		    dd->dd_phys->dd_child_dir_zapobj,
338789Sahrens 		    buf, sizeof (ddobj), 1, &ddobj);
3391544Seschrock 		if (err) {
3401544Seschrock 			if (err == ENOENT)
3411544Seschrock 				err = 0;
342789Sahrens 			break;
343789Sahrens 		}
344789Sahrens 
3451544Seschrock 		err = dsl_dir_open_obj(dp, ddobj, buf, tag, &child_ds);
3461544Seschrock 		if (err)
3471544Seschrock 			break;
348789Sahrens 		dsl_dir_close(dd, tag);
349789Sahrens 		dd = child_ds;
350789Sahrens 		next = nextnext;
351789Sahrens 	}
352789Sahrens 	rw_exit(&dp->dp_config_rwlock);
353789Sahrens 
3541544Seschrock 	if (err) {
3551544Seschrock 		dsl_dir_close(dd, tag);
3561544Seschrock 		if (openedspa)
3571544Seschrock 			spa_close(spa, FTAG);
3581544Seschrock 		return (err);
3591544Seschrock 	}
3601544Seschrock 
361789Sahrens 	/*
362789Sahrens 	 * It's an error if there's more than one component left, or
363789Sahrens 	 * tailp==NULL and there's any component left.
364789Sahrens 	 */
365789Sahrens 	if (next != NULL &&
366789Sahrens 	    (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
367789Sahrens 		/* bad path name */
368789Sahrens 		dsl_dir_close(dd, tag);
369789Sahrens 		dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
3701544Seschrock 		err = ENOENT;
371789Sahrens 	}
372789Sahrens 	if (tailp)
373789Sahrens 		*tailp = next;
374789Sahrens 	if (openedspa)
375789Sahrens 		spa_close(spa, FTAG);
3761544Seschrock 	*ddp = dd;
3771544Seschrock 	return (err);
378789Sahrens }
379789Sahrens 
380789Sahrens /*
381789Sahrens  * Return the dsl_dir_t, and possibly the last component which couldn't
382789Sahrens  * be found in *tail.  Return NULL if the path is bogus, or if
383789Sahrens  * tail==NULL and we couldn't parse the whole name.  (*tail)[0] == '@'
384789Sahrens  * means that the last component is a snapshot.
385789Sahrens  */
3861544Seschrock int
3871544Seschrock dsl_dir_open(const char *name, void *tag, dsl_dir_t **ddp, const char **tailp)
388789Sahrens {
3891544Seschrock 	return (dsl_dir_open_spa(NULL, name, tag, ddp, tailp));
390789Sahrens }
391789Sahrens 
3922199Sahrens uint64_t
3937046Sahrens dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
3947046Sahrens     dmu_tx_t *tx)
395789Sahrens {
3967046Sahrens 	objset_t *mos = dp->dp_meta_objset;
397789Sahrens 	uint64_t ddobj;
398789Sahrens 	dsl_dir_phys_t *dsphys;
399789Sahrens 	dmu_buf_t *dbuf;
400789Sahrens 
401928Stabriz 	ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
402928Stabriz 	    DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
4037046Sahrens 	if (pds) {
4047046Sahrens 		VERIFY(0 == zap_add(mos, pds->dd_phys->dd_child_dir_zapobj,
4057046Sahrens 		    name, sizeof (uint64_t), 1, &ddobj, tx));
4067046Sahrens 	} else {
4077046Sahrens 		/* it's the root dir */
4087046Sahrens 		VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
4097046Sahrens 		    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
4107046Sahrens 	}
4111544Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
412789Sahrens 	dmu_buf_will_dirty(dbuf, tx);
413789Sahrens 	dsphys = dbuf->db_data;
414789Sahrens 
415789Sahrens 	dsphys->dd_creation_time = gethrestime_sec();
4167046Sahrens 	if (pds)
4177046Sahrens 		dsphys->dd_parent_obj = pds->dd_object;
418789Sahrens 	dsphys->dd_props_zapobj = zap_create(mos,
419789Sahrens 	    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
420789Sahrens 	dsphys->dd_child_dir_zapobj = zap_create(mos,
421885Sahrens 	    DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
4227390SMatthew.Ahrens@Sun.COM 	if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
4237390SMatthew.Ahrens@Sun.COM 		dsphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
4241544Seschrock 	dmu_buf_rele(dbuf, FTAG);
425789Sahrens 
4262199Sahrens 	return (ddobj);
4272199Sahrens }
4282199Sahrens 
4292199Sahrens /* ARGSUSED */
4302199Sahrens int
4312199Sahrens dsl_dir_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
4322199Sahrens {
433*11022STom.Erickson@Sun.COM 	dsl_dataset_t *ds = arg1;
434*11022STom.Erickson@Sun.COM 	dsl_dir_t *dd = ds->ds_dir;
4352199Sahrens 	dsl_pool_t *dp = dd->dd_pool;
4362199Sahrens 	objset_t *mos = dp->dp_meta_objset;
4372199Sahrens 	int err;
4382199Sahrens 	uint64_t count;
4392199Sahrens 
4402199Sahrens 	/*
4412199Sahrens 	 * There should be exactly two holds, both from
4422199Sahrens 	 * dsl_dataset_destroy: one on the dd directory, and one on its
4432199Sahrens 	 * head ds.  Otherwise, someone is trying to lookup something
4442199Sahrens 	 * inside this dir while we want to destroy it.  The
4452199Sahrens 	 * config_rwlock ensures that nobody else opens it after we
4462199Sahrens 	 * check.
4472199Sahrens 	 */
4482199Sahrens 	if (dmu_buf_refcount(dd->dd_dbuf) > 2)
4492199Sahrens 		return (EBUSY);
4502199Sahrens 
4512199Sahrens 	err = zap_count(mos, dd->dd_phys->dd_child_dir_zapobj, &count);
4522199Sahrens 	if (err)
4532199Sahrens 		return (err);
4542199Sahrens 	if (count != 0)
4552199Sahrens 		return (EEXIST);
456789Sahrens 
457789Sahrens 	return (0);
458789Sahrens }
459789Sahrens 
4602199Sahrens void
4614543Smarks dsl_dir_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx)
462789Sahrens {
463*11022STom.Erickson@Sun.COM 	dsl_dataset_t *ds = arg1;
464*11022STom.Erickson@Sun.COM 	dsl_dir_t *dd = ds->ds_dir;
4652199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
466*11022STom.Erickson@Sun.COM 	dsl_prop_setarg_t psa;
467*11022STom.Erickson@Sun.COM 	uint64_t value = 0;
468*11022STom.Erickson@Sun.COM 	uint64_t obj;
4697390SMatthew.Ahrens@Sun.COM 	dd_used_t t;
470789Sahrens 
4712199Sahrens 	ASSERT(RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock));
472789Sahrens 	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
473789Sahrens 
4742199Sahrens 	/* Remove our reservation. */
475*11022STom.Erickson@Sun.COM 	dsl_prop_setarg_init_uint64(&psa, "reservation",
476*11022STom.Erickson@Sun.COM 	    (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
477*11022STom.Erickson@Sun.COM 	    &value);
478*11022STom.Erickson@Sun.COM 	psa.psa_effective_value = 0;	/* predict default value */
479*11022STom.Erickson@Sun.COM 
480*11022STom.Erickson@Sun.COM 	dsl_dir_set_reservation_sync(ds, &psa, cr, tx);
481*11022STom.Erickson@Sun.COM 
4827390SMatthew.Ahrens@Sun.COM 	ASSERT3U(dd->dd_phys->dd_used_bytes, ==, 0);
483789Sahrens 	ASSERT3U(dd->dd_phys->dd_reserved, ==, 0);
4847390SMatthew.Ahrens@Sun.COM 	for (t = 0; t < DD_USED_NUM; t++)
4857390SMatthew.Ahrens@Sun.COM 		ASSERT3U(dd->dd_phys->dd_used_breakdown[t], ==, 0);
486789Sahrens 
4872199Sahrens 	VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
4882199Sahrens 	VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
4894543Smarks 	VERIFY(0 == dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx));
4902199Sahrens 	VERIFY(0 == zap_remove(mos,
4912199Sahrens 	    dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
492789Sahrens 
4932199Sahrens 	obj = dd->dd_object;
4942199Sahrens 	dsl_dir_close(dd, tag);
4952199Sahrens 	VERIFY(0 == dmu_object_free(mos, obj, tx));
496789Sahrens }
497789Sahrens 
4987046Sahrens boolean_t
4997046Sahrens dsl_dir_is_clone(dsl_dir_t *dd)
500789Sahrens {
5017046Sahrens 	return (dd->dd_phys->dd_origin_obj &&
5027046Sahrens 	    (dd->dd_pool->dp_origin_snap == NULL ||
5037046Sahrens 	    dd->dd_phys->dd_origin_obj !=
5047046Sahrens 	    dd->dd_pool->dp_origin_snap->ds_object));
505789Sahrens }
506789Sahrens 
507789Sahrens void
5082885Sahrens dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
509789Sahrens {
510789Sahrens 	mutex_enter(&dd->dd_lock);
5117390SMatthew.Ahrens@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
5127390SMatthew.Ahrens@Sun.COM 	    dd->dd_phys->dd_used_bytes);
5135378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA, dd->dd_phys->dd_quota);
5142885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
5152885Sahrens 	    dd->dd_phys->dd_reserved);
5162885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
5172885Sahrens 	    dd->dd_phys->dd_compressed_bytes == 0 ? 100 :
5182885Sahrens 	    (dd->dd_phys->dd_uncompressed_bytes * 100 /
5192885Sahrens 	    dd->dd_phys->dd_compressed_bytes));
5207390SMatthew.Ahrens@Sun.COM 	if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
5217390SMatthew.Ahrens@Sun.COM 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
5227390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]);
5237390SMatthew.Ahrens@Sun.COM 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
5247390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_breakdown[DD_USED_HEAD]);
5257390SMatthew.Ahrens@Sun.COM 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
5267390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_breakdown[DD_USED_REFRSRV]);
5277390SMatthew.Ahrens@Sun.COM 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
5287390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_breakdown[DD_USED_CHILD] +
5297390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_breakdown[DD_USED_CHILD_RSRV]);
5307390SMatthew.Ahrens@Sun.COM 	}
531789Sahrens 	mutex_exit(&dd->dd_lock);
532789Sahrens 
5335446Sahrens 	rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
5347046Sahrens 	if (dsl_dir_is_clone(dd)) {
535789Sahrens 		dsl_dataset_t *ds;
5362885Sahrens 		char buf[MAXNAMELEN];
537789Sahrens 
5386689Smaybee 		VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
5396689Smaybee 		    dd->dd_phys->dd_origin_obj, FTAG, &ds));
5402885Sahrens 		dsl_dataset_name(ds, buf);
5416689Smaybee 		dsl_dataset_rele(ds, FTAG);
5422885Sahrens 		dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
543789Sahrens 	}
5445446Sahrens 	rw_exit(&dd->dd_pool->dp_config_rwlock);
545789Sahrens }
546789Sahrens 
547789Sahrens void
548789Sahrens dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
549789Sahrens {
550789Sahrens 	dsl_pool_t *dp = dd->dd_pool;
551789Sahrens 
552789Sahrens 	ASSERT(dd->dd_phys);
553789Sahrens 
554789Sahrens 	if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg) == 0) {
555789Sahrens 		/* up the hold count until we can be written out */
556789Sahrens 		dmu_buf_add_ref(dd->dd_dbuf, dd);
557789Sahrens 	}
558789Sahrens }
559789Sahrens 
560789Sahrens static int64_t
561789Sahrens parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
562789Sahrens {
563789Sahrens 	uint64_t old_accounted = MAX(used, dd->dd_phys->dd_reserved);
564789Sahrens 	uint64_t new_accounted = MAX(used + delta, dd->dd_phys->dd_reserved);
565789Sahrens 	return (new_accounted - old_accounted);
566789Sahrens }
567789Sahrens 
568789Sahrens void
569789Sahrens dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
570789Sahrens {
571789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
572789Sahrens 
573789Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
574789Sahrens 
575789Sahrens 	mutex_enter(&dd->dd_lock);
576789Sahrens 	ASSERT3U(dd->dd_tempreserved[tx->tx_txg&TXG_MASK], ==, 0);
577789Sahrens 	dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
578789Sahrens 	    dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
579789Sahrens 	dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
580789Sahrens 	mutex_exit(&dd->dd_lock);
581789Sahrens 
582789Sahrens 	/* release the hold from dsl_dir_dirty */
5831544Seschrock 	dmu_buf_rele(dd->dd_dbuf, dd);
584789Sahrens }
585789Sahrens 
586789Sahrens static uint64_t
5875378Sck153898 dsl_dir_space_towrite(dsl_dir_t *dd)
588789Sahrens {
5895378Sck153898 	uint64_t space = 0;
590789Sahrens 	int i;
591789Sahrens 
592789Sahrens 	ASSERT(MUTEX_HELD(&dd->dd_lock));
593789Sahrens 
594789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
595789Sahrens 		space += dd->dd_space_towrite[i&TXG_MASK];
596789Sahrens 		ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
597789Sahrens 	}
598789Sahrens 	return (space);
599789Sahrens }
600789Sahrens 
601789Sahrens /*
602789Sahrens  * How much space would dd have available if ancestor had delta applied
603789Sahrens  * to it?  If ondiskonly is set, we're only interested in what's
604789Sahrens  * on-disk, not estimated pending changes.
605789Sahrens  */
6062885Sahrens uint64_t
607789Sahrens dsl_dir_space_available(dsl_dir_t *dd,
608789Sahrens     dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
609789Sahrens {
610789Sahrens 	uint64_t parentspace, myspace, quota, used;
611789Sahrens 
612789Sahrens 	/*
613789Sahrens 	 * If there are no restrictions otherwise, assume we have
614789Sahrens 	 * unlimited space available.
615789Sahrens 	 */
616789Sahrens 	quota = UINT64_MAX;
617789Sahrens 	parentspace = UINT64_MAX;
618789Sahrens 
619789Sahrens 	if (dd->dd_parent != NULL) {
620789Sahrens 		parentspace = dsl_dir_space_available(dd->dd_parent,
621789Sahrens 		    ancestor, delta, ondiskonly);
622789Sahrens 	}
623789Sahrens 
624789Sahrens 	mutex_enter(&dd->dd_lock);
625789Sahrens 	if (dd->dd_phys->dd_quota != 0)
626789Sahrens 		quota = dd->dd_phys->dd_quota;
6277390SMatthew.Ahrens@Sun.COM 	used = dd->dd_phys->dd_used_bytes;
6285378Sck153898 	if (!ondiskonly)
6295378Sck153898 		used += dsl_dir_space_towrite(dd);
630789Sahrens 
631789Sahrens 	if (dd->dd_parent == NULL) {
6322082Seschrock 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
633789Sahrens 		quota = MIN(quota, poolsize);
634789Sahrens 	}
635789Sahrens 
636789Sahrens 	if (dd->dd_phys->dd_reserved > used && parentspace != UINT64_MAX) {
637789Sahrens 		/*
638789Sahrens 		 * We have some space reserved, in addition to what our
639789Sahrens 		 * parent gave us.
640789Sahrens 		 */
641789Sahrens 		parentspace += dd->dd_phys->dd_reserved - used;
642789Sahrens 	}
643789Sahrens 
6447390SMatthew.Ahrens@Sun.COM 	if (dd == ancestor) {
6457390SMatthew.Ahrens@Sun.COM 		ASSERT(delta <= 0);
6467390SMatthew.Ahrens@Sun.COM 		ASSERT(used >= -delta);
6477390SMatthew.Ahrens@Sun.COM 		used += delta;
6487390SMatthew.Ahrens@Sun.COM 		if (parentspace != UINT64_MAX)
6497390SMatthew.Ahrens@Sun.COM 			parentspace -= delta;
6507390SMatthew.Ahrens@Sun.COM 	}
6517390SMatthew.Ahrens@Sun.COM 
652789Sahrens 	if (used > quota) {
653789Sahrens 		/* over quota */
654789Sahrens 		myspace = 0;
6552082Seschrock 
6562082Seschrock 		/*
6572082Seschrock 		 * While it's OK to be a little over quota, if
6582082Seschrock 		 * we think we are using more space than there
6592082Seschrock 		 * is in the pool (which is already 1.6% more than
6602082Seschrock 		 * dsl_pool_adjustedsize()), something is very
6612082Seschrock 		 * wrong.
6622082Seschrock 		 */
66310974SJeff.Bonwick@Sun.COM 		ASSERT3U(used, <=, spa_get_dspace(dd->dd_pool->dp_spa));
664789Sahrens 	} else {
665789Sahrens 		/*
6662082Seschrock 		 * the lesser of the space provided by our parent and
6672082Seschrock 		 * the space left in our quota
668789Sahrens 		 */
669789Sahrens 		myspace = MIN(parentspace, quota - used);
670789Sahrens 	}
671789Sahrens 
672789Sahrens 	mutex_exit(&dd->dd_lock);
673789Sahrens 
674789Sahrens 	return (myspace);
675789Sahrens }
676789Sahrens 
677789Sahrens struct tempreserve {
678789Sahrens 	list_node_t tr_node;
6796245Smaybee 	dsl_pool_t *tr_dp;
680789Sahrens 	dsl_dir_t *tr_ds;
681789Sahrens 	uint64_t tr_size;
682789Sahrens };
683789Sahrens 
684789Sahrens static int
6855378Sck153898 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
6865378Sck153898     boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
6876300Sck153898     dmu_tx_t *tx, boolean_t first)
688789Sahrens {
689789Sahrens 	uint64_t txg = tx->tx_txg;
6905378Sck153898 	uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
69110921STim.Haley@Sun.COM 	uint64_t deferred = 0;
6925378Sck153898 	struct tempreserve *tr;
69310921STim.Haley@Sun.COM 	int retval = EDQUOT;
694789Sahrens 	int txgidx = txg & TXG_MASK;
695789Sahrens 	int i;
6965831Sck153898 	uint64_t ref_rsrv = 0;
697789Sahrens 
698789Sahrens 	ASSERT3U(txg, !=, 0);
6995378Sck153898 	ASSERT3S(asize, >, 0);
700789Sahrens 
701789Sahrens 	mutex_enter(&dd->dd_lock);
7025378Sck153898 
703789Sahrens 	/*
704789Sahrens 	 * Check against the dsl_dir's quota.  We don't add in the delta
705789Sahrens 	 * when checking for over-quota because they get one free hit.
706789Sahrens 	 */
7075378Sck153898 	est_inflight = dsl_dir_space_towrite(dd);
708789Sahrens 	for (i = 0; i < TXG_SIZE; i++)
7095378Sck153898 		est_inflight += dd->dd_tempreserved[i];
7107390SMatthew.Ahrens@Sun.COM 	used_on_disk = dd->dd_phys->dd_used_bytes;
711789Sahrens 
7124709Smaybee 	/*
7136300Sck153898 	 * On the first iteration, fetch the dataset's used-on-disk and
7146300Sck153898 	 * refreservation values. Also, if checkrefquota is set, test if
7156300Sck153898 	 * allocating this space would exceed the dataset's refquota.
7164709Smaybee 	 */
7176300Sck153898 	if (first && tx->tx_objset) {
7185392Smaybee 		int error;
71910298SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
7205392Smaybee 
7215378Sck153898 		error = dsl_dataset_check_quota(ds, checkrefquota,
7225831Sck153898 		    asize, est_inflight, &used_on_disk, &ref_rsrv);
7235378Sck153898 		if (error) {
7245378Sck153898 			mutex_exit(&dd->dd_lock);
7255378Sck153898 			return (error);
7265378Sck153898 		}
7275378Sck153898 	}
7285378Sck153898 
7295378Sck153898 	/*
7305378Sck153898 	 * If this transaction will result in a net free of space,
7315378Sck153898 	 * we want to let it through.
7325378Sck153898 	 */
7335378Sck153898 	if (ignorequota || netfree || dd->dd_phys->dd_quota == 0)
7344709Smaybee 		quota = UINT64_MAX;
7354709Smaybee 	else
736789Sahrens 		quota = dd->dd_phys->dd_quota;
737789Sahrens 
738789Sahrens 	/*
73910921STim.Haley@Sun.COM 	 * Adjust the quota against the actual pool size at the root
74010921STim.Haley@Sun.COM 	 * minus any outstanding deferred frees.
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) {
74910922SJeff.Bonwick@Sun.COM 		spa_t *spa = dd->dd_pool->dp_spa;
750789Sahrens 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
75110922SJeff.Bonwick@Sun.COM 		deferred = metaslab_class_get_deferred(spa_normal_class(spa));
75210921STim.Haley@Sun.COM 		if (poolsize - deferred < quota) {
75310921STim.Haley@Sun.COM 			quota = poolsize - deferred;
75410921STim.Haley@Sun.COM 			retval = ENOSPC;
755789Sahrens 		}
756789Sahrens 	}
757789Sahrens 
758789Sahrens 	/*
759789Sahrens 	 * If they are requesting more space, and our current estimate
7605378Sck153898 	 * is over quota, they get to try again unless the actual
7611544Seschrock 	 * on-disk is over quota and there are no pending changes (which
7621544Seschrock 	 * may free up space for us).
763789Sahrens 	 */
76410921STim.Haley@Sun.COM 	if (used_on_disk + est_inflight >= quota) {
76510921STim.Haley@Sun.COM 		if (est_inflight > 0 || used_on_disk < quota ||
76610921STim.Haley@Sun.COM 		    (retval == ENOSPC && used_on_disk < quota + deferred))
76710921STim.Haley@Sun.COM 			retval = ERESTART;
7685378Sck153898 		dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
769789Sahrens 		    "quota=%lluK tr=%lluK err=%d\n",
7705378Sck153898 		    used_on_disk>>10, est_inflight>>10,
77110921STim.Haley@Sun.COM 		    quota>>10, asize>>10, retval);
772789Sahrens 		mutex_exit(&dd->dd_lock);
77310921STim.Haley@Sun.COM 		return (retval);
774789Sahrens 	}
775789Sahrens 
776789Sahrens 	/* We need to up our estimated delta before dropping dd_lock */
777789Sahrens 	dd->dd_tempreserved[txgidx] += asize;
778789Sahrens 
7795831Sck153898 	parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
7805831Sck153898 	    asize - ref_rsrv);
781789Sahrens 	mutex_exit(&dd->dd_lock);
782789Sahrens 
7836245Smaybee 	tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
784789Sahrens 	tr->tr_ds = dd;
785789Sahrens 	tr->tr_size = asize;
786789Sahrens 	list_insert_tail(tr_list, tr);
787789Sahrens 
788789Sahrens 	/* see if it's OK with our parent */
7894944Smaybee 	if (dd->dd_parent && parent_rsrv) {
7904944Smaybee 		boolean_t ismos = (dd->dd_phys->dd_head_dataset_obj == 0);
7914944Smaybee 
792789Sahrens 		return (dsl_dir_tempreserve_impl(dd->dd_parent,
7936300Sck153898 		    parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE));
794789Sahrens 	} else {
795789Sahrens 		return (0);
796789Sahrens 	}
797789Sahrens }
798789Sahrens 
799789Sahrens /*
800789Sahrens  * Reserve space in this dsl_dir, to be used in this tx's txg.
8015378Sck153898  * After the space has been dirtied (and dsl_dir_willuse_space()
8025378Sck153898  * has been called), the reservation should be canceled, using
8035378Sck153898  * dsl_dir_tempreserve_clear().
804789Sahrens  */
805789Sahrens int
8065378Sck153898 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
8075378Sck153898     uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
808789Sahrens {
8096245Smaybee 	int err;
810789Sahrens 	list_t *tr_list;
811789Sahrens 
8125378Sck153898 	if (asize == 0) {
8135378Sck153898 		*tr_cookiep = NULL;
8145378Sck153898 		return (0);
8155378Sck153898 	}
8165378Sck153898 
817789Sahrens 	tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
818789Sahrens 	list_create(tr_list, sizeof (struct tempreserve),
819789Sahrens 	    offsetof(struct tempreserve, tr_node));
8205378Sck153898 	ASSERT3S(asize, >, 0);
8211544Seschrock 	ASSERT3S(fsize, >=, 0);
822789Sahrens 
8236245Smaybee 	err = arc_tempreserve_space(lsize, tx->tx_txg);
8246245Smaybee 	if (err == 0) {
8256245Smaybee 		struct tempreserve *tr;
8266245Smaybee 
8276245Smaybee 		tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
8286245Smaybee 		tr->tr_size = lsize;
8296245Smaybee 		list_insert_tail(tr_list, tr);
8306245Smaybee 
8316245Smaybee 		err = dsl_pool_tempreserve_space(dd->dd_pool, asize, tx);
8326245Smaybee 	} else {
8336245Smaybee 		if (err == EAGAIN) {
8346245Smaybee 			txg_delay(dd->dd_pool, tx->tx_txg, 1);
8356245Smaybee 			err = ERESTART;
8366245Smaybee 		}
8376245Smaybee 		dsl_pool_memory_pressure(dd->dd_pool);
8386245Smaybee 	}
839789Sahrens 
840789Sahrens 	if (err == 0) {
841789Sahrens 		struct tempreserve *tr;
842789Sahrens 
8436245Smaybee 		tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
8446245Smaybee 		tr->tr_dp = dd->dd_pool;
8456245Smaybee 		tr->tr_size = asize;
8466245Smaybee 		list_insert_tail(tr_list, tr);
8476245Smaybee 
8486245Smaybee 		err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
8496300Sck153898 		    FALSE, asize > usize, tr_list, tx, TRUE);
850789Sahrens 	}
851789Sahrens 
852789Sahrens 	if (err)
853789Sahrens 		dsl_dir_tempreserve_clear(tr_list, tx);
854789Sahrens 	else
855789Sahrens 		*tr_cookiep = tr_list;
8566245Smaybee 
857789Sahrens 	return (err);
858789Sahrens }
859789Sahrens 
860789Sahrens /*
861789Sahrens  * Clear a temporary reservation that we previously made with
862789Sahrens  * dsl_dir_tempreserve_space().
863789Sahrens  */
864789Sahrens void
865789Sahrens dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
866789Sahrens {
867789Sahrens 	int txgidx = tx->tx_txg & TXG_MASK;
868789Sahrens 	list_t *tr_list = tr_cookie;
869789Sahrens 	struct tempreserve *tr;
870789Sahrens 
871789Sahrens 	ASSERT3U(tx->tx_txg, !=, 0);
872789Sahrens 
8735378Sck153898 	if (tr_cookie == NULL)
8745378Sck153898 		return;
8755378Sck153898 
876789Sahrens 	while (tr = list_head(tr_list)) {
8776245Smaybee 		if (tr->tr_dp) {
8786245Smaybee 			dsl_pool_tempreserve_clear(tr->tr_dp, tr->tr_size, tx);
8796245Smaybee 		} else if (tr->tr_ds) {
880789Sahrens 			mutex_enter(&tr->tr_ds->dd_lock);
881789Sahrens 			ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
882789Sahrens 			    tr->tr_size);
883789Sahrens 			tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
884789Sahrens 			mutex_exit(&tr->tr_ds->dd_lock);
8856245Smaybee 		} else {
8866245Smaybee 			arc_tempreserve_clear(tr->tr_size);
887789Sahrens 		}
888789Sahrens 		list_remove(tr_list, tr);
889789Sahrens 		kmem_free(tr, sizeof (struct tempreserve));
890789Sahrens 	}
891789Sahrens 
892789Sahrens 	kmem_free(tr_list, sizeof (list_t));
893789Sahrens }
894789Sahrens 
8956245Smaybee static void
8966245Smaybee dsl_dir_willuse_space_impl(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
897789Sahrens {
898789Sahrens 	int64_t parent_space;
899789Sahrens 	uint64_t est_used;
900789Sahrens 
901789Sahrens 	mutex_enter(&dd->dd_lock);
902789Sahrens 	if (space > 0)
903789Sahrens 		dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
904789Sahrens 
9057390SMatthew.Ahrens@Sun.COM 	est_used = dsl_dir_space_towrite(dd) + dd->dd_phys->dd_used_bytes;
906789Sahrens 	parent_space = parent_delta(dd, est_used, space);
907789Sahrens 	mutex_exit(&dd->dd_lock);
908789Sahrens 
909789Sahrens 	/* Make sure that we clean up dd_space_to* */
910789Sahrens 	dsl_dir_dirty(dd, tx);
911789Sahrens 
912789Sahrens 	/* XXX this is potentially expensive and unnecessary... */
913789Sahrens 	if (parent_space && dd->dd_parent)
9146245Smaybee 		dsl_dir_willuse_space_impl(dd->dd_parent, parent_space, tx);
9156245Smaybee }
9166245Smaybee 
9176245Smaybee /*
9186245Smaybee  * Call in open context when we think we're going to write/free space,
9196245Smaybee  * eg. when dirtying data.  Be conservative (ie. OK to write less than
9206245Smaybee  * this or free more than this, but don't write more or free less).
9216245Smaybee  */
9226245Smaybee void
9236245Smaybee dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
9246245Smaybee {
9256245Smaybee 	dsl_pool_willuse_space(dd->dd_pool, space, tx);
9266245Smaybee 	dsl_dir_willuse_space_impl(dd, space, tx);
927789Sahrens }
928789Sahrens 
929789Sahrens /* call from syncing context when we actually write/free space for this dd */
930789Sahrens void
9317390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
932789Sahrens     int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
933789Sahrens {
934789Sahrens 	int64_t accounted_delta;
9357595SMatthew.Ahrens@Sun.COM 	boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
936789Sahrens 
937789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
9387390SMatthew.Ahrens@Sun.COM 	ASSERT(type < DD_USED_NUM);
939789Sahrens 
940789Sahrens 	dsl_dir_dirty(dd, tx);
941789Sahrens 
9427595SMatthew.Ahrens@Sun.COM 	if (needlock)
9437595SMatthew.Ahrens@Sun.COM 		mutex_enter(&dd->dd_lock);
9447390SMatthew.Ahrens@Sun.COM 	accounted_delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, used);
9457390SMatthew.Ahrens@Sun.COM 	ASSERT(used >= 0 || dd->dd_phys->dd_used_bytes >= -used);
946789Sahrens 	ASSERT(compressed >= 0 ||
947789Sahrens 	    dd->dd_phys->dd_compressed_bytes >= -compressed);
948789Sahrens 	ASSERT(uncompressed >= 0 ||
949789Sahrens 	    dd->dd_phys->dd_uncompressed_bytes >= -uncompressed);
9507390SMatthew.Ahrens@Sun.COM 	dd->dd_phys->dd_used_bytes += used;
951789Sahrens 	dd->dd_phys->dd_uncompressed_bytes += uncompressed;
952789Sahrens 	dd->dd_phys->dd_compressed_bytes += compressed;
9537390SMatthew.Ahrens@Sun.COM 
9547390SMatthew.Ahrens@Sun.COM 	if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
9557390SMatthew.Ahrens@Sun.COM 		ASSERT(used > 0 ||
9567390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_breakdown[type] >= -used);
9577390SMatthew.Ahrens@Sun.COM 		dd->dd_phys->dd_used_breakdown[type] += used;
9587390SMatthew.Ahrens@Sun.COM #ifdef DEBUG
9597390SMatthew.Ahrens@Sun.COM 		dd_used_t t;
9607390SMatthew.Ahrens@Sun.COM 		uint64_t u = 0;
9617390SMatthew.Ahrens@Sun.COM 		for (t = 0; t < DD_USED_NUM; t++)
9627390SMatthew.Ahrens@Sun.COM 			u += dd->dd_phys->dd_used_breakdown[t];
9637390SMatthew.Ahrens@Sun.COM 		ASSERT3U(u, ==, dd->dd_phys->dd_used_bytes);
9647390SMatthew.Ahrens@Sun.COM #endif
9657390SMatthew.Ahrens@Sun.COM 	}
9667595SMatthew.Ahrens@Sun.COM 	if (needlock)
9677595SMatthew.Ahrens@Sun.COM 		mutex_exit(&dd->dd_lock);
968789Sahrens 
969789Sahrens 	if (dd->dd_parent != NULL) {
9707390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
971789Sahrens 		    accounted_delta, compressed, uncompressed, tx);
9727390SMatthew.Ahrens@Sun.COM 		dsl_dir_transfer_space(dd->dd_parent,
9737390SMatthew.Ahrens@Sun.COM 		    used - accounted_delta,
9747390SMatthew.Ahrens@Sun.COM 		    DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
975789Sahrens 	}
976789Sahrens }
977789Sahrens 
9787390SMatthew.Ahrens@Sun.COM void
9797390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
9807390SMatthew.Ahrens@Sun.COM     dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
9817390SMatthew.Ahrens@Sun.COM {
9827595SMatthew.Ahrens@Sun.COM 	boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
9837595SMatthew.Ahrens@Sun.COM 
9847390SMatthew.Ahrens@Sun.COM 	ASSERT(dmu_tx_is_syncing(tx));
9857390SMatthew.Ahrens@Sun.COM 	ASSERT(oldtype < DD_USED_NUM);
9867390SMatthew.Ahrens@Sun.COM 	ASSERT(newtype < DD_USED_NUM);
9877390SMatthew.Ahrens@Sun.COM 
9887390SMatthew.Ahrens@Sun.COM 	if (delta == 0 || !(dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN))
9897390SMatthew.Ahrens@Sun.COM 		return;
9907390SMatthew.Ahrens@Sun.COM 
9917390SMatthew.Ahrens@Sun.COM 	dsl_dir_dirty(dd, tx);
9927595SMatthew.Ahrens@Sun.COM 	if (needlock)
9937595SMatthew.Ahrens@Sun.COM 		mutex_enter(&dd->dd_lock);
9947390SMatthew.Ahrens@Sun.COM 	ASSERT(delta > 0 ?
9957390SMatthew.Ahrens@Sun.COM 	    dd->dd_phys->dd_used_breakdown[oldtype] >= delta :
9967390SMatthew.Ahrens@Sun.COM 	    dd->dd_phys->dd_used_breakdown[newtype] >= -delta);
9977390SMatthew.Ahrens@Sun.COM 	ASSERT(dd->dd_phys->dd_used_bytes >= ABS(delta));
9987390SMatthew.Ahrens@Sun.COM 	dd->dd_phys->dd_used_breakdown[oldtype] -= delta;
9997390SMatthew.Ahrens@Sun.COM 	dd->dd_phys->dd_used_breakdown[newtype] += delta;
10007595SMatthew.Ahrens@Sun.COM 	if (needlock)
10017595SMatthew.Ahrens@Sun.COM 		mutex_exit(&dd->dd_lock);
10027390SMatthew.Ahrens@Sun.COM }
10037390SMatthew.Ahrens@Sun.COM 
1004789Sahrens static int
10052199Sahrens dsl_dir_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
1006789Sahrens {
1007*11022STom.Erickson@Sun.COM 	dsl_dataset_t *ds = arg1;
1008*11022STom.Erickson@Sun.COM 	dsl_dir_t *dd = ds->ds_dir;
1009*11022STom.Erickson@Sun.COM 	dsl_prop_setarg_t *psa = arg2;
1010*11022STom.Erickson@Sun.COM 	int err;
10112199Sahrens 	uint64_t towrite;
10122199Sahrens 
1013*11022STom.Erickson@Sun.COM 	if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
1014*11022STom.Erickson@Sun.COM 		return (err);
1015*11022STom.Erickson@Sun.COM 
1016*11022STom.Erickson@Sun.COM 	if (psa->psa_effective_value == 0)
10172199Sahrens 		return (0);
10182199Sahrens 
10192199Sahrens 	mutex_enter(&dd->dd_lock);
10202199Sahrens 	/*
10212199Sahrens 	 * If we are doing the preliminary check in open context, and
10222199Sahrens 	 * there are pending changes, then don't fail it, since the
10235378Sck153898 	 * pending changes could under-estimate the amount of space to be
10242199Sahrens 	 * freed up.
10252199Sahrens 	 */
10265378Sck153898 	towrite = dsl_dir_space_towrite(dd);
10272199Sahrens 	if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1028*11022STom.Erickson@Sun.COM 	    (psa->psa_effective_value < dd->dd_phys->dd_reserved ||
1029*11022STom.Erickson@Sun.COM 	    psa->psa_effective_value < dd->dd_phys->dd_used_bytes + towrite)) {
10302199Sahrens 		err = ENOSPC;
10312199Sahrens 	}
10322199Sahrens 	mutex_exit(&dd->dd_lock);
10332199Sahrens 	return (err);
10342199Sahrens }
10352199Sahrens 
1036*11022STom.Erickson@Sun.COM extern void dsl_prop_set_sync(void *, void *, cred_t *, dmu_tx_t *);
1037*11022STom.Erickson@Sun.COM 
10384543Smarks /* ARGSUSED */
10392199Sahrens static void
10404543Smarks dsl_dir_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
10412199Sahrens {
1042*11022STom.Erickson@Sun.COM 	dsl_dataset_t *ds = arg1;
1043*11022STom.Erickson@Sun.COM 	dsl_dir_t *dd = ds->ds_dir;
1044*11022STom.Erickson@Sun.COM 	dsl_prop_setarg_t *psa = arg2;
1045*11022STom.Erickson@Sun.COM 	uint64_t effective_value = psa->psa_effective_value;
1046*11022STom.Erickson@Sun.COM 
1047*11022STom.Erickson@Sun.COM 	dsl_prop_set_sync(ds, psa, cr, tx);
1048*11022STom.Erickson@Sun.COM 	DSL_PROP_CHECK_PREDICTION(dd, psa);
1049789Sahrens 
1050789Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1051789Sahrens 
1052789Sahrens 	mutex_enter(&dd->dd_lock);
1053*11022STom.Erickson@Sun.COM 	dd->dd_phys->dd_quota = effective_value;
1054789Sahrens 	mutex_exit(&dd->dd_lock);
10554543Smarks 
10564543Smarks 	spa_history_internal_log(LOG_DS_QUOTA, dd->dd_pool->dp_spa,
10574543Smarks 	    tx, cr, "%lld dataset = %llu ",
1058*11022STom.Erickson@Sun.COM 	    (longlong_t)effective_value, dd->dd_phys->dd_head_dataset_obj);
1059789Sahrens }
1060789Sahrens 
1061789Sahrens int
1062*11022STom.Erickson@Sun.COM dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1063789Sahrens {
1064789Sahrens 	dsl_dir_t *dd;
1065*11022STom.Erickson@Sun.COM 	dsl_dataset_t *ds;
1066*11022STom.Erickson@Sun.COM 	dsl_prop_setarg_t psa;
1067789Sahrens 	int err;
1068789Sahrens 
1069*11022STom.Erickson@Sun.COM 	dsl_prop_setarg_init_uint64(&psa, "quota", source, &quota);
1070*11022STom.Erickson@Sun.COM 
1071*11022STom.Erickson@Sun.COM 	err = dsl_dataset_hold(ddname, FTAG, &ds);
10721544Seschrock 	if (err)
10731544Seschrock 		return (err);
1074789Sahrens 
1075*11022STom.Erickson@Sun.COM 	err = dsl_dir_open(ddname, FTAG, &dd, NULL);
1076*11022STom.Erickson@Sun.COM 	if (err) {
1077*11022STom.Erickson@Sun.COM 		dsl_dataset_rele(ds, FTAG);
1078*11022STom.Erickson@Sun.COM 		return (err);
1079*11022STom.Erickson@Sun.COM 	}
1080*11022STom.Erickson@Sun.COM 
1081*11022STom.Erickson@Sun.COM 	ASSERT(ds->ds_dir == dd);
10825481Sck153898 
1083*11022STom.Erickson@Sun.COM 	/*
1084*11022STom.Erickson@Sun.COM 	 * If someone removes a file, then tries to set the quota, we want to
1085*11022STom.Erickson@Sun.COM 	 * make sure the file freeing takes effect.
1086*11022STom.Erickson@Sun.COM 	 */
1087*11022STom.Erickson@Sun.COM 	txg_wait_open(dd->dd_pool, 0);
1088*11022STom.Erickson@Sun.COM 
1089*11022STom.Erickson@Sun.COM 	err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_quota_check,
1090*11022STom.Erickson@Sun.COM 	    dsl_dir_set_quota_sync, ds, &psa, 0);
1091*11022STom.Erickson@Sun.COM 
1092789Sahrens 	dsl_dir_close(dd, FTAG);
1093*11022STom.Erickson@Sun.COM 	dsl_dataset_rele(ds, FTAG);
1094789Sahrens 	return (err);
1095789Sahrens }
1096789Sahrens 
10975378Sck153898 int
10982199Sahrens dsl_dir_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
1099789Sahrens {
1100*11022STom.Erickson@Sun.COM 	dsl_dataset_t *ds = arg1;
1101*11022STom.Erickson@Sun.COM 	dsl_dir_t *dd = ds->ds_dir;
1102*11022STom.Erickson@Sun.COM 	dsl_prop_setarg_t *psa = arg2;
1103*11022STom.Erickson@Sun.COM 	uint64_t effective_value;
1104789Sahrens 	uint64_t used, avail;
1105*11022STom.Erickson@Sun.COM 	int err;
1106*11022STom.Erickson@Sun.COM 
1107*11022STom.Erickson@Sun.COM 	if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
1108*11022STom.Erickson@Sun.COM 		return (err);
1109*11022STom.Erickson@Sun.COM 
1110*11022STom.Erickson@Sun.COM 	effective_value = psa->psa_effective_value;
1111789Sahrens 
11122199Sahrens 	/*
11132199Sahrens 	 * If we are doing the preliminary check in open context, the
11142199Sahrens 	 * space estimates may be inaccurate.
11152199Sahrens 	 */
11162199Sahrens 	if (!dmu_tx_is_syncing(tx))
11172199Sahrens 		return (0);
11182199Sahrens 
1119789Sahrens 	mutex_enter(&dd->dd_lock);
11207390SMatthew.Ahrens@Sun.COM 	used = dd->dd_phys->dd_used_bytes;
1121789Sahrens 	mutex_exit(&dd->dd_lock);
1122789Sahrens 
1123789Sahrens 	if (dd->dd_parent) {
1124789Sahrens 		avail = dsl_dir_space_available(dd->dd_parent,
1125789Sahrens 		    NULL, 0, FALSE);
1126789Sahrens 	} else {
1127789Sahrens 		avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1128789Sahrens 	}
1129789Sahrens 
1130*11022STom.Erickson@Sun.COM 	if (MAX(used, effective_value) > MAX(used, dd->dd_phys->dd_reserved)) {
1131*11022STom.Erickson@Sun.COM 		uint64_t delta = MAX(used, effective_value) -
11328525SEric.Schrock@Sun.COM 		    MAX(used, dd->dd_phys->dd_reserved);
11338525SEric.Schrock@Sun.COM 
11348525SEric.Schrock@Sun.COM 		if (delta > avail)
11358525SEric.Schrock@Sun.COM 			return (ENOSPC);
11368525SEric.Schrock@Sun.COM 		if (dd->dd_phys->dd_quota > 0 &&
1137*11022STom.Erickson@Sun.COM 		    effective_value > dd->dd_phys->dd_quota)
11388525SEric.Schrock@Sun.COM 			return (ENOSPC);
11398525SEric.Schrock@Sun.COM 	}
11408525SEric.Schrock@Sun.COM 
11412199Sahrens 	return (0);
11422199Sahrens }
11432199Sahrens 
11444543Smarks /* ARGSUSED */
11452199Sahrens static void
11464543Smarks dsl_dir_set_reservation_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
11472199Sahrens {
1148*11022STom.Erickson@Sun.COM 	dsl_dataset_t *ds = arg1;
1149*11022STom.Erickson@Sun.COM 	dsl_dir_t *dd = ds->ds_dir;
1150*11022STom.Erickson@Sun.COM 	dsl_prop_setarg_t *psa = arg2;
1151*11022STom.Erickson@Sun.COM 	uint64_t effective_value = psa->psa_effective_value;
11522199Sahrens 	uint64_t used;
11532199Sahrens 	int64_t delta;
11542199Sahrens 
1155*11022STom.Erickson@Sun.COM 	dsl_prop_set_sync(ds, psa, cr, tx);
1156*11022STom.Erickson@Sun.COM 	DSL_PROP_CHECK_PREDICTION(dd, psa);
1157*11022STom.Erickson@Sun.COM 
11585378Sck153898 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
11595378Sck153898 
11602199Sahrens 	mutex_enter(&dd->dd_lock);
11617390SMatthew.Ahrens@Sun.COM 	used = dd->dd_phys->dd_used_bytes;
1162*11022STom.Erickson@Sun.COM 	delta = MAX(used, effective_value) -
11632199Sahrens 	    MAX(used, dd->dd_phys->dd_reserved);
1164*11022STom.Erickson@Sun.COM 	dd->dd_phys->dd_reserved = effective_value;
1165789Sahrens 
1166789Sahrens 	if (dd->dd_parent != NULL) {
1167789Sahrens 		/* Roll up this additional usage into our ancestors */
11687390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
11697390SMatthew.Ahrens@Sun.COM 		    delta, 0, 0, tx);
1170789Sahrens 	}
11717595SMatthew.Ahrens@Sun.COM 	mutex_exit(&dd->dd_lock);
11724543Smarks 
11734543Smarks 	spa_history_internal_log(LOG_DS_RESERVATION, dd->dd_pool->dp_spa,
11744543Smarks 	    tx, cr, "%lld dataset = %llu",
1175*11022STom.Erickson@Sun.COM 	    (longlong_t)effective_value, dd->dd_phys->dd_head_dataset_obj);
1176789Sahrens }
1177789Sahrens 
1178789Sahrens int
1179*11022STom.Erickson@Sun.COM dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
1180*11022STom.Erickson@Sun.COM     uint64_t reservation)
1181789Sahrens {
1182789Sahrens 	dsl_dir_t *dd;
1183*11022STom.Erickson@Sun.COM 	dsl_dataset_t *ds;
1184*11022STom.Erickson@Sun.COM 	dsl_prop_setarg_t psa;
1185789Sahrens 	int err;
1186789Sahrens 
1187*11022STom.Erickson@Sun.COM 	dsl_prop_setarg_init_uint64(&psa, "reservation", source, &reservation);
1188*11022STom.Erickson@Sun.COM 
1189*11022STom.Erickson@Sun.COM 	err = dsl_dataset_hold(ddname, FTAG, &ds);
1190*11022STom.Erickson@Sun.COM 	if (err)
1191*11022STom.Erickson@Sun.COM 		return (err);
1192*11022STom.Erickson@Sun.COM 
11931544Seschrock 	err = dsl_dir_open(ddname, FTAG, &dd, NULL);
1194*11022STom.Erickson@Sun.COM 	if (err) {
1195*11022STom.Erickson@Sun.COM 		dsl_dataset_rele(ds, FTAG);
11961544Seschrock 		return (err);
1197*11022STom.Erickson@Sun.COM 	}
1198*11022STom.Erickson@Sun.COM 
1199*11022STom.Erickson@Sun.COM 	ASSERT(ds->ds_dir == dd);
1200*11022STom.Erickson@Sun.COM 
12012199Sahrens 	err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_reservation_check,
1202*11022STom.Erickson@Sun.COM 	    dsl_dir_set_reservation_sync, ds, &psa, 0);
1203*11022STom.Erickson@Sun.COM 
1204789Sahrens 	dsl_dir_close(dd, FTAG);
1205*11022STom.Erickson@Sun.COM 	dsl_dataset_rele(ds, FTAG);
1206789Sahrens 	return (err);
1207789Sahrens }
1208789Sahrens 
1209789Sahrens static dsl_dir_t *
1210789Sahrens closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1211789Sahrens {
1212789Sahrens 	for (; ds1; ds1 = ds1->dd_parent) {
1213789Sahrens 		dsl_dir_t *dd;
1214789Sahrens 		for (dd = ds2; dd; dd = dd->dd_parent) {
1215789Sahrens 			if (ds1 == dd)
1216789Sahrens 				return (dd);
1217789Sahrens 		}
1218789Sahrens 	}
1219789Sahrens 	return (NULL);
1220789Sahrens }
1221789Sahrens 
1222789Sahrens /*
1223789Sahrens  * If delta is applied to dd, how much of that delta would be applied to
1224789Sahrens  * ancestor?  Syncing context only.
1225789Sahrens  */
1226789Sahrens static int64_t
1227789Sahrens would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1228789Sahrens {
1229789Sahrens 	if (dd == ancestor)
1230789Sahrens 		return (delta);
1231789Sahrens 
1232789Sahrens 	mutex_enter(&dd->dd_lock);
12337390SMatthew.Ahrens@Sun.COM 	delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, delta);
1234789Sahrens 	mutex_exit(&dd->dd_lock);
1235789Sahrens 	return (would_change(dd->dd_parent, delta, ancestor));
1236789Sahrens }
1237789Sahrens 
12382199Sahrens struct renamearg {
12392199Sahrens 	dsl_dir_t *newparent;
12402199Sahrens 	const char *mynewname;
12412199Sahrens };
12422199Sahrens 
12434543Smarks /*ARGSUSED*/
12442199Sahrens static int
12452199Sahrens dsl_dir_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
1246789Sahrens {
12472199Sahrens 	dsl_dir_t *dd = arg1;
12482199Sahrens 	struct renamearg *ra = arg2;
1249789Sahrens 	dsl_pool_t *dp = dd->dd_pool;
1250789Sahrens 	objset_t *mos = dp->dp_meta_objset;
12512199Sahrens 	int err;
12522199Sahrens 	uint64_t val;
12532199Sahrens 
12542199Sahrens 	/* There should be 2 references: the open and the dirty */
12552199Sahrens 	if (dmu_buf_refcount(dd->dd_dbuf) > 2)
12562199Sahrens 		return (EBUSY);
1257789Sahrens 
12582199Sahrens 	/* check for existing name */
12592199Sahrens 	err = zap_lookup(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
12602199Sahrens 	    ra->mynewname, 8, 1, &val);
12612199Sahrens 	if (err == 0)
12622199Sahrens 		return (EEXIST);
12632199Sahrens 	if (err != ENOENT)
12641544Seschrock 		return (err);
1265789Sahrens 
12662199Sahrens 	if (ra->newparent != dd->dd_parent) {
12672082Seschrock 		/* is there enough space? */
12682082Seschrock 		uint64_t myspace =
12697390SMatthew.Ahrens@Sun.COM 		    MAX(dd->dd_phys->dd_used_bytes, dd->dd_phys->dd_reserved);
1270789Sahrens 
12712199Sahrens 		/* no rename into our descendant */
12722199Sahrens 		if (closest_common_ancestor(dd, ra->newparent) == dd)
1273789Sahrens 			return (EINVAL);
12742199Sahrens 
12752199Sahrens 		if (err = dsl_dir_transfer_possible(dd->dd_parent,
12762199Sahrens 		    ra->newparent, myspace))
12772199Sahrens 			return (err);
12782199Sahrens 	}
12792199Sahrens 
12802199Sahrens 	return (0);
12812199Sahrens }
1282789Sahrens 
12832199Sahrens static void
12844543Smarks dsl_dir_rename_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
12852199Sahrens {
12862199Sahrens 	dsl_dir_t *dd = arg1;
12872199Sahrens 	struct renamearg *ra = arg2;
12882199Sahrens 	dsl_pool_t *dp = dd->dd_pool;
12892199Sahrens 	objset_t *mos = dp->dp_meta_objset;
12902199Sahrens 	int err;
1291789Sahrens 
12922199Sahrens 	ASSERT(dmu_buf_refcount(dd->dd_dbuf) <= 2);
12932199Sahrens 
12942199Sahrens 	if (ra->newparent != dd->dd_parent) {
12957390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
12967390SMatthew.Ahrens@Sun.COM 		    -dd->dd_phys->dd_used_bytes,
1297789Sahrens 		    -dd->dd_phys->dd_compressed_bytes,
1298789Sahrens 		    -dd->dd_phys->dd_uncompressed_bytes, tx);
12997390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD,
13007390SMatthew.Ahrens@Sun.COM 		    dd->dd_phys->dd_used_bytes,
1301789Sahrens 		    dd->dd_phys->dd_compressed_bytes,
1302789Sahrens 		    dd->dd_phys->dd_uncompressed_bytes, tx);
13037390SMatthew.Ahrens@Sun.COM 
13047390SMatthew.Ahrens@Sun.COM 		if (dd->dd_phys->dd_reserved > dd->dd_phys->dd_used_bytes) {
13057390SMatthew.Ahrens@Sun.COM 			uint64_t unused_rsrv = dd->dd_phys->dd_reserved -
13067390SMatthew.Ahrens@Sun.COM 			    dd->dd_phys->dd_used_bytes;
13077390SMatthew.Ahrens@Sun.COM 
13087390SMatthew.Ahrens@Sun.COM 			dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
13097390SMatthew.Ahrens@Sun.COM 			    -unused_rsrv, 0, 0, tx);
13107390SMatthew.Ahrens@Sun.COM 			dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD_RSRV,
13117390SMatthew.Ahrens@Sun.COM 			    unused_rsrv, 0, 0, tx);
13127390SMatthew.Ahrens@Sun.COM 		}
1313789Sahrens 	}
1314789Sahrens 
1315789Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1316789Sahrens 
1317789Sahrens 	/* remove from old parent zapobj */
1318789Sahrens 	err = zap_remove(mos, dd->dd_parent->dd_phys->dd_child_dir_zapobj,
1319789Sahrens 	    dd->dd_myname, tx);
1320789Sahrens 	ASSERT3U(err, ==, 0);
1321789Sahrens 
13222199Sahrens 	(void) strcpy(dd->dd_myname, ra->mynewname);
1323789Sahrens 	dsl_dir_close(dd->dd_parent, dd);
13242199Sahrens 	dd->dd_phys->dd_parent_obj = ra->newparent->dd_object;
13251544Seschrock 	VERIFY(0 == dsl_dir_open_obj(dd->dd_pool,
13262199Sahrens 	    ra->newparent->dd_object, NULL, dd, &dd->dd_parent));
1327789Sahrens 
1328789Sahrens 	/* add to new parent zapobj */
13292199Sahrens 	err = zap_add(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
1330789Sahrens 	    dd->dd_myname, 8, 1, &dd->dd_object, tx);
1331789Sahrens 	ASSERT3U(err, ==, 0);
13324543Smarks 
13334543Smarks 	spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa,
13344543Smarks 	    tx, cr, "dataset = %llu", dd->dd_phys->dd_head_dataset_obj);
13352199Sahrens }
1336789Sahrens 
13372199Sahrens int
13382199Sahrens dsl_dir_rename(dsl_dir_t *dd, const char *newname)
13392199Sahrens {
13402199Sahrens 	struct renamearg ra;
13412199Sahrens 	int err;
13422199Sahrens 
13432199Sahrens 	/* new parent should exist */
13442199Sahrens 	err = dsl_dir_open(newname, FTAG, &ra.newparent, &ra.mynewname);
13452199Sahrens 	if (err)
13462199Sahrens 		return (err);
13472199Sahrens 
13482199Sahrens 	/* can't rename to different pool */
13492199Sahrens 	if (dd->dd_pool != ra.newparent->dd_pool) {
13502199Sahrens 		err = ENXIO;
13512199Sahrens 		goto out;
13522199Sahrens 	}
13532199Sahrens 
13542199Sahrens 	/* new name should not already exist */
13552199Sahrens 	if (ra.mynewname == NULL) {
13562199Sahrens 		err = EEXIST;
13572199Sahrens 		goto out;
13582199Sahrens 	}
13592199Sahrens 
13602199Sahrens 	err = dsl_sync_task_do(dd->dd_pool,
13612199Sahrens 	    dsl_dir_rename_check, dsl_dir_rename_sync, dd, &ra, 3);
13622199Sahrens 
13632199Sahrens out:
13642199Sahrens 	dsl_dir_close(ra.newparent, FTAG);
13652199Sahrens 	return (err);
1366789Sahrens }
13672082Seschrock 
13682082Seschrock int
13692082Seschrock dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, uint64_t space)
13702082Seschrock {
13712082Seschrock 	dsl_dir_t *ancestor;
13722082Seschrock 	int64_t adelta;
13732082Seschrock 	uint64_t avail;
13742082Seschrock 
13752082Seschrock 	ancestor = closest_common_ancestor(sdd, tdd);
13762082Seschrock 	adelta = would_change(sdd, -space, ancestor);
13772082Seschrock 	avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
13782082Seschrock 	if (avail < space)
13792082Seschrock 		return (ENOSPC);
13802082Seschrock 
13812082Seschrock 	return (0);
13822082Seschrock }
138310373Schris.kirby@sun.com 
138410373Schris.kirby@sun.com timestruc_t
138510373Schris.kirby@sun.com dsl_dir_snap_cmtime(dsl_dir_t *dd)
138610373Schris.kirby@sun.com {
138710373Schris.kirby@sun.com 	timestruc_t t;
138810373Schris.kirby@sun.com 
138910373Schris.kirby@sun.com 	mutex_enter(&dd->dd_lock);
139010373Schris.kirby@sun.com 	t = dd->dd_snap_cmtime;
139110373Schris.kirby@sun.com 	mutex_exit(&dd->dd_lock);
139210373Schris.kirby@sun.com 
139310373Schris.kirby@sun.com 	return (t);
139410373Schris.kirby@sun.com }
139510373Schris.kirby@sun.com 
139610373Schris.kirby@sun.com void
139710373Schris.kirby@sun.com dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
139810373Schris.kirby@sun.com {
139910373Schris.kirby@sun.com 	timestruc_t t;
140010373Schris.kirby@sun.com 
140110373Schris.kirby@sun.com 	gethrestime(&t);
140210373Schris.kirby@sun.com 	mutex_enter(&dd->dd_lock);
140310373Schris.kirby@sun.com 	dd->dd_snap_cmtime = t;
140410373Schris.kirby@sun.com 	mutex_exit(&dd->dd_lock);
140510373Schris.kirby@sun.com }
1406