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