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