]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/dnode_sync.c
Illumos 4975 - missing mutex_destroy() calls in zfs
[mirror_zfs.git] / module / zfs / dnode_sync.c
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) 2012, 2014 by Delphix. All rights reserved.
25 */
26
27 #include <sys/zfs_context.h>
28 #include <sys/dbuf.h>
29 #include <sys/dnode.h>
30 #include <sys/dmu.h>
31 #include <sys/dmu_tx.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dsl_dataset.h>
34 #include <sys/spa.h>
35 #include <sys/range_tree.h>
36 #include <sys/zfeature.h>
37
38 static void
39 dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx)
40 {
41 dmu_buf_impl_t *db;
42 int txgoff = tx->tx_txg & TXG_MASK;
43 int nblkptr = dn->dn_phys->dn_nblkptr;
44 int old_toplvl = dn->dn_phys->dn_nlevels - 1;
45 int new_level = dn->dn_next_nlevels[txgoff];
46 int i;
47
48 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
49
50 /* this dnode can't be paged out because it's dirty */
51 ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
52 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
53 ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0);
54
55 db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG);
56 ASSERT(db != NULL);
57
58 dn->dn_phys->dn_nlevels = new_level;
59 dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset,
60 dn->dn_object, dn->dn_phys->dn_nlevels);
61
62 /* check for existing blkptrs in the dnode */
63 for (i = 0; i < nblkptr; i++)
64 if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i]))
65 break;
66 if (i != nblkptr) {
67 /* transfer dnode's block pointers to new indirect block */
68 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT);
69 ASSERT(db->db.db_data);
70 ASSERT(arc_released(db->db_buf));
71 ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size);
72 bcopy(dn->dn_phys->dn_blkptr, db->db.db_data,
73 sizeof (blkptr_t) * nblkptr);
74 arc_buf_freeze(db->db_buf);
75 }
76
77 /* set dbuf's parent pointers to new indirect buf */
78 for (i = 0; i < nblkptr; i++) {
79 dmu_buf_impl_t *child = dbuf_find(dn, old_toplvl, i);
80
81 if (child == NULL)
82 continue;
83 #ifdef DEBUG
84 DB_DNODE_ENTER(child);
85 ASSERT3P(DB_DNODE(child), ==, dn);
86 DB_DNODE_EXIT(child);
87 #endif /* DEBUG */
88 if (child->db_parent && child->db_parent != dn->dn_dbuf) {
89 ASSERT(child->db_parent->db_level == db->db_level);
90 ASSERT(child->db_blkptr !=
91 &dn->dn_phys->dn_blkptr[child->db_blkid]);
92 mutex_exit(&child->db_mtx);
93 continue;
94 }
95 ASSERT(child->db_parent == NULL ||
96 child->db_parent == dn->dn_dbuf);
97
98 child->db_parent = db;
99 dbuf_add_ref(db, child);
100 if (db->db.db_data)
101 child->db_blkptr = (blkptr_t *)db->db.db_data + i;
102 else
103 child->db_blkptr = NULL;
104 dprintf_dbuf_bp(child, child->db_blkptr,
105 "changed db_blkptr to new indirect %s", "");
106
107 mutex_exit(&child->db_mtx);
108 }
109
110 bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr);
111
112 dbuf_rele(db, FTAG);
113
114 rw_exit(&dn->dn_struct_rwlock);
115 }
116
117 static void
118 free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx)
119 {
120 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
121 uint64_t bytesfreed = 0;
122 int i;
123
124 dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num);
125
126 for (i = 0; i < num; i++, bp++) {
127 uint64_t lsize, lvl;
128 dmu_object_type_t type;
129
130 if (BP_IS_HOLE(bp))
131 continue;
132
133 bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE);
134 ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys));
135
136 /*
137 * Save some useful information on the holes being
138 * punched, including logical size, type, and indirection
139 * level. Retaining birth time enables detection of when
140 * holes are punched for reducing the number of free
141 * records transmitted during a zfs send.
142 */
143
144 lsize = BP_GET_LSIZE(bp);
145 type = BP_GET_TYPE(bp);
146 lvl = BP_GET_LEVEL(bp);
147
148 bzero(bp, sizeof (blkptr_t));
149
150 if (spa_feature_is_active(dn->dn_objset->os_spa,
151 SPA_FEATURE_HOLE_BIRTH)) {
152 BP_SET_LSIZE(bp, lsize);
153 BP_SET_TYPE(bp, type);
154 BP_SET_LEVEL(bp, lvl);
155 BP_SET_BIRTH(bp, dmu_tx_get_txg(tx), 0);
156 }
157 }
158 dnode_diduse_space(dn, -bytesfreed);
159 }
160
161 #ifdef ZFS_DEBUG
162 static void
163 free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx)
164 {
165 int off, num;
166 int i, err, epbs;
167 uint64_t txg = tx->tx_txg;
168 dnode_t *dn;
169
170 DB_DNODE_ENTER(db);
171 dn = DB_DNODE(db);
172 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
173 off = start - (db->db_blkid * 1<<epbs);
174 num = end - start + 1;
175
176 ASSERT3U(off, >=, 0);
177 ASSERT3U(num, >=, 0);
178 ASSERT3U(db->db_level, >, 0);
179 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
180 ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT);
181 ASSERT(db->db_blkptr != NULL);
182
183 for (i = off; i < off+num; i++) {
184 uint64_t *buf;
185 dmu_buf_impl_t *child;
186 dbuf_dirty_record_t *dr;
187 int j;
188
189 ASSERT(db->db_level == 1);
190
191 rw_enter(&dn->dn_struct_rwlock, RW_READER);
192 err = dbuf_hold_impl(dn, db->db_level-1,
193 (db->db_blkid << epbs) + i, TRUE, FTAG, &child);
194 rw_exit(&dn->dn_struct_rwlock);
195 if (err == ENOENT)
196 continue;
197 ASSERT(err == 0);
198 ASSERT(child->db_level == 0);
199 dr = child->db_last_dirty;
200 while (dr && dr->dr_txg > txg)
201 dr = dr->dr_next;
202 ASSERT(dr == NULL || dr->dr_txg == txg);
203
204 /* data_old better be zeroed */
205 if (dr) {
206 buf = dr->dt.dl.dr_data->b_data;
207 for (j = 0; j < child->db.db_size >> 3; j++) {
208 if (buf[j] != 0) {
209 panic("freed data not zero: "
210 "child=%p i=%d off=%d num=%d\n",
211 (void *)child, i, off, num);
212 }
213 }
214 }
215
216 /*
217 * db_data better be zeroed unless it's dirty in a
218 * future txg.
219 */
220 mutex_enter(&child->db_mtx);
221 buf = child->db.db_data;
222 if (buf != NULL && child->db_state != DB_FILL &&
223 child->db_last_dirty == NULL) {
224 for (j = 0; j < child->db.db_size >> 3; j++) {
225 if (buf[j] != 0) {
226 panic("freed data not zero: "
227 "child=%p i=%d off=%d num=%d\n",
228 (void *)child, i, off, num);
229 }
230 }
231 }
232 mutex_exit(&child->db_mtx);
233
234 dbuf_rele(child, FTAG);
235 }
236 DB_DNODE_EXIT(db);
237 }
238 #endif
239
240 static void
241 free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks,
242 dmu_tx_t *tx)
243 {
244 dnode_t *dn;
245 blkptr_t *bp;
246 dmu_buf_impl_t *subdb;
247 uint64_t start, end, dbstart, dbend, i;
248 int epbs, shift;
249
250 /*
251 * There is a small possibility that this block will not be cached:
252 * 1 - if level > 1 and there are no children with level <= 1
253 * 2 - if this block was evicted since we read it from
254 * dmu_tx_hold_free().
255 */
256 if (db->db_state != DB_CACHED)
257 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
258
259 dbuf_release_bp(db);
260 bp = db->db.db_data;
261
262 DB_DNODE_ENTER(db);
263 dn = DB_DNODE(db);
264 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
265 shift = (db->db_level - 1) * epbs;
266 dbstart = db->db_blkid << epbs;
267 start = blkid >> shift;
268 if (dbstart < start) {
269 bp += start - dbstart;
270 } else {
271 start = dbstart;
272 }
273 dbend = ((db->db_blkid + 1) << epbs) - 1;
274 end = (blkid + nblks - 1) >> shift;
275 if (dbend <= end)
276 end = dbend;
277
278 ASSERT3U(start, <=, end);
279
280 if (db->db_level == 1) {
281 FREE_VERIFY(db, start, end, tx);
282 free_blocks(dn, bp, end-start+1, tx);
283 } else {
284 for (i = start; i <= end; i++, bp++) {
285 if (BP_IS_HOLE(bp))
286 continue;
287 rw_enter(&dn->dn_struct_rwlock, RW_READER);
288 VERIFY0(dbuf_hold_impl(dn, db->db_level - 1,
289 i, B_TRUE, FTAG, &subdb));
290 rw_exit(&dn->dn_struct_rwlock);
291 ASSERT3P(bp, ==, subdb->db_blkptr);
292
293 free_children(subdb, blkid, nblks, tx);
294 dbuf_rele(subdb, FTAG);
295 }
296 }
297
298 /* If this whole block is free, free ourself too. */
299 for (i = 0, bp = db->db.db_data; i < 1 << epbs; i++, bp++) {
300 if (!BP_IS_HOLE(bp))
301 break;
302 }
303 if (i == 1 << epbs) {
304 /* didn't find any non-holes */
305 bzero(db->db.db_data, db->db.db_size);
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 int i;
355
356 ASSERT(start < dn->dn_phys->dn_nblkptr);
357 bp += start;
358 for (i = start; i <= end; i++, bp++) {
359 if (BP_IS_HOLE(bp))
360 continue;
361 rw_enter(&dn->dn_struct_rwlock, RW_READER);
362 VERIFY0(dbuf_hold_impl(dn, dnlevel - 1, i,
363 TRUE, FTAG, &db));
364 rw_exit(&dn->dn_struct_rwlock);
365
366 free_children(db, blkid, nblks, tx);
367 dbuf_rele(db, FTAG);
368 }
369 }
370
371 if (trunc) {
372 ASSERTV(uint64_t off);
373 dn->dn_phys->dn_maxblkid = blkid == 0 ? 0 : blkid - 1;
374
375 ASSERTV(off = (dn->dn_phys->dn_maxblkid + 1) *
376 (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT));
377 ASSERT(off < dn->dn_phys->dn_maxblkid ||
378 dn->dn_phys->dn_maxblkid == 0 ||
379 dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
380 }
381 }
382
383 typedef struct dnode_sync_free_range_arg {
384 dnode_t *dsfra_dnode;
385 dmu_tx_t *dsfra_tx;
386 } dnode_sync_free_range_arg_t;
387
388 static void
389 dnode_sync_free_range(void *arg, uint64_t blkid, uint64_t nblks)
390 {
391 dnode_sync_free_range_arg_t *dsfra = arg;
392 dnode_t *dn = dsfra->dsfra_dnode;
393
394 mutex_exit(&dn->dn_mtx);
395 dnode_sync_free_range_impl(dn, blkid, nblks, dsfra->dsfra_tx);
396 mutex_enter(&dn->dn_mtx);
397 }
398
399 /*
400 * Try to kick all the dnode's dbufs out of the cache...
401 */
402 void
403 dnode_evict_dbufs(dnode_t *dn)
404 {
405 int progress;
406 int pass = 0;
407
408 do {
409 dmu_buf_impl_t *db, marker;
410 int evicting = FALSE;
411
412 progress = FALSE;
413 mutex_enter(&dn->dn_dbufs_mtx);
414 list_insert_tail(&dn->dn_dbufs, &marker);
415 db = list_head(&dn->dn_dbufs);
416 for (; db != &marker; db = list_head(&dn->dn_dbufs)) {
417 list_remove(&dn->dn_dbufs, db);
418 list_insert_tail(&dn->dn_dbufs, db);
419 #ifdef DEBUG
420 DB_DNODE_ENTER(db);
421 ASSERT3P(DB_DNODE(db), ==, dn);
422 DB_DNODE_EXIT(db);
423 #endif /* DEBUG */
424
425 mutex_enter(&db->db_mtx);
426 if (db->db_state == DB_EVICTING) {
427 progress = TRUE;
428 evicting = TRUE;
429 mutex_exit(&db->db_mtx);
430 } else if (refcount_is_zero(&db->db_holds)) {
431 progress = TRUE;
432 dbuf_clear(db); /* exits db_mtx for us */
433 } else {
434 mutex_exit(&db->db_mtx);
435 }
436
437 }
438 list_remove(&dn->dn_dbufs, &marker);
439 /*
440 * NB: we need to drop dn_dbufs_mtx between passes so
441 * that any DB_EVICTING dbufs can make progress.
442 * Ideally, we would have some cv we could wait on, but
443 * since we don't, just wait a bit to give the other
444 * thread a chance to run.
445 */
446 mutex_exit(&dn->dn_dbufs_mtx);
447 if (evicting)
448 delay(1);
449 pass++;
450 if ((pass % 100) == 0)
451 dprintf("Exceeded %d passes evicting dbufs\n", pass);
452 } while (progress);
453
454 if (pass >= 100)
455 dprintf("Required %d passes to evict dbufs\n", pass);
456
457 dnode_evict_bonus(dn);
458 }
459
460 void
461 dnode_evict_bonus(dnode_t *dn)
462 {
463 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
464 if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) {
465 mutex_enter(&dn->dn_bonus->db_mtx);
466 dbuf_evict(dn->dn_bonus);
467 dn->dn_bonus = NULL;
468 }
469 rw_exit(&dn->dn_struct_rwlock);
470 }
471
472 static void
473 dnode_undirty_dbufs(list_t *list)
474 {
475 dbuf_dirty_record_t *dr;
476
477 while ((dr = list_head(list))) {
478 dmu_buf_impl_t *db = dr->dr_dbuf;
479 uint64_t txg = dr->dr_txg;
480
481 if (db->db_level != 0)
482 dnode_undirty_dbufs(&dr->dt.di.dr_children);
483
484 mutex_enter(&db->db_mtx);
485 /* XXX - use dbuf_undirty()? */
486 list_remove(list, dr);
487 ASSERT(db->db_last_dirty == dr);
488 db->db_last_dirty = NULL;
489 db->db_dirtycnt -= 1;
490 if (db->db_level == 0) {
491 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
492 dr->dt.dl.dr_data == db->db_buf);
493 dbuf_unoverride(dr);
494 } else {
495 mutex_destroy(&dr->dt.di.dr_mtx);
496 list_destroy(&dr->dt.di.dr_children);
497 }
498 kmem_free(dr, sizeof (dbuf_dirty_record_t));
499 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
500 }
501 }
502
503 static void
504 dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
505 {
506 int txgoff = tx->tx_txg & TXG_MASK;
507
508 ASSERT(dmu_tx_is_syncing(tx));
509
510 /*
511 * Our contents should have been freed in dnode_sync() by the
512 * free range record inserted by the caller of dnode_free().
513 */
514 ASSERT0(DN_USED_BYTES(dn->dn_phys));
515 ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr));
516
517 dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
518 dnode_evict_dbufs(dn);
519 ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
520 ASSERT3P(dn->dn_bonus, ==, NULL);
521
522 /*
523 * XXX - It would be nice to assert this, but we may still
524 * have residual holds from async evictions from the arc...
525 *
526 * zfs_obj_to_path() also depends on this being
527 * commented out.
528 *
529 * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1);
530 */
531
532 /* Undirty next bits */
533 dn->dn_next_nlevels[txgoff] = 0;
534 dn->dn_next_indblkshift[txgoff] = 0;
535 dn->dn_next_blksz[txgoff] = 0;
536
537 /* ASSERT(blkptrs are zero); */
538 ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
539 ASSERT(dn->dn_type != DMU_OT_NONE);
540
541 ASSERT(dn->dn_free_txg > 0);
542 if (dn->dn_allocated_txg != dn->dn_free_txg)
543 dmu_buf_will_dirty(&dn->dn_dbuf->db, tx);
544 bzero(dn->dn_phys, sizeof (dnode_phys_t));
545
546 mutex_enter(&dn->dn_mtx);
547 dn->dn_type = DMU_OT_NONE;
548 dn->dn_maxblkid = 0;
549 dn->dn_allocated_txg = 0;
550 dn->dn_free_txg = 0;
551 dn->dn_have_spill = B_FALSE;
552 mutex_exit(&dn->dn_mtx);
553
554 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
555
556 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
557 /*
558 * Now that we've released our hold, the dnode may
559 * be evicted, so we musn't access it.
560 */
561 }
562
563 /*
564 * Write out the dnode's dirty buffers.
565 */
566 void
567 dnode_sync(dnode_t *dn, dmu_tx_t *tx)
568 {
569 dnode_phys_t *dnp = dn->dn_phys;
570 int txgoff = tx->tx_txg & TXG_MASK;
571 list_t *list = &dn->dn_dirty_records[txgoff];
572 boolean_t kill_spill = B_FALSE;
573 boolean_t freeing_dnode;
574 ASSERTV(static const dnode_phys_t zerodn = { 0 });
575
576 ASSERT(dmu_tx_is_syncing(tx));
577 ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
578 ASSERT(dnp->dn_type != DMU_OT_NONE ||
579 bcmp(dnp, &zerodn, DNODE_SIZE) == 0);
580 DNODE_VERIFY(dn);
581
582 ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
583
584 if (dmu_objset_userused_enabled(dn->dn_objset) &&
585 !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
586 mutex_enter(&dn->dn_mtx);
587 dn->dn_oldused = DN_USED_BYTES(dn->dn_phys);
588 dn->dn_oldflags = dn->dn_phys->dn_flags;
589 dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED;
590 mutex_exit(&dn->dn_mtx);
591 dmu_objset_userquota_get_ids(dn, B_FALSE, tx);
592 } else {
593 /* Once we account for it, we should always account for it. */
594 ASSERT(!(dn->dn_phys->dn_flags &
595 DNODE_FLAG_USERUSED_ACCOUNTED));
596 }
597
598 mutex_enter(&dn->dn_mtx);
599 if (dn->dn_allocated_txg == tx->tx_txg) {
600 /* The dnode is newly allocated or reallocated */
601 if (dnp->dn_type == DMU_OT_NONE) {
602 /* this is a first alloc, not a realloc */
603 dnp->dn_nlevels = 1;
604 dnp->dn_nblkptr = dn->dn_nblkptr;
605 }
606
607 dnp->dn_type = dn->dn_type;
608 dnp->dn_bonustype = dn->dn_bonustype;
609 dnp->dn_bonuslen = dn->dn_bonuslen;
610 }
611 ASSERT(dnp->dn_nlevels > 1 ||
612 BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
613 BP_IS_EMBEDDED(&dnp->dn_blkptr[0]) ||
614 BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
615 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
616 ASSERT(dnp->dn_nlevels < 2 ||
617 BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
618 BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 1 << dnp->dn_indblkshift);
619
620 if (dn->dn_next_type[txgoff] != 0) {
621 dnp->dn_type = dn->dn_type;
622 dn->dn_next_type[txgoff] = 0;
623 }
624
625 if (dn->dn_next_blksz[txgoff] != 0) {
626 ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
627 SPA_MINBLOCKSIZE) == 0);
628 ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
629 dn->dn_maxblkid == 0 || list_head(list) != NULL ||
630 dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
631 dnp->dn_datablkszsec ||
632 range_tree_space(dn->dn_free_ranges[txgoff]) != 0);
633 dnp->dn_datablkszsec =
634 dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
635 dn->dn_next_blksz[txgoff] = 0;
636 }
637
638 if (dn->dn_next_bonuslen[txgoff] != 0) {
639 if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
640 dnp->dn_bonuslen = 0;
641 else
642 dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
643 ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN);
644 dn->dn_next_bonuslen[txgoff] = 0;
645 }
646
647 if (dn->dn_next_bonustype[txgoff] != 0) {
648 ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff]));
649 dnp->dn_bonustype = dn->dn_next_bonustype[txgoff];
650 dn->dn_next_bonustype[txgoff] = 0;
651 }
652
653 freeing_dnode = dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg;
654
655 /*
656 * We will either remove a spill block when a file is being removed
657 * or we have been asked to remove it.
658 */
659 if (dn->dn_rm_spillblk[txgoff] ||
660 ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) && freeing_dnode)) {
661 if ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
662 kill_spill = B_TRUE;
663 dn->dn_rm_spillblk[txgoff] = 0;
664 }
665
666 if (dn->dn_next_indblkshift[txgoff] != 0) {
667 ASSERT(dnp->dn_nlevels == 1);
668 dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
669 dn->dn_next_indblkshift[txgoff] = 0;
670 }
671
672 /*
673 * Just take the live (open-context) values for checksum and compress.
674 * Strictly speaking it's a future leak, but nothing bad happens if we
675 * start using the new checksum or compress algorithm a little early.
676 */
677 dnp->dn_checksum = dn->dn_checksum;
678 dnp->dn_compress = dn->dn_compress;
679
680 mutex_exit(&dn->dn_mtx);
681
682 if (kill_spill) {
683 free_blocks(dn, &dn->dn_phys->dn_spill, 1, tx);
684 mutex_enter(&dn->dn_mtx);
685 dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR;
686 mutex_exit(&dn->dn_mtx);
687 }
688
689 /* process all the "freed" ranges in the file */
690 if (dn->dn_free_ranges[txgoff] != NULL) {
691 dnode_sync_free_range_arg_t dsfra;
692 dsfra.dsfra_dnode = dn;
693 dsfra.dsfra_tx = tx;
694 mutex_enter(&dn->dn_mtx);
695 range_tree_vacate(dn->dn_free_ranges[txgoff],
696 dnode_sync_free_range, &dsfra);
697 range_tree_destroy(dn->dn_free_ranges[txgoff]);
698 dn->dn_free_ranges[txgoff] = NULL;
699 mutex_exit(&dn->dn_mtx);
700 }
701
702 if (freeing_dnode) {
703 dnode_sync_free(dn, tx);
704 return;
705 }
706
707 if (dn->dn_next_nlevels[txgoff]) {
708 dnode_increase_indirection(dn, tx);
709 dn->dn_next_nlevels[txgoff] = 0;
710 }
711
712 if (dn->dn_next_nblkptr[txgoff]) {
713 /* this should only happen on a realloc */
714 ASSERT(dn->dn_allocated_txg == tx->tx_txg);
715 if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) {
716 /* zero the new blkptrs we are gaining */
717 bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
718 sizeof (blkptr_t) *
719 (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr));
720 #ifdef ZFS_DEBUG
721 } else {
722 int i;
723 ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr);
724 /* the blkptrs we are losing better be unallocated */
725 for (i = 0; i < dnp->dn_nblkptr; i++) {
726 if (i >= dn->dn_next_nblkptr[txgoff])
727 ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i]));
728 }
729 #endif
730 }
731 mutex_enter(&dn->dn_mtx);
732 dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff];
733 dn->dn_next_nblkptr[txgoff] = 0;
734 mutex_exit(&dn->dn_mtx);
735 }
736
737 dbuf_sync_list(list, tx);
738
739 if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
740 ASSERT3P(list_head(list), ==, NULL);
741 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
742 }
743
744 /*
745 * Although we have dropped our reference to the dnode, it
746 * can't be evicted until its written, and we haven't yet
747 * initiated the IO for the dnode's dbuf.
748 */
749 }