Merge commit '0b09d754d66bb2026be92bbbc38f7c8ba454cf0c'
[unleashed.git] / kernel / fs / zfs / dnode.c
bloba37f4b7e92740474d02727cffbee189fe95dc818
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
24 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
25 * Copyright (c) 2014 Integros [integros.com]
28 #include <sys/zfs_context.h>
29 #include <sys/dbuf.h>
30 #include <sys/dnode.h>
31 #include <sys/dmu.h>
32 #include <sys/dmu_impl.h>
33 #include <sys/dmu_tx.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/dsl_dir.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/spa.h>
38 #include <sys/zio.h>
39 #include <sys/dmu_zfetch.h>
40 #include <sys/range_tree.h>
42 static kmem_cache_t *dnode_cache;
44 * Define DNODE_STATS to turn on statistic gathering. By default, it is only
45 * turned on when DEBUG is also defined.
47 #ifdef DEBUG
48 #define DNODE_STATS
49 #endif /* DEBUG */
51 #ifdef DNODE_STATS
52 #define DNODE_STAT_ADD(stat) ((stat)++)
53 #else
54 #define DNODE_STAT_ADD(stat) /* nothing */
55 #endif /* DNODE_STATS */
57 static dnode_phys_t dnode_phys_zero;
59 int zfs_default_bs = SPA_MINBLOCKSHIFT;
60 int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
62 static kmem_cbrc_t dnode_move(void *, void *, size_t, void *);
64 static int
65 dbuf_compare(const void *x1, const void *x2)
67 const dmu_buf_impl_t *d1 = x1;
68 const dmu_buf_impl_t *d2 = x2;
70 if (d1->db_level < d2->db_level) {
71 return (-1);
73 if (d1->db_level > d2->db_level) {
74 return (1);
77 if (d1->db_blkid < d2->db_blkid) {
78 return (-1);
80 if (d1->db_blkid > d2->db_blkid) {
81 return (1);
84 if (d1->db_state == DB_SEARCH) {
85 ASSERT3S(d2->db_state, !=, DB_SEARCH);
86 return (-1);
87 } else if (d2->db_state == DB_SEARCH) {
88 ASSERT3S(d1->db_state, !=, DB_SEARCH);
89 return (1);
92 if ((uintptr_t)d1 < (uintptr_t)d2) {
93 return (-1);
95 if ((uintptr_t)d1 > (uintptr_t)d2) {
96 return (1);
98 return (0);
101 /* ARGSUSED */
102 static int
103 dnode_cons(void *arg, void *unused, int kmflag)
105 dnode_t *dn = arg;
106 int i;
108 rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL);
109 mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
110 mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
111 cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL);
114 * Every dbuf has a reference, and dropping a tracked reference is
115 * O(number of references), so don't track dn_holds.
117 refcount_create_untracked(&dn->dn_holds);
118 refcount_create(&dn->dn_tx_holds);
119 list_link_init(&dn->dn_link);
121 bzero(&dn->dn_next_nblkptr[0], sizeof (dn->dn_next_nblkptr));
122 bzero(&dn->dn_next_nlevels[0], sizeof (dn->dn_next_nlevels));
123 bzero(&dn->dn_next_indblkshift[0], sizeof (dn->dn_next_indblkshift));
124 bzero(&dn->dn_next_bonustype[0], sizeof (dn->dn_next_bonustype));
125 bzero(&dn->dn_rm_spillblk[0], sizeof (dn->dn_rm_spillblk));
126 bzero(&dn->dn_next_bonuslen[0], sizeof (dn->dn_next_bonuslen));
127 bzero(&dn->dn_next_blksz[0], sizeof (dn->dn_next_blksz));
129 for (i = 0; i < TXG_SIZE; i++) {
130 list_link_init(&dn->dn_dirty_link[i]);
131 dn->dn_free_ranges[i] = NULL;
132 list_create(&dn->dn_dirty_records[i],
133 sizeof (dbuf_dirty_record_t),
134 offsetof(dbuf_dirty_record_t, dr_dirty_node));
137 dn->dn_allocated_txg = 0;
138 dn->dn_free_txg = 0;
139 dn->dn_assigned_txg = 0;
140 dn->dn_dirtyctx = 0;
141 dn->dn_dirtyctx_firstset = NULL;
142 dn->dn_bonus = NULL;
143 dn->dn_have_spill = B_FALSE;
144 dn->dn_zio = NULL;
145 dn->dn_oldused = 0;
146 dn->dn_oldflags = 0;
147 dn->dn_olduid = 0;
148 dn->dn_oldgid = 0;
149 dn->dn_newuid = 0;
150 dn->dn_newgid = 0;
151 dn->dn_id_flags = 0;
153 dn->dn_dbufs_count = 0;
154 avl_create(&dn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t),
155 offsetof(dmu_buf_impl_t, db_link));
157 dn->dn_moved = 0;
158 return (0);
161 /* ARGSUSED */
162 static void
163 dnode_dest(void *arg, void *unused)
165 int i;
166 dnode_t *dn = arg;
168 rw_destroy(&dn->dn_struct_rwlock);
169 mutex_destroy(&dn->dn_mtx);
170 mutex_destroy(&dn->dn_dbufs_mtx);
171 cv_destroy(&dn->dn_notxholds);
172 refcount_destroy(&dn->dn_holds);
173 refcount_destroy(&dn->dn_tx_holds);
174 ASSERT(!list_link_active(&dn->dn_link));
176 for (i = 0; i < TXG_SIZE; i++) {
177 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
178 ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
179 list_destroy(&dn->dn_dirty_records[i]);
180 ASSERT0(dn->dn_next_nblkptr[i]);
181 ASSERT0(dn->dn_next_nlevels[i]);
182 ASSERT0(dn->dn_next_indblkshift[i]);
183 ASSERT0(dn->dn_next_bonustype[i]);
184 ASSERT0(dn->dn_rm_spillblk[i]);
185 ASSERT0(dn->dn_next_bonuslen[i]);
186 ASSERT0(dn->dn_next_blksz[i]);
189 ASSERT0(dn->dn_allocated_txg);
190 ASSERT0(dn->dn_free_txg);
191 ASSERT0(dn->dn_assigned_txg);
192 ASSERT0(dn->dn_dirtyctx);
193 ASSERT3P(dn->dn_dirtyctx_firstset, ==, NULL);
194 ASSERT3P(dn->dn_bonus, ==, NULL);
195 ASSERT(!dn->dn_have_spill);
196 ASSERT3P(dn->dn_zio, ==, NULL);
197 ASSERT0(dn->dn_oldused);
198 ASSERT0(dn->dn_oldflags);
199 ASSERT0(dn->dn_olduid);
200 ASSERT0(dn->dn_oldgid);
201 ASSERT0(dn->dn_newuid);
202 ASSERT0(dn->dn_newgid);
203 ASSERT0(dn->dn_id_flags);
205 ASSERT0(dn->dn_dbufs_count);
206 avl_destroy(&dn->dn_dbufs);
209 void
210 dnode_init(void)
212 ASSERT(dnode_cache == NULL);
213 dnode_cache = kmem_cache_create("dnode_t",
214 sizeof (dnode_t),
215 0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
216 kmem_cache_set_move(dnode_cache, dnode_move);
219 void
220 dnode_fini(void)
222 kmem_cache_destroy(dnode_cache);
223 dnode_cache = NULL;
227 #ifdef ZFS_DEBUG
228 void
229 dnode_verify(dnode_t *dn)
231 int drop_struct_lock = FALSE;
233 ASSERT(dn->dn_phys);
234 ASSERT(dn->dn_objset);
235 ASSERT(dn->dn_handle->dnh_dnode == dn);
237 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
239 if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
240 return;
242 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
243 rw_enter(&dn->dn_struct_rwlock, RW_READER);
244 drop_struct_lock = TRUE;
246 if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
247 int i;
248 ASSERT3U(dn->dn_indblkshift, >=, 0);
249 ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
250 if (dn->dn_datablkshift) {
251 ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
252 ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
253 ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
255 ASSERT3U(dn->dn_nlevels, <=, 30);
256 ASSERT(DMU_OT_IS_VALID(dn->dn_type));
257 ASSERT3U(dn->dn_nblkptr, >=, 1);
258 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
259 ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
260 ASSERT3U(dn->dn_datablksz, ==,
261 dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
262 ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
263 ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
264 dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
265 for (i = 0; i < TXG_SIZE; i++) {
266 ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
269 if (dn->dn_phys->dn_type != DMU_OT_NONE)
270 ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
271 ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_object) || dn->dn_dbuf != NULL);
272 if (dn->dn_dbuf != NULL) {
273 ASSERT3P(dn->dn_phys, ==,
274 (dnode_phys_t *)dn->dn_dbuf->db.db_data +
275 (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
277 if (drop_struct_lock)
278 rw_exit(&dn->dn_struct_rwlock);
280 #endif
282 void
283 dnode_byteswap(dnode_phys_t *dnp)
285 uint64_t *buf64 = (void*)&dnp->dn_blkptr;
286 int i;
288 if (dnp->dn_type == DMU_OT_NONE) {
289 bzero(dnp, sizeof (dnode_phys_t));
290 return;
293 dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
294 dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
295 dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
296 dnp->dn_used = BSWAP_64(dnp->dn_used);
299 * dn_nblkptr is only one byte, so it's OK to read it in either
300 * byte order. We can't read dn_bouslen.
302 ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
303 ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
304 for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
305 buf64[i] = BSWAP_64(buf64[i]);
308 * OK to check dn_bonuslen for zero, because it won't matter if
309 * we have the wrong byte order. This is necessary because the
310 * dnode dnode is smaller than a regular dnode.
312 if (dnp->dn_bonuslen != 0) {
314 * Note that the bonus length calculated here may be
315 * longer than the actual bonus buffer. This is because
316 * we always put the bonus buffer after the last block
317 * pointer (instead of packing it against the end of the
318 * dnode buffer).
320 int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
321 size_t len = DN_MAX_BONUSLEN - off;
322 ASSERT(DMU_OT_IS_VALID(dnp->dn_bonustype));
323 dmu_object_byteswap_t byteswap =
324 DMU_OT_BYTESWAP(dnp->dn_bonustype);
325 dmu_ot_byteswap[byteswap].ob_func(dnp->dn_bonus + off, len);
328 /* Swap SPILL block if we have one */
329 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
330 byteswap_uint64_array(&dnp->dn_spill, sizeof (blkptr_t));
334 void
335 dnode_buf_byteswap(void *vbuf, size_t size)
337 dnode_phys_t *buf = vbuf;
338 int i;
340 ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
341 ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
343 size >>= DNODE_SHIFT;
344 for (i = 0; i < size; i++) {
345 dnode_byteswap(buf);
346 buf++;
350 void
351 dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx)
353 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
355 dnode_setdirty(dn, tx);
356 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
357 ASSERT3U(newsize, <=, DN_MAX_BONUSLEN -
358 (dn->dn_nblkptr-1) * sizeof (blkptr_t));
359 dn->dn_bonuslen = newsize;
360 if (newsize == 0)
361 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN;
362 else
363 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
364 rw_exit(&dn->dn_struct_rwlock);
367 void
368 dnode_setbonus_type(dnode_t *dn, dmu_object_type_t newtype, dmu_tx_t *tx)
370 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
371 dnode_setdirty(dn, tx);
372 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
373 dn->dn_bonustype = newtype;
374 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
375 rw_exit(&dn->dn_struct_rwlock);
378 void
379 dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx)
381 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
382 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
383 dnode_setdirty(dn, tx);
384 dn->dn_rm_spillblk[tx->tx_txg&TXG_MASK] = DN_KILL_SPILLBLK;
385 dn->dn_have_spill = B_FALSE;
388 static void
389 dnode_setdblksz(dnode_t *dn, int size)
391 ASSERT0(P2PHASE(size, SPA_MINBLOCKSIZE));
392 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
393 ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
394 ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
395 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
396 dn->dn_datablksz = size;
397 dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
398 dn->dn_datablkshift = ISP2(size) ? highbit64(size - 1) : 0;
401 static dnode_t *
402 dnode_create(objset_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
403 uint64_t object, dnode_handle_t *dnh)
405 dnode_t *dn;
407 dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
408 ASSERT(!POINTER_IS_VALID(dn->dn_objset));
409 dn->dn_moved = 0;
412 * Defer setting dn_objset until the dnode is ready to be a candidate
413 * for the dnode_move() callback.
415 dn->dn_object = object;
416 dn->dn_dbuf = db;
417 dn->dn_handle = dnh;
418 dn->dn_phys = dnp;
420 if (dnp->dn_datablkszsec) {
421 dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
422 } else {
423 dn->dn_datablksz = 0;
424 dn->dn_datablkszsec = 0;
425 dn->dn_datablkshift = 0;
427 dn->dn_indblkshift = dnp->dn_indblkshift;
428 dn->dn_nlevels = dnp->dn_nlevels;
429 dn->dn_type = dnp->dn_type;
430 dn->dn_nblkptr = dnp->dn_nblkptr;
431 dn->dn_checksum = dnp->dn_checksum;
432 dn->dn_compress = dnp->dn_compress;
433 dn->dn_bonustype = dnp->dn_bonustype;
434 dn->dn_bonuslen = dnp->dn_bonuslen;
435 dn->dn_maxblkid = dnp->dn_maxblkid;
436 dn->dn_have_spill = ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0);
437 dn->dn_id_flags = 0;
439 dmu_zfetch_init(&dn->dn_zfetch, dn);
441 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
443 mutex_enter(&os->os_lock);
444 if (dnh->dnh_dnode != NULL) {
445 /* Lost the allocation race. */
446 mutex_exit(&os->os_lock);
447 kmem_cache_free(dnode_cache, dn);
448 return (dnh->dnh_dnode);
452 * Exclude special dnodes from os_dnodes so an empty os_dnodes
453 * signifies that the special dnodes have no references from
454 * their children (the entries in os_dnodes). This allows
455 * dnode_destroy() to easily determine if the last child has
456 * been removed and then complete eviction of the objset.
458 if (!DMU_OBJECT_IS_SPECIAL(object))
459 list_insert_head(&os->os_dnodes, dn);
460 membar_producer();
463 * Everything else must be valid before assigning dn_objset
464 * makes the dnode eligible for dnode_move().
466 dn->dn_objset = os;
468 dnh->dnh_dnode = dn;
469 mutex_exit(&os->os_lock);
471 arc_space_consume(sizeof (dnode_t), ARC_SPACE_OTHER);
472 return (dn);
476 * Caller must be holding the dnode handle, which is released upon return.
478 static void
479 dnode_destroy(dnode_t *dn)
481 objset_t *os = dn->dn_objset;
482 boolean_t complete_os_eviction = B_FALSE;
484 ASSERT((dn->dn_id_flags & DN_ID_NEW_EXIST) == 0);
486 mutex_enter(&os->os_lock);
487 POINTER_INVALIDATE(&dn->dn_objset);
488 if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
489 list_remove(&os->os_dnodes, dn);
490 complete_os_eviction =
491 list_is_empty(&os->os_dnodes) &&
492 list_link_active(&os->os_evicting_node);
494 mutex_exit(&os->os_lock);
496 /* the dnode can no longer move, so we can release the handle */
497 zrl_remove(&dn->dn_handle->dnh_zrlock);
499 dn->dn_allocated_txg = 0;
500 dn->dn_free_txg = 0;
501 dn->dn_assigned_txg = 0;
503 dn->dn_dirtyctx = 0;
504 if (dn->dn_dirtyctx_firstset != NULL) {
505 kmem_free(dn->dn_dirtyctx_firstset, 1);
506 dn->dn_dirtyctx_firstset = NULL;
508 if (dn->dn_bonus != NULL) {
509 mutex_enter(&dn->dn_bonus->db_mtx);
510 dbuf_destroy(dn->dn_bonus);
511 dn->dn_bonus = NULL;
513 dn->dn_zio = NULL;
515 dn->dn_have_spill = B_FALSE;
516 dn->dn_oldused = 0;
517 dn->dn_oldflags = 0;
518 dn->dn_olduid = 0;
519 dn->dn_oldgid = 0;
520 dn->dn_newuid = 0;
521 dn->dn_newgid = 0;
522 dn->dn_id_flags = 0;
524 dmu_zfetch_fini(&dn->dn_zfetch);
525 kmem_cache_free(dnode_cache, dn);
526 arc_space_return(sizeof (dnode_t), ARC_SPACE_OTHER);
528 if (complete_os_eviction)
529 dmu_objset_evict_done(os);
532 void
533 dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
534 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
536 int i;
538 ASSERT3U(blocksize, <=,
539 spa_maxblocksize(dmu_objset_spa(dn->dn_objset)));
540 if (blocksize == 0)
541 blocksize = 1 << zfs_default_bs;
542 else
543 blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
545 if (ibs == 0)
546 ibs = zfs_default_ibs;
548 ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
550 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
551 dn->dn_object, tx->tx_txg, blocksize, ibs);
553 ASSERT(dn->dn_type == DMU_OT_NONE);
554 ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
555 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
556 ASSERT(ot != DMU_OT_NONE);
557 ASSERT(DMU_OT_IS_VALID(ot));
558 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
559 (bonustype == DMU_OT_SA && bonuslen == 0) ||
560 (bonustype != DMU_OT_NONE && bonuslen != 0));
561 ASSERT(DMU_OT_IS_VALID(bonustype));
562 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
563 ASSERT(dn->dn_type == DMU_OT_NONE);
564 ASSERT0(dn->dn_maxblkid);
565 ASSERT0(dn->dn_allocated_txg);
566 ASSERT0(dn->dn_assigned_txg);
567 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
568 ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
569 ASSERT(avl_is_empty(&dn->dn_dbufs));
571 for (i = 0; i < TXG_SIZE; i++) {
572 ASSERT0(dn->dn_next_nblkptr[i]);
573 ASSERT0(dn->dn_next_nlevels[i]);
574 ASSERT0(dn->dn_next_indblkshift[i]);
575 ASSERT0(dn->dn_next_bonuslen[i]);
576 ASSERT0(dn->dn_next_bonustype[i]);
577 ASSERT0(dn->dn_rm_spillblk[i]);
578 ASSERT0(dn->dn_next_blksz[i]);
579 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
580 ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
581 ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
584 dn->dn_type = ot;
585 dnode_setdblksz(dn, blocksize);
586 dn->dn_indblkshift = ibs;
587 dn->dn_nlevels = 1;
588 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
589 dn->dn_nblkptr = 1;
590 else
591 dn->dn_nblkptr = 1 +
592 ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
593 dn->dn_bonustype = bonustype;
594 dn->dn_bonuslen = bonuslen;
595 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
596 dn->dn_compress = ZIO_COMPRESS_INHERIT;
597 dn->dn_dirtyctx = 0;
599 dn->dn_free_txg = 0;
600 if (dn->dn_dirtyctx_firstset) {
601 kmem_free(dn->dn_dirtyctx_firstset, 1);
602 dn->dn_dirtyctx_firstset = NULL;
605 dn->dn_allocated_txg = tx->tx_txg;
606 dn->dn_id_flags = 0;
608 dnode_setdirty(dn, tx);
609 dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
610 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
611 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
612 dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
615 void
616 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
617 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
619 int nblkptr;
621 ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
622 ASSERT3U(blocksize, <=,
623 spa_maxblocksize(dmu_objset_spa(dn->dn_objset)));
624 ASSERT0(blocksize % SPA_MINBLOCKSIZE);
625 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
626 ASSERT(tx->tx_txg != 0);
627 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
628 (bonustype != DMU_OT_NONE && bonuslen != 0) ||
629 (bonustype == DMU_OT_SA && bonuslen == 0));
630 ASSERT(DMU_OT_IS_VALID(bonustype));
631 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
633 /* clean up any unreferenced dbufs */
634 dnode_evict_dbufs(dn);
636 dn->dn_id_flags = 0;
638 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
639 dnode_setdirty(dn, tx);
640 if (dn->dn_datablksz != blocksize) {
641 /* change blocksize */
642 ASSERT(dn->dn_maxblkid == 0 &&
643 (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
644 dnode_block_freed(dn, 0)));
645 dnode_setdblksz(dn, blocksize);
646 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
648 if (dn->dn_bonuslen != bonuslen)
649 dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen;
651 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
652 nblkptr = 1;
653 else
654 nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
655 if (dn->dn_bonustype != bonustype)
656 dn->dn_next_bonustype[tx->tx_txg&TXG_MASK] = bonustype;
657 if (dn->dn_nblkptr != nblkptr)
658 dn->dn_next_nblkptr[tx->tx_txg&TXG_MASK] = nblkptr;
659 if (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
660 dbuf_rm_spill(dn, tx);
661 dnode_rm_spill(dn, tx);
663 rw_exit(&dn->dn_struct_rwlock);
665 /* change type */
666 dn->dn_type = ot;
668 /* change bonus size and type */
669 mutex_enter(&dn->dn_mtx);
670 dn->dn_bonustype = bonustype;
671 dn->dn_bonuslen = bonuslen;
672 dn->dn_nblkptr = nblkptr;
673 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
674 dn->dn_compress = ZIO_COMPRESS_INHERIT;
675 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
677 /* fix up the bonus db_size */
678 if (dn->dn_bonus) {
679 dn->dn_bonus->db.db_size =
680 DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t);
681 ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
684 dn->dn_allocated_txg = tx->tx_txg;
685 mutex_exit(&dn->dn_mtx);
688 #ifdef DNODE_STATS
689 static struct {
690 uint64_t dms_dnode_invalid;
691 uint64_t dms_dnode_recheck1;
692 uint64_t dms_dnode_recheck2;
693 uint64_t dms_dnode_special;
694 uint64_t dms_dnode_handle;
695 uint64_t dms_dnode_rwlock;
696 uint64_t dms_dnode_active;
697 } dnode_move_stats;
698 #endif /* DNODE_STATS */
700 static void
701 dnode_move_impl(dnode_t *odn, dnode_t *ndn)
703 int i;
705 ASSERT(!RW_LOCK_HELD(&odn->dn_struct_rwlock));
706 ASSERT(MUTEX_NOT_HELD(&odn->dn_mtx));
707 ASSERT(MUTEX_NOT_HELD(&odn->dn_dbufs_mtx));
708 ASSERT(!RW_LOCK_HELD(&odn->dn_zfetch.zf_rwlock));
710 /* Copy fields. */
711 ndn->dn_objset = odn->dn_objset;
712 ndn->dn_object = odn->dn_object;
713 ndn->dn_dbuf = odn->dn_dbuf;
714 ndn->dn_handle = odn->dn_handle;
715 ndn->dn_phys = odn->dn_phys;
716 ndn->dn_type = odn->dn_type;
717 ndn->dn_bonuslen = odn->dn_bonuslen;
718 ndn->dn_bonustype = odn->dn_bonustype;
719 ndn->dn_nblkptr = odn->dn_nblkptr;
720 ndn->dn_checksum = odn->dn_checksum;
721 ndn->dn_compress = odn->dn_compress;
722 ndn->dn_nlevels = odn->dn_nlevels;
723 ndn->dn_indblkshift = odn->dn_indblkshift;
724 ndn->dn_datablkshift = odn->dn_datablkshift;
725 ndn->dn_datablkszsec = odn->dn_datablkszsec;
726 ndn->dn_datablksz = odn->dn_datablksz;
727 ndn->dn_maxblkid = odn->dn_maxblkid;
728 bcopy(&odn->dn_next_nblkptr[0], &ndn->dn_next_nblkptr[0],
729 sizeof (odn->dn_next_nblkptr));
730 bcopy(&odn->dn_next_nlevels[0], &ndn->dn_next_nlevels[0],
731 sizeof (odn->dn_next_nlevels));
732 bcopy(&odn->dn_next_indblkshift[0], &ndn->dn_next_indblkshift[0],
733 sizeof (odn->dn_next_indblkshift));
734 bcopy(&odn->dn_next_bonustype[0], &ndn->dn_next_bonustype[0],
735 sizeof (odn->dn_next_bonustype));
736 bcopy(&odn->dn_rm_spillblk[0], &ndn->dn_rm_spillblk[0],
737 sizeof (odn->dn_rm_spillblk));
738 bcopy(&odn->dn_next_bonuslen[0], &ndn->dn_next_bonuslen[0],
739 sizeof (odn->dn_next_bonuslen));
740 bcopy(&odn->dn_next_blksz[0], &ndn->dn_next_blksz[0],
741 sizeof (odn->dn_next_blksz));
742 for (i = 0; i < TXG_SIZE; i++) {
743 list_move_tail(&ndn->dn_dirty_records[i],
744 &odn->dn_dirty_records[i]);
746 bcopy(&odn->dn_free_ranges[0], &ndn->dn_free_ranges[0],
747 sizeof (odn->dn_free_ranges));
748 ndn->dn_allocated_txg = odn->dn_allocated_txg;
749 ndn->dn_free_txg = odn->dn_free_txg;
750 ndn->dn_assigned_txg = odn->dn_assigned_txg;
751 ndn->dn_dirtyctx = odn->dn_dirtyctx;
752 ndn->dn_dirtyctx_firstset = odn->dn_dirtyctx_firstset;
753 ASSERT(refcount_count(&odn->dn_tx_holds) == 0);
754 refcount_transfer(&ndn->dn_holds, &odn->dn_holds);
755 ASSERT(avl_is_empty(&ndn->dn_dbufs));
756 avl_swap(&ndn->dn_dbufs, &odn->dn_dbufs);
757 ndn->dn_dbufs_count = odn->dn_dbufs_count;
758 ndn->dn_bonus = odn->dn_bonus;
759 ndn->dn_have_spill = odn->dn_have_spill;
760 ndn->dn_zio = odn->dn_zio;
761 ndn->dn_oldused = odn->dn_oldused;
762 ndn->dn_oldflags = odn->dn_oldflags;
763 ndn->dn_olduid = odn->dn_olduid;
764 ndn->dn_oldgid = odn->dn_oldgid;
765 ndn->dn_newuid = odn->dn_newuid;
766 ndn->dn_newgid = odn->dn_newgid;
767 ndn->dn_id_flags = odn->dn_id_flags;
768 dmu_zfetch_init(&ndn->dn_zfetch, NULL);
769 list_move_tail(&ndn->dn_zfetch.zf_stream, &odn->dn_zfetch.zf_stream);
770 ndn->dn_zfetch.zf_dnode = odn->dn_zfetch.zf_dnode;
773 * Update back pointers. Updating the handle fixes the back pointer of
774 * every descendant dbuf as well as the bonus dbuf.
776 ASSERT(ndn->dn_handle->dnh_dnode == odn);
777 ndn->dn_handle->dnh_dnode = ndn;
778 if (ndn->dn_zfetch.zf_dnode == odn) {
779 ndn->dn_zfetch.zf_dnode = ndn;
783 * Invalidate the original dnode by clearing all of its back pointers.
785 odn->dn_dbuf = NULL;
786 odn->dn_handle = NULL;
787 avl_create(&odn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t),
788 offsetof(dmu_buf_impl_t, db_link));
789 odn->dn_dbufs_count = 0;
790 odn->dn_bonus = NULL;
791 odn->dn_zfetch.zf_dnode = NULL;
794 * Set the low bit of the objset pointer to ensure that dnode_move()
795 * recognizes the dnode as invalid in any subsequent callback.
797 POINTER_INVALIDATE(&odn->dn_objset);
800 * Satisfy the destructor.
802 for (i = 0; i < TXG_SIZE; i++) {
803 list_create(&odn->dn_dirty_records[i],
804 sizeof (dbuf_dirty_record_t),
805 offsetof(dbuf_dirty_record_t, dr_dirty_node));
806 odn->dn_free_ranges[i] = NULL;
807 odn->dn_next_nlevels[i] = 0;
808 odn->dn_next_indblkshift[i] = 0;
809 odn->dn_next_bonustype[i] = 0;
810 odn->dn_rm_spillblk[i] = 0;
811 odn->dn_next_bonuslen[i] = 0;
812 odn->dn_next_blksz[i] = 0;
814 odn->dn_allocated_txg = 0;
815 odn->dn_free_txg = 0;
816 odn->dn_assigned_txg = 0;
817 odn->dn_dirtyctx = 0;
818 odn->dn_dirtyctx_firstset = NULL;
819 odn->dn_have_spill = B_FALSE;
820 odn->dn_zio = NULL;
821 odn->dn_oldused = 0;
822 odn->dn_oldflags = 0;
823 odn->dn_olduid = 0;
824 odn->dn_oldgid = 0;
825 odn->dn_newuid = 0;
826 odn->dn_newgid = 0;
827 odn->dn_id_flags = 0;
830 * Mark the dnode.
832 ndn->dn_moved = 1;
833 odn->dn_moved = (uint8_t)-1;
836 #ifdef _KERNEL
837 /*ARGSUSED*/
838 static kmem_cbrc_t
839 dnode_move(void *buf, void *newbuf, size_t size, void *arg)
841 dnode_t *odn = buf, *ndn = newbuf;
842 objset_t *os;
843 int64_t refcount;
844 uint32_t dbufs;
847 * The dnode is on the objset's list of known dnodes if the objset
848 * pointer is valid. We set the low bit of the objset pointer when
849 * freeing the dnode to invalidate it, and the memory patterns written
850 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
851 * A newly created dnode sets the objset pointer last of all to indicate
852 * that the dnode is known and in a valid state to be moved by this
853 * function.
855 os = odn->dn_objset;
856 if (!POINTER_IS_VALID(os)) {
857 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_invalid);
858 return (KMEM_CBRC_DONT_KNOW);
862 * Ensure that the objset does not go away during the move.
864 rw_enter(&os_lock, RW_WRITER);
865 if (os != odn->dn_objset) {
866 rw_exit(&os_lock);
867 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck1);
868 return (KMEM_CBRC_DONT_KNOW);
872 * If the dnode is still valid, then so is the objset. We know that no
873 * valid objset can be freed while we hold os_lock, so we can safely
874 * ensure that the objset remains in use.
876 mutex_enter(&os->os_lock);
879 * Recheck the objset pointer in case the dnode was removed just before
880 * acquiring the lock.
882 if (os != odn->dn_objset) {
883 mutex_exit(&os->os_lock);
884 rw_exit(&os_lock);
885 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck2);
886 return (KMEM_CBRC_DONT_KNOW);
890 * At this point we know that as long as we hold os->os_lock, the dnode
891 * cannot be freed and fields within the dnode can be safely accessed.
892 * The objset listing this dnode cannot go away as long as this dnode is
893 * on its list.
895 rw_exit(&os_lock);
896 if (DMU_OBJECT_IS_SPECIAL(odn->dn_object)) {
897 mutex_exit(&os->os_lock);
898 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_special);
899 return (KMEM_CBRC_NO);
901 ASSERT(odn->dn_dbuf != NULL); /* only "special" dnodes have no parent */
904 * Lock the dnode handle to prevent the dnode from obtaining any new
905 * holds. This also prevents the descendant dbufs and the bonus dbuf
906 * from accessing the dnode, so that we can discount their holds. The
907 * handle is safe to access because we know that while the dnode cannot
908 * go away, neither can its handle. Once we hold dnh_zrlock, we can
909 * safely move any dnode referenced only by dbufs.
911 if (!zrl_tryenter(&odn->dn_handle->dnh_zrlock)) {
912 mutex_exit(&os->os_lock);
913 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_handle);
914 return (KMEM_CBRC_LATER);
918 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
919 * We need to guarantee that there is a hold for every dbuf in order to
920 * determine whether the dnode is actively referenced. Falsely matching
921 * a dbuf to an active hold would lead to an unsafe move. It's possible
922 * that a thread already having an active dnode hold is about to add a
923 * dbuf, and we can't compare hold and dbuf counts while the add is in
924 * progress.
926 if (!rw_tryenter(&odn->dn_struct_rwlock, RW_WRITER)) {
927 zrl_exit(&odn->dn_handle->dnh_zrlock);
928 mutex_exit(&os->os_lock);
929 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_rwlock);
930 return (KMEM_CBRC_LATER);
934 * A dbuf may be removed (evicted) without an active dnode hold. In that
935 * case, the dbuf count is decremented under the handle lock before the
936 * dbuf's hold is released. This order ensures that if we count the hold
937 * after the dbuf is removed but before its hold is released, we will
938 * treat the unmatched hold as active and exit safely. If we count the
939 * hold before the dbuf is removed, the hold is discounted, and the
940 * removal is blocked until the move completes.
942 refcount = refcount_count(&odn->dn_holds);
943 ASSERT(refcount >= 0);
944 dbufs = odn->dn_dbufs_count;
946 /* We can't have more dbufs than dnode holds. */
947 ASSERT3U(dbufs, <=, refcount);
948 DTRACE_PROBE3(dnode__move, dnode_t *, odn, int64_t, refcount,
949 uint32_t, dbufs);
951 if (refcount > dbufs) {
952 rw_exit(&odn->dn_struct_rwlock);
953 zrl_exit(&odn->dn_handle->dnh_zrlock);
954 mutex_exit(&os->os_lock);
955 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_active);
956 return (KMEM_CBRC_LATER);
959 rw_exit(&odn->dn_struct_rwlock);
962 * At this point we know that anyone with a hold on the dnode is not
963 * actively referencing it. The dnode is known and in a valid state to
964 * move. We're holding the locks needed to execute the critical section.
966 dnode_move_impl(odn, ndn);
968 list_link_replace(&odn->dn_link, &ndn->dn_link);
969 /* If the dnode was safe to move, the refcount cannot have changed. */
970 ASSERT(refcount == refcount_count(&ndn->dn_holds));
971 ASSERT(dbufs == ndn->dn_dbufs_count);
972 zrl_exit(&ndn->dn_handle->dnh_zrlock); /* handle has moved */
973 mutex_exit(&os->os_lock);
975 return (KMEM_CBRC_YES);
977 #endif /* _KERNEL */
979 void
980 dnode_special_close(dnode_handle_t *dnh)
982 dnode_t *dn = dnh->dnh_dnode;
985 * Wait for final references to the dnode to clear. This can
986 * only happen if the arc is asyncronously evicting state that
987 * has a hold on this dnode while we are trying to evict this
988 * dnode.
990 while (refcount_count(&dn->dn_holds) > 0)
991 delay(1);
992 ASSERT(dn->dn_dbuf == NULL ||
993 dmu_buf_get_user(&dn->dn_dbuf->db) == NULL);
994 zrl_add(&dnh->dnh_zrlock);
995 dnode_destroy(dn); /* implicit zrl_remove() */
996 zrl_destroy(&dnh->dnh_zrlock);
997 dnh->dnh_dnode = NULL;
1000 void
1001 dnode_special_open(objset_t *os, dnode_phys_t *dnp, uint64_t object,
1002 dnode_handle_t *dnh)
1004 dnode_t *dn;
1006 dn = dnode_create(os, dnp, NULL, object, dnh);
1007 zrl_init(&dnh->dnh_zrlock);
1008 DNODE_VERIFY(dn);
1011 static void
1012 dnode_buf_evict_async(void *dbu)
1014 dnode_children_t *children_dnodes = dbu;
1015 int i;
1017 for (i = 0; i < children_dnodes->dnc_count; i++) {
1018 dnode_handle_t *dnh = &children_dnodes->dnc_children[i];
1019 dnode_t *dn;
1022 * The dnode handle lock guards against the dnode moving to
1023 * another valid address, so there is no need here to guard
1024 * against changes to or from NULL.
1026 if (dnh->dnh_dnode == NULL) {
1027 zrl_destroy(&dnh->dnh_zrlock);
1028 continue;
1031 zrl_add(&dnh->dnh_zrlock);
1032 dn = dnh->dnh_dnode;
1034 * If there are holds on this dnode, then there should
1035 * be holds on the dnode's containing dbuf as well; thus
1036 * it wouldn't be eligible for eviction and this function
1037 * would not have been called.
1039 ASSERT(refcount_is_zero(&dn->dn_holds));
1040 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
1042 dnode_destroy(dn); /* implicit zrl_remove() */
1043 zrl_destroy(&dnh->dnh_zrlock);
1044 dnh->dnh_dnode = NULL;
1046 kmem_free(children_dnodes, sizeof (dnode_children_t) +
1047 children_dnodes->dnc_count * sizeof (dnode_handle_t));
1051 * errors:
1052 * EINVAL - invalid object number.
1053 * EIO - i/o error.
1054 * succeeds even for free dnodes.
1057 dnode_hold_impl(objset_t *os, uint64_t object, int flag,
1058 void *tag, dnode_t **dnp)
1060 int epb, idx, err;
1061 int drop_struct_lock = FALSE;
1062 int type;
1063 uint64_t blk;
1064 dnode_t *mdn, *dn;
1065 dmu_buf_impl_t *db;
1066 dnode_children_t *children_dnodes;
1067 dnode_handle_t *dnh;
1070 * If you are holding the spa config lock as writer, you shouldn't
1071 * be asking the DMU to do *anything* unless it's the root pool
1072 * which may require us to read from the root filesystem while
1073 * holding some (not all) of the locks as writer.
1075 ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0 ||
1076 (spa_is_root(os->os_spa) &&
1077 spa_config_held(os->os_spa, SCL_STATE, RW_WRITER)));
1079 ASSERT((flag & DNODE_MUST_BE_ALLOCATED) || (flag & DNODE_MUST_BE_FREE));
1081 if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT) {
1082 dn = (object == DMU_USERUSED_OBJECT) ?
1083 DMU_USERUSED_DNODE(os) : DMU_GROUPUSED_DNODE(os);
1084 if (dn == NULL)
1085 return (SET_ERROR(ENOENT));
1086 type = dn->dn_type;
1087 if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE)
1088 return (SET_ERROR(ENOENT));
1089 if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)
1090 return (SET_ERROR(EEXIST));
1091 DNODE_VERIFY(dn);
1092 (void) refcount_add(&dn->dn_holds, tag);
1093 *dnp = dn;
1094 return (0);
1097 if (object == 0 || object >= DN_MAX_OBJECT)
1098 return (SET_ERROR(EINVAL));
1100 mdn = DMU_META_DNODE(os);
1101 ASSERT(mdn->dn_object == DMU_META_DNODE_OBJECT);
1103 DNODE_VERIFY(mdn);
1105 if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
1106 rw_enter(&mdn->dn_struct_rwlock, RW_READER);
1107 drop_struct_lock = TRUE;
1110 blk = dbuf_whichblock(mdn, 0, object * sizeof (dnode_phys_t));
1112 db = dbuf_hold(mdn, blk, FTAG);
1113 if (drop_struct_lock)
1114 rw_exit(&mdn->dn_struct_rwlock);
1115 if (db == NULL)
1116 return (SET_ERROR(EIO));
1117 err = dbuf_read(db, NULL, DB_RF_CANFAIL);
1118 if (err) {
1119 dbuf_rele(db, FTAG);
1120 return (err);
1123 ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
1124 epb = db->db.db_size >> DNODE_SHIFT;
1126 idx = object & (epb-1);
1128 ASSERT(DB_DNODE(db)->dn_type == DMU_OT_DNODE);
1129 children_dnodes = dmu_buf_get_user(&db->db);
1130 if (children_dnodes == NULL) {
1131 int i;
1132 dnode_children_t *winner;
1133 children_dnodes = kmem_zalloc(sizeof (dnode_children_t) +
1134 epb * sizeof (dnode_handle_t), KM_SLEEP);
1135 children_dnodes->dnc_count = epb;
1136 dnh = &children_dnodes->dnc_children[0];
1137 for (i = 0; i < epb; i++) {
1138 zrl_init(&dnh[i].dnh_zrlock);
1140 dmu_buf_init_user(&children_dnodes->dnc_dbu, NULL,
1141 dnode_buf_evict_async, NULL);
1142 winner = dmu_buf_set_user(&db->db, &children_dnodes->dnc_dbu);
1143 if (winner != NULL) {
1145 for (i = 0; i < epb; i++) {
1146 zrl_destroy(&dnh[i].dnh_zrlock);
1149 kmem_free(children_dnodes, sizeof (dnode_children_t) +
1150 epb * sizeof (dnode_handle_t));
1151 children_dnodes = winner;
1154 ASSERT(children_dnodes->dnc_count == epb);
1156 dnh = &children_dnodes->dnc_children[idx];
1157 zrl_add(&dnh->dnh_zrlock);
1158 dn = dnh->dnh_dnode;
1159 if (dn == NULL) {
1160 dnode_phys_t *phys = (dnode_phys_t *)db->db.db_data+idx;
1162 dn = dnode_create(os, phys, db, object, dnh);
1165 mutex_enter(&dn->dn_mtx);
1166 type = dn->dn_type;
1167 if (dn->dn_free_txg ||
1168 ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
1169 ((flag & DNODE_MUST_BE_FREE) &&
1170 (type != DMU_OT_NONE || !refcount_is_zero(&dn->dn_holds)))) {
1171 mutex_exit(&dn->dn_mtx);
1172 zrl_remove(&dnh->dnh_zrlock);
1173 dbuf_rele(db, FTAG);
1174 return ((flag & DNODE_MUST_BE_ALLOCATED) ? ENOENT : EEXIST);
1176 if (refcount_add(&dn->dn_holds, tag) == 1)
1177 dbuf_add_ref(db, dnh);
1178 mutex_exit(&dn->dn_mtx);
1180 /* Now we can rely on the hold to prevent the dnode from moving. */
1181 zrl_remove(&dnh->dnh_zrlock);
1183 DNODE_VERIFY(dn);
1184 ASSERT3P(dn->dn_dbuf, ==, db);
1185 ASSERT3U(dn->dn_object, ==, object);
1186 dbuf_rele(db, FTAG);
1188 *dnp = dn;
1189 return (0);
1193 * Return held dnode if the object is allocated, NULL if not.
1196 dnode_hold(objset_t *os, uint64_t object, void *tag, dnode_t **dnp)
1198 return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
1202 * Can only add a reference if there is already at least one
1203 * reference on the dnode. Returns FALSE if unable to add a
1204 * new reference.
1206 boolean_t
1207 dnode_add_ref(dnode_t *dn, void *tag)
1209 mutex_enter(&dn->dn_mtx);
1210 if (refcount_is_zero(&dn->dn_holds)) {
1211 mutex_exit(&dn->dn_mtx);
1212 return (FALSE);
1214 VERIFY(1 < refcount_add(&dn->dn_holds, tag));
1215 mutex_exit(&dn->dn_mtx);
1216 return (TRUE);
1219 void
1220 dnode_rele(dnode_t *dn, void *tag)
1222 mutex_enter(&dn->dn_mtx);
1223 dnode_rele_and_unlock(dn, tag);
1226 void
1227 dnode_rele_and_unlock(dnode_t *dn, void *tag)
1229 uint64_t refs;
1230 /* Get while the hold prevents the dnode from moving. */
1231 dmu_buf_impl_t *db = dn->dn_dbuf;
1232 dnode_handle_t *dnh = dn->dn_handle;
1234 refs = refcount_remove(&dn->dn_holds, tag);
1235 mutex_exit(&dn->dn_mtx);
1238 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1239 * indirectly by dbuf_rele() while relying on the dnode handle to
1240 * prevent the dnode from moving, since releasing the last hold could
1241 * result in the dnode's parent dbuf evicting its dnode handles. For
1242 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1243 * other direct or indirect hold on the dnode must first drop the dnode
1244 * handle.
1246 ASSERT(refs > 0 || dnh->dnh_zrlock.zr_owner != curthread);
1248 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1249 if (refs == 0 && db != NULL) {
1251 * Another thread could add a hold to the dnode handle in
1252 * dnode_hold_impl() while holding the parent dbuf. Since the
1253 * hold on the parent dbuf prevents the handle from being
1254 * destroyed, the hold on the handle is OK. We can't yet assert
1255 * that the handle has zero references, but that will be
1256 * asserted anyway when the handle gets destroyed.
1258 dbuf_rele(db, dnh);
1262 void
1263 dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
1265 objset_t *os = dn->dn_objset;
1266 uint64_t txg = tx->tx_txg;
1268 if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
1269 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1270 return;
1273 DNODE_VERIFY(dn);
1275 #ifdef ZFS_DEBUG
1276 mutex_enter(&dn->dn_mtx);
1277 ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
1278 ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg);
1279 mutex_exit(&dn->dn_mtx);
1280 #endif
1283 * Determine old uid/gid when necessary
1285 dmu_objset_userquota_get_ids(dn, B_TRUE, tx);
1287 multilist_t *dirtylist = os->os_dirty_dnodes[txg & TXG_MASK];
1288 multilist_sublist_t *mls = multilist_sublist_lock_obj(dirtylist, dn);
1291 * If we are already marked dirty, we're done.
1293 if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
1294 multilist_sublist_unlock(mls);
1295 return;
1298 ASSERT(!refcount_is_zero(&dn->dn_holds) ||
1299 !avl_is_empty(&dn->dn_dbufs));
1300 ASSERT(dn->dn_datablksz != 0);
1301 ASSERT0(dn->dn_next_bonuslen[txg&TXG_MASK]);
1302 ASSERT0(dn->dn_next_blksz[txg&TXG_MASK]);
1303 ASSERT0(dn->dn_next_bonustype[txg&TXG_MASK]);
1305 dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
1306 dn->dn_object, txg);
1308 multilist_sublist_insert_head(mls, dn);
1310 multilist_sublist_unlock(mls);
1313 * The dnode maintains a hold on its containing dbuf as
1314 * long as there are holds on it. Each instantiated child
1315 * dbuf maintains a hold on the dnode. When the last child
1316 * drops its hold, the dnode will drop its hold on the
1317 * containing dbuf. We add a "dirty hold" here so that the
1318 * dnode will hang around after we finish processing its
1319 * children.
1321 VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
1323 (void) dbuf_dirty(dn->dn_dbuf, tx);
1325 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1328 void
1329 dnode_free(dnode_t *dn, dmu_tx_t *tx)
1331 mutex_enter(&dn->dn_mtx);
1332 if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
1333 mutex_exit(&dn->dn_mtx);
1334 return;
1336 dn->dn_free_txg = tx->tx_txg;
1337 mutex_exit(&dn->dn_mtx);
1339 dnode_setdirty(dn, tx);
1343 * Try to change the block size for the indicated dnode. This can only
1344 * succeed if there are no blocks allocated or dirty beyond first block
1347 dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
1349 dmu_buf_impl_t *db;
1350 int err;
1352 ASSERT3U(size, <=, spa_maxblocksize(dmu_objset_spa(dn->dn_objset)));
1353 if (size == 0)
1354 size = SPA_MINBLOCKSIZE;
1355 else
1356 size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
1358 if (ibs == dn->dn_indblkshift)
1359 ibs = 0;
1361 if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
1362 return (0);
1364 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1366 /* Check for any allocated blocks beyond the first */
1367 if (dn->dn_maxblkid != 0)
1368 goto fail;
1370 mutex_enter(&dn->dn_dbufs_mtx);
1371 for (db = avl_first(&dn->dn_dbufs); db != NULL;
1372 db = AVL_NEXT(&dn->dn_dbufs, db)) {
1373 if (db->db_blkid != 0 && db->db_blkid != DMU_BONUS_BLKID &&
1374 db->db_blkid != DMU_SPILL_BLKID) {
1375 mutex_exit(&dn->dn_dbufs_mtx);
1376 goto fail;
1379 mutex_exit(&dn->dn_dbufs_mtx);
1381 if (ibs && dn->dn_nlevels != 1)
1382 goto fail;
1384 /* resize the old block */
1385 err = dbuf_hold_impl(dn, 0, 0, TRUE, FALSE, FTAG, &db);
1386 if (err == 0)
1387 dbuf_new_size(db, size, tx);
1388 else if (err != ENOENT)
1389 goto fail;
1391 dnode_setdblksz(dn, size);
1392 dnode_setdirty(dn, tx);
1393 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
1394 if (ibs) {
1395 dn->dn_indblkshift = ibs;
1396 dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
1398 /* rele after we have fixed the blocksize in the dnode */
1399 if (db)
1400 dbuf_rele(db, FTAG);
1402 rw_exit(&dn->dn_struct_rwlock);
1403 return (0);
1405 fail:
1406 rw_exit(&dn->dn_struct_rwlock);
1407 return (SET_ERROR(ENOTSUP));
1410 /* read-holding callers must not rely on the lock being continuously held */
1411 void
1412 dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read)
1414 uint64_t txgoff = tx->tx_txg & TXG_MASK;
1415 int epbs, new_nlevels;
1416 uint64_t sz;
1418 ASSERT(blkid != DMU_BONUS_BLKID);
1420 ASSERT(have_read ?
1421 RW_READ_HELD(&dn->dn_struct_rwlock) :
1422 RW_WRITE_HELD(&dn->dn_struct_rwlock));
1425 * if we have a read-lock, check to see if we need to do any work
1426 * before upgrading to a write-lock.
1428 if (have_read) {
1429 if (blkid <= dn->dn_maxblkid)
1430 return;
1432 if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
1433 rw_exit(&dn->dn_struct_rwlock);
1434 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1438 if (blkid <= dn->dn_maxblkid)
1439 goto out;
1441 dn->dn_maxblkid = blkid;
1444 * Compute the number of levels necessary to support the new maxblkid.
1446 new_nlevels = 1;
1447 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1448 for (sz = dn->dn_nblkptr;
1449 sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
1450 new_nlevels++;
1452 if (new_nlevels > dn->dn_nlevels) {
1453 int old_nlevels = dn->dn_nlevels;
1454 dmu_buf_impl_t *db;
1455 list_t *list;
1456 dbuf_dirty_record_t *new, *dr, *dr_next;
1458 dn->dn_nlevels = new_nlevels;
1460 ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
1461 dn->dn_next_nlevels[txgoff] = new_nlevels;
1463 /* dirty the left indirects */
1464 db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
1465 ASSERT(db != NULL);
1466 new = dbuf_dirty(db, tx);
1467 dbuf_rele(db, FTAG);
1469 /* transfer the dirty records to the new indirect */
1470 mutex_enter(&dn->dn_mtx);
1471 mutex_enter(&new->dt.di.dr_mtx);
1472 list = &dn->dn_dirty_records[txgoff];
1473 for (dr = list_head(list); dr; dr = dr_next) {
1474 dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
1475 if (dr->dr_dbuf->db_level != new_nlevels-1 &&
1476 dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
1477 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
1478 ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
1479 list_remove(&dn->dn_dirty_records[txgoff], dr);
1480 list_insert_tail(&new->dt.di.dr_children, dr);
1481 dr->dr_parent = new;
1484 mutex_exit(&new->dt.di.dr_mtx);
1485 mutex_exit(&dn->dn_mtx);
1488 out:
1489 if (have_read)
1490 rw_downgrade(&dn->dn_struct_rwlock);
1493 static void
1494 dnode_dirty_l1(dnode_t *dn, uint64_t l1blkid, dmu_tx_t *tx)
1496 dmu_buf_impl_t *db = dbuf_hold_level(dn, 1, l1blkid, FTAG);
1497 if (db != NULL) {
1498 dmu_buf_will_dirty(&db->db, tx);
1499 dbuf_rele(db, FTAG);
1503 void
1504 dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
1506 dmu_buf_impl_t *db;
1507 uint64_t blkoff, blkid, nblks;
1508 int blksz, blkshift, head, tail;
1509 int trunc = FALSE;
1510 int epbs;
1512 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1513 blksz = dn->dn_datablksz;
1514 blkshift = dn->dn_datablkshift;
1515 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1517 if (len == DMU_OBJECT_END) {
1518 len = UINT64_MAX - off;
1519 trunc = TRUE;
1523 * First, block align the region to free:
1525 if (ISP2(blksz)) {
1526 head = P2NPHASE(off, blksz);
1527 blkoff = P2PHASE(off, blksz);
1528 if ((off >> blkshift) > dn->dn_maxblkid)
1529 goto out;
1530 } else {
1531 ASSERT(dn->dn_maxblkid == 0);
1532 if (off == 0 && len >= blksz) {
1534 * Freeing the whole block; fast-track this request.
1535 * Note that we won't dirty any indirect blocks,
1536 * which is fine because we will be freeing the entire
1537 * file and thus all indirect blocks will be freed
1538 * by free_children().
1540 blkid = 0;
1541 nblks = 1;
1542 goto done;
1543 } else if (off >= blksz) {
1544 /* Freeing past end-of-data */
1545 goto out;
1546 } else {
1547 /* Freeing part of the block. */
1548 head = blksz - off;
1549 ASSERT3U(head, >, 0);
1551 blkoff = off;
1553 /* zero out any partial block data at the start of the range */
1554 if (head) {
1555 ASSERT3U(blkoff + head, ==, blksz);
1556 if (len < head)
1557 head = len;
1558 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, 0, off),
1559 TRUE, FALSE, FTAG, &db) == 0) {
1560 caddr_t data;
1562 /* don't dirty if it isn't on disk and isn't dirty */
1563 if (db->db_last_dirty ||
1564 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1565 rw_exit(&dn->dn_struct_rwlock);
1566 dmu_buf_will_dirty(&db->db, tx);
1567 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1568 data = db->db.db_data;
1569 bzero(data + blkoff, head);
1571 dbuf_rele(db, FTAG);
1573 off += head;
1574 len -= head;
1577 /* If the range was less than one block, we're done */
1578 if (len == 0)
1579 goto out;
1581 /* If the remaining range is past end of file, we're done */
1582 if ((off >> blkshift) > dn->dn_maxblkid)
1583 goto out;
1585 ASSERT(ISP2(blksz));
1586 if (trunc)
1587 tail = 0;
1588 else
1589 tail = P2PHASE(len, blksz);
1591 ASSERT0(P2PHASE(off, blksz));
1592 /* zero out any partial block data at the end of the range */
1593 if (tail) {
1594 if (len < tail)
1595 tail = len;
1596 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, 0, off+len),
1597 TRUE, FALSE, FTAG, &db) == 0) {
1598 /* don't dirty if not on disk and not dirty */
1599 if (db->db_last_dirty ||
1600 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1601 rw_exit(&dn->dn_struct_rwlock);
1602 dmu_buf_will_dirty(&db->db, tx);
1603 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1604 bzero(db->db.db_data, tail);
1606 dbuf_rele(db, FTAG);
1608 len -= tail;
1611 /* If the range did not include a full block, we are done */
1612 if (len == 0)
1613 goto out;
1615 ASSERT(IS_P2ALIGNED(off, blksz));
1616 ASSERT(trunc || IS_P2ALIGNED(len, blksz));
1617 blkid = off >> blkshift;
1618 nblks = len >> blkshift;
1619 if (trunc)
1620 nblks += 1;
1623 * Dirty all the indirect blocks in this range. Note that only
1624 * the first and last indirect blocks can actually be written
1625 * (if they were partially freed) -- they must be dirtied, even if
1626 * they do not exist on disk yet. The interior blocks will
1627 * be freed by free_children(), so they will not actually be written.
1628 * Even though these interior blocks will not be written, we
1629 * dirty them for two reasons:
1631 * - It ensures that the indirect blocks remain in memory until
1632 * syncing context. (They have already been prefetched by
1633 * dmu_tx_hold_free(), so we don't have to worry about reading
1634 * them serially here.)
1636 * - The dirty space accounting will put pressure on the txg sync
1637 * mechanism to begin syncing, and to delay transactions if there
1638 * is a large amount of freeing. Even though these indirect
1639 * blocks will not be written, we could need to write the same
1640 * amount of space if we copy the freed BPs into deadlists.
1642 if (dn->dn_nlevels > 1) {
1643 uint64_t first, last;
1645 first = blkid >> epbs;
1646 dnode_dirty_l1(dn, first, tx);
1647 if (trunc)
1648 last = dn->dn_maxblkid >> epbs;
1649 else
1650 last = (blkid + nblks - 1) >> epbs;
1651 if (last != first)
1652 dnode_dirty_l1(dn, last, tx);
1654 int shift = dn->dn_datablkshift + dn->dn_indblkshift -
1655 SPA_BLKPTRSHIFT;
1656 for (uint64_t i = first + 1; i < last; i++) {
1658 * Set i to the blockid of the next non-hole
1659 * level-1 indirect block at or after i. Note
1660 * that dnode_next_offset() operates in terms of
1661 * level-0-equivalent bytes.
1663 uint64_t ibyte = i << shift;
1664 int err = dnode_next_offset(dn, DNODE_FIND_HAVELOCK,
1665 &ibyte, 2, 1, 0);
1666 i = ibyte >> shift;
1667 if (i >= last)
1668 break;
1671 * Normally we should not see an error, either
1672 * from dnode_next_offset() or dbuf_hold_level()
1673 * (except for ESRCH from dnode_next_offset).
1674 * If there is an i/o error, then when we read
1675 * this block in syncing context, it will use
1676 * ZIO_FLAG_MUSTSUCCEED, and thus hang/panic according
1677 * to the "failmode" property. dnode_next_offset()
1678 * doesn't have a flag to indicate MUSTSUCCEED.
1680 if (err != 0)
1681 break;
1683 dnode_dirty_l1(dn, i, tx);
1687 done:
1689 * Add this range to the dnode range list.
1690 * We will finish up this free operation in the syncing phase.
1692 mutex_enter(&dn->dn_mtx);
1693 int txgoff = tx->tx_txg & TXG_MASK;
1694 if (dn->dn_free_ranges[txgoff] == NULL) {
1695 dn->dn_free_ranges[txgoff] = range_tree_create(NULL, NULL);
1697 range_tree_clear(dn->dn_free_ranges[txgoff], blkid, nblks);
1698 range_tree_add(dn->dn_free_ranges[txgoff], blkid, nblks);
1699 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1700 blkid, nblks, tx->tx_txg);
1701 mutex_exit(&dn->dn_mtx);
1703 dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
1704 dnode_setdirty(dn, tx);
1705 out:
1707 rw_exit(&dn->dn_struct_rwlock);
1710 static boolean_t
1711 dnode_spill_freed(dnode_t *dn)
1713 int i;
1715 mutex_enter(&dn->dn_mtx);
1716 for (i = 0; i < TXG_SIZE; i++) {
1717 if (dn->dn_rm_spillblk[i] == DN_KILL_SPILLBLK)
1718 break;
1720 mutex_exit(&dn->dn_mtx);
1721 return (i < TXG_SIZE);
1724 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1725 uint64_t
1726 dnode_block_freed(dnode_t *dn, uint64_t blkid)
1728 void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1729 int i;
1731 if (blkid == DMU_BONUS_BLKID)
1732 return (FALSE);
1735 * If we're in the process of opening the pool, dp will not be
1736 * set yet, but there shouldn't be anything dirty.
1738 if (dp == NULL)
1739 return (FALSE);
1741 if (dn->dn_free_txg)
1742 return (TRUE);
1744 if (blkid == DMU_SPILL_BLKID)
1745 return (dnode_spill_freed(dn));
1747 mutex_enter(&dn->dn_mtx);
1748 for (i = 0; i < TXG_SIZE; i++) {
1749 if (dn->dn_free_ranges[i] != NULL &&
1750 range_tree_contains(dn->dn_free_ranges[i], blkid, 1))
1751 break;
1753 mutex_exit(&dn->dn_mtx);
1754 return (i < TXG_SIZE);
1757 /* call from syncing context when we actually write/free space for this dnode */
1758 void
1759 dnode_diduse_space(dnode_t *dn, int64_t delta)
1761 uint64_t space;
1762 dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
1763 dn, dn->dn_phys,
1764 (u_longlong_t)dn->dn_phys->dn_used,
1765 (longlong_t)delta);
1767 mutex_enter(&dn->dn_mtx);
1768 space = DN_USED_BYTES(dn->dn_phys);
1769 if (delta > 0) {
1770 ASSERT3U(space + delta, >=, space); /* no overflow */
1771 } else {
1772 ASSERT3U(space, >=, -delta); /* no underflow */
1774 space += delta;
1775 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
1776 ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
1777 ASSERT0(P2PHASE(space, 1<<DEV_BSHIFT));
1778 dn->dn_phys->dn_used = space >> DEV_BSHIFT;
1779 } else {
1780 dn->dn_phys->dn_used = space;
1781 dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
1783 mutex_exit(&dn->dn_mtx);
1787 * Scans a block at the indicated "level" looking for a hole or data,
1788 * depending on 'flags'.
1790 * If level > 0, then we are scanning an indirect block looking at its
1791 * pointers. If level == 0, then we are looking at a block of dnodes.
1793 * If we don't find what we are looking for in the block, we return ESRCH.
1794 * Otherwise, return with *offset pointing to the beginning (if searching
1795 * forwards) or end (if searching backwards) of the range covered by the
1796 * block pointer we matched on (or dnode).
1798 * The basic search algorithm used below by dnode_next_offset() is to
1799 * use this function to search up the block tree (widen the search) until
1800 * we find something (i.e., we don't return ESRCH) and then search back
1801 * down the tree (narrow the search) until we reach our original search
1802 * level.
1804 static int
1805 dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
1806 int lvl, uint64_t blkfill, uint64_t txg)
1808 dmu_buf_impl_t *db = NULL;
1809 void *data = NULL;
1810 uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1811 uint64_t epb = 1ULL << epbs;
1812 uint64_t minfill, maxfill;
1813 boolean_t hole;
1814 int i, inc, error, span;
1816 dprintf("probing object %llu offset %llx level %d of %u\n",
1817 dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
1819 hole = ((flags & DNODE_FIND_HOLE) != 0);
1820 inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1;
1821 ASSERT(txg == 0 || !hole);
1823 if (lvl == dn->dn_phys->dn_nlevels) {
1824 error = 0;
1825 epb = dn->dn_phys->dn_nblkptr;
1826 data = dn->dn_phys->dn_blkptr;
1827 } else {
1828 uint64_t blkid = dbuf_whichblock(dn, lvl, *offset);
1829 error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FALSE, FTAG, &db);
1830 if (error) {
1831 if (error != ENOENT)
1832 return (error);
1833 if (hole)
1834 return (0);
1836 * This can only happen when we are searching up
1837 * the block tree for data. We don't really need to
1838 * adjust the offset, as we will just end up looking
1839 * at the pointer to this block in its parent, and its
1840 * going to be unallocated, so we will skip over it.
1842 return (SET_ERROR(ESRCH));
1844 error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT);
1845 if (error) {
1846 dbuf_rele(db, FTAG);
1847 return (error);
1849 data = db->db.db_data;
1853 if (db != NULL && txg != 0 && (db->db_blkptr == NULL ||
1854 db->db_blkptr->blk_birth <= txg ||
1855 BP_IS_HOLE(db->db_blkptr))) {
1857 * This can only happen when we are searching up the tree
1858 * and these conditions mean that we need to keep climbing.
1860 error = SET_ERROR(ESRCH);
1861 } else if (lvl == 0) {
1862 dnode_phys_t *dnp = data;
1863 span = DNODE_SHIFT;
1864 ASSERT(dn->dn_type == DMU_OT_DNODE);
1866 for (i = (*offset >> span) & (blkfill - 1);
1867 i >= 0 && i < blkfill; i += inc) {
1868 if ((dnp[i].dn_type == DMU_OT_NONE) == hole)
1869 break;
1870 *offset += (1ULL << span) * inc;
1872 if (i < 0 || i == blkfill)
1873 error = SET_ERROR(ESRCH);
1874 } else {
1875 blkptr_t *bp = data;
1876 uint64_t start = *offset;
1877 span = (lvl - 1) * epbs + dn->dn_datablkshift;
1878 minfill = 0;
1879 maxfill = blkfill << ((lvl - 1) * epbs);
1881 if (hole)
1882 maxfill--;
1883 else
1884 minfill++;
1886 *offset = *offset >> span;
1887 for (i = BF64_GET(*offset, 0, epbs);
1888 i >= 0 && i < epb; i += inc) {
1889 if (BP_GET_FILL(&bp[i]) >= minfill &&
1890 BP_GET_FILL(&bp[i]) <= maxfill &&
1891 (hole || bp[i].blk_birth > txg))
1892 break;
1893 if (inc > 0 || *offset > 0)
1894 *offset += inc;
1896 *offset = *offset << span;
1897 if (inc < 0) {
1898 /* traversing backwards; position offset at the end */
1899 ASSERT3U(*offset, <=, start);
1900 *offset = MIN(*offset + (1ULL << span) - 1, start);
1901 } else if (*offset < start) {
1902 *offset = start;
1904 if (i < 0 || i >= epb)
1905 error = SET_ERROR(ESRCH);
1908 if (db)
1909 dbuf_rele(db, FTAG);
1911 return (error);
1915 * Find the next hole, data, or sparse region at or after *offset.
1916 * The value 'blkfill' tells us how many items we expect to find
1917 * in an L0 data block; this value is 1 for normal objects,
1918 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1919 * DNODES_PER_BLOCK when searching for sparse regions thereof.
1921 * Examples:
1923 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
1924 * Finds the next/previous hole/data in a file.
1925 * Used in dmu_offset_next().
1927 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
1928 * Finds the next free/allocated dnode an objset's meta-dnode.
1929 * Only finds objects that have new contents since txg (ie.
1930 * bonus buffer changes and content removal are ignored).
1931 * Used in dmu_object_next().
1933 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
1934 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
1935 * Used in dmu_object_alloc().
1938 dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
1939 int minlvl, uint64_t blkfill, uint64_t txg)
1941 uint64_t initial_offset = *offset;
1942 int lvl, maxlvl;
1943 int error = 0;
1945 if (!(flags & DNODE_FIND_HAVELOCK))
1946 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1948 if (dn->dn_phys->dn_nlevels == 0) {
1949 error = SET_ERROR(ESRCH);
1950 goto out;
1953 if (dn->dn_datablkshift == 0) {
1954 if (*offset < dn->dn_datablksz) {
1955 if (flags & DNODE_FIND_HOLE)
1956 *offset = dn->dn_datablksz;
1957 } else {
1958 error = SET_ERROR(ESRCH);
1960 goto out;
1963 maxlvl = dn->dn_phys->dn_nlevels;
1965 for (lvl = minlvl; lvl <= maxlvl; lvl++) {
1966 error = dnode_next_offset_level(dn,
1967 flags, offset, lvl, blkfill, txg);
1968 if (error != ESRCH)
1969 break;
1972 while (error == 0 && --lvl >= minlvl) {
1973 error = dnode_next_offset_level(dn,
1974 flags, offset, lvl, blkfill, txg);
1978 * There's always a "virtual hole" at the end of the object, even
1979 * if all BP's which physically exist are non-holes.
1981 if ((flags & DNODE_FIND_HOLE) && error == ESRCH && txg == 0 &&
1982 minlvl == 1 && blkfill == 1 && !(flags & DNODE_FIND_BACKWARDS)) {
1983 error = 0;
1986 if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
1987 initial_offset < *offset : initial_offset > *offset))
1988 error = SET_ERROR(ESRCH);
1989 out:
1990 if (!(flags & DNODE_FIND_HAVELOCK))
1991 rw_exit(&dn->dn_struct_rwlock);
1993 return (error);