]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/dnode.c
Encryption Stability and On-Disk Format Fixes
[mirror_zfs.git] / module / zfs / dnode.c
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
24 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
25 */
26
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_dnode.h>
41
42 dnode_stats_t dnode_stats = {
43 { "dnode_hold_dbuf_hold", KSTAT_DATA_UINT64 },
44 { "dnode_hold_dbuf_read", KSTAT_DATA_UINT64 },
45 { "dnode_hold_alloc_hits", KSTAT_DATA_UINT64 },
46 { "dnode_hold_alloc_misses", KSTAT_DATA_UINT64 },
47 { "dnode_hold_alloc_interior", KSTAT_DATA_UINT64 },
48 { "dnode_hold_alloc_lock_retry", KSTAT_DATA_UINT64 },
49 { "dnode_hold_alloc_lock_misses", KSTAT_DATA_UINT64 },
50 { "dnode_hold_alloc_type_none", KSTAT_DATA_UINT64 },
51 { "dnode_hold_free_hits", KSTAT_DATA_UINT64 },
52 { "dnode_hold_free_misses", KSTAT_DATA_UINT64 },
53 { "dnode_hold_free_lock_misses", KSTAT_DATA_UINT64 },
54 { "dnode_hold_free_lock_retry", KSTAT_DATA_UINT64 },
55 { "dnode_hold_free_overflow", KSTAT_DATA_UINT64 },
56 { "dnode_hold_free_refcount", KSTAT_DATA_UINT64 },
57 { "dnode_hold_free_txg", KSTAT_DATA_UINT64 },
58 { "dnode_allocate", KSTAT_DATA_UINT64 },
59 { "dnode_reallocate", KSTAT_DATA_UINT64 },
60 { "dnode_buf_evict", KSTAT_DATA_UINT64 },
61 { "dnode_alloc_next_chunk", KSTAT_DATA_UINT64 },
62 { "dnode_alloc_race", KSTAT_DATA_UINT64 },
63 { "dnode_alloc_next_block", KSTAT_DATA_UINT64 },
64 { "dnode_move_invalid", KSTAT_DATA_UINT64 },
65 { "dnode_move_recheck1", KSTAT_DATA_UINT64 },
66 { "dnode_move_recheck2", KSTAT_DATA_UINT64 },
67 { "dnode_move_special", KSTAT_DATA_UINT64 },
68 { "dnode_move_handle", KSTAT_DATA_UINT64 },
69 { "dnode_move_rwlock", KSTAT_DATA_UINT64 },
70 { "dnode_move_active", KSTAT_DATA_UINT64 },
71 };
72
73 static kstat_t *dnode_ksp;
74 static kmem_cache_t *dnode_cache;
75
76 ASSERTV(static dnode_phys_t dnode_phys_zero);
77
78 int zfs_default_bs = SPA_MINBLOCKSHIFT;
79 int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
80
81 #ifdef _KERNEL
82 static kmem_cbrc_t dnode_move(void *, void *, size_t, void *);
83 #endif /* _KERNEL */
84
85 static int
86 dbuf_compare(const void *x1, const void *x2)
87 {
88 const dmu_buf_impl_t *d1 = x1;
89 const dmu_buf_impl_t *d2 = x2;
90
91 int cmp = AVL_CMP(d1->db_level, d2->db_level);
92 if (likely(cmp))
93 return (cmp);
94
95 cmp = AVL_CMP(d1->db_blkid, d2->db_blkid);
96 if (likely(cmp))
97 return (cmp);
98
99 if (d1->db_state == DB_SEARCH) {
100 ASSERT3S(d2->db_state, !=, DB_SEARCH);
101 return (-1);
102 } else if (d2->db_state == DB_SEARCH) {
103 ASSERT3S(d1->db_state, !=, DB_SEARCH);
104 return (1);
105 }
106
107 return (AVL_PCMP(d1, d2));
108 }
109
110 /* ARGSUSED */
111 static int
112 dnode_cons(void *arg, void *unused, int kmflag)
113 {
114 dnode_t *dn = arg;
115 int i;
116
117 rw_init(&dn->dn_struct_rwlock, NULL, RW_NOLOCKDEP, NULL);
118 mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
119 mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
120 cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL);
121
122 /*
123 * Every dbuf has a reference, and dropping a tracked reference is
124 * O(number of references), so don't track dn_holds.
125 */
126 refcount_create_untracked(&dn->dn_holds);
127 refcount_create(&dn->dn_tx_holds);
128 list_link_init(&dn->dn_link);
129
130 bzero(&dn->dn_next_nblkptr[0], sizeof (dn->dn_next_nblkptr));
131 bzero(&dn->dn_next_nlevels[0], sizeof (dn->dn_next_nlevels));
132 bzero(&dn->dn_next_indblkshift[0], sizeof (dn->dn_next_indblkshift));
133 bzero(&dn->dn_next_bonustype[0], sizeof (dn->dn_next_bonustype));
134 bzero(&dn->dn_rm_spillblk[0], sizeof (dn->dn_rm_spillblk));
135 bzero(&dn->dn_next_bonuslen[0], sizeof (dn->dn_next_bonuslen));
136 bzero(&dn->dn_next_blksz[0], sizeof (dn->dn_next_blksz));
137 bzero(&dn->dn_next_maxblkid[0], sizeof (dn->dn_next_maxblkid));
138
139 for (i = 0; i < TXG_SIZE; i++) {
140 list_link_init(&dn->dn_dirty_link[i]);
141 dn->dn_free_ranges[i] = NULL;
142 list_create(&dn->dn_dirty_records[i],
143 sizeof (dbuf_dirty_record_t),
144 offsetof(dbuf_dirty_record_t, dr_dirty_node));
145 }
146
147 dn->dn_allocated_txg = 0;
148 dn->dn_free_txg = 0;
149 dn->dn_assigned_txg = 0;
150 dn->dn_dirtyctx = 0;
151 dn->dn_dirtyctx_firstset = NULL;
152 dn->dn_bonus = NULL;
153 dn->dn_have_spill = B_FALSE;
154 dn->dn_zio = NULL;
155 dn->dn_oldused = 0;
156 dn->dn_oldflags = 0;
157 dn->dn_olduid = 0;
158 dn->dn_oldgid = 0;
159 dn->dn_newuid = 0;
160 dn->dn_newgid = 0;
161 dn->dn_id_flags = 0;
162
163 dn->dn_dbufs_count = 0;
164 avl_create(&dn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t),
165 offsetof(dmu_buf_impl_t, db_link));
166
167 dn->dn_moved = 0;
168 return (0);
169 }
170
171 /* ARGSUSED */
172 static void
173 dnode_dest(void *arg, void *unused)
174 {
175 int i;
176 dnode_t *dn = arg;
177
178 rw_destroy(&dn->dn_struct_rwlock);
179 mutex_destroy(&dn->dn_mtx);
180 mutex_destroy(&dn->dn_dbufs_mtx);
181 cv_destroy(&dn->dn_notxholds);
182 refcount_destroy(&dn->dn_holds);
183 refcount_destroy(&dn->dn_tx_holds);
184 ASSERT(!list_link_active(&dn->dn_link));
185
186 for (i = 0; i < TXG_SIZE; i++) {
187 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
188 ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
189 list_destroy(&dn->dn_dirty_records[i]);
190 ASSERT0(dn->dn_next_nblkptr[i]);
191 ASSERT0(dn->dn_next_nlevels[i]);
192 ASSERT0(dn->dn_next_indblkshift[i]);
193 ASSERT0(dn->dn_next_bonustype[i]);
194 ASSERT0(dn->dn_rm_spillblk[i]);
195 ASSERT0(dn->dn_next_bonuslen[i]);
196 ASSERT0(dn->dn_next_blksz[i]);
197 ASSERT0(dn->dn_next_maxblkid[i]);
198 }
199
200 ASSERT0(dn->dn_allocated_txg);
201 ASSERT0(dn->dn_free_txg);
202 ASSERT0(dn->dn_assigned_txg);
203 ASSERT0(dn->dn_dirtyctx);
204 ASSERT3P(dn->dn_dirtyctx_firstset, ==, NULL);
205 ASSERT3P(dn->dn_bonus, ==, NULL);
206 ASSERT(!dn->dn_have_spill);
207 ASSERT3P(dn->dn_zio, ==, NULL);
208 ASSERT0(dn->dn_oldused);
209 ASSERT0(dn->dn_oldflags);
210 ASSERT0(dn->dn_olduid);
211 ASSERT0(dn->dn_oldgid);
212 ASSERT0(dn->dn_newuid);
213 ASSERT0(dn->dn_newgid);
214 ASSERT0(dn->dn_id_flags);
215
216 ASSERT0(dn->dn_dbufs_count);
217 avl_destroy(&dn->dn_dbufs);
218 }
219
220 void
221 dnode_init(void)
222 {
223 ASSERT(dnode_cache == NULL);
224 dnode_cache = kmem_cache_create("dnode_t", sizeof (dnode_t),
225 0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
226 kmem_cache_set_move(dnode_cache, dnode_move);
227
228 dnode_ksp = kstat_create("zfs", 0, "dnodestats", "misc",
229 KSTAT_TYPE_NAMED, sizeof (dnode_stats) / sizeof (kstat_named_t),
230 KSTAT_FLAG_VIRTUAL);
231 if (dnode_ksp != NULL) {
232 dnode_ksp->ks_data = &dnode_stats;
233 kstat_install(dnode_ksp);
234 }
235 }
236
237 void
238 dnode_fini(void)
239 {
240 if (dnode_ksp != NULL) {
241 kstat_delete(dnode_ksp);
242 dnode_ksp = NULL;
243 }
244
245 kmem_cache_destroy(dnode_cache);
246 dnode_cache = NULL;
247 }
248
249
250 #ifdef ZFS_DEBUG
251 void
252 dnode_verify(dnode_t *dn)
253 {
254 int drop_struct_lock = FALSE;
255
256 ASSERT(dn->dn_phys);
257 ASSERT(dn->dn_objset);
258 ASSERT(dn->dn_handle->dnh_dnode == dn);
259
260 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
261
262 if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
263 return;
264
265 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
266 rw_enter(&dn->dn_struct_rwlock, RW_READER);
267 drop_struct_lock = TRUE;
268 }
269 if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
270 int i;
271 int max_bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots);
272 ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
273 if (dn->dn_datablkshift) {
274 ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
275 ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
276 ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
277 }
278 ASSERT3U(dn->dn_nlevels, <=, 30);
279 ASSERT(DMU_OT_IS_VALID(dn->dn_type));
280 ASSERT3U(dn->dn_nblkptr, >=, 1);
281 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
282 ASSERT3U(dn->dn_bonuslen, <=, max_bonuslen);
283 ASSERT3U(dn->dn_datablksz, ==,
284 dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
285 ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
286 ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
287 dn->dn_bonuslen, <=, max_bonuslen);
288 for (i = 0; i < TXG_SIZE; i++) {
289 ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
290 }
291 }
292 if (dn->dn_phys->dn_type != DMU_OT_NONE)
293 ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
294 ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_object) || dn->dn_dbuf != NULL);
295 if (dn->dn_dbuf != NULL) {
296 ASSERT3P(dn->dn_phys, ==,
297 (dnode_phys_t *)dn->dn_dbuf->db.db_data +
298 (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
299 }
300 if (drop_struct_lock)
301 rw_exit(&dn->dn_struct_rwlock);
302 }
303 #endif
304
305 void
306 dnode_byteswap(dnode_phys_t *dnp)
307 {
308 uint64_t *buf64 = (void*)&dnp->dn_blkptr;
309 int i;
310
311 if (dnp->dn_type == DMU_OT_NONE) {
312 bzero(dnp, sizeof (dnode_phys_t));
313 return;
314 }
315
316 dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
317 dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
318 dnp->dn_extra_slots = BSWAP_8(dnp->dn_extra_slots);
319 dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
320 dnp->dn_used = BSWAP_64(dnp->dn_used);
321
322 /*
323 * dn_nblkptr is only one byte, so it's OK to read it in either
324 * byte order. We can't read dn_bouslen.
325 */
326 ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
327 ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
328 for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
329 buf64[i] = BSWAP_64(buf64[i]);
330
331 /*
332 * OK to check dn_bonuslen for zero, because it won't matter if
333 * we have the wrong byte order. This is necessary because the
334 * dnode dnode is smaller than a regular dnode.
335 */
336 if (dnp->dn_bonuslen != 0) {
337 /*
338 * Note that the bonus length calculated here may be
339 * longer than the actual bonus buffer. This is because
340 * we always put the bonus buffer after the last block
341 * pointer (instead of packing it against the end of the
342 * dnode buffer).
343 */
344 int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
345 int slots = dnp->dn_extra_slots + 1;
346 size_t len = DN_SLOTS_TO_BONUSLEN(slots) - off;
347 dmu_object_byteswap_t byteswap;
348 ASSERT(DMU_OT_IS_VALID(dnp->dn_bonustype));
349 byteswap = DMU_OT_BYTESWAP(dnp->dn_bonustype);
350 dmu_ot_byteswap[byteswap].ob_func(dnp->dn_bonus + off, len);
351 }
352
353 /* Swap SPILL block if we have one */
354 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
355 byteswap_uint64_array(DN_SPILL_BLKPTR(dnp), sizeof (blkptr_t));
356 }
357
358 void
359 dnode_buf_byteswap(void *vbuf, size_t size)
360 {
361 int i = 0;
362
363 ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
364 ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
365
366 while (i < size) {
367 dnode_phys_t *dnp = (void *)(((char *)vbuf) + i);
368 dnode_byteswap(dnp);
369
370 i += DNODE_MIN_SIZE;
371 if (dnp->dn_type != DMU_OT_NONE)
372 i += dnp->dn_extra_slots * DNODE_MIN_SIZE;
373 }
374 }
375
376 void
377 dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx)
378 {
379 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
380
381 dnode_setdirty(dn, tx);
382 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
383 ASSERT3U(newsize, <=, DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots) -
384 (dn->dn_nblkptr-1) * sizeof (blkptr_t));
385 dn->dn_bonuslen = newsize;
386 if (newsize == 0)
387 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN;
388 else
389 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
390 rw_exit(&dn->dn_struct_rwlock);
391 }
392
393 void
394 dnode_setbonus_type(dnode_t *dn, dmu_object_type_t newtype, dmu_tx_t *tx)
395 {
396 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
397 dnode_setdirty(dn, tx);
398 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
399 dn->dn_bonustype = newtype;
400 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
401 rw_exit(&dn->dn_struct_rwlock);
402 }
403
404 void
405 dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx)
406 {
407 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
408 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
409 dnode_setdirty(dn, tx);
410 dn->dn_rm_spillblk[tx->tx_txg&TXG_MASK] = DN_KILL_SPILLBLK;
411 dn->dn_have_spill = B_FALSE;
412 }
413
414 static void
415 dnode_setdblksz(dnode_t *dn, int size)
416 {
417 ASSERT0(P2PHASE(size, SPA_MINBLOCKSIZE));
418 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
419 ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
420 ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
421 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
422 dn->dn_datablksz = size;
423 dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
424 dn->dn_datablkshift = ISP2(size) ? highbit64(size - 1) : 0;
425 }
426
427 static dnode_t *
428 dnode_create(objset_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
429 uint64_t object, dnode_handle_t *dnh)
430 {
431 dnode_t *dn;
432
433 dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
434 ASSERT(!POINTER_IS_VALID(dn->dn_objset));
435 dn->dn_moved = 0;
436
437 /*
438 * Defer setting dn_objset until the dnode is ready to be a candidate
439 * for the dnode_move() callback.
440 */
441 dn->dn_object = object;
442 dn->dn_dbuf = db;
443 dn->dn_handle = dnh;
444 dn->dn_phys = dnp;
445
446 if (dnp->dn_datablkszsec) {
447 dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
448 } else {
449 dn->dn_datablksz = 0;
450 dn->dn_datablkszsec = 0;
451 dn->dn_datablkshift = 0;
452 }
453 dn->dn_indblkshift = dnp->dn_indblkshift;
454 dn->dn_nlevels = dnp->dn_nlevels;
455 dn->dn_type = dnp->dn_type;
456 dn->dn_nblkptr = dnp->dn_nblkptr;
457 dn->dn_checksum = dnp->dn_checksum;
458 dn->dn_compress = dnp->dn_compress;
459 dn->dn_bonustype = dnp->dn_bonustype;
460 dn->dn_bonuslen = dnp->dn_bonuslen;
461 dn->dn_num_slots = dnp->dn_extra_slots + 1;
462 dn->dn_maxblkid = dnp->dn_maxblkid;
463 dn->dn_have_spill = ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0);
464 dn->dn_id_flags = 0;
465
466 dmu_zfetch_init(&dn->dn_zfetch, dn);
467
468 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
469 ASSERT(zrl_is_locked(&dnh->dnh_zrlock));
470 ASSERT(!DN_SLOT_IS_PTR(dnh->dnh_dnode));
471
472 mutex_enter(&os->os_lock);
473
474 /*
475 * Exclude special dnodes from os_dnodes so an empty os_dnodes
476 * signifies that the special dnodes have no references from
477 * their children (the entries in os_dnodes). This allows
478 * dnode_destroy() to easily determine if the last child has
479 * been removed and then complete eviction of the objset.
480 */
481 if (!DMU_OBJECT_IS_SPECIAL(object))
482 list_insert_head(&os->os_dnodes, dn);
483 membar_producer();
484
485 /*
486 * Everything else must be valid before assigning dn_objset
487 * makes the dnode eligible for dnode_move().
488 */
489 dn->dn_objset = os;
490
491 dnh->dnh_dnode = dn;
492 mutex_exit(&os->os_lock);
493
494 arc_space_consume(sizeof (dnode_t), ARC_SPACE_DNODE);
495
496 return (dn);
497 }
498
499 /*
500 * Caller must be holding the dnode handle, which is released upon return.
501 */
502 static void
503 dnode_destroy(dnode_t *dn)
504 {
505 objset_t *os = dn->dn_objset;
506 boolean_t complete_os_eviction = B_FALSE;
507
508 ASSERT((dn->dn_id_flags & DN_ID_NEW_EXIST) == 0);
509
510 mutex_enter(&os->os_lock);
511 POINTER_INVALIDATE(&dn->dn_objset);
512 if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
513 list_remove(&os->os_dnodes, dn);
514 complete_os_eviction =
515 list_is_empty(&os->os_dnodes) &&
516 list_link_active(&os->os_evicting_node);
517 }
518 mutex_exit(&os->os_lock);
519
520 /* the dnode can no longer move, so we can release the handle */
521 zrl_remove(&dn->dn_handle->dnh_zrlock);
522
523 dn->dn_allocated_txg = 0;
524 dn->dn_free_txg = 0;
525 dn->dn_assigned_txg = 0;
526
527 dn->dn_dirtyctx = 0;
528 if (dn->dn_dirtyctx_firstset != NULL) {
529 kmem_free(dn->dn_dirtyctx_firstset, 1);
530 dn->dn_dirtyctx_firstset = NULL;
531 }
532 if (dn->dn_bonus != NULL) {
533 mutex_enter(&dn->dn_bonus->db_mtx);
534 dbuf_destroy(dn->dn_bonus);
535 dn->dn_bonus = NULL;
536 }
537 dn->dn_zio = NULL;
538
539 dn->dn_have_spill = B_FALSE;
540 dn->dn_oldused = 0;
541 dn->dn_oldflags = 0;
542 dn->dn_olduid = 0;
543 dn->dn_oldgid = 0;
544 dn->dn_newuid = 0;
545 dn->dn_newgid = 0;
546 dn->dn_id_flags = 0;
547
548 dmu_zfetch_fini(&dn->dn_zfetch);
549 kmem_cache_free(dnode_cache, dn);
550 arc_space_return(sizeof (dnode_t), ARC_SPACE_DNODE);
551
552 if (complete_os_eviction)
553 dmu_objset_evict_done(os);
554 }
555
556 void
557 dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
558 dmu_object_type_t bonustype, int bonuslen, int dn_slots, dmu_tx_t *tx)
559 {
560 int i;
561
562 ASSERT3U(dn_slots, >, 0);
563 ASSERT3U(dn_slots << DNODE_SHIFT, <=,
564 spa_maxdnodesize(dmu_objset_spa(dn->dn_objset)));
565 ASSERT3U(blocksize, <=,
566 spa_maxblocksize(dmu_objset_spa(dn->dn_objset)));
567 if (blocksize == 0)
568 blocksize = 1 << zfs_default_bs;
569 else
570 blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
571
572 if (ibs == 0)
573 ibs = zfs_default_ibs;
574
575 ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
576
577 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d dn_slots=%d\n",
578 dn->dn_objset, dn->dn_object, tx->tx_txg, blocksize, ibs, dn_slots);
579 DNODE_STAT_BUMP(dnode_allocate);
580
581 ASSERT(dn->dn_type == DMU_OT_NONE);
582 ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
583 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
584 ASSERT(ot != DMU_OT_NONE);
585 ASSERT(DMU_OT_IS_VALID(ot));
586 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
587 (bonustype == DMU_OT_SA && bonuslen == 0) ||
588 (bonustype != DMU_OT_NONE && bonuslen != 0));
589 ASSERT(DMU_OT_IS_VALID(bonustype));
590 ASSERT3U(bonuslen, <=, DN_SLOTS_TO_BONUSLEN(dn_slots));
591 ASSERT(dn->dn_type == DMU_OT_NONE);
592 ASSERT0(dn->dn_maxblkid);
593 ASSERT0(dn->dn_allocated_txg);
594 ASSERT0(dn->dn_assigned_txg);
595 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
596 ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
597 ASSERT(avl_is_empty(&dn->dn_dbufs));
598
599 for (i = 0; i < TXG_SIZE; i++) {
600 ASSERT0(dn->dn_next_nblkptr[i]);
601 ASSERT0(dn->dn_next_nlevels[i]);
602 ASSERT0(dn->dn_next_indblkshift[i]);
603 ASSERT0(dn->dn_next_bonuslen[i]);
604 ASSERT0(dn->dn_next_bonustype[i]);
605 ASSERT0(dn->dn_rm_spillblk[i]);
606 ASSERT0(dn->dn_next_blksz[i]);
607 ASSERT0(dn->dn_next_maxblkid[i]);
608 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
609 ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
610 ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
611 }
612
613 dn->dn_type = ot;
614 dnode_setdblksz(dn, blocksize);
615 dn->dn_indblkshift = ibs;
616 dn->dn_nlevels = 1;
617 dn->dn_num_slots = dn_slots;
618 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
619 dn->dn_nblkptr = 1;
620 else {
621 dn->dn_nblkptr = MIN(DN_MAX_NBLKPTR,
622 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots) - bonuslen) >>
623 SPA_BLKPTRSHIFT));
624 }
625
626 dn->dn_bonustype = bonustype;
627 dn->dn_bonuslen = bonuslen;
628 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
629 dn->dn_compress = ZIO_COMPRESS_INHERIT;
630 dn->dn_dirtyctx = 0;
631
632 dn->dn_free_txg = 0;
633 if (dn->dn_dirtyctx_firstset) {
634 kmem_free(dn->dn_dirtyctx_firstset, 1);
635 dn->dn_dirtyctx_firstset = NULL;
636 }
637
638 dn->dn_allocated_txg = tx->tx_txg;
639 dn->dn_id_flags = 0;
640
641 dnode_setdirty(dn, tx);
642 dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
643 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
644 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
645 dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
646 }
647
648 void
649 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
650 dmu_object_type_t bonustype, int bonuslen, int dn_slots, dmu_tx_t *tx)
651 {
652 int nblkptr;
653
654 ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
655 ASSERT3U(blocksize, <=,
656 spa_maxblocksize(dmu_objset_spa(dn->dn_objset)));
657 ASSERT0(blocksize % SPA_MINBLOCKSIZE);
658 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
659 ASSERT(tx->tx_txg != 0);
660 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
661 (bonustype != DMU_OT_NONE && bonuslen != 0) ||
662 (bonustype == DMU_OT_SA && bonuslen == 0));
663 ASSERT(DMU_OT_IS_VALID(bonustype));
664 ASSERT3U(bonuslen, <=,
665 DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(dn->dn_objset))));
666
667 dn_slots = dn_slots > 0 ? dn_slots : DNODE_MIN_SLOTS;
668 DNODE_STAT_BUMP(dnode_reallocate);
669
670 /* clean up any unreferenced dbufs */
671 dnode_evict_dbufs(dn);
672
673 dn->dn_id_flags = 0;
674
675 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
676 dnode_setdirty(dn, tx);
677 if (dn->dn_datablksz != blocksize) {
678 /* change blocksize */
679 ASSERT(dn->dn_maxblkid == 0 &&
680 (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
681 dnode_block_freed(dn, 0)));
682 dnode_setdblksz(dn, blocksize);
683 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
684 }
685 if (dn->dn_bonuslen != bonuslen)
686 dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen;
687
688 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
689 nblkptr = 1;
690 else
691 nblkptr = MIN(DN_MAX_NBLKPTR,
692 1 + ((DN_SLOTS_TO_BONUSLEN(dn_slots) - bonuslen) >>
693 SPA_BLKPTRSHIFT));
694 if (dn->dn_bonustype != bonustype)
695 dn->dn_next_bonustype[tx->tx_txg&TXG_MASK] = bonustype;
696 if (dn->dn_nblkptr != nblkptr)
697 dn->dn_next_nblkptr[tx->tx_txg&TXG_MASK] = nblkptr;
698 if (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
699 dbuf_rm_spill(dn, tx);
700 dnode_rm_spill(dn, tx);
701 }
702 rw_exit(&dn->dn_struct_rwlock);
703
704 /* change type */
705 dn->dn_type = ot;
706
707 /* change bonus size and type */
708 mutex_enter(&dn->dn_mtx);
709 dn->dn_bonustype = bonustype;
710 dn->dn_bonuslen = bonuslen;
711 dn->dn_num_slots = dn_slots;
712 dn->dn_nblkptr = nblkptr;
713 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
714 dn->dn_compress = ZIO_COMPRESS_INHERIT;
715 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
716
717 /* fix up the bonus db_size */
718 if (dn->dn_bonus) {
719 dn->dn_bonus->db.db_size =
720 DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots) -
721 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
722 ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
723 }
724
725 dn->dn_allocated_txg = tx->tx_txg;
726 mutex_exit(&dn->dn_mtx);
727 }
728
729 #ifdef _KERNEL
730 static void
731 dnode_move_impl(dnode_t *odn, dnode_t *ndn)
732 {
733 int i;
734
735 ASSERT(!RW_LOCK_HELD(&odn->dn_struct_rwlock));
736 ASSERT(MUTEX_NOT_HELD(&odn->dn_mtx));
737 ASSERT(MUTEX_NOT_HELD(&odn->dn_dbufs_mtx));
738 ASSERT(!RW_LOCK_HELD(&odn->dn_zfetch.zf_rwlock));
739
740 /* Copy fields. */
741 ndn->dn_objset = odn->dn_objset;
742 ndn->dn_object = odn->dn_object;
743 ndn->dn_dbuf = odn->dn_dbuf;
744 ndn->dn_handle = odn->dn_handle;
745 ndn->dn_phys = odn->dn_phys;
746 ndn->dn_type = odn->dn_type;
747 ndn->dn_bonuslen = odn->dn_bonuslen;
748 ndn->dn_bonustype = odn->dn_bonustype;
749 ndn->dn_nblkptr = odn->dn_nblkptr;
750 ndn->dn_checksum = odn->dn_checksum;
751 ndn->dn_compress = odn->dn_compress;
752 ndn->dn_nlevels = odn->dn_nlevels;
753 ndn->dn_indblkshift = odn->dn_indblkshift;
754 ndn->dn_datablkshift = odn->dn_datablkshift;
755 ndn->dn_datablkszsec = odn->dn_datablkszsec;
756 ndn->dn_datablksz = odn->dn_datablksz;
757 ndn->dn_maxblkid = odn->dn_maxblkid;
758 ndn->dn_num_slots = odn->dn_num_slots;
759 bcopy(&odn->dn_next_nblkptr[0], &ndn->dn_next_nblkptr[0],
760 sizeof (odn->dn_next_nblkptr));
761 bcopy(&odn->dn_next_nlevels[0], &ndn->dn_next_nlevels[0],
762 sizeof (odn->dn_next_nlevels));
763 bcopy(&odn->dn_next_indblkshift[0], &ndn->dn_next_indblkshift[0],
764 sizeof (odn->dn_next_indblkshift));
765 bcopy(&odn->dn_next_bonustype[0], &ndn->dn_next_bonustype[0],
766 sizeof (odn->dn_next_bonustype));
767 bcopy(&odn->dn_rm_spillblk[0], &ndn->dn_rm_spillblk[0],
768 sizeof (odn->dn_rm_spillblk));
769 bcopy(&odn->dn_next_bonuslen[0], &ndn->dn_next_bonuslen[0],
770 sizeof (odn->dn_next_bonuslen));
771 bcopy(&odn->dn_next_blksz[0], &ndn->dn_next_blksz[0],
772 sizeof (odn->dn_next_blksz));
773 bcopy(&odn->dn_next_maxblkid[0], &ndn->dn_next_maxblkid[0],
774 sizeof (odn->dn_next_maxblkid));
775 for (i = 0; i < TXG_SIZE; i++) {
776 list_move_tail(&ndn->dn_dirty_records[i],
777 &odn->dn_dirty_records[i]);
778 }
779 bcopy(&odn->dn_free_ranges[0], &ndn->dn_free_ranges[0],
780 sizeof (odn->dn_free_ranges));
781 ndn->dn_allocated_txg = odn->dn_allocated_txg;
782 ndn->dn_free_txg = odn->dn_free_txg;
783 ndn->dn_assigned_txg = odn->dn_assigned_txg;
784 ndn->dn_dirtyctx = odn->dn_dirtyctx;
785 ndn->dn_dirtyctx_firstset = odn->dn_dirtyctx_firstset;
786 ASSERT(refcount_count(&odn->dn_tx_holds) == 0);
787 refcount_transfer(&ndn->dn_holds, &odn->dn_holds);
788 ASSERT(avl_is_empty(&ndn->dn_dbufs));
789 avl_swap(&ndn->dn_dbufs, &odn->dn_dbufs);
790 ndn->dn_dbufs_count = odn->dn_dbufs_count;
791 ndn->dn_bonus = odn->dn_bonus;
792 ndn->dn_have_spill = odn->dn_have_spill;
793 ndn->dn_zio = odn->dn_zio;
794 ndn->dn_oldused = odn->dn_oldused;
795 ndn->dn_oldflags = odn->dn_oldflags;
796 ndn->dn_olduid = odn->dn_olduid;
797 ndn->dn_oldgid = odn->dn_oldgid;
798 ndn->dn_newuid = odn->dn_newuid;
799 ndn->dn_newgid = odn->dn_newgid;
800 ndn->dn_id_flags = odn->dn_id_flags;
801 dmu_zfetch_init(&ndn->dn_zfetch, NULL);
802 list_move_tail(&ndn->dn_zfetch.zf_stream, &odn->dn_zfetch.zf_stream);
803 ndn->dn_zfetch.zf_dnode = odn->dn_zfetch.zf_dnode;
804
805 /*
806 * Update back pointers. Updating the handle fixes the back pointer of
807 * every descendant dbuf as well as the bonus dbuf.
808 */
809 ASSERT(ndn->dn_handle->dnh_dnode == odn);
810 ndn->dn_handle->dnh_dnode = ndn;
811 if (ndn->dn_zfetch.zf_dnode == odn) {
812 ndn->dn_zfetch.zf_dnode = ndn;
813 }
814
815 /*
816 * Invalidate the original dnode by clearing all of its back pointers.
817 */
818 odn->dn_dbuf = NULL;
819 odn->dn_handle = NULL;
820 avl_create(&odn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t),
821 offsetof(dmu_buf_impl_t, db_link));
822 odn->dn_dbufs_count = 0;
823 odn->dn_bonus = NULL;
824 odn->dn_zfetch.zf_dnode = NULL;
825
826 /*
827 * Set the low bit of the objset pointer to ensure that dnode_move()
828 * recognizes the dnode as invalid in any subsequent callback.
829 */
830 POINTER_INVALIDATE(&odn->dn_objset);
831
832 /*
833 * Satisfy the destructor.
834 */
835 for (i = 0; i < TXG_SIZE; i++) {
836 list_create(&odn->dn_dirty_records[i],
837 sizeof (dbuf_dirty_record_t),
838 offsetof(dbuf_dirty_record_t, dr_dirty_node));
839 odn->dn_free_ranges[i] = NULL;
840 odn->dn_next_nlevels[i] = 0;
841 odn->dn_next_indblkshift[i] = 0;
842 odn->dn_next_bonustype[i] = 0;
843 odn->dn_rm_spillblk[i] = 0;
844 odn->dn_next_bonuslen[i] = 0;
845 odn->dn_next_blksz[i] = 0;
846 }
847 odn->dn_allocated_txg = 0;
848 odn->dn_free_txg = 0;
849 odn->dn_assigned_txg = 0;
850 odn->dn_dirtyctx = 0;
851 odn->dn_dirtyctx_firstset = NULL;
852 odn->dn_have_spill = B_FALSE;
853 odn->dn_zio = NULL;
854 odn->dn_oldused = 0;
855 odn->dn_oldflags = 0;
856 odn->dn_olduid = 0;
857 odn->dn_oldgid = 0;
858 odn->dn_newuid = 0;
859 odn->dn_newgid = 0;
860 odn->dn_id_flags = 0;
861
862 /*
863 * Mark the dnode.
864 */
865 ndn->dn_moved = 1;
866 odn->dn_moved = (uint8_t)-1;
867 }
868
869 /*ARGSUSED*/
870 static kmem_cbrc_t
871 dnode_move(void *buf, void *newbuf, size_t size, void *arg)
872 {
873 dnode_t *odn = buf, *ndn = newbuf;
874 objset_t *os;
875 int64_t refcount;
876 uint32_t dbufs;
877
878 /*
879 * The dnode is on the objset's list of known dnodes if the objset
880 * pointer is valid. We set the low bit of the objset pointer when
881 * freeing the dnode to invalidate it, and the memory patterns written
882 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
883 * A newly created dnode sets the objset pointer last of all to indicate
884 * that the dnode is known and in a valid state to be moved by this
885 * function.
886 */
887 os = odn->dn_objset;
888 if (!POINTER_IS_VALID(os)) {
889 DNODE_STAT_BUMP(dnode_move_invalid);
890 return (KMEM_CBRC_DONT_KNOW);
891 }
892
893 /*
894 * Ensure that the objset does not go away during the move.
895 */
896 rw_enter(&os_lock, RW_WRITER);
897 if (os != odn->dn_objset) {
898 rw_exit(&os_lock);
899 DNODE_STAT_BUMP(dnode_move_recheck1);
900 return (KMEM_CBRC_DONT_KNOW);
901 }
902
903 /*
904 * If the dnode is still valid, then so is the objset. We know that no
905 * valid objset can be freed while we hold os_lock, so we can safely
906 * ensure that the objset remains in use.
907 */
908 mutex_enter(&os->os_lock);
909
910 /*
911 * Recheck the objset pointer in case the dnode was removed just before
912 * acquiring the lock.
913 */
914 if (os != odn->dn_objset) {
915 mutex_exit(&os->os_lock);
916 rw_exit(&os_lock);
917 DNODE_STAT_BUMP(dnode_move_recheck2);
918 return (KMEM_CBRC_DONT_KNOW);
919 }
920
921 /*
922 * At this point we know that as long as we hold os->os_lock, the dnode
923 * cannot be freed and fields within the dnode can be safely accessed.
924 * The objset listing this dnode cannot go away as long as this dnode is
925 * on its list.
926 */
927 rw_exit(&os_lock);
928 if (DMU_OBJECT_IS_SPECIAL(odn->dn_object)) {
929 mutex_exit(&os->os_lock);
930 DNODE_STAT_BUMP(dnode_move_special);
931 return (KMEM_CBRC_NO);
932 }
933 ASSERT(odn->dn_dbuf != NULL); /* only "special" dnodes have no parent */
934
935 /*
936 * Lock the dnode handle to prevent the dnode from obtaining any new
937 * holds. This also prevents the descendant dbufs and the bonus dbuf
938 * from accessing the dnode, so that we can discount their holds. The
939 * handle is safe to access because we know that while the dnode cannot
940 * go away, neither can its handle. Once we hold dnh_zrlock, we can
941 * safely move any dnode referenced only by dbufs.
942 */
943 if (!zrl_tryenter(&odn->dn_handle->dnh_zrlock)) {
944 mutex_exit(&os->os_lock);
945 DNODE_STAT_BUMP(dnode_move_handle);
946 return (KMEM_CBRC_LATER);
947 }
948
949 /*
950 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
951 * We need to guarantee that there is a hold for every dbuf in order to
952 * determine whether the dnode is actively referenced. Falsely matching
953 * a dbuf to an active hold would lead to an unsafe move. It's possible
954 * that a thread already having an active dnode hold is about to add a
955 * dbuf, and we can't compare hold and dbuf counts while the add is in
956 * progress.
957 */
958 if (!rw_tryenter(&odn->dn_struct_rwlock, RW_WRITER)) {
959 zrl_exit(&odn->dn_handle->dnh_zrlock);
960 mutex_exit(&os->os_lock);
961 DNODE_STAT_BUMP(dnode_move_rwlock);
962 return (KMEM_CBRC_LATER);
963 }
964
965 /*
966 * A dbuf may be removed (evicted) without an active dnode hold. In that
967 * case, the dbuf count is decremented under the handle lock before the
968 * dbuf's hold is released. This order ensures that if we count the hold
969 * after the dbuf is removed but before its hold is released, we will
970 * treat the unmatched hold as active and exit safely. If we count the
971 * hold before the dbuf is removed, the hold is discounted, and the
972 * removal is blocked until the move completes.
973 */
974 refcount = refcount_count(&odn->dn_holds);
975 ASSERT(refcount >= 0);
976 dbufs = odn->dn_dbufs_count;
977
978 /* We can't have more dbufs than dnode holds. */
979 ASSERT3U(dbufs, <=, refcount);
980 DTRACE_PROBE3(dnode__move, dnode_t *, odn, int64_t, refcount,
981 uint32_t, dbufs);
982
983 if (refcount > dbufs) {
984 rw_exit(&odn->dn_struct_rwlock);
985 zrl_exit(&odn->dn_handle->dnh_zrlock);
986 mutex_exit(&os->os_lock);
987 DNODE_STAT_BUMP(dnode_move_active);
988 return (KMEM_CBRC_LATER);
989 }
990
991 rw_exit(&odn->dn_struct_rwlock);
992
993 /*
994 * At this point we know that anyone with a hold on the dnode is not
995 * actively referencing it. The dnode is known and in a valid state to
996 * move. We're holding the locks needed to execute the critical section.
997 */
998 dnode_move_impl(odn, ndn);
999
1000 list_link_replace(&odn->dn_link, &ndn->dn_link);
1001 /* If the dnode was safe to move, the refcount cannot have changed. */
1002 ASSERT(refcount == refcount_count(&ndn->dn_holds));
1003 ASSERT(dbufs == ndn->dn_dbufs_count);
1004 zrl_exit(&ndn->dn_handle->dnh_zrlock); /* handle has moved */
1005 mutex_exit(&os->os_lock);
1006
1007 return (KMEM_CBRC_YES);
1008 }
1009 #endif /* _KERNEL */
1010
1011 static void
1012 dnode_slots_hold(dnode_children_t *children, int idx, int slots)
1013 {
1014 ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK);
1015
1016 for (int i = idx; i < idx + slots; i++) {
1017 dnode_handle_t *dnh = &children->dnc_children[i];
1018 zrl_add(&dnh->dnh_zrlock);
1019 }
1020 }
1021
1022 static void
1023 dnode_slots_rele(dnode_children_t *children, int idx, int slots)
1024 {
1025 ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK);
1026
1027 for (int i = idx; i < idx + slots; i++) {
1028 dnode_handle_t *dnh = &children->dnc_children[i];
1029
1030 if (zrl_is_locked(&dnh->dnh_zrlock))
1031 zrl_exit(&dnh->dnh_zrlock);
1032 else
1033 zrl_remove(&dnh->dnh_zrlock);
1034 }
1035 }
1036
1037 static int
1038 dnode_slots_tryenter(dnode_children_t *children, int idx, int slots)
1039 {
1040 ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK);
1041
1042 for (int i = idx; i < idx + slots; i++) {
1043 dnode_handle_t *dnh = &children->dnc_children[i];
1044
1045 if (!zrl_tryenter(&dnh->dnh_zrlock)) {
1046 for (int j = idx; j < i; j++) {
1047 dnh = &children->dnc_children[j];
1048 zrl_exit(&dnh->dnh_zrlock);
1049 }
1050
1051 return (0);
1052 }
1053 }
1054
1055 return (1);
1056 }
1057
1058 static void
1059 dnode_set_slots(dnode_children_t *children, int idx, int slots, void *ptr)
1060 {
1061 ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK);
1062
1063 for (int i = idx; i < idx + slots; i++) {
1064 dnode_handle_t *dnh = &children->dnc_children[i];
1065 dnh->dnh_dnode = ptr;
1066 }
1067 }
1068
1069 static boolean_t
1070 dnode_check_slots(dnode_children_t *children, int idx, int slots, void *ptr)
1071 {
1072 ASSERT3S(idx + slots, <=, DNODES_PER_BLOCK);
1073
1074 for (int i = idx; i < idx + slots; i++) {
1075 dnode_handle_t *dnh = &children->dnc_children[i];
1076 if (dnh->dnh_dnode != ptr)
1077 return (B_FALSE);
1078 }
1079
1080 return (B_TRUE);
1081 }
1082
1083 void
1084 dnode_special_close(dnode_handle_t *dnh)
1085 {
1086 dnode_t *dn = dnh->dnh_dnode;
1087
1088 /*
1089 * Wait for final references to the dnode to clear. This can
1090 * only happen if the arc is asynchronously evicting state that
1091 * has a hold on this dnode while we are trying to evict this
1092 * dnode.
1093 */
1094 while (refcount_count(&dn->dn_holds) > 0)
1095 delay(1);
1096 ASSERT(dn->dn_dbuf == NULL ||
1097 dmu_buf_get_user(&dn->dn_dbuf->db) == NULL);
1098 zrl_add(&dnh->dnh_zrlock);
1099 dnode_destroy(dn); /* implicit zrl_remove() */
1100 zrl_destroy(&dnh->dnh_zrlock);
1101 dnh->dnh_dnode = NULL;
1102 }
1103
1104 void
1105 dnode_special_open(objset_t *os, dnode_phys_t *dnp, uint64_t object,
1106 dnode_handle_t *dnh)
1107 {
1108 dnode_t *dn;
1109
1110 zrl_init(&dnh->dnh_zrlock);
1111 zrl_tryenter(&dnh->dnh_zrlock);
1112
1113 dn = dnode_create(os, dnp, NULL, object, dnh);
1114 DNODE_VERIFY(dn);
1115
1116 zrl_exit(&dnh->dnh_zrlock);
1117 }
1118
1119 static void
1120 dnode_buf_evict_async(void *dbu)
1121 {
1122 dnode_children_t *dnc = dbu;
1123
1124 DNODE_STAT_BUMP(dnode_buf_evict);
1125
1126 for (int i = 0; i < dnc->dnc_count; i++) {
1127 dnode_handle_t *dnh = &dnc->dnc_children[i];
1128 dnode_t *dn;
1129
1130 /*
1131 * The dnode handle lock guards against the dnode moving to
1132 * another valid address, so there is no need here to guard
1133 * against changes to or from NULL.
1134 */
1135 if (!DN_SLOT_IS_PTR(dnh->dnh_dnode)) {
1136 zrl_destroy(&dnh->dnh_zrlock);
1137 dnh->dnh_dnode = DN_SLOT_UNINIT;
1138 continue;
1139 }
1140
1141 zrl_add(&dnh->dnh_zrlock);
1142 dn = dnh->dnh_dnode;
1143 /*
1144 * If there are holds on this dnode, then there should
1145 * be holds on the dnode's containing dbuf as well; thus
1146 * it wouldn't be eligible for eviction and this function
1147 * would not have been called.
1148 */
1149 ASSERT(refcount_is_zero(&dn->dn_holds));
1150 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
1151
1152 dnode_destroy(dn); /* implicit zrl_remove() for first slot */
1153 zrl_destroy(&dnh->dnh_zrlock);
1154 dnh->dnh_dnode = DN_SLOT_UNINIT;
1155 }
1156 kmem_free(dnc, sizeof (dnode_children_t) +
1157 dnc->dnc_count * sizeof (dnode_handle_t));
1158 }
1159
1160 /*
1161 * When the DNODE_MUST_BE_FREE flag is set, the "slots" parameter is used
1162 * to ensure the hole at the specified object offset is large enough to
1163 * hold the dnode being created. The slots parameter is also used to ensure
1164 * a dnode does not span multiple dnode blocks. In both of these cases, if
1165 * a failure occurs, ENOSPC is returned. Keep in mind, these failure cases
1166 * are only possible when using DNODE_MUST_BE_FREE.
1167 *
1168 * If the DNODE_MUST_BE_ALLOCATED flag is set, "slots" must be 0.
1169 * dnode_hold_impl() will check if the requested dnode is already consumed
1170 * as an extra dnode slot by an large dnode, in which case it returns
1171 * ENOENT.
1172 *
1173 * errors:
1174 * EINVAL - Invalid object number or flags.
1175 * ENOSPC - Hole too small to fulfill "slots" request (DNODE_MUST_BE_FREE)
1176 * EEXIST - Refers to an allocated dnode (DNODE_MUST_BE_FREE)
1177 * - Refers to an interior dnode slot (DNODE_MUST_BE_ALLOCATED)
1178 * ENOENT - The requested dnode is not allocated (DNODE_MUST_BE_ALLOCATED)
1179 * EIO - I/O error when reading the meta dnode dbuf.
1180 *
1181 * succeeds even for free dnodes.
1182 */
1183 int
1184 dnode_hold_impl(objset_t *os, uint64_t object, int flag, int slots,
1185 void *tag, dnode_t **dnp)
1186 {
1187 int epb, idx, err;
1188 int drop_struct_lock = FALSE;
1189 int type;
1190 uint64_t blk;
1191 dnode_t *mdn, *dn;
1192 dmu_buf_impl_t *db;
1193 dnode_children_t *dnc;
1194 dnode_phys_t *dn_block;
1195 dnode_handle_t *dnh;
1196
1197 ASSERT(!(flag & DNODE_MUST_BE_ALLOCATED) || (slots == 0));
1198 ASSERT(!(flag & DNODE_MUST_BE_FREE) || (slots > 0));
1199
1200 /*
1201 * If you are holding the spa config lock as writer, you shouldn't
1202 * be asking the DMU to do *anything* unless it's the root pool
1203 * which may require us to read from the root filesystem while
1204 * holding some (not all) of the locks as writer.
1205 */
1206 ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0 ||
1207 (spa_is_root(os->os_spa) &&
1208 spa_config_held(os->os_spa, SCL_STATE, RW_WRITER)));
1209
1210 if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT) {
1211 dn = (object == DMU_USERUSED_OBJECT) ?
1212 DMU_USERUSED_DNODE(os) : DMU_GROUPUSED_DNODE(os);
1213 if (dn == NULL)
1214 return (SET_ERROR(ENOENT));
1215 type = dn->dn_type;
1216 if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE)
1217 return (SET_ERROR(ENOENT));
1218 if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)
1219 return (SET_ERROR(EEXIST));
1220 DNODE_VERIFY(dn);
1221 (void) refcount_add(&dn->dn_holds, tag);
1222 *dnp = dn;
1223 return (0);
1224 }
1225
1226 if (object == 0 || object >= DN_MAX_OBJECT)
1227 return (SET_ERROR(EINVAL));
1228
1229 mdn = DMU_META_DNODE(os);
1230 ASSERT(mdn->dn_object == DMU_META_DNODE_OBJECT);
1231
1232 DNODE_VERIFY(mdn);
1233
1234 if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
1235 rw_enter(&mdn->dn_struct_rwlock, RW_READER);
1236 drop_struct_lock = TRUE;
1237 }
1238
1239 blk = dbuf_whichblock(mdn, 0, object * sizeof (dnode_phys_t));
1240
1241 db = dbuf_hold(mdn, blk, FTAG);
1242 if (drop_struct_lock)
1243 rw_exit(&mdn->dn_struct_rwlock);
1244 if (db == NULL) {
1245 DNODE_STAT_BUMP(dnode_hold_dbuf_hold);
1246 return (SET_ERROR(EIO));
1247 }
1248
1249 /*
1250 * We do not need to decrypt to read the dnode so it doesn't matter
1251 * if we get the encrypted or decrypted version.
1252 */
1253 err = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_NO_DECRYPT);
1254 if (err) {
1255 DNODE_STAT_BUMP(dnode_hold_dbuf_read);
1256 dbuf_rele(db, FTAG);
1257 return (err);
1258 }
1259
1260 ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
1261 epb = db->db.db_size >> DNODE_SHIFT;
1262
1263 idx = object & (epb - 1);
1264 dn_block = (dnode_phys_t *)db->db.db_data;
1265
1266 ASSERT(DB_DNODE(db)->dn_type == DMU_OT_DNODE);
1267 dnc = dmu_buf_get_user(&db->db);
1268 dnh = NULL;
1269 if (dnc == NULL) {
1270 dnode_children_t *winner;
1271 int skip = 0;
1272
1273 dnc = kmem_zalloc(sizeof (dnode_children_t) +
1274 epb * sizeof (dnode_handle_t), KM_SLEEP);
1275 dnc->dnc_count = epb;
1276 dnh = &dnc->dnc_children[0];
1277
1278 /* Initialize dnode slot status from dnode_phys_t */
1279 for (int i = 0; i < epb; i++) {
1280 zrl_init(&dnh[i].dnh_zrlock);
1281
1282 if (skip) {
1283 skip--;
1284 continue;
1285 }
1286
1287 if (dn_block[i].dn_type != DMU_OT_NONE) {
1288 int interior = dn_block[i].dn_extra_slots;
1289
1290 dnode_set_slots(dnc, i, 1, DN_SLOT_ALLOCATED);
1291 dnode_set_slots(dnc, i + 1, interior,
1292 DN_SLOT_INTERIOR);
1293 skip = interior;
1294 } else {
1295 dnh[i].dnh_dnode = DN_SLOT_FREE;
1296 skip = 0;
1297 }
1298 }
1299
1300 dmu_buf_init_user(&dnc->dnc_dbu, NULL,
1301 dnode_buf_evict_async, NULL);
1302 winner = dmu_buf_set_user(&db->db, &dnc->dnc_dbu);
1303 if (winner != NULL) {
1304
1305 for (int i = 0; i < epb; i++)
1306 zrl_destroy(&dnh[i].dnh_zrlock);
1307
1308 kmem_free(dnc, sizeof (dnode_children_t) +
1309 epb * sizeof (dnode_handle_t));
1310 dnc = winner;
1311 }
1312 }
1313
1314 ASSERT(dnc->dnc_count == epb);
1315 dn = DN_SLOT_UNINIT;
1316
1317 if (flag & DNODE_MUST_BE_ALLOCATED) {
1318 slots = 1;
1319
1320 while (dn == DN_SLOT_UNINIT) {
1321 dnode_slots_hold(dnc, idx, slots);
1322 dnh = &dnc->dnc_children[idx];
1323
1324 if (DN_SLOT_IS_PTR(dnh->dnh_dnode)) {
1325 dn = dnh->dnh_dnode;
1326 break;
1327 } else if (dnh->dnh_dnode == DN_SLOT_INTERIOR) {
1328 DNODE_STAT_BUMP(dnode_hold_alloc_interior);
1329 dnode_slots_rele(dnc, idx, slots);
1330 dbuf_rele(db, FTAG);
1331 return (SET_ERROR(EEXIST));
1332 } else if (dnh->dnh_dnode != DN_SLOT_ALLOCATED) {
1333 DNODE_STAT_BUMP(dnode_hold_alloc_misses);
1334 dnode_slots_rele(dnc, idx, slots);
1335 dbuf_rele(db, FTAG);
1336 return (SET_ERROR(ENOENT));
1337 }
1338
1339 dnode_slots_rele(dnc, idx, slots);
1340 if (!dnode_slots_tryenter(dnc, idx, slots)) {
1341 DNODE_STAT_BUMP(dnode_hold_alloc_lock_retry);
1342 continue;
1343 }
1344
1345 /*
1346 * Someone else won the race and called dnode_create()
1347 * after we checked DN_SLOT_IS_PTR() above but before
1348 * we acquired the lock.
1349 */
1350 if (DN_SLOT_IS_PTR(dnh->dnh_dnode)) {
1351 DNODE_STAT_BUMP(dnode_hold_alloc_lock_misses);
1352 dn = dnh->dnh_dnode;
1353 } else {
1354 dn = dnode_create(os, dn_block + idx, db,
1355 object, dnh);
1356 }
1357 }
1358
1359 mutex_enter(&dn->dn_mtx);
1360 if (dn->dn_type == DMU_OT_NONE) {
1361 DNODE_STAT_BUMP(dnode_hold_alloc_type_none);
1362 mutex_exit(&dn->dn_mtx);
1363 dnode_slots_rele(dnc, idx, slots);
1364 dbuf_rele(db, FTAG);
1365 return (SET_ERROR(ENOENT));
1366 }
1367
1368 DNODE_STAT_BUMP(dnode_hold_alloc_hits);
1369 } else if (flag & DNODE_MUST_BE_FREE) {
1370
1371 if (idx + slots - 1 >= DNODES_PER_BLOCK) {
1372 DNODE_STAT_BUMP(dnode_hold_free_overflow);
1373 dbuf_rele(db, FTAG);
1374 return (SET_ERROR(ENOSPC));
1375 }
1376
1377 while (dn == DN_SLOT_UNINIT) {
1378 dnode_slots_hold(dnc, idx, slots);
1379
1380 if (!dnode_check_slots(dnc, idx, slots, DN_SLOT_FREE)) {
1381 DNODE_STAT_BUMP(dnode_hold_free_misses);
1382 dnode_slots_rele(dnc, idx, slots);
1383 dbuf_rele(db, FTAG);
1384 return (SET_ERROR(ENOSPC));
1385 }
1386
1387 dnode_slots_rele(dnc, idx, slots);
1388 if (!dnode_slots_tryenter(dnc, idx, slots)) {
1389 DNODE_STAT_BUMP(dnode_hold_free_lock_retry);
1390 continue;
1391 }
1392
1393 if (!dnode_check_slots(dnc, idx, slots, DN_SLOT_FREE)) {
1394 DNODE_STAT_BUMP(dnode_hold_free_lock_misses);
1395 dnode_slots_rele(dnc, idx, slots);
1396 dbuf_rele(db, FTAG);
1397 return (SET_ERROR(ENOSPC));
1398 }
1399
1400 dnh = &dnc->dnc_children[idx];
1401 dn = dnode_create(os, dn_block + idx, db, object, dnh);
1402 }
1403
1404 mutex_enter(&dn->dn_mtx);
1405 if (!refcount_is_zero(&dn->dn_holds)) {
1406 DNODE_STAT_BUMP(dnode_hold_free_refcount);
1407 mutex_exit(&dn->dn_mtx);
1408 dnode_slots_rele(dnc, idx, slots);
1409 dbuf_rele(db, FTAG);
1410 return (SET_ERROR(EEXIST));
1411 }
1412
1413 dnode_set_slots(dnc, idx + 1, slots - 1, DN_SLOT_INTERIOR);
1414 DNODE_STAT_BUMP(dnode_hold_free_hits);
1415 } else {
1416 dbuf_rele(db, FTAG);
1417 return (SET_ERROR(EINVAL));
1418 }
1419
1420 if (dn->dn_free_txg) {
1421 DNODE_STAT_BUMP(dnode_hold_free_txg);
1422 type = dn->dn_type;
1423 mutex_exit(&dn->dn_mtx);
1424 dnode_slots_rele(dnc, idx, slots);
1425 dbuf_rele(db, FTAG);
1426 return (SET_ERROR(type == DMU_OT_NONE ? ENOENT : EEXIST));
1427 }
1428
1429 if (refcount_add(&dn->dn_holds, tag) == 1)
1430 dbuf_add_ref(db, dnh);
1431
1432 mutex_exit(&dn->dn_mtx);
1433
1434 /* Now we can rely on the hold to prevent the dnode from moving. */
1435 dnode_slots_rele(dnc, idx, slots);
1436
1437 DNODE_VERIFY(dn);
1438 ASSERT3P(dn->dn_dbuf, ==, db);
1439 ASSERT3U(dn->dn_object, ==, object);
1440 dbuf_rele(db, FTAG);
1441
1442 *dnp = dn;
1443 return (0);
1444 }
1445
1446 /*
1447 * Return held dnode if the object is allocated, NULL if not.
1448 */
1449 int
1450 dnode_hold(objset_t *os, uint64_t object, void *tag, dnode_t **dnp)
1451 {
1452 return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, 0, tag,
1453 dnp));
1454 }
1455
1456 /*
1457 * Can only add a reference if there is already at least one
1458 * reference on the dnode. Returns FALSE if unable to add a
1459 * new reference.
1460 */
1461 boolean_t
1462 dnode_add_ref(dnode_t *dn, void *tag)
1463 {
1464 mutex_enter(&dn->dn_mtx);
1465 if (refcount_is_zero(&dn->dn_holds)) {
1466 mutex_exit(&dn->dn_mtx);
1467 return (FALSE);
1468 }
1469 VERIFY(1 < refcount_add(&dn->dn_holds, tag));
1470 mutex_exit(&dn->dn_mtx);
1471 return (TRUE);
1472 }
1473
1474 void
1475 dnode_rele(dnode_t *dn, void *tag)
1476 {
1477 mutex_enter(&dn->dn_mtx);
1478 dnode_rele_and_unlock(dn, tag);
1479 }
1480
1481 void
1482 dnode_rele_and_unlock(dnode_t *dn, void *tag)
1483 {
1484 uint64_t refs;
1485 /* Get while the hold prevents the dnode from moving. */
1486 dmu_buf_impl_t *db = dn->dn_dbuf;
1487 dnode_handle_t *dnh = dn->dn_handle;
1488
1489 refs = refcount_remove(&dn->dn_holds, tag);
1490 mutex_exit(&dn->dn_mtx);
1491
1492 /*
1493 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1494 * indirectly by dbuf_rele() while relying on the dnode handle to
1495 * prevent the dnode from moving, since releasing the last hold could
1496 * result in the dnode's parent dbuf evicting its dnode handles. For
1497 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1498 * other direct or indirect hold on the dnode must first drop the dnode
1499 * handle.
1500 */
1501 ASSERT(refs > 0 || dnh->dnh_zrlock.zr_owner != curthread);
1502
1503 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1504 if (refs == 0 && db != NULL) {
1505 /*
1506 * Another thread could add a hold to the dnode handle in
1507 * dnode_hold_impl() while holding the parent dbuf. Since the
1508 * hold on the parent dbuf prevents the handle from being
1509 * destroyed, the hold on the handle is OK. We can't yet assert
1510 * that the handle has zero references, but that will be
1511 * asserted anyway when the handle gets destroyed.
1512 */
1513 dbuf_rele(db, dnh);
1514 }
1515 }
1516
1517 void
1518 dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
1519 {
1520 objset_t *os = dn->dn_objset;
1521 uint64_t txg = tx->tx_txg;
1522
1523 if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
1524 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1525 return;
1526 }
1527
1528 DNODE_VERIFY(dn);
1529
1530 #ifdef ZFS_DEBUG
1531 mutex_enter(&dn->dn_mtx);
1532 ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
1533 ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg);
1534 mutex_exit(&dn->dn_mtx);
1535 #endif
1536
1537 /*
1538 * Determine old uid/gid when necessary
1539 */
1540 dmu_objset_userquota_get_ids(dn, B_TRUE, tx);
1541
1542 multilist_t *dirtylist = os->os_dirty_dnodes[txg & TXG_MASK];
1543 multilist_sublist_t *mls = multilist_sublist_lock_obj(dirtylist, dn);
1544
1545 /*
1546 * If we are already marked dirty, we're done.
1547 */
1548 if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
1549 multilist_sublist_unlock(mls);
1550 return;
1551 }
1552
1553 ASSERT(!refcount_is_zero(&dn->dn_holds) ||
1554 !avl_is_empty(&dn->dn_dbufs));
1555 ASSERT(dn->dn_datablksz != 0);
1556 ASSERT0(dn->dn_next_bonuslen[txg&TXG_MASK]);
1557 ASSERT0(dn->dn_next_blksz[txg&TXG_MASK]);
1558 ASSERT0(dn->dn_next_bonustype[txg&TXG_MASK]);
1559
1560 dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
1561 dn->dn_object, txg);
1562
1563 multilist_sublist_insert_head(mls, dn);
1564
1565 multilist_sublist_unlock(mls);
1566
1567 /*
1568 * The dnode maintains a hold on its containing dbuf as
1569 * long as there are holds on it. Each instantiated child
1570 * dbuf maintains a hold on the dnode. When the last child
1571 * drops its hold, the dnode will drop its hold on the
1572 * containing dbuf. We add a "dirty hold" here so that the
1573 * dnode will hang around after we finish processing its
1574 * children.
1575 */
1576 VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
1577
1578 (void) dbuf_dirty(dn->dn_dbuf, tx);
1579
1580 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1581 }
1582
1583 void
1584 dnode_free(dnode_t *dn, dmu_tx_t *tx)
1585 {
1586 mutex_enter(&dn->dn_mtx);
1587 if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
1588 mutex_exit(&dn->dn_mtx);
1589 return;
1590 }
1591 dn->dn_free_txg = tx->tx_txg;
1592 mutex_exit(&dn->dn_mtx);
1593
1594 dnode_setdirty(dn, tx);
1595 }
1596
1597 /*
1598 * Try to change the block size for the indicated dnode. This can only
1599 * succeed if there are no blocks allocated or dirty beyond first block
1600 */
1601 int
1602 dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
1603 {
1604 dmu_buf_impl_t *db;
1605 int err;
1606
1607 ASSERT3U(size, <=, spa_maxblocksize(dmu_objset_spa(dn->dn_objset)));
1608 if (size == 0)
1609 size = SPA_MINBLOCKSIZE;
1610 else
1611 size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
1612
1613 if (ibs == dn->dn_indblkshift)
1614 ibs = 0;
1615
1616 if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
1617 return (0);
1618
1619 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1620
1621 /* Check for any allocated blocks beyond the first */
1622 if (dn->dn_maxblkid != 0)
1623 goto fail;
1624
1625 mutex_enter(&dn->dn_dbufs_mtx);
1626 for (db = avl_first(&dn->dn_dbufs); db != NULL;
1627 db = AVL_NEXT(&dn->dn_dbufs, db)) {
1628 if (db->db_blkid != 0 && db->db_blkid != DMU_BONUS_BLKID &&
1629 db->db_blkid != DMU_SPILL_BLKID) {
1630 mutex_exit(&dn->dn_dbufs_mtx);
1631 goto fail;
1632 }
1633 }
1634 mutex_exit(&dn->dn_dbufs_mtx);
1635
1636 if (ibs && dn->dn_nlevels != 1)
1637 goto fail;
1638
1639 /* resize the old block */
1640 err = dbuf_hold_impl(dn, 0, 0, TRUE, FALSE, FTAG, &db);
1641 if (err == 0)
1642 dbuf_new_size(db, size, tx);
1643 else if (err != ENOENT)
1644 goto fail;
1645
1646 dnode_setdblksz(dn, size);
1647 dnode_setdirty(dn, tx);
1648 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
1649 if (ibs) {
1650 dn->dn_indblkshift = ibs;
1651 dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
1652 }
1653 /* rele after we have fixed the blocksize in the dnode */
1654 if (db)
1655 dbuf_rele(db, FTAG);
1656
1657 rw_exit(&dn->dn_struct_rwlock);
1658 return (0);
1659
1660 fail:
1661 rw_exit(&dn->dn_struct_rwlock);
1662 return (SET_ERROR(ENOTSUP));
1663 }
1664
1665 static void
1666 dnode_set_nlevels_impl(dnode_t *dn, int new_nlevels, dmu_tx_t *tx)
1667 {
1668 uint64_t txgoff = tx->tx_txg & TXG_MASK;
1669 int old_nlevels = dn->dn_nlevels;
1670 dmu_buf_impl_t *db;
1671 list_t *list;
1672 dbuf_dirty_record_t *new, *dr, *dr_next;
1673
1674 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
1675
1676 dn->dn_nlevels = new_nlevels;
1677
1678 ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
1679 dn->dn_next_nlevels[txgoff] = new_nlevels;
1680
1681 /* dirty the left indirects */
1682 db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
1683 ASSERT(db != NULL);
1684 new = dbuf_dirty(db, tx);
1685 dbuf_rele(db, FTAG);
1686
1687 /* transfer the dirty records to the new indirect */
1688 mutex_enter(&dn->dn_mtx);
1689 mutex_enter(&new->dt.di.dr_mtx);
1690 list = &dn->dn_dirty_records[txgoff];
1691 for (dr = list_head(list); dr; dr = dr_next) {
1692 dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
1693 if (dr->dr_dbuf->db_level != new_nlevels-1 &&
1694 dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
1695 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
1696 ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
1697 list_remove(&dn->dn_dirty_records[txgoff], dr);
1698 list_insert_tail(&new->dt.di.dr_children, dr);
1699 dr->dr_parent = new;
1700 }
1701 }
1702 mutex_exit(&new->dt.di.dr_mtx);
1703 mutex_exit(&dn->dn_mtx);
1704 }
1705
1706 int
1707 dnode_set_nlevels(dnode_t *dn, int nlevels, dmu_tx_t *tx)
1708 {
1709 int ret = 0;
1710
1711 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1712
1713 if (dn->dn_nlevels == nlevels) {
1714 ret = 0;
1715 goto out;
1716 } else if (nlevels < dn->dn_nlevels) {
1717 ret = SET_ERROR(EINVAL);
1718 goto out;
1719 }
1720
1721 dnode_set_nlevels_impl(dn, nlevels, tx);
1722
1723 out:
1724 rw_exit(&dn->dn_struct_rwlock);
1725 return (ret);
1726 }
1727
1728 /* read-holding callers must not rely on the lock being continuously held */
1729 void
1730 dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read)
1731 {
1732 int epbs, new_nlevels;
1733 uint64_t sz;
1734
1735 ASSERT(blkid != DMU_BONUS_BLKID);
1736
1737 ASSERT(have_read ?
1738 RW_READ_HELD(&dn->dn_struct_rwlock) :
1739 RW_WRITE_HELD(&dn->dn_struct_rwlock));
1740
1741 /*
1742 * if we have a read-lock, check to see if we need to do any work
1743 * before upgrading to a write-lock.
1744 */
1745 if (have_read) {
1746 if (blkid <= dn->dn_maxblkid)
1747 return;
1748
1749 if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
1750 rw_exit(&dn->dn_struct_rwlock);
1751 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1752 }
1753 }
1754
1755 if (blkid <= dn->dn_maxblkid)
1756 goto out;
1757
1758 dn->dn_maxblkid = blkid;
1759 dn->dn_next_maxblkid[tx->tx_txg & TXG_MASK] = blkid;
1760
1761 /*
1762 * Compute the number of levels necessary to support the new maxblkid.
1763 */
1764 new_nlevels = 1;
1765 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1766 for (sz = dn->dn_nblkptr;
1767 sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
1768 new_nlevels++;
1769
1770 ASSERT3U(new_nlevels, <=, DN_MAX_LEVELS);
1771
1772 if (new_nlevels > dn->dn_nlevels)
1773 dnode_set_nlevels_impl(dn, new_nlevels, tx);
1774
1775 out:
1776 if (have_read)
1777 rw_downgrade(&dn->dn_struct_rwlock);
1778 }
1779
1780 static void
1781 dnode_dirty_l1(dnode_t *dn, uint64_t l1blkid, dmu_tx_t *tx)
1782 {
1783 dmu_buf_impl_t *db = dbuf_hold_level(dn, 1, l1blkid, FTAG);
1784 if (db != NULL) {
1785 dmu_buf_will_dirty(&db->db, tx);
1786 dbuf_rele(db, FTAG);
1787 }
1788 }
1789
1790 void
1791 dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
1792 {
1793 dmu_buf_impl_t *db;
1794 uint64_t blkoff, blkid, nblks;
1795 int blksz, blkshift, head, tail;
1796 int trunc = FALSE;
1797 int epbs;
1798
1799 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1800 blksz = dn->dn_datablksz;
1801 blkshift = dn->dn_datablkshift;
1802 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1803
1804 if (len == DMU_OBJECT_END) {
1805 len = UINT64_MAX - off;
1806 trunc = TRUE;
1807 }
1808
1809 /*
1810 * First, block align the region to free:
1811 */
1812 if (ISP2(blksz)) {
1813 head = P2NPHASE(off, blksz);
1814 blkoff = P2PHASE(off, blksz);
1815 if ((off >> blkshift) > dn->dn_maxblkid)
1816 goto out;
1817 } else {
1818 ASSERT(dn->dn_maxblkid == 0);
1819 if (off == 0 && len >= blksz) {
1820 /*
1821 * Freeing the whole block; fast-track this request.
1822 * Note that we won't dirty any indirect blocks,
1823 * which is fine because we will be freeing the entire
1824 * file and thus all indirect blocks will be freed
1825 * by free_children().
1826 */
1827 blkid = 0;
1828 nblks = 1;
1829 goto done;
1830 } else if (off >= blksz) {
1831 /* Freeing past end-of-data */
1832 goto out;
1833 } else {
1834 /* Freeing part of the block. */
1835 head = blksz - off;
1836 ASSERT3U(head, >, 0);
1837 }
1838 blkoff = off;
1839 }
1840 /* zero out any partial block data at the start of the range */
1841 if (head) {
1842 ASSERT3U(blkoff + head, ==, blksz);
1843 if (len < head)
1844 head = len;
1845 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, 0, off),
1846 TRUE, FALSE, FTAG, &db) == 0) {
1847 caddr_t data;
1848
1849 /* don't dirty if it isn't on disk and isn't dirty */
1850 if (db->db_last_dirty ||
1851 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1852 rw_exit(&dn->dn_struct_rwlock);
1853 dmu_buf_will_dirty(&db->db, tx);
1854 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1855 data = db->db.db_data;
1856 bzero(data + blkoff, head);
1857 }
1858 dbuf_rele(db, FTAG);
1859 }
1860 off += head;
1861 len -= head;
1862 }
1863
1864 /* If the range was less than one block, we're done */
1865 if (len == 0)
1866 goto out;
1867
1868 /* If the remaining range is past end of file, we're done */
1869 if ((off >> blkshift) > dn->dn_maxblkid)
1870 goto out;
1871
1872 ASSERT(ISP2(blksz));
1873 if (trunc)
1874 tail = 0;
1875 else
1876 tail = P2PHASE(len, blksz);
1877
1878 ASSERT0(P2PHASE(off, blksz));
1879 /* zero out any partial block data at the end of the range */
1880 if (tail) {
1881 if (len < tail)
1882 tail = len;
1883 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, 0, off+len),
1884 TRUE, FALSE, FTAG, &db) == 0) {
1885 /* don't dirty if not on disk and not dirty */
1886 if (db->db_last_dirty ||
1887 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1888 rw_exit(&dn->dn_struct_rwlock);
1889 dmu_buf_will_dirty(&db->db, tx);
1890 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1891 bzero(db->db.db_data, tail);
1892 }
1893 dbuf_rele(db, FTAG);
1894 }
1895 len -= tail;
1896 }
1897
1898 /* If the range did not include a full block, we are done */
1899 if (len == 0)
1900 goto out;
1901
1902 ASSERT(IS_P2ALIGNED(off, blksz));
1903 ASSERT(trunc || IS_P2ALIGNED(len, blksz));
1904 blkid = off >> blkshift;
1905 nblks = len >> blkshift;
1906 if (trunc)
1907 nblks += 1;
1908
1909 /*
1910 * Dirty all the indirect blocks in this range. Note that only
1911 * the first and last indirect blocks can actually be written
1912 * (if they were partially freed) -- they must be dirtied, even if
1913 * they do not exist on disk yet. The interior blocks will
1914 * be freed by free_children(), so they will not actually be written.
1915 * Even though these interior blocks will not be written, we
1916 * dirty them for two reasons:
1917 *
1918 * - It ensures that the indirect blocks remain in memory until
1919 * syncing context. (They have already been prefetched by
1920 * dmu_tx_hold_free(), so we don't have to worry about reading
1921 * them serially here.)
1922 *
1923 * - The dirty space accounting will put pressure on the txg sync
1924 * mechanism to begin syncing, and to delay transactions if there
1925 * is a large amount of freeing. Even though these indirect
1926 * blocks will not be written, we could need to write the same
1927 * amount of space if we copy the freed BPs into deadlists.
1928 */
1929 if (dn->dn_nlevels > 1) {
1930 uint64_t first, last;
1931
1932 first = blkid >> epbs;
1933 dnode_dirty_l1(dn, first, tx);
1934 if (trunc)
1935 last = dn->dn_maxblkid >> epbs;
1936 else
1937 last = (blkid + nblks - 1) >> epbs;
1938 if (last != first)
1939 dnode_dirty_l1(dn, last, tx);
1940
1941 int shift = dn->dn_datablkshift + dn->dn_indblkshift -
1942 SPA_BLKPTRSHIFT;
1943 for (uint64_t i = first + 1; i < last; i++) {
1944 /*
1945 * Set i to the blockid of the next non-hole
1946 * level-1 indirect block at or after i. Note
1947 * that dnode_next_offset() operates in terms of
1948 * level-0-equivalent bytes.
1949 */
1950 uint64_t ibyte = i << shift;
1951 int err = dnode_next_offset(dn, DNODE_FIND_HAVELOCK,
1952 &ibyte, 2, 1, 0);
1953 i = ibyte >> shift;
1954 if (i >= last)
1955 break;
1956
1957 /*
1958 * Normally we should not see an error, either
1959 * from dnode_next_offset() or dbuf_hold_level()
1960 * (except for ESRCH from dnode_next_offset).
1961 * If there is an i/o error, then when we read
1962 * this block in syncing context, it will use
1963 * ZIO_FLAG_MUSTSUCCEED, and thus hang/panic according
1964 * to the "failmode" property. dnode_next_offset()
1965 * doesn't have a flag to indicate MUSTSUCCEED.
1966 */
1967 if (err != 0)
1968 break;
1969
1970 dnode_dirty_l1(dn, i, tx);
1971 }
1972 }
1973
1974 done:
1975 /*
1976 * Add this range to the dnode range list.
1977 * We will finish up this free operation in the syncing phase.
1978 */
1979 mutex_enter(&dn->dn_mtx);
1980 {
1981 int txgoff = tx->tx_txg & TXG_MASK;
1982 if (dn->dn_free_ranges[txgoff] == NULL) {
1983 dn->dn_free_ranges[txgoff] =
1984 range_tree_create(NULL, NULL, &dn->dn_mtx);
1985 }
1986 range_tree_clear(dn->dn_free_ranges[txgoff], blkid, nblks);
1987 range_tree_add(dn->dn_free_ranges[txgoff], blkid, nblks);
1988 }
1989 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1990 blkid, nblks, tx->tx_txg);
1991 mutex_exit(&dn->dn_mtx);
1992
1993 dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
1994 dnode_setdirty(dn, tx);
1995 out:
1996
1997 rw_exit(&dn->dn_struct_rwlock);
1998 }
1999
2000 static boolean_t
2001 dnode_spill_freed(dnode_t *dn)
2002 {
2003 int i;
2004
2005 mutex_enter(&dn->dn_mtx);
2006 for (i = 0; i < TXG_SIZE; i++) {
2007 if (dn->dn_rm_spillblk[i] == DN_KILL_SPILLBLK)
2008 break;
2009 }
2010 mutex_exit(&dn->dn_mtx);
2011 return (i < TXG_SIZE);
2012 }
2013
2014 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
2015 uint64_t
2016 dnode_block_freed(dnode_t *dn, uint64_t blkid)
2017 {
2018 void *dp = spa_get_dsl(dn->dn_objset->os_spa);
2019 int i;
2020
2021 if (blkid == DMU_BONUS_BLKID)
2022 return (FALSE);
2023
2024 /*
2025 * If we're in the process of opening the pool, dp will not be
2026 * set yet, but there shouldn't be anything dirty.
2027 */
2028 if (dp == NULL)
2029 return (FALSE);
2030
2031 if (dn->dn_free_txg)
2032 return (TRUE);
2033
2034 if (blkid == DMU_SPILL_BLKID)
2035 return (dnode_spill_freed(dn));
2036
2037 mutex_enter(&dn->dn_mtx);
2038 for (i = 0; i < TXG_SIZE; i++) {
2039 if (dn->dn_free_ranges[i] != NULL &&
2040 range_tree_contains(dn->dn_free_ranges[i], blkid, 1))
2041 break;
2042 }
2043 mutex_exit(&dn->dn_mtx);
2044 return (i < TXG_SIZE);
2045 }
2046
2047 /* call from syncing context when we actually write/free space for this dnode */
2048 void
2049 dnode_diduse_space(dnode_t *dn, int64_t delta)
2050 {
2051 uint64_t space;
2052 dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
2053 dn, dn->dn_phys,
2054 (u_longlong_t)dn->dn_phys->dn_used,
2055 (longlong_t)delta);
2056
2057 mutex_enter(&dn->dn_mtx);
2058 space = DN_USED_BYTES(dn->dn_phys);
2059 if (delta > 0) {
2060 ASSERT3U(space + delta, >=, space); /* no overflow */
2061 } else {
2062 ASSERT3U(space, >=, -delta); /* no underflow */
2063 }
2064 space += delta;
2065 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
2066 ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
2067 ASSERT0(P2PHASE(space, 1<<DEV_BSHIFT));
2068 dn->dn_phys->dn_used = space >> DEV_BSHIFT;
2069 } else {
2070 dn->dn_phys->dn_used = space;
2071 dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
2072 }
2073 mutex_exit(&dn->dn_mtx);
2074 }
2075
2076 /*
2077 * Scans a block at the indicated "level" looking for a hole or data,
2078 * depending on 'flags'.
2079 *
2080 * If level > 0, then we are scanning an indirect block looking at its
2081 * pointers. If level == 0, then we are looking at a block of dnodes.
2082 *
2083 * If we don't find what we are looking for in the block, we return ESRCH.
2084 * Otherwise, return with *offset pointing to the beginning (if searching
2085 * forwards) or end (if searching backwards) of the range covered by the
2086 * block pointer we matched on (or dnode).
2087 *
2088 * The basic search algorithm used below by dnode_next_offset() is to
2089 * use this function to search up the block tree (widen the search) until
2090 * we find something (i.e., we don't return ESRCH) and then search back
2091 * down the tree (narrow the search) until we reach our original search
2092 * level.
2093 */
2094 static int
2095 dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
2096 int lvl, uint64_t blkfill, uint64_t txg)
2097 {
2098 dmu_buf_impl_t *db = NULL;
2099 void *data = NULL;
2100 uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2101 uint64_t epb = 1ULL << epbs;
2102 uint64_t minfill, maxfill;
2103 boolean_t hole;
2104 int i, inc, error, span;
2105
2106 hole = ((flags & DNODE_FIND_HOLE) != 0);
2107 inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1;
2108 ASSERT(txg == 0 || !hole);
2109
2110 if (lvl == dn->dn_phys->dn_nlevels) {
2111 error = 0;
2112 epb = dn->dn_phys->dn_nblkptr;
2113 data = dn->dn_phys->dn_blkptr;
2114 } else {
2115 uint64_t blkid = dbuf_whichblock(dn, lvl, *offset);
2116 error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FALSE, FTAG, &db);
2117 if (error) {
2118 if (error != ENOENT)
2119 return (error);
2120 if (hole)
2121 return (0);
2122 /*
2123 * This can only happen when we are searching up
2124 * the block tree for data. We don't really need to
2125 * adjust the offset, as we will just end up looking
2126 * at the pointer to this block in its parent, and its
2127 * going to be unallocated, so we will skip over it.
2128 */
2129 return (SET_ERROR(ESRCH));
2130 }
2131 error = dbuf_read(db, NULL,
2132 DB_RF_CANFAIL | DB_RF_HAVESTRUCT | DB_RF_NO_DECRYPT);
2133 if (error) {
2134 dbuf_rele(db, FTAG);
2135 return (error);
2136 }
2137 data = db->db.db_data;
2138 }
2139
2140
2141 if (db != NULL && txg != 0 && (db->db_blkptr == NULL ||
2142 db->db_blkptr->blk_birth <= txg ||
2143 BP_IS_HOLE(db->db_blkptr))) {
2144 /*
2145 * This can only happen when we are searching up the tree
2146 * and these conditions mean that we need to keep climbing.
2147 */
2148 error = SET_ERROR(ESRCH);
2149 } else if (lvl == 0) {
2150 dnode_phys_t *dnp = data;
2151
2152 ASSERT(dn->dn_type == DMU_OT_DNODE);
2153 ASSERT(!(flags & DNODE_FIND_BACKWARDS));
2154
2155 for (i = (*offset >> DNODE_SHIFT) & (blkfill - 1);
2156 i < blkfill; i += dnp[i].dn_extra_slots + 1) {
2157 if ((dnp[i].dn_type == DMU_OT_NONE) == hole)
2158 break;
2159 }
2160
2161 if (i == blkfill)
2162 error = SET_ERROR(ESRCH);
2163
2164 *offset = (*offset & ~(DNODE_BLOCK_SIZE - 1)) +
2165 (i << DNODE_SHIFT);
2166 } else {
2167 blkptr_t *bp = data;
2168 uint64_t start = *offset;
2169 span = (lvl - 1) * epbs + dn->dn_datablkshift;
2170 minfill = 0;
2171 maxfill = blkfill << ((lvl - 1) * epbs);
2172
2173 if (hole)
2174 maxfill--;
2175 else
2176 minfill++;
2177
2178 if (span >= 8 * sizeof (*offset)) {
2179 /* This only happens on the highest indirection level */
2180 ASSERT3U((lvl - 1), ==, dn->dn_phys->dn_nlevels - 1);
2181 *offset = 0;
2182 } else {
2183 *offset = *offset >> span;
2184 }
2185
2186 for (i = BF64_GET(*offset, 0, epbs);
2187 i >= 0 && i < epb; i += inc) {
2188 if (BP_GET_FILL(&bp[i]) >= minfill &&
2189 BP_GET_FILL(&bp[i]) <= maxfill &&
2190 (hole || bp[i].blk_birth > txg))
2191 break;
2192 if (inc > 0 || *offset > 0)
2193 *offset += inc;
2194 }
2195
2196 if (span >= 8 * sizeof (*offset)) {
2197 *offset = start;
2198 } else {
2199 *offset = *offset << span;
2200 }
2201
2202 if (inc < 0) {
2203 /* traversing backwards; position offset at the end */
2204 ASSERT3U(*offset, <=, start);
2205 *offset = MIN(*offset + (1ULL << span) - 1, start);
2206 } else if (*offset < start) {
2207 *offset = start;
2208 }
2209 if (i < 0 || i >= epb)
2210 error = SET_ERROR(ESRCH);
2211 }
2212
2213 if (db)
2214 dbuf_rele(db, FTAG);
2215
2216 return (error);
2217 }
2218
2219 /*
2220 * Find the next hole, data, or sparse region at or after *offset.
2221 * The value 'blkfill' tells us how many items we expect to find
2222 * in an L0 data block; this value is 1 for normal objects,
2223 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
2224 * DNODES_PER_BLOCK when searching for sparse regions thereof.
2225 *
2226 * Examples:
2227 *
2228 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
2229 * Finds the next/previous hole/data in a file.
2230 * Used in dmu_offset_next().
2231 *
2232 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
2233 * Finds the next free/allocated dnode an objset's meta-dnode.
2234 * Only finds objects that have new contents since txg (ie.
2235 * bonus buffer changes and content removal are ignored).
2236 * Used in dmu_object_next().
2237 *
2238 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
2239 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
2240 * Used in dmu_object_alloc().
2241 */
2242 int
2243 dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
2244 int minlvl, uint64_t blkfill, uint64_t txg)
2245 {
2246 uint64_t initial_offset = *offset;
2247 int lvl, maxlvl;
2248 int error = 0;
2249
2250 if (!(flags & DNODE_FIND_HAVELOCK))
2251 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2252
2253 if (dn->dn_phys->dn_nlevels == 0) {
2254 error = SET_ERROR(ESRCH);
2255 goto out;
2256 }
2257
2258 if (dn->dn_datablkshift == 0) {
2259 if (*offset < dn->dn_datablksz) {
2260 if (flags & DNODE_FIND_HOLE)
2261 *offset = dn->dn_datablksz;
2262 } else {
2263 error = SET_ERROR(ESRCH);
2264 }
2265 goto out;
2266 }
2267
2268 maxlvl = dn->dn_phys->dn_nlevels;
2269
2270 for (lvl = minlvl; lvl <= maxlvl; lvl++) {
2271 error = dnode_next_offset_level(dn,
2272 flags, offset, lvl, blkfill, txg);
2273 if (error != ESRCH)
2274 break;
2275 }
2276
2277 while (error == 0 && --lvl >= minlvl) {
2278 error = dnode_next_offset_level(dn,
2279 flags, offset, lvl, blkfill, txg);
2280 }
2281
2282 /*
2283 * There's always a "virtual hole" at the end of the object, even
2284 * if all BP's which physically exist are non-holes.
2285 */
2286 if ((flags & DNODE_FIND_HOLE) && error == ESRCH && txg == 0 &&
2287 minlvl == 1 && blkfill == 1 && !(flags & DNODE_FIND_BACKWARDS)) {
2288 error = 0;
2289 }
2290
2291 if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
2292 initial_offset < *offset : initial_offset > *offset))
2293 error = SET_ERROR(ESRCH);
2294 out:
2295 if (!(flags & DNODE_FIND_HAVELOCK))
2296 rw_exit(&dn->dn_struct_rwlock);
2297
2298 return (error);
2299 }