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