1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or https://opensource.org/licenses/CDDL-1.0. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2011, 2020 by Delphix. All rights reserved. 25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved. 26 * Copyright (c) 2013, Joyent, Inc. All rights reserved. 27 * Copyright (c) 2016, Nexenta Systems, Inc. All rights reserved. 28 * Copyright (c) 2015 by Chunwei Chen. All rights reserved. 29 * Copyright (c) 2019 Datto Inc. 30 * Copyright (c) 2019, 2023, Klara Inc. 31 * Copyright (c) 2019, Allan Jude 32 * Copyright (c) 2022 Hewlett Packard Enterprise Development LP. 33 * Copyright (c) 2021, 2022 by Pawel Jakub Dawidek 34 */ 35 36 #include <sys/dmu.h> 37 #include <sys/dmu_impl.h> 38 #include <sys/dmu_tx.h> 39 #include <sys/dbuf.h> 40 #include <sys/dnode.h> 41 #include <sys/zfs_context.h> 42 #include <sys/dmu_objset.h> 43 #include <sys/dmu_traverse.h> 44 #include <sys/dsl_dataset.h> 45 #include <sys/dsl_dir.h> 46 #include <sys/dsl_pool.h> 47 #include <sys/dsl_synctask.h> 48 #include <sys/dsl_prop.h> 49 #include <sys/dmu_zfetch.h> 50 #include <sys/zfs_ioctl.h> 51 #include <sys/zap.h> 52 #include <sys/zio_checksum.h> 53 #include <sys/zio_compress.h> 54 #include <sys/sa.h> 55 #include <sys/zfeature.h> 56 #include <sys/abd.h> 57 #include <sys/brt.h> 58 #include <sys/trace_zfs.h> 59 #include <sys/zfs_racct.h> 60 #include <sys/zfs_rlock.h> 61 #ifdef _KERNEL 62 #include <sys/vmsystm.h> 63 #include <sys/zfs_znode.h> 64 #endif 65 66 /* 67 * Enable/disable nopwrite feature. 68 */ 69 static int zfs_nopwrite_enabled = 1; 70 71 /* 72 * Tunable to control percentage of dirtied L1 blocks from frees allowed into 73 * one TXG. After this threshold is crossed, additional dirty blocks from frees 74 * will wait until the next TXG. 75 * A value of zero will disable this throttle. 76 */ 77 static uint_t zfs_per_txg_dirty_frees_percent = 30; 78 79 /* 80 * Enable/disable forcing txg sync when dirty checking for holes with lseek(). 81 * By default this is enabled to ensure accurate hole reporting, it can result 82 * in a significant performance penalty for lseek(SEEK_HOLE) heavy workloads. 83 * Disabling this option will result in holes never being reported in dirty 84 * files which is always safe. 85 */ 86 static int zfs_dmu_offset_next_sync = 1; 87 88 /* 89 * Limit the amount we can prefetch with one call to this amount. This 90 * helps to limit the amount of memory that can be used by prefetching. 91 * Larger objects should be prefetched a bit at a time. 92 */ 93 #ifdef _ILP32 94 uint_t dmu_prefetch_max = 8 * 1024 * 1024; 95 #else 96 uint_t dmu_prefetch_max = 8 * SPA_MAXBLOCKSIZE; 97 #endif 98 99 /* 100 * Override copies= for dedup state objects. 0 means the traditional behaviour 101 * (ie the default for the containing objset ie 3 for the MOS). 102 */ 103 uint_t dmu_ddt_copies = 0; 104 105 const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = { 106 {DMU_BSWAP_UINT8, TRUE, FALSE, FALSE, "unallocated" }, 107 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "object directory" }, 108 {DMU_BSWAP_UINT64, TRUE, TRUE, FALSE, "object array" }, 109 {DMU_BSWAP_UINT8, TRUE, FALSE, FALSE, "packed nvlist" }, 110 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "packed nvlist size" }, 111 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "bpobj" }, 112 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "bpobj header" }, 113 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "SPA space map header" }, 114 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "SPA space map" }, 115 {DMU_BSWAP_UINT64, TRUE, FALSE, TRUE, "ZIL intent log" }, 116 {DMU_BSWAP_DNODE, TRUE, FALSE, TRUE, "DMU dnode" }, 117 {DMU_BSWAP_OBJSET, TRUE, TRUE, FALSE, "DMU objset" }, 118 {DMU_BSWAP_UINT64, TRUE, TRUE, FALSE, "DSL directory" }, 119 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL directory child map"}, 120 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL dataset snap map" }, 121 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL props" }, 122 {DMU_BSWAP_UINT64, TRUE, TRUE, FALSE, "DSL dataset" }, 123 {DMU_BSWAP_ZNODE, TRUE, FALSE, FALSE, "ZFS znode" }, 124 {DMU_BSWAP_OLDACL, TRUE, FALSE, TRUE, "ZFS V0 ACL" }, 125 {DMU_BSWAP_UINT8, FALSE, FALSE, TRUE, "ZFS plain file" }, 126 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "ZFS directory" }, 127 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "ZFS master node" }, 128 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "ZFS delete queue" }, 129 {DMU_BSWAP_UINT8, FALSE, FALSE, TRUE, "zvol object" }, 130 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "zvol prop" }, 131 {DMU_BSWAP_UINT8, FALSE, FALSE, TRUE, "other uint8[]" }, 132 {DMU_BSWAP_UINT64, FALSE, FALSE, TRUE, "other uint64[]" }, 133 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "other ZAP" }, 134 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "persistent error log" }, 135 {DMU_BSWAP_UINT8, TRUE, FALSE, FALSE, "SPA history" }, 136 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "SPA history offsets" }, 137 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "Pool properties" }, 138 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL permissions" }, 139 {DMU_BSWAP_ACL, TRUE, FALSE, TRUE, "ZFS ACL" }, 140 {DMU_BSWAP_UINT8, TRUE, FALSE, TRUE, "ZFS SYSACL" }, 141 {DMU_BSWAP_UINT8, TRUE, FALSE, TRUE, "FUID table" }, 142 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "FUID table size" }, 143 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL dataset next clones"}, 144 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "scan work queue" }, 145 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "ZFS user/group/project used" }, 146 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "ZFS user/group/project quota"}, 147 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "snapshot refcount tags"}, 148 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "DDT ZAP algorithm" }, 149 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "DDT statistics" }, 150 {DMU_BSWAP_UINT8, TRUE, FALSE, TRUE, "System attributes" }, 151 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "SA master node" }, 152 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "SA attr registration" }, 153 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "SA attr layouts" }, 154 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "scan translations" }, 155 {DMU_BSWAP_UINT8, FALSE, FALSE, TRUE, "deduplicated block" }, 156 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL deadlist map" }, 157 {DMU_BSWAP_UINT64, TRUE, TRUE, FALSE, "DSL deadlist map hdr" }, 158 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL dir clones" }, 159 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "bpobj subobj" } 160 }; 161 162 dmu_object_byteswap_info_t dmu_ot_byteswap[DMU_BSWAP_NUMFUNCS] = { 163 { byteswap_uint8_array, "uint8" }, 164 { byteswap_uint16_array, "uint16" }, 165 { byteswap_uint32_array, "uint32" }, 166 { byteswap_uint64_array, "uint64" }, 167 { zap_byteswap, "zap" }, 168 { dnode_buf_byteswap, "dnode" }, 169 { dmu_objset_byteswap, "objset" }, 170 { zfs_znode_byteswap, "znode" }, 171 { zfs_oldacl_byteswap, "oldacl" }, 172 { zfs_acl_byteswap, "acl" } 173 }; 174 175 int 176 dmu_buf_hold_noread_by_dnode(dnode_t *dn, uint64_t offset, 177 const void *tag, dmu_buf_t **dbp) 178 { 179 uint64_t blkid; 180 dmu_buf_impl_t *db; 181 182 rw_enter(&dn->dn_struct_rwlock, RW_READER); 183 blkid = dbuf_whichblock(dn, 0, offset); 184 db = dbuf_hold(dn, blkid, tag); 185 rw_exit(&dn->dn_struct_rwlock); 186 187 if (db == NULL) { 188 *dbp = NULL; 189 return (SET_ERROR(EIO)); 190 } 191 192 *dbp = &db->db; 193 return (0); 194 } 195 196 int 197 dmu_buf_hold_noread(objset_t *os, uint64_t object, uint64_t offset, 198 const void *tag, dmu_buf_t **dbp) 199 { 200 dnode_t *dn; 201 uint64_t blkid; 202 dmu_buf_impl_t *db; 203 int err; 204 205 err = dnode_hold(os, object, FTAG, &dn); 206 if (err) 207 return (err); 208 rw_enter(&dn->dn_struct_rwlock, RW_READER); 209 blkid = dbuf_whichblock(dn, 0, offset); 210 db = dbuf_hold(dn, blkid, tag); 211 rw_exit(&dn->dn_struct_rwlock); 212 dnode_rele(dn, FTAG); 213 214 if (db == NULL) { 215 *dbp = NULL; 216 return (SET_ERROR(EIO)); 217 } 218 219 *dbp = &db->db; 220 return (err); 221 } 222 223 int 224 dmu_buf_hold_by_dnode(dnode_t *dn, uint64_t offset, 225 const void *tag, dmu_buf_t **dbp, dmu_flags_t flags) 226 { 227 int err; 228 229 err = dmu_buf_hold_noread_by_dnode(dn, offset, tag, dbp); 230 if (err == 0) { 231 dmu_buf_impl_t *db = (dmu_buf_impl_t *)(*dbp); 232 err = dbuf_read(db, NULL, flags | DB_RF_CANFAIL); 233 if (err != 0) { 234 dbuf_rele(db, tag); 235 *dbp = NULL; 236 } 237 } 238 239 return (err); 240 } 241 242 int 243 dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset, 244 const void *tag, dmu_buf_t **dbp, dmu_flags_t flags) 245 { 246 int err; 247 248 err = dmu_buf_hold_noread(os, object, offset, tag, dbp); 249 if (err == 0) { 250 dmu_buf_impl_t *db = (dmu_buf_impl_t *)(*dbp); 251 err = dbuf_read(db, NULL, flags | DB_RF_CANFAIL); 252 if (err != 0) { 253 dbuf_rele(db, tag); 254 *dbp = NULL; 255 } 256 } 257 258 return (err); 259 } 260 261 int 262 dmu_bonus_max(void) 263 { 264 return (DN_OLD_MAX_BONUSLEN); 265 } 266 267 int 268 dmu_set_bonus(dmu_buf_t *db_fake, int newsize, dmu_tx_t *tx) 269 { 270 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 271 dnode_t *dn; 272 int error; 273 274 if (newsize < 0 || newsize > db_fake->db_size) 275 return (SET_ERROR(EINVAL)); 276 277 DB_DNODE_ENTER(db); 278 dn = DB_DNODE(db); 279 280 if (dn->dn_bonus != db) { 281 error = SET_ERROR(EINVAL); 282 } else { 283 dnode_setbonuslen(dn, newsize, tx); 284 error = 0; 285 } 286 287 DB_DNODE_EXIT(db); 288 return (error); 289 } 290 291 int 292 dmu_set_bonustype(dmu_buf_t *db_fake, dmu_object_type_t type, dmu_tx_t *tx) 293 { 294 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 295 dnode_t *dn; 296 int error; 297 298 if (!DMU_OT_IS_VALID(type)) 299 return (SET_ERROR(EINVAL)); 300 301 DB_DNODE_ENTER(db); 302 dn = DB_DNODE(db); 303 304 if (dn->dn_bonus != db) { 305 error = SET_ERROR(EINVAL); 306 } else { 307 dnode_setbonus_type(dn, type, tx); 308 error = 0; 309 } 310 311 DB_DNODE_EXIT(db); 312 return (error); 313 } 314 315 dmu_object_type_t 316 dmu_get_bonustype(dmu_buf_t *db_fake) 317 { 318 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 319 dmu_object_type_t type; 320 321 DB_DNODE_ENTER(db); 322 type = DB_DNODE(db)->dn_bonustype; 323 DB_DNODE_EXIT(db); 324 325 return (type); 326 } 327 328 int 329 dmu_rm_spill(objset_t *os, uint64_t object, dmu_tx_t *tx) 330 { 331 dnode_t *dn; 332 int error; 333 334 error = dnode_hold(os, object, FTAG, &dn); 335 dbuf_rm_spill(dn, tx); 336 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 337 dnode_rm_spill(dn, tx); 338 rw_exit(&dn->dn_struct_rwlock); 339 dnode_rele(dn, FTAG); 340 return (error); 341 } 342 343 /* 344 * Lookup and hold the bonus buffer for the provided dnode. If the dnode 345 * has not yet been allocated a new bonus dbuf a will be allocated. 346 * Returns ENOENT, EIO, or 0. 347 */ 348 int dmu_bonus_hold_by_dnode(dnode_t *dn, const void *tag, dmu_buf_t **dbp, 349 dmu_flags_t flags) 350 { 351 dmu_buf_impl_t *db; 352 int error; 353 354 rw_enter(&dn->dn_struct_rwlock, RW_READER); 355 if (dn->dn_bonus == NULL) { 356 if (!rw_tryupgrade(&dn->dn_struct_rwlock)) { 357 rw_exit(&dn->dn_struct_rwlock); 358 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 359 } 360 if (dn->dn_bonus == NULL) 361 dbuf_create_bonus(dn); 362 } 363 db = dn->dn_bonus; 364 365 /* as long as the bonus buf is held, the dnode will be held */ 366 if (zfs_refcount_add(&db->db_holds, tag) == 1) { 367 VERIFY(dnode_add_ref(dn, db)); 368 atomic_inc_32(&dn->dn_dbufs_count); 369 } 370 371 /* 372 * Wait to drop dn_struct_rwlock until after adding the bonus dbuf's 373 * hold and incrementing the dbuf count to ensure that dnode_move() sees 374 * a dnode hold for every dbuf. 375 */ 376 rw_exit(&dn->dn_struct_rwlock); 377 378 error = dbuf_read(db, NULL, flags | DB_RF_CANFAIL); 379 if (error) { 380 dnode_evict_bonus(dn); 381 dbuf_rele(db, tag); 382 *dbp = NULL; 383 return (error); 384 } 385 386 *dbp = &db->db; 387 return (0); 388 } 389 390 int 391 dmu_bonus_hold(objset_t *os, uint64_t object, const void *tag, dmu_buf_t **dbp) 392 { 393 dnode_t *dn; 394 int error; 395 396 error = dnode_hold(os, object, FTAG, &dn); 397 if (error) 398 return (error); 399 400 error = dmu_bonus_hold_by_dnode(dn, tag, dbp, DMU_READ_NO_PREFETCH); 401 dnode_rele(dn, FTAG); 402 403 return (error); 404 } 405 406 /* 407 * returns ENOENT, EIO, or 0. 408 * 409 * This interface will allocate a blank spill dbuf when a spill blk 410 * doesn't already exist on the dnode. 411 * 412 * if you only want to find an already existing spill db, then 413 * dmu_spill_hold_existing() should be used. 414 */ 415 int 416 dmu_spill_hold_by_dnode(dnode_t *dn, dmu_flags_t flags, const void *tag, 417 dmu_buf_t **dbp) 418 { 419 dmu_buf_impl_t *db = NULL; 420 int err; 421 422 if ((flags & DB_RF_HAVESTRUCT) == 0) 423 rw_enter(&dn->dn_struct_rwlock, RW_READER); 424 425 db = dbuf_hold(dn, DMU_SPILL_BLKID, tag); 426 427 if ((flags & DB_RF_HAVESTRUCT) == 0) 428 rw_exit(&dn->dn_struct_rwlock); 429 430 if (db == NULL) { 431 *dbp = NULL; 432 return (SET_ERROR(EIO)); 433 } 434 err = dbuf_read(db, NULL, flags); 435 if (err == 0) 436 *dbp = &db->db; 437 else { 438 dbuf_rele(db, tag); 439 *dbp = NULL; 440 } 441 return (err); 442 } 443 444 int 445 dmu_spill_hold_existing(dmu_buf_t *bonus, const void *tag, dmu_buf_t **dbp) 446 { 447 dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus; 448 dnode_t *dn; 449 int err; 450 451 DB_DNODE_ENTER(db); 452 dn = DB_DNODE(db); 453 454 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_SA) { 455 err = SET_ERROR(EINVAL); 456 } else { 457 rw_enter(&dn->dn_struct_rwlock, RW_READER); 458 459 if (!dn->dn_have_spill) { 460 err = SET_ERROR(ENOENT); 461 } else { 462 err = dmu_spill_hold_by_dnode(dn, 463 DB_RF_HAVESTRUCT | DB_RF_CANFAIL, tag, dbp); 464 } 465 466 rw_exit(&dn->dn_struct_rwlock); 467 } 468 469 DB_DNODE_EXIT(db); 470 return (err); 471 } 472 473 int 474 dmu_spill_hold_by_bonus(dmu_buf_t *bonus, dmu_flags_t flags, const void *tag, 475 dmu_buf_t **dbp) 476 { 477 dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus; 478 int err; 479 480 DB_DNODE_ENTER(db); 481 err = dmu_spill_hold_by_dnode(DB_DNODE(db), flags, tag, dbp); 482 DB_DNODE_EXIT(db); 483 484 return (err); 485 } 486 487 /* 488 * Note: longer-term, we should modify all of the dmu_buf_*() interfaces 489 * to take a held dnode rather than <os, object> -- the lookup is wasteful, 490 * and can induce severe lock contention when writing to several files 491 * whose dnodes are in the same block. 492 */ 493 int 494 dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset, uint64_t length, 495 boolean_t read, const void *tag, int *numbufsp, dmu_buf_t ***dbpp, 496 dmu_flags_t flags) 497 { 498 dmu_buf_t **dbp; 499 zstream_t *zs = NULL; 500 uint64_t blkid, nblks, i; 501 dmu_flags_t dbuf_flags; 502 int err; 503 zio_t *zio = NULL; 504 boolean_t missed = B_FALSE; 505 506 ASSERT(!read || length <= DMU_MAX_ACCESS); 507 508 /* 509 * Note: We directly notify the prefetch code of this read, so that 510 * we can tell it about the multi-block read. dbuf_read() only knows 511 * about the one block it is accessing. 512 */ 513 dbuf_flags = (flags & ~DMU_READ_PREFETCH) | DMU_READ_NO_PREFETCH | 514 DB_RF_CANFAIL | DB_RF_NEVERWAIT | DB_RF_HAVESTRUCT; 515 516 rw_enter(&dn->dn_struct_rwlock, RW_READER); 517 if (dn->dn_datablkshift) { 518 int blkshift = dn->dn_datablkshift; 519 nblks = (P2ROUNDUP(offset + length, 1ULL << blkshift) - 520 P2ALIGN_TYPED(offset, 1ULL << blkshift, uint64_t)) 521 >> blkshift; 522 } else { 523 if (offset + length > dn->dn_datablksz) { 524 zfs_panic_recover("zfs: accessing past end of object " 525 "%llx/%llx (size=%u access=%llu+%llu)", 526 (longlong_t)dn->dn_objset-> 527 os_dsl_dataset->ds_object, 528 (longlong_t)dn->dn_object, dn->dn_datablksz, 529 (longlong_t)offset, (longlong_t)length); 530 rw_exit(&dn->dn_struct_rwlock); 531 return (SET_ERROR(EIO)); 532 } 533 nblks = 1; 534 } 535 dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP); 536 537 if (read) 538 zio = zio_root(dn->dn_objset->os_spa, NULL, NULL, 539 ZIO_FLAG_CANFAIL); 540 blkid = dbuf_whichblock(dn, 0, offset); 541 if ((flags & DMU_READ_NO_PREFETCH) == 0) { 542 /* 543 * Prepare the zfetch before initiating the demand reads, so 544 * that if multiple threads block on same indirect block, we 545 * base predictions on the original less racy request order. 546 */ 547 zs = dmu_zfetch_prepare(&dn->dn_zfetch, blkid, nblks, 548 read && !(flags & DMU_DIRECTIO), B_TRUE); 549 } 550 for (i = 0; i < nblks; i++) { 551 dmu_buf_impl_t *db = dbuf_hold(dn, blkid + i, tag); 552 if (db == NULL) { 553 if (zs) { 554 dmu_zfetch_run(&dn->dn_zfetch, zs, missed, 555 B_TRUE, (flags & DMU_UNCACHEDIO)); 556 } 557 rw_exit(&dn->dn_struct_rwlock); 558 dmu_buf_rele_array(dbp, nblks, tag); 559 if (read) 560 zio_nowait(zio); 561 return (SET_ERROR(EIO)); 562 } 563 564 /* 565 * Initiate async demand data read. 566 * We check the db_state after calling dbuf_read() because 567 * (1) dbuf_read() may change the state to CACHED due to a 568 * hit in the ARC, and (2) on a cache miss, a child will 569 * have been added to "zio" but not yet completed, so the 570 * state will not yet be CACHED. 571 */ 572 if (read) { 573 if (i == nblks - 1 && blkid + i < dn->dn_maxblkid && 574 offset + length < db->db.db_offset + 575 db->db.db_size) { 576 if (offset <= db->db.db_offset) 577 dbuf_flags |= DMU_PARTIAL_FIRST; 578 else 579 dbuf_flags |= DMU_PARTIAL_MORE; 580 } 581 (void) dbuf_read(db, zio, dbuf_flags); 582 if (db->db_state != DB_CACHED) 583 missed = B_TRUE; 584 } 585 dbp[i] = &db->db; 586 } 587 588 /* 589 * If we are doing O_DIRECT we still hold the dbufs, even for reads, 590 * but we do not issue any reads here. We do not want to account for 591 * writes in this case. 592 * 593 * O_DIRECT write/read accounting takes place in 594 * dmu_{write/read}_abd(). 595 */ 596 if (!read && ((flags & DMU_DIRECTIO) == 0)) 597 zfs_racct_write(dn->dn_objset->os_spa, length, nblks, flags); 598 599 if (zs) { 600 dmu_zfetch_run(&dn->dn_zfetch, zs, missed, B_TRUE, 601 (flags & DMU_UNCACHEDIO)); 602 } 603 rw_exit(&dn->dn_struct_rwlock); 604 605 if (read) { 606 /* wait for async read i/o */ 607 err = zio_wait(zio); 608 if (err) { 609 dmu_buf_rele_array(dbp, nblks, tag); 610 return (err); 611 } 612 613 /* wait for other io to complete */ 614 for (i = 0; i < nblks; i++) { 615 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i]; 616 mutex_enter(&db->db_mtx); 617 while (db->db_state == DB_READ || 618 db->db_state == DB_FILL) 619 cv_wait(&db->db_changed, &db->db_mtx); 620 if (db->db_state == DB_UNCACHED) 621 err = SET_ERROR(EIO); 622 mutex_exit(&db->db_mtx); 623 if (err) { 624 dmu_buf_rele_array(dbp, nblks, tag); 625 return (err); 626 } 627 } 628 } 629 630 *numbufsp = nblks; 631 *dbpp = dbp; 632 return (0); 633 } 634 635 int 636 dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset, 637 uint64_t length, int read, const void *tag, int *numbufsp, 638 dmu_buf_t ***dbpp, dmu_flags_t flags) 639 { 640 dnode_t *dn; 641 int err; 642 643 err = dnode_hold(os, object, FTAG, &dn); 644 if (err) 645 return (err); 646 647 err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag, 648 numbufsp, dbpp, flags); 649 650 dnode_rele(dn, FTAG); 651 652 return (err); 653 } 654 655 int 656 dmu_buf_hold_array_by_bonus(dmu_buf_t *db_fake, uint64_t offset, 657 uint64_t length, boolean_t read, const void *tag, int *numbufsp, 658 dmu_buf_t ***dbpp, dmu_flags_t flags) 659 { 660 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 661 int err; 662 663 DB_DNODE_ENTER(db); 664 err = dmu_buf_hold_array_by_dnode(DB_DNODE(db), offset, length, read, 665 tag, numbufsp, dbpp, flags); 666 DB_DNODE_EXIT(db); 667 668 return (err); 669 } 670 671 void 672 dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, const void *tag) 673 { 674 int i; 675 dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake; 676 677 if (numbufs == 0) 678 return; 679 680 for (i = 0; i < numbufs; i++) { 681 if (dbp[i]) 682 dbuf_rele(dbp[i], tag); 683 } 684 685 kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs); 686 } 687 688 /* 689 * Issue prefetch I/Os for the given blocks. If level is greater than 0, the 690 * indirect blocks prefetched will be those that point to the blocks containing 691 * the data starting at offset, and continuing to offset + len. If the range 692 * is too long, prefetch the first dmu_prefetch_max bytes as requested, while 693 * for the rest only a higher level, also fitting within dmu_prefetch_max. It 694 * should primarily help random reads, since for long sequential reads there is 695 * a speculative prefetcher. 696 * 697 * Note that if the indirect blocks above the blocks being prefetched are not 698 * in cache, they will be asynchronously read in. Dnode read by dnode_hold() 699 * is currently synchronous. 700 */ 701 void 702 dmu_prefetch(objset_t *os, uint64_t object, int64_t level, uint64_t offset, 703 uint64_t len, zio_priority_t pri) 704 { 705 dnode_t *dn; 706 707 if (dmu_prefetch_max == 0 || len == 0) { 708 dmu_prefetch_dnode(os, object, pri); 709 return; 710 } 711 712 if (dnode_hold(os, object, FTAG, &dn) != 0) 713 return; 714 715 dmu_prefetch_by_dnode(dn, level, offset, len, pri); 716 717 dnode_rele(dn, FTAG); 718 } 719 720 void 721 dmu_prefetch_by_dnode(dnode_t *dn, int64_t level, uint64_t offset, 722 uint64_t len, zio_priority_t pri) 723 { 724 int64_t level2 = level; 725 uint64_t start, end, start2, end2; 726 727 /* 728 * Depending on len we may do two prefetches: blocks [start, end) at 729 * level, and following blocks [start2, end2) at higher level2. 730 */ 731 rw_enter(&dn->dn_struct_rwlock, RW_READER); 732 if (dn->dn_datablkshift != 0) { 733 734 /* 735 * Limit prefetch to present blocks. 736 */ 737 uint64_t size = (dn->dn_maxblkid + 1) << dn->dn_datablkshift; 738 if (offset >= size) { 739 rw_exit(&dn->dn_struct_rwlock); 740 return; 741 } 742 if (offset + len < offset || offset + len > size) 743 len = size - offset; 744 745 /* 746 * The object has multiple blocks. Calculate the full range 747 * of blocks [start, end2) and then split it into two parts, 748 * so that the first [start, end) fits into dmu_prefetch_max. 749 */ 750 start = dbuf_whichblock(dn, level, offset); 751 end2 = dbuf_whichblock(dn, level, offset + len - 1) + 1; 752 uint8_t ibs = dn->dn_indblkshift; 753 uint8_t bs = (level == 0) ? dn->dn_datablkshift : ibs; 754 uint_t limit = P2ROUNDUP(dmu_prefetch_max, 1 << bs) >> bs; 755 start2 = end = MIN(end2, start + limit); 756 757 /* 758 * Find level2 where [start2, end2) fits into dmu_prefetch_max. 759 */ 760 uint8_t ibps = ibs - SPA_BLKPTRSHIFT; 761 limit = P2ROUNDUP(dmu_prefetch_max, 1 << ibs) >> ibs; 762 if (limit == 0) 763 end2 = start2; 764 do { 765 level2++; 766 start2 = P2ROUNDUP(start2, 1 << ibps) >> ibps; 767 end2 = P2ROUNDUP(end2, 1 << ibps) >> ibps; 768 } while (end2 - start2 > limit); 769 } else { 770 /* There is only one block. Prefetch it or nothing. */ 771 start = start2 = end2 = 0; 772 end = start + (level == 0 && offset < dn->dn_datablksz); 773 } 774 775 for (uint64_t i = start; i < end; i++) 776 dbuf_prefetch(dn, level, i, pri, 0); 777 for (uint64_t i = start2; i < end2; i++) 778 dbuf_prefetch(dn, level2, i, pri, 0); 779 rw_exit(&dn->dn_struct_rwlock); 780 } 781 782 /* 783 * Prime a prefetch for sequential accesses from offset for at least len bytes. 784 */ 785 void 786 dmu_prefetch_stream(objset_t *os, uint64_t object, uint64_t offset, 787 uint64_t len, boolean_t start_now) 788 { 789 dnode_t *dn; 790 791 if (dnode_hold(os, object, FTAG, &dn) != 0) 792 return; 793 dmu_prefetch_stream_by_dnode(dn, offset, len, start_now); 794 dnode_rele(dn, FTAG); 795 } 796 797 void 798 dmu_prefetch_stream_by_dnode(dnode_t *dn, uint64_t offset, uint64_t len, 799 boolean_t start_now) 800 { 801 rw_enter(&dn->dn_struct_rwlock, RW_READER); 802 if (dn->dn_datablkshift != 0) { 803 uint64_t start = dbuf_whichblock(dn, 0, offset); 804 if (len == 0) { 805 if (dmu_zfetch_prime(&dn->dn_zfetch, start, start) && 806 start_now) { 807 dmu_zfetch(&dn->dn_zfetch, start, 0, B_TRUE, 808 B_TRUE, B_TRUE, B_FALSE); 809 } 810 } else { 811 uint64_t end = dbuf_whichblock(dn, 0, offset + len - 1); 812 if (start == end) { 813 if (start_now) { 814 dbuf_prefetch(dn, 0, start, 815 ZIO_PRIORITY_ASYNC_READ, 0); 816 } 817 } else if ( 818 dmu_zfetch_prime(&dn->dn_zfetch, start, end + 1) && 819 start_now) { 820 dmu_zfetch(&dn->dn_zfetch, start, 0, B_TRUE, 821 B_TRUE, B_TRUE, B_FALSE); 822 } 823 } 824 } else if (offset < dn->dn_datablksz && start_now) { 825 dbuf_prefetch(dn, 0, 0, ZIO_PRIORITY_ASYNC_READ, 0); 826 } 827 rw_exit(&dn->dn_struct_rwlock); 828 } 829 830 typedef struct { 831 kmutex_t dpa_lock; 832 kcondvar_t dpa_cv; 833 uint64_t dpa_pending_io; 834 } dmu_prefetch_arg_t; 835 836 static void 837 dmu_prefetch_done(void *arg, uint64_t level, uint64_t blkid, boolean_t issued) 838 { 839 (void) level; (void) blkid; (void)issued; 840 dmu_prefetch_arg_t *dpa = arg; 841 842 ASSERT0(level); 843 844 mutex_enter(&dpa->dpa_lock); 845 ASSERT3U(dpa->dpa_pending_io, >, 0); 846 if (--dpa->dpa_pending_io == 0) 847 cv_broadcast(&dpa->dpa_cv); 848 mutex_exit(&dpa->dpa_lock); 849 } 850 851 static void 852 dmu_prefetch_wait_by_dnode(dnode_t *dn, uint64_t offset, uint64_t len) 853 { 854 dmu_prefetch_arg_t dpa; 855 856 mutex_init(&dpa.dpa_lock, NULL, MUTEX_DEFAULT, NULL); 857 cv_init(&dpa.dpa_cv, NULL, CV_DEFAULT, NULL); 858 859 rw_enter(&dn->dn_struct_rwlock, RW_READER); 860 861 uint64_t start = dbuf_whichblock(dn, 0, offset); 862 uint64_t end = dbuf_whichblock(dn, 0, offset + len - 1) + 1; 863 dpa.dpa_pending_io = end - start; 864 865 for (uint64_t blk = start; blk < end; blk++) { 866 (void) dbuf_prefetch_impl(dn, 0, blk, ZIO_PRIORITY_ASYNC_READ, 867 0, dmu_prefetch_done, &dpa); 868 } 869 870 rw_exit(&dn->dn_struct_rwlock); 871 872 /* wait for prefetch L0 reads to finish */ 873 mutex_enter(&dpa.dpa_lock); 874 while (dpa.dpa_pending_io > 0) { 875 cv_wait(&dpa.dpa_cv, &dpa.dpa_lock); 876 877 } 878 mutex_exit(&dpa.dpa_lock); 879 880 mutex_destroy(&dpa.dpa_lock); 881 cv_destroy(&dpa.dpa_cv); 882 } 883 884 /* 885 * Issue prefetch I/Os for the given L0 block range and wait for the I/O 886 * to complete. This does not enforce dmu_prefetch_max and will prefetch 887 * the entire range. The blocks are read from disk into the ARC but no 888 * decompression occurs (i.e., the dbuf cache is not required). 889 */ 890 int 891 dmu_prefetch_wait(objset_t *os, uint64_t object, uint64_t offset, uint64_t size) 892 { 893 dnode_t *dn; 894 int err = 0; 895 896 err = dnode_hold(os, object, FTAG, &dn); 897 if (err != 0) 898 return (err); 899 900 /* 901 * Chunk the requests (16 indirects worth) so that we can be 902 * interrupted. Prefetch at least SPA_MAXBLOCKSIZE at a time 903 * to better utilize pools with smaller block sizes. 904 */ 905 uint64_t chunksize; 906 if (dn->dn_indblkshift) { 907 uint64_t nbps = bp_span_in_blocks(dn->dn_indblkshift, 1); 908 chunksize = (nbps * 16) << dn->dn_datablkshift; 909 chunksize = MAX(chunksize, SPA_MAXBLOCKSIZE); 910 } else { 911 chunksize = dn->dn_datablksz; 912 } 913 914 while (size > 0) { 915 uint64_t mylen = MIN(size, chunksize); 916 917 dmu_prefetch_wait_by_dnode(dn, offset, mylen); 918 919 offset += mylen; 920 size -= mylen; 921 922 if (issig()) { 923 err = SET_ERROR(EINTR); 924 break; 925 } 926 } 927 928 dnode_rele(dn, FTAG); 929 930 return (err); 931 } 932 933 /* 934 * Issue prefetch I/Os for the given object's dnode. 935 */ 936 void 937 dmu_prefetch_dnode(objset_t *os, uint64_t object, zio_priority_t pri) 938 { 939 if (object == 0 || object >= DN_MAX_OBJECT) 940 return; 941 942 dnode_t *dn = DMU_META_DNODE(os); 943 rw_enter(&dn->dn_struct_rwlock, RW_READER); 944 uint64_t blkid = dbuf_whichblock(dn, 0, object * sizeof (dnode_phys_t)); 945 dbuf_prefetch(dn, 0, blkid, pri, 0); 946 rw_exit(&dn->dn_struct_rwlock); 947 } 948 949 /* 950 * Advisory cache eviction for a byte range of an object. 951 */ 952 void 953 dmu_evict_range(objset_t *os, uint64_t object, uint64_t offset, uint64_t len) 954 { 955 dnode_t *dn; 956 957 if (len == 0) 958 return; 959 if (dnode_hold(os, object, FTAG, &dn) != 0) 960 return; 961 962 /* 963 * Exclude the last block if the range end is not block-aligned: 964 * a sequential access may continue into that block. The first 965 * block is included even when partially covered since backwards 966 * access patterns are rare. 967 */ 968 rw_enter(&dn->dn_struct_rwlock, RW_READER); 969 uint64_t start = dbuf_whichblock(dn, 0, offset); 970 uint64_t end = dbuf_whichblock(dn, 0, offset + len); 971 if (end > start) 972 dbuf_evict_range(dn, start, end - 1); 973 rw_exit(&dn->dn_struct_rwlock); 974 975 dnode_rele(dn, FTAG); 976 } 977 978 /* 979 * Get the next "chunk" of file data to free. We traverse the file from 980 * the end so that the file gets shorter over time (if we crash in the 981 * middle, this will leave us in a better state). We find allocated file 982 * data by simply searching the allocated level 1 indirects. 983 * 984 * On input, *start should be the first offset that does not need to be 985 * freed (e.g. "offset + length"). On return, *start will be the first 986 * offset that should be freed and l1blks is set to the number of level 1 987 * indirect blocks found within the chunk. 988 */ 989 static int 990 get_next_chunk(dnode_t *dn, uint64_t *start, uint64_t minimum, uint64_t *l1blks) 991 { 992 uint64_t blks; 993 uint64_t maxblks = DMU_MAX_ACCESS >> (dn->dn_indblkshift + 1); 994 /* bytes of data covered by a level-1 indirect block */ 995 uint64_t iblkrange = (uint64_t)dn->dn_datablksz * 996 EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT); 997 998 ASSERT3U(minimum, <=, *start); 999 1000 /* dn_nlevels == 1 means we don't have any L1 blocks */ 1001 if (dn->dn_nlevels <= 1) { 1002 *l1blks = 0; 1003 *start = minimum; 1004 return (0); 1005 } 1006 1007 /* 1008 * Check if we can free the entire range assuming that all of the 1009 * L1 blocks in this range have data. If we can, we use this 1010 * worst case value as an estimate so we can avoid having to look 1011 * at the object's actual data. 1012 */ 1013 uint64_t total_l1blks = 1014 (roundup(*start, iblkrange) - (minimum / iblkrange * iblkrange)) / 1015 iblkrange; 1016 if (total_l1blks <= maxblks) { 1017 *l1blks = total_l1blks; 1018 *start = minimum; 1019 return (0); 1020 } 1021 ASSERT(ISP2(iblkrange)); 1022 1023 for (blks = 0; *start > minimum && blks < maxblks; blks++) { 1024 int err; 1025 1026 /* 1027 * dnode_next_offset(BACKWARDS) will find an allocated L1 1028 * indirect block at or before the input offset. We must 1029 * decrement *start so that it is at the end of the region 1030 * to search. 1031 */ 1032 (*start)--; 1033 1034 err = dnode_next_offset(dn, 1035 DNODE_FIND_BACKWARDS, start, 2, 1, 0); 1036 1037 /* if there are no indirect blocks before start, we are done */ 1038 if (err == ESRCH) { 1039 *start = minimum; 1040 break; 1041 } else if (err != 0) { 1042 *l1blks = blks; 1043 return (err); 1044 } 1045 1046 /* set start to the beginning of this L1 indirect */ 1047 *start = P2ALIGN_TYPED(*start, iblkrange, uint64_t); 1048 } 1049 if (*start < minimum) 1050 *start = minimum; 1051 *l1blks = blks; 1052 1053 return (0); 1054 } 1055 1056 /* 1057 * If this objset is of type OST_ZFS return true if vfs's unmounted flag is set, 1058 * otherwise return false. 1059 * Used below in dmu_free_long_range_impl() to enable abort when unmounting 1060 */ 1061 static boolean_t 1062 dmu_objset_zfs_unmounting(objset_t *os) 1063 { 1064 #ifdef _KERNEL 1065 if (dmu_objset_type(os) == DMU_OST_ZFS) 1066 return (zfs_get_vfs_flag_unmounted(os)); 1067 #else 1068 (void) os; 1069 #endif 1070 return (B_FALSE); 1071 } 1072 1073 static int 1074 dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset, 1075 uint64_t length) 1076 { 1077 uint64_t object_size; 1078 int err; 1079 uint64_t dirty_frees_threshold; 1080 dsl_pool_t *dp = dmu_objset_pool(os); 1081 1082 if (dn == NULL) 1083 return (SET_ERROR(EINVAL)); 1084 1085 object_size = (dn->dn_maxblkid + 1) * dn->dn_datablksz; 1086 if (offset >= object_size) 1087 return (0); 1088 1089 if (zfs_per_txg_dirty_frees_percent <= 100) 1090 dirty_frees_threshold = 1091 zfs_per_txg_dirty_frees_percent * zfs_dirty_data_max / 100; 1092 else 1093 dirty_frees_threshold = zfs_dirty_data_max / 20; 1094 1095 if (length == DMU_OBJECT_END || offset + length > object_size) 1096 length = object_size - offset; 1097 1098 while (length != 0) { 1099 uint64_t chunk_end, chunk_begin, chunk_len; 1100 uint64_t l1blks; 1101 dmu_tx_t *tx; 1102 1103 if (dmu_objset_zfs_unmounting(dn->dn_objset)) 1104 return (SET_ERROR(EINTR)); 1105 1106 chunk_end = chunk_begin = offset + length; 1107 1108 /* move chunk_begin backwards to the beginning of this chunk */ 1109 err = get_next_chunk(dn, &chunk_begin, offset, &l1blks); 1110 if (err) 1111 return (err); 1112 ASSERT3U(chunk_begin, >=, offset); 1113 ASSERT3U(chunk_begin, <=, chunk_end); 1114 1115 chunk_len = chunk_end - chunk_begin; 1116 1117 tx = dmu_tx_create(os); 1118 dmu_tx_hold_free(tx, dn->dn_object, chunk_begin, chunk_len); 1119 1120 /* 1121 * Mark this transaction as typically resulting in a net 1122 * reduction in space used. 1123 */ 1124 dmu_tx_mark_netfree(tx); 1125 err = dmu_tx_assign(tx, DMU_TX_WAIT); 1126 if (err) { 1127 dmu_tx_abort(tx); 1128 return (err); 1129 } 1130 1131 uint64_t txg = dmu_tx_get_txg(tx); 1132 1133 mutex_enter(&dp->dp_lock); 1134 uint64_t long_free_dirty = 1135 dp->dp_long_free_dirty_pertxg[txg & TXG_MASK]; 1136 mutex_exit(&dp->dp_lock); 1137 1138 /* 1139 * To avoid filling up a TXG with just frees, wait for 1140 * the next TXG to open before freeing more chunks if 1141 * we have reached the threshold of frees. 1142 */ 1143 if (dirty_frees_threshold != 0 && 1144 long_free_dirty >= dirty_frees_threshold) { 1145 DMU_TX_STAT_BUMP(dmu_tx_dirty_frees_delay); 1146 dmu_tx_commit(tx); 1147 txg_wait_open(dp, 0, B_TRUE); 1148 continue; 1149 } 1150 1151 /* 1152 * In order to prevent unnecessary write throttling, for each 1153 * TXG, we track the cumulative size of L1 blocks being dirtied 1154 * in dnode_free_range() below. We compare this number to a 1155 * tunable threshold, past which we prevent new L1 dirty freeing 1156 * blocks from being added into the open TXG. See 1157 * dmu_free_long_range_impl() for details. The threshold 1158 * prevents write throttle activation due to dirty freeing L1 1159 * blocks taking up a large percentage of zfs_dirty_data_max. 1160 */ 1161 mutex_enter(&dp->dp_lock); 1162 dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] += 1163 l1blks << dn->dn_indblkshift; 1164 mutex_exit(&dp->dp_lock); 1165 DTRACE_PROBE3(free__long__range, 1166 uint64_t, long_free_dirty, uint64_t, chunk_len, 1167 uint64_t, txg); 1168 dnode_free_range(dn, chunk_begin, chunk_len, tx); 1169 1170 dmu_tx_commit(tx); 1171 1172 length -= chunk_len; 1173 } 1174 return (0); 1175 } 1176 1177 int 1178 dmu_free_long_range(objset_t *os, uint64_t object, 1179 uint64_t offset, uint64_t length) 1180 { 1181 dnode_t *dn; 1182 int err; 1183 1184 err = dnode_hold(os, object, FTAG, &dn); 1185 if (err != 0) 1186 return (err); 1187 err = dmu_free_long_range_impl(os, dn, offset, length); 1188 1189 /* 1190 * It is important to zero out the maxblkid when freeing the entire 1191 * file, so that (a) subsequent calls to dmu_free_long_range_impl() 1192 * will take the fast path, and (b) dnode_reallocate() can verify 1193 * that the entire file has been freed. 1194 */ 1195 if (err == 0 && offset == 0 && length == DMU_OBJECT_END) 1196 dn->dn_maxblkid = 0; 1197 1198 dnode_rele(dn, FTAG); 1199 return (err); 1200 } 1201 1202 int 1203 dmu_free_long_object(objset_t *os, uint64_t object) 1204 { 1205 dmu_tx_t *tx; 1206 int err; 1207 1208 err = dmu_free_long_range(os, object, 0, DMU_OBJECT_END); 1209 if (err != 0) 1210 return (err); 1211 1212 tx = dmu_tx_create(os); 1213 dmu_tx_hold_bonus(tx, object); 1214 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END); 1215 dmu_tx_mark_netfree(tx); 1216 err = dmu_tx_assign(tx, DMU_TX_WAIT); 1217 if (err == 0) { 1218 err = dmu_object_free(os, object, tx); 1219 dmu_tx_commit(tx); 1220 } else { 1221 dmu_tx_abort(tx); 1222 } 1223 1224 return (err); 1225 } 1226 1227 int 1228 dmu_free_range(objset_t *os, uint64_t object, uint64_t offset, 1229 uint64_t size, dmu_tx_t *tx) 1230 { 1231 dnode_t *dn; 1232 int err = dnode_hold(os, object, FTAG, &dn); 1233 if (err) 1234 return (err); 1235 ASSERT(offset < UINT64_MAX); 1236 ASSERT(size == DMU_OBJECT_END || size <= UINT64_MAX - offset); 1237 dnode_free_range(dn, offset, size, tx); 1238 dnode_rele(dn, FTAG); 1239 return (0); 1240 } 1241 1242 static int 1243 dmu_read_impl(dnode_t *dn, uint64_t offset, uint64_t size, 1244 void *buf, dmu_flags_t flags) 1245 { 1246 dmu_buf_t **dbp; 1247 int numbufs, err = 0; 1248 1249 /* 1250 * Deal with odd block sizes, where there can't be data past the first 1251 * block. If we ever do the tail block optimization, we will need to 1252 * handle that here as well. 1253 */ 1254 if (dn->dn_maxblkid == 0) { 1255 uint64_t newsz = offset > dn->dn_datablksz ? 0 : 1256 MIN(size, dn->dn_datablksz - offset); 1257 memset((char *)buf + newsz, 0, size - newsz); 1258 size = newsz; 1259 } 1260 1261 if (size == 0) 1262 return (0); 1263 1264 /* Allow Direct I/O when requested and properly aligned */ 1265 if ((flags & DMU_DIRECTIO) && zfs_dio_page_aligned(buf) && 1266 zfs_dio_aligned(offset, size, PAGESIZE)) { 1267 abd_t *data = abd_get_from_buf(buf, size); 1268 err = dmu_read_abd(dn, offset, size, data, flags); 1269 abd_free(data); 1270 return (err); 1271 } 1272 flags &= ~DMU_DIRECTIO; 1273 1274 while (size > 0) { 1275 uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2); 1276 int i; 1277 1278 /* 1279 * NB: we could do this block-at-a-time, but it's nice 1280 * to be reading in parallel. 1281 */ 1282 err = dmu_buf_hold_array_by_dnode(dn, offset, mylen, 1283 TRUE, FTAG, &numbufs, &dbp, flags); 1284 if (err) 1285 break; 1286 1287 for (i = 0; i < numbufs; i++) { 1288 uint64_t tocpy; 1289 int64_t bufoff; 1290 dmu_buf_t *db = dbp[i]; 1291 1292 ASSERT(size > 0); 1293 1294 bufoff = offset - db->db_offset; 1295 tocpy = MIN(db->db_size - bufoff, size); 1296 1297 ASSERT(db->db_data != NULL); 1298 (void) memcpy(buf, (char *)db->db_data + bufoff, tocpy); 1299 1300 offset += tocpy; 1301 size -= tocpy; 1302 buf = (char *)buf + tocpy; 1303 } 1304 dmu_buf_rele_array(dbp, numbufs, FTAG); 1305 } 1306 return (err); 1307 } 1308 1309 int 1310 dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 1311 void *buf, dmu_flags_t flags) 1312 { 1313 dnode_t *dn; 1314 int err; 1315 1316 err = dnode_hold(os, object, FTAG, &dn); 1317 if (err != 0) 1318 return (err); 1319 1320 err = dmu_read_impl(dn, offset, size, buf, flags); 1321 dnode_rele(dn, FTAG); 1322 return (err); 1323 } 1324 1325 int 1326 dmu_read_by_dnode(dnode_t *dn, uint64_t offset, uint64_t size, void *buf, 1327 dmu_flags_t flags) 1328 { 1329 return (dmu_read_impl(dn, offset, size, buf, flags)); 1330 } 1331 1332 static void 1333 dmu_write_impl(dmu_buf_t **dbp, int numbufs, uint64_t offset, uint64_t size, 1334 const void *buf, dmu_tx_t *tx, dmu_flags_t flags) 1335 { 1336 int i; 1337 1338 for (i = 0; i < numbufs; i++) { 1339 uint64_t tocpy; 1340 int64_t bufoff; 1341 dmu_buf_t *db = dbp[i]; 1342 1343 ASSERT(size > 0); 1344 1345 bufoff = offset - db->db_offset; 1346 tocpy = MIN(db->db_size - bufoff, size); 1347 1348 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size); 1349 1350 if (tocpy == db->db_size) { 1351 dmu_buf_will_fill_flags(db, tx, B_FALSE, flags); 1352 } else { 1353 if (i == numbufs - 1 && bufoff + tocpy < db->db_size) { 1354 if (bufoff == 0) 1355 flags |= DMU_PARTIAL_FIRST; 1356 else 1357 flags |= DMU_PARTIAL_MORE; 1358 } 1359 dmu_buf_will_dirty_flags(db, tx, flags); 1360 } 1361 1362 ASSERT(db->db_data != NULL); 1363 (void) memcpy((char *)db->db_data + bufoff, buf, tocpy); 1364 1365 if (tocpy == db->db_size) 1366 dmu_buf_fill_done(db, tx, B_FALSE); 1367 1368 offset += tocpy; 1369 size -= tocpy; 1370 buf = (char *)buf + tocpy; 1371 } 1372 } 1373 1374 void 1375 dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 1376 const void *buf, dmu_tx_t *tx, dmu_flags_t flags) 1377 { 1378 dmu_buf_t **dbp; 1379 int numbufs; 1380 1381 if (size == 0) 1382 return; 1383 1384 VERIFY0(dmu_buf_hold_array(os, object, offset, size, 1385 FALSE, FTAG, &numbufs, &dbp, flags)); 1386 dmu_write_impl(dbp, numbufs, offset, size, buf, tx, flags); 1387 dmu_buf_rele_array(dbp, numbufs, FTAG); 1388 } 1389 1390 int 1391 dmu_write_by_dnode(dnode_t *dn, uint64_t offset, uint64_t size, 1392 const void *buf, dmu_tx_t *tx, dmu_flags_t flags) 1393 { 1394 dmu_buf_t **dbp; 1395 int numbufs; 1396 int error; 1397 1398 if (size == 0) 1399 return (0); 1400 1401 /* Allow Direct I/O when requested and properly aligned */ 1402 if ((flags & DMU_DIRECTIO) && zfs_dio_page_aligned((void *)buf) && 1403 zfs_dio_aligned(offset, size, dn->dn_datablksz)) { 1404 abd_t *data = abd_get_from_buf((void *)buf, size); 1405 error = dmu_write_abd(dn, offset, size, data, flags, tx); 1406 abd_free(data); 1407 return (error); 1408 } 1409 flags &= ~DMU_DIRECTIO; 1410 1411 VERIFY0(dmu_buf_hold_array_by_dnode(dn, offset, size, 1412 FALSE, FTAG, &numbufs, &dbp, flags)); 1413 dmu_write_impl(dbp, numbufs, offset, size, buf, tx, flags); 1414 dmu_buf_rele_array(dbp, numbufs, FTAG); 1415 return (0); 1416 } 1417 1418 void 1419 dmu_prealloc(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 1420 dmu_tx_t *tx) 1421 { 1422 dmu_buf_t **dbp; 1423 int numbufs, i; 1424 1425 if (size == 0) 1426 return; 1427 1428 VERIFY0(dmu_buf_hold_array(os, object, offset, size, 1429 FALSE, FTAG, &numbufs, &dbp, DMU_READ_PREFETCH)); 1430 1431 for (i = 0; i < numbufs; i++) { 1432 dmu_buf_t *db = dbp[i]; 1433 1434 dmu_buf_will_not_fill(db, tx); 1435 } 1436 dmu_buf_rele_array(dbp, numbufs, FTAG); 1437 } 1438 1439 void 1440 dmu_write_embedded(objset_t *os, uint64_t object, uint64_t offset, 1441 void *data, uint8_t etype, uint8_t comp, int uncompressed_size, 1442 int compressed_size, int byteorder, dmu_tx_t *tx) 1443 { 1444 dmu_buf_t *db; 1445 1446 ASSERT3U(etype, <, NUM_BP_EMBEDDED_TYPES); 1447 ASSERT3U(comp, <, ZIO_COMPRESS_FUNCTIONS); 1448 VERIFY0(dmu_buf_hold_noread(os, object, offset, 1449 FTAG, &db)); 1450 1451 dmu_buf_write_embedded(db, 1452 data, (bp_embedded_type_t)etype, (enum zio_compress)comp, 1453 uncompressed_size, compressed_size, byteorder, tx); 1454 1455 dmu_buf_rele(db, FTAG); 1456 } 1457 1458 void 1459 dmu_redact(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 1460 dmu_tx_t *tx) 1461 { 1462 int numbufs, i; 1463 dmu_buf_t **dbp; 1464 1465 VERIFY0(dmu_buf_hold_array(os, object, offset, size, FALSE, FTAG, 1466 &numbufs, &dbp, DMU_READ_PREFETCH)); 1467 for (i = 0; i < numbufs; i++) 1468 dmu_buf_redact(dbp[i], tx); 1469 dmu_buf_rele_array(dbp, numbufs, FTAG); 1470 } 1471 1472 #ifdef _KERNEL 1473 int 1474 dmu_read_uio_dnode(dnode_t *dn, zfs_uio_t *uio, uint64_t size, 1475 dmu_flags_t flags) 1476 { 1477 dmu_buf_t **dbp; 1478 int numbufs, i, err; 1479 1480 if ((flags & DMU_DIRECTIO) && (uio->uio_extflg & UIO_DIRECT)) 1481 return (dmu_read_uio_direct(dn, uio, size, flags)); 1482 flags &= ~DMU_DIRECTIO; 1483 1484 /* 1485 * NB: we could do this block-at-a-time, but it's nice 1486 * to be reading in parallel. 1487 */ 1488 err = dmu_buf_hold_array_by_dnode(dn, zfs_uio_offset(uio), size, 1489 TRUE, FTAG, &numbufs, &dbp, flags); 1490 if (err) 1491 return (err); 1492 1493 for (i = 0; i < numbufs; i++) { 1494 uint64_t tocpy; 1495 int64_t bufoff; 1496 dmu_buf_t *db = dbp[i]; 1497 1498 ASSERT(size > 0); 1499 1500 bufoff = zfs_uio_offset(uio) - db->db_offset; 1501 tocpy = MIN(db->db_size - bufoff, size); 1502 1503 ASSERT(db->db_data != NULL); 1504 err = zfs_uio_fault_move((char *)db->db_data + bufoff, tocpy, 1505 UIO_READ, uio); 1506 1507 if (err) 1508 break; 1509 1510 size -= tocpy; 1511 } 1512 dmu_buf_rele_array(dbp, numbufs, FTAG); 1513 1514 return (err); 1515 } 1516 1517 /* 1518 * Read 'size' bytes into the uio buffer. 1519 * From object zdb->db_object. 1520 * Starting at zfs_uio_offset(uio). 1521 * 1522 * If the caller already has a dbuf in the target object 1523 * (e.g. its bonus buffer), this routine is faster than dmu_read_uio(), 1524 * because we don't have to find the dnode_t for the object. 1525 */ 1526 int 1527 dmu_read_uio_dbuf(dmu_buf_t *zdb, zfs_uio_t *uio, uint64_t size, 1528 dmu_flags_t flags) 1529 { 1530 dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb; 1531 int err; 1532 1533 if (size == 0) 1534 return (0); 1535 1536 DB_DNODE_ENTER(db); 1537 err = dmu_read_uio_dnode(DB_DNODE(db), uio, size, flags); 1538 DB_DNODE_EXIT(db); 1539 1540 return (err); 1541 } 1542 1543 /* 1544 * Read 'size' bytes into the uio buffer. 1545 * From the specified object 1546 * Starting at offset zfs_uio_offset(uio). 1547 */ 1548 int 1549 dmu_read_uio(objset_t *os, uint64_t object, zfs_uio_t *uio, uint64_t size, 1550 dmu_flags_t flags) 1551 { 1552 dnode_t *dn; 1553 int err; 1554 1555 if (size == 0) 1556 return (0); 1557 1558 err = dnode_hold(os, object, FTAG, &dn); 1559 if (err) 1560 return (err); 1561 1562 err = dmu_read_uio_dnode(dn, uio, size, flags); 1563 1564 dnode_rele(dn, FTAG); 1565 1566 return (err); 1567 } 1568 1569 int 1570 dmu_write_uio_dnode(dnode_t *dn, zfs_uio_t *uio, uint64_t size, dmu_tx_t *tx, 1571 dmu_flags_t flags) 1572 { 1573 dmu_buf_t **dbp; 1574 int numbufs; 1575 int err = 0; 1576 uint64_t write_size; 1577 dmu_flags_t oflags = flags; 1578 1579 top: 1580 write_size = size; 1581 1582 /* 1583 * We only allow Direct I/O writes to happen if we are block 1584 * sized aligned. Otherwise, we pass the write off to the ARC. 1585 */ 1586 if ((flags & DMU_DIRECTIO) && (uio->uio_extflg & UIO_DIRECT) && 1587 (write_size >= dn->dn_datablksz)) { 1588 if (zfs_dio_aligned(zfs_uio_offset(uio), write_size, 1589 dn->dn_datablksz)) { 1590 return (dmu_write_uio_direct(dn, uio, size, flags, tx)); 1591 } else if (write_size > dn->dn_datablksz && 1592 zfs_dio_offset_aligned(zfs_uio_offset(uio), 1593 dn->dn_datablksz)) { 1594 write_size = 1595 dn->dn_datablksz * (write_size / dn->dn_datablksz); 1596 err = dmu_write_uio_direct(dn, uio, write_size, flags, 1597 tx); 1598 if (err == 0) { 1599 size -= write_size; 1600 goto top; 1601 } else { 1602 return (err); 1603 } 1604 } else { 1605 write_size = 1606 P2PHASE(zfs_uio_offset(uio), dn->dn_datablksz); 1607 } 1608 } 1609 flags &= ~DMU_DIRECTIO; 1610 1611 err = dmu_buf_hold_array_by_dnode(dn, zfs_uio_offset(uio), write_size, 1612 FALSE, FTAG, &numbufs, &dbp, flags); 1613 if (err) 1614 return (err); 1615 1616 for (int i = 0; i < numbufs; i++) { 1617 uint64_t tocpy; 1618 int64_t bufoff; 1619 dmu_buf_t *db = dbp[i]; 1620 1621 ASSERT(write_size > 0); 1622 1623 offset_t off = zfs_uio_offset(uio); 1624 bufoff = off - db->db_offset; 1625 tocpy = MIN(db->db_size - bufoff, write_size); 1626 1627 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size); 1628 1629 if (tocpy == db->db_size) { 1630 dmu_buf_will_fill_flags(db, tx, B_TRUE, flags); 1631 } else { 1632 if (i == numbufs - 1 && bufoff + tocpy < db->db_size) { 1633 if (bufoff == 0) 1634 flags |= DMU_PARTIAL_FIRST; 1635 else 1636 flags |= DMU_PARTIAL_MORE; 1637 } 1638 dmu_buf_will_dirty_flags(db, tx, flags); 1639 } 1640 1641 ASSERT(db->db_data != NULL); 1642 err = zfs_uio_fault_move((char *)db->db_data + bufoff, 1643 tocpy, UIO_WRITE, uio); 1644 1645 if (tocpy == db->db_size && dmu_buf_fill_done(db, tx, err)) { 1646 /* The fill was reverted. Undo any uio progress. */ 1647 zfs_uio_advance(uio, off - zfs_uio_offset(uio)); 1648 } 1649 1650 if (err) 1651 break; 1652 1653 write_size -= tocpy; 1654 size -= tocpy; 1655 } 1656 1657 IMPLY(err == 0, write_size == 0); 1658 1659 dmu_buf_rele_array(dbp, numbufs, FTAG); 1660 1661 if ((oflags & DMU_DIRECTIO) && (uio->uio_extflg & UIO_DIRECT) && 1662 err == 0 && size > 0) { 1663 flags = oflags; 1664 goto top; 1665 } 1666 IMPLY(err == 0, size == 0); 1667 1668 return (err); 1669 } 1670 1671 /* 1672 * Write 'size' bytes from the uio buffer. 1673 * To object zdb->db_object. 1674 * Starting at offset zfs_uio_offset(uio). 1675 * 1676 * If the caller already has a dbuf in the target object 1677 * (e.g. its bonus buffer), this routine is faster than dmu_write_uio(), 1678 * because we don't have to find the dnode_t for the object. 1679 */ 1680 int 1681 dmu_write_uio_dbuf(dmu_buf_t *zdb, zfs_uio_t *uio, uint64_t size, 1682 dmu_tx_t *tx, dmu_flags_t flags) 1683 { 1684 dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb; 1685 int err; 1686 1687 if (size == 0) 1688 return (0); 1689 1690 DB_DNODE_ENTER(db); 1691 err = dmu_write_uio_dnode(DB_DNODE(db), uio, size, tx, flags); 1692 DB_DNODE_EXIT(db); 1693 1694 return (err); 1695 } 1696 1697 /* 1698 * Write 'size' bytes from the uio buffer. 1699 * To the specified object. 1700 * Starting at offset zfs_uio_offset(uio). 1701 */ 1702 int 1703 dmu_write_uio(objset_t *os, uint64_t object, zfs_uio_t *uio, uint64_t size, 1704 dmu_tx_t *tx, dmu_flags_t flags) 1705 { 1706 dnode_t *dn; 1707 int err; 1708 1709 if (size == 0) 1710 return (0); 1711 1712 err = dnode_hold(os, object, FTAG, &dn); 1713 if (err) 1714 return (err); 1715 1716 err = dmu_write_uio_dnode(dn, uio, size, tx, flags); 1717 1718 dnode_rele(dn, FTAG); 1719 1720 return (err); 1721 } 1722 #endif /* _KERNEL */ 1723 1724 static void 1725 dmu_cached_bps(spa_t *spa, blkptr_t *bps, uint_t nbps, 1726 uint64_t *l1sz, uint64_t *l2sz) 1727 { 1728 int cached_flags; 1729 1730 if (bps == NULL) 1731 return; 1732 1733 for (size_t blk_off = 0; blk_off < nbps; blk_off++) { 1734 blkptr_t *bp = &bps[blk_off]; 1735 1736 if (BP_IS_HOLE(bp)) 1737 continue; 1738 1739 cached_flags = arc_cached(spa, bp); 1740 if (cached_flags == 0) 1741 continue; 1742 1743 if ((cached_flags & (ARC_CACHED_IN_L1 | ARC_CACHED_IN_L2)) == 1744 ARC_CACHED_IN_L2) 1745 *l2sz += BP_GET_LSIZE(bp); 1746 else 1747 *l1sz += BP_GET_LSIZE(bp); 1748 } 1749 } 1750 1751 /* 1752 * Estimate DMU object cached size. 1753 */ 1754 int 1755 dmu_object_cached_size(objset_t *os, uint64_t object, 1756 uint64_t *l1sz, uint64_t *l2sz) 1757 { 1758 dnode_t *dn; 1759 dmu_object_info_t doi; 1760 int err = 0; 1761 1762 *l1sz = *l2sz = 0; 1763 1764 if (dnode_hold(os, object, FTAG, &dn) != 0) 1765 return (0); 1766 1767 if (dn->dn_nlevels < 2) { 1768 dnode_rele(dn, FTAG); 1769 return (0); 1770 } 1771 1772 dmu_object_info_from_dnode(dn, &doi); 1773 1774 for (uint64_t off = 0; off < doi.doi_max_offset && 1775 dmu_prefetch_max > 0; off += dmu_prefetch_max) { 1776 /* dbuf_read doesn't prefetch L1 blocks. */ 1777 dmu_prefetch_by_dnode(dn, 1, off, 1778 dmu_prefetch_max, ZIO_PRIORITY_SYNC_READ); 1779 } 1780 1781 /* 1782 * Hold all valid L1 blocks, asking ARC the status of each BP 1783 * contained in each such L1 block. 1784 */ 1785 uint_t nbps = bp_span_in_blocks(dn->dn_indblkshift, 1); 1786 uint64_t l1blks = 1 + (dn->dn_maxblkid / nbps); 1787 1788 rw_enter(&dn->dn_struct_rwlock, RW_READER); 1789 for (uint64_t blk = 0; blk < l1blks; blk++) { 1790 dmu_buf_impl_t *db = NULL; 1791 1792 if (issig()) { 1793 /* 1794 * On interrupt, get out, and bubble up EINTR 1795 */ 1796 err = EINTR; 1797 break; 1798 } 1799 1800 /* 1801 * If we get an i/o error here, the L1 can't be read, 1802 * and nothing under it could be cached, so we just 1803 * continue. Ignoring the error from dbuf_hold_impl 1804 * or from dbuf_read is then a reasonable choice. 1805 */ 1806 err = dbuf_hold_impl(dn, 1, blk, B_TRUE, B_FALSE, FTAG, &db); 1807 if (err != 0) { 1808 /* 1809 * ignore error and continue 1810 */ 1811 err = 0; 1812 continue; 1813 } 1814 1815 err = dbuf_read(db, NULL, DB_RF_CANFAIL); 1816 if (err == 0) { 1817 dmu_cached_bps(dmu_objset_spa(os), db->db.db_data, 1818 nbps, l1sz, l2sz); 1819 } 1820 /* 1821 * error may be ignored, and we continue 1822 */ 1823 err = 0; 1824 dbuf_rele(db, FTAG); 1825 } 1826 rw_exit(&dn->dn_struct_rwlock); 1827 1828 dnode_rele(dn, FTAG); 1829 return (err); 1830 } 1831 1832 /* 1833 * Allocate a loaned anonymous arc buffer. 1834 */ 1835 arc_buf_t * 1836 dmu_request_arcbuf(dmu_buf_t *handle, int size) 1837 { 1838 dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle; 1839 1840 return (arc_loan_buf(db->db_objset->os_spa, B_FALSE, size)); 1841 } 1842 1843 /* 1844 * Free a loaned arc buffer. 1845 */ 1846 void 1847 dmu_return_arcbuf(arc_buf_t *buf) 1848 { 1849 arc_return_buf(buf, FTAG); 1850 arc_buf_destroy(buf, FTAG); 1851 } 1852 1853 /* 1854 * A "lightweight" write is faster than a regular write (e.g. 1855 * dmu_write_by_dnode() or dmu_assign_arcbuf_by_dnode()), because it avoids the 1856 * CPU cost of creating a dmu_buf_impl_t and arc_buf_[hdr_]_t. However, the 1857 * data can not be read or overwritten until the transaction's txg has been 1858 * synced. This makes it appropriate for workloads that are known to be 1859 * (temporarily) write-only, like "zfs receive". 1860 * 1861 * A single block is written, starting at the specified offset in bytes. If 1862 * the call is successful, it returns 0 and the provided abd has been 1863 * consumed (the caller should not free it). 1864 */ 1865 int 1866 dmu_lightweight_write_by_dnode(dnode_t *dn, uint64_t offset, abd_t *abd, 1867 const zio_prop_t *zp, zio_flag_t flags, dmu_tx_t *tx) 1868 { 1869 dbuf_dirty_record_t *dr = 1870 dbuf_dirty_lightweight(dn, dbuf_whichblock(dn, 0, offset), tx); 1871 if (dr == NULL) 1872 return (SET_ERROR(EIO)); 1873 dr->dt.dll.dr_abd = abd; 1874 dr->dt.dll.dr_props = *zp; 1875 dr->dt.dll.dr_flags = flags; 1876 return (0); 1877 } 1878 1879 /* 1880 * When possible directly assign passed loaned arc buffer to a dbuf. 1881 * If this is not possible copy the contents of passed arc buf via 1882 * dmu_write(). 1883 */ 1884 int 1885 dmu_assign_arcbuf_by_dnode(dnode_t *dn, uint64_t offset, arc_buf_t *buf, 1886 dmu_tx_t *tx, dmu_flags_t flags) 1887 { 1888 dmu_buf_impl_t *db; 1889 objset_t *os = dn->dn_objset; 1890 uint32_t blksz = (uint32_t)arc_buf_lsize(buf); 1891 uint64_t blkid; 1892 1893 rw_enter(&dn->dn_struct_rwlock, RW_READER); 1894 blkid = dbuf_whichblock(dn, 0, offset); 1895 db = dbuf_hold(dn, blkid, FTAG); 1896 rw_exit(&dn->dn_struct_rwlock); 1897 if (db == NULL) 1898 return (SET_ERROR(EIO)); 1899 1900 /* 1901 * We can only assign if the offset is aligned and the arc buf is the 1902 * same size as the dbuf. 1903 */ 1904 if (offset == db->db.db_offset && blksz == db->db.db_size) { 1905 zfs_racct_write(os->os_spa, blksz, 1, flags); 1906 dbuf_assign_arcbuf(db, buf, tx, flags); 1907 dbuf_rele(db, FTAG); 1908 } else { 1909 /* compressed bufs must always be assignable to their dbuf */ 1910 ASSERT3U(arc_get_compression(buf), ==, ZIO_COMPRESS_OFF); 1911 ASSERT(!(buf->b_flags & ARC_BUF_FLAG_COMPRESSED)); 1912 1913 dbuf_rele(db, FTAG); 1914 dmu_write_by_dnode(dn, offset, blksz, buf->b_data, tx, flags); 1915 dmu_return_arcbuf(buf); 1916 } 1917 1918 return (0); 1919 } 1920 1921 int 1922 dmu_assign_arcbuf_by_dbuf(dmu_buf_t *handle, uint64_t offset, arc_buf_t *buf, 1923 dmu_tx_t *tx, dmu_flags_t flags) 1924 { 1925 int err; 1926 dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle; 1927 1928 DB_DNODE_ENTER(db); 1929 err = dmu_assign_arcbuf_by_dnode(DB_DNODE(db), offset, buf, tx, flags); 1930 DB_DNODE_EXIT(db); 1931 1932 return (err); 1933 } 1934 1935 void 1936 dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg) 1937 { 1938 (void) buf; 1939 dmu_sync_arg_t *dsa = varg; 1940 1941 if (zio->io_error == 0) { 1942 dbuf_dirty_record_t *dr = dsa->dsa_dr; 1943 blkptr_t *bp = zio->io_bp; 1944 1945 if (BP_IS_HOLE(bp)) { 1946 dmu_buf_t *db = NULL; 1947 if (dr) 1948 db = &(dr->dr_dbuf->db); 1949 else 1950 db = dsa->dsa_zgd->zgd_db; 1951 /* 1952 * A block of zeros may compress to a hole, but the 1953 * block size still needs to be known for replay. 1954 */ 1955 BP_SET_LSIZE(bp, db->db_size); 1956 } else if (!BP_IS_EMBEDDED(bp)) { 1957 ASSERT0(BP_GET_LEVEL(bp)); 1958 BP_SET_FILL(bp, 1); 1959 } 1960 } 1961 } 1962 1963 static void 1964 dmu_sync_late_arrival_ready(zio_t *zio) 1965 { 1966 dmu_sync_ready(zio, NULL, zio->io_private); 1967 } 1968 1969 void 1970 dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg) 1971 { 1972 (void) buf; 1973 dmu_sync_arg_t *dsa = varg; 1974 dbuf_dirty_record_t *dr = dsa->dsa_dr; 1975 dmu_buf_impl_t *db = dr->dr_dbuf; 1976 zgd_t *zgd = dsa->dsa_zgd; 1977 1978 /* 1979 * Record the vdev(s) backing this blkptr so they can be flushed after 1980 * the writes for the lwb have completed. 1981 */ 1982 if (zgd && zio->io_error == 0) { 1983 zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp); 1984 } 1985 1986 mutex_enter(&db->db_mtx); 1987 ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC); 1988 if (zio->io_error == 0) { 1989 ASSERT0(dr->dt.dl.dr_has_raw_params); 1990 dr->dt.dl.dr_nopwrite = !!(zio->io_flags & ZIO_FLAG_NOPWRITE); 1991 if (dr->dt.dl.dr_nopwrite) { 1992 blkptr_t *bp = zio->io_bp; 1993 blkptr_t *bp_orig = &zio->io_bp_orig; 1994 uint8_t chksum = BP_GET_CHECKSUM(bp_orig); 1995 1996 ASSERT(BP_EQUAL(bp, bp_orig)); 1997 VERIFY(BP_EQUAL(bp, db->db_blkptr)); 1998 ASSERT(zio->io_prop.zp_compress != ZIO_COMPRESS_OFF); 1999 VERIFY(zio_checksum_table[chksum].ci_flags & 2000 ZCHECKSUM_FLAG_NOPWRITE); 2001 } 2002 dr->dt.dl.dr_overridden_by = *zio->io_bp; 2003 dr->dt.dl.dr_override_state = DR_OVERRIDDEN; 2004 dr->dt.dl.dr_copies = zio->io_prop.zp_copies; 2005 dr->dt.dl.dr_gang_copies = zio->io_prop.zp_gang_copies; 2006 2007 /* 2008 * Old style holes are filled with all zeros, whereas 2009 * new-style holes maintain their lsize, type, level, 2010 * and birth time (see zio_write_compress). While we 2011 * need to reset the BP_SET_LSIZE() call that happened 2012 * in dmu_sync_ready for old style holes, we do *not* 2013 * want to wipe out the information contained in new 2014 * style holes. Thus, only zero out the block pointer if 2015 * it's an old style hole. 2016 */ 2017 if (BP_IS_HOLE(&dr->dt.dl.dr_overridden_by) && 2018 BP_GET_LOGICAL_BIRTH(&dr->dt.dl.dr_overridden_by) == 0) 2019 BP_ZERO(&dr->dt.dl.dr_overridden_by); 2020 } else { 2021 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN; 2022 } 2023 2024 cv_broadcast(&db->db_changed); 2025 mutex_exit(&db->db_mtx); 2026 2027 if (dsa->dsa_done) 2028 dsa->dsa_done(dsa->dsa_zgd, zio->io_error); 2029 2030 kmem_free(dsa, sizeof (*dsa)); 2031 } 2032 2033 static void 2034 dmu_sync_late_arrival_done(zio_t *zio) 2035 { 2036 blkptr_t *bp = zio->io_bp; 2037 dmu_sync_arg_t *dsa = zio->io_private; 2038 zgd_t *zgd = dsa->dsa_zgd; 2039 2040 if (zio->io_error == 0) { 2041 /* 2042 * Record the vdev(s) backing this blkptr so they can be 2043 * flushed after the writes for the lwb have completed. 2044 */ 2045 zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp); 2046 2047 if (!BP_IS_HOLE(bp)) { 2048 blkptr_t *bp_orig __maybe_unused = &zio->io_bp_orig; 2049 ASSERT(!(zio->io_flags & ZIO_FLAG_NOPWRITE)); 2050 ASSERT(BP_IS_HOLE(bp_orig) || !BP_EQUAL(bp, bp_orig)); 2051 ASSERT(BP_GET_BIRTH(zio->io_bp) == zio->io_txg); 2052 ASSERT(zio->io_txg > spa_syncing_txg(zio->io_spa)); 2053 zio_free(zio->io_spa, zio->io_txg, zio->io_bp); 2054 } 2055 } 2056 2057 dmu_tx_commit(dsa->dsa_tx); 2058 2059 dsa->dsa_done(dsa->dsa_zgd, zio->io_error); 2060 2061 abd_free(zio->io_abd); 2062 kmem_free(dsa, sizeof (*dsa)); 2063 } 2064 2065 static int 2066 dmu_sync_late_arrival(zio_t *pio, objset_t *os, dmu_sync_cb_t *done, zgd_t *zgd, 2067 zio_prop_t *zp, zbookmark_phys_t *zb) 2068 { 2069 dmu_sync_arg_t *dsa; 2070 dmu_tx_t *tx; 2071 int error; 2072 2073 error = dbuf_read((dmu_buf_impl_t *)zgd->zgd_db, NULL, 2074 DB_RF_CANFAIL | DMU_READ_NO_PREFETCH | DMU_KEEP_CACHING); 2075 if (error != 0) 2076 return (error); 2077 2078 tx = dmu_tx_create(os); 2079 dmu_tx_hold_space(tx, zgd->zgd_db->db_size); 2080 /* 2081 * This transaction does not produce any dirty data or log blocks, so 2082 * it should not be throttled. All other cases wait for TXG sync, by 2083 * which time the log block we are writing will be obsolete, so we can 2084 * skip waiting and just return error here instead. 2085 */ 2086 if (dmu_tx_assign(tx, DMU_TX_NOWAIT | DMU_TX_NOTHROTTLE) != 0) { 2087 dmu_tx_abort(tx); 2088 /* Make zl_get_data do txg_waited_synced() */ 2089 return (SET_ERROR(EIO)); 2090 } 2091 2092 /* 2093 * In order to prevent the zgd's lwb from being free'd prior to 2094 * dmu_sync_late_arrival_done() being called, we have to ensure 2095 * the lwb's "max txg" takes this tx's txg into account. 2096 */ 2097 zil_lwb_add_txg(zgd->zgd_lwb, dmu_tx_get_txg(tx)); 2098 2099 dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP); 2100 dsa->dsa_dr = NULL; 2101 dsa->dsa_done = done; 2102 dsa->dsa_zgd = zgd; 2103 dsa->dsa_tx = tx; 2104 2105 /* 2106 * Since we are currently syncing this txg, it's nontrivial to 2107 * determine what BP to nopwrite against, so we disable nopwrite. 2108 * 2109 * When syncing, the db_blkptr is initially the BP of the previous 2110 * txg. We can not nopwrite against it because it will be changed 2111 * (this is similar to the non-late-arrival case where the dbuf is 2112 * dirty in a future txg). 2113 * 2114 * Then dbuf_write_ready() sets bp_blkptr to the location we will write. 2115 * We can not nopwrite against it because although the BP will not 2116 * (typically) be changed, the data has not yet been persisted to this 2117 * location. 2118 * 2119 * Finally, when dbuf_write_done() is called, it is theoretically 2120 * possible to always nopwrite, because the data that was written in 2121 * this txg is the same data that we are trying to write. However we 2122 * would need to check that this dbuf is not dirty in any future 2123 * txg's (as we do in the normal dmu_sync() path). For simplicity, we 2124 * don't nopwrite in this case. 2125 */ 2126 zp->zp_nopwrite = B_FALSE; 2127 2128 zio_nowait(zio_write(pio, os->os_spa, dmu_tx_get_txg(tx), zgd->zgd_bp, 2129 abd_get_from_buf(zgd->zgd_db->db_data, zgd->zgd_db->db_size), 2130 zgd->zgd_db->db_size, zgd->zgd_db->db_size, zp, 2131 dmu_sync_late_arrival_ready, NULL, dmu_sync_late_arrival_done, 2132 dsa, ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, zb)); 2133 2134 return (0); 2135 } 2136 2137 /* 2138 * Intent log support: sync the block associated with db to disk. 2139 * N.B. and XXX: the caller is responsible for making sure that the 2140 * data isn't changing while dmu_sync() is writing it. 2141 * 2142 * Return values: 2143 * 2144 * EEXIST: this txg has already been synced, so there's nothing to do. 2145 * The caller should not log the write. 2146 * 2147 * ENOENT: the block was dbuf_free_range()'d, so there's nothing to do. 2148 * The caller should not log the write. 2149 * 2150 * EALREADY: this block is already in the process of being synced. 2151 * The caller should track its progress (somehow). 2152 * 2153 * EIO: could not do the I/O. 2154 * The caller should do a txg_wait_synced(). 2155 * 2156 * 0: the I/O has been initiated. 2157 * The caller should log this blkptr in the done callback. 2158 * It is possible that the I/O will fail, in which case 2159 * the error will be reported to the done callback and 2160 * propagated to pio from zio_done(). 2161 */ 2162 int 2163 dmu_sync(zio_t *pio, uint64_t txg, dmu_sync_cb_t *done, zgd_t *zgd) 2164 { 2165 dmu_buf_impl_t *db = (dmu_buf_impl_t *)zgd->zgd_db; 2166 objset_t *os = db->db_objset; 2167 dsl_dataset_t *ds = os->os_dsl_dataset; 2168 dbuf_dirty_record_t *dr, *dr_next; 2169 dmu_sync_arg_t *dsa; 2170 zbookmark_phys_t zb; 2171 zio_prop_t zp; 2172 2173 ASSERT(pio != NULL); 2174 ASSERT(txg != 0); 2175 2176 SET_BOOKMARK(&zb, ds->ds_object, 2177 db->db.db_object, db->db_level, db->db_blkid); 2178 2179 DB_DNODE_ENTER(db); 2180 dmu_write_policy(os, DB_DNODE(db), db->db_level, WP_DMU_SYNC, &zp); 2181 DB_DNODE_EXIT(db); 2182 2183 /* 2184 * If we're frozen (running ziltest), we always need to generate a bp. 2185 */ 2186 if (txg > spa_freeze_txg(os->os_spa)) 2187 return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb)); 2188 2189 /* 2190 * Grabbing db_mtx now provides a barrier between dbuf_sync_leaf() 2191 * and us. If we determine that this txg is not yet syncing, 2192 * but it begins to sync a moment later, that's OK because the 2193 * sync thread will block in dbuf_sync_leaf() until we drop db_mtx. 2194 */ 2195 mutex_enter(&db->db_mtx); 2196 2197 if (txg <= spa_last_synced_txg(os->os_spa)) { 2198 /* 2199 * This txg has already synced. There's nothing to do. 2200 */ 2201 mutex_exit(&db->db_mtx); 2202 return (SET_ERROR(EEXIST)); 2203 } 2204 2205 if (txg <= spa_syncing_txg(os->os_spa)) { 2206 /* 2207 * This txg is currently syncing, so we can't mess with 2208 * the dirty record anymore; just write a new log block. 2209 */ 2210 mutex_exit(&db->db_mtx); 2211 return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb)); 2212 } 2213 2214 dr = dbuf_find_dirty_eq(db, txg); 2215 2216 if (dr == NULL) { 2217 /* 2218 * There's no dr for this dbuf, so it must have been freed. 2219 * There's no need to log writes to freed blocks, so we're done. 2220 */ 2221 mutex_exit(&db->db_mtx); 2222 return (SET_ERROR(ENOENT)); 2223 } 2224 2225 dr_next = list_next(&db->db_dirty_records, dr); 2226 ASSERT(dr_next == NULL || dr_next->dr_txg < txg); 2227 2228 if (db->db_blkptr != NULL) { 2229 /* 2230 * We need to fill in zgd_bp with the current blkptr so that 2231 * the nopwrite code can check if we're writing the same 2232 * data that's already on disk. We can only nopwrite if we 2233 * are sure that after making the copy, db_blkptr will not 2234 * change until our i/o completes. We ensure this by 2235 * holding the db_mtx, and only allowing nopwrite if the 2236 * block is not already dirty (see below). This is verified 2237 * by dmu_sync_done(), which VERIFYs that the db_blkptr has 2238 * not changed. 2239 */ 2240 *zgd->zgd_bp = *db->db_blkptr; 2241 } 2242 2243 /* 2244 * Assume the on-disk data is X, the current syncing data (in 2245 * txg - 1) is Y, and the current in-memory data is Z (currently 2246 * in dmu_sync). 2247 * 2248 * We usually want to perform a nopwrite if X and Z are the 2249 * same. However, if Y is different (i.e. the BP is going to 2250 * change before this write takes effect), then a nopwrite will 2251 * be incorrect - we would override with X, which could have 2252 * been freed when Y was written. 2253 * 2254 * (Note that this is not a concern when we are nop-writing from 2255 * syncing context, because X and Y must be identical, because 2256 * all previous txgs have been synced.) 2257 * 2258 * Therefore, we disable nopwrite if the current BP could change 2259 * before this TXG. There are two ways it could change: by 2260 * being dirty (dr_next is non-NULL), or by being freed 2261 * (dnode_block_freed()). This behavior is verified by 2262 * zio_done(), which VERIFYs that the override BP is identical 2263 * to the on-disk BP. 2264 */ 2265 if (dr_next != NULL) { 2266 zp.zp_nopwrite = B_FALSE; 2267 } else { 2268 DB_DNODE_ENTER(db); 2269 if (dnode_block_freed(DB_DNODE(db), db->db_blkid)) 2270 zp.zp_nopwrite = B_FALSE; 2271 DB_DNODE_EXIT(db); 2272 } 2273 2274 ASSERT(dr->dr_txg == txg); 2275 if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC || 2276 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) { 2277 /* 2278 * We have already issued a sync write for this buffer, 2279 * or this buffer has already been synced. It could not 2280 * have been dirtied since, or we would have cleared the state. 2281 */ 2282 mutex_exit(&db->db_mtx); 2283 return (SET_ERROR(EALREADY)); 2284 } 2285 2286 ASSERT0(dr->dt.dl.dr_has_raw_params); 2287 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN); 2288 dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC; 2289 mutex_exit(&db->db_mtx); 2290 2291 dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP); 2292 dsa->dsa_dr = dr; 2293 dsa->dsa_done = done; 2294 dsa->dsa_zgd = zgd; 2295 dsa->dsa_tx = NULL; 2296 2297 zio_nowait(arc_write(pio, os->os_spa, txg, zgd->zgd_bp, 2298 dr->dt.dl.dr_data, !DBUF_IS_CACHEABLE(db), 2299 dbuf_is_l2cacheable(db, NULL), &zp, dmu_sync_ready, NULL, 2300 dmu_sync_done, dsa, ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, 2301 &zb)); 2302 2303 return (0); 2304 } 2305 2306 int 2307 dmu_object_set_nlevels(objset_t *os, uint64_t object, int nlevels, dmu_tx_t *tx) 2308 { 2309 dnode_t *dn; 2310 int err; 2311 2312 err = dnode_hold(os, object, FTAG, &dn); 2313 if (err) 2314 return (err); 2315 err = dnode_set_nlevels(dn, nlevels, tx); 2316 dnode_rele(dn, FTAG); 2317 return (err); 2318 } 2319 2320 int 2321 dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs, 2322 dmu_tx_t *tx) 2323 { 2324 dnode_t *dn; 2325 int err; 2326 2327 err = dnode_hold(os, object, FTAG, &dn); 2328 if (err) 2329 return (err); 2330 err = dnode_set_blksz(dn, size, ibs, tx); 2331 dnode_rele(dn, FTAG); 2332 return (err); 2333 } 2334 2335 int 2336 dmu_object_set_maxblkid(objset_t *os, uint64_t object, uint64_t maxblkid, 2337 dmu_tx_t *tx) 2338 { 2339 dnode_t *dn; 2340 int err; 2341 2342 err = dnode_hold(os, object, FTAG, &dn); 2343 if (err) 2344 return (err); 2345 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 2346 dnode_new_blkid(dn, maxblkid, tx, B_FALSE, B_TRUE); 2347 rw_exit(&dn->dn_struct_rwlock); 2348 dnode_rele(dn, FTAG); 2349 return (0); 2350 } 2351 2352 void 2353 dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum, 2354 dmu_tx_t *tx) 2355 { 2356 dnode_t *dn; 2357 2358 /* 2359 * Send streams include each object's checksum function. This 2360 * check ensures that the receiving system can understand the 2361 * checksum function transmitted. 2362 */ 2363 ASSERT3U(checksum, <, ZIO_CHECKSUM_LEGACY_FUNCTIONS); 2364 2365 VERIFY0(dnode_hold(os, object, FTAG, &dn)); 2366 ASSERT3U(checksum, <, ZIO_CHECKSUM_FUNCTIONS); 2367 dn->dn_checksum = checksum; 2368 dnode_setdirty(dn, tx); 2369 dnode_rele(dn, FTAG); 2370 } 2371 2372 void 2373 dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress, 2374 dmu_tx_t *tx) 2375 { 2376 dnode_t *dn; 2377 2378 /* 2379 * Send streams include each object's compression function. This 2380 * check ensures that the receiving system can understand the 2381 * compression function transmitted. 2382 */ 2383 ASSERT3U(compress, <, ZIO_COMPRESS_LEGACY_FUNCTIONS); 2384 2385 VERIFY0(dnode_hold(os, object, FTAG, &dn)); 2386 dn->dn_compress = compress; 2387 dnode_setdirty(dn, tx); 2388 dnode_rele(dn, FTAG); 2389 } 2390 2391 /* 2392 * When the "redundant_metadata" property is set to "most", only indirect 2393 * blocks of this level and higher will have an additional ditto block. 2394 */ 2395 static const int zfs_redundant_metadata_most_ditto_level = 2; 2396 2397 void 2398 dmu_write_policy(objset_t *os, dnode_t *dn, int level, int wp, zio_prop_t *zp) 2399 { 2400 dmu_object_type_t type = dn ? dn->dn_type : DMU_OT_OBJSET; 2401 boolean_t ismd = (level > 0 || DMU_OT_IS_METADATA(type) || 2402 (wp & WP_SPILL)); 2403 enum zio_checksum checksum = os->os_checksum; 2404 enum zio_compress compress = os->os_compress; 2405 uint8_t complevel = os->os_complevel; 2406 enum zio_checksum dedup_checksum = os->os_dedup_checksum; 2407 boolean_t dedup = B_FALSE; 2408 boolean_t nopwrite = B_FALSE; 2409 boolean_t dedup_verify = os->os_dedup_verify; 2410 boolean_t encrypt = B_FALSE; 2411 int copies = os->os_copies; 2412 int gang_copies = os->os_copies; 2413 2414 /* 2415 * We maintain different write policies for each of the following 2416 * types of data: 2417 * 1. metadata 2418 * 2. preallocated blocks (i.e. level-0 blocks of a dump device) 2419 * 3. all other level 0 blocks 2420 */ 2421 if (ismd) { 2422 /* 2423 * XXX -- we should design a compression algorithm 2424 * that specializes in arrays of bps. 2425 */ 2426 compress = zio_compress_select(os->os_spa, 2427 ZIO_COMPRESS_ON, ZIO_COMPRESS_ON); 2428 2429 /* 2430 * Metadata always gets checksummed. If the data 2431 * checksum is multi-bit correctable, and it's not a 2432 * ZBT-style checksum, then it's suitable for metadata 2433 * as well. Otherwise, the metadata checksum defaults 2434 * to fletcher4. 2435 */ 2436 if (!(zio_checksum_table[checksum].ci_flags & 2437 ZCHECKSUM_FLAG_METADATA) || 2438 (zio_checksum_table[checksum].ci_flags & 2439 ZCHECKSUM_FLAG_EMBEDDED)) 2440 checksum = ZIO_CHECKSUM_FLETCHER_4; 2441 2442 switch (os->os_redundant_metadata) { 2443 case ZFS_REDUNDANT_METADATA_ALL: 2444 copies++; 2445 gang_copies++; 2446 break; 2447 case ZFS_REDUNDANT_METADATA_MOST: 2448 if (level >= zfs_redundant_metadata_most_ditto_level || 2449 DMU_OT_IS_METADATA(type) || (wp & WP_SPILL)) 2450 copies++; 2451 if (level + 1 >= 2452 zfs_redundant_metadata_most_ditto_level || 2453 DMU_OT_IS_METADATA(type) || (wp & WP_SPILL)) 2454 gang_copies++; 2455 break; 2456 case ZFS_REDUNDANT_METADATA_SOME: 2457 if (DMU_OT_IS_CRITICAL(type, level)) { 2458 copies++; 2459 gang_copies++; 2460 } else if (DMU_OT_IS_METADATA(type)) { 2461 gang_copies++; 2462 } 2463 break; 2464 case ZFS_REDUNDANT_METADATA_NONE: 2465 break; 2466 } 2467 2468 if (dmu_ddt_copies > 0) { 2469 /* 2470 * If this tunable is set, and this is a write for a 2471 * dedup entry store (zap or log), then we treat it 2472 * something like ZFS_REDUNDANT_METADATA_MOST on a 2473 * regular dataset: this many copies, and one more for 2474 * "higher" indirect blocks. This specific exception is 2475 * necessary because dedup objects are stored in the 2476 * MOS, which always has the highest possible copies. 2477 */ 2478 dmu_object_type_t stype = 2479 dn ? dn->dn_storage_type : DMU_OT_NONE; 2480 if (stype == DMU_OT_NONE) 2481 stype = type; 2482 if (stype == DMU_OT_DDT_ZAP) { 2483 copies = dmu_ddt_copies; 2484 if (level >= 2485 zfs_redundant_metadata_most_ditto_level) 2486 copies++; 2487 } 2488 } 2489 } else if (wp & WP_NOFILL) { 2490 ASSERT0(level); 2491 2492 /* 2493 * If we're writing preallocated blocks, we aren't actually 2494 * writing them so don't set any policy properties. These 2495 * blocks are currently only used by an external subsystem 2496 * outside of zfs (i.e. dump) and not written by the zio 2497 * pipeline. 2498 */ 2499 compress = ZIO_COMPRESS_OFF; 2500 checksum = ZIO_CHECKSUM_OFF; 2501 } else { 2502 compress = zio_compress_select(os->os_spa, dn->dn_compress, 2503 compress); 2504 complevel = zio_complevel_select(os->os_spa, compress, 2505 complevel, complevel); 2506 2507 /* 2508 * Storing many references to an all zeros block in the dedup 2509 * table would be expensive. Instead, if dedup is enabled, 2510 * store them as holes even if compression is not enabled. 2511 */ 2512 if (compress == ZIO_COMPRESS_OFF && 2513 dedup_checksum != ZIO_CHECKSUM_OFF) 2514 compress = ZIO_COMPRESS_EMPTY; 2515 2516 checksum = (dedup_checksum == ZIO_CHECKSUM_OFF) ? 2517 zio_checksum_select(dn->dn_checksum, checksum) : 2518 dedup_checksum; 2519 2520 /* 2521 * Determine dedup setting. If we are in dmu_sync(), 2522 * we won't actually dedup now because that's all 2523 * done in syncing context; but we do want to use the 2524 * dedup checksum. If the checksum is not strong 2525 * enough to ensure unique signatures, force 2526 * dedup_verify. 2527 */ 2528 if (dedup_checksum != ZIO_CHECKSUM_OFF) { 2529 dedup = (wp & WP_DMU_SYNC) ? B_FALSE : B_TRUE; 2530 if (!(zio_checksum_table[checksum].ci_flags & 2531 ZCHECKSUM_FLAG_DEDUP)) 2532 dedup_verify = B_TRUE; 2533 } 2534 2535 /* 2536 * Enable nopwrite if we have secure enough checksum 2537 * algorithm (see comment in zio_nop_write) and 2538 * compression is enabled. We don't enable nopwrite if 2539 * dedup is enabled as the two features are mutually 2540 * exclusive. 2541 */ 2542 nopwrite = (!dedup && (zio_checksum_table[checksum].ci_flags & 2543 ZCHECKSUM_FLAG_NOPWRITE) && 2544 compress != ZIO_COMPRESS_OFF && zfs_nopwrite_enabled); 2545 2546 if (os->os_redundant_metadata == ZFS_REDUNDANT_METADATA_ALL || 2547 (os->os_redundant_metadata == 2548 ZFS_REDUNDANT_METADATA_MOST && 2549 zfs_redundant_metadata_most_ditto_level <= 1)) 2550 gang_copies++; 2551 } 2552 2553 /* 2554 * All objects in an encrypted objset are protected from modification 2555 * via a MAC. Encrypted objects store their IV and salt in the last DVA 2556 * in the bp, so we cannot use all copies. Encrypted objects are also 2557 * not subject to nopwrite since writing the same data will still 2558 * result in a new ciphertext. Only encrypted blocks can be dedup'd 2559 * to avoid ambiguity in the dedup code since the DDT does not store 2560 * object types. 2561 */ 2562 if (os->os_encrypted && (wp & WP_NOFILL) == 0) { 2563 encrypt = B_TRUE; 2564 2565 if (DMU_OT_IS_ENCRYPTED(type)) { 2566 copies = MIN(copies, SPA_DVAS_PER_BP - 1); 2567 gang_copies = MIN(gang_copies, SPA_DVAS_PER_BP - 1); 2568 nopwrite = B_FALSE; 2569 } else { 2570 dedup = B_FALSE; 2571 } 2572 2573 if (level <= 0 && 2574 (type == DMU_OT_DNODE || type == DMU_OT_OBJSET)) { 2575 compress = ZIO_COMPRESS_EMPTY; 2576 } 2577 } 2578 2579 zp->zp_compress = compress; 2580 zp->zp_complevel = complevel; 2581 zp->zp_checksum = checksum; 2582 zp->zp_type = (wp & WP_SPILL) ? dn->dn_bonustype : type; 2583 zp->zp_level = level; 2584 zp->zp_copies = MIN(copies, spa_max_replication(os->os_spa)); 2585 zp->zp_gang_copies = MIN(MAX(gang_copies, copies), 2586 spa_max_replication(os->os_spa)); 2587 zp->zp_dedup = dedup; 2588 zp->zp_dedup_verify = dedup && dedup_verify; 2589 zp->zp_nopwrite = nopwrite; 2590 zp->zp_encrypt = encrypt; 2591 zp->zp_byteorder = ZFS_HOST_BYTEORDER; 2592 zp->zp_direct_write = (wp & WP_DIRECT_WR) ? B_TRUE : B_FALSE; 2593 zp->zp_rewrite = B_FALSE; 2594 memset(zp->zp_salt, 0, ZIO_DATA_SALT_LEN); 2595 memset(zp->zp_iv, 0, ZIO_DATA_IV_LEN); 2596 memset(zp->zp_mac, 0, ZIO_DATA_MAC_LEN); 2597 zp->zp_zpl_smallblk = os->os_zpl_special_smallblock; 2598 zp->zp_storage_type = dn ? dn->dn_storage_type : DMU_OT_NONE; 2599 2600 ASSERT3U(zp->zp_compress, !=, ZIO_COMPRESS_INHERIT); 2601 } 2602 2603 /* 2604 * Reports the location of data and holes in an object. In order to 2605 * accurately report holes all dirty data must be synced to disk. This 2606 * causes extremely poor performance when seeking for holes in a dirty file. 2607 * As a compromise, only provide hole data when the dnode is clean. When 2608 * a dnode is dirty report the dnode as having no holes by returning EBUSY 2609 * which is always safe to do. 2610 */ 2611 int 2612 dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off) 2613 { 2614 dnode_t *dn; 2615 uint64_t txg, maxtxg = 0; 2616 int err; 2617 2618 restart: 2619 err = dnode_hold(os, object, FTAG, &dn); 2620 if (err) 2621 return (err); 2622 2623 rw_enter(&dn->dn_struct_rwlock, RW_READER); 2624 2625 if (dnode_is_dirty(dn)) { 2626 /* 2627 * If the zfs_dmu_offset_next_sync module option is enabled 2628 * then hole reporting has been requested. Dirty dnodes 2629 * must be synced to disk to accurately report holes. 2630 * 2631 * Provided a RL_READER rangelock spanning 0-UINT64_MAX is 2632 * held by the caller only limited restarts will be required. 2633 * We tolerate callers which do not hold the rangelock by 2634 * returning EBUSY and not reporting holes after at most 2635 * TXG_CONCURRENT_STATES (3) restarts. 2636 */ 2637 if (zfs_dmu_offset_next_sync) { 2638 rw_exit(&dn->dn_struct_rwlock); 2639 dnode_rele(dn, FTAG); 2640 2641 if (maxtxg == 0) { 2642 txg = spa_last_synced_txg(dmu_objset_spa(os)); 2643 maxtxg = txg + TXG_CONCURRENT_STATES; 2644 } else if (txg >= maxtxg) 2645 return (SET_ERROR(EBUSY)); 2646 2647 txg_wait_synced(dmu_objset_pool(os), ++txg); 2648 goto restart; 2649 } 2650 2651 err = SET_ERROR(EBUSY); 2652 } else { 2653 err = dnode_next_offset(dn, DNODE_FIND_HAVELOCK | 2654 (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0); 2655 } 2656 2657 rw_exit(&dn->dn_struct_rwlock); 2658 dnode_rele(dn, FTAG); 2659 2660 return (err); 2661 } 2662 2663 int 2664 dmu_read_l0_bps(objset_t *os, uint64_t object, uint64_t offset, uint64_t length, 2665 blkptr_t *bps, size_t *nbpsp) 2666 { 2667 dmu_buf_t **dbp, *dbuf; 2668 dmu_buf_impl_t *db; 2669 blkptr_t *bp; 2670 int error, numbufs; 2671 2672 error = dmu_buf_hold_array(os, object, offset, length, FALSE, FTAG, 2673 &numbufs, &dbp, DMU_READ_PREFETCH); 2674 if (error != 0) { 2675 if (error == ESRCH) { 2676 error = SET_ERROR(ENXIO); 2677 } 2678 return (error); 2679 } 2680 2681 ASSERT3U(numbufs, <=, *nbpsp); 2682 2683 for (int i = 0; i < numbufs; i++) { 2684 dbuf = dbp[i]; 2685 db = (dmu_buf_impl_t *)dbuf; 2686 2687 mutex_enter(&db->db_mtx); 2688 2689 if (!list_is_empty(&db->db_dirty_records)) { 2690 dbuf_dirty_record_t *dr; 2691 2692 dr = list_head(&db->db_dirty_records); 2693 if (dr->dt.dl.dr_brtwrite) { 2694 /* 2695 * This is very special case where we clone a 2696 * block and in the same transaction group we 2697 * read its BP (most likely to clone the clone). 2698 */ 2699 bp = &dr->dt.dl.dr_overridden_by; 2700 } else { 2701 /* 2702 * The block was modified in the same 2703 * transaction group. 2704 */ 2705 mutex_exit(&db->db_mtx); 2706 error = SET_ERROR(EAGAIN); 2707 goto out; 2708 } 2709 } else { 2710 bp = db->db_blkptr; 2711 } 2712 2713 mutex_exit(&db->db_mtx); 2714 2715 if (bp == NULL) { 2716 /* 2717 * The file size was increased, but the block was never 2718 * written, otherwise we would either have the block 2719 * pointer or the dirty record and would not get here. 2720 * It is effectively a hole, so report it as such. 2721 */ 2722 BP_ZERO(&bps[i]); 2723 continue; 2724 } 2725 /* 2726 * Make sure we clone only data blocks. 2727 */ 2728 if (BP_IS_METADATA(bp) && !BP_IS_HOLE(bp)) { 2729 error = SET_ERROR(EINVAL); 2730 goto out; 2731 } 2732 2733 /* 2734 * If the block was allocated in transaction group that is not 2735 * yet synced, we could clone it, but we couldn't write this 2736 * operation into ZIL, or it may be impossible to replay, since 2737 * the block may appear not yet allocated at that point. 2738 */ 2739 if (BP_GET_PHYSICAL_BIRTH(bp) > spa_freeze_txg(os->os_spa)) { 2740 error = SET_ERROR(EINVAL); 2741 goto out; 2742 } 2743 if (BP_GET_PHYSICAL_BIRTH(bp) > 2744 spa_last_synced_txg(os->os_spa)) { 2745 error = SET_ERROR(EAGAIN); 2746 goto out; 2747 } 2748 2749 bps[i] = *bp; 2750 } 2751 2752 *nbpsp = numbufs; 2753 out: 2754 dmu_buf_rele_array(dbp, numbufs, FTAG); 2755 2756 return (error); 2757 } 2758 2759 int 2760 dmu_brt_clone(objset_t *os, uint64_t object, uint64_t offset, uint64_t length, 2761 dmu_tx_t *tx, const blkptr_t *bps, size_t nbps) 2762 { 2763 spa_t *spa; 2764 dmu_buf_t **dbp, *dbuf; 2765 dmu_buf_impl_t *db; 2766 struct dirty_leaf *dl; 2767 dbuf_dirty_record_t *dr; 2768 const blkptr_t *bp; 2769 int error = 0, i, numbufs; 2770 2771 spa = os->os_spa; 2772 2773 VERIFY0(dmu_buf_hold_array(os, object, offset, length, FALSE, FTAG, 2774 &numbufs, &dbp, DMU_READ_PREFETCH)); 2775 ASSERT3U(nbps, ==, numbufs); 2776 2777 /* 2778 * Before we start cloning make sure that the dbufs sizes match new BPs 2779 * sizes. If they don't, that's a no-go, as we are not able to shrink 2780 * dbufs. 2781 */ 2782 for (i = 0; i < numbufs; i++) { 2783 dbuf = dbp[i]; 2784 db = (dmu_buf_impl_t *)dbuf; 2785 bp = &bps[i]; 2786 2787 ASSERT3U(db->db.db_object, !=, DMU_META_DNODE_OBJECT); 2788 ASSERT0(db->db_level); 2789 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 2790 ASSERT(db->db_blkid != DMU_SPILL_BLKID); 2791 2792 if (!BP_IS_HOLE(bp) && BP_GET_LSIZE(bp) != dbuf->db_size) { 2793 error = SET_ERROR(EXDEV); 2794 goto out; 2795 } 2796 } 2797 2798 for (i = 0; i < numbufs; i++) { 2799 dbuf = dbp[i]; 2800 db = (dmu_buf_impl_t *)dbuf; 2801 bp = &bps[i]; 2802 2803 dmu_buf_will_clone_or_dio(dbuf, tx); 2804 2805 mutex_enter(&db->db_mtx); 2806 2807 dr = list_head(&db->db_dirty_records); 2808 VERIFY(dr != NULL); 2809 ASSERT3U(dr->dr_txg, ==, tx->tx_txg); 2810 dl = &dr->dt.dl; 2811 ASSERT0(dl->dr_has_raw_params); 2812 dl->dr_overridden_by = *bp; 2813 if (!BP_IS_HOLE(bp) || BP_GET_LOGICAL_BIRTH(bp) != 0) { 2814 if (!BP_IS_EMBEDDED(bp)) { 2815 BP_SET_BIRTH(&dl->dr_overridden_by, dr->dr_txg, 2816 BP_GET_PHYSICAL_BIRTH(bp)); 2817 BP_SET_REWRITE(&dl->dr_overridden_by, 0); 2818 } else { 2819 BP_SET_LOGICAL_BIRTH(&dl->dr_overridden_by, 2820 dr->dr_txg); 2821 } 2822 } 2823 dl->dr_brtwrite = B_TRUE; 2824 dl->dr_override_state = DR_OVERRIDDEN; 2825 2826 mutex_exit(&db->db_mtx); 2827 2828 /* 2829 * When data in embedded into BP there is no need to create 2830 * BRT entry as there is no data block. Just copy the BP as 2831 * it contains the data. 2832 */ 2833 if (!BP_IS_HOLE(bp) && !BP_IS_EMBEDDED(bp)) { 2834 brt_pending_add(spa, bp, tx); 2835 } 2836 } 2837 out: 2838 dmu_buf_rele_array(dbp, numbufs, FTAG); 2839 2840 return (error); 2841 } 2842 2843 void 2844 __dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi) 2845 { 2846 dnode_phys_t *dnp = dn->dn_phys; 2847 2848 doi->doi_data_block_size = dn->dn_datablksz; 2849 doi->doi_metadata_block_size = dn->dn_indblkshift ? 2850 1ULL << dn->dn_indblkshift : 0; 2851 doi->doi_type = dn->dn_type; 2852 doi->doi_bonus_type = dn->dn_bonustype; 2853 doi->doi_bonus_size = dn->dn_bonuslen; 2854 doi->doi_dnodesize = dn->dn_num_slots << DNODE_SHIFT; 2855 doi->doi_indirection = dn->dn_nlevels; 2856 doi->doi_checksum = dn->dn_checksum; 2857 doi->doi_compress = dn->dn_compress; 2858 doi->doi_nblkptr = dn->dn_nblkptr; 2859 doi->doi_physical_blocks_512 = (DN_USED_BYTES(dnp) + 256) >> 9; 2860 doi->doi_max_offset = (dn->dn_maxblkid + 1) * dn->dn_datablksz; 2861 doi->doi_fill_count = 0; 2862 for (int i = 0; i < dnp->dn_nblkptr; i++) 2863 doi->doi_fill_count += BP_GET_FILL(&dnp->dn_blkptr[i]); 2864 } 2865 2866 void 2867 dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi) 2868 { 2869 rw_enter(&dn->dn_struct_rwlock, RW_READER); 2870 mutex_enter(&dn->dn_mtx); 2871 2872 __dmu_object_info_from_dnode(dn, doi); 2873 2874 mutex_exit(&dn->dn_mtx); 2875 rw_exit(&dn->dn_struct_rwlock); 2876 } 2877 2878 /* 2879 * Get information on a DMU object. 2880 * If doi is NULL, just indicates whether the object exists. 2881 */ 2882 int 2883 dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi) 2884 { 2885 dnode_t *dn; 2886 int err = dnode_hold(os, object, FTAG, &dn); 2887 2888 if (err) 2889 return (err); 2890 2891 if (doi != NULL) 2892 dmu_object_info_from_dnode(dn, doi); 2893 2894 dnode_rele(dn, FTAG); 2895 return (0); 2896 } 2897 2898 /* 2899 * As above, but faster; can be used when you have a held dbuf in hand. 2900 */ 2901 void 2902 dmu_object_info_from_db(dmu_buf_t *db_fake, dmu_object_info_t *doi) 2903 { 2904 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2905 2906 DB_DNODE_ENTER(db); 2907 dmu_object_info_from_dnode(DB_DNODE(db), doi); 2908 DB_DNODE_EXIT(db); 2909 } 2910 2911 /* 2912 * Faster still when you only care about the size. 2913 */ 2914 void 2915 dmu_object_size_from_db(dmu_buf_t *db_fake, uint32_t *blksize, 2916 u_longlong_t *nblk512) 2917 { 2918 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2919 dnode_t *dn; 2920 2921 DB_DNODE_ENTER(db); 2922 dn = DB_DNODE(db); 2923 2924 *blksize = dn->dn_datablksz; 2925 /* add in number of slots used for the dnode itself */ 2926 *nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >> 2927 SPA_MINBLOCKSHIFT) + dn->dn_num_slots; 2928 DB_DNODE_EXIT(db); 2929 } 2930 2931 void 2932 dmu_object_dnsize_from_db(dmu_buf_t *db_fake, int *dnsize) 2933 { 2934 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2935 2936 DB_DNODE_ENTER(db); 2937 *dnsize = DB_DNODE(db)->dn_num_slots << DNODE_SHIFT; 2938 DB_DNODE_EXIT(db); 2939 } 2940 2941 void 2942 byteswap_uint64_array(void *vbuf, size_t size) 2943 { 2944 uint64_t *buf = vbuf; 2945 size_t count = size >> 3; 2946 int i; 2947 2948 ASSERT0((size & 7)); 2949 2950 for (i = 0; i < count; i++) 2951 buf[i] = BSWAP_64(buf[i]); 2952 } 2953 2954 void 2955 byteswap_uint32_array(void *vbuf, size_t size) 2956 { 2957 uint32_t *buf = vbuf; 2958 size_t count = size >> 2; 2959 int i; 2960 2961 ASSERT0((size & 3)); 2962 2963 for (i = 0; i < count; i++) 2964 buf[i] = BSWAP_32(buf[i]); 2965 } 2966 2967 void 2968 byteswap_uint16_array(void *vbuf, size_t size) 2969 { 2970 uint16_t *buf = vbuf; 2971 size_t count = size >> 1; 2972 int i; 2973 2974 ASSERT0((size & 1)); 2975 2976 for (i = 0; i < count; i++) 2977 buf[i] = BSWAP_16(buf[i]); 2978 } 2979 2980 void 2981 byteswap_uint8_array(void *vbuf, size_t size) 2982 { 2983 (void) vbuf, (void) size; 2984 } 2985 2986 void 2987 dmu_init(void) 2988 { 2989 abd_init(); 2990 zfs_dbgmsg_init(); 2991 sa_cache_init(); 2992 dmu_objset_init(); 2993 dnode_init(); 2994 zfetch_init(); 2995 dmu_tx_init(); 2996 l2arc_init(); 2997 arc_init(); 2998 dbuf_init(); 2999 } 3000 3001 void 3002 dmu_fini(void) 3003 { 3004 arc_fini(); /* arc depends on l2arc, so arc must go first */ 3005 l2arc_fini(); 3006 dmu_tx_fini(); 3007 zfetch_fini(); 3008 dbuf_fini(); 3009 dnode_fini(); 3010 dmu_objset_fini(); 3011 sa_cache_fini(); 3012 zfs_dbgmsg_fini(); 3013 abd_fini(); 3014 } 3015 3016 EXPORT_SYMBOL(dmu_bonus_hold); 3017 EXPORT_SYMBOL(dmu_bonus_hold_by_dnode); 3018 EXPORT_SYMBOL(dmu_buf_hold_array_by_bonus); 3019 EXPORT_SYMBOL(dmu_buf_rele_array); 3020 EXPORT_SYMBOL(dmu_prefetch); 3021 EXPORT_SYMBOL(dmu_prefetch_by_dnode); 3022 EXPORT_SYMBOL(dmu_prefetch_dnode); 3023 EXPORT_SYMBOL(dmu_prefetch_stream); 3024 EXPORT_SYMBOL(dmu_prefetch_stream_by_dnode); 3025 EXPORT_SYMBOL(dmu_free_range); 3026 EXPORT_SYMBOL(dmu_free_long_range); 3027 EXPORT_SYMBOL(dmu_free_long_object); 3028 EXPORT_SYMBOL(dmu_read); 3029 EXPORT_SYMBOL(dmu_read_by_dnode); 3030 EXPORT_SYMBOL(dmu_read_uio); 3031 EXPORT_SYMBOL(dmu_read_uio_dbuf); 3032 EXPORT_SYMBOL(dmu_read_uio_dnode); 3033 EXPORT_SYMBOL(dmu_write); 3034 EXPORT_SYMBOL(dmu_write_by_dnode); 3035 EXPORT_SYMBOL(dmu_write_uio); 3036 EXPORT_SYMBOL(dmu_write_uio_dbuf); 3037 EXPORT_SYMBOL(dmu_write_uio_dnode); 3038 EXPORT_SYMBOL(dmu_prealloc); 3039 EXPORT_SYMBOL(dmu_object_info); 3040 EXPORT_SYMBOL(dmu_object_info_from_dnode); 3041 EXPORT_SYMBOL(dmu_object_info_from_db); 3042 EXPORT_SYMBOL(dmu_object_size_from_db); 3043 EXPORT_SYMBOL(dmu_object_dnsize_from_db); 3044 EXPORT_SYMBOL(dmu_object_set_nlevels); 3045 EXPORT_SYMBOL(dmu_object_set_blocksize); 3046 EXPORT_SYMBOL(dmu_object_set_maxblkid); 3047 EXPORT_SYMBOL(dmu_object_set_checksum); 3048 EXPORT_SYMBOL(dmu_object_set_compress); 3049 EXPORT_SYMBOL(dmu_offset_next); 3050 EXPORT_SYMBOL(dmu_write_policy); 3051 EXPORT_SYMBOL(dmu_sync); 3052 EXPORT_SYMBOL(dmu_request_arcbuf); 3053 EXPORT_SYMBOL(dmu_return_arcbuf); 3054 EXPORT_SYMBOL(dmu_assign_arcbuf_by_dnode); 3055 EXPORT_SYMBOL(dmu_assign_arcbuf_by_dbuf); 3056 EXPORT_SYMBOL(dmu_buf_hold); 3057 EXPORT_SYMBOL(dmu_ot); 3058 3059 ZFS_MODULE_PARAM(zfs, zfs_, nopwrite_enabled, INT, ZMOD_RW, 3060 "Enable NOP writes"); 3061 3062 ZFS_MODULE_PARAM(zfs, zfs_, per_txg_dirty_frees_percent, UINT, ZMOD_RW, 3063 "Percentage of dirtied blocks from frees in one TXG"); 3064 3065 ZFS_MODULE_PARAM(zfs, zfs_, dmu_offset_next_sync, INT, ZMOD_RW, 3066 "Enable forcing txg sync to find holes"); 3067 3068 ZFS_MODULE_PARAM(zfs, , dmu_prefetch_max, UINT, ZMOD_RW, 3069 "Limit one prefetch call to this size"); 3070 3071 ZFS_MODULE_PARAM(zfs, , dmu_ddt_copies, UINT, ZMOD_RW, 3072 "Override copies= for dedup objects"); 3073