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