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