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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <sys/dmu_objset.h>
27 #include <sys/dmu_tx.h>
28 #include <sys/dsl_dataset.h>
29 #include <sys/dsl_dir.h>
30 #include <sys/dsl_prop.h>
31 #include <sys/dsl_synctask.h>
32 #include <sys/dsl_deleg.h>
34 #include <sys/metaslab.h>
38 #include <sys/sunddi.h>
39 #include "zfs_namecheck.h"
41 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
42 static void dsl_dir_set_reservation_sync(void *arg1, void *arg2, dmu_tx_t *tx);
47 dsl_dir_evict(dmu_buf_t *db, void *arg)
50 ASSERTV(dsl_pool_t *dp = dd->dd_pool;)
53 for (t = 0; t < TXG_SIZE; t++) {
54 ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
55 ASSERT(dd->dd_tempreserved[t] == 0);
56 ASSERT(dd->dd_space_towrite[t] == 0);
60 dsl_dir_close(dd->dd_parent, dd);
62 spa_close(dd->dd_pool->dp_spa, dd);
65 * The props callback list should have been cleaned up by
68 list_destroy(&dd->dd_prop_cbs);
69 mutex_destroy(&dd->dd_lock);
70 kmem_free(dd, sizeof (dsl_dir_t));
74 dsl_dir_open_obj(dsl_pool_t *dp, uint64_t ddobj,
75 const char *tail, void *tag, dsl_dir_t **ddp)
81 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
82 dsl_pool_sync_context(dp));
84 err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
87 dd = dmu_buf_get_user(dbuf);
90 dmu_object_info_t doi;
91 dmu_object_info_from_db(dbuf, &doi);
92 ASSERT3U(doi.doi_type, ==, DMU_OT_DSL_DIR);
93 ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
99 dd = kmem_zalloc(sizeof (dsl_dir_t), KM_PUSHPAGE);
100 dd->dd_object = ddobj;
103 dd->dd_phys = dbuf->db_data;
104 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
106 list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t),
107 offsetof(dsl_prop_cb_record_t, cbr_node));
109 dsl_dir_snap_cmtime_update(dd);
111 if (dd->dd_phys->dd_parent_obj) {
112 err = dsl_dir_open_obj(dp, dd->dd_phys->dd_parent_obj,
113 NULL, dd, &dd->dd_parent);
120 err = zap_lookup(dp->dp_meta_objset,
121 dd->dd_parent->dd_phys->dd_child_dir_zapobj,
122 tail, sizeof (foundobj), 1, &foundobj);
123 ASSERT(err || foundobj == ddobj);
125 (void) strcpy(dd->dd_myname, tail);
127 err = zap_value_search(dp->dp_meta_objset,
128 dd->dd_parent->dd_phys->dd_child_dir_zapobj,
129 ddobj, 0, dd->dd_myname);
134 (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
137 if (dsl_dir_is_clone(dd)) {
138 dmu_buf_t *origin_bonus;
139 dsl_dataset_phys_t *origin_phys;
142 * We can't open the origin dataset, because
143 * that would require opening this dsl_dir.
144 * Just look at its phys directly instead.
146 err = dmu_bonus_hold(dp->dp_meta_objset,
147 dd->dd_phys->dd_origin_obj, FTAG, &origin_bonus);
150 origin_phys = origin_bonus->db_data;
152 origin_phys->ds_creation_txg;
153 dmu_buf_rele(origin_bonus, FTAG);
156 winner = dmu_buf_set_user_ie(dbuf, dd, &dd->dd_phys,
160 dsl_dir_close(dd->dd_parent, dd);
161 mutex_destroy(&dd->dd_lock);
162 kmem_free(dd, sizeof (dsl_dir_t));
165 spa_open_ref(dp->dp_spa, dd);
170 * The dsl_dir_t has both open-to-close and instantiate-to-evict
171 * holds on the spa. We need the open-to-close holds because
172 * otherwise the spa_refcnt wouldn't change when we open a
173 * dir which the spa also has open, so we could incorrectly
174 * think it was OK to unload/export/destroy the pool. We need
175 * the instantiate-to-evict hold because the dsl_dir_t has a
176 * pointer to the dd_pool, which has a pointer to the spa_t.
178 spa_open_ref(dp->dp_spa, tag);
179 ASSERT3P(dd->dd_pool, ==, dp);
180 ASSERT3U(dd->dd_object, ==, ddobj);
181 ASSERT3P(dd->dd_dbuf, ==, dbuf);
187 dsl_dir_close(dd->dd_parent, dd);
188 mutex_destroy(&dd->dd_lock);
189 kmem_free(dd, sizeof (dsl_dir_t));
190 dmu_buf_rele(dbuf, tag);
196 dsl_dir_close(dsl_dir_t *dd, void *tag)
198 dprintf_dd(dd, "%s\n", "");
199 spa_close(dd->dd_pool->dp_spa, tag);
200 dmu_buf_rele(dd->dd_dbuf, tag);
203 /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */
205 dsl_dir_name(dsl_dir_t *dd, char *buf)
208 dsl_dir_name(dd->dd_parent, buf);
209 (void) strcat(buf, "/");
213 if (!MUTEX_HELD(&dd->dd_lock)) {
215 * recursive mutex so that we can use
216 * dprintf_dd() with dd_lock held
218 mutex_enter(&dd->dd_lock);
219 (void) strcat(buf, dd->dd_myname);
220 mutex_exit(&dd->dd_lock);
222 (void) strcat(buf, dd->dd_myname);
226 /* Calculate name legnth, avoiding all the strcat calls of dsl_dir_name */
228 dsl_dir_namelen(dsl_dir_t *dd)
233 /* parent's name + 1 for the "/" */
234 result = dsl_dir_namelen(dd->dd_parent) + 1;
237 if (!MUTEX_HELD(&dd->dd_lock)) {
238 /* see dsl_dir_name */
239 mutex_enter(&dd->dd_lock);
240 result += strlen(dd->dd_myname);
241 mutex_exit(&dd->dd_lock);
243 result += strlen(dd->dd_myname);
250 getcomponent(const char *path, char *component, const char **nextp)
253 if ((path == NULL) || (path[0] == '\0'))
255 /* This would be a good place to reserve some namespace... */
256 p = strpbrk(path, "/@");
257 if (p && (p[1] == '/' || p[1] == '@')) {
258 /* two separators in a row */
261 if (p == NULL || p == path) {
263 * if the first thing is an @ or /, it had better be an
264 * @ and it had better not have any more ats or slashes,
265 * and it had better have something after the @.
268 (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
270 if (strlen(path) >= MAXNAMELEN)
271 return (ENAMETOOLONG);
272 (void) strcpy(component, path);
274 } else if (p[0] == '/') {
275 if (p-path >= MAXNAMELEN)
276 return (ENAMETOOLONG);
277 (void) strncpy(component, path, p - path);
278 component[p-path] = '\0';
280 } else if (p[0] == '@') {
282 * if the next separator is an @, there better not be
285 if (strchr(path, '/'))
287 if (p-path >= MAXNAMELEN)
288 return (ENAMETOOLONG);
289 (void) strncpy(component, path, p - path);
290 component[p-path] = '\0';
292 ASSERT(!"invalid p");
299 * same as dsl_open_dir, ignore the first component of name and use the
303 dsl_dir_open_spa(spa_t *spa, const char *name, void *tag,
304 dsl_dir_t **ddp, const char **tailp)
307 const char *next, *nextnext = NULL;
312 int openedspa = FALSE;
314 dprintf("%s\n", name);
316 buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
317 err = getcomponent(name, buf, &next);
321 err = spa_open(buf, &spa, FTAG);
323 dprintf("spa_open(%s) failed\n", buf);
328 /* XXX this assertion belongs in spa_open */
329 ASSERT(!dsl_pool_sync_context(spa_get_dsl(spa)));
332 dp = spa_get_dsl(spa);
334 rw_enter(&dp->dp_config_rwlock, RW_READER);
335 err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
337 rw_exit(&dp->dp_config_rwlock);
339 spa_close(spa, FTAG);
343 while (next != NULL) {
345 err = getcomponent(next, buf, &nextnext);
348 ASSERT(next[0] != '\0');
351 dprintf("looking up %s in obj%lld\n",
352 buf, dd->dd_phys->dd_child_dir_zapobj);
354 err = zap_lookup(dp->dp_meta_objset,
355 dd->dd_phys->dd_child_dir_zapobj,
356 buf, sizeof (ddobj), 1, &ddobj);
363 err = dsl_dir_open_obj(dp, ddobj, buf, tag, &child_ds);
366 dsl_dir_close(dd, tag);
370 rw_exit(&dp->dp_config_rwlock);
373 dsl_dir_close(dd, tag);
375 spa_close(spa, FTAG);
380 * It's an error if there's more than one component left, or
381 * tailp==NULL and there's any component left.
384 (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
386 dsl_dir_close(dd, tag);
387 dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
393 spa_close(spa, FTAG);
396 kmem_free(buf, MAXNAMELEN);
401 * Return the dsl_dir_t, and possibly the last component which couldn't
402 * be found in *tail. Return NULL if the path is bogus, or if
403 * tail==NULL and we couldn't parse the whole name. (*tail)[0] == '@'
404 * means that the last component is a snapshot.
407 dsl_dir_open(const char *name, void *tag, dsl_dir_t **ddp, const char **tailp)
409 return (dsl_dir_open_spa(NULL, name, tag, ddp, tailp));
413 dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
416 objset_t *mos = dp->dp_meta_objset;
418 dsl_dir_phys_t *ddphys;
421 ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
422 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
424 VERIFY(0 == zap_add(mos, pds->dd_phys->dd_child_dir_zapobj,
425 name, sizeof (uint64_t), 1, &ddobj, tx));
427 /* it's the root dir */
428 VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
429 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
431 VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
432 dmu_buf_will_dirty(dbuf, tx);
433 ddphys = dbuf->db_data;
435 ddphys->dd_creation_time = gethrestime_sec();
437 ddphys->dd_parent_obj = pds->dd_object;
438 ddphys->dd_props_zapobj = zap_create(mos,
439 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
440 ddphys->dd_child_dir_zapobj = zap_create(mos,
441 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
442 if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
443 ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
444 dmu_buf_rele(dbuf, FTAG);
451 dsl_dir_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
453 dsl_dataset_t *ds = arg1;
454 dsl_dir_t *dd = ds->ds_dir;
455 dsl_pool_t *dp = dd->dd_pool;
456 objset_t *mos = dp->dp_meta_objset;
461 * There should be exactly two holds, both from
462 * dsl_dataset_destroy: one on the dd directory, and one on its
463 * head ds. If there are more holds, then a concurrent thread is
464 * performing a lookup inside this dir while we're trying to destroy
465 * it. To minimize this possibility, we perform this check only
466 * in syncing context and fail the operation if we encounter
467 * additional holds. The dp_config_rwlock ensures that nobody else
468 * opens it after we check.
470 if (dmu_tx_is_syncing(tx) && dmu_buf_refcount(dd->dd_dbuf) > 2)
473 err = zap_count(mos, dd->dd_phys->dd_child_dir_zapobj, &count);
483 dsl_dir_destroy_sync(void *arg1, void *tag, dmu_tx_t *tx)
485 dsl_dataset_t *ds = arg1;
486 dsl_dir_t *dd = ds->ds_dir;
487 objset_t *mos = dd->dd_pool->dp_meta_objset;
488 dsl_prop_setarg_t psa;
493 ASSERT(RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock));
494 ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
496 /* Remove our reservation. */
497 dsl_prop_setarg_init_uint64(&psa, "reservation",
498 (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
500 psa.psa_effective_value = 0; /* predict default value */
502 dsl_dir_set_reservation_sync(ds, &psa, tx);
504 ASSERT3U(dd->dd_phys->dd_used_bytes, ==, 0);
505 ASSERT3U(dd->dd_phys->dd_reserved, ==, 0);
506 for (t = 0; t < DD_USED_NUM; t++)
507 ASSERT3U(dd->dd_phys->dd_used_breakdown[t], ==, 0);
509 VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
510 VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
511 VERIFY(0 == dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx));
512 VERIFY(0 == zap_remove(mos,
513 dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
516 dsl_dir_close(dd, tag);
517 VERIFY(0 == dmu_object_free(mos, obj, tx));
521 dsl_dir_is_clone(dsl_dir_t *dd)
523 return (dd->dd_phys->dd_origin_obj &&
524 (dd->dd_pool->dp_origin_snap == NULL ||
525 dd->dd_phys->dd_origin_obj !=
526 dd->dd_pool->dp_origin_snap->ds_object));
530 dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
532 mutex_enter(&dd->dd_lock);
533 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
534 dd->dd_phys->dd_used_bytes);
535 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA, dd->dd_phys->dd_quota);
536 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
537 dd->dd_phys->dd_reserved);
538 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
539 dd->dd_phys->dd_compressed_bytes == 0 ? 100 :
540 (dd->dd_phys->dd_uncompressed_bytes * 100 /
541 dd->dd_phys->dd_compressed_bytes));
542 if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
543 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
544 dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]);
545 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
546 dd->dd_phys->dd_used_breakdown[DD_USED_HEAD]);
547 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
548 dd->dd_phys->dd_used_breakdown[DD_USED_REFRSRV]);
549 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
550 dd->dd_phys->dd_used_breakdown[DD_USED_CHILD] +
551 dd->dd_phys->dd_used_breakdown[DD_USED_CHILD_RSRV]);
553 mutex_exit(&dd->dd_lock);
555 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
556 if (dsl_dir_is_clone(dd)) {
558 char buf[MAXNAMELEN];
560 VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
561 dd->dd_phys->dd_origin_obj, FTAG, &ds));
562 dsl_dataset_name(ds, buf);
563 dsl_dataset_rele(ds, FTAG);
564 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
566 rw_exit(&dd->dd_pool->dp_config_rwlock);
570 dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
572 dsl_pool_t *dp = dd->dd_pool;
576 if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg) == 0) {
577 /* up the hold count until we can be written out */
578 dmu_buf_add_ref(dd->dd_dbuf, dd);
583 parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
585 uint64_t old_accounted = MAX(used, dd->dd_phys->dd_reserved);
586 uint64_t new_accounted = MAX(used + delta, dd->dd_phys->dd_reserved);
587 return (new_accounted - old_accounted);
591 dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
593 ASSERT(dmu_tx_is_syncing(tx));
595 dmu_buf_will_dirty(dd->dd_dbuf, tx);
597 mutex_enter(&dd->dd_lock);
598 ASSERT3U(dd->dd_tempreserved[tx->tx_txg&TXG_MASK], ==, 0);
599 dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
600 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
601 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
602 mutex_exit(&dd->dd_lock);
604 /* release the hold from dsl_dir_dirty */
605 dmu_buf_rele(dd->dd_dbuf, dd);
609 dsl_dir_space_towrite(dsl_dir_t *dd)
614 ASSERT(MUTEX_HELD(&dd->dd_lock));
616 for (i = 0; i < TXG_SIZE; i++) {
617 space += dd->dd_space_towrite[i&TXG_MASK];
618 ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
624 * How much space would dd have available if ancestor had delta applied
625 * to it? If ondiskonly is set, we're only interested in what's
626 * on-disk, not estimated pending changes.
629 dsl_dir_space_available(dsl_dir_t *dd,
630 dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
632 uint64_t parentspace, myspace, quota, used;
635 * If there are no restrictions otherwise, assume we have
636 * unlimited space available.
639 parentspace = UINT64_MAX;
641 if (dd->dd_parent != NULL) {
642 parentspace = dsl_dir_space_available(dd->dd_parent,
643 ancestor, delta, ondiskonly);
646 mutex_enter(&dd->dd_lock);
647 if (dd->dd_phys->dd_quota != 0)
648 quota = dd->dd_phys->dd_quota;
649 used = dd->dd_phys->dd_used_bytes;
651 used += dsl_dir_space_towrite(dd);
653 if (dd->dd_parent == NULL) {
654 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
655 quota = MIN(quota, poolsize);
658 if (dd->dd_phys->dd_reserved > used && parentspace != UINT64_MAX) {
660 * We have some space reserved, in addition to what our
663 parentspace += dd->dd_phys->dd_reserved - used;
666 if (dd == ancestor) {
668 ASSERT(used >= -delta);
670 if (parentspace != UINT64_MAX)
671 parentspace -= delta;
679 * the lesser of the space provided by our parent and
680 * the space left in our quota
682 myspace = MIN(parentspace, quota - used);
685 mutex_exit(&dd->dd_lock);
698 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
699 boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
700 dmu_tx_t *tx, boolean_t first)
702 uint64_t txg = tx->tx_txg;
703 uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
704 uint64_t deferred = 0;
705 struct tempreserve *tr;
707 int txgidx = txg & TXG_MASK;
709 uint64_t ref_rsrv = 0;
711 ASSERT3U(txg, !=, 0);
712 ASSERT3S(asize, >, 0);
714 mutex_enter(&dd->dd_lock);
717 * Check against the dsl_dir's quota. We don't add in the delta
718 * when checking for over-quota because they get one free hit.
720 est_inflight = dsl_dir_space_towrite(dd);
721 for (i = 0; i < TXG_SIZE; i++)
722 est_inflight += dd->dd_tempreserved[i];
723 used_on_disk = dd->dd_phys->dd_used_bytes;
726 * On the first iteration, fetch the dataset's used-on-disk and
727 * refreservation values. Also, if checkrefquota is set, test if
728 * allocating this space would exceed the dataset's refquota.
730 if (first && tx->tx_objset) {
732 dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
734 error = dsl_dataset_check_quota(ds, checkrefquota,
735 asize, est_inflight, &used_on_disk, &ref_rsrv);
737 mutex_exit(&dd->dd_lock);
743 * If this transaction will result in a net free of space,
744 * we want to let it through.
746 if (ignorequota || netfree || dd->dd_phys->dd_quota == 0)
749 quota = dd->dd_phys->dd_quota;
752 * Adjust the quota against the actual pool size at the root
753 * minus any outstanding deferred frees.
754 * To ensure that it's possible to remove files from a full
755 * pool without inducing transient overcommits, we throttle
756 * netfree transactions against a quota that is slightly larger,
757 * but still within the pool's allocation slop. In cases where
758 * we're very close to full, this will allow a steady trickle of
759 * removes to get through.
761 if (dd->dd_parent == NULL) {
762 spa_t *spa = dd->dd_pool->dp_spa;
763 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
764 deferred = metaslab_class_get_deferred(spa_normal_class(spa));
765 if (poolsize - deferred < quota) {
766 quota = poolsize - deferred;
772 * If they are requesting more space, and our current estimate
773 * is over quota, they get to try again unless the actual
774 * on-disk is over quota and there are no pending changes (which
775 * may free up space for us).
777 if (used_on_disk + est_inflight >= quota) {
778 if (est_inflight > 0 || used_on_disk < quota ||
779 (retval == ENOSPC && used_on_disk < quota + deferred))
781 dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
782 "quota=%lluK tr=%lluK err=%d\n",
783 used_on_disk>>10, est_inflight>>10,
784 quota>>10, asize>>10, retval);
785 mutex_exit(&dd->dd_lock);
789 /* We need to up our estimated delta before dropping dd_lock */
790 dd->dd_tempreserved[txgidx] += asize;
792 parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
794 mutex_exit(&dd->dd_lock);
796 tr = kmem_zalloc(sizeof (struct tempreserve), KM_PUSHPAGE);
799 list_insert_tail(tr_list, tr);
801 /* see if it's OK with our parent */
802 if (dd->dd_parent && parent_rsrv) {
803 boolean_t ismos = (dd->dd_phys->dd_head_dataset_obj == 0);
805 return (dsl_dir_tempreserve_impl(dd->dd_parent,
806 parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE));
813 * Reserve space in this dsl_dir, to be used in this tx's txg.
814 * After the space has been dirtied (and dsl_dir_willuse_space()
815 * has been called), the reservation should be canceled, using
816 * dsl_dir_tempreserve_clear().
819 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
820 uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
830 tr_list = kmem_alloc(sizeof (list_t), KM_PUSHPAGE);
831 list_create(tr_list, sizeof (struct tempreserve),
832 offsetof(struct tempreserve, tr_node));
833 ASSERT3S(asize, >, 0);
834 ASSERT3S(fsize, >=, 0);
836 err = arc_tempreserve_space(lsize, tx->tx_txg);
838 struct tempreserve *tr;
840 tr = kmem_zalloc(sizeof (struct tempreserve), KM_PUSHPAGE);
842 list_insert_tail(tr_list, tr);
844 err = dsl_pool_tempreserve_space(dd->dd_pool, asize, tx);
847 txg_delay(dd->dd_pool, tx->tx_txg, 1);
850 dsl_pool_memory_pressure(dd->dd_pool);
854 struct tempreserve *tr;
856 tr = kmem_zalloc(sizeof (struct tempreserve), KM_PUSHPAGE);
857 tr->tr_dp = dd->dd_pool;
859 list_insert_tail(tr_list, tr);
861 err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
862 FALSE, asize > usize, tr_list, tx, TRUE);
866 dsl_dir_tempreserve_clear(tr_list, tx);
868 *tr_cookiep = tr_list;
874 * Clear a temporary reservation that we previously made with
875 * dsl_dir_tempreserve_space().
878 dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
880 int txgidx = tx->tx_txg & TXG_MASK;
881 list_t *tr_list = tr_cookie;
882 struct tempreserve *tr;
884 ASSERT3U(tx->tx_txg, !=, 0);
886 if (tr_cookie == NULL)
889 while ((tr = list_head(tr_list))) {
891 dsl_pool_tempreserve_clear(tr->tr_dp, tr->tr_size, tx);
892 } else if (tr->tr_ds) {
893 mutex_enter(&tr->tr_ds->dd_lock);
894 ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
896 tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
897 mutex_exit(&tr->tr_ds->dd_lock);
899 arc_tempreserve_clear(tr->tr_size);
901 list_remove(tr_list, tr);
902 kmem_free(tr, sizeof (struct tempreserve));
905 kmem_free(tr_list, sizeof (list_t));
909 dsl_dir_willuse_space_impl(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
911 int64_t parent_space;
914 mutex_enter(&dd->dd_lock);
916 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
918 est_used = dsl_dir_space_towrite(dd) + dd->dd_phys->dd_used_bytes;
919 parent_space = parent_delta(dd, est_used, space);
920 mutex_exit(&dd->dd_lock);
922 /* Make sure that we clean up dd_space_to* */
923 dsl_dir_dirty(dd, tx);
925 /* XXX this is potentially expensive and unnecessary... */
926 if (parent_space && dd->dd_parent)
927 dsl_dir_willuse_space_impl(dd->dd_parent, parent_space, tx);
931 * Call in open context when we think we're going to write/free space,
932 * eg. when dirtying data. Be conservative (ie. OK to write less than
933 * this or free more than this, but don't write more or free less).
936 dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
938 dsl_pool_willuse_space(dd->dd_pool, space, tx);
939 dsl_dir_willuse_space_impl(dd, space, tx);
942 /* call from syncing context when we actually write/free space for this dd */
944 dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
945 int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
947 int64_t accounted_delta;
948 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
950 ASSERT(dmu_tx_is_syncing(tx));
951 ASSERT(type < DD_USED_NUM);
953 dsl_dir_dirty(dd, tx);
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 dd->dd_phys->dd_used_bytes += used;
964 dd->dd_phys->dd_uncompressed_bytes += uncompressed;
965 dd->dd_phys->dd_compressed_bytes += compressed;
967 if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
969 dd->dd_phys->dd_used_breakdown[type] >= -used);
970 dd->dd_phys->dd_used_breakdown[type] += used;
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);
982 mutex_exit(&dd->dd_lock);
984 if (dd->dd_parent != NULL) {
985 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
986 accounted_delta, compressed, uncompressed, tx);
987 dsl_dir_transfer_space(dd->dd_parent,
988 used - accounted_delta,
989 DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
994 dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
995 dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
997 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
999 ASSERT(dmu_tx_is_syncing(tx));
1000 ASSERT(oldtype < DD_USED_NUM);
1001 ASSERT(newtype < DD_USED_NUM);
1003 if (delta == 0 || !(dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN))
1006 dsl_dir_dirty(dd, tx);
1008 mutex_enter(&dd->dd_lock);
1010 dd->dd_phys->dd_used_breakdown[oldtype] >= delta :
1011 dd->dd_phys->dd_used_breakdown[newtype] >= -delta);
1012 ASSERT(dd->dd_phys->dd_used_bytes >= ABS(delta));
1013 dd->dd_phys->dd_used_breakdown[oldtype] -= delta;
1014 dd->dd_phys->dd_used_breakdown[newtype] += delta;
1016 mutex_exit(&dd->dd_lock);
1020 dsl_dir_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
1022 dsl_dataset_t *ds = arg1;
1023 dsl_dir_t *dd = ds->ds_dir;
1024 dsl_prop_setarg_t *psa = arg2;
1028 if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
1031 if (psa->psa_effective_value == 0)
1034 mutex_enter(&dd->dd_lock);
1036 * If we are doing the preliminary check in open context, and
1037 * there are pending changes, then don't fail it, since the
1038 * pending changes could under-estimate the amount of space to be
1041 towrite = dsl_dir_space_towrite(dd);
1042 if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1043 (psa->psa_effective_value < dd->dd_phys->dd_reserved ||
1044 psa->psa_effective_value < dd->dd_phys->dd_used_bytes + towrite)) {
1047 mutex_exit(&dd->dd_lock);
1051 extern dsl_syncfunc_t dsl_prop_set_sync;
1054 dsl_dir_set_quota_sync(void *arg1, void *arg2, dmu_tx_t *tx)
1056 dsl_dataset_t *ds = arg1;
1057 dsl_dir_t *dd = ds->ds_dir;
1058 dsl_prop_setarg_t *psa = arg2;
1059 uint64_t effective_value = psa->psa_effective_value;
1061 dsl_prop_set_sync(ds, psa, tx);
1062 DSL_PROP_CHECK_PREDICTION(dd, psa);
1064 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1066 mutex_enter(&dd->dd_lock);
1067 dd->dd_phys->dd_quota = effective_value;
1068 mutex_exit(&dd->dd_lock);
1070 spa_history_log_internal(LOG_DS_QUOTA, dd->dd_pool->dp_spa,
1071 tx, "%lld dataset = %llu ",
1072 (longlong_t)effective_value, dd->dd_phys->dd_head_dataset_obj);
1076 dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1080 dsl_prop_setarg_t psa;
1083 dsl_prop_setarg_init_uint64(&psa, "quota", source, "a);
1085 err = dsl_dataset_hold(ddname, FTAG, &ds);
1089 err = dsl_dir_open(ddname, FTAG, &dd, NULL);
1091 dsl_dataset_rele(ds, FTAG);
1095 ASSERT(ds->ds_dir == dd);
1098 * If someone removes a file, then tries to set the quota, we want to
1099 * make sure the file freeing takes effect.
1101 txg_wait_open(dd->dd_pool, 0);
1103 err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_quota_check,
1104 dsl_dir_set_quota_sync, ds, &psa, 0);
1106 dsl_dir_close(dd, FTAG);
1107 dsl_dataset_rele(ds, FTAG);
1112 dsl_dir_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
1114 dsl_dataset_t *ds = arg1;
1115 dsl_dir_t *dd = ds->ds_dir;
1116 dsl_prop_setarg_t *psa = arg2;
1117 uint64_t effective_value;
1118 uint64_t used, avail;
1121 if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
1124 effective_value = psa->psa_effective_value;
1127 * If we are doing the preliminary check in open context, the
1128 * space estimates may be inaccurate.
1130 if (!dmu_tx_is_syncing(tx))
1133 mutex_enter(&dd->dd_lock);
1134 used = dd->dd_phys->dd_used_bytes;
1135 mutex_exit(&dd->dd_lock);
1137 if (dd->dd_parent) {
1138 avail = dsl_dir_space_available(dd->dd_parent,
1141 avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1144 if (MAX(used, effective_value) > MAX(used, dd->dd_phys->dd_reserved)) {
1145 uint64_t delta = MAX(used, effective_value) -
1146 MAX(used, dd->dd_phys->dd_reserved);
1150 if (dd->dd_phys->dd_quota > 0 &&
1151 effective_value > dd->dd_phys->dd_quota)
1159 dsl_dir_set_reservation_sync(void *arg1, void *arg2, dmu_tx_t *tx)
1161 dsl_dataset_t *ds = arg1;
1162 dsl_dir_t *dd = ds->ds_dir;
1163 dsl_prop_setarg_t *psa = arg2;
1164 uint64_t effective_value = psa->psa_effective_value;
1168 dsl_prop_set_sync(ds, psa, tx);
1169 DSL_PROP_CHECK_PREDICTION(dd, psa);
1171 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1173 mutex_enter(&dd->dd_lock);
1174 used = dd->dd_phys->dd_used_bytes;
1175 delta = MAX(used, effective_value) -
1176 MAX(used, dd->dd_phys->dd_reserved);
1177 dd->dd_phys->dd_reserved = effective_value;
1179 if (dd->dd_parent != NULL) {
1180 /* Roll up this additional usage into our ancestors */
1181 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1184 mutex_exit(&dd->dd_lock);
1186 spa_history_log_internal(LOG_DS_RESERVATION, dd->dd_pool->dp_spa,
1187 tx, "%lld dataset = %llu",
1188 (longlong_t)effective_value, dd->dd_phys->dd_head_dataset_obj);
1192 dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
1193 uint64_t reservation)
1197 dsl_prop_setarg_t psa;
1200 dsl_prop_setarg_init_uint64(&psa, "reservation", source, &reservation);
1202 err = dsl_dataset_hold(ddname, FTAG, &ds);
1206 err = dsl_dir_open(ddname, FTAG, &dd, NULL);
1208 dsl_dataset_rele(ds, FTAG);
1212 ASSERT(ds->ds_dir == dd);
1214 err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_reservation_check,
1215 dsl_dir_set_reservation_sync, ds, &psa, 0);
1217 dsl_dir_close(dd, FTAG);
1218 dsl_dataset_rele(ds, FTAG);
1223 closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1225 for (; ds1; ds1 = ds1->dd_parent) {
1227 for (dd = ds2; dd; dd = dd->dd_parent) {
1236 * If delta is applied to dd, how much of that delta would be applied to
1237 * ancestor? Syncing context only.
1240 would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1245 mutex_enter(&dd->dd_lock);
1246 delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, delta);
1247 mutex_exit(&dd->dd_lock);
1248 return (would_change(dd->dd_parent, delta, ancestor));
1252 dsl_dir_t *newparent;
1253 const char *mynewname;
1257 dsl_dir_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
1259 dsl_dir_t *dd = arg1;
1260 struct renamearg *ra = arg2;
1261 dsl_pool_t *dp = dd->dd_pool;
1262 objset_t *mos = dp->dp_meta_objset;
1267 * There should only be one reference, from dmu_objset_rename().
1268 * Fleeting holds are also possible (eg, from "zfs list" getting
1269 * stats), but any that are present in open context will likely
1270 * be gone by syncing context, so only fail from syncing
1273 if (dmu_tx_is_syncing(tx) && dmu_buf_refcount(dd->dd_dbuf) > 1)
1276 /* check for existing name */
1277 err = zap_lookup(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
1278 ra->mynewname, 8, 1, &val);
1284 if (ra->newparent != dd->dd_parent) {
1285 /* is there enough space? */
1287 MAX(dd->dd_phys->dd_used_bytes, dd->dd_phys->dd_reserved);
1289 /* no rename into our descendant */
1290 if (closest_common_ancestor(dd, ra->newparent) == dd)
1293 if ((err = dsl_dir_transfer_possible(dd->dd_parent,
1294 ra->newparent, myspace)))
1302 dsl_dir_rename_sync(void *arg1, void *arg2, dmu_tx_t *tx)
1304 dsl_dir_t *dd = arg1;
1305 struct renamearg *ra = arg2;
1306 dsl_pool_t *dp = dd->dd_pool;
1307 objset_t *mos = dp->dp_meta_objset;
1310 ASSERT(dmu_buf_refcount(dd->dd_dbuf) <= 2);
1312 if (ra->newparent != dd->dd_parent) {
1313 dsl_dir_diduse_space(dd->dd_parent, 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 dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD,
1318 dd->dd_phys->dd_used_bytes,
1319 dd->dd_phys->dd_compressed_bytes,
1320 dd->dd_phys->dd_uncompressed_bytes, tx);
1322 if (dd->dd_phys->dd_reserved > dd->dd_phys->dd_used_bytes) {
1323 uint64_t unused_rsrv = dd->dd_phys->dd_reserved -
1324 dd->dd_phys->dd_used_bytes;
1326 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1327 -unused_rsrv, 0, 0, tx);
1328 dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD_RSRV,
1329 unused_rsrv, 0, 0, tx);
1333 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1335 /* remove from old parent zapobj */
1336 err = zap_remove(mos, dd->dd_parent->dd_phys->dd_child_dir_zapobj,
1338 ASSERT3U(err, ==, 0);
1340 (void) strcpy(dd->dd_myname, ra->mynewname);
1341 dsl_dir_close(dd->dd_parent, dd);
1342 dd->dd_phys->dd_parent_obj = ra->newparent->dd_object;
1343 VERIFY(0 == dsl_dir_open_obj(dd->dd_pool,
1344 ra->newparent->dd_object, NULL, dd, &dd->dd_parent));
1346 /* add to new parent zapobj */
1347 err = zap_add(mos, ra->newparent->dd_phys->dd_child_dir_zapobj,
1348 dd->dd_myname, 8, 1, &dd->dd_object, tx);
1349 ASSERT3U(err, ==, 0);
1351 spa_history_log_internal(LOG_DS_RENAME, dd->dd_pool->dp_spa,
1352 tx, "dataset = %llu", dd->dd_phys->dd_head_dataset_obj);
1356 dsl_dir_rename(dsl_dir_t *dd, const char *newname)
1358 struct renamearg ra;
1361 /* new parent should exist */
1362 err = dsl_dir_open(newname, FTAG, &ra.newparent, &ra.mynewname);
1366 /* can't rename to different pool */
1367 if (dd->dd_pool != ra.newparent->dd_pool) {
1372 /* new name should not already exist */
1373 if (ra.mynewname == NULL) {
1378 err = dsl_sync_task_do(dd->dd_pool,
1379 dsl_dir_rename_check, dsl_dir_rename_sync, dd, &ra, 3);
1382 dsl_dir_close(ra.newparent, FTAG);
1387 dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, uint64_t space)
1389 dsl_dir_t *ancestor;
1393 ancestor = closest_common_ancestor(sdd, tdd);
1394 adelta = would_change(sdd, -space, ancestor);
1395 avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
1403 dsl_dir_snap_cmtime(dsl_dir_t *dd)
1407 mutex_enter(&dd->dd_lock);
1408 t = dd->dd_snap_cmtime;
1409 mutex_exit(&dd->dd_lock);
1415 dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
1420 mutex_enter(&dd->dd_lock);
1421 dd->dd_snap_cmtime = t;
1422 mutex_exit(&dd->dd_lock);
1425 #if defined(_KERNEL) && defined(HAVE_SPL)
1426 EXPORT_SYMBOL(dsl_dir_set_quota);
1427 EXPORT_SYMBOL(dsl_dir_set_reservation);
1428 EXPORT_SYMBOL(dsl_dir_open);
1429 EXPORT_SYMBOL(dsl_dir_close);