]> git.proxmox.com Git - mirror_zfs-debian.git/blob - module/zfs/dnode_sync.c
Imported Upstream version 0.6.4.2
[mirror_zfs-debian.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 }
495 kmem_free(dr, sizeof (dbuf_dirty_record_t));
496 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
497 }
498 }
499
500 static void
501 dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
502 {
503 int txgoff = tx->tx_txg & TXG_MASK;
504
505 ASSERT(dmu_tx_is_syncing(tx));
506
507 /*
508 * Our contents should have been freed in dnode_sync() by the
509 * free range record inserted by the caller of dnode_free().
510 */
511 ASSERT0(DN_USED_BYTES(dn->dn_phys));
512 ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr));
513
514 dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
515 dnode_evict_dbufs(dn);
516 ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
517 ASSERT3P(dn->dn_bonus, ==, NULL);
518
519 /*
520 * XXX - It would be nice to assert this, but we may still
521 * have residual holds from async evictions from the arc...
522 *
523 * zfs_obj_to_path() also depends on this being
524 * commented out.
525 *
526 * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1);
527 */
528
529 /* Undirty next bits */
530 dn->dn_next_nlevels[txgoff] = 0;
531 dn->dn_next_indblkshift[txgoff] = 0;
532 dn->dn_next_blksz[txgoff] = 0;
533
534 /* ASSERT(blkptrs are zero); */
535 ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
536 ASSERT(dn->dn_type != DMU_OT_NONE);
537
538 ASSERT(dn->dn_free_txg > 0);
539 if (dn->dn_allocated_txg != dn->dn_free_txg)
540 dmu_buf_will_dirty(&dn->dn_dbuf->db, tx);
541 bzero(dn->dn_phys, sizeof (dnode_phys_t));
542
543 mutex_enter(&dn->dn_mtx);
544 dn->dn_type = DMU_OT_NONE;
545 dn->dn_maxblkid = 0;
546 dn->dn_allocated_txg = 0;
547 dn->dn_free_txg = 0;
548 dn->dn_have_spill = B_FALSE;
549 mutex_exit(&dn->dn_mtx);
550
551 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
552
553 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
554 /*
555 * Now that we've released our hold, the dnode may
556 * be evicted, so we musn't access it.
557 */
558 }
559
560 /*
561 * Write out the dnode's dirty buffers.
562 */
563 void
564 dnode_sync(dnode_t *dn, dmu_tx_t *tx)
565 {
566 dnode_phys_t *dnp = dn->dn_phys;
567 int txgoff = tx->tx_txg & TXG_MASK;
568 list_t *list = &dn->dn_dirty_records[txgoff];
569 boolean_t kill_spill = B_FALSE;
570 boolean_t freeing_dnode;
571 ASSERTV(static const dnode_phys_t zerodn = { 0 });
572
573 ASSERT(dmu_tx_is_syncing(tx));
574 ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
575 ASSERT(dnp->dn_type != DMU_OT_NONE ||
576 bcmp(dnp, &zerodn, DNODE_SIZE) == 0);
577 DNODE_VERIFY(dn);
578
579 ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
580
581 if (dmu_objset_userused_enabled(dn->dn_objset) &&
582 !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
583 mutex_enter(&dn->dn_mtx);
584 dn->dn_oldused = DN_USED_BYTES(dn->dn_phys);
585 dn->dn_oldflags = dn->dn_phys->dn_flags;
586 dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED;
587 mutex_exit(&dn->dn_mtx);
588 dmu_objset_userquota_get_ids(dn, B_FALSE, tx);
589 } else {
590 /* Once we account for it, we should always account for it. */
591 ASSERT(!(dn->dn_phys->dn_flags &
592 DNODE_FLAG_USERUSED_ACCOUNTED));
593 }
594
595 mutex_enter(&dn->dn_mtx);
596 if (dn->dn_allocated_txg == tx->tx_txg) {
597 /* The dnode is newly allocated or reallocated */
598 if (dnp->dn_type == DMU_OT_NONE) {
599 /* this is a first alloc, not a realloc */
600 dnp->dn_nlevels = 1;
601 dnp->dn_nblkptr = dn->dn_nblkptr;
602 }
603
604 dnp->dn_type = dn->dn_type;
605 dnp->dn_bonustype = dn->dn_bonustype;
606 dnp->dn_bonuslen = dn->dn_bonuslen;
607 }
608 ASSERT(dnp->dn_nlevels > 1 ||
609 BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
610 BP_IS_EMBEDDED(&dnp->dn_blkptr[0]) ||
611 BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
612 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
613 ASSERT(dnp->dn_nlevels < 2 ||
614 BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
615 BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 1 << dnp->dn_indblkshift);
616
617 if (dn->dn_next_type[txgoff] != 0) {
618 dnp->dn_type = dn->dn_type;
619 dn->dn_next_type[txgoff] = 0;
620 }
621
622 if (dn->dn_next_blksz[txgoff] != 0) {
623 ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
624 SPA_MINBLOCKSIZE) == 0);
625 ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
626 dn->dn_maxblkid == 0 || list_head(list) != NULL ||
627 dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
628 dnp->dn_datablkszsec ||
629 range_tree_space(dn->dn_free_ranges[txgoff]) != 0);
630 dnp->dn_datablkszsec =
631 dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
632 dn->dn_next_blksz[txgoff] = 0;
633 }
634
635 if (dn->dn_next_bonuslen[txgoff] != 0) {
636 if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
637 dnp->dn_bonuslen = 0;
638 else
639 dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
640 ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN);
641 dn->dn_next_bonuslen[txgoff] = 0;
642 }
643
644 if (dn->dn_next_bonustype[txgoff] != 0) {
645 ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff]));
646 dnp->dn_bonustype = dn->dn_next_bonustype[txgoff];
647 dn->dn_next_bonustype[txgoff] = 0;
648 }
649
650 freeing_dnode = dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg;
651
652 /*
653 * We will either remove a spill block when a file is being removed
654 * or we have been asked to remove it.
655 */
656 if (dn->dn_rm_spillblk[txgoff] ||
657 ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) && freeing_dnode)) {
658 if ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
659 kill_spill = B_TRUE;
660 dn->dn_rm_spillblk[txgoff] = 0;
661 }
662
663 if (dn->dn_next_indblkshift[txgoff] != 0) {
664 ASSERT(dnp->dn_nlevels == 1);
665 dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
666 dn->dn_next_indblkshift[txgoff] = 0;
667 }
668
669 /*
670 * Just take the live (open-context) values for checksum and compress.
671 * Strictly speaking it's a future leak, but nothing bad happens if we
672 * start using the new checksum or compress algorithm a little early.
673 */
674 dnp->dn_checksum = dn->dn_checksum;
675 dnp->dn_compress = dn->dn_compress;
676
677 mutex_exit(&dn->dn_mtx);
678
679 if (kill_spill) {
680 free_blocks(dn, &dn->dn_phys->dn_spill, 1, tx);
681 mutex_enter(&dn->dn_mtx);
682 dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR;
683 mutex_exit(&dn->dn_mtx);
684 }
685
686 /* process all the "freed" ranges in the file */
687 if (dn->dn_free_ranges[txgoff] != NULL) {
688 dnode_sync_free_range_arg_t dsfra;
689 dsfra.dsfra_dnode = dn;
690 dsfra.dsfra_tx = tx;
691 mutex_enter(&dn->dn_mtx);
692 range_tree_vacate(dn->dn_free_ranges[txgoff],
693 dnode_sync_free_range, &dsfra);
694 range_tree_destroy(dn->dn_free_ranges[txgoff]);
695 dn->dn_free_ranges[txgoff] = NULL;
696 mutex_exit(&dn->dn_mtx);
697 }
698
699 if (freeing_dnode) {
700 dnode_sync_free(dn, tx);
701 return;
702 }
703
704 if (dn->dn_next_nlevels[txgoff]) {
705 dnode_increase_indirection(dn, tx);
706 dn->dn_next_nlevels[txgoff] = 0;
707 }
708
709 if (dn->dn_next_nblkptr[txgoff]) {
710 /* this should only happen on a realloc */
711 ASSERT(dn->dn_allocated_txg == tx->tx_txg);
712 if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) {
713 /* zero the new blkptrs we are gaining */
714 bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
715 sizeof (blkptr_t) *
716 (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr));
717 #ifdef ZFS_DEBUG
718 } else {
719 int i;
720 ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr);
721 /* the blkptrs we are losing better be unallocated */
722 for (i = 0; i < dnp->dn_nblkptr; i++) {
723 if (i >= dn->dn_next_nblkptr[txgoff])
724 ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i]));
725 }
726 #endif
727 }
728 mutex_enter(&dn->dn_mtx);
729 dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff];
730 dn->dn_next_nblkptr[txgoff] = 0;
731 mutex_exit(&dn->dn_mtx);
732 }
733
734 dbuf_sync_list(list, tx);
735
736 if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
737 ASSERT3P(list_head(list), ==, NULL);
738 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
739 }
740
741 /*
742 * Although we have dropped our reference to the dnode, it
743 * can't be evicted until its written, and we haven't yet
744 * initiated the IO for the dnode's dbuf.
745 */
746 }