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