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