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