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