Allow dsl_deadlist_open() return errors
[zfs.git] / module / zfs / dnode.c
blobecc6761f8fa4344c0a9feae0b417612c99c25e46
1 /*
2 * CDDL HEADER START
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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
19 * CDDL HEADER END
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2020 by Delphix. All rights reserved.
24 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 #include <sys/zfs_context.h>
28 #include <sys/dbuf.h>
29 #include <sys/dnode.h>
30 #include <sys/dmu.h>
31 #include <sys/dmu_impl.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dmu_objset.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dsl_dataset.h>
36 #include <sys/spa.h>
37 #include <sys/zio.h>
38 #include <sys/dmu_zfetch.h>
39 #include <sys/range_tree.h>
40 #include <sys/trace_zfs.h>
41 #include <sys/zfs_project.h>
43 dnode_stats_t dnode_stats = {
44 { "dnode_hold_dbuf_hold", KSTAT_DATA_UINT64 },
45 { "dnode_hold_dbuf_read", KSTAT_DATA_UINT64 },
46 { "dnode_hold_alloc_hits", KSTAT_DATA_UINT64 },
47 { "dnode_hold_alloc_misses", KSTAT_DATA_UINT64 },
48 { "dnode_hold_alloc_interior", KSTAT_DATA_UINT64 },
49 { "dnode_hold_alloc_lock_retry", KSTAT_DATA_UINT64 },
50 { "dnode_hold_alloc_lock_misses", KSTAT_DATA_UINT64 },
51 { "dnode_hold_alloc_type_none", KSTAT_DATA_UINT64 },
52 { "dnode_hold_free_hits", KSTAT_DATA_UINT64 },
53 { "dnode_hold_free_misses", KSTAT_DATA_UINT64 },
54 { "dnode_hold_free_lock_misses", KSTAT_DATA_UINT64 },
55 { "dnode_hold_free_lock_retry", KSTAT_DATA_UINT64 },
56 { "dnode_hold_free_overflow", KSTAT_DATA_UINT64 },
57 { "dnode_hold_free_refcount", KSTAT_DATA_UINT64 },
58 { "dnode_free_interior_lock_retry", KSTAT_DATA_UINT64 },
59 { "dnode_allocate", KSTAT_DATA_UINT64 },
60 { "dnode_reallocate", KSTAT_DATA_UINT64 },
61 { "dnode_buf_evict", KSTAT_DATA_UINT64 },
62 { "dnode_alloc_next_chunk", KSTAT_DATA_UINT64 },
63 { "dnode_alloc_race", KSTAT_DATA_UINT64 },
64 { "dnode_alloc_next_block", KSTAT_DATA_UINT64 },
65 { "dnode_move_invalid", KSTAT_DATA_UINT64 },
66 { "dnode_move_recheck1", KSTAT_DATA_UINT64 },
67 { "dnode_move_recheck2", KSTAT_DATA_UINT64 },
68 { "dnode_move_special", KSTAT_DATA_UINT64 },
69 { "dnode_move_handle", KSTAT_DATA_UINT64 },
70 { "dnode_move_rwlock", KSTAT_DATA_UINT64 },
71 { "dnode_move_active", KSTAT_DATA_UINT64 },
74 dnode_sums_t dnode_sums;
76 static kstat_t *dnode_ksp;
77 static kmem_cache_t *dnode_cache;
79 static dnode_phys_t dnode_phys_zero __maybe_unused;
81 int zfs_default_bs = SPA_MINBLOCKSHIFT;
82 int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
84 #ifdef _KERNEL
85 static kmem_cbrc_t dnode_move(void *, void *, size_t, void *);
86 #endif /* _KERNEL */
88 static int
89 dbuf_compare(const void *x1, const void *x2)
91 const dmu_buf_impl_t *d1 = x1;
92 const dmu_buf_impl_t *d2 = x2;
94 int cmp = TREE_CMP(d1->db_level, d2->db_level);
95 if (likely(cmp))
96 return (cmp);
98 cmp = TREE_CMP(d1->db_blkid, d2->db_blkid);
99 if (likely(cmp))
100 return (cmp);
102 if (d1->db_state == DB_MARKER) {
103 ASSERT3S(d2->db_state, !=, DB_MARKER);
104 return (TREE_PCMP(d1->db_parent, d2));
105 } else if (d2->db_state == DB_MARKER) {
106 ASSERT3S(d1->db_state, !=, DB_MARKER);
107 return (TREE_PCMP(d1, d2->db_parent));
110 if (d1->db_state == DB_SEARCH) {
111 ASSERT3S(d2->db_state, !=, DB_SEARCH);
112 return (-1);
113 } else if (d2->db_state == DB_SEARCH) {
114 ASSERT3S(d1->db_state, !=, DB_SEARCH);
115 return (1);
118 return (TREE_PCMP(d1, d2));
121 static int
122 dnode_cons(void *arg, void *unused, int kmflag)
124 (void) unused, (void) kmflag;
125 dnode_t *dn = arg;
127 rw_init(&dn->dn_struct_rwlock, NULL, RW_NOLOCKDEP, NULL);
128 mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
129 mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
130 cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL);
131 cv_init(&dn->dn_nodnholds, NULL, CV_DEFAULT, NULL);
134 * Every dbuf has a reference, and dropping a tracked reference is
135 * O(number of references), so don't track dn_holds.
137 zfs_refcount_create_untracked(&dn->dn_holds);
138 zfs_refcount_create(&dn->dn_tx_holds);
139 list_link_init(&dn->dn_link);
141 memset(dn->dn_next_type, 0, sizeof (dn->dn_next_type));
142 memset(dn->dn_next_nblkptr, 0, sizeof (dn->dn_next_nblkptr));
143 memset(dn->dn_next_nlevels, 0, sizeof (dn->dn_next_nlevels));
144 memset(dn->dn_next_indblkshift, 0, sizeof (dn->dn_next_indblkshift));
145 memset(dn->dn_next_bonustype, 0, sizeof (dn->dn_next_bonustype));
146 memset(dn->dn_rm_spillblk, 0, sizeof (dn->dn_rm_spillblk));
147 memset(dn->dn_next_bonuslen, 0, sizeof (dn->dn_next_bonuslen));
148 memset(dn->dn_next_blksz, 0, sizeof (dn->dn_next_blksz));
149 memset(dn->dn_next_maxblkid, 0, sizeof (dn->dn_next_maxblkid));
151 for (int i = 0; i < TXG_SIZE; i++) {
152 multilist_link_init(&dn->dn_dirty_link[i]);
153 dn->dn_free_ranges[i] = NULL;
154 list_create(&dn->dn_dirty_records[i],
155 sizeof (dbuf_dirty_record_t),
156 offsetof(dbuf_dirty_record_t, dr_dirty_node));
159 dn->dn_allocated_txg = 0;
160 dn->dn_free_txg = 0;
161 dn->dn_assigned_txg = 0;
162 dn->dn_dirty_txg = 0;
163 dn->dn_dirtyctx = 0;
164 dn->dn_dirtyctx_firstset = NULL;
165 dn->dn_bonus = NULL;
166 dn->dn_have_spill = B_FALSE;
167 dn->dn_zio = NULL;
168 dn->dn_oldused = 0;
169 dn->dn_oldflags = 0;
170 dn->dn_olduid = 0;
171 dn->dn_oldgid = 0;
172 dn->dn_oldprojid = ZFS_DEFAULT_PROJID;
173 dn->dn_newuid = 0;
174 dn->dn_newgid = 0;
175 dn->dn_newprojid = ZFS_DEFAULT_PROJID;
176 dn->dn_id_flags = 0;
178 dn->dn_dbufs_count = 0;
179 avl_create(&dn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t),
180 offsetof(dmu_buf_impl_t, db_link));
182 dn->dn_moved = 0;
183 return (0);
186 static void
187 dnode_dest(void *arg, void *unused)
189 (void) unused;
190 dnode_t *dn = arg;
192 rw_destroy(&dn->dn_struct_rwlock);
193 mutex_destroy(&dn->dn_mtx);
194 mutex_destroy(&dn->dn_dbufs_mtx);
195 cv_destroy(&dn->dn_notxholds);
196 cv_destroy(&dn->dn_nodnholds);
197 zfs_refcount_destroy(&dn->dn_holds);
198 zfs_refcount_destroy(&dn->dn_tx_holds);
199 ASSERT(!list_link_active(&dn->dn_link));
201 for (int i = 0; i < TXG_SIZE; i++) {
202 ASSERT(!multilist_link_active(&dn->dn_dirty_link[i]));
203 ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
204 list_destroy(&dn->dn_dirty_records[i]);
205 ASSERT0(dn->dn_next_nblkptr[i]);
206 ASSERT0(dn->dn_next_nlevels[i]);
207 ASSERT0(dn->dn_next_indblkshift[i]);
208 ASSERT0(dn->dn_next_bonustype[i]);
209 ASSERT0(dn->dn_rm_spillblk[i]);
210 ASSERT0(dn->dn_next_bonuslen[i]);
211 ASSERT0(dn->dn_next_blksz[i]);
212 ASSERT0(dn->dn_next_maxblkid[i]);
215 ASSERT0(dn->dn_allocated_txg);
216 ASSERT0(dn->dn_free_txg);
217 ASSERT0(dn->dn_assigned_txg);
218 ASSERT0(dn->dn_dirty_txg);
219 ASSERT0(dn->dn_dirtyctx);
220 ASSERT3P(dn->dn_dirtyctx_firstset, ==, NULL);
221 ASSERT3P(dn->dn_bonus, ==, NULL);
222 ASSERT(!dn->dn_have_spill);
223 ASSERT3P(dn->dn_zio, ==, NULL);
224 ASSERT0(dn->dn_oldused);
225 ASSERT0(dn->dn_oldflags);
226 ASSERT0(dn->dn_olduid);
227 ASSERT0(dn->dn_oldgid);
228 ASSERT0(dn->dn_oldprojid);
229 ASSERT0(dn->dn_newuid);
230 ASSERT0(dn->dn_newgid);
231 ASSERT0(dn->dn_newprojid);
232 ASSERT0(dn->dn_id_flags);
234 ASSERT0(dn->dn_dbufs_count);
235 avl_destroy(&dn->dn_dbufs);
238 static int
239 dnode_kstats_update(kstat_t *ksp, int rw)
241 dnode_stats_t *ds = ksp->ks_data;
243 if (rw == KSTAT_WRITE)
244 return (EACCES);
245 ds->dnode_hold_dbuf_hold.value.ui64 =
246 wmsum_value(&dnode_sums.dnode_hold_dbuf_hold);
247 ds->dnode_hold_dbuf_read.value.ui64 =
248 wmsum_value(&dnode_sums.dnode_hold_dbuf_read);
249 ds->dnode_hold_alloc_hits.value.ui64 =
250 wmsum_value(&dnode_sums.dnode_hold_alloc_hits);
251 ds->dnode_hold_alloc_misses.value.ui64 =
252 wmsum_value(&dnode_sums.dnode_hold_alloc_misses);
253 ds->dnode_hold_alloc_interior.value.ui64 =
254 wmsum_value(&dnode_sums.dnode_hold_alloc_interior);
255 ds->dnode_hold_alloc_lock_retry.value.ui64 =
256 wmsum_value(&dnode_sums.dnode_hold_alloc_lock_retry);
257 ds->dnode_hold_alloc_lock_misses.value.ui64 =
258 wmsum_value(&dnode_sums.dnode_hold_alloc_lock_misses);
259 ds->dnode_hold_alloc_type_none.value.ui64 =
260 wmsum_value(&dnode_sums.dnode_hold_alloc_type_none);
261 ds->dnode_hold_free_hits.value.ui64 =
262 wmsum_value(&dnode_sums.dnode_hold_free_hits);
263 ds->dnode_hold_free_misses.value.ui64 =
264 wmsum_value(&dnode_sums.dnode_hold_free_misses);
265 ds->dnode_hold_free_lock_misses.value.ui64 =
266 wmsum_value(&dnode_sums.dnode_hold_free_lock_misses);
267 ds->dnode_hold_free_lock_retry.value.ui64 =
268 wmsum_value(&dnode_sums.dnode_hold_free_lock_retry);
269 ds->dnode_hold_free_refcount.value.ui64 =
270 wmsum_value(&dnode_sums.dnode_hold_free_refcount);
271 ds->dnode_hold_free_overflow.value.ui64 =
272 wmsum_value(&dnode_sums.dnode_hold_free_overflow);
273 ds->dnode_free_interior_lock_retry.value.ui64 =
274 wmsum_value(&dnode_sums.dnode_free_interior_lock_retry);
275 ds->dnode_allocate.value.ui64 =
276 wmsum_value(&dnode_sums.dnode_allocate);
277 ds->dnode_reallocate.value.ui64 =
278 wmsum_value(&dnode_sums.dnode_reallocate);
279 ds->dnode_buf_evict.value.ui64 =
280 wmsum_value(&dnode_sums.dnode_buf_evict);
281 ds->dnode_alloc_next_chunk.value.ui64 =
282 wmsum_value(&dnode_sums.dnode_alloc_next_chunk);
283 ds->dnode_alloc_race.value.ui64 =
284 wmsum_value(&dnode_sums.dnode_alloc_race);
285 ds->dnode_alloc_next_block.value.ui64 =
286 wmsum_value(&dnode_sums.dnode_alloc_next_block);
287 ds->dnode_move_invalid.value.ui64 =
288 wmsum_value(&dnode_sums.dnode_move_invalid);
289 ds->dnode_move_recheck1.value.ui64 =
290 wmsum_value(&dnode_sums.dnode_move_recheck1);
291 ds->dnode_move_recheck2.value.ui64 =
292 wmsum_value(&dnode_sums.dnode_move_recheck2);
293 ds->dnode_move_special.value.ui64 =
294 wmsum_value(&dnode_sums.dnode_move_special);
295 ds->dnode_move_handle.value.ui64 =
296 wmsum_value(&dnode_sums.dnode_move_handle);
297 ds->dnode_move_rwlock.value.ui64 =
298 wmsum_value(&dnode_sums.dnode_move_rwlock);
299 ds->dnode_move_active.value.ui64 =
300 wmsum_value(&dnode_sums.dnode_move_active);
301 return (0);
304 void
305 dnode_init(void)
307 ASSERT(dnode_cache == NULL);
308 dnode_cache = kmem_cache_create("dnode_t", sizeof (dnode_t),
309 0, dnode_cons, dnode_dest, NULL, NULL, NULL, KMC_RECLAIMABLE);
310 kmem_cache_set_move(dnode_cache, dnode_move);
312 wmsum_init(&dnode_sums.dnode_hold_dbuf_hold, 0);
313 wmsum_init(&dnode_sums.dnode_hold_dbuf_read, 0);
314 wmsum_init(&dnode_sums.dnode_hold_alloc_hits, 0);
315 wmsum_init(&dnode_sums.dnode_hold_alloc_misses, 0);
316 wmsum_init(&dnode_sums.dnode_hold_alloc_interior, 0);
317 wmsum_init(&dnode_sums.dnode_hold_alloc_lock_retry, 0);
318 wmsum_init(&dnode_sums.dnode_hold_alloc_lock_misses, 0);
319 wmsum_init(&dnode_sums.dnode_hold_alloc_type_none, 0);
320 wmsum_init(&dnode_sums.dnode_hold_free_hits, 0);
321 wmsum_init(&dnode_sums.dnode_hold_free_misses, 0);
322 wmsum_init(&dnode_sums.dnode_hold_free_lock_misses, 0);
323 wmsum_init(&dnode_sums.dnode_hold_free_lock_retry, 0);
324 wmsum_init(&dnode_sums.dnode_hold_free_refcount, 0);
325 wmsum_init(&dnode_sums.dnode_hold_free_overflow, 0);
326 wmsum_init(&dnode_sums.dnode_free_interior_lock_retry, 0);
327 wmsum_init(&dnode_sums.dnode_allocate, 0);
328 wmsum_init(&dnode_sums.dnode_reallocate, 0);
329 wmsum_init(&dnode_sums.dnode_buf_evict, 0);
330 wmsum_init(&dnode_sums.dnode_alloc_next_chunk, 0);
331 wmsum_init(&dnode_sums.dnode_alloc_race, 0);
332 wmsum_init(&dnode_sums.dnode_alloc_next_block, 0);
333 wmsum_init(&dnode_sums.dnode_move_invalid, 0);
334 wmsum_init(&dnode_sums.dnode_move_recheck1, 0);
335 wmsum_init(&dnode_sums.dnode_move_recheck2, 0);
336 wmsum_init(&dnode_sums.dnode_move_special, 0);
337 wmsum_init(&dnode_sums.dnode_move_handle, 0);
338 wmsum_init(&dnode_sums.dnode_move_rwlock, 0);
339 wmsum_init(&dnode_sums.dnode_move_active, 0);
341 dnode_ksp = kstat_create("zfs", 0, "dnodestats", "misc",
342 KSTAT_TYPE_NAMED, sizeof (dnode_stats) / sizeof (kstat_named_t),
343 KSTAT_FLAG_VIRTUAL);
344 if (dnode_ksp != NULL) {
345 dnode_ksp->ks_data = &dnode_stats;
346 dnode_ksp->ks_update = dnode_kstats_update;
347 kstat_install(dnode_ksp);
351 void
352 dnode_fini(void)
354 if (dnode_ksp != NULL) {
355 kstat_delete(dnode_ksp);
356 dnode_ksp = NULL;
359 wmsum_fini(&dnode_sums.dnode_hold_dbuf_hold);
360 wmsum_fini(&dnode_sums.dnode_hold_dbuf_read);
361 wmsum_fini(&dnode_sums.dnode_hold_alloc_hits);
362 wmsum_fini(&dnode_sums.dnode_hold_alloc_misses);
363 wmsum_fini(&dnode_sums.dnode_hold_alloc_interior);
364 wmsum_fini(&dnode_sums.dnode_hold_alloc_lock_retry);
365 wmsum_fini(&dnode_sums.dnode_hold_alloc_lock_misses);
366 wmsum_fini(&dnode_sums.dnode_hold_alloc_type_none);
367 wmsum_fini(&dnode_sums.dnode_hold_free_hits);
368 wmsum_fini(&dnode_sums.dnode_hold_free_misses);
369 wmsum_fini(&dnode_sums.dnode_hold_free_lock_misses);
370 wmsum_fini(&dnode_sums.dnode_hold_free_lock_retry);
371 wmsum_fini(&dnode_sums.dnode_hold_free_refcount);
372 wmsum_fini(&dnode_sums.dnode_hold_free_overflow);
373 wmsum_fini(&dnode_sums.dnode_free_interior_lock_retry);
374 wmsum_fini(&dnode_sums.dnode_allocate);
375 wmsum_fini(&dnode_sums.dnode_reallocate);
376 wmsum_fini(&dnode_sums.dnode_buf_evict);
377 wmsum_fini(&dnode_sums.dnode_alloc_next_chunk);
378 wmsum_fini(&dnode_sums.dnode_alloc_race);
379 wmsum_fini(&dnode_sums.dnode_alloc_next_block);
380 wmsum_fini(&dnode_sums.dnode_move_invalid);
381 wmsum_fini(&dnode_sums.dnode_move_recheck1);
382 wmsum_fini(&dnode_sums.dnode_move_recheck2);
383 wmsum_fini(&dnode_sums.dnode_move_special);
384 wmsum_fini(&dnode_sums.dnode_move_handle);
385 wmsum_fini(&dnode_sums.dnode_move_rwlock);
386 wmsum_fini(&dnode_sums.dnode_move_active);
388 kmem_cache_destroy(dnode_cache);
389 dnode_cache = NULL;
393 #ifdef ZFS_DEBUG
394 void
395 dnode_verify(dnode_t *dn)
397 int drop_struct_lock = FALSE;
399 ASSERT(dn->dn_phys);
400 ASSERT(dn->dn_objset);
401 ASSERT(dn->dn_handle->dnh_dnode == dn);
403 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
405 if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
406 return;
408 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
409 rw_enter(&dn->dn_struct_rwlock, RW_READER);
410 drop_struct_lock = TRUE;
412 if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
413 int i;
414 int max_bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots);
415 ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
416 if (dn->dn_datablkshift) {
417 ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
418 ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
419 ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
421 ASSERT3U(dn->dn_nlevels, <=, 30);
422 ASSERT(DMU_OT_IS_VALID(dn->dn_type));
423 ASSERT3U(dn->dn_nblkptr, >=, 1);
424 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
425 ASSERT3U(dn->dn_bonuslen, <=, max_bonuslen);
426 ASSERT3U(dn->dn_datablksz, ==,
427 dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
428 ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
429 ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
430 dn->dn_bonuslen, <=, max_bonuslen);
431 for (i = 0; i < TXG_SIZE; i++) {
432 ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
435 if (dn->dn_phys->dn_type != DMU_OT_NONE)
436 ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
437 ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_object) || dn->dn_dbuf != NULL);
438 if (dn->dn_dbuf != NULL) {
439 ASSERT3P(dn->dn_phys, ==,
440 (dnode_phys_t *)dn->dn_dbuf->db.db_data +
441 (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
443 if (drop_struct_lock)
444 rw_exit(&dn->dn_struct_rwlock);
446 #endif
448 void
449 dnode_byteswap(dnode_phys_t *dnp)
451 uint64_t *buf64 = (void*)&dnp->dn_blkptr;
452 int i;
454 if (dnp->dn_type == DMU_OT_NONE) {
455 memset(dnp, 0, sizeof (dnode_phys_t));
456 return;
459 dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
460 dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
461 dnp->dn_extra_slots = BSWAP_8(dnp->dn_extra_slots);
462 dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
463 dnp->dn_used = BSWAP_64(dnp->dn_used);
466 * dn_nblkptr is only one byte, so it's OK to read it in either
467 * byte order. We can't read dn_bouslen.
469 ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
470 ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
471 for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
472 buf64[i] = BSWAP_64(buf64[i]);
475 * OK to check dn_bonuslen for zero, because it won't matter if
476 * we have the wrong byte order. This is necessary because the
477 * dnode dnode is smaller than a regular dnode.
479 if (dnp->dn_bonuslen != 0) {
480 dmu_object_byteswap_t byteswap;
481 ASSERT(DMU_OT_IS_VALID(dnp->dn_bonustype));
482 byteswap = DMU_OT_BYTESWAP(dnp->dn_bonustype);
483 dmu_ot_byteswap[byteswap].ob_func(DN_BONUS(dnp),
484 DN_MAX_BONUS_LEN(dnp));
487 /* Swap SPILL block if we have one */
488 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
489 byteswap_uint64_array(DN_SPILL_BLKPTR(dnp), sizeof (blkptr_t));
492 void
493 dnode_buf_byteswap(void *vbuf, size_t size)
495 int i = 0;
497 ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
498 ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
500 while (i < size) {
501 dnode_phys_t *dnp = (void *)(((char *)vbuf) + i);
502 dnode_byteswap(dnp);
504 i += DNODE_MIN_SIZE;
505 if (dnp->dn_type != DMU_OT_NONE)
506 i += dnp->dn_extra_slots * DNODE_MIN_SIZE;
510 void
511 dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx)
513 ASSERT3U(zfs_refcount_count(&dn->dn_holds), >=, 1);
515 dnode_setdirty(dn, tx);
516 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
517 ASSERT3U(newsize, <=, DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots) -
518 (dn->dn_nblkptr-1) * sizeof (blkptr_t));
520 if (newsize < dn->dn_bonuslen) {
521 /* clear any data after the end of the new size */
522 size_t diff = dn->dn_bonuslen - newsize;
523 char *data_end = ((char *)dn->dn_bonus->db.db_data) + newsize;
524 memset(data_end, 0, diff);
527 dn->dn_bonuslen = newsize;
528 if (newsize == 0)
529 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN;
530 else
531 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
532 rw_exit(&dn->dn_struct_rwlock);
535 void
536 dnode_setbonus_type(dnode_t *dn, dmu_object_type_t newtype, dmu_tx_t *tx)
538 ASSERT3U(zfs_refcount_count(&dn->dn_holds), >=, 1);
539 dnode_setdirty(dn, tx);
540 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
541 dn->dn_bonustype = newtype;
542 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
543 rw_exit(&dn->dn_struct_rwlock);
546 void
547 dnode_set_storage_type(dnode_t *dn, dmu_object_type_t newtype)
550 * This is not in the dnode_phys, but it should be, and perhaps one day
551 * will. For now we require it be set after taking a hold.
553 ASSERT3U(zfs_refcount_count(&dn->dn_holds), >=, 1);
554 dn->dn_storage_type = newtype;
557 void
558 dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx)
560 ASSERT3U(zfs_refcount_count(&dn->dn_holds), >=, 1);
561 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
562 dnode_setdirty(dn, tx);
563 dn->dn_rm_spillblk[tx->tx_txg & TXG_MASK] = DN_KILL_SPILLBLK;
564 dn->dn_have_spill = B_FALSE;
567 static void
568 dnode_setdblksz(dnode_t *dn, int size)
570 ASSERT0(P2PHASE(size, SPA_MINBLOCKSIZE));
571 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
572 ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
573 ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
574 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
575 dn->dn_datablksz = size;
576 dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
577 dn->dn_datablkshift = ISP2(size) ? highbit64(size - 1) : 0;
580 static dnode_t *
581 dnode_create(objset_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
582 uint64_t object, dnode_handle_t *dnh)
584 dnode_t *dn;
586 dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
587 dn->dn_moved = 0;
590 * Defer setting dn_objset until the dnode is ready to be a candidate
591 * for the dnode_move() callback.
593 dn->dn_object = object;
594 dn->dn_dbuf = db;
595 dn->dn_handle = dnh;
596 dn->dn_phys = dnp;
598 if (dnp->dn_datablkszsec) {
599 dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
600 } else {
601 dn->dn_datablksz = 0;
602 dn->dn_datablkszsec = 0;
603 dn->dn_datablkshift = 0;
605 dn->dn_indblkshift = dnp->dn_indblkshift;
606 dn->dn_nlevels = dnp->dn_nlevels;
607 dn->dn_type = dnp->dn_type;
608 dn->dn_nblkptr = dnp->dn_nblkptr;
609 dn->dn_checksum = dnp->dn_checksum;
610 dn->dn_compress = dnp->dn_compress;
611 dn->dn_bonustype = dnp->dn_bonustype;
612 dn->dn_bonuslen = dnp->dn_bonuslen;
613 dn->dn_num_slots = dnp->dn_extra_slots + 1;
614 dn->dn_maxblkid = dnp->dn_maxblkid;
615 dn->dn_have_spill = ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0);
616 dn->dn_id_flags = 0;
618 dn->dn_storage_type = DMU_OT_NONE;
620 dmu_zfetch_init(&dn->dn_zfetch, dn);
622 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
623 ASSERT(zrl_is_locked(&dnh->dnh_zrlock));
624 ASSERT(!DN_SLOT_IS_PTR(dnh->dnh_dnode));
626 mutex_enter(&os->os_lock);
629 * Exclude special dnodes from os_dnodes so an empty os_dnodes
630 * signifies that the special dnodes have no references from
631 * their children (the entries in os_dnodes). This allows
632 * dnode_destroy() to easily determine if the last child has
633 * been removed and then complete eviction of the objset.
635 if (!DMU_OBJECT_IS_SPECIAL(object))
636 list_insert_head(&os->os_dnodes, dn);
637 membar_producer();
640 * Everything else must be valid before assigning dn_objset
641 * makes the dnode eligible for dnode_move().
643 dn->dn_objset = os;
645 dnh->dnh_dnode = dn;
646 mutex_exit(&os->os_lock);
648 arc_space_consume(sizeof (dnode_t), ARC_SPACE_DNODE);
650 return (dn);
654 * Caller must be holding the dnode handle, which is released upon return.
656 static void
657 dnode_destroy(dnode_t *dn)
659 objset_t *os = dn->dn_objset;
660 boolean_t complete_os_eviction = B_FALSE;
662 ASSERT((dn->dn_id_flags & DN_ID_NEW_EXIST) == 0);
664 mutex_enter(&os->os_lock);
665 POINTER_INVALIDATE(&dn->dn_objset);
666 if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
667 list_remove(&os->os_dnodes, dn);
668 complete_os_eviction =
669 list_is_empty(&os->os_dnodes) &&
670 list_link_active(&os->os_evicting_node);
672 mutex_exit(&os->os_lock);
674 /* the dnode can no longer move, so we can release the handle */
675 if (!zrl_is_locked(&dn->dn_handle->dnh_zrlock))
676 zrl_remove(&dn->dn_handle->dnh_zrlock);
678 dn->dn_allocated_txg = 0;
679 dn->dn_free_txg = 0;
680 dn->dn_assigned_txg = 0;
681 dn->dn_dirty_txg = 0;
683 dn->dn_dirtyctx = 0;
684 dn->dn_dirtyctx_firstset = NULL;
685 if (dn->dn_bonus != NULL) {
686 mutex_enter(&dn->dn_bonus->db_mtx);
687 dbuf_destroy(dn->dn_bonus);
688 dn->dn_bonus = NULL;
690 dn->dn_zio = NULL;
692 dn->dn_have_spill = B_FALSE;
693 dn->dn_oldused = 0;
694 dn->dn_oldflags = 0;
695 dn->dn_olduid = 0;
696 dn->dn_oldgid = 0;
697 dn->dn_oldprojid = ZFS_DEFAULT_PROJID;
698 dn->dn_newuid = 0;
699 dn->dn_newgid = 0;
700 dn->dn_newprojid = ZFS_DEFAULT_PROJID;
701 dn->dn_id_flags = 0;
703 dn->dn_storage_type = DMU_OT_NONE;
705 dmu_zfetch_fini(&dn->dn_zfetch);
706 kmem_cache_free(dnode_cache, dn);
707 arc_space_return(sizeof (dnode_t), ARC_SPACE_DNODE);
709 if (complete_os_eviction)
710 dmu_objset_evict_done(os);
713 void
714 dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
715 dmu_object_type_t bonustype, int bonuslen, int dn_slots, dmu_tx_t *tx)
717 int i;
719 ASSERT3U(dn_slots, >, 0);
720 ASSERT3U(dn_slots << DNODE_SHIFT, <=,
721 spa_maxdnodesize(dmu_objset_spa(dn->dn_objset)));
722 ASSERT3U(blocksize, <=,
723 spa_maxblocksize(dmu_objset_spa(dn->dn_objset)));
724 if (blocksize == 0)
725 blocksize = 1 << zfs_default_bs;
726 else
727 blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
729 if (ibs == 0)
730 ibs = zfs_default_ibs;
732 ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
734 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d dn_slots=%d\n",
735 dn->dn_objset, (u_longlong_t)dn->dn_object,
736 (u_longlong_t)tx->tx_txg, blocksize, ibs, dn_slots);
737 DNODE_STAT_BUMP(dnode_allocate);
739 ASSERT(dn->dn_type == DMU_OT_NONE);
740 ASSERT0(memcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)));
741 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
742 ASSERT(ot != DMU_OT_NONE);
743 ASSERT(DMU_OT_IS_VALID(ot));
744 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
745 (bonustype == DMU_OT_SA && bonuslen == 0) ||
746 (bonustype == DMU_OTN_UINT64_METADATA && bonuslen == 0) ||
747 (bonustype != DMU_OT_NONE && bonuslen != 0));
748 ASSERT(DMU_OT_IS_VALID(bonustype));
749 ASSERT3U(bonuslen, <=, DN_SLOTS_TO_BONUSLEN(dn_slots));
750 ASSERT(dn->dn_type == DMU_OT_NONE);
751 ASSERT0(dn->dn_maxblkid);
752 ASSERT0(dn->dn_allocated_txg);
753 ASSERT0(dn->dn_assigned_txg);
754 ASSERT(zfs_refcount_is_zero(&dn->dn_tx_holds));
755 ASSERT3U(zfs_refcount_count(&dn->dn_holds), <=, 1);
756 ASSERT(avl_is_empty(&dn->dn_dbufs));
758 for (i = 0; i < TXG_SIZE; i++) {
759 ASSERT0(dn->dn_next_nblkptr[i]);
760 ASSERT0(dn->dn_next_nlevels[i]);
761 ASSERT0(dn->dn_next_indblkshift[i]);
762 ASSERT0(dn->dn_next_bonuslen[i]);
763 ASSERT0(dn->dn_next_bonustype[i]);
764 ASSERT0(dn->dn_rm_spillblk[i]);
765 ASSERT0(dn->dn_next_blksz[i]);
766 ASSERT0(dn->dn_next_maxblkid[i]);
767 ASSERT(!multilist_link_active(&dn->dn_dirty_link[i]));
768 ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
769 ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
772 dn->dn_type = ot;
773 dnode_setdblksz(dn, blocksize);
774 dn->dn_indblkshift = ibs;
775 dn->dn_nlevels = 1;
776 dn->dn_num_slots = dn_slots;
777 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
778 dn->dn_nblkptr = 1;
779 else {
780 dn->dn_nblkptr = MIN(DN_MAX_NBLKPTR,
781 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots) - bonuslen) >>
782 SPA_BLKPTRSHIFT));
785 dn->dn_bonustype = bonustype;
786 dn->dn_bonuslen = bonuslen;
787 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
788 dn->dn_compress = ZIO_COMPRESS_INHERIT;
789 dn->dn_dirtyctx = 0;
791 dn->dn_free_txg = 0;
792 dn->dn_dirtyctx_firstset = NULL;
793 dn->dn_dirty_txg = 0;
795 dn->dn_allocated_txg = tx->tx_txg;
796 dn->dn_id_flags = 0;
798 dnode_setdirty(dn, tx);
799 dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
800 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
801 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
802 dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
805 void
806 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
807 dmu_object_type_t bonustype, int bonuslen, int dn_slots,
808 boolean_t keep_spill, dmu_tx_t *tx)
810 int nblkptr;
812 ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
813 ASSERT3U(blocksize, <=,
814 spa_maxblocksize(dmu_objset_spa(dn->dn_objset)));
815 ASSERT0(blocksize % SPA_MINBLOCKSIZE);
816 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
817 ASSERT(tx->tx_txg != 0);
818 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
819 (bonustype != DMU_OT_NONE && bonuslen != 0) ||
820 (bonustype == DMU_OT_SA && bonuslen == 0));
821 ASSERT(DMU_OT_IS_VALID(bonustype));
822 ASSERT3U(bonuslen, <=,
823 DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(dn->dn_objset))));
824 ASSERT3U(bonuslen, <=, DN_BONUS_SIZE(dn_slots << DNODE_SHIFT));
826 dnode_free_interior_slots(dn);
827 DNODE_STAT_BUMP(dnode_reallocate);
829 /* clean up any unreferenced dbufs */
830 dnode_evict_dbufs(dn);
832 dn->dn_id_flags = 0;
834 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
835 dnode_setdirty(dn, tx);
836 if (dn->dn_datablksz != blocksize) {
837 /* change blocksize */
838 ASSERT0(dn->dn_maxblkid);
839 ASSERT(BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
840 dnode_block_freed(dn, 0));
842 dnode_setdblksz(dn, blocksize);
843 dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = blocksize;
845 if (dn->dn_bonuslen != bonuslen)
846 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = bonuslen;
848 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
849 nblkptr = 1;
850 else
851 nblkptr = MIN(DN_MAX_NBLKPTR,
852 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots) - bonuslen) >>
853 SPA_BLKPTRSHIFT));
854 if (dn->dn_bonustype != bonustype)
855 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = bonustype;
856 if (dn->dn_nblkptr != nblkptr)
857 dn->dn_next_nblkptr[tx->tx_txg & TXG_MASK] = nblkptr;
858 if (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR && !keep_spill) {
859 dbuf_rm_spill(dn, tx);
860 dnode_rm_spill(dn, tx);
863 rw_exit(&dn->dn_struct_rwlock);
865 /* change type */
866 dn->dn_type = ot;
868 /* change bonus size and type */
869 mutex_enter(&dn->dn_mtx);
870 dn->dn_bonustype = bonustype;
871 dn->dn_bonuslen = bonuslen;
872 dn->dn_num_slots = dn_slots;
873 dn->dn_nblkptr = nblkptr;
874 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
875 dn->dn_compress = ZIO_COMPRESS_INHERIT;
876 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
878 /* fix up the bonus db_size */
879 if (dn->dn_bonus) {
880 dn->dn_bonus->db.db_size =
881 DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots) -
882 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
883 ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
886 dn->dn_allocated_txg = tx->tx_txg;
887 mutex_exit(&dn->dn_mtx);
890 #ifdef _KERNEL
891 static void
892 dnode_move_impl(dnode_t *odn, dnode_t *ndn)
894 ASSERT(!RW_LOCK_HELD(&odn->dn_struct_rwlock));
895 ASSERT(MUTEX_NOT_HELD(&odn->dn_mtx));
896 ASSERT(MUTEX_NOT_HELD(&odn->dn_dbufs_mtx));
898 /* Copy fields. */
899 ndn->dn_objset = odn->dn_objset;
900 ndn->dn_object = odn->dn_object;
901 ndn->dn_dbuf = odn->dn_dbuf;
902 ndn->dn_handle = odn->dn_handle;
903 ndn->dn_phys = odn->dn_phys;
904 ndn->dn_type = odn->dn_type;
905 ndn->dn_bonuslen = odn->dn_bonuslen;
906 ndn->dn_bonustype = odn->dn_bonustype;
907 ndn->dn_nblkptr = odn->dn_nblkptr;
908 ndn->dn_checksum = odn->dn_checksum;
909 ndn->dn_compress = odn->dn_compress;
910 ndn->dn_nlevels = odn->dn_nlevels;
911 ndn->dn_indblkshift = odn->dn_indblkshift;
912 ndn->dn_datablkshift = odn->dn_datablkshift;
913 ndn->dn_datablkszsec = odn->dn_datablkszsec;
914 ndn->dn_datablksz = odn->dn_datablksz;
915 ndn->dn_maxblkid = odn->dn_maxblkid;
916 ndn->dn_num_slots = odn->dn_num_slots;
917 memcpy(ndn->dn_next_type, odn->dn_next_type,
918 sizeof (odn->dn_next_type));
919 memcpy(ndn->dn_next_nblkptr, odn->dn_next_nblkptr,
920 sizeof (odn->dn_next_nblkptr));
921 memcpy(ndn->dn_next_nlevels, odn->dn_next_nlevels,
922 sizeof (odn->dn_next_nlevels));
923 memcpy(ndn->dn_next_indblkshift, odn->dn_next_indblkshift,
924 sizeof (odn->dn_next_indblkshift));
925 memcpy(ndn->dn_next_bonustype, odn->dn_next_bonustype,
926 sizeof (odn->dn_next_bonustype));
927 memcpy(ndn->dn_rm_spillblk, odn->dn_rm_spillblk,
928 sizeof (odn->dn_rm_spillblk));
929 memcpy(ndn->dn_next_bonuslen, odn->dn_next_bonuslen,
930 sizeof (odn->dn_next_bonuslen));
931 memcpy(ndn->dn_next_blksz, odn->dn_next_blksz,
932 sizeof (odn->dn_next_blksz));
933 memcpy(ndn->dn_next_maxblkid, odn->dn_next_maxblkid,
934 sizeof (odn->dn_next_maxblkid));
935 for (int i = 0; i < TXG_SIZE; i++) {
936 list_move_tail(&ndn->dn_dirty_records[i],
937 &odn->dn_dirty_records[i]);
939 memcpy(ndn->dn_free_ranges, odn->dn_free_ranges,
940 sizeof (odn->dn_free_ranges));
941 ndn->dn_allocated_txg = odn->dn_allocated_txg;
942 ndn->dn_free_txg = odn->dn_free_txg;
943 ndn->dn_assigned_txg = odn->dn_assigned_txg;
944 ndn->dn_dirty_txg = odn->dn_dirty_txg;
945 ndn->dn_dirtyctx = odn->dn_dirtyctx;
946 ndn->dn_dirtyctx_firstset = odn->dn_dirtyctx_firstset;
947 ASSERT(zfs_refcount_count(&odn->dn_tx_holds) == 0);
948 zfs_refcount_transfer(&ndn->dn_holds, &odn->dn_holds);
949 ASSERT(avl_is_empty(&ndn->dn_dbufs));
950 avl_swap(&ndn->dn_dbufs, &odn->dn_dbufs);
951 ndn->dn_dbufs_count = odn->dn_dbufs_count;
952 ndn->dn_bonus = odn->dn_bonus;
953 ndn->dn_have_spill = odn->dn_have_spill;
954 ndn->dn_zio = odn->dn_zio;
955 ndn->dn_oldused = odn->dn_oldused;
956 ndn->dn_oldflags = odn->dn_oldflags;
957 ndn->dn_olduid = odn->dn_olduid;
958 ndn->dn_oldgid = odn->dn_oldgid;
959 ndn->dn_oldprojid = odn->dn_oldprojid;
960 ndn->dn_newuid = odn->dn_newuid;
961 ndn->dn_newgid = odn->dn_newgid;
962 ndn->dn_newprojid = odn->dn_newprojid;
963 ndn->dn_id_flags = odn->dn_id_flags;
964 ndn->dn_storage_type = odn->dn_storage_type;
965 dmu_zfetch_init(&ndn->dn_zfetch, ndn);
968 * Update back pointers. Updating the handle fixes the back pointer of
969 * every descendant dbuf as well as the bonus dbuf.
971 ASSERT(ndn->dn_handle->dnh_dnode == odn);
972 ndn->dn_handle->dnh_dnode = ndn;
975 * Invalidate the original dnode by clearing all of its back pointers.
977 odn->dn_dbuf = NULL;
978 odn->dn_handle = NULL;
979 avl_create(&odn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t),
980 offsetof(dmu_buf_impl_t, db_link));
981 odn->dn_dbufs_count = 0;
982 odn->dn_bonus = NULL;
983 dmu_zfetch_fini(&odn->dn_zfetch);
986 * Set the low bit of the objset pointer to ensure that dnode_move()
987 * recognizes the dnode as invalid in any subsequent callback.
989 POINTER_INVALIDATE(&odn->dn_objset);
992 * Satisfy the destructor.
994 for (int i = 0; i < TXG_SIZE; i++) {
995 list_create(&odn->dn_dirty_records[i],
996 sizeof (dbuf_dirty_record_t),
997 offsetof(dbuf_dirty_record_t, dr_dirty_node));
998 odn->dn_free_ranges[i] = NULL;
999 odn->dn_next_nlevels[i] = 0;
1000 odn->dn_next_indblkshift[i] = 0;
1001 odn->dn_next_bonustype[i] = 0;
1002 odn->dn_rm_spillblk[i] = 0;
1003 odn->dn_next_bonuslen[i] = 0;
1004 odn->dn_next_blksz[i] = 0;
1006 odn->dn_allocated_txg = 0;
1007 odn->dn_free_txg = 0;
1008 odn->dn_assigned_txg = 0;
1009 odn->dn_dirty_txg = 0;
1010 odn->dn_dirtyctx = 0;
1011 odn->dn_dirtyctx_firstset = NULL;
1012 odn->dn_have_spill = B_FALSE;
1013 odn->dn_zio = NULL;
1014 odn->dn_oldused = 0;
1015 odn->dn_oldflags = 0;
1016 odn->dn_olduid = 0;
1017 odn->dn_oldgid = 0;
1018 odn->dn_oldprojid = ZFS_DEFAULT_PROJID;
1019 odn->dn_newuid = 0;
1020 odn->dn_newgid = 0;
1021 odn->dn_newprojid = ZFS_DEFAULT_PROJID;
1022 odn->dn_id_flags = 0;
1023 odn->dn_storage_type = DMU_OT_NONE;
1026 * Mark the dnode.
1028 ndn->dn_moved = 1;
1029 odn->dn_moved = (uint8_t)-1;
1032 static kmem_cbrc_t
1033 dnode_move(void *buf, void *newbuf, size_t size, void *arg)
1035 dnode_t *odn = buf, *ndn = newbuf;
1036 objset_t *os;
1037 int64_t refcount;
1038 uint32_t dbufs;
1040 #ifndef USE_DNODE_HANDLE
1042 * We can't move dnodes if dbufs reference them directly without
1043 * using handles and respecitve locking. Unless USE_DNODE_HANDLE
1044 * is defined the code below is only to make sure it still builds,
1045 * but it should never be used, since it is unsafe.
1047 #ifdef ZFS_DEBUG
1048 PANIC("dnode_move() called without USE_DNODE_HANDLE");
1049 #endif
1050 return (KMEM_CBRC_NO);
1051 #endif
1054 * The dnode is on the objset's list of known dnodes if the objset
1055 * pointer is valid. We set the low bit of the objset pointer when
1056 * freeing the dnode to invalidate it, and the memory patterns written
1057 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
1058 * A newly created dnode sets the objset pointer last of all to indicate
1059 * that the dnode is known and in a valid state to be moved by this
1060 * function.
1062 os = odn->dn_objset;
1063 if (!POINTER_IS_VALID(os)) {
1064 DNODE_STAT_BUMP(dnode_move_invalid);
1065 return (KMEM_CBRC_DONT_KNOW);
1069 * Ensure that the objset does not go away during the move.
1071 rw_enter(&os_lock, RW_WRITER);
1072 if (os != odn->dn_objset) {
1073 rw_exit(&os_lock);
1074 DNODE_STAT_BUMP(dnode_move_recheck1);
1075 return (KMEM_CBRC_DONT_KNOW);
1079 * If the dnode is still valid, then so is the objset. We know that no
1080 * valid objset can be freed while we hold os_lock, so we can safely
1081 * ensure that the objset remains in use.
1083 mutex_enter(&os->os_lock);
1086 * Recheck the objset pointer in case the dnode was removed just before
1087 * acquiring the lock.
1089 if (os != odn->dn_objset) {
1090 mutex_exit(&os->os_lock);
1091 rw_exit(&os_lock);
1092 DNODE_STAT_BUMP(dnode_move_recheck2);
1093 return (KMEM_CBRC_DONT_KNOW);
1097 * At this point we know that as long as we hold os->os_lock, the dnode
1098 * cannot be freed and fields within the dnode can be safely accessed.
1099 * The objset listing this dnode cannot go away as long as this dnode is
1100 * on its list.
1102 rw_exit(&os_lock);
1103 if (DMU_OBJECT_IS_SPECIAL(odn->dn_object)) {
1104 mutex_exit(&os->os_lock);
1105 DNODE_STAT_BUMP(dnode_move_special);
1106 return (KMEM_CBRC_NO);
1108 ASSERT(odn->dn_dbuf != NULL); /* only "special" dnodes have no parent */
1111 * Lock the dnode handle to prevent the dnode from obtaining any new
1112 * holds. This also prevents the descendant dbufs and the bonus dbuf
1113 * from accessing the dnode, so that we can discount their holds. The
1114 * handle is safe to access because we know that while the dnode cannot
1115 * go away, neither can its handle. Once we hold dnh_zrlock, we can
1116 * safely move any dnode referenced only by dbufs.
1118 if (!zrl_tryenter(&odn->dn_handle->dnh_zrlock)) {
1119 mutex_exit(&os->os_lock);
1120 DNODE_STAT_BUMP(dnode_move_handle);
1121 return (KMEM_CBRC_LATER);
1125 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
1126 * We need to guarantee that there is a hold for every dbuf in order to
1127 * determine whether the dnode is actively referenced. Falsely matching
1128 * a dbuf to an active hold would lead to an unsafe move. It's possible
1129 * that a thread already having an active dnode hold is about to add a
1130 * dbuf, and we can't compare hold and dbuf counts while the add is in
1131 * progress.
1133 if (!rw_tryenter(&odn->dn_struct_rwlock, RW_WRITER)) {
1134 zrl_exit(&odn->dn_handle->dnh_zrlock);
1135 mutex_exit(&os->os_lock);
1136 DNODE_STAT_BUMP(dnode_move_rwlock);
1137 return (KMEM_CBRC_LATER);
1141 * A dbuf may be removed (evicted) without an active dnode hold. In that
1142 * case, the dbuf count is decremented under the handle lock before the
1143 * dbuf's hold is released. This order ensures that if we count the hold
1144 * after the dbuf is removed but before its hold is released, we will
1145 * treat the unmatched hold as active and exit safely. If we count the
1146 * hold before the dbuf is removed, the hold is discounted, and the
1147 * removal is blocked until the move completes.
1149 refcount = zfs_refcount_count(&odn->dn_holds);
1150 ASSERT(refcount >= 0);
1151 dbufs = DN_DBUFS_COUNT(odn);
1153 /* We can't have more dbufs than dnode holds. */
1154 ASSERT3U(dbufs, <=, refcount);
1155 DTRACE_PROBE3(dnode__move, dnode_t *, odn, int64_t, refcount,
1156 uint32_t, dbufs);
1158 if (refcount > dbufs) {
1159 rw_exit(&odn->dn_struct_rwlock);
1160 zrl_exit(&odn->dn_handle->dnh_zrlock);
1161 mutex_exit(&os->os_lock);
1162 DNODE_STAT_BUMP(dnode_move_active);
1163 return (KMEM_CBRC_LATER);
1166 rw_exit(&odn->dn_struct_rwlock);
1169 * At this point we know that anyone with a hold on the dnode is not
1170 * actively referencing it. The dnode is known and in a valid state to
1171 * move. We're holding the locks needed to execute the critical section.
1173 dnode_move_impl(odn, ndn);
1175 list_link_replace(&odn->dn_link, &ndn->dn_link);
1176 /* If the dnode was safe to move, the refcount cannot have changed. */
1177 ASSERT(refcount == zfs_refcount_count(&ndn->dn_holds));
1178 ASSERT(dbufs == DN_DBUFS_COUNT(ndn));
1179 zrl_exit(&ndn->dn_handle->dnh_zrlock); /* handle has moved */
1180 mutex_exit(&os->os_lock);
1182 return (KMEM_CBRC_YES);
1184 #endif /* _KERNEL */
1186 static void
1187 dnode_slots_hold(dnode_children_t *children, int idx, int slots)
1189 ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK);
1191 for (int i = idx; i < idx + slots; i++) {
1192 dnode_handle_t *dnh = &children->dnc_children[i];
1193 zrl_add(&dnh->dnh_zrlock);
1197 static void
1198 dnode_slots_rele(dnode_children_t *children, int idx, int slots)
1200 ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK);
1202 for (int i = idx; i < idx + slots; i++) {
1203 dnode_handle_t *dnh = &children->dnc_children[i];
1205 if (zrl_is_locked(&dnh->dnh_zrlock))
1206 zrl_exit(&dnh->dnh_zrlock);
1207 else
1208 zrl_remove(&dnh->dnh_zrlock);
1212 static int
1213 dnode_slots_tryenter(dnode_children_t *children, int idx, int slots)
1215 ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK);
1217 for (int i = idx; i < idx + slots; i++) {
1218 dnode_handle_t *dnh = &children->dnc_children[i];
1220 if (!zrl_tryenter(&dnh->dnh_zrlock)) {
1221 for (int j = idx; j < i; j++) {
1222 dnh = &children->dnc_children[j];
1223 zrl_exit(&dnh->dnh_zrlock);
1226 return (0);
1230 return (1);
1233 static void
1234 dnode_set_slots(dnode_children_t *children, int idx, int slots, void *ptr)
1236 ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK);
1238 for (int i = idx; i < idx + slots; i++) {
1239 dnode_handle_t *dnh = &children->dnc_children[i];
1240 dnh->dnh_dnode = ptr;
1244 static boolean_t
1245 dnode_check_slots_free(dnode_children_t *children, int idx, int slots)
1247 ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK);
1250 * If all dnode slots are either already free or
1251 * evictable return B_TRUE.
1253 for (int i = idx; i < idx + slots; i++) {
1254 dnode_handle_t *dnh = &children->dnc_children[i];
1255 dnode_t *dn = dnh->dnh_dnode;
1257 if (dn == DN_SLOT_FREE) {
1258 continue;
1259 } else if (DN_SLOT_IS_PTR(dn)) {
1260 mutex_enter(&dn->dn_mtx);
1261 boolean_t can_free = (dn->dn_type == DMU_OT_NONE &&
1262 zfs_refcount_is_zero(&dn->dn_holds) &&
1263 !DNODE_IS_DIRTY(dn));
1264 mutex_exit(&dn->dn_mtx);
1266 if (!can_free)
1267 return (B_FALSE);
1268 else
1269 continue;
1270 } else {
1271 return (B_FALSE);
1275 return (B_TRUE);
1278 static uint_t
1279 dnode_reclaim_slots(dnode_children_t *children, int idx, int slots)
1281 uint_t reclaimed = 0;
1283 ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK);
1285 for (int i = idx; i < idx + slots; i++) {
1286 dnode_handle_t *dnh = &children->dnc_children[i];
1288 ASSERT(zrl_is_locked(&dnh->dnh_zrlock));
1290 if (DN_SLOT_IS_PTR(dnh->dnh_dnode)) {
1291 ASSERT3S(dnh->dnh_dnode->dn_type, ==, DMU_OT_NONE);
1292 dnode_destroy(dnh->dnh_dnode);
1293 dnh->dnh_dnode = DN_SLOT_FREE;
1294 reclaimed++;
1298 return (reclaimed);
1301 void
1302 dnode_free_interior_slots(dnode_t *dn)
1304 dnode_children_t *children = dmu_buf_get_user(&dn->dn_dbuf->db);
1305 int epb = dn->dn_dbuf->db.db_size >> DNODE_SHIFT;
1306 int idx = (dn->dn_object & (epb - 1)) + 1;
1307 int slots = dn->dn_num_slots - 1;
1309 if (slots == 0)
1310 return;
1312 ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK);
1314 while (!dnode_slots_tryenter(children, idx, slots)) {
1315 DNODE_STAT_BUMP(dnode_free_interior_lock_retry);
1316 kpreempt(KPREEMPT_SYNC);
1319 dnode_set_slots(children, idx, slots, DN_SLOT_FREE);
1320 dnode_slots_rele(children, idx, slots);
1323 void
1324 dnode_special_close(dnode_handle_t *dnh)
1326 dnode_t *dn = dnh->dnh_dnode;
1329 * Ensure dnode_rele_and_unlock() has released dn_mtx, after final
1330 * zfs_refcount_remove()
1332 mutex_enter(&dn->dn_mtx);
1333 if (zfs_refcount_count(&dn->dn_holds) > 0)
1334 cv_wait(&dn->dn_nodnholds, &dn->dn_mtx);
1335 mutex_exit(&dn->dn_mtx);
1336 ASSERT3U(zfs_refcount_count(&dn->dn_holds), ==, 0);
1338 ASSERT(dn->dn_dbuf == NULL ||
1339 dmu_buf_get_user(&dn->dn_dbuf->db) == NULL);
1340 zrl_add(&dnh->dnh_zrlock);
1341 dnode_destroy(dn); /* implicit zrl_remove() */
1342 zrl_destroy(&dnh->dnh_zrlock);
1343 dnh->dnh_dnode = NULL;
1346 void
1347 dnode_special_open(objset_t *os, dnode_phys_t *dnp, uint64_t object,
1348 dnode_handle_t *dnh)
1350 dnode_t *dn;
1352 zrl_init(&dnh->dnh_zrlock);
1353 VERIFY3U(1, ==, zrl_tryenter(&dnh->dnh_zrlock));
1355 dn = dnode_create(os, dnp, NULL, object, dnh);
1356 DNODE_VERIFY(dn);
1358 zrl_exit(&dnh->dnh_zrlock);
1361 static void
1362 dnode_buf_evict_async(void *dbu)
1364 dnode_children_t *dnc = dbu;
1366 DNODE_STAT_BUMP(dnode_buf_evict);
1368 for (int i = 0; i < dnc->dnc_count; i++) {
1369 dnode_handle_t *dnh = &dnc->dnc_children[i];
1370 dnode_t *dn;
1373 * The dnode handle lock guards against the dnode moving to
1374 * another valid address, so there is no need here to guard
1375 * against changes to or from NULL.
1377 if (!DN_SLOT_IS_PTR(dnh->dnh_dnode)) {
1378 zrl_destroy(&dnh->dnh_zrlock);
1379 dnh->dnh_dnode = DN_SLOT_UNINIT;
1380 continue;
1383 zrl_add(&dnh->dnh_zrlock);
1384 dn = dnh->dnh_dnode;
1386 * If there are holds on this dnode, then there should
1387 * be holds on the dnode's containing dbuf as well; thus
1388 * it wouldn't be eligible for eviction and this function
1389 * would not have been called.
1391 ASSERT(zfs_refcount_is_zero(&dn->dn_holds));
1392 ASSERT(zfs_refcount_is_zero(&dn->dn_tx_holds));
1394 dnode_destroy(dn); /* implicit zrl_remove() for first slot */
1395 zrl_destroy(&dnh->dnh_zrlock);
1396 dnh->dnh_dnode = DN_SLOT_UNINIT;
1398 kmem_free(dnc, sizeof (dnode_children_t) +
1399 dnc->dnc_count * sizeof (dnode_handle_t));
1403 * When the DNODE_MUST_BE_FREE flag is set, the "slots" parameter is used
1404 * to ensure the hole at the specified object offset is large enough to
1405 * hold the dnode being created. The slots parameter is also used to ensure
1406 * a dnode does not span multiple dnode blocks. In both of these cases, if
1407 * a failure occurs, ENOSPC is returned. Keep in mind, these failure cases
1408 * are only possible when using DNODE_MUST_BE_FREE.
1410 * If the DNODE_MUST_BE_ALLOCATED flag is set, "slots" must be 0.
1411 * dnode_hold_impl() will check if the requested dnode is already consumed
1412 * as an extra dnode slot by an large dnode, in which case it returns
1413 * ENOENT.
1415 * If the DNODE_DRY_RUN flag is set, we don't actually hold the dnode, just
1416 * return whether the hold would succeed or not. tag and dnp should set to
1417 * NULL in this case.
1419 * errors:
1420 * EINVAL - Invalid object number or flags.
1421 * ENOSPC - Hole too small to fulfill "slots" request (DNODE_MUST_BE_FREE)
1422 * EEXIST - Refers to an allocated dnode (DNODE_MUST_BE_FREE)
1423 * - Refers to a freeing dnode (DNODE_MUST_BE_FREE)
1424 * - Refers to an interior dnode slot (DNODE_MUST_BE_ALLOCATED)
1425 * ENOENT - The requested dnode is not allocated (DNODE_MUST_BE_ALLOCATED)
1426 * - The requested dnode is being freed (DNODE_MUST_BE_ALLOCATED)
1427 * EIO - I/O error when reading the meta dnode dbuf.
1429 * succeeds even for free dnodes.
1432 dnode_hold_impl(objset_t *os, uint64_t object, int flag, int slots,
1433 const void *tag, dnode_t **dnp)
1435 int epb, idx, err;
1436 int drop_struct_lock = FALSE;
1437 int type;
1438 uint64_t blk;
1439 dnode_t *mdn, *dn;
1440 dmu_buf_impl_t *db;
1441 dnode_children_t *dnc;
1442 dnode_phys_t *dn_block;
1443 dnode_handle_t *dnh;
1445 ASSERT(!(flag & DNODE_MUST_BE_ALLOCATED) || (slots == 0));
1446 ASSERT(!(flag & DNODE_MUST_BE_FREE) || (slots > 0));
1447 IMPLY(flag & DNODE_DRY_RUN, (tag == NULL) && (dnp == NULL));
1450 * If you are holding the spa config lock as writer, you shouldn't
1451 * be asking the DMU to do *anything* unless it's the root pool
1452 * which may require us to read from the root filesystem while
1453 * holding some (not all) of the locks as writer.
1455 ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0 ||
1456 (spa_is_root(os->os_spa) &&
1457 spa_config_held(os->os_spa, SCL_STATE, RW_WRITER)));
1459 ASSERT((flag & DNODE_MUST_BE_ALLOCATED) || (flag & DNODE_MUST_BE_FREE));
1461 if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT ||
1462 object == DMU_PROJECTUSED_OBJECT) {
1463 if (object == DMU_USERUSED_OBJECT)
1464 dn = DMU_USERUSED_DNODE(os);
1465 else if (object == DMU_GROUPUSED_OBJECT)
1466 dn = DMU_GROUPUSED_DNODE(os);
1467 else
1468 dn = DMU_PROJECTUSED_DNODE(os);
1469 if (dn == NULL)
1470 return (SET_ERROR(ENOENT));
1471 type = dn->dn_type;
1472 if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE)
1473 return (SET_ERROR(ENOENT));
1474 if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)
1475 return (SET_ERROR(EEXIST));
1476 DNODE_VERIFY(dn);
1477 /* Don't actually hold if dry run, just return 0 */
1478 if (!(flag & DNODE_DRY_RUN)) {
1479 (void) zfs_refcount_add(&dn->dn_holds, tag);
1480 *dnp = dn;
1482 return (0);
1485 if (object == 0 || object >= DN_MAX_OBJECT)
1486 return (SET_ERROR(EINVAL));
1488 mdn = DMU_META_DNODE(os);
1489 ASSERT(mdn->dn_object == DMU_META_DNODE_OBJECT);
1491 DNODE_VERIFY(mdn);
1493 if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
1494 rw_enter(&mdn->dn_struct_rwlock, RW_READER);
1495 drop_struct_lock = TRUE;
1498 blk = dbuf_whichblock(mdn, 0, object * sizeof (dnode_phys_t));
1499 db = dbuf_hold(mdn, blk, FTAG);
1500 if (drop_struct_lock)
1501 rw_exit(&mdn->dn_struct_rwlock);
1502 if (db == NULL) {
1503 DNODE_STAT_BUMP(dnode_hold_dbuf_hold);
1504 return (SET_ERROR(EIO));
1508 * We do not need to decrypt to read the dnode so it doesn't matter
1509 * if we get the encrypted or decrypted version.
1511 err = dbuf_read(db, NULL, DB_RF_CANFAIL |
1512 DB_RF_NO_DECRYPT | DB_RF_NOPREFETCH);
1513 if (err) {
1514 DNODE_STAT_BUMP(dnode_hold_dbuf_read);
1515 dbuf_rele(db, FTAG);
1516 return (err);
1519 ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
1520 epb = db->db.db_size >> DNODE_SHIFT;
1522 idx = object & (epb - 1);
1523 dn_block = (dnode_phys_t *)db->db.db_data;
1525 ASSERT(DB_DNODE(db)->dn_type == DMU_OT_DNODE);
1526 dnc = dmu_buf_get_user(&db->db);
1527 dnh = NULL;
1528 if (dnc == NULL) {
1529 dnode_children_t *winner;
1530 int skip = 0;
1532 dnc = kmem_zalloc(sizeof (dnode_children_t) +
1533 epb * sizeof (dnode_handle_t), KM_SLEEP);
1534 dnc->dnc_count = epb;
1535 dnh = &dnc->dnc_children[0];
1537 /* Initialize dnode slot status from dnode_phys_t */
1538 for (int i = 0; i < epb; i++) {
1539 zrl_init(&dnh[i].dnh_zrlock);
1541 if (skip) {
1542 skip--;
1543 continue;
1546 if (dn_block[i].dn_type != DMU_OT_NONE) {
1547 int interior = dn_block[i].dn_extra_slots;
1549 dnode_set_slots(dnc, i, 1, DN_SLOT_ALLOCATED);
1550 dnode_set_slots(dnc, i + 1, interior,
1551 DN_SLOT_INTERIOR);
1552 skip = interior;
1553 } else {
1554 dnh[i].dnh_dnode = DN_SLOT_FREE;
1555 skip = 0;
1559 dmu_buf_init_user(&dnc->dnc_dbu, NULL,
1560 dnode_buf_evict_async, NULL);
1561 winner = dmu_buf_set_user(&db->db, &dnc->dnc_dbu);
1562 if (winner != NULL) {
1564 for (int i = 0; i < epb; i++)
1565 zrl_destroy(&dnh[i].dnh_zrlock);
1567 kmem_free(dnc, sizeof (dnode_children_t) +
1568 epb * sizeof (dnode_handle_t));
1569 dnc = winner;
1573 ASSERT(dnc->dnc_count == epb);
1575 if (flag & DNODE_MUST_BE_ALLOCATED) {
1576 slots = 1;
1578 dnode_slots_hold(dnc, idx, slots);
1579 dnh = &dnc->dnc_children[idx];
1581 if (DN_SLOT_IS_PTR(dnh->dnh_dnode)) {
1582 dn = dnh->dnh_dnode;
1583 } else if (dnh->dnh_dnode == DN_SLOT_INTERIOR) {
1584 DNODE_STAT_BUMP(dnode_hold_alloc_interior);
1585 dnode_slots_rele(dnc, idx, slots);
1586 dbuf_rele(db, FTAG);
1587 return (SET_ERROR(EEXIST));
1588 } else if (dnh->dnh_dnode != DN_SLOT_ALLOCATED) {
1589 DNODE_STAT_BUMP(dnode_hold_alloc_misses);
1590 dnode_slots_rele(dnc, idx, slots);
1591 dbuf_rele(db, FTAG);
1592 return (SET_ERROR(ENOENT));
1593 } else {
1594 dnode_slots_rele(dnc, idx, slots);
1595 while (!dnode_slots_tryenter(dnc, idx, slots)) {
1596 DNODE_STAT_BUMP(dnode_hold_alloc_lock_retry);
1597 kpreempt(KPREEMPT_SYNC);
1601 * Someone else won the race and called dnode_create()
1602 * after we checked DN_SLOT_IS_PTR() above but before
1603 * we acquired the lock.
1605 if (DN_SLOT_IS_PTR(dnh->dnh_dnode)) {
1606 DNODE_STAT_BUMP(dnode_hold_alloc_lock_misses);
1607 dn = dnh->dnh_dnode;
1608 } else {
1609 dn = dnode_create(os, dn_block + idx, db,
1610 object, dnh);
1611 dmu_buf_add_user_size(&db->db,
1612 sizeof (dnode_t));
1616 mutex_enter(&dn->dn_mtx);
1617 if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg != 0) {
1618 DNODE_STAT_BUMP(dnode_hold_alloc_type_none);
1619 mutex_exit(&dn->dn_mtx);
1620 dnode_slots_rele(dnc, idx, slots);
1621 dbuf_rele(db, FTAG);
1622 return (SET_ERROR(ENOENT));
1625 /* Don't actually hold if dry run, just return 0 */
1626 if (flag & DNODE_DRY_RUN) {
1627 mutex_exit(&dn->dn_mtx);
1628 dnode_slots_rele(dnc, idx, slots);
1629 dbuf_rele(db, FTAG);
1630 return (0);
1633 DNODE_STAT_BUMP(dnode_hold_alloc_hits);
1634 } else if (flag & DNODE_MUST_BE_FREE) {
1636 if (idx + slots - 1 >= DNODES_PER_BLOCK) {
1637 DNODE_STAT_BUMP(dnode_hold_free_overflow);
1638 dbuf_rele(db, FTAG);
1639 return (SET_ERROR(ENOSPC));
1642 dnode_slots_hold(dnc, idx, slots);
1644 if (!dnode_check_slots_free(dnc, idx, slots)) {
1645 DNODE_STAT_BUMP(dnode_hold_free_misses);
1646 dnode_slots_rele(dnc, idx, slots);
1647 dbuf_rele(db, FTAG);
1648 return (SET_ERROR(ENOSPC));
1651 dnode_slots_rele(dnc, idx, slots);
1652 while (!dnode_slots_tryenter(dnc, idx, slots)) {
1653 DNODE_STAT_BUMP(dnode_hold_free_lock_retry);
1654 kpreempt(KPREEMPT_SYNC);
1657 if (!dnode_check_slots_free(dnc, idx, slots)) {
1658 DNODE_STAT_BUMP(dnode_hold_free_lock_misses);
1659 dnode_slots_rele(dnc, idx, slots);
1660 dbuf_rele(db, FTAG);
1661 return (SET_ERROR(ENOSPC));
1665 * Allocated but otherwise free dnodes which would
1666 * be in the interior of a multi-slot dnodes need
1667 * to be freed. Single slot dnodes can be safely
1668 * re-purposed as a performance optimization.
1670 if (slots > 1) {
1671 uint_t reclaimed =
1672 dnode_reclaim_slots(dnc, idx + 1, slots - 1);
1673 if (reclaimed > 0)
1674 dmu_buf_sub_user_size(&db->db,
1675 reclaimed * sizeof (dnode_t));
1678 dnh = &dnc->dnc_children[idx];
1679 if (DN_SLOT_IS_PTR(dnh->dnh_dnode)) {
1680 dn = dnh->dnh_dnode;
1681 } else {
1682 dn = dnode_create(os, dn_block + idx, db,
1683 object, dnh);
1684 dmu_buf_add_user_size(&db->db, sizeof (dnode_t));
1687 mutex_enter(&dn->dn_mtx);
1688 if (!zfs_refcount_is_zero(&dn->dn_holds) || dn->dn_free_txg) {
1689 DNODE_STAT_BUMP(dnode_hold_free_refcount);
1690 mutex_exit(&dn->dn_mtx);
1691 dnode_slots_rele(dnc, idx, slots);
1692 dbuf_rele(db, FTAG);
1693 return (SET_ERROR(EEXIST));
1696 /* Don't actually hold if dry run, just return 0 */
1697 if (flag & DNODE_DRY_RUN) {
1698 mutex_exit(&dn->dn_mtx);
1699 dnode_slots_rele(dnc, idx, slots);
1700 dbuf_rele(db, FTAG);
1701 return (0);
1704 dnode_set_slots(dnc, idx + 1, slots - 1, DN_SLOT_INTERIOR);
1705 DNODE_STAT_BUMP(dnode_hold_free_hits);
1706 } else {
1707 dbuf_rele(db, FTAG);
1708 return (SET_ERROR(EINVAL));
1711 ASSERT0(dn->dn_free_txg);
1713 if (zfs_refcount_add(&dn->dn_holds, tag) == 1)
1714 dbuf_add_ref(db, dnh);
1716 mutex_exit(&dn->dn_mtx);
1718 /* Now we can rely on the hold to prevent the dnode from moving. */
1719 dnode_slots_rele(dnc, idx, slots);
1721 DNODE_VERIFY(dn);
1722 ASSERT3P(dnp, !=, NULL);
1723 ASSERT3P(dn->dn_dbuf, ==, db);
1724 ASSERT3U(dn->dn_object, ==, object);
1725 dbuf_rele(db, FTAG);
1727 *dnp = dn;
1728 return (0);
1732 * Return held dnode if the object is allocated, NULL if not.
1735 dnode_hold(objset_t *os, uint64_t object, const void *tag, dnode_t **dnp)
1737 return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, 0, tag,
1738 dnp));
1742 * Can only add a reference if there is already at least one
1743 * reference on the dnode. Returns FALSE if unable to add a
1744 * new reference.
1746 boolean_t
1747 dnode_add_ref(dnode_t *dn, const void *tag)
1749 mutex_enter(&dn->dn_mtx);
1750 if (zfs_refcount_is_zero(&dn->dn_holds)) {
1751 mutex_exit(&dn->dn_mtx);
1752 return (FALSE);
1754 VERIFY(1 < zfs_refcount_add(&dn->dn_holds, tag));
1755 mutex_exit(&dn->dn_mtx);
1756 return (TRUE);
1759 void
1760 dnode_rele(dnode_t *dn, const void *tag)
1762 mutex_enter(&dn->dn_mtx);
1763 dnode_rele_and_unlock(dn, tag, B_FALSE);
1766 void
1767 dnode_rele_and_unlock(dnode_t *dn, const void *tag, boolean_t evicting)
1769 uint64_t refs;
1770 /* Get while the hold prevents the dnode from moving. */
1771 dmu_buf_impl_t *db = dn->dn_dbuf;
1772 dnode_handle_t *dnh = dn->dn_handle;
1774 refs = zfs_refcount_remove(&dn->dn_holds, tag);
1775 if (refs == 0)
1776 cv_broadcast(&dn->dn_nodnholds);
1777 mutex_exit(&dn->dn_mtx);
1778 /* dnode could get destroyed at this point, so don't use it anymore */
1781 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1782 * indirectly by dbuf_rele() while relying on the dnode handle to
1783 * prevent the dnode from moving, since releasing the last hold could
1784 * result in the dnode's parent dbuf evicting its dnode handles. For
1785 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1786 * other direct or indirect hold on the dnode must first drop the dnode
1787 * handle.
1789 #ifdef ZFS_DEBUG
1790 ASSERT(refs > 0 || zrl_owner(&dnh->dnh_zrlock) != curthread);
1791 #endif
1793 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1794 if (refs == 0 && db != NULL) {
1796 * Another thread could add a hold to the dnode handle in
1797 * dnode_hold_impl() while holding the parent dbuf. Since the
1798 * hold on the parent dbuf prevents the handle from being
1799 * destroyed, the hold on the handle is OK. We can't yet assert
1800 * that the handle has zero references, but that will be
1801 * asserted anyway when the handle gets destroyed.
1803 mutex_enter(&db->db_mtx);
1804 dbuf_rele_and_unlock(db, dnh, evicting);
1809 * Test whether we can create a dnode at the specified location.
1812 dnode_try_claim(objset_t *os, uint64_t object, int slots)
1814 return (dnode_hold_impl(os, object, DNODE_MUST_BE_FREE | DNODE_DRY_RUN,
1815 slots, NULL, NULL));
1819 * Checks if the dnode itself is dirty, or is carrying any uncommitted records.
1820 * It is important to check both conditions, as some operations (eg appending
1821 * to a file) can dirty both as a single logical unit, but they are not synced
1822 * out atomically, so checking one and not the other can result in an object
1823 * appearing to be clean mid-way through a commit.
1825 * Do not change this lightly! If you get it wrong, dmu_offset_next() can
1826 * detect a hole where there is really data, leading to silent corruption.
1828 boolean_t
1829 dnode_is_dirty(dnode_t *dn)
1831 mutex_enter(&dn->dn_mtx);
1833 for (int i = 0; i < TXG_SIZE; i++) {
1834 if (multilist_link_active(&dn->dn_dirty_link[i]) ||
1835 !list_is_empty(&dn->dn_dirty_records[i])) {
1836 mutex_exit(&dn->dn_mtx);
1837 return (B_TRUE);
1841 mutex_exit(&dn->dn_mtx);
1843 return (B_FALSE);
1846 void
1847 dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
1849 objset_t *os = dn->dn_objset;
1850 uint64_t txg = tx->tx_txg;
1852 if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
1853 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1854 return;
1857 DNODE_VERIFY(dn);
1859 #ifdef ZFS_DEBUG
1860 mutex_enter(&dn->dn_mtx);
1861 ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
1862 ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg);
1863 mutex_exit(&dn->dn_mtx);
1864 #endif
1867 * Determine old uid/gid when necessary
1869 dmu_objset_userquota_get_ids(dn, B_TRUE, tx);
1871 multilist_t *dirtylist = &os->os_dirty_dnodes[txg & TXG_MASK];
1872 multilist_sublist_t *mls = multilist_sublist_lock_obj(dirtylist, dn);
1875 * If we are already marked dirty, we're done.
1877 if (multilist_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
1878 multilist_sublist_unlock(mls);
1879 return;
1882 ASSERT(!zfs_refcount_is_zero(&dn->dn_holds) ||
1883 !avl_is_empty(&dn->dn_dbufs));
1884 ASSERT(dn->dn_datablksz != 0);
1885 ASSERT0(dn->dn_next_bonuslen[txg & TXG_MASK]);
1886 ASSERT0(dn->dn_next_blksz[txg & TXG_MASK]);
1887 ASSERT0(dn->dn_next_bonustype[txg & TXG_MASK]);
1889 dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
1890 (u_longlong_t)dn->dn_object, (u_longlong_t)txg);
1892 multilist_sublist_insert_head(mls, dn);
1894 multilist_sublist_unlock(mls);
1897 * The dnode maintains a hold on its containing dbuf as
1898 * long as there are holds on it. Each instantiated child
1899 * dbuf maintains a hold on the dnode. When the last child
1900 * drops its hold, the dnode will drop its hold on the
1901 * containing dbuf. We add a "dirty hold" here so that the
1902 * dnode will hang around after we finish processing its
1903 * children.
1905 VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
1907 (void) dbuf_dirty(dn->dn_dbuf, tx);
1909 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1912 void
1913 dnode_free(dnode_t *dn, dmu_tx_t *tx)
1915 mutex_enter(&dn->dn_mtx);
1916 if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
1917 mutex_exit(&dn->dn_mtx);
1918 return;
1920 dn->dn_free_txg = tx->tx_txg;
1921 mutex_exit(&dn->dn_mtx);
1923 dnode_setdirty(dn, tx);
1927 * Try to change the block size for the indicated dnode. This can only
1928 * succeed if there are no blocks allocated or dirty beyond first block
1931 dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
1933 dmu_buf_impl_t *db;
1934 int err;
1936 ASSERT3U(size, <=, spa_maxblocksize(dmu_objset_spa(dn->dn_objset)));
1937 if (size == 0)
1938 size = SPA_MINBLOCKSIZE;
1939 else
1940 size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
1942 if (ibs == dn->dn_indblkshift)
1943 ibs = 0;
1945 if (size == dn->dn_datablksz && ibs == 0)
1946 return (0);
1948 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1950 /* Check for any allocated blocks beyond the first */
1951 if (dn->dn_maxblkid != 0)
1952 goto fail;
1954 mutex_enter(&dn->dn_dbufs_mtx);
1955 for (db = avl_first(&dn->dn_dbufs); db != NULL;
1956 db = AVL_NEXT(&dn->dn_dbufs, db)) {
1957 if (db->db_blkid != 0 && db->db_blkid != DMU_BONUS_BLKID &&
1958 db->db_blkid != DMU_SPILL_BLKID) {
1959 mutex_exit(&dn->dn_dbufs_mtx);
1960 goto fail;
1963 mutex_exit(&dn->dn_dbufs_mtx);
1965 if (ibs && dn->dn_nlevels != 1)
1966 goto fail;
1968 dnode_setdirty(dn, tx);
1969 if (size != dn->dn_datablksz) {
1970 /* resize the old block */
1971 err = dbuf_hold_impl(dn, 0, 0, TRUE, FALSE, FTAG, &db);
1972 if (err == 0) {
1973 dbuf_new_size(db, size, tx);
1974 } else if (err != ENOENT) {
1975 goto fail;
1978 dnode_setdblksz(dn, size);
1979 dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = size;
1980 if (db)
1981 dbuf_rele(db, FTAG);
1983 if (ibs) {
1984 dn->dn_indblkshift = ibs;
1985 dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
1988 rw_exit(&dn->dn_struct_rwlock);
1989 return (0);
1991 fail:
1992 rw_exit(&dn->dn_struct_rwlock);
1993 return (SET_ERROR(ENOTSUP));
1996 static void
1997 dnode_set_nlevels_impl(dnode_t *dn, int new_nlevels, dmu_tx_t *tx)
1999 uint64_t txgoff = tx->tx_txg & TXG_MASK;
2000 int old_nlevels = dn->dn_nlevels;
2001 dmu_buf_impl_t *db;
2002 list_t *list;
2003 dbuf_dirty_record_t *new, *dr, *dr_next;
2005 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
2007 ASSERT3U(new_nlevels, >, dn->dn_nlevels);
2008 dn->dn_nlevels = new_nlevels;
2010 ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
2011 dn->dn_next_nlevels[txgoff] = new_nlevels;
2013 /* dirty the left indirects */
2014 db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
2015 ASSERT(db != NULL);
2016 new = dbuf_dirty(db, tx);
2017 dbuf_rele(db, FTAG);
2019 /* transfer the dirty records to the new indirect */
2020 mutex_enter(&dn->dn_mtx);
2021 mutex_enter(&new->dt.di.dr_mtx);
2022 list = &dn->dn_dirty_records[txgoff];
2023 for (dr = list_head(list); dr; dr = dr_next) {
2024 dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
2026 IMPLY(dr->dr_dbuf == NULL, old_nlevels == 1);
2027 if (dr->dr_dbuf == NULL ||
2028 (dr->dr_dbuf->db_level == old_nlevels - 1 &&
2029 dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
2030 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID)) {
2031 list_remove(&dn->dn_dirty_records[txgoff], dr);
2032 list_insert_tail(&new->dt.di.dr_children, dr);
2033 dr->dr_parent = new;
2036 mutex_exit(&new->dt.di.dr_mtx);
2037 mutex_exit(&dn->dn_mtx);
2041 dnode_set_nlevels(dnode_t *dn, int nlevels, dmu_tx_t *tx)
2043 int ret = 0;
2045 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2047 if (dn->dn_nlevels == nlevels) {
2048 ret = 0;
2049 goto out;
2050 } else if (nlevels < dn->dn_nlevels) {
2051 ret = SET_ERROR(EINVAL);
2052 goto out;
2055 dnode_set_nlevels_impl(dn, nlevels, tx);
2057 out:
2058 rw_exit(&dn->dn_struct_rwlock);
2059 return (ret);
2062 /* read-holding callers must not rely on the lock being continuously held */
2063 void
2064 dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read,
2065 boolean_t force)
2067 int epbs, new_nlevels;
2068 uint64_t sz;
2070 ASSERT(blkid != DMU_BONUS_BLKID);
2072 ASSERT(have_read ?
2073 RW_READ_HELD(&dn->dn_struct_rwlock) :
2074 RW_WRITE_HELD(&dn->dn_struct_rwlock));
2077 * if we have a read-lock, check to see if we need to do any work
2078 * before upgrading to a write-lock.
2080 if (have_read) {
2081 if (blkid <= dn->dn_maxblkid)
2082 return;
2084 if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
2085 rw_exit(&dn->dn_struct_rwlock);
2086 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2091 * Raw sends (indicated by the force flag) require that we take the
2092 * given blkid even if the value is lower than the current value.
2094 if (!force && blkid <= dn->dn_maxblkid)
2095 goto out;
2098 * We use the (otherwise unused) top bit of dn_next_maxblkid[txgoff]
2099 * to indicate that this field is set. This allows us to set the
2100 * maxblkid to 0 on an existing object in dnode_sync().
2102 dn->dn_maxblkid = blkid;
2103 dn->dn_next_maxblkid[tx->tx_txg & TXG_MASK] =
2104 blkid | DMU_NEXT_MAXBLKID_SET;
2107 * Compute the number of levels necessary to support the new maxblkid.
2108 * Raw sends will ensure nlevels is set correctly for us.
2110 new_nlevels = 1;
2111 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
2112 for (sz = dn->dn_nblkptr;
2113 sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
2114 new_nlevels++;
2116 ASSERT3U(new_nlevels, <=, DN_MAX_LEVELS);
2118 if (!force) {
2119 if (new_nlevels > dn->dn_nlevels)
2120 dnode_set_nlevels_impl(dn, new_nlevels, tx);
2121 } else {
2122 ASSERT3U(dn->dn_nlevels, >=, new_nlevels);
2125 out:
2126 if (have_read)
2127 rw_downgrade(&dn->dn_struct_rwlock);
2130 static void
2131 dnode_dirty_l1(dnode_t *dn, uint64_t l1blkid, dmu_tx_t *tx)
2133 dmu_buf_impl_t *db = dbuf_hold_level(dn, 1, l1blkid, FTAG);
2134 if (db != NULL) {
2135 dmu_buf_will_dirty(&db->db, tx);
2136 dbuf_rele(db, FTAG);
2141 * Dirty all the in-core level-1 dbufs in the range specified by start_blkid
2142 * and end_blkid.
2144 static void
2145 dnode_dirty_l1range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid,
2146 dmu_tx_t *tx)
2148 dmu_buf_impl_t *db_search;
2149 dmu_buf_impl_t *db;
2150 avl_index_t where;
2152 db_search = kmem_zalloc(sizeof (dmu_buf_impl_t), KM_SLEEP);
2154 mutex_enter(&dn->dn_dbufs_mtx);
2156 db_search->db_level = 1;
2157 db_search->db_blkid = start_blkid + 1;
2158 db_search->db_state = DB_SEARCH;
2159 for (;;) {
2161 db = avl_find(&dn->dn_dbufs, db_search, &where);
2162 if (db == NULL)
2163 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
2165 if (db == NULL || db->db_level != 1 ||
2166 db->db_blkid >= end_blkid) {
2167 break;
2171 * Setup the next blkid we want to search for.
2173 db_search->db_blkid = db->db_blkid + 1;
2174 ASSERT3U(db->db_blkid, >=, start_blkid);
2177 * If the dbuf transitions to DB_EVICTING while we're trying
2178 * to dirty it, then we will be unable to discover it in
2179 * the dbuf hash table. This will result in a call to
2180 * dbuf_create() which needs to acquire the dn_dbufs_mtx
2181 * lock. To avoid a deadlock, we drop the lock before
2182 * dirtying the level-1 dbuf.
2184 mutex_exit(&dn->dn_dbufs_mtx);
2185 dnode_dirty_l1(dn, db->db_blkid, tx);
2186 mutex_enter(&dn->dn_dbufs_mtx);
2189 #ifdef ZFS_DEBUG
2191 * Walk all the in-core level-1 dbufs and verify they have been dirtied.
2193 db_search->db_level = 1;
2194 db_search->db_blkid = start_blkid + 1;
2195 db_search->db_state = DB_SEARCH;
2196 db = avl_find(&dn->dn_dbufs, db_search, &where);
2197 if (db == NULL)
2198 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
2199 for (; db != NULL; db = AVL_NEXT(&dn->dn_dbufs, db)) {
2200 if (db->db_level != 1 || db->db_blkid >= end_blkid)
2201 break;
2202 if (db->db_state != DB_EVICTING)
2203 ASSERT(db->db_dirtycnt > 0);
2205 #endif
2206 kmem_free(db_search, sizeof (dmu_buf_impl_t));
2207 mutex_exit(&dn->dn_dbufs_mtx);
2210 void
2211 dnode_set_dirtyctx(dnode_t *dn, dmu_tx_t *tx, const void *tag)
2214 * Don't set dirtyctx to SYNC if we're just modifying this as we
2215 * initialize the objset.
2217 if (dn->dn_dirtyctx == DN_UNDIRTIED) {
2218 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
2220 if (ds != NULL) {
2221 rrw_enter(&ds->ds_bp_rwlock, RW_READER, tag);
2223 if (!BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
2224 if (dmu_tx_is_syncing(tx))
2225 dn->dn_dirtyctx = DN_DIRTY_SYNC;
2226 else
2227 dn->dn_dirtyctx = DN_DIRTY_OPEN;
2228 dn->dn_dirtyctx_firstset = tag;
2230 if (ds != NULL) {
2231 rrw_exit(&ds->ds_bp_rwlock, tag);
2236 static void
2237 dnode_partial_zero(dnode_t *dn, uint64_t off, uint64_t blkoff, uint64_t len,
2238 dmu_tx_t *tx)
2240 dmu_buf_impl_t *db;
2241 int res;
2243 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2244 res = dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, 0, off), TRUE, FALSE,
2245 FTAG, &db);
2246 rw_exit(&dn->dn_struct_rwlock);
2247 if (res == 0) {
2248 db_lock_type_t dblt;
2249 boolean_t dirty;
2251 dblt = dmu_buf_lock_parent(db, RW_READER, FTAG);
2252 /* don't dirty if not on disk and not dirty */
2253 dirty = !list_is_empty(&db->db_dirty_records) ||
2254 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr));
2255 dmu_buf_unlock_parent(db, dblt, FTAG);
2256 if (dirty) {
2257 caddr_t data;
2259 dmu_buf_will_dirty(&db->db, tx);
2260 data = db->db.db_data;
2261 memset(data + blkoff, 0, len);
2263 dbuf_rele(db, FTAG);
2267 void
2268 dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
2270 uint64_t blkoff, blkid, nblks;
2271 int blksz, blkshift, head, tail;
2272 int trunc = FALSE;
2273 int epbs;
2275 blksz = dn->dn_datablksz;
2276 blkshift = dn->dn_datablkshift;
2277 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
2279 if (len == DMU_OBJECT_END) {
2280 len = UINT64_MAX - off;
2281 trunc = TRUE;
2285 * First, block align the region to free:
2287 if (ISP2(blksz)) {
2288 head = P2NPHASE(off, blksz);
2289 blkoff = P2PHASE(off, blksz);
2290 if ((off >> blkshift) > dn->dn_maxblkid)
2291 return;
2292 } else {
2293 ASSERT(dn->dn_maxblkid == 0);
2294 if (off == 0 && len >= blksz) {
2296 * Freeing the whole block; fast-track this request.
2298 blkid = 0;
2299 nblks = 1;
2300 if (dn->dn_nlevels > 1) {
2301 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2302 dnode_dirty_l1(dn, 0, tx);
2303 rw_exit(&dn->dn_struct_rwlock);
2305 goto done;
2306 } else if (off >= blksz) {
2307 /* Freeing past end-of-data */
2308 return;
2309 } else {
2310 /* Freeing part of the block. */
2311 head = blksz - off;
2312 ASSERT3U(head, >, 0);
2314 blkoff = off;
2316 /* zero out any partial block data at the start of the range */
2317 if (head) {
2318 ASSERT3U(blkoff + head, ==, blksz);
2319 if (len < head)
2320 head = len;
2321 dnode_partial_zero(dn, off, blkoff, head, tx);
2322 off += head;
2323 len -= head;
2326 /* If the range was less than one block, we're done */
2327 if (len == 0)
2328 return;
2330 /* If the remaining range is past end of file, we're done */
2331 if ((off >> blkshift) > dn->dn_maxblkid)
2332 return;
2334 ASSERT(ISP2(blksz));
2335 if (trunc)
2336 tail = 0;
2337 else
2338 tail = P2PHASE(len, blksz);
2340 ASSERT0(P2PHASE(off, blksz));
2341 /* zero out any partial block data at the end of the range */
2342 if (tail) {
2343 if (len < tail)
2344 tail = len;
2345 dnode_partial_zero(dn, off + len, 0, tail, tx);
2346 len -= tail;
2349 /* If the range did not include a full block, we are done */
2350 if (len == 0)
2351 return;
2353 ASSERT(IS_P2ALIGNED(off, blksz));
2354 ASSERT(trunc || IS_P2ALIGNED(len, blksz));
2355 blkid = off >> blkshift;
2356 nblks = len >> blkshift;
2357 if (trunc)
2358 nblks += 1;
2361 * Dirty all the indirect blocks in this range. Note that only
2362 * the first and last indirect blocks can actually be written
2363 * (if they were partially freed) -- they must be dirtied, even if
2364 * they do not exist on disk yet. The interior blocks will
2365 * be freed by free_children(), so they will not actually be written.
2366 * Even though these interior blocks will not be written, we
2367 * dirty them for two reasons:
2369 * - It ensures that the indirect blocks remain in memory until
2370 * syncing context. (They have already been prefetched by
2371 * dmu_tx_hold_free(), so we don't have to worry about reading
2372 * them serially here.)
2374 * - The dirty space accounting will put pressure on the txg sync
2375 * mechanism to begin syncing, and to delay transactions if there
2376 * is a large amount of freeing. Even though these indirect
2377 * blocks will not be written, we could need to write the same
2378 * amount of space if we copy the freed BPs into deadlists.
2380 if (dn->dn_nlevels > 1) {
2381 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2382 uint64_t first, last;
2384 first = blkid >> epbs;
2385 dnode_dirty_l1(dn, first, tx);
2386 if (trunc)
2387 last = dn->dn_maxblkid >> epbs;
2388 else
2389 last = (blkid + nblks - 1) >> epbs;
2390 if (last != first)
2391 dnode_dirty_l1(dn, last, tx);
2393 dnode_dirty_l1range(dn, first, last, tx);
2395 int shift = dn->dn_datablkshift + dn->dn_indblkshift -
2396 SPA_BLKPTRSHIFT;
2397 for (uint64_t i = first + 1; i < last; i++) {
2399 * Set i to the blockid of the next non-hole
2400 * level-1 indirect block at or after i. Note
2401 * that dnode_next_offset() operates in terms of
2402 * level-0-equivalent bytes.
2404 uint64_t ibyte = i << shift;
2405 int err = dnode_next_offset(dn, DNODE_FIND_HAVELOCK,
2406 &ibyte, 2, 1, 0);
2407 i = ibyte >> shift;
2408 if (i >= last)
2409 break;
2412 * Normally we should not see an error, either
2413 * from dnode_next_offset() or dbuf_hold_level()
2414 * (except for ESRCH from dnode_next_offset).
2415 * If there is an i/o error, then when we read
2416 * this block in syncing context, it will use
2417 * ZIO_FLAG_MUSTSUCCEED, and thus hang/panic according
2418 * to the "failmode" property. dnode_next_offset()
2419 * doesn't have a flag to indicate MUSTSUCCEED.
2421 if (err != 0)
2422 break;
2424 dnode_dirty_l1(dn, i, tx);
2426 rw_exit(&dn->dn_struct_rwlock);
2429 done:
2431 * Add this range to the dnode range list.
2432 * We will finish up this free operation in the syncing phase.
2434 mutex_enter(&dn->dn_mtx);
2436 int txgoff = tx->tx_txg & TXG_MASK;
2437 if (dn->dn_free_ranges[txgoff] == NULL) {
2438 dn->dn_free_ranges[txgoff] = range_tree_create(NULL,
2439 RANGE_SEG64, NULL, 0, 0);
2441 range_tree_clear(dn->dn_free_ranges[txgoff], blkid, nblks);
2442 range_tree_add(dn->dn_free_ranges[txgoff], blkid, nblks);
2444 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
2445 (u_longlong_t)blkid, (u_longlong_t)nblks,
2446 (u_longlong_t)tx->tx_txg);
2447 mutex_exit(&dn->dn_mtx);
2449 dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
2450 dnode_setdirty(dn, tx);
2453 static boolean_t
2454 dnode_spill_freed(dnode_t *dn)
2456 int i;
2458 mutex_enter(&dn->dn_mtx);
2459 for (i = 0; i < TXG_SIZE; i++) {
2460 if (dn->dn_rm_spillblk[i] == DN_KILL_SPILLBLK)
2461 break;
2463 mutex_exit(&dn->dn_mtx);
2464 return (i < TXG_SIZE);
2467 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
2468 uint64_t
2469 dnode_block_freed(dnode_t *dn, uint64_t blkid)
2471 int i;
2473 if (blkid == DMU_BONUS_BLKID)
2474 return (FALSE);
2476 if (dn->dn_free_txg)
2477 return (TRUE);
2479 if (blkid == DMU_SPILL_BLKID)
2480 return (dnode_spill_freed(dn));
2482 mutex_enter(&dn->dn_mtx);
2483 for (i = 0; i < TXG_SIZE; i++) {
2484 if (dn->dn_free_ranges[i] != NULL &&
2485 range_tree_contains(dn->dn_free_ranges[i], blkid, 1))
2486 break;
2488 mutex_exit(&dn->dn_mtx);
2489 return (i < TXG_SIZE);
2492 /* call from syncing context when we actually write/free space for this dnode */
2493 void
2494 dnode_diduse_space(dnode_t *dn, int64_t delta)
2496 uint64_t space;
2497 dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
2498 dn, dn->dn_phys,
2499 (u_longlong_t)dn->dn_phys->dn_used,
2500 (longlong_t)delta);
2502 mutex_enter(&dn->dn_mtx);
2503 space = DN_USED_BYTES(dn->dn_phys);
2504 if (delta > 0) {
2505 ASSERT3U(space + delta, >=, space); /* no overflow */
2506 } else {
2507 ASSERT3U(space, >=, -delta); /* no underflow */
2509 space += delta;
2510 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
2511 ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
2512 ASSERT0(P2PHASE(space, 1<<DEV_BSHIFT));
2513 dn->dn_phys->dn_used = space >> DEV_BSHIFT;
2514 } else {
2515 dn->dn_phys->dn_used = space;
2516 dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
2518 mutex_exit(&dn->dn_mtx);
2522 * Scans a block at the indicated "level" looking for a hole or data,
2523 * depending on 'flags'.
2525 * If level > 0, then we are scanning an indirect block looking at its
2526 * pointers. If level == 0, then we are looking at a block of dnodes.
2528 * If we don't find what we are looking for in the block, we return ESRCH.
2529 * Otherwise, return with *offset pointing to the beginning (if searching
2530 * forwards) or end (if searching backwards) of the range covered by the
2531 * block pointer we matched on (or dnode).
2533 * The basic search algorithm used below by dnode_next_offset() is to
2534 * use this function to search up the block tree (widen the search) until
2535 * we find something (i.e., we don't return ESRCH) and then search back
2536 * down the tree (narrow the search) until we reach our original search
2537 * level.
2539 static int
2540 dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
2541 int lvl, uint64_t blkfill, uint64_t txg)
2543 dmu_buf_impl_t *db = NULL;
2544 void *data = NULL;
2545 uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2546 uint64_t epb = 1ULL << epbs;
2547 uint64_t minfill, maxfill;
2548 boolean_t hole;
2549 int i, inc, error, span;
2551 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2553 hole = ((flags & DNODE_FIND_HOLE) != 0);
2554 inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1;
2555 ASSERT(txg == 0 || !hole);
2557 if (lvl == dn->dn_phys->dn_nlevels) {
2558 error = 0;
2559 epb = dn->dn_phys->dn_nblkptr;
2560 data = dn->dn_phys->dn_blkptr;
2561 } else {
2562 uint64_t blkid = dbuf_whichblock(dn, lvl, *offset);
2563 error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FALSE, FTAG, &db);
2564 if (error) {
2565 if (error != ENOENT)
2566 return (error);
2567 if (hole)
2568 return (0);
2570 * This can only happen when we are searching up
2571 * the block tree for data. We don't really need to
2572 * adjust the offset, as we will just end up looking
2573 * at the pointer to this block in its parent, and its
2574 * going to be unallocated, so we will skip over it.
2576 return (SET_ERROR(ESRCH));
2578 error = dbuf_read(db, NULL,
2579 DB_RF_CANFAIL | DB_RF_HAVESTRUCT |
2580 DB_RF_NO_DECRYPT | DB_RF_NOPREFETCH);
2581 if (error) {
2582 dbuf_rele(db, FTAG);
2583 return (error);
2585 data = db->db.db_data;
2586 rw_enter(&db->db_rwlock, RW_READER);
2589 if (db != NULL && txg != 0 && (db->db_blkptr == NULL ||
2590 BP_GET_LOGICAL_BIRTH(db->db_blkptr) <= txg ||
2591 BP_IS_HOLE(db->db_blkptr))) {
2593 * This can only happen when we are searching up the tree
2594 * and these conditions mean that we need to keep climbing.
2596 error = SET_ERROR(ESRCH);
2597 } else if (lvl == 0) {
2598 dnode_phys_t *dnp = data;
2600 ASSERT(dn->dn_type == DMU_OT_DNODE);
2601 ASSERT(!(flags & DNODE_FIND_BACKWARDS));
2603 for (i = (*offset >> DNODE_SHIFT) & (blkfill - 1);
2604 i < blkfill; i += dnp[i].dn_extra_slots + 1) {
2605 if ((dnp[i].dn_type == DMU_OT_NONE) == hole)
2606 break;
2609 if (i == blkfill)
2610 error = SET_ERROR(ESRCH);
2612 *offset = (*offset & ~(DNODE_BLOCK_SIZE - 1)) +
2613 (i << DNODE_SHIFT);
2614 } else {
2615 blkptr_t *bp = data;
2616 uint64_t start = *offset;
2617 span = (lvl - 1) * epbs + dn->dn_datablkshift;
2618 minfill = 0;
2619 maxfill = blkfill << ((lvl - 1) * epbs);
2621 if (hole)
2622 maxfill--;
2623 else
2624 minfill++;
2626 if (span >= 8 * sizeof (*offset)) {
2627 /* This only happens on the highest indirection level */
2628 ASSERT3U((lvl - 1), ==, dn->dn_phys->dn_nlevels - 1);
2629 *offset = 0;
2630 } else {
2631 *offset = *offset >> span;
2634 for (i = BF64_GET(*offset, 0, epbs);
2635 i >= 0 && i < epb; i += inc) {
2636 if (BP_GET_FILL(&bp[i]) >= minfill &&
2637 BP_GET_FILL(&bp[i]) <= maxfill &&
2638 (hole || BP_GET_LOGICAL_BIRTH(&bp[i]) > txg))
2639 break;
2640 if (inc > 0 || *offset > 0)
2641 *offset += inc;
2644 if (span >= 8 * sizeof (*offset)) {
2645 *offset = start;
2646 } else {
2647 *offset = *offset << span;
2650 if (inc < 0) {
2651 /* traversing backwards; position offset at the end */
2652 if (span < 8 * sizeof (*offset))
2653 *offset = MIN(*offset + (1ULL << span) - 1,
2654 start);
2655 } else if (*offset < start) {
2656 *offset = start;
2658 if (i < 0 || i >= epb)
2659 error = SET_ERROR(ESRCH);
2662 if (db != NULL) {
2663 rw_exit(&db->db_rwlock);
2664 dbuf_rele(db, FTAG);
2667 return (error);
2671 * Find the next hole, data, or sparse region at or after *offset.
2672 * The value 'blkfill' tells us how many items we expect to find
2673 * in an L0 data block; this value is 1 for normal objects,
2674 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
2675 * DNODES_PER_BLOCK when searching for sparse regions thereof.
2677 * Examples:
2679 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
2680 * Finds the next/previous hole/data in a file.
2681 * Used in dmu_offset_next().
2683 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
2684 * Finds the next free/allocated dnode an objset's meta-dnode.
2685 * Only finds objects that have new contents since txg (ie.
2686 * bonus buffer changes and content removal are ignored).
2687 * Used in dmu_object_next().
2689 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
2690 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
2691 * Used in dmu_object_alloc().
2694 dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
2695 int minlvl, uint64_t blkfill, uint64_t txg)
2697 uint64_t initial_offset = *offset;
2698 int lvl, maxlvl;
2699 int error = 0;
2701 if (!(flags & DNODE_FIND_HAVELOCK))
2702 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2704 if (dn->dn_phys->dn_nlevels == 0) {
2705 error = SET_ERROR(ESRCH);
2706 goto out;
2709 if (dn->dn_datablkshift == 0) {
2710 if (*offset < dn->dn_datablksz) {
2711 if (flags & DNODE_FIND_HOLE)
2712 *offset = dn->dn_datablksz;
2713 } else {
2714 error = SET_ERROR(ESRCH);
2716 goto out;
2719 maxlvl = dn->dn_phys->dn_nlevels;
2721 for (lvl = minlvl; lvl <= maxlvl; lvl++) {
2722 error = dnode_next_offset_level(dn,
2723 flags, offset, lvl, blkfill, txg);
2724 if (error != ESRCH)
2725 break;
2728 while (error == 0 && --lvl >= minlvl) {
2729 error = dnode_next_offset_level(dn,
2730 flags, offset, lvl, blkfill, txg);
2734 * There's always a "virtual hole" at the end of the object, even
2735 * if all BP's which physically exist are non-holes.
2737 if ((flags & DNODE_FIND_HOLE) && error == ESRCH && txg == 0 &&
2738 minlvl == 1 && blkfill == 1 && !(flags & DNODE_FIND_BACKWARDS)) {
2739 error = 0;
2742 if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
2743 initial_offset < *offset : initial_offset > *offset))
2744 error = SET_ERROR(ESRCH);
2745 out:
2746 if (!(flags & DNODE_FIND_HAVELOCK))
2747 rw_exit(&dn->dn_struct_rwlock);
2749 return (error);
2752 #if defined(_KERNEL)
2753 EXPORT_SYMBOL(dnode_hold);
2754 EXPORT_SYMBOL(dnode_rele);
2755 EXPORT_SYMBOL(dnode_set_nlevels);
2756 EXPORT_SYMBOL(dnode_set_blksz);
2757 EXPORT_SYMBOL(dnode_free_range);
2758 EXPORT_SYMBOL(dnode_evict_dbufs);
2759 EXPORT_SYMBOL(dnode_evict_bonus);
2760 #endif
2762 ZFS_MODULE_PARAM(zfs, zfs_, default_bs, INT, ZMOD_RW,
2763 "Default dnode block shift");
2764 ZFS_MODULE_PARAM(zfs, zfs_, default_ibs, INT, ZMOD_RW,
2765 "Default dnode indirect block shift");