1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23 /*
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2012, 2020 by Delphix. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright 2020 Oxide Computer Company
28 */
29
30 #include <sys/zfs_context.h>
31 #include <sys/dbuf.h>
32 #include <sys/dnode.h>
33 #include <sys/dmu.h>
34 #include <sys/dmu_tx.h>
35 #include <sys/dmu_objset.h>
36 #include <sys/dmu_recv.h>
37 #include <sys/dsl_dataset.h>
38 #include <sys/spa.h>
39 #include <sys/range_tree.h>
40 #include <sys/zfeature.h>
41
42 static void
dnode_increase_indirection(dnode_t * dn,dmu_tx_t * tx)43 dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx)
44 {
45 dmu_buf_impl_t *db;
46 int txgoff = tx->tx_txg & TXG_MASK;
47 int nblkptr = dn->dn_phys->dn_nblkptr;
48 int old_toplvl = dn->dn_phys->dn_nlevels - 1;
49 int new_level = dn->dn_next_nlevels[txgoff];
50 int i;
51
52 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
53
54 /* this dnode can't be paged out because it's dirty */
55 ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
56 ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0);
57
58 db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG);
59 ASSERT(db != NULL);
60
61 dn->dn_phys->dn_nlevels = new_level;
62 dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset,
63 (u_longlong_t)dn->dn_object, dn->dn_phys->dn_nlevels);
64
65 /*
66 * Lock ordering requires that we hold the children's db_mutexes (by
67 * calling dbuf_find()) before holding the parent's db_rwlock. The lock
68 * order is imposed by dbuf_read's steps of "grab the lock to protect
69 * db_parent, get db_parent, hold db_parent's db_rwlock".
70 */
71 dmu_buf_impl_t *children[DN_MAX_NBLKPTR];
72 ASSERT3U(nblkptr, <=, DN_MAX_NBLKPTR);
73 for (i = 0; i < nblkptr; i++) {
74 children[i] = dbuf_find(dn->dn_objset, dn->dn_object,
75 old_toplvl, i, NULL);
76 }
77
78 /* transfer dnode's block pointers to new indirect block */
79 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT);
80 if (dn->dn_dbuf != NULL)
81 rw_enter(&dn->dn_dbuf->db_rwlock, RW_WRITER);
82 rw_enter(&db->db_rwlock, RW_WRITER);
83 ASSERT(db->db.db_data);
84 ASSERT(arc_released(db->db_buf));
85 ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size);
86 memcpy(db->db.db_data, dn->dn_phys->dn_blkptr,
87 sizeof (blkptr_t) * nblkptr);
88 arc_buf_freeze(db->db_buf);
89
90 /* set dbuf's parent pointers to new indirect buf */
91 for (i = 0; i < nblkptr; i++) {
92 dmu_buf_impl_t *child = children[i];
93
94 if (child == NULL)
95 continue;
96 #ifdef ZFS_DEBUG
97 DB_DNODE_ENTER(child);
98 ASSERT3P(DB_DNODE(child), ==, dn);
99 DB_DNODE_EXIT(child);
100 #endif /* DEBUG */
101 if (child->db_parent && child->db_parent != dn->dn_dbuf) {
102 ASSERT(child->db_parent->db_level == db->db_level);
103 ASSERT(child->db_blkptr !=
104 &dn->dn_phys->dn_blkptr[child->db_blkid]);
105 mutex_exit(&child->db_mtx);
106 continue;
107 }
108 ASSERT(child->db_parent == NULL ||
109 child->db_parent == dn->dn_dbuf);
110
111 child->db_parent = db;
112 dbuf_add_ref(db, child);
113 if (db->db.db_data)
114 child->db_blkptr = (blkptr_t *)db->db.db_data + i;
115 else
116 child->db_blkptr = NULL;
117 dprintf_dbuf_bp(child, child->db_blkptr,
118 "changed db_blkptr to new indirect %s", "");
119
120 mutex_exit(&child->db_mtx);
121 }
122
123 memset(dn->dn_phys->dn_blkptr, 0, sizeof (blkptr_t) * nblkptr);
124
125 rw_exit(&db->db_rwlock);
126 if (dn->dn_dbuf != NULL)
127 rw_exit(&dn->dn_dbuf->db_rwlock);
128
129 dbuf_rele(db, FTAG);
130
131 rw_exit(&dn->dn_struct_rwlock);
132 }
133
134 static void
free_blocks(dnode_t * dn,blkptr_t * bp,int num,dmu_tx_t * tx)135 free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx)
136 {
137 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
138 uint64_t bytesfreed = 0;
139
140 dprintf("ds=%p obj=%llx num=%d\n", ds, (u_longlong_t)dn->dn_object,
141 num);
142
143 for (int i = 0; i < num; i++, bp++) {
144 if (BP_IS_HOLE(bp))
145 continue;
146
147 bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE);
148 ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys));
149
150 /*
151 * Save some useful information on the holes being
152 * punched, including logical size, type, and indirection
153 * level. Retaining birth time enables detection of when
154 * holes are punched for reducing the number of free
155 * records transmitted during a zfs send.
156 */
157
158 uint64_t lsize = BP_GET_LSIZE(bp);
159 dmu_object_type_t type = BP_GET_TYPE(bp);
160 uint64_t lvl = BP_GET_LEVEL(bp);
161
162 memset(bp, 0, sizeof (blkptr_t));
163
164 if (spa_feature_is_active(dn->dn_objset->os_spa,
165 SPA_FEATURE_HOLE_BIRTH)) {
166 BP_SET_LSIZE(bp, lsize);
167 BP_SET_TYPE(bp, type);
168 BP_SET_LEVEL(bp, lvl);
169 BP_SET_BIRTH(bp, dmu_tx_get_txg(tx), 0);
170 }
171 }
172 dnode_diduse_space(dn, -bytesfreed);
173 }
174
175 #ifdef ZFS_DEBUG
176 static void
free_verify(dmu_buf_impl_t * db,uint64_t start,uint64_t end,dmu_tx_t * tx)177 free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx)
178 {
179 uint64_t off, num, i, j;
180 unsigned int epbs;
181 int err;
182 uint64_t txg = tx->tx_txg;
183 dnode_t *dn;
184
185 DB_DNODE_ENTER(db);
186 dn = DB_DNODE(db);
187 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
188 off = start - (db->db_blkid << epbs);
189 num = end - start + 1;
190
191 ASSERT3U(dn->dn_phys->dn_indblkshift, >=, SPA_BLKPTRSHIFT);
192 ASSERT3U(end + 1, >=, start);
193 ASSERT3U(start, >=, (db->db_blkid << epbs));
194 ASSERT3U(db->db_level, >, 0);
195 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
196 ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT);
197 ASSERT(db->db_blkptr != NULL);
198
199 for (i = off; i < off+num; i++) {
200 uint64_t *buf;
201 dmu_buf_impl_t *child;
202 dbuf_dirty_record_t *dr;
203
204 ASSERT(db->db_level == 1);
205
206 rw_enter(&dn->dn_struct_rwlock, RW_READER);
207 err = dbuf_hold_impl(dn, db->db_level - 1,
208 (db->db_blkid << epbs) + i, TRUE, FALSE, FTAG, &child);
209 rw_exit(&dn->dn_struct_rwlock);
210 if (err == ENOENT)
211 continue;
212 ASSERT(err == 0);
213 ASSERT(child->db_level == 0);
214 dr = dbuf_find_dirty_eq(child, txg);
215
216 /* data_old better be zeroed */
217 if (dr) {
218 buf = dr->dt.dl.dr_data->b_data;
219 for (j = 0; j < child->db.db_size >> 3; j++) {
220 if (buf[j] != 0) {
221 panic("freed data not zero: "
222 "child=%p i=%llu off=%llu "
223 "num=%llu\n",
224 (void *)child, (u_longlong_t)i,
225 (u_longlong_t)off,
226 (u_longlong_t)num);
227 }
228 }
229 }
230
231 /*
232 * db_data better be zeroed unless it's dirty in a
233 * future txg.
234 */
235 mutex_enter(&child->db_mtx);
236 buf = child->db.db_data;
237 if (buf != NULL && child->db_state != DB_FILL &&
238 list_is_empty(&child->db_dirty_records)) {
239 for (j = 0; j < child->db.db_size >> 3; j++) {
240 if (buf[j] != 0) {
241 panic("freed data not zero: "
242 "child=%p i=%llu off=%llu "
243 "num=%llu\n",
244 (void *)child, (u_longlong_t)i,
245 (u_longlong_t)off,
246 (u_longlong_t)num);
247 }
248 }
249 }
250 mutex_exit(&child->db_mtx);
251
252 dbuf_rele(child, FTAG);
253 }
254 DB_DNODE_EXIT(db);
255 }
256 #endif
257
258 /*
259 * We don't usually free the indirect blocks here. If in one txg we have a
260 * free_range and a write to the same indirect block, it's important that we
261 * preserve the hole's birth times. Therefore, we don't free any any indirect
262 * blocks in free_children(). If an indirect block happens to turn into all
263 * holes, it will be freed by dbuf_write_children_ready, which happens at a
264 * point in the syncing process where we know for certain the contents of the
265 * indirect block.
266 *
267 * However, if we're freeing a dnode, its space accounting must go to zero
268 * before we actually try to free the dnode, or we will trip an assertion. In
269 * addition, we know the case described above cannot occur, because the dnode is
270 * being freed. Therefore, we free the indirect blocks immediately in that
271 * case.
272 */
273 static void
free_children(dmu_buf_impl_t * db,uint64_t blkid,uint64_t nblks,boolean_t free_indirects,dmu_tx_t * tx)274 free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks,
275 boolean_t free_indirects, dmu_tx_t *tx)
276 {
277 dnode_t *dn;
278 blkptr_t *bp;
279 dmu_buf_impl_t *subdb;
280 uint64_t start, end, dbstart, dbend;
281 unsigned int epbs, shift, i;
282
283 /*
284 * There is a small possibility that this block will not be cached:
285 * 1 - if level > 1 and there are no children with level <= 1
286 * 2 - if this block was evicted since we read it from
287 * dmu_tx_hold_free().
288 */
289 if (db->db_state != DB_CACHED)
290 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
291
292 /*
293 * If we modify this indirect block, and we are not freeing the
294 * dnode (!free_indirects), then this indirect block needs to get
295 * written to disk by dbuf_write(). If it is dirty, we know it will
296 * be written (otherwise, we would have incorrect on-disk state
297 * because the space would be freed but still referenced by the BP
298 * in this indirect block). Therefore we VERIFY that it is
299 * dirty.
300 *
301 * Our VERIFY covers some cases that do not actually have to be
302 * dirty, but the open-context code happens to dirty. E.g. if the
303 * blocks we are freeing are all holes, because in that case, we
304 * are only freeing part of this indirect block, so it is an
305 * ancestor of the first or last block to be freed. The first and
306 * last L1 indirect blocks are always dirtied by dnode_free_range().
307 */
308 db_lock_type_t dblt = dmu_buf_lock_parent(db, RW_READER, FTAG);
309 VERIFY(BP_GET_FILL(db->db_blkptr) == 0 || db->db_dirtycnt > 0);
310 dmu_buf_unlock_parent(db, dblt, FTAG);
311
312 dbuf_release_bp(db);
313 bp = db->db.db_data;
314
315 DB_DNODE_ENTER(db);
316 dn = DB_DNODE(db);
317 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
318 ASSERT3U(epbs, <, 31);
319 shift = (db->db_level - 1) * epbs;
320 dbstart = db->db_blkid << epbs;
321 start = blkid >> shift;
322 if (dbstart < start) {
323 bp += start - dbstart;
324 } else {
325 start = dbstart;
326 }
327 dbend = ((db->db_blkid + 1) << epbs) - 1;
328 end = (blkid + nblks - 1) >> shift;
329 if (dbend <= end)
330 end = dbend;
331
332 ASSERT3U(start, <=, end);
333
334 if (db->db_level == 1) {
335 FREE_VERIFY(db, start, end, tx);
336 rw_enter(&db->db_rwlock, RW_WRITER);
337 free_blocks(dn, bp, end - start + 1, tx);
338 rw_exit(&db->db_rwlock);
339 } else {
340 for (uint64_t id = start; id <= end; id++, bp++) {
341 if (BP_IS_HOLE(bp))
342 continue;
343 rw_enter(&dn->dn_struct_rwlock, RW_READER);
344 VERIFY0(dbuf_hold_impl(dn, db->db_level - 1,
345 id, TRUE, FALSE, FTAG, &subdb));
346 rw_exit(&dn->dn_struct_rwlock);
347 ASSERT3P(bp, ==, subdb->db_blkptr);
348
349 free_children(subdb, blkid, nblks, free_indirects, tx);
350 dbuf_rele(subdb, FTAG);
351 }
352 }
353
354 if (free_indirects) {
355 rw_enter(&db->db_rwlock, RW_WRITER);
356 for (i = 0, bp = db->db.db_data; i < 1 << epbs; i++, bp++)
357 ASSERT(BP_IS_HOLE(bp));
358 memset(db->db.db_data, 0, db->db.db_size);
359 free_blocks(dn, db->db_blkptr, 1, tx);
360 rw_exit(&db->db_rwlock);
361 }
362
363 DB_DNODE_EXIT(db);
364 arc_buf_freeze(db->db_buf);
365 }
366
367 /*
368 * Traverse the indicated range of the provided file
369 * and "free" all the blocks contained there.
370 */
371 static void
dnode_sync_free_range_impl(dnode_t * dn,uint64_t blkid,uint64_t nblks,boolean_t free_indirects,dmu_tx_t * tx)372 dnode_sync_free_range_impl(dnode_t *dn, uint64_t blkid, uint64_t nblks,
373 boolean_t free_indirects, dmu_tx_t *tx)
374 {
375 blkptr_t *bp = dn->dn_phys->dn_blkptr;
376 int dnlevel = dn->dn_phys->dn_nlevels;
377 boolean_t trunc = B_FALSE;
378
379 if (blkid > dn->dn_phys->dn_maxblkid)
380 return;
381
382 ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX);
383 if (blkid + nblks > dn->dn_phys->dn_maxblkid) {
384 nblks = dn->dn_phys->dn_maxblkid - blkid + 1;
385 trunc = B_TRUE;
386 }
387
388 /* There are no indirect blocks in the object */
389 if (dnlevel == 1) {
390 if (blkid >= dn->dn_phys->dn_nblkptr) {
391 /* this range was never made persistent */
392 return;
393 }
394 ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr);
395 free_blocks(dn, bp + blkid, nblks, tx);
396 } else {
397 int shift = (dnlevel - 1) *
398 (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT);
399 int start = blkid >> shift;
400 int end = (blkid + nblks - 1) >> shift;
401 dmu_buf_impl_t *db;
402
403 ASSERT(start < dn->dn_phys->dn_nblkptr);
404 bp += start;
405 for (int i = start; i <= end; i++, bp++) {
406 if (BP_IS_HOLE(bp))
407 continue;
408 rw_enter(&dn->dn_struct_rwlock, RW_READER);
409 VERIFY0(dbuf_hold_impl(dn, dnlevel - 1, i,
410 TRUE, FALSE, FTAG, &db));
411 rw_exit(&dn->dn_struct_rwlock);
412 free_children(db, blkid, nblks, free_indirects, tx);
413 dbuf_rele(db, FTAG);
414 }
415 }
416
417 /*
418 * Do not truncate the maxblkid if we are performing a raw
419 * receive. The raw receive sets the maxblkid manually and
420 * must not be overridden. Usually, the last DRR_FREE record
421 * will be at the maxblkid, because the source system sets
422 * the maxblkid when truncating. However, if the last block
423 * was freed by overwriting with zeros and being compressed
424 * away to a hole, the source system will generate a DRR_FREE
425 * record while leaving the maxblkid after the end of that
426 * record. In this case we need to leave the maxblkid as
427 * indicated in the DRR_OBJECT record, so that it matches the
428 * source system, ensuring that the cryptographic hashes will
429 * match.
430 */
431 if (trunc && !dn->dn_objset->os_raw_receive) {
432 uint64_t off __maybe_unused;
433 dn->dn_phys->dn_maxblkid = blkid == 0 ? 0 : blkid - 1;
434
435 off = (dn->dn_phys->dn_maxblkid + 1) *
436 (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
437 ASSERT(off < dn->dn_phys->dn_maxblkid ||
438 dn->dn_phys->dn_maxblkid == 0 ||
439 dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
440 }
441 }
442
443 typedef struct dnode_sync_free_range_arg {
444 dnode_t *dsfra_dnode;
445 dmu_tx_t *dsfra_tx;
446 boolean_t dsfra_free_indirects;
447 } dnode_sync_free_range_arg_t;
448
449 static void
dnode_sync_free_range(void * arg,uint64_t blkid,uint64_t nblks)450 dnode_sync_free_range(void *arg, uint64_t blkid, uint64_t nblks)
451 {
452 dnode_sync_free_range_arg_t *dsfra = arg;
453 dnode_t *dn = dsfra->dsfra_dnode;
454
455 mutex_exit(&dn->dn_mtx);
456 dnode_sync_free_range_impl(dn, blkid, nblks,
457 dsfra->dsfra_free_indirects, dsfra->dsfra_tx);
458 mutex_enter(&dn->dn_mtx);
459 }
460
461 /*
462 * Try to kick all the dnode's dbufs out of the cache...
463 */
464 void
dnode_evict_dbufs(dnode_t * dn)465 dnode_evict_dbufs(dnode_t *dn)
466 {
467 dmu_buf_impl_t *db_marker;
468 dmu_buf_impl_t *db, *db_next;
469
470 db_marker = kmem_alloc(sizeof (dmu_buf_impl_t), KM_SLEEP);
471
472 mutex_enter(&dn->dn_dbufs_mtx);
473 for (db = avl_first(&dn->dn_dbufs); db != NULL; db = db_next) {
474
475 #ifdef ZFS_DEBUG
476 DB_DNODE_ENTER(db);
477 ASSERT3P(DB_DNODE(db), ==, dn);
478 DB_DNODE_EXIT(db);
479 #endif /* DEBUG */
480
481 mutex_enter(&db->db_mtx);
482 if (db->db_state != DB_EVICTING &&
483 zfs_refcount_is_zero(&db->db_holds)) {
484 db_marker->db_level = db->db_level;
485 db_marker->db_blkid = db->db_blkid;
486 /*
487 * Insert a MARKER node with the same level and blkid.
488 * And to resolve any ties in dbuf_compare() use the
489 * pointer of the dbuf that we are evicting. Pass the
490 * address in db_parent.
491 */
492 db_marker->db_state = DB_MARKER;
493 db_marker->db_parent = (void *)((uintptr_t)db - 1);
494 avl_insert_here(&dn->dn_dbufs, db_marker, db,
495 AVL_BEFORE);
496
497 /*
498 * We need to use the "marker" dbuf rather than
499 * simply getting the next dbuf, because
500 * dbuf_destroy() may actually remove multiple dbufs.
501 * It can call itself recursively on the parent dbuf,
502 * which may also be removed from dn_dbufs. The code
503 * flow would look like:
504 *
505 * dbuf_destroy():
506 * dnode_rele_and_unlock(parent_dbuf, evicting=TRUE):
507 * if (!cacheable || pending_evict)
508 * dbuf_destroy()
509 */
510 dbuf_destroy(db);
511
512 db_next = AVL_NEXT(&dn->dn_dbufs, db_marker);
513 avl_remove(&dn->dn_dbufs, db_marker);
514 } else {
515 db->db_pending_evict = TRUE;
516 mutex_exit(&db->db_mtx);
517 db_next = AVL_NEXT(&dn->dn_dbufs, db);
518 }
519 }
520 mutex_exit(&dn->dn_dbufs_mtx);
521
522 kmem_free(db_marker, sizeof (dmu_buf_impl_t));
523
524 dnode_evict_bonus(dn);
525 }
526
527 void
dnode_evict_bonus(dnode_t * dn)528 dnode_evict_bonus(dnode_t *dn)
529 {
530 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
531 if (dn->dn_bonus != NULL) {
532 if (zfs_refcount_is_zero(&dn->dn_bonus->db_holds)) {
533 mutex_enter(&dn->dn_bonus->db_mtx);
534 dbuf_destroy(dn->dn_bonus);
535 dn->dn_bonus = NULL;
536 } else {
537 dn->dn_bonus->db_pending_evict = TRUE;
538 }
539 }
540 rw_exit(&dn->dn_struct_rwlock);
541 }
542
543 static void
dnode_undirty_dbufs(list_t * list)544 dnode_undirty_dbufs(list_t *list)
545 {
546 dbuf_dirty_record_t *dr;
547
548 while ((dr = list_head(list))) {
549 dmu_buf_impl_t *db = dr->dr_dbuf;
550 uint64_t txg = dr->dr_txg;
551
552 if (db->db_level != 0)
553 dnode_undirty_dbufs(&dr->dt.di.dr_children);
554
555 mutex_enter(&db->db_mtx);
556 /* XXX - use dbuf_undirty()? */
557 list_remove(list, dr);
558 ASSERT(list_head(&db->db_dirty_records) == dr);
559 list_remove_head(&db->db_dirty_records);
560 ASSERT(list_is_empty(&db->db_dirty_records));
561 db->db_dirtycnt -= 1;
562 if (db->db_level == 0) {
563 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
564 dr->dt.dl.dr_data == db->db_buf);
565 dbuf_unoverride(dr);
566 } else {
567 mutex_destroy(&dr->dt.di.dr_mtx);
568 list_destroy(&dr->dt.di.dr_children);
569 }
570 kmem_cache_free(dbuf_dirty_kmem_cache, dr);
571 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg, B_FALSE);
572 }
573 }
574
575 static void
dnode_sync_free(dnode_t * dn,dmu_tx_t * tx)576 dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
577 {
578 int txgoff = tx->tx_txg & TXG_MASK;
579
580 ASSERT(dmu_tx_is_syncing(tx));
581
582 /*
583 * Our contents should have been freed in dnode_sync() by the
584 * free range record inserted by the caller of dnode_free().
585 */
586 ASSERT0(DN_USED_BYTES(dn->dn_phys));
587 ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr));
588
589 dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
590 dnode_evict_dbufs(dn);
591
592 /*
593 * XXX - It would be nice to assert this, but we may still
594 * have residual holds from async evictions from the arc...
595 *
596 * zfs_obj_to_path() also depends on this being
597 * commented out.
598 *
599 * ASSERT3U(zfs_refcount_count(&dn->dn_holds), ==, 1);
600 */
601
602 /* Undirty next bits */
603 dn->dn_next_nlevels[txgoff] = 0;
604 dn->dn_next_indblkshift[txgoff] = 0;
605 dn->dn_next_blksz[txgoff] = 0;
606 dn->dn_next_maxblkid[txgoff] = 0;
607
608 /* ASSERT(blkptrs are zero); */
609 ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
610 ASSERT(dn->dn_type != DMU_OT_NONE);
611
612 ASSERT(dn->dn_free_txg > 0);
613 if (dn->dn_allocated_txg != dn->dn_free_txg)
614 dmu_buf_will_dirty(&dn->dn_dbuf->db, tx);
615 memset(dn->dn_phys, 0, sizeof (dnode_phys_t) * dn->dn_num_slots);
616 dnode_free_interior_slots(dn);
617
618 mutex_enter(&dn->dn_mtx);
619 dn->dn_type = DMU_OT_NONE;
620 dn->dn_maxblkid = 0;
621 dn->dn_allocated_txg = 0;
622 dn->dn_free_txg = 0;
623 dn->dn_have_spill = B_FALSE;
624 dn->dn_num_slots = 1;
625 mutex_exit(&dn->dn_mtx);
626
627 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
628
629 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
630 /*
631 * Now that we've released our hold, the dnode may
632 * be evicted, so we mustn't access it.
633 */
634 }
635
636 /*
637 * Write out the dnode's dirty buffers.
638 * Does not wait for zio completions.
639 */
640 void
dnode_sync(dnode_t * dn,dmu_tx_t * tx)641 dnode_sync(dnode_t *dn, dmu_tx_t *tx)
642 {
643 objset_t *os = dn->dn_objset;
644 dnode_phys_t *dnp = dn->dn_phys;
645 int txgoff = tx->tx_txg & TXG_MASK;
646 list_t *list = &dn->dn_dirty_records[txgoff];
647 static const dnode_phys_t zerodn __maybe_unused = { 0 };
648 boolean_t kill_spill = B_FALSE;
649
650 ASSERT(dmu_tx_is_syncing(tx));
651 ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
652 ASSERT(dnp->dn_type != DMU_OT_NONE ||
653 memcmp(dnp, &zerodn, DNODE_MIN_SIZE) == 0);
654 DNODE_VERIFY(dn);
655
656 ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
657
658 /*
659 * Do user accounting if it is enabled and this is not
660 * an encrypted receive.
661 */
662 if (dmu_objset_userused_enabled(os) &&
663 !DMU_OBJECT_IS_SPECIAL(dn->dn_object) &&
664 (!os->os_encrypted || !dmu_objset_is_receiving(os))) {
665 mutex_enter(&dn->dn_mtx);
666 dn->dn_oldused = DN_USED_BYTES(dn->dn_phys);
667 dn->dn_oldflags = dn->dn_phys->dn_flags;
668 dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED;
669 if (dmu_objset_userobjused_enabled(dn->dn_objset))
670 dn->dn_phys->dn_flags |=
671 DNODE_FLAG_USEROBJUSED_ACCOUNTED;
672 mutex_exit(&dn->dn_mtx);
673 dmu_objset_userquota_get_ids(dn, B_FALSE, tx);
674 } else if (!(os->os_encrypted && dmu_objset_is_receiving(os))) {
675 /*
676 * Once we account for it, we should always account for it,
677 * except for the case of a raw receive. We will not be able
678 * to account for it until the receiving dataset has been
679 * mounted.
680 */
681 ASSERT(!(dn->dn_phys->dn_flags &
682 DNODE_FLAG_USERUSED_ACCOUNTED));
683 ASSERT(!(dn->dn_phys->dn_flags &
684 DNODE_FLAG_USEROBJUSED_ACCOUNTED));
685 }
686
687 mutex_enter(&dn->dn_mtx);
688 if (dn->dn_allocated_txg == tx->tx_txg) {
689 /* The dnode is newly allocated or reallocated */
690 if (dnp->dn_type == DMU_OT_NONE) {
691 /* this is a first alloc, not a realloc */
692 dnp->dn_nlevels = 1;
693 dnp->dn_nblkptr = dn->dn_nblkptr;
694 }
695
696 dnp->dn_type = dn->dn_type;
697 dnp->dn_bonustype = dn->dn_bonustype;
698 dnp->dn_bonuslen = dn->dn_bonuslen;
699 }
700
701 dnp->dn_extra_slots = dn->dn_num_slots - 1;
702
703 ASSERT(dnp->dn_nlevels > 1 ||
704 BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
705 BP_IS_EMBEDDED(&dnp->dn_blkptr[0]) ||
706 BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
707 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
708 ASSERT(dnp->dn_nlevels < 2 ||
709 BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
710 BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 1 << dnp->dn_indblkshift);
711
712 if (dn->dn_next_type[txgoff] != 0) {
713 dnp->dn_type = dn->dn_type;
714 dn->dn_next_type[txgoff] = 0;
715 }
716
717 if (dn->dn_next_blksz[txgoff] != 0) {
718 ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
719 SPA_MINBLOCKSIZE) == 0);
720 ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
721 dn->dn_maxblkid == 0 || list_head(list) != NULL ||
722 dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
723 dnp->dn_datablkszsec ||
724 !zfs_range_tree_is_empty(dn->dn_free_ranges[txgoff]));
725 dnp->dn_datablkszsec =
726 dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
727 dn->dn_next_blksz[txgoff] = 0;
728 }
729
730 if (dn->dn_next_bonuslen[txgoff] != 0) {
731 if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
732 dnp->dn_bonuslen = 0;
733 else
734 dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
735 ASSERT(dnp->dn_bonuslen <=
736 DN_SLOTS_TO_BONUSLEN(dnp->dn_extra_slots + 1));
737 dn->dn_next_bonuslen[txgoff] = 0;
738 }
739
740 if (dn->dn_next_bonustype[txgoff] != 0) {
741 ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff]));
742 dnp->dn_bonustype = dn->dn_next_bonustype[txgoff];
743 dn->dn_next_bonustype[txgoff] = 0;
744 }
745
746 boolean_t freeing_dnode = dn->dn_free_txg > 0 &&
747 dn->dn_free_txg <= tx->tx_txg;
748
749 /*
750 * Remove the spill block if we have been explicitly asked to
751 * remove it, or if the object is being removed.
752 */
753 if (dn->dn_rm_spillblk[txgoff] || freeing_dnode) {
754 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
755 kill_spill = B_TRUE;
756 dn->dn_rm_spillblk[txgoff] = 0;
757 }
758
759 if (dn->dn_next_indblkshift[txgoff] != 0) {
760 ASSERT(dnp->dn_nlevels == 1);
761 dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
762 dn->dn_next_indblkshift[txgoff] = 0;
763 }
764
765 /*
766 * Just take the live (open-context) values for checksum and compress.
767 * Strictly speaking it's a future leak, but nothing bad happens if we
768 * start using the new checksum or compress algorithm a little early.
769 */
770 dnp->dn_checksum = dn->dn_checksum;
771 dnp->dn_compress = dn->dn_compress;
772
773 mutex_exit(&dn->dn_mtx);
774
775 if (kill_spill) {
776 free_blocks(dn, DN_SPILL_BLKPTR(dn->dn_phys), 1, tx);
777 mutex_enter(&dn->dn_mtx);
778 dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR;
779 mutex_exit(&dn->dn_mtx);
780 }
781
782 /* process all the "freed" ranges in the file */
783 if (dn->dn_free_ranges[txgoff] != NULL) {
784 dnode_sync_free_range_arg_t dsfra;
785 dsfra.dsfra_dnode = dn;
786 dsfra.dsfra_tx = tx;
787 dsfra.dsfra_free_indirects = freeing_dnode;
788 mutex_enter(&dn->dn_mtx);
789 if (freeing_dnode) {
790 ASSERT(zfs_range_tree_contains(
791 dn->dn_free_ranges[txgoff], 0,
792 dn->dn_maxblkid + 1));
793 }
794 /*
795 * Because dnode_sync_free_range() must drop dn_mtx during its
796 * processing, using it as a callback to zfs_range_tree_vacate()
797 * is not safe. No other operations (besides destroy) are
798 * allowed once zfs_range_tree_vacate() has begun, and dropping
799 * dn_mtx would leave a window open for another thread to
800 * observe that invalid (and unsafe) state.
801 */
802 zfs_range_tree_walk(dn->dn_free_ranges[txgoff],
803 dnode_sync_free_range, &dsfra);
804 zfs_range_tree_vacate(dn->dn_free_ranges[txgoff], NULL, NULL);
805 zfs_range_tree_destroy(dn->dn_free_ranges[txgoff]);
806 dn->dn_free_ranges[txgoff] = NULL;
807 mutex_exit(&dn->dn_mtx);
808 }
809
810 if (freeing_dnode) {
811 dn->dn_objset->os_freed_dnodes++;
812 dnode_sync_free(dn, tx);
813 return;
814 }
815
816 if (dn->dn_num_slots > DNODE_MIN_SLOTS) {
817 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
818 mutex_enter(&ds->ds_lock);
819 ds->ds_feature_activation[SPA_FEATURE_LARGE_DNODE] =
820 (void *)B_TRUE;
821 mutex_exit(&ds->ds_lock);
822 }
823
824 if (dn->dn_next_nlevels[txgoff]) {
825 dnode_increase_indirection(dn, tx);
826 dn->dn_next_nlevels[txgoff] = 0;
827 }
828
829 /*
830 * This must be done after dnode_sync_free_range()
831 * and dnode_increase_indirection(). See dnode_new_blkid()
832 * for an explanation of the high bit being set.
833 */
834 if (dn->dn_next_maxblkid[txgoff]) {
835 mutex_enter(&dn->dn_mtx);
836 dnp->dn_maxblkid =
837 dn->dn_next_maxblkid[txgoff] & ~DMU_NEXT_MAXBLKID_SET;
838 dn->dn_next_maxblkid[txgoff] = 0;
839 mutex_exit(&dn->dn_mtx);
840 }
841
842 if (dn->dn_next_nblkptr[txgoff]) {
843 /* this should only happen on a realloc */
844 ASSERT(dn->dn_allocated_txg == tx->tx_txg);
845 if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) {
846 /* zero the new blkptrs we are gaining */
847 memset(dnp->dn_blkptr + dnp->dn_nblkptr, 0,
848 sizeof (blkptr_t) *
849 (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr));
850 #ifdef ZFS_DEBUG
851 } else {
852 int i;
853 ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr);
854 /* the blkptrs we are losing better be unallocated */
855 for (i = 0; i < dnp->dn_nblkptr; i++) {
856 if (i >= dn->dn_next_nblkptr[txgoff])
857 ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i]));
858 }
859 #endif
860 }
861 mutex_enter(&dn->dn_mtx);
862 dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff];
863 dn->dn_next_nblkptr[txgoff] = 0;
864 mutex_exit(&dn->dn_mtx);
865 }
866
867 dbuf_sync_list(list, dn->dn_phys->dn_nlevels - 1, tx);
868
869 if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
870 ASSERT3P(list_head(list), ==, NULL);
871 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
872 }
873
874 ASSERT3U(dnp->dn_bonuslen, <=, DN_MAX_BONUS_LEN(dnp));
875
876 /*
877 * Although we have dropped our reference to the dnode, it
878 * can't be evicted until its written, and we haven't yet
879 * initiated the IO for the dnode's dbuf. Additionally, the caller
880 * has already added a reference to the dnode because it's on the
881 * os_synced_dnodes list.
882 */
883 }
884