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