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.
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.
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]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
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>
38 #include "zfs_namecheck.h"
40 static uint64_t dsl_dir_estimated_space(dsl_dir_t *dd);
41 static void dsl_dir_set_reservation_sync(void *arg1, void *arg2, dmu_tx_t *tx);
46 dsl_dir_evict(dmu_buf_t *db, void *arg)
49 dsl_pool_t *dp = dd->dd_pool;
52 for (t = 0; t < TXG_SIZE; t++) {
53 ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
54 ASSERT(dd->dd_tempreserved[t] == 0);
55 ASSERT(dd->dd_space_towrite[t] == 0);
58 ASSERT3U(dd->dd_used_bytes, ==, dd->dd_phys->dd_used_bytes);
61 dsl_dir_close(dd->dd_parent, dd);
63 spa_close(dd->dd_pool->dp_spa, dd);
66 * The props callback list should be empty since they hold the
69 list_destroy(&dd->dd_prop_cbs);
70 mutex_destroy(&dd->dd_lock);
71 kmem_free(dd, sizeof (dsl_dir_t));
75 dsl_dir_open_obj(dsl_pool_t *dp, uint64_t ddobj,
76 const char *tail, void *tag, dsl_dir_t **ddp)
82 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
83 dsl_pool_sync_context(dp));
85 err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
88 dd = dmu_buf_get_user(dbuf);
91 dmu_object_info_t doi;
92 dmu_object_info_from_db(dbuf, &doi);
93 ASSERT3U(doi.doi_type, ==, DMU_OT_DSL_DIR);
96 /* XXX assert bonus buffer size is correct */
101 dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
102 dd->dd_object = ddobj;
105 dd->dd_phys = dbuf->db_data;
106 dd->dd_used_bytes = dd->dd_phys->dd_used_bytes;
107 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
109 list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t),
110 offsetof(dsl_prop_cb_record_t, cbr_node));
112 if (dd->dd_phys->dd_parent_obj) {
113 err = dsl_dir_open_obj(dp, dd->dd_phys->dd_parent_obj,
114 NULL, dd, &dd->dd_parent);
116 mutex_destroy(&dd->dd_lock);
117 kmem_free(dd, sizeof (dsl_dir_t));
118 dmu_buf_rele(dbuf, tag);
125 err = zap_lookup(dp->dp_meta_objset,
126 dd->dd_parent->dd_phys->
128 tail, sizeof (foundobj), 1, &foundobj);
129 ASSERT(err || foundobj == ddobj);
131 (void) strcpy(dd->dd_myname, tail);
133 err = zap_value_search(dp->dp_meta_objset,
134 dd->dd_parent->dd_phys->
136 ddobj, dd->dd_myname);
139 dsl_dir_close(dd->dd_parent, dd);
140 mutex_destroy(&dd->dd_lock);
141 kmem_free(dd, sizeof (dsl_dir_t));
142 dmu_buf_rele(dbuf, tag);
146 (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
149 winner = dmu_buf_set_user_ie(dbuf, dd, &dd->dd_phys,
153 dsl_dir_close(dd->dd_parent, dd);
154 mutex_destroy(&dd->dd_lock);
155 kmem_free(dd, sizeof (dsl_dir_t));
158 spa_open_ref(dp->dp_spa, dd);
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.
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);
180 dsl_dir_close(dsl_dir_t *dd, void *tag)
182 dprintf_dd(dd, "%s\n", "");
183 spa_close(dd->dd_pool->dp_spa, tag);
184 dmu_buf_rele(dd->dd_dbuf, tag);
187 /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */
189 dsl_dir_name(dsl_dir_t *dd, char *buf)
192 dsl_dir_name(dd->dd_parent, buf);
193 (void) strcat(buf, "/");
197 if (!MUTEX_HELD(&dd->dd_lock)) {
199 * recursive mutex so that we can use
200 * dprintf_dd() with dd_lock held
202 mutex_enter(&dd->dd_lock);
203 (void) strcat(buf, dd->dd_myname);
204 mutex_exit(&dd->dd_lock);
206 (void) strcat(buf, dd->dd_myname);
210 /* Calculate name legnth, avoiding all the strcat calls of dsl_dir_name */
212 dsl_dir_namelen(dsl_dir_t *dd)
217 /* parent's name + 1 for the "/" */
218 result = dsl_dir_namelen(dd->dd_parent) + 1;
221 if (!MUTEX_HELD(&dd->dd_lock)) {
222 /* see dsl_dir_name */
223 mutex_enter(&dd->dd_lock);
224 result += strlen(dd->dd_myname);
225 mutex_exit(&dd->dd_lock);
227 result += strlen(dd->dd_myname);
234 dsl_dir_is_private(dsl_dir_t *dd)
238 if (dd->dd_parent && dsl_dir_is_private(dd->dd_parent))
240 if (dataset_name_hidden(dd->dd_myname))
247 getcomponent(const char *path, char *component, const char **nextp)
252 /* This would be a good place to reserve some namespace... */
253 p = strpbrk(path, "/@");
254 if (p && (p[1] == '/' || p[1] == '@')) {
255 /* two separators in a row */
258 if (p == NULL || p == path) {
260 * if the first thing is an @ or /, it had better be an
261 * @ and it had better not have any more ats or slashes,
262 * and it had better have something after the @.
265 (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
267 if (strlen(path) >= MAXNAMELEN)
268 return (ENAMETOOLONG);
269 (void) strcpy(component, path);
271 } else if (p[0] == '/') {
272 if (p-path >= MAXNAMELEN)
273 return (ENAMETOOLONG);
274 (void) strncpy(component, path, p - path);
275 component[p-path] = '\0';
277 } else if (p[0] == '@') {
279 * if the next separator is an @, there better not be
282 if (strchr(path, '/'))
284 if (p-path >= MAXNAMELEN)
285 return (ENAMETOOLONG);
286 (void) strncpy(component, path, p - path);
287 component[p-path] = '\0';
289 ASSERT(!"invalid p");
296 * same as dsl_open_dir, ignore the first component of name and use the
300 dsl_dir_open_spa(spa_t *spa, const char *name, void *tag,
301 dsl_dir_t **ddp, const char **tailp)
303 char buf[MAXNAMELEN];
304 const char *next, *nextnext = NULL;
309 int openedspa = FALSE;
311 dprintf("%s\n", name);
313 err = getcomponent(name, buf, &next);
317 err = spa_open(buf, &spa, FTAG);
319 dprintf("spa_open(%s) failed\n", buf);
324 /* XXX this assertion belongs in spa_open */
325 ASSERT(!dsl_pool_sync_context(spa_get_dsl(spa)));
328 dp = spa_get_dsl(spa);
330 rw_enter(&dp->dp_config_rwlock, RW_READER);
331 err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
333 rw_exit(&dp->dp_config_rwlock);
335 spa_close(spa, FTAG);
339 while (next != NULL) {
341 err = getcomponent(next, buf, &nextnext);
344 ASSERT(next[0] != '\0');
347 dprintf("looking up %s in obj%lld\n",
348 buf, dd->dd_phys->dd_child_dir_zapobj);
350 err = zap_lookup(dp->dp_meta_objset,
351 dd->dd_phys->dd_child_dir_zapobj,
352 buf, sizeof (ddobj), 1, &ddobj);
359 err = dsl_dir_open_obj(dp, ddobj, buf, tag, &child_ds);
362 dsl_dir_close(dd, tag);
366 rw_exit(&dp->dp_config_rwlock);
369 dsl_dir_close(dd, tag);
371 spa_close(spa, FTAG);
376 * It's an error if there's more than one component left, or
377 * tailp==NULL and there's any component left.
380 (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
382 dsl_dir_close(dd, tag);
383 dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
389 spa_close(spa, FTAG);
395 * Return the dsl_dir_t, and possibly the last component which couldn't
396 * be found in *tail. Return NULL if the path is bogus, or if
397 * tail==NULL and we couldn't parse the whole name. (*tail)[0] == '@'
398 * means that the last component is a snapshot.
401 dsl_dir_open(const char *name, void *tag, dsl_dir_t **ddp, const char **tailp)
403 return (dsl_dir_open_spa(NULL, name, tag, ddp, tailp));
407 dsl_dir_create_sync(dsl_dir_t *pds, const char *name, dmu_tx_t *tx)
409 objset_t *mos = pds->dd_pool->dp_meta_objset;
411 dsl_dir_phys_t *dsphys;
414 ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
415 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
416 VERIFY(0 == zap_add(mos, pds->dd_phys->dd_child_dir_zapobj,
417 name, sizeof (uint64_t), 1, &ddobj, tx));
418 VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
419 dmu_buf_will_dirty(dbuf, tx);
420 dsphys = dbuf->db_data;
422 dsphys->dd_creation_time = gethrestime_sec();
423 dsphys->dd_parent_obj = pds->dd_object;
424 dsphys->dd_props_zapobj = zap_create(mos,
425 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
426 dsphys->dd_child_dir_zapobj = zap_create(mos,
427 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
428 dmu_buf_rele(dbuf, FTAG);
435 dsl_dir_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
437 dsl_dir_t *dd = arg1;
438 dsl_pool_t *dp = dd->dd_pool;
439 objset_t *mos = dp->dp_meta_objset;
444 * There should be exactly two holds, both from
445 * dsl_dataset_destroy: one on the dd directory, and one on its
446 * head ds. Otherwise, someone is trying to lookup something
447 * inside this dir while we want to destroy it. The
448 * config_rwlock ensures that nobody else opens it after we
451 if (dmu_buf_refcount(dd->dd_dbuf) > 2)
454 err = zap_count(mos, dd->dd_phys->dd_child_dir_zapobj, &count);
464 dsl_dir_destroy_sync(void *arg1, void *tag, dmu_tx_t *tx)
466 dsl_dir_t *dd = arg1;
467 objset_t *mos = dd->dd_pool->dp_meta_objset;
470 ASSERT(RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock));
471 ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
473 /* Remove our reservation. */
475 dsl_dir_set_reservation_sync(dd, &val, tx);
476 ASSERT3U(dd->dd_used_bytes, ==, 0);
477 ASSERT3U(dd->dd_phys->dd_reserved, ==, 0);
479 VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
480 VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
481 VERIFY(0 == zap_remove(mos,
482 dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
485 dsl_dir_close(dd, tag);
486 VERIFY(0 == dmu_object_free(mos, obj, tx));
490 dsl_dir_create_root(objset_t *mos, uint64_t *ddobjp, dmu_tx_t *tx)
496 *ddobjp = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
497 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
499 error = zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ROOT_DATASET,
500 sizeof (uint64_t), 1, ddobjp, tx);
501 ASSERT3U(error, ==, 0);
503 VERIFY(0 == dmu_bonus_hold(mos, *ddobjp, FTAG, &dbuf));
504 dmu_buf_will_dirty(dbuf, tx);
507 dsp->dd_creation_time = gethrestime_sec();
508 dsp->dd_props_zapobj = zap_create(mos,
509 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
510 dsp->dd_child_dir_zapobj = zap_create(mos,
511 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
513 dmu_buf_rele(dbuf, FTAG);
517 dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
519 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE,
520 dsl_dir_space_available(dd, NULL, 0, TRUE));
522 mutex_enter(&dd->dd_lock);
523 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, dd->dd_used_bytes);
524 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
525 dd->dd_phys->dd_quota);
526 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
527 dd->dd_phys->dd_reserved);
528 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
529 dd->dd_phys->dd_compressed_bytes == 0 ? 100 :
530 (dd->dd_phys->dd_uncompressed_bytes * 100 /
531 dd->dd_phys->dd_compressed_bytes));
532 mutex_exit(&dd->dd_lock);
534 if (dd->dd_phys->dd_clone_parent_obj) {
536 char buf[MAXNAMELEN];
538 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
539 VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool,
540 dd->dd_phys->dd_clone_parent_obj,
541 NULL, DS_MODE_NONE, FTAG, &ds));
542 dsl_dataset_name(ds, buf);
543 dsl_dataset_close(ds, DS_MODE_NONE, FTAG);
544 rw_exit(&dd->dd_pool->dp_config_rwlock);
546 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
551 dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
553 dsl_pool_t *dp = dd->dd_pool;
557 if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg) == 0) {
558 /* up the hold count until we can be written out */
559 dmu_buf_add_ref(dd->dd_dbuf, dd);
564 parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
566 uint64_t old_accounted = MAX(used, dd->dd_phys->dd_reserved);
567 uint64_t new_accounted = MAX(used + delta, dd->dd_phys->dd_reserved);
568 return (new_accounted - old_accounted);
572 dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
574 ASSERT(dmu_tx_is_syncing(tx));
576 dmu_buf_will_dirty(dd->dd_dbuf, tx);
578 mutex_enter(&dd->dd_lock);
579 ASSERT3U(dd->dd_tempreserved[tx->tx_txg&TXG_MASK], ==, 0);
580 dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
581 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
582 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
583 dd->dd_phys->dd_used_bytes = dd->dd_used_bytes;
584 mutex_exit(&dd->dd_lock);
586 /* release the hold from dsl_dir_dirty */
587 dmu_buf_rele(dd->dd_dbuf, dd);
591 dsl_dir_estimated_space(dsl_dir_t *dd)
596 ASSERT(MUTEX_HELD(&dd->dd_lock));
598 space = dd->dd_phys->dd_used_bytes;
600 for (i = 0; i < TXG_SIZE; i++) {
601 space += dd->dd_space_towrite[i&TXG_MASK];
602 ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
608 * How much space would dd have available if ancestor had delta applied
609 * to it? If ondiskonly is set, we're only interested in what's
610 * on-disk, not estimated pending changes.
613 dsl_dir_space_available(dsl_dir_t *dd,
614 dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
616 uint64_t parentspace, myspace, quota, used;
619 * If there are no restrictions otherwise, assume we have
620 * unlimited space available.
623 parentspace = UINT64_MAX;
625 if (dd->dd_parent != NULL) {
626 parentspace = dsl_dir_space_available(dd->dd_parent,
627 ancestor, delta, ondiskonly);
630 mutex_enter(&dd->dd_lock);
631 if (dd->dd_phys->dd_quota != 0)
632 quota = dd->dd_phys->dd_quota;
634 used = dd->dd_used_bytes;
636 used = dsl_dir_estimated_space(dd);
641 if (dd->dd_parent == NULL) {
642 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
643 quota = MIN(quota, poolsize);
646 if (dd->dd_phys->dd_reserved > used && parentspace != UINT64_MAX) {
648 * We have some space reserved, in addition to what our
651 parentspace += dd->dd_phys->dd_reserved - used;
659 * While it's OK to be a little over quota, if
660 * we think we are using more space than there
661 * is in the pool (which is already 1.6% more than
662 * dsl_pool_adjustedsize()), something is very
665 ASSERT3U(used, <=, spa_get_space(dd->dd_pool->dp_spa));
668 * the lesser of the space provided by our parent and
669 * the space left in our quota
671 myspace = MIN(parentspace, quota - used);
674 mutex_exit(&dd->dd_lock);
686 * Reserve space in this dsl_dir, to be used in this tx's txg.
687 * After the space has been dirtied (and thus
688 * dsl_dir_willuse_space() has been called), the reservation should
689 * be canceled, using dsl_dir_tempreserve_clear().
692 dsl_dir_tempreserve_impl(dsl_dir_t *dd,
693 uint64_t asize, boolean_t netfree, list_t *tr_list, dmu_tx_t *tx)
695 uint64_t txg = tx->tx_txg;
696 uint64_t est_used, quota, parent_rsrv;
698 int txgidx = txg & TXG_MASK;
700 struct tempreserve *tr;
702 ASSERT3U(txg, !=, 0);
703 ASSERT3S(asize, >=, 0);
705 mutex_enter(&dd->dd_lock);
707 * Check against the dsl_dir's quota. We don't add in the delta
708 * when checking for over-quota because they get one free hit.
710 est_used = dsl_dir_estimated_space(dd);
711 for (i = 0; i < TXG_SIZE; i++)
712 est_used += dd->dd_tempreserved[i];
716 if (dd->dd_phys->dd_quota)
717 quota = dd->dd_phys->dd_quota;
720 * If this transaction will result in a net free of space, we want
721 * to let it through, but we have to be careful: the space that it
722 * frees won't become available until *after* this txg syncs.
723 * Therefore, to ensure that it's possible to remove files from
724 * a full pool without inducing transient overcommits, we throttle
725 * netfree transactions against a quota that is slightly larger,
726 * but still within the pool's allocation slop. In cases where
727 * we're very close to full, this will allow a steady trickle of
728 * removes to get through.
730 if (dd->dd_parent == NULL) {
731 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
732 if (poolsize < quota) {
736 } else if (netfree) {
741 * If they are requesting more space, and our current estimate
742 * is over quota. They get to try again unless the actual
743 * on-disk is over quota and there are no pending changes (which
744 * may free up space for us).
746 if (asize > 0 && est_used > quota) {
747 if (dd->dd_space_towrite[txg & TXG_MASK] != 0 ||
748 dd->dd_space_towrite[(txg-1) & TXG_MASK] != 0 ||
749 dd->dd_space_towrite[(txg-2) & TXG_MASK] != 0 ||
750 dd->dd_used_bytes < quota)
752 dprintf_dd(dd, "failing: used=%lluK est_used = %lluK "
753 "quota=%lluK tr=%lluK err=%d\n",
754 dd->dd_used_bytes>>10, est_used>>10,
755 quota>>10, asize>>10, edquot);
756 mutex_exit(&dd->dd_lock);
760 /* We need to up our estimated delta before dropping dd_lock */
761 dd->dd_tempreserved[txgidx] += asize;
763 parent_rsrv = parent_delta(dd, est_used, asize);
764 mutex_exit(&dd->dd_lock);
766 tr = kmem_alloc(sizeof (struct tempreserve), KM_SLEEP);
769 list_insert_tail(tr_list, tr);
771 /* see if it's OK with our parent */
772 if (dd->dd_parent && parent_rsrv) {
773 return (dsl_dir_tempreserve_impl(dd->dd_parent,
774 parent_rsrv, netfree, tr_list, tx));
781 * Reserve space in this dsl_dir, to be used in this tx's txg.
782 * After the space has been dirtied (and thus
783 * dsl_dir_willuse_space() has been called), the reservation should
784 * be canceled, using dsl_dir_tempreserve_clear().
787 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize,
788 uint64_t asize, uint64_t fsize, void **tr_cookiep, dmu_tx_t *tx)
793 tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
794 list_create(tr_list, sizeof (struct tempreserve),
795 offsetof(struct tempreserve, tr_node));
796 ASSERT3S(asize, >=, 0);
797 ASSERT3S(fsize, >=, 0);
799 err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
803 struct tempreserve *tr;
805 err = arc_tempreserve_space(lsize);
807 tr = kmem_alloc(sizeof (struct tempreserve), KM_SLEEP);
810 list_insert_tail(tr_list, tr);
815 dsl_dir_tempreserve_clear(tr_list, tx);
817 *tr_cookiep = tr_list;
822 * Clear a temporary reservation that we previously made with
823 * dsl_dir_tempreserve_space().
826 dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
828 int txgidx = tx->tx_txg & TXG_MASK;
829 list_t *tr_list = tr_cookie;
830 struct tempreserve *tr;
832 ASSERT3U(tx->tx_txg, !=, 0);
834 while (tr = list_head(tr_list)) {
835 if (tr->tr_ds == NULL) {
836 arc_tempreserve_clear(tr->tr_size);
838 mutex_enter(&tr->tr_ds->dd_lock);
839 ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
841 tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
842 mutex_exit(&tr->tr_ds->dd_lock);
844 list_remove(tr_list, tr);
845 kmem_free(tr, sizeof (struct tempreserve));
848 kmem_free(tr_list, sizeof (list_t));
852 * Call in open context when we think we're going to write/free space,
853 * eg. when dirtying data. Be conservative (ie. OK to write less than
854 * this or free more than this, but don't write more or free less).
857 dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
859 int64_t parent_space;
862 mutex_enter(&dd->dd_lock);
864 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
866 est_used = dsl_dir_estimated_space(dd);
867 parent_space = parent_delta(dd, est_used, space);
868 mutex_exit(&dd->dd_lock);
870 /* Make sure that we clean up dd_space_to* */
871 dsl_dir_dirty(dd, tx);
873 /* XXX this is potentially expensive and unnecessary... */
874 if (parent_space && dd->dd_parent)
875 dsl_dir_willuse_space(dd->dd_parent, parent_space, tx);
878 /* call from syncing context when we actually write/free space for this dd */
880 dsl_dir_diduse_space(dsl_dir_t *dd,
881 int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
883 int64_t accounted_delta;
885 ASSERT(dmu_tx_is_syncing(tx));
887 dsl_dir_dirty(dd, tx);
889 mutex_enter(&dd->dd_lock);
890 accounted_delta = parent_delta(dd, dd->dd_used_bytes, used);
891 ASSERT(used >= 0 || dd->dd_used_bytes >= -used);
892 ASSERT(compressed >= 0 ||
893 dd->dd_phys->dd_compressed_bytes >= -compressed);
894 ASSERT(uncompressed >= 0 ||
895 dd->dd_phys->dd_uncompressed_bytes >= -uncompressed);
896 dd->dd_used_bytes += used;
897 dd->dd_phys->dd_uncompressed_bytes += uncompressed;
898 dd->dd_phys->dd_compressed_bytes += compressed;
899 mutex_exit(&dd->dd_lock);
901 if (dd->dd_parent != NULL) {
902 dsl_dir_diduse_space(dd->dd_parent,
903 accounted_delta, compressed, uncompressed, tx);
909 dsl_dir_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
911 dsl_dir_t *dd = arg1;
912 uint64_t *quotap = arg2;
913 uint64_t new_quota = *quotap;
920 mutex_enter(&dd->dd_lock);
922 * If we are doing the preliminary check in open context, and
923 * there are pending changes, then don't fail it, since the
924 * pending changes could under-estimat the amount of space to be
927 towrite = dd->dd_space_towrite[0] + dd->dd_space_towrite[1] +
928 dd->dd_space_towrite[2] + dd->dd_space_towrite[3];
929 if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
930 (new_quota < dd->dd_phys->dd_reserved ||
931 new_quota < dsl_dir_estimated_space(dd))) {
934 mutex_exit(&dd->dd_lock);
939 dsl_dir_set_quota_sync(void *arg1, void *arg2, dmu_tx_t *tx)
941 dsl_dir_t *dd = arg1;
942 uint64_t *quotap = arg2;
943 uint64_t new_quota = *quotap;
945 dmu_buf_will_dirty(dd->dd_dbuf, tx);
947 mutex_enter(&dd->dd_lock);
948 dd->dd_phys->dd_quota = new_quota;
949 mutex_exit(&dd->dd_lock);
953 dsl_dir_set_quota(const char *ddname, uint64_t quota)
958 err = dsl_dir_open(ddname, FTAG, &dd, NULL);
962 * If someone removes a file, then tries to set the quota, we
963 * want to make sure the file freeing takes effect.
965 txg_wait_open(dd->dd_pool, 0);
967 err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_quota_check,
968 dsl_dir_set_quota_sync, dd, "a, 0);
969 dsl_dir_close(dd, FTAG);
975 dsl_dir_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
977 dsl_dir_t *dd = arg1;
978 uint64_t *reservationp = arg2;
979 uint64_t new_reservation = *reservationp;
980 uint64_t used, avail;
983 if (new_reservation > INT64_MAX)
987 * If we are doing the preliminary check in open context, the
988 * space estimates may be inaccurate.
990 if (!dmu_tx_is_syncing(tx))
993 mutex_enter(&dd->dd_lock);
994 used = dd->dd_used_bytes;
995 delta = MAX(used, new_reservation) -
996 MAX(used, dd->dd_phys->dd_reserved);
997 mutex_exit(&dd->dd_lock);
1000 avail = dsl_dir_space_available(dd->dd_parent,
1003 avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1006 if (delta > 0 && delta > avail)
1008 if (delta > 0 && dd->dd_phys->dd_quota > 0 &&
1009 new_reservation > dd->dd_phys->dd_quota)
1015 dsl_dir_set_reservation_sync(void *arg1, void *arg2, dmu_tx_t *tx)
1017 dsl_dir_t *dd = arg1;
1018 uint64_t *reservationp = arg2;
1019 uint64_t new_reservation = *reservationp;
1023 mutex_enter(&dd->dd_lock);
1024 used = dd->dd_used_bytes;
1025 delta = MAX(used, new_reservation) -
1026 MAX(used, dd->dd_phys->dd_reserved);
1027 mutex_exit(&dd->dd_lock);
1029 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1030 dd->dd_phys->dd_reserved = new_reservation;
1032 if (dd->dd_parent != NULL) {
1033 /* Roll up this additional usage into our ancestors */
1034 dsl_dir_diduse_space(dd->dd_parent, delta, 0, 0, tx);
1039 dsl_dir_set_reservation(const char *ddname, uint64_t reservation)
1044 err = dsl_dir_open(ddname, FTAG, &dd, NULL);
1047 err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_reservation_check,
1048 dsl_dir_set_reservation_sync, dd, &reservation, 0);
1049 dsl_dir_close(dd, FTAG);
1054 closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1056 for (; ds1; ds1 = ds1->dd_parent) {
1058 for (dd = ds2; dd; dd = dd->dd_parent) {
1067 * If delta is applied to dd, how much of that delta would be applied to
1068 * ancestor? Syncing context only.
1071 would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1076 mutex_enter(&dd->dd_lock);
1077 delta = parent_delta(dd, dd->dd_used_bytes, delta);
1078 mutex_exit(&dd->dd_lock);
1079 return (would_change(dd->dd_parent, delta, ancestor));
1083 dsl_dir_t *newparent;
1084 const char *mynewname;
1089 dsl_dir_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
1091 dsl_dir_t *dd = arg1;
1092 struct renamearg *ra = arg2;
1093 dsl_pool_t *dp = dd->dd_pool;
1094 objset_t *mos = dp->dp_meta_objset;
1098 /* There should be 2 references: the open and the dirty */
1099 if (dmu_buf_refcount(dd->dd_dbuf) > 2)
1102 /* check for existing name */
1103 err = zap_lookup(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
1104 ra->mynewname, 8, 1, &val);
1110 if (ra->newparent != dd->dd_parent) {
1111 /* is there enough space? */
1113 MAX(dd->dd_used_bytes, dd->dd_phys->dd_reserved);
1115 /* no rename into our descendant */
1116 if (closest_common_ancestor(dd, ra->newparent) == dd)
1119 if (err = dsl_dir_transfer_possible(dd->dd_parent,
1120 ra->newparent, myspace))
1128 dsl_dir_rename_sync(void *arg1, void *arg2, dmu_tx_t *tx)
1130 dsl_dir_t *dd = arg1;
1131 struct renamearg *ra = arg2;
1132 dsl_pool_t *dp = dd->dd_pool;
1133 objset_t *mos = dp->dp_meta_objset;
1136 ASSERT(dmu_buf_refcount(dd->dd_dbuf) <= 2);
1138 if (ra->newparent != dd->dd_parent) {
1140 MAX(dd->dd_used_bytes, dd->dd_phys->dd_reserved);
1142 dsl_dir_diduse_space(dd->dd_parent, -myspace,
1143 -dd->dd_phys->dd_compressed_bytes,
1144 -dd->dd_phys->dd_uncompressed_bytes, tx);
1145 dsl_dir_diduse_space(ra->newparent, myspace,
1146 dd->dd_phys->dd_compressed_bytes,
1147 dd->dd_phys->dd_uncompressed_bytes, tx);
1150 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1152 /* remove from old parent zapobj */
1153 err = zap_remove(mos, dd->dd_parent->dd_phys->dd_child_dir_zapobj,
1155 ASSERT3U(err, ==, 0);
1157 (void) strcpy(dd->dd_myname, ra->mynewname);
1158 dsl_dir_close(dd->dd_parent, dd);
1159 dd->dd_phys->dd_parent_obj = ra->newparent->dd_object;
1160 VERIFY(0 == dsl_dir_open_obj(dd->dd_pool,
1161 ra->newparent->dd_object, NULL, dd, &dd->dd_parent));
1163 /* add to new parent zapobj */
1164 err = zap_add(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
1165 dd->dd_myname, 8, 1, &dd->dd_object, tx);
1166 ASSERT3U(err, ==, 0);
1170 dsl_dir_rename(dsl_dir_t *dd, const char *newname)
1172 struct renamearg ra;
1175 /* new parent should exist */
1176 err = dsl_dir_open(newname, FTAG, &ra.newparent, &ra.mynewname);
1180 /* can't rename to different pool */
1181 if (dd->dd_pool != ra.newparent->dd_pool) {
1186 /* new name should not already exist */
1187 if (ra.mynewname == NULL) {
1193 err = dsl_sync_task_do(dd->dd_pool,
1194 dsl_dir_rename_check, dsl_dir_rename_sync, dd, &ra, 3);
1197 dsl_dir_close(ra.newparent, FTAG);
1202 dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, uint64_t space)
1204 dsl_dir_t *ancestor;
1208 ancestor = closest_common_ancestor(sdd, tdd);
1209 adelta = would_change(sdd, -space, ancestor);
1210 avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);