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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 #include <sys/zfs_context.h>
30 #include <sys/dnode.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dmu_objset.h>
34 #include <sys/dmu_send.h>
35 #include <sys/dsl_dataset.h>
37 #include <sys/range_tree.h>
38 #include <sys/zfeature.h>
41 dnode_increase_indirection(dnode_t
*dn
, dmu_tx_t
*tx
)
44 int txgoff
= tx
->tx_txg
& TXG_MASK
;
45 int nblkptr
= dn
->dn_phys
->dn_nblkptr
;
46 int old_toplvl
= dn
->dn_phys
->dn_nlevels
- 1;
47 int new_level
= dn
->dn_next_nlevels
[txgoff
];
50 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
52 /* this dnode can't be paged out because it's dirty */
53 ASSERT(dn
->dn_phys
->dn_type
!= DMU_OT_NONE
);
54 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
55 ASSERT(new_level
> 1 && dn
->dn_phys
->dn_nlevels
> 0);
57 db
= dbuf_hold_level(dn
, dn
->dn_phys
->dn_nlevels
, 0, FTAG
);
60 dn
->dn_phys
->dn_nlevels
= new_level
;
61 dprintf("os=%p obj=%llu, increase to %d\n", dn
->dn_objset
,
62 dn
->dn_object
, dn
->dn_phys
->dn_nlevels
);
64 /* transfer dnode's block pointers to new indirect block */
65 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
|DB_RF_HAVESTRUCT
);
66 ASSERT(db
->db
.db_data
);
67 ASSERT(arc_released(db
->db_buf
));
68 ASSERT3U(sizeof (blkptr_t
) * nblkptr
, <=, db
->db
.db_size
);
69 bcopy(dn
->dn_phys
->dn_blkptr
, db
->db
.db_data
,
70 sizeof (blkptr_t
) * nblkptr
);
71 arc_buf_freeze(db
->db_buf
);
73 /* set dbuf's parent pointers to new indirect buf */
74 for (i
= 0; i
< nblkptr
; i
++) {
75 dmu_buf_impl_t
*child
=
76 dbuf_find(dn
->dn_objset
, dn
->dn_object
, old_toplvl
, i
);
81 DB_DNODE_ENTER(child
);
82 ASSERT3P(DB_DNODE(child
), ==, dn
);
85 if (child
->db_parent
&& child
->db_parent
!= dn
->dn_dbuf
) {
86 ASSERT(child
->db_parent
->db_level
== db
->db_level
);
87 ASSERT(child
->db_blkptr
!=
88 &dn
->dn_phys
->dn_blkptr
[child
->db_blkid
]);
89 mutex_exit(&child
->db_mtx
);
92 ASSERT(child
->db_parent
== NULL
||
93 child
->db_parent
== dn
->dn_dbuf
);
95 child
->db_parent
= db
;
96 dbuf_add_ref(db
, child
);
98 child
->db_blkptr
= (blkptr_t
*)db
->db
.db_data
+ i
;
100 child
->db_blkptr
= NULL
;
101 dprintf_dbuf_bp(child
, child
->db_blkptr
,
102 "changed db_blkptr to new indirect %s", "");
104 mutex_exit(&child
->db_mtx
);
107 bzero(dn
->dn_phys
->dn_blkptr
, sizeof (blkptr_t
) * nblkptr
);
111 rw_exit(&dn
->dn_struct_rwlock
);
115 free_blocks(dnode_t
*dn
, blkptr_t
*bp
, int num
, dmu_tx_t
*tx
)
117 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
118 uint64_t bytesfreed
= 0;
120 dprintf("ds=%p obj=%llx num=%d\n", ds
, dn
->dn_object
, num
);
122 for (int i
= 0; i
< num
; i
++, bp
++) {
126 bytesfreed
+= dsl_dataset_block_kill(ds
, bp
, tx
, B_FALSE
);
127 ASSERT3U(bytesfreed
, <=, DN_USED_BYTES(dn
->dn_phys
));
130 * Save some useful information on the holes being
131 * punched, including logical size, type, and indirection
132 * level. Retaining birth time enables detection of when
133 * holes are punched for reducing the number of free
134 * records transmitted during a zfs send.
137 uint64_t lsize
= BP_GET_LSIZE(bp
);
138 dmu_object_type_t type
= BP_GET_TYPE(bp
);
139 uint64_t lvl
= BP_GET_LEVEL(bp
);
141 bzero(bp
, sizeof (blkptr_t
));
143 if (spa_feature_is_active(dn
->dn_objset
->os_spa
,
144 SPA_FEATURE_HOLE_BIRTH
)) {
145 BP_SET_LSIZE(bp
, lsize
);
146 BP_SET_TYPE(bp
, type
);
147 BP_SET_LEVEL(bp
, lvl
);
148 BP_SET_BIRTH(bp
, dmu_tx_get_txg(tx
), 0);
151 dnode_diduse_space(dn
, -bytesfreed
);
156 free_verify(dmu_buf_impl_t
*db
, uint64_t start
, uint64_t end
, dmu_tx_t
*tx
)
160 uint64_t txg
= tx
->tx_txg
;
165 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
166 off
= start
- (db
->db_blkid
* 1<<epbs
);
167 num
= end
- start
+ 1;
169 ASSERT3U(off
, >=, 0);
170 ASSERT3U(num
, >=, 0);
171 ASSERT3U(db
->db_level
, >, 0);
172 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
173 ASSERT3U(off
+num
, <=, db
->db
.db_size
>> SPA_BLKPTRSHIFT
);
174 ASSERT(db
->db_blkptr
!= NULL
);
176 for (i
= off
; i
< off
+num
; i
++) {
178 dmu_buf_impl_t
*child
;
179 dbuf_dirty_record_t
*dr
;
182 ASSERT(db
->db_level
== 1);
184 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
185 err
= dbuf_hold_impl(dn
, db
->db_level
-1,
186 (db
->db_blkid
<< epbs
) + i
, TRUE
, FALSE
, FTAG
, &child
);
187 rw_exit(&dn
->dn_struct_rwlock
);
191 ASSERT(child
->db_level
== 0);
192 dr
= child
->db_last_dirty
;
193 while (dr
&& dr
->dr_txg
> txg
)
195 ASSERT(dr
== NULL
|| dr
->dr_txg
== txg
);
197 /* data_old better be zeroed */
199 buf
= dr
->dt
.dl
.dr_data
->b_data
;
200 for (j
= 0; j
< child
->db
.db_size
>> 3; j
++) {
202 panic("freed data not zero: "
203 "child=%p i=%d off=%d num=%d\n",
204 (void *)child
, i
, off
, num
);
210 * db_data better be zeroed unless it's dirty in a
213 mutex_enter(&child
->db_mtx
);
214 buf
= child
->db
.db_data
;
215 if (buf
!= NULL
&& child
->db_state
!= DB_FILL
&&
216 child
->db_last_dirty
== NULL
) {
217 for (j
= 0; j
< child
->db
.db_size
>> 3; j
++) {
219 panic("freed data not zero: "
220 "child=%p i=%d off=%d num=%d\n",
221 (void *)child
, i
, off
, num
);
225 mutex_exit(&child
->db_mtx
);
227 dbuf_rele(child
, FTAG
);
234 * We don't usually free the indirect blocks here. If in one txg we have a
235 * free_range and a write to the same indirect block, it's important that we
236 * preserve the hole's birth times. Therefore, we don't free any any indirect
237 * blocks in free_children(). If an indirect block happens to turn into all
238 * holes, it will be freed by dbuf_write_children_ready, which happens at a
239 * point in the syncing process where we know for certain the contents of the
242 * However, if we're freeing a dnode, its space accounting must go to zero
243 * before we actually try to free the dnode, or we will trip an assertion. In
244 * addition, we know the case described above cannot occur, because the dnode is
245 * being freed. Therefore, we free the indirect blocks immediately in that
249 free_children(dmu_buf_impl_t
*db
, uint64_t blkid
, uint64_t nblks
,
250 boolean_t free_indirects
, dmu_tx_t
*tx
)
254 dmu_buf_impl_t
*subdb
;
255 uint64_t start
, end
, dbstart
, dbend
;
256 unsigned int epbs
, shift
, i
;
259 * There is a small possibility that this block will not be cached:
260 * 1 - if level > 1 and there are no children with level <= 1
261 * 2 - if this block was evicted since we read it from
262 * dmu_tx_hold_free().
264 if (db
->db_state
!= DB_CACHED
)
265 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
272 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
273 ASSERT3U(epbs
, <, 31);
274 shift
= (db
->db_level
- 1) * epbs
;
275 dbstart
= db
->db_blkid
<< epbs
;
276 start
= blkid
>> shift
;
277 if (dbstart
< start
) {
278 bp
+= start
- dbstart
;
282 dbend
= ((db
->db_blkid
+ 1) << epbs
) - 1;
283 end
= (blkid
+ nblks
- 1) >> shift
;
287 ASSERT3U(start
, <=, end
);
289 if (db
->db_level
== 1) {
290 FREE_VERIFY(db
, start
, end
, tx
);
291 free_blocks(dn
, bp
, end
-start
+1, tx
);
293 for (uint64_t id
= start
; id
<= end
; id
++, bp
++) {
296 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
297 VERIFY0(dbuf_hold_impl(dn
, db
->db_level
- 1,
298 id
, TRUE
, FALSE
, FTAG
, &subdb
));
299 rw_exit(&dn
->dn_struct_rwlock
);
300 ASSERT3P(bp
, ==, subdb
->db_blkptr
);
302 free_children(subdb
, blkid
, nblks
, free_indirects
, tx
);
303 dbuf_rele(subdb
, FTAG
);
307 if (free_indirects
) {
308 for (i
= 0, bp
= db
->db
.db_data
; i
< 1 << epbs
; i
++, bp
++)
309 ASSERT(BP_IS_HOLE(bp
));
310 bzero(db
->db
.db_data
, db
->db
.db_size
);
311 free_blocks(dn
, db
->db_blkptr
, 1, tx
);
315 arc_buf_freeze(db
->db_buf
);
319 * Traverse the indicated range of the provided file
320 * and "free" all the blocks contained there.
323 dnode_sync_free_range_impl(dnode_t
*dn
, uint64_t blkid
, uint64_t nblks
,
324 boolean_t free_indirects
, dmu_tx_t
*tx
)
326 blkptr_t
*bp
= dn
->dn_phys
->dn_blkptr
;
327 int dnlevel
= dn
->dn_phys
->dn_nlevels
;
328 boolean_t trunc
= B_FALSE
;
330 if (blkid
> dn
->dn_phys
->dn_maxblkid
)
333 ASSERT(dn
->dn_phys
->dn_maxblkid
< UINT64_MAX
);
334 if (blkid
+ nblks
> dn
->dn_phys
->dn_maxblkid
) {
335 nblks
= dn
->dn_phys
->dn_maxblkid
- blkid
+ 1;
339 /* There are no indirect blocks in the object */
341 if (blkid
>= dn
->dn_phys
->dn_nblkptr
) {
342 /* this range was never made persistent */
345 ASSERT3U(blkid
+ nblks
, <=, dn
->dn_phys
->dn_nblkptr
);
346 free_blocks(dn
, bp
+ blkid
, nblks
, tx
);
348 int shift
= (dnlevel
- 1) *
349 (dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
);
350 int start
= blkid
>> shift
;
351 int end
= (blkid
+ nblks
- 1) >> shift
;
354 ASSERT(start
< dn
->dn_phys
->dn_nblkptr
);
356 for (int i
= start
; i
<= end
; i
++, bp
++) {
359 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
360 VERIFY0(dbuf_hold_impl(dn
, dnlevel
- 1, i
,
361 TRUE
, FALSE
, FTAG
, &db
));
362 rw_exit(&dn
->dn_struct_rwlock
);
364 free_children(db
, blkid
, nblks
, free_indirects
, tx
);
370 * Do not truncate the maxblkid if we are performing a raw
371 * receive. The raw receive sets the mablkid manually and
372 * must not be overriden.
374 if (trunc
&& !dn
->dn_objset
->os_raw_receive
) {
375 ASSERTV(uint64_t off
);
376 dn
->dn_phys
->dn_maxblkid
= blkid
== 0 ? 0 : blkid
- 1;
378 ASSERTV(off
= (dn
->dn_phys
->dn_maxblkid
+ 1) *
379 (dn
->dn_phys
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
));
380 ASSERT(off
< dn
->dn_phys
->dn_maxblkid
||
381 dn
->dn_phys
->dn_maxblkid
== 0 ||
382 dnode_next_offset(dn
, 0, &off
, 1, 1, 0) != 0);
386 typedef struct dnode_sync_free_range_arg
{
387 dnode_t
*dsfra_dnode
;
389 boolean_t dsfra_free_indirects
;
390 } dnode_sync_free_range_arg_t
;
393 dnode_sync_free_range(void *arg
, uint64_t blkid
, uint64_t nblks
)
395 dnode_sync_free_range_arg_t
*dsfra
= arg
;
396 dnode_t
*dn
= dsfra
->dsfra_dnode
;
398 mutex_exit(&dn
->dn_mtx
);
399 dnode_sync_free_range_impl(dn
, blkid
, nblks
,
400 dsfra
->dsfra_free_indirects
, dsfra
->dsfra_tx
);
401 mutex_enter(&dn
->dn_mtx
);
405 * Try to kick all the dnode's dbufs out of the cache...
408 dnode_evict_dbufs(dnode_t
*dn
)
410 dmu_buf_impl_t
*db_marker
;
411 dmu_buf_impl_t
*db
, *db_next
;
413 db_marker
= kmem_alloc(sizeof (dmu_buf_impl_t
), KM_SLEEP
);
415 mutex_enter(&dn
->dn_dbufs_mtx
);
416 for (db
= avl_first(&dn
->dn_dbufs
); db
!= NULL
; db
= db_next
) {
420 ASSERT3P(DB_DNODE(db
), ==, dn
);
424 mutex_enter(&db
->db_mtx
);
425 if (db
->db_state
!= DB_EVICTING
&&
426 refcount_is_zero(&db
->db_holds
)) {
427 db_marker
->db_level
= db
->db_level
;
428 db_marker
->db_blkid
= db
->db_blkid
;
429 db_marker
->db_state
= DB_SEARCH
;
430 avl_insert_here(&dn
->dn_dbufs
, db_marker
, db
,
434 * We need to use the "marker" dbuf rather than
435 * simply getting the next dbuf, because
436 * dbuf_destroy() may actually remove multiple dbufs.
437 * It can call itself recursively on the parent dbuf,
438 * which may also be removed from dn_dbufs. The code
439 * flow would look like:
442 * dnode_rele_and_unlock(parent_dbuf):
443 * if (!cacheable || pending_evict)
448 db_next
= AVL_NEXT(&dn
->dn_dbufs
, db_marker
);
449 avl_remove(&dn
->dn_dbufs
, db_marker
);
451 db
->db_pending_evict
= TRUE
;
452 mutex_exit(&db
->db_mtx
);
453 db_next
= AVL_NEXT(&dn
->dn_dbufs
, db
);
456 mutex_exit(&dn
->dn_dbufs_mtx
);
458 kmem_free(db_marker
, sizeof (dmu_buf_impl_t
));
460 dnode_evict_bonus(dn
);
464 dnode_evict_bonus(dnode_t
*dn
)
466 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
467 if (dn
->dn_bonus
!= NULL
) {
468 if (refcount_is_zero(&dn
->dn_bonus
->db_holds
)) {
469 mutex_enter(&dn
->dn_bonus
->db_mtx
);
470 dbuf_destroy(dn
->dn_bonus
);
473 dn
->dn_bonus
->db_pending_evict
= TRUE
;
476 rw_exit(&dn
->dn_struct_rwlock
);
480 dnode_undirty_dbufs(list_t
*list
)
482 dbuf_dirty_record_t
*dr
;
484 while ((dr
= list_head(list
))) {
485 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
486 uint64_t txg
= dr
->dr_txg
;
488 if (db
->db_level
!= 0)
489 dnode_undirty_dbufs(&dr
->dt
.di
.dr_children
);
491 mutex_enter(&db
->db_mtx
);
492 /* XXX - use dbuf_undirty()? */
493 list_remove(list
, dr
);
494 ASSERT(db
->db_last_dirty
== dr
);
495 db
->db_last_dirty
= NULL
;
496 db
->db_dirtycnt
-= 1;
497 if (db
->db_level
== 0) {
498 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
499 dr
->dt
.dl
.dr_data
== db
->db_buf
);
502 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
503 list_destroy(&dr
->dt
.di
.dr_children
);
505 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
506 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
511 dnode_sync_free(dnode_t
*dn
, dmu_tx_t
*tx
)
513 int txgoff
= tx
->tx_txg
& TXG_MASK
;
515 ASSERT(dmu_tx_is_syncing(tx
));
518 * Our contents should have been freed in dnode_sync() by the
519 * free range record inserted by the caller of dnode_free().
521 ASSERT0(DN_USED_BYTES(dn
->dn_phys
));
522 ASSERT(BP_IS_HOLE(dn
->dn_phys
->dn_blkptr
));
524 dnode_undirty_dbufs(&dn
->dn_dirty_records
[txgoff
]);
525 dnode_evict_dbufs(dn
);
528 * XXX - It would be nice to assert this, but we may still
529 * have residual holds from async evictions from the arc...
531 * zfs_obj_to_path() also depends on this being
534 * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1);
537 /* Undirty next bits */
538 dn
->dn_next_nlevels
[txgoff
] = 0;
539 dn
->dn_next_indblkshift
[txgoff
] = 0;
540 dn
->dn_next_blksz
[txgoff
] = 0;
541 dn
->dn_next_maxblkid
[txgoff
] = 0;
543 /* ASSERT(blkptrs are zero); */
544 ASSERT(dn
->dn_phys
->dn_type
!= DMU_OT_NONE
);
545 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
547 ASSERT(dn
->dn_free_txg
> 0);
548 if (dn
->dn_allocated_txg
!= dn
->dn_free_txg
)
549 dmu_buf_will_dirty(&dn
->dn_dbuf
->db
, tx
);
550 bzero(dn
->dn_phys
, sizeof (dnode_phys_t
) * dn
->dn_num_slots
);
551 dnode_free_interior_slots(dn
);
553 mutex_enter(&dn
->dn_mtx
);
554 dn
->dn_type
= DMU_OT_NONE
;
556 dn
->dn_allocated_txg
= 0;
558 dn
->dn_have_spill
= B_FALSE
;
559 dn
->dn_num_slots
= 1;
560 mutex_exit(&dn
->dn_mtx
);
562 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
564 dnode_rele(dn
, (void *)(uintptr_t)tx
->tx_txg
);
566 * Now that we've released our hold, the dnode may
567 * be evicted, so we mustn't access it.
572 * Write out the dnode's dirty buffers.
575 dnode_sync(dnode_t
*dn
, dmu_tx_t
*tx
)
577 objset_t
*os
= dn
->dn_objset
;
578 dnode_phys_t
*dnp
= dn
->dn_phys
;
579 int txgoff
= tx
->tx_txg
& TXG_MASK
;
580 list_t
*list
= &dn
->dn_dirty_records
[txgoff
];
581 ASSERTV(static const dnode_phys_t zerodn
= { 0 });
582 boolean_t kill_spill
= B_FALSE
;
584 ASSERT(dmu_tx_is_syncing(tx
));
585 ASSERT(dnp
->dn_type
!= DMU_OT_NONE
|| dn
->dn_allocated_txg
);
586 ASSERT(dnp
->dn_type
!= DMU_OT_NONE
||
587 bcmp(dnp
, &zerodn
, DNODE_MIN_SIZE
) == 0);
590 ASSERT(dn
->dn_dbuf
== NULL
|| arc_released(dn
->dn_dbuf
->db_buf
));
593 * Do user accounting if it is enabled and this is not
594 * an encrypted receive.
596 if (dmu_objset_userused_enabled(os
) &&
597 !DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) &&
598 (!os
->os_encrypted
|| !dmu_objset_is_receiving(os
))) {
599 mutex_enter(&dn
->dn_mtx
);
600 dn
->dn_oldused
= DN_USED_BYTES(dn
->dn_phys
);
601 dn
->dn_oldflags
= dn
->dn_phys
->dn_flags
;
602 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_USERUSED_ACCOUNTED
;
603 if (dmu_objset_userobjused_enabled(dn
->dn_objset
))
604 dn
->dn_phys
->dn_flags
|=
605 DNODE_FLAG_USEROBJUSED_ACCOUNTED
;
606 mutex_exit(&dn
->dn_mtx
);
607 dmu_objset_userquota_get_ids(dn
, B_FALSE
, tx
);
609 /* Once we account for it, we should always account for it */
610 ASSERT(!(dn
->dn_phys
->dn_flags
&
611 DNODE_FLAG_USERUSED_ACCOUNTED
));
612 ASSERT(!(dn
->dn_phys
->dn_flags
&
613 DNODE_FLAG_USEROBJUSED_ACCOUNTED
));
616 mutex_enter(&dn
->dn_mtx
);
617 if (dn
->dn_allocated_txg
== tx
->tx_txg
) {
618 /* The dnode is newly allocated or reallocated */
619 if (dnp
->dn_type
== DMU_OT_NONE
) {
620 /* this is a first alloc, not a realloc */
622 dnp
->dn_nblkptr
= dn
->dn_nblkptr
;
625 dnp
->dn_type
= dn
->dn_type
;
626 dnp
->dn_bonustype
= dn
->dn_bonustype
;
627 dnp
->dn_bonuslen
= dn
->dn_bonuslen
;
630 dnp
->dn_extra_slots
= dn
->dn_num_slots
- 1;
632 ASSERT(dnp
->dn_nlevels
> 1 ||
633 BP_IS_HOLE(&dnp
->dn_blkptr
[0]) ||
634 BP_IS_EMBEDDED(&dnp
->dn_blkptr
[0]) ||
635 BP_GET_LSIZE(&dnp
->dn_blkptr
[0]) ==
636 dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
637 ASSERT(dnp
->dn_nlevels
< 2 ||
638 BP_IS_HOLE(&dnp
->dn_blkptr
[0]) ||
639 BP_GET_LSIZE(&dnp
->dn_blkptr
[0]) == 1 << dnp
->dn_indblkshift
);
641 if (dn
->dn_next_type
[txgoff
] != 0) {
642 dnp
->dn_type
= dn
->dn_type
;
643 dn
->dn_next_type
[txgoff
] = 0;
646 if (dn
->dn_next_blksz
[txgoff
] != 0) {
647 ASSERT(P2PHASE(dn
->dn_next_blksz
[txgoff
],
648 SPA_MINBLOCKSIZE
) == 0);
649 ASSERT(BP_IS_HOLE(&dnp
->dn_blkptr
[0]) ||
650 dn
->dn_maxblkid
== 0 || list_head(list
) != NULL
||
651 dn
->dn_next_blksz
[txgoff
] >> SPA_MINBLOCKSHIFT
==
652 dnp
->dn_datablkszsec
||
653 !range_tree_is_empty(dn
->dn_free_ranges
[txgoff
]));
654 dnp
->dn_datablkszsec
=
655 dn
->dn_next_blksz
[txgoff
] >> SPA_MINBLOCKSHIFT
;
656 dn
->dn_next_blksz
[txgoff
] = 0;
659 if (dn
->dn_next_bonuslen
[txgoff
] != 0) {
660 if (dn
->dn_next_bonuslen
[txgoff
] == DN_ZERO_BONUSLEN
)
661 dnp
->dn_bonuslen
= 0;
663 dnp
->dn_bonuslen
= dn
->dn_next_bonuslen
[txgoff
];
664 ASSERT(dnp
->dn_bonuslen
<=
665 DN_SLOTS_TO_BONUSLEN(dnp
->dn_extra_slots
+ 1));
666 dn
->dn_next_bonuslen
[txgoff
] = 0;
669 if (dn
->dn_next_bonustype
[txgoff
] != 0) {
670 ASSERT(DMU_OT_IS_VALID(dn
->dn_next_bonustype
[txgoff
]));
671 dnp
->dn_bonustype
= dn
->dn_next_bonustype
[txgoff
];
672 dn
->dn_next_bonustype
[txgoff
] = 0;
675 boolean_t freeing_dnode
= dn
->dn_free_txg
> 0 &&
676 dn
->dn_free_txg
<= tx
->tx_txg
;
679 * Remove the spill block if we have been explicitly asked to
680 * remove it, or if the object is being removed.
682 if (dn
->dn_rm_spillblk
[txgoff
] || freeing_dnode
) {
683 if (dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)
685 dn
->dn_rm_spillblk
[txgoff
] = 0;
688 if (dn
->dn_next_indblkshift
[txgoff
] != 0) {
689 ASSERT(dnp
->dn_nlevels
== 1);
690 dnp
->dn_indblkshift
= dn
->dn_next_indblkshift
[txgoff
];
691 dn
->dn_next_indblkshift
[txgoff
] = 0;
695 * Just take the live (open-context) values for checksum and compress.
696 * Strictly speaking it's a future leak, but nothing bad happens if we
697 * start using the new checksum or compress algorithm a little early.
699 dnp
->dn_checksum
= dn
->dn_checksum
;
700 dnp
->dn_compress
= dn
->dn_compress
;
702 mutex_exit(&dn
->dn_mtx
);
705 free_blocks(dn
, DN_SPILL_BLKPTR(dn
->dn_phys
), 1, tx
);
706 mutex_enter(&dn
->dn_mtx
);
707 dnp
->dn_flags
&= ~DNODE_FLAG_SPILL_BLKPTR
;
708 mutex_exit(&dn
->dn_mtx
);
711 /* process all the "freed" ranges in the file */
712 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
713 dnode_sync_free_range_arg_t dsfra
;
714 dsfra
.dsfra_dnode
= dn
;
716 dsfra
.dsfra_free_indirects
= freeing_dnode
;
718 ASSERT(range_tree_contains(dn
->dn_free_ranges
[txgoff
],
719 0, dn
->dn_maxblkid
+ 1));
721 mutex_enter(&dn
->dn_mtx
);
722 range_tree_vacate(dn
->dn_free_ranges
[txgoff
],
723 dnode_sync_free_range
, &dsfra
);
724 range_tree_destroy(dn
->dn_free_ranges
[txgoff
]);
725 dn
->dn_free_ranges
[txgoff
] = NULL
;
726 mutex_exit(&dn
->dn_mtx
);
730 dn
->dn_objset
->os_freed_dnodes
++;
731 dnode_sync_free(dn
, tx
);
735 if (dn
->dn_num_slots
> DNODE_MIN_SLOTS
) {
736 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
737 mutex_enter(&ds
->ds_lock
);
738 ds
->ds_feature_activation_needed
[SPA_FEATURE_LARGE_DNODE
] =
740 mutex_exit(&ds
->ds_lock
);
743 if (dn
->dn_next_nlevels
[txgoff
]) {
744 dnode_increase_indirection(dn
, tx
);
745 dn
->dn_next_nlevels
[txgoff
] = 0;
749 * This must be done after dnode_sync_free_range()
750 * and dnode_increase_indirection().
752 if (dn
->dn_next_maxblkid
[txgoff
]) {
753 mutex_enter(&dn
->dn_mtx
);
754 dnp
->dn_maxblkid
= dn
->dn_next_maxblkid
[txgoff
];
755 dn
->dn_next_maxblkid
[txgoff
] = 0;
756 mutex_exit(&dn
->dn_mtx
);
759 if (dn
->dn_next_nblkptr
[txgoff
]) {
760 /* this should only happen on a realloc */
761 ASSERT(dn
->dn_allocated_txg
== tx
->tx_txg
);
762 if (dn
->dn_next_nblkptr
[txgoff
] > dnp
->dn_nblkptr
) {
763 /* zero the new blkptrs we are gaining */
764 bzero(dnp
->dn_blkptr
+ dnp
->dn_nblkptr
,
766 (dn
->dn_next_nblkptr
[txgoff
] - dnp
->dn_nblkptr
));
770 ASSERT(dn
->dn_next_nblkptr
[txgoff
] < dnp
->dn_nblkptr
);
771 /* the blkptrs we are losing better be unallocated */
772 for (i
= 0; i
< dnp
->dn_nblkptr
; i
++) {
773 if (i
>= dn
->dn_next_nblkptr
[txgoff
])
774 ASSERT(BP_IS_HOLE(&dnp
->dn_blkptr
[i
]));
778 mutex_enter(&dn
->dn_mtx
);
779 dnp
->dn_nblkptr
= dn
->dn_next_nblkptr
[txgoff
];
780 dn
->dn_next_nblkptr
[txgoff
] = 0;
781 mutex_exit(&dn
->dn_mtx
);
784 dbuf_sync_list(list
, dn
->dn_phys
->dn_nlevels
- 1, tx
);
786 if (!DMU_OBJECT_IS_SPECIAL(dn
->dn_object
)) {
787 ASSERT3P(list_head(list
), ==, NULL
);
788 dnode_rele(dn
, (void *)(uintptr_t)tx
->tx_txg
);
792 * Although we have dropped our reference to the dnode, it
793 * can't be evicted until its written, and we haven't yet
794 * initiated the IO for the dnode's dbuf.