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