1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/zfs_context.h> 29 #include <sys/dbuf.h> 30 #include <sys/dnode.h> 31 #include <sys/dmu.h> 32 #include <sys/dmu_impl.h> 33 #include <sys/dmu_tx.h> 34 #include <sys/dmu_objset.h> 35 #include <sys/dsl_dir.h> 36 #include <sys/dsl_dataset.h> 37 #include <sys/spa.h> 38 #include <sys/zio.h> 39 #include <sys/dmu_zfetch.h> 40 41 static int free_range_compar(const void *node1, const void *node2); 42 43 static kmem_cache_t *dnode_cache; 44 45 static dnode_phys_t dnode_phys_zero; 46 47 int zfs_default_bs = SPA_MINBLOCKSHIFT; 48 int zfs_default_ibs = DN_MAX_INDBLKSHIFT; 49 50 /* ARGSUSED */ 51 static int 52 dnode_cons(void *arg, void *unused, int kmflag) 53 { 54 int i; 55 dnode_t *dn = arg; 56 bzero(dn, sizeof (dnode_t)); 57 58 rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL); 59 mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL); 60 mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL); 61 refcount_create(&dn->dn_holds); 62 refcount_create(&dn->dn_tx_holds); 63 64 for (i = 0; i < TXG_SIZE; i++) { 65 avl_create(&dn->dn_ranges[i], free_range_compar, 66 sizeof (free_range_t), 67 offsetof(struct free_range, fr_node)); 68 list_create(&dn->dn_dirty_records[i], 69 sizeof (dbuf_dirty_record_t), 70 offsetof(dbuf_dirty_record_t, dr_dirty_node)); 71 } 72 73 list_create(&dn->dn_dbufs, sizeof (dmu_buf_impl_t), 74 offsetof(dmu_buf_impl_t, db_link)); 75 76 return (0); 77 } 78 79 /* ARGSUSED */ 80 static void 81 dnode_dest(void *arg, void *unused) 82 { 83 int i; 84 dnode_t *dn = arg; 85 86 rw_destroy(&dn->dn_struct_rwlock); 87 mutex_destroy(&dn->dn_mtx); 88 mutex_destroy(&dn->dn_dbufs_mtx); 89 refcount_destroy(&dn->dn_holds); 90 refcount_destroy(&dn->dn_tx_holds); 91 92 for (i = 0; i < TXG_SIZE; i++) { 93 avl_destroy(&dn->dn_ranges[i]); 94 list_destroy(&dn->dn_dirty_records[i]); 95 } 96 97 list_destroy(&dn->dn_dbufs); 98 } 99 100 void 101 dnode_init(void) 102 { 103 dnode_cache = kmem_cache_create("dnode_t", 104 sizeof (dnode_t), 105 0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0); 106 } 107 108 void 109 dnode_fini(void) 110 { 111 kmem_cache_destroy(dnode_cache); 112 } 113 114 115 #ifdef ZFS_DEBUG 116 void 117 dnode_verify(dnode_t *dn) 118 { 119 int drop_struct_lock = FALSE; 120 121 ASSERT(dn->dn_phys); 122 ASSERT(dn->dn_objset); 123 124 ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES); 125 126 if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY)) 127 return; 128 129 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) { 130 rw_enter(&dn->dn_struct_rwlock, RW_READER); 131 drop_struct_lock = TRUE; 132 } 133 if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) { 134 int i; 135 ASSERT3U(dn->dn_indblkshift, >=, 0); 136 ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT); 137 if (dn->dn_datablkshift) { 138 ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT); 139 ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT); 140 ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz); 141 } 142 ASSERT3U(dn->dn_nlevels, <=, 30); 143 ASSERT3U(dn->dn_type, <=, DMU_OT_NUMTYPES); 144 ASSERT3U(dn->dn_nblkptr, >=, 1); 145 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR); 146 ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN); 147 ASSERT3U(dn->dn_datablksz, ==, 148 dn->dn_datablkszsec << SPA_MINBLOCKSHIFT); 149 ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0); 150 ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) + 151 dn->dn_bonuslen, <=, DN_MAX_BONUSLEN); 152 for (i = 0; i < TXG_SIZE; i++) { 153 ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels); 154 } 155 } 156 if (dn->dn_phys->dn_type != DMU_OT_NONE) 157 ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels); 158 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT || dn->dn_dbuf != NULL); 159 if (dn->dn_dbuf != NULL) { 160 ASSERT3P(dn->dn_phys, ==, 161 (dnode_phys_t *)dn->dn_dbuf->db.db_data + 162 (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT))); 163 } 164 if (drop_struct_lock) 165 rw_exit(&dn->dn_struct_rwlock); 166 } 167 #endif 168 169 void 170 dnode_byteswap(dnode_phys_t *dnp) 171 { 172 uint64_t *buf64 = (void*)&dnp->dn_blkptr; 173 int i; 174 175 if (dnp->dn_type == DMU_OT_NONE) { 176 bzero(dnp, sizeof (dnode_phys_t)); 177 return; 178 } 179 180 dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec); 181 dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen); 182 dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid); 183 dnp->dn_used = BSWAP_64(dnp->dn_used); 184 185 /* 186 * dn_nblkptr is only one byte, so it's OK to read it in either 187 * byte order. We can't read dn_bouslen. 188 */ 189 ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT); 190 ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR); 191 for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++) 192 buf64[i] = BSWAP_64(buf64[i]); 193 194 /* 195 * OK to check dn_bonuslen for zero, because it won't matter if 196 * we have the wrong byte order. This is necessary because the 197 * dnode dnode is smaller than a regular dnode. 198 */ 199 if (dnp->dn_bonuslen != 0) { 200 /* 201 * Note that the bonus length calculated here may be 202 * longer than the actual bonus buffer. This is because 203 * we always put the bonus buffer after the last block 204 * pointer (instead of packing it against the end of the 205 * dnode buffer). 206 */ 207 int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t); 208 size_t len = DN_MAX_BONUSLEN - off; 209 ASSERT3U(dnp->dn_bonustype, <, DMU_OT_NUMTYPES); 210 dmu_ot[dnp->dn_bonustype].ot_byteswap(dnp->dn_bonus + off, len); 211 } 212 } 213 214 void 215 dnode_buf_byteswap(void *vbuf, size_t size) 216 { 217 dnode_phys_t *buf = vbuf; 218 int i; 219 220 ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT)); 221 ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0); 222 223 size >>= DNODE_SHIFT; 224 for (i = 0; i < size; i++) { 225 dnode_byteswap(buf); 226 buf++; 227 } 228 } 229 230 static int 231 free_range_compar(const void *node1, const void *node2) 232 { 233 const free_range_t *rp1 = node1; 234 const free_range_t *rp2 = node2; 235 236 if (rp1->fr_blkid < rp2->fr_blkid) 237 return (-1); 238 else if (rp1->fr_blkid > rp2->fr_blkid) 239 return (1); 240 else return (0); 241 } 242 243 static void 244 dnode_setdblksz(dnode_t *dn, int size) 245 { 246 ASSERT3U(P2PHASE(size, SPA_MINBLOCKSIZE), ==, 0); 247 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE); 248 ASSERT3U(size, >=, SPA_MINBLOCKSIZE); 249 ASSERT3U(size >> SPA_MINBLOCKSHIFT, <, 250 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8)); 251 dn->dn_datablksz = size; 252 dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT; 253 dn->dn_datablkshift = ISP2(size) ? highbit(size - 1) : 0; 254 } 255 256 static dnode_t * 257 dnode_create(objset_impl_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db, 258 uint64_t object) 259 { 260 dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP); 261 (void) dnode_cons(dn, NULL, 0); /* XXX */ 262 263 dn->dn_objset = os; 264 dn->dn_object = object; 265 dn->dn_dbuf = db; 266 dn->dn_phys = dnp; 267 268 if (dnp->dn_datablkszsec) 269 dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 270 dn->dn_indblkshift = dnp->dn_indblkshift; 271 dn->dn_nlevels = dnp->dn_nlevels; 272 dn->dn_type = dnp->dn_type; 273 dn->dn_nblkptr = dnp->dn_nblkptr; 274 dn->dn_checksum = dnp->dn_checksum; 275 dn->dn_compress = dnp->dn_compress; 276 dn->dn_bonustype = dnp->dn_bonustype; 277 dn->dn_bonuslen = dnp->dn_bonuslen; 278 dn->dn_maxblkid = dnp->dn_maxblkid; 279 280 dmu_zfetch_init(&dn->dn_zfetch, dn); 281 282 ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES); 283 mutex_enter(&os->os_lock); 284 list_insert_head(&os->os_dnodes, dn); 285 mutex_exit(&os->os_lock); 286 287 arc_space_consume(sizeof (dnode_t)); 288 return (dn); 289 } 290 291 static void 292 dnode_destroy(dnode_t *dn) 293 { 294 objset_impl_t *os = dn->dn_objset; 295 296 #ifdef ZFS_DEBUG 297 int i; 298 299 for (i = 0; i < TXG_SIZE; i++) { 300 ASSERT(!list_link_active(&dn->dn_dirty_link[i])); 301 ASSERT(NULL == list_head(&dn->dn_dirty_records[i])); 302 ASSERT(0 == avl_numnodes(&dn->dn_ranges[i])); 303 } 304 ASSERT(NULL == list_head(&dn->dn_dbufs)); 305 #endif 306 307 mutex_enter(&os->os_lock); 308 list_remove(&os->os_dnodes, dn); 309 mutex_exit(&os->os_lock); 310 311 if (dn->dn_dirtyctx_firstset) { 312 kmem_free(dn->dn_dirtyctx_firstset, 1); 313 dn->dn_dirtyctx_firstset = NULL; 314 } 315 dmu_zfetch_rele(&dn->dn_zfetch); 316 if (dn->dn_bonus) { 317 mutex_enter(&dn->dn_bonus->db_mtx); 318 dbuf_evict(dn->dn_bonus); 319 dn->dn_bonus = NULL; 320 } 321 kmem_cache_free(dnode_cache, dn); 322 arc_space_return(sizeof (dnode_t)); 323 } 324 325 void 326 dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs, 327 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 328 { 329 int i; 330 331 if (blocksize == 0) 332 blocksize = 1 << zfs_default_bs; 333 else if (blocksize > SPA_MAXBLOCKSIZE) 334 blocksize = SPA_MAXBLOCKSIZE; 335 else 336 blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE); 337 338 if (ibs == 0) 339 ibs = zfs_default_ibs; 340 341 ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT); 342 343 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset, 344 dn->dn_object, tx->tx_txg, blocksize, ibs); 345 346 ASSERT(dn->dn_type == DMU_OT_NONE); 347 ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0); 348 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE); 349 ASSERT(ot != DMU_OT_NONE); 350 ASSERT3U(ot, <, DMU_OT_NUMTYPES); 351 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) || 352 (bonustype != DMU_OT_NONE && bonuslen != 0)); 353 ASSERT3U(bonustype, <, DMU_OT_NUMTYPES); 354 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN); 355 ASSERT(dn->dn_type == DMU_OT_NONE); 356 ASSERT3U(dn->dn_maxblkid, ==, 0); 357 ASSERT3U(dn->dn_allocated_txg, ==, 0); 358 ASSERT3U(dn->dn_assigned_txg, ==, 0); 359 ASSERT(refcount_is_zero(&dn->dn_tx_holds)); 360 ASSERT3U(refcount_count(&dn->dn_holds), <=, 1); 361 ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL); 362 363 for (i = 0; i < TXG_SIZE; i++) { 364 ASSERT3U(dn->dn_next_nlevels[i], ==, 0); 365 ASSERT3U(dn->dn_next_indblkshift[i], ==, 0); 366 ASSERT3U(dn->dn_next_blksz[i], ==, 0); 367 ASSERT(!list_link_active(&dn->dn_dirty_link[i])); 368 ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL); 369 ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0); 370 } 371 372 dn->dn_type = ot; 373 dnode_setdblksz(dn, blocksize); 374 dn->dn_indblkshift = ibs; 375 dn->dn_nlevels = 1; 376 dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT); 377 dn->dn_bonustype = bonustype; 378 dn->dn_bonuslen = bonuslen; 379 dn->dn_checksum = ZIO_CHECKSUM_INHERIT; 380 dn->dn_compress = ZIO_COMPRESS_INHERIT; 381 dn->dn_dirtyctx = 0; 382 383 dn->dn_free_txg = 0; 384 if (dn->dn_dirtyctx_firstset) { 385 kmem_free(dn->dn_dirtyctx_firstset, 1); 386 dn->dn_dirtyctx_firstset = NULL; 387 } 388 389 dn->dn_allocated_txg = tx->tx_txg; 390 391 dnode_setdirty(dn, tx); 392 dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs; 393 dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz; 394 } 395 396 void 397 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, 398 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 399 { 400 int i; 401 dmu_buf_impl_t *db = NULL; 402 403 ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE); 404 ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE); 405 ASSERT3U(blocksize % SPA_MINBLOCKSIZE, ==, 0); 406 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx)); 407 ASSERT(tx->tx_txg != 0); 408 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) || 409 (bonustype != DMU_OT_NONE && bonuslen != 0)); 410 ASSERT3U(bonustype, <, DMU_OT_NUMTYPES); 411 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN); 412 413 for (i = 0; i < TXG_SIZE; i++) 414 ASSERT(!list_link_active(&dn->dn_dirty_link[i])); 415 416 /* clean up any unreferenced dbufs */ 417 (void) dnode_evict_dbufs(dn, 0); 418 ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL); 419 420 /* 421 * XXX I should really have a generation number to tell if we 422 * need to do this... 423 */ 424 if (blocksize != dn->dn_datablksz || 425 dn->dn_bonustype != bonustype || dn->dn_bonuslen != bonuslen) { 426 /* free all old data */ 427 dnode_free_range(dn, 0, -1ULL, tx); 428 } 429 430 /* change blocksize */ 431 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 432 if (blocksize != dn->dn_datablksz && 433 (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) || 434 list_head(&dn->dn_dbufs) != NULL)) { 435 db = dbuf_hold(dn, 0, FTAG); 436 dbuf_new_size(db, blocksize, tx); 437 } 438 dnode_setdblksz(dn, blocksize); 439 dnode_setdirty(dn, tx); 440 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize; 441 rw_exit(&dn->dn_struct_rwlock); 442 if (db) { 443 dbuf_rele(db, FTAG); 444 db = NULL; 445 } 446 447 /* change type */ 448 dn->dn_type = ot; 449 450 if (dn->dn_bonuslen != bonuslen) { 451 /* change bonus size */ 452 if (bonuslen == 0) 453 bonuslen = 1; /* XXX */ 454 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 455 if (dn->dn_bonus == NULL) 456 dn->dn_bonus = dbuf_create_bonus(dn); 457 db = dn->dn_bonus; 458 rw_exit(&dn->dn_struct_rwlock); 459 if (refcount_add(&db->db_holds, FTAG) == 1) 460 dnode_add_ref(dn, db); 461 VERIFY(0 == dbuf_read(db, NULL, DB_RF_MUST_SUCCEED)); 462 mutex_enter(&db->db_mtx); 463 ASSERT3U(db->db.db_size, ==, dn->dn_bonuslen); 464 ASSERT(db->db.db_data != NULL); 465 db->db.db_size = bonuslen; 466 mutex_exit(&db->db_mtx); 467 (void) dbuf_dirty(db, tx); 468 } 469 470 /* change bonus size and type */ 471 mutex_enter(&dn->dn_mtx); 472 dn->dn_bonustype = bonustype; 473 dn->dn_bonuslen = bonuslen; 474 dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT); 475 dn->dn_checksum = ZIO_CHECKSUM_INHERIT; 476 dn->dn_compress = ZIO_COMPRESS_INHERIT; 477 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR); 478 479 /* 480 * NB: we have to do the dbuf_rele after we've changed the 481 * dn_bonuslen, for the sake of dbuf_verify(). 482 */ 483 if (db) 484 dbuf_rele(db, FTAG); 485 486 dn->dn_allocated_txg = tx->tx_txg; 487 mutex_exit(&dn->dn_mtx); 488 } 489 490 void 491 dnode_special_close(dnode_t *dn) 492 { 493 /* 494 * Wait for final references to the dnode to clear. This can 495 * only happen if the arc is asyncronously evicting state that 496 * has a hold on this dnode while we are trying to evict this 497 * dnode. 498 */ 499 while (refcount_count(&dn->dn_holds) > 0) 500 delay(1); 501 dnode_destroy(dn); 502 } 503 504 dnode_t * 505 dnode_special_open(objset_impl_t *os, dnode_phys_t *dnp, uint64_t object) 506 { 507 dnode_t *dn = dnode_create(os, dnp, NULL, object); 508 DNODE_VERIFY(dn); 509 return (dn); 510 } 511 512 static void 513 dnode_buf_pageout(dmu_buf_t *db, void *arg) 514 { 515 dnode_t **children_dnodes = arg; 516 int i; 517 int epb = db->db_size >> DNODE_SHIFT; 518 519 for (i = 0; i < epb; i++) { 520 dnode_t *dn = children_dnodes[i]; 521 int n; 522 523 if (dn == NULL) 524 continue; 525 #ifdef ZFS_DEBUG 526 /* 527 * If there are holds on this dnode, then there should 528 * be holds on the dnode's containing dbuf as well; thus 529 * it wouldn't be eligable for eviction and this function 530 * would not have been called. 531 */ 532 ASSERT(refcount_is_zero(&dn->dn_holds)); 533 ASSERT(list_head(&dn->dn_dbufs) == NULL); 534 ASSERT(refcount_is_zero(&dn->dn_tx_holds)); 535 536 for (n = 0; n < TXG_SIZE; n++) 537 ASSERT(!list_link_active(&dn->dn_dirty_link[n])); 538 #endif 539 children_dnodes[i] = NULL; 540 dnode_destroy(dn); 541 } 542 kmem_free(children_dnodes, epb * sizeof (dnode_t *)); 543 } 544 545 /* 546 * errors: 547 * EINVAL - invalid object number. 548 * EIO - i/o error. 549 * succeeds even for free dnodes. 550 */ 551 int 552 dnode_hold_impl(objset_impl_t *os, uint64_t object, int flag, 553 void *tag, dnode_t **dnp) 554 { 555 int epb, idx, err; 556 int drop_struct_lock = FALSE; 557 int type; 558 uint64_t blk; 559 dnode_t *mdn, *dn; 560 dmu_buf_impl_t *db; 561 dnode_t **children_dnodes; 562 563 if (object == 0 || object >= DN_MAX_OBJECT) 564 return (EINVAL); 565 566 mdn = os->os_meta_dnode; 567 568 DNODE_VERIFY(mdn); 569 570 if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) { 571 rw_enter(&mdn->dn_struct_rwlock, RW_READER); 572 drop_struct_lock = TRUE; 573 } 574 575 blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t)); 576 577 db = dbuf_hold(mdn, blk, FTAG); 578 if (drop_struct_lock) 579 rw_exit(&mdn->dn_struct_rwlock); 580 if (db == NULL) 581 return (EIO); 582 err = dbuf_read(db, NULL, DB_RF_CANFAIL); 583 if (err) { 584 dbuf_rele(db, FTAG); 585 return (err); 586 } 587 588 ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT); 589 epb = db->db.db_size >> DNODE_SHIFT; 590 591 idx = object & (epb-1); 592 593 children_dnodes = dmu_buf_get_user(&db->db); 594 if (children_dnodes == NULL) { 595 dnode_t **winner; 596 children_dnodes = kmem_zalloc(epb * sizeof (dnode_t *), 597 KM_SLEEP); 598 if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL, 599 dnode_buf_pageout)) { 600 kmem_free(children_dnodes, epb * sizeof (dnode_t *)); 601 children_dnodes = winner; 602 } 603 } 604 605 if ((dn = children_dnodes[idx]) == NULL) { 606 dnode_phys_t *dnp = (dnode_phys_t *)db->db.db_data+idx; 607 dnode_t *winner; 608 609 dn = dnode_create(os, dnp, db, object); 610 winner = atomic_cas_ptr(&children_dnodes[idx], NULL, dn); 611 if (winner != NULL) { 612 dnode_destroy(dn); 613 dn = winner; 614 } 615 } 616 617 mutex_enter(&dn->dn_mtx); 618 type = dn->dn_type; 619 if (dn->dn_free_txg || 620 ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) || 621 ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)) { 622 mutex_exit(&dn->dn_mtx); 623 dbuf_rele(db, FTAG); 624 return (type == DMU_OT_NONE ? ENOENT : EEXIST); 625 } 626 mutex_exit(&dn->dn_mtx); 627 628 if (refcount_add(&dn->dn_holds, tag) == 1) 629 dbuf_add_ref(db, dn); 630 631 DNODE_VERIFY(dn); 632 ASSERT3P(dn->dn_dbuf, ==, db); 633 ASSERT3U(dn->dn_object, ==, object); 634 dbuf_rele(db, FTAG); 635 636 *dnp = dn; 637 return (0); 638 } 639 640 /* 641 * Return held dnode if the object is allocated, NULL if not. 642 */ 643 int 644 dnode_hold(objset_impl_t *os, uint64_t object, void *tag, dnode_t **dnp) 645 { 646 return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp)); 647 } 648 649 void 650 dnode_add_ref(dnode_t *dn, void *tag) 651 { 652 ASSERT(refcount_count(&dn->dn_holds) > 0); 653 (void) refcount_add(&dn->dn_holds, tag); 654 } 655 656 void 657 dnode_rele(dnode_t *dn, void *tag) 658 { 659 uint64_t refs; 660 661 refs = refcount_remove(&dn->dn_holds, tag); 662 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */ 663 if (refs == 0 && dn->dn_dbuf) 664 dbuf_rele(dn->dn_dbuf, dn); 665 } 666 667 void 668 dnode_setdirty(dnode_t *dn, dmu_tx_t *tx) 669 { 670 objset_impl_t *os = dn->dn_objset; 671 uint64_t txg = tx->tx_txg; 672 673 if (dn->dn_object == DMU_META_DNODE_OBJECT) 674 return; 675 676 DNODE_VERIFY(dn); 677 678 #ifdef ZFS_DEBUG 679 mutex_enter(&dn->dn_mtx); 680 ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg); 681 /* ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg); */ 682 mutex_exit(&dn->dn_mtx); 683 #endif 684 685 mutex_enter(&os->os_lock); 686 687 /* 688 * If we are already marked dirty, we're done. 689 */ 690 if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) { 691 mutex_exit(&os->os_lock); 692 return; 693 } 694 695 ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs)); 696 ASSERT(dn->dn_datablksz != 0); 697 ASSERT3U(dn->dn_next_blksz[txg&TXG_MASK], ==, 0); 698 699 dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n", 700 dn->dn_object, txg); 701 702 if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) { 703 list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn); 704 } else { 705 list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn); 706 } 707 708 mutex_exit(&os->os_lock); 709 710 /* 711 * The dnode maintains a hold on its containing dbuf as 712 * long as there are holds on it. Each instantiated child 713 * dbuf maintaines a hold on the dnode. When the last child 714 * drops its hold, the dnode will drop its hold on the 715 * containing dbuf. We add a "dirty hold" here so that the 716 * dnode will hang around after we finish processing its 717 * children. 718 */ 719 dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg); 720 721 (void) dbuf_dirty(dn->dn_dbuf, tx); 722 723 dsl_dataset_dirty(os->os_dsl_dataset, tx); 724 } 725 726 void 727 dnode_free(dnode_t *dn, dmu_tx_t *tx) 728 { 729 int txgoff = tx->tx_txg & TXG_MASK; 730 731 dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg); 732 733 /* we should be the only holder... hopefully */ 734 /* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */ 735 736 mutex_enter(&dn->dn_mtx); 737 if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) { 738 mutex_exit(&dn->dn_mtx); 739 return; 740 } 741 dn->dn_free_txg = tx->tx_txg; 742 mutex_exit(&dn->dn_mtx); 743 744 /* 745 * If the dnode is already dirty, it needs to be moved from 746 * the dirty list to the free list. 747 */ 748 mutex_enter(&dn->dn_objset->os_lock); 749 if (list_link_active(&dn->dn_dirty_link[txgoff])) { 750 list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn); 751 list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn); 752 mutex_exit(&dn->dn_objset->os_lock); 753 } else { 754 mutex_exit(&dn->dn_objset->os_lock); 755 dnode_setdirty(dn, tx); 756 } 757 } 758 759 /* 760 * Try to change the block size for the indicated dnode. This can only 761 * succeed if there are no blocks allocated or dirty beyond first block 762 */ 763 int 764 dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx) 765 { 766 dmu_buf_impl_t *db, *db_next; 767 int have_db0 = FALSE; 768 769 if (size == 0) 770 size = SPA_MINBLOCKSIZE; 771 if (size > SPA_MAXBLOCKSIZE) 772 size = SPA_MAXBLOCKSIZE; 773 else 774 size = P2ROUNDUP(size, SPA_MINBLOCKSIZE); 775 776 if (ibs == dn->dn_indblkshift) 777 ibs = 0; 778 779 if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0) 780 return (0); 781 782 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 783 784 /* Check for any allocated blocks beyond the first */ 785 if (dn->dn_phys->dn_maxblkid != 0) 786 goto fail; 787 788 mutex_enter(&dn->dn_dbufs_mtx); 789 for (db = list_head(&dn->dn_dbufs); db; db = db_next) { 790 db_next = list_next(&dn->dn_dbufs, db); 791 792 if (db->db_blkid == 0) { 793 have_db0 = TRUE; 794 } else if (db->db_blkid != DB_BONUS_BLKID) { 795 mutex_exit(&dn->dn_dbufs_mtx); 796 goto fail; 797 } 798 } 799 mutex_exit(&dn->dn_dbufs_mtx); 800 801 if (ibs && dn->dn_nlevels != 1) 802 goto fail; 803 804 db = NULL; 805 if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) || have_db0) { 806 /* obtain the old block */ 807 db = dbuf_hold(dn, 0, FTAG); 808 dbuf_new_size(db, size, tx); 809 } 810 811 dnode_setdblksz(dn, size); 812 dnode_setdirty(dn, tx); 813 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size; 814 if (ibs) { 815 dn->dn_indblkshift = ibs; 816 dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs; 817 } 818 819 if (db) 820 dbuf_rele(db, FTAG); 821 822 rw_exit(&dn->dn_struct_rwlock); 823 return (0); 824 825 fail: 826 rw_exit(&dn->dn_struct_rwlock); 827 return (ENOTSUP); 828 } 829 830 void 831 dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx) 832 { 833 uint64_t txgoff = tx->tx_txg & TXG_MASK; 834 int drop_struct_lock = FALSE; 835 int epbs, new_nlevels; 836 uint64_t sz; 837 838 ASSERT(blkid != DB_BONUS_BLKID); 839 840 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) { 841 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 842 drop_struct_lock = TRUE; 843 } 844 845 if (blkid <= dn->dn_maxblkid) 846 goto out; 847 848 dn->dn_maxblkid = blkid; 849 850 /* 851 * Compute the number of levels necessary to support the new maxblkid. 852 */ 853 new_nlevels = 1; 854 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 855 for (sz = dn->dn_nblkptr; 856 sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs) 857 new_nlevels++; 858 859 if (new_nlevels > dn->dn_nlevels) { 860 int old_nlevels = dn->dn_nlevels; 861 dmu_buf_impl_t *db; 862 list_t *list; 863 dbuf_dirty_record_t *new, *dr, *dr_next; 864 865 dn->dn_nlevels = new_nlevels; 866 867 ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]); 868 dn->dn_next_nlevels[txgoff] = new_nlevels; 869 870 /* dirty the left indirects */ 871 db = dbuf_hold_level(dn, old_nlevels, 0, FTAG); 872 new = dbuf_dirty(db, tx); 873 dbuf_rele(db, FTAG); 874 875 /* transfer the dirty records to the new indirect */ 876 mutex_enter(&dn->dn_mtx); 877 mutex_enter(&new->dt.di.dr_mtx); 878 list = &dn->dn_dirty_records[txgoff]; 879 for (dr = list_head(list); dr; dr = dr_next) { 880 dr_next = list_next(&dn->dn_dirty_records[txgoff], dr); 881 if (dr->dr_dbuf->db_level != new_nlevels-1 && 882 dr->dr_dbuf->db_blkid != DB_BONUS_BLKID) { 883 ASSERT(dr->dr_dbuf->db_level == old_nlevels-1); 884 list_remove(&dn->dn_dirty_records[txgoff], dr); 885 list_insert_tail(&new->dt.di.dr_children, dr); 886 dr->dr_parent = new; 887 } 888 } 889 mutex_exit(&new->dt.di.dr_mtx); 890 mutex_exit(&dn->dn_mtx); 891 } 892 893 out: 894 if (drop_struct_lock) 895 rw_exit(&dn->dn_struct_rwlock); 896 } 897 898 void 899 dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx) 900 { 901 avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK]; 902 avl_index_t where; 903 free_range_t *rp; 904 free_range_t rp_tofind; 905 uint64_t endblk = blkid + nblks; 906 907 ASSERT(MUTEX_HELD(&dn->dn_mtx)); 908 ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */ 909 910 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n", 911 blkid, nblks, tx->tx_txg); 912 rp_tofind.fr_blkid = blkid; 913 rp = avl_find(tree, &rp_tofind, &where); 914 if (rp == NULL) 915 rp = avl_nearest(tree, where, AVL_BEFORE); 916 if (rp == NULL) 917 rp = avl_nearest(tree, where, AVL_AFTER); 918 919 while (rp && (rp->fr_blkid <= blkid + nblks)) { 920 uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks; 921 free_range_t *nrp = AVL_NEXT(tree, rp); 922 923 if (blkid <= rp->fr_blkid && endblk >= fr_endblk) { 924 /* clear this entire range */ 925 avl_remove(tree, rp); 926 kmem_free(rp, sizeof (free_range_t)); 927 } else if (blkid <= rp->fr_blkid && 928 endblk > rp->fr_blkid && endblk < fr_endblk) { 929 /* clear the beginning of this range */ 930 rp->fr_blkid = endblk; 931 rp->fr_nblks = fr_endblk - endblk; 932 } else if (blkid > rp->fr_blkid && blkid < fr_endblk && 933 endblk >= fr_endblk) { 934 /* clear the end of this range */ 935 rp->fr_nblks = blkid - rp->fr_blkid; 936 } else if (blkid > rp->fr_blkid && endblk < fr_endblk) { 937 /* clear a chunk out of this range */ 938 free_range_t *new_rp = 939 kmem_alloc(sizeof (free_range_t), KM_SLEEP); 940 941 new_rp->fr_blkid = endblk; 942 new_rp->fr_nblks = fr_endblk - endblk; 943 avl_insert_here(tree, new_rp, rp, AVL_AFTER); 944 rp->fr_nblks = blkid - rp->fr_blkid; 945 } 946 /* there may be no overlap */ 947 rp = nrp; 948 } 949 } 950 951 void 952 dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx) 953 { 954 dmu_buf_impl_t *db; 955 uint64_t blkoff, blkid, nblks; 956 int blksz, head; 957 int trunc = FALSE; 958 959 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 960 blksz = dn->dn_datablksz; 961 962 /* If the range is past the end of the file, this is a no-op */ 963 if (off >= blksz * (dn->dn_maxblkid+1)) 964 goto out; 965 if (len == -1ULL) { 966 len = UINT64_MAX - off; 967 trunc = TRUE; 968 } 969 970 /* 971 * First, block align the region to free: 972 */ 973 if (ISP2(blksz)) { 974 head = P2NPHASE(off, blksz); 975 blkoff = P2PHASE(off, blksz); 976 } else { 977 ASSERT(dn->dn_maxblkid == 0); 978 if (off == 0 && len >= blksz) { 979 /* Freeing the whole block; don't do any head. */ 980 head = 0; 981 } else { 982 /* Freeing part of the block. */ 983 head = blksz - off; 984 ASSERT3U(head, >, 0); 985 } 986 blkoff = off; 987 } 988 /* zero out any partial block data at the start of the range */ 989 if (head) { 990 ASSERT3U(blkoff + head, ==, blksz); 991 if (len < head) 992 head = len; 993 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE, 994 FTAG, &db) == 0) { 995 caddr_t data; 996 997 /* don't dirty if it isn't on disk and isn't dirty */ 998 if (db->db_last_dirty || 999 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) { 1000 rw_exit(&dn->dn_struct_rwlock); 1001 dbuf_will_dirty(db, tx); 1002 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 1003 data = db->db.db_data; 1004 bzero(data + blkoff, head); 1005 } 1006 dbuf_rele(db, FTAG); 1007 } 1008 off += head; 1009 len -= head; 1010 } 1011 1012 /* If the range was less than one block, we're done */ 1013 if (len == 0 || off >= blksz * (dn->dn_maxblkid+1)) 1014 goto out; 1015 1016 if (!ISP2(blksz)) { 1017 /* 1018 * They are freeing the whole block of a 1019 * non-power-of-two blocksize file. Skip all the messy 1020 * math. 1021 */ 1022 ASSERT3U(off, ==, 0); 1023 ASSERT3U(len, >=, blksz); 1024 blkid = 0; 1025 nblks = 1; 1026 } else { 1027 int tail; 1028 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 1029 int blkshift = dn->dn_datablkshift; 1030 1031 /* If the remaining range is past end of file, we're done */ 1032 if (off > dn->dn_maxblkid << blkshift) 1033 goto out; 1034 1035 if (off + len == UINT64_MAX) 1036 tail = 0; 1037 else 1038 tail = P2PHASE(len, blksz); 1039 1040 ASSERT3U(P2PHASE(off, blksz), ==, 0); 1041 /* zero out any partial block data at the end of the range */ 1042 if (tail) { 1043 if (len < tail) 1044 tail = len; 1045 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len), 1046 TRUE, FTAG, &db) == 0) { 1047 /* don't dirty if not on disk and not dirty */ 1048 if (db->db_last_dirty || 1049 (db->db_blkptr && 1050 !BP_IS_HOLE(db->db_blkptr))) { 1051 rw_exit(&dn->dn_struct_rwlock); 1052 dbuf_will_dirty(db, tx); 1053 rw_enter(&dn->dn_struct_rwlock, 1054 RW_WRITER); 1055 bzero(db->db.db_data, tail); 1056 } 1057 dbuf_rele(db, FTAG); 1058 } 1059 len -= tail; 1060 } 1061 /* If the range did not include a full block, we are done */ 1062 if (len == 0) 1063 goto out; 1064 1065 /* dirty the left indirects */ 1066 if (dn->dn_nlevels > 1 && off != 0) { 1067 db = dbuf_hold_level(dn, 1, 1068 (off - head) >> (blkshift + epbs), FTAG); 1069 dbuf_will_dirty(db, tx); 1070 dbuf_rele(db, FTAG); 1071 } 1072 1073 /* dirty the right indirects */ 1074 if (dn->dn_nlevels > 1 && !trunc) { 1075 db = dbuf_hold_level(dn, 1, 1076 (off + len + tail - 1) >> (blkshift + epbs), FTAG); 1077 dbuf_will_dirty(db, tx); 1078 dbuf_rele(db, FTAG); 1079 } 1080 1081 /* 1082 * Finally, add this range to the dnode range list, we 1083 * will finish up this free operation in the syncing phase. 1084 */ 1085 ASSERT(IS_P2ALIGNED(off, 1<<blkshift)); 1086 ASSERT(off + len == UINT64_MAX || 1087 IS_P2ALIGNED(len, 1<<blkshift)); 1088 blkid = off >> blkshift; 1089 nblks = len >> blkshift; 1090 1091 if (trunc) 1092 dn->dn_maxblkid = (blkid ? blkid - 1 : 0); 1093 } 1094 1095 mutex_enter(&dn->dn_mtx); 1096 dnode_clear_range(dn, blkid, nblks, tx); 1097 { 1098 free_range_t *rp, *found; 1099 avl_index_t where; 1100 avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK]; 1101 1102 /* Add new range to dn_ranges */ 1103 rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP); 1104 rp->fr_blkid = blkid; 1105 rp->fr_nblks = nblks; 1106 found = avl_find(tree, rp, &where); 1107 ASSERT(found == NULL); 1108 avl_insert(tree, rp, where); 1109 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n", 1110 blkid, nblks, tx->tx_txg); 1111 } 1112 mutex_exit(&dn->dn_mtx); 1113 1114 dbuf_free_range(dn, blkid, nblks, tx); 1115 dnode_setdirty(dn, tx); 1116 out: 1117 rw_exit(&dn->dn_struct_rwlock); 1118 } 1119 1120 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */ 1121 uint64_t 1122 dnode_block_freed(dnode_t *dn, uint64_t blkid) 1123 { 1124 free_range_t range_tofind; 1125 void *dp = spa_get_dsl(dn->dn_objset->os_spa); 1126 int i; 1127 1128 if (blkid == DB_BONUS_BLKID) 1129 return (FALSE); 1130 1131 /* 1132 * If we're in the process of opening the pool, dp will not be 1133 * set yet, but there shouldn't be anything dirty. 1134 */ 1135 if (dp == NULL) 1136 return (FALSE); 1137 1138 if (dn->dn_free_txg) 1139 return (TRUE); 1140 1141 /* 1142 * If dn_datablkshift is not set, then there's only a single 1143 * block, in which case there will never be a free range so it 1144 * won't matter. 1145 */ 1146 range_tofind.fr_blkid = blkid; 1147 mutex_enter(&dn->dn_mtx); 1148 for (i = 0; i < TXG_SIZE; i++) { 1149 free_range_t *range_found; 1150 avl_index_t idx; 1151 1152 range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx); 1153 if (range_found) { 1154 ASSERT(range_found->fr_nblks > 0); 1155 break; 1156 } 1157 range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE); 1158 if (range_found && 1159 range_found->fr_blkid + range_found->fr_nblks > blkid) 1160 break; 1161 } 1162 mutex_exit(&dn->dn_mtx); 1163 return (i < TXG_SIZE); 1164 } 1165 1166 /* call from syncing context when we actually write/free space for this dnode */ 1167 void 1168 dnode_diduse_space(dnode_t *dn, int64_t delta) 1169 { 1170 uint64_t space; 1171 dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n", 1172 dn, dn->dn_phys, 1173 (u_longlong_t)dn->dn_phys->dn_used, 1174 (longlong_t)delta); 1175 1176 mutex_enter(&dn->dn_mtx); 1177 space = DN_USED_BYTES(dn->dn_phys); 1178 if (delta > 0) { 1179 ASSERT3U(space + delta, >=, space); /* no overflow */ 1180 } else { 1181 ASSERT3U(space, >=, -delta); /* no underflow */ 1182 } 1183 space += delta; 1184 if (spa_version(dn->dn_objset->os_spa) < ZFS_VERSION_DNODE_BYTES) { 1185 ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0); 1186 ASSERT3U(P2PHASE(space, 1<<DEV_BSHIFT), ==, 0); 1187 dn->dn_phys->dn_used = space >> DEV_BSHIFT; 1188 } else { 1189 dn->dn_phys->dn_used = space; 1190 dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES; 1191 } 1192 mutex_exit(&dn->dn_mtx); 1193 } 1194 1195 /* 1196 * Call when we think we're going to write/free space in open context. 1197 * Be conservative (ie. OK to write less than this or free more than 1198 * this, but don't write more or free less). 1199 */ 1200 void 1201 dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx) 1202 { 1203 objset_impl_t *os = dn->dn_objset; 1204 dsl_dataset_t *ds = os->os_dsl_dataset; 1205 1206 if (space > 0) 1207 space = spa_get_asize(os->os_spa, space); 1208 1209 if (ds) 1210 dsl_dir_willuse_space(ds->ds_dir, space, tx); 1211 1212 dmu_tx_willuse_space(tx, space); 1213 } 1214 1215 static int 1216 dnode_next_offset_level(dnode_t *dn, boolean_t hole, uint64_t *offset, 1217 int lvl, uint64_t blkfill, uint64_t txg) 1218 { 1219 dmu_buf_impl_t *db = NULL; 1220 void *data = NULL; 1221 uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 1222 uint64_t epb = 1ULL << epbs; 1223 uint64_t minfill, maxfill; 1224 int i, error, span; 1225 1226 dprintf("probing object %llu offset %llx level %d of %u\n", 1227 dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels); 1228 1229 if (lvl == dn->dn_phys->dn_nlevels) { 1230 error = 0; 1231 epb = dn->dn_phys->dn_nblkptr; 1232 data = dn->dn_phys->dn_blkptr; 1233 } else { 1234 uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl); 1235 error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db); 1236 if (error) { 1237 if (error == ENOENT) 1238 return (hole ? 0 : ESRCH); 1239 return (error); 1240 } 1241 error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT); 1242 if (error) { 1243 dbuf_rele(db, FTAG); 1244 return (error); 1245 } 1246 data = db->db.db_data; 1247 } 1248 1249 if (db && txg && 1250 (db->db_blkptr == NULL || db->db_blkptr->blk_birth <= txg)) { 1251 error = ESRCH; 1252 } else if (lvl == 0) { 1253 dnode_phys_t *dnp = data; 1254 span = DNODE_SHIFT; 1255 ASSERT(dn->dn_type == DMU_OT_DNODE); 1256 1257 for (i = (*offset >> span) & (blkfill - 1); i < blkfill; i++) { 1258 boolean_t newcontents = B_TRUE; 1259 if (txg) { 1260 int j; 1261 newcontents = B_FALSE; 1262 for (j = 0; j < dnp[i].dn_nblkptr; j++) { 1263 if (dnp[i].dn_blkptr[j].blk_birth > txg) 1264 newcontents = B_TRUE; 1265 } 1266 } 1267 if (!dnp[i].dn_type == hole && newcontents) 1268 break; 1269 *offset += 1ULL << span; 1270 } 1271 if (i == blkfill) 1272 error = ESRCH; 1273 } else { 1274 blkptr_t *bp = data; 1275 span = (lvl - 1) * epbs + dn->dn_datablkshift; 1276 minfill = 0; 1277 maxfill = blkfill << ((lvl - 1) * epbs); 1278 1279 if (hole) 1280 maxfill--; 1281 else 1282 minfill++; 1283 1284 for (i = (*offset >> span) & ((1ULL << epbs) - 1); 1285 i < epb; i++) { 1286 if (bp[i].blk_fill >= minfill && 1287 bp[i].blk_fill <= maxfill && 1288 bp[i].blk_birth > txg) 1289 break; 1290 *offset += 1ULL << span; 1291 } 1292 if (i >= epb) 1293 error = ESRCH; 1294 } 1295 1296 if (db) 1297 dbuf_rele(db, FTAG); 1298 1299 return (error); 1300 } 1301 1302 /* 1303 * Find the next hole, data, or sparse region at or after *offset. 1304 * The value 'blkfill' tells us how many items we expect to find 1305 * in an L0 data block; this value is 1 for normal objects, 1306 * DNODES_PER_BLOCK for the meta dnode, and some fraction of 1307 * DNODES_PER_BLOCK when searching for sparse regions thereof. 1308 * 1309 * Examples: 1310 * 1311 * dnode_next_offset(dn, hole, offset, 1, 1, 0); 1312 * Finds the next hole/data in a file. 1313 * Used in dmu_offset_next(). 1314 * 1315 * dnode_next_offset(mdn, hole, offset, 0, DNODES_PER_BLOCK, txg); 1316 * Finds the next free/allocated dnode an objset's meta-dnode. 1317 * Only finds objects that have new contents since txg (ie. 1318 * bonus buffer changes and content removal are ignored). 1319 * Used in dmu_object_next(). 1320 * 1321 * dnode_next_offset(mdn, TRUE, offset, 2, DNODES_PER_BLOCK >> 2, 0); 1322 * Finds the next L2 meta-dnode bp that's at most 1/4 full. 1323 * Used in dmu_object_alloc(). 1324 */ 1325 int 1326 dnode_next_offset(dnode_t *dn, boolean_t hole, uint64_t *offset, 1327 int minlvl, uint64_t blkfill, uint64_t txg) 1328 { 1329 int lvl, maxlvl; 1330 int error = 0; 1331 uint64_t initial_offset = *offset; 1332 1333 rw_enter(&dn->dn_struct_rwlock, RW_READER); 1334 1335 if (dn->dn_phys->dn_nlevels == 0) { 1336 rw_exit(&dn->dn_struct_rwlock); 1337 return (ESRCH); 1338 } 1339 1340 if (dn->dn_datablkshift == 0) { 1341 if (*offset < dn->dn_datablksz) { 1342 if (hole) 1343 *offset = dn->dn_datablksz; 1344 } else { 1345 error = ESRCH; 1346 } 1347 rw_exit(&dn->dn_struct_rwlock); 1348 return (error); 1349 } 1350 1351 maxlvl = dn->dn_phys->dn_nlevels; 1352 1353 for (lvl = minlvl; lvl <= maxlvl; lvl++) { 1354 error = dnode_next_offset_level(dn, 1355 hole, offset, lvl, blkfill, txg); 1356 if (error != ESRCH) 1357 break; 1358 } 1359 1360 while (--lvl >= minlvl && error == 0) { 1361 error = dnode_next_offset_level(dn, 1362 hole, offset, lvl, blkfill, txg); 1363 } 1364 1365 rw_exit(&dn->dn_struct_rwlock); 1366 1367 if (error == 0 && initial_offset > *offset) 1368 error = ESRCH; 1369 1370 return (error); 1371 } 1372