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