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