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