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) 2012 by Delphix. All rights reserved.
26 #include <sys/zfs_context.h>
28 #include <sys/dnode.h>
30 #include <sys/dmu_impl.h>
31 #include <sys/dmu_tx.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dsl_dir.h>
34 #include <sys/dsl_dataset.h>
37 #include <sys/dmu_zfetch.h>
39 static int free_range_compar(const void *node1, const void *node2);
41 static kmem_cache_t *dnode_cache;
43 * Define DNODE_STATS to turn on statistic gathering. By default, it is only
44 * turned on when DEBUG is also defined.
51 #define DNODE_STAT_ADD(stat) ((stat)++)
53 #define DNODE_STAT_ADD(stat) /* nothing */
54 #endif /* DNODE_STATS */
56 ASSERTV(static dnode_phys_t dnode_phys_zero);
58 int zfs_default_bs = SPA_MINBLOCKSHIFT;
59 int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
62 static kmem_cbrc_t dnode_move(void *, void *, size_t, void *);
67 dnode_cons(void *arg, void *unused, int kmflag)
72 rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL);
73 mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
74 mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
75 cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL);
77 refcount_create(&dn->dn_holds);
78 refcount_create(&dn->dn_tx_holds);
79 list_link_init(&dn->dn_link);
81 bzero(&dn->dn_next_nblkptr[0], sizeof (dn->dn_next_nblkptr));
82 bzero(&dn->dn_next_nlevels[0], sizeof (dn->dn_next_nlevels));
83 bzero(&dn->dn_next_indblkshift[0], sizeof (dn->dn_next_indblkshift));
84 bzero(&dn->dn_next_bonustype[0], sizeof (dn->dn_next_bonustype));
85 bzero(&dn->dn_rm_spillblk[0], sizeof (dn->dn_rm_spillblk));
86 bzero(&dn->dn_next_bonuslen[0], sizeof (dn->dn_next_bonuslen));
87 bzero(&dn->dn_next_blksz[0], sizeof (dn->dn_next_blksz));
89 for (i = 0; i < TXG_SIZE; i++) {
90 list_link_init(&dn->dn_dirty_link[i]);
91 avl_create(&dn->dn_ranges[i], free_range_compar,
92 sizeof (free_range_t),
93 offsetof(struct free_range, fr_node));
94 list_create(&dn->dn_dirty_records[i],
95 sizeof (dbuf_dirty_record_t),
96 offsetof(dbuf_dirty_record_t, dr_dirty_node));
99 dn->dn_allocated_txg = 0;
101 dn->dn_assigned_txg = 0;
103 dn->dn_dirtyctx_firstset = NULL;
105 dn->dn_have_spill = B_FALSE;
115 dn->dn_dbufs_count = 0;
116 list_create(&dn->dn_dbufs, sizeof (dmu_buf_impl_t),
117 offsetof(dmu_buf_impl_t, db_link));
125 dnode_dest(void *arg, void *unused)
130 rw_destroy(&dn->dn_struct_rwlock);
131 mutex_destroy(&dn->dn_mtx);
132 mutex_destroy(&dn->dn_dbufs_mtx);
133 cv_destroy(&dn->dn_notxholds);
134 refcount_destroy(&dn->dn_holds);
135 refcount_destroy(&dn->dn_tx_holds);
136 ASSERT(!list_link_active(&dn->dn_link));
138 for (i = 0; i < TXG_SIZE; i++) {
139 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
140 avl_destroy(&dn->dn_ranges[i]);
141 list_destroy(&dn->dn_dirty_records[i]);
142 ASSERT0(dn->dn_next_nblkptr[i]);
143 ASSERT0(dn->dn_next_nlevels[i]);
144 ASSERT0(dn->dn_next_indblkshift[i]);
145 ASSERT0(dn->dn_next_bonustype[i]);
146 ASSERT0(dn->dn_rm_spillblk[i]);
147 ASSERT0(dn->dn_next_bonuslen[i]);
148 ASSERT0(dn->dn_next_blksz[i]);
151 ASSERT0(dn->dn_allocated_txg);
152 ASSERT0(dn->dn_free_txg);
153 ASSERT0(dn->dn_assigned_txg);
154 ASSERT0(dn->dn_dirtyctx);
155 ASSERT3P(dn->dn_dirtyctx_firstset, ==, NULL);
156 ASSERT3P(dn->dn_bonus, ==, NULL);
157 ASSERT(!dn->dn_have_spill);
158 ASSERT3P(dn->dn_zio, ==, NULL);
159 ASSERT0(dn->dn_oldused);
160 ASSERT0(dn->dn_oldflags);
161 ASSERT0(dn->dn_olduid);
162 ASSERT0(dn->dn_oldgid);
163 ASSERT0(dn->dn_newuid);
164 ASSERT0(dn->dn_newgid);
165 ASSERT0(dn->dn_id_flags);
167 ASSERT0(dn->dn_dbufs_count);
168 list_destroy(&dn->dn_dbufs);
174 ASSERT(dnode_cache == NULL);
175 dnode_cache = kmem_cache_create("dnode_t", sizeof (dnode_t),
176 0, dnode_cons, dnode_dest, NULL, NULL, NULL, KMC_KMEM);
177 kmem_cache_set_move(dnode_cache, dnode_move);
183 kmem_cache_destroy(dnode_cache);
190 dnode_verify(dnode_t *dn)
192 int drop_struct_lock = FALSE;
195 ASSERT(dn->dn_objset);
196 ASSERT(dn->dn_handle->dnh_dnode == dn);
198 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
200 if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
203 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
204 rw_enter(&dn->dn_struct_rwlock, RW_READER);
205 drop_struct_lock = TRUE;
207 if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
209 ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
210 if (dn->dn_datablkshift) {
211 ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
212 ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
213 ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
215 ASSERT3U(dn->dn_nlevels, <=, 30);
216 ASSERT(DMU_OT_IS_VALID(dn->dn_type));
217 ASSERT3U(dn->dn_nblkptr, >=, 1);
218 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
219 ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
220 ASSERT3U(dn->dn_datablksz, ==,
221 dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
222 ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
223 ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
224 dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
225 for (i = 0; i < TXG_SIZE; i++) {
226 ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
229 if (dn->dn_phys->dn_type != DMU_OT_NONE)
230 ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
231 ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_object) || dn->dn_dbuf != NULL);
232 if (dn->dn_dbuf != NULL) {
233 ASSERT3P(dn->dn_phys, ==,
234 (dnode_phys_t *)dn->dn_dbuf->db.db_data +
235 (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
237 if (drop_struct_lock)
238 rw_exit(&dn->dn_struct_rwlock);
243 dnode_byteswap(dnode_phys_t *dnp)
245 uint64_t *buf64 = (void*)&dnp->dn_blkptr;
248 if (dnp->dn_type == DMU_OT_NONE) {
249 bzero(dnp, sizeof (dnode_phys_t));
253 dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
254 dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
255 dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
256 dnp->dn_used = BSWAP_64(dnp->dn_used);
259 * dn_nblkptr is only one byte, so it's OK to read it in either
260 * byte order. We can't read dn_bouslen.
262 ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
263 ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
264 for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
265 buf64[i] = BSWAP_64(buf64[i]);
268 * OK to check dn_bonuslen for zero, because it won't matter if
269 * we have the wrong byte order. This is necessary because the
270 * dnode dnode is smaller than a regular dnode.
272 if (dnp->dn_bonuslen != 0) {
274 * Note that the bonus length calculated here may be
275 * longer than the actual bonus buffer. This is because
276 * we always put the bonus buffer after the last block
277 * pointer (instead of packing it against the end of the
280 int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
281 size_t len = DN_MAX_BONUSLEN - off;
282 dmu_object_byteswap_t byteswap;
283 ASSERT(DMU_OT_IS_VALID(dnp->dn_bonustype));
284 byteswap = DMU_OT_BYTESWAP(dnp->dn_bonustype);
285 dmu_ot_byteswap[byteswap].ob_func(dnp->dn_bonus + off, len);
288 /* Swap SPILL block if we have one */
289 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
290 byteswap_uint64_array(&dnp->dn_spill, sizeof (blkptr_t));
295 dnode_buf_byteswap(void *vbuf, size_t size)
297 dnode_phys_t *buf = vbuf;
300 ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
301 ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
303 size >>= DNODE_SHIFT;
304 for (i = 0; i < size; i++) {
311 free_range_compar(const void *node1, const void *node2)
313 const free_range_t *rp1 = node1;
314 const free_range_t *rp2 = node2;
316 if (rp1->fr_blkid < rp2->fr_blkid)
318 else if (rp1->fr_blkid > rp2->fr_blkid)
324 dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx)
326 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
328 dnode_setdirty(dn, tx);
329 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
330 ASSERT3U(newsize, <=, DN_MAX_BONUSLEN -
331 (dn->dn_nblkptr-1) * sizeof (blkptr_t));
332 dn->dn_bonuslen = newsize;
334 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN;
336 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
337 rw_exit(&dn->dn_struct_rwlock);
341 dnode_setbonus_type(dnode_t *dn, dmu_object_type_t newtype, dmu_tx_t *tx)
343 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
344 dnode_setdirty(dn, tx);
345 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
346 dn->dn_bonustype = newtype;
347 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
348 rw_exit(&dn->dn_struct_rwlock);
352 dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx)
354 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
355 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
356 dnode_setdirty(dn, tx);
357 dn->dn_rm_spillblk[tx->tx_txg&TXG_MASK] = DN_KILL_SPILLBLK;
358 dn->dn_have_spill = B_FALSE;
362 dnode_setdblksz(dnode_t *dn, int size)
364 ASSERT0(P2PHASE(size, SPA_MINBLOCKSIZE));
365 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
366 ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
367 ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
368 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
369 dn->dn_datablksz = size;
370 dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
371 dn->dn_datablkshift = ISP2(size) ? highbit(size - 1) : 0;
375 dnode_create(objset_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
376 uint64_t object, dnode_handle_t *dnh)
378 dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_PUSHPAGE);
380 ASSERT(!POINTER_IS_VALID(dn->dn_objset));
384 * Defer setting dn_objset until the dnode is ready to be a candidate
385 * for the dnode_move() callback.
387 dn->dn_object = object;
392 if (dnp->dn_datablkszsec) {
393 dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
395 dn->dn_datablksz = 0;
396 dn->dn_datablkszsec = 0;
397 dn->dn_datablkshift = 0;
399 dn->dn_indblkshift = dnp->dn_indblkshift;
400 dn->dn_nlevels = dnp->dn_nlevels;
401 dn->dn_type = dnp->dn_type;
402 dn->dn_nblkptr = dnp->dn_nblkptr;
403 dn->dn_checksum = dnp->dn_checksum;
404 dn->dn_compress = dnp->dn_compress;
405 dn->dn_bonustype = dnp->dn_bonustype;
406 dn->dn_bonuslen = dnp->dn_bonuslen;
407 dn->dn_maxblkid = dnp->dn_maxblkid;
408 dn->dn_have_spill = ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0);
411 dmu_zfetch_init(&dn->dn_zfetch, dn);
413 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
415 mutex_enter(&os->os_lock);
416 list_insert_head(&os->os_dnodes, dn);
419 * Everything else must be valid before assigning dn_objset makes the
420 * dnode eligible for dnode_move().
423 mutex_exit(&os->os_lock);
425 arc_space_consume(sizeof (dnode_t), ARC_SPACE_OTHER);
430 * Caller must be holding the dnode handle, which is released upon return.
433 dnode_destroy(dnode_t *dn)
435 objset_t *os = dn->dn_objset;
437 ASSERT((dn->dn_id_flags & DN_ID_NEW_EXIST) == 0);
439 mutex_enter(&os->os_lock);
440 POINTER_INVALIDATE(&dn->dn_objset);
441 list_remove(&os->os_dnodes, dn);
442 mutex_exit(&os->os_lock);
444 /* the dnode can no longer move, so we can release the handle */
445 zrl_remove(&dn->dn_handle->dnh_zrlock);
447 dn->dn_allocated_txg = 0;
449 dn->dn_assigned_txg = 0;
452 if (dn->dn_dirtyctx_firstset != NULL) {
453 kmem_free(dn->dn_dirtyctx_firstset, 1);
454 dn->dn_dirtyctx_firstset = NULL;
456 if (dn->dn_bonus != NULL) {
457 mutex_enter(&dn->dn_bonus->db_mtx);
458 dbuf_evict(dn->dn_bonus);
463 dn->dn_have_spill = B_FALSE;
472 dmu_zfetch_rele(&dn->dn_zfetch);
473 kmem_cache_free(dnode_cache, dn);
474 arc_space_return(sizeof (dnode_t), ARC_SPACE_OTHER);
478 dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
479 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
484 blocksize = 1 << zfs_default_bs;
485 else if (blocksize > SPA_MAXBLOCKSIZE)
486 blocksize = SPA_MAXBLOCKSIZE;
488 blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
491 ibs = zfs_default_ibs;
493 ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
495 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
496 dn->dn_object, tx->tx_txg, blocksize, ibs);
498 ASSERT(dn->dn_type == DMU_OT_NONE);
499 ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
500 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
501 ASSERT(ot != DMU_OT_NONE);
502 ASSERT(DMU_OT_IS_VALID(ot));
503 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
504 (bonustype == DMU_OT_SA && bonuslen == 0) ||
505 (bonustype != DMU_OT_NONE && bonuslen != 0));
506 ASSERT(DMU_OT_IS_VALID(bonustype));
507 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
508 ASSERT(dn->dn_type == DMU_OT_NONE);
509 ASSERT0(dn->dn_maxblkid);
510 ASSERT0(dn->dn_allocated_txg);
511 ASSERT0(dn->dn_assigned_txg);
512 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
513 ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
514 ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
516 for (i = 0; i < TXG_SIZE; i++) {
517 ASSERT0(dn->dn_next_nblkptr[i]);
518 ASSERT0(dn->dn_next_nlevels[i]);
519 ASSERT0(dn->dn_next_indblkshift[i]);
520 ASSERT0(dn->dn_next_bonuslen[i]);
521 ASSERT0(dn->dn_next_bonustype[i]);
522 ASSERT0(dn->dn_rm_spillblk[i]);
523 ASSERT0(dn->dn_next_blksz[i]);
524 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
525 ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
526 ASSERT0(avl_numnodes(&dn->dn_ranges[i]));
530 dnode_setdblksz(dn, blocksize);
531 dn->dn_indblkshift = ibs;
533 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
537 ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
538 dn->dn_bonustype = bonustype;
539 dn->dn_bonuslen = bonuslen;
540 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
541 dn->dn_compress = ZIO_COMPRESS_INHERIT;
545 if (dn->dn_dirtyctx_firstset) {
546 kmem_free(dn->dn_dirtyctx_firstset, 1);
547 dn->dn_dirtyctx_firstset = NULL;
550 dn->dn_allocated_txg = tx->tx_txg;
553 dnode_setdirty(dn, tx);
554 dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
555 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
556 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
557 dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
561 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
562 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
566 ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
567 ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE);
568 ASSERT0(blocksize % SPA_MINBLOCKSIZE);
569 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
570 ASSERT(tx->tx_txg != 0);
571 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
572 (bonustype != DMU_OT_NONE && bonuslen != 0) ||
573 (bonustype == DMU_OT_SA && bonuslen == 0));
574 ASSERT(DMU_OT_IS_VALID(bonustype));
575 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
577 /* clean up any unreferenced dbufs */
578 dnode_evict_dbufs(dn);
582 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
583 dnode_setdirty(dn, tx);
584 if (dn->dn_datablksz != blocksize) {
585 /* change blocksize */
586 ASSERT(dn->dn_maxblkid == 0 &&
587 (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
588 dnode_block_freed(dn, 0)));
589 dnode_setdblksz(dn, blocksize);
590 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
592 if (dn->dn_bonuslen != bonuslen)
593 dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen;
595 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
598 nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
599 if (dn->dn_bonustype != bonustype)
600 dn->dn_next_bonustype[tx->tx_txg&TXG_MASK] = bonustype;
601 if (dn->dn_nblkptr != nblkptr)
602 dn->dn_next_nblkptr[tx->tx_txg&TXG_MASK] = nblkptr;
603 if (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
604 dbuf_rm_spill(dn, tx);
605 dnode_rm_spill(dn, tx);
607 rw_exit(&dn->dn_struct_rwlock);
612 /* change bonus size and type */
613 mutex_enter(&dn->dn_mtx);
614 dn->dn_bonustype = bonustype;
615 dn->dn_bonuslen = bonuslen;
616 dn->dn_nblkptr = nblkptr;
617 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
618 dn->dn_compress = ZIO_COMPRESS_INHERIT;
619 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
621 /* fix up the bonus db_size */
623 dn->dn_bonus->db.db_size =
624 DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t);
625 ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
628 dn->dn_allocated_txg = tx->tx_txg;
629 mutex_exit(&dn->dn_mtx);
635 uint64_t dms_dnode_invalid;
636 uint64_t dms_dnode_recheck1;
637 uint64_t dms_dnode_recheck2;
638 uint64_t dms_dnode_special;
639 uint64_t dms_dnode_handle;
640 uint64_t dms_dnode_rwlock;
641 uint64_t dms_dnode_active;
643 #endif /* DNODE_STATS */
646 dnode_move_impl(dnode_t *odn, dnode_t *ndn)
650 ASSERT(!RW_LOCK_HELD(&odn->dn_struct_rwlock));
651 ASSERT(MUTEX_NOT_HELD(&odn->dn_mtx));
652 ASSERT(MUTEX_NOT_HELD(&odn->dn_dbufs_mtx));
653 ASSERT(!RW_LOCK_HELD(&odn->dn_zfetch.zf_rwlock));
656 ndn->dn_objset = odn->dn_objset;
657 ndn->dn_object = odn->dn_object;
658 ndn->dn_dbuf = odn->dn_dbuf;
659 ndn->dn_handle = odn->dn_handle;
660 ndn->dn_phys = odn->dn_phys;
661 ndn->dn_type = odn->dn_type;
662 ndn->dn_bonuslen = odn->dn_bonuslen;
663 ndn->dn_bonustype = odn->dn_bonustype;
664 ndn->dn_nblkptr = odn->dn_nblkptr;
665 ndn->dn_checksum = odn->dn_checksum;
666 ndn->dn_compress = odn->dn_compress;
667 ndn->dn_nlevels = odn->dn_nlevels;
668 ndn->dn_indblkshift = odn->dn_indblkshift;
669 ndn->dn_datablkshift = odn->dn_datablkshift;
670 ndn->dn_datablkszsec = odn->dn_datablkszsec;
671 ndn->dn_datablksz = odn->dn_datablksz;
672 ndn->dn_maxblkid = odn->dn_maxblkid;
673 bcopy(&odn->dn_next_nblkptr[0], &ndn->dn_next_nblkptr[0],
674 sizeof (odn->dn_next_nblkptr));
675 bcopy(&odn->dn_next_nlevels[0], &ndn->dn_next_nlevels[0],
676 sizeof (odn->dn_next_nlevels));
677 bcopy(&odn->dn_next_indblkshift[0], &ndn->dn_next_indblkshift[0],
678 sizeof (odn->dn_next_indblkshift));
679 bcopy(&odn->dn_next_bonustype[0], &ndn->dn_next_bonustype[0],
680 sizeof (odn->dn_next_bonustype));
681 bcopy(&odn->dn_rm_spillblk[0], &ndn->dn_rm_spillblk[0],
682 sizeof (odn->dn_rm_spillblk));
683 bcopy(&odn->dn_next_bonuslen[0], &ndn->dn_next_bonuslen[0],
684 sizeof (odn->dn_next_bonuslen));
685 bcopy(&odn->dn_next_blksz[0], &ndn->dn_next_blksz[0],
686 sizeof (odn->dn_next_blksz));
687 for (i = 0; i < TXG_SIZE; i++) {
688 list_move_tail(&ndn->dn_dirty_records[i],
689 &odn->dn_dirty_records[i]);
691 bcopy(&odn->dn_ranges[0], &ndn->dn_ranges[0], sizeof (odn->dn_ranges));
692 ndn->dn_allocated_txg = odn->dn_allocated_txg;
693 ndn->dn_free_txg = odn->dn_free_txg;
694 ndn->dn_assigned_txg = odn->dn_assigned_txg;
695 ndn->dn_dirtyctx = odn->dn_dirtyctx;
696 ndn->dn_dirtyctx_firstset = odn->dn_dirtyctx_firstset;
697 ASSERT(refcount_count(&odn->dn_tx_holds) == 0);
698 refcount_transfer(&ndn->dn_holds, &odn->dn_holds);
699 ASSERT(list_is_empty(&ndn->dn_dbufs));
700 list_move_tail(&ndn->dn_dbufs, &odn->dn_dbufs);
701 ndn->dn_dbufs_count = odn->dn_dbufs_count;
702 ndn->dn_bonus = odn->dn_bonus;
703 ndn->dn_have_spill = odn->dn_have_spill;
704 ndn->dn_zio = odn->dn_zio;
705 ndn->dn_oldused = odn->dn_oldused;
706 ndn->dn_oldflags = odn->dn_oldflags;
707 ndn->dn_olduid = odn->dn_olduid;
708 ndn->dn_oldgid = odn->dn_oldgid;
709 ndn->dn_newuid = odn->dn_newuid;
710 ndn->dn_newgid = odn->dn_newgid;
711 ndn->dn_id_flags = odn->dn_id_flags;
712 dmu_zfetch_init(&ndn->dn_zfetch, NULL);
713 list_move_tail(&ndn->dn_zfetch.zf_stream, &odn->dn_zfetch.zf_stream);
714 ndn->dn_zfetch.zf_dnode = odn->dn_zfetch.zf_dnode;
715 ndn->dn_zfetch.zf_stream_cnt = odn->dn_zfetch.zf_stream_cnt;
716 ndn->dn_zfetch.zf_alloc_fail = odn->dn_zfetch.zf_alloc_fail;
719 * Update back pointers. Updating the handle fixes the back pointer of
720 * every descendant dbuf as well as the bonus dbuf.
722 ASSERT(ndn->dn_handle->dnh_dnode == odn);
723 ndn->dn_handle->dnh_dnode = ndn;
724 if (ndn->dn_zfetch.zf_dnode == odn) {
725 ndn->dn_zfetch.zf_dnode = ndn;
729 * Invalidate the original dnode by clearing all of its back pointers.
732 odn->dn_handle = NULL;
733 list_create(&odn->dn_dbufs, sizeof (dmu_buf_impl_t),
734 offsetof(dmu_buf_impl_t, db_link));
735 odn->dn_dbufs_count = 0;
736 odn->dn_bonus = NULL;
737 odn->dn_zfetch.zf_dnode = NULL;
740 * Set the low bit of the objset pointer to ensure that dnode_move()
741 * recognizes the dnode as invalid in any subsequent callback.
743 POINTER_INVALIDATE(&odn->dn_objset);
746 * Satisfy the destructor.
748 for (i = 0; i < TXG_SIZE; i++) {
749 list_create(&odn->dn_dirty_records[i],
750 sizeof (dbuf_dirty_record_t),
751 offsetof(dbuf_dirty_record_t, dr_dirty_node));
752 odn->dn_ranges[i].avl_root = NULL;
753 odn->dn_ranges[i].avl_numnodes = 0;
754 odn->dn_next_nlevels[i] = 0;
755 odn->dn_next_indblkshift[i] = 0;
756 odn->dn_next_bonustype[i] = 0;
757 odn->dn_rm_spillblk[i] = 0;
758 odn->dn_next_bonuslen[i] = 0;
759 odn->dn_next_blksz[i] = 0;
761 odn->dn_allocated_txg = 0;
762 odn->dn_free_txg = 0;
763 odn->dn_assigned_txg = 0;
764 odn->dn_dirtyctx = 0;
765 odn->dn_dirtyctx_firstset = NULL;
766 odn->dn_have_spill = B_FALSE;
769 odn->dn_oldflags = 0;
774 odn->dn_id_flags = 0;
780 odn->dn_moved = (uint8_t)-1;
785 dnode_move(void *buf, void *newbuf, size_t size, void *arg)
787 dnode_t *odn = buf, *ndn = newbuf;
793 * The dnode is on the objset's list of known dnodes if the objset
794 * pointer is valid. We set the low bit of the objset pointer when
795 * freeing the dnode to invalidate it, and the memory patterns written
796 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
797 * A newly created dnode sets the objset pointer last of all to indicate
798 * that the dnode is known and in a valid state to be moved by this
802 if (!POINTER_IS_VALID(os)) {
803 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_invalid);
804 return (KMEM_CBRC_DONT_KNOW);
808 * Ensure that the objset does not go away during the move.
810 rw_enter(&os_lock, RW_WRITER);
811 if (os != odn->dn_objset) {
813 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck1);
814 return (KMEM_CBRC_DONT_KNOW);
818 * If the dnode is still valid, then so is the objset. We know that no
819 * valid objset can be freed while we hold os_lock, so we can safely
820 * ensure that the objset remains in use.
822 mutex_enter(&os->os_lock);
825 * Recheck the objset pointer in case the dnode was removed just before
826 * acquiring the lock.
828 if (os != odn->dn_objset) {
829 mutex_exit(&os->os_lock);
831 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck2);
832 return (KMEM_CBRC_DONT_KNOW);
836 * At this point we know that as long as we hold os->os_lock, the dnode
837 * cannot be freed and fields within the dnode can be safely accessed.
838 * The objset listing this dnode cannot go away as long as this dnode is
842 if (DMU_OBJECT_IS_SPECIAL(odn->dn_object)) {
843 mutex_exit(&os->os_lock);
844 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_special);
845 return (KMEM_CBRC_NO);
847 ASSERT(odn->dn_dbuf != NULL); /* only "special" dnodes have no parent */
850 * Lock the dnode handle to prevent the dnode from obtaining any new
851 * holds. This also prevents the descendant dbufs and the bonus dbuf
852 * from accessing the dnode, so that we can discount their holds. The
853 * handle is safe to access because we know that while the dnode cannot
854 * go away, neither can its handle. Once we hold dnh_zrlock, we can
855 * safely move any dnode referenced only by dbufs.
857 if (!zrl_tryenter(&odn->dn_handle->dnh_zrlock)) {
858 mutex_exit(&os->os_lock);
859 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_handle);
860 return (KMEM_CBRC_LATER);
864 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
865 * We need to guarantee that there is a hold for every dbuf in order to
866 * determine whether the dnode is actively referenced. Falsely matching
867 * a dbuf to an active hold would lead to an unsafe move. It's possible
868 * that a thread already having an active dnode hold is about to add a
869 * dbuf, and we can't compare hold and dbuf counts while the add is in
872 if (!rw_tryenter(&odn->dn_struct_rwlock, RW_WRITER)) {
873 zrl_exit(&odn->dn_handle->dnh_zrlock);
874 mutex_exit(&os->os_lock);
875 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_rwlock);
876 return (KMEM_CBRC_LATER);
880 * A dbuf may be removed (evicted) without an active dnode hold. In that
881 * case, the dbuf count is decremented under the handle lock before the
882 * dbuf's hold is released. This order ensures that if we count the hold
883 * after the dbuf is removed but before its hold is released, we will
884 * treat the unmatched hold as active and exit safely. If we count the
885 * hold before the dbuf is removed, the hold is discounted, and the
886 * removal is blocked until the move completes.
888 refcount = refcount_count(&odn->dn_holds);
889 ASSERT(refcount >= 0);
890 dbufs = odn->dn_dbufs_count;
892 /* We can't have more dbufs than dnode holds. */
893 ASSERT3U(dbufs, <=, refcount);
894 DTRACE_PROBE3(dnode__move, dnode_t *, odn, int64_t, refcount,
897 if (refcount > dbufs) {
898 rw_exit(&odn->dn_struct_rwlock);
899 zrl_exit(&odn->dn_handle->dnh_zrlock);
900 mutex_exit(&os->os_lock);
901 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_active);
902 return (KMEM_CBRC_LATER);
905 rw_exit(&odn->dn_struct_rwlock);
908 * At this point we know that anyone with a hold on the dnode is not
909 * actively referencing it. The dnode is known and in a valid state to
910 * move. We're holding the locks needed to execute the critical section.
912 dnode_move_impl(odn, ndn);
914 list_link_replace(&odn->dn_link, &ndn->dn_link);
915 /* If the dnode was safe to move, the refcount cannot have changed. */
916 ASSERT(refcount == refcount_count(&ndn->dn_holds));
917 ASSERT(dbufs == ndn->dn_dbufs_count);
918 zrl_exit(&ndn->dn_handle->dnh_zrlock); /* handle has moved */
919 mutex_exit(&os->os_lock);
921 return (KMEM_CBRC_YES);
926 dnode_special_close(dnode_handle_t *dnh)
928 dnode_t *dn = dnh->dnh_dnode;
931 * Wait for final references to the dnode to clear. This can
932 * only happen if the arc is asyncronously evicting state that
933 * has a hold on this dnode while we are trying to evict this
936 while (refcount_count(&dn->dn_holds) > 0)
938 zrl_add(&dnh->dnh_zrlock);
939 dnode_destroy(dn); /* implicit zrl_remove() */
940 zrl_destroy(&dnh->dnh_zrlock);
941 dnh->dnh_dnode = NULL;
945 dnode_special_open(objset_t *os, dnode_phys_t *dnp, uint64_t object,
948 dnode_t *dn = dnode_create(os, dnp, NULL, object, dnh);
950 zrl_init(&dnh->dnh_zrlock);
956 dnode_buf_pageout(dmu_buf_t *db, void *arg)
958 dnode_children_t *children_dnodes = arg;
960 int epb = db->db_size >> DNODE_SHIFT;
962 ASSERT(epb == children_dnodes->dnc_count);
964 for (i = 0; i < epb; i++) {
965 dnode_handle_t *dnh = &children_dnodes->dnc_children[i];
969 * The dnode handle lock guards against the dnode moving to
970 * another valid address, so there is no need here to guard
971 * against changes to or from NULL.
973 if (dnh->dnh_dnode == NULL) {
974 zrl_destroy(&dnh->dnh_zrlock);
978 zrl_add(&dnh->dnh_zrlock);
981 * If there are holds on this dnode, then there should
982 * be holds on the dnode's containing dbuf as well; thus
983 * it wouldn't be eligible for eviction and this function
984 * would not have been called.
986 ASSERT(refcount_is_zero(&dn->dn_holds));
987 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
989 dnode_destroy(dn); /* implicit zrl_remove() */
990 zrl_destroy(&dnh->dnh_zrlock);
991 dnh->dnh_dnode = NULL;
993 kmem_free(children_dnodes, sizeof (dnode_children_t) +
994 (epb - 1) * sizeof (dnode_handle_t));
999 * EINVAL - invalid object number.
1001 * succeeds even for free dnodes.
1004 dnode_hold_impl(objset_t *os, uint64_t object, int flag,
1005 void *tag, dnode_t **dnp)
1008 int drop_struct_lock = FALSE;
1013 dnode_children_t *children_dnodes;
1014 dnode_handle_t *dnh;
1017 * If you are holding the spa config lock as writer, you shouldn't
1018 * be asking the DMU to do *anything* unless it's the root pool
1019 * which may require us to read from the root filesystem while
1020 * holding some (not all) of the locks as writer.
1022 ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0 ||
1023 (spa_is_root(os->os_spa) &&
1024 spa_config_held(os->os_spa, SCL_STATE, RW_WRITER)));
1026 if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT) {
1027 dn = (object == DMU_USERUSED_OBJECT) ?
1028 DMU_USERUSED_DNODE(os) : DMU_GROUPUSED_DNODE(os);
1032 if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE)
1034 if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)
1037 (void) refcount_add(&dn->dn_holds, tag);
1042 if (object == 0 || object >= DN_MAX_OBJECT)
1045 mdn = DMU_META_DNODE(os);
1046 ASSERT(mdn->dn_object == DMU_META_DNODE_OBJECT);
1050 if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
1051 rw_enter(&mdn->dn_struct_rwlock, RW_READER);
1052 drop_struct_lock = TRUE;
1055 blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t));
1057 db = dbuf_hold(mdn, blk, FTAG);
1058 if (drop_struct_lock)
1059 rw_exit(&mdn->dn_struct_rwlock);
1062 err = dbuf_read(db, NULL, DB_RF_CANFAIL);
1064 dbuf_rele(db, FTAG);
1068 ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
1069 epb = db->db.db_size >> DNODE_SHIFT;
1071 idx = object & (epb-1);
1073 ASSERT(DB_DNODE(db)->dn_type == DMU_OT_DNODE);
1074 children_dnodes = dmu_buf_get_user(&db->db);
1075 if (children_dnodes == NULL) {
1077 dnode_children_t *winner;
1078 children_dnodes = kmem_alloc(sizeof (dnode_children_t) +
1079 (epb - 1) * sizeof (dnode_handle_t),
1080 KM_PUSHPAGE | KM_NODEBUG);
1081 children_dnodes->dnc_count = epb;
1082 dnh = &children_dnodes->dnc_children[0];
1083 for (i = 0; i < epb; i++) {
1084 zrl_init(&dnh[i].dnh_zrlock);
1085 dnh[i].dnh_dnode = NULL;
1087 if ((winner = dmu_buf_set_user(&db->db, children_dnodes, NULL,
1088 dnode_buf_pageout))) {
1089 kmem_free(children_dnodes, sizeof (dnode_children_t) +
1090 (epb - 1) * sizeof (dnode_handle_t));
1091 children_dnodes = winner;
1094 ASSERT(children_dnodes->dnc_count == epb);
1096 dnh = &children_dnodes->dnc_children[idx];
1097 zrl_add(&dnh->dnh_zrlock);
1098 if ((dn = dnh->dnh_dnode) == NULL) {
1099 dnode_phys_t *phys = (dnode_phys_t *)db->db.db_data+idx;
1102 dn = dnode_create(os, phys, db, object, dnh);
1103 winner = atomic_cas_ptr(&dnh->dnh_dnode, NULL, dn);
1104 if (winner != NULL) {
1105 zrl_add(&dnh->dnh_zrlock);
1106 dnode_destroy(dn); /* implicit zrl_remove() */
1111 mutex_enter(&dn->dn_mtx);
1113 if (dn->dn_free_txg ||
1114 ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
1115 ((flag & DNODE_MUST_BE_FREE) &&
1116 (type != DMU_OT_NONE || !refcount_is_zero(&dn->dn_holds)))) {
1117 mutex_exit(&dn->dn_mtx);
1118 zrl_remove(&dnh->dnh_zrlock);
1119 dbuf_rele(db, FTAG);
1120 return (type == DMU_OT_NONE ? ENOENT : EEXIST);
1122 mutex_exit(&dn->dn_mtx);
1124 if (refcount_add(&dn->dn_holds, tag) == 1)
1125 dbuf_add_ref(db, dnh);
1126 /* Now we can rely on the hold to prevent the dnode from moving. */
1127 zrl_remove(&dnh->dnh_zrlock);
1130 ASSERT3P(dn->dn_dbuf, ==, db);
1131 ASSERT3U(dn->dn_object, ==, object);
1132 dbuf_rele(db, FTAG);
1139 * Return held dnode if the object is allocated, NULL if not.
1142 dnode_hold(objset_t *os, uint64_t object, void *tag, dnode_t **dnp)
1144 return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
1148 * Can only add a reference if there is already at least one
1149 * reference on the dnode. Returns FALSE if unable to add a
1153 dnode_add_ref(dnode_t *dn, void *tag)
1155 mutex_enter(&dn->dn_mtx);
1156 if (refcount_is_zero(&dn->dn_holds)) {
1157 mutex_exit(&dn->dn_mtx);
1160 VERIFY(1 < refcount_add(&dn->dn_holds, tag));
1161 mutex_exit(&dn->dn_mtx);
1166 dnode_rele(dnode_t *dn, void *tag)
1169 /* Get while the hold prevents the dnode from moving. */
1170 dmu_buf_impl_t *db = dn->dn_dbuf;
1171 dnode_handle_t *dnh = dn->dn_handle;
1173 mutex_enter(&dn->dn_mtx);
1174 refs = refcount_remove(&dn->dn_holds, tag);
1175 mutex_exit(&dn->dn_mtx);
1178 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1179 * indirectly by dbuf_rele() while relying on the dnode handle to
1180 * prevent the dnode from moving, since releasing the last hold could
1181 * result in the dnode's parent dbuf evicting its dnode handles. For
1182 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1183 * other direct or indirect hold on the dnode must first drop the dnode
1186 ASSERT(refs > 0 || dnh->dnh_zrlock.zr_owner != curthread);
1188 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1189 if (refs == 0 && db != NULL) {
1191 * Another thread could add a hold to the dnode handle in
1192 * dnode_hold_impl() while holding the parent dbuf. Since the
1193 * hold on the parent dbuf prevents the handle from being
1194 * destroyed, the hold on the handle is OK. We can't yet assert
1195 * that the handle has zero references, but that will be
1196 * asserted anyway when the handle gets destroyed.
1203 dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
1205 objset_t *os = dn->dn_objset;
1206 uint64_t txg = tx->tx_txg;
1208 if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
1209 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1216 mutex_enter(&dn->dn_mtx);
1217 ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
1218 ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg);
1219 mutex_exit(&dn->dn_mtx);
1223 * Determine old uid/gid when necessary
1225 dmu_objset_userquota_get_ids(dn, B_TRUE, tx);
1227 mutex_enter(&os->os_lock);
1230 * If we are already marked dirty, we're done.
1232 if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
1233 mutex_exit(&os->os_lock);
1237 ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs));
1238 ASSERT(dn->dn_datablksz != 0);
1239 ASSERT0(dn->dn_next_bonuslen[txg&TXG_MASK]);
1240 ASSERT0(dn->dn_next_blksz[txg&TXG_MASK]);
1241 ASSERT0(dn->dn_next_bonustype[txg&TXG_MASK]);
1243 dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
1244 dn->dn_object, txg);
1246 if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
1247 list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
1249 list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
1252 mutex_exit(&os->os_lock);
1255 * The dnode maintains a hold on its containing dbuf as
1256 * long as there are holds on it. Each instantiated child
1257 * dbuf maintains a hold on the dnode. When the last child
1258 * drops its hold, the dnode will drop its hold on the
1259 * containing dbuf. We add a "dirty hold" here so that the
1260 * dnode will hang around after we finish processing its
1263 VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
1265 (void) dbuf_dirty(dn->dn_dbuf, tx);
1267 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1271 dnode_free(dnode_t *dn, dmu_tx_t *tx)
1273 int txgoff = tx->tx_txg & TXG_MASK;
1275 dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
1277 /* we should be the only holder... hopefully */
1278 /* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
1280 mutex_enter(&dn->dn_mtx);
1281 if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
1282 mutex_exit(&dn->dn_mtx);
1285 dn->dn_free_txg = tx->tx_txg;
1286 mutex_exit(&dn->dn_mtx);
1289 * If the dnode is already dirty, it needs to be moved from
1290 * the dirty list to the free list.
1292 mutex_enter(&dn->dn_objset->os_lock);
1293 if (list_link_active(&dn->dn_dirty_link[txgoff])) {
1294 list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
1295 list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
1296 mutex_exit(&dn->dn_objset->os_lock);
1298 mutex_exit(&dn->dn_objset->os_lock);
1299 dnode_setdirty(dn, tx);
1304 * Try to change the block size for the indicated dnode. This can only
1305 * succeed if there are no blocks allocated or dirty beyond first block
1308 dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
1310 dmu_buf_impl_t *db, *db_next;
1314 size = SPA_MINBLOCKSIZE;
1315 if (size > SPA_MAXBLOCKSIZE)
1316 size = SPA_MAXBLOCKSIZE;
1318 size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
1320 if (ibs == dn->dn_indblkshift)
1323 if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
1326 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1328 /* Check for any allocated blocks beyond the first */
1329 if (dn->dn_phys->dn_maxblkid != 0)
1332 mutex_enter(&dn->dn_dbufs_mtx);
1333 for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
1334 db_next = list_next(&dn->dn_dbufs, db);
1336 if (db->db_blkid != 0 && db->db_blkid != DMU_BONUS_BLKID &&
1337 db->db_blkid != DMU_SPILL_BLKID) {
1338 mutex_exit(&dn->dn_dbufs_mtx);
1342 mutex_exit(&dn->dn_dbufs_mtx);
1344 if (ibs && dn->dn_nlevels != 1)
1347 /* resize the old block */
1348 err = dbuf_hold_impl(dn, 0, 0, TRUE, FTAG, &db);
1350 dbuf_new_size(db, size, tx);
1351 else if (err != ENOENT)
1354 dnode_setdblksz(dn, size);
1355 dnode_setdirty(dn, tx);
1356 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
1358 dn->dn_indblkshift = ibs;
1359 dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
1361 /* rele after we have fixed the blocksize in the dnode */
1363 dbuf_rele(db, FTAG);
1365 rw_exit(&dn->dn_struct_rwlock);
1369 rw_exit(&dn->dn_struct_rwlock);
1373 /* read-holding callers must not rely on the lock being continuously held */
1375 dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read)
1377 uint64_t txgoff = tx->tx_txg & TXG_MASK;
1378 int epbs, new_nlevels;
1381 ASSERT(blkid != DMU_BONUS_BLKID);
1384 RW_READ_HELD(&dn->dn_struct_rwlock) :
1385 RW_WRITE_HELD(&dn->dn_struct_rwlock));
1388 * if we have a read-lock, check to see if we need to do any work
1389 * before upgrading to a write-lock.
1392 if (blkid <= dn->dn_maxblkid)
1395 if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
1396 rw_exit(&dn->dn_struct_rwlock);
1397 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1401 if (blkid <= dn->dn_maxblkid)
1404 dn->dn_maxblkid = blkid;
1407 * Compute the number of levels necessary to support the new maxblkid.
1410 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1411 for (sz = dn->dn_nblkptr;
1412 sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
1415 if (new_nlevels > dn->dn_nlevels) {
1416 int old_nlevels = dn->dn_nlevels;
1419 dbuf_dirty_record_t *new, *dr, *dr_next;
1421 dn->dn_nlevels = new_nlevels;
1423 ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
1424 dn->dn_next_nlevels[txgoff] = new_nlevels;
1426 /* dirty the left indirects */
1427 db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
1429 new = dbuf_dirty(db, tx);
1430 dbuf_rele(db, FTAG);
1432 /* transfer the dirty records to the new indirect */
1433 mutex_enter(&dn->dn_mtx);
1434 mutex_enter(&new->dt.di.dr_mtx);
1435 list = &dn->dn_dirty_records[txgoff];
1436 for (dr = list_head(list); dr; dr = dr_next) {
1437 dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
1438 if (dr->dr_dbuf->db_level != new_nlevels-1 &&
1439 dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
1440 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
1441 ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
1442 list_remove(&dn->dn_dirty_records[txgoff], dr);
1443 list_insert_tail(&new->dt.di.dr_children, dr);
1444 dr->dr_parent = new;
1447 mutex_exit(&new->dt.di.dr_mtx);
1448 mutex_exit(&dn->dn_mtx);
1453 rw_downgrade(&dn->dn_struct_rwlock);
1457 dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
1459 avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1462 free_range_t rp_tofind;
1463 uint64_t endblk = blkid + nblks;
1465 ASSERT(MUTEX_HELD(&dn->dn_mtx));
1466 ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */
1468 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1469 blkid, nblks, tx->tx_txg);
1470 rp_tofind.fr_blkid = blkid;
1471 rp = avl_find(tree, &rp_tofind, &where);
1473 rp = avl_nearest(tree, where, AVL_BEFORE);
1475 rp = avl_nearest(tree, where, AVL_AFTER);
1477 while (rp && (rp->fr_blkid <= blkid + nblks)) {
1478 uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks;
1479 free_range_t *nrp = AVL_NEXT(tree, rp);
1481 if (blkid <= rp->fr_blkid && endblk >= fr_endblk) {
1482 /* clear this entire range */
1483 avl_remove(tree, rp);
1484 kmem_free(rp, sizeof (free_range_t));
1485 } else if (blkid <= rp->fr_blkid &&
1486 endblk > rp->fr_blkid && endblk < fr_endblk) {
1487 /* clear the beginning of this range */
1488 rp->fr_blkid = endblk;
1489 rp->fr_nblks = fr_endblk - endblk;
1490 } else if (blkid > rp->fr_blkid && blkid < fr_endblk &&
1491 endblk >= fr_endblk) {
1492 /* clear the end of this range */
1493 rp->fr_nblks = blkid - rp->fr_blkid;
1494 } else if (blkid > rp->fr_blkid && endblk < fr_endblk) {
1495 /* clear a chunk out of this range */
1496 free_range_t *new_rp =
1497 kmem_alloc(sizeof (free_range_t), KM_PUSHPAGE);
1499 new_rp->fr_blkid = endblk;
1500 new_rp->fr_nblks = fr_endblk - endblk;
1501 avl_insert_here(tree, new_rp, rp, AVL_AFTER);
1502 rp->fr_nblks = blkid - rp->fr_blkid;
1504 /* there may be no overlap */
1510 dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
1513 uint64_t blkoff, blkid, nblks;
1514 int blksz, blkshift, head, tail;
1518 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1519 blksz = dn->dn_datablksz;
1520 blkshift = dn->dn_datablkshift;
1521 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1524 len = UINT64_MAX - off;
1529 * First, block align the region to free:
1532 head = P2NPHASE(off, blksz);
1533 blkoff = P2PHASE(off, blksz);
1534 if ((off >> blkshift) > dn->dn_maxblkid)
1537 ASSERT(dn->dn_maxblkid == 0);
1538 if (off == 0 && len >= blksz) {
1539 /* Freeing the whole block; fast-track this request */
1543 } else if (off >= blksz) {
1544 /* Freeing past end-of-data */
1547 /* Freeing part of the block. */
1549 ASSERT3U(head, >, 0);
1553 /* zero out any partial block data at the start of the range */
1555 ASSERT3U(blkoff + head, ==, blksz);
1558 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE,
1562 /* don't dirty if it isn't on disk and isn't dirty */
1563 if (db->db_last_dirty ||
1564 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1565 rw_exit(&dn->dn_struct_rwlock);
1566 dbuf_will_dirty(db, tx);
1567 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1568 data = db->db.db_data;
1569 bzero(data + blkoff, head);
1571 dbuf_rele(db, FTAG);
1577 /* If the range was less than one block, we're done */
1581 /* If the remaining range is past end of file, we're done */
1582 if ((off >> blkshift) > dn->dn_maxblkid)
1585 ASSERT(ISP2(blksz));
1589 tail = P2PHASE(len, blksz);
1591 ASSERT0(P2PHASE(off, blksz));
1592 /* zero out any partial block data at the end of the range */
1596 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
1597 TRUE, FTAG, &db) == 0) {
1598 /* don't dirty if not on disk and not dirty */
1599 if (db->db_last_dirty ||
1600 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1601 rw_exit(&dn->dn_struct_rwlock);
1602 dbuf_will_dirty(db, tx);
1603 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1604 bzero(db->db.db_data, tail);
1606 dbuf_rele(db, FTAG);
1611 /* If the range did not include a full block, we are done */
1615 ASSERT(IS_P2ALIGNED(off, blksz));
1616 ASSERT(trunc || IS_P2ALIGNED(len, blksz));
1617 blkid = off >> blkshift;
1618 nblks = len >> blkshift;
1623 * Read in and mark all the level-1 indirects dirty,
1624 * so that they will stay in memory until syncing phase.
1625 * Always dirty the first and last indirect to make sure
1626 * we dirty all the partial indirects.
1628 if (dn->dn_nlevels > 1) {
1629 uint64_t i, first, last;
1630 int shift = epbs + dn->dn_datablkshift;
1632 first = blkid >> epbs;
1633 if ((db = dbuf_hold_level(dn, 1, first, FTAG))) {
1634 dbuf_will_dirty(db, tx);
1635 dbuf_rele(db, FTAG);
1638 last = dn->dn_maxblkid >> epbs;
1640 last = (blkid + nblks - 1) >> epbs;
1641 if (last > first && (db = dbuf_hold_level(dn, 1, last, FTAG))) {
1642 dbuf_will_dirty(db, tx);
1643 dbuf_rele(db, FTAG);
1645 for (i = first + 1; i < last; i++) {
1646 uint64_t ibyte = i << shift;
1649 err = dnode_next_offset(dn,
1650 DNODE_FIND_HAVELOCK, &ibyte, 1, 1, 0);
1652 if (err == ESRCH || i >= last)
1655 db = dbuf_hold_level(dn, 1, i, FTAG);
1657 dbuf_will_dirty(db, tx);
1658 dbuf_rele(db, FTAG);
1664 * Add this range to the dnode range list.
1665 * We will finish up this free operation in the syncing phase.
1667 mutex_enter(&dn->dn_mtx);
1668 dnode_clear_range(dn, blkid, nblks, tx);
1670 free_range_t *rp, *found;
1672 avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1674 /* Add new range to dn_ranges */
1675 rp = kmem_alloc(sizeof (free_range_t), KM_PUSHPAGE);
1676 rp->fr_blkid = blkid;
1677 rp->fr_nblks = nblks;
1678 found = avl_find(tree, rp, &where);
1679 ASSERT(found == NULL);
1680 avl_insert(tree, rp, where);
1681 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1682 blkid, nblks, tx->tx_txg);
1684 mutex_exit(&dn->dn_mtx);
1686 dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
1687 dnode_setdirty(dn, tx);
1689 if (trunc && dn->dn_maxblkid >= (off >> blkshift))
1690 dn->dn_maxblkid = (off >> blkshift ? (off >> blkshift) - 1 : 0);
1692 rw_exit(&dn->dn_struct_rwlock);
1696 dnode_spill_freed(dnode_t *dn)
1700 mutex_enter(&dn->dn_mtx);
1701 for (i = 0; i < TXG_SIZE; i++) {
1702 if (dn->dn_rm_spillblk[i] == DN_KILL_SPILLBLK)
1705 mutex_exit(&dn->dn_mtx);
1706 return (i < TXG_SIZE);
1709 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1711 dnode_block_freed(dnode_t *dn, uint64_t blkid)
1713 free_range_t range_tofind;
1714 void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1717 if (blkid == DMU_BONUS_BLKID)
1721 * If we're in the process of opening the pool, dp will not be
1722 * set yet, but there shouldn't be anything dirty.
1727 if (dn->dn_free_txg)
1730 if (blkid == DMU_SPILL_BLKID)
1731 return (dnode_spill_freed(dn));
1733 range_tofind.fr_blkid = blkid;
1734 mutex_enter(&dn->dn_mtx);
1735 for (i = 0; i < TXG_SIZE; i++) {
1736 free_range_t *range_found;
1739 range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx);
1741 ASSERT(range_found->fr_nblks > 0);
1744 range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE);
1746 range_found->fr_blkid + range_found->fr_nblks > blkid)
1749 mutex_exit(&dn->dn_mtx);
1750 return (i < TXG_SIZE);
1753 /* call from syncing context when we actually write/free space for this dnode */
1755 dnode_diduse_space(dnode_t *dn, int64_t delta)
1758 dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
1760 (u_longlong_t)dn->dn_phys->dn_used,
1763 mutex_enter(&dn->dn_mtx);
1764 space = DN_USED_BYTES(dn->dn_phys);
1766 ASSERT3U(space + delta, >=, space); /* no overflow */
1768 ASSERT3U(space, >=, -delta); /* no underflow */
1771 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
1772 ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
1773 ASSERT0(P2PHASE(space, 1<<DEV_BSHIFT));
1774 dn->dn_phys->dn_used = space >> DEV_BSHIFT;
1776 dn->dn_phys->dn_used = space;
1777 dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
1779 mutex_exit(&dn->dn_mtx);
1783 * Call when we think we're going to write/free space in open context.
1784 * Be conservative (ie. OK to write less than this or free more than
1785 * this, but don't write more or free less).
1788 dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1790 objset_t *os = dn->dn_objset;
1791 dsl_dataset_t *ds = os->os_dsl_dataset;
1794 space = spa_get_asize(os->os_spa, space);
1797 dsl_dir_willuse_space(ds->ds_dir, space, tx);
1799 dmu_tx_willuse_space(tx, space);
1803 * This function scans a block at the indicated "level" looking for
1804 * a hole or data (depending on 'flags'). If level > 0, then we are
1805 * scanning an indirect block looking at its pointers. If level == 0,
1806 * then we are looking at a block of dnodes. If we don't find what we
1807 * are looking for in the block, we return ESRCH. Otherwise, return
1808 * with *offset pointing to the beginning (if searching forwards) or
1809 * end (if searching backwards) of the range covered by the block
1810 * pointer we matched on (or dnode).
1812 * The basic search algorithm used below by dnode_next_offset() is to
1813 * use this function to search up the block tree (widen the search) until
1814 * we find something (i.e., we don't return ESRCH) and then search back
1815 * down the tree (narrow the search) until we reach our original search
1819 dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
1820 int lvl, uint64_t blkfill, uint64_t txg)
1822 dmu_buf_impl_t *db = NULL;
1824 uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1825 uint64_t epb = 1ULL << epbs;
1826 uint64_t minfill, maxfill;
1828 int i, inc, error, span;
1830 dprintf("probing object %llu offset %llx level %d of %u\n",
1831 dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
1833 hole = ((flags & DNODE_FIND_HOLE) != 0);
1834 inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1;
1835 ASSERT(txg == 0 || !hole);
1837 if (lvl == dn->dn_phys->dn_nlevels) {
1839 epb = dn->dn_phys->dn_nblkptr;
1840 data = dn->dn_phys->dn_blkptr;
1842 uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl);
1843 error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db);
1845 if (error != ENOENT)
1850 * This can only happen when we are searching up
1851 * the block tree for data. We don't really need to
1852 * adjust the offset, as we will just end up looking
1853 * at the pointer to this block in its parent, and its
1854 * going to be unallocated, so we will skip over it.
1858 error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT);
1860 dbuf_rele(db, FTAG);
1863 data = db->db.db_data;
1867 (db->db_blkptr == NULL || db->db_blkptr->blk_birth <= txg)) {
1869 * This can only happen when we are searching up the tree
1870 * and these conditions mean that we need to keep climbing.
1873 } else if (lvl == 0) {
1874 dnode_phys_t *dnp = data;
1876 ASSERT(dn->dn_type == DMU_OT_DNODE);
1878 for (i = (*offset >> span) & (blkfill - 1);
1879 i >= 0 && i < blkfill; i += inc) {
1880 if ((dnp[i].dn_type == DMU_OT_NONE) == hole)
1882 *offset += (1ULL << span) * inc;
1884 if (i < 0 || i == blkfill)
1887 blkptr_t *bp = data;
1888 uint64_t start = *offset;
1889 span = (lvl - 1) * epbs + dn->dn_datablkshift;
1891 maxfill = blkfill << ((lvl - 1) * epbs);
1898 *offset = *offset >> span;
1899 for (i = BF64_GET(*offset, 0, epbs);
1900 i >= 0 && i < epb; i += inc) {
1901 if (bp[i].blk_fill >= minfill &&
1902 bp[i].blk_fill <= maxfill &&
1903 (hole || bp[i].blk_birth > txg))
1905 if (inc > 0 || *offset > 0)
1908 *offset = *offset << span;
1910 /* traversing backwards; position offset at the end */
1911 ASSERT3U(*offset, <=, start);
1912 *offset = MIN(*offset + (1ULL << span) - 1, start);
1913 } else if (*offset < start) {
1916 if (i < 0 || i >= epb)
1921 dbuf_rele(db, FTAG);
1927 * Find the next hole, data, or sparse region at or after *offset.
1928 * The value 'blkfill' tells us how many items we expect to find
1929 * in an L0 data block; this value is 1 for normal objects,
1930 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1931 * DNODES_PER_BLOCK when searching for sparse regions thereof.
1935 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
1936 * Finds the next/previous hole/data in a file.
1937 * Used in dmu_offset_next().
1939 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
1940 * Finds the next free/allocated dnode an objset's meta-dnode.
1941 * Only finds objects that have new contents since txg (ie.
1942 * bonus buffer changes and content removal are ignored).
1943 * Used in dmu_object_next().
1945 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
1946 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
1947 * Used in dmu_object_alloc().
1950 dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
1951 int minlvl, uint64_t blkfill, uint64_t txg)
1953 uint64_t initial_offset = *offset;
1957 if (!(flags & DNODE_FIND_HAVELOCK))
1958 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1960 if (dn->dn_phys->dn_nlevels == 0) {
1965 if (dn->dn_datablkshift == 0) {
1966 if (*offset < dn->dn_datablksz) {
1967 if (flags & DNODE_FIND_HOLE)
1968 *offset = dn->dn_datablksz;
1975 maxlvl = dn->dn_phys->dn_nlevels;
1977 for (lvl = minlvl; lvl <= maxlvl; lvl++) {
1978 error = dnode_next_offset_level(dn,
1979 flags, offset, lvl, blkfill, txg);
1984 while (error == 0 && --lvl >= minlvl) {
1985 error = dnode_next_offset_level(dn,
1986 flags, offset, lvl, blkfill, txg);
1989 if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
1990 initial_offset < *offset : initial_offset > *offset))
1993 if (!(flags & DNODE_FIND_HAVELOCK))
1994 rw_exit(&dn->dn_struct_rwlock);