xref: /titanic_52/usr/src/uts/common/fs/zfs/dsl_dir.c (revision f498645a3eecf2ddd304b4ea9c7f1b4c155ff79e)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/dmu.h>
29 #include <sys/dmu_tx.h>
30 #include <sys/dsl_dataset.h>
31 #include <sys/dsl_dir.h>
32 #include <sys/dsl_prop.h>
33 #include <sys/spa.h>
34 #include <sys/zap.h>
35 #include <sys/zio.h>
36 #include <sys/arc.h>
37 #include "zfs_namecheck.h"
38 
39 static uint64_t dsl_dir_space_accounted(dsl_dir_t *dd);
40 static uint64_t dsl_dir_estimated_space(dsl_dir_t *dd);
41 static int dsl_dir_set_reservation_sync(dsl_dir_t *dd,
42     void *arg, dmu_tx_t *tx);
43 static uint64_t dsl_dir_space_available(dsl_dir_t *dd,
44     dsl_dir_t *ancestor, int64_t delta, int ondiskonly);
45 
46 
47 /* ARGSUSED */
48 static void
49 dsl_dir_evict(dmu_buf_t *db, void *arg)
50 {
51 	dsl_dir_t *dd = arg;
52 	dsl_pool_t *dp = dd->dd_pool;
53 	int t;
54 
55 	for (t = 0; t < TXG_SIZE; t++) {
56 		ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
57 		ASSERT(dd->dd_tempreserved[t] == 0);
58 		ASSERT(dd->dd_space_towrite[t] == 0);
59 	}
60 
61 	ASSERT3U(dd->dd_used_bytes, ==, dd->dd_phys->dd_used_bytes);
62 
63 	ASSERT(dd->dd_sync_txg == 0);
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 be empty since they hold the
72 	 * dir open.
73 	 */
74 	list_destroy(&dd->dd_prop_cbs);
75 	kmem_free(dd, sizeof (dsl_dir_t));
76 }
77 
78 int
79 dsl_dir_open_obj(dsl_pool_t *dp, uint64_t ddobj,
80     const char *tail, void *tag, dsl_dir_t **ddp)
81 {
82 	dmu_buf_t *dbuf;
83 	dsl_dir_t *dd;
84 	int err;
85 
86 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
87 	    dsl_pool_sync_context(dp));
88 
89 	err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
90 	if (err)
91 		return (err);
92 	dd = dmu_buf_get_user(dbuf);
93 #ifdef ZFS_DEBUG
94 	{
95 		dmu_object_info_t doi;
96 		dmu_object_info_from_db(dbuf, &doi);
97 		ASSERT3U(doi.doi_type, ==, DMU_OT_DSL_DIR);
98 	}
99 #endif
100 	/* XXX assert bonus buffer size is correct */
101 	if (dd == NULL) {
102 		dsl_dir_t *winner;
103 		int err;
104 
105 		dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
106 		dd->dd_object = ddobj;
107 		dd->dd_dbuf = dbuf;
108 		dd->dd_pool = dp;
109 		dd->dd_phys = dbuf->db_data;
110 		dd->dd_used_bytes = dd->dd_phys->dd_used_bytes;
111 
112 		list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t),
113 		    offsetof(dsl_prop_cb_record_t, cbr_node));
114 
115 		if (dd->dd_phys->dd_parent_obj) {
116 			err = dsl_dir_open_obj(dp, dd->dd_phys->dd_parent_obj,
117 			    NULL, dd, &dd->dd_parent);
118 			if (err) {
119 				kmem_free(dd, sizeof (dsl_dir_t));
120 				dmu_buf_rele(dbuf, tag);
121 				return (err);
122 			}
123 			if (tail) {
124 #ifdef ZFS_DEBUG
125 				uint64_t foundobj;
126 
127 				err = zap_lookup(dp->dp_meta_objset,
128 				    dd->dd_parent->dd_phys->
129 				    dd_child_dir_zapobj,
130 				    tail, sizeof (foundobj), 1, &foundobj);
131 				ASSERT(err || foundobj == ddobj);
132 #endif
133 				(void) strcpy(dd->dd_myname, tail);
134 			} else {
135 				err = zap_value_search(dp->dp_meta_objset,
136 				    dd->dd_parent->dd_phys->
137 				    dd_child_dir_zapobj,
138 				    ddobj, dd->dd_myname);
139 			}
140 			if (err) {
141 				dsl_dir_close(dd->dd_parent, dd);
142 				kmem_free(dd, sizeof (dsl_dir_t));
143 				dmu_buf_rele(dbuf, tag);
144 				return (err);
145 			}
146 		} else {
147 			(void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
148 		}
149 
150 		winner = dmu_buf_set_user_ie(dbuf, dd, &dd->dd_phys,
151 		    dsl_dir_evict);
152 		if (winner) {
153 			if (dd->dd_parent)
154 				dsl_dir_close(dd->dd_parent, dd);
155 			kmem_free(dd, sizeof (dsl_dir_t));
156 			dd = winner;
157 		} else {
158 			spa_open_ref(dp->dp_spa, dd);
159 		}
160 	}
161 
162 	/*
163 	 * The dsl_dir_t has both open-to-close and instantiate-to-evict
164 	 * holds on the spa.  We need the open-to-close holds because
165 	 * otherwise the spa_refcnt wouldn't change when we open a
166 	 * dir which the spa also has open, so we could incorrectly
167 	 * think it was OK to unload/export/destroy the pool.  We need
168 	 * the instantiate-to-evict hold because the dsl_dir_t has a
169 	 * pointer to the dd_pool, which has a pointer to the spa_t.
170 	 */
171 	spa_open_ref(dp->dp_spa, tag);
172 	ASSERT3P(dd->dd_pool, ==, dp);
173 	ASSERT3U(dd->dd_object, ==, ddobj);
174 	ASSERT3P(dd->dd_dbuf, ==, dbuf);
175 	*ddp = dd;
176 	return (0);
177 }
178 
179 void
180 dsl_dir_close(dsl_dir_t *dd, void *tag)
181 {
182 	dprintf_dd(dd, "%s\n", "");
183 	spa_close(dd->dd_pool->dp_spa, tag);
184 	dmu_buf_rele(dd->dd_dbuf, tag);
185 }
186 
187 /* buf must be long enough (MAXNAMELEN should do) */
188 void
189 dsl_dir_name(dsl_dir_t *dd, char *buf)
190 {
191 	if (dd->dd_parent) {
192 		dsl_dir_name(dd->dd_parent, buf);
193 		(void) strcat(buf, "/");
194 	} else {
195 		buf[0] = '\0';
196 	}
197 	if (!MUTEX_HELD(&dd->dd_lock)) {
198 		/*
199 		 * recursive mutex so that we can use
200 		 * dprintf_dd() with dd_lock held
201 		 */
202 		mutex_enter(&dd->dd_lock);
203 		(void) strcat(buf, dd->dd_myname);
204 		mutex_exit(&dd->dd_lock);
205 	} else {
206 		(void) strcat(buf, dd->dd_myname);
207 	}
208 }
209 
210 int
211 dsl_dir_is_private(dsl_dir_t *dd)
212 {
213 	int rv = FALSE;
214 
215 	if (dd->dd_parent && dsl_dir_is_private(dd->dd_parent))
216 		rv = TRUE;
217 	if (dataset_name_hidden(dd->dd_myname))
218 		rv = TRUE;
219 	return (rv);
220 }
221 
222 
223 static int
224 getcomponent(const char *path, char *component, const char **nextp)
225 {
226 	char *p;
227 	if (path == NULL)
228 		return (NULL);
229 	/* This would be a good place to reserve some namespace... */
230 	p = strpbrk(path, "/@");
231 	if (p && (p[1] == '/' || p[1] == '@')) {
232 		/* two separators in a row */
233 		return (EINVAL);
234 	}
235 	if (p == NULL || p == path) {
236 		/*
237 		 * if the first thing is an @ or /, it had better be an
238 		 * @ and it had better not have any more ats or slashes,
239 		 * and it had better have something after the @.
240 		 */
241 		if (p != NULL &&
242 		    (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
243 			return (EINVAL);
244 		if (strlen(path) >= MAXNAMELEN)
245 			return (ENAMETOOLONG);
246 		(void) strcpy(component, path);
247 		p = NULL;
248 	} else if (p[0] == '/') {
249 		if (p-path >= MAXNAMELEN)
250 			return (ENAMETOOLONG);
251 		(void) strncpy(component, path, p - path);
252 		component[p-path] = '\0';
253 		p++;
254 	} else if (p[0] == '@') {
255 		/*
256 		 * if the next separator is an @, there better not be
257 		 * any more slashes.
258 		 */
259 		if (strchr(path, '/'))
260 			return (EINVAL);
261 		if (p-path >= MAXNAMELEN)
262 			return (ENAMETOOLONG);
263 		(void) strncpy(component, path, p - path);
264 		component[p-path] = '\0';
265 	} else {
266 		ASSERT(!"invalid p");
267 	}
268 	*nextp = p;
269 	return (0);
270 }
271 
272 /*
273  * same as dsl_open_dir, ignore the first component of name and use the
274  * spa instead
275  */
276 int
277 dsl_dir_open_spa(spa_t *spa, const char *name, void *tag,
278     dsl_dir_t **ddp, const char **tailp)
279 {
280 	char buf[MAXNAMELEN];
281 	const char *next, *nextnext = NULL;
282 	int err;
283 	dsl_dir_t *dd;
284 	dsl_pool_t *dp;
285 	uint64_t ddobj;
286 	int openedspa = FALSE;
287 
288 	dprintf("%s\n", name);
289 
290 	if (name == NULL)
291 		return (ENOENT);
292 	err = getcomponent(name, buf, &next);
293 	if (err)
294 		return (err);
295 	if (spa == NULL) {
296 		err = spa_open(buf, &spa, FTAG);
297 		if (err) {
298 			dprintf("spa_open(%s) failed\n", buf);
299 			return (err);
300 		}
301 		openedspa = TRUE;
302 
303 		/* XXX this assertion belongs in spa_open */
304 		ASSERT(!dsl_pool_sync_context(spa_get_dsl(spa)));
305 	}
306 
307 	dp = spa_get_dsl(spa);
308 
309 	rw_enter(&dp->dp_config_rwlock, RW_READER);
310 	err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
311 	if (err) {
312 		rw_exit(&dp->dp_config_rwlock);
313 		if (openedspa)
314 			spa_close(spa, FTAG);
315 		return (err);
316 	}
317 
318 	while (next != NULL) {
319 		dsl_dir_t *child_ds;
320 		err = getcomponent(next, buf, &nextnext);
321 		if (err)
322 			break;
323 		ASSERT(next[0] != '\0');
324 		if (next[0] == '@')
325 			break;
326 		if (dd->dd_phys->dd_child_dir_zapobj == 0)
327 			break;
328 		dprintf("looking up %s in obj%lld\n",
329 		    buf, dd->dd_phys->dd_child_dir_zapobj);
330 
331 		err = zap_lookup(dp->dp_meta_objset,
332 		    dd->dd_phys->dd_child_dir_zapobj,
333 		    buf, sizeof (ddobj), 1, &ddobj);
334 		if (err) {
335 			if (err == ENOENT)
336 				err = 0;
337 			break;
338 		}
339 
340 		err = dsl_dir_open_obj(dp, ddobj, buf, tag, &child_ds);
341 		if (err)
342 			break;
343 		dsl_dir_close(dd, tag);
344 		dd = child_ds;
345 		next = nextnext;
346 	}
347 	rw_exit(&dp->dp_config_rwlock);
348 
349 	if (err) {
350 		dsl_dir_close(dd, tag);
351 		if (openedspa)
352 			spa_close(spa, FTAG);
353 		return (err);
354 	}
355 
356 	/*
357 	 * It's an error if there's more than one component left, or
358 	 * tailp==NULL and there's any component left.
359 	 */
360 	if (next != NULL &&
361 	    (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
362 		/* bad path name */
363 		dsl_dir_close(dd, tag);
364 		dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
365 		err = ENOENT;
366 	}
367 	if (tailp)
368 		*tailp = next;
369 	if (openedspa)
370 		spa_close(spa, FTAG);
371 	*ddp = dd;
372 	return (err);
373 }
374 
375 /*
376  * Return the dsl_dir_t, and possibly the last component which couldn't
377  * be found in *tail.  Return NULL if the path is bogus, or if
378  * tail==NULL and we couldn't parse the whole name.  (*tail)[0] == '@'
379  * means that the last component is a snapshot.
380  */
381 int
382 dsl_dir_open(const char *name, void *tag, dsl_dir_t **ddp, const char **tailp)
383 {
384 	return (dsl_dir_open_spa(NULL, name, tag, ddp, tailp));
385 }
386 
387 int
388 dsl_dir_create_sync(dsl_dir_t *pds, const char *name, dmu_tx_t *tx)
389 {
390 	objset_t *mos = pds->dd_pool->dp_meta_objset;
391 	uint64_t ddobj;
392 	dsl_dir_phys_t *dsphys;
393 	dmu_buf_t *dbuf;
394 	int err;
395 
396 	ASSERT(dmu_tx_is_syncing(tx));
397 
398 	if (pds->dd_phys->dd_child_dir_zapobj == 0) {
399 		dmu_buf_will_dirty(pds->dd_dbuf, tx);
400 		pds->dd_phys->dd_child_dir_zapobj = zap_create(mos,
401 		    DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
402 	}
403 
404 	rw_enter(&pds->dd_pool->dp_config_rwlock, RW_WRITER);
405 	err = zap_lookup(mos, pds->dd_phys->dd_child_dir_zapobj,
406 	    name, sizeof (uint64_t), 1, &ddobj);
407 	if (err != ENOENT) {
408 		rw_exit(&pds->dd_pool->dp_config_rwlock);
409 		return (err ? err : EEXIST);
410 	}
411 
412 	ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
413 	    DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
414 	err = zap_add(mos, pds->dd_phys->dd_child_dir_zapobj,
415 	    name, sizeof (uint64_t), 1, &ddobj, tx);
416 	ASSERT3U(err, ==, 0);
417 	dprintf("dataset_create: zap_add %s->%lld to %lld returned %d\n",
418 	    name, ddobj, pds->dd_phys->dd_child_dir_zapobj, err);
419 
420 	VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
421 	dmu_buf_will_dirty(dbuf, tx);
422 	dsphys = dbuf->db_data;
423 
424 	dsphys->dd_creation_time = gethrestime_sec();
425 	dsphys->dd_parent_obj = pds->dd_object;
426 	dsphys->dd_props_zapobj = zap_create(mos,
427 	    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
428 	dsphys->dd_child_dir_zapobj = zap_create(mos,
429 	    DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
430 	dmu_buf_rele(dbuf, FTAG);
431 
432 	rw_exit(&pds->dd_pool->dp_config_rwlock);
433 
434 	return (0);
435 }
436 
437 int
438 dsl_dir_destroy_sync(dsl_dir_t *pds, void *arg, dmu_tx_t *tx)
439 {
440 	const char *name = arg;
441 	dsl_dir_t *dd = NULL;
442 	dsl_pool_t *dp = pds->dd_pool;
443 	objset_t *mos = dp->dp_meta_objset;
444 	uint64_t val, obj, child_zapobj, props_zapobj;
445 	int t, err;
446 
447 	rw_enter(&dp->dp_config_rwlock, RW_WRITER);
448 
449 	err = zap_lookup(mos, pds->dd_phys->dd_child_dir_zapobj, name,
450 	    8, 1, &obj);
451 	if (err)
452 		goto out;
453 
454 	err = dsl_dir_open_obj(dp, obj, name, FTAG, &dd);
455 	if (err)
456 		goto out;
457 	ASSERT3U(dd->dd_phys->dd_parent_obj, ==, pds->dd_object);
458 
459 	if (dmu_buf_refcount(dd->dd_dbuf) > 1) {
460 		err = EBUSY;
461 		goto out;
462 	}
463 
464 	for (t = 0; t < TXG_SIZE; t++) {
465 		/*
466 		 * if they were dirty, they'd also be open.
467 		 * dp_config_rwlock ensures that it stays that way.
468 		 */
469 		ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
470 	}
471 
472 	child_zapobj = dd->dd_phys->dd_child_dir_zapobj;
473 	props_zapobj = dd->dd_phys->dd_props_zapobj;
474 
475 	if (child_zapobj != 0) {
476 		uint64_t count;
477 		err = EEXIST;
478 		(void) zap_count(mos, child_zapobj, &count);
479 		if (count != 0)
480 			goto out;
481 	}
482 
483 	if (dd->dd_phys->dd_head_dataset_obj != 0) {
484 		err = dsl_dataset_destroy_sync(dd, NULL, tx);
485 		if (err)
486 			goto out;
487 	}
488 	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
489 
490 	/* The point of no (unsuccessful) return */
491 
492 	/* Make sure parent's used gets updated */
493 	val = 0;
494 	err = dsl_dir_set_reservation_sync(dd, &val, tx);
495 	ASSERT(err == 0);
496 	ASSERT3U(dd->dd_used_bytes, ==, 0);
497 	ASSERT3U(dd->dd_phys->dd_reserved, ==, 0);
498 	dsl_dir_close(dd, FTAG);
499 	dd = NULL;
500 
501 	err = dmu_object_free(mos, obj, tx);
502 	ASSERT(err == 0);
503 
504 	if (child_zapobj)
505 		err = zap_destroy(mos, child_zapobj, tx);
506 	ASSERT(err == 0);
507 
508 	if (props_zapobj)
509 		err = zap_destroy(mos, props_zapobj, tx);
510 	ASSERT(err == 0);
511 
512 	err = zap_remove(mos, pds->dd_phys->dd_child_dir_zapobj, name, tx);
513 	ASSERT(err == 0);
514 
515 out:
516 	rw_exit(&dp->dp_config_rwlock);
517 	if (dd)
518 		dsl_dir_close(dd, FTAG);
519 
520 	return (err);
521 }
522 
523 void
524 dsl_dir_create_root(objset_t *mos, uint64_t *ddobjp, dmu_tx_t *tx)
525 {
526 	dsl_dir_phys_t *dsp;
527 	dmu_buf_t *dbuf;
528 	int error;
529 
530 	*ddobjp = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
531 	    DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
532 
533 	error = zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ROOT_DATASET,
534 	    sizeof (uint64_t), 1, ddobjp, tx);
535 	ASSERT3U(error, ==, 0);
536 
537 	VERIFY(0 == dmu_bonus_hold(mos, *ddobjp, FTAG, &dbuf));
538 	dmu_buf_will_dirty(dbuf, tx);
539 	dsp = dbuf->db_data;
540 
541 	dsp->dd_creation_time = gethrestime_sec();
542 	dsp->dd_props_zapobj = zap_create(mos,
543 	    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
544 	dsp->dd_child_dir_zapobj = zap_create(mos,
545 	    DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
546 
547 	dmu_buf_rele(dbuf, FTAG);
548 }
549 
550 void
551 dsl_dir_stats(dsl_dir_t *dd, dmu_objset_stats_t *dds)
552 {
553 	bzero(dds, sizeof (dmu_objset_stats_t));
554 
555 	dds->dds_available = dsl_dir_space_available(dd, NULL, 0, TRUE);
556 
557 	mutex_enter(&dd->dd_lock);
558 	dds->dds_space_used = dd->dd_used_bytes;
559 	dds->dds_compressed_bytes = dd->dd_phys->dd_compressed_bytes;
560 	dds->dds_uncompressed_bytes = dd->dd_phys->dd_uncompressed_bytes;
561 	dds->dds_quota = dd->dd_phys->dd_quota;
562 	dds->dds_reserved = dd->dd_phys->dd_reserved;
563 	mutex_exit(&dd->dd_lock);
564 
565 	dds->dds_creation_time = dd->dd_phys->dd_creation_time;
566 
567 	if (dd->dd_phys->dd_clone_parent_obj) {
568 		dsl_dataset_t *ds;
569 
570 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
571 		VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool,
572 		    dd->dd_phys->dd_clone_parent_obj,
573 		    NULL, DS_MODE_NONE, FTAG, &ds));
574 		dsl_dataset_name(ds, dds->dds_clone_of);
575 		dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
576 		rw_exit(&dd->dd_pool->dp_config_rwlock);
577 	}
578 }
579 
580 int
581 dsl_dir_sync_task(dsl_dir_t *dd,
582     int (*func)(dsl_dir_t *, void*, dmu_tx_t *), void *arg, uint64_t space)
583 {
584 	dmu_tx_t *tx;
585 	dsl_pool_t *dp = dd->dd_pool;
586 	int err = 0;
587 	uint64_t txg;
588 
589 	dprintf_dd(dd, "func=%p space=%llu\n", func, space);
590 
591 again:
592 	tx = dmu_tx_create_ds(dd);
593 	dmu_tx_hold_space(tx, space);
594 	err = dmu_tx_assign(tx, TXG_WAIT);
595 	if (err == ENOSPC || err == EDQUOT) {
596 		dsl_dir_t *rds;
597 		/*
598 		 * They can get their space from either this dd, or the
599 		 * root dd.
600 		 */
601 		for (rds = dd; rds->dd_parent; rds = rds->dd_parent)
602 			continue;
603 		dmu_tx_abort(tx);
604 		tx = dmu_tx_create_ds(rds);
605 		dmu_tx_hold_space(tx, space);
606 		err = dmu_tx_assign(tx, TXG_WAIT);
607 	}
608 	if (err) {
609 		dmu_tx_abort(tx);
610 		return (err);
611 	}
612 
613 	txg = dmu_tx_get_txg(tx);
614 	mutex_enter(&dd->dd_lock);
615 	if (dd->dd_sync_txg != 0) {
616 		mutex_exit(&dd->dd_lock);
617 		dmu_tx_commit(tx);
618 		txg_wait_synced(dp, 0);
619 		goto again;
620 	}
621 
622 	/* We're good to go */
623 
624 	dd->dd_sync_txg = txg;
625 	dd->dd_sync_func = func;
626 	dd->dd_sync_arg = arg;
627 
628 	mutex_exit(&dd->dd_lock);
629 
630 	dsl_dir_dirty(dd, tx);
631 	dmu_tx_commit(tx);
632 
633 	txg_wait_synced(dp, txg);
634 
635 	mutex_enter(&dd->dd_lock);
636 	ASSERT(dd->dd_sync_txg == txg);
637 	ASSERT(dd->dd_sync_func == NULL);
638 	err = dd->dd_sync_err;
639 	dd->dd_sync_txg = 0;
640 	mutex_exit(&dd->dd_lock);
641 
642 	return (err);
643 }
644 
645 void
646 dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
647 {
648 	dsl_pool_t *dp = dd->dd_pool;
649 
650 	ASSERT(dd->dd_phys);
651 
652 	if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg) == 0) {
653 		/* up the hold count until we can be written out */
654 		dmu_buf_add_ref(dd->dd_dbuf, dd);
655 	}
656 }
657 
658 static int64_t
659 parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
660 {
661 	uint64_t old_accounted = MAX(used, dd->dd_phys->dd_reserved);
662 	uint64_t new_accounted = MAX(used + delta, dd->dd_phys->dd_reserved);
663 	return (new_accounted - old_accounted);
664 }
665 
666 void
667 dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
668 {
669 	if (dd->dd_sync_txg == tx->tx_txg && dd->dd_sync_func) {
670 		dd->dd_sync_err = dd->dd_sync_func(dd, dd->dd_sync_arg, tx);
671 		dd->dd_sync_func = NULL;
672 	}
673 
674 	ASSERT(dmu_tx_is_syncing(tx));
675 
676 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
677 
678 	mutex_enter(&dd->dd_lock);
679 	ASSERT3U(dd->dd_tempreserved[tx->tx_txg&TXG_MASK], ==, 0);
680 	dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
681 	    dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
682 	dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
683 	dd->dd_phys->dd_used_bytes = dd->dd_used_bytes;
684 	mutex_exit(&dd->dd_lock);
685 
686 	/* release the hold from dsl_dir_dirty */
687 	dmu_buf_rele(dd->dd_dbuf, dd);
688 }
689 
690 static uint64_t
691 dsl_dir_estimated_space(dsl_dir_t *dd)
692 {
693 	int64_t space;
694 	int i;
695 
696 	ASSERT(MUTEX_HELD(&dd->dd_lock));
697 
698 	space = dd->dd_phys->dd_used_bytes;
699 	ASSERT(space >= 0);
700 	for (i = 0; i < TXG_SIZE; i++) {
701 		space += dd->dd_space_towrite[i&TXG_MASK];
702 		ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
703 	}
704 	return (space);
705 }
706 
707 /*
708  * How much space would dd have available if ancestor had delta applied
709  * to it?  If ondiskonly is set, we're only interested in what's
710  * on-disk, not estimated pending changes.
711  */
712 static uint64_t
713 dsl_dir_space_available(dsl_dir_t *dd,
714     dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
715 {
716 	uint64_t parentspace, myspace, quota, used;
717 
718 	/*
719 	 * If there are no restrictions otherwise, assume we have
720 	 * unlimited space available.
721 	 */
722 	quota = UINT64_MAX;
723 	parentspace = UINT64_MAX;
724 
725 	if (dd->dd_parent != NULL) {
726 		parentspace = dsl_dir_space_available(dd->dd_parent,
727 		    ancestor, delta, ondiskonly);
728 	}
729 
730 	mutex_enter(&dd->dd_lock);
731 	if (dd->dd_phys->dd_quota != 0)
732 		quota = dd->dd_phys->dd_quota;
733 	if (ondiskonly) {
734 		used = dd->dd_used_bytes;
735 	} else {
736 		used = dsl_dir_estimated_space(dd);
737 	}
738 	if (dd == ancestor)
739 		used += delta;
740 
741 	if (dd->dd_parent == NULL) {
742 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
743 		quota = MIN(quota, poolsize);
744 	}
745 
746 	if (dd->dd_phys->dd_reserved > used && parentspace != UINT64_MAX) {
747 		/*
748 		 * We have some space reserved, in addition to what our
749 		 * parent gave us.
750 		 */
751 		parentspace += dd->dd_phys->dd_reserved - used;
752 	}
753 
754 	if (used > quota) {
755 		/* over quota */
756 		myspace = 0;
757 
758 		/*
759 		 * While it's OK to be a little over quota, if
760 		 * we think we are using more space than there
761 		 * is in the pool (which is already 1.6% more than
762 		 * dsl_pool_adjustedsize()), something is very
763 		 * wrong.
764 		 */
765 		ASSERT3U(used, <=, spa_get_space(dd->dd_pool->dp_spa));
766 	} else {
767 		/*
768 		 * the lesser of the space provided by our parent and
769 		 * the space left in our quota
770 		 */
771 		myspace = MIN(parentspace, quota - used);
772 	}
773 
774 	mutex_exit(&dd->dd_lock);
775 
776 	return (myspace);
777 }
778 
779 struct tempreserve {
780 	list_node_t tr_node;
781 	dsl_dir_t *tr_ds;
782 	uint64_t tr_size;
783 };
784 
785 /*
786  * Reserve space in this dsl_dir, to be used in this tx's txg.
787  * After the space has been dirtied (and thus
788  * dsl_dir_willuse_space() has been called), the reservation should
789  * be canceled, using dsl_dir_tempreserve_clear().
790  */
791 static int
792 dsl_dir_tempreserve_impl(dsl_dir_t *dd,
793     uint64_t asize, boolean_t netfree, list_t *tr_list, dmu_tx_t *tx)
794 {
795 	uint64_t txg = tx->tx_txg;
796 	uint64_t est_used, quota, parent_rsrv;
797 	int edquot = EDQUOT;
798 	int txgidx = txg & TXG_MASK;
799 	int i;
800 	struct tempreserve *tr;
801 
802 	ASSERT3U(txg, !=, 0);
803 	ASSERT3S(asize, >=, 0);
804 
805 	mutex_enter(&dd->dd_lock);
806 	/*
807 	 * Check against the dsl_dir's quota.  We don't add in the delta
808 	 * when checking for over-quota because they get one free hit.
809 	 */
810 	est_used = dsl_dir_estimated_space(dd);
811 	for (i = 0; i < TXG_SIZE; i++)
812 		est_used += dd->dd_tempreserved[i];
813 
814 	quota = UINT64_MAX;
815 
816 	if (dd->dd_phys->dd_quota)
817 		quota = dd->dd_phys->dd_quota;
818 
819 	/*
820 	 * If this transaction will result in a net free of space, we want
821 	 * to let it through, but we have to be careful: the space that it
822 	 * frees won't become available until *after* this txg syncs.
823 	 * Therefore, to ensure that it's possible to remove files from
824 	 * a full pool without inducing transient overcommits, we throttle
825 	 * netfree transactions against a quota that is slightly larger,
826 	 * but still within the pool's allocation slop.  In cases where
827 	 * we're very close to full, this will allow a steady trickle of
828 	 * removes to get through.
829 	 */
830 	if (dd->dd_parent == NULL) {
831 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
832 		if (poolsize < quota) {
833 			quota = poolsize;
834 			edquot = ENOSPC;
835 		}
836 	} else if (netfree) {
837 		quota = UINT64_MAX;
838 	}
839 
840 	/*
841 	 * If they are requesting more space, and our current estimate
842 	 * is over quota.  They get to try again unless the actual
843 	 * on-disk is over quota and there are no pending changes (which
844 	 * may free up space for us).
845 	 */
846 	if (asize > 0 && est_used > quota) {
847 		if (dd->dd_space_towrite[txg & TXG_MASK] != 0 ||
848 		    dd->dd_space_towrite[(txg-1) & TXG_MASK] != 0 ||
849 		    dd->dd_space_towrite[(txg-2) & TXG_MASK] != 0 ||
850 		    dd->dd_used_bytes < quota)
851 			edquot = ERESTART;
852 		dprintf_dd(dd, "failing: used=%lluK est_used = %lluK "
853 		    "quota=%lluK tr=%lluK err=%d\n",
854 		    dd->dd_used_bytes>>10, est_used>>10,
855 		    quota>>10, asize>>10, edquot);
856 		mutex_exit(&dd->dd_lock);
857 		return (edquot);
858 	}
859 
860 	/* We need to up our estimated delta before dropping dd_lock */
861 	dd->dd_tempreserved[txgidx] += asize;
862 
863 	parent_rsrv = parent_delta(dd, est_used, asize);
864 	mutex_exit(&dd->dd_lock);
865 
866 	tr = kmem_alloc(sizeof (struct tempreserve), KM_SLEEP);
867 	tr->tr_ds = dd;
868 	tr->tr_size = asize;
869 	list_insert_tail(tr_list, tr);
870 
871 	/* see if it's OK with our parent */
872 	if (dd->dd_parent && parent_rsrv) {
873 		return (dsl_dir_tempreserve_impl(dd->dd_parent,
874 		    parent_rsrv, netfree, tr_list, tx));
875 	} else {
876 		return (0);
877 	}
878 }
879 
880 /*
881  * Reserve space in this dsl_dir, to be used in this tx's txg.
882  * After the space has been dirtied (and thus
883  * dsl_dir_willuse_space() has been called), the reservation should
884  * be canceled, using dsl_dir_tempreserve_clear().
885  */
886 int
887 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize,
888     uint64_t asize, uint64_t fsize, void **tr_cookiep, dmu_tx_t *tx)
889 {
890 	int err = 0;
891 	list_t *tr_list;
892 
893 	tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
894 	list_create(tr_list, sizeof (struct tempreserve),
895 	    offsetof(struct tempreserve, tr_node));
896 	ASSERT3S(asize, >=, 0);
897 	ASSERT3S(fsize, >=, 0);
898 
899 	err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
900 	    tr_list, tx);
901 
902 	if (err == 0) {
903 		struct tempreserve *tr;
904 
905 		err = arc_tempreserve_space(lsize);
906 		if (err == 0) {
907 			tr = kmem_alloc(sizeof (struct tempreserve), KM_SLEEP);
908 			tr->tr_ds = NULL;
909 			tr->tr_size = lsize;
910 			list_insert_tail(tr_list, tr);
911 		}
912 	}
913 
914 	if (err)
915 		dsl_dir_tempreserve_clear(tr_list, tx);
916 	else
917 		*tr_cookiep = tr_list;
918 	return (err);
919 }
920 
921 /*
922  * Clear a temporary reservation that we previously made with
923  * dsl_dir_tempreserve_space().
924  */
925 void
926 dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
927 {
928 	int txgidx = tx->tx_txg & TXG_MASK;
929 	list_t *tr_list = tr_cookie;
930 	struct tempreserve *tr;
931 
932 	ASSERT3U(tx->tx_txg, !=, 0);
933 
934 	while (tr = list_head(tr_list)) {
935 		if (tr->tr_ds == NULL) {
936 			arc_tempreserve_clear(tr->tr_size);
937 		} else {
938 			mutex_enter(&tr->tr_ds->dd_lock);
939 			ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
940 			    tr->tr_size);
941 			tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
942 			mutex_exit(&tr->tr_ds->dd_lock);
943 		}
944 		list_remove(tr_list, tr);
945 		kmem_free(tr, sizeof (struct tempreserve));
946 	}
947 
948 	kmem_free(tr_list, sizeof (list_t));
949 }
950 
951 /*
952  * Call in open context when we think we're going to write/free space,
953  * eg. when dirtying data.  Be conservative (ie. OK to write less than
954  * this or free more than this, but don't write more or free less).
955  */
956 void
957 dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
958 {
959 	int64_t parent_space;
960 	uint64_t est_used;
961 
962 	mutex_enter(&dd->dd_lock);
963 	if (space > 0)
964 		dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
965 
966 	est_used = dsl_dir_estimated_space(dd);
967 	parent_space = parent_delta(dd, est_used, space);
968 	mutex_exit(&dd->dd_lock);
969 
970 	/* Make sure that we clean up dd_space_to* */
971 	dsl_dir_dirty(dd, tx);
972 
973 	/* XXX this is potentially expensive and unnecessary... */
974 	if (parent_space && dd->dd_parent)
975 		dsl_dir_willuse_space(dd->dd_parent, parent_space, tx);
976 }
977 
978 /* call from syncing context when we actually write/free space for this dd */
979 void
980 dsl_dir_diduse_space(dsl_dir_t *dd,
981     int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
982 {
983 	int64_t accounted_delta;
984 
985 	ASSERT(dmu_tx_is_syncing(tx));
986 
987 	dsl_dir_dirty(dd, tx);
988 
989 	mutex_enter(&dd->dd_lock);
990 	accounted_delta = parent_delta(dd, dd->dd_used_bytes, used);
991 	ASSERT(used >= 0 || dd->dd_used_bytes >= -used);
992 	ASSERT(compressed >= 0 ||
993 	    dd->dd_phys->dd_compressed_bytes >= -compressed);
994 	ASSERT(uncompressed >= 0 ||
995 	    dd->dd_phys->dd_uncompressed_bytes >= -uncompressed);
996 	dd->dd_used_bytes += used;
997 	dd->dd_phys->dd_uncompressed_bytes += uncompressed;
998 	dd->dd_phys->dd_compressed_bytes += compressed;
999 	mutex_exit(&dd->dd_lock);
1000 
1001 	if (dd->dd_parent != NULL) {
1002 		dsl_dir_diduse_space(dd->dd_parent,
1003 		    accounted_delta, compressed, uncompressed, tx);
1004 	}
1005 }
1006 
1007 static int
1008 dsl_dir_set_quota_sync(dsl_dir_t *dd, void *arg, dmu_tx_t *tx)
1009 {
1010 	uint64_t *quotap = arg;
1011 	uint64_t new_quota = *quotap;
1012 	int err = 0;
1013 
1014 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1015 
1016 	mutex_enter(&dd->dd_lock);
1017 	if (new_quota != 0 && (new_quota < dd->dd_phys->dd_reserved ||
1018 	    new_quota < dsl_dir_estimated_space(dd))) {
1019 		err = ENOSPC;
1020 	} else {
1021 		dd->dd_phys->dd_quota = new_quota;
1022 	}
1023 	mutex_exit(&dd->dd_lock);
1024 	return (err);
1025 }
1026 
1027 int
1028 dsl_dir_set_quota(const char *ddname, uint64_t quota)
1029 {
1030 	dsl_dir_t *dd;
1031 	int err;
1032 
1033 	err = dsl_dir_open(ddname, FTAG, &dd, NULL);
1034 	if (err)
1035 		return (err);
1036 	/*
1037 	 * If someone removes a file, then tries to set the quota, we
1038 	 * want to make sure the file freeing takes effect.
1039 	 */
1040 	txg_wait_open(dd->dd_pool, 0);
1041 
1042 	err = dsl_dir_sync_task(dd, dsl_dir_set_quota_sync, &quota, 0);
1043 	dsl_dir_close(dd, FTAG);
1044 	return (err);
1045 }
1046 
1047 static int
1048 dsl_dir_set_reservation_sync(dsl_dir_t *dd, void *arg, dmu_tx_t *tx)
1049 {
1050 	uint64_t *reservationp = arg;
1051 	uint64_t new_reservation = *reservationp;
1052 	uint64_t used, avail;
1053 	int64_t delta;
1054 
1055 	if (new_reservation > INT64_MAX)
1056 		return (EOVERFLOW);
1057 
1058 	mutex_enter(&dd->dd_lock);
1059 	used = dd->dd_used_bytes;
1060 	delta = MAX(used, new_reservation) -
1061 	    MAX(used, dd->dd_phys->dd_reserved);
1062 	mutex_exit(&dd->dd_lock);
1063 
1064 	if (dd->dd_parent) {
1065 		avail = dsl_dir_space_available(dd->dd_parent,
1066 		    NULL, 0, FALSE);
1067 	} else {
1068 		avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1069 	}
1070 
1071 	if (delta > 0 && delta > avail)
1072 		return (ENOSPC);
1073 	if (delta > 0 && dd->dd_phys->dd_quota > 0 &&
1074 	    new_reservation > dd->dd_phys->dd_quota)
1075 		return (ENOSPC);
1076 
1077 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1078 	dd->dd_phys->dd_reserved = new_reservation;
1079 
1080 	if (dd->dd_parent != NULL) {
1081 		/* Roll up this additional usage into our ancestors */
1082 		dsl_dir_diduse_space(dd->dd_parent, delta, 0, 0, tx);
1083 	}
1084 	return (0);
1085 }
1086 
1087 int
1088 dsl_dir_set_reservation(const char *ddname, uint64_t reservation)
1089 {
1090 	dsl_dir_t *dd;
1091 	int err;
1092 
1093 	err = dsl_dir_open(ddname, FTAG, &dd, NULL);
1094 	if (err)
1095 		return (err);
1096 	err = dsl_dir_sync_task(dd,
1097 	    dsl_dir_set_reservation_sync, &reservation, 0);
1098 	dsl_dir_close(dd, FTAG);
1099 	return (err);
1100 }
1101 
1102 static dsl_dir_t *
1103 closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1104 {
1105 	for (; ds1; ds1 = ds1->dd_parent) {
1106 		dsl_dir_t *dd;
1107 		for (dd = ds2; dd; dd = dd->dd_parent) {
1108 			if (ds1 == dd)
1109 				return (dd);
1110 		}
1111 	}
1112 	return (NULL);
1113 }
1114 
1115 /*
1116  * If delta is applied to dd, how much of that delta would be applied to
1117  * ancestor?  Syncing context only.
1118  */
1119 static int64_t
1120 would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1121 {
1122 	if (dd == ancestor)
1123 		return (delta);
1124 
1125 	mutex_enter(&dd->dd_lock);
1126 	delta = parent_delta(dd, dd->dd_used_bytes, delta);
1127 	mutex_exit(&dd->dd_lock);
1128 	return (would_change(dd->dd_parent, delta, ancestor));
1129 }
1130 
1131 int
1132 dsl_dir_rename_sync(dsl_dir_t *dd, void *arg, dmu_tx_t *tx)
1133 {
1134 	const char *newname = arg;
1135 	dsl_pool_t *dp = dd->dd_pool;
1136 	objset_t *mos = dp->dp_meta_objset;
1137 	dsl_dir_t *newpds;
1138 	const char *tail;
1139 	int err, len;
1140 
1141 	/* can't rename to different pool */
1142 	len = strlen(dp->dp_root_dir->dd_myname);
1143 	if (strncmp(dp->dp_root_dir->dd_myname, newname, len != 0) ||
1144 	    newname[len] != '/') {
1145 		return (ENXIO);
1146 	}
1147 
1148 	/* new parent should exist */
1149 	err = dsl_dir_open_spa(dp->dp_spa, newname, FTAG, &newpds, &tail);
1150 	if (err)
1151 		return (err);
1152 
1153 	/* new name should not already exist */
1154 	if (tail == NULL) {
1155 		dsl_dir_close(newpds, FTAG);
1156 		return (EEXIST);
1157 	}
1158 
1159 	rw_enter(&dp->dp_config_rwlock, RW_WRITER);
1160 
1161 	/* There should be 2 references: the open and the dirty */
1162 	if (dmu_buf_refcount(dd->dd_dbuf) > 2) {
1163 		rw_exit(&dp->dp_config_rwlock);
1164 		dsl_dir_close(newpds, FTAG);
1165 		return (EBUSY);
1166 	}
1167 
1168 	if (newpds != dd->dd_parent) {
1169 		/* is there enough space? */
1170 		uint64_t myspace =
1171 		    MAX(dd->dd_used_bytes, dd->dd_phys->dd_reserved);
1172 
1173 		/* no rename into our descendent */
1174 		if (closest_common_ancestor(dd, newpds) == dd) {
1175 			dsl_dir_close(newpds, FTAG);
1176 			rw_exit(&dp->dp_config_rwlock);
1177 			return (EINVAL);
1178 		}
1179 
1180 		if (err = dsl_dir_transfer_possible(dd->dd_parent, newpds,
1181 		    myspace)) {
1182 			dsl_dir_close(newpds, FTAG);
1183 			rw_exit(&dp->dp_config_rwlock);
1184 			return (err);
1185 		}
1186 
1187 		/* The point of no (unsuccessful) return */
1188 
1189 		dsl_dir_diduse_space(dd->dd_parent, -myspace,
1190 		    -dd->dd_phys->dd_compressed_bytes,
1191 		    -dd->dd_phys->dd_uncompressed_bytes, tx);
1192 		dsl_dir_diduse_space(newpds, myspace,
1193 		    dd->dd_phys->dd_compressed_bytes,
1194 		    dd->dd_phys->dd_uncompressed_bytes, tx);
1195 	}
1196 
1197 	/* The point of no (unsuccessful) return */
1198 
1199 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1200 
1201 	/* remove from old parent zapobj */
1202 	err = zap_remove(mos, dd->dd_parent->dd_phys->dd_child_dir_zapobj,
1203 	    dd->dd_myname, tx);
1204 	ASSERT3U(err, ==, 0);
1205 
1206 	(void) strcpy(dd->dd_myname, tail);
1207 	dsl_dir_close(dd->dd_parent, dd);
1208 	dd->dd_phys->dd_parent_obj = newpds->dd_object;
1209 	VERIFY(0 == dsl_dir_open_obj(dd->dd_pool,
1210 	    newpds->dd_object, NULL, dd, &dd->dd_parent));
1211 
1212 	/* add to new parent zapobj */
1213 	err = zap_add(mos, newpds->dd_phys->dd_child_dir_zapobj,
1214 	    dd->dd_myname, 8, 1, &dd->dd_object, tx);
1215 	ASSERT3U(err, ==, 0);
1216 
1217 	dsl_dir_close(newpds, FTAG);
1218 	rw_exit(&dp->dp_config_rwlock);
1219 	return (0);
1220 }
1221 
1222 int
1223 dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, uint64_t space)
1224 {
1225 	dsl_dir_t *ancestor;
1226 	int64_t adelta;
1227 	uint64_t avail;
1228 
1229 	ancestor = closest_common_ancestor(sdd, tdd);
1230 	adelta = would_change(sdd, -space, ancestor);
1231 	avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
1232 	if (avail < space)
1233 		return (ENOSPC);
1234 
1235 	return (0);
1236 }
1237