1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2014 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 */
28
29 #include <sys/zfs_context.h>
30 #include <sys/dbuf.h>
31 #include <sys/dnode.h>
32 #include <sys/dmu.h>
33 #include <sys/dmu_tx.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/dsl_dataset.h>
36 #include <sys/spa.h>
37 #include <sys/range_tree.h>
38 #include <sys/zfeature.h>
39
40 static void
41 dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx)
42 {
43 dmu_buf_impl_t *db;
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];
48 int i;
49
50 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
51
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);
56
57 db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG);
58 ASSERT(db != NULL);
59
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);
63
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);
72
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);
77
78 if (child == NULL)
79 continue;
80 #ifdef DEBUG
81 DB_DNODE_ENTER(child);
82 ASSERT3P(DB_DNODE(child), ==, dn);
83 DB_DNODE_EXIT(child);
84 #endif /* DEBUG */
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);
90 continue;
91 }
92 ASSERT(child->db_parent == NULL ||
93 child->db_parent == dn->dn_dbuf);
94
95 child->db_parent = db;
96 dbuf_add_ref(db, child);
97 if (db->db.db_data)
98 child->db_blkptr = (blkptr_t *)db->db.db_data + i;
99 else
100 child->db_blkptr = NULL;
101 dprintf_dbuf_bp(child, child->db_blkptr,
102 "changed db_blkptr to new indirect %s", "");
103
104 mutex_exit(&child->db_mtx);
105 }
106
107 bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr);
108
109 dbuf_rele(db, FTAG);
110
111 rw_exit(&dn->dn_struct_rwlock);
112 }
113
114 static void
115 free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx)
116 {
117 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
118 uint64_t bytesfreed = 0;
119
120 dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num);
121
122 for (int i = 0; i < num; i++, bp++) {
123 if (BP_IS_HOLE(bp))
124 continue;
125
126 bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE);
127 ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys));
128
129 /*
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.
135 */
136
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);
140
141 bzero(bp, sizeof (blkptr_t));
142
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);
149 }
150 }
151 dnode_diduse_space(dn, -bytesfreed);
152 }
153
154 #ifdef ZFS_DEBUG
155 static void
156 free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx)
157 {
158 int off, num;
159 int i, err, epbs;
160 uint64_t txg = tx->tx_txg;
161 dnode_t *dn;
162
163 DB_DNODE_ENTER(db);
164 dn = DB_DNODE(db);
165 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
166 off = start - (db->db_blkid * 1<<epbs);
167 num = end - start + 1;
168
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);
175
176 for (i = off; i < off+num; i++) {
177 uint64_t *buf;
178 dmu_buf_impl_t *child;
179 dbuf_dirty_record_t *dr;
180 int j;
181
182 ASSERT(db->db_level == 1);
183
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);
188 if (err == ENOENT)
189 continue;
190 ASSERT(err == 0);
191 ASSERT(child->db_level == 0);
192 dr = child->db_last_dirty;
193 while (dr && dr->dr_txg > txg)
194 dr = dr->dr_next;
195 ASSERT(dr == NULL || dr->dr_txg == txg);
196
197 /* data_old better be zeroed */
198 if (dr) {
199 buf = dr->dt.dl.dr_data->b_data;
200 for (j = 0; j < child->db.db_size >> 3; j++) {
201 if (buf[j] != 0) {
202 panic("freed data not zero: "
203 "child=%p i=%d off=%d num=%d\n",
204 (void *)child, i, off, num);
205 }
206 }
207 }
208
209 /*
210 * db_data better be zeroed unless it's dirty in a
211 * future txg.
212 */
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++) {
218 if (buf[j] != 0) {
219 panic("freed data not zero: "
220 "child=%p i=%d off=%d num=%d\n",
221 (void *)child, i, off, num);
222 }
223 }
224 }
225 mutex_exit(&child->db_mtx);
226
227 dbuf_rele(child, FTAG);
228 }
229 DB_DNODE_EXIT(db);
230 }
231 #endif
232
233 static void
234 free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks,
235 dmu_tx_t *tx)
236 {
237 dnode_t *dn;
238 blkptr_t *bp;
239 dmu_buf_impl_t *subdb;
240 uint64_t start, end, dbstart, dbend;
241 unsigned int epbs, shift, i;
242
243 /*
244 * There is a small possibility that this block will not be cached:
245 * 1 - if level > 1 and there are no children with level <= 1
246 * 2 - if this block was evicted since we read it from
247 * dmu_tx_hold_free().
248 */
249 if (db->db_state != DB_CACHED)
250 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
251
252 dbuf_release_bp(db);
253 bp = db->db.db_data;
254
255 DB_DNODE_ENTER(db);
256 dn = DB_DNODE(db);
257 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
258 ASSERT3U(epbs, <, 31);
259 shift = (db->db_level - 1) * epbs;
260 dbstart = db->db_blkid << epbs;
261 start = blkid >> shift;
262 if (dbstart < start) {
263 bp += start - dbstart;
264 } else {
265 start = dbstart;
266 }
267 dbend = ((db->db_blkid + 1) << epbs) - 1;
268 end = (blkid + nblks - 1) >> shift;
269 if (dbend <= end)
270 end = dbend;
271
272 ASSERT3U(start, <=, end);
273
274 if (db->db_level == 1) {
275 FREE_VERIFY(db, start, end, tx);
276 free_blocks(dn, bp, end-start+1, tx);
277 } else {
278 for (uint64_t id = start; id <= end; id++, bp++) {
279 if (BP_IS_HOLE(bp))
280 continue;
281 rw_enter(&dn->dn_struct_rwlock, RW_READER);
282 VERIFY0(dbuf_hold_impl(dn, db->db_level - 1,
283 id, TRUE, FALSE, FTAG, &subdb));
284 rw_exit(&dn->dn_struct_rwlock);
285 ASSERT3P(bp, ==, subdb->db_blkptr);
286
287 free_children(subdb, blkid, nblks, tx);
288 dbuf_rele(subdb, FTAG);
289 }
290 }
291
292 /* If this whole block is free, free ourself too. */
293 for (i = 0, bp = db->db.db_data; i < 1 << epbs; i++, bp++) {
294 if (!BP_IS_HOLE(bp))
295 break;
296 }
297 if (i == 1 << epbs) {
298 /*
299 * We only found holes. Grab the rwlock to prevent
300 * anybody from reading the blocks we're about to
301 * zero out.
302 */
303 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
304 bzero(db->db.db_data, db->db.db_size);
305 rw_exit(&dn->dn_struct_rwlock);
306 free_blocks(dn, db->db_blkptr, 1, tx);
307 } else {
308 /*
309 * Partial block free; must be marked dirty so that it
310 * will be written out.
311 */
312 ASSERT(db->db_dirtycnt > 0);
313 }
314
315 DB_DNODE_EXIT(db);
316 arc_buf_freeze(db->db_buf);
317 }
318
319 /*
320 * Traverse the indicated range of the provided file
321 * and "free" all the blocks contained there.
322 */
323 static void
324 dnode_sync_free_range_impl(dnode_t *dn, uint64_t blkid, uint64_t nblks,
325 dmu_tx_t *tx)
326 {
327 blkptr_t *bp = dn->dn_phys->dn_blkptr;
328 int dnlevel = dn->dn_phys->dn_nlevels;
329 boolean_t trunc = B_FALSE;
330
331 if (blkid > dn->dn_phys->dn_maxblkid)
332 return;
333
334 ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX);
335 if (blkid + nblks > dn->dn_phys->dn_maxblkid) {
336 nblks = dn->dn_phys->dn_maxblkid - blkid + 1;
337 trunc = B_TRUE;
338 }
339
340 /* There are no indirect blocks in the object */
341 if (dnlevel == 1) {
342 if (blkid >= dn->dn_phys->dn_nblkptr) {
343 /* this range was never made persistent */
344 return;
345 }
346 ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr);
347 free_blocks(dn, bp + blkid, nblks, tx);
348 } else {
349 int shift = (dnlevel - 1) *
350 (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT);
351 int start = blkid >> shift;
352 int end = (blkid + nblks - 1) >> shift;
353 dmu_buf_impl_t *db;
354
355 ASSERT(start < dn->dn_phys->dn_nblkptr);
356 bp += start;
357 for (int i = start; i <= end; i++, bp++) {
358 if (BP_IS_HOLE(bp))
359 continue;
360 rw_enter(&dn->dn_struct_rwlock, RW_READER);
361 VERIFY0(dbuf_hold_impl(dn, dnlevel - 1, i,
362 TRUE, FALSE, FTAG, &db));
363 rw_exit(&dn->dn_struct_rwlock);
364
365 free_children(db, blkid, nblks, tx);
366 dbuf_rele(db, FTAG);
367 }
368 }
369
370 if (trunc) {
371 dn->dn_phys->dn_maxblkid = blkid == 0 ? 0 : blkid - 1;
372
373 uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
374 (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
375 ASSERT(off < dn->dn_phys->dn_maxblkid ||
376 dn->dn_phys->dn_maxblkid == 0 ||
377 dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
378 }
379 }
380
381 typedef struct dnode_sync_free_range_arg {
382 dnode_t *dsfra_dnode;
383 dmu_tx_t *dsfra_tx;
384 } dnode_sync_free_range_arg_t;
385
386 static void
387 dnode_sync_free_range(void *arg, uint64_t blkid, uint64_t nblks)
388 {
389 dnode_sync_free_range_arg_t *dsfra = arg;
390 dnode_t *dn = dsfra->dsfra_dnode;
391
392 mutex_exit(&dn->dn_mtx);
393 dnode_sync_free_range_impl(dn, blkid, nblks, dsfra->dsfra_tx);
394 mutex_enter(&dn->dn_mtx);
395 }
396
397 /*
398 * Try to kick all the dnode's dbufs out of the cache...
399 */
400 void
401 dnode_evict_dbufs(dnode_t *dn, int level)
402 {
403 dmu_buf_impl_t db_marker;
404 dmu_buf_impl_t *db, *db_next;
405
406 mutex_enter(&dn->dn_dbufs_mtx);
407 for (db = avl_first(&dn->dn_dbufs); db != NULL; db = db_next) {
408
409 #ifdef DEBUG
410 DB_DNODE_ENTER(db);
411 ASSERT3P(DB_DNODE(db), ==, dn);
412 DB_DNODE_EXIT(db);
413 #endif /* DEBUG */
414
415 mutex_enter(&db->db_mtx);
416 if (level != DBUF_EVICT_ALL && db->db_level != level) {
417 mutex_exit(&db->db_mtx);
418 db_next = AVL_NEXT(&dn->dn_dbufs, db);
419 continue;
420 }
421
422 if (db->db_state != DB_EVICTING &&
423 refcount_is_zero(&db->db_holds)) {
424 db_marker.db_level = db->db_level;
425 db_marker.db_blkid = db->db_blkid;
426 db_marker.db_state = DB_SEARCH;
427 avl_insert_here(&dn->dn_dbufs, &db_marker, db,
428 AVL_BEFORE);
429
430 dbuf_destroy(db);
431
432 db_next = AVL_NEXT(&dn->dn_dbufs, &db_marker);
433 avl_remove(&dn->dn_dbufs, &db_marker);
434 } else {
435 db->db_pending_evict = TRUE;
436 mutex_exit(&db->db_mtx);
437 db_next = AVL_NEXT(&dn->dn_dbufs, db);
438 }
439 }
440 mutex_exit(&dn->dn_dbufs_mtx);
441
442 dnode_evict_bonus(dn);
443 }
444
445 void
446 dnode_evict_bonus(dnode_t *dn)
447 {
448 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
449 if (dn->dn_bonus != NULL) {
450 if (refcount_is_zero(&dn->dn_bonus->db_holds)) {
451 mutex_enter(&dn->dn_bonus->db_mtx);
452 dbuf_destroy(dn->dn_bonus);
453 dn->dn_bonus = NULL;
454 } else {
455 dn->dn_bonus->db_pending_evict = TRUE;
456 }
457 }
458 rw_exit(&dn->dn_struct_rwlock);
459 }
460
461 static void
462 dnode_undirty_dbufs(list_t *list)
463 {
464 dbuf_dirty_record_t *dr;
465
466 while (dr = list_head(list)) {
467 dmu_buf_impl_t *db = dr->dr_dbuf;
468 uint64_t txg = dr->dr_txg;
469
470 if (db->db_level != 0)
471 dnode_undirty_dbufs(&dr->dt.di.dr_children);
472
473 mutex_enter(&db->db_mtx);
474 /* XXX - use dbuf_undirty()? */
475 list_remove(list, dr);
476 ASSERT(db->db_last_dirty == dr);
477 db->db_last_dirty = NULL;
478 db->db_dirtycnt -= 1;
479 if (db->db_level == 0) {
480 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
481 dr->dt.dl.dr_data == db->db_buf);
482 dbuf_unoverride(dr);
483 } else {
484 mutex_destroy(&dr->dt.di.dr_mtx);
485 list_destroy(&dr->dt.di.dr_children);
486 }
487 kmem_free(dr, sizeof (dbuf_dirty_record_t));
488 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
489 }
490 }
491
492 static void
493 dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
494 {
495 int txgoff = tx->tx_txg & TXG_MASK;
496
497 ASSERT(dmu_tx_is_syncing(tx));
498
499 /*
500 * Our contents should have been freed in dnode_sync() by the
501 * free range record inserted by the caller of dnode_free().
502 */
503 ASSERT0(DN_USED_BYTES(dn->dn_phys));
504 ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr));
505
506 dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
507 dnode_evict_dbufs(dn, DBUF_EVICT_ALL);
508 ASSERT(avl_is_empty(&dn->dn_dbufs));
509
510 /*
511 * XXX - It would be nice to assert this, but we may still
512 * have residual holds from async evictions from the arc...
513 *
514 * zfs_obj_to_path() also depends on this being
515 * commented out.
516 *
517 * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1);
518 */
519
520 /* Undirty next bits */
521 dn->dn_next_nlevels[txgoff] = 0;
522 dn->dn_next_indblkshift[txgoff] = 0;
523 dn->dn_next_blksz[txgoff] = 0;
524
525 /* ASSERT(blkptrs are zero); */
526 ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
527 ASSERT(dn->dn_type != DMU_OT_NONE);
528
529 ASSERT(dn->dn_free_txg > 0);
530 if (dn->dn_allocated_txg != dn->dn_free_txg)
531 dmu_buf_will_dirty(&dn->dn_dbuf->db, tx);
532 bzero(dn->dn_phys, sizeof (dnode_phys_t));
533
534 mutex_enter(&dn->dn_mtx);
535 dn->dn_type = DMU_OT_NONE;
536 dn->dn_maxblkid = 0;
537 dn->dn_allocated_txg = 0;
538 dn->dn_free_txg = 0;
539 dn->dn_have_spill = B_FALSE;
540 mutex_exit(&dn->dn_mtx);
541
542 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
543
544 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
545 /*
546 * Now that we've released our hold, the dnode may
547 * be evicted, so we musn't access it.
548 */
549 }
550
551 /*
552 * Write out the dnode's dirty buffers.
553 */
554 void
555 dnode_sync(dnode_t *dn, dmu_tx_t *tx)
556 {
557 dnode_phys_t *dnp = dn->dn_phys;
558 int txgoff = tx->tx_txg & TXG_MASK;
559 list_t *list = &dn->dn_dirty_records[txgoff];
560 static const dnode_phys_t zerodn = { 0 };
561 boolean_t kill_spill = B_FALSE;
562
563 ASSERT(dmu_tx_is_syncing(tx));
564 ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
565 ASSERT(dnp->dn_type != DMU_OT_NONE ||
566 bcmp(dnp, &zerodn, DNODE_SIZE) == 0);
567 DNODE_VERIFY(dn);
568
569 ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
570
571 if (dmu_objset_userused_enabled(dn->dn_objset) &&
572 !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
573 mutex_enter(&dn->dn_mtx);
574 dn->dn_oldused = DN_USED_BYTES(dn->dn_phys);
575 dn->dn_oldflags = dn->dn_phys->dn_flags;
576 dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED;
577 mutex_exit(&dn->dn_mtx);
578 dmu_objset_userquota_get_ids(dn, B_FALSE, tx);
579 } else {
580 /* Once we account for it, we should always account for it. */
581 ASSERT(!(dn->dn_phys->dn_flags &
582 DNODE_FLAG_USERUSED_ACCOUNTED));
583 }
584
585 mutex_enter(&dn->dn_mtx);
586 if (dn->dn_allocated_txg == tx->tx_txg) {
587 /* The dnode is newly allocated or reallocated */
588 if (dnp->dn_type == DMU_OT_NONE) {
589 /* this is a first alloc, not a realloc */
590 dnp->dn_nlevels = 1;
591 dnp->dn_nblkptr = dn->dn_nblkptr;
592 }
593
594 dnp->dn_type = dn->dn_type;
595 dnp->dn_bonustype = dn->dn_bonustype;
596 dnp->dn_bonuslen = dn->dn_bonuslen;
597 }
598 ASSERT(dnp->dn_nlevels > 1 ||
599 BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
600 BP_IS_EMBEDDED(&dnp->dn_blkptr[0]) ||
601 BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
602 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
603 ASSERT(dnp->dn_nlevels < 2 ||
604 BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
605 BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 1 << dnp->dn_indblkshift);
606
607 if (dn->dn_next_type[txgoff] != 0) {
608 dnp->dn_type = dn->dn_type;
609 dn->dn_next_type[txgoff] = 0;
610 }
611
612 if (dn->dn_next_blksz[txgoff] != 0) {
613 ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
614 SPA_MINBLOCKSIZE) == 0);
615 ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
616 dn->dn_maxblkid == 0 || list_head(list) != NULL ||
617 dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
618 dnp->dn_datablkszsec ||
619 range_tree_space(dn->dn_free_ranges[txgoff]) != 0);
620 dnp->dn_datablkszsec =
621 dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
622 dn->dn_next_blksz[txgoff] = 0;
623 }
624
625 if (dn->dn_next_bonuslen[txgoff] != 0) {
626 if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
627 dnp->dn_bonuslen = 0;
628 else
629 dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
630 ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN);
631 dn->dn_next_bonuslen[txgoff] = 0;
632 }
633
634 if (dn->dn_next_bonustype[txgoff] != 0) {
635 ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff]));
636 dnp->dn_bonustype = dn->dn_next_bonustype[txgoff];
637 dn->dn_next_bonustype[txgoff] = 0;
638 }
639
640 boolean_t freeing_dnode = dn->dn_free_txg > 0 &&
641 dn->dn_free_txg <= tx->tx_txg;
642
643 /*
644 * Remove the spill block if we have been explicitly asked to
645 * remove it, or if the object is being removed.
646 */
647 if (dn->dn_rm_spillblk[txgoff] || freeing_dnode) {
648 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
649 kill_spill = B_TRUE;
650 dn->dn_rm_spillblk[txgoff] = 0;
651 }
652
653 if (dn->dn_next_indblkshift[txgoff] != 0) {
654 ASSERT(dnp->dn_nlevels == 1);
655 dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
656 dn->dn_next_indblkshift[txgoff] = 0;
657 }
658
659 /*
660 * Just take the live (open-context) values for checksum and compress.
661 * Strictly speaking it's a future leak, but nothing bad happens if we
662 * start using the new checksum or compress algorithm a little early.
663 */
664 dnp->dn_checksum = dn->dn_checksum;
665 dnp->dn_compress = dn->dn_compress;
666
667 mutex_exit(&dn->dn_mtx);
668
669 if (kill_spill) {
670 free_blocks(dn, &dn->dn_phys->dn_spill, 1, tx);
671 mutex_enter(&dn->dn_mtx);
672 dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR;
673 mutex_exit(&dn->dn_mtx);
674 }
675
676 /* process all the "freed" ranges in the file */
677 if (dn->dn_free_ranges[txgoff] != NULL) {
678 dnode_sync_free_range_arg_t dsfra;
679 dsfra.dsfra_dnode = dn;
680 dsfra.dsfra_tx = tx;
681 mutex_enter(&dn->dn_mtx);
682 range_tree_vacate(dn->dn_free_ranges[txgoff],
683 dnode_sync_free_range, &dsfra);
684 range_tree_destroy(dn->dn_free_ranges[txgoff]);
685 dn->dn_free_ranges[txgoff] = NULL;
686 mutex_exit(&dn->dn_mtx);
687 }
688
689 if (freeing_dnode) {
690 dn->dn_objset->os_freed_dnodes++;
691 dnode_sync_free(dn, tx);
692 return;
693 }
694
695 if (dn->dn_next_nlevels[txgoff]) {
696 dnode_increase_indirection(dn, tx);
697 dn->dn_next_nlevels[txgoff] = 0;
698 }
699
700 if (dn->dn_next_nblkptr[txgoff]) {
701 /* this should only happen on a realloc */
702 ASSERT(dn->dn_allocated_txg == tx->tx_txg);
703 if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) {
704 /* zero the new blkptrs we are gaining */
705 bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
706 sizeof (blkptr_t) *
707 (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr));
708 #ifdef ZFS_DEBUG
709 } else {
710 int i;
711 ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr);
712 /* the blkptrs we are losing better be unallocated */
713 for (i = dn->dn_next_nblkptr[txgoff];
714 i < dnp->dn_nblkptr; i++)
715 ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i]));
716 #endif
717 }
718 mutex_enter(&dn->dn_mtx);
719 dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff];
720 dn->dn_next_nblkptr[txgoff] = 0;
721 mutex_exit(&dn->dn_mtx);
722 }
723
724 dbuf_sync_list(list, dn->dn_phys->dn_nlevels - 1, tx);
725
726 if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
727 ASSERT3P(list_head(list), ==, NULL);
728 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
729 }
730
731 /*
732 * Although we have dropped our reference to the dnode, it
733 * can't be evicted until its written, and we haven't yet
734 * initiated the IO for the dnode's dbuf.
735 */
736 }