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
);
195 dsl_dir_close(dsl_dir_t
*dd
, void *tag
)
197 dprintf_dd(dd
, "%s\n", "");
198 spa_close(dd
->dd_pool
->dp_spa
, tag
);
199 dmu_buf_rele(dd
->dd_dbuf
, tag
);
202 /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */
204 dsl_dir_name(dsl_dir_t
*dd
, char *buf
)
207 dsl_dir_name(dd
->dd_parent
, buf
);
208 (void) strcat(buf
, "/");
212 if (!MUTEX_HELD(&dd
->dd_lock
)) {
214 * recursive mutex so that we can use
215 * dprintf_dd() with dd_lock held
217 mutex_enter(&dd
->dd_lock
);
218 (void) strcat(buf
, dd
->dd_myname
);
219 mutex_exit(&dd
->dd_lock
);
221 (void) strcat(buf
, dd
->dd_myname
);
225 /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
227 dsl_dir_namelen(dsl_dir_t
*dd
)
232 /* parent's name + 1 for the "/" */
233 result
= dsl_dir_namelen(dd
->dd_parent
) + 1;
236 if (!MUTEX_HELD(&dd
->dd_lock
)) {
237 /* see dsl_dir_name */
238 mutex_enter(&dd
->dd_lock
);
239 result
+= strlen(dd
->dd_myname
);
240 mutex_exit(&dd
->dd_lock
);
242 result
+= strlen(dd
->dd_myname
);
249 getcomponent(const char *path
, char *component
, const char **nextp
)
252 if ((path
== NULL
) || (path
[0] == '\0'))
254 /* This would be a good place to reserve some namespace... */
255 p
= strpbrk(path
, "/@");
256 if (p
&& (p
[1] == '/' || p
[1] == '@')) {
257 /* two separators in a row */
260 if (p
== NULL
|| p
== path
) {
262 * if the first thing is an @ or /, it had better be an
263 * @ and it had better not have any more ats or slashes,
264 * and it had better have something after the @.
267 (p
[0] != '@' || strpbrk(path
+1, "/@") || p
[1] == '\0'))
269 if (strlen(path
) >= MAXNAMELEN
)
270 return (ENAMETOOLONG
);
271 (void) strcpy(component
, path
);
273 } else if (p
[0] == '/') {
274 if (p
-path
>= MAXNAMELEN
)
275 return (ENAMETOOLONG
);
276 (void) strncpy(component
, path
, p
- path
);
277 component
[p
-path
] = '\0';
279 } else if (p
[0] == '@') {
281 * if the next separator is an @, there better not be
284 if (strchr(path
, '/'))
286 if (p
-path
>= MAXNAMELEN
)
287 return (ENAMETOOLONG
);
288 (void) strncpy(component
, path
, p
- path
);
289 component
[p
-path
] = '\0';
291 ASSERT(!"invalid p");
298 * same as dsl_dir_open, ignore the first component of name and use the
302 dsl_dir_open_spa(spa_t
*spa
, const char *name
, void *tag
,
303 dsl_dir_t
**ddp
, const char **tailp
)
306 const char *next
, *nextnext
= NULL
;
311 int openedspa
= FALSE
;
313 dprintf("%s\n", name
);
315 buf
= kmem_alloc(MAXNAMELEN
, KM_PUSHPAGE
);
316 err
= getcomponent(name
, buf
, &next
);
320 err
= spa_open(buf
, &spa
, FTAG
);
322 dprintf("spa_open(%s) failed\n", buf
);
327 /* XXX this assertion belongs in spa_open */
328 ASSERT(!dsl_pool_sync_context(spa_get_dsl(spa
)));
331 dp
= spa_get_dsl(spa
);
333 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
334 err
= dsl_dir_open_obj(dp
, dp
->dp_root_dir_obj
, NULL
, tag
, &dd
);
336 rw_exit(&dp
->dp_config_rwlock
);
338 spa_close(spa
, FTAG
);
342 while (next
!= NULL
) {
344 err
= getcomponent(next
, buf
, &nextnext
);
347 ASSERT(next
[0] != '\0');
350 dprintf("looking up %s in obj%lld\n",
351 buf
, dd
->dd_phys
->dd_child_dir_zapobj
);
353 err
= zap_lookup(dp
->dp_meta_objset
,
354 dd
->dd_phys
->dd_child_dir_zapobj
,
355 buf
, sizeof (ddobj
), 1, &ddobj
);
362 err
= dsl_dir_open_obj(dp
, ddobj
, buf
, tag
, &child_ds
);
365 dsl_dir_close(dd
, tag
);
369 rw_exit(&dp
->dp_config_rwlock
);
372 dsl_dir_close(dd
, tag
);
374 spa_close(spa
, FTAG
);
379 * It's an error if there's more than one component left, or
380 * tailp==NULL and there's any component left.
383 (tailp
== NULL
|| (nextnext
&& nextnext
[0] != '\0'))) {
385 dsl_dir_close(dd
, tag
);
386 dprintf("next=%p (%s) tail=%p\n", next
, next
?next
:"", tailp
);
392 spa_close(spa
, FTAG
);
395 kmem_free(buf
, MAXNAMELEN
);
400 * Return the dsl_dir_t, and possibly the last component which couldn't
401 * be found in *tail. Return NULL if the path is bogus, or if
402 * tail==NULL and we couldn't parse the whole name. (*tail)[0] == '@'
403 * means that the last component is a snapshot.
406 dsl_dir_open(const char *name
, void *tag
, dsl_dir_t
**ddp
, const char **tailp
)
408 return (dsl_dir_open_spa(NULL
, name
, tag
, ddp
, tailp
));
412 dsl_dir_create_sync(dsl_pool_t
*dp
, dsl_dir_t
*pds
, const char *name
,
415 objset_t
*mos
= dp
->dp_meta_objset
;
417 dsl_dir_phys_t
*ddphys
;
420 ddobj
= dmu_object_alloc(mos
, DMU_OT_DSL_DIR
, 0,
421 DMU_OT_DSL_DIR
, sizeof (dsl_dir_phys_t
), tx
);
423 VERIFY(0 == zap_add(mos
, pds
->dd_phys
->dd_child_dir_zapobj
,
424 name
, sizeof (uint64_t), 1, &ddobj
, tx
));
426 /* it's the root dir */
427 VERIFY(0 == zap_add(mos
, DMU_POOL_DIRECTORY_OBJECT
,
428 DMU_POOL_ROOT_DATASET
, sizeof (uint64_t), 1, &ddobj
, tx
));
430 VERIFY(0 == dmu_bonus_hold(mos
, ddobj
, FTAG
, &dbuf
));
431 dmu_buf_will_dirty(dbuf
, tx
);
432 ddphys
= dbuf
->db_data
;
434 ddphys
->dd_creation_time
= gethrestime_sec();
436 ddphys
->dd_parent_obj
= pds
->dd_object
;
437 ddphys
->dd_props_zapobj
= zap_create(mos
,
438 DMU_OT_DSL_PROPS
, DMU_OT_NONE
, 0, tx
);
439 ddphys
->dd_child_dir_zapobj
= zap_create(mos
,
440 DMU_OT_DSL_DIR_CHILD_MAP
, DMU_OT_NONE
, 0, tx
);
441 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_USED_BREAKDOWN
)
442 ddphys
->dd_flags
|= DD_FLAG_USED_BREAKDOWN
;
443 dmu_buf_rele(dbuf
, FTAG
);
450 dsl_dir_destroy_check(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
452 dsl_dataset_t
*ds
= arg1
;
453 dsl_dir_t
*dd
= ds
->ds_dir
;
454 dsl_pool_t
*dp
= dd
->dd_pool
;
455 objset_t
*mos
= dp
->dp_meta_objset
;
460 * There should be exactly two holds, both from
461 * dsl_dataset_destroy: one on the dd directory, and one on its
462 * head ds. If there are more holds, then a concurrent thread is
463 * performing a lookup inside this dir while we're trying to destroy
464 * it. To minimize this possibility, we perform this check only
465 * in syncing context and fail the operation if we encounter
466 * additional holds. The dp_config_rwlock ensures that nobody else
467 * opens it after we check.
469 if (dmu_tx_is_syncing(tx
) && dmu_buf_refcount(dd
->dd_dbuf
) > 2)
472 err
= zap_count(mos
, dd
->dd_phys
->dd_child_dir_zapobj
, &count
);
482 dsl_dir_destroy_sync(void *arg1
, void *tag
, dmu_tx_t
*tx
)
484 dsl_dataset_t
*ds
= arg1
;
485 dsl_dir_t
*dd
= ds
->ds_dir
;
486 objset_t
*mos
= dd
->dd_pool
->dp_meta_objset
;
487 dsl_prop_setarg_t psa
;
492 ASSERT(RW_WRITE_HELD(&dd
->dd_pool
->dp_config_rwlock
));
493 ASSERT(dd
->dd_phys
->dd_head_dataset_obj
== 0);
495 /* Remove our reservation. */
496 dsl_prop_setarg_init_uint64(&psa
, "reservation",
497 (ZPROP_SRC_NONE
| ZPROP_SRC_LOCAL
| ZPROP_SRC_RECEIVED
),
499 psa
.psa_effective_value
= 0; /* predict default value */
501 dsl_dir_set_reservation_sync(ds
, &psa
, tx
);
503 ASSERT0(dd
->dd_phys
->dd_used_bytes
);
504 ASSERT0(dd
->dd_phys
->dd_reserved
);
505 for (t
= 0; t
< DD_USED_NUM
; t
++)
506 ASSERT0(dd
->dd_phys
->dd_used_breakdown
[t
]);
508 VERIFY(0 == zap_destroy(mos
, dd
->dd_phys
->dd_child_dir_zapobj
, tx
));
509 VERIFY(0 == zap_destroy(mos
, dd
->dd_phys
->dd_props_zapobj
, tx
));
510 VERIFY(0 == dsl_deleg_destroy(mos
, dd
->dd_phys
->dd_deleg_zapobj
, tx
));
511 VERIFY(0 == zap_remove(mos
,
512 dd
->dd_parent
->dd_phys
->dd_child_dir_zapobj
, dd
->dd_myname
, tx
));
515 dsl_dir_close(dd
, tag
);
516 VERIFY(0 == dmu_object_free(mos
, obj
, tx
));
520 dsl_dir_is_clone(dsl_dir_t
*dd
)
522 return (dd
->dd_phys
->dd_origin_obj
&&
523 (dd
->dd_pool
->dp_origin_snap
== NULL
||
524 dd
->dd_phys
->dd_origin_obj
!=
525 dd
->dd_pool
->dp_origin_snap
->ds_object
));
529 dsl_dir_stats(dsl_dir_t
*dd
, nvlist_t
*nv
)
531 mutex_enter(&dd
->dd_lock
);
532 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USED
,
533 dd
->dd_phys
->dd_used_bytes
);
534 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_QUOTA
, dd
->dd_phys
->dd_quota
);
535 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_RESERVATION
,
536 dd
->dd_phys
->dd_reserved
);
537 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_COMPRESSRATIO
,
538 dd
->dd_phys
->dd_compressed_bytes
== 0 ? 100 :
539 (dd
->dd_phys
->dd_uncompressed_bytes
* 100 /
540 dd
->dd_phys
->dd_compressed_bytes
));
541 if (dd
->dd_phys
->dd_flags
& DD_FLAG_USED_BREAKDOWN
) {
542 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USEDSNAP
,
543 dd
->dd_phys
->dd_used_breakdown
[DD_USED_SNAP
]);
544 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USEDDS
,
545 dd
->dd_phys
->dd_used_breakdown
[DD_USED_HEAD
]);
546 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USEDREFRESERV
,
547 dd
->dd_phys
->dd_used_breakdown
[DD_USED_REFRSRV
]);
548 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USEDCHILD
,
549 dd
->dd_phys
->dd_used_breakdown
[DD_USED_CHILD
] +
550 dd
->dd_phys
->dd_used_breakdown
[DD_USED_CHILD_RSRV
]);
552 mutex_exit(&dd
->dd_lock
);
554 rw_enter(&dd
->dd_pool
->dp_config_rwlock
, RW_READER
);
555 if (dsl_dir_is_clone(dd
)) {
557 char buf
[MAXNAMELEN
];
559 VERIFY(0 == dsl_dataset_hold_obj(dd
->dd_pool
,
560 dd
->dd_phys
->dd_origin_obj
, FTAG
, &ds
));
561 dsl_dataset_name(ds
, buf
);
562 dsl_dataset_rele(ds
, FTAG
);
563 dsl_prop_nvlist_add_string(nv
, ZFS_PROP_ORIGIN
, buf
);
565 rw_exit(&dd
->dd_pool
->dp_config_rwlock
);
569 dsl_dir_dirty(dsl_dir_t
*dd
, dmu_tx_t
*tx
)
571 dsl_pool_t
*dp
= dd
->dd_pool
;
575 if (txg_list_add(&dp
->dp_dirty_dirs
, dd
, tx
->tx_txg
) == 0) {
576 /* up the hold count until we can be written out */
577 dmu_buf_add_ref(dd
->dd_dbuf
, dd
);
582 parent_delta(dsl_dir_t
*dd
, uint64_t used
, int64_t delta
)
584 uint64_t old_accounted
= MAX(used
, dd
->dd_phys
->dd_reserved
);
585 uint64_t new_accounted
= MAX(used
+ delta
, dd
->dd_phys
->dd_reserved
);
586 return (new_accounted
- old_accounted
);
590 dsl_dir_sync(dsl_dir_t
*dd
, dmu_tx_t
*tx
)
592 ASSERT(dmu_tx_is_syncing(tx
));
594 mutex_enter(&dd
->dd_lock
);
595 ASSERT0(dd
->dd_tempreserved
[tx
->tx_txg
&TXG_MASK
]);
596 dprintf_dd(dd
, "txg=%llu towrite=%lluK\n", tx
->tx_txg
,
597 dd
->dd_space_towrite
[tx
->tx_txg
&TXG_MASK
] / 1024);
598 dd
->dd_space_towrite
[tx
->tx_txg
&TXG_MASK
] = 0;
599 mutex_exit(&dd
->dd_lock
);
601 /* release the hold from dsl_dir_dirty */
602 dmu_buf_rele(dd
->dd_dbuf
, dd
);
606 dsl_dir_space_towrite(dsl_dir_t
*dd
)
611 ASSERT(MUTEX_HELD(&dd
->dd_lock
));
613 for (i
= 0; i
< TXG_SIZE
; i
++) {
614 space
+= dd
->dd_space_towrite
[i
&TXG_MASK
];
615 ASSERT3U(dd
->dd_space_towrite
[i
&TXG_MASK
], >=, 0);
621 * How much space would dd have available if ancestor had delta applied
622 * to it? If ondiskonly is set, we're only interested in what's
623 * on-disk, not estimated pending changes.
626 dsl_dir_space_available(dsl_dir_t
*dd
,
627 dsl_dir_t
*ancestor
, int64_t delta
, int ondiskonly
)
629 uint64_t parentspace
, myspace
, quota
, used
;
632 * If there are no restrictions otherwise, assume we have
633 * unlimited space available.
636 parentspace
= UINT64_MAX
;
638 if (dd
->dd_parent
!= NULL
) {
639 parentspace
= dsl_dir_space_available(dd
->dd_parent
,
640 ancestor
, delta
, ondiskonly
);
643 mutex_enter(&dd
->dd_lock
);
644 if (dd
->dd_phys
->dd_quota
!= 0)
645 quota
= dd
->dd_phys
->dd_quota
;
646 used
= dd
->dd_phys
->dd_used_bytes
;
648 used
+= dsl_dir_space_towrite(dd
);
650 if (dd
->dd_parent
== NULL
) {
651 uint64_t poolsize
= dsl_pool_adjustedsize(dd
->dd_pool
, FALSE
);
652 quota
= MIN(quota
, poolsize
);
655 if (dd
->dd_phys
->dd_reserved
> used
&& parentspace
!= UINT64_MAX
) {
657 * We have some space reserved, in addition to what our
660 parentspace
+= dd
->dd_phys
->dd_reserved
- used
;
663 if (dd
== ancestor
) {
665 ASSERT(used
>= -delta
);
667 if (parentspace
!= UINT64_MAX
)
668 parentspace
-= delta
;
676 * the lesser of the space provided by our parent and
677 * the space left in our quota
679 myspace
= MIN(parentspace
, quota
- used
);
682 mutex_exit(&dd
->dd_lock
);
695 dsl_dir_tempreserve_impl(dsl_dir_t
*dd
, uint64_t asize
, boolean_t netfree
,
696 boolean_t ignorequota
, boolean_t checkrefquota
, list_t
*tr_list
,
697 dmu_tx_t
*tx
, boolean_t first
)
699 uint64_t txg
= tx
->tx_txg
;
700 uint64_t est_inflight
, used_on_disk
, quota
, parent_rsrv
;
701 uint64_t deferred
= 0;
702 struct tempreserve
*tr
;
704 int txgidx
= txg
& TXG_MASK
;
706 uint64_t ref_rsrv
= 0;
708 ASSERT3U(txg
, !=, 0);
709 ASSERT3S(asize
, >, 0);
711 mutex_enter(&dd
->dd_lock
);
714 * Check against the dsl_dir's quota. We don't add in the delta
715 * when checking for over-quota because they get one free hit.
717 est_inflight
= dsl_dir_space_towrite(dd
);
718 for (i
= 0; i
< TXG_SIZE
; i
++)
719 est_inflight
+= dd
->dd_tempreserved
[i
];
720 used_on_disk
= dd
->dd_phys
->dd_used_bytes
;
723 * On the first iteration, fetch the dataset's used-on-disk and
724 * refreservation values. Also, if checkrefquota is set, test if
725 * allocating this space would exceed the dataset's refquota.
727 if (first
&& tx
->tx_objset
) {
729 dsl_dataset_t
*ds
= tx
->tx_objset
->os_dsl_dataset
;
731 error
= dsl_dataset_check_quota(ds
, checkrefquota
,
732 asize
, est_inflight
, &used_on_disk
, &ref_rsrv
);
734 mutex_exit(&dd
->dd_lock
);
740 * If this transaction will result in a net free of space,
741 * we want to let it through.
743 if (ignorequota
|| netfree
|| dd
->dd_phys
->dd_quota
== 0)
746 quota
= dd
->dd_phys
->dd_quota
;
749 * Adjust the quota against the actual pool size at the root
750 * minus any outstanding deferred frees.
751 * To ensure that it's possible to remove files from a full
752 * pool without inducing transient overcommits, we throttle
753 * netfree transactions against a quota that is slightly larger,
754 * but still within the pool's allocation slop. In cases where
755 * we're very close to full, this will allow a steady trickle of
756 * removes to get through.
758 if (dd
->dd_parent
== NULL
) {
759 spa_t
*spa
= dd
->dd_pool
->dp_spa
;
760 uint64_t poolsize
= dsl_pool_adjustedsize(dd
->dd_pool
, netfree
);
761 deferred
= metaslab_class_get_deferred(spa_normal_class(spa
));
762 if (poolsize
- deferred
< quota
) {
763 quota
= poolsize
- deferred
;
769 * If they are requesting more space, and our current estimate
770 * is over quota, they get to try again unless the actual
771 * on-disk is over quota and there are no pending changes (which
772 * may free up space for us).
774 if (used_on_disk
+ est_inflight
>= quota
) {
775 if (est_inflight
> 0 || used_on_disk
< quota
||
776 (retval
== ENOSPC
&& used_on_disk
< quota
+ deferred
))
778 dprintf_dd(dd
, "failing: used=%lluK inflight = %lluK "
779 "quota=%lluK tr=%lluK err=%d\n",
780 used_on_disk
>>10, est_inflight
>>10,
781 quota
>>10, asize
>>10, retval
);
782 mutex_exit(&dd
->dd_lock
);
786 /* We need to up our estimated delta before dropping dd_lock */
787 dd
->dd_tempreserved
[txgidx
] += asize
;
789 parent_rsrv
= parent_delta(dd
, used_on_disk
+ est_inflight
,
791 mutex_exit(&dd
->dd_lock
);
793 tr
= kmem_zalloc(sizeof (struct tempreserve
), KM_PUSHPAGE
);
796 list_insert_tail(tr_list
, tr
);
798 /* see if it's OK with our parent */
799 if (dd
->dd_parent
&& parent_rsrv
) {
800 boolean_t ismos
= (dd
->dd_phys
->dd_head_dataset_obj
== 0);
802 return (dsl_dir_tempreserve_impl(dd
->dd_parent
,
803 parent_rsrv
, netfree
, ismos
, TRUE
, tr_list
, tx
, FALSE
));
810 * Reserve space in this dsl_dir, to be used in this tx's txg.
811 * After the space has been dirtied (and dsl_dir_willuse_space()
812 * has been called), the reservation should be canceled, using
813 * dsl_dir_tempreserve_clear().
816 dsl_dir_tempreserve_space(dsl_dir_t
*dd
, uint64_t lsize
, uint64_t asize
,
817 uint64_t fsize
, uint64_t usize
, void **tr_cookiep
, dmu_tx_t
*tx
)
827 tr_list
= kmem_alloc(sizeof (list_t
), KM_PUSHPAGE
);
828 list_create(tr_list
, sizeof (struct tempreserve
),
829 offsetof(struct tempreserve
, tr_node
));
830 ASSERT3S(asize
, >, 0);
831 ASSERT3S(fsize
, >=, 0);
833 err
= arc_tempreserve_space(lsize
, tx
->tx_txg
);
835 struct tempreserve
*tr
;
837 tr
= kmem_zalloc(sizeof (struct tempreserve
), KM_PUSHPAGE
);
839 list_insert_tail(tr_list
, tr
);
841 err
= dsl_pool_tempreserve_space(dd
->dd_pool
, asize
, tx
);
844 txg_delay(dd
->dd_pool
, tx
->tx_txg
, 1);
847 dsl_pool_memory_pressure(dd
->dd_pool
);
851 struct tempreserve
*tr
;
853 tr
= kmem_zalloc(sizeof (struct tempreserve
), KM_PUSHPAGE
);
854 tr
->tr_dp
= dd
->dd_pool
;
856 list_insert_tail(tr_list
, tr
);
858 err
= dsl_dir_tempreserve_impl(dd
, asize
, fsize
>= asize
,
859 FALSE
, asize
> usize
, tr_list
, tx
, TRUE
);
863 dsl_dir_tempreserve_clear(tr_list
, tx
);
865 *tr_cookiep
= tr_list
;
871 * Clear a temporary reservation that we previously made with
872 * dsl_dir_tempreserve_space().
875 dsl_dir_tempreserve_clear(void *tr_cookie
, dmu_tx_t
*tx
)
877 int txgidx
= tx
->tx_txg
& TXG_MASK
;
878 list_t
*tr_list
= tr_cookie
;
879 struct tempreserve
*tr
;
881 ASSERT3U(tx
->tx_txg
, !=, 0);
883 if (tr_cookie
== NULL
)
886 while ((tr
= list_head(tr_list
))) {
888 dsl_pool_tempreserve_clear(tr
->tr_dp
, tr
->tr_size
, tx
);
889 } else if (tr
->tr_ds
) {
890 mutex_enter(&tr
->tr_ds
->dd_lock
);
891 ASSERT3U(tr
->tr_ds
->dd_tempreserved
[txgidx
], >=,
893 tr
->tr_ds
->dd_tempreserved
[txgidx
] -= tr
->tr_size
;
894 mutex_exit(&tr
->tr_ds
->dd_lock
);
896 arc_tempreserve_clear(tr
->tr_size
);
898 list_remove(tr_list
, tr
);
899 kmem_free(tr
, sizeof (struct tempreserve
));
902 kmem_free(tr_list
, sizeof (list_t
));
906 dsl_dir_willuse_space_impl(dsl_dir_t
*dd
, int64_t space
, dmu_tx_t
*tx
)
908 int64_t parent_space
;
911 mutex_enter(&dd
->dd_lock
);
913 dd
->dd_space_towrite
[tx
->tx_txg
& TXG_MASK
] += space
;
915 est_used
= dsl_dir_space_towrite(dd
) + dd
->dd_phys
->dd_used_bytes
;
916 parent_space
= parent_delta(dd
, est_used
, space
);
917 mutex_exit(&dd
->dd_lock
);
919 /* Make sure that we clean up dd_space_to* */
920 dsl_dir_dirty(dd
, tx
);
922 /* XXX this is potentially expensive and unnecessary... */
923 if (parent_space
&& dd
->dd_parent
)
924 dsl_dir_willuse_space_impl(dd
->dd_parent
, parent_space
, tx
);
928 * Call in open context when we think we're going to write/free space,
929 * eg. when dirtying data. Be conservative (ie. OK to write less than
930 * this or free more than this, but don't write more or free less).
933 dsl_dir_willuse_space(dsl_dir_t
*dd
, int64_t space
, dmu_tx_t
*tx
)
935 dsl_pool_willuse_space(dd
->dd_pool
, space
, tx
);
936 dsl_dir_willuse_space_impl(dd
, space
, tx
);
939 /* call from syncing context when we actually write/free space for this dd */
941 dsl_dir_diduse_space(dsl_dir_t
*dd
, dd_used_t type
,
942 int64_t used
, int64_t compressed
, int64_t uncompressed
, dmu_tx_t
*tx
)
944 int64_t accounted_delta
;
945 boolean_t needlock
= !MUTEX_HELD(&dd
->dd_lock
);
947 ASSERT(dmu_tx_is_syncing(tx
));
948 ASSERT(type
< DD_USED_NUM
);
951 mutex_enter(&dd
->dd_lock
);
952 accounted_delta
= parent_delta(dd
, dd
->dd_phys
->dd_used_bytes
, used
);
953 ASSERT(used
>= 0 || dd
->dd_phys
->dd_used_bytes
>= -used
);
954 ASSERT(compressed
>= 0 ||
955 dd
->dd_phys
->dd_compressed_bytes
>= -compressed
);
956 ASSERT(uncompressed
>= 0 ||
957 dd
->dd_phys
->dd_uncompressed_bytes
>= -uncompressed
);
958 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
959 dd
->dd_phys
->dd_used_bytes
+= used
;
960 dd
->dd_phys
->dd_uncompressed_bytes
+= uncompressed
;
961 dd
->dd_phys
->dd_compressed_bytes
+= compressed
;
963 if (dd
->dd_phys
->dd_flags
& DD_FLAG_USED_BREAKDOWN
) {
965 dd
->dd_phys
->dd_used_breakdown
[type
] >= -used
);
966 dd
->dd_phys
->dd_used_breakdown
[type
] += used
;
971 for (t
= 0; t
< DD_USED_NUM
; t
++)
972 u
+= dd
->dd_phys
->dd_used_breakdown
[t
];
973 ASSERT3U(u
, ==, dd
->dd_phys
->dd_used_bytes
);
978 mutex_exit(&dd
->dd_lock
);
980 if (dd
->dd_parent
!= NULL
) {
981 dsl_dir_diduse_space(dd
->dd_parent
, DD_USED_CHILD
,
982 accounted_delta
, compressed
, uncompressed
, tx
);
983 dsl_dir_transfer_space(dd
->dd_parent
,
984 used
- accounted_delta
,
985 DD_USED_CHILD_RSRV
, DD_USED_CHILD
, tx
);
990 dsl_dir_transfer_space(dsl_dir_t
*dd
, int64_t delta
,
991 dd_used_t oldtype
, dd_used_t newtype
, dmu_tx_t
*tx
)
993 boolean_t needlock
= !MUTEX_HELD(&dd
->dd_lock
);
995 ASSERT(dmu_tx_is_syncing(tx
));
996 ASSERT(oldtype
< DD_USED_NUM
);
997 ASSERT(newtype
< DD_USED_NUM
);
999 if (delta
== 0 || !(dd
->dd_phys
->dd_flags
& DD_FLAG_USED_BREAKDOWN
))
1003 mutex_enter(&dd
->dd_lock
);
1005 dd
->dd_phys
->dd_used_breakdown
[oldtype
] >= delta
:
1006 dd
->dd_phys
->dd_used_breakdown
[newtype
] >= -delta
);
1007 ASSERT(dd
->dd_phys
->dd_used_bytes
>= ABS(delta
));
1008 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
1009 dd
->dd_phys
->dd_used_breakdown
[oldtype
] -= delta
;
1010 dd
->dd_phys
->dd_used_breakdown
[newtype
] += delta
;
1012 mutex_exit(&dd
->dd_lock
);
1016 dsl_dir_set_quota_check(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
1018 dsl_dataset_t
*ds
= arg1
;
1019 dsl_dir_t
*dd
= ds
->ds_dir
;
1020 dsl_prop_setarg_t
*psa
= arg2
;
1024 if ((err
= dsl_prop_predict_sync(ds
->ds_dir
, psa
)) != 0)
1027 if (psa
->psa_effective_value
== 0)
1030 mutex_enter(&dd
->dd_lock
);
1032 * If we are doing the preliminary check in open context, and
1033 * there are pending changes, then don't fail it, since the
1034 * pending changes could under-estimate the amount of space to be
1037 towrite
= dsl_dir_space_towrite(dd
);
1038 if ((dmu_tx_is_syncing(tx
) || towrite
== 0) &&
1039 (psa
->psa_effective_value
< dd
->dd_phys
->dd_reserved
||
1040 psa
->psa_effective_value
< dd
->dd_phys
->dd_used_bytes
+ towrite
)) {
1043 mutex_exit(&dd
->dd_lock
);
1047 extern dsl_syncfunc_t dsl_prop_set_sync
;
1050 dsl_dir_set_quota_sync(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
1052 dsl_dataset_t
*ds
= arg1
;
1053 dsl_dir_t
*dd
= ds
->ds_dir
;
1054 dsl_prop_setarg_t
*psa
= arg2
;
1055 uint64_t effective_value
= psa
->psa_effective_value
;
1057 dsl_prop_set_sync(ds
, psa
, tx
);
1058 DSL_PROP_CHECK_PREDICTION(dd
, psa
);
1060 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
1062 mutex_enter(&dd
->dd_lock
);
1063 dd
->dd_phys
->dd_quota
= effective_value
;
1064 mutex_exit(&dd
->dd_lock
);
1068 dsl_dir_set_quota(const char *ddname
, zprop_source_t source
, uint64_t quota
)
1072 dsl_prop_setarg_t psa
;
1075 dsl_prop_setarg_init_uint64(&psa
, "quota", source
, "a
);
1077 err
= dsl_dataset_hold(ddname
, FTAG
, &ds
);
1081 err
= dsl_dir_open(ddname
, FTAG
, &dd
, NULL
);
1083 dsl_dataset_rele(ds
, FTAG
);
1087 ASSERT(ds
->ds_dir
== dd
);
1090 * If someone removes a file, then tries to set the quota, we want to
1091 * make sure the file freeing takes effect.
1093 txg_wait_open(dd
->dd_pool
, 0);
1095 err
= dsl_sync_task_do(dd
->dd_pool
, dsl_dir_set_quota_check
,
1096 dsl_dir_set_quota_sync
, ds
, &psa
, 0);
1098 dsl_dir_close(dd
, FTAG
);
1099 dsl_dataset_rele(ds
, FTAG
);
1104 dsl_dir_set_reservation_check(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
1106 dsl_dataset_t
*ds
= arg1
;
1107 dsl_dir_t
*dd
= ds
->ds_dir
;
1108 dsl_prop_setarg_t
*psa
= arg2
;
1109 uint64_t effective_value
;
1110 uint64_t used
, avail
;
1113 if ((err
= dsl_prop_predict_sync(ds
->ds_dir
, psa
)) != 0)
1116 effective_value
= psa
->psa_effective_value
;
1119 * If we are doing the preliminary check in open context, the
1120 * space estimates may be inaccurate.
1122 if (!dmu_tx_is_syncing(tx
))
1125 mutex_enter(&dd
->dd_lock
);
1126 used
= dd
->dd_phys
->dd_used_bytes
;
1127 mutex_exit(&dd
->dd_lock
);
1129 if (dd
->dd_parent
) {
1130 avail
= dsl_dir_space_available(dd
->dd_parent
,
1133 avail
= dsl_pool_adjustedsize(dd
->dd_pool
, B_FALSE
) - used
;
1136 if (MAX(used
, effective_value
) > MAX(used
, dd
->dd_phys
->dd_reserved
)) {
1137 uint64_t delta
= MAX(used
, effective_value
) -
1138 MAX(used
, dd
->dd_phys
->dd_reserved
);
1142 if (dd
->dd_phys
->dd_quota
> 0 &&
1143 effective_value
> dd
->dd_phys
->dd_quota
)
1151 dsl_dir_set_reservation_sync(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
1153 dsl_dataset_t
*ds
= arg1
;
1154 dsl_dir_t
*dd
= ds
->ds_dir
;
1155 dsl_prop_setarg_t
*psa
= arg2
;
1156 uint64_t effective_value
= psa
->psa_effective_value
;
1160 dsl_prop_set_sync(ds
, psa
, tx
);
1161 DSL_PROP_CHECK_PREDICTION(dd
, psa
);
1163 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
1165 mutex_enter(&dd
->dd_lock
);
1166 used
= dd
->dd_phys
->dd_used_bytes
;
1167 delta
= MAX(used
, effective_value
) -
1168 MAX(used
, dd
->dd_phys
->dd_reserved
);
1169 dd
->dd_phys
->dd_reserved
= effective_value
;
1171 if (dd
->dd_parent
!= NULL
) {
1172 /* Roll up this additional usage into our ancestors */
1173 dsl_dir_diduse_space(dd
->dd_parent
, DD_USED_CHILD_RSRV
,
1176 mutex_exit(&dd
->dd_lock
);
1180 dsl_dir_set_reservation(const char *ddname
, zprop_source_t source
,
1181 uint64_t reservation
)
1185 dsl_prop_setarg_t psa
;
1188 dsl_prop_setarg_init_uint64(&psa
, "reservation", source
, &reservation
);
1190 err
= dsl_dataset_hold(ddname
, FTAG
, &ds
);
1194 err
= dsl_dir_open(ddname
, FTAG
, &dd
, NULL
);
1196 dsl_dataset_rele(ds
, FTAG
);
1200 ASSERT(ds
->ds_dir
== dd
);
1202 err
= dsl_sync_task_do(dd
->dd_pool
, dsl_dir_set_reservation_check
,
1203 dsl_dir_set_reservation_sync
, ds
, &psa
, 0);
1205 dsl_dir_close(dd
, FTAG
);
1206 dsl_dataset_rele(ds
, FTAG
);
1211 closest_common_ancestor(dsl_dir_t
*ds1
, dsl_dir_t
*ds2
)
1213 for (; ds1
; ds1
= ds1
->dd_parent
) {
1215 for (dd
= ds2
; dd
; dd
= dd
->dd_parent
) {
1224 * If delta is applied to dd, how much of that delta would be applied to
1225 * ancestor? Syncing context only.
1228 would_change(dsl_dir_t
*dd
, int64_t delta
, dsl_dir_t
*ancestor
)
1233 mutex_enter(&dd
->dd_lock
);
1234 delta
= parent_delta(dd
, dd
->dd_phys
->dd_used_bytes
, delta
);
1235 mutex_exit(&dd
->dd_lock
);
1236 return (would_change(dd
->dd_parent
, delta
, ancestor
));
1240 dsl_dir_t
*newparent
;
1241 const char *mynewname
;
1245 dsl_dir_rename_check(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
1247 dsl_dir_t
*dd
= arg1
;
1248 struct renamearg
*ra
= arg2
;
1249 dsl_pool_t
*dp
= dd
->dd_pool
;
1250 objset_t
*mos
= dp
->dp_meta_objset
;
1255 * There should only be one reference, from dmu_objset_rename().
1256 * Fleeting holds are also possible (eg, from "zfs list" getting
1257 * stats), but any that are present in open context will likely
1258 * be gone by syncing context, so only fail from syncing
1261 if (dmu_tx_is_syncing(tx
) && dmu_buf_refcount(dd
->dd_dbuf
) > 1)
1264 /* check for existing name */
1265 err
= zap_lookup(mos
, ra
->newparent
->dd_phys
->dd_child_dir_zapobj
,
1266 ra
->mynewname
, 8, 1, &val
);
1272 if (ra
->newparent
!= dd
->dd_parent
) {
1273 /* is there enough space? */
1275 MAX(dd
->dd_phys
->dd_used_bytes
, dd
->dd_phys
->dd_reserved
);
1277 /* no rename into our descendant */
1278 if (closest_common_ancestor(dd
, ra
->newparent
) == dd
)
1281 if ((err
= dsl_dir_transfer_possible(dd
->dd_parent
,
1282 ra
->newparent
, myspace
)))
1290 dsl_dir_rename_sync(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
1292 dsl_dir_t
*dd
= arg1
;
1293 struct renamearg
*ra
= arg2
;
1294 dsl_pool_t
*dp
= dd
->dd_pool
;
1295 objset_t
*mos
= dp
->dp_meta_objset
;
1298 ASSERT(dmu_buf_refcount(dd
->dd_dbuf
) <= 2);
1300 if (ra
->newparent
!= dd
->dd_parent
) {
1301 dsl_dir_diduse_space(dd
->dd_parent
, DD_USED_CHILD
,
1302 -dd
->dd_phys
->dd_used_bytes
,
1303 -dd
->dd_phys
->dd_compressed_bytes
,
1304 -dd
->dd_phys
->dd_uncompressed_bytes
, tx
);
1305 dsl_dir_diduse_space(ra
->newparent
, DD_USED_CHILD
,
1306 dd
->dd_phys
->dd_used_bytes
,
1307 dd
->dd_phys
->dd_compressed_bytes
,
1308 dd
->dd_phys
->dd_uncompressed_bytes
, tx
);
1310 if (dd
->dd_phys
->dd_reserved
> dd
->dd_phys
->dd_used_bytes
) {
1311 uint64_t unused_rsrv
= dd
->dd_phys
->dd_reserved
-
1312 dd
->dd_phys
->dd_used_bytes
;
1314 dsl_dir_diduse_space(dd
->dd_parent
, DD_USED_CHILD_RSRV
,
1315 -unused_rsrv
, 0, 0, tx
);
1316 dsl_dir_diduse_space(ra
->newparent
, DD_USED_CHILD_RSRV
,
1317 unused_rsrv
, 0, 0, tx
);
1321 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
1323 /* remove from old parent zapobj */
1324 err
= zap_remove(mos
, dd
->dd_parent
->dd_phys
->dd_child_dir_zapobj
,
1328 (void) strcpy(dd
->dd_myname
, ra
->mynewname
);
1329 dsl_dir_close(dd
->dd_parent
, dd
);
1330 dd
->dd_phys
->dd_parent_obj
= ra
->newparent
->dd_object
;
1331 VERIFY(0 == dsl_dir_open_obj(dd
->dd_pool
,
1332 ra
->newparent
->dd_object
, NULL
, dd
, &dd
->dd_parent
));
1334 /* add to new parent zapobj */
1335 err
= zap_add(mos
, ra
->newparent
->dd_phys
->dd_child_dir_zapobj
,
1336 dd
->dd_myname
, 8, 1, &dd
->dd_object
, tx
);
1339 spa_history_log_internal(LOG_DS_RENAME
, dd
->dd_pool
->dp_spa
,
1340 tx
, "dataset = %llu", dd
->dd_phys
->dd_head_dataset_obj
);
1344 dsl_dir_rename(dsl_dir_t
*dd
, const char *newname
)
1346 struct renamearg ra
;
1349 /* new parent should exist */
1350 err
= dsl_dir_open(newname
, FTAG
, &ra
.newparent
, &ra
.mynewname
);
1354 /* can't rename to different pool */
1355 if (dd
->dd_pool
!= ra
.newparent
->dd_pool
) {
1360 /* new name should not already exist */
1361 if (ra
.mynewname
== NULL
) {
1366 err
= dsl_sync_task_do(dd
->dd_pool
,
1367 dsl_dir_rename_check
, dsl_dir_rename_sync
, dd
, &ra
, 3);
1370 dsl_dir_close(ra
.newparent
, FTAG
);
1375 dsl_dir_transfer_possible(dsl_dir_t
*sdd
, dsl_dir_t
*tdd
, uint64_t space
)
1377 dsl_dir_t
*ancestor
;
1381 ancestor
= closest_common_ancestor(sdd
, tdd
);
1382 adelta
= would_change(sdd
, -space
, ancestor
);
1383 avail
= dsl_dir_space_available(tdd
, ancestor
, adelta
, FALSE
);
1391 dsl_dir_snap_cmtime(dsl_dir_t
*dd
)
1395 mutex_enter(&dd
->dd_lock
);
1396 t
= dd
->dd_snap_cmtime
;
1397 mutex_exit(&dd
->dd_lock
);
1403 dsl_dir_snap_cmtime_update(dsl_dir_t
*dd
)
1408 mutex_enter(&dd
->dd_lock
);
1409 dd
->dd_snap_cmtime
= t
;
1410 mutex_exit(&dd
->dd_lock
);
1413 #if defined(_KERNEL) && defined(HAVE_SPL)
1414 EXPORT_SYMBOL(dsl_dir_set_quota
);
1415 EXPORT_SYMBOL(dsl_dir_set_reservation
);
1416 EXPORT_SYMBOL(dsl_dir_open
);
1417 EXPORT_SYMBOL(dsl_dir_close
);