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