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.
23 * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>.
24 * All rights reserved.
25 * Copyright (c) 2013 by Delphix. All rights reserved.
29 #include <sys/dmu_objset.h>
30 #include <sys/dmu_tx.h>
31 #include <sys/dsl_dataset.h>
32 #include <sys/dsl_dir.h>
33 #include <sys/dsl_prop.h>
34 #include <sys/dsl_synctask.h>
35 #include <sys/dsl_deleg.h>
37 #include <sys/metaslab.h>
41 #include <sys/sunddi.h>
44 #include <sys/zfs_vfsops.h>
46 #include "zfs_namecheck.h"
48 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
52 dsl_dir_evict(dmu_buf_t *db, void *arg)
55 dsl_pool_t *dp = dd->dd_pool;
58 for (t = 0; t < TXG_SIZE; t++) {
59 ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
60 ASSERT(dd->dd_tempreserved[t] == 0);
61 ASSERT(dd->dd_space_towrite[t] == 0);
65 dsl_dir_rele(dd->dd_parent, dd);
67 spa_close(dd->dd_pool->dp_spa, dd);
70 * The props callback list should have been cleaned up by
73 list_destroy(&dd->dd_prop_cbs);
74 mutex_destroy(&dd->dd_lock);
75 kmem_free(dd, sizeof (dsl_dir_t));
79 dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
80 const char *tail, void *tag, dsl_dir_t **ddp)
86 ASSERT(dsl_pool_config_held(dp));
88 err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
91 dd = dmu_buf_get_user(dbuf);
94 dmu_object_info_t doi;
95 dmu_object_info_from_db(dbuf, &doi);
96 ASSERT3U(doi.doi_type, ==, DMU_OT_DSL_DIR);
97 ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
103 dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
104 dd->dd_object = ddobj;
107 dd->dd_phys = dbuf->db_data;
108 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
110 list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t),
111 offsetof(dsl_prop_cb_record_t, cbr_node));
113 dsl_dir_snap_cmtime_update(dd);
115 if (dd->dd_phys->dd_parent_obj) {
116 err = dsl_dir_hold_obj(dp, dd->dd_phys->dd_parent_obj,
117 NULL, dd, &dd->dd_parent);
124 err = zap_lookup(dp->dp_meta_objset,
125 dd->dd_parent->dd_phys->dd_child_dir_zapobj,
126 tail, sizeof (foundobj), 1, &foundobj);
127 ASSERT(err || foundobj == ddobj);
129 (void) strcpy(dd->dd_myname, tail);
131 err = zap_value_search(dp->dp_meta_objset,
132 dd->dd_parent->dd_phys->dd_child_dir_zapobj,
133 ddobj, 0, dd->dd_myname);
138 (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
141 if (dsl_dir_is_clone(dd)) {
142 dmu_buf_t *origin_bonus;
143 dsl_dataset_phys_t *origin_phys;
146 * We can't open the origin dataset, because
147 * that would require opening this dsl_dir.
148 * Just look at its phys directly instead.
150 err = dmu_bonus_hold(dp->dp_meta_objset,
151 dd->dd_phys->dd_origin_obj, FTAG, &origin_bonus);
154 origin_phys = origin_bonus->db_data;
156 origin_phys->ds_creation_txg;
157 dmu_buf_rele(origin_bonus, FTAG);
160 winner = dmu_buf_set_user_ie(dbuf, dd, &dd->dd_phys,
164 dsl_dir_rele(dd->dd_parent, dd);
165 mutex_destroy(&dd->dd_lock);
166 kmem_free(dd, sizeof (dsl_dir_t));
169 spa_open_ref(dp->dp_spa, dd);
174 * The dsl_dir_t has both open-to-close and instantiate-to-evict
175 * holds on the spa. We need the open-to-close holds because
176 * otherwise the spa_refcnt wouldn't change when we open a
177 * dir which the spa also has open, so we could incorrectly
178 * think it was OK to unload/export/destroy the pool. We need
179 * the instantiate-to-evict hold because the dsl_dir_t has a
180 * pointer to the dd_pool, which has a pointer to the spa_t.
182 spa_open_ref(dp->dp_spa, tag);
183 ASSERT3P(dd->dd_pool, ==, dp);
184 ASSERT3U(dd->dd_object, ==, ddobj);
185 ASSERT3P(dd->dd_dbuf, ==, dbuf);
191 dsl_dir_rele(dd->dd_parent, dd);
192 mutex_destroy(&dd->dd_lock);
193 kmem_free(dd, sizeof (dsl_dir_t));
194 dmu_buf_rele(dbuf, tag);
199 dsl_dir_rele(dsl_dir_t *dd, void *tag)
201 dprintf_dd(dd, "%s\n", "");
202 spa_close(dd->dd_pool->dp_spa, tag);
203 dmu_buf_rele(dd->dd_dbuf, tag);
206 /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */
208 dsl_dir_name(dsl_dir_t *dd, char *buf)
211 dsl_dir_name(dd->dd_parent, buf);
212 (void) strcat(buf, "/");
216 if (!MUTEX_HELD(&dd->dd_lock)) {
218 * recursive mutex so that we can use
219 * dprintf_dd() with dd_lock held
221 mutex_enter(&dd->dd_lock);
222 (void) strcat(buf, dd->dd_myname);
223 mutex_exit(&dd->dd_lock);
225 (void) strcat(buf, dd->dd_myname);
229 /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
231 dsl_dir_namelen(dsl_dir_t *dd)
236 /* parent's name + 1 for the "/" */
237 result = dsl_dir_namelen(dd->dd_parent) + 1;
240 if (!MUTEX_HELD(&dd->dd_lock)) {
241 /* see dsl_dir_name */
242 mutex_enter(&dd->dd_lock);
243 result += strlen(dd->dd_myname);
244 mutex_exit(&dd->dd_lock);
246 result += strlen(dd->dd_myname);
253 getcomponent(const char *path, char *component, const char **nextp)
257 if ((path == NULL) || (path[0] == '\0'))
258 return (SET_ERROR(ENOENT));
259 /* This would be a good place to reserve some namespace... */
260 p = strpbrk(path, "/@");
261 if (p && (p[1] == '/' || p[1] == '@')) {
262 /* two separators in a row */
263 return (SET_ERROR(EINVAL));
265 if (p == NULL || p == path) {
267 * if the first thing is an @ or /, it had better be an
268 * @ and it had better not have any more ats or slashes,
269 * and it had better have something after the @.
272 (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
273 return (SET_ERROR(EINVAL));
274 if (strlen(path) >= MAXNAMELEN)
275 return (SET_ERROR(ENAMETOOLONG));
276 (void) strcpy(component, path);
278 } else if (p[0] == '/') {
279 if (p - path >= MAXNAMELEN)
280 return (SET_ERROR(ENAMETOOLONG));
281 (void) strncpy(component, path, p - path);
282 component[p - path] = '\0';
284 } else if (p[0] == '@') {
286 * if the next separator is an @, there better not be
289 if (strchr(path, '/'))
290 return (SET_ERROR(EINVAL));
291 if (p - path >= MAXNAMELEN)
292 return (SET_ERROR(ENAMETOOLONG));
293 (void) strncpy(component, path, p - path);
294 component[p - path] = '\0';
296 panic("invalid p=%p", (void *)p);
303 * Return the dsl_dir_t, and possibly the last component which couldn't
304 * be found in *tail. The name must be in the specified dsl_pool_t. This
305 * thread must hold the dp_config_rwlock for the pool. Returns NULL if the
306 * path is bogus, or if tail==NULL and we couldn't parse the whole name.
307 * (*tail)[0] == '@' means that the last component is a snapshot.
310 dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag,
311 dsl_dir_t **ddp, const char **tailp)
313 char buf[MAXNAMELEN];
314 const char *spaname, *next, *nextnext = NULL;
319 err = getcomponent(name, buf, &next);
323 /* Make sure the name is in the specified pool. */
324 spaname = spa_name(dp->dp_spa);
325 if (strcmp(buf, spaname) != 0)
326 return (SET_ERROR(EINVAL));
328 ASSERT(dsl_pool_config_held(dp));
330 err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
335 while (next != NULL) {
337 err = getcomponent(next, buf, &nextnext);
340 ASSERT(next[0] != '\0');
343 dprintf("looking up %s in obj%lld\n",
344 buf, dd->dd_phys->dd_child_dir_zapobj);
346 err = zap_lookup(dp->dp_meta_objset,
347 dd->dd_phys->dd_child_dir_zapobj,
348 buf, sizeof (ddobj), 1, &ddobj);
355 err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_ds);
358 dsl_dir_rele(dd, tag);
364 dsl_dir_rele(dd, tag);
369 * It's an error if there's more than one component left, or
370 * tailp==NULL and there's any component left.
373 (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
375 dsl_dir_rele(dd, tag);
376 dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
377 err = SET_ERROR(ENOENT);
386 dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
389 objset_t *mos = dp->dp_meta_objset;
391 dsl_dir_phys_t *ddphys;
394 ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
395 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
397 VERIFY(0 == zap_add(mos, pds->dd_phys->dd_child_dir_zapobj,
398 name, sizeof (uint64_t), 1, &ddobj, tx));
400 /* it's the root dir */
401 VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
402 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
404 VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
405 dmu_buf_will_dirty(dbuf, tx);
406 ddphys = dbuf->db_data;
408 ddphys->dd_creation_time = gethrestime_sec();
410 ddphys->dd_parent_obj = pds->dd_object;
411 ddphys->dd_props_zapobj = zap_create(mos,
412 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
413 ddphys->dd_child_dir_zapobj = zap_create(mos,
414 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
415 if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
416 ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
417 dmu_buf_rele(dbuf, FTAG);
423 dsl_dir_is_clone(dsl_dir_t *dd)
425 return (dd->dd_phys->dd_origin_obj &&
426 (dd->dd_pool->dp_origin_snap == NULL ||
427 dd->dd_phys->dd_origin_obj !=
428 dd->dd_pool->dp_origin_snap->ds_object));
432 dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
434 mutex_enter(&dd->dd_lock);
435 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
436 dd->dd_phys->dd_used_bytes);
437 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA, dd->dd_phys->dd_quota);
438 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
439 dd->dd_phys->dd_reserved);
440 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
441 dd->dd_phys->dd_compressed_bytes == 0 ? 100 :
442 (dd->dd_phys->dd_uncompressed_bytes * 100 /
443 dd->dd_phys->dd_compressed_bytes));
444 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
445 dd->dd_phys->dd_uncompressed_bytes);
446 if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
447 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
448 dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]);
449 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
450 dd->dd_phys->dd_used_breakdown[DD_USED_HEAD]);
451 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
452 dd->dd_phys->dd_used_breakdown[DD_USED_REFRSRV]);
453 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
454 dd->dd_phys->dd_used_breakdown[DD_USED_CHILD] +
455 dd->dd_phys->dd_used_breakdown[DD_USED_CHILD_RSRV]);
457 mutex_exit(&dd->dd_lock);
459 if (dsl_dir_is_clone(dd)) {
461 char buf[MAXNAMELEN];
463 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
464 dd->dd_phys->dd_origin_obj, FTAG, &ds));
465 dsl_dataset_name(ds, buf);
466 dsl_dataset_rele(ds, FTAG);
467 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
472 dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
474 dsl_pool_t *dp = dd->dd_pool;
478 if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {
479 /* up the hold count until we can be written out */
480 dmu_buf_add_ref(dd->dd_dbuf, dd);
485 parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
487 uint64_t old_accounted = MAX(used, dd->dd_phys->dd_reserved);
488 uint64_t new_accounted = MAX(used + delta, dd->dd_phys->dd_reserved);
489 return (new_accounted - old_accounted);
493 dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
495 ASSERT(dmu_tx_is_syncing(tx));
497 mutex_enter(&dd->dd_lock);
498 ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]);
499 dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
500 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
501 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
502 mutex_exit(&dd->dd_lock);
504 /* release the hold from dsl_dir_dirty */
505 dmu_buf_rele(dd->dd_dbuf, dd);
509 dsl_dir_space_towrite(dsl_dir_t *dd)
514 ASSERT(MUTEX_HELD(&dd->dd_lock));
516 for (i = 0; i < TXG_SIZE; i++) {
517 space += dd->dd_space_towrite[i&TXG_MASK];
518 ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
524 * How much space would dd have available if ancestor had delta applied
525 * to it? If ondiskonly is set, we're only interested in what's
526 * on-disk, not estimated pending changes.
529 dsl_dir_space_available(dsl_dir_t *dd,
530 dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
532 uint64_t parentspace, myspace, quota, used;
535 * If there are no restrictions otherwise, assume we have
536 * unlimited space available.
539 parentspace = UINT64_MAX;
541 if (dd->dd_parent != NULL) {
542 parentspace = dsl_dir_space_available(dd->dd_parent,
543 ancestor, delta, ondiskonly);
546 mutex_enter(&dd->dd_lock);
547 if (dd->dd_phys->dd_quota != 0)
548 quota = dd->dd_phys->dd_quota;
549 used = dd->dd_phys->dd_used_bytes;
551 used += dsl_dir_space_towrite(dd);
553 if (dd->dd_parent == NULL) {
554 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
555 quota = MIN(quota, poolsize);
558 if (dd->dd_phys->dd_reserved > used && parentspace != UINT64_MAX) {
560 * We have some space reserved, in addition to what our
563 parentspace += dd->dd_phys->dd_reserved - used;
566 if (dd == ancestor) {
568 ASSERT(used >= -delta);
570 if (parentspace != UINT64_MAX)
571 parentspace -= delta;
579 * the lesser of the space provided by our parent and
580 * the space left in our quota
582 myspace = MIN(parentspace, quota - used);
585 mutex_exit(&dd->dd_lock);
597 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
598 boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
599 dmu_tx_t *tx, boolean_t first)
601 uint64_t txg = tx->tx_txg;
602 uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
603 uint64_t deferred = 0;
604 struct tempreserve *tr;
606 int txgidx = txg & TXG_MASK;
608 uint64_t ref_rsrv = 0;
610 ASSERT3U(txg, !=, 0);
611 ASSERT3S(asize, >, 0);
613 mutex_enter(&dd->dd_lock);
616 * Check against the dsl_dir's quota. We don't add in the delta
617 * when checking for over-quota because they get one free hit.
619 est_inflight = dsl_dir_space_towrite(dd);
620 for (i = 0; i < TXG_SIZE; i++)
621 est_inflight += dd->dd_tempreserved[i];
622 used_on_disk = dd->dd_phys->dd_used_bytes;
625 * On the first iteration, fetch the dataset's used-on-disk and
626 * refreservation values. Also, if checkrefquota is set, test if
627 * allocating this space would exceed the dataset's refquota.
629 if (first && tx->tx_objset) {
631 dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
633 error = dsl_dataset_check_quota(ds, checkrefquota,
634 asize, est_inflight, &used_on_disk, &ref_rsrv);
636 mutex_exit(&dd->dd_lock);
642 * If this transaction will result in a net free of space,
643 * we want to let it through.
645 if (ignorequota || netfree || dd->dd_phys->dd_quota == 0)
648 quota = dd->dd_phys->dd_quota;
651 * Adjust the quota against the actual pool size at the root
652 * minus any outstanding deferred frees.
653 * To ensure that it's possible to remove files from a full
654 * pool without inducing transient overcommits, we throttle
655 * netfree transactions against a quota that is slightly larger,
656 * but still within the pool's allocation slop. In cases where
657 * we're very close to full, this will allow a steady trickle of
658 * removes to get through.
660 if (dd->dd_parent == NULL) {
661 spa_t *spa = dd->dd_pool->dp_spa;
662 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
663 deferred = metaslab_class_get_deferred(spa_normal_class(spa));
664 if (poolsize - deferred < quota) {
665 quota = poolsize - deferred;
671 * If they are requesting more space, and our current estimate
672 * is over quota, they get to try again unless the actual
673 * on-disk is over quota and there are no pending changes (which
674 * may free up space for us).
676 if (used_on_disk + est_inflight >= quota) {
677 if (est_inflight > 0 || used_on_disk < quota ||
678 (retval == ENOSPC && used_on_disk < quota + deferred))
680 dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
681 "quota=%lluK tr=%lluK err=%d\n",
682 used_on_disk>>10, est_inflight>>10,
683 quota>>10, asize>>10, retval);
684 mutex_exit(&dd->dd_lock);
685 return (SET_ERROR(retval));
688 /* We need to up our estimated delta before dropping dd_lock */
689 dd->dd_tempreserved[txgidx] += asize;
691 parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
693 mutex_exit(&dd->dd_lock);
695 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
698 list_insert_tail(tr_list, tr);
700 /* see if it's OK with our parent */
701 if (dd->dd_parent && parent_rsrv) {
702 boolean_t ismos = (dd->dd_phys->dd_head_dataset_obj == 0);
704 return (dsl_dir_tempreserve_impl(dd->dd_parent,
705 parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE));
712 * Reserve space in this dsl_dir, to be used in this tx's txg.
713 * After the space has been dirtied (and dsl_dir_willuse_space()
714 * has been called), the reservation should be canceled, using
715 * dsl_dir_tempreserve_clear().
718 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
719 uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
729 tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
730 list_create(tr_list, sizeof (struct tempreserve),
731 offsetof(struct tempreserve, tr_node));
732 ASSERT3S(asize, >, 0);
733 ASSERT3S(fsize, >=, 0);
735 err = arc_tempreserve_space(lsize, tx->tx_txg);
737 struct tempreserve *tr;
739 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
741 list_insert_tail(tr_list, tr);
745 * If arc_memory_throttle() detected that pageout
746 * is running and we are low on memory, we delay new
747 * non-pageout transactions to give pageout an
750 * It is unfortunate to be delaying while the caller's
753 txg_delay(dd->dd_pool, tx->tx_txg,
754 MSEC2NSEC(10), MSEC2NSEC(10));
755 err = SET_ERROR(ERESTART);
760 err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
761 FALSE, asize > usize, tr_list, tx, TRUE);
765 dsl_dir_tempreserve_clear(tr_list, tx);
767 *tr_cookiep = tr_list;
773 * Clear a temporary reservation that we previously made with
774 * dsl_dir_tempreserve_space().
777 dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
779 int txgidx = tx->tx_txg & TXG_MASK;
780 list_t *tr_list = tr_cookie;
781 struct tempreserve *tr;
783 ASSERT3U(tx->tx_txg, !=, 0);
785 if (tr_cookie == NULL)
788 while ((tr = list_head(tr_list)) != NULL) {
790 mutex_enter(&tr->tr_ds->dd_lock);
791 ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
793 tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
794 mutex_exit(&tr->tr_ds->dd_lock);
796 arc_tempreserve_clear(tr->tr_size);
798 list_remove(tr_list, tr);
799 kmem_free(tr, sizeof (struct tempreserve));
802 kmem_free(tr_list, sizeof (list_t));
806 * This should be called from open context when we think we're going to write
807 * or free space, for example when dirtying data. Be conservative; it's okay
808 * to write less space or free more, but we don't want to write more or free
809 * less than the amount specified.
812 dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
814 int64_t parent_space;
817 mutex_enter(&dd->dd_lock);
819 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
821 est_used = dsl_dir_space_towrite(dd) + dd->dd_phys->dd_used_bytes;
822 parent_space = parent_delta(dd, est_used, space);
823 mutex_exit(&dd->dd_lock);
825 /* Make sure that we clean up dd_space_to* */
826 dsl_dir_dirty(dd, tx);
828 /* XXX this is potentially expensive and unnecessary... */
829 if (parent_space && dd->dd_parent)
830 dsl_dir_willuse_space(dd->dd_parent, parent_space, tx);
833 /* call from syncing context when we actually write/free space for this dd */
835 dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
836 int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
838 int64_t accounted_delta;
839 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
841 ASSERT(dmu_tx_is_syncing(tx));
842 ASSERT(type < DD_USED_NUM);
845 mutex_enter(&dd->dd_lock);
846 accounted_delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, used);
847 ASSERT(used >= 0 || dd->dd_phys->dd_used_bytes >= -used);
848 ASSERT(compressed >= 0 ||
849 dd->dd_phys->dd_compressed_bytes >= -compressed);
850 ASSERT(uncompressed >= 0 ||
851 dd->dd_phys->dd_uncompressed_bytes >= -uncompressed);
852 dmu_buf_will_dirty(dd->dd_dbuf, tx);
853 dd->dd_phys->dd_used_bytes += used;
854 dd->dd_phys->dd_uncompressed_bytes += uncompressed;
855 dd->dd_phys->dd_compressed_bytes += compressed;
857 if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
859 dd->dd_phys->dd_used_breakdown[type] >= -used);
860 dd->dd_phys->dd_used_breakdown[type] += used;
864 for (t = 0; t < DD_USED_NUM; t++)
865 u += dd->dd_phys->dd_used_breakdown[t];
866 ASSERT3U(u, ==, dd->dd_phys->dd_used_bytes);
870 mutex_exit(&dd->dd_lock);
872 if (dd->dd_parent != NULL) {
873 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
874 accounted_delta, compressed, uncompressed, tx);
875 dsl_dir_transfer_space(dd->dd_parent,
876 used - accounted_delta,
877 DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
882 dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
883 dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
885 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
887 ASSERT(dmu_tx_is_syncing(tx));
888 ASSERT(oldtype < DD_USED_NUM);
889 ASSERT(newtype < DD_USED_NUM);
891 if (delta == 0 || !(dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN))
895 mutex_enter(&dd->dd_lock);
897 dd->dd_phys->dd_used_breakdown[oldtype] >= delta :
898 dd->dd_phys->dd_used_breakdown[newtype] >= -delta);
899 ASSERT(dd->dd_phys->dd_used_bytes >= ABS(delta));
900 dmu_buf_will_dirty(dd->dd_dbuf, tx);
901 dd->dd_phys->dd_used_breakdown[oldtype] -= delta;
902 dd->dd_phys->dd_used_breakdown[newtype] += delta;
904 mutex_exit(&dd->dd_lock);
907 typedef struct dsl_dir_set_qr_arg {
908 const char *ddsqra_name;
909 zprop_source_t ddsqra_source;
910 uint64_t ddsqra_value;
911 } dsl_dir_set_qr_arg_t;
914 dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)
916 dsl_dir_set_qr_arg_t *ddsqra = arg;
917 dsl_pool_t *dp = dmu_tx_pool(tx);
920 uint64_t towrite, newval;
922 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
926 error = dsl_prop_predict(ds->ds_dir, "quota",
927 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
929 dsl_dataset_rele(ds, FTAG);
934 dsl_dataset_rele(ds, FTAG);
938 mutex_enter(&ds->ds_dir->dd_lock);
940 * If we are doing the preliminary check in open context, and
941 * there are pending changes, then don't fail it, since the
942 * pending changes could under-estimate the amount of space to be
945 towrite = dsl_dir_space_towrite(ds->ds_dir);
946 if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
947 (newval < ds->ds_dir->dd_phys->dd_reserved ||
948 newval < ds->ds_dir->dd_phys->dd_used_bytes + towrite)) {
949 error = SET_ERROR(ENOSPC);
951 mutex_exit(&ds->ds_dir->dd_lock);
952 dsl_dataset_rele(ds, FTAG);
957 dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)
959 dsl_dir_set_qr_arg_t *ddsqra = arg;
960 dsl_pool_t *dp = dmu_tx_pool(tx);
964 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
966 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
967 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),
968 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
969 &ddsqra->ddsqra_value, tx);
971 VERIFY0(dsl_prop_get_int_ds(ds,
972 zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));
974 newval = ddsqra->ddsqra_value;
975 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
976 zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);
979 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
980 mutex_enter(&ds->ds_dir->dd_lock);
981 ds->ds_dir->dd_phys->dd_quota = newval;
982 mutex_exit(&ds->ds_dir->dd_lock);
983 dsl_dataset_rele(ds, FTAG);
987 dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
989 dsl_dir_set_qr_arg_t ddsqra;
991 ddsqra.ddsqra_name = ddname;
992 ddsqra.ddsqra_source = source;
993 ddsqra.ddsqra_value = quota;
995 return (dsl_sync_task(ddname, dsl_dir_set_quota_check,
996 dsl_dir_set_quota_sync, &ddsqra, 0));
1000 dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)
1002 dsl_dir_set_qr_arg_t *ddsqra = arg;
1003 dsl_pool_t *dp = dmu_tx_pool(tx);
1006 uint64_t newval, used, avail;
1009 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1015 * If we are doing the preliminary check in open context, the
1016 * space estimates may be inaccurate.
1018 if (!dmu_tx_is_syncing(tx)) {
1019 dsl_dataset_rele(ds, FTAG);
1023 error = dsl_prop_predict(ds->ds_dir,
1024 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1025 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1027 dsl_dataset_rele(ds, FTAG);
1031 mutex_enter(&dd->dd_lock);
1032 used = dd->dd_phys->dd_used_bytes;
1033 mutex_exit(&dd->dd_lock);
1035 if (dd->dd_parent) {
1036 avail = dsl_dir_space_available(dd->dd_parent,
1039 avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1042 if (MAX(used, newval) > MAX(used, dd->dd_phys->dd_reserved)) {
1043 uint64_t delta = MAX(used, newval) -
1044 MAX(used, dd->dd_phys->dd_reserved);
1046 if (delta > avail ||
1047 (dd->dd_phys->dd_quota > 0 &&
1048 newval > dd->dd_phys->dd_quota))
1049 error = SET_ERROR(ENOSPC);
1052 dsl_dataset_rele(ds, FTAG);
1057 dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
1062 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1064 mutex_enter(&dd->dd_lock);
1065 used = dd->dd_phys->dd_used_bytes;
1066 delta = MAX(used, value) - MAX(used, dd->dd_phys->dd_reserved);
1067 dd->dd_phys->dd_reserved = value;
1069 if (dd->dd_parent != NULL) {
1070 /* Roll up this additional usage into our ancestors */
1071 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1074 mutex_exit(&dd->dd_lock);
1078 dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)
1080 dsl_dir_set_qr_arg_t *ddsqra = arg;
1081 dsl_pool_t *dp = dmu_tx_pool(tx);
1085 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1087 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1088 dsl_prop_set_sync_impl(ds,
1089 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1090 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1091 &ddsqra->ddsqra_value, tx);
1093 VERIFY0(dsl_prop_get_int_ds(ds,
1094 zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));
1096 newval = ddsqra->ddsqra_value;
1097 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1098 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1099 (longlong_t)newval);
1102 dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);
1103 dsl_dataset_rele(ds, FTAG);
1107 dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
1108 uint64_t reservation)
1110 dsl_dir_set_qr_arg_t ddsqra;
1112 ddsqra.ddsqra_name = ddname;
1113 ddsqra.ddsqra_source = source;
1114 ddsqra.ddsqra_value = reservation;
1116 return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,
1117 dsl_dir_set_reservation_sync, &ddsqra, 0));
1121 closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1123 for (; ds1; ds1 = ds1->dd_parent) {
1125 for (dd = ds2; dd; dd = dd->dd_parent) {
1134 * If delta is applied to dd, how much of that delta would be applied to
1135 * ancestor? Syncing context only.
1138 would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1143 mutex_enter(&dd->dd_lock);
1144 delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, delta);
1145 mutex_exit(&dd->dd_lock);
1146 return (would_change(dd->dd_parent, delta, ancestor));
1149 typedef struct dsl_dir_rename_arg {
1150 const char *ddra_oldname;
1151 const char *ddra_newname;
1152 } dsl_dir_rename_arg_t;
1156 dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1159 char namebuf[MAXNAMELEN];
1161 dsl_dataset_name(ds, namebuf);
1163 if (strlen(namebuf) + *deltap >= MAXNAMELEN)
1164 return (SET_ERROR(ENAMETOOLONG));
1169 dsl_dir_rename_check(void *arg, dmu_tx_t *tx)
1171 dsl_dir_rename_arg_t *ddra = arg;
1172 dsl_pool_t *dp = dmu_tx_pool(tx);
1173 dsl_dir_t *dd, *newparent;
1174 const char *mynewname;
1176 int delta = strlen(ddra->ddra_newname) - strlen(ddra->ddra_oldname);
1178 /* target dir should exist */
1179 error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);
1183 /* new parent should exist */
1184 error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,
1185 &newparent, &mynewname);
1187 dsl_dir_rele(dd, FTAG);
1191 /* can't rename to different pool */
1192 if (dd->dd_pool != newparent->dd_pool) {
1193 dsl_dir_rele(newparent, FTAG);
1194 dsl_dir_rele(dd, FTAG);
1195 return (SET_ERROR(ENXIO));
1198 /* new name should not already exist */
1199 if (mynewname == NULL) {
1200 dsl_dir_rele(newparent, FTAG);
1201 dsl_dir_rele(dd, FTAG);
1202 return (SET_ERROR(EEXIST));
1205 /* if the name length is growing, validate child name lengths */
1207 error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,
1208 &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1210 dsl_dir_rele(newparent, FTAG);
1211 dsl_dir_rele(dd, FTAG);
1216 if (newparent != dd->dd_parent) {
1217 /* is there enough space? */
1219 MAX(dd->dd_phys->dd_used_bytes, dd->dd_phys->dd_reserved);
1221 /* no rename into our descendant */
1222 if (closest_common_ancestor(dd, newparent) == dd) {
1223 dsl_dir_rele(newparent, FTAG);
1224 dsl_dir_rele(dd, FTAG);
1225 return (SET_ERROR(EINVAL));
1228 error = dsl_dir_transfer_possible(dd->dd_parent,
1229 newparent, myspace);
1231 dsl_dir_rele(newparent, FTAG);
1232 dsl_dir_rele(dd, FTAG);
1237 dsl_dir_rele(newparent, FTAG);
1238 dsl_dir_rele(dd, FTAG);
1243 dsl_dir_rename_sync(void *arg, dmu_tx_t *tx)
1245 dsl_dir_rename_arg_t *ddra = arg;
1246 dsl_pool_t *dp = dmu_tx_pool(tx);
1247 dsl_dir_t *dd, *newparent;
1248 const char *mynewname;
1250 objset_t *mos = dp->dp_meta_objset;
1252 VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL));
1253 VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent,
1256 /* Log this before we change the name. */
1257 spa_history_log_internal_dd(dd, "rename", tx,
1258 "-> %s", ddra->ddra_newname);
1260 if (newparent != dd->dd_parent) {
1261 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1262 -dd->dd_phys->dd_used_bytes,
1263 -dd->dd_phys->dd_compressed_bytes,
1264 -dd->dd_phys->dd_uncompressed_bytes, tx);
1265 dsl_dir_diduse_space(newparent, DD_USED_CHILD,
1266 dd->dd_phys->dd_used_bytes,
1267 dd->dd_phys->dd_compressed_bytes,
1268 dd->dd_phys->dd_uncompressed_bytes, tx);
1270 if (dd->dd_phys->dd_reserved > dd->dd_phys->dd_used_bytes) {
1271 uint64_t unused_rsrv = dd->dd_phys->dd_reserved -
1272 dd->dd_phys->dd_used_bytes;
1274 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1275 -unused_rsrv, 0, 0, tx);
1276 dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV,
1277 unused_rsrv, 0, 0, tx);
1281 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1283 /* remove from old parent zapobj */
1284 error = zap_remove(mos, dd->dd_parent->dd_phys->dd_child_dir_zapobj,
1288 (void) strcpy(dd->dd_myname, mynewname);
1289 dsl_dir_rele(dd->dd_parent, dd);
1290 dd->dd_phys->dd_parent_obj = newparent->dd_object;
1291 VERIFY0(dsl_dir_hold_obj(dp,
1292 newparent->dd_object, NULL, dd, &dd->dd_parent));
1294 /* add to new parent zapobj */
1295 VERIFY0(zap_add(mos, newparent->dd_phys->dd_child_dir_zapobj,
1296 dd->dd_myname, 8, 1, &dd->dd_object, tx));
1300 zfsvfs_update_fromname(ddra->ddra_oldname, ddra->ddra_newname);
1301 zvol_rename_minors(ddra->ddra_oldname, ddra->ddra_newname);
1305 dsl_prop_notify_all(dd);
1307 dsl_dir_rele(newparent, FTAG);
1308 dsl_dir_rele(dd, FTAG);
1312 dsl_dir_rename(const char *oldname, const char *newname)
1314 dsl_dir_rename_arg_t ddra;
1316 ddra.ddra_oldname = oldname;
1317 ddra.ddra_newname = newname;
1319 return (dsl_sync_task(oldname,
1320 dsl_dir_rename_check, dsl_dir_rename_sync, &ddra, 3));
1324 dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, uint64_t space)
1326 dsl_dir_t *ancestor;
1330 ancestor = closest_common_ancestor(sdd, tdd);
1331 adelta = would_change(sdd, -space, ancestor);
1332 avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
1334 return (SET_ERROR(ENOSPC));
1340 dsl_dir_snap_cmtime(dsl_dir_t *dd)
1344 mutex_enter(&dd->dd_lock);
1345 t = dd->dd_snap_cmtime;
1346 mutex_exit(&dd->dd_lock);
1352 dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
1357 mutex_enter(&dd->dd_lock);
1358 dd->dd_snap_cmtime = t;
1359 mutex_exit(&dd->dd_lock);