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