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 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "@(#)dnode.c 1.20 07/08/26 SMI"
28 #include <sys/zfs_context.h>
30 #include <sys/dnode.h>
32 #include <sys/dmu_impl.h>
33 #include <sys/dmu_tx.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/dsl_dir.h>
36 #include <sys/dsl_dataset.h>
39 #include <sys/dmu_zfetch.h>
41 static int free_range_compar(const void *node1, const void *node2);
43 static kmem_cache_t *dnode_cache;
45 static dnode_phys_t dnode_phys_zero;
47 int zfs_default_bs = SPA_MINBLOCKSHIFT;
48 int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
52 dnode_cons(void *arg, void *unused, int kmflag)
56 bzero(dn, sizeof (dnode_t));
58 rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL);
59 mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
60 mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
61 refcount_create(&dn->dn_holds);
62 refcount_create(&dn->dn_tx_holds);
64 for (i = 0; i < TXG_SIZE; i++) {
65 avl_create(&dn->dn_ranges[i], free_range_compar,
66 sizeof (free_range_t),
67 offsetof(struct free_range, fr_node));
68 list_create(&dn->dn_dirty_records[i],
69 sizeof (dbuf_dirty_record_t),
70 offsetof(dbuf_dirty_record_t, dr_dirty_node));
73 list_create(&dn->dn_dbufs, sizeof (dmu_buf_impl_t),
74 offsetof(dmu_buf_impl_t, db_link));
81 dnode_dest(void *arg, void *unused)
86 rw_destroy(&dn->dn_struct_rwlock);
87 mutex_destroy(&dn->dn_mtx);
88 mutex_destroy(&dn->dn_dbufs_mtx);
89 refcount_destroy(&dn->dn_holds);
90 refcount_destroy(&dn->dn_tx_holds);
92 for (i = 0; i < TXG_SIZE; i++) {
93 avl_destroy(&dn->dn_ranges[i]);
94 list_destroy(&dn->dn_dirty_records[i]);
97 list_destroy(&dn->dn_dbufs);
103 dnode_cache = kmem_cache_create("dnode_t",
105 0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
111 kmem_cache_destroy(dnode_cache);
117 dnode_verify(dnode_t *dn)
119 int drop_struct_lock = FALSE;
122 ASSERT(dn->dn_objset);
124 ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
126 if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
129 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
130 rw_enter(&dn->dn_struct_rwlock, RW_READER);
131 drop_struct_lock = TRUE;
133 if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
135 ASSERT3U(dn->dn_indblkshift, >=, 0);
136 ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
137 if (dn->dn_datablkshift) {
138 ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
139 ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
140 ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
142 ASSERT3U(dn->dn_nlevels, <=, 30);
143 ASSERT3U(dn->dn_type, <=, DMU_OT_NUMTYPES);
144 ASSERT3U(dn->dn_nblkptr, >=, 1);
145 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
146 ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
147 ASSERT3U(dn->dn_datablksz, ==,
148 dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
149 ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
150 ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
151 dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
152 for (i = 0; i < TXG_SIZE; i++) {
153 ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
156 if (dn->dn_phys->dn_type != DMU_OT_NONE)
157 ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
158 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT || dn->dn_dbuf != NULL);
159 if (dn->dn_dbuf != NULL) {
160 ASSERT3P(dn->dn_phys, ==,
161 (dnode_phys_t *)dn->dn_dbuf->db.db_data +
162 (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
164 if (drop_struct_lock)
165 rw_exit(&dn->dn_struct_rwlock);
170 dnode_byteswap(dnode_phys_t *dnp)
172 uint64_t *buf64 = (void*)&dnp->dn_blkptr;
175 if (dnp->dn_type == DMU_OT_NONE) {
176 bzero(dnp, sizeof (dnode_phys_t));
180 dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
181 dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
182 dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
183 dnp->dn_used = BSWAP_64(dnp->dn_used);
186 * dn_nblkptr is only one byte, so it's OK to read it in either
187 * byte order. We can't read dn_bouslen.
189 ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
190 ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
191 for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
192 buf64[i] = BSWAP_64(buf64[i]);
195 * OK to check dn_bonuslen for zero, because it won't matter if
196 * we have the wrong byte order. This is necessary because the
197 * dnode dnode is smaller than a regular dnode.
199 if (dnp->dn_bonuslen != 0) {
201 * Note that the bonus length calculated here may be
202 * longer than the actual bonus buffer. This is because
203 * we always put the bonus buffer after the last block
204 * pointer (instead of packing it against the end of the
207 int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
208 size_t len = DN_MAX_BONUSLEN - off;
209 ASSERT3U(dnp->dn_bonustype, <, DMU_OT_NUMTYPES);
210 dmu_ot[dnp->dn_bonustype].ot_byteswap(dnp->dn_bonus + off, len);
215 dnode_buf_byteswap(void *vbuf, size_t size)
217 dnode_phys_t *buf = vbuf;
220 ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
221 ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
223 size >>= DNODE_SHIFT;
224 for (i = 0; i < size; i++) {
231 free_range_compar(const void *node1, const void *node2)
233 const free_range_t *rp1 = node1;
234 const free_range_t *rp2 = node2;
236 if (rp1->fr_blkid < rp2->fr_blkid)
238 else if (rp1->fr_blkid > rp2->fr_blkid)
244 dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx)
246 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
248 dnode_setdirty(dn, tx);
249 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
250 ASSERT3U(newsize, <=, DN_MAX_BONUSLEN -
251 (dn->dn_nblkptr-1) * sizeof (blkptr_t));
252 dn->dn_bonuslen = newsize;
254 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN;
256 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
257 rw_exit(&dn->dn_struct_rwlock);
261 dnode_setdblksz(dnode_t *dn, int size)
263 ASSERT3U(P2PHASE(size, SPA_MINBLOCKSIZE), ==, 0);
264 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
265 ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
266 ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
267 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
268 dn->dn_datablksz = size;
269 dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
270 dn->dn_datablkshift = ISP2(size) ? highbit(size - 1) : 0;
274 dnode_create(objset_impl_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
277 dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
278 (void) dnode_cons(dn, NULL, 0); /* XXX */
281 dn->dn_object = object;
285 if (dnp->dn_datablkszsec)
286 dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
287 dn->dn_indblkshift = dnp->dn_indblkshift;
288 dn->dn_nlevels = dnp->dn_nlevels;
289 dn->dn_type = dnp->dn_type;
290 dn->dn_nblkptr = dnp->dn_nblkptr;
291 dn->dn_checksum = dnp->dn_checksum;
292 dn->dn_compress = dnp->dn_compress;
293 dn->dn_bonustype = dnp->dn_bonustype;
294 dn->dn_bonuslen = dnp->dn_bonuslen;
295 dn->dn_maxblkid = dnp->dn_maxblkid;
297 dmu_zfetch_init(&dn->dn_zfetch, dn);
299 ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
300 mutex_enter(&os->os_lock);
301 list_insert_head(&os->os_dnodes, dn);
302 mutex_exit(&os->os_lock);
304 arc_space_consume(sizeof (dnode_t));
309 dnode_destroy(dnode_t *dn)
311 objset_impl_t *os = dn->dn_objset;
316 for (i = 0; i < TXG_SIZE; i++) {
317 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
318 ASSERT(NULL == list_head(&dn->dn_dirty_records[i]));
319 ASSERT(0 == avl_numnodes(&dn->dn_ranges[i]));
321 ASSERT(NULL == list_head(&dn->dn_dbufs));
324 mutex_enter(&os->os_lock);
325 list_remove(&os->os_dnodes, dn);
326 mutex_exit(&os->os_lock);
328 if (dn->dn_dirtyctx_firstset) {
329 kmem_free(dn->dn_dirtyctx_firstset, 1);
330 dn->dn_dirtyctx_firstset = NULL;
332 dmu_zfetch_rele(&dn->dn_zfetch);
334 mutex_enter(&dn->dn_bonus->db_mtx);
335 dbuf_evict(dn->dn_bonus);
338 kmem_cache_free(dnode_cache, dn);
339 arc_space_return(sizeof (dnode_t));
343 dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
344 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
349 blocksize = 1 << zfs_default_bs;
350 else if (blocksize > SPA_MAXBLOCKSIZE)
351 blocksize = SPA_MAXBLOCKSIZE;
353 blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
356 ibs = zfs_default_ibs;
358 ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
360 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
361 dn->dn_object, tx->tx_txg, blocksize, ibs);
363 ASSERT(dn->dn_type == DMU_OT_NONE);
364 ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
365 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
366 ASSERT(ot != DMU_OT_NONE);
367 ASSERT3U(ot, <, DMU_OT_NUMTYPES);
368 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
369 (bonustype != DMU_OT_NONE && bonuslen != 0));
370 ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
371 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
372 ASSERT(dn->dn_type == DMU_OT_NONE);
373 ASSERT3U(dn->dn_maxblkid, ==, 0);
374 ASSERT3U(dn->dn_allocated_txg, ==, 0);
375 ASSERT3U(dn->dn_assigned_txg, ==, 0);
376 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
377 ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
378 ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
380 for (i = 0; i < TXG_SIZE; i++) {
381 ASSERT3U(dn->dn_next_nlevels[i], ==, 0);
382 ASSERT3U(dn->dn_next_indblkshift[i], ==, 0);
383 ASSERT3U(dn->dn_next_bonuslen[i], ==, 0);
384 ASSERT3U(dn->dn_next_blksz[i], ==, 0);
385 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
386 ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
387 ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0);
391 dnode_setdblksz(dn, blocksize);
392 dn->dn_indblkshift = ibs;
394 dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
395 dn->dn_bonustype = bonustype;
396 dn->dn_bonuslen = bonuslen;
397 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
398 dn->dn_compress = ZIO_COMPRESS_INHERIT;
402 if (dn->dn_dirtyctx_firstset) {
403 kmem_free(dn->dn_dirtyctx_firstset, 1);
404 dn->dn_dirtyctx_firstset = NULL;
407 dn->dn_allocated_txg = tx->tx_txg;
409 dnode_setdirty(dn, tx);
410 dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
411 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
412 dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
416 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
417 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
420 dmu_buf_impl_t *db = NULL;
422 ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
423 ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE);
424 ASSERT3U(blocksize % SPA_MINBLOCKSIZE, ==, 0);
425 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
426 ASSERT(tx->tx_txg != 0);
427 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
428 (bonustype != DMU_OT_NONE && bonuslen != 0));
429 ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
430 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
432 for (i = 0; i < TXG_SIZE; i++)
433 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
435 /* clean up any unreferenced dbufs */
436 dnode_evict_dbufs(dn);
437 ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
440 * XXX I should really have a generation number to tell if we
443 if (blocksize != dn->dn_datablksz ||
444 dn->dn_bonustype != bonustype || dn->dn_bonuslen != bonuslen) {
445 /* free all old data */
446 dnode_free_range(dn, 0, -1ULL, tx);
449 /* change blocksize */
450 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
451 if (blocksize != dn->dn_datablksz &&
452 (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
453 list_head(&dn->dn_dbufs) != NULL)) {
454 db = dbuf_hold(dn, 0, FTAG);
455 dbuf_new_size(db, blocksize, tx);
457 dnode_setdblksz(dn, blocksize);
458 dnode_setdirty(dn, tx);
459 dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen;
460 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
461 rw_exit(&dn->dn_struct_rwlock);
468 /* change bonus size and type */
469 mutex_enter(&dn->dn_mtx);
470 old_nblkptr = dn->dn_nblkptr;
471 dn->dn_bonustype = bonustype;
472 dn->dn_bonuslen = bonuslen;
473 dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
474 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
475 dn->dn_compress = ZIO_COMPRESS_INHERIT;
476 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
478 /* XXX - for now, we can't make nblkptr smaller */
479 ASSERT3U(dn->dn_nblkptr, >=, old_nblkptr);
481 /* fix up the bonus db_size if dn_nblkptr has changed */
482 if (dn->dn_bonus && dn->dn_bonuslen != old_nblkptr) {
483 dn->dn_bonus->db.db_size =
484 DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t);
485 ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
488 dn->dn_allocated_txg = tx->tx_txg;
489 mutex_exit(&dn->dn_mtx);
493 dnode_special_close(dnode_t *dn)
496 * Wait for final references to the dnode to clear. This can
497 * only happen if the arc is asyncronously evicting state that
498 * has a hold on this dnode while we are trying to evict this
501 while (refcount_count(&dn->dn_holds) > 0)
507 dnode_special_open(objset_impl_t *os, dnode_phys_t *dnp, uint64_t object)
509 dnode_t *dn = dnode_create(os, dnp, NULL, object);
515 dnode_buf_pageout(dmu_buf_t *db, void *arg)
517 dnode_t **children_dnodes = arg;
519 int epb = db->db_size >> DNODE_SHIFT;
521 for (i = 0; i < epb; i++) {
522 dnode_t *dn = children_dnodes[i];
529 * If there are holds on this dnode, then there should
530 * be holds on the dnode's containing dbuf as well; thus
531 * it wouldn't be eligable for eviction and this function
532 * would not have been called.
534 ASSERT(refcount_is_zero(&dn->dn_holds));
535 ASSERT(list_head(&dn->dn_dbufs) == NULL);
536 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
538 for (n = 0; n < TXG_SIZE; n++)
539 ASSERT(!list_link_active(&dn->dn_dirty_link[n]));
541 children_dnodes[i] = NULL;
544 kmem_free(children_dnodes, epb * sizeof (dnode_t *));
549 * EINVAL - invalid object number.
551 * succeeds even for free dnodes.
554 dnode_hold_impl(objset_impl_t *os, uint64_t object, int flag,
555 void *tag, dnode_t **dnp)
558 int drop_struct_lock = FALSE;
563 dnode_t **children_dnodes;
565 if (object == 0 || object >= DN_MAX_OBJECT)
568 mdn = os->os_meta_dnode;
572 if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
573 rw_enter(&mdn->dn_struct_rwlock, RW_READER);
574 drop_struct_lock = TRUE;
577 blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t));
579 db = dbuf_hold(mdn, blk, FTAG);
580 if (drop_struct_lock)
581 rw_exit(&mdn->dn_struct_rwlock);
584 err = dbuf_read(db, NULL, DB_RF_CANFAIL);
590 ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
591 epb = db->db.db_size >> DNODE_SHIFT;
593 idx = object & (epb-1);
595 children_dnodes = dmu_buf_get_user(&db->db);
596 if (children_dnodes == NULL) {
598 children_dnodes = kmem_zalloc(epb * sizeof (dnode_t *),
600 if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL,
601 dnode_buf_pageout)) {
602 kmem_free(children_dnodes, epb * sizeof (dnode_t *));
603 children_dnodes = winner;
607 if ((dn = children_dnodes[idx]) == NULL) {
608 dnode_phys_t *dnp = (dnode_phys_t *)db->db.db_data+idx;
611 dn = dnode_create(os, dnp, db, object);
612 winner = atomic_cas_ptr(&children_dnodes[idx], NULL, dn);
613 if (winner != NULL) {
619 mutex_enter(&dn->dn_mtx);
621 if (dn->dn_free_txg ||
622 ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
623 ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)) {
624 mutex_exit(&dn->dn_mtx);
626 return (type == DMU_OT_NONE ? ENOENT : EEXIST);
628 mutex_exit(&dn->dn_mtx);
630 if (refcount_add(&dn->dn_holds, tag) == 1)
631 dbuf_add_ref(db, dn);
634 ASSERT3P(dn->dn_dbuf, ==, db);
635 ASSERT3U(dn->dn_object, ==, object);
643 * Return held dnode if the object is allocated, NULL if not.
646 dnode_hold(objset_impl_t *os, uint64_t object, void *tag, dnode_t **dnp)
648 return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
652 * Can only add a reference if there is already at least one
653 * reference on the dnode. Returns FALSE if unable to add a
657 dnode_add_ref(dnode_t *dn, void *tag)
659 mutex_enter(&dn->dn_mtx);
660 if (refcount_is_zero(&dn->dn_holds)) {
661 mutex_exit(&dn->dn_mtx);
664 VERIFY(1 < refcount_add(&dn->dn_holds, tag));
665 mutex_exit(&dn->dn_mtx);
670 dnode_rele(dnode_t *dn, void *tag)
674 mutex_enter(&dn->dn_mtx);
675 refs = refcount_remove(&dn->dn_holds, tag);
676 mutex_exit(&dn->dn_mtx);
677 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
678 if (refs == 0 && dn->dn_dbuf)
679 dbuf_rele(dn->dn_dbuf, dn);
683 dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
685 objset_impl_t *os = dn->dn_objset;
686 uint64_t txg = tx->tx_txg;
688 if (dn->dn_object == DMU_META_DNODE_OBJECT)
694 mutex_enter(&dn->dn_mtx);
695 ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
696 /* ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg); */
697 mutex_exit(&dn->dn_mtx);
700 mutex_enter(&os->os_lock);
703 * If we are already marked dirty, we're done.
705 if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
706 mutex_exit(&os->os_lock);
710 ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs));
711 ASSERT(dn->dn_datablksz != 0);
712 ASSERT3U(dn->dn_next_bonuslen[txg&TXG_MASK], ==, 0);
713 ASSERT3U(dn->dn_next_blksz[txg&TXG_MASK], ==, 0);
715 dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
718 if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
719 list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
721 list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
724 mutex_exit(&os->os_lock);
727 * The dnode maintains a hold on its containing dbuf as
728 * long as there are holds on it. Each instantiated child
729 * dbuf maintaines a hold on the dnode. When the last child
730 * drops its hold, the dnode will drop its hold on the
731 * containing dbuf. We add a "dirty hold" here so that the
732 * dnode will hang around after we finish processing its
735 VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
737 (void) dbuf_dirty(dn->dn_dbuf, tx);
739 dsl_dataset_dirty(os->os_dsl_dataset, tx);
743 dnode_free(dnode_t *dn, dmu_tx_t *tx)
745 int txgoff = tx->tx_txg & TXG_MASK;
747 dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
749 /* we should be the only holder... hopefully */
750 /* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
752 mutex_enter(&dn->dn_mtx);
753 if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
754 mutex_exit(&dn->dn_mtx);
757 dn->dn_free_txg = tx->tx_txg;
758 mutex_exit(&dn->dn_mtx);
761 * If the dnode is already dirty, it needs to be moved from
762 * the dirty list to the free list.
764 mutex_enter(&dn->dn_objset->os_lock);
765 if (list_link_active(&dn->dn_dirty_link[txgoff])) {
766 list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
767 list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
768 mutex_exit(&dn->dn_objset->os_lock);
770 mutex_exit(&dn->dn_objset->os_lock);
771 dnode_setdirty(dn, tx);
776 * Try to change the block size for the indicated dnode. This can only
777 * succeed if there are no blocks allocated or dirty beyond first block
780 dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
782 dmu_buf_impl_t *db, *db_next;
783 int have_db0 = FALSE;
786 size = SPA_MINBLOCKSIZE;
787 if (size > SPA_MAXBLOCKSIZE)
788 size = SPA_MAXBLOCKSIZE;
790 size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
792 if (ibs == dn->dn_indblkshift)
795 if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
798 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
800 /* Check for any allocated blocks beyond the first */
801 if (dn->dn_phys->dn_maxblkid != 0)
804 mutex_enter(&dn->dn_dbufs_mtx);
805 for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
806 db_next = list_next(&dn->dn_dbufs, db);
808 if (db->db_blkid == 0) {
810 } else if (db->db_blkid != DB_BONUS_BLKID) {
811 mutex_exit(&dn->dn_dbufs_mtx);
815 mutex_exit(&dn->dn_dbufs_mtx);
817 if (ibs && dn->dn_nlevels != 1)
821 if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) || have_db0) {
822 /* obtain the old block */
823 db = dbuf_hold(dn, 0, FTAG);
824 dbuf_new_size(db, size, tx);
827 dnode_setdblksz(dn, size);
828 dnode_setdirty(dn, tx);
829 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
831 dn->dn_indblkshift = ibs;
832 dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
838 rw_exit(&dn->dn_struct_rwlock);
842 rw_exit(&dn->dn_struct_rwlock);
847 dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx)
849 uint64_t txgoff = tx->tx_txg & TXG_MASK;
850 int drop_struct_lock = FALSE;
851 int epbs, new_nlevels;
854 ASSERT(blkid != DB_BONUS_BLKID);
856 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
857 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
858 drop_struct_lock = TRUE;
861 if (blkid <= dn->dn_maxblkid)
864 dn->dn_maxblkid = blkid;
867 * Compute the number of levels necessary to support the new maxblkid.
870 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
871 for (sz = dn->dn_nblkptr;
872 sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
875 if (new_nlevels > dn->dn_nlevels) {
876 int old_nlevels = dn->dn_nlevels;
879 dbuf_dirty_record_t *new, *dr, *dr_next;
881 dn->dn_nlevels = new_nlevels;
883 ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
884 dn->dn_next_nlevels[txgoff] = new_nlevels;
886 /* dirty the left indirects */
887 db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
888 new = dbuf_dirty(db, tx);
891 /* transfer the dirty records to the new indirect */
892 mutex_enter(&dn->dn_mtx);
893 mutex_enter(&new->dt.di.dr_mtx);
894 list = &dn->dn_dirty_records[txgoff];
895 for (dr = list_head(list); dr; dr = dr_next) {
896 dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
897 if (dr->dr_dbuf->db_level != new_nlevels-1 &&
898 dr->dr_dbuf->db_blkid != DB_BONUS_BLKID) {
899 ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
900 list_remove(&dn->dn_dirty_records[txgoff], dr);
901 list_insert_tail(&new->dt.di.dr_children, dr);
905 mutex_exit(&new->dt.di.dr_mtx);
906 mutex_exit(&dn->dn_mtx);
910 if (drop_struct_lock)
911 rw_exit(&dn->dn_struct_rwlock);
915 dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
917 avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
920 free_range_t rp_tofind;
921 uint64_t endblk = blkid + nblks;
923 ASSERT(MUTEX_HELD(&dn->dn_mtx));
924 ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */
926 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
927 blkid, nblks, tx->tx_txg);
928 rp_tofind.fr_blkid = blkid;
929 rp = avl_find(tree, &rp_tofind, &where);
931 rp = avl_nearest(tree, where, AVL_BEFORE);
933 rp = avl_nearest(tree, where, AVL_AFTER);
935 while (rp && (rp->fr_blkid <= blkid + nblks)) {
936 uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks;
937 free_range_t *nrp = AVL_NEXT(tree, rp);
939 if (blkid <= rp->fr_blkid && endblk >= fr_endblk) {
940 /* clear this entire range */
941 avl_remove(tree, rp);
942 kmem_free(rp, sizeof (free_range_t));
943 } else if (blkid <= rp->fr_blkid &&
944 endblk > rp->fr_blkid && endblk < fr_endblk) {
945 /* clear the beginning of this range */
946 rp->fr_blkid = endblk;
947 rp->fr_nblks = fr_endblk - endblk;
948 } else if (blkid > rp->fr_blkid && blkid < fr_endblk &&
949 endblk >= fr_endblk) {
950 /* clear the end of this range */
951 rp->fr_nblks = blkid - rp->fr_blkid;
952 } else if (blkid > rp->fr_blkid && endblk < fr_endblk) {
953 /* clear a chunk out of this range */
954 free_range_t *new_rp =
955 kmem_alloc(sizeof (free_range_t), KM_SLEEP);
957 new_rp->fr_blkid = endblk;
958 new_rp->fr_nblks = fr_endblk - endblk;
959 avl_insert_here(tree, new_rp, rp, AVL_AFTER);
960 rp->fr_nblks = blkid - rp->fr_blkid;
962 /* there may be no overlap */
968 dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
971 uint64_t blkoff, blkid, nblks;
975 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
976 blksz = dn->dn_datablksz;
978 /* If the range is past the end of the file, this is a no-op */
979 if (off >= blksz * (dn->dn_maxblkid+1))
982 len = UINT64_MAX - off;
987 * First, block align the region to free:
990 head = P2NPHASE(off, blksz);
991 blkoff = P2PHASE(off, blksz);
993 ASSERT(dn->dn_maxblkid == 0);
994 if (off == 0 && len >= blksz) {
995 /* Freeing the whole block; don't do any head. */
998 /* Freeing part of the block. */
1000 ASSERT3U(head, >, 0);
1004 /* zero out any partial block data at the start of the range */
1006 ASSERT3U(blkoff + head, ==, blksz);
1009 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE,
1013 /* don't dirty if it isn't on disk and isn't dirty */
1014 if (db->db_last_dirty ||
1015 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1016 rw_exit(&dn->dn_struct_rwlock);
1017 dbuf_will_dirty(db, tx);
1018 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1019 data = db->db.db_data;
1020 bzero(data + blkoff, head);
1022 dbuf_rele(db, FTAG);
1028 /* If the range was less than one block, we're done */
1029 if (len == 0 || off >= blksz * (dn->dn_maxblkid+1))
1034 * They are freeing the whole block of a
1035 * non-power-of-two blocksize file. Skip all the messy
1038 ASSERT3U(off, ==, 0);
1039 ASSERT3U(len, >=, blksz);
1044 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1045 int blkshift = dn->dn_datablkshift;
1047 /* If the remaining range is past end of file, we're done */
1048 if (off > dn->dn_maxblkid << blkshift)
1051 if (off + len == UINT64_MAX)
1054 tail = P2PHASE(len, blksz);
1056 ASSERT3U(P2PHASE(off, blksz), ==, 0);
1057 /* zero out any partial block data at the end of the range */
1061 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
1062 TRUE, FTAG, &db) == 0) {
1063 /* don't dirty if not on disk and not dirty */
1064 if (db->db_last_dirty ||
1066 !BP_IS_HOLE(db->db_blkptr))) {
1067 rw_exit(&dn->dn_struct_rwlock);
1068 dbuf_will_dirty(db, tx);
1069 rw_enter(&dn->dn_struct_rwlock,
1071 bzero(db->db.db_data, tail);
1073 dbuf_rele(db, FTAG);
1077 /* If the range did not include a full block, we are done */
1081 /* dirty the left indirects */
1082 if (dn->dn_nlevels > 1 && off != 0) {
1083 db = dbuf_hold_level(dn, 1,
1084 (off - head) >> (blkshift + epbs), FTAG);
1085 dbuf_will_dirty(db, tx);
1086 dbuf_rele(db, FTAG);
1089 /* dirty the right indirects */
1090 if (dn->dn_nlevels > 1 && !trunc) {
1091 db = dbuf_hold_level(dn, 1,
1092 (off + len + tail - 1) >> (blkshift + epbs), FTAG);
1093 dbuf_will_dirty(db, tx);
1094 dbuf_rele(db, FTAG);
1098 * Finally, add this range to the dnode range list, we
1099 * will finish up this free operation in the syncing phase.
1101 ASSERT(IS_P2ALIGNED(off, 1<<blkshift));
1102 ASSERT(off + len == UINT64_MAX ||
1103 IS_P2ALIGNED(len, 1<<blkshift));
1104 blkid = off >> blkshift;
1105 nblks = len >> blkshift;
1108 dn->dn_maxblkid = (blkid ? blkid - 1 : 0);
1111 mutex_enter(&dn->dn_mtx);
1112 dnode_clear_range(dn, blkid, nblks, tx);
1114 free_range_t *rp, *found;
1116 avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1118 /* Add new range to dn_ranges */
1119 rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP);
1120 rp->fr_blkid = blkid;
1121 rp->fr_nblks = nblks;
1122 found = avl_find(tree, rp, &where);
1123 ASSERT(found == NULL);
1124 avl_insert(tree, rp, where);
1125 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1126 blkid, nblks, tx->tx_txg);
1128 mutex_exit(&dn->dn_mtx);
1130 dbuf_free_range(dn, blkid, nblks, tx);
1131 dnode_setdirty(dn, tx);
1133 rw_exit(&dn->dn_struct_rwlock);
1136 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1138 dnode_block_freed(dnode_t *dn, uint64_t blkid)
1140 free_range_t range_tofind;
1141 void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1144 if (blkid == DB_BONUS_BLKID)
1148 * If we're in the process of opening the pool, dp will not be
1149 * set yet, but there shouldn't be anything dirty.
1154 if (dn->dn_free_txg)
1158 * If dn_datablkshift is not set, then there's only a single
1159 * block, in which case there will never be a free range so it
1162 range_tofind.fr_blkid = blkid;
1163 mutex_enter(&dn->dn_mtx);
1164 for (i = 0; i < TXG_SIZE; i++) {
1165 free_range_t *range_found;
1168 range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx);
1170 ASSERT(range_found->fr_nblks > 0);
1173 range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE);
1175 range_found->fr_blkid + range_found->fr_nblks > blkid)
1178 mutex_exit(&dn->dn_mtx);
1179 return (i < TXG_SIZE);
1182 /* call from syncing context when we actually write/free space for this dnode */
1184 dnode_diduse_space(dnode_t *dn, int64_t delta)
1187 dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
1189 (u_longlong_t)dn->dn_phys->dn_used,
1192 mutex_enter(&dn->dn_mtx);
1193 space = DN_USED_BYTES(dn->dn_phys);
1195 ASSERT3U(space + delta, >=, space); /* no overflow */
1197 ASSERT3U(space, >=, -delta); /* no underflow */
1200 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
1201 ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
1202 ASSERT3U(P2PHASE(space, 1<<DEV_BSHIFT), ==, 0);
1203 dn->dn_phys->dn_used = space >> DEV_BSHIFT;
1205 dn->dn_phys->dn_used = space;
1206 dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
1208 mutex_exit(&dn->dn_mtx);
1212 * Call when we think we're going to write/free space in open context.
1213 * Be conservative (ie. OK to write less than this or free more than
1214 * this, but don't write more or free less).
1217 dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1219 objset_impl_t *os = dn->dn_objset;
1220 dsl_dataset_t *ds = os->os_dsl_dataset;
1223 space = spa_get_asize(os->os_spa, space);
1226 dsl_dir_willuse_space(ds->ds_dir, space, tx);
1228 dmu_tx_willuse_space(tx, space);
1232 dnode_next_offset_level(dnode_t *dn, boolean_t hole, uint64_t *offset,
1233 int lvl, uint64_t blkfill, uint64_t txg)
1235 dmu_buf_impl_t *db = NULL;
1237 uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1238 uint64_t epb = 1ULL << epbs;
1239 uint64_t minfill, maxfill;
1242 dprintf("probing object %llu offset %llx level %d of %u\n",
1243 dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
1245 if (lvl == dn->dn_phys->dn_nlevels) {
1247 epb = dn->dn_phys->dn_nblkptr;
1248 data = dn->dn_phys->dn_blkptr;
1250 uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl);
1251 error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db);
1253 if (error == ENOENT)
1254 return (hole ? 0 : ESRCH);
1257 error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT);
1259 dbuf_rele(db, FTAG);
1262 data = db->db.db_data;
1266 (db->db_blkptr == NULL || db->db_blkptr->blk_birth <= txg)) {
1268 } else if (lvl == 0) {
1269 dnode_phys_t *dnp = data;
1271 ASSERT(dn->dn_type == DMU_OT_DNODE);
1273 for (i = (*offset >> span) & (blkfill - 1); i < blkfill; i++) {
1274 boolean_t newcontents = B_TRUE;
1277 newcontents = B_FALSE;
1278 for (j = 0; j < dnp[i].dn_nblkptr; j++) {
1279 if (dnp[i].dn_blkptr[j].blk_birth > txg)
1280 newcontents = B_TRUE;
1283 if (!dnp[i].dn_type == hole && newcontents)
1285 *offset += 1ULL << span;
1290 blkptr_t *bp = data;
1291 span = (lvl - 1) * epbs + dn->dn_datablkshift;
1293 maxfill = blkfill << ((lvl - 1) * epbs);
1300 for (i = (*offset >> span) & ((1ULL << epbs) - 1);
1302 if (bp[i].blk_fill >= minfill &&
1303 bp[i].blk_fill <= maxfill &&
1304 bp[i].blk_birth > txg)
1306 *offset += 1ULL << span;
1313 dbuf_rele(db, FTAG);
1319 * Find the next hole, data, or sparse region at or after *offset.
1320 * The value 'blkfill' tells us how many items we expect to find
1321 * in an L0 data block; this value is 1 for normal objects,
1322 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1323 * DNODES_PER_BLOCK when searching for sparse regions thereof.
1327 * dnode_next_offset(dn, hole, offset, 1, 1, 0);
1328 * Finds the next hole/data in a file.
1329 * Used in dmu_offset_next().
1331 * dnode_next_offset(mdn, hole, offset, 0, DNODES_PER_BLOCK, txg);
1332 * Finds the next free/allocated dnode an objset's meta-dnode.
1333 * Only finds objects that have new contents since txg (ie.
1334 * bonus buffer changes and content removal are ignored).
1335 * Used in dmu_object_next().
1337 * dnode_next_offset(mdn, TRUE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
1338 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
1339 * Used in dmu_object_alloc().
1342 dnode_next_offset(dnode_t *dn, boolean_t hole, uint64_t *offset,
1343 int minlvl, uint64_t blkfill, uint64_t txg)
1347 uint64_t initial_offset = *offset;
1349 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1351 if (dn->dn_phys->dn_nlevels == 0) {
1352 rw_exit(&dn->dn_struct_rwlock);
1356 if (dn->dn_datablkshift == 0) {
1357 if (*offset < dn->dn_datablksz) {
1359 *offset = dn->dn_datablksz;
1363 rw_exit(&dn->dn_struct_rwlock);
1367 maxlvl = dn->dn_phys->dn_nlevels;
1369 for (lvl = minlvl; lvl <= maxlvl; lvl++) {
1370 error = dnode_next_offset_level(dn,
1371 hole, offset, lvl, blkfill, txg);
1376 while (--lvl >= minlvl && error == 0) {
1377 error = dnode_next_offset_level(dn,
1378 hole, offset, lvl, blkfill, txg);
1381 rw_exit(&dn->dn_struct_rwlock);
1383 if (error == 0 && initial_offset > *offset)