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.
25 #include <sys/zfs_context.h>
27 #include <sys/dnode.h>
29 #include <sys/dmu_impl.h>
30 #include <sys/dmu_tx.h>
31 #include <sys/dmu_objset.h>
32 #include <sys/dsl_dir.h>
33 #include <sys/dsl_dataset.h>
36 #include <sys/dmu_zfetch.h>
38 static int free_range_compar(const void *node1, const void *node2);
40 static kmem_cache_t *dnode_cache;
42 * Define DNODE_STATS to turn on statistic gathering. By default, it is only
43 * turned on when DEBUG is also defined.
50 #define DNODE_STAT_ADD(stat) ((stat)++)
52 #define DNODE_STAT_ADD(stat) /* nothing */
53 #endif /* DNODE_STATS */
55 static dnode_phys_t dnode_phys_zero;
57 int zfs_default_bs = SPA_MINBLOCKSHIFT;
58 int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
61 static kmem_cbrc_t dnode_move(void *, void *, size_t, void *);
66 dnode_cons(void *arg, void *unused, int kmflag)
71 rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL);
72 mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
73 mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
74 cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL);
76 refcount_create(&dn->dn_holds);
77 refcount_create(&dn->dn_tx_holds);
78 list_link_init(&dn->dn_link);
80 bzero(&dn->dn_next_nblkptr[0], sizeof (dn->dn_next_nblkptr));
81 bzero(&dn->dn_next_nlevels[0], sizeof (dn->dn_next_nlevels));
82 bzero(&dn->dn_next_indblkshift[0], sizeof (dn->dn_next_indblkshift));
83 bzero(&dn->dn_next_bonustype[0], sizeof (dn->dn_next_bonustype));
84 bzero(&dn->dn_rm_spillblk[0], sizeof (dn->dn_rm_spillblk));
85 bzero(&dn->dn_next_bonuslen[0], sizeof (dn->dn_next_bonuslen));
86 bzero(&dn->dn_next_blksz[0], sizeof (dn->dn_next_blksz));
88 for (i = 0; i < TXG_SIZE; i++) {
89 list_link_init(&dn->dn_dirty_link[i]);
90 avl_create(&dn->dn_ranges[i], free_range_compar,
91 sizeof (free_range_t),
92 offsetof(struct free_range, fr_node));
93 list_create(&dn->dn_dirty_records[i],
94 sizeof (dbuf_dirty_record_t),
95 offsetof(dbuf_dirty_record_t, dr_dirty_node));
98 dn->dn_allocated_txg = 0;
100 dn->dn_assigned_txg = 0;
102 dn->dn_dirtyctx_firstset = NULL;
104 dn->dn_have_spill = B_FALSE;
114 dn->dn_dbufs_count = 0;
115 list_create(&dn->dn_dbufs, sizeof (dmu_buf_impl_t),
116 offsetof(dmu_buf_impl_t, db_link));
119 POINTER_INVALIDATE(&dn->dn_objset);
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 ASSERT3U(dn->dn_next_nblkptr[i], ==, 0);
143 ASSERT3U(dn->dn_next_nlevels[i], ==, 0);
144 ASSERT3U(dn->dn_next_indblkshift[i], ==, 0);
145 ASSERT3U(dn->dn_next_bonustype[i], ==, 0);
146 ASSERT3U(dn->dn_rm_spillblk[i], ==, 0);
147 ASSERT3U(dn->dn_next_bonuslen[i], ==, 0);
148 ASSERT3U(dn->dn_next_blksz[i], ==, 0);
151 ASSERT3U(dn->dn_allocated_txg, ==, 0);
152 ASSERT3U(dn->dn_free_txg, ==, 0);
153 ASSERT3U(dn->dn_assigned_txg, ==, 0);
154 ASSERT3U(dn->dn_dirtyctx, ==, 0);
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 ASSERT3U(dn->dn_oldused, ==, 0);
160 ASSERT3U(dn->dn_oldflags, ==, 0);
161 ASSERT3U(dn->dn_olduid, ==, 0);
162 ASSERT3U(dn->dn_oldgid, ==, 0);
163 ASSERT3U(dn->dn_newuid, ==, 0);
164 ASSERT3U(dn->dn_newgid, ==, 0);
165 ASSERT3U(dn->dn_id_flags, ==, 0);
167 ASSERT3U(dn->dn_dbufs_count, ==, 0);
168 list_destroy(&dn->dn_dbufs);
174 ASSERT(dnode_cache == NULL);
175 dnode_cache = kmem_cache_create("dnode_t",
177 0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
178 kmem_cache_set_move(dnode_cache, dnode_move);
184 kmem_cache_destroy(dnode_cache);
191 dnode_verify(dnode_t *dn)
193 int drop_struct_lock = FALSE;
196 ASSERT(dn->dn_objset);
197 ASSERT(dn->dn_handle->dnh_dnode == dn);
199 ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
201 if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
204 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
205 rw_enter(&dn->dn_struct_rwlock, RW_READER);
206 drop_struct_lock = TRUE;
208 if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
210 ASSERT3U(dn->dn_indblkshift, >=, 0);
211 ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
212 if (dn->dn_datablkshift) {
213 ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
214 ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
215 ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
217 ASSERT3U(dn->dn_nlevels, <=, 30);
218 ASSERT3U(dn->dn_type, <=, DMU_OT_NUMTYPES);
219 ASSERT3U(dn->dn_nblkptr, >=, 1);
220 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
221 ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
222 ASSERT3U(dn->dn_datablksz, ==,
223 dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
224 ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
225 ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
226 dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
227 for (i = 0; i < TXG_SIZE; i++) {
228 ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
231 if (dn->dn_phys->dn_type != DMU_OT_NONE)
232 ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
233 ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_object) || dn->dn_dbuf != NULL);
234 if (dn->dn_dbuf != NULL) {
235 ASSERT3P(dn->dn_phys, ==,
236 (dnode_phys_t *)dn->dn_dbuf->db.db_data +
237 (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
239 if (drop_struct_lock)
240 rw_exit(&dn->dn_struct_rwlock);
245 dnode_byteswap(dnode_phys_t *dnp)
247 uint64_t *buf64 = (void*)&dnp->dn_blkptr;
250 if (dnp->dn_type == DMU_OT_NONE) {
251 bzero(dnp, sizeof (dnode_phys_t));
255 dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
256 dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
257 dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
258 dnp->dn_used = BSWAP_64(dnp->dn_used);
261 * dn_nblkptr is only one byte, so it's OK to read it in either
262 * byte order. We can't read dn_bouslen.
264 ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
265 ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
266 for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
267 buf64[i] = BSWAP_64(buf64[i]);
270 * OK to check dn_bonuslen for zero, because it won't matter if
271 * we have the wrong byte order. This is necessary because the
272 * dnode dnode is smaller than a regular dnode.
274 if (dnp->dn_bonuslen != 0) {
276 * Note that the bonus length calculated here may be
277 * longer than the actual bonus buffer. This is because
278 * we always put the bonus buffer after the last block
279 * pointer (instead of packing it against the end of the
282 int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
283 size_t len = DN_MAX_BONUSLEN - off;
284 ASSERT3U(dnp->dn_bonustype, <, DMU_OT_NUMTYPES);
285 dmu_ot[dnp->dn_bonustype].ot_byteswap(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 ASSERT3U(P2PHASE(size, SPA_MINBLOCKSIZE), ==, 0);
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_SLEEP);
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(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
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 ASSERT3U(ot, <, DMU_OT_NUMTYPES);
503 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
504 (bonustype == DMU_OT_SA && bonuslen == 0) ||
505 (bonustype != DMU_OT_NONE && bonuslen != 0));
506 ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
507 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
508 ASSERT(dn->dn_type == DMU_OT_NONE);
509 ASSERT3U(dn->dn_maxblkid, ==, 0);
510 ASSERT3U(dn->dn_allocated_txg, ==, 0);
511 ASSERT3U(dn->dn_assigned_txg, ==, 0);
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 ASSERT3U(dn->dn_next_nblkptr[i], ==, 0);
518 ASSERT3U(dn->dn_next_nlevels[i], ==, 0);
519 ASSERT3U(dn->dn_next_indblkshift[i], ==, 0);
520 ASSERT3U(dn->dn_next_bonuslen[i], ==, 0);
521 ASSERT3U(dn->dn_next_bonustype[i], ==, 0);
522 ASSERT3U(dn->dn_rm_spillblk[i], ==, 0);
523 ASSERT3U(dn->dn_next_blksz[i], ==, 0);
524 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
525 ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
526 ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0);
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 ASSERT3U(blocksize % SPA_MINBLOCKSIZE, ==, 0);
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 ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
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);
634 uint64_t dms_dnode_invalid;
635 uint64_t dms_dnode_recheck1;
636 uint64_t dms_dnode_recheck2;
637 uint64_t dms_dnode_special;
638 uint64_t dms_dnode_handle;
639 uint64_t dms_dnode_rwlock;
640 uint64_t dms_dnode_active;
642 #endif /* DNODE_STATS */
645 dnode_move_impl(dnode_t *odn, dnode_t *ndn)
649 ASSERT(!RW_LOCK_HELD(&odn->dn_struct_rwlock));
650 ASSERT(MUTEX_NOT_HELD(&odn->dn_mtx));
651 ASSERT(MUTEX_NOT_HELD(&odn->dn_dbufs_mtx));
652 ASSERT(!RW_LOCK_HELD(&odn->dn_zfetch.zf_rwlock));
655 ndn->dn_objset = odn->dn_objset;
656 ndn->dn_object = odn->dn_object;
657 ndn->dn_dbuf = odn->dn_dbuf;
658 ndn->dn_handle = odn->dn_handle;
659 ndn->dn_phys = odn->dn_phys;
660 ndn->dn_type = odn->dn_type;
661 ndn->dn_bonuslen = odn->dn_bonuslen;
662 ndn->dn_bonustype = odn->dn_bonustype;
663 ndn->dn_nblkptr = odn->dn_nblkptr;
664 ndn->dn_checksum = odn->dn_checksum;
665 ndn->dn_compress = odn->dn_compress;
666 ndn->dn_nlevels = odn->dn_nlevels;
667 ndn->dn_indblkshift = odn->dn_indblkshift;
668 ndn->dn_datablkshift = odn->dn_datablkshift;
669 ndn->dn_datablkszsec = odn->dn_datablkszsec;
670 ndn->dn_datablksz = odn->dn_datablksz;
671 ndn->dn_maxblkid = odn->dn_maxblkid;
672 bcopy(&odn->dn_next_nblkptr[0], &ndn->dn_next_nblkptr[0],
673 sizeof (odn->dn_next_nblkptr));
674 bcopy(&odn->dn_next_nlevels[0], &ndn->dn_next_nlevels[0],
675 sizeof (odn->dn_next_nlevels));
676 bcopy(&odn->dn_next_indblkshift[0], &ndn->dn_next_indblkshift[0],
677 sizeof (odn->dn_next_indblkshift));
678 bcopy(&odn->dn_next_bonustype[0], &ndn->dn_next_bonustype[0],
679 sizeof (odn->dn_next_bonustype));
680 bcopy(&odn->dn_rm_spillblk[0], &ndn->dn_rm_spillblk[0],
681 sizeof (odn->dn_rm_spillblk));
682 bcopy(&odn->dn_next_bonuslen[0], &ndn->dn_next_bonuslen[0],
683 sizeof (odn->dn_next_bonuslen));
684 bcopy(&odn->dn_next_blksz[0], &ndn->dn_next_blksz[0],
685 sizeof (odn->dn_next_blksz));
686 for (i = 0; i < TXG_SIZE; i++) {
687 list_move_tail(&ndn->dn_dirty_records[i],
688 &odn->dn_dirty_records[i]);
690 bcopy(&odn->dn_ranges[0], &ndn->dn_ranges[0], sizeof (odn->dn_ranges));
691 ndn->dn_allocated_txg = odn->dn_allocated_txg;
692 ndn->dn_free_txg = odn->dn_free_txg;
693 ndn->dn_assigned_txg = odn->dn_assigned_txg;
694 ndn->dn_dirtyctx = odn->dn_dirtyctx;
695 ndn->dn_dirtyctx_firstset = odn->dn_dirtyctx_firstset;
696 ASSERT(refcount_count(&odn->dn_tx_holds) == 0);
697 refcount_transfer(&ndn->dn_holds, &odn->dn_holds);
698 ASSERT(list_is_empty(&ndn->dn_dbufs));
699 list_move_tail(&ndn->dn_dbufs, &odn->dn_dbufs);
700 ndn->dn_dbufs_count = odn->dn_dbufs_count;
701 ndn->dn_bonus = odn->dn_bonus;
702 ndn->dn_have_spill = odn->dn_have_spill;
703 ndn->dn_zio = odn->dn_zio;
704 ndn->dn_oldused = odn->dn_oldused;
705 ndn->dn_oldflags = odn->dn_oldflags;
706 ndn->dn_olduid = odn->dn_olduid;
707 ndn->dn_oldgid = odn->dn_oldgid;
708 ndn->dn_newuid = odn->dn_newuid;
709 ndn->dn_newgid = odn->dn_newgid;
710 ndn->dn_id_flags = odn->dn_id_flags;
711 dmu_zfetch_init(&ndn->dn_zfetch, NULL);
712 list_move_tail(&ndn->dn_zfetch.zf_stream, &odn->dn_zfetch.zf_stream);
713 ndn->dn_zfetch.zf_dnode = odn->dn_zfetch.zf_dnode;
714 ndn->dn_zfetch.zf_stream_cnt = odn->dn_zfetch.zf_stream_cnt;
715 ndn->dn_zfetch.zf_alloc_fail = odn->dn_zfetch.zf_alloc_fail;
718 * Update back pointers. Updating the handle fixes the back pointer of
719 * every descendant dbuf as well as the bonus dbuf.
721 ASSERT(ndn->dn_handle->dnh_dnode == odn);
722 ndn->dn_handle->dnh_dnode = ndn;
723 if (ndn->dn_zfetch.zf_dnode == odn) {
724 ndn->dn_zfetch.zf_dnode = ndn;
728 * Invalidate the original dnode by clearing all of its back pointers.
731 odn->dn_handle = NULL;
732 list_create(&odn->dn_dbufs, sizeof (dmu_buf_impl_t),
733 offsetof(dmu_buf_impl_t, db_link));
734 odn->dn_dbufs_count = 0;
735 odn->dn_bonus = NULL;
736 odn->dn_zfetch.zf_dnode = NULL;
739 * Set the low bit of the objset pointer to ensure that dnode_move()
740 * recognizes the dnode as invalid in any subsequent callback.
742 POINTER_INVALIDATE(&odn->dn_objset);
745 * Satisfy the destructor.
747 for (i = 0; i < TXG_SIZE; i++) {
748 list_create(&odn->dn_dirty_records[i],
749 sizeof (dbuf_dirty_record_t),
750 offsetof(dbuf_dirty_record_t, dr_dirty_node));
751 odn->dn_ranges[i].avl_root = NULL;
752 odn->dn_ranges[i].avl_numnodes = 0;
753 odn->dn_next_nlevels[i] = 0;
754 odn->dn_next_indblkshift[i] = 0;
755 odn->dn_next_bonustype[i] = 0;
756 odn->dn_rm_spillblk[i] = 0;
757 odn->dn_next_bonuslen[i] = 0;
758 odn->dn_next_blksz[i] = 0;
760 odn->dn_allocated_txg = 0;
761 odn->dn_free_txg = 0;
762 odn->dn_assigned_txg = 0;
763 odn->dn_dirtyctx = 0;
764 odn->dn_dirtyctx_firstset = NULL;
765 odn->dn_have_spill = B_FALSE;
768 odn->dn_oldflags = 0;
773 odn->dn_id_flags = 0;
779 odn->dn_moved = (uint8_t)-1;
786 dnode_move(void *buf, void *newbuf, size_t size, void *arg)
788 dnode_t *odn = buf, *ndn = newbuf;
794 * The dnode is on the objset's list of known dnodes if the objset
795 * pointer is valid. We set the low bit of the objset pointer when
796 * freeing the dnode to invalidate it, and the memory patterns written
797 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
798 * A newly created dnode sets the objset pointer last of all to indicate
799 * that the dnode is known and in a valid state to be moved by this
803 if (!POINTER_IS_VALID(os)) {
804 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_invalid);
805 return (KMEM_CBRC_DONT_KNOW);
809 * Ensure that the objset does not go away during the move.
811 rw_enter(&os_lock, RW_WRITER);
812 if (os != odn->dn_objset) {
814 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck1);
815 return (KMEM_CBRC_DONT_KNOW);
819 * If the dnode is still valid, then so is the objset. We know that no
820 * valid objset can be freed while we hold os_lock, so we can safely
821 * ensure that the objset remains in use.
823 mutex_enter(&os->os_lock);
826 * Recheck the objset pointer in case the dnode was removed just before
827 * acquiring the lock.
829 if (os != odn->dn_objset) {
830 mutex_exit(&os->os_lock);
832 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck2);
833 return (KMEM_CBRC_DONT_KNOW);
837 * At this point we know that as long as we hold os->os_lock, the dnode
838 * cannot be freed and fields within the dnode can be safely accessed.
839 * The objset listing this dnode cannot go away as long as this dnode is
843 if (DMU_OBJECT_IS_SPECIAL(odn->dn_object)) {
844 mutex_exit(&os->os_lock);
845 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_special);
846 return (KMEM_CBRC_NO);
848 ASSERT(odn->dn_dbuf != NULL); /* only "special" dnodes have no parent */
851 * Lock the dnode handle to prevent the dnode from obtaining any new
852 * holds. This also prevents the descendant dbufs and the bonus dbuf
853 * from accessing the dnode, so that we can discount their holds. The
854 * handle is safe to access because we know that while the dnode cannot
855 * go away, neither can its handle. Once we hold dnh_zrlock, we can
856 * safely move any dnode referenced only by dbufs.
858 if (!zrl_tryenter(&odn->dn_handle->dnh_zrlock)) {
859 mutex_exit(&os->os_lock);
860 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_handle);
861 return (KMEM_CBRC_LATER);
865 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
866 * We need to guarantee that there is a hold for every dbuf in order to
867 * determine whether the dnode is actively referenced. Falsely matching
868 * a dbuf to an active hold would lead to an unsafe move. It's possible
869 * that a thread already having an active dnode hold is about to add a
870 * dbuf, and we can't compare hold and dbuf counts while the add is in
873 if (!rw_tryenter(&odn->dn_struct_rwlock, RW_WRITER)) {
874 zrl_exit(&odn->dn_handle->dnh_zrlock);
875 mutex_exit(&os->os_lock);
876 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_rwlock);
877 return (KMEM_CBRC_LATER);
881 * A dbuf may be removed (evicted) without an active dnode hold. In that
882 * case, the dbuf count is decremented under the handle lock before the
883 * dbuf's hold is released. This order ensures that if we count the hold
884 * after the dbuf is removed but before its hold is released, we will
885 * treat the unmatched hold as active and exit safely. If we count the
886 * hold before the dbuf is removed, the hold is discounted, and the
887 * removal is blocked until the move completes.
889 refcount = refcount_count(&odn->dn_holds);
890 ASSERT(refcount >= 0);
891 dbufs = odn->dn_dbufs_count;
893 /* We can't have more dbufs than dnode holds. */
894 ASSERT3U(dbufs, <=, refcount);
895 DTRACE_PROBE3(dnode__move, dnode_t *, odn, int64_t, refcount,
898 if (refcount > dbufs) {
899 rw_exit(&odn->dn_struct_rwlock);
900 zrl_exit(&odn->dn_handle->dnh_zrlock);
901 mutex_exit(&os->os_lock);
902 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_active);
903 return (KMEM_CBRC_LATER);
906 rw_exit(&odn->dn_struct_rwlock);
909 * At this point we know that anyone with a hold on the dnode is not
910 * actively referencing it. The dnode is known and in a valid state to
911 * move. We're holding the locks needed to execute the critical section.
913 dnode_move_impl(odn, ndn);
915 list_link_replace(&odn->dn_link, &ndn->dn_link);
916 /* If the dnode was safe to move, the refcount cannot have changed. */
917 ASSERT(refcount == refcount_count(&ndn->dn_holds));
918 ASSERT(dbufs == ndn->dn_dbufs_count);
919 zrl_exit(&ndn->dn_handle->dnh_zrlock); /* handle has moved */
920 mutex_exit(&os->os_lock);
922 return (KMEM_CBRC_YES);
928 dnode_special_close(dnode_handle_t *dnh)
930 dnode_t *dn = dnh->dnh_dnode;
933 * Wait for final references to the dnode to clear. This can
934 * only happen if the arc is asyncronously evicting state that
935 * has a hold on this dnode while we are trying to evict this
938 while (refcount_count(&dn->dn_holds) > 0)
940 zrl_add(&dnh->dnh_zrlock);
941 dnode_destroy(dn); /* implicit zrl_remove() */
942 zrl_destroy(&dnh->dnh_zrlock);
943 dnh->dnh_dnode = NULL;
947 dnode_special_open(objset_t *os, dnode_phys_t *dnp, uint64_t object,
950 dnode_t *dn = dnode_create(os, dnp, NULL, object, dnh);
952 zrl_init(&dnh->dnh_zrlock);
958 dnode_buf_pageout(dmu_buf_t *db, void *arg)
960 dnode_children_t *children_dnodes = arg;
962 int epb = db->db_size >> DNODE_SHIFT;
964 ASSERT(epb == children_dnodes->dnc_count);
966 for (i = 0; i < epb; i++) {
967 dnode_handle_t *dnh = &children_dnodes->dnc_children[i];
971 * The dnode handle lock guards against the dnode moving to
972 * another valid address, so there is no need here to guard
973 * against changes to or from NULL.
975 if (dnh->dnh_dnode == NULL) {
976 zrl_destroy(&dnh->dnh_zrlock);
980 zrl_add(&dnh->dnh_zrlock);
983 * If there are holds on this dnode, then there should
984 * be holds on the dnode's containing dbuf as well; thus
985 * it wouldn't be eligible for eviction and this function
986 * would not have been called.
988 ASSERT(refcount_is_zero(&dn->dn_holds));
989 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
991 dnode_destroy(dn); /* implicit zrl_remove() */
992 zrl_destroy(&dnh->dnh_zrlock);
993 dnh->dnh_dnode = NULL;
995 kmem_free(children_dnodes, sizeof (dnode_children_t) +
996 (epb - 1) * sizeof (dnode_handle_t));
1001 * EINVAL - invalid object number.
1003 * succeeds even for free dnodes.
1006 dnode_hold_impl(objset_t *os, uint64_t object, int flag,
1007 void *tag, dnode_t **dnp)
1010 int drop_struct_lock = FALSE;
1015 dnode_children_t *children_dnodes;
1016 dnode_handle_t *dnh;
1019 * If you are holding the spa config lock as writer, you shouldn't
1020 * be asking the DMU to do *anything* unless it's the root pool
1021 * which may require us to read from the root filesystem while
1022 * holding some (not all) of the locks as writer.
1024 ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0 ||
1025 (spa_is_root(os->os_spa) &&
1026 spa_config_held(os->os_spa, SCL_STATE, RW_WRITER)));
1028 if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT) {
1029 dn = (object == DMU_USERUSED_OBJECT) ?
1030 DMU_USERUSED_DNODE(os) : DMU_GROUPUSED_DNODE(os);
1034 if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE)
1036 if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)
1039 (void) refcount_add(&dn->dn_holds, tag);
1044 if (object == 0 || object >= DN_MAX_OBJECT)
1047 mdn = DMU_META_DNODE(os);
1048 ASSERT(mdn->dn_object == DMU_META_DNODE_OBJECT);
1052 if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
1053 rw_enter(&mdn->dn_struct_rwlock, RW_READER);
1054 drop_struct_lock = TRUE;
1057 blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t));
1059 db = dbuf_hold(mdn, blk, FTAG);
1060 if (drop_struct_lock)
1061 rw_exit(&mdn->dn_struct_rwlock);
1064 err = dbuf_read(db, NULL, DB_RF_CANFAIL);
1066 dbuf_rele(db, FTAG);
1070 ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
1071 epb = db->db.db_size >> DNODE_SHIFT;
1073 idx = object & (epb-1);
1075 ASSERT(DB_DNODE(db)->dn_type == DMU_OT_DNODE);
1076 children_dnodes = dmu_buf_get_user(&db->db);
1077 if (children_dnodes == NULL) {
1079 dnode_children_t *winner;
1080 children_dnodes = kmem_alloc(sizeof (dnode_children_t) +
1081 (epb - 1) * sizeof (dnode_handle_t), KM_SLEEP);
1082 children_dnodes->dnc_count = epb;
1083 dnh = &children_dnodes->dnc_children[0];
1084 for (i = 0; i < epb; i++) {
1085 zrl_init(&dnh[i].dnh_zrlock);
1086 dnh[i].dnh_dnode = NULL;
1088 if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL,
1089 dnode_buf_pageout)) {
1090 kmem_free(children_dnodes, sizeof (dnode_children_t) +
1091 (epb - 1) * sizeof (dnode_handle_t));
1092 children_dnodes = winner;
1095 ASSERT(children_dnodes->dnc_count == epb);
1097 dnh = &children_dnodes->dnc_children[idx];
1098 zrl_add(&dnh->dnh_zrlock);
1099 if ((dn = dnh->dnh_dnode) == NULL) {
1100 dnode_phys_t *phys = (dnode_phys_t *)db->db.db_data+idx;
1103 dn = dnode_create(os, phys, db, object, dnh);
1104 winner = atomic_cas_ptr(&dnh->dnh_dnode, NULL, dn);
1105 if (winner != NULL) {
1106 zrl_add(&dnh->dnh_zrlock);
1107 dnode_destroy(dn); /* implicit zrl_remove() */
1112 mutex_enter(&dn->dn_mtx);
1114 if (dn->dn_free_txg ||
1115 ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
1116 ((flag & DNODE_MUST_BE_FREE) &&
1117 (type != DMU_OT_NONE || !refcount_is_zero(&dn->dn_holds)))) {
1118 mutex_exit(&dn->dn_mtx);
1119 zrl_remove(&dnh->dnh_zrlock);
1120 dbuf_rele(db, FTAG);
1121 return (type == DMU_OT_NONE ? ENOENT : EEXIST);
1123 mutex_exit(&dn->dn_mtx);
1125 if (refcount_add(&dn->dn_holds, tag) == 1)
1126 dbuf_add_ref(db, dnh);
1127 /* Now we can rely on the hold to prevent the dnode from moving. */
1128 zrl_remove(&dnh->dnh_zrlock);
1131 ASSERT3P(dn->dn_dbuf, ==, db);
1132 ASSERT3U(dn->dn_object, ==, object);
1133 dbuf_rele(db, FTAG);
1140 * Return held dnode if the object is allocated, NULL if not.
1143 dnode_hold(objset_t *os, uint64_t object, void *tag, dnode_t **dnp)
1145 return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
1149 * Can only add a reference if there is already at least one
1150 * reference on the dnode. Returns FALSE if unable to add a
1154 dnode_add_ref(dnode_t *dn, void *tag)
1156 mutex_enter(&dn->dn_mtx);
1157 if (refcount_is_zero(&dn->dn_holds)) {
1158 mutex_exit(&dn->dn_mtx);
1161 VERIFY(1 < refcount_add(&dn->dn_holds, tag));
1162 mutex_exit(&dn->dn_mtx);
1167 dnode_rele(dnode_t *dn, void *tag)
1170 /* Get while the hold prevents the dnode from moving. */
1171 dmu_buf_impl_t *db = dn->dn_dbuf;
1172 dnode_handle_t *dnh = dn->dn_handle;
1174 mutex_enter(&dn->dn_mtx);
1175 refs = refcount_remove(&dn->dn_holds, tag);
1176 mutex_exit(&dn->dn_mtx);
1179 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1180 * indirectly by dbuf_rele() while relying on the dnode handle to
1181 * prevent the dnode from moving, since releasing the last hold could
1182 * result in the dnode's parent dbuf evicting its dnode handles. For
1183 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1184 * other direct or indirect hold on the dnode must first drop the dnode
1187 ASSERT(refs > 0 || dnh->dnh_zrlock.zr_owner != curthread);
1189 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1190 if (refs == 0 && db != NULL) {
1192 * Another thread could add a hold to the dnode handle in
1193 * dnode_hold_impl() while holding the parent dbuf. Since the
1194 * hold on the parent dbuf prevents the handle from being
1195 * destroyed, the hold on the handle is OK. We can't yet assert
1196 * that the handle has zero references, but that will be
1197 * asserted anyway when the handle gets destroyed.
1204 dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
1206 objset_t *os = dn->dn_objset;
1207 uint64_t txg = tx->tx_txg;
1209 if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
1210 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1217 mutex_enter(&dn->dn_mtx);
1218 ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
1219 ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg);
1220 mutex_exit(&dn->dn_mtx);
1224 * Determine old uid/gid when necessary
1226 dmu_objset_userquota_get_ids(dn, B_TRUE, tx);
1228 mutex_enter(&os->os_lock);
1231 * If we are already marked dirty, we're done.
1233 if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
1234 mutex_exit(&os->os_lock);
1238 ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs));
1239 ASSERT(dn->dn_datablksz != 0);
1240 ASSERT3U(dn->dn_next_bonuslen[txg&TXG_MASK], ==, 0);
1241 ASSERT3U(dn->dn_next_blksz[txg&TXG_MASK], ==, 0);
1242 ASSERT3U(dn->dn_next_bonustype[txg&TXG_MASK], ==, 0);
1244 dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
1245 dn->dn_object, txg);
1247 if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
1248 list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
1250 list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
1253 mutex_exit(&os->os_lock);
1256 * The dnode maintains a hold on its containing dbuf as
1257 * long as there are holds on it. Each instantiated child
1258 * dbuf maintains a hold on the dnode. When the last child
1259 * drops its hold, the dnode will drop its hold on the
1260 * containing dbuf. We add a "dirty hold" here so that the
1261 * dnode will hang around after we finish processing its
1264 VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
1266 (void) dbuf_dirty(dn->dn_dbuf, tx);
1268 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1272 dnode_free(dnode_t *dn, dmu_tx_t *tx)
1274 int txgoff = tx->tx_txg & TXG_MASK;
1276 dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
1278 /* we should be the only holder... hopefully */
1279 /* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
1281 mutex_enter(&dn->dn_mtx);
1282 if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
1283 mutex_exit(&dn->dn_mtx);
1286 dn->dn_free_txg = tx->tx_txg;
1287 mutex_exit(&dn->dn_mtx);
1290 * If the dnode is already dirty, it needs to be moved from
1291 * the dirty list to the free list.
1293 mutex_enter(&dn->dn_objset->os_lock);
1294 if (list_link_active(&dn->dn_dirty_link[txgoff])) {
1295 list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
1296 list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
1297 mutex_exit(&dn->dn_objset->os_lock);
1299 mutex_exit(&dn->dn_objset->os_lock);
1300 dnode_setdirty(dn, tx);
1305 * Try to change the block size for the indicated dnode. This can only
1306 * succeed if there are no blocks allocated or dirty beyond first block
1309 dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
1311 dmu_buf_impl_t *db, *db_next;
1315 size = SPA_MINBLOCKSIZE;
1316 if (size > SPA_MAXBLOCKSIZE)
1317 size = SPA_MAXBLOCKSIZE;
1319 size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
1321 if (ibs == dn->dn_indblkshift)
1324 if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
1327 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1329 /* Check for any allocated blocks beyond the first */
1330 if (dn->dn_phys->dn_maxblkid != 0)
1333 mutex_enter(&dn->dn_dbufs_mtx);
1334 for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
1335 db_next = list_next(&dn->dn_dbufs, db);
1337 if (db->db_blkid != 0 && db->db_blkid != DMU_BONUS_BLKID &&
1338 db->db_blkid != DMU_SPILL_BLKID) {
1339 mutex_exit(&dn->dn_dbufs_mtx);
1343 mutex_exit(&dn->dn_dbufs_mtx);
1345 if (ibs && dn->dn_nlevels != 1)
1348 /* resize the old block */
1349 err = dbuf_hold_impl(dn, 0, 0, TRUE, FTAG, &db);
1351 dbuf_new_size(db, size, tx);
1352 else if (err != ENOENT)
1355 dnode_setdblksz(dn, size);
1356 dnode_setdirty(dn, tx);
1357 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
1359 dn->dn_indblkshift = ibs;
1360 dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
1362 /* rele after we have fixed the blocksize in the dnode */
1364 dbuf_rele(db, FTAG);
1366 rw_exit(&dn->dn_struct_rwlock);
1370 rw_exit(&dn->dn_struct_rwlock);
1374 /* read-holding callers must not rely on the lock being continuously held */
1376 dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read)
1378 uint64_t txgoff = tx->tx_txg & TXG_MASK;
1379 int epbs, new_nlevels;
1382 ASSERT(blkid != DMU_BONUS_BLKID);
1385 RW_READ_HELD(&dn->dn_struct_rwlock) :
1386 RW_WRITE_HELD(&dn->dn_struct_rwlock));
1389 * if we have a read-lock, check to see if we need to do any work
1390 * before upgrading to a write-lock.
1393 if (blkid <= dn->dn_maxblkid)
1396 if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
1397 rw_exit(&dn->dn_struct_rwlock);
1398 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1402 if (blkid <= dn->dn_maxblkid)
1405 dn->dn_maxblkid = blkid;
1408 * Compute the number of levels necessary to support the new maxblkid.
1411 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1412 for (sz = dn->dn_nblkptr;
1413 sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
1416 if (new_nlevels > dn->dn_nlevels) {
1417 int old_nlevels = dn->dn_nlevels;
1420 dbuf_dirty_record_t *new, *dr, *dr_next;
1422 dn->dn_nlevels = new_nlevels;
1424 ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
1425 dn->dn_next_nlevels[txgoff] = new_nlevels;
1427 /* dirty the left indirects */
1428 db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
1430 new = dbuf_dirty(db, tx);
1431 dbuf_rele(db, FTAG);
1433 /* transfer the dirty records to the new indirect */
1434 mutex_enter(&dn->dn_mtx);
1435 mutex_enter(&new->dt.di.dr_mtx);
1436 list = &dn->dn_dirty_records[txgoff];
1437 for (dr = list_head(list); dr; dr = dr_next) {
1438 dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
1439 if (dr->dr_dbuf->db_level != new_nlevels-1 &&
1440 dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
1441 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
1442 ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
1443 list_remove(&dn->dn_dirty_records[txgoff], dr);
1444 list_insert_tail(&new->dt.di.dr_children, dr);
1445 dr->dr_parent = new;
1448 mutex_exit(&new->dt.di.dr_mtx);
1449 mutex_exit(&dn->dn_mtx);
1454 rw_downgrade(&dn->dn_struct_rwlock);
1458 dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
1460 avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1463 free_range_t rp_tofind;
1464 uint64_t endblk = blkid + nblks;
1466 ASSERT(MUTEX_HELD(&dn->dn_mtx));
1467 ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */
1469 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1470 blkid, nblks, tx->tx_txg);
1471 rp_tofind.fr_blkid = blkid;
1472 rp = avl_find(tree, &rp_tofind, &where);
1474 rp = avl_nearest(tree, where, AVL_BEFORE);
1476 rp = avl_nearest(tree, where, AVL_AFTER);
1478 while (rp && (rp->fr_blkid <= blkid + nblks)) {
1479 uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks;
1480 free_range_t *nrp = AVL_NEXT(tree, rp);
1482 if (blkid <= rp->fr_blkid && endblk >= fr_endblk) {
1483 /* clear this entire range */
1484 avl_remove(tree, rp);
1485 kmem_free(rp, sizeof (free_range_t));
1486 } else if (blkid <= rp->fr_blkid &&
1487 endblk > rp->fr_blkid && endblk < fr_endblk) {
1488 /* clear the beginning of this range */
1489 rp->fr_blkid = endblk;
1490 rp->fr_nblks = fr_endblk - endblk;
1491 } else if (blkid > rp->fr_blkid && blkid < fr_endblk &&
1492 endblk >= fr_endblk) {
1493 /* clear the end of this range */
1494 rp->fr_nblks = blkid - rp->fr_blkid;
1495 } else if (blkid > rp->fr_blkid && endblk < fr_endblk) {
1496 /* clear a chunk out of this range */
1497 free_range_t *new_rp =
1498 kmem_alloc(sizeof (free_range_t), KM_SLEEP);
1500 new_rp->fr_blkid = endblk;
1501 new_rp->fr_nblks = fr_endblk - endblk;
1502 avl_insert_here(tree, new_rp, rp, AVL_AFTER);
1503 rp->fr_nblks = blkid - rp->fr_blkid;
1505 /* there may be no overlap */
1511 dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
1514 uint64_t blkoff, blkid, nblks;
1515 int blksz, blkshift, head, tail;
1519 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1520 blksz = dn->dn_datablksz;
1521 blkshift = dn->dn_datablkshift;
1522 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1525 len = UINT64_MAX - off;
1530 * First, block align the region to free:
1533 head = P2NPHASE(off, blksz);
1534 blkoff = P2PHASE(off, blksz);
1535 if ((off >> blkshift) > dn->dn_maxblkid)
1538 ASSERT(dn->dn_maxblkid == 0);
1539 if (off == 0 && len >= blksz) {
1540 /* Freeing the whole block; fast-track this request */
1544 } else if (off >= blksz) {
1545 /* Freeing past end-of-data */
1548 /* Freeing part of the block. */
1550 ASSERT3U(head, >, 0);
1554 /* zero out any partial block data at the start of the range */
1556 ASSERT3U(blkoff + head, ==, blksz);
1559 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE,
1563 /* don't dirty if it isn't on disk and isn't dirty */
1564 if (db->db_last_dirty ||
1565 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1566 rw_exit(&dn->dn_struct_rwlock);
1567 dbuf_will_dirty(db, tx);
1568 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1569 data = db->db.db_data;
1570 bzero(data + blkoff, head);
1572 dbuf_rele(db, FTAG);
1578 /* If the range was less than one block, we're done */
1582 /* If the remaining range is past end of file, we're done */
1583 if ((off >> blkshift) > dn->dn_maxblkid)
1586 ASSERT(ISP2(blksz));
1590 tail = P2PHASE(len, blksz);
1592 ASSERT3U(P2PHASE(off, blksz), ==, 0);
1593 /* zero out any partial block data at the end of the range */
1597 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
1598 TRUE, FTAG, &db) == 0) {
1599 /* don't dirty if not on disk and not dirty */
1600 if (db->db_last_dirty ||
1601 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1602 rw_exit(&dn->dn_struct_rwlock);
1603 dbuf_will_dirty(db, tx);
1604 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1605 bzero(db->db.db_data, tail);
1607 dbuf_rele(db, FTAG);
1612 /* If the range did not include a full block, we are done */
1616 ASSERT(IS_P2ALIGNED(off, blksz));
1617 ASSERT(trunc || IS_P2ALIGNED(len, blksz));
1618 blkid = off >> blkshift;
1619 nblks = len >> blkshift;
1624 * Read in and mark all the level-1 indirects dirty,
1625 * so that they will stay in memory until syncing phase.
1626 * Always dirty the first and last indirect to make sure
1627 * we dirty all the partial indirects.
1629 if (dn->dn_nlevels > 1) {
1630 uint64_t i, first, last;
1631 int shift = epbs + dn->dn_datablkshift;
1633 first = blkid >> epbs;
1634 if (db = dbuf_hold_level(dn, 1, first, FTAG)) {
1635 dbuf_will_dirty(db, tx);
1636 dbuf_rele(db, FTAG);
1639 last = dn->dn_maxblkid >> epbs;
1641 last = (blkid + nblks - 1) >> epbs;
1642 if (last > first && (db = dbuf_hold_level(dn, 1, last, FTAG))) {
1643 dbuf_will_dirty(db, tx);
1644 dbuf_rele(db, FTAG);
1646 for (i = first + 1; i < last; i++) {
1647 uint64_t ibyte = i << shift;
1650 err = dnode_next_offset(dn,
1651 DNODE_FIND_HAVELOCK, &ibyte, 1, 1, 0);
1653 if (err == ESRCH || i >= last)
1656 db = dbuf_hold_level(dn, 1, i, FTAG);
1658 dbuf_will_dirty(db, tx);
1659 dbuf_rele(db, FTAG);
1665 * Add this range to the dnode range list.
1666 * We will finish up this free operation in the syncing phase.
1668 mutex_enter(&dn->dn_mtx);
1669 dnode_clear_range(dn, blkid, nblks, tx);
1671 free_range_t *rp, *found;
1673 avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1675 /* Add new range to dn_ranges */
1676 rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP);
1677 rp->fr_blkid = blkid;
1678 rp->fr_nblks = nblks;
1679 found = avl_find(tree, rp, &where);
1680 ASSERT(found == NULL);
1681 avl_insert(tree, rp, where);
1682 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1683 blkid, nblks, tx->tx_txg);
1685 mutex_exit(&dn->dn_mtx);
1687 dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
1688 dnode_setdirty(dn, tx);
1690 if (trunc && dn->dn_maxblkid >= (off >> blkshift))
1691 dn->dn_maxblkid = (off >> blkshift ? (off >> blkshift) - 1 : 0);
1693 rw_exit(&dn->dn_struct_rwlock);
1697 dnode_spill_freed(dnode_t *dn)
1701 mutex_enter(&dn->dn_mtx);
1702 for (i = 0; i < TXG_SIZE; i++) {
1703 if (dn->dn_rm_spillblk[i] == DN_KILL_SPILLBLK)
1706 mutex_exit(&dn->dn_mtx);
1707 return (i < TXG_SIZE);
1710 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1712 dnode_block_freed(dnode_t *dn, uint64_t blkid)
1714 free_range_t range_tofind;
1715 void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1718 if (blkid == DMU_BONUS_BLKID)
1722 * If we're in the process of opening the pool, dp will not be
1723 * set yet, but there shouldn't be anything dirty.
1728 if (dn->dn_free_txg)
1731 if (blkid == DMU_SPILL_BLKID)
1732 return (dnode_spill_freed(dn));
1734 range_tofind.fr_blkid = blkid;
1735 mutex_enter(&dn->dn_mtx);
1736 for (i = 0; i < TXG_SIZE; i++) {
1737 free_range_t *range_found;
1740 range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx);
1742 ASSERT(range_found->fr_nblks > 0);
1745 range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE);
1747 range_found->fr_blkid + range_found->fr_nblks > blkid)
1750 mutex_exit(&dn->dn_mtx);
1751 return (i < TXG_SIZE);
1754 /* call from syncing context when we actually write/free space for this dnode */
1756 dnode_diduse_space(dnode_t *dn, int64_t delta)
1759 dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
1761 (u_longlong_t)dn->dn_phys->dn_used,
1764 mutex_enter(&dn->dn_mtx);
1765 space = DN_USED_BYTES(dn->dn_phys);
1767 ASSERT3U(space + delta, >=, space); /* no overflow */
1769 ASSERT3U(space, >=, -delta); /* no underflow */
1772 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
1773 ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
1774 ASSERT3U(P2PHASE(space, 1<<DEV_BSHIFT), ==, 0);
1775 dn->dn_phys->dn_used = space >> DEV_BSHIFT;
1777 dn->dn_phys->dn_used = space;
1778 dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
1780 mutex_exit(&dn->dn_mtx);
1784 * Call when we think we're going to write/free space in open context.
1785 * Be conservative (ie. OK to write less than this or free more than
1786 * this, but don't write more or free less).
1789 dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1791 objset_t *os = dn->dn_objset;
1792 dsl_dataset_t *ds = os->os_dsl_dataset;
1795 space = spa_get_asize(os->os_spa, space);
1798 dsl_dir_willuse_space(ds->ds_dir, space, tx);
1800 dmu_tx_willuse_space(tx, space);
1804 * This function scans a block at the indicated "level" looking for
1805 * a hole or data (depending on 'flags'). If level > 0, then we are
1806 * scanning an indirect block looking at its pointers. If level == 0,
1807 * then we are looking at a block of dnodes. If we don't find what we
1808 * are looking for in the block, we return ESRCH. Otherwise, return
1809 * with *offset pointing to the beginning (if searching forwards) or
1810 * end (if searching backwards) of the range covered by the block
1811 * pointer we matched on (or dnode).
1813 * The basic search algorithm used below by dnode_next_offset() is to
1814 * use this function to search up the block tree (widen the search) until
1815 * we find something (i.e., we don't return ESRCH) and then search back
1816 * down the tree (narrow the search) until we reach our original search
1820 dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
1821 int lvl, uint64_t blkfill, uint64_t txg)
1823 dmu_buf_impl_t *db = NULL;
1825 uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1826 uint64_t epb = 1ULL << epbs;
1827 uint64_t minfill, maxfill;
1829 int i, inc, error, span;
1831 dprintf("probing object %llu offset %llx level %d of %u\n",
1832 dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
1834 hole = ((flags & DNODE_FIND_HOLE) != 0);
1835 inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1;
1836 ASSERT(txg == 0 || !hole);
1838 if (lvl == dn->dn_phys->dn_nlevels) {
1840 epb = dn->dn_phys->dn_nblkptr;
1841 data = dn->dn_phys->dn_blkptr;
1843 uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl);
1844 error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db);
1846 if (error != ENOENT)
1851 * This can only happen when we are searching up
1852 * the block tree for data. We don't really need to
1853 * adjust the offset, as we will just end up looking
1854 * at the pointer to this block in its parent, and its
1855 * going to be unallocated, so we will skip over it.
1859 error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT);
1861 dbuf_rele(db, FTAG);
1864 data = db->db.db_data;
1868 (db->db_blkptr == NULL || db->db_blkptr->blk_birth <= txg)) {
1870 * This can only happen when we are searching up the tree
1871 * and these conditions mean that we need to keep climbing.
1874 } else if (lvl == 0) {
1875 dnode_phys_t *dnp = data;
1877 ASSERT(dn->dn_type == DMU_OT_DNODE);
1879 for (i = (*offset >> span) & (blkfill - 1);
1880 i >= 0 && i < blkfill; i += inc) {
1881 if ((dnp[i].dn_type == DMU_OT_NONE) == hole)
1883 *offset += (1ULL << span) * inc;
1885 if (i < 0 || i == blkfill)
1888 blkptr_t *bp = data;
1889 uint64_t start = *offset;
1890 span = (lvl - 1) * epbs + dn->dn_datablkshift;
1892 maxfill = blkfill << ((lvl - 1) * epbs);
1899 *offset = *offset >> span;
1900 for (i = BF64_GET(*offset, 0, epbs);
1901 i >= 0 && i < epb; i += inc) {
1902 if (bp[i].blk_fill >= minfill &&
1903 bp[i].blk_fill <= maxfill &&
1904 (hole || bp[i].blk_birth > txg))
1906 if (inc > 0 || *offset > 0)
1909 *offset = *offset << span;
1911 /* traversing backwards; position offset at the end */
1912 ASSERT3U(*offset, <=, start);
1913 *offset = MIN(*offset + (1ULL << span) - 1, start);
1914 } else if (*offset < start) {
1917 if (i < 0 || i >= epb)
1922 dbuf_rele(db, FTAG);
1928 * Find the next hole, data, or sparse region at or after *offset.
1929 * The value 'blkfill' tells us how many items we expect to find
1930 * in an L0 data block; this value is 1 for normal objects,
1931 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1932 * DNODES_PER_BLOCK when searching for sparse regions thereof.
1936 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
1937 * Finds the next/previous hole/data in a file.
1938 * Used in dmu_offset_next().
1940 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
1941 * Finds the next free/allocated dnode an objset's meta-dnode.
1942 * Only finds objects that have new contents since txg (ie.
1943 * bonus buffer changes and content removal are ignored).
1944 * Used in dmu_object_next().
1946 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
1947 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
1948 * Used in dmu_object_alloc().
1951 dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
1952 int minlvl, uint64_t blkfill, uint64_t txg)
1954 uint64_t initial_offset = *offset;
1958 if (!(flags & DNODE_FIND_HAVELOCK))
1959 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1961 if (dn->dn_phys->dn_nlevels == 0) {
1966 if (dn->dn_datablkshift == 0) {
1967 if (*offset < dn->dn_datablksz) {
1968 if (flags & DNODE_FIND_HOLE)
1969 *offset = dn->dn_datablksz;
1976 maxlvl = dn->dn_phys->dn_nlevels;
1978 for (lvl = minlvl; lvl <= maxlvl; lvl++) {
1979 error = dnode_next_offset_level(dn,
1980 flags, offset, lvl, blkfill, txg);
1985 while (error == 0 && --lvl >= minlvl) {
1986 error = dnode_next_offset_level(dn,
1987 flags, offset, lvl, blkfill, txg);
1990 if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
1991 initial_offset < *offset : initial_offset > *offset))
1994 if (!(flags & DNODE_FIND_HAVELOCK))
1995 rw_exit(&dn->dn_struct_rwlock);