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