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