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