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