1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2011, 2020 by Delphix. All rights reserved. 24 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved. 25 * Copyright (c) 2013, Joyent, Inc. All rights reserved. 26 * Copyright (c) 2016, Nexenta Systems, Inc. All rights reserved. 27 * Copyright (c) 2015 by Chunwei Chen. All rights reserved. 28 * Copyright (c) 2019 Datto Inc. 29 * Copyright (c) 2019, Klara Inc. 30 * Copyright (c) 2019, Allan Jude 31 */ 32 33 #include <sys/dmu.h> 34 #include <sys/dmu_impl.h> 35 #include <sys/dmu_tx.h> 36 #include <sys/dbuf.h> 37 #include <sys/dnode.h> 38 #include <sys/zfs_context.h> 39 #include <sys/dmu_objset.h> 40 #include <sys/dmu_traverse.h> 41 #include <sys/dsl_dataset.h> 42 #include <sys/dsl_dir.h> 43 #include <sys/dsl_pool.h> 44 #include <sys/dsl_synctask.h> 45 #include <sys/dsl_prop.h> 46 #include <sys/dmu_zfetch.h> 47 #include <sys/zfs_ioctl.h> 48 #include <sys/zap.h> 49 #include <sys/zio_checksum.h> 50 #include <sys/zio_compress.h> 51 #include <sys/sa.h> 52 #include <sys/zfeature.h> 53 #include <sys/abd.h> 54 #include <sys/trace_zfs.h> 55 #include <sys/zfs_racct.h> 56 #include <sys/zfs_rlock.h> 57 #ifdef _KERNEL 58 #include <sys/vmsystm.h> 59 #include <sys/zfs_znode.h> 60 #endif 61 62 /* 63 * Enable/disable nopwrite feature. 64 */ 65 int zfs_nopwrite_enabled = 1; 66 67 /* 68 * Tunable to control percentage of dirtied L1 blocks from frees allowed into 69 * one TXG. After this threshold is crossed, additional dirty blocks from frees 70 * will wait until the next TXG. 71 * A value of zero will disable this throttle. 72 */ 73 unsigned long zfs_per_txg_dirty_frees_percent = 5; 74 75 /* 76 * Enable/disable forcing txg sync when dirty in dmu_offset_next. 77 */ 78 int zfs_dmu_offset_next_sync = 0; 79 80 /* 81 * Limit the amount we can prefetch with one call to this amount. This 82 * helps to limit the amount of memory that can be used by prefetching. 83 * Larger objects should be prefetched a bit at a time. 84 */ 85 int dmu_prefetch_max = 8 * SPA_MAXBLOCKSIZE; 86 87 const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = { 88 {DMU_BSWAP_UINT8, TRUE, FALSE, FALSE, "unallocated" }, 89 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "object directory" }, 90 {DMU_BSWAP_UINT64, TRUE, TRUE, FALSE, "object array" }, 91 {DMU_BSWAP_UINT8, TRUE, FALSE, FALSE, "packed nvlist" }, 92 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "packed nvlist size" }, 93 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "bpobj" }, 94 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "bpobj header" }, 95 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "SPA space map header" }, 96 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "SPA space map" }, 97 {DMU_BSWAP_UINT64, TRUE, FALSE, TRUE, "ZIL intent log" }, 98 {DMU_BSWAP_DNODE, TRUE, FALSE, TRUE, "DMU dnode" }, 99 {DMU_BSWAP_OBJSET, TRUE, TRUE, FALSE, "DMU objset" }, 100 {DMU_BSWAP_UINT64, TRUE, TRUE, FALSE, "DSL directory" }, 101 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL directory child map"}, 102 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL dataset snap map" }, 103 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL props" }, 104 {DMU_BSWAP_UINT64, TRUE, TRUE, FALSE, "DSL dataset" }, 105 {DMU_BSWAP_ZNODE, TRUE, FALSE, FALSE, "ZFS znode" }, 106 {DMU_BSWAP_OLDACL, TRUE, FALSE, TRUE, "ZFS V0 ACL" }, 107 {DMU_BSWAP_UINT8, FALSE, FALSE, TRUE, "ZFS plain file" }, 108 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "ZFS directory" }, 109 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "ZFS master node" }, 110 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "ZFS delete queue" }, 111 {DMU_BSWAP_UINT8, FALSE, FALSE, TRUE, "zvol object" }, 112 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "zvol prop" }, 113 {DMU_BSWAP_UINT8, FALSE, FALSE, TRUE, "other uint8[]" }, 114 {DMU_BSWAP_UINT64, FALSE, FALSE, TRUE, "other uint64[]" }, 115 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "other ZAP" }, 116 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "persistent error log" }, 117 {DMU_BSWAP_UINT8, TRUE, FALSE, FALSE, "SPA history" }, 118 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "SPA history offsets" }, 119 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "Pool properties" }, 120 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL permissions" }, 121 {DMU_BSWAP_ACL, TRUE, FALSE, TRUE, "ZFS ACL" }, 122 {DMU_BSWAP_UINT8, TRUE, FALSE, TRUE, "ZFS SYSACL" }, 123 {DMU_BSWAP_UINT8, TRUE, FALSE, TRUE, "FUID table" }, 124 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "FUID table size" }, 125 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL dataset next clones"}, 126 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "scan work queue" }, 127 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "ZFS user/group/project used" }, 128 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "ZFS user/group/project quota"}, 129 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "snapshot refcount tags"}, 130 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "DDT ZAP algorithm" }, 131 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "DDT statistics" }, 132 {DMU_BSWAP_UINT8, TRUE, FALSE, TRUE, "System attributes" }, 133 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "SA master node" }, 134 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "SA attr registration" }, 135 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "SA attr layouts" }, 136 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "scan translations" }, 137 {DMU_BSWAP_UINT8, FALSE, FALSE, TRUE, "deduplicated block" }, 138 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL deadlist map" }, 139 {DMU_BSWAP_UINT64, TRUE, TRUE, FALSE, "DSL deadlist map hdr" }, 140 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL dir clones" }, 141 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "bpobj subobj" } 142 }; 143 144 const dmu_object_byteswap_info_t dmu_ot_byteswap[DMU_BSWAP_NUMFUNCS] = { 145 { byteswap_uint8_array, "uint8" }, 146 { byteswap_uint16_array, "uint16" }, 147 { byteswap_uint32_array, "uint32" }, 148 { byteswap_uint64_array, "uint64" }, 149 { zap_byteswap, "zap" }, 150 { dnode_buf_byteswap, "dnode" }, 151 { dmu_objset_byteswap, "objset" }, 152 { zfs_znode_byteswap, "znode" }, 153 { zfs_oldacl_byteswap, "oldacl" }, 154 { zfs_acl_byteswap, "acl" } 155 }; 156 157 static int 158 dmu_buf_hold_noread_by_dnode(dnode_t *dn, uint64_t offset, 159 void *tag, dmu_buf_t **dbp) 160 { 161 uint64_t blkid; 162 dmu_buf_impl_t *db; 163 164 rw_enter(&dn->dn_struct_rwlock, RW_READER); 165 blkid = dbuf_whichblock(dn, 0, offset); 166 db = dbuf_hold(dn, blkid, tag); 167 rw_exit(&dn->dn_struct_rwlock); 168 169 if (db == NULL) { 170 *dbp = NULL; 171 return (SET_ERROR(EIO)); 172 } 173 174 *dbp = &db->db; 175 return (0); 176 } 177 int 178 dmu_buf_hold_noread(objset_t *os, uint64_t object, uint64_t offset, 179 void *tag, dmu_buf_t **dbp) 180 { 181 dnode_t *dn; 182 uint64_t blkid; 183 dmu_buf_impl_t *db; 184 int err; 185 186 err = dnode_hold(os, object, FTAG, &dn); 187 if (err) 188 return (err); 189 rw_enter(&dn->dn_struct_rwlock, RW_READER); 190 blkid = dbuf_whichblock(dn, 0, offset); 191 db = dbuf_hold(dn, blkid, tag); 192 rw_exit(&dn->dn_struct_rwlock); 193 dnode_rele(dn, FTAG); 194 195 if (db == NULL) { 196 *dbp = NULL; 197 return (SET_ERROR(EIO)); 198 } 199 200 *dbp = &db->db; 201 return (err); 202 } 203 204 int 205 dmu_buf_hold_by_dnode(dnode_t *dn, uint64_t offset, 206 void *tag, dmu_buf_t **dbp, int flags) 207 { 208 int err; 209 int db_flags = DB_RF_CANFAIL; 210 211 if (flags & DMU_READ_NO_PREFETCH) 212 db_flags |= DB_RF_NOPREFETCH; 213 if (flags & DMU_READ_NO_DECRYPT) 214 db_flags |= DB_RF_NO_DECRYPT; 215 216 err = dmu_buf_hold_noread_by_dnode(dn, offset, tag, dbp); 217 if (err == 0) { 218 dmu_buf_impl_t *db = (dmu_buf_impl_t *)(*dbp); 219 err = dbuf_read(db, NULL, db_flags); 220 if (err != 0) { 221 dbuf_rele(db, tag); 222 *dbp = NULL; 223 } 224 } 225 226 return (err); 227 } 228 229 int 230 dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset, 231 void *tag, dmu_buf_t **dbp, int flags) 232 { 233 int err; 234 int db_flags = DB_RF_CANFAIL; 235 236 if (flags & DMU_READ_NO_PREFETCH) 237 db_flags |= DB_RF_NOPREFETCH; 238 if (flags & DMU_READ_NO_DECRYPT) 239 db_flags |= DB_RF_NO_DECRYPT; 240 241 err = dmu_buf_hold_noread(os, object, offset, tag, dbp); 242 if (err == 0) { 243 dmu_buf_impl_t *db = (dmu_buf_impl_t *)(*dbp); 244 err = dbuf_read(db, NULL, db_flags); 245 if (err != 0) { 246 dbuf_rele(db, tag); 247 *dbp = NULL; 248 } 249 } 250 251 return (err); 252 } 253 254 int 255 dmu_bonus_max(void) 256 { 257 return (DN_OLD_MAX_BONUSLEN); 258 } 259 260 int 261 dmu_set_bonus(dmu_buf_t *db_fake, int newsize, dmu_tx_t *tx) 262 { 263 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 264 dnode_t *dn; 265 int error; 266 267 DB_DNODE_ENTER(db); 268 dn = DB_DNODE(db); 269 270 if (dn->dn_bonus != db) { 271 error = SET_ERROR(EINVAL); 272 } else if (newsize < 0 || newsize > db_fake->db_size) { 273 error = SET_ERROR(EINVAL); 274 } else { 275 dnode_setbonuslen(dn, newsize, tx); 276 error = 0; 277 } 278 279 DB_DNODE_EXIT(db); 280 return (error); 281 } 282 283 int 284 dmu_set_bonustype(dmu_buf_t *db_fake, dmu_object_type_t type, dmu_tx_t *tx) 285 { 286 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 287 dnode_t *dn; 288 int error; 289 290 DB_DNODE_ENTER(db); 291 dn = DB_DNODE(db); 292 293 if (!DMU_OT_IS_VALID(type)) { 294 error = SET_ERROR(EINVAL); 295 } else if (dn->dn_bonus != db) { 296 error = SET_ERROR(EINVAL); 297 } else { 298 dnode_setbonus_type(dn, type, tx); 299 error = 0; 300 } 301 302 DB_DNODE_EXIT(db); 303 return (error); 304 } 305 306 dmu_object_type_t 307 dmu_get_bonustype(dmu_buf_t *db_fake) 308 { 309 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 310 dnode_t *dn; 311 dmu_object_type_t type; 312 313 DB_DNODE_ENTER(db); 314 dn = DB_DNODE(db); 315 type = dn->dn_bonustype; 316 DB_DNODE_EXIT(db); 317 318 return (type); 319 } 320 321 int 322 dmu_rm_spill(objset_t *os, uint64_t object, dmu_tx_t *tx) 323 { 324 dnode_t *dn; 325 int error; 326 327 error = dnode_hold(os, object, FTAG, &dn); 328 dbuf_rm_spill(dn, tx); 329 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 330 dnode_rm_spill(dn, tx); 331 rw_exit(&dn->dn_struct_rwlock); 332 dnode_rele(dn, FTAG); 333 return (error); 334 } 335 336 /* 337 * Lookup and hold the bonus buffer for the provided dnode. If the dnode 338 * has not yet been allocated a new bonus dbuf a will be allocated. 339 * Returns ENOENT, EIO, or 0. 340 */ 341 int dmu_bonus_hold_by_dnode(dnode_t *dn, void *tag, dmu_buf_t **dbp, 342 uint32_t flags) 343 { 344 dmu_buf_impl_t *db; 345 int error; 346 uint32_t db_flags = DB_RF_MUST_SUCCEED; 347 348 if (flags & DMU_READ_NO_PREFETCH) 349 db_flags |= DB_RF_NOPREFETCH; 350 if (flags & DMU_READ_NO_DECRYPT) 351 db_flags |= DB_RF_NO_DECRYPT; 352 353 rw_enter(&dn->dn_struct_rwlock, RW_READER); 354 if (dn->dn_bonus == NULL) { 355 rw_exit(&dn->dn_struct_rwlock); 356 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 357 if (dn->dn_bonus == NULL) 358 dbuf_create_bonus(dn); 359 } 360 db = dn->dn_bonus; 361 362 /* as long as the bonus buf is held, the dnode will be held */ 363 if (zfs_refcount_add(&db->db_holds, tag) == 1) { 364 VERIFY(dnode_add_ref(dn, db)); 365 atomic_inc_32(&dn->dn_dbufs_count); 366 } 367 368 /* 369 * Wait to drop dn_struct_rwlock until after adding the bonus dbuf's 370 * hold and incrementing the dbuf count to ensure that dnode_move() sees 371 * a dnode hold for every dbuf. 372 */ 373 rw_exit(&dn->dn_struct_rwlock); 374 375 error = dbuf_read(db, NULL, db_flags); 376 if (error) { 377 dnode_evict_bonus(dn); 378 dbuf_rele(db, tag); 379 *dbp = NULL; 380 return (error); 381 } 382 383 *dbp = &db->db; 384 return (0); 385 } 386 387 int 388 dmu_bonus_hold(objset_t *os, uint64_t object, void *tag, dmu_buf_t **dbp) 389 { 390 dnode_t *dn; 391 int error; 392 393 error = dnode_hold(os, object, FTAG, &dn); 394 if (error) 395 return (error); 396 397 error = dmu_bonus_hold_by_dnode(dn, tag, dbp, DMU_READ_NO_PREFETCH); 398 dnode_rele(dn, FTAG); 399 400 return (error); 401 } 402 403 /* 404 * returns ENOENT, EIO, or 0. 405 * 406 * This interface will allocate a blank spill dbuf when a spill blk 407 * doesn't already exist on the dnode. 408 * 409 * if you only want to find an already existing spill db, then 410 * dmu_spill_hold_existing() should be used. 411 */ 412 int 413 dmu_spill_hold_by_dnode(dnode_t *dn, uint32_t flags, void *tag, dmu_buf_t **dbp) 414 { 415 dmu_buf_impl_t *db = NULL; 416 int err; 417 418 if ((flags & DB_RF_HAVESTRUCT) == 0) 419 rw_enter(&dn->dn_struct_rwlock, RW_READER); 420 421 db = dbuf_hold(dn, DMU_SPILL_BLKID, tag); 422 423 if ((flags & DB_RF_HAVESTRUCT) == 0) 424 rw_exit(&dn->dn_struct_rwlock); 425 426 if (db == NULL) { 427 *dbp = NULL; 428 return (SET_ERROR(EIO)); 429 } 430 err = dbuf_read(db, NULL, flags); 431 if (err == 0) 432 *dbp = &db->db; 433 else { 434 dbuf_rele(db, tag); 435 *dbp = NULL; 436 } 437 return (err); 438 } 439 440 int 441 dmu_spill_hold_existing(dmu_buf_t *bonus, void *tag, dmu_buf_t **dbp) 442 { 443 dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus; 444 dnode_t *dn; 445 int err; 446 447 DB_DNODE_ENTER(db); 448 dn = DB_DNODE(db); 449 450 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_SA) { 451 err = SET_ERROR(EINVAL); 452 } else { 453 rw_enter(&dn->dn_struct_rwlock, RW_READER); 454 455 if (!dn->dn_have_spill) { 456 err = SET_ERROR(ENOENT); 457 } else { 458 err = dmu_spill_hold_by_dnode(dn, 459 DB_RF_HAVESTRUCT | DB_RF_CANFAIL, tag, dbp); 460 } 461 462 rw_exit(&dn->dn_struct_rwlock); 463 } 464 465 DB_DNODE_EXIT(db); 466 return (err); 467 } 468 469 int 470 dmu_spill_hold_by_bonus(dmu_buf_t *bonus, uint32_t flags, void *tag, 471 dmu_buf_t **dbp) 472 { 473 dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus; 474 dnode_t *dn; 475 int err; 476 uint32_t db_flags = DB_RF_CANFAIL; 477 478 if (flags & DMU_READ_NO_DECRYPT) 479 db_flags |= DB_RF_NO_DECRYPT; 480 481 DB_DNODE_ENTER(db); 482 dn = DB_DNODE(db); 483 err = dmu_spill_hold_by_dnode(dn, db_flags, tag, dbp); 484 DB_DNODE_EXIT(db); 485 486 return (err); 487 } 488 489 /* 490 * Note: longer-term, we should modify all of the dmu_buf_*() interfaces 491 * to take a held dnode rather than <os, object> -- the lookup is wasteful, 492 * and can induce severe lock contention when writing to several files 493 * whose dnodes are in the same block. 494 */ 495 int 496 dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset, uint64_t length, 497 boolean_t read, void *tag, int *numbufsp, dmu_buf_t ***dbpp, uint32_t flags) 498 { 499 dmu_buf_t **dbp; 500 zstream_t *zs = NULL; 501 uint64_t blkid, nblks, i; 502 uint32_t dbuf_flags; 503 int err; 504 zio_t *zio = NULL; 505 boolean_t missed = B_FALSE; 506 507 ASSERT(length <= DMU_MAX_ACCESS); 508 509 /* 510 * Note: We directly notify the prefetch code of this read, so that 511 * we can tell it about the multi-block read. dbuf_read() only knows 512 * about the one block it is accessing. 513 */ 514 dbuf_flags = DB_RF_CANFAIL | DB_RF_NEVERWAIT | DB_RF_HAVESTRUCT | 515 DB_RF_NOPREFETCH; 516 517 rw_enter(&dn->dn_struct_rwlock, RW_READER); 518 if (dn->dn_datablkshift) { 519 int blkshift = dn->dn_datablkshift; 520 nblks = (P2ROUNDUP(offset + length, 1ULL << blkshift) - 521 P2ALIGN(offset, 1ULL << blkshift)) >> 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 DNODE_META_IS_CACHEABLE(dn) && length <= zfetch_array_rd_sz) { 543 /* 544 * Prepare the zfetch before initiating the demand reads, so 545 * that if multiple threads block on same indirect block, we 546 * base predictions on the original less racy request order. 547 */ 548 zs = dmu_zfetch_prepare(&dn->dn_zfetch, blkid, nblks, 549 read && DNODE_IS_CACHEABLE(dn), B_TRUE); 550 } 551 for (i = 0; i < nblks; i++) { 552 dmu_buf_impl_t *db = dbuf_hold(dn, blkid + i, tag); 553 if (db == NULL) { 554 if (zs) 555 dmu_zfetch_run(zs, missed, B_TRUE); 556 rw_exit(&dn->dn_struct_rwlock); 557 dmu_buf_rele_array(dbp, nblks, tag); 558 if (read) 559 zio_nowait(zio); 560 return (SET_ERROR(EIO)); 561 } 562 563 /* 564 * Initiate async demand data read. 565 * We check the db_state after calling dbuf_read() because 566 * (1) dbuf_read() may change the state to CACHED due to a 567 * hit in the ARC, and (2) on a cache miss, a child will 568 * have been added to "zio" but not yet completed, so the 569 * state will not yet be CACHED. 570 */ 571 if (read) { 572 (void) dbuf_read(db, zio, dbuf_flags); 573 if (db->db_state != DB_CACHED) 574 missed = B_TRUE; 575 } 576 dbp[i] = &db->db; 577 } 578 579 if (!read) 580 zfs_racct_write(length, nblks); 581 582 if (zs) 583 dmu_zfetch_run(zs, missed, B_TRUE); 584 rw_exit(&dn->dn_struct_rwlock); 585 586 if (read) { 587 /* wait for async read i/o */ 588 err = zio_wait(zio); 589 if (err) { 590 dmu_buf_rele_array(dbp, nblks, tag); 591 return (err); 592 } 593 594 /* wait for other io to complete */ 595 for (i = 0; i < nblks; i++) { 596 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i]; 597 mutex_enter(&db->db_mtx); 598 while (db->db_state == DB_READ || 599 db->db_state == DB_FILL) 600 cv_wait(&db->db_changed, &db->db_mtx); 601 if (db->db_state == DB_UNCACHED) 602 err = SET_ERROR(EIO); 603 mutex_exit(&db->db_mtx); 604 if (err) { 605 dmu_buf_rele_array(dbp, nblks, tag); 606 return (err); 607 } 608 } 609 } 610 611 *numbufsp = nblks; 612 *dbpp = dbp; 613 return (0); 614 } 615 616 int 617 dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset, 618 uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp) 619 { 620 dnode_t *dn; 621 int err; 622 623 err = dnode_hold(os, object, FTAG, &dn); 624 if (err) 625 return (err); 626 627 err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag, 628 numbufsp, dbpp, DMU_READ_PREFETCH); 629 630 dnode_rele(dn, FTAG); 631 632 return (err); 633 } 634 635 int 636 dmu_buf_hold_array_by_bonus(dmu_buf_t *db_fake, uint64_t offset, 637 uint64_t length, boolean_t read, void *tag, int *numbufsp, 638 dmu_buf_t ***dbpp) 639 { 640 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 641 dnode_t *dn; 642 int err; 643 644 DB_DNODE_ENTER(db); 645 dn = DB_DNODE(db); 646 err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag, 647 numbufsp, dbpp, DMU_READ_PREFETCH); 648 DB_DNODE_EXIT(db); 649 650 return (err); 651 } 652 653 void 654 dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, void *tag) 655 { 656 int i; 657 dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake; 658 659 if (numbufs == 0) 660 return; 661 662 for (i = 0; i < numbufs; i++) { 663 if (dbp[i]) 664 dbuf_rele(dbp[i], tag); 665 } 666 667 kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs); 668 } 669 670 /* 671 * Issue prefetch i/os for the given blocks. If level is greater than 0, the 672 * indirect blocks prefetched will be those that point to the blocks containing 673 * the data starting at offset, and continuing to offset + len. 674 * 675 * Note that if the indirect blocks above the blocks being prefetched are not 676 * in cache, they will be asynchronously read in. 677 */ 678 void 679 dmu_prefetch(objset_t *os, uint64_t object, int64_t level, uint64_t offset, 680 uint64_t len, zio_priority_t pri) 681 { 682 dnode_t *dn; 683 uint64_t blkid; 684 int nblks, err; 685 686 if (len == 0) { /* they're interested in the bonus buffer */ 687 dn = DMU_META_DNODE(os); 688 689 if (object == 0 || object >= DN_MAX_OBJECT) 690 return; 691 692 rw_enter(&dn->dn_struct_rwlock, RW_READER); 693 blkid = dbuf_whichblock(dn, level, 694 object * sizeof (dnode_phys_t)); 695 dbuf_prefetch(dn, level, blkid, pri, 0); 696 rw_exit(&dn->dn_struct_rwlock); 697 return; 698 } 699 700 /* 701 * See comment before the definition of dmu_prefetch_max. 702 */ 703 len = MIN(len, dmu_prefetch_max); 704 705 /* 706 * XXX - Note, if the dnode for the requested object is not 707 * already cached, we will do a *synchronous* read in the 708 * dnode_hold() call. The same is true for any indirects. 709 */ 710 err = dnode_hold(os, object, FTAG, &dn); 711 if (err != 0) 712 return; 713 714 /* 715 * offset + len - 1 is the last byte we want to prefetch for, and offset 716 * is the first. Then dbuf_whichblk(dn, level, off + len - 1) is the 717 * last block we want to prefetch, and dbuf_whichblock(dn, level, 718 * offset) is the first. Then the number we need to prefetch is the 719 * last - first + 1. 720 */ 721 rw_enter(&dn->dn_struct_rwlock, RW_READER); 722 if (level > 0 || dn->dn_datablkshift != 0) { 723 nblks = dbuf_whichblock(dn, level, offset + len - 1) - 724 dbuf_whichblock(dn, level, offset) + 1; 725 } else { 726 nblks = (offset < dn->dn_datablksz); 727 } 728 729 if (nblks != 0) { 730 blkid = dbuf_whichblock(dn, level, offset); 731 for (int i = 0; i < nblks; i++) 732 dbuf_prefetch(dn, level, blkid + i, pri, 0); 733 } 734 rw_exit(&dn->dn_struct_rwlock); 735 736 dnode_rele(dn, FTAG); 737 } 738 739 /* 740 * Get the next "chunk" of file data to free. We traverse the file from 741 * the end so that the file gets shorter over time (if we crashes in the 742 * middle, this will leave us in a better state). We find allocated file 743 * data by simply searching the allocated level 1 indirects. 744 * 745 * On input, *start should be the first offset that does not need to be 746 * freed (e.g. "offset + length"). On return, *start will be the first 747 * offset that should be freed and l1blks is set to the number of level 1 748 * indirect blocks found within the chunk. 749 */ 750 static int 751 get_next_chunk(dnode_t *dn, uint64_t *start, uint64_t minimum, uint64_t *l1blks) 752 { 753 uint64_t blks; 754 uint64_t maxblks = DMU_MAX_ACCESS >> (dn->dn_indblkshift + 1); 755 /* bytes of data covered by a level-1 indirect block */ 756 uint64_t iblkrange = (uint64_t)dn->dn_datablksz * 757 EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT); 758 759 ASSERT3U(minimum, <=, *start); 760 761 /* 762 * Check if we can free the entire range assuming that all of the 763 * L1 blocks in this range have data. If we can, we use this 764 * worst case value as an estimate so we can avoid having to look 765 * at the object's actual data. 766 */ 767 uint64_t total_l1blks = 768 (roundup(*start, iblkrange) - (minimum / iblkrange * iblkrange)) / 769 iblkrange; 770 if (total_l1blks <= maxblks) { 771 *l1blks = total_l1blks; 772 *start = minimum; 773 return (0); 774 } 775 ASSERT(ISP2(iblkrange)); 776 777 for (blks = 0; *start > minimum && blks < maxblks; blks++) { 778 int err; 779 780 /* 781 * dnode_next_offset(BACKWARDS) will find an allocated L1 782 * indirect block at or before the input offset. We must 783 * decrement *start so that it is at the end of the region 784 * to search. 785 */ 786 (*start)--; 787 788 err = dnode_next_offset(dn, 789 DNODE_FIND_BACKWARDS, start, 2, 1, 0); 790 791 /* if there are no indirect blocks before start, we are done */ 792 if (err == ESRCH) { 793 *start = minimum; 794 break; 795 } else if (err != 0) { 796 *l1blks = blks; 797 return (err); 798 } 799 800 /* set start to the beginning of this L1 indirect */ 801 *start = P2ALIGN(*start, iblkrange); 802 } 803 if (*start < minimum) 804 *start = minimum; 805 *l1blks = blks; 806 807 return (0); 808 } 809 810 /* 811 * If this objset is of type OST_ZFS return true if vfs's unmounted flag is set, 812 * otherwise return false. 813 * Used below in dmu_free_long_range_impl() to enable abort when unmounting 814 */ 815 /*ARGSUSED*/ 816 static boolean_t 817 dmu_objset_zfs_unmounting(objset_t *os) 818 { 819 #ifdef _KERNEL 820 if (dmu_objset_type(os) == DMU_OST_ZFS) 821 return (zfs_get_vfs_flag_unmounted(os)); 822 #endif 823 return (B_FALSE); 824 } 825 826 static int 827 dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset, 828 uint64_t length) 829 { 830 uint64_t object_size; 831 int err; 832 uint64_t dirty_frees_threshold; 833 dsl_pool_t *dp = dmu_objset_pool(os); 834 835 if (dn == NULL) 836 return (SET_ERROR(EINVAL)); 837 838 object_size = (dn->dn_maxblkid + 1) * dn->dn_datablksz; 839 if (offset >= object_size) 840 return (0); 841 842 if (zfs_per_txg_dirty_frees_percent <= 100) 843 dirty_frees_threshold = 844 zfs_per_txg_dirty_frees_percent * zfs_dirty_data_max / 100; 845 else 846 dirty_frees_threshold = zfs_dirty_data_max / 20; 847 848 if (length == DMU_OBJECT_END || offset + length > object_size) 849 length = object_size - offset; 850 851 while (length != 0) { 852 uint64_t chunk_end, chunk_begin, chunk_len; 853 uint64_t l1blks; 854 dmu_tx_t *tx; 855 856 if (dmu_objset_zfs_unmounting(dn->dn_objset)) 857 return (SET_ERROR(EINTR)); 858 859 chunk_end = chunk_begin = offset + length; 860 861 /* move chunk_begin backwards to the beginning of this chunk */ 862 err = get_next_chunk(dn, &chunk_begin, offset, &l1blks); 863 if (err) 864 return (err); 865 ASSERT3U(chunk_begin, >=, offset); 866 ASSERT3U(chunk_begin, <=, chunk_end); 867 868 chunk_len = chunk_end - chunk_begin; 869 870 tx = dmu_tx_create(os); 871 dmu_tx_hold_free(tx, dn->dn_object, chunk_begin, chunk_len); 872 873 /* 874 * Mark this transaction as typically resulting in a net 875 * reduction in space used. 876 */ 877 dmu_tx_mark_netfree(tx); 878 err = dmu_tx_assign(tx, TXG_WAIT); 879 if (err) { 880 dmu_tx_abort(tx); 881 return (err); 882 } 883 884 uint64_t txg = dmu_tx_get_txg(tx); 885 886 mutex_enter(&dp->dp_lock); 887 uint64_t long_free_dirty = 888 dp->dp_long_free_dirty_pertxg[txg & TXG_MASK]; 889 mutex_exit(&dp->dp_lock); 890 891 /* 892 * To avoid filling up a TXG with just frees, wait for 893 * the next TXG to open before freeing more chunks if 894 * we have reached the threshold of frees. 895 */ 896 if (dirty_frees_threshold != 0 && 897 long_free_dirty >= dirty_frees_threshold) { 898 DMU_TX_STAT_BUMP(dmu_tx_dirty_frees_delay); 899 dmu_tx_commit(tx); 900 txg_wait_open(dp, 0, B_TRUE); 901 continue; 902 } 903 904 /* 905 * In order to prevent unnecessary write throttling, for each 906 * TXG, we track the cumulative size of L1 blocks being dirtied 907 * in dnode_free_range() below. We compare this number to a 908 * tunable threshold, past which we prevent new L1 dirty freeing 909 * blocks from being added into the open TXG. See 910 * dmu_free_long_range_impl() for details. The threshold 911 * prevents write throttle activation due to dirty freeing L1 912 * blocks taking up a large percentage of zfs_dirty_data_max. 913 */ 914 mutex_enter(&dp->dp_lock); 915 dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] += 916 l1blks << dn->dn_indblkshift; 917 mutex_exit(&dp->dp_lock); 918 DTRACE_PROBE3(free__long__range, 919 uint64_t, long_free_dirty, uint64_t, chunk_len, 920 uint64_t, txg); 921 dnode_free_range(dn, chunk_begin, chunk_len, tx); 922 923 dmu_tx_commit(tx); 924 925 length -= chunk_len; 926 } 927 return (0); 928 } 929 930 int 931 dmu_free_long_range(objset_t *os, uint64_t object, 932 uint64_t offset, uint64_t length) 933 { 934 dnode_t *dn; 935 int err; 936 937 err = dnode_hold(os, object, FTAG, &dn); 938 if (err != 0) 939 return (err); 940 err = dmu_free_long_range_impl(os, dn, offset, length); 941 942 /* 943 * It is important to zero out the maxblkid when freeing the entire 944 * file, so that (a) subsequent calls to dmu_free_long_range_impl() 945 * will take the fast path, and (b) dnode_reallocate() can verify 946 * that the entire file has been freed. 947 */ 948 if (err == 0 && offset == 0 && length == DMU_OBJECT_END) 949 dn->dn_maxblkid = 0; 950 951 dnode_rele(dn, FTAG); 952 return (err); 953 } 954 955 int 956 dmu_free_long_object(objset_t *os, uint64_t object) 957 { 958 dmu_tx_t *tx; 959 int err; 960 961 err = dmu_free_long_range(os, object, 0, DMU_OBJECT_END); 962 if (err != 0) 963 return (err); 964 965 tx = dmu_tx_create(os); 966 dmu_tx_hold_bonus(tx, object); 967 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END); 968 dmu_tx_mark_netfree(tx); 969 err = dmu_tx_assign(tx, TXG_WAIT); 970 if (err == 0) { 971 err = dmu_object_free(os, object, tx); 972 dmu_tx_commit(tx); 973 } else { 974 dmu_tx_abort(tx); 975 } 976 977 return (err); 978 } 979 980 int 981 dmu_free_range(objset_t *os, uint64_t object, uint64_t offset, 982 uint64_t size, dmu_tx_t *tx) 983 { 984 dnode_t *dn; 985 int err = dnode_hold(os, object, FTAG, &dn); 986 if (err) 987 return (err); 988 ASSERT(offset < UINT64_MAX); 989 ASSERT(size == DMU_OBJECT_END || size <= UINT64_MAX - offset); 990 dnode_free_range(dn, offset, size, tx); 991 dnode_rele(dn, FTAG); 992 return (0); 993 } 994 995 static int 996 dmu_read_impl(dnode_t *dn, uint64_t offset, uint64_t size, 997 void *buf, uint32_t flags) 998 { 999 dmu_buf_t **dbp; 1000 int numbufs, err = 0; 1001 1002 /* 1003 * Deal with odd block sizes, where there can't be data past the first 1004 * block. If we ever do the tail block optimization, we will need to 1005 * handle that here as well. 1006 */ 1007 if (dn->dn_maxblkid == 0) { 1008 uint64_t newsz = offset > dn->dn_datablksz ? 0 : 1009 MIN(size, dn->dn_datablksz - offset); 1010 bzero((char *)buf + newsz, size - newsz); 1011 size = newsz; 1012 } 1013 1014 while (size > 0) { 1015 uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2); 1016 int i; 1017 1018 /* 1019 * NB: we could do this block-at-a-time, but it's nice 1020 * to be reading in parallel. 1021 */ 1022 err = dmu_buf_hold_array_by_dnode(dn, offset, mylen, 1023 TRUE, FTAG, &numbufs, &dbp, flags); 1024 if (err) 1025 break; 1026 1027 for (i = 0; i < numbufs; i++) { 1028 uint64_t tocpy; 1029 int64_t bufoff; 1030 dmu_buf_t *db = dbp[i]; 1031 1032 ASSERT(size > 0); 1033 1034 bufoff = offset - db->db_offset; 1035 tocpy = MIN(db->db_size - bufoff, size); 1036 1037 (void) memcpy(buf, (char *)db->db_data + bufoff, tocpy); 1038 1039 offset += tocpy; 1040 size -= tocpy; 1041 buf = (char *)buf + tocpy; 1042 } 1043 dmu_buf_rele_array(dbp, numbufs, FTAG); 1044 } 1045 return (err); 1046 } 1047 1048 int 1049 dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 1050 void *buf, uint32_t flags) 1051 { 1052 dnode_t *dn; 1053 int err; 1054 1055 err = dnode_hold(os, object, FTAG, &dn); 1056 if (err != 0) 1057 return (err); 1058 1059 err = dmu_read_impl(dn, offset, size, buf, flags); 1060 dnode_rele(dn, FTAG); 1061 return (err); 1062 } 1063 1064 int 1065 dmu_read_by_dnode(dnode_t *dn, uint64_t offset, uint64_t size, void *buf, 1066 uint32_t flags) 1067 { 1068 return (dmu_read_impl(dn, offset, size, buf, flags)); 1069 } 1070 1071 static void 1072 dmu_write_impl(dmu_buf_t **dbp, int numbufs, uint64_t offset, uint64_t size, 1073 const void *buf, dmu_tx_t *tx) 1074 { 1075 int i; 1076 1077 for (i = 0; i < numbufs; i++) { 1078 uint64_t tocpy; 1079 int64_t bufoff; 1080 dmu_buf_t *db = dbp[i]; 1081 1082 ASSERT(size > 0); 1083 1084 bufoff = offset - db->db_offset; 1085 tocpy = MIN(db->db_size - bufoff, size); 1086 1087 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size); 1088 1089 if (tocpy == db->db_size) 1090 dmu_buf_will_fill(db, tx); 1091 else 1092 dmu_buf_will_dirty(db, tx); 1093 1094 (void) memcpy((char *)db->db_data + bufoff, buf, tocpy); 1095 1096 if (tocpy == db->db_size) 1097 dmu_buf_fill_done(db, tx); 1098 1099 offset += tocpy; 1100 size -= tocpy; 1101 buf = (char *)buf + tocpy; 1102 } 1103 } 1104 1105 void 1106 dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 1107 const void *buf, dmu_tx_t *tx) 1108 { 1109 dmu_buf_t **dbp; 1110 int numbufs; 1111 1112 if (size == 0) 1113 return; 1114 1115 VERIFY0(dmu_buf_hold_array(os, object, offset, size, 1116 FALSE, FTAG, &numbufs, &dbp)); 1117 dmu_write_impl(dbp, numbufs, offset, size, buf, tx); 1118 dmu_buf_rele_array(dbp, numbufs, FTAG); 1119 } 1120 1121 /* 1122 * Note: Lustre is an external consumer of this interface. 1123 */ 1124 void 1125 dmu_write_by_dnode(dnode_t *dn, uint64_t offset, uint64_t size, 1126 const void *buf, dmu_tx_t *tx) 1127 { 1128 dmu_buf_t **dbp; 1129 int numbufs; 1130 1131 if (size == 0) 1132 return; 1133 1134 VERIFY0(dmu_buf_hold_array_by_dnode(dn, offset, size, 1135 FALSE, FTAG, &numbufs, &dbp, DMU_READ_PREFETCH)); 1136 dmu_write_impl(dbp, numbufs, offset, size, buf, tx); 1137 dmu_buf_rele_array(dbp, numbufs, FTAG); 1138 } 1139 1140 void 1141 dmu_prealloc(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 1142 dmu_tx_t *tx) 1143 { 1144 dmu_buf_t **dbp; 1145 int numbufs, i; 1146 1147 if (size == 0) 1148 return; 1149 1150 VERIFY(0 == dmu_buf_hold_array(os, object, offset, size, 1151 FALSE, FTAG, &numbufs, &dbp)); 1152 1153 for (i = 0; i < numbufs; i++) { 1154 dmu_buf_t *db = dbp[i]; 1155 1156 dmu_buf_will_not_fill(db, tx); 1157 } 1158 dmu_buf_rele_array(dbp, numbufs, FTAG); 1159 } 1160 1161 void 1162 dmu_write_embedded(objset_t *os, uint64_t object, uint64_t offset, 1163 void *data, uint8_t etype, uint8_t comp, int uncompressed_size, 1164 int compressed_size, int byteorder, dmu_tx_t *tx) 1165 { 1166 dmu_buf_t *db; 1167 1168 ASSERT3U(etype, <, NUM_BP_EMBEDDED_TYPES); 1169 ASSERT3U(comp, <, ZIO_COMPRESS_FUNCTIONS); 1170 VERIFY0(dmu_buf_hold_noread(os, object, offset, 1171 FTAG, &db)); 1172 1173 dmu_buf_write_embedded(db, 1174 data, (bp_embedded_type_t)etype, (enum zio_compress)comp, 1175 uncompressed_size, compressed_size, byteorder, tx); 1176 1177 dmu_buf_rele(db, FTAG); 1178 } 1179 1180 void 1181 dmu_redact(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 1182 dmu_tx_t *tx) 1183 { 1184 int numbufs, i; 1185 dmu_buf_t **dbp; 1186 1187 VERIFY0(dmu_buf_hold_array(os, object, offset, size, FALSE, FTAG, 1188 &numbufs, &dbp)); 1189 for (i = 0; i < numbufs; i++) 1190 dmu_buf_redact(dbp[i], tx); 1191 dmu_buf_rele_array(dbp, numbufs, FTAG); 1192 } 1193 1194 #ifdef _KERNEL 1195 int 1196 dmu_read_uio_dnode(dnode_t *dn, zfs_uio_t *uio, uint64_t size) 1197 { 1198 dmu_buf_t **dbp; 1199 int numbufs, i, err; 1200 1201 /* 1202 * NB: we could do this block-at-a-time, but it's nice 1203 * to be reading in parallel. 1204 */ 1205 err = dmu_buf_hold_array_by_dnode(dn, zfs_uio_offset(uio), size, 1206 TRUE, FTAG, &numbufs, &dbp, 0); 1207 if (err) 1208 return (err); 1209 1210 for (i = 0; i < numbufs; i++) { 1211 uint64_t tocpy; 1212 int64_t bufoff; 1213 dmu_buf_t *db = dbp[i]; 1214 1215 ASSERT(size > 0); 1216 1217 bufoff = zfs_uio_offset(uio) - db->db_offset; 1218 tocpy = MIN(db->db_size - bufoff, size); 1219 1220 err = zfs_uio_fault_move((char *)db->db_data + bufoff, tocpy, 1221 UIO_READ, uio); 1222 1223 if (err) 1224 break; 1225 1226 size -= tocpy; 1227 } 1228 dmu_buf_rele_array(dbp, numbufs, FTAG); 1229 1230 return (err); 1231 } 1232 1233 /* 1234 * Read 'size' bytes into the uio buffer. 1235 * From object zdb->db_object. 1236 * Starting at zfs_uio_offset(uio). 1237 * 1238 * If the caller already has a dbuf in the target object 1239 * (e.g. its bonus buffer), this routine is faster than dmu_read_uio(), 1240 * because we don't have to find the dnode_t for the object. 1241 */ 1242 int 1243 dmu_read_uio_dbuf(dmu_buf_t *zdb, zfs_uio_t *uio, uint64_t size) 1244 { 1245 dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb; 1246 dnode_t *dn; 1247 int err; 1248 1249 if (size == 0) 1250 return (0); 1251 1252 DB_DNODE_ENTER(db); 1253 dn = DB_DNODE(db); 1254 err = dmu_read_uio_dnode(dn, uio, size); 1255 DB_DNODE_EXIT(db); 1256 1257 return (err); 1258 } 1259 1260 /* 1261 * Read 'size' bytes into the uio buffer. 1262 * From the specified object 1263 * Starting at offset zfs_uio_offset(uio). 1264 */ 1265 int 1266 dmu_read_uio(objset_t *os, uint64_t object, zfs_uio_t *uio, uint64_t size) 1267 { 1268 dnode_t *dn; 1269 int err; 1270 1271 if (size == 0) 1272 return (0); 1273 1274 err = dnode_hold(os, object, FTAG, &dn); 1275 if (err) 1276 return (err); 1277 1278 err = dmu_read_uio_dnode(dn, uio, size); 1279 1280 dnode_rele(dn, FTAG); 1281 1282 return (err); 1283 } 1284 1285 int 1286 dmu_write_uio_dnode(dnode_t *dn, zfs_uio_t *uio, uint64_t size, dmu_tx_t *tx) 1287 { 1288 dmu_buf_t **dbp; 1289 int numbufs; 1290 int err = 0; 1291 int i; 1292 1293 err = dmu_buf_hold_array_by_dnode(dn, zfs_uio_offset(uio), size, 1294 FALSE, FTAG, &numbufs, &dbp, DMU_READ_PREFETCH); 1295 if (err) 1296 return (err); 1297 1298 for (i = 0; i < numbufs; i++) { 1299 uint64_t tocpy; 1300 int64_t bufoff; 1301 dmu_buf_t *db = dbp[i]; 1302 1303 ASSERT(size > 0); 1304 1305 bufoff = zfs_uio_offset(uio) - db->db_offset; 1306 tocpy = MIN(db->db_size - bufoff, size); 1307 1308 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size); 1309 1310 if (tocpy == db->db_size) 1311 dmu_buf_will_fill(db, tx); 1312 else 1313 dmu_buf_will_dirty(db, tx); 1314 1315 /* 1316 * XXX zfs_uiomove could block forever (eg.nfs-backed 1317 * pages). There needs to be a uiolockdown() function 1318 * to lock the pages in memory, so that zfs_uiomove won't 1319 * block. 1320 */ 1321 err = zfs_uio_fault_move((char *)db->db_data + bufoff, 1322 tocpy, UIO_WRITE, uio); 1323 1324 if (tocpy == db->db_size) 1325 dmu_buf_fill_done(db, tx); 1326 1327 if (err) 1328 break; 1329 1330 size -= tocpy; 1331 } 1332 1333 dmu_buf_rele_array(dbp, numbufs, FTAG); 1334 return (err); 1335 } 1336 1337 /* 1338 * Write 'size' bytes from the uio buffer. 1339 * To object zdb->db_object. 1340 * Starting at offset zfs_uio_offset(uio). 1341 * 1342 * If the caller already has a dbuf in the target object 1343 * (e.g. its bonus buffer), this routine is faster than dmu_write_uio(), 1344 * because we don't have to find the dnode_t for the object. 1345 */ 1346 int 1347 dmu_write_uio_dbuf(dmu_buf_t *zdb, zfs_uio_t *uio, uint64_t size, 1348 dmu_tx_t *tx) 1349 { 1350 dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb; 1351 dnode_t *dn; 1352 int err; 1353 1354 if (size == 0) 1355 return (0); 1356 1357 DB_DNODE_ENTER(db); 1358 dn = DB_DNODE(db); 1359 err = dmu_write_uio_dnode(dn, uio, size, tx); 1360 DB_DNODE_EXIT(db); 1361 1362 return (err); 1363 } 1364 1365 /* 1366 * Write 'size' bytes from the uio buffer. 1367 * To the specified object. 1368 * Starting at offset zfs_uio_offset(uio). 1369 */ 1370 int 1371 dmu_write_uio(objset_t *os, uint64_t object, zfs_uio_t *uio, uint64_t size, 1372 dmu_tx_t *tx) 1373 { 1374 dnode_t *dn; 1375 int err; 1376 1377 if (size == 0) 1378 return (0); 1379 1380 err = dnode_hold(os, object, FTAG, &dn); 1381 if (err) 1382 return (err); 1383 1384 err = dmu_write_uio_dnode(dn, uio, size, tx); 1385 1386 dnode_rele(dn, FTAG); 1387 1388 return (err); 1389 } 1390 #endif /* _KERNEL */ 1391 1392 /* 1393 * Allocate a loaned anonymous arc buffer. 1394 */ 1395 arc_buf_t * 1396 dmu_request_arcbuf(dmu_buf_t *handle, int size) 1397 { 1398 dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle; 1399 1400 return (arc_loan_buf(db->db_objset->os_spa, B_FALSE, size)); 1401 } 1402 1403 /* 1404 * Free a loaned arc buffer. 1405 */ 1406 void 1407 dmu_return_arcbuf(arc_buf_t *buf) 1408 { 1409 arc_return_buf(buf, FTAG); 1410 arc_buf_destroy(buf, FTAG); 1411 } 1412 1413 /* 1414 * A "lightweight" write is faster than a regular write (e.g. 1415 * dmu_write_by_dnode() or dmu_assign_arcbuf_by_dnode()), because it avoids the 1416 * CPU cost of creating a dmu_buf_impl_t and arc_buf_[hdr_]_t. However, the 1417 * data can not be read or overwritten until the transaction's txg has been 1418 * synced. This makes it appropriate for workloads that are known to be 1419 * (temporarily) write-only, like "zfs receive". 1420 * 1421 * A single block is written, starting at the specified offset in bytes. If 1422 * the call is successful, it returns 0 and the provided abd has been 1423 * consumed (the caller should not free it). 1424 */ 1425 int 1426 dmu_lightweight_write_by_dnode(dnode_t *dn, uint64_t offset, abd_t *abd, 1427 const zio_prop_t *zp, enum zio_flag flags, dmu_tx_t *tx) 1428 { 1429 dbuf_dirty_record_t *dr = 1430 dbuf_dirty_lightweight(dn, dbuf_whichblock(dn, 0, offset), tx); 1431 if (dr == NULL) 1432 return (SET_ERROR(EIO)); 1433 dr->dt.dll.dr_abd = abd; 1434 dr->dt.dll.dr_props = *zp; 1435 dr->dt.dll.dr_flags = flags; 1436 return (0); 1437 } 1438 1439 /* 1440 * When possible directly assign passed loaned arc buffer to a dbuf. 1441 * If this is not possible copy the contents of passed arc buf via 1442 * dmu_write(). 1443 */ 1444 int 1445 dmu_assign_arcbuf_by_dnode(dnode_t *dn, uint64_t offset, arc_buf_t *buf, 1446 dmu_tx_t *tx) 1447 { 1448 dmu_buf_impl_t *db; 1449 objset_t *os = dn->dn_objset; 1450 uint64_t object = dn->dn_object; 1451 uint32_t blksz = (uint32_t)arc_buf_lsize(buf); 1452 uint64_t blkid; 1453 1454 rw_enter(&dn->dn_struct_rwlock, RW_READER); 1455 blkid = dbuf_whichblock(dn, 0, offset); 1456 db = dbuf_hold(dn, blkid, FTAG); 1457 if (db == NULL) 1458 return (SET_ERROR(EIO)); 1459 rw_exit(&dn->dn_struct_rwlock); 1460 1461 /* 1462 * We can only assign if the offset is aligned and the arc buf is the 1463 * same size as the dbuf. 1464 */ 1465 if (offset == db->db.db_offset && blksz == db->db.db_size) { 1466 zfs_racct_write(blksz, 1); 1467 dbuf_assign_arcbuf(db, buf, tx); 1468 dbuf_rele(db, FTAG); 1469 } else { 1470 /* compressed bufs must always be assignable to their dbuf */ 1471 ASSERT3U(arc_get_compression(buf), ==, ZIO_COMPRESS_OFF); 1472 ASSERT(!(buf->b_flags & ARC_BUF_FLAG_COMPRESSED)); 1473 1474 dbuf_rele(db, FTAG); 1475 dmu_write(os, object, offset, blksz, buf->b_data, tx); 1476 dmu_return_arcbuf(buf); 1477 } 1478 1479 return (0); 1480 } 1481 1482 int 1483 dmu_assign_arcbuf_by_dbuf(dmu_buf_t *handle, uint64_t offset, arc_buf_t *buf, 1484 dmu_tx_t *tx) 1485 { 1486 int err; 1487 dmu_buf_impl_t *dbuf = (dmu_buf_impl_t *)handle; 1488 1489 DB_DNODE_ENTER(dbuf); 1490 err = dmu_assign_arcbuf_by_dnode(DB_DNODE(dbuf), offset, buf, tx); 1491 DB_DNODE_EXIT(dbuf); 1492 1493 return (err); 1494 } 1495 1496 typedef struct { 1497 dbuf_dirty_record_t *dsa_dr; 1498 dmu_sync_cb_t *dsa_done; 1499 zgd_t *dsa_zgd; 1500 dmu_tx_t *dsa_tx; 1501 } dmu_sync_arg_t; 1502 1503 /* ARGSUSED */ 1504 static void 1505 dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg) 1506 { 1507 dmu_sync_arg_t *dsa = varg; 1508 dmu_buf_t *db = dsa->dsa_zgd->zgd_db; 1509 blkptr_t *bp = zio->io_bp; 1510 1511 if (zio->io_error == 0) { 1512 if (BP_IS_HOLE(bp)) { 1513 /* 1514 * A block of zeros may compress to a hole, but the 1515 * block size still needs to be known for replay. 1516 */ 1517 BP_SET_LSIZE(bp, db->db_size); 1518 } else if (!BP_IS_EMBEDDED(bp)) { 1519 ASSERT(BP_GET_LEVEL(bp) == 0); 1520 BP_SET_FILL(bp, 1); 1521 } 1522 } 1523 } 1524 1525 static void 1526 dmu_sync_late_arrival_ready(zio_t *zio) 1527 { 1528 dmu_sync_ready(zio, NULL, zio->io_private); 1529 } 1530 1531 /* ARGSUSED */ 1532 static void 1533 dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg) 1534 { 1535 dmu_sync_arg_t *dsa = varg; 1536 dbuf_dirty_record_t *dr = dsa->dsa_dr; 1537 dmu_buf_impl_t *db = dr->dr_dbuf; 1538 zgd_t *zgd = dsa->dsa_zgd; 1539 1540 /* 1541 * Record the vdev(s) backing this blkptr so they can be flushed after 1542 * the writes for the lwb have completed. 1543 */ 1544 if (zio->io_error == 0) { 1545 zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp); 1546 } 1547 1548 mutex_enter(&db->db_mtx); 1549 ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC); 1550 if (zio->io_error == 0) { 1551 dr->dt.dl.dr_nopwrite = !!(zio->io_flags & ZIO_FLAG_NOPWRITE); 1552 if (dr->dt.dl.dr_nopwrite) { 1553 blkptr_t *bp = zio->io_bp; 1554 blkptr_t *bp_orig = &zio->io_bp_orig; 1555 uint8_t chksum = BP_GET_CHECKSUM(bp_orig); 1556 1557 ASSERT(BP_EQUAL(bp, bp_orig)); 1558 VERIFY(BP_EQUAL(bp, db->db_blkptr)); 1559 ASSERT(zio->io_prop.zp_compress != ZIO_COMPRESS_OFF); 1560 VERIFY(zio_checksum_table[chksum].ci_flags & 1561 ZCHECKSUM_FLAG_NOPWRITE); 1562 } 1563 dr->dt.dl.dr_overridden_by = *zio->io_bp; 1564 dr->dt.dl.dr_override_state = DR_OVERRIDDEN; 1565 dr->dt.dl.dr_copies = zio->io_prop.zp_copies; 1566 1567 /* 1568 * Old style holes are filled with all zeros, whereas 1569 * new-style holes maintain their lsize, type, level, 1570 * and birth time (see zio_write_compress). While we 1571 * need to reset the BP_SET_LSIZE() call that happened 1572 * in dmu_sync_ready for old style holes, we do *not* 1573 * want to wipe out the information contained in new 1574 * style holes. Thus, only zero out the block pointer if 1575 * it's an old style hole. 1576 */ 1577 if (BP_IS_HOLE(&dr->dt.dl.dr_overridden_by) && 1578 dr->dt.dl.dr_overridden_by.blk_birth == 0) 1579 BP_ZERO(&dr->dt.dl.dr_overridden_by); 1580 } else { 1581 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN; 1582 } 1583 cv_broadcast(&db->db_changed); 1584 mutex_exit(&db->db_mtx); 1585 1586 dsa->dsa_done(dsa->dsa_zgd, zio->io_error); 1587 1588 kmem_free(dsa, sizeof (*dsa)); 1589 } 1590 1591 static void 1592 dmu_sync_late_arrival_done(zio_t *zio) 1593 { 1594 blkptr_t *bp = zio->io_bp; 1595 dmu_sync_arg_t *dsa = zio->io_private; 1596 zgd_t *zgd = dsa->dsa_zgd; 1597 1598 if (zio->io_error == 0) { 1599 /* 1600 * Record the vdev(s) backing this blkptr so they can be 1601 * flushed after the writes for the lwb have completed. 1602 */ 1603 zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp); 1604 1605 if (!BP_IS_HOLE(bp)) { 1606 blkptr_t *bp_orig __maybe_unused = &zio->io_bp_orig; 1607 ASSERT(!(zio->io_flags & ZIO_FLAG_NOPWRITE)); 1608 ASSERT(BP_IS_HOLE(bp_orig) || !BP_EQUAL(bp, bp_orig)); 1609 ASSERT(zio->io_bp->blk_birth == zio->io_txg); 1610 ASSERT(zio->io_txg > spa_syncing_txg(zio->io_spa)); 1611 zio_free(zio->io_spa, zio->io_txg, zio->io_bp); 1612 } 1613 } 1614 1615 dmu_tx_commit(dsa->dsa_tx); 1616 1617 dsa->dsa_done(dsa->dsa_zgd, zio->io_error); 1618 1619 abd_free(zio->io_abd); 1620 kmem_free(dsa, sizeof (*dsa)); 1621 } 1622 1623 static int 1624 dmu_sync_late_arrival(zio_t *pio, objset_t *os, dmu_sync_cb_t *done, zgd_t *zgd, 1625 zio_prop_t *zp, zbookmark_phys_t *zb) 1626 { 1627 dmu_sync_arg_t *dsa; 1628 dmu_tx_t *tx; 1629 1630 tx = dmu_tx_create(os); 1631 dmu_tx_hold_space(tx, zgd->zgd_db->db_size); 1632 if (dmu_tx_assign(tx, TXG_WAIT) != 0) { 1633 dmu_tx_abort(tx); 1634 /* Make zl_get_data do txg_waited_synced() */ 1635 return (SET_ERROR(EIO)); 1636 } 1637 1638 /* 1639 * In order to prevent the zgd's lwb from being free'd prior to 1640 * dmu_sync_late_arrival_done() being called, we have to ensure 1641 * the lwb's "max txg" takes this tx's txg into account. 1642 */ 1643 zil_lwb_add_txg(zgd->zgd_lwb, dmu_tx_get_txg(tx)); 1644 1645 dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP); 1646 dsa->dsa_dr = NULL; 1647 dsa->dsa_done = done; 1648 dsa->dsa_zgd = zgd; 1649 dsa->dsa_tx = tx; 1650 1651 /* 1652 * Since we are currently syncing this txg, it's nontrivial to 1653 * determine what BP to nopwrite against, so we disable nopwrite. 1654 * 1655 * When syncing, the db_blkptr is initially the BP of the previous 1656 * txg. We can not nopwrite against it because it will be changed 1657 * (this is similar to the non-late-arrival case where the dbuf is 1658 * dirty in a future txg). 1659 * 1660 * Then dbuf_write_ready() sets bp_blkptr to the location we will write. 1661 * We can not nopwrite against it because although the BP will not 1662 * (typically) be changed, the data has not yet been persisted to this 1663 * location. 1664 * 1665 * Finally, when dbuf_write_done() is called, it is theoretically 1666 * possible to always nopwrite, because the data that was written in 1667 * this txg is the same data that we are trying to write. However we 1668 * would need to check that this dbuf is not dirty in any future 1669 * txg's (as we do in the normal dmu_sync() path). For simplicity, we 1670 * don't nopwrite in this case. 1671 */ 1672 zp->zp_nopwrite = B_FALSE; 1673 1674 zio_nowait(zio_write(pio, os->os_spa, dmu_tx_get_txg(tx), zgd->zgd_bp, 1675 abd_get_from_buf(zgd->zgd_db->db_data, zgd->zgd_db->db_size), 1676 zgd->zgd_db->db_size, zgd->zgd_db->db_size, zp, 1677 dmu_sync_late_arrival_ready, NULL, NULL, dmu_sync_late_arrival_done, 1678 dsa, ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, zb)); 1679 1680 return (0); 1681 } 1682 1683 /* 1684 * Intent log support: sync the block associated with db to disk. 1685 * N.B. and XXX: the caller is responsible for making sure that the 1686 * data isn't changing while dmu_sync() is writing it. 1687 * 1688 * Return values: 1689 * 1690 * EEXIST: this txg has already been synced, so there's nothing to do. 1691 * The caller should not log the write. 1692 * 1693 * ENOENT: the block was dbuf_free_range()'d, so there's nothing to do. 1694 * The caller should not log the write. 1695 * 1696 * EALREADY: this block is already in the process of being synced. 1697 * The caller should track its progress (somehow). 1698 * 1699 * EIO: could not do the I/O. 1700 * The caller should do a txg_wait_synced(). 1701 * 1702 * 0: the I/O has been initiated. 1703 * The caller should log this blkptr in the done callback. 1704 * It is possible that the I/O will fail, in which case 1705 * the error will be reported to the done callback and 1706 * propagated to pio from zio_done(). 1707 */ 1708 int 1709 dmu_sync(zio_t *pio, uint64_t txg, dmu_sync_cb_t *done, zgd_t *zgd) 1710 { 1711 dmu_buf_impl_t *db = (dmu_buf_impl_t *)zgd->zgd_db; 1712 objset_t *os = db->db_objset; 1713 dsl_dataset_t *ds = os->os_dsl_dataset; 1714 dbuf_dirty_record_t *dr, *dr_next; 1715 dmu_sync_arg_t *dsa; 1716 zbookmark_phys_t zb; 1717 zio_prop_t zp; 1718 dnode_t *dn; 1719 1720 ASSERT(pio != NULL); 1721 ASSERT(txg != 0); 1722 1723 SET_BOOKMARK(&zb, ds->ds_object, 1724 db->db.db_object, db->db_level, db->db_blkid); 1725 1726 DB_DNODE_ENTER(db); 1727 dn = DB_DNODE(db); 1728 dmu_write_policy(os, dn, db->db_level, WP_DMU_SYNC, &zp); 1729 DB_DNODE_EXIT(db); 1730 1731 /* 1732 * If we're frozen (running ziltest), we always need to generate a bp. 1733 */ 1734 if (txg > spa_freeze_txg(os->os_spa)) 1735 return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb)); 1736 1737 /* 1738 * Grabbing db_mtx now provides a barrier between dbuf_sync_leaf() 1739 * and us. If we determine that this txg is not yet syncing, 1740 * but it begins to sync a moment later, that's OK because the 1741 * sync thread will block in dbuf_sync_leaf() until we drop db_mtx. 1742 */ 1743 mutex_enter(&db->db_mtx); 1744 1745 if (txg <= spa_last_synced_txg(os->os_spa)) { 1746 /* 1747 * This txg has already synced. There's nothing to do. 1748 */ 1749 mutex_exit(&db->db_mtx); 1750 return (SET_ERROR(EEXIST)); 1751 } 1752 1753 if (txg <= spa_syncing_txg(os->os_spa)) { 1754 /* 1755 * This txg is currently syncing, so we can't mess with 1756 * the dirty record anymore; just write a new log block. 1757 */ 1758 mutex_exit(&db->db_mtx); 1759 return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb)); 1760 } 1761 1762 dr = dbuf_find_dirty_eq(db, txg); 1763 1764 if (dr == NULL) { 1765 /* 1766 * There's no dr for this dbuf, so it must have been freed. 1767 * There's no need to log writes to freed blocks, so we're done. 1768 */ 1769 mutex_exit(&db->db_mtx); 1770 return (SET_ERROR(ENOENT)); 1771 } 1772 1773 dr_next = list_next(&db->db_dirty_records, dr); 1774 ASSERT(dr_next == NULL || dr_next->dr_txg < txg); 1775 1776 if (db->db_blkptr != NULL) { 1777 /* 1778 * We need to fill in zgd_bp with the current blkptr so that 1779 * the nopwrite code can check if we're writing the same 1780 * data that's already on disk. We can only nopwrite if we 1781 * are sure that after making the copy, db_blkptr will not 1782 * change until our i/o completes. We ensure this by 1783 * holding the db_mtx, and only allowing nopwrite if the 1784 * block is not already dirty (see below). This is verified 1785 * by dmu_sync_done(), which VERIFYs that the db_blkptr has 1786 * not changed. 1787 */ 1788 *zgd->zgd_bp = *db->db_blkptr; 1789 } 1790 1791 /* 1792 * Assume the on-disk data is X, the current syncing data (in 1793 * txg - 1) is Y, and the current in-memory data is Z (currently 1794 * in dmu_sync). 1795 * 1796 * We usually want to perform a nopwrite if X and Z are the 1797 * same. However, if Y is different (i.e. the BP is going to 1798 * change before this write takes effect), then a nopwrite will 1799 * be incorrect - we would override with X, which could have 1800 * been freed when Y was written. 1801 * 1802 * (Note that this is not a concern when we are nop-writing from 1803 * syncing context, because X and Y must be identical, because 1804 * all previous txgs have been synced.) 1805 * 1806 * Therefore, we disable nopwrite if the current BP could change 1807 * before this TXG. There are two ways it could change: by 1808 * being dirty (dr_next is non-NULL), or by being freed 1809 * (dnode_block_freed()). This behavior is verified by 1810 * zio_done(), which VERIFYs that the override BP is identical 1811 * to the on-disk BP. 1812 */ 1813 DB_DNODE_ENTER(db); 1814 dn = DB_DNODE(db); 1815 if (dr_next != NULL || dnode_block_freed(dn, db->db_blkid)) 1816 zp.zp_nopwrite = B_FALSE; 1817 DB_DNODE_EXIT(db); 1818 1819 ASSERT(dr->dr_txg == txg); 1820 if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC || 1821 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) { 1822 /* 1823 * We have already issued a sync write for this buffer, 1824 * or this buffer has already been synced. It could not 1825 * have been dirtied since, or we would have cleared the state. 1826 */ 1827 mutex_exit(&db->db_mtx); 1828 return (SET_ERROR(EALREADY)); 1829 } 1830 1831 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN); 1832 dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC; 1833 mutex_exit(&db->db_mtx); 1834 1835 dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP); 1836 dsa->dsa_dr = dr; 1837 dsa->dsa_done = done; 1838 dsa->dsa_zgd = zgd; 1839 dsa->dsa_tx = NULL; 1840 1841 zio_nowait(arc_write(pio, os->os_spa, txg, 1842 zgd->zgd_bp, dr->dt.dl.dr_data, DBUF_IS_L2CACHEABLE(db), 1843 &zp, dmu_sync_ready, NULL, NULL, dmu_sync_done, dsa, 1844 ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, &zb)); 1845 1846 return (0); 1847 } 1848 1849 int 1850 dmu_object_set_nlevels(objset_t *os, uint64_t object, int nlevels, dmu_tx_t *tx) 1851 { 1852 dnode_t *dn; 1853 int err; 1854 1855 err = dnode_hold(os, object, FTAG, &dn); 1856 if (err) 1857 return (err); 1858 err = dnode_set_nlevels(dn, nlevels, tx); 1859 dnode_rele(dn, FTAG); 1860 return (err); 1861 } 1862 1863 int 1864 dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs, 1865 dmu_tx_t *tx) 1866 { 1867 dnode_t *dn; 1868 int err; 1869 1870 err = dnode_hold(os, object, FTAG, &dn); 1871 if (err) 1872 return (err); 1873 err = dnode_set_blksz(dn, size, ibs, tx); 1874 dnode_rele(dn, FTAG); 1875 return (err); 1876 } 1877 1878 int 1879 dmu_object_set_maxblkid(objset_t *os, uint64_t object, uint64_t maxblkid, 1880 dmu_tx_t *tx) 1881 { 1882 dnode_t *dn; 1883 int err; 1884 1885 err = dnode_hold(os, object, FTAG, &dn); 1886 if (err) 1887 return (err); 1888 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 1889 dnode_new_blkid(dn, maxblkid, tx, B_FALSE, B_TRUE); 1890 rw_exit(&dn->dn_struct_rwlock); 1891 dnode_rele(dn, FTAG); 1892 return (0); 1893 } 1894 1895 void 1896 dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum, 1897 dmu_tx_t *tx) 1898 { 1899 dnode_t *dn; 1900 1901 /* 1902 * Send streams include each object's checksum function. This 1903 * check ensures that the receiving system can understand the 1904 * checksum function transmitted. 1905 */ 1906 ASSERT3U(checksum, <, ZIO_CHECKSUM_LEGACY_FUNCTIONS); 1907 1908 VERIFY0(dnode_hold(os, object, FTAG, &dn)); 1909 ASSERT3U(checksum, <, ZIO_CHECKSUM_FUNCTIONS); 1910 dn->dn_checksum = checksum; 1911 dnode_setdirty(dn, tx); 1912 dnode_rele(dn, FTAG); 1913 } 1914 1915 void 1916 dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress, 1917 dmu_tx_t *tx) 1918 { 1919 dnode_t *dn; 1920 1921 /* 1922 * Send streams include each object's compression function. This 1923 * check ensures that the receiving system can understand the 1924 * compression function transmitted. 1925 */ 1926 ASSERT3U(compress, <, ZIO_COMPRESS_LEGACY_FUNCTIONS); 1927 1928 VERIFY0(dnode_hold(os, object, FTAG, &dn)); 1929 dn->dn_compress = compress; 1930 dnode_setdirty(dn, tx); 1931 dnode_rele(dn, FTAG); 1932 } 1933 1934 /* 1935 * When the "redundant_metadata" property is set to "most", only indirect 1936 * blocks of this level and higher will have an additional ditto block. 1937 */ 1938 int zfs_redundant_metadata_most_ditto_level = 2; 1939 1940 void 1941 dmu_write_policy(objset_t *os, dnode_t *dn, int level, int wp, zio_prop_t *zp) 1942 { 1943 dmu_object_type_t type = dn ? dn->dn_type : DMU_OT_OBJSET; 1944 boolean_t ismd = (level > 0 || DMU_OT_IS_METADATA(type) || 1945 (wp & WP_SPILL)); 1946 enum zio_checksum checksum = os->os_checksum; 1947 enum zio_compress compress = os->os_compress; 1948 uint8_t complevel = os->os_complevel; 1949 enum zio_checksum dedup_checksum = os->os_dedup_checksum; 1950 boolean_t dedup = B_FALSE; 1951 boolean_t nopwrite = B_FALSE; 1952 boolean_t dedup_verify = os->os_dedup_verify; 1953 boolean_t encrypt = B_FALSE; 1954 int copies = os->os_copies; 1955 1956 /* 1957 * We maintain different write policies for each of the following 1958 * types of data: 1959 * 1. metadata 1960 * 2. preallocated blocks (i.e. level-0 blocks of a dump device) 1961 * 3. all other level 0 blocks 1962 */ 1963 if (ismd) { 1964 /* 1965 * XXX -- we should design a compression algorithm 1966 * that specializes in arrays of bps. 1967 */ 1968 compress = zio_compress_select(os->os_spa, 1969 ZIO_COMPRESS_ON, ZIO_COMPRESS_ON); 1970 1971 /* 1972 * Metadata always gets checksummed. If the data 1973 * checksum is multi-bit correctable, and it's not a 1974 * ZBT-style checksum, then it's suitable for metadata 1975 * as well. Otherwise, the metadata checksum defaults 1976 * to fletcher4. 1977 */ 1978 if (!(zio_checksum_table[checksum].ci_flags & 1979 ZCHECKSUM_FLAG_METADATA) || 1980 (zio_checksum_table[checksum].ci_flags & 1981 ZCHECKSUM_FLAG_EMBEDDED)) 1982 checksum = ZIO_CHECKSUM_FLETCHER_4; 1983 1984 if (os->os_redundant_metadata == ZFS_REDUNDANT_METADATA_ALL || 1985 (os->os_redundant_metadata == 1986 ZFS_REDUNDANT_METADATA_MOST && 1987 (level >= zfs_redundant_metadata_most_ditto_level || 1988 DMU_OT_IS_METADATA(type) || (wp & WP_SPILL)))) 1989 copies++; 1990 } else if (wp & WP_NOFILL) { 1991 ASSERT(level == 0); 1992 1993 /* 1994 * If we're writing preallocated blocks, we aren't actually 1995 * writing them so don't set any policy properties. These 1996 * blocks are currently only used by an external subsystem 1997 * outside of zfs (i.e. dump) and not written by the zio 1998 * pipeline. 1999 */ 2000 compress = ZIO_COMPRESS_OFF; 2001 checksum = ZIO_CHECKSUM_OFF; 2002 } else { 2003 compress = zio_compress_select(os->os_spa, dn->dn_compress, 2004 compress); 2005 complevel = zio_complevel_select(os->os_spa, compress, 2006 complevel, complevel); 2007 2008 checksum = (dedup_checksum == ZIO_CHECKSUM_OFF) ? 2009 zio_checksum_select(dn->dn_checksum, checksum) : 2010 dedup_checksum; 2011 2012 /* 2013 * Determine dedup setting. If we are in dmu_sync(), 2014 * we won't actually dedup now because that's all 2015 * done in syncing context; but we do want to use the 2016 * dedup checksum. If the checksum is not strong 2017 * enough to ensure unique signatures, force 2018 * dedup_verify. 2019 */ 2020 if (dedup_checksum != ZIO_CHECKSUM_OFF) { 2021 dedup = (wp & WP_DMU_SYNC) ? B_FALSE : B_TRUE; 2022 if (!(zio_checksum_table[checksum].ci_flags & 2023 ZCHECKSUM_FLAG_DEDUP)) 2024 dedup_verify = B_TRUE; 2025 } 2026 2027 /* 2028 * Enable nopwrite if we have secure enough checksum 2029 * algorithm (see comment in zio_nop_write) and 2030 * compression is enabled. We don't enable nopwrite if 2031 * dedup is enabled as the two features are mutually 2032 * exclusive. 2033 */ 2034 nopwrite = (!dedup && (zio_checksum_table[checksum].ci_flags & 2035 ZCHECKSUM_FLAG_NOPWRITE) && 2036 compress != ZIO_COMPRESS_OFF && zfs_nopwrite_enabled); 2037 } 2038 2039 /* 2040 * All objects in an encrypted objset are protected from modification 2041 * via a MAC. Encrypted objects store their IV and salt in the last DVA 2042 * in the bp, so we cannot use all copies. Encrypted objects are also 2043 * not subject to nopwrite since writing the same data will still 2044 * result in a new ciphertext. Only encrypted blocks can be dedup'd 2045 * to avoid ambiguity in the dedup code since the DDT does not store 2046 * object types. 2047 */ 2048 if (os->os_encrypted && (wp & WP_NOFILL) == 0) { 2049 encrypt = B_TRUE; 2050 2051 if (DMU_OT_IS_ENCRYPTED(type)) { 2052 copies = MIN(copies, SPA_DVAS_PER_BP - 1); 2053 nopwrite = B_FALSE; 2054 } else { 2055 dedup = B_FALSE; 2056 } 2057 2058 if (level <= 0 && 2059 (type == DMU_OT_DNODE || type == DMU_OT_OBJSET)) { 2060 compress = ZIO_COMPRESS_EMPTY; 2061 } 2062 } 2063 2064 zp->zp_compress = compress; 2065 zp->zp_complevel = complevel; 2066 zp->zp_checksum = checksum; 2067 zp->zp_type = (wp & WP_SPILL) ? dn->dn_bonustype : type; 2068 zp->zp_level = level; 2069 zp->zp_copies = MIN(copies, spa_max_replication(os->os_spa)); 2070 zp->zp_dedup = dedup; 2071 zp->zp_dedup_verify = dedup && dedup_verify; 2072 zp->zp_nopwrite = nopwrite; 2073 zp->zp_encrypt = encrypt; 2074 zp->zp_byteorder = ZFS_HOST_BYTEORDER; 2075 bzero(zp->zp_salt, ZIO_DATA_SALT_LEN); 2076 bzero(zp->zp_iv, ZIO_DATA_IV_LEN); 2077 bzero(zp->zp_mac, ZIO_DATA_MAC_LEN); 2078 zp->zp_zpl_smallblk = DMU_OT_IS_FILE(zp->zp_type) ? 2079 os->os_zpl_special_smallblock : 0; 2080 2081 ASSERT3U(zp->zp_compress, !=, ZIO_COMPRESS_INHERIT); 2082 } 2083 2084 /* 2085 * This function is only called from zfs_holey_common() for zpl_llseek() 2086 * in order to determine the location of holes. In order to accurately 2087 * report holes all dirty data must be synced to disk. This causes extremely 2088 * poor performance when seeking for holes in a dirty file. As a compromise, 2089 * only provide hole data when the dnode is clean. When a dnode is dirty 2090 * report the dnode as having no holes which is always a safe thing to do. 2091 */ 2092 int 2093 dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off) 2094 { 2095 dnode_t *dn; 2096 int i, err; 2097 boolean_t clean = B_TRUE; 2098 2099 err = dnode_hold(os, object, FTAG, &dn); 2100 if (err) 2101 return (err); 2102 2103 /* 2104 * Check if dnode is dirty 2105 */ 2106 for (i = 0; i < TXG_SIZE; i++) { 2107 if (multilist_link_active(&dn->dn_dirty_link[i])) { 2108 clean = B_FALSE; 2109 break; 2110 } 2111 } 2112 2113 /* 2114 * If compatibility option is on, sync any current changes before 2115 * we go trundling through the block pointers. 2116 */ 2117 if (!clean && zfs_dmu_offset_next_sync) { 2118 clean = B_TRUE; 2119 dnode_rele(dn, FTAG); 2120 txg_wait_synced(dmu_objset_pool(os), 0); 2121 err = dnode_hold(os, object, FTAG, &dn); 2122 if (err) 2123 return (err); 2124 } 2125 2126 if (clean) 2127 err = dnode_next_offset(dn, 2128 (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0); 2129 else 2130 err = SET_ERROR(EBUSY); 2131 2132 dnode_rele(dn, FTAG); 2133 2134 return (err); 2135 } 2136 2137 void 2138 __dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi) 2139 { 2140 dnode_phys_t *dnp = dn->dn_phys; 2141 2142 doi->doi_data_block_size = dn->dn_datablksz; 2143 doi->doi_metadata_block_size = dn->dn_indblkshift ? 2144 1ULL << dn->dn_indblkshift : 0; 2145 doi->doi_type = dn->dn_type; 2146 doi->doi_bonus_type = dn->dn_bonustype; 2147 doi->doi_bonus_size = dn->dn_bonuslen; 2148 doi->doi_dnodesize = dn->dn_num_slots << DNODE_SHIFT; 2149 doi->doi_indirection = dn->dn_nlevels; 2150 doi->doi_checksum = dn->dn_checksum; 2151 doi->doi_compress = dn->dn_compress; 2152 doi->doi_nblkptr = dn->dn_nblkptr; 2153 doi->doi_physical_blocks_512 = (DN_USED_BYTES(dnp) + 256) >> 9; 2154 doi->doi_max_offset = (dn->dn_maxblkid + 1) * dn->dn_datablksz; 2155 doi->doi_fill_count = 0; 2156 for (int i = 0; i < dnp->dn_nblkptr; i++) 2157 doi->doi_fill_count += BP_GET_FILL(&dnp->dn_blkptr[i]); 2158 } 2159 2160 void 2161 dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi) 2162 { 2163 rw_enter(&dn->dn_struct_rwlock, RW_READER); 2164 mutex_enter(&dn->dn_mtx); 2165 2166 __dmu_object_info_from_dnode(dn, doi); 2167 2168 mutex_exit(&dn->dn_mtx); 2169 rw_exit(&dn->dn_struct_rwlock); 2170 } 2171 2172 /* 2173 * Get information on a DMU object. 2174 * If doi is NULL, just indicates whether the object exists. 2175 */ 2176 int 2177 dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi) 2178 { 2179 dnode_t *dn; 2180 int err = dnode_hold(os, object, FTAG, &dn); 2181 2182 if (err) 2183 return (err); 2184 2185 if (doi != NULL) 2186 dmu_object_info_from_dnode(dn, doi); 2187 2188 dnode_rele(dn, FTAG); 2189 return (0); 2190 } 2191 2192 /* 2193 * As above, but faster; can be used when you have a held dbuf in hand. 2194 */ 2195 void 2196 dmu_object_info_from_db(dmu_buf_t *db_fake, dmu_object_info_t *doi) 2197 { 2198 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2199 2200 DB_DNODE_ENTER(db); 2201 dmu_object_info_from_dnode(DB_DNODE(db), doi); 2202 DB_DNODE_EXIT(db); 2203 } 2204 2205 /* 2206 * Faster still when you only care about the size. 2207 */ 2208 void 2209 dmu_object_size_from_db(dmu_buf_t *db_fake, uint32_t *blksize, 2210 u_longlong_t *nblk512) 2211 { 2212 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2213 dnode_t *dn; 2214 2215 DB_DNODE_ENTER(db); 2216 dn = DB_DNODE(db); 2217 2218 *blksize = dn->dn_datablksz; 2219 /* add in number of slots used for the dnode itself */ 2220 *nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >> 2221 SPA_MINBLOCKSHIFT) + dn->dn_num_slots; 2222 DB_DNODE_EXIT(db); 2223 } 2224 2225 void 2226 dmu_object_dnsize_from_db(dmu_buf_t *db_fake, int *dnsize) 2227 { 2228 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2229 dnode_t *dn; 2230 2231 DB_DNODE_ENTER(db); 2232 dn = DB_DNODE(db); 2233 *dnsize = dn->dn_num_slots << DNODE_SHIFT; 2234 DB_DNODE_EXIT(db); 2235 } 2236 2237 void 2238 byteswap_uint64_array(void *vbuf, size_t size) 2239 { 2240 uint64_t *buf = vbuf; 2241 size_t count = size >> 3; 2242 int i; 2243 2244 ASSERT((size & 7) == 0); 2245 2246 for (i = 0; i < count; i++) 2247 buf[i] = BSWAP_64(buf[i]); 2248 } 2249 2250 void 2251 byteswap_uint32_array(void *vbuf, size_t size) 2252 { 2253 uint32_t *buf = vbuf; 2254 size_t count = size >> 2; 2255 int i; 2256 2257 ASSERT((size & 3) == 0); 2258 2259 for (i = 0; i < count; i++) 2260 buf[i] = BSWAP_32(buf[i]); 2261 } 2262 2263 void 2264 byteswap_uint16_array(void *vbuf, size_t size) 2265 { 2266 uint16_t *buf = vbuf; 2267 size_t count = size >> 1; 2268 int i; 2269 2270 ASSERT((size & 1) == 0); 2271 2272 for (i = 0; i < count; i++) 2273 buf[i] = BSWAP_16(buf[i]); 2274 } 2275 2276 /* ARGSUSED */ 2277 void 2278 byteswap_uint8_array(void *vbuf, size_t size) 2279 { 2280 } 2281 2282 void 2283 dmu_init(void) 2284 { 2285 abd_init(); 2286 zfs_dbgmsg_init(); 2287 sa_cache_init(); 2288 dmu_objset_init(); 2289 dnode_init(); 2290 zfetch_init(); 2291 dmu_tx_init(); 2292 l2arc_init(); 2293 arc_init(); 2294 dbuf_init(); 2295 } 2296 2297 void 2298 dmu_fini(void) 2299 { 2300 arc_fini(); /* arc depends on l2arc, so arc must go first */ 2301 l2arc_fini(); 2302 dmu_tx_fini(); 2303 zfetch_fini(); 2304 dbuf_fini(); 2305 dnode_fini(); 2306 dmu_objset_fini(); 2307 sa_cache_fini(); 2308 zfs_dbgmsg_fini(); 2309 abd_fini(); 2310 } 2311 2312 EXPORT_SYMBOL(dmu_bonus_hold); 2313 EXPORT_SYMBOL(dmu_bonus_hold_by_dnode); 2314 EXPORT_SYMBOL(dmu_buf_hold_array_by_bonus); 2315 EXPORT_SYMBOL(dmu_buf_rele_array); 2316 EXPORT_SYMBOL(dmu_prefetch); 2317 EXPORT_SYMBOL(dmu_free_range); 2318 EXPORT_SYMBOL(dmu_free_long_range); 2319 EXPORT_SYMBOL(dmu_free_long_object); 2320 EXPORT_SYMBOL(dmu_read); 2321 EXPORT_SYMBOL(dmu_read_by_dnode); 2322 EXPORT_SYMBOL(dmu_write); 2323 EXPORT_SYMBOL(dmu_write_by_dnode); 2324 EXPORT_SYMBOL(dmu_prealloc); 2325 EXPORT_SYMBOL(dmu_object_info); 2326 EXPORT_SYMBOL(dmu_object_info_from_dnode); 2327 EXPORT_SYMBOL(dmu_object_info_from_db); 2328 EXPORT_SYMBOL(dmu_object_size_from_db); 2329 EXPORT_SYMBOL(dmu_object_dnsize_from_db); 2330 EXPORT_SYMBOL(dmu_object_set_nlevels); 2331 EXPORT_SYMBOL(dmu_object_set_blocksize); 2332 EXPORT_SYMBOL(dmu_object_set_maxblkid); 2333 EXPORT_SYMBOL(dmu_object_set_checksum); 2334 EXPORT_SYMBOL(dmu_object_set_compress); 2335 EXPORT_SYMBOL(dmu_offset_next); 2336 EXPORT_SYMBOL(dmu_write_policy); 2337 EXPORT_SYMBOL(dmu_sync); 2338 EXPORT_SYMBOL(dmu_request_arcbuf); 2339 EXPORT_SYMBOL(dmu_return_arcbuf); 2340 EXPORT_SYMBOL(dmu_assign_arcbuf_by_dnode); 2341 EXPORT_SYMBOL(dmu_assign_arcbuf_by_dbuf); 2342 EXPORT_SYMBOL(dmu_buf_hold); 2343 EXPORT_SYMBOL(dmu_ot); 2344 2345 /* BEGIN CSTYLED */ 2346 ZFS_MODULE_PARAM(zfs, zfs_, nopwrite_enabled, INT, ZMOD_RW, 2347 "Enable NOP writes"); 2348 2349 ZFS_MODULE_PARAM(zfs, zfs_, per_txg_dirty_frees_percent, ULONG, ZMOD_RW, 2350 "Percentage of dirtied blocks from frees in one TXG"); 2351 2352 ZFS_MODULE_PARAM(zfs, zfs_, dmu_offset_next_sync, INT, ZMOD_RW, 2353 "Enable forcing txg sync to find holes"); 2354 2355 ZFS_MODULE_PARAM(zfs, , dmu_prefetch_max, INT, ZMOD_RW, 2356 "Limit one prefetch call to this size"); 2357 /* END CSTYLED */ 2358