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