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