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