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