]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dnode_sync.c
OpenZFS 9577 - remove zfs_dbuf_evict_key tsd
[mirror_zfs.git] / module / zfs / dnode_sync.c
CommitLineData
34dc7c2f
BB
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 */
9ae529ec 21
34dc7c2f 22/*
428870ff 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
01937958 24 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
0c66c32d 25 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
34dc7c2f
BB
26 */
27
34dc7c2f
BB
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>
9bd274dd 36#include <sys/range_tree.h>
b0bc7a84 37#include <sys/zfeature.h>
34dc7c2f
BB
38
39static void
40dnode_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
bc77ba73
PD
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);
34dc7c2f
BB
71
72 /* set dbuf's parent pointers to new indirect buf */
73 for (i = 0; i < nblkptr; i++) {
6ebebace
JG
74 dmu_buf_impl_t *child =
75 dbuf_find(dn->dn_objset, dn->dn_object, old_toplvl, i);
34dc7c2f
BB
76
77 if (child == NULL)
78 continue;
572e2857
BB
79#ifdef DEBUG
80 DB_DNODE_ENTER(child);
81 ASSERT3P(DB_DNODE(child), ==, dn);
82 DB_DNODE_EXIT(child);
83#endif /* DEBUG */
34dc7c2f
BB
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
b0bc7a84 113static void
34dc7c2f
BB
114free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx)
115{
b128c09f 116 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
34dc7c2f 117 uint64_t bytesfreed = 0;
b0bc7a84 118 int i;
34dc7c2f 119
b128c09f 120 dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num);
34dc7c2f
BB
121
122 for (i = 0; i < num; i++, bp++) {
b0bc7a84
MG
123 uint64_t lsize, lvl;
124 dmu_object_type_t type;
125
34dc7c2f
BB
126 if (BP_IS_HOLE(bp))
127 continue;
128
428870ff 129 bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE);
34dc7c2f 130 ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys));
b0bc7a84
MG
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
34dc7c2f 144 bzero(bp, sizeof (blkptr_t));
b0bc7a84
MG
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 }
34dc7c2f
BB
153 }
154 dnode_diduse_space(dn, -bytesfreed);
155}
156
157#ifdef ZFS_DEBUG
158static void
159free_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;
572e2857 164 dnode_t *dn;
34dc7c2f 165
572e2857
BB
166 DB_DNODE_ENTER(db);
167 dn = DB_DNODE(db);
168 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
34dc7c2f
BB
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);
572e2857 175 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
34dc7c2f
BB
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
572e2857
BB
187 rw_enter(&dn->dn_struct_rwlock, RW_READER);
188 err = dbuf_hold_impl(dn, db->db_level-1,
fcff0f35 189 (db->db_blkid << epbs) + i, TRUE, FALSE, FTAG, &child);
572e2857 190 rw_exit(&dn->dn_struct_rwlock);
34dc7c2f
BB
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",
b128c09f 207 (void *)child, i, off, num);
34dc7c2f
BB
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",
b128c09f 224 (void *)child, i, off, num);
34dc7c2f
BB
225 }
226 }
227 }
228 mutex_exit(&child->db_mtx);
229
230 dbuf_rele(child, FTAG);
231 }
572e2857 232 DB_DNODE_EXIT(db);
34dc7c2f
BB
233}
234#endif
235
b0bc7a84
MG
236static void
237free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks,
34dc7c2f
BB
238 dmu_tx_t *tx)
239{
572e2857 240 dnode_t *dn;
34dc7c2f
BB
241 blkptr_t *bp;
242 dmu_buf_impl_t *subdb;
721ed0ee
GM
243 uint64_t start, end, dbstart, dbend;
244 unsigned int epbs, shift, i;
245 uint64_t id;
b128c09f
BB
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
b0bc7a84
MG
250 * 2 - if this block was evicted since we read it from
251 * dmu_tx_hold_free().
b128c09f
BB
252 */
253 if (db->db_state != DB_CACHED)
254 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
34dc7c2f 255
428870ff 256 dbuf_release_bp(db);
b0bc7a84 257 bp = db->db.db_data;
34dc7c2f 258
572e2857
BB
259 DB_DNODE_ENTER(db);
260 dn = DB_DNODE(db);
261 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
721ed0ee 262 ASSERT3U(epbs, <, 31);
34dc7c2f
BB
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;
34dc7c2f
BB
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;
b0bc7a84 275
34dc7c2f
BB
276 ASSERT3U(start, <=, end);
277
278 if (db->db_level == 1) {
279 FREE_VERIFY(db, start, end, tx);
b0bc7a84
MG
280 free_blocks(dn, bp, end-start+1, tx);
281 } else {
721ed0ee 282 for (id = start; id <= end; id++, bp++) {
b0bc7a84
MG
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,
721ed0ee 287 id, TRUE, FALSE, FTAG, &subdb));
b0bc7a84
MG
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 }
34dc7c2f
BB
294 }
295
b0bc7a84 296 /* If this whole block is free, free ourself too. */
f4bae2ed 297 for (i = 0, bp = db->db.db_data; i < 1ULL << epbs; i++, bp++) {
b0bc7a84
MG
298 if (!BP_IS_HOLE(bp))
299 break;
300 }
301 if (i == 1 << epbs) {
721ed0ee
GM
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);
b0bc7a84 308 bzero(db->db.db_data, db->db.db_size);
721ed0ee 309 rw_exit(&dn->dn_struct_rwlock);
b0bc7a84
MG
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);
34dc7c2f 317 }
b0bc7a84 318
572e2857 319 DB_DNODE_EXIT(db);
34dc7c2f 320 arc_buf_freeze(db->db_buf);
34dc7c2f
BB
321}
322
323/*
d3cc8b15 324 * Traverse the indicated range of the provided file
34dc7c2f
BB
325 * and "free" all the blocks contained there.
326 */
327static void
9bd274dd 328dnode_sync_free_range_impl(dnode_t *dn, uint64_t blkid, uint64_t nblks,
b0bc7a84 329 dmu_tx_t *tx)
34dc7c2f
BB
330{
331 blkptr_t *bp = dn->dn_phys->dn_blkptr;
34dc7c2f 332 int dnlevel = dn->dn_phys->dn_nlevels;
b0bc7a84 333 boolean_t trunc = B_FALSE;
34dc7c2f
BB
334
335 if (blkid > dn->dn_phys->dn_maxblkid)
336 return;
337
338 ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX);
b0bc7a84 339 if (blkid + nblks > dn->dn_phys->dn_maxblkid) {
34dc7c2f 340 nblks = dn->dn_phys->dn_maxblkid - blkid + 1;
b0bc7a84
MG
341 trunc = B_TRUE;
342 }
34dc7c2f
BB
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);
b0bc7a84
MG
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,
fcff0f35 367 TRUE, FALSE, FTAG, &db));
b0bc7a84
MG
368 rw_exit(&dn->dn_struct_rwlock);
369
370 free_children(db, blkid, nblks, tx);
371 dbuf_rele(db, FTAG);
34dc7c2f 372 }
34dc7c2f
BB
373 }
374
34dc7c2f 375 if (trunc) {
b0bc7a84
MG
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) *
1fde1e37 380 (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT));
34dc7c2f
BB
381 ASSERT(off < dn->dn_phys->dn_maxblkid ||
382 dn->dn_phys->dn_maxblkid == 0 ||
b128c09f 383 dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
34dc7c2f
BB
384 }
385}
386
9bd274dd
MA
387typedef 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
392static void
393dnode_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
34dc7c2f 403/*
d3cc8b15 404 * Try to kick all the dnode's dbufs out of the cache...
34dc7c2f
BB
405 */
406void
407dnode_evict_dbufs(dnode_t *dn)
408{
0c66c32d
JG
409 dmu_buf_impl_t *db_marker;
410 dmu_buf_impl_t *db, *db_next;
34dc7c2f 411
0c66c32d
JG
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) {
34dc7c2f 416
572e2857 417#ifdef DEBUG
0c66c32d
JG
418 DB_DNODE_ENTER(db);
419 ASSERT3P(DB_DNODE(db), ==, dn);
420 DB_DNODE_EXIT(db);
572e2857 421#endif /* DEBUG */
34dc7c2f 422
0c66c32d
JG
423 mutex_enter(&db->db_mtx);
424 if (db->db_state != DB_EVICTING &&
b884768e 425 zfs_refcount_is_zero(&db->db_holds)) {
0c66c32d
JG
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
01937958
MA
432 /*
433 * We need to use the "marker" dbuf rather than
434 * simply getting the next dbuf, because
435 * dbuf_destroy() may actually remove multiple dbufs.
436 * It can call itself recursively on the parent dbuf,
437 * which may also be removed from dn_dbufs. The code
438 * flow would look like:
439 *
440 * dbuf_destroy():
441 * dnode_rele_and_unlock(parent_dbuf, evicting=TRUE):
442 * if (!cacheable || pending_evict)
443 * dbuf_destroy()
444 */
d3c2ae1c 445 dbuf_destroy(db);
0c66c32d
JG
446
447 db_next = AVL_NEXT(&dn->dn_dbufs, db_marker);
448 avl_remove(&dn->dn_dbufs, db_marker);
449 } else {
bc4501f7 450 db->db_pending_evict = TRUE;
0c66c32d
JG
451 mutex_exit(&db->db_mtx);
452 db_next = AVL_NEXT(&dn->dn_dbufs, db);
34dc7c2f 453 }
0c66c32d
JG
454 }
455 mutex_exit(&dn->dn_dbufs_mtx);
34dc7c2f 456
0c66c32d 457 kmem_free(db_marker, sizeof (dmu_buf_impl_t));
754c6663 458
4c7b7eed
JG
459 dnode_evict_bonus(dn);
460}
461
462void
463dnode_evict_bonus(dnode_t *dn)
464{
34dc7c2f 465 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
bc4501f7 466 if (dn->dn_bonus != NULL) {
b884768e 467 if (zfs_refcount_is_zero(&dn->dn_bonus->db_holds)) {
bc4501f7 468 mutex_enter(&dn->dn_bonus->db_mtx);
d3c2ae1c 469 dbuf_destroy(dn->dn_bonus);
bc4501f7
JG
470 dn->dn_bonus = NULL;
471 } else {
472 dn->dn_bonus->db_pending_evict = TRUE;
473 }
34dc7c2f
BB
474 }
475 rw_exit(&dn->dn_struct_rwlock);
476}
477
478static void
479dnode_undirty_dbufs(list_t *list)
480{
481 dbuf_dirty_record_t *dr;
482
c65aa5b2 483 while ((dr = list_head(list))) {
34dc7c2f
BB
484 dmu_buf_impl_t *db = dr->dr_dbuf;
485 uint64_t txg = dr->dr_txg;
486
428870ff
BB
487 if (db->db_level != 0)
488 dnode_undirty_dbufs(&dr->dt.di.dr_children);
489
34dc7c2f
BB
490 mutex_enter(&db->db_mtx);
491 /* XXX - use dbuf_undirty()? */
492 list_remove(list, dr);
493 ASSERT(db->db_last_dirty == dr);
494 db->db_last_dirty = NULL;
495 db->db_dirtycnt -= 1;
496 if (db->db_level == 0) {
428870ff 497 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
34dc7c2f
BB
498 dr->dt.dl.dr_data == db->db_buf);
499 dbuf_unoverride(dr);
58c4aa00
JL
500 } else {
501 mutex_destroy(&dr->dt.di.dr_mtx);
502 list_destroy(&dr->dt.di.dr_children);
34dc7c2f
BB
503 }
504 kmem_free(dr, sizeof (dbuf_dirty_record_t));
01937958 505 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg, B_FALSE);
34dc7c2f
BB
506 }
507}
508
509static void
510dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
511{
512 int txgoff = tx->tx_txg & TXG_MASK;
513
514 ASSERT(dmu_tx_is_syncing(tx));
515
b128c09f
BB
516 /*
517 * Our contents should have been freed in dnode_sync() by the
518 * free range record inserted by the caller of dnode_free().
519 */
c99c9001 520 ASSERT0(DN_USED_BYTES(dn->dn_phys));
b128c09f
BB
521 ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr));
522
34dc7c2f
BB
523 dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
524 dnode_evict_dbufs(dn);
34dc7c2f
BB
525
526 /*
527 * XXX - It would be nice to assert this, but we may still
528 * have residual holds from async evictions from the arc...
529 *
530 * zfs_obj_to_path() also depends on this being
531 * commented out.
532 *
b884768e 533 * ASSERT3U(zfs_refcount_count(&dn->dn_holds), ==, 1);
34dc7c2f
BB
534 */
535
536 /* Undirty next bits */
537 dn->dn_next_nlevels[txgoff] = 0;
538 dn->dn_next_indblkshift[txgoff] = 0;
539 dn->dn_next_blksz[txgoff] = 0;
540
34dc7c2f
BB
541 /* ASSERT(blkptrs are zero); */
542 ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
543 ASSERT(dn->dn_type != DMU_OT_NONE);
544
545 ASSERT(dn->dn_free_txg > 0);
546 if (dn->dn_allocated_txg != dn->dn_free_txg)
b0bc7a84 547 dmu_buf_will_dirty(&dn->dn_dbuf->db, tx);
50c957f7 548 bzero(dn->dn_phys, sizeof (dnode_phys_t) * dn->dn_num_slots);
d2c8103a 549 dnode_free_interior_slots(dn);
34dc7c2f
BB
550
551 mutex_enter(&dn->dn_mtx);
552 dn->dn_type = DMU_OT_NONE;
553 dn->dn_maxblkid = 0;
554 dn->dn_allocated_txg = 0;
555 dn->dn_free_txg = 0;
428870ff 556 dn->dn_have_spill = B_FALSE;
d2c8103a 557 dn->dn_num_slots = 1;
34dc7c2f
BB
558 mutex_exit(&dn->dn_mtx);
559
560 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
561
562 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
563 /*
564 * Now that we've released our hold, the dnode may
4e33ba4c 565 * be evicted, so we mustn't access it.
34dc7c2f
BB
566 */
567}
568
569/*
570 * Write out the dnode's dirty buffers.
34dc7c2f
BB
571 */
572void
573dnode_sync(dnode_t *dn, dmu_tx_t *tx)
574{
34dc7c2f
BB
575 dnode_phys_t *dnp = dn->dn_phys;
576 int txgoff = tx->tx_txg & TXG_MASK;
577 list_t *list = &dn->dn_dirty_records[txgoff];
428870ff 578 boolean_t kill_spill = B_FALSE;
b0bc7a84 579 boolean_t freeing_dnode;
1fde1e37 580 ASSERTV(static const dnode_phys_t zerodn = { 0 });
34dc7c2f
BB
581
582 ASSERT(dmu_tx_is_syncing(tx));
583 ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
9babb374 584 ASSERT(dnp->dn_type != DMU_OT_NONE ||
50c957f7 585 bcmp(dnp, &zerodn, DNODE_MIN_SIZE) == 0);
34dc7c2f
BB
586 DNODE_VERIFY(dn);
587
588 ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
589
9babb374
BB
590 if (dmu_objset_userused_enabled(dn->dn_objset) &&
591 !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
428870ff
BB
592 mutex_enter(&dn->dn_mtx);
593 dn->dn_oldused = DN_USED_BYTES(dn->dn_phys);
594 dn->dn_oldflags = dn->dn_phys->dn_flags;
9babb374 595 dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED;
1de321e6
JX
596 if (dmu_objset_userobjused_enabled(dn->dn_objset))
597 dn->dn_phys->dn_flags |=
598 DNODE_FLAG_USEROBJUSED_ACCOUNTED;
428870ff
BB
599 mutex_exit(&dn->dn_mtx);
600 dmu_objset_userquota_get_ids(dn, B_FALSE, tx);
9babb374
BB
601 } else {
602 /* Once we account for it, we should always account for it. */
603 ASSERT(!(dn->dn_phys->dn_flags &
604 DNODE_FLAG_USERUSED_ACCOUNTED));
1de321e6
JX
605 ASSERT(!(dn->dn_phys->dn_flags &
606 DNODE_FLAG_USEROBJUSED_ACCOUNTED));
9babb374
BB
607 }
608
34dc7c2f
BB
609 mutex_enter(&dn->dn_mtx);
610 if (dn->dn_allocated_txg == tx->tx_txg) {
611 /* The dnode is newly allocated or reallocated */
612 if (dnp->dn_type == DMU_OT_NONE) {
613 /* this is a first alloc, not a realloc */
34dc7c2f 614 dnp->dn_nlevels = 1;
d164b209 615 dnp->dn_nblkptr = dn->dn_nblkptr;
34dc7c2f
BB
616 }
617
34dc7c2f
BB
618 dnp->dn_type = dn->dn_type;
619 dnp->dn_bonustype = dn->dn_bonustype;
620 dnp->dn_bonuslen = dn->dn_bonuslen;
34dc7c2f 621 }
50c957f7
NB
622
623 dnp->dn_extra_slots = dn->dn_num_slots - 1;
624
34dc7c2f
BB
625 ASSERT(dnp->dn_nlevels > 1 ||
626 BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
9b67f605 627 BP_IS_EMBEDDED(&dnp->dn_blkptr[0]) ||
34dc7c2f
BB
628 BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
629 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
9b67f605
MA
630 ASSERT(dnp->dn_nlevels < 2 ||
631 BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
632 BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 1 << dnp->dn_indblkshift);
34dc7c2f 633
fa86b5db
MA
634 if (dn->dn_next_type[txgoff] != 0) {
635 dnp->dn_type = dn->dn_type;
636 dn->dn_next_type[txgoff] = 0;
637 }
638
639 if (dn->dn_next_blksz[txgoff] != 0) {
34dc7c2f
BB
640 ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
641 SPA_MINBLOCKSIZE) == 0);
642 ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
b128c09f 643 dn->dn_maxblkid == 0 || list_head(list) != NULL ||
34dc7c2f 644 dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
9bd274dd
MA
645 dnp->dn_datablkszsec ||
646 range_tree_space(dn->dn_free_ranges[txgoff]) != 0);
34dc7c2f
BB
647 dnp->dn_datablkszsec =
648 dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
649 dn->dn_next_blksz[txgoff] = 0;
650 }
651
fa86b5db 652 if (dn->dn_next_bonuslen[txgoff] != 0) {
34dc7c2f
BB
653 if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
654 dnp->dn_bonuslen = 0;
655 else
656 dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
50c957f7
NB
657 ASSERT(dnp->dn_bonuslen <=
658 DN_SLOTS_TO_BONUSLEN(dnp->dn_extra_slots + 1));
34dc7c2f
BB
659 dn->dn_next_bonuslen[txgoff] = 0;
660 }
661
fa86b5db 662 if (dn->dn_next_bonustype[txgoff] != 0) {
9ae529ec 663 ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff]));
428870ff
BB
664 dnp->dn_bonustype = dn->dn_next_bonustype[txgoff];
665 dn->dn_next_bonustype[txgoff] = 0;
666 }
667
b0bc7a84
MG
668 freeing_dnode = dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg;
669
428870ff 670 /*
08dc1b2d
MA
671 * Remove the spill block if we have been explicitly asked to
672 * remove it, or if the object is being removed.
428870ff 673 */
08dc1b2d
MA
674 if (dn->dn_rm_spillblk[txgoff] || freeing_dnode) {
675 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
428870ff
BB
676 kill_spill = B_TRUE;
677 dn->dn_rm_spillblk[txgoff] = 0;
678 }
679
fa86b5db 680 if (dn->dn_next_indblkshift[txgoff] != 0) {
34dc7c2f
BB
681 ASSERT(dnp->dn_nlevels == 1);
682 dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
683 dn->dn_next_indblkshift[txgoff] = 0;
684 }
685
686 /*
687 * Just take the live (open-context) values for checksum and compress.
688 * Strictly speaking it's a future leak, but nothing bad happens if we
689 * start using the new checksum or compress algorithm a little early.
690 */
691 dnp->dn_checksum = dn->dn_checksum;
692 dnp->dn_compress = dn->dn_compress;
693
694 mutex_exit(&dn->dn_mtx);
695
428870ff 696 if (kill_spill) {
50c957f7 697 free_blocks(dn, DN_SPILL_BLKPTR(dn->dn_phys), 1, tx);
428870ff
BB
698 mutex_enter(&dn->dn_mtx);
699 dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR;
700 mutex_exit(&dn->dn_mtx);
701 }
702
34dc7c2f 703 /* process all the "freed" ranges in the file */
9bd274dd
MA
704 if (dn->dn_free_ranges[txgoff] != NULL) {
705 dnode_sync_free_range_arg_t dsfra;
706 dsfra.dsfra_dnode = dn;
707 dsfra.dsfra_tx = tx;
b128c09f 708 mutex_enter(&dn->dn_mtx);
9bd274dd
MA
709 range_tree_vacate(dn->dn_free_ranges[txgoff],
710 dnode_sync_free_range, &dsfra);
711 range_tree_destroy(dn->dn_free_ranges[txgoff]);
712 dn->dn_free_ranges[txgoff] = NULL;
b128c09f 713 mutex_exit(&dn->dn_mtx);
34dc7c2f 714 }
34dc7c2f 715
b0bc7a84 716 if (freeing_dnode) {
68cbd56e 717 dn->dn_objset->os_freed_dnodes++;
34dc7c2f
BB
718 dnode_sync_free(dn, tx);
719 return;
720 }
721
50c957f7
NB
722 if (dn->dn_num_slots > DNODE_MIN_SLOTS) {
723 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
724 mutex_enter(&ds->ds_lock);
725 ds->ds_feature_activation_needed[SPA_FEATURE_LARGE_DNODE] =
726 B_TRUE;
727 mutex_exit(&ds->ds_lock);
728 }
729
4f68d787
GW
730 if (dn->dn_next_nlevels[txgoff]) {
731 dnode_increase_indirection(dn, tx);
732 dn->dn_next_nlevels[txgoff] = 0;
733 }
734
d164b209
BB
735 if (dn->dn_next_nblkptr[txgoff]) {
736 /* this should only happen on a realloc */
737 ASSERT(dn->dn_allocated_txg == tx->tx_txg);
738 if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) {
739 /* zero the new blkptrs we are gaining */
740 bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
741 sizeof (blkptr_t) *
742 (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr));
743#ifdef ZFS_DEBUG
744 } else {
745 int i;
746 ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr);
747 /* the blkptrs we are losing better be unallocated */
37f000c5
NB
748 for (i = 0; i < dnp->dn_nblkptr; i++) {
749 if (i >= dn->dn_next_nblkptr[txgoff])
750 ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i]));
751 }
d164b209
BB
752#endif
753 }
754 mutex_enter(&dn->dn_mtx);
755 dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff];
756 dn->dn_next_nblkptr[txgoff] = 0;
757 mutex_exit(&dn->dn_mtx);
758 }
759
4bda3bd0 760 dbuf_sync_list(list, dn->dn_phys->dn_nlevels - 1, tx);
34dc7c2f 761
9babb374 762 if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
34dc7c2f
BB
763 ASSERT3P(list_head(list), ==, NULL);
764 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
765 }
766
767 /*
768 * Although we have dropped our reference to the dnode, it
769 * can't be evicted until its written, and we haven't yet
770 * initiated the IO for the dnode's dbuf.
771 */
772}