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