]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dnode_sync.c
Introduce ARC Buffer Data (ABD)
[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.
bc77ba73 24 * Copyright (c) 2012, 2016 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;
243 uint64_t start, end, dbstart, dbend, i;
b0bc7a84 244 int epbs, shift;
b128c09f
BB
245
246 /*
247 * There is a small possibility that this block will not be cached:
248 * 1 - if level > 1 and there are no children with level <= 1
b0bc7a84
MG
249 * 2 - if this block was evicted since we read it from
250 * dmu_tx_hold_free().
b128c09f
BB
251 */
252 if (db->db_state != DB_CACHED)
253 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
34dc7c2f 254
428870ff 255 dbuf_release_bp(db);
b0bc7a84 256 bp = db->db.db_data;
34dc7c2f 257
572e2857
BB
258 DB_DNODE_ENTER(db);
259 dn = DB_DNODE(db);
260 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
34dc7c2f
BB
261 shift = (db->db_level - 1) * epbs;
262 dbstart = db->db_blkid << epbs;
263 start = blkid >> shift;
264 if (dbstart < start) {
265 bp += start - dbstart;
34dc7c2f
BB
266 } else {
267 start = dbstart;
268 }
269 dbend = ((db->db_blkid + 1) << epbs) - 1;
270 end = (blkid + nblks - 1) >> shift;
271 if (dbend <= end)
272 end = dbend;
b0bc7a84 273
34dc7c2f
BB
274 ASSERT3U(start, <=, end);
275
276 if (db->db_level == 1) {
277 FREE_VERIFY(db, start, end, tx);
b0bc7a84
MG
278 free_blocks(dn, bp, end-start+1, tx);
279 } else {
280 for (i = start; i <= end; i++, bp++) {
281 if (BP_IS_HOLE(bp))
282 continue;
283 rw_enter(&dn->dn_struct_rwlock, RW_READER);
284 VERIFY0(dbuf_hold_impl(dn, db->db_level - 1,
fcff0f35 285 i, TRUE, FALSE, FTAG, &subdb));
b0bc7a84
MG
286 rw_exit(&dn->dn_struct_rwlock);
287 ASSERT3P(bp, ==, subdb->db_blkptr);
288
289 free_children(subdb, blkid, nblks, tx);
290 dbuf_rele(subdb, FTAG);
291 }
34dc7c2f
BB
292 }
293
b0bc7a84 294 /* If this whole block is free, free ourself too. */
f4bae2ed 295 for (i = 0, bp = db->db.db_data; i < 1ULL << epbs; i++, bp++) {
b0bc7a84
MG
296 if (!BP_IS_HOLE(bp))
297 break;
298 }
299 if (i == 1 << epbs) {
300 /* didn't find any non-holes */
301 bzero(db->db.db_data, db->db.db_size);
302 free_blocks(dn, db->db_blkptr, 1, tx);
303 } else {
304 /*
305 * Partial block free; must be marked dirty so that it
306 * will be written out.
307 */
308 ASSERT(db->db_dirtycnt > 0);
34dc7c2f 309 }
b0bc7a84 310
572e2857 311 DB_DNODE_EXIT(db);
34dc7c2f 312 arc_buf_freeze(db->db_buf);
34dc7c2f
BB
313}
314
315/*
d3cc8b15 316 * Traverse the indicated range of the provided file
34dc7c2f
BB
317 * and "free" all the blocks contained there.
318 */
319static void
9bd274dd 320dnode_sync_free_range_impl(dnode_t *dn, uint64_t blkid, uint64_t nblks,
b0bc7a84 321 dmu_tx_t *tx)
34dc7c2f
BB
322{
323 blkptr_t *bp = dn->dn_phys->dn_blkptr;
34dc7c2f 324 int dnlevel = dn->dn_phys->dn_nlevels;
b0bc7a84 325 boolean_t trunc = B_FALSE;
34dc7c2f
BB
326
327 if (blkid > dn->dn_phys->dn_maxblkid)
328 return;
329
330 ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX);
b0bc7a84 331 if (blkid + nblks > dn->dn_phys->dn_maxblkid) {
34dc7c2f 332 nblks = dn->dn_phys->dn_maxblkid - blkid + 1;
b0bc7a84
MG
333 trunc = B_TRUE;
334 }
34dc7c2f
BB
335
336 /* There are no indirect blocks in the object */
337 if (dnlevel == 1) {
338 if (blkid >= dn->dn_phys->dn_nblkptr) {
339 /* this range was never made persistent */
340 return;
341 }
342 ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr);
b0bc7a84
MG
343 free_blocks(dn, bp + blkid, nblks, tx);
344 } else {
345 int shift = (dnlevel - 1) *
346 (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT);
347 int start = blkid >> shift;
348 int end = (blkid + nblks - 1) >> shift;
349 dmu_buf_impl_t *db;
350 int i;
351
352 ASSERT(start < dn->dn_phys->dn_nblkptr);
353 bp += start;
354 for (i = start; i <= end; i++, bp++) {
355 if (BP_IS_HOLE(bp))
356 continue;
357 rw_enter(&dn->dn_struct_rwlock, RW_READER);
358 VERIFY0(dbuf_hold_impl(dn, dnlevel - 1, i,
fcff0f35 359 TRUE, FALSE, FTAG, &db));
b0bc7a84
MG
360 rw_exit(&dn->dn_struct_rwlock);
361
362 free_children(db, blkid, nblks, tx);
363 dbuf_rele(db, FTAG);
34dc7c2f 364 }
34dc7c2f
BB
365 }
366
34dc7c2f 367 if (trunc) {
b0bc7a84
MG
368 ASSERTV(uint64_t off);
369 dn->dn_phys->dn_maxblkid = blkid == 0 ? 0 : blkid - 1;
370
371 ASSERTV(off = (dn->dn_phys->dn_maxblkid + 1) *
1fde1e37 372 (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT));
34dc7c2f
BB
373 ASSERT(off < dn->dn_phys->dn_maxblkid ||
374 dn->dn_phys->dn_maxblkid == 0 ||
b128c09f 375 dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
34dc7c2f
BB
376 }
377}
378
9bd274dd
MA
379typedef struct dnode_sync_free_range_arg {
380 dnode_t *dsfra_dnode;
381 dmu_tx_t *dsfra_tx;
382} dnode_sync_free_range_arg_t;
383
384static void
385dnode_sync_free_range(void *arg, uint64_t blkid, uint64_t nblks)
386{
387 dnode_sync_free_range_arg_t *dsfra = arg;
388 dnode_t *dn = dsfra->dsfra_dnode;
389
390 mutex_exit(&dn->dn_mtx);
391 dnode_sync_free_range_impl(dn, blkid, nblks, dsfra->dsfra_tx);
392 mutex_enter(&dn->dn_mtx);
393}
394
34dc7c2f 395/*
d3cc8b15 396 * Try to kick all the dnode's dbufs out of the cache...
34dc7c2f
BB
397 */
398void
399dnode_evict_dbufs(dnode_t *dn)
400{
0c66c32d
JG
401 dmu_buf_impl_t *db_marker;
402 dmu_buf_impl_t *db, *db_next;
34dc7c2f 403
0c66c32d
JG
404 db_marker = kmem_alloc(sizeof (dmu_buf_impl_t), KM_SLEEP);
405
406 mutex_enter(&dn->dn_dbufs_mtx);
407 for (db = avl_first(&dn->dn_dbufs); db != NULL; db = db_next) {
34dc7c2f 408
572e2857 409#ifdef DEBUG
0c66c32d
JG
410 DB_DNODE_ENTER(db);
411 ASSERT3P(DB_DNODE(db), ==, dn);
412 DB_DNODE_EXIT(db);
572e2857 413#endif /* DEBUG */
34dc7c2f 414
0c66c32d
JG
415 mutex_enter(&db->db_mtx);
416 if (db->db_state != DB_EVICTING &&
417 refcount_is_zero(&db->db_holds)) {
418 db_marker->db_level = db->db_level;
419 db_marker->db_blkid = db->db_blkid;
420 db_marker->db_state = DB_SEARCH;
421 avl_insert_here(&dn->dn_dbufs, db_marker, db,
422 AVL_BEFORE);
423
d3c2ae1c 424 dbuf_destroy(db);
0c66c32d
JG
425
426 db_next = AVL_NEXT(&dn->dn_dbufs, db_marker);
427 avl_remove(&dn->dn_dbufs, db_marker);
428 } else {
bc4501f7 429 db->db_pending_evict = TRUE;
0c66c32d
JG
430 mutex_exit(&db->db_mtx);
431 db_next = AVL_NEXT(&dn->dn_dbufs, db);
34dc7c2f 432 }
0c66c32d
JG
433 }
434 mutex_exit(&dn->dn_dbufs_mtx);
34dc7c2f 435
0c66c32d 436 kmem_free(db_marker, sizeof (dmu_buf_impl_t));
754c6663 437
4c7b7eed
JG
438 dnode_evict_bonus(dn);
439}
440
441void
442dnode_evict_bonus(dnode_t *dn)
443{
34dc7c2f 444 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
bc4501f7
JG
445 if (dn->dn_bonus != NULL) {
446 if (refcount_is_zero(&dn->dn_bonus->db_holds)) {
447 mutex_enter(&dn->dn_bonus->db_mtx);
d3c2ae1c 448 dbuf_destroy(dn->dn_bonus);
bc4501f7
JG
449 dn->dn_bonus = NULL;
450 } else {
451 dn->dn_bonus->db_pending_evict = TRUE;
452 }
34dc7c2f
BB
453 }
454 rw_exit(&dn->dn_struct_rwlock);
455}
456
457static void
458dnode_undirty_dbufs(list_t *list)
459{
460 dbuf_dirty_record_t *dr;
461
c65aa5b2 462 while ((dr = list_head(list))) {
34dc7c2f
BB
463 dmu_buf_impl_t *db = dr->dr_dbuf;
464 uint64_t txg = dr->dr_txg;
465
428870ff
BB
466 if (db->db_level != 0)
467 dnode_undirty_dbufs(&dr->dt.di.dr_children);
468
34dc7c2f
BB
469 mutex_enter(&db->db_mtx);
470 /* XXX - use dbuf_undirty()? */
471 list_remove(list, dr);
472 ASSERT(db->db_last_dirty == dr);
473 db->db_last_dirty = NULL;
474 db->db_dirtycnt -= 1;
475 if (db->db_level == 0) {
428870ff 476 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
34dc7c2f
BB
477 dr->dt.dl.dr_data == db->db_buf);
478 dbuf_unoverride(dr);
58c4aa00
JL
479 } else {
480 mutex_destroy(&dr->dt.di.dr_mtx);
481 list_destroy(&dr->dt.di.dr_children);
34dc7c2f
BB
482 }
483 kmem_free(dr, sizeof (dbuf_dirty_record_t));
428870ff 484 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
34dc7c2f
BB
485 }
486}
487
488static void
489dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
490{
491 int txgoff = tx->tx_txg & TXG_MASK;
492
493 ASSERT(dmu_tx_is_syncing(tx));
494
b128c09f
BB
495 /*
496 * Our contents should have been freed in dnode_sync() by the
497 * free range record inserted by the caller of dnode_free().
498 */
c99c9001 499 ASSERT0(DN_USED_BYTES(dn->dn_phys));
b128c09f
BB
500 ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr));
501
34dc7c2f
BB
502 dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
503 dnode_evict_dbufs(dn);
34dc7c2f
BB
504
505 /*
506 * XXX - It would be nice to assert this, but we may still
507 * have residual holds from async evictions from the arc...
508 *
509 * zfs_obj_to_path() also depends on this being
510 * commented out.
511 *
512 * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1);
513 */
514
515 /* Undirty next bits */
516 dn->dn_next_nlevels[txgoff] = 0;
517 dn->dn_next_indblkshift[txgoff] = 0;
518 dn->dn_next_blksz[txgoff] = 0;
519
34dc7c2f
BB
520 /* ASSERT(blkptrs are zero); */
521 ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
522 ASSERT(dn->dn_type != DMU_OT_NONE);
523
524 ASSERT(dn->dn_free_txg > 0);
525 if (dn->dn_allocated_txg != dn->dn_free_txg)
b0bc7a84 526 dmu_buf_will_dirty(&dn->dn_dbuf->db, tx);
50c957f7 527 bzero(dn->dn_phys, sizeof (dnode_phys_t) * dn->dn_num_slots);
34dc7c2f
BB
528
529 mutex_enter(&dn->dn_mtx);
530 dn->dn_type = DMU_OT_NONE;
531 dn->dn_maxblkid = 0;
532 dn->dn_allocated_txg = 0;
533 dn->dn_free_txg = 0;
428870ff 534 dn->dn_have_spill = B_FALSE;
34dc7c2f
BB
535 mutex_exit(&dn->dn_mtx);
536
537 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
538
539 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
540 /*
541 * Now that we've released our hold, the dnode may
542 * be evicted, so we musn't access it.
543 */
544}
545
546/*
547 * Write out the dnode's dirty buffers.
34dc7c2f
BB
548 */
549void
550dnode_sync(dnode_t *dn, dmu_tx_t *tx)
551{
34dc7c2f
BB
552 dnode_phys_t *dnp = dn->dn_phys;
553 int txgoff = tx->tx_txg & TXG_MASK;
554 list_t *list = &dn->dn_dirty_records[txgoff];
428870ff 555 boolean_t kill_spill = B_FALSE;
b0bc7a84 556 boolean_t freeing_dnode;
1fde1e37 557 ASSERTV(static const dnode_phys_t zerodn = { 0 });
34dc7c2f
BB
558
559 ASSERT(dmu_tx_is_syncing(tx));
560 ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
9babb374 561 ASSERT(dnp->dn_type != DMU_OT_NONE ||
50c957f7 562 bcmp(dnp, &zerodn, DNODE_MIN_SIZE) == 0);
34dc7c2f
BB
563 DNODE_VERIFY(dn);
564
565 ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
566
9babb374
BB
567 if (dmu_objset_userused_enabled(dn->dn_objset) &&
568 !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
428870ff
BB
569 mutex_enter(&dn->dn_mtx);
570 dn->dn_oldused = DN_USED_BYTES(dn->dn_phys);
571 dn->dn_oldflags = dn->dn_phys->dn_flags;
9babb374 572 dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED;
1de321e6
JX
573 if (dmu_objset_userobjused_enabled(dn->dn_objset))
574 dn->dn_phys->dn_flags |=
575 DNODE_FLAG_USEROBJUSED_ACCOUNTED;
428870ff
BB
576 mutex_exit(&dn->dn_mtx);
577 dmu_objset_userquota_get_ids(dn, B_FALSE, tx);
9babb374
BB
578 } else {
579 /* Once we account for it, we should always account for it. */
580 ASSERT(!(dn->dn_phys->dn_flags &
581 DNODE_FLAG_USERUSED_ACCOUNTED));
1de321e6
JX
582 ASSERT(!(dn->dn_phys->dn_flags &
583 DNODE_FLAG_USEROBJUSED_ACCOUNTED));
9babb374
BB
584 }
585
34dc7c2f
BB
586 mutex_enter(&dn->dn_mtx);
587 if (dn->dn_allocated_txg == tx->tx_txg) {
588 /* The dnode is newly allocated or reallocated */
589 if (dnp->dn_type == DMU_OT_NONE) {
590 /* this is a first alloc, not a realloc */
34dc7c2f 591 dnp->dn_nlevels = 1;
d164b209 592 dnp->dn_nblkptr = dn->dn_nblkptr;
34dc7c2f
BB
593 }
594
34dc7c2f
BB
595 dnp->dn_type = dn->dn_type;
596 dnp->dn_bonustype = dn->dn_bonustype;
597 dnp->dn_bonuslen = dn->dn_bonuslen;
34dc7c2f 598 }
50c957f7
NB
599
600 dnp->dn_extra_slots = dn->dn_num_slots - 1;
601
34dc7c2f
BB
602 ASSERT(dnp->dn_nlevels > 1 ||
603 BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
9b67f605 604 BP_IS_EMBEDDED(&dnp->dn_blkptr[0]) ||
34dc7c2f
BB
605 BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
606 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
9b67f605
MA
607 ASSERT(dnp->dn_nlevels < 2 ||
608 BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
609 BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 1 << dnp->dn_indblkshift);
34dc7c2f 610
fa86b5db
MA
611 if (dn->dn_next_type[txgoff] != 0) {
612 dnp->dn_type = dn->dn_type;
613 dn->dn_next_type[txgoff] = 0;
614 }
615
616 if (dn->dn_next_blksz[txgoff] != 0) {
34dc7c2f
BB
617 ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
618 SPA_MINBLOCKSIZE) == 0);
619 ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
b128c09f 620 dn->dn_maxblkid == 0 || list_head(list) != NULL ||
34dc7c2f 621 dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
9bd274dd
MA
622 dnp->dn_datablkszsec ||
623 range_tree_space(dn->dn_free_ranges[txgoff]) != 0);
34dc7c2f
BB
624 dnp->dn_datablkszsec =
625 dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
626 dn->dn_next_blksz[txgoff] = 0;
627 }
628
fa86b5db 629 if (dn->dn_next_bonuslen[txgoff] != 0) {
34dc7c2f
BB
630 if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
631 dnp->dn_bonuslen = 0;
632 else
633 dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
50c957f7
NB
634 ASSERT(dnp->dn_bonuslen <=
635 DN_SLOTS_TO_BONUSLEN(dnp->dn_extra_slots + 1));
34dc7c2f
BB
636 dn->dn_next_bonuslen[txgoff] = 0;
637 }
638
fa86b5db 639 if (dn->dn_next_bonustype[txgoff] != 0) {
9ae529ec 640 ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff]));
428870ff
BB
641 dnp->dn_bonustype = dn->dn_next_bonustype[txgoff];
642 dn->dn_next_bonustype[txgoff] = 0;
643 }
644
b0bc7a84
MG
645 freeing_dnode = dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg;
646
428870ff 647 /*
08dc1b2d
MA
648 * Remove the spill block if we have been explicitly asked to
649 * remove it, or if the object is being removed.
428870ff 650 */
08dc1b2d
MA
651 if (dn->dn_rm_spillblk[txgoff] || freeing_dnode) {
652 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
428870ff
BB
653 kill_spill = B_TRUE;
654 dn->dn_rm_spillblk[txgoff] = 0;
655 }
656
fa86b5db 657 if (dn->dn_next_indblkshift[txgoff] != 0) {
34dc7c2f
BB
658 ASSERT(dnp->dn_nlevels == 1);
659 dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
660 dn->dn_next_indblkshift[txgoff] = 0;
661 }
662
663 /*
664 * Just take the live (open-context) values for checksum and compress.
665 * Strictly speaking it's a future leak, but nothing bad happens if we
666 * start using the new checksum or compress algorithm a little early.
667 */
668 dnp->dn_checksum = dn->dn_checksum;
669 dnp->dn_compress = dn->dn_compress;
670
671 mutex_exit(&dn->dn_mtx);
672
428870ff 673 if (kill_spill) {
50c957f7 674 free_blocks(dn, DN_SPILL_BLKPTR(dn->dn_phys), 1, tx);
428870ff
BB
675 mutex_enter(&dn->dn_mtx);
676 dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR;
677 mutex_exit(&dn->dn_mtx);
678 }
679
34dc7c2f 680 /* process all the "freed" ranges in the file */
9bd274dd
MA
681 if (dn->dn_free_ranges[txgoff] != NULL) {
682 dnode_sync_free_range_arg_t dsfra;
683 dsfra.dsfra_dnode = dn;
684 dsfra.dsfra_tx = tx;
b128c09f 685 mutex_enter(&dn->dn_mtx);
9bd274dd
MA
686 range_tree_vacate(dn->dn_free_ranges[txgoff],
687 dnode_sync_free_range, &dsfra);
688 range_tree_destroy(dn->dn_free_ranges[txgoff]);
689 dn->dn_free_ranges[txgoff] = NULL;
b128c09f 690 mutex_exit(&dn->dn_mtx);
34dc7c2f 691 }
34dc7c2f 692
b0bc7a84 693 if (freeing_dnode) {
68cbd56e 694 dn->dn_objset->os_freed_dnodes++;
34dc7c2f
BB
695 dnode_sync_free(dn, tx);
696 return;
697 }
698
50c957f7
NB
699 if (dn->dn_num_slots > DNODE_MIN_SLOTS) {
700 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
701 mutex_enter(&ds->ds_lock);
702 ds->ds_feature_activation_needed[SPA_FEATURE_LARGE_DNODE] =
703 B_TRUE;
704 mutex_exit(&ds->ds_lock);
705 }
706
4f68d787
GW
707 if (dn->dn_next_nlevels[txgoff]) {
708 dnode_increase_indirection(dn, tx);
709 dn->dn_next_nlevels[txgoff] = 0;
710 }
711
d164b209
BB
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 */
37f000c5
NB
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 }
d164b209
BB
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
4bda3bd0 737 dbuf_sync_list(list, dn->dn_phys->dn_nlevels - 1, tx);
34dc7c2f 738
9babb374 739 if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
34dc7c2f
BB
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}