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